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