of_serial.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 = get_property(np, "current-speed", NULL);
  30. clk = 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. port->dev = &ofdev->dev;
  48. port->custom_divisor = *clk / (16 * (*spd));
  49. return 0;
  50. }
  51. /*
  52. * Try to register a serial port
  53. */
  54. static int __devinit of_platform_serial_probe(struct of_device *ofdev,
  55. const struct of_device_id *id)
  56. {
  57. struct uart_port port;
  58. int port_type;
  59. int ret;
  60. if (of_find_property(ofdev->node, "used-by-rtas", NULL))
  61. return -EBUSY;
  62. port_type = (unsigned long)id->data;
  63. ret = of_platform_serial_setup(ofdev, port_type, &port);
  64. if (ret)
  65. goto out;
  66. switch (port_type) {
  67. case PORT_UNKNOWN:
  68. dev_info(&ofdev->dev, "Unknown serial port found, "
  69. "attempting to use 8250 driver\n");
  70. /* fallthrough */
  71. case PORT_8250 ... PORT_MAX_8250:
  72. ret = serial8250_register_port(&port);
  73. break;
  74. default:
  75. /* need to add code for these */
  76. ret = -ENODEV;
  77. break;
  78. }
  79. if (ret < 0)
  80. goto out;
  81. ofdev->dev.driver_data = (void *)(unsigned long)ret;
  82. return 0;
  83. out:
  84. irq_dispose_mapping(port.irq);
  85. return ret;
  86. }
  87. /*
  88. * Release a line
  89. */
  90. static int of_platform_serial_remove(struct of_device *ofdev)
  91. {
  92. int line = (unsigned long)ofdev->dev.driver_data;
  93. serial8250_unregister_port(line);
  94. return 0;
  95. }
  96. /*
  97. * A few common types, add more as needed.
  98. */
  99. static struct of_device_id __devinitdata of_platform_serial_table[] = {
  100. { .type = "serial", .compatible = "ns8250", .data = (void *)PORT_8250, },
  101. { .type = "serial", .compatible = "ns16450", .data = (void *)PORT_16450, },
  102. { .type = "serial", .compatible = "ns16550", .data = (void *)PORT_16550, },
  103. { .type = "serial", .compatible = "ns16750", .data = (void *)PORT_16750, },
  104. { .type = "serial", .data = (void *)PORT_UNKNOWN, },
  105. { /* end of list */ },
  106. };
  107. static struct of_platform_driver __devinitdata of_platform_serial_driver = {
  108. .owner = THIS_MODULE,
  109. .name = "of_serial",
  110. .probe = of_platform_serial_probe,
  111. .remove = of_platform_serial_remove,
  112. .match_table = of_platform_serial_table,
  113. };
  114. static int __init of_platform_serial_init(void)
  115. {
  116. return of_register_platform_driver(&of_platform_serial_driver);
  117. }
  118. module_init(of_platform_serial_init);
  119. static void __exit of_platform_serial_exit(void)
  120. {
  121. return of_unregister_platform_driver(&of_platform_serial_driver);
  122. };
  123. module_exit(of_platform_serial_exit);
  124. MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>");
  125. MODULE_LICENSE("GPL");
  126. MODULE_DESCRIPTION("Serial Port driver for Open Firmware platform devices");