of_serial.c 5.2 KB

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