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 void __init olpc_dt_fixup(void)
230 node = olpc_dt_finddevice("/battery@0");
235 * If the battery node has a compatible property, we are running a new
236 * enough firmware and don't have fixups to make.
238 r = olpc_dt_getproperty(node, "compatible", buf, sizeof(buf));
242 pr_info("PROM DT: Old firmware detected, applying fixes\n");
244 /* Add olpc,xo1-battery compatible marker to battery node */
245 olpc_dt_interpret("\" /battery@0\" find-device"
246 " \" olpc,xo1-battery\" +compatible"
249 board_rev = olpc_dt_get_board_revision();
253 if (board_rev >= olpc_board_pre(0xd0)) {
254 /* XO-1.5: add dcon device */
255 olpc_dt_interpret("\" /pci/display@1\" find-device"
257 " \" dcon\" device-name \" olpc,xo1-dcon\" +compatible"
258 " finish-device device-end");
260 /* XO-1: add dcon device, mark RTC as olpc,xo1-rtc */
261 olpc_dt_interpret("\" /pci/display@1,1\" find-device"
263 " \" dcon\" device-name \" olpc,xo1-dcon\" +compatible"
264 " finish-device device-end"
265 " \" /rtc\" find-device"
266 " \" olpc,xo1-rtc\" +compatible"
271 void __init olpc_dt_build_devicetree(void)
275 if (!olpc_ofw_is_installed())
280 root = olpc_dt_getsibling(0);
282 pr_err("PROM: unable to get root node from OFW!\n");
285 of_pdt_build_devicetree(root, &prom_olpc_ops);
287 pr_info("PROM DT: Built device tree with %u bytes of memory.\n",
288 prom_early_allocated);