pwctl.c revision 1.1 1 1.1 takemura /* $NetBSD: pwctl.c,v 1.1 2001/04/30 10:09:14 takemura Exp $ */
2 1.1 takemura
3 1.1 takemura /*-
4 1.1 takemura * Copyright (c) 1999
5 1.1 takemura * Shin Takemura and PocketBSD Project. All rights reserved.
6 1.1 takemura * Copyright (c) 2000,2001
7 1.1 takemura * SATO Kazumi. All rights reserved.
8 1.1 takemura *
9 1.1 takemura * Redistribution and use in source and binary forms, with or without
10 1.1 takemura * modification, are permitted provided that the following conditions
11 1.1 takemura * are met:
12 1.1 takemura * 1. Redistributions of source code must retain the above copyright
13 1.1 takemura * notice, this list of conditions and the following disclaimer.
14 1.1 takemura * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 takemura * notice, this list of conditions and the following disclaimer in the
16 1.1 takemura * documentation and/or other materials provided with the distribution.
17 1.1 takemura * 3. All advertising materials mentioning features or use of this software
18 1.1 takemura * must display the following acknowledgement:
19 1.1 takemura * This product includes software developed by the PocketBSD project
20 1.1 takemura * and its contributors.
21 1.1 takemura * 4. Neither the name of the project nor the names of its contributors
22 1.1 takemura * may be used to endorse or promote products derived from this software
23 1.1 takemura * without specific prior written permission.
24 1.1 takemura *
25 1.1 takemura * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 1.1 takemura * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 1.1 takemura * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 1.1 takemura * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 1.1 takemura * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 1.1 takemura * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 1.1 takemura * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 1.1 takemura * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 1.1 takemura * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 1.1 takemura * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 1.1 takemura * SUCH DAMAGE.
36 1.1 takemura *
37 1.1 takemura */
38 1.1 takemura
39 1.1 takemura #include <sys/param.h>
40 1.1 takemura #include <sys/systm.h>
41 1.1 takemura #include <sys/device.h>
42 1.1 takemura
43 1.1 takemura #include <machine/bus.h>
44 1.1 takemura
45 1.1 takemura #include <machine/config_hook.h>
46 1.1 takemura #include <machine/platid.h>
47 1.1 takemura #include <machine/platid_mask.h>
48 1.1 takemura
49 1.1 takemura #include <dev/hpc/hpciovar.h>
50 1.1 takemura
51 1.1 takemura #include "opt_vr41xx.h"
52 1.1 takemura #include <hpcmips/vr/vripvar.h>
53 1.1 takemura
54 1.1 takemura #include "locators.h"
55 1.1 takemura
56 1.1 takemura #define PWCTLVRGIUDEBUG
57 1.1 takemura #ifdef PWCTLVRGIUDEBUG
58 1.1 takemura int pwctl_vrgiu_debug = 0;
59 1.1 takemura #define DPRINTF(arg) if (pwctl_vrgiu_debug) printf arg;
60 1.1 takemura #else
61 1.1 takemura #define DPRINTF(arg)
62 1.1 takemura #endif
63 1.1 takemura
64 1.1 takemura struct pwctl_vrgiu_softc {
65 1.1 takemura struct device sc_dev;
66 1.1 takemura hpcio_chip_t sc_hc;
67 1.1 takemura int sc_port;
68 1.1 takemura long sc_id;
69 1.1 takemura int sc_on, sc_off;
70 1.1 takemura config_hook_tag sc_hook_tag;
71 1.1 takemura config_hook_tag sc_hook_hardpower;
72 1.1 takemura config_hook_tag sc_ghook_tag;
73 1.1 takemura int sc_save;
74 1.1 takemura int sc_initvalue;
75 1.1 takemura };
76 1.1 takemura
77 1.1 takemura static int pwctl_vrgiu_match __P((struct device *, struct cfdata *,
78 1.1 takemura void *));
79 1.1 takemura static void pwctl_vrgiu_attach __P((struct device *, struct device *,
80 1.1 takemura void *));
81 1.1 takemura static int pwctl_vrgiu_hook __P((void *ctx, int type, long id,
82 1.1 takemura void *msg));
83 1.1 takemura static int pwctl_vrgiu_ghook __P((void *ctx, int type, long id,
84 1.1 takemura void *msg));
85 1.1 takemura int pwctl_vrgiu_hardpower __P((void *, int, long, void *));
86 1.1 takemura
87 1.1 takemura struct cfattach pwctl_vrgiu_ca = {
88 1.1 takemura sizeof(struct pwctl_vrgiu_softc), pwctl_vrgiu_match, pwctl_vrgiu_attach
89 1.1 takemura };
90 1.1 takemura
91 1.1 takemura int
92 1.1 takemura pwctl_vrgiu_match(parent, match, aux)
93 1.1 takemura struct device *parent;
94 1.1 takemura struct cfdata *match;
95 1.1 takemura void *aux;
96 1.1 takemura {
97 1.1 takemura platid_mask_t mask;
98 1.1 takemura
99 1.1 takemura if (match->cf_loc[HPCIOIFCF_PLATFORM] == 0)
100 1.1 takemura return 0;
101 1.1 takemura mask = PLATID_DEREF(match->cf_loc[HPCIOIFCF_PLATFORM]);
102 1.1 takemura if (!platid_match(&platid, &mask))
103 1.1 takemura return 0;
104 1.1 takemura return 1;
105 1.1 takemura }
106 1.1 takemura
107 1.1 takemura void
108 1.1 takemura pwctl_vrgiu_attach(parent, self, aux)
109 1.1 takemura struct device *parent;
110 1.1 takemura struct device *self;
111 1.1 takemura void *aux;
112 1.1 takemura {
113 1.1 takemura struct hpcio_attach_args *haa = aux;
114 1.1 takemura int *loc;
115 1.1 takemura struct pwctl_vrgiu_softc *sc = (void*)self;
116 1.1 takemura
117 1.1 takemura sc->sc_hc = (*haa->haa_getchip)(haa->haa_sc, VRIP_IOCHIP_VRGIU);
118 1.1 takemura
119 1.1 takemura loc = sc->sc_dev.dv_cfdata->cf_loc;
120 1.1 takemura sc->sc_port = loc[HPCIOIFCF_PORT];
121 1.1 takemura sc->sc_id = loc[HPCIOIFCF_ID];
122 1.1 takemura sc->sc_on = loc[HPCIOIFCF_ACTIVE] ? 1 : 0;
123 1.1 takemura sc->sc_off = loc[HPCIOIFCF_ACTIVE] ? 0 : 1;
124 1.1 takemura sc->sc_initvalue = loc[HPCIOIFCF_INITVALUE];
125 1.1 takemura printf(" port=%d id=%ld on=%d%s",
126 1.1 takemura sc->sc_port, sc->sc_id, sc->sc_on,
127 1.1 takemura sc->sc_initvalue == -1 ? "" :
128 1.1 takemura sc->sc_initvalue ? " init=on" : " init=off");
129 1.1 takemura
130 1.1 takemura if (sc->sc_port == HPCIOIFCF_PORT_DEFAULT ||
131 1.1 takemura sc->sc_id == HPCIOIFCF_ID_DEFAULT) {
132 1.1 takemura printf(" (ignored)");
133 1.1 takemura } else {
134 1.1 takemura sc->sc_hook_tag = config_hook(CONFIG_HOOK_POWERCONTROL,
135 1.1 takemura sc->sc_id, CONFIG_HOOK_SHARE,
136 1.1 takemura pwctl_vrgiu_hook, sc);
137 1.1 takemura sc->sc_ghook_tag = config_hook(CONFIG_HOOK_GET,
138 1.1 takemura sc->sc_id, CONFIG_HOOK_SHARE,
139 1.1 takemura pwctl_vrgiu_ghook, sc);
140 1.1 takemura sc->sc_hook_hardpower = config_hook(CONFIG_HOOK_PMEVENT,
141 1.1 takemura CONFIG_HOOK_PMEVENT_HARDPOWER,
142 1.1 takemura CONFIG_HOOK_SHARE,
143 1.1 takemura pwctl_vrgiu_hardpower, sc);
144 1.1 takemura
145 1.1 takemura }
146 1.1 takemura if (sc->sc_initvalue != -1)
147 1.1 takemura hpcio_portwrite(sc->sc_hc, sc->sc_port,
148 1.1 takemura sc->sc_initvalue ? sc->sc_on : sc->sc_off);
149 1.1 takemura printf("\n");
150 1.1 takemura }
151 1.1 takemura
152 1.1 takemura int
153 1.1 takemura pwctl_vrgiu_hook(ctx, type, id, msg)
154 1.1 takemura void *ctx;
155 1.1 takemura int type;
156 1.1 takemura long id;
157 1.1 takemura void *msg;
158 1.1 takemura {
159 1.1 takemura struct pwctl_vrgiu_softc *sc = ctx;
160 1.1 takemura
161 1.1 takemura DPRINTF(("pwctl hook: port %d %s(%d)", sc->sc_port,
162 1.1 takemura msg ? "ON" : "OFF", msg ? sc->sc_on : sc->sc_off));
163 1.1 takemura hpcio_portwrite(sc->sc_hc, sc->sc_port,
164 1.1 takemura msg ? sc->sc_on : sc->sc_off);
165 1.1 takemura return (0);
166 1.1 takemura }
167 1.1 takemura
168 1.1 takemura int
169 1.1 takemura pwctl_vrgiu_ghook(ctx, type, id, msg)
170 1.1 takemura void *ctx;
171 1.1 takemura int type;
172 1.1 takemura long id;
173 1.1 takemura void *msg;
174 1.1 takemura {
175 1.1 takemura struct pwctl_vrgiu_softc *sc = ctx;
176 1.1 takemura
177 1.1 takemura if (CONFIG_HOOK_VALUEP(msg))
178 1.1 takemura return 1;
179 1.1 takemura
180 1.1 takemura *(int*)msg = hpcio_portread(sc->sc_hc, sc->sc_port) == sc->sc_on;
181 1.1 takemura DPRINTF(("pwctl ghook: port %d %s(%d)", sc->sc_port,
182 1.1 takemura *(int*)msg? "ON" : "OFF", *(int*)msg ? sc->sc_on : sc->sc_off));
183 1.1 takemura return 0;
184 1.1 takemura }
185 1.1 takemura
186 1.1 takemura int
187 1.1 takemura pwctl_vrgiu_hardpower(ctx, type, id, msg)
188 1.1 takemura void *ctx;
189 1.1 takemura int type;
190 1.1 takemura long id;
191 1.1 takemura void *msg;
192 1.1 takemura {
193 1.1 takemura struct pwctl_vrgiu_softc *sc = ctx;
194 1.1 takemura int why =(int)msg;
195 1.1 takemura
196 1.1 takemura #if 0
197 1.1 takemura /* XXX debug print cause hang system... Huum...*/
198 1.1 takemura DPRINTF(("pwctl hardpower: port %d %s: %s(%d)\n", sc->sc_port,
199 1.1 takemura why == PWR_RESUME? "resume"
200 1.1 takemura : why == PWR_SUSPEND? "suspend" : "standby",
201 1.1 takemura sc->sc_save == sc->sc_on ? "on": "off", sc->sc_save));
202 1.1 takemura #endif /* 0 */
203 1.1 takemura switch (why) {
204 1.1 takemura case PWR_STANDBY:
205 1.1 takemura break;
206 1.1 takemura case PWR_SUSPEND:
207 1.1 takemura sc->sc_save = hpcio_portread(sc->sc_hc, sc->sc_port);
208 1.1 takemura hpcio_portwrite(sc->sc_hc, sc->sc_port, sc->sc_off);
209 1.1 takemura break;
210 1.1 takemura case PWR_RESUME:
211 1.1 takemura hpcio_portwrite(sc->sc_hc, sc->sc_port, sc->sc_save);
212 1.1 takemura break;
213 1.1 takemura }
214 1.1 takemura return (0);
215 1.1 takemura }
216