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/bootmem.h>
22 #include <linux/of_pdt.h>
23 #include <asm/olpc_ofw.h>
25 static phandle __init olpc_dt_getsibling(phandle node)
27 const void *args[] = { (void *)node };
28 void *res[] = { &node };
33 if (olpc_ofw("peer", args, res) || (s32)node == -1)
39 static phandle __init olpc_dt_getchild(phandle node)
41 const void *args[] = { (void *)node };
42 void *res[] = { &node };
47 if (olpc_ofw("child", args, res) || (s32)node == -1) {
48 pr_err("PROM: %s: fetching child failed!\n", __func__);
55 static int __init olpc_dt_getproplen(phandle node, const char *prop)
57 const void *args[] = { (void *)node, prop };
59 void *res[] = { &len };
64 if (olpc_ofw("getproplen", args, res)) {
65 pr_err("PROM: %s: getproplen failed!\n", __func__);
72 static int __init olpc_dt_getproperty(phandle node, const char *prop,
73 char *buf, int bufsize)
77 plen = olpc_dt_getproplen(node, prop);
78 if (plen > bufsize || plen < 1) {
81 const void *args[] = { (void *)node, prop, buf, (void *)plen };
82 void *res[] = { &plen };
84 if (olpc_ofw("getprop", args, res)) {
85 pr_err("PROM: %s: getprop failed!\n", __func__);
93 static int __init olpc_dt_nextprop(phandle node, char *prev, char *buf)
95 const void *args[] = { (void *)node, prev, buf };
97 void *res[] = { &success };
104 if (olpc_ofw("nextprop", args, res) || success != 1)
110 static int __init olpc_dt_pkg2path(phandle node, char *buf,
111 const int buflen, int *len)
113 const void *args[] = { (void *)node, buf, (void *)buflen };
114 void *res[] = { len };
119 if (olpc_ofw("package-to-path", args, res) || *len < 1)
125 static unsigned int prom_early_allocated __initdata;
127 void * __init prom_early_alloc(unsigned long size)
130 static size_t free_mem;
133 if (free_mem < size) {
134 const size_t chunk_size = max(PAGE_SIZE, size);
137 * To mimimize the number of allocations, grab at least
138 * PAGE_SIZE of memory (that's an arbitrary choice that's
139 * fast enough on the platforms we care about while minimizing
140 * wasted bootmem) and hand off chunks of it to callers.
142 res = alloc_bootmem(chunk_size);
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 void __init olpc_dt_build_devicetree(void)
170 if (!olpc_ofw_is_installed())
173 root = olpc_dt_getsibling(0);
175 pr_err("PROM: unable to get root node from OFW!\n");
178 of_pdt_build_devicetree(root, &prom_olpc_ops);
180 pr_info("PROM DT: Built device tree with %u bytes of memory.\n",
181 prom_early_allocated);