8250_of.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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 reset_control *rst;
  25. int type;
  26. int line;
  27. struct notifier_block clk_notifier;
  28. };
  29. /* Nuvoton NPCM timeout register */
  30. #define UART_NPCM_TOR 7
  31. #define UART_NPCM_TOIE BIT(7) /* Timeout Interrupt Enable */
  32. static int npcm_startup(struct uart_port *port)
  33. {
  34. /*
  35. * Nuvoton calls the scratch register 'UART_TOR' (timeout
  36. * register). Enable it, and set TIOC (timeout interrupt
  37. * comparator) to be 0x20 for correct operation.
  38. */
  39. serial_port_out(port, UART_NPCM_TOR, UART_NPCM_TOIE | 0x20);
  40. return serial8250_do_startup(port);
  41. }
  42. /* Nuvoton NPCM UARTs have a custom divisor calculation */
  43. static unsigned int npcm_get_divisor(struct uart_port *port, unsigned int baud,
  44. unsigned int *frac)
  45. {
  46. return DIV_ROUND_CLOSEST(port->uartclk, 16 * baud + 2) - 2;
  47. }
  48. static int npcm_setup(struct uart_port *port)
  49. {
  50. port->get_divisor = npcm_get_divisor;
  51. port->startup = npcm_startup;
  52. return 0;
  53. }
  54. static inline struct of_serial_info *clk_nb_to_info(struct notifier_block *nb)
  55. {
  56. return container_of(nb, struct of_serial_info, clk_notifier);
  57. }
  58. static int of_platform_serial_clk_notifier_cb(struct notifier_block *nb, unsigned long event,
  59. void *data)
  60. {
  61. struct of_serial_info *info = clk_nb_to_info(nb);
  62. struct uart_8250_port *port8250 = serial8250_get_port(info->line);
  63. struct clk_notifier_data *ndata = data;
  64. if (event == POST_RATE_CHANGE) {
  65. serial8250_update_uartclk(&port8250->port, ndata->new_rate);
  66. return NOTIFY_OK;
  67. }
  68. return NOTIFY_DONE;
  69. }
  70. /*
  71. * Fill a struct uart_port for a given device node
  72. */
  73. static int of_platform_serial_setup(struct platform_device *ofdev,
  74. int type, struct uart_8250_port *up,
  75. struct of_serial_info *info)
  76. {
  77. struct resource resource;
  78. struct device *dev = &ofdev->dev;
  79. struct device_node *np = dev->of_node;
  80. struct uart_port *port = &up->port;
  81. u32 spd;
  82. int ret;
  83. memset(port, 0, sizeof *port);
  84. pm_runtime_enable(&ofdev->dev);
  85. pm_runtime_get_sync(&ofdev->dev);
  86. ret = of_address_to_resource(np, 0, &resource);
  87. if (ret) {
  88. dev_err_probe(dev, ret, "invalid address\n");
  89. goto err_pmruntime;
  90. }
  91. port->dev = &ofdev->dev;
  92. port->flags = UPF_BOOT_AUTOCONF | UPF_FIXED_PORT | UPF_FIXED_TYPE;
  93. spin_lock_init(&port->lock);
  94. if (resource_type(&resource) == IORESOURCE_IO) {
  95. port->iotype = UPIO_PORT;
  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. info->clk = devm_clk_get_enabled(dev, NULL);
  108. if (IS_ERR(info->clk)) {
  109. ret = dev_err_probe(dev, PTR_ERR(info->clk), "failed to get clock\n");
  110. goto err_pmruntime;
  111. }
  112. port->uartclk = clk_get_rate(info->clk);
  113. }
  114. /* If current-speed was set, then try not to change it. */
  115. if (of_property_read_u32(np, "current-speed", &spd) == 0)
  116. port->custom_divisor = port->uartclk / (16 * spd);
  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. info->rst = devm_reset_control_get_optional_shared(&ofdev->dev, NULL);
  121. if (IS_ERR(info->rst)) {
  122. ret = PTR_ERR(info->rst);
  123. goto err_pmruntime;
  124. }
  125. ret = reset_control_deassert(info->rst);
  126. if (ret)
  127. goto err_pmruntime;
  128. port->type = type;
  129. port->rs485_config = serial8250_em485_config;
  130. port->rs485_supported = serial8250_em485_supported;
  131. up->rs485_start_tx = serial8250_em485_start_tx;
  132. up->rs485_stop_tx = serial8250_em485_stop_tx;
  133. switch (type) {
  134. case PORT_RT2880:
  135. ret = rt288x_setup(port);
  136. break;
  137. case PORT_NPCM:
  138. ret = npcm_setup(port);
  139. break;
  140. default:
  141. /* Nothing to do */
  142. ret = 0;
  143. break;
  144. }
  145. if (ret)
  146. goto err_pmruntime;
  147. if (IS_REACHABLE(CONFIG_SERIAL_8250_FSL) &&
  148. (of_device_is_compatible(np, "fsl,ns16550") ||
  149. of_device_is_compatible(np, "fsl,16550-FIFO64"))) {
  150. port->handle_irq = fsl8250_handle_irq;
  151. port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_8250_CONSOLE);
  152. }
  153. return 0;
  154. err_pmruntime:
  155. pm_runtime_put_sync(&ofdev->dev);
  156. pm_runtime_disable(&ofdev->dev);
  157. return ret;
  158. }
  159. /*
  160. * Try to register a serial port
  161. */
  162. static int of_platform_serial_probe(struct platform_device *ofdev)
  163. {
  164. struct of_serial_info *info;
  165. struct uart_8250_port port8250;
  166. unsigned int port_type;
  167. u32 tx_threshold;
  168. int ret;
  169. if (IS_ENABLED(CONFIG_SERIAL_8250_BCM7271) &&
  170. of_device_is_compatible(ofdev->dev.of_node, "brcm,bcm7271-uart"))
  171. return -ENODEV;
  172. port_type = (unsigned long)of_device_get_match_data(&ofdev->dev);
  173. if (port_type == PORT_UNKNOWN)
  174. return -EINVAL;
  175. if (of_property_read_bool(ofdev->dev.of_node, "used-by-rtas"))
  176. return -EBUSY;
  177. info = kzalloc(sizeof(*info), GFP_KERNEL);
  178. if (info == NULL)
  179. return -ENOMEM;
  180. memset(&port8250, 0, sizeof(port8250));
  181. ret = of_platform_serial_setup(ofdev, port_type, &port8250, info);
  182. if (ret)
  183. goto err_free;
  184. if (port8250.port.fifosize)
  185. port8250.capabilities = UART_CAP_FIFO;
  186. /* Check for TX FIFO threshold & set tx_loadsz */
  187. if ((of_property_read_u32(ofdev->dev.of_node, "tx-threshold",
  188. &tx_threshold) == 0) &&
  189. (tx_threshold < port8250.port.fifosize))
  190. port8250.tx_loadsz = port8250.port.fifosize - tx_threshold;
  191. if (of_property_read_bool(ofdev->dev.of_node, "auto-flow-control"))
  192. port8250.capabilities |= UART_CAP_AFE;
  193. if (of_property_read_u32(ofdev->dev.of_node,
  194. "overrun-throttle-ms",
  195. &port8250.overrun_backoff_time_ms) != 0)
  196. port8250.overrun_backoff_time_ms = 0;
  197. ret = serial8250_register_8250_port(&port8250);
  198. if (ret < 0)
  199. goto err_dispose;
  200. info->type = port_type;
  201. info->line = ret;
  202. platform_set_drvdata(ofdev, info);
  203. if (info->clk) {
  204. info->clk_notifier.notifier_call = of_platform_serial_clk_notifier_cb;
  205. ret = clk_notifier_register(info->clk, &info->clk_notifier);
  206. if (ret) {
  207. dev_err_probe(port8250.port.dev, ret, "Failed to set the clock notifier\n");
  208. goto err_unregister;
  209. }
  210. }
  211. return 0;
  212. err_unregister:
  213. serial8250_unregister_port(info->line);
  214. err_dispose:
  215. pm_runtime_put_sync(&ofdev->dev);
  216. pm_runtime_disable(&ofdev->dev);
  217. err_free:
  218. kfree(info);
  219. return ret;
  220. }
  221. /*
  222. * Release a line
  223. */
  224. static void of_platform_serial_remove(struct platform_device *ofdev)
  225. {
  226. struct of_serial_info *info = platform_get_drvdata(ofdev);
  227. if (info->clk)
  228. clk_notifier_unregister(info->clk, &info->clk_notifier);
  229. serial8250_unregister_port(info->line);
  230. reset_control_assert(info->rst);
  231. pm_runtime_put_sync(&ofdev->dev);
  232. pm_runtime_disable(&ofdev->dev);
  233. kfree(info);
  234. }
  235. #ifdef CONFIG_PM_SLEEP
  236. static int of_serial_suspend(struct device *dev)
  237. {
  238. struct of_serial_info *info = dev_get_drvdata(dev);
  239. struct uart_8250_port *port8250 = serial8250_get_port(info->line);
  240. struct uart_port *port = &port8250->port;
  241. serial8250_suspend_port(info->line);
  242. if (!uart_console(port) || console_suspend_enabled) {
  243. pm_runtime_put_sync(dev);
  244. clk_disable_unprepare(info->clk);
  245. }
  246. return 0;
  247. }
  248. static int of_serial_resume(struct device *dev)
  249. {
  250. struct of_serial_info *info = dev_get_drvdata(dev);
  251. struct uart_8250_port *port8250 = serial8250_get_port(info->line);
  252. struct uart_port *port = &port8250->port;
  253. if (!uart_console(port) || console_suspend_enabled) {
  254. pm_runtime_get_sync(dev);
  255. clk_prepare_enable(info->clk);
  256. }
  257. serial8250_resume_port(info->line);
  258. return 0;
  259. }
  260. #endif
  261. static SIMPLE_DEV_PM_OPS(of_serial_pm_ops, of_serial_suspend, of_serial_resume);
  262. /*
  263. * A few common types, add more as needed.
  264. */
  265. static const struct of_device_id of_platform_serial_table[] = {
  266. { .compatible = "ns8250", .data = (void *)PORT_8250, },
  267. { .compatible = "ns16450", .data = (void *)PORT_16450, },
  268. { .compatible = "ns16550a", .data = (void *)PORT_16550A, },
  269. { .compatible = "ns16550", .data = (void *)PORT_16550, },
  270. { .compatible = "ns16750", .data = (void *)PORT_16750, },
  271. { .compatible = "ns16850", .data = (void *)PORT_16850, },
  272. { .compatible = "nxp,lpc3220-uart", .data = (void *)PORT_LPC3220, },
  273. { .compatible = "ralink,rt2880-uart", .data = (void *)PORT_RT2880, },
  274. { .compatible = "intel,xscale-uart", .data = (void *)PORT_XSCALE, },
  275. { .compatible = "altr,16550-FIFO32",
  276. .data = (void *)PORT_ALTR_16550_F32, },
  277. { .compatible = "altr,16550-FIFO64",
  278. .data = (void *)PORT_ALTR_16550_F64, },
  279. { .compatible = "altr,16550-FIFO128",
  280. .data = (void *)PORT_ALTR_16550_F128, },
  281. { .compatible = "fsl,16550-FIFO64",
  282. .data = (void *)PORT_16550A_FSL64, },
  283. { .compatible = "mediatek,mtk-btif",
  284. .data = (void *)PORT_MTK_BTIF, },
  285. { .compatible = "mrvl,mmp-uart",
  286. .data = (void *)PORT_XSCALE, },
  287. { .compatible = "ti,da830-uart", .data = (void *)PORT_DA830, },
  288. { .compatible = "nuvoton,wpcm450-uart", .data = (void *)PORT_NPCM, },
  289. { .compatible = "nuvoton,npcm750-uart", .data = (void *)PORT_NPCM, },
  290. { /* end of list */ },
  291. };
  292. MODULE_DEVICE_TABLE(of, of_platform_serial_table);
  293. static struct platform_driver of_platform_serial_driver = {
  294. .driver = {
  295. .name = "of_serial",
  296. .of_match_table = of_platform_serial_table,
  297. .pm = &of_serial_pm_ops,
  298. },
  299. .probe = of_platform_serial_probe,
  300. .remove_new = of_platform_serial_remove,
  301. };
  302. module_platform_driver(of_platform_serial_driver);
  303. MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>");
  304. MODULE_LICENSE("GPL");
  305. MODULE_DESCRIPTION("Serial Port driver for Open Firmware platform devices");