debug_uart.h 4.2 KB

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