1/* 2 * Copyright (c) 2013-2017, ARM Limited and Contributors. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are met: 6 * 7 * Redistributions of source code must retain the above copyright notice, this 8 * list of conditions and the following disclaimer. 9 * 10 * Redistributions in binary form must reproduce the above copyright notice, 11 * this list of conditions and the following disclaimer in the documentation 12 * and/or other materials provided with the distribution. 13 * 14 * Neither the name of ARM nor the names of its contributors may be used 15 * to endorse or promote products derived from this software without specific 16 * prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 * POSSIBILITY OF SUCH DAMAGE. 29 */ 30#include <arch.h> 31#include <asm_macros.S> 32#include <pl011.h> 33 34/* 35 * Pull in generic functions to provide backwards compatibility for 36 * platform makefiles 37 */ 38#include "../../../console/aarch64/console.S" 39 40 41 .globl console_core_init 42 .globl console_core_putc 43 .globl console_core_getc 44 .globl console_core_flush 45 46 47 /* ----------------------------------------------- 48 * int console_core_init(uintptr_t base_addr, 49 * unsigned int uart_clk, unsigned int baud_rate) 50 * Function to initialize the console without a 51 * C Runtime to print debug information. This 52 * function will be accessed by console_init and 53 * crash reporting. 54 * In: x0 - console base address 55 * w1 - Uart clock in Hz 56 * w2 - Baud rate 57 * Out: return 1 on success else 0 on error 58 * Clobber list : x1, x2, x3, x4 59 * ----------------------------------------------- 60 */ 61func console_core_init 62 /* Check the input base address */ 63 cbz x0, core_init_fail 64#if !PL011_GENERIC_UART 65 /* Check baud rate and uart clock for sanity */ 66 cbz w1, core_init_fail 67 cbz w2, core_init_fail 68 /* Disable uart before programming */ 69 ldr w3, [x0, #UARTCR] 70 mov w4, #PL011_UARTCR_UARTEN 71 bic w3, w3, w4 72 str w3, [x0, #UARTCR] 73 /* Program the baudrate */ 74 /* Divisor = (Uart clock * 4) / baudrate */ 75 lsl w1, w1, #2 76 udiv w2, w1, w2 77 /* IBRD = Divisor >> 6 */ 78 lsr w1, w2, #6 79 /* Write the IBRD */ 80 str w1, [x0, #UARTIBRD] 81 /* FBRD = Divisor & 0x3F */ 82 and w1, w2, #0x3f 83 /* Write the FBRD */ 84 str w1, [x0, #UARTFBRD] 85 mov w1, #PL011_LINE_CONTROL 86 str w1, [x0, #UARTLCR_H] 87 /* Clear any pending errors */ 88 str wzr, [x0, #UARTECR] 89 /* Enable tx, rx, and uart overall */ 90 mov w1, #(PL011_UARTCR_RXE | PL011_UARTCR_TXE | PL011_UARTCR_UARTEN) 91 str w1, [x0, #UARTCR] 92#endif 93 mov w0, #1 94 ret 95core_init_fail: 96 mov w0, wzr 97 ret 98endfunc console_core_init 99 100 /* -------------------------------------------------------- 101 * int console_core_putc(int c, uintptr_t base_addr) 102 * Function to output a character over the console. It 103 * returns the character printed on success or -1 on error. 104 * In : w0 - character to be printed 105 * x1 - console base address 106 * Out : return -1 on error else return character. 107 * Clobber list : x2 108 * -------------------------------------------------------- 109 */ 110func console_core_putc 111 /* Check the input parameter */ 112 cbz x1, putc_error 113 /* Prepend '\r' to '\n' */ 114 cmp w0, #0xA 115 b.ne 2f 1161: 117 /* Check if the transmit FIFO is full */ 118 ldr w2, [x1, #UARTFR] 119 tbnz w2, #PL011_UARTFR_TXFF_BIT, 1b 120 mov w2, #0xD 121 str w2, [x1, #UARTDR] 1222: 123 /* Check if the transmit FIFO is full */ 124 ldr w2, [x1, #UARTFR] 125 tbnz w2, #PL011_UARTFR_TXFF_BIT, 2b 126 str w0, [x1, #UARTDR] 127 ret 128putc_error: 129 mov w0, #-1 130 ret 131endfunc console_core_putc 132 133 /* --------------------------------------------- 134 * int console_core_getc(uintptr_t base_addr) 135 * Function to get a character from the console. 136 * It returns the character grabbed on success 137 * or -1 on error. 138 * In : x0 - console base address 139 * Clobber list : x0, x1 140 * --------------------------------------------- 141 */ 142func console_core_getc 143 cbz x0, getc_error 1441: 145 /* Check if the receive FIFO is empty */ 146 ldr w1, [x0, #UARTFR] 147 tbnz w1, #PL011_UARTFR_RXFE_BIT, 1b 148 ldr w1, [x0, #UARTDR] 149 mov w0, w1 150 ret 151getc_error: 152 mov w0, #-1 153 ret 154endfunc console_core_getc 155 156 /* --------------------------------------------- 157 * int console_core_flush(uintptr_t base_addr) 158 * Function to force a write of all buffered 159 * data that hasn't been output. 160 * In : x0 - console base address 161 * Out : return -1 on error else return 0. 162 * Clobber list : x0, x1 163 * --------------------------------------------- 164 */ 165func console_core_flush 166 cbz x0, flush_error 167 1681: 169 /* Loop until the transmit FIFO is empty */ 170 ldr w1, [x0, #UARTFR] 171 tbnz w1, #PL011_UARTFR_BUSY_BIT, 1b 172 173 mov w0, #0 174 ret 175flush_error: 176 mov w0, #-1 177 ret 178endfunc console_core_flush 179