blob: 8fa0ff561e9fbd901acb46787bff587b035ddd79 [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>
Stephen Rothwellc401b042008-05-23 16:32:54 +100016#include <linux/of_platform.h>
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +010017
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +010018#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;
John Linnb912b5e2008-04-03 10:22:19 +110034 const u32 *prop;
35 int ret, prop_size;
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +010036
37 memset(port, 0, sizeof *port);
Stephen Rothwell40cd3a42007-05-01 13:54:02 +100038 spd = of_get_property(np, "current-speed", NULL);
39 clk = of_get_property(np, "clock-frequency", NULL);
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +010040 if (!clk) {
41 dev_warn(&ofdev->dev, "no clock-frequency property set\n");
42 return -ENODEV;
43 }
44
45 ret = of_address_to_resource(np, 0, &resource);
46 if (ret) {
47 dev_warn(&ofdev->dev, "invalid address\n");
48 return ret;
49 }
50
51 spin_lock_init(&port->lock);
52 port->mapbase = resource.start;
John Linnb912b5e2008-04-03 10:22:19 +110053
54 /* Check for shifted address mapping */
55 prop = of_get_property(np, "reg-offset", &prop_size);
56 if (prop && (prop_size == sizeof(u32)))
57 port->mapbase += *prop;
58
59 /* Check for registers offset within the devices address range */
60 prop = of_get_property(np, "reg-shift", &prop_size);
61 if (prop && (prop_size == sizeof(u32)))
62 port->regshift = *prop;
63
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +010064 port->irq = irq_of_parse_and_map(np, 0);
65 port->iotype = UPIO_MEM;
66 port->type = type;
67 port->uartclk = *clk;
David Gibsonabb4a232007-05-06 14:48:49 -070068 port->flags = UPF_SHARE_IRQ | UPF_BOOT_AUTOCONF | UPF_IOREMAP
69 | UPF_FIXED_PORT;
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +010070 port->dev = &ofdev->dev;
Stephen Neuendorffer19a74262008-04-03 03:52:13 +110071 /* If current-speed was set, then try not to change it. */
72 if (spd)
73 port->custom_divisor = *clk / (16 * (*spd));
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +010074
75 return 0;
76}
77
78/*
79 * Try to register a serial port
80 */
81static int __devinit of_platform_serial_probe(struct of_device *ofdev,
82 const struct of_device_id *id)
83{
Ishizaki Koue34b9c92007-05-31 19:33:04 +100084 struct of_serial_info *info;
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +010085 struct uart_port port;
86 int port_type;
87 int ret;
88
89 if (of_find_property(ofdev->node, "used-by-rtas", NULL))
90 return -EBUSY;
91
Ishizaki Koue34b9c92007-05-31 19:33:04 +100092 info = kmalloc(sizeof(*info), GFP_KERNEL);
93 if (info == NULL)
94 return -ENOMEM;
95
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +010096 port_type = (unsigned long)id->data;
97 ret = of_platform_serial_setup(ofdev, port_type, &port);
98 if (ret)
99 goto out;
100
101 switch (port_type) {
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100102 case PORT_8250 ... PORT_MAX_8250:
103 ret = serial8250_register_port(&port);
104 break;
105 default:
106 /* need to add code for these */
Ishizaki Kou1558f9b2007-05-31 19:30:33 +1000107 case PORT_UNKNOWN:
108 dev_info(&ofdev->dev, "Unknown serial port found, ignored\n");
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100109 ret = -ENODEV;
110 break;
111 }
112 if (ret < 0)
113 goto out;
114
Ishizaki Koue34b9c92007-05-31 19:33:04 +1000115 info->type = port_type;
116 info->line = ret;
117 ofdev->dev.driver_data = info;
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100118 return 0;
119out:
Ishizaki Koue34b9c92007-05-31 19:33:04 +1000120 kfree(info);
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100121 irq_dispose_mapping(port.irq);
122 return ret;
123}
124
125/*
126 * Release a line
127 */
128static int of_platform_serial_remove(struct of_device *ofdev)
129{
Ishizaki Koue34b9c92007-05-31 19:33:04 +1000130 struct of_serial_info *info = ofdev->dev.driver_data;
131 switch (info->type) {
132 case PORT_8250 ... PORT_MAX_8250:
133 serial8250_unregister_port(info->line);
134 break;
135 default:
136 /* need to add code for these */
137 break;
138 }
139 kfree(info);
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100140 return 0;
141}
142
143/*
144 * A few common types, add more as needed.
145 */
146static struct of_device_id __devinitdata of_platform_serial_table[] = {
147 { .type = "serial", .compatible = "ns8250", .data = (void *)PORT_8250, },
148 { .type = "serial", .compatible = "ns16450", .data = (void *)PORT_16450, },
149 { .type = "serial", .compatible = "ns16550", .data = (void *)PORT_16550, },
150 { .type = "serial", .compatible = "ns16750", .data = (void *)PORT_16750, },
151 { .type = "serial", .data = (void *)PORT_UNKNOWN, },
152 { /* end of list */ },
153};
154
Josh Boyere84290d2008-03-10 11:43:35 -0700155static struct of_platform_driver of_platform_serial_driver = {
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100156 .owner = THIS_MODULE,
157 .name = "of_serial",
158 .probe = of_platform_serial_probe,
159 .remove = of_platform_serial_remove,
160 .match_table = of_platform_serial_table,
161};
162
163static int __init of_platform_serial_init(void)
164{
165 return of_register_platform_driver(&of_platform_serial_driver);
166}
167module_init(of_platform_serial_init);
168
169static void __exit of_platform_serial_exit(void)
170{
171 return of_unregister_platform_driver(&of_platform_serial_driver);
172};
173module_exit(of_platform_serial_exit);
174
175MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>");
176MODULE_LICENSE("GPL");
177MODULE_DESCRIPTION("Serial Port driver for Open Firmware platform devices");