Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/dev/i2c implement fan control, get calibration data from...



details:   https://anonhg.NetBSD.org/src/rev/40365215fd41
branches:  trunk
changeset: 831221:40365215fd41
user:      macallan <macallan%NetBSD.org@localhost>
date:      Fri Mar 16 22:11:53 2018 +0000

description:
implement fan control, get calibration data from EEPROM via uni_n

diffstat:

 sys/dev/i2c/fcu.c     |  310 +++++++++++++++++++++++++++++++++++++++++++++++++-
 sys/dev/i2c/files.i2c |    4 +-
 2 files changed, 310 insertions(+), 4 deletions(-)

diffs (truncated from 414 to 300 lines):

diff -r a856362f251d -r 40365215fd41 sys/dev/i2c/fcu.c
--- a/sys/dev/i2c/fcu.c Fri Mar 16 22:10:31 2018 +0000
+++ b/sys/dev/i2c/fcu.c Fri Mar 16 22:11:53 2018 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: fcu.c,v 1.1 2018/03/08 23:25:56 macallan Exp $ */
+/* $NetBSD: fcu.c,v 1.2 2018/03/16 22:11:53 macallan Exp $ */
 
 /*-
  * Copyright (c) 2018 Michael Lorenz
@@ -27,13 +27,14 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: fcu.c,v 1.1 2018/03/08 23:25:56 macallan Exp $");
+__KERNEL_RCSID(0, "$NetBSD: fcu.c,v 1.2 2018/03/16 22:11:53 macallan Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
 #include <sys/device.h>
 #include <sys/conf.h>
 #include <sys/bus.h>
+#include <sys/kthread.h>
 
 #include <dev/i2c/i2cvar.h>
 
@@ -41,6 +42,13 @@
 
 #include <dev/ofw/openfirm.h>
 
+//#define FCU_DEBUG
+#ifdef FCU_DEBUG
+#define DPRINTF printf
+#else
+#define DPRINTF if (0) printf
+#endif
+
 /* FCU registers, from OpenBSD's fcu.c */
 #define FCU_FAN_FAIL   0x0b            /* fans states in bits 0<1-6>7 */
 #define FCU_FAN_ACTIVE 0x0d
@@ -50,6 +58,29 @@
 #define FCU_PWM_ACTIVE 0x2d
 #define FCU_PWMREAD(x) 0x30 + (x)*2
 
+#define FCU_MAX_FANS 10
+
+typedef struct _fcu_zone {
+       bool (*filter)(const envsys_data_t *);
+       int nfans;
+       int fans[FCU_MAX_FANS];
+       int threshold;
+} fcu_zone_t; 
+
+typedef struct _fcu_fan {
+       int target;
+       int reg;
+       int base_rpm, max_rpm;
+       int step;
+       int duty;       /* for pwm fans */
+} fcu_fan_t;
+
+#define FCU_ZONE_CPU_A         0
+#define FCU_ZONE_CPU_B         1
+#define FCU_ZONE_CASE          2
+#define FCU_ZONE_DRIVEBAY      3
+#define FCU_ZONE_COUNT         4
+
 struct fcu_softc {
        device_t        sc_dev;
        i2c_tag_t       sc_i2c;
@@ -58,6 +89,13 @@
        struct sysmon_envsys *sc_sme;
        envsys_data_t   sc_sensors[32];
        int             sc_nsensors;
+       fcu_zone_t      sc_zones[FCU_ZONE_COUNT];
+       fcu_fan_t       sc_fans[FCU_MAX_FANS];
+       int             sc_nfans;
+       lwp_t           *sc_thread;
+       bool            sc_dying, sc_pwm;
+       uint8_t         sc_eeprom0[160];
+       uint8_t         sc_eeprom1[160];
 };
 
 static int     fcu_match(device_t, cfdata_t, void *);
@@ -65,6 +103,15 @@
 
 static void    fcu_sensors_refresh(struct sysmon_envsys *, envsys_data_t *);
 
+static bool is_cpu_a(const envsys_data_t *);
+static bool is_cpu_b(const envsys_data_t *);
+static bool is_case(const envsys_data_t *);
+static bool is_drive(const envsys_data_t *);
+
+static void fcu_set_fan_rpm(struct fcu_softc *, fcu_fan_t *, int);
+static void fcu_adjust_zone(struct fcu_softc *, int);
+static void fcu_adjust(void *);
+
 CFATTACH_DECL_NEW(fcu, sizeof(struct fcu_softc),
     fcu_match, fcu_attach, NULL, NULL);
 
