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>
7 #include <linux/of_device.h>
11 #include <asm/serial.h>
13 #include <asm/pci-bridge.h>
14 #include <asm/ppc-pci.h>
19 #define DBG(fmt...) do { printk(fmt); } while(0)
21 #define DBG(fmt...) do { } while(0)
24 #define MAX_LEGACY_SERIAL_PORTS 8
26 static struct plat_serial8250_port
27 legacy_serial_ports
[MAX_LEGACY_SERIAL_PORTS
+1];
28 static struct legacy_serial_info
{
29 struct device_node
*np
;
34 } legacy_serial_infos
[MAX_LEGACY_SERIAL_PORTS
];
36 static struct __initdata of_device_id parents
[] = {
38 {.type
= "tsi-bridge",},
39 {.type
= "opb", .compatible
= "ibm,opb",},
40 {.compatible
= "simple-bus",},
41 {.compatible
= "wrs,epld-localbus",},
44 static unsigned int legacy_serial_count
;
45 static int legacy_serial_console
= -1;
47 static int __init
add_legacy_port(struct device_node
*np
, int want_index
,
48 int iotype
, phys_addr_t base
,
49 phys_addr_t taddr
, unsigned long irq
,
50 upf_t flags
, int irq_check_parent
)
53 u32 clock
= BASE_BAUD
* 16;
56 /* get clock freq. if present */
57 clk
= of_get_property(np
, "clock-frequency", NULL
);
61 /* get default speed if present */
62 spd
= of_get_property(np
, "current-speed", NULL
);
64 /* If we have a location index, then try to use it */
65 if (want_index
>= 0 && want_index
< MAX_LEGACY_SERIAL_PORTS
)
68 index
= legacy_serial_count
;
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
74 if (index
>= MAX_LEGACY_SERIAL_PORTS
)
76 if (index
>= legacy_serial_count
)
77 legacy_serial_count
= index
+ 1;
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
) {
83 printk(KERN_DEBUG
"Moved legacy port %d -> %d\n",
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
++;
91 printk(KERN_DEBUG
"Replacing legacy port %d\n", index
);
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
;
101 legacy_serial_ports
[index
].mapbase
= base
;
102 legacy_serial_ports
[index
].iotype
= iotype
;
103 legacy_serial_ports
[index
].uartclk
= clock
;
104 legacy_serial_ports
[index
].irq
= irq
;
105 legacy_serial_ports
[index
].flags
= flags
;
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;
110 legacy_serial_infos
[index
].irq_check_parent
= irq_check_parent
;
112 printk(KERN_DEBUG
"Found legacy serial port %d for %s\n",
113 index
, np
->full_name
);
114 printk(KERN_DEBUG
" %s=%llx, taddr=%llx, irq=%lx, clk=%d, speed=%d\n",
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
);
123 static int __init
add_legacy_soc_port(struct device_node
*np
,
124 struct device_node
*soc_dev
)
128 upf_t flags
= UPF_BOOT_AUTOCONF
| UPF_SKIP_TEST
| UPF_SHARE_IRQ
130 struct device_node
*tsi
= of_get_parent(np
);
132 /* We only support ports that have a clock frequency properly
133 * encoded in the device-tree.
135 if (of_get_property(np
, "clock-frequency", NULL
) == NULL
)
138 /* if rtas uses this device, don't try to use it as well */
139 if (of_get_property(np
, "used-by-rtas", NULL
) != NULL
)
142 /* Get the address */
143 addrp
= of_get_address(soc_dev
, 0, NULL
, NULL
);
147 addr
= of_translate_address(soc_dev
, addrp
);
148 if (addr
== OF_BAD_ADDR
)
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
154 if (tsi
&& !strcmp(tsi
->type
, "tsi-bridge"))
155 return add_legacy_port(np
, -1, UPIO_TSI
, addr
, addr
, NO_IRQ
, flags
, 0);
157 return add_legacy_port(np
, -1, UPIO_MEM
, addr
, addr
, NO_IRQ
, flags
, 0);
160 static int __init
add_legacy_isa_port(struct device_node
*np
,
161 struct device_node
*isa_brg
)
168 DBG(" -> add_legacy_isa_port(%s)\n", np
->full_name
);
170 /* Get the ISA port number */
171 reg
= of_get_property(np
, "reg", NULL
);
175 /* Verify it's an IO port, we don't support anything else */
176 if (!(reg
[0] & 0x00000001))
179 /* Now look for an "ibm,aix-loc" property that gives us ordering
182 typep
= of_get_property(np
, "ibm,aix-loc", NULL
);
184 /* If we have a location index, then use it */
185 if (typep
&& *typep
== 'S')
186 index
= simple_strtol(typep
+1, NULL
, 0) - 1;
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
192 taddr
= of_translate_address(np
, reg
);
193 if (taddr
== OF_BAD_ADDR
)
196 /* Add port, irq will be dealt with later */
197 return add_legacy_port(np
, index
, UPIO_PORT
, reg
[1], taddr
,
198 NO_IRQ
, UPF_BOOT_AUTOCONF
, 0);
203 static int __init
add_legacy_pci_port(struct device_node
*np
,
204 struct device_node
*pci_dev
)
209 int iotype
, index
= -1, lindex
= 0;
211 DBG(" -> add_legacy_pci_port(%s)\n", np
->full_name
);
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
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
220 if (of_get_property(np
, "clock-frequency", NULL
) == NULL
)
223 /* Get the PCI address. Assume BAR 0 */
224 addrp
= of_get_pci_address(pci_dev
, 0, NULL
, &flags
);
228 /* We only support BAR 0 for now */
229 iotype
= (flags
& IORESOURCE_MEM
) ? UPIO_MEM
: UPIO_PORT
;
230 addr
= of_translate_address(pci_dev
, addrp
);
231 if (addr
== OF_BAD_ADDR
)
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)
237 if (iotype
== UPIO_MEM
)
242 /* Try to guess an index... If we have subdevices of the pci dev,
243 * we get to their "reg" property
246 const u32
*reg
= of_get_property(np
, "reg", NULL
);
247 if (reg
&& (*reg
< 4))
248 index
= lindex
= *reg
;
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
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")) {
260 addr
+= 0x200 * lindex
;
261 base
+= 0x200 * lindex
;
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
270 return add_legacy_port(np
, index
, iotype
, base
, addr
, NO_IRQ
,
271 UPF_BOOT_AUTOCONF
, np
!= pci_dev
);
275 static void __init
setup_legacy_serial_console(int console
)
277 struct legacy_serial_info
*info
=
278 &legacy_serial_infos
[console
];
281 if (info
->taddr
== 0)
283 addr
= ioremap(info
->taddr
, 0x1000);
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
);
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
301 void __init
find_legacy_serial_ports(void)
303 struct device_node
*np
, *stdout
= NULL
;
307 DBG(" -> find_legacy_serial_port()\n");
309 /* Now find out if one of these is out firmware console */
310 path
= of_get_property(of_chosen
, "linux,stdout-path", NULL
);
312 stdout
= of_find_node_by_path(path
);
314 DBG("stdout is %s\n", stdout
->full_name
);
316 DBG(" no linux,stdout-path !\n");
319 /* Iterate over all the 16550 ports, looking for known parents */
320 for_each_compatible_node(np
, "serial", "ns16550") {
321 struct device_node
*parent
= of_get_parent(np
);
324 if (of_match_node(parents
, parent
) != NULL
) {
325 index
= add_legacy_soc_port(np
, np
);
326 if (index
>= 0 && np
== stdout
)
327 legacy_serial_console
= index
;
332 /* Next, fill our array with ISA ports */
333 for_each_node_by_type(np
, "serial") {
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
;
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")) {
351 if (strcmp(np
->name
, "serial") && strcmp(np
->type
, "serial")) {
355 /* Check for known pciclass, and also check wether we have
356 * a device with child nodes for ports or not
358 if (of_device_is_compatible(np
, "pciclass,0700") ||
359 of_device_is_compatible(np
, "pciclass,070002"))
361 else if (of_device_is_compatible(parent
, "pciclass,0700") ||
362 of_device_is_compatible(parent
, "pciclass,070002"))
368 index
= add_legacy_pci_port(np
, pci
);
369 if (index
>= 0 && np
== stdout
)
370 legacy_serial_console
= index
;
375 DBG("legacy_serial_console = %d\n", legacy_serial_console
);
376 if (legacy_serial_console
>= 0)
377 setup_legacy_serial_console(legacy_serial_console
);
378 DBG(" <- find_legacy_serial_port()\n");
381 static struct platform_device serial_device
= {
382 .name
= "serial8250",
383 .id
= PLAT8250_DEV_PLATFORM
,
385 .platform_data
= legacy_serial_ports
,
389 static void __init
fixup_port_irq(int index
,
390 struct device_node
*np
,
391 struct plat_serial8250_port
*port
)
395 DBG("fixup_port_irq(%d)\n", index
);
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
);
402 virq
= irq_of_parse_and_map(np
, 0);
411 static void __init
fixup_port_pio(int index
,
412 struct device_node
*np
,
413 struct plat_serial8250_port
*port
)
416 struct pci_controller
*hose
;
418 DBG("fixup_port_pio(%d)\n", index
);
420 hose
= pci_find_hose_for_OF_device(np
);
422 unsigned long offset
= (unsigned long)hose
->io_base_virt
-
428 DBG("port %d, IO %lx -> %lx\n",
429 index
, port
->iobase
, port
->iobase
+ offset
);
430 port
->iobase
+= offset
;
435 static void __init
fixup_port_mmio(int index
,
436 struct device_node
*np
,
437 struct plat_serial8250_port
*port
)
439 DBG("fixup_port_mmio(%d)\n", index
);
441 port
->membase
= ioremap(port
->mapbase
, 0x100);
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.
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
457 static int __init
serial_dev_init(void)
461 if (legacy_serial_count
== 0)
465 * Before we register the platfrom serial devices, we need
466 * to fixup their interrupts and their IO ports.
468 DBG("Fixing serial ports interrupts and IO ports ...\n");
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
;
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
);
478 if ((port
->iotype
== UPIO_MEM
) || (port
->iotype
== UPIO_TSI
))
479 fixup_port_mmio(i
, np
, port
);
482 DBG("Registering platform serial ports\n");
484 return platform_device_register(&serial_device
);
486 device_initcall(serial_dev_init
);
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.
497 static int __init
check_legacy_serial_console(void)
499 struct device_node
*prom_stdout
= NULL
;
500 int speed
= 0, offset
= 0;
504 DBG(" -> check_legacy_serial_console()\n");
506 /* The user has requested a console so this is already set up. */
507 if (strstr(boot_command_line
, "console=")) {
508 DBG(" console was specified !\n");
513 DBG(" of_chosen is NULL !\n");
517 if (legacy_serial_console
< 0) {
518 DBG(" legacy_serial_console not found !\n");
521 /* We are getting a weird phandle from OF ... */
522 /* ... So use the full path instead */
523 name
= of_get_property(of_chosen
, "linux,stdout-path", NULL
);
525 DBG(" no linux,stdout-path !\n");
528 prom_stdout
= of_find_node_by_path(name
);
530 DBG(" can't find stdout package %s !\n", name
);
533 DBG("stdout is %s\n", prom_stdout
->full_name
);
535 name
= of_get_property(prom_stdout
, "name", NULL
);
537 DBG(" stdout package has no name !\n");
540 spd
= of_get_property(prom_stdout
, "current-speed", NULL
);
546 #ifdef CONFIG_SERIAL_8250_CONSOLE
547 else if (strcmp(name
, "serial") == 0) {
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
)
554 speed
= legacy_serial_infos
[i
].speed
;
557 if (i
>= legacy_serial_count
)
560 #endif /* CONFIG_SERIAL_8250_CONSOLE */
561 #ifdef CONFIG_SERIAL_PMACZILOG_CONSOLE
562 else if (strcmp(name
, "ch-a") == 0)
564 else if (strcmp(name
, "ch-b") == 0)
566 #endif /* CONFIG_SERIAL_PMACZILOG_CONSOLE */
569 of_node_put(prom_stdout
);
571 DBG("Found serial console at ttyS%d\n", offset
);
574 static char __initdata opt
[16];
575 sprintf(opt
, "%d", speed
);
576 return add_preferred_console("ttyS", offset
, opt
);
578 return add_preferred_console("ttyS", offset
, NULL
);
581 DBG("No preferred console found !\n");
582 of_node_put(prom_stdout
);
585 console_initcall(check_legacy_serial_console
);