xref: /freebsd/sys/powerpc/powermac/platform_powermac.c (revision ce3adf4362fcca6a43e500b2531f0038adbfbd21)
1 /*-
2  * Copyright (c) 2008 Marcel Moolenaar
3  * Copyright (c) 2009 Nathan Whitehorn
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
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.
15  *
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.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/bus.h>
35 #include <sys/pcpu.h>
36 #include <sys/proc.h>
37 #include <sys/smp.h>
38 #include <vm/vm.h>
39 #include <vm/pmap.h>
40 
41 #include <machine/bus.h>
42 #include <machine/cpu.h>
43 #include <machine/hid.h>
44 #include <machine/platformvar.h>
45 #include <machine/pmap.h>
46 #include <machine/smp.h>
47 #include <machine/spr.h>
48 
49 #include <dev/ofw/openfirm.h>
50 #include <machine/ofw_machdep.h>
51 
52 #include "platform_if.h"
53 
54 #ifdef SMP
55 extern void *ap_pcpu;
56 #endif
57 
58 static int powermac_probe(platform_t);
59 void powermac_mem_regions(platform_t, struct mem_region **phys, int *physsz,
60     struct mem_region **avail, int *availsz);
61 static u_long powermac_timebase_freq(platform_t, struct cpuref *cpuref);
62 static int powermac_smp_first_cpu(platform_t, struct cpuref *cpuref);
63 static int powermac_smp_next_cpu(platform_t, struct cpuref *cpuref);
64 static int powermac_smp_get_bsp(platform_t, struct cpuref *cpuref);
65 static int powermac_smp_start_cpu(platform_t, struct pcpu *cpu);
66 static void powermac_reset(platform_t);
67 
68 static platform_method_t powermac_methods[] = {
69 	PLATFORMMETHOD(platform_probe, 		powermac_probe),
70 	PLATFORMMETHOD(platform_mem_regions,	powermac_mem_regions),
71 	PLATFORMMETHOD(platform_timebase_freq,	powermac_timebase_freq),
72 
73 	PLATFORMMETHOD(platform_smp_first_cpu,	powermac_smp_first_cpu),
74 	PLATFORMMETHOD(platform_smp_next_cpu,	powermac_smp_next_cpu),
75 	PLATFORMMETHOD(platform_smp_get_bsp,	powermac_smp_get_bsp),
76 	PLATFORMMETHOD(platform_smp_start_cpu,	powermac_smp_start_cpu),
77 
78 	PLATFORMMETHOD(platform_reset,		powermac_reset),
79 
80 	PLATFORMMETHOD_END
81 };
82 
83 static platform_def_t powermac_platform = {
84 	"powermac",
85 	powermac_methods,
86 	0
87 };
88 
89 PLATFORM_DEF(powermac_platform);
90 
91 static int
92 powermac_probe(platform_t plat)
93 {
94 	char compat[255];
95 	ssize_t compatlen;
96 	char *curstr;
97 	phandle_t root;
98 
99 	root = OF_peer(0);
100 	if (root == 0)
101 		return (ENXIO);
102 
103 	compatlen = OF_getprop(root, "compatible", compat, sizeof(compat));
104 
105 	for (curstr = compat; curstr < compat + compatlen;
106 	    curstr += strlen(curstr) + 1) {
107 		if (strncmp(curstr, "MacRISC", 7) == 0)
108 			return (BUS_PROBE_SPECIFIC);
109 	}
110 
111 	return (ENXIO);
112 }
113 
114 void
115 powermac_mem_regions(platform_t plat, struct mem_region **phys, int *physsz,
116     struct mem_region **avail, int *availsz)
117 {
118 	ofw_mem_regions(phys,physsz,avail,availsz);
119 }
120 
121 static u_long
122 powermac_timebase_freq(platform_t plat, struct cpuref *cpuref)
123 {
124 	phandle_t phandle;
125 	int32_t ticks = -1;
126 
127 	phandle = cpuref->cr_hwref;
128 
129 	OF_getprop(phandle, "timebase-frequency", &ticks, sizeof(ticks));
130 
131 	if (ticks <= 0)
132 		panic("Unable to determine timebase frequency!");
133 
134 	return (ticks);
135 }
136 
137 
138 static int
139 powermac_smp_fill_cpuref(struct cpuref *cpuref, phandle_t cpu)
140 {
141 	cell_t cpuid, res;
142 
143 	cpuref->cr_hwref = cpu;
144 	res = OF_getprop(cpu, "reg", &cpuid, sizeof(cpuid));
145 
146 	/*
147 	 * psim doesn't have a reg property, so assume 0 as for the
148 	 * uniprocessor case in the CHRP spec.
149 	 */
150 	if (res < 0) {
151 		cpuid = 0;
152 	}
153 
154 	cpuref->cr_cpuid = cpuid & 0xff;
155 	return (0);
156 }
157 
158 static int
159 powermac_smp_first_cpu(platform_t plat, struct cpuref *cpuref)
160 {
161 	char buf[8];
162 	phandle_t cpu, dev, root;
163 	int res;
164 
165 	root = OF_peer(0);
166 
167 	dev = OF_child(root);
168 	while (dev != 0) {
169 		res = OF_getprop(dev, "name", buf, sizeof(buf));
170 		if (res > 0 && strcmp(buf, "cpus") == 0)
171 			break;
172 		dev = OF_peer(dev);
173 	}
174 	if (dev == 0) {
175 		/*
176 		 * psim doesn't have a name property on the /cpus node,
177 		 * but it can be found directly
178 		 */
179 		dev = OF_finddevice("/cpus");
180 		if (dev == -1)
181 			return (ENOENT);
182 	}
183 
184 	cpu = OF_child(dev);
185 
186 	while (cpu != 0) {
187 		res = OF_getprop(cpu, "device_type", buf, sizeof(buf));
188 		if (res > 0 && strcmp(buf, "cpu") == 0)
189 			break;
190 		cpu = OF_peer(cpu);
191 	}
192 	if (cpu == 0)
193 		return (ENOENT);
194 
195 	return (powermac_smp_fill_cpuref(cpuref, cpu));
196 }
197 
198 static int
199 powermac_smp_next_cpu(platform_t plat, struct cpuref *cpuref)
200 {
201 	char buf[8];
202 	phandle_t cpu;
203 	int res;
204 
205 	cpu = OF_peer(cpuref->cr_hwref);
206 	while (cpu != 0) {
207 		res = OF_getprop(cpu, "device_type", buf, sizeof(buf));
208 		if (res > 0 && strcmp(buf, "cpu") == 0)
209 			break;
210 		cpu = OF_peer(cpu);
211 	}
212 	if (cpu == 0)
213 		return (ENOENT);
214 
215 	return (powermac_smp_fill_cpuref(cpuref, cpu));
216 }
217 
218 static int
219 powermac_smp_get_bsp(platform_t plat, struct cpuref *cpuref)
220 {
221 	ihandle_t inst;
222 	phandle_t bsp, chosen;
223 	int res;
224 
225 	chosen = OF_finddevice("/chosen");
226 	if (chosen == -1)
227 		return (ENXIO);
228 
229 	res = OF_getprop(chosen, "cpu", &inst, sizeof(inst));
230 	if (res < 0)
231 		return (ENXIO);
232 
233 	bsp = OF_instance_to_package(inst);
234 	return (powermac_smp_fill_cpuref(cpuref, bsp));
235 }
236 
237 static int
238 powermac_smp_start_cpu(platform_t plat, struct pcpu *pc)
239 {
240 #ifdef SMP
241 	phandle_t cpu;
242 	volatile uint8_t *rstvec;
243 	static volatile uint8_t *rstvec_virtbase = NULL;
244 	int res, reset, timeout;
245 
246 	cpu = pc->pc_hwref;
247 	res = OF_getprop(cpu, "soft-reset", &reset, sizeof(reset));
248 	if (res < 0) {
249 		reset = 0x58;
250 
251 		switch (pc->pc_cpuid) {
252 		case 0:
253 			reset += 0x03;
254 			break;
255 		case 1:
256 			reset += 0x04;
257 			break;
258 		case 2:
259 			reset += 0x0f;
260 			break;
261 		case 3:
262 			reset += 0x10;
263 			break;
264 		default:
265 			return (ENXIO);
266 		}
267 	}
268 
269 	ap_pcpu = pc;
270 
271 	if (rstvec_virtbase == NULL)
272 		rstvec_virtbase = pmap_mapdev(0x80000000, PAGE_SIZE);
273 
274 	rstvec = rstvec_virtbase + reset;
275 
276 	*rstvec = 4;
277 	powerpc_sync();
278 	(void)(*rstvec);
279 	powerpc_sync();
280 	DELAY(1);
281 	*rstvec = 0;
282 	powerpc_sync();
283 	(void)(*rstvec);
284 	powerpc_sync();
285 
286 	timeout = 10000;
287 	while (!pc->pc_awake && timeout--)
288 		DELAY(100);
289 
290 	return ((pc->pc_awake) ? 0 : EBUSY);
291 #else
292 	/* No SMP support */
293 	return (ENXIO);
294 #endif
295 }
296 
297 static void
298 powermac_reset(platform_t platform)
299 {
300 	OF_reboot();
301 }
302 
303