blob: 8f8384e98bf739b331b6839771998930df68dec4 [file] [log] [blame]
Michal Simek89272a52009-03-27 14:25:22 +01001/*
2 * Early printk support for Microblaze.
3 *
4 * Copyright (C) 2007-2009 Michal Simek <monstr@monstr.eu>
5 * Copyright (C) 2007-2009 PetaLogix
6 * Copyright (C) 2003-2006 Yasushi SHOJI <yashi@atmark-techno.com>
7 *
8 * This file is subject to the terms and conditions of the GNU General Public
9 * License. See the file "COPYING" in the main directory of this archive
10 * for more details.
11 */
12
13#include <linux/console.h>
14#include <linux/kernel.h>
15#include <linux/init.h>
16#include <linux/string.h>
17#include <linux/tty.h>
18#include <linux/io.h>
19#include <asm/processor.h>
20#include <linux/fcntl.h>
21#include <asm/setup.h>
22#include <asm/prom.h>
23
24static u32 early_console_initialized;
25static u32 base_addr;
26
Michal Simek51f5fa52010-09-28 16:40:00 +100027#ifdef CONFIG_SERIAL_UARTLITE_CONSOLE
Michal Simek2af9ebe2010-09-28 16:33:53 +100028static void early_printk_uartlite_putc(char c)
Michal Simek89272a52009-03-27 14:25:22 +010029{
30 /*
31 * Limit how many times we'll spin waiting for TX FIFO status.
32 * This will prevent lockups if the base address is incorrectly
33 * set, or any other issue on the UARTLITE.
34 * This limit is pretty arbitrary, unless we are at about 10 baud
35 * we'll never timeout on a working UART.
36 */
37
38 unsigned retries = 10000;
39 /* read status bit - 0x8 offset */
Roel Kluin6e60c142009-04-16 22:49:17 +020040 while (--retries && (in_be32(base_addr + 8) & (1 << 3)))
Michal Simek89272a52009-03-27 14:25:22 +010041 ;
42
43 /* Only attempt the iowrite if we didn't timeout */
44 /* write to TX_FIFO - 0x4 offset */
45 if (retries)
46 out_be32(base_addr + 4, c & 0xff);
47}
48
Michal Simek2af9ebe2010-09-28 16:33:53 +100049static void early_printk_uartlite_write(struct console *unused,
Michal Simek89272a52009-03-27 14:25:22 +010050 const char *s, unsigned n)
51{
52 while (*s && n-- > 0) {
Michal Simek2af9ebe2010-09-28 16:33:53 +100053 early_printk_uartlite_putc(*s);
Michal Simek89272a52009-03-27 14:25:22 +010054 if (*s == '\n')
Michal Simek2af9ebe2010-09-28 16:33:53 +100055 early_printk_uartlite_putc('\r');
Michal Simek89272a52009-03-27 14:25:22 +010056 s++;
57 }
58}
59
Michal Simek2af9ebe2010-09-28 16:33:53 +100060static struct console early_serial_uartlite_console = {
Michal Simek89272a52009-03-27 14:25:22 +010061 .name = "earlyser",
Michal Simek2af9ebe2010-09-28 16:33:53 +100062 .write = early_printk_uartlite_write,
Michal Simeke721a452011-04-04 15:45:06 +020063 .flags = CON_PRINTBUFFER | CON_BOOT,
Michal Simek89272a52009-03-27 14:25:22 +010064 .index = -1,
65};
Michal Simek51f5fa52010-09-28 16:40:00 +100066#endif /* CONFIG_SERIAL_UARTLITE_CONSOLE */
Michal Simek89272a52009-03-27 14:25:22 +010067
Michal Simek67f4aaa2010-09-28 16:17:03 +100068#ifdef CONFIG_SERIAL_8250_CONSOLE
69static void early_printk_uart16550_putc(char c)
70{
71 /*
72 * Limit how many times we'll spin waiting for TX FIFO status.
73 * This will prevent lockups if the base address is incorrectly
74 * set, or any other issue on the UARTLITE.
75 * This limit is pretty arbitrary, unless we are at about 10 baud
76 * we'll never timeout on a working UART.
77 */
78
79 #define UART_LSR_TEMT 0x40 /* Transmitter empty */
80 #define UART_LSR_THRE 0x20 /* Transmit-hold-register empty */
81 #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
82
83 unsigned retries = 10000;
84
85 while (--retries &&
86 !((in_be32(base_addr + 0x14) & BOTH_EMPTY) == BOTH_EMPTY))
87 ;
88
89 if (retries)
90 out_be32(base_addr, c & 0xff);
91}
92
93static void early_printk_uart16550_write(struct console *unused,
94 const char *s, unsigned n)
95{
96 while (*s && n-- > 0) {
97 early_printk_uart16550_putc(*s);
98 if (*s == '\n')
99 early_printk_uart16550_putc('\r');
100 s++;
101 }
102}
103
104static struct console early_serial_uart16550_console = {
105 .name = "earlyser",
106 .write = early_printk_uart16550_write,
Michal Simeke721a452011-04-04 15:45:06 +0200107 .flags = CON_PRINTBUFFER | CON_BOOT,
Michal Simek67f4aaa2010-09-28 16:17:03 +1000108 .index = -1,
109};
110#endif /* CONFIG_SERIAL_8250_CONSOLE */
111
Michal Simek9a7e8d82010-09-28 16:38:28 +1000112static struct console *early_console;
Michal Simek89272a52009-03-27 14:25:22 +0100113
114void early_printk(const char *fmt, ...)
115{
116 char buf[512];
117 int n;
118 va_list ap;
119
120 if (early_console_initialized) {
121 va_start(ap, fmt);
122 n = vscnprintf(buf, 512, fmt, ap);
123 early_console->write(early_console, buf, n);
124 va_end(ap);
125 }
126}
127
128int __init setup_early_printk(char *opt)
129{
130 if (early_console_initialized)
131 return 1;
132
Michal Simek51f5fa52010-09-28 16:40:00 +1000133#ifdef CONFIG_SERIAL_UARTLITE_CONSOLE
Michal Simek89272a52009-03-27 14:25:22 +0100134 base_addr = early_uartlite_console();
135 if (base_addr) {
136 early_console_initialized = 1;
Michal Simeka43acfb2009-05-26 16:30:10 +0200137#ifdef CONFIG_MMU
138 early_console_reg_tlb_alloc(base_addr);
139#endif
Michal Simek9a7e8d82010-09-28 16:38:28 +1000140 early_console = &early_serial_uartlite_console;
Michal Simek89272a52009-03-27 14:25:22 +0100141 early_printk("early_printk_console is enabled at 0x%08x\n",
142 base_addr);
143
Michal Simeke721a452011-04-04 15:45:06 +0200144 register_console(early_console);
Michal Simek89272a52009-03-27 14:25:22 +0100145
146 return 0;
Michal Simek51f5fa52010-09-28 16:40:00 +1000147 }
148#endif /* CONFIG_SERIAL_UARTLITE_CONSOLE */
149
Michal Simek67f4aaa2010-09-28 16:17:03 +1000150#ifdef CONFIG_SERIAL_8250_CONSOLE
151 base_addr = early_uart16550_console();
152 base_addr &= ~3; /* clear register offset */
153 if (base_addr) {
154 early_console_initialized = 1;
155#ifdef CONFIG_MMU
156 early_console_reg_tlb_alloc(base_addr);
157#endif
158 early_console = &early_serial_uart16550_console;
159
160 early_printk("early_printk_console is enabled at 0x%08x\n",
161 base_addr);
162
Michal Simeke721a452011-04-04 15:45:06 +0200163 register_console(early_console);
Michal Simek67f4aaa2010-09-28 16:17:03 +1000164
165 return 0;
166 }
167#endif /* CONFIG_SERIAL_8250_CONSOLE */
168
Michal Simek51f5fa52010-09-28 16:40:00 +1000169 return 1;
Michal Simek89272a52009-03-27 14:25:22 +0100170}
171
Michal Simeke721a452011-04-04 15:45:06 +0200172/* Remap early console to virtual address and do not allocate one TLB
173 * only for early console because of performance degression */
174void __init remap_early_printk(void)
175{
176 if (!early_console_initialized || !early_console)
177 return;
178 printk(KERN_INFO "early_printk_console remaping from 0x%x to ",
179 base_addr);
180 base_addr = (u32) ioremap(base_addr, PAGE_SIZE);
181 printk(KERN_CONT "0x%x\n", base_addr);
182}
183
Michal Simek89272a52009-03-27 14:25:22 +0100184void __init disable_early_printk(void)
185{
186 if (!early_console_initialized || !early_console)
187 return;
188 printk(KERN_WARNING "disabling early console\n");
189 unregister_console(early_console);
190 early_console_initialized = 0;
191}