Soby Mathew | c389d77 | 2014-06-24 12:28:41 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013-2014, 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 | .globl console_init |
| 35 | .globl console_putc |
| 36 | .globl console_core_init |
| 37 | .globl console_core_putc |
| 38 | .globl console_getc |
| 39 | |
| 40 | /* |
| 41 | * The console base is in the data section and not in .bss |
| 42 | * even though it is zero-init. In particular, this allows |
| 43 | * the console functions to start using this variable before |
| 44 | * the runtime memory is initialized for images which do not |
| 45 | * need to copy the .data section from ROM to RAM. |
| 46 | */ |
| 47 | .section .data.console_base ; .align 3 |
| 48 | console_base: .quad 0x0 |
| 49 | |
Soby Mathew | 69817f7 | 2014-07-14 15:43:21 +0100 | [diff] [blame] | 50 | /* ----------------------------------------------- |
| 51 | * int console_init(unsigned long base_addr, |
| 52 | * unsigned int uart_clk, unsigned int baud_rate) |
Soby Mathew | c389d77 | 2014-06-24 12:28:41 +0100 | [diff] [blame] | 53 | * Function to initialize the console without a |
| 54 | * C Runtime to print debug information. It saves |
| 55 | * the console base to the data section. |
| 56 | * In: x0 - console base address |
Soby Mathew | 69817f7 | 2014-07-14 15:43:21 +0100 | [diff] [blame] | 57 | * w1 - Uart clock in Hz |
| 58 | * w2 - Baud rate |
Soby Mathew | c389d77 | 2014-06-24 12:28:41 +0100 | [diff] [blame] | 59 | * out: return 1 on success. |
Soby Mathew | 69817f7 | 2014-07-14 15:43:21 +0100 | [diff] [blame] | 60 | * Clobber list : x1 - x3 |
| 61 | * ----------------------------------------------- |
Soby Mathew | c389d77 | 2014-06-24 12:28:41 +0100 | [diff] [blame] | 62 | */ |
| 63 | func console_init |
Soby Mathew | 69817f7 | 2014-07-14 15:43:21 +0100 | [diff] [blame] | 64 | adrp x3, console_base |
| 65 | str x0, [x3, :lo12:console_base] |
Soby Mathew | c389d77 | 2014-06-24 12:28:41 +0100 | [diff] [blame] | 66 | b console_core_init |
Kévin Petit | a877c25 | 2015-03-24 14:03:57 +0000 | [diff] [blame] | 67 | endfunc console_init |
Soby Mathew | c389d77 | 2014-06-24 12:28:41 +0100 | [diff] [blame] | 68 | |
Soby Mathew | 69817f7 | 2014-07-14 15:43:21 +0100 | [diff] [blame] | 69 | /* ----------------------------------------------- |
| 70 | * int console_core_init(unsigned long base_addr, |
| 71 | * unsigned int uart_clk, unsigned int baud_rate) |
Soby Mathew | c389d77 | 2014-06-24 12:28:41 +0100 | [diff] [blame] | 72 | * Function to initialize the console without a |
| 73 | * C Runtime to print debug information. This |
| 74 | * function will be accessed by console_init and |
| 75 | * crash reporting. |
| 76 | * In: x0 - console base address |
Soby Mathew | 69817f7 | 2014-07-14 15:43:21 +0100 | [diff] [blame] | 77 | * w1 - Uart clock in Hz |
| 78 | * w2 - Baud rate |
Soby Mathew | c389d77 | 2014-06-24 12:28:41 +0100 | [diff] [blame] | 79 | * Out: return 1 on success |
| 80 | * Clobber list : x1, x2 |
Soby Mathew | 69817f7 | 2014-07-14 15:43:21 +0100 | [diff] [blame] | 81 | * ----------------------------------------------- |
Soby Mathew | c389d77 | 2014-06-24 12:28:41 +0100 | [diff] [blame] | 82 | */ |
| 83 | func console_core_init |
| 84 | /* Check the input base address */ |
| 85 | cbz x0, init_fail |
Soby Mathew | 69817f7 | 2014-07-14 15:43:21 +0100 | [diff] [blame] | 86 | /* Check baud rate and uart clock for sanity */ |
| 87 | cbz w1, init_fail |
| 88 | cbz w2, init_fail |
Soby Mathew | c389d77 | 2014-06-24 12:28:41 +0100 | [diff] [blame] | 89 | /* Program the baudrate */ |
Soby Mathew | 69817f7 | 2014-07-14 15:43:21 +0100 | [diff] [blame] | 90 | /* Divisor = (Uart clock * 4) / baudrate */ |
| 91 | lsl w1, w1, #2 |
| 92 | udiv w2, w1, w2 |
| 93 | /* IBRD = Divisor >> 6 */ |
| 94 | lsr w1, w2, #6 |
Soby Mathew | c389d77 | 2014-06-24 12:28:41 +0100 | [diff] [blame] | 95 | /* Write the IBRD */ |
Soby Mathew | c389d77 | 2014-06-24 12:28:41 +0100 | [diff] [blame] | 96 | str w1, [x0, #UARTIBRD] |
Soby Mathew | 69817f7 | 2014-07-14 15:43:21 +0100 | [diff] [blame] | 97 | /* FBRD = Divisor & 0x3F */ |
| 98 | and w1, w2, #0x3f |
Soby Mathew | c389d77 | 2014-06-24 12:28:41 +0100 | [diff] [blame] | 99 | /* Write the FBRD */ |
Soby Mathew | c389d77 | 2014-06-24 12:28:41 +0100 | [diff] [blame] | 100 | str w1, [x0, #UARTFBRD] |
Soby Mathew | c389d77 | 2014-06-24 12:28:41 +0100 | [diff] [blame] | 101 | mov w1, #PL011_LINE_CONTROL |
| 102 | str w1, [x0, #UARTLCR_H] |
| 103 | /* Clear any pending errors */ |
| 104 | str wzr, [x0, #UARTECR] |
| 105 | /* Enable tx, rx, and uart overall */ |
| 106 | mov w1, #(PL011_UARTCR_RXE | PL011_UARTCR_TXE | PL011_UARTCR_UARTEN) |
| 107 | str w1, [x0, #UARTCR] |
| 108 | mov w0, #1 |
| 109 | init_fail: |
| 110 | ret |
Kévin Petit | a877c25 | 2015-03-24 14:03:57 +0000 | [diff] [blame] | 111 | endfunc console_core_init |
Soby Mathew | c389d77 | 2014-06-24 12:28:41 +0100 | [diff] [blame] | 112 | |
| 113 | /* --------------------------------------------- |
| 114 | * int console_putc(int c) |
| 115 | * Function to output a character over the |
| 116 | * console. It returns the character printed on |
| 117 | * success or -1 on error. |
| 118 | * In : x0 - character to be printed |
| 119 | * Out : return -1 on error else return character. |
| 120 | * Clobber list : x1, x2 |
| 121 | * --------------------------------------------- |
| 122 | */ |
| 123 | func console_putc |
| 124 | adrp x2, console_base |
| 125 | ldr x1, [x2, :lo12:console_base] |
| 126 | b console_core_putc |
Kévin Petit | a877c25 | 2015-03-24 14:03:57 +0000 | [diff] [blame] | 127 | endfunc console_putc |
Soby Mathew | c389d77 | 2014-06-24 12:28:41 +0100 | [diff] [blame] | 128 | |
| 129 | /* -------------------------------------------------------- |
Soby Mathew | 69817f7 | 2014-07-14 15:43:21 +0100 | [diff] [blame] | 130 | * int console_core_putc(int c, unsigned int base_addr) |
Soby Mathew | c389d77 | 2014-06-24 12:28:41 +0100 | [diff] [blame] | 131 | * Function to output a character over the console. It |
| 132 | * returns the character printed on success or -1 on error. |
Soby Mathew | 69817f7 | 2014-07-14 15:43:21 +0100 | [diff] [blame] | 133 | * In : w0 - character to be printed |
Soby Mathew | c389d77 | 2014-06-24 12:28:41 +0100 | [diff] [blame] | 134 | * x1 - console base address |
| 135 | * Out : return -1 on error else return character. |
| 136 | * Clobber list : x2 |
| 137 | * -------------------------------------------------------- |
| 138 | */ |
| 139 | func console_core_putc |
| 140 | /* Check the input parameter */ |
| 141 | cbz x1, putc_error |
| 142 | /* Prepend '\r' to '\n' */ |
Soby Mathew | 69817f7 | 2014-07-14 15:43:21 +0100 | [diff] [blame] | 143 | cmp w0, #0xA |
Soby Mathew | c389d77 | 2014-06-24 12:28:41 +0100 | [diff] [blame] | 144 | b.ne 2f |
| 145 | 1: |
| 146 | /* Check if the transmit FIFO is full */ |
| 147 | ldr w2, [x1, #UARTFR] |
| 148 | tbnz w2, #PL011_UARTFR_TXFF_BIT, 1b |
| 149 | mov w2, #0xD |
| 150 | str w2, [x1, #UARTDR] |
| 151 | 2: |
| 152 | /* Check if the transmit FIFO is full */ |
| 153 | ldr w2, [x1, #UARTFR] |
| 154 | tbnz w2, #PL011_UARTFR_TXFF_BIT, 2b |
| 155 | str w0, [x1, #UARTDR] |
| 156 | ret |
| 157 | putc_error: |
| 158 | mov w0, #-1 |
| 159 | ret |
Kévin Petit | a877c25 | 2015-03-24 14:03:57 +0000 | [diff] [blame] | 160 | endfunc console_core_putc |
Soby Mathew | c389d77 | 2014-06-24 12:28:41 +0100 | [diff] [blame] | 161 | |
| 162 | /* --------------------------------------------- |
| 163 | * int console_getc(void) |
| 164 | * Function to get a character from the console. |
| 165 | * It returns the character grabbed on success |
| 166 | * or -1 on error. |
| 167 | * Clobber list : x0, x1 |
| 168 | * --------------------------------------------- |
| 169 | */ |
| 170 | func console_getc |
| 171 | adrp x0, console_base |
| 172 | ldr x1, [x0, :lo12:console_base] |
| 173 | cbz x1, getc_error |
| 174 | 1: |
| 175 | /* Check if the receive FIFO is empty */ |
| 176 | ldr w0, [x1, #UARTFR] |
| 177 | tbnz w0, #PL011_UARTFR_RXFE_BIT, 1b |
| 178 | ldr w0, [x1, #UARTDR] |
| 179 | ret |
| 180 | getc_error: |
| 181 | mov w0, #-1 |
| 182 | ret |
Kévin Petit | a877c25 | 2015-03-24 14:03:57 +0000 | [diff] [blame] | 183 | endfunc console_getc |