xref: /netbsd-src/sys/arch/sgimips/dev/ctl.c (revision eb488f671d3f62b03268182548a1bfc622ffc37b)
1*eb488f67Smacallan /*	$NetBSD: ctl.c,v 1.4 2015/02/18 16:47:58 macallan Exp $	 */
2b610ccb0Srumble 
3b610ccb0Srumble /*
4b610ccb0Srumble  * Copyright (c) 2009 Stephen M. Rumble
5b610ccb0Srumble  * All rights reserved.
6b610ccb0Srumble  *
7b610ccb0Srumble  * Redistribution and use in source and binary forms, with or without
8b610ccb0Srumble  * modification, are permitted provided that the following conditions
9b610ccb0Srumble  * are met:
10b610ccb0Srumble  * 1. Redistributions of source code must retain the above copyright
11b610ccb0Srumble  *    notice, this list of conditions and the following disclaimer.
12b610ccb0Srumble  * 2. Redistributions in binary form must reproduce the above copyright
13b610ccb0Srumble  *    notice, this list of conditions and the following disclaimer in the
14b610ccb0Srumble  *    documentation and/or other materials provided with the distribution.
15b610ccb0Srumble  * 3. The name of the author may not be used to endorse or promote products
16b610ccb0Srumble  *    derived from this software without specific prior written permission.
17b610ccb0Srumble  *
18b610ccb0Srumble  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19b610ccb0Srumble  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20b610ccb0Srumble  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21b610ccb0Srumble  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22b610ccb0Srumble  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23b610ccb0Srumble  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24b610ccb0Srumble  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25b610ccb0Srumble  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26b610ccb0Srumble  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27b610ccb0Srumble  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28b610ccb0Srumble  */
29b610ccb0Srumble 
30b610ccb0Srumble #include <sys/cdefs.h>
31*eb488f67Smacallan __KERNEL_RCSID(0, "$NetBSD: ctl.c,v 1.4 2015/02/18 16:47:58 macallan Exp $");
32b610ccb0Srumble 
33b610ccb0Srumble #include <sys/param.h>
34b610ccb0Srumble #include <sys/kernel.h>
35b610ccb0Srumble #include <sys/device.h>
36b610ccb0Srumble #include <sys/systm.h>
37b610ccb0Srumble #include <sys/callout.h>
38b610ccb0Srumble 
39b610ccb0Srumble #include <machine/cpu.h>
40b610ccb0Srumble #include <machine/locore.h>
41b610ccb0Srumble #include <machine/autoconf.h>
42cf10107dSdyoung #include <sys/bus.h>
43b610ccb0Srumble #include <machine/machtype.h>
44b610ccb0Srumble #include <machine/sysconf.h>
45b610ccb0Srumble 
46b610ccb0Srumble #include <sgimips/dev/ctlreg.h>
47b610ccb0Srumble 
48b610ccb0Srumble struct ctl_softc {
4923347d39Smatt 	device_t	   	sc_dev;
50b610ccb0Srumble 
51b610ccb0Srumble 	bus_space_tag_t		iot;
52b610ccb0Srumble 	bus_space_handle_t	ioh;
53b610ccb0Srumble };
54b610ccb0Srumble 
5523347d39Smatt static int      ctl_match(device_t, cfdata_t, void *);
5623347d39Smatt static void     ctl_attach(device_t, device_t, void *);
57b610ccb0Srumble static void	ctl_bus_reset(void);
5823347d39Smatt static void	ctl_bus_error(vaddr_t, uint32_t, uint32_t);
59b610ccb0Srumble static void	ctl_watchdog_enable(void);
60b610ccb0Srumble static void	ctl_watchdog_disable(void);
61b610ccb0Srumble static void	ctl_watchdog_tickle(void);
62b610ccb0Srumble 
63b610ccb0Srumble #if defined(BLINK)
64b610ccb0Srumble static callout_t ctl_blink_ch;
65b610ccb0Srumble static void	ctl_blink(void *);
66b610ccb0Srumble #endif
67b610ccb0Srumble 
6823347d39Smatt CFATTACH_DECL_NEW(ctl, sizeof(struct ctl_softc),
69b610ccb0Srumble     ctl_match, ctl_attach, NULL, NULL);
70b610ccb0Srumble 
7123347d39Smatt static struct ctl_softc *csc;
72b610ccb0Srumble 
73b610ccb0Srumble static int
ctl_match(device_t parent,cfdata_t match,void * aux)7423347d39Smatt ctl_match(device_t parent, cfdata_t match, void *aux)
75b610ccb0Srumble {
7623347d39Smatt 	if (csc != NULL)
7723347d39Smatt 		return 0;
7823347d39Smatt 
79b610ccb0Srumble 	/*
80b610ccb0Srumble 	 * CTL exists on IP6/IP10 systems.
81b610ccb0Srumble 	 */
82b610ccb0Srumble 	if (mach_type == MACH_SGI_IP6 || mach_type == MACH_SGI_IP10)
83b610ccb0Srumble 		return 1;
84b610ccb0Srumble 	else
85b610ccb0Srumble 		return 0;
86b610ccb0Srumble }
87b610ccb0Srumble 
88b610ccb0Srumble static void
ctl_attach(device_t parent,device_t self,void * aux)8923347d39Smatt ctl_attach(device_t parent, device_t self, void *aux)
90b610ccb0Srumble {
91b610ccb0Srumble 	struct mainbus_attach_args *ma = aux;
9223347d39Smatt 	struct ctl_softc * const sc = device_private(self);
93b610ccb0Srumble 
94b610ccb0Srumble #ifdef BLINK
95b610ccb0Srumble 	callout_init(&ctl_blink_ch, 0);
96b610ccb0Srumble #endif
97b610ccb0Srumble 
9823347d39Smatt 	sc->sc_dev = self;
9923347d39Smatt 	csc = sc;
10023347d39Smatt 
101*eb488f67Smacallan 	sc->iot = normal_memt;
102*eb488f67Smacallan 	if (bus_space_map(sc->iot, ma->ma_addr, 0x10000 /* XXX */,
10323347d39Smatt 	    BUS_SPACE_MAP_LINEAR, &sc->ioh))
104b610ccb0Srumble 		panic("ctl_attach: could not allocate memory\n");
105b610ccb0Srumble 
106b610ccb0Srumble 	platform.bus_reset = ctl_bus_reset;
107b610ccb0Srumble 	platform.intr5 = ctl_bus_error;
108b610ccb0Srumble 	platform.watchdog_enable = ctl_watchdog_enable;
109b610ccb0Srumble 	platform.watchdog_disable = ctl_watchdog_disable;
110b610ccb0Srumble 	platform.watchdog_reset = ctl_watchdog_tickle;
111b610ccb0Srumble 
11223347d39Smatt 	bus_space_write_2(sc->iot, sc->ioh, CTL_CPUCTRL,
113b610ccb0Srumble 	    (CTL_CPUCTRL_PARITY | CTL_CPUCTRL_SLAVE));
114b610ccb0Srumble 
115b610ccb0Srumble 	printf("\n");
116b610ccb0Srumble 
117b610ccb0Srumble 	ctl_bus_reset();
118b610ccb0Srumble 
119b610ccb0Srumble #if defined(BLINK)
12023347d39Smatt 	ctl_blink(sc);
121b610ccb0Srumble #endif
122b610ccb0Srumble }
123b610ccb0Srumble 
124b610ccb0Srumble static void
ctl_bus_reset(void)125b610ccb0Srumble ctl_bus_reset(void)
126b610ccb0Srumble {
12723347d39Smatt 	struct ctl_softc * const sc = csc;
128b610ccb0Srumble 
12923347d39Smatt 	bus_space_read_1(sc->iot, sc->ioh, CTL_LAN_PAR_CLR);
13023347d39Smatt 	bus_space_read_1(sc->iot, sc->ioh, CTL_DMA_PAR_CLR);
13123347d39Smatt 	bus_space_read_1(sc->iot, sc->ioh, CTL_CPU_PAR_CLR);
13223347d39Smatt 	bus_space_read_1(sc->iot, sc->ioh, CTL_VME_PAR_CLR);
133b610ccb0Srumble }
134b610ccb0Srumble 
135b610ccb0Srumble static void
ctl_bus_error(vaddr_t pc,uint32_t status,uint32_t ipending)13623347d39Smatt ctl_bus_error(vaddr_t pc, uint32_t status, uint32_t ipending)
137b610ccb0Srumble {
138b610ccb0Srumble 
139b610ccb0Srumble 	printf("ctl0: bus error\n");
140b610ccb0Srumble 	ctl_bus_reset();
141b610ccb0Srumble }
142b610ccb0Srumble 
143b610ccb0Srumble static void
ctl_watchdog_enable(void)144b610ccb0Srumble ctl_watchdog_enable(void)
145b610ccb0Srumble {
14623347d39Smatt 	struct ctl_softc * const sc = csc;
147b610ccb0Srumble 	uint32_t reg;
148b610ccb0Srumble 
149b610ccb0Srumble 	/* XXX- doesn't seem to work properly */
150b610ccb0Srumble 	return;
151b610ccb0Srumble 
15223347d39Smatt 	reg = bus_space_read_2(sc->iot, sc->ioh, CTL_CPUCTRL);
153b610ccb0Srumble 	reg |= CTL_CPUCTRL_WDOG;
15423347d39Smatt 	bus_space_write_2(sc->iot, sc->ioh, CTL_CPUCTRL, reg);
155b610ccb0Srumble }
156b610ccb0Srumble 
157b610ccb0Srumble static void
ctl_watchdog_disable(void)158b610ccb0Srumble ctl_watchdog_disable(void)
159b610ccb0Srumble {
16023347d39Smatt 	struct ctl_softc * const sc = csc;
161b610ccb0Srumble 	uint16_t reg;
162b610ccb0Srumble 
163b610ccb0Srumble 	/* XXX- doesn't seem to work properly */
164b610ccb0Srumble 	return;
165b610ccb0Srumble 
16623347d39Smatt 	reg = bus_space_read_2(sc->iot, sc->ioh, CTL_CPUCTRL_WDOG);
167b610ccb0Srumble 	reg &= ~(CTL_CPUCTRL_WDOG);
16823347d39Smatt 	bus_space_write_2(sc->iot, sc->ioh, CTL_CPUCTRL, reg);
169b610ccb0Srumble }
170b610ccb0Srumble 
171b610ccb0Srumble static void
ctl_watchdog_tickle(void)172b610ccb0Srumble ctl_watchdog_tickle(void)
173b610ccb0Srumble {
174b610ccb0Srumble 
175b610ccb0Srumble 	ctl_watchdog_disable();
176b610ccb0Srumble 	ctl_watchdog_enable();
177b610ccb0Srumble }
178b610ccb0Srumble 
179b610ccb0Srumble #if defined(BLINK)
180b610ccb0Srumble static void
ctl_blink(void * arg)18123347d39Smatt ctl_blink(void *arg)
182b610ccb0Srumble {
18323347d39Smatt 	struct ctl_softc *sc = arg;
184b610ccb0Srumble 	int s, value;
185b610ccb0Srumble 
186b610ccb0Srumble 	s = splhigh();
187b610ccb0Srumble 
188b610ccb0Srumble 	value = bus_space_read_1(sc->iot, sc->ioh, CTL_AUX_CPUCTRL);
189b610ccb0Srumble 	value ^= CTL_AUX_CPUCTRL_CONSLED;
190b610ccb0Srumble 	bus_space_write_1(sc->iot, sc->ioh, CTL_AUX_CPUCTRL, value);
191b610ccb0Srumble 	splx(s);
192b610ccb0Srumble 
193b610ccb0Srumble 	/*
194b610ccb0Srumble 	 * Blink rate is:
195b610ccb0Srumble 	 *      full cycle every second if completely idle (loadav = 0)
196b610ccb0Srumble 	 *      full cycle every 2 seconds if loadav = 1
197b610ccb0Srumble 	 *      full cycle every 3 seconds if loadav = 2
198b610ccb0Srumble 	 * etc.
199b610ccb0Srumble 	 */
20023347d39Smatt 	int ticks = ((averunnable.ldavg[0] + FSCALE) * hz) >> (FSHIFT + 1);
20123347d39Smatt 	callout_reset(&ctl_blink_ch, ticks, ctl_blink, sc);
202b610ccb0Srumble }
203b610ccb0Srumble #endif
204