xref: /freebsd/sys/powerpc/booke/platform_bare.c (revision 4d846d260e2b9a3d4d0a701462568268cbfe7a5b)
1b40ce02aSNathan Whitehorn /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
371e3c308SPedro F. Giffuni  *
4925f0a6eSRafal Jaworowski  * Copyright (c) 2008-2012 Semihalf.
5b40ce02aSNathan Whitehorn  * All rights reserved.
6b40ce02aSNathan Whitehorn  *
7b40ce02aSNathan Whitehorn  * Redistribution and use in source and binary forms, with or without
8b40ce02aSNathan Whitehorn  * modification, are permitted provided that the following conditions
9b40ce02aSNathan Whitehorn  * are met:
10b40ce02aSNathan Whitehorn  *
11b40ce02aSNathan Whitehorn  * 1. Redistributions of source code must retain the above copyright
12b40ce02aSNathan Whitehorn  *    notice, this list of conditions and the following disclaimer.
13b40ce02aSNathan Whitehorn  * 2. Redistributions in binary form must reproduce the above copyright
14b40ce02aSNathan Whitehorn  *    notice, this list of conditions and the following disclaimer in the
15b40ce02aSNathan Whitehorn  *    documentation and/or other materials provided with the distribution.
16b40ce02aSNathan Whitehorn  *
17b40ce02aSNathan Whitehorn  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18b40ce02aSNathan Whitehorn  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19b40ce02aSNathan Whitehorn  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20b40ce02aSNathan Whitehorn  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21b40ce02aSNathan Whitehorn  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22b40ce02aSNathan Whitehorn  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23b40ce02aSNathan Whitehorn  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24b40ce02aSNathan Whitehorn  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25b40ce02aSNathan Whitehorn  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26b40ce02aSNathan Whitehorn  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27b40ce02aSNathan Whitehorn  */
28b40ce02aSNathan Whitehorn 
29b40ce02aSNathan Whitehorn #include <sys/cdefs.h>
30b40ce02aSNathan Whitehorn __FBSDID("$FreeBSD$");
31b40ce02aSNathan Whitehorn 
32b40ce02aSNathan Whitehorn #include <sys/param.h>
33b40ce02aSNathan Whitehorn #include <sys/systm.h>
34b40ce02aSNathan Whitehorn #include <sys/kernel.h>
35b40ce02aSNathan Whitehorn #include <sys/bus.h>
36b40ce02aSNathan Whitehorn #include <sys/pcpu.h>
37b40ce02aSNathan Whitehorn #include <sys/proc.h>
38b40ce02aSNathan Whitehorn #include <sys/smp.h>
39b40ce02aSNathan Whitehorn 
40d1d3233eSRafal Jaworowski #include <dev/ofw/openfirm.h>
41d1d3233eSRafal Jaworowski 
42302acc2eSNathan Whitehorn #include <machine/platform.h>
43302acc2eSNathan Whitehorn #include <machine/platformvar.h>
44b40ce02aSNathan Whitehorn 
45b40ce02aSNathan Whitehorn #include "platform_if.h"
46b40ce02aSNathan Whitehorn 
47e3d41006SMarcel Moolenaar extern uint32_t *bootinfo;
48e3d41006SMarcel Moolenaar 
49b40ce02aSNathan Whitehorn static int bare_probe(platform_t);
50c1cb22d7SNathan Whitehorn static void bare_mem_regions(platform_t, struct mem_region *phys, int *physsz,
51c1cb22d7SNathan Whitehorn     struct mem_region *avail, int *availsz);
52b40ce02aSNathan Whitehorn static u_long bare_timebase_freq(platform_t, struct cpuref *cpuref);
53b40ce02aSNathan Whitehorn 
54302acc2eSNathan Whitehorn static void bare_reset(platform_t);
55b2a237beSNathan Whitehorn 
56b40ce02aSNathan Whitehorn static platform_method_t bare_methods[] = {
57b40ce02aSNathan Whitehorn 	PLATFORMMETHOD(platform_probe,		bare_probe),
58b40ce02aSNathan Whitehorn 	PLATFORMMETHOD(platform_mem_regions,	bare_mem_regions),
59b40ce02aSNathan Whitehorn 	PLATFORMMETHOD(platform_timebase_freq,	bare_timebase_freq),
60b40ce02aSNathan Whitehorn 
61302acc2eSNathan Whitehorn 	PLATFORMMETHOD(platform_reset,		bare_reset),
62b2a237beSNathan Whitehorn 
63eaba9848SRui Paulo 	PLATFORMMETHOD_END
64b40ce02aSNathan Whitehorn };
65b40ce02aSNathan Whitehorn 
66b40ce02aSNathan Whitehorn static platform_def_t bare_platform = {
67302acc2eSNathan Whitehorn 	"bare",
68b40ce02aSNathan Whitehorn 	bare_methods,
69b40ce02aSNathan Whitehorn 	0
70b40ce02aSNathan Whitehorn };
71b40ce02aSNathan Whitehorn 
72b40ce02aSNathan Whitehorn PLATFORM_DEF(bare_platform);
73b40ce02aSNathan Whitehorn 
74b40ce02aSNathan Whitehorn static int
75b40ce02aSNathan Whitehorn bare_probe(platform_t plat)
76b40ce02aSNathan Whitehorn {
772b7b2d79SRafal Jaworowski 
78302acc2eSNathan Whitehorn 	if (OF_peer(0) == -1) /* Needs device tree to work */
79302acc2eSNathan Whitehorn 		return (ENXIO);
80d1d3233eSRafal Jaworowski 
81b40ce02aSNathan Whitehorn 	return (BUS_PROBE_GENERIC);
82b40ce02aSNathan Whitehorn }
83b40ce02aSNathan Whitehorn 
84b40ce02aSNathan Whitehorn void
85c1cb22d7SNathan Whitehorn bare_mem_regions(platform_t plat, struct mem_region *phys, int *physsz,
86c1cb22d7SNathan Whitehorn     struct mem_region *avail, int *availsz)
87b40ce02aSNathan Whitehorn {
88b40ce02aSNathan Whitehorn 
89302acc2eSNathan Whitehorn 	ofw_mem_regions(phys, physsz, avail, availsz);
90b40ce02aSNathan Whitehorn }
91b40ce02aSNathan Whitehorn 
92b40ce02aSNathan Whitehorn static u_long
93b40ce02aSNathan Whitehorn bare_timebase_freq(platform_t plat, struct cpuref *cpuref)
94b40ce02aSNathan Whitehorn {
95e3d41006SMarcel Moolenaar 	u_long ticks;
96d1d3233eSRafal Jaworowski 	phandle_t cpus, child;
97d1d3233eSRafal Jaworowski 	pcell_t freq;
98b40ce02aSNathan Whitehorn 
995ce36fdbSMarcel Moolenaar 	if (bootinfo != NULL) {
1002b5bf115SMarcel Moolenaar 		if (bootinfo[0] == 1) {
101e3d41006SMarcel Moolenaar 			/* Backward compatibility. See 8-STABLE. */
102e3d41006SMarcel Moolenaar 			ticks = bootinfo[3] >> 3;
1032b5bf115SMarcel Moolenaar 		} else {
1045ce36fdbSMarcel Moolenaar 			/* Compatibility with Juniper's loader. */
1052b5bf115SMarcel Moolenaar 			ticks = bootinfo[5] >> 3;
1065ce36fdbSMarcel Moolenaar 		}
1077512c508SMarcel Moolenaar 	} else
1087512c508SMarcel Moolenaar 		ticks = 0;
109e3d41006SMarcel Moolenaar 
11007042befSJayachandran C. 	if ((cpus = OF_finddevice("/cpus")) == -1)
111d1d3233eSRafal Jaworowski 		goto out;
112d1d3233eSRafal Jaworowski 
113d1d3233eSRafal Jaworowski 	if ((child = OF_child(cpus)) == 0)
114d1d3233eSRafal Jaworowski 		goto out;
115d1d3233eSRafal Jaworowski 
1161c02b4c9SNathan Whitehorn 	switch (OF_getproplen(child, "timebase-frequency")) {
1171c02b4c9SNathan Whitehorn 	case 4:
1181c02b4c9SNathan Whitehorn 	{
1191c02b4c9SNathan Whitehorn 		uint32_t tbase;
1201c02b4c9SNathan Whitehorn 		OF_getprop(child, "timebase-frequency", &tbase, sizeof(tbase));
1211c02b4c9SNathan Whitehorn 		ticks = tbase;
1221c02b4c9SNathan Whitehorn 		return (ticks);
1231c02b4c9SNathan Whitehorn 	}
1241c02b4c9SNathan Whitehorn 	case 8:
1251c02b4c9SNathan Whitehorn 	{
1261c02b4c9SNathan Whitehorn 		uint64_t tbase;
1271c02b4c9SNathan Whitehorn 		OF_getprop(child, "timebase-frequency", &tbase, sizeof(tbase));
1281c02b4c9SNathan Whitehorn 		ticks = tbase;
1291c02b4c9SNathan Whitehorn 		return (ticks);
1301c02b4c9SNathan Whitehorn 	}
1311c02b4c9SNathan Whitehorn 	default:
1321c02b4c9SNathan Whitehorn 		break;
1331c02b4c9SNathan Whitehorn 	}
134d26eb2c1SNathan Whitehorn 
135e3d41006SMarcel Moolenaar 	freq = 0;
136d1d3233eSRafal Jaworowski 	if (OF_getprop(child, "bus-frequency", (void *)&freq,
137d1d3233eSRafal Jaworowski 	    sizeof(freq)) <= 0)
138d1d3233eSRafal Jaworowski 		goto out;
139e3d41006SMarcel Moolenaar 
140b40ce02aSNathan Whitehorn 	/*
141b40ce02aSNathan Whitehorn 	 * Time Base and Decrementer are updated every 8 CCB bus clocks.
142b40ce02aSNathan Whitehorn 	 * HID0[SEL_TBCLK] = 0
143b40ce02aSNathan Whitehorn 	 */
144e3d41006SMarcel Moolenaar 	if (freq != 0)
145d1d3233eSRafal Jaworowski 		ticks = freq / 8;
146e3d41006SMarcel Moolenaar 
147d1d3233eSRafal Jaworowski out:
148b40ce02aSNathan Whitehorn 	if (ticks <= 0)
149b40ce02aSNathan Whitehorn 		panic("Unable to determine timebase frequency!");
150b40ce02aSNathan Whitehorn 
151b40ce02aSNathan Whitehorn 	return (ticks);
152b40ce02aSNathan Whitehorn }
153b40ce02aSNathan Whitehorn 
154b2a237beSNathan Whitehorn static void
155302acc2eSNathan Whitehorn bare_reset(platform_t plat)
156b2a237beSNathan Whitehorn {
157b2a237beSNathan Whitehorn 
158b2a237beSNathan Whitehorn 	printf("Reset failed...\n");
1592f6bd241SRafal Jaworowski 	while (1)
1602f6bd241SRafal Jaworowski 		;
161b2a237beSNathan Whitehorn }
162