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 legacy_serial_parents
[] = {
38 {.type
= "tsi-bridge",},
40 {.compatible
= "ibm,opb",},
41 {.compatible
= "simple-bus",},
42 {.compatible
= "wrs,epld-localbus",},
46 static unsigned int legacy_serial_count
;
47 static int legacy_serial_console
= -1;
49 static int __init
add_legacy_port(struct device_node
*np
, int want_index
,
50 int iotype
, phys_addr_t base
,
51 phys_addr_t taddr
, unsigned long irq
,
52 upf_t flags
, int irq_check_parent
)
55 u32 clock
= BASE_BAUD
* 16;
58 /* get clock freq. if present */
59 clk
= of_get_property(np
, "clock-frequency", NULL
);
63 /* get default speed if present */
64 spd
= of_get_property(np
, "current-speed", NULL
);
66 /* If we have a location index, then try to use it */
67 if (want_index
>= 0 && want_index
< MAX_LEGACY_SERIAL_PORTS
)
70 index
= legacy_serial_count
;
72 /* if our index is still out of range, that mean that
73 * array is full, we could scan for a free slot but that
74 * make little sense to bother, just skip the port
76 if (index
>= MAX_LEGACY_SERIAL_PORTS
)
78 if (index
>= legacy_serial_count
)
79 legacy_serial_count
= index
+ 1;
81 /* Check if there is a port who already claimed our slot */
82 if (legacy_serial_infos
[index
].np
!= 0) {
83 /* if we still have some room, move it, else override */
84 if (legacy_serial_count
< MAX_LEGACY_SERIAL_PORTS
) {
85 printk(KERN_DEBUG
"Moved legacy port %d -> %d\n",
86 index
, legacy_serial_count
);
87 legacy_serial_ports
[legacy_serial_count
] =
88 legacy_serial_ports
[index
];
89 legacy_serial_infos
[legacy_serial_count
] =
90 legacy_serial_infos
[index
];
91 legacy_serial_count
++;
93 printk(KERN_DEBUG
"Replacing legacy port %d\n", index
);
97 /* Now fill the entry */
98 memset(&legacy_serial_ports
[index
], 0,
99 sizeof(struct plat_serial8250_port
));
100 if (iotype
== UPIO_PORT
)
101 legacy_serial_ports
[index
].iobase
= base
;
103 legacy_serial_ports
[index
].mapbase
= base
;
104 legacy_serial_ports
[index
].iotype
= iotype
;
105 legacy_serial_ports
[index
].uartclk
= clock
;
106 legacy_serial_ports
[index
].irq
= irq
;
107 legacy_serial_ports
[index
].flags
= flags
;
108 legacy_serial_infos
[index
].taddr
= taddr
;
109 legacy_serial_infos
[index
].np
= of_node_get(np
);
110 legacy_serial_infos
[index
].clock
= clock
;
111 legacy_serial_infos
[index
].speed
= spd
? *spd
: 0;
112 legacy_serial_infos
[index
].irq_check_parent
= irq_check_parent
;
114 printk(KERN_DEBUG
"Found legacy serial port %d for %s\n",
115 index
, np
->full_name
);
116 printk(KERN_DEBUG
" %s=%llx, taddr=%llx, irq=%lx, clk=%d, speed=%d\n",
117 (iotype
== UPIO_PORT
) ? "port" : "mem",
118 (unsigned long long)base
, (unsigned long long)taddr
, irq
,
119 legacy_serial_ports
[index
].uartclk
,
120 legacy_serial_infos
[index
].speed
);
125 static int __init
add_legacy_soc_port(struct device_node
*np
,
126 struct device_node
*soc_dev
)
130 upf_t flags
= UPF_BOOT_AUTOCONF
| UPF_SKIP_TEST
| UPF_SHARE_IRQ
132 struct device_node
*tsi
= of_get_parent(np
);
134 /* We only support ports that have a clock frequency properly
135 * encoded in the device-tree.
137 if (of_get_property(np
, "clock-frequency", NULL
) == NULL
)
140 /* if reg-shift or offset, don't try to use it */
141 if ((of_get_property(np
, "reg-shift", NULL
) != NULL
) ||
142 (of_get_property(np
, "reg-offset", NULL
) != NULL
))
145 /* if rtas uses this device, don't try to use it as well */
146 if (of_get_property(np
, "used-by-rtas", NULL
) != NULL
)
149 /* Get the address */
150 addrp
= of_get_address(soc_dev
, 0, NULL
, NULL
);
154 addr
= of_translate_address(soc_dev
, addrp
);
155 if (addr
== OF_BAD_ADDR
)
158 /* Add port, irq will be dealt with later. We passed a translated
159 * IO port value. It will be fixed up later along with the irq
161 if (tsi
&& !strcmp(tsi
->type
, "tsi-bridge"))
162 return add_legacy_port(np
, -1, UPIO_TSI
, addr
, addr
, NO_IRQ
, flags
, 0);
164 return add_legacy_port(np
, -1, UPIO_MEM
, addr
, addr
, NO_IRQ
, flags
, 0);
167 static int __init
add_legacy_isa_port(struct device_node
*np
,
168 struct device_node
*isa_brg
)
175 DBG(" -> add_legacy_isa_port(%s)\n", np
->full_name
);
177 /* Get the ISA port number */
178 reg
= of_get_property(np
, "reg", NULL
);
182 /* Verify it's an IO port, we don't support anything else */
183 if (!(reg
[0] & 0x00000001))
186 /* Now look for an "ibm,aix-loc" property that gives us ordering
189 typep
= of_get_property(np
, "ibm,aix-loc", NULL
);
191 /* If we have a location index, then use it */
192 if (typep
&& *typep
== 'S')
193 index
= simple_strtol(typep
+1, NULL
, 0) - 1;
195 /* Translate ISA address. If it fails, we still register the port
196 * with no translated address so that it can be picked up as an IO
197 * port later by the serial driver
199 taddr
= of_translate_address(np
, reg
);
200 if (taddr
== OF_BAD_ADDR
)
203 /* Add port, irq will be dealt with later */
204 return add_legacy_port(np
, index
, UPIO_PORT
, reg
[1], taddr
,
205 NO_IRQ
, UPF_BOOT_AUTOCONF
, 0);
210 static int __init
add_legacy_pci_port(struct device_node
*np
,
211 struct device_node
*pci_dev
)
216 int iotype
, index
= -1, lindex
= 0;
218 DBG(" -> add_legacy_pci_port(%s)\n", np
->full_name
);
220 /* We only support ports that have a clock frequency properly
221 * encoded in the device-tree (that is have an fcode). Anything
222 * else can't be used that early and will be normally probed by
223 * the generic 8250_pci driver later on. The reason is that 8250
224 * compatible UARTs on PCI need all sort of quirks (port offsets
225 * etc...) that this code doesn't know about
227 if (of_get_property(np
, "clock-frequency", NULL
) == NULL
)
230 /* Get the PCI address. Assume BAR 0 */
231 addrp
= of_get_pci_address(pci_dev
, 0, NULL
, &flags
);
235 /* We only support BAR 0 for now */
236 iotype
= (flags
& IORESOURCE_MEM
) ? UPIO_MEM
: UPIO_PORT
;
237 addr
= of_translate_address(pci_dev
, addrp
);
238 if (addr
== OF_BAD_ADDR
)
241 /* Set the IO base to the same as the translated address for MMIO,
242 * or to the domain local IO base for PIO (it will be fixed up later)
244 if (iotype
== UPIO_MEM
)
249 /* Try to guess an index... If we have subdevices of the pci dev,
250 * we get to their "reg" property
253 const u32
*reg
= of_get_property(np
, "reg", NULL
);
254 if (reg
&& (*reg
< 4))
255 index
= lindex
= *reg
;
258 /* Local index means it's the Nth port in the PCI chip. Unfortunately
259 * the offset to add here is device specific. We know about those
260 * EXAR ports and we default to the most common case. If your UART
261 * doesn't work for these settings, you'll have to add your own special
264 if (of_device_is_compatible(pci_dev
, "pci13a8,152") ||
265 of_device_is_compatible(pci_dev
, "pci13a8,154") ||
266 of_device_is_compatible(pci_dev
, "pci13a8,158")) {
267 addr
+= 0x200 * lindex
;
268 base
+= 0x200 * lindex
;
274 /* Add port, irq will be dealt with later. We passed a translated
275 * IO port value. It will be fixed up later along with the irq
277 return add_legacy_port(np
, index
, iotype
, base
, addr
, NO_IRQ
,
278 UPF_BOOT_AUTOCONF
, np
!= pci_dev
);
282 static void __init
setup_legacy_serial_console(int console
)
284 struct legacy_serial_info
*info
=
285 &legacy_serial_infos
[console
];
288 if (info
->taddr
== 0)
290 addr
= ioremap(info
->taddr
, 0x1000);
293 if (info
->speed
== 0)
294 info
->speed
= udbg_probe_uart_speed(addr
, info
->clock
);
295 DBG("default console speed = %d\n", info
->speed
);
296 udbg_init_uart(addr
, info
->speed
, info
->clock
);
300 * This is called very early, as part of setup_system() or eventually
301 * setup_arch(), basically before anything else in this file. This function
302 * will try to build a list of all the available 8250-compatible serial ports
303 * in the machine using the Open Firmware device-tree. It currently only deals
304 * with ISA and PCI busses but could be extended. It allows a very early boot
305 * console to be initialized, that list is also used later to provide 8250 with
306 * the machine non-PCI ports and to properly pick the default console port
308 void __init
find_legacy_serial_ports(void)
310 struct device_node
*np
, *stdout
= NULL
;
314 DBG(" -> find_legacy_serial_port()\n");
316 /* Now find out if one of these is out firmware console */
317 path
= of_get_property(of_chosen
, "linux,stdout-path", NULL
);
319 stdout
= of_find_node_by_path(path
);
321 DBG("stdout is %s\n", stdout
->full_name
);
323 DBG(" no linux,stdout-path !\n");
326 /* Iterate over all the 16550 ports, looking for known parents */
327 for_each_compatible_node(np
, "serial", "ns16550") {
328 struct device_node
*parent
= of_get_parent(np
);
331 if (of_match_node(legacy_serial_parents
, parent
) != NULL
) {
332 index
= add_legacy_soc_port(np
, np
);
333 if (index
>= 0 && np
== stdout
)
334 legacy_serial_console
= index
;
339 /* Next, fill our array with ISA ports */
340 for_each_node_by_type(np
, "serial") {
341 struct device_node
*isa
= of_get_parent(np
);
342 if (isa
&& !strcmp(isa
->name
, "isa")) {
343 index
= add_legacy_isa_port(np
, isa
);
344 if (index
>= 0 && np
== stdout
)
345 legacy_serial_console
= index
;
351 /* Next, try to locate PCI ports */
352 for (np
= NULL
; (np
= of_find_all_nodes(np
));) {
353 struct device_node
*pci
, *parent
= of_get_parent(np
);
354 if (parent
&& !strcmp(parent
->name
, "isa")) {
358 if (strcmp(np
->name
, "serial") && strcmp(np
->type
, "serial")) {
362 /* Check for known pciclass, and also check wether we have
363 * a device with child nodes for ports or not
365 if (of_device_is_compatible(np
, "pciclass,0700") ||
366 of_device_is_compatible(np
, "pciclass,070002"))
368 else if (of_device_is_compatible(parent
, "pciclass,0700") ||
369 of_device_is_compatible(parent
, "pciclass,070002"))
375 index
= add_legacy_pci_port(np
, pci
);
376 if (index
>= 0 && np
== stdout
)
377 legacy_serial_console
= index
;
382 DBG("legacy_serial_console = %d\n", legacy_serial_console
);
383 if (legacy_serial_console
>= 0)
384 setup_legacy_serial_console(legacy_serial_console
);
385 DBG(" <- find_legacy_serial_port()\n");
388 static struct platform_device serial_device
= {
389 .name
= "serial8250",
390 .id
= PLAT8250_DEV_PLATFORM
,
392 .platform_data
= legacy_serial_ports
,
396 static void __init
fixup_port_irq(int index
,
397 struct device_node
*np
,
398 struct plat_serial8250_port
*port
)
402 DBG("fixup_port_irq(%d)\n", index
);
404 virq
= irq_of_parse_and_map(np
, 0);
405 if (virq
== NO_IRQ
&& legacy_serial_infos
[index
].irq_check_parent
) {
406 np
= of_get_parent(np
);
409 virq
= irq_of_parse_and_map(np
, 0);
418 static void __init
fixup_port_pio(int index
,
419 struct device_node
*np
,
420 struct plat_serial8250_port
*port
)
423 struct pci_controller
*hose
;
425 DBG("fixup_port_pio(%d)\n", index
);
427 hose
= pci_find_hose_for_OF_device(np
);
429 unsigned long offset
= (unsigned long)hose
->io_base_virt
-
435 DBG("port %d, IO %lx -> %lx\n",
436 index
, port
->iobase
, port
->iobase
+ offset
);
437 port
->iobase
+= offset
;
442 static void __init
fixup_port_mmio(int index
,
443 struct device_node
*np
,
444 struct plat_serial8250_port
*port
)
446 DBG("fixup_port_mmio(%d)\n", index
);
448 port
->membase
= ioremap(port
->mapbase
, 0x100);
452 * This is called as an arch initcall, hopefully before the PCI bus is
453 * probed and/or the 8250 driver loaded since we need to register our
454 * platform devices before 8250 PCI ones are detected as some of them
455 * must properly "override" the platform ones.
457 * This function fixes up the interrupt value for platform ports as it
458 * couldn't be done earlier before interrupt maps have been parsed. It
459 * also "corrects" the IO address for PIO ports for the same reason,
460 * since earlier, the PHBs virtual IO space wasn't assigned yet. It then
461 * registers all those platform ports for use by the 8250 driver when it
464 static int __init
serial_dev_init(void)
468 if (legacy_serial_count
== 0)
472 * Before we register the platfrom serial devices, we need
473 * to fixup their interrupts and their IO ports.
475 DBG("Fixing serial ports interrupts and IO ports ...\n");
477 for (i
= 0; i
< legacy_serial_count
; i
++) {
478 struct plat_serial8250_port
*port
= &legacy_serial_ports
[i
];
479 struct device_node
*np
= legacy_serial_infos
[i
].np
;
481 if (port
->irq
== NO_IRQ
)
482 fixup_port_irq(i
, np
, port
);
483 if (port
->iotype
== UPIO_PORT
)
484 fixup_port_pio(i
, np
, port
);
485 if ((port
->iotype
== UPIO_MEM
) || (port
->iotype
== UPIO_TSI
))
486 fixup_port_mmio(i
, np
, port
);
489 DBG("Registering platform serial ports\n");
491 return platform_device_register(&serial_device
);
493 device_initcall(serial_dev_init
);
496 #ifdef CONFIG_SERIAL_8250_CONSOLE
498 * This is called very early, as part of console_init() (typically just after
499 * time_init()). This function is respondible for trying to find a good
500 * default console on serial ports. It tries to match the open firmware
501 * default output with one of the available serial console drivers that have
502 * been probed earlier by find_legacy_serial_ports()
504 static int __init
check_legacy_serial_console(void)
506 struct device_node
*prom_stdout
= NULL
;
507 int i
, speed
= 0, offset
= 0;
511 DBG(" -> check_legacy_serial_console()\n");
513 /* The user has requested a console so this is already set up. */
514 if (strstr(boot_command_line
, "console=")) {
515 DBG(" console was specified !\n");
520 DBG(" of_chosen is NULL !\n");
524 if (legacy_serial_console
< 0) {
525 DBG(" legacy_serial_console not found !\n");
528 /* We are getting a weird phandle from OF ... */
529 /* ... So use the full path instead */
530 name
= of_get_property(of_chosen
, "linux,stdout-path", NULL
);
532 DBG(" no linux,stdout-path !\n");
535 prom_stdout
= of_find_node_by_path(name
);
537 DBG(" can't find stdout package %s !\n", name
);
540 DBG("stdout is %s\n", prom_stdout
->full_name
);
542 name
= of_get_property(prom_stdout
, "name", NULL
);
544 DBG(" stdout package has no name !\n");
547 spd
= of_get_property(prom_stdout
, "current-speed", NULL
);
551 if (strcmp(name
, "serial") != 0)
554 /* Look for it in probed array */
555 for (i
= 0; i
< legacy_serial_count
; i
++) {
556 if (prom_stdout
!= legacy_serial_infos
[i
].np
)
559 speed
= legacy_serial_infos
[i
].speed
;
562 if (i
>= legacy_serial_count
)
565 of_node_put(prom_stdout
);
567 DBG("Found serial console at ttyS%d\n", offset
);
570 static char __initdata opt
[16];
571 sprintf(opt
, "%d", speed
);
572 return add_preferred_console("ttyS", offset
, opt
);
574 return add_preferred_console("ttyS", offset
, NULL
);
577 DBG("No preferred console found !\n");
578 of_node_put(prom_stdout
);
581 console_initcall(check_legacy_serial_console
);
583 #endif /* CONFIG_SERIAL_8250_CONSOLE */