166be868eSSoby Mathew/* 27e2bbef9SDaniel Boulby * Copyright (c) 2016-2018, ARM Limited and Contributors. All rights reserved. 366be868eSSoby Mathew * 482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause 566be868eSSoby Mathew */ 666be868eSSoby Mathew#include <arch.h> 766be868eSSoby Mathew#include <asm_macros.S> 87e2bbef9SDaniel Boulby#include <assert_macros.S> 9*cc5859caSSoby Mathew#define USE_FINISH_CONSOLE_REG_2 107e2bbef9SDaniel Boulby#include <console_macros.S> 1166be868eSSoby Mathew#include <pl011.h> 1266be868eSSoby Mathew 1366be868eSSoby Mathew/* 1466be868eSSoby Mathew * Pull in generic functions to provide backwards compatibility for 1566be868eSSoby Mathew * platform makefiles 1666be868eSSoby Mathew */ 1766be868eSSoby Mathew#include "../../../console/aarch32/console.S" 1866be868eSSoby Mathew 197e2bbef9SDaniel Boulby /* 207e2bbef9SDaniel Boulby * "core" functions are low-level implementations that don't require 217e2bbef9SDaniel Boulby * writeable memory and are thus safe to call in BL1 crash context. 227e2bbef9SDaniel Boulby */ 237e2bbef9SDaniel Boulby .globl console_pl011_core_init 247e2bbef9SDaniel Boulby .globl console_pl011_core_putc 257e2bbef9SDaniel Boulby .globl console_pl011_core_getc 267e2bbef9SDaniel Boulby .globl console_pl011_core_flush 277e2bbef9SDaniel Boulby 287e2bbef9SDaniel Boulby .globl console_pl011_putc 297e2bbef9SDaniel Boulby .globl console_pl011_getc 307e2bbef9SDaniel Boulby .globl console_pl011_flush 3166be868eSSoby Mathew 3266be868eSSoby Mathew 3366be868eSSoby Mathew /* ----------------------------------------------- 3466be868eSSoby Mathew * int console_core_init(uintptr_t base_addr, 3566be868eSSoby Mathew * unsigned int uart_clk, unsigned int baud_rate) 3666be868eSSoby Mathew * Function to initialize the console without a 3766be868eSSoby Mathew * C Runtime to print debug information. This 3866be868eSSoby Mathew * function will be accessed by console_init and 3966be868eSSoby Mathew * crash reporting. 4066be868eSSoby Mathew * In: r0 - console base address 4166be868eSSoby Mathew * r1 - Uart clock in Hz 4266be868eSSoby Mathew * r2 - Baud rate 4366be868eSSoby Mathew * Out: return 1 on success else 0 on error 4466be868eSSoby Mathew * Clobber list : r1, r2, r3 4566be868eSSoby Mathew * ----------------------------------------------- 4666be868eSSoby Mathew */ 477e2bbef9SDaniel Boulbyfunc console_pl011_core_init 4866be868eSSoby Mathew /* Check the input base address */ 4966be868eSSoby Mathew cmp r0, #0 5066be868eSSoby Mathew beq core_init_fail 5166be868eSSoby Mathew#if !PL011_GENERIC_UART 5266be868eSSoby Mathew /* Check baud rate and uart clock for sanity */ 5366be868eSSoby Mathew cmp r1, #0 5466be868eSSoby Mathew beq core_init_fail 5566be868eSSoby Mathew cmp r2, #0 5666be868eSSoby Mathew beq core_init_fail 5766be868eSSoby Mathew /* Disable the UART before initialization */ 5866be868eSSoby Mathew ldr r3, [r0, #UARTCR] 5966be868eSSoby Mathew bic r3, r3, #PL011_UARTCR_UARTEN 6066be868eSSoby Mathew str r3, [r0, #UARTCR] 6166be868eSSoby Mathew /* Program the baudrate */ 6266be868eSSoby Mathew /* Divisor = (Uart clock * 4) / baudrate */ 6366be868eSSoby Mathew lsl r1, r1, #2 6466be868eSSoby Mathew udiv r2, r1, r2 6566be868eSSoby Mathew /* IBRD = Divisor >> 6 */ 6666be868eSSoby Mathew lsr r1, r2, #6 6766be868eSSoby Mathew /* Write the IBRD */ 6866be868eSSoby Mathew str r1, [r0, #UARTIBRD] 6966be868eSSoby Mathew /* FBRD = Divisor & 0x3F */ 7066be868eSSoby Mathew and r1, r2, #0x3f 7166be868eSSoby Mathew /* Write the FBRD */ 7266be868eSSoby Mathew str r1, [r0, #UARTFBRD] 7366be868eSSoby Mathew mov r1, #PL011_LINE_CONTROL 7466be868eSSoby Mathew str r1, [r0, #UARTLCR_H] 7566be868eSSoby Mathew /* Clear any pending errors */ 7666be868eSSoby Mathew mov r1, #0 7766be868eSSoby Mathew str r1, [r0, #UARTECR] 7866be868eSSoby Mathew /* Enable tx, rx, and uart overall */ 7966be868eSSoby Mathew ldr r1, =(PL011_UARTCR_RXE | PL011_UARTCR_TXE | PL011_UARTCR_UARTEN) 8066be868eSSoby Mathew str r1, [r0, #UARTCR] 8166be868eSSoby Mathew#endif 8266be868eSSoby Mathew mov r0, #1 8366be868eSSoby Mathew bx lr 8466be868eSSoby Mathewcore_init_fail: 8566be868eSSoby Mathew mov r0, #0 8666be868eSSoby Mathew bx lr 877e2bbef9SDaniel Boulbyendfunc console_pl011_core_init 887e2bbef9SDaniel Boulby 897e2bbef9SDaniel Boulby#if MULTI_CONSOLE_API 907e2bbef9SDaniel Boulby .globl console_pl011_register 917e2bbef9SDaniel Boulby 927e2bbef9SDaniel Boulby /* ------------------------------------------------------- 9365199dc8SAntonio Nino Diaz * int console_pl011_register(uintptr_t baseaddr, 9465199dc8SAntonio Nino Diaz * uint32_t clock, uint32_t baud, 9565199dc8SAntonio Nino Diaz * console_pl011_t *console); 967e2bbef9SDaniel Boulby * Function to initialize and register a new PL011 977e2bbef9SDaniel Boulby * console. Storage passed in for the console struct 987e2bbef9SDaniel Boulby * *must* be persistent (i.e. not from the stack). 997e2bbef9SDaniel Boulby * In: r0 - UART register base address 1007e2bbef9SDaniel Boulby * r1 - UART clock in Hz 1017e2bbef9SDaniel Boulby * r2 - Baud rate 1027e2bbef9SDaniel Boulby * r3 - pointer to empty console_pl011_t struct 1037e2bbef9SDaniel Boulby * Out: return 1 on success, 0 on error 1047e2bbef9SDaniel Boulby * Clobber list : r0, r1, r2 1057e2bbef9SDaniel Boulby * ------------------------------------------------------- 1067e2bbef9SDaniel Boulby */ 1077e2bbef9SDaniel Boulbyfunc console_pl011_register 1087e2bbef9SDaniel Boulby push {r4, lr} 1097e2bbef9SDaniel Boulby mov r4, r3 1107e2bbef9SDaniel Boulby cmp r4, #0 1117e2bbef9SDaniel Boulby beq register_fail 1127e2bbef9SDaniel Boulby str r0, [r4, #CONSOLE_T_PL011_BASE] 1137e2bbef9SDaniel Boulby 1147e2bbef9SDaniel Boulby bl console_pl011_core_init 1157e2bbef9SDaniel Boulby cmp r0, #0 1167e2bbef9SDaniel Boulby beq register_fail 1177e2bbef9SDaniel Boulby 1187e2bbef9SDaniel Boulby mov r0, r4 1197e2bbef9SDaniel Boulby pop {r4, lr} 120*cc5859caSSoby Mathew finish_console_register pl011 putc=1, getc=1, flush=1 1217e2bbef9SDaniel Boulby 1227e2bbef9SDaniel Boulbyregister_fail: 1237e2bbef9SDaniel Boulby pop {r4, pc} 1247e2bbef9SDaniel Boulbyendfunc console_pl011_register 1257e2bbef9SDaniel Boulby#else 1267e2bbef9SDaniel Boulby .globl console_core_init 1277e2bbef9SDaniel Boulby .globl console_core_putc 1287e2bbef9SDaniel Boulby .globl console_core_getc 1297e2bbef9SDaniel Boulby .globl console_core_flush 1307e2bbef9SDaniel Boulby .equ console_core_init, console_pl011_core_init 1317e2bbef9SDaniel Boulby .equ console_core_putc, console_pl011_core_putc 1327e2bbef9SDaniel Boulby .equ console_core_getc, console_pl011_core_getc 1337e2bbef9SDaniel Boulby .equ console_core_flush, console_pl011_core_flush 1347e2bbef9SDaniel Boulby#endif 13566be868eSSoby Mathew 13666be868eSSoby Mathew /* -------------------------------------------------------- 13766be868eSSoby Mathew * int console_core_putc(int c, uintptr_t base_addr) 13866be868eSSoby Mathew * Function to output a character over the console. It 13966be868eSSoby Mathew * returns the character printed on success or -1 on error. 14066be868eSSoby Mathew * In : r0 - character to be printed 14166be868eSSoby Mathew * r1 - console base address 14266be868eSSoby Mathew * Out : return -1 on error else return character. 14366be868eSSoby Mathew * Clobber list : r2 14466be868eSSoby Mathew * -------------------------------------------------------- 14566be868eSSoby Mathew */ 1467e2bbef9SDaniel Boulbyfunc console_pl011_core_putc 14766be868eSSoby Mathew /* Check the input parameter */ 14866be868eSSoby Mathew cmp r1, #0 14966be868eSSoby Mathew beq putc_error 15066be868eSSoby Mathew /* Prepend '\r' to '\n' */ 15166be868eSSoby Mathew cmp r0, #0xA 15266be868eSSoby Mathew bne 2f 15366be868eSSoby Mathew1: 15466be868eSSoby Mathew /* Check if the transmit FIFO is full */ 15566be868eSSoby Mathew ldr r2, [r1, #UARTFR] 15669d59e0cSYatharth Kochar tst r2, #PL011_UARTFR_TXFF 15769d59e0cSYatharth Kochar bne 1b 15866be868eSSoby Mathew mov r2, #0xD 15966be868eSSoby Mathew str r2, [r1, #UARTDR] 16066be868eSSoby Mathew2: 16166be868eSSoby Mathew /* Check if the transmit FIFO is full */ 16266be868eSSoby Mathew ldr r2, [r1, #UARTFR] 16369d59e0cSYatharth Kochar tst r2, #PL011_UARTFR_TXFF 16469d59e0cSYatharth Kochar bne 2b 16566be868eSSoby Mathew str r0, [r1, #UARTDR] 16666be868eSSoby Mathew bx lr 16766be868eSSoby Mathewputc_error: 16866be868eSSoby Mathew mov r0, #-1 16966be868eSSoby Mathew bx lr 1707e2bbef9SDaniel Boulbyendfunc console_pl011_core_putc 1717e2bbef9SDaniel Boulby 1727e2bbef9SDaniel Boulby /* -------------------------------------------------------- 1737e2bbef9SDaniel Boulby * int console_pl011_putc(int c, console_pl011_t *console) 1747e2bbef9SDaniel Boulby * Function to output a character over the console. It 1757e2bbef9SDaniel Boulby * returns the character printed on success or -1 on error. 1767e2bbef9SDaniel Boulby * In: r0 - character to be printed 1777e2bbef9SDaniel Boulby * r1 - pointer to console_t structure 1787e2bbef9SDaniel Boulby * Out : return -1 on error else return character. 1797e2bbef9SDaniel Boulby * Clobber list: r2 1807e2bbef9SDaniel Boulby * ------------------------------------------------------- 1817e2bbef9SDaniel Boulby */ 1827e2bbef9SDaniel Boulbyfunc console_pl011_putc 1837e2bbef9SDaniel Boulby#if ENABLE_ASSERTIONS 1847e2bbef9SDaniel Boulby cmp r1, #0 1857e2bbef9SDaniel Boulby ASM_ASSERT(ne) 1867e2bbef9SDaniel Boulby#endif /* ENABLE_ASSERTIONS */ 1877e2bbef9SDaniel Boulby ldr r1, [r1, #CONSOLE_T_PL011_BASE] 1887e2bbef9SDaniel Boulby b console_pl011_core_putc 1897e2bbef9SDaniel Boulbyendfunc console_pl011_putc 19066be868eSSoby Mathew 19166be868eSSoby Mathew /* --------------------------------------------- 19266be868eSSoby Mathew * int console_core_getc(uintptr_t base_addr) 19366be868eSSoby Mathew * Function to get a character from the console. 19466be868eSSoby Mathew * It returns the character grabbed on success 19566be868eSSoby Mathew * or -1 on error. 19666be868eSSoby Mathew * In : r0 - console base address 19766be868eSSoby Mathew * Clobber list : r0, r1 19866be868eSSoby Mathew * --------------------------------------------- 19966be868eSSoby Mathew */ 2007e2bbef9SDaniel Boulbyfunc console_pl011_core_getc 20166be868eSSoby Mathew cmp r0, #0 20266be868eSSoby Mathew beq getc_error 20366be868eSSoby Mathew1: 20466be868eSSoby Mathew /* Check if the receive FIFO is empty */ 20566be868eSSoby Mathew ldr r1, [r0, #UARTFR] 20669d59e0cSYatharth Kochar tst r1, #PL011_UARTFR_RXFE 20769d59e0cSYatharth Kochar bne 1b 20866be868eSSoby Mathew ldr r1, [r0, #UARTDR] 20966be868eSSoby Mathew mov r0, r1 21066be868eSSoby Mathew bx lr 21166be868eSSoby Mathewgetc_error: 21266be868eSSoby Mathew mov r0, #-1 21366be868eSSoby Mathew bx lr 2147e2bbef9SDaniel Boulbyendfunc console_pl011_core_getc 2157e2bbef9SDaniel Boulby 2167e2bbef9SDaniel Boulby /* ------------------------------------------------ 2177e2bbef9SDaniel Boulby * int console_pl011_getc(console_pl011_t *console) 2187e2bbef9SDaniel Boulby * Function to get a character from the console. 2197e2bbef9SDaniel Boulby * It returns the character grabbed on success 2207e2bbef9SDaniel Boulby * or -1 if no character is available. 2217e2bbef9SDaniel Boulby * In : r0 - pointer to console_t structure 2227e2bbef9SDaniel Boulby * Out: r0 - character if available, else -1 2237e2bbef9SDaniel Boulby * Clobber list: r0, r1 2247e2bbef9SDaniel Boulby * ------------------------------------------------ 2257e2bbef9SDaniel Boulby */ 2267e2bbef9SDaniel Boulbyfunc console_pl011_getc 2277e2bbef9SDaniel Boulby#if ENABLE_ASSERTIONS 2287e2bbef9SDaniel Boulby cmp r0, #0 2297e2bbef9SDaniel Boulby ASM_ASSERT(ne) 2307e2bbef9SDaniel Boulby#endif /* ENABLE_ASSERTIONS */ 2317e2bbef9SDaniel Boulby ldr r0, [r0, #CONSOLE_T_PL011_BASE] 2327e2bbef9SDaniel Boulby b console_pl011_core_getc 2337e2bbef9SDaniel Boulbyendfunc console_pl011_getc 23473e05284SAntonio Nino Diaz 23573e05284SAntonio Nino Diaz /* --------------------------------------------- 23673e05284SAntonio Nino Diaz * int console_core_flush(uintptr_t base_addr) 23773e05284SAntonio Nino Diaz * Function to force a write of all buffered 23873e05284SAntonio Nino Diaz * data that hasn't been output. 23973e05284SAntonio Nino Diaz * In : r0 - console base address 24073e05284SAntonio Nino Diaz * Out : return -1 on error else return 0. 24173e05284SAntonio Nino Diaz * Clobber list : r0, r1 24273e05284SAntonio Nino Diaz * --------------------------------------------- 24373e05284SAntonio Nino Diaz */ 2447e2bbef9SDaniel Boulbyfunc console_pl011_core_flush 24573e05284SAntonio Nino Diaz cmp r0, #0 24673e05284SAntonio Nino Diaz beq flush_error 24773e05284SAntonio Nino Diaz 24873e05284SAntonio Nino Diaz1: 24973e05284SAntonio Nino Diaz /* Loop while the transmit FIFO is busy */ 25073e05284SAntonio Nino Diaz ldr r1, [r0, #UARTFR] 25173e05284SAntonio Nino Diaz tst r1, #PL011_UARTFR_BUSY 25273e05284SAntonio Nino Diaz bne 1b 25373e05284SAntonio Nino Diaz 25473e05284SAntonio Nino Diaz mov r0, #0 25573e05284SAntonio Nino Diaz bx lr 25673e05284SAntonio Nino Diazflush_error: 25773e05284SAntonio Nino Diaz mov r0, #-1 25873e05284SAntonio Nino Diaz bx lr 2597e2bbef9SDaniel Boulbyendfunc console_pl011_core_flush 2607e2bbef9SDaniel Boulby 2617e2bbef9SDaniel Boulby /* --------------------------------------------- 2627e2bbef9SDaniel Boulby * int console_pl011_flush(console_pl011_t *console) 2637e2bbef9SDaniel Boulby * Function to force a write of all buffered 2647e2bbef9SDaniel Boulby * data that hasn't been output. 2657e2bbef9SDaniel Boulby * In : r0 - pointer to console_t structure 2667e2bbef9SDaniel Boulby * Out : return -1 on error else return 0. 2677e2bbef9SDaniel Boulby * Clobber list: r0, r1 2687e2bbef9SDaniel Boulby * --------------------------------------------- 2697e2bbef9SDaniel Boulby */ 2707e2bbef9SDaniel Boulbyfunc console_pl011_flush 2717e2bbef9SDaniel Boulby#if ENABLE_ASSERTIONS 2727e2bbef9SDaniel Boulby cmp r0, #0 2737e2bbef9SDaniel Boulby ASM_ASSERT(ne) 2747e2bbef9SDaniel Boulby#endif /* ENABLE_ASSERTIONS */ 2757e2bbef9SDaniel Boulby ldr r0, [r0, #CONSOLE_T_PL011_BASE] 2767e2bbef9SDaniel Boulby b console_pl011_core_flush 2777e2bbef9SDaniel Boulbyendfunc console_pl011_flush 278