blob: d5e397d5e0adb4a69aafbaf9db280e2ec3ffba71 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Simon Glassbac6fd82015-01-26 18:27:07 -07002/*
3 * Early debug UART support
4 *
5 * (C) Copyright 2014 Google, Inc
6 * Writte by Simon Glass <sjg@chromium.org>
Simon Glassbac6fd82015-01-26 18:27:07 -07007 */
8
9#ifndef _DEBUG_UART_H
10#define _DEBUG_UART_H
11
Simon Glassbac6fd82015-01-26 18:27:07 -070012/*
13 * The debug UART is intended for use very early in U-Boot to debug problems
14 * when an ICE or other debug mechanism is not available.
15 *
16 * To use it you should:
17 * - Make sure your UART supports this interface
18 * - Enable CONFIG_DEBUG_UART
19 * - Enable the CONFIG for your UART to tell it to provide this interface
20 * (e.g. CONFIG_DEBUG_UART_NS16550)
21 * - Define the required settings as needed (see below)
22 * - Call debug_uart_init() before use
23 * - Call printch() to output a character
24 *
25 * Depending on your platform it may be possible to use this UART before a
26 * stack is available.
27 *
28 * If your UART does not support this interface you can probably add support
29 * quite easily. Remember that you cannot use driver model and it is preferred
30 * to use no stack.
31 *
32 * You must not use this UART once driver model is working and the serial
33 * drivers are up and running (done in serial_init()). Otherwise the drivers
34 * may conflict and you will get strange output.
35 *
36 *
37 * To enable the debug UART in your serial driver:
38 *
39 * - #include <debug_uart.h>
Simon Glass60517d72015-10-18 19:51:23 -060040 * - Define _debug_uart_init(), trying to avoid using the stack
Simon Glassbac6fd82015-01-26 18:27:07 -070041 * - Define _debug_uart_putc() as static inline (avoiding stack usage)
42 * - Immediately afterwards, add DEBUG_UART_FUNCS to define the rest of the
43 * functionality (printch(), etc.)
Simon Glassc52be122015-10-18 19:51:24 -060044 *
45 * If your board needs additional init for the UART to work, enable
46 * CONFIG_DEBUG_UART_BOARD_INIT and write a function called
47 * board_debug_uart_init() to perform that init. When debug_uart_init() is
48 * called, the init will happen automatically.
Simon Glassbac6fd82015-01-26 18:27:07 -070049 */
50
51/**
52 * debug_uart_init() - Set up the debug UART ready for use
53 *
54 * This sets up the UART with the correct baud rate, etc.
55 *
56 * Available CONFIG is:
57 *
58 * - CONFIG_DEBUG_UART_BASE: Base address of UART
59 * - CONFIG_BAUDRATE: Requested baud rate
60 * - CONFIG_DEBUG_UART_CLOCK: Input clock for UART
61 */
62void debug_uart_init(void);
63
Simon Glassc52be122015-10-18 19:51:24 -060064#ifdef CONFIG_DEBUG_UART_BOARD_INIT
65void board_debug_uart_init(void);
66#else
67static inline void board_debug_uart_init(void)
68{
69}
70#endif
71
Simon Glassbac6fd82015-01-26 18:27:07 -070072/**
73 * printch() - Output a character to the debug UART
74 *
75 * @ch: Character to output
76 */
Simon Glass2a8a0532015-06-23 15:38:32 -060077void printch(int ch);
Simon Glassbac6fd82015-01-26 18:27:07 -070078
79/**
80 * printascii() - Output an ASCII string to the debug UART
81 *
82 * @str: String to output
83 */
Simon Glass2a8a0532015-06-23 15:38:32 -060084void printascii(const char *str);
Simon Glassbac6fd82015-01-26 18:27:07 -070085
86/**
87 * printhex2() - Output a 2-digit hex value
88 *
89 * @value: Value to output
90 */
Masahiro Yamada61336642020-02-25 02:24:54 +090091void printhex2(unsigned int value);
Simon Glassbac6fd82015-01-26 18:27:07 -070092
93/**
94 * printhex4() - Output a 4-digit hex value
95 *
96 * @value: Value to output
97 */
Masahiro Yamada61336642020-02-25 02:24:54 +090098void printhex4(unsigned int value);
Simon Glassbac6fd82015-01-26 18:27:07 -070099
100/**
101 * printhex8() - Output a 8-digit hex value
102 *
103 * @value: Value to output
104 */
Masahiro Yamada61336642020-02-25 02:24:54 +0900105void printhex8(unsigned int value);
Simon Glassbac6fd82015-01-26 18:27:07 -0700106
Jagan Tekifb84c202019-07-15 23:58:47 +0530107/**
108 * printdec() - Output a decimalism value
109 *
110 * @value: Value to output
111 */
Masahiro Yamada61336642020-02-25 02:24:54 +0900112void printdec(unsigned int value);
Jagan Tekifb84c202019-07-15 23:58:47 +0530113
Simon Glass1f7338a2015-10-18 19:51:25 -0600114#ifdef CONFIG_DEBUG_UART_ANNOUNCE
Stefan Roese79447302020-05-15 07:09:03 +0200115#define _DEBUG_UART_ANNOUNCE printascii("\n<debug_uart>\n");
Simon Glass1f7338a2015-10-18 19:51:25 -0600116#else
117#define _DEBUG_UART_ANNOUNCE
118#endif
119
Lokesh Vutla771d69c2017-04-22 15:57:25 +0530120#define serial_dout(reg, value) \
121 serial_out_shift((char *)com_port + \
122 ((char *)reg - (char *)com_port) * \
123 (1 << CONFIG_DEBUG_UART_SHIFT), \
124 CONFIG_DEBUG_UART_SHIFT, value)
125#define serial_din(reg) \
126 serial_in_shift((char *)com_port + \
127 ((char *)reg - (char *)com_port) * \
128 (1 << CONFIG_DEBUG_UART_SHIFT), \
129 CONFIG_DEBUG_UART_SHIFT)
130
Lukasz Czechowski4927c812025-05-20 13:36:42 +0200131#ifdef CONFIG_DEBUG_UART
132
Simon Glassbac6fd82015-01-26 18:27:07 -0700133/*
134 * Now define some functions - this should be inserted into the serial driver
135 */
136#define DEBUG_UART_FUNCS \
developera14c5162017-04-06 16:32:41 +0100137\
138 static inline void _printch(int ch) \
Simon Glassbac6fd82015-01-26 18:27:07 -0700139 { \
Masahiro Yamadaea8ef6e2016-03-08 18:19:10 +0900140 if (ch == '\n') \
141 _debug_uart_putc('\r'); \
Simon Glassbac6fd82015-01-26 18:27:07 -0700142 _debug_uart_putc(ch); \
143 } \
144\
developera14c5162017-04-06 16:32:41 +0100145 void printch(int ch) \
146 { \
147 _printch(ch); \
148 } \
149\
Simon Glass2a8a0532015-06-23 15:38:32 -0600150 void printascii(const char *str) \
Simon Glassbac6fd82015-01-26 18:27:07 -0700151 { \
152 while (*str) \
developera14c5162017-04-06 16:32:41 +0100153 _printch(*str++); \
Simon Glassbac6fd82015-01-26 18:27:07 -0700154 } \
155\
Masahiro Yamada61336642020-02-25 02:24:54 +0900156 static inline void printhex1(unsigned int digit) \
Simon Glassbac6fd82015-01-26 18:27:07 -0700157 { \
158 digit &= 0xf; \
159 _debug_uart_putc(digit > 9 ? digit - 10 + 'a' : digit + '0'); \
160 } \
161\
Masahiro Yamada61336642020-02-25 02:24:54 +0900162 static inline void printhex(unsigned int value, int digits) \
Simon Glassbac6fd82015-01-26 18:27:07 -0700163 { \
164 while (digits-- > 0) \
165 printhex1(value >> (4 * digits)); \
166 } \
167\
Masahiro Yamada61336642020-02-25 02:24:54 +0900168 void printhex2(unsigned int value) \
Simon Glassbac6fd82015-01-26 18:27:07 -0700169 { \
170 printhex(value, 2); \
171 } \
172\
Masahiro Yamada61336642020-02-25 02:24:54 +0900173 void printhex4(unsigned int value) \
Simon Glassbac6fd82015-01-26 18:27:07 -0700174 { \
175 printhex(value, 4); \
176 } \
177\
Masahiro Yamada61336642020-02-25 02:24:54 +0900178 void printhex8(unsigned int value) \
Simon Glassbac6fd82015-01-26 18:27:07 -0700179 { \
180 printhex(value, 8); \
Simon Glass60517d72015-10-18 19:51:23 -0600181 } \
182\
Masahiro Yamada61336642020-02-25 02:24:54 +0900183 void printdec(unsigned int value) \
Jagan Tekifb84c202019-07-15 23:58:47 +0530184 { \
185 if (value > 10) { \
186 printdec(value / 10); \
187 value %= 10; \
188 } else if (value == 10) { \
189 _debug_uart_putc('1'); \
190 value = 0; \
191 } \
192 _debug_uart_putc('0' + value); \
193 } \
194\
Simon Glass60517d72015-10-18 19:51:23 -0600195 void debug_uart_init(void) \
196 { \
Simon Glassc52be122015-10-18 19:51:24 -0600197 board_debug_uart_init(); \
Simon Glass60517d72015-10-18 19:51:23 -0600198 _debug_uart_init(); \
Simon Glass1f7338a2015-10-18 19:51:25 -0600199 _DEBUG_UART_ANNOUNCE \
Simon Glass60517d72015-10-18 19:51:23 -0600200 } \
Simon Glassbac6fd82015-01-26 18:27:07 -0700201
Lukasz Czechowski4927c812025-05-20 13:36:42 +0200202#else
203
204#define DEBUG_UART_FUNCS
205
206#define _printch(ch) (void)(ch)
207#define printhex1(digit) (void)(digit)
208#define printhex(value, digits) do { (void)(value); (void)(digits); } while(0)
209
210#define printch(ch) (void)(ch)
211#define printascii(str) (void)(str)
212#define printhex2(value) (void)(value)
213#define printhex4(value) (void)(value)
214#define printhex8(value) (void)(value)
215#define printdec(value) (void)(value)
216#define debug_uart_init() ((void)0)
217
218#endif
219
Simon Glassbac6fd82015-01-26 18:27:07 -0700220#endif