xref: /netbsd-src/sys/dev/hpc/pwctl.c (revision 26b0905dd0c60304b307826bd4d268045b737458)
1 /*	$NetBSD: pwctl.c,v 1.2 2001/04/30 11:42:17 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/config_hook.h>
44 #include <machine/platid.h>
45 #include <machine/platid_mask.h>
46 
47 #include <dev/hpc/hpciovar.h>
48 
49 #include "locators.h"
50 
51 #define PWCTLVRGIUDEBUG
52 #ifdef PWCTLVRGIUDEBUG
53 int	pwctl_debug = 0;
54 #define	DPRINTF(arg) if (pwctl_debug) printf arg;
55 #else
56 #define	DPRINTF(arg)
57 #endif
58 
59 struct pwctl_softc {
60 	struct device sc_dev;
61 	hpcio_chip_t sc_hc;
62 	int sc_port;
63 	long sc_id;
64 	int sc_on, sc_off;
65 	config_hook_tag sc_hook_tag;
66 	config_hook_tag sc_hook_hardpower;
67 	config_hook_tag sc_ghook_tag;
68 	int sc_save;
69 	int sc_initvalue;
70 };
71 
72 static int	pwctl_match __P((struct device *, struct cfdata *,
73 				       void *));
74 static void	pwctl_attach __P((struct device *, struct device *,
75 					void *));
76 static int	pwctl_hook __P((void *ctx, int type, long id,
77 				      void *msg));
78 static int	pwctl_ghook __P((void *ctx, int type, long id,
79 				      void *msg));
80 int	pwctl_hardpower __P((void *, int, long, void *));
81 
82 struct cfattach pwctl_ca = {
83 	sizeof(struct pwctl_softc), pwctl_match, pwctl_attach
84 };
85 
86 int
87 pwctl_match(parent, match, aux)
88 	struct device *parent;
89 	struct cfdata *match;
90 	void *aux;
91 {
92 	platid_mask_t mask;
93 
94 	if (match->cf_loc[HPCIOIFCF_PLATFORM] == 0)
95 		return 0;
96 	mask = PLATID_DEREF(match->cf_loc[HPCIOIFCF_PLATFORM]);
97 	if (!platid_match(&platid, &mask))
98 		return 0;
99 	return 1;
100 }
101 
102 void
103 pwctl_attach(parent, self, aux)
104 	struct device *parent;
105 	struct device *self;
106 	void *aux;
107 {
108 	struct hpcio_attach_args *haa = aux;
109 	int *loc;
110 	struct pwctl_softc *sc = (void*)self;
111 
112 	loc = sc->sc_dev.dv_cfdata->cf_loc;
113 	sc->sc_hc = (*haa->haa_getchip)(haa->haa_sc, loc[HPCIOIFCF_IOCHIP]);
114 	sc->sc_port = loc[HPCIOIFCF_PORT];
115 	sc->sc_id = loc[HPCIOIFCF_ID];
116 	sc->sc_on = loc[HPCIOIFCF_ACTIVE] ? 1 : 0;
117 	sc->sc_off = loc[HPCIOIFCF_ACTIVE] ? 0 : 1;
118 	sc->sc_initvalue = loc[HPCIOIFCF_INITVALUE];
119 	printf(" port=%d id=%ld on=%d%s",
120 		 sc->sc_port, sc->sc_id, sc->sc_on,
121 		 sc->sc_initvalue == -1 ? "" :
122 		 sc->sc_initvalue ? " init=on" : " init=off");
123 
124 	if (sc->sc_port == HPCIOIFCF_PORT_DEFAULT ||
125 	    sc->sc_id == HPCIOIFCF_ID_DEFAULT) {
126 		printf(" (ignored)");
127 	} else {
128 		sc->sc_hook_tag = config_hook(CONFIG_HOOK_POWERCONTROL,
129 					      sc->sc_id, CONFIG_HOOK_SHARE,
130 					      pwctl_hook, sc);
131 		sc->sc_ghook_tag = config_hook(CONFIG_HOOK_GET,
132 					      sc->sc_id, CONFIG_HOOK_SHARE,
133 					      pwctl_ghook, sc);
134 		sc->sc_hook_hardpower = config_hook(CONFIG_HOOK_PMEVENT,
135 						CONFIG_HOOK_PMEVENT_HARDPOWER,
136 						CONFIG_HOOK_SHARE,
137 						pwctl_hardpower, sc);
138 
139 	}
140 	if (sc->sc_initvalue != -1)
141 		hpcio_portwrite(sc->sc_hc, sc->sc_port,
142 				sc->sc_initvalue ? sc->sc_on : sc->sc_off);
143 	printf("\n");
144 }
145 
146 int
147 pwctl_hook(ctx, type, id, msg)
148 	void *ctx;
149 	int type;
150 	long id;
151 	void *msg;
152 {
153 	struct pwctl_softc *sc = ctx;
154 
155 	DPRINTF(("pwctl hook: port %d %s(%d)", sc->sc_port,
156 		 msg ? "ON" : "OFF", msg ? sc->sc_on : sc->sc_off));
157 	hpcio_portwrite(sc->sc_hc, sc->sc_port,
158 			msg ? sc->sc_on : sc->sc_off);
159 	return (0);
160 }
161 
162 int
163 pwctl_ghook(ctx, type, id, msg)
164 	void *ctx;
165 	int type;
166 	long id;
167 	void *msg;
168 {
169 	struct pwctl_softc *sc = ctx;
170 
171 	if (CONFIG_HOOK_VALUEP(msg))
172 		return 1;
173 
174 	*(int*)msg = hpcio_portread(sc->sc_hc, sc->sc_port) == sc->sc_on;
175 	DPRINTF(("pwctl ghook: port %d %s(%d)", sc->sc_port,
176 		 *(int*)msg? "ON" : "OFF", *(int*)msg ? sc->sc_on : sc->sc_off));
177 	return 0;
178 }
179 
180 int
181 pwctl_hardpower(ctx, type, id, msg)
182 	void *ctx;
183 	int type;
184 	long id;
185 	void *msg;
186 {
187 	struct pwctl_softc *sc = ctx;
188 	int why =(int)msg;
189 
190 #if 0
191 	/* XXX debug print cause hang system... Huum...*/
192 	DPRINTF(("pwctl hardpower: port %d %s: %s(%d)\n", sc->sc_port,
193 		why == PWR_RESUME? "resume"
194 		: why == PWR_SUSPEND? "suspend" : "standby",
195 		sc->sc_save == sc->sc_on ? "on": "off", sc->sc_save));
196 #endif /* 0 */
197 	switch (why) {
198 	case PWR_STANDBY:
199 		break;
200 	case PWR_SUSPEND:
201 		sc->sc_save = hpcio_portread(sc->sc_hc, sc->sc_port);
202 		hpcio_portwrite(sc->sc_hc, sc->sc_port, sc->sc_off);
203 		break;
204 	case PWR_RESUME:
205 		hpcio_portwrite(sc->sc_hc, sc->sc_port, sc->sc_save);
206 		break;
207 	}
208 	return (0);
209 }
210