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