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_PAS_REALMODE)
40 	udbg_init_pas_realmode();
41 #elif defined(CONFIG_PPC_EARLY_DEBUG_BOOTX)
42 	udbg_init_btext();
43 #elif defined(CONFIG_PPC_EARLY_DEBUG_44x)
44 	/* PPC44x debug */
45 	udbg_init_44x_as1();
46 #elif defined(CONFIG_PPC_EARLY_DEBUG_CPM)
47 	udbg_init_cpm();
48 #elif defined(CONFIG_PPC_EARLY_DEBUG_USBGECKO)
49 	udbg_init_usbgecko();
50 #elif defined(CONFIG_PPC_EARLY_DEBUG_MEMCONS)
51 	/* In memory console */
52 	udbg_init_memcons();
53 #elif defined(CONFIG_PPC_EARLY_DEBUG_EHV_BC)
54 	udbg_init_ehv_bc();
55 #elif defined(CONFIG_PPC_EARLY_DEBUG_PS3GELIC)
56 	udbg_init_ps3gelic();
57 #elif defined(CONFIG_PPC_EARLY_DEBUG_OPAL_RAW)
58 	udbg_init_debug_opal_raw();
59 #elif defined(CONFIG_PPC_EARLY_DEBUG_OPAL_HVSI)
60 	udbg_init_debug_opal_hvsi();
61 #elif defined(CONFIG_PPC_EARLY_DEBUG_16550)
62 	udbg_init_debug_16550();
63 #endif
64 
65 #ifdef CONFIG_PPC_EARLY_DEBUG
66 	console_loglevel = CONSOLE_LOGLEVEL_DEBUG;
67 
68 	register_early_udbg_console();
69 #endif
70 }
71 
72 /* udbg library, used by xmon et al */
udbg_puts(const char * s)73 void udbg_puts(const char *s)
74 {
75 	if (udbg_putc) {
76 		char c;
77 
78 		if (s && *s != '\0') {
79 			while ((c = *s++) != '\0')
80 				udbg_putc(c);
81 		}
82 
83 		if (udbg_flush)
84 			udbg_flush();
85 	}
86 #if 0
87 	else {
88 		printk("%s", s);
89 	}
90 #endif
91 }
92 
udbg_write(const char * s,int n)93 int udbg_write(const char *s, int n)
94 {
95 	int remain = n;
96 	char c;
97 
98 	if (!udbg_putc)
99 		return 0;
100 
101 	if (s && *s != '\0') {
102 		while (((c = *s++) != '\0') && (remain-- > 0)) {
103 			udbg_putc(c);
104 		}
105 	}
106 
107 	if (udbg_flush)
108 		udbg_flush();
109 
110 	return n - remain;
111 }
112 
113 #define UDBG_BUFSIZE 256
udbg_printf(const char * fmt,...)114 void udbg_printf(const char *fmt, ...)
115 {
116 	if (udbg_putc) {
117 		char buf[UDBG_BUFSIZE];
118 		va_list args;
119 
120 		va_start(args, fmt);
121 		vsnprintf(buf, UDBG_BUFSIZE, fmt, args);
122 		udbg_puts(buf);
123 		va_end(args);
124 	}
125 }
126 
udbg_progress(char * s,unsigned short hex)127 void __init udbg_progress(char *s, unsigned short hex)
128 {
129 	udbg_puts(s);
130 	udbg_puts("\n");
131 }
132 
133 /*
134  * Early boot console based on udbg
135  */
udbg_console_write(struct console * con,const char * s,unsigned int n)136 static void udbg_console_write(struct console *con, const char *s,
137 		unsigned int n)
138 {
139 	udbg_write(s, n);
140 }
141 
142 static struct console udbg_console = {
143 	.name	= "udbg",
144 	.write	= udbg_console_write,
145 	.flags	= CON_PRINTBUFFER | CON_ENABLED | CON_BOOT | CON_ANYTIME,
146 	.index	= 0,
147 };
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  */
register_early_udbg_console(void)153 void __init register_early_udbg_console(void)
154 {
155 	if (early_console)
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 = &udbg_console;
166 	register_console(&udbg_console);
167 }
168 
169 #if 0   /* if you want to use this as a regular output console */
170 console_initcall(register_udbg_console);
171 #endif
172