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