8250_uniphier.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2015 Masahiro Yamada <yamada.masahiro@socionext.com>
  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 u32 uniphier_serial_in(struct uart_port *p, unsigned 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, unsigned int offset, u32 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 u32 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, u32 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 ret;
  146. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  147. if (!regs) {
  148. dev_err(dev, "failed to get memory resource\n");
  149. return -EINVAL;
  150. }
  151. membase = devm_ioremap(dev, regs->start, resource_size(regs));
  152. if (!membase)
  153. return -ENOMEM;
  154. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  155. if (!priv)
  156. return -ENOMEM;
  157. memset(&up, 0, sizeof(up));
  158. priv->clk = devm_clk_get(dev, NULL);
  159. if (IS_ERR(priv->clk)) {
  160. dev_err(dev, "failed to get clock\n");
  161. return PTR_ERR(priv->clk);
  162. }
  163. ret = clk_prepare_enable(priv->clk);
  164. if (ret)
  165. return ret;
  166. up.port.uartclk = clk_get_rate(priv->clk);
  167. spin_lock_init(&priv->atomic_write_lock);
  168. up.port.dev = dev;
  169. up.port.private_data = priv;
  170. up.port.mapbase = regs->start;
  171. up.port.mapsize = resource_size(regs);
  172. up.port.membase = membase;
  173. ret = uart_read_port_properties(&up.port);
  174. if (ret)
  175. return ret;
  176. up.port.type = PORT_16550A;
  177. up.port.iotype = UPIO_MEM32;
  178. up.port.fifosize = 64;
  179. up.port.regshift = UNIPHIER_UART_REGSHIFT;
  180. up.port.flags = UPF_FIXED_PORT | UPF_FIXED_TYPE;
  181. up.capabilities = UART_CAP_FIFO;
  182. if (of_property_read_bool(dev->of_node, "auto-flow-control"))
  183. up.capabilities |= UART_CAP_AFE;
  184. up.port.serial_in = uniphier_serial_in;
  185. up.port.serial_out = uniphier_serial_out;
  186. up.dl_read = uniphier_serial_dl_read;
  187. up.dl_write = uniphier_serial_dl_write;
  188. ret = serial8250_register_8250_port(&up);
  189. if (ret < 0) {
  190. dev_err(dev, "failed to register 8250 port\n");
  191. clk_disable_unprepare(priv->clk);
  192. return ret;
  193. }
  194. priv->line = ret;
  195. platform_set_drvdata(pdev, priv);
  196. return 0;
  197. }
  198. static void uniphier_uart_remove(struct platform_device *pdev)
  199. {
  200. struct uniphier8250_priv *priv = platform_get_drvdata(pdev);
  201. serial8250_unregister_port(priv->line);
  202. clk_disable_unprepare(priv->clk);
  203. }
  204. static int __maybe_unused uniphier_uart_suspend(struct device *dev)
  205. {
  206. struct uniphier8250_priv *priv = dev_get_drvdata(dev);
  207. struct uart_8250_port *up = serial8250_get_port(priv->line);
  208. serial8250_suspend_port(priv->line);
  209. if (!uart_console(&up->port) || console_suspend_enabled)
  210. clk_disable_unprepare(priv->clk);
  211. return 0;
  212. }
  213. static int __maybe_unused uniphier_uart_resume(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. int ret;
  218. if (!uart_console(&up->port) || console_suspend_enabled) {
  219. ret = clk_prepare_enable(priv->clk);
  220. if (ret)
  221. return ret;
  222. }
  223. serial8250_resume_port(priv->line);
  224. return 0;
  225. }
  226. static const struct dev_pm_ops uniphier_uart_pm_ops = {
  227. SET_SYSTEM_SLEEP_PM_OPS(uniphier_uart_suspend, uniphier_uart_resume)
  228. };
  229. static const struct of_device_id uniphier_uart_match[] = {
  230. { .compatible = "socionext,uniphier-uart" },
  231. { /* sentinel */ }
  232. };
  233. MODULE_DEVICE_TABLE(of, uniphier_uart_match);
  234. static struct platform_driver uniphier_uart_platform_driver = {
  235. .probe = uniphier_uart_probe,
  236. .remove = uniphier_uart_remove,
  237. .driver = {
  238. .name = "uniphier-uart",
  239. .of_match_table = uniphier_uart_match,
  240. .pm = &uniphier_uart_pm_ops,
  241. },
  242. };
  243. module_platform_driver(uniphier_uart_platform_driver);
  244. MODULE_AUTHOR("Masahiro Yamada <yamada.masahiro@socionext.com>");
  245. MODULE_DESCRIPTION("UniPhier UART driver");
  246. MODULE_LICENSE("GPL");