8250_uniphier.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2015 Masahiro Yamada <[email protected]>
  4. */
  5. #include <linux/clk.h>
  6. #include <linux/console.h>
  7. #include <linux/io.h>
  8. #include <linux/module.h>
  9. #include <linux/of.h>
  10. #include <linux/platform_device.h>
  11. #include "8250.h"
  12. /*
  13. * This hardware is similar to 8250, but its register map is a bit different:
  14. * - MMIO32 (regshift = 2)
  15. * - FCR is not at 2, but 3
  16. * - LCR and MCR are not at 3 and 4, they share 4
  17. * - No SCR (Instead, CHAR can be used as a scratch register)
  18. * - Divisor latch at 9, no divisor latch access bit
  19. */
  20. #define UNIPHIER_UART_REGSHIFT 2
  21. /* bit[15:8] = CHAR, bit[7:0] = FCR */
  22. #define UNIPHIER_UART_CHAR_FCR (3 << (UNIPHIER_UART_REGSHIFT))
  23. /* bit[15:8] = LCR, bit[7:0] = MCR */
  24. #define UNIPHIER_UART_LCR_MCR (4 << (UNIPHIER_UART_REGSHIFT))
  25. /* Divisor Latch Register */
  26. #define UNIPHIER_UART_DLR (9 << (UNIPHIER_UART_REGSHIFT))
  27. struct uniphier8250_priv {
  28. int line;
  29. struct clk *clk;
  30. spinlock_t atomic_write_lock;
  31. };
  32. #ifdef CONFIG_SERIAL_8250_CONSOLE
  33. static int __init uniphier_early_console_setup(struct earlycon_device *device,
  34. const char *options)
  35. {
  36. if (!device->port.membase)
  37. return -ENODEV;
  38. /* This hardware always expects MMIO32 register interface. */
  39. device->port.iotype = UPIO_MEM32;
  40. device->port.regshift = UNIPHIER_UART_REGSHIFT;
  41. /*
  42. * Do not touch the divisor register in early_serial8250_setup();
  43. * we assume it has been initialized by a boot loader.
  44. */
  45. device->baud = 0;
  46. return early_serial8250_setup(device, options);
  47. }
  48. OF_EARLYCON_DECLARE(uniphier, "socionext,uniphier-uart",
  49. uniphier_early_console_setup);
  50. #endif
  51. /*
  52. * The register map is slightly different from that of 8250.
  53. * IO callbacks must be overridden for correct access to FCR, LCR, MCR and SCR.
  54. */
  55. static unsigned int uniphier_serial_in(struct uart_port *p, int offset)
  56. {
  57. unsigned int valshift = 0;
  58. switch (offset) {
  59. case UART_SCR:
  60. /* No SCR for this hardware. Use CHAR as a scratch register */
  61. valshift = 8;
  62. offset = UNIPHIER_UART_CHAR_FCR;
  63. break;
  64. case UART_LCR:
  65. valshift = 8;
  66. fallthrough;
  67. case UART_MCR:
  68. offset = UNIPHIER_UART_LCR_MCR;
  69. break;
  70. default:
  71. offset <<= UNIPHIER_UART_REGSHIFT;
  72. break;
  73. }
  74. /*
  75. * The return value must be masked with 0xff because some registers
  76. * share the same offset that must be accessed by 32-bit write/read.
  77. * 8 or 16 bit access to this hardware result in unexpected behavior.
  78. */
  79. return (readl(p->membase + offset) >> valshift) & 0xff;
  80. }
  81. static void uniphier_serial_out(struct uart_port *p, int offset, int value)
  82. {
  83. unsigned int valshift = 0;
  84. bool normal = false;
  85. switch (offset) {
  86. case UART_SCR:
  87. /* No SCR for this hardware. Use CHAR as a scratch register */
  88. valshift = 8;
  89. fallthrough;
  90. case UART_FCR:
  91. offset = UNIPHIER_UART_CHAR_FCR;
  92. break;
  93. case UART_LCR:
  94. valshift = 8;
  95. /* Divisor latch access bit does not exist. */
  96. value &= ~UART_LCR_DLAB;
  97. fallthrough;
  98. case UART_MCR:
  99. offset = UNIPHIER_UART_LCR_MCR;
  100. break;
  101. default:
  102. offset <<= UNIPHIER_UART_REGSHIFT;
  103. normal = true;
  104. break;
  105. }
  106. if (normal) {
  107. writel(value, p->membase + offset);
  108. } else {
  109. /*
  110. * Special case: two registers share the same address that
  111. * must be 32-bit accessed. As this is not longer atomic safe,
  112. * take a lock just in case.
  113. */
  114. struct uniphier8250_priv *priv = p->private_data;
  115. unsigned long flags;
  116. u32 tmp;
  117. spin_lock_irqsave(&priv->atomic_write_lock, flags);
  118. tmp = readl(p->membase + offset);
  119. tmp &= ~(0xff << valshift);
  120. tmp |= value << valshift;
  121. writel(tmp, p->membase + offset);
  122. spin_unlock_irqrestore(&priv->atomic_write_lock, flags);
  123. }
  124. }
  125. /*
  126. * This hardware does not have the divisor latch access bit.
  127. * The divisor latch register exists at different address.
  128. * Override dl_read/write callbacks.
  129. */
  130. static int uniphier_serial_dl_read(struct uart_8250_port *up)
  131. {
  132. return readl(up->port.membase + UNIPHIER_UART_DLR);
  133. }
  134. static void uniphier_serial_dl_write(struct uart_8250_port *up, int value)
  135. {
  136. writel(value, up->port.membase + UNIPHIER_UART_DLR);
  137. }
  138. static int uniphier_uart_probe(struct platform_device *pdev)
  139. {
  140. struct device *dev = &pdev->dev;
  141. struct uart_8250_port up;
  142. struct uniphier8250_priv *priv;
  143. struct resource *regs;
  144. void __iomem *membase;
  145. int irq;
  146. int ret;
  147. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  148. if (!regs) {
  149. dev_err(dev, "failed to get memory resource\n");
  150. return -EINVAL;
  151. }
  152. membase = devm_ioremap(dev, regs->start, resource_size(regs));
  153. if (!membase)
  154. return -ENOMEM;
  155. irq = platform_get_irq(pdev, 0);
  156. if (irq < 0)
  157. return irq;
  158. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  159. if (!priv)
  160. return -ENOMEM;
  161. memset(&up, 0, sizeof(up));
  162. ret = of_alias_get_id(dev->of_node, "serial");
  163. if (ret < 0) {
  164. dev_err(dev, "failed to get alias id\n");
  165. return ret;
  166. }
  167. up.port.line = ret;
  168. priv->clk = devm_clk_get(dev, NULL);
  169. if (IS_ERR(priv->clk)) {
  170. dev_err(dev, "failed to get clock\n");
  171. return PTR_ERR(priv->clk);
  172. }
  173. ret = clk_prepare_enable(priv->clk);
  174. if (ret)
  175. return ret;
  176. up.port.uartclk = clk_get_rate(priv->clk);
  177. spin_lock_init(&priv->atomic_write_lock);
  178. up.port.dev = dev;
  179. up.port.private_data = priv;
  180. up.port.mapbase = regs->start;
  181. up.port.mapsize = resource_size(regs);
  182. up.port.membase = membase;
  183. up.port.irq = irq;
  184. up.port.type = PORT_16550A;
  185. up.port.iotype = UPIO_MEM32;
  186. up.port.fifosize = 64;
  187. up.port.regshift = UNIPHIER_UART_REGSHIFT;
  188. up.port.flags = UPF_FIXED_PORT | UPF_FIXED_TYPE;
  189. up.capabilities = UART_CAP_FIFO;
  190. if (of_property_read_bool(dev->of_node, "auto-flow-control"))
  191. up.capabilities |= UART_CAP_AFE;
  192. up.port.serial_in = uniphier_serial_in;
  193. up.port.serial_out = uniphier_serial_out;
  194. up.dl_read = uniphier_serial_dl_read;
  195. up.dl_write = uniphier_serial_dl_write;
  196. ret = serial8250_register_8250_port(&up);
  197. if (ret < 0) {
  198. dev_err(dev, "failed to register 8250 port\n");
  199. clk_disable_unprepare(priv->clk);
  200. return ret;
  201. }
  202. priv->line = ret;
  203. platform_set_drvdata(pdev, priv);
  204. return 0;
  205. }
  206. static int uniphier_uart_remove(struct platform_device *pdev)
  207. {
  208. struct uniphier8250_priv *priv = platform_get_drvdata(pdev);
  209. serial8250_unregister_port(priv->line);
  210. clk_disable_unprepare(priv->clk);
  211. return 0;
  212. }
  213. static int __maybe_unused uniphier_uart_suspend(struct device *dev)
  214. {
  215. struct uniphier8250_priv *priv = dev_get_drvdata(dev);
  216. struct uart_8250_port *up = serial8250_get_port(priv->line);
  217. serial8250_suspend_port(priv->line);
  218. if (!uart_console(&up->port) || console_suspend_enabled)
  219. clk_disable_unprepare(priv->clk);
  220. return 0;
  221. }
  222. static int __maybe_unused uniphier_uart_resume(struct device *dev)
  223. {
  224. struct uniphier8250_priv *priv = dev_get_drvdata(dev);
  225. struct uart_8250_port *up = serial8250_get_port(priv->line);
  226. int ret;
  227. if (!uart_console(&up->port) || console_suspend_enabled) {
  228. ret = clk_prepare_enable(priv->clk);
  229. if (ret)
  230. return ret;
  231. }
  232. serial8250_resume_port(priv->line);
  233. return 0;
  234. }
  235. static const struct dev_pm_ops uniphier_uart_pm_ops = {
  236. SET_SYSTEM_SLEEP_PM_OPS(uniphier_uart_suspend, uniphier_uart_resume)
  237. };
  238. static const struct of_device_id uniphier_uart_match[] = {
  239. { .compatible = "socionext,uniphier-uart" },
  240. { /* sentinel */ }
  241. };
  242. MODULE_DEVICE_TABLE(of, uniphier_uart_match);
  243. static struct platform_driver uniphier_uart_platform_driver = {
  244. .probe = uniphier_uart_probe,
  245. .remove = uniphier_uart_remove,
  246. .driver = {
  247. .name = "uniphier-uart",
  248. .of_match_table = uniphier_uart_match,
  249. .pm = &uniphier_uart_pm_ops,
  250. },
  251. };
  252. module_platform_driver(uniphier_uart_platform_driver);
  253. MODULE_AUTHOR("Masahiro Yamada <[email protected]>");
  254. MODULE_DESCRIPTION("UniPhier UART driver");
  255. MODULE_LICENSE("GPL");