of_serial.c 5.1 KB

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