of_serial.c 5.0 KB

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