blob: 0c781a2a5b3acf87a9ec40316b2506615963eeb7 [file] [log] [blame]
Soby Mathewc389d772014-06-24 12:28:41 +01001/*
2 * Copyright (c) 2013-2014, ARM Limited and Contributors. All rights reserved.
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
34 .globl console_init
35 .globl console_putc
36 .globl console_core_init
37 .globl console_core_putc
38 .globl console_getc
39
40 /*
41 * The console base is in the data section and not in .bss
42 * even though it is zero-init. In particular, this allows
43 * the console functions to start using this variable before
44 * the runtime memory is initialized for images which do not
45 * need to copy the .data section from ROM to RAM.
46 */
47.section .data.console_base ; .align 3
48 console_base: .quad 0x0
49
Soby Mathew69817f72014-07-14 15:43:21 +010050 /* -----------------------------------------------
51 * int console_init(unsigned long base_addr,
52 * unsigned int uart_clk, unsigned int baud_rate)
Soby Mathewc389d772014-06-24 12:28:41 +010053 * Function to initialize the console without a
54 * C Runtime to print debug information. It saves
55 * the console base to the data section.
56 * In: x0 - console base address
Soby Mathew69817f72014-07-14 15:43:21 +010057 * w1 - Uart clock in Hz
58 * w2 - Baud rate
Soby Mathewc389d772014-06-24 12:28:41 +010059 * out: return 1 on success.
Soby Mathew69817f72014-07-14 15:43:21 +010060 * Clobber list : x1 - x3
61 * -----------------------------------------------
Soby Mathewc389d772014-06-24 12:28:41 +010062 */
63func console_init
Soby Mathew69817f72014-07-14 15:43:21 +010064 adrp x3, console_base
65 str x0, [x3, :lo12:console_base]
Soby Mathewc389d772014-06-24 12:28:41 +010066 b console_core_init
Kévin Petita877c252015-03-24 14:03:57 +000067endfunc console_init
Soby Mathewc389d772014-06-24 12:28:41 +010068
Soby Mathew69817f72014-07-14 15:43:21 +010069 /* -----------------------------------------------
70 * int console_core_init(unsigned long base_addr,
71 * unsigned int uart_clk, unsigned int baud_rate)
Soby Mathewc389d772014-06-24 12:28:41 +010072 * Function to initialize the console without a
73 * C Runtime to print debug information. This
74 * function will be accessed by console_init and
75 * crash reporting.
76 * In: x0 - console base address
Soby Mathew69817f72014-07-14 15:43:21 +010077 * w1 - Uart clock in Hz
78 * w2 - Baud rate
Soby Mathewc389d772014-06-24 12:28:41 +010079 * Out: return 1 on success
80 * Clobber list : x1, x2
Soby Mathew69817f72014-07-14 15:43:21 +010081 * -----------------------------------------------
Soby Mathewc389d772014-06-24 12:28:41 +010082 */
83func console_core_init
84 /* Check the input base address */
85 cbz x0, init_fail
Soby Mathew69817f72014-07-14 15:43:21 +010086 /* Check baud rate and uart clock for sanity */
87 cbz w1, init_fail
88 cbz w2, init_fail
Soby Mathewc389d772014-06-24 12:28:41 +010089 /* Program the baudrate */
Soby Mathew69817f72014-07-14 15:43:21 +010090 /* Divisor = (Uart clock * 4) / baudrate */
91 lsl w1, w1, #2
92 udiv w2, w1, w2
93 /* IBRD = Divisor >> 6 */
94 lsr w1, w2, #6
Soby Mathewc389d772014-06-24 12:28:41 +010095 /* Write the IBRD */
Soby Mathewc389d772014-06-24 12:28:41 +010096 str w1, [x0, #UARTIBRD]
Soby Mathew69817f72014-07-14 15:43:21 +010097 /* FBRD = Divisor & 0x3F */
98 and w1, w2, #0x3f
Soby Mathewc389d772014-06-24 12:28:41 +010099 /* Write the FBRD */
Soby Mathewc389d772014-06-24 12:28:41 +0100100 str w1, [x0, #UARTFBRD]
Soby Mathewc389d772014-06-24 12:28:41 +0100101 mov w1, #PL011_LINE_CONTROL
102 str w1, [x0, #UARTLCR_H]
103 /* Clear any pending errors */
104 str wzr, [x0, #UARTECR]
105 /* Enable tx, rx, and uart overall */
106 mov w1, #(PL011_UARTCR_RXE | PL011_UARTCR_TXE | PL011_UARTCR_UARTEN)
107 str w1, [x0, #UARTCR]
108 mov w0, #1
109init_fail:
110 ret
Kévin Petita877c252015-03-24 14:03:57 +0000111endfunc console_core_init
Soby Mathewc389d772014-06-24 12:28:41 +0100112
113 /* ---------------------------------------------
114 * int console_putc(int c)
115 * Function to output a character over the
116 * console. It returns the character printed on
117 * success or -1 on error.
118 * In : x0 - character to be printed
119 * Out : return -1 on error else return character.
120 * Clobber list : x1, x2
121 * ---------------------------------------------
122 */
123func console_putc
124 adrp x2, console_base
125 ldr x1, [x2, :lo12:console_base]
126 b console_core_putc
Kévin Petita877c252015-03-24 14:03:57 +0000127endfunc console_putc
Soby Mathewc389d772014-06-24 12:28:41 +0100128
129 /* --------------------------------------------------------
Soby Mathew69817f72014-07-14 15:43:21 +0100130 * int console_core_putc(int c, unsigned int base_addr)
Soby Mathewc389d772014-06-24 12:28:41 +0100131 * Function to output a character over the console. It
132 * returns the character printed on success or -1 on error.
Soby Mathew69817f72014-07-14 15:43:21 +0100133 * In : w0 - character to be printed
Soby Mathewc389d772014-06-24 12:28:41 +0100134 * x1 - console base address
135 * Out : return -1 on error else return character.
136 * Clobber list : x2
137 * --------------------------------------------------------
138 */
139func console_core_putc
140 /* Check the input parameter */
141 cbz x1, putc_error
142 /* Prepend '\r' to '\n' */
Soby Mathew69817f72014-07-14 15:43:21 +0100143 cmp w0, #0xA
Soby Mathewc389d772014-06-24 12:28:41 +0100144 b.ne 2f
1451:
146 /* Check if the transmit FIFO is full */
147 ldr w2, [x1, #UARTFR]
148 tbnz w2, #PL011_UARTFR_TXFF_BIT, 1b
149 mov w2, #0xD
150 str w2, [x1, #UARTDR]
1512:
152 /* Check if the transmit FIFO is full */
153 ldr w2, [x1, #UARTFR]
154 tbnz w2, #PL011_UARTFR_TXFF_BIT, 2b
155 str w0, [x1, #UARTDR]
156 ret
157putc_error:
158 mov w0, #-1
159 ret
Kévin Petita877c252015-03-24 14:03:57 +0000160endfunc console_core_putc
Soby Mathewc389d772014-06-24 12:28:41 +0100161
162 /* ---------------------------------------------
163 * int console_getc(void)
164 * Function to get a character from the console.
165 * It returns the character grabbed on success
166 * or -1 on error.
167 * Clobber list : x0, x1
168 * ---------------------------------------------
169 */
170func console_getc
171 adrp x0, console_base
172 ldr x1, [x0, :lo12:console_base]
173 cbz x1, getc_error
1741:
175 /* Check if the receive FIFO is empty */
176 ldr w0, [x1, #UARTFR]
177 tbnz w0, #PL011_UARTFR_RXFE_BIT, 1b
178 ldr w0, [x1, #UARTDR]
179 ret
180getc_error:
181 mov w0, #-1
182 ret
Kévin Petita877c252015-03-24 14:03:57 +0000183endfunc console_getc