of_serial.c 4.0 KB

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