19c94d3b3SSoby Mathew/* 251e24ec2SAmbroise Vincent * Copyright (c) 2013-2019, ARM Limited and Contributors. All rights reserved. 39c94d3b3SSoby Mathew * 482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause 59c94d3b3SSoby Mathew */ 69c94d3b3SSoby Mathew#include <arch.h> 79c94d3b3SSoby Mathew#include <asm_macros.S> 84a0c4571SJulius Werner#include <assert_macros.S> 93d2690d6SMichalis Pappas#include <console_macros.S> 1009d40e0eSAntonio Nino Diaz#include <drivers/arm/pl011.h> 119c94d3b3SSoby Mathew 124a0c4571SJulius Werner /* 134a0c4571SJulius Werner * "core" functions are low-level implementations that don't require 144a0c4571SJulius Werner * writable memory and are thus safe to call in BL1 crash context. 154a0c4571SJulius Werner */ 164a0c4571SJulius Werner .globl console_pl011_core_init 174a0c4571SJulius Werner .globl console_pl011_core_putc 184a0c4571SJulius Werner .globl console_pl011_core_getc 194a0c4571SJulius Werner .globl console_pl011_core_flush 209c94d3b3SSoby Mathew 214a0c4571SJulius Werner .globl console_pl011_putc 224a0c4571SJulius Werner .globl console_pl011_getc 234a0c4571SJulius Werner .globl console_pl011_flush 249c94d3b3SSoby Mathew 259c94d3b3SSoby Mathew /* ----------------------------------------------- 264a0c4571SJulius Werner * int console_pl011_core_init(uintptr_t base_addr, 279c94d3b3SSoby Mathew * unsigned int uart_clk, unsigned int baud_rate) 289c94d3b3SSoby Mathew * Function to initialize the console without a 299c94d3b3SSoby Mathew * C Runtime to print debug information. This 309c94d3b3SSoby Mathew * function will be accessed by console_init and 319c94d3b3SSoby Mathew * crash reporting. 329c94d3b3SSoby Mathew * In: x0 - console base address 339c94d3b3SSoby Mathew * w1 - Uart clock in Hz 349c94d3b3SSoby Mathew * w2 - Baud rate 359c94d3b3SSoby Mathew * Out: return 1 on success else 0 on error 369c94d3b3SSoby Mathew * Clobber list : x1, x2, x3, x4 379c94d3b3SSoby Mathew * ----------------------------------------------- 389c94d3b3SSoby Mathew */ 394a0c4571SJulius Wernerfunc console_pl011_core_init 409c94d3b3SSoby Mathew /* Check the input base address */ 419c94d3b3SSoby Mathew cbz x0, core_init_fail 429c94d3b3SSoby Mathew#if !PL011_GENERIC_UART 439c94d3b3SSoby Mathew /* Check baud rate and uart clock for sanity */ 449c94d3b3SSoby Mathew cbz w1, core_init_fail 459c94d3b3SSoby Mathew cbz w2, core_init_fail 469c94d3b3SSoby Mathew /* Disable uart before programming */ 479c94d3b3SSoby Mathew ldr w3, [x0, #UARTCR] 489c94d3b3SSoby Mathew mov w4, #PL011_UARTCR_UARTEN 499c94d3b3SSoby Mathew bic w3, w3, w4 509c94d3b3SSoby Mathew str w3, [x0, #UARTCR] 519c94d3b3SSoby Mathew /* Program the baudrate */ 529c94d3b3SSoby Mathew /* Divisor = (Uart clock * 4) / baudrate */ 539c94d3b3SSoby Mathew lsl w1, w1, #2 549c94d3b3SSoby Mathew udiv w2, w1, w2 559c94d3b3SSoby Mathew /* IBRD = Divisor >> 6 */ 569c94d3b3SSoby Mathew lsr w1, w2, #6 579c94d3b3SSoby Mathew /* Write the IBRD */ 589c94d3b3SSoby Mathew str w1, [x0, #UARTIBRD] 599c94d3b3SSoby Mathew /* FBRD = Divisor & 0x3F */ 609c94d3b3SSoby Mathew and w1, w2, #0x3f 619c94d3b3SSoby Mathew /* Write the FBRD */ 629c94d3b3SSoby Mathew str w1, [x0, #UARTFBRD] 639c94d3b3SSoby Mathew mov w1, #PL011_LINE_CONTROL 649c94d3b3SSoby Mathew str w1, [x0, #UARTLCR_H] 659c94d3b3SSoby Mathew /* Clear any pending errors */ 669c94d3b3SSoby Mathew str wzr, [x0, #UARTECR] 679c94d3b3SSoby Mathew /* Enable tx, rx, and uart overall */ 689c94d3b3SSoby Mathew mov w1, #(PL011_UARTCR_RXE | PL011_UARTCR_TXE | PL011_UARTCR_UARTEN) 699c94d3b3SSoby Mathew str w1, [x0, #UARTCR] 709c94d3b3SSoby Mathew#endif 719c94d3b3SSoby Mathew mov w0, #1 729c94d3b3SSoby Mathew ret 739c94d3b3SSoby Mathewcore_init_fail: 749c94d3b3SSoby Mathew mov w0, wzr 759c94d3b3SSoby Mathew ret 764a0c4571SJulius Wernerendfunc console_pl011_core_init 774a0c4571SJulius Werner 784a0c4571SJulius Werner .globl console_pl011_register 794a0c4571SJulius Werner 804a0c4571SJulius Werner /* ----------------------------------------------- 8165199dc8SAntonio Nino Diaz * int console_pl011_register(uintptr_t baseaddr, 8265199dc8SAntonio Nino Diaz * uint32_t clock, uint32_t baud, 83*f695e1e0SAndre Przywara * console_t *console); 844a0c4571SJulius Werner * Function to initialize and register a new PL011 854a0c4571SJulius Werner * console. Storage passed in for the console struct 864a0c4571SJulius Werner * *must* be persistent (i.e. not from the stack). 874a0c4571SJulius Werner * In: x0 - UART register base address 884a0c4571SJulius Werner * w1 - UART clock in Hz 894a0c4571SJulius Werner * w2 - Baud rate 90*f695e1e0SAndre Przywara * x3 - pointer to empty console_t struct 914a0c4571SJulius Werner * Out: return 1 on success, 0 on error 924a0c4571SJulius Werner * Clobber list : x0, x1, x2, x6, x7, x14 934a0c4571SJulius Werner * ----------------------------------------------- 944a0c4571SJulius Werner */ 954a0c4571SJulius Wernerfunc console_pl011_register 964a0c4571SJulius Werner mov x7, x30 974a0c4571SJulius Werner mov x6, x3 984a0c4571SJulius Werner cbz x6, register_fail 99*f695e1e0SAndre Przywara str x0, [x6, #CONSOLE_T_BASE] 1004a0c4571SJulius Werner 1014a0c4571SJulius Werner bl console_pl011_core_init 1024a0c4571SJulius Werner cbz x0, register_fail 1034a0c4571SJulius Werner 1044a0c4571SJulius Werner mov x0, x6 1054a0c4571SJulius Werner mov x30, x7 106cc5859caSSoby Mathew finish_console_register pl011 putc=1, getc=1, flush=1 1074a0c4571SJulius Werner 1084a0c4571SJulius Wernerregister_fail: 1094a0c4571SJulius Werner ret x7 1104a0c4571SJulius Wernerendfunc console_pl011_register 1119c94d3b3SSoby Mathew 1129c94d3b3SSoby Mathew /* -------------------------------------------------------- 1134a0c4571SJulius Werner * int console_pl011_core_putc(int c, uintptr_t base_addr) 1149c94d3b3SSoby Mathew * Function to output a character over the console. It 1159c94d3b3SSoby Mathew * returns the character printed on success or -1 on error. 1169c94d3b3SSoby Mathew * In : w0 - character to be printed 1179c94d3b3SSoby Mathew * x1 - console base address 1189c94d3b3SSoby Mathew * Out : return -1 on error else return character. 1199c94d3b3SSoby Mathew * Clobber list : x2 1209c94d3b3SSoby Mathew * -------------------------------------------------------- 1219c94d3b3SSoby Mathew */ 1224a0c4571SJulius Wernerfunc console_pl011_core_putc 1234a0c4571SJulius Werner#if ENABLE_ASSERTIONS 1244a0c4571SJulius Werner cmp x1, #0 1254a0c4571SJulius Werner ASM_ASSERT(ne) 1264a0c4571SJulius Werner#endif /* ENABLE_ASSERTIONS */ 1274a0c4571SJulius Werner 1289c94d3b3SSoby Mathew /* Prepend '\r' to '\n' */ 1299c94d3b3SSoby Mathew cmp w0, #0xA 1309c94d3b3SSoby Mathew b.ne 2f 1319c94d3b3SSoby Mathew1: 1329c94d3b3SSoby Mathew /* Check if the transmit FIFO is full */ 1339c94d3b3SSoby Mathew ldr w2, [x1, #UARTFR] 1349c94d3b3SSoby Mathew tbnz w2, #PL011_UARTFR_TXFF_BIT, 1b 1359c94d3b3SSoby Mathew mov w2, #0xD 1369c94d3b3SSoby Mathew str w2, [x1, #UARTDR] 1379c94d3b3SSoby Mathew2: 1389c94d3b3SSoby Mathew /* Check if the transmit FIFO is full */ 1399c94d3b3SSoby Mathew ldr w2, [x1, #UARTFR] 1409c94d3b3SSoby Mathew tbnz w2, #PL011_UARTFR_TXFF_BIT, 2b 1419c94d3b3SSoby Mathew str w0, [x1, #UARTDR] 1429c94d3b3SSoby Mathew ret 1434a0c4571SJulius Wernerendfunc console_pl011_core_putc 1444a0c4571SJulius Werner 1454a0c4571SJulius Werner /* -------------------------------------------------------- 146*f695e1e0SAndre Przywara * int console_pl011_putc(int c, console_t *console) 1474a0c4571SJulius Werner * Function to output a character over the console. It 1484a0c4571SJulius Werner * returns the character printed on success or -1 on error. 1494a0c4571SJulius Werner * In : w0 - character to be printed 1504a0c4571SJulius Werner * x1 - pointer to console_t structure 1514a0c4571SJulius Werner * Out : return -1 on error else return character. 1524a0c4571SJulius Werner * Clobber list : x2 1534a0c4571SJulius Werner * -------------------------------------------------------- 1544a0c4571SJulius Werner */ 1554a0c4571SJulius Wernerfunc console_pl011_putc 1564a0c4571SJulius Werner#if ENABLE_ASSERTIONS 1574a0c4571SJulius Werner cmp x1, #0 1584a0c4571SJulius Werner ASM_ASSERT(ne) 1594a0c4571SJulius Werner#endif /* ENABLE_ASSERTIONS */ 160*f695e1e0SAndre Przywara ldr x1, [x1, #CONSOLE_T_BASE] 1614a0c4571SJulius Werner b console_pl011_core_putc 1624a0c4571SJulius Wernerendfunc console_pl011_putc 1639c94d3b3SSoby Mathew 1649c94d3b3SSoby Mathew /* --------------------------------------------- 1654a0c4571SJulius Werner * int console_pl011_core_getc(uintptr_t base_addr) 1669c94d3b3SSoby Mathew * Function to get a character from the console. 1679c94d3b3SSoby Mathew * It returns the character grabbed on success 1684a0c4571SJulius Werner * or -1 if no character is available. 1699c94d3b3SSoby Mathew * In : x0 - console base address 1704a0c4571SJulius Werner * Out: w0 - character if available, else -1 1719c94d3b3SSoby Mathew * Clobber list : x0, x1 1729c94d3b3SSoby Mathew * --------------------------------------------- 1739c94d3b3SSoby Mathew */ 1744a0c4571SJulius Wernerfunc console_pl011_core_getc 1754a0c4571SJulius Werner#if ENABLE_ASSERTIONS 1764a0c4571SJulius Werner cmp x0, #0 1774a0c4571SJulius Werner ASM_ASSERT(ne) 1784a0c4571SJulius Werner#endif /* ENABLE_ASSERTIONS */ 1794a0c4571SJulius Werner 1809c94d3b3SSoby Mathew /* Check if the receive FIFO is empty */ 1819c94d3b3SSoby Mathew ldr w1, [x0, #UARTFR] 1824a0c4571SJulius Werner tbnz w1, #PL011_UARTFR_RXFE_BIT, no_char 1839c94d3b3SSoby Mathew ldr w1, [x0, #UARTDR] 1849c94d3b3SSoby Mathew mov w0, w1 1859c94d3b3SSoby Mathew ret 1864a0c4571SJulius Wernerno_char: 1874a0c4571SJulius Werner mov w0, #ERROR_NO_PENDING_CHAR 1889c94d3b3SSoby Mathew ret 1894a0c4571SJulius Wernerendfunc console_pl011_core_getc 19073e05284SAntonio Nino Diaz 19173e05284SAntonio Nino Diaz /* --------------------------------------------- 192*f695e1e0SAndre Przywara * int console_pl011_getc(console_t *console) 1934a0c4571SJulius Werner * Function to get a character from the console. 1944a0c4571SJulius Werner * It returns the character grabbed on success 1954a0c4571SJulius Werner * or -1 if no character is available. 1964a0c4571SJulius Werner * In : x0 - pointer to console_t structure 1974a0c4571SJulius Werner * Out: w0 - character if available, else -1 1984a0c4571SJulius Werner * Clobber list : x0, x1 1994a0c4571SJulius Werner * --------------------------------------------- 2004a0c4571SJulius Werner */ 2014a0c4571SJulius Wernerfunc console_pl011_getc 2024a0c4571SJulius Werner#if ENABLE_ASSERTIONS 2034a0c4571SJulius Werner cmp x0, #0 2044a0c4571SJulius Werner ASM_ASSERT(ne) 2054a0c4571SJulius Werner#endif /* ENABLE_ASSERTIONS */ 206*f695e1e0SAndre Przywara ldr x0, [x0, #CONSOLE_T_BASE] 2074a0c4571SJulius Werner b console_pl011_core_getc 2084a0c4571SJulius Wernerendfunc console_pl011_getc 2094a0c4571SJulius Werner 2104a0c4571SJulius Werner /* --------------------------------------------- 2114a0c4571SJulius Werner * int console_pl011_core_flush(uintptr_t base_addr) 21273e05284SAntonio Nino Diaz * Function to force a write of all buffered 21373e05284SAntonio Nino Diaz * data that hasn't been output. 21473e05284SAntonio Nino Diaz * In : x0 - console base address 21573e05284SAntonio Nino Diaz * Out : return -1 on error else return 0. 21673e05284SAntonio Nino Diaz * Clobber list : x0, x1 21773e05284SAntonio Nino Diaz * --------------------------------------------- 21873e05284SAntonio Nino Diaz */ 2194a0c4571SJulius Wernerfunc console_pl011_core_flush 2204a0c4571SJulius Werner#if ENABLE_ASSERTIONS 2214a0c4571SJulius Werner cmp x0, #0 2224a0c4571SJulius Werner ASM_ASSERT(ne) 2234a0c4571SJulius Werner#endif /* ENABLE_ASSERTIONS */ 22473e05284SAntonio Nino Diaz1: 22573e05284SAntonio Nino Diaz /* Loop until the transmit FIFO is empty */ 22673e05284SAntonio Nino Diaz ldr w1, [x0, #UARTFR] 22773e05284SAntonio Nino Diaz tbnz w1, #PL011_UARTFR_BUSY_BIT, 1b 22873e05284SAntonio Nino Diaz 22973e05284SAntonio Nino Diaz mov w0, #0 23073e05284SAntonio Nino Diaz ret 2314a0c4571SJulius Wernerendfunc console_pl011_core_flush 2324a0c4571SJulius Werner 2334a0c4571SJulius Werner /* --------------------------------------------- 234*f695e1e0SAndre Przywara * int console_pl011_flush(console_t *console) 2354a0c4571SJulius Werner * Function to force a write of all buffered 2364a0c4571SJulius Werner * data that hasn't been output. 2374a0c4571SJulius Werner * In : x0 - pointer to console_t structure 2384a0c4571SJulius Werner * Out : return -1 on error else return 0. 2394a0c4571SJulius Werner * Clobber list : x0, x1 2404a0c4571SJulius Werner * --------------------------------------------- 2414a0c4571SJulius Werner */ 2424a0c4571SJulius Wernerfunc console_pl011_flush 2434a0c4571SJulius Werner#if ENABLE_ASSERTIONS 2444a0c4571SJulius Werner cmp x0, #0 2454a0c4571SJulius Werner ASM_ASSERT(ne) 2464a0c4571SJulius Werner#endif /* ENABLE_ASSERTIONS */ 247*f695e1e0SAndre Przywara ldr x0, [x0, #CONSOLE_T_BASE] 2484a0c4571SJulius Werner b console_pl011_core_flush 2494a0c4571SJulius Wernerendfunc console_pl011_flush 250