1 /* $NetBSD: vrpmu.c,v 1.16.6.3 2004/09/21 13:16:15 skrll Exp $ */
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.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: vrpmu.c,v 1.16.6.3 2004/09/21 13:16:15 skrll Exp $");
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/device.h>
37 #include <machine/bus.h>
38 #include <machine/config_hook.h>
39 #include <machine/debug.h>
41 #include <hpcmips/vr/vripif.h>
42 #include <hpcmips/vr/vrpmuvar.h>
43 #include <hpcmips/vr/vrpmureg.h>
47 #include <hpcmips/vr/bcuvar.h>
48 #include <hpcmips/vr/bcureg.h>
52 #define DEBUG_BOOT 0x1 /* boot time */
53 #define DEBUG_INTR 0x2 /* intr */
54 #define DEBUG_IO 0x4 /* I/O */
55 #ifndef VRPMUDEBUG_CONF
56 #define VRPMUDEBUG_CONF 0
57 #endif /* VRPMUDEBUG_CONF */
58 int vrpmudebug
= VRPMUDEBUG_CONF
;
59 #define DPRINTF(flag, arg) if (vrpmudebug&flag) printf arg;
60 #define DDUMP_INTR2(flag, arg1, arg2) \
61 if (vrpmudebug&flag) vrpmu_dump_intr2(arg1,arg2);
62 #define DDUMP_REGS(flag, arg) if (vrpmudebug&flag) vrpmu_dump_regs(arg);
63 #else /* VRPMUDEBUG */
64 #define DPRINTF(flag, arg)
65 #define DDUMP_INTR2(flag, arg1, arg2)
66 #define DDUMP_REGS(flag, arg)
67 #endif /* VRPMUDEBUG */
69 static int vrpmumatch(struct device
*, struct cfdata
*, void *);
70 static void vrpmuattach(struct device
*, struct device
*, void *);
72 static void vrpmu_write(struct vrpmu_softc
*, int, unsigned short);
73 static unsigned short vrpmu_read(struct vrpmu_softc
*, int);
75 int vrpmu_intr(void *);
76 void vrpmu_dump_intr(void *);
77 void vrpmu_dump_intr2(unsigned int, unsigned int);
78 void vrpmu_dump_regs(void *);
80 CFATTACH_DECL(vrpmu
, sizeof(struct vrpmu_softc
),
81 vrpmumatch
, vrpmuattach
, NULL
, NULL
);
83 struct vrpmu_softc
*this_pmu
;
86 vrpmu_write(struct vrpmu_softc
*sc
, int port
, unsigned short val
)
89 bus_space_write_2(sc
->sc_iot
, sc
->sc_ioh
, port
, val
);
92 static inline unsigned short
93 vrpmu_read(struct vrpmu_softc
*sc
, int port
)
96 return (bus_space_read_2(sc
->sc_iot
, sc
->sc_ioh
, port
));
100 vrpmumatch(struct device
*parent
, struct cfdata
*cf
, void *aux
)
107 vrpmuattach(struct device
*parent
, struct device
*self
, void *aux
)
109 struct vrpmu_softc
*sc
= (struct vrpmu_softc
*)self
;
110 struct vrip_attach_args
*va
= aux
;
113 #endif /* NVRBCU > 0 */
115 bus_space_tag_t iot
= va
->va_iot
;
116 bus_space_handle_t ioh
;
118 if (bus_space_map(iot
, va
->va_addr
, 1, 0, &ioh
)) {
119 printf(": can't map bus space\n");
126 if (!(sc
->sc_handler
=
127 vrip_intr_establish(va
->va_vc
, va
->va_unit
, 0, IPL_TTY
,
129 printf (": can't map interrupt line.\n");
132 if (!vrip_intr_establish(va
->va_vc
, va
->va_unit
, 1, IPL_TTY
,
134 printf (": can't map interrupt line.\n");
139 /* dump current intrrupt states */
141 DDUMP_REGS(DEBUG_BOOT
, sc
);
142 /* clear interrupt status */
143 vrpmu_write(sc
, PMUINT_REG_W
, PMUINT_ALL
);
144 vrpmu_write(sc
, PMUINT2_REG_W
, PMUINT2_ALL
);
146 cpuid
= vrbcu_vrip_getcpuid();
147 if (cpuid
>= BCUREVID_RID_4111
){
148 vrpmu_write(sc
, PMUWAIT_REG_W
, PMUWAIT_DEFAULT
);
156 * dump PMU intr status regs
160 vrpmu_dump_intr(void *arg
)
162 struct vrpmu_softc
*sc
= arg
;
163 unsigned int intstat1
;
164 unsigned int intstat2
;
166 intstat1
= vrpmu_read(sc
, PMUINT_REG_W
);
167 intstat2
= vrpmu_read(sc
, PMUINT2_REG_W
);
168 vrpmu_dump_intr2(intstat1
, intstat2
);
172 * dump PMU intr status regs
175 vrpmu_dump_intr2(unsigned int intstat1
, unsigned int intstat2
)
177 if (intstat1
& PMUINT_GPIO3
)
178 printf("vrpmu: GPIO[3] activation\n");
179 if (intstat1
& PMUINT_GPIO2
)
180 printf("vrpmu: GPIO[2] activation\n");
181 if (intstat1
& PMUINT_GPIO1
)
182 printf("vrpmu: GPIO[1] activation\n");
183 if (intstat1
& PMUINT_GPIO0
)
184 printf("vrpmu: GPIO[0] activation\n");
186 if (intstat1
& PMUINT_RTC
)
187 printf("vrpmu: RTC alarm detected\n");
188 if (intstat1
& PMUINT_BATT
)
189 printf("vrpmu: Battery low during activation\n");
191 if (intstat1
& PMUINT_TIMOUTRST
)
192 printf("vrpmu: HAL timer reset\n");
193 if (intstat1
& PMUINT_RTCRST
)
194 printf("vrpmu: RTC reset detected\n");
195 if (intstat1
& PMUINT_RSTSWRST
)
196 printf("vrpmu: RESET switch detected\n");
197 if (intstat1
& PMUINT_DMSWRST
)
198 printf("vrpmu: Deadman's switch detected\n");
199 if (intstat1
& PMUINT_BATTINTR
)
200 printf("vrpmu: Battery low during normal ops\n");
201 if (intstat1
& PMUINT_POWERSW
)
202 printf("vrpmu: POWER switch detected\n");
204 if (intstat2
& PMUINT_GPIO12
)
205 printf("vrpmu: GPIO[12] activation\n");
206 if (intstat2
& PMUINT_GPIO11
)
207 printf("vrpmu: GPIO[11] activation\n");
208 if (intstat2
& PMUINT_GPIO10
)
209 printf("vrpmu: GPIO[10] activation\n");
210 if (intstat2
& PMUINT_GPIO9
)
211 printf("vrpmu: GPIO[9] activation\n");
219 vrpmu_dump_regs(void *arg
)
221 struct vrpmu_softc
*sc
= arg
;
222 unsigned int intstat1
;
223 unsigned int intstat2
;
228 intstat1
= vrpmu_read(sc
, PMUINT_REG_W
);
229 intstat2
= vrpmu_read(sc
, PMUINT2_REG_W
);
232 reg
= vrpmu_read(sc
, PMUCNT_REG_W
);
233 printf("vrpmu: cnt 0x%x: ", reg
);
235 reg
= vrpmu_read(sc
, PMUCNT2_REG_W
);
236 printf("vrpmu: cnt2 0x%x: ", reg
);
239 cpuid
= vrbcu_vrip_getcpuid();
240 if (cpuid
>= BCUREVID_RID_4111
){
241 reg
= vrpmu_read(sc
, PMUWAIT_REG_W
);
242 printf("vrpmu: wait 0x%x", reg
);
244 if (cpuid
>= BCUREVID_RID_4121
){
245 reg
= vrpmu_read(sc
, PMUDIV_REG_W
);
246 printf(" div 0x%x", reg
);
249 #endif /* NVRBCU > 0 */
253 * PMU interrupt handler.
256 * In the following interrupt routine we should actually DO something
257 * with the knowledge that we've gained. For now we just report it.
260 vrpmu_intr(void *arg
)
262 struct vrpmu_softc
*sc
= arg
;
263 unsigned int intstat1
;
264 unsigned int intstat2
;
266 intstat1
= vrpmu_read(sc
, PMUINT_REG_W
);
267 /* clear interrupt status */
268 vrpmu_write(sc
, PMUINT_REG_W
, intstat1
);
271 intstat2
= vrpmu_read(sc
, PMUINT2_REG_W
);
272 /* clear interrupt status */
273 vrpmu_write(sc
, PMUINT2_REG_W
, intstat2
);
275 DDUMP_INTR2(DEBUG_INTR
, intstat1
, intstat2
);
277 if (intstat1
& PMUINT_GPIO3
)
279 if (intstat1
& PMUINT_GPIO2
)
281 if (intstat1
& PMUINT_GPIO1
)
283 if (intstat1
& PMUINT_GPIO0
)
286 if (intstat1
& PMUINT_RTC
)
288 if (intstat1
& PMUINT_BATT
)
289 config_hook_call(CONFIG_HOOK_PMEVENT
,
290 CONFIG_HOOK_PMEVENT_SUSPENDREQ
, NULL
);
291 if (intstat1
& PMUINT_TIMOUTRST
)
293 if (intstat1
& PMUINT_RTCRST
)
295 if (intstat1
& PMUINT_RSTSWRST
)
297 if (intstat1
& PMUINT_DMSWRST
)
299 if (intstat1
& PMUINT_BATTINTR
)
300 config_hook_call(CONFIG_HOOK_PMEVENT
,
301 CONFIG_HOOK_PMEVENT_SUSPENDREQ
, NULL
);
302 if (intstat1
& PMUINT_POWERSW
) {
304 * you can't detect when the button is released
306 config_hook_call(CONFIG_HOOK_BUTTONEVENT
,
307 CONFIG_HOOK_BUTTONEVENT_POWER
,
309 config_hook_call(CONFIG_HOOK_BUTTONEVENT
,
310 CONFIG_HOOK_BUTTONEVENT_POWER
,
314 if (intstat2
& PMUINT_GPIO12
)
316 if (intstat2
& PMUINT_GPIO11
)
318 if (intstat2
& PMUINT_GPIO10
)
320 if (intstat2
& PMUINT_GPIO9
)