of_serial.c 3.7 KB

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