[PATCH] powerpc: serial port discovery: cope with broken firmware
[safe/jmp/linux-2.6] / arch / powerpc / kernel / legacy_serial.c
1 #include <linux/config.h>
2 #include <linux/kernel.h>
3 #include <linux/serial.h>
4 #include <linux/serial_8250.h>
5 #include <linux/serial_core.h>
6 #include <linux/console.h>
7 #include <linux/pci.h>
8 #include <asm/io.h>
9 #include <asm/mmu.h>
10 #include <asm/prom.h>
11 #include <asm/serial.h>
12 #include <asm/udbg.h>
13 #include <asm/pci-bridge.h>
14 #include <asm/ppc-pci.h>
15
16 #undef DEBUG
17
18 #ifdef DEBUG
19 #define DBG(fmt...) do { printk(fmt); } while(0)
20 #else
21 #define DBG(fmt...) do { } while(0)
22 #endif
23
24 #define MAX_LEGACY_SERIAL_PORTS 8
25
26 static struct plat_serial8250_port
27 legacy_serial_ports[MAX_LEGACY_SERIAL_PORTS+1];
28 static struct legacy_serial_info {
29         struct device_node              *np;
30         unsigned int                    speed;
31         unsigned int                    clock;
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 {
41         u32 *clk, *spd, clock;
42         int index;
43
44         /* get clock freq. if present */
45         clk = (u32 *)get_property(np, "clock-frequency", NULL);
46         if (clk && *clk)
47                 clock = *clk;
48         else
49                 clock = BASE_BAUD * 16;
50
51         /* get default speed if present */
52         spd = (u32 *)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_INFO "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_INFO "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].membase = (void __iomem *)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 = ASYNC_BOOT_AUTOCONF;
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
101         printk(KERN_INFO "Found legacy serial port %d for %s\n",
102                index, np->full_name);
103         printk(KERN_INFO "  %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_isa_port(struct device_node *np,
113                                       struct device_node *isa_bridge)
114 {
115         u32 *reg;
116         char *typep;
117         int index = -1;
118         phys_addr_t taddr;
119
120         /* Get the ISA port number */
121         reg = (u32 *)get_property(np, "reg", NULL);
122         if (reg == NULL)
123                 return -1;
124
125         /* Verify it's an IO port, we don't support anything else */
126         if (!(reg[0] & 0x00000001))
127                 return -1;
128
129         /* Now look for an "ibm,aix-loc" property that gives us ordering
130          * if any...
131          */
132         typep = (char *)get_property(np, "ibm,aix-loc", NULL);
133
134         /* If we have a location index, then use it */
135         if (typep && *typep == 'S')
136                 index = simple_strtol(typep+1, NULL, 0) - 1;
137
138         /* Translate ISA address */
139         taddr = of_translate_address(np, reg);
140
141         /* Add port, irq will be dealt with later */
142         return add_legacy_port(np, index, UPIO_PORT, reg[1], taddr, NO_IRQ);
143
144 }
145
146 static int __init add_legacy_pci_port(struct device_node *np,
147                                       struct device_node *pci_dev)
148 {
149         phys_addr_t addr, base;
150         u32 *addrp;
151         int iotype, index = -1;
152
153 #if 0
154         /* We only support ports that have a clock frequency properly
155          * encoded in the device-tree (that is have an fcode). Anything
156          * else can't be used that early and will be normally probed by
157          * the generic 8250_pci driver later on.
158          */
159         if (get_property(np, "clock-frequency", NULL) == NULL)
160                 return -1;
161 #endif
162
163         /* Get the PCI address. Assume BAR 0 */
164         addrp = of_get_pci_address(pci_dev, 0, NULL);
165         if (addrp == NULL)
166                 return -1;
167
168         /* We only support BAR 0 for now */
169         iotype = (addrp[0] & 0x02000000) ? UPIO_MEM : UPIO_PORT;
170         addr = of_translate_address(pci_dev, addrp);
171
172         /* Set the IO base to the same as the translated address for MMIO,
173          * or to the domain local IO base for PIO (it will be fixed up later)
174          */
175         if (iotype == UPIO_MEM)
176                 base = addr;
177         else
178                 base = addrp[2];
179
180         /* Try to guess an index... If we have subdevices of the pci dev,
181          * we get to their "reg" property
182          */
183         if (np != pci_dev) {
184                 u32 *reg = (u32 *)get_property(np, "reg", NULL);
185                 if (reg && (*reg < 4))
186                         index = legacy_serial_count + *reg;
187         }
188
189         /* Add port, irq will be dealt with later. We passed a translated
190          * IO port value. It will be fixed up later along with the irq
191          */
192         return add_legacy_port(np, index, iotype, base, addr, NO_IRQ);
193 }
194
195 /*
196  * This is called very early, as part of setup_system() or eventually
197  * setup_arch(), basically before anything else in this file. This function
198  * will try to build a list of all the available 8250-compatible serial ports
199  * in the machine using the Open Firmware device-tree. It currently only deals
200  * with ISA and PCI busses but could be extended. It allows a very early boot
201  * console to be initialized, that list is also used later to provide 8250 with
202  * the machine non-PCI ports and to properly pick the default console port
203  */
204 void __init find_legacy_serial_ports(void)
205 {
206         struct device_node *np, *stdout;
207         char *path;
208         int index;
209
210         DBG(" -> find_legacy_serial_port()\n");
211
212         /* Now find out if one of these is out firmware console */
213         path = (char *)get_property(of_chosen, "linux,stdout-path", NULL);
214         if (path == NULL) {
215                 DBG(" no linux,stdout-path !\n");
216                 return;
217         }
218         stdout = of_find_node_by_path(path);
219         if (stdout) {
220                 DBG("stdout is %s\n", stdout->full_name);
221         }
222
223         /* First fill our array with ISA ports */
224         for (np = NULL; (np = of_find_node_by_type(np, "serial"));) {
225                 struct device_node *isa = of_get_parent(np);
226                 if (isa && !strcmp(isa->name, "isa")) {
227                         index = add_legacy_isa_port(np, isa);
228                         if (index >= 0 && np == stdout)
229                                 legacy_serial_console = index;
230                 }
231                 of_node_put(isa);
232         }
233
234         /* Next, try to locate PCI ports */
235         for (np = NULL; (np = of_find_all_nodes(np));) {
236                 struct device_node *pci, *parent = of_get_parent(np);
237                 if (parent && !strcmp(parent->name, "isa")) {
238                         of_node_put(parent);
239                         continue;
240                 }
241                 if (strcmp(np->name, "serial") && strcmp(np->type, "serial")) {
242                         of_node_put(parent);
243                         continue;
244                 }
245                 /* Check for known pciclass, and also check wether we have
246                  * a device with child nodes for ports or not
247                  */
248                 if (device_is_compatible(np, "pciclass,0700") ||
249                     device_is_compatible(np, "pciclass,070002"))
250                         pci = np;
251                 else if (device_is_compatible(parent, "pciclass,0700") ||
252                          device_is_compatible(parent, "pciclass,070002"))
253                         pci = parent;
254                 else {
255                         of_node_put(parent);
256                         continue;
257                 }
258                 index = add_legacy_pci_port(np, pci);
259                 if (index >= 0 && np == stdout)
260                         legacy_serial_console = index;
261                 of_node_put(parent);
262         }
263
264         DBG("legacy_serial_console = %d\n", legacy_serial_console);
265
266         /* udbg is 64 bits only for now, that will change soon though ... */
267 #ifdef CONFIG_PPC64
268         while (legacy_serial_console >= 0) {
269                 struct legacy_serial_info *info =
270                         &legacy_serial_infos[legacy_serial_console];
271                 void __iomem *addr;
272
273                 if (info->taddr == 0)
274                         break;
275                 addr = ioremap(info->taddr, 0x1000);
276                 if (addr == NULL)
277                         break;
278                 if (info->speed == 0)
279                         info->speed = udbg_probe_uart_speed(addr, info->clock);
280                 DBG("default console speed = %d\n", info->speed);
281                 udbg_init_uart(addr, info->speed, info->clock);
282                 break;
283         }
284 #endif /* CONFIG_PPC64 */
285
286         DBG(" <- find_legacy_serial_port()\n");
287 }
288
289 static struct platform_device serial_device = {
290         .name   = "serial8250",
291         .id     = PLAT8250_DEV_PLATFORM,
292         .dev    = {
293                 .platform_data = legacy_serial_ports,
294         },
295 };
296
297 static void __init fixup_port_irq(int index,
298                                   struct device_node *np,
299                                   struct plat_serial8250_port *port)
300 {
301         DBG("fixup_port_irq(%d)\n", index);
302
303         /* Check for interrupts in that node */
304         if (np->n_intrs > 0) {
305                 port->irq = np->intrs[0].line;
306                 DBG(" port %d (%s), irq=%d\n",
307                     index, np->full_name, port->irq);
308                 return;
309         }
310
311         /* Check for interrupts in the parent */
312         np = of_get_parent(np);
313         if (np == NULL)
314                 return;
315
316         if (np->n_intrs > 0) {
317                 port->irq = np->intrs[0].line;
318                 DBG(" port %d (%s), irq=%d\n",
319                     index, np->full_name, port->irq);
320         }
321         of_node_put(np);
322 }
323
324 static void __init fixup_port_pio(int index,
325                                   struct device_node *np,
326                                   struct plat_serial8250_port *port)
327 {
328         struct pci_controller *hose;
329
330         DBG("fixup_port_pio(%d)\n", index);
331
332         hose = pci_find_hose_for_OF_device(np);
333         if (hose) {
334                 unsigned long offset = (unsigned long)hose->io_base_virt -
335 #ifdef CONFIG_PPC64
336                         pci_io_base;
337 #else
338                         isa_io_base;
339 #endif
340                 DBG("port %d, IO %lx -> %lx\n",
341                     index, port->iobase, port->iobase + offset);
342                 port->iobase += offset;
343         }
344 }
345
346 /*
347  * This is called as an arch initcall, hopefully before the PCI bus is
348  * probed and/or the 8250 driver loaded since we need to register our
349  * platform devices before 8250 PCI ones are detected as some of them
350  * must properly "override" the platform ones.
351  *
352  * This function fixes up the interrupt value for platform ports as it
353  * couldn't be done earlier before interrupt maps have been parsed. It
354  * also "corrects" the IO address for PIO ports for the same reason,
355  * since earlier, the PHBs virtual IO space wasn't assigned yet. It then
356  * registers all those platform ports for use by the 8250 driver when it
357  * finally loads.
358  */
359 static int __init serial_dev_init(void)
360 {
361         int i;
362
363         if (legacy_serial_count == 0)
364                 return -ENODEV;
365
366         /*
367          * Before we register the platfrom serial devices, we need
368          * to fixup their interrutps and their IO ports.
369          */
370         DBG("Fixing serial ports interrupts and IO ports ...\n");
371
372         for (i = 0; i < legacy_serial_count; i++) {
373                 struct plat_serial8250_port *port = &legacy_serial_ports[i];
374                 struct device_node *np = legacy_serial_infos[i].np;
375
376                 if (port->irq == NO_IRQ)
377                         fixup_port_irq(i, np, port);
378                 if (port->iotype == UPIO_PORT)
379                         fixup_port_pio(i, np, port);
380         }
381
382         DBG("Registering platform serial ports\n");
383
384         return platform_device_register(&serial_device);
385 }
386 arch_initcall(serial_dev_init);
387
388
389 /*
390  * This is called very early, as part of console_init() (typically just after
391  * time_init()). This function is respondible for trying to find a good
392  * default console on serial ports. It tries to match the open firmware
393  * default output with one of the available serial console drivers, either
394  * one of the platform serial ports that have been probed earlier by
395  * find_legacy_serial_ports() or some more platform specific ones.
396  */
397 static int __init check_legacy_serial_console(void)
398 {
399         struct device_node *prom_stdout = NULL;
400         int speed = 0, offset = 0;
401         char *name;
402         u32 *spd;
403
404         DBG(" -> check_legacy_serial_console()\n");
405
406         /* The user has requested a console so this is already set up. */
407         if (strstr(saved_command_line, "console=")) {
408                 DBG(" console was specified !\n");
409                 return -EBUSY;
410         }
411
412         if (!of_chosen) {
413                 DBG(" of_chosen is NULL !\n");
414                 return -ENODEV;
415         }
416         /* We are getting a weird phandle from OF ... */
417         /* ... So use the full path instead */
418         name = (char *)get_property(of_chosen, "linux,stdout-path", NULL);
419         if (name == NULL) {
420                 DBG(" no linux,stdout-path !\n");
421                 return -ENODEV;
422         }
423         prom_stdout = of_find_node_by_path(name);
424         if (!prom_stdout) {
425                 DBG(" can't find stdout package %s !\n", name);
426                 return -ENODEV;
427         }
428         DBG("stdout is %s\n", prom_stdout->full_name);
429
430         name = (char *)get_property(prom_stdout, "name", NULL);
431         if (!name) {
432                 DBG(" stdout package has no name !\n");
433                 goto not_found;
434         }
435         spd = (u32 *)get_property(prom_stdout, "current-speed", NULL);
436         if (spd)
437                 speed = *spd;
438
439         if (0)
440                 ;
441 #ifdef CONFIG_SERIAL_8250_CONSOLE
442         else if (strcmp(name, "serial") == 0) {
443                 int i;
444                 /* Look for it in probed array */
445                 for (i = 0; i < legacy_serial_count; i++) {
446                         if (prom_stdout != legacy_serial_infos[i].np)
447                                 continue;
448                         offset = i;
449                         speed = legacy_serial_infos[i].speed;
450                         break;
451                 }
452                 if (i >= legacy_serial_count)
453                         goto not_found;
454         }
455 #endif /* CONFIG_SERIAL_8250_CONSOLE */
456 #ifdef CONFIG_SERIAL_PMACZILOG_CONSOLE
457         else if (strcmp(name, "ch-a") == 0)
458                 offset = 0;
459         else if (strcmp(name, "ch-b") == 0)
460                 offset = 1;
461 #endif /* CONFIG_SERIAL_PMACZILOG_CONSOLE */
462         else
463                 goto not_found;
464         of_node_put(prom_stdout);
465
466         DBG("Found serial console at ttyS%d\n", offset);
467
468         if (speed) {
469                 static char __initdata opt[16];
470                 sprintf(opt, "%d", speed);
471                 return add_preferred_console("ttyS", offset, opt);
472         } else
473                 return add_preferred_console("ttyS", offset, NULL);
474
475  not_found:
476         DBG("No preferred console found !\n");
477         of_node_put(prom_stdout);
478         return -ENODEV;
479 }
480 console_initcall(check_legacy_serial_console);
481