diff options
| author | macallan <macallan@NetBSD.org> | 2018-03-16 22:11:53 +0000 |
|---|---|---|
| committer | macallan <macallan@NetBSD.org> | 2018-03-16 22:11:53 +0000 |
| commit | 384d36dfb4b5b222d33bccf3510ea2883a5b97f2 (patch) | |
| tree | ddf33eeff9d95bb8c2eb56660ca44242f706ea1b /sys/dev | |
| parent | cfb7499e6bd6e650d4191d34f4b8966864365ae1 (diff) | |
implement fan control, get calibration data from EEPROM via uni_n
Diffstat (limited to 'sys/dev')
| -rw-r--r-- | sys/dev/i2c/fcu.c | 310 | ||||
| -rw-r--r-- | sys/dev/i2c/files.i2c | 4 |
2 files changed, 310 insertions, 4 deletions
diff --git a/sys/dev/i2c/fcu.c b/sys/dev/i2c/fcu.c index c8db1161197..6a93f8f9cd6 100644 --- a/sys/dev/i2c/fcu.c +++ b/sys/dev/i2c/fcu.c @@ -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 @@ __KERNEL_RCSID(0, "$NetBSD: fcu.c,v 1.1 2018/03/08 23:25:56 macallan Exp $"); #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 @@ __KERNEL_RCSID(0, "$NetBSD: fcu.c,v 1.1 2018/03/08 23:25:56 macallan Exp $"); #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 fcu_softc { 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_attach(device_t, device_t, void *); 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 @@ fcu_attach(device_t parent, device_t self, void *aux) { 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 @@ fcu_attach(device_t parent, device_t self, void *aux) 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 @@ fcu_attach(device_t parent, device_t self, void *aux) 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 @@ fcu_attach(device_t parent, device_t self, void *aux) 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 @@ fcu_sensors_refresh(struct sysmon_envsys *sme, envsys_data_t *edata) 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); + iic_acquire_bus(sc->sc_i2c, 0); + cmd = f->reg; + data = (speed << 3); + error = iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP, + sc->sc_addr, &cmd, 1, &data, 2, 0); + iic_release_bus(sc->sc_i2c, 0); + } else { + int diff; + int nduty = f->duty; + uint16_t data; + /* pwm fan, measure speed, then adjust duty cycle */ + DPRINTF("pwm fan "); + iic_acquire_bus(sc->sc_i2c, 0); + cmd = f->reg + 1; + error = iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP, + sc->sc_addr, &cmd, 1, &data, 2, 0); + data = data >> 3; + diff = data - speed; + DPRINTF("d %d s %d t %d diff %d ", f->duty, data, speed, diff); + if (diff > 100) { + nduty = max(20, nduty - 1); + } + if (diff < -100) { + nduty = min(0xd0, nduty + 1); + } + cmd = f->reg; + DPRINTF("%s nduty %d", __func__, nduty); + if (nduty != f->duty) { + uint8_t arg = nduty; + error = iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP, + sc->sc_addr, &cmd, 1, &arg, 1, 0); + f->duty = nduty; + sc->sc_pwm = TRUE; + + } + iic_release_bus(sc->sc_i2c, 0); + DPRINTF("ok\n"); + } + if (error) printf("boo\n"); + f->target = speed; +} + +static void +fcu_adjust_zone(struct fcu_softc *sc, int which) +{ + fcu_zone_t *z = &sc->sc_zones[which]; + fcu_fan_t *f; + int temp, i, speed, diff; + + + if (z->nfans <= 0) + return; + + temp = sysmon_envsys_get_max_value(z->filter, true); + if (temp == 0) { + /* no sensor data - leave fan alone */ + DPRINTF("nodata\n"); + return; + } + + temp = (temp - 273150000) / 1000000; + diff = (temp - z->threshold); + + /* now adjust each fan to the new duty cycle */ + for (i = 0; i < z->nfans; i++) { + if (z->fans[i] > 8) { + printf("wtf?!\n"); + continue; + } + f = &sc->sc_fans[z->fans[i]]; + speed = f->base_rpm + diff * f->step; + fcu_set_fan_rpm(sc, f, speed); + } +} + +static void +fcu_adjust(void *cookie) +{ + struct fcu_softc *sc = cookie; + int i; + uint8_t cmd, data; + + while (!sc->sc_dying) { + /* poke the FCU so we don't go 747 */ + iic_acquire_bus(sc->sc_i2c, 0); + cmd = FCU_FAN_ACTIVE; + iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP, + sc->sc_addr, &cmd, 1, &data, 1, 0); + iic_release_bus(sc->sc_i2c, 0); + sc->sc_pwm = FALSE; + for (i = 0; i < FCU_ZONE_COUNT; i++) + fcu_adjust_zone(sc, i); + kpause("fanctrl", true, mstohz(sc->sc_pwm ? 2000 : 30000), NULL); + } + kthread_exit(0); +} diff --git a/sys/dev/i2c/files.i2c b/sys/dev/i2c/files.i2c index 2df610a0163..e97c711b6a7 100644 --- a/sys/dev/i2c/files.i2c +++ b/sys/dev/i2c/files.i2c @@ -1,4 +1,4 @@ -# $NetBSD: files.i2c,v 1.86 2018/03/09 20:16:54 macallan Exp $ +# $NetBSD: files.i2c,v 1.87 2018/03/16 22:11:53 macallan Exp $ obsolete defflag opt_i2cbus.h I2C_SCAN define i2cbus { } @@ -307,7 +307,7 @@ file dev/i2c/em3027.c em3027rtc # Apple Fan Control Unit found in some G5 device fcu: sysmon_envsys attach fcu at iic -file dev/i2c/fcu.c fcu +file dev/i2c/fcu.c fcu needs-flag # Analog Devices AD7417 thermometer and ADC device adadc: sysmon_envsys |
