8250_of.c 9.2 KB

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