8250_mtk.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * Mediatek 8250 driver.
  3. *
  4. * Copyright (c) 2014 MundoReader S.L.
  5. * Author: Matthias Brugger <matthias.bgg@gmail.com>
  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. };
  35. static void
  36. mtk8250_set_termios(struct uart_port *port, struct ktermios *termios,
  37. struct ktermios *old)
  38. {
  39. unsigned long flags;
  40. unsigned int baud, quot;
  41. struct uart_8250_port *up =
  42. container_of(port, struct uart_8250_port, port);
  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 / 16);
  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_CLOSEST(port->uartclk, 4 * baud);
  69. } else {
  70. serial_port_out(port, UART_MTK_HIGHS, 0x3);
  71. /* Set to highest baudrate supported */
  72. if (baud >= 1152000)
  73. baud = 921600;
  74. quot = DIV_ROUND_CLOSEST(port->uartclk, 256 * baud);
  75. }
  76. /*
  77. * Ok, we're now changing the port state. Do it with
  78. * interrupts disabled.
  79. */
  80. spin_lock_irqsave(&port->lock, flags);
  81. /* set DLAB we have cval saved in up->lcr from the call to the core */
  82. serial_port_out(port, UART_LCR, up->lcr | UART_LCR_DLAB);
  83. serial_dl_write(up, quot);
  84. /* reset DLAB */
  85. serial_port_out(port, UART_LCR, up->lcr);
  86. if (baud > 460800) {
  87. unsigned int tmp;
  88. tmp = DIV_ROUND_CLOSEST(port->uartclk, quot * baud);
  89. serial_port_out(port, UART_MTK_SAMPLE_COUNT, tmp - 1);
  90. serial_port_out(port, UART_MTK_SAMPLE_POINT,
  91. (tmp - 2) >> 1);
  92. } else {
  93. serial_port_out(port, UART_MTK_SAMPLE_COUNT, 0x00);
  94. serial_port_out(port, UART_MTK_SAMPLE_POINT, 0xff);
  95. }
  96. spin_unlock_irqrestore(&port->lock, flags);
  97. /* Don't rewrite B0 */
  98. if (tty_termios_baud_rate(termios))
  99. tty_termios_encode_baud_rate(termios, baud, baud);
  100. }
  101. static void
  102. mtk8250_do_pm(struct uart_port *port, unsigned int state, unsigned int old)
  103. {
  104. if (!state)
  105. pm_runtime_get_sync(port->dev);
  106. serial8250_do_pm(port, state, old);
  107. if (state)
  108. pm_runtime_put_sync_suspend(port->dev);
  109. }
  110. static int mtk8250_probe_of(struct platform_device *pdev, struct uart_port *p,
  111. struct mtk8250_data *data)
  112. {
  113. int err;
  114. struct device_node *np = pdev->dev.of_node;
  115. data->uart_clk = of_clk_get(np, 0);
  116. if (IS_ERR(data->uart_clk)) {
  117. dev_warn(&pdev->dev, "Can't get timer clock\n");
  118. return PTR_ERR(data->uart_clk);
  119. }
  120. err = clk_prepare_enable(data->uart_clk);
  121. if (err) {
  122. dev_warn(&pdev->dev, "Can't prepare clock\n");
  123. clk_put(data->uart_clk);
  124. return err;
  125. }
  126. p->uartclk = clk_get_rate(data->uart_clk);
  127. return 0;
  128. }
  129. static int mtk8250_probe(struct platform_device *pdev)
  130. {
  131. struct uart_8250_port uart = {};
  132. struct resource *regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  133. struct resource *irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  134. struct mtk8250_data *data;
  135. int err;
  136. if (!regs || !irq) {
  137. dev_err(&pdev->dev, "no registers/irq defined\n");
  138. return -EINVAL;
  139. }
  140. uart.port.membase = devm_ioremap(&pdev->dev, regs->start,
  141. resource_size(regs));
  142. if (!uart.port.membase)
  143. return -ENOMEM;
  144. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  145. if (!data)
  146. return -ENOMEM;
  147. if (pdev->dev.of_node) {
  148. err = mtk8250_probe_of(pdev, &uart.port, data);
  149. if (err)
  150. return err;
  151. } else
  152. return -ENODEV;
  153. spin_lock_init(&uart.port.lock);
  154. uart.port.mapbase = regs->start;
  155. uart.port.irq = irq->start;
  156. uart.port.pm = mtk8250_do_pm;
  157. uart.port.type = PORT_16550;
  158. uart.port.flags = UPF_BOOT_AUTOCONF | UPF_FIXED_PORT;
  159. uart.port.dev = &pdev->dev;
  160. uart.port.iotype = UPIO_MEM32;
  161. uart.port.regshift = 2;
  162. uart.port.private_data = data;
  163. uart.port.set_termios = mtk8250_set_termios;
  164. /* Disable Rate Fix function */
  165. writel(0x0, uart.port.membase +
  166. (MTK_UART_RATE_FIX << uart.port.regshift));
  167. data->line = serial8250_register_8250_port(&uart);
  168. if (data->line < 0)
  169. return data->line;
  170. platform_set_drvdata(pdev, data);
  171. pm_runtime_set_active(&pdev->dev);
  172. pm_runtime_enable(&pdev->dev);
  173. return 0;
  174. }
  175. static int mtk8250_remove(struct platform_device *pdev)
  176. {
  177. struct mtk8250_data *data = platform_get_drvdata(pdev);
  178. pm_runtime_get_sync(&pdev->dev);
  179. serial8250_unregister_port(data->line);
  180. if (!IS_ERR(data->uart_clk)) {
  181. clk_disable_unprepare(data->uart_clk);
  182. clk_put(data->uart_clk);
  183. }
  184. pm_runtime_disable(&pdev->dev);
  185. pm_runtime_put_noidle(&pdev->dev);
  186. return 0;
  187. }
  188. #ifdef CONFIG_PM_SLEEP
  189. static int mtk8250_suspend(struct device *dev)
  190. {
  191. struct mtk8250_data *data = dev_get_drvdata(dev);
  192. serial8250_suspend_port(data->line);
  193. return 0;
  194. }
  195. static int mtk8250_resume(struct device *dev)
  196. {
  197. struct mtk8250_data *data = dev_get_drvdata(dev);
  198. serial8250_resume_port(data->line);
  199. return 0;
  200. }
  201. #endif /* CONFIG_PM_SLEEP */
  202. #ifdef CONFIG_PM_RUNTIME
  203. static int mtk8250_runtime_suspend(struct device *dev)
  204. {
  205. struct mtk8250_data *data = dev_get_drvdata(dev);
  206. if (!IS_ERR(data->uart_clk))
  207. clk_disable_unprepare(data->uart_clk);
  208. return 0;
  209. }
  210. static int mtk8250_runtime_resume(struct device *dev)
  211. {
  212. struct mtk8250_data *data = dev_get_drvdata(dev);
  213. if (!IS_ERR(data->uart_clk))
  214. clk_prepare_enable(data->uart_clk);
  215. return 0;
  216. }
  217. #endif
  218. static const struct dev_pm_ops mtk8250_pm_ops = {
  219. SET_SYSTEM_SLEEP_PM_OPS(mtk8250_suspend, mtk8250_resume)
  220. SET_RUNTIME_PM_OPS(mtk8250_runtime_suspend, mtk8250_runtime_resume,
  221. NULL)
  222. };
  223. static const struct of_device_id mtk8250_of_match[] = {
  224. { .compatible = "mediatek,mt6577-uart" },
  225. { /* Sentinel */ }
  226. };
  227. MODULE_DEVICE_TABLE(of, mtk8250_of_match);
  228. static struct platform_driver mtk8250_platform_driver = {
  229. .driver = {
  230. .name = "mt6577-uart",
  231. .pm = &mtk8250_pm_ops,
  232. .of_match_table = mtk8250_of_match,
  233. },
  234. .probe = mtk8250_probe,
  235. .remove = mtk8250_remove,
  236. };
  237. module_platform_driver(mtk8250_platform_driver);
  238. MODULE_AUTHOR("Matthias Brugger");
  239. MODULE_LICENSE("GPL");
  240. MODULE_DESCRIPTION("Mediatek 8250 serial port driver");