]> err.no Git - linux-2.6/blob - drivers/serial/of_serial.c
d7752af1c7ecb2e083a5cb0a4707ab63255b2fa3
[linux-2.6] / drivers / serial / of_serial.c
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
17 #include <asm/of_platform.h>
18 #include <asm/prom.h>
19
20 /*
21  * Fill a struct uart_port for a given device node
22  */
23 static int __devinit of_platform_serial_setup(struct of_device *ofdev,
24                                         int type, struct uart_port *port)
25 {
26         struct resource resource;
27         struct device_node *np = ofdev->node;
28         const unsigned int *clk, *spd;
29         int ret;
30
31         memset(port, 0, sizeof *port);
32         spd = of_get_property(np, "current-speed", NULL);
33         clk = of_get_property(np, "clock-frequency", NULL);
34         if (!clk) {
35                 dev_warn(&ofdev->dev, "no clock-frequency property set\n");
36                 return -ENODEV;
37         }
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
45         spin_lock_init(&port->lock);
46         port->mapbase = resource.start;
47         port->irq = irq_of_parse_and_map(np, 0);
48         port->iotype = UPIO_MEM;
49         port->type = type;
50         port->uartclk = *clk;
51         port->flags = UPF_SHARE_IRQ | UPF_BOOT_AUTOCONF | UPF_IOREMAP
52                 | UPF_FIXED_PORT;
53         port->dev = &ofdev->dev;
54         port->custom_divisor = *clk / (16 * (*spd));
55
56         return 0;
57 }
58
59 /*
60  * Try to register a serial port
61  */
62 static int __devinit of_platform_serial_probe(struct of_device *ofdev,
63                                                 const struct of_device_id *id)
64 {
65         struct uart_port port;
66         int port_type;
67         int ret;
68
69         if (of_find_property(ofdev->node, "used-by-rtas", NULL))
70                 return -EBUSY;
71
72         port_type = (unsigned long)id->data;
73         ret = of_platform_serial_setup(ofdev, port_type, &port);
74         if (ret)
75                 goto out;
76
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
91         ofdev->dev.driver_data = (void *)(unsigned long)ret;
92         return 0;
93 out:
94         irq_dispose_mapping(port.irq);
95         return ret;
96 }
97
98 /*
99  * Release a line
100  */
101 static int of_platform_serial_remove(struct of_device *ofdev)
102 {
103         int line = (unsigned long)ofdev->dev.driver_data;
104         serial8250_unregister_port(line);
105         return 0;
106 }
107
108 /*
109  * A few common types, add more as needed.
110  */
111 static struct of_device_id __devinitdata of_platform_serial_table[] = {
112         { .type = "serial", .compatible = "ns8250",   .data = (void *)PORT_8250, },
113         { .type = "serial", .compatible = "ns16450",  .data = (void *)PORT_16450, },
114         { .type = "serial", .compatible = "ns16550",  .data = (void *)PORT_16550, },
115         { .type = "serial", .compatible = "ns16750",  .data = (void *)PORT_16750, },
116         { .type = "serial",                           .data = (void *)PORT_UNKNOWN, },
117         { /* end of list */ },
118 };
119
120 static struct of_platform_driver __devinitdata of_platform_serial_driver = {
121         .owner = THIS_MODULE,
122         .name = "of_serial",
123         .probe = of_platform_serial_probe,
124         .remove = of_platform_serial_remove,
125         .match_table = of_platform_serial_table,
126 };
127
128 static int __init of_platform_serial_init(void)
129 {
130         return of_register_platform_driver(&of_platform_serial_driver);
131 }
132 module_init(of_platform_serial_init);
133
134 static void __exit of_platform_serial_exit(void)
135 {
136         return of_unregister_platform_driver(&of_platform_serial_driver);
137 };
138 module_exit(of_platform_serial_exit);
139
140 MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>");
141 MODULE_LICENSE("GPL");
142 MODULE_DESCRIPTION("Serial Port driver for Open Firmware platform devices");