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