linflex_console.S 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * Copyright 2024 NXP
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <lib/libc/errno.h>
  7. #include <asm_macros.S>
  8. #include <console_macros.S>
  9. #include <lib/utils_def.h>
  10. #define LDIV_MULTIPLIER U(16)
  11. #define LINFLEX_LINCR1 (0x0)
  12. #define LINCR1_INIT BIT_32(0)
  13. #define LINCR1_MME BIT_32(4)
  14. #define LINFLEX_LINSR (0x8)
  15. #define LINSR_LINS_INITMODE (0x00001000)
  16. #define LINSR_LINS_RX_TX_MODE (0x00008000)
  17. #define LINSR_LINS_MASK (0x0000F000)
  18. #define LINFLEX_UARTCR (0x10)
  19. #define UARTCR_ROSE BIT_32(23)
  20. #define LINFLEX_UARTSR (0x14)
  21. #define LINFLEX_LINIBRR (0x28)
  22. #define LINFLEX_LINFBRR (0x24)
  23. #define LINFLEX_BDRL (0x38)
  24. #define LINFLEX_UARTPTO (0x50)
  25. #define UARTCR_UART BIT_32(0)
  26. #define UARTCR_WL0 BIT_32(1)
  27. #define UARTCR_PC0 BIT_32(3)
  28. #define UARTCR_TXEN BIT_32(4)
  29. #define UARTCR_RXEN BIT_32(5)
  30. #define UARTCR_PC1 BIT_32(6)
  31. #define UARTCR_TFBM BIT_32(8)
  32. #define UARTCR_RFBM BIT_32(9)
  33. #define UARTCR_OSR_SHIFT U(24)
  34. #define UARTCR_OSR_WIDTH U(4)
  35. #define UARTSR_DTF BIT_32(1)
  36. /*
  37. * "core" functions are low-level implementations that do not require
  38. * writable memory and are thus safe to call in BL1 crash context.
  39. */
  40. .globl console_linflex_core_init
  41. .globl console_linflex_core_putc
  42. .globl console_linflex_core_flush
  43. .globl console_linflex_register
  44. .globl console_linflex_putc
  45. .globl console_linflex_flush
  46. /**
  47. * uint32_t get_ldiv_mult(uintptr_t baseaddr, uint32_t clock,
  48. * uint32_t baud, console_t *console,);
  49. *
  50. * Clobber list : x0 - x6
  51. * Out x4: LDIV multiplier
  52. */
  53. func get_ldiv_mult
  54. ldr w4, [x0, LINFLEX_UARTCR]
  55. mov w5, w4
  56. /* Prepare choices in w5 and w6 */
  57. ubfx x5, x5, #UARTCR_OSR_SHIFT, #UARTCR_OSR_WIDTH
  58. mov w6, #LDIV_MULTIPLIER
  59. and w4, w4, #UARTCR_ROSE
  60. cmp w4, #0x0
  61. csel w4, w5, w6, ne
  62. ret
  63. endfunc get_ldiv_mult
  64. /*
  65. * void linflex_set_brg(uintptr_t baseaddr, uint32_t clock
  66. * uint32_t baud, console_t *console);
  67. *
  68. * Clobber list : x0 - x7, x13
  69. */
  70. func linflex_set_brg
  71. mov x13, x30
  72. bl get_ldiv_mult
  73. mov x30, x13
  74. /* (x4) dividr = baudrate * ldiv_mult */
  75. mul x4, x4, x2
  76. /* (x5) divisr = clock rate */
  77. mov x5, x1
  78. /* (x6) ibr = divisr / dividr */
  79. udiv x6, x5, x4
  80. /* (x7) fbr = divisr % dividr */
  81. msub x7, x6, x4, x5
  82. /* fbr *= 16 / dividr */
  83. lsl x7, x7, #4
  84. udiv x7, x7, x4
  85. /* fbr &= 0xf */
  86. and w7, w7, #0xf
  87. str w6, [x0, LINFLEX_LINIBRR]
  88. str w7, [x0, LINFLEX_LINFBRR]
  89. ret
  90. endfunc linflex_set_brg
  91. /**
  92. * int console_linflex_core_init(uintptr_t baseaddr, uint32_t clock,
  93. * uint32_t baud);
  94. *
  95. * In: x0 - Linflex base address
  96. * x1 - clock frequency
  97. * x2 - baudrate
  98. * Out: x0 - 1 on success, 0 on error
  99. * Clobber list : x0 - x7, x13 - x14
  100. */
  101. func console_linflex_core_init
  102. /* Set master mode and init mode */
  103. mov w4, #(LINCR1_INIT)
  104. str w4, [x0, LINFLEX_LINCR1]
  105. mov w4, #(LINCR1_MME | LINCR1_INIT)
  106. str w4, [x0, LINFLEX_LINCR1]
  107. /* wait for init mode entry */
  108. wait_init_entry:
  109. ldr w4, [x0, LINFLEX_LINSR]
  110. and w4, w4, #LINSR_LINS_MASK
  111. cmp w4, #LINSR_LINS_INITMODE
  112. b.ne wait_init_entry
  113. /* Set UART bit */
  114. mov w4, #UARTCR_UART
  115. str w4, [x0, LINFLEX_UARTCR]
  116. mov x14, x30
  117. bl linflex_set_brg
  118. mov x30, x14
  119. /* Set preset timeout register value. */
  120. mov w4, #0xf
  121. str w4, [x0, LINFLEX_UARTPTO]
  122. /* 8-bit data, no parity, Tx/Rx enabled, UART mode */
  123. mov w4, #(UARTCR_PC1 | UARTCR_RXEN | UARTCR_TXEN | UARTCR_PC0 | \
  124. UARTCR_WL0 | UARTCR_UART | UARTCR_RFBM | UARTCR_TFBM)
  125. str w4, [x0, LINFLEX_UARTCR]
  126. /* End init mode */
  127. ldr w4, [x0, LINFLEX_LINCR1]
  128. bic w4, w4, #LINCR1_INIT
  129. str w4, [x0, LINFLEX_LINCR1]
  130. ret
  131. endfunc console_linflex_core_init
  132. /**
  133. * int console_linflex_register(uintptr_t baseaddr, uint32_t clock,
  134. * uint32_t clock, uint32_t baud);
  135. *
  136. * Function to initialize and register the console.
  137. * The caller needs to pass an empty console_linflex_t
  138. * structure in which *MUST* be allocated in
  139. * persistent memory (e.g. a global or static local
  140. * variable, *NOT* on the stack).
  141. * In: x0 - Linflex base address
  142. * x1 - clock frequency
  143. * x2 - baudrate
  144. * x3 - pointer to empty console_t structure
  145. * Out: x0 - 1 on success, 0 on error
  146. * Clobber list : x0 - x7, x13 - x15
  147. */
  148. func console_linflex_register
  149. mov x15, x30
  150. bl console_linflex_core_init
  151. mov x30, x15
  152. /* Populate the base address */
  153. str x0, [x3, #CONSOLE_T_BASE]
  154. mov x0, x3
  155. finish_console_register linflex, putc=1, getc=0, flush=1
  156. endfunc console_linflex_register
  157. /**
  158. * int console_linflex_core_flush(uintptr_t baseaddr);
  159. *
  160. * Loop while the TX fifo is not empty, depending on the selected UART mode.
  161. *
  162. * In: x0 - Linflex base address
  163. * Clobber list : x0 - x1
  164. */
  165. func console_linflex_core_flush
  166. wait_rx_tx:
  167. ldr w1, [x0, LINFLEX_LINSR]
  168. and w1, w1, #LINSR_LINS_MASK
  169. cmp w1, #LINSR_LINS_RX_TX_MODE
  170. b.eq wait_rx_tx
  171. mov x0, #0
  172. ret
  173. endfunc console_linflex_core_flush
  174. /**
  175. * int console_linflex_core_putc(int c, uintptr_t baseaddr);
  176. * Out: w0 - printed character on success, < 0 on error.
  177. * Clobber list : x0 - x3
  178. */
  179. func console_linflex_core_putc
  180. cbz x1, putc_error
  181. cmp w0, #'\n'
  182. b.ne print_char
  183. /* Print '\r\n' for each '\n' */
  184. mov x0, #'\r'
  185. mov x14, x30
  186. bl console_linflex_core_putc
  187. mov x30, x14
  188. mov x0, #'\n'
  189. print_char:
  190. ldr w2, [x1, LINFLEX_UARTCR]
  191. and w2, w2, #UARTCR_TFBM
  192. cmp w2, #0x0
  193. b.eq buffer_mode
  194. fifo_mode:
  195. /* UART is in FIFO mode */
  196. ldr w2, [x1, LINFLEX_UARTSR]
  197. and w2, w2, #UARTSR_DTF
  198. cmp w2, #0
  199. b.ne fifo_mode
  200. strb w0, [x1, LINFLEX_BDRL]
  201. b no_error
  202. buffer_mode:
  203. strb w0, [x1, LINFLEX_BDRL]
  204. buffer_loop:
  205. ldr w2, [x1, LINFLEX_UARTSR]
  206. and w3, w2, #UARTSR_DTF
  207. cmp w3, #0
  208. b.eq buffer_loop
  209. /**
  210. * In Buffer Mode the DTFTFF bit of UARTSR register
  211. * has to be set in software
  212. */
  213. mov w2, #UARTSR_DTF
  214. str w2, [x1, LINFLEX_UARTSR]
  215. no_error:
  216. mov x0, #0
  217. ret
  218. putc_error:
  219. mov x0, #-EINVAL
  220. ret
  221. endfunc console_linflex_core_putc
  222. /**
  223. * int console_linflex_putc(int c, console_t *console);
  224. *
  225. * Function to output a character over the console. It
  226. * returns the character printed on success or -EINVAL on error.
  227. * In : w0 - character to be printed
  228. * x1 - pointer to console_t struct
  229. * Out: w0 - printed character on success, < 0 on error.
  230. * Clobber list : x0 - x3, x15
  231. */
  232. func console_linflex_putc
  233. cbz x1, putc_error
  234. ldr x1, [x1, #CONSOLE_T_BASE]
  235. b console_linflex_core_putc
  236. puct_error:
  237. mov x0, #-EINVAL
  238. ret
  239. endfunc console_linflex_putc
  240. /**
  241. * int console_linflex_flush(console_t *console);
  242. *
  243. * Function to wait for the TX FIFO to be cleared.
  244. * In : x0 - pointer to console_t struct
  245. * Out: x0 - return -1 on error else return 0.
  246. * Clobber list : x0 - x1
  247. */
  248. func console_linflex_flush
  249. cbz x0, flush_error
  250. ldr x0, [x0, #CONSOLE_T_BASE]
  251. b console_linflex_core_flush
  252. flush_error:
  253. mov x0, #-EINVAL
  254. ret
  255. endfunc console_linflex_flush