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>
17 #include "pic32mzda.h"
18 #include "early_pin.h"
20 /* Default early console parameters */
21 #define EARLY_CONSOLE_PORT 1
22 #define EARLY_CONSOLE_BAUDRATE 115200
24 #define UART_ENABLE BIT(15)
25 #define UART_ENABLE_RX BIT(12)
26 #define UART_ENABLE_TX BIT(10)
27 #define UART_TX_FULL BIT(9)
29 /* UART1(x == 0) - UART6(x == 5) */
30 #define UART_BASE(x) ((x) * 0x0200)
31 #define U_MODE(x) UART_BASE(x)
32 #define U_STA(x) (UART_BASE(x) + 0x10)
33 #define U_TXR(x) (UART_BASE(x) + 0x20)
34 #define U_BRG(x) (UART_BASE(x) + 0x40)
36 static void __iomem
*uart_base
;
37 static char console_port
= -1;
39 static int __init
configure_uart_pins(int port
)
43 pic32_pps_input(IN_FUNC_U2RX
, IN_RPB0
);
44 pic32_pps_output(OUT_FUNC_U2TX
, OUT_RPG9
);
47 pic32_pps_input(IN_FUNC_U6RX
, IN_RPD0
);
48 pic32_pps_output(OUT_FUNC_U6TX
, OUT_RPB8
);
57 static void __init
configure_uart(char port
, int baud
)
61 pbclk
= pic32_get_pbclk(2);
63 __raw_writel(0, uart_base
+ U_MODE(port
));
64 __raw_writel(((pbclk
/ baud
) / 16) - 1, uart_base
+ U_BRG(port
));
65 __raw_writel(UART_ENABLE
, uart_base
+ U_MODE(port
));
66 __raw_writel(UART_ENABLE_TX
| UART_ENABLE_RX
,
67 uart_base
+ PIC32_SET(U_STA(port
)));
70 static void __init
setup_early_console(char port
, int baud
)
72 if (configure_uart_pins(port
))
76 configure_uart(console_port
, baud
);
79 static char * __init
pic32_getcmdline(void)
82 * arch_mem_init() has not been called yet, so we don't have a real
83 * command line setup if using CONFIG_CMDLINE_BOOL.
85 #ifdef CONFIG_CMDLINE_OVERRIDE
86 return CONFIG_CMDLINE
;
88 return fw_getcmdline();
92 static int __init
get_port_from_cmdline(char *arch_cmdline
)
97 if (!arch_cmdline
|| *arch_cmdline
== '\0')
100 s
= strstr(arch_cmdline
, "earlyprintk=");
102 s
= strstr(s
, "ttyS");
115 static int __init
get_baud_from_cmdline(char *arch_cmdline
)
120 if (!arch_cmdline
|| *arch_cmdline
== '\0')
123 s
= strstr(arch_cmdline
, "earlyprintk=");
125 s
= strstr(s
, "ttyS");
132 while (*s
>= '0' && *s
<= '9')
133 baud
= baud
* 10 + *s
++ - '0';
140 void __init
fw_init_early_console(char port
)
142 char *arch_cmdline
= pic32_getcmdline();
145 uart_base
= ioremap_nocache(PIC32_BASE_UART
, 0xc00);
147 baud
= get_baud_from_cmdline(arch_cmdline
);
149 port
= get_port_from_cmdline(arch_cmdline
);
152 port
= EARLY_CONSOLE_PORT
;
155 baud
= EARLY_CONSOLE_BAUDRATE
;
157 setup_early_console(port
, baud
);
160 int prom_putchar(char c
)
162 if (console_port
>= 0) {
164 uart_base
+ U_STA(console_port
)) & UART_TX_FULL
)
167 __raw_writel(c
, uart_base
+ U_TXR(console_port
));