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