Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 1 | /* |
Jimmy Brisson | 39f9eee | 2020-08-05 13:44:05 -0500 | [diff] [blame] | 2 | * Copyright (c) 2013-2020, ARM Limited and Contributors. All rights reserved. |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 5 | */ |
| 6 | #include <arch.h> |
| 7 | #include <asm_macros.S> |
Julius Werner | ca3930a | 2017-09-18 16:59:43 -0700 | [diff] [blame] | 8 | #include <assert_macros.S> |
Michalis Pappas | 2e9a8a3 | 2018-03-04 14:16:01 +0800 | [diff] [blame] | 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 | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 11 | |
Julius Werner | ca3930a | 2017-09-18 16:59:43 -0700 | [diff] [blame] | 12 | /* |
| 13 | * "core" functions are low-level implementations that don't require |
| 14 | * writable 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 |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 20 | |
Julius Werner | ca3930a | 2017-09-18 16:59:43 -0700 | [diff] [blame] | 21 | .globl console_pl011_putc |
| 22 | .globl console_pl011_getc |
| 23 | .globl console_pl011_flush |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 24 | |
| 25 | /* ----------------------------------------------- |
Julius Werner | ca3930a | 2017-09-18 16:59:43 -0700 | [diff] [blame] | 26 | * int console_pl011_core_init(uintptr_t base_addr, |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 27 | * unsigned int uart_clk, unsigned int baud_rate) |
| 28 | * Function to initialize the console without a |
| 29 | * C Runtime to print debug information. This |
| 30 | * function will be accessed by console_init and |
| 31 | * crash reporting. |
| 32 | * In: x0 - console base address |
| 33 | * w1 - Uart clock in Hz |
| 34 | * w2 - Baud rate |
| 35 | * Out: return 1 on success else 0 on error |
| 36 | * Clobber list : x1, x2, x3, x4 |
| 37 | * ----------------------------------------------- |
| 38 | */ |
Julius Werner | ca3930a | 2017-09-18 16:59:43 -0700 | [diff] [blame] | 39 | func console_pl011_core_init |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 40 | /* Check the input base address */ |
| 41 | cbz x0, core_init_fail |
| 42 | #if !PL011_GENERIC_UART |
| 43 | /* Check baud rate and uart clock for sanity */ |
| 44 | cbz w1, core_init_fail |
| 45 | cbz w2, core_init_fail |
| 46 | /* Disable uart before programming */ |
| 47 | ldr w3, [x0, #UARTCR] |
| 48 | mov w4, #PL011_UARTCR_UARTEN |
| 49 | bic w3, w3, w4 |
| 50 | str w3, [x0, #UARTCR] |
| 51 | /* Program the baudrate */ |
| 52 | /* Divisor = (Uart clock * 4) / baudrate */ |
| 53 | lsl w1, w1, #2 |
| 54 | udiv w2, w1, w2 |
| 55 | /* IBRD = Divisor >> 6 */ |
| 56 | lsr w1, w2, #6 |
| 57 | /* Write the IBRD */ |
| 58 | str w1, [x0, #UARTIBRD] |
| 59 | /* FBRD = Divisor & 0x3F */ |
| 60 | and w1, w2, #0x3f |
| 61 | /* Write the FBRD */ |
| 62 | str w1, [x0, #UARTFBRD] |
| 63 | mov w1, #PL011_LINE_CONTROL |
| 64 | str w1, [x0, #UARTLCR_H] |
| 65 | /* Clear any pending errors */ |
| 66 | str wzr, [x0, #UARTECR] |
| 67 | /* Enable tx, rx, and uart overall */ |
| 68 | mov w1, #(PL011_UARTCR_RXE | PL011_UARTCR_TXE | PL011_UARTCR_UARTEN) |
| 69 | str w1, [x0, #UARTCR] |
| 70 | #endif |
| 71 | mov w0, #1 |
| 72 | ret |
| 73 | core_init_fail: |
| 74 | mov w0, wzr |
| 75 | ret |
Julius Werner | ca3930a | 2017-09-18 16:59:43 -0700 | [diff] [blame] | 76 | endfunc console_pl011_core_init |
| 77 | |
Julius Werner | ca3930a | 2017-09-18 16:59:43 -0700 | [diff] [blame] | 78 | .globl console_pl011_register |
| 79 | |
| 80 | /* ----------------------------------------------- |
Antonio Nino Diaz | 992c5ac | 2018-10-08 13:26:48 +0100 | [diff] [blame] | 81 | * int console_pl011_register(uintptr_t baseaddr, |
| 82 | * uint32_t clock, uint32_t baud, |
Andre Przywara | 2b1b1a5 | 2020-01-25 00:58:35 +0000 | [diff] [blame] | 83 | * console_t *console); |
Julius Werner | ca3930a | 2017-09-18 16:59:43 -0700 | [diff] [blame] | 84 | * Function to initialize and register a new PL011 |
| 85 | * console. Storage passed in for the console struct |
| 86 | * *must* be persistent (i.e. not from the stack). |
| 87 | * In: x0 - UART register base address |
| 88 | * w1 - UART clock in Hz |
| 89 | * w2 - Baud rate |
Andre Przywara | 2b1b1a5 | 2020-01-25 00:58:35 +0000 | [diff] [blame] | 90 | * x3 - pointer to empty console_t struct |
Julius Werner | ca3930a | 2017-09-18 16:59:43 -0700 | [diff] [blame] | 91 | * Out: return 1 on success, 0 on error |
| 92 | * Clobber list : x0, x1, x2, x6, x7, x14 |
| 93 | * ----------------------------------------------- |
| 94 | */ |
| 95 | func console_pl011_register |
| 96 | mov x7, x30 |
| 97 | mov x6, x3 |
| 98 | cbz x6, register_fail |
Andre Przywara | 2b1b1a5 | 2020-01-25 00:58:35 +0000 | [diff] [blame] | 99 | str x0, [x6, #CONSOLE_T_BASE] |
Julius Werner | ca3930a | 2017-09-18 16:59:43 -0700 | [diff] [blame] | 100 | |
| 101 | bl console_pl011_core_init |
| 102 | cbz x0, register_fail |
| 103 | |
| 104 | mov x0, x6 |
| 105 | mov x30, x7 |
Soby Mathew | 58873ae | 2018-10-10 16:03:09 +0100 | [diff] [blame] | 106 | finish_console_register pl011 putc=1, getc=1, flush=1 |
Julius Werner | ca3930a | 2017-09-18 16:59:43 -0700 | [diff] [blame] | 107 | |
| 108 | register_fail: |
| 109 | ret x7 |
| 110 | endfunc console_pl011_register |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 111 | |
| 112 | /* -------------------------------------------------------- |
Julius Werner | ca3930a | 2017-09-18 16:59:43 -0700 | [diff] [blame] | 113 | * int console_pl011_core_putc(int c, uintptr_t base_addr) |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 114 | * Function to output a character over the console. It |
| 115 | * returns the character printed on success or -1 on error. |
| 116 | * In : w0 - character to be printed |
| 117 | * x1 - console base address |
| 118 | * Out : return -1 on error else return character. |
| 119 | * Clobber list : x2 |
| 120 | * -------------------------------------------------------- |
| 121 | */ |
Julius Werner | ca3930a | 2017-09-18 16:59:43 -0700 | [diff] [blame] | 122 | func console_pl011_core_putc |
| 123 | #if ENABLE_ASSERTIONS |
| 124 | cmp x1, #0 |
| 125 | ASM_ASSERT(ne) |
| 126 | #endif /* ENABLE_ASSERTIONS */ |
| 127 | |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 128 | /* Prepend '\r' to '\n' */ |
| 129 | cmp w0, #0xA |
| 130 | b.ne 2f |
| 131 | 1: |
| 132 | /* Check if the transmit FIFO is full */ |
| 133 | ldr w2, [x1, #UARTFR] |
| 134 | tbnz w2, #PL011_UARTFR_TXFF_BIT, 1b |
| 135 | mov w2, #0xD |
| 136 | str w2, [x1, #UARTDR] |
| 137 | 2: |
| 138 | /* Check if the transmit FIFO is full */ |
| 139 | ldr w2, [x1, #UARTFR] |
| 140 | tbnz w2, #PL011_UARTFR_TXFF_BIT, 2b |
| 141 | str w0, [x1, #UARTDR] |
| 142 | ret |
Julius Werner | ca3930a | 2017-09-18 16:59:43 -0700 | [diff] [blame] | 143 | endfunc console_pl011_core_putc |
| 144 | |
| 145 | /* -------------------------------------------------------- |
Andre Przywara | 2b1b1a5 | 2020-01-25 00:58:35 +0000 | [diff] [blame] | 146 | * int console_pl011_putc(int c, console_t *console) |
Julius Werner | ca3930a | 2017-09-18 16:59:43 -0700 | [diff] [blame] | 147 | * Function to output a character over the console. It |
| 148 | * returns the character printed on success or -1 on error. |
| 149 | * In : w0 - character to be printed |
| 150 | * x1 - pointer to console_t structure |
| 151 | * Out : return -1 on error else return character. |
| 152 | * Clobber list : x2 |
| 153 | * -------------------------------------------------------- |
| 154 | */ |
| 155 | func console_pl011_putc |
| 156 | #if ENABLE_ASSERTIONS |
| 157 | cmp x1, #0 |
| 158 | ASM_ASSERT(ne) |
| 159 | #endif /* ENABLE_ASSERTIONS */ |
Andre Przywara | 2b1b1a5 | 2020-01-25 00:58:35 +0000 | [diff] [blame] | 160 | ldr x1, [x1, #CONSOLE_T_BASE] |
Julius Werner | ca3930a | 2017-09-18 16:59:43 -0700 | [diff] [blame] | 161 | b console_pl011_core_putc |
| 162 | endfunc console_pl011_putc |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 163 | |
| 164 | /* --------------------------------------------- |
Julius Werner | ca3930a | 2017-09-18 16:59:43 -0700 | [diff] [blame] | 165 | * int console_pl011_core_getc(uintptr_t base_addr) |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 166 | * Function to get a character from the console. |
| 167 | * It returns the character grabbed on success |
Julius Werner | ca3930a | 2017-09-18 16:59:43 -0700 | [diff] [blame] | 168 | * or -1 if no character is available. |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 169 | * In : x0 - console base address |
Julius Werner | ca3930a | 2017-09-18 16:59:43 -0700 | [diff] [blame] | 170 | * Out: w0 - character if available, else -1 |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 171 | * Clobber list : x0, x1 |
| 172 | * --------------------------------------------- |
| 173 | */ |
Julius Werner | ca3930a | 2017-09-18 16:59:43 -0700 | [diff] [blame] | 174 | func console_pl011_core_getc |
| 175 | #if ENABLE_ASSERTIONS |
| 176 | cmp x0, #0 |
| 177 | ASM_ASSERT(ne) |
| 178 | #endif /* ENABLE_ASSERTIONS */ |
| 179 | |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 180 | /* Check if the receive FIFO is empty */ |
| 181 | ldr w1, [x0, #UARTFR] |
Julius Werner | ca3930a | 2017-09-18 16:59:43 -0700 | [diff] [blame] | 182 | tbnz w1, #PL011_UARTFR_RXFE_BIT, no_char |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 183 | ldr w1, [x0, #UARTDR] |
| 184 | mov w0, w1 |
| 185 | ret |
Julius Werner | ca3930a | 2017-09-18 16:59:43 -0700 | [diff] [blame] | 186 | no_char: |
| 187 | mov w0, #ERROR_NO_PENDING_CHAR |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 188 | ret |
Julius Werner | ca3930a | 2017-09-18 16:59:43 -0700 | [diff] [blame] | 189 | endfunc console_pl011_core_getc |
| 190 | |
| 191 | /* --------------------------------------------- |
Andre Przywara | 2b1b1a5 | 2020-01-25 00:58:35 +0000 | [diff] [blame] | 192 | * int console_pl011_getc(console_t *console) |
Julius Werner | ca3930a | 2017-09-18 16:59:43 -0700 | [diff] [blame] | 193 | * Function to get a character from the console. |
| 194 | * It returns the character grabbed on success |
| 195 | * or -1 if no character is available. |
| 196 | * In : x0 - pointer to console_t structure |
| 197 | * Out: w0 - character if available, else -1 |
| 198 | * Clobber list : x0, x1 |
| 199 | * --------------------------------------------- |
| 200 | */ |
| 201 | func console_pl011_getc |
| 202 | #if ENABLE_ASSERTIONS |
| 203 | cmp x0, #0 |
| 204 | ASM_ASSERT(ne) |
| 205 | #endif /* ENABLE_ASSERTIONS */ |
Andre Przywara | 2b1b1a5 | 2020-01-25 00:58:35 +0000 | [diff] [blame] | 206 | ldr x0, [x0, #CONSOLE_T_BASE] |
Julius Werner | ca3930a | 2017-09-18 16:59:43 -0700 | [diff] [blame] | 207 | b console_pl011_core_getc |
| 208 | endfunc console_pl011_getc |
Antonio Nino Diaz | 8377ae4 | 2017-02-06 16:03:41 +0000 | [diff] [blame] | 209 | |
| 210 | /* --------------------------------------------- |
Jimmy Brisson | 39f9eee | 2020-08-05 13:44:05 -0500 | [diff] [blame] | 211 | * void console_pl011_core_flush(uintptr_t base_addr) |
Antonio Nino Diaz | 8377ae4 | 2017-02-06 16:03:41 +0000 | [diff] [blame] | 212 | * Function to force a write of all buffered |
| 213 | * data that hasn't been output. |
| 214 | * In : x0 - console base address |
Jimmy Brisson | 39f9eee | 2020-08-05 13:44:05 -0500 | [diff] [blame] | 215 | * Out : void. |
Antonio Nino Diaz | 8377ae4 | 2017-02-06 16:03:41 +0000 | [diff] [blame] | 216 | * Clobber list : x0, x1 |
| 217 | * --------------------------------------------- |
| 218 | */ |
Julius Werner | ca3930a | 2017-09-18 16:59:43 -0700 | [diff] [blame] | 219 | func console_pl011_core_flush |
| 220 | #if ENABLE_ASSERTIONS |
| 221 | cmp x0, #0 |
| 222 | ASM_ASSERT(ne) |
| 223 | #endif /* ENABLE_ASSERTIONS */ |
Antonio Nino Diaz | 8377ae4 | 2017-02-06 16:03:41 +0000 | [diff] [blame] | 224 | 1: |
| 225 | /* Loop until the transmit FIFO is empty */ |
| 226 | ldr w1, [x0, #UARTFR] |
| 227 | tbnz w1, #PL011_UARTFR_BUSY_BIT, 1b |
Antonio Nino Diaz | 8377ae4 | 2017-02-06 16:03:41 +0000 | [diff] [blame] | 228 | ret |
Julius Werner | ca3930a | 2017-09-18 16:59:43 -0700 | [diff] [blame] | 229 | endfunc console_pl011_core_flush |
| 230 | |
| 231 | /* --------------------------------------------- |
Jimmy Brisson | 39f9eee | 2020-08-05 13:44:05 -0500 | [diff] [blame] | 232 | * void console_pl011_flush(console_t *console) |
Julius Werner | ca3930a | 2017-09-18 16:59:43 -0700 | [diff] [blame] | 233 | * Function to force a write of all buffered |
| 234 | * data that hasn't been output. |
| 235 | * In : x0 - pointer to console_t structure |
Jimmy Brisson | 39f9eee | 2020-08-05 13:44:05 -0500 | [diff] [blame] | 236 | * Out : void |
Julius Werner | ca3930a | 2017-09-18 16:59:43 -0700 | [diff] [blame] | 237 | * Clobber list : x0, x1 |
| 238 | * --------------------------------------------- |
| 239 | */ |
| 240 | func console_pl011_flush |
| 241 | #if ENABLE_ASSERTIONS |
| 242 | cmp x0, #0 |
| 243 | ASM_ASSERT(ne) |
| 244 | #endif /* ENABLE_ASSERTIONS */ |
Andre Przywara | 2b1b1a5 | 2020-01-25 00:58:35 +0000 | [diff] [blame] | 245 | ldr x0, [x0, #CONSOLE_T_BASE] |
Julius Werner | ca3930a | 2017-09-18 16:59:43 -0700 | [diff] [blame] | 246 | b console_pl011_core_flush |
| 247 | endfunc console_pl011_flush |