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