blob: 8aacfb78deabe03e08ca8d8b4df0e6235eff35ee [file] [log] [blame]
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +01001/*
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
Ishizaki Koue34b9c92007-05-31 19:33:04 +100020struct of_serial_info {
21 int type;
22 int line;
23};
24
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +010025/*
26 * Fill a struct uart_port for a given device node
27 */
28static int __devinit of_platform_serial_setup(struct of_device *ofdev,
29 int type, struct uart_port *port)
30{
31 struct resource resource;
32 struct device_node *np = ofdev->node;
33 const unsigned int *clk, *spd;
34 int ret;
35
36 memset(port, 0, sizeof *port);
Stephen Rothwell40cd3a42007-05-01 13:54:02 +100037 spd = of_get_property(np, "current-speed", NULL);
38 clk = of_get_property(np, "clock-frequency", NULL);
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +010039 if (!clk) {
40 dev_warn(&ofdev->dev, "no clock-frequency property set\n");
41 return -ENODEV;
42 }
43
44 ret = of_address_to_resource(np, 0, &resource);
45 if (ret) {
46 dev_warn(&ofdev->dev, "invalid address\n");
47 return ret;
48 }
49
50 spin_lock_init(&port->lock);
51 port->mapbase = resource.start;
52 port->irq = irq_of_parse_and_map(np, 0);
53 port->iotype = UPIO_MEM;
54 port->type = type;
55 port->uartclk = *clk;
David Gibsonabb4a232007-05-06 14:48:49 -070056 port->flags = UPF_SHARE_IRQ | UPF_BOOT_AUTOCONF | UPF_IOREMAP
57 | UPF_FIXED_PORT;
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +010058 port->dev = &ofdev->dev;
Stephen Neuendorffer19a74262008-04-03 03:52:13 +110059 /* If current-speed was set, then try not to change it. */
60 if (spd)
61 port->custom_divisor = *clk / (16 * (*spd));
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +010062
63 return 0;
64}
65
66/*
67 * Try to register a serial port
68 */
69static int __devinit of_platform_serial_probe(struct of_device *ofdev,
70 const struct of_device_id *id)
71{
Ishizaki Koue34b9c92007-05-31 19:33:04 +100072 struct of_serial_info *info;
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +010073 struct uart_port port;
74 int port_type;
75 int ret;
76
77 if (of_find_property(ofdev->node, "used-by-rtas", NULL))
78 return -EBUSY;
79
Ishizaki Koue34b9c92007-05-31 19:33:04 +100080 info = kmalloc(sizeof(*info), GFP_KERNEL);
81 if (info == NULL)
82 return -ENOMEM;
83
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +010084 port_type = (unsigned long)id->data;
85 ret = of_platform_serial_setup(ofdev, port_type, &port);
86 if (ret)
87 goto out;
88
89 switch (port_type) {
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +010090 case PORT_8250 ... PORT_MAX_8250:
91 ret = serial8250_register_port(&port);
92 break;
93 default:
94 /* need to add code for these */
Ishizaki Kou1558f9b2007-05-31 19:30:33 +100095 case PORT_UNKNOWN:
96 dev_info(&ofdev->dev, "Unknown serial port found, ignored\n");
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +010097 ret = -ENODEV;
98 break;
99 }
100 if (ret < 0)
101 goto out;
102
Ishizaki Koue34b9c92007-05-31 19:33:04 +1000103 info->type = port_type;
104 info->line = ret;
105 ofdev->dev.driver_data = info;
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100106 return 0;
107out:
Ishizaki Koue34b9c92007-05-31 19:33:04 +1000108 kfree(info);
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100109 irq_dispose_mapping(port.irq);
110 return ret;
111}
112
113/*
114 * Release a line
115 */
116static int of_platform_serial_remove(struct of_device *ofdev)
117{
Ishizaki Koue34b9c92007-05-31 19:33:04 +1000118 struct of_serial_info *info = ofdev->dev.driver_data;
119 switch (info->type) {
120 case PORT_8250 ... PORT_MAX_8250:
121 serial8250_unregister_port(info->line);
122 break;
123 default:
124 /* need to add code for these */
125 break;
126 }
127 kfree(info);
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100128 return 0;
129}
130
131/*
132 * A few common types, add more as needed.
133 */
134static struct of_device_id __devinitdata of_platform_serial_table[] = {
135 { .type = "serial", .compatible = "ns8250", .data = (void *)PORT_8250, },
136 { .type = "serial", .compatible = "ns16450", .data = (void *)PORT_16450, },
137 { .type = "serial", .compatible = "ns16550", .data = (void *)PORT_16550, },
138 { .type = "serial", .compatible = "ns16750", .data = (void *)PORT_16750, },
139 { .type = "serial", .data = (void *)PORT_UNKNOWN, },
140 { /* end of list */ },
141};
142
Josh Boyere84290d2008-03-10 11:43:35 -0700143static struct of_platform_driver of_platform_serial_driver = {
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100144 .owner = THIS_MODULE,
145 .name = "of_serial",
146 .probe = of_platform_serial_probe,
147 .remove = of_platform_serial_remove,
148 .match_table = of_platform_serial_table,
149};
150
151static int __init of_platform_serial_init(void)
152{
153 return of_register_platform_driver(&of_platform_serial_driver);
154}
155module_init(of_platform_serial_init);
156
157static void __exit of_platform_serial_exit(void)
158{
159 return of_unregister_platform_driver(&of_platform_serial_driver);
160};
161module_exit(of_platform_serial_exit);
162
163MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>");
164MODULE_LICENSE("GPL");
165MODULE_DESCRIPTION("Serial Port driver for Open Firmware platform devices");