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