blob: 8da248c138fc5fe99f06d3d4968bb6460ac7ca87 [file] [log] [blame]
developer65014b82015-04-13 14:47:57 +08001/*
Antonio Nino Diaz56ec1d52017-02-08 15:58:12 +00002 * Copyright (c) 2015-2017, ARM Limited and Contributors. All rights reserved.
developer65014b82015-04-13 14:47:57 +08003 *
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 <asm_macros.S>
31#include <uart8250.h>
32
33 .globl console_core_init
34 .globl console_core_putc
35 .globl console_core_getc
Antonio Nino Diaz56ec1d52017-02-08 15:58:12 +000036 .globl console_core_flush
developer65014b82015-04-13 14:47:57 +080037
38 /* -----------------------------------------------
39 * int console_core_init(unsigned long base_addr,
40 * unsigned int uart_clk, unsigned int baud_rate)
41 * Function to initialize the console without a
42 * C Runtime to print debug information. This
43 * function will be accessed by console_init and
44 * crash reporting.
45 * In: x0 - console base address
46 * w1 - Uart clock in Hz
47 * w2 - Baud rate
48 * Out: return 1 on success else 0 on error
49 * Clobber list : x1, x2, x3
50 * -----------------------------------------------
51 */
52func console_core_init
53 /* Check the input base address */
54 cbz x0, core_init_fail
55 /* Check baud rate and uart clock for sanity */
56 cbz w1, core_init_fail
57 cbz w2, core_init_fail
58
59 /* Disable interrupt */
60 str wzr, [x0, #UART_IER]
61
62 /* Force DTR and RTS to high */
63 mov w3, #(UART_MCR_DTR | UART_MCR_RTS)
64 str w3, [x0, #UART_MCR]
65
66 /* Check high speed */
67 movz w3, #:abs_g1:115200
68 movk w3, #:abs_g0_nc:115200
69 cmp w2, w3
70 b.hi 1f
71
72 /* Non high speed */
73 lsl w2, w2, #4
74 mov w3, wzr
75 b 2f
76
77 /* High speed */
781: lsl w2, w2, #2
79 mov w3, #2
80
81 /* Set high speed UART register */
822: str w3, [x0, #UART_HIGHSPEED]
83
84 /* Calculate divisor */
85 udiv w3, w1, w2 /* divisor = uartclk / (quot * baudrate) */
86 msub w1, w3, w2, w1 /* remainder = uartclk % (quot * baudrate) */
87 lsr w2, w2, #1
88 cmp w1, w2
89 cinc w3, w3, hs
90
91 /* Set line configuration, access divisor latches */
92 mov w1, #(UART_LCR_DLAB | UART_LCR_WLS_8)
93 str w1, [x0, #UART_LCR]
94
95 /* Set the divisor */
96 and w1, w3, #0xff
97 str w1, [x0, #UART_DLL]
98 lsr w1, w3, #8
99 and w1, w1, #0xff
100 str w1, [x0, #UART_DLH]
101
102 /* Hide the divisor latches */
103 mov w1, #UART_LCR_WLS_8
104 str w1, [x0, #UART_LCR]
105
106 /* Enable FIFOs, and clear receive and transmit */
107 mov w1, #(UART_FCR_FIFO_EN | UART_FCR_CLEAR_RCVR | \
108 UART_FCR_CLEAR_XMIT)
109 str w1, [x0, #UART_FCR]
110
111 mov w0, #1
112 ret
113core_init_fail:
114 mov w0, wzr
115 ret
116endfunc console_core_init
117
118 /* --------------------------------------------------------
119 * int console_core_putc(int c, unsigned long base_addr)
120 * Function to output a character over the console. It
121 * returns the character printed on success or -1 on error.
122 * In : w0 - character to be printed
123 * x1 - console base address
124 * Out : return -1 on error else return character.
125 * Clobber list : x2
126 * --------------------------------------------------------
127 */
128func console_core_putc
129 /* Check the input parameter */
130 cbz x1, putc_error
131 /* Prepend '\r' to '\n' */
132 cmp w0, #0xA
133 b.ne 2f
134
135 /* Check if the transmit FIFO is full */
1361: ldr w2, [x1, #UART_LSR]
137 and w2, w2, #UART_LSR_THRE
138 cbz w2, 1b
139 mov w2, #0xD
140 str w2, [x1, #UART_THR]
141
142 /* Check if the transmit FIFO is full */
1432: ldr w2, [x1, #UART_LSR]
144 and w2, w2, #UART_LSR_THRE
145 cbz w2, 2b
146 str w0, [x1, #UART_THR]
147 ret
148putc_error:
149 mov w0, #-1
150 ret
151endfunc console_core_putc
152
153 /* ---------------------------------------------
154 * int console_core_getc(unsigned long base_addr)
155 * Function to get a character from the console.
156 * It returns the character grabbed on success
157 * or -1 on error.
158 * In : x0 - console base address
159 * Clobber list : x0, x1
160 * ---------------------------------------------
161 */
162func console_core_getc
163 cbz x0, getc_error
164
165 /* Check if the receive FIFO is empty */
1661: ldr w1, [x0, #UART_LSR]
167 tbz w1, #UART_LSR_DR, 1b
168 ldr w0, [x0, #UART_RBR]
169 ret
170getc_error:
171 mov w0, #-1
172 ret
173endfunc console_core_getc
Antonio Nino Diaz56ec1d52017-02-08 15:58:12 +0000174
175 /* ---------------------------------------------
176 * int console_core_flush(uintptr_t base_addr)
177 * Function to force a write of all buffered
178 * data that hasn't been output.
179 * In : x0 - console base address
180 * Out : return -1 on error else return 0.
181 * Clobber list : x0, x1
182 * ---------------------------------------------
183 */
184func console_core_flush
185 /* Placeholder */
186 mov w0, #0
187 ret
188endfunc console_core_flush