kvm tools, setup: Create private directory
[linux-2.6/next.git] / arch / x86 / platform / olpc / olpc_dt.c
blobd39f63d017d2860aecc2f8b0237a56da5c3ca4c7
1 /*
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>
21 #include <linux/of.h>
22 #include <linux/of_platform.h>
23 #include <linux/of_pdt.h>
24 #include <asm/olpc.h>
25 #include <asm/olpc_ofw.h>
27 static phandle __init olpc_dt_getsibling(phandle node)
29 const void *args[] = { (void *)node };
30 void *res[] = { &node };
32 if ((s32)node == -1)
33 return 0;
35 if (olpc_ofw("peer", args, res) || (s32)node == -1)
36 return 0;
38 return node;
41 static phandle __init olpc_dt_getchild(phandle node)
43 const void *args[] = { (void *)node };
44 void *res[] = { &node };
46 if ((s32)node == -1)
47 return 0;
49 if (olpc_ofw("child", args, res) || (s32)node == -1) {
50 pr_err("PROM: %s: fetching child failed!\n", __func__);
51 return 0;
54 return node;
57 static int __init olpc_dt_getproplen(phandle node, const char *prop)
59 const void *args[] = { (void *)node, prop };
60 int len;
61 void *res[] = { &len };
63 if ((s32)node == -1)
64 return -1;
66 if (olpc_ofw("getproplen", args, res)) {
67 pr_err("PROM: %s: getproplen failed!\n", __func__);
68 return -1;
71 return len;
74 static int __init olpc_dt_getproperty(phandle node, const char *prop,
75 char *buf, int bufsize)
77 int plen;
79 plen = olpc_dt_getproplen(node, prop);
80 if (plen > bufsize || plen < 1) {
81 return -1;
82 } else {
83 const void *args[] = { (void *)node, prop, buf, (void *)plen };
84 void *res[] = { &plen };
86 if (olpc_ofw("getprop", args, res)) {
87 pr_err("PROM: %s: getprop failed!\n", __func__);
88 return -1;
92 return plen;
95 static int __init olpc_dt_nextprop(phandle node, char *prev, char *buf)
97 const void *args[] = { (void *)node, prev, buf };
98 int success;
99 void *res[] = { &success };
101 buf[0] = '\0';
103 if ((s32)node == -1)
104 return -1;
106 if (olpc_ofw("nextprop", args, res) || success != 1)
107 return -1;
109 return 0;
112 static int __init olpc_dt_pkg2path(phandle node, char *buf,
113 const int buflen, int *len)
115 const void *args[] = { (void *)node, buf, (void *)buflen };
116 void *res[] = { len };
118 if ((s32)node == -1)
119 return -1;
121 if (olpc_ofw("package-to-path", args, res) || *len < 1)
122 return -1;
124 return 0;
127 static unsigned int prom_early_allocated __initdata;
129 void * __init prom_early_alloc(unsigned long size)
131 static u8 *mem;
132 static size_t free_mem;
133 void *res;
135 if (free_mem < size) {
136 const size_t chunk_size = max(PAGE_SIZE, size);
139 * To mimimize the number of allocations, grab at least
140 * PAGE_SIZE of memory (that's an arbitrary choice that's
141 * fast enough on the platforms we care about while minimizing
142 * wasted bootmem) and hand off chunks of it to callers.
144 res = alloc_bootmem(chunk_size);
145 BUG_ON(!res);
146 prom_early_allocated += chunk_size;
147 memset(res, 0, chunk_size);
148 free_mem = chunk_size;
149 mem = res;
152 /* allocate from the local cache */
153 free_mem -= size;
154 res = mem;
155 mem += size;
156 return res;
159 static struct of_pdt_ops prom_olpc_ops __initdata = {
160 .nextprop = olpc_dt_nextprop,
161 .getproplen = olpc_dt_getproplen,
162 .getproperty = olpc_dt_getproperty,
163 .getchild = olpc_dt_getchild,
164 .getsibling = olpc_dt_getsibling,
165 .pkg2path = olpc_dt_pkg2path,
168 void __init olpc_dt_build_devicetree(void)
170 phandle root;
172 if (!olpc_ofw_is_installed())
173 return;
175 root = olpc_dt_getsibling(0);
176 if (!root) {
177 pr_err("PROM: unable to get root node from OFW!\n");
178 return;
180 of_pdt_build_devicetree(root, &prom_olpc_ops);
182 pr_info("PROM DT: Built device tree with %u bytes of memory.\n",
183 prom_early_allocated);
186 /* A list of DT node/bus matches that we want to expose as platform devices */
187 static struct of_device_id __initdata of_ids[] = {
188 { .compatible = "olpc,xo1-battery" },
189 { .compatible = "olpc,xo1-dcon" },
190 { .compatible = "olpc,xo1-rtc" },
194 static int __init olpc_create_platform_devices(void)
196 if (machine_is_olpc())
197 return of_platform_bus_probe(NULL, of_ids, NULL);
198 else
199 return 0;
201 device_initcall(olpc_create_platform_devices);