2 * Copyright (c) 2008-2012 Semihalf.
5 * Redistribution and use in source and binary forms, with or without
6 * 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.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
38 #include <machine/bus.h>
39 #include <machine/cpu.h>
40 #include <machine/hid.h>
41 #include <machine/platform.h>
42 #include <machine/platformvar.h>
43 #include <machine/smp.h>
44 #include <machine/spr.h>
45 #include <machine/vmparam.h>
47 #include <dev/fdt/fdt_common.h>
48 #include <dev/ofw/ofw_bus.h>
49 #include <dev/ofw/ofw_bus_subr.h>
50 #include <dev/ofw/openfirm.h>
52 #include <powerpc/mpc85xx/mpc85xx.h>
54 #include "platform_if.h"
58 extern uint8_t __boot_page[]; /* Boot page body */
59 extern uint32_t bp_kernload; /* Kernel physical load address */
60 extern uint32_t bp_trace; /* AP boot trace field */
63 extern uint32_t *bootinfo;
65 static int cpu, maxcpu;
67 static int bare_probe(platform_t);
68 static void bare_mem_regions(platform_t, struct mem_region **phys, int *physsz,
69 struct mem_region **avail, int *availsz);
70 static u_long bare_timebase_freq(platform_t, struct cpuref *cpuref);
71 static int bare_smp_first_cpu(platform_t, struct cpuref *cpuref);
72 static int bare_smp_next_cpu(platform_t, struct cpuref *cpuref);
73 static int bare_smp_get_bsp(platform_t, struct cpuref *cpuref);
74 static int bare_smp_start_cpu(platform_t, struct pcpu *cpu);
76 static void booke_reset(platform_t);
78 static platform_method_t bare_methods[] = {
79 PLATFORMMETHOD(platform_probe, bare_probe),
80 PLATFORMMETHOD(platform_mem_regions, bare_mem_regions),
81 PLATFORMMETHOD(platform_timebase_freq, bare_timebase_freq),
83 PLATFORMMETHOD(platform_smp_first_cpu, bare_smp_first_cpu),
84 PLATFORMMETHOD(platform_smp_next_cpu, bare_smp_next_cpu),
85 PLATFORMMETHOD(platform_smp_get_bsp, bare_smp_get_bsp),
86 PLATFORMMETHOD(platform_smp_start_cpu, bare_smp_start_cpu),
88 PLATFORMMETHOD(platform_reset, booke_reset),
93 static platform_def_t bare_platform = {
99 PLATFORM_DEF(bare_platform);
102 bare_probe(platform_t plat)
104 phandle_t cpus, child;
108 if ((cpus = OF_finddevice("/cpus")) != 0) {
109 for (maxcpu = 0, child = OF_child(cpus); child != 0;
110 child = OF_peer(child), maxcpu++)
116 * Clear local access windows. Skip DRAM entries, so we don't shoot
117 * ourselves in the foot.
119 law_max = law_getmax();
120 for (i = 0; i < law_max; i++) {
121 sr = ccsr_read4(OCP85XX_LAWSR(i));
122 if ((sr & 0x80000000) == 0)
124 tgt = (sr & 0x01f00000) >> 20;
125 if (tgt == OCP85XX_TGTIF_RAM1 || tgt == OCP85XX_TGTIF_RAM2 ||
126 tgt == OCP85XX_TGTIF_RAM_INTL)
129 ccsr_write4(OCP85XX_LAWSR(i), sr & 0x7fffffff);
132 return (BUS_PROBE_GENERIC);
135 #define MEM_REGIONS 8
136 static struct mem_region avail_regions[MEM_REGIONS];
139 bare_mem_regions(platform_t plat, struct mem_region **phys, int *physsz,
140 struct mem_region **avail, int *availsz)
145 rv = fdt_get_mem_regions(avail_regions, availsz, &memsize);
147 panic("%s: could not retrieve mem regions from the 'memory' "
148 "node, error: %d", __func__, rv);
150 for (i = 0; i < *availsz; i++) {
151 if (avail_regions[i].mr_start < 1048576) {
152 avail_regions[i].mr_size =
153 avail_regions[i].mr_size -
154 (1048576 - avail_regions[i].mr_start);
155 avail_regions[i].mr_start = 1048576;
158 *avail = avail_regions;
160 /* On the bare metal platform phys == avail memory */
166 bare_timebase_freq(platform_t plat, struct cpuref *cpuref)
169 phandle_t cpus, child;
172 if (bootinfo != NULL) {
173 if (bootinfo[0] == 1) {
174 /* Backward compatibility. See 8-STABLE. */
175 ticks = bootinfo[3] >> 3;
177 /* Compatibility with Juniper's loader. */
178 ticks = bootinfo[5] >> 3;
183 if ((cpus = OF_finddevice("/cpus")) == -1)
186 if ((child = OF_child(cpus)) == 0)
190 if (OF_getprop(child, "bus-frequency", (void *)&freq,
195 * Time Base and Decrementer are updated every 8 CCB bus clocks.
196 * HID0[SEL_TBCLK] = 0
203 panic("Unable to determine timebase frequency!");
209 bare_smp_first_cpu(platform_t plat, struct cpuref *cpuref)
213 cpuref->cr_cpuid = cpu;
214 cpuref->cr_hwref = cpuref->cr_cpuid;
216 printf("powerpc_smp_first_cpu: cpuid %d\n", cpuref->cr_cpuid);
223 bare_smp_next_cpu(platform_t plat, struct cpuref *cpuref)
229 cpuref->cr_cpuid = cpu++;
230 cpuref->cr_hwref = cpuref->cr_cpuid;
232 printf("powerpc_smp_next_cpu: cpuid %d\n", cpuref->cr_cpuid);
238 bare_smp_get_bsp(platform_t plat, struct cpuref *cpuref)
241 cpuref->cr_cpuid = mfspr(SPR_PIR);
242 cpuref->cr_hwref = cpuref->cr_cpuid;
248 bare_smp_start_cpu(platform_t plat, struct pcpu *pc)
251 uint32_t bptr, eebpcr;
254 eebpcr = ccsr_read4(OCP85XX_EEBPCR);
255 if ((eebpcr & (1 << (pc->pc_cpuid + 24))) != 0) {
256 printf("SMP: CPU %d already out of hold-off state!\n",
262 __asm __volatile("msync; isync");
265 * Set BPTR to the physical address of the boot page
267 bptr = ((uint32_t)__boot_page - KERNBASE) + bp_kernload;
268 ccsr_write4(OCP85XX_BPTR, (bptr >> 12) | 0x80000000);
271 * Release AP from hold-off state
274 eebpcr |= (1 << (pc->pc_cpuid + 24));
275 ccsr_write4(OCP85XX_EEBPCR, eebpcr);
276 __asm __volatile("isync; msync");
279 while (!pc->pc_awake && timeout--)
280 DELAY(1000); /* wait 1ms */
283 * Disable boot page translation so that the 4K page at the default
284 * address (= 0xfffff000) isn't permanently remapped and thus not
287 ccsr_write4(OCP85XX_BPTR, 0);
290 printf("SMP: CPU %d didn't wake up (trace code %#x).\n",
291 pc->pc_awake, bp_trace);
292 return ((pc->pc_awake) ? 0 : EBUSY);
300 booke_reset(platform_t plat)
304 * Try the dedicated reset register first.
305 * If the SoC doesn't have one, we'll fall
306 * back to using the debug control register.
308 ccsr_write4(OCP85XX_RSTCR, 2);
310 /* Clear DBCR0, disables debug interrupts and events. */
312 __asm __volatile("isync");
314 /* Enable Debug Interrupts in MSR. */
315 mtmsr(mfmsr() | PSL_DE);
317 /* Enable debug interrupts and issue reset. */
318 mtspr(SPR_DBCR0, mfspr(SPR_DBCR0) | DBCR0_IDM | DBCR0_RST_SYSTEM);
320 printf("Reset failed...\n");