8250_uniphier.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * Copyright (C) 2015 Masahiro Yamada <[email protected]>
  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. #define UNIPHIER_UART_CHAR_FCR 3 /* Character / FIFO Control Register */
  24. #define UNIPHIER_UART_LCR_MCR 4 /* Line/Modem Control Register */
  25. #define UNIPHIER_UART_LCR_SHIFT 8
  26. #define UNIPHIER_UART_DLR 9 /* Divisor Latch Register */
  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 = 2;
  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, and MCR.
  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_LCR:
  60. valshift = UNIPHIER_UART_LCR_SHIFT;
  61. /* fall through */
  62. case UART_MCR:
  63. offset = UNIPHIER_UART_LCR_MCR;
  64. break;
  65. default:
  66. break;
  67. }
  68. offset <<= p->regshift;
  69. /*
  70. * The return value must be masked with 0xff because LCR and MCR reside
  71. * in the same register that must be accessed by 32-bit write/read.
  72. * 8 or 16 bit access to this hardware result in unexpected behavior.
  73. */
  74. return (readl(p->membase + offset) >> valshift) & 0xff;
  75. }
  76. static void uniphier_serial_out(struct uart_port *p, int offset, int value)
  77. {
  78. unsigned int valshift = 0;
  79. bool normal = false;
  80. switch (offset) {
  81. case UART_FCR:
  82. offset = UNIPHIER_UART_CHAR_FCR;
  83. break;
  84. case UART_LCR:
  85. valshift = UNIPHIER_UART_LCR_SHIFT;
  86. /* Divisor latch access bit does not exist. */
  87. value &= ~UART_LCR_DLAB;
  88. /* fall through */
  89. case UART_MCR:
  90. offset = UNIPHIER_UART_LCR_MCR;
  91. break;
  92. default:
  93. normal = true;
  94. break;
  95. }
  96. offset <<= p->regshift;
  97. if (normal) {
  98. writel(value, p->membase + offset);
  99. } else {
  100. /*
  101. * Special case: two registers share the same address that
  102. * must be 32-bit accessed. As this is not longer atomic safe,
  103. * take a lock just in case.
  104. */
  105. struct uniphier8250_priv *priv = p->private_data;
  106. unsigned long flags;
  107. u32 tmp;
  108. spin_lock_irqsave(&priv->atomic_write_lock, flags);
  109. tmp = readl(p->membase + offset);
  110. tmp &= ~(0xff << valshift);
  111. tmp |= value << valshift;
  112. writel(tmp, p->membase + offset);
  113. spin_unlock_irqrestore(&priv->atomic_write_lock, flags);
  114. }
  115. }
  116. /*
  117. * This hardware does not have the divisor latch access bit.
  118. * The divisor latch register exists at different address.
  119. * Override dl_read/write callbacks.
  120. */
  121. static int uniphier_serial_dl_read(struct uart_8250_port *up)
  122. {
  123. int offset = UNIPHIER_UART_DLR << up->port.regshift;
  124. return readl(up->port.membase + offset);
  125. }
  126. static void uniphier_serial_dl_write(struct uart_8250_port *up, int value)
  127. {
  128. int offset = UNIPHIER_UART_DLR << up->port.regshift;
  129. writel(value, up->port.membase + offset);
  130. }
  131. static int uniphier_of_serial_setup(struct device *dev, struct uart_port *port,
  132. struct uniphier8250_priv *priv)
  133. {
  134. int ret;
  135. u32 prop;
  136. struct device_node *np = dev->of_node;
  137. ret = of_alias_get_id(np, "serial");
  138. if (ret < 0) {
  139. dev_err(dev, "failed to get alias id\n");
  140. return ret;
  141. }
  142. port->line = priv->line = ret;
  143. /* Get clk rate through clk driver */
  144. priv->clk = devm_clk_get(dev, NULL);
  145. if (IS_ERR(priv->clk)) {
  146. dev_err(dev, "failed to get clock\n");
  147. return PTR_ERR(priv->clk);
  148. }
  149. ret = clk_prepare_enable(priv->clk);
  150. if (ret < 0)
  151. return ret;
  152. port->uartclk = clk_get_rate(priv->clk);
  153. /* Check for fifo size */
  154. if (of_property_read_u32(np, "fifo-size", &prop) == 0)
  155. port->fifosize = prop;
  156. else
  157. port->fifosize = UNIPHIER_UART_DEFAULT_FIFO_SIZE;
  158. return 0;
  159. }
  160. static int uniphier_uart_probe(struct platform_device *pdev)
  161. {
  162. struct device *dev = &pdev->dev;
  163. struct uart_8250_port up;
  164. struct uniphier8250_priv *priv;
  165. struct resource *regs;
  166. void __iomem *membase;
  167. int irq;
  168. int ret;
  169. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  170. if (!regs) {
  171. dev_err(dev, "failed to get memory resource\n");
  172. return -EINVAL;
  173. }
  174. membase = devm_ioremap(dev, regs->start, resource_size(regs));
  175. if (!membase)
  176. return -ENOMEM;
  177. irq = platform_get_irq(pdev, 0);
  178. if (irq < 0) {
  179. dev_err(dev, "failed to get IRQ number\n");
  180. return irq;
  181. }
  182. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  183. if (!priv)
  184. return -ENOMEM;
  185. memset(&up, 0, sizeof(up));
  186. ret = uniphier_of_serial_setup(dev, &up.port, priv);
  187. if (ret < 0)
  188. return ret;
  189. spin_lock_init(&priv->atomic_write_lock);
  190. up.port.dev = dev;
  191. up.port.private_data = priv;
  192. up.port.mapbase = regs->start;
  193. up.port.mapsize = resource_size(regs);
  194. up.port.membase = membase;
  195. up.port.irq = irq;
  196. up.port.type = PORT_16550A;
  197. up.port.iotype = UPIO_MEM32;
  198. up.port.regshift = 2;
  199. up.port.flags = UPF_FIXED_PORT | UPF_FIXED_TYPE;
  200. up.capabilities = UART_CAP_FIFO;
  201. up.port.serial_in = uniphier_serial_in;
  202. up.port.serial_out = uniphier_serial_out;
  203. up.dl_read = uniphier_serial_dl_read;
  204. up.dl_write = uniphier_serial_dl_write;
  205. ret = serial8250_register_8250_port(&up);
  206. if (ret < 0) {
  207. dev_err(dev, "failed to register 8250 port\n");
  208. clk_disable_unprepare(priv->clk);
  209. return ret;
  210. }
  211. platform_set_drvdata(pdev, priv);
  212. return 0;
  213. }
  214. static int uniphier_uart_remove(struct platform_device *pdev)
  215. {
  216. struct uniphier8250_priv *priv = platform_get_drvdata(pdev);
  217. serial8250_unregister_port(priv->line);
  218. clk_disable_unprepare(priv->clk);
  219. return 0;
  220. }
  221. static const struct of_device_id uniphier_uart_match[] = {
  222. { .compatible = "socionext,uniphier-uart" },
  223. { /* sentinel */ }
  224. };
  225. MODULE_DEVICE_TABLE(of, uniphier_uart_match);
  226. static struct platform_driver uniphier_uart_platform_driver = {
  227. .probe = uniphier_uart_probe,
  228. .remove = uniphier_uart_remove,
  229. .driver = {
  230. .name = "uniphier-uart",
  231. .of_match_table = uniphier_uart_match,
  232. },
  233. };
  234. module_platform_driver(uniphier_uart_platform_driver);
  235. MODULE_AUTHOR("Masahiro Yamada <[email protected]>");
  236. MODULE_DESCRIPTION("UniPhier UART driver");
  237. MODULE_LICENSE("GPL");