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