xref: /openbmc/linux/arch/x86/platform/olpc/olpc_dt.c (revision 023e41632e065d49bcbe31b3c4b336217f96a271)
1 /*
2  * OLPC-specific OFW device tree support code.
3  *
4  * Paul Mackerras	August 1996.
5  * Copyright (C) 1996-2005 Paul Mackerras.
6  *
7  *  Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
8  *    {engebret|bergner}@us.ibm.com
9  *
10  *  Adapted for sparc by David S. Miller davem@davemloft.net
11  *  Adapted for x86/OLPC by Andres Salomon <dilinger@queued.net>
12  *
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.
17  */
18 
19 #include <linux/kernel.h>
20 #include <linux/memblock.h>
21 #include <linux/of.h>
22 #include <linux/of_pdt.h>
23 #include <asm/olpc.h>
24 #include <asm/olpc_ofw.h>
25 
26 static phandle __init olpc_dt_getsibling(phandle node)
27 {
28 	const void *args[] = { (void *)node };
29 	void *res[] = { &node };
30 
31 	if ((s32)node == -1)
32 		return 0;
33 
34 	if (olpc_ofw("peer", args, res) || (s32)node == -1)
35 		return 0;
36 
37 	return node;
38 }
39 
40 static phandle __init olpc_dt_getchild(phandle node)
41 {
42 	const void *args[] = { (void *)node };
43 	void *res[] = { &node };
44 
45 	if ((s32)node == -1)
46 		return 0;
47 
48 	if (olpc_ofw("child", args, res) || (s32)node == -1) {
49 		pr_err("PROM: %s: fetching child failed!\n", __func__);
50 		return 0;
51 	}
52 
53 	return node;
54 }
55 
56 static int __init olpc_dt_getproplen(phandle node, const char *prop)
57 {
58 	const void *args[] = { (void *)node, prop };
59 	int len;
60 	void *res[] = { &len };
61 
62 	if ((s32)node == -1)
63 		return -1;
64 
65 	if (olpc_ofw("getproplen", args, res)) {
66 		pr_err("PROM: %s: getproplen failed!\n", __func__);
67 		return -1;
68 	}
69 
70 	return len;
71 }
72 
73 static int __init olpc_dt_getproperty(phandle node, const char *prop,
74 		char *buf, int bufsize)
75 {
76 	int plen;
77 
78 	plen = olpc_dt_getproplen(node, prop);
79 	if (plen > bufsize || plen < 1) {
80 		return -1;
81 	} else {
82 		const void *args[] = { (void *)node, prop, buf, (void *)plen };
83 		void *res[] = { &plen };
84 
85 		if (olpc_ofw("getprop", args, res)) {
86 			pr_err("PROM: %s: getprop failed!\n", __func__);
87 			return -1;
88 		}
89 	}
90 
91 	return plen;
92 }
93 
94 static int __init olpc_dt_nextprop(phandle node, char *prev, char *buf)
95 {
96 	const void *args[] = { (void *)node, prev, buf };
97 	int success;
98 	void *res[] = { &success };
99 
100 	buf[0] = '\0';
101 
102 	if ((s32)node == -1)
103 		return -1;
104 
105 	if (olpc_ofw("nextprop", args, res) || success != 1)
106 		return -1;
107 
108 	return 0;
109 }
110 
111 static int __init olpc_dt_pkg2path(phandle node, char *buf,
112 		const int buflen, int *len)
113 {
114 	const void *args[] = { (void *)node, buf, (void *)buflen };
115 	void *res[] = { len };
116 
117 	if ((s32)node == -1)
118 		return -1;
119 
120 	if (olpc_ofw("package-to-path", args, res) || *len < 1)
121 		return -1;
122 
123 	return 0;
124 }
125 
126 static unsigned int prom_early_allocated __initdata;
127 
128 void * __init prom_early_alloc(unsigned long size)
129 {
130 	static u8 *mem;
131 	static size_t free_mem;
132 	void *res;
133 
134 	if (free_mem < size) {
135 		const size_t chunk_size = max(PAGE_SIZE, size);
136 
137 		/*
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.
142 		 */
143 		res = memblock_alloc(chunk_size, SMP_CACHE_BYTES);
144 		if (!res)
145 			panic("%s: Failed to allocate %zu bytes\n", __func__,
146 			      chunk_size);
147 		BUG_ON(!res);
148 		prom_early_allocated += chunk_size;
149 		memset(res, 0, chunk_size);
150 		free_mem = chunk_size;
151 		mem = res;
152 	}
153 
154 	/* allocate from the local cache */
155 	free_mem -= size;
156 	res = mem;
157 	mem += size;
158 	return res;
159 }
160 
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,
168 };
169 
170 static phandle __init olpc_dt_finddevice(const char *path)
171 {
172 	phandle node;
173 	const void *args[] = { path };
174 	void *res[] = { &node };
175 
176 	if (olpc_ofw("finddevice", args, res)) {
177 		pr_err("olpc_dt: finddevice failed!\n");
178 		return 0;
179 	}
180 
181 	if ((s32) node == -1)
182 		return 0;
183 
184 	return node;
185 }
186 
187 static int __init olpc_dt_interpret(const char *words)
188 {
189 	int result;
190 	const void *args[] = { words };
191 	void *res[] = { &result };
192 
193 	if (olpc_ofw("interpret", args, res)) {
194 		pr_err("olpc_dt: interpret failed!\n");
195 		return -1;
196 	}
197 
198 	return result;
199 }
200 
201 /*
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.
204  */
205 static u32 __init olpc_dt_get_board_revision(void)
206 {
207 	phandle node;
208 	__be32 rev;
209 	int r;
210 
211 	node = olpc_dt_finddevice("/");
212 	if (!node)
213 		return 0;
214 
215 	r = olpc_dt_getproperty(node, "board-revision-int",
216 				(char *) &rev, sizeof(rev));
217 	if (r < 0)
218 		return 0;
219 
220 	return be32_to_cpu(rev);
221 }
222 
223 void __init olpc_dt_fixup(void)
224 {
225 	int r;
226 	char buf[64];
227 	phandle node;
228 	u32 board_rev;
229 
230 	node = olpc_dt_finddevice("/battery@0");
231 	if (!node)
232 		return;
233 
234 	/*
235 	 * If the battery node has a compatible property, we are running a new
236 	 * enough firmware and don't have fixups to make.
237 	 */
238 	r = olpc_dt_getproperty(node, "compatible", buf, sizeof(buf));
239 	if (r > 0)
240 		return;
241 
242 	pr_info("PROM DT: Old firmware detected, applying fixes\n");
243 
244 	/* Add olpc,xo1-battery compatible marker to battery node */
245 	olpc_dt_interpret("\" /battery@0\" find-device"
246 		" \" olpc,xo1-battery\" +compatible"
247 		" device-end");
248 
249 	board_rev = olpc_dt_get_board_revision();
250 	if (!board_rev)
251 		return;
252 
253 	if (board_rev >= olpc_board_pre(0xd0)) {
254 		/* XO-1.5: add dcon device */
255 		olpc_dt_interpret("\" /pci/display@1\" find-device"
256 			" new-device"
257 			" \" dcon\" device-name \" olpc,xo1-dcon\" +compatible"
258 			" finish-device device-end");
259 	} else {
260 		/* XO-1: add dcon device, mark RTC as olpc,xo1-rtc */
261 		olpc_dt_interpret("\" /pci/display@1,1\" find-device"
262 			" new-device"
263 			" \" dcon\" device-name \" olpc,xo1-dcon\" +compatible"
264 			" finish-device device-end"
265 			" \" /rtc\" find-device"
266 			" \" olpc,xo1-rtc\" +compatible"
267 			" device-end");
268 	}
269 }
270 
271 void __init olpc_dt_build_devicetree(void)
272 {
273 	phandle root;
274 
275 	if (!olpc_ofw_is_installed())
276 		return;
277 
278 	olpc_dt_fixup();
279 
280 	root = olpc_dt_getsibling(0);
281 	if (!root) {
282 		pr_err("PROM: unable to get root node from OFW!\n");
283 		return;
284 	}
285 	of_pdt_build_devicetree(root, &prom_olpc_ops);
286 
287 	pr_info("PROM DT: Built device tree with %u bytes of memory.\n",
288 			prom_early_allocated);
289 }
290