Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 1 | #include <linux/kernel.h> |
| 2 | #include <linux/serial.h> |
| 3 | #include <linux/serial_8250.h> |
| 4 | #include <linux/serial_core.h> |
| 5 | #include <linux/console.h> |
| 6 | #include <linux/pci.h> |
Grant Likely | 22ae782 | 2010-07-29 11:49:01 -0600 | [diff] [blame] | 7 | #include <linux/of_address.h> |
Paul Gortmaker | 1a7507c | 2008-01-24 11:59:12 -0500 | [diff] [blame] | 8 | #include <linux/of_device.h> |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 9 | #include <asm/io.h> |
| 10 | #include <asm/mmu.h> |
| 11 | #include <asm/prom.h> |
| 12 | #include <asm/serial.h> |
| 13 | #include <asm/udbg.h> |
| 14 | #include <asm/pci-bridge.h> |
| 15 | #include <asm/ppc-pci.h> |
| 16 | |
| 17 | #undef DEBUG |
| 18 | |
| 19 | #ifdef DEBUG |
| 20 | #define DBG(fmt...) do { printk(fmt); } while(0) |
| 21 | #else |
| 22 | #define DBG(fmt...) do { } while(0) |
| 23 | #endif |
| 24 | |
| 25 | #define MAX_LEGACY_SERIAL_PORTS 8 |
| 26 | |
| 27 | static struct plat_serial8250_port |
| 28 | legacy_serial_ports[MAX_LEGACY_SERIAL_PORTS+1]; |
| 29 | static struct legacy_serial_info { |
| 30 | struct device_node *np; |
| 31 | unsigned int speed; |
| 32 | unsigned int clock; |
Benjamin Herrenschmidt | 0ebfff1 | 2006-07-03 21:36:01 +1000 | [diff] [blame] | 33 | int irq_check_parent; |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 34 | phys_addr_t taddr; |
| 35 | } legacy_serial_infos[MAX_LEGACY_SERIAL_PORTS]; |
Paul Gortmaker | 1a7507c | 2008-01-24 11:59:12 -0500 | [diff] [blame] | 36 | |
Benjamin Herrenschmidt | 3bc5ab9 | 2008-07-07 16:39:50 +1000 | [diff] [blame] | 37 | static struct __initdata of_device_id legacy_serial_parents[] = { |
Paul Gortmaker | 1a7507c | 2008-01-24 11:59:12 -0500 | [diff] [blame] | 38 | {.type = "soc",}, |
| 39 | {.type = "tsi-bridge",}, |
Michael Ellerman | f5903ed | 2008-02-05 23:01:50 +1100 | [diff] [blame] | 40 | {.type = "opb", }, |
| 41 | {.compatible = "ibm,opb",}, |
Paul Gortmaker | 1a7507c | 2008-01-24 11:59:12 -0500 | [diff] [blame] | 42 | {.compatible = "simple-bus",}, |
| 43 | {.compatible = "wrs,epld-localbus",}, |
Benjamin Herrenschmidt | 3bc5ab9 | 2008-07-07 16:39:50 +1000 | [diff] [blame] | 44 | {}, |
Paul Gortmaker | 1a7507c | 2008-01-24 11:59:12 -0500 | [diff] [blame] | 45 | }; |
| 46 | |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 47 | static unsigned int legacy_serial_count; |
| 48 | static int legacy_serial_console = -1; |
| 49 | |
| 50 | static int __init add_legacy_port(struct device_node *np, int want_index, |
| 51 | int iotype, phys_addr_t base, |
Kumar Gala | b580d46 | 2005-12-20 16:16:52 -0600 | [diff] [blame] | 52 | phys_addr_t taddr, unsigned long irq, |
Benjamin Herrenschmidt | 0ebfff1 | 2006-07-03 21:36:01 +1000 | [diff] [blame] | 53 | upf_t flags, int irq_check_parent) |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 54 | { |
Jeremy Kerr | a7f67bd | 2006-07-12 15:35:54 +1000 | [diff] [blame] | 55 | const u32 *clk, *spd; |
| 56 | u32 clock = BASE_BAUD * 16; |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 57 | int index; |
| 58 | |
| 59 | /* get clock freq. if present */ |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 60 | clk = of_get_property(np, "clock-frequency", NULL); |
David Woodhouse | 31df167 | 2005-11-24 17:08:56 +0000 | [diff] [blame] | 61 | if (clk && *clk) |
| 62 | clock = *clk; |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 63 | |
| 64 | /* get default speed if present */ |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 65 | spd = of_get_property(np, "current-speed", NULL); |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 66 | |
| 67 | /* If we have a location index, then try to use it */ |
| 68 | if (want_index >= 0 && want_index < MAX_LEGACY_SERIAL_PORTS) |
| 69 | index = want_index; |
| 70 | else |
| 71 | index = legacy_serial_count; |
| 72 | |
| 73 | /* if our index is still out of range, that mean that |
| 74 | * array is full, we could scan for a free slot but that |
| 75 | * make little sense to bother, just skip the port |
| 76 | */ |
| 77 | if (index >= MAX_LEGACY_SERIAL_PORTS) |
| 78 | return -1; |
| 79 | if (index >= legacy_serial_count) |
| 80 | legacy_serial_count = index + 1; |
| 81 | |
| 82 | /* Check if there is a port who already claimed our slot */ |
| 83 | if (legacy_serial_infos[index].np != 0) { |
| 84 | /* if we still have some room, move it, else override */ |
| 85 | if (legacy_serial_count < MAX_LEGACY_SERIAL_PORTS) { |
Benjamin Herrenschmidt | 0ebfff1 | 2006-07-03 21:36:01 +1000 | [diff] [blame] | 86 | printk(KERN_DEBUG "Moved legacy port %d -> %d\n", |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 87 | index, legacy_serial_count); |
| 88 | legacy_serial_ports[legacy_serial_count] = |
| 89 | legacy_serial_ports[index]; |
| 90 | legacy_serial_infos[legacy_serial_count] = |
| 91 | legacy_serial_infos[index]; |
| 92 | legacy_serial_count++; |
| 93 | } else { |
Benjamin Herrenschmidt | 0ebfff1 | 2006-07-03 21:36:01 +1000 | [diff] [blame] | 94 | printk(KERN_DEBUG "Replacing legacy port %d\n", index); |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |
| 98 | /* Now fill the entry */ |
| 99 | memset(&legacy_serial_ports[index], 0, |
| 100 | sizeof(struct plat_serial8250_port)); |
| 101 | if (iotype == UPIO_PORT) |
| 102 | legacy_serial_ports[index].iobase = base; |
| 103 | else |
Benjamin Herrenschmidt | 8dacaed | 2005-11-29 11:21:59 +1100 | [diff] [blame] | 104 | legacy_serial_ports[index].mapbase = base; |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 105 | legacy_serial_ports[index].iotype = iotype; |
| 106 | legacy_serial_ports[index].uartclk = clock; |
| 107 | legacy_serial_ports[index].irq = irq; |
Kumar Gala | b580d46 | 2005-12-20 16:16:52 -0600 | [diff] [blame] | 108 | legacy_serial_ports[index].flags = flags; |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 109 | legacy_serial_infos[index].taddr = taddr; |
| 110 | legacy_serial_infos[index].np = of_node_get(np); |
| 111 | legacy_serial_infos[index].clock = clock; |
| 112 | legacy_serial_infos[index].speed = spd ? *spd : 0; |
Benjamin Herrenschmidt | 0ebfff1 | 2006-07-03 21:36:01 +1000 | [diff] [blame] | 113 | legacy_serial_infos[index].irq_check_parent = irq_check_parent; |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 114 | |
Benjamin Herrenschmidt | 0ebfff1 | 2006-07-03 21:36:01 +1000 | [diff] [blame] | 115 | printk(KERN_DEBUG "Found legacy serial port %d for %s\n", |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 116 | index, np->full_name); |
Benjamin Herrenschmidt | 0ebfff1 | 2006-07-03 21:36:01 +1000 | [diff] [blame] | 117 | printk(KERN_DEBUG " %s=%llx, taddr=%llx, irq=%lx, clk=%d, speed=%d\n", |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 118 | (iotype == UPIO_PORT) ? "port" : "mem", |
| 119 | (unsigned long long)base, (unsigned long long)taddr, irq, |
| 120 | legacy_serial_ports[index].uartclk, |
| 121 | legacy_serial_infos[index].speed); |
| 122 | |
| 123 | return index; |
| 124 | } |
| 125 | |
Kumar Gala | b580d46 | 2005-12-20 16:16:52 -0600 | [diff] [blame] | 126 | static int __init add_legacy_soc_port(struct device_node *np, |
| 127 | struct device_node *soc_dev) |
| 128 | { |
Benjamin Herrenschmidt | f704b8d | 2006-07-04 14:14:07 +1000 | [diff] [blame] | 129 | u64 addr; |
Jeremy Kerr | a7f67bd | 2006-07-12 15:35:54 +1000 | [diff] [blame] | 130 | const u32 *addrp; |
David Gibson | abb4a23 | 2007-05-06 14:48:49 -0700 | [diff] [blame] | 131 | upf_t flags = UPF_BOOT_AUTOCONF | UPF_SKIP_TEST | UPF_SHARE_IRQ |
| 132 | | UPF_FIXED_PORT; |
Zang Roy-r61911 | be9633e | 2006-08-23 10:20:27 +0800 | [diff] [blame] | 133 | struct device_node *tsi = of_get_parent(np); |
Kumar Gala | b580d46 | 2005-12-20 16:16:52 -0600 | [diff] [blame] | 134 | |
| 135 | /* We only support ports that have a clock frequency properly |
| 136 | * encoded in the device-tree. |
| 137 | */ |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 138 | if (of_get_property(np, "clock-frequency", NULL) == NULL) |
Kumar Gala | b580d46 | 2005-12-20 16:16:52 -0600 | [diff] [blame] | 139 | return -1; |
| 140 | |
John Linn | 1e6d1f2 | 2008-07-01 10:52:41 -0700 | [diff] [blame] | 141 | /* if reg-shift or offset, don't try to use it */ |
| 142 | if ((of_get_property(np, "reg-shift", NULL) != NULL) || |
| 143 | (of_get_property(np, "reg-offset", NULL) != NULL)) |
| 144 | return -1; |
| 145 | |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 146 | /* if rtas uses this device, don't try to use it as well */ |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 147 | if (of_get_property(np, "used-by-rtas", NULL) != NULL) |
Arnd Bergmann | 8d38a5b | 2007-02-13 21:35:38 +0100 | [diff] [blame] | 148 | return -1; |
| 149 | |
Kumar Gala | b580d46 | 2005-12-20 16:16:52 -0600 | [diff] [blame] | 150 | /* Get the address */ |
| 151 | addrp = of_get_address(soc_dev, 0, NULL, NULL); |
| 152 | if (addrp == NULL) |
| 153 | return -1; |
| 154 | |
| 155 | addr = of_translate_address(soc_dev, addrp); |
Benjamin Herrenschmidt | 7c6efda | 2006-07-03 17:24:15 +1000 | [diff] [blame] | 156 | if (addr == OF_BAD_ADDR) |
| 157 | return -1; |
Kumar Gala | b580d46 | 2005-12-20 16:16:52 -0600 | [diff] [blame] | 158 | |
| 159 | /* Add port, irq will be dealt with later. We passed a translated |
| 160 | * IO port value. It will be fixed up later along with the irq |
| 161 | */ |
Zang Roy-r61911 | be9633e | 2006-08-23 10:20:27 +0800 | [diff] [blame] | 162 | if (tsi && !strcmp(tsi->type, "tsi-bridge")) |
| 163 | return add_legacy_port(np, -1, UPIO_TSI, addr, addr, NO_IRQ, flags, 0); |
| 164 | else |
| 165 | return add_legacy_port(np, -1, UPIO_MEM, addr, addr, NO_IRQ, flags, 0); |
Kumar Gala | b580d46 | 2005-12-20 16:16:52 -0600 | [diff] [blame] | 166 | } |
| 167 | |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 168 | static int __init add_legacy_isa_port(struct device_node *np, |
Kumar Gala | 017e0fa | 2006-01-03 16:15:21 -0600 | [diff] [blame] | 169 | struct device_node *isa_brg) |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 170 | { |
Jeremy Kerr | a7f67bd | 2006-07-12 15:35:54 +1000 | [diff] [blame] | 171 | const u32 *reg; |
| 172 | const char *typep; |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 173 | int index = -1; |
Benjamin Herrenschmidt | f704b8d | 2006-07-04 14:14:07 +1000 | [diff] [blame] | 174 | u64 taddr; |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 175 | |
Benjamin Herrenschmidt | 7c6efda | 2006-07-03 17:24:15 +1000 | [diff] [blame] | 176 | DBG(" -> add_legacy_isa_port(%s)\n", np->full_name); |
| 177 | |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 178 | /* Get the ISA port number */ |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 179 | reg = of_get_property(np, "reg", NULL); |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 180 | if (reg == NULL) |
| 181 | return -1; |
| 182 | |
| 183 | /* Verify it's an IO port, we don't support anything else */ |
| 184 | if (!(reg[0] & 0x00000001)) |
| 185 | return -1; |
| 186 | |
| 187 | /* Now look for an "ibm,aix-loc" property that gives us ordering |
| 188 | * if any... |
| 189 | */ |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 190 | typep = of_get_property(np, "ibm,aix-loc", NULL); |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 191 | |
| 192 | /* If we have a location index, then use it */ |
| 193 | if (typep && *typep == 'S') |
| 194 | index = simple_strtol(typep+1, NULL, 0) - 1; |
| 195 | |
Benjamin Herrenschmidt | f704b8d | 2006-07-04 14:14:07 +1000 | [diff] [blame] | 196 | /* Translate ISA address. If it fails, we still register the port |
| 197 | * with no translated address so that it can be picked up as an IO |
| 198 | * port later by the serial driver |
| 199 | */ |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 200 | taddr = of_translate_address(np, reg); |
Benjamin Herrenschmidt | 7c6efda | 2006-07-03 17:24:15 +1000 | [diff] [blame] | 201 | if (taddr == OF_BAD_ADDR) |
Benjamin Herrenschmidt | f704b8d | 2006-07-04 14:14:07 +1000 | [diff] [blame] | 202 | taddr = 0; |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 203 | |
| 204 | /* Add port, irq will be dealt with later */ |
Benjamin Herrenschmidt | 7c6efda | 2006-07-03 17:24:15 +1000 | [diff] [blame] | 205 | return add_legacy_port(np, index, UPIO_PORT, reg[1], taddr, |
Benjamin Herrenschmidt | 0ebfff1 | 2006-07-03 21:36:01 +1000 | [diff] [blame] | 206 | NO_IRQ, UPF_BOOT_AUTOCONF, 0); |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 207 | |
| 208 | } |
| 209 | |
Kumar Gala | 017e0fa | 2006-01-03 16:15:21 -0600 | [diff] [blame] | 210 | #ifdef CONFIG_PCI |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 211 | static int __init add_legacy_pci_port(struct device_node *np, |
| 212 | struct device_node *pci_dev) |
| 213 | { |
Benjamin Herrenschmidt | f704b8d | 2006-07-04 14:14:07 +1000 | [diff] [blame] | 214 | u64 addr, base; |
Jeremy Kerr | a7f67bd | 2006-07-12 15:35:54 +1000 | [diff] [blame] | 215 | const u32 *addrp; |
Benjamin Herrenschmidt | d2dd482 | 2005-11-30 16:57:28 +1100 | [diff] [blame] | 216 | unsigned int flags; |
Benjamin Herrenschmidt | 8dacaed | 2005-11-29 11:21:59 +1100 | [diff] [blame] | 217 | int iotype, index = -1, lindex = 0; |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 218 | |
Benjamin Herrenschmidt | 7c6efda | 2006-07-03 17:24:15 +1000 | [diff] [blame] | 219 | DBG(" -> add_legacy_pci_port(%s)\n", np->full_name); |
| 220 | |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 221 | /* We only support ports that have a clock frequency properly |
| 222 | * encoded in the device-tree (that is have an fcode). Anything |
| 223 | * else can't be used that early and will be normally probed by |
Benjamin Herrenschmidt | 8dacaed | 2005-11-29 11:21:59 +1100 | [diff] [blame] | 224 | * the generic 8250_pci driver later on. The reason is that 8250 |
| 225 | * compatible UARTs on PCI need all sort of quirks (port offsets |
| 226 | * etc...) that this code doesn't know about |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 227 | */ |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 228 | if (of_get_property(np, "clock-frequency", NULL) == NULL) |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 229 | return -1; |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 230 | |
| 231 | /* Get the PCI address. Assume BAR 0 */ |
Benjamin Herrenschmidt | d2dd482 | 2005-11-30 16:57:28 +1100 | [diff] [blame] | 232 | addrp = of_get_pci_address(pci_dev, 0, NULL, &flags); |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 233 | if (addrp == NULL) |
| 234 | return -1; |
| 235 | |
| 236 | /* We only support BAR 0 for now */ |
Benjamin Herrenschmidt | d2dd482 | 2005-11-30 16:57:28 +1100 | [diff] [blame] | 237 | iotype = (flags & IORESOURCE_MEM) ? UPIO_MEM : UPIO_PORT; |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 238 | addr = of_translate_address(pci_dev, addrp); |
Benjamin Herrenschmidt | 7c6efda | 2006-07-03 17:24:15 +1000 | [diff] [blame] | 239 | if (addr == OF_BAD_ADDR) |
| 240 | return -1; |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 241 | |
| 242 | /* Set the IO base to the same as the translated address for MMIO, |
| 243 | * or to the domain local IO base for PIO (it will be fixed up later) |
| 244 | */ |
| 245 | if (iotype == UPIO_MEM) |
| 246 | base = addr; |
| 247 | else |
| 248 | base = addrp[2]; |
| 249 | |
| 250 | /* Try to guess an index... If we have subdevices of the pci dev, |
| 251 | * we get to their "reg" property |
| 252 | */ |
| 253 | if (np != pci_dev) { |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 254 | const u32 *reg = of_get_property(np, "reg", NULL); |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 255 | if (reg && (*reg < 4)) |
Benjamin Herrenschmidt | 8dacaed | 2005-11-29 11:21:59 +1100 | [diff] [blame] | 256 | index = lindex = *reg; |
| 257 | } |
| 258 | |
| 259 | /* Local index means it's the Nth port in the PCI chip. Unfortunately |
| 260 | * the offset to add here is device specific. We know about those |
| 261 | * EXAR ports and we default to the most common case. If your UART |
| 262 | * doesn't work for these settings, you'll have to add your own special |
| 263 | * cases here |
| 264 | */ |
Stephen Rothwell | 55b61fe | 2007-05-03 17:26:52 +1000 | [diff] [blame] | 265 | if (of_device_is_compatible(pci_dev, "pci13a8,152") || |
| 266 | of_device_is_compatible(pci_dev, "pci13a8,154") || |
| 267 | of_device_is_compatible(pci_dev, "pci13a8,158")) { |
Benjamin Herrenschmidt | 8dacaed | 2005-11-29 11:21:59 +1100 | [diff] [blame] | 268 | addr += 0x200 * lindex; |
| 269 | base += 0x200 * lindex; |
| 270 | } else { |
| 271 | addr += 8 * lindex; |
| 272 | base += 8 * lindex; |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | /* Add port, irq will be dealt with later. We passed a translated |
| 276 | * IO port value. It will be fixed up later along with the irq |
| 277 | */ |
Benjamin Herrenschmidt | 0ebfff1 | 2006-07-03 21:36:01 +1000 | [diff] [blame] | 278 | return add_legacy_port(np, index, iotype, base, addr, NO_IRQ, |
| 279 | UPF_BOOT_AUTOCONF, np != pci_dev); |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 280 | } |
Kumar Gala | 017e0fa | 2006-01-03 16:15:21 -0600 | [diff] [blame] | 281 | #endif |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 282 | |
Michael Neuling | 37a801c | 2006-03-26 10:07:35 +1000 | [diff] [blame] | 283 | static void __init setup_legacy_serial_console(int console) |
| 284 | { |
| 285 | struct legacy_serial_info *info = |
| 286 | &legacy_serial_infos[console]; |
| 287 | void __iomem *addr; |
| 288 | |
| 289 | if (info->taddr == 0) |
| 290 | return; |
| 291 | addr = ioremap(info->taddr, 0x1000); |
| 292 | if (addr == NULL) |
| 293 | return; |
| 294 | if (info->speed == 0) |
| 295 | info->speed = udbg_probe_uart_speed(addr, info->clock); |
| 296 | DBG("default console speed = %d\n", info->speed); |
| 297 | udbg_init_uart(addr, info->speed, info->clock); |
| 298 | } |
| 299 | |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 300 | /* |
| 301 | * This is called very early, as part of setup_system() or eventually |
| 302 | * setup_arch(), basically before anything else in this file. This function |
| 303 | * will try to build a list of all the available 8250-compatible serial ports |
| 304 | * in the machine using the Open Firmware device-tree. It currently only deals |
| 305 | * with ISA and PCI busses but could be extended. It allows a very early boot |
| 306 | * console to be initialized, that list is also used later to provide 8250 with |
| 307 | * the machine non-PCI ports and to properly pick the default console port |
| 308 | */ |
| 309 | void __init find_legacy_serial_ports(void) |
| 310 | { |
Kumar Gala | b580d46 | 2005-12-20 16:16:52 -0600 | [diff] [blame] | 311 | struct device_node *np, *stdout = NULL; |
Jeremy Kerr | a7f67bd | 2006-07-12 15:35:54 +1000 | [diff] [blame] | 312 | const char *path; |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 313 | int index; |
| 314 | |
| 315 | DBG(" -> find_legacy_serial_port()\n"); |
| 316 | |
| 317 | /* Now find out if one of these is out firmware console */ |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 318 | path = of_get_property(of_chosen, "linux,stdout-path", NULL); |
Kumar Gala | b580d46 | 2005-12-20 16:16:52 -0600 | [diff] [blame] | 319 | if (path != NULL) { |
| 320 | stdout = of_find_node_by_path(path); |
| 321 | if (stdout) |
| 322 | DBG("stdout is %s\n", stdout->full_name); |
| 323 | } else { |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 324 | DBG(" no linux,stdout-path !\n"); |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 325 | } |
Kumar Gala | b580d46 | 2005-12-20 16:16:52 -0600 | [diff] [blame] | 326 | |
Paul Gortmaker | 1a7507c | 2008-01-24 11:59:12 -0500 | [diff] [blame] | 327 | /* Iterate over all the 16550 ports, looking for known parents */ |
Cyrill Gorcunov | 3329c0d | 2007-11-30 06:45:47 +1100 | [diff] [blame] | 328 | for_each_compatible_node(np, "serial", "ns16550") { |
Paul Gortmaker | 1a7507c | 2008-01-24 11:59:12 -0500 | [diff] [blame] | 329 | struct device_node *parent = of_get_parent(np); |
| 330 | if (!parent) |
| 331 | continue; |
Benjamin Herrenschmidt | 3bc5ab9 | 2008-07-07 16:39:50 +1000 | [diff] [blame] | 332 | if (of_match_node(legacy_serial_parents, parent) != NULL) { |
Kumar Gala | b580d46 | 2005-12-20 16:16:52 -0600 | [diff] [blame] | 333 | index = add_legacy_soc_port(np, np); |
| 334 | if (index >= 0 && np == stdout) |
| 335 | legacy_serial_console = index; |
| 336 | } |
Paul Gortmaker | 1a7507c | 2008-01-24 11:59:12 -0500 | [diff] [blame] | 337 | of_node_put(parent); |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 338 | } |
| 339 | |
Paul Gortmaker | 1a7507c | 2008-01-24 11:59:12 -0500 | [diff] [blame] | 340 | /* Next, fill our array with ISA ports */ |
Cyrill Gorcunov | 3329c0d | 2007-11-30 06:45:47 +1100 | [diff] [blame] | 341 | for_each_node_by_type(np, "serial") { |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 342 | struct device_node *isa = of_get_parent(np); |
| 343 | if (isa && !strcmp(isa->name, "isa")) { |
| 344 | index = add_legacy_isa_port(np, isa); |
| 345 | if (index >= 0 && np == stdout) |
| 346 | legacy_serial_console = index; |
| 347 | } |
| 348 | of_node_put(isa); |
| 349 | } |
| 350 | |
Kumar Gala | 017e0fa | 2006-01-03 16:15:21 -0600 | [diff] [blame] | 351 | #ifdef CONFIG_PCI |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 352 | /* Next, try to locate PCI ports */ |
| 353 | for (np = NULL; (np = of_find_all_nodes(np));) { |
| 354 | struct device_node *pci, *parent = of_get_parent(np); |
| 355 | if (parent && !strcmp(parent->name, "isa")) { |
| 356 | of_node_put(parent); |
| 357 | continue; |
| 358 | } |
| 359 | if (strcmp(np->name, "serial") && strcmp(np->type, "serial")) { |
| 360 | of_node_put(parent); |
| 361 | continue; |
| 362 | } |
| 363 | /* Check for known pciclass, and also check wether we have |
| 364 | * a device with child nodes for ports or not |
| 365 | */ |
Stephen Rothwell | 55b61fe | 2007-05-03 17:26:52 +1000 | [diff] [blame] | 366 | if (of_device_is_compatible(np, "pciclass,0700") || |
| 367 | of_device_is_compatible(np, "pciclass,070002")) |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 368 | pci = np; |
Stephen Rothwell | 55b61fe | 2007-05-03 17:26:52 +1000 | [diff] [blame] | 369 | else if (of_device_is_compatible(parent, "pciclass,0700") || |
| 370 | of_device_is_compatible(parent, "pciclass,070002")) |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 371 | pci = parent; |
| 372 | else { |
| 373 | of_node_put(parent); |
| 374 | continue; |
| 375 | } |
| 376 | index = add_legacy_pci_port(np, pci); |
| 377 | if (index >= 0 && np == stdout) |
| 378 | legacy_serial_console = index; |
| 379 | of_node_put(parent); |
| 380 | } |
Kumar Gala | 017e0fa | 2006-01-03 16:15:21 -0600 | [diff] [blame] | 381 | #endif |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 382 | |
| 383 | DBG("legacy_serial_console = %d\n", legacy_serial_console); |
Michael Neuling | 37a801c | 2006-03-26 10:07:35 +1000 | [diff] [blame] | 384 | if (legacy_serial_console >= 0) |
| 385 | setup_legacy_serial_console(legacy_serial_console); |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 386 | DBG(" <- find_legacy_serial_port()\n"); |
| 387 | } |
| 388 | |
| 389 | static struct platform_device serial_device = { |
| 390 | .name = "serial8250", |
| 391 | .id = PLAT8250_DEV_PLATFORM, |
| 392 | .dev = { |
| 393 | .platform_data = legacy_serial_ports, |
| 394 | }, |
| 395 | }; |
| 396 | |
| 397 | static void __init fixup_port_irq(int index, |
| 398 | struct device_node *np, |
| 399 | struct plat_serial8250_port *port) |
| 400 | { |
Benjamin Herrenschmidt | 0ebfff1 | 2006-07-03 21:36:01 +1000 | [diff] [blame] | 401 | unsigned int virq; |
| 402 | |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 403 | DBG("fixup_port_irq(%d)\n", index); |
| 404 | |
Benjamin Herrenschmidt | 0ebfff1 | 2006-07-03 21:36:01 +1000 | [diff] [blame] | 405 | virq = irq_of_parse_and_map(np, 0); |
| 406 | if (virq == NO_IRQ && legacy_serial_infos[index].irq_check_parent) { |
| 407 | np = of_get_parent(np); |
| 408 | if (np == NULL) |
| 409 | return; |
| 410 | virq = irq_of_parse_and_map(np, 0); |
| 411 | of_node_put(np); |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 412 | } |
Benjamin Herrenschmidt | 0ebfff1 | 2006-07-03 21:36:01 +1000 | [diff] [blame] | 413 | if (virq == NO_IRQ) |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 414 | return; |
| 415 | |
Benjamin Herrenschmidt | 0ebfff1 | 2006-07-03 21:36:01 +1000 | [diff] [blame] | 416 | port->irq = virq; |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | static void __init fixup_port_pio(int index, |
| 420 | struct device_node *np, |
| 421 | struct plat_serial8250_port *port) |
| 422 | { |
Kumar Gala | 017e0fa | 2006-01-03 16:15:21 -0600 | [diff] [blame] | 423 | #ifdef CONFIG_PCI |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 424 | struct pci_controller *hose; |
| 425 | |
| 426 | DBG("fixup_port_pio(%d)\n", index); |
| 427 | |
| 428 | hose = pci_find_hose_for_OF_device(np); |
| 429 | if (hose) { |
| 430 | unsigned long offset = (unsigned long)hose->io_base_virt - |
| 431 | #ifdef CONFIG_PPC64 |
| 432 | pci_io_base; |
| 433 | #else |
| 434 | isa_io_base; |
| 435 | #endif |
| 436 | DBG("port %d, IO %lx -> %lx\n", |
| 437 | index, port->iobase, port->iobase + offset); |
| 438 | port->iobase += offset; |
| 439 | } |
Kumar Gala | 017e0fa | 2006-01-03 16:15:21 -0600 | [diff] [blame] | 440 | #endif |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 441 | } |
| 442 | |
Benjamin Herrenschmidt | 8dacaed | 2005-11-29 11:21:59 +1100 | [diff] [blame] | 443 | static void __init fixup_port_mmio(int index, |
| 444 | struct device_node *np, |
| 445 | struct plat_serial8250_port *port) |
| 446 | { |
| 447 | DBG("fixup_port_mmio(%d)\n", index); |
| 448 | |
| 449 | port->membase = ioremap(port->mapbase, 0x100); |
| 450 | } |
| 451 | |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 452 | /* |
| 453 | * This is called as an arch initcall, hopefully before the PCI bus is |
| 454 | * probed and/or the 8250 driver loaded since we need to register our |
| 455 | * platform devices before 8250 PCI ones are detected as some of them |
| 456 | * must properly "override" the platform ones. |
| 457 | * |
| 458 | * This function fixes up the interrupt value for platform ports as it |
| 459 | * couldn't be done earlier before interrupt maps have been parsed. It |
| 460 | * also "corrects" the IO address for PIO ports for the same reason, |
| 461 | * since earlier, the PHBs virtual IO space wasn't assigned yet. It then |
| 462 | * registers all those platform ports for use by the 8250 driver when it |
| 463 | * finally loads. |
| 464 | */ |
| 465 | static int __init serial_dev_init(void) |
| 466 | { |
| 467 | int i; |
| 468 | |
| 469 | if (legacy_serial_count == 0) |
| 470 | return -ENODEV; |
| 471 | |
| 472 | /* |
Stefan Weil | 947af29 | 2010-01-07 00:03:52 +0100 | [diff] [blame] | 473 | * Before we register the platform serial devices, we need |
joe@perches.com | 00d7041 | 2007-12-18 06:30:12 +1100 | [diff] [blame] | 474 | * to fixup their interrupts and their IO ports. |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 475 | */ |
| 476 | DBG("Fixing serial ports interrupts and IO ports ...\n"); |
| 477 | |
| 478 | for (i = 0; i < legacy_serial_count; i++) { |
| 479 | struct plat_serial8250_port *port = &legacy_serial_ports[i]; |
| 480 | struct device_node *np = legacy_serial_infos[i].np; |
| 481 | |
| 482 | if (port->irq == NO_IRQ) |
| 483 | fixup_port_irq(i, np, port); |
| 484 | if (port->iotype == UPIO_PORT) |
| 485 | fixup_port_pio(i, np, port); |
Zang Roy-r61911 | be9633e | 2006-08-23 10:20:27 +0800 | [diff] [blame] | 486 | if ((port->iotype == UPIO_MEM) || (port->iotype == UPIO_TSI)) |
Benjamin Herrenschmidt | 8dacaed | 2005-11-29 11:21:59 +1100 | [diff] [blame] | 487 | fixup_port_mmio(i, np, port); |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | DBG("Registering platform serial ports\n"); |
| 491 | |
| 492 | return platform_device_register(&serial_device); |
| 493 | } |
Olof Johansson | ee56c47 | 2007-08-22 19:26:37 -0500 | [diff] [blame] | 494 | device_initcall(serial_dev_init); |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 495 | |
| 496 | |
Benjamin Herrenschmidt | 025d791 | 2008-07-28 13:49:15 +1000 | [diff] [blame] | 497 | #ifdef CONFIG_SERIAL_8250_CONSOLE |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 498 | /* |
| 499 | * This is called very early, as part of console_init() (typically just after |
| 500 | * time_init()). This function is respondible for trying to find a good |
| 501 | * default console on serial ports. It tries to match the open firmware |
Benjamin Herrenschmidt | 025d791 | 2008-07-28 13:49:15 +1000 | [diff] [blame] | 502 | * default output with one of the available serial console drivers that have |
| 503 | * been probed earlier by find_legacy_serial_ports() |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 504 | */ |
| 505 | static int __init check_legacy_serial_console(void) |
| 506 | { |
| 507 | struct device_node *prom_stdout = NULL; |
Benjamin Herrenschmidt | 025d791 | 2008-07-28 13:49:15 +1000 | [diff] [blame] | 508 | int i, speed = 0, offset = 0; |
Jeremy Kerr | a7f67bd | 2006-07-12 15:35:54 +1000 | [diff] [blame] | 509 | const char *name; |
| 510 | const u32 *spd; |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 511 | |
| 512 | DBG(" -> check_legacy_serial_console()\n"); |
| 513 | |
| 514 | /* The user has requested a console so this is already set up. */ |
Alon Bar-Lev | b8757b2 | 2007-02-12 00:54:17 -0800 | [diff] [blame] | 515 | if (strstr(boot_command_line, "console=")) { |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 516 | DBG(" console was specified !\n"); |
| 517 | return -EBUSY; |
| 518 | } |
| 519 | |
| 520 | if (!of_chosen) { |
| 521 | DBG(" of_chosen is NULL !\n"); |
| 522 | return -ENODEV; |
| 523 | } |
Kumar Gala | b580d46 | 2005-12-20 16:16:52 -0600 | [diff] [blame] | 524 | |
| 525 | if (legacy_serial_console < 0) { |
| 526 | DBG(" legacy_serial_console not found !\n"); |
| 527 | return -ENODEV; |
| 528 | } |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 529 | /* We are getting a weird phandle from OF ... */ |
| 530 | /* ... So use the full path instead */ |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 531 | name = of_get_property(of_chosen, "linux,stdout-path", NULL); |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 532 | if (name == NULL) { |
| 533 | DBG(" no linux,stdout-path !\n"); |
| 534 | return -ENODEV; |
| 535 | } |
| 536 | prom_stdout = of_find_node_by_path(name); |
| 537 | if (!prom_stdout) { |
| 538 | DBG(" can't find stdout package %s !\n", name); |
| 539 | return -ENODEV; |
| 540 | } |
| 541 | DBG("stdout is %s\n", prom_stdout->full_name); |
| 542 | |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 543 | name = of_get_property(prom_stdout, "name", NULL); |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 544 | if (!name) { |
| 545 | DBG(" stdout package has no name !\n"); |
| 546 | goto not_found; |
| 547 | } |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 548 | spd = of_get_property(prom_stdout, "current-speed", NULL); |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 549 | if (spd) |
| 550 | speed = *spd; |
| 551 | |
Benjamin Herrenschmidt | 025d791 | 2008-07-28 13:49:15 +1000 | [diff] [blame] | 552 | if (strcmp(name, "serial") != 0) |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 553 | goto not_found; |
Benjamin Herrenschmidt | 025d791 | 2008-07-28 13:49:15 +1000 | [diff] [blame] | 554 | |
| 555 | /* Look for it in probed array */ |
| 556 | for (i = 0; i < legacy_serial_count; i++) { |
| 557 | if (prom_stdout != legacy_serial_infos[i].np) |
| 558 | continue; |
| 559 | offset = i; |
| 560 | speed = legacy_serial_infos[i].speed; |
| 561 | break; |
| 562 | } |
| 563 | if (i >= legacy_serial_count) |
| 564 | goto not_found; |
| 565 | |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 566 | of_node_put(prom_stdout); |
| 567 | |
| 568 | DBG("Found serial console at ttyS%d\n", offset); |
| 569 | |
| 570 | if (speed) { |
| 571 | static char __initdata opt[16]; |
| 572 | sprintf(opt, "%d", speed); |
| 573 | return add_preferred_console("ttyS", offset, opt); |
| 574 | } else |
| 575 | return add_preferred_console("ttyS", offset, NULL); |
| 576 | |
| 577 | not_found: |
| 578 | DBG("No preferred console found !\n"); |
| 579 | of_node_put(prom_stdout); |
| 580 | return -ENODEV; |
| 581 | } |
| 582 | console_initcall(check_legacy_serial_console); |
| 583 | |
Benjamin Herrenschmidt | 025d791 | 2008-07-28 13:49:15 +1000 | [diff] [blame] | 584 | #endif /* CONFIG_SERIAL_8250_CONSOLE */ |