vrpmu.c revision 1.8 1 /* $NetBSD: vrpmu.c,v 1.8 2000/07/02 10:01:31 takemura Exp $ */
2
3 /*
4 * Copyright (c) 1999 M. Warner Losh. All rights reserved.
5 * Copyright (c) 1999 PocketBSD Project. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/device.h>
32
33 #include <machine/bus.h>
34 #include <machine/config_hook.h>
35
36 #include <hpcmips/vr/vripvar.h>
37 #include <hpcmips/vr/vrpmuvar.h>
38 #include <hpcmips/vr/vrpmureg.h>
39
40 #include "vrbcu.h"
41 #if NVRBCU > 0
42 #include <hpcmips/vr/bcuvar.h>
43 #include <hpcmips/vr/bcureg.h>
44 #endif
45
46 #ifdef VRPMUDEBUG
47 #define DEBUG_BOOT 0x1 /* boot time */
48 #define DEBUG_INTR 0x2 /* intr */
49 #define DEBUG_IO 0x4 /* I/O */
50 #ifndef VRPMUDEBUG_CONF
51 #define VRPMUDEBUG_CONF 0
52 #endif /* VRPMUDEBUG_CONF */
53 int vrpmudebug = VRPMUDEBUG_CONF;
54 #define DPRINTF(flag, arg) if (vrpmudebug&flag) printf arg;
55 #define DDUMP_INTR2(flag, arg1, arg2) if (vrpmudebug&flag) vrpmu_dump_intr2(arg1,arg2);
56 #define DDUMP_REGS(flag, arg) if (vrpmudebug&flag) vrpmu_dump_regs(arg);
57 #else /* VRPMUDEBUG */
58 #define DPRINTF(flag, arg)
59 #define DDUMP_INTR2(flag, arg1, arg2)
60 #define DDUMP_REGS(flag, arg)
61 #endif /* VRPMUDEBUG */
62
63 static int vrpmumatch __P((struct device *, struct cfdata *, void *));
64 static void vrpmuattach __P((struct device *, struct device *, void *));
65
66 static void vrpmu_write __P((struct vrpmu_softc *, int, unsigned short));
67 static unsigned short vrpmu_read __P((struct vrpmu_softc *, int));
68
69 int vrpmu_intr __P((void *));
70 void vrpmu_dump_intr __P((void *));
71 void vrpmu_dump_intr2 __P((unsigned int, unsigned int));
72 void vrpmu_dump_regs __P((void *));
73
74 struct cfattach vrpmu_ca = {
75 sizeof(struct vrpmu_softc), vrpmumatch, vrpmuattach
76 };
77
78 static inline void
79 vrpmu_write(sc, port, val)
80 struct vrpmu_softc *sc;
81 int port;
82 unsigned short val;
83 {
84 bus_space_write_2(sc->sc_iot, sc->sc_ioh, port, val);
85 }
86
87 static inline unsigned short
88 vrpmu_read(sc, port)
89 struct vrpmu_softc *sc;
90 int port;
91 {
92 return bus_space_read_2(sc->sc_iot, sc->sc_ioh, port);
93 }
94
95 static int
96 vrpmumatch(parent, cf, aux)
97 struct device *parent;
98 struct cfdata *cf;
99 void *aux;
100 {
101 return 1;
102 }
103
104 static void
105 vrpmuattach(parent, self, aux)
106 struct device *parent;
107 struct device *self;
108 void *aux;
109 {
110 struct vrpmu_softc *sc = (struct vrpmu_softc *)self;
111 struct vrip_attach_args *va = aux;
112
113 bus_space_tag_t iot = va->va_iot;
114 bus_space_handle_t ioh;
115
116 if (bus_space_map(iot, va->va_addr, 1, 0, &ioh)) {
117 printf(": can't map bus space\n");
118 return;
119 }
120
121 sc->sc_iot = iot;
122 sc->sc_ioh = ioh;
123
124 if (!(sc->sc_handler =
125 vrip_intr_establish(va->va_vc, va->va_intr, IPL_TTY,
126 vrpmu_intr, sc))) {
127 printf (": can't map interrupt line.\n");
128 return;
129 }
130
131 printf("\n");
132 /* dump current intrrupt states */
133 vrpmu_dump_intr(sc);
134 DDUMP_REGS(DEBUG_BOOT, sc);
135 /* clear interrupt status */
136 vrpmu_write(sc, PMUINT_REG_W, PMUINT_ALL);
137 vrpmu_write(sc, PMUINT2_REG_W, PMUINT2_ALL);
138 }
139
140 /*
141 * dump PMU intr status regs
142 *
143 */
144 void
145 vrpmu_dump_intr(arg)
146 void *arg;
147 {
148 struct vrpmu_softc *sc = arg;
149 unsigned int intstat1;
150 unsigned int intstat2;
151 intstat1 = vrpmu_read(sc, PMUINT_REG_W);
152 intstat2 = vrpmu_read(sc, PMUINT2_REG_W);
153 vrpmu_dump_intr2(intstat1, intstat2);
154
155 }
156
157 /*
158 * dump PMU intr status regs
159 */
160 void
161 vrpmu_dump_intr2(intstat1, intstat2)
162 unsigned int intstat1, intstat2;
163 {
164 if (intstat1 & PMUINT_GPIO3)
165 printf("vrpmu: GPIO[3] activation\n");
166 if (intstat1 & PMUINT_GPIO2)
167 printf("vrpmu: GPIO[2] activation\n");
168 if (intstat1 & PMUINT_GPIO1)
169 printf("vrpmu: GPIO[1] activation\n");
170 if (intstat1 & PMUINT_GPIO0)
171 printf("vrpmu: GPIO[0] activation\n");
172
173 if (intstat1 & PMUINT_RTC)
174 printf("vrpmu: RTC alarm detected\n");
175 if (intstat1 & PMUINT_BATT)
176 printf("vrpmu: Battery low during activation\n");
177
178 if (intstat1 & PMUINT_TIMOUTRST)
179 printf("vrpmu: HAL timer reset\n");
180 if (intstat1 & PMUINT_RTCRST)
181 printf("vrpmu: RTC reset detected\n");
182 if (intstat1 & PMUINT_RSTSWRST)
183 printf("vrpmu: RESET switch detected\n");
184 if (intstat1 & PMUINT_DMSWRST)
185 printf("vrpmu: Deadman's switch detected\n");
186 if (intstat1 & PMUINT_BATTINTR)
187 printf("vrpmu: Battery low during normal ops\n");
188 if (intstat1 & PMUINT_POWERSW)
189 printf("vrpmu: POWER switch detected\n");
190
191 if (intstat2 & PMUINT_GPIO12)
192 printf("vrpmu: GPIO[12] activation\n");
193 if (intstat2 & PMUINT_GPIO11)
194 printf("vrpmu: GPIO[11] activation\n");
195 if (intstat2 & PMUINT_GPIO10)
196 printf("vrpmu: GPIO[10] activation\n");
197 if (intstat2 & PMUINT_GPIO9)
198 printf("vrpmu: GPIO[9] activation\n");
199 }
200
201 /*
202 * dump PMU registers
203 *
204 */
205 void
206 vrpmu_dump_regs(arg)
207 void *arg;
208 {
209 struct vrpmu_softc *sc = arg;
210 unsigned int intstat1;
211 unsigned int intstat2;
212 unsigned int reg;
213 #if NVRBCU > 0
214 int cpuid;
215 #endif
216 intstat1 = vrpmu_read(sc, PMUINT_REG_W);
217 intstat2 = vrpmu_read(sc, PMUINT2_REG_W);
218
219 /* others? XXXX */
220 reg = vrpmu_read(sc, PMUCNT_REG_W);
221 printf("vrpmu: cnt 0x%x: ", reg);
222 bitdisp16(reg);
223 reg = vrpmu_read(sc, PMUCNT2_REG_W);
224 printf("vrpmu: cnt2 0x%x: ", reg);
225 bitdisp16(reg);
226 #if NVRBCU > 0
227 cpuid = vrbcu_vrip_getcpuid();
228 if (cpuid >= BCUREVID_RID_4111){
229 reg = vrpmu_read(sc, PMUWAIT_REG_W);
230 printf("vrpmu: wait 0x%x", reg);
231 }
232 if (cpuid >= BCUREVID_RID_4121){
233 reg = vrpmu_read(sc, PMUDIV_REG_W);
234 printf(" div 0x%x", reg);
235 }
236 printf("\n");
237 #endif /* NVRBCU > 0 */
238 }
239
240 /*
241 * PMU interrupt handler.
242 * XXX
243 *
244 * In the following interrupt routine we should actually DO something
245 * with the knowledge that we've gained. For now we just report it.
246 */
247 int
248 vrpmu_intr(arg)
249 void *arg;
250 {
251 struct vrpmu_softc *sc = arg;
252 unsigned int intstat1;
253 unsigned int intstat2;
254
255 intstat1 = vrpmu_read(sc, PMUINT_REG_W);
256 /* clear interrupt status */
257 vrpmu_write(sc, PMUINT_REG_W, intstat1);
258
259
260 intstat2 = vrpmu_read(sc, PMUINT2_REG_W);
261 /* clear interrupt status */
262 vrpmu_write(sc, PMUINT2_REG_W, intstat2);
263
264 DDUMP_INTR2(DEBUG_INTR, intstat1, intstat2);
265
266 if (intstat1 & PMUINT_GPIO3)
267 ;
268 if (intstat1 & PMUINT_GPIO2)
269 ;
270 if (intstat1 & PMUINT_GPIO1)
271 ;
272 if (intstat1 & PMUINT_GPIO0)
273 ;
274
275 if (intstat1 & PMUINT_RTC)
276 ;
277 if (intstat1 & PMUINT_BATT)
278 ;
279
280 if (intstat1 & PMUINT_TIMOUTRST)
281 ;
282 if (intstat1 & PMUINT_RTCRST)
283 ;
284 if (intstat1 & PMUINT_RSTSWRST)
285 ;
286 if (intstat1 & PMUINT_DMSWRST)
287 ;
288 if (intstat1 & PMUINT_BATTINTR)
289 ;
290 if (intstat1 & PMUINT_POWERSW) {
291 /*
292 * you can't detect when the button is released
293 */
294 config_hook_call(CONFIG_HOOK_BUTTONEVENT,
295 CONFIG_HOOK_BUTTONEVENT_POWER,
296 (void*)1 /* on */);
297 config_hook_call(CONFIG_HOOK_BUTTONEVENT,
298 CONFIG_HOOK_BUTTONEVENT_POWER,
299 (void*)0 /* off */);
300 }
301
302 if (intstat2 & PMUINT_GPIO12)
303 ;
304 if (intstat2 & PMUINT_GPIO11)
305 ;
306 if (intstat2 & PMUINT_GPIO10)
307 ;
308 if (intstat2 & PMUINT_GPIO9)
309 ;
310
311 return 0;
312 }
313