Simon Glass | bac6fd8 | 2015-01-26 18:27:07 -0700 | [diff] [blame] | 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 | |
Simon Glass | bac6fd8 | 2015-01-26 18:27:07 -0700 | [diff] [blame] | 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 | |
| 47 | /** |
| 48 | * debug_uart_init() - Set up the debug UART ready for use |
| 49 | * |
| 50 | * This sets up the UART with the correct baud rate, etc. |
| 51 | * |
| 52 | * Available CONFIG is: |
| 53 | * |
| 54 | * - CONFIG_DEBUG_UART_BASE: Base address of UART |
| 55 | * - CONFIG_BAUDRATE: Requested baud rate |
| 56 | * - CONFIG_DEBUG_UART_CLOCK: Input clock for UART |
| 57 | */ |
| 58 | void debug_uart_init(void); |
| 59 | |
| 60 | /** |
| 61 | * printch() - Output a character to the debug UART |
| 62 | * |
| 63 | * @ch: Character to output |
| 64 | */ |
Simon Glass | 2a8a053 | 2015-06-23 15:38:32 -0600 | [diff] [blame] | 65 | void printch(int ch); |
Simon Glass | bac6fd8 | 2015-01-26 18:27:07 -0700 | [diff] [blame] | 66 | |
| 67 | /** |
| 68 | * printascii() - Output an ASCII string to the debug UART |
| 69 | * |
| 70 | * @str: String to output |
| 71 | */ |
Simon Glass | 2a8a053 | 2015-06-23 15:38:32 -0600 | [diff] [blame] | 72 | void printascii(const char *str); |
Simon Glass | bac6fd8 | 2015-01-26 18:27:07 -0700 | [diff] [blame] | 73 | |
| 74 | /** |
| 75 | * printhex2() - Output a 2-digit hex value |
| 76 | * |
| 77 | * @value: Value to output |
| 78 | */ |
Simon Glass | 2a8a053 | 2015-06-23 15:38:32 -0600 | [diff] [blame] | 79 | void printhex2(uint value); |
Simon Glass | bac6fd8 | 2015-01-26 18:27:07 -0700 | [diff] [blame] | 80 | |
| 81 | /** |
| 82 | * printhex4() - Output a 4-digit hex value |
| 83 | * |
| 84 | * @value: Value to output |
| 85 | */ |
Simon Glass | 2a8a053 | 2015-06-23 15:38:32 -0600 | [diff] [blame] | 86 | void printhex4(uint value); |
Simon Glass | bac6fd8 | 2015-01-26 18:27:07 -0700 | [diff] [blame] | 87 | |
| 88 | /** |
| 89 | * printhex8() - Output a 8-digit hex value |
| 90 | * |
| 91 | * @value: Value to output |
| 92 | */ |
Simon Glass | 2a8a053 | 2015-06-23 15:38:32 -0600 | [diff] [blame] | 93 | void printhex8(uint value); |
Simon Glass | bac6fd8 | 2015-01-26 18:27:07 -0700 | [diff] [blame] | 94 | |
| 95 | /* |
| 96 | * Now define some functions - this should be inserted into the serial driver |
| 97 | */ |
| 98 | #define DEBUG_UART_FUNCS \ |
Simon Glass | 2a8a053 | 2015-06-23 15:38:32 -0600 | [diff] [blame] | 99 | void printch(int ch) \ |
Simon Glass | bac6fd8 | 2015-01-26 18:27:07 -0700 | [diff] [blame] | 100 | { \ |
| 101 | _debug_uart_putc(ch); \ |
| 102 | } \ |
| 103 | \ |
Simon Glass | 2a8a053 | 2015-06-23 15:38:32 -0600 | [diff] [blame] | 104 | void printascii(const char *str) \ |
Simon Glass | bac6fd8 | 2015-01-26 18:27:07 -0700 | [diff] [blame] | 105 | { \ |
| 106 | while (*str) \ |
| 107 | _debug_uart_putc(*str++); \ |
| 108 | } \ |
| 109 | \ |
| 110 | static inline void printhex1(uint digit) \ |
| 111 | { \ |
| 112 | digit &= 0xf; \ |
| 113 | _debug_uart_putc(digit > 9 ? digit - 10 + 'a' : digit + '0'); \ |
| 114 | } \ |
| 115 | \ |
| 116 | static inline void printhex(uint value, int digits) \ |
| 117 | { \ |
| 118 | while (digits-- > 0) \ |
| 119 | printhex1(value >> (4 * digits)); \ |
| 120 | } \ |
| 121 | \ |
Simon Glass | 2a8a053 | 2015-06-23 15:38:32 -0600 | [diff] [blame] | 122 | void printhex2(uint value) \ |
Simon Glass | bac6fd8 | 2015-01-26 18:27:07 -0700 | [diff] [blame] | 123 | { \ |
| 124 | printhex(value, 2); \ |
| 125 | } \ |
| 126 | \ |
Simon Glass | 2a8a053 | 2015-06-23 15:38:32 -0600 | [diff] [blame] | 127 | void printhex4(uint value) \ |
Simon Glass | bac6fd8 | 2015-01-26 18:27:07 -0700 | [diff] [blame] | 128 | { \ |
| 129 | printhex(value, 4); \ |
| 130 | } \ |
| 131 | \ |
Simon Glass | 2a8a053 | 2015-06-23 15:38:32 -0600 | [diff] [blame] | 132 | void printhex8(uint value) \ |
Simon Glass | bac6fd8 | 2015-01-26 18:27:07 -0700 | [diff] [blame] | 133 | { \ |
| 134 | printhex(value, 8); \ |
| 135 | } |
| 136 | |
| 137 | #endif |