Home | History | Annotate | Line # | Download | only in dev
obiofan.c revision 1.1
      1  1.1  macallan /*	$NetBSD: obiofan.c,v 1.1 2021/09/10 23:32:17 macallan Exp $	*/
      2  1.1  macallan /*	Id: ki2c.c,v 1.7 2002/10/05 09:56:05 tsubai Exp	*/
      3  1.1  macallan 
      4  1.1  macallan /*-
      5  1.1  macallan  * Copyright (c) 2006 Michael Lorenz
      6  1.1  macallan  * All rights reserved.
      7  1.1  macallan  *
      8  1.1  macallan  * Redistribution and use in source and binary forms, with or without
      9  1.1  macallan  * modification, are permitted provided that the following conditions
     10  1.1  macallan  * are met:
     11  1.1  macallan  * 1. Redistributions of source code must retain the above copyright
     12  1.1  macallan  *    notice, this list of conditions and the following disclaimer.
     13  1.1  macallan  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1  macallan  *    notice, this list of conditions and the following disclaimer in the
     15  1.1  macallan  *    documentation and/or other materials provided with the distribution.
     16  1.1  macallan  *
     17  1.1  macallan  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     18  1.1  macallan  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     19  1.1  macallan  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     20  1.1  macallan  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     21  1.1  macallan  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     22  1.1  macallan  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     23  1.1  macallan  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     24  1.1  macallan  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     25  1.1  macallan  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     26  1.1  macallan  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     27  1.1  macallan  * POSSIBILITY OF SUCH DAMAGE.
     28  1.1  macallan  */
     29  1.1  macallan 
     30  1.1  macallan #include <sys/param.h>
     31  1.1  macallan #include <sys/device.h>
     32  1.1  macallan #include <sys/systm.h>
     33  1.1  macallan #include <sys/mutex.h>
     34  1.1  macallan #include <sys/callout.h>
     35  1.1  macallan #include <sys/time.h>
     36  1.1  macallan 
     37  1.1  macallan #include <dev/ofw/openfirm.h>
     38  1.1  macallan #include <dev/sysmon/sysmonvar.h>
     39  1.1  macallan #include <machine/autoconf.h>
     40  1.1  macallan #include <macppc/dev/obiovar.h>
     41  1.1  macallan 
     42  1.1  macallan #include "opt_obiofan.h"
     43  1.1  macallan 
     44  1.1  macallan #ifdef OBIOFAN_DEBUG
     45  1.1  macallan #define DPRINTF printf
     46  1.1  macallan #else
     47  1.1  macallan #define DPRINTF if (0) printf
     48  1.1  macallan #endif
     49  1.1  macallan 
     50  1.1  macallan struct obiofan_softc {
     51  1.1  macallan 	device_t sc_dev;
     52  1.1  macallan 	struct sysmon_envsys	*sc_sme;
     53  1.1  macallan 	envsys_data_t		sc_sensors[4];
     54  1.1  macallan 	callout_t		sc_callout;
     55  1.1  macallan 	time_t			sc_stamp;
     56  1.1  macallan 	int 			sc_node;
     57  1.1  macallan 	int 			sc_reg, sc_shift;
     58  1.1  macallan 	int			sc_count, sc_rpm;
     59  1.1  macallan };
     60  1.1  macallan 
     61  1.1  macallan int obiofan_match(device_t, cfdata_t, void *);
     62  1.1  macallan void obiofan_attach(device_t, device_t, void *);
     63  1.1  macallan static void obiofan_refresh(struct sysmon_envsys *, envsys_data_t *);
     64  1.1  macallan static void obiofan_update(void *);
     65  1.1  macallan 
     66  1.1  macallan CFATTACH_DECL_NEW(obiofan, sizeof(struct obiofan_softc), obiofan_match,
     67  1.1  macallan 	obiofan_attach, NULL, NULL);
     68  1.1  macallan 
     69  1.1  macallan int
     70  1.1  macallan obiofan_match(device_t parent, cfdata_t match, void *aux)
     71  1.1  macallan {
     72  1.1  macallan 	struct confargs *ca = aux;
     73  1.1  macallan 
     74  1.1  macallan 	if (strcmp(ca->ca_name, "fans") == 0)
     75  1.1  macallan 		return 1;
     76  1.1  macallan 
     77  1.1  macallan 	return 0;
     78  1.1  macallan }
     79  1.1  macallan 
     80  1.1  macallan void
     81  1.1  macallan obiofan_attach(device_t parent, device_t self, void *aux)
     82  1.1  macallan {
     83  1.1  macallan 	struct obiofan_softc *sc = device_private(self);
     84  1.1  macallan 	struct confargs *ca = aux;
     85  1.1  macallan 	char descr[32] = "fan";
     86  1.1  macallan 	int node = ca->ca_node, tc_node;
     87  1.1  macallan 	int regs[8], tc_regs[32];
     88  1.1  macallan 
     89  1.1  macallan 	printf("\n");
     90  1.1  macallan 
     91  1.1  macallan 	if (OF_getprop(node, "reg", regs, sizeof(regs)) <= 0)
     92  1.1  macallan 		return;
     93  1.1  macallan 
     94  1.1  macallan 	sc->sc_node = node;
     95  1.1  macallan 
     96  1.1  macallan #ifdef OBIOFAN_DEBUG
     97  1.1  macallan 	int i;
     98  1.1  macallan 	for (i = 0; i < 7; i += 2)
     99  1.1  macallan 		printf("%02x: %08x\n", regs[i], obio_read_4(regs[i]));
    100  1.1  macallan #endif
    101  1.1  macallan 
    102  1.1  macallan 	tc_node = OF_parent(node);
    103  1.1  macallan 
    104  1.1  macallan 	if (OF_getprop(tc_node, "platform-do-getTACHCount", tc_regs, 32) <= 0) {
    105  1.1  macallan 		aprint_error("no platform-do-getTACHCount\n");
    106  1.1  macallan 		return;
    107  1.1  macallan 	}
    108  1.1  macallan 
    109  1.1  macallan 	/*
    110  1.1  macallan 	 * XXX this is guesswork based on poking around & observation
    111  1.1  macallan 	 *
    112  1.1  macallan 	 * the parameter format seems to be:
    113  1.1  macallan 	 * reg[0] - node number for the fan, or pointer into OF space
    114  1.1  macallan 	 * reg[1] - 0x08000000
    115  1.1  macallan 	 * reg[2] - 0x0000001a - varies with different platform-do-*
    116  1.1  macallan 	 * reg[3] - register number in obio space
    117  1.1  macallan 	 * reg[4] - bitmask in the register
    118  1.1  macallan 	 * reg[5] - unknown
    119  1.1  macallan 	 * reg[6] - unknown
    120  1.1  macallan 	 *
    121  1.1  macallan 	 * for now only get the parameters for the 1st fan since that's all we
    122  1.1  macallan 	 * have on my 7,3
    123  1.1  macallan 	 */
    124  1.1  macallan 
    125  1.1  macallan 	sc->sc_reg = tc_regs[3];
    126  1.1  macallan 	sc->sc_shift = ffs(tc_regs[4]) - 1;
    127  1.1  macallan 
    128  1.1  macallan 	OF_getprop(tc_regs[0], "location", descr, 32);
    129  1.1  macallan 	DPRINTF("%s: %02x %d\n", descr, sc->sc_reg, sc->sc_shift);
    130  1.1  macallan 
    131  1.1  macallan 	sc->sc_stamp = time_second;
    132  1.1  macallan 	sc->sc_count = obio_read_4(sc->sc_reg) >> sc->sc_shift;
    133  1.1  macallan 	sc->sc_rpm = -1;
    134  1.1  macallan 
    135  1.1  macallan 	sc->sc_sme = sysmon_envsys_create();
    136  1.1  macallan 	sc->sc_sme->sme_name = device_xname(self);
    137  1.1  macallan 	sc->sc_sme->sme_cookie = sc;
    138  1.1  macallan 	sc->sc_sme->sme_refresh = obiofan_refresh;
    139  1.1  macallan 
    140  1.1  macallan 	sc->sc_sensors[0].units = ENVSYS_SFANRPM;
    141  1.1  macallan 	sc->sc_sensors[0].state = ENVSYS_SINVALID;
    142  1.1  macallan 	strcpy(sc->sc_sensors[0].desc, descr);
    143  1.1  macallan 	sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensors[0]);
    144  1.1  macallan 	sysmon_envsys_register(sc->sc_sme);
    145  1.1  macallan 
    146  1.1  macallan 	callout_init(&sc->sc_callout, 0);
    147  1.1  macallan 	callout_setfunc(&sc->sc_callout, obiofan_update, sc);
    148  1.1  macallan 	callout_schedule(&sc->sc_callout, mstohz(5000));
    149  1.1  macallan }
    150  1.1  macallan 
    151  1.1  macallan static void
    152  1.1  macallan obiofan_update(void *cookie)
    153  1.1  macallan {
    154  1.1  macallan 	struct obiofan_softc *sc = cookie;
    155  1.1  macallan 	time_t now = time_second, diff;
    156  1.1  macallan 	int spin, spins;
    157  1.1  macallan 	diff = now - sc->sc_stamp;
    158  1.1  macallan 	if (diff < 5) return;
    159  1.1  macallan 	spin = obio_read_4(sc->sc_reg) >> sc->sc_shift;
    160  1.1  macallan 	spins = (spin - sc->sc_count) & 0xffff;
    161  1.1  macallan 	sc->sc_rpm = spins * 60 / diff;
    162  1.1  macallan 	sc->sc_count = spin;
    163  1.1  macallan 	sc->sc_stamp = now;
    164  1.1  macallan 	DPRINTF("%s %lld %d\n", __func__, diff, spins);
    165  1.1  macallan 	callout_schedule(&sc->sc_callout, mstohz(5000));
    166  1.1  macallan }
    167  1.1  macallan 
    168  1.1  macallan static void
    169  1.1  macallan obiofan_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
    170  1.1  macallan {
    171  1.1  macallan 	struct obiofan_softc *sc = sme->sme_cookie;
    172  1.1  macallan 	if (sc->sc_rpm >= 0) {
    173  1.1  macallan 		edata->state = ENVSYS_SVALID;
    174  1.1  macallan 		edata->value_cur = sc->sc_rpm;
    175  1.1  macallan 	}
    176  1.1  macallan }
    177