uartdm_console.S 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright (c) 2021-2023, Stephan Gerhold <stephan@gerhold.net>
  3. *
  4. * Based on aarch32/skeleton_console.S:
  5. * Copyright (c) 2016-2020, ARM Limited and Contributors. All rights reserved.
  6. *
  7. * SPDX-License-Identifier: BSD-3-Clause
  8. */
  9. #include <asm_macros.S>
  10. #include <console_macros.S>
  11. /* UART DM registers */
  12. #define UART_DM_DMEN 0x03c /* DMA / data packing */
  13. #define UART_DM_SR 0x0a4 /* status register */
  14. #define UART_DM_CR 0x0a8 /* command register */
  15. #define UART_DM_TF 0x100 /* transmit FIFO */
  16. #define UART_DM_DMEN_TX_SC BIT_32(4) /* TX single character mode */
  17. #define UART_DM_SR_TXRDY BIT_32(2) /* TX FIFO has space */
  18. #define UART_DM_SR_TXEMT BIT_32(3) /* TX FIFO is empty */
  19. #define UART_DM_CR_RESET_RX (U(0x01) << 4) /* reset receiver */
  20. #define UART_DM_CR_RESET_TX (U(0x02) << 4) /* reset transmitter */
  21. #define UART_DM_CR_TX_ENABLE BIT_32(2) /* enable transmitter */
  22. .globl console_uartdm_register
  23. .globl console_uartdm_core_init
  24. .globl console_uartdm_putc
  25. .globl console_uartdm_core_putc
  26. .globl console_uartdm_flush
  27. .globl console_uartdm_core_flush
  28. /* -----------------------------------------------------------
  29. * int console_uartdm_register(console_t *console,
  30. * uintptr_t base_addr)
  31. * Function to initialize and register the console. The caller
  32. * needs to pass an empty console_t structure in which *MUST*
  33. * be allocated in persistent memory (e.g. a global or static
  34. * local variable, *NOT* on the stack).
  35. * In : r0 - pointer to empty console_t structure
  36. * r1 - base address
  37. * Out: r0 - 1 on success, 0 on error
  38. * Clobber list : r0 - r7
  39. * -----------------------------------------------------------
  40. */
  41. func console_uartdm_register
  42. str r1, [r0, #CONSOLE_T_BASE]
  43. mov r7, lr
  44. bl console_uartdm_core_init
  45. mov lr, r7
  46. /* Register the new console */
  47. finish_console_register uartdm putc=1, flush=1
  48. endfunc console_uartdm_register
  49. /* -----------------------------------------------------------
  50. * void console_uartdm_core_init(unused, uintptr_t base_addr)
  51. * Function to initialize the console.
  52. * In : r0 - unused
  53. * r1 - base address
  54. * Out: void
  55. * Clobber list : r1, r2, r3
  56. * -----------------------------------------------------------
  57. */
  58. func console_uartdm_core_init
  59. /*
  60. * Try to flush remaining characters from the TX FIFO before resetting
  61. * the transmitter. Unfortunately there is no good way to check if
  62. * the transmitter is actually enabled (and will finish eventually),
  63. * so use a timeout to avoid looping forever.
  64. */
  65. mov r2, #65536
  66. 1:
  67. ldr r3, [r1, #UART_DM_SR]
  68. tst r3, #UART_DM_SR_TXEMT
  69. bne 2f
  70. subs r2, r2, #1
  71. bne 1b
  72. /* Timeout */
  73. 2: /* Reset receiver */
  74. mov r3, #UART_DM_CR_RESET_RX
  75. str r3, [r1, #UART_DM_CR]
  76. /* Reset transmitter */
  77. mov r3, #UART_DM_CR_RESET_TX
  78. str r3, [r1, #UART_DM_CR]
  79. /*
  80. * Disable BAM/DMA modes but enable single-character mode for TX.
  81. * The single character mode allows simplifying the putc implementation
  82. * since characters can be written directly to the FIFO instead of
  83. * having to initiate a new transfer and waiting for its completion.
  84. */
  85. mov r3, #UART_DM_DMEN_TX_SC
  86. str r3, [r1, #UART_DM_DMEN]
  87. /* Enable transmitter */
  88. mov r3, #UART_DM_CR_TX_ENABLE
  89. str r3, [r1, #UART_DM_CR]
  90. bx lr
  91. endfunc console_uartdm_core_init
  92. /* -----------------------------------------------------------
  93. * int console_uartdm_putc(int c, console_t *console)
  94. * Function to output a character over the console.
  95. * In : r0 - character to be printed
  96. * r1 - pointer to console_t struct
  97. * Out: r0 - printed character on success, < 0 on error.
  98. * Clobber list : r0, r1, r2
  99. * -----------------------------------------------------------
  100. */
  101. func console_uartdm_putc
  102. ldr r1, [r1, #CONSOLE_T_BASE]
  103. b console_uartdm_core_putc
  104. endfunc console_uartdm_putc
  105. /* -----------------------------------------------------------
  106. * int console_uartdm_core_putc(int c, uintptr_t base_addr)
  107. * Function to output a character over the console.
  108. * In : r0 - character to be printed
  109. * r1 - base address
  110. * Out: r0 - printed character on success, < 0 on error.
  111. * Clobber list : r2
  112. * -----------------------------------------------------------
  113. */
  114. func console_uartdm_core_putc
  115. cmp r0, #'\n'
  116. bne 2f
  117. 1: /* Loop until TX FIFO has space */
  118. ldr r2, [r1, #UART_DM_SR]
  119. tst r2, #UART_DM_SR_TXRDY
  120. beq 1b
  121. /* Prepend '\r' to '\n' */
  122. mov r2, #'\r'
  123. str r2, [r1, #UART_DM_TF]
  124. 2: /* Loop until TX FIFO has space */
  125. ldr r2, [r1, #UART_DM_SR]
  126. tst r2, #UART_DM_SR_TXRDY
  127. beq 2b
  128. /* Write character to FIFO */
  129. str r0, [r1, #UART_DM_TF]
  130. bx lr
  131. endfunc console_uartdm_core_putc
  132. /* -----------------------------------------------------------
  133. * void console_uartdm_flush(console_t *console)
  134. * Function to force a write of all buffered data
  135. * that has not been output.
  136. * In : r0 - pointer to console_t struct
  137. * Out: void
  138. * Clobber list : r0, r1, r2, r3, r4, r5
  139. * -----------------------------------------------------------
  140. */
  141. func console_uartdm_flush
  142. ldr r1, [r0, #CONSOLE_T_BASE]
  143. b console_uartdm_core_flush
  144. endfunc console_uartdm_flush
  145. /* -----------------------------------------------------------
  146. * void console_uartdm_core_flush(unused, uintptr_t base_addr)
  147. * Function to force a write of all buffered data
  148. * that has not been output.
  149. * In : r0 - unused
  150. * r1 - base address
  151. * Out: void
  152. * Clobber list : r2
  153. * -----------------------------------------------------------
  154. */
  155. func console_uartdm_core_flush
  156. 1: /* Loop until TX FIFO is empty */
  157. ldr r2, [r1, #UART_DM_SR]
  158. tst r2, #UART_DM_SR_TXEMT
  159. beq 1b
  160. bx lr
  161. endfunc console_uartdm_core_flush