8250_mtk.c 8.0 KB

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