vrpmu.c revision 1.11.4.3 1 1.11.4.3 nathanw /* $NetBSD: vrpmu.c,v 1.11.4.3 2002/10/18 02:37:21 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.3 nathanw CFATTACH_DECL(vrpmu, sizeof(struct vrpmu_softc),
78 1.11.4.3 nathanw vrpmumatch, vrpmuattach, NULL, NULL);
79 1.11.4.2 nathanw
80 1.11.4.2 nathanw struct vrpmu_softc *this_pmu;
81 1.11.4.2 nathanw
82 1.11.4.2 nathanw static inline void
83 1.11.4.2 nathanw vrpmu_write(struct vrpmu_softc *sc, int port, unsigned short val)
84 1.11.4.2 nathanw {
85 1.11.4.2 nathanw
86 1.11.4.2 nathanw bus_space_write_2(sc->sc_iot, sc->sc_ioh, port, val);
87 1.11.4.2 nathanw }
88 1.11.4.2 nathanw
89 1.11.4.2 nathanw static inline unsigned short
90 1.11.4.2 nathanw vrpmu_read(struct vrpmu_softc *sc, int port)
91 1.11.4.2 nathanw {
92 1.11.4.2 nathanw
93 1.11.4.2 nathanw return (bus_space_read_2(sc->sc_iot, sc->sc_ioh, port));
94 1.11.4.2 nathanw }
95 1.11.4.2 nathanw
96 1.11.4.2 nathanw static int
97 1.11.4.2 nathanw vrpmumatch(struct device *parent, struct cfdata *cf, void *aux)
98 1.11.4.2 nathanw {
99 1.11.4.2 nathanw
100 1.11.4.2 nathanw return (1);
101 1.11.4.2 nathanw }
102 1.11.4.2 nathanw
103 1.11.4.2 nathanw static void
104 1.11.4.2 nathanw vrpmuattach(struct device *parent, struct device *self, void *aux)
105 1.11.4.2 nathanw {
106 1.11.4.2 nathanw struct vrpmu_softc *sc = (struct vrpmu_softc *)self;
107 1.11.4.2 nathanw struct vrip_attach_args *va = aux;
108 1.11.4.2 nathanw #if NVRBCU > 0
109 1.11.4.2 nathanw int cpuid;
110 1.11.4.2 nathanw #endif /* NVRBCU > 0 */
111 1.11.4.2 nathanw
112 1.11.4.2 nathanw bus_space_tag_t iot = va->va_iot;
113 1.11.4.2 nathanw bus_space_handle_t ioh;
114 1.11.4.2 nathanw
115 1.11.4.2 nathanw if (bus_space_map(iot, va->va_addr, 1, 0, &ioh)) {
116 1.11.4.2 nathanw printf(": can't map bus space\n");
117 1.11.4.2 nathanw return;
118 1.11.4.2 nathanw }
119 1.11.4.2 nathanw
120 1.11.4.2 nathanw sc->sc_iot = iot;
121 1.11.4.2 nathanw sc->sc_ioh = ioh;
122 1.11.4.2 nathanw
123 1.11.4.2 nathanw if (!(sc->sc_handler =
124 1.11.4.2 nathanw vrip_intr_establish(va->va_vc, va->va_unit, 0, IPL_TTY,
125 1.11.4.2 nathanw vrpmu_intr, sc))) {
126 1.11.4.2 nathanw printf (": can't map interrupt line.\n");
127 1.11.4.2 nathanw return;
128 1.11.4.2 nathanw }
129 1.11.4.2 nathanw if (!vrip_intr_establish(va->va_vc, va->va_unit, 1, IPL_TTY,
130 1.11.4.2 nathanw vrpmu_intr, sc)) {
131 1.11.4.2 nathanw printf (": can't map interrupt line.\n");
132 1.11.4.2 nathanw return;
133 1.11.4.2 nathanw }
134 1.11.4.2 nathanw
135 1.11.4.2 nathanw printf("\n");
136 1.11.4.2 nathanw /* dump current intrrupt states */
137 1.11.4.2 nathanw vrpmu_dump_intr(sc);
138 1.11.4.2 nathanw DDUMP_REGS(DEBUG_BOOT, sc);
139 1.11.4.2 nathanw /* clear interrupt status */
140 1.11.4.2 nathanw vrpmu_write(sc, PMUINT_REG_W, PMUINT_ALL);
141 1.11.4.2 nathanw vrpmu_write(sc, PMUINT2_REG_W, PMUINT2_ALL);
142 1.11.4.2 nathanw #if NVRBCU > 0
143 1.11.4.2 nathanw cpuid = vrbcu_vrip_getcpuid();
144 1.11.4.2 nathanw if (cpuid >= BCUREVID_RID_4111){
145 1.11.4.2 nathanw vrpmu_write(sc, PMUWAIT_REG_W, PMUWAIT_DEFAULT);
146 1.11.4.2 nathanw }
147 1.11.4.2 nathanw #endif /* NVRBCU */
148 1.11.4.2 nathanw
149 1.11.4.2 nathanw this_pmu = sc;
150 1.11.4.2 nathanw }
151 1.11.4.2 nathanw
152 1.11.4.2 nathanw /*
153 1.11.4.2 nathanw * dump PMU intr status regs
154 1.11.4.2 nathanw *
155 1.11.4.2 nathanw */
156 1.11.4.2 nathanw void
157 1.11.4.2 nathanw vrpmu_dump_intr(void *arg)
158 1.11.4.2 nathanw {
159 1.11.4.2 nathanw struct vrpmu_softc *sc = arg;
160 1.11.4.2 nathanw unsigned int intstat1;
161 1.11.4.2 nathanw unsigned int intstat2;
162 1.11.4.2 nathanw
163 1.11.4.2 nathanw intstat1 = vrpmu_read(sc, PMUINT_REG_W);
164 1.11.4.2 nathanw intstat2 = vrpmu_read(sc, PMUINT2_REG_W);
165 1.11.4.2 nathanw vrpmu_dump_intr2(intstat1, intstat2);
166 1.11.4.2 nathanw }
167 1.11.4.2 nathanw
168 1.11.4.2 nathanw /*
169 1.11.4.2 nathanw * dump PMU intr status regs
170 1.11.4.2 nathanw */
171 1.11.4.2 nathanw void
172 1.11.4.2 nathanw vrpmu_dump_intr2(unsigned int intstat1, unsigned int intstat2)
173 1.11.4.2 nathanw {
174 1.11.4.2 nathanw if (intstat1 & PMUINT_GPIO3)
175 1.11.4.2 nathanw printf("vrpmu: GPIO[3] activation\n");
176 1.11.4.2 nathanw if (intstat1 & PMUINT_GPIO2)
177 1.11.4.2 nathanw printf("vrpmu: GPIO[2] activation\n");
178 1.11.4.2 nathanw if (intstat1 & PMUINT_GPIO1)
179 1.11.4.2 nathanw printf("vrpmu: GPIO[1] activation\n");
180 1.11.4.2 nathanw if (intstat1 & PMUINT_GPIO0)
181 1.11.4.2 nathanw printf("vrpmu: GPIO[0] activation\n");
182 1.11.4.2 nathanw
183 1.11.4.2 nathanw if (intstat1 & PMUINT_RTC)
184 1.11.4.2 nathanw printf("vrpmu: RTC alarm detected\n");
185 1.11.4.2 nathanw if (intstat1 & PMUINT_BATT)
186 1.11.4.2 nathanw printf("vrpmu: Battery low during activation\n");
187 1.11.4.2 nathanw
188 1.11.4.2 nathanw if (intstat1 & PMUINT_TIMOUTRST)
189 1.11.4.2 nathanw printf("vrpmu: HAL timer reset\n");
190 1.11.4.2 nathanw if (intstat1 & PMUINT_RTCRST)
191 1.11.4.2 nathanw printf("vrpmu: RTC reset detected\n");
192 1.11.4.2 nathanw if (intstat1 & PMUINT_RSTSWRST)
193 1.11.4.2 nathanw printf("vrpmu: RESET switch detected\n");
194 1.11.4.2 nathanw if (intstat1 & PMUINT_DMSWRST)
195 1.11.4.2 nathanw printf("vrpmu: Deadman's switch detected\n");
196 1.11.4.2 nathanw if (intstat1 & PMUINT_BATTINTR)
197 1.11.4.2 nathanw printf("vrpmu: Battery low during normal ops\n");
198 1.11.4.2 nathanw if (intstat1 & PMUINT_POWERSW)
199 1.11.4.2 nathanw printf("vrpmu: POWER switch detected\n");
200 1.11.4.2 nathanw
201 1.11.4.2 nathanw if (intstat2 & PMUINT_GPIO12)
202 1.11.4.2 nathanw printf("vrpmu: GPIO[12] activation\n");
203 1.11.4.2 nathanw if (intstat2 & PMUINT_GPIO11)
204 1.11.4.2 nathanw printf("vrpmu: GPIO[11] activation\n");
205 1.11.4.2 nathanw if (intstat2 & PMUINT_GPIO10)
206 1.11.4.2 nathanw printf("vrpmu: GPIO[10] activation\n");
207 1.11.4.2 nathanw if (intstat2 & PMUINT_GPIO9)
208 1.11.4.2 nathanw printf("vrpmu: GPIO[9] activation\n");
209 1.11.4.2 nathanw }
210 1.11.4.2 nathanw
211 1.11.4.2 nathanw /*
212 1.11.4.2 nathanw * dump PMU registers
213 1.11.4.2 nathanw *
214 1.11.4.2 nathanw */
215 1.11.4.2 nathanw void
216 1.11.4.2 nathanw vrpmu_dump_regs(void *arg)
217 1.11.4.2 nathanw {
218 1.11.4.2 nathanw struct vrpmu_softc *sc = arg;
219 1.11.4.2 nathanw unsigned int intstat1;
220 1.11.4.2 nathanw unsigned int intstat2;
221 1.11.4.2 nathanw unsigned int reg;
222 1.11.4.2 nathanw #if NVRBCU > 0
223 1.11.4.2 nathanw int cpuid;
224 1.11.4.2 nathanw #endif
225 1.11.4.2 nathanw intstat1 = vrpmu_read(sc, PMUINT_REG_W);
226 1.11.4.2 nathanw intstat2 = vrpmu_read(sc, PMUINT2_REG_W);
227 1.11.4.2 nathanw
228 1.11.4.2 nathanw /* others? XXXX */
229 1.11.4.2 nathanw reg = vrpmu_read(sc, PMUCNT_REG_W);
230 1.11.4.2 nathanw printf("vrpmu: cnt 0x%x: ", reg);
231 1.11.4.2 nathanw dbg_bit_print(reg);
232 1.11.4.2 nathanw reg = vrpmu_read(sc, PMUCNT2_REG_W);
233 1.11.4.2 nathanw printf("vrpmu: cnt2 0x%x: ", reg);
234 1.11.4.2 nathanw dbg_bit_print(reg);
235 1.11.4.2 nathanw #if NVRBCU > 0
236 1.11.4.2 nathanw cpuid = vrbcu_vrip_getcpuid();
237 1.11.4.2 nathanw if (cpuid >= BCUREVID_RID_4111){
238 1.11.4.2 nathanw reg = vrpmu_read(sc, PMUWAIT_REG_W);
239 1.11.4.2 nathanw printf("vrpmu: wait 0x%x", reg);
240 1.11.4.2 nathanw }
241 1.11.4.2 nathanw if (cpuid >= BCUREVID_RID_4121){
242 1.11.4.2 nathanw reg = vrpmu_read(sc, PMUDIV_REG_W);
243 1.11.4.2 nathanw printf(" div 0x%x", reg);
244 1.11.4.2 nathanw }
245 1.11.4.2 nathanw printf("\n");
246 1.11.4.2 nathanw #endif /* NVRBCU > 0 */
247 1.11.4.2 nathanw }
248 1.11.4.2 nathanw
249 1.11.4.2 nathanw /*
250 1.11.4.2 nathanw * PMU interrupt handler.
251 1.11.4.2 nathanw * XXX
252 1.11.4.2 nathanw *
253 1.11.4.2 nathanw * In the following interrupt routine we should actually DO something
254 1.11.4.2 nathanw * with the knowledge that we've gained. For now we just report it.
255 1.11.4.2 nathanw */
256 1.11.4.2 nathanw int
257 1.11.4.2 nathanw vrpmu_intr(void *arg)
258 1.11.4.2 nathanw {
259 1.11.4.2 nathanw struct vrpmu_softc *sc = arg;
260 1.11.4.2 nathanw unsigned int intstat1;
261 1.11.4.2 nathanw unsigned int intstat2;
262 1.11.4.2 nathanw
263 1.11.4.2 nathanw intstat1 = vrpmu_read(sc, PMUINT_REG_W);
264 1.11.4.2 nathanw /* clear interrupt status */
265 1.11.4.2 nathanw vrpmu_write(sc, PMUINT_REG_W, intstat1);
266 1.11.4.2 nathanw
267 1.11.4.2 nathanw
268 1.11.4.2 nathanw intstat2 = vrpmu_read(sc, PMUINT2_REG_W);
269 1.11.4.2 nathanw /* clear interrupt status */
270 1.11.4.2 nathanw vrpmu_write(sc, PMUINT2_REG_W, intstat2);
271 1.11.4.2 nathanw
272 1.11.4.2 nathanw DDUMP_INTR2(DEBUG_INTR, intstat1, intstat2);
273 1.11.4.2 nathanw
274 1.11.4.2 nathanw if (intstat1 & PMUINT_GPIO3)
275 1.11.4.2 nathanw ;
276 1.11.4.2 nathanw if (intstat1 & PMUINT_GPIO2)
277 1.11.4.2 nathanw ;
278 1.11.4.2 nathanw if (intstat1 & PMUINT_GPIO1)
279 1.11.4.2 nathanw ;
280 1.11.4.2 nathanw if (intstat1 & PMUINT_GPIO0)
281 1.11.4.2 nathanw ;
282 1.11.4.2 nathanw
283 1.11.4.2 nathanw if (intstat1 & PMUINT_RTC)
284 1.11.4.2 nathanw ;
285 1.11.4.2 nathanw if (intstat1 & PMUINT_BATT)
286 1.11.4.2 nathanw config_hook_call(CONFIG_HOOK_PMEVENT,
287 1.11.4.2 nathanw CONFIG_HOOK_PMEVENT_SUSPENDREQ, NULL);
288 1.11.4.2 nathanw if (intstat1 & PMUINT_TIMOUTRST)
289 1.11.4.2 nathanw ;
290 1.11.4.2 nathanw if (intstat1 & PMUINT_RTCRST)
291 1.11.4.2 nathanw ;
292 1.11.4.2 nathanw if (intstat1 & PMUINT_RSTSWRST)
293 1.11.4.2 nathanw ;
294 1.11.4.2 nathanw if (intstat1 & PMUINT_DMSWRST)
295 1.11.4.2 nathanw ;
296 1.11.4.2 nathanw if (intstat1 & PMUINT_BATTINTR)
297 1.11.4.2 nathanw config_hook_call(CONFIG_HOOK_PMEVENT,
298 1.11.4.2 nathanw CONFIG_HOOK_PMEVENT_SUSPENDREQ, NULL);
299 1.11.4.2 nathanw if (intstat1 & PMUINT_POWERSW) {
300 1.11.4.2 nathanw /*
301 1.11.4.2 nathanw * you can't detect when the button is released
302 1.11.4.2 nathanw */
303 1.11.4.2 nathanw config_hook_call(CONFIG_HOOK_BUTTONEVENT,
304 1.11.4.2 nathanw CONFIG_HOOK_BUTTONEVENT_POWER,
305 1.11.4.2 nathanw (void*)1 /* on */);
306 1.11.4.2 nathanw config_hook_call(CONFIG_HOOK_BUTTONEVENT,
307 1.11.4.2 nathanw CONFIG_HOOK_BUTTONEVENT_POWER,
308 1.11.4.2 nathanw (void*)0 /* off */);
309 1.11.4.2 nathanw }
310 1.11.4.2 nathanw
311 1.11.4.2 nathanw if (intstat2 & PMUINT_GPIO12)
312 1.11.4.2 nathanw ;
313 1.11.4.2 nathanw if (intstat2 & PMUINT_GPIO11)
314 1.11.4.2 nathanw ;
315 1.11.4.2 nathanw if (intstat2 & PMUINT_GPIO10)
316 1.11.4.2 nathanw ;
317 1.11.4.2 nathanw if (intstat2 & PMUINT_GPIO9)
318 1.11.4.2 nathanw ;
319 1.11.4.2 nathanw
320 1.11.4.2 nathanw return (0);
321 1.11.4.2 nathanw }
322