Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 1 | /* |
Antonio Nino Diaz | 56ec1d5 | 2017-02-08 15:58:12 +0000 | [diff] [blame] | 2 | * Copyright (c) 2015-2017, ARM Limited and Contributors. All rights reserved. |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 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 | |
| 31 | #include <arch.h> |
| 32 | #include <asm_macros.S> |
| 33 | #include <uart_16550.h> |
| 34 | |
| 35 | .globl console_core_init |
| 36 | .globl console_core_putc |
| 37 | .globl console_core_getc |
Antonio Nino Diaz | 56ec1d5 | 2017-02-08 15:58:12 +0000 | [diff] [blame] | 38 | .globl console_core_flush |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 39 | |
| 40 | /* ----------------------------------------------- |
| 41 | * int console_core_init(unsigned long base_addr, |
| 42 | * unsigned int uart_clk, unsigned int baud_rate) |
| 43 | * Function to initialize the console without a |
| 44 | * C Runtime to print debug information. This |
| 45 | * function will be accessed by console_init and |
| 46 | * crash reporting. |
| 47 | * In: x0 - console base address |
| 48 | * w1 - Uart clock in Hz |
| 49 | * w2 - Baud rate |
| 50 | * Out: return 1 on success |
| 51 | * Clobber list : x1, x2, x3 |
| 52 | * ----------------------------------------------- |
| 53 | */ |
| 54 | func console_core_init |
| 55 | /* Check the input base address */ |
| 56 | cbz x0, init_fail |
| 57 | /* Check baud rate and uart clock for sanity */ |
| 58 | cbz w1, init_fail |
| 59 | cbz w2, init_fail |
| 60 | |
| 61 | /* Program the baudrate */ |
| 62 | /* Divisor = Uart clock / (16 * baudrate) */ |
| 63 | lsl w2, w2, #4 |
| 64 | udiv w2, w1, w2 |
| 65 | and w1, w2, #0xff /* w1 = DLL */ |
| 66 | lsr w2, w2, #8 |
| 67 | and w2, w2, #0xff /* w2 = DLLM */ |
| 68 | ldr w3, [x0, #UARTLCR] |
| 69 | orr w3, w3, #UARTLCR_DLAB |
| 70 | str w3, [x0, #UARTLCR] /* enable DLL, DLLM programming */ |
| 71 | str w1, [x0, #UARTDLL] /* program DLL */ |
| 72 | str w2, [x0, #UARTDLLM] /* program DLLM */ |
| 73 | mov w2, #~UARTLCR_DLAB |
| 74 | and w3, w3, w2 |
| 75 | str w3, [x0, #UARTLCR] /* disable DLL, DLLM programming */ |
| 76 | |
| 77 | /* 8n1 */ |
| 78 | mov w3, #3 |
| 79 | str w3, [x0, #UARTLCR] |
| 80 | /* no interrupt */ |
| 81 | mov w3, #0 |
| 82 | str w3, [x0, #UARTIER] |
| 83 | /* enable fifo, DMA */ |
| 84 | mov w3, #(UARTFCR_FIFOEN | UARTFCR_DMAEN) |
| 85 | str w3, [x0, #UARTFCR] |
| 86 | /* DTR + RTS */ |
| 87 | mov w3, #3 |
| 88 | str w3, [x0, #UARTMCR] |
| 89 | mov w0, #1 |
| 90 | init_fail: |
| 91 | ret |
| 92 | endfunc console_core_init |
| 93 | |
| 94 | /* -------------------------------------------------------- |
| 95 | * int console_core_putc(int c, unsigned int base_addr) |
| 96 | * Function to output a character over the console. It |
| 97 | * returns the character printed on success or -1 on error. |
| 98 | * In : w0 - character to be printed |
| 99 | * x1 - console base address |
| 100 | * Out : return -1 on error else return character. |
| 101 | * Clobber list : x2 |
| 102 | * -------------------------------------------------------- |
| 103 | */ |
| 104 | func console_core_putc |
| 105 | /* Check the input parameter */ |
| 106 | cbz x1, putc_error |
| 107 | |
| 108 | /* Prepend '\r' to '\n' */ |
| 109 | cmp w0, #0xA |
| 110 | b.ne 2f |
| 111 | /* Check if the transmit FIFO is full */ |
| 112 | 1: ldr w2, [x1, #UARTLSR] |
| 113 | and w2, w2, #(UARTLSR_TEMT | UARTLSR_THRE) |
| 114 | cmp w2, #(UARTLSR_TEMT | UARTLSR_THRE) |
| 115 | b.ne 1b |
| 116 | mov w2, #0xD /* '\r' */ |
| 117 | str w2, [x1, #UARTTX] |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 118 | |
| 119 | /* Check if the transmit FIFO is full */ |
| 120 | 2: ldr w2, [x1, #UARTLSR] |
| 121 | and w2, w2, #(UARTLSR_TEMT | UARTLSR_THRE) |
| 122 | cmp w2, #(UARTLSR_TEMT | UARTLSR_THRE) |
| 123 | b.ne 2b |
| 124 | str w0, [x1, #UARTTX] |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 125 | ret |
| 126 | putc_error: |
| 127 | mov w0, #-1 |
| 128 | ret |
| 129 | endfunc console_core_putc |
| 130 | |
| 131 | /* --------------------------------------------- |
| 132 | * int console_core_getc(void) |
| 133 | * Function to get a character from the console. |
| 134 | * It returns the character grabbed on success |
| 135 | * or -1 on error. |
| 136 | * In : w0 - console base address |
| 137 | * Out : return -1 on error else return character. |
| 138 | * Clobber list : x0, x1 |
| 139 | * --------------------------------------------- |
| 140 | */ |
| 141 | func console_core_getc |
| 142 | /* Check if the receive FIFO is empty */ |
| 143 | 1: ldr w1, [x0, #UARTLSR] |
Nishanth Menon | 7b4b513 | 2017-01-10 09:34:07 -0600 | [diff] [blame] | 144 | tbz w1, #UARTLSR_RDR_BIT, 1b |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 145 | ldr w0, [x0, #UARTRX] |
| 146 | ret |
| 147 | getc_error: |
| 148 | mov w0, #-1 |
| 149 | ret |
| 150 | endfunc console_core_getc |
Antonio Nino Diaz | 56ec1d5 | 2017-02-08 15:58:12 +0000 | [diff] [blame] | 151 | |
| 152 | /* --------------------------------------------- |
| 153 | * int console_core_flush(uintptr_t base_addr) |
| 154 | * Function to force a write of all buffered |
| 155 | * data that hasn't been output. |
| 156 | * In : x0 - console base address |
| 157 | * Out : return -1 on error else return 0. |
| 158 | * Clobber list : x0, x1 |
| 159 | * --------------------------------------------- |
| 160 | */ |
| 161 | func console_core_flush |
| 162 | /* Placeholder */ |
| 163 | mov w0, #0 |
| 164 | ret |
| 165 | endfunc console_core_flush |