8250_of.c 9.4 KB

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