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 int irq_check_parent;
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,
39 phys_addr_t taddr, unsigned long irq,
40 upf_t flags, int irq_check_parent)
41{
42 const u32 *clk, *spd;
43 u32 clock = BASE_BAUD * 16;
44 int index;
45
46
47 clk = of_get_property(np, "clock-frequency", NULL);
48 if (clk && *clk)
49 clock = *clk;
50
51
52 spd = of_get_property(np, "current-speed", NULL);
53
54
55 if (want_index >= 0 && want_index < MAX_LEGACY_SERIAL_PORTS)
56 index = want_index;
57 else
58 index = legacy_serial_count;
59
60
61
62
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
70 if (legacy_serial_infos[index].np != 0) {
71
72 if (legacy_serial_count < MAX_LEGACY_SERIAL_PORTS) {
73 printk(KERN_DEBUG "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_DEBUG "Replacing legacy port %d\n", index);
82 }
83 }
84
85
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].mapbase = 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 = flags;
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 legacy_serial_infos[index].irq_check_parent = irq_check_parent;
101
102 printk(KERN_DEBUG "Found legacy serial port %d for %s\n",
103 index, np->full_name);
104 printk(KERN_DEBUG " %s=%llx, taddr=%llx, irq=%lx, clk=%d, speed=%d\n",
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
113static int __init add_legacy_soc_port(struct device_node *np,
114 struct device_node *soc_dev)
115{
116 u64 addr;
117 const u32 *addrp;
118 upf_t flags = UPF_BOOT_AUTOCONF | UPF_SKIP_TEST | UPF_SHARE_IRQ
119 | UPF_FIXED_PORT;
120 struct device_node *tsi = of_get_parent(np);
121
122
123
124
125 if (of_get_property(np, "clock-frequency", NULL) == NULL)
126 return -1;
127
128
129 if (of_get_property(np, "used-by-rtas", NULL) != NULL)
130 return -1;
131
132
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);
138 if (addr == OF_BAD_ADDR)
139 return -1;
140
141
142
143
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);
148}
149
150static int __init add_legacy_isa_port(struct device_node *np,
151 struct device_node *isa_brg)
152{
153 const u32 *reg;
154 const char *typep;
155 int index = -1;
156 u64 taddr;
157
158 DBG(" -> add_legacy_isa_port(%s)\n", np->full_name);
159
160
161 reg = of_get_property(np, "reg", NULL);
162 if (reg == NULL)
163 return -1;
164
165
166 if (!(reg[0] & 0x00000001))
167 return -1;
168
169
170
171
172 typep = of_get_property(np, "ibm,aix-loc", NULL);
173
174
175 if (typep && *typep == 'S')
176 index = simple_strtol(typep+1, NULL, 0) - 1;
177
178
179
180
181
182 taddr = of_translate_address(np, reg);
183 if (taddr == OF_BAD_ADDR)
184 taddr = 0;
185
186
187 return add_legacy_port(np, index, UPIO_PORT, reg[1], taddr,
188 NO_IRQ, UPF_BOOT_AUTOCONF, 0);
189
190}
191
192#ifdef CONFIG_PCI
193static int __init add_legacy_pci_port(struct device_node *np,
194 struct device_node *pci_dev)
195{
196 u64 addr, base;
197 const u32 *addrp;
198 unsigned int flags;
199 int iotype, index = -1, lindex = 0;
200
201 DBG(" -> add_legacy_pci_port(%s)\n", np->full_name);
202
203
204
205
206
207
208
209
210 if (of_get_property(np, "clock-frequency", NULL) == NULL)
211 return -1;
212
213
214 addrp = of_get_pci_address(pci_dev, 0, NULL, &flags);
215 if (addrp == NULL)
216 return -1;
217
218
219 iotype = (flags & IORESOURCE_MEM) ? UPIO_MEM : UPIO_PORT;
220 addr = of_translate_address(pci_dev, addrp);
221 if (addr == OF_BAD_ADDR)
222 return -1;
223
224
225
226
227 if (iotype == UPIO_MEM)
228 base = addr;
229 else
230 base = addrp[2];
231
232
233
234
235 if (np != pci_dev) {
236 const u32 *reg = of_get_property(np, "reg", NULL);
237 if (reg && (*reg < 4))
238 index = lindex = *reg;
239 }
240
241
242
243
244
245
246
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")) {
250 addr += 0x200 * lindex;
251 base += 0x200 * lindex;
252 } else {
253 addr += 8 * lindex;
254 base += 8 * lindex;
255 }
256
257
258
259
260 return add_legacy_port(np, index, iotype, base, addr, NO_IRQ,
261 UPF_BOOT_AUTOCONF, np != pci_dev);
262}
263#endif
264
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
282
283
284
285
286
287
288
289
290
291void __init find_legacy_serial_ports(void)
292{
293 struct device_node *np, *stdout = NULL;
294 const char *path;
295 int index;
296
297 DBG(" -> find_legacy_serial_port()\n");
298
299
300 path = of_get_property(of_chosen, "linux,stdout-path", NULL);
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 {
306 DBG(" no linux,stdout-path !\n");
307 }
308
309
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);
318 }
319
320
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
331
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
342
343 for (np = NULL; (np = of_find_compatible_node(np, "serial", "ns16550")) != NULL;) {
344 struct device_node *opb = of_get_parent(np);
345 if (opb && (!strcmp(opb->type, "opb") ||
346 of_device_is_compatible(opb, "ibm,opb"))) {
347 index = add_legacy_soc_port(np, np);
348 if (index >= 0 && np == stdout)
349 legacy_serial_console = index;
350 }
351 of_node_put(opb);
352 }
353
354#ifdef CONFIG_PCI
355
356 for (np = NULL; (np = of_find_all_nodes(np));) {
357 struct device_node *pci, *parent = of_get_parent(np);
358 if (parent && !strcmp(parent->name, "isa")) {
359 of_node_put(parent);
360 continue;
361 }
362 if (strcmp(np->name, "serial") && strcmp(np->type, "serial")) {
363 of_node_put(parent);
364 continue;
365 }
366
367
368
369 if (of_device_is_compatible(np, "pciclass,0700") ||
370 of_device_is_compatible(np, "pciclass,070002"))
371 pci = np;
372 else if (of_device_is_compatible(parent, "pciclass,0700") ||
373 of_device_is_compatible(parent, "pciclass,070002"))
374 pci = parent;
375 else {
376 of_node_put(parent);
377 continue;
378 }
379 index = add_legacy_pci_port(np, pci);
380 if (index >= 0 && np == stdout)
381 legacy_serial_console = index;
382 of_node_put(parent);
383 }
384#endif
385
386 DBG("legacy_serial_console = %d\n", legacy_serial_console);
387 if (legacy_serial_console >= 0)
388 setup_legacy_serial_console(legacy_serial_console);
389 DBG(" <- find_legacy_serial_port()\n");
390}
391
392static struct platform_device serial_device = {
393 .name = "serial8250",
394 .id = PLAT8250_DEV_PLATFORM,
395 .dev = {
396 .platform_data = legacy_serial_ports,
397 },
398};
399
400static void __init fixup_port_irq(int index,
401 struct device_node *np,
402 struct plat_serial8250_port *port)
403{
404 unsigned int virq;
405
406 DBG("fixup_port_irq(%d)\n", index);
407
408 virq = irq_of_parse_and_map(np, 0);
409 if (virq == NO_IRQ && legacy_serial_infos[index].irq_check_parent) {
410 np = of_get_parent(np);
411 if (np == NULL)
412 return;
413 virq = irq_of_parse_and_map(np, 0);
414 of_node_put(np);
415 }
416 if (virq == NO_IRQ)
417 return;
418
419 port->irq = virq;
420}
421
422static void __init fixup_port_pio(int index,
423 struct device_node *np,
424 struct plat_serial8250_port *port)
425{
426#ifdef CONFIG_PCI
427 struct pci_controller *hose;
428
429 DBG("fixup_port_pio(%d)\n", index);
430
431 hose = pci_find_hose_for_OF_device(np);
432 if (hose) {
433 unsigned long offset = (unsigned long)hose->io_base_virt -
434#ifdef CONFIG_PPC64
435 pci_io_base;
436#else
437 isa_io_base;
438#endif
439 DBG("port %d, IO %lx -> %lx\n",
440 index, port->iobase, port->iobase + offset);
441 port->iobase += offset;
442 }
443#endif
444}
445
446static void __init fixup_port_mmio(int index,
447 struct device_node *np,
448 struct plat_serial8250_port *port)
449{
450 DBG("fixup_port_mmio(%d)\n", index);
451
452 port->membase = ioremap(port->mapbase, 0x100);
453}
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468static int __init serial_dev_init(void)
469{
470 int i;
471
472 if (legacy_serial_count == 0)
473 return -ENODEV;
474
475
476
477
478
479 DBG("Fixing serial ports interrupts and IO ports ...\n");
480
481 for (i = 0; i < legacy_serial_count; i++) {
482 struct plat_serial8250_port *port = &legacy_serial_ports[i];
483 struct device_node *np = legacy_serial_infos[i].np;
484
485 if (port->irq == NO_IRQ)
486 fixup_port_irq(i, np, port);
487 if (port->iotype == UPIO_PORT)
488 fixup_port_pio(i, np, port);
489 if ((port->iotype == UPIO_MEM) || (port->iotype == UPIO_TSI))
490 fixup_port_mmio(i, np, port);
491 }
492
493 DBG("Registering platform serial ports\n");
494
495 return platform_device_register(&serial_device);
496}
497device_initcall(serial_dev_init);
498
499
500
501
502
503
504
505
506
507
508static int __init check_legacy_serial_console(void)
509{
510 struct device_node *prom_stdout = NULL;
511 int speed = 0, offset = 0;
512 const char *name;
513 const u32 *spd;
514
515 DBG(" -> check_legacy_serial_console()\n");
516
517
518 if (strstr(boot_command_line, "console=")) {
519 DBG(" console was specified !\n");
520 return -EBUSY;
521 }
522
523 if (!of_chosen) {
524 DBG(" of_chosen is NULL !\n");
525 return -ENODEV;
526 }
527
528 if (legacy_serial_console < 0) {
529 DBG(" legacy_serial_console not found !\n");
530 return -ENODEV;
531 }
532
533
534 name = of_get_property(of_chosen, "linux,stdout-path", NULL);
535 if (name == NULL) {
536 DBG(" no linux,stdout-path !\n");
537 return -ENODEV;
538 }
539 prom_stdout = of_find_node_by_path(name);
540 if (!prom_stdout) {
541 DBG(" can't find stdout package %s !\n", name);
542 return -ENODEV;
543 }
544 DBG("stdout is %s\n", prom_stdout->full_name);
545
546 name = of_get_property(prom_stdout, "name", NULL);
547 if (!name) {
548 DBG(" stdout package has no name !\n");
549 goto not_found;
550 }
551 spd = of_get_property(prom_stdout, "current-speed", NULL);
552 if (spd)
553 speed = *spd;
554
555 if (0)
556 ;
557#ifdef CONFIG_SERIAL_8250_CONSOLE
558 else if (strcmp(name, "serial") == 0) {
559 int i;
560
561 for (i = 0; i < legacy_serial_count; i++) {
562 if (prom_stdout != legacy_serial_infos[i].np)
563 continue;
564 offset = i;
565 speed = legacy_serial_infos[i].speed;
566 break;
567 }
568 if (i >= legacy_serial_count)
569 goto not_found;
570 }
571#endif
572#ifdef CONFIG_SERIAL_PMACZILOG_CONSOLE
573 else if (strcmp(name, "ch-a") == 0)
574 offset = 0;
575 else if (strcmp(name, "ch-b") == 0)
576 offset = 1;
577#endif
578 else
579 goto not_found;
580 of_node_put(prom_stdout);
581
582 DBG("Found serial console at ttyS%d\n", offset);
583
584 if (speed) {
585 static char __initdata opt[16];
586 sprintf(opt, "%d", speed);
587 return add_preferred_console("ttyS", offset, opt);
588 } else
589 return add_preferred_console("ttyS", offset, NULL);
590
591 not_found:
592 DBG("No preferred console found !\n");
593 of_node_put(prom_stdout);
594 return -ENODEV;
595}
596console_initcall(check_legacy_serial_console);
597
598