2 * Copyright (c) 2008 Marcel Moolenaar
3 * Copyright (c) 2009 Nathan Whitehorn
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
41 #include <machine/altivec.h> /* For save_vec() */
42 #include <machine/bus.h>
43 #include <machine/cpu.h>
44 #include <machine/fpu.h> /* For save_fpu() */
45 #include <machine/hid.h>
46 #include <machine/platformvar.h>
47 #include <machine/pmap.h>
48 #include <machine/setjmp.h>
49 #include <machine/smp.h>
50 #include <machine/spr.h>
52 #include <dev/ofw/openfirm.h>
53 #include <machine/ofw_machdep.h>
55 #include "platform_if.h"
59 static int powermac_probe(platform_t);
60 static int powermac_attach(platform_t);
61 void powermac_mem_regions(platform_t, struct mem_region *phys, int *physsz,
62 struct mem_region *avail, int *availsz);
63 static u_long powermac_timebase_freq(platform_t, struct cpuref *cpuref);
64 static int powermac_smp_first_cpu(platform_t, struct cpuref *cpuref);
65 static int powermac_smp_next_cpu(platform_t, struct cpuref *cpuref);
66 static int powermac_smp_get_bsp(platform_t, struct cpuref *cpuref);
67 static int powermac_smp_start_cpu(platform_t, struct pcpu *cpu);
68 static void powermac_reset(platform_t);
69 static void powermac_sleep(platform_t);
71 static platform_method_t powermac_methods[] = {
72 PLATFORMMETHOD(platform_probe, powermac_probe),
73 PLATFORMMETHOD(platform_attach, powermac_attach),
74 PLATFORMMETHOD(platform_mem_regions, powermac_mem_regions),
75 PLATFORMMETHOD(platform_timebase_freq, powermac_timebase_freq),
77 PLATFORMMETHOD(platform_smp_first_cpu, powermac_smp_first_cpu),
78 PLATFORMMETHOD(platform_smp_next_cpu, powermac_smp_next_cpu),
79 PLATFORMMETHOD(platform_smp_get_bsp, powermac_smp_get_bsp),
80 PLATFORMMETHOD(platform_smp_start_cpu, powermac_smp_start_cpu),
82 PLATFORMMETHOD(platform_reset, powermac_reset),
83 PLATFORMMETHOD(platform_sleep, powermac_sleep),
88 static platform_def_t powermac_platform = {
94 PLATFORM_DEF(powermac_platform);
97 powermac_probe(platform_t plat)
108 compatlen = OF_getprop(root, "compatible", compat, sizeof(compat));
110 for (curstr = compat; curstr < compat + compatlen;
111 curstr += strlen(curstr) + 1) {
112 if (strncmp(curstr, "MacRISC", 7) == 0)
113 return (BUS_PROBE_SPECIFIC);
120 powermac_mem_regions(platform_t plat, struct mem_region *phys, int *physsz,
121 struct mem_region *avail, int *availsz)
124 cell_t memoryprop[PHYS_AVAIL_SZ * 2];
125 ssize_t propsize, i, j;
128 memory = OF_finddevice("/memory");
130 memory = OF_finddevice("/memory@0");
132 /* "reg" has variable #address-cells, but #size-cells is always 1 */
133 OF_getprop(OF_parent(memory), "#address-cells", &physacells,
136 propsize = OF_getprop(memory, "reg", memoryprop, sizeof(memoryprop));
137 propsize /= sizeof(cell_t);
138 for (i = 0, j = 0; i < propsize; i += physacells+1, j++) {
139 phys[j].mr_start = memoryprop[i];
140 if (physacells == 2) {
141 #ifndef __powerpc64__
142 /* On 32-bit PPC, ignore regions starting above 4 GB */
143 if (memoryprop[i] != 0) {
148 phys[j].mr_start <<= 32;
150 phys[j].mr_start |= memoryprop[i+1];
152 phys[j].mr_size = memoryprop[i + physacells];
156 /* "available" always has #address-cells = 1 */
157 propsize = OF_getprop(memory, "available", memoryprop,
160 for (i = 0; i < *physsz; i++) {
161 avail[i].mr_start = phys[i].mr_start;
162 avail[i].mr_size = phys[i].mr_size;
167 propsize /= sizeof(cell_t);
168 for (i = 0, j = 0; i < propsize; i += 2, j++) {
169 avail[j].mr_start = memoryprop[i];
170 avail[j].mr_size = memoryprop[i + 1];
174 /* Add in regions above 4 GB to the available list */
175 for (i = 0; i < *physsz; i++) {
176 if (phys[i].mr_start > BUS_SPACE_MAXADDR_32BIT) {
177 avail[j].mr_start = phys[i].mr_start;
178 avail[j].mr_size = phys[i].mr_size;
188 powermac_attach(platform_t plat)
195 * Quiesce Open Firmware on PowerMac11,2 and 12,1. It is
196 * necessary there to shut down a background thread doing fan
197 * management, and is harmful on other machines (it will make OF
198 * shut off power to various system components it had turned on).
200 * Note: we don't need to worry about which OF module we are
201 * using since this is called only from very early boot, within
205 rootnode = OF_finddevice("/");
206 if (OF_getprop(rootnode, "model", model, sizeof(model)) > 0) {
207 if (strcmp(model, "PowerMac11,2") == 0 ||
208 strcmp(model, "PowerMac12,1") == 0) {
217 powermac_timebase_freq(platform_t plat, struct cpuref *cpuref)
222 phandle = cpuref->cr_hwref;
224 OF_getprop(phandle, "timebase-frequency", &ticks, sizeof(ticks));
227 panic("Unable to determine timebase frequency!");
234 powermac_smp_fill_cpuref(struct cpuref *cpuref, phandle_t cpu)
239 cpuref->cr_hwref = cpu;
240 res = OF_getprop(cpu, "reg", &cpuid, sizeof(cpuid));
243 * psim doesn't have a reg property, so assume 0 as for the
244 * uniprocessor case in the CHRP spec.
250 cpuref->cr_cpuid = cpuid & 0xff;
255 powermac_smp_first_cpu(platform_t plat, struct cpuref *cpuref)
258 phandle_t cpu, dev, root;
263 dev = OF_child(root);
265 res = OF_getprop(dev, "name", buf, sizeof(buf));
266 if (res > 0 && strcmp(buf, "cpus") == 0)
272 * psim doesn't have a name property on the /cpus node,
273 * but it can be found directly
275 dev = OF_finddevice("/cpus");
283 res = OF_getprop(cpu, "device_type", buf, sizeof(buf));
284 if (res > 0 && strcmp(buf, "cpu") == 0)
291 return (powermac_smp_fill_cpuref(cpuref, cpu));
295 powermac_smp_next_cpu(platform_t plat, struct cpuref *cpuref)
301 cpu = OF_peer(cpuref->cr_hwref);
303 res = OF_getprop(cpu, "device_type", buf, sizeof(buf));
304 if (res > 0 && strcmp(buf, "cpu") == 0)
311 return (powermac_smp_fill_cpuref(cpuref, cpu));
315 powermac_smp_get_bsp(platform_t plat, struct cpuref *cpuref)
318 phandle_t bsp, chosen;
321 chosen = OF_finddevice("/chosen");
325 res = OF_getprop(chosen, "cpu", &inst, sizeof(inst));
329 bsp = OF_instance_to_package(inst);
330 return (powermac_smp_fill_cpuref(cpuref, bsp));
334 powermac_smp_start_cpu(platform_t plat, struct pcpu *pc)
338 volatile uint8_t *rstvec;
339 static volatile uint8_t *rstvec_virtbase = NULL;
340 int res, reset, timeout;
343 res = OF_getprop(cpu, "soft-reset", &reset, sizeof(reset));
347 switch (pc->pc_cpuid) {
367 if (rstvec_virtbase == NULL)
368 rstvec_virtbase = pmap_mapdev(0x80000000, PAGE_SIZE);
370 rstvec = rstvec_virtbase + reset;
383 while (!pc->pc_awake && timeout--)
386 return ((pc->pc_awake) ? 0 : EBUSY);
394 powermac_reset(platform_t platform)
400 powermac_sleep(platform_t platform)
403 *(unsigned long *)0x80 = 0x100;