0001
0002 #include <linux/kernel.h>
0003 #include <linux/serial.h>
0004 #include <linux/serial_8250.h>
0005 #include <linux/serial_core.h>
0006 #include <linux/console.h>
0007 #include <linux/pci.h>
0008 #include <linux/of_address.h>
0009 #include <linux/of_device.h>
0010 #include <linux/of_irq.h>
0011 #include <linux/serial_reg.h>
0012 #include <asm/io.h>
0013 #include <asm/mmu.h>
0014 #include <asm/serial.h>
0015 #include <asm/udbg.h>
0016 #include <asm/pci-bridge.h>
0017 #include <asm/ppc-pci.h>
0018 #include <asm/early_ioremap.h>
0019
0020 #undef DEBUG
0021
0022 #ifdef DEBUG
0023 #define DBG(fmt...) do { printk(fmt); } while(0)
0024 #else
0025 #define DBG(fmt...) do { } while(0)
0026 #endif
0027
0028 #define MAX_LEGACY_SERIAL_PORTS 8
0029
0030 static struct plat_serial8250_port
0031 legacy_serial_ports[MAX_LEGACY_SERIAL_PORTS+1];
0032 static struct legacy_serial_info {
0033 struct device_node *np;
0034 unsigned int speed;
0035 unsigned int clock;
0036 int irq_check_parent;
0037 phys_addr_t taddr;
0038 void __iomem *early_addr;
0039 } legacy_serial_infos[MAX_LEGACY_SERIAL_PORTS];
0040
0041 static const struct of_device_id legacy_serial_parents[] __initconst = {
0042 {.type = "soc",},
0043 {.type = "tsi-bridge",},
0044 {.type = "opb", },
0045 {.compatible = "ibm,opb",},
0046 {.compatible = "simple-bus",},
0047 {.compatible = "wrs,epld-localbus",},
0048 {},
0049 };
0050
0051 static unsigned int legacy_serial_count;
0052 static int legacy_serial_console = -1;
0053
0054 static const upf_t legacy_port_flags = UPF_BOOT_AUTOCONF | UPF_SKIP_TEST |
0055 UPF_SHARE_IRQ | UPF_FIXED_PORT;
0056
0057 static unsigned int tsi_serial_in(struct uart_port *p, int offset)
0058 {
0059 unsigned int tmp;
0060 offset = offset << p->regshift;
0061 if (offset == UART_IIR) {
0062 tmp = readl(p->membase + (UART_IIR & ~3));
0063 return (tmp >> 16) & 0xff;
0064 } else
0065 return readb(p->membase + offset);
0066 }
0067
0068 static void tsi_serial_out(struct uart_port *p, int offset, int value)
0069 {
0070 offset = offset << p->regshift;
0071 if (!((offset == UART_IER) && (value & UART_IER_UUE)))
0072 writeb(value, p->membase + offset);
0073 }
0074
0075 static int __init add_legacy_port(struct device_node *np, int want_index,
0076 int iotype, phys_addr_t base,
0077 phys_addr_t taddr, unsigned long irq,
0078 upf_t flags, int irq_check_parent)
0079 {
0080 const __be32 *clk, *spd, *rs;
0081 u32 clock = BASE_BAUD * 16;
0082 u32 shift = 0;
0083 int index;
0084
0085
0086 clk = of_get_property(np, "clock-frequency", NULL);
0087 if (clk && *clk)
0088 clock = be32_to_cpup(clk);
0089
0090
0091 spd = of_get_property(np, "current-speed", NULL);
0092
0093
0094 rs = of_get_property(np, "reg-shift", NULL);
0095 if (rs && *rs)
0096 shift = be32_to_cpup(rs);
0097
0098
0099 if (want_index >= 0 && want_index < MAX_LEGACY_SERIAL_PORTS)
0100 index = want_index;
0101 else
0102 index = legacy_serial_count;
0103
0104
0105
0106
0107
0108 if (index >= MAX_LEGACY_SERIAL_PORTS)
0109 return -1;
0110 if (index >= legacy_serial_count)
0111 legacy_serial_count = index + 1;
0112
0113
0114 if (legacy_serial_infos[index].np != NULL) {
0115
0116 if (legacy_serial_count < MAX_LEGACY_SERIAL_PORTS) {
0117 printk(KERN_DEBUG "Moved legacy port %d -> %d\n",
0118 index, legacy_serial_count);
0119 legacy_serial_ports[legacy_serial_count] =
0120 legacy_serial_ports[index];
0121 legacy_serial_infos[legacy_serial_count] =
0122 legacy_serial_infos[index];
0123 legacy_serial_count++;
0124 } else {
0125 printk(KERN_DEBUG "Replacing legacy port %d\n", index);
0126 }
0127 }
0128
0129
0130 memset(&legacy_serial_ports[index], 0,
0131 sizeof(struct plat_serial8250_port));
0132 if (iotype == UPIO_PORT)
0133 legacy_serial_ports[index].iobase = base;
0134 else
0135 legacy_serial_ports[index].mapbase = base;
0136
0137 legacy_serial_ports[index].iotype = iotype;
0138 legacy_serial_ports[index].uartclk = clock;
0139 legacy_serial_ports[index].irq = irq;
0140 legacy_serial_ports[index].flags = flags;
0141 legacy_serial_ports[index].regshift = shift;
0142 legacy_serial_infos[index].taddr = taddr;
0143 legacy_serial_infos[index].np = of_node_get(np);
0144 legacy_serial_infos[index].clock = clock;
0145 legacy_serial_infos[index].speed = spd ? be32_to_cpup(spd) : 0;
0146 legacy_serial_infos[index].irq_check_parent = irq_check_parent;
0147
0148 if (iotype == UPIO_TSI) {
0149 legacy_serial_ports[index].serial_in = tsi_serial_in;
0150 legacy_serial_ports[index].serial_out = tsi_serial_out;
0151 }
0152
0153 printk(KERN_DEBUG "Found legacy serial port %d for %pOF\n",
0154 index, np);
0155 printk(KERN_DEBUG " %s=%llx, taddr=%llx, irq=%lx, clk=%d, speed=%d\n",
0156 (iotype == UPIO_PORT) ? "port" : "mem",
0157 (unsigned long long)base, (unsigned long long)taddr, irq,
0158 legacy_serial_ports[index].uartclk,
0159 legacy_serial_infos[index].speed);
0160
0161 return index;
0162 }
0163
0164 static int __init add_legacy_soc_port(struct device_node *np,
0165 struct device_node *soc_dev)
0166 {
0167 u64 addr;
0168 const __be32 *addrp;
0169 struct device_node *tsi = of_get_parent(np);
0170
0171
0172
0173
0174 if (of_get_property(np, "clock-frequency", NULL) == NULL)
0175 return -1;
0176
0177
0178 if ((of_get_property(np, "reg-offset", NULL) != NULL))
0179 return -1;
0180
0181
0182 if (of_get_property(np, "used-by-rtas", NULL) != NULL)
0183 return -1;
0184
0185
0186 addrp = of_get_address(soc_dev, 0, NULL, NULL);
0187 if (addrp == NULL)
0188 return -1;
0189
0190 addr = of_translate_address(soc_dev, addrp);
0191 if (addr == OF_BAD_ADDR)
0192 return -1;
0193
0194
0195
0196
0197 if (of_node_is_type(tsi, "tsi-bridge"))
0198 return add_legacy_port(np, -1, UPIO_TSI, addr, addr,
0199 0, legacy_port_flags, 0);
0200 else
0201 return add_legacy_port(np, -1, UPIO_MEM, addr, addr,
0202 0, legacy_port_flags, 0);
0203 }
0204
0205 static int __init add_legacy_isa_port(struct device_node *np,
0206 struct device_node *isa_brg)
0207 {
0208 const __be32 *reg;
0209 const char *typep;
0210 int index = -1;
0211 u64 taddr;
0212
0213 DBG(" -> add_legacy_isa_port(%pOF)\n", np);
0214
0215
0216 reg = of_get_property(np, "reg", NULL);
0217 if (reg == NULL)
0218 return -1;
0219
0220
0221 if (!(be32_to_cpu(reg[0]) & 0x00000001))
0222 return -1;
0223
0224
0225
0226
0227 typep = of_get_property(np, "ibm,aix-loc", NULL);
0228
0229
0230 if (typep && *typep == 'S')
0231 index = simple_strtol(typep+1, NULL, 0) - 1;
0232
0233
0234
0235
0236
0237
0238
0239 if (!of_device_is_compatible(isa_brg, "ibm,power8-lpc") ||
0240 of_get_property(isa_brg, "ranges", NULL)) {
0241 taddr = of_translate_address(np, reg);
0242 if (taddr == OF_BAD_ADDR)
0243 taddr = 0;
0244 } else
0245 taddr = 0;
0246
0247
0248 return add_legacy_port(np, index, UPIO_PORT, be32_to_cpu(reg[1]),
0249 taddr, 0, legacy_port_flags, 0);
0250
0251 }
0252
0253 #ifdef CONFIG_PCI
0254 static int __init add_legacy_pci_port(struct device_node *np,
0255 struct device_node *pci_dev)
0256 {
0257 u64 addr, base;
0258 const __be32 *addrp;
0259 unsigned int flags;
0260 int iotype, index = -1, lindex = 0;
0261
0262 DBG(" -> add_legacy_pci_port(%pOF)\n", np);
0263
0264
0265
0266
0267
0268
0269
0270
0271 if (of_get_property(np, "clock-frequency", NULL) == NULL)
0272 return -1;
0273
0274
0275 addrp = of_get_pci_address(pci_dev, 0, NULL, &flags);
0276 if (addrp == NULL)
0277 return -1;
0278
0279
0280 iotype = (flags & IORESOURCE_MEM) ? UPIO_MEM : UPIO_PORT;
0281 addr = of_translate_address(pci_dev, addrp);
0282 if (addr == OF_BAD_ADDR)
0283 return -1;
0284
0285
0286
0287
0288 if (iotype == UPIO_MEM)
0289 base = addr;
0290 else
0291 base = of_read_number(&addrp[2], 1);
0292
0293
0294
0295
0296 if (np != pci_dev) {
0297 const __be32 *reg = of_get_property(np, "reg", NULL);
0298 if (reg && (be32_to_cpup(reg) < 4))
0299 index = lindex = be32_to_cpup(reg);
0300 }
0301
0302
0303
0304
0305
0306
0307
0308 if (of_device_is_compatible(pci_dev, "pci13a8,152") ||
0309 of_device_is_compatible(pci_dev, "pci13a8,154") ||
0310 of_device_is_compatible(pci_dev, "pci13a8,158")) {
0311 addr += 0x200 * lindex;
0312 base += 0x200 * lindex;
0313 } else {
0314 addr += 8 * lindex;
0315 base += 8 * lindex;
0316 }
0317
0318
0319
0320
0321 return add_legacy_port(np, index, iotype, base, addr, 0,
0322 legacy_port_flags, np != pci_dev);
0323 }
0324 #endif
0325
0326 static void __init setup_legacy_serial_console(int console)
0327 {
0328 struct legacy_serial_info *info = &legacy_serial_infos[console];
0329 struct plat_serial8250_port *port = &legacy_serial_ports[console];
0330 unsigned int stride;
0331
0332 stride = 1 << port->regshift;
0333
0334
0335 if (info->taddr) {
0336 info->early_addr = early_ioremap(info->taddr, 0x1000);
0337 if (info->early_addr == NULL)
0338 return;
0339 udbg_uart_init_mmio(info->early_addr, stride);
0340 } else {
0341
0342 if (port->iotype == UPIO_PORT && isa_io_special)
0343 udbg_uart_init_pio(port->iobase, stride);
0344 else
0345 return;
0346 }
0347
0348
0349 if (info->speed == 0)
0350 info->speed = udbg_probe_uart_speed(info->clock);
0351
0352
0353 DBG("default console speed = %d\n", info->speed);
0354 udbg_uart_setup(info->speed, info->clock);
0355 }
0356
0357 static int __init ioremap_legacy_serial_console(void)
0358 {
0359 struct plat_serial8250_port *port;
0360 struct legacy_serial_info *info;
0361 void __iomem *vaddr;
0362
0363 if (legacy_serial_console < 0)
0364 return 0;
0365
0366 info = &legacy_serial_infos[legacy_serial_console];
0367 port = &legacy_serial_ports[legacy_serial_console];
0368
0369 if (!info->early_addr)
0370 return 0;
0371
0372 vaddr = ioremap(info->taddr, 0x1000);
0373 if (WARN_ON(!vaddr))
0374 return -ENOMEM;
0375
0376 udbg_uart_init_mmio(vaddr, 1 << port->regshift);
0377 early_iounmap(info->early_addr, 0x1000);
0378 info->early_addr = NULL;
0379
0380 return 0;
0381 }
0382 early_initcall(ioremap_legacy_serial_console);
0383
0384
0385
0386
0387
0388
0389
0390
0391
0392
0393 void __init find_legacy_serial_ports(void)
0394 {
0395 struct device_node *np, *stdout = NULL;
0396 const char *path;
0397 int index;
0398
0399 DBG(" -> find_legacy_serial_port()\n");
0400
0401
0402 path = of_get_property(of_chosen, "linux,stdout-path", NULL);
0403 if (path == NULL)
0404 path = of_get_property(of_chosen, "stdout-path", NULL);
0405 if (path != NULL) {
0406 stdout = of_find_node_by_path(path);
0407 if (stdout)
0408 DBG("stdout is %pOF\n", stdout);
0409 } else {
0410 DBG(" no linux,stdout-path !\n");
0411 }
0412
0413
0414 for_each_compatible_node(np, "serial", "ns16550") {
0415 struct device_node *parent = of_get_parent(np);
0416 if (!parent)
0417 continue;
0418 if (of_match_node(legacy_serial_parents, parent) != NULL) {
0419 if (of_device_is_available(np)) {
0420 index = add_legacy_soc_port(np, np);
0421 if (index >= 0 && np == stdout)
0422 legacy_serial_console = index;
0423 }
0424 }
0425 of_node_put(parent);
0426 }
0427
0428
0429 for_each_node_by_type(np, "serial") {
0430 struct device_node *isa = of_get_parent(np);
0431 if (of_node_name_eq(isa, "isa") || of_node_name_eq(isa, "lpc")) {
0432 if (of_device_is_available(np)) {
0433 index = add_legacy_isa_port(np, isa);
0434 if (index >= 0 && np == stdout)
0435 legacy_serial_console = index;
0436 }
0437 }
0438 of_node_put(isa);
0439 }
0440
0441 #ifdef CONFIG_PCI
0442
0443 for (np = NULL; (np = of_find_all_nodes(np));) {
0444 struct device_node *pci, *parent = of_get_parent(np);
0445 if (of_node_name_eq(parent, "isa")) {
0446 of_node_put(parent);
0447 continue;
0448 }
0449 if (!of_node_name_eq(np, "serial") &&
0450 !of_node_is_type(np, "serial")) {
0451 of_node_put(parent);
0452 continue;
0453 }
0454
0455
0456
0457 if (of_device_is_compatible(np, "pciclass,0700") ||
0458 of_device_is_compatible(np, "pciclass,070002"))
0459 pci = np;
0460 else if (of_device_is_compatible(parent, "pciclass,0700") ||
0461 of_device_is_compatible(parent, "pciclass,070002"))
0462 pci = parent;
0463 else {
0464 of_node_put(parent);
0465 continue;
0466 }
0467 index = add_legacy_pci_port(np, pci);
0468 if (index >= 0 && np == stdout)
0469 legacy_serial_console = index;
0470 of_node_put(parent);
0471 }
0472 #endif
0473
0474 DBG("legacy_serial_console = %d\n", legacy_serial_console);
0475 if (legacy_serial_console >= 0)
0476 setup_legacy_serial_console(legacy_serial_console);
0477 DBG(" <- find_legacy_serial_port()\n");
0478 }
0479
0480 static struct platform_device serial_device = {
0481 .name = "serial8250",
0482 .id = PLAT8250_DEV_PLATFORM,
0483 .dev = {
0484 .platform_data = legacy_serial_ports,
0485 },
0486 };
0487
0488 static void __init fixup_port_irq(int index,
0489 struct device_node *np,
0490 struct plat_serial8250_port *port)
0491 {
0492 unsigned int virq;
0493
0494 DBG("fixup_port_irq(%d)\n", index);
0495
0496 virq = irq_of_parse_and_map(np, 0);
0497 if (!virq && legacy_serial_infos[index].irq_check_parent) {
0498 np = of_get_parent(np);
0499 if (np == NULL)
0500 return;
0501 virq = irq_of_parse_and_map(np, 0);
0502 of_node_put(np);
0503 }
0504 if (!virq)
0505 return;
0506
0507 port->irq = virq;
0508
0509 #ifdef CONFIG_SERIAL_8250_FSL
0510 if (of_device_is_compatible(np, "fsl,ns16550")) {
0511 port->handle_irq = fsl8250_handle_irq;
0512 port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_8250_CONSOLE);
0513 }
0514 #endif
0515 }
0516
0517 static void __init fixup_port_pio(int index,
0518 struct device_node *np,
0519 struct plat_serial8250_port *port)
0520 {
0521 #ifdef CONFIG_PCI
0522 struct pci_controller *hose;
0523
0524 DBG("fixup_port_pio(%d)\n", index);
0525
0526 hose = pci_find_hose_for_OF_device(np);
0527 if (hose) {
0528 unsigned long offset = (unsigned long)hose->io_base_virt -
0529 #ifdef CONFIG_PPC64
0530 pci_io_base;
0531 #else
0532 isa_io_base;
0533 #endif
0534 DBG("port %d, IO %lx -> %lx\n",
0535 index, port->iobase, port->iobase + offset);
0536 port->iobase += offset;
0537 }
0538 #endif
0539 }
0540
0541 static void __init fixup_port_mmio(int index,
0542 struct device_node *np,
0543 struct plat_serial8250_port *port)
0544 {
0545 DBG("fixup_port_mmio(%d)\n", index);
0546
0547 port->membase = ioremap(port->mapbase, 0x100);
0548 }
0549
0550
0551
0552
0553
0554
0555
0556
0557
0558
0559
0560
0561
0562
0563 static int __init serial_dev_init(void)
0564 {
0565 int i;
0566
0567 if (legacy_serial_count == 0)
0568 return -ENODEV;
0569
0570
0571
0572
0573
0574 DBG("Fixing serial ports interrupts and IO ports ...\n");
0575
0576 for (i = 0; i < legacy_serial_count; i++) {
0577 struct plat_serial8250_port *port = &legacy_serial_ports[i];
0578 struct device_node *np = legacy_serial_infos[i].np;
0579
0580 if (!port->irq)
0581 fixup_port_irq(i, np, port);
0582 if (port->iotype == UPIO_PORT)
0583 fixup_port_pio(i, np, port);
0584 if ((port->iotype == UPIO_MEM) || (port->iotype == UPIO_TSI))
0585 fixup_port_mmio(i, np, port);
0586 }
0587
0588 DBG("Registering platform serial ports\n");
0589
0590 return platform_device_register(&serial_device);
0591 }
0592 device_initcall(serial_dev_init);
0593
0594
0595 #ifdef CONFIG_SERIAL_8250_CONSOLE
0596
0597
0598
0599
0600
0601
0602
0603 static int __init check_legacy_serial_console(void)
0604 {
0605 struct device_node *prom_stdout = NULL;
0606 int i, speed = 0, offset = 0;
0607 const char *name;
0608 const __be32 *spd;
0609
0610 DBG(" -> check_legacy_serial_console()\n");
0611
0612
0613 if (strstr(boot_command_line, "console=")) {
0614 DBG(" console was specified !\n");
0615 return -EBUSY;
0616 }
0617
0618 if (!of_chosen) {
0619 DBG(" of_chosen is NULL !\n");
0620 return -ENODEV;
0621 }
0622
0623 if (legacy_serial_console < 0) {
0624 DBG(" legacy_serial_console not found !\n");
0625 return -ENODEV;
0626 }
0627
0628
0629 name = of_get_property(of_chosen, "linux,stdout-path", NULL);
0630 if (name == NULL)
0631 name = of_get_property(of_chosen, "stdout-path", NULL);
0632 if (name == NULL) {
0633 DBG(" no stdout-path !\n");
0634 return -ENODEV;
0635 }
0636 prom_stdout = of_find_node_by_path(name);
0637 if (!prom_stdout) {
0638 DBG(" can't find stdout package %s !\n", name);
0639 return -ENODEV;
0640 }
0641 DBG("stdout is %pOF\n", prom_stdout);
0642
0643 name = of_get_property(prom_stdout, "name", NULL);
0644 if (!name) {
0645 DBG(" stdout package has no name !\n");
0646 goto not_found;
0647 }
0648 spd = of_get_property(prom_stdout, "current-speed", NULL);
0649 if (spd)
0650 speed = be32_to_cpup(spd);
0651
0652 if (strcmp(name, "serial") != 0)
0653 goto not_found;
0654
0655
0656 for (i = 0; i < legacy_serial_count; i++) {
0657 if (prom_stdout != legacy_serial_infos[i].np)
0658 continue;
0659 offset = i;
0660 speed = legacy_serial_infos[i].speed;
0661 break;
0662 }
0663 if (i >= legacy_serial_count)
0664 goto not_found;
0665
0666 of_node_put(prom_stdout);
0667
0668 DBG("Found serial console at ttyS%d\n", offset);
0669
0670 if (speed) {
0671 static char __initdata opt[16];
0672 sprintf(opt, "%d", speed);
0673 return add_preferred_console("ttyS", offset, opt);
0674 } else
0675 return add_preferred_console("ttyS", offset, NULL);
0676
0677 not_found:
0678 DBG("No preferred console found !\n");
0679 of_node_put(prom_stdout);
0680 return -ENODEV;
0681 }
0682 console_initcall(check_legacy_serial_console);
0683
0684 #endif