19c94d3b3SSoby Mathew/* 2*ad4c2ec6SAntonio Nino Diaz * Copyright (c) 2015-2017, ARM Limited and Contributors. All rights reserved. 39c94d3b3SSoby Mathew * 49c94d3b3SSoby Mathew * Redistribution and use in source and binary forms, with or without 59c94d3b3SSoby Mathew * modification, are permitted provided that the following conditions are met: 69c94d3b3SSoby Mathew * 79c94d3b3SSoby Mathew * Redistributions of source code must retain the above copyright notice, this 89c94d3b3SSoby Mathew * list of conditions and the following disclaimer. 99c94d3b3SSoby Mathew * 109c94d3b3SSoby Mathew * Redistributions in binary form must reproduce the above copyright notice, 119c94d3b3SSoby Mathew * this list of conditions and the following disclaimer in the documentation 129c94d3b3SSoby Mathew * and/or other materials provided with the distribution. 139c94d3b3SSoby Mathew * 149c94d3b3SSoby Mathew * Neither the name of ARM nor the names of its contributors may be used 159c94d3b3SSoby Mathew * to endorse or promote products derived from this software without specific 169c94d3b3SSoby Mathew * prior written permission. 179c94d3b3SSoby Mathew * 189c94d3b3SSoby Mathew * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 199c94d3b3SSoby Mathew * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 209c94d3b3SSoby Mathew * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 219c94d3b3SSoby Mathew * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 229c94d3b3SSoby Mathew * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 239c94d3b3SSoby Mathew * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 249c94d3b3SSoby Mathew * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 259c94d3b3SSoby Mathew * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 269c94d3b3SSoby Mathew * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 279c94d3b3SSoby Mathew * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 289c94d3b3SSoby Mathew * POSSIBILITY OF SUCH DAMAGE. 299c94d3b3SSoby Mathew */ 309c94d3b3SSoby Mathew 319c94d3b3SSoby Mathew#include <arch.h> 329c94d3b3SSoby Mathew#include <asm_macros.S> 339c94d3b3SSoby Mathew#include <uart_16550.h> 349c94d3b3SSoby Mathew 359c94d3b3SSoby Mathew .globl console_core_init 369c94d3b3SSoby Mathew .globl console_core_putc 379c94d3b3SSoby Mathew .globl console_core_getc 38*ad4c2ec6SAntonio Nino Diaz .globl console_core_flush 399c94d3b3SSoby Mathew 409c94d3b3SSoby Mathew /* ----------------------------------------------- 419c94d3b3SSoby Mathew * int console_core_init(unsigned long base_addr, 429c94d3b3SSoby Mathew * unsigned int uart_clk, unsigned int baud_rate) 439c94d3b3SSoby Mathew * Function to initialize the console without a 449c94d3b3SSoby Mathew * C Runtime to print debug information. This 459c94d3b3SSoby Mathew * function will be accessed by console_init and 469c94d3b3SSoby Mathew * crash reporting. 479c94d3b3SSoby Mathew * In: x0 - console base address 489c94d3b3SSoby Mathew * w1 - Uart clock in Hz 499c94d3b3SSoby Mathew * w2 - Baud rate 509c94d3b3SSoby Mathew * Out: return 1 on success 519c94d3b3SSoby Mathew * Clobber list : x1, x2, x3 529c94d3b3SSoby Mathew * ----------------------------------------------- 539c94d3b3SSoby Mathew */ 549c94d3b3SSoby Mathewfunc console_core_init 559c94d3b3SSoby Mathew /* Check the input base address */ 569c94d3b3SSoby Mathew cbz x0, init_fail 579c94d3b3SSoby Mathew /* Check baud rate and uart clock for sanity */ 589c94d3b3SSoby Mathew cbz w1, init_fail 599c94d3b3SSoby Mathew cbz w2, init_fail 609c94d3b3SSoby Mathew 619c94d3b3SSoby Mathew /* Program the baudrate */ 629c94d3b3SSoby Mathew /* Divisor = Uart clock / (16 * baudrate) */ 639c94d3b3SSoby Mathew lsl w2, w2, #4 649c94d3b3SSoby Mathew udiv w2, w1, w2 659c94d3b3SSoby Mathew and w1, w2, #0xff /* w1 = DLL */ 669c94d3b3SSoby Mathew lsr w2, w2, #8 679c94d3b3SSoby Mathew and w2, w2, #0xff /* w2 = DLLM */ 689c94d3b3SSoby Mathew ldr w3, [x0, #UARTLCR] 699c94d3b3SSoby Mathew orr w3, w3, #UARTLCR_DLAB 709c94d3b3SSoby Mathew str w3, [x0, #UARTLCR] /* enable DLL, DLLM programming */ 719c94d3b3SSoby Mathew str w1, [x0, #UARTDLL] /* program DLL */ 729c94d3b3SSoby Mathew str w2, [x0, #UARTDLLM] /* program DLLM */ 739c94d3b3SSoby Mathew mov w2, #~UARTLCR_DLAB 749c94d3b3SSoby Mathew and w3, w3, w2 759c94d3b3SSoby Mathew str w3, [x0, #UARTLCR] /* disable DLL, DLLM programming */ 769c94d3b3SSoby Mathew 779c94d3b3SSoby Mathew /* 8n1 */ 789c94d3b3SSoby Mathew mov w3, #3 799c94d3b3SSoby Mathew str w3, [x0, #UARTLCR] 809c94d3b3SSoby Mathew /* no interrupt */ 819c94d3b3SSoby Mathew mov w3, #0 829c94d3b3SSoby Mathew str w3, [x0, #UARTIER] 839c94d3b3SSoby Mathew /* enable fifo, DMA */ 849c94d3b3SSoby Mathew mov w3, #(UARTFCR_FIFOEN | UARTFCR_DMAEN) 859c94d3b3SSoby Mathew str w3, [x0, #UARTFCR] 869c94d3b3SSoby Mathew /* DTR + RTS */ 879c94d3b3SSoby Mathew mov w3, #3 889c94d3b3SSoby Mathew str w3, [x0, #UARTMCR] 899c94d3b3SSoby Mathew mov w0, #1 909c94d3b3SSoby Mathewinit_fail: 919c94d3b3SSoby Mathew ret 929c94d3b3SSoby Mathewendfunc console_core_init 939c94d3b3SSoby Mathew 949c94d3b3SSoby Mathew /* -------------------------------------------------------- 959c94d3b3SSoby Mathew * int console_core_putc(int c, unsigned int base_addr) 969c94d3b3SSoby Mathew * Function to output a character over the console. It 979c94d3b3SSoby Mathew * returns the character printed on success or -1 on error. 989c94d3b3SSoby Mathew * In : w0 - character to be printed 999c94d3b3SSoby Mathew * x1 - console base address 1009c94d3b3SSoby Mathew * Out : return -1 on error else return character. 1019c94d3b3SSoby Mathew * Clobber list : x2 1029c94d3b3SSoby Mathew * -------------------------------------------------------- 1039c94d3b3SSoby Mathew */ 1049c94d3b3SSoby Mathewfunc console_core_putc 1059c94d3b3SSoby Mathew /* Check the input parameter */ 1069c94d3b3SSoby Mathew cbz x1, putc_error 1079c94d3b3SSoby Mathew 1089c94d3b3SSoby Mathew /* Prepend '\r' to '\n' */ 1099c94d3b3SSoby Mathew cmp w0, #0xA 1109c94d3b3SSoby Mathew b.ne 2f 1119c94d3b3SSoby Mathew /* Check if the transmit FIFO is full */ 1129c94d3b3SSoby Mathew1: ldr w2, [x1, #UARTLSR] 1139c94d3b3SSoby Mathew and w2, w2, #(UARTLSR_TEMT | UARTLSR_THRE) 1149c94d3b3SSoby Mathew cmp w2, #(UARTLSR_TEMT | UARTLSR_THRE) 1159c94d3b3SSoby Mathew b.ne 1b 1169c94d3b3SSoby Mathew mov w2, #0xD /* '\r' */ 1179c94d3b3SSoby Mathew str w2, [x1, #UARTTX] 1189c94d3b3SSoby Mathew ldr w2, [x1, #UARTFCR] 1199c94d3b3SSoby Mathew orr w2, w2, #UARTFCR_TXCLR 1209c94d3b3SSoby Mathew str w2, [x1, #UARTFCR] 1219c94d3b3SSoby Mathew 1229c94d3b3SSoby Mathew /* Check if the transmit FIFO is full */ 1239c94d3b3SSoby Mathew2: ldr w2, [x1, #UARTLSR] 1249c94d3b3SSoby Mathew and w2, w2, #(UARTLSR_TEMT | UARTLSR_THRE) 1259c94d3b3SSoby Mathew cmp w2, #(UARTLSR_TEMT | UARTLSR_THRE) 1269c94d3b3SSoby Mathew b.ne 2b 1279c94d3b3SSoby Mathew str w0, [x1, #UARTTX] 1289c94d3b3SSoby Mathew ldr w2, [x1, #UARTFCR] 1299c94d3b3SSoby Mathew orr w2, w2, #UARTFCR_TXCLR 1309c94d3b3SSoby Mathew str w2, [x1, #UARTFCR] 1319c94d3b3SSoby Mathew ret 1329c94d3b3SSoby Mathewputc_error: 1339c94d3b3SSoby Mathew mov w0, #-1 1349c94d3b3SSoby Mathew ret 1359c94d3b3SSoby Mathewendfunc console_core_putc 1369c94d3b3SSoby Mathew 1379c94d3b3SSoby Mathew /* --------------------------------------------- 1389c94d3b3SSoby Mathew * int console_core_getc(void) 1399c94d3b3SSoby Mathew * Function to get a character from the console. 1409c94d3b3SSoby Mathew * It returns the character grabbed on success 1419c94d3b3SSoby Mathew * or -1 on error. 1429c94d3b3SSoby Mathew * In : w0 - console base address 1439c94d3b3SSoby Mathew * Out : return -1 on error else return character. 1449c94d3b3SSoby Mathew * Clobber list : x0, x1 1459c94d3b3SSoby Mathew * --------------------------------------------- 1469c94d3b3SSoby Mathew */ 1479c94d3b3SSoby Mathewfunc console_core_getc 1489c94d3b3SSoby Mathew /* Check if the receive FIFO is empty */ 1499c94d3b3SSoby Mathew1: ldr w1, [x0, #UARTLSR] 150861ac52aSNishanth Menon tbz w1, #UARTLSR_RDR_BIT, 1b 1519c94d3b3SSoby Mathew ldr w0, [x0, #UARTRX] 1529c94d3b3SSoby Mathew ret 1539c94d3b3SSoby Mathewgetc_error: 1549c94d3b3SSoby Mathew mov w0, #-1 1559c94d3b3SSoby Mathew ret 1569c94d3b3SSoby Mathewendfunc console_core_getc 157*ad4c2ec6SAntonio Nino Diaz 158*ad4c2ec6SAntonio Nino Diaz /* --------------------------------------------- 159*ad4c2ec6SAntonio Nino Diaz * int console_core_flush(uintptr_t base_addr) 160*ad4c2ec6SAntonio Nino Diaz * Function to force a write of all buffered 161*ad4c2ec6SAntonio Nino Diaz * data that hasn't been output. 162*ad4c2ec6SAntonio Nino Diaz * In : x0 - console base address 163*ad4c2ec6SAntonio Nino Diaz * Out : return -1 on error else return 0. 164*ad4c2ec6SAntonio Nino Diaz * Clobber list : x0, x1 165*ad4c2ec6SAntonio Nino Diaz * --------------------------------------------- 166*ad4c2ec6SAntonio Nino Diaz */ 167*ad4c2ec6SAntonio Nino Diazfunc console_core_flush 168*ad4c2ec6SAntonio Nino Diaz /* Placeholder */ 169*ad4c2ec6SAntonio Nino Diaz mov w0, #0 170*ad4c2ec6SAntonio Nino Diaz ret 171*ad4c2ec6SAntonio Nino Diazendfunc console_core_flush 172