xref: /freebsd/sys/powerpc/booke/platform_bare.c (revision b2a237be5c4031f91743bde57571075ff388e497)
1b40ce02aSNathan Whitehorn /*-
2b40ce02aSNathan Whitehorn  * Copyright (c) 2008-2009 Semihalf, Rafal Jaworowski
3b40ce02aSNathan Whitehorn  * All rights reserved.
4b40ce02aSNathan Whitehorn  *
5b40ce02aSNathan Whitehorn  * Redistribution and use in source and binary forms, with or without
6b40ce02aSNathan Whitehorn  * modification, are permitted provided that the following conditions
7b40ce02aSNathan Whitehorn  * are met:
8b40ce02aSNathan Whitehorn  *
9b40ce02aSNathan Whitehorn  * 1. Redistributions of source code must retain the above copyright
10b40ce02aSNathan Whitehorn  *    notice, this list of conditions and the following disclaimer.
11b40ce02aSNathan Whitehorn  * 2. Redistributions in binary form must reproduce the above copyright
12b40ce02aSNathan Whitehorn  *    notice, this list of conditions and the following disclaimer in the
13b40ce02aSNathan Whitehorn  *    documentation and/or other materials provided with the distribution.
14b40ce02aSNathan Whitehorn  *
15b40ce02aSNathan Whitehorn  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16b40ce02aSNathan Whitehorn  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17b40ce02aSNathan Whitehorn  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18b40ce02aSNathan Whitehorn  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19b40ce02aSNathan Whitehorn  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20b40ce02aSNathan Whitehorn  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21b40ce02aSNathan Whitehorn  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22b40ce02aSNathan Whitehorn  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23b40ce02aSNathan Whitehorn  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24b40ce02aSNathan Whitehorn  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25b40ce02aSNathan Whitehorn  */
26b40ce02aSNathan Whitehorn 
27b40ce02aSNathan Whitehorn #include <sys/cdefs.h>
28b40ce02aSNathan Whitehorn __FBSDID("$FreeBSD$");
29b40ce02aSNathan Whitehorn 
30b40ce02aSNathan Whitehorn #include <sys/param.h>
31b40ce02aSNathan Whitehorn #include <sys/systm.h>
32b40ce02aSNathan Whitehorn #include <sys/kernel.h>
33b40ce02aSNathan Whitehorn #include <sys/bus.h>
34b40ce02aSNathan Whitehorn #include <sys/pcpu.h>
35b40ce02aSNathan Whitehorn #include <sys/proc.h>
36b40ce02aSNathan Whitehorn #include <sys/smp.h>
37b40ce02aSNathan Whitehorn 
38b40ce02aSNathan Whitehorn #include <machine/bus.h>
39b40ce02aSNathan Whitehorn #include <machine/cpu.h>
40b40ce02aSNathan Whitehorn #include <machine/hid.h>
41b40ce02aSNathan Whitehorn #include <machine/platform.h>
42b40ce02aSNathan Whitehorn #include <machine/platformvar.h>
43b40ce02aSNathan Whitehorn #include <machine/smp.h>
44b40ce02aSNathan Whitehorn #include <machine/spr.h>
45b40ce02aSNathan Whitehorn #include <machine/vmparam.h>
46b40ce02aSNathan Whitehorn 
47d1d3233eSRafal Jaworowski #include <dev/fdt/fdt_common.h>
48d1d3233eSRafal Jaworowski #include <dev/ofw/ofw_bus.h>
49d1d3233eSRafal Jaworowski #include <dev/ofw/ofw_bus_subr.h>
50d1d3233eSRafal Jaworowski #include <dev/ofw/openfirm.h>
51d1d3233eSRafal Jaworowski 
52b40ce02aSNathan Whitehorn #include <powerpc/mpc85xx/mpc85xx.h>
53b40ce02aSNathan Whitehorn 
54b40ce02aSNathan Whitehorn #include "platform_if.h"
55b40ce02aSNathan Whitehorn 
5628bb01e5SRafal Jaworowski #ifdef SMP
5728bb01e5SRafal Jaworowski extern void *ap_pcpu;
5828bb01e5SRafal Jaworowski extern uint8_t __boot_page[];		/* Boot page body */
5928bb01e5SRafal Jaworowski extern uint32_t kernload;		/* Kernel physical load address */
6028bb01e5SRafal Jaworowski #endif
6128bb01e5SRafal Jaworowski 
622b7b2d79SRafal Jaworowski static int cpu, maxcpu;
63b40ce02aSNathan Whitehorn 
64b40ce02aSNathan Whitehorn static int bare_probe(platform_t);
65b40ce02aSNathan Whitehorn static void bare_mem_regions(platform_t, struct mem_region **phys, int *physsz,
66b40ce02aSNathan Whitehorn     struct mem_region **avail, int *availsz);
67b40ce02aSNathan Whitehorn static u_long bare_timebase_freq(platform_t, struct cpuref *cpuref);
68b40ce02aSNathan Whitehorn static int bare_smp_first_cpu(platform_t, struct cpuref *cpuref);
69b40ce02aSNathan Whitehorn static int bare_smp_next_cpu(platform_t, struct cpuref *cpuref);
70b40ce02aSNathan Whitehorn static int bare_smp_get_bsp(platform_t, struct cpuref *cpuref);
71b40ce02aSNathan Whitehorn static int bare_smp_start_cpu(platform_t, struct pcpu *cpu);
72b40ce02aSNathan Whitehorn 
73*b2a237beSNathan Whitehorn static void e500_reset(platform_t);
74*b2a237beSNathan Whitehorn 
75b40ce02aSNathan Whitehorn static platform_method_t bare_methods[] = {
76b40ce02aSNathan Whitehorn 	PLATFORMMETHOD(platform_probe, 		bare_probe),
77b40ce02aSNathan Whitehorn 	PLATFORMMETHOD(platform_mem_regions,	bare_mem_regions),
78b40ce02aSNathan Whitehorn 	PLATFORMMETHOD(platform_timebase_freq,	bare_timebase_freq),
79b40ce02aSNathan Whitehorn 
80b40ce02aSNathan Whitehorn 	PLATFORMMETHOD(platform_smp_first_cpu,	bare_smp_first_cpu),
81b40ce02aSNathan Whitehorn 	PLATFORMMETHOD(platform_smp_next_cpu,	bare_smp_next_cpu),
82b40ce02aSNathan Whitehorn 	PLATFORMMETHOD(platform_smp_get_bsp,	bare_smp_get_bsp),
83b40ce02aSNathan Whitehorn 	PLATFORMMETHOD(platform_smp_start_cpu,	bare_smp_start_cpu),
84b40ce02aSNathan Whitehorn 
85*b2a237beSNathan Whitehorn 	PLATFORMMETHOD(platform_reset,		e500_reset);
86*b2a237beSNathan Whitehorn 
87b40ce02aSNathan Whitehorn 	{ 0, 0 }
88b40ce02aSNathan Whitehorn };
89b40ce02aSNathan Whitehorn 
90b40ce02aSNathan Whitehorn static platform_def_t bare_platform = {
91b40ce02aSNathan Whitehorn 	"bare metal",
92b40ce02aSNathan Whitehorn 	bare_methods,
93b40ce02aSNathan Whitehorn 	0
94b40ce02aSNathan Whitehorn };
95b40ce02aSNathan Whitehorn 
96b40ce02aSNathan Whitehorn PLATFORM_DEF(bare_platform);
97b40ce02aSNathan Whitehorn 
98b40ce02aSNathan Whitehorn static int
99b40ce02aSNathan Whitehorn bare_probe(platform_t plat)
100b40ce02aSNathan Whitehorn {
101d1d3233eSRafal Jaworowski 	uint32_t ver, sr;
102d1d3233eSRafal Jaworowski 	int i, law_max, tgt;
1032b7b2d79SRafal Jaworowski 
1042b7b2d79SRafal Jaworowski 	ver = SVR_VER(mfspr(SPR_SVR));
1052b7b2d79SRafal Jaworowski 	if (ver == SVR_MPC8572E || ver == SVR_MPC8572)
1062b7b2d79SRafal Jaworowski 		maxcpu = 2;
1072b7b2d79SRafal Jaworowski 	else
1082b7b2d79SRafal Jaworowski 		maxcpu = 1;
109b40ce02aSNathan Whitehorn 
110d1d3233eSRafal Jaworowski 	/*
111d1d3233eSRafal Jaworowski 	 * Clear local access windows. Skip DRAM entries, so we don't shoot
112d1d3233eSRafal Jaworowski 	 * ourselves in the foot.
113d1d3233eSRafal Jaworowski 	 */
114d1d3233eSRafal Jaworowski 	law_max = law_getmax();
115d1d3233eSRafal Jaworowski 	for (i = 0; i < law_max; i++) {
116d1d3233eSRafal Jaworowski 		sr = ccsr_read4(OCP85XX_LAWSR(i));
117d1d3233eSRafal Jaworowski 		if ((sr & 0x80000000) == 0)
118d1d3233eSRafal Jaworowski 			continue;
119d1d3233eSRafal Jaworowski 		tgt = (sr & 0x01f00000) >> 20;
120d1d3233eSRafal Jaworowski 		if (tgt == OCP85XX_TGTIF_RAM1 || tgt == OCP85XX_TGTIF_RAM2 ||
121d1d3233eSRafal Jaworowski 		    tgt == OCP85XX_TGTIF_RAM_INTL)
122d1d3233eSRafal Jaworowski 			continue;
123d1d3233eSRafal Jaworowski 
124d1d3233eSRafal Jaworowski 		ccsr_write4(OCP85XX_LAWSR(i), sr & 0x7fffffff);
125d1d3233eSRafal Jaworowski 	}
126d1d3233eSRafal Jaworowski 
127b40ce02aSNathan Whitehorn 	return (BUS_PROBE_GENERIC);
128b40ce02aSNathan Whitehorn }
129b40ce02aSNathan Whitehorn 
130b40ce02aSNathan Whitehorn #define MEM_REGIONS	8
131b40ce02aSNathan Whitehorn static struct mem_region avail_regions[MEM_REGIONS];
132b40ce02aSNathan Whitehorn 
133b40ce02aSNathan Whitehorn void
134b40ce02aSNathan Whitehorn bare_mem_regions(platform_t plat, struct mem_region **phys, int *physsz,
135b40ce02aSNathan Whitehorn     struct mem_region **avail, int *availsz)
136b40ce02aSNathan Whitehorn {
137d1d3233eSRafal Jaworowski 	uint32_t memsize;
138d1d3233eSRafal Jaworowski 	int i, rv;
139b40ce02aSNathan Whitehorn 
140d1d3233eSRafal Jaworowski 	rv = fdt_get_mem_regions(avail_regions, availsz, &memsize);
141d1d3233eSRafal Jaworowski 
142d1d3233eSRafal Jaworowski 	if (rv != 0)
143d1d3233eSRafal Jaworowski 		return;
144d1d3233eSRafal Jaworowski 
145d1d3233eSRafal Jaworowski 	for (i = 0; i < *availsz; i++) {
146d1d3233eSRafal Jaworowski 		if (avail_regions[i].mr_start < 1048576) {
147d1d3233eSRafal Jaworowski 			avail_regions[i].mr_size =
148d1d3233eSRafal Jaworowski 			    avail_regions[i].mr_size -
149d1d3233eSRafal Jaworowski 			    (1048576 - avail_regions[i].mr_start);
150b40ce02aSNathan Whitehorn 			avail_regions[i].mr_start = 1048576;
151b40ce02aSNathan Whitehorn 		}
152b40ce02aSNathan Whitehorn 	}
153b40ce02aSNathan Whitehorn 	*avail = avail_regions;
154b40ce02aSNathan Whitehorn 
155b40ce02aSNathan Whitehorn 	/* On the bare metal platform phys == avail memory */
156b40ce02aSNathan Whitehorn 	*physsz = *availsz;
157b40ce02aSNathan Whitehorn 	*phys = *avail;
158b40ce02aSNathan Whitehorn }
159b40ce02aSNathan Whitehorn 
160b40ce02aSNathan Whitehorn static u_long
161b40ce02aSNathan Whitehorn bare_timebase_freq(platform_t plat, struct cpuref *cpuref)
162b40ce02aSNathan Whitehorn {
163b40ce02aSNathan Whitehorn 	u_long ticks = -1;
164d1d3233eSRafal Jaworowski 	phandle_t cpus, child;
165d1d3233eSRafal Jaworowski 	pcell_t freq;
166b40ce02aSNathan Whitehorn 
167d1d3233eSRafal Jaworowski 	if ((cpus = OF_finddevice("/cpus")) == 0)
168d1d3233eSRafal Jaworowski 		goto out;
169d1d3233eSRafal Jaworowski 
170d1d3233eSRafal Jaworowski 	if ((child = OF_child(cpus)) == 0)
171d1d3233eSRafal Jaworowski 		goto out;
172d1d3233eSRafal Jaworowski 
173d1d3233eSRafal Jaworowski 	if (OF_getprop(child, "bus-frequency", (void *)&freq,
174d1d3233eSRafal Jaworowski 	    sizeof(freq)) <= 0)
175d1d3233eSRafal Jaworowski 		goto out;
176b40ce02aSNathan Whitehorn 	/*
177b40ce02aSNathan Whitehorn 	 * Time Base and Decrementer are updated every 8 CCB bus clocks.
178b40ce02aSNathan Whitehorn 	 * HID0[SEL_TBCLK] = 0
179b40ce02aSNathan Whitehorn 	 */
180d1d3233eSRafal Jaworowski 	ticks = freq / 8;
181d1d3233eSRafal Jaworowski out:
182b40ce02aSNathan Whitehorn 	if (ticks <= 0)
183b40ce02aSNathan Whitehorn 		panic("Unable to determine timebase frequency!");
184b40ce02aSNathan Whitehorn 
185b40ce02aSNathan Whitehorn 	return (ticks);
186b40ce02aSNathan Whitehorn }
187b40ce02aSNathan Whitehorn 
188b40ce02aSNathan Whitehorn static int
189b40ce02aSNathan Whitehorn bare_smp_first_cpu(platform_t plat, struct cpuref *cpuref)
190b40ce02aSNathan Whitehorn {
191b40ce02aSNathan Whitehorn 
192b40ce02aSNathan Whitehorn 	cpu = 0;
193b40ce02aSNathan Whitehorn 	cpuref->cr_cpuid = cpu;
194b40ce02aSNathan Whitehorn 	cpuref->cr_hwref = cpuref->cr_cpuid;
195b40ce02aSNathan Whitehorn 	if (bootverbose)
196b40ce02aSNathan Whitehorn 		printf("powerpc_smp_first_cpu: cpuid %d\n", cpuref->cr_cpuid);
197b40ce02aSNathan Whitehorn 	cpu++;
198b40ce02aSNathan Whitehorn 
199b40ce02aSNathan Whitehorn 	return (0);
200b40ce02aSNathan Whitehorn }
201b40ce02aSNathan Whitehorn 
202b40ce02aSNathan Whitehorn static int
203b40ce02aSNathan Whitehorn bare_smp_next_cpu(platform_t plat, struct cpuref *cpuref)
204b40ce02aSNathan Whitehorn {
205b40ce02aSNathan Whitehorn 
2062b7b2d79SRafal Jaworowski 	if (cpu >= maxcpu)
207b40ce02aSNathan Whitehorn 		return (ENOENT);
208b40ce02aSNathan Whitehorn 
209b40ce02aSNathan Whitehorn 	cpuref->cr_cpuid = cpu++;
210b40ce02aSNathan Whitehorn 	cpuref->cr_hwref = cpuref->cr_cpuid;
211b40ce02aSNathan Whitehorn 	if (bootverbose)
212b40ce02aSNathan Whitehorn 		printf("powerpc_smp_next_cpu: cpuid %d\n", cpuref->cr_cpuid);
213b40ce02aSNathan Whitehorn 
214b40ce02aSNathan Whitehorn 	return (0);
215b40ce02aSNathan Whitehorn }
216b40ce02aSNathan Whitehorn 
217b40ce02aSNathan Whitehorn static int
218b40ce02aSNathan Whitehorn bare_smp_get_bsp(platform_t plat, struct cpuref *cpuref)
219b40ce02aSNathan Whitehorn {
220b40ce02aSNathan Whitehorn 
221b40ce02aSNathan Whitehorn 	cpuref->cr_cpuid = mfspr(SPR_PIR);
222b40ce02aSNathan Whitehorn 	cpuref->cr_hwref = cpuref->cr_cpuid;
223b40ce02aSNathan Whitehorn 
224b40ce02aSNathan Whitehorn 	return (0);
225b40ce02aSNathan Whitehorn }
226b40ce02aSNathan Whitehorn 
227b40ce02aSNathan Whitehorn static int
228b40ce02aSNathan Whitehorn bare_smp_start_cpu(platform_t plat, struct pcpu *pc)
229b40ce02aSNathan Whitehorn {
23028bb01e5SRafal Jaworowski #ifdef SMP
23128bb01e5SRafal Jaworowski 	uint32_t bptr, eebpcr;
23228bb01e5SRafal Jaworowski 	int timeout;
233b40ce02aSNathan Whitehorn 
23428bb01e5SRafal Jaworowski 	eebpcr = ccsr_read4(OCP85XX_EEBPCR);
23528bb01e5SRafal Jaworowski 	if ((eebpcr & (pc->pc_cpumask << 24)) != 0) {
23628bb01e5SRafal Jaworowski 		printf("%s: CPU=%d already out of hold-off state!\n",
23728bb01e5SRafal Jaworowski 		    __func__, pc->pc_cpuid);
23828bb01e5SRafal Jaworowski 		return (ENXIO);
23928bb01e5SRafal Jaworowski 	}
24028bb01e5SRafal Jaworowski 
24128bb01e5SRafal Jaworowski 	ap_pcpu = pc;
24228bb01e5SRafal Jaworowski 	__asm __volatile("msync; isync");
24328bb01e5SRafal Jaworowski 
24428bb01e5SRafal Jaworowski 	/*
24528bb01e5SRafal Jaworowski 	 * Set BPTR to the physical address of the boot page
24628bb01e5SRafal Jaworowski 	 */
24728bb01e5SRafal Jaworowski 	bptr = ((uint32_t)__boot_page - KERNBASE) + kernload;
24828bb01e5SRafal Jaworowski 	ccsr_write4(OCP85XX_BPTR, (bptr >> 12) | 0x80000000);
24928bb01e5SRafal Jaworowski 
25028bb01e5SRafal Jaworowski 	/*
25128bb01e5SRafal Jaworowski 	 * Release AP from hold-off state
25228bb01e5SRafal Jaworowski 	 */
25328bb01e5SRafal Jaworowski 	eebpcr |= (pc->pc_cpumask << 24);
25428bb01e5SRafal Jaworowski 	ccsr_write4(OCP85XX_EEBPCR, eebpcr);
25528bb01e5SRafal Jaworowski 	__asm __volatile("isync; msync");
25628bb01e5SRafal Jaworowski 
25728bb01e5SRafal Jaworowski 	timeout = 500;
25828bb01e5SRafal Jaworowski 	while (!pc->pc_awake && timeout--)
25928bb01e5SRafal Jaworowski 		DELAY(1000);	/* wait 1ms */
26028bb01e5SRafal Jaworowski 
26128bb01e5SRafal Jaworowski 	return ((pc->pc_awake) ? 0 : EBUSY);
26228bb01e5SRafal Jaworowski #else
263b40ce02aSNathan Whitehorn 	/* No SMP support */
264b40ce02aSNathan Whitehorn 	return (ENXIO);
26528bb01e5SRafal Jaworowski #endif
266b40ce02aSNathan Whitehorn }
267*b2a237beSNathan Whitehorn 
268*b2a237beSNathan Whitehorn static void
269*b2a237beSNathan Whitehorn e500_reset(platform_t plat)
270*b2a237beSNathan Whitehorn {
271*b2a237beSNathan Whitehorn 	uint32_t ver = SVR_VER(mfspr(SPR_SVR));
272*b2a237beSNathan Whitehorn 
273*b2a237beSNathan Whitehorn 	if (ver == SVR_MPC8572E || ver == SVR_MPC8572 ||
274*b2a237beSNathan Whitehorn 	    ver == SVR_MPC8548E || ver == SVR_MPC8548)
275*b2a237beSNathan Whitehorn 		/* Systems with dedicated reset register */
276*b2a237beSNathan Whitehorn 		ccsr_write4(OCP85XX_RSTCR, 2);
277*b2a237beSNathan Whitehorn 	else {
278*b2a237beSNathan Whitehorn 		/* Clear DBCR0, disables debug interrupts and events. */
279*b2a237beSNathan Whitehorn 		mtspr(SPR_DBCR0, 0);
280*b2a237beSNathan Whitehorn 		__asm __volatile("isync");
281*b2a237beSNathan Whitehorn 
282*b2a237beSNathan Whitehorn 		/* Enable Debug Interrupts in MSR. */
283*b2a237beSNathan Whitehorn 		mtmsr(mfmsr() | PSL_DE);
284*b2a237beSNathan Whitehorn 
285*b2a237beSNathan Whitehorn 		/* Enable debug interrupts and issue reset. */
286*b2a237beSNathan Whitehorn 		mtspr(SPR_DBCR0, mfspr(SPR_DBCR0) | DBCR0_IDM |
287*b2a237beSNathan Whitehorn 		    DBCR0_RST_SYSTEM);
288*b2a237beSNathan Whitehorn 	}
289*b2a237beSNathan Whitehorn 
290*b2a237beSNathan Whitehorn 	printf("Reset failed...\n");
291*b2a237beSNathan Whitehorn 	while (1);
292*b2a237beSNathan Whitehorn }
293*b2a237beSNathan Whitehorn 
294