8250_mtk.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Mediatek 8250 driver.
  4. *
  5. * Copyright (c) 2014 MundoReader S.L.
  6. * Author: Matthias Brugger <matthias.bgg@gmail.com>
  7. */
  8. #include <linux/clk.h>
  9. #include <linux/io.h>
  10. #include <linux/module.h>
  11. #include <linux/of_irq.h>
  12. #include <linux/of_platform.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/pm_runtime.h>
  15. #include <linux/serial_8250.h>
  16. #include <linux/serial_reg.h>
  17. #include "8250.h"
  18. #define UART_MTK_HIGHS 0x09 /* Highspeed register */
  19. #define UART_MTK_SAMPLE_COUNT 0x0a /* Sample count register */
  20. #define UART_MTK_SAMPLE_POINT 0x0b /* Sample point register */
  21. #define MTK_UART_RATE_FIX 0x0d /* UART Rate Fix Register */
  22. struct mtk8250_data {
  23. int line;
  24. struct clk *uart_clk;
  25. struct clk *bus_clk;
  26. };
  27. static void
  28. mtk8250_set_termios(struct uart_port *port, struct ktermios *termios,
  29. struct ktermios *old)
  30. {
  31. struct uart_8250_port *up = up_to_u8250p(port);
  32. unsigned long flags;
  33. unsigned int baud, quot;
  34. /*
  35. * Store the requested baud rate before calling the generic 8250
  36. * set_termios method. Standard 8250 port expects bauds to be
  37. * no higher than (uartclk / 16) so the baud will be clamped if it
  38. * gets out of that bound. Mediatek 8250 port supports speed
  39. * higher than that, therefore we'll get original baud rate back
  40. * after calling the generic set_termios method and recalculate
  41. * the speed later in this method.
  42. */
  43. baud = tty_termios_baud_rate(termios);
  44. serial8250_do_set_termios(port, termios, NULL);
  45. tty_termios_encode_baud_rate(termios, baud, baud);
  46. /*
  47. * Mediatek UARTs use an extra highspeed register (UART_MTK_HIGHS)
  48. *
  49. * We need to recalcualte the quot register, as the claculation depends
  50. * on the vaule in the highspeed register.
  51. *
  52. * Some baudrates are not supported by the chip, so we use the next
  53. * lower rate supported and update termios c_flag.
  54. *
  55. * If highspeed register is set to 3, we need to specify sample count
  56. * and sample point to increase accuracy. If not, we reset the
  57. * registers to their default values.
  58. */
  59. baud = uart_get_baud_rate(port, termios, old,
  60. port->uartclk / 16 / UART_DIV_MAX,
  61. port->uartclk);
  62. if (baud <= 115200) {
  63. serial_port_out(port, UART_MTK_HIGHS, 0x0);
  64. quot = uart_get_divisor(port, baud);
  65. } else if (baud <= 576000) {
  66. serial_port_out(port, UART_MTK_HIGHS, 0x2);
  67. /* Set to next lower baudrate supported */
  68. if ((baud == 500000) || (baud == 576000))
  69. baud = 460800;
  70. quot = DIV_ROUND_UP(port->uartclk, 4 * baud);
  71. } else {
  72. serial_port_out(port, UART_MTK_HIGHS, 0x3);
  73. quot = DIV_ROUND_UP(port->uartclk, 256 * baud);
  74. }
  75. /*
  76. * Ok, we're now changing the port state. Do it with
  77. * interrupts disabled.
  78. */
  79. spin_lock_irqsave(&port->lock, flags);
  80. /*
  81. * Update the per-port timeout.
  82. */
  83. uart_update_timeout(port, termios->c_cflag, baud);
  84. /* set DLAB we have cval saved in up->lcr from the call to the core */
  85. serial_port_out(port, UART_LCR, up->lcr | UART_LCR_DLAB);
  86. serial_dl_write(up, quot);
  87. /* reset DLAB */
  88. serial_port_out(port, UART_LCR, up->lcr);
  89. if (baud > 460800) {
  90. unsigned int tmp;
  91. tmp = DIV_ROUND_CLOSEST(port->uartclk, quot * baud);
  92. serial_port_out(port, UART_MTK_SAMPLE_COUNT, tmp - 1);
  93. serial_port_out(port, UART_MTK_SAMPLE_POINT,
  94. (tmp - 2) >> 1);
  95. } else {
  96. serial_port_out(port, UART_MTK_SAMPLE_COUNT, 0x00);
  97. serial_port_out(port, UART_MTK_SAMPLE_POINT, 0xff);
  98. }
  99. spin_unlock_irqrestore(&port->lock, flags);
  100. /* Don't rewrite B0 */
  101. if (tty_termios_baud_rate(termios))
  102. tty_termios_encode_baud_rate(termios, baud, baud);
  103. }
  104. static int __maybe_unused mtk8250_runtime_suspend(struct device *dev)
  105. {
  106. struct mtk8250_data *data = dev_get_drvdata(dev);
  107. clk_disable_unprepare(data->uart_clk);
  108. clk_disable_unprepare(data->bus_clk);
  109. return 0;
  110. }
  111. static int __maybe_unused mtk8250_runtime_resume(struct device *dev)
  112. {
  113. struct mtk8250_data *data = dev_get_drvdata(dev);
  114. int err;
  115. err = clk_prepare_enable(data->uart_clk);
  116. if (err) {
  117. dev_warn(dev, "Can't enable clock\n");
  118. return err;
  119. }
  120. err = clk_prepare_enable(data->bus_clk);
  121. if (err) {
  122. dev_warn(dev, "Can't enable bus clock\n");
  123. return err;
  124. }
  125. return 0;
  126. }
  127. static void
  128. mtk8250_do_pm(struct uart_port *port, unsigned int state, unsigned int old)
  129. {
  130. if (!state)
  131. pm_runtime_get_sync(port->dev);
  132. serial8250_do_pm(port, state, old);
  133. if (state)
  134. pm_runtime_put_sync_suspend(port->dev);
  135. }
  136. static int mtk8250_probe_of(struct platform_device *pdev, struct uart_port *p,
  137. struct mtk8250_data *data)
  138. {
  139. data->uart_clk = devm_clk_get(&pdev->dev, "baud");
  140. if (IS_ERR(data->uart_clk)) {
  141. /*
  142. * For compatibility with older device trees try unnamed
  143. * clk when no baud clk can be found.
  144. */
  145. data->uart_clk = devm_clk_get(&pdev->dev, NULL);
  146. if (IS_ERR(data->uart_clk)) {
  147. dev_warn(&pdev->dev, "Can't get uart clock\n");
  148. return PTR_ERR(data->uart_clk);
  149. }
  150. return 0;
  151. }
  152. data->bus_clk = devm_clk_get(&pdev->dev, "bus");
  153. return PTR_ERR_OR_ZERO(data->bus_clk);
  154. }
  155. static int mtk8250_probe(struct platform_device *pdev)
  156. {
  157. struct uart_8250_port uart = {};
  158. struct resource *regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  159. struct resource *irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  160. struct mtk8250_data *data;
  161. int err;
  162. if (!regs || !irq) {
  163. dev_err(&pdev->dev, "no registers/irq defined\n");
  164. return -EINVAL;
  165. }
  166. uart.port.membase = devm_ioremap(&pdev->dev, regs->start,
  167. resource_size(regs));
  168. if (!uart.port.membase)
  169. return -ENOMEM;
  170. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  171. if (!data)
  172. return -ENOMEM;
  173. if (pdev->dev.of_node) {
  174. err = mtk8250_probe_of(pdev, &uart.port, data);
  175. if (err)
  176. return err;
  177. } else
  178. return -ENODEV;
  179. spin_lock_init(&uart.port.lock);
  180. uart.port.mapbase = regs->start;
  181. uart.port.irq = irq->start;
  182. uart.port.pm = mtk8250_do_pm;
  183. uart.port.type = PORT_16550;
  184. uart.port.flags = UPF_BOOT_AUTOCONF | UPF_FIXED_PORT;
  185. uart.port.dev = &pdev->dev;
  186. uart.port.iotype = UPIO_MEM32;
  187. uart.port.regshift = 2;
  188. uart.port.private_data = data;
  189. uart.port.set_termios = mtk8250_set_termios;
  190. uart.port.uartclk = clk_get_rate(data->uart_clk);
  191. /* Disable Rate Fix function */
  192. writel(0x0, uart.port.membase +
  193. (MTK_UART_RATE_FIX << uart.port.regshift));
  194. platform_set_drvdata(pdev, data);
  195. err = mtk8250_runtime_resume(&pdev->dev);
  196. if (err)
  197. return err;
  198. data->line = serial8250_register_8250_port(&uart);
  199. if (data->line < 0)
  200. return data->line;
  201. pm_runtime_set_active(&pdev->dev);
  202. pm_runtime_enable(&pdev->dev);
  203. return 0;
  204. }
  205. static int mtk8250_remove(struct platform_device *pdev)
  206. {
  207. struct mtk8250_data *data = platform_get_drvdata(pdev);
  208. pm_runtime_get_sync(&pdev->dev);
  209. serial8250_unregister_port(data->line);
  210. mtk8250_runtime_suspend(&pdev->dev);
  211. pm_runtime_disable(&pdev->dev);
  212. pm_runtime_put_noidle(&pdev->dev);
  213. return 0;
  214. }
  215. static int __maybe_unused mtk8250_suspend(struct device *dev)
  216. {
  217. struct mtk8250_data *data = dev_get_drvdata(dev);
  218. serial8250_suspend_port(data->line);
  219. return 0;
  220. }
  221. static int __maybe_unused mtk8250_resume(struct device *dev)
  222. {
  223. struct mtk8250_data *data = dev_get_drvdata(dev);
  224. serial8250_resume_port(data->line);
  225. return 0;
  226. }
  227. static const struct dev_pm_ops mtk8250_pm_ops = {
  228. SET_SYSTEM_SLEEP_PM_OPS(mtk8250_suspend, mtk8250_resume)
  229. SET_RUNTIME_PM_OPS(mtk8250_runtime_suspend, mtk8250_runtime_resume,
  230. NULL)
  231. };
  232. static const struct of_device_id mtk8250_of_match[] = {
  233. { .compatible = "mediatek,mt6577-uart" },
  234. { /* Sentinel */ }
  235. };
  236. MODULE_DEVICE_TABLE(of, mtk8250_of_match);
  237. static struct platform_driver mtk8250_platform_driver = {
  238. .driver = {
  239. .name = "mt6577-uart",
  240. .pm = &mtk8250_pm_ops,
  241. .of_match_table = mtk8250_of_match,
  242. },
  243. .probe = mtk8250_probe,
  244. .remove = mtk8250_remove,
  245. };
  246. module_platform_driver(mtk8250_platform_driver);
  247. #ifdef CONFIG_SERIAL_8250_CONSOLE
  248. static int __init early_mtk8250_setup(struct earlycon_device *device,
  249. const char *options)
  250. {
  251. if (!device->port.membase)
  252. return -ENODEV;
  253. device->port.iotype = UPIO_MEM32;
  254. return early_serial8250_setup(device, NULL);
  255. }
  256. OF_EARLYCON_DECLARE(mtk8250, "mediatek,mt6577-uart", early_mtk8250_setup);
  257. #endif
  258. MODULE_AUTHOR("Matthias Brugger");
  259. MODULE_LICENSE("GPL");
  260. MODULE_DESCRIPTION("Mediatek 8250 serial port driver");