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