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>
8 #include <linux/of_address.h>
9 #include <linux/of_device.h>
10 #include <linux/serial_reg.h>
14 #include <asm/serial.h>
16 #include <asm/pci-bridge.h>
17 #include <asm/ppc-pci.h>
22 #define DBG(fmt...) do { printk(fmt); } while(0)
24 #define DBG(fmt...) do { } while(0)
27 #define MAX_LEGACY_SERIAL_PORTS 8
29 static struct plat_serial8250_port
30 legacy_serial_ports
[MAX_LEGACY_SERIAL_PORTS
+1];
31 static struct legacy_serial_info
{
32 struct device_node
*np
;
37 } legacy_serial_infos
[MAX_LEGACY_SERIAL_PORTS
];
39 static const struct of_device_id legacy_serial_parents
[] __initconst
= {
41 {.type
= "tsi-bridge",},
43 {.compatible
= "ibm,opb",},
44 {.compatible
= "simple-bus",},
45 {.compatible
= "wrs,epld-localbus",},
49 static unsigned int legacy_serial_count
;
50 static int legacy_serial_console
= -1;
52 static const upf_t legacy_port_flags
= UPF_BOOT_AUTOCONF
| UPF_SKIP_TEST
|
53 UPF_SHARE_IRQ
| UPF_FIXED_PORT
;
55 static unsigned int tsi_serial_in(struct uart_port
*p
, int offset
)
58 offset
= offset
<< p
->regshift
;
59 if (offset
== UART_IIR
) {
60 tmp
= readl(p
->membase
+ (UART_IIR
& ~3));
61 return (tmp
>> 16) & 0xff; /* UART_IIR % 4 == 2 */
63 return readb(p
->membase
+ offset
);
66 static void tsi_serial_out(struct uart_port
*p
, int offset
, int value
)
68 offset
= offset
<< p
->regshift
;
69 if (!((offset
== UART_IER
) && (value
& UART_IER_UUE
)))
70 writeb(value
, p
->membase
+ offset
);
73 static int __init
add_legacy_port(struct device_node
*np
, int want_index
,
74 int iotype
, phys_addr_t base
,
75 phys_addr_t taddr
, unsigned long irq
,
76 upf_t flags
, int irq_check_parent
)
78 const __be32
*clk
, *spd
, *rs
;
79 u32 clock
= BASE_BAUD
* 16;
83 /* get clock freq. if present */
84 clk
= of_get_property(np
, "clock-frequency", NULL
);
86 clock
= be32_to_cpup(clk
);
88 /* get default speed if present */
89 spd
= of_get_property(np
, "current-speed", NULL
);
91 /* get register shift if present */
92 rs
= of_get_property(np
, "reg-shift", NULL
);
94 shift
= be32_to_cpup(rs
);
96 /* If we have a location index, then try to use it */
97 if (want_index
>= 0 && want_index
< MAX_LEGACY_SERIAL_PORTS
)
100 index
= legacy_serial_count
;
102 /* if our index is still out of range, that mean that
103 * array is full, we could scan for a free slot but that
104 * make little sense to bother, just skip the port
106 if (index
>= MAX_LEGACY_SERIAL_PORTS
)
108 if (index
>= legacy_serial_count
)
109 legacy_serial_count
= index
+ 1;
111 /* Check if there is a port who already claimed our slot */
112 if (legacy_serial_infos
[index
].np
!= NULL
) {
113 /* if we still have some room, move it, else override */
114 if (legacy_serial_count
< MAX_LEGACY_SERIAL_PORTS
) {
115 printk(KERN_DEBUG
"Moved legacy port %d -> %d\n",
116 index
, legacy_serial_count
);
117 legacy_serial_ports
[legacy_serial_count
] =
118 legacy_serial_ports
[index
];
119 legacy_serial_infos
[legacy_serial_count
] =
120 legacy_serial_infos
[index
];
121 legacy_serial_count
++;
123 printk(KERN_DEBUG
"Replacing legacy port %d\n", index
);
127 /* Now fill the entry */
128 memset(&legacy_serial_ports
[index
], 0,
129 sizeof(struct plat_serial8250_port
));
130 if (iotype
== UPIO_PORT
)
131 legacy_serial_ports
[index
].iobase
= base
;
133 legacy_serial_ports
[index
].mapbase
= base
;
135 legacy_serial_ports
[index
].iotype
= iotype
;
136 legacy_serial_ports
[index
].uartclk
= clock
;
137 legacy_serial_ports
[index
].irq
= irq
;
138 legacy_serial_ports
[index
].flags
= flags
;
139 legacy_serial_ports
[index
].regshift
= shift
;
140 legacy_serial_infos
[index
].taddr
= taddr
;
141 legacy_serial_infos
[index
].np
= of_node_get(np
);
142 legacy_serial_infos
[index
].clock
= clock
;
143 legacy_serial_infos
[index
].speed
= spd
? be32_to_cpup(spd
) : 0;
144 legacy_serial_infos
[index
].irq_check_parent
= irq_check_parent
;
146 if (iotype
== UPIO_TSI
) {
147 legacy_serial_ports
[index
].serial_in
= tsi_serial_in
;
148 legacy_serial_ports
[index
].serial_out
= tsi_serial_out
;
151 printk(KERN_DEBUG
"Found legacy serial port %d for %pOF\n",
153 printk(KERN_DEBUG
" %s=%llx, taddr=%llx, irq=%lx, clk=%d, speed=%d\n",
154 (iotype
== UPIO_PORT
) ? "port" : "mem",
155 (unsigned long long)base
, (unsigned long long)taddr
, irq
,
156 legacy_serial_ports
[index
].uartclk
,
157 legacy_serial_infos
[index
].speed
);
162 static int __init
add_legacy_soc_port(struct device_node
*np
,
163 struct device_node
*soc_dev
)
167 struct device_node
*tsi
= of_get_parent(np
);
169 /* We only support ports that have a clock frequency properly
170 * encoded in the device-tree.
172 if (of_get_property(np
, "clock-frequency", NULL
) == NULL
)
175 /* if reg-offset don't try to use it */
176 if ((of_get_property(np
, "reg-offset", NULL
) != NULL
))
179 /* if rtas uses this device, don't try to use it as well */
180 if (of_get_property(np
, "used-by-rtas", NULL
) != NULL
)
183 /* Get the address */
184 addrp
= of_get_address(soc_dev
, 0, NULL
, NULL
);
188 addr
= of_translate_address(soc_dev
, addrp
);
189 if (addr
== OF_BAD_ADDR
)
192 /* Add port, irq will be dealt with later. We passed a translated
193 * IO port value. It will be fixed up later along with the irq
195 if (of_node_is_type(tsi
, "tsi-bridge"))
196 return add_legacy_port(np
, -1, UPIO_TSI
, addr
, addr
,
197 0, legacy_port_flags
, 0);
199 return add_legacy_port(np
, -1, UPIO_MEM
, addr
, addr
,
200 0, legacy_port_flags
, 0);
203 static int __init
add_legacy_isa_port(struct device_node
*np
,
204 struct device_node
*isa_brg
)
211 DBG(" -> add_legacy_isa_port(%pOF)\n", np
);
213 /* Get the ISA port number */
214 reg
= of_get_property(np
, "reg", NULL
);
218 /* Verify it's an IO port, we don't support anything else */
219 if (!(be32_to_cpu(reg
[0]) & 0x00000001))
222 /* Now look for an "ibm,aix-loc" property that gives us ordering
225 typep
= of_get_property(np
, "ibm,aix-loc", NULL
);
227 /* If we have a location index, then use it */
228 if (typep
&& *typep
== 'S')
229 index
= simple_strtol(typep
+1, NULL
, 0) - 1;
231 /* Translate ISA address. If it fails, we still register the port
232 * with no translated address so that it can be picked up as an IO
233 * port later by the serial driver
235 * Note: Don't even try on P8 lpc, we know it's not directly mapped
237 if (!of_device_is_compatible(isa_brg
, "ibm,power8-lpc") ||
238 of_get_property(isa_brg
, "ranges", NULL
)) {
239 taddr
= of_translate_address(np
, reg
);
240 if (taddr
== OF_BAD_ADDR
)
245 /* Add port, irq will be dealt with later */
246 return add_legacy_port(np
, index
, UPIO_PORT
, be32_to_cpu(reg
[1]),
247 taddr
, 0, legacy_port_flags
, 0);
252 static int __init
add_legacy_pci_port(struct device_node
*np
,
253 struct device_node
*pci_dev
)
258 int iotype
, index
= -1, lindex
= 0;
260 DBG(" -> add_legacy_pci_port(%pOF)\n", np
);
262 /* We only support ports that have a clock frequency properly
263 * encoded in the device-tree (that is have an fcode). Anything
264 * else can't be used that early and will be normally probed by
265 * the generic 8250_pci driver later on. The reason is that 8250
266 * compatible UARTs on PCI need all sort of quirks (port offsets
267 * etc...) that this code doesn't know about
269 if (of_get_property(np
, "clock-frequency", NULL
) == NULL
)
272 /* Get the PCI address. Assume BAR 0 */
273 addrp
= of_get_pci_address(pci_dev
, 0, NULL
, &flags
);
277 /* We only support BAR 0 for now */
278 iotype
= (flags
& IORESOURCE_MEM
) ? UPIO_MEM
: UPIO_PORT
;
279 addr
= of_translate_address(pci_dev
, addrp
);
280 if (addr
== OF_BAD_ADDR
)
283 /* Set the IO base to the same as the translated address for MMIO,
284 * or to the domain local IO base for PIO (it will be fixed up later)
286 if (iotype
== UPIO_MEM
)
289 base
= of_read_number(&addrp
[2], 1);
291 /* Try to guess an index... If we have subdevices of the pci dev,
292 * we get to their "reg" property
295 const __be32
*reg
= of_get_property(np
, "reg", NULL
);
296 if (reg
&& (be32_to_cpup(reg
) < 4))
297 index
= lindex
= be32_to_cpup(reg
);
300 /* Local index means it's the Nth port in the PCI chip. Unfortunately
301 * the offset to add here is device specific. We know about those
302 * EXAR ports and we default to the most common case. If your UART
303 * doesn't work for these settings, you'll have to add your own special
306 if (of_device_is_compatible(pci_dev
, "pci13a8,152") ||
307 of_device_is_compatible(pci_dev
, "pci13a8,154") ||
308 of_device_is_compatible(pci_dev
, "pci13a8,158")) {
309 addr
+= 0x200 * lindex
;
310 base
+= 0x200 * lindex
;
316 /* Add port, irq will be dealt with later. We passed a translated
317 * IO port value. It will be fixed up later along with the irq
319 return add_legacy_port(np
, index
, iotype
, base
, addr
, 0,
320 legacy_port_flags
, np
!= pci_dev
);
324 static void __init
setup_legacy_serial_console(int console
)
326 struct legacy_serial_info
*info
= &legacy_serial_infos
[console
];
327 struct plat_serial8250_port
*port
= &legacy_serial_ports
[console
];
331 stride
= 1 << port
->regshift
;
333 /* Check if a translated MMIO address has been found */
335 addr
= ioremap(info
->taddr
, 0x1000);
338 udbg_uart_init_mmio(addr
, stride
);
340 /* Check if it's PIO and we support untranslated PIO */
341 if (port
->iotype
== UPIO_PORT
&& isa_io_special
)
342 udbg_uart_init_pio(port
->iobase
, stride
);
347 /* Try to query the current speed */
348 if (info
->speed
== 0)
349 info
->speed
= udbg_probe_uart_speed(info
->clock
);
352 DBG("default console speed = %d\n", info
->speed
);
353 udbg_uart_setup(info
->speed
, info
->clock
);
357 * This is called very early, as part of setup_system() or eventually
358 * setup_arch(), basically before anything else in this file. This function
359 * will try to build a list of all the available 8250-compatible serial ports
360 * in the machine using the Open Firmware device-tree. It currently only deals
361 * with ISA and PCI busses but could be extended. It allows a very early boot
362 * console to be initialized, that list is also used later to provide 8250 with
363 * the machine non-PCI ports and to properly pick the default console port
365 void __init
find_legacy_serial_ports(void)
367 struct device_node
*np
, *stdout
= NULL
;
371 DBG(" -> find_legacy_serial_port()\n");
373 /* Now find out if one of these is out firmware console */
374 path
= of_get_property(of_chosen
, "linux,stdout-path", NULL
);
376 path
= of_get_property(of_chosen
, "stdout-path", NULL
);
378 stdout
= of_find_node_by_path(path
);
380 DBG("stdout is %pOF\n", stdout
);
382 DBG(" no linux,stdout-path !\n");
385 /* Iterate over all the 16550 ports, looking for known parents */
386 for_each_compatible_node(np
, "serial", "ns16550") {
387 struct device_node
*parent
= of_get_parent(np
);
390 if (of_match_node(legacy_serial_parents
, parent
) != NULL
) {
391 if (of_device_is_available(np
)) {
392 index
= add_legacy_soc_port(np
, np
);
393 if (index
>= 0 && np
== stdout
)
394 legacy_serial_console
= index
;
400 /* Next, fill our array with ISA ports */
401 for_each_node_by_type(np
, "serial") {
402 struct device_node
*isa
= of_get_parent(np
);
403 if (of_node_name_eq(isa
, "isa") || of_node_name_eq(isa
, "lpc")) {
404 if (of_device_is_available(np
)) {
405 index
= add_legacy_isa_port(np
, isa
);
406 if (index
>= 0 && np
== stdout
)
407 legacy_serial_console
= index
;
414 /* Next, try to locate PCI ports */
415 for (np
= NULL
; (np
= of_find_all_nodes(np
));) {
416 struct device_node
*pci
, *parent
= of_get_parent(np
);
417 if (of_node_name_eq(parent
, "isa")) {
421 if (!of_node_name_eq(np
, "serial") &&
422 !of_node_is_type(np
, "serial")) {
426 /* Check for known pciclass, and also check whether we have
427 * a device with child nodes for ports or not
429 if (of_device_is_compatible(np
, "pciclass,0700") ||
430 of_device_is_compatible(np
, "pciclass,070002"))
432 else if (of_device_is_compatible(parent
, "pciclass,0700") ||
433 of_device_is_compatible(parent
, "pciclass,070002"))
439 index
= add_legacy_pci_port(np
, pci
);
440 if (index
>= 0 && np
== stdout
)
441 legacy_serial_console
= index
;
446 DBG("legacy_serial_console = %d\n", legacy_serial_console
);
447 if (legacy_serial_console
>= 0)
448 setup_legacy_serial_console(legacy_serial_console
);
449 DBG(" <- find_legacy_serial_port()\n");
452 static struct platform_device serial_device
= {
453 .name
= "serial8250",
454 .id
= PLAT8250_DEV_PLATFORM
,
456 .platform_data
= legacy_serial_ports
,
460 static void __init
fixup_port_irq(int index
,
461 struct device_node
*np
,
462 struct plat_serial8250_port
*port
)
466 DBG("fixup_port_irq(%d)\n", index
);
468 virq
= irq_of_parse_and_map(np
, 0);
469 if (!virq
&& legacy_serial_infos
[index
].irq_check_parent
) {
470 np
= of_get_parent(np
);
473 virq
= irq_of_parse_and_map(np
, 0);
481 #ifdef CONFIG_SERIAL_8250_FSL
482 if (of_device_is_compatible(np
, "fsl,ns16550")) {
483 port
->handle_irq
= fsl8250_handle_irq
;
484 port
->has_sysrq
= IS_ENABLED(CONFIG_SERIAL_8250_CONSOLE
);
489 static void __init
fixup_port_pio(int index
,
490 struct device_node
*np
,
491 struct plat_serial8250_port
*port
)
494 struct pci_controller
*hose
;
496 DBG("fixup_port_pio(%d)\n", index
);
498 hose
= pci_find_hose_for_OF_device(np
);
500 unsigned long offset
= (unsigned long)hose
->io_base_virt
-
506 DBG("port %d, IO %lx -> %lx\n",
507 index
, port
->iobase
, port
->iobase
+ offset
);
508 port
->iobase
+= offset
;
513 static void __init
fixup_port_mmio(int index
,
514 struct device_node
*np
,
515 struct plat_serial8250_port
*port
)
517 DBG("fixup_port_mmio(%d)\n", index
);
519 port
->membase
= ioremap(port
->mapbase
, 0x100);
523 * This is called as an arch initcall, hopefully before the PCI bus is
524 * probed and/or the 8250 driver loaded since we need to register our
525 * platform devices before 8250 PCI ones are detected as some of them
526 * must properly "override" the platform ones.
528 * This function fixes up the interrupt value for platform ports as it
529 * couldn't be done earlier before interrupt maps have been parsed. It
530 * also "corrects" the IO address for PIO ports for the same reason,
531 * since earlier, the PHBs virtual IO space wasn't assigned yet. It then
532 * registers all those platform ports for use by the 8250 driver when it
535 static int __init
serial_dev_init(void)
539 if (legacy_serial_count
== 0)
543 * Before we register the platform serial devices, we need
544 * to fixup their interrupts and their IO ports.
546 DBG("Fixing serial ports interrupts and IO ports ...\n");
548 for (i
= 0; i
< legacy_serial_count
; i
++) {
549 struct plat_serial8250_port
*port
= &legacy_serial_ports
[i
];
550 struct device_node
*np
= legacy_serial_infos
[i
].np
;
553 fixup_port_irq(i
, np
, port
);
554 if (port
->iotype
== UPIO_PORT
)
555 fixup_port_pio(i
, np
, port
);
556 if ((port
->iotype
== UPIO_MEM
) || (port
->iotype
== UPIO_TSI
))
557 fixup_port_mmio(i
, np
, port
);
560 DBG("Registering platform serial ports\n");
562 return platform_device_register(&serial_device
);
564 device_initcall(serial_dev_init
);
567 #ifdef CONFIG_SERIAL_8250_CONSOLE
569 * This is called very early, as part of console_init() (typically just after
570 * time_init()). This function is respondible for trying to find a good
571 * default console on serial ports. It tries to match the open firmware
572 * default output with one of the available serial console drivers that have
573 * been probed earlier by find_legacy_serial_ports()
575 static int __init
check_legacy_serial_console(void)
577 struct device_node
*prom_stdout
= NULL
;
578 int i
, speed
= 0, offset
= 0;
582 DBG(" -> check_legacy_serial_console()\n");
584 /* The user has requested a console so this is already set up. */
585 if (strstr(boot_command_line
, "console=")) {
586 DBG(" console was specified !\n");
591 DBG(" of_chosen is NULL !\n");
595 if (legacy_serial_console
< 0) {
596 DBG(" legacy_serial_console not found !\n");
599 /* We are getting a weird phandle from OF ... */
600 /* ... So use the full path instead */
601 name
= of_get_property(of_chosen
, "linux,stdout-path", NULL
);
603 name
= of_get_property(of_chosen
, "stdout-path", NULL
);
605 DBG(" no stdout-path !\n");
608 prom_stdout
= of_find_node_by_path(name
);
610 DBG(" can't find stdout package %s !\n", name
);
613 DBG("stdout is %pOF\n", prom_stdout
);
615 name
= of_get_property(prom_stdout
, "name", NULL
);
617 DBG(" stdout package has no name !\n");
620 spd
= of_get_property(prom_stdout
, "current-speed", NULL
);
622 speed
= be32_to_cpup(spd
);
624 if (strcmp(name
, "serial") != 0)
627 /* Look for it in probed array */
628 for (i
= 0; i
< legacy_serial_count
; i
++) {
629 if (prom_stdout
!= legacy_serial_infos
[i
].np
)
632 speed
= legacy_serial_infos
[i
].speed
;
635 if (i
>= legacy_serial_count
)
638 of_node_put(prom_stdout
);
640 DBG("Found serial console at ttyS%d\n", offset
);
643 static char __initdata opt
[16];
644 sprintf(opt
, "%d", speed
);
645 return add_preferred_console("ttyS", offset
, opt
);
647 return add_preferred_console("ttyS", offset
, NULL
);
650 DBG("No preferred console found !\n");
651 of_node_put(prom_stdout
);
654 console_initcall(check_legacy_serial_console
);
656 #endif /* CONFIG_SERIAL_8250_CONSOLE */