8250_uniphier.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * Copyright (C) 2015 Masahiro Yamada <yamada.masahiro@socionext.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <linux/clk.h>
  15. #include <linux/console.h>
  16. #include <linux/io.h>
  17. #include <linux/module.h>
  18. #include <linux/of.h>
  19. #include <linux/platform_device.h>
  20. #include "8250.h"
  21. /* Most (but not all) of UniPhier UART devices have 64-depth FIFO. */
  22. #define UNIPHIER_UART_DEFAULT_FIFO_SIZE 64
  23. /*
  24. * This hardware is similar to 8250, but its register map is a bit different:
  25. * - MMIO32 (regshift = 2)
  26. * - FCR is not at 2, but 3
  27. * - LCR and MCR are not at 3 and 4, they share 4
  28. * - Divisor latch at 9, no divisor latch access bit
  29. */
  30. #define UNIPHIER_UART_REGSHIFT 2
  31. /* bit[15:8] = CHAR (not used), bit[7:0] = FCR */
  32. #define UNIPHIER_UART_CHAR_FCR (3 << (UNIPHIER_UART_REGSHIFT))
  33. /* bit[15:8] = LCR, bit[7:0] = MCR */
  34. #define UNIPHIER_UART_LCR_MCR (4 << (UNIPHIER_UART_REGSHIFT))
  35. /* Divisor Latch Register */
  36. #define UNIPHIER_UART_DLR (9 << (UNIPHIER_UART_REGSHIFT))
  37. struct uniphier8250_priv {
  38. int line;
  39. struct clk *clk;
  40. spinlock_t atomic_write_lock;
  41. };
  42. #ifdef CONFIG_SERIAL_8250_CONSOLE
  43. static int __init uniphier_early_console_setup(struct earlycon_device *device,
  44. const char *options)
  45. {
  46. if (!device->port.membase)
  47. return -ENODEV;
  48. /* This hardware always expects MMIO32 register interface. */
  49. device->port.iotype = UPIO_MEM32;
  50. device->port.regshift = UNIPHIER_UART_REGSHIFT;
  51. /*
  52. * Do not touch the divisor register in early_serial8250_setup();
  53. * we assume it has been initialized by a boot loader.
  54. */
  55. device->baud = 0;
  56. return early_serial8250_setup(device, options);
  57. }
  58. OF_EARLYCON_DECLARE(uniphier, "socionext,uniphier-uart",
  59. uniphier_early_console_setup);
  60. #endif
  61. /*
  62. * The register map is slightly different from that of 8250.
  63. * IO callbacks must be overridden for correct access to FCR, LCR, and MCR.
  64. */
  65. static unsigned int uniphier_serial_in(struct uart_port *p, int offset)
  66. {
  67. unsigned int valshift = 0;
  68. switch (offset) {
  69. case UART_LCR:
  70. valshift = 8;
  71. /* fall through */
  72. case UART_MCR:
  73. offset = UNIPHIER_UART_LCR_MCR;
  74. break;
  75. default:
  76. offset <<= UNIPHIER_UART_REGSHIFT;
  77. break;
  78. }
  79. /*
  80. * The return value must be masked with 0xff because LCR and MCR reside
  81. * in the same register that must be accessed by 32-bit write/read.
  82. * 8 or 16 bit access to this hardware result in unexpected behavior.
  83. */
  84. return (readl(p->membase + offset) >> valshift) & 0xff;
  85. }
  86. static void uniphier_serial_out(struct uart_port *p, int offset, int value)
  87. {
  88. unsigned int valshift = 0;
  89. bool normal = true;
  90. switch (offset) {
  91. case UART_FCR:
  92. offset = UNIPHIER_UART_CHAR_FCR;
  93. break;
  94. case UART_LCR:
  95. valshift = 8;
  96. /* Divisor latch access bit does not exist. */
  97. value &= ~UART_LCR_DLAB;
  98. /* fall through */
  99. case UART_MCR:
  100. offset = UNIPHIER_UART_LCR_MCR;
  101. normal = false;
  102. break;
  103. default:
  104. offset <<= UNIPHIER_UART_REGSHIFT;
  105. break;
  106. }
  107. if (normal) {
  108. writel(value, p->membase + offset);
  109. } else {
  110. /*
  111. * Special case: two registers share the same address that
  112. * must be 32-bit accessed. As this is not longer atomic safe,
  113. * take a lock just in case.
  114. */
  115. struct uniphier8250_priv *priv = p->private_data;
  116. unsigned long flags;
  117. u32 tmp;
  118. spin_lock_irqsave(&priv->atomic_write_lock, flags);
  119. tmp = readl(p->membase + offset);
  120. tmp &= ~(0xff << valshift);
  121. tmp |= value << valshift;
  122. writel(tmp, p->membase + offset);
  123. spin_unlock_irqrestore(&priv->atomic_write_lock, flags);
  124. }
  125. }
  126. /*
  127. * This hardware does not have the divisor latch access bit.
  128. * The divisor latch register exists at different address.
  129. * Override dl_read/write callbacks.
  130. */
  131. static int uniphier_serial_dl_read(struct uart_8250_port *up)
  132. {
  133. return readl(up->port.membase + UNIPHIER_UART_DLR);
  134. }
  135. static void uniphier_serial_dl_write(struct uart_8250_port *up, int value)
  136. {
  137. writel(value, up->port.membase + UNIPHIER_UART_DLR);
  138. }
  139. static int uniphier_of_serial_setup(struct device *dev, struct uart_port *port,
  140. struct uniphier8250_priv *priv)
  141. {
  142. int ret;
  143. u32 prop;
  144. struct device_node *np = dev->of_node;
  145. ret = of_alias_get_id(np, "serial");
  146. if (ret < 0) {
  147. dev_err(dev, "failed to get alias id\n");
  148. return ret;
  149. }
  150. port->line = priv->line = ret;
  151. /* Get clk rate through clk driver */
  152. priv->clk = devm_clk_get(dev, NULL);
  153. if (IS_ERR(priv->clk)) {
  154. dev_err(dev, "failed to get clock\n");
  155. return PTR_ERR(priv->clk);
  156. }
  157. ret = clk_prepare_enable(priv->clk);
  158. if (ret < 0)
  159. return ret;
  160. port->uartclk = clk_get_rate(priv->clk);
  161. /* Check for fifo size */
  162. if (of_property_read_u32(np, "fifo-size", &prop) == 0)
  163. port->fifosize = prop;
  164. else
  165. port->fifosize = UNIPHIER_UART_DEFAULT_FIFO_SIZE;
  166. return 0;
  167. }
  168. static int uniphier_uart_probe(struct platform_device *pdev)
  169. {
  170. struct device *dev = &pdev->dev;
  171. struct uart_8250_port up;
  172. struct uniphier8250_priv *priv;
  173. struct resource *regs;
  174. void __iomem *membase;
  175. int irq;
  176. int ret;
  177. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  178. if (!regs) {
  179. dev_err(dev, "failed to get memory resource\n");
  180. return -EINVAL;
  181. }
  182. membase = devm_ioremap(dev, regs->start, resource_size(regs));
  183. if (!membase)
  184. return -ENOMEM;
  185. irq = platform_get_irq(pdev, 0);
  186. if (irq < 0) {
  187. dev_err(dev, "failed to get IRQ number\n");
  188. return irq;
  189. }
  190. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  191. if (!priv)
  192. return -ENOMEM;
  193. memset(&up, 0, sizeof(up));
  194. ret = uniphier_of_serial_setup(dev, &up.port, priv);
  195. if (ret < 0)
  196. return ret;
  197. spin_lock_init(&priv->atomic_write_lock);
  198. up.port.dev = dev;
  199. up.port.private_data = priv;
  200. up.port.mapbase = regs->start;
  201. up.port.mapsize = resource_size(regs);
  202. up.port.membase = membase;
  203. up.port.irq = irq;
  204. up.port.type = PORT_16550A;
  205. up.port.iotype = UPIO_MEM32;
  206. up.port.regshift = UNIPHIER_UART_REGSHIFT;
  207. up.port.flags = UPF_FIXED_PORT | UPF_FIXED_TYPE;
  208. up.capabilities = UART_CAP_FIFO;
  209. up.port.serial_in = uniphier_serial_in;
  210. up.port.serial_out = uniphier_serial_out;
  211. up.dl_read = uniphier_serial_dl_read;
  212. up.dl_write = uniphier_serial_dl_write;
  213. ret = serial8250_register_8250_port(&up);
  214. if (ret < 0) {
  215. dev_err(dev, "failed to register 8250 port\n");
  216. clk_disable_unprepare(priv->clk);
  217. return ret;
  218. }
  219. platform_set_drvdata(pdev, priv);
  220. return 0;
  221. }
  222. static int uniphier_uart_remove(struct platform_device *pdev)
  223. {
  224. struct uniphier8250_priv *priv = platform_get_drvdata(pdev);
  225. serial8250_unregister_port(priv->line);
  226. clk_disable_unprepare(priv->clk);
  227. return 0;
  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. },
  241. };
  242. module_platform_driver(uniphier_uart_platform_driver);
  243. MODULE_AUTHOR("Masahiro Yamada <yamada.masahiro@socionext.com>");
  244. MODULE_DESCRIPTION("UniPhier UART driver");
  245. MODULE_LICENSE("GPL");