stm32mp_dt.c 12.7 KB
Newer Older
1
/*
2
 * Copyright (c) 2017-2020, ARM Limited and Contributors. All rights reserved.
3
4
5
6
7
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <assert.h>
8
#include <errno.h>
9

10
#include <libfdt.h>
11

12
#include <platform_def.h>
13
14

#include <common/debug.h>
15
#include <common/fdt_wrappers.h>
16
17
18
19
#include <drivers/st/stm32_gpio.h>
#include <drivers/st/stm32mp1_ddr.h>
#include <drivers/st/stm32mp1_ram.h>

20
21
#include <stm32mp_dt.h>

22
23
static int fdt_checked;

24
static void *fdt = (void *)(uintptr_t)STM32MP_DTB_BASE;
25
26
27

/*******************************************************************************
 * This function checks device tree file with its header.
28
 * Returns 0 on success and a negative FDT error code on failure.
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
 ******************************************************************************/
int dt_open_and_check(void)
{
	int ret = fdt_check_header(fdt);

	if (ret == 0) {
		fdt_checked = 1;
	}

	return ret;
}

/*******************************************************************************
 * This function gets the address of the DT.
 * If DT is OK, fdt_addr is filled with DT address.
 * Returns 1 if success, 0 otherwise.
 ******************************************************************************/
int fdt_get_address(void **fdt_addr)
{
	if (fdt_checked == 1) {
		*fdt_addr = fdt;
	}

	return fdt_checked;
}

/*******************************************************************************
 * This function check the presence of a node (generic use of fdt library).
57
 * Returns true if present, else return false.
58
59
60
61
62
63
64
65
66
67
68
69
 ******************************************************************************/
bool fdt_check_node(int node)
{
	int len;
	const char *cchar;

	cchar = fdt_get_name(fdt, node, &len);

	return (cchar != NULL) && (len >= 0);
}

/*******************************************************************************
70
 * This function return global node status (generic use of fdt library).
71
 ******************************************************************************/
72
uint8_t fdt_get_status(int node)
73
{
74
	uint8_t status = DT_DISABLED;
75
76
77
78
	int len;
	const char *cchar;

	cchar = fdt_getprop(fdt, node, "status", &len);
79
80
81
	if ((cchar == NULL) ||
	    (strncmp(cchar, "okay", (size_t)len) == 0)) {
		status |= DT_NON_SECURE;
82
83
84
85
	}

	cchar = fdt_getprop(fdt, node, "secure-status", &len);
	if (cchar == NULL) {
86
87
88
89
90
		if (status == DT_NON_SECURE) {
			status |= DT_SECURE;
		}
	} else if (strncmp(cchar, "okay", (size_t)len) == 0) {
		status |= DT_SECURE;
91
92
	}

93
	return status;
94
95
}

96
#if ENABLE_ASSERTIONS
97
98
99
100
101
102
103
104
/*******************************************************************************
 * This function returns the address cells from the node parent.
 * Returns:
 * - #address-cells value if success.
 * - invalid value if error.
 * - a default value if undefined #address-cells property as per libfdt
 *   implementation.
 ******************************************************************************/
105
static int fdt_get_node_parent_address_cells(int node)
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
{
	int parent;

	parent = fdt_parent_offset(fdt, node);
	if (parent < 0) {
		return -FDT_ERR_NOTFOUND;
	}

	return fdt_address_cells(fdt, parent);
}

/*******************************************************************************
 * This function returns the size cells from the node parent.
 * Returns:
 * - #size-cells value if success.
 * - invalid value if error.
 * - a default value if undefined #size-cells property as per libfdt
 *   implementation.
 ******************************************************************************/
125
static int fdt_get_node_parent_size_cells(int node)
126
127
128
129
130
131
132
133
134
135
{
	int parent;

	parent = fdt_parent_offset(fdt, node);
	if (parent < 0) {
		return -FDT_ERR_NOTFOUND;
	}

	return fdt_size_cells(fdt, parent);
}
136
#endif
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

/*******************************************************************************
 * This function fills reg node info (base & size) with an index found by
 * checking the reg-names node.
 * Returns 0 on success and a negative FDT error code on failure.
 ******************************************************************************/
int fdt_get_reg_props_by_name(int node, const char *name, uintptr_t *base,
			      size_t *size)
{
	const fdt32_t *cuint;
	int index, len;

	assert((fdt_get_node_parent_address_cells(node) == 1) &&
	       (fdt_get_node_parent_size_cells(node) == 1));

	index = fdt_stringlist_search(fdt, node, "reg-names", name);
	if (index < 0) {
		return index;
	}

	cuint = fdt_getprop(fdt, node, "reg", &len);
	if (cuint == NULL) {
		return -FDT_ERR_NOTFOUND;
	}

	if ((index * (int)sizeof(uint32_t)) > len) {
		return -FDT_ERR_BADVALUE;
	}

	cuint += index << 1;
	if (base != NULL) {
		*base = fdt32_to_cpu(*cuint);
	}
	cuint++;
	if (size != NULL) {
		*size = fdt32_to_cpu(*cuint);
	}

	return 0;
}

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
213
214
215
216
217
218
219
220
221
222
223
/*******************************************************************************
 * This function gets the stdout path node.
 * It reads the value indicated inside the device tree.
 * Returns node offset on success and a negative FDT error code on failure.
 ******************************************************************************/
