of_serial.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. /* Compatibility with the deprecated pxa driver and 8250_pxa drivers. */
  54. if (of_device_is_compatible(np, "mrvl,mmp-uart"))
  55. port->regshift = 2;
  56. /* Check for registers offset within the devices address range */
  57. if (of_property_read_u32(np, "reg-shift", &prop) == 0)
  58. port->regshift = prop;
  59. port->irq = irq_of_parse_and_map(np, 0);
  60. port->iotype = UPIO_MEM;
  61. if (of_property_read_u32(np, "reg-io-width", &prop) == 0) {
  62. switch (prop) {
  63. case 1:
  64. port->iotype = UPIO_MEM;
  65. break;
  66. case 4:
  67. port->iotype = UPIO_MEM32;
  68. break;
  69. default:
  70. dev_warn(&ofdev->dev, "unsupported reg-io-width (%d)\n",
  71. prop);
  72. return -EINVAL;
  73. }
  74. }
  75. port->type = type;
  76. port->uartclk = clk;
  77. port->flags = UPF_SHARE_IRQ | UPF_BOOT_AUTOCONF | UPF_IOREMAP
  78. | UPF_FIXED_PORT | UPF_FIXED_TYPE;
  79. port->dev = &ofdev->dev;
  80. return 0;
  81. }
  82. /*
  83. * Try to register a serial port
  84. */
  85. static struct of_device_id of_platform_serial_table[];
  86. static int __devinit of_platform_serial_probe(struct platform_device *ofdev)
  87. {
  88. const struct of_device_id *match;
  89. struct of_serial_info *info;
  90. struct uart_port port;
  91. int port_type;
  92. int ret;
  93. match = of_match_device(of_platform_serial_table, &ofdev->dev);
  94. if (!match)
  95. return -EINVAL;
  96. if (of_find_property(ofdev->dev.of_node, "used-by-rtas", NULL))
  97. return -EBUSY;
  98. info = kmalloc(sizeof(*info), GFP_KERNEL);
  99. if (info == NULL)
  100. return -ENOMEM;
  101. port_type = (unsigned long)match->data;
  102. ret = of_platform_serial_setup(ofdev, port_type, &port);
  103. if (ret)
  104. goto out;
  105. switch (port_type) {
  106. #ifdef CONFIG_SERIAL_8250
  107. case PORT_8250 ... PORT_MAX_8250:
  108. ret = serial8250_register_port(&port);
  109. break;
  110. #endif
  111. #ifdef CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL
  112. case PORT_NWPSERIAL:
  113. ret = nwpserial_register_port(&port);
  114. break;
  115. #endif
  116. default:
  117. /* need to add code for these */
  118. case PORT_UNKNOWN:
  119. dev_info(&ofdev->dev, "Unknown serial port found, ignored\n");
  120. ret = -ENODEV;
  121. break;
  122. }
  123. if (ret < 0)
  124. goto out;
  125. info->type = port_type;
  126. info->line = ret;
  127. dev_set_drvdata(&ofdev->dev, info);
  128. return 0;
  129. out:
  130. kfree(info);
  131. irq_dispose_mapping(port.irq);
  132. return ret;
  133. }
  134. /*
  135. * Release a line
  136. */
  137. static int of_platform_serial_remove(struct platform_device *ofdev)
  138. {
  139. struct of_serial_info *info = dev_get_drvdata(&ofdev->dev);
  140. switch (info->type) {
  141. #ifdef CONFIG_SERIAL_8250
  142. case PORT_8250 ... PORT_MAX_8250:
  143. serial8250_unregister_port(info->line);
  144. break;
  145. #endif
  146. #ifdef CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL
  147. case PORT_NWPSERIAL:
  148. nwpserial_unregister_port(info->line);
  149. break;
  150. #endif
  151. default:
  152. /* need to add code for these */
  153. break;
  154. }
  155. kfree(info);
  156. return 0;
  157. }
  158. /*
  159. * A few common types, add more as needed.
  160. */
  161. static struct of_device_id __devinitdata of_platform_serial_table[] = {
  162. { .compatible = "ns8250", .data = (void *)PORT_8250, },
  163. { .compatible = "ns16450", .data = (void *)PORT_16450, },
  164. { .compatible = "ns16550a", .data = (void *)PORT_16550A, },
  165. { .compatible = "ns16550", .data = (void *)PORT_16550, },
  166. { .compatible = "ns16750", .data = (void *)PORT_16750, },
  167. { .compatible = "ns16850", .data = (void *)PORT_16850, },
  168. { .compatible = "nvidia,tegra20-uart", .data = (void *)PORT_TEGRA, },
  169. { .compatible = "altr,16550-FIFO32",
  170. .data = (void *)PORT_ALTR_16550_F32, },
  171. { .compatible = "altr,16550-FIFO64",
  172. .data = (void *)PORT_ALTR_16550_F64, },
  173. { .compatible = "altr,16550-FIFO128",
  174. .data = (void *)PORT_ALTR_16550_F128, },
  175. #ifdef CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL
  176. { .compatible = "ibm,qpace-nwp-serial",
  177. .data = (void *)PORT_NWPSERIAL, },
  178. #endif
  179. { /* end of list */ },
  180. };
  181. static struct platform_driver of_platform_serial_driver = {
  182. .driver = {
  183. .name = "of_serial",
  184. .owner = THIS_MODULE,
  185. .of_match_table = of_platform_serial_table,
  186. },
  187. .probe = of_platform_serial_probe,
  188. .remove = of_platform_serial_remove,
  189. };
  190. module_platform_driver(of_platform_serial_driver);
  191. MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>");
  192. MODULE_LICENSE("GPL");
  193. MODULE_DESCRIPTION("Serial Port driver for Open Firmware platform devices");