1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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/cdefs.h>
30__FBSDID("$FreeBSD$");
31
32#include <sys/param.h>
33#include <sys/systm.h>
34#include <sys/kernel.h>
35#include <sys/bus.h>
36#include <sys/pcpu.h>
37#include <sys/proc.h>
38#include <sys/smp.h>
39
40#include <dev/ofw/openfirm.h>
41
42#include <machine/platform.h>
43#include <machine/platformvar.h>
44
45#include "platform_if.h"
46
47extern uint32_t *bootinfo;
48
49static int bare_probe(platform_t);
50static void bare_mem_regions(platform_t, struct mem_region *phys, int *physsz,
51    struct mem_region *avail, int *availsz);
52static u_long bare_timebase_freq(platform_t, struct cpuref *cpuref);
53
54static void bare_reset(platform_t);
55
56static platform_method_t bare_methods[] = {
57	PLATFORMMETHOD(platform_probe,		bare_probe),
58	PLATFORMMETHOD(platform_mem_regions,	bare_mem_regions),
59	PLATFORMMETHOD(platform_timebase_freq,	bare_timebase_freq),
60
61	PLATFORMMETHOD(platform_reset,		bare_reset),
62
63	PLATFORMMETHOD_END
64};
65
66static platform_def_t bare_platform = {
67	"bare",
68	bare_methods,
69	0
70};
71
72PLATFORM_DEF(bare_platform);
73
74static int
75bare_probe(platform_t plat)
76{
77
78	if (OF_peer(0) == -1) /* Needs device tree to work */
79		return (ENXIO);
80
81	return (BUS_PROBE_GENERIC);
82}
83
84void
85bare_mem_regions(platform_t plat, struct mem_region *phys, int *physsz,
86    struct mem_region *avail, int *availsz)
87{
88
89	ofw_mem_regions(phys, physsz, avail, availsz);
90}
91
92static u_long
93bare_timebase_freq(platform_t plat, struct cpuref *cpuref)
94{
95	u_long ticks;
96	phandle_t cpus, child;
97	pcell_t freq;
98
99	if (bootinfo != NULL) {
100		if (bootinfo[0] == 1) {
101			/* Backward compatibility. See 8-STABLE. */
102			ticks = bootinfo[3] >> 3;
103		} else {
104			/* Compatibility with Juniper's loader. */
105			ticks = bootinfo[5] >> 3;
106		}
107	} else
108		ticks = 0;
109
110	if ((cpus = OF_finddevice("/cpus")) == -1)
111		goto out;
112
113	if ((child = OF_child(cpus)) == 0)
114		goto out;
115
116	switch (OF_getproplen(child, "timebase-frequency")) {
117	case 4:
118	{
119		uint32_t tbase;
120		OF_getprop(child, "timebase-frequency", &tbase, sizeof(tbase));
121		ticks = tbase;
122		return (ticks);
123	}
124	case 8:
125	{
126		uint64_t tbase;
127		OF_getprop(child, "timebase-frequency", &tbase, sizeof(tbase));
128		ticks = tbase;
129		return (ticks);
130	}
131	default:
132		break;
133	}
134
135	freq = 0;
136	if (OF_getprop(child, "bus-frequency", (void *)&freq,
137	    sizeof(freq)) <= 0)
138		goto out;
139
140	/*
141	 * Time Base and Decrementer are updated every 8 CCB bus clocks.
142	 * HID0[SEL_TBCLK] = 0
143	 */
144	if (freq != 0)
145		ticks = freq / 8;
146
147out:
148	if (ticks <= 0)
149		panic("Unable to determine timebase frequency!");
150
151	return (ticks);
152}
153
154static void
155bare_reset(platform_t plat)
156{
157
158	printf("Reset failed...\n");
159	while (1)
160		;
161}
162