8250_of.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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/bits.h>
  8. #include <linux/console.h>
  9. #include <linux/math.h>
  10. #include <linux/module.h>
  11. #include <linux/slab.h>
  12. #include <linux/serial_core.h>
  13. #include <linux/serial_reg.h>
  14. #include <linux/of_address.h>
  15. #include <linux/of_irq.h>
  16. #include <linux/of_platform.h>
  17. #include <linux/pm_runtime.h>
  18. #include <linux/clk.h>
  19. #include <linux/reset.h>
  20. #include <linux/notifier.h>
  21. #include "8250.h"
  22. struct of_serial_info {
  23. struct clk *clk;
  24. struct clk *bus_clk;
  25. struct reset_control *rst;
  26. int type;
  27. int line;
  28. struct notifier_block clk_notifier;
  29. };
  30. /* Nuvoton NPCM timeout register */
  31. #define UART_NPCM_TOR 7
  32. #define UART_NPCM_TOIE BIT(7) /* Timeout Interrupt Enable */
  33. static int npcm_startup(struct uart_port *port)
  34. {
  35. /*
  36. * Nuvoton calls the scratch register 'UART_TOR' (timeout
  37. * register). Enable it, and set TIOC (timeout interrupt
  38. * comparator) to be 0x20 for correct operation.
  39. */
  40. serial_port_out(port, UART_NPCM_TOR, UART_NPCM_TOIE | 0x20);
  41. return serial8250_do_startup(port);
  42. }
  43. /* Nuvoton NPCM UARTs have a custom divisor calculation */
  44. static unsigned int npcm_get_divisor(struct uart_port *port, unsigned int baud,
  45. unsigned int *frac)
  46. {
  47. return DIV_ROUND_CLOSEST(port->uartclk, 16 * baud + 2) - 2;
  48. }
  49. static int npcm_setup(struct uart_port *port)
  50. {
  51. port->get_divisor = npcm_get_divisor;
  52. port->startup = npcm_startup;
  53. return 0;
  54. }
  55. static inline struct of_serial_info *clk_nb_to_info(struct notifier_block *nb)
  56. {
  57. return container_of(nb, struct of_serial_info, clk_notifier);
  58. }
  59. static int of_platform_serial_clk_notifier_cb(struct notifier_block *nb, unsigned long event,
  60. void *data)
  61. {
  62. struct of_serial_info *info = clk_nb_to_info(nb);
  63. struct uart_8250_port *port8250 = serial8250_get_port(info->line);
  64. struct clk_notifier_data *ndata = data;
  65. if (event == POST_RATE_CHANGE) {
  66. serial8250_update_uartclk(&port8250->port, ndata->new_rate);
  67. return NOTIFY_OK;
  68. }
  69. return NOTIFY_DONE;
  70. }
  71. /*
  72. * Fill a struct uart_port for a given device node
  73. */
  74. static int of_platform_serial_setup(struct platform_device *ofdev,
  75. int type, struct uart_8250_port *up,
  76. struct of_serial_info *info)
  77. {
  78. struct resource resource;
  79. struct device *dev = &ofdev->dev;
  80. struct device_node *np = dev->of_node;
  81. struct uart_port *port = &up->port;
  82. u32 spd;
  83. int ret;
  84. memset(port, 0, sizeof(*port));
  85. pm_runtime_enable(&ofdev->dev);
  86. pm_runtime_get_sync(&ofdev->dev);
  87. ret = of_address_to_resource(np, 0, &resource);
  88. if (ret) {
  89. dev_err_probe(dev, ret, "invalid address\n");
  90. goto err_pmruntime;
  91. }
  92. port->dev = &ofdev->dev;
  93. port->flags = UPF_BOOT_AUTOCONF | UPF_FIXED_PORT | UPF_FIXED_TYPE;
  94. spin_lock_init(&port->lock);
  95. if (resource_type(&resource) == IORESOURCE_IO) {
  96. port->iobase = resource.start;
  97. } else {
  98. port->mapbase = resource.start;
  99. port->mapsize = resource_size(&resource);
  100. port->flags |= UPF_IOREMAP;
  101. }
  102. ret = uart_read_and_validate_port_properties(port);
  103. if (ret)
  104. goto err_pmruntime;
  105. /* Get clk rate through clk driver if present */
  106. if (!port->uartclk) {
  107. struct clk *bus_clk;
  108. bus_clk = devm_clk_get_optional_enabled(dev, "bus");
  109. if (IS_ERR(bus_clk)) {
  110. ret = dev_err_probe(dev, PTR_ERR(bus_clk), "failed to get bus clock\n");
  111. goto err_pmruntime;
  112. }
  113. /* If the bus clock is required, core clock must be named */
  114. info->clk = devm_clk_get_enabled(dev, bus_clk ? "core" : NULL);
  115. if (IS_ERR(info->clk)) {
  116. ret = dev_err_probe(dev, PTR_ERR(info->clk), "failed to get clock\n");
  117. goto err_pmruntime;
  118. }
  119. info->bus_clk = bus_clk;
  120. port->uartclk = clk_get_rate(info->clk);
  121. }
  122. /* If current-speed was set, then try not to change it. */
  123. if (of_property_read_u32(np, "current-speed", &spd) == 0)
  124. port->custom_divisor = port->uartclk / (16 * spd);
  125. /* Compatibility with the deprecated pxa driver and 8250_pxa drivers. */
  126. if (of_device_is_compatible(np, "mrvl,mmp-uart"))
  127. port->regshift = 2;
  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_pmruntime;
  132. }
  133. ret = reset_control_deassert(info->rst);
  134. if (ret)
  135. goto err_pmruntime;
  136. port->type = type;
  137. port->rs485_config = serial8250_em485_config;
  138. port->rs485_supported = serial8250_em485_supported;
  139. up->rs485_start_tx = serial8250_em485_start_tx;
  140. up->rs485_stop_tx = serial8250_em485_stop_tx;
  141. switch (type) {
  142. case PORT_RT2880:
  143. ret = rt288x_setup(port);
  144. break;
  145. case PORT_NPCM:
  146. ret = npcm_setup(port);
  147. break;
  148. default:
  149. /* Nothing to do */
  150. ret = 0;
  151. break;
  152. }
  153. if (ret)
  154. goto err_pmruntime;
  155. if (IS_REACHABLE(CONFIG_SERIAL_8250_FSL) &&
  156. (of_device_is_compatible(np, "fsl,ns16550") ||
  157. of_device_is_compatible(np, "fsl,16550-FIFO64"))) {
  158. port->handle_irq = fsl8250_handle_irq;
  159. port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_8250_CONSOLE);
  160. }
  161. return 0;
  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 int of_platform_serial_probe(struct platform_device *ofdev)
  171. {
  172. struct of_serial_info *info;
  173. struct uart_8250_port port8250;
  174. unsigned int port_type;
  175. u32 tx_threshold;
  176. int ret;
  177. if (IS_ENABLED(CONFIG_SERIAL_8250_BCM7271) &&
  178. of_device_is_compatible(ofdev->dev.of_node, "brcm,bcm7271-uart"))
  179. return -ENODEV;
  180. port_type = (unsigned long)of_device_get_match_data(&ofdev->dev);
  181. if (port_type == PORT_UNKNOWN)
  182. return -EINVAL;
  183. if (of_property_read_bool(ofdev->dev.of_node, "used-by-rtas"))
  184. return -EBUSY;
  185. info = kzalloc_obj(*info);
  186. if (info == NULL)
  187. return -ENOMEM;
  188. memset(&port8250, 0, sizeof(port8250));
  189. ret = of_platform_serial_setup(ofdev, port_type, &port8250, 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. if (of_property_read_u32(ofdev->dev.of_node,
  202. "overrun-throttle-ms",
  203. &port8250.overrun_backoff_time_ms) != 0)
  204. port8250.overrun_backoff_time_ms = 0;
  205. ret = serial8250_register_8250_port(&port8250);
  206. if (ret < 0)
  207. goto err_dispose;
  208. info->type = port_type;
  209. info->line = ret;
  210. platform_set_drvdata(ofdev, info);
  211. if (info->clk) {
  212. info->clk_notifier.notifier_call = of_platform_serial_clk_notifier_cb;
  213. ret = clk_notifier_register(info->clk, &info->clk_notifier);
  214. if (ret) {
  215. dev_err_probe(port8250.port.dev, ret, "Failed to set the clock notifier\n");
  216. goto err_unregister;
  217. }
  218. }
  219. return 0;
  220. err_unregister:
  221. serial8250_unregister_port(info->line);
  222. err_dispose:
  223. pm_runtime_put_sync(&ofdev->dev);
  224. pm_runtime_disable(&ofdev->dev);
  225. err_free:
  226. kfree(info);
  227. return ret;
  228. }
  229. /*
  230. * Release a line
  231. */
  232. static void of_platform_serial_remove(struct platform_device *ofdev)
  233. {
  234. struct of_serial_info *info = platform_get_drvdata(ofdev);
  235. if (info->clk)
  236. clk_notifier_unregister(info->clk, &info->clk_notifier);
  237. serial8250_unregister_port(info->line);
  238. reset_control_assert(info->rst);
  239. pm_runtime_put_sync(&ofdev->dev);
  240. pm_runtime_disable(&ofdev->dev);
  241. kfree(info);
  242. }
  243. #ifdef CONFIG_PM_SLEEP
  244. static int of_serial_suspend(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. serial8250_suspend_port(info->line);
  250. if (!uart_console(port) || console_suspend_enabled) {
  251. pm_runtime_put_sync(dev);
  252. clk_disable_unprepare(info->clk);
  253. clk_disable_unprepare(info->bus_clk);
  254. }
  255. return 0;
  256. }
  257. static int of_serial_resume(struct device *dev)
  258. {
  259. struct of_serial_info *info = dev_get_drvdata(dev);
  260. struct uart_8250_port *port8250 = serial8250_get_port(info->line);
  261. struct uart_port *port = &port8250->port;
  262. if (!uart_console(port) || console_suspend_enabled) {
  263. pm_runtime_get_sync(dev);
  264. clk_prepare_enable(info->bus_clk);
  265. clk_prepare_enable(info->clk);
  266. }
  267. serial8250_resume_port(info->line);
  268. return 0;
  269. }
  270. #endif
  271. static SIMPLE_DEV_PM_OPS(of_serial_pm_ops, of_serial_suspend, of_serial_resume);
  272. /*
  273. * A few common types, add more as needed.
  274. */
  275. static const struct of_device_id of_platform_serial_table[] = {
  276. { .compatible = "ns8250", .data = (void *)PORT_8250, },
  277. { .compatible = "ns16450", .data = (void *)PORT_16450, },
  278. { .compatible = "ns16550a", .data = (void *)PORT_16550A, },
  279. { .compatible = "ns16550", .data = (void *)PORT_16550, },
  280. { .compatible = "ns16750", .data = (void *)PORT_16750, },
  281. { .compatible = "ns16850", .data = (void *)PORT_16850, },
  282. { .compatible = "nxp,lpc3220-uart", .data = (void *)PORT_LPC3220, },
  283. { .compatible = "ralink,rt2880-uart", .data = (void *)PORT_RT2880, },
  284. { .compatible = "intel,xscale-uart", .data = (void *)PORT_XSCALE, },
  285. { .compatible = "altr,16550-FIFO32",
  286. .data = (void *)PORT_ALTR_16550_F32, },
  287. { .compatible = "altr,16550-FIFO64",
  288. .data = (void *)PORT_ALTR_16550_F64, },
  289. { .compatible = "altr,16550-FIFO128",
  290. .data = (void *)PORT_ALTR_16550_F128, },
  291. { .compatible = "fsl,16550-FIFO64",
  292. .data = (void *)PORT_16550A_FSL64, },
  293. { .compatible = "mediatek,mtk-btif",
  294. .data = (void *)PORT_MTK_BTIF, },
  295. { .compatible = "mrvl,mmp-uart",
  296. .data = (void *)PORT_XSCALE, },
  297. { .compatible = "ti,da830-uart", .data = (void *)PORT_DA830, },
  298. { .compatible = "nuvoton,wpcm450-uart", .data = (void *)PORT_NPCM, },
  299. { .compatible = "nuvoton,npcm750-uart", .data = (void *)PORT_NPCM, },
  300. { /* end of list */ },
  301. };
  302. MODULE_DEVICE_TABLE(of, of_platform_serial_table);
  303. static struct platform_driver of_platform_serial_driver = {
  304. .driver = {
  305. .name = "of_serial",
  306. .of_match_table = of_platform_serial_table,
  307. .pm = &of_serial_pm_ops,
  308. },
  309. .probe = of_platform_serial_probe,
  310. .remove = of_platform_serial_remove,
  311. };
  312. module_platform_driver(of_platform_serial_driver);
  313. MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>");
  314. MODULE_LICENSE("GPL");
  315. MODULE_DESCRIPTION("Serial Port driver for Open Firmware platform devices");