2 * Joshua Henderson <joshua.henderson@microchip.com>
3 * Copyright (C) 2015 Microchip Technology Inc. All rights reserved.
5 * This program is free software; you can distribute it and/or modify it
6 * under the terms of the GNU General Public License (Version 2) as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 #include <asm/mach-pic32/pic32.h>
15 #include <asm/fw/fw.h>
16 #include <asm/setup.h>
18 #include "pic32mzda.h"
19 #include "early_pin.h"
21 /* Default early console parameters */
22 #define EARLY_CONSOLE_PORT 1
23 #define EARLY_CONSOLE_BAUDRATE 115200
25 #define UART_ENABLE BIT(15)
26 #define UART_ENABLE_RX BIT(12)
27 #define UART_ENABLE_TX BIT(10)
28 #define UART_TX_FULL BIT(9)
30 /* UART1(x == 0) - UART6(x == 5) */
31 #define UART_BASE(x) ((x) * 0x0200)
32 #define U_MODE(x) UART_BASE(x)
33 #define U_STA(x) (UART_BASE(x) + 0x10)
34 #define U_TXR(x) (UART_BASE(x) + 0x20)
35 #define U_BRG(x) (UART_BASE(x) + 0x40)
37 static void __iomem *uart_base;
38 static char console_port = -1;
40 static int __init configure_uart_pins(int port)
44 pic32_pps_input(IN_FUNC_U2RX, IN_RPB0);
45 pic32_pps_output(OUT_FUNC_U2TX, OUT_RPG9);
48 pic32_pps_input(IN_FUNC_U6RX, IN_RPD0);
49 pic32_pps_output(OUT_FUNC_U6TX, OUT_RPB8);
58 static void __init configure_uart(char port, int baud)
62 pbclk = pic32_get_pbclk(2);
64 __raw_writel(0, uart_base + U_MODE(port));
65 __raw_writel(((pbclk / baud) / 16) - 1, uart_base + U_BRG(port));
66 __raw_writel(UART_ENABLE, uart_base + U_MODE(port));
67 __raw_writel(UART_ENABLE_TX | UART_ENABLE_RX,
68 uart_base + PIC32_SET(U_STA(port)));
71 static void __init setup_early_console(char port, int baud)
73 if (configure_uart_pins(port))
77 configure_uart(console_port, baud);
80 static char * __init pic32_getcmdline(void)
83 * arch_mem_init() has not been called yet, so we don't have a real
84 * command line setup if using CONFIG_CMDLINE_BOOL.
86 #ifdef CONFIG_CMDLINE_OVERRIDE
87 return CONFIG_CMDLINE;
89 return fw_getcmdline();
93 static int __init get_port_from_cmdline(char *arch_cmdline)
98 if (!arch_cmdline || *arch_cmdline == '\0')
101 s = strstr(arch_cmdline, "earlyprintk=");
103 s = strstr(s, "ttyS");
116 static int __init get_baud_from_cmdline(char *arch_cmdline)
121 if (!arch_cmdline || *arch_cmdline == '\0')
124 s = strstr(arch_cmdline, "earlyprintk=");
126 s = strstr(s, "ttyS");
133 while (*s >= '0' && *s <= '9')
134 baud = baud * 10 + *s++ - '0';
141 void __init fw_init_early_console(char port)
143 char *arch_cmdline = pic32_getcmdline();
146 uart_base = ioremap_nocache(PIC32_BASE_UART, 0xc00);
148 baud = get_baud_from_cmdline(arch_cmdline);
150 port = get_port_from_cmdline(arch_cmdline);
153 port = EARLY_CONSOLE_PORT;
156 baud = EARLY_CONSOLE_BAUDRATE;
158 setup_early_console(port, baud);
161 void prom_putchar(char c)
163 if (console_port >= 0) {
165 uart_base + U_STA(console_port)) & UART_TX_FULL)
168 __raw_writel(c, uart_base + U_TXR(console_port));