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_address.h>
8 #include <linux/of_device.h>
9 #include <linux/serial_reg.h>
13 #include <asm/serial.h>
15 #include <asm/pci-bridge.h>
16 #include <asm/ppc-pci.h>
21 #define DBG(fmt...) do { printk(fmt); } while(0)
23 #define DBG(fmt...) do { } while(0)
26 #define MAX_LEGACY_SERIAL_PORTS 8
28 static struct plat_serial8250_port
29 legacy_serial_ports
[MAX_LEGACY_SERIAL_PORTS
+1];
30 static struct legacy_serial_info
{
31 struct device_node
*np
;
36 } legacy_serial_infos
[MAX_LEGACY_SERIAL_PORTS
];
38 static struct __initdata of_device_id legacy_serial_parents
[] = {
40 {.type
= "tsi-bridge",},
42 {.compatible
= "ibm,opb",},
43 {.compatible
= "simple-bus",},
44 {.compatible
= "wrs,epld-localbus",},
48 static unsigned int legacy_serial_count
;
49 static int legacy_serial_console
= -1;
51 static const upf_t legacy_port_flags
= UPF_BOOT_AUTOCONF
| UPF_SKIP_TEST
|
52 UPF_SHARE_IRQ
| UPF_FIXED_PORT
;
54 static unsigned int tsi_serial_in(struct uart_port
*p
, int offset
)
57 offset
= offset
<< p
->regshift
;
58 if (offset
== UART_IIR
) {
59 tmp
= readl(p
->membase
+ (UART_IIR
& ~3));
60 return (tmp
>> 16) & 0xff; /* UART_IIR % 4 == 2 */
62 return readb(p
->membase
+ offset
);
65 static void tsi_serial_out(struct uart_port
*p
, int offset
, int value
)
67 offset
= offset
<< p
->regshift
;
68 if (!((offset
== UART_IER
) && (value
& UART_IER_UUE
)))
69 writeb(value
, p
->membase
+ offset
);
72 static int __init
add_legacy_port(struct device_node
*np
, int want_index
,
73 int iotype
, phys_addr_t base
,
74 phys_addr_t taddr
, unsigned long irq
,
75 upf_t flags
, int irq_check_parent
)
77 const __be32
*clk
, *spd
;
78 u32 clock
= BASE_BAUD
* 16;
81 /* get clock freq. if present */
82 clk
= of_get_property(np
, "clock-frequency", NULL
);
84 clock
= be32_to_cpup(clk
);
86 /* get default speed if present */
87 spd
= of_get_property(np
, "current-speed", NULL
);
89 /* If we have a location index, then try to use it */
90 if (want_index
>= 0 && want_index
< MAX_LEGACY_SERIAL_PORTS
)
93 index
= legacy_serial_count
;
95 /* if our index is still out of range, that mean that
96 * array is full, we could scan for a free slot but that
97 * make little sense to bother, just skip the port
99 if (index
>= MAX_LEGACY_SERIAL_PORTS
)
101 if (index
>= legacy_serial_count
)
102 legacy_serial_count
= index
+ 1;
104 /* Check if there is a port who already claimed our slot */
105 if (legacy_serial_infos
[index
].np
!= NULL
) {
106 /* if we still have some room, move it, else override */
107 if (legacy_serial_count
< MAX_LEGACY_SERIAL_PORTS
) {
108 printk(KERN_DEBUG
"Moved legacy port %d -> %d\n",
109 index
, legacy_serial_count
);
110 legacy_serial_ports
[legacy_serial_count
] =
111 legacy_serial_ports
[index
];
112 legacy_serial_infos
[legacy_serial_count
] =
113 legacy_serial_infos
[index
];
114 legacy_serial_count
++;
116 printk(KERN_DEBUG
"Replacing legacy port %d\n", index
);
120 /* Now fill the entry */
121 memset(&legacy_serial_ports
[index
], 0,
122 sizeof(struct plat_serial8250_port
));
123 if (iotype
== UPIO_PORT
)
124 legacy_serial_ports
[index
].iobase
= base
;
126 legacy_serial_ports
[index
].mapbase
= base
;
128 legacy_serial_ports
[index
].iotype
= iotype
;
129 legacy_serial_ports
[index
].uartclk
= clock
;
130 legacy_serial_ports
[index
].irq
= irq
;
131 legacy_serial_ports
[index
].flags
= flags
;
132 legacy_serial_infos
[index
].taddr
= taddr
;
133 legacy_serial_infos
[index
].np
= of_node_get(np
);
134 legacy_serial_infos
[index
].clock
= clock
;
135 legacy_serial_infos
[index
].speed
= spd
? be32_to_cpup(spd
) : 0;
136 legacy_serial_infos
[index
].irq_check_parent
= irq_check_parent
;
138 if (iotype
== UPIO_TSI
) {
139 legacy_serial_ports
[index
].serial_in
= tsi_serial_in
;
140 legacy_serial_ports
[index
].serial_out
= tsi_serial_out
;
143 printk(KERN_DEBUG
"Found legacy serial port %d for %s\n",
144 index
, np
->full_name
);
145 printk(KERN_DEBUG
" %s=%llx, taddr=%llx, irq=%lx, clk=%d, speed=%d\n",
146 (iotype
== UPIO_PORT
) ? "port" : "mem",
147 (unsigned long long)base
, (unsigned long long)taddr
, irq
,
148 legacy_serial_ports
[index
].uartclk
,
149 legacy_serial_infos
[index
].speed
);
154 static int __init
add_legacy_soc_port(struct device_node
*np
,
155 struct device_node
*soc_dev
)
159 struct device_node
*tsi
= of_get_parent(np
);
161 /* We only support ports that have a clock frequency properly
162 * encoded in the device-tree.
164 if (of_get_property(np
, "clock-frequency", NULL
) == NULL
)
167 /* if reg-shift or offset, don't try to use it */
168 if ((of_get_property(np
, "reg-shift", NULL
) != NULL
) ||
169 (of_get_property(np
, "reg-offset", NULL
) != NULL
))
172 /* if rtas uses this device, don't try to use it as well */
173 if (of_get_property(np
, "used-by-rtas", NULL
) != NULL
)
176 /* Get the address */
177 addrp
= of_get_address(soc_dev
, 0, NULL
, NULL
);
181 addr
= of_translate_address(soc_dev
, addrp
);
182 if (addr
== OF_BAD_ADDR
)
185 /* Add port, irq will be dealt with later. We passed a translated
186 * IO port value. It will be fixed up later along with the irq
188 if (tsi
&& !strcmp(tsi
->type
, "tsi-bridge"))
189 return add_legacy_port(np
, -1, UPIO_TSI
, addr
, addr
,
190 NO_IRQ
, legacy_port_flags
, 0);
192 return add_legacy_port(np
, -1, UPIO_MEM
, addr
, addr
,
193 NO_IRQ
, legacy_port_flags
, 0);
196 static int __init
add_legacy_isa_port(struct device_node
*np
,
197 struct device_node
*isa_brg
)
204 DBG(" -> add_legacy_isa_port(%s)\n", np
->full_name
);
206 /* Get the ISA port number */
207 reg
= of_get_property(np
, "reg", NULL
);
211 /* Verify it's an IO port, we don't support anything else */
212 if (!(be32_to_cpu(reg
[0]) & 0x00000001))
215 /* Now look for an "ibm,aix-loc" property that gives us ordering
218 typep
= of_get_property(np
, "ibm,aix-loc", NULL
);
220 /* If we have a location index, then use it */
221 if (typep
&& *typep
== 'S')
222 index
= simple_strtol(typep
+1, NULL
, 0) - 1;
224 /* Translate ISA address. If it fails, we still register the port
225 * with no translated address so that it can be picked up as an IO
226 * port later by the serial driver
228 * Note: Don't even try on P8 lpc, we know it's not directly mapped
230 if (!of_device_is_compatible(isa_brg
, "ibm,power8-lpc")) {
231 taddr
= of_translate_address(np
, reg
);
232 if (taddr
== OF_BAD_ADDR
)
237 /* Add port, irq will be dealt with later */
238 return add_legacy_port(np
, index
, UPIO_PORT
, be32_to_cpu(reg
[1]),
239 taddr
, NO_IRQ
, legacy_port_flags
, 0);
244 static int __init
add_legacy_pci_port(struct device_node
*np
,
245 struct device_node
*pci_dev
)
250 int iotype
, index
= -1, lindex
= 0;
252 DBG(" -> add_legacy_pci_port(%s)\n", np
->full_name
);
254 /* We only support ports that have a clock frequency properly
255 * encoded in the device-tree (that is have an fcode). Anything
256 * else can't be used that early and will be normally probed by
257 * the generic 8250_pci driver later on. The reason is that 8250
258 * compatible UARTs on PCI need all sort of quirks (port offsets
259 * etc...) that this code doesn't know about
261 if (of_get_property(np
, "clock-frequency", NULL
) == NULL
)
264 /* Get the PCI address. Assume BAR 0 */
265 addrp
= of_get_pci_address(pci_dev
, 0, NULL
, &flags
);
269 /* We only support BAR 0 for now */
270 iotype
= (flags
& IORESOURCE_MEM
) ? UPIO_MEM
: UPIO_PORT
;
271 addr
= of_translate_address(pci_dev
, addrp
);
272 if (addr
== OF_BAD_ADDR
)
275 /* Set the IO base to the same as the translated address for MMIO,
276 * or to the domain local IO base for PIO (it will be fixed up later)
278 if (iotype
== UPIO_MEM
)
281 base
= of_read_number(&addrp
[2], 1);
283 /* Try to guess an index... If we have subdevices of the pci dev,
284 * we get to their "reg" property
287 const __be32
*reg
= of_get_property(np
, "reg", NULL
);
288 if (reg
&& (be32_to_cpup(reg
) < 4))
289 index
= lindex
= be32_to_cpup(reg
);
292 /* Local index means it's the Nth port in the PCI chip. Unfortunately
293 * the offset to add here is device specific. We know about those
294 * EXAR ports and we default to the most common case. If your UART
295 * doesn't work for these settings, you'll have to add your own special
298 if (of_device_is_compatible(pci_dev
, "pci13a8,152") ||
299 of_device_is_compatible(pci_dev
, "pci13a8,154") ||
300 of_device_is_compatible(pci_dev
, "pci13a8,158")) {
301 addr
+= 0x200 * lindex
;
302 base
+= 0x200 * lindex
;
308 /* Add port, irq will be dealt with later. We passed a translated
309 * IO port value. It will be fixed up later along with the irq
311 return add_legacy_port(np
, index
, iotype
, base
, addr
, NO_IRQ
,
312 legacy_port_flags
, np
!= pci_dev
);
316 static void __init
setup_legacy_serial_console(int console
)
318 struct legacy_serial_info
*info
= &legacy_serial_infos
[console
];
319 struct plat_serial8250_port
*port
= &legacy_serial_ports
[console
];
322 /* Check if a translated MMIO address has been found */
324 addr
= ioremap(info
->taddr
, 0x1000);
327 udbg_uart_init_mmio(addr
, 1);
329 /* Check if it's PIO and we support untranslated PIO */
330 if (port
->iotype
== UPIO_PORT
&& isa_io_special
)
331 udbg_uart_init_pio(port
->iobase
, 1);
336 /* Try to query the current speed */
337 if (info
->speed
== 0)
338 info
->speed
= udbg_probe_uart_speed(info
->clock
);
341 DBG("default console speed = %d\n", info
->speed
);
342 udbg_uart_setup(info
->speed
, info
->clock
);
346 * This is called very early, as part of setup_system() or eventually
347 * setup_arch(), basically before anything else in this file. This function
348 * will try to build a list of all the available 8250-compatible serial ports
349 * in the machine using the Open Firmware device-tree. It currently only deals
350 * with ISA and PCI busses but could be extended. It allows a very early boot
351 * console to be initialized, that list is also used later to provide 8250 with
352 * the machine non-PCI ports and to properly pick the default console port
354 void __init
find_legacy_serial_ports(void)
356 struct device_node
*np
, *stdout
= NULL
;
360 DBG(" -> find_legacy_serial_port()\n");
362 /* Now find out if one of these is out firmware console */
363 path
= of_get_property(of_chosen
, "linux,stdout-path", NULL
);
365 stdout
= of_find_node_by_path(path
);
367 DBG("stdout is %s\n", stdout
->full_name
);
369 DBG(" no linux,stdout-path !\n");
372 /* Iterate over all the 16550 ports, looking for known parents */
373 for_each_compatible_node(np
, "serial", "ns16550") {
374 struct device_node
*parent
= of_get_parent(np
);
377 if (of_match_node(legacy_serial_parents
, parent
) != NULL
) {
378 if (of_device_is_available(np
)) {
379 index
= add_legacy_soc_port(np
, np
);
380 if (index
>= 0 && np
== stdout
)
381 legacy_serial_console
= index
;
387 /* Next, fill our array with ISA ports */
388 for_each_node_by_type(np
, "serial") {
389 struct device_node
*isa
= of_get_parent(np
);
390 if (isa
&& (!strcmp(isa
->name
, "isa") ||
391 !strcmp(isa
->name
, "lpc"))) {
392 if (of_device_is_available(np
)) {
393 index
= add_legacy_isa_port(np
, isa
);
394 if (index
>= 0 && np
== stdout
)
395 legacy_serial_console
= index
;
402 /* Next, try to locate PCI ports */
403 for (np
= NULL
; (np
= of_find_all_nodes(np
));) {
404 struct device_node
*pci
, *parent
= of_get_parent(np
);
405 if (parent
&& !strcmp(parent
->name
, "isa")) {
409 if (strcmp(np
->name
, "serial") && strcmp(np
->type
, "serial")) {
413 /* Check for known pciclass, and also check whether we have
414 * a device with child nodes for ports or not
416 if (of_device_is_compatible(np
, "pciclass,0700") ||
417 of_device_is_compatible(np
, "pciclass,070002"))
419 else if (of_device_is_compatible(parent
, "pciclass,0700") ||
420 of_device_is_compatible(parent
, "pciclass,070002"))
426 index
= add_legacy_pci_port(np
, pci
);
427 if (index
>= 0 && np
== stdout
)
428 legacy_serial_console
= index
;
433 DBG("legacy_serial_console = %d\n", legacy_serial_console
);
434 if (legacy_serial_console
>= 0)
435 setup_legacy_serial_console(legacy_serial_console
);
436 DBG(" <- find_legacy_serial_port()\n");
439 static struct platform_device serial_device
= {
440 .name
= "serial8250",
441 .id
= PLAT8250_DEV_PLATFORM
,
443 .platform_data
= legacy_serial_ports
,
447 static void __init
fixup_port_irq(int index
,
448 struct device_node
*np
,
449 struct plat_serial8250_port
*port
)
453 DBG("fixup_port_irq(%d)\n", index
);
455 virq
= irq_of_parse_and_map(np
, 0);
456 if (virq
== NO_IRQ
&& legacy_serial_infos
[index
].irq_check_parent
) {
457 np
= of_get_parent(np
);
460 virq
= irq_of_parse_and_map(np
, 0);
468 #ifdef CONFIG_SERIAL_8250_FSL
469 if (of_device_is_compatible(np
, "fsl,ns16550"))
470 port
->handle_irq
= fsl8250_handle_irq
;
474 static void __init
fixup_port_pio(int index
,
475 struct device_node
*np
,
476 struct plat_serial8250_port
*port
)
479 struct pci_controller
*hose
;
481 DBG("fixup_port_pio(%d)\n", index
);
483 hose
= pci_find_hose_for_OF_device(np
);
485 unsigned long offset
= (unsigned long)hose
->io_base_virt
-
491 DBG("port %d, IO %lx -> %lx\n",
492 index
, port
->iobase
, port
->iobase
+ offset
);
493 port
->iobase
+= offset
;
498 static void __init
fixup_port_mmio(int index
,
499 struct device_node
*np
,
500 struct plat_serial8250_port
*port
)
502 DBG("fixup_port_mmio(%d)\n", index
);
504 port
->membase
= ioremap(port
->mapbase
, 0x100);
508 * This is called as an arch initcall, hopefully before the PCI bus is
509 * probed and/or the 8250 driver loaded since we need to register our
510 * platform devices before 8250 PCI ones are detected as some of them
511 * must properly "override" the platform ones.
513 * This function fixes up the interrupt value for platform ports as it
514 * couldn't be done earlier before interrupt maps have been parsed. It
515 * also "corrects" the IO address for PIO ports for the same reason,
516 * since earlier, the PHBs virtual IO space wasn't assigned yet. It then
517 * registers all those platform ports for use by the 8250 driver when it
520 static int __init
serial_dev_init(void)
524 if (legacy_serial_count
== 0)
528 * Before we register the platform serial devices, we need
529 * to fixup their interrupts and their IO ports.
531 DBG("Fixing serial ports interrupts and IO ports ...\n");
533 for (i
= 0; i
< legacy_serial_count
; i
++) {
534 struct plat_serial8250_port
*port
= &legacy_serial_ports
[i
];
535 struct device_node
*np
= legacy_serial_infos
[i
].np
;
537 if (port
->irq
== NO_IRQ
)
538 fixup_port_irq(i
, np
, port
);
539 if (port
->iotype
== UPIO_PORT
)
540 fixup_port_pio(i
, np
, port
);
541 if ((port
->iotype
== UPIO_MEM
) || (port
->iotype
== UPIO_TSI
))
542 fixup_port_mmio(i
, np
, port
);
545 DBG("Registering platform serial ports\n");
547 return platform_device_register(&serial_device
);
549 device_initcall(serial_dev_init
);
552 #ifdef CONFIG_SERIAL_8250_CONSOLE
554 * This is called very early, as part of console_init() (typically just after
555 * time_init()). This function is respondible for trying to find a good
556 * default console on serial ports. It tries to match the open firmware
557 * default output with one of the available serial console drivers that have
558 * been probed earlier by find_legacy_serial_ports()
560 static int __init
check_legacy_serial_console(void)
562 struct device_node
*prom_stdout
= NULL
;
563 int i
, speed
= 0, offset
= 0;
567 DBG(" -> check_legacy_serial_console()\n");
569 /* The user has requested a console so this is already set up. */
570 if (strstr(boot_command_line
, "console=")) {
571 DBG(" console was specified !\n");
576 DBG(" of_chosen is NULL !\n");
580 if (legacy_serial_console
< 0) {
581 DBG(" legacy_serial_console not found !\n");
584 /* We are getting a weird phandle from OF ... */
585 /* ... So use the full path instead */
586 name
= of_get_property(of_chosen
, "linux,stdout-path", NULL
);
588 DBG(" no linux,stdout-path !\n");
591 prom_stdout
= of_find_node_by_path(name
);
593 DBG(" can't find stdout package %s !\n", name
);
596 DBG("stdout is %s\n", prom_stdout
->full_name
);
598 name
= of_get_property(prom_stdout
, "name", NULL
);
600 DBG(" stdout package has no name !\n");
603 spd
= of_get_property(prom_stdout
, "current-speed", NULL
);
605 speed
= be32_to_cpup(spd
);
607 if (strcmp(name
, "serial") != 0)
610 /* Look for it in probed array */
611 for (i
= 0; i
< legacy_serial_count
; i
++) {
612 if (prom_stdout
!= legacy_serial_infos
[i
].np
)
615 speed
= legacy_serial_infos
[i
].speed
;
618 if (i
>= legacy_serial_count
)
621 of_node_put(prom_stdout
);
623 DBG("Found serial console at ttyS%d\n", offset
);
626 static char __initdata opt
[16];
627 sprintf(opt
, "%d", speed
);
628 return add_preferred_console("ttyS", offset
, opt
);
630 return add_preferred_console("ttyS", offset
, NULL
);
633 DBG("No preferred console found !\n");
634 of_node_put(prom_stdout
);
637 console_initcall(check_legacy_serial_console
);
639 #endif /* CONFIG_SERIAL_8250_CONSOLE */