xref: /OK3568_Linux_fs/u-boot/common/fdt_support.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * (C) Copyright 2007
3*4882a593Smuzhiyun  * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright 2010-2011 Freescale Semiconductor, Inc.
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * SPDX-License-Identifier:	GPL-2.0+
8*4882a593Smuzhiyun  */
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #include <common.h>
11*4882a593Smuzhiyun #include <android_image.h>
12*4882a593Smuzhiyun #include <exports.h>
13*4882a593Smuzhiyun #include <fdt_support.h>
14*4882a593Smuzhiyun #include <fdtdec.h>
15*4882a593Smuzhiyun #include <inttypes.h>
16*4882a593Smuzhiyun #include <malloc.h>
17*4882a593Smuzhiyun #ifdef CONFIG_MTD_BLK
18*4882a593Smuzhiyun #include <mtd_blk.h>
19*4882a593Smuzhiyun #endif
20*4882a593Smuzhiyun #include <stdio_dev.h>
21*4882a593Smuzhiyun #include <asm/arch/hotkey.h>
22*4882a593Smuzhiyun #include <asm/global_data.h>
23*4882a593Smuzhiyun #include <linux/ctype.h>
24*4882a593Smuzhiyun #include <linux/libfdt.h>
25*4882a593Smuzhiyun #include <linux/types.h>
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun DECLARE_GLOBAL_DATA_PTR;
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun /**
30*4882a593Smuzhiyun  * fdt_getprop_u32_default_node - Return a node's property or a default
31*4882a593Smuzhiyun  *
32*4882a593Smuzhiyun  * @fdt: ptr to device tree
33*4882a593Smuzhiyun  * @off: offset of node
34*4882a593Smuzhiyun  * @cell: cell offset in property
35*4882a593Smuzhiyun  * @prop: property name
36*4882a593Smuzhiyun  * @dflt: default value if the property isn't found
37*4882a593Smuzhiyun  *
38*4882a593Smuzhiyun  * Convenience function to return a node's property or a default value if
39*4882a593Smuzhiyun  * the property doesn't exist.
40*4882a593Smuzhiyun  */
fdt_getprop_u32_default_node(const void * fdt,int off,int cell,const char * prop,const u32 dflt)41*4882a593Smuzhiyun u32 fdt_getprop_u32_default_node(const void *fdt, int off, int cell,
42*4882a593Smuzhiyun 				const char *prop, const u32 dflt)
43*4882a593Smuzhiyun {
44*4882a593Smuzhiyun 	const fdt32_t *val;
45*4882a593Smuzhiyun 	int len;
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun 	val = fdt_getprop(fdt, off, prop, &len);
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun 	/* Check if property exists */
50*4882a593Smuzhiyun 	if (!val)
51*4882a593Smuzhiyun 		return dflt;
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun 	/* Check if property is long enough */
54*4882a593Smuzhiyun 	if (len < ((cell + 1) * sizeof(uint32_t)))
55*4882a593Smuzhiyun 		return dflt;
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun 	return fdt32_to_cpu(*val);
58*4882a593Smuzhiyun }
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun /**
61*4882a593Smuzhiyun  * fdt_getprop_u32_default - Find a node and return it's property or a default
62*4882a593Smuzhiyun  *
63*4882a593Smuzhiyun  * @fdt: ptr to device tree
64*4882a593Smuzhiyun  * @path: path of node
65*4882a593Smuzhiyun  * @prop: property name
66*4882a593Smuzhiyun  * @dflt: default value if the property isn't found
67*4882a593Smuzhiyun  *
68*4882a593Smuzhiyun  * Convenience function to find a node and return it's property or a
69*4882a593Smuzhiyun  * default value if it doesn't exist.
70*4882a593Smuzhiyun  */
fdt_getprop_u32_default(const void * fdt,const char * path,const char * prop,const u32 dflt)71*4882a593Smuzhiyun u32 fdt_getprop_u32_default(const void *fdt, const char *path,
72*4882a593Smuzhiyun 				const char *prop, const u32 dflt)
73*4882a593Smuzhiyun {
74*4882a593Smuzhiyun 	int off;
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun 	off = fdt_path_offset(fdt, path);
77*4882a593Smuzhiyun 	if (off < 0)
78*4882a593Smuzhiyun 		return dflt;
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun 	return fdt_getprop_u32_default_node(fdt, off, 0, prop, dflt);
81*4882a593Smuzhiyun }
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun /**
84*4882a593Smuzhiyun  * fdt_find_and_setprop: Find a node and set it's property
85*4882a593Smuzhiyun  *
86*4882a593Smuzhiyun  * @fdt: ptr to device tree
87*4882a593Smuzhiyun  * @node: path of node
88*4882a593Smuzhiyun  * @prop: property name
89*4882a593Smuzhiyun  * @val: ptr to new value
90*4882a593Smuzhiyun  * @len: length of new property value
91*4882a593Smuzhiyun  * @create: flag to create the property if it doesn't exist
92*4882a593Smuzhiyun  *
93*4882a593Smuzhiyun  * Convenience function to directly set a property given the path to the node.
94*4882a593Smuzhiyun  */
fdt_find_and_setprop(void * fdt,const char * node,const char * prop,const void * val,int len,int create)95*4882a593Smuzhiyun int fdt_find_and_setprop(void *fdt, const char *node, const char *prop,
96*4882a593Smuzhiyun 			 const void *val, int len, int create)
97*4882a593Smuzhiyun {
98*4882a593Smuzhiyun 	int nodeoff = fdt_path_offset(fdt, node);
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun 	if (nodeoff < 0)
101*4882a593Smuzhiyun 		return nodeoff;
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun 	if ((!create) && (fdt_get_property(fdt, nodeoff, prop, NULL) == NULL))
104*4882a593Smuzhiyun 		return 0; /* create flag not set; so exit quietly */
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun 	return fdt_setprop(fdt, nodeoff, prop, val, len);
107*4882a593Smuzhiyun }
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun /**
110*4882a593Smuzhiyun  * fdt_find_or_add_subnode() - find or possibly add a subnode of a given node
111*4882a593Smuzhiyun  *
112*4882a593Smuzhiyun  * @fdt: pointer to the device tree blob
113*4882a593Smuzhiyun  * @parentoffset: structure block offset of a node
114*4882a593Smuzhiyun  * @name: name of the subnode to locate
115*4882a593Smuzhiyun  *
116*4882a593Smuzhiyun  * fdt_subnode_offset() finds a subnode of the node with a given name.
117*4882a593Smuzhiyun  * If the subnode does not exist, it will be created.
118*4882a593Smuzhiyun  */
fdt_find_or_add_subnode(void * fdt,int parentoffset,const char * name)119*4882a593Smuzhiyun int fdt_find_or_add_subnode(void *fdt, int parentoffset, const char *name)
120*4882a593Smuzhiyun {
121*4882a593Smuzhiyun 	int offset;
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun 	offset = fdt_subnode_offset(fdt, parentoffset, name);
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun 	if (offset == -FDT_ERR_NOTFOUND)
126*4882a593Smuzhiyun 		offset = fdt_add_subnode(fdt, parentoffset, name);
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun 	if (offset < 0)
129*4882a593Smuzhiyun 		printf("%s: %s: %s\n", __func__, name, fdt_strerror(offset));
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun 	return offset;
132*4882a593Smuzhiyun }
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun /* rename to CONFIG_OF_STDOUT_PATH ? */
135*4882a593Smuzhiyun #if defined(OF_STDOUT_PATH)
fdt_fixup_stdout(void * fdt,int chosenoff)136*4882a593Smuzhiyun static int fdt_fixup_stdout(void *fdt, int chosenoff)
137*4882a593Smuzhiyun {
138*4882a593Smuzhiyun 	return fdt_setprop(fdt, chosenoff, "linux,stdout-path",
139*4882a593Smuzhiyun 			      OF_STDOUT_PATH, strlen(OF_STDOUT_PATH) + 1);
140*4882a593Smuzhiyun }
141*4882a593Smuzhiyun #elif defined(CONFIG_OF_STDOUT_VIA_ALIAS) && defined(CONFIG_CONS_INDEX)
fdt_fixup_stdout(void * fdt,int chosenoff)142*4882a593Smuzhiyun static int fdt_fixup_stdout(void *fdt, int chosenoff)
143*4882a593Smuzhiyun {
144*4882a593Smuzhiyun 	int err;
145*4882a593Smuzhiyun 	int aliasoff;
146*4882a593Smuzhiyun 	char sername[9] = { 0 };
147*4882a593Smuzhiyun 	const void *path;
148*4882a593Smuzhiyun 	int len;
149*4882a593Smuzhiyun 	char tmp[256]; /* long enough */
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun 	sprintf(sername, "serial%d", CONFIG_CONS_INDEX - 1);
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun 	aliasoff = fdt_path_offset(fdt, "/aliases");
154*4882a593Smuzhiyun 	if (aliasoff < 0) {
155*4882a593Smuzhiyun 		err = aliasoff;
156*4882a593Smuzhiyun 		goto noalias;
157*4882a593Smuzhiyun 	}
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun 	path = fdt_getprop(fdt, aliasoff, sername, &len);
160*4882a593Smuzhiyun 	if (!path) {
161*4882a593Smuzhiyun 		err = len;
162*4882a593Smuzhiyun 		goto noalias;
163*4882a593Smuzhiyun 	}
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun 	/* fdt_setprop may break "path" so we copy it to tmp buffer */
166*4882a593Smuzhiyun 	memcpy(tmp, path, len);
167*4882a593Smuzhiyun 
168*4882a593Smuzhiyun 	err = fdt_setprop(fdt, chosenoff, "linux,stdout-path", tmp, len);
169*4882a593Smuzhiyun 	if (err < 0)
170*4882a593Smuzhiyun 		printf("WARNING: could not set linux,stdout-path %s.\n",
171*4882a593Smuzhiyun 		       fdt_strerror(err));
172*4882a593Smuzhiyun 
173*4882a593Smuzhiyun 	return err;
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun noalias:
176*4882a593Smuzhiyun 	printf("WARNING: %s: could not read %s alias: %s\n",
177*4882a593Smuzhiyun 	       __func__, sername, fdt_strerror(err));
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun 	return 0;
180*4882a593Smuzhiyun }
181*4882a593Smuzhiyun #else
fdt_fixup_stdout(void * fdt,int chosenoff)182*4882a593Smuzhiyun static int fdt_fixup_stdout(void *fdt, int chosenoff)
183*4882a593Smuzhiyun {
184*4882a593Smuzhiyun 	return 0;
185*4882a593Smuzhiyun }
186*4882a593Smuzhiyun #endif
187*4882a593Smuzhiyun 
fdt_setprop_uxx(void * fdt,int nodeoffset,const char * name,uint64_t val,int is_u64)188*4882a593Smuzhiyun int fdt_setprop_uxx(void *fdt, int nodeoffset, const char *name,
189*4882a593Smuzhiyun 		    uint64_t val, int is_u64)
190*4882a593Smuzhiyun {
191*4882a593Smuzhiyun 	if (is_u64)
192*4882a593Smuzhiyun 		return fdt_setprop_u64(fdt, nodeoffset, name, val);
193*4882a593Smuzhiyun 	else
194*4882a593Smuzhiyun 		return fdt_setprop_u32(fdt, nodeoffset, name, (uint32_t)val);
195*4882a593Smuzhiyun }
196*4882a593Smuzhiyun 
fdt_root(void * fdt)197*4882a593Smuzhiyun int fdt_root(void *fdt)
198*4882a593Smuzhiyun {
199*4882a593Smuzhiyun 	char *serial;
200*4882a593Smuzhiyun 	int err;
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun 	err = fdt_check_header(fdt);
203*4882a593Smuzhiyun 	if (err < 0) {
204*4882a593Smuzhiyun 		printf("fdt_root: %s\n", fdt_strerror(err));
205*4882a593Smuzhiyun 		return err;
206*4882a593Smuzhiyun 	}
207*4882a593Smuzhiyun 
208*4882a593Smuzhiyun 	serial = env_get("serial#");
209*4882a593Smuzhiyun 	if (serial) {
210*4882a593Smuzhiyun 		err = fdt_setprop(fdt, 0, "serial-number", serial,
211*4882a593Smuzhiyun 				  strlen(serial) + 1);
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun 		if (err < 0) {
214*4882a593Smuzhiyun 			printf("WARNING: could not set serial-number %s.\n",
215*4882a593Smuzhiyun 			       fdt_strerror(err));
216*4882a593Smuzhiyun 			return err;
217*4882a593Smuzhiyun 		}
218*4882a593Smuzhiyun 	}
219*4882a593Smuzhiyun 
220*4882a593Smuzhiyun 	return 0;
221*4882a593Smuzhiyun }
222*4882a593Smuzhiyun 
fdt_initrd(void * fdt,ulong initrd_start,ulong initrd_end)223*4882a593Smuzhiyun int fdt_initrd(void *fdt, ulong initrd_start, ulong initrd_end)
224*4882a593Smuzhiyun {
225*4882a593Smuzhiyun 	int   nodeoffset;
226*4882a593Smuzhiyun 	int   err, j, total;
227*4882a593Smuzhiyun 	int is_u64;
228*4882a593Smuzhiyun 	uint64_t addr, size;
229*4882a593Smuzhiyun 
230*4882a593Smuzhiyun 	/* just return if the size of initrd is zero */
231*4882a593Smuzhiyun 	if (initrd_start == initrd_end)
232*4882a593Smuzhiyun 		return 0;
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun 	/* find or create "/chosen" node. */
235*4882a593Smuzhiyun 	nodeoffset = fdt_find_or_add_subnode(fdt, 0, "chosen");
236*4882a593Smuzhiyun 	if (nodeoffset < 0)
237*4882a593Smuzhiyun 		return nodeoffset;
238*4882a593Smuzhiyun 
239*4882a593Smuzhiyun 	total = fdt_num_mem_rsv(fdt);
240*4882a593Smuzhiyun 
241*4882a593Smuzhiyun 	/*
242*4882a593Smuzhiyun 	 * Look for an existing entry and update it.  If we don't find
243*4882a593Smuzhiyun 	 * the entry, we will j be the next available slot.
244*4882a593Smuzhiyun 	 */
245*4882a593Smuzhiyun 	for (j = 0; j < total; j++) {
246*4882a593Smuzhiyun 		err = fdt_get_mem_rsv(fdt, j, &addr, &size);
247*4882a593Smuzhiyun 		if (addr == initrd_start) {
248*4882a593Smuzhiyun 			fdt_del_mem_rsv(fdt, j);
249*4882a593Smuzhiyun 			break;
250*4882a593Smuzhiyun 		}
251*4882a593Smuzhiyun 	}
252*4882a593Smuzhiyun 
253*4882a593Smuzhiyun 	err = fdt_add_mem_rsv(fdt, initrd_start, initrd_end - initrd_start);
254*4882a593Smuzhiyun 	if (err < 0) {
255*4882a593Smuzhiyun 		printf("fdt_initrd: %s\n", fdt_strerror(err));
256*4882a593Smuzhiyun 		return err;
257*4882a593Smuzhiyun 	}
258*4882a593Smuzhiyun 
259*4882a593Smuzhiyun 	is_u64 = (fdt_address_cells(fdt, 0) == 2);
260*4882a593Smuzhiyun 
261*4882a593Smuzhiyun 	err = fdt_setprop_uxx(fdt, nodeoffset, "linux,initrd-start",
262*4882a593Smuzhiyun 			      (uint64_t)initrd_start, is_u64);
263*4882a593Smuzhiyun 
264*4882a593Smuzhiyun 	if (err < 0) {
265*4882a593Smuzhiyun 		printf("WARNING: could not set linux,initrd-start %s.\n",
266*4882a593Smuzhiyun 		       fdt_strerror(err));
267*4882a593Smuzhiyun 		return err;
268*4882a593Smuzhiyun 	}
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun 	err = fdt_setprop_uxx(fdt, nodeoffset, "linux,initrd-end",
271*4882a593Smuzhiyun 			      (uint64_t)initrd_end, is_u64);
272*4882a593Smuzhiyun 
273*4882a593Smuzhiyun 	if (err < 0) {
274*4882a593Smuzhiyun 		printf("WARNING: could not set linux,initrd-end %s.\n",
275*4882a593Smuzhiyun 		       fdt_strerror(err));
276*4882a593Smuzhiyun 
277*4882a593Smuzhiyun 		return err;
278*4882a593Smuzhiyun 	}
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun 	return 0;
281*4882a593Smuzhiyun }
282*4882a593Smuzhiyun 
fdt_bootargs_append(void * fdt,char * data)283*4882a593Smuzhiyun int fdt_bootargs_append(void *fdt, char *data)
284*4882a593Smuzhiyun {
285*4882a593Smuzhiyun 	const char *arr_bootargs[] = { "bootargs", "bootargs_ext" };
286*4882a593Smuzhiyun 	int nodeoffset, len;
287*4882a593Smuzhiyun 	const char *bootargs;
288*4882a593Smuzhiyun 	char *str;
289*4882a593Smuzhiyun 	int i, ret = 0;
290*4882a593Smuzhiyun 
291*4882a593Smuzhiyun 	if (!data)
292*4882a593Smuzhiyun 		return 0;
293*4882a593Smuzhiyun 
294*4882a593Smuzhiyun 	/* find or create "/chosen" node. */
295*4882a593Smuzhiyun 	nodeoffset = fdt_find_or_add_subnode(fdt, 0, "chosen");
296*4882a593Smuzhiyun 	if (nodeoffset < 0)
297*4882a593Smuzhiyun 		return nodeoffset;
298*4882a593Smuzhiyun 
299*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(arr_bootargs); i++) {
300*4882a593Smuzhiyun 		bootargs = fdt_getprop(fdt, nodeoffset,
301*4882a593Smuzhiyun 				       arr_bootargs[i], NULL);
302*4882a593Smuzhiyun 		if (bootargs) {
303*4882a593Smuzhiyun 			len = strlen(bootargs) + strlen(data) + 2;
304*4882a593Smuzhiyun 			str = malloc(len);
305*4882a593Smuzhiyun 			if (!str)
306*4882a593Smuzhiyun 				return -ENOMEM;
307*4882a593Smuzhiyun 
308*4882a593Smuzhiyun 			fdt_increase_size(fdt, 512);
309*4882a593Smuzhiyun 			snprintf(str, len, "%s %s", bootargs, data);
310*4882a593Smuzhiyun 			ret = fdt_setprop(fdt, nodeoffset, arr_bootargs[i],
311*4882a593Smuzhiyun 					  str, len);
312*4882a593Smuzhiyun 			if (ret < 0)
313*4882a593Smuzhiyun 				printf("WARNING: could not set bootargs %s.\n", fdt_strerror(ret));
314*4882a593Smuzhiyun 
315*4882a593Smuzhiyun 			free(str);
316*4882a593Smuzhiyun 			break;
317*4882a593Smuzhiyun 		}
318*4882a593Smuzhiyun 	}
319*4882a593Smuzhiyun 
320*4882a593Smuzhiyun 	return ret;
321*4882a593Smuzhiyun }
322*4882a593Smuzhiyun 
fdt_bootargs_append_ab(void * fdt,char * slot)323*4882a593Smuzhiyun int fdt_bootargs_append_ab(void *fdt, char *slot)
324*4882a593Smuzhiyun {
325*4882a593Smuzhiyun 	char *str;
326*4882a593Smuzhiyun 	int len, ret = 0;
327*4882a593Smuzhiyun 
328*4882a593Smuzhiyun 	if (!slot)
329*4882a593Smuzhiyun 		return 0;
330*4882a593Smuzhiyun 
331*4882a593Smuzhiyun 	len = strlen(ANDROID_ARG_SLOT_SUFFIX) + strlen(slot) + 1;
332*4882a593Smuzhiyun 	str = malloc(len);
333*4882a593Smuzhiyun 	if (!str)
334*4882a593Smuzhiyun 		return -ENOMEM;
335*4882a593Smuzhiyun 
336*4882a593Smuzhiyun 	snprintf(str, len, "%s%s", ANDROID_ARG_SLOT_SUFFIX, slot);
337*4882a593Smuzhiyun 	ret = fdt_bootargs_append(fdt, str);
338*4882a593Smuzhiyun 	if (ret)
339*4882a593Smuzhiyun 		printf("Apend slot info to bootargs fail");
340*4882a593Smuzhiyun 
341*4882a593Smuzhiyun 	free(str);
342*4882a593Smuzhiyun 
343*4882a593Smuzhiyun 	return ret;
344*4882a593Smuzhiyun }
345*4882a593Smuzhiyun 
346*4882a593Smuzhiyun /**
347*4882a593Smuzhiyun  * board_fdt_chosen_bootargs - boards may override this function to use
348*4882a593Smuzhiyun  *                             alternative kernel command line arguments
349*4882a593Smuzhiyun  */
board_fdt_chosen_bootargs(void * fdt)350*4882a593Smuzhiyun __weak char *board_fdt_chosen_bootargs(void *fdt)
351*4882a593Smuzhiyun {
352*4882a593Smuzhiyun 	return env_get("bootargs");
353*4882a593Smuzhiyun }
354*4882a593Smuzhiyun 
fdt_chosen(void * fdt)355*4882a593Smuzhiyun int fdt_chosen(void *fdt)
356*4882a593Smuzhiyun {
357*4882a593Smuzhiyun 	int   nodeoffset;
358*4882a593Smuzhiyun 	int   err;
359*4882a593Smuzhiyun 	char  *str;		/* used to set string properties */
360*4882a593Smuzhiyun 
361*4882a593Smuzhiyun 	err = fdt_check_header(fdt);
362*4882a593Smuzhiyun 	if (err < 0) {
363*4882a593Smuzhiyun 		printf("fdt_chosen: %s\n", fdt_strerror(err));
364*4882a593Smuzhiyun 		return err;
365*4882a593Smuzhiyun 	}
366*4882a593Smuzhiyun 
367*4882a593Smuzhiyun 	/* find or create "/chosen" node. */
368*4882a593Smuzhiyun 	nodeoffset = fdt_find_or_add_subnode(fdt, 0, "chosen");
369*4882a593Smuzhiyun 	if (nodeoffset < 0)
370*4882a593Smuzhiyun 		return nodeoffset;
371*4882a593Smuzhiyun 
372*4882a593Smuzhiyun 	str = board_fdt_chosen_bootargs(fdt);
373*4882a593Smuzhiyun 	if (str) {
374*4882a593Smuzhiyun 		err = fdt_setprop(fdt, nodeoffset, "bootargs", str,
375*4882a593Smuzhiyun 				  strlen(str) + 1);
376*4882a593Smuzhiyun 		if (err < 0) {
377*4882a593Smuzhiyun 			printf("WARNING: could not set bootargs %s.\n",
378*4882a593Smuzhiyun 			       fdt_strerror(err));
379*4882a593Smuzhiyun 			return err;
380*4882a593Smuzhiyun 		}
381*4882a593Smuzhiyun 	}
382*4882a593Smuzhiyun 
383*4882a593Smuzhiyun 	return fdt_fixup_stdout(fdt, nodeoffset);
384*4882a593Smuzhiyun }
385*4882a593Smuzhiyun 
do_fixup_by_path(void * fdt,const char * path,const char * prop,const void * val,int len,int create)386*4882a593Smuzhiyun void do_fixup_by_path(void *fdt, const char *path, const char *prop,
387*4882a593Smuzhiyun 		      const void *val, int len, int create)
388*4882a593Smuzhiyun {
389*4882a593Smuzhiyun #if defined(DEBUG)
390*4882a593Smuzhiyun 	int i;
391*4882a593Smuzhiyun 	debug("Updating property '%s/%s' = ", path, prop);
392*4882a593Smuzhiyun 	for (i = 0; i < len; i++)
393*4882a593Smuzhiyun 		debug(" %.2x", *(u8*)(val+i));
394*4882a593Smuzhiyun 	debug("\n");
395*4882a593Smuzhiyun #endif
396*4882a593Smuzhiyun 	int rc = fdt_find_and_setprop(fdt, path, prop, val, len, create);
397*4882a593Smuzhiyun 	if (rc)
398*4882a593Smuzhiyun 		printf("Unable to update property %s:%s, err=%s\n",
399*4882a593Smuzhiyun 			path, prop, fdt_strerror(rc));
400*4882a593Smuzhiyun }
401*4882a593Smuzhiyun 
do_fixup_by_path_u32(void * fdt,const char * path,const char * prop,u32 val,int create)402*4882a593Smuzhiyun void do_fixup_by_path_u32(void *fdt, const char *path, const char *prop,
403*4882a593Smuzhiyun 			  u32 val, int create)
404*4882a593Smuzhiyun {
405*4882a593Smuzhiyun 	fdt32_t tmp = cpu_to_fdt32(val);
406*4882a593Smuzhiyun 	do_fixup_by_path(fdt, path, prop, &tmp, sizeof(tmp), create);
407*4882a593Smuzhiyun }
408*4882a593Smuzhiyun 
do_fixup_by_prop(void * fdt,const char * pname,const void * pval,int plen,const char * prop,const void * val,int len,int create)409*4882a593Smuzhiyun void do_fixup_by_prop(void *fdt,
410*4882a593Smuzhiyun 		      const char *pname, const void *pval, int plen,
411*4882a593Smuzhiyun 		      const char *prop, const void *val, int len,
412*4882a593Smuzhiyun 		      int create)
413*4882a593Smuzhiyun {
414*4882a593Smuzhiyun 	int off;
415*4882a593Smuzhiyun #if defined(DEBUG)
416*4882a593Smuzhiyun 	int i;
417*4882a593Smuzhiyun 	debug("Updating property '%s' = ", prop);
418*4882a593Smuzhiyun 	for (i = 0; i < len; i++)
419*4882a593Smuzhiyun 		debug(" %.2x", *(u8*)(val+i));
420*4882a593Smuzhiyun 	debug("\n");
421*4882a593Smuzhiyun #endif
422*4882a593Smuzhiyun 	off = fdt_node_offset_by_prop_value(fdt, -1, pname, pval, plen);
423*4882a593Smuzhiyun 	while (off != -FDT_ERR_NOTFOUND) {
424*4882a593Smuzhiyun 		if (create || (fdt_get_property(fdt, off, prop, NULL) != NULL))
425*4882a593Smuzhiyun 			fdt_setprop(fdt, off, prop, val, len);
426*4882a593Smuzhiyun 		off = fdt_node_offset_by_prop_value(fdt, off, pname, pval, plen);
427*4882a593Smuzhiyun 	}
428*4882a593Smuzhiyun }
429*4882a593Smuzhiyun 
do_fixup_by_prop_u32(void * fdt,const char * pname,const void * pval,int plen,const char * prop,u32 val,int create)430*4882a593Smuzhiyun void do_fixup_by_prop_u32(void *fdt,
431*4882a593Smuzhiyun 			  const char *pname, const void *pval, int plen,
432*4882a593Smuzhiyun 			  const char *prop, u32 val, int create)
433*4882a593Smuzhiyun {
434*4882a593Smuzhiyun 	fdt32_t tmp = cpu_to_fdt32(val);
435*4882a593Smuzhiyun 	do_fixup_by_prop(fdt, pname, pval, plen, prop, &tmp, 4, create);
436*4882a593Smuzhiyun }
437*4882a593Smuzhiyun 
do_fixup_by_compat(void * fdt,const char * compat,const char * prop,const void * val,int len,int create)438*4882a593Smuzhiyun void do_fixup_by_compat(void *fdt, const char *compat,
439*4882a593Smuzhiyun 			const char *prop, const void *val, int len, int create)
440*4882a593Smuzhiyun {
441*4882a593Smuzhiyun 	int off = -1;
442*4882a593Smuzhiyun #if defined(DEBUG)
443*4882a593Smuzhiyun 	int i;
444*4882a593Smuzhiyun 	debug("Updating property '%s' = ", prop);
445*4882a593Smuzhiyun 	for (i = 0; i < len; i++)
446*4882a593Smuzhiyun 		debug(" %.2x", *(u8*)(val+i));
447*4882a593Smuzhiyun 	debug("\n");
448*4882a593Smuzhiyun #endif
449*4882a593Smuzhiyun 	off = fdt_node_offset_by_compatible(fdt, -1, compat);
450*4882a593Smuzhiyun 	while (off != -FDT_ERR_NOTFOUND) {
451*4882a593Smuzhiyun 		if (create || (fdt_get_property(fdt, off, prop, NULL) != NULL))
452*4882a593Smuzhiyun 			fdt_setprop(fdt, off, prop, val, len);
453*4882a593Smuzhiyun 		off = fdt_node_offset_by_compatible(fdt, off, compat);
454*4882a593Smuzhiyun 	}
455*4882a593Smuzhiyun }
456*4882a593Smuzhiyun 
do_fixup_by_compat_u32(void * fdt,const char * compat,const char * prop,u32 val,int create)457*4882a593Smuzhiyun void do_fixup_by_compat_u32(void *fdt, const char *compat,
458*4882a593Smuzhiyun 			    const char *prop, u32 val, int create)
459*4882a593Smuzhiyun {
460*4882a593Smuzhiyun 	fdt32_t tmp = cpu_to_fdt32(val);
461*4882a593Smuzhiyun 	do_fixup_by_compat(fdt, compat, prop, &tmp, 4, create);
462*4882a593Smuzhiyun }
463*4882a593Smuzhiyun 
464*4882a593Smuzhiyun /*
465*4882a593Smuzhiyun  * fdt_pack_reg - pack address and size array into the "reg"-suitable stream
466*4882a593Smuzhiyun  */
fdt_pack_reg(const void * fdt,void * buf,u64 * address,u64 * size,int n)467*4882a593Smuzhiyun static int fdt_pack_reg(const void *fdt, void *buf, u64 *address, u64 *size,
468*4882a593Smuzhiyun 			int n)
469*4882a593Smuzhiyun {
470*4882a593Smuzhiyun 	int i;
471*4882a593Smuzhiyun 	int address_cells = fdt_address_cells(fdt, 0);
472*4882a593Smuzhiyun 	int size_cells = fdt_size_cells(fdt, 0);
473*4882a593Smuzhiyun 	char *p = buf;
474*4882a593Smuzhiyun 
475*4882a593Smuzhiyun 	for (i = 0; i < n; i++) {
476*4882a593Smuzhiyun 		if (address_cells == 2)
477*4882a593Smuzhiyun 			*(fdt64_t *)p = cpu_to_fdt64(address[i]);
478*4882a593Smuzhiyun 		else
479*4882a593Smuzhiyun 			*(fdt32_t *)p = cpu_to_fdt32(address[i]);
480*4882a593Smuzhiyun 		p += 4 * address_cells;
481*4882a593Smuzhiyun 
482*4882a593Smuzhiyun 		if (size_cells == 2)
483*4882a593Smuzhiyun 			*(fdt64_t *)p = cpu_to_fdt64(size[i]);
484*4882a593Smuzhiyun 		else
485*4882a593Smuzhiyun 			*(fdt32_t *)p = cpu_to_fdt32(size[i]);
486*4882a593Smuzhiyun 		p += 4 * size_cells;
487*4882a593Smuzhiyun 	}
488*4882a593Smuzhiyun 
489*4882a593Smuzhiyun 	return p - (char *)buf;
490*4882a593Smuzhiyun }
491*4882a593Smuzhiyun 
fdt_record_loadable(void * blob,u32 index,const char * name,uintptr_t load_addr,u32 size,uintptr_t entry_point,const char * type,const char * os)492*4882a593Smuzhiyun int fdt_record_loadable(void *blob, u32 index, const char *name,
493*4882a593Smuzhiyun 			uintptr_t load_addr, u32 size, uintptr_t entry_point,
494*4882a593Smuzhiyun 			const char *type, const char *os)
495*4882a593Smuzhiyun {
496*4882a593Smuzhiyun 	int err, node;
497*4882a593Smuzhiyun 
498*4882a593Smuzhiyun 	err = fdt_check_header(blob);
499*4882a593Smuzhiyun 	if (err < 0) {
500*4882a593Smuzhiyun 		printf("%s: %s\n", __func__, fdt_strerror(err));
501*4882a593Smuzhiyun 		return err;
502*4882a593Smuzhiyun 	}
503*4882a593Smuzhiyun 
504*4882a593Smuzhiyun 	/* find or create "/fit-images" node */
505*4882a593Smuzhiyun 	node = fdt_find_or_add_subnode(blob, 0, "fit-images");
506*4882a593Smuzhiyun 	if (node < 0)
507*4882a593Smuzhiyun 			return node;
508*4882a593Smuzhiyun 
509*4882a593Smuzhiyun 	/* find or create "/fit-images/<name>" node */
510*4882a593Smuzhiyun 	node = fdt_find_or_add_subnode(blob, node, name);
511*4882a593Smuzhiyun 	if (node < 0)
512*4882a593Smuzhiyun 		return node;
513*4882a593Smuzhiyun 
514*4882a593Smuzhiyun 	/*
515*4882a593Smuzhiyun 	 * We record these as 32bit entities, possibly truncating addresses.
516*4882a593Smuzhiyun 	 * However, spl_fit.c is not 64bit safe either: i.e. we should not
517*4882a593Smuzhiyun 	 * have an issue here.
518*4882a593Smuzhiyun 	 */
519*4882a593Smuzhiyun 	fdt_setprop_u32(blob, node, "load-addr", load_addr);
520*4882a593Smuzhiyun 	if (entry_point != -1)
521*4882a593Smuzhiyun 		fdt_setprop_u32(blob, node, "entry-point", entry_point);
522*4882a593Smuzhiyun 	fdt_setprop_u32(blob, node, "size", size);
523*4882a593Smuzhiyun 	if (type)
524*4882a593Smuzhiyun 		fdt_setprop_string(blob, node, "type", type);
525*4882a593Smuzhiyun 	if (os)
526*4882a593Smuzhiyun 		fdt_setprop_string(blob, node, "os", os);
527*4882a593Smuzhiyun 
528*4882a593Smuzhiyun 	return node;
529*4882a593Smuzhiyun }
530*4882a593Smuzhiyun 
531*4882a593Smuzhiyun #ifdef CONFIG_NR_DRAM_BANKS
532*4882a593Smuzhiyun #define MEMORY_BANKS_MAX CONFIG_NR_DRAM_BANKS
533*4882a593Smuzhiyun #else
534*4882a593Smuzhiyun #define MEMORY_BANKS_MAX 4
535*4882a593Smuzhiyun #endif
536*4882a593Smuzhiyun 
537*4882a593Smuzhiyun #ifdef CONFIG_ARCH_FIXUP_FDT_MEMORY
fdt_fixup_memory_banks(void * blob,u64 start[],u64 size[],int banks)538*4882a593Smuzhiyun int fdt_fixup_memory_banks(void *blob, u64 start[], u64 size[], int banks)
539*4882a593Smuzhiyun {
540*4882a593Smuzhiyun 	int err, nodeoffset;
541*4882a593Smuzhiyun 	int len;
542*4882a593Smuzhiyun 	u8 tmp[MEMORY_BANKS_MAX * 16]; /* Up to 64-bit address + 64-bit size */
543*4882a593Smuzhiyun 
544*4882a593Smuzhiyun 	if (banks > MEMORY_BANKS_MAX) {
545*4882a593Smuzhiyun 		printf("%s: num banks %d exceeds hardcoded limit %d."
546*4882a593Smuzhiyun 		       " Recompile with higher MEMORY_BANKS_MAX?\n",
547*4882a593Smuzhiyun 		       __FUNCTION__, banks, MEMORY_BANKS_MAX);
548*4882a593Smuzhiyun 		return -1;
549*4882a593Smuzhiyun 	}
550*4882a593Smuzhiyun 
551*4882a593Smuzhiyun 	err = fdt_check_header(blob);
552*4882a593Smuzhiyun 	if (err < 0) {
553*4882a593Smuzhiyun 		printf("%s: %s\n", __FUNCTION__, fdt_strerror(err));
554*4882a593Smuzhiyun 		return err;
555*4882a593Smuzhiyun 	}
556*4882a593Smuzhiyun 
557*4882a593Smuzhiyun 	/* find or create "/memory" node. */
558*4882a593Smuzhiyun 	nodeoffset = fdt_find_or_add_subnode(blob, 0, "memory");
559*4882a593Smuzhiyun 	if (nodeoffset < 0)
560*4882a593Smuzhiyun 		return nodeoffset;
561*4882a593Smuzhiyun 
562*4882a593Smuzhiyun 	err = fdt_setprop(blob, nodeoffset, "device_type", "memory",
563*4882a593Smuzhiyun 			sizeof("memory"));
564*4882a593Smuzhiyun 	if (err < 0) {
565*4882a593Smuzhiyun 		printf("WARNING: could not set %s %s.\n", "device_type",
566*4882a593Smuzhiyun 				fdt_strerror(err));
567*4882a593Smuzhiyun 		return err;
568*4882a593Smuzhiyun 	}
569*4882a593Smuzhiyun 
570*4882a593Smuzhiyun 	if (!banks)
571*4882a593Smuzhiyun 		return 0;
572*4882a593Smuzhiyun 
573*4882a593Smuzhiyun 	len = fdt_pack_reg(blob, tmp, start, size, banks);
574*4882a593Smuzhiyun 
575*4882a593Smuzhiyun 	err = fdt_setprop(blob, nodeoffset, "reg", tmp, len);
576*4882a593Smuzhiyun 	if (err < 0) {
577*4882a593Smuzhiyun 		printf("WARNING: could not set %s %s.\n",
578*4882a593Smuzhiyun 				"reg", fdt_strerror(err));
579*4882a593Smuzhiyun 		return err;
580*4882a593Smuzhiyun 	}
581*4882a593Smuzhiyun 	return 0;
582*4882a593Smuzhiyun }
583*4882a593Smuzhiyun #else
fdt_fixup_memory_banks(void * blob,u64 start[],u64 size[],int banks)584*4882a593Smuzhiyun int fdt_fixup_memory_banks(void *blob, u64 start[], u64 size[], int banks)
585*4882a593Smuzhiyun {
586*4882a593Smuzhiyun 	struct fdt_resource res;
587*4882a593Smuzhiyun 	int i, nodeoffset;
588*4882a593Smuzhiyun 
589*4882a593Smuzhiyun 	/* show memory */
590*4882a593Smuzhiyun 	nodeoffset = fdt_subnode_offset(blob, 0, "memory");
591*4882a593Smuzhiyun 	if (nodeoffset > 0) {
592*4882a593Smuzhiyun 		for (i = 0; i < MEMORY_BANKS_MAX; i++) {
593*4882a593Smuzhiyun 			if (fdt_get_resource(blob, nodeoffset, "reg", i, &res))
594*4882a593Smuzhiyun 				break;
595*4882a593Smuzhiyun 			res.end += 1;
596*4882a593Smuzhiyun 			if (!res.start && !res.end)
597*4882a593Smuzhiyun 				break;
598*4882a593Smuzhiyun 			printf("fixed bank: 0x%08llx - 0x%08llx (size: 0x%08llx)\n",
599*4882a593Smuzhiyun 			       (u64)res.start, (u64)res.end, (u64)res.end - (u64)res.start);
600*4882a593Smuzhiyun 		}
601*4882a593Smuzhiyun 	}
602*4882a593Smuzhiyun 
603*4882a593Smuzhiyun 	return 0;
604*4882a593Smuzhiyun }
605*4882a593Smuzhiyun 
606*4882a593Smuzhiyun #endif
607*4882a593Smuzhiyun 
fdt_fixup_memory(void * blob,u64 start,u64 size)608*4882a593Smuzhiyun int fdt_fixup_memory(void *blob, u64 start, u64 size)
609*4882a593Smuzhiyun {
610*4882a593Smuzhiyun 	return fdt_fixup_memory_banks(blob, &start, &size, 1);
611*4882a593Smuzhiyun }
612*4882a593Smuzhiyun 
fdt_update_reserved_memory(void * blob,char * name,u64 start,u64 size)613*4882a593Smuzhiyun int fdt_update_reserved_memory(void *blob, char *name, u64 start, u64 size)
614*4882a593Smuzhiyun {
615*4882a593Smuzhiyun 	int nodeoffset, len, err;
616*4882a593Smuzhiyun 	u8 tmp[16]; /* Up to 64-bit address + 64-bit size */
617*4882a593Smuzhiyun 
618*4882a593Smuzhiyun 	nodeoffset = fdt_node_offset_by_compatible(blob, 0, name);
619*4882a593Smuzhiyun 	if (nodeoffset < 0)
620*4882a593Smuzhiyun 		debug("Can't find nodeoffset: %d\n", nodeoffset);
621*4882a593Smuzhiyun 
622*4882a593Smuzhiyun 	if (!size)
623*4882a593Smuzhiyun 		return nodeoffset;
624*4882a593Smuzhiyun 
625*4882a593Smuzhiyun 	len = fdt_pack_reg(blob, tmp, &start, &size, 1);
626*4882a593Smuzhiyun 	err = fdt_setprop(blob, nodeoffset, "reg", tmp, len);
627*4882a593Smuzhiyun 	if (err < 0) {
628*4882a593Smuzhiyun 		printf("WARNING: could not set %s %s.\n",
629*4882a593Smuzhiyun 				"reg", fdt_strerror(err));
630*4882a593Smuzhiyun 		return err;
631*4882a593Smuzhiyun 	}
632*4882a593Smuzhiyun 
633*4882a593Smuzhiyun 	return nodeoffset;
634*4882a593Smuzhiyun }
635*4882a593Smuzhiyun 
fdt_fixup_ethernet(void * fdt)636*4882a593Smuzhiyun void fdt_fixup_ethernet(void *fdt)
637*4882a593Smuzhiyun {
638*4882a593Smuzhiyun 	int i = 0, j, prop;
639*4882a593Smuzhiyun 	char *tmp, *end;
640*4882a593Smuzhiyun 	char mac[16];
641*4882a593Smuzhiyun 	const char *path;
642*4882a593Smuzhiyun 	unsigned char mac_addr[ARP_HLEN];
643*4882a593Smuzhiyun 	int offset;
644*4882a593Smuzhiyun #ifdef FDT_SEQ_MACADDR_FROM_ENV
645*4882a593Smuzhiyun 	int nodeoff;
646*4882a593Smuzhiyun 	const struct fdt_property *fdt_prop;
647*4882a593Smuzhiyun #endif
648*4882a593Smuzhiyun 
649*4882a593Smuzhiyun 	if (fdt_path_offset(fdt, "/aliases") < 0)
650*4882a593Smuzhiyun 		return;
651*4882a593Smuzhiyun 
652*4882a593Smuzhiyun 	/* Cycle through all aliases */
653*4882a593Smuzhiyun 	for (prop = 0; ; prop++) {
654*4882a593Smuzhiyun 		const char *name;
655*4882a593Smuzhiyun 
656*4882a593Smuzhiyun 		/* FDT might have been edited, recompute the offset */
657*4882a593Smuzhiyun 		offset = fdt_first_property_offset(fdt,
658*4882a593Smuzhiyun 			fdt_path_offset(fdt, "/aliases"));
659*4882a593Smuzhiyun 		/* Select property number 'prop' */
660*4882a593Smuzhiyun 		for (j = 0; j < prop; j++)
661*4882a593Smuzhiyun 			offset = fdt_next_property_offset(fdt, offset);
662*4882a593Smuzhiyun 
663*4882a593Smuzhiyun 		if (offset < 0)
664*4882a593Smuzhiyun 			break;
665*4882a593Smuzhiyun 
666*4882a593Smuzhiyun 		path = fdt_getprop_by_offset(fdt, offset, &name, NULL);
667*4882a593Smuzhiyun 		if (!strncmp(name, "ethernet", 8)) {
668*4882a593Smuzhiyun 			/* Treat plain "ethernet" same as "ethernet0". */
669*4882a593Smuzhiyun 			if (!strcmp(name, "ethernet")
670*4882a593Smuzhiyun #ifdef FDT_SEQ_MACADDR_FROM_ENV
671*4882a593Smuzhiyun 			 || !strcmp(name, "ethernet0")
672*4882a593Smuzhiyun #endif
673*4882a593Smuzhiyun 			)
674*4882a593Smuzhiyun 				i = 0;
675*4882a593Smuzhiyun #ifndef FDT_SEQ_MACADDR_FROM_ENV
676*4882a593Smuzhiyun 			else
677*4882a593Smuzhiyun 				i = trailing_strtol(name);
678*4882a593Smuzhiyun #endif
679*4882a593Smuzhiyun 			if (i != -1) {
680*4882a593Smuzhiyun 				if (i == 0)
681*4882a593Smuzhiyun 					strcpy(mac, "ethaddr");
682*4882a593Smuzhiyun 				else
683*4882a593Smuzhiyun 					sprintf(mac, "eth%daddr", i);
684*4882a593Smuzhiyun 			} else {
685*4882a593Smuzhiyun 				continue;
686*4882a593Smuzhiyun 			}
687*4882a593Smuzhiyun #ifdef FDT_SEQ_MACADDR_FROM_ENV
688*4882a593Smuzhiyun 			nodeoff = fdt_path_offset(fdt, path);
689*4882a593Smuzhiyun 			fdt_prop = fdt_get_property(fdt, nodeoff, "status",
690*4882a593Smuzhiyun 						    NULL);
691*4882a593Smuzhiyun 			if (fdt_prop && !strcmp(fdt_prop->data, "disabled"))
692*4882a593Smuzhiyun 				continue;
693*4882a593Smuzhiyun 			i++;
694*4882a593Smuzhiyun #endif
695*4882a593Smuzhiyun 			tmp = env_get(mac);
696*4882a593Smuzhiyun 			if (!tmp)
697*4882a593Smuzhiyun 				continue;
698*4882a593Smuzhiyun 
699*4882a593Smuzhiyun 			for (j = 0; j < 6; j++) {
700*4882a593Smuzhiyun 				mac_addr[j] = tmp ?
701*4882a593Smuzhiyun 					      simple_strtoul(tmp, &end, 16) : 0;
702*4882a593Smuzhiyun 				if (tmp)
703*4882a593Smuzhiyun 					tmp = (*end) ? end + 1 : end;
704*4882a593Smuzhiyun 			}
705*4882a593Smuzhiyun 
706*4882a593Smuzhiyun 			do_fixup_by_path(fdt, path, "mac-address",
707*4882a593Smuzhiyun 					 &mac_addr, 6, 0);
708*4882a593Smuzhiyun 			do_fixup_by_path(fdt, path, "local-mac-address",
709*4882a593Smuzhiyun 					 &mac_addr, 6, 1);
710*4882a593Smuzhiyun 		}
711*4882a593Smuzhiyun 	}
712*4882a593Smuzhiyun }
713*4882a593Smuzhiyun 
714*4882a593Smuzhiyun /* Resize the fdt to its actual size + a bit of padding */
fdt_shrink_to_minimum(void * blob,uint extrasize)715*4882a593Smuzhiyun int fdt_shrink_to_minimum(void *blob, uint extrasize)
716*4882a593Smuzhiyun {
717*4882a593Smuzhiyun 	int i;
718*4882a593Smuzhiyun 	uint64_t addr, size;
719*4882a593Smuzhiyun 	int total, ret;
720*4882a593Smuzhiyun 	uint actualsize;
721*4882a593Smuzhiyun 
722*4882a593Smuzhiyun 	if (!blob)
723*4882a593Smuzhiyun 		return 0;
724*4882a593Smuzhiyun 
725*4882a593Smuzhiyun 	total = fdt_num_mem_rsv(blob);
726*4882a593Smuzhiyun 	for (i = 0; i < total; i++) {
727*4882a593Smuzhiyun 		fdt_get_mem_rsv(blob, i, &addr, &size);
728*4882a593Smuzhiyun 		if (addr == (uintptr_t)blob) {
729*4882a593Smuzhiyun 			fdt_del_mem_rsv(blob, i);
730*4882a593Smuzhiyun 			break;
731*4882a593Smuzhiyun 		}
732*4882a593Smuzhiyun 	}
733*4882a593Smuzhiyun 
734*4882a593Smuzhiyun 	/*
735*4882a593Smuzhiyun 	 * Calculate the actual size of the fdt
736*4882a593Smuzhiyun 	 * plus the size needed for 5 fdt_add_mem_rsv, one
737*4882a593Smuzhiyun 	 * for the fdt itself and 4 for a possible initrd
738*4882a593Smuzhiyun 	 * ((initrd-start + initrd-end) * 2 (name & value))
739*4882a593Smuzhiyun 	 */
740*4882a593Smuzhiyun 	actualsize = fdt_off_dt_strings(blob) +
741*4882a593Smuzhiyun 		fdt_size_dt_strings(blob) + 5 * sizeof(struct fdt_reserve_entry);
742*4882a593Smuzhiyun 
743*4882a593Smuzhiyun 	actualsize += extrasize;
744*4882a593Smuzhiyun 	/* Make it so the fdt ends on a page boundary */
745*4882a593Smuzhiyun 	actualsize = ALIGN(actualsize + ((uintptr_t)blob & 0xfff), 0x1000);
746*4882a593Smuzhiyun 	actualsize = actualsize - ((uintptr_t)blob & 0xfff);
747*4882a593Smuzhiyun 
748*4882a593Smuzhiyun 	/* Change the fdt header to reflect the correct size */
749*4882a593Smuzhiyun 	fdt_set_totalsize(blob, actualsize);
750*4882a593Smuzhiyun 
751*4882a593Smuzhiyun 	/* Add the new reservation */
752*4882a593Smuzhiyun 	ret = fdt_add_mem_rsv(blob, (uintptr_t)blob, actualsize);
753*4882a593Smuzhiyun 	if (ret < 0)
754*4882a593Smuzhiyun 		return ret;
755*4882a593Smuzhiyun 
756*4882a593Smuzhiyun 	return actualsize;
757*4882a593Smuzhiyun }
758*4882a593Smuzhiyun 
759*4882a593Smuzhiyun #ifdef CONFIG_PCI
760*4882a593Smuzhiyun #define CONFIG_SYS_PCI_NR_INBOUND_WIN 4
761*4882a593Smuzhiyun 
762*4882a593Smuzhiyun #define FDT_PCI_PREFETCH	(0x40000000)
763*4882a593Smuzhiyun #define FDT_PCI_MEM32		(0x02000000)
764*4882a593Smuzhiyun #define FDT_PCI_IO		(0x01000000)
765*4882a593Smuzhiyun #define FDT_PCI_MEM64		(0x03000000)
766*4882a593Smuzhiyun 
fdt_pci_dma_ranges(void * blob,int phb_off,struct pci_controller * hose)767*4882a593Smuzhiyun int fdt_pci_dma_ranges(void *blob, int phb_off, struct pci_controller *hose) {
768*4882a593Smuzhiyun 
769*4882a593Smuzhiyun 	int addrcell, sizecell, len, r;
770*4882a593Smuzhiyun 	u32 *dma_range;
771*4882a593Smuzhiyun 	/* sized based on pci addr cells, size-cells, & address-cells */
772*4882a593Smuzhiyun 	u32 dma_ranges[(3 + 2 + 2) * CONFIG_SYS_PCI_NR_INBOUND_WIN];
773*4882a593Smuzhiyun 
774*4882a593Smuzhiyun 	addrcell = fdt_getprop_u32_default(blob, "/", "#address-cells", 1);
775*4882a593Smuzhiyun 	sizecell = fdt_getprop_u32_default(blob, "/", "#size-cells", 1);
776*4882a593Smuzhiyun 
777*4882a593Smuzhiyun 	dma_range = &dma_ranges[0];
778*4882a593Smuzhiyun 	for (r = 0; r < hose->region_count; r++) {
779*4882a593Smuzhiyun 		u64 bus_start, phys_start, size;
780*4882a593Smuzhiyun 
781*4882a593Smuzhiyun 		/* skip if !PCI_REGION_SYS_MEMORY */
782*4882a593Smuzhiyun 		if (!(hose->regions[r].flags & PCI_REGION_SYS_MEMORY))
783*4882a593Smuzhiyun 			continue;
784*4882a593Smuzhiyun 
785*4882a593Smuzhiyun 		bus_start = (u64)hose->regions[r].bus_start;
786*4882a593Smuzhiyun 		phys_start = (u64)hose->regions[r].phys_start;
787*4882a593Smuzhiyun 		size = (u64)hose->regions[r].size;
788*4882a593Smuzhiyun 
789*4882a593Smuzhiyun 		dma_range[0] = 0;
790*4882a593Smuzhiyun 		if (size >= 0x100000000ull)
791*4882a593Smuzhiyun 			dma_range[0] |= FDT_PCI_MEM64;
792*4882a593Smuzhiyun 		else
793*4882a593Smuzhiyun 			dma_range[0] |= FDT_PCI_MEM32;
794*4882a593Smuzhiyun 		if (hose->regions[r].flags & PCI_REGION_PREFETCH)
795*4882a593Smuzhiyun 			dma_range[0] |= FDT_PCI_PREFETCH;
796*4882a593Smuzhiyun #ifdef CONFIG_SYS_PCI_64BIT
797*4882a593Smuzhiyun 		dma_range[1] = bus_start >> 32;
798*4882a593Smuzhiyun #else
799*4882a593Smuzhiyun 		dma_range[1] = 0;
800*4882a593Smuzhiyun #endif
801*4882a593Smuzhiyun 		dma_range[2] = bus_start & 0xffffffff;
802*4882a593Smuzhiyun 
803*4882a593Smuzhiyun 		if (addrcell == 2) {
804*4882a593Smuzhiyun 			dma_range[3] = phys_start >> 32;
805*4882a593Smuzhiyun 			dma_range[4] = phys_start & 0xffffffff;
806*4882a593Smuzhiyun 		} else {
807*4882a593Smuzhiyun 			dma_range[3] = phys_start & 0xffffffff;
808*4882a593Smuzhiyun 		}
809*4882a593Smuzhiyun 
810*4882a593Smuzhiyun 		if (sizecell == 2) {
811*4882a593Smuzhiyun 			dma_range[3 + addrcell + 0] = size >> 32;
812*4882a593Smuzhiyun 			dma_range[3 + addrcell + 1] = size & 0xffffffff;
813*4882a593Smuzhiyun 		} else {
814*4882a593Smuzhiyun 			dma_range[3 + addrcell + 0] = size & 0xffffffff;
815*4882a593Smuzhiyun 		}
816*4882a593Smuzhiyun 
817*4882a593Smuzhiyun 		dma_range += (3 + addrcell + sizecell);
818*4882a593Smuzhiyun 	}
819*4882a593Smuzhiyun 
820*4882a593Smuzhiyun 	len = dma_range - &dma_ranges[0];
821*4882a593Smuzhiyun 	if (len)
822*4882a593Smuzhiyun 		fdt_setprop(blob, phb_off, "dma-ranges", &dma_ranges[0], len*4);
823*4882a593Smuzhiyun 
824*4882a593Smuzhiyun 	return 0;
825*4882a593Smuzhiyun }
826*4882a593Smuzhiyun #endif
827*4882a593Smuzhiyun 
fdt_increase_size(void * fdt,int add_len)828*4882a593Smuzhiyun int fdt_increase_size(void *fdt, int add_len)
829*4882a593Smuzhiyun {
830*4882a593Smuzhiyun 	int newlen;
831*4882a593Smuzhiyun 
832*4882a593Smuzhiyun 	newlen = fdt_totalsize(fdt) + add_len;
833*4882a593Smuzhiyun 
834*4882a593Smuzhiyun 	/* Open in place with a new len */
835*4882a593Smuzhiyun 	return fdt_open_into(fdt, fdt, newlen);
836*4882a593Smuzhiyun }
837*4882a593Smuzhiyun 
838*4882a593Smuzhiyun #ifdef CONFIG_FDT_FIXUP_PARTITIONS
839*4882a593Smuzhiyun #include <jffs2/load_kernel.h>
840*4882a593Smuzhiyun #include <mtd_node.h>
841*4882a593Smuzhiyun 
842*4882a593Smuzhiyun struct reg_cell {
843*4882a593Smuzhiyun 	unsigned int r0;
844*4882a593Smuzhiyun 	unsigned int r1;
845*4882a593Smuzhiyun };
846*4882a593Smuzhiyun 
fdt_del_subnodes(const void * blob,int parent_offset)847*4882a593Smuzhiyun int fdt_del_subnodes(const void *blob, int parent_offset)
848*4882a593Smuzhiyun {
849*4882a593Smuzhiyun 	int off, ndepth;
850*4882a593Smuzhiyun 	int ret;
851*4882a593Smuzhiyun 
852*4882a593Smuzhiyun 	for (ndepth = 0, off = fdt_next_node(blob, parent_offset, &ndepth);
853*4882a593Smuzhiyun 	     (off >= 0) && (ndepth > 0);
854*4882a593Smuzhiyun 	     off = fdt_next_node(blob, off, &ndepth)) {
855*4882a593Smuzhiyun 		if (ndepth == 1) {
856*4882a593Smuzhiyun 			debug("delete %s: offset: %x\n",
857*4882a593Smuzhiyun 				fdt_get_name(blob, off, 0), off);
858*4882a593Smuzhiyun 			ret = fdt_del_node((void *)blob, off);
859*4882a593Smuzhiyun 			if (ret < 0) {
860*4882a593Smuzhiyun 				printf("Can't delete node: %s\n",
861*4882a593Smuzhiyun 					fdt_strerror(ret));
862*4882a593Smuzhiyun 				return ret;
863*4882a593Smuzhiyun 			} else {
864*4882a593Smuzhiyun 				ndepth = 0;
865*4882a593Smuzhiyun 				off = parent_offset;
866*4882a593Smuzhiyun 			}
867*4882a593Smuzhiyun 		}
868*4882a593Smuzhiyun 	}
869*4882a593Smuzhiyun 	return 0;
870*4882a593Smuzhiyun }
871*4882a593Smuzhiyun 
fdt_del_partitions(void * blob,int parent_offset)872*4882a593Smuzhiyun int fdt_del_partitions(void *blob, int parent_offset)
873*4882a593Smuzhiyun {
874*4882a593Smuzhiyun 	const void *prop;
875*4882a593Smuzhiyun 	int ndepth = 0;
876*4882a593Smuzhiyun 	int off;
877*4882a593Smuzhiyun 	int ret;
878*4882a593Smuzhiyun 
879*4882a593Smuzhiyun 	off = fdt_next_node(blob, parent_offset, &ndepth);
880*4882a593Smuzhiyun 	if (off > 0 && ndepth == 1) {
881*4882a593Smuzhiyun 		prop = fdt_getprop(blob, off, "label", NULL);
882*4882a593Smuzhiyun 		if (prop == NULL) {
883*4882a593Smuzhiyun 			/*
884*4882a593Smuzhiyun 			 * Could not find label property, nand {}; node?
885*4882a593Smuzhiyun 			 * Check subnode, delete partitions there if any.
886*4882a593Smuzhiyun 			 */
887*4882a593Smuzhiyun 			return fdt_del_partitions(blob, off);
888*4882a593Smuzhiyun 		} else {
889*4882a593Smuzhiyun 			ret = fdt_del_subnodes(blob, parent_offset);
890*4882a593Smuzhiyun 			if (ret < 0) {
891*4882a593Smuzhiyun 				printf("Can't remove subnodes: %s\n",
892*4882a593Smuzhiyun 					fdt_strerror(ret));
893*4882a593Smuzhiyun 				return ret;
894*4882a593Smuzhiyun 			}
895*4882a593Smuzhiyun 		}
896*4882a593Smuzhiyun 	}
897*4882a593Smuzhiyun 	return 0;
898*4882a593Smuzhiyun }
899*4882a593Smuzhiyun 
fdt_node_set_part_info(void * blob,int parent_offset,struct mtd_device * dev)900*4882a593Smuzhiyun int fdt_node_set_part_info(void *blob, int parent_offset,
901*4882a593Smuzhiyun 			   struct mtd_device *dev)
902*4882a593Smuzhiyun {
903*4882a593Smuzhiyun 	struct list_head *pentry;
904*4882a593Smuzhiyun 	struct part_info *part;
905*4882a593Smuzhiyun 	struct reg_cell cell;
906*4882a593Smuzhiyun 	int off, ndepth = 0;
907*4882a593Smuzhiyun 	int part_num, ret;
908*4882a593Smuzhiyun 	char buf[64];
909*4882a593Smuzhiyun 
910*4882a593Smuzhiyun 	ret = fdt_del_partitions(blob, parent_offset);
911*4882a593Smuzhiyun 	if (ret < 0)
912*4882a593Smuzhiyun 		return ret;
913*4882a593Smuzhiyun 
914*4882a593Smuzhiyun 	/*
915*4882a593Smuzhiyun 	 * Check if it is nand {}; subnode, adjust
916*4882a593Smuzhiyun 	 * the offset in this case
917*4882a593Smuzhiyun 	 */
918*4882a593Smuzhiyun 	off = fdt_next_node(blob, parent_offset, &ndepth);
919*4882a593Smuzhiyun 	if (off > 0 && ndepth == 1)
920*4882a593Smuzhiyun 		parent_offset = off;
921*4882a593Smuzhiyun 
922*4882a593Smuzhiyun 	part_num = 0;
923*4882a593Smuzhiyun 	list_for_each_prev(pentry, &dev->parts) {
924*4882a593Smuzhiyun 		int newoff;
925*4882a593Smuzhiyun 
926*4882a593Smuzhiyun 		part = list_entry(pentry, struct part_info, link);
927*4882a593Smuzhiyun 
928*4882a593Smuzhiyun 		debug("%2d: %-20s0x%08llx\t0x%08llx\t%d\n",
929*4882a593Smuzhiyun 			part_num, part->name, part->size,
930*4882a593Smuzhiyun 			part->offset, part->mask_flags);
931*4882a593Smuzhiyun 
932*4882a593Smuzhiyun 		sprintf(buf, "partition@%llx", part->offset);
933*4882a593Smuzhiyun add_sub:
934*4882a593Smuzhiyun 		ret = fdt_add_subnode(blob, parent_offset, buf);
935*4882a593Smuzhiyun 		if (ret == -FDT_ERR_NOSPACE) {
936*4882a593Smuzhiyun 			ret = fdt_increase_size(blob, 512);
937*4882a593Smuzhiyun 			if (!ret)
938*4882a593Smuzhiyun 				goto add_sub;
939*4882a593Smuzhiyun 			else
940*4882a593Smuzhiyun 				goto err_size;
941*4882a593Smuzhiyun 		} else if (ret < 0) {
942*4882a593Smuzhiyun 			printf("Can't add partition node: %s\n",
943*4882a593Smuzhiyun 				fdt_strerror(ret));
944*4882a593Smuzhiyun 			return ret;
945*4882a593Smuzhiyun 		}
946*4882a593Smuzhiyun 		newoff = ret;
947*4882a593Smuzhiyun 
948*4882a593Smuzhiyun 		/* Check MTD_WRITEABLE_CMD flag */
949*4882a593Smuzhiyun 		if (part->mask_flags & 1) {
950*4882a593Smuzhiyun add_ro:
951*4882a593Smuzhiyun 			ret = fdt_setprop(blob, newoff, "read_only", NULL, 0);
952*4882a593Smuzhiyun 			if (ret == -FDT_ERR_NOSPACE) {
953*4882a593Smuzhiyun 				ret = fdt_increase_size(blob, 512);
954*4882a593Smuzhiyun 				if (!ret)
955*4882a593Smuzhiyun 					goto add_ro;
956*4882a593Smuzhiyun 				else
957*4882a593Smuzhiyun 					goto err_size;
958*4882a593Smuzhiyun 			} else if (ret < 0)
959*4882a593Smuzhiyun 				goto err_prop;
960*4882a593Smuzhiyun 		}
961*4882a593Smuzhiyun 
962*4882a593Smuzhiyun 		cell.r0 = cpu_to_fdt32(part->offset);
963*4882a593Smuzhiyun 		cell.r1 = cpu_to_fdt32(part->size);
964*4882a593Smuzhiyun add_reg:
965*4882a593Smuzhiyun 		ret = fdt_setprop(blob, newoff, "reg", &cell, sizeof(cell));
966*4882a593Smuzhiyun 		if (ret == -FDT_ERR_NOSPACE) {
967*4882a593Smuzhiyun 			ret = fdt_increase_size(blob, 512);
968*4882a593Smuzhiyun 			if (!ret)
969*4882a593Smuzhiyun 				goto add_reg;
970*4882a593Smuzhiyun 			else
971*4882a593Smuzhiyun 				goto err_size;
972*4882a593Smuzhiyun 		} else if (ret < 0)
973*4882a593Smuzhiyun 			goto err_prop;
974*4882a593Smuzhiyun 
975*4882a593Smuzhiyun add_label:
976*4882a593Smuzhiyun 		ret = fdt_setprop_string(blob, newoff, "label", part->name);
977*4882a593Smuzhiyun 		if (ret == -FDT_ERR_NOSPACE) {
978*4882a593Smuzhiyun 			ret = fdt_increase_size(blob, 512);
979*4882a593Smuzhiyun 			if (!ret)
980*4882a593Smuzhiyun 				goto add_label;
981*4882a593Smuzhiyun 			else
982*4882a593Smuzhiyun 				goto err_size;
983*4882a593Smuzhiyun 		} else if (ret < 0)
984*4882a593Smuzhiyun 			goto err_prop;
985*4882a593Smuzhiyun 
986*4882a593Smuzhiyun 		part_num++;
987*4882a593Smuzhiyun 	}
988*4882a593Smuzhiyun 	return 0;
989*4882a593Smuzhiyun err_size:
990*4882a593Smuzhiyun 	printf("Can't increase blob size: %s\n", fdt_strerror(ret));
991*4882a593Smuzhiyun 	return ret;
992*4882a593Smuzhiyun err_prop:
993*4882a593Smuzhiyun 	printf("Can't add property: %s\n", fdt_strerror(ret));
994*4882a593Smuzhiyun 	return ret;
995*4882a593Smuzhiyun }
996*4882a593Smuzhiyun 
997*4882a593Smuzhiyun /*
998*4882a593Smuzhiyun  * Update partitions in nor/nand nodes using info from
999*4882a593Smuzhiyun  * mtdparts environment variable. The nodes to update are
1000*4882a593Smuzhiyun  * specified by node_info structure which contains mtd device
1001*4882a593Smuzhiyun  * type and compatible string: E. g. the board code in
1002*4882a593Smuzhiyun  * ft_board_setup() could use:
1003*4882a593Smuzhiyun  *
1004*4882a593Smuzhiyun  *	struct node_info nodes[] = {
1005*4882a593Smuzhiyun  *		{ "fsl,mpc5121-nfc",    MTD_DEV_TYPE_NAND, },
1006*4882a593Smuzhiyun  *		{ "cfi-flash",          MTD_DEV_TYPE_NOR,  },
1007*4882a593Smuzhiyun  *	};
1008*4882a593Smuzhiyun  *
1009*4882a593Smuzhiyun  *	fdt_fixup_mtdparts(blob, nodes, ARRAY_SIZE(nodes));
1010*4882a593Smuzhiyun  */
fdt_fixup_mtdparts(void * blob,void * node_info,int node_info_size)1011*4882a593Smuzhiyun void fdt_fixup_mtdparts(void *blob, void *node_info, int node_info_size)
1012*4882a593Smuzhiyun {
1013*4882a593Smuzhiyun 	struct node_info *ni = node_info;
1014*4882a593Smuzhiyun 	struct mtd_device *dev;
1015*4882a593Smuzhiyun 	int i, idx;
1016*4882a593Smuzhiyun 	int noff;
1017*4882a593Smuzhiyun 
1018*4882a593Smuzhiyun 	if (mtdparts_init() != 0)
1019*4882a593Smuzhiyun 		return;
1020*4882a593Smuzhiyun 
1021*4882a593Smuzhiyun 	for (i = 0; i < node_info_size; i++) {
1022*4882a593Smuzhiyun 		idx = 0;
1023*4882a593Smuzhiyun 		noff = fdt_node_offset_by_compatible(blob, -1, ni[i].compat);
1024*4882a593Smuzhiyun 		while (noff != -FDT_ERR_NOTFOUND) {
1025*4882a593Smuzhiyun 			debug("%s: %s, mtd dev type %d\n",
1026*4882a593Smuzhiyun 				fdt_get_name(blob, noff, 0),
1027*4882a593Smuzhiyun 				ni[i].compat, ni[i].type);
1028*4882a593Smuzhiyun 			dev = device_find(ni[i].type, idx++);
1029*4882a593Smuzhiyun 			if (dev) {
1030*4882a593Smuzhiyun 				if (fdt_node_set_part_info(blob, noff, dev))
1031*4882a593Smuzhiyun 					return; /* return on error */
1032*4882a593Smuzhiyun 			}
1033*4882a593Smuzhiyun 
1034*4882a593Smuzhiyun 			/* Jump to next flash node */
1035*4882a593Smuzhiyun 			noff = fdt_node_offset_by_compatible(blob, noff,
1036*4882a593Smuzhiyun 							     ni[i].compat);
1037*4882a593Smuzhiyun 		}
1038*4882a593Smuzhiyun 	}
1039*4882a593Smuzhiyun }
1040*4882a593Smuzhiyun #endif
1041*4882a593Smuzhiyun 
fdt_del_node_and_alias(void * blob,const char * alias)1042*4882a593Smuzhiyun void fdt_del_node_and_alias(void *blob, const char *alias)
1043*4882a593Smuzhiyun {
1044*4882a593Smuzhiyun 	int off = fdt_path_offset(blob, alias);
1045*4882a593Smuzhiyun 
1046*4882a593Smuzhiyun 	if (off < 0)
1047*4882a593Smuzhiyun 		return;
1048*4882a593Smuzhiyun 
1049*4882a593Smuzhiyun 	fdt_del_node(blob, off);
1050*4882a593Smuzhiyun 
1051*4882a593Smuzhiyun 	off = fdt_path_offset(blob, "/aliases");
1052*4882a593Smuzhiyun 	fdt_delprop(blob, off, alias);
1053*4882a593Smuzhiyun }
1054*4882a593Smuzhiyun 
1055*4882a593Smuzhiyun /* Max address size we deal with */
1056*4882a593Smuzhiyun #define OF_MAX_ADDR_CELLS	4
1057*4882a593Smuzhiyun #define OF_BAD_ADDR	FDT_ADDR_T_NONE
1058*4882a593Smuzhiyun #define OF_CHECK_COUNTS(na, ns)	((na) > 0 && (na) <= OF_MAX_ADDR_CELLS && \
1059*4882a593Smuzhiyun 			(ns) > 0)
1060*4882a593Smuzhiyun 
1061*4882a593Smuzhiyun /* Debug utility */
1062*4882a593Smuzhiyun #ifdef DEBUG
of_dump_addr(const char * s,const fdt32_t * addr,int na)1063*4882a593Smuzhiyun static void of_dump_addr(const char *s, const fdt32_t *addr, int na)
1064*4882a593Smuzhiyun {
1065*4882a593Smuzhiyun 	printf("%s", s);
1066*4882a593Smuzhiyun 	while(na--)
1067*4882a593Smuzhiyun 		printf(" %08x", *(addr++));
1068*4882a593Smuzhiyun 	printf("\n");
1069*4882a593Smuzhiyun }
1070*4882a593Smuzhiyun #else
of_dump_addr(const char * s,const fdt32_t * addr,int na)1071*4882a593Smuzhiyun static void of_dump_addr(const char *s, const fdt32_t *addr, int na) { }
1072*4882a593Smuzhiyun #endif
1073*4882a593Smuzhiyun 
1074*4882a593Smuzhiyun /**
1075*4882a593Smuzhiyun  * struct of_bus - Callbacks for bus specific translators
1076*4882a593Smuzhiyun  * @name:	A string used to identify this bus in debug output.
1077*4882a593Smuzhiyun  * @addresses:	The name of the DT property from which addresses are
1078*4882a593Smuzhiyun  *		to be read, typically "reg".
1079*4882a593Smuzhiyun  * @match:	Return non-zero if the node whose parent is at
1080*4882a593Smuzhiyun  *		parentoffset in the FDT blob corresponds to a bus
1081*4882a593Smuzhiyun  *		of this type, otherwise return zero. If NULL a match
1082*4882a593Smuzhiyun  *		is assumed.
1083*4882a593Smuzhiyun  * @count_cells:Count how many cells (be32 values) a node whose parent
1084*4882a593Smuzhiyun  *		is at parentoffset in the FDT blob will require to
1085*4882a593Smuzhiyun  *		represent its address (written to *addrc) & size
1086*4882a593Smuzhiyun  *		(written to *sizec).
1087*4882a593Smuzhiyun  * @map:	Map the address addr from the address space of this
1088*4882a593Smuzhiyun  *		bus to that of its parent, making use of the ranges
1089*4882a593Smuzhiyun  *		read from DT to an array at range. na and ns are the
1090*4882a593Smuzhiyun  *		number of cells (be32 values) used to hold and address
1091*4882a593Smuzhiyun  *		or size, respectively, for this bus. pna is the number
1092*4882a593Smuzhiyun  *		of cells used to hold an address for the parent bus.
1093*4882a593Smuzhiyun  *		Returns the address in the address space of the parent
1094*4882a593Smuzhiyun  *		bus.
1095*4882a593Smuzhiyun  * @translate:	Update the value of the address cells at addr within an
1096*4882a593Smuzhiyun  *		FDT by adding offset to it. na specifies the number of
1097*4882a593Smuzhiyun  *		cells used to hold the address being translated. Returns
1098*4882a593Smuzhiyun  *		zero on success, non-zero on error.
1099*4882a593Smuzhiyun  *
1100*4882a593Smuzhiyun  * Each bus type will include a struct of_bus in the of_busses array,
1101*4882a593Smuzhiyun  * providing implementations of some or all of the functions used to
1102*4882a593Smuzhiyun  * match the bus & handle address translation for its children.
1103*4882a593Smuzhiyun  */
1104*4882a593Smuzhiyun struct of_bus {
1105*4882a593Smuzhiyun 	const char	*name;
1106*4882a593Smuzhiyun 	const char	*addresses;
1107*4882a593Smuzhiyun 	int		(*match)(const void *blob, int parentoffset);
1108*4882a593Smuzhiyun 	void		(*count_cells)(const void *blob, int parentoffset,
1109*4882a593Smuzhiyun 				int *addrc, int *sizec);
1110*4882a593Smuzhiyun 	u64		(*map)(fdt32_t *addr, const fdt32_t *range,
1111*4882a593Smuzhiyun 				int na, int ns, int pna);
1112*4882a593Smuzhiyun 	int		(*translate)(fdt32_t *addr, u64 offset, int na);
1113*4882a593Smuzhiyun };
1114*4882a593Smuzhiyun 
1115*4882a593Smuzhiyun /* Default translator (generic bus) */
fdt_support_default_count_cells(const void * blob,int parentoffset,int * addrc,int * sizec)1116*4882a593Smuzhiyun void fdt_support_default_count_cells(const void *blob, int parentoffset,
1117*4882a593Smuzhiyun 					int *addrc, int *sizec)
1118*4882a593Smuzhiyun {
1119*4882a593Smuzhiyun 	const fdt32_t *prop;
1120*4882a593Smuzhiyun 
1121*4882a593Smuzhiyun 	if (addrc)
1122*4882a593Smuzhiyun 		*addrc = fdt_address_cells(blob, parentoffset);
1123*4882a593Smuzhiyun 
1124*4882a593Smuzhiyun 	if (sizec) {
1125*4882a593Smuzhiyun 		prop = fdt_getprop(blob, parentoffset, "#size-cells", NULL);
1126*4882a593Smuzhiyun 		if (prop)
1127*4882a593Smuzhiyun 			*sizec = be32_to_cpup(prop);
1128*4882a593Smuzhiyun 		else
1129*4882a593Smuzhiyun 			*sizec = 1;
1130*4882a593Smuzhiyun 	}
1131*4882a593Smuzhiyun }
1132*4882a593Smuzhiyun 
of_bus_default_map(fdt32_t * addr,const fdt32_t * range,int na,int ns,int pna)1133*4882a593Smuzhiyun static u64 of_bus_default_map(fdt32_t *addr, const fdt32_t *range,
1134*4882a593Smuzhiyun 		int na, int ns, int pna)
1135*4882a593Smuzhiyun {
1136*4882a593Smuzhiyun 	u64 cp, s, da;
1137*4882a593Smuzhiyun 
1138*4882a593Smuzhiyun 	cp = fdt_read_number(range, na);
1139*4882a593Smuzhiyun 	s  = fdt_read_number(range + na + pna, ns);
1140*4882a593Smuzhiyun 	da = fdt_read_number(addr, na);
1141*4882a593Smuzhiyun 
1142*4882a593Smuzhiyun 	debug("OF: default map, cp=%" PRIu64 ", s=%" PRIu64
1143*4882a593Smuzhiyun 	      ", da=%" PRIu64 "\n", cp, s, da);
1144*4882a593Smuzhiyun 
1145*4882a593Smuzhiyun 	if (da < cp || da >= (cp + s))
1146*4882a593Smuzhiyun 		return OF_BAD_ADDR;
1147*4882a593Smuzhiyun 	return da - cp;
1148*4882a593Smuzhiyun }
1149*4882a593Smuzhiyun 
of_bus_default_translate(fdt32_t * addr,u64 offset,int na)1150*4882a593Smuzhiyun static int of_bus_default_translate(fdt32_t *addr, u64 offset, int na)
1151*4882a593Smuzhiyun {
1152*4882a593Smuzhiyun 	u64 a = fdt_read_number(addr, na);
1153*4882a593Smuzhiyun 	memset(addr, 0, na * 4);
1154*4882a593Smuzhiyun 	a += offset;
1155*4882a593Smuzhiyun 	if (na > 1)
1156*4882a593Smuzhiyun 		addr[na - 2] = cpu_to_fdt32(a >> 32);
1157*4882a593Smuzhiyun 	addr[na - 1] = cpu_to_fdt32(a & 0xffffffffu);
1158*4882a593Smuzhiyun 
1159*4882a593Smuzhiyun 	return 0;
1160*4882a593Smuzhiyun }
1161*4882a593Smuzhiyun 
1162*4882a593Smuzhiyun #ifdef CONFIG_OF_ISA_BUS
1163*4882a593Smuzhiyun 
1164*4882a593Smuzhiyun /* ISA bus translator */
of_bus_isa_match(const void * blob,int parentoffset)1165*4882a593Smuzhiyun static int of_bus_isa_match(const void *blob, int parentoffset)
1166*4882a593Smuzhiyun {
1167*4882a593Smuzhiyun 	const char *name;
1168*4882a593Smuzhiyun 
1169*4882a593Smuzhiyun 	name = fdt_get_name(blob, parentoffset, NULL);
1170*4882a593Smuzhiyun 	if (!name)
1171*4882a593Smuzhiyun 		return 0;
1172*4882a593Smuzhiyun 
1173*4882a593Smuzhiyun 	return !strcmp(name, "isa");
1174*4882a593Smuzhiyun }
1175*4882a593Smuzhiyun 
of_bus_isa_count_cells(const void * blob,int parentoffset,int * addrc,int * sizec)1176*4882a593Smuzhiyun static void of_bus_isa_count_cells(const void *blob, int parentoffset,
1177*4882a593Smuzhiyun 				   int *addrc, int *sizec)
1178*4882a593Smuzhiyun {
1179*4882a593Smuzhiyun 	if (addrc)
1180*4882a593Smuzhiyun 		*addrc = 2;
1181*4882a593Smuzhiyun 	if (sizec)
1182*4882a593Smuzhiyun 		*sizec = 1;
1183*4882a593Smuzhiyun }
1184*4882a593Smuzhiyun 
of_bus_isa_map(fdt32_t * addr,const fdt32_t * range,int na,int ns,int pna)1185*4882a593Smuzhiyun static u64 of_bus_isa_map(fdt32_t *addr, const fdt32_t *range,
1186*4882a593Smuzhiyun 			  int na, int ns, int pna)
1187*4882a593Smuzhiyun {
1188*4882a593Smuzhiyun 	u64 cp, s, da;
1189*4882a593Smuzhiyun 
1190*4882a593Smuzhiyun 	/* Check address type match */
1191*4882a593Smuzhiyun 	if ((addr[0] ^ range[0]) & cpu_to_be32(1))
1192*4882a593Smuzhiyun 		return OF_BAD_ADDR;
1193*4882a593Smuzhiyun 
1194*4882a593Smuzhiyun 	cp = fdt_read_number(range + 1, na - 1);
1195*4882a593Smuzhiyun 	s  = fdt_read_number(range + na + pna, ns);
1196*4882a593Smuzhiyun 	da = fdt_read_number(addr + 1, na - 1);
1197*4882a593Smuzhiyun 
1198*4882a593Smuzhiyun 	debug("OF: ISA map, cp=%" PRIu64 ", s=%" PRIu64
1199*4882a593Smuzhiyun 	      ", da=%" PRIu64 "\n", cp, s, da);
1200*4882a593Smuzhiyun 
1201*4882a593Smuzhiyun 	if (da < cp || da >= (cp + s))
1202*4882a593Smuzhiyun 		return OF_BAD_ADDR;
1203*4882a593Smuzhiyun 	return da - cp;
1204*4882a593Smuzhiyun }
1205*4882a593Smuzhiyun 
of_bus_isa_translate(fdt32_t * addr,u64 offset,int na)1206*4882a593Smuzhiyun static int of_bus_isa_translate(fdt32_t *addr, u64 offset, int na)
1207*4882a593Smuzhiyun {
1208*4882a593Smuzhiyun 	return of_bus_default_translate(addr + 1, offset, na - 1);
1209*4882a593Smuzhiyun }
1210*4882a593Smuzhiyun 
1211*4882a593Smuzhiyun #endif /* CONFIG_OF_ISA_BUS */
1212*4882a593Smuzhiyun 
1213*4882a593Smuzhiyun /* Array of bus specific translators */
1214*4882a593Smuzhiyun static struct of_bus of_busses[] = {
1215*4882a593Smuzhiyun #ifdef CONFIG_OF_ISA_BUS
1216*4882a593Smuzhiyun 	/* ISA */
1217*4882a593Smuzhiyun 	{
1218*4882a593Smuzhiyun 		.name = "isa",
1219*4882a593Smuzhiyun 		.addresses = "reg",
1220*4882a593Smuzhiyun 		.match = of_bus_isa_match,
1221*4882a593Smuzhiyun 		.count_cells = of_bus_isa_count_cells,
1222*4882a593Smuzhiyun 		.map = of_bus_isa_map,
1223*4882a593Smuzhiyun 		.translate = of_bus_isa_translate,
1224*4882a593Smuzhiyun 	},
1225*4882a593Smuzhiyun #endif /* CONFIG_OF_ISA_BUS */
1226*4882a593Smuzhiyun 	/* Default */
1227*4882a593Smuzhiyun 	{
1228*4882a593Smuzhiyun 		.name = "default",
1229*4882a593Smuzhiyun 		.addresses = "reg",
1230*4882a593Smuzhiyun 		.count_cells = fdt_support_default_count_cells,
1231*4882a593Smuzhiyun 		.map = of_bus_default_map,
1232*4882a593Smuzhiyun 		.translate = of_bus_default_translate,
1233*4882a593Smuzhiyun 	},
1234*4882a593Smuzhiyun };
1235*4882a593Smuzhiyun 
of_match_bus(const void * blob,int parentoffset)1236*4882a593Smuzhiyun static struct of_bus *of_match_bus(const void *blob, int parentoffset)
1237*4882a593Smuzhiyun {
1238*4882a593Smuzhiyun 	struct of_bus *bus;
1239*4882a593Smuzhiyun 
1240*4882a593Smuzhiyun 	if (ARRAY_SIZE(of_busses) == 1)
1241*4882a593Smuzhiyun 		return of_busses;
1242*4882a593Smuzhiyun 
1243*4882a593Smuzhiyun 	for (bus = of_busses; bus; bus++) {
1244*4882a593Smuzhiyun 		if (!bus->match || bus->match(blob, parentoffset))
1245*4882a593Smuzhiyun 			return bus;
1246*4882a593Smuzhiyun 	}
1247*4882a593Smuzhiyun 
1248*4882a593Smuzhiyun 	/*
1249*4882a593Smuzhiyun 	 * We should always have matched the default bus at least, since
1250*4882a593Smuzhiyun 	 * it has a NULL match field. If we didn't then it somehow isn't
1251*4882a593Smuzhiyun 	 * in the of_busses array or something equally catastrophic has
1252*4882a593Smuzhiyun 	 * gone wrong.
1253*4882a593Smuzhiyun 	 */
1254*4882a593Smuzhiyun 	assert(0);
1255*4882a593Smuzhiyun 	return NULL;
1256*4882a593Smuzhiyun }
1257*4882a593Smuzhiyun 
of_translate_one(const void * blob,int parent,struct of_bus * bus,struct of_bus * pbus,fdt32_t * addr,int na,int ns,int pna,const char * rprop)1258*4882a593Smuzhiyun static int of_translate_one(const void *blob, int parent, struct of_bus *bus,
1259*4882a593Smuzhiyun 			    struct of_bus *pbus, fdt32_t *addr,
1260*4882a593Smuzhiyun 			    int na, int ns, int pna, const char *rprop)
1261*4882a593Smuzhiyun {
1262*4882a593Smuzhiyun 	const fdt32_t *ranges;
1263*4882a593Smuzhiyun 	int rlen;
1264*4882a593Smuzhiyun 	int rone;
1265*4882a593Smuzhiyun 	u64 offset = OF_BAD_ADDR;
1266*4882a593Smuzhiyun 
1267*4882a593Smuzhiyun 	/* Normally, an absence of a "ranges" property means we are
1268*4882a593Smuzhiyun 	 * crossing a non-translatable boundary, and thus the addresses
1269*4882a593Smuzhiyun 	 * below the current not cannot be converted to CPU physical ones.
1270*4882a593Smuzhiyun 	 * Unfortunately, while this is very clear in the spec, it's not
1271*4882a593Smuzhiyun 	 * what Apple understood, and they do have things like /uni-n or
1272*4882a593Smuzhiyun 	 * /ht nodes with no "ranges" property and a lot of perfectly
1273*4882a593Smuzhiyun 	 * useable mapped devices below them. Thus we treat the absence of
1274*4882a593Smuzhiyun 	 * "ranges" as equivalent to an empty "ranges" property which means
1275*4882a593Smuzhiyun 	 * a 1:1 translation at that level. It's up to the caller not to try
1276*4882a593Smuzhiyun 	 * to translate addresses that aren't supposed to be translated in
1277*4882a593Smuzhiyun 	 * the first place. --BenH.
1278*4882a593Smuzhiyun 	 */
1279*4882a593Smuzhiyun 	ranges = fdt_getprop(blob, parent, rprop, &rlen);
1280*4882a593Smuzhiyun 	if (ranges == NULL || rlen == 0) {
1281*4882a593Smuzhiyun 		offset = fdt_read_number(addr, na);
1282*4882a593Smuzhiyun 		memset(addr, 0, pna * 4);
1283*4882a593Smuzhiyun 		debug("OF: no ranges, 1:1 translation\n");
1284*4882a593Smuzhiyun 		goto finish;
1285*4882a593Smuzhiyun 	}
1286*4882a593Smuzhiyun 
1287*4882a593Smuzhiyun 	debug("OF: walking ranges...\n");
1288*4882a593Smuzhiyun 
1289*4882a593Smuzhiyun 	/* Now walk through the ranges */
1290*4882a593Smuzhiyun 	rlen /= 4;
1291*4882a593Smuzhiyun 	rone = na + pna + ns;
1292*4882a593Smuzhiyun 	for (; rlen >= rone; rlen -= rone, ranges += rone) {
1293*4882a593Smuzhiyun 		offset = bus->map(addr, ranges, na, ns, pna);
1294*4882a593Smuzhiyun 		if (offset != OF_BAD_ADDR)
1295*4882a593Smuzhiyun 			break;
1296*4882a593Smuzhiyun 	}
1297*4882a593Smuzhiyun 	if (offset == OF_BAD_ADDR) {
1298*4882a593Smuzhiyun 		debug("OF: not found !\n");
1299*4882a593Smuzhiyun 		return 1;
1300*4882a593Smuzhiyun 	}
1301*4882a593Smuzhiyun 	memcpy(addr, ranges + na, 4 * pna);
1302*4882a593Smuzhiyun 
1303*4882a593Smuzhiyun  finish:
1304*4882a593Smuzhiyun 	of_dump_addr("OF: parent translation for:", addr, pna);
1305*4882a593Smuzhiyun 	debug("OF: with offset: %" PRIu64 "\n", offset);
1306*4882a593Smuzhiyun 
1307*4882a593Smuzhiyun 	/* Translate it into parent bus space */
1308*4882a593Smuzhiyun 	return pbus->translate(addr, offset, pna);
1309*4882a593Smuzhiyun }
1310*4882a593Smuzhiyun 
1311*4882a593Smuzhiyun /*
1312*4882a593Smuzhiyun  * Translate an address from the device-tree into a CPU physical address,
1313*4882a593Smuzhiyun  * this walks up the tree and applies the various bus mappings on the
1314*4882a593Smuzhiyun  * way.
1315*4882a593Smuzhiyun  *
1316*4882a593Smuzhiyun  * Note: We consider that crossing any level with #size-cells == 0 to mean
1317*4882a593Smuzhiyun  * that translation is impossible (that is we are not dealing with a value
1318*4882a593Smuzhiyun  * that can be mapped to a cpu physical address). This is not really specified
1319*4882a593Smuzhiyun  * that way, but this is traditionally the way IBM at least do things
1320*4882a593Smuzhiyun  */
__of_translate_address(const void * blob,int node_offset,const fdt32_t * in_addr,const char * rprop)1321*4882a593Smuzhiyun static u64 __of_translate_address(const void *blob, int node_offset,
1322*4882a593Smuzhiyun 				  const fdt32_t *in_addr, const char *rprop)
1323*4882a593Smuzhiyun {
1324*4882a593Smuzhiyun 	int parent;
1325*4882a593Smuzhiyun 	struct of_bus *bus, *pbus;
1326*4882a593Smuzhiyun 	fdt32_t addr[OF_MAX_ADDR_CELLS];
1327*4882a593Smuzhiyun 	int na, ns, pna, pns;
1328*4882a593Smuzhiyun 	u64 result = OF_BAD_ADDR;
1329*4882a593Smuzhiyun 
1330*4882a593Smuzhiyun 	debug("OF: ** translation for device %s **\n",
1331*4882a593Smuzhiyun 		fdt_get_name(blob, node_offset, NULL));
1332*4882a593Smuzhiyun 
1333*4882a593Smuzhiyun 	/* Get parent & match bus type */
1334*4882a593Smuzhiyun 	parent = fdt_parent_offset(blob, node_offset);
1335*4882a593Smuzhiyun 	if (parent < 0)
1336*4882a593Smuzhiyun 		goto bail;
1337*4882a593Smuzhiyun 	bus = of_match_bus(blob, parent);
1338*4882a593Smuzhiyun 
1339*4882a593Smuzhiyun 	/* Cound address cells & copy address locally */
1340*4882a593Smuzhiyun 	bus->count_cells(blob, parent, &na, &ns);
1341*4882a593Smuzhiyun 	if (!OF_CHECK_COUNTS(na, ns)) {
1342*4882a593Smuzhiyun 		printf("%s: Bad cell count for %s\n", __FUNCTION__,
1343*4882a593Smuzhiyun 		       fdt_get_name(blob, node_offset, NULL));
1344*4882a593Smuzhiyun 		goto bail;
1345*4882a593Smuzhiyun 	}
1346*4882a593Smuzhiyun 	memcpy(addr, in_addr, na * 4);
1347*4882a593Smuzhiyun 
1348*4882a593Smuzhiyun 	debug("OF: bus is %s (na=%d, ns=%d) on %s\n",
1349*4882a593Smuzhiyun 	    bus->name, na, ns, fdt_get_name(blob, parent, NULL));
1350*4882a593Smuzhiyun 	of_dump_addr("OF: translating address:", addr, na);
1351*4882a593Smuzhiyun 
1352*4882a593Smuzhiyun 	/* Translate */
1353*4882a593Smuzhiyun 	for (;;) {
1354*4882a593Smuzhiyun 		/* Switch to parent bus */
1355*4882a593Smuzhiyun 		node_offset = parent;
1356*4882a593Smuzhiyun 		parent = fdt_parent_offset(blob, node_offset);
1357*4882a593Smuzhiyun 
1358*4882a593Smuzhiyun 		/* If root, we have finished */
1359*4882a593Smuzhiyun 		if (parent < 0) {
1360*4882a593Smuzhiyun 			debug("OF: reached root node\n");
1361*4882a593Smuzhiyun 			result = fdt_read_number(addr, na);
1362*4882a593Smuzhiyun 			break;
1363*4882a593Smuzhiyun 		}
1364*4882a593Smuzhiyun 
1365*4882a593Smuzhiyun 		/* Get new parent bus and counts */
1366*4882a593Smuzhiyun 		pbus = of_match_bus(blob, parent);
1367*4882a593Smuzhiyun 		pbus->count_cells(blob, parent, &pna, &pns);
1368*4882a593Smuzhiyun 		if (!OF_CHECK_COUNTS(pna, pns)) {
1369*4882a593Smuzhiyun 			printf("%s: Bad cell count for %s\n", __FUNCTION__,
1370*4882a593Smuzhiyun 				fdt_get_name(blob, node_offset, NULL));
1371*4882a593Smuzhiyun 			break;
1372*4882a593Smuzhiyun 		}
1373*4882a593Smuzhiyun 
1374*4882a593Smuzhiyun 		debug("OF: parent bus is %s (na=%d, ns=%d) on %s\n",
1375*4882a593Smuzhiyun 		    pbus->name, pna, pns, fdt_get_name(blob, parent, NULL));
1376*4882a593Smuzhiyun 
1377*4882a593Smuzhiyun 		/* Apply bus translation */
1378*4882a593Smuzhiyun 		if (of_translate_one(blob, node_offset, bus, pbus,
1379*4882a593Smuzhiyun 					addr, na, ns, pna, rprop))
1380*4882a593Smuzhiyun 			break;
1381*4882a593Smuzhiyun 
1382*4882a593Smuzhiyun 		/* Complete the move up one level */
1383*4882a593Smuzhiyun 		na = pna;
1384*4882a593Smuzhiyun 		ns = pns;
1385*4882a593Smuzhiyun 		bus = pbus;
1386*4882a593Smuzhiyun 
1387*4882a593Smuzhiyun 		of_dump_addr("OF: one level translation:", addr, na);
1388*4882a593Smuzhiyun 	}
1389*4882a593Smuzhiyun  bail:
1390*4882a593Smuzhiyun 
1391*4882a593Smuzhiyun 	return result;
1392*4882a593Smuzhiyun }
1393*4882a593Smuzhiyun 
fdt_translate_address(const void * blob,int node_offset,const fdt32_t * in_addr)1394*4882a593Smuzhiyun u64 fdt_translate_address(const void *blob, int node_offset,
1395*4882a593Smuzhiyun 			  const fdt32_t *in_addr)
1396*4882a593Smuzhiyun {
1397*4882a593Smuzhiyun 	return __of_translate_address(blob, node_offset, in_addr, "ranges");
1398*4882a593Smuzhiyun }
1399*4882a593Smuzhiyun 
1400*4882a593Smuzhiyun /**
1401*4882a593Smuzhiyun  * fdt_node_offset_by_compat_reg: Find a node that matches compatiable and
1402*4882a593Smuzhiyun  * who's reg property matches a physical cpu address
1403*4882a593Smuzhiyun  *
1404*4882a593Smuzhiyun  * @blob: ptr to device tree
1405*4882a593Smuzhiyun  * @compat: compatiable string to match
1406*4882a593Smuzhiyun  * @compat_off: property name
1407*4882a593Smuzhiyun  *
1408*4882a593Smuzhiyun  */
fdt_node_offset_by_compat_reg(void * blob,const char * compat,phys_addr_t compat_off)1409*4882a593Smuzhiyun int fdt_node_offset_by_compat_reg(void *blob, const char *compat,
1410*4882a593Smuzhiyun 					phys_addr_t compat_off)
1411*4882a593Smuzhiyun {
1412*4882a593Smuzhiyun 	int len, off = fdt_node_offset_by_compatible(blob, -1, compat);
1413*4882a593Smuzhiyun 	while (off != -FDT_ERR_NOTFOUND) {
1414*4882a593Smuzhiyun 		const fdt32_t *reg = fdt_getprop(blob, off, "reg", &len);
1415*4882a593Smuzhiyun 		if (reg) {
1416*4882a593Smuzhiyun 			if (compat_off == fdt_translate_address(blob, off, reg))
1417*4882a593Smuzhiyun 				return off;
1418*4882a593Smuzhiyun 		}
1419*4882a593Smuzhiyun 		off = fdt_node_offset_by_compatible(blob, off, compat);
1420*4882a593Smuzhiyun 	}
1421*4882a593Smuzhiyun 
1422*4882a593Smuzhiyun 	return -FDT_ERR_NOTFOUND;
1423*4882a593Smuzhiyun }
1424*4882a593Smuzhiyun 
1425*4882a593Smuzhiyun /**
1426*4882a593Smuzhiyun  * fdt_alloc_phandle: Return next free phandle value
1427*4882a593Smuzhiyun  *
1428*4882a593Smuzhiyun  * @blob: ptr to device tree
1429*4882a593Smuzhiyun  */
fdt_alloc_phandle(void * blob)1430*4882a593Smuzhiyun int fdt_alloc_phandle(void *blob)
1431*4882a593Smuzhiyun {
1432*4882a593Smuzhiyun 	int offset;
1433*4882a593Smuzhiyun 	uint32_t phandle = 0;
1434*4882a593Smuzhiyun 
1435*4882a593Smuzhiyun 	for (offset = fdt_next_node(blob, -1, NULL); offset >= 0;
1436*4882a593Smuzhiyun 	     offset = fdt_next_node(blob, offset, NULL)) {
1437*4882a593Smuzhiyun 		phandle = max(phandle, fdt_get_phandle(blob, offset));
1438*4882a593Smuzhiyun 	}
1439*4882a593Smuzhiyun 
1440*4882a593Smuzhiyun 	return phandle + 1;
1441*4882a593Smuzhiyun }
1442*4882a593Smuzhiyun 
1443*4882a593Smuzhiyun /*
1444*4882a593Smuzhiyun  * fdt_set_phandle: Create a phandle property for the given node
1445*4882a593Smuzhiyun  *
1446*4882a593Smuzhiyun  * @fdt: ptr to device tree
1447*4882a593Smuzhiyun  * @nodeoffset: node to update
1448*4882a593Smuzhiyun  * @phandle: phandle value to set (must be unique)
1449*4882a593Smuzhiyun  */
fdt_set_phandle(void * fdt,int nodeoffset,uint32_t phandle)1450*4882a593Smuzhiyun int fdt_set_phandle(void *fdt, int nodeoffset, uint32_t phandle)
1451*4882a593Smuzhiyun {
1452*4882a593Smuzhiyun 	int ret;
1453*4882a593Smuzhiyun 
1454*4882a593Smuzhiyun #ifdef DEBUG
1455*4882a593Smuzhiyun 	int off = fdt_node_offset_by_phandle(fdt, phandle);
1456*4882a593Smuzhiyun 
1457*4882a593Smuzhiyun 	if ((off >= 0) && (off != nodeoffset)) {
1458*4882a593Smuzhiyun 		char buf[64];
1459*4882a593Smuzhiyun 
1460*4882a593Smuzhiyun 		fdt_get_path(fdt, nodeoffset, buf, sizeof(buf));
1461*4882a593Smuzhiyun 		printf("Trying to update node %s with phandle %u ",
1462*4882a593Smuzhiyun 		       buf, phandle);
1463*4882a593Smuzhiyun 
1464*4882a593Smuzhiyun 		fdt_get_path(fdt, off, buf, sizeof(buf));
1465*4882a593Smuzhiyun 		printf("that already exists in node %s.\n", buf);
1466*4882a593Smuzhiyun 		return -FDT_ERR_BADPHANDLE;
1467*4882a593Smuzhiyun 	}
1468*4882a593Smuzhiyun #endif
1469*4882a593Smuzhiyun 
1470*4882a593Smuzhiyun 	ret = fdt_setprop_cell(fdt, nodeoffset, "phandle", phandle);
1471*4882a593Smuzhiyun 	if (ret < 0)
1472*4882a593Smuzhiyun 		return ret;
1473*4882a593Smuzhiyun 
1474*4882a593Smuzhiyun 	/*
1475*4882a593Smuzhiyun 	 * For now, also set the deprecated "linux,phandle" property, so that we
1476*4882a593Smuzhiyun 	 * don't break older kernels.
1477*4882a593Smuzhiyun 	 */
1478*4882a593Smuzhiyun 	ret = fdt_setprop_cell(fdt, nodeoffset, "linux,phandle", phandle);
1479*4882a593Smuzhiyun 
1480*4882a593Smuzhiyun 	return ret;
1481*4882a593Smuzhiyun }
1482*4882a593Smuzhiyun 
1483*4882a593Smuzhiyun /*
1484*4882a593Smuzhiyun  * fdt_create_phandle: Create a phandle property for the given node
1485*4882a593Smuzhiyun  *
1486*4882a593Smuzhiyun  * @fdt: ptr to device tree
1487*4882a593Smuzhiyun  * @nodeoffset: node to update
1488*4882a593Smuzhiyun  */
fdt_create_phandle(void * fdt,int nodeoffset)1489*4882a593Smuzhiyun unsigned int fdt_create_phandle(void *fdt, int nodeoffset)
1490*4882a593Smuzhiyun {
1491*4882a593Smuzhiyun 	/* see if there is a phandle already */
1492*4882a593Smuzhiyun 	int phandle = fdt_get_phandle(fdt, nodeoffset);
1493*4882a593Smuzhiyun 
1494*4882a593Smuzhiyun 	/* if we got 0, means no phandle so create one */
1495*4882a593Smuzhiyun 	if (phandle == 0) {
1496*4882a593Smuzhiyun 		int ret;
1497*4882a593Smuzhiyun 
1498*4882a593Smuzhiyun 		phandle = fdt_alloc_phandle(fdt);
1499*4882a593Smuzhiyun 		ret = fdt_set_phandle(fdt, nodeoffset, phandle);
1500*4882a593Smuzhiyun 		if (ret < 0) {
1501*4882a593Smuzhiyun 			printf("Can't set phandle %u: %s\n", phandle,
1502*4882a593Smuzhiyun 			       fdt_strerror(ret));
1503*4882a593Smuzhiyun 			return 0;
1504*4882a593Smuzhiyun 		}
1505*4882a593Smuzhiyun 	}
1506*4882a593Smuzhiyun 
1507*4882a593Smuzhiyun 	return phandle;
1508*4882a593Smuzhiyun }
1509*4882a593Smuzhiyun 
1510*4882a593Smuzhiyun /*
1511*4882a593Smuzhiyun  * fdt_set_node_status: Set status for the given node
1512*4882a593Smuzhiyun  *
1513*4882a593Smuzhiyun  * @fdt: ptr to device tree
1514*4882a593Smuzhiyun  * @nodeoffset: node to update
1515*4882a593Smuzhiyun  * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED,
1516*4882a593Smuzhiyun  *	    FDT_STATUS_FAIL, FDT_STATUS_FAIL_ERROR_CODE
1517*4882a593Smuzhiyun  * @error_code: optional, only used if status is FDT_STATUS_FAIL_ERROR_CODE
1518*4882a593Smuzhiyun  */
fdt_set_node_status(void * fdt,int nodeoffset,enum fdt_status status,unsigned int error_code)1519*4882a593Smuzhiyun int fdt_set_node_status(void *fdt, int nodeoffset,
1520*4882a593Smuzhiyun 			enum fdt_status status, unsigned int error_code)
1521*4882a593Smuzhiyun {
1522*4882a593Smuzhiyun 	char buf[16];
1523*4882a593Smuzhiyun 	int ret = 0;
1524*4882a593Smuzhiyun 
1525*4882a593Smuzhiyun 	if (nodeoffset < 0)
1526*4882a593Smuzhiyun 		return nodeoffset;
1527*4882a593Smuzhiyun 
1528*4882a593Smuzhiyun 	switch (status) {
1529*4882a593Smuzhiyun 	case FDT_STATUS_OKAY:
1530*4882a593Smuzhiyun 		ret = fdt_setprop_string(fdt, nodeoffset, "status", "okay");
1531*4882a593Smuzhiyun 		break;
1532*4882a593Smuzhiyun 	case FDT_STATUS_DISABLED:
1533*4882a593Smuzhiyun 		ret = fdt_setprop_string(fdt, nodeoffset, "status", "disabled");
1534*4882a593Smuzhiyun 		break;
1535*4882a593Smuzhiyun 	case FDT_STATUS_FAIL:
1536*4882a593Smuzhiyun 		ret = fdt_setprop_string(fdt, nodeoffset, "status", "fail");
1537*4882a593Smuzhiyun 		break;
1538*4882a593Smuzhiyun 	case FDT_STATUS_FAIL_ERROR_CODE:
1539*4882a593Smuzhiyun 		sprintf(buf, "fail-%d", error_code);
1540*4882a593Smuzhiyun 		ret = fdt_setprop_string(fdt, nodeoffset, "status", buf);
1541*4882a593Smuzhiyun 		break;
1542*4882a593Smuzhiyun 	default:
1543*4882a593Smuzhiyun 		printf("Invalid fdt status: %x\n", status);
1544*4882a593Smuzhiyun 		ret = -1;
1545*4882a593Smuzhiyun 		break;
1546*4882a593Smuzhiyun 	}
1547*4882a593Smuzhiyun 
1548*4882a593Smuzhiyun 	return ret;
1549*4882a593Smuzhiyun }
1550*4882a593Smuzhiyun 
1551*4882a593Smuzhiyun /*
1552*4882a593Smuzhiyun  * fdt_set_status_by_alias: Set status for the given node given an alias
1553*4882a593Smuzhiyun  *
1554*4882a593Smuzhiyun  * @fdt: ptr to device tree
1555*4882a593Smuzhiyun  * @alias: alias of node to update
1556*4882a593Smuzhiyun  * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED,
1557*4882a593Smuzhiyun  *	    FDT_STATUS_FAIL, FDT_STATUS_FAIL_ERROR_CODE
1558*4882a593Smuzhiyun  * @error_code: optional, only used if status is FDT_STATUS_FAIL_ERROR_CODE
1559*4882a593Smuzhiyun  */
fdt_set_status_by_alias(void * fdt,const char * alias,enum fdt_status status,unsigned int error_code)1560*4882a593Smuzhiyun int fdt_set_status_by_alias(void *fdt, const char* alias,
1561*4882a593Smuzhiyun 			    enum fdt_status status, unsigned int error_code)
1562*4882a593Smuzhiyun {
1563*4882a593Smuzhiyun 	int offset = fdt_path_offset(fdt, alias);
1564*4882a593Smuzhiyun 
1565*4882a593Smuzhiyun 	return fdt_set_node_status(fdt, offset, status, error_code);
1566*4882a593Smuzhiyun }
1567*4882a593Smuzhiyun 
1568*4882a593Smuzhiyun #if defined(CONFIG_VIDEO) || defined(CONFIG_LCD)
fdt_add_edid(void * blob,const char * compat,unsigned char * edid_buf)1569*4882a593Smuzhiyun int fdt_add_edid(void *blob, const char *compat, unsigned char *edid_buf)
1570*4882a593Smuzhiyun {
1571*4882a593Smuzhiyun 	int noff;
1572*4882a593Smuzhiyun 	int ret;
1573*4882a593Smuzhiyun 
1574*4882a593Smuzhiyun 	noff = fdt_node_offset_by_compatible(blob, -1, compat);
1575*4882a593Smuzhiyun 	if (noff != -FDT_ERR_NOTFOUND) {
1576*4882a593Smuzhiyun 		debug("%s: %s\n", fdt_get_name(blob, noff, 0), compat);
1577*4882a593Smuzhiyun add_edid:
1578*4882a593Smuzhiyun 		ret = fdt_setprop(blob, noff, "edid", edid_buf, 128);
1579*4882a593Smuzhiyun 		if (ret == -FDT_ERR_NOSPACE) {
1580*4882a593Smuzhiyun 			ret = fdt_increase_size(blob, 512);
1581*4882a593Smuzhiyun 			if (!ret)
1582*4882a593Smuzhiyun 				goto add_edid;
1583*4882a593Smuzhiyun 			else
1584*4882a593Smuzhiyun 				goto err_size;
1585*4882a593Smuzhiyun 		} else if (ret < 0) {
1586*4882a593Smuzhiyun 			printf("Can't add property: %s\n", fdt_strerror(ret));
1587*4882a593Smuzhiyun 			return ret;
1588*4882a593Smuzhiyun 		}
1589*4882a593Smuzhiyun 	}
1590*4882a593Smuzhiyun 	return 0;
1591*4882a593Smuzhiyun err_size:
1592*4882a593Smuzhiyun 	printf("Can't increase blob size: %s\n", fdt_strerror(ret));
1593*4882a593Smuzhiyun 	return ret;
1594*4882a593Smuzhiyun }
1595*4882a593Smuzhiyun #endif
1596*4882a593Smuzhiyun 
1597*4882a593Smuzhiyun /*
1598*4882a593Smuzhiyun  * Verify the physical address of device tree node for a given alias
1599*4882a593Smuzhiyun  *
1600*4882a593Smuzhiyun  * This function locates the device tree node of a given alias, and then
1601*4882a593Smuzhiyun  * verifies that the physical address of that device matches the given
1602*4882a593Smuzhiyun  * parameter.  It displays a message if there is a mismatch.
1603*4882a593Smuzhiyun  *
1604*4882a593Smuzhiyun  * Returns 1 on success, 0 on failure
1605*4882a593Smuzhiyun  */
fdt_verify_alias_address(void * fdt,int anode,const char * alias,u64 addr)1606*4882a593Smuzhiyun int fdt_verify_alias_address(void *fdt, int anode, const char *alias, u64 addr)
1607*4882a593Smuzhiyun {
1608*4882a593Smuzhiyun 	const char *path;
1609*4882a593Smuzhiyun 	const fdt32_t *reg;
1610*4882a593Smuzhiyun 	int node, len;
1611*4882a593Smuzhiyun 	u64 dt_addr;
1612*4882a593Smuzhiyun 
1613*4882a593Smuzhiyun 	path = fdt_getprop(fdt, anode, alias, NULL);
1614*4882a593Smuzhiyun 	if (!path) {
1615*4882a593Smuzhiyun 		/* If there's no such alias, then it's not a failure */
1616*4882a593Smuzhiyun 		return 1;
1617*4882a593Smuzhiyun 	}
1618*4882a593Smuzhiyun 
1619*4882a593Smuzhiyun 	node = fdt_path_offset(fdt, path);
1620*4882a593Smuzhiyun 	if (node < 0) {
1621*4882a593Smuzhiyun 		printf("Warning: device tree alias '%s' points to invalid "
1622*4882a593Smuzhiyun 		       "node %s.\n", alias, path);
1623*4882a593Smuzhiyun 		return 0;
1624*4882a593Smuzhiyun 	}
1625*4882a593Smuzhiyun 
1626*4882a593Smuzhiyun 	reg = fdt_getprop(fdt, node, "reg", &len);
1627*4882a593Smuzhiyun 	if (!reg) {
1628*4882a593Smuzhiyun 		printf("Warning: device tree node '%s' has no address.\n",
1629*4882a593Smuzhiyun 		       path);
1630*4882a593Smuzhiyun 		return 0;
1631*4882a593Smuzhiyun 	}
1632*4882a593Smuzhiyun 
1633*4882a593Smuzhiyun 	dt_addr = fdt_translate_address(fdt, node, reg);
1634*4882a593Smuzhiyun 	if (addr != dt_addr) {
1635*4882a593Smuzhiyun 		printf("Warning: U-Boot configured device %s at address %"
1636*4882a593Smuzhiyun 		       PRIx64 ",\n but the device tree has it address %"
1637*4882a593Smuzhiyun 		       PRIx64 ".\n", alias, addr, dt_addr);
1638*4882a593Smuzhiyun 		return 0;
1639*4882a593Smuzhiyun 	}
1640*4882a593Smuzhiyun 
1641*4882a593Smuzhiyun 	return 1;
1642*4882a593Smuzhiyun }
1643*4882a593Smuzhiyun 
1644*4882a593Smuzhiyun /*
1645*4882a593Smuzhiyun  * Returns the base address of an SOC or PCI node
1646*4882a593Smuzhiyun  */
fdt_get_base_address(const void * fdt,int node)1647*4882a593Smuzhiyun u64 fdt_get_base_address(const void *fdt, int node)
1648*4882a593Smuzhiyun {
1649*4882a593Smuzhiyun 	int size;
1650*4882a593Smuzhiyun 	const fdt32_t *prop;
1651*4882a593Smuzhiyun 
1652*4882a593Smuzhiyun 	prop = fdt_getprop(fdt, node, "reg", &size);
1653*4882a593Smuzhiyun 
1654*4882a593Smuzhiyun 	return prop ? fdt_translate_address(fdt, node, prop) : 0;
1655*4882a593Smuzhiyun }
1656*4882a593Smuzhiyun 
1657*4882a593Smuzhiyun /*
1658*4882a593Smuzhiyun  * Read a property of size <prop_len>. Currently only supports 1 or 2 cells.
1659*4882a593Smuzhiyun  */
fdt_read_prop(const fdt32_t * prop,int prop_len,int cell_off,uint64_t * val,int cells)1660*4882a593Smuzhiyun static int fdt_read_prop(const fdt32_t *prop, int prop_len, int cell_off,
1661*4882a593Smuzhiyun 			 uint64_t *val, int cells)
1662*4882a593Smuzhiyun {
1663*4882a593Smuzhiyun 	const fdt32_t *prop32 = &prop[cell_off];
1664*4882a593Smuzhiyun 	const fdt64_t *prop64 = (const fdt64_t *)&prop[cell_off];
1665*4882a593Smuzhiyun 
1666*4882a593Smuzhiyun 	if ((cell_off + cells) > prop_len)
1667*4882a593Smuzhiyun 		return -FDT_ERR_NOSPACE;
1668*4882a593Smuzhiyun 
1669*4882a593Smuzhiyun 	switch (cells) {
1670*4882a593Smuzhiyun 	case 1:
1671*4882a593Smuzhiyun 		*val = fdt32_to_cpu(*prop32);
1672*4882a593Smuzhiyun 		break;
1673*4882a593Smuzhiyun 	case 2:
1674*4882a593Smuzhiyun 		*val = fdt64_to_cpu(*prop64);
1675*4882a593Smuzhiyun 		break;
1676*4882a593Smuzhiyun 	default:
1677*4882a593Smuzhiyun 		return -FDT_ERR_NOSPACE;
1678*4882a593Smuzhiyun 	}
1679*4882a593Smuzhiyun 
1680*4882a593Smuzhiyun 	return 0;
1681*4882a593Smuzhiyun }
1682*4882a593Smuzhiyun 
1683*4882a593Smuzhiyun /**
1684*4882a593Smuzhiyun  * fdt_read_range - Read a node's n'th range property
1685*4882a593Smuzhiyun  *
1686*4882a593Smuzhiyun  * @fdt: ptr to device tree
1687*4882a593Smuzhiyun  * @node: offset of node
1688*4882a593Smuzhiyun  * @n: range index
1689*4882a593Smuzhiyun  * @child_addr: pointer to storage for the "child address" field
1690*4882a593Smuzhiyun  * @addr: pointer to storage for the CPU view translated physical start
1691*4882a593Smuzhiyun  * @len: pointer to storage for the range length
1692*4882a593Smuzhiyun  *
1693*4882a593Smuzhiyun  * Convenience function that reads and interprets a specific range out of
1694*4882a593Smuzhiyun  * a number of the "ranges" property array.
1695*4882a593Smuzhiyun  */
fdt_read_range(void * fdt,int node,int n,uint64_t * child_addr,uint64_t * addr,uint64_t * len)1696*4882a593Smuzhiyun int fdt_read_range(void *fdt, int node, int n, uint64_t *child_addr,
1697*4882a593Smuzhiyun 		   uint64_t *addr, uint64_t *len)
1698*4882a593Smuzhiyun {
1699*4882a593Smuzhiyun 	int pnode = fdt_parent_offset(fdt, node);
1700*4882a593Smuzhiyun 	const fdt32_t *ranges;
1701*4882a593Smuzhiyun 	int pacells;
1702*4882a593Smuzhiyun 	int acells;
1703*4882a593Smuzhiyun 	int scells;
1704*4882a593Smuzhiyun 	int ranges_len;
1705*4882a593Smuzhiyun 	int cell = 0;
1706*4882a593Smuzhiyun 	int r = 0;
1707*4882a593Smuzhiyun 
1708*4882a593Smuzhiyun 	/*
1709*4882a593Smuzhiyun 	 * The "ranges" property is an array of
1710*4882a593Smuzhiyun 	 * { <child address> <parent address> <size in child address space> }
1711*4882a593Smuzhiyun 	 *
1712*4882a593Smuzhiyun 	 * All 3 elements can span a diffent number of cells. Fetch their size.
1713*4882a593Smuzhiyun 	 */
1714*4882a593Smuzhiyun 	pacells = fdt_getprop_u32_default_node(fdt, pnode, 0, "#address-cells", 1);
1715*4882a593Smuzhiyun 	acells = fdt_getprop_u32_default_node(fdt, node, 0, "#address-cells", 1);
1716*4882a593Smuzhiyun 	scells = fdt_getprop_u32_default_node(fdt, node, 0, "#size-cells", 1);
1717*4882a593Smuzhiyun 
1718*4882a593Smuzhiyun 	/* Now try to get the ranges property */
1719*4882a593Smuzhiyun 	ranges = fdt_getprop(fdt, node, "ranges", &ranges_len);
1720*4882a593Smuzhiyun 	if (!ranges)
1721*4882a593Smuzhiyun 		return -FDT_ERR_NOTFOUND;
1722*4882a593Smuzhiyun 	ranges_len /= sizeof(uint32_t);
1723*4882a593Smuzhiyun 
1724*4882a593Smuzhiyun 	/* Jump to the n'th entry */
1725*4882a593Smuzhiyun 	cell = n * (pacells + acells + scells);
1726*4882a593Smuzhiyun 
1727*4882a593Smuzhiyun 	/* Read <child address> */
1728*4882a593Smuzhiyun 	if (child_addr) {
1729*4882a593Smuzhiyun 		r = fdt_read_prop(ranges, ranges_len, cell, child_addr,
1730*4882a593Smuzhiyun 				  acells);
1731*4882a593Smuzhiyun 		if (r)
1732*4882a593Smuzhiyun 			return r;
1733*4882a593Smuzhiyun 	}
1734*4882a593Smuzhiyun 	cell += acells;
1735*4882a593Smuzhiyun 
1736*4882a593Smuzhiyun 	/* Read <parent address> */
1737*4882a593Smuzhiyun 	if (addr)
1738*4882a593Smuzhiyun 		*addr = fdt_translate_address(fdt, node, ranges + cell);
1739*4882a593Smuzhiyun 	cell += pacells;
1740*4882a593Smuzhiyun 
1741*4882a593Smuzhiyun 	/* Read <size in child address space> */
1742*4882a593Smuzhiyun 	if (len) {
1743*4882a593Smuzhiyun 		r = fdt_read_prop(ranges, ranges_len, cell, len, scells);
1744*4882a593Smuzhiyun 		if (r)
1745*4882a593Smuzhiyun 			return r;
1746*4882a593Smuzhiyun 	}
1747*4882a593Smuzhiyun 
1748*4882a593Smuzhiyun 	return 0;
1749*4882a593Smuzhiyun }
1750*4882a593Smuzhiyun 
1751*4882a593Smuzhiyun /**
1752*4882a593Smuzhiyun  * fdt_setup_simplefb_node - Fill and enable a simplefb node
1753*4882a593Smuzhiyun  *
1754*4882a593Smuzhiyun  * @fdt: ptr to device tree
1755*4882a593Smuzhiyun  * @node: offset of the simplefb node
1756*4882a593Smuzhiyun  * @base_address: framebuffer base address
1757*4882a593Smuzhiyun  * @width: width in pixels
1758*4882a593Smuzhiyun  * @height: height in pixels
1759*4882a593Smuzhiyun  * @stride: bytes per line
1760*4882a593Smuzhiyun  * @format: pixel format string
1761*4882a593Smuzhiyun  *
1762*4882a593Smuzhiyun  * Convenience function to fill and enable a simplefb node.
1763*4882a593Smuzhiyun  */
fdt_setup_simplefb_node(void * fdt,int node,u64 base_address,u32 width,u32 height,u32 stride,const char * format)1764*4882a593Smuzhiyun int fdt_setup_simplefb_node(void *fdt, int node, u64 base_address, u32 width,
1765*4882a593Smuzhiyun 			    u32 height, u32 stride, const char *format)
1766*4882a593Smuzhiyun {
1767*4882a593Smuzhiyun 	char name[32];
1768*4882a593Smuzhiyun 	fdt32_t cells[4];
1769*4882a593Smuzhiyun 	int i, addrc, sizec, ret;
1770*4882a593Smuzhiyun 
1771*4882a593Smuzhiyun 	fdt_support_default_count_cells(fdt, fdt_parent_offset(fdt, node),
1772*4882a593Smuzhiyun 					&addrc, &sizec);
1773*4882a593Smuzhiyun 	i = 0;
1774*4882a593Smuzhiyun 	if (addrc == 2)
1775*4882a593Smuzhiyun 		cells[i++] = cpu_to_fdt32(base_address >> 32);
1776*4882a593Smuzhiyun 	cells[i++] = cpu_to_fdt32(base_address);
1777*4882a593Smuzhiyun 	if (sizec == 2)
1778*4882a593Smuzhiyun 		cells[i++] = 0;
1779*4882a593Smuzhiyun 	cells[i++] = cpu_to_fdt32(height * stride);
1780*4882a593Smuzhiyun 
1781*4882a593Smuzhiyun 	ret = fdt_setprop(fdt, node, "reg", cells, sizeof(cells[0]) * i);
1782*4882a593Smuzhiyun 	if (ret < 0)
1783*4882a593Smuzhiyun 		return ret;
1784*4882a593Smuzhiyun 
1785*4882a593Smuzhiyun 	snprintf(name, sizeof(name), "framebuffer@%" PRIx64, base_address);
1786*4882a593Smuzhiyun 	ret = fdt_set_name(fdt, node, name);
1787*4882a593Smuzhiyun 	if (ret < 0)
1788*4882a593Smuzhiyun 		return ret;
1789*4882a593Smuzhiyun 
1790*4882a593Smuzhiyun 	ret = fdt_setprop_u32(fdt, node, "width", width);
1791*4882a593Smuzhiyun 	if (ret < 0)
1792*4882a593Smuzhiyun 		return ret;
1793*4882a593Smuzhiyun 
1794*4882a593Smuzhiyun 	ret = fdt_setprop_u32(fdt, node, "height", height);
1795*4882a593Smuzhiyun 	if (ret < 0)
1796*4882a593Smuzhiyun 		return ret;
1797*4882a593Smuzhiyun 
1798*4882a593Smuzhiyun 	ret = fdt_setprop_u32(fdt, node, "stride", stride);
1799*4882a593Smuzhiyun 	if (ret < 0)
1800*4882a593Smuzhiyun 		return ret;
1801*4882a593Smuzhiyun 
1802*4882a593Smuzhiyun 	ret = fdt_setprop_string(fdt, node, "format", format);
1803*4882a593Smuzhiyun 	if (ret < 0)
1804*4882a593Smuzhiyun 		return ret;
1805*4882a593Smuzhiyun 
1806*4882a593Smuzhiyun 	ret = fdt_setprop_string(fdt, node, "status", "okay");
1807*4882a593Smuzhiyun 	if (ret < 0)
1808*4882a593Smuzhiyun 		return ret;
1809*4882a593Smuzhiyun 
1810*4882a593Smuzhiyun 	return 0;
1811*4882a593Smuzhiyun }
1812*4882a593Smuzhiyun 
1813*4882a593Smuzhiyun /*
1814*4882a593Smuzhiyun  * Update native-mode in display-timings from display environment variable.
1815*4882a593Smuzhiyun  * The node to update are specified by path.
1816*4882a593Smuzhiyun  */
fdt_fixup_display(void * blob,const char * path,const char * display)1817*4882a593Smuzhiyun int fdt_fixup_display(void *blob, const char *path, const char *display)
1818*4882a593Smuzhiyun {
1819*4882a593Smuzhiyun 	int off, toff;
1820*4882a593Smuzhiyun 
1821*4882a593Smuzhiyun 	if (!display || !path)
1822*4882a593Smuzhiyun 		return -FDT_ERR_NOTFOUND;
1823*4882a593Smuzhiyun 
1824*4882a593Smuzhiyun 	toff = fdt_path_offset(blob, path);
1825*4882a593Smuzhiyun 	if (toff >= 0)
1826*4882a593Smuzhiyun 		toff = fdt_subnode_offset(blob, toff, "display-timings");
1827*4882a593Smuzhiyun 	if (toff < 0)
1828*4882a593Smuzhiyun 		return toff;
1829*4882a593Smuzhiyun 
1830*4882a593Smuzhiyun 	for (off = fdt_first_subnode(blob, toff);
1831*4882a593Smuzhiyun 	     off >= 0;
1832*4882a593Smuzhiyun 	     off = fdt_next_subnode(blob, off)) {
1833*4882a593Smuzhiyun 		uint32_t h = fdt_get_phandle(blob, off);
1834*4882a593Smuzhiyun 		debug("%s:0x%x\n", fdt_get_name(blob, off, NULL),
1835*4882a593Smuzhiyun 		      fdt32_to_cpu(h));
1836*4882a593Smuzhiyun 		if (strcasecmp(fdt_get_name(blob, off, NULL), display) == 0)
1837*4882a593Smuzhiyun 			return fdt_setprop_u32(blob, toff, "native-mode", h);
1838*4882a593Smuzhiyun 	}
1839*4882a593Smuzhiyun 	return toff;
1840*4882a593Smuzhiyun }
1841*4882a593Smuzhiyun 
1842*4882a593Smuzhiyun #ifdef CONFIG_OF_LIBFDT_OVERLAY
1843*4882a593Smuzhiyun /**
1844*4882a593Smuzhiyun  * fdt_overlay_apply_verbose - Apply an overlay with verbose error reporting
1845*4882a593Smuzhiyun  *
1846*4882a593Smuzhiyun  * @fdt: ptr to device tree
1847*4882a593Smuzhiyun  * @fdto: ptr to device tree overlay
1848*4882a593Smuzhiyun  *
1849*4882a593Smuzhiyun  * Convenience function to apply an overlay and display helpful messages
1850*4882a593Smuzhiyun  * in the case of an error
1851*4882a593Smuzhiyun  */
fdt_overlay_apply_verbose(void * fdt,void * fdto)1852*4882a593Smuzhiyun int fdt_overlay_apply_verbose(void *fdt, void *fdto)
1853*4882a593Smuzhiyun {
1854*4882a593Smuzhiyun 	int err;
1855*4882a593Smuzhiyun 	bool has_symbols;
1856*4882a593Smuzhiyun 
1857*4882a593Smuzhiyun 	err = fdt_path_offset(fdt, "/__symbols__");
1858*4882a593Smuzhiyun 	has_symbols = err >= 0;
1859*4882a593Smuzhiyun 
1860*4882a593Smuzhiyun 	err = fdt_overlay_apply(fdt, fdto);
1861*4882a593Smuzhiyun 	if (err < 0) {
1862*4882a593Smuzhiyun 		printf("failed on fdt_overlay_apply(): %s\n",
1863*4882a593Smuzhiyun 				fdt_strerror(err));
1864*4882a593Smuzhiyun 		if (!has_symbols) {
1865*4882a593Smuzhiyun 			printf("base fdt does did not have a /__symbols__ node\n");
1866*4882a593Smuzhiyun 			printf("make sure you've compiled with -@\n");
1867*4882a593Smuzhiyun 		}
1868*4882a593Smuzhiyun 	}
1869*4882a593Smuzhiyun 	return err;
1870*4882a593Smuzhiyun }
1871*4882a593Smuzhiyun #endif
1872