8250_of.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Serial Port driver for Open Firmware platform devices
  4. *
  5. * Copyright (C) 2006 Arnd Bergmann <arnd@arndb.de>, IBM Corp.
  6. */
  7. #include <linux/console.h>
  8. #include <linux/module.h>
  9. #include <linux/slab.h>
  10. #include <linux/delay.h>
  11. #include <linux/serial_core.h>
  12. #include <linux/serial_reg.h>
  13. #include <linux/of_address.h>
  14. #include <linux/of_irq.h>
  15. #include <linux/of_platform.h>
  16. #include <linux/pm_runtime.h>
  17. #include <linux/clk.h>
  18. #include <linux/reset.h>
  19. #include "8250.h"
  20. struct of_serial_info {
  21. struct clk *clk;
  22. struct reset_control *rst;
  23. int type;
  24. int line;
  25. };
  26. #ifdef CONFIG_ARCH_TEGRA
  27. static void tegra_serial_handle_break(struct uart_port *p)
  28. {
  29. unsigned int status, tmout = 10000;
  30. do {
  31. status = p->serial_in(p, UART_LSR);
  32. if (status & (UART_LSR_FIFOE | UART_LSR_BRK_ERROR_BITS))
  33. status = p->serial_in(p, UART_RX);
  34. else
  35. break;
  36. if (--tmout == 0)
  37. break;
  38. udelay(1);
  39. } while (1);
  40. }
  41. #else
  42. static inline void tegra_serial_handle_break(struct uart_port *port)
  43. {
  44. }
  45. #endif
  46. /*
  47. * Fill a struct uart_port for a given device node
  48. */
  49. static int of_platform_serial_setup(struct platform_device *ofdev,
  50. int type, struct uart_port *port,
  51. struct of_serial_info *info)
  52. {
  53. struct resource resource;
  54. struct device_node *np = ofdev->dev.of_node;
  55. u32 clk, spd, prop;
  56. int ret;
  57. memset(port, 0, sizeof *port);
  58. pm_runtime_enable(&ofdev->dev);
  59. pm_runtime_get_sync(&ofdev->dev);
  60. if (of_property_read_u32(np, "clock-frequency", &clk)) {
  61. /* Get clk rate through clk driver if present */
  62. info->clk = devm_clk_get(&ofdev->dev, NULL);
  63. if (IS_ERR(info->clk)) {
  64. dev_warn(&ofdev->dev,
  65. "clk or clock-frequency not defined\n");
  66. ret = PTR_ERR(info->clk);
  67. goto err_pmruntime;
  68. }
  69. ret = clk_prepare_enable(info->clk);
  70. if (ret < 0)
  71. goto err_pmruntime;
  72. clk = clk_get_rate(info->clk);
  73. }
  74. /* If current-speed was set, then try not to change it. */
  75. if (of_property_read_u32(np, "current-speed", &spd) == 0)
  76. port->custom_divisor = clk / (16 * spd);
  77. ret = of_address_to_resource(np, 0, &resource);
  78. if (ret) {
  79. dev_warn(&ofdev->dev, "invalid address\n");
  80. goto err_unprepare;
  81. }
  82. spin_lock_init(&port->lock);
  83. port->mapbase = resource.start;
  84. port->mapsize = resource_size(&resource);
  85. /* Check for shifted address mapping */
  86. if (of_property_read_u32(np, "reg-offset", &prop) == 0)
  87. port->mapbase += prop;
  88. /* Check for registers offset within the devices address range */
  89. if (of_property_read_u32(np, "reg-shift", &prop) == 0)
  90. port->regshift = prop;
  91. /* Check for fifo size */
  92. if (of_property_read_u32(np, "fifo-size", &prop) == 0)
  93. port->fifosize = prop;
  94. /* Check for a fixed line number */
  95. ret = of_alias_get_id(np, "serial");
  96. if (ret >= 0)
  97. port->line = ret;
  98. port->irq = irq_of_parse_and_map(np, 0);
  99. port->iotype = UPIO_MEM;
  100. if (of_property_read_u32(np, "reg-io-width", &prop) == 0) {
  101. switch (prop) {
  102. case 1:
  103. port->iotype = UPIO_MEM;
  104. break;
  105. case 2:
  106. port->iotype = UPIO_MEM16;
  107. break;
  108. case 4:
  109. port->iotype = of_device_is_big_endian(np) ?
  110. UPIO_MEM32BE : UPIO_MEM32;
  111. break;
  112. default:
  113. dev_warn(&ofdev->dev, "unsupported reg-io-width (%d)\n",
  114. prop);
  115. ret = -EINVAL;
  116. goto err_dispose;
  117. }
  118. }
  119. info->rst = devm_reset_control_get_optional_shared(&ofdev->dev, NULL);
  120. if (IS_ERR(info->rst)) {
  121. ret = PTR_ERR(info->rst);
  122. goto err_dispose;
  123. }
  124. ret = reset_control_deassert(info->rst);
  125. if (ret)
  126. goto err_dispose;
  127. port->type = type;
  128. port->uartclk = clk;
  129. port->flags = UPF_SHARE_IRQ | UPF_BOOT_AUTOCONF | UPF_IOREMAP
  130. | UPF_FIXED_PORT | UPF_FIXED_TYPE;
  131. if (of_property_read_bool(np, "no-loopback-test"))
  132. port->flags |= UPF_SKIP_TEST;
  133. port->dev = &ofdev->dev;
  134. switch (type) {
  135. case PORT_TEGRA:
  136. port->handle_break = tegra_serial_handle_break;
  137. break;
  138. case PORT_RT2880:
  139. port->iotype = UPIO_AU;
  140. break;
  141. }
  142. if (IS_ENABLED(CONFIG_SERIAL_8250_FSL) &&
  143. (of_device_is_compatible(np, "fsl,ns16550") ||
  144. of_device_is_compatible(np, "fsl,16550-FIFO64")))
  145. port->handle_irq = fsl8250_handle_irq;
  146. return 0;
  147. err_dispose:
  148. irq_dispose_mapping(port->irq);
  149. err_unprepare:
  150. clk_disable_unprepare(info->clk);
  151. err_pmruntime:
  152. pm_runtime_put_sync(&ofdev->dev);
  153. pm_runtime_disable(&ofdev->dev);
  154. return ret;
  155. }
  156. /*
  157. * Try to register a serial port
  158. */
  159. static const struct of_device_id of_platform_serial_table[];
  160. static int of_platform_serial_probe(struct platform_device *ofdev)
  161. {
  162. const struct of_device_id *match;
  163. struct of_serial_info *info;
  164. struct uart_8250_port port8250;
  165. u32 tx_threshold;
  166. int port_type;
  167. int ret;
  168. match = of_match_device(of_platform_serial_table, &ofdev->dev);
  169. if (!match)
  170. return -EINVAL;
  171. if (of_property_read_bool(ofdev->dev.of_node, "used-by-rtas"))
  172. return -EBUSY;
  173. info = kzalloc(sizeof(*info), GFP_KERNEL);
  174. if (info == NULL)
  175. return -ENOMEM;
  176. port_type = (unsigned long)match->data;
  177. memset(&port8250, 0, sizeof(port8250));
  178. ret = of_platform_serial_setup(ofdev, port_type, &port8250.port, info);
  179. if (ret)
  180. goto err_free;
  181. if (port8250.port.fifosize)
  182. port8250.capabilities = UART_CAP_FIFO;
  183. /* Check for TX FIFO threshold & set tx_loadsz */
  184. if ((of_property_read_u32(ofdev->dev.of_node, "tx-threshold",
  185. &tx_threshold) == 0) &&
  186. (tx_threshold < port8250.port.fifosize))
  187. port8250.tx_loadsz = port8250.port.fifosize - tx_threshold;
  188. if (of_property_read_bool(ofdev->dev.of_node, "auto-flow-control"))
  189. port8250.capabilities |= UART_CAP_AFE;
  190. ret = serial8250_register_8250_port(&port8250);
  191. if (ret < 0)
  192. goto err_dispose;
  193. info->type = port_type;
  194. info->line = ret;
  195. platform_set_drvdata(ofdev, info);
  196. return 0;
  197. err_dispose:
  198. irq_dispose_mapping(port8250.port.irq);
  199. pm_runtime_put_sync(&ofdev->dev);
  200. pm_runtime_disable(&ofdev->dev);
  201. clk_disable_unprepare(info->clk);
  202. err_free:
  203. kfree(info);
  204. return ret;
  205. }
  206. /*
  207. * Release a line
  208. */
  209. static int of_platform_serial_remove(struct platform_device *ofdev)
  210. {
  211. struct of_serial_info *info = platform_get_drvdata(ofdev);
  212. serial8250_unregister_port(info->line);
  213. reset_control_assert(info->rst);
  214. pm_runtime_put_sync(&ofdev->dev);
  215. pm_runtime_disable(&ofdev->dev);
  216. clk_disable_unprepare(info->clk);
  217. kfree(info);
  218. return 0;
  219. }
  220. #ifdef CONFIG_PM_SLEEP
  221. static int of_serial_suspend(struct device *dev)
  222. {
  223. struct of_serial_info *info = dev_get_drvdata(dev);
  224. struct uart_8250_port *port8250 = serial8250_get_port(info->line);
  225. struct uart_port *port = &port8250->port;
  226. serial8250_suspend_port(info->line);
  227. if (!uart_console(port) || console_suspend_enabled) {
  228. pm_runtime_put_sync(dev);
  229. clk_disable_unprepare(info->clk);
  230. }
  231. return 0;
  232. }
  233. static int of_serial_resume(struct device *dev)
  234. {
  235. struct of_serial_info *info = dev_get_drvdata(dev);
  236. struct uart_8250_port *port8250 = serial8250_get_port(info->line);
  237. struct uart_port *port = &port8250->port;
  238. if (!uart_console(port) || console_suspend_enabled) {
  239. pm_runtime_get_sync(dev);
  240. clk_prepare_enable(info->clk);
  241. }
  242. serial8250_resume_port(info->line);
  243. return 0;
  244. }
  245. #endif
  246. static SIMPLE_DEV_PM_OPS(of_serial_pm_ops, of_serial_suspend, of_serial_resume);
  247. /*
  248. * A few common types, add more as needed.
  249. */
  250. static const struct of_device_id of_platform_serial_table[] = {
  251. { .compatible = "ns8250", .data = (void *)PORT_8250, },
  252. { .compatible = "ns16450", .data = (void *)PORT_16450, },
  253. { .compatible = "ns16550a", .data = (void *)PORT_16550A, },
  254. { .compatible = "ns16550", .data = (void *)PORT_16550, },
  255. { .compatible = "ns16750", .data = (void *)PORT_16750, },
  256. { .compatible = "ns16850", .data = (void *)PORT_16850, },
  257. { .compatible = "nvidia,tegra20-uart", .data = (void *)PORT_TEGRA, },
  258. { .compatible = "nxp,lpc3220-uart", .data = (void *)PORT_LPC3220, },
  259. { .compatible = "ralink,rt2880-uart", .data = (void *)PORT_RT2880, },
  260. { .compatible = "altr,16550-FIFO32",
  261. .data = (void *)PORT_ALTR_16550_F32, },
  262. { .compatible = "altr,16550-FIFO64",
  263. .data = (void *)PORT_ALTR_16550_F64, },
  264. { .compatible = "altr,16550-FIFO128",
  265. .data = (void *)PORT_ALTR_16550_F128, },
  266. { .compatible = "mediatek,mtk-btif",
  267. .data = (void *)PORT_MTK_BTIF, },
  268. { .compatible = "mrvl,mmp-uart",
  269. .data = (void *)PORT_XSCALE, },
  270. { .compatible = "ti,da830-uart", .data = (void *)PORT_DA830, },
  271. { .compatible = "nuvoton,npcm750-uart", .data = (void *)PORT_NPCM, },
  272. { /* end of list */ },
  273. };
  274. MODULE_DEVICE_TABLE(of, of_platform_serial_table);
  275. static struct platform_driver of_platform_serial_driver = {
  276. .driver = {
  277. .name = "of_serial",
  278. .of_match_table = of_platform_serial_table,
  279. .pm = &of_serial_pm_ops,
  280. },
  281. .probe = of_platform_serial_probe,
  282. .remove = of_platform_serial_remove,
  283. };
  284. module_platform_driver(of_platform_serial_driver);
  285. MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>");
  286. MODULE_LICENSE("GPL");
  287. MODULE_DESCRIPTION("Serial Port driver for Open Firmware platform devices");