of_serial.c 4.1 KB

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