static int dt_get_stdout_node_offset(void)
{
	int node;
	const char *cchar;

	node = fdt_path_offset(fdt, "/secure-chosen");
	if (node < 0) {
		node = fdt_path_offset(fdt, "/chosen");
		if (node < 0) {
			return -FDT_ERR_NOTFOUND;
		}
	}

	cchar = fdt_getprop(fdt, node, "stdout-path", NULL);
	if (cchar == NULL) {
		return -FDT_ERR_NOTFOUND;
	}

	node = -FDT_ERR_NOTFOUND;
	if (strchr(cchar, (int)':') != NULL) {
		const char *name;
		char *str = (char *)cchar;
		int len = 0;

		while (strncmp(":", str, 1)) {
			len++;
			str++;
		}

		name = fdt_get_alias_namelen(fdt, cchar, len);

		if (name != NULL) {
			node = fdt_path_offset(fdt, name);
		}
	} else {
		node = fdt_path_offset(fdt, cchar);
	}

	return node;
}

Yann Gautier's avatar
Yann Gautier committed
224
225
226
/*******************************************************************************
 * This function gets the stdout pin configuration information from the DT.
 * And then calls the sub-function to treat it and set GPIO registers.
227
 * Returns 0 on success and a negative FDT error code on failure.
Yann Gautier's avatar
Yann Gautier committed
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
 ******************************************************************************/
int dt_set_stdout_pinctrl(void)
{
	int node;

	node = dt_get_stdout_node_offset();
	if (node < 0) {
		return -FDT_ERR_NOTFOUND;
	}

	return dt_set_pinctrl_config(node);
}

/*******************************************************************************
 * This function fills the generic information from a given node.
 ******************************************************************************/
void dt_fill_device_info(struct dt_node_info *info, int node)
{
	const fdt32_t *cuint;

248
249
	assert(fdt_get_node_parent_address_cells(node) == 1);

Yann Gautier's avatar
Yann Gautier committed
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
	cuint = fdt_getprop(fdt, node, "reg", NULL);
	if (cuint != NULL) {
		info->base = fdt32_to_cpu(*cuint);
	} else {
		info->base = 0;
	}

	cuint = fdt_getprop(fdt, node, "clocks", NULL);
	if (cuint != NULL) {
		cuint++;
		info->clock = (int)fdt32_to_cpu(*cuint);
	} else {
		info->clock = -1;
	}

	cuint = fdt_getprop(fdt, node, "resets", NULL);
	if (cuint != NULL) {
		cuint++;
		info->reset = (int)fdt32_to_cpu(*cuint);
	} else {
		info->reset = -1;
	}

273
	info->status = fdt_get_status(node);
Yann Gautier's avatar
Yann Gautier committed
274
275
276
277
}

/*******************************************************************************
 * This function retrieve the generic information from DT.
278
 * Returns node on success and a negative FDT error code on failure.
Yann Gautier's avatar
Yann Gautier committed
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
 ******************************************************************************/
int dt_get_node(struct dt_node_info *info, int offset, const char *compat)
{
	int node;

	node = fdt_node_offset_by_compatible(fdt, offset, compat);
	if (node < 0) {
		return -FDT_ERR_NOTFOUND;
	}

	dt_fill_device_info(info, node);

	return node;
}

/*******************************************************************************
 * This function gets the UART instance info of stdout from the DT.
296
 * Returns node on success and a negative FDT error code on failure.
Yann Gautier's avatar
Yann Gautier committed
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
 ******************************************************************************/
int dt_get_stdout_uart_info(struct dt_node_info *info)
{
	int node;

	node = dt_get_stdout_node_offset();
	if (node < 0) {
		return -FDT_ERR_NOTFOUND;
	}

	dt_fill_device_info(info, node);

	return node;
}

312
313
/*******************************************************************************
 * This function gets DDR size information from the DT.
314
 * Returns value in bytes on success, and 0 on failure.
315
316
317
318
319
320
321
322
 ******************************************************************************/
uint32_t dt_get_ddr_size(void)
{
	int node;

	node = fdt_node_offset_by_compatible(fdt, -1, DT_DDR_COMPAT);
	if (node < 0) {
		INFO("%s: Cannot read DDR node in DT\n", __func__);
323
		return 0;
324
325
	}

326
	return fdt_read_uint32_default(fdt, node, "st,mem-size", 0);
327
328
}

