uartdm_console.S 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright (c) 2021-2023, Stephan Gerhold <stephan@gerhold.net>
  3. *
  4. * Based on aarch64/skeleton_console.S:
  5. * Copyright (c) 2015-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 2 /* TX FIFO has space */
  18. #define UART_DM_SR_TXEMT_BIT 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 : x0 - pointer to empty console_t structure
  36. * x1 - base address
  37. * Out: x0 - 1 on success, 0 on error
  38. * Clobber list : x0 - x7
  39. * -----------------------------------------------------------
  40. */
  41. func console_uartdm_register
  42. str x1, [x0, #CONSOLE_T_BASE]
  43. mov x7, lr
  44. bl console_uartdm_core_init
  45. mov lr, x7
  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 : x0 - unused
  53. * x1 - base address
  54. * Out: void
  55. * Clobber list : x1, x2, x3
  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 w2, #65536
  66. 1:
  67. ldr w3, [x1, #UART_DM_SR]
  68. tbnz w3, #UART_DM_SR_TXEMT_BIT, 2f
  69. subs w2, w2, #1
  70. b.ne 1b
  71. /* Timeout */
  72. 2: /* Reset receiver */
  73. mov w3, #UART_DM_CR_RESET_RX
  74. str w3, [x1, #UART_DM_CR]
  75. /* Reset transmitter */
  76. mov w3, #UART_DM_CR_RESET_TX
  77. str w3, [x1, #UART_DM_CR]
  78. /*
  79. * Disable BAM/DMA modes but enable single-character mode for TX.
  80. * The single character mode allows simplifying the putc implementation
  81. * since characters can be written directly to the FIFO instead of
  82. * having to initiate a new transfer and waiting for its completion.
  83. */
  84. mov w3, #UART_DM_DMEN_TX_SC
  85. str w3, [x1, #UART_DM_DMEN]
  86. /* Enable transmitter */
  87. mov w3, #UART_DM_CR_TX_ENABLE
  88. str w3, [x1, #UART_DM_CR]
  89. ret
  90. endfunc console_uartdm_core_init
  91. /* -----------------------------------------------------------
  92. * int console_uartdm_putc(int c, console_t *console)
  93. * Function to output a character over the console.
  94. * In : w0 - character to be printed
  95. * x1 - pointer to console_t struct
  96. * Out: w0 - printed character on success, < 0 on error.
  97. * Clobber list : x0, x1, x2
  98. * -----------------------------------------------------------
  99. */
  100. func console_uartdm_putc
  101. ldr x1, [x1, #CONSOLE_T_BASE]
  102. b console_uartdm_core_putc
  103. endfunc console_uartdm_putc
  104. /* -----------------------------------------------------------
  105. * int console_uartdm_core_putc(int c, uintptr_t base_addr)
  106. * Function to output a character over the console.
  107. * In : w0 - character to be printed
  108. * x1 - base address
  109. * Out: w0 - printed character on success, < 0 on error.
  110. * Clobber list : x2
  111. * -----------------------------------------------------------
  112. */
  113. func console_uartdm_core_putc
  114. cmp w0, #'\n'
  115. b.ne 2f
  116. 1: /* Loop until TX FIFO has space */
  117. ldr w2, [x1, #UART_DM_SR]
  118. tbz w2, #UART_DM_SR_TXRDY_BIT, 1b
  119. /* Prepend '\r' to '\n' */
  120. mov w2, #'\r'
  121. str w2, [x1, #UART_DM_TF]
  122. 2: /* Loop until TX FIFO has space */
  123. ldr w2, [x1, #UART_DM_SR]
  124. tbz w2, #UART_DM_SR_TXRDY_BIT, 2b
  125. /* Write character to FIFO */
  126. str w0, [x1, #UART_DM_TF]
  127. ret
  128. endfunc console_uartdm_core_putc
  129. /* -----------------------------------------------------------
  130. * void console_uartdm_flush(console_t *console)
  131. * Function to force a write of all buffered data
  132. * that has not been output.
  133. * In : x0 - pointer to console_t struct
  134. * Out: void
  135. * Clobber list : x0, x1, x2, x3, x4, x5
  136. * -----------------------------------------------------------
  137. */
  138. func console_uartdm_flush
  139. ldr x1, [x0, #CONSOLE_T_BASE]
  140. b console_uartdm_core_flush
  141. endfunc console_uartdm_flush
  142. /* -----------------------------------------------------------
  143. * void console_uartdm_core_flush(unused, uintptr_t base_addr)
  144. * Function to force a write of all buffered data
  145. * that has not been output.
  146. * In : x0 - unused
  147. * x1 - base address
  148. * Out: void
  149. * Clobber list : x2
  150. * -----------------------------------------------------------
  151. */
  152. func console_uartdm_core_flush
  153. 1: /* Loop until TX FIFO is empty */
  154. ldr w2, [x1, #UART_DM_SR]
  155. tbz w2, #UART_DM_SR_TXEMT_BIT, 1b
  156. ret
  157. endfunc console_uartdm_core_flush