2 * OLPC-specific OFW device tree support code.
4 * Paul Mackerras August 1996.
5 * Copyright (C) 1996-2005 Paul Mackerras.
7 * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
8 * {engebret|bergner}@us.ibm.com
10 * Adapted for sparc by David S. Miller davem@davemloft.net
11 * Adapted for x86/OLPC by Andres Salomon <dilinger@queued.net>
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version
16 * 2 of the License, or (at your option) any later version.
19 #include <linux/kernel.h>
20 #include <linux/memblock.h>
22 #include <linux/of_pdt.h>
24 #include <asm/olpc_ofw.h>
26 static phandle __init olpc_dt_getsibling(phandle node)
28 const void *args[] = { (void *)node };
29 void *res[] = { &node };
34 if (olpc_ofw("peer", args, res) || (s32)node == -1)
40 static phandle __init olpc_dt_getchild(phandle node)
42 const void *args[] = { (void *)node };
43 void *res[] = { &node };
48 if (olpc_ofw("child", args, res) || (s32)node == -1) {
49 pr_err("PROM: %s: fetching child failed!\n", __func__);
56 static int __init olpc_dt_getproplen(phandle node, const char *prop)
58 const void *args[] = { (void *)node, prop };
60 void *res[] = { &len };
65 if (olpc_ofw("getproplen", args, res)) {
66 pr_err("PROM: %s: getproplen failed!\n", __func__);
73 static int __init olpc_dt_getproperty(phandle node, const char *prop,
74 char *buf, int bufsize)
78 plen = olpc_dt_getproplen(node, prop);
79 if (plen > bufsize || plen < 1) {
82 const void *args[] = { (void *)node, prop, buf, (void *)plen };
83 void *res[] = { &plen };
85 if (olpc_ofw("getprop", args, res)) {
86 pr_err("PROM: %s: getprop failed!\n", __func__);
94 static int __init olpc_dt_nextprop(phandle node, char *prev, char *buf)
96 const void *args[] = { (void *)node, prev, buf };
98 void *res[] = { &success };
105 if (olpc_ofw("nextprop", args, res) || success != 1)
111 static int __init olpc_dt_pkg2path(phandle node, char *buf,
112 const int buflen, int *len)
114 const void *args[] = { (void *)node, buf, (void *)buflen };
115 void *res[] = { len };
120 if (olpc_ofw("package-to-path", args, res) || *len < 1)
126 static unsigned int prom_early_allocated __initdata;
128 void * __init prom_early_alloc(unsigned long size)
131 static size_t free_mem;
134 if (free_mem < size) {
135 const size_t chunk_size = max(PAGE_SIZE, size);
138 * To mimimize the number of allocations, grab at least
139 * PAGE_SIZE of memory (that's an arbitrary choice that's
140 * fast enough on the platforms we care about while minimizing
141 * wasted bootmem) and hand off chunks of it to callers.
143 res = memblock_alloc(chunk_size, SMP_CACHE_BYTES);
145 panic("%s: Failed to allocate %zu bytes\n", __func__,
148 prom_early_allocated += chunk_size;
149 memset(res, 0, chunk_size);
150 free_mem = chunk_size;
154 /* allocate from the local cache */
161 static struct of_pdt_ops prom_olpc_ops __initdata = {
162 .nextprop = olpc_dt_nextprop,
163 .getproplen = olpc_dt_getproplen,
164 .getproperty = olpc_dt_getproperty,
165 .getchild = olpc_dt_getchild,
166 .getsibling = olpc_dt_getsibling,
167 .pkg2path = olpc_dt_pkg2path,
170 static phandle __init olpc_dt_finddevice(const char *path)
173 const void *args[] = { path };
174 void *res[] = { &node };
176 if (olpc_ofw("finddevice", args, res)) {
177 pr_err("olpc_dt: finddevice failed!\n");
181 if ((s32) node == -1)
187 static int __init olpc_dt_interpret(const char *words)
190 const void *args[] = { words };
191 void *res[] = { &result };
193 if (olpc_ofw("interpret", args, res)) {
194 pr_err("olpc_dt: interpret failed!\n");
202 * Extract board revision directly from OFW device tree.
203 * We can't use olpc_platform_info because that hasn't been set up yet.
205 static u32 __init olpc_dt_get_board_revision(void)
211 node = olpc_dt_finddevice("/");
215 r = olpc_dt_getproperty(node, "board-revision-int",
216 (char *) &rev, sizeof(rev));
220 return be32_to_cpu(rev);
223 static int __init olpc_dt_compatible_match(phandle node, const char *compat)
228 plen = olpc_dt_getproperty(node, "compatible", buf, sizeof(buf));
232 len = strlen(compat);
233 for (p = buf; p < buf + plen; p += strlen(p) + 1) {
234 if (strcmp(p, compat) == 0)
241 void __init olpc_dt_fixup(void)
246 node = olpc_dt_finddevice("/battery@0");
250 board_rev = olpc_dt_get_board_revision();
254 if (board_rev >= olpc_board_pre(0xd0)) {
257 if (olpc_dt_compatible_match(node, "olpc,xo1.5-battery"))
260 /* Add olpc,xo1.5-battery compatible marker to battery node */
261 olpc_dt_interpret("\" /battery@0\" find-device");
262 olpc_dt_interpret(" \" olpc,xo1.5-battery\" +compatible");
263 olpc_dt_interpret("device-end");
265 if (olpc_dt_compatible_match(node, "olpc,xo1-battery")) {
267 * If we have a olpc,xo1-battery compatible, then we're
268 * running a new enough firmware that already has
274 /* Add dcon device */
275 olpc_dt_interpret("\" /pci/display@1\" find-device");
276 olpc_dt_interpret(" new-device");
277 olpc_dt_interpret(" \" dcon\" device-name");
278 olpc_dt_interpret(" \" olpc,xo1-dcon\" +compatible");
279 olpc_dt_interpret(" finish-device");
280 olpc_dt_interpret("device-end");
284 if (olpc_dt_compatible_match(node, "olpc,xo1-battery")) {
286 * If we have a olpc,xo1-battery compatible, then we're
287 * running a new enough firmware that already has
288 * the dcon and RTC nodes.
293 /* Add dcon device, mark RTC as olpc,xo1-rtc */
294 olpc_dt_interpret("\" /pci/display@1,1\" find-device");
295 olpc_dt_interpret(" new-device");
296 olpc_dt_interpret(" \" dcon\" device-name");
297 olpc_dt_interpret(" \" olpc,xo1-dcon\" +compatible");
298 olpc_dt_interpret(" finish-device");
299 olpc_dt_interpret("device-end");
301 olpc_dt_interpret("\" /rtc\" find-device");
302 olpc_dt_interpret(" \" olpc,xo1-rtc\" +compatible");
303 olpc_dt_interpret("device-end");
306 /* Add olpc,xo1-battery compatible marker to battery node */
307 olpc_dt_interpret("\" /battery@0\" find-device");
308 olpc_dt_interpret(" \" olpc,xo1-battery\" +compatible");
309 olpc_dt_interpret("device-end");
312 void __init olpc_dt_build_devicetree(void)
316 if (!olpc_ofw_is_installed())
321 root = olpc_dt_getsibling(0);
323 pr_err("PROM: unable to get root node from OFW!\n");
326 of_pdt_build_devicetree(root, &prom_olpc_ops);
328 pr_info("PROM DT: Built device tree with %u bytes of memory.\n",
329 prom_early_allocated);