blob: bf26b6b088581bdc864f897522aec7b4b14112ba [file] [log] [blame]
Soby Mathewfce5f752014-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
50 /* ---------------------------------------------
51 * int console_init(unsigned long base_addr)
52 * Function to initialize the console without a
53 * C Runtime to print debug information. It saves
54 * the console base to the data section.
55 * In: x0 - console base address
56 * out: return 1 on success.
57 * Clobber list : x1, x2
58 * ---------------------------------------------
59 */
60func console_init
61 adrp x1, console_base
62 str x0, [x1, :lo12:console_base]
63 b console_core_init
64
65 /* ---------------------------------------------
66 * int console_core_init(unsigned long base_addr)
67 * Function to initialize the console without a
68 * C Runtime to print debug information. This
69 * function will be accessed by console_init and
70 * crash reporting.
71 * In: x0 - console base address
72 * Out: return 1 on success
73 * Clobber list : x1, x2
74 * ---------------------------------------------
75 */
76func console_core_init
77 /* Check the input base address */
78 cbz x0, init_fail
79 /* Program the baudrate */
80#if defined(PL011_INTEGER) && defined(PL011_FRACTIONAL)
81 mov w1, #PL011_INTEGER
82 str w1, [x0, #UARTIBRD]
83 mov w1, #PL011_FRACTIONAL
84 str w1, [x0, #UARTFBRD]
85#else
86.set BRD, ((PL011_CLK_IN_HZ << 2) / PL011_BAUDRATE)
87 /* Write the IBRD */
88 mov w1, #((BRD >> 6) & 0xffff)
89.if BRD>=0x400000
90 movk w1, #(BRD >> 22), LSL #16
91.endif
92 str w1, [x0, #UARTIBRD]
93 /* Write the FBRD */
94 mov w1, #(BRD & 0x3f)
95 str w1, [x0, #UARTFBRD]
96#endif
97 mov w1, #PL011_LINE_CONTROL
98 str w1, [x0, #UARTLCR_H]
99 /* Clear any pending errors */
100 str wzr, [x0, #UARTECR]
101 /* Enable tx, rx, and uart overall */
102 mov w1, #(PL011_UARTCR_RXE | PL011_UARTCR_TXE | PL011_UARTCR_UARTEN)
103 str w1, [x0, #UARTCR]
104 mov w0, #1
105init_fail:
106 ret
107
108 /* ---------------------------------------------
109 * int console_putc(int c)
110 * Function to output a character over the
111 * console. It returns the character printed on
112 * success or -1 on error.
113 * In : x0 - character to be printed
114 * Out : return -1 on error else return character.
115 * Clobber list : x1, x2
116 * ---------------------------------------------
117 */
118func console_putc
119 adrp x2, console_base
120 ldr x1, [x2, :lo12:console_base]
121 b console_core_putc
122
123 /* --------------------------------------------------------
124 * int console_core_putc(int c, unsigned long base_addr)
125 * Function to output a character over the console. It
126 * returns the character printed on success or -1 on error.
127 * In : x0 - character to be printed
128 * x1 - console base address
129 * Out : return -1 on error else return character.
130 * Clobber list : x2
131 * --------------------------------------------------------
132 */
133func console_core_putc
134 /* Check the input parameter */
135 cbz x1, putc_error
136 /* Prepend '\r' to '\n' */
137 cmp x0, #0xA
138 b.ne 2f
1391:
140 /* Check if the transmit FIFO is full */
141 ldr w2, [x1, #UARTFR]
142 tbnz w2, #PL011_UARTFR_TXFF_BIT, 1b
143 mov w2, #0xD
144 str w2, [x1, #UARTDR]
1452:
146 /* Check if the transmit FIFO is full */
147 ldr w2, [x1, #UARTFR]
148 tbnz w2, #PL011_UARTFR_TXFF_BIT, 2b
149 str w0, [x1, #UARTDR]
150 ret
151putc_error:
152 mov w0, #-1
153 ret
154
155 /* ---------------------------------------------
156 * int console_getc(void)
157 * Function to get a character from the console.
158 * It returns the character grabbed on success
159 * or -1 on error.
160 * Clobber list : x0, x1
161 * ---------------------------------------------
162 */
163func console_getc
164 adrp x0, console_base
165 ldr x1, [x0, :lo12:console_base]
166 cbz x1, getc_error
1671:
168 /* Check if the receive FIFO is empty */
169 ldr w0, [x1, #UARTFR]
170 tbnz w0, #PL011_UARTFR_RXFE_BIT, 1b
171 ldr w0, [x1, #UARTDR]
172 ret
173getc_error:
174 mov w0, #-1
175 ret