of_serial.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * Serial Port driver for Open Firmware platform devices
  3. *
  4. * Copyright (C) 2006 Arnd Bergmann <arnd@arndb.de>, 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/module.h>
  13. #include <linux/slab.h>
  14. #include <linux/delay.h>
  15. #include <linux/serial_core.h>
  16. #include <linux/serial_reg.h>
  17. #include <linux/of_address.h>
  18. #include <linux/of_irq.h>
  19. #include <linux/of_platform.h>
  20. #include <linux/nwpserial.h>
  21. #include <linux/clk.h>
  22. #include "8250/8250.h"
  23. struct of_serial_info {
  24. struct clk *clk;
  25. int type;
  26. int line;
  27. };
  28. #ifdef CONFIG_ARCH_TEGRA
  29. 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 = 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. clk_prepare_enable(info->clk);
  69. clk = clk_get_rate(info->clk);
  70. }
  71. /* If current-speed was set, then try not to change it. */
  72. if (of_property_read_u32(np, "current-speed", &spd) == 0)
  73. port->custom_divisor = clk / (16 * spd);
  74. ret = of_address_to_resource(np, 0, &resource);
  75. if (ret) {
  76. dev_warn(&ofdev->dev, "invalid address\n");
  77. goto out;
  78. }
  79. spin_lock_init(&port->lock);
  80. port->mapbase = resource.start;
  81. /* Check for shifted address mapping */
  82. if (of_property_read_u32(np, "reg-offset", &prop) == 0)
  83. port->mapbase += prop;
  84. /* Check for registers offset within the devices address range */
  85. if (of_property_read_u32(np, "reg-shift", &prop) == 0)
  86. port->regshift = prop;
  87. /* Check for fifo size */
  88. if (of_property_read_u32(np, "fifo-size", &prop) == 0)
  89. port->fifosize = prop;
  90. port->irq = irq_of_parse_and_map(np, 0);
  91. port->iotype = UPIO_MEM;
  92. if (of_property_read_u32(np, "reg-io-width", &prop) == 0) {
  93. switch (prop) {
  94. case 1:
  95. port->iotype = UPIO_MEM;
  96. break;
  97. case 4:
  98. port->iotype = UPIO_MEM32;
  99. break;
  100. default:
  101. dev_warn(&ofdev->dev, "unsupported reg-io-width (%d)\n",
  102. prop);
  103. ret = -EINVAL;
  104. goto out;
  105. }
  106. }
  107. port->type = type;
  108. port->uartclk = clk;
  109. port->flags = UPF_SHARE_IRQ | UPF_BOOT_AUTOCONF | UPF_IOREMAP
  110. | UPF_FIXED_PORT | UPF_FIXED_TYPE;
  111. if (of_find_property(np, "no-loopback-test", NULL))
  112. port->flags |= UPF_SKIP_TEST;
  113. port->dev = &ofdev->dev;
  114. if (type == PORT_TEGRA)
  115. port->handle_break = tegra_serial_handle_break;
  116. return 0;
  117. out:
  118. if (info->clk)
  119. clk_disable_unprepare(info->clk);
  120. return ret;
  121. }
  122. /*
  123. * Try to register a serial port
  124. */
  125. static struct of_device_id of_platform_serial_table[];
  126. static int of_platform_serial_probe(struct platform_device *ofdev)
  127. {
  128. const struct of_device_id *match;
  129. struct of_serial_info *info;
  130. struct uart_port port;
  131. int port_type;
  132. int ret;
  133. match = of_match_device(of_platform_serial_table, &ofdev->dev);
  134. if (!match)
  135. return -EINVAL;
  136. if (of_find_property(ofdev->dev.of_node, "used-by-rtas", NULL))
  137. return -EBUSY;
  138. info = kmalloc(sizeof(*info), GFP_KERNEL);
  139. if (info == NULL)
  140. return -ENOMEM;
  141. port_type = (unsigned long)match->data;
  142. ret = of_platform_serial_setup(ofdev, port_type, &port, info);
  143. if (ret)
  144. goto out;
  145. switch (port_type) {
  146. #ifdef CONFIG_SERIAL_8250
  147. case PORT_8250 ... PORT_MAX_8250:
  148. {
  149. struct uart_8250_port port8250;
  150. memset(&port8250, 0, sizeof(port8250));
  151. port.type = port_type;
  152. port8250.port = port;
  153. if (port.fifosize)
  154. port8250.capabilities = UART_CAP_FIFO;
  155. if (of_property_read_bool(ofdev->dev.of_node,
  156. "auto-flow-control"))
  157. port8250.capabilities |= UART_CAP_AFE;
  158. if (of_property_read_bool(ofdev->dev.of_node,
  159. "has-hw-flow-control"))
  160. port8250.port.flags |= UPF_HARD_FLOW;
  161. ret = serial8250_register_8250_port(&port8250);
  162. break;
  163. }
  164. #endif
  165. #ifdef CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL
  166. case PORT_NWPSERIAL:
  167. ret = nwpserial_register_port(&port);
  168. break;
  169. #endif
  170. default:
  171. /* need to add code for these */
  172. case PORT_UNKNOWN:
  173. dev_info(&ofdev->dev, "Unknown serial port found, ignored\n");
  174. ret = -ENODEV;
  175. break;
  176. }
  177. if (ret < 0)
  178. goto out;
  179. info->type = port_type;
  180. info->line = ret;
  181. platform_set_drvdata(ofdev, info);
  182. return 0;
  183. out:
  184. kfree(info);
  185. irq_dispose_mapping(port.irq);
  186. return ret;
  187. }
  188. /*
  189. * Release a line
  190. */
  191. static int of_platform_serial_remove(struct platform_device *ofdev)
  192. {
  193. struct of_serial_info *info = platform_get_drvdata(ofdev);
  194. switch (info->type) {
  195. #ifdef CONFIG_SERIAL_8250
  196. case PORT_8250 ... PORT_MAX_8250:
  197. serial8250_unregister_port(info->line);
  198. break;
  199. #endif
  200. #ifdef CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL
  201. case PORT_NWPSERIAL:
  202. nwpserial_unregister_port(info->line);
  203. break;
  204. #endif
  205. default:
  206. /* need to add code for these */
  207. break;
  208. }
  209. if (info->clk)
  210. clk_disable_unprepare(info->clk);
  211. kfree(info);
  212. return 0;
  213. }
  214. /*
  215. * A few common types, add more as needed.
  216. */
  217. static struct of_device_id of_platform_serial_table[] = {
  218. { .compatible = "ns8250", .data = (void *)PORT_8250, },
  219. { .compatible = "ns16450", .data = (void *)PORT_16450, },
  220. { .compatible = "ns16550a", .data = (void *)PORT_16550A, },
  221. { .compatible = "ns16550", .data = (void *)PORT_16550, },
  222. { .compatible = "ns16750", .data = (void *)PORT_16750, },
  223. { .compatible = "ns16850", .data = (void *)PORT_16850, },
  224. { .compatible = "nvidia,tegra20-uart", .data = (void *)PORT_TEGRA, },
  225. { .compatible = "nxp,lpc3220-uart", .data = (void *)PORT_LPC3220, },
  226. { .compatible = "altr,16550-FIFO32",
  227. .data = (void *)PORT_ALTR_16550_F32, },
  228. { .compatible = "altr,16550-FIFO64",
  229. .data = (void *)PORT_ALTR_16550_F64, },
  230. { .compatible = "altr,16550-FIFO128",
  231. .data = (void *)PORT_ALTR_16550_F128, },
  232. #ifdef CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL
  233. { .compatible = "ibm,qpace-nwp-serial",
  234. .data = (void *)PORT_NWPSERIAL, },
  235. #endif
  236. { .type = "serial", .data = (void *)PORT_UNKNOWN, },
  237. { /* end of list */ },
  238. };
  239. static struct platform_driver of_platform_serial_driver = {
  240. .driver = {
  241. .name = "of_serial",
  242. .owner = THIS_MODULE,
  243. .of_match_table = of_platform_serial_table,
  244. },
  245. .probe = of_platform_serial_probe,
  246. .remove = of_platform_serial_remove,
  247. };
  248. module_platform_driver(of_platform_serial_driver);
  249. MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>");
  250. MODULE_LICENSE("GPL");
  251. MODULE_DESCRIPTION("Serial Port driver for Open Firmware platform devices");