19c94d3b3SSoby Mathew/* 2831b0e98SJimmy Brisson * Copyright (c) 2015-2020, 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> 1109d40e0eSAntonio Nino Diaz#include <drivers/ti/uart/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] 69529b541eSBenjamin Fair#ifdef TI_16550_MDR_QUIRK 70529b541eSBenjamin Fair /* UART must be enabled on some platforms via the MDR register */ 71529b541eSBenjamin Fair str w3, [x0, #UARTMDR1] 72529b541eSBenjamin 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 .globl console_16550_register 8736c42ca1SJulius Werner 8836c42ca1SJulius Werner /* ----------------------------------------------- 8965199dc8SAntonio Nino Diaz * int console_16550_register(uintptr_t baseaddr, 9065199dc8SAntonio Nino Diaz * uint32_t clock, uint32_t baud, 9198964f05SAndre Przywara * console_t *console); 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). 95cd50ffd2SAndre Przywara * If w1 (UART clock) is 0, initialisation will be 96cd50ffd2SAndre Przywara * skipped, relying on previous code to have done 97cd50ffd2SAndre Przywara * this already. w2 is ignored then as well. 9836c42ca1SJulius Werner * In: x0 - UART register base address 9936c42ca1SJulius Werner * w1 - UART clock in Hz 100cd50ffd2SAndre Przywara * w2 - Baud rate (ignored if w1 is 0) 10198964f05SAndre Przywara * x3 - pointer to empty console_t struct 10236c42ca1SJulius Werner * Out: return 1 on success, 0 on error 10336c42ca1SJulius Werner * Clobber list : x0, x1, x2, x6, x7, x14 10436c42ca1SJulius Werner * ----------------------------------------------- 10536c42ca1SJulius Werner */ 10636c42ca1SJulius Wernerfunc console_16550_register 10736c42ca1SJulius Werner mov x7, x30 10836c42ca1SJulius Werner mov x6, x3 10936c42ca1SJulius Werner cbz x6, register_fail 11098964f05SAndre Przywara str x0, [x6, #CONSOLE_T_BASE] 11136c42ca1SJulius Werner 112cd50ffd2SAndre Przywara /* A clock rate of zero means to skip the initialisation. */ 113cd50ffd2SAndre Przywara cbz w1, register_16550 114cd50ffd2SAndre Przywara 11536c42ca1SJulius Werner bl console_16550_core_init 11636c42ca1SJulius Werner cbz x0, register_fail 11736c42ca1SJulius Werner 118cd50ffd2SAndre Przywararegister_16550: 11936c42ca1SJulius Werner mov x0, x6 12036c42ca1SJulius Werner mov x30, x7 121*85bebe18SSandrine Bailleux finish_console_register 16550 putc=1, getc=ENABLE_CONSOLE_GETC, flush=1 12236c42ca1SJulius Werner 12336c42ca1SJulius Wernerregister_fail: 12436c42ca1SJulius Werner ret x7 12536c42ca1SJulius Wernerendfunc console_16550_register 1269c94d3b3SSoby Mathew 1279c94d3b3SSoby Mathew /* -------------------------------------------------------- 12836c42ca1SJulius Werner * int console_16550_core_putc(int c, uintptr_t base_addr) 1299c94d3b3SSoby Mathew * Function to output a character over the console. It 1309c94d3b3SSoby Mathew * returns the character printed on success or -1 on error. 1319c94d3b3SSoby Mathew * In : w0 - character to be printed 1329c94d3b3SSoby Mathew * x1 - console base address 1339c94d3b3SSoby Mathew * Out : return -1 on error else return character. 1349c94d3b3SSoby Mathew * Clobber list : x2 1359c94d3b3SSoby Mathew * -------------------------------------------------------- 1369c94d3b3SSoby Mathew */ 13736c42ca1SJulius Wernerfunc console_16550_core_putc 13836c42ca1SJulius Werner#if ENABLE_ASSERTIONS 13936c42ca1SJulius Werner cmp x1, #0 14036c42ca1SJulius Werner ASM_ASSERT(ne) 14136c42ca1SJulius Werner#endif /* ENABLE_ASSERTIONS */ 1429c94d3b3SSoby Mathew 1439c94d3b3SSoby Mathew /* Prepend '\r' to '\n' */ 1449c94d3b3SSoby Mathew cmp w0, #0xA 1459c94d3b3SSoby Mathew b.ne 2f 1469c94d3b3SSoby Mathew /* Check if the transmit FIFO is full */ 1479c94d3b3SSoby Mathew1: ldr w2, [x1, #UARTLSR] 1489c94d3b3SSoby Mathew and w2, w2, #(UARTLSR_TEMT | UARTLSR_THRE) 1499c94d3b3SSoby Mathew cmp w2, #(UARTLSR_TEMT | UARTLSR_THRE) 1509c94d3b3SSoby Mathew b.ne 1b 1519c94d3b3SSoby Mathew mov w2, #0xD /* '\r' */ 1529c94d3b3SSoby Mathew str w2, [x1, #UARTTX] 1539c94d3b3SSoby Mathew 1549c94d3b3SSoby Mathew /* Check if the transmit FIFO is full */ 1559c94d3b3SSoby Mathew2: ldr w2, [x1, #UARTLSR] 1569c94d3b3SSoby Mathew and w2, w2, #(UARTLSR_TEMT | UARTLSR_THRE) 1579c94d3b3SSoby Mathew cmp w2, #(UARTLSR_TEMT | UARTLSR_THRE) 1589c94d3b3SSoby Mathew b.ne 2b 1599c94d3b3SSoby Mathew str w0, [x1, #UARTTX] 1609c94d3b3SSoby Mathew ret 16136c42ca1SJulius Wernerendfunc console_16550_core_putc 16236c42ca1SJulius Werner 16336c42ca1SJulius Werner /* -------------------------------------------------------- 16498964f05SAndre Przywara * int console_16550_putc(int c, console_t *console) 16536c42ca1SJulius Werner * Function to output a character over the console. It 16636c42ca1SJulius Werner * returns the character printed on success or -1 on error. 16736c42ca1SJulius Werner * In : w0 - character to be printed 16836c42ca1SJulius Werner * x1 - pointer to console_t structure 16936c42ca1SJulius Werner * Out : return -1 on error else return character. 17036c42ca1SJulius Werner * Clobber list : x2 17136c42ca1SJulius Werner * -------------------------------------------------------- 17236c42ca1SJulius Werner */ 17336c42ca1SJulius Wernerfunc console_16550_putc 17436c42ca1SJulius Werner#if ENABLE_ASSERTIONS 17536c42ca1SJulius Werner cmp x1, #0 17636c42ca1SJulius Werner ASM_ASSERT(ne) 17736c42ca1SJulius Werner#endif /* ENABLE_ASSERTIONS */ 17898964f05SAndre Przywara ldr x1, [x1, #CONSOLE_T_BASE] 17936c42ca1SJulius Werner b console_16550_core_putc 18036c42ca1SJulius Wernerendfunc console_16550_putc 1819c94d3b3SSoby Mathew 1829c94d3b3SSoby Mathew /* --------------------------------------------- 18336c42ca1SJulius Werner * int console_16550_core_getc(uintptr_t base_addr) 1849c94d3b3SSoby Mathew * Function to get a character from the console. 1859c94d3b3SSoby Mathew * It returns the character grabbed on success 18636c42ca1SJulius Werner * or -1 on if no character is available. 18736c42ca1SJulius Werner * In : x0 - console base address 18836c42ca1SJulius Werner * Out : w0 - character if available, else -1 1899c94d3b3SSoby Mathew * Clobber list : x0, x1 1909c94d3b3SSoby Mathew * --------------------------------------------- 1919c94d3b3SSoby Mathew */ 19236c42ca1SJulius Wernerfunc console_16550_core_getc 19336c42ca1SJulius Werner#if ENABLE_ASSERTIONS 19436c42ca1SJulius Werner cmp x0, #0 19536c42ca1SJulius Werner ASM_ASSERT(ne) 19636c42ca1SJulius Werner#endif /* ENABLE_ASSERTIONS */ 19736c42ca1SJulius Werner 1989c94d3b3SSoby Mathew /* Check if the receive FIFO is empty */ 1999c94d3b3SSoby Mathew1: ldr w1, [x0, #UARTLSR] 20036c42ca1SJulius Werner tbz w1, #UARTLSR_RDR_BIT, no_char 2019c94d3b3SSoby Mathew ldr w0, [x0, #UARTRX] 2029c94d3b3SSoby Mathew ret 20336c42ca1SJulius Wernerno_char: 20436c42ca1SJulius Werner mov w0, #ERROR_NO_PENDING_CHAR 2059c94d3b3SSoby Mathew ret 20636c42ca1SJulius Wernerendfunc console_16550_core_getc 20736c42ca1SJulius Werner 20836c42ca1SJulius Werner /* --------------------------------------------- 20998964f05SAndre Przywara * int console_16550_getc(console_t *console) 21036c42ca1SJulius Werner * Function to get a character from the console. 21136c42ca1SJulius Werner * It returns the character grabbed on success 21236c42ca1SJulius Werner * or -1 on if no character is available. 21336c42ca1SJulius Werner * In : x0 - pointer to console_t stucture 21436c42ca1SJulius Werner * Out : w0 - character if available, else -1 21536c42ca1SJulius Werner * Clobber list : x0, x1 21636c42ca1SJulius Werner * --------------------------------------------- 21736c42ca1SJulius Werner */ 21836c42ca1SJulius Wernerfunc console_16550_getc 21936c42ca1SJulius Werner#if ENABLE_ASSERTIONS 22036c42ca1SJulius Werner cmp x1, #0 22136c42ca1SJulius Werner ASM_ASSERT(ne) 22236c42ca1SJulius Werner#endif /* ENABLE_ASSERTIONS */ 22398964f05SAndre Przywara ldr x0, [x0, #CONSOLE_T_BASE] 22436c42ca1SJulius Werner b console_16550_core_getc 22536c42ca1SJulius Wernerendfunc console_16550_getc 226ad4c2ec6SAntonio Nino Diaz 227ad4c2ec6SAntonio Nino Diaz /* --------------------------------------------- 228831b0e98SJimmy Brisson * void console_16550_core_flush(uintptr_t base_addr) 229ad4c2ec6SAntonio Nino Diaz * Function to force a write of all buffered 230ad4c2ec6SAntonio Nino Diaz * data that hasn't been output. 231ad4c2ec6SAntonio Nino Diaz * In : x0 - console base address 232831b0e98SJimmy Brisson * Out : void. 233ad4c2ec6SAntonio Nino Diaz * Clobber list : x0, x1 234ad4c2ec6SAntonio Nino Diaz * --------------------------------------------- 235ad4c2ec6SAntonio Nino Diaz */ 2364f8053ddSAntonio Nino Diazfunc console_16550_core_flush 2374f8053ddSAntonio Nino Diaz#if ENABLE_ASSERTIONS 2384f8053ddSAntonio Nino Diaz cmp x0, #0 2394f8053ddSAntonio Nino Diaz ASM_ASSERT(ne) 2404f8053ddSAntonio Nino Diaz#endif /* ENABLE_ASSERTIONS */ 2414f8053ddSAntonio Nino Diaz 2424f8053ddSAntonio Nino Diaz /* Loop until the transmit FIFO is empty */ 2434f8053ddSAntonio Nino Diaz1: ldr w1, [x0, #UARTLSR] 2444f8053ddSAntonio Nino Diaz and w1, w1, #(UARTLSR_TEMT | UARTLSR_THRE) 2454f8053ddSAntonio Nino Diaz cmp w1, #(UARTLSR_TEMT | UARTLSR_THRE) 2464f8053ddSAntonio Nino Diaz b.ne 1b 2474f8053ddSAntonio Nino Diaz 248ad4c2ec6SAntonio Nino Diaz ret 2494f8053ddSAntonio Nino Diazendfunc console_16550_core_flush 2504f8053ddSAntonio Nino Diaz 2514f8053ddSAntonio Nino Diaz /* --------------------------------------------- 252831b0e98SJimmy Brisson * void console_16550_flush(console_t *console) 2534f8053ddSAntonio Nino Diaz * Function to force a write of all buffered 2544f8053ddSAntonio Nino Diaz * data that hasn't been output. 2554f8053ddSAntonio Nino Diaz * In : x0 - pointer to console_t structure 256831b0e98SJimmy Brisson * Out : void. 2574f8053ddSAntonio Nino Diaz * Clobber list : x0, x1 2584f8053ddSAntonio Nino Diaz * --------------------------------------------- 2594f8053ddSAntonio Nino Diaz */ 2604f8053ddSAntonio Nino Diazfunc console_16550_flush 2614f8053ddSAntonio Nino Diaz#if ENABLE_ASSERTIONS 2624f8053ddSAntonio Nino Diaz cmp x0, #0 2634f8053ddSAntonio Nino Diaz ASM_ASSERT(ne) 2644f8053ddSAntonio Nino Diaz#endif /* ENABLE_ASSERTIONS */ 26598964f05SAndre Przywara ldr x0, [x0, #CONSOLE_T_BASE] 2664f8053ddSAntonio Nino Diaz b console_16550_core_flush 2674f8053ddSAntonio Nino Diazendfunc console_16550_flush 268