blob: 71b35cc692f5312514ea3fb2eea0df82c2f4fdca [file] [log] [blame]
Christophe Leroy3155b0a2023-04-04 12:42:15 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2010-2020 CS Group
4 * Charles Frey <charles.frey@c-s.fr>
5 * Florent Trinh Thai <florent.trinh-thai@c-s.fr>
6 * Christophe Leroy <christophe.leroy@c-s.fr>
7 *
8 * Common specific routines for the CS Group boards
9 */
10
11#include <fdt_support.h>
12
13static int fdt_set_node_and_value(void *blob, char *node, const char *prop,
14 void *var, int size)
15{
16 int ret, off;
17
18 off = fdt_path_offset(blob, node);
19
20 if (off < 0) {
21 printf("Cannot find %s node err:%s\n", node, fdt_strerror(off));
22
23 return off;
24 }
25
26 ret = fdt_setprop(blob, off, prop, var, size);
27
28 if (ret < 0)
29 printf("Cannot set %s/%s prop err: %s\n", node, prop, fdt_strerror(ret));
30
31 return ret;
32}
33
34/* Checks front/rear id and remove unneeded nodes from the blob */
35void ft_cleanup(void *blob, unsigned long id, const char *prop, const char *compatible)
36{
37 int off;
38
39 off = fdt_node_offset_by_compatible(blob, -1, compatible);
40
41 while (off != -FDT_ERR_NOTFOUND) {
42 const struct fdt_property *ids;
43 int nb_ids, idx;
44 int tmp = -1;
45
46 ids = fdt_get_property(blob, off, prop, &nb_ids);
47
48 for (idx = 0; idx < nb_ids; idx += 4) {
49 if (*((uint32_t *)&ids->data[idx]) == id)
50 break;
51 }
52
53 if (idx >= nb_ids)
54 fdt_del_node(blob, off);
55 else
56 tmp = off;
57
58 off = fdt_node_offset_by_compatible(blob, tmp, compatible);
59 }
60
61 fdt_set_node_and_value(blob, "/", prop, &id, sizeof(uint32_t));
62}