blob: 21ed7ab8ec88ba1e8a0a1f96c512cce3b78fcc16 [file] [log] [blame]
Soby Mathew420cc372016-03-24 16:52:40 +00001/*
2 * Copyright (c) 2016, 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/*
35 * Pull in generic functions to provide backwards compatibility for
36 * platform makefiles
37 */
38#include "../../../console/aarch32/console.S"
39
40 .globl console_core_init
41 .globl console_core_putc
42 .globl console_core_getc
43
44
45 /* -----------------------------------------------
46 * int console_core_init(uintptr_t base_addr,
47 * unsigned int uart_clk, unsigned int baud_rate)
48 * Function to initialize the console without a
49 * C Runtime to print debug information. This
50 * function will be accessed by console_init and
51 * crash reporting.
52 * In: r0 - console base address
53 * r1 - Uart clock in Hz
54 * r2 - Baud rate
55 * Out: return 1 on success else 0 on error
56 * Clobber list : r1, r2, r3
57 * -----------------------------------------------
58 */
59func console_core_init
60 /* Check the input base address */
61 cmp r0, #0
62 beq core_init_fail
63#if !PL011_GENERIC_UART
64 /* Check baud rate and uart clock for sanity */
65 cmp r1, #0
66 beq core_init_fail
67 cmp r2, #0
68 beq core_init_fail
69 /* Disable the UART before initialization */
70 ldr r3, [r0, #UARTCR]
71 bic r3, r3, #PL011_UARTCR_UARTEN
72 str r3, [r0, #UARTCR]
73 /* Program the baudrate */
74 /* Divisor = (Uart clock * 4) / baudrate */
75 lsl r1, r1, #2
76 udiv r2, r1, r2
77 /* IBRD = Divisor >> 6 */
78 lsr r1, r2, #6
79 /* Write the IBRD */
80 str r1, [r0, #UARTIBRD]
81 /* FBRD = Divisor & 0x3F */
82 and r1, r2, #0x3f
83 /* Write the FBRD */
84 str r1, [r0, #UARTFBRD]
85 mov r1, #PL011_LINE_CONTROL
86 str r1, [r0, #UARTLCR_H]
87 /* Clear any pending errors */
88 mov r1, #0
89 str r1, [r0, #UARTECR]
90 /* Enable tx, rx, and uart overall */
91 ldr r1, =(PL011_UARTCR_RXE | PL011_UARTCR_TXE | PL011_UARTCR_UARTEN)
92 str r1, [r0, #UARTCR]
93#endif
94 mov r0, #1
95 bx lr
96core_init_fail:
97 mov r0, #0
98 bx lr
99endfunc console_core_init
100
101 /* --------------------------------------------------------
102 * int console_core_putc(int c, uintptr_t base_addr)
103 * Function to output a character over the console. It
104 * returns the character printed on success or -1 on error.
105 * In : r0 - character to be printed
106 * r1 - console base address
107 * Out : return -1 on error else return character.
108 * Clobber list : r2
109 * --------------------------------------------------------
110 */
111func console_core_putc
112 /* Check the input parameter */
113 cmp r1, #0
114 beq putc_error
115 /* Prepend '\r' to '\n' */
116 cmp r0, #0xA
117 bne 2f
1181:
119 /* Check if the transmit FIFO is full */
120 ldr r2, [r1, #UARTFR]
121 tst r2, #PL011_UARTFR_TXFF_BIT
122 beq 1b
123 mov r2, #0xD
124 str r2, [r1, #UARTDR]
1252:
126 /* Check if the transmit FIFO is full */
127 ldr r2, [r1, #UARTFR]
128 tst r2, #PL011_UARTFR_TXFF_BIT
129 beq 2b
130 str r0, [r1, #UARTDR]
131 bx lr
132putc_error:
133 mov r0, #-1
134 bx lr
135endfunc console_core_putc
136
137 /* ---------------------------------------------
138 * int console_core_getc(uintptr_t base_addr)
139 * Function to get a character from the console.
140 * It returns the character grabbed on success
141 * or -1 on error.
142 * In : r0 - console base address
143 * Clobber list : r0, r1
144 * ---------------------------------------------
145 */
146func console_core_getc
147 cmp r0, #0
148 beq getc_error
1491:
150 /* Check if the receive FIFO is empty */
151 ldr r1, [r0, #UARTFR]
152 tst r1, #PL011_UARTFR_RXFE_BIT
153 beq 1b
154 ldr r1, [r0, #UARTDR]
155 mov r0, r1
156 bx lr
157getc_error:
158 mov r0, #-1
159 bx lr
160endfunc console_core_getc