| 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> | 
| Rob Herring | 73930a8 | 2010-11-17 17:50:23 -0600 | [diff] [blame] | 18 | #include <linux/of_irq.h> | 
| Stephen Rothwell | c401b04 | 2008-05-23 16:32:54 +1000 | [diff] [blame] | 19 | #include <linux/of_platform.h> | 
| Benjamin Krill | 5886188 | 2009-01-07 10:32:38 +0100 | [diff] [blame] | 20 | #include <linux/nwpserial.h> | 
| Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 21 |  | 
| Ishizaki Kou | e34b9c9 | 2007-05-31 19:33:04 +1000 | [diff] [blame] | 22 | struct of_serial_info { | 
|  | 23 | int type; | 
|  | 24 | int line; | 
|  | 25 | }; | 
|  | 26 |  | 
| Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 27 | /* | 
|  | 28 | * Fill a struct uart_port for a given device node | 
|  | 29 | */ | 
| Grant Likely | 2dc1158 | 2010-08-06 09:25:50 -0600 | [diff] [blame] | 30 | static int __devinit of_platform_serial_setup(struct platform_device *ofdev, | 
| Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 31 | int type, struct uart_port *port) | 
|  | 32 | { | 
|  | 33 | struct resource resource; | 
| Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 34 | struct device_node *np = ofdev->dev.of_node; | 
| Grant Likely | b84e773 | 2011-06-30 12:39:12 -0600 | [diff] [blame] | 35 | u32 clk, spd, prop; | 
|  | 36 | int ret; | 
| Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 37 |  | 
|  | 38 | memset(port, 0, sizeof *port); | 
| Grant Likely | b84e773 | 2011-06-30 12:39:12 -0600 | [diff] [blame] | 39 | if (of_property_read_u32(np, "clock-frequency", &clk)) { | 
| Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 40 | dev_warn(&ofdev->dev, "no clock-frequency property set\n"); | 
|  | 41 | return -ENODEV; | 
|  | 42 | } | 
| Grant Likely | b84e773 | 2011-06-30 12:39:12 -0600 | [diff] [blame] | 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); | 
| Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 46 |  | 
|  | 47 | ret = of_address_to_resource(np, 0, &resource); | 
|  | 48 | if (ret) { | 
|  | 49 | dev_warn(&ofdev->dev, "invalid address\n"); | 
|  | 50 | return ret; | 
|  | 51 | } | 
|  | 52 |  | 
|  | 53 | spin_lock_init(&port->lock); | 
|  | 54 | port->mapbase = resource.start; | 
| John Linn | b912b5e | 2008-04-03 10:22:19 +1100 | [diff] [blame] | 55 |  | 
|  | 56 | /* Check for shifted address mapping */ | 
| Grant Likely | b84e773 | 2011-06-30 12:39:12 -0600 | [diff] [blame] | 57 | if (of_property_read_u32(np, "reg-offset", &prop) == 0) | 
|  | 58 | port->mapbase += prop; | 
| John Linn | b912b5e | 2008-04-03 10:22:19 +1100 | [diff] [blame] | 59 |  | 
|  | 60 | /* Check for registers offset within the devices address range */ | 
| Grant Likely | b84e773 | 2011-06-30 12:39:12 -0600 | [diff] [blame] | 61 | if (of_property_read_u32(np, "reg-shift", &prop) == 0) | 
|  | 62 | port->regshift = prop; | 
| John Linn | b912b5e | 2008-04-03 10:22:19 +1100 | [diff] [blame] | 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; | 
| Grant Likely | b84e773 | 2011-06-30 12:39:12 -0600 | [diff] [blame] | 66 | if (of_property_read_u32(np, "reg-io-width", &prop) == 0) { | 
|  | 67 | switch (prop) { | 
| Jamie Iles | 7423734 | 2011-06-27 13:32:34 +0100 | [diff] [blame] | 68 | case 1: | 
|  | 69 | port->iotype = UPIO_MEM; | 
|  | 70 | break; | 
|  | 71 | case 4: | 
|  | 72 | port->iotype = UPIO_MEM32; | 
|  | 73 | break; | 
|  | 74 | default: | 
| Grant Likely | b84e773 | 2011-06-30 12:39:12 -0600 | [diff] [blame] | 75 | dev_warn(&ofdev->dev, "unsupported reg-io-width (%d)\n", | 
|  | 76 | prop); | 
| Jamie Iles | 7423734 | 2011-06-27 13:32:34 +0100 | [diff] [blame] | 77 | return -EINVAL; | 
|  | 78 | } | 
|  | 79 | } | 
|  | 80 |  | 
| Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 81 | port->type = type; | 
| Grant Likely | b84e773 | 2011-06-30 12:39:12 -0600 | [diff] [blame] | 82 | port->uartclk = clk; | 
| David Gibson | abb4a23 | 2007-05-06 14:48:49 -0700 | [diff] [blame] | 83 | port->flags = UPF_SHARE_IRQ | UPF_BOOT_AUTOCONF | UPF_IOREMAP | 
| Dave Mitchell | eedacbf | 2009-06-09 13:39:47 +0000 | [diff] [blame] | 84 | | UPF_FIXED_PORT | UPF_FIXED_TYPE; | 
| Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 85 | port->dev = &ofdev->dev; | 
| Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 86 |  | 
|  | 87 | return 0; | 
|  | 88 | } | 
|  | 89 |  | 
|  | 90 | /* | 
|  | 91 | * Try to register a serial port | 
|  | 92 | */ | 
| Grant Likely | b1608d6 | 2011-05-18 11:19:24 -0600 | [diff] [blame] | 93 | static struct of_device_id of_platform_serial_table[]; | 
| Grant Likely | 793218d | 2011-02-22 21:10:26 -0700 | [diff] [blame] | 94 | static int __devinit of_platform_serial_probe(struct platform_device *ofdev) | 
| Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 95 | { | 
| Grant Likely | b1608d6 | 2011-05-18 11:19:24 -0600 | [diff] [blame] | 96 | const struct of_device_id *match; | 
| Ishizaki Kou | e34b9c9 | 2007-05-31 19:33:04 +1000 | [diff] [blame] | 97 | struct of_serial_info *info; | 
| Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 98 | struct uart_port port; | 
|  | 99 | int port_type; | 
|  | 100 | int ret; | 
|  | 101 |  | 
| Grant Likely | b1608d6 | 2011-05-18 11:19:24 -0600 | [diff] [blame] | 102 | match = of_match_device(of_platform_serial_table, &ofdev->dev); | 
|  | 103 | if (!match) | 
| Grant Likely | 793218d | 2011-02-22 21:10:26 -0700 | [diff] [blame] | 104 | return -EINVAL; | 
|  | 105 |  | 
| Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 106 | if (of_find_property(ofdev->dev.of_node, "used-by-rtas", NULL)) | 
| Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 107 | return -EBUSY; | 
|  | 108 |  | 
| Ishizaki Kou | e34b9c9 | 2007-05-31 19:33:04 +1000 | [diff] [blame] | 109 | info = kmalloc(sizeof(*info), GFP_KERNEL); | 
|  | 110 | if (info == NULL) | 
|  | 111 | return -ENOMEM; | 
|  | 112 |  | 
| Grant Likely | b1608d6 | 2011-05-18 11:19:24 -0600 | [diff] [blame] | 113 | port_type = (unsigned long)match->data; | 
| Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 114 | ret = of_platform_serial_setup(ofdev, port_type, &port); | 
|  | 115 | if (ret) | 
|  | 116 | goto out; | 
|  | 117 |  | 
|  | 118 | switch (port_type) { | 
| Benjamin Krill | 5886188 | 2009-01-07 10:32:38 +0100 | [diff] [blame] | 119 | #ifdef CONFIG_SERIAL_8250 | 
| Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 120 | case PORT_8250 ... PORT_MAX_8250: | 
|  | 121 | ret = serial8250_register_port(&port); | 
|  | 122 | break; | 
| Benjamin Krill | 5886188 | 2009-01-07 10:32:38 +0100 | [diff] [blame] | 123 | #endif | 
|  | 124 | #ifdef CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL | 
|  | 125 | case PORT_NWPSERIAL: | 
|  | 126 | ret = nwpserial_register_port(&port); | 
|  | 127 | break; | 
|  | 128 | #endif | 
| Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 129 | default: | 
|  | 130 | /* need to add code for these */ | 
| Ishizaki Kou | 1558f9b | 2007-05-31 19:30:33 +1000 | [diff] [blame] | 131 | case PORT_UNKNOWN: | 
|  | 132 | dev_info(&ofdev->dev, "Unknown serial port found, ignored\n"); | 
| Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 133 | ret = -ENODEV; | 
|  | 134 | break; | 
|  | 135 | } | 
|  | 136 | if (ret < 0) | 
|  | 137 | goto out; | 
|  | 138 |  | 
| Ishizaki Kou | e34b9c9 | 2007-05-31 19:33:04 +1000 | [diff] [blame] | 139 | info->type = port_type; | 
|  | 140 | info->line = ret; | 
| Greg Kroah-Hartman | 3cf62a5 | 2009-05-04 12:40:54 -0700 | [diff] [blame] | 141 | dev_set_drvdata(&ofdev->dev, info); | 
| Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 142 | return 0; | 
|  | 143 | out: | 
| Ishizaki Kou | e34b9c9 | 2007-05-31 19:33:04 +1000 | [diff] [blame] | 144 | kfree(info); | 
| Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 145 | irq_dispose_mapping(port.irq); | 
|  | 146 | return ret; | 
|  | 147 | } | 
|  | 148 |  | 
|  | 149 | /* | 
|  | 150 | * Release a line | 
|  | 151 | */ | 
| Grant Likely | 2dc1158 | 2010-08-06 09:25:50 -0600 | [diff] [blame] | 152 | static int of_platform_serial_remove(struct platform_device *ofdev) | 
| Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 153 | { | 
| Greg Kroah-Hartman | 3cf62a5 | 2009-05-04 12:40:54 -0700 | [diff] [blame] | 154 | struct of_serial_info *info = dev_get_drvdata(&ofdev->dev); | 
| Ishizaki Kou | e34b9c9 | 2007-05-31 19:33:04 +1000 | [diff] [blame] | 155 | switch (info->type) { | 
| Benjamin Krill | 5886188 | 2009-01-07 10:32:38 +0100 | [diff] [blame] | 156 | #ifdef CONFIG_SERIAL_8250 | 
| Ishizaki Kou | e34b9c9 | 2007-05-31 19:33:04 +1000 | [diff] [blame] | 157 | case PORT_8250 ... PORT_MAX_8250: | 
|  | 158 | serial8250_unregister_port(info->line); | 
|  | 159 | break; | 
| Benjamin Krill | 5886188 | 2009-01-07 10:32:38 +0100 | [diff] [blame] | 160 | #endif | 
|  | 161 | #ifdef CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL | 
|  | 162 | case PORT_NWPSERIAL: | 
|  | 163 | nwpserial_unregister_port(info->line); | 
|  | 164 | break; | 
|  | 165 | #endif | 
| Ishizaki Kou | e34b9c9 | 2007-05-31 19:33:04 +1000 | [diff] [blame] | 166 | default: | 
|  | 167 | /* need to add code for these */ | 
|  | 168 | break; | 
|  | 169 | } | 
|  | 170 | kfree(info); | 
| Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 171 | return 0; | 
|  | 172 | } | 
|  | 173 |  | 
|  | 174 | /* | 
|  | 175 | * A few common types, add more as needed. | 
|  | 176 | */ | 
|  | 177 | static struct of_device_id __devinitdata of_platform_serial_table[] = { | 
| Grant Likely | 8c6e911 | 2011-02-22 19:12:21 -0700 | [diff] [blame] | 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, }, | 
| Grant Likely | 2e39e5b | 2011-07-05 23:42:36 -0600 | [diff] [blame] | 184 | { .compatible = "nvidia,tegra20-uart", .data = (void *)PORT_TEGRA, }, | 
| Benjamin Krill | 5886188 | 2009-01-07 10:32:38 +0100 | [diff] [blame] | 185 | #ifdef CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL | 
| Grant Likely | 8c6e911 | 2011-02-22 19:12:21 -0700 | [diff] [blame] | 186 | { .compatible = "ibm,qpace-nwp-serial", | 
|  | 187 | .data = (void *)PORT_NWPSERIAL, }, | 
| Benjamin Krill | 5886188 | 2009-01-07 10:32:38 +0100 | [diff] [blame] | 188 | #endif | 
| Grant Likely | 8c6e911 | 2011-02-22 19:12:21 -0700 | [diff] [blame] | 189 | { .type = "serial",         .data = (void *)PORT_UNKNOWN, }, | 
| Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 190 | { /* end of list */ }, | 
|  | 191 | }; | 
|  | 192 |  | 
| Grant Likely | 793218d | 2011-02-22 21:10:26 -0700 | [diff] [blame] | 193 | static struct platform_driver of_platform_serial_driver = { | 
| Grant Likely | 4018294 | 2010-04-13 16:13:02 -0700 | [diff] [blame] | 194 | .driver = { | 
|  | 195 | .name = "of_serial", | 
|  | 196 | .owner = THIS_MODULE, | 
|  | 197 | .of_match_table = of_platform_serial_table, | 
|  | 198 | }, | 
| Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 199 | .probe = of_platform_serial_probe, | 
|  | 200 | .remove = of_platform_serial_remove, | 
| Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 201 | }; | 
|  | 202 |  | 
| Grant Likely | 940ab88 | 2011-10-05 11:29:49 -0600 | [diff] [blame] | 203 | module_platform_driver(of_platform_serial_driver); | 
| Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 204 |  | 
|  | 205 | MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>"); | 
|  | 206 | MODULE_LICENSE("GPL"); | 
|  | 207 | MODULE_DESCRIPTION("Serial Port driver for Open Firmware platform devices"); |