of_serial.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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/serial_core.h>
  16. #include <linux/serial_8250.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. struct of_serial_info {
  22. int type;
  23. int line;
  24. };
  25. /*
  26. * Fill a struct uart_port for a given device node
  27. */
  28. static int __devinit of_platform_serial_setup(struct platform_device *ofdev,
  29. int type, struct uart_port *port)
  30. {
  31. struct resource resource;
  32. struct device_node *np = ofdev->dev.of_node;
  33. u32 clk, spd, prop;
  34. int ret;
  35. memset(port, 0, sizeof *port);
  36. if (of_property_read_u32(np, "clock-frequency", &clk)) {
  37. dev_warn(&ofdev->dev, "no clock-frequency property set\n");
  38. return -ENODEV;
  39. }
  40. /* If current-speed was set, then try not to change it. */
  41. if (of_property_read_u32(np, "current-speed", &spd) == 0)
  42. port->custom_divisor = clk / (16 * spd);
  43. ret = of_address_to_resource(np, 0, &resource);
  44. if (ret) {
  45. dev_warn(&ofdev->dev, "invalid address\n");
  46. return ret;
  47. }
  48. spin_lock_init(&port->lock);
  49. port->mapbase = resource.start;
  50. /* Check for shifted address mapping */
  51. if (of_property_read_u32(np, "reg-offset", &prop) == 0)
  52. port->mapbase += prop;
  53. /* Check for registers offset within the devices address range */
  54. if (of_property_read_u32(np, "reg-shift", &prop) == 0)
  55. port->regshift = prop;
  56. port->irq = irq_of_parse_and_map(np, 0);
  57. port->iotype = UPIO_MEM;
  58. if (of_property_read_u32(np, "reg-io-width", &prop) == 0) {
  59. switch (prop) {
  60. case 1:
  61. port->iotype = UPIO_MEM;
  62. break;
  63. case 4:
  64. port->iotype = UPIO_MEM32;
  65. break;
  66. default:
  67. dev_warn(&ofdev->dev, "unsupported reg-io-width (%d)\n",
  68. prop);
  69. return -EINVAL;
  70. }
  71. }
  72. port->type = type;
  73. port->uartclk = clk;
  74. port->flags = UPF_SHARE_IRQ | UPF_BOOT_AUTOCONF | UPF_IOREMAP
  75. | UPF_FIXED_PORT | UPF_FIXED_TYPE;
  76. port->dev = &ofdev->dev;
  77. return 0;
  78. }
  79. /*
  80. * Try to register a serial port
  81. */
  82. static struct of_device_id of_platform_serial_table[];
  83. static int __devinit of_platform_serial_probe(struct platform_device *ofdev)
  84. {
  85. const struct of_device_id *match;
  86. struct of_serial_info *info;
  87. struct uart_port port;
  88. int port_type;
  89. int ret;
  90. match = of_match_device(of_platform_serial_table, &ofdev->dev);
  91. if (!match)
  92. return -EINVAL;
  93. if (of_find_property(ofdev->dev.of_node, "used-by-rtas", NULL))
  94. return -EBUSY;
  95. info = kmalloc(sizeof(*info), GFP_KERNEL);
  96. if (info == NULL)
  97. return -ENOMEM;
  98. port_type = (unsigned long)match->data;
  99. ret = of_platform_serial_setup(ofdev, port_type, &port);
  100. if (ret)
  101. goto out;
  102. switch (port_type) {
  103. #ifdef CONFIG_SERIAL_8250
  104. case PORT_8250 ... PORT_MAX_8250:
  105. ret = serial8250_register_port(&port);
  106. break;
  107. #endif
  108. #ifdef CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL
  109. case PORT_NWPSERIAL:
  110. ret = nwpserial_register_port(&port);
  111. break;
  112. #endif
  113. default:
  114. /* need to add code for these */
  115. case PORT_UNKNOWN:
  116. dev_info(&ofdev->dev, "Unknown serial port found, ignored\n");
  117. ret = -ENODEV;
  118. break;
  119. }
  120. if (ret < 0)
  121. goto out;
  122. info->type = port_type;
  123. info->line = ret;
  124. dev_set_drvdata(&ofdev->dev, info);
  125. return 0;
  126. out:
  127. kfree(info);
  128. irq_dispose_mapping(port.irq);
  129. return ret;
  130. }
  131. /*
  132. * Release a line
  133. */
  134. static int of_platform_serial_remove(struct platform_device *ofdev)
  135. {
  136. struct of_serial_info *info = dev_get_drvdata(&ofdev->dev);
  137. switch (info->type) {
  138. #ifdef CONFIG_SERIAL_8250
  139. case PORT_8250 ... PORT_MAX_8250:
  140. serial8250_unregister_port(info->line);
  141. break;
  142. #endif
  143. #ifdef CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL
  144. case PORT_NWPSERIAL:
  145. nwpserial_unregister_port(info->line);
  146. break;
  147. #endif
  148. default:
  149. /* need to add code for these */
  150. break;
  151. }
  152. kfree(info);
  153. return 0;
  154. }
  155. /*
  156. * A few common types, add more as needed.
  157. */
  158. static struct of_device_id __devinitdata of_platform_serial_table[] = {
  159. { .compatible = "ns8250", .data = (void *)PORT_8250, },
  160. { .compatible = "ns16450", .data = (void *)PORT_16450, },
  161. { .compatible = "ns16550a", .data = (void *)PORT_16550A, },
  162. { .compatible = "ns16550", .data = (void *)PORT_16550, },
  163. { .compatible = "ns16750", .data = (void *)PORT_16750, },
  164. { .compatible = "ns16850", .data = (void *)PORT_16850, },
  165. { .compatible = "nvidia,tegra20-uart", .data = (void *)PORT_TEGRA, },
  166. { .compatible = "altr,16550-FIFO32",
  167. .data = (void *)PORT_ALTR_16550_F32, },
  168. { .compatible = "altr,16550-FIFO64",
  169. .data = (void *)PORT_ALTR_16550_F64, },
  170. { .compatible = "altr,16550-FIFO128",
  171. .data = (void *)PORT_ALTR_16550_F128, },
  172. #ifdef CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL
  173. { .compatible = "ibm,qpace-nwp-serial",
  174. .data = (void *)PORT_NWPSERIAL, },
  175. #endif
  176. { /* end of list */ },
  177. };
  178. static struct platform_driver of_platform_serial_driver = {
  179. .driver = {
  180. .name = "of_serial",
  181. .owner = THIS_MODULE,
  182. .of_match_table = of_platform_serial_table,
  183. },
  184. .probe = of_platform_serial_probe,
  185. .remove = of_platform_serial_remove,
  186. };
  187. module_platform_driver(of_platform_serial_driver);
  188. MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>");
  189. MODULE_LICENSE("GPL");
  190. MODULE_DESCRIPTION("Serial Port driver for Open Firmware platform devices");