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