xref: /OK3568_Linux_fs/u-boot/include/debug_uart.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Early debug UART support
3  *
4  * (C) Copyright 2014 Google, Inc
5  * Writte by Simon Glass <sjg@chromium.org>
6  *
7  * SPDX-License-Identifier:	GPL-2.0+
8  */
9 
10 #ifndef _DEBUG_UART_H
11 #define _DEBUG_UART_H
12 
13 /*
14  * The debug UART is intended for use very early in U-Boot to debug problems
15  * when an ICE or other debug mechanism is not available.
16  *
17  * To use it you should:
18  * - Make sure your UART supports this interface
19  * - Enable CONFIG_DEBUG_UART
20  * - Enable the CONFIG for your UART to tell it to provide this interface
21  *       (e.g. CONFIG_DEBUG_UART_NS16550)
22  * - Define the required settings as needed (see below)
23  * - Call debug_uart_init() before use
24  * - Call printch() to output a character
25  *
26  * Depending on your platform it may be possible to use this UART before a
27  * stack is available.
28  *
29  * If your UART does not support this interface you can probably add support
30  * quite easily. Remember that you cannot use driver model and it is preferred
31  * to use no stack.
32  *
33  * You must not use this UART once driver model is working and the serial
34  * drivers are up and running (done in serial_init()). Otherwise the drivers
35  * may conflict and you will get strange output.
36  *
37  *
38  * To enable the debug UART in your serial driver:
39  *
40  * - #include <debug_uart.h>
41  * - Define _debug_uart_init(), trying to avoid using the stack
42  * - Define _debug_uart_putc() as static inline (avoiding stack usage)
43  * - Immediately afterwards, add DEBUG_UART_FUNCS to define the rest of the
44  *     functionality (printch(), etc.)
45  *
46  * If your board needs additional init for the UART to work, enable
47  * CONFIG_DEBUG_UART_BOARD_INIT and write a function called
48  * board_debug_uart_init() to perform that init. When debug_uart_init() is
49  * called, the init will happen automatically.
50  */
51 
52 /**
53  * debug_uart_init() - Set up the debug UART ready for use
54  *
55  * This sets up the UART with the correct baud rate, etc.
56  *
57  * Available CONFIG is:
58  *
59  *    - CONFIG_DEBUG_UART_BASE: Base address of UART
60  *    - CONFIG_BAUDRATE: Requested baud rate
61  *    - CONFIG_DEBUG_UART_CLOCK: Input clock for UART
62  */
63 void debug_uart_init(void);
64 
65 #ifdef CONFIG_DEBUG_UART_BOARD_INIT
66 void board_debug_uart_init(void);
67 #else
board_debug_uart_init(void)68 static inline void board_debug_uart_init(void)
69 {
70 }
71 #endif
72 
73 /**
74  * printch() - Output a character to the debug UART
75  *
76  * @ch:		Character to output
77  */
78 void printch(int ch);
79 int debug_uart_getc(void);
80 int debug_uart_tstc(void);
81 int debug_uart_clrc(void);
82 int debug_uart_flushc(void);
83 int debug_uart_setbrg(void);
84 
85 /**
86  * printascii() - Output an ASCII string to the debug UART
87  *
88  * @str:	String to output
89  */
90 void printascii(const char *str);
91 
92 /**
93  * printhex2() - Output a 2-digit hex value
94  *
95  * @value:	Value to output
96  */
97 void printhex2(uint value);
98 
99 /**
100  * printhex4() - Output a 4-digit hex value
101  *
102  * @value:	Value to output
103  */
104 void printhex4(uint value);
105 
106 /**
107  * printhex8() - Output a 8-digit hex value
108  *
109  * @value:	Value to output
110  */
111 void printhex8(uint value);
112 
113 /**
114  * printdec() - Output a decimalism value
115  *
116  * @value:	Value to output
117  */
118 void printdec(uint value);
119 
120 #ifdef CONFIG_DEBUG_UART_ANNOUNCE
121 #define _DEBUG_UART_ANNOUNCE	printascii("<debug_uart> ");
122 #else
123 #define _DEBUG_UART_ANNOUNCE
124 #endif
125 
126 #define serial_dout(reg, value)	\
127 	serial_out_shift((char *)com_port + \
128 		((char *)reg - (char *)com_port) * \
129 			(1 << CONFIG_DEBUG_UART_SHIFT), \
130 		CONFIG_DEBUG_UART_SHIFT, value)
131 #define serial_din(reg) \
132 	serial_in_shift((char *)com_port + \
133 		((char *)reg - (char *)com_port) * \
134 			(1 << CONFIG_DEBUG_UART_SHIFT), \
135 		CONFIG_DEBUG_UART_SHIFT)
136 
137 /*
138  * Now define some functions - this should be inserted into the serial driver
139  */
140 #define DEBUG_UART_FUNCS \
141 \
142 	static inline void _printch(int ch) \
143 	{ \
144 		if (ch == '\n') \
145 			_debug_uart_putc('\r'); \
146 		_debug_uart_putc(ch); \
147 	} \
148 \
149 	void printch(int ch) \
150 	{ \
151 		_printch(ch); \
152 	} \
153 \
154 	int debug_uart_getc(void)\
155 	{ \
156 		return _debug_uart_getc(); \
157 	} \
158 \
159 	int debug_uart_tstc(void)\
160 	{ \
161 		return _debug_uart_tstc(true); \
162 	} \
163 \
164 	int debug_uart_clrc(void)\
165 	{ \
166 		return _debug_uart_clrc(); \
167 	} \
168 \
169 	int debug_uart_flushc(void)\
170 	{ \
171 		return _debug_uart_flushc(); \
172 	} \
173 \
174 	int debug_uart_setbrg(void)\
175 	{ \
176 		return _debug_uart_setbrg(); \
177 	} \
178 \
179 	void printascii(const char *str) \
180 	{ \
181 		while (*str) \
182 			_printch(*str++); \
183 	} \
184 \
185 	static inline void printhex1(uint digit) \
186 	{ \
187 		digit &= 0xf; \
188 		_debug_uart_putc(digit > 9 ? digit - 10 + 'a' : digit + '0'); \
189 	} \
190 \
191 	static inline void printhex(uint value, int digits) \
192 	{ \
193 		while (digits-- > 0) \
194 			printhex1(value >> (4 * digits)); \
195 	} \
196 \
197 	void printhex2(uint value) \
198 	{ \
199 		printhex(value, 2); \
200 	} \
201 \
202 	void printhex4(uint value) \
203 	{ \
204 		printhex(value, 4); \
205 	} \
206 \
207 	void printhex8(uint value) \
208 	{ \
209 		printhex(value, 8); \
210 	} \
211 \
212 	void printdec(uint value) \
213 	{ \
214 		if (value > 10) { \
215 			printdec(value / 10); \
216 			value %= 10; \
217 		} else if (value == 10) { \
218 			_debug_uart_putc('1'); \
219 			value = 0; \
220 		} \
221 		_debug_uart_putc('0' + value); \
222 	} \
223 \
224 	void debug_uart_init(void) \
225 	{ \
226 		board_debug_uart_init(); \
227 		_debug_uart_init(); \
228 		_DEBUG_UART_ANNOUNCE \
229 	} \
230 
231 #endif
232