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