of_serial.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 int __devinit of_platform_serial_probe(struct platform_device *ofdev,
  74. const struct of_device_id *id)
  75. {
  76. struct of_serial_info *info;
  77. struct uart_port port;
  78. int port_type;
  79. int ret;
  80. if (of_find_property(ofdev->dev.of_node, "used-by-rtas", NULL))
  81. return -EBUSY;
  82. info = kmalloc(sizeof(*info), GFP_KERNEL);
  83. if (info == NULL)
  84. return -ENOMEM;
  85. port_type = (unsigned long)id->data;
  86. ret = of_platform_serial_setup(ofdev, port_type, &port);
  87. if (ret)
  88. goto out;
  89. switch (port_type) {
  90. #ifdef CONFIG_SERIAL_8250
  91. case PORT_8250 ... PORT_MAX_8250:
  92. ret = serial8250_register_port(&port);
  93. break;
  94. #endif
  95. #ifdef CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL
  96. case PORT_NWPSERIAL:
  97. ret = nwpserial_register_port(&port);
  98. break;
  99. #endif
  100. default:
  101. /* need to add code for these */
  102. case PORT_UNKNOWN:
  103. dev_info(&ofdev->dev, "Unknown serial port found, ignored\n");
  104. ret = -ENODEV;
  105. break;
  106. }
  107. if (ret < 0)
  108. goto out;
  109. info->type = port_type;
  110. info->line = ret;
  111. dev_set_drvdata(&ofdev->dev, info);
  112. return 0;
  113. out:
  114. kfree(info);
  115. irq_dispose_mapping(port.irq);
  116. return ret;
  117. }
  118. /*
  119. * Release a line
  120. */
  121. static int of_platform_serial_remove(struct platform_device *ofdev)
  122. {
  123. struct of_serial_info *info = dev_get_drvdata(&ofdev->dev);
  124. switch (info->type) {
  125. #ifdef CONFIG_SERIAL_8250
  126. case PORT_8250 ... PORT_MAX_8250:
  127. serial8250_unregister_port(info->line);
  128. break;
  129. #endif
  130. #ifdef CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL
  131. case PORT_NWPSERIAL:
  132. nwpserial_unregister_port(info->line);
  133. break;
  134. #endif
  135. default:
  136. /* need to add code for these */
  137. break;
  138. }
  139. kfree(info);
  140. return 0;
  141. }
  142. /*
  143. * A few common types, add more as needed.
  144. */
  145. static struct of_device_id __devinitdata of_platform_serial_table[] = {
  146. { .type = "serial", .compatible = "ns8250", .data = (void *)PORT_8250, },
  147. { .type = "serial", .compatible = "ns16450", .data = (void *)PORT_16450, },
  148. { .type = "serial", .compatible = "ns16550a", .data = (void *)PORT_16550A, },
  149. { .type = "serial", .compatible = "ns16550", .data = (void *)PORT_16550, },
  150. { .type = "serial", .compatible = "ns16750", .data = (void *)PORT_16750, },
  151. { .type = "serial", .compatible = "ns16850", .data = (void *)PORT_16850, },
  152. #ifdef CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL
  153. { .type = "serial", .compatible = "ibm,qpace-nwp-serial",
  154. .data = (void *)PORT_NWPSERIAL, },
  155. #endif
  156. { .type = "serial", .data = (void *)PORT_UNKNOWN, },
  157. { /* end of list */ },
  158. };
  159. static struct of_platform_driver of_platform_serial_driver = {
  160. .driver = {
  161. .name = "of_serial",
  162. .owner = THIS_MODULE,
  163. .of_match_table = of_platform_serial_table,
  164. },
  165. .probe = of_platform_serial_probe,
  166. .remove = of_platform_serial_remove,
  167. };
  168. static int __init of_platform_serial_init(void)
  169. {
  170. return of_register_platform_driver(&of_platform_serial_driver);
  171. }
  172. module_init(of_platform_serial_init);
  173. static void __exit of_platform_serial_exit(void)
  174. {
  175. return of_unregister_platform_driver(&of_platform_serial_driver);
  176. };
  177. module_exit(of_platform_serial_exit);
  178. MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>");
  179. MODULE_LICENSE("GPL");
  180. MODULE_DESCRIPTION("Serial Port driver for Open Firmware platform devices");