early_printk.c 4.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 * Early printk support for Microblaze.
 *
 * Copyright (C) 2007-2009 Michal Simek <monstr@monstr.eu>
 * Copyright (C) 2007-2009 PetaLogix
 * Copyright (C) 2003-2006 Yasushi SHOJI <yashi@atmark-techno.com>
 *
 * This file is subject to the terms and conditions of the GNU General Public
 * License. See the file "COPYING" in the main directory of this archive
 * for more details.
 */

#include <linux/console.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/string.h>
#include <linux/tty.h>
#include <linux/io.h>
#include <asm/processor.h>
#include <linux/fcntl.h>
#include <asm/setup.h>
#include <asm/prom.h>

static u32 base_addr;

26
#ifdef CONFIG_SERIAL_UARTLITE_CONSOLE
27
static void early_printk_uartlite_putc(char c)
28 29 30 31 32 33 34 35 36
{
	/*
	 * Limit how many times we'll spin waiting for TX FIFO status.
	 * This will prevent lockups if the base address is incorrectly
	 * set, or any other issue on the UARTLITE.
	 * This limit is pretty arbitrary, unless we are at about 10 baud
	 * we'll never timeout on a working UART.
	 */

37
	unsigned retries = 1000000;
38
	/* read status bit - 0x8 offset */
R
Roel Kluin 已提交
39
	while (--retries && (in_be32(base_addr + 8) & (1 << 3)))
40 41 42 43 44 45 46 47
		;

	/* Only attempt the iowrite if we didn't timeout */
	/* write to TX_FIFO - 0x4 offset */
	if (retries)
		out_be32(base_addr + 4, c & 0xff);
}

48
static void early_printk_uartlite_write(struct console *unused,
49 50 51 52
					const char *s, unsigned n)
{
	while (*s && n-- > 0) {
		if (*s == '\n')
53
			early_printk_uartlite_putc('\r');
54
		early_printk_uartlite_putc(*s);
55 56 57 58
		s++;
	}
}

59
static struct console early_serial_uartlite_console = {
60
	.name = "earlyser",
61
	.write = early_printk_uartlite_write,
62
	.flags = CON_PRINTBUFFER | CON_BOOT,
63 64
	.index = -1,
};
65
#endif /* CONFIG_SERIAL_UARTLITE_CONSOLE */
66

67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
#ifdef CONFIG_SERIAL_8250_CONSOLE
static void early_printk_uart16550_putc(char c)
{
	/*
	 * Limit how many times we'll spin waiting for TX FIFO status.
	 * This will prevent lockups if the base address is incorrectly
	 * set, or any other issue on the UARTLITE.
	 * This limit is pretty arbitrary, unless we are at about 10 baud
	 * we'll never timeout on a working UART.
	 */

	#define UART_LSR_TEMT	0x40 /* Transmitter empty */
	#define UART_LSR_THRE	0x20 /* Transmit-hold-register empty */
	#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)

	unsigned retries = 10000;

	while (--retries &&
		!((in_be32(base_addr + 0x14) & BOTH_EMPTY) == BOTH_EMPTY))
		;

	if (retries)
		out_be32(base_addr, c & 0xff);
}

static void early_printk_uart16550_write(struct console *unused,
					const char *s, unsigned n)
{
	while (*s && n-- > 0) {
		if (*s == '\n')
			early_printk_uart16550_putc('\r');
98
		early_printk_uart16550_putc(*s);
99 100 101 102 103 104 105
		s++;
	}
}

static struct console early_serial_uart16550_console = {
	.name = "earlyser",
	.write = early_printk_uart16550_write,
106
	.flags = CON_PRINTBUFFER | CON_BOOT,
107 108 109 110
	.index = -1,
};
#endif /* CONFIG_SERIAL_8250_CONSOLE */

111 112
int __init setup_early_printk(char *opt)
{
113 114
	int version = 0;

115
	if (early_console)
116 117
		return 1;

118
	base_addr = of_early_console(&version);
119
	if (base_addr) {
120 121 122
#ifdef CONFIG_MMU
		early_console_reg_tlb_alloc(base_addr);
#endif
123 124 125
		switch (version) {
#ifdef CONFIG_SERIAL_UARTLITE_CONSOLE
		case UARTLITE:
126 127
			pr_info("Early console on uartlite at 0x%08x\n",
								base_addr);
128 129 130
			early_console = &early_serial_uartlite_console;
			break;
#endif
131
#ifdef CONFIG_SERIAL_8250_CONSOLE
132
		case UART16550:
133 134
			pr_info("Early console on uart16650 at 0x%08x\n",
								base_addr);
135 136
			early_console = &early_serial_uart16550_console;
			break;
137
#endif
138
		default:
139
			pr_info("Unsupported early console %d\n",
140 141 142
								version);
			return 1;
		}
143

144
		register_console(early_console);
145 146
		return 0;
	}
147
	return 1;
148 149
}

150 151 152 153
/* Remap early console to virtual address and do not allocate one TLB
 * only for early console because of performance degression */
void __init remap_early_printk(void)
{
154
	if (!early_console)
155
		return;
156
	pr_info("early_printk_console remapping from 0x%x to ", base_addr);
157
	base_addr = (u32) ioremap(base_addr, PAGE_SIZE);
158
	pr_cont("0x%x\n", base_addr);
159

160
#ifdef CONFIG_MMU
161 162 163 164 165 166 167 168 169 170 171 172 173
	/*
	 * Early console is on the top of skipped TLB entries
	 * decrease tlb_skip size ensure that hardcoded TLB entry will be
	 * used by generic algorithm
	 * FIXME check if early console mapping is on the top by rereading
	 * TLB entry and compare baseaddr
	 *  mts  rtlbx, (tlb_skip - 1)
	 *  nop
	 *  mfs  rX, rtlblo
	 *  nop
	 *  cmp rX, orig_base_addr
	 */
	tlb_skip -= 1;
174
#endif
175 176
}

177 178
void __init disable_early_printk(void)
{
179
	if (!early_console)
180
		return;
181
	pr_warn("disabling early console\n");
182
	unregister_console(early_console);
183
	early_console = NULL;
184
}