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/serial_core.h>
15 #include <linux/serial_8250.h>
16 #include <linux/of_platform.h>
17 #include <linux/nwpserial.h>
21 struct of_serial_info
{
27 * Fill a struct uart_port for a given device node
29 static int __devinit
of_platform_serial_setup(struct of_device
*ofdev
,
30 int type
, struct uart_port
*port
)
32 struct resource resource
;
33 struct device_node
*np
= ofdev
->node
;
34 const unsigned int *clk
, *spd
;
38 memset(port
, 0, sizeof *port
);
39 spd
= of_get_property(np
, "current-speed", NULL
);
40 clk
= of_get_property(np
, "clock-frequency", NULL
);
42 dev_warn(&ofdev
->dev
, "no clock-frequency property set\n");
46 ret
= of_address_to_resource(np
, 0, &resource
);
48 dev_warn(&ofdev
->dev
, "invalid address\n");
52 spin_lock_init(&port
->lock
);
53 port
->mapbase
= resource
.start
;
55 /* Check for shifted address mapping */
56 prop
= of_get_property(np
, "reg-offset", &prop_size
);
57 if (prop
&& (prop_size
== sizeof(u32
)))
58 port
->mapbase
+= *prop
;
60 /* Check for registers offset within the devices address range */
61 prop
= of_get_property(np
, "reg-shift", &prop_size
);
62 if (prop
&& (prop_size
== sizeof(u32
)))
63 port
->regshift
= *prop
;
65 port
->irq
= irq_of_parse_and_map(np
, 0);
66 port
->iotype
= UPIO_MEM
;
69 port
->flags
= UPF_SHARE_IRQ
| UPF_BOOT_AUTOCONF
| UPF_IOREMAP
70 | UPF_FIXED_PORT
| UPF_FIXED_TYPE
;
71 port
->dev
= &ofdev
->dev
;
72 /* If current-speed was set, then try not to change it. */
74 port
->custom_divisor
= *clk
/ (16 * (*spd
));
80 * Try to register a serial port
82 static int __devinit
of_platform_serial_probe(struct of_device
*ofdev
,
83 const struct of_device_id
*id
)
85 struct of_serial_info
*info
;
86 struct uart_port port
;
90 if (of_find_property(ofdev
->node
, "used-by-rtas", NULL
))
93 info
= kmalloc(sizeof(*info
), GFP_KERNEL
);
97 port_type
= (unsigned long)id
->data
;
98 ret
= of_platform_serial_setup(ofdev
, port_type
, &port
);
103 #ifdef CONFIG_SERIAL_8250
104 case PORT_8250
... PORT_MAX_8250
:
105 ret
= serial8250_register_port(&port
);
108 #ifdef CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL
110 ret
= nwpserial_register_port(&port
);
114 /* need to add code for these */
116 dev_info(&ofdev
->dev
, "Unknown serial port found, ignored\n");
123 info
->type
= port_type
;
125 dev_set_drvdata(&ofdev
->dev
, info
);
129 irq_dispose_mapping(port
.irq
);
136 static int of_platform_serial_remove(struct of_device
*ofdev
)
138 struct of_serial_info
*info
= dev_get_drvdata(&ofdev
->dev
);
139 switch (info
->type
) {
140 #ifdef CONFIG_SERIAL_8250
141 case PORT_8250
... PORT_MAX_8250
:
142 serial8250_unregister_port(info
->line
);
145 #ifdef CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL
147 nwpserial_unregister_port(info
->line
);
151 /* need to add code for these */
159 * A few common types, add more as needed.
161 static struct of_device_id __devinitdata of_platform_serial_table
[] = {
162 { .type
= "serial", .compatible
= "ns8250", .data
= (void *)PORT_8250
, },
163 { .type
= "serial", .compatible
= "ns16450", .data
= (void *)PORT_16450
, },
164 { .type
= "serial", .compatible
= "ns16550", .data
= (void *)PORT_16550
, },
165 { .type
= "serial", .compatible
= "ns16750", .data
= (void *)PORT_16750
, },
166 { .type
= "serial", .compatible
= "ns16850", .data
= (void *)PORT_16850
, },
167 #ifdef CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL
168 { .type
= "serial", .compatible
= "ibm,qpace-nwp-serial",
169 .data
= (void *)PORT_NWPSERIAL
, },
171 { .type
= "serial", .data
= (void *)PORT_UNKNOWN
, },
172 { /* end of list */ },
175 static struct of_platform_driver of_platform_serial_driver
= {
176 .owner
= THIS_MODULE
,
178 .probe
= of_platform_serial_probe
,
179 .remove
= of_platform_serial_remove
,
180 .match_table
= of_platform_serial_table
,
183 static int __init
of_platform_serial_init(void)
185 return of_register_platform_driver(&of_platform_serial_driver
);
187 module_init(of_platform_serial_init
);
189 static void __exit
of_platform_serial_exit(void)
191 return of_unregister_platform_driver(&of_platform_serial_driver
);
193 module_exit(of_platform_serial_exit
);
195 MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>");
196 MODULE_LICENSE("GPL");
197 MODULE_DESCRIPTION("Serial Port driver for Open Firmware platform devices");