8250_uniphier.c 8.1 KB

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