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>
50 #include <dev/ofw/ofw_bus.h>
53 #include <vm/vm_param.h>
54 #include <vm/vm_page.h>
56 #include <machine/bus.h>
57 #include <machine/md_var.h>
58 #include <machine/platform.h>
59 #include <machine/ofw_machdep.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];
66 vm_offset_t mr_start_hi;
67 vm_offset_t mr_start_lo;
71 extern register_t ofmsr[5];
72 extern struct pmap ofw_pmap;
73 static int (*ofwcall)(void *);
77 static int openfirmware(void *args);
80 * Saved SPRG0-3 from OpenFirmware. Will be restored prior to the callback.
82 register_t ofw_sprg0_save;
85 ofw_sprg_prepare(void)
88 * Assume that interrupt are disabled at this point, or
89 * SPRG1-3 could be trashed
91 __asm __volatile("mfsprg0 %0\n\t"
96 : "=&r"(ofw_sprg0_save)
104 ofw_sprg_restore(void)
107 * Note that SPRG1-3 contents are irrelevant. They are scratch
108 * registers used in the early portion of trap handling when
109 * interrupts are disabled.
111 * PCPU data cannot be used until this routine is called !
113 __asm __volatile("mtsprg0 %0" :: "r"(ofw_sprg0_save));
117 * Memory region utilities: determine if two regions overlap,
118 * and merge two overlapping regions into one
121 memr_overlap(struct mem_region *r1, struct mem_region *r2)
123 if ((r1->mr_start + r1->mr_size) < r2->mr_start ||
124 (r2->mr_start + r2->mr_size) < r1->mr_start)
131 memr_merge(struct mem_region *from, struct mem_region *to)
134 end = imax(to->mr_start + to->mr_size, from->mr_start + from->mr_size);
135 to->mr_start = imin(from->mr_start, to->mr_start);
136 to->mr_size = end - to->mr_start;
140 * This is called during powerpc_init, before the system is really initialized.
141 * It shall provide the total and the available regions of RAM.
142 * Both lists must have a zero-size entry as terminator.
143 * The available regions need not take the kernel into account, but needs
144 * to provide space for two additional entry beyond the terminating one.
147 ofw_mem_regions(struct mem_region **memp, int *memsz,
148 struct mem_region **availp, int *availsz)
154 cell_t address_cells;
159 * Get #address-cells from root node, defaulting to 1 if it cannot
162 phandle = OF_finddevice("/");
163 if (OF_getprop(phandle, "#address-cells", &address_cells,
164 sizeof(address_cells)) < sizeof(address_cells))
170 if ((phandle = OF_finddevice("/memory")) == -1
171 || (asz = OF_getprop(phandle, "available",
172 OFavail, sizeof OFavail[0] * OFMEM_REGIONS)) <= 0)
176 printf("Physical memory unknown -- guessing 128 MB\n");
178 /* Leave the first 0xA000000 bytes for the kernel */
179 OFavail[0].mr_start = 0xA00000;
180 OFavail[0].mr_size = 0x75FFFFF;
181 asz = sizeof(OFavail[0]);
187 if (address_cells == 2) {
188 struct mem_region64 OFmem64[OFMEM_REGIONS + 1];
189 if ((phandle == -1) || (msz = OF_getprop(phandle, "reg",
190 OFmem64, sizeof OFmem64[0] * OFMEM_REGIONS)) <= 0) {
193 OFmem64[0].mr_start_hi = 0;
194 OFmem64[0].mr_start_lo = 0x0;
195 OFmem64[0].mr_size = 0x7FFFFFF;
196 msz = sizeof(OFmem64[0]);
198 panic("Physical memory map not found");
202 for (i = 0, j = 0; i < msz/sizeof(OFmem64[0]); i++) {
203 if (OFmem64[i].mr_start_hi == 0) {
204 OFmem[i].mr_start = OFmem64[i].mr_start_lo;
205 OFmem[i].mr_size = OFmem64[i].mr_size;
208 * Check for memory regions extending above 32-bit
209 * memory space, and restrict them to stay there.
211 if (((uint64_t)OFmem[i].mr_start +
212 (uint64_t)OFmem[i].mr_size) >
213 BUS_SPACE_MAXADDR_32BIT) {
214 OFmem[i].mr_size = BUS_SPACE_MAXADDR_32BIT -
220 msz = j*sizeof(OFmem[0]);
222 if ((msz = OF_getprop(phandle, "reg",
223 OFmem, sizeof OFmem[0] * OFMEM_REGIONS)) <= 0)
224 panic("Physical memory map not found");
228 *memsz = msz / sizeof(struct mem_region);
232 * OFavail may have overlapping regions - collapse these
233 * and copy out remaining regions to OFfree
235 asz /= sizeof(struct mem_region);
237 still_merging = FALSE;
238 for (i = 0; i < asz; i++) {
239 if (OFavail[i].mr_size == 0)
241 for (j = i+1; j < asz; j++) {
242 if (OFavail[j].mr_size == 0)
244 if (memr_overlap(&OFavail[j], &OFavail[i])) {
245 memr_merge(&OFavail[j], &OFavail[i]);
247 OFavail[j].mr_size = 0;
248 still_merging = TRUE;
252 } while (still_merging == TRUE);
254 /* evict inactive ranges */
255 for (i = 0, fsz = 0; i < asz; i++) {
256 if (OFavail[i].mr_size != 0) {
257 OFfree[fsz] = OFavail[i];
267 OF_initial_setup(void *fdt_ptr, void *junk, int (*openfirm)(void *))
269 if (ofmsr[0] & PSL_DR)
281 boolean_t status = FALSE;
283 if (ofwcall != NULL) {
285 status = OF_install(OFW_STD_REAL, 0);
287 status = OF_install(OFW_STD_DIRECT, 0);
292 OF_init(openfirmware);
294 status = OF_install(OFW_FDT, 0);
306 openfirmware(void *args)
313 if (pmap_bootstrapped && ofw_real_mode)
314 args = (void *)pmap_kextract((vm_offset_t)args);
316 __asm __volatile( "\t"
327 if (pmap_bootstrapped && !ofw_real_mode) {
329 * Swap the kernel's address space with Open Firmware's
331 for (i = 0; i < 16; i++) {
332 srsave[i] = mfsrin(i << ADDR_SR_SHFT);
333 mtsrin(i << ADDR_SR_SHFT, ofw_pmap.pm_sr[i]);
337 * Clear battable[] translations
340 __asm __volatile("mtdbatu 2, %0\n"
341 "mtdbatu 3, %0" : : "r" (0));
346 result = ofwcall(args);
348 if (pmap_bootstrapped && !ofw_real_mode) {
350 * Restore the kernel's addr space. The isync() doesn;t
351 * work outside the loop unless mtsrin() is open-coded
352 * in an asm statement :(
354 for (i = 0; i < 16; i++) {
355 mtsrin(i << ADDR_SR_SHFT, srsave[i]);
374 int retval; /* dummy, this may not be needed */
376 OF_interpret("shut-down", 1, &retval);
377 for (;;); /* just in case */
383 int retval; /* dummy, this may not be needed */
385 OF_interpret("reset-all", 1, &retval);
386 for (;;); /* just in case */
390 OF_getetheraddr(device_t dev, u_char *addr)
394 node = ofw_bus_get_node(dev);
395 OF_getprop(node, "local-mac-address", addr, ETHER_ADDR_LEN);
399 * Return a bus handle and bus tag that corresponds to the register
400 * numbered regno for the device referenced by the package handle
401 * dev. This function is intended to be used by console drivers in
402 * early boot only. It works by mapping the address of the device's
403 * register in the address space of its parent and recursively walk
404 * the device tree upward this way.
407 OF_get_addr_props(phandle_t node, uint32_t *addrp, uint32_t *sizep, int *pcip)
413 res = OF_getprop(node, "#address-cells", &addr, sizeof(addr));
416 res = OF_getprop(node, "#size-cells", &size, sizeof(size));
420 if (addr == 3 && size == 2) {
421 res = OF_getprop(node, "name", name, sizeof(name));
423 name[sizeof(name) - 1] = '\0';
424 pci = (strcmp(name, "pci") == 0) ? 1 : 0;
436 OF_decode_addr(phandle_t dev, int regno, bus_space_tag_t *tag,
437 bus_space_handle_t *handle)
440 bus_addr_t addr, raddr, baddr;
441 bus_size_t size, rsize;
442 uint32_t c, nbridge, naddr, nsize;
443 phandle_t bridge, parent;
447 /* Sanity checking. */
450 bridge = OF_parent(dev);
455 if (tag == NULL || handle == NULL)
458 /* Get the requested register. */
459 OF_get_addr_props(bridge, &naddr, &nsize, &pci);
460 res = OF_getprop(dev, (pci) ? "assigned-addresses" : "reg",
464 if (res % sizeof(cell[0]))
466 res /= sizeof(cell[0]);
467 regno *= naddr + nsize;
468 if (regno + naddr + nsize > res)
470 spc = (pci) ? cell[regno] & OFW_PCI_PHYS_HI_SPACEMASK : ~0;
472 for (c = 0; c < naddr; c++)
473 addr = ((uint64_t)addr << 32) | cell[regno++];
475 for (c = 0; c < nsize; c++)
476 size = ((uint64_t)size << 32) | cell[regno++];
479 * Map the address range in the bridge's decoding window as given
480 * by the "ranges" property. If a node doesn't have such property
481 * then no mapping is done.
483 parent = OF_parent(bridge);
484 while (parent != 0) {
485 OF_get_addr_props(parent, &nbridge, NULL, &pcib);
486 res = OF_getprop(bridge, "ranges", cell, sizeof(cell));
489 if (res % sizeof(cell[0]))
491 res /= sizeof(cell[0]);
493 while (regno < res) {
495 ? cell[regno] & OFW_PCI_PHYS_HI_SPACEMASK
498 regno += naddr + nbridge + nsize;
502 for (c = 0; c < naddr; c++)
503 raddr = ((uint64_t)raddr << 32) | cell[regno++];
505 ? cell[regno] & OFW_PCI_PHYS_HI_SPACEMASK
508 for (c = 0; c < nbridge; c++)
509 baddr = ((uint64_t)baddr << 32) | cell[regno++];
511 for (c = 0; c < nsize; c++)
512 rsize = ((uint64_t)rsize << 32) | cell[regno++];
513 if (addr < raddr || addr >= raddr + rsize)
515 addr = addr - raddr + baddr;
522 parent = OF_parent(bridge);
523 OF_get_addr_props(bridge, &naddr, &nsize, &pci);
527 return (bus_space_map(*tag, addr, size, 0, handle));
531 mem_valid(vm_offset_t addr, int len)
535 for (i = 0; i < OFMEM_REGIONS; i++)
536 if ((addr >= OFmem[i].mr_start)
537 && (addr + len < OFmem[i].mr_start + OFmem[i].mr_size))