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