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
|
static uintptr_t tb_uart_base;
static uint32 tb_uart_clock;
static bool tb_uart_ready;
static int tb_read_reg(const void *blob, int node, int index,
uintptr_t *base, uint32 *size)
{
const fdt32_t *prop;
int len;
int offset;
prop = fdt_getprop(blob, node, "reg", &len);
if (!prop)
return -1;
offset = index * 3;
if (offset + 3 > len / 4)
return -1;
*base = ((uintptr_t)fdt32_to_cpu(prop[offset]) << 32) |
fdt32_to_cpu(prop[offset + 1]);
*size = fdt32_to_cpu(prop[offset + 2]);
return 0;
}
static int tb_console_from_stdout_path(const void *blob)
{
const char *path;
const char *p;
char buf[32];
int node;
int chosen;
int len;
chosen = fdt_path_offset(blob, "/chosen");
if (chosen < 0)
return -1;
path = fdt_getprop(blob, chosen, "stdout-path", &len);
if (!path || len <= 0)
return -1;
p = strchr(path, ':');
len = p ? (int)(p - path) : strlen(path);
if (len >= (int)sizeof(buf))
len = sizeof(buf) - 1;
memcpy(buf, path, len);
buf[len] = 0;
if (buf[0] == '/')
node = fdt_path_offset(blob, buf);
else
node = fdt_path_offset(blob, fdt_get_alias(blob, buf));
if (node < 0)
return -1;
return node;
}
static int tb_console_scan(const void *blob)
{
const char *compat;
int node;
int len;
fdt_for_each_subnode(node, blob, 0) {
compat = fdt_getprop(blob, node, "compatible", &len);
if (!compat)
continue;
if (fdt_stringlist_contains(compat, len, "arm,pl011"))
return node;
}
return -1;
}
int tb_console_init(const void *dtb)
{
const fdt32_t *clk;
int node;
uint32 size;
uint32 baud;
int len;
node = tb_console_from_stdout_path(dtb);
if (node < 0)
node = tb_console_scan(dtb);
if (node < 0)
return -1;
if (tb_read_reg(dtb, node, 0, &tb_uart_base, &size))
return -1;
clk = fdt_getprop(dtb, node, "clock-frequency", &len);
tb_uart_clock = (clk && len == 4) ? fdt32_to_cpu(*clk) : 24000000;
{
const fdt32_t *cs;
int cslen;
cs = fdt_getprop(dtb, node, "current-speed", &cslen);
baud = (cs && cslen == 4) ? fdt32_to_cpu(*cs) : 115200;
}
pl011_init(tb_uart_base, tb_uart_clock, baud);
tb_uart_ready = true;
return 0;
}
static int tb_console_out(char c, void *state)
{
if (c == '\n')
pl011_putc(tb_uart_base, '\r');
pl011_putc(tb_uart_base, c);
return 1;
}
int _dprintf(const char *fmt, ...)
{
va_list ap;
int ret;
if (!tb_uart_ready)
return 0;
va_start(ap, fmt);
ret = _dvprintf(fmt, ap);
va_end(ap);
return ret;
}
int _dvprintf(const char *fmt, va_list ap)
{
if (!tb_uart_ready)
return 0;
return _printf_engine(tb_console_out, NULL, fmt, ap);
}
void platform_halt(void)
{
for (;;)
__asm__ volatile("wfe");
}
|