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