• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Earlyprintk support.
3  *
4  * Copyright (C) 2012 ARM Ltd.
5  * Author: Catalin Marinas <catalin.marinas@arm.com>
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 #include <linux/kernel.h>
20 #include <linux/console.h>
21 #include <linux/init.h>
22 #include <linux/string.h>
23 #include <linux/mm.h>
24 #include <linux/io.h>
25 
26 #include <linux/amba/serial.h>
27 #include <linux/serial_reg.h>
28 
29 #include <asm/fixmap.h>
30 
31 #define UART_USR       31      /* I/O: uart status Register */
32 #define UART_USR_NF    0x02    /* Tansmit fifo not full */
33 
34 static void __iomem *early_base;
35 static void (*printch)(char ch);
36 
37 /*
38  * PL011 single character TX.
39  */
pl011_printch(char ch)40 static void pl011_printch(char ch)
41 {
42 	while (readl_relaxed(early_base + UART01x_FR) & UART01x_FR_TXFF)
43 		;
44 	writeb_relaxed(ch, early_base + UART01x_DR);
45 	while (readl_relaxed(early_base + UART01x_FR) & UART01x_FR_BUSY)
46 		;
47 }
48 
49 /*
50  * Semihosting-based debug console
51  */
smh_printch(char ch)52 static void smh_printch(char ch)
53 {
54 	asm volatile("mov  x1, %0\n"
55 		     "mov  x0, #3\n"
56 		     "hlt  0xf000\n"
57 		     : : "r" (&ch) : "x0", "x1", "memory");
58 }
59 
60 /*
61  * 8250/16550 (8-bit aligned registers) single character TX.
62  */
uart8250_8bit_printch(char ch)63 static void uart8250_8bit_printch(char ch)
64 {
65 	while (!(readb_relaxed(early_base + UART_LSR) & UART_LSR_THRE))
66 		;
67 	writeb_relaxed(ch, early_base + UART_TX);
68 }
69 
70 /*
71  * 8250/16550 (32-bit aligned registers) single character TX.
72  */
uart8250_32bit_printch(char ch)73 static void uart8250_32bit_printch(char ch)
74 {
75 	while (!(readl_relaxed(early_base + (UART_LSR << 2)) & UART_LSR_THRE))
76 		;
77 	writel_relaxed(ch, early_base + (UART_TX << 2));
78 }
79 
80 /*
81  * sunxi-uart single character TX.
82  */
sunxi_uart_printch(char ch)83 static void sunxi_uart_printch(char ch)
84 {
85 
86 	while (!(readl_relaxed(early_base + (UART_USR << 2)) & UART_USR_NF))
87 		;
88 
89 	writel_relaxed(ch, early_base + (UART_TX << 2));
90 }
91 
92 struct earlycon_match {
93 	const char *name;
94 	void (*printch)(char ch);
95 };
96 
97 static const struct earlycon_match earlycon_match[] __initconst = {
98 	{ .name = "pl011", .printch = pl011_printch, },
99 	{ .name = "smh", .printch = smh_printch, },
100 	{ .name = "uart8250-8bit", .printch = uart8250_8bit_printch, },
101 	{ .name = "uart8250-32bit", .printch = uart8250_32bit_printch, },
102 	{ .name = "sunxi-uart", .printch = sunxi_uart_printch, },
103 	{}
104 };
105 
early_write(struct console * con,const char * s,unsigned n)106 static void early_write(struct console *con, const char *s, unsigned n)
107 {
108 	while (n-- > 0) {
109 		if (*s == '\n')
110 			printch('\r');
111 		printch(*s);
112 		s++;
113 	}
114 }
115 
116 static struct console early_console_dev = {
117 	.name =		"earlycon",
118 	.write =	early_write,
119 	.flags =	CON_PRINTBUFFER | CON_BOOT,
120 	.index =	-1,
121 };
122 
123 /*
124  * Parse earlyprintk=... parameter in the format:
125  *
126  *   <name>[,<addr>][,<options>]
127  *
128  * and register the early console. It is assumed that the UART has been
129  * initialised by the bootloader already.
130  */
setup_early_printk(char * buf)131 static int __init setup_early_printk(char *buf)
132 {
133 	const struct earlycon_match *match = earlycon_match;
134 	phys_addr_t paddr = 0;
135 	char *e;
136 
137 	if (!buf) {
138 		pr_warn("No earlyprintk arguments passed.\n");
139 		return 0;
140 	}
141 
142 	while (match->name) {
143 		size_t len = strlen(match->name);
144 		if (!strncmp(buf, match->name, len)) {
145 			buf += len;
146 			break;
147 		}
148 		match++;
149 	}
150 	if (!match->name) {
151 		pr_warn("Unknown earlyprintk arguments: %s\n", buf);
152 		return 0;
153 	}
154 
155 	/* I/O address */
156 	if (!strncmp(buf, ",0x", 3)) {
157 		paddr = simple_strtoul(buf + 1, &e, 16);
158 	}
159 	/* no options parsing yet */
160 
161 	if (paddr) {
162 		set_fixmap_io(FIX_EARLYCON_MEM_BASE, paddr);
163 		early_base = (void __iomem *)(fix_to_virt(FIX_EARLYCON_MEM_BASE)
164 						|(paddr & (PAGE_SIZE - 1)));
165 	}
166 	printch = match->printch;
167 	early_console = &early_console_dev;
168 	register_console(&early_console_dev);
169 
170 	return 0;
171 }
172 
173 early_param("earlyprintk", setup_early_printk);
174