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>
10 #include <asm/serial.h>
12 #include <asm/pci-bridge.h>
13 #include <asm/ppc-pci.h>
18 #define DBG(fmt...) do { printk(fmt); } while(0)
20 #define DBG(fmt...) do { } while(0)
23 #define MAX_LEGACY_SERIAL_PORTS 8
25 static struct plat_serial8250_port
26 legacy_serial_ports
[MAX_LEGACY_SERIAL_PORTS
+1];
27 static struct legacy_serial_info
{
28 struct device_node
*np
;
33 } legacy_serial_infos
[MAX_LEGACY_SERIAL_PORTS
];
34 static unsigned int legacy_serial_count
;
35 static int legacy_serial_console
= -1;
37 static 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
)
43 u32 clock
= BASE_BAUD
* 16;
46 /* get clock freq. if present */
47 clk
= of_get_property(np
, "clock-frequency", NULL
);
51 /* get default speed if present */
52 spd
= of_get_property(np
, "current-speed", NULL
);
54 /* If we have a location index, then try to use it */
55 if (want_index
>= 0 && want_index
< MAX_LEGACY_SERIAL_PORTS
)
58 index
= legacy_serial_count
;
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
64 if (index
>= MAX_LEGACY_SERIAL_PORTS
)
66 if (index
>= legacy_serial_count
)
67 legacy_serial_count
= index
+ 1;
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
) {
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
++;
81 printk(KERN_DEBUG
"Replacing legacy port %d\n", index
);
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
;
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
;
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
);
113 static int __init
add_legacy_soc_port(struct device_node
*np
,
114 struct device_node
*soc_dev
)
118 upf_t flags
= UPF_BOOT_AUTOCONF
| UPF_SKIP_TEST
| UPF_SHARE_IRQ
120 struct device_node
*tsi
= of_get_parent(np
);
122 /* We only support ports that have a clock frequency properly
123 * encoded in the device-tree.
125 if (of_get_property(np
, "clock-frequency", NULL
) == NULL
)
128 /* if rtas uses this device, don't try to use it as well */
129 if (of_get_property(np
, "used-by-rtas", NULL
) != NULL
)
132 /* Get the address */
133 addrp
= of_get_address(soc_dev
, 0, NULL
, NULL
);
137 addr
= of_translate_address(soc_dev
, addrp
);
138 if (addr
== OF_BAD_ADDR
)
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
144 if (tsi
&& !strcmp(tsi
->type
, "tsi-bridge"))
145 return add_legacy_port(np
, -1, UPIO_TSI
, addr
, addr
, NO_IRQ
, flags
, 0);
147 return add_legacy_port(np
, -1, UPIO_MEM
, addr
, addr
, NO_IRQ
, flags
, 0);
150 static int __init
add_legacy_isa_port(struct device_node
*np
,
151 struct device_node
*isa_brg
)
158 DBG(" -> add_legacy_isa_port(%s)\n", np
->full_name
);
160 /* Get the ISA port number */
161 reg
= of_get_property(np
, "reg", NULL
);
165 /* Verify it's an IO port, we don't support anything else */
166 if (!(reg
[0] & 0x00000001))
169 /* Now look for an "ibm,aix-loc" property that gives us ordering
172 typep
= of_get_property(np
, "ibm,aix-loc", NULL
);
174 /* If we have a location index, then use it */
175 if (typep
&& *typep
== 'S')
176 index
= simple_strtol(typep
+1, NULL
, 0) - 1;
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
182 taddr
= of_translate_address(np
, reg
);
183 if (taddr
== OF_BAD_ADDR
)
186 /* Add port, irq will be dealt with later */
187 return add_legacy_port(np
, index
, UPIO_PORT
, reg
[1], taddr
,
188 NO_IRQ
, UPF_BOOT_AUTOCONF
, 0);
193 static int __init
add_legacy_pci_port(struct device_node
*np
,
194 struct device_node
*pci_dev
)
199 int iotype
, index
= -1, lindex
= 0;
201 DBG(" -> add_legacy_pci_port(%s)\n", np
->full_name
);
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
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
210 if (of_get_property(np
, "clock-frequency", NULL
) == NULL
)
213 /* Get the PCI address. Assume BAR 0 */
214 addrp
= of_get_pci_address(pci_dev
, 0, NULL
, &flags
);
218 /* We only support BAR 0 for now */
219 iotype
= (flags
& IORESOURCE_MEM
) ? UPIO_MEM
: UPIO_PORT
;
220 addr
= of_translate_address(pci_dev
, addrp
);
221 if (addr
== OF_BAD_ADDR
)
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)
227 if (iotype
== UPIO_MEM
)
232 /* Try to guess an index... If we have subdevices of the pci dev,
233 * we get to their "reg" property
236 const u32
*reg
= of_get_property(np
, "reg", NULL
);
237 if (reg
&& (*reg
< 4))
238 index
= lindex
= *reg
;
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
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
;
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
260 return add_legacy_port(np
, index
, iotype
, base
, addr
, NO_IRQ
,
261 UPF_BOOT_AUTOCONF
, np
!= pci_dev
);
265 static void __init
setup_legacy_serial_console(int console
)
267 struct legacy_serial_info
*info
=
268 &legacy_serial_infos
[console
];
271 if (info
->taddr
== 0)
273 addr
= ioremap(info
->taddr
, 0x1000);
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
);
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
291 void __init
find_legacy_serial_ports(void)
293 struct device_node
*np
, *stdout
= NULL
;
297 DBG(" -> find_legacy_serial_port()\n");
299 /* Now find out if one of these is out firmware console */
300 path
= of_get_property(of_chosen
, "linux,stdout-path", NULL
);
302 stdout
= of_find_node_by_path(path
);
304 DBG("stdout is %s\n", stdout
->full_name
);
306 DBG(" no linux,stdout-path !\n");
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
;
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
;
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
;
342 /* First fill our array with opb bus ports */
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
;
355 /* Next, try to locate PCI ports */
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")) {
362 if (strcmp(np
->name
, "serial") && strcmp(np
->type
, "serial")) {
366 /* Check for known pciclass, and also check wether we have
367 * a device with child nodes for ports or not
369 if (of_device_is_compatible(np
, "pciclass,0700") ||
370 of_device_is_compatible(np
, "pciclass,070002"))
372 else if (of_device_is_compatible(parent
, "pciclass,0700") ||
373 of_device_is_compatible(parent
, "pciclass,070002"))
379 index
= add_legacy_pci_port(np
, pci
);
380 if (index
>= 0 && np
== stdout
)
381 legacy_serial_console
= index
;
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");
392 static struct platform_device serial_device
= {
393 .name
= "serial8250",
394 .id
= PLAT8250_DEV_PLATFORM
,
396 .platform_data
= legacy_serial_ports
,
400 static void __init
fixup_port_irq(int index
,
401 struct device_node
*np
,
402 struct plat_serial8250_port
*port
)
406 DBG("fixup_port_irq(%d)\n", index
);
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
);
413 virq
= irq_of_parse_and_map(np
, 0);
422 static void __init
fixup_port_pio(int index
,
423 struct device_node
*np
,
424 struct plat_serial8250_port
*port
)
427 struct pci_controller
*hose
;
429 DBG("fixup_port_pio(%d)\n", index
);
431 hose
= pci_find_hose_for_OF_device(np
);
433 unsigned long offset
= (unsigned long)hose
->io_base_virt
-
439 DBG("port %d, IO %lx -> %lx\n",
440 index
, port
->iobase
, port
->iobase
+ offset
);
441 port
->iobase
+= offset
;
446 static void __init
fixup_port_mmio(int index
,
447 struct device_node
*np
,
448 struct plat_serial8250_port
*port
)
450 DBG("fixup_port_mmio(%d)\n", index
);
452 port
->membase
= ioremap(port
->mapbase
, 0x100);
456 * This is called as an arch initcall, hopefully before the PCI bus is
457 * probed and/or the 8250 driver loaded since we need to register our
458 * platform devices before 8250 PCI ones are detected as some of them
459 * must properly "override" the platform ones.
461 * This function fixes up the interrupt value for platform ports as it
462 * couldn't be done earlier before interrupt maps have been parsed. It
463 * also "corrects" the IO address for PIO ports for the same reason,
464 * since earlier, the PHBs virtual IO space wasn't assigned yet. It then
465 * registers all those platform ports for use by the 8250 driver when it
468 static int __init
serial_dev_init(void)
472 if (legacy_serial_count
== 0)
476 * Before we register the platfrom serial devices, we need
477 * to fixup their interrutps and their IO ports.
479 DBG("Fixing serial ports interrupts and IO ports ...\n");
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
;
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
);
493 DBG("Registering platform serial ports\n");
495 return platform_device_register(&serial_device
);
497 device_initcall(serial_dev_init
);
501 * This is called very early, as part of console_init() (typically just after
502 * time_init()). This function is respondible for trying to find a good
503 * default console on serial ports. It tries to match the open firmware
504 * default output with one of the available serial console drivers, either
505 * one of the platform serial ports that have been probed earlier by
506 * find_legacy_serial_ports() or some more platform specific ones.
508 static int __init
check_legacy_serial_console(void)
510 struct device_node
*prom_stdout
= NULL
;
511 int speed
= 0, offset
= 0;
515 DBG(" -> check_legacy_serial_console()\n");
517 /* The user has requested a console so this is already set up. */
518 if (strstr(boot_command_line
, "console=")) {
519 DBG(" console was specified !\n");
524 DBG(" of_chosen is NULL !\n");
528 if (legacy_serial_console
< 0) {
529 DBG(" legacy_serial_console not found !\n");
532 /* We are getting a weird phandle from OF ... */
533 /* ... So use the full path instead */
534 name
= of_get_property(of_chosen
, "linux,stdout-path", NULL
);
536 DBG(" no linux,stdout-path !\n");
539 prom_stdout
= of_find_node_by_path(name
);
541 DBG(" can't find stdout package %s !\n", name
);
544 DBG("stdout is %s\n", prom_stdout
->full_name
);
546 name
= of_get_property(prom_stdout
, "name", NULL
);
548 DBG(" stdout package has no name !\n");
551 spd
= of_get_property(prom_stdout
, "current-speed", NULL
);
557 #ifdef CONFIG_SERIAL_8250_CONSOLE
558 else if (strcmp(name
, "serial") == 0) {
560 /* Look for it in probed array */
561 for (i
= 0; i
< legacy_serial_count
; i
++) {
562 if (prom_stdout
!= legacy_serial_infos
[i
].np
)
565 speed
= legacy_serial_infos
[i
].speed
;
568 if (i
>= legacy_serial_count
)
571 #endif /* CONFIG_SERIAL_8250_CONSOLE */
572 #ifdef CONFIG_SERIAL_PMACZILOG_CONSOLE
573 else if (strcmp(name
, "ch-a") == 0)
575 else if (strcmp(name
, "ch-b") == 0)
577 #endif /* CONFIG_SERIAL_PMACZILOG_CONSOLE */
580 of_node_put(prom_stdout
);
582 DBG("Found serial console at ttyS%d\n", offset
);
585 static char __initdata opt
[16];
586 sprintf(opt
, "%d", speed
);
587 return add_preferred_console("ttyS", offset
, opt
);
589 return add_preferred_console("ttyS", offset
, NULL
);
592 DBG("No preferred console found !\n");
593 of_node_put(prom_stdout
);
596 console_initcall(check_legacy_serial_console
);