9d22f6f9e1ef654fd80d7b743fbe7883ed6363ab
[openwrt/staging/jow.git] / target / linux / rtl838x / files-5.4 / arch / mips / rtl838x / serial.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * 8250 serial console setup for the Realtek RTL838X SoC
4 *
5 * based on the original BSP by
6 * Copyright (C) 2006-2012 Tony Wu (tonywu@realtek.com)
7 *
8 * Copyright (C) 2020 B. Koblitz
9 *
10 */
11
12 #include <linux/types.h>
13 #include <linux/ctype.h>
14 #include <linux/kernel.h>
15 #include <linux/init.h>
16 #include <linux/version.h>
17 #include <linux/serial.h>
18 #include <linux/serial_core.h>
19 #include <linux/serial_8250.h>
20 #include <linux/serial_reg.h>
21 #include <linux/tty.h>
22 #include <linux/clk.h>
23
24 #include <asm/mach-rtl838x/mach-rtl838x.h>
25
26 extern char arcs_cmdline[];
27
28 int __init rtl838x_serial_init(void)
29 {
30 #ifdef CONFIG_SERIAL_8250
31 int ret;
32 struct uart_port p;
33 unsigned long baud = 0;
34 int err;
35 char parity = '\0', bits = '\0', flow = '\0';
36 char *s;
37 struct device_node *dn;
38
39 dn = of_find_compatible_node(NULL, NULL, "ns16550a");
40 if (dn) {
41 pr_info("Found NS16550a: %s (%s)\n", dn->name, dn->full_name);
42 dn = of_find_compatible_node(dn, NULL, "ns16550a");
43 if (dn && of_device_is_available(dn) && soc_info.family == RTL8380_FAMILY_ID) {
44 /* Enable UART1 on RTL838x */
45 pr_info("Enabling uart1\n");
46 sw_w32(0x10, RTL838X_GMII_INTF_SEL);
47 }
48 } else {
49 pr_err("No NS16550a UART found!");
50 return -ENODEV;
51 }
52
53 s = strstr(arcs_cmdline, "console=ttyS0,");
54 if (s) {
55 s += 14;
56 err = kstrtoul(s, 10, &baud);
57 if (err)
58 baud = 0;
59 while (isdigit(*s))
60 s++;
61 if (*s == ',')
62 s++;
63 if (*s)
64 parity = *s++;
65 if (*s == ',')
66 s++;
67 if (*s)
68 bits = *s++;
69 if (*s == ',')
70 s++;
71 if (*s == 'h')
72 flow = 'r';
73 }
74
75 if (baud == 0) {
76 baud = 38400;
77 pr_warn("Using default baud rate: %lu\n", baud);
78 }
79 if (parity != 'n' && parity != 'o' && parity != 'e')
80 parity = 'n';
81 if (bits != '7' && bits != '8')
82 bits = '8';
83
84 memset(&p, 0, sizeof(p));
85
86 p.type = PORT_16550A;
87 p.membase = (unsigned char *) RTL838X_UART0_BASE;
88 p.irq = RTL838X_UART0_IRQ;
89 p.uartclk = SYSTEM_FREQ - (24 * baud);
90 p.flags = UPF_SKIP_TEST | UPF_LOW_LATENCY | UPF_FIXED_TYPE;
91 p.iotype = UPIO_MEM;
92 p.regshift = 2;
93 p.fifosize = 1;
94
95 /* Call early_serial_setup() here, to set up 8250 console driver */
96 if (early_serial_setup(&p) != 0)
97 ret = 1;
98 #endif
99 return 0;
100 }