1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * OLPC-specific OFW device tree support code.
5 * Paul Mackerras August 1996.
6 * Copyright (C) 1996-2005 Paul Mackerras.
8 * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
9 * {engebret|bergner}@us.ibm.com
11 * Adapted for sparc by David S. Miller davem@davemloft.net
12 * Adapted for x86/OLPC by Andres Salomon <dilinger@queued.net>
15 #include <linux/kernel.h>
16 #include <linux/memblock.h>
18 #include <linux/of_pdt.h>
20 #include <asm/olpc_ofw.h>
22 static phandle __init
olpc_dt_getsibling(phandle node
)
24 const void *args
[] = { (void *)node
};
25 void *res
[] = { &node
};
30 if (olpc_ofw("peer", args
, res
) || (s32
)node
== -1)
36 static phandle __init
olpc_dt_getchild(phandle node
)
38 const void *args
[] = { (void *)node
};
39 void *res
[] = { &node
};
44 if (olpc_ofw("child", args
, res
) || (s32
)node
== -1) {
45 pr_err("PROM: %s: fetching child failed!\n", __func__
);
52 static int __init
olpc_dt_getproplen(phandle node
, const char *prop
)
54 const void *args
[] = { (void *)node
, prop
};
56 void *res
[] = { &len
};
61 if (olpc_ofw("getproplen", args
, res
)) {
62 pr_err("PROM: %s: getproplen failed!\n", __func__
);
69 static int __init
olpc_dt_getproperty(phandle node
, const char *prop
,
70 char *buf
, int bufsize
)
74 plen
= olpc_dt_getproplen(node
, prop
);
75 if (plen
> bufsize
|| plen
< 1) {
78 const void *args
[] = { (void *)node
, prop
, buf
, (void *)plen
};
79 void *res
[] = { &plen
};
81 if (olpc_ofw("getprop", args
, res
)) {
82 pr_err("PROM: %s: getprop failed!\n", __func__
);
90 static int __init
olpc_dt_nextprop(phandle node
, char *prev
, char *buf
)
92 const void *args
[] = { (void *)node
, prev
, buf
};
94 void *res
[] = { &success
};
101 if (olpc_ofw("nextprop", args
, res
) || success
!= 1)
107 static int __init
olpc_dt_pkg2path(phandle node
, char *buf
,
108 const int buflen
, int *len
)
110 const void *args
[] = { (void *)node
, buf
, (void *)buflen
};
111 void *res
[] = { len
};
116 if (olpc_ofw("package-to-path", args
, res
) || *len
< 1)
122 static unsigned int prom_early_allocated __initdata
;
124 void * __init
prom_early_alloc(unsigned long size
)
127 static size_t free_mem
;
130 if (free_mem
< size
) {
131 const size_t chunk_size
= max(PAGE_SIZE
, size
);
134 * To mimimize the number of allocations, grab at least
135 * PAGE_SIZE of memory (that's an arbitrary choice that's
136 * fast enough on the platforms we care about while minimizing
137 * wasted bootmem) and hand off chunks of it to callers.
139 res
= memblock_alloc(chunk_size
, SMP_CACHE_BYTES
);
141 panic("%s: Failed to allocate %zu bytes\n", __func__
,
144 prom_early_allocated
+= chunk_size
;
145 memset(res
, 0, chunk_size
);
146 free_mem
= chunk_size
;
150 /* allocate from the local cache */
157 static struct of_pdt_ops prom_olpc_ops __initdata
= {
158 .nextprop
= olpc_dt_nextprop
,
159 .getproplen
= olpc_dt_getproplen
,
160 .getproperty
= olpc_dt_getproperty
,
161 .getchild
= olpc_dt_getchild
,
162 .getsibling
= olpc_dt_getsibling
,
163 .pkg2path
= olpc_dt_pkg2path
,
166 static phandle __init
olpc_dt_finddevice(const char *path
)
169 const void *args
[] = { path
};
170 void *res
[] = { &node
};
172 if (olpc_ofw("finddevice", args
, res
)) {
173 pr_err("olpc_dt: finddevice failed!\n");
177 if ((s32
) node
== -1)
183 static int __init
olpc_dt_interpret(const char *words
)
186 const void *args
[] = { words
};
187 void *res
[] = { &result
};
189 if (olpc_ofw("interpret", args
, res
)) {
190 pr_err("olpc_dt: interpret failed!\n");
198 * Extract board revision directly from OFW device tree.
199 * We can't use olpc_platform_info because that hasn't been set up yet.
201 static u32 __init
olpc_dt_get_board_revision(void)
207 node
= olpc_dt_finddevice("/");
211 r
= olpc_dt_getproperty(node
, "board-revision-int",
212 (char *) &rev
, sizeof(rev
));
216 return be32_to_cpu(rev
);
219 static int __init
olpc_dt_compatible_match(phandle node
, const char *compat
)
224 plen
= olpc_dt_getproperty(node
, "compatible", buf
, sizeof(buf
));
228 len
= strlen(compat
);
229 for (p
= buf
; p
< buf
+ plen
; p
+= strlen(p
) + 1) {
230 if (strcmp(p
, compat
) == 0)
237 void __init
olpc_dt_fixup(void)
242 node
= olpc_dt_finddevice("/battery@0");
246 board_rev
= olpc_dt_get_board_revision();
250 if (board_rev
>= olpc_board_pre(0xd0)) {
253 if (olpc_dt_compatible_match(node
, "olpc,xo1.5-battery"))
256 /* Add olpc,xo1.5-battery compatible marker to battery node */
257 olpc_dt_interpret("\" /battery@0\" find-device");
258 olpc_dt_interpret(" \" olpc,xo1.5-battery\" +compatible");
259 olpc_dt_interpret("device-end");
261 if (olpc_dt_compatible_match(node
, "olpc,xo1-battery")) {
263 * If we have a olpc,xo1-battery compatible, then we're
264 * running a new enough firmware that already has
270 /* Add dcon device */
271 olpc_dt_interpret("\" /pci/display@1\" find-device");
272 olpc_dt_interpret(" new-device");
273 olpc_dt_interpret(" \" dcon\" device-name");
274 olpc_dt_interpret(" \" olpc,xo1-dcon\" +compatible");
275 olpc_dt_interpret(" finish-device");
276 olpc_dt_interpret("device-end");
280 if (olpc_dt_compatible_match(node
, "olpc,xo1-battery")) {
282 * If we have a olpc,xo1-battery compatible, then we're
283 * running a new enough firmware that already has
284 * the dcon and RTC nodes.
289 /* Add dcon device, mark RTC as olpc,xo1-rtc */
290 olpc_dt_interpret("\" /pci/display@1,1\" find-device");
291 olpc_dt_interpret(" new-device");
292 olpc_dt_interpret(" \" dcon\" device-name");
293 olpc_dt_interpret(" \" olpc,xo1-dcon\" +compatible");
294 olpc_dt_interpret(" finish-device");
295 olpc_dt_interpret("device-end");
297 olpc_dt_interpret("\" /rtc\" find-device");
298 olpc_dt_interpret(" \" olpc,xo1-rtc\" +compatible");
299 olpc_dt_interpret("device-end");
302 /* Add olpc,xo1-battery compatible marker to battery node */
303 olpc_dt_interpret("\" /battery@0\" find-device");
304 olpc_dt_interpret(" \" olpc,xo1-battery\" +compatible");
305 olpc_dt_interpret("device-end");
308 void __init
olpc_dt_build_devicetree(void)
312 if (!olpc_ofw_is_installed())
317 root
= olpc_dt_getsibling(0);
319 pr_err("PROM: unable to get root node from OFW!\n");
322 of_pdt_build_devicetree(root
, &prom_olpc_ops
);
324 pr_info("PROM DT: Built device tree with %u bytes of memory.\n",
325 prom_early_allocated
);