early_printk.c 6.4 KB
Newer Older
T
Thomas Gleixner 已提交
1 2 3 4 5
#include <linux/console.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/string.h>
#include <linux/screen_info.h>
6 7 8 9
#include <linux/usb/ch9.h>
#include <linux/pci_regs.h>
#include <linux/pci_ids.h>
#include <linux/errno.h>
T
Thomas Gleixner 已提交
10 11 12
#include <asm/io.h>
#include <asm/processor.h>
#include <asm/fcntl.h>
13
#include <asm/setup.h>
T
Thomas Gleixner 已提交
14
#include <xen/hvc-console.h>
15 16
#include <asm/pci-direct.h>
#include <asm/fixmap.h>
17
#include <asm/mrst.h>
18
#include <asm/pgtable.h>
19
#include <linux/usb/ehci_def.h>
L
Linus Torvalds 已提交
20

T
Thomas Gleixner 已提交
21 22 23 24
/* Simple VGA output */
#define VGABASE		(__ISA_IO_base + 0xb8000)

static int max_ypos = 25, max_xpos = 80;
25
static int current_ypos = 25, current_xpos;
T
Thomas Gleixner 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44

static void early_vga_write(struct console *con, const char *str, unsigned n)
{
	char c;
	int  i, k, j;

	while ((c = *str++) != '\0' && n-- > 0) {
		if (current_ypos >= max_ypos) {
			/* scroll 1 line up */
			for (k = 1, j = 0; k < max_ypos; k++, j++) {
				for (i = 0; i < max_xpos; i++) {
					writew(readw(VGABASE+2*(max_xpos*k+i)),
					       VGABASE + 2*(max_xpos*j + i));
				}
			}
			for (i = 0; i < max_xpos; i++)
				writew(0x720, VGABASE + 2*(max_xpos*j + i));
			current_ypos = max_ypos-1;
		}
45 46 47 48 49 50 51 52
#ifdef CONFIG_KGDB_KDB
		if (c == '\b') {
			if (current_xpos > 0)
				current_xpos--;
		} else if (c == '\r') {
			current_xpos = 0;
		} else
#endif
T
Thomas Gleixner 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 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
		if (c == '\n') {
			current_xpos = 0;
			current_ypos++;
		} else if (c != '\r')  {
			writew(((0x7 << 8) | (unsigned short) c),
			       VGABASE + 2*(max_xpos*current_ypos +
						current_xpos++));
			if (current_xpos >= max_xpos) {
				current_xpos = 0;
				current_ypos++;
			}
		}
	}
}

static struct console early_vga_console = {
	.name =		"earlyvga",
	.write =	early_vga_write,
	.flags =	CON_PRINTBUFFER,
	.index =	-1,
};

/* Serial functions loosely based on a similar package from Klaus P. Gerlicher */

static int early_serial_base = 0x3f8;  /* ttyS0 */

#define XMTRDY          0x20

#define DLAB		0x80

#define TXR             0       /*  Transmit register (WRITE) */
#define RXR             0       /*  Receive register  (READ)  */
#define IER             1       /*  Interrupt Enable          */
#define IIR             2       /*  Interrupt ID              */
#define FCR             2       /*  FIFO control              */
#define LCR             3       /*  Line control              */
#define MCR             4       /*  Modem control             */
#define LSR             5       /*  Line Status               */
#define MSR             6       /*  Modem Status              */
#define DLL             0       /*  Divisor Latch Low         */
#define DLH             1       /*  Divisor latch High        */

static int early_serial_putc(unsigned char ch)
{
	unsigned timeout = 0xffff;
98

T
Thomas Gleixner 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
	while ((inb(early_serial_base + LSR) & XMTRDY) == 0 && --timeout)
		cpu_relax();
	outb(ch, early_serial_base + TXR);
	return timeout ? 0 : -1;
}

static void early_serial_write(struct console *con, const char *s, unsigned n)
{
	while (*s && n-- > 0) {
		if (*s == '\n')
			early_serial_putc('\r');
		early_serial_putc(*s);
		s++;
	}
}

#define DEFAULT_BAUD 9600

