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