blob: a6b7ce8e6eede1a1d74850e22663cb0f973bd63c [file] [log] [blame]
Simon Glass2f964aa2015-01-26 18:27:07 -07001/*
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
Simon Glass2f964aa2015-01-26 18:27:07 -070013/*
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>
Simon Glass97b05972015-10-18 19:51:23 -060041 * - Define _debug_uart_init(), trying to avoid using the stack
Simon Glass2f964aa2015-01-26 18:27:07 -070042 * - 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.)
Simon Glass0e977bc2015-10-18 19:51:24 -060045 *
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.
Simon Glass2f964aa2015-01-26 18:27:07 -070050 */
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 */
63void debug_uart_init(void);
64
Simon Glass0e977bc2015-10-18 19:51:24 -060065#ifdef CONFIG_DEBUG_UART_BOARD_INIT
66void board_debug_uart_init(void);
67#else
68static inline void board_debug_uart_init(void)
69{
70}
71#endif
72
Simon Glass2f964aa2015-01-26 18:27:07 -070073/**
74 * printch() - Output a character to the debug UART
75 *
76 * @ch: Character to output
77 */
Simon Glassd0d73612015-06-23 15:38:32 -060078void printch(int ch);
Simon Glass2f964aa2015-01-26 18:27:07 -070079
80/**
81 * printascii() - Output an ASCII string to the debug UART
82 *
83 * @str: String to output
84 */
Simon Glassd0d73612015-06-23 15:38:32 -060085void printascii(const char *str);
Simon Glass2f964aa2015-01-26 18:27:07 -070086
87/**
88 * printhex2() - Output a 2-digit hex value
89 *
90 * @value: Value to output
91 */
Simon Glassd0d73612015-06-23 15:38:32 -060092void printhex2(uint value);
Simon Glass2f964aa2015-01-26 18:27:07 -070093
94/**
95 * printhex4() - Output a 4-digit hex value
96 *
97 * @value: Value to output
98 */
Simon Glassd0d73612015-06-23 15:38:32 -060099void printhex4(uint value);
Simon Glass2f964aa2015-01-26 18:27:07 -0700100
101/**
102 * printhex8() - Output a 8-digit hex value
103 *
104 * @value: Value to output
105 */
Simon Glassd0d73612015-06-23 15:38:32 -0600106void printhex8(uint value);
Simon Glass2f964aa2015-01-26 18:27:07 -0700107
108/*
109 * Now define some functions - this should be inserted into the serial driver
110 */
111#define DEBUG_UART_FUNCS \
Simon Glassd0d73612015-06-23 15:38:32 -0600112 void printch(int ch) \
Simon Glass2f964aa2015-01-26 18:27:07 -0700113 { \
114 _debug_uart_putc(ch); \
115 } \
116\
Simon Glassd0d73612015-06-23 15:38:32 -0600117 void printascii(const char *str) \
Simon Glass2f964aa2015-01-26 18:27:07 -0700118 { \
119 while (*str) \
120 _debug_uart_putc(*str++); \
121 } \
122\
123 static inline void printhex1(uint digit) \
124 { \
125 digit &= 0xf; \
126 _debug_uart_putc(digit > 9 ? digit - 10 + 'a' : digit + '0'); \
127 } \
128\
129 static inline void printhex(uint value, int digits) \
130 { \
131 while (digits-- > 0) \
132 printhex1(value >> (4 * digits)); \
133 } \
134\
Simon Glassd0d73612015-06-23 15:38:32 -0600135 void printhex2(uint value) \
Simon Glass2f964aa2015-01-26 18:27:07 -0700136 { \
137 printhex(value, 2); \
138 } \
139\
Simon Glassd0d73612015-06-23 15:38:32 -0600140 void printhex4(uint value) \
Simon Glass2f964aa2015-01-26 18:27:07 -0700141 { \
142 printhex(value, 4); \
143 } \
144\
Simon Glassd0d73612015-06-23 15:38:32 -0600145 void printhex8(uint value) \
Simon Glass2f964aa2015-01-26 18:27:07 -0700146 { \
147 printhex(value, 8); \
Simon Glass97b05972015-10-18 19:51:23 -0600148 } \
149\
150 void debug_uart_init(void) \
151 { \
Simon Glass0e977bc2015-10-18 19:51:24 -0600152 board_debug_uart_init(); \
Simon Glass97b05972015-10-18 19:51:23 -0600153 _debug_uart_init(); \
154 } \
Simon Glass2f964aa2015-01-26 18:27:07 -0700155
156#endif