2 * Serial Port driver for Open Firmware platform devices
4 * Copyright (C) 2006 Arnd Bergmann <arnd@arndb.de>, IBM Corp.
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.
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/slab.h>
15 #include <linux/serial_core.h>
16 #include <linux/serial_8250.h>
17 #include <linux/of_address.h>
18 #include <linux/of_irq.h>
19 #include <linux/of_platform.h>
20 #include <linux/nwpserial.h>
22 struct of_serial_info
{
28 * Fill a struct uart_port for a given device node
30 static int __devinit
of_platform_serial_setup(struct platform_device
*ofdev
,
31 int type
, struct uart_port
*port
)
33 struct resource resource
;
34 struct device_node
*np
= ofdev
->dev
.of_node
;
38 memset(port
, 0, sizeof *port
);
39 if (of_property_read_u32(np
, "clock-frequency", &clk
)) {
40 dev_warn(&ofdev
->dev
, "no clock-frequency property set\n");
43 /* If current-speed was set, then try not to change it. */
44 if (of_property_read_u32(np
, "current-speed", &spd
) == 0)
45 port
->custom_divisor
= clk
/ (16 * spd
);
47 ret
= of_address_to_resource(np
, 0, &resource
);
49 dev_warn(&ofdev
->dev
, "invalid address\n");
53 spin_lock_init(&port
->lock
);
54 port
->mapbase
= resource
.start
;
56 /* Check for shifted address mapping */
57 if (of_property_read_u32(np
, "reg-offset", &prop
) == 0)
58 port
->mapbase
+= prop
;
60 /* Check for registers offset within the devices address range */
61 if (of_property_read_u32(np
, "reg-shift", &prop
) == 0)
62 port
->regshift
= prop
;
64 port
->irq
= irq_of_parse_and_map(np
, 0);
65 port
->iotype
= UPIO_MEM
;
66 if (of_property_read_u32(np
, "reg-io-width", &prop
) == 0) {
69 port
->iotype
= UPIO_MEM
;
72 port
->iotype
= UPIO_MEM32
;
75 dev_warn(&ofdev
->dev
, "unsupported reg-io-width (%d)\n",
83 port
->flags
= UPF_SHARE_IRQ
| UPF_BOOT_AUTOCONF
| UPF_IOREMAP
84 | UPF_FIXED_PORT
| UPF_FIXED_TYPE
;
85 port
->dev
= &ofdev
->dev
;
91 * Try to register a serial port
93 static struct of_device_id of_platform_serial_table
[];
94 static int __devinit
of_platform_serial_probe(struct platform_device
*ofdev
)
96 const struct of_device_id
*match
;
97 struct of_serial_info
*info
;
98 struct uart_port port
;
102 match
= of_match_device(of_platform_serial_table
, &ofdev
->dev
);
106 if (of_find_property(ofdev
->dev
.of_node
, "used-by-rtas", NULL
))
109 info
= kmalloc(sizeof(*info
), GFP_KERNEL
);
113 port_type
= (unsigned long)match
->data
;
114 ret
= of_platform_serial_setup(ofdev
, port_type
, &port
);
119 #ifdef CONFIG_SERIAL_8250
120 case PORT_8250
... PORT_MAX_8250
:
121 ret
= serial8250_register_port(&port
);
124 #ifdef CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL
126 ret
= nwpserial_register_port(&port
);
130 /* need to add code for these */
132 dev_info(&ofdev
->dev
, "Unknown serial port found, ignored\n");
139 info
->type
= port_type
;
141 dev_set_drvdata(&ofdev
->dev
, info
);
145 irq_dispose_mapping(port
.irq
);
152 static int of_platform_serial_remove(struct platform_device
*ofdev
)
154 struct of_serial_info
*info
= dev_get_drvdata(&ofdev
->dev
);
155 switch (info
->type
) {
156 #ifdef CONFIG_SERIAL_8250
157 case PORT_8250
... PORT_MAX_8250
:
158 serial8250_unregister_port(info
->line
);
161 #ifdef CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL
163 nwpserial_unregister_port(info
->line
);
167 /* need to add code for these */
175 * A few common types, add more as needed.
177 static struct of_device_id __devinitdata of_platform_serial_table
[] = {
178 { .compatible
= "ns8250", .data
= (void *)PORT_8250
, },
179 { .compatible
= "ns16450", .data
= (void *)PORT_16450
, },
180 { .compatible
= "ns16550a", .data
= (void *)PORT_16550A
, },
181 { .compatible
= "ns16550", .data
= (void *)PORT_16550
, },
182 { .compatible
= "ns16750", .data
= (void *)PORT_16750
, },
183 { .compatible
= "ns16850", .data
= (void *)PORT_16850
, },
184 { .compatible
= "nvidia,tegra20-uart", .data
= (void *)PORT_TEGRA
, },
185 { .compatible
= "altr,16550-FIFO32",
186 .data
= (void *)PORT_ALTR_16550_F32
, },
187 { .compatible
= "altr,16550-FIFO64",
188 .data
= (void *)PORT_ALTR_16550_F64
, },
189 { .compatible
= "altr,16550-FIFO128",
190 .data
= (void *)PORT_ALTR_16550_F128
, },
191 #ifdef CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL
192 { .compatible
= "ibm,qpace-nwp-serial",
193 .data
= (void *)PORT_NWPSERIAL
, },
195 { .type
= "serial", .data
= (void *)PORT_UNKNOWN
, },
196 { /* end of list */ },
199 static struct platform_driver of_platform_serial_driver
= {
202 .owner
= THIS_MODULE
,
203 .of_match_table
= of_platform_serial_table
,
205 .probe
= of_platform_serial_probe
,
206 .remove
= of_platform_serial_remove
,
209 module_platform_driver(of_platform_serial_driver
);
211 MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>");
212 MODULE_LICENSE("GPL");
213 MODULE_DESCRIPTION("Serial Port driver for Open Firmware platform devices");