blob: c772a9ec3a6b3aac1fb7c2cc4dac47451b9bf256 [file] [log] [blame]
Soby Mathew7abebe82016-08-08 12:38:52 +01001/*
Ambroise Vincentc5f7bd12019-03-27 10:22:10 +00002 * Copyright (c) 2013-2019, ARM Limited and Contributors. All rights reserved.
Soby Mathew7abebe82016-08-08 12:38:52 +01003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Soby Mathew7abebe82016-08-08 12:38:52 +01005 */
6#include <arch.h>
7#include <asm_macros.S>
Julius Wernerca3930a2017-09-18 16:59:43 -07008#include <assert_macros.S>
Michalis Pappas2e9a8a32018-03-04 14:16:01 +08009#include <console_macros.S>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000010#include <drivers/arm/pl011.h>
Soby Mathew7abebe82016-08-08 12:38:52 +010011
Julius Wernerca3930a2017-09-18 16:59:43 -070012 /*
13 * "core" functions are low-level implementations that don't require
14 * writable memory and are thus safe to call in BL1 crash context.
15 */
16 .globl console_pl011_core_init
17 .globl console_pl011_core_putc
18 .globl console_pl011_core_getc
19 .globl console_pl011_core_flush
Soby Mathew7abebe82016-08-08 12:38:52 +010020
Julius Wernerca3930a2017-09-18 16:59:43 -070021 .globl console_pl011_putc
22 .globl console_pl011_getc
23 .globl console_pl011_flush
Soby Mathew7abebe82016-08-08 12:38:52 +010024
25 /* -----------------------------------------------
Julius Wernerca3930a2017-09-18 16:59:43 -070026 * int console_pl011_core_init(uintptr_t base_addr,
Soby Mathew7abebe82016-08-08 12:38:52 +010027 * unsigned int uart_clk, unsigned int baud_rate)
28 * Function to initialize the console without a
29 * C Runtime to print debug information. This
30 * function will be accessed by console_init and
31 * crash reporting.
32 * In: x0 - console base address
33 * w1 - Uart clock in Hz
34 * w2 - Baud rate
35 * Out: return 1 on success else 0 on error
36 * Clobber list : x1, x2, x3, x4
37 * -----------------------------------------------
38 */
Julius Wernerca3930a2017-09-18 16:59:43 -070039func console_pl011_core_init
Soby Mathew7abebe82016-08-08 12:38:52 +010040 /* Check the input base address */
41 cbz x0, core_init_fail
42#if !PL011_GENERIC_UART
43 /* Check baud rate and uart clock for sanity */
44 cbz w1, core_init_fail
45 cbz w2, core_init_fail
46 /* Disable uart before programming */
47 ldr w3, [x0, #UARTCR]
48 mov w4, #PL011_UARTCR_UARTEN
49 bic w3, w3, w4
50 str w3, [x0, #UARTCR]
51 /* Program the baudrate */
52 /* Divisor = (Uart clock * 4) / baudrate */
53 lsl w1, w1, #2
54 udiv w2, w1, w2
55 /* IBRD = Divisor >> 6 */
56 lsr w1, w2, #6
57 /* Write the IBRD */
58 str w1, [x0, #UARTIBRD]
59 /* FBRD = Divisor & 0x3F */
60 and w1, w2, #0x3f
61 /* Write the FBRD */
62 str w1, [x0, #UARTFBRD]
63 mov w1, #PL011_LINE_CONTROL
64 str w1, [x0, #UARTLCR_H]
65 /* Clear any pending errors */
66 str wzr, [x0, #UARTECR]
67 /* Enable tx, rx, and uart overall */
68 mov w1, #(PL011_UARTCR_RXE | PL011_UARTCR_TXE | PL011_UARTCR_UARTEN)
69 str w1, [x0, #UARTCR]
70#endif
71 mov w0, #1
72 ret
73core_init_fail:
74 mov w0, wzr
75 ret
Julius Wernerca3930a2017-09-18 16:59:43 -070076endfunc console_pl011_core_init
77
78#if MULTI_CONSOLE_API
79 .globl console_pl011_register
80
81 /* -----------------------------------------------
Antonio Nino Diaz992c5ac2018-10-08 13:26:48 +010082 * int console_pl011_register(uintptr_t baseaddr,
83 * uint32_t clock, uint32_t baud,
84 * console_pl011_t *console);
Julius Wernerca3930a2017-09-18 16:59:43 -070085 * Function to initialize and register a new PL011
86 * console. Storage passed in for the console struct
87 * *must* be persistent (i.e. not from the stack).
88 * In: x0 - UART register base address
89 * w1 - UART clock in Hz
90 * w2 - Baud rate
91 * x3 - pointer to empty console_pl011_t struct
92 * Out: return 1 on success, 0 on error
93 * Clobber list : x0, x1, x2, x6, x7, x14
94 * -----------------------------------------------
95 */
96func console_pl011_register
97 mov x7, x30
98 mov x6, x3
99 cbz x6, register_fail
100 str x0, [x6, #CONSOLE_T_PL011_BASE]
101
102 bl console_pl011_core_init
103 cbz x0, register_fail
104
105 mov x0, x6
106 mov x30, x7
Soby Mathew58873ae2018-10-10 16:03:09 +0100107 finish_console_register pl011 putc=1, getc=1, flush=1
Julius Wernerca3930a2017-09-18 16:59:43 -0700108
109register_fail:
110 ret x7
111endfunc console_pl011_register
112#else
113 .globl console_core_init
114 .globl console_core_putc
115 .globl console_core_getc
116 .globl console_core_flush
117 .equ console_core_init,console_pl011_core_init
118 .equ console_core_putc,console_pl011_core_putc
119 .equ console_core_getc,console_pl011_core_getc
120 .equ console_core_flush,console_pl011_core_flush
121#endif
Soby Mathew7abebe82016-08-08 12:38:52 +0100122
123 /* --------------------------------------------------------
Julius Wernerca3930a2017-09-18 16:59:43 -0700124 * int console_pl011_core_putc(int c, uintptr_t base_addr)
Soby Mathew7abebe82016-08-08 12:38:52 +0100125 * Function to output a character over the console. It
126 * returns the character printed on success or -1 on error.
127 * In : w0 - character to be printed
128 * x1 - console base address
129 * Out : return -1 on error else return character.
130 * Clobber list : x2
131 * --------------------------------------------------------
132 */
Julius Wernerca3930a2017-09-18 16:59:43 -0700133func console_pl011_core_putc
134#if ENABLE_ASSERTIONS
135 cmp x1, #0
136 ASM_ASSERT(ne)
137#endif /* ENABLE_ASSERTIONS */
138
Soby Mathew7abebe82016-08-08 12:38:52 +0100139 /* Prepend '\r' to '\n' */
140 cmp w0, #0xA
141 b.ne 2f
1421:
143 /* Check if the transmit FIFO is full */
144 ldr w2, [x1, #UARTFR]
145 tbnz w2, #PL011_UARTFR_TXFF_BIT, 1b
146 mov w2, #0xD
147 str w2, [x1, #UARTDR]
1482:
149 /* Check if the transmit FIFO is full */
150 ldr w2, [x1, #UARTFR]
151 tbnz w2, #PL011_UARTFR_TXFF_BIT, 2b
152 str w0, [x1, #UARTDR]
153 ret
Julius Wernerca3930a2017-09-18 16:59:43 -0700154endfunc console_pl011_core_putc
155
156 /* --------------------------------------------------------
157 * int console_pl011_putc(int c, console_pl011_t *console)
158 * Function to output a character over the console. It
159 * returns the character printed on success or -1 on error.
160 * In : w0 - character to be printed
161 * x1 - pointer to console_t structure
162 * Out : return -1 on error else return character.
163 * Clobber list : x2
164 * --------------------------------------------------------
165 */
166func console_pl011_putc
167#if ENABLE_ASSERTIONS
168 cmp x1, #0
169 ASM_ASSERT(ne)
170#endif /* ENABLE_ASSERTIONS */
171 ldr x1, [x1, #CONSOLE_T_PL011_BASE]
172 b console_pl011_core_putc
173endfunc console_pl011_putc
Soby Mathew7abebe82016-08-08 12:38:52 +0100174
175 /* ---------------------------------------------
Julius Wernerca3930a2017-09-18 16:59:43 -0700176 * int console_pl011_core_getc(uintptr_t base_addr)
Soby Mathew7abebe82016-08-08 12:38:52 +0100177 * Function to get a character from the console.
178 * It returns the character grabbed on success
Julius Wernerca3930a2017-09-18 16:59:43 -0700179 * or -1 if no character is available.
Soby Mathew7abebe82016-08-08 12:38:52 +0100180 * In : x0 - console base address
Julius Wernerca3930a2017-09-18 16:59:43 -0700181 * Out: w0 - character if available, else -1
Soby Mathew7abebe82016-08-08 12:38:52 +0100182 * Clobber list : x0, x1
183 * ---------------------------------------------
184 */
Julius Wernerca3930a2017-09-18 16:59:43 -0700185func console_pl011_core_getc
186#if ENABLE_ASSERTIONS
187 cmp x0, #0
188 ASM_ASSERT(ne)
189#endif /* ENABLE_ASSERTIONS */
190
Soby Mathew7abebe82016-08-08 12:38:52 +0100191 /* Check if the receive FIFO is empty */
192 ldr w1, [x0, #UARTFR]
Julius Wernerca3930a2017-09-18 16:59:43 -0700193 tbnz w1, #PL011_UARTFR_RXFE_BIT, no_char
Soby Mathew7abebe82016-08-08 12:38:52 +0100194 ldr w1, [x0, #UARTDR]
195 mov w0, w1
196 ret
Julius Wernerca3930a2017-09-18 16:59:43 -0700197no_char:
198 mov w0, #ERROR_NO_PENDING_CHAR
Soby Mathew7abebe82016-08-08 12:38:52 +0100199 ret
Julius Wernerca3930a2017-09-18 16:59:43 -0700200endfunc console_pl011_core_getc
201
202 /* ---------------------------------------------
203 * int console_pl011_getc(console_pl011_t *console)
204 * Function to get a character from the console.
205 * It returns the character grabbed on success
206 * or -1 if no character is available.
207 * In : x0 - pointer to console_t structure
208 * Out: w0 - character if available, else -1
209 * Clobber list : x0, x1
210 * ---------------------------------------------
211 */
212func console_pl011_getc
213#if ENABLE_ASSERTIONS
214 cmp x0, #0
215 ASM_ASSERT(ne)
216#endif /* ENABLE_ASSERTIONS */
217 ldr x0, [x0, #CONSOLE_T_PL011_BASE]
218 b console_pl011_core_getc
219endfunc console_pl011_getc
Antonio Nino Diaz8377ae42017-02-06 16:03:41 +0000220
221 /* ---------------------------------------------
Julius Wernerca3930a2017-09-18 16:59:43 -0700222 * int console_pl011_core_flush(uintptr_t base_addr)
Antonio Nino Diaz8377ae42017-02-06 16:03:41 +0000223 * Function to force a write of all buffered
224 * data that hasn't been output.
225 * In : x0 - console base address
226 * Out : return -1 on error else return 0.
227 * Clobber list : x0, x1
228 * ---------------------------------------------
229 */
Julius Wernerca3930a2017-09-18 16:59:43 -0700230func console_pl011_core_flush
231#if ENABLE_ASSERTIONS
232 cmp x0, #0
233 ASM_ASSERT(ne)
234#endif /* ENABLE_ASSERTIONS */
Antonio Nino Diaz8377ae42017-02-06 16:03:41 +00002351:
236 /* Loop until the transmit FIFO is empty */
237 ldr w1, [x0, #UARTFR]
238 tbnz w1, #PL011_UARTFR_BUSY_BIT, 1b
239
240 mov w0, #0
241 ret
Julius Wernerca3930a2017-09-18 16:59:43 -0700242endfunc console_pl011_core_flush
243
244 /* ---------------------------------------------
245 * int console_pl011_flush(console_pl011_t *console)
246 * Function to force a write of all buffered
247 * data that hasn't been output.
248 * In : x0 - pointer to console_t structure
249 * Out : return -1 on error else return 0.
250 * Clobber list : x0, x1
251 * ---------------------------------------------
252 */
253func console_pl011_flush
254#if ENABLE_ASSERTIONS
255 cmp x0, #0
256 ASM_ASSERT(ne)
257#endif /* ENABLE_ASSERTIONS */
258 ldr x0, [x0, #CONSOLE_T_PL011_BASE]
259 b console_pl011_core_flush
260endfunc console_pl011_flush