summaryrefslogtreecommitdiffstats
path: root/sys/mips/sibyte/sb_scd.c
blob: c8fec6978a9894eb44a3cae4da2899fee3bb070c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*-
 * Copyright (c) 2009 Neelkanth Natu
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/systm.h>
#include <sys/module.h>
#include <sys/bus.h>

#include <machine/resource.h>
#include <machine/hwfunc.h>

#include "sb_scd.h"

extern void	sb_store64(uint32_t addr, uint64_t val);
extern uint64_t	sb_load64(uint32_t addr);

/*
 * System Control and Debug (SCD) unit on the Sibyte ZBbus.
 */

/*
 * Extract the value starting at bit position 'b' for 'n' bits from 'x'.
 */
#define	GET_VAL_64(x, b, n)	(((x) >> (b)) & ((1ULL << (n)) - 1))

#define	SYSREV_ADDR		MIPS_PHYS_TO_KSEG1(0x10020000)
#define	SYSREV_NUM_PROCESSORS(x) GET_VAL_64((x), 24, 4)

#define	SYSCFG_ADDR		MIPS_PHYS_TO_KSEG1(0x10020008)
#define SYSCFG_PLLDIV(x)	GET_VAL_64((x), 7, 5)

#define	ZBBUS_CYCLE_COUNT_ADDR	MIPS_PHYS_TO_KSEG1(0x10030000)

#define	INTSRC_MASK_ADDR(cpu)	\
	(MIPS_PHYS_TO_KSEG1(0x10020028) | ((cpu) << 13))

#define	INTSRC_MAP_ADDR(cpu, intsrc)	\
	(MIPS_PHYS_TO_KSEG1(0x10020200) | ((cpu) << 13)) + (intsrc * 8)

#define	MAILBOX_SET_ADDR(cpu)	\
	(MIPS_PHYS_TO_KSEG1(0x100200C8) | ((cpu) << 13))

#define	MAILBOX_CLEAR_ADDR(cpu)	\
	(MIPS_PHYS_TO_KSEG1(0x100200D0) | ((cpu) << 13))

static uint64_t
sb_read_syscfg(void)
{

	return (sb_load64(SYSCFG_ADDR));
}

static void
sb_write_syscfg(uint64_t val)
{
	
	sb_store64(SYSCFG_ADDR, val);
}

uint64_t
sb_zbbus_cycle_count(void)
{

	return (sb_load64(ZBBUS_CYCLE_COUNT_ADDR));
}

uint64_t
sb_cpu_speed(void)
{
	int plldiv;
	const uint64_t MHZ = 1000000;
	
	plldiv = SYSCFG_PLLDIV(sb_read_syscfg());
	if (plldiv == 0) {
		printf("PLL_DIV is 0 - assuming 6 (300MHz).\n");
		plldiv = 6;
	}

	return (plldiv * 50 * MHZ);
}

void
sb_system_reset(void)
{
	uint64_t syscfg;

	const uint64_t SYSTEM_RESET = 1ULL << 60;
	const uint64_t EXT_RESET = 1ULL << 59;
	const uint64_t SOFT_RESET = 1ULL << 58;

	syscfg = sb_read_syscfg();
	syscfg &= ~SOFT_RESET;
	syscfg |= SYSTEM_RESET | EXT_RESET;
	sb_write_syscfg(syscfg);
}

void
sb_disable_intsrc(int cpu, int src)
{
	uint32_t regaddr;
	uint64_t val;

	regaddr = INTSRC_MASK_ADDR(cpu);

	val = sb_load64(regaddr);
	val |= 1ULL << src;
	sb_store64(regaddr, val);
}

void
sb_enable_intsrc(int cpu, int src)
{
	uint32_t regaddr;
	uint64_t val;

	regaddr = INTSRC_MASK_ADDR(cpu);

	val = sb_load64(regaddr);
	val &= ~(1ULL << src);
	sb_store64(regaddr, val);
}

void
sb_write_intsrc_mask(int cpu, uint64_t val)
{
	uint32_t regaddr;

	regaddr = INTSRC_MASK_ADDR(cpu);
	sb_store64(regaddr, val);
}

uint64_t
sb_read_intsrc_mask(int cpu)
{
	uint32_t regaddr;
	uint64_t val;

	regaddr = INTSRC_MASK_ADDR(cpu);
	val = sb_load64(regaddr);

	return (val);
}

void
sb_write_intmap(int cpu, int intsrc, int intrnum)
{
	uint32_t regaddr;

	regaddr = INTSRC_MAP_ADDR(cpu, intsrc);
	sb_store64(regaddr, intrnum);
}

int
sb_read_intmap(int cpu, int intsrc)
{
	uint32_t regaddr;

	regaddr = INTSRC_MAP_ADDR(cpu, intsrc);
	return (sb_load64(regaddr) & 0x7);
}

int
sb_route_intsrc(int intsrc)
{
	int intrnum;

	KASSERT(intsrc >= 0 && intsrc < NUM_INTSRC,
		("Invalid interrupt source number (%d)", intsrc));

	/*
	 * Interrupt 5 is used by sources internal to the CPU (e.g. timer).
	 * Use a deterministic mapping for the remaining sources.
	 */
#ifdef SMP
	KASSERT(platform_ipi_intrnum() == 4,
		("Unexpected interrupt number used for IPI"));
	intrnum = intsrc % 4;
#else
	intrnum = intsrc % 5;
#endif

	return (intrnum);
}

#ifdef SMP
static uint64_t
sb_read_sysrev(void)
{

	return (sb_load64(SYSREV_ADDR));
}

void
sb_set_mailbox(int cpu, uint64_t val)
{
	uint32_t regaddr;

	regaddr = MAILBOX_SET_ADDR(cpu);
	sb_store64(regaddr, val);
}

void
sb_clear_mailbox(int cpu, uint64_t val)
{
	uint32_t regaddr;

	regaddr = MAILBOX_CLEAR_ADDR(cpu);
	sb_store64(regaddr, val);
}

int
platform_num_processors(void)
{

	return (SYSREV_NUM_PROCESSORS(sb_read_sysrev()));
}
#endif	/* SMP */

#define	SCD_PHYSADDR	0x10000000
#define	SCD_SIZE	0x00060000

static int
scd_probe(device_t dev)
{

	device_set_desc(dev, "Broadcom/Sibyte System Control and Debug");
	return (0);
}

static int
scd_attach(device_t dev)
{
	int rid;
	struct resource *res;

	if (bootverbose)
		device_printf(dev, "attached.\n");

	rid = 0;
	res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid, SCD_PHYSADDR,
				 SCD_PHYSADDR + SCD_SIZE - 1, SCD_SIZE, 0);
	if (res == NULL)
		panic("Cannot allocate resource for system control and debug.");
	
	return (0);
}

static device_method_t scd_methods[] ={
	/* Device interface */
	DEVMETHOD(device_probe,		scd_probe),
	DEVMETHOD(device_attach,	scd_attach),
	DEVMETHOD(device_detach,	bus_generic_detach),
	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
	DEVMETHOD(device_suspend,	bus_generic_suspend),
	DEVMETHOD(device_resume,	bus_generic_resume),

	{ 0, 0 }
};

static driver_t scd_driver = {
	"scd",
	scd_methods
};

static devclass_t scd_devclass;

DRIVER_MODULE(scd, zbbus, scd_driver, scd_devclass, 0, 0);
OpenPOWER on IntegriCloud