x86/efi: Enforce CONFIG_RELOCATABLE for EFI boot stub
[linux/fpc-iii.git] / arch / powerpc / kernel / legacy_serial.c
bloba531358f971e9638f512230524538778e6bd68d8
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_address.h>
8 #include <linux/of_device.h>
9 #include <linux/serial_reg.h>
10 #include <asm/io.h>
11 #include <asm/mmu.h>
12 #include <asm/prom.h>
13 #include <asm/serial.h>
14 #include <asm/udbg.h>
15 #include <asm/pci-bridge.h>
16 #include <asm/ppc-pci.h>
18 #undef DEBUG
20 #ifdef DEBUG
21 #define DBG(fmt...) do { printk(fmt); } while(0)
22 #else
23 #define DBG(fmt...) do { } while(0)
24 #endif
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;
32 unsigned int speed;
33 unsigned int clock;
34 int irq_check_parent;
35 phys_addr_t taddr;
36 } legacy_serial_infos[MAX_LEGACY_SERIAL_PORTS];
38 static struct __initdata of_device_id legacy_serial_parents[] = {
39 {.type = "soc",},
40 {.type = "tsi-bridge",},
41 {.type = "opb", },
42 {.compatible = "ibm,opb",},
43 {.compatible = "simple-bus",},
44 {.compatible = "wrs,epld-localbus",},
45 {},
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)
56 unsigned int tmp;
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 */
61 } else
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;
79 int index;
81 /* get clock freq. if present */
82 clk = of_get_property(np, "clock-frequency", NULL);
83 if (clk && *clk)
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)
91 index = want_index;
92 else
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)
100 return -1;
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 != NULL) {
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++;
115 } else {
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;
125 else
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);
151 return index;
154 static int __init add_legacy_soc_port(struct device_node *np,
155 struct device_node *soc_dev)
157 u64 addr;
158 const __be32 *addrp;
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)
165 return -1;
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))
170 return -1;
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)
174 return -1;
176 /* Get the address */
177 addrp = of_get_address(soc_dev, 0, NULL, NULL);
178 if (addrp == NULL)
179 return -1;
181 addr = of_translate_address(soc_dev, addrp);
182 if (addr == OF_BAD_ADDR)
183 return -1;
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);
191 else
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)
199 const __be32 *reg;
200 const char *typep;
201 int index = -1;
202 u64 taddr;
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);
208 if (reg == NULL)
209 return -1;
211 /* Verify it's an IO port, we don't support anything else */
212 if (!(be32_to_cpu(reg[0]) & 0x00000001))
213 return -1;
215 /* Now look for an "ibm,aix-loc" property that gives us ordering
216 * if any...
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 * Note: Don't even try on P8 lpc, we know it's not directly mapped
230 if (!of_device_is_compatible(isa_brg, "ibm,power8-lpc")) {
231 taddr = of_translate_address(np, reg);
232 if (taddr == OF_BAD_ADDR)
233 taddr = 0;
234 } else
235 taddr = 0;
237 /* Add port, irq will be dealt with later */
238 return add_legacy_port(np, index, UPIO_PORT, be32_to_cpu(reg[1]),
239 taddr, NO_IRQ, legacy_port_flags, 0);
243 #ifdef CONFIG_PCI
244 static int __init add_legacy_pci_port(struct device_node *np,
245 struct device_node *pci_dev)
247 u64 addr, base;
248 const __be32 *addrp;
249 unsigned int flags;
250 int iotype, index = -1, lindex = 0;
252 DBG(" -> add_legacy_pci_port(%s)\n", np->full_name);
254 /* We only support ports that have a clock frequency properly
255 * encoded in the device-tree (that is have an fcode). Anything
256 * else can't be used that early and will be normally probed by
257 * the generic 8250_pci driver later on. The reason is that 8250
258 * compatible UARTs on PCI need all sort of quirks (port offsets
259 * etc...) that this code doesn't know about
261 if (of_get_property(np, "clock-frequency", NULL) == NULL)
262 return -1;
264 /* Get the PCI address. Assume BAR 0 */
265 addrp = of_get_pci_address(pci_dev, 0, NULL, &flags);
266 if (addrp == NULL)
267 return -1;
269 /* We only support BAR 0 for now */
270 iotype = (flags & IORESOURCE_MEM) ? UPIO_MEM : UPIO_PORT;
271 addr = of_translate_address(pci_dev, addrp);
272 if (addr == OF_BAD_ADDR)
273 return -1;
275 /* Set the IO base to the same as the translated address for MMIO,
276 * or to the domain local IO base for PIO (it will be fixed up later)
278 if (iotype == UPIO_MEM)
279 base = addr;
280 else
281 base = of_read_number(&addrp[2], 1);
283 /* Try to guess an index... If we have subdevices of the pci dev,
284 * we get to their "reg" property
286 if (np != pci_dev) {
287 const __be32 *reg = of_get_property(np, "reg", NULL);
288 if (reg && (be32_to_cpup(reg) < 4))
289 index = lindex = be32_to_cpup(reg);
292 /* Local index means it's the Nth port in the PCI chip. Unfortunately
293 * the offset to add here is device specific. We know about those
294 * EXAR ports and we default to the most common case. If your UART
295 * doesn't work for these settings, you'll have to add your own special
296 * cases here
298 if (of_device_is_compatible(pci_dev, "pci13a8,152") ||
299 of_device_is_compatible(pci_dev, "pci13a8,154") ||
300 of_device_is_compatible(pci_dev, "pci13a8,158")) {
301 addr += 0x200 * lindex;
302 base += 0x200 * lindex;
303 } else {
304 addr += 8 * lindex;
305 base += 8 * lindex;
308 /* Add port, irq will be dealt with later. We passed a translated
309 * IO port value. It will be fixed up later along with the irq
311 return add_legacy_port(np, index, iotype, base, addr, NO_IRQ,
312 legacy_port_flags, np != pci_dev);
314 #endif
316 static void __init setup_legacy_serial_console(int console)
318 struct legacy_serial_info *info = &legacy_serial_infos[console];
319 struct plat_serial8250_port *port = &legacy_serial_ports[console];
320 void __iomem *addr;
322 /* Check if a translated MMIO address has been found */
323 if (info->taddr) {
324 addr = ioremap(info->taddr, 0x1000);
325 if (addr == NULL)
326 return;
327 udbg_uart_init_mmio(addr, 1);
328 } else {
329 /* Check if it's PIO and we support untranslated PIO */
330 if (port->iotype == UPIO_PORT && isa_io_special)
331 udbg_uart_init_pio(port->iobase, 1);
332 else
333 return;
336 /* Try to query the current speed */
337 if (info->speed == 0)
338 info->speed = udbg_probe_uart_speed(info->clock);
340 /* Set it up */
341 DBG("default console speed = %d\n", info->speed);
342 udbg_uart_setup(info->speed, info->clock);
346 * This is called very early, as part of setup_system() or eventually
347 * setup_arch(), basically before anything else in this file. This function
348 * will try to build a list of all the available 8250-compatible serial ports
349 * in the machine using the Open Firmware device-tree. It currently only deals
350 * with ISA and PCI busses but could be extended. It allows a very early boot
351 * console to be initialized, that list is also used later to provide 8250 with
352 * the machine non-PCI ports and to properly pick the default console port
354 void __init find_legacy_serial_ports(void)
356 struct device_node *np, *stdout = NULL;
357 const char *path;
358 int index;
360 DBG(" -> find_legacy_serial_port()\n");
362 /* Now find out if one of these is out firmware console */
363 path = of_get_property(of_chosen, "linux,stdout-path", NULL);
364 if (path != NULL) {
365 stdout = of_find_node_by_path(path);
366 if (stdout)
367 DBG("stdout is %s\n", stdout->full_name);
368 } else {
369 DBG(" no linux,stdout-path !\n");
372 /* Iterate over all the 16550 ports, looking for known parents */
373 for_each_compatible_node(np, "serial", "ns16550") {
374 struct device_node *parent = of_get_parent(np);
375 if (!parent)
376 continue;
377 if (of_match_node(legacy_serial_parents, parent) != NULL) {
378 if (of_device_is_available(np)) {
379 index = add_legacy_soc_port(np, np);
380 if (index >= 0 && np == stdout)
381 legacy_serial_console = index;
384 of_node_put(parent);
387 /* Next, fill our array with ISA ports */
388 for_each_node_by_type(np, "serial") {
389 struct device_node *isa = of_get_parent(np);
390 if (isa && (!strcmp(isa->name, "isa") ||
391 !strcmp(isa->name, "lpc"))) {
392 if (of_device_is_available(np)) {
393 index = add_legacy_isa_port(np, isa);
394 if (index >= 0 && np == stdout)
395 legacy_serial_console = index;
398 of_node_put(isa);
401 #ifdef CONFIG_PCI
402 /* Next, try to locate PCI ports */
403 for (np = NULL; (np = of_find_all_nodes(np));) {
404 struct device_node *pci, *parent = of_get_parent(np);
405 if (parent && !strcmp(parent->name, "isa")) {
406 of_node_put(parent);
407 continue;
409 if (strcmp(np->name, "serial") && strcmp(np->type, "serial")) {
410 of_node_put(parent);
411 continue;
413 /* Check for known pciclass, and also check whether we have
414 * a device with child nodes for ports or not
416 if (of_device_is_compatible(np, "pciclass,0700") ||
417 of_device_is_compatible(np, "pciclass,070002"))
418 pci = np;
419 else if (of_device_is_compatible(parent, "pciclass,0700") ||
420 of_device_is_compatible(parent, "pciclass,070002"))
421 pci = parent;
422 else {
423 of_node_put(parent);
424 continue;
426 index = add_legacy_pci_port(np, pci);
427 if (index >= 0 && np == stdout)
428 legacy_serial_console = index;
429 of_node_put(parent);
431 #endif
433 DBG("legacy_serial_console = %d\n", legacy_serial_console);
434 if (legacy_serial_console >= 0)
435 setup_legacy_serial_console(legacy_serial_console);
436 DBG(" <- find_legacy_serial_port()\n");
439 static struct platform_device serial_device = {
440 .name = "serial8250",
441 .id = PLAT8250_DEV_PLATFORM,
442 .dev = {
443 .platform_data = legacy_serial_ports,
447 static void __init fixup_port_irq(int index,
448 struct device_node *np,
449 struct plat_serial8250_port *port)
451 unsigned int virq;
453 DBG("fixup_port_irq(%d)\n", index);
455 virq = irq_of_parse_and_map(np, 0);
456 if (virq == NO_IRQ && legacy_serial_infos[index].irq_check_parent) {
457 np = of_get_parent(np);
458 if (np == NULL)
459 return;
460 virq = irq_of_parse_and_map(np, 0);
461 of_node_put(np);
463 if (virq == NO_IRQ)
464 return;
466 port->irq = virq;
468 #ifdef CONFIG_SERIAL_8250_FSL
469 if (of_device_is_compatible(np, "fsl,ns16550"))
470 port->handle_irq = fsl8250_handle_irq;
471 #endif
474 static void __init fixup_port_pio(int index,
475 struct device_node *np,
476 struct plat_serial8250_port *port)
478 #ifdef CONFIG_PCI
479 struct pci_controller *hose;
481 DBG("fixup_port_pio(%d)\n", index);
483 hose = pci_find_hose_for_OF_device(np);
484 if (hose) {
485 unsigned long offset = (unsigned long)hose->io_base_virt -
486 #ifdef CONFIG_PPC64
487 pci_io_base;
488 #else
489 isa_io_base;
490 #endif
491 DBG("port %d, IO %lx -> %lx\n",
492 index, port->iobase, port->iobase + offset);
493 port->iobase += offset;
495 #endif
498 static void __init fixup_port_mmio(int index,
499 struct device_node *np,
500 struct plat_serial8250_port *port)
502 DBG("fixup_port_mmio(%d)\n", index);
504 port->membase = ioremap(port->mapbase, 0x100);
508 * This is called as an arch initcall, hopefully before the PCI bus is
509 * probed and/or the 8250 driver loaded since we need to register our
510 * platform devices before 8250 PCI ones are detected as some of them
511 * must properly "override" the platform ones.
513 * This function fixes up the interrupt value for platform ports as it
514 * couldn't be done earlier before interrupt maps have been parsed. It
515 * also "corrects" the IO address for PIO ports for the same reason,
516 * since earlier, the PHBs virtual IO space wasn't assigned yet. It then
517 * registers all those platform ports for use by the 8250 driver when it
518 * finally loads.
520 static int __init serial_dev_init(void)
522 int i;
524 if (legacy_serial_count == 0)
525 return -ENODEV;
528 * Before we register the platform serial devices, we need
529 * to fixup their interrupts and their IO ports.
531 DBG("Fixing serial ports interrupts and IO ports ...\n");
533 for (i = 0; i < legacy_serial_count; i++) {
534 struct plat_serial8250_port *port = &legacy_serial_ports[i];
535 struct device_node *np = legacy_serial_infos[i].np;
537 if (port->irq == NO_IRQ)
538 fixup_port_irq(i, np, port);
539 if (port->iotype == UPIO_PORT)
540 fixup_port_pio(i, np, port);
541 if ((port->iotype == UPIO_MEM) || (port->iotype == UPIO_TSI))
542 fixup_port_mmio(i, np, port);
545 DBG("Registering platform serial ports\n");
547 return platform_device_register(&serial_device);
549 device_initcall(serial_dev_init);
552 #ifdef CONFIG_SERIAL_8250_CONSOLE
554 * This is called very early, as part of console_init() (typically just after
555 * time_init()). This function is respondible for trying to find a good
556 * default console on serial ports. It tries to match the open firmware
557 * default output with one of the available serial console drivers that have
558 * been probed earlier by find_legacy_serial_ports()
560 static int __init check_legacy_serial_console(void)
562 struct device_node *prom_stdout = NULL;
563 int i, speed = 0, offset = 0;
564 const char *name;
565 const __be32 *spd;
567 DBG(" -> check_legacy_serial_console()\n");
569 /* The user has requested a console so this is already set up. */
570 if (strstr(boot_command_line, "console=")) {
571 DBG(" console was specified !\n");
572 return -EBUSY;
575 if (!of_chosen) {
576 DBG(" of_chosen is NULL !\n");
577 return -ENODEV;
580 if (legacy_serial_console < 0) {
581 DBG(" legacy_serial_console not found !\n");
582 return -ENODEV;
584 /* We are getting a weird phandle from OF ... */
585 /* ... So use the full path instead */
586 name = of_get_property(of_chosen, "linux,stdout-path", NULL);
587 if (name == NULL) {
588 DBG(" no linux,stdout-path !\n");
589 return -ENODEV;
591 prom_stdout = of_find_node_by_path(name);
592 if (!prom_stdout) {
593 DBG(" can't find stdout package %s !\n", name);
594 return -ENODEV;
596 DBG("stdout is %s\n", prom_stdout->full_name);
598 name = of_get_property(prom_stdout, "name", NULL);
599 if (!name) {
600 DBG(" stdout package has no name !\n");
601 goto not_found;
603 spd = of_get_property(prom_stdout, "current-speed", NULL);
604 if (spd)
605 speed = be32_to_cpup(spd);
607 if (strcmp(name, "serial") != 0)
608 goto not_found;
610 /* Look for it in probed array */
611 for (i = 0; i < legacy_serial_count; i++) {
612 if (prom_stdout != legacy_serial_infos[i].np)
613 continue;
614 offset = i;
615 speed = legacy_serial_infos[i].speed;
616 break;
618 if (i >= legacy_serial_count)
619 goto not_found;
621 of_node_put(prom_stdout);
623 DBG("Found serial console at ttyS%d\n", offset);
625 if (speed) {
626 static char __initdata opt[16];
627 sprintf(opt, "%d", speed);
628 return add_preferred_console("ttyS", offset, opt);
629 } else
630 return add_preferred_console("ttyS", offset, NULL);
632 not_found:
633 DBG("No preferred console found !\n");
634 of_node_put(prom_stdout);
635 return -ENODEV;
637 console_initcall(check_legacy_serial_console);
639 #endif /* CONFIG_SERIAL_8250_CONSOLE */