1*9c94d3b3SSoby Mathew/* 2*9c94d3b3SSoby Mathew * Copyright (c) 2016, ARM Limited and Contributors. All rights reserved. 3*9c94d3b3SSoby Mathew * 4*9c94d3b3SSoby Mathew * Redistribution and use in source and binary forms, with or without 5*9c94d3b3SSoby Mathew * modification, are permitted provided that the following conditions are met: 6*9c94d3b3SSoby Mathew * 7*9c94d3b3SSoby Mathew * Redistributions of source code must retain the above copyright notice, this 8*9c94d3b3SSoby Mathew * list of conditions and the following disclaimer. 9*9c94d3b3SSoby Mathew * 10*9c94d3b3SSoby Mathew * Redistributions in binary form must reproduce the above copyright notice, 11*9c94d3b3SSoby Mathew * this list of conditions and the following disclaimer in the documentation 12*9c94d3b3SSoby Mathew * and/or other materials provided with the distribution. 13*9c94d3b3SSoby Mathew * 14*9c94d3b3SSoby Mathew * Neither the name of ARM nor the names of its contributors may be used 15*9c94d3b3SSoby Mathew * to endorse or promote products derived from this software without specific 16*9c94d3b3SSoby Mathew * prior written permission. 17*9c94d3b3SSoby Mathew * 18*9c94d3b3SSoby Mathew * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19*9c94d3b3SSoby Mathew * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20*9c94d3b3SSoby Mathew * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21*9c94d3b3SSoby Mathew * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22*9c94d3b3SSoby Mathew * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23*9c94d3b3SSoby Mathew * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24*9c94d3b3SSoby Mathew * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25*9c94d3b3SSoby Mathew * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26*9c94d3b3SSoby Mathew * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27*9c94d3b3SSoby Mathew * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28*9c94d3b3SSoby Mathew * POSSIBILITY OF SUCH DAMAGE. 29*9c94d3b3SSoby Mathew */ 30*9c94d3b3SSoby Mathew#include <arch.h> 31*9c94d3b3SSoby Mathew#include <asm_macros.S> 32*9c94d3b3SSoby Mathew#include <cadence/cdns_uart.h> 33*9c94d3b3SSoby Mathew 34*9c94d3b3SSoby Mathew .globl console_core_init 35*9c94d3b3SSoby Mathew .globl console_core_putc 36*9c94d3b3SSoby Mathew .globl console_core_getc 37*9c94d3b3SSoby Mathew 38*9c94d3b3SSoby Mathew /* ----------------------------------------------- 39*9c94d3b3SSoby Mathew * int console_core_init(unsigned long base_addr, 40*9c94d3b3SSoby Mathew * unsigned int uart_clk, unsigned int baud_rate) 41*9c94d3b3SSoby Mathew * Function to initialize the console without a 42*9c94d3b3SSoby Mathew * C Runtime to print debug information. This 43*9c94d3b3SSoby Mathew * function will be accessed by console_init and 44*9c94d3b3SSoby Mathew * crash reporting. 45*9c94d3b3SSoby Mathew * We assume that the bootloader already set up 46*9c94d3b3SSoby Mathew * the HW (baud, ...) and only enable the trans- 47*9c94d3b3SSoby Mathew * mitter and receiver here. 48*9c94d3b3SSoby Mathew * In: x0 - console base address 49*9c94d3b3SSoby Mathew * w1 - Uart clock in Hz 50*9c94d3b3SSoby Mathew * w2 - Baud rate 51*9c94d3b3SSoby Mathew * Out: return 1 on success else 0 on error 52*9c94d3b3SSoby Mathew * Clobber list : x1, x2, x3 53*9c94d3b3SSoby Mathew * ----------------------------------------------- 54*9c94d3b3SSoby Mathew */ 55*9c94d3b3SSoby Mathewfunc console_core_init 56*9c94d3b3SSoby Mathew /* Check the input base address */ 57*9c94d3b3SSoby Mathew cbz x0, core_init_fail 58*9c94d3b3SSoby Mathew /* Check baud rate and uart clock for sanity */ 59*9c94d3b3SSoby Mathew cbz w1, core_init_fail 60*9c94d3b3SSoby Mathew cbz w2, core_init_fail 61*9c94d3b3SSoby Mathew 62*9c94d3b3SSoby Mathew /* RX/TX enabled & reset */ 63*9c94d3b3SSoby Mathew mov w3, #(R_UART_CR_TX_EN | R_UART_CR_RX_EN | R_UART_CR_TXRST | R_UART_CR_RXRST) 64*9c94d3b3SSoby Mathew str w3, [x0, #R_UART_CR] 65*9c94d3b3SSoby Mathew 66*9c94d3b3SSoby Mathew mov w0, #1 67*9c94d3b3SSoby Mathew ret 68*9c94d3b3SSoby Mathewcore_init_fail: 69*9c94d3b3SSoby Mathew mov w0, wzr 70*9c94d3b3SSoby Mathew ret 71*9c94d3b3SSoby Mathewendfunc console_core_init 72*9c94d3b3SSoby Mathew 73*9c94d3b3SSoby Mathew /* -------------------------------------------------------- 74*9c94d3b3SSoby Mathew * int console_core_putc(int c, unsigned long base_addr) 75*9c94d3b3SSoby Mathew * Function to output a character over the console. It 76*9c94d3b3SSoby Mathew * returns the character printed on success or -1 on error. 77*9c94d3b3SSoby Mathew * In : w0 - character to be printed 78*9c94d3b3SSoby Mathew * x1 - console base address 79*9c94d3b3SSoby Mathew * Out : return -1 on error else return character. 80*9c94d3b3SSoby Mathew * Clobber list : x2 81*9c94d3b3SSoby Mathew * -------------------------------------------------------- 82*9c94d3b3SSoby Mathew */ 83*9c94d3b3SSoby Mathewfunc console_core_putc 84*9c94d3b3SSoby Mathew /* Check the input parameter */ 85*9c94d3b3SSoby Mathew cbz x1, putc_error 86*9c94d3b3SSoby Mathew /* Prepend '\r' to '\n' */ 87*9c94d3b3SSoby Mathew cmp w0, #0xA 88*9c94d3b3SSoby Mathew b.ne 2f 89*9c94d3b3SSoby Mathew1: 90*9c94d3b3SSoby Mathew /* Check if the transmit FIFO is full */ 91*9c94d3b3SSoby Mathew ldr w2, [x1, #R_UART_SR] 92*9c94d3b3SSoby Mathew tbnz w2, #UART_SR_INTR_TFUL_BIT, 1b 93*9c94d3b3SSoby Mathew mov w2, #0xD 94*9c94d3b3SSoby Mathew str w2, [x1, #R_UART_TX] 95*9c94d3b3SSoby Mathew2: 96*9c94d3b3SSoby Mathew /* Check if the transmit FIFO is full */ 97*9c94d3b3SSoby Mathew ldr w2, [x1, #R_UART_SR] 98*9c94d3b3SSoby Mathew tbnz w2, #UART_SR_INTR_TFUL_BIT, 2b 99*9c94d3b3SSoby Mathew str w0, [x1, #R_UART_TX] 100*9c94d3b3SSoby Mathew ret 101*9c94d3b3SSoby Mathewputc_error: 102*9c94d3b3SSoby Mathew mov w0, #-1 103*9c94d3b3SSoby Mathew ret 104*9c94d3b3SSoby Mathewendfunc console_core_putc 105*9c94d3b3SSoby Mathew 106*9c94d3b3SSoby Mathew /* --------------------------------------------- 107*9c94d3b3SSoby Mathew * int console_core_getc(unsigned long base_addr) 108*9c94d3b3SSoby Mathew * Function to get a character from the console. 109*9c94d3b3SSoby Mathew * It returns the character grabbed on success 110*9c94d3b3SSoby Mathew * or -1 on error. 111*9c94d3b3SSoby Mathew * In : x0 - console base address 112*9c94d3b3SSoby Mathew * Clobber list : x0, x1 113*9c94d3b3SSoby Mathew * --------------------------------------------- 114*9c94d3b3SSoby Mathew */ 115*9c94d3b3SSoby Mathewfunc console_core_getc 116*9c94d3b3SSoby Mathew cbz x0, getc_error 117*9c94d3b3SSoby Mathew1: 118*9c94d3b3SSoby Mathew /* Check if the receive FIFO is empty */ 119*9c94d3b3SSoby Mathew ldr w1, [x0, #R_UART_SR] 120*9c94d3b3SSoby Mathew tbnz w1, #UART_SR_INTR_REMPTY_BIT, 1b 121*9c94d3b3SSoby Mathew ldr w1, [x0, #R_UART_RX] 122*9c94d3b3SSoby Mathew mov w0, w1 123*9c94d3b3SSoby Mathew ret 124*9c94d3b3SSoby Mathewgetc_error: 125*9c94d3b3SSoby Mathew mov w0, #-1 126*9c94d3b3SSoby Mathew ret 127*9c94d3b3SSoby Mathewendfunc console_core_getc 128