8250_uniphier.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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/io.h>
  16. #include <linux/module.h>
  17. #include <linux/of.h>
  18. #include <linux/platform_device.h>
  19. #include "8250.h"
  20. /* Most (but not all) of UniPhier UART devices have 64-depth FIFO. */
  21. #define UNIPHIER_UART_DEFAULT_FIFO_SIZE 64
  22. #define UNIPHIER_UART_CHAR_FCR 3 /* Character / FIFO Control Register */
  23. #define UNIPHIER_UART_LCR_MCR 4 /* Line/Modem Control Register */
  24. #define UNIPHIER_UART_LCR_SHIFT 8
  25. #define UNIPHIER_UART_DLR 9 /* Divisor Latch Register */
  26. struct uniphier8250_priv {
  27. int line;
  28. struct clk *clk;
  29. spinlock_t atomic_write_lock;
  30. };
  31. /*
  32. * The register map is slightly different from that of 8250.
  33. * IO callbacks must be overridden for correct access to FCR, LCR, and MCR.
  34. */
  35. static unsigned int uniphier_serial_in(struct uart_port *p, int offset)
  36. {
  37. unsigned int valshift = 0;
  38. switch (offset) {
  39. case UART_LCR:
  40. valshift = UNIPHIER_UART_LCR_SHIFT;
  41. /* fall through */
  42. case UART_MCR:
  43. offset = UNIPHIER_UART_LCR_MCR;
  44. break;
  45. default:
  46. break;
  47. }
  48. offset <<= p->regshift;
  49. /*
  50. * The return value must be masked with 0xff because LCR and MCR reside
  51. * in the same register that must be accessed by 32-bit write/read.
  52. * 8 or 16 bit access to this hardware result in unexpected behavior.
  53. */
  54. return (readl(p->membase + offset) >> valshift) & 0xff;
  55. }
  56. static void uniphier_serial_out(struct uart_port *p, int offset, int value)
  57. {
  58. unsigned int valshift = 0;
  59. bool normal = false;
  60. switch (offset) {
  61. case UART_FCR:
  62. offset = UNIPHIER_UART_CHAR_FCR;
  63. break;
  64. case UART_LCR:
  65. valshift = UNIPHIER_UART_LCR_SHIFT;
  66. /* Divisor latch access bit does not exist. */
  67. value &= ~(UART_LCR_DLAB << valshift);
  68. /* fall through */
  69. case UART_MCR:
  70. offset = UNIPHIER_UART_LCR_MCR;
  71. break;
  72. default:
  73. normal = true;
  74. break;
  75. }
  76. offset <<= p->regshift;
  77. if (normal) {
  78. writel(value, p->membase + offset);
  79. } else {
  80. /*
  81. * Special case: two registers share the same address that
  82. * must be 32-bit accessed. As this is not longer atomic safe,
  83. * take a lock just in case.
  84. */
  85. struct uniphier8250_priv *priv = p->private_data;
  86. unsigned long flags;
  87. u32 tmp;
  88. spin_lock_irqsave(&priv->atomic_write_lock, flags);
  89. tmp = readl(p->membase + offset);
  90. tmp &= ~(0xff << valshift);
  91. tmp |= value << valshift;
  92. writel(tmp, p->membase + offset);
  93. spin_unlock_irqrestore(&priv->atomic_write_lock, flags);
  94. }
  95. }
  96. /*
  97. * This hardware does not have the divisor latch access bit.
  98. * The divisor latch register exists at different address.
  99. * Override dl_read/write callbacks.
  100. */
  101. static int uniphier_serial_dl_read(struct uart_8250_port *up)
  102. {
  103. return readl(up->port.membase + UNIPHIER_UART_DLR);
  104. }
  105. static void uniphier_serial_dl_write(struct uart_8250_port *up, int value)
  106. {
  107. writel(value, up->port.membase + UNIPHIER_UART_DLR);
  108. }
  109. static int uniphier_of_serial_setup(struct device *dev, struct uart_port *port,
  110. struct uniphier8250_priv *priv)
  111. {
  112. int ret;
  113. u32 prop;
  114. struct device_node *np = dev->of_node;
  115. ret = of_alias_get_id(np, "serial");
  116. if (ret < 0) {
  117. dev_err(dev, "failed to get alias id\n");
  118. return ret;
  119. }
  120. port->line = priv->line = ret;
  121. /* Get clk rate through clk driver */
  122. priv->clk = devm_clk_get(dev, NULL);
  123. if (IS_ERR(priv->clk)) {
  124. dev_err(dev, "failed to get clock\n");
  125. return PTR_ERR(priv->clk);
  126. }
  127. ret = clk_prepare_enable(priv->clk);
  128. if (ret < 0)
  129. return ret;
  130. port->uartclk = clk_get_rate(priv->clk);
  131. /* Check for fifo size */
  132. if (of_property_read_u32(np, "fifo-size", &prop) == 0)
  133. port->fifosize = prop;
  134. else
  135. port->fifosize = UNIPHIER_UART_DEFAULT_FIFO_SIZE;
  136. return 0;
  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");
  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. dev_err(dev, "failed to get IRQ number");
  158. return irq;
  159. }
  160. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  161. if (!priv)
  162. return -ENOMEM;
  163. memset(&up, 0, sizeof(up));
  164. ret = uniphier_of_serial_setup(dev, &up.port, priv);
  165. if (ret < 0)
  166. return ret;
  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. up.port.irq = irq;
  174. up.port.type = PORT_16550A;
  175. up.port.iotype = UPIO_MEM32;
  176. up.port.regshift = 2;
  177. up.port.flags = UPF_FIXED_PORT | UPF_FIXED_TYPE;
  178. up.capabilities = UART_CAP_FIFO;
  179. up.port.serial_in = uniphier_serial_in;
  180. up.port.serial_out = uniphier_serial_out;
  181. up.dl_read = uniphier_serial_dl_read;
  182. up.dl_write = uniphier_serial_dl_write;
  183. ret = serial8250_register_8250_port(&up);
  184. if (ret < 0) {
  185. dev_err(dev, "failed to register 8250 port\n");
  186. clk_disable_unprepare(priv->clk);
  187. return ret;
  188. }
  189. platform_set_drvdata(pdev, priv);
  190. return 0;
  191. }
  192. static int uniphier_uart_remove(struct platform_device *pdev)
  193. {
  194. struct uniphier8250_priv *priv = platform_get_drvdata(pdev);
  195. serial8250_unregister_port(priv->line);
  196. clk_disable_unprepare(priv->clk);
  197. return 0;
  198. }
  199. static const struct of_device_id uniphier_uart_match[] = {
  200. { .compatible = "socionext,uniphier-uart" },
  201. { /* sentinel */ }
  202. };
  203. MODULE_DEVICE_TABLE(of, uniphier_uart_match);
  204. static struct platform_driver uniphier_uart_platform_driver = {
  205. .probe = uniphier_uart_probe,
  206. .remove = uniphier_uart_remove,
  207. .driver = {
  208. .name = "uniphier-uart",
  209. .of_match_table = uniphier_uart_match,
  210. },
  211. };
  212. module_platform_driver(uniphier_uart_platform_driver);
  213. MODULE_AUTHOR("Masahiro Yamada <yamada.masahiro@socionext.com>");
  214. MODULE_DESCRIPTION("UniPhier UART driver");
  215. MODULE_LICENSE("GPL");