pl011_console.S 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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/arm/pl011.h>
  11. /*
  12. * "core" functions are low-level implementations that don't require
  13. * writeable 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_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: r0 - console base address
  30. * r1 - Uart clock in Hz
  31. * r2 - Baud rate
  32. * Out: return 1 on success else 0 on error
  33. * Clobber list : r1, r2, r3
  34. * -----------------------------------------------
  35. */
  36. func console_pl011_core_init
  37. /* Check the input base address */
  38. cmp r0, #0
  39. beq core_init_fail
  40. #if !PL011_GENERIC_UART
  41. /* Check baud rate and uart clock for sanity */
  42. cmp r1, #0
  43. beq core_init_fail
  44. cmp r2, #0
  45. beq core_init_fail
  46. /* Disable the UART before initialization */
  47. ldr r3, [r0, #UARTCR]
  48. bic r3, r3, #PL011_UARTCR_UARTEN
  49. str r3, [r0, #UARTCR]
  50. /* Program the baudrate */
  51. /* Divisor = (Uart clock * 4) / baudrate */
  52. lsl r1, r1, #2
  53. #if (ARM_ARCH_MAJOR == 7) && !defined(ARMV7_SUPPORTS_VIRTUALIZATION)
  54. push {r0,r3}
  55. softudiv r0,r1,r2,r3
  56. mov r2, r0
  57. pop {r0,r3}
  58. #else
  59. udiv r2, r1, r2
  60. #endif
  61. /* IBRD = Divisor >> 6 */
  62. lsr r1, r2, #6
  63. /* Write the IBRD */
  64. str r1, [r0, #UARTIBRD]
  65. /* FBRD = Divisor & 0x3F */
  66. and r1, r2, #0x3f
  67. /* Write the FBRD */
  68. str r1, [r0, #UARTFBRD]
  69. mov r1, #PL011_LINE_CONTROL
  70. str r1, [r0, #UARTLCR_H]
  71. /* Clear any pending errors */
  72. mov r1, #0
  73. str r1, [r0, #UARTECR]
  74. /* Enable tx, rx, and uart overall */
  75. ldr r1, =(PL011_UARTCR_RXE | PL011_UARTCR_TXE | PL011_UARTCR_UARTEN)
  76. str r1, [r0, #UARTCR]
  77. #endif
  78. mov r0, #1
  79. bx lr
  80. core_init_fail:
  81. mov r0, #0
  82. bx lr
  83. endfunc console_pl011_core_init
  84. .globl console_pl011_register
  85. /* -------------------------------------------------------
  86. * int console_pl011_register(uintptr_t baseaddr,
  87. * uint32_t clock, uint32_t baud,
  88. * console_t *console);
  89. * Function to initialize and register a new PL011
  90. * console. Storage passed in for the console struct
  91. * *must* be persistent (i.e. not from the stack).
  92. * In: r0 - UART register base address
  93. * r1 - UART clock in Hz
  94. * r2 - Baud rate
  95. * r3 - pointer to empty console_t struct
  96. * Out: return 1 on success, 0 on error
  97. * Clobber list : r0, r1, r2
  98. * -------------------------------------------------------
  99. */
  100. func console_pl011_register
  101. push {r4, lr}
  102. mov r4, r3
  103. cmp r4, #0
  104. beq register_fail
  105. str r0, [r4, #CONSOLE_T_BASE]
  106. bl console_pl011_core_init
  107. cmp r0, #0
  108. beq register_fail
  109. mov r0, r4
  110. pop {r4, lr}
  111. finish_console_register pl011 putc=1, getc=ENABLE_CONSOLE_GETC, flush=1
  112. register_fail:
  113. pop {r4, pc}
  114. endfunc console_pl011_register
  115. /* --------------------------------------------------------
  116. * int console_core_putc(int c, uintptr_t base_addr)
  117. * Function to output a character over the console. It
  118. * returns the character printed on success or -1 on error.
  119. * In : r0 - character to be printed
  120. * r1 - console base address
  121. * Out : return -1 on error else return character.
  122. * Clobber list : r2
  123. * --------------------------------------------------------
  124. */
  125. func console_pl011_core_putc
  126. /* Check the input parameter */
  127. cmp r1, #0
  128. beq putc_error
  129. /* Prepend '\r' to '\n' */
  130. cmp r0, #0xA
  131. bne 2f
  132. 1:
  133. /* Check if the transmit FIFO is full */
  134. ldr r2, [r1, #UARTFR]
  135. tst r2, #PL011_UARTFR_TXFF
  136. bne 1b
  137. mov r2, #0xD
  138. str r2, [r1, #UARTDR]
  139. 2:
  140. /* Check if the transmit FIFO is full */
  141. ldr r2, [r1, #UARTFR]
  142. tst r2, #PL011_UARTFR_TXFF
  143. bne 2b
  144. str r0, [r1, #UARTDR]
  145. bx lr
  146. putc_error:
  147. mov r0, #-1
  148. bx lr
  149. endfunc console_pl011_core_putc
  150. /* --------------------------------------------------------
  151. * int console_pl011_putc(int c, console_t *console)
  152. * Function to output a character over the console. It
  153. * returns the character printed on success or -1 on error.
  154. * In: r0 - character to be printed
  155. * r1 - pointer to console_t structure
  156. * Out : return -1 on error else return character.
  157. * Clobber list: r2
  158. * -------------------------------------------------------
  159. */
  160. func console_pl011_putc
  161. #if ENABLE_ASSERTIONS
  162. cmp r1, #0
  163. ASM_ASSERT(ne)
  164. #endif /* ENABLE_ASSERTIONS */
  165. ldr r1, [r1, #CONSOLE_T_BASE]
  166. b console_pl011_core_putc
  167. endfunc console_pl011_putc
  168. /* ---------------------------------------------
  169. * int console_core_getc(uintptr_t base_addr)
  170. * Function to get a character from the console.
  171. * It returns the character grabbed on success
  172. * or -1 on error.
  173. * In : r0 - console base address
  174. * Clobber list : r0, r1
  175. * ---------------------------------------------
  176. */
  177. func console_pl011_core_getc
  178. cmp r0, #0
  179. beq getc_error
  180. 1:
  181. /* Check if the receive FIFO is empty */
  182. ldr r1, [r0, #UARTFR]
  183. tst r1, #PL011_UARTFR_RXFE
  184. bne 1b
  185. ldr r1, [r0, #UARTDR]
  186. mov r0, r1
  187. bx lr
  188. getc_error:
  189. mov r0, #-1
  190. bx lr
  191. endfunc console_pl011_core_getc
  192. /* ------------------------------------------------
  193. * int console_pl011_getc(console_t *console)
  194. * Function to get a character from the console.
  195. * It returns the character grabbed on success
  196. * or -1 if no character is available.
  197. * In : r0 - pointer to console_t structure
  198. * Out: r0 - character if available, else -1
  199. * Clobber list: r0, r1
  200. * ------------------------------------------------
  201. */
  202. func console_pl011_getc
  203. #if ENABLE_ASSERTIONS
  204. cmp r0, #0
  205. ASM_ASSERT(ne)
  206. #endif /* ENABLE_ASSERTIONS */
  207. ldr r0, [r0, #CONSOLE_T_BASE]
  208. b console_pl011_core_getc
  209. endfunc console_pl011_getc
  210. /* ---------------------------------------------
  211. * void console_core_flush(uintptr_t base_addr)
  212. * Function to force a write of all buffered
  213. * data that hasn't been output.
  214. * In : r0 - console base address
  215. * Out : void
  216. * Clobber list : r0, r1
  217. * ---------------------------------------------
  218. */
  219. func console_pl011_core_flush
  220. #if ENABLE_ASSERTIONS
  221. cmp r0, #0
  222. ASM_ASSERT(ne)
  223. #endif /* ENABLE_ASSERTIONS */
  224. 1:
  225. /* Loop while the transmit FIFO is busy */
  226. ldr r1, [r0, #UARTFR]
  227. tst r1, #PL011_UARTFR_BUSY
  228. bne 1b
  229. bx lr
  230. endfunc console_pl011_core_flush
  231. /* ---------------------------------------------
  232. * void console_pl011_flush(console_t *console)
  233. * Function to force a write of all buffered
  234. * data that hasn't been output.
  235. * In : r0 - pointer to console_t structure
  236. * Out : void
  237. * Clobber list: r0, r1
  238. * ---------------------------------------------
  239. */
  240. func console_pl011_flush
  241. #if ENABLE_ASSERTIONS
  242. cmp r0, #0
  243. ASM_ASSERT(ne)
  244. #endif /* ENABLE_ASSERTIONS */
  245. ldr r0, [r0, #CONSOLE_T_BASE]
  246. b console_pl011_core_flush
  247. endfunc console_pl011_flush