2 * Copyright (C) 1996 Wolfgang Solfrank.
3 * Copyright (C) 1996 TooLs GmbH.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by TooLs GmbH.
17 * 4. The name of TooLs GmbH may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 * $NetBSD: ofw_machdep.c,v 1.5 2000/05/23 13:25:43 tsubai Exp $
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
39 #include <sys/systm.h>
42 #include <sys/fcntl.h>
43 #include <sys/malloc.h>
46 #include <net/ethernet.h>
48 #include <dev/ofw/openfirm.h>
51 #include <vm/vm_param.h>
52 #include <vm/vm_page.h>
54 #include <machine/bus.h>
55 #include <machine/md_var.h>
56 #include <machine/powerpc.h>
57 #include <machine/ofw_machdep.h>
58 #include <powerpc/ofw/ofw_pci.h>
60 #define OFMEM_REGIONS 32
61 static struct mem_region OFmem[OFMEM_REGIONS + 1], OFavail[OFMEM_REGIONS + 3];
62 static struct mem_region OFfree[OFMEM_REGIONS + 3];
64 extern register_t ofmsr[5];
65 extern struct pcpu __pcpu[MAXCPU];
66 extern struct pmap ofw_pmap;
67 static int (*ofwcall)(void *);
70 * Saved SPRG0-3 from OpenFirmware. Will be restored prior to the callback.
72 register_t ofw_sprg0_save;
75 ofw_sprg_prepare(void)
78 * Assume that interrupt are disabled at this point, or
79 * SPRG1-3 could be trashed
81 __asm __volatile("mfsprg0 %0\n\t"
86 : "=&r"(ofw_sprg0_save)
94 ofw_sprg_restore(void)
97 * Note that SPRG1-3 contents are irrelevant. They are scratch
98 * registers used in the early portion of trap handling when
99 * interrupts are disabled.
101 * PCPU data cannot be used until this routine is called !
103 __asm __volatile("mtsprg0 %0" :: "r"(ofw_sprg0_save));
107 * Memory region utilities: determine if two regions overlap,
108 * and merge two overlapping regions into one
111 memr_overlap(struct mem_region *r1, struct mem_region *r2)
113 if ((r1->mr_start + r1->mr_size) < r2->mr_start ||
114 (r2->mr_start + r2->mr_size) < r1->mr_start)
121 memr_merge(struct mem_region *from, struct mem_region *to)
124 end = imax(to->mr_start + to->mr_size, from->mr_start + from->mr_size);
125 to->mr_start = imin(from->mr_start, to->mr_start);
126 to->mr_size = end - to->mr_start;
130 * This is called during powerpc_init, before the system is really initialized.
131 * It shall provide the total and the available regions of RAM.
132 * Both lists must have a zero-size entry as terminator.
133 * The available regions need not take the kernel into account, but needs
134 * to provide space for two additional entry beyond the terminating one.
137 mem_regions(struct mem_region **memp, int *memsz,
138 struct mem_region **availp, int *availsz)
148 if ((phandle = OF_finddevice("/memory")) == -1
149 || (msz = OF_getprop(phandle, "reg",
150 OFmem, sizeof OFmem[0] * OFMEM_REGIONS))
152 || (asz = OF_getprop(phandle, "available",
153 OFavail, sizeof OFavail[0] * OFMEM_REGIONS))
157 *memsz = msz / sizeof(struct mem_region);
160 * OFavail may have overlapping regions - collapse these
161 * and copy out remaining regions to OFfree
163 asz /= sizeof(struct mem_region);
165 still_merging = FALSE;
166 for (i = 0; i < asz; i++) {
167 if (OFavail[i].mr_size == 0)
169 for (j = i+1; j < asz; j++) {
170 if (OFavail[j].mr_size == 0)
172 if (memr_overlap(&OFavail[j], &OFavail[i])) {
173 memr_merge(&OFavail[j], &OFavail[i]);
175 OFavail[j].mr_size = 0;
176 still_merging = TRUE;
180 } while (still_merging == TRUE);
182 /* evict inactive ranges */
183 for (i = 0, fsz = 0; i < asz; i++) {
184 if (OFavail[i].mr_size != 0) {
185 OFfree[fsz] = OFavail[i];
195 set_openfirm_callback(int (*openfirm)(void *))
202 openfirmware(void *args)
209 __asm __volatile( "\t"
220 if (pmap_bootstrapped) {
222 * Swap the kernel's address space with Open Firmware's
224 for (i = 0; i < 16; i++) {
225 srsave[i] = mfsrin(i << ADDR_SR_SHFT);
226 mtsrin(i << ADDR_SR_SHFT, ofw_pmap.pm_sr[i]);
230 * Clear battable[] translations
232 __asm __volatile("mtdbatu 2, %0\n"
233 "mtdbatu 3, %0" : : "r" (0));
237 result = ofwcall(args);
239 if (pmap_bootstrapped) {
241 * Restore the kernel's addr space. The isync() doesn;t
242 * work outside the loop unless mtsrin() is open-coded
243 * in an asm statement :(
245 for (i = 0; i < 16; i++) {
246 mtsrin(i << ADDR_SR_SHFT, srsave[i]);
265 int retval; /* dummy, this may not be needed */
267 OF_interpret("shut-down", 1, &retval);
268 for (;;); /* just in case */
274 int retval; /* dummy, this may not be needed */
276 OF_interpret("reset-all", 1, &retval);
277 for (;;); /* just in case */
281 OF_getetheraddr(device_t dev, u_char *addr)
285 node = ofw_pci_find_node(dev);
286 OF_getprop(node, "local-mac-address", addr, ETHER_ADDR_LEN);
290 * Return the physical address and the bus space to use for a node
291 * referenced by its package handle and the index of the register bank
292 * to decode. Intended to be used by console drivers in early boot only.
293 * Works by mapping the address of the node's bank given in the address
294 * space of its parent upward in the device tree at each bridge along the
298 OF_decode_addr(phandle_t node, int bank, bus_space_tag_t *tag,
299 bus_space_handle_t *handle)
306 mem_valid(vm_offset_t addr, int len)
310 for (i = 0; i < OFMEM_REGIONS; i++)
311 if ((addr >= OFmem[i].mr_start)
312 && (addr + len < OFmem[i].mr_start + OFmem[i].mr_size))