8250_bcm2835aux.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Serial port driver for BCM2835AUX UART
  4. *
  5. * Copyright (C) 2016 Martin Sperl <[email protected]>
  6. *
  7. * Based on 8250_lpc18xx.c:
  8. * Copyright (C) 2015 Joachim Eastwood <[email protected]>
  9. *
  10. * The bcm2835aux is capable of RTS auto flow-control, but this driver doesn't
  11. * take advantage of it yet. When adding support, be sure not to enable it
  12. * simultaneously to rs485.
  13. */
  14. #include <linux/clk.h>
  15. #include <linux/io.h>
  16. #include <linux/module.h>
  17. #include <linux/of.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/property.h>
  20. #include "8250.h"
  21. #define BCM2835_AUX_UART_CNTL 8
  22. #define BCM2835_AUX_UART_CNTL_RXEN 0x01 /* Receiver enable */
  23. #define BCM2835_AUX_UART_CNTL_TXEN 0x02 /* Transmitter enable */
  24. #define BCM2835_AUX_UART_CNTL_AUTORTS 0x04 /* RTS set by RX fill level */
  25. #define BCM2835_AUX_UART_CNTL_AUTOCTS 0x08 /* CTS stops transmitter */
  26. #define BCM2835_AUX_UART_CNTL_RTS3 0x00 /* RTS set until 3 chars left */
  27. #define BCM2835_AUX_UART_CNTL_RTS2 0x10 /* RTS set until 2 chars left */
  28. #define BCM2835_AUX_UART_CNTL_RTS1 0x20 /* RTS set until 1 chars left */
  29. #define BCM2835_AUX_UART_CNTL_RTS4 0x30 /* RTS set until 4 chars left */
  30. #define BCM2835_AUX_UART_CNTL_RTSINV 0x40 /* Invert auto RTS polarity */
  31. #define BCM2835_AUX_UART_CNTL_CTSINV 0x80 /* Invert auto CTS polarity */
  32. /**
  33. * struct bcm2835aux_data - driver private data of BCM2835 auxiliary UART
  34. * @clk: clock producer of the port's uartclk
  35. * @line: index of the port's serial8250_ports[] entry
  36. * @cntl: cached copy of CNTL register
  37. */
  38. struct bcm2835aux_data {
  39. struct clk *clk;
  40. int line;
  41. u32 cntl;
  42. };
  43. struct bcm2835_aux_serial_driver_data {
  44. resource_size_t offset;
  45. };
  46. static void bcm2835aux_rs485_start_tx(struct uart_8250_port *up)
  47. {
  48. if (!(up->port.rs485.flags & SER_RS485_RX_DURING_TX)) {
  49. struct bcm2835aux_data *data = dev_get_drvdata(up->port.dev);
  50. data->cntl &= ~BCM2835_AUX_UART_CNTL_RXEN;
  51. serial_out(up, BCM2835_AUX_UART_CNTL, data->cntl);
  52. }
  53. /*
  54. * On the bcm2835aux, the MCR register contains no other
  55. * flags besides RTS. So no need for a read-modify-write.
  56. */
  57. if (up->port.rs485.flags & SER_RS485_RTS_ON_SEND)
  58. serial8250_out_MCR(up, 0);
  59. else
  60. serial8250_out_MCR(up, UART_MCR_RTS);
  61. }
  62. static void bcm2835aux_rs485_stop_tx(struct uart_8250_port *up)
  63. {
  64. if (up->port.rs485.flags & SER_RS485_RTS_AFTER_SEND)
  65. serial8250_out_MCR(up, 0);
  66. else
  67. serial8250_out_MCR(up, UART_MCR_RTS);
  68. if (!(up->port.rs485.flags & SER_RS485_RX_DURING_TX)) {
  69. struct bcm2835aux_data *data = dev_get_drvdata(up->port.dev);
  70. data->cntl |= BCM2835_AUX_UART_CNTL_RXEN;
  71. serial_out(up, BCM2835_AUX_UART_CNTL, data->cntl);
  72. }
  73. }
  74. static int bcm2835aux_serial_probe(struct platform_device *pdev)
  75. {
  76. const struct bcm2835_aux_serial_driver_data *bcm_data;
  77. struct uart_8250_port up = { };
  78. struct bcm2835aux_data *data;
  79. resource_size_t offset = 0;
  80. struct resource *res;
  81. unsigned int uartclk;
  82. int ret;
  83. /* allocate the custom structure */
  84. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  85. if (!data)
  86. return -ENOMEM;
  87. /* initialize data */
  88. up.capabilities = UART_CAP_FIFO | UART_CAP_MINI;
  89. up.port.dev = &pdev->dev;
  90. up.port.regshift = 2;
  91. up.port.type = PORT_16550;
  92. up.port.iotype = UPIO_MEM;
  93. up.port.fifosize = 8;
  94. up.port.flags = UPF_SHARE_IRQ | UPF_FIXED_PORT | UPF_FIXED_TYPE |
  95. UPF_SKIP_TEST | UPF_IOREMAP;
  96. up.port.rs485_config = serial8250_em485_config;
  97. up.port.rs485_supported = serial8250_em485_supported;
  98. up.rs485_start_tx = bcm2835aux_rs485_start_tx;
  99. up.rs485_stop_tx = bcm2835aux_rs485_stop_tx;
  100. /* initialize cached copy with power-on reset value */
  101. data->cntl = BCM2835_AUX_UART_CNTL_RXEN | BCM2835_AUX_UART_CNTL_TXEN;
  102. platform_set_drvdata(pdev, data);
  103. /* get the clock - this also enables the HW */
  104. data->clk = devm_clk_get_optional(&pdev->dev, NULL);
  105. /* get the interrupt */
  106. ret = platform_get_irq(pdev, 0);
  107. if (ret < 0)
  108. return ret;
  109. up.port.irq = ret;
  110. /* map the main registers */
  111. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  112. if (!res) {
  113. dev_err(&pdev->dev, "memory resource not found");
  114. return -EINVAL;
  115. }
  116. bcm_data = device_get_match_data(&pdev->dev);
  117. /* Some UEFI implementations (e.g. tianocore/edk2 for the Raspberry Pi)
  118. * describe the miniuart with a base address that encompasses the auxiliary
  119. * registers shared between the miniuart and spi.
  120. *
  121. * This is due to historical reasons, see discussion here :
  122. * https://edk2.groups.io/g/devel/topic/87501357#84349
  123. *
  124. * We need to add the offset between the miniuart and auxiliary
  125. * registers to get the real miniuart base address.
  126. */
  127. if (bcm_data)
  128. offset = bcm_data->offset;
  129. up.port.mapbase = res->start + offset;
  130. up.port.mapsize = resource_size(res) - offset;
  131. /* Check for a fixed line number */
  132. ret = of_alias_get_id(pdev->dev.of_node, "serial");
  133. if (ret >= 0)
  134. up.port.line = ret;
  135. /* enable the clock as a last step */
  136. ret = clk_prepare_enable(data->clk);
  137. if (ret) {
  138. dev_err(&pdev->dev, "unable to enable uart clock - %d\n",
  139. ret);
  140. return ret;
  141. }
  142. uartclk = clk_get_rate(data->clk);
  143. if (!uartclk) {
  144. ret = device_property_read_u32(&pdev->dev, "clock-frequency", &uartclk);
  145. if (ret) {
  146. dev_err_probe(&pdev->dev, ret, "could not get clk rate\n");
  147. goto dis_clk;
  148. }
  149. }
  150. /* the HW-clock divider for bcm2835aux is 8,
  151. * but 8250 expects a divider of 16,
  152. * so we have to multiply the actual clock by 2
  153. * to get identical baudrates.
  154. */
  155. up.port.uartclk = uartclk * 2;
  156. /* register the port */
  157. ret = serial8250_register_8250_port(&up);
  158. if (ret < 0) {
  159. dev_err_probe(&pdev->dev, ret, "unable to register 8250 port\n");
  160. goto dis_clk;
  161. }
  162. data->line = ret;
  163. return 0;
  164. dis_clk:
  165. clk_disable_unprepare(data->clk);
  166. return ret;
  167. }
  168. static int bcm2835aux_serial_remove(struct platform_device *pdev)
  169. {
  170. struct bcm2835aux_data *data = platform_get_drvdata(pdev);
  171. serial8250_unregister_port(data->line);
  172. clk_disable_unprepare(data->clk);
  173. return 0;
  174. }
  175. static const struct bcm2835_aux_serial_driver_data bcm2835_acpi_data = {
  176. .offset = 0x40,
  177. };
  178. static const struct of_device_id bcm2835aux_serial_match[] = {
  179. { .compatible = "brcm,bcm2835-aux-uart" },
  180. { },
  181. };
  182. MODULE_DEVICE_TABLE(of, bcm2835aux_serial_match);
  183. static const struct acpi_device_id bcm2835aux_serial_acpi_match[] = {
  184. { "BCM2836", (kernel_ulong_t)&bcm2835_acpi_data },
  185. { }
  186. };
  187. MODULE_DEVICE_TABLE(acpi, bcm2835aux_serial_acpi_match);
  188. static struct platform_driver bcm2835aux_serial_driver = {
  189. .driver = {
  190. .name = "bcm2835-aux-uart",
  191. .of_match_table = bcm2835aux_serial_match,
  192. .acpi_match_table = bcm2835aux_serial_acpi_match,
  193. },
  194. .probe = bcm2835aux_serial_probe,
  195. .remove = bcm2835aux_serial_remove,
  196. };
  197. module_platform_driver(bcm2835aux_serial_driver);
  198. #ifdef CONFIG_SERIAL_8250_CONSOLE
  199. static int __init early_bcm2835aux_setup(struct earlycon_device *device,
  200. const char *options)
  201. {
  202. if (!device->port.membase)
  203. return -ENODEV;
  204. device->port.iotype = UPIO_MEM32;
  205. device->port.regshift = 2;
  206. return early_serial8250_setup(device, NULL);
  207. }
  208. OF_EARLYCON_DECLARE(bcm2835aux, "brcm,bcm2835-aux-uart",
  209. early_bcm2835aux_setup);
  210. #endif
  211. MODULE_DESCRIPTION("BCM2835 auxiliar UART driver");
  212. MODULE_AUTHOR("Martin Sperl <[email protected]>");
  213. MODULE_LICENSE("GPL v2");