1/*
2 * polling mode stateless debugging stuff, originally for NS16550 Serial Ports
3 *
4 * c 2001 PPC 64 Team, IBM Corp
5 *
6 *      This program is free software; you can redistribute it and/or
7 *      modify it under the terms of the GNU General Public License
8 *      as published by the Free Software Foundation; either version
9 *      2 of the License, or (at your option) any later version.
10 */
11
12#include <stdarg.h>
13#include <linux/types.h>
14#include <linux/sched.h>
15#include <linux/console.h>
16#include <linux/init.h>
17#include <asm/processor.h>
18#include <asm/udbg.h>
19
20void (*udbg_putc)(char c);
21int (*udbg_getc)(void);
22int (*udbg_getc_poll)(void);
23
24/*
25 * Early debugging facilities. You can enable _one_ of these via .config,
26 * if you do so your kernel _will not boot_ on anything else. Be careful.
27 */
28void __init udbg_early_init(void)
29{
30#if defined(CONFIG_PPC_EARLY_DEBUG_LPAR)
31	/* For LPAR machines that have an HVC console on vterm 0 */
32	udbg_init_debug_lpar();
33#elif defined(CONFIG_PPC_EARLY_DEBUG_G5)
34	/* For use on Apple G5 machines */
35	udbg_init_pmac_realmode();
36#elif defined(CONFIG_PPC_EARLY_DEBUG_RTAS_PANEL)
37	/* RTAS panel debug */
38	udbg_init_rtas_panel();
39#elif defined(CONFIG_PPC_EARLY_DEBUG_RTAS_CONSOLE)
40	/* RTAS console debug */
41	udbg_init_rtas_console();
42#elif defined(CONFIG_PPC_EARLY_DEBUG_MAPLE)
43	/* Maple real mode debug */
44	udbg_init_maple_realmode();
45#elif defined(CONFIG_PPC_EARLY_DEBUG_ISERIES)
46	/* For iSeries - hit Ctrl-x Ctrl-x to see the output */
47	udbg_init_iseries();
48#elif defined(CONFIG_PPC_EARLY_DEBUG_BEAT)
49	udbg_init_debug_beat();
50#elif defined(CONFIG_PPC_EARLY_DEBUG_PAS_REALMODE)
51	udbg_init_pas_realmode();
52#elif defined(CONFIG_BOOTX_TEXT)
53	udbg_init_btext();
54#elif defined(CONFIG_PPC_EARLY_DEBUG_44x)
55	/* PPC44x debug */
56	udbg_init_44x_as1();
57#endif
58}
59
60/* udbg library, used by xmon et al */
61void udbg_puts(const char *s)
62{
63	if (udbg_putc) {
64		char c;
65
66		if (s && *s != '\0') {
67			while ((c = *s++) != '\0')
68				udbg_putc(c);
69		}
70	}
71}
72
73int udbg_write(const char *s, int n)
74{
75	int remain = n;
76	char c;
77
78	if (!udbg_putc)
79		return 0;
80
81	if (s && *s != '\0') {
82		while (((c = *s++) != '\0') && (remain-- > 0)) {
83			udbg_putc(c);
84		}
85	}
86
87	return n - remain;
88}
89
90int udbg_read(char *buf, int buflen)
91{
92	char *p = buf;
93	int i, c;
94
95	if (!udbg_getc)
96		return 0;
97
98	for (i = 0; i < buflen; ++i) {
99		do {
100			c = udbg_getc();
101			if (c == -1 && i == 0)
102				return -1;
103
104		} while (c == 0x11 || c == 0x13);
105		if (c == 0 || c == -1)
106			break;
107		*p++ = c;
108	}
109
110	return i;
111}
112
113#define UDBG_BUFSIZE 256
114void udbg_printf(const char *fmt, ...)
115{
116	char buf[UDBG_BUFSIZE];
117	va_list args;
118
119	va_start(args, fmt);
120	vsnprintf(buf, UDBG_BUFSIZE, fmt, args);
121	udbg_puts(buf);
122	va_end(args);
123}
124
125void __init udbg_progress(char *s, unsigned short hex)
126{
127	udbg_puts(s);
128	udbg_puts("\n");
129}
130
131/*
132 * Early boot console based on udbg
133 */
134static void udbg_console_write(struct console *con, const char *s,
135		unsigned int n)
136{
137	udbg_write(s, n);
138}
139
140static struct console udbg_console = {
141	.name	= "udbg",
142	.write	= udbg_console_write,
143	.flags	= CON_PRINTBUFFER | CON_ENABLED | CON_BOOT,
144	.index	= -1,
145};
146
147static int early_console_initialized;
148
149/*
150 * Called by setup_system after ppc_md->probe and ppc_md->early_init.
151 * Call it again after setting udbg_putc in ppc_md->setup_arch.
152 */
153void register_early_udbg_console(void)
154{
155	if (early_console_initialized)
156		return;
157
158	if (!udbg_putc)
159		return;
160
161	if (strstr(boot_command_line, "udbg-immortal")) {
162		printk(KERN_INFO "early console immortal !\n");
163		udbg_console.flags &= ~CON_BOOT;
164	}
165	early_console_initialized = 1;
166	register_console(&udbg_console);
167}
168