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>
49 #include <dev/ofw/ofw_pci.h>
52 #include <vm/vm_param.h>
53 #include <vm/vm_page.h>
55 #include <machine/bus.h>
56 #include <machine/md_var.h>
57 #include <machine/powerpc.h>
58 #include <machine/ofw_machdep.h>
59 #include <powerpc/ofw/ofw_pci.h>
61 #define OFMEM_REGIONS 32
62 static struct mem_region OFmem[OFMEM_REGIONS + 1], OFavail[OFMEM_REGIONS + 3];
63 static struct mem_region OFfree[OFMEM_REGIONS + 3];
65 extern register_t ofmsr[5];
66 extern struct pcpu __pcpu[MAXCPU];
67 extern struct pmap ofw_pmap;
68 static int (*ofwcall)(void *);
71 * Saved SPRG0-3 from OpenFirmware. Will be restored prior to the callback.
73 register_t ofw_sprg0_save;
76 ofw_sprg_prepare(void)
79 * Assume that interrupt are disabled at this point, or
80 * SPRG1-3 could be trashed
82 __asm __volatile("mfsprg0 %0\n\t"
87 : "=&r"(ofw_sprg0_save)
95 ofw_sprg_restore(void)
98 * Note that SPRG1-3 contents are irrelevant. They are scratch
99 * registers used in the early portion of trap handling when
100 * interrupts are disabled.
102 * PCPU data cannot be used until this routine is called !
104 __asm __volatile("mtsprg0 %0" :: "r"(ofw_sprg0_save));
108 * Memory region utilities: determine if two regions overlap,
109 * and merge two overlapping regions into one
112 memr_overlap(struct mem_region *r1, struct mem_region *r2)
114 if ((r1->mr_start + r1->mr_size) < r2->mr_start ||
115 (r2->mr_start + r2->mr_size) < r1->mr_start)
122 memr_merge(struct mem_region *from, struct mem_region *to)
125 end = imax(to->mr_start + to->mr_size, from->mr_start + from->mr_size);
126 to->mr_start = imin(from->mr_start, to->mr_start);
127 to->mr_size = end - to->mr_start;
131 * This is called during powerpc_init, before the system is really initialized.
132 * It shall provide the total and the available regions of RAM.
133 * Both lists must have a zero-size entry as terminator.
134 * The available regions need not take the kernel into account, but needs
135 * to provide space for two additional entry beyond the terminating one.
138 mem_regions(struct mem_region **memp, int *memsz,
139 struct mem_region **availp, int *availsz)
149 if ((phandle = OF_finddevice("/memory")) == -1
150 || (msz = OF_getprop(phandle, "reg",
151 OFmem, sizeof OFmem[0] * OFMEM_REGIONS))
153 || (asz = OF_getprop(phandle, "available",
154 OFavail, sizeof OFavail[0] * OFMEM_REGIONS))
158 *memsz = msz / sizeof(struct mem_region);
161 * OFavail may have overlapping regions - collapse these
162 * and copy out remaining regions to OFfree
164 asz /= sizeof(struct mem_region);
166 still_merging = FALSE;
167 for (i = 0; i < asz; i++) {
168 if (OFavail[i].mr_size == 0)
170 for (j = i+1; j < asz; j++) {
171 if (OFavail[j].mr_size == 0)
173 if (memr_overlap(&OFavail[j], &OFavail[i])) {
174 memr_merge(&OFavail[j], &OFavail[i]);
176 OFavail[j].mr_size = 0;
177 still_merging = TRUE;
181 } while (still_merging == TRUE);
183 /* evict inactive ranges */
184 for (i = 0, fsz = 0; i < asz; i++) {
185 if (OFavail[i].mr_size != 0) {
186 OFfree[fsz] = OFavail[i];
196 set_openfirm_callback(int (*openfirm)(void *))
203 openfirmware(void *args)
210 __asm __volatile( "\t"
221 if (pmap_bootstrapped) {
223 * Swap the kernel's address space with Open Firmware's
225 for (i = 0; i < 16; i++) {
226 srsave[i] = mfsrin(i << ADDR_SR_SHFT);
227 mtsrin(i << ADDR_SR_SHFT, ofw_pmap.pm_sr[i]);
231 * Clear battable[] translations
233 __asm __volatile("mtdbatu 2, %0\n"
234 "mtdbatu 3, %0" : : "r" (0));
238 result = ofwcall(args);
240 if (pmap_bootstrapped) {
242 * Restore the kernel's addr space. The isync() doesn;t
243 * work outside the loop unless mtsrin() is open-coded
244 * in an asm statement :(
246 for (i = 0; i < 16; i++) {
247 mtsrin(i << ADDR_SR_SHFT, srsave[i]);
266 int retval; /* dummy, this may not be needed */
268 OF_interpret("shut-down", 1, &retval);
269 for (;;); /* just in case */
275 int retval; /* dummy, this may not be needed */
277 OF_interpret("reset-all", 1, &retval);
278 for (;;); /* just in case */
282 OF_getetheraddr(device_t dev, u_char *addr)
286 node = ofw_pci_find_node(dev);
287 OF_getprop(node, "local-mac-address", addr, ETHER_ADDR_LEN);
291 * Return a bus handle and bus tag that corresponds to the register
292 * numbered regno for the device referenced by the package handle
293 * dev. This function is intended to be used by console drivers in
294 * early boot only. It works by mapping the address of the device's
295 * register in the address space of its parent and recursively walk
296 * the device tree upward this way.
299 OF_get_addr_props(phandle_t node, uint32_t *addrp, uint32_t *sizep, int *pcip)
305 res = OF_getprop(node, "#address-cells", &addr, sizeof(addr));
308 res = OF_getprop(node, "#size-cells", &size, sizeof(size));
312 if (addr == 3 && size == 2) {
313 res = OF_getprop(node, "name", name, sizeof(name));
315 name[sizeof(name) - 1] = '\0';
316 pci = (strcmp(name, "pci") == 0) ? 1 : 0;
328 OF_decode_addr(phandle_t dev, int regno, bus_space_tag_t *tag,
329 bus_space_handle_t *handle)
332 bus_addr_t addr, raddr, baddr;
333 bus_size_t size, rsize;
334 uint32_t c, nbridge, naddr, nsize;
335 phandle_t bridge, parent;
339 /* Sanity checking. */
342 bridge = OF_parent(dev);
347 if (tag == NULL || handle == NULL)
350 /* Get the requested register. */
351 OF_get_addr_props(bridge, &naddr, &nsize, &pci);
352 res = OF_getprop(dev, (pci) ? "assigned-addresses" : "reg",
356 if (res % sizeof(cell[0]))
358 res /= sizeof(cell[0]);
359 regno *= naddr + nsize;
360 if (regno + naddr + nsize > res)
362 spc = (pci) ? cell[regno] & OFW_PCI_PHYS_HI_SPACEMASK : ~0;
364 for (c = 0; c < naddr; c++)
365 addr = ((uint64_t)addr << 32) | cell[regno++];
367 for (c = 0; c < nsize; c++)
368 size = ((uint64_t)size << 32) | cell[regno++];
371 * Map the address range in the bridge's decoding window as given
372 * by the "ranges" property. If a node doesn't have such property
373 * then no mapping is done.
375 parent = OF_parent(bridge);
376 while (parent != 0) {
377 OF_get_addr_props(parent, &nbridge, NULL, &pcib);
378 res = OF_getprop(bridge, "ranges", cell, sizeof(cell));
381 if (res % sizeof(cell[0]))
383 res /= sizeof(cell[0]);
385 while (regno < res) {
387 ? cell[regno] & OFW_PCI_PHYS_HI_SPACEMASK
390 regno += naddr + nbridge + nsize;
394 for (c = 0; c < naddr; c++)
395 raddr = ((uint64_t)raddr << 32) | cell[regno++];
397 ? cell[regno] & OFW_PCI_PHYS_HI_SPACEMASK
400 for (c = 0; c < nbridge; c++)
401 baddr = ((uint64_t)baddr << 32) | cell[regno++];
403 for (c = 0; c < nsize; c++)
404 rsize = ((uint64_t)rsize << 32) | cell[regno++];
405 if (addr < raddr || addr >= raddr + rsize)
407 addr = addr - raddr + baddr;
414 parent = OF_parent(bridge);
415 OF_get_addr_props(bridge, &naddr, &nsize, &pci);
418 /* Default to memory mapped I/O. */
419 *tag = PPC_BUS_SPACE_MEM;
420 if (spc == OFW_PCI_PHYS_HI_SPACE_IO)
421 *tag = PPC_BUS_SPACE_IO;
422 return (bus_space_map(*tag, addr, size, 0, handle));
426 mem_valid(vm_offset_t addr, int len)
430 for (i = 0; i < OFMEM_REGIONS; i++)
431 if ((addr >= OFmem[i].mr_start)
432 && (addr + len < OFmem[i].mr_start + OFmem[i].mr_size))