blob: 93045f03dc7b51f9194a3c1a3e16ed59d0eb9eaa [file] [log] [blame]
Soby Mathew420cc372016-03-24 16:52:40 +00001/*
Ambroise Vincentc5f7bd12019-03-27 10:22:10 +00002 * Copyright (c) 2016-2019, ARM Limited and Contributors. All rights reserved.
Soby Mathew420cc372016-03-24 16:52:40 +00003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Soby Mathew420cc372016-03-24 16:52:40 +00005 */
6#include <arch.h>
7#include <asm_macros.S>
Daniel Boulby05e7f562018-09-19 13:58:20 +01008#include <assert_macros.S>
9#include <console_macros.S>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000010#include <drivers/arm/pl011.h>
Soby Mathew420cc372016-03-24 16:52:40 +000011
Daniel Boulby05e7f562018-09-19 13:58:20 +010012 /*
13 * "core" functions are low-level implementations that don't require
14 * writeable 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
20
21 .globl console_pl011_putc
22 .globl console_pl011_getc
23 .globl console_pl011_flush
Soby Mathew420cc372016-03-24 16:52:40 +000024
25
26 /* -----------------------------------------------
27 * int console_core_init(uintptr_t base_addr,
28 * unsigned int uart_clk, unsigned int baud_rate)
29 * Function to initialize the console without a
30 * C Runtime to print debug information. This
31 * function will be accessed by console_init and
32 * crash reporting.
33 * In: r0 - console base address
34 * r1 - Uart clock in Hz
35 * r2 - Baud rate
36 * Out: return 1 on success else 0 on error
37 * Clobber list : r1, r2, r3
38 * -----------------------------------------------
39 */
Daniel Boulby05e7f562018-09-19 13:58:20 +010040func console_pl011_core_init
Soby Mathew420cc372016-03-24 16:52:40 +000041 /* Check the input base address */
42 cmp r0, #0
43 beq core_init_fail
44#if !PL011_GENERIC_UART
45 /* Check baud rate and uart clock for sanity */
46 cmp r1, #0
47 beq core_init_fail
48 cmp r2, #0
49 beq core_init_fail
50 /* Disable the UART before initialization */
51 ldr r3, [r0, #UARTCR]
52 bic r3, r3, #PL011_UARTCR_UARTEN
53 str r3, [r0, #UARTCR]
54 /* Program the baudrate */
55 /* Divisor = (Uart clock * 4) / baudrate */
56 lsl r1, r1, #2
Usama Arifb69ac082018-12-12 17:08:33 +000057#if (ARM_ARCH_MAJOR == 7) && !defined(ARMV7_SUPPORTS_VIRTUALIZATION)
58 push {r0,r3}
59 softudiv r0,r1,r2,r3
Avinash Mehta96124f42019-10-08 12:09:04 +010060 mov r2, r0
Usama Arifb69ac082018-12-12 17:08:33 +000061 pop {r0,r3}
62#else
Soby Mathew420cc372016-03-24 16:52:40 +000063 udiv r2, r1, r2
Usama Arifb69ac082018-12-12 17:08:33 +000064#endif
Soby Mathew420cc372016-03-24 16:52:40 +000065 /* IBRD = Divisor >> 6 */
66 lsr r1, r2, #6
67 /* Write the IBRD */
68 str r1, [r0, #UARTIBRD]
69 /* FBRD = Divisor & 0x3F */
70 and r1, r2, #0x3f
71 /* Write the FBRD */
72 str r1, [r0, #UARTFBRD]
73 mov r1, #PL011_LINE_CONTROL
74 str r1, [r0, #UARTLCR_H]
75 /* Clear any pending errors */
76 mov r1, #0
77 str r1, [r0, #UARTECR]
78 /* Enable tx, rx, and uart overall */
79 ldr r1, =(PL011_UARTCR_RXE | PL011_UARTCR_TXE | PL011_UARTCR_UARTEN)
80 str r1, [r0, #UARTCR]
81#endif
82 mov r0, #1
83 bx lr
84core_init_fail:
85 mov r0, #0
86 bx lr
Daniel Boulby05e7f562018-09-19 13:58:20 +010087endfunc console_pl011_core_init
88
Daniel Boulby05e7f562018-09-19 13:58:20 +010089 .globl console_pl011_register
90
91 /* -------------------------------------------------------
Antonio Nino Diaz992c5ac2018-10-08 13:26:48 +010092 * int console_pl011_register(uintptr_t baseaddr,
93 * uint32_t clock, uint32_t baud,
Andre Przywara2b1b1a52020-01-25 00:58:35 +000094 * console_t *console);
Daniel Boulby05e7f562018-09-19 13:58:20 +010095 * Function to initialize and register a new PL011
96 * console. Storage passed in for the console struct
97 * *must* be persistent (i.e. not from the stack).
98 * In: r0 - UART register base address
99 * r1 - UART clock in Hz
100 * r2 - Baud rate
Andre Przywara2b1b1a52020-01-25 00:58:35 +0000101 * r3 - pointer to empty console_t struct
Daniel Boulby05e7f562018-09-19 13:58:20 +0100102 * Out: return 1 on success, 0 on error
103 * Clobber list : r0, r1, r2
104 * -------------------------------------------------------
105 */
106func console_pl011_register
107 push {r4, lr}
108 mov r4, r3
109 cmp r4, #0
110 beq register_fail
Andre Przywara2b1b1a52020-01-25 00:58:35 +0000111 str r0, [r4, #CONSOLE_T_BASE]
Daniel Boulby05e7f562018-09-19 13:58:20 +0100112
113 bl console_pl011_core_init
114 cmp r0, #0
115 beq register_fail
116
117 mov r0, r4
118 pop {r4, lr}
Soby Mathew58873ae2018-10-10 16:03:09 +0100119 finish_console_register pl011 putc=1, getc=1, flush=1
Daniel Boulby05e7f562018-09-19 13:58:20 +0100120
121register_fail:
122 pop {r4, pc}
123endfunc console_pl011_register
Soby Mathew420cc372016-03-24 16:52:40 +0000124
125 /* --------------------------------------------------------
126 * int console_core_putc(int c, uintptr_t base_addr)
127 * Function to output a character over the console. It
128 * returns the character printed on success or -1 on error.
129 * In : r0 - character to be printed
130 * r1 - console base address
131 * Out : return -1 on error else return character.
132 * Clobber list : r2
133 * --------------------------------------------------------
134 */
Daniel Boulby05e7f562018-09-19 13:58:20 +0100135func console_pl011_core_putc
Soby Mathew420cc372016-03-24 16:52:40 +0000136 /* Check the input parameter */
137 cmp r1, #0
138 beq putc_error
139 /* Prepend '\r' to '\n' */
140 cmp r0, #0xA
141 bne 2f
1421:
143 /* Check if the transmit FIFO is full */
144 ldr r2, [r1, #UARTFR]
Yatharth Kochard0f5f9c2016-11-09 15:39:25 +0000145 tst r2, #PL011_UARTFR_TXFF
146 bne 1b
Soby Mathew420cc372016-03-24 16:52:40 +0000147 mov r2, #0xD
148 str r2, [r1, #UARTDR]
1492:
150 /* Check if the transmit FIFO is full */
151 ldr r2, [r1, #UARTFR]
Yatharth Kochard0f5f9c2016-11-09 15:39:25 +0000152 tst r2, #PL011_UARTFR_TXFF
153 bne 2b
Soby Mathew420cc372016-03-24 16:52:40 +0000154 str r0, [r1, #UARTDR]
155 bx lr
156putc_error:
157 mov r0, #-1
158 bx lr
Daniel Boulby05e7f562018-09-19 13:58:20 +0100159endfunc console_pl011_core_putc
160
161 /* --------------------------------------------------------
Andre Przywara2b1b1a52020-01-25 00:58:35 +0000162 * int console_pl011_putc(int c, console_t *console)
Daniel Boulby05e7f562018-09-19 13:58:20 +0100163 * Function to output a character over the console. It
164 * returns the character printed on success or -1 on error.
165 * In: r0 - character to be printed
166 * r1 - pointer to console_t structure
167 * Out : return -1 on error else return character.
168 * Clobber list: r2
169 * -------------------------------------------------------
170 */
171func console_pl011_putc
172#if ENABLE_ASSERTIONS
173 cmp r1, #0
174 ASM_ASSERT(ne)
175#endif /* ENABLE_ASSERTIONS */
Andre Przywara2b1b1a52020-01-25 00:58:35 +0000176 ldr r1, [r1, #CONSOLE_T_BASE]
Daniel Boulby05e7f562018-09-19 13:58:20 +0100177 b console_pl011_core_putc
178endfunc console_pl011_putc
Soby Mathew420cc372016-03-24 16:52:40 +0000179
180 /* ---------------------------------------------
181 * int console_core_getc(uintptr_t base_addr)
182 * Function to get a character from the console.
183 * It returns the character grabbed on success
184 * or -1 on error.
185 * In : r0 - console base address
186 * Clobber list : r0, r1
187 * ---------------------------------------------
188 */
Daniel Boulby05e7f562018-09-19 13:58:20 +0100189func console_pl011_core_getc
Soby Mathew420cc372016-03-24 16:52:40 +0000190 cmp r0, #0
191 beq getc_error
1921:
193 /* Check if the receive FIFO is empty */
194 ldr r1, [r0, #UARTFR]
Yatharth Kochard0f5f9c2016-11-09 15:39:25 +0000195 tst r1, #PL011_UARTFR_RXFE
196 bne 1b
Soby Mathew420cc372016-03-24 16:52:40 +0000197 ldr r1, [r0, #UARTDR]
198 mov r0, r1
199 bx lr
200getc_error:
201 mov r0, #-1
202 bx lr
Daniel Boulby05e7f562018-09-19 13:58:20 +0100203endfunc console_pl011_core_getc
204
205 /* ------------------------------------------------
Andre Przywara2b1b1a52020-01-25 00:58:35 +0000206 * int console_pl011_getc(console_t *console)
Daniel Boulby05e7f562018-09-19 13:58:20 +0100207 * Function to get a character from the console.
208 * It returns the character grabbed on success
209 * or -1 if no character is available.
210 * In : r0 - pointer to console_t structure
211 * Out: r0 - character if available, else -1
212 * Clobber list: r0, r1
213 * ------------------------------------------------
214 */
215func console_pl011_getc
216#if ENABLE_ASSERTIONS
217 cmp r0, #0
218 ASM_ASSERT(ne)
219#endif /* ENABLE_ASSERTIONS */
Andre Przywara2b1b1a52020-01-25 00:58:35 +0000220 ldr r0, [r0, #CONSOLE_T_BASE]
Daniel Boulby05e7f562018-09-19 13:58:20 +0100221 b console_pl011_core_getc
222endfunc console_pl011_getc
Antonio Nino Diaz8377ae42017-02-06 16:03:41 +0000223
224 /* ---------------------------------------------
225 * int console_core_flush(uintptr_t base_addr)
226 * Function to force a write of all buffered
227 * data that hasn't been output.
228 * In : r0 - console base address
229 * Out : return -1 on error else return 0.
230 * Clobber list : r0, r1
231 * ---------------------------------------------
232 */
Daniel Boulby05e7f562018-09-19 13:58:20 +0100233func console_pl011_core_flush
Antonio Nino Diaz8377ae42017-02-06 16:03:41 +0000234 cmp r0, #0
235 beq flush_error
236
2371:
238 /* Loop while the transmit FIFO is busy */
239 ldr r1, [r0, #UARTFR]
240 tst r1, #PL011_UARTFR_BUSY
241 bne 1b
242
243 mov r0, #0
244 bx lr
245flush_error:
246 mov r0, #-1
247 bx lr
Daniel Boulby05e7f562018-09-19 13:58:20 +0100248endfunc console_pl011_core_flush
249
250 /* ---------------------------------------------
Andre Przywara2b1b1a52020-01-25 00:58:35 +0000251 * int console_pl011_flush(console_t *console)
Daniel Boulby05e7f562018-09-19 13:58:20 +0100252 * Function to force a write of all buffered
253 * data that hasn't been output.
254 * In : r0 - pointer to console_t structure
255 * Out : return -1 on error else return 0.
256 * Clobber list: r0, r1
257 * ---------------------------------------------
258 */
259func console_pl011_flush
260#if ENABLE_ASSERTIONS
261 cmp r0, #0
262 ASM_ASSERT(ne)
263#endif /* ENABLE_ASSERTIONS */
Andre Przywara2b1b1a52020-01-25 00:58:35 +0000264 ldr r0, [r0, #CONSOLE_T_BASE]
Daniel Boulby05e7f562018-09-19 13:58:20 +0100265 b console_pl011_core_flush
266endfunc console_pl011_flush