1 // SPDX-License-Identifier: GPL-2.0
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>
9 #include <linux/of_address.h>
10 #include <linux/of_irq.h>
11 #include <linux/serial_reg.h>
14 #include <asm/serial.h>
16 #include <asm/pci-bridge.h>
17 #include <asm/ppc-pci.h>
18 #include <asm/early_ioremap.h>
23 #define DBG(fmt...) do { printk(fmt); } while(0)
25 #define DBG(fmt...) do { } while(0)
28 #define MAX_LEGACY_SERIAL_PORTS 8
30 static struct plat_serial8250_port
31 legacy_serial_ports
[MAX_LEGACY_SERIAL_PORTS
+1];
32 static struct legacy_serial_info
{
33 struct device_node
*np
;
38 void __iomem
*early_addr
;
39 } legacy_serial_infos
[MAX_LEGACY_SERIAL_PORTS
];
41 static const struct of_device_id legacy_serial_parents
[] __initconst
= {
43 {.type
= "tsi-bridge",},
45 {.compatible
= "ibm,opb",},
46 {.compatible
= "simple-bus",},
47 {.compatible
= "wrs,epld-localbus",},
51 static unsigned int legacy_serial_count
;
52 static int legacy_serial_console
= -1;
54 static const upf_t legacy_port_flags
= UPF_BOOT_AUTOCONF
| UPF_SKIP_TEST
|
55 UPF_SHARE_IRQ
| UPF_FIXED_PORT
;
57 static unsigned int tsi_serial_in(struct uart_port
*p
, int offset
)
60 offset
= offset
<< p
->regshift
;
61 if (offset
== UART_IIR
) {
62 tmp
= readl(p
->membase
+ (UART_IIR
& ~3));
63 return (tmp
>> 16) & 0xff; /* UART_IIR % 4 == 2 */
65 return readb(p
->membase
+ offset
);
68 static void tsi_serial_out(struct uart_port
*p
, int offset
, int value
)
70 offset
= offset
<< p
->regshift
;
71 if (!((offset
== UART_IER
) && (value
& UART_IER_UUE
)))
72 writeb(value
, p
->membase
+ offset
);
75 static int __init
add_legacy_port(struct device_node
*np
, int want_index
,
76 int iotype
, phys_addr_t base
,
77 phys_addr_t taddr
, unsigned long irq
,
78 upf_t flags
, int irq_check_parent
)
80 const __be32
*clk
, *spd
, *rs
;
81 u32 clock
= BASE_BAUD
* 16;
85 /* get clock freq. if present */
86 clk
= of_get_property(np
, "clock-frequency", NULL
);
88 clock
= be32_to_cpup(clk
);
90 /* get default speed if present */
91 spd
= of_get_property(np
, "current-speed", NULL
);
93 /* get register shift if present */
94 rs
= of_get_property(np
, "reg-shift", NULL
);
96 shift
= be32_to_cpup(rs
);
98 /* If we have a location index, then try to use it */
99 if (want_index
>= 0 && want_index
< MAX_LEGACY_SERIAL_PORTS
)
102 index
= legacy_serial_count
;
104 /* if our index is still out of range, that mean that
105 * array is full, we could scan for a free slot but that
106 * make little sense to bother, just skip the port
108 if (index
>= MAX_LEGACY_SERIAL_PORTS
)
110 if (index
>= legacy_serial_count
)
111 legacy_serial_count
= index
+ 1;
113 /* Check if there is a port who already claimed our slot */
114 if (legacy_serial_infos
[index
].np
!= NULL
) {
115 /* if we still have some room, move it, else override */
116 if (legacy_serial_count
< MAX_LEGACY_SERIAL_PORTS
) {
117 printk(KERN_DEBUG
"Moved legacy port %d -> %d\n",
118 index
, legacy_serial_count
);
119 legacy_serial_ports
[legacy_serial_count
] =
120 legacy_serial_ports
[index
];
121 legacy_serial_infos
[legacy_serial_count
] =
122 legacy_serial_infos
[index
];
123 legacy_serial_count
++;
125 printk(KERN_DEBUG
"Replacing legacy port %d\n", index
);
129 /* Now fill the entry */
130 memset(&legacy_serial_ports
[index
], 0,
131 sizeof(struct plat_serial8250_port
));
132 if (iotype
== UPIO_PORT
)
133 legacy_serial_ports
[index
].iobase
= base
;
135 legacy_serial_ports
[index
].mapbase
= base
;
137 legacy_serial_ports
[index
].iotype
= iotype
;
138 legacy_serial_ports
[index
].uartclk
= clock
;
139 legacy_serial_ports
[index
].irq
= irq
;
140 legacy_serial_ports
[index
].flags
= flags
;
141 legacy_serial_ports
[index
].regshift
= shift
;
142 legacy_serial_infos
[index
].taddr
= taddr
;
143 legacy_serial_infos
[index
].np
= of_node_get(np
);
144 legacy_serial_infos
[index
].clock
= clock
;
145 legacy_serial_infos
[index
].speed
= spd
? be32_to_cpup(spd
) : 0;
146 legacy_serial_infos
[index
].irq_check_parent
= irq_check_parent
;
148 if (iotype
== UPIO_TSI
) {
149 legacy_serial_ports
[index
].serial_in
= tsi_serial_in
;
150 legacy_serial_ports
[index
].serial_out
= tsi_serial_out
;
153 printk(KERN_DEBUG
"Found legacy serial port %d for %pOF\n",
155 printk(KERN_DEBUG
" %s=%llx, taddr=%llx, irq=%lx, clk=%d, speed=%d\n",
156 (iotype
== UPIO_PORT
) ? "port" : "mem",
157 (unsigned long long)base
, (unsigned long long)taddr
, irq
,
158 legacy_serial_ports
[index
].uartclk
,
159 legacy_serial_infos
[index
].speed
);
164 static int __init
add_legacy_soc_port(struct device_node
*np
,
165 struct device_node
*soc_dev
)
169 struct device_node
*tsi
= of_get_parent(np
);
171 /* We only support ports that have a clock frequency properly
172 * encoded in the device-tree.
174 if (!of_property_present(np
, "clock-frequency"))
177 /* if reg-offset don't try to use it */
178 if (of_property_present(np
, "reg-offset"))
181 /* if rtas uses this device, don't try to use it as well */
182 if (of_property_read_bool(np
, "used-by-rtas"))
185 /* Get the address */
186 addrp
= of_get_address(soc_dev
, 0, NULL
, NULL
);
190 addr
= of_translate_address(soc_dev
, addrp
);
191 if (addr
== OF_BAD_ADDR
)
194 /* Add port, irq will be dealt with later. We passed a translated
195 * IO port value. It will be fixed up later along with the irq
197 if (of_node_is_type(tsi
, "tsi-bridge"))
198 return add_legacy_port(np
, -1, UPIO_TSI
, addr
, addr
,
199 0, legacy_port_flags
, 0);
201 return add_legacy_port(np
, -1, UPIO_MEM
, addr
, addr
,
202 0, legacy_port_flags
, 0);
205 static int __init
add_legacy_isa_port(struct device_node
*np
,
206 struct device_node
*isa_brg
)
213 DBG(" -> add_legacy_isa_port(%pOF)\n", np
);
215 /* Get the ISA port number */
216 reg
= of_get_property(np
, "reg", NULL
);
220 /* Verify it's an IO port, we don't support anything else */
221 if (!(be32_to_cpu(reg
[0]) & 0x00000001))
224 /* Now look for an "ibm,aix-loc" property that gives us ordering
227 typep
= of_get_property(np
, "ibm,aix-loc", NULL
);
229 /* If we have a location index, then use it */
230 if (typep
&& *typep
== 'S')
231 index
= simple_strtol(typep
+1, NULL
, 0) - 1;
233 /* Translate ISA address. If it fails, we still register the port
234 * with no translated address so that it can be picked up as an IO
235 * port later by the serial driver
237 * Note: Don't even try on P8 lpc, we know it's not directly mapped
239 if (!of_device_is_compatible(isa_brg
, "ibm,power8-lpc") ||
240 of_property_present(isa_brg
, "ranges")) {
241 taddr
= of_translate_address(np
, reg
);
242 if (taddr
== OF_BAD_ADDR
)
247 /* Add port, irq will be dealt with later */
248 return add_legacy_port(np
, index
, UPIO_PORT
, be32_to_cpu(reg
[1]),
249 taddr
, 0, legacy_port_flags
, 0);
254 static int __init
add_legacy_pci_port(struct device_node
*np
,
255 struct device_node
*pci_dev
)
260 int iotype
, index
= -1, lindex
= 0;
262 DBG(" -> add_legacy_pci_port(%pOF)\n", np
);
264 /* We only support ports that have a clock frequency properly
265 * encoded in the device-tree (that is have an fcode). Anything
266 * else can't be used that early and will be normally probed by
267 * the generic 8250_pci driver later on. The reason is that 8250
268 * compatible UARTs on PCI need all sort of quirks (port offsets
269 * etc...) that this code doesn't know about
271 if (!of_property_present(np
, "clock-frequency"))
274 /* Get the PCI address. Assume BAR 0 */
275 addrp
= of_get_pci_address(pci_dev
, 0, NULL
, &flags
);
279 /* We only support BAR 0 for now */
280 iotype
= (flags
& IORESOURCE_MEM
) ? UPIO_MEM
: UPIO_PORT
;
281 addr
= of_translate_address(pci_dev
, addrp
);
282 if (addr
== OF_BAD_ADDR
)
285 /* Set the IO base to the same as the translated address for MMIO,
286 * or to the domain local IO base for PIO (it will be fixed up later)
288 if (iotype
== UPIO_MEM
)
291 base
= of_read_number(&addrp
[2], 1);
293 /* Try to guess an index... If we have subdevices of the pci dev,
294 * we get to their "reg" property
297 const __be32
*reg
= of_get_property(np
, "reg", NULL
);
298 if (reg
&& (be32_to_cpup(reg
) < 4))
299 index
= lindex
= be32_to_cpup(reg
);
302 /* Local index means it's the Nth port in the PCI chip. Unfortunately
303 * the offset to add here is device specific. We know about those
304 * EXAR ports and we default to the most common case. If your UART
305 * doesn't work for these settings, you'll have to add your own special
308 if (of_device_is_compatible(pci_dev
, "pci13a8,152") ||
309 of_device_is_compatible(pci_dev
, "pci13a8,154") ||
310 of_device_is_compatible(pci_dev
, "pci13a8,158")) {
311 addr
+= 0x200 * lindex
;
312 base
+= 0x200 * lindex
;
318 /* Add port, irq will be dealt with later. We passed a translated
319 * IO port value. It will be fixed up later along with the irq
321 return add_legacy_port(np
, index
, iotype
, base
, addr
, 0,
322 legacy_port_flags
, np
!= pci_dev
);
326 static void __init
setup_legacy_serial_console(int console
)
328 struct legacy_serial_info
*info
= &legacy_serial_infos
[console
];
329 struct plat_serial8250_port
*port
= &legacy_serial_ports
[console
];
332 stride
= 1 << port
->regshift
;
334 /* Check if a translated MMIO address has been found */
336 info
->early_addr
= early_ioremap(info
->taddr
, 0x1000);
337 if (info
->early_addr
== NULL
)
339 udbg_uart_init_mmio(info
->early_addr
, stride
);
341 /* Check if it's PIO and we support untranslated PIO */
342 if (port
->iotype
== UPIO_PORT
&& isa_io_special
)
343 udbg_uart_init_pio(port
->iobase
, stride
);
348 /* Try to query the current speed */
349 if (info
->speed
== 0)
350 info
->speed
= udbg_probe_uart_speed(info
->clock
);
353 DBG("default console speed = %d\n", info
->speed
);
354 udbg_uart_setup(info
->speed
, info
->clock
);
357 static int __init
ioremap_legacy_serial_console(void)
359 struct plat_serial8250_port
*port
;
360 struct legacy_serial_info
*info
;
363 if (legacy_serial_console
< 0)
366 info
= &legacy_serial_infos
[legacy_serial_console
];
367 port
= &legacy_serial_ports
[legacy_serial_console
];
369 if (!info
->early_addr
)
372 vaddr
= ioremap(info
->taddr
, 0x1000);
376 udbg_uart_init_mmio(vaddr
, 1 << port
->regshift
);
377 early_iounmap(info
->early_addr
, 0x1000);
378 info
->early_addr
= NULL
;
382 early_initcall(ioremap_legacy_serial_console
);
385 * This is called very early, as part of setup_system() or eventually
386 * setup_arch(), basically before anything else in this file. This function
387 * will try to build a list of all the available 8250-compatible serial ports
388 * in the machine using the Open Firmware device-tree. It currently only deals
389 * with ISA and PCI busses but could be extended. It allows a very early boot
390 * console to be initialized, that list is also used later to provide 8250 with
391 * the machine non-PCI ports and to properly pick the default console port
393 void __init
find_legacy_serial_ports(void)
395 struct device_node
*np
, *stdout
= NULL
;
399 DBG(" -> find_legacy_serial_port()\n");
401 /* Now find out if one of these is out firmware console */
402 path
= of_get_property(of_chosen
, "linux,stdout-path", NULL
);
404 path
= of_get_property(of_chosen
, "stdout-path", NULL
);
406 stdout
= of_find_node_by_path(path
);
408 DBG("stdout is %pOF\n", stdout
);
410 DBG(" no linux,stdout-path !\n");
413 /* Iterate over all the 16550 ports, looking for known parents */
414 for_each_compatible_node(np
, "serial", "ns16550") {
415 struct device_node
*parent
= of_get_parent(np
);
418 if (of_match_node(legacy_serial_parents
, parent
) != NULL
) {
419 if (of_device_is_available(np
)) {
420 index
= add_legacy_soc_port(np
, np
);
421 if (index
>= 0 && np
== stdout
)
422 legacy_serial_console
= index
;
428 /* Next, fill our array with ISA ports */
429 for_each_node_by_type(np
, "serial") {
430 struct device_node
*isa
= of_get_parent(np
);
431 if (of_node_name_eq(isa
, "isa") || of_node_name_eq(isa
, "lpc")) {
432 if (of_device_is_available(np
)) {
433 index
= add_legacy_isa_port(np
, isa
);
434 if (index
>= 0 && np
== stdout
)
435 legacy_serial_console
= index
;
442 /* Next, try to locate PCI ports */
443 for (np
= NULL
; (np
= of_find_all_nodes(np
));) {
444 struct device_node
*pci
, *parent
= of_get_parent(np
);
445 if (of_node_name_eq(parent
, "isa")) {
449 if (!of_node_name_eq(np
, "serial") &&
450 !of_node_is_type(np
, "serial")) {
454 /* Check for known pciclass, and also check whether we have
455 * a device with child nodes for ports or not
457 if (of_device_is_compatible(np
, "pciclass,0700") ||
458 of_device_is_compatible(np
, "pciclass,070002"))
460 else if (of_device_is_compatible(parent
, "pciclass,0700") ||
461 of_device_is_compatible(parent
, "pciclass,070002"))
467 index
= add_legacy_pci_port(np
, pci
);
468 if (index
>= 0 && np
== stdout
)
469 legacy_serial_console
= index
;
476 DBG("legacy_serial_console = %d\n", legacy_serial_console
);
477 if (legacy_serial_console
>= 0)
478 setup_legacy_serial_console(legacy_serial_console
);
479 DBG(" <- find_legacy_serial_port()\n");
482 static struct platform_device serial_device
= {
483 .name
= "serial8250",
484 .id
= PLAT8250_DEV_PLATFORM
,
486 .platform_data
= legacy_serial_ports
,
490 static void __init
fixup_port_irq(int index
,
491 struct device_node
*np
,
492 struct plat_serial8250_port
*port
)
496 DBG("fixup_port_irq(%d)\n", index
);
498 virq
= irq_of_parse_and_map(np
, 0);
499 if (!virq
&& legacy_serial_infos
[index
].irq_check_parent
) {
500 np
= of_get_parent(np
);
503 virq
= irq_of_parse_and_map(np
, 0);
511 if (IS_ENABLED(CONFIG_SERIAL_8250
) &&
512 of_device_is_compatible(np
, "fsl,ns16550")) {
513 if (IS_REACHABLE(CONFIG_SERIAL_8250_FSL
)) {
514 port
->handle_irq
= fsl8250_handle_irq
;
515 port
->has_sysrq
= IS_ENABLED(CONFIG_SERIAL_8250_CONSOLE
);
517 pr_warn_once("Not activating Freescale specific workaround for device %pOFP\n",
523 static void __init
fixup_port_pio(int index
,
524 struct device_node
*np
,
525 struct plat_serial8250_port
*port
)
528 struct pci_controller
*hose
;
530 DBG("fixup_port_pio(%d)\n", index
);
532 hose
= pci_find_hose_for_OF_device(np
);
534 unsigned long offset
= (unsigned long)hose
->io_base_virt
-
540 DBG("port %d, IO %lx -> %lx\n",
541 index
, port
->iobase
, port
->iobase
+ offset
);
542 port
->iobase
+= offset
;
547 static void __init
fixup_port_mmio(int index
,
548 struct device_node
*np
,
549 struct plat_serial8250_port
*port
)
551 DBG("fixup_port_mmio(%d)\n", index
);
553 port
->membase
= ioremap(port
->mapbase
, 0x100);
557 * This is called as an arch initcall, hopefully before the PCI bus is
558 * probed and/or the 8250 driver loaded since we need to register our
559 * platform devices before 8250 PCI ones are detected as some of them
560 * must properly "override" the platform ones.
562 * This function fixes up the interrupt value for platform ports as it
563 * couldn't be done earlier before interrupt maps have been parsed. It
564 * also "corrects" the IO address for PIO ports for the same reason,
565 * since earlier, the PHBs virtual IO space wasn't assigned yet. It then
566 * registers all those platform ports for use by the 8250 driver when it
569 static int __init
serial_dev_init(void)
573 if (legacy_serial_count
== 0)
577 * Before we register the platform serial devices, we need
578 * to fixup their interrupts and their IO ports.
580 DBG("Fixing serial ports interrupts and IO ports ...\n");
582 for (i
= 0; i
< legacy_serial_count
; i
++) {
583 struct plat_serial8250_port
*port
= &legacy_serial_ports
[i
];
584 struct device_node
*np
= legacy_serial_infos
[i
].np
;
587 fixup_port_irq(i
, np
, port
);
588 if (port
->iotype
== UPIO_PORT
)
589 fixup_port_pio(i
, np
, port
);
590 if ((port
->iotype
== UPIO_MEM
) || (port
->iotype
== UPIO_TSI
))
591 fixup_port_mmio(i
, np
, port
);
594 DBG("Registering platform serial ports\n");
596 return platform_device_register(&serial_device
);
598 device_initcall(serial_dev_init
);
601 #ifdef CONFIG_SERIAL_8250_CONSOLE
603 * This is called very early, as part of console_init() (typically just after
604 * time_init()). This function is respondible for trying to find a good
605 * default console on serial ports. It tries to match the open firmware
606 * default output with one of the available serial console drivers that have
607 * been probed earlier by find_legacy_serial_ports()
609 static int __init
check_legacy_serial_console(void)
611 struct device_node
*prom_stdout
= NULL
;
612 int i
, speed
= 0, offset
= 0;
616 DBG(" -> check_legacy_serial_console()\n");
618 /* The user has requested a console so this is already set up. */
619 if (strstr(boot_command_line
, "console=")) {
620 DBG(" console was specified !\n");
625 DBG(" of_chosen is NULL !\n");
629 if (legacy_serial_console
< 0) {
630 DBG(" legacy_serial_console not found !\n");
633 /* We are getting a weird phandle from OF ... */
634 /* ... So use the full path instead */
635 name
= of_get_property(of_chosen
, "linux,stdout-path", NULL
);
637 name
= of_get_property(of_chosen
, "stdout-path", NULL
);
639 DBG(" no stdout-path !\n");
642 prom_stdout
= of_find_node_by_path(name
);
644 DBG(" can't find stdout package %s !\n", name
);
647 DBG("stdout is %pOF\n", prom_stdout
);
649 name
= of_get_property(prom_stdout
, "name", NULL
);
651 DBG(" stdout package has no name !\n");
654 spd
= of_get_property(prom_stdout
, "current-speed", NULL
);
656 speed
= be32_to_cpup(spd
);
658 if (strcmp(name
, "serial") != 0)
661 /* Look for it in probed array */
662 for (i
= 0; i
< legacy_serial_count
; i
++) {
663 if (prom_stdout
!= legacy_serial_infos
[i
].np
)
666 speed
= legacy_serial_infos
[i
].speed
;
669 if (i
>= legacy_serial_count
)
672 of_node_put(prom_stdout
);
674 DBG("Found serial console at ttyS%d\n", offset
);
677 static char __initdata opt
[16];
678 sprintf(opt
, "%d", speed
);
679 return add_preferred_console("ttyS", offset
, opt
);
681 return add_preferred_console("ttyS", offset
, NULL
);
684 DBG("No preferred console found !\n");
685 of_node_put(prom_stdout
);
688 console_initcall(check_legacy_serial_console
);
690 #endif /* CONFIG_SERIAL_8250_CONSOLE */