blob: a48d9080f5525f1c97ac0fff59023f85f19d1b67 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +010015#include <linux/serial_core.h>
16#include <linux/serial_8250.h>
Stephen Rothwellc401b042008-05-23 16:32:54 +100017#include <linux/of_platform.h>
Benjamin Krill5886188d2009-01-07 10:32:38 +010018#include <linux/nwpserial.h>
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +010019
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +010020#include <asm/prom.h>
21
Ishizaki Koue34b9c92007-05-31 19:33:04 +100022struct of_serial_info {
23 int type;
24 int line;
25};
26
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +010027/*
28 * Fill a struct uart_port for a given device node
29 */
30static int __devinit of_platform_serial_setup(struct of_device *ofdev,
31 int type, struct uart_port *port)
32{
33 struct resource resource;
Grant Likely61c7a082010-04-13 16:12:29 -070034 struct device_node *np = ofdev->dev.of_node;
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +010035 const unsigned int *clk, *spd;
John Linnb912b5e2008-04-03 10:22:19 +110036 const u32 *prop;
37 int ret, prop_size;
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +010038
39 memset(port, 0, sizeof *port);
Stephen Rothwell40cd3a42007-05-01 13:54:02 +100040 spd = of_get_property(np, "current-speed", NULL);
41 clk = of_get_property(np, "clock-frequency", NULL);
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +010042 if (!clk) {
43 dev_warn(&ofdev->dev, "no clock-frequency property set\n");
44 return -ENODEV;
45 }
46
47 ret = of_address_to_resource(np, 0, &resource);
48 if (ret) {
49 dev_warn(&ofdev->dev, "invalid address\n");
50 return ret;
51 }
52
53 spin_lock_init(&port->lock);
54 port->mapbase = resource.start;
John Linnb912b5e2008-04-03 10:22:19 +110055
56 /* Check for shifted address mapping */
57 prop = of_get_property(np, "reg-offset", &prop_size);
58 if (prop && (prop_size == sizeof(u32)))
59 port->mapbase += *prop;
60
61 /* Check for registers offset within the devices address range */
62 prop = of_get_property(np, "reg-shift", &prop_size);
63 if (prop && (prop_size == sizeof(u32)))
64 port->regshift = *prop;
65
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +010066 port->irq = irq_of_parse_and_map(np, 0);
67 port->iotype = UPIO_MEM;
68 port->type = type;
69 port->uartclk = *clk;
David Gibsonabb4a232007-05-06 14:48:49 -070070 port->flags = UPF_SHARE_IRQ | UPF_BOOT_AUTOCONF | UPF_IOREMAP
Dave Mitchelleedacbf2009-06-09 13:39:47 +000071 | UPF_FIXED_PORT | UPF_FIXED_TYPE;
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +010072 port->dev = &ofdev->dev;
Stephen Neuendorffer19a74262008-04-03 03:52:13 +110073 /* If current-speed was set, then try not to change it. */
74 if (spd)
75 port->custom_divisor = *clk / (16 * (*spd));
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +010076
77 return 0;
78}
79
80/*
81 * Try to register a serial port
82 */
83static int __devinit of_platform_serial_probe(struct of_device *ofdev,
84 const struct of_device_id *id)
85{
Ishizaki Koue34b9c92007-05-31 19:33:04 +100086 struct of_serial_info *info;
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +010087 struct uart_port port;
88 int port_type;
89 int ret;
90
Grant Likely61c7a082010-04-13 16:12:29 -070091 if (of_find_property(ofdev->dev.of_node, "used-by-rtas", NULL))
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +010092 return -EBUSY;
93
Ishizaki Koue34b9c92007-05-31 19:33:04 +100094 info = kmalloc(sizeof(*info), GFP_KERNEL);
95 if (info == NULL)
96 return -ENOMEM;
97
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +010098 port_type = (unsigned long)id->data;
99 ret = of_platform_serial_setup(ofdev, port_type, &port);
100 if (ret)
101 goto out;
102
103 switch (port_type) {
Benjamin Krill5886188d2009-01-07 10:32:38 +0100104#ifdef CONFIG_SERIAL_8250
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100105 case PORT_8250 ... PORT_MAX_8250:
106 ret = serial8250_register_port(&port);
107 break;
Benjamin Krill5886188d2009-01-07 10:32:38 +0100108#endif
109#ifdef CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL
110 case PORT_NWPSERIAL:
111 ret = nwpserial_register_port(&port);
112 break;
113#endif
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100114 default:
115 /* need to add code for these */
Ishizaki Kou1558f9b2007-05-31 19:30:33 +1000116 case PORT_UNKNOWN:
117 dev_info(&ofdev->dev, "Unknown serial port found, ignored\n");
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100118 ret = -ENODEV;
119 break;
120 }
121 if (ret < 0)
122 goto out;
123
Ishizaki Koue34b9c92007-05-31 19:33:04 +1000124 info->type = port_type;
125 info->line = ret;
Greg Kroah-Hartman3cf62a52009-05-04 12:40:54 -0700126 dev_set_drvdata(&ofdev->dev, info);
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100127 return 0;
128out:
Ishizaki Koue34b9c92007-05-31 19:33:04 +1000129 kfree(info);
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100130 irq_dispose_mapping(port.irq);
131 return ret;
132}
133
134/*
135 * Release a line
136 */
137static int of_platform_serial_remove(struct of_device *ofdev)
138{
Greg Kroah-Hartman3cf62a52009-05-04 12:40:54 -0700139 struct of_serial_info *info = dev_get_drvdata(&ofdev->dev);
Ishizaki Koue34b9c92007-05-31 19:33:04 +1000140 switch (info->type) {
Benjamin Krill5886188d2009-01-07 10:32:38 +0100141#ifdef CONFIG_SERIAL_8250
Ishizaki Koue34b9c92007-05-31 19:33:04 +1000142 case PORT_8250 ... PORT_MAX_8250:
143 serial8250_unregister_port(info->line);
144 break;
Benjamin Krill5886188d2009-01-07 10:32:38 +0100145#endif
146#ifdef CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL
147 case PORT_NWPSERIAL:
148 nwpserial_unregister_port(info->line);
149 break;
150#endif
Ishizaki Koue34b9c92007-05-31 19:33:04 +1000151 default:
152 /* need to add code for these */
153 break;
154 }
155 kfree(info);
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100156 return 0;
157}
158
159/*
160 * A few common types, add more as needed.
161 */
162static struct of_device_id __devinitdata of_platform_serial_table[] = {
163 { .type = "serial", .compatible = "ns8250", .data = (void *)PORT_8250, },
164 { .type = "serial", .compatible = "ns16450", .data = (void *)PORT_16450, },
Michal Simek16173c72009-11-24 10:22:41 +0000165 { .type = "serial", .compatible = "ns16550a", .data = (void *)PORT_16550A, },
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100166 { .type = "serial", .compatible = "ns16550", .data = (void *)PORT_16550, },
167 { .type = "serial", .compatible = "ns16750", .data = (void *)PORT_16750, },
Matthias Fuchs0025e752009-01-15 09:43:35 +0000168 { .type = "serial", .compatible = "ns16850", .data = (void *)PORT_16850, },
Benjamin Krill5886188d2009-01-07 10:32:38 +0100169#ifdef CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL
170 { .type = "serial", .compatible = "ibm,qpace-nwp-serial",
171 .data = (void *)PORT_NWPSERIAL, },
172#endif
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100173 { .type = "serial", .data = (void *)PORT_UNKNOWN, },
174 { /* end of list */ },
175};
176
Josh Boyere84290d2008-03-10 11:43:35 -0700177static struct of_platform_driver of_platform_serial_driver = {
Grant Likely40182942010-04-13 16:13:02 -0700178 .driver = {
179 .name = "of_serial",
180 .owner = THIS_MODULE,
181 .of_match_table = of_platform_serial_table,
182 },
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100183 .probe = of_platform_serial_probe,
184 .remove = of_platform_serial_remove,
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100185};
186
187static int __init of_platform_serial_init(void)
188{
189 return of_register_platform_driver(&of_platform_serial_driver);
190}
191module_init(of_platform_serial_init);
192
193static void __exit of_platform_serial_exit(void)
194{
195 return of_unregister_platform_driver(&of_platform_serial_driver);
196};
197module_exit(of_platform_serial_exit);
198
199MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>");
200MODULE_LICENSE("GPL");
201MODULE_DESCRIPTION("Serial Port driver for Open Firmware platform devices");