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