xref: /freebsd/sys/powerpc/booke/platform_bare.c (revision fdafd315)
1b40ce02aSNathan Whitehorn /*-
24d846d26SWarner 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/param.h>
30b40ce02aSNathan Whitehorn #include <sys/systm.h>
31b40ce02aSNathan Whitehorn #include <sys/kernel.h>
32b40ce02aSNathan Whitehorn #include <sys/bus.h>
33b40ce02aSNathan Whitehorn #include <sys/pcpu.h>
34b40ce02aSNathan Whitehorn #include <sys/proc.h>
35b40ce02aSNathan Whitehorn #include <sys/smp.h>
36b40ce02aSNathan Whitehorn 
37d1d3233eSRafal Jaworowski #include <dev/ofw/openfirm.h>
38d1d3233eSRafal Jaworowski 
39302acc2eSNathan Whitehorn #include <machine/platform.h>
40302acc2eSNathan Whitehorn #include <machine/platformvar.h>
41b40ce02aSNathan Whitehorn 
42b40ce02aSNathan Whitehorn #include "platform_if.h"
43b40ce02aSNathan Whitehorn 
44e3d41006SMarcel Moolenaar extern uint32_t *bootinfo;
45e3d41006SMarcel Moolenaar 
46b40ce02aSNathan Whitehorn static int bare_probe(platform_t);
47c1cb22d7SNathan Whitehorn static void bare_mem_regions(platform_t, struct mem_region *phys, int *physsz,
48c1cb22d7SNathan Whitehorn     struct mem_region *avail, int *availsz);
49b40ce02aSNathan Whitehorn static u_long bare_timebase_freq(platform_t, struct cpuref *cpuref);
50b40ce02aSNathan Whitehorn 
51302acc2eSNathan Whitehorn static void bare_reset(platform_t);
52b2a237beSNathan Whitehorn 
53b40ce02aSNathan Whitehorn static platform_method_t bare_methods[] = {
54b40ce02aSNathan Whitehorn 	PLATFORMMETHOD(platform_probe,		bare_probe),
55b40ce02aSNathan Whitehorn 	PLATFORMMETHOD(platform_mem_regions,	bare_mem_regions),
56b40ce02aSNathan Whitehorn 	PLATFORMMETHOD(platform_timebase_freq,	bare_timebase_freq),
57b40ce02aSNathan Whitehorn 
58302acc2eSNathan Whitehorn 	PLATFORMMETHOD(platform_reset,		bare_reset),
59b2a237beSNathan Whitehorn 
60eaba9848SRui Paulo 	PLATFORMMETHOD_END
61b40ce02aSNathan Whitehorn };
62b40ce02aSNathan Whitehorn 
63b40ce02aSNathan Whitehorn static platform_def_t bare_platform = {
64302acc2eSNathan Whitehorn 	"bare",
65b40ce02aSNathan Whitehorn 	bare_methods,
66b40ce02aSNathan Whitehorn 	0
67b40ce02aSNathan Whitehorn };
68b40ce02aSNathan Whitehorn 
69b40ce02aSNathan Whitehorn PLATFORM_DEF(bare_platform);
70b40ce02aSNathan Whitehorn 
71b40ce02aSNathan Whitehorn static int
bare_probe(platform_t plat)72b40ce02aSNathan Whitehorn bare_probe(platform_t plat)
73b40ce02aSNathan Whitehorn {
742b7b2d79SRafal Jaworowski 
75302acc2eSNathan Whitehorn 	if (OF_peer(0) == -1) /* Needs device tree to work */
76302acc2eSNathan Whitehorn 		return (ENXIO);
77d1d3233eSRafal Jaworowski 
78b40ce02aSNathan Whitehorn 	return (BUS_PROBE_GENERIC);
79b40ce02aSNathan Whitehorn }
80b40ce02aSNathan Whitehorn 
81b40ce02aSNathan Whitehorn void
bare_mem_regions(platform_t plat,struct mem_region * phys,int * physsz,struct mem_region * avail,int * availsz)82c1cb22d7SNathan Whitehorn bare_mem_regions(platform_t plat, struct mem_region *phys, int *physsz,
83c1cb22d7SNathan Whitehorn     struct mem_region *avail, int *availsz)
84b40ce02aSNathan Whitehorn {
85b40ce02aSNathan Whitehorn 
86302acc2eSNathan Whitehorn 	ofw_mem_regions(phys, physsz, avail, availsz);
87b40ce02aSNathan Whitehorn }
88b40ce02aSNathan Whitehorn 
89b40ce02aSNathan Whitehorn static u_long
bare_timebase_freq(platform_t plat,struct cpuref * cpuref)90b40ce02aSNathan Whitehorn bare_timebase_freq(platform_t plat, struct cpuref *cpuref)
91b40ce02aSNathan Whitehorn {
92e3d41006SMarcel Moolenaar 	u_long ticks;
93d1d3233eSRafal Jaworowski 	phandle_t cpus, child;
94d1d3233eSRafal Jaworowski 	pcell_t freq;
95b40ce02aSNathan Whitehorn 
965ce36fdbSMarcel Moolenaar 	if (bootinfo != NULL) {
972b5bf115SMarcel Moolenaar 		if (bootinfo[0] == 1) {
98e3d41006SMarcel Moolenaar 			/* Backward compatibility. See 8-STABLE. */
99e3d41006SMarcel Moolenaar 			ticks = bootinfo[3] >> 3;
1002b5bf115SMarcel Moolenaar 		} else {
1015ce36fdbSMarcel Moolenaar 			/* Compatibility with Juniper's loader. */
1022b5bf115SMarcel Moolenaar 			ticks = bootinfo[5] >> 3;
1035ce36fdbSMarcel Moolenaar 		}
1047512c508SMarcel Moolenaar 	} else
1057512c508SMarcel Moolenaar 		ticks = 0;
106e3d41006SMarcel Moolenaar 
10707042befSJayachandran C. 	if ((cpus = OF_finddevice("/cpus")) == -1)
108d1d3233eSRafal Jaworowski 		goto out;
109d1d3233eSRafal Jaworowski 
110d1d3233eSRafal Jaworowski 	if ((child = OF_child(cpus)) == 0)
111d1d3233eSRafal Jaworowski 		goto out;
112d1d3233eSRafal Jaworowski 
1131c02b4c9SNathan Whitehorn 	switch (OF_getproplen(child, "timebase-frequency")) {
1141c02b4c9SNathan Whitehorn 	case 4:
1151c02b4c9SNathan Whitehorn 	{
1161c02b4c9SNathan Whitehorn 		uint32_t tbase;
1171c02b4c9SNathan Whitehorn 		OF_getprop(child, "timebase-frequency", &tbase, sizeof(tbase));
1181c02b4c9SNathan Whitehorn 		ticks = tbase;
1191c02b4c9SNathan Whitehorn 		return (ticks);
1201c02b4c9SNathan Whitehorn 	}
1211c02b4c9SNathan Whitehorn 	case 8:
1221c02b4c9SNathan Whitehorn 	{
1231c02b4c9SNathan Whitehorn 		uint64_t tbase;
1241c02b4c9SNathan Whitehorn 		OF_getprop(child, "timebase-frequency", &tbase, sizeof(tbase));
1251c02b4c9SNathan Whitehorn 		ticks = tbase;
1261c02b4c9SNathan Whitehorn 		return (ticks);
1271c02b4c9SNathan Whitehorn 	}
1281c02b4c9SNathan Whitehorn 	default:
1291c02b4c9SNathan Whitehorn 		break;
1301c02b4c9SNathan Whitehorn 	}
131d26eb2c1SNathan Whitehorn 
132e3d41006SMarcel Moolenaar 	freq = 0;
133d1d3233eSRafal Jaworowski 	if (OF_getprop(child, "bus-frequency", (void *)&freq,
134d1d3233eSRafal Jaworowski 	    sizeof(freq)) <= 0)
135d1d3233eSRafal Jaworowski 		goto out;
136e3d41006SMarcel Moolenaar 
137b40ce02aSNathan Whitehorn 	/*
138b40ce02aSNathan Whitehorn 	 * Time Base and Decrementer are updated every 8 CCB bus clocks.
139b40ce02aSNathan Whitehorn 	 * HID0[SEL_TBCLK] = 0
140b40ce02aSNathan Whitehorn 	 */
141e3d41006SMarcel Moolenaar 	if (freq != 0)
142d1d3233eSRafal Jaworowski 		ticks = freq / 8;
143e3d41006SMarcel Moolenaar 
144d1d3233eSRafal Jaworowski out:
145b40ce02aSNathan Whitehorn 	if (ticks <= 0)
146b40ce02aSNathan Whitehorn 		panic("Unable to determine timebase frequency!");
147b40ce02aSNathan Whitehorn 
148b40ce02aSNathan Whitehorn 	return (ticks);
149b40ce02aSNathan Whitehorn }
150b40ce02aSNathan Whitehorn 
151b2a237beSNathan Whitehorn static void
bare_reset(platform_t plat)152302acc2eSNathan Whitehorn bare_reset(platform_t plat)
153b2a237beSNathan Whitehorn {
154b2a237beSNathan Whitehorn 
155b2a237beSNathan Whitehorn 	printf("Reset failed...\n");
1562f6bd241SRafal Jaworowski 	while (1)
1572f6bd241SRafal Jaworowski 		;
158b2a237beSNathan Whitehorn }
159