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