19c94d3b3SSoby Mathew/* 24f8053ddSAntonio Nino Diaz * Copyright (c) 2015-2018, ARM Limited and Contributors. All rights reserved. 39c94d3b3SSoby Mathew * 482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause 59c94d3b3SSoby Mathew */ 69c94d3b3SSoby Mathew 79c94d3b3SSoby Mathew#include <arch.h> 89c94d3b3SSoby Mathew#include <asm_macros.S> 936c42ca1SJulius Werner#include <assert_macros.S> 1036c42ca1SJulius Werner#include <console_macros.S> 119c94d3b3SSoby Mathew#include <uart_16550.h> 129c94d3b3SSoby Mathew 1336c42ca1SJulius Werner /* 1436c42ca1SJulius Werner * "core" functions are low-level implementations that don't require 1536c42ca1SJulius Werner * writable memory and are thus safe to call in BL1 crash context. 1636c42ca1SJulius Werner */ 1736c42ca1SJulius Werner .globl console_16550_core_init 1836c42ca1SJulius Werner .globl console_16550_core_putc 1936c42ca1SJulius Werner .globl console_16550_core_getc 204f8053ddSAntonio Nino Diaz .globl console_16550_core_flush 2136c42ca1SJulius Werner 2236c42ca1SJulius Werner .globl console_16550_putc 2336c42ca1SJulius Werner .globl console_16550_getc 244f8053ddSAntonio Nino Diaz .globl console_16550_flush 259c94d3b3SSoby Mathew 269c94d3b3SSoby Mathew /* ----------------------------------------------- 2736c42ca1SJulius Werner * int console_16550_core_init(uintptr_t base_addr, 289c94d3b3SSoby Mathew * unsigned int uart_clk, unsigned int baud_rate) 299c94d3b3SSoby Mathew * Function to initialize the console without a 309c94d3b3SSoby Mathew * C Runtime to print debug information. This 319c94d3b3SSoby Mathew * function will be accessed by console_init and 329c94d3b3SSoby Mathew * crash reporting. 339c94d3b3SSoby Mathew * In: x0 - console base address 349c94d3b3SSoby Mathew * w1 - Uart clock in Hz 359c94d3b3SSoby Mathew * w2 - Baud rate 3636c42ca1SJulius Werner * Out: return 1 on success, 0 on error 379c94d3b3SSoby Mathew * Clobber list : x1, x2, x3 389c94d3b3SSoby Mathew * ----------------------------------------------- 399c94d3b3SSoby Mathew */ 4036c42ca1SJulius Wernerfunc console_16550_core_init 419c94d3b3SSoby Mathew /* Check the input base address */ 429c94d3b3SSoby Mathew cbz x0, init_fail 439c94d3b3SSoby Mathew /* Check baud rate and uart clock for sanity */ 449c94d3b3SSoby Mathew cbz w1, init_fail 459c94d3b3SSoby Mathew cbz w2, init_fail 469c94d3b3SSoby Mathew 479c94d3b3SSoby Mathew /* Program the baudrate */ 489c94d3b3SSoby Mathew /* Divisor = Uart clock / (16 * baudrate) */ 499c94d3b3SSoby Mathew lsl w2, w2, #4 509c94d3b3SSoby Mathew udiv w2, w1, w2 519c94d3b3SSoby Mathew and w1, w2, #0xff /* w1 = DLL */ 529c94d3b3SSoby Mathew lsr w2, w2, #8 539c94d3b3SSoby Mathew and w2, w2, #0xff /* w2 = DLLM */ 549c94d3b3SSoby Mathew ldr w3, [x0, #UARTLCR] 559c94d3b3SSoby Mathew orr w3, w3, #UARTLCR_DLAB 569c94d3b3SSoby Mathew str w3, [x0, #UARTLCR] /* enable DLL, DLLM programming */ 579c94d3b3SSoby Mathew str w1, [x0, #UARTDLL] /* program DLL */ 589c94d3b3SSoby Mathew str w2, [x0, #UARTDLLM] /* program DLLM */ 599c94d3b3SSoby Mathew mov w2, #~UARTLCR_DLAB 609c94d3b3SSoby Mathew and w3, w3, w2 619c94d3b3SSoby Mathew str w3, [x0, #UARTLCR] /* disable DLL, DLLM programming */ 629c94d3b3SSoby Mathew 639c94d3b3SSoby Mathew /* 8n1 */ 649c94d3b3SSoby Mathew mov w3, #3 659c94d3b3SSoby Mathew str w3, [x0, #UARTLCR] 669c94d3b3SSoby Mathew /* no interrupt */ 679c94d3b3SSoby Mathew mov w3, #0 689c94d3b3SSoby Mathew str w3, [x0, #UARTIER] 69*529b541eSBenjamin Fair#ifdef TI_16550_MDR_QUIRK 70*529b541eSBenjamin Fair /* UART must be enabled on some platforms via the MDR register */ 71*529b541eSBenjamin Fair str w3, [x0, #UARTMDR1] 72*529b541eSBenjamin Fair#endif /* TI_16550_MDR_QUIRK */ 739c94d3b3SSoby Mathew /* enable fifo, DMA */ 749c94d3b3SSoby Mathew mov w3, #(UARTFCR_FIFOEN | UARTFCR_DMAEN) 759c94d3b3SSoby Mathew str w3, [x0, #UARTFCR] 769c94d3b3SSoby Mathew /* DTR + RTS */ 779c94d3b3SSoby Mathew mov w3, #3 789c94d3b3SSoby Mathew str w3, [x0, #UARTMCR] 799c94d3b3SSoby Mathew mov w0, #1 809c94d3b3SSoby Mathew ret 8136c42ca1SJulius Wernerinit_fail: 8236c42ca1SJulius Werner mov w0, #0 8336c42ca1SJulius Werner ret 8436c42ca1SJulius Wernerendfunc console_16550_core_init 8536c42ca1SJulius Werner 8636c42ca1SJulius Werner#if MULTI_CONSOLE_API 8736c42ca1SJulius Werner .globl console_16550_register 8836c42ca1SJulius Werner 8936c42ca1SJulius Werner /* ----------------------------------------------- 9036c42ca1SJulius Werner * int console_16550_register(console_16550_t *console, 9136c42ca1SJulius Werner uintptr_t base, uint32_t clk, uint32_t baud) 9236c42ca1SJulius Werner * Function to initialize and register a new 16550 9336c42ca1SJulius Werner * console. Storage passed in for the console struct 9436c42ca1SJulius Werner * *must* be persistent (i.e. not from the stack). 9536c42ca1SJulius Werner * In: x0 - UART register base address 9636c42ca1SJulius Werner * w1 - UART clock in Hz 9736c42ca1SJulius Werner * w2 - Baud rate 9836c42ca1SJulius Werner * x3 - pointer to empty console_16550_t struct 9936c42ca1SJulius Werner * Out: return 1 on success, 0 on error 10036c42ca1SJulius Werner * Clobber list : x0, x1, x2, x6, x7, x14 10136c42ca1SJulius Werner * ----------------------------------------------- 10236c42ca1SJulius Werner */ 10336c42ca1SJulius Wernerfunc console_16550_register 10436c42ca1SJulius Werner mov x7, x30 10536c42ca1SJulius Werner mov x6, x3 10636c42ca1SJulius Werner cbz x6, register_fail 10736c42ca1SJulius Werner str x0, [x6, #CONSOLE_T_16550_BASE] 10836c42ca1SJulius Werner 10936c42ca1SJulius Werner bl console_16550_core_init 11036c42ca1SJulius Werner cbz x0, register_fail 11136c42ca1SJulius Werner 11236c42ca1SJulius Werner mov x0, x6 11336c42ca1SJulius Werner mov x30, x7 11436c42ca1SJulius Werner finish_console_register 16550 11536c42ca1SJulius Werner 11636c42ca1SJulius Wernerregister_fail: 11736c42ca1SJulius Werner ret x7 11836c42ca1SJulius Wernerendfunc console_16550_register 11936c42ca1SJulius Werner#else 12036c42ca1SJulius Werner .globl console_core_init 12136c42ca1SJulius Werner .globl console_core_putc 12236c42ca1SJulius Werner .globl console_core_getc 12336c42ca1SJulius Werner .globl console_core_flush 12436c42ca1SJulius Werner .equ console_core_init,console_16550_core_init 12536c42ca1SJulius Werner .equ console_core_putc,console_16550_core_putc 12636c42ca1SJulius Werner .equ console_core_getc,console_16550_core_getc 1274f8053ddSAntonio Nino Diaz .equ console_core_flush,console_16550_core_flush 12836c42ca1SJulius Werner#endif 1299c94d3b3SSoby Mathew 1309c94d3b3SSoby Mathew /* -------------------------------------------------------- 13136c42ca1SJulius Werner * int console_16550_core_putc(int c, uintptr_t base_addr) 1329c94d3b3SSoby Mathew * Function to output a character over the console. It 1339c94d3b3SSoby Mathew * returns the character printed on success or -1 on error. 1349c94d3b3SSoby Mathew * In : w0 - character to be printed 1359c94d3b3SSoby Mathew * x1 - console base address 1369c94d3b3SSoby Mathew * Out : return -1 on error else return character. 1379c94d3b3SSoby Mathew * Clobber list : x2 1389c94d3b3SSoby Mathew * -------------------------------------------------------- 1399c94d3b3SSoby Mathew */ 14036c42ca1SJulius Wernerfunc console_16550_core_putc 14136c42ca1SJulius Werner#if ENABLE_ASSERTIONS 14236c42ca1SJulius Werner cmp x1, #0 14336c42ca1SJulius Werner ASM_ASSERT(ne) 14436c42ca1SJulius Werner#endif /* ENABLE_ASSERTIONS */ 1459c94d3b3SSoby Mathew 1469c94d3b3SSoby Mathew /* Prepend '\r' to '\n' */ 1479c94d3b3SSoby Mathew cmp w0, #0xA 1489c94d3b3SSoby Mathew b.ne 2f 1499c94d3b3SSoby Mathew /* Check if the transmit FIFO is full */ 1509c94d3b3SSoby Mathew1: ldr w2, [x1, #UARTLSR] 1519c94d3b3SSoby Mathew and w2, w2, #(UARTLSR_TEMT | UARTLSR_THRE) 1529c94d3b3SSoby Mathew cmp w2, #(UARTLSR_TEMT | UARTLSR_THRE) 1539c94d3b3SSoby Mathew b.ne 1b 1549c94d3b3SSoby Mathew mov w2, #0xD /* '\r' */ 1559c94d3b3SSoby Mathew str w2, [x1, #UARTTX] 1569c94d3b3SSoby Mathew 1579c94d3b3SSoby Mathew /* Check if the transmit FIFO is full */ 1589c94d3b3SSoby Mathew2: ldr w2, [x1, #UARTLSR] 1599c94d3b3SSoby Mathew and w2, w2, #(UARTLSR_TEMT | UARTLSR_THRE) 1609c94d3b3SSoby Mathew cmp w2, #(UARTLSR_TEMT | UARTLSR_THRE) 1619c94d3b3SSoby Mathew b.ne 2b 1629c94d3b3SSoby Mathew str w0, [x1, #UARTTX] 1639c94d3b3SSoby Mathew ret 16436c42ca1SJulius Wernerendfunc console_16550_core_putc 16536c42ca1SJulius Werner 16636c42ca1SJulius Werner /* -------------------------------------------------------- 16736c42ca1SJulius Werner * int console_16550_putc(int c, console_16550_t *console) 16836c42ca1SJulius Werner * Function to output a character over the console. It 16936c42ca1SJulius Werner * returns the character printed on success or -1 on error. 17036c42ca1SJulius Werner * In : w0 - character to be printed 17136c42ca1SJulius Werner * x1 - pointer to console_t structure 17236c42ca1SJulius Werner * Out : return -1 on error else return character. 17336c42ca1SJulius Werner * Clobber list : x2 17436c42ca1SJulius Werner * -------------------------------------------------------- 17536c42ca1SJulius Werner */ 17636c42ca1SJulius Wernerfunc console_16550_putc 17736c42ca1SJulius Werner#if ENABLE_ASSERTIONS 17836c42ca1SJulius Werner cmp x1, #0 17936c42ca1SJulius Werner ASM_ASSERT(ne) 18036c42ca1SJulius Werner#endif /* ENABLE_ASSERTIONS */ 18136c42ca1SJulius Werner ldr x1, [x1, #CONSOLE_T_16550_BASE] 18236c42ca1SJulius Werner b console_16550_core_putc 18336c42ca1SJulius Wernerendfunc console_16550_putc 1849c94d3b3SSoby Mathew 1859c94d3b3SSoby Mathew /* --------------------------------------------- 18636c42ca1SJulius Werner * int console_16550_core_getc(uintptr_t base_addr) 1879c94d3b3SSoby Mathew * Function to get a character from the console. 1889c94d3b3SSoby Mathew * It returns the character grabbed on success 18936c42ca1SJulius Werner * or -1 on if no character is available. 19036c42ca1SJulius Werner * In : x0 - console base address 19136c42ca1SJulius Werner * Out : w0 - character if available, else -1 1929c94d3b3SSoby Mathew * Clobber list : x0, x1 1939c94d3b3SSoby Mathew * --------------------------------------------- 1949c94d3b3SSoby Mathew */ 19536c42ca1SJulius Wernerfunc console_16550_core_getc 19636c42ca1SJulius Werner#if ENABLE_ASSERTIONS 19736c42ca1SJulius Werner cmp x0, #0 19836c42ca1SJulius Werner ASM_ASSERT(ne) 19936c42ca1SJulius Werner#endif /* ENABLE_ASSERTIONS */ 20036c42ca1SJulius Werner 2019c94d3b3SSoby Mathew /* Check if the receive FIFO is empty */ 2029c94d3b3SSoby Mathew1: ldr w1, [x0, #UARTLSR] 20336c42ca1SJulius Werner tbz w1, #UARTLSR_RDR_BIT, no_char 2049c94d3b3SSoby Mathew ldr w0, [x0, #UARTRX] 2059c94d3b3SSoby Mathew ret 20636c42ca1SJulius Wernerno_char: 20736c42ca1SJulius Werner mov w0, #ERROR_NO_PENDING_CHAR 2089c94d3b3SSoby Mathew ret 20936c42ca1SJulius Wernerendfunc console_16550_core_getc 21036c42ca1SJulius Werner 21136c42ca1SJulius Werner /* --------------------------------------------- 21236c42ca1SJulius Werner * int console_16550_getc(console_16550_t *console) 21336c42ca1SJulius Werner * Function to get a character from the console. 21436c42ca1SJulius Werner * It returns the character grabbed on success 21536c42ca1SJulius Werner * or -1 on if no character is available. 21636c42ca1SJulius Werner * In : x0 - pointer to console_t stucture 21736c42ca1SJulius Werner * Out : w0 - character if available, else -1 21836c42ca1SJulius Werner * Clobber list : x0, x1 21936c42ca1SJulius Werner * --------------------------------------------- 22036c42ca1SJulius Werner */ 22136c42ca1SJulius Wernerfunc console_16550_getc 22236c42ca1SJulius Werner#if ENABLE_ASSERTIONS 22336c42ca1SJulius Werner cmp x1, #0 22436c42ca1SJulius Werner ASM_ASSERT(ne) 22536c42ca1SJulius Werner#endif /* ENABLE_ASSERTIONS */ 22636c42ca1SJulius Werner ldr x0, [x0, #CONSOLE_T_16550_BASE] 22736c42ca1SJulius Werner b console_16550_core_getc 22836c42ca1SJulius Wernerendfunc console_16550_getc 229ad4c2ec6SAntonio Nino Diaz 230ad4c2ec6SAntonio Nino Diaz /* --------------------------------------------- 2314f8053ddSAntonio Nino Diaz * int console_16550_core_flush(uintptr_t base_addr) 232ad4c2ec6SAntonio Nino Diaz * Function to force a write of all buffered 233ad4c2ec6SAntonio Nino Diaz * data that hasn't been output. 234ad4c2ec6SAntonio Nino Diaz * In : x0 - console base address 235ad4c2ec6SAntonio Nino Diaz * Out : return -1 on error else return 0. 236ad4c2ec6SAntonio Nino Diaz * Clobber list : x0, x1 237ad4c2ec6SAntonio Nino Diaz * --------------------------------------------- 238ad4c2ec6SAntonio Nino Diaz */ 2394f8053ddSAntonio Nino Diazfunc console_16550_core_flush 2404f8053ddSAntonio Nino Diaz#if ENABLE_ASSERTIONS 2414f8053ddSAntonio Nino Diaz cmp x0, #0 2424f8053ddSAntonio Nino Diaz ASM_ASSERT(ne) 2434f8053ddSAntonio Nino Diaz#endif /* ENABLE_ASSERTIONS */ 2444f8053ddSAntonio Nino Diaz 2454f8053ddSAntonio Nino Diaz /* Loop until the transmit FIFO is empty */ 2464f8053ddSAntonio Nino Diaz1: ldr w1, [x0, #UARTLSR] 2474f8053ddSAntonio Nino Diaz and w1, w1, #(UARTLSR_TEMT | UARTLSR_THRE) 2484f8053ddSAntonio Nino Diaz cmp w1, #(UARTLSR_TEMT | UARTLSR_THRE) 2494f8053ddSAntonio Nino Diaz b.ne 1b 2504f8053ddSAntonio Nino Diaz 251ad4c2ec6SAntonio Nino Diaz mov w0, #0 252ad4c2ec6SAntonio Nino Diaz ret 2534f8053ddSAntonio Nino Diazendfunc console_16550_core_flush 2544f8053ddSAntonio Nino Diaz 2554f8053ddSAntonio Nino Diaz /* --------------------------------------------- 2564f8053ddSAntonio Nino Diaz * int console_16550_flush(console_pl011_t *console) 2574f8053ddSAntonio Nino Diaz * Function to force a write of all buffered 2584f8053ddSAntonio Nino Diaz * data that hasn't been output. 2594f8053ddSAntonio Nino Diaz * In : x0 - pointer to console_t structure 2604f8053ddSAntonio Nino Diaz * Out : return -1 on error else return 0. 2614f8053ddSAntonio Nino Diaz * Clobber list : x0, x1 2624f8053ddSAntonio Nino Diaz * --------------------------------------------- 2634f8053ddSAntonio Nino Diaz */ 2644f8053ddSAntonio Nino Diazfunc console_16550_flush 2654f8053ddSAntonio Nino Diaz#if ENABLE_ASSERTIONS 2664f8053ddSAntonio Nino Diaz cmp x0, #0 2674f8053ddSAntonio Nino Diaz ASM_ASSERT(ne) 2684f8053ddSAntonio Nino Diaz#endif /* ENABLE_ASSERTIONS */ 2694f8053ddSAntonio Nino Diaz ldr x0, [x0, #CONSOLE_T_16550_BASE] 2704f8053ddSAntonio Nino Diaz b console_16550_core_flush 2714f8053ddSAntonio Nino Diazendfunc console_16550_flush 272