cdns_console.S 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * Copyright (c) 2016-2020, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <arch.h>
  7. #include <asm_macros.S>
  8. #include <assert_macros.S>
  9. #include <console_macros.S>
  10. #include <drivers/cadence/cdns_uart.h>
  11. /*
  12. * "core" functions are low-level implementations that don't require
  13. * writable memory and are thus safe to call in BL1 crash context.
  14. */
  15. .globl console_cdns_core_init
  16. .globl console_cdns_core_putc
  17. .globl console_cdns_core_getc
  18. .globl console_cdns_core_flush
  19. .globl console_cdns_putc
  20. .globl console_cdns_getc
  21. .globl console_cdns_flush
  22. /* -----------------------------------------------
  23. * int console_cdns_core_init(uintptr_t base_addr)
  24. * Function to initialize the console without a
  25. * C Runtime to print debug information. This
  26. * function will be accessed by console_init and
  27. * crash reporting.
  28. * We assume that the bootloader already set up
  29. * the HW (baud, ...) and only enable the trans-
  30. * mitter and receiver here.
  31. * In: x0 - console base address
  32. * Out: return 1 on success else 0 on error
  33. * Clobber list : x1, x2, x3
  34. * -----------------------------------------------
  35. */
  36. func console_cdns_core_init
  37. /* Check the input base address */
  38. cbz x0, core_init_fail
  39. /* RX/TX enabled & reset */
  40. mov w3, #(R_UART_CR_TX_EN | R_UART_CR_RX_EN | R_UART_CR_TXRST | R_UART_CR_RXRST)
  41. str w3, [x0, #R_UART_CR]
  42. mov w0, #1
  43. ret
  44. core_init_fail:
  45. mov w0, wzr
  46. ret
  47. endfunc console_cdns_core_init
  48. .globl console_cdns_register
  49. /* -----------------------------------------------
  50. * int console_cdns_register(uintptr_t baseaddr,
  51. * uint32_t clock, uint32_t baud,
  52. * console_t *console);
  53. * Function to initialize and register a new CDNS
  54. * console. Storage passed in for the console struct
  55. * *must* be persistent (i.e. not from the stack).
  56. * In: x0 - UART register base address
  57. * w1 - UART clock in Hz
  58. * w2 - Baud rate
  59. * x3 - pointer to empty console_t struct
  60. * Out: return 1 on success, 0 on error
  61. * Clobber list : x0, x1, x2, x6, x7, x14
  62. * -----------------------------------------------
  63. */
  64. func console_cdns_register
  65. mov x7, x30
  66. mov x6, x3
  67. cbz x6, register_fail
  68. str x0, [x6, #CONSOLE_T_BASE]
  69. bl console_cdns_core_init
  70. cbz x0, register_fail
  71. mov x0, x6
  72. mov x30, x7
  73. finish_console_register cdns putc=1, getc=ENABLE_CONSOLE_GETC, flush=1
  74. register_fail:
  75. ret x7
  76. endfunc console_cdns_register
  77. /* --------------------------------------------------------
  78. * int console_cdns_core_putc(int c, uintptr_t base_addr)
  79. * Function to output a character over the console. It
  80. * returns the character printed on success or -1 on error.
  81. * In : w0 - character to be printed
  82. * x1 - console base address
  83. * Out : return -1 on error else return character.
  84. * Clobber list : x2
  85. * --------------------------------------------------------
  86. */
  87. func console_cdns_core_putc
  88. #if ENABLE_ASSERTIONS
  89. cmp x1, #0
  90. ASM_ASSERT(ne)
  91. #endif /* ENABLE_ASSERTIONS */
  92. /* Prepend '\r' to '\n' */
  93. cmp w0, #0xA
  94. b.ne 2f
  95. 1:
  96. /* Check if the transmit FIFO is empty */
  97. ldr w2, [x1, #R_UART_SR]
  98. tbz w2, #UART_SR_INTR_TEMPTY_BIT, 1b
  99. mov w2, #0xD
  100. str w2, [x1, #R_UART_TX]
  101. 2:
  102. /* Check if the transmit FIFO is empty */
  103. ldr w2, [x1, #R_UART_SR]
  104. tbz w2, #UART_SR_INTR_TEMPTY_BIT, 2b
  105. str w0, [x1, #R_UART_TX]
  106. ret
  107. endfunc console_cdns_core_putc
  108. /* --------------------------------------------------------
  109. * int console_cdns_putc(int c, console_t *cdns)
  110. * Function to output a character over the console. It
  111. * returns the character printed on success or -1 on error.
  112. * In : w0 - character to be printed
  113. * x1 - pointer to console_t structure
  114. * Out : return -1 on error else return character.
  115. * Clobber list : x2
  116. * --------------------------------------------------------
  117. */
  118. func console_cdns_putc
  119. #if ENABLE_ASSERTIONS
  120. cmp x1, #0
  121. ASM_ASSERT(ne)
  122. #endif /* ENABLE_ASSERTIONS */
  123. ldr x1, [x1, #CONSOLE_T_BASE]
  124. b console_cdns_core_putc
  125. endfunc console_cdns_putc
  126. /* ---------------------------------------------
  127. * int console_cdns_core_getc(uintptr_t base_addr)
  128. * Function to get a character from the console.
  129. * It returns the character grabbed on success
  130. * or -1 if no character is available.
  131. * In : x0 - console base address
  132. * Out: w0 - character if available, else -1
  133. * Clobber list : x0, x1
  134. * ---------------------------------------------
  135. */
  136. func console_cdns_core_getc
  137. #if ENABLE_ASSERTIONS
  138. cmp x0, #0
  139. ASM_ASSERT(ne)
  140. #endif /* ENABLE_ASSERTIONS */
  141. /* Check if the receive FIFO is empty */
  142. ldr w1, [x0, #R_UART_SR]
  143. tbnz w1, #UART_SR_INTR_REMPTY_BIT, no_char
  144. ldr w1, [x0, #R_UART_RX]
  145. mov w0, w1
  146. ret
  147. no_char:
  148. mov w0, #ERROR_NO_PENDING_CHAR
  149. ret
  150. endfunc console_cdns_core_getc
  151. /* ---------------------------------------------
  152. * int console_cdns_getc(console_t *console)
  153. * Function to get a character from the console.
  154. * It returns the character grabbed on success
  155. * or -1 if no character is available.
  156. * In : x0 - pointer to console_t structure
  157. * Out: w0 - character if available, else -1
  158. * Clobber list : x0, x1
  159. * ---------------------------------------------
  160. */
  161. func console_cdns_getc
  162. #if ENABLE_ASSERTIONS
  163. cmp x0, #0
  164. ASM_ASSERT(ne)
  165. #endif /* ENABLE_ASSERTIONS */
  166. ldr x0, [x0, #CONSOLE_T_BASE]
  167. b console_cdns_core_getc
  168. endfunc console_cdns_getc
  169. /* ---------------------------------------------
  170. * void console_cdns_core_flush(uintptr_t base_addr)
  171. * Function to force a write of all buffered
  172. * data that hasn't been output.
  173. * In : x0 - console base address
  174. * Out : void
  175. * Clobber list : x0, x1
  176. * ---------------------------------------------
  177. */
  178. func console_cdns_core_flush
  179. #if ENABLE_ASSERTIONS
  180. cmp x0, #0
  181. ASM_ASSERT(ne)
  182. #endif /* ENABLE_ASSERTIONS */
  183. /* Loop until the transmit FIFO is empty */
  184. check_txfifo_empty:
  185. ldr w2, [x0, #R_UART_SR]
  186. tbz w2, #UART_SR_INTR_TEMPTY_BIT, check_txfifo_empty
  187. /* Wait until the Transmit is Inactive */
  188. check_tx_inactive_state:
  189. ldr w2, [x0, #R_UART_SR]
  190. tbnz w2, #UART_SR_INTR_TACTIVE_BIT, check_tx_inactive_state
  191. ret
  192. endfunc console_cdns_core_flush
  193. /* ---------------------------------------------
  194. * void console_cdns_flush(console_t *console)
  195. * Function to force a write of all buffered
  196. * data that hasn't been output.
  197. * In : x0 - pointer to console_t structure
  198. * Out : void.
  199. * Clobber list : x0, x1
  200. * ---------------------------------------------
  201. */
  202. func console_cdns_flush
  203. #if ENABLE_ASSERTIONS
  204. cmp x0, #0
  205. ASM_ASSERT(ne)
  206. #endif /* ENABLE_ASSERTIONS */
  207. ldr x0, [x0, #CONSOLE_T_BASE]
  208. b console_cdns_core_flush
  209. endfunc console_cdns_flush