xref: /freebsd/sys/powerpc/booke/platform_bare.c (revision 302acc2e5f017a6b5c25595e98051cbafacfaca3)
1b40ce02aSNathan Whitehorn /*-
2925f0a6eSRafal Jaworowski  * Copyright (c) 2008-2012 Semihalf.
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 
38d1d3233eSRafal Jaworowski #include <dev/ofw/openfirm.h>
39d1d3233eSRafal Jaworowski 
40*302acc2eSNathan Whitehorn #include <machine/platform.h>
41*302acc2eSNathan Whitehorn #include <machine/platformvar.h>
42b40ce02aSNathan Whitehorn 
43b40ce02aSNathan Whitehorn #include "platform_if.h"
44b40ce02aSNathan Whitehorn 
45e3d41006SMarcel Moolenaar extern uint32_t *bootinfo;
46e3d41006SMarcel Moolenaar 
47b40ce02aSNathan Whitehorn static int bare_probe(platform_t);
48b40ce02aSNathan Whitehorn static void bare_mem_regions(platform_t, struct mem_region **phys, int *physsz,
49b40ce02aSNathan Whitehorn     struct mem_region **avail, int *availsz);
50b40ce02aSNathan Whitehorn static u_long bare_timebase_freq(platform_t, struct cpuref *cpuref);
51b40ce02aSNathan Whitehorn 
52*302acc2eSNathan Whitehorn static void bare_reset(platform_t);
53b2a237beSNathan Whitehorn 
54b40ce02aSNathan Whitehorn static platform_method_t bare_methods[] = {
55b40ce02aSNathan Whitehorn 	PLATFORMMETHOD(platform_probe,		bare_probe),
56b40ce02aSNathan Whitehorn 	PLATFORMMETHOD(platform_mem_regions,	bare_mem_regions),
57b40ce02aSNathan Whitehorn 	PLATFORMMETHOD(platform_timebase_freq,	bare_timebase_freq),
58b40ce02aSNathan Whitehorn 
59*302acc2eSNathan Whitehorn 	PLATFORMMETHOD(platform_reset,		bare_reset),
60b2a237beSNathan Whitehorn 
61eaba9848SRui Paulo 	PLATFORMMETHOD_END
62b40ce02aSNathan Whitehorn };
63b40ce02aSNathan Whitehorn 
64b40ce02aSNathan Whitehorn static platform_def_t bare_platform = {
65*302acc2eSNathan Whitehorn 	"bare",
66b40ce02aSNathan Whitehorn 	bare_methods,
67b40ce02aSNathan Whitehorn 	0
68b40ce02aSNathan Whitehorn };
69b40ce02aSNathan Whitehorn 
70b40ce02aSNathan Whitehorn PLATFORM_DEF(bare_platform);
71b40ce02aSNathan Whitehorn 
72b40ce02aSNathan Whitehorn static int
73b40ce02aSNathan Whitehorn bare_probe(platform_t plat)
74b40ce02aSNathan Whitehorn {
752b7b2d79SRafal Jaworowski 
76*302acc2eSNathan Whitehorn 	if (OF_peer(0) == -1) /* Needs device tree to work */
77*302acc2eSNathan Whitehorn 		return (ENXIO);
78d1d3233eSRafal Jaworowski 
79b40ce02aSNathan Whitehorn 	return (BUS_PROBE_GENERIC);
80b40ce02aSNathan Whitehorn }
81b40ce02aSNathan Whitehorn 
82b40ce02aSNathan Whitehorn void
83b40ce02aSNathan Whitehorn bare_mem_regions(platform_t plat, struct mem_region **phys, int *physsz,
84b40ce02aSNathan Whitehorn     struct mem_region **avail, int *availsz)
85b40ce02aSNathan Whitehorn {
86b40ce02aSNathan Whitehorn 
87*302acc2eSNathan Whitehorn 	ofw_mem_regions(phys, physsz, avail, availsz);
88b40ce02aSNathan Whitehorn }
89b40ce02aSNathan Whitehorn 
90b40ce02aSNathan Whitehorn static u_long
91b40ce02aSNathan Whitehorn bare_timebase_freq(platform_t plat, struct cpuref *cpuref)
92b40ce02aSNathan Whitehorn {
93e3d41006SMarcel Moolenaar 	u_long ticks;
94d1d3233eSRafal Jaworowski 	phandle_t cpus, child;
95d1d3233eSRafal Jaworowski 	pcell_t freq;
96b40ce02aSNathan Whitehorn 
975ce36fdbSMarcel Moolenaar 	if (bootinfo != NULL) {
982b5bf115SMarcel Moolenaar 		if (bootinfo[0] == 1) {
99e3d41006SMarcel Moolenaar 			/* Backward compatibility. See 8-STABLE. */
100e3d41006SMarcel Moolenaar 			ticks = bootinfo[3] >> 3;
1012b5bf115SMarcel Moolenaar 		} else {
1025ce36fdbSMarcel Moolenaar 			/* Compatibility with Juniper's loader. */
1032b5bf115SMarcel Moolenaar 			ticks = bootinfo[5] >> 3;
1045ce36fdbSMarcel Moolenaar 		}
1057512c508SMarcel Moolenaar 	} else
1067512c508SMarcel Moolenaar 		ticks = 0;
107e3d41006SMarcel Moolenaar 
10807042befSJayachandran C. 	if ((cpus = OF_finddevice("/cpus")) == -1)
109d1d3233eSRafal Jaworowski 		goto out;
110d1d3233eSRafal Jaworowski 
111d1d3233eSRafal Jaworowski 	if ((child = OF_child(cpus)) == 0)
112d1d3233eSRafal Jaworowski 		goto out;
113d1d3233eSRafal Jaworowski 
1141c02b4c9SNathan Whitehorn 	switch (OF_getproplen(child, "timebase-frequency")) {
1151c02b4c9SNathan Whitehorn 	case 4:
1161c02b4c9SNathan Whitehorn 	{
1171c02b4c9SNathan Whitehorn 		uint32_t tbase;
1181c02b4c9SNathan Whitehorn 		OF_getprop(child, "timebase-frequency", &tbase, sizeof(tbase));
1191c02b4c9SNathan Whitehorn 		ticks = tbase;
1201c02b4c9SNathan Whitehorn 		return (ticks);
1211c02b4c9SNathan Whitehorn 	}
1221c02b4c9SNathan Whitehorn 	case 8:
1231c02b4c9SNathan Whitehorn 	{
1241c02b4c9SNathan Whitehorn 		uint64_t tbase;
1251c02b4c9SNathan Whitehorn 		OF_getprop(child, "timebase-frequency", &tbase, sizeof(tbase));
1261c02b4c9SNathan Whitehorn 		ticks = tbase;
1271c02b4c9SNathan Whitehorn 		return (ticks);
1281c02b4c9SNathan Whitehorn 	}
1291c02b4c9SNathan Whitehorn 	default:
1301c02b4c9SNathan Whitehorn 		break;
1311c02b4c9SNathan Whitehorn 	}
132d26eb2c1SNathan Whitehorn 
133e3d41006SMarcel Moolenaar 	freq = 0;
134d1d3233eSRafal Jaworowski 	if (OF_getprop(child, "bus-frequency", (void *)&freq,
135d1d3233eSRafal Jaworowski 	    sizeof(freq)) <= 0)
136d1d3233eSRafal Jaworowski 		goto out;
137e3d41006SMarcel Moolenaar 
138b40ce02aSNathan Whitehorn 	/*
139b40ce02aSNathan Whitehorn 	 * Time Base and Decrementer are updated every 8 CCB bus clocks.
140b40ce02aSNathan Whitehorn 	 * HID0[SEL_TBCLK] = 0
141b40ce02aSNathan Whitehorn 	 */
142e3d41006SMarcel Moolenaar 	if (freq != 0)
143d1d3233eSRafal Jaworowski 		ticks = freq / 8;
144e3d41006SMarcel Moolenaar 
145d1d3233eSRafal Jaworowski out:
146b40ce02aSNathan Whitehorn 	if (ticks <= 0)
147b40ce02aSNathan Whitehorn 		panic("Unable to determine timebase frequency!");
148b40ce02aSNathan Whitehorn 
149b40ce02aSNathan Whitehorn 	return (ticks);
150b40ce02aSNathan Whitehorn }
151b40ce02aSNathan Whitehorn 
152b2a237beSNathan Whitehorn static void
153*302acc2eSNathan Whitehorn bare_reset(platform_t plat)
154b2a237beSNathan Whitehorn {
155b2a237beSNathan Whitehorn 
156b2a237beSNathan Whitehorn 	printf("Reset failed...\n");
1572f6bd241SRafal Jaworowski 	while (1)
1582f6bd241SRafal Jaworowski 		;
159b2a237beSNathan Whitehorn }
160b2a237beSNathan Whitehorn 
161