blob: 861d2ed22d9652d117e131868746d7ed545d6992 [file] [log] [blame]
Soby Mathew7abebe82016-08-08 12:38:52 +01001/*
Jimmy Brisson39f9eee2020-08-05 13:44:05 -05002 * Copyright (c) 2013-2020, 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
Julius Wernerca3930a2017-09-18 16:59:43 -070078 .globl console_pl011_register
79
80 /* -----------------------------------------------
Antonio Nino Diaz992c5ac2018-10-08 13:26:48 +010081 * int console_pl011_register(uintptr_t baseaddr,
82 * uint32_t clock, uint32_t baud,
Andre Przywara2b1b1a52020-01-25 00:58:35 +000083 * console_t *console);
Julius Wernerca3930a2017-09-18 16:59:43 -070084 * Function to initialize and register a new PL011
85 * console. Storage passed in for the console struct
86 * *must* be persistent (i.e. not from the stack).
87 * In: x0 - UART register base address
88 * w1 - UART clock in Hz
89 * w2 - Baud rate
Andre Przywara2b1b1a52020-01-25 00:58:35 +000090 * x3 - pointer to empty console_t struct
Julius Wernerca3930a2017-09-18 16:59:43 -070091 * Out: return 1 on success, 0 on error
92 * Clobber list : x0, x1, x2, x6, x7, x14
93 * -----------------------------------------------
94 */
95func console_pl011_register
96 mov x7, x30
97 mov x6, x3
98 cbz x6, register_fail
Andre Przywara2b1b1a52020-01-25 00:58:35 +000099 str x0, [x6, #CONSOLE_T_BASE]
Julius Wernerca3930a2017-09-18 16:59:43 -0700100
101 bl console_pl011_core_init
102 cbz x0, register_fail
103
104 mov x0, x6
105 mov x30, x7
Soby Mathew58873ae2018-10-10 16:03:09 +0100106 finish_console_register pl011 putc=1, getc=1, flush=1
Julius Wernerca3930a2017-09-18 16:59:43 -0700107
108register_fail:
109 ret x7
110endfunc console_pl011_register
Soby Mathew7abebe82016-08-08 12:38:52 +0100111
112 /* --------------------------------------------------------
Julius Wernerca3930a2017-09-18 16:59:43 -0700113 * int console_pl011_core_putc(int c, uintptr_t base_addr)
Soby Mathew7abebe82016-08-08 12:38:52 +0100114 * Function to output a character over the console. It
115 * returns the character printed on success or -1 on error.
116 * In : w0 - character to be printed
117 * x1 - console base address
118 * Out : return -1 on error else return character.
119 * Clobber list : x2
120 * --------------------------------------------------------
121 */
Julius Wernerca3930a2017-09-18 16:59:43 -0700122func console_pl011_core_putc
123#if ENABLE_ASSERTIONS
124 cmp x1, #0
125 ASM_ASSERT(ne)
126#endif /* ENABLE_ASSERTIONS */
127
Soby Mathew7abebe82016-08-08 12:38:52 +0100128 /* Prepend '\r' to '\n' */
129 cmp w0, #0xA
130 b.ne 2f
1311:
132 /* Check if the transmit FIFO is full */
133 ldr w2, [x1, #UARTFR]
134 tbnz w2, #PL011_UARTFR_TXFF_BIT, 1b
135 mov w2, #0xD
136 str w2, [x1, #UARTDR]
1372:
138 /* Check if the transmit FIFO is full */
139 ldr w2, [x1, #UARTFR]
140 tbnz w2, #PL011_UARTFR_TXFF_BIT, 2b
141 str w0, [x1, #UARTDR]
142 ret
Julius Wernerca3930a2017-09-18 16:59:43 -0700143endfunc console_pl011_core_putc
144
145 /* --------------------------------------------------------
Andre Przywara2b1b1a52020-01-25 00:58:35 +0000146 * int console_pl011_putc(int c, console_t *console)
Julius Wernerca3930a2017-09-18 16:59:43 -0700147 * Function to output a character over the console. It
148 * returns the character printed on success or -1 on error.
149 * In : w0 - character to be printed
150 * x1 - pointer to console_t structure
151 * Out : return -1 on error else return character.
152 * Clobber list : x2
153 * --------------------------------------------------------
154 */
155func console_pl011_putc
156#if ENABLE_ASSERTIONS
157 cmp x1, #0
158 ASM_ASSERT(ne)
159#endif /* ENABLE_ASSERTIONS */
Andre Przywara2b1b1a52020-01-25 00:58:35 +0000160 ldr x1, [x1, #CONSOLE_T_BASE]
Julius Wernerca3930a2017-09-18 16:59:43 -0700161 b console_pl011_core_putc
162endfunc console_pl011_putc
Soby Mathew7abebe82016-08-08 12:38:52 +0100163
164 /* ---------------------------------------------
Julius Wernerca3930a2017-09-18 16:59:43 -0700165 * int console_pl011_core_getc(uintptr_t base_addr)
Soby Mathew7abebe82016-08-08 12:38:52 +0100166 * Function to get a character from the console.
167 * It returns the character grabbed on success
Julius Wernerca3930a2017-09-18 16:59:43 -0700168 * or -1 if no character is available.
Soby Mathew7abebe82016-08-08 12:38:52 +0100169 * In : x0 - console base address
Julius Wernerca3930a2017-09-18 16:59:43 -0700170 * Out: w0 - character if available, else -1
Soby Mathew7abebe82016-08-08 12:38:52 +0100171 * Clobber list : x0, x1
172 * ---------------------------------------------
173 */
Julius Wernerca3930a2017-09-18 16:59:43 -0700174func console_pl011_core_getc
175#if ENABLE_ASSERTIONS
176 cmp x0, #0
177 ASM_ASSERT(ne)
178#endif /* ENABLE_ASSERTIONS */
179
Soby Mathew7abebe82016-08-08 12:38:52 +0100180 /* Check if the receive FIFO is empty */
181 ldr w1, [x0, #UARTFR]
Julius Wernerca3930a2017-09-18 16:59:43 -0700182 tbnz w1, #PL011_UARTFR_RXFE_BIT, no_char
Soby Mathew7abebe82016-08-08 12:38:52 +0100183 ldr w1, [x0, #UARTDR]
184 mov w0, w1
185 ret
Julius Wernerca3930a2017-09-18 16:59:43 -0700186no_char:
187 mov w0, #ERROR_NO_PENDING_CHAR
Soby Mathew7abebe82016-08-08 12:38:52 +0100188 ret
Julius Wernerca3930a2017-09-18 16:59:43 -0700189endfunc console_pl011_core_getc
190
191 /* ---------------------------------------------
Andre Przywara2b1b1a52020-01-25 00:58:35 +0000192 * int console_pl011_getc(console_t *console)
Julius Wernerca3930a2017-09-18 16:59:43 -0700193 * Function to get a character from the console.
194 * It returns the character grabbed on success
195 * or -1 if no character is available.
196 * In : x0 - pointer to console_t structure
197 * Out: w0 - character if available, else -1
198 * Clobber list : x0, x1
199 * ---------------------------------------------
200 */
201func console_pl011_getc
202#if ENABLE_ASSERTIONS
203 cmp x0, #0
204 ASM_ASSERT(ne)
205#endif /* ENABLE_ASSERTIONS */
Andre Przywara2b1b1a52020-01-25 00:58:35 +0000206 ldr x0, [x0, #CONSOLE_T_BASE]
Julius Wernerca3930a2017-09-18 16:59:43 -0700207 b console_pl011_core_getc
208endfunc console_pl011_getc
Antonio Nino Diaz8377ae42017-02-06 16:03:41 +0000209
210 /* ---------------------------------------------
Jimmy Brisson39f9eee2020-08-05 13:44:05 -0500211 * void console_pl011_core_flush(uintptr_t base_addr)
Antonio Nino Diaz8377ae42017-02-06 16:03:41 +0000212 * Function to force a write of all buffered
213 * data that hasn't been output.
214 * In : x0 - console base address
Jimmy Brisson39f9eee2020-08-05 13:44:05 -0500215 * Out : void.
Antonio Nino Diaz8377ae42017-02-06 16:03:41 +0000216 * Clobber list : x0, x1
217 * ---------------------------------------------
218 */
Julius Wernerca3930a2017-09-18 16:59:43 -0700219func console_pl011_core_flush
220#if ENABLE_ASSERTIONS
221 cmp x0, #0
222 ASM_ASSERT(ne)
223#endif /* ENABLE_ASSERTIONS */
Antonio Nino Diaz8377ae42017-02-06 16:03:41 +00002241:
225 /* Loop until the transmit FIFO is empty */
226 ldr w1, [x0, #UARTFR]
227 tbnz w1, #PL011_UARTFR_BUSY_BIT, 1b
Antonio Nino Diaz8377ae42017-02-06 16:03:41 +0000228 ret
Julius Wernerca3930a2017-09-18 16:59:43 -0700229endfunc console_pl011_core_flush
230
231 /* ---------------------------------------------
Jimmy Brisson39f9eee2020-08-05 13:44:05 -0500232 * void console_pl011_flush(console_t *console)
Julius Wernerca3930a2017-09-18 16:59:43 -0700233 * Function to force a write of all buffered
234 * data that hasn't been output.
235 * In : x0 - pointer to console_t structure
Jimmy Brisson39f9eee2020-08-05 13:44:05 -0500236 * Out : void
Julius Wernerca3930a2017-09-18 16:59:43 -0700237 * Clobber list : x0, x1
238 * ---------------------------------------------
239 */
240func console_pl011_flush
241#if ENABLE_ASSERTIONS
242 cmp x0, #0
243 ASM_ASSERT(ne)
244#endif /* ENABLE_ASSERTIONS */
Andre Przywara2b1b1a52020-01-25 00:58:35 +0000245 ldr x0, [x0, #CONSOLE_T_BASE]
Julius Wernerca3930a2017-09-18 16:59:43 -0700246 b console_pl011_core_flush
247endfunc console_pl011_flush