xref: /linux/arch/mips/pic32/pic32mzda/early_console.c (revision 7ae9fb1b7ecbb5d85d07857943f677fd1a559b18)
104dc82e1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
22572f00dSJoshua Henderson /*
32572f00dSJoshua Henderson  * Joshua Henderson <joshua.henderson@microchip.com>
42572f00dSJoshua Henderson  * Copyright (C) 2015 Microchip Technology Inc.  All rights reserved.
52572f00dSJoshua Henderson  */
62572f00dSJoshua Henderson #include <asm/mach-pic32/pic32.h>
72572f00dSJoshua Henderson #include <asm/fw/fw.h>
85c93316cSAlexander Sverdlin #include <asm/setup.h>
92572f00dSJoshua Henderson 
102572f00dSJoshua Henderson #include "pic32mzda.h"
112572f00dSJoshua Henderson #include "early_pin.h"
122572f00dSJoshua Henderson 
132572f00dSJoshua Henderson /* Default early console parameters */
142572f00dSJoshua Henderson #define EARLY_CONSOLE_PORT	1
152572f00dSJoshua Henderson #define EARLY_CONSOLE_BAUDRATE	115200
162572f00dSJoshua Henderson 
172572f00dSJoshua Henderson #define UART_ENABLE		BIT(15)
182572f00dSJoshua Henderson #define UART_ENABLE_RX		BIT(12)
192572f00dSJoshua Henderson #define UART_ENABLE_TX		BIT(10)
202572f00dSJoshua Henderson #define UART_TX_FULL		BIT(9)
212572f00dSJoshua Henderson 
222572f00dSJoshua Henderson /* UART1(x == 0) - UART6(x == 5) */
232572f00dSJoshua Henderson #define UART_BASE(x)	((x) * 0x0200)
242572f00dSJoshua Henderson #define U_MODE(x)	UART_BASE(x)
252572f00dSJoshua Henderson #define U_STA(x)	(UART_BASE(x) + 0x10)
262572f00dSJoshua Henderson #define U_TXR(x)	(UART_BASE(x) + 0x20)
272572f00dSJoshua Henderson #define U_BRG(x)	(UART_BASE(x) + 0x40)
282572f00dSJoshua Henderson 
292572f00dSJoshua Henderson static void __iomem *uart_base;
30*64806090SJason A. Donenfeld static int console_port = -1;
312572f00dSJoshua Henderson 
configure_uart_pins(int port)322572f00dSJoshua Henderson static int __init configure_uart_pins(int port)
332572f00dSJoshua Henderson {
342572f00dSJoshua Henderson 	switch (port) {
352572f00dSJoshua Henderson 	case 1:
362572f00dSJoshua Henderson 		pic32_pps_input(IN_FUNC_U2RX, IN_RPB0);
372572f00dSJoshua Henderson 		pic32_pps_output(OUT_FUNC_U2TX, OUT_RPG9);
382572f00dSJoshua Henderson 		break;
392572f00dSJoshua Henderson 	case 5:
402572f00dSJoshua Henderson 		pic32_pps_input(IN_FUNC_U6RX, IN_RPD0);
412572f00dSJoshua Henderson 		pic32_pps_output(OUT_FUNC_U6TX, OUT_RPB8);
422572f00dSJoshua Henderson 		break;
432572f00dSJoshua Henderson 	default:
442572f00dSJoshua Henderson 		return -1;
452572f00dSJoshua Henderson 	}
462572f00dSJoshua Henderson 
472572f00dSJoshua Henderson 	return 0;
482572f00dSJoshua Henderson }
492572f00dSJoshua Henderson 
configure_uart(int port,int baud)50*64806090SJason A. Donenfeld static void __init configure_uart(int port, int baud)
512572f00dSJoshua Henderson {
522572f00dSJoshua Henderson 	u32 pbclk;
532572f00dSJoshua Henderson 
542572f00dSJoshua Henderson 	pbclk = pic32_get_pbclk(2);
552572f00dSJoshua Henderson 
562572f00dSJoshua Henderson 	__raw_writel(0, uart_base + U_MODE(port));
572572f00dSJoshua Henderson 	__raw_writel(((pbclk / baud) / 16) - 1, uart_base + U_BRG(port));
582572f00dSJoshua Henderson 	__raw_writel(UART_ENABLE, uart_base + U_MODE(port));
592572f00dSJoshua Henderson 	__raw_writel(UART_ENABLE_TX | UART_ENABLE_RX,
602572f00dSJoshua Henderson 		     uart_base + PIC32_SET(U_STA(port)));
612572f00dSJoshua Henderson }
622572f00dSJoshua Henderson 
setup_early_console(int port,int baud)63*64806090SJason A. Donenfeld static void __init setup_early_console(int port, int baud)
642572f00dSJoshua Henderson {
652572f00dSJoshua Henderson 	if (configure_uart_pins(port))
662572f00dSJoshua Henderson 		return;
672572f00dSJoshua Henderson 
682572f00dSJoshua Henderson 	console_port = port;
692572f00dSJoshua Henderson 	configure_uart(console_port, baud);
702572f00dSJoshua Henderson }
712572f00dSJoshua Henderson 
pic32_getcmdline(void)722572f00dSJoshua Henderson static char * __init pic32_getcmdline(void)
732572f00dSJoshua Henderson {
742572f00dSJoshua Henderson 	/*
752572f00dSJoshua Henderson 	 * arch_mem_init() has not been called yet, so we don't have a real
762572f00dSJoshua Henderson 	 * command line setup if using CONFIG_CMDLINE_BOOL.
772572f00dSJoshua Henderson 	 */
782572f00dSJoshua Henderson #ifdef CONFIG_CMDLINE_OVERRIDE
792572f00dSJoshua Henderson 	return CONFIG_CMDLINE;
802572f00dSJoshua Henderson #else
812572f00dSJoshua Henderson 	return fw_getcmdline();
822572f00dSJoshua Henderson #endif
832572f00dSJoshua Henderson }
842572f00dSJoshua Henderson 
get_port_from_cmdline(char * arch_cmdline)852572f00dSJoshua Henderson static int __init get_port_from_cmdline(char *arch_cmdline)
862572f00dSJoshua Henderson {
872572f00dSJoshua Henderson 	char *s;
882572f00dSJoshua Henderson 	int port = -1;
892572f00dSJoshua Henderson 
902572f00dSJoshua Henderson 	if (!arch_cmdline || *arch_cmdline == '\0')
912572f00dSJoshua Henderson 		goto _out;
922572f00dSJoshua Henderson 
932572f00dSJoshua Henderson 	s = strstr(arch_cmdline, "earlyprintk=");
942572f00dSJoshua Henderson 	if (s) {
952572f00dSJoshua Henderson 		s = strstr(s, "ttyS");
962572f00dSJoshua Henderson 		if (s)
972572f00dSJoshua Henderson 			s += 4;
982572f00dSJoshua Henderson 		else
992572f00dSJoshua Henderson 			goto _out;
1002572f00dSJoshua Henderson 
1012572f00dSJoshua Henderson 		port = (*s) - '0';
1022572f00dSJoshua Henderson 	}
1032572f00dSJoshua Henderson 
1042572f00dSJoshua Henderson _out:
1052572f00dSJoshua Henderson 	return port;
1062572f00dSJoshua Henderson }
1072572f00dSJoshua Henderson 
get_baud_from_cmdline(char * arch_cmdline)1082572f00dSJoshua Henderson static int __init get_baud_from_cmdline(char *arch_cmdline)
1092572f00dSJoshua Henderson {
1102572f00dSJoshua Henderson 	char *s;
1112572f00dSJoshua Henderson 	int baud = -1;
1122572f00dSJoshua Henderson 
1132572f00dSJoshua Henderson 	if (!arch_cmdline || *arch_cmdline == '\0')
1142572f00dSJoshua Henderson 		goto _out;
1152572f00dSJoshua Henderson 
1162572f00dSJoshua Henderson 	s = strstr(arch_cmdline, "earlyprintk=");
1172572f00dSJoshua Henderson 	if (s) {
1182572f00dSJoshua Henderson 		s = strstr(s, "ttyS");
1192572f00dSJoshua Henderson 		if (s)
1202572f00dSJoshua Henderson 			s += 6;
1212572f00dSJoshua Henderson 		else
1222572f00dSJoshua Henderson 			goto _out;
1232572f00dSJoshua Henderson 
1242572f00dSJoshua Henderson 		baud = 0;
1252572f00dSJoshua Henderson 		while (*s >= '0' && *s <= '9')
1262572f00dSJoshua Henderson 			baud = baud * 10 + *s++ - '0';
1272572f00dSJoshua Henderson 	}
1282572f00dSJoshua Henderson 
1292572f00dSJoshua Henderson _out:
1302572f00dSJoshua Henderson 	return baud;
1312572f00dSJoshua Henderson }
1322572f00dSJoshua Henderson 
fw_init_early_console(void)133*64806090SJason A. Donenfeld void __init fw_init_early_console(void)
1342572f00dSJoshua Henderson {
1352572f00dSJoshua Henderson 	char *arch_cmdline = pic32_getcmdline();
136*64806090SJason A. Donenfeld 	int baud, port;
1372572f00dSJoshua Henderson 
1384bdc0d67SChristoph Hellwig 	uart_base = ioremap(PIC32_BASE_UART, 0xc00);
1392572f00dSJoshua Henderson 
1402572f00dSJoshua Henderson 	baud = get_baud_from_cmdline(arch_cmdline);
1412572f00dSJoshua Henderson 	port = get_port_from_cmdline(arch_cmdline);
1422572f00dSJoshua Henderson 
1432572f00dSJoshua Henderson 	if (port == -1)
1442572f00dSJoshua Henderson 		port = EARLY_CONSOLE_PORT;
1452572f00dSJoshua Henderson 
1462572f00dSJoshua Henderson 	if (baud == -1)
1472572f00dSJoshua Henderson 		baud = EARLY_CONSOLE_BAUDRATE;
1482572f00dSJoshua Henderson 
1492572f00dSJoshua Henderson 	setup_early_console(port, baud);
1502572f00dSJoshua Henderson }
1512572f00dSJoshua Henderson 
prom_putchar(char c)1525c93316cSAlexander Sverdlin void prom_putchar(char c)
1532572f00dSJoshua Henderson {
1542572f00dSJoshua Henderson 	if (console_port >= 0) {
1552572f00dSJoshua Henderson 		while (__raw_readl(
1562572f00dSJoshua Henderson 				uart_base + U_STA(console_port)) & UART_TX_FULL)
1572572f00dSJoshua Henderson 			;
1582572f00dSJoshua Henderson 
1592572f00dSJoshua Henderson 		__raw_writel(c, uart_base + U_TXR(console_port));
1602572f00dSJoshua Henderson 	}
1612572f00dSJoshua Henderson }
162