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>
39 #include <machine/bus.h>
40 #include <machine/cpu.h>
41 #include <machine/hid.h>
42 #include <machine/platformvar.h>
43 #include <machine/smp.h>
44 #include <machine/spr.h>
46 #include <dev/ofw/openfirm.h>
47 #include <machine/ofw_machdep.h>
49 #include "platform_if.h"
55 static int chrp_probe(platform_t);
56 void chrp_mem_regions(platform_t, struct mem_region **phys, int *physsz,
57 struct mem_region **avail, int *availsz);
58 static u_long chrp_timebase_freq(platform_t, struct cpuref *cpuref);
59 static int chrp_smp_first_cpu(platform_t, struct cpuref *cpuref);
60 static int chrp_smp_next_cpu(platform_t, struct cpuref *cpuref);
61 static int chrp_smp_get_bsp(platform_t, struct cpuref *cpuref);
62 static int chrp_smp_start_cpu(platform_t, struct pcpu *cpu);
64 static platform_method_t chrp_methods[] = {
65 PLATFORMMETHOD(platform_probe, chrp_probe),
66 PLATFORMMETHOD(platform_mem_regions, chrp_mem_regions),
67 PLATFORMMETHOD(platform_timebase_freq, chrp_timebase_freq),
69 PLATFORMMETHOD(platform_smp_first_cpu, chrp_smp_first_cpu),
70 PLATFORMMETHOD(platform_smp_next_cpu, chrp_smp_next_cpu),
71 PLATFORMMETHOD(platform_smp_get_bsp, chrp_smp_get_bsp),
72 PLATFORMMETHOD(platform_smp_start_cpu, chrp_smp_start_cpu),
77 static platform_def_t chrp_platform = {
83 PLATFORM_DEF(chrp_platform);
86 chrp_probe(platform_t plat)
88 if (OF_finddevice("/memory") != -1)
89 return (BUS_PROBE_GENERIC);
95 chrp_mem_regions(platform_t plat, struct mem_region **phys, int *physsz,
96 struct mem_region **avail, int *availsz)
98 ofw_mem_regions(phys,physsz,avail,availsz);
102 chrp_timebase_freq(platform_t plat, struct cpuref *cpuref)
107 phandle = cpuref->cr_hwref;
109 OF_getprop(phandle, "timebase-frequency", &ticks, sizeof(ticks));
112 panic("Unable to determine timebase frequency!");
119 chrp_smp_fill_cpuref(struct cpuref *cpuref, phandle_t cpu)
123 cpuref->cr_hwref = cpu;
124 res = OF_getprop(cpu, "reg", &cpuid, sizeof(cpuid));
127 * psim doesn't have a reg property, so assume 0 as for the
128 * uniprocessor case in the CHRP spec.
134 cpuref->cr_cpuid = cpuid & 0xff;
139 chrp_smp_first_cpu(platform_t plat, struct cpuref *cpuref)
142 phandle_t cpu, dev, root;
147 dev = OF_child(root);
149 res = OF_getprop(dev, "name", buf, sizeof(buf));
150 if (res > 0 && strcmp(buf, "cpus") == 0)
156 * psim doesn't have a name property on the /cpus node,
157 * but it can be found directly
159 dev = OF_finddevice("/cpus");
167 res = OF_getprop(cpu, "device_type", buf, sizeof(buf));
168 if (res > 0 && strcmp(buf, "cpu") == 0)
175 return (chrp_smp_fill_cpuref(cpuref, cpu));
179 chrp_smp_next_cpu(platform_t plat, struct cpuref *cpuref)
185 cpu = OF_peer(cpuref->cr_hwref);
187 res = OF_getprop(cpu, "device_type", buf, sizeof(buf));
188 if (res > 0 && strcmp(buf, "cpu") == 0)
195 return (chrp_smp_fill_cpuref(cpuref, cpu));
199 chrp_smp_get_bsp(platform_t plat, struct cpuref *cpuref)
202 phandle_t bsp, chosen;
205 chosen = OF_finddevice("/chosen");
209 res = OF_getprop(chosen, "cpu", &inst, sizeof(inst));
213 bsp = OF_instance_to_package(inst);
214 return (chrp_smp_fill_cpuref(cpuref, bsp));
218 chrp_smp_start_cpu(platform_t plat, struct pcpu *pc)
222 volatile uint8_t *rstvec;
223 int res, reset, timeout;
226 res = OF_getprop(cpu, "soft-reset", &reset, sizeof(reset));
232 rstvec = (uint8_t *)(0x80000000 + reset);
241 while (!pc->pc_awake && timeout--)
244 return ((pc->pc_awake) ? 0 : EBUSY);