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