1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
/*
* Copyright (c) 2017, ARM Limited and Contributors. All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <asm_macros.S>
#define UNIPHIER_UART_BASE 0x54006800
#define UNIPHIER_UART_END 0x54006c00
#define UNIPHIER_UART_OFFSET 0x100
#define UNIPHIER_UART_RX 0x00 /* In: Receive buffer */
#define UNIPHIER_UART_TX 0x00 /* Out: Transmit buffer */
#define UNIPHIER_UART_FCR 0x0c /* Char/FIFO Control Register */
#define UNIPHIER_UART_FCR_ENABLE_FIFO 0x01 /* Enable the FIFO */
#define UNIPHIER_UART_LCR_MCR 0x10 /* Line/Modem Control Register */
#define UNIPHIER_UART_LCR_WLEN8 0x03 /* Wordlength: 8 bits */
#define UNIPHIER_UART_LSR 0x14 /* Line Status Register */
#define UNIPHIER_UART_LSR_TEMT_BIT 6 /* Transmitter empty */
#define UNIPHIER_UART_LSR_THRE_BIT 5 /* Transmit-hold-register empty */
#define UNIPHIER_UART_LSR_DR_BIT 0 /* Receiver data ready */
#define UNIPHIER_UART_DLR 0x24 /* Divisor Latch Register */
/*
* Uncomment for debug
*/
/* #define UNIPHIER_UART_INIT_DIVISOR */
#define UNIPHIER_UART_DEFAULT_BASE (UNIPHIER_UART_BASE)
#define UNIPHIER_UART_CLK_RATE 58820000
#define UNIPHIER_UART_DEFAULT_BAUDRATE 115200
/*
* In: x0 - console base address
* w1 - uart clock in Hz
* w2 - baud rate
* Out: return 1 on success, or 0 on error
*/
.globl console_core_init
func console_core_init
cbz x0, 1f
#ifdef UNIPHIER_UART_INIT_DIVISOR
cbz w1, 1f
cbz w2, 1f
/* divisor = uart_clock / (16 * baud_rate) */
udiv w2, w1, w2
lsr w2, w2, #4
#endif
/* Make sure the transmitter is empty before the divisor set/change */
0: ldr w1, [x0, #UNIPHIER_UART_LSR]
tbz w1, #UNIPHIER_UART_LSR_TEMT_BIT, 0b
#ifdef UNIPHIER_UART_INIT_DIVISOR
str w2, [x0, #UNIPHIER_UART_DLR]
#endif
mov w2, #UNIPHIER_UART_FCR_ENABLE_FIFO
str w2, [x0, #UNIPHIER_UART_FCR]
mov w2, #(UNIPHIER_UART_LCR_WLEN8 << 8)
str w2, [x0, #UNIPHIER_UART_LCR_MCR]
mov w0, #1
ret
1: mov w0, #0
ret
endfunc console_core_init
/*
* In: w0 - character to be printed
* x1 - console base address
* Out: return the character written, or -1 on error
* Clobber: x2
*/
.globl console_core_putc
func console_core_putc
/* Error out if the console is not initialized */
cbz x1, 2f
/* Wait until the transmitter FIFO gets empty */
0: ldr w2, [x1, #UNIPHIER_UART_LSR]
tbz w2, #UNIPHIER_UART_LSR_THRE_BIT, 0b
mov w2, w0
1: str w2, [x1, #UNIPHIER_UART_TX]
cmp w2, #'\n'
b.ne 3f
mov w2, #'\r' /* Append '\r' to '\n' */
b 1b
2: mov w0, #-1
3: ret
endfunc console_core_putc
/*
* In: x0 - console base address
* Out: return the character read
* Clobber: x1
*/
.globl console_core_getc
func console_core_getc
/* Error out if the console is not initialized */
cbz x0, 1f
/* Wait while the receiver FIFO is empty */
0: ldr w1, [x0, #UNIPHIER_UART_LSR]
tbz w1, #UNIPHIER_UART_LSR_DR_BIT, 0b
ldr w0, [x0, #UNIPHIER_UART_RX]
ret
1: mov w0, #-1
ret
endfunc console_core_getc
/*
* In: x0 - console base address
* Out: return 0, or -1 on error
* Clobber: x1
*/
.global console_core_flush
func console_core_flush
/* Error out if the console is not initialized */
cbz x0, 1f
/* wait until the transmitter gets empty */
0: ldr w1, [x0, #UNIPHIER_UART_LSR]
tbz w1, #UNIPHIER_UART_LSR_TEMT_BIT, 0b
mov w0, #0
ret
1: mov w0, #-1
ret
endfunc console_core_flush
/* find initialized UART port */
.macro uniphier_console_get_base base, tmpx, tmpw
ldr \base, =UNIPHIER_UART_BASE
0000: ldr \tmpw, [\base, #UNIPHIER_UART_DLR]
mvn \tmpw, \tmpw
uxth \tmpw, \tmpw
cbnz \tmpw, 0001f
add \base, \base, #UNIPHIER_UART_OFFSET
ldr \tmpx, =UNIPHIER_UART_END
cmp \base, \tmpx
b.lo 0000b
mov \base, #0
0001:
.endm
/*
* int plat_crash_console_init(void)
* Clobber: x0-x2
*/
.globl plat_crash_console_init
func plat_crash_console_init
#ifdef UNIPHIER_UART_INIT_DIVISOR
ldr x0, =UNIPHIER_UART_DEFAULT_BASE
ldr x1, =UNIPHIER_UART_CLK_RATE
ldr x2, =UNIPHIER_UART_DEFAULT_BAUDRATE
b console_core_init
#else
ret
#endif
endfunc plat_crash_console_init
/*
* int plat_crash_console_putc(int c)
* Clobber: x1, x2
*/
.globl plat_crash_console_putc
func plat_crash_console_putc
#ifdef UNIPHIER_UART_INIT_DIVISOR
ldr x1, =UNIPHIER_UART_DEFAULT_BASE
#else
uniphier_console_get_base x1, x2, w2
#endif
b console_core_putc
endfunc plat_crash_console_putc
/*
* int plat_crash_console_flush(void)
* Clobber: x0, x1
*/
.global plat_crash_console_flush
func plat_crash_console_flush
#ifdef UNIPHIER_UART_INIT_DIVISOR
ldr x0, =UNIPHIER_UART_DEFAULT_BASE
#else
uniphier_console_get_base x0, x1, w1
#endif
b console_core_flush
endfunc plat_crash_console_flush
/*
* void uniphier_console_setup(void)
* Clobber: x0-x2
*/
.globl uniphier_console_setup
func uniphier_console_setup
#ifdef UNIPHIER_UART_INIT_DIVISOR
ldr x0, =UNIPHIER_UART_DEFAULT_BASE
ldr w1, =UNIPHIER_UART_CLK_RATE
ldr w2, =UNIPHIER_UART_DEFAULT_BAUDRATE
#else
uniphier_console_get_base x0, x1, w1
mov w1, #0
mov w2, #0
#endif
b console_init
endfunc uniphier_console_setup
|