static __init void early_serial_init(char *s)
{
	unsigned char c;
	unsigned divisor;
	unsigned baud = DEFAULT_BAUD;
	char *e;

	if (*s == ',')
		++s;

	if (*s) {
		unsigned port;
129
		if (!strncmp(s, "0x", 2)) {
T
Thomas Gleixner 已提交
130 131
			early_serial_base = simple_strtoul(s, &e, 16);
		} else {
132
			static const int __initconst bases[] = { 0x3f8, 0x2f8 };
T
Thomas Gleixner 已提交
133

134
			if (!strncmp(s, "ttyS", 4))
T
Thomas Gleixner 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
				s += 4;
			port = simple_strtoul(s, &e, 10);
			if (port > 1 || s == e)
				port = 0;
			early_serial_base = bases[port];
		}
		s += strcspn(s, ",");
		if (*s == ',')
			s++;
	}

	outb(0x3, early_serial_base + LCR);	/* 8n1 */
	outb(0, early_serial_base + IER);	/* no interrupt */
	outb(0, early_serial_base + FCR);	/* no fifo */
	outb(0x3, early_serial_base + MCR);	/* DTR + RTS */

	if (*s) {
		baud = simple_strtoul(s, &e, 0);
		if (baud == 0 || s == e)
			baud = DEFAULT_BAUD;
	}

	divisor = 115200 / baud;
	c = inb(early_serial_base + LCR);
	outb(c | DLAB, early_serial_base + LCR);
	outb(divisor & 0xff, early_serial_base + DLL);
	outb((divisor >> 8) & 0xff, early_serial_base + DLH);
	outb(c & ~DLAB, early_serial_base + LCR);
}

static struct console early_serial_console = {
	.name =		"earlyser",
	.write =	early_serial_write,
	.flags =	CON_PRINTBUFFER,
	.index =	-1,
};

/* Direct interface for emergencies */
173
static struct console *early_console = &early_vga_console;
174
static int __initdata early_console_initialized;
T
Thomas Gleixner 已提交
175

176
asmlinkage void early_printk(const char *fmt, ...)
T
Thomas Gleixner 已提交
177 178 179 180 181
{
	char buf[512];
	int n;
	va_list ap;

182
	va_start(ap, fmt);
183
	n = vscnprintf(buf, sizeof(buf), fmt, ap);
184
	early_console->write(early_console, buf, n);
T
Thomas Gleixner 已提交
185 186 187
	va_end(ap);
}

188 189
static inline void early_console_register(struct console *con, int keep_early)
{
190 191 192 193 194
	if (early_console->index != -1) {
		printk(KERN_CRIT "ERROR: earlyprintk= %s already used\n",
		       con->name);
		return;
	}
195 196 197 198 199 200 201
	early_console = con;
	if (keep_early)
		early_console->flags &= ~CON_BOOT;
	else
		early_console->flags |= CON_BOOT;
	register_console(early_console);
}
T
Thomas Gleixner 已提交
202 203 204

static int __init setup_early_printk(char *buf)
{
205
	int keep;
206

T
Thomas Gleixner 已提交
207 208 209 210 211 212 213
	if (!buf)
		return 0;

	if (early_console_initialized)
		return 0;
	early_console_initialized = 1;

214 215 216 217
	keep = (strstr(buf, "keep") != NULL);

	while (*buf != '\0') {
		if (!strncmp(buf, "serial", 6)) {
218 219
			buf += 6;
			early_serial_init(buf);
220
			early_console_register(&early_serial_console, keep);
221 222
			if (!strncmp(buf, ",ttyS", 5))
				buf += 5;
223 224 225 226 227 228 229 230 231 232 233 234
		}
		if (!strncmp(buf, "ttyS", 4)) {
			early_serial_init(buf + 4);
			early_console_register(&early_serial_console, keep);
		}
		if (!strncmp(buf, "vga", 3) &&
		    boot_params.screen_info.orig_video_isVGA == 1) {
			max_xpos = boot_params.screen_info.orig_video_cols;
			max_ypos = boot_params.screen_info.orig_video_lines;
			current_ypos = boot_params.screen_info.orig_y;
			early_console_register(&early_vga_console, keep);
		}
235
#ifdef CONFIG_EARLY_PRINTK_DBGP
236 237
		if (!strncmp(buf, "dbgp", 4) && !early_dbgp_init(buf + 4))
			early_console_register(&early_dbgp_console, keep);
238
#endif
T
Thomas Gleixner 已提交
239
#ifdef CONFIG_HVC_XEN
240 241
		if (!strncmp(buf, "xen", 3))
			early_console_register(&xenboot_console, keep);
242
#endif
243
#ifdef CONFIG_EARLY_PRINTK_INTEL_MID
244 245 246 247
		if (!strncmp(buf, "mrst", 4)) {
			mrst_early_console_init();
			early_console_register(&early_mrst_console, keep);
		}
248 249

		if (!strncmp(buf, "hsu", 3)) {
250
			hsu_early_console_init(buf + 3);
251 252
			early_console_register(&early_hsu_console, keep);
		}
T
Thomas Gleixner 已提交
253
#endif
254
		buf++;
T
Thomas Gleixner 已提交
255 256 257
	}
	return 0;
}
258

T
Thomas Gleixner 已提交
259
early_param("earlyprintk", setup_early_printk);