329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*******************************************************************************
 * This function gets DDRCTRL base address information from the DT.
 * Returns value on success, and 0 on failure.
 ******************************************************************************/
uintptr_t dt_get_ddrctrl_base(void)
{
	int node;
	uint32_t array[4];

	node = fdt_node_offset_by_compatible(fdt, -1, DT_DDR_COMPAT);
	if (node < 0) {
		INFO("%s: Cannot read DDR node in DT\n", __func__);
		return 0;
	}

344
345
346
	assert((fdt_get_node_parent_address_cells(node) == 1) &&
	       (fdt_get_node_parent_size_cells(node) == 1));

347
	if (fdt_read_uint32_array(fdt, node, "reg", 4, array) < 0) {
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
		return 0;
	}

	return array[0];
}

/*******************************************************************************
 * This function gets DDRPHYC base address information from the DT.
 * Returns value on success, and 0 on failure.
 ******************************************************************************/
uintptr_t dt_get_ddrphyc_base(void)
{
	int node;
	uint32_t array[4];

	node = fdt_node_offset_by_compatible(fdt, -1, DT_DDR_COMPAT);
	if (node < 0) {
		INFO("%s: Cannot read DDR node in DT\n", __func__);
		return 0;
	}

369
370
371
	assert((fdt_get_node_parent_address_cells(node) == 1) &&
	       (fdt_get_node_parent_size_cells(node) == 1));

372
	if (fdt_read_uint32_array(fdt, node, "reg", 4, array) < 0) {
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
		return 0;
	}

	return array[2];
}

/*******************************************************************************
 * This function gets PWR base address information from the DT.
 * Returns value on success, and 0 on failure.
 ******************************************************************************/
uintptr_t dt_get_pwr_base(void)
{
	int node;
	const fdt32_t *cuint;

	node = fdt_node_offset_by_compatible(fdt, -1, DT_PWR_COMPAT);
	if (node < 0) {
		INFO("%s: Cannot read PWR node in DT\n", __func__);
		return 0;
	}

394
395
	assert(fdt_get_node_parent_address_cells(node) == 1);

396
397
398
399
400
401
402
403
	cuint = fdt_getprop(fdt, node, "reg", NULL);
	if (cuint == NULL) {
		return 0;
	}

	return fdt32_to_cpu(*cuint);
}

404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/*******************************************************************************
 * This function gets PWR VDD regulator voltage information from the DT.
 * Returns value in microvolts on success, and 0 on failure.
 ******************************************************************************/
uint32_t dt_get_pwr_vdd_voltage(void)
{
	int node, pwr_regulators_node;
	const fdt32_t *cuint;

	node = fdt_node_offset_by_compatible(fdt, -1, DT_PWR_COMPAT);
	if (node < 0) {
		INFO("%s: Cannot read PWR node in DT\n", __func__);
		return 0;
	}

	pwr_regulators_node = fdt_subnode_offset(fdt, node, "pwr-regulators");
420
	if (pwr_regulators_node < 0) {
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
		INFO("%s: Cannot read pwr-regulators node in DT\n", __func__);
		return 0;
	}

	cuint = fdt_getprop(fdt, pwr_regulators_node, "vdd-supply", NULL);
	if (cuint == NULL) {
		return 0;
	}

	node = fdt_node_offset_by_phandle(fdt, fdt32_to_cpu(*cuint));
	if (node < 0) {
		return 0;
	}

	cuint = fdt_getprop(fdt, node, "regulator-min-microvolt", NULL);
	if (cuint == NULL) {
		return 0;
	}

	return fdt32_to_cpu(*cuint);
}

/*******************************************************************************
 * This function gets SYSCFG base address information from the DT.
 * Returns value on success, and 0 on failure.
 ******************************************************************************/
uintptr_t dt_get_syscfg_base(void)
{
	int node;
	const fdt32_t *cuint;

	node = fdt_node_offset_by_compatible(fdt, -1, DT_SYSCFG_COMPAT);
	if (node < 0) {
		INFO("%s: Cannot read SYSCFG node in DT\n", __func__);
		return 0;
	}

458
459
	assert(fdt_get_node_parent_address_cells(node) == 1);

460
461
462
463
464
465
466
467
	cuint = fdt_getprop(fdt, node, "reg", NULL);
	if (cuint == NULL) {
		return 0;
	}

	return fdt32_to_cpu(*cuint);
}

Yann Gautier's avatar
Yann Gautier committed
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/*******************************************************************************
 * This function retrieves board model from DT
 * Returns string taken from model node, NULL otherwise
 ******************************************************************************/
const char *dt_get_board_model(void)
{
	int node = fdt_path_offset(fdt, "/");

	if (node < 0) {
		return NULL;
	}

	return (const char *)fdt_getprop(fdt, node, "model", NULL);
}