Home | History | Annotate | Line # | Download | only in dev
fcu.c revision 1.1
      1 /* $NetBSD: fcu.c,v 1.1 2021/07/27 20:23:41 macallan Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2018 Michael Lorenz
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: fcu.c,v 1.1 2021/07/27 20:23:41 macallan Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/systm.h>
     34 #include <sys/device.h>
     35 #include <sys/conf.h>
     36 #include <sys/bus.h>
     37 #include <sys/kthread.h>
     38 
     39 #include <dev/i2c/i2cvar.h>
     40 
     41 #include <dev/sysmon/sysmonvar.h>
     42 
     43 #include <dev/ofw/openfirm.h>
     44 
     45 //#define FCU_DEBUG
     46 #ifdef FCU_DEBUG
     47 #define DPRINTF printf
     48 #else
     49 #define DPRINTF if (0) printf
     50 #endif
     51 
     52 /* FCU registers, from OpenBSD's fcu.c */
     53 #define FCU_FAN_FAIL	0x0b		/* fans states in bits 0<1-6>7 */
     54 #define FCU_FAN_ACTIVE	0x0d
     55 #define FCU_FANREAD(x)	0x11 + (x)*2
     56 #define FCU_FANSET(x)	0x10 + (x)*2
     57 #define FCU_PWM_FAIL	0x2b
     58 #define FCU_PWM_ACTIVE	0x2d
     59 #define FCU_PWMREAD(x)	0x30 + (x)*2
     60 
     61 #define FCU_MAX_FANS 10
     62 
     63 typedef struct _fcu_zone {
     64 	bool (*filter)(const envsys_data_t *);
     65 	int nfans;
     66 	int fans[FCU_MAX_FANS];
     67 	int threshold;
     68 } fcu_zone_t;
     69 
     70 typedef struct _fcu_fan {
     71 	int target;
     72 	int reg;
     73 	int base_rpm, max_rpm;
     74 	int step;
     75 	int duty;	/* for pwm fans */
     76 } fcu_fan_t;
     77 
     78 #define FCU_ZONE_CPU		0
     79 #define FCU_ZONE_CASE		1
     80 #define FCU_ZONE_DRIVEBAY	2
     81 #define FCU_ZONE_COUNT		3
     82 
     83 struct fcu_softc {
     84 	device_t	sc_dev;
     85 	i2c_tag_t	sc_i2c;
     86 	i2c_addr_t	sc_addr;
     87 
     88 	struct sysmon_envsys *sc_sme;
     89 	envsys_data_t	sc_sensors[32];
     90 	int		sc_nsensors;
     91 	fcu_zone_t	sc_zones[FCU_ZONE_COUNT];
     92 	fcu_fan_t	sc_fans[FCU_MAX_FANS];
     93 	int		sc_nfans;
     94 	lwp_t		*sc_thread;
     95 	bool		sc_dying, sc_pwm;
     96 	uint8_t		sc_eeprom0[160];
     97 	uint8_t		sc_eeprom1[160];
     98 };
     99 
    100 static int	fcu_match(device_t, cfdata_t, void *);
    101 static void	fcu_attach(device_t, device_t, void *);
    102 
    103 static void	fcu_sensors_refresh(struct sysmon_envsys *, envsys_data_t *);
    104 
    105 static bool is_cpu(const envsys_data_t *);
    106 static bool is_case(const envsys_data_t *);
    107 static bool is_drive(const envsys_data_t *);
    108 
    109 static void fcu_set_fan_rpm(struct fcu_softc *, fcu_fan_t *, int);
    110 static void fcu_adjust_zone(struct fcu_softc *, int);
    111 static void fcu_adjust(void *);
    112 
    113 CFATTACH_DECL_NEW(fcu, sizeof(struct fcu_softc),
    114     fcu_match, fcu_attach, NULL, NULL);
    115 
    116 static const struct device_compatible_entry compat_data[] = {
    117 	{ .compat = "fcu" },
    118 	DEVICE_COMPAT_EOL
    119 };
    120 
    121 static int
    122 fcu_match(device_t parent, cfdata_t match, void *aux)
    123 {
    124 	struct i2c_attach_args *ia = aux;
    125 	int match_result;
    126 
    127 	if (iic_use_direct_match(ia, match, compat_data, &match_result))
    128 		return match_result;
    129 
    130 	if (ia->ia_addr == 0x2f)
    131 		return I2C_MATCH_ADDRESS_ONLY;
    132 
    133 	return 0;
    134 }
    135 
    136 static void
    137 fcu_attach(device_t parent, device_t self, void *aux)
    138 {
    139 	struct fcu_softc *sc = device_private(self);
    140 	struct i2c_attach_args *ia = aux;
    141 	int have_eeprom1 = 1;
    142 
    143 	sc->sc_dev = self;
    144 	sc->sc_i2c = ia->ia_tag;
    145 	sc->sc_addr = ia->ia_addr;
    146 
    147 	aprint_naive("\n");
    148 	aprint_normal(": Fan Control Unit\n");
    149 
    150 	if (get_cpuid(0, sc->sc_eeprom0) < 160) {
    151 		/*
    152 		 * XXX this should never happen, we depend on the EEPROM for
    153 		 * calibration data to make sense of temperature and voltage
    154 		 * sensors elsewhere, and fan parameters here.
    155 		 */
    156 		aprint_error_dev(self, "no EEPROM data for CPU 0\n");
    157 		return;
    158 	}
    159 	if (get_cpuid(1, sc->sc_eeprom1) < 160)
    160 		have_eeprom1 = 0;
    161 
    162 	/* init zones */
    163 	sc->sc_zones[FCU_ZONE_CPU].filter = is_cpu;
    164 	sc->sc_zones[FCU_ZONE_CPU].threshold = 50;
    165 	sc->sc_zones[FCU_ZONE_CPU].nfans = 0;
    166 	sc->sc_zones[FCU_ZONE_CASE].filter = is_case;
    167 	sc->sc_zones[FCU_ZONE_CASE].threshold = 50;
    168 	sc->sc_zones[FCU_ZONE_CASE].nfans = 0;
    169 	sc->sc_zones[FCU_ZONE_DRIVEBAY].filter = is_drive;
    170 	sc->sc_zones[FCU_ZONE_DRIVEBAY].threshold = 30;
    171 	sc->sc_zones[FCU_ZONE_DRIVEBAY].nfans = 0;
    172 
    173 	sc->sc_sme = sysmon_envsys_create();
    174 	sc->sc_sme->sme_name = device_xname(self);
    175 	sc->sc_sme->sme_cookie = sc;
    176 	sc->sc_sme->sme_refresh = fcu_sensors_refresh;
    177 
    178 	sc->sc_sensors[0].units = ENVSYS_SFANRPM;
    179 	sc->sc_sensors[1].state = ENVSYS_SINVALID;
    180 	sc->sc_nfans = 0;
    181 
    182 	/* round up sensors */
    183 	int ch;
    184 
    185 	sc->sc_nsensors = 0;
    186 	ch = OF_child(ia->ia_cookie);
    187 	while (ch != 0) {
    188 		char type[32], descr[32];
    189 		uint32_t reg;
    190 
    191 		envsys_data_t *s = &sc->sc_sensors[sc->sc_nsensors];
    192 
    193 		s->state = ENVSYS_SINVALID;
    194 
    195 		if (OF_getprop(ch, "device_type", type, 32) <= 0)
    196 			goto next;
    197 
    198 		if (strcmp(type, "fan-rpm-control") == 0) {
    199 			s->units = ENVSYS_SFANRPM;
    200 		} else if (strcmp(type, "fan-pwm-control") == 0) {
    201 			/* XXX we get the type from the register number */
    202 			s->units = ENVSYS_SFANRPM;
    203 /* skip those for now since we don't really know how to interpret them */
    204 #if 0
    205 		} else if (strcmp(type, "power-sensor") == 0) {
    206 			s->units = ENVSYS_SVOLTS_DC;
    207 #endif
    208 		} else if (strcmp(type, "gpi-sensor") == 0) {
    209 			s->units = ENVSYS_INDICATOR;
    210 		} else {
    211 			/* ignore other types for now */
    212 			goto next;
    213 		}
    214 
    215 		if (OF_getprop(ch, "reg", &reg, sizeof(reg)) <= 0)
    216 			goto next;
    217 		s->private = reg;
    218 
    219 		if (OF_getprop(ch, "location", descr, 32) <= 0)
    220 			goto next;
    221 		strcpy(s->desc, descr);
    222 
    223 		if (s->units == ENVSYS_SFANRPM) {
    224 			fcu_fan_t *fan = &sc->sc_fans[sc->sc_nfans];
    225 			uint8_t *eeprom = NULL;
    226 			uint16_t rmin, rmax;
    227 
    228 			if (strstr(descr, "CPU A") != NULL)
    229 				eeprom = sc->sc_eeprom0;
    230 			if (strstr(descr, "CPU B") != NULL) {
    231 				/*
    232 				 * XXX
    233 				 * this should never happen
    234 				 */
    235 				if (have_eeprom1 == 0) {
    236 					eeprom = sc->sc_eeprom0;
    237 				} else
    238 					eeprom = sc->sc_eeprom1;
    239 			}
    240 
    241 			fan->reg = reg;
    242 			fan->target = 0;
    243 			fan->duty = 0x80;
    244 
    245 			/* speed settings from EEPROM */
    246 			if (strstr(descr, "PUMP") != NULL) {
    247 				KASSERT(eeprom != NULL);
    248 				memcpy(&rmin, &eeprom[0x54], 2);
    249 				memcpy(&rmax, &eeprom[0x56], 2);
    250 				fan->base_rpm = rmin;
    251 				fan->max_rpm = rmax;
    252 				fan->step = (rmax - rmin) / 30;
    253 			} else if (strstr(descr, "INTAKE") != NULL) {
    254 				KASSERT(eeprom != NULL);
    255 				memcpy(&rmin, &eeprom[0x4c], 2);
    256 				memcpy(&rmax, &eeprom[0x4e], 2);
    257 				fan->base_rpm = rmin;
    258 				fan->max_rpm = rmax;
    259 				fan->step = (rmax - rmin) / 30;
    260 			} else if (strstr(descr, "EXHAUST") != NULL) {
    261 				KASSERT(eeprom != NULL);
    262 				memcpy(&rmin, &eeprom[0x50], 2);
    263 				memcpy(&rmax, &eeprom[0x52], 2);
    264 				fan->base_rpm = rmin;
    265 				fan->max_rpm = rmax;
    266 				fan->step = (rmax - rmin) / 30;
    267 			} else if (strstr(descr, "DRIVE") != NULL ) {
    268 				fan->base_rpm = 1000;
    269 				fan->max_rpm = 3000;
    270 				fan->step = 100;
    271 			} else {
    272 				fan->base_rpm = 1000;
    273 				fan->max_rpm = 3000;
    274 				fan->step = 100;
    275 			}
    276 			DPRINTF("fan %s: %d - %d rpm, step %d\n",
    277 			   descr, fan->base_rpm, fan->max_rpm, fan->step);
    278 
    279 			/* now stuff them into zones */
    280 			if (strstr(descr, "CPU") != NULL) {
    281 				fcu_zone_t *z = &sc->sc_zones[FCU_ZONE_CPU];
    282 				z->fans[z->nfans] = sc->sc_nfans;
    283 				z->nfans++;
    284 			} else if ((strstr(descr, "BACKSIDE") != NULL) ||
    285 				   (strstr(descr, "SLOT") != NULL))  {
    286 				fcu_zone_t *z = &sc->sc_zones[FCU_ZONE_CASE];
    287 				z->fans[z->nfans] = sc->sc_nfans;
    288 				z->nfans++;
    289 			} else if (strstr(descr, "DRIVE") != NULL) {
    290 				fcu_zone_t *z = &sc->sc_zones[FCU_ZONE_DRIVEBAY];
    291 				z->fans[z->nfans] = sc->sc_nfans;
    292 				z->nfans++;
    293 			}
    294 			sc->sc_nfans++;
    295 		}
    296 		sysmon_envsys_sensor_attach(sc->sc_sme, s);
    297 		sc->sc_nsensors++;
    298 next:
    299 		ch = OF_peer(ch);
    300 	}
    301 	sysmon_envsys_register(sc->sc_sme);
    302 
    303 	sc->sc_dying = FALSE;
    304 	kthread_create(PRI_NONE, 0, curcpu(), fcu_adjust, sc, &sc->sc_thread,
    305 	    "fan control");
    306 }
    307 
    308 static void
    309 fcu_sensors_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
    310 {
    311 	struct fcu_softc *sc = sme->sme_cookie;
    312 	uint8_t cmd;
    313 	uint16_t data = -1;
    314 	int error;
    315 
    316 	if (edata->units == ENVSYS_SFANRPM) {
    317 	    	cmd = edata->private + 1;
    318 	} else
    319 		cmd = edata->private;
    320 
    321 	/* fcu is a macppc only thing so we can safely assume big endian */
    322 	iic_acquire_bus(sc->sc_i2c, 0);
    323 	error = iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP,
    324 	    sc->sc_addr, &cmd, 1, &data, 2, 0);
    325 	iic_release_bus(sc->sc_i2c, 0);
    326 
    327 	if (error) {
    328 		edata->state = ENVSYS_SINVALID;
    329 		return;
    330 	}
    331 
    332 	edata->state = ENVSYS_SVALID;
    333 
    334 	switch (edata->units) {
    335 		case ENVSYS_SFANRPM:
    336 			edata->value_cur = data >> 3;
    337 			break;
    338 		case ENVSYS_SVOLTS_DC:
    339 			/* XXX this reads bogus */
    340 			edata->value_cur = data * 1000;
    341 			break;
    342 		case ENVSYS_INDICATOR:
    343 			/* guesswork for now */
    344 			edata->value_cur = data >> 8;
    345 			break;
    346 		default:
    347 			edata->state = ENVSYS_SINVALID;
    348 	}
    349 }
    350 
    351 static bool
    352 is_cpu(const envsys_data_t *edata)
    353 {
    354 	if (edata->units != ENVSYS_STEMP)
    355 		return false;
    356 	if (strstr(edata->desc, "CPU") != NULL)
    357 		return TRUE;
    358 	return false;
    359 }
    360 
    361 static bool
    362 is_case(const envsys_data_t *edata)
    363 {
    364 	if (edata->units != ENVSYS_STEMP)
    365 		return false;
    366 	if ((strstr(edata->desc, "MLB") != NULL) ||
    367 	    (strstr(edata->desc, "BACKSIDE") != NULL) ||
    368 	    (strstr(edata->desc, "U3") != NULL))
    369 		return TRUE;
    370 	return false;
    371 }
    372 
    373 static bool
    374 is_drive(const envsys_data_t *edata)
    375 {
    376 	if (edata->units != ENVSYS_STEMP)
    377 		return false;
    378 	if (strstr(edata->desc, "DRIVE") != NULL)
    379 		return TRUE;
    380 	return false;
    381 }
    382 
    383 static void
    384 fcu_set_fan_rpm(struct fcu_softc *sc, fcu_fan_t *f, int speed)
    385 {
    386 	int error;
    387 	uint8_t cmd;
    388 
    389 	if (speed > f->max_rpm) speed = f->max_rpm;
    390 	if (speed < f->base_rpm) speed = f->base_rpm;
    391 
    392 	if (f->reg < 0x30) {
    393 		uint16_t data;
    394 		/* simple rpm fan, just poke the register */
    395 
    396 		if (f->target == speed) return;
    397 		iic_acquire_bus(sc->sc_i2c, 0);
    398 		cmd = f->reg;
    399 		data = (speed << 3);
    400 		error = iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP,
    401 		    sc->sc_addr, &cmd, 1, &data, 2, 0);
    402 		iic_release_bus(sc->sc_i2c, 0);
    403 	} else {
    404 		int diff;
    405 		int nduty = f->duty;
    406 		uint16_t data;
    407 		/* pwm fan, measure speed, then adjust duty cycle */
    408 		DPRINTF("pwm fan ");
    409 		iic_acquire_bus(sc->sc_i2c, 0);
    410 		cmd = f->reg + 1;
    411 		error = iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP,
    412 		    sc->sc_addr, &cmd, 1, &data, 2, 0);
    413 		data = data >> 3;
    414 		diff = data - speed;
    415 		DPRINTF("d %d s %d t %d diff %d ", f->duty, data, speed, diff);
    416 		if (diff > 100) {
    417 			nduty = uimax(20, nduty - 1);
    418 		}
    419 		if (diff < -100) {
    420 			nduty = uimin(0xd0, nduty + 1);
    421 		}
    422 		cmd = f->reg;
    423 		DPRINTF("%s nduty %d", __func__, nduty);
    424 		if (nduty != f->duty) {
    425 			uint8_t arg = nduty;
    426 			error = iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP,
    427 			    sc->sc_addr, &cmd, 1, &arg, 1, 0);
    428 			f->duty = nduty;
    429 			sc->sc_pwm = TRUE;
    430 
    431 		}
    432 		iic_release_bus(sc->sc_i2c, 0);
    433 		DPRINTF("ok\n");
    434 	}
    435 	if (error) printf("boo\n");
    436 	f->target = speed;
    437 }
    438 
    439 static void
    440 fcu_adjust_zone(struct fcu_softc *sc, int which)
    441 {
    442 	fcu_zone_t *z = &sc->sc_zones[which];
    443 	fcu_fan_t *f;
    444 	int temp, i, speed, diff;
    445 
    446 	if (z->nfans <= 0)
    447 		return;
    448 
    449 	temp = sysmon_envsys_get_max_value(z->filter, true);
    450 	if (temp == 0) {
    451 		/* no sensor data - leave fan alone */
    452 		DPRINTF("nodata\n");
    453 		return;
    454 	}
    455 
    456 	temp = (temp - 273150000) / 1000000;
    457 	diff = temp - z->threshold;
    458 	if (diff < 0) diff = 0;
    459 
    460 	/* now adjust each fan to the new duty cycle */
    461 	for (i = 0; i < z->nfans; i++) {
    462 		if (z->fans[i] > 8) {
    463 			printf("wtf?!\n");
    464 			continue;
    465 		}
    466 		f = &sc->sc_fans[z->fans[i]];
    467 		speed = f->base_rpm + diff * f->step;
    468 		DPRINTF("diff %d base %d sp %d\n", diff, f->base_rpm, speed);
    469 		fcu_set_fan_rpm(sc, f, speed);
    470 	}
    471 }
    472 
    473 static void
    474 fcu_adjust(void *cookie)
    475 {
    476 	struct fcu_softc *sc = cookie;
    477 	int i;
    478 	uint8_t cmd, data;
    479 
    480 	while (!sc->sc_dying) {
    481 		/* poke the FCU so we don't go 747 */
    482 		iic_acquire_bus(sc->sc_i2c, 0);
    483 		cmd = FCU_FAN_ACTIVE;
    484 		iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP,
    485 		    sc->sc_addr, &cmd, 1, &data, 1, 0);
    486 		iic_release_bus(sc->sc_i2c, 0);
    487 		sc->sc_pwm = FALSE;
    488 		for (i = 0; i < FCU_ZONE_COUNT; i++)
    489 			fcu_adjust_zone(sc, i);
    490 		kpause("fanctrl", true, mstohz(sc->sc_pwm ? 1000 : 5000), NULL);
    491 	}
    492 	kthread_exit(0);
    493 }
    494