ofwoea_machdep.c revision 1.2.2.2 1 1.2.2.2 ad /* $NetBSD: ofwoea_machdep.c,v 1.2.2.2 2007/10/23 20:36:10 ad Exp $ */
2 1.2.2.2 ad
3 1.2.2.2 ad /*-
4 1.2.2.2 ad * Copyright (c) 2007 The NetBSD Foundation, Inc.
5 1.2.2.2 ad * All rights reserved.
6 1.2.2.2 ad *
7 1.2.2.2 ad * This code is derived from software contributed to The NetBSD Foundation
8 1.2.2.2 ad * by Tim Rightnour
9 1.2.2.2 ad *
10 1.2.2.2 ad * Redistribution and use in source and binary forms, with or without
11 1.2.2.2 ad * modification, are permitted provided that the following conditions
12 1.2.2.2 ad * are met:
13 1.2.2.2 ad * 1. Redistributions of source code must retain the above copyright
14 1.2.2.2 ad * notice, this list of conditions and the following disclaimer.
15 1.2.2.2 ad * 2. Redistributions in binary form must reproduce the above copyright
16 1.2.2.2 ad * notice, this list of conditions and the following disclaimer in the
17 1.2.2.2 ad * documentation and/or other materials provided with the distribution.
18 1.2.2.2 ad * 3. All advertising materials mentioning features or use of this software
19 1.2.2.2 ad * must display the following acknowledgement:
20 1.2.2.2 ad * This product includes software developed by the NetBSD
21 1.2.2.2 ad * Foundation, Inc. and its contributors.
22 1.2.2.2 ad * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.2.2.2 ad * contributors may be used to endorse or promote products derived
24 1.2.2.2 ad * from this software without specific prior written permission.
25 1.2.2.2 ad *
26 1.2.2.2 ad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.2.2.2 ad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.2.2.2 ad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.2.2.2 ad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.2.2.2 ad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.2.2.2 ad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.2.2.2 ad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.2.2.2 ad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.2.2.2 ad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.2.2.2 ad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.2.2.2 ad * POSSIBILITY OF SUCH DAMAGE.
37 1.2.2.2 ad */
38 1.2.2.2 ad
39 1.2.2.2 ad #include <sys/cdefs.h>
40 1.2.2.2 ad __KERNEL_RCSID(0, "$NetBSD: ofwoea_machdep.c,v 1.2.2.2 2007/10/23 20:36:10 ad Exp $");
41 1.2.2.2 ad
42 1.2.2.2 ad
43 1.2.2.2 ad #include "opt_compat_netbsd.h"
44 1.2.2.2 ad #include "opt_ddb.h"
45 1.2.2.2 ad #include "opt_kgdb.h"
46 1.2.2.2 ad #include "opt_ipkdb.h"
47 1.2.2.2 ad
48 1.2.2.2 ad #include <sys/param.h>
49 1.2.2.2 ad #include <sys/buf.h>
50 1.2.2.2 ad #include <sys/boot_flag.h>
51 1.2.2.2 ad #include <sys/extent.h>
52 1.2.2.2 ad #include <sys/kernel.h>
53 1.2.2.2 ad #include <sys/ksyms.h>
54 1.2.2.2 ad #include <uvm/uvm_extern.h>
55 1.2.2.2 ad
56 1.2.2.2 ad #include <dev/ofw/openfirm.h>
57 1.2.2.2 ad #include <machine/pmap.h>
58 1.2.2.2 ad #include <machine/powerpc.h>
59 1.2.2.2 ad #include <machine/trap.h>
60 1.2.2.2 ad #include <powerpc/bus.h>
61 1.2.2.2 ad #include <powerpc/oea/bat.h>
62 1.2.2.2 ad #include <powerpc/ofw_bus.h>
63 1.2.2.2 ad #include <powerpc/ofw_cons.h>
64 1.2.2.2 ad #include <powerpc/spr.h>
65 1.2.2.2 ad #include <arch/powerpc/pic/picvar.h>
66 1.2.2.2 ad
67 1.2.2.2 ad #include "opt_oea.h"
68 1.2.2.2 ad
69 1.2.2.2 ad #include "ksyms.h"
70 1.2.2.2 ad
71 1.2.2.2 ad #ifdef DDB
72 1.2.2.2 ad #include <machine/db_machdep.h>
73 1.2.2.2 ad #include <ddb/db_extern.h>
74 1.2.2.2 ad #endif
75 1.2.2.2 ad
76 1.2.2.2 ad #ifdef KGDB
77 1.2.2.2 ad #include <sys/kgdb.h>
78 1.2.2.2 ad #endif
79 1.2.2.2 ad
80 1.2.2.2 ad #ifdef IPKDB
81 1.2.2.2 ad #include <ipkdb/ipkdb.h>
82 1.2.2.2 ad #endif
83 1.2.2.2 ad
84 1.2.2.2 ad #include "opt_ofwoea.h"
85 1.2.2.2 ad
86 1.2.2.2 ad #ifdef OFWOEA_DEBUG
87 1.2.2.2 ad #define DPRINTF printf
88 1.2.2.2 ad #else
89 1.2.2.2 ad #define DPRINTF while(0) printf
90 1.2.2.2 ad #endif
91 1.2.2.2 ad
92 1.2.2.2 ad typedef struct _rangemap {
93 1.2.2.2 ad u_int32_t addr;
94 1.2.2.2 ad u_int32_t size;
95 1.2.2.2 ad int type;
96 1.2.2.2 ad } rangemap_t;
97 1.2.2.2 ad
98 1.2.2.2 ad struct ofw_translations {
99 1.2.2.2 ad vaddr_t va;
100 1.2.2.2 ad int len;
101 1.2.2.2 ad #if defined (PMAC_G5)
102 1.2.2.2 ad register64_t pa;
103 1.2.2.2 ad #else
104 1.2.2.2 ad register_t pa;
105 1.2.2.2 ad #endif
106 1.2.2.2 ad int mode;
107 1.2.2.2 ad }__attribute__((packed));
108 1.2.2.2 ad
109 1.2.2.2 ad struct pmap ofw_pmap;
110 1.2.2.2 ad struct ofw_translations ofmap[32];
111 1.2.2.2 ad char bootpath[256];
112 1.2.2.2 ad #if NKSYMS || defined(DDB) || defined(LKM)
113 1.2.2.2 ad void *startsym, *endsym;
114 1.2.2.2 ad #endif
115 1.2.2.2 ad #ifdef TIMEBASE_FREQ
116 1.2.2.2 ad u_int timebase_freq = TIMEBASE_FREQ;
117 1.2.2.2 ad #else
118 1.2.2.2 ad u_int timebase_freq = 0;
119 1.2.2.2 ad #endif
120 1.2.2.2 ad
121 1.2.2.2 ad extern int ofmsr;
122 1.2.2.2 ad extern int chosen;
123 1.2.2.2 ad extern uint32_t ticks_per_sec;
124 1.2.2.2 ad extern uint32_t ns_per_tick;
125 1.2.2.2 ad extern uint32_t ticks_per_intr;
126 1.2.2.2 ad
127 1.2.2.2 ad static int save_ofmap(struct ofw_translations *, int);
128 1.2.2.2 ad static void restore_ofmap(struct ofw_translations *, int);
129 1.2.2.2 ad static void set_timebase(void);
130 1.2.2.2 ad
131 1.2.2.2 ad void
132 1.2.2.2 ad ofwoea_initppc(u_int startkernel, u_int endkernel, char *args)
133 1.2.2.2 ad {
134 1.2.2.2 ad int ofmaplen;
135 1.2.2.2 ad #if defined (PPC_OEA64_BRIDGE)
136 1.2.2.2 ad register_t scratch;
137 1.2.2.2 ad #endif
138 1.2.2.2 ad
139 1.2.2.2 ad #if defined (PPC_OEA)
140 1.2.2.2 ad /* initialze bats */
141 1.2.2.2 ad ofwoea_batinit();
142 1.2.2.2 ad #elif defined (PPC_OEA64) || defined (PPC_OEA64_BRIDGE)
143 1.2.2.2 ad #endif /* PPC_OEA */
144 1.2.2.2 ad
145 1.2.2.2 ad #if NKSYMS || defined(DDB) || defined(LKM)
146 1.2.2.2 ad /* get info of kernel symbol table from bootloader */
147 1.2.2.2 ad memcpy(&startsym, args + strlen(args) + 1, sizeof(startsym));
148 1.2.2.2 ad memcpy(&endsym, args + strlen(args) + 1 + sizeof(startsym),
149 1.2.2.2 ad sizeof(endsym));
150 1.2.2.2 ad if (startsym == NULL || endsym == NULL)
151 1.2.2.2 ad startsym = endsym = NULL;
152 1.2.2.2 ad #endif
153 1.2.2.2 ad
154 1.2.2.2 ad ofwoea_bus_space_init();
155 1.2.2.2 ad
156 1.2.2.2 ad ofwoea_consinit();
157 1.2.2.2 ad
158 1.2.2.2 ad oea_init(pic_ext_intr);
159 1.2.2.2 ad
160 1.2.2.2 ad ofmaplen = save_ofmap(NULL, 0);
161 1.2.2.2 ad if (ofmaplen > 0)
162 1.2.2.2 ad save_ofmap(ofmap, ofmaplen);
163 1.2.2.2 ad
164 1.2.2.2 ad ofmsr &= ~PSL_IP;
165 1.2.2.2 ad
166 1.2.2.2 ad /* Parse the args string */
167 1.2.2.2 ad if (args) {
168 1.2.2.2 ad strcpy(bootpath, args);
169 1.2.2.2 ad args = bootpath;
170 1.2.2.2 ad while (*++args && *args != ' ');
171 1.2.2.2 ad if (*args) {
172 1.2.2.2 ad *args++ = 0;
173 1.2.2.2 ad while (*args)
174 1.2.2.2 ad BOOT_FLAG(*args++, boothowto);
175 1.2.2.2 ad }
176 1.2.2.2 ad }
177 1.2.2.2 ad
178 1.2.2.2 ad uvm_setpagesize();
179 1.2.2.2 ad pmap_bootstrap(startkernel, endkernel);
180 1.2.2.2 ad
181 1.2.2.2 ad #if defined(PPC_OEA64) || defined (PPC_OEA64_BRIDGE)
182 1.2.2.2 ad #if defined (PMAC_G5)
183 1.2.2.2 ad /* Mapin 1st 256MB segment 1:1, also map in mem needed to access OFW*/
184 1.2.2.2 ad pmap_setup_segment0_map(0, 0xff800000, 0x3fc00000, 0x400000, 0x0);
185 1.2.2.2 ad #elif defined (MAMBO)
186 1.2.2.2 ad /* Mapin 1st 256MB segment 1:1, also map in mem needed to access OFW*/
187 1.2.2.2 ad pmap_setup_segment0_map(0, 0xf4000000, 0xf4000000, 0x1000, 0x0);
188 1.2.2.2 ad #endif /* PMAC_G5 */
189 1.2.2.2 ad
190 1.2.2.2 ad /* Now enable translation (and machine checks/recoverable interrupts) */
191 1.2.2.2 ad __asm __volatile ("sync; mfmsr %0; ori %0,%0,%1; mtmsr %0; isync"
192 1.2.2.2 ad : "=r"(scratch)
193 1.2.2.2 ad : "K"(PSL_IR|PSL_DR|PSL_ME|PSL_RI));
194 1.2.2.2 ad #endif /* PPC_OEA64 || PPC_OEA64_BRIDGE */
195 1.2.2.2 ad
196 1.2.2.2 ad restore_ofmap(ofmap, ofmaplen);
197 1.2.2.2 ad
198 1.2.2.2 ad #if NKSYMS || defined(DDB) || defined(LKM)
199 1.2.2.2 ad ksyms_init((int)((u_int)endsym - (u_int)startsym), startsym, endsym);
200 1.2.2.2 ad #endif
201 1.2.2.2 ad
202 1.2.2.2 ad /* CPU clock stuff */
203 1.2.2.2 ad set_timebase();
204 1.2.2.2 ad }
205 1.2.2.2 ad
206 1.2.2.2 ad void
207 1.2.2.2 ad set_timebase(void)
208 1.2.2.2 ad {
209 1.2.2.2 ad int qhandle, phandle, msr, scratch;
210 1.2.2.2 ad char type[32];
211 1.2.2.2 ad
212 1.2.2.2 ad if (timebase_freq != 0) {
213 1.2.2.2 ad ticks_per_sec = timebase_freq;
214 1.2.2.2 ad goto found;
215 1.2.2.2 ad }
216 1.2.2.2 ad
217 1.2.2.2 ad for (qhandle = OF_peer(0); qhandle; qhandle = phandle) {
218 1.2.2.2 ad if (OF_getprop(qhandle, "device_type", type, sizeof type) > 0
219 1.2.2.2 ad && strcmp(type, "cpu") == 0
220 1.2.2.2 ad && OF_getprop(qhandle, "timebase-frequency",
221 1.2.2.2 ad &ticks_per_sec, sizeof ticks_per_sec) > 0) {
222 1.2.2.2 ad goto found;
223 1.2.2.2 ad }
224 1.2.2.2 ad if ((phandle = OF_child(qhandle)))
225 1.2.2.2 ad continue;
226 1.2.2.2 ad while (qhandle) {
227 1.2.2.2 ad if ((phandle = OF_peer(qhandle)))
228 1.2.2.2 ad break;
229 1.2.2.2 ad qhandle = OF_parent(qhandle);
230 1.2.2.2 ad }
231 1.2.2.2 ad }
232 1.2.2.2 ad panic("no cpu node");
233 1.2.2.2 ad
234 1.2.2.2 ad found:
235 1.2.2.2 ad __asm volatile ("mfmsr %0; andi. %1,%0,%2; mtmsr %1"
236 1.2.2.2 ad : "=r"(msr), "=r"(scratch) : "K"((u_short)~PSL_EE));
237 1.2.2.2 ad ns_per_tick = 1000000000 / ticks_per_sec;
238 1.2.2.2 ad ticks_per_intr = ticks_per_sec / hz;
239 1.2.2.2 ad cpu_timebase = ticks_per_sec;
240 1.2.2.2 ad curcpu()->ci_lasttb = mftbl();
241 1.2.2.2 ad mtspr(SPR_DEC, ticks_per_intr);
242 1.2.2.2 ad mtmsr(msr);
243 1.2.2.2 ad }
244 1.2.2.2 ad
245 1.2.2.2 ad static int
246 1.2.2.2 ad save_ofmap(struct ofw_translations *map, int maxlen)
247 1.2.2.2 ad {
248 1.2.2.2 ad int mmui, mmu, len;
249 1.2.2.2 ad
250 1.2.2.2 ad OF_getprop(chosen, "mmu", &mmui, sizeof mmui);
251 1.2.2.2 ad mmu = OF_instance_to_package(mmui);
252 1.2.2.2 ad
253 1.2.2.2 ad if (map) {
254 1.2.2.2 ad memset(map, 0, maxlen); /* to be safe */
255 1.2.2.2 ad len = OF_getprop(mmu, "translations", map, maxlen);
256 1.2.2.2 ad } else
257 1.2.2.2 ad len = OF_getproplen(mmu, "translations");
258 1.2.2.2 ad
259 1.2.2.2 ad if (len < 0)
260 1.2.2.2 ad len = 0;
261 1.2.2.2 ad return len;
262 1.2.2.2 ad }
263 1.2.2.2 ad
264 1.2.2.2 ad void
265 1.2.2.2 ad restore_ofmap(struct ofw_translations *map, int len)
266 1.2.2.2 ad {
267 1.2.2.2 ad int n = len / sizeof(struct ofw_translations);
268 1.2.2.2 ad int i;
269 1.2.2.2 ad
270 1.2.2.2 ad pmap_pinit(&ofw_pmap);
271 1.2.2.2 ad
272 1.2.2.2 ad #if defined(PPC_OEA64_BRIDGE)
273 1.2.2.2 ad ofw_pmap.pm_sr[0x0] = KERNELN_SEGMENT(0);
274 1.2.2.2 ad #endif
275 1.2.2.2 ad ofw_pmap.pm_sr[KERNEL_SR] = KERNEL_SEGMENT;
276 1.2.2.2 ad
277 1.2.2.2 ad #ifdef KERNEL2_SR
278 1.2.2.2 ad ofw_pmap.pm_sr[KERNEL2_SR] = KERNEL2_SEGMENT;
279 1.2.2.2 ad #endif
280 1.2.2.2 ad
281 1.2.2.2 ad for (i = 0; i < n; i++) {
282 1.2.2.2 ad #if defined (PMAC_G5)
283 1.2.2.2 ad register64_t pa = map[i].pa;
284 1.2.2.2 ad #else
285 1.2.2.2 ad register_t pa = map[i].pa;
286 1.2.2.2 ad #endif
287 1.2.2.2 ad vaddr_t va = map[i].va;
288 1.2.2.2 ad size_t length = map[i].len;
289 1.2.2.2 ad
290 1.2.2.2 ad if (va < 0xf0000000) /* XXX */
291 1.2.2.2 ad continue;
292 1.2.2.2 ad
293 1.2.2.2 ad while (length > 0) {
294 1.2.2.2 ad pmap_enter(&ofw_pmap, va, (paddr_t)pa, VM_PROT_ALL,
295 1.2.2.2 ad VM_PROT_ALL|PMAP_WIRED);
296 1.2.2.2 ad pa += PAGE_SIZE;
297 1.2.2.2 ad va += PAGE_SIZE;
298 1.2.2.2 ad length -= PAGE_SIZE;
299 1.2.2.2 ad }
300 1.2.2.2 ad }
301 1.2.2.2 ad pmap_update(&ofw_pmap);
302 1.2.2.2 ad }
303 1.2.2.2 ad
304 1.2.2.2 ad
305 1.2.2.2 ad
306 1.2.2.2 ad /*
307 1.2.2.2 ad * Scan the device tree for ranges, and batmap them.
308 1.2.2.2 ad */
309 1.2.2.2 ad
310 1.2.2.2 ad static u_int16_t
311 1.2.2.2 ad ranges_bitmap(int node, u_int16_t bitmap)
312 1.2.2.2 ad {
313 1.2.2.2 ad int child, mlen, len, acells, scells, reclen, i, addr, j;
314 1.2.2.2 ad u_int32_t map[160];
315 1.2.2.2 ad
316 1.2.2.2 ad for (child = OF_child(node); child; child = OF_peer(child)) {
317 1.2.2.2 ad mlen = OF_getprop(child, "ranges", map, sizeof(map));
318 1.2.2.2 ad if (mlen == -1)
319 1.2.2.2 ad goto noranges;
320 1.2.2.2 ad len = OF_getprop(child, "#address-cells", &acells,
321 1.2.2.2 ad sizeof(acells));
322 1.2.2.2 ad if (len == -1)
323 1.2.2.2 ad goto noranges;
324 1.2.2.2 ad len = OF_getprop(child, "#size-cells", &scells,
325 1.2.2.2 ad sizeof(scells));
326 1.2.2.2 ad if (len == -1)
327 1.2.2.2 ad goto noranges;
328 1.2.2.2 ad
329 1.2.2.2 ad reclen = acells + 1 + scells;
330 1.2.2.2 ad
331 1.2.2.2 ad for (i=0; i < reclen/(mlen/4); i++) {
332 1.2.2.2 ad addr = map[reclen * i + acells + 1];
333 1.2.2.2 ad len = map[reclen * i + reclen - 1];
334 1.2.2.2 ad for (j = 0; j < len / 0x10000000; j++)
335 1.2.2.2 ad bitmap |= 1 << ((addr+j*0x10000000) >>28);
336 1.2.2.2 ad bitmap |= 1 << (addr >> 28);
337 1.2.2.2 ad }
338 1.2.2.2 ad
339 1.2.2.2 ad noranges:
340 1.2.2.2 ad bitmap |= ranges_bitmap(child, bitmap);
341 1.2.2.2 ad continue;
342 1.2.2.2 ad }
343 1.2.2.2 ad return bitmap;
344 1.2.2.2 ad }
345 1.2.2.2 ad
346 1.2.2.2 ad void
347 1.2.2.2 ad ofwoea_batinit(void)
348 1.2.2.2 ad {
349 1.2.2.2 ad #if 0
350 1.2.2.2 ad /* this is what macppc used to do */
351 1.2.2.2 ad oea_batinit(0x80000000, BAT_BL_256M, 0xf0000000, BAT_BL_256M,
352 1.2.2.2 ad 0x90000000, BAT_BL_256M, 0xa0000000, BAT_BL_256M,
353 1.2.2.2 ad 0xb0000000, BAT_BL_256M, 0);
354 1.2.2.2 ad #else
355 1.2.2.2 ad u_int16_t bitmap;
356 1.2.2.2 ad int node, i;
357 1.2.2.2 ad
358 1.2.2.2 ad node = OF_finddevice("/");
359 1.2.2.2 ad bitmap = ranges_bitmap(node, 0);
360 1.2.2.2 ad oea_batinit(0);
361 1.2.2.2 ad
362 1.2.2.2 ad /* XXX this is a macppc-specific hack */
363 1.2.2.2 ad bitmap = 0x8f00;
364 1.2.2.2 ad for (i=1; i < 0x10; i++)
365 1.2.2.2 ad if (bitmap & (1 << i))
366 1.2.2.2 ad oea_iobat_add(0x10000000 * i, BAT_BL_256M);
367 1.2.2.2 ad #endif
368 1.2.2.2 ad }
369 1.2.2.2 ad
370 1.2.2.2 ad
371 1.2.2.2 ad /* we define these partially, as we will fill the rest in later */
372 1.2.2.2 ad struct powerpc_bus_space genppc_isa_io_space_tag = {
373 1.2.2.2 ad .pbs_flags = _BUS_SPACE_LITTLE_ENDIAN|_BUS_SPACE_IO_TYPE,
374 1.2.2.2 ad .pbs_base = 0x00000000,
375 1.2.2.2 ad };
376 1.2.2.2 ad
377 1.2.2.2 ad struct powerpc_bus_space genppc_isa_mem_space_tag = {
378 1.2.2.2 ad .pbs_flags = _BUS_SPACE_LITTLE_ENDIAN|_BUS_SPACE_MEM_TYPE,
379 1.2.2.2 ad .pbs_base = 0x00000000,
380 1.2.2.2 ad };
381 1.2.2.2 ad
382 1.2.2.2 ad /* This gives us a maximum of 6 PCI busses, assuming both io/mem on each.
383 1.2.2.2 ad * Increase if necc.
384 1.2.2.2 ad */
385 1.2.2.2 ad static char ex_storage[EXSTORAGE_MAX][EXTENT_FIXED_STORAGE_SIZE(EXTMAP_RANGES)]
386 1.2.2.2 ad __attribute__((aligned(8)));
387 1.2.2.2 ad
388 1.2.2.2 ad
389 1.2.2.2 ad static void
390 1.2.2.2 ad find_ranges(int base, rangemap_t *regions, int *cur, int type)
391 1.2.2.2 ad {
392 1.2.2.2 ad int node, i, len, reclen;
393 1.2.2.2 ad u_int32_t acells, scells, map[160];
394 1.2.2.2 ad char tmp[32];
395 1.2.2.2 ad
396 1.2.2.2 ad node = base;
397 1.2.2.2 ad if (OF_getprop(node, "device_type", tmp, sizeof(tmp)) == -1)
398 1.2.2.2 ad goto rec;
399 1.2.2.2 ad if ((type == RANGE_TYPE_PCI || type == RANGE_TYPE_FIRSTPCI) &&
400 1.2.2.2 ad strcmp("pci", tmp) != 0)
401 1.2.2.2 ad goto rec;
402 1.2.2.2 ad if (type == RANGE_TYPE_ISA && strcmp("isa", tmp) != 0)
403 1.2.2.2 ad goto rec;
404 1.2.2.2 ad len = OF_getprop(node, "ranges", map, sizeof(map));
405 1.2.2.2 ad if (len == -1)
406 1.2.2.2 ad goto rec;
407 1.2.2.2 ad if (OF_getprop(node, "#address-cells", &acells,
408 1.2.2.2 ad sizeof(acells)) != sizeof(acells))
409 1.2.2.2 ad acells = 1;
410 1.2.2.2 ad if (OF_getprop(node, "#size-cells", &scells,
411 1.2.2.2 ad sizeof(scells)) != sizeof(scells))
412 1.2.2.2 ad scells = 1;
413 1.2.2.2 ad if (type == RANGE_TYPE_ISA)
414 1.2.2.2 ad reclen = 6;
415 1.2.2.2 ad else
416 1.2.2.2 ad reclen = acells + scells + 1;
417 1.2.2.2 ad DPRINTF("found a map reclen=%d cur=%d\n", reclen, *cur);
418 1.2.2.2 ad switch (type) {
419 1.2.2.2 ad case RANGE_TYPE_PCI:
420 1.2.2.2 ad case RANGE_TYPE_FIRSTPCI:
421 1.2.2.2 ad for (i=0; i < len/(4*reclen); i++) {
422 1.2.2.2 ad regions[*cur].type = map[i*reclen] >> 24;
423 1.2.2.2 ad regions[*cur].addr = map[i*reclen + acells];
424 1.2.2.2 ad regions[*cur].size =
425 1.2.2.2 ad map[i*reclen + acells + scells];
426 1.2.2.2 ad (*cur)++;
427 1.2.2.2 ad }
428 1.2.2.2 ad break;
429 1.2.2.2 ad case RANGE_TYPE_ISA:
430 1.2.2.2 ad for (i=0; i < len/(4*reclen); i++) {
431 1.2.2.2 ad if (map[i*reclen] == 1)
432 1.2.2.2 ad regions[*cur].type = RANGE_IO;
433 1.2.2.2 ad else
434 1.2.2.2 ad regions[*cur].type = RANGE_MEM;
435 1.2.2.2 ad regions[*cur].size =
436 1.2.2.2 ad map[i*reclen + acells + scells];
437 1.2.2.2 ad (*cur)++;
438 1.2.2.2 ad }
439 1.2.2.2 ad break;
440 1.2.2.2 ad }
441 1.2.2.2 ad return;
442 1.2.2.2 ad rec:
443 1.2.2.2 ad for (node = OF_child(base); node; node = OF_peer(node))
444 1.2.2.2 ad find_ranges(node, regions, cur, type);
445 1.2.2.2 ad }
446 1.2.2.2 ad
447 1.2.2.2 ad static int
448 1.2.2.2 ad find_lowest_range(rangemap_t *ranges, int nrof, int type)
449 1.2.2.2 ad {
450 1.2.2.2 ad int i, low = 0;
451 1.2.2.2 ad u_int32_t addr = 0xffffffff;
452 1.2.2.2 ad
453 1.2.2.2 ad for (i=0; i < nrof; i++) {
454 1.2.2.2 ad if (ranges[i].type == type && ranges[i].addr != 0 &&
455 1.2.2.2 ad ranges[i].addr < addr) {
456 1.2.2.2 ad low = i;
457 1.2.2.2 ad addr = ranges[i].addr;
458 1.2.2.2 ad }
459 1.2.2.2 ad }
460 1.2.2.2 ad if (addr == 0xffffffff)
461 1.2.2.2 ad return -1;
462 1.2.2.2 ad return low;
463 1.2.2.2 ad }
464 1.2.2.2 ad
465 1.2.2.2 ad /*
466 1.2.2.2 ad * Find a region of memory, and create a bus_space_tag for it.
467 1.2.2.2 ad * Notes:
468 1.2.2.2 ad * For ISA node is ignored.
469 1.2.2.2 ad * node is the starting node. if -1, we start at / and map everything.
470 1.2.2.2 ad */
471 1.2.2.2 ad
472 1.2.2.2 ad int
473 1.2.2.2 ad ofwoea_map_space(int rangetype, int iomem, int node,
474 1.2.2.2 ad struct powerpc_bus_space *tag, const char *name)
475 1.2.2.2 ad {
476 1.2.2.2 ad int i, cur, range, nrofholes, error;
477 1.2.2.2 ad static int exmap=0;
478 1.2.2.2 ad u_int32_t addr;
479 1.2.2.2 ad rangemap_t region, holes[32], list[32];
480 1.2.2.2 ad
481 1.2.2.2 ad memset(list, 0, sizeof(list));
482 1.2.2.2 ad cur = 0;
483 1.2.2.2 ad if (rangetype == RANGE_TYPE_ISA || node == -1)
484 1.2.2.2 ad node = OF_finddevice("/");
485 1.2.2.2 ad if (rangetype == RANGE_TYPE_ISA) {
486 1.2.2.2 ad u_int32_t size = 0;
487 1.2.2.2 ad rangemap_t regions[32];
488 1.2.2.2 ad
489 1.2.2.2 ad find_ranges(node, list, &cur, RANGE_TYPE_FIRSTPCI);
490 1.2.2.2 ad range = 0;
491 1.2.2.2 ad find_ranges(node, regions, &range, RANGE_TYPE_ISA);
492 1.2.2.2 ad if (range == 0 || cur == 0)
493 1.2.2.2 ad return -1; /* no isa stuff found */
494 1.2.2.2 ad for (i=0; i < range; i++)
495 1.2.2.2 ad if (regions[i].type == iomem)
496 1.2.2.2 ad size = regions[i].size;
497 1.2.2.2 ad if (iomem == RANGE_IO) {
498 1.2.2.2 ad /* the first io range is the one */
499 1.2.2.2 ad for (i=0; i < cur; i++)
500 1.2.2.2 ad if (list[i].type == RANGE_IO && size) {
501 1.2.2.2 ad tag->pbs_offset = list[i].addr;
502 1.2.2.2 ad tag->pbs_limit = size;
503 1.2.2.2 ad error = bus_space_init(tag, name, NULL, 0);
504 1.2.2.2 ad return error;
505 1.2.2.2 ad }
506 1.2.2.2 ad } else {
507 1.2.2.2 ad for (i=0; i < cur; i++)
508 1.2.2.2 ad if (list[i].type == RANGE_MEM &&
509 1.2.2.2 ad list[i].size == size) {
510 1.2.2.2 ad tag->pbs_offset = list[i].addr;
511 1.2.2.2 ad tag->pbs_limit = size;
512 1.2.2.2 ad error = bus_space_init(tag, name, NULL, 0);
513 1.2.2.2 ad return error;
514 1.2.2.2 ad }
515 1.2.2.2 ad }
516 1.2.2.2 ad return -1; /* NO ISA FOUND */
517 1.2.2.2 ad }
518 1.2.2.2 ad find_ranges(node, list, &cur, rangetype);
519 1.2.2.2 ad
520 1.2.2.2 ad DPRINTF("cur == %d\n", cur);
521 1.2.2.2 ad /* now list should contain a list of memory regions */
522 1.2.2.2 ad for (i=0; i < cur; i++)
523 1.2.2.2 ad DPRINTF("addr=0x%x size=0x%x type=%d\n", list[i].addr,
524 1.2.2.2 ad list[i].size, list[i].type);
525 1.2.2.2 ad
526 1.2.2.2 ad addr=0;
527 1.2.2.2 ad range = find_lowest_range(list, cur, iomem);
528 1.2.2.2 ad i = 0;
529 1.2.2.2 ad nrofholes = 0;
530 1.2.2.2 ad while (range != -1) {
531 1.2.2.2 ad DPRINTF("range==%d\n", range);
532 1.2.2.2 ad DPRINTF("i==%d\n", i);
533 1.2.2.2 ad if (i == 0) {
534 1.2.2.2 ad memcpy(®ion, &list[range], sizeof(rangemap_t));
535 1.2.2.2 ad list[range].addr = 0;
536 1.2.2.2 ad i++;
537 1.2.2.2 ad range = find_lowest_range(list, cur, iomem);
538 1.2.2.2 ad continue;
539 1.2.2.2 ad }
540 1.2.2.2 ad if (region.addr + region.size < list[range].addr) {
541 1.2.2.2 ad /* allocate a hole */
542 1.2.2.2 ad holes[nrofholes].addr = region.size + region.addr;
543 1.2.2.2 ad holes[nrofholes].size = list[range].addr -
544 1.2.2.2 ad holes[nrofholes].addr - 1;
545 1.2.2.2 ad nrofholes++;
546 1.2.2.2 ad }
547 1.2.2.2 ad region.size = list[range].size + list[range].addr -
548 1.2.2.2 ad region.addr;
549 1.2.2.2 ad list[range].addr = 0;
550 1.2.2.2 ad range = find_lowest_range(list, cur, iomem);
551 1.2.2.2 ad }
552 1.2.2.2 ad DPRINTF("RANGE iomem=%d FOUND\n", iomem);
553 1.2.2.2 ad DPRINTF("addr=0x%x size=0x%x type=%d\n", region.addr,
554 1.2.2.2 ad region.size, region.type);
555 1.2.2.2 ad DPRINTF("HOLES FOUND\n");
556 1.2.2.2 ad for (i=0; i < nrofholes; i++)
557 1.2.2.2 ad DPRINTF("addr=0x%x size=0x%x type=%d\n", holes[i].addr,
558 1.2.2.2 ad holes[i].size, holes[i].type);
559 1.2.2.2 ad /* AT THIS POINT WE MAP IT */
560 1.2.2.2 ad
561 1.2.2.2 ad if (rangetype == RANGE_TYPE_PCI) {
562 1.2.2.2 ad if (exmap == EXSTORAGE_MAX)
563 1.2.2.2 ad panic("Not enough ex_storage space. "
564 1.2.2.2 ad "Increase EXSTORAGE_MAX");
565 1.2.2.2 ad
566 1.2.2.2 ad /* XXX doing this in here might be wrong */
567 1.2.2.2 ad if (iomem == 1) {
568 1.2.2.2 ad /* we map an IO region */
569 1.2.2.2 ad tag->pbs_offset = region.addr;
570 1.2.2.2 ad tag->pbs_base = 0;
571 1.2.2.2 ad tag->pbs_limit = region.size;
572 1.2.2.2 ad } else {
573 1.2.2.2 ad /* ... or a memory region */
574 1.2.2.2 ad tag->pbs_offset = 0;
575 1.2.2.2 ad tag->pbs_base = region.addr;
576 1.2.2.2 ad tag->pbs_limit = region.size + region.addr;
577 1.2.2.2 ad }
578 1.2.2.2 ad
579 1.2.2.2 ad error = bus_space_init(tag, name, ex_storage[exmap],
580 1.2.2.2 ad sizeof(ex_storage[exmap]));
581 1.2.2.2 ad exmap++;
582 1.2.2.2 ad if (error)
583 1.2.2.2 ad panic("ofwoea_bus_space_init: can't init tag %s", name);
584 1.2.2.2 ad for (i=0; i < nrofholes; i++) {
585 1.2.2.2 ad error =
586 1.2.2.2 ad extent_alloc_region(tag->pbs_extent,
587 1.2.2.2 ad holes[i].addr, holes[i].size, EX_NOWAIT);
588 1.2.2.2 ad if (error)
589 1.2.2.2 ad panic("ofwoea_bus_space_init: can't block out"
590 1.2.2.2 ad " reserved space 0x%x-0x%x: error=%d",
591 1.2.2.2 ad holes[i].addr, holes[i].addr+holes[i].size,
592 1.2.2.2 ad error);
593 1.2.2.2 ad }
594 1.2.2.2 ad return error;
595 1.2.2.2 ad }
596 1.2.2.2 ad return -1;
597 1.2.2.2 ad }
598 1.2.2.2 ad
599 1.2.2.2 ad void
600 1.2.2.2 ad ofwoea_bus_space_init(void)
601 1.2.2.2 ad {
602 1.2.2.2 ad int error;
603 1.2.2.2 ad
604 1.2.2.2 ad error = ofwoea_map_space(RANGE_TYPE_ISA, RANGE_IO, -1,
605 1.2.2.2 ad &genppc_isa_io_space_tag, "isa-ioport");
606 1.2.2.2 ad if (error > 0)
607 1.2.2.2 ad panic("Could not map ISA IO");
608 1.2.2.2 ad error = ofwoea_map_space(RANGE_TYPE_ISA, RANGE_MEM, -1,
609 1.2.2.2 ad &genppc_isa_mem_space_tag, "isa-iomem");
610 1.2.2.2 ad if (error > 0)
611 1.2.2.2 ad panic("Could not map ISA MEM");
612 1.2.2.2 ad }
613