Home | History | Annotate | Line # | Download | only in dev
      1 /*	$NetBSD: apc.c,v 1.3 2025/05/31 15:14:40 jdc Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2010 Manuel Bouyer.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25  */
     26 
     27 #include <sys/cdefs.h>
     28 __KERNEL_RCSID(0, "$NetBSD: apc.c,v 1.3 2025/05/31 15:14:40 jdc Exp $");
     29 
     30 
     31 /*
     32  * driver for the power management functions of the Aurora Personality Chip
     33  * on SPARCstation-4/5 and derivatives
     34  */
     35 
     36 #include <sys/param.h>
     37 #include <sys/systm.h>
     38 #include <sys/errno.h>
     39 #include <sys/device.h>
     40 #include <sys/bus.h>
     41 
     42 #include <machine/autoconf.h>
     43 
     44 #include <sparc/dev/apcreg.h>
     45 
     46 static int apcmatch(device_t, struct cfdata *, void *);
     47 static void apcattach(device_t, device_t, void *);
     48 static void apc_cpu_sleep(void);
     49 
     50 struct apc_softc {
     51 	device_t	sc_dev;
     52 	bus_space_tag_t	sc_bt;
     53 	bus_space_handle_t sc_bh;
     54 };
     55 
     56 struct apc_softc *apc = NULL;
     57 
     58 CFATTACH_DECL_NEW(apc, sizeof(struct apc_softc),
     59     apcmatch, apcattach, NULL, NULL);
     60 
     61 
     62 static int
     63 apcmatch(device_t parent, struct cfdata *cf, void *aux)
     64 {
     65 	struct sbus_attach_args *sa = aux;
     66 	if (apc != NULL) /* only one instance */
     67 		return 0;
     68 	return strcmp("power-management", sa->sa_name) == 0;
     69 }
     70 
     71 static void
     72 apcattach(device_t parent, device_t self, void *aux)
     73 {
     74 	struct sbus_attach_args *sa = aux;
     75 	struct apc_softc *sc = device_private(self);
     76 #ifdef MULTIPROCESSOR
     77 	struct cpu_info *cpi;
     78 	CPU_INFO_ITERATOR n;
     79 #endif
     80 
     81 	sc->sc_bt = sa->sa_bustag;
     82 	if (sbus_bus_map(sa->sa_bustag,
     83 	    sa->sa_slot, sa->sa_offset, APC_REG_SIZE, 0, &sc->sc_bh) != 0) {
     84 		aprint_error(": cannot map registers\n");
     85 		return;
     86 	}
     87 	aprint_normal("\n");
     88 	apc = sc;
     89 #ifdef MULTIPROCESSOR
     90 	for (CPU_INFO_FOREACH(n, cpi)) {
     91 		cpi->idlespin = apc_cpu_sleep;
     92 	}
     93 #else
     94 	curcpu()->idlespin = apc_cpu_sleep;
     95 #endif
     96 }
     97 
     98 static void
     99 apc_cpu_sleep(void)
    100 {
    101 	uint8_t val;
    102 	if (apc == NULL)
    103 		return;
    104 	val = bus_space_read_1(apc->sc_bt, apc->sc_bh, APC_IDLE_REG);
    105 	bus_space_write_1(apc->sc_bt, apc->sc_bh, APC_IDLE_REG,
    106 	    val | APC_IDLE_REG_IDLE);
    107 }
    108