debug_uart.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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(unsigned int value);
  85. /**
  86. * printhex4() - Output a 4-digit hex value
  87. *
  88. * @value: Value to output
  89. */
  90. void printhex4(unsigned int value);
  91. /**
  92. * printhex8() - Output a 8-digit hex value
  93. *
  94. * @value: Value to output
  95. */
  96. void printhex8(unsigned int value);
  97. /**
  98. * printdec() - Output a decimalism value
  99. *
  100. * @value: Value to output
  101. */
  102. void printdec(unsigned int value);
  103. #ifdef CONFIG_DEBUG_UART_ANNOUNCE
  104. #define _DEBUG_UART_ANNOUNCE printascii("\n<debug_uart>\n");
  105. #else
  106. #define _DEBUG_UART_ANNOUNCE
  107. #endif
  108. #define serial_dout(reg, value) \
  109. serial_out_shift((char *)com_port + \
  110. ((char *)reg - (char *)com_port) * \
  111. (1 << CONFIG_DEBUG_UART_SHIFT), \
  112. CONFIG_DEBUG_UART_SHIFT, value)
  113. #define serial_din(reg) \
  114. serial_in_shift((char *)com_port + \
  115. ((char *)reg - (char *)com_port) * \
  116. (1 << CONFIG_DEBUG_UART_SHIFT), \
  117. CONFIG_DEBUG_UART_SHIFT)
  118. /*
  119. * Now define some functions - this should be inserted into the serial driver
  120. */
  121. #define DEBUG_UART_FUNCS \
  122. \
  123. static inline void _printch(int ch) \
  124. { \
  125. if (ch == '\n') \
  126. _debug_uart_putc('\r'); \
  127. _debug_uart_putc(ch); \
  128. } \
  129. \
  130. void printch(int ch) \
  131. { \
  132. _printch(ch); \
  133. } \
  134. \
  135. void printascii(const char *str) \
  136. { \
  137. while (*str) \
  138. _printch(*str++); \
  139. } \
  140. \
  141. static inline void printhex1(unsigned int digit) \
  142. { \
  143. digit &= 0xf; \
  144. _debug_uart_putc(digit > 9 ? digit - 10 + 'a' : digit + '0'); \
  145. } \
  146. \
  147. static inline void printhex(unsigned int value, int digits) \
  148. { \
  149. while (digits-- > 0) \
  150. printhex1(value >> (4 * digits)); \
  151. } \
  152. \
  153. void printhex2(unsigned int value) \
  154. { \
  155. printhex(value, 2); \
  156. } \
  157. \
  158. void printhex4(unsigned int value) \
  159. { \
  160. printhex(value, 4); \
  161. } \
  162. \
  163. void printhex8(unsigned int value) \
  164. { \
  165. printhex(value, 8); \
  166. } \
  167. \
  168. void printdec(unsigned int value) \
  169. { \
  170. if (value > 10) { \
  171. printdec(value / 10); \
  172. value %= 10; \
  173. } else if (value == 10) { \
  174. _debug_uart_putc('1'); \
  175. value = 0; \
  176. } \
  177. _debug_uart_putc('0' + value); \
  178. } \
  179. \
  180. void debug_uart_init(void) \
  181. { \
  182. board_debug_uart_init(); \
  183. _debug_uart_init(); \
  184. _DEBUG_UART_ANNOUNCE \
  185. } \
  186. #endif