| 1 |
/* |
| 2 |
* Copyright (c) 2016-2020, ARM Limited and Contributors. All rights reserved. |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: BSD-3-Clause |
| 5 |
*/ |
| 6 |
#include <arch.h> |
| 7 |
#include <asm_macros.S> |
| 8 |
#include <assert_macros.S> |
| 9 |
#include <console_macros.S> |
| 10 |
#include <drivers/arm/pl011.h> |
| 11 |
|
| 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 |
| 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 |
*/ |
| 40 |
func console_pl011_core_init |
| 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 |
| 57 |
#if (ARM_ARCH_MAJOR == 7) && !defined(ARMV7_SUPPORTS_VIRTUALIZATION) |
| 58 |
push {r0,r3} |
| 59 |
softudiv r0,r1,r2,r3 |
| 60 |
mov r2, r0 |
| 61 |
pop {r0,r3} |
| 62 |
#else |
| 63 |
udiv r2, r1, r2 |
| 64 |
#endif |
| 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 |
| 87 |
endfunc console_pl011_core_init |
| 88 |
|
| 89 |
.globl console_pl011_register |
| 90 |
|
| 91 |
/* ------------------------------------------------------- |
| 92 |
* int console_pl011_register(uintptr_t baseaddr, |
| 93 |
* uint32_t clock, uint32_t baud, |
| 94 |
* console_t *console); |
| 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_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_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} |
| 119 |
finish_console_register pl011 putc=1, getc=ENABLE_CONSOLE_GETC, flush=1 |
| 120 |
|
| 121 |
register_fail: |
| 122 |
pop {r4, pc} |
| 123 |
endfunc console_pl011_register |
| 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 |
*/ |
| 135 |
func console_pl011_core_putc |
| 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] |
| 145 |
tst r2, #PL011_UARTFR_TXFF |
| 146 |
bne 1b |
| 147 |
mov r2, #0xD |
| 148 |
str r2, [r1, #UARTDR] |
| 149 |
2: |
| 150 |
/* Check if the transmit FIFO is full */ |
| 151 |
ldr r2, [r1, #UARTFR] |
| 152 |
tst r2, #PL011_UARTFR_TXFF |
| 153 |
bne 2b |
| 154 |
str r0, [r1, #UARTDR] |
| 155 |
bx lr |
| 156 |
putc_error: |
| 157 |
mov r0, #-1 |
| 158 |
bx lr |
| 159 |
endfunc console_pl011_core_putc |
| 160 |
|
| 161 |
/* -------------------------------------------------------- |
| 162 |
* int console_pl011_putc(int c, console_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_BASE] |
| 177 |
b console_pl011_core_putc |
| 178 |
endfunc console_pl011_putc |
| 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 |
*/ |
| 189 |
func console_pl011_core_getc |
| 190 |
cmp r0, #0 |
| 191 |
beq getc_error |
| 192 |
1: |
| 193 |
/* Check if the receive FIFO is empty */ |
| 194 |
ldr r1, [r0, #UARTFR] |
| 195 |
tst r1, #PL011_UARTFR_RXFE |
| 196 |
bne 1b |
| 197 |
ldr r1, [r0, #UARTDR] |
| 198 |
mov r0, r1 |
| 199 |
bx lr |
| 200 |
getc_error: |
| 201 |
mov r0, #-1 |
| 202 |
bx lr |
| 203 |
endfunc console_pl011_core_getc |
| 204 |
|
| 205 |
/* ------------------------------------------------ |
| 206 |
* int console_pl011_getc(console_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_BASE] |
| 221 |
b console_pl011_core_getc |
| 222 |
endfunc console_pl011_getc |
| 223 |
|
| 224 |
/* --------------------------------------------- |
| 225 |
* void 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 : void |
| 230 |
* Clobber list : r0, r1 |
| 231 |
* --------------------------------------------- |
| 232 |
*/ |
| 233 |
func console_pl011_core_flush |
| 234 |
#if ENABLE_ASSERTIONS |
| 235 |
cmp r0, #0 |
| 236 |
ASM_ASSERT(ne) |
| 237 |
#endif /* ENABLE_ASSERTIONS */ |
| 238 |
|
| 239 |
1: |
| 240 |
/* Loop while the transmit FIFO is busy */ |
| 241 |
ldr r1, [r0, #UARTFR] |
| 242 |
tst r1, #PL011_UARTFR_BUSY |
| 243 |
bne 1b |
| 244 |
|
| 245 |
bx lr |
| 246 |
endfunc console_pl011_core_flush |
| 247 |
|
| 248 |
/* --------------------------------------------- |
| 249 |
* void console_pl011_flush(console_t *console) |
| 250 |
* Function to force a write of all buffered |
| 251 |
* data that hasn't been output. |
| 252 |
* In : r0 - pointer to console_t structure |
| 253 |
* Out : void |
| 254 |
* Clobber list: r0, r1 |
| 255 |
* --------------------------------------------- |
| 256 |
*/ |
| 257 |
func console_pl011_flush |
| 258 |
#if ENABLE_ASSERTIONS |
| 259 |
cmp r0, #0 |
| 260 |
ASM_ASSERT(ne) |
| 261 |
#endif /* ENABLE_ASSERTIONS */ |
| 262 |
ldr r0, [r0, #CONSOLE_T_BASE] |
| 263 |
b console_pl011_core_flush |
| 264 |
endfunc console_pl011_flush |