of_serial.c 5.3 KB

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