of_serial.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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/init.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_8250.h>
  18. #include <linux/serial_reg.h>
  19. #include <linux/of_address.h>
  20. #include <linux/of_irq.h>
  21. #include <linux/of_serial.h>
  22. #include <linux/of_platform.h>
  23. #include <linux/nwpserial.h>
  24. struct of_serial_info {
  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. /* FIXME remove this export when tegra finishes conversion to open firmware */
  44. EXPORT_SYMBOL_GPL(tegra_serial_handle_break);
  45. #endif
  46. /*
  47. * Fill a struct uart_port for a given device node
  48. */
  49. static int __devinit of_platform_serial_setup(struct platform_device *ofdev,
  50. int type, struct uart_port *port)
  51. {
  52. struct resource resource;
  53. struct device_node *np = ofdev->dev.of_node;
  54. u32 clk, spd, prop;
  55. int ret;
  56. memset(port, 0, sizeof *port);
  57. if (of_property_read_u32(np, "clock-frequency", &clk)) {
  58. dev_warn(&ofdev->dev, "no clock-frequency property set\n");
  59. return -ENODEV;
  60. }
  61. /* If current-speed was set, then try not to change it. */
  62. if (of_property_read_u32(np, "current-speed", &spd) == 0)
  63. port->custom_divisor = clk / (16 * spd);
  64. ret = of_address_to_resource(np, 0, &resource);
  65. if (ret) {
  66. dev_warn(&ofdev->dev, "invalid address\n");
  67. return ret;
  68. }
  69. spin_lock_init(&port->lock);
  70. port->mapbase = resource.start;
  71. /* Check for shifted address mapping */
  72. if (of_property_read_u32(np, "reg-offset", &prop) == 0)
  73. port->mapbase += prop;
  74. /* Check for registers offset within the devices address range */
  75. if (of_property_read_u32(np, "reg-shift", &prop) == 0)
  76. port->regshift = prop;
  77. port->irq = irq_of_parse_and_map(np, 0);
  78. port->iotype = UPIO_MEM;
  79. if (of_property_read_u32(np, "reg-io-width", &prop) == 0) {
  80. switch (prop) {
  81. case 1:
  82. port->iotype = UPIO_MEM;
  83. break;
  84. case 4:
  85. port->iotype = UPIO_MEM32;
  86. break;
  87. default:
  88. dev_warn(&ofdev->dev, "unsupported reg-io-width (%d)\n",
  89. prop);
  90. return -EINVAL;
  91. }
  92. }
  93. port->type = type;
  94. port->uartclk = clk;
  95. port->flags = UPF_SHARE_IRQ | UPF_BOOT_AUTOCONF | UPF_IOREMAP
  96. | UPF_FIXED_PORT | UPF_FIXED_TYPE;
  97. if (of_find_property(np, "no-loopback-test", NULL))
  98. port->flags |= UPF_SKIP_TEST;
  99. port->dev = &ofdev->dev;
  100. if (type == PORT_TEGRA)
  101. port->handle_break = tegra_serial_handle_break;
  102. return 0;
  103. }
  104. /*
  105. * Try to register a serial port
  106. */
  107. static struct of_device_id of_platform_serial_table[];
  108. static int __devinit of_platform_serial_probe(struct platform_device *ofdev)
  109. {
  110. const struct of_device_id *match;
  111. struct of_serial_info *info;
  112. struct uart_port port;
  113. int port_type;
  114. int ret;
  115. match = of_match_device(of_platform_serial_table, &ofdev->dev);
  116. if (!match)
  117. return -EINVAL;
  118. if (of_find_property(ofdev->dev.of_node, "used-by-rtas", NULL))
  119. return -EBUSY;
  120. info = kmalloc(sizeof(*info), GFP_KERNEL);
  121. if (info == NULL)
  122. return -ENOMEM;
  123. port_type = (unsigned long)match->data;
  124. ret = of_platform_serial_setup(ofdev, port_type, &port);
  125. if (ret)
  126. goto out;
  127. switch (port_type) {
  128. #ifdef CONFIG_SERIAL_8250
  129. case PORT_8250 ... PORT_MAX_8250:
  130. {
  131. /* For now the of bindings don't support the extra
  132. 8250 specific bits */
  133. struct uart_8250_port port8250;
  134. memset(&port8250, 0, sizeof(port8250));
  135. port8250.port = port;
  136. ret = serial8250_register_8250_port(&port8250);
  137. break;
  138. }
  139. #endif
  140. #ifdef CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL
  141. case PORT_NWPSERIAL:
  142. ret = nwpserial_register_port(&port);
  143. break;
  144. #endif
  145. default:
  146. /* need to add code for these */
  147. case PORT_UNKNOWN:
  148. dev_info(&ofdev->dev, "Unknown serial port found, ignored\n");
  149. ret = -ENODEV;
  150. break;
  151. }
  152. if (ret < 0)
  153. goto out;
  154. info->type = port_type;
  155. info->line = ret;
  156. dev_set_drvdata(&ofdev->dev, info);
  157. return 0;
  158. out:
  159. kfree(info);
  160. irq_dispose_mapping(port.irq);
  161. return ret;
  162. }
  163. /*
  164. * Release a line
  165. */
  166. static int of_platform_serial_remove(struct platform_device *ofdev)
  167. {
  168. struct of_serial_info *info = dev_get_drvdata(&ofdev->dev);
  169. switch (info->type) {
  170. #ifdef CONFIG_SERIAL_8250
  171. case PORT_8250 ... PORT_MAX_8250:
  172. serial8250_unregister_port(info->line);
  173. break;
  174. #endif
  175. #ifdef CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL
  176. case PORT_NWPSERIAL:
  177. nwpserial_unregister_port(info->line);
  178. break;
  179. #endif
  180. default:
  181. /* need to add code for these */
  182. break;
  183. }
  184. kfree(info);
  185. return 0;
  186. }
  187. /*
  188. * A few common types, add more as needed.
  189. */
  190. static struct of_device_id __devinitdata of_platform_serial_table[] = {
  191. { .compatible = "ns8250", .data = (void *)PORT_8250, },
  192. { .compatible = "ns16450", .data = (void *)PORT_16450, },
  193. { .compatible = "ns16550a", .data = (void *)PORT_16550A, },
  194. { .compatible = "ns16550", .data = (void *)PORT_16550, },
  195. { .compatible = "ns16750", .data = (void *)PORT_16750, },
  196. { .compatible = "ns16850", .data = (void *)PORT_16850, },
  197. { .compatible = "nvidia,tegra20-uart", .data = (void *)PORT_TEGRA, },
  198. { .compatible = "nxp,lpc3220-uart", .data = (void *)PORT_LPC3220, },
  199. #ifdef CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL
  200. { .compatible = "ibm,qpace-nwp-serial",
  201. .data = (void *)PORT_NWPSERIAL, },
  202. #endif
  203. { .type = "serial", .data = (void *)PORT_UNKNOWN, },
  204. { /* end of list */ },
  205. };
  206. static struct platform_driver of_platform_serial_driver = {
  207. .driver = {
  208. .name = "of_serial",
  209. .owner = THIS_MODULE,
  210. .of_match_table = of_platform_serial_table,
  211. },
  212. .probe = of_platform_serial_probe,
  213. .remove = of_platform_serial_remove,
  214. };
  215. module_platform_driver(of_platform_serial_driver);
  216. MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>");
  217. MODULE_LICENSE("GPL");
  218. MODULE_DESCRIPTION("Serial Port driver for Open Firmware platform devices");