xref: /netbsd-src/sys/dev/hpc/pwctl.c (revision 387ddeb6f12e5d0ed394556b54353322afa6df30)
1 /*	$NetBSD: pwctl.c,v 1.6 2001/07/31 10:37:49 sato 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 #include <sys/reboot.h>
43 
44 #include <machine/bus.h>
45 #include <machine/config_hook.h>
46 #include <machine/platid.h>
47 #include <machine/platid_mask.h>
48 
49 #include <dev/hpc/hpciovar.h>
50 
51 #include "locators.h"
52 
53 #define PWCTLVRGIUDEBUG
54 #ifdef PWCTLVRGIUDEBUG
55 int	pwctl_debug = 1;
56 #define	DPRINTF(arg) if (pwctl_debug) printf arg;
57 #define	VPRINTF(arg) if (bootverbose) printf arg;
58 #else
59 #define	DPRINTF(arg)
60 #define	VPRINTF(arg) if (bootverbose) printf arg;
61 #endif
62 
63 struct pwctl_softc {
64 	struct device sc_dev;
65 	hpcio_chip_t sc_hc;
66 	int sc_port;
67 	long sc_id;
68 	int sc_on, sc_off;
69 	config_hook_tag sc_hook_tag;
70 	config_hook_tag sc_hook_hardpower;
71 	config_hook_tag sc_ghook_tag;
72 	int sc_save;
73 	int sc_initvalue;
74 };
75 
76 static int	pwctl_match(struct device *, struct cfdata *, void *);
77 static void	pwctl_attach(struct device *, struct device *, void *);
78 static int	pwctl_hook(void *, int, long, void *);
79 static int	pwctl_ghook(void *, int, long, void *);
80 int	pwctl_hardpower(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(struct device *parent, struct cfdata *match, void *aux)
88 {
89 	struct hpcio_attach_args *haa = aux;
90 	platid_mask_t mask;
91 
92 	if (strcmp(haa->haa_busname, HPCIO_BUSNAME))
93 		return (0);
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(struct device *parent, struct device *self, void *aux)
104 {
105 	struct hpcio_attach_args *haa = aux;
106 	int *loc;
107 	struct pwctl_softc *sc = (void*)self;
108 
109 	loc = sc->sc_dev.dv_cfdata->cf_loc;
110 	sc->sc_hc = (*haa->haa_getchip)(haa->haa_sc, loc[HPCIOIFCF_IOCHIP]);
111 	sc->sc_port = loc[HPCIOIFCF_PORT];
112 	sc->sc_id = loc[HPCIOIFCF_ID];
113 	sc->sc_on = loc[HPCIOIFCF_ACTIVE] ? 1 : 0;
114 	sc->sc_off = loc[HPCIOIFCF_ACTIVE] ? 0 : 1;
115 	sc->sc_initvalue = loc[HPCIOIFCF_INITVALUE];
116 
117 	printf(" port=%d id=%ld on=%d%s",
118 	    sc->sc_port, sc->sc_id, sc->sc_on,
119 	    sc->sc_initvalue == -1 ? "" :
120 	    sc->sc_initvalue ? " init=on" : " init=off");
121 
122 	if (sc->sc_port == HPCIOIFCF_PORT_DEFAULT ||
123 	    sc->sc_id == HPCIOIFCF_ID_DEFAULT) {
124 		printf(" (ignored)");
125 	} else {
126 		sc->sc_hook_tag = config_hook(CONFIG_HOOK_POWERCONTROL,
127 		    sc->sc_id, CONFIG_HOOK_SHARE, pwctl_hook, sc);
128 		sc->sc_ghook_tag = config_hook(CONFIG_HOOK_GET,
129 		    sc->sc_id, CONFIG_HOOK_SHARE, pwctl_ghook, sc);
130 		sc->sc_hook_hardpower = config_hook(CONFIG_HOOK_PMEVENT,
131 		    CONFIG_HOOK_PMEVENT_HARDPOWER, CONFIG_HOOK_SHARE,
132 		    pwctl_hardpower, sc);
133 	}
134 
135 	if (sc->sc_initvalue != -1)
136 		hpcio_portwrite(sc->sc_hc, sc->sc_port,
137 		    sc->sc_initvalue ? sc->sc_on : sc->sc_off);
138 	printf("\n");
139 }
140 
141 int
142 pwctl_hook(void *ctx, int type, long id, void *msg)
143 {
144 	struct pwctl_softc *sc = ctx;
145 
146 	DPRINTF(("pwctl hook: port %d %s(%d)", sc->sc_port,
147 	    msg ? "ON" : "OFF", msg ? sc->sc_on : sc->sc_off));
148 	hpcio_portwrite(sc->sc_hc, sc->sc_port,
149 	    msg ? sc->sc_on : sc->sc_off);
150 
151 	return (0);
152 }
153 
154 int
155 pwctl_ghook(void *ctx, int type, long id, void *msg)
156 {
157 	struct pwctl_softc *sc = ctx;
158 
159 	if (CONFIG_HOOK_VALUEP(msg))
160 		return (1);
161 
162 	*(int*)msg = hpcio_portread(sc->sc_hc, sc->sc_port) == sc->sc_on;
163 	DPRINTF(("pwctl ghook: port %d %s(%d)", sc->sc_port,
164 	    *(int*)msg? "ON" : "OFF", *(int*)msg ? sc->sc_on : sc->sc_off));
165 
166 	return (0);
167 }
168 
169 int
170 pwctl_hardpower(void *ctx, int type, long id, void *msg)
171 {
172 	struct pwctl_softc *sc = ctx;
173 	int why =(int)msg;
174 
175 	VPRINTF(("pwctl hardpower: port %d %s: %s(%d)\n", sc->sc_port,
176 	    why == PWR_RESUME? "resume"
177 	    : why == PWR_SUSPEND? "suspend" : "standby",
178 	    sc->sc_save == sc->sc_on ? "on": "off", sc->sc_save));
179 
180 	switch (why) {
181 	case PWR_STANDBY:
182 		break;
183 	case PWR_SUSPEND:
184 		sc->sc_save = hpcio_portread(sc->sc_hc, sc->sc_port);
185 		hpcio_portwrite(sc->sc_hc, sc->sc_port, sc->sc_off);
186 		break;
187 	case PWR_RESUME:
188 		hpcio_portwrite(sc->sc_hc, sc->sc_port, sc->sc_save);
189 		break;
190 	}
191 
192 	return (0);
193 }
194