xref: /netbsd-src/sys/arch/hpcmips/vr/vrpmu.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /*	$NetBSD: vrpmu.c,v 1.19 2012/10/27 17:17:56 chs Exp $	*/
2 
3 /*
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.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: vrpmu.c,v 1.19 2012/10/27 17:17:56 chs Exp $");
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/device.h>
36 
37 #include <machine/bus.h>
38 #include <machine/config_hook.h>
39 #include <machine/debug.h>
40 
41 #include <hpcmips/vr/vripif.h>
42 #include <hpcmips/vr/vrpmuvar.h>
43 #include <hpcmips/vr/vrpmureg.h>
44 
45 #include "vrbcu.h"
46 #if NVRBCU > 0
47 #include <hpcmips/vr/bcuvar.h>
48 #include <hpcmips/vr/bcureg.h>
49 #endif
50 
51 #ifdef VRPMUDEBUG
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 */
68 
69 static int vrpmumatch(device_t, cfdata_t, void *);
70 static void vrpmuattach(device_t, device_t, void *);
71 
72 static void vrpmu_write(struct vrpmu_softc *, int, unsigned short);
73 static unsigned short vrpmu_read(struct vrpmu_softc *, int);
74 
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 *);
79 
80 CFATTACH_DECL_NEW(vrpmu, sizeof(struct vrpmu_softc),
81     vrpmumatch, vrpmuattach, NULL, NULL);
82 
83 struct vrpmu_softc *this_pmu;
84 
85 static inline void
86 vrpmu_write(struct vrpmu_softc *sc, int port, unsigned short val)
87 {
88 
89 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, port, val);
90 }
91 
92 static inline unsigned short
93 vrpmu_read(struct vrpmu_softc *sc, int port)
94 {
95 
96 	return (bus_space_read_2(sc->sc_iot, sc->sc_ioh, port));
97 }
98 
99 static int
100 vrpmumatch(device_t parent, cfdata_t cf, void *aux)
101 {
102 
103 	return (1);
104 }
105 
106 static void
107 vrpmuattach(device_t parent, device_t self, void *aux)
108 {
109 	struct vrpmu_softc *sc = device_private(self);
110 	struct vrip_attach_args *va = aux;
111 #if NVRBCU > 0
112 	int cpuid;
113 #endif /* NVRBCU > 0 */
114 
115 	bus_space_tag_t iot = va->va_iot;
116 	bus_space_handle_t ioh;
117 
118 	if (bus_space_map(iot, va->va_addr, 1, 0, &ioh)) {
119 		printf(": can't map bus space\n");
120 		return;
121 	}
122 
123 	sc->sc_iot = iot;
124 	sc->sc_ioh = ioh;
125 
126 	if (!(sc->sc_handler =
127 	    vrip_intr_establish(va->va_vc, va->va_unit, 0, IPL_TTY,
128 		vrpmu_intr, sc))) {
129 		printf (": can't map interrupt line.\n");
130 		return;
131 	}
132 	if (!vrip_intr_establish(va->va_vc, va->va_unit, 1, IPL_TTY,
133 	    vrpmu_intr, sc)) {
134 		printf (": can't map interrupt line.\n");
135 		return;
136 	}
137 
138 	printf("\n");
139 	/* dump current intrrupt states */
140 	vrpmu_dump_intr(sc);
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);
145 #if NVRBCU > 0
146 	cpuid = vrbcu_vrip_getcpuid();
147 	if (cpuid >= BCUREVID_RID_4111){
148 		vrpmu_write(sc, PMUWAIT_REG_W, PMUWAIT_DEFAULT);
149 	}
150 #endif /* NVRBCU */
151 
152 	this_pmu = sc;
153 }
154 
155 /*
156  * dump PMU intr status regs
157  *
158  */
159 void
160 vrpmu_dump_intr(void *arg)
161 {
162         struct vrpmu_softc *sc = arg;
163 	unsigned int intstat1;
164 	unsigned int intstat2;
165 
166 	intstat1 = vrpmu_read(sc, PMUINT_REG_W);
167 	intstat2 = vrpmu_read(sc, PMUINT2_REG_W);
168 	vrpmu_dump_intr2(intstat1, intstat2);
169 }
170 
171 /*
172  * dump PMU intr status regs
173  */
174 void
175 vrpmu_dump_intr2(unsigned int intstat1, unsigned int intstat2)
176 {
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");
185 
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");
190 
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");
203 
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");
212 }
213 
214 /*
215  * dump PMU registers
216  *
217  */
218 void
219 vrpmu_dump_regs(void *arg)
220 {
221         struct vrpmu_softc *sc = arg;
222 	unsigned int intstat1;
223 	unsigned int intstat2;
224 	unsigned int reg;
225 #if NVRBCU > 0
226 	int cpuid;
227 #endif
228 	intstat1 = vrpmu_read(sc, PMUINT_REG_W);
229 	intstat2 = vrpmu_read(sc, PMUINT2_REG_W);
230 
231 	/* others? XXXX */
232 	reg = vrpmu_read(sc, PMUCNT_REG_W);
233 	printf("vrpmu: cnt 0x%x: ", reg);
234 	dbg_bit_print(reg);
235 	reg = vrpmu_read(sc, PMUCNT2_REG_W);
236 	printf("vrpmu: cnt2 0x%x: ", reg);
237 	dbg_bit_print(reg);
238 #if NVRBCU > 0
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);
243 	}
244 	if (cpuid >= BCUREVID_RID_4121){
245 		reg = vrpmu_read(sc, PMUDIV_REG_W);
246 		printf(" div 0x%x", reg);
247 	}
248 	printf("\n");
249 #endif /*  NVRBCU > 0 */
250 }
251 
252 /*
253  * PMU interrupt handler.
254  * XXX
255  *
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.
258  */
259 int
260 vrpmu_intr(void *arg)
261 {
262         struct vrpmu_softc *sc = arg;
263 	unsigned int intstat1;
264 	unsigned int intstat2;
265 
266 	intstat1 = vrpmu_read(sc, PMUINT_REG_W);
267 	/* clear interrupt status */
268 	vrpmu_write(sc, PMUINT_REG_W, intstat1);
269 
270 
271 	intstat2 = vrpmu_read(sc, PMUINT2_REG_W);
272 	/* clear interrupt status */
273 	vrpmu_write(sc, PMUINT2_REG_W, intstat2);
274 
275 	DDUMP_INTR2(DEBUG_INTR, intstat1, intstat2);
276 
277 	if (intstat1 & PMUINT_GPIO3)
278 		;
279 	if (intstat1 & PMUINT_GPIO2)
280 		;
281 	if (intstat1 & PMUINT_GPIO1)
282 		;
283 	if (intstat1 & PMUINT_GPIO0)
284 		;
285 
286 	if (intstat1 & PMUINT_RTC)
287 		;
288 	if (intstat1 & PMUINT_BATT)
289 		config_hook_call(CONFIG_HOOK_PMEVENT,
290 		    CONFIG_HOOK_PMEVENT_SUSPENDREQ, NULL);
291 	if (intstat1 & PMUINT_TIMOUTRST)
292 		;
293 	if (intstat1 & PMUINT_RTCRST)
294 		;
295 	if (intstat1 & PMUINT_RSTSWRST)
296 		;
297 	if (intstat1 & PMUINT_DMSWRST)
298 		;
299 	if (intstat1 & PMUINT_BATTINTR)
300 		config_hook_call(CONFIG_HOOK_PMEVENT,
301 		    CONFIG_HOOK_PMEVENT_SUSPENDREQ, NULL);
302 	if (intstat1 & PMUINT_POWERSW) {
303 		/*
304 		 * you can't detect when the button is released
305 		 */
306 		config_hook_call(CONFIG_HOOK_BUTTONEVENT,
307 		    CONFIG_HOOK_BUTTONEVENT_POWER,
308 		    (void*)1 /* on */);
309 		config_hook_call(CONFIG_HOOK_BUTTONEVENT,
310 		    CONFIG_HOOK_BUTTONEVENT_POWER,
311 		    (void*)0 /* off */);
312 	}
313 
314 	if (intstat2 & PMUINT_GPIO12)
315 		;
316 	if (intstat2 & PMUINT_GPIO11)
317 		;
318 	if (intstat2 & PMUINT_GPIO10)
319 		;
320 	if (intstat2 & PMUINT_GPIO9)
321 		;
322 
323 	return (0);
324 }
325