of_serial.c 4.4 KB

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