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