sb_scd.c revision 203697
1195333Simp/*-
2195333Simp * Copyright (c) 2009 Neelkanth Natu
3195333Simp * All rights reserved.
4195333Simp *
5195333Simp * Redistribution and use in source and binary forms, with or without
6195333Simp * modification, are permitted provided that the following conditions
7195333Simp * are met:
8195333Simp * 1. Redistributions of source code must retain the above copyright
9195333Simp *    notice, this list of conditions and the following disclaimer.
10195333Simp * 2. Redistributions in binary form must reproduce the above copyright
11195333Simp *    notice, this list of conditions and the following disclaimer in the
12195333Simp *    documentation and/or other materials provided with the distribution.
13195333Simp *
14195333Simp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15195333Simp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16195333Simp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17195333Simp * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18195333Simp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19195333Simp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20195333Simp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21195333Simp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22195333Simp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23195333Simp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24195333Simp * SUCH DAMAGE.
25195333Simp */
26203509Sneel
27203509Sneel#include <sys/cdefs.h>
28203509Sneel__FBSDID("$FreeBSD: head/sys/mips/sibyte/sb_scd.c 203697 2010-02-09 06:24:43Z neel $");
29203509Sneel
30195333Simp#include <sys/param.h>
31195333Simp#include <sys/kernel.h>
32195333Simp#include <sys/systm.h>
33195333Simp#include <sys/module.h>
34195333Simp#include <sys/bus.h>
35195333Simp
36195333Simp#include <machine/resource.h>
37203697Sneel#include <machine/hwfunc.h>
38195333Simp
39195333Simp#include "sb_scd.h"
40195333Simp
41203509Sneelextern void	sb_store64(uint32_t addr, uint64_t val);
42203509Sneelextern uint64_t	sb_load64(uint32_t addr);
43195333Simp
44195333Simp/*
45195333Simp * System Control and Debug (SCD) unit on the Sibyte ZBbus.
46195333Simp */
47195333Simp
48195333Simp/*
49195333Simp * Extract the value starting at bit position 'b' for 'n' bits from 'x'.
50195333Simp */
51195333Simp#define	GET_VAL_64(x, b, n)	(((x) >> (b)) & ((1ULL << (n)) - 1))
52195333Simp
53203509Sneel#define	SYSREV_ADDR		MIPS_PHYS_TO_KSEG1(0x10020000)
54203509Sneel#define	SYSREV_NUM_PROCESSORS(x) GET_VAL_64((x), 24, 4)
55203509Sneel
56203509Sneel#define	SYSCFG_ADDR		MIPS_PHYS_TO_KSEG1(0x10020008)
57195333Simp#define SYSCFG_PLLDIV(x)	GET_VAL_64((x), 7, 5)
58195333Simp
59203509Sneel#define	INTSRC_MASK_ADDR(cpu)	\
60203509Sneel	(MIPS_PHYS_TO_KSEG1(0x10020028) | ((cpu) << 13))
61203509Sneel
62203509Sneel#define	INTSRC_MAP_ADDR(cpu, intsrc)	\
63203509Sneel	(MIPS_PHYS_TO_KSEG1(0x10020200) | ((cpu) << 13)) + (intsrc * 8)
64203509Sneel
65203509Sneel#define	MAILBOX_SET_ADDR(cpu)	\
66203509Sneel	(MIPS_PHYS_TO_KSEG1(0x100200C8) | ((cpu) << 13))
67203509Sneel
68203509Sneel#define	MAILBOX_CLEAR_ADDR(cpu)	\
69203509Sneel	(MIPS_PHYS_TO_KSEG1(0x100200D0) | ((cpu) << 13))
70203509Sneel
71203509Sneelstatic uint64_t
72203509Sneelsb_read_syscfg(void)
73203509Sneel{
74203509Sneel
75203509Sneel	return (sb_load64(SYSCFG_ADDR));
76203509Sneel}
77203509Sneel
78203509Sneelstatic void
79203509Sneelsb_write_syscfg(uint64_t val)
80203509Sneel{
81203509Sneel
82203509Sneel	sb_store64(SYSCFG_ADDR, val);
83203509Sneel}
84203509Sneel
85195333Simpuint64_t
86195333Simpsb_cpu_speed(void)
87195333Simp{
88195333Simp	int plldiv;
89195333Simp	const uint64_t MHZ = 1000000;
90195333Simp
91195333Simp	plldiv = SYSCFG_PLLDIV(sb_read_syscfg());
92195333Simp	if (plldiv == 0) {
93195333Simp		printf("PLL_DIV is 0 - assuming 6 (300MHz).\n");
94195333Simp		plldiv = 6;
95195333Simp	}
96195333Simp
97195333Simp	return (plldiv * 50 * MHZ);
98195333Simp}
99195333Simp
100195333Simpvoid
101195333Simpsb_system_reset(void)
102195333Simp{
103195333Simp	uint64_t syscfg;
104195333Simp
105195333Simp	const uint64_t SYSTEM_RESET = 1ULL << 60;
106195333Simp	const uint64_t EXT_RESET = 1ULL << 59;
107195333Simp	const uint64_t SOFT_RESET = 1ULL << 58;
108195333Simp
109195333Simp	syscfg = sb_read_syscfg();
110195333Simp	syscfg &= ~SOFT_RESET;
111195333Simp	syscfg |= SYSTEM_RESET | EXT_RESET;
112195333Simp	sb_write_syscfg(syscfg);
113195333Simp}
114195333Simp
115203509Sneelvoid
116203509Sneelsb_disable_intsrc(int cpu, int src)
117203509Sneel{
118203509Sneel	uint32_t regaddr;
119203509Sneel	uint64_t val;
120203509Sneel
121203509Sneel	regaddr = INTSRC_MASK_ADDR(cpu);
122203509Sneel
123203509Sneel	val = sb_load64(regaddr);
124203509Sneel	val |= 1ULL << src;
125203509Sneel	sb_store64(regaddr, val);
126203509Sneel}
127203509Sneel
128203509Sneelvoid
129203509Sneelsb_enable_intsrc(int cpu, int src)
130203509Sneel{
131203509Sneel	uint32_t regaddr;
132203509Sneel	uint64_t val;
133203509Sneel
134203509Sneel	regaddr = INTSRC_MASK_ADDR(cpu);
135203509Sneel
136203509Sneel	val = sb_load64(regaddr);
137203509Sneel	val &= ~(1ULL << src);
138203509Sneel	sb_store64(regaddr, val);
139203509Sneel}
140203509Sneel
141203509Sneelvoid
142203509Sneelsb_write_intsrc_mask(int cpu, uint64_t val)
143203509Sneel{
144203509Sneel	uint32_t regaddr;
145203509Sneel
146203509Sneel	regaddr = INTSRC_MASK_ADDR(cpu);
147203509Sneel	sb_store64(regaddr, val);
148203509Sneel}
149203509Sneel
150203509Sneeluint64_t
151203509Sneelsb_read_intsrc_mask(int cpu)
152203509Sneel{
153203509Sneel	uint32_t regaddr;
154203509Sneel	uint64_t val;
155203509Sneel
156203509Sneel	regaddr = INTSRC_MASK_ADDR(cpu);
157203509Sneel	val = sb_load64(regaddr);
158203509Sneel
159203509Sneel	return (val);
160203509Sneel}
161203509Sneel
162203509Sneelvoid
163203509Sneelsb_write_intmap(int cpu, int intsrc, int intrnum)
164203509Sneel{
165203509Sneel	uint32_t regaddr;
166203509Sneel
167203509Sneel	regaddr = INTSRC_MAP_ADDR(cpu, intsrc);
168203509Sneel	sb_store64(regaddr, intrnum);
169203509Sneel}
170203509Sneel
171195333Simpint
172203509Sneelsb_read_intmap(int cpu, int intsrc)
173203509Sneel{
174203509Sneel	uint32_t regaddr;
175203509Sneel
176203509Sneel	regaddr = INTSRC_MAP_ADDR(cpu, intsrc);
177203509Sneel	return (sb_load64(regaddr) & 0x7);
178203509Sneel}
179203509Sneel
180203509Sneelint
181195333Simpsb_route_intsrc(int intsrc)
182195333Simp{
183195333Simp	int intrnum;
184195333Simp
185195333Simp	KASSERT(intsrc >= 0 && intsrc < NUM_INTSRC,
186195333Simp		("Invalid interrupt source number (%d)", intsrc));
187195333Simp
188195333Simp	/*
189195333Simp	 * Interrupt 5 is used by sources internal to the CPU (e.g. timer).
190203509Sneel	 * Use a deterministic mapping for the remaining sources.
191195333Simp	 */
192203697Sneel#ifdef SMP
193203697Sneel	KASSERT(platform_ipi_intrnum() == 4,
194203697Sneel		("Unexpected interrupt number used for IPI"));
195203697Sneel	intrnum = intsrc % 4;
196203697Sneel#else
197195333Simp	intrnum = intsrc % 5;
198203697Sneel#endif
199195333Simp
200195333Simp	return (intrnum);
201195333Simp}
202195333Simp
203203697Sneel#ifdef SMP
204203697Sneelstatic uint64_t
205203697Sneelsb_read_sysrev(void)
206203697Sneel{
207203697Sneel
208203697Sneel	return (sb_load64(SYSREV_ADDR));
209203697Sneel}
210203697Sneel
211203697Sneelvoid
212203697Sneelsb_set_mailbox(int cpu, uint64_t val)
213203697Sneel{
214203697Sneel	uint32_t regaddr;
215203697Sneel
216203697Sneel	regaddr = MAILBOX_SET_ADDR(cpu);
217203697Sneel	sb_store64(regaddr, val);
218203697Sneel}
219203697Sneel
220203697Sneelvoid
221203697Sneelsb_clear_mailbox(int cpu, uint64_t val)
222203697Sneel{
223203697Sneel	uint32_t regaddr;
224203697Sneel
225203697Sneel	regaddr = MAILBOX_CLEAR_ADDR(cpu);
226203697Sneel	sb_store64(regaddr, val);
227203697Sneel}
228203697Sneel
229203697Sneelint
230203697Sneelplatform_num_processors(void)
231203697Sneel{
232203697Sneel
233203697Sneel	return (SYSREV_NUM_PROCESSORS(sb_read_sysrev()));
234203697Sneel}
235203697Sneel#endif	/* SMP */
236203697Sneel
237195333Simp#define	SCD_PHYSADDR	0x10000000
238195333Simp#define	SCD_SIZE	0x00060000
239195333Simp
240195333Simpstatic int
241195333Simpscd_probe(device_t dev)
242195333Simp{
243195333Simp
244195333Simp	device_set_desc(dev, "Broadcom/Sibyte System Control and Debug");
245195333Simp	return (0);
246195333Simp}
247195333Simp
248195333Simpstatic int
249195333Simpscd_attach(device_t dev)
250195333Simp{
251195333Simp	int rid;
252195333Simp	struct resource *res;
253195333Simp
254203509Sneel	if (bootverbose)
255195333Simp		device_printf(dev, "attached.\n");
256195333Simp
257195333Simp	rid = 0;
258195333Simp	res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid, SCD_PHYSADDR,
259195333Simp				 SCD_PHYSADDR + SCD_SIZE - 1, SCD_SIZE, 0);
260203509Sneel	if (res == NULL)
261195333Simp		panic("Cannot allocate resource for system control and debug.");
262195333Simp
263195333Simp	return (0);
264195333Simp}
265195333Simp
266195333Simpstatic device_method_t scd_methods[] ={
267195333Simp	/* Device interface */
268195333Simp	DEVMETHOD(device_probe,		scd_probe),
269195333Simp	DEVMETHOD(device_attach,	scd_attach),
270195333Simp	DEVMETHOD(device_detach,	bus_generic_detach),
271195333Simp	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
272195333Simp	DEVMETHOD(device_suspend,	bus_generic_suspend),
273195333Simp	DEVMETHOD(device_resume,	bus_generic_resume),
274195333Simp
275195333Simp	{ 0, 0 }
276195333Simp};
277195333Simp
278195333Simpstatic driver_t scd_driver = {
279195333Simp	"scd",
280195333Simp	scd_methods
281195333Simp};
282195333Simp
283195333Simpstatic devclass_t scd_devclass;
284195333Simp
285195333SimpDRIVER_MODULE(scd, zbbus, scd_driver, scd_devclass, 0, 0);
286