8250_lpc18xx.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * Serial port driver for NXP LPC18xx/43xx UART
  3. *
  4. * Copyright (C) 2015 Joachim Eastwood <manabian@gmail.com>
  5. *
  6. * Based on 8250_mtk.c:
  7. * Copyright (c) 2014 MundoReader S.L.
  8. * Matthias Brugger <matthias.bgg@gmail.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. *
  14. */
  15. #include <linux/clk.h>
  16. #include <linux/io.h>
  17. #include <linux/module.h>
  18. #include <linux/of.h>
  19. #include <linux/platform_device.h>
  20. #include "8250.h"
  21. /* Additional LPC18xx/43xx 8250 registers and bits */
  22. #define LPC18XX_UART_RS485CTRL (0x04c / sizeof(u32))
  23. #define LPC18XX_UART_RS485CTRL_NMMEN BIT(0)
  24. #define LPC18XX_UART_RS485CTRL_DCTRL BIT(4)
  25. #define LPC18XX_UART_RS485CTRL_OINV BIT(5)
  26. #define LPC18XX_UART_RS485DLY (0x054 / sizeof(u32))
  27. #define LPC18XX_UART_RS485DLY_MAX 255
  28. struct lpc18xx_uart_data {
  29. struct uart_8250_dma dma;
  30. struct clk *clk_uart;
  31. struct clk *clk_reg;
  32. int line;
  33. };
  34. static int lpc18xx_rs485_config(struct uart_port *port,
  35. struct serial_rs485 *rs485)
  36. {
  37. struct uart_8250_port *up = up_to_u8250p(port);
  38. u32 rs485_ctrl_reg = 0;
  39. u32 rs485_dly_reg = 0;
  40. unsigned baud_clk;
  41. if (rs485->flags & SER_RS485_ENABLED)
  42. memset(rs485->padding, 0, sizeof(rs485->padding));
  43. else
  44. memset(rs485, 0, sizeof(*rs485));
  45. rs485->flags &= SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND |
  46. SER_RS485_RTS_AFTER_SEND;
  47. if (rs485->flags & SER_RS485_ENABLED) {
  48. rs485_ctrl_reg |= LPC18XX_UART_RS485CTRL_NMMEN |
  49. LPC18XX_UART_RS485CTRL_DCTRL;
  50. if (rs485->flags & SER_RS485_RTS_ON_SEND) {
  51. rs485_ctrl_reg |= LPC18XX_UART_RS485CTRL_OINV;
  52. rs485->flags &= ~SER_RS485_RTS_AFTER_SEND;
  53. } else {
  54. rs485->flags |= SER_RS485_RTS_AFTER_SEND;
  55. }
  56. }
  57. if (rs485->delay_rts_after_send) {
  58. baud_clk = port->uartclk / up->dl_read(up);
  59. rs485_dly_reg = DIV_ROUND_UP(rs485->delay_rts_after_send
  60. * baud_clk, MSEC_PER_SEC);
  61. if (rs485_dly_reg > LPC18XX_UART_RS485DLY_MAX)
  62. rs485_dly_reg = LPC18XX_UART_RS485DLY_MAX;
  63. /* Calculate the resulting delay in ms */
  64. rs485->delay_rts_after_send = (rs485_dly_reg * MSEC_PER_SEC)
  65. / baud_clk;
  66. }
  67. /* Delay RTS before send not supported */
  68. rs485->delay_rts_before_send = 0;
  69. serial_out(up, LPC18XX_UART_RS485CTRL, rs485_ctrl_reg);
  70. serial_out(up, LPC18XX_UART_RS485DLY, rs485_dly_reg);
  71. port->rs485 = *rs485;
  72. return 0;
  73. }
  74. static void lpc18xx_uart_serial_out(struct uart_port *p, int offset, int value)
  75. {
  76. /*
  77. * For DMA mode one must ensure that the UART_FCR_DMA_SELECT
  78. * bit is set when FIFO is enabled. Even if DMA is not used
  79. * setting this bit doesn't seem to affect anything.
  80. */
  81. if (offset == UART_FCR && (value & UART_FCR_ENABLE_FIFO))
  82. value |= UART_FCR_DMA_SELECT;
  83. offset = offset << p->regshift;
  84. writel(value, p->membase + offset);
  85. }
  86. static int lpc18xx_serial_probe(struct platform_device *pdev)
  87. {
  88. struct lpc18xx_uart_data *data;
  89. struct uart_8250_port uart;
  90. struct resource *res;
  91. int irq, ret;
  92. irq = platform_get_irq(pdev, 0);
  93. if (irq < 0) {
  94. dev_err(&pdev->dev, "irq not found");
  95. return irq;
  96. }
  97. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  98. if (!res) {
  99. dev_err(&pdev->dev, "memory resource not found");
  100. return -EINVAL;
  101. }
  102. memset(&uart, 0, sizeof(uart));
  103. uart.port.membase = devm_ioremap(&pdev->dev, res->start,
  104. resource_size(res));
  105. if (!uart.port.membase)
  106. return -ENOMEM;
  107. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  108. if (!data)
  109. return -ENOMEM;
  110. data->clk_uart = devm_clk_get(&pdev->dev, "uartclk");
  111. if (IS_ERR(data->clk_uart)) {
  112. dev_err(&pdev->dev, "uart clock not found\n");
  113. return PTR_ERR(data->clk_uart);
  114. }
  115. data->clk_reg = devm_clk_get(&pdev->dev, "reg");
  116. if (IS_ERR(data->clk_reg)) {
  117. dev_err(&pdev->dev, "reg clock not found\n");
  118. return PTR_ERR(data->clk_reg);
  119. }
  120. ret = clk_prepare_enable(data->clk_reg);
  121. if (ret) {
  122. dev_err(&pdev->dev, "unable to enable reg clock\n");
  123. return ret;
  124. }
  125. ret = clk_prepare_enable(data->clk_uart);
  126. if (ret) {
  127. dev_err(&pdev->dev, "unable to enable uart clock\n");
  128. goto dis_clk_reg;
  129. }
  130. ret = of_alias_get_id(pdev->dev.of_node, "serial");
  131. if (ret >= 0)
  132. uart.port.line = ret;
  133. data->dma.rx_param = data;
  134. data->dma.tx_param = data;
  135. spin_lock_init(&uart.port.lock);
  136. uart.port.dev = &pdev->dev;
  137. uart.port.irq = irq;
  138. uart.port.iotype = UPIO_MEM32;
  139. uart.port.mapbase = res->start;
  140. uart.port.regshift = 2;
  141. uart.port.type = PORT_16550A;
  142. uart.port.flags = UPF_FIXED_PORT | UPF_FIXED_TYPE | UPF_SKIP_TEST;
  143. uart.port.uartclk = clk_get_rate(data->clk_uart);
  144. uart.port.private_data = data;
  145. uart.port.rs485_config = lpc18xx_rs485_config;
  146. uart.port.serial_out = lpc18xx_uart_serial_out;
  147. uart.dma = &data->dma;
  148. uart.dma->rxconf.src_maxburst = 1;
  149. uart.dma->txconf.dst_maxburst = 1;
  150. ret = serial8250_register_8250_port(&uart);
  151. if (ret < 0) {
  152. dev_err(&pdev->dev, "unable to register 8250 port\n");
  153. goto dis_uart_clk;
  154. }
  155. data->line = ret;
  156. platform_set_drvdata(pdev, data);
  157. return 0;
  158. dis_uart_clk:
  159. clk_disable_unprepare(data->clk_uart);
  160. dis_clk_reg:
  161. clk_disable_unprepare(data->clk_reg);
  162. return ret;
  163. }
  164. static int lpc18xx_serial_remove(struct platform_device *pdev)
  165. {
  166. struct lpc18xx_uart_data *data = platform_get_drvdata(pdev);
  167. serial8250_unregister_port(data->line);
  168. clk_disable_unprepare(data->clk_uart);
  169. clk_disable_unprepare(data->clk_reg);
  170. return 0;
  171. }
  172. static const struct of_device_id lpc18xx_serial_match[] = {
  173. { .compatible = "nxp,lpc1850-uart" },
  174. { },
  175. };
  176. MODULE_DEVICE_TABLE(of, lpc18xx_serial_match);
  177. static struct platform_driver lpc18xx_serial_driver = {
  178. .probe = lpc18xx_serial_probe,
  179. .remove = lpc18xx_serial_remove,
  180. .driver = {
  181. .name = "lpc18xx-uart",
  182. .of_match_table = lpc18xx_serial_match,
  183. },
  184. };
  185. module_platform_driver(lpc18xx_serial_driver);
  186. MODULE_AUTHOR("Joachim Eastwood <manabian@gmail.com>");
  187. MODULE_DESCRIPTION("Serial port driver NXP LPC18xx/43xx devices");
  188. MODULE_LICENSE("GPL v2");