vrpmu.c revision 1.4 1 /* $NetBSD: vrpmu.c,v 1.4 1999/12/23 06:26:10 takemura 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 static int vrpmumatch __P((struct device *, struct cfdata *, void *));
47 static void vrpmuattach __P((struct device *, struct device *, void *));
48
49 static void vrpmu_write __P((struct vrpmu_softc *, int, unsigned short));
50 static unsigned short vrpmu_read __P((struct vrpmu_softc *, int));
51
52 int vrpmu_intr __P((void *));
53 static void vrpmu_dump_intr __P((unsigned int, unsigned int));
54 static void vrpmu_dump_regs __P((void *));
55
56 struct cfattach vrpmu_ca = {
57 sizeof(struct vrpmu_softc), vrpmumatch, vrpmuattach
58 };
59
60 static inline void
61 vrpmu_write(sc, port, val)
62 struct vrpmu_softc *sc;
63 int port;
64 unsigned short val;
65 {
66 bus_space_write_2(sc->sc_iot, sc->sc_ioh, port, val);
67 }
68
69 static inline unsigned short
70 vrpmu_read(sc, port)
71 struct vrpmu_softc *sc;
72 int port;
73 {
74 return bus_space_read_2(sc->sc_iot, sc->sc_ioh, port);
75 }
76
77 static int
78 vrpmumatch(parent, cf, aux)
79 struct device *parent;
80 struct cfdata *cf;
81 void *aux;
82 {
83 return 1;
84 }
85
86 static void
87 vrpmuattach(parent, self, aux)
88 struct device *parent;
89 struct device *self;
90 void *aux;
91 {
92 struct vrpmu_softc *sc = (struct vrpmu_softc *)self;
93 struct vrip_attach_args *va = aux;
94
95 bus_space_tag_t iot = va->va_iot;
96 bus_space_handle_t ioh;
97
98 if (bus_space_map(iot, va->va_addr, 1, 0, &ioh)) {
99 printf(": can't map bus space\n");
100 return;
101 }
102
103 sc->sc_iot = iot;
104 sc->sc_ioh = ioh;
105
106 if (!(sc->sc_handler =
107 vrip_intr_establish(va->va_vc, va->va_intr, IPL_TTY,
108 vrpmu_intr, sc))) {
109 printf (": can't map interrupt line.\n");
110 return;
111 }
112
113 printf("\n");
114 /* dump current registers */
115 vrpmu_dump_regs(sc);
116 /* clear interrupt status */
117 vrpmu_write(sc, PMUINT_REG_W, PMUINT_ALL);
118 vrpmu_write(sc, PMUINT2_REG_W, PMUINT2_ALL);
119 }
120
121 /*
122 * dump PMU intr status regs
123 */
124 void
125 vrpmu_dump_intr(intstat1, intstat2)
126 unsigned int intstat1, intstat2;
127 {
128 if (intstat1 & PMUINT_GPIO3)
129 printf("vrpmu: GPIO[3] activation\n");
130 if (intstat1 & PMUINT_GPIO2)
131 printf("vrpmu: GPIO[2] activation\n");
132 if (intstat1 & PMUINT_GPIO1)
133 printf("vrpmu: GPIO[1] activation\n");
134 if (intstat1 & PMUINT_GPIO0)
135 printf("vrpmu: GPIO[0] activation\n");
136
137 if (intstat1 & PMUINT_RTC)
138 printf("vrpmu: RTC alarm detected\n");
139 if (intstat1 & PMUINT_BATT)
140 printf("vrpmu: Battery low during activation\n");
141
142 if (intstat1 & PMUINT_TIMOUTRST)
143 printf("vrpmu: HAL timer reset\n");
144 if (intstat1 & PMUINT_RTCRST)
145 printf("vrpmu: RTC reset detected\n");
146 if (intstat1 & PMUINT_RSTSWRST)
147 printf("vrpmu: RESET switch detected\n");
148 if (intstat1 & PMUINT_DMSWRST)
149 printf("vrpmu: Deadman's switch detected\n");
150 if (intstat1 & PMUINT_BATTINTR)
151 printf("vrpmu: Battery low during normal ops\n");
152 if (intstat1 & PMUINT_POWERSW)
153 printf("vrpmu: POWER switch detected\n");
154
155 if (intstat2 & PMUINT_GPIO12)
156 printf("vrpmu: GPIO[12] activation\n");
157 if (intstat2 & PMUINT_GPIO11)
158 printf("vrpmu: GPIO[11] activation\n");
159 if (intstat2 & PMUINT_GPIO10)
160 printf("vrpmu: GPIO[10] activation\n");
161 if (intstat2 & PMUINT_GPIO9)
162 printf("vrpmu: GPIO[9] activation\n");
163 }
164
165 /*
166 * dump PMU registers
167 *
168 */
169 void
170 vrpmu_dump_regs(arg)
171 void *arg;
172 {
173 struct vrpmu_softc *sc = arg;
174 unsigned int intstat1;
175 unsigned int intstat2;
176 unsigned int reg;
177 #if NVRBCU > 0
178 int cpuid;
179 #endif
180 intstat1 = vrpmu_read(sc, PMUINT_REG_W);
181 intstat2 = vrpmu_read(sc, PMUINT2_REG_W);
182 vrpmu_dump_intr(intstat1, intstat2);
183
184 /* others? XXXX */
185 reg = vrpmu_read(sc, PMUCNT_REG_W);
186 printf("vrpmu: cnt 0x%x: ", reg);
187 bitdisp16(reg);
188 reg = vrpmu_read(sc, PMUCNT2_REG_W);
189 printf("vrpmu: cnt2 0x%x: ", reg);
190 bitdisp16(reg);
191 #if NVRBCU > 0
192 cpuid = vrbcu_vrip_getcpuid();
193 if (cpuid >= BCUREVID_RID_4111){
194 reg = vrpmu_read(sc, PMUWAIT_REG_W);
195 printf("vrpmu: wait 0x%x", reg);
196 }
197 if (cpuid >= BCUREVID_RID_4121){
198 reg = vrpmu_read(sc, PMUDIV_REG_W);
199 printf(" div 0x%x", reg);
200 }
201 #endif
202 printf("\n");
203
204 }
205
206 /*
207 * PMU interrupt handler.
208 * XXX
209 *
210 * In the following interrupt routine we should actually DO something
211 * with the knowledge that we've gained. For now we just report it.
212 */
213 int
214 vrpmu_intr(arg)
215 void *arg;
216 {
217 struct vrpmu_softc *sc = arg;
218 unsigned int intstat1;
219 unsigned int intstat2;
220
221 intstat1 = vrpmu_read(sc, PMUINT_REG_W);
222 /* clear interrupt status */
223 vrpmu_write(sc, PMUINT_REG_W, intstat1);
224
225
226 intstat2 = vrpmu_read(sc, PMUINT2_REG_W);
227 /* clear interrupt status */
228 vrpmu_write(sc, PMUINT2_REG_W, intstat2);
229
230 vrpmu_dump_intr(intstat1, intstat2);
231
232 if (intstat1 & PMUINT_GPIO3)
233 ;
234 if (intstat1 & PMUINT_GPIO2)
235 ;
236 if (intstat1 & PMUINT_GPIO1)
237 ;
238 if (intstat1 & PMUINT_GPIO0)
239 ;
240
241 if (intstat1 & PMUINT_RTC)
242 ;
243 if (intstat1 & PMUINT_BATT)
244 ;
245
246 if (intstat1 & PMUINT_TIMOUTRST)
247 ;
248 if (intstat1 & PMUINT_RTCRST)
249 ;
250 if (intstat1 & PMUINT_RSTSWRST)
251 ;
252 if (intstat1 & PMUINT_DMSWRST)
253 ;
254 if (intstat1 & PMUINT_BATTINTR)
255 ;
256 if (intstat1 & PMUINT_POWERSW)
257 config_hook_call(CONFIG_HOOK_BUTTONEVENT,
258 CONFIG_HOOK_BUTTONEVENT_POWER,
259 (void*)1);
260
261 if (intstat2 & PMUINT_GPIO12)
262 ;
263 if (intstat2 & PMUINT_GPIO11)
264 ;
265 if (intstat2 & PMUINT_GPIO10)
266 ;
267 if (intstat2 & PMUINT_GPIO9)
268 ;
269
270 return 0;
271 }
272