8250_of.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. port->flags = UPF_SHARE_IRQ | UPF_BOOT_AUTOCONF | UPF_FIXED_PORT |
  83. UPF_FIXED_TYPE;
  84. spin_lock_init(&port->lock);
  85. if (resource_type(&resource) == IORESOURCE_IO) {
  86. port->iotype = UPIO_PORT;
  87. port->iobase = resource.start;
  88. } else {
  89. port->mapbase = resource.start;
  90. port->mapsize = resource_size(&resource);
  91. /* Check for shifted address mapping */
  92. if (of_property_read_u32(np, "reg-offset", &prop) == 0)
  93. port->mapbase += prop;
  94. port->iotype = UPIO_MEM;
  95. if (of_property_read_u32(np, "reg-io-width", &prop) == 0) {
  96. switch (prop) {
  97. case 1:
  98. port->iotype = UPIO_MEM;
  99. break;
  100. case 2:
  101. port->iotype = UPIO_MEM16;
  102. break;
  103. case 4:
  104. port->iotype = of_device_is_big_endian(np) ?
  105. UPIO_MEM32BE : UPIO_MEM32;
  106. break;
  107. default:
  108. dev_warn(&ofdev->dev, "unsupported reg-io-width (%d)\n",
  109. prop);
  110. ret = -EINVAL;
  111. goto err_unprepare;
  112. }
  113. }
  114. port->flags |= UPF_IOREMAP;
  115. }
  116. /* Check for registers offset within the devices address range */
  117. if (of_property_read_u32(np, "reg-shift", &prop) == 0)
  118. port->regshift = prop;
  119. /* Check for fifo size */
  120. if (of_property_read_u32(np, "fifo-size", &prop) == 0)
  121. port->fifosize = prop;
  122. /* Check for a fixed line number */
  123. ret = of_alias_get_id(np, "serial");
  124. if (ret >= 0)
  125. port->line = ret;
  126. port->irq = irq_of_parse_and_map(np, 0);
  127. if (!port->irq) {
  128. ret = -EPROBE_DEFER;
  129. goto err_unprepare;
  130. }
  131. info->rst = devm_reset_control_get_optional_shared(&ofdev->dev, NULL);
  132. if (IS_ERR(info->rst)) {
  133. ret = PTR_ERR(info->rst);
  134. goto err_dispose;
  135. }
  136. ret = reset_control_deassert(info->rst);
  137. if (ret)
  138. goto err_dispose;
  139. port->type = type;
  140. port->uartclk = clk;
  141. port->irqflags |= IRQF_SHARED;
  142. if (of_property_read_bool(np, "no-loopback-test"))
  143. port->flags |= UPF_SKIP_TEST;
  144. port->dev = &ofdev->dev;
  145. switch (type) {
  146. case PORT_TEGRA:
  147. port->handle_break = tegra_serial_handle_break;
  148. break;
  149. case PORT_RT2880:
  150. port->iotype = UPIO_AU;
  151. break;
  152. }
  153. if (IS_ENABLED(CONFIG_SERIAL_8250_FSL) &&
  154. (of_device_is_compatible(np, "fsl,ns16550") ||
  155. of_device_is_compatible(np, "fsl,16550-FIFO64")))
  156. port->handle_irq = fsl8250_handle_irq;
  157. return 0;
  158. err_dispose:
  159. irq_dispose_mapping(port->irq);
  160. err_unprepare:
  161. clk_disable_unprepare(info->clk);
  162. err_pmruntime:
  163. pm_runtime_put_sync(&ofdev->dev);
  164. pm_runtime_disable(&ofdev->dev);
  165. return ret;
  166. }
  167. /*
  168. * Try to register a serial port
  169. */
  170. static const struct of_device_id of_platform_serial_table[];
  171. static int of_platform_serial_probe(struct platform_device *ofdev)
  172. {
  173. const struct of_device_id *match;
  174. struct of_serial_info *info;
  175. struct uart_8250_port port8250;
  176. u32 tx_threshold;
  177. int port_type;
  178. int ret;
  179. match = of_match_device(of_platform_serial_table, &ofdev->dev);
  180. if (!match)
  181. return -EINVAL;
  182. if (of_property_read_bool(ofdev->dev.of_node, "used-by-rtas"))
  183. return -EBUSY;
  184. info = kzalloc(sizeof(*info), GFP_KERNEL);
  185. if (info == NULL)
  186. return -ENOMEM;
  187. port_type = (unsigned long)match->data;
  188. memset(&port8250, 0, sizeof(port8250));
  189. ret = of_platform_serial_setup(ofdev, port_type, &port8250.port, info);
  190. if (ret)
  191. goto err_free;
  192. if (port8250.port.fifosize)
  193. port8250.capabilities = UART_CAP_FIFO;
  194. /* Check for TX FIFO threshold & set tx_loadsz */
  195. if ((of_property_read_u32(ofdev->dev.of_node, "tx-threshold",
  196. &tx_threshold) == 0) &&
  197. (tx_threshold < port8250.port.fifosize))
  198. port8250.tx_loadsz = port8250.port.fifosize - tx_threshold;
  199. if (of_property_read_bool(ofdev->dev.of_node, "auto-flow-control"))
  200. port8250.capabilities |= UART_CAP_AFE;
  201. ret = serial8250_register_8250_port(&port8250);
  202. if (ret < 0)
  203. goto err_dispose;
  204. info->type = port_type;
  205. info->line = ret;
  206. platform_set_drvdata(ofdev, info);
  207. return 0;
  208. err_dispose:
  209. irq_dispose_mapping(port8250.port.irq);
  210. pm_runtime_put_sync(&ofdev->dev);
  211. pm_runtime_disable(&ofdev->dev);
  212. clk_disable_unprepare(info->clk);
  213. err_free:
  214. kfree(info);
  215. return ret;
  216. }
  217. /*
  218. * Release a line
  219. */
  220. static int of_platform_serial_remove(struct platform_device *ofdev)
  221. {
  222. struct of_serial_info *info = platform_get_drvdata(ofdev);
  223. serial8250_unregister_port(info->line);
  224. reset_control_assert(info->rst);
  225. pm_runtime_put_sync(&ofdev->dev);
  226. pm_runtime_disable(&ofdev->dev);
  227. clk_disable_unprepare(info->clk);
  228. kfree(info);
  229. return 0;
  230. }
  231. #ifdef CONFIG_PM_SLEEP
  232. static int of_serial_suspend(struct device *dev)
  233. {
  234. struct of_serial_info *info = dev_get_drvdata(dev);
  235. struct uart_8250_port *port8250 = serial8250_get_port(info->line);
  236. struct uart_port *port = &port8250->port;
  237. serial8250_suspend_port(info->line);
  238. if (!uart_console(port) || console_suspend_enabled) {
  239. pm_runtime_put_sync(dev);
  240. clk_disable_unprepare(info->clk);
  241. }
  242. return 0;
  243. }
  244. static int of_serial_resume(struct device *dev)
  245. {
  246. struct of_serial_info *info = dev_get_drvdata(dev);
  247. struct uart_8250_port *port8250 = serial8250_get_port(info->line);
  248. struct uart_port *port = &port8250->port;
  249. if (!uart_console(port) || console_suspend_enabled) {
  250. pm_runtime_get_sync(dev);
  251. clk_prepare_enable(info->clk);
  252. }
  253. serial8250_resume_port(info->line);
  254. return 0;
  255. }
  256. #endif
  257. static SIMPLE_DEV_PM_OPS(of_serial_pm_ops, of_serial_suspend, of_serial_resume);
  258. /*
  259. * A few common types, add more as needed.
  260. */
  261. static const struct of_device_id of_platform_serial_table[] = {
  262. { .compatible = "ns8250", .data = (void *)PORT_8250, },
  263. { .compatible = "ns16450", .data = (void *)PORT_16450, },
  264. { .compatible = "ns16550a", .data = (void *)PORT_16550A, },
  265. { .compatible = "ns16550", .data = (void *)PORT_16550, },
  266. { .compatible = "ns16750", .data = (void *)PORT_16750, },
  267. { .compatible = "ns16850", .data = (void *)PORT_16850, },
  268. { .compatible = "nvidia,tegra20-uart", .data = (void *)PORT_TEGRA, },
  269. { .compatible = "nxp,lpc3220-uart", .data = (void *)PORT_LPC3220, },
  270. { .compatible = "ralink,rt2880-uart", .data = (void *)PORT_RT2880, },
  271. { .compatible = "altr,16550-FIFO32",
  272. .data = (void *)PORT_ALTR_16550_F32, },
  273. { .compatible = "altr,16550-FIFO64",
  274. .data = (void *)PORT_ALTR_16550_F64, },
  275. { .compatible = "altr,16550-FIFO128",
  276. .data = (void *)PORT_ALTR_16550_F128, },
  277. { .compatible = "mediatek,mtk-btif",
  278. .data = (void *)PORT_MTK_BTIF, },
  279. { .compatible = "mrvl,mmp-uart",
  280. .data = (void *)PORT_XSCALE, },
  281. { .compatible = "ti,da830-uart", .data = (void *)PORT_DA830, },
  282. { .compatible = "nuvoton,npcm750-uart", .data = (void *)PORT_NPCM, },
  283. { /* end of list */ },
  284. };
  285. MODULE_DEVICE_TABLE(of, of_platform_serial_table);
  286. static struct platform_driver of_platform_serial_driver = {
  287. .driver = {
  288. .name = "of_serial",
  289. .of_match_table = of_platform_serial_table,
  290. .pm = &of_serial_pm_ops,
  291. },
  292. .probe = of_platform_serial_probe,
  293. .remove = of_platform_serial_remove,
  294. };
  295. module_platform_driver(of_platform_serial_driver);
  296. MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>");
  297. MODULE_LICENSE("GPL");
  298. MODULE_DESCRIPTION("Serial Port driver for Open Firmware platform devices");