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