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