Soby Mathew | 420cc37 | 2016-03-24 16:52:40 +0000 | [diff] [blame] | 1 | /* |
Daniel Boulby | 05e7f56 | 2018-09-19 13:58:20 +0100 | [diff] [blame] | 2 | * Copyright (c) 2016-2018, ARM Limited and Contributors. All rights reserved. |
Soby Mathew | 420cc37 | 2016-03-24 16:52:40 +0000 | [diff] [blame] | 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Soby Mathew | 420cc37 | 2016-03-24 16:52:40 +0000 | [diff] [blame] | 5 | */ |
| 6 | #include <arch.h> |
| 7 | #include <asm_macros.S> |
Daniel Boulby | 05e7f56 | 2018-09-19 13:58:20 +0100 | [diff] [blame] | 8 | #include <assert_macros.S> |
| 9 | #include <console_macros.S> |
Soby Mathew | 420cc37 | 2016-03-24 16:52:40 +0000 | [diff] [blame] | 10 | #include <pl011.h> |
| 11 | |
| 12 | /* |
| 13 | * Pull in generic functions to provide backwards compatibility for |
| 14 | * platform makefiles |
| 15 | */ |
| 16 | #include "../../../console/aarch32/console.S" |
| 17 | |
Daniel Boulby | 05e7f56 | 2018-09-19 13:58:20 +0100 | [diff] [blame] | 18 | /* |
| 19 | * "core" functions are low-level implementations that don't require |
| 20 | * writeable memory and are thus safe to call in BL1 crash context. |
| 21 | */ |
| 22 | .globl console_pl011_core_init |
| 23 | .globl console_pl011_core_putc |
| 24 | .globl console_pl011_core_getc |
| 25 | .globl console_pl011_core_flush |
| 26 | |
| 27 | .globl console_pl011_putc |
| 28 | .globl console_pl011_getc |
| 29 | .globl console_pl011_flush |
Soby Mathew | 420cc37 | 2016-03-24 16:52:40 +0000 | [diff] [blame] | 30 | |
| 31 | |
| 32 | /* ----------------------------------------------- |
| 33 | * int console_core_init(uintptr_t base_addr, |
| 34 | * unsigned int uart_clk, unsigned int baud_rate) |
| 35 | * Function to initialize the console without a |
| 36 | * C Runtime to print debug information. This |
| 37 | * function will be accessed by console_init and |
| 38 | * crash reporting. |
| 39 | * In: r0 - console base address |
| 40 | * r1 - Uart clock in Hz |
| 41 | * r2 - Baud rate |
| 42 | * Out: return 1 on success else 0 on error |
| 43 | * Clobber list : r1, r2, r3 |
| 44 | * ----------------------------------------------- |
| 45 | */ |
Daniel Boulby | 05e7f56 | 2018-09-19 13:58:20 +0100 | [diff] [blame] | 46 | func console_pl011_core_init |
Soby Mathew | 420cc37 | 2016-03-24 16:52:40 +0000 | [diff] [blame] | 47 | /* Check the input base address */ |
| 48 | cmp r0, #0 |
| 49 | beq core_init_fail |
| 50 | #if !PL011_GENERIC_UART |
| 51 | /* Check baud rate and uart clock for sanity */ |
| 52 | cmp r1, #0 |
| 53 | beq core_init_fail |
| 54 | cmp r2, #0 |
| 55 | beq core_init_fail |
| 56 | /* Disable the UART before initialization */ |
| 57 | ldr r3, [r0, #UARTCR] |
| 58 | bic r3, r3, #PL011_UARTCR_UARTEN |
| 59 | str r3, [r0, #UARTCR] |
| 60 | /* Program the baudrate */ |
| 61 | /* Divisor = (Uart clock * 4) / baudrate */ |
| 62 | lsl r1, r1, #2 |
| 63 | udiv r2, r1, r2 |
| 64 | /* IBRD = Divisor >> 6 */ |
| 65 | lsr r1, r2, #6 |
| 66 | /* Write the IBRD */ |
| 67 | str r1, [r0, #UARTIBRD] |
| 68 | /* FBRD = Divisor & 0x3F */ |
| 69 | and r1, r2, #0x3f |
| 70 | /* Write the FBRD */ |
| 71 | str r1, [r0, #UARTFBRD] |
| 72 | mov r1, #PL011_LINE_CONTROL |
| 73 | str r1, [r0, #UARTLCR_H] |
| 74 | /* Clear any pending errors */ |
| 75 | mov r1, #0 |
| 76 | str r1, [r0, #UARTECR] |
| 77 | /* Enable tx, rx, and uart overall */ |
| 78 | ldr r1, =(PL011_UARTCR_RXE | PL011_UARTCR_TXE | PL011_UARTCR_UARTEN) |
| 79 | str r1, [r0, #UARTCR] |
| 80 | #endif |
| 81 | mov r0, #1 |
| 82 | bx lr |
| 83 | core_init_fail: |
| 84 | mov r0, #0 |
| 85 | bx lr |
Daniel Boulby | 05e7f56 | 2018-09-19 13:58:20 +0100 | [diff] [blame] | 86 | endfunc console_pl011_core_init |
| 87 | |
| 88 | #if MULTI_CONSOLE_API |
| 89 | .globl console_pl011_register |
| 90 | |
| 91 | /* ------------------------------------------------------- |
| 92 | * init console_pl011_register(console_pl011_t *console, |
| 93 | * uintptr_t base, uint32_t clk, uint32_t baud) |
| 94 | * Function to initialize and register a new PL011 |
| 95 | * console. Storage passed in for the console struct |
| 96 | * *must* be persistent (i.e. not from the stack). |
| 97 | * In: r0 - UART register base address |
| 98 | * r1 - UART clock in Hz |
| 99 | * r2 - Baud rate |
| 100 | * r3 - pointer to empty console_pl011_t struct |
| 101 | * Out: return 1 on success, 0 on error |
| 102 | * Clobber list : r0, r1, r2 |
| 103 | * ------------------------------------------------------- |
| 104 | */ |
| 105 | func console_pl011_register |
| 106 | push {r4, lr} |
| 107 | mov r4, r3 |
| 108 | cmp r4, #0 |
| 109 | beq register_fail |
| 110 | str r0, [r4, #CONSOLE_T_PL011_BASE] |
| 111 | |
| 112 | bl console_pl011_core_init |
| 113 | cmp r0, #0 |
| 114 | beq register_fail |
| 115 | |
| 116 | mov r0, r4 |
| 117 | pop {r4, lr} |
| 118 | finish_console_register pl011 |
| 119 | |
| 120 | register_fail: |
| 121 | pop {r4, pc} |
| 122 | endfunc console_pl011_register |
| 123 | #else |
| 124 | .globl console_core_init |
| 125 | .globl console_core_putc |
| 126 | .globl console_core_getc |
| 127 | .globl console_core_flush |
| 128 | .equ console_core_init, console_pl011_core_init |
| 129 | .equ console_core_putc, console_pl011_core_putc |
| 130 | .equ console_core_getc, console_pl011_core_getc |
| 131 | .equ console_core_flush, console_pl011_core_flush |
| 132 | #endif |
Soby Mathew | 420cc37 | 2016-03-24 16:52:40 +0000 | [diff] [blame] | 133 | |
| 134 | /* -------------------------------------------------------- |
| 135 | * int console_core_putc(int c, uintptr_t base_addr) |
| 136 | * Function to output a character over the console. It |
| 137 | * returns the character printed on success or -1 on error. |
| 138 | * In : r0 - character to be printed |
| 139 | * r1 - console base address |
| 140 | * Out : return -1 on error else return character. |
| 141 | * Clobber list : r2 |
| 142 | * -------------------------------------------------------- |
| 143 | */ |
Daniel Boulby | 05e7f56 | 2018-09-19 13:58:20 +0100 | [diff] [blame] | 144 | func console_pl011_core_putc |
Soby Mathew | 420cc37 | 2016-03-24 16:52:40 +0000 | [diff] [blame] | 145 | /* Check the input parameter */ |
| 146 | cmp r1, #0 |
| 147 | beq putc_error |
| 148 | /* Prepend '\r' to '\n' */ |
| 149 | cmp r0, #0xA |
| 150 | bne 2f |
| 151 | 1: |
| 152 | /* Check if the transmit FIFO is full */ |
| 153 | ldr r2, [r1, #UARTFR] |
Yatharth Kochar | d0f5f9c | 2016-11-09 15:39:25 +0000 | [diff] [blame] | 154 | tst r2, #PL011_UARTFR_TXFF |
| 155 | bne 1b |
Soby Mathew | 420cc37 | 2016-03-24 16:52:40 +0000 | [diff] [blame] | 156 | mov r2, #0xD |
| 157 | str r2, [r1, #UARTDR] |
| 158 | 2: |
| 159 | /* Check if the transmit FIFO is full */ |
| 160 | ldr r2, [r1, #UARTFR] |
Yatharth Kochar | d0f5f9c | 2016-11-09 15:39:25 +0000 | [diff] [blame] | 161 | tst r2, #PL011_UARTFR_TXFF |
| 162 | bne 2b |
Soby Mathew | 420cc37 | 2016-03-24 16:52:40 +0000 | [diff] [blame] | 163 | str r0, [r1, #UARTDR] |
| 164 | bx lr |
| 165 | putc_error: |
| 166 | mov r0, #-1 |
| 167 | bx lr |
Daniel Boulby | 05e7f56 | 2018-09-19 13:58:20 +0100 | [diff] [blame] | 168 | endfunc console_pl011_core_putc |
| 169 | |
| 170 | /* -------------------------------------------------------- |
| 171 | * int console_pl011_putc(int c, console_pl011_t *console) |
| 172 | * Function to output a character over the console. It |
| 173 | * returns the character printed on success or -1 on error. |
| 174 | * In: r0 - character to be printed |
| 175 | * r1 - pointer to console_t structure |
| 176 | * Out : return -1 on error else return character. |
| 177 | * Clobber list: r2 |
| 178 | * ------------------------------------------------------- |
| 179 | */ |
| 180 | func console_pl011_putc |
| 181 | #if ENABLE_ASSERTIONS |
| 182 | cmp r1, #0 |
| 183 | ASM_ASSERT(ne) |
| 184 | #endif /* ENABLE_ASSERTIONS */ |
| 185 | ldr r1, [r1, #CONSOLE_T_PL011_BASE] |
| 186 | b console_pl011_core_putc |
| 187 | endfunc console_pl011_putc |
Soby Mathew | 420cc37 | 2016-03-24 16:52:40 +0000 | [diff] [blame] | 188 | |
| 189 | /* --------------------------------------------- |
| 190 | * int console_core_getc(uintptr_t base_addr) |
| 191 | * Function to get a character from the console. |
| 192 | * It returns the character grabbed on success |
| 193 | * or -1 on error. |
| 194 | * In : r0 - console base address |
| 195 | * Clobber list : r0, r1 |
| 196 | * --------------------------------------------- |
| 197 | */ |
Daniel Boulby | 05e7f56 | 2018-09-19 13:58:20 +0100 | [diff] [blame] | 198 | func console_pl011_core_getc |
Soby Mathew | 420cc37 | 2016-03-24 16:52:40 +0000 | [diff] [blame] | 199 | cmp r0, #0 |
| 200 | beq getc_error |
| 201 | 1: |
| 202 | /* Check if the receive FIFO is empty */ |
| 203 | ldr r1, [r0, #UARTFR] |
Yatharth Kochar | d0f5f9c | 2016-11-09 15:39:25 +0000 | [diff] [blame] | 204 | tst r1, #PL011_UARTFR_RXFE |
| 205 | bne 1b |
Soby Mathew | 420cc37 | 2016-03-24 16:52:40 +0000 | [diff] [blame] | 206 | ldr r1, [r0, #UARTDR] |
| 207 | mov r0, r1 |
| 208 | bx lr |
| 209 | getc_error: |
| 210 | mov r0, #-1 |
| 211 | bx lr |
Daniel Boulby | 05e7f56 | 2018-09-19 13:58:20 +0100 | [diff] [blame] | 212 | endfunc console_pl011_core_getc |
| 213 | |
| 214 | /* ------------------------------------------------ |
| 215 | * int console_pl011_getc(console_pl011_t *console) |
| 216 | * Function to get a character from the console. |
| 217 | * It returns the character grabbed on success |
| 218 | * or -1 if no character is available. |
| 219 | * In : r0 - pointer to console_t structure |
| 220 | * Out: r0 - character if available, else -1 |
| 221 | * Clobber list: r0, r1 |
| 222 | * ------------------------------------------------ |
| 223 | */ |
| 224 | func console_pl011_getc |
| 225 | #if ENABLE_ASSERTIONS |
| 226 | cmp r0, #0 |
| 227 | ASM_ASSERT(ne) |
| 228 | #endif /* ENABLE_ASSERTIONS */ |
| 229 | ldr r0, [r0, #CONSOLE_T_PL011_BASE] |
| 230 | b console_pl011_core_getc |
| 231 | endfunc console_pl011_getc |
Antonio Nino Diaz | 8377ae4 | 2017-02-06 16:03:41 +0000 | [diff] [blame] | 232 | |
| 233 | /* --------------------------------------------- |
| 234 | * int console_core_flush(uintptr_t base_addr) |
| 235 | * Function to force a write of all buffered |
| 236 | * data that hasn't been output. |
| 237 | * In : r0 - console base address |
| 238 | * Out : return -1 on error else return 0. |
| 239 | * Clobber list : r0, r1 |
| 240 | * --------------------------------------------- |
| 241 | */ |
Daniel Boulby | 05e7f56 | 2018-09-19 13:58:20 +0100 | [diff] [blame] | 242 | func console_pl011_core_flush |
Antonio Nino Diaz | 8377ae4 | 2017-02-06 16:03:41 +0000 | [diff] [blame] | 243 | cmp r0, #0 |
| 244 | beq flush_error |
| 245 | |
| 246 | 1: |
| 247 | /* Loop while the transmit FIFO is busy */ |
| 248 | ldr r1, [r0, #UARTFR] |
| 249 | tst r1, #PL011_UARTFR_BUSY |
| 250 | bne 1b |
| 251 | |
| 252 | mov r0, #0 |
| 253 | bx lr |
| 254 | flush_error: |
| 255 | mov r0, #-1 |
| 256 | bx lr |
Daniel Boulby | 05e7f56 | 2018-09-19 13:58:20 +0100 | [diff] [blame] | 257 | endfunc console_pl011_core_flush |
| 258 | |
| 259 | /* --------------------------------------------- |
| 260 | * int console_pl011_flush(console_pl011_t *console) |
| 261 | * Function to force a write of all buffered |
| 262 | * data that hasn't been output. |
| 263 | * In : r0 - pointer to console_t structure |
| 264 | * Out : return -1 on error else return 0. |
| 265 | * Clobber list: r0, r1 |
| 266 | * --------------------------------------------- |
| 267 | */ |
| 268 | func console_pl011_flush |
| 269 | #if ENABLE_ASSERTIONS |
| 270 | cmp r0, #0 |
| 271 | ASM_ASSERT(ne) |
| 272 | #endif /* ENABLE_ASSERTIONS */ |
| 273 | ldr r0, [r0, #CONSOLE_T_PL011_BASE] |
| 274 | b console_pl011_core_flush |
| 275 | endfunc console_pl011_flush |