Merge with git://www.denx.de/git/u-boot.git
[platform/kernel/u-boot.git] / libfdt / fdt_wip.c
1 /*
2  * libfdt - Flat Device Tree manipulation
3  * Copyright (C) 2006 David Gibson, IBM Corporation.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public License
7  * as published by the Free Software Foundation; either version 2.1 of
8  * the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 #include "config.h"
20 #if CONFIG_OF_LIBFDT
21
22 #include "libfdt_env.h"
23
24 #include <fdt.h>
25 #include <libfdt.h>
26
27 #include "libfdt_internal.h"
28
29 int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name,
30                         const void *val, int len)
31 {
32         void *propval;
33         int proplen;
34
35         propval = fdt_getprop(fdt, nodeoffset, name, &proplen);
36         if (! propval)
37                 return proplen;
38
39         if (proplen != len)
40                 return -FDT_ERR_NOSPACE;
41
42         memcpy(propval, val, len);
43         return 0;
44 }
45
46 static void nop_region(void *start, int len)
47 {
48         uint32_t *p;
49
50         for (p = start; (void *)p < (start + len); p++)
51                 *p = cpu_to_fdt32(FDT_NOP);
52 }
53
54 int fdt_nop_property(void *fdt, int nodeoffset, const char *name)
55 {
56         struct fdt_property *prop;
57         int len;
58
59         prop = fdt_get_property(fdt, nodeoffset, name, &len);
60         if (! prop)
61                 return len;
62
63         nop_region(prop, len + sizeof(*prop));
64
65         return 0;
66 }
67
68 int _fdt_node_end_offset(void *fdt, int nodeoffset)
69 {
70         int level = 0;
71         uint32_t tag;
72         int offset, nextoffset;
73
74         tag = fdt_next_tag(fdt, nodeoffset, &nextoffset, NULL);
75         if (tag != FDT_BEGIN_NODE)
76                 return -FDT_ERR_BADOFFSET;
77         do {
78                 offset = nextoffset;
79                 tag = fdt_next_tag(fdt, offset, &nextoffset, NULL);
80
81                 switch (tag) {
82                 case FDT_END:
83                         return offset;
84
85                 case FDT_BEGIN_NODE:
86                         level++;
87                         break;
88
89                 case FDT_END_NODE:
90                         level--;
91                         break;
92
93                 case FDT_PROP:
94                 case FDT_NOP:
95                         break;
96
97                 default:
98                         return -FDT_ERR_BADSTRUCTURE;
99                 }
100         } while (level >= 0);
101
102         return nextoffset;
103 }
104
105 int fdt_nop_node(void *fdt, int nodeoffset)
106 {
107         int endoffset;
108
109         endoffset = _fdt_node_end_offset(fdt, nodeoffset);
110         if (endoffset < 0)
111                 return endoffset;
112
113         nop_region(fdt_offset_ptr(fdt, nodeoffset, 0), endoffset - nodeoffset);
114         return 0;
115 }
116
117 /*
118  * Replace a reserve map entry in the nth slot.
119  */
120 int fdt_replace_reservemap_entry(void *fdt, int n, uint64_t addr, uint64_t size)
121 {
122         struct fdt_reserve_entry *re;
123         int  used;
124         int  total;
125         int  err;
126
127         err = fdt_num_reservemap(fdt, &used, &total);
128         if (err != 0)
129                 return err;
130
131         if (n >= total)
132                 return -FDT_ERR_NOSPACE;
133         re = (struct fdt_reserve_entry *)
134                 (fdt + fdt_off_mem_rsvmap(fdt) +
135                  (n * sizeof(struct fdt_reserve_entry)));
136         re->address = cpu_to_fdt64(addr);
137         re->size    = cpu_to_fdt64(size);
138
139         return 0;
140 }
141
142 #endif /* CONFIG_OF_LIBFDT */