Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 1 | /* |
| 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 Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 14 | #include <linux/slab.h> |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 15 | #include <linux/serial_core.h> |
| 16 | #include <linux/serial_8250.h> |
Grant Likely | f1ca09b | 2010-08-16 23:44:49 -0600 | [diff] [blame] | 17 | #include <linux/of_address.h> |
Stephen Rothwell | c401b04 | 2008-05-23 16:32:54 +1000 | [diff] [blame] | 18 | #include <linux/of_platform.h> |
Benjamin Krill | 5886188d | 2009-01-07 10:32:38 +0100 | [diff] [blame] | 19 | #include <linux/nwpserial.h> |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 20 | |
Ishizaki Kou | e34b9c9 | 2007-05-31 19:33:04 +1000 | [diff] [blame] | 21 | struct of_serial_info { |
| 22 | int type; |
| 23 | int line; |
| 24 | }; |
| 25 | |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 26 | /* |
| 27 | * Fill a struct uart_port for a given device node |
| 28 | */ |
Grant Likely | 2dc1158 | 2010-08-06 09:25:50 -0600 | [diff] [blame] | 29 | static int __devinit of_platform_serial_setup(struct platform_device *ofdev, |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 30 | int type, struct uart_port *port) |
| 31 | { |
| 32 | struct resource resource; |
Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 33 | struct device_node *np = ofdev->dev.of_node; |
Ian Munsie | f14362d | 2010-10-01 17:06:07 +1000 | [diff] [blame] | 34 | const __be32 *clk, *spd; |
| 35 | const __be32 *prop; |
John Linn | b912b5e | 2008-04-03 10:22:19 +1100 | [diff] [blame] | 36 | int ret, prop_size; |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 37 | |
| 38 | memset(port, 0, sizeof *port); |
Stephen Rothwell | 40cd3a4 | 2007-05-01 13:54:02 +1000 | [diff] [blame] | 39 | spd = of_get_property(np, "current-speed", NULL); |
| 40 | clk = of_get_property(np, "clock-frequency", NULL); |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 41 | if (!clk) { |
| 42 | dev_warn(&ofdev->dev, "no clock-frequency property set\n"); |
| 43 | return -ENODEV; |
| 44 | } |
| 45 | |
| 46 | ret = of_address_to_resource(np, 0, &resource); |
| 47 | if (ret) { |
| 48 | dev_warn(&ofdev->dev, "invalid address\n"); |
| 49 | return ret; |
| 50 | } |
| 51 | |
| 52 | spin_lock_init(&port->lock); |
| 53 | port->mapbase = resource.start; |
John Linn | b912b5e | 2008-04-03 10:22:19 +1100 | [diff] [blame] | 54 | |
| 55 | /* Check for shifted address mapping */ |
| 56 | prop = of_get_property(np, "reg-offset", &prop_size); |
| 57 | if (prop && (prop_size == sizeof(u32))) |
Ian Munsie | f14362d | 2010-10-01 17:06:07 +1000 | [diff] [blame] | 58 | port->mapbase += be32_to_cpup(prop); |
John Linn | b912b5e | 2008-04-03 10:22:19 +1100 | [diff] [blame] | 59 | |
| 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))) |
Ian Munsie | f14362d | 2010-10-01 17:06:07 +1000 | [diff] [blame] | 63 | port->regshift = be32_to_cpup(prop); |
John Linn | b912b5e | 2008-04-03 10:22:19 +1100 | [diff] [blame] | 64 | |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 65 | port->irq = irq_of_parse_and_map(np, 0); |
| 66 | port->iotype = UPIO_MEM; |
| 67 | port->type = type; |
Ian Munsie | f14362d | 2010-10-01 17:06:07 +1000 | [diff] [blame] | 68 | port->uartclk = be32_to_cpup(clk); |
David Gibson | abb4a23 | 2007-05-06 14:48:49 -0700 | [diff] [blame] | 69 | port->flags = UPF_SHARE_IRQ | UPF_BOOT_AUTOCONF | UPF_IOREMAP |
Dave Mitchell | eedacbf | 2009-06-09 13:39:47 +0000 | [diff] [blame] | 70 | | UPF_FIXED_PORT | UPF_FIXED_TYPE; |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 71 | port->dev = &ofdev->dev; |
Stephen Neuendorffer | 19a7426 | 2008-04-03 03:52:13 +1100 | [diff] [blame] | 72 | /* If current-speed was set, then try not to change it. */ |
| 73 | if (spd) |
Ian Munsie | f14362d | 2010-10-01 17:06:07 +1000 | [diff] [blame] | 74 | port->custom_divisor = be32_to_cpup(clk) / (16 * (be32_to_cpup(spd))); |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 75 | |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | /* |
| 80 | * Try to register a serial port |
| 81 | */ |
Grant Likely | 2dc1158 | 2010-08-06 09:25:50 -0600 | [diff] [blame] | 82 | static int __devinit of_platform_serial_probe(struct platform_device *ofdev, |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 83 | const struct of_device_id *id) |
| 84 | { |
Ishizaki Kou | e34b9c9 | 2007-05-31 19:33:04 +1000 | [diff] [blame] | 85 | struct of_serial_info *info; |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 86 | struct uart_port port; |
| 87 | int port_type; |
| 88 | int ret; |
| 89 | |
Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 90 | if (of_find_property(ofdev->dev.of_node, "used-by-rtas", NULL)) |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 91 | return -EBUSY; |
| 92 | |
Ishizaki Kou | e34b9c9 | 2007-05-31 19:33:04 +1000 | [diff] [blame] | 93 | info = kmalloc(sizeof(*info), GFP_KERNEL); |
| 94 | if (info == NULL) |
| 95 | return -ENOMEM; |
| 96 | |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 97 | port_type = (unsigned long)id->data; |
| 98 | ret = of_platform_serial_setup(ofdev, port_type, &port); |
| 99 | if (ret) |
| 100 | goto out; |
| 101 | |
| 102 | switch (port_type) { |
Benjamin Krill | 5886188d | 2009-01-07 10:32:38 +0100 | [diff] [blame] | 103 | #ifdef CONFIG_SERIAL_8250 |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 104 | case PORT_8250 ... PORT_MAX_8250: |
| 105 | ret = serial8250_register_port(&port); |
| 106 | break; |
Benjamin Krill | 5886188d | 2009-01-07 10:32:38 +0100 | [diff] [blame] | 107 | #endif |
| 108 | #ifdef CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL |
| 109 | case PORT_NWPSERIAL: |
| 110 | ret = nwpserial_register_port(&port); |
| 111 | break; |
| 112 | #endif |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 113 | default: |
| 114 | /* need to add code for these */ |
Ishizaki Kou | 1558f9b | 2007-05-31 19:30:33 +1000 | [diff] [blame] | 115 | case PORT_UNKNOWN: |
| 116 | dev_info(&ofdev->dev, "Unknown serial port found, ignored\n"); |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 117 | ret = -ENODEV; |
| 118 | break; |
| 119 | } |
| 120 | if (ret < 0) |
| 121 | goto out; |
| 122 | |
Ishizaki Kou | e34b9c9 | 2007-05-31 19:33:04 +1000 | [diff] [blame] | 123 | info->type = port_type; |
| 124 | info->line = ret; |
Greg Kroah-Hartman | 3cf62a5 | 2009-05-04 12:40:54 -0700 | [diff] [blame] | 125 | dev_set_drvdata(&ofdev->dev, info); |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 126 | return 0; |
| 127 | out: |
Ishizaki Kou | e34b9c9 | 2007-05-31 19:33:04 +1000 | [diff] [blame] | 128 | kfree(info); |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 129 | irq_dispose_mapping(port.irq); |
| 130 | return ret; |
| 131 | } |
| 132 | |
| 133 | /* |
| 134 | * Release a line |
| 135 | */ |
Grant Likely | 2dc1158 | 2010-08-06 09:25:50 -0600 | [diff] [blame] | 136 | static int of_platform_serial_remove(struct platform_device *ofdev) |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 137 | { |
Greg Kroah-Hartman | 3cf62a5 | 2009-05-04 12:40:54 -0700 | [diff] [blame] | 138 | struct of_serial_info *info = dev_get_drvdata(&ofdev->dev); |
Ishizaki Kou | e34b9c9 | 2007-05-31 19:33:04 +1000 | [diff] [blame] | 139 | switch (info->type) { |
Benjamin Krill | 5886188d | 2009-01-07 10:32:38 +0100 | [diff] [blame] | 140 | #ifdef CONFIG_SERIAL_8250 |
Ishizaki Kou | e34b9c9 | 2007-05-31 19:33:04 +1000 | [diff] [blame] | 141 | case PORT_8250 ... PORT_MAX_8250: |
| 142 | serial8250_unregister_port(info->line); |
| 143 | break; |
Benjamin Krill | 5886188d | 2009-01-07 10:32:38 +0100 | [diff] [blame] | 144 | #endif |
| 145 | #ifdef CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL |
| 146 | case PORT_NWPSERIAL: |
| 147 | nwpserial_unregister_port(info->line); |
| 148 | break; |
| 149 | #endif |
Ishizaki Kou | e34b9c9 | 2007-05-31 19:33:04 +1000 | [diff] [blame] | 150 | default: |
| 151 | /* need to add code for these */ |
| 152 | break; |
| 153 | } |
| 154 | kfree(info); |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | /* |
| 159 | * A few common types, add more as needed. |
| 160 | */ |
| 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, }, |
Michal Simek | 16173c7 | 2009-11-24 10:22:41 +0000 | [diff] [blame] | 164 | { .type = "serial", .compatible = "ns16550a", .data = (void *)PORT_16550A, }, |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 165 | { .type = "serial", .compatible = "ns16550", .data = (void *)PORT_16550, }, |
| 166 | { .type = "serial", .compatible = "ns16750", .data = (void *)PORT_16750, }, |
Matthias Fuchs | 0025e75 | 2009-01-15 09:43:35 +0000 | [diff] [blame] | 167 | { .type = "serial", .compatible = "ns16850", .data = (void *)PORT_16850, }, |
Benjamin Krill | 5886188d | 2009-01-07 10:32:38 +0100 | [diff] [blame] | 168 | #ifdef CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL |
| 169 | { .type = "serial", .compatible = "ibm,qpace-nwp-serial", |
| 170 | .data = (void *)PORT_NWPSERIAL, }, |
| 171 | #endif |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 172 | { .type = "serial", .data = (void *)PORT_UNKNOWN, }, |
| 173 | { /* end of list */ }, |
| 174 | }; |
| 175 | |
Josh Boyer | e84290d | 2008-03-10 11:43:35 -0700 | [diff] [blame] | 176 | static struct of_platform_driver of_platform_serial_driver = { |
Grant Likely | 4018294 | 2010-04-13 16:13:02 -0700 | [diff] [blame] | 177 | .driver = { |
| 178 | .name = "of_serial", |
| 179 | .owner = THIS_MODULE, |
| 180 | .of_match_table = of_platform_serial_table, |
| 181 | }, |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 182 | .probe = of_platform_serial_probe, |
| 183 | .remove = of_platform_serial_remove, |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 184 | }; |
| 185 | |
| 186 | static int __init of_platform_serial_init(void) |
| 187 | { |
| 188 | return of_register_platform_driver(&of_platform_serial_driver); |
| 189 | } |
| 190 | module_init(of_platform_serial_init); |
| 191 | |
| 192 | static void __exit of_platform_serial_exit(void) |
| 193 | { |
| 194 | return of_unregister_platform_driver(&of_platform_serial_driver); |
| 195 | }; |
| 196 | module_exit(of_platform_serial_exit); |
| 197 | |
| 198 | MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>"); |
| 199 | MODULE_LICENSE("GPL"); |
| 200 | MODULE_DESCRIPTION("Serial Port driver for Open Firmware platform devices"); |