Soby Mathew | c389d77 | 2014-06-24 12:28:41 +0100 | [diff] [blame] | 1 | /* |
Dan Handley | c863fa4 | 2015-04-01 16:51:20 +0100 | [diff] [blame] | 2 | * Copyright (c) 2013-2015, ARM Limited and Contributors. All rights reserved. |
Soby Mathew | c389d77 | 2014-06-24 12:28:41 +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 | #include <arch.h> |
| 31 | #include <asm_macros.S> |
| 32 | #include <pl011.h> |
| 33 | |
Dan Handley | c863fa4 | 2015-04-01 16:51:20 +0100 | [diff] [blame] | 34 | /* |
| 35 | * Pull in generic functions to provide backwards compatibility for |
| 36 | * platform makefiles |
| 37 | */ |
| 38 | #include "../../console/console.S" |
| 39 | |
| 40 | |
Soby Mathew | c389d77 | 2014-06-24 12:28:41 +0100 | [diff] [blame] | 41 | .globl console_core_init |
| 42 | .globl console_core_putc |
Dan Handley | c863fa4 | 2015-04-01 16:51:20 +0100 | [diff] [blame] | 43 | .globl console_core_getc |
Soby Mathew | c389d77 | 2014-06-24 12:28:41 +0100 | [diff] [blame] | 44 | |
Soby Mathew | c389d77 | 2014-06-24 12:28:41 +0100 | [diff] [blame] | 45 | |
Soby Mathew | 69817f7 | 2014-07-14 15:43:21 +0100 | [diff] [blame] | 46 | /* ----------------------------------------------- |
Juan Castillo | 7f1f062 | 2014-09-09 09:49:23 +0100 | [diff] [blame] | 47 | * int console_core_init(uintptr_t base_addr, |
Soby Mathew | 69817f7 | 2014-07-14 15:43:21 +0100 | [diff] [blame] | 48 | * unsigned int uart_clk, unsigned int baud_rate) |
Soby Mathew | c389d77 | 2014-06-24 12:28:41 +0100 | [diff] [blame] | 49 | * Function to initialize the console without a |
| 50 | * C Runtime to print debug information. This |
| 51 | * function will be accessed by console_init and |
| 52 | * crash reporting. |
| 53 | * In: x0 - console base address |
Soby Mathew | 69817f7 | 2014-07-14 15:43:21 +0100 | [diff] [blame] | 54 | * w1 - Uart clock in Hz |
| 55 | * w2 - Baud rate |
Dan Handley | c863fa4 | 2015-04-01 16:51:20 +0100 | [diff] [blame] | 56 | * Out: return 1 on success else 0 on error |
Juan Castillo | e7ae6db | 2015-11-26 14:52:15 +0000 | [diff] [blame] | 57 | * Clobber list : x1, x2, x3, x4 |
Soby Mathew | 69817f7 | 2014-07-14 15:43:21 +0100 | [diff] [blame] | 58 | * ----------------------------------------------- |
Soby Mathew | c389d77 | 2014-06-24 12:28:41 +0100 | [diff] [blame] | 59 | */ |
| 60 | func console_core_init |
| 61 | /* Check the input base address */ |
Dan Handley | c863fa4 | 2015-04-01 16:51:20 +0100 | [diff] [blame] | 62 | cbz x0, core_init_fail |
Juan Castillo | 7ac4b11 | 2015-11-16 16:53:38 +0000 | [diff] [blame] | 63 | #if !PL011_GENERIC_UART |
Soby Mathew | 69817f7 | 2014-07-14 15:43:21 +0100 | [diff] [blame] | 64 | /* Check baud rate and uart clock for sanity */ |
Dan Handley | c863fa4 | 2015-04-01 16:51:20 +0100 | [diff] [blame] | 65 | cbz w1, core_init_fail |
| 66 | cbz w2, core_init_fail |
Juan Castillo | e7ae6db | 2015-11-26 14:52:15 +0000 | [diff] [blame] | 67 | /* Disable uart before programming */ |
| 68 | ldr w3, [x0, #UARTCR] |
| 69 | mov w4, #PL011_UARTCR_UARTEN |
| 70 | bic w3, w3, w4 |
| 71 | str w3, [x0, #UARTCR] |
| 72 | /* Flush the transmit FIFO */ |
| 73 | ldr w3, [x0, #UARTLCR_H] |
| 74 | mov w4, #PL011_UARTLCR_H_FEN |
| 75 | bic w3, w3, w4 |
| 76 | str w3, [x0, #UARTLCR_H] |
| 77 | /* Wait for the end of Tx of current character */ |
| 78 | busy_loop: |
| 79 | ldr w3, [x0, #UARTFR] |
| 80 | tbnz w3, #PL011_UARTFR_BUSY_BIT, busy_loop |
Soby Mathew | c389d77 | 2014-06-24 12:28:41 +0100 | [diff] [blame] | 81 | /* Program the baudrate */ |
Soby Mathew | 69817f7 | 2014-07-14 15:43:21 +0100 | [diff] [blame] | 82 | /* Divisor = (Uart clock * 4) / baudrate */ |
| 83 | lsl w1, w1, #2 |
| 84 | udiv w2, w1, w2 |
| 85 | /* IBRD = Divisor >> 6 */ |
| 86 | lsr w1, w2, #6 |
Soby Mathew | c389d77 | 2014-06-24 12:28:41 +0100 | [diff] [blame] | 87 | /* Write the IBRD */ |
Soby Mathew | c389d77 | 2014-06-24 12:28:41 +0100 | [diff] [blame] | 88 | str w1, [x0, #UARTIBRD] |
Soby Mathew | 69817f7 | 2014-07-14 15:43:21 +0100 | [diff] [blame] | 89 | /* FBRD = Divisor & 0x3F */ |
| 90 | and w1, w2, #0x3f |
Soby Mathew | c389d77 | 2014-06-24 12:28:41 +0100 | [diff] [blame] | 91 | /* Write the FBRD */ |
Soby Mathew | c389d77 | 2014-06-24 12:28:41 +0100 | [diff] [blame] | 92 | str w1, [x0, #UARTFBRD] |
Soby Mathew | c389d77 | 2014-06-24 12:28:41 +0100 | [diff] [blame] | 93 | mov w1, #PL011_LINE_CONTROL |
| 94 | str w1, [x0, #UARTLCR_H] |
| 95 | /* Clear any pending errors */ |
| 96 | str wzr, [x0, #UARTECR] |
| 97 | /* Enable tx, rx, and uart overall */ |
| 98 | mov w1, #(PL011_UARTCR_RXE | PL011_UARTCR_TXE | PL011_UARTCR_UARTEN) |
| 99 | str w1, [x0, #UARTCR] |
Juan Castillo | 7ac4b11 | 2015-11-16 16:53:38 +0000 | [diff] [blame] | 100 | #endif |
Soby Mathew | c389d77 | 2014-06-24 12:28:41 +0100 | [diff] [blame] | 101 | mov w0, #1 |
Dan Handley | c863fa4 | 2015-04-01 16:51:20 +0100 | [diff] [blame] | 102 | ret |
| 103 | core_init_fail: |
| 104 | mov w0, wzr |
Soby Mathew | c389d77 | 2014-06-24 12:28:41 +0100 | [diff] [blame] | 105 | ret |
Kévin Petit | a877c25 | 2015-03-24 14:03:57 +0000 | [diff] [blame] | 106 | endfunc console_core_init |
Soby Mathew | c389d77 | 2014-06-24 12:28:41 +0100 | [diff] [blame] | 107 | |
Soby Mathew | c389d77 | 2014-06-24 12:28:41 +0100 | [diff] [blame] | 108 | /* -------------------------------------------------------- |
Juan Castillo | 7f1f062 | 2014-09-09 09:49:23 +0100 | [diff] [blame] | 109 | * int console_core_putc(int c, uintptr_t base_addr) |
Soby Mathew | c389d77 | 2014-06-24 12:28:41 +0100 | [diff] [blame] | 110 | * Function to output a character over the console. It |
| 111 | * returns the character printed on success or -1 on error. |
Soby Mathew | 69817f7 | 2014-07-14 15:43:21 +0100 | [diff] [blame] | 112 | * In : w0 - character to be printed |
Soby Mathew | c389d77 | 2014-06-24 12:28:41 +0100 | [diff] [blame] | 113 | * x1 - console base address |
| 114 | * Out : return -1 on error else return character. |
| 115 | * Clobber list : x2 |
| 116 | * -------------------------------------------------------- |
| 117 | */ |
| 118 | func console_core_putc |
| 119 | /* Check the input parameter */ |
| 120 | cbz x1, putc_error |
| 121 | /* Prepend '\r' to '\n' */ |
Soby Mathew | 69817f7 | 2014-07-14 15:43:21 +0100 | [diff] [blame] | 122 | cmp w0, #0xA |
Soby Mathew | c389d77 | 2014-06-24 12:28:41 +0100 | [diff] [blame] | 123 | b.ne 2f |
| 124 | 1: |
| 125 | /* Check if the transmit FIFO is full */ |
| 126 | ldr w2, [x1, #UARTFR] |
| 127 | tbnz w2, #PL011_UARTFR_TXFF_BIT, 1b |
| 128 | mov w2, #0xD |
| 129 | str w2, [x1, #UARTDR] |
| 130 | 2: |
| 131 | /* Check if the transmit FIFO is full */ |
| 132 | ldr w2, [x1, #UARTFR] |
| 133 | tbnz w2, #PL011_UARTFR_TXFF_BIT, 2b |
| 134 | str w0, [x1, #UARTDR] |
| 135 | ret |
| 136 | putc_error: |
| 137 | mov w0, #-1 |
| 138 | ret |
Kévin Petit | a877c25 | 2015-03-24 14:03:57 +0000 | [diff] [blame] | 139 | endfunc console_core_putc |
Soby Mathew | c389d77 | 2014-06-24 12:28:41 +0100 | [diff] [blame] | 140 | |
| 141 | /* --------------------------------------------- |
Juan Castillo | 7f1f062 | 2014-09-09 09:49:23 +0100 | [diff] [blame] | 142 | * int console_core_getc(uintptr_t base_addr) |
Soby Mathew | c389d77 | 2014-06-24 12:28:41 +0100 | [diff] [blame] | 143 | * Function to get a character from the console. |
| 144 | * It returns the character grabbed on success |
| 145 | * or -1 on error. |
Dan Handley | c863fa4 | 2015-04-01 16:51:20 +0100 | [diff] [blame] | 146 | * In : x0 - console base address |
Soby Mathew | c389d77 | 2014-06-24 12:28:41 +0100 | [diff] [blame] | 147 | * Clobber list : x0, x1 |
| 148 | * --------------------------------------------- |
| 149 | */ |
Dan Handley | c863fa4 | 2015-04-01 16:51:20 +0100 | [diff] [blame] | 150 | func console_core_getc |
| 151 | cbz x0, getc_error |
Soby Mathew | c389d77 | 2014-06-24 12:28:41 +0100 | [diff] [blame] | 152 | 1: |
| 153 | /* Check if the receive FIFO is empty */ |
Dan Handley | c863fa4 | 2015-04-01 16:51:20 +0100 | [diff] [blame] | 154 | ldr w1, [x0, #UARTFR] |
| 155 | tbnz w1, #PL011_UARTFR_RXFE_BIT, 1b |
| 156 | ldr w1, [x0, #UARTDR] |
| 157 | mov w0, w1 |
Soby Mathew | c389d77 | 2014-06-24 12:28:41 +0100 | [diff] [blame] | 158 | ret |
| 159 | getc_error: |
| 160 | mov w0, #-1 |
| 161 | ret |
Dan Handley | c863fa4 | 2015-04-01 16:51:20 +0100 | [diff] [blame] | 162 | endfunc console_core_getc |