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