tx39power.c revision 1.8.2.2 1 1.8.2.2 bouyer /* $NetBSD: tx39power.c,v 1.8.2.2 2000/11/20 20:47:26 bouyer Exp $ */
2 1.8.2.2 bouyer
3 1.8.2.2 bouyer /*-
4 1.8.2.2 bouyer * Copyright (c) 1999, 2000 The NetBSD Foundation, Inc.
5 1.8.2.2 bouyer * All rights reserved.
6 1.8.2.2 bouyer *
7 1.8.2.2 bouyer * This code is derived from software contributed to The NetBSD Foundation
8 1.8.2.2 bouyer * by UCHIYAMA Yasushi.
9 1.8.2.2 bouyer *
10 1.8.2.2 bouyer * Redistribution and use in source and binary forms, with or without
11 1.8.2.2 bouyer * modification, are permitted provided that the following conditions
12 1.8.2.2 bouyer * are met:
13 1.8.2.2 bouyer * 1. Redistributions of source code must retain the above copyright
14 1.8.2.2 bouyer * notice, this list of conditions and the following disclaimer.
15 1.8.2.2 bouyer * 2. Redistributions in binary form must reproduce the above copyright
16 1.8.2.2 bouyer * notice, this list of conditions and the following disclaimer in the
17 1.8.2.2 bouyer * documentation and/or other materials provided with the distribution.
18 1.8.2.2 bouyer * 3. All advertising materials mentioning features or use of this software
19 1.8.2.2 bouyer * must display the following acknowledgement:
20 1.8.2.2 bouyer * This product includes software developed by the NetBSD
21 1.8.2.2 bouyer * Foundation, Inc. and its contributors.
22 1.8.2.2 bouyer * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.8.2.2 bouyer * contributors may be used to endorse or promote products derived
24 1.8.2.2 bouyer * from this software without specific prior written permission.
25 1.8.2.2 bouyer *
26 1.8.2.2 bouyer * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.8.2.2 bouyer * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.8.2.2 bouyer * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.8.2.2 bouyer * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.8.2.2 bouyer * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.8.2.2 bouyer * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.8.2.2 bouyer * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.8.2.2 bouyer * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.8.2.2 bouyer * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.8.2.2 bouyer * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.8.2.2 bouyer * POSSIBILITY OF SUCH DAMAGE.
37 1.8.2.2 bouyer */
38 1.8.2.2 bouyer
39 1.8.2.2 bouyer #include "opt_tx39_debug.h"
40 1.8.2.2 bouyer //#include "opt_tx39powerdebug.h"
41 1.8.2.2 bouyer #define TX39POWERDEBUG
42 1.8.2.2 bouyer
43 1.8.2.2 bouyer #include <sys/param.h>
44 1.8.2.2 bouyer #include <sys/systm.h>
45 1.8.2.2 bouyer #include <sys/device.h>
46 1.8.2.2 bouyer
47 1.8.2.2 bouyer #include <machine/bus.h>
48 1.8.2.2 bouyer #include <machine/intr.h>
49 1.8.2.2 bouyer #include <machine/config_hook.h>
50 1.8.2.2 bouyer
51 1.8.2.2 bouyer #include <hpcmips/tx/tx39var.h>
52 1.8.2.2 bouyer #include <hpcmips/tx/tx39icureg.h>
53 1.8.2.2 bouyer #include <hpcmips/tx/tx39powerreg.h>
54 1.8.2.2 bouyer
55 1.8.2.2 bouyer #ifdef TX39POWERDEBUG
56 1.8.2.2 bouyer int tx39power_debug = 1;
57 1.8.2.2 bouyer #define DPRINTF(arg) if (tx39power_debug) printf arg;
58 1.8.2.2 bouyer #define DPRINTFN(n, arg) if (tx39power_debug > (n)) printf arg;
59 1.8.2.2 bouyer #define DUMP_REGS(x) __tx39power_dump(x)
60 1.8.2.2 bouyer #else
61 1.8.2.2 bouyer #define DPRINTF(arg) ((void)0)
62 1.8.2.2 bouyer #define DPRINTFN(n, arg) ((void)0)
63 1.8.2.2 bouyer #define DUMP_REGS(x) ((void)0)
64 1.8.2.2 bouyer #endif
65 1.8.2.2 bouyer
66 1.8.2.2 bouyer #define ISSET(x, v) ((x) & (v))
67 1.8.2.2 bouyer #define ISSETPRINT(r, m) __is_set_print(r, TX39_POWERCTRL_##m, #m)
68 1.8.2.2 bouyer
69 1.8.2.2 bouyer int tx39power_match(struct device *, struct cfdata *, void *);
70 1.8.2.2 bouyer void tx39power_attach(struct device *, struct device *, void *);
71 1.8.2.2 bouyer
72 1.8.2.2 bouyer struct tx39power_softc {
73 1.8.2.2 bouyer struct device sc_dev;
74 1.8.2.2 bouyer tx_chipset_tag_t sc_tc;
75 1.8.2.2 bouyer
76 1.8.2.2 bouyer /* save interrupt status for resume */
77 1.8.2.2 bouyer txreg_t sc_icu_state[TX39_INTRSET_MAX + 1];
78 1.8.2.2 bouyer };
79 1.8.2.2 bouyer
80 1.8.2.2 bouyer struct cfattach tx39power_ca = {
81 1.8.2.2 bouyer sizeof(struct tx39power_softc), tx39power_match, tx39power_attach
82 1.8.2.2 bouyer };
83 1.8.2.2 bouyer
84 1.8.2.2 bouyer void tx39power_suspend_cpu(void); /* automatic hardware resume */
85 1.8.2.2 bouyer
86 1.8.2.2 bouyer static int tx39power_intr_p(void *);
87 1.8.2.2 bouyer static int tx39power_intr_n(void *);
88 1.8.2.2 bouyer static int tx39power_ok_intr_p(void *);
89 1.8.2.2 bouyer static int tx39power_ok_intr_n(void *);
90 1.8.2.2 bouyer static int tx39power_button_intr_p(void *);
91 1.8.2.2 bouyer static int tx39power_button_intr_n(void *);
92 1.8.2.2 bouyer #ifdef TX39POWERDEBUG
93 1.8.2.2 bouyer static void __tx39power_dump(struct tx39power_softc *);
94 1.8.2.2 bouyer #endif
95 1.8.2.2 bouyer
96 1.8.2.2 bouyer int
97 1.8.2.2 bouyer tx39power_match(struct device *parent, struct cfdata *cf, void *aux)
98 1.8.2.2 bouyer {
99 1.8.2.2 bouyer return ATTACH_FIRST;
100 1.8.2.2 bouyer }
101 1.8.2.2 bouyer
102 1.8.2.2 bouyer void
103 1.8.2.2 bouyer tx39power_attach(struct device *parent, struct device *self, void *aux)
104 1.8.2.2 bouyer {
105 1.8.2.2 bouyer struct txsim_attach_args *ta = aux;
106 1.8.2.2 bouyer struct tx39power_softc *sc = (void*)self;
107 1.8.2.2 bouyer tx_chipset_tag_t tc;
108 1.8.2.2 bouyer txreg_t reg;
109 1.8.2.2 bouyer
110 1.8.2.2 bouyer tc = sc->sc_tc = ta->ta_tc;
111 1.8.2.2 bouyer tx_conf_register_power(tc, self);
112 1.8.2.2 bouyer
113 1.8.2.2 bouyer printf("\n");
114 1.8.2.2 bouyer DUMP_REGS(sc);
115 1.8.2.2 bouyer
116 1.8.2.2 bouyer /* power button setting */
117 1.8.2.2 bouyer reg = tx_conf_read(tc, TX39_POWERCTRL_REG);
118 1.8.2.2 bouyer reg |= TX39_POWERCTRL_DBNCONBUTN;
119 1.8.2.2 bouyer tx_conf_write(tc, TX39_POWERCTRL_REG, reg);
120 1.8.2.2 bouyer
121 1.8.2.2 bouyer /* enable stop timer */
122 1.8.2.2 bouyer reg = tx_conf_read(tc, TX39_POWERCTRL_REG);
123 1.8.2.2 bouyer reg &= ~(TX39_POWERCTRL_STPTIMERVAL_MASK <<
124 1.8.2.2 bouyer TX39_POWERCTRL_STPTIMERVAL_SHIFT);
125 1.8.2.2 bouyer reg = TX39_POWERCTRL_STPTIMERVAL_SET(reg,
126 1.8.2.2 bouyer TX39_POWERCTRL_STPTIMERVAL_MAX);
127 1.8.2.2 bouyer reg |= TX39_POWERCTRL_ENSTPTIMER;
128 1.8.2.2 bouyer tx_conf_write(tc, TX39_POWERCTRL_REG, reg);
129 1.8.2.2 bouyer
130 1.8.2.2 bouyer /* install power event handler */
131 1.8.2.2 bouyer /* low priority */
132 1.8.2.2 bouyer tx_intr_establish(tc, MAKEINTR(5, TX39_INTRSTATUS5_POSPWRINT),
133 1.8.2.2 bouyer IST_EDGE, IPL_CLOCK,
134 1.8.2.2 bouyer tx39power_intr_p, sc);
135 1.8.2.2 bouyer tx_intr_establish(tc, MAKEINTR(5, TX39_INTRSTATUS5_NEGPWRINT),
136 1.8.2.2 bouyer IST_EDGE, IPL_CLOCK,
137 1.8.2.2 bouyer tx39power_intr_n, sc);
138 1.8.2.2 bouyer /* high priority */
139 1.8.2.2 bouyer tx_intr_establish(tc, MAKEINTR(5, TX39_INTRSTATUS5_POSPWROKINT),
140 1.8.2.2 bouyer IST_EDGE, IPL_CLOCK,
141 1.8.2.2 bouyer tx39power_ok_intr_p, sc);
142 1.8.2.2 bouyer tx_intr_establish(tc, MAKEINTR(5, TX39_INTRSTATUS5_NEGPWROKINT),
143 1.8.2.2 bouyer IST_EDGE, IPL_CLOCK,
144 1.8.2.2 bouyer tx39power_ok_intr_n, sc);
145 1.8.2.2 bouyer /* user driven event */
146 1.8.2.2 bouyer tx_intr_establish(tc, MAKEINTR(5, TX39_INTRSTATUS5_POSONBUTNINT),
147 1.8.2.2 bouyer IST_EDGE, IPL_CLOCK,
148 1.8.2.2 bouyer tx39power_button_intr_p, sc);
149 1.8.2.2 bouyer tx_intr_establish(tc, MAKEINTR(5, TX39_INTRSTATUS5_NEGONBUTNINT),
150 1.8.2.2 bouyer IST_EDGE, IPL_CLOCK,
151 1.8.2.2 bouyer tx39power_button_intr_n, sc);
152 1.8.2.2 bouyer }
153 1.8.2.2 bouyer
154 1.8.2.2 bouyer void
155 1.8.2.2 bouyer tx39power_suspend_cpu() /* I assume already splhigh */
156 1.8.2.2 bouyer {
157 1.8.2.2 bouyer tx_chipset_tag_t tc = tx_conf_get_tag();
158 1.8.2.2 bouyer struct tx39power_softc *sc = tc->tc_powert;
159 1.8.2.2 bouyer txreg_t reg, *iregs = sc->sc_icu_state;
160 1.8.2.2 bouyer
161 1.8.2.2 bouyer printf ("%s: CPU sleep\n", sc->sc_dev.dv_xname);
162 1.8.2.2 bouyer __asm__ __volatile__(".set noreorder");
163 1.8.2.2 bouyer reg = tx_conf_read(tc, TX39_POWERCTRL_REG);
164 1.8.2.2 bouyer reg |= TX39_POWERCTRL_STOPCPU;
165 1.8.2.2 bouyer /* save interrupt state */
166 1.8.2.2 bouyer iregs[0] = tx_conf_read(tc, TX39_INTRENABLE6_REG);
167 1.8.2.2 bouyer iregs[1] = tx_conf_read(tc, TX39_INTRENABLE1_REG);
168 1.8.2.2 bouyer iregs[2] = tx_conf_read(tc, TX39_INTRENABLE2_REG);
169 1.8.2.2 bouyer iregs[3] = tx_conf_read(tc, TX39_INTRENABLE3_REG);
170 1.8.2.2 bouyer iregs[4] = tx_conf_read(tc, TX39_INTRENABLE4_REG);
171 1.8.2.2 bouyer iregs[5] = tx_conf_read(tc, TX39_INTRENABLE5_REG);
172 1.8.2.2 bouyer #ifdef TX392X
173 1.8.2.2 bouyer iregs[7] = tx_conf_read(tc, TX39_INTRENABLE7_REG);
174 1.8.2.2 bouyer iregs[8] = tx_conf_read(tc, TX39_INTRENABLE8_REG);
175 1.8.2.2 bouyer #endif
176 1.8.2.2 bouyer /* disable all interrupt (don't disable GLOBALEN) */
177 1.8.2.2 bouyer tx_conf_write(tc, TX39_INTRENABLE6_REG, TX39_INTRENABLE6_GLOBALEN);
178 1.8.2.2 bouyer tx_conf_write(tc, TX39_INTRENABLE1_REG, 0);
179 1.8.2.2 bouyer tx_conf_write(tc, TX39_INTRENABLE2_REG, 0);
180 1.8.2.2 bouyer tx_conf_write(tc, TX39_INTRENABLE3_REG, 0);
181 1.8.2.2 bouyer tx_conf_write(tc, TX39_INTRENABLE4_REG, 0);
182 1.8.2.2 bouyer tx_conf_write(tc, TX39_INTRENABLE5_REG, 0);
183 1.8.2.2 bouyer #ifdef TX392X
184 1.8.2.2 bouyer tx_conf_write(tc, TX39_INTRENABLE7_REG, 0);
185 1.8.2.2 bouyer tx_conf_write(tc, TX39_INTRENABLE8_REG, 0);
186 1.8.2.2 bouyer #endif
187 1.8.2.2 bouyer /* enable power button interrupt only */
188 1.8.2.2 bouyer tx_conf_write(tc, TX39_INTRCLEAR5_REG, TX39_INTRSTATUS5_NEGONBUTNINT);
189 1.8.2.2 bouyer tx_conf_write(tc, TX39_INTRENABLE5_REG, TX39_INTRSTATUS5_NEGONBUTNINT);
190 1.8.2.2 bouyer __asm__ __volatile__("sync");
191 1.8.2.2 bouyer
192 1.8.2.2 bouyer /* stop CPU clock */
193 1.8.2.2 bouyer tx_conf_write(tc, TX39_POWERCTRL_REG, reg);
194 1.8.2.2 bouyer __asm__ __volatile__("sync");
195 1.8.2.2 bouyer /* wait until power button pressed */
196 1.8.2.2 bouyer /* clear interrupt */
197 1.8.2.2 bouyer tx_conf_write(tc, TX39_INTRCLEAR5_REG, TX39_INTRSTATUS5_NEGONBUTNINT);
198 1.8.2.2 bouyer #ifdef TX392X
199 1.8.2.2 bouyer /* Clear WARMSTART bit to reset vector(0xbfc00000) work correctly */
200 1.8.2.2 bouyer reg = tx_conf_read(tc, TX39_POWERCTRL_REG);
201 1.8.2.2 bouyer reg &= ~TX39_POWERCTRL_WARMSTART;
202 1.8.2.2 bouyer tx_conf_write(tc, TX39_POWERCTRL_REG, reg);
203 1.8.2.2 bouyer #endif
204 1.8.2.2 bouyer
205 1.8.2.2 bouyer /* restore interrupt state */
206 1.8.2.2 bouyer tx_conf_write(tc, TX39_INTRENABLE6_REG, iregs[0]);
207 1.8.2.2 bouyer tx_conf_write(tc, TX39_INTRENABLE1_REG, iregs[1]);
208 1.8.2.2 bouyer tx_conf_write(tc, TX39_INTRENABLE2_REG, iregs[2]);
209 1.8.2.2 bouyer tx_conf_write(tc, TX39_INTRENABLE3_REG, iregs[3]);
210 1.8.2.2 bouyer tx_conf_write(tc, TX39_INTRENABLE4_REG, iregs[4]);
211 1.8.2.2 bouyer tx_conf_write(tc, TX39_INTRENABLE5_REG, iregs[5]);
212 1.8.2.2 bouyer #ifdef TX392X
213 1.8.2.2 bouyer tx_conf_write(tc, TX39_INTRENABLE7_REG, iregs[7]);
214 1.8.2.2 bouyer tx_conf_write(tc, TX39_INTRENABLE8_REG, iregs[8]);
215 1.8.2.2 bouyer #endif
216 1.8.2.2 bouyer __asm__ __volatile__(".set reorder");
217 1.8.2.2 bouyer
218 1.8.2.2 bouyer printf ("%s: CPU wakeup\n", sc->sc_dev.dv_xname);
219 1.8.2.2 bouyer }
220 1.8.2.2 bouyer
221 1.8.2.2 bouyer static int
222 1.8.2.2 bouyer tx39power_button_intr_p(void *arg)
223 1.8.2.2 bouyer {
224 1.8.2.2 bouyer config_hook_call(CONFIG_HOOK_BUTTONEVENT,
225 1.8.2.2 bouyer CONFIG_HOOK_BUTTONEVENT_POWER,
226 1.8.2.2 bouyer (void *)1 /* on */);
227 1.8.2.2 bouyer return 0;
228 1.8.2.2 bouyer }
229 1.8.2.2 bouyer
230 1.8.2.2 bouyer static int
231 1.8.2.2 bouyer tx39power_button_intr_n(void *arg)
232 1.8.2.2 bouyer {
233 1.8.2.2 bouyer config_hook_call(CONFIG_HOOK_BUTTONEVENT,
234 1.8.2.2 bouyer CONFIG_HOOK_BUTTONEVENT_POWER,
235 1.8.2.2 bouyer (void *)0 /* off */);
236 1.8.2.2 bouyer DUMP_REGS(arg);
237 1.8.2.2 bouyer return 0;
238 1.8.2.2 bouyer }
239 1.8.2.2 bouyer
240 1.8.2.2 bouyer int
241 1.8.2.2 bouyer tx39power_intr_p(void *arg)
242 1.8.2.2 bouyer {
243 1.8.2.2 bouyer /* low priority event */
244 1.8.2.2 bouyer printf("power_p\n");
245 1.8.2.2 bouyer DUMP_REGS(arg);
246 1.8.2.2 bouyer return 0;
247 1.8.2.2 bouyer }
248 1.8.2.2 bouyer
249 1.8.2.2 bouyer static int
250 1.8.2.2 bouyer tx39power_intr_n(void *arg)
251 1.8.2.2 bouyer {
252 1.8.2.2 bouyer /* low priority event */
253 1.8.2.2 bouyer printf("power_n\n");
254 1.8.2.2 bouyer DUMP_REGS(arg);
255 1.8.2.2 bouyer return 0;
256 1.8.2.2 bouyer }
257 1.8.2.2 bouyer
258 1.8.2.2 bouyer static int
259 1.8.2.2 bouyer tx39power_ok_intr_p(void *arg)
260 1.8.2.2 bouyer {
261 1.8.2.2 bouyer /* high priority event */
262 1.8.2.2 bouyer printf("power NG\n");
263 1.8.2.2 bouyer DUMP_REGS(arg);
264 1.8.2.2 bouyer config_hook_call(CONFIG_HOOK_PMEVENT,
265 1.8.2.2 bouyer CONFIG_HOOK_PMEVENT_SUSPENDREQ, NULL);
266 1.8.2.2 bouyer return 0;
267 1.8.2.2 bouyer }
268 1.8.2.2 bouyer
269 1.8.2.2 bouyer static int
270 1.8.2.2 bouyer tx39power_ok_intr_n(void *arg)
271 1.8.2.2 bouyer {
272 1.8.2.2 bouyer /* high priority event */
273 1.8.2.2 bouyer printf("power OK\n");
274 1.8.2.2 bouyer DUMP_REGS(arg);
275 1.8.2.2 bouyer return 0;
276 1.8.2.2 bouyer }
277 1.8.2.2 bouyer
278 1.8.2.2 bouyer #ifdef TX39POWERDEBUG
279 1.8.2.2 bouyer static void
280 1.8.2.2 bouyer __tx39power_dump (struct tx39power_softc *sc)
281 1.8.2.2 bouyer {
282 1.8.2.2 bouyer tx_chipset_tag_t tc = sc->sc_tc;
283 1.8.2.2 bouyer txreg_t reg;
284 1.8.2.2 bouyer
285 1.8.2.2 bouyer reg = tx_conf_read(tc, TX39_POWERCTRL_REG);
286 1.8.2.2 bouyer ISSETPRINT(reg, ONBUTN);
287 1.8.2.2 bouyer ISSETPRINT(reg, PWRINT);
288 1.8.2.2 bouyer ISSETPRINT(reg, PWROK);
289 1.8.2.2 bouyer #ifdef TX392X
290 1.8.2.2 bouyer ISSETPRINT(reg, PWROKNMI);
291 1.8.2.2 bouyer #endif /* TX392X */
292 1.8.2.2 bouyer ISSETPRINT(reg, SLOWBUS);
293 1.8.2.2 bouyer #ifdef TX391X
294 1.8.2.2 bouyer ISSETPRINT(reg, DIVMOD);
295 1.8.2.2 bouyer #endif /* TX391X */
296 1.8.2.2 bouyer ISSETPRINT(reg, ENSTPTIMER);
297 1.8.2.2 bouyer ISSETPRINT(reg, ENFORCESHUTDWN);
298 1.8.2.2 bouyer ISSETPRINT(reg, FORCESHUTDWN);
299 1.8.2.2 bouyer ISSETPRINT(reg, FORCESHUTDWNOCC);
300 1.8.2.2 bouyer ISSETPRINT(reg, SELC2MS);
301 1.8.2.2 bouyer #ifdef TX392X
302 1.8.2.2 bouyer ISSETPRINT(reg, WARMSTART);
303 1.8.2.2 bouyer #endif /* TX392X */
304 1.8.2.2 bouyer ISSETPRINT(reg, BPDBVCC3);
305 1.8.2.2 bouyer ISSETPRINT(reg, STOPCPU);
306 1.8.2.2 bouyer ISSETPRINT(reg, DBNCONBUTN);
307 1.8.2.2 bouyer ISSETPRINT(reg, COLDSTART);
308 1.8.2.2 bouyer ISSETPRINT(reg, PWRCS);
309 1.8.2.2 bouyer ISSETPRINT(reg, VCCON);
310 1.8.2.2 bouyer #ifdef TX391X
311 1.8.2.2 bouyer printf("VIDRF=%d ", TX39_POWERCTRL_VIDRF(reg));
312 1.8.2.2 bouyer #endif /* TX391X */
313 1.8.2.2 bouyer printf("STPTIMERVAL=%d ", TX39_POWERCTRL_STPTIMERVAL(reg));
314 1.8.2.2 bouyer printf("\n");
315 1.8.2.2 bouyer }
316 1.8.2.2 bouyer #endif /* TX39POWERDEBUG */
317 1.8.2.2 bouyer
318