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