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> |
| 14 | #include <linux/serial_core.h> |
| 15 | #include <linux/serial_8250.h> |
Stephen Rothwell | c401b04 | 2008-05-23 16:32:54 +1000 | [diff] [blame] | 16 | #include <linux/of_platform.h> |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 17 | |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 18 | #include <asm/prom.h> |
| 19 | |
Ishizaki Kou | e34b9c9 | 2007-05-31 19:33:04 +1000 | [diff] [blame] | 20 | struct of_serial_info { |
| 21 | int type; |
| 22 | int line; |
| 23 | }; |
| 24 | |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 25 | /* |
| 26 | * Fill a struct uart_port for a given device node |
| 27 | */ |
| 28 | static 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 Linn | b912b5e | 2008-04-03 10:22:19 +1100 | [diff] [blame] | 34 | const u32 *prop; |
| 35 | int ret, prop_size; |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 36 | |
| 37 | memset(port, 0, sizeof *port); |
Stephen Rothwell | 40cd3a4 | 2007-05-01 13:54:02 +1000 | [diff] [blame] | 38 | spd = of_get_property(np, "current-speed", NULL); |
| 39 | clk = of_get_property(np, "clock-frequency", NULL); |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 40 | 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 Linn | b912b5e | 2008-04-03 10:22:19 +1100 | [diff] [blame] | 53 | |
| 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 Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 64 | port->irq = irq_of_parse_and_map(np, 0); |
| 65 | port->iotype = UPIO_MEM; |
| 66 | port->type = type; |
| 67 | port->uartclk = *clk; |
David Gibson | abb4a23 | 2007-05-06 14:48:49 -0700 | [diff] [blame] | 68 | port->flags = UPF_SHARE_IRQ | UPF_BOOT_AUTOCONF | UPF_IOREMAP |
| 69 | | UPF_FIXED_PORT; |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 70 | port->dev = &ofdev->dev; |
Stephen Neuendorffer | 19a7426 | 2008-04-03 03:52:13 +1100 | [diff] [blame] | 71 | /* If current-speed was set, then try not to change it. */ |
| 72 | if (spd) |
| 73 | port->custom_divisor = *clk / (16 * (*spd)); |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 74 | |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | /* |
| 79 | * Try to register a serial port |
| 80 | */ |
| 81 | static int __devinit of_platform_serial_probe(struct of_device *ofdev, |
| 82 | const struct of_device_id *id) |
| 83 | { |
Ishizaki Kou | e34b9c9 | 2007-05-31 19:33:04 +1000 | [diff] [blame] | 84 | struct of_serial_info *info; |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 85 | 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 Kou | e34b9c9 | 2007-05-31 19:33:04 +1000 | [diff] [blame] | 92 | info = kmalloc(sizeof(*info), GFP_KERNEL); |
| 93 | if (info == NULL) |
| 94 | return -ENOMEM; |
| 95 | |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 96 | 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 Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 102 | case PORT_8250 ... PORT_MAX_8250: |
| 103 | ret = serial8250_register_port(&port); |
| 104 | break; |
| 105 | default: |
| 106 | /* need to add code for these */ |
Ishizaki Kou | 1558f9b | 2007-05-31 19:30:33 +1000 | [diff] [blame] | 107 | case PORT_UNKNOWN: |
| 108 | dev_info(&ofdev->dev, "Unknown serial port found, ignored\n"); |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 109 | ret = -ENODEV; |
| 110 | break; |
| 111 | } |
| 112 | if (ret < 0) |
| 113 | goto out; |
| 114 | |
Ishizaki Kou | e34b9c9 | 2007-05-31 19:33:04 +1000 | [diff] [blame] | 115 | info->type = port_type; |
| 116 | info->line = ret; |
| 117 | ofdev->dev.driver_data = info; |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 118 | return 0; |
| 119 | out: |
Ishizaki Kou | e34b9c9 | 2007-05-31 19:33:04 +1000 | [diff] [blame] | 120 | kfree(info); |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 121 | irq_dispose_mapping(port.irq); |
| 122 | return ret; |
| 123 | } |
| 124 | |
| 125 | /* |
| 126 | * Release a line |
| 127 | */ |
| 128 | static int of_platform_serial_remove(struct of_device *ofdev) |
| 129 | { |
Ishizaki Kou | e34b9c9 | 2007-05-31 19:33:04 +1000 | [diff] [blame] | 130 | 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 Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | /* |
| 144 | * A few common types, add more as needed. |
| 145 | */ |
| 146 | static 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 Boyer | e84290d | 2008-03-10 11:43:35 -0700 | [diff] [blame] | 155 | static struct of_platform_driver of_platform_serial_driver = { |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 156 | .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 | |
| 163 | static int __init of_platform_serial_init(void) |
| 164 | { |
| 165 | return of_register_platform_driver(&of_platform_serial_driver); |
| 166 | } |
| 167 | module_init(of_platform_serial_init); |
| 168 | |
| 169 | static void __exit of_platform_serial_exit(void) |
| 170 | { |
| 171 | return of_unregister_platform_driver(&of_platform_serial_driver); |
| 172 | }; |
| 173 | module_exit(of_platform_serial_exit); |
| 174 | |
| 175 | MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>"); |
| 176 | MODULE_LICENSE("GPL"); |
| 177 | MODULE_DESCRIPTION("Serial Port driver for Open Firmware platform devices"); |