blob: 11e3df777d711bed1b69f808fd41003ef0c4858e [file] [log] [blame]
Soby Mathew7abebe82016-08-08 12:38:52 +01001/*
2 * Copyright (c) 2013-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/aarch64/console.S"
39
40
41 .globl console_core_init
42 .globl console_core_putc
43 .globl console_core_getc
44
45
46 /* -----------------------------------------------
47 * int console_core_init(uintptr_t base_addr,
48 * unsigned int uart_clk, unsigned int baud_rate)
49 * Function to initialize the console without a
50 * C Runtime to print debug information. This
51 * function will be accessed by console_init and
52 * crash reporting.
53 * In: x0 - console base address
54 * w1 - Uart clock in Hz
55 * w2 - Baud rate
56 * Out: return 1 on success else 0 on error
57 * Clobber list : x1, x2, x3, x4
58 * -----------------------------------------------
59 */
60func console_core_init
61 /* Check the input base address */
62 cbz x0, core_init_fail
63#if !PL011_GENERIC_UART
64 /* Check baud rate and uart clock for sanity */
65 cbz w1, core_init_fail
66 cbz w2, core_init_fail
67 /* Disable uart before programming */
68 ldr w3, [x0, #UARTCR]
69 mov w4, #PL011_UARTCR_UARTEN
70 bic w3, w3, w4
71 str w3, [x0, #UARTCR]
72 /* Program the baudrate */
73 /* Divisor = (Uart clock * 4) / baudrate */
74 lsl w1, w1, #2
75 udiv w2, w1, w2
76 /* IBRD = Divisor >> 6 */
77 lsr w1, w2, #6
78 /* Write the IBRD */
79 str w1, [x0, #UARTIBRD]
80 /* FBRD = Divisor & 0x3F */
81 and w1, w2, #0x3f
82 /* Write the FBRD */
83 str w1, [x0, #UARTFBRD]
84 mov w1, #PL011_LINE_CONTROL
85 str w1, [x0, #UARTLCR_H]
86 /* Clear any pending errors */
87 str wzr, [x0, #UARTECR]
88 /* Enable tx, rx, and uart overall */
89 mov w1, #(PL011_UARTCR_RXE | PL011_UARTCR_TXE | PL011_UARTCR_UARTEN)
90 str w1, [x0, #UARTCR]
91#endif
92 mov w0, #1
93 ret
94core_init_fail:
95 mov w0, wzr
96 ret
97endfunc console_core_init
98
99 /* --------------------------------------------------------
100 * int console_core_putc(int c, uintptr_t base_addr)
101 * Function to output a character over the console. It
102 * returns the character printed on success or -1 on error.
103 * In : w0 - character to be printed
104 * x1 - console base address
105 * Out : return -1 on error else return character.
106 * Clobber list : x2
107 * --------------------------------------------------------
108 */
109func console_core_putc
110 /* Check the input parameter */
111 cbz x1, putc_error
112 /* Prepend '\r' to '\n' */
113 cmp w0, #0xA
114 b.ne 2f
1151:
116 /* Check if the transmit FIFO is full */
117 ldr w2, [x1, #UARTFR]
118 tbnz w2, #PL011_UARTFR_TXFF_BIT, 1b
119 mov w2, #0xD
120 str w2, [x1, #UARTDR]
1212:
122 /* Check if the transmit FIFO is full */
123 ldr w2, [x1, #UARTFR]
124 tbnz w2, #PL011_UARTFR_TXFF_BIT, 2b
125 str w0, [x1, #UARTDR]
126 ret
127putc_error:
128 mov w0, #-1
129 ret
130endfunc console_core_putc
131
132 /* ---------------------------------------------
133 * int console_core_getc(uintptr_t base_addr)
134 * Function to get a character from the console.
135 * It returns the character grabbed on success
136 * or -1 on error.
137 * In : x0 - console base address
138 * Clobber list : x0, x1
139 * ---------------------------------------------
140 */
141func console_core_getc
142 cbz x0, getc_error
1431:
144 /* Check if the receive FIFO is empty */
145 ldr w1, [x0, #UARTFR]
146 tbnz w1, #PL011_UARTFR_RXFE_BIT, 1b
147 ldr w1, [x0, #UARTDR]
148 mov w0, w1
149 ret
150getc_error:
151 mov w0, #-1
152 ret
153endfunc console_core_getc