debug_uart.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. #ifndef _DEBUG_UART_H
  10. #define _DEBUG_UART_H
  11. /*
  12. * The debug UART is intended for use very early in U-Boot to debug problems
  13. * when an ICE or other debug mechanism is not available.
  14. *
  15. * To use it you should:
  16. * - Make sure your UART supports this interface
  17. * - Enable CONFIG_DEBUG_UART
  18. * - Enable the CONFIG for your UART to tell it to provide this interface
  19. * (e.g. CONFIG_DEBUG_UART_NS16550)
  20. * - Define the required settings as needed (see below)
  21. * - Call debug_uart_init() before use
  22. * - Call printch() to output a character
  23. *
  24. * Depending on your platform it may be possible to use this UART before a
  25. * stack is available.
  26. *
  27. * If your UART does not support this interface you can probably add support
  28. * quite easily. Remember that you cannot use driver model and it is preferred
  29. * to use no stack.
  30. *
  31. * You must not use this UART once driver model is working and the serial
  32. * drivers are up and running (done in serial_init()). Otherwise the drivers
  33. * may conflict and you will get strange output.
  34. *
  35. *
  36. * To enable the debug UART in your serial driver:
  37. *
  38. * - #include <debug_uart.h>
  39. * - Define _debug_uart_init(), trying to avoid using the stack
  40. * - Define _debug_uart_putc() as static inline (avoiding stack usage)
  41. * - Immediately afterwards, add DEBUG_UART_FUNCS to define the rest of the
  42. * functionality (printch(), etc.)
  43. *
  44. * If your board needs additional init for the UART to work, enable
  45. * CONFIG_DEBUG_UART_BOARD_INIT and write a function called
  46. * board_debug_uart_init() to perform that init. When debug_uart_init() is
  47. * called, the init will happen automatically.
  48. */
  49. /**
  50. * debug_uart_init() - Set up the debug UART ready for use
  51. *
  52. * This sets up the UART with the correct baud rate, etc.
  53. *
  54. * Available CONFIG is:
  55. *
  56. * - CONFIG_DEBUG_UART_BASE: Base address of UART
  57. * - CONFIG_BAUDRATE: Requested baud rate
  58. * - CONFIG_DEBUG_UART_CLOCK: Input clock for UART
  59. */
  60. void debug_uart_init(void);
  61. #ifdef CONFIG_DEBUG_UART_BOARD_INIT
  62. void board_debug_uart_init(void);
  63. #else
  64. static inline void board_debug_uart_init(void)
  65. {
  66. }
  67. #endif
  68. /**
  69. * printch() - Output a character to the debug UART
  70. *
  71. * @ch: Character to output
  72. */
  73. void printch(int ch);
  74. /**
  75. * printascii() - Output an ASCII string to the debug UART
  76. *
  77. * @str: String to output
  78. */
  79. void printascii(const char *str);
  80. /**
  81. * printhex2() - Output a 2-digit hex value
  82. *
  83. * @value: Value to output
  84. */
  85. void printhex2(uint value);
  86. /**
  87. * printhex4() - Output a 4-digit hex value
  88. *
  89. * @value: Value to output
  90. */
  91. void printhex4(uint value);
  92. /**
  93. * printhex8() - Output a 8-digit hex value
  94. *
  95. * @value: Value to output
  96. */
  97. void printhex8(uint value);
  98. #ifdef CONFIG_DEBUG_UART_ANNOUNCE
  99. #define _DEBUG_UART_ANNOUNCE printascii("<debug_uart> ");
  100. #else
  101. #define _DEBUG_UART_ANNOUNCE
  102. #endif
  103. /*
  104. * Now define some functions - this should be inserted into the serial driver
  105. */
  106. #define DEBUG_UART_FUNCS \
  107. void printch(int ch) \
  108. { \
  109. if (ch == '\n') \
  110. _debug_uart_putc('\r'); \
  111. _debug_uart_putc(ch); \
  112. } \
  113. \
  114. void printascii(const char *str) \
  115. { \
  116. while (*str) \
  117. printch(*str++); \
  118. } \
  119. \
  120. static inline void printhex1(uint digit) \
  121. { \
  122. digit &= 0xf; \
  123. _debug_uart_putc(digit > 9 ? digit - 10 + 'a' : digit + '0'); \
  124. } \
  125. \
  126. static inline void printhex(uint value, int digits) \
  127. { \
  128. while (digits-- > 0) \
  129. printhex1(value >> (4 * digits)); \
  130. } \
  131. \
  132. void printhex2(uint value) \
  133. { \
  134. printhex(value, 2); \
  135. } \
  136. \
  137. void printhex4(uint value) \
  138. { \
  139. printhex(value, 4); \
  140. } \
  141. \
  142. void printhex8(uint value) \
  143. { \
  144. printhex(value, 8); \
  145. } \
  146. \
  147. void debug_uart_init(void) \
  148. { \
  149. board_debug_uart_init(); \
  150. _debug_uart_init(); \
  151. _DEBUG_UART_ANNOUNCE \
  152. } \
  153. #endif