@@ -93,6 +140,7 @@
 {
        struct fcu_softc *sc = device_private(self);
        struct i2c_attach_args *ia = aux;
+       int have_eeprom1 = 1;
 
        sc->sc_dev = self;
        sc->sc_i2c = ia->ia_tag;
@@ -101,6 +149,32 @@
        aprint_naive("\n");
        aprint_normal(": Fan Control Unit\n");
 
+       if (get_cpuid(0, sc->sc_eeprom0) < 160) {
+               /*
+                * XXX this should never happen, we depend on the EEPROM for
+                * calibration data to make sense of temperature and voltage
+                * sensors elsewhere, and fan parameters here.
+                */
+               aprint_error_dev(self, "no EEPROM data for CPU 0\n");
+               return;
+       }
+       if (get_cpuid(1, sc->sc_eeprom1) < 160)
+               have_eeprom1 = 0;
+
+       /* init zones */
+       sc->sc_zones[FCU_ZONE_CPU_A].filter = is_cpu_a;
+       sc->sc_zones[FCU_ZONE_CPU_A].threshold = 45;
+       sc->sc_zones[FCU_ZONE_CPU_A].nfans = 0;
+       sc->sc_zones[FCU_ZONE_CPU_B].filter = is_cpu_b;
+       sc->sc_zones[FCU_ZONE_CPU_B].threshold = 45;
+       sc->sc_zones[FCU_ZONE_CPU_B].nfans = 0;
+       sc->sc_zones[FCU_ZONE_CASE].filter = is_case;
+       sc->sc_zones[FCU_ZONE_CASE].threshold = 50;
+       sc->sc_zones[FCU_ZONE_CASE].nfans = 0;
+       sc->sc_zones[FCU_ZONE_DRIVEBAY].filter = is_drive;
+       sc->sc_zones[FCU_ZONE_DRIVEBAY].threshold = 30;
+       sc->sc_zones[FCU_ZONE_DRIVEBAY].nfans = 0;
+
        sc->sc_sme = sysmon_envsys_create();
        sc->sc_sme->sme_name = device_xname(self);
        sc->sc_sme->sme_cookie = sc;
@@ -108,6 +182,7 @@
 
        sc->sc_sensors[0].units = ENVSYS_SFANRPM;
        sc->sc_sensors[1].state = ENVSYS_SINVALID;
+       sc->sc_nfans = 0;
 
        /* round up sensors */
        int ch;
@@ -148,12 +223,91 @@
                        goto next;
                strcpy(s->desc, descr);
 
+               if (s->units == ENVSYS_SFANRPM) {
+                       fcu_fan_t *fan = &sc->sc_fans[sc->sc_nfans];
+                       uint8_t *eeprom = NULL;
+                       uint16_t rmin, rmax;
+
+                       if (strstr(descr, "CPU A") != NULL)
+                               eeprom = sc->sc_eeprom0;
+                       if (strstr(descr, "CPU B") != NULL) {
+                               /*
+                                * XXX
+                                * this should never happen
+                                */
+                               if (have_eeprom1 == 0) {
+                                       eeprom = sc->sc_eeprom0;
+                               } else
+                                       eeprom = sc->sc_eeprom1;
+                       }
+
+                       fan->reg = reg;
+                       fan->target = 0;
+                       fan->duty = 0x80;
+
+                       /* speed settings from EEPROM */
+                       if (strstr(descr, "PUMP") != NULL) {
+                               KASSERT(eeprom != NULL);
+                               memcpy(&rmin, &eeprom[0x54], 2);
+                               memcpy(&rmax, &eeprom[0x56], 2);
+                               fan->base_rpm = rmin;
+                               fan->max_rpm = rmax;
+                               fan->step = (rmax - rmin) / 30;
+                       } else if (strstr(descr, "INTAKE") != NULL) {
+                               KASSERT(eeprom != NULL);
+                               memcpy(&rmin, &eeprom[0x4c], 2);
+                               memcpy(&rmax, &eeprom[0x5e], 2);
+                               fan->base_rpm = rmin;
+                               fan->max_rpm = rmax;
+                               fan->step = (rmax - rmin) / 30;
+                       } else if (strstr(descr, "EXHAUST") != NULL) {
+                               KASSERT(eeprom != NULL);
+                               memcpy(&rmin, &eeprom[0x50], 2);
+                               memcpy(&rmax, &eeprom[0x52], 2);
+                               fan->base_rpm = rmin;
+                               fan->max_rpm = rmax;
+                               fan->step = (rmax - rmin) / 30;
+                       } else if (strstr(descr, "DRIVE") != NULL ) {
+                               fan->base_rpm = 1000;
+                               fan->max_rpm = 3000;
+                               fan->step = 100;
+                       } else {
+                               fan->base_rpm = 1000;
+                               fan->max_rpm = 3000;
+                               fan->step = 100;
+                       }
+
+                       /* now stuff them into zones */
+                       if (strstr(descr, "CPU A") != NULL) {
+                               fcu_zone_t *z = &sc->sc_zones[FCU_ZONE_CPU_A];
+                               z->fans[z->nfans] = sc->sc_nfans;
+                               z->nfans++;
+                       } else if (strstr(descr, "CPU B") != NULL) {
+                               fcu_zone_t *z = &sc->sc_zones[FCU_ZONE_CPU_B];
+                               z->fans[z->nfans] = sc->sc_nfans;
+                               z->nfans++;
+                       } else if ((strstr(descr, "BACKSIDE") != NULL) ||
+                                  (strstr(descr, "SLOT") != NULL))  {
+                               fcu_zone_t *z = &sc->sc_zones[FCU_ZONE_CASE];
+                               z->fans[z->nfans] = sc->sc_nfans;
+                               z->nfans++;
+                       } else if (strstr(descr, "DRIVE") != NULL) {
+                               fcu_zone_t *z = &sc->sc_zones[FCU_ZONE_DRIVEBAY];
+                               z->fans[z->nfans] = sc->sc_nfans;
+                               z->nfans++;
+                       }
+                       sc->sc_nfans++;
+               }
                sysmon_envsys_sensor_attach(sc->sc_sme, s);
                sc->sc_nsensors++;
 next:
                ch = OF_peer(ch);
        }               
        sysmon_envsys_register(sc->sc_sme);
