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> 9cc5859caSSoby Mathew#define USE_FINISH_CONSOLE_REG_2 107e2bbef9SDaniel Boulby#include <console_macros.S> 11*09d40e0eSAntonio Nino Diaz#include <drivers/arm/pl011.h> 1266be868eSSoby Mathew 13985ee0b7SJulius Werner#if !MULTI_CONSOLE_API 1466be868eSSoby Mathew/* 1566be868eSSoby Mathew * Pull in generic functions to provide backwards compatibility for 1666be868eSSoby Mathew * platform makefiles 1766be868eSSoby Mathew */ 1866be868eSSoby Mathew#include "../../../console/aarch32/console.S" 19985ee0b7SJulius Werner#endif 2066be868eSSoby Mathew 217e2bbef9SDaniel Boulby /* 227e2bbef9SDaniel Boulby * "core" functions are low-level implementations that don't require 237e2bbef9SDaniel Boulby * writeable memory and are thus safe to call in BL1 crash context. 247e2bbef9SDaniel Boulby */ 257e2bbef9SDaniel Boulby .globl console_pl011_core_init 267e2bbef9SDaniel Boulby .globl console_pl011_core_putc 277e2bbef9SDaniel Boulby .globl console_pl011_core_getc 287e2bbef9SDaniel Boulby .globl console_pl011_core_flush 297e2bbef9SDaniel Boulby 307e2bbef9SDaniel Boulby .globl console_pl011_putc 317e2bbef9SDaniel Boulby .globl console_pl011_getc 327e2bbef9SDaniel Boulby .globl console_pl011_flush 3366be868eSSoby Mathew 3466be868eSSoby Mathew 3566be868eSSoby Mathew /* ----------------------------------------------- 3666be868eSSoby Mathew * int console_core_init(uintptr_t base_addr, 3766be868eSSoby Mathew * unsigned int uart_clk, unsigned int baud_rate) 3866be868eSSoby Mathew * Function to initialize the console without a 3966be868eSSoby Mathew * C Runtime to print debug information. This 4066be868eSSoby Mathew * function will be accessed by console_init and 4166be868eSSoby Mathew * crash reporting. 4266be868eSSoby Mathew * In: r0 - console base address 4366be868eSSoby Mathew * r1 - Uart clock in Hz 4466be868eSSoby Mathew * r2 - Baud rate 4566be868eSSoby Mathew * Out: return 1 on success else 0 on error 4666be868eSSoby Mathew * Clobber list : r1, r2, r3 4766be868eSSoby Mathew * ----------------------------------------------- 4866be868eSSoby Mathew */ 497e2bbef9SDaniel Boulbyfunc console_pl011_core_init 5066be868eSSoby Mathew /* Check the input base address */ 5166be868eSSoby Mathew cmp r0, #0 5266be868eSSoby Mathew beq core_init_fail 5366be868eSSoby Mathew#if !PL011_GENERIC_UART 5466be868eSSoby Mathew /* Check baud rate and uart clock for sanity */ 5566be868eSSoby Mathew cmp r1, #0 5666be868eSSoby Mathew beq core_init_fail 5766be868eSSoby Mathew cmp r2, #0 5866be868eSSoby Mathew beq core_init_fail 5966be868eSSoby Mathew /* Disable the UART before initialization */ 6066be868eSSoby Mathew ldr r3, [r0, #UARTCR] 6166be868eSSoby Mathew bic r3, r3, #PL011_UARTCR_UARTEN 6266be868eSSoby Mathew str r3, [r0, #UARTCR] 6366be868eSSoby Mathew /* Program the baudrate */ 6466be868eSSoby Mathew /* Divisor = (Uart clock * 4) / baudrate */ 6566be868eSSoby Mathew lsl r1, r1, #2 6666be868eSSoby Mathew udiv r2, r1, r2 6766be868eSSoby Mathew /* IBRD = Divisor >> 6 */ 6866be868eSSoby Mathew lsr r1, r2, #6 6966be868eSSoby Mathew /* Write the IBRD */ 7066be868eSSoby Mathew str r1, [r0, #UARTIBRD] 7166be868eSSoby Mathew /* FBRD = Divisor & 0x3F */ 7266be868eSSoby Mathew and r1, r2, #0x3f 7366be868eSSoby Mathew /* Write the FBRD */ 7466be868eSSoby Mathew str r1, [r0, #UARTFBRD] 7566be868eSSoby Mathew mov r1, #PL011_LINE_CONTROL 7666be868eSSoby Mathew str r1, [r0, #UARTLCR_H] 7766be868eSSoby Mathew /* Clear any pending errors */ 7866be868eSSoby Mathew mov r1, #0 7966be868eSSoby Mathew str r1, [r0, #UARTECR] 8066be868eSSoby Mathew /* Enable tx, rx, and uart overall */ 8166be868eSSoby Mathew ldr r1, =(PL011_UARTCR_RXE | PL011_UARTCR_TXE | PL011_UARTCR_UARTEN) 8266be868eSSoby Mathew str r1, [r0, #UARTCR] 8366be868eSSoby Mathew#endif 8466be868eSSoby Mathew mov r0, #1 8566be868eSSoby Mathew bx lr 8666be868eSSoby Mathewcore_init_fail: 8766be868eSSoby Mathew mov r0, #0 8866be868eSSoby Mathew bx lr 897e2bbef9SDaniel Boulbyendfunc console_pl011_core_init 907e2bbef9SDaniel Boulby 917e2bbef9SDaniel Boulby#if MULTI_CONSOLE_API 927e2bbef9SDaniel Boulby .globl console_pl011_register 937e2bbef9SDaniel Boulby 947e2bbef9SDaniel Boulby /* ------------------------------------------------------- 9565199dc8SAntonio Nino Diaz * int console_pl011_register(uintptr_t baseaddr, 9665199dc8SAntonio Nino Diaz * uint32_t clock, uint32_t baud, 9765199dc8SAntonio Nino Diaz * console_pl011_t *console); 987e2bbef9SDaniel Boulby * Function to initialize and register a new PL011 997e2bbef9SDaniel Boulby * console. Storage passed in for the console struct 1007e2bbef9SDaniel Boulby * *must* be persistent (i.e. not from the stack). 1017e2bbef9SDaniel Boulby * In: r0 - UART register base address 1027e2bbef9SDaniel Boulby * r1 - UART clock in Hz 1037e2bbef9SDaniel Boulby * r2 - Baud rate 1047e2bbef9SDaniel Boulby * r3 - pointer to empty console_pl011_t struct 1057e2bbef9SDaniel Boulby * Out: return 1 on success, 0 on error 1067e2bbef9SDaniel Boulby * Clobber list : r0, r1, r2 1077e2bbef9SDaniel Boulby * ------------------------------------------------------- 1087e2bbef9SDaniel Boulby */ 1097e2bbef9SDaniel Boulbyfunc console_pl011_register 1107e2bbef9SDaniel Boulby push {r4, lr} 1117e2bbef9SDaniel Boulby mov r4, r3 1127e2bbef9SDaniel Boulby cmp r4, #0 1137e2bbef9SDaniel Boulby beq register_fail 1147e2bbef9SDaniel Boulby str r0, [r4, #CONSOLE_T_PL011_BASE] 1157e2bbef9SDaniel Boulby 1167e2bbef9SDaniel Boulby bl console_pl011_core_init 1177e2bbef9SDaniel Boulby cmp r0, #0 1187e2bbef9SDaniel Boulby beq register_fail 1197e2bbef9SDaniel Boulby 1207e2bbef9SDaniel Boulby mov r0, r4 1217e2bbef9SDaniel Boulby pop {r4, lr} 122cc5859caSSoby Mathew finish_console_register pl011 putc=1, getc=1, flush=1 1237e2bbef9SDaniel Boulby 1247e2bbef9SDaniel Boulbyregister_fail: 1257e2bbef9SDaniel Boulby pop {r4, pc} 1267e2bbef9SDaniel Boulbyendfunc console_pl011_register 1277e2bbef9SDaniel Boulby#else 1287e2bbef9SDaniel Boulby .globl console_core_init 1297e2bbef9SDaniel Boulby .globl console_core_putc 1307e2bbef9SDaniel Boulby .globl console_core_getc 1317e2bbef9SDaniel Boulby .globl console_core_flush 1327e2bbef9SDaniel Boulby .equ console_core_init, console_pl011_core_init 1337e2bbef9SDaniel Boulby .equ console_core_putc, console_pl011_core_putc 1347e2bbef9SDaniel Boulby .equ console_core_getc, console_pl011_core_getc 1357e2bbef9SDaniel Boulby .equ console_core_flush, console_pl011_core_flush 1367e2bbef9SDaniel Boulby#endif 13766be868eSSoby Mathew 13866be868eSSoby Mathew /* -------------------------------------------------------- 13966be868eSSoby Mathew * int console_core_putc(int c, uintptr_t base_addr) 14066be868eSSoby Mathew * Function to output a character over the console. It 14166be868eSSoby Mathew * returns the character printed on success or -1 on error. 14266be868eSSoby Mathew * In : r0 - character to be printed 14366be868eSSoby Mathew * r1 - console base address 14466be868eSSoby Mathew * Out : return -1 on error else return character. 14566be868eSSoby Mathew * Clobber list : r2 14666be868eSSoby Mathew * -------------------------------------------------------- 14766be868eSSoby Mathew */ 1487e2bbef9SDaniel Boulbyfunc console_pl011_core_putc 14966be868eSSoby Mathew /* Check the input parameter */ 15066be868eSSoby Mathew cmp r1, #0 15166be868eSSoby Mathew beq putc_error 15266be868eSSoby Mathew /* Prepend '\r' to '\n' */ 15366be868eSSoby Mathew cmp r0, #0xA 15466be868eSSoby Mathew bne 2f 15566be868eSSoby Mathew1: 15666be868eSSoby Mathew /* Check if the transmit FIFO is full */ 15766be868eSSoby Mathew ldr r2, [r1, #UARTFR] 15869d59e0cSYatharth Kochar tst r2, #PL011_UARTFR_TXFF 15969d59e0cSYatharth Kochar bne 1b 16066be868eSSoby Mathew mov r2, #0xD 16166be868eSSoby Mathew str r2, [r1, #UARTDR] 16266be868eSSoby Mathew2: 16366be868eSSoby Mathew /* Check if the transmit FIFO is full */ 16466be868eSSoby Mathew ldr r2, [r1, #UARTFR] 16569d59e0cSYatharth Kochar tst r2, #PL011_UARTFR_TXFF 16669d59e0cSYatharth Kochar bne 2b 16766be868eSSoby Mathew str r0, [r1, #UARTDR] 16866be868eSSoby Mathew bx lr 16966be868eSSoby Mathewputc_error: 17066be868eSSoby Mathew mov r0, #-1 17166be868eSSoby Mathew bx lr 1727e2bbef9SDaniel Boulbyendfunc console_pl011_core_putc 1737e2bbef9SDaniel Boulby 1747e2bbef9SDaniel Boulby /* -------------------------------------------------------- 1757e2bbef9SDaniel Boulby * int console_pl011_putc(int c, console_pl011_t *console) 1767e2bbef9SDaniel Boulby * Function to output a character over the console. It 1777e2bbef9SDaniel Boulby * returns the character printed on success or -1 on error. 1787e2bbef9SDaniel Boulby * In: r0 - character to be printed 1797e2bbef9SDaniel Boulby * r1 - pointer to console_t structure 1807e2bbef9SDaniel Boulby * Out : return -1 on error else return character. 1817e2bbef9SDaniel Boulby * Clobber list: r2 1827e2bbef9SDaniel Boulby * ------------------------------------------------------- 1837e2bbef9SDaniel Boulby */ 1847e2bbef9SDaniel Boulbyfunc console_pl011_putc 1857e2bbef9SDaniel Boulby#if ENABLE_ASSERTIONS 1867e2bbef9SDaniel Boulby cmp r1, #0 1877e2bbef9SDaniel Boulby ASM_ASSERT(ne) 1887e2bbef9SDaniel Boulby#endif /* ENABLE_ASSERTIONS */ 1897e2bbef9SDaniel Boulby ldr r1, [r1, #CONSOLE_T_PL011_BASE] 1907e2bbef9SDaniel Boulby b console_pl011_core_putc 1917e2bbef9SDaniel Boulbyendfunc console_pl011_putc 19266be868eSSoby Mathew 19366be868eSSoby Mathew /* --------------------------------------------- 19466be868eSSoby Mathew * int console_core_getc(uintptr_t base_addr) 19566be868eSSoby Mathew * Function to get a character from the console. 19666be868eSSoby Mathew * It returns the character grabbed on success 19766be868eSSoby Mathew * or -1 on error. 19866be868eSSoby Mathew * In : r0 - console base address 19966be868eSSoby Mathew * Clobber list : r0, r1 20066be868eSSoby Mathew * --------------------------------------------- 20166be868eSSoby Mathew */ 2027e2bbef9SDaniel Boulbyfunc console_pl011_core_getc 20366be868eSSoby Mathew cmp r0, #0 20466be868eSSoby Mathew beq getc_error 20566be868eSSoby Mathew1: 20666be868eSSoby Mathew /* Check if the receive FIFO is empty */ 20766be868eSSoby Mathew ldr r1, [r0, #UARTFR] 20869d59e0cSYatharth Kochar tst r1, #PL011_UARTFR_RXFE 20969d59e0cSYatharth Kochar bne 1b 21066be868eSSoby Mathew ldr r1, [r0, #UARTDR] 21166be868eSSoby Mathew mov r0, r1 21266be868eSSoby Mathew bx lr 21366be868eSSoby Mathewgetc_error: 21466be868eSSoby Mathew mov r0, #-1 21566be868eSSoby Mathew bx lr 2167e2bbef9SDaniel Boulbyendfunc console_pl011_core_getc 2177e2bbef9SDaniel Boulby 2187e2bbef9SDaniel Boulby /* ------------------------------------------------ 2197e2bbef9SDaniel Boulby * int console_pl011_getc(console_pl011_t *console) 2207e2bbef9SDaniel Boulby * Function to get a character from the console. 2217e2bbef9SDaniel Boulby * It returns the character grabbed on success 2227e2bbef9SDaniel Boulby * or -1 if no character is available. 2237e2bbef9SDaniel Boulby * In : r0 - pointer to console_t structure 2247e2bbef9SDaniel Boulby * Out: r0 - character if available, else -1 2257e2bbef9SDaniel Boulby * Clobber list: r0, r1 2267e2bbef9SDaniel Boulby * ------------------------------------------------ 2277e2bbef9SDaniel Boulby */ 2287e2bbef9SDaniel Boulbyfunc console_pl011_getc 2297e2bbef9SDaniel Boulby#if ENABLE_ASSERTIONS 2307e2bbef9SDaniel Boulby cmp r0, #0 2317e2bbef9SDaniel Boulby ASM_ASSERT(ne) 2327e2bbef9SDaniel Boulby#endif /* ENABLE_ASSERTIONS */ 2337e2bbef9SDaniel Boulby ldr r0, [r0, #CONSOLE_T_PL011_BASE] 2347e2bbef9SDaniel Boulby b console_pl011_core_getc 2357e2bbef9SDaniel Boulbyendfunc console_pl011_getc 23673e05284SAntonio Nino Diaz 23773e05284SAntonio Nino Diaz /* --------------------------------------------- 23873e05284SAntonio Nino Diaz * int console_core_flush(uintptr_t base_addr) 23973e05284SAntonio Nino Diaz * Function to force a write of all buffered 24073e05284SAntonio Nino Diaz * data that hasn't been output. 24173e05284SAntonio Nino Diaz * In : r0 - console base address 24273e05284SAntonio Nino Diaz * Out : return -1 on error else return 0. 24373e05284SAntonio Nino Diaz * Clobber list : r0, r1 24473e05284SAntonio Nino Diaz * --------------------------------------------- 24573e05284SAntonio Nino Diaz */ 2467e2bbef9SDaniel Boulbyfunc console_pl011_core_flush 24773e05284SAntonio Nino Diaz cmp r0, #0 24873e05284SAntonio Nino Diaz beq flush_error 24973e05284SAntonio Nino Diaz 25073e05284SAntonio Nino Diaz1: 25173e05284SAntonio Nino Diaz /* Loop while the transmit FIFO is busy */ 25273e05284SAntonio Nino Diaz ldr r1, [r0, #UARTFR] 25373e05284SAntonio Nino Diaz tst r1, #PL011_UARTFR_BUSY 25473e05284SAntonio Nino Diaz bne 1b 25573e05284SAntonio Nino Diaz 25673e05284SAntonio Nino Diaz mov r0, #0 25773e05284SAntonio Nino Diaz bx lr 25873e05284SAntonio Nino Diazflush_error: 25973e05284SAntonio Nino Diaz mov r0, #-1 26073e05284SAntonio Nino Diaz bx lr 2617e2bbef9SDaniel Boulbyendfunc console_pl011_core_flush 2627e2bbef9SDaniel Boulby 2637e2bbef9SDaniel Boulby /* --------------------------------------------- 2647e2bbef9SDaniel Boulby * int console_pl011_flush(console_pl011_t *console) 2657e2bbef9SDaniel Boulby * Function to force a write of all buffered 2667e2bbef9SDaniel Boulby * data that hasn't been output. 2677e2bbef9SDaniel Boulby * In : r0 - pointer to console_t structure 2687e2bbef9SDaniel Boulby * Out : return -1 on error else return 0. 2697e2bbef9SDaniel Boulby * Clobber list: r0, r1 2707e2bbef9SDaniel Boulby * --------------------------------------------- 2717e2bbef9SDaniel Boulby */ 2727e2bbef9SDaniel Boulbyfunc console_pl011_flush 2737e2bbef9SDaniel Boulby#if ENABLE_ASSERTIONS 2747e2bbef9SDaniel Boulby cmp r0, #0 2757e2bbef9SDaniel Boulby ASM_ASSERT(ne) 2767e2bbef9SDaniel Boulby#endif /* ENABLE_ASSERTIONS */ 2777e2bbef9SDaniel Boulby ldr r0, [r0, #CONSOLE_T_PL011_BASE] 2787e2bbef9SDaniel Boulby b console_pl011_core_flush 2797e2bbef9SDaniel Boulbyendfunc console_pl011_flush 280