16f78eb5cSHeiko Stuebner/* 2831b0e98SJimmy Brisson * Copyright (c) 2015-2020, ARM Limited and Contributors. All rights reserved. 36f78eb5cSHeiko Stuebner * 46f78eb5cSHeiko Stuebner * SPDX-License-Identifier: BSD-3-Clause 56f78eb5cSHeiko Stuebner */ 66f78eb5cSHeiko Stuebner 76f78eb5cSHeiko Stuebner#include <arch.h> 86f78eb5cSHeiko Stuebner#include <asm_macros.S> 96f78eb5cSHeiko Stuebner#include <assert_macros.S> 106f78eb5cSHeiko Stuebner#include <console_macros.S> 116f78eb5cSHeiko Stuebner#include <drivers/ti/uart/uart_16550.h> 126f78eb5cSHeiko Stuebner 136f78eb5cSHeiko Stuebner /* 146f78eb5cSHeiko Stuebner * "core" functions are low-level implementations that don't require 156f78eb5cSHeiko Stuebner * writable memory and are thus safe to call in BL1 crash context. 166f78eb5cSHeiko Stuebner */ 176f78eb5cSHeiko Stuebner .globl console_16550_core_init 186f78eb5cSHeiko Stuebner .globl console_16550_core_putc 196f78eb5cSHeiko Stuebner .globl console_16550_core_getc 206f78eb5cSHeiko Stuebner .globl console_16550_core_flush 216f78eb5cSHeiko Stuebner 226f78eb5cSHeiko Stuebner .globl console_16550_putc 236f78eb5cSHeiko Stuebner .globl console_16550_getc 246f78eb5cSHeiko Stuebner .globl console_16550_flush 256f78eb5cSHeiko Stuebner 266f78eb5cSHeiko Stuebner /* ----------------------------------------------- 276f78eb5cSHeiko Stuebner * int console_16550_core_init(uintptr_t base_addr, 286f78eb5cSHeiko Stuebner * unsigned int uart_clk, unsigned int baud_rate) 296f78eb5cSHeiko Stuebner * Function to initialize the console without a 306f78eb5cSHeiko Stuebner * C Runtime to print debug information. This 316f78eb5cSHeiko Stuebner * function will be accessed by console_init and 326f78eb5cSHeiko Stuebner * crash reporting. 336f78eb5cSHeiko Stuebner * In: r0 - console base address 346f78eb5cSHeiko Stuebner * r1 - Uart clock in Hz 356f78eb5cSHeiko Stuebner * r2 - Baud rate 366f78eb5cSHeiko Stuebner * Out: return 1 on success, 0 on error 376f78eb5cSHeiko Stuebner * Clobber list : r1, r2, r3 386f78eb5cSHeiko Stuebner * ----------------------------------------------- 396f78eb5cSHeiko Stuebner */ 406f78eb5cSHeiko Stuebnerfunc console_16550_core_init 416f78eb5cSHeiko Stuebner /* Check the input base address */ 426f78eb5cSHeiko Stuebner cmp r0, #0 436f78eb5cSHeiko Stuebner beq init_fail 446f78eb5cSHeiko Stuebner /* Check baud rate and uart clock for sanity */ 456f78eb5cSHeiko Stuebner cmp r1, #0 466f78eb5cSHeiko Stuebner beq init_fail 476f78eb5cSHeiko Stuebner cmp r2, #0 486f78eb5cSHeiko Stuebner beq init_fail 496f78eb5cSHeiko Stuebner 506f78eb5cSHeiko Stuebner /* Program the baudrate */ 516f78eb5cSHeiko Stuebner /* Divisor = Uart clock / (16 * baudrate) */ 526f78eb5cSHeiko Stuebner lsl r2, r2, #4 536f78eb5cSHeiko Stuebner udiv r2, r1, r2 546f78eb5cSHeiko Stuebner and r1, r2, #0xff /* w1 = DLL */ 556f78eb5cSHeiko Stuebner lsr r2, r2, #8 566f78eb5cSHeiko Stuebner and r2, r2, #0xff /* w2 = DLLM */ 576f78eb5cSHeiko Stuebner ldr r3, [r0, #UARTLCR] 586f78eb5cSHeiko Stuebner orr r3, r3, #UARTLCR_DLAB 596f78eb5cSHeiko Stuebner str r3, [r0, #UARTLCR] /* enable DLL, DLLM programming */ 606f78eb5cSHeiko Stuebner str r1, [r0, #UARTDLL] /* program DLL */ 616f78eb5cSHeiko Stuebner str r2, [r0, #UARTDLLM] /* program DLLM */ 626f78eb5cSHeiko Stuebner mov r2, #~UARTLCR_DLAB 636f78eb5cSHeiko Stuebner and r3, r3, r2 646f78eb5cSHeiko Stuebner str r3, [r0, #UARTLCR] /* disable DLL, DLLM programming */ 656f78eb5cSHeiko Stuebner 666f78eb5cSHeiko Stuebner /* 8n1 */ 676f78eb5cSHeiko Stuebner mov r3, #3 686f78eb5cSHeiko Stuebner str r3, [r0, #UARTLCR] 696f78eb5cSHeiko Stuebner /* no interrupt */ 706f78eb5cSHeiko Stuebner mov r3, #0 716f78eb5cSHeiko Stuebner str r3, [r0, #UARTIER] 726f78eb5cSHeiko Stuebner#ifdef TI_16550_MDR_QUIRK 736f78eb5cSHeiko Stuebner /* UART must be enabled on some platforms via the MDR register */ 746f78eb5cSHeiko Stuebner str r3, [r0, #UARTMDR1] 756f78eb5cSHeiko Stuebner#endif /* TI_16550_MDR_QUIRK */ 766f78eb5cSHeiko Stuebner /* enable fifo, DMA */ 776f78eb5cSHeiko Stuebner mov r3, #(UARTFCR_FIFOEN | UARTFCR_DMAEN) 786f78eb5cSHeiko Stuebner str r3, [r0, #UARTFCR] 796f78eb5cSHeiko Stuebner /* DTR + RTS */ 806f78eb5cSHeiko Stuebner mov r3, #3 816f78eb5cSHeiko Stuebner str r3, [r0, #UARTMCR] 826f78eb5cSHeiko Stuebner mov r0, #1 836f78eb5cSHeiko Stuebner bx lr 846f78eb5cSHeiko Stuebnerinit_fail: 856f78eb5cSHeiko Stuebner mov r0, #0 866f78eb5cSHeiko Stuebner bx lr 876f78eb5cSHeiko Stuebnerendfunc console_16550_core_init 886f78eb5cSHeiko Stuebner 896f78eb5cSHeiko Stuebner .globl console_16550_register 906f78eb5cSHeiko Stuebner 916f78eb5cSHeiko Stuebner /* ------------------------------------------------------- 92cd50ffd2SAndre Przywara * int console_16550_register(uintptr_t baseaddr, 936f78eb5cSHeiko Stuebner * uint32_t clock, uint32_t baud, 9498964f05SAndre Przywara * console_t *console); 95cd50ffd2SAndre Przywara * Function to initialize and register a new 16550 966f78eb5cSHeiko Stuebner * console. Storage passed in for the console struct 976f78eb5cSHeiko Stuebner * *must* be persistent (i.e. not from the stack). 98cd50ffd2SAndre Przywara * If r1 (UART clock) is 0, initialisation will be 99cd50ffd2SAndre Przywara * skipped, relying on previous code to have done 100cd50ffd2SAndre Przywara * this already. r2 is ignored then as well. 1016f78eb5cSHeiko Stuebner * In: r0 - UART register base address 1026f78eb5cSHeiko Stuebner * r1 - UART clock in Hz 103cd50ffd2SAndre Przywara * r2 - Baud rate (ignored if r1 is 0) 10498964f05SAndre Przywara * r3 - pointer to empty console_t struct 1056f78eb5cSHeiko Stuebner * Out: return 1 on success, 0 on error 1066f78eb5cSHeiko Stuebner * Clobber list : r0, r1, r2 1076f78eb5cSHeiko Stuebner * ------------------------------------------------------- 1086f78eb5cSHeiko Stuebner */ 1096f78eb5cSHeiko Stuebnerfunc console_16550_register 1106f78eb5cSHeiko Stuebner push {r4, lr} 1116f78eb5cSHeiko Stuebner mov r4, r3 1126f78eb5cSHeiko Stuebner cmp r4, #0 1136f78eb5cSHeiko Stuebner beq register_fail 11498964f05SAndre Przywara str r0, [r4, #CONSOLE_T_BASE] 1156f78eb5cSHeiko Stuebner 116cd50ffd2SAndre Przywara /* A clock rate of zero means to skip the initialisation. */ 117cd50ffd2SAndre Przywara cmp r1, #0 118cd50ffd2SAndre Przywara beq register_16550 119cd50ffd2SAndre Przywara 1206f78eb5cSHeiko Stuebner bl console_16550_core_init 1216f78eb5cSHeiko Stuebner cmp r0, #0 1226f78eb5cSHeiko Stuebner beq register_fail 1236f78eb5cSHeiko Stuebner 124cd50ffd2SAndre Przywararegister_16550: 1256f78eb5cSHeiko Stuebner mov r0, r4 1266f78eb5cSHeiko Stuebner pop {r4, lr} 127*85bebe18SSandrine Bailleux finish_console_register 16550 putc=1, getc=ENABLE_CONSOLE_GETC, flush=1 1286f78eb5cSHeiko Stuebner 1296f78eb5cSHeiko Stuebnerregister_fail: 1306f78eb5cSHeiko Stuebner pop {r4, pc} 1316f78eb5cSHeiko Stuebnerendfunc console_16550_register 1326f78eb5cSHeiko Stuebner 1336f78eb5cSHeiko Stuebner /* -------------------------------------------------------- 1346f78eb5cSHeiko Stuebner * int console_16550_core_putc(int c, uintptr_t base_addr) 1356f78eb5cSHeiko Stuebner * Function to output a character over the console. It 1366f78eb5cSHeiko Stuebner * returns the character printed on success or -1 on error. 1376f78eb5cSHeiko Stuebner * In : r0 - character to be printed 1386f78eb5cSHeiko Stuebner * r1 - console base address 1396f78eb5cSHeiko Stuebner * Out : return -1 on error else return character. 1406f78eb5cSHeiko Stuebner * Clobber list : r2 1416f78eb5cSHeiko Stuebner * -------------------------------------------------------- 1426f78eb5cSHeiko Stuebner */ 1436f78eb5cSHeiko Stuebnerfunc console_16550_core_putc 1446f78eb5cSHeiko Stuebner#if ENABLE_ASSERTIONS 1456f78eb5cSHeiko Stuebner cmp r1, #0 1466f78eb5cSHeiko Stuebner ASM_ASSERT(ne) 1476f78eb5cSHeiko Stuebner#endif /* ENABLE_ASSERTIONS */ 1486f78eb5cSHeiko Stuebner 1496f78eb5cSHeiko Stuebner /* Prepend '\r' to '\n' */ 1506f78eb5cSHeiko Stuebner cmp r0, #0xA 1516f78eb5cSHeiko Stuebner bne 2f 1526f78eb5cSHeiko Stuebner /* Check if the transmit FIFO is full */ 1536f78eb5cSHeiko Stuebner1: ldr r2, [r1, #UARTLSR] 1546f78eb5cSHeiko Stuebner and r2, r2, #(UARTLSR_TEMT | UARTLSR_THRE) 1556f78eb5cSHeiko Stuebner cmp r2, #(UARTLSR_TEMT | UARTLSR_THRE) 1566f78eb5cSHeiko Stuebner bne 1b 1576f78eb5cSHeiko Stuebner mov r2, #0xD /* '\r' */ 1586f78eb5cSHeiko Stuebner str r2, [r1, #UARTTX] 1596f78eb5cSHeiko Stuebner 1606f78eb5cSHeiko Stuebner /* Check if the transmit FIFO is full */ 1616f78eb5cSHeiko Stuebner2: ldr r2, [r1, #UARTLSR] 1626f78eb5cSHeiko Stuebner and r2, r2, #(UARTLSR_TEMT | UARTLSR_THRE) 1636f78eb5cSHeiko Stuebner cmp r2, #(UARTLSR_TEMT | UARTLSR_THRE) 1646f78eb5cSHeiko Stuebner bne 2b 1656f78eb5cSHeiko Stuebner str r0, [r1, #UARTTX] 1666f78eb5cSHeiko Stuebner bx lr 1676f78eb5cSHeiko Stuebnerendfunc console_16550_core_putc 1686f78eb5cSHeiko Stuebner 1696f78eb5cSHeiko Stuebner /* -------------------------------------------------------- 17098964f05SAndre Przywara * int console_16550_putc(int c, console_t *console) 1716f78eb5cSHeiko Stuebner * Function to output a character over the console. It 1726f78eb5cSHeiko Stuebner * returns the character printed on success or -1 on error. 1736f78eb5cSHeiko Stuebner * In : r0 - character to be printed 1746f78eb5cSHeiko Stuebner * r1 - pointer to console_t structure 1756f78eb5cSHeiko Stuebner * Out : return -1 on error else return character. 1766f78eb5cSHeiko Stuebner * Clobber list : r2 1776f78eb5cSHeiko Stuebner * -------------------------------------------------------- 1786f78eb5cSHeiko Stuebner */ 1796f78eb5cSHeiko Stuebnerfunc console_16550_putc 1806f78eb5cSHeiko Stuebner#if ENABLE_ASSERTIONS 1816f78eb5cSHeiko Stuebner cmp r1, #0 1826f78eb5cSHeiko Stuebner ASM_ASSERT(ne) 1836f78eb5cSHeiko Stuebner#endif /* ENABLE_ASSERTIONS */ 18498964f05SAndre Przywara ldr r1, [r1, #CONSOLE_T_BASE] 1856f78eb5cSHeiko Stuebner b console_16550_core_putc 1866f78eb5cSHeiko Stuebnerendfunc console_16550_putc 1876f78eb5cSHeiko Stuebner 1886f78eb5cSHeiko Stuebner /* --------------------------------------------- 1896f78eb5cSHeiko Stuebner * int console_16550_core_getc(uintptr_t base_addr) 1906f78eb5cSHeiko Stuebner * Function to get a character from the console. 1916f78eb5cSHeiko Stuebner * It returns the character grabbed on success 1926f78eb5cSHeiko Stuebner * or -1 on if no character is available. 1936f78eb5cSHeiko Stuebner * In : r0 - console base address 1946f78eb5cSHeiko Stuebner * Clobber list : r0, r1 1956f78eb5cSHeiko Stuebner * --------------------------------------------- 1966f78eb5cSHeiko Stuebner */ 1976f78eb5cSHeiko Stuebnerfunc console_16550_core_getc 1986f78eb5cSHeiko Stuebner#if ENABLE_ASSERTIONS 1996f78eb5cSHeiko Stuebner cmp r0, #0 2006f78eb5cSHeiko Stuebner ASM_ASSERT(ne) 2016f78eb5cSHeiko Stuebner#endif /* ENABLE_ASSERTIONS */ 2026f78eb5cSHeiko Stuebner 2036f78eb5cSHeiko Stuebner /* Check if the receive FIFO is empty */ 2046f78eb5cSHeiko Stuebner1: ldr r1, [r0, #UARTLSR] 2056f78eb5cSHeiko Stuebner tst r1, #UARTLSR_RDR_BIT 2066f78eb5cSHeiko Stuebner beq no_char 2076f78eb5cSHeiko Stuebner ldr r1, [r0, #UARTRX] 2086f78eb5cSHeiko Stuebner mov r0, r1 2096f78eb5cSHeiko Stuebner bx lr 2106f78eb5cSHeiko Stuebnerno_char: 2116f78eb5cSHeiko Stuebner mov r0, #ERROR_NO_PENDING_CHAR 2126f78eb5cSHeiko Stuebner bx lr 2136f78eb5cSHeiko Stuebnerendfunc console_16550_core_getc 2146f78eb5cSHeiko Stuebner 2156f78eb5cSHeiko Stuebner /* --------------------------------------------- 21698964f05SAndre Przywara * int console_16550_getc(console_t *console) 2176f78eb5cSHeiko Stuebner * Function to get a character from the console. 2186f78eb5cSHeiko Stuebner * It returns the character grabbed on success 2196f78eb5cSHeiko Stuebner * or -1 on if no character is available. 2206f78eb5cSHeiko Stuebner * In : r0 - pointer to console_t stucture 2216f78eb5cSHeiko Stuebner * Out : r0 - character if available, else -1 2226f78eb5cSHeiko Stuebner * Clobber list : r0, r1 2236f78eb5cSHeiko Stuebner * --------------------------------------------- 2246f78eb5cSHeiko Stuebner */ 2256f78eb5cSHeiko Stuebnerfunc console_16550_getc 2266f78eb5cSHeiko Stuebner#if ENABLE_ASSERTIONS 2276f78eb5cSHeiko Stuebner cmp r0, #0 2286f78eb5cSHeiko Stuebner ASM_ASSERT(ne) 2296f78eb5cSHeiko Stuebner#endif /* ENABLE_ASSERTIONS */ 23098964f05SAndre Przywara ldr r0, [r0, #CONSOLE_T_BASE] 2316f78eb5cSHeiko Stuebner b console_16550_core_getc 2326f78eb5cSHeiko Stuebnerendfunc console_16550_getc 2336f78eb5cSHeiko Stuebner 2346f78eb5cSHeiko Stuebner /* --------------------------------------------- 235831b0e98SJimmy Brisson * void console_16550_core_flush(uintptr_t base_addr) 2366f78eb5cSHeiko Stuebner * Function to force a write of all buffered 2376f78eb5cSHeiko Stuebner * data that hasn't been output. 2386f78eb5cSHeiko Stuebner * In : r0 - console base address 239831b0e98SJimmy Brisson * Out : void. 2406f78eb5cSHeiko Stuebner * Clobber list : r0, r1 2416f78eb5cSHeiko Stuebner * --------------------------------------------- 2426f78eb5cSHeiko Stuebner */ 2436f78eb5cSHeiko Stuebnerfunc console_16550_core_flush 2446f78eb5cSHeiko Stuebner#if ENABLE_ASSERTIONS 2456f78eb5cSHeiko Stuebner cmp r0, #0 2466f78eb5cSHeiko Stuebner ASM_ASSERT(ne) 2476f78eb5cSHeiko Stuebner#endif /* ENABLE_ASSERTIONS */ 2486f78eb5cSHeiko Stuebner 2496f78eb5cSHeiko Stuebner /* Loop until the transmit FIFO is empty */ 2506f78eb5cSHeiko Stuebner1: ldr r1, [r0, #UARTLSR] 2516f78eb5cSHeiko Stuebner and r1, r1, #(UARTLSR_TEMT | UARTLSR_THRE) 2526f78eb5cSHeiko Stuebner cmp r1, #(UARTLSR_TEMT | UARTLSR_THRE) 2536f78eb5cSHeiko Stuebner bne 1b 2546f78eb5cSHeiko Stuebner 2556f78eb5cSHeiko Stuebner bx lr 2566f78eb5cSHeiko Stuebnerendfunc console_16550_core_flush 2576f78eb5cSHeiko Stuebner 2586f78eb5cSHeiko Stuebner /* --------------------------------------------- 259831b0e98SJimmy Brisson * void console_16550_flush(console_t *console) 2606f78eb5cSHeiko Stuebner * Function to force a write of all buffered 2616f78eb5cSHeiko Stuebner * data that hasn't been output. 2626f78eb5cSHeiko Stuebner * In : r0 - pointer to console_t structure 263831b0e98SJimmy Brisson * Out : void 2646f78eb5cSHeiko Stuebner * Clobber list : r0, r1 2656f78eb5cSHeiko Stuebner * --------------------------------------------- 2666f78eb5cSHeiko Stuebner */ 2676f78eb5cSHeiko Stuebnerfunc console_16550_flush 2686f78eb5cSHeiko Stuebner#if ENABLE_ASSERTIONS 2696f78eb5cSHeiko Stuebner cmp r0, #0 2706f78eb5cSHeiko Stuebner ASM_ASSERT(ne) 2716f78eb5cSHeiko Stuebner#endif /* ENABLE_ASSERTIONS */ 27298964f05SAndre Przywara ldr r0, [r0, #CONSOLE_T_BASE] 2736f78eb5cSHeiko Stuebner b console_16550_core_flush 2746f78eb5cSHeiko Stuebnerendfunc console_16550_flush 275