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