meson_console.S 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * Copyright (c) 2018-2020, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <asm_macros.S>
  7. #include <assert_macros.S>
  8. #include <console_macros.S>
  9. #include <drivers/amlogic/meson_console.h>
  10. .globl console_meson_register
  11. .globl console_meson_init
  12. .globl console_meson_putc
  13. .globl console_meson_getc
  14. .globl console_meson_flush
  15. .globl console_meson_core_putc
  16. .globl console_meson_core_getc
  17. .globl console_meson_core_flush
  18. /* -----------------------------------------------
  19. * Hardware definitions
  20. * -----------------------------------------------
  21. */
  22. #define MESON_WFIFO_OFFSET 0x0
  23. #define MESON_RFIFO_OFFSET 0x4
  24. #define MESON_CONTROL_OFFSET 0x8
  25. #define MESON_STATUS_OFFSET 0xC
  26. #define MESON_MISC_OFFSET 0x10
  27. #define MESON_REG5_OFFSET 0x14
  28. #define MESON_CONTROL_CLR_ERROR_BIT 24
  29. #define MESON_CONTROL_RX_RESET_BIT 23
  30. #define MESON_CONTROL_TX_RESET_BIT 22
  31. #define MESON_CONTROL_RX_ENABLE_BIT 13
  32. #define MESON_CONTROL_TX_ENABLE_BIT 12
  33. #define MESON_STATUS_RX_EMPTY_BIT 20
  34. #define MESON_STATUS_TX_FULL_BIT 21
  35. #define MESON_STATUS_TX_EMPTY_BIT 22
  36. #define MESON_REG5_USE_XTAL_CLK_BIT 24
  37. #define MESON_REG5_USE_NEW_RATE_BIT 23
  38. #define MESON_REG5_NEW_BAUD_RATE_MASK 0x7FFFFF
  39. /* -----------------------------------------------
  40. * int console_meson_register(uintptr_t base,
  41. * uint32_t clk, uint32_t baud,
  42. * console_t *console);
  43. * Function to initialize and register a new MESON
  44. * console. Storage passed in for the console struct
  45. * *must* be persistent (i.e. not from the stack).
  46. * In: x0 - UART register base address
  47. * w1 - UART clock in Hz
  48. * w2 - Baud rate
  49. * x3 - pointer to empty console_t struct
  50. * Out: return 1 on success, 0 on error
  51. * Clobber list : x0, x1, x2, x6, x7, x14
  52. * -----------------------------------------------
  53. */
  54. func console_meson_register
  55. mov x7, x30
  56. mov x6, x3
  57. cbz x6, register_fail
  58. str x0, [x6, #CONSOLE_T_BASE]
  59. bl console_meson_init
  60. cbz x0, register_fail
  61. mov x0, x6
  62. mov x30, x7
  63. finish_console_register meson putc=1, getc=ENABLE_CONSOLE_GETC, flush=1
  64. register_fail:
  65. ret x7
  66. endfunc console_meson_register
  67. /* -----------------------------------------------
  68. * int console_meson_init(uintptr_t base_addr,
  69. * unsigned int uart_clk, unsigned int baud_rate)
  70. * Function to initialize the console without a
  71. * C Runtime to print debug information. This
  72. * function will be accessed by console_init and
  73. * crash reporting.
  74. * In: x0 - console base address
  75. * w1 - Uart clock in Hz
  76. * w2 - Baud rate
  77. * Out: return 1 on success else 0 on error
  78. * Clobber list : x0-x3
  79. * -----------------------------------------------
  80. */
  81. func console_meson_init
  82. cmp w0, #0
  83. beq init_fail
  84. mov_imm w3, 24000000 /* TODO: This only works with a 24 MHz clock. */
  85. cmp w1, w3
  86. bne init_fail
  87. cmp w2, #0
  88. beq init_fail
  89. /* Set baud rate: value = ((clock / 3) / baudrate) - 1 */
  90. mov w3, #3
  91. udiv w3, w1, w3
  92. udiv w3, w3, w2
  93. sub w3, w3, #1
  94. orr w3, w3, #((1 << MESON_REG5_USE_XTAL_CLK_BIT) | \
  95. (1 << MESON_REG5_USE_NEW_RATE_BIT))
  96. str w3, [x0, #MESON_REG5_OFFSET]
  97. /* Reset UART and clear error flag */
  98. ldr w3, [x0, #MESON_CONTROL_OFFSET]
  99. orr w3, w3, #((1 << MESON_CONTROL_CLR_ERROR_BIT) | \
  100. (1 << MESON_CONTROL_RX_RESET_BIT) | \
  101. (1 << MESON_CONTROL_TX_RESET_BIT))
  102. str w3, [x0, #MESON_CONTROL_OFFSET]
  103. bic w3, w3, #((1 << MESON_CONTROL_CLR_ERROR_BIT) | \
  104. (1 << MESON_CONTROL_RX_RESET_BIT) | \
  105. (1 << MESON_CONTROL_TX_RESET_BIT))
  106. str w3, [x0, #MESON_CONTROL_OFFSET]
  107. /* Enable transfer and receive FIFO */
  108. orr w3, w3, #((1 << MESON_CONTROL_RX_ENABLE_BIT) | \
  109. (1 << MESON_CONTROL_TX_ENABLE_BIT))
  110. str w3, [x0, #MESON_CONTROL_OFFSET]
  111. /* Success */
  112. mov w0, #1
  113. ret
  114. init_fail:
  115. mov w0, wzr
  116. ret
  117. endfunc console_meson_init
  118. /* --------------------------------------------------------
  119. * int console_meson_putc(int c, console_t *console)
  120. * Function to output a character over the console. It
  121. * returns the character printed on success or -1 on error.
  122. * In : w0 - character to be printed
  123. * x1 - pointer to console_t structure
  124. * Out : return -1 on error else return character.
  125. * Clobber list : x2
  126. * --------------------------------------------------------
  127. */
  128. func console_meson_putc
  129. #if ENABLE_ASSERTIONS
  130. cmp x1, #0
  131. ASM_ASSERT(ne)
  132. #endif /* ENABLE_ASSERTIONS */
  133. ldr x1, [x1, #CONSOLE_T_BASE]
  134. b console_meson_core_putc
  135. endfunc console_meson_putc
  136. /* --------------------------------------------------------
  137. * int console_meson_core_putc(int c, uintptr_t base_addr)
  138. * Function to output a character over the console. It
  139. * returns the character printed on success or -1 on error.
  140. * In : w0 - character to be printed
  141. * x1 - console base address
  142. * Out : return -1 on error else return character.
  143. * Clobber list : x2
  144. * --------------------------------------------------------
  145. */
  146. func console_meson_core_putc
  147. #if ENABLE_ASSERTIONS
  148. cmp x1, #0
  149. ASM_ASSERT(ne)
  150. #endif
  151. /* Prepend '\r' to '\n' */
  152. cmp w0, #0xA
  153. b.ne 2f
  154. /* Wait until the transmit FIFO isn't full */
  155. 1: ldr w2, [x1, #MESON_STATUS_OFFSET]
  156. tbnz w2, #MESON_STATUS_TX_FULL_BIT, 1b
  157. /* Write '\r' if needed */
  158. mov w2, #0xD
  159. str w2, [x1, #MESON_WFIFO_OFFSET]
  160. /* Wait until the transmit FIFO isn't full */
  161. 2: ldr w2, [x1, #MESON_STATUS_OFFSET]
  162. tbnz w2, #MESON_STATUS_TX_FULL_BIT, 2b
  163. /* Write input character */
  164. str w0, [x1, #MESON_WFIFO_OFFSET]
  165. ret
  166. endfunc console_meson_core_putc
  167. /* ---------------------------------------------
  168. * int console_meson_getc(console_t *console)
  169. * Function to get a character from the console.
  170. * It returns the character grabbed on success
  171. * or -1 if no character is available.
  172. * In : x0 - pointer to console_t structure
  173. * Out: w0 - character if available, else -1
  174. * Clobber list : x0, x1
  175. * ---------------------------------------------
  176. */
  177. func console_meson_getc
  178. #if ENABLE_ASSERTIONS
  179. cmp x0, #0
  180. ASM_ASSERT(ne)
  181. #endif /* ENABLE_ASSERTIONS */
  182. ldr x0, [x0, #CONSOLE_T_BASE]
  183. b console_meson_core_getc
  184. endfunc console_meson_getc
  185. /* ---------------------------------------------
  186. * int console_meson_core_getc(uintptr_t base_addr)
  187. * Function to get a character from the console.
  188. * It returns the character grabbed on success
  189. * or -1 if no character is available.
  190. * In : x0 - console base address
  191. * Out: w0 - character if available, else -1
  192. * Clobber list : x0, x1
  193. * ---------------------------------------------
  194. */
  195. func console_meson_core_getc
  196. #if ENABLE_ASSERTIONS
  197. cmp x0, #0
  198. ASM_ASSERT(ne)
  199. #endif
  200. /* Is the receive FIFO empty? */
  201. ldr w1, [x0, #MESON_STATUS_OFFSET]
  202. tbnz w1, #MESON_STATUS_RX_EMPTY_BIT, 1f
  203. /* Read one character from the RX FIFO */
  204. ldr w0, [x0, #MESON_RFIFO_OFFSET]
  205. ret
  206. 1:
  207. mov w0, #ERROR_NO_PENDING_CHAR
  208. ret
  209. endfunc console_meson_core_getc
  210. /* ---------------------------------------------
  211. * void console_meson_flush(console_t *console)
  212. * Function to force a write of all buffered
  213. * data that hasn't been output.
  214. * In : x0 - pointer to console_t structure
  215. * Out : void.
  216. * Clobber list : x0, x1
  217. * ---------------------------------------------
  218. */
  219. func console_meson_flush
  220. #if ENABLE_ASSERTIONS
  221. cmp x0, #0
  222. ASM_ASSERT(ne)
  223. #endif /* ENABLE_ASSERTIONS */
  224. ldr x0, [x0, #CONSOLE_T_BASE]
  225. b console_meson_core_flush
  226. endfunc console_meson_flush
  227. /* ---------------------------------------------
  228. * void console_meson_core_flush(uintptr_t base_addr)
  229. * Function to force a write of all buffered
  230. * data that hasn't been output.
  231. * In : x0 - console base address
  232. * Out : void.
  233. * Clobber list : x0, x1
  234. * ---------------------------------------------
  235. */
  236. func console_meson_core_flush
  237. #if ENABLE_ASSERTIONS
  238. cmp x0, #0
  239. ASM_ASSERT(ne)
  240. #endif
  241. /* Wait until the transmit FIFO is empty */
  242. 1: ldr w1, [x0, #MESON_STATUS_OFFSET]
  243. tbz w1, #MESON_STATUS_TX_EMPTY_BIT, 1b
  244. ret
  245. endfunc console_meson_core_flush