blob: 0bb7436c57dce90421e97bd773cf65feb9405800 [file] [log] [blame]
Catalin Marinas2475ff92012-10-23 14:55:08 +01001/*
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
28static void __iomem *early_base;
29static void (*printch)(char ch);
30
31/*
32 * PL011 single character TX.
33 */
34static void pl011_printch(char ch)
35{
36 while (readl_relaxed(early_base + UART01x_FR) & UART01x_FR_TXFF)
37 ;
38 writeb_relaxed(ch, early_base + UART01x_DR);
39 while (readl_relaxed(early_base + UART01x_FR) & UART01x_FR_BUSY)
40 ;
41}
42
Marc Zyngier0492f722013-02-27 18:06:00 +000043/*
44 * Semihosting-based debug console
45 */
46static void smh_printch(char ch)
47{
48 asm volatile("mov x1, %0\n"
49 "mov x0, #3\n"
50 "hlt 0xf000\n"
51 : : "r" (&ch) : "x0", "x1", "memory");
52}
53
Catalin Marinas2475ff92012-10-23 14:55:08 +010054struct earlycon_match {
55 const char *name;
56 void (*printch)(char ch);
57};
58
59static const struct earlycon_match earlycon_match[] __initconst = {
60 { .name = "pl011", .printch = pl011_printch, },
Marc Zyngier0492f722013-02-27 18:06:00 +000061 { .name = "smh", .printch = smh_printch, },
Catalin Marinas2475ff92012-10-23 14:55:08 +010062 {}
63};
64
65static void early_write(struct console *con, const char *s, unsigned n)
66{
67 while (n-- > 0) {
68 if (*s == '\n')
69 printch('\r');
70 printch(*s);
71 s++;
72 }
73}
74
75static struct console early_console = {
76 .name = "earlycon",
77 .write = early_write,
78 .flags = CON_PRINTBUFFER | CON_BOOT,
79 .index = -1,
80};
81
82/*
83 * Parse earlyprintk=... parameter in the format:
84 *
85 * <name>[,<addr>][,<options>]
86 *
87 * and register the early console. It is assumed that the UART has been
88 * initialised by the bootloader already.
89 */
90static int __init setup_early_printk(char *buf)
91{
92 const struct earlycon_match *match = earlycon_match;
93 phys_addr_t paddr = 0;
94
95 if (!buf) {
96 pr_warning("No earlyprintk arguments passed.\n");
97 return 0;
98 }
99
100 while (match->name) {
101 size_t len = strlen(match->name);
102 if (!strncmp(buf, match->name, len)) {
103 buf += len;
104 break;
105 }
106 match++;
107 }
108 if (!match->name) {
109 pr_warning("Unknown earlyprintk arguments: %s\n", buf);
110 return 0;
111 }
112
113 /* I/O address */
114 if (!strncmp(buf, ",0x", 3)) {
115 char *e;
116 paddr = simple_strtoul(buf + 1, &e, 16);
117 buf = e;
118 }
119 /* no options parsing yet */
120
121 if (paddr)
122 early_base = early_io_map(paddr, EARLYCON_IOBASE);
123
124 printch = match->printch;
125 register_console(&early_console);
126
127 return 0;
128}
129
130early_param("earlyprintk", setup_early_printk);