pl011_console.S 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * Copyright (c) 2013-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/arm/pl011.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_pl011_core_init
  16. .globl console_pl011_core_putc
  17. .globl console_pl011_core_getc
  18. .globl console_pl011_core_flush
  19. .globl console_pl011_putc
  20. .globl console_pl011_getc
  21. .globl console_pl011_flush
  22. /* -----------------------------------------------
  23. * int console_pl011_core_init(uintptr_t base_addr,
  24. * unsigned int uart_clk, unsigned int baud_rate)
  25. * Function to initialize the console without a
  26. * C Runtime to print debug information. This
  27. * function will be accessed by console_init and
  28. * crash reporting.
  29. * In: x0 - console base address
  30. * w1 - Uart clock in Hz
  31. * w2 - Baud rate
  32. * Out: return 1 on success else 0 on error
  33. * Clobber list : x1, x2, x3, x4
  34. * -----------------------------------------------
  35. */
  36. func console_pl011_core_init
  37. /* Check the input base address */
  38. cbz x0, core_init_fail
  39. #if !PL011_GENERIC_UART
  40. /* Check baud rate and uart clock for sanity */
  41. cbz w1, core_init_fail
  42. cbz w2, core_init_fail
  43. /* Disable uart before programming */
  44. ldr w3, [x0, #UARTCR]
  45. mov w4, #PL011_UARTCR_UARTEN
  46. bic w3, w3, w4
  47. str w3, [x0, #UARTCR]
  48. /* Program the baudrate */
  49. /* Divisor = (Uart clock * 4) / baudrate */
  50. lsl w1, w1, #2
  51. udiv w2, w1, w2
  52. /* IBRD = Divisor >> 6 */
  53. lsr w1, w2, #6
  54. /* Write the IBRD */
  55. str w1, [x0, #UARTIBRD]
  56. /* FBRD = Divisor & 0x3F */
  57. and w1, w2, #0x3f
  58. /* Write the FBRD */
  59. str w1, [x0, #UARTFBRD]
  60. mov w1, #PL011_LINE_CONTROL
  61. str w1, [x0, #UARTLCR_H]
  62. /* Clear any pending errors */
  63. str wzr, [x0, #UARTECR]
  64. /* Enable tx, rx, and uart overall */
  65. mov w1, #(PL011_UARTCR_RXE | PL011_UARTCR_TXE | PL011_UARTCR_UARTEN)
  66. str w1, [x0, #UARTCR]
  67. #endif
  68. mov w0, #1
  69. ret
  70. core_init_fail:
  71. mov w0, wzr
  72. ret
  73. endfunc console_pl011_core_init
  74. .globl console_pl011_register
  75. /* -----------------------------------------------
  76. * int console_pl011_register(uintptr_t baseaddr,
  77. * uint32_t clock, uint32_t baud,
  78. * console_t *console);
  79. * Function to initialize and register a new PL011
  80. * console. Storage passed in for the console struct
  81. * *must* be persistent (i.e. not from the stack).
  82. * In: x0 - UART register base address
  83. * w1 - UART clock in Hz
  84. * w2 - Baud rate
  85. * x3 - pointer to empty console_t struct
  86. * Out: return 1 on success, 0 on error
  87. * Clobber list : x0, x1, x2, x6, x7, x14
  88. * -----------------------------------------------
  89. */
  90. func console_pl011_register
  91. mov x7, x30
  92. mov x6, x3
  93. cbz x6, register_fail
  94. str x0, [x6, #CONSOLE_T_BASE]
  95. bl console_pl011_core_init
  96. cbz x0, register_fail
  97. mov x0, x6
  98. mov x30, x7
  99. finish_console_register pl011 putc=1, getc=ENABLE_CONSOLE_GETC, flush=1
  100. register_fail:
  101. ret x7
  102. endfunc console_pl011_register
  103. /* --------------------------------------------------------
  104. * int console_pl011_core_putc(int c, uintptr_t base_addr)
  105. * Function to output a character over the console. It
  106. * returns the character printed on success or -1 on error.
  107. * In : w0 - character to be printed
  108. * x1 - console base address
  109. * Out : return -1 on error else return character.
  110. * Clobber list : x2
  111. * --------------------------------------------------------
  112. */
  113. func console_pl011_core_putc
  114. #if ENABLE_ASSERTIONS
  115. cmp x1, #0
  116. ASM_ASSERT(ne)
  117. #endif /* ENABLE_ASSERTIONS */
  118. /* Prepend '\r' to '\n' */
  119. cmp w0, #0xA
  120. b.ne 2f
  121. 1:
  122. /* Check if the transmit FIFO is full */
  123. ldr w2, [x1, #UARTFR]
  124. tbnz w2, #PL011_UARTFR_TXFF_BIT, 1b
  125. mov w2, #0xD
  126. str w2, [x1, #UARTDR]
  127. 2:
  128. /* Check if the transmit FIFO is full */
  129. ldr w2, [x1, #UARTFR]
  130. tbnz w2, #PL011_UARTFR_TXFF_BIT, 2b
  131. str w0, [x1, #UARTDR]
  132. ret
  133. endfunc console_pl011_core_putc
  134. /* --------------------------------------------------------
  135. * int console_pl011_putc(int c, console_t *console)
  136. * Function to output a character over the console. It
  137. * returns the character printed on success or -1 on error.
  138. * In : w0 - character to be printed
  139. * x1 - pointer to console_t structure
  140. * Out : return -1 on error else return character.
  141. * Clobber list : x2
  142. * --------------------------------------------------------
  143. */
  144. func console_pl011_putc
  145. #if ENABLE_ASSERTIONS
  146. cmp x1, #0
  147. ASM_ASSERT(ne)
  148. #endif /* ENABLE_ASSERTIONS */
  149. ldr x1, [x1, #CONSOLE_T_BASE]
  150. b console_pl011_core_putc
  151. endfunc console_pl011_putc
  152. /* ---------------------------------------------
  153. * int console_pl011_core_getc(uintptr_t base_addr)
  154. * Function to get a character from the console.
  155. * It returns the character grabbed on success
  156. * or -1 if no character is available.
  157. * In : x0 - console base address
  158. * Out: w0 - character if available, else -1
  159. * Clobber list : x0, x1
  160. * ---------------------------------------------
  161. */
  162. func console_pl011_core_getc
  163. #if ENABLE_ASSERTIONS
  164. cmp x0, #0
  165. ASM_ASSERT(ne)
  166. #endif /* ENABLE_ASSERTIONS */
  167. /* Check if the receive FIFO is empty */
  168. ldr w1, [x0, #UARTFR]
  169. tbnz w1, #PL011_UARTFR_RXFE_BIT, no_char
  170. ldr w1, [x0, #UARTDR]
  171. mov w0, w1
  172. ret
  173. no_char:
  174. mov w0, #ERROR_NO_PENDING_CHAR
  175. ret
  176. endfunc console_pl011_core_getc
  177. /* ---------------------------------------------
  178. * int console_pl011_getc(console_t *console)
  179. * Function to get a character from the console.
  180. * It returns the character grabbed on success
  181. * or -1 if no character is available.
  182. * In : x0 - pointer to console_t structure
  183. * Out: w0 - character if available, else -1
  184. * Clobber list : x0, x1
  185. * ---------------------------------------------
  186. */
  187. func console_pl011_getc
  188. #if ENABLE_ASSERTIONS
  189. cmp x0, #0
  190. ASM_ASSERT(ne)
  191. #endif /* ENABLE_ASSERTIONS */
  192. ldr x0, [x0, #CONSOLE_T_BASE]
  193. b console_pl011_core_getc
  194. endfunc console_pl011_getc
  195. /* ---------------------------------------------
  196. * void console_pl011_core_flush(uintptr_t base_addr)
  197. * Function to force a write of all buffered
  198. * data that hasn't been output.
  199. * In : x0 - console base address
  200. * Out : void.
  201. * Clobber list : x0, x1
  202. * ---------------------------------------------
  203. */
  204. func console_pl011_core_flush
  205. #if ENABLE_ASSERTIONS
  206. cmp x0, #0
  207. ASM_ASSERT(ne)
  208. #endif /* ENABLE_ASSERTIONS */
  209. 1:
  210. /* Loop until the transmit FIFO is empty */
  211. ldr w1, [x0, #UARTFR]
  212. tbnz w1, #PL011_UARTFR_BUSY_BIT, 1b
  213. ret
  214. endfunc console_pl011_core_flush
  215. /* ---------------------------------------------
  216. * void console_pl011_flush(console_t *console)
  217. * Function to force a write of all buffered
  218. * data that hasn't been output.
  219. * In : x0 - pointer to console_t structure
  220. * Out : void
  221. * Clobber list : x0, x1
  222. * ---------------------------------------------
  223. */
  224. func console_pl011_flush
  225. #if ENABLE_ASSERTIONS
  226. cmp x0, #0
  227. ASM_ASSERT(ne)
  228. #endif /* ENABLE_ASSERTIONS */
  229. ldr x0, [x0, #CONSOLE_T_BASE]
  230. b console_pl011_core_flush
  231. endfunc console_pl011_flush