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/cpu.h>
58 #include <machine/md_var.h>
59 #include <machine/platform.h>
60 #include <machine/ofw_machdep.h>
62 #define OFMEM_REGIONS 32
63 static struct mem_region OFmem[OFMEM_REGIONS + 1], OFavail[OFMEM_REGIONS + 3];
64 static struct mem_region OFfree[OFMEM_REGIONS + 3];
67 vm_offset_t mr_start_hi;
68 vm_offset_t mr_start_lo;
72 extern register_t ofmsr[5];
73 extern struct pmap ofw_pmap;
74 static int (*ofwcall)(void *);
78 static void ofw_quiesce(void);
79 static int openfirmware(void *args);
82 * Saved SPRG0-3 from OpenFirmware. Will be restored prior to the callback.
84 register_t ofw_sprg0_save;
87 ofw_sprg_prepare(void)
90 * Assume that interrupt are disabled at this point, or
91 * SPRG1-3 could be trashed
93 __asm __volatile("mfsprg0 %0\n\t"
98 : "=&r"(ofw_sprg0_save)
106 ofw_sprg_restore(void)
109 * Note that SPRG1-3 contents are irrelevant. They are scratch
110 * registers used in the early portion of trap handling when
111 * interrupts are disabled.
113 * PCPU data cannot be used until this routine is called !
115 __asm __volatile("mtsprg0 %0" :: "r"(ofw_sprg0_save));
119 * Memory region utilities: determine if two regions overlap,
120 * and merge two overlapping regions into one
123 memr_overlap(struct mem_region *r1, struct mem_region *r2)
125 if ((r1->mr_start + r1->mr_size) < r2->mr_start ||
126 (r2->mr_start + r2->mr_size) < r1->mr_start)
133 memr_merge(struct mem_region *from, struct mem_region *to)
136 end = imax(to->mr_start + to->mr_size, from->mr_start + from->mr_size);
137 to->mr_start = imin(from->mr_start, to->mr_start);
138 to->mr_size = end - to->mr_start;
142 * This is called during powerpc_init, before the system is really initialized.
143 * It shall provide the total and the available regions of RAM.
144 * Both lists must have a zero-size entry as terminator.
145 * The available regions need not take the kernel into account, but needs
146 * to provide space for two additional entry beyond the terminating one.
149 ofw_mem_regions(struct mem_region **memp, int *memsz,
150 struct mem_region **availp, int *availsz)
156 cell_t address_cells;
161 * Get #address-cells from root node, defaulting to 1 if it cannot
164 phandle = OF_finddevice("/");
165 if (OF_getprop(phandle, "#address-cells", &address_cells,
166 sizeof(address_cells)) < sizeof(address_cells))
172 if ((phandle = OF_finddevice("/memory")) == -1
173 || (asz = OF_getprop(phandle, "available",
174 OFavail, sizeof OFavail[0] * OFMEM_REGIONS)) <= 0)
178 printf("Physical memory unknown -- guessing 128 MB\n");
180 /* Leave the first 0xA000000 bytes for the kernel */
181 OFavail[0].mr_start = 0xA00000;
182 OFavail[0].mr_size = 0x75FFFFF;
183 asz = sizeof(OFavail[0]);
189 if (address_cells == 2) {
190 struct mem_region64 OFmem64[OFMEM_REGIONS + 1];
191 if ((phandle == -1) || (msz = OF_getprop(phandle, "reg",
192 OFmem64, sizeof OFmem64[0] * OFMEM_REGIONS)) <= 0) {
195 OFmem64[0].mr_start_hi = 0;
196 OFmem64[0].mr_start_lo = 0x0;
197 OFmem64[0].mr_size = 0x7FFFFFF;
198 msz = sizeof(OFmem64[0]);
200 panic("Physical memory map not found");
204 for (i = 0, j = 0; i < msz/sizeof(OFmem64[0]); i++) {
205 if (OFmem64[i].mr_start_hi == 0) {
206 OFmem[i].mr_start = OFmem64[i].mr_start_lo;
207 OFmem[i].mr_size = OFmem64[i].mr_size;
210 * Check for memory regions extending above 32-bit
211 * memory space, and restrict them to stay there.
213 if (((uint64_t)OFmem[i].mr_start +
214 (uint64_t)OFmem[i].mr_size) >
215 BUS_SPACE_MAXADDR_32BIT) {
216 OFmem[i].mr_size = BUS_SPACE_MAXADDR_32BIT -
222 msz = j*sizeof(OFmem[0]);
224 if ((msz = OF_getprop(phandle, "reg",
225 OFmem, sizeof OFmem[0] * OFMEM_REGIONS)) <= 0)
226 panic("Physical memory map not found");
230 *memsz = msz / sizeof(struct mem_region);
234 * OFavail may have overlapping regions - collapse these
235 * and copy out remaining regions to OFfree
237 asz /= sizeof(struct mem_region);
239 still_merging = FALSE;
240 for (i = 0; i < asz; i++) {
241 if (OFavail[i].mr_size == 0)
243 for (j = i+1; j < asz; j++) {
244 if (OFavail[j].mr_size == 0)
246 if (memr_overlap(&OFavail[j], &OFavail[i])) {
247 memr_merge(&OFavail[j], &OFavail[i]);
249 OFavail[j].mr_size = 0;
250 still_merging = TRUE;
254 } while (still_merging == TRUE);
256 /* evict inactive ranges */
257 for (i = 0, fsz = 0; i < asz; i++) {
258 if (OFavail[i].mr_size != 0) {
259 OFfree[fsz] = OFavail[i];
269 OF_initial_setup(void *fdt_ptr, void *junk, int (*openfirm)(void *))
271 if (ofmsr[0] & PSL_DR)
283 boolean_t status = FALSE;
285 if (ofwcall != NULL) {
287 status = OF_install(OFW_STD_REAL, 0);
289 status = OF_install(OFW_STD_DIRECT, 0);
294 OF_init(openfirmware);
297 * On some machines, we need to quiesce OF to turn off
298 * background processes.
302 status = OF_install(OFW_FDT, 0);
325 * Only quiesce Open Firmware on PowerMac11,2 and 12,1. It is
326 * necessary there to shut down a background thread doing fan
327 * management, and is harmful on other machines.
329 * Note: we don't need to worry about which OF module we are
330 * using since this is called only from very early boot, within
334 rootnode = OF_finddevice("/");
335 if (OF_getprop(rootnode, "model", model, sizeof(model)) > 0) {
336 if (strcmp(model, "PowerMac11,2") == 0 ||
337 strcmp(model, "PowerMac12,1") == 0) {
338 args.name = (cell_t)(uintptr_t)"quiesce";
347 openfirmware(void *args)
354 if (pmap_bootstrapped && ofw_real_mode)
355 args = (void *)pmap_kextract((vm_offset_t)args);
357 __asm __volatile( "\t"
368 if (pmap_bootstrapped && !ofw_real_mode) {
370 * Swap the kernel's address space with Open Firmware's
372 for (i = 0; i < 16; i++) {
373 srsave[i] = mfsrin(i << ADDR_SR_SHFT);
374 mtsrin(i << ADDR_SR_SHFT, ofw_pmap.pm_sr[i]);
378 * Clear battable[] translations
380 if (!(cpu_features & PPC_FEATURE_64)) {
381 __asm __volatile("mtdbatu 2, %0\n"
382 "mtdbatu 3, %0" : : "r" (0));
387 result = ofwcall(args);
389 if (pmap_bootstrapped && !ofw_real_mode) {
391 * Restore the kernel's addr space. The isync() doesn;t
392 * work outside the loop unless mtsrin() is open-coded
393 * in an asm statement :(
395 for (i = 0; i < 16; i++) {
396 mtsrin(i << ADDR_SR_SHFT, srsave[i]);
415 int retval; /* dummy, this may not be needed */
417 OF_interpret("shut-down", 1, &retval);
418 for (;;); /* just in case */
424 int retval; /* dummy, this may not be needed */
426 OF_interpret("reset-all", 1, &retval);
427 for (;;); /* just in case */
431 OF_getetheraddr(device_t dev, u_char *addr)
435 node = ofw_bus_get_node(dev);
436 OF_getprop(node, "local-mac-address", addr, ETHER_ADDR_LEN);
440 * Return a bus handle and bus tag that corresponds to the register
441 * numbered regno for the device referenced by the package handle
442 * dev. This function is intended to be used by console drivers in
443 * early boot only. It works by mapping the address of the device's
444 * register in the address space of its parent and recursively walk
445 * the device tree upward this way.
448 OF_get_addr_props(phandle_t node, uint32_t *addrp, uint32_t *sizep, int *pcip)
454 res = OF_getprop(node, "#address-cells", &addr, sizeof(addr));
457 res = OF_getprop(node, "#size-cells", &size, sizeof(size));
461 if (addr == 3 && size == 2) {
462 res = OF_getprop(node, "name", name, sizeof(name));
464 name[sizeof(name) - 1] = '\0';
465 pci = (strcmp(name, "pci") == 0) ? 1 : 0;
477 OF_decode_addr(phandle_t dev, int regno, bus_space_tag_t *tag,
478 bus_space_handle_t *handle)
481 bus_addr_t addr, raddr, baddr;
482 bus_size_t size, rsize;
483 uint32_t c, nbridge, naddr, nsize;
484 phandle_t bridge, parent;
488 /* Sanity checking. */
491 bridge = OF_parent(dev);
496 if (tag == NULL || handle == NULL)
499 /* Get the requested register. */
500 OF_get_addr_props(bridge, &naddr, &nsize, &pci);
501 res = OF_getprop(dev, (pci) ? "assigned-addresses" : "reg",
505 if (res % sizeof(cell[0]))
507 res /= sizeof(cell[0]);
508 regno *= naddr + nsize;
509 if (regno + naddr + nsize > res)
511 spc = (pci) ? cell[regno] & OFW_PCI_PHYS_HI_SPACEMASK : ~0;
513 for (c = 0; c < naddr; c++)
514 addr = ((uint64_t)addr << 32) | cell[regno++];
516 for (c = 0; c < nsize; c++)
517 size = ((uint64_t)size << 32) | cell[regno++];
520 * Map the address range in the bridge's decoding window as given
521 * by the "ranges" property. If a node doesn't have such property
522 * then no mapping is done.
524 parent = OF_parent(bridge);
525 while (parent != 0) {
526 OF_get_addr_props(parent, &nbridge, NULL, &pcib);
527 res = OF_getprop(bridge, "ranges", cell, sizeof(cell));
530 if (res % sizeof(cell[0]))
532 res /= sizeof(cell[0]);
534 while (regno < res) {
536 ? cell[regno] & OFW_PCI_PHYS_HI_SPACEMASK
539 regno += naddr + nbridge + nsize;
543 for (c = 0; c < naddr; c++)
544 raddr = ((uint64_t)raddr << 32) | cell[regno++];
546 ? cell[regno] & OFW_PCI_PHYS_HI_SPACEMASK
549 for (c = 0; c < nbridge; c++)
550 baddr = ((uint64_t)baddr << 32) | cell[regno++];
552 for (c = 0; c < nsize; c++)
553 rsize = ((uint64_t)rsize << 32) | cell[regno++];
554 if (addr < raddr || addr >= raddr + rsize)
556 addr = addr - raddr + baddr;
563 parent = OF_parent(bridge);
564 OF_get_addr_props(bridge, &naddr, &nsize, &pci);
568 return (bus_space_map(*tag, addr, size, 0, handle));
572 mem_valid(vm_offset_t addr, int len)
576 for (i = 0; i < OFMEM_REGIONS; i++)
577 if ((addr >= OFmem[i].mr_start)
578 && (addr + len < OFmem[i].mr_start + OFmem[i].mr_size))