19c94d3b3SSoby Mathew/* 2ad4c2ec6SAntonio Nino Diaz * Copyright (c) 2016-2017, ARM Limited and Contributors. All rights reserved. 39c94d3b3SSoby Mathew * 482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause 59c94d3b3SSoby Mathew */ 69c94d3b3SSoby Mathew#include <arch.h> 79c94d3b3SSoby Mathew#include <asm_macros.S> 838ba8e93SJulius Werner#include <assert_macros.S> 9cc5859caSSoby Mathew#define USE_FINISH_CONSOLE_REG_2 10cc5859caSSoby Mathew#include <console_macros.S> 11*09d40e0eSAntonio Nino Diaz#include <drivers/cadence/cdns_uart.h> 129c94d3b3SSoby Mathew 1338ba8e93SJulius Werner /* 1438ba8e93SJulius Werner * "core" functions are low-level implementations that don't require 1538ba8e93SJulius Werner * writable memory and are thus safe to call in BL1 crash context. 1638ba8e93SJulius Werner */ 1738ba8e93SJulius Werner .globl console_cdns_core_init 1838ba8e93SJulius Werner .globl console_cdns_core_putc 1938ba8e93SJulius Werner .globl console_cdns_core_getc 2061d2b40dSAntonio Nino Diaz .globl console_cdns_core_flush 2138ba8e93SJulius Werner 2238ba8e93SJulius Werner .globl console_cdns_putc 2338ba8e93SJulius Werner .globl console_cdns_getc 2461d2b40dSAntonio Nino Diaz .globl console_cdns_flush 259c94d3b3SSoby Mathew 269c94d3b3SSoby Mathew /* ----------------------------------------------- 2738ba8e93SJulius Werner * int console_cdns_core_init(uintptr_t base_addr) 289c94d3b3SSoby Mathew * Function to initialize the console without a 299c94d3b3SSoby Mathew * C Runtime to print debug information. This 309c94d3b3SSoby Mathew * function will be accessed by console_init and 319c94d3b3SSoby Mathew * crash reporting. 329c94d3b3SSoby Mathew * We assume that the bootloader already set up 339c94d3b3SSoby Mathew * the HW (baud, ...) and only enable the trans- 349c94d3b3SSoby Mathew * mitter and receiver here. 359c94d3b3SSoby Mathew * In: x0 - console base address 369c94d3b3SSoby Mathew * Out: return 1 on success else 0 on error 379c94d3b3SSoby Mathew * Clobber list : x1, x2, x3 389c94d3b3SSoby Mathew * ----------------------------------------------- 399c94d3b3SSoby Mathew */ 4038ba8e93SJulius Wernerfunc console_cdns_core_init 419c94d3b3SSoby Mathew /* Check the input base address */ 429c94d3b3SSoby Mathew cbz x0, core_init_fail 439c94d3b3SSoby Mathew 449c94d3b3SSoby Mathew /* RX/TX enabled & reset */ 459c94d3b3SSoby Mathew mov w3, #(R_UART_CR_TX_EN | R_UART_CR_RX_EN | R_UART_CR_TXRST | R_UART_CR_RXRST) 469c94d3b3SSoby Mathew str w3, [x0, #R_UART_CR] 479c94d3b3SSoby Mathew 489c94d3b3SSoby Mathew mov w0, #1 499c94d3b3SSoby Mathew ret 509c94d3b3SSoby Mathewcore_init_fail: 519c94d3b3SSoby Mathew mov w0, wzr 529c94d3b3SSoby Mathew ret 5338ba8e93SJulius Wernerendfunc console_cdns_core_init 5438ba8e93SJulius Werner 5538ba8e93SJulius Werner#if MULTI_CONSOLE_API 5638ba8e93SJulius Werner .globl console_cdns_register 5738ba8e93SJulius Werner 5838ba8e93SJulius Werner /* ----------------------------------------------- 5923b6fa4eSAlexei Colin * int console_cdns_register(uintptr_t baseaddr, 6065199dc8SAntonio Nino Diaz * uint32_t clock, uint32_t baud, 6165199dc8SAntonio Nino Diaz * console_cdns_t *console); 6238ba8e93SJulius Werner * Function to initialize and register a new CDNS 6338ba8e93SJulius Werner * console. Storage passed in for the console struct 6438ba8e93SJulius Werner * *must* be persistent (i.e. not from the stack). 6538ba8e93SJulius Werner * In: x0 - UART register base address 6623b6fa4eSAlexei Colin * w1 - UART clock in Hz 6723b6fa4eSAlexei Colin * w2 - Baud rate 6823b6fa4eSAlexei Colin * x3 - pointer to empty console_16550_t struct 6938ba8e93SJulius Werner * Out: return 1 on success, 0 on error 7038ba8e93SJulius Werner * Clobber list : x0, x1, x2, x6, x7, x14 7138ba8e93SJulius Werner * ----------------------------------------------- 7238ba8e93SJulius Werner */ 7338ba8e93SJulius Wernerfunc console_cdns_register 7438ba8e93SJulius Werner mov x7, x30 7523b6fa4eSAlexei Colin mov x6, x3 7638ba8e93SJulius Werner cbz x6, register_fail 7738ba8e93SJulius Werner str x0, [x6, #CONSOLE_T_CDNS_BASE] 7838ba8e93SJulius Werner 7938ba8e93SJulius Werner bl console_cdns_core_init 8038ba8e93SJulius Werner cbz x0, register_fail 8138ba8e93SJulius Werner 8238ba8e93SJulius Werner mov x0, x6 8323b6fa4eSAlexei Colin mov x30, x7 84cc5859caSSoby Mathew finish_console_register cdns putc=1, getc=1, flush=1 8538ba8e93SJulius Werner 8638ba8e93SJulius Wernerregister_fail: 8738ba8e93SJulius Werner ret x7 8838ba8e93SJulius Wernerendfunc console_cdns_register 8938ba8e93SJulius Werner#else 9038ba8e93SJulius Werner .globl console_core_init 9138ba8e93SJulius Werner .globl console_core_putc 9238ba8e93SJulius Werner .globl console_core_getc 9338ba8e93SJulius Werner .globl console_core_flush 9438ba8e93SJulius Werner .equ console_core_init,console_cdns_core_init 9538ba8e93SJulius Werner .equ console_core_putc,console_cdns_core_putc 9638ba8e93SJulius Werner .equ console_core_getc,console_cdns_core_getc 9761d2b40dSAntonio Nino Diaz .equ console_core_flush,console_cdns_core_flush 9838ba8e93SJulius Werner#endif 999c94d3b3SSoby Mathew 1009c94d3b3SSoby Mathew /* -------------------------------------------------------- 10138ba8e93SJulius Werner * int console_cdns_core_putc(int c, uintptr_t base_addr) 1029c94d3b3SSoby Mathew * Function to output a character over the console. It 1039c94d3b3SSoby Mathew * returns the character printed on success or -1 on error. 1049c94d3b3SSoby Mathew * In : w0 - character to be printed 1059c94d3b3SSoby Mathew * x1 - console base address 1069c94d3b3SSoby Mathew * Out : return -1 on error else return character. 1079c94d3b3SSoby Mathew * Clobber list : x2 1089c94d3b3SSoby Mathew * -------------------------------------------------------- 1099c94d3b3SSoby Mathew */ 11038ba8e93SJulius Wernerfunc console_cdns_core_putc 11138ba8e93SJulius Werner#if ENABLE_ASSERTIONS 11238ba8e93SJulius Werner cmp x1, #0 11338ba8e93SJulius Werner ASM_ASSERT(ne) 11438ba8e93SJulius Werner#endif /* ENABLE_ASSERTIONS */ 11538ba8e93SJulius Werner 1169c94d3b3SSoby Mathew /* Prepend '\r' to '\n' */ 1179c94d3b3SSoby Mathew cmp w0, #0xA 1189c94d3b3SSoby Mathew b.ne 2f 1199c94d3b3SSoby Mathew1: 1209c94d3b3SSoby Mathew /* Check if the transmit FIFO is full */ 1219c94d3b3SSoby Mathew ldr w2, [x1, #R_UART_SR] 1229c94d3b3SSoby Mathew tbnz w2, #UART_SR_INTR_TFUL_BIT, 1b 1239c94d3b3SSoby Mathew mov w2, #0xD 1249c94d3b3SSoby Mathew str w2, [x1, #R_UART_TX] 1259c94d3b3SSoby Mathew2: 1269c94d3b3SSoby Mathew /* Check if the transmit FIFO is full */ 1279c94d3b3SSoby Mathew ldr w2, [x1, #R_UART_SR] 1289c94d3b3SSoby Mathew tbnz w2, #UART_SR_INTR_TFUL_BIT, 2b 1299c94d3b3SSoby Mathew str w0, [x1, #R_UART_TX] 1309c94d3b3SSoby Mathew ret 13138ba8e93SJulius Wernerendfunc console_cdns_core_putc 13238ba8e93SJulius Werner 13338ba8e93SJulius Werner /* -------------------------------------------------------- 13438ba8e93SJulius Werner * int console_cdns_putc(int c, console_cdns_t *cdns) 13538ba8e93SJulius Werner * Function to output a character over the console. It 13638ba8e93SJulius Werner * returns the character printed on success or -1 on error. 13738ba8e93SJulius Werner * In : w0 - character to be printed 13838ba8e93SJulius Werner * x1 - pointer to console_t structure 13938ba8e93SJulius Werner * Out : return -1 on error else return character. 14038ba8e93SJulius Werner * Clobber list : x2 14138ba8e93SJulius Werner * -------------------------------------------------------- 14238ba8e93SJulius Werner */ 14338ba8e93SJulius Wernerfunc console_cdns_putc 14438ba8e93SJulius Werner#if ENABLE_ASSERTIONS 14538ba8e93SJulius Werner cmp x1, #0 14638ba8e93SJulius Werner ASM_ASSERT(ne) 14738ba8e93SJulius Werner#endif /* ENABLE_ASSERTIONS */ 14838ba8e93SJulius Werner ldr x1, [x1, #CONSOLE_T_CDNS_BASE] 14938ba8e93SJulius Werner b console_cdns_core_putc 15038ba8e93SJulius Wernerendfunc console_cdns_putc 1519c94d3b3SSoby Mathew 1529c94d3b3SSoby Mathew /* --------------------------------------------- 15338ba8e93SJulius Werner * int console_cdns_core_getc(uintptr_t base_addr) 1549c94d3b3SSoby Mathew * Function to get a character from the console. 1559c94d3b3SSoby Mathew * It returns the character grabbed on success 15638ba8e93SJulius Werner * or -1 if no character is available. 1579c94d3b3SSoby Mathew * In : x0 - console base address 15838ba8e93SJulius Werner * Out: w0 - character if available, else -1 1599c94d3b3SSoby Mathew * Clobber list : x0, x1 1609c94d3b3SSoby Mathew * --------------------------------------------- 1619c94d3b3SSoby Mathew */ 16238ba8e93SJulius Wernerfunc console_cdns_core_getc 16338ba8e93SJulius Werner#if ENABLE_ASSERTIONS 16438ba8e93SJulius Werner cmp x0, #0 16538ba8e93SJulius Werner ASM_ASSERT(ne) 16638ba8e93SJulius Werner#endif /* ENABLE_ASSERTIONS */ 16738ba8e93SJulius Werner 1689c94d3b3SSoby Mathew /* Check if the receive FIFO is empty */ 1699c94d3b3SSoby Mathew ldr w1, [x0, #R_UART_SR] 17038ba8e93SJulius Werner tbnz w1, #UART_SR_INTR_REMPTY_BIT, no_char 1719c94d3b3SSoby Mathew ldr w1, [x0, #R_UART_RX] 1729c94d3b3SSoby Mathew mov w0, w1 1739c94d3b3SSoby Mathew ret 17438ba8e93SJulius Wernerno_char: 17538ba8e93SJulius Werner mov w0, #ERROR_NO_PENDING_CHAR 1769c94d3b3SSoby Mathew ret 17738ba8e93SJulius Wernerendfunc console_cdns_core_getc 17838ba8e93SJulius Werner 17938ba8e93SJulius Werner /* --------------------------------------------- 18038ba8e93SJulius Werner * int console_cdns_getc(console_cdns_t *console) 18138ba8e93SJulius Werner * Function to get a character from the console. 18238ba8e93SJulius Werner * It returns the character grabbed on success 18338ba8e93SJulius Werner * or -1 if no character is available. 18438ba8e93SJulius Werner * In : x0 - pointer to console_t structure 18538ba8e93SJulius Werner * Out: w0 - character if available, else -1 18638ba8e93SJulius Werner * Clobber list : x0, x1 18738ba8e93SJulius Werner * --------------------------------------------- 18838ba8e93SJulius Werner */ 18938ba8e93SJulius Wernerfunc console_cdns_getc 19038ba8e93SJulius Werner#if ENABLE_ASSERTIONS 19138ba8e93SJulius Werner cmp x0, #0 19238ba8e93SJulius Werner ASM_ASSERT(ne) 19338ba8e93SJulius Werner#endif /* ENABLE_ASSERTIONS */ 19438ba8e93SJulius Werner ldr x0, [x0, #CONSOLE_T_CDNS_BASE] 19538ba8e93SJulius Werner b console_cdns_core_getc 19638ba8e93SJulius Wernerendfunc console_cdns_getc 197ad4c2ec6SAntonio Nino Diaz 198ad4c2ec6SAntonio Nino Diaz /* --------------------------------------------- 19961d2b40dSAntonio Nino Diaz * int console_cdns_core_flush(uintptr_t base_addr) 200ad4c2ec6SAntonio Nino Diaz * Function to force a write of all buffered 201ad4c2ec6SAntonio Nino Diaz * data that hasn't been output. 202ad4c2ec6SAntonio Nino Diaz * In : x0 - console base address 203ad4c2ec6SAntonio Nino Diaz * Out : return -1 on error else return 0. 204ad4c2ec6SAntonio Nino Diaz * Clobber list : x0, x1 205ad4c2ec6SAntonio Nino Diaz * --------------------------------------------- 206ad4c2ec6SAntonio Nino Diaz */ 20761d2b40dSAntonio Nino Diazfunc console_cdns_core_flush 20861d2b40dSAntonio Nino Diaz#if ENABLE_ASSERTIONS 20961d2b40dSAntonio Nino Diaz cmp x0, #0 21061d2b40dSAntonio Nino Diaz ASM_ASSERT(ne) 21161d2b40dSAntonio Nino Diaz#endif /* ENABLE_ASSERTIONS */ 212ad4c2ec6SAntonio Nino Diaz /* Placeholder */ 213ad4c2ec6SAntonio Nino Diaz mov w0, #0 214ad4c2ec6SAntonio Nino Diaz ret 21561d2b40dSAntonio Nino Diazendfunc console_cdns_core_flush 21661d2b40dSAntonio Nino Diaz 21761d2b40dSAntonio Nino Diaz /* --------------------------------------------- 21861d2b40dSAntonio Nino Diaz * int console_cdns_flush(console_pl011_t *console) 21961d2b40dSAntonio Nino Diaz * Function to force a write of all buffered 22061d2b40dSAntonio Nino Diaz * data that hasn't been output. 22161d2b40dSAntonio Nino Diaz * In : x0 - pointer to console_t structure 22261d2b40dSAntonio Nino Diaz * Out : return -1 on error else return 0. 22361d2b40dSAntonio Nino Diaz * Clobber list : x0, x1 22461d2b40dSAntonio Nino Diaz * --------------------------------------------- 22561d2b40dSAntonio Nino Diaz */ 22661d2b40dSAntonio Nino Diazfunc console_cdns_flush 22761d2b40dSAntonio Nino Diaz#if ENABLE_ASSERTIONS 22861d2b40dSAntonio Nino Diaz cmp x0, #0 22961d2b40dSAntonio Nino Diaz ASM_ASSERT(ne) 23061d2b40dSAntonio Nino Diaz#endif /* ENABLE_ASSERTIONS */ 23161d2b40dSAntonio Nino Diaz ldr x0, [x0, #CONSOLE_T_CDNS_BASE] 23261d2b40dSAntonio Nino Diaz b console_cdns_core_flush 23361d2b40dSAntonio Nino Diazendfunc console_cdns_flush 234