+
+       sc->sc_dying = FALSE;
+       kthread_create(PRI_NONE, 0, curcpu(), fcu_adjust, sc, &sc->sc_thread,
+           "fan control"); 
 }
 
 static void
@@ -198,3 +352,155 @@
                        edata->state = ENVSYS_SINVALID;
        }       
 }
+
+static bool
+is_cpu_a(const envsys_data_t *edata)
+{
+       if (edata->units != ENVSYS_STEMP)
+               return false;
+       if (strstr(edata->desc, "CPU A") != NULL)
+               return TRUE;
+       return false;
+}
+
+static bool
+is_cpu_b(const envsys_data_t *edata)
+{
+       if (edata->units != ENVSYS_STEMP)
+               return false;
+       if (strstr(edata->desc, "CPU B") != NULL)
+               return TRUE;
+       return false;
+}
+
+static bool
+is_case(const envsys_data_t *edata)
+{
+       if (edata->units != ENVSYS_STEMP)
+               return false;
+       if ((strstr(edata->desc, "MLB") != NULL) ||
+           (strstr(edata->desc, "BACKSIDE") != NULL) ||
+           (strstr(edata->desc, "U3") != NULL))
+               return TRUE;
+       return false;
+}
+
+static bool
+is_drive(const envsys_data_t *edata)
+{
+       if (edata->units != ENVSYS_STEMP)
+               return false;
+       if (strstr(edata->desc, "DRIVE") != NULL)
+               return TRUE;
+       return false;
+}
+
+static void
+fcu_set_fan_rpm(struct fcu_softc *sc, fcu_fan_t *f, int speed)
+{
+       int error;
+       uint8_t cmd;
+
+       if (f->reg < 0x30) {
+               uint16_t data;
+               /* simple rpm fan, just poke the register */
+
+               if (f->target == speed) return;
+               speed = min(speed, f->max_rpm);
+               speed = max(speed, f->base_rpm);



Home | Main Index | Thread Index | Old Index