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>
7 #include <linux/of_address.h>
8 #include <linux/of_device.h>
9 #include <linux/serial_reg.h>
13 #include <asm/serial.h>
15 #include <asm/pci-bridge.h>
16 #include <asm/ppc-pci.h>
21 #define DBG(fmt...) do { printk(fmt); } while(0)
23 #define DBG(fmt...) do { } while(0)
26 #define MAX_LEGACY_SERIAL_PORTS 8
28 static struct plat_serial8250_port
29 legacy_serial_ports[MAX_LEGACY_SERIAL_PORTS+1];
30 static struct legacy_serial_info {
31 struct device_node *np;
36 } legacy_serial_infos[MAX_LEGACY_SERIAL_PORTS];
38 static struct __initdata of_device_id legacy_serial_parents[] = {
40 {.type = "tsi-bridge",},
42 {.compatible = "ibm,opb",},
43 {.compatible = "simple-bus",},
44 {.compatible = "wrs,epld-localbus",},
48 static unsigned int legacy_serial_count;
49 static int legacy_serial_console = -1;
51 static const upf_t legacy_port_flags = UPF_BOOT_AUTOCONF | UPF_SKIP_TEST |
52 UPF_SHARE_IRQ | UPF_FIXED_PORT;
54 static unsigned int tsi_serial_in(struct uart_port *p, int offset)
57 offset = offset << p->regshift;
58 if (offset == UART_IIR) {
59 tmp = readl(p->membase + (UART_IIR & ~3));
60 return (tmp >> 16) & 0xff; /* UART_IIR % 4 == 2 */
62 return readb(p->membase + offset);
65 static void tsi_serial_out(struct uart_port *p, int offset, int value)
67 offset = offset << p->regshift;
68 if (!((offset == UART_IER) && (value & UART_IER_UUE)))
69 writeb(value, p->membase + offset);
72 static int __init add_legacy_port(struct device_node *np, int want_index,
73 int iotype, phys_addr_t base,
74 phys_addr_t taddr, unsigned long irq,
75 upf_t flags, int irq_check_parent)
77 const __be32 *clk, *spd;
78 u32 clock = BASE_BAUD * 16;
81 /* get clock freq. if present */
82 clk = of_get_property(np, "clock-frequency", NULL);
84 clock = be32_to_cpup(clk);
86 /* get default speed if present */
87 spd = of_get_property(np, "current-speed", NULL);
89 /* If we have a location index, then try to use it */
90 if (want_index >= 0 && want_index < MAX_LEGACY_SERIAL_PORTS)
93 index = legacy_serial_count;
95 /* if our index is still out of range, that mean that
96 * array is full, we could scan for a free slot but that
97 * make little sense to bother, just skip the port
99 if (index >= MAX_LEGACY_SERIAL_PORTS)
101 if (index >= legacy_serial_count)
102 legacy_serial_count = index + 1;
104 /* Check if there is a port who already claimed our slot */
105 if (legacy_serial_infos[index].np != 0) {
106 /* if we still have some room, move it, else override */
107 if (legacy_serial_count < MAX_LEGACY_SERIAL_PORTS) {
108 printk(KERN_DEBUG "Moved legacy port %d -> %d\n",
109 index, legacy_serial_count);
110 legacy_serial_ports[legacy_serial_count] =
111 legacy_serial_ports[index];
112 legacy_serial_infos[legacy_serial_count] =
113 legacy_serial_infos[index];
114 legacy_serial_count++;
116 printk(KERN_DEBUG "Replacing legacy port %d\n", index);
120 /* Now fill the entry */
121 memset(&legacy_serial_ports[index], 0,
122 sizeof(struct plat_serial8250_port));
123 if (iotype == UPIO_PORT)
124 legacy_serial_ports[index].iobase = base;
126 legacy_serial_ports[index].mapbase = base;
128 legacy_serial_ports[index].iotype = iotype;
129 legacy_serial_ports[index].uartclk = clock;
130 legacy_serial_ports[index].irq = irq;
131 legacy_serial_ports[index].flags = flags;
132 legacy_serial_infos[index].taddr = taddr;
133 legacy_serial_infos[index].np = of_node_get(np);
134 legacy_serial_infos[index].clock = clock;
135 legacy_serial_infos[index].speed = spd ? be32_to_cpup(spd) : 0;
136 legacy_serial_infos[index].irq_check_parent = irq_check_parent;
138 if (iotype == UPIO_TSI) {
139 legacy_serial_ports[index].serial_in = tsi_serial_in;
140 legacy_serial_ports[index].serial_out = tsi_serial_out;
143 printk(KERN_DEBUG "Found legacy serial port %d for %s\n",
144 index, np->full_name);
145 printk(KERN_DEBUG " %s=%llx, taddr=%llx, irq=%lx, clk=%d, speed=%d\n",
146 (iotype == UPIO_PORT) ? "port" : "mem",
147 (unsigned long long)base, (unsigned long long)taddr, irq,
148 legacy_serial_ports[index].uartclk,
149 legacy_serial_infos[index].speed);
154 static int __init add_legacy_soc_port(struct device_node *np,
155 struct device_node *soc_dev)
159 struct device_node *tsi = of_get_parent(np);
161 /* We only support ports that have a clock frequency properly
162 * encoded in the device-tree.
164 if (of_get_property(np, "clock-frequency", NULL) == NULL)
167 /* if reg-shift or offset, don't try to use it */
168 if ((of_get_property(np, "reg-shift", NULL) != NULL) ||
169 (of_get_property(np, "reg-offset", NULL) != NULL))
172 /* if rtas uses this device, don't try to use it as well */
173 if (of_get_property(np, "used-by-rtas", NULL) != NULL)
176 /* Get the address */
177 addrp = of_get_address(soc_dev, 0, NULL, NULL);
181 addr = of_translate_address(soc_dev, addrp);
182 if (addr == OF_BAD_ADDR)
185 /* Add port, irq will be dealt with later. We passed a translated
186 * IO port value. It will be fixed up later along with the irq
188 if (tsi && !strcmp(tsi->type, "tsi-bridge"))
189 return add_legacy_port(np, -1, UPIO_TSI, addr, addr,
190 NO_IRQ, legacy_port_flags, 0);
192 return add_legacy_port(np, -1, UPIO_MEM, addr, addr,
193 NO_IRQ, legacy_port_flags, 0);
196 static int __init add_legacy_isa_port(struct device_node *np,
197 struct device_node *isa_brg)
204 DBG(" -> add_legacy_isa_port(%s)\n", np->full_name);
206 /* Get the ISA port number */
207 reg = of_get_property(np, "reg", NULL);
211 /* Verify it's an IO port, we don't support anything else */
212 if (!(be32_to_cpu(reg[0]) & 0x00000001))
215 /* Now look for an "ibm,aix-loc" property that gives us ordering
218 typep = of_get_property(np, "ibm,aix-loc", NULL);
220 /* If we have a location index, then use it */
221 if (typep && *typep == 'S')
222 index = simple_strtol(typep+1, NULL, 0) - 1;
224 /* Translate ISA address. If it fails, we still register the port
225 * with no translated address so that it can be picked up as an IO
226 * port later by the serial driver
228 taddr = of_translate_address(np, reg);
229 if (taddr == OF_BAD_ADDR)
232 /* Add port, irq will be dealt with later */
233 return add_legacy_port(np, index, UPIO_PORT, be32_to_cpu(reg[1]), taddr,
234 NO_IRQ, legacy_port_flags, 0);
239 static int __init add_legacy_pci_port(struct device_node *np,
240 struct device_node *pci_dev)
245 int iotype, index = -1, lindex = 0;
247 DBG(" -> add_legacy_pci_port(%s)\n", np->full_name);
249 /* We only support ports that have a clock frequency properly
250 * encoded in the device-tree (that is have an fcode). Anything
251 * else can't be used that early and will be normally probed by
252 * the generic 8250_pci driver later on. The reason is that 8250
253 * compatible UARTs on PCI need all sort of quirks (port offsets
254 * etc...) that this code doesn't know about
256 if (of_get_property(np, "clock-frequency", NULL) == NULL)
259 /* Get the PCI address. Assume BAR 0 */
260 addrp = of_get_pci_address(pci_dev, 0, NULL, &flags);
264 /* We only support BAR 0 for now */
265 iotype = (flags & IORESOURCE_MEM) ? UPIO_MEM : UPIO_PORT;
266 addr = of_translate_address(pci_dev, addrp);
267 if (addr == OF_BAD_ADDR)
270 /* Set the IO base to the same as the translated address for MMIO,
271 * or to the domain local IO base for PIO (it will be fixed up later)
273 if (iotype == UPIO_MEM)
278 /* Try to guess an index... If we have subdevices of the pci dev,
279 * we get to their "reg" property
282 const __be32 *reg = of_get_property(np, "reg", NULL);
283 if (reg && (be32_to_cpup(reg) < 4))
284 index = lindex = be32_to_cpup(reg);
287 /* Local index means it's the Nth port in the PCI chip. Unfortunately
288 * the offset to add here is device specific. We know about those
289 * EXAR ports and we default to the most common case. If your UART
290 * doesn't work for these settings, you'll have to add your own special
293 if (of_device_is_compatible(pci_dev, "pci13a8,152") ||
294 of_device_is_compatible(pci_dev, "pci13a8,154") ||
295 of_device_is_compatible(pci_dev, "pci13a8,158")) {
296 addr += 0x200 * lindex;
297 base += 0x200 * lindex;
303 /* Add port, irq will be dealt with later. We passed a translated
304 * IO port value. It will be fixed up later along with the irq
306 return add_legacy_port(np, index, iotype, base, addr, NO_IRQ,
307 legacy_port_flags, np != pci_dev);
311 static void __init setup_legacy_serial_console(int console)
313 struct legacy_serial_info *info =
314 &legacy_serial_infos[console];
317 if (info->taddr == 0)
319 addr = ioremap(info->taddr, 0x1000);
322 if (info->speed == 0)
323 info->speed = udbg_probe_uart_speed(addr, info->clock);
324 DBG("default console speed = %d\n", info->speed);
325 udbg_init_uart(addr, info->speed, info->clock);
329 * This is called very early, as part of setup_system() or eventually
330 * setup_arch(), basically before anything else in this file. This function
331 * will try to build a list of all the available 8250-compatible serial ports
332 * in the machine using the Open Firmware device-tree. It currently only deals
333 * with ISA and PCI busses but could be extended. It allows a very early boot
334 * console to be initialized, that list is also used later to provide 8250 with
335 * the machine non-PCI ports and to properly pick the default console port
337 void __init find_legacy_serial_ports(void)
339 struct device_node *np, *stdout = NULL;
343 DBG(" -> find_legacy_serial_port()\n");
345 /* Now find out if one of these is out firmware console */
346 path = of_get_property(of_chosen, "linux,stdout-path", NULL);
348 stdout = of_find_node_by_path(path);
350 DBG("stdout is %s\n", stdout->full_name);
352 DBG(" no linux,stdout-path !\n");
355 /* Iterate over all the 16550 ports, looking for known parents */
356 for_each_compatible_node(np, "serial", "ns16550") {
357 struct device_node *parent = of_get_parent(np);
360 if (of_match_node(legacy_serial_parents, parent) != NULL) {
361 if (of_device_is_available(np)) {
362 index = add_legacy_soc_port(np, np);
363 if (index >= 0 && np == stdout)
364 legacy_serial_console = index;
370 /* Next, fill our array with ISA ports */
371 for_each_node_by_type(np, "serial") {
372 struct device_node *isa = of_get_parent(np);
373 if (isa && !strcmp(isa->name, "isa")) {
374 index = add_legacy_isa_port(np, isa);
375 if (index >= 0 && np == stdout)
376 legacy_serial_console = index;
382 /* Next, try to locate PCI ports */
383 for (np = NULL; (np = of_find_all_nodes(np));) {
384 struct device_node *pci, *parent = of_get_parent(np);
385 if (parent && !strcmp(parent->name, "isa")) {
389 if (strcmp(np->name, "serial") && strcmp(np->type, "serial")) {
393 /* Check for known pciclass, and also check wether we have
394 * a device with child nodes for ports or not
396 if (of_device_is_compatible(np, "pciclass,0700") ||
397 of_device_is_compatible(np, "pciclass,070002"))
399 else if (of_device_is_compatible(parent, "pciclass,0700") ||
400 of_device_is_compatible(parent, "pciclass,070002"))
406 index = add_legacy_pci_port(np, pci);
407 if (index >= 0 && np == stdout)
408 legacy_serial_console = index;
413 DBG("legacy_serial_console = %d\n", legacy_serial_console);
414 if (legacy_serial_console >= 0)
415 setup_legacy_serial_console(legacy_serial_console);
416 DBG(" <- find_legacy_serial_port()\n");
419 static struct platform_device serial_device = {
420 .name = "serial8250",
421 .id = PLAT8250_DEV_PLATFORM,
423 .platform_data = legacy_serial_ports,
427 static void __init fixup_port_irq(int index,
428 struct device_node *np,
429 struct plat_serial8250_port *port)
433 DBG("fixup_port_irq(%d)\n", index);
435 virq = irq_of_parse_and_map(np, 0);
436 if (virq == NO_IRQ && legacy_serial_infos[index].irq_check_parent) {
437 np = of_get_parent(np);
440 virq = irq_of_parse_and_map(np, 0);
449 static void __init fixup_port_pio(int index,
450 struct device_node *np,
451 struct plat_serial8250_port *port)
454 struct pci_controller *hose;
456 DBG("fixup_port_pio(%d)\n", index);
458 hose = pci_find_hose_for_OF_device(np);
460 unsigned long offset = (unsigned long)hose->io_base_virt -
466 DBG("port %d, IO %lx -> %lx\n",
467 index, port->iobase, port->iobase + offset);
468 port->iobase += offset;
473 static void __init fixup_port_mmio(int index,
474 struct device_node *np,
475 struct plat_serial8250_port *port)
477 DBG("fixup_port_mmio(%d)\n", index);
479 port->membase = ioremap(port->mapbase, 0x100);
483 * This is called as an arch initcall, hopefully before the PCI bus is
484 * probed and/or the 8250 driver loaded since we need to register our
485 * platform devices before 8250 PCI ones are detected as some of them
486 * must properly "override" the platform ones.
488 * This function fixes up the interrupt value for platform ports as it
489 * couldn't be done earlier before interrupt maps have been parsed. It
490 * also "corrects" the IO address for PIO ports for the same reason,
491 * since earlier, the PHBs virtual IO space wasn't assigned yet. It then
492 * registers all those platform ports for use by the 8250 driver when it
495 static int __init serial_dev_init(void)
499 if (legacy_serial_count == 0)
503 * Before we register the platform serial devices, we need
504 * to fixup their interrupts and their IO ports.
506 DBG("Fixing serial ports interrupts and IO ports ...\n");
508 for (i = 0; i < legacy_serial_count; i++) {
509 struct plat_serial8250_port *port = &legacy_serial_ports[i];
510 struct device_node *np = legacy_serial_infos[i].np;
512 if (port->irq == NO_IRQ)
513 fixup_port_irq(i, np, port);
514 if (port->iotype == UPIO_PORT)
515 fixup_port_pio(i, np, port);
516 if ((port->iotype == UPIO_MEM) || (port->iotype == UPIO_TSI))
517 fixup_port_mmio(i, np, port);
520 DBG("Registering platform serial ports\n");
522 return platform_device_register(&serial_device);
524 device_initcall(serial_dev_init);
527 #ifdef CONFIG_SERIAL_8250_CONSOLE
529 * This is called very early, as part of console_init() (typically just after
530 * time_init()). This function is respondible for trying to find a good
531 * default console on serial ports. It tries to match the open firmware
532 * default output with one of the available serial console drivers that have
533 * been probed earlier by find_legacy_serial_ports()
535 static int __init check_legacy_serial_console(void)
537 struct device_node *prom_stdout = NULL;
538 int i, speed = 0, offset = 0;
542 DBG(" -> check_legacy_serial_console()\n");
544 /* The user has requested a console so this is already set up. */
545 if (strstr(boot_command_line, "console=")) {
546 DBG(" console was specified !\n");
551 DBG(" of_chosen is NULL !\n");
555 if (legacy_serial_console < 0) {
556 DBG(" legacy_serial_console not found !\n");
559 /* We are getting a weird phandle from OF ... */
560 /* ... So use the full path instead */
561 name = of_get_property(of_chosen, "linux,stdout-path", NULL);
563 DBG(" no linux,stdout-path !\n");
566 prom_stdout = of_find_node_by_path(name);
568 DBG(" can't find stdout package %s !\n", name);
571 DBG("stdout is %s\n", prom_stdout->full_name);
573 name = of_get_property(prom_stdout, "name", NULL);
575 DBG(" stdout package has no name !\n");
578 spd = of_get_property(prom_stdout, "current-speed", NULL);
580 speed = be32_to_cpup(spd);
582 if (strcmp(name, "serial") != 0)
585 /* Look for it in probed array */
586 for (i = 0; i < legacy_serial_count; i++) {
587 if (prom_stdout != legacy_serial_infos[i].np)
590 speed = legacy_serial_infos[i].speed;
593 if (i >= legacy_serial_count)
596 of_node_put(prom_stdout);
598 DBG("Found serial console at ttyS%d\n", offset);
601 static char __initdata opt[16];
602 sprintf(opt, "%d", speed);
603 return add_preferred_console("ttyS", offset, opt);
605 return add_preferred_console("ttyS", offset, NULL);
608 DBG("No preferred console found !\n");
609 of_node_put(prom_stdout);
612 console_initcall(check_legacy_serial_console);
614 #endif /* CONFIG_SERIAL_8250_CONSOLE */