1 // SPDX-License-Identifier: GPL-2.0-only
3 * Joshua Henderson <joshua.henderson@microchip.com>
4 * Copyright (C) 2015 Microchip Technology Inc. All rights reserved.
6 #include <asm/mach-pic32/pic32.h>
10 #include "pic32mzda.h"
11 #include "early_pin.h"
13 /* Default early console parameters */
14 #define EARLY_CONSOLE_PORT 1
15 #define EARLY_CONSOLE_BAUDRATE 115200
17 #define UART_ENABLE BIT(15)
18 #define UART_ENABLE_RX BIT(12)
19 #define UART_ENABLE_TX BIT(10)
20 #define UART_TX_FULL BIT(9)
22 /* UART1(x == 0) - UART6(x == 5) */
23 #define UART_BASE(x) ((x) * 0x0200)
24 #define U_MODE(x) UART_BASE(x)
25 #define U_STA(x) (UART_BASE(x) + 0x10)
26 #define U_TXR(x) (UART_BASE(x) + 0x20)
27 #define U_BRG(x) (UART_BASE(x) + 0x40)
29 static void __iomem *uart_base;
30 static int console_port = -1;
32 static int __init configure_uart_pins(int port)
36 pic32_pps_input(IN_FUNC_U2RX, IN_RPB0);
37 pic32_pps_output(OUT_FUNC_U2TX, OUT_RPG9);
40 pic32_pps_input(IN_FUNC_U6RX, IN_RPD0);
41 pic32_pps_output(OUT_FUNC_U6TX, OUT_RPB8);
50 static void __init configure_uart(int port, int baud)
54 pbclk = pic32_get_pbclk(2);
56 __raw_writel(0, uart_base + U_MODE(port));
57 __raw_writel(((pbclk / baud) / 16) - 1, uart_base + U_BRG(port));
58 __raw_writel(UART_ENABLE, uart_base + U_MODE(port));
59 __raw_writel(UART_ENABLE_TX | UART_ENABLE_RX,
60 uart_base + PIC32_SET(U_STA(port)));
63 static void __init setup_early_console(int port, int baud)
65 if (configure_uart_pins(port))
69 configure_uart(console_port, baud);
72 static char * __init pic32_getcmdline(void)
75 * arch_mem_init() has not been called yet, so we don't have a real
76 * command line setup if using CONFIG_CMDLINE_BOOL.
78 #ifdef CONFIG_CMDLINE_OVERRIDE
79 return CONFIG_CMDLINE;
81 return fw_getcmdline();
85 static int __init get_port_from_cmdline(char *arch_cmdline)
90 if (!arch_cmdline || *arch_cmdline == '\0')
93 s = strstr(arch_cmdline, "earlyprintk=");
95 s = strstr(s, "ttyS");
108 static int __init get_baud_from_cmdline(char *arch_cmdline)
113 if (!arch_cmdline || *arch_cmdline == '\0')
116 s = strstr(arch_cmdline, "earlyprintk=");
118 s = strstr(s, "ttyS");
125 while (*s >= '0' && *s <= '9')
126 baud = baud * 10 + *s++ - '0';
133 void __init fw_init_early_console(void)
135 char *arch_cmdline = pic32_getcmdline();
138 uart_base = ioremap(PIC32_BASE_UART, 0xc00);
140 baud = get_baud_from_cmdline(arch_cmdline);
141 port = get_port_from_cmdline(arch_cmdline);
144 port = EARLY_CONSOLE_PORT;
147 baud = EARLY_CONSOLE_BAUDRATE;
149 setup_early_console(port, baud);
152 void prom_putchar(char c)
154 if (console_port >= 0) {
156 uart_base + U_STA(console_port)) & UART_TX_FULL)
159 __raw_writel(c, uart_base + U_TXR(console_port));