a3700_console.S 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * Copyright (C) 2016 Marvell International Ltd.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. * https://spdx.org/licenses
  6. */
  7. #include <arch.h>
  8. #include <asm_macros.S>
  9. #include <console_macros.S>
  10. #include <drivers/marvell/uart/a3700_console.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_a3700_core_putc
  16. .globl console_a3700_core_init
  17. .globl console_a3700_core_getc
  18. .globl console_a3700_core_flush
  19. .globl console_a3700_putc
  20. .globl console_a3700_getc
  21. .globl console_a3700_flush
  22. /* -----------------------------------------------
  23. * int console_a3700_core_init(unsigned long 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
  33. * Clobber list : x1, x2, x3, x4
  34. * -----------------------------------------------
  35. */
  36. func console_a3700_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. /*
  43. * Wait for the TX (THR and TSR) to be empty. If wait for 3ms, the TX FIFO is
  44. * still not empty, TX FIFO will reset by all means.
  45. */
  46. mov w4, #30 /* max time out 30 * 100 us */
  47. 2:
  48. /* Check whether TX (THR and TSR) is empty */
  49. ldr w3, [x0, #UART_STATUS_REG]
  50. and w3, w3, #UARTLSR_TXEMPTY
  51. cmp w3, #0
  52. b.ne 4f
  53. /* Delay */
  54. mov w3, #60000 /* 60000 cycles of below 3 instructions on 1200 MHz CPU ~~ 100 us */
  55. 3:
  56. sub w3, w3, #1
  57. cmp w3, #0
  58. b.ne 3b
  59. /* Check whether wait timeout expired */
  60. sub w4, w4, #1
  61. cmp w4, #0
  62. b.ne 2b
  63. 4:
  64. /* Reset UART via North Bridge Peripheral */
  65. mov_imm x4, MVEBU_NB_RESET_REG
  66. ldr w3, [x4]
  67. bic w3, w3, #MVEBU_NB_RESET_UART_N
  68. str w3, [x4]
  69. orr w3, w3, #MVEBU_NB_RESET_UART_N
  70. str w3, [x4]
  71. /* Reset FIFO */
  72. mov w3, #UART_CTRL_RXFIFO_RESET
  73. orr w3, w3, #UART_CTRL_TXFIFO_RESET
  74. str w3, [x0, #UART_CTRL_REG]
  75. /* Delay */
  76. mov w3, #2000
  77. 1:
  78. sub w3, w3, #1
  79. cmp w3, #0
  80. b.ne 1b
  81. /* Program the baudrate */
  82. /* Divisor = Round(Uartclock / (16 * baudrate)) */
  83. lsl w2, w2, #4
  84. add w1, w1, w2, lsr #1
  85. udiv w2, w1, w2
  86. and w2, w2, #0x3ff /* clear all other bits to use default clock */
  87. str w2, [x0, #UART_BAUD_REG]/* set baud rate divisor */
  88. /* Set UART to default 16X scheme */
  89. mov w3, #0
  90. str w3, [x0, #UART_POSSR_REG]
  91. /* No Parity, 1 Stop */
  92. mov w3, #0
  93. str w3, [x0, #UART_CTRL_REG]
  94. mov w0, #1
  95. ret
  96. init_fail:
  97. mov w0, #0
  98. ret
  99. endfunc console_a3700_core_init
  100. .globl console_a3700_register
  101. /* -----------------------------------------------
  102. * int console_a3700_register(console_t *console,
  103. uintptr_t base, uint32_t clk, uint32_t baud)
  104. * Function to initialize and register a new a3700
  105. * console. Storage passed in for the console struct
  106. * *must* be persistent (i.e. not from the stack).
  107. * In: x0 - UART register base address
  108. * w1 - UART clock in Hz
  109. * w2 - Baud rate
  110. * x3 - pointer to empty console_t struct
  111. * Out: return 1 on success, 0 on error
  112. * Clobber list : x0, x1, x2, x3, x4, x6, x7, x14
  113. * -----------------------------------------------
  114. */
  115. func console_a3700_register
  116. mov x7, x30
  117. mov x6, x3
  118. cbz x6, register_fail
  119. str x0, [x6, #CONSOLE_T_BASE]
  120. bl console_a3700_core_init
  121. cbz x0, register_fail
  122. mov x0, x6
  123. mov x30, x7
  124. finish_console_register a3700, putc=1, getc=ENABLE_CONSOLE_GETC, flush=1
  125. register_fail:
  126. ret x7
  127. endfunc console_a3700_register
  128. /* --------------------------------------------------------
  129. * int console_a3700_core_putc(int c, unsigned int base_addr)
  130. * Function to output a character over the console. It
  131. * returns the character printed on success or -1 on error.
  132. * In : w0 - character to be printed
  133. * x1 - console base address
  134. * Out : return -1 on error else return character.
  135. * Clobber list : x2
  136. * --------------------------------------------------------
  137. */
  138. func console_a3700_core_putc
  139. /* Check the input parameter */
  140. cbz x1, putc_error
  141. /* Prepend '\r' to '\n' */
  142. cmp w0, #0xA
  143. b.ne 2f
  144. /* Check if the transmit FIFO is full */
  145. 1: ldr w2, [x1, #UART_STATUS_REG]
  146. and w2, w2, #UARTLSR_TXFIFOFULL
  147. cmp w2, #UARTLSR_TXFIFOFULL
  148. b.eq 1b
  149. mov w2, #0xD /* '\r' */
  150. str w2, [x1, #UART_TX_REG]
  151. /* Check if the transmit FIFO is full */
  152. 2: ldr w2, [x1, #UART_STATUS_REG]
  153. and w2, w2, #UARTLSR_TXFIFOFULL
  154. cmp w2, #UARTLSR_TXFIFOFULL
  155. b.eq 2b
  156. str w0, [x1, #UART_TX_REG]
  157. ret
  158. putc_error:
  159. mov w0, #-1
  160. ret
  161. endfunc console_a3700_core_putc
  162. /* --------------------------------------------------------
  163. * int console_a3700_putc(int c, console_t *console)
  164. * Function to output a character over the console. It
  165. * returns the character printed on success or -1 on error.
  166. * In : w0 - character to be printed
  167. * x1 - pointer to console_t structure
  168. * Out : return -1 on error else return character.
  169. * Clobber list : x2
  170. * --------------------------------------------------------
  171. */
  172. func console_a3700_putc
  173. ldr x1, [x1, #CONSOLE_T_BASE]
  174. b console_a3700_core_putc
  175. endfunc console_a3700_putc
  176. /* ---------------------------------------------
  177. * int console_a3700_core_getc(void)
  178. * Function to get a character from the console.
  179. * It returns the character grabbed on success
  180. * or -1 if no character is available.
  181. * In : w0 - console base address
  182. * Out : w0 - character if available, else -1
  183. * Clobber list : x0, x1
  184. * ---------------------------------------------
  185. */
  186. func console_a3700_core_getc
  187. /* Check if there is a pending character */
  188. ldr w1, [x0, #UART_STATUS_REG]
  189. and w1, w1, #UARTLSR_RXRDY
  190. cmp w1, #UARTLSR_RXRDY
  191. b.ne getc_no_char
  192. ldr w0, [x0, #UART_RX_REG]
  193. and w0, w0, #0xff
  194. ret
  195. getc_no_char:
  196. mov w0, #ERROR_NO_PENDING_CHAR
  197. ret
  198. endfunc console_a3700_core_getc
  199. /* ---------------------------------------------
  200. * int console_a3700_getc(console_t *console)
  201. * Function to get a character from the console.
  202. * It returns the character grabbed on success
  203. * or -1 on if no character is available.
  204. * In : x0 - pointer to console_t structure
  205. * Out : w0 - character if available, else -1
  206. * Clobber list : x0, x1
  207. * ---------------------------------------------
  208. */
  209. func console_a3700_getc
  210. ldr x0, [x0, #CONSOLE_T_BASE]
  211. b console_a3700_core_getc
  212. endfunc console_a3700_getc
  213. /* ---------------------------------------------
  214. * void console_a3700_core_flush(uintptr_t base_addr)
  215. * Function to force a write of all buffered
  216. * data that hasn't been output.
  217. * In : x0 - console base address
  218. * Out : void.
  219. * Clobber list : x0, x1
  220. * ---------------------------------------------
  221. */
  222. func console_a3700_core_flush
  223. /* Wait for the TX (THR and TSR) to be empty */
  224. 1: ldr w1, [x0, #UART_STATUS_REG]
  225. and w1, w1, #UARTLSR_TXEMPTY
  226. cmp w1, #UARTLSR_TXEMPTY
  227. b.ne 1b
  228. ret
  229. endfunc console_a3700_core_flush
  230. /* ---------------------------------------------
  231. * void console_a3700_flush(console_t *console)
  232. * Function to force a write of all buffered
  233. * data that hasn't been output.
  234. * In : x0 - pointer to console_t structure
  235. * Out : void.
  236. * Clobber list : x0, x1
  237. * ---------------------------------------------
  238. */
  239. func console_a3700_flush
  240. ldr x0, [x0, #CONSOLE_T_BASE]
  241. b console_a3700_core_flush
  242. endfunc console_a3700_flush