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