8250_of.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. * Serial Port driver for Open Firmware platform devices
  3. *
  4. * Copyright (C) 2006 Arnd Bergmann <[email protected]>, IBM Corp.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. *
  11. */
  12. #include <linux/console.h>
  13. #include <linux/module.h>
  14. #include <linux/slab.h>
  15. #include <linux/delay.h>
  16. #include <linux/serial_core.h>
  17. #include <linux/serial_reg.h>
  18. #include <linux/of_address.h>
  19. #include <linux/of_irq.h>
  20. #include <linux/of_platform.h>
  21. #include <linux/clk.h>
  22. #include "8250.h"
  23. struct of_serial_info {
  24. struct clk *clk;
  25. int type;
  26. int line;
  27. };
  28. #ifdef CONFIG_ARCH_TEGRA
  29. static void tegra_serial_handle_break(struct uart_port *p)
  30. {
  31. unsigned int status, tmout = 10000;
  32. do {
  33. status = p->serial_in(p, UART_LSR);
  34. if (status & (UART_LSR_FIFOE | UART_LSR_BRK_ERROR_BITS))
  35. status = p->serial_in(p, UART_RX);
  36. else
  37. break;
  38. if (--tmout == 0)
  39. break;
  40. udelay(1);
  41. } while (1);
  42. }
  43. #else
  44. static inline void tegra_serial_handle_break(struct uart_port *port)
  45. {
  46. }
  47. #endif
  48. /*
  49. * Fill a struct uart_port for a given device node
  50. */
  51. static int of_platform_serial_setup(struct platform_device *ofdev,
  52. int type, struct uart_port *port,
  53. struct of_serial_info *info)
  54. {
  55. struct resource resource;
  56. struct device_node *np = ofdev->dev.of_node;
  57. u32 clk, spd, prop;
  58. int ret;
  59. memset(port, 0, sizeof *port);
  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. return PTR_ERR(info->clk);
  67. }
  68. ret = clk_prepare_enable(info->clk);
  69. if (ret < 0)
  70. return ret;
  71. clk = clk_get_rate(info->clk);
  72. }
  73. /* If current-speed was set, then try not to change it. */
  74. if (of_property_read_u32(np, "current-speed", &spd) == 0)
  75. port->custom_divisor = clk / (16 * spd);
  76. ret = of_address_to_resource(np, 0, &resource);
  77. if (ret) {
  78. dev_warn(&ofdev->dev, "invalid address\n");
  79. goto out;
  80. }
  81. spin_lock_init(&port->lock);
  82. port->mapbase = resource.start;
  83. port->mapsize = resource_size(&resource);
  84. /* Check for shifted address mapping */
  85. if (of_property_read_u32(np, "reg-offset", &prop) == 0)
  86. port->mapbase += prop;
  87. /* Compatibility with the deprecated pxa driver and 8250_pxa drivers. */
  88. if (of_device_is_compatible(np, "mrvl,mmp-uart"))
  89. port->regshift = 2;
  90. /* Check for registers offset within the devices address range */
  91. if (of_property_read_u32(np, "reg-shift", &prop) == 0)
  92. port->regshift = prop;
  93. /* Check for fifo size */
  94. if (of_property_read_u32(np, "fifo-size", &prop) == 0)
  95. port->fifosize = prop;
  96. /* Check for a fixed line number */
  97. ret = of_alias_get_id(np, "serial");
  98. if (ret >= 0)
  99. port->line = ret;
  100. port->irq = irq_of_parse_and_map(np, 0);
  101. port->iotype = UPIO_MEM;
  102. if (of_property_read_u32(np, "reg-io-width", &prop) == 0) {
  103. switch (prop) {
  104. case 1:
  105. port->iotype = UPIO_MEM;
  106. break;
  107. case 2:
  108. port->iotype = UPIO_MEM16;
  109. break;
  110. case 4:
  111. port->iotype = of_device_is_big_endian(np) ?
  112. UPIO_MEM32BE : UPIO_MEM32;
  113. break;
  114. default:
  115. dev_warn(&ofdev->dev, "unsupported reg-io-width (%d)\n",
  116. prop);
  117. ret = -EINVAL;
  118. goto out;
  119. }
  120. }
  121. port->type = type;
  122. port->uartclk = clk;
  123. port->flags = UPF_SHARE_IRQ | UPF_BOOT_AUTOCONF | UPF_IOREMAP
  124. | UPF_FIXED_PORT | UPF_FIXED_TYPE;
  125. if (of_find_property(np, "no-loopback-test", NULL))
  126. port->flags |= UPF_SKIP_TEST;
  127. port->dev = &ofdev->dev;
  128. switch (type) {
  129. case PORT_TEGRA:
  130. port->handle_break = tegra_serial_handle_break;
  131. break;
  132. case PORT_RT2880:
  133. port->iotype = UPIO_AU;
  134. break;
  135. }
  136. if (IS_ENABLED(CONFIG_SERIAL_8250_FSL) &&
  137. (of_device_is_compatible(np, "fsl,ns16550") ||
  138. of_device_is_compatible(np, "fsl,16550-FIFO64")))
  139. port->handle_irq = fsl8250_handle_irq;
  140. return 0;
  141. out:
  142. if (info->clk)
  143. clk_disable_unprepare(info->clk);
  144. return ret;
  145. }
  146. /*
  147. * Try to register a serial port
  148. */
  149. static const struct of_device_id of_platform_serial_table[];
  150. static int of_platform_serial_probe(struct platform_device *ofdev)
  151. {
  152. const struct of_device_id *match;
  153. struct of_serial_info *info;
  154. struct uart_port port;
  155. int port_type;
  156. int ret;
  157. match = of_match_device(of_platform_serial_table, &ofdev->dev);
  158. if (!match)
  159. return -EINVAL;
  160. if (of_find_property(ofdev->dev.of_node, "used-by-rtas", NULL))
  161. return -EBUSY;
  162. info = kzalloc(sizeof(*info), GFP_KERNEL);
  163. if (info == NULL)
  164. return -ENOMEM;
  165. port_type = (unsigned long)match->data;
  166. ret = of_platform_serial_setup(ofdev, port_type, &port, info);
  167. if (ret)
  168. goto out;
  169. switch (port_type) {
  170. case PORT_8250 ... PORT_MAX_8250:
  171. {
  172. u32 tx_threshold;
  173. struct uart_8250_port port8250;
  174. memset(&port8250, 0, sizeof(port8250));
  175. port8250.port = port;
  176. if (port.fifosize)
  177. port8250.capabilities = UART_CAP_FIFO;
  178. /* Check for TX FIFO threshold & set tx_loadsz */
  179. if ((of_property_read_u32(ofdev->dev.of_node, "tx-threshold",
  180. &tx_threshold) == 0) &&
  181. (tx_threshold < port.fifosize))
  182. port8250.tx_loadsz = port.fifosize - tx_threshold;
  183. if (of_property_read_bool(ofdev->dev.of_node,
  184. "auto-flow-control"))
  185. port8250.capabilities |= UART_CAP_AFE;
  186. ret = serial8250_register_8250_port(&port8250);
  187. break;
  188. }
  189. default:
  190. /* need to add code for these */
  191. case PORT_UNKNOWN:
  192. dev_info(&ofdev->dev, "Unknown serial port found, ignored\n");
  193. ret = -ENODEV;
  194. break;
  195. }
  196. if (ret < 0)
  197. goto out;
  198. info->type = port_type;
  199. info->line = ret;
  200. platform_set_drvdata(ofdev, info);
  201. return 0;
  202. out:
  203. kfree(info);
  204. irq_dispose_mapping(port.irq);
  205. return ret;
  206. }
  207. /*
  208. * Release a line
  209. */
  210. static int of_platform_serial_remove(struct platform_device *ofdev)
  211. {
  212. struct of_serial_info *info = platform_get_drvdata(ofdev);
  213. switch (info->type) {
  214. case PORT_8250 ... PORT_MAX_8250:
  215. serial8250_unregister_port(info->line);
  216. break;
  217. default:
  218. /* need to add code for these */
  219. break;
  220. }
  221. if (info->clk)
  222. clk_disable_unprepare(info->clk);
  223. kfree(info);
  224. return 0;
  225. }
  226. #ifdef CONFIG_PM_SLEEP
  227. static void of_serial_suspend_8250(struct of_serial_info *info)
  228. {
  229. struct uart_8250_port *port8250 = serial8250_get_port(info->line);
  230. struct uart_port *port = &port8250->port;
  231. serial8250_suspend_port(info->line);
  232. if (info->clk && (!uart_console(port) || console_suspend_enabled))
  233. clk_disable_unprepare(info->clk);
  234. }
  235. static void of_serial_resume_8250(struct of_serial_info *info)
  236. {
  237. struct uart_8250_port *port8250 = serial8250_get_port(info->line);
  238. struct uart_port *port = &port8250->port;
  239. if (info->clk && (!uart_console(port) || console_suspend_enabled))
  240. clk_prepare_enable(info->clk);
  241. serial8250_resume_port(info->line);
  242. }
  243. static int of_serial_suspend(struct device *dev)
  244. {
  245. struct of_serial_info *info = dev_get_drvdata(dev);
  246. switch (info->type) {
  247. case PORT_8250 ... PORT_MAX_8250:
  248. of_serial_suspend_8250(info);
  249. break;
  250. default:
  251. break;
  252. }
  253. return 0;
  254. }
  255. static int of_serial_resume(struct device *dev)
  256. {
  257. struct of_serial_info *info = dev_get_drvdata(dev);
  258. switch (info->type) {
  259. case PORT_8250 ... PORT_MAX_8250:
  260. of_serial_resume_8250(info);
  261. break;
  262. default:
  263. break;
  264. }
  265. return 0;
  266. }
  267. #endif
  268. static SIMPLE_DEV_PM_OPS(of_serial_pm_ops, of_serial_suspend, of_serial_resume);
  269. /*
  270. * A few common types, add more as needed.
  271. */
  272. static const struct of_device_id of_platform_serial_table[] = {
  273. { .compatible = "ns8250", .data = (void *)PORT_8250, },
  274. { .compatible = "ns16450", .data = (void *)PORT_16450, },
  275. { .compatible = "ns16550a", .data = (void *)PORT_16550A, },
  276. { .compatible = "ns16550", .data = (void *)PORT_16550, },
  277. { .compatible = "ns16750", .data = (void *)PORT_16750, },
  278. { .compatible = "ns16850", .data = (void *)PORT_16850, },
  279. { .compatible = "nvidia,tegra20-uart", .data = (void *)PORT_TEGRA, },
  280. { .compatible = "nxp,lpc3220-uart", .data = (void *)PORT_LPC3220, },
  281. { .compatible = "ralink,rt2880-uart", .data = (void *)PORT_RT2880, },
  282. { .compatible = "altr,16550-FIFO32",
  283. .data = (void *)PORT_ALTR_16550_F32, },
  284. { .compatible = "altr,16550-FIFO64",
  285. .data = (void *)PORT_ALTR_16550_F64, },
  286. { .compatible = "altr,16550-FIFO128",
  287. .data = (void *)PORT_ALTR_16550_F128, },
  288. { .compatible = "mrvl,mmp-uart",
  289. .data = (void *)PORT_XSCALE, },
  290. { .compatible = "mrvl,pxa-uart",
  291. .data = (void *)PORT_XSCALE, },
  292. { /* end of list */ },
  293. };
  294. MODULE_DEVICE_TABLE(of, of_platform_serial_table);
  295. static struct platform_driver of_platform_serial_driver = {
  296. .driver = {
  297. .name = "of_serial",
  298. .of_match_table = of_platform_serial_table,
  299. .pm = &of_serial_pm_ops,
  300. },
  301. .probe = of_platform_serial_probe,
  302. .remove = of_platform_serial_remove,
  303. };
  304. module_platform_driver(of_platform_serial_driver);
  305. MODULE_AUTHOR("Arnd Bergmann <[email protected]>");
  306. MODULE_LICENSE("GPL");
  307. MODULE_DESCRIPTION("Serial Port driver for Open Firmware platform devices");