1/*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2008-2012 Semihalf.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <sys/param.h>
30#include <sys/systm.h>
31#include <sys/kernel.h>
32#include <sys/bus.h>
33#include <sys/pcpu.h>
34#include <sys/proc.h>
35#include <sys/smp.h>
36
37#include <dev/ofw/openfirm.h>
38
39#include <machine/platform.h>
40#include <machine/platformvar.h>
41
42#include "platform_if.h"
43
44extern uint32_t *bootinfo;
45
46static int bare_probe(platform_t);
47static void bare_mem_regions(platform_t, struct mem_region *phys, int *physsz,
48    struct mem_region *avail, int *availsz);
49static u_long bare_timebase_freq(platform_t, struct cpuref *cpuref);
50
51static void bare_reset(platform_t);
52
53static platform_method_t bare_methods[] = {
54	PLATFORMMETHOD(platform_probe,		bare_probe),
55	PLATFORMMETHOD(platform_mem_regions,	bare_mem_regions),
56	PLATFORMMETHOD(platform_timebase_freq,	bare_timebase_freq),
57
58	PLATFORMMETHOD(platform_reset,		bare_reset),
59
60	PLATFORMMETHOD_END
61};
62
63static platform_def_t bare_platform = {
64	"bare",
65	bare_methods,
66	0
67};
68
69PLATFORM_DEF(bare_platform);
70
71static int
72bare_probe(platform_t plat)
73{
74
75	if (OF_peer(0) == -1) /* Needs device tree to work */
76		return (ENXIO);
77
78	return (BUS_PROBE_GENERIC);
79}
80
81void
82bare_mem_regions(platform_t plat, struct mem_region *phys, int *physsz,
83    struct mem_region *avail, int *availsz)
84{
85
86	ofw_mem_regions(phys, physsz, avail, availsz);
87}
88
89static u_long
90bare_timebase_freq(platform_t plat, struct cpuref *cpuref)
91{
92	u_long ticks;
93	phandle_t cpus, child;
94	pcell_t freq;
95
96	if (bootinfo != NULL) {
97		if (bootinfo[0] == 1) {
98			/* Backward compatibility. See 8-STABLE. */
99			ticks = bootinfo[3] >> 3;
100		} else {
101			/* Compatibility with Juniper's loader. */
102			ticks = bootinfo[5] >> 3;
103		}
104	} else
105		ticks = 0;
106
107	if ((cpus = OF_finddevice("/cpus")) == -1)
108		goto out;
109
110	if ((child = OF_child(cpus)) == 0)
111		goto out;
112
113	switch (OF_getproplen(child, "timebase-frequency")) {
114	case 4:
115	{
116		uint32_t tbase;
117		OF_getprop(child, "timebase-frequency", &tbase, sizeof(tbase));
118		ticks = tbase;
119		return (ticks);
120	}
121	case 8:
122	{
123		uint64_t tbase;
124		OF_getprop(child, "timebase-frequency", &tbase, sizeof(tbase));
125		ticks = tbase;
126		return (ticks);
127	}
128	default:
129		break;
130	}
131
132	freq = 0;
133	if (OF_getprop(child, "bus-frequency", (void *)&freq,
134	    sizeof(freq)) <= 0)
135		goto out;
136
137	/*
138	 * Time Base and Decrementer are updated every 8 CCB bus clocks.
139	 * HID0[SEL_TBCLK] = 0
140	 */
141	if (freq != 0)
142		ticks = freq / 8;
143
144out:
145	if (ticks <= 0)
146		panic("Unable to determine timebase frequency!");
147
148	return (ticks);
149}
150
151static void
152bare_reset(platform_t plat)
153{
154
155	printf("Reset failed...\n");
156	while (1)
157		;
158}
159