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