pandora: defconfig: update
[pandora-kernel.git] / arch / powerpc / kernel / legacy_serial.c
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>
17
18 #undef DEBUG
19
20 #ifdef DEBUG
21 #define DBG(fmt...) do { printk(fmt); } while(0)
22 #else
23 #define DBG(fmt...) do { } while(0)
24 #endif
25
26 #define MAX_LEGACY_SERIAL_PORTS 8
27
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];
37
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         {},
46 };
47
48 static unsigned int legacy_serial_count;
49 static int legacy_serial_console = -1;
50
51 static const upf_t legacy_port_flags = UPF_BOOT_AUTOCONF | UPF_SKIP_TEST |
52         UPF_SHARE_IRQ | UPF_FIXED_PORT;
53
54 static unsigned int tsi_serial_in(struct uart_port *p, int offset)
55 {
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);
63 }
64
65 static void tsi_serial_out(struct uart_port *p, int offset, int value)
66 {
67         offset = offset << p->regshift;
68         if (!((offset == UART_IER) && (value & UART_IER_UUE)))
69                 writeb(value, p->membase + offset);
70 }
71
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)
76 {
77         const __be32 *clk, *spd;
78         u32 clock = BASE_BAUD * 16;
79         int index;
80
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);
85
86         /* get default speed if present */
87         spd = of_get_property(np, "current-speed", NULL);
88
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;
94
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
98          */
99         if (index >= MAX_LEGACY_SERIAL_PORTS)
100                 return -1;
101         if (index >= legacy_serial_count)
102                 legacy_serial_count = index + 1;
103
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++;
115                 } else {
116                         printk(KERN_DEBUG "Replacing legacy port %d\n", index);
117                 }
118         }
119
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;
127
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;
137
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;
141         }
142
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);
150
151         return index;
152 }
153
154 static int __init add_legacy_soc_port(struct device_node *np,
155                                       struct device_node *soc_dev)
156 {
157         u64 addr;
158         const u32 *addrp;
159         struct device_node *tsi = of_get_parent(np);
160
161         /* We only support ports that have a clock frequency properly
162          * encoded in the device-tree.
163          */
164         if (of_get_property(np, "clock-frequency", NULL) == NULL)
165                 return -1;
166
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;
171
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;
175
176         /* Get the address */
177         addrp = of_get_address(soc_dev, 0, NULL, NULL);
178         if (addrp == NULL)
179                 return -1;
180
181         addr = of_translate_address(soc_dev, addrp);
182         if (addr == OF_BAD_ADDR)
183                 return -1;
184
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
187          */
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);
194 }
195
196 static int __init add_legacy_isa_port(struct device_node *np,
197                                       struct device_node *isa_brg)
198 {
199         const __be32 *reg;
200         const char *typep;
201         int index = -1;
202         u64 taddr;
203
204         DBG(" -> add_legacy_isa_port(%s)\n", np->full_name);
205
206         /* Get the ISA port number */
207         reg = of_get_property(np, "reg", NULL);
208         if (reg == NULL)
209                 return -1;
210
211         /* Verify it's an IO port, we don't support anything else */
212         if (!(be32_to_cpu(reg[0]) & 0x00000001))
213                 return -1;
214
215         /* Now look for an "ibm,aix-loc" property that gives us ordering
216          * if any...
217          */
218         typep = of_get_property(np, "ibm,aix-loc", NULL);
219
220         /* If we have a location index, then use it */
221         if (typep && *typep == 'S')
222                 index = simple_strtol(typep+1, NULL, 0) - 1;
223
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
227          */
228         taddr = of_translate_address(np, reg);
229         if (taddr == OF_BAD_ADDR)
230                 taddr = 0;
231
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);
235
236 }
237
238 #ifdef CONFIG_PCI
239 static int __init add_legacy_pci_port(struct device_node *np,
240                                       struct device_node *pci_dev)
241 {
242         u64 addr, base;
243         const u32 *addrp;
244         unsigned int flags;
245         int iotype, index = -1, lindex = 0;
246
247         DBG(" -> add_legacy_pci_port(%s)\n", np->full_name);
248
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
255          */
256         if (of_get_property(np, "clock-frequency", NULL) == NULL)
257                 return -1;
258
259         /* Get the PCI address. Assume BAR 0 */
260         addrp = of_get_pci_address(pci_dev, 0, NULL, &flags);
261         if (addrp == NULL)
262                 return -1;
263
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)
268                 return -1;
269
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)
272          */
273         if (iotype == UPIO_MEM)
274                 base = addr;
275         else
276                 base = addrp[2];
277
278         /* Try to guess an index... If we have subdevices of the pci dev,
279          * we get to their "reg" property
280          */
281         if (np != pci_dev) {
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);
285         }
286
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
291          * cases here
292          */
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;
298         } else {
299                 addr += 8 * lindex;
300                 base += 8 * lindex;
301         }
302
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
305          */
306         return add_legacy_port(np, index, iotype, base, addr, NO_IRQ,
307                                legacy_port_flags, np != pci_dev);
308 }
309 #endif
310
311 static void __init setup_legacy_serial_console(int console)
312 {
313         struct legacy_serial_info *info =
314                 &legacy_serial_infos[console];
315         void __iomem *addr;
316
317         if (info->taddr == 0)
318                 return;
319         addr = ioremap(info->taddr, 0x1000);
320         if (addr == NULL)
321                 return;
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);
326 }
327
328 /*
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
336  */
337 void __init find_legacy_serial_ports(void)
338 {
339         struct device_node *np, *stdout = NULL;
340         const char *path;
341         int index;
342
343         DBG(" -> find_legacy_serial_port()\n");
344
345         /* Now find out if one of these is out firmware console */
346         path = of_get_property(of_chosen, "linux,stdout-path", NULL);
347         if (path != NULL) {
348                 stdout = of_find_node_by_path(path);
349                 if (stdout)
350                         DBG("stdout is %s\n", stdout->full_name);
351         } else {
352                 DBG(" no linux,stdout-path !\n");
353         }
354
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);
358                 if (!parent)
359                         continue;
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;
365                         }
366                 }
367                 of_node_put(parent);
368         }
369
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;
377                 }
378                 of_node_put(isa);
379         }
380
381 #ifdef CONFIG_PCI
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")) {
386                         of_node_put(parent);
387                         continue;
388                 }
389                 if (strcmp(np->name, "serial") && strcmp(np->type, "serial")) {
390                         of_node_put(parent);
391                         continue;
392                 }
393                 /* Check for known pciclass, and also check wether we have
394                  * a device with child nodes for ports or not
395                  */
396                 if (of_device_is_compatible(np, "pciclass,0700") ||
397                     of_device_is_compatible(np, "pciclass,070002"))
398                         pci = np;
399                 else if (of_device_is_compatible(parent, "pciclass,0700") ||
400                          of_device_is_compatible(parent, "pciclass,070002"))
401                         pci = parent;
402                 else {
403                         of_node_put(parent);
404                         continue;
405                 }
406                 index = add_legacy_pci_port(np, pci);
407                 if (index >= 0 && np == stdout)
408                         legacy_serial_console = index;
409                 of_node_put(parent);
410         }
411 #endif
412
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");
417 }
418
419 static struct platform_device serial_device = {
420         .name   = "serial8250",
421         .id     = PLAT8250_DEV_PLATFORM,
422         .dev    = {
423                 .platform_data = legacy_serial_ports,
424         },
425 };
426
427 static void __init fixup_port_irq(int index,
428                                   struct device_node *np,
429                                   struct plat_serial8250_port *port)
430 {
431         unsigned int virq;
432
433         DBG("fixup_port_irq(%d)\n", index);
434
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);
438                 if (np == NULL)
439                         return;
440                 virq = irq_of_parse_and_map(np, 0);
441                 of_node_put(np);
442         }
443         if (virq == NO_IRQ)
444                 return;
445
446         port->irq = virq;
447 }
448
449 static void __init fixup_port_pio(int index,
450                                   struct device_node *np,
451                                   struct plat_serial8250_port *port)
452 {
453 #ifdef CONFIG_PCI
454         struct pci_controller *hose;
455
456         DBG("fixup_port_pio(%d)\n", index);
457
458         hose = pci_find_hose_for_OF_device(np);
459         if (hose) {
460                 unsigned long offset = (unsigned long)hose->io_base_virt -
461 #ifdef CONFIG_PPC64
462                         pci_io_base;
463 #else
464                         isa_io_base;
465 #endif
466                 DBG("port %d, IO %lx -> %lx\n",
467                     index, port->iobase, port->iobase + offset);
468                 port->iobase += offset;
469         }
470 #endif
471 }
472
473 static void __init fixup_port_mmio(int index,
474                                    struct device_node *np,
475                                    struct plat_serial8250_port *port)
476 {
477         DBG("fixup_port_mmio(%d)\n", index);
478
479         port->membase = ioremap(port->mapbase, 0x100);
480 }
481
482 /*
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.
487  *
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
493  * finally loads.
494  */
495 static int __init serial_dev_init(void)
496 {
497         int i;
498
499         if (legacy_serial_count == 0)
500                 return -ENODEV;
501
502         /*
503          * Before we register the platform serial devices, we need
504          * to fixup their interrupts and their IO ports.
505          */
506         DBG("Fixing serial ports interrupts and IO ports ...\n");
507
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;
511
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);
518         }
519
520         DBG("Registering platform serial ports\n");
521
522         return platform_device_register(&serial_device);
523 }
524 device_initcall(serial_dev_init);
525
526
527 #ifdef CONFIG_SERIAL_8250_CONSOLE
528 /*
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()
534  */
535 static int __init check_legacy_serial_console(void)
536 {
537         struct device_node *prom_stdout = NULL;
538         int i, speed = 0, offset = 0;
539         const char *name;
540         const __be32 *spd;
541
542         DBG(" -> check_legacy_serial_console()\n");
543
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");
547                 return -EBUSY;
548         }
549
550         if (!of_chosen) {
551                 DBG(" of_chosen is NULL !\n");
552                 return -ENODEV;
553         }
554
555         if (legacy_serial_console < 0) {
556                 DBG(" legacy_serial_console not found !\n");
557                 return -ENODEV;
558         }
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);
562         if (name == NULL) {
563                 DBG(" no linux,stdout-path !\n");
564                 return -ENODEV;
565         }
566         prom_stdout = of_find_node_by_path(name);
567         if (!prom_stdout) {
568                 DBG(" can't find stdout package %s !\n", name);
569                 return -ENODEV;
570         }
571         DBG("stdout is %s\n", prom_stdout->full_name);
572
573         name = of_get_property(prom_stdout, "name", NULL);
574         if (!name) {
575                 DBG(" stdout package has no name !\n");
576                 goto not_found;
577         }
578         spd = of_get_property(prom_stdout, "current-speed", NULL);
579         if (spd)
580                 speed = be32_to_cpup(spd);
581
582         if (strcmp(name, "serial") != 0)
583                 goto not_found;
584
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)
588                         continue;
589                 offset = i;
590                 speed = legacy_serial_infos[i].speed;
591                 break;
592         }
593         if (i >= legacy_serial_count)
594                 goto not_found;
595
596         of_node_put(prom_stdout);
597
598         DBG("Found serial console at ttyS%d\n", offset);
599
600         if (speed) {
601                 static char __initdata opt[16];
602                 sprintf(opt, "%d", speed);
603                 return add_preferred_console("ttyS", offset, opt);
604         } else
605                 return add_preferred_console("ttyS", offset, NULL);
606
607  not_found:
608         DBG("No preferred console found !\n");
609         of_node_put(prom_stdout);
610         return -ENODEV;
611 }
612 console_initcall(check_legacy_serial_console);
613
614 #endif /* CONFIG_SERIAL_8250_CONSOLE */