udbg.c 2.2 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2
 * polling mode stateless debugging stuff, originally for NS16550 Serial Ports
L
Linus Torvalds 已提交
3 4 5 6 7 8 9 10 11 12 13 14
 *
 * c 2001 PPC 64 Team, IBM Corp
 *
 *      This program is free software; you can redistribute it and/or
 *      modify it under the terms of the GNU General Public License
 *      as published by the Free Software Foundation; either version
 *      2 of the License, or (at your option) any later version.
 */

#include <stdarg.h>
#include <linux/config.h>
#include <linux/types.h>
15
#include <linux/sched.h>
16
#include <linux/console.h>
L
Linus Torvalds 已提交
17 18
#include <asm/processor.h>

19 20 21 22
void (*udbg_putc)(unsigned char c);
unsigned char (*udbg_getc)(void);
int (*udbg_getc_poll)(void);

23
/* udbg library, used by xmon et al */
L
Linus Torvalds 已提交
24 25
void udbg_puts(const char *s)
{
26
	if (udbg_putc) {
L
Linus Torvalds 已提交
27 28 29 30
		char c;

		if (s && *s != '\0') {
			while ((c = *s++) != '\0')
31
				udbg_putc(c);
L
Linus Torvalds 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45
		}
	}
#if 0
	else {
		printk("%s", s);
	}
#endif
}

int udbg_write(const char *s, int n)
{
	int remain = n;
	char c;

46
	if (!udbg_putc)
L
Linus Torvalds 已提交
47 48 49 50
		return 0;

	if (s && *s != '\0') {
		while (((c = *s++) != '\0') && (remain-- > 0)) {
51
			udbg_putc(c);
L
Linus Torvalds 已提交
52 53 54 55 56 57 58 59 60 61 62
		}
	}

	return n - remain;
}

int udbg_read(char *buf, int buflen)
{
	char c, *p = buf;
	int i;

63
	if (!udbg_getc)
L
Linus Torvalds 已提交
64 65 66 67
		return 0;

	for (i = 0; i < buflen; ++i) {
		do {
68
			c = udbg_getc();
L
Linus Torvalds 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
		} while (c == 0x11 || c == 0x13);
		if (c == 0)
			break;
		*p++ = c;
	}

	return i;
}

#define UDBG_BUFSIZE 256
void udbg_printf(const char *fmt, ...)
{
	unsigned char buf[UDBG_BUFSIZE];
	va_list args;

	va_start(args, fmt);
	vsnprintf(buf, UDBG_BUFSIZE, fmt, args);
	udbg_puts(buf);
	va_end(args);
}

90 91 92 93 94 95 96 97 98 99 100 101
/*
 * Early boot console based on udbg
 */
static void udbg_console_write(struct console *con, const char *s,
		unsigned int n)
{
	udbg_write(s, n);
}

static struct console udbg_console = {
	.name	= "udbg",
	.write	= udbg_console_write,
102
	.flags	= CON_PRINTBUFFER | CON_ENABLED,
103 104 105
	.index	= -1,
};

106 107
static int early_console_initialized;

108 109
void __init disable_early_printk(void)
{
110 111
	if (!early_console_initialized)
		return;
112
	unregister_console(&udbg_console);
113
	early_console_initialized = 0;
114 115 116 117 118
}

/* called by setup_system */
void register_early_udbg_console(void)
{
119
	early_console_initialized = 1;
120 121 122 123 124 125
	register_console(&udbg_console);
}

#if 0   /* if you want to use this as a regular output console */
console_initcall(register_udbg_console);
#endif