1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * polling mode stateless debugging stuff, originally for NS16550 Serial Ports
4  *
5  * c 2001 PPC 64 Team, IBM Corp
6  */
7 
8 #include <linux/stdarg.h>
9 #include <linux/types.h>
10 #include <linux/sched.h>
11 #include <linux/console.h>
12 #include <linux/init.h>
13 #include <asm/processor.h>
14 #include <asm/udbg.h>
15 
16 void (*udbg_putc)(char c);
17 void (*udbg_flush)(void);
18 int (*udbg_getc)(void);
19 int (*udbg_getc_poll)(void);
20 
21 /*
22  * Early debugging facilities. You can enable _one_ of these via .config,
23  * if you do so your kernel _will not boot_ on anything else. Be careful.
24  */
udbg_early_init(void)25 void __init udbg_early_init(void)
26 {
27 #if defined(CONFIG_PPC_EARLY_DEBUG_LPAR)
28 	/* For LPAR machines that have an HVC console on vterm 0 */
29 	udbg_init_debug_lpar();
30 #elif defined(CONFIG_PPC_EARLY_DEBUG_LPAR_HVSI)
31 	/* For LPAR machines that have an HVSI console on vterm 0 */
32 	udbg_init_debug_lpar_hvsi();
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_PAS_REALMODE)
46 	udbg_init_pas_realmode();
47 #elif defined(CONFIG_PPC_EARLY_DEBUG_BOOTX)
48 	udbg_init_btext();
49 #elif defined(CONFIG_PPC_EARLY_DEBUG_44x)
50 	/* PPC44x debug */
51 	udbg_init_44x_as1();
52 #elif defined(CONFIG_PPC_EARLY_DEBUG_CPM)
53 	udbg_init_cpm();
54 #elif defined(CONFIG_PPC_EARLY_DEBUG_USBGECKO)
55 	udbg_init_usbgecko();
56 #elif defined(CONFIG_PPC_EARLY_DEBUG_MEMCONS)
57 	/* In memory console */
58 	udbg_init_memcons();
59 #elif defined(CONFIG_PPC_EARLY_DEBUG_EHV_BC)
60 	udbg_init_ehv_bc();
61 #elif defined(CONFIG_PPC_EARLY_DEBUG_PS3GELIC)
62 	udbg_init_ps3gelic();
63 #elif defined(CONFIG_PPC_EARLY_DEBUG_OPAL_RAW)
64 	udbg_init_debug_opal_raw();
65 #elif defined(CONFIG_PPC_EARLY_DEBUG_OPAL_HVSI)
66 	udbg_init_debug_opal_hvsi();
67 #elif defined(CONFIG_PPC_EARLY_DEBUG_16550)
68 	udbg_init_debug_16550();
69 #endif
70 
71 #ifdef CONFIG_PPC_EARLY_DEBUG
72 	console_loglevel = CONSOLE_LOGLEVEL_DEBUG;
73 
74 	register_early_udbg_console();
75 #endif
76 }
77 
78 /* udbg library, used by xmon et al */
udbg_puts(const char * s)79 void udbg_puts(const char *s)
80 {
81 	if (udbg_putc) {
82 		char c;
83 
84 		if (s && *s != '\0') {
85 			while ((c = *s++) != '\0')
86 				udbg_putc(c);
87 		}
88 
89 		if (udbg_flush)
90 			udbg_flush();
91 	}
92 #if 0
93 	else {
94 		printk("%s", s);
95 	}
96 #endif
97 }
98 
udbg_write(const char * s,int n)99 int udbg_write(const char *s, int n)
100 {
101 	int remain = n;
102 	char c;
103 
104 	if (!udbg_putc)
105 		return 0;
106 
107 	if (s && *s != '\0') {
108 		while (((c = *s++) != '\0') && (remain-- > 0)) {
109 			udbg_putc(c);
110 		}
111 	}
112 
113 	if (udbg_flush)
114 		udbg_flush();
115 
116 	return n - remain;
117 }
118 
119 #define UDBG_BUFSIZE 256
udbg_printf(const char * fmt,...)120 void udbg_printf(const char *fmt, ...)
121 {
122 	if (udbg_putc) {
123 		char buf[UDBG_BUFSIZE];
124 		va_list args;
125 
126 		va_start(args, fmt);
127 		vsnprintf(buf, UDBG_BUFSIZE, fmt, args);
128 		udbg_puts(buf);
129 		va_end(args);
130 	}
131 }
132 
udbg_progress(char * s,unsigned short hex)133 void __init udbg_progress(char *s, unsigned short hex)
134 {
135 	udbg_puts(s);
136 	udbg_puts("\n");
137 }
138 
139 /*
140  * Early boot console based on udbg
141  */
udbg_console_write(struct console * con,const char * s,unsigned int n)142 static void udbg_console_write(struct console *con, const char *s,
143 		unsigned int n)
144 {
145 	udbg_write(s, n);
146 }
147 
148 static struct console udbg_console = {
149 	.name	= "udbg",
150 	.write	= udbg_console_write,
151 	.flags	= CON_PRINTBUFFER | CON_ENABLED | CON_BOOT | CON_ANYTIME,
152 	.index	= 0,
153 };
154 
155 /*
156  * Called by setup_system after ppc_md->probe and ppc_md->early_init.
157  * Call it again after setting udbg_putc in ppc_md->setup_arch.
158  */
register_early_udbg_console(void)159 void __init register_early_udbg_console(void)
160 {
161 	if (early_console)
162 		return;
163 
164 	if (!udbg_putc)
165 		return;
166 
167 	if (strstr(boot_command_line, "udbg-immortal")) {
168 		printk(KERN_INFO "early console immortal !\n");
169 		udbg_console.flags &= ~CON_BOOT;
170 	}
171 	early_console = &udbg_console;
172 	register_console(&udbg_console);
173 }
174 
175 #if 0   /* if you want to use this as a regular output console */
176 console_initcall(register_udbg_console);
177 #endif
178