16550_console.S 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. * Copyright (c) 2015-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/ti/uart/uart_16550.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_16550_core_init
  16. .globl console_16550_core_putc
  17. .globl console_16550_core_getc
  18. .globl console_16550_core_flush
  19. .globl console_16550_putc
  20. .globl console_16550_getc
  21. .globl console_16550_flush
  22. /* -----------------------------------------------
  23. * int console_16550_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, 0 on error
  33. * Clobber list : x1, x2, x3
  34. * -----------------------------------------------
  35. */
  36. func console_16550_core_init
  37. /* Check the input base address */
  38. cbz x0, init_fail
  39. /* Check baud rate and uart clock for sanity */
  40. cbz w1, init_fail
  41. cbz w2, init_fail
  42. /* Program the baudrate */
  43. /* Divisor = Uart clock / (16 * baudrate) */
  44. lsl w2, w2, #4
  45. udiv w2, w1, w2
  46. and w1, w2, #0xff /* w1 = DLL */
  47. lsr w2, w2, #8
  48. and w2, w2, #0xff /* w2 = DLLM */
  49. ldr w3, [x0, #UARTLCR]
  50. orr w3, w3, #UARTLCR_DLAB
  51. str w3, [x0, #UARTLCR] /* enable DLL, DLLM programming */
  52. str w1, [x0, #UARTDLL] /* program DLL */
  53. str w2, [x0, #UARTDLLM] /* program DLLM */
  54. mov w2, #~UARTLCR_DLAB
  55. and w3, w3, w2
  56. str w3, [x0, #UARTLCR] /* disable DLL, DLLM programming */
  57. /* 8n1 */
  58. mov w3, #3
  59. str w3, [x0, #UARTLCR]
  60. /* no interrupt */
  61. mov w3, #0
  62. str w3, [x0, #UARTIER]
  63. #ifdef TI_16550_MDR_QUIRK
  64. /* UART must be enabled on some platforms via the MDR register */
  65. str w3, [x0, #UARTMDR1]
  66. #endif /* TI_16550_MDR_QUIRK */
  67. /* enable fifo, DMA */
  68. mov w3, #(UARTFCR_FIFOEN | UARTFCR_DMAEN)
  69. str w3, [x0, #UARTFCR]
  70. /* DTR + RTS */
  71. mov w3, #3
  72. str w3, [x0, #UARTMCR]
  73. mov w0, #1
  74. ret
  75. init_fail:
  76. mov w0, #0
  77. ret
  78. endfunc console_16550_core_init
  79. .globl console_16550_register
  80. /* -----------------------------------------------
  81. * int console_16550_register(uintptr_t baseaddr,
  82. * uint32_t clock, uint32_t baud,
  83. * console_t *console);
  84. * Function to initialize and register a new 16550
  85. * console. Storage passed in for the console struct
  86. * *must* be persistent (i.e. not from the stack).
  87. * If w1 (UART clock) is 0, initialisation will be
  88. * skipped, relying on previous code to have done
  89. * this already. w2 is ignored then as well.
  90. * In: x0 - UART register base address
  91. * w1 - UART clock in Hz
  92. * w2 - Baud rate (ignored if w1 is 0)
  93. * x3 - pointer to empty console_t struct
  94. * Out: return 1 on success, 0 on error
  95. * Clobber list : x0, x1, x2, x6, x7, x14
  96. * -----------------------------------------------
  97. */
  98. func console_16550_register
  99. mov x7, x30
  100. mov x6, x3
  101. cbz x6, register_fail
  102. str x0, [x6, #CONSOLE_T_BASE]
  103. /* A clock rate of zero means to skip the initialisation. */
  104. cbz w1, register_16550
  105. bl console_16550_core_init
  106. cbz x0, register_fail
  107. register_16550:
  108. mov x0, x6
  109. mov x30, x7
  110. finish_console_register 16550 putc=1, getc=ENABLE_CONSOLE_GETC, flush=1
  111. register_fail:
  112. ret x7
  113. endfunc console_16550_register
  114. /* --------------------------------------------------------
  115. * int console_16550_core_putc(int c, uintptr_t base_addr)
  116. * Function to output a character over the console. It
  117. * returns the character printed on success or -1 on error.
  118. * In : w0 - character to be printed
  119. * x1 - console base address
  120. * Out : return -1 on error else return character.
  121. * Clobber list : x2
  122. * --------------------------------------------------------
  123. */
  124. func console_16550_core_putc
  125. #if ENABLE_ASSERTIONS
  126. cmp x1, #0
  127. ASM_ASSERT(ne)
  128. #endif /* ENABLE_ASSERTIONS */
  129. /* Prepend '\r' to '\n' */
  130. cmp w0, #0xA
  131. b.ne 2f
  132. /* Check if the transmit FIFO is full */
  133. 1: ldr w2, [x1, #UARTLSR]
  134. and w2, w2, #(UARTLSR_TEMT | UARTLSR_THRE)
  135. cmp w2, #(UARTLSR_TEMT | UARTLSR_THRE)
  136. b.ne 1b
  137. mov w2, #0xD /* '\r' */
  138. str w2, [x1, #UARTTX]
  139. /* Check if the transmit FIFO is full */
  140. 2: ldr w2, [x1, #UARTLSR]
  141. and w2, w2, #(UARTLSR_TEMT | UARTLSR_THRE)
  142. cmp w2, #(UARTLSR_TEMT | UARTLSR_THRE)
  143. b.ne 2b
  144. str w0, [x1, #UARTTX]
  145. ret
  146. endfunc console_16550_core_putc
  147. /* --------------------------------------------------------
  148. * int console_16550_putc(int c, console_t *console)
  149. * Function to output a character over the console. It
  150. * returns the character printed on success or -1 on error.
  151. * In : w0 - character to be printed
  152. * x1 - pointer to console_t structure
  153. * Out : return -1 on error else return character.
  154. * Clobber list : x2
  155. * --------------------------------------------------------
  156. */
  157. func console_16550_putc
  158. #if ENABLE_ASSERTIONS
  159. cmp x1, #0
  160. ASM_ASSERT(ne)
  161. #endif /* ENABLE_ASSERTIONS */
  162. ldr x1, [x1, #CONSOLE_T_BASE]
  163. b console_16550_core_putc
  164. endfunc console_16550_putc
  165. /* ---------------------------------------------
  166. * int console_16550_core_getc(uintptr_t base_addr)
  167. * Function to get a character from the console.
  168. * It returns the character grabbed on success
  169. * or -1 on if no character is available.
  170. * In : x0 - console base address
  171. * Out : w0 - character if available, else -1
  172. * Clobber list : x0, x1
  173. * ---------------------------------------------
  174. */
  175. func console_16550_core_getc
  176. #if ENABLE_ASSERTIONS
  177. cmp x0, #0
  178. ASM_ASSERT(ne)
  179. #endif /* ENABLE_ASSERTIONS */
  180. /* Check if the receive FIFO is empty */
  181. 1: ldr w1, [x0, #UARTLSR]
  182. tbz w1, #UARTLSR_RDR_BIT, no_char
  183. ldr w0, [x0, #UARTRX]
  184. ret
  185. no_char:
  186. mov w0, #ERROR_NO_PENDING_CHAR
  187. ret
  188. endfunc console_16550_core_getc
  189. /* ---------------------------------------------
  190. * int console_16550_getc(console_t *console)
  191. * Function to get a character from the console.
  192. * It returns the character grabbed on success
  193. * or -1 on if no character is available.
  194. * In : x0 - pointer to console_t stucture
  195. * Out : w0 - character if available, else -1
  196. * Clobber list : x0, x1
  197. * ---------------------------------------------
  198. */
  199. func console_16550_getc
  200. #if ENABLE_ASSERTIONS
  201. cmp x1, #0
  202. ASM_ASSERT(ne)
  203. #endif /* ENABLE_ASSERTIONS */
  204. ldr x0, [x0, #CONSOLE_T_BASE]
  205. b console_16550_core_getc
  206. endfunc console_16550_getc
  207. /* ---------------------------------------------
  208. * void console_16550_core_flush(uintptr_t base_addr)
  209. * Function to force a write of all buffered
  210. * data that hasn't been output.
  211. * In : x0 - console base address
  212. * Out : void.
  213. * Clobber list : x0, x1
  214. * ---------------------------------------------
  215. */
  216. func console_16550_core_flush
  217. #if ENABLE_ASSERTIONS
  218. cmp x0, #0
  219. ASM_ASSERT(ne)
  220. #endif /* ENABLE_ASSERTIONS */
  221. /* Loop until the transmit FIFO is empty */
  222. 1: ldr w1, [x0, #UARTLSR]
  223. and w1, w1, #(UARTLSR_TEMT | UARTLSR_THRE)
  224. cmp w1, #(UARTLSR_TEMT | UARTLSR_THRE)
  225. b.ne 1b
  226. ret
  227. endfunc console_16550_core_flush
  228. /* ---------------------------------------------
  229. * void console_16550_flush(console_t *console)
  230. * Function to force a write of all buffered
  231. * data that hasn't been output.
  232. * In : x0 - pointer to console_t structure
  233. * Out : void.
  234. * Clobber list : x0, x1
  235. * ---------------------------------------------
  236. */
  237. func console_16550_flush
  238. #if ENABLE_ASSERTIONS
  239. cmp x0, #0
  240. ASM_ASSERT(ne)
  241. #endif /* ENABLE_ASSERTIONS */
  242. ldr x0, [x0, #CONSOLE_T_BASE]
  243. b console_16550_core_flush
  244. endfunc console_16550_flush