blob: c911b2419abb8a664f3bfad7d258a94c1363fa5b [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>
Grant Likelyf1ca09b2010-08-16 23:44:49 -060017#include <linux/of_address.h>
Rob Herring73930a82010-11-17 17:50:23 -060018#include <linux/of_irq.h>
Stephen Rothwellc401b042008-05-23 16:32:54 +100019#include <linux/of_platform.h>
Benjamin Krill58861882009-01-07 10:32:38 +010020#include <linux/nwpserial.h>
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +010021
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 */
Grant Likely2dc11582010-08-06 09:25:50 -060030static int __devinit of_platform_serial_setup(struct platform_device *ofdev,
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +010031 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;
Ian Munsief14362d2010-10-01 17:06:07 +100035 const __be32 *clk, *spd;
36 const __be32 *prop;
John Linnb912b5e2008-04-03 10:22:19 +110037 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)))
Ian Munsief14362d2010-10-01 17:06:07 +100059 port->mapbase += be32_to_cpup(prop);
John Linnb912b5e2008-04-03 10:22:19 +110060
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)))
Ian Munsief14362d2010-10-01 17:06:07 +100064 port->regshift = be32_to_cpup(prop);
John Linnb912b5e2008-04-03 10:22:19 +110065
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;
Ian Munsief14362d2010-10-01 17:06:07 +100069 port->uartclk = be32_to_cpup(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)
Ian Munsief14362d2010-10-01 17:06:07 +100075 port->custom_divisor = be32_to_cpup(clk) / (16 * (be32_to_cpup(spd)));
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +010076
77 return 0;
78}
79
80/*
81 * Try to register a serial port
82 */
Grant Likelyb1608d62011-05-18 11:19:24 -060083static struct of_device_id of_platform_serial_table[];
Grant Likely793218d2011-02-22 21:10:26 -070084static int __devinit of_platform_serial_probe(struct platform_device *ofdev)
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +010085{
Grant Likelyb1608d62011-05-18 11:19:24 -060086 const struct of_device_id *match;
Ishizaki Koue34b9c92007-05-31 19:33:04 +100087 struct of_serial_info *info;
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +010088 struct uart_port port;
89 int port_type;
90 int ret;
91
Grant Likelyb1608d62011-05-18 11:19:24 -060092 match = of_match_device(of_platform_serial_table, &ofdev->dev);
93 if (!match)
Grant Likely793218d2011-02-22 21:10:26 -070094 return -EINVAL;
95
Grant Likely61c7a082010-04-13 16:12:29 -070096 if (of_find_property(ofdev->dev.of_node, "used-by-rtas", NULL))
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +010097 return -EBUSY;
98
Ishizaki Koue34b9c92007-05-31 19:33:04 +100099 info = kmalloc(sizeof(*info), GFP_KERNEL);
100 if (info == NULL)
101 return -ENOMEM;
102
Grant Likelyb1608d62011-05-18 11:19:24 -0600103 port_type = (unsigned long)match->data;
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100104 ret = of_platform_serial_setup(ofdev, port_type, &port);
105 if (ret)
106 goto out;
107
108 switch (port_type) {
Benjamin Krill58861882009-01-07 10:32:38 +0100109#ifdef CONFIG_SERIAL_8250
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100110 case PORT_8250 ... PORT_MAX_8250:
111 ret = serial8250_register_port(&port);
112 break;
Benjamin Krill58861882009-01-07 10:32:38 +0100113#endif
114#ifdef CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL
115 case PORT_NWPSERIAL:
116 ret = nwpserial_register_port(&port);
117 break;
118#endif
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100119 default:
120 /* need to add code for these */
Ishizaki Kou1558f9b2007-05-31 19:30:33 +1000121 case PORT_UNKNOWN:
122 dev_info(&ofdev->dev, "Unknown serial port found, ignored\n");
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100123 ret = -ENODEV;
124 break;
125 }
126 if (ret < 0)
127 goto out;
128
Ishizaki Koue34b9c92007-05-31 19:33:04 +1000129 info->type = port_type;
130 info->line = ret;
Greg Kroah-Hartman3cf62a52009-05-04 12:40:54 -0700131 dev_set_drvdata(&ofdev->dev, info);
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100132 return 0;
133out:
Ishizaki Koue34b9c92007-05-31 19:33:04 +1000134 kfree(info);
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100135 irq_dispose_mapping(port.irq);
136 return ret;
137}
138
139/*
140 * Release a line
141 */
Grant Likely2dc11582010-08-06 09:25:50 -0600142static int of_platform_serial_remove(struct platform_device *ofdev)
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100143{
Greg Kroah-Hartman3cf62a52009-05-04 12:40:54 -0700144 struct of_serial_info *info = dev_get_drvdata(&ofdev->dev);
Ishizaki Koue34b9c92007-05-31 19:33:04 +1000145 switch (info->type) {
Benjamin Krill58861882009-01-07 10:32:38 +0100146#ifdef CONFIG_SERIAL_8250
Ishizaki Koue34b9c92007-05-31 19:33:04 +1000147 case PORT_8250 ... PORT_MAX_8250:
148 serial8250_unregister_port(info->line);
149 break;
Benjamin Krill58861882009-01-07 10:32:38 +0100150#endif
151#ifdef CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL
152 case PORT_NWPSERIAL:
153 nwpserial_unregister_port(info->line);
154 break;
155#endif
Ishizaki Koue34b9c92007-05-31 19:33:04 +1000156 default:
157 /* need to add code for these */
158 break;
159 }
160 kfree(info);
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100161 return 0;
162}
163
164/*
165 * A few common types, add more as needed.
166 */
167static struct of_device_id __devinitdata of_platform_serial_table[] = {
Grant Likely8c6e9112011-02-22 19:12:21 -0700168 { .compatible = "ns8250", .data = (void *)PORT_8250, },
169 { .compatible = "ns16450", .data = (void *)PORT_16450, },
170 { .compatible = "ns16550a", .data = (void *)PORT_16550A, },
171 { .compatible = "ns16550", .data = (void *)PORT_16550, },
172 { .compatible = "ns16750", .data = (void *)PORT_16750, },
173 { .compatible = "ns16850", .data = (void *)PORT_16850, },
Benjamin Krill58861882009-01-07 10:32:38 +0100174#ifdef CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL
Grant Likely8c6e9112011-02-22 19:12:21 -0700175 { .compatible = "ibm,qpace-nwp-serial",
176 .data = (void *)PORT_NWPSERIAL, },
Benjamin Krill58861882009-01-07 10:32:38 +0100177#endif
Grant Likely8c6e9112011-02-22 19:12:21 -0700178 { .type = "serial", .data = (void *)PORT_UNKNOWN, },
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100179 { /* end of list */ },
180};
181
Grant Likely793218d2011-02-22 21:10:26 -0700182static struct platform_driver of_platform_serial_driver = {
Grant Likely40182942010-04-13 16:13:02 -0700183 .driver = {
184 .name = "of_serial",
185 .owner = THIS_MODULE,
186 .of_match_table = of_platform_serial_table,
187 },
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100188 .probe = of_platform_serial_probe,
189 .remove = of_platform_serial_remove,
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100190};
191
192static int __init of_platform_serial_init(void)
193{
Grant Likely793218d2011-02-22 21:10:26 -0700194 return platform_driver_register(&of_platform_serial_driver);
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100195}
196module_init(of_platform_serial_init);
197
198static void __exit of_platform_serial_exit(void)
199{
Grant Likely793218d2011-02-22 21:10:26 -0700200 return platform_driver_unregister(&of_platform_serial_driver);
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100201};
202module_exit(of_platform_serial_exit);
203
204MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>");
205MODULE_LICENSE("GPL");
206MODULE_DESCRIPTION("Serial Port driver for Open Firmware platform devices");