Home | History | Annotate | Line # | Download | only in dev
fcu.c revision 1.3
      1 /* $NetBSD: fcu.c,v 1.3 2021/07/28 00:59:10 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.3 2021/07/28 00:59:10 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 #include <macppc/dev/fancontrolvar.h>
     46 
     47 //#define FCU_DEBUG
     48 #ifdef FCU_DEBUG
     49 #define DPRINTF printf
     50 #else
     51 #define DPRINTF if (0) printf
     52 #endif
     53 
     54 /* FCU registers, from OpenBSD's fcu.c */
     55 #define FCU_FAN_FAIL	0x0b		/* fans states in bits 0<1-6>7 */
     56 #define FCU_FAN_ACTIVE	0x0d
     57 #define FCU_FANREAD(x)	0x11 + (x)*2
     58 #define FCU_FANSET(x)	0x10 + (x)*2
     59 #define FCU_PWM_FAIL	0x2b
     60 #define FCU_PWM_ACTIVE	0x2d
     61 #define FCU_PWMREAD(x)	0x30 + (x)*2
     62 
     63 
     64 typedef struct _fcu_fan {
     65 	int target;
     66 	int reg;
     67 	int base_rpm, max_rpm;
     68 	int step;
     69 	int duty;	/* for pwm fans */
     70 } fcu_fan_t;
     71 
     72 #define FCU_ZONE_CPU		0
     73 #define FCU_ZONE_CASE		1
     74 #define FCU_ZONE_DRIVEBAY	2
     75 #define FCU_ZONE_COUNT		3
     76 
     77 struct fcu_softc {
     78 	device_t	sc_dev;
     79 	i2c_tag_t	sc_i2c;
     80 	i2c_addr_t	sc_addr;
     81 
     82 	struct sysmon_envsys *sc_sme;
     83 	envsys_data_t		sc_sensors[32];
     84 	int			sc_nsensors;
     85 	fancontrol_zone_t	sc_zones[FCU_ZONE_COUNT];
     86 	fcu_fan_t		sc_fans[FANCONTROL_MAX_FANS];
     87 	int			sc_nfans;
     88 	lwp_t			*sc_thread;
     89 	bool			sc_dying, sc_pwm;
     90 	uint8_t			sc_eeprom0[160];
     91 	uint8_t			sc_eeprom1[160];
     92 };
     93 
     94 static int	fcu_match(device_t, cfdata_t, void *);
     95 static void	fcu_attach(device_t, device_t, void *);
     96 
     97 static void	fcu_sensors_refresh(struct sysmon_envsys *, envsys_data_t *);
     98 
     99 static bool is_cpu(const envsys_data_t *);
    100 static bool is_case(const envsys_data_t *);
    101 static bool is_drive(const envsys_data_t *);
    102 
    103 static int fcu_set_rpm(void *, int, int);
    104 static int fcu_get_rpm(void *, int);
    105 static void fcu_adjust(void *);
    106 
    107 CFATTACH_DECL_NEW(fcu, sizeof(struct fcu_softc),
    108     fcu_match, fcu_attach, NULL, NULL);
    109 
    110 static const struct device_compatible_entry compat_data[] = {
    111 	{ .compat = "fcu" },
    112 	DEVICE_COMPAT_EOL
    113 };
    114 
    115 static int
    116 fcu_match(device_t parent, cfdata_t match, void *aux)
    117 {
    118 	struct i2c_attach_args *ia = aux;
    119 	int match_result;
    120 
    121 	if (iic_use_direct_match(ia, match, compat_data, &match_result))
    122 		return match_result;
    123 
    124 	if (ia->ia_addr == 0x2f)
    125 		return I2C_MATCH_ADDRESS_ONLY;
    126 
    127 	return 0;
    128 }
    129 
    130 static void
    131 fcu_attach(device_t parent, device_t self, void *aux)
    132 {
    133 	struct fcu_softc *sc = device_private(self);
    134 	struct i2c_attach_args *ia = aux;
    135 	int have_eeprom1 = 1;
    136 
    137 	sc->sc_dev = self;
    138 	sc->sc_i2c = ia->ia_tag;
    139 	sc->sc_addr = ia->ia_addr;
    140 
    141 	aprint_naive("\n");
    142 	aprint_normal(": Fan Control Unit\n");
    143 
    144 	if (get_cpuid(0, sc->sc_eeprom0) < 160) {
    145 		/*
    146 		 * XXX this should never happen, we depend on the EEPROM for
    147 		 * calibration data to make sense of temperature and voltage
    148 		 * sensors elsewhere, and fan parameters here.
    149 		 */
    150 		aprint_error_dev(self, "no EEPROM data for CPU 0\n");
    151 		return;
    152 	}
    153 	if (get_cpuid(1, sc->sc_eeprom1) < 160)
    154 		have_eeprom1 = 0;
    155 
    156 	/* init zones */
    157 	sc->sc_zones[FCU_ZONE_CPU].name = "CPUs";
    158 	sc->sc_zones[FCU_ZONE_CPU].filter = is_cpu;
    159 	sc->sc_zones[FCU_ZONE_CPU].cookie = sc;
    160 	sc->sc_zones[FCU_ZONE_CPU].get_rpm = fcu_get_rpm;
    161 	sc->sc_zones[FCU_ZONE_CPU].set_rpm = fcu_set_rpm;
    162 	sc->sc_zones[FCU_ZONE_CPU].Tmin = 50;
    163 	sc->sc_zones[FCU_ZONE_CPU].Tmax = 85;
    164 	sc->sc_zones[FCU_ZONE_CPU].nfans = 0;
    165 	sc->sc_zones[FCU_ZONE_CASE].name = "Slots";
    166 	sc->sc_zones[FCU_ZONE_CASE].filter = is_case;
    167 	sc->sc_zones[FCU_ZONE_CASE].Tmin = 50;
    168 	sc->sc_zones[FCU_ZONE_CASE].cookie = sc;
    169 	sc->sc_zones[FCU_ZONE_CASE].get_rpm = fcu_get_rpm;
    170 	sc->sc_zones[FCU_ZONE_CASE].set_rpm = fcu_set_rpm;
    171 	sc->sc_zones[FCU_ZONE_CASE].Tmax = 75;
    172 	sc->sc_zones[FCU_ZONE_CASE].nfans = 0;
    173 	sc->sc_zones[FCU_ZONE_DRIVEBAY].name = "Drive bays";
    174 	sc->sc_zones[FCU_ZONE_DRIVEBAY].filter = is_drive;
    175 	sc->sc_zones[FCU_ZONE_DRIVEBAY].cookie = sc;
    176 	sc->sc_zones[FCU_ZONE_DRIVEBAY].get_rpm = fcu_get_rpm;
    177 	sc->sc_zones[FCU_ZONE_DRIVEBAY].set_rpm = fcu_set_rpm;
    178 	sc->sc_zones[FCU_ZONE_DRIVEBAY].Tmin = 30;
    179 	sc->sc_zones[FCU_ZONE_DRIVEBAY].Tmax = 60;
    180 	sc->sc_zones[FCU_ZONE_DRIVEBAY].nfans = 0;
    181 
    182 	sc->sc_sme = sysmon_envsys_create();
    183 	sc->sc_sme->sme_name = device_xname(self);
    184 	sc->sc_sme->sme_cookie = sc;
    185 	sc->sc_sme->sme_refresh = fcu_sensors_refresh;
    186 
    187 	sc->sc_sensors[0].units = ENVSYS_SFANRPM;
    188 	sc->sc_sensors[1].state = ENVSYS_SINVALID;
    189 	sc->sc_nfans = 0;
    190 
    191 	/* round up sensors */
    192 	int ch;
    193 
    194 	sc->sc_nsensors = 0;
    195 	ch = OF_child(ia->ia_cookie);
    196 	while (ch != 0) {
    197 		char type[32], descr[32];
    198 		uint32_t reg;
    199 
    200 		envsys_data_t *s = &sc->sc_sensors[sc->sc_nsensors];
    201 
    202 		s->state = ENVSYS_SINVALID;
    203 
    204 		if (OF_getprop(ch, "device_type", type, 32) <= 0)
    205 			goto next;
    206 
    207 		if (strcmp(type, "fan-rpm-control") == 0) {
    208 			s->units = ENVSYS_SFANRPM;
    209 		} else if (strcmp(type, "fan-pwm-control") == 0) {
    210 			/* XXX we get the type from the register number */
    211 			s->units = ENVSYS_SFANRPM;
    212 /* skip those for now since we don't really know how to interpret them */
    213 #if 0
    214 		} else if (strcmp(type, "power-sensor") == 0) {
    215 			s->units = ENVSYS_SVOLTS_DC;
    216 #endif
    217 		} else if (strcmp(type, "gpi-sensor") == 0) {
    218 			s->units = ENVSYS_INDICATOR;
    219 		} else {
    220 			/* ignore other types for now */
    221 			goto next;
    222 		}
    223 
    224 		if (OF_getprop(ch, "reg", &reg, sizeof(reg)) <= 0)
    225 			goto next;
    226 		s->private = reg;
    227 
    228 		if (OF_getprop(ch, "location", descr, 32) <= 0)
    229 			goto next;
    230 		strcpy(s->desc, descr);
    231 
    232 		if (s->units == ENVSYS_SFANRPM) {
    233 			fcu_fan_t *fan = &sc->sc_fans[sc->sc_nfans];
    234 			uint8_t *eeprom = NULL;
    235 			uint16_t rmin, rmax;
    236 
    237 			if (strstr(descr, "CPU A") != NULL)
    238 				eeprom = sc->sc_eeprom0;
    239 			if (strstr(descr, "CPU B") != NULL) {
    240 				/*
    241 				 * XXX
    242 				 * this should never happen
    243 				 */
    244 				if (have_eeprom1 == 0) {
    245 					eeprom = sc->sc_eeprom0;
    246 				} else
    247 					eeprom = sc->sc_eeprom1;
    248 			}
    249 
    250 			fan->reg = reg;
    251 			fan->target = 0;
    252 			fan->duty = 0x80;
    253 
    254 			/* speed settings from EEPROM */
    255 			if (strstr(descr, "PUMP") != NULL) {
    256 				KASSERT(eeprom != NULL);
    257 				memcpy(&rmin, &eeprom[0x54], 2);
    258 				memcpy(&rmax, &eeprom[0x56], 2);
    259 				fan->base_rpm = rmin;
    260 				fan->max_rpm = rmax;
    261 				fan->step = (rmax - rmin) / 30;
    262 			} else if (strstr(descr, "INTAKE") != NULL) {
    263 				KASSERT(eeprom != NULL);
    264 				memcpy(&rmin, &eeprom[0x4c], 2);
    265 				memcpy(&rmax, &eeprom[0x4e], 2);
    266 				fan->base_rpm = rmin;
    267 				fan->max_rpm = rmax;
    268 				fan->step = (rmax - rmin) / 30;
    269 			} else if (strstr(descr, "EXHAUST") != NULL) {
    270 				KASSERT(eeprom != NULL);
    271 				memcpy(&rmin, &eeprom[0x50], 2);
    272 				memcpy(&rmax, &eeprom[0x52], 2);
    273 				fan->base_rpm = rmin;
    274 				fan->max_rpm = rmax;
    275 				fan->step = (rmax - rmin) / 30;
    276 			} else if (strstr(descr, "DRIVE") != NULL ) {
    277 				fan->base_rpm = 1000;
    278 				fan->max_rpm = 3000;
    279 				fan->step = 100;
    280 			} else {
    281 				fan->base_rpm = 1000;
    282 				fan->max_rpm = 3000;
    283 				fan->step = 100;
    284 			}
    285 			DPRINTF("fan %s: %d - %d rpm, step %d\n",
    286 			   descr, fan->base_rpm, fan->max_rpm, fan->step);
    287 
    288 			/* now stuff them into zones */
    289 			if (strstr(descr, "CPU") != NULL) {
    290 				fancontrol_zone_t *z = &sc->sc_zones[FCU_ZONE_CPU];
    291 				z->fans[z->nfans].num = sc->sc_nfans;
    292 				z->fans[z->nfans].min_rpm = fan->base_rpm;
    293 				z->fans[z->nfans].max_rpm = fan->max_rpm;
    294 				z->fans[z->nfans].name = s->desc;
    295 				z->nfans++;
    296 			} else if ((strstr(descr, "BACKSIDE") != NULL) ||
    297 				   (strstr(descr, "SLOT") != NULL))  {
    298 				fancontrol_zone_t *z = &sc->sc_zones[FCU_ZONE_CASE];
    299 				z->fans[z->nfans].num = sc->sc_nfans;
    300 				z->fans[z->nfans].min_rpm = fan->base_rpm;
    301 				z->fans[z->nfans].max_rpm = fan->max_rpm;
    302 				z->fans[z->nfans].name = s->desc;
    303 				z->nfans++;
    304 			} else if (strstr(descr, "DRIVE") != NULL) {
    305 				fancontrol_zone_t *z = &sc->sc_zones[FCU_ZONE_DRIVEBAY];
    306 				z->fans[z->nfans].num = sc->sc_nfans;
    307 				z->fans[z->nfans].min_rpm = fan->base_rpm;
    308 				z->fans[z->nfans].max_rpm = fan->max_rpm;
    309 				z->fans[z->nfans].name = s->desc;
    310 				z->nfans++;
    311 			}
    312 			sc->sc_nfans++;
    313 		}
    314 		sysmon_envsys_sensor_attach(sc->sc_sme, s);
    315 		sc->sc_nsensors++;
    316 next:
    317 		ch = OF_peer(ch);
    318 	}
    319 	sysmon_envsys_register(sc->sc_sme);
    320 
    321 	sc->sc_dying = FALSE;
    322 	kthread_create(PRI_NONE, 0, curcpu(), fcu_adjust, sc, &sc->sc_thread,
    323 	    "fan control");
    324 }
    325 
    326 static void
    327 fcu_sensors_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
    328 {
    329 	struct fcu_softc *sc = sme->sme_cookie;
    330 	uint8_t cmd;
    331 	uint16_t data = -1;
    332 	int error;
    333 
    334 	if (edata->units == ENVSYS_SFANRPM) {
    335 	    	cmd = edata->private + 1;
    336 	} else
    337 		cmd = edata->private;
    338 
    339 	/* fcu is a macppc only thing so we can safely assume big endian */
    340 	iic_acquire_bus(sc->sc_i2c, 0);
    341 	error = iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP,
    342 	    sc->sc_addr, &cmd, 1, &data, 2, 0);
    343 	iic_release_bus(sc->sc_i2c, 0);
    344 
    345 	if (error) {
    346 		edata->state = ENVSYS_SINVALID;
    347 		return;
    348 	}
    349 
    350 	edata->state = ENVSYS_SVALID;
    351 
    352 	switch (edata->units) {
    353 		case ENVSYS_SFANRPM:
    354 			edata->value_cur = data >> 3;
    355 			break;
    356 		case ENVSYS_SVOLTS_DC:
    357 			/* XXX this reads bogus */
    358 			edata->value_cur = data * 1000;
    359 			break;
    360 		case ENVSYS_INDICATOR:
    361 			/* guesswork for now */
    362 			edata->value_cur = data >> 8;
    363 			break;
    364 		default:
    365 			edata->state = ENVSYS_SINVALID;
    366 	}
    367 }
    368 
    369 static bool
    370 is_cpu(const envsys_data_t *edata)
    371 {
    372 	if (edata->units != ENVSYS_STEMP)
    373 		return false;
    374 	if (strstr(edata->desc, "CPU") != NULL)
    375 		return TRUE;
    376 	return false;
    377 }
    378 
    379 static bool
    380 is_case(const envsys_data_t *edata)
    381 {
    382 	if (edata->units != ENVSYS_STEMP)
    383 		return false;
    384 	if ((strstr(edata->desc, "MLB") != NULL) ||
    385 	    (strstr(edata->desc, "BACKSIDE") != NULL) ||
    386 	    (strstr(edata->desc, "U3") != NULL))
    387 		return TRUE;
    388 	return false;
    389 }
    390 
    391 static bool
    392 is_drive(const envsys_data_t *edata)
    393 {
    394 	if (edata->units != ENVSYS_STEMP)
    395 		return false;
    396 	if (strstr(edata->desc, "DRIVE") != NULL)
    397 		return TRUE;
    398 	return false;
    399 }
    400 
    401 static int
    402 fcu_get_rpm(void *cookie, int which)
    403 {
    404 	struct fcu_softc *sc = cookie;
    405 	fcu_fan_t *f = &sc->sc_fans[which];
    406 	int error;
    407 	uint16_t data;
    408 	uint8_t cmd;
    409 
    410 	iic_acquire_bus(sc->sc_i2c, 0);
    411 	cmd = f->reg + 1;
    412 	error = iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP,
    413 	    sc->sc_addr, &cmd, 1, &data, 2, 0);
    414 	iic_release_bus(sc->sc_i2c, 0);
    415 	if (error != 0) return -1;
    416 	data = data >> 3;
    417 	return data;
    418 }
    419 
    420 static int
    421 fcu_set_rpm(void *cookie, int which, int speed)
    422 {
    423 	struct fcu_softc *sc = cookie;
    424 	fcu_fan_t *f = &sc->sc_fans[which];
    425 	int error = 0;
    426 	uint8_t cmd;
    427 
    428 	if (speed > f->max_rpm) speed = f->max_rpm;
    429 	if (speed < f->base_rpm) speed = f->base_rpm;
    430 
    431 	if (f->reg < 0x30) {
    432 		uint16_t data;
    433 		/* simple rpm fan, just poke the register */
    434 
    435 		if (f->target == speed) return 0;
    436 		iic_acquire_bus(sc->sc_i2c, 0);
    437 		cmd = f->reg;
    438 		data = (speed << 3);
    439 		error = iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP,
    440 		    sc->sc_addr, &cmd, 1, &data, 2, 0);
    441 		iic_release_bus(sc->sc_i2c, 0);
    442 	} else {
    443 		int diff;
    444 		int nduty = f->duty;
    445 		int current_speed;
    446 		/* pwm fan, measure speed, then adjust duty cycle */
    447 		DPRINTF("pwm fan ");
    448 		current_speed = fcu_get_rpm(sc, which);
    449 		diff = current_speed - speed;
    450 		DPRINTF("d %d s %d t %d diff %d ", f->duty, current_speed, speed, diff);
    451 		if (diff > 100) {
    452 			nduty = uimax(20, nduty - 1);
    453 		}
    454 		if (diff < -100) {
    455 			nduty = uimin(0xd0, nduty + 1);
    456 		}
    457 		cmd = f->reg;
    458 		DPRINTF("%s nduty %d", __func__, nduty);
    459 		if (nduty != f->duty) {
    460 			uint8_t arg = nduty;
    461 			iic_acquire_bus(sc->sc_i2c, 0);
    462 			error = iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP,
    463 			    sc->sc_addr, &cmd, 1, &arg, 1, 0);
    464 			iic_release_bus(sc->sc_i2c, 0);
    465 			f->duty = nduty;
    466 			sc->sc_pwm = TRUE;
    467 
    468 		}
    469 		DPRINTF("ok\n");
    470 	}
    471 	if (error) printf("boo\n");
    472 	f->target = speed;
    473 	return 0;
    474 }
    475 
    476 static void
    477 fcu_adjust(void *cookie)
    478 {
    479 	struct fcu_softc *sc = cookie;
    480 	int i;
    481 	uint8_t cmd, data;
    482 
    483 	while (!sc->sc_dying) {
    484 		/* poke the FCU so we don't go 747 */
    485 		iic_acquire_bus(sc->sc_i2c, 0);
    486 		cmd = FCU_FAN_ACTIVE;
    487 		iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP,
    488 		    sc->sc_addr, &cmd, 1, &data, 1, 0);
    489 		iic_release_bus(sc->sc_i2c, 0);
    490 		sc->sc_pwm = FALSE;
    491 		for (i = 0; i < FCU_ZONE_COUNT; i++)
    492 			fancontrol_adjust_zone(&sc->sc_zones[i]);
    493 		/*
    494 		 * take a shorter nap if we're in the proccess of adjusting a
    495 		 * PWM fan, which relies on measuring speed and then changing
    496 		 * its duty cycle until we're reasonable close to the target
    497 		 * speed
    498 		 */
    499 		kpause("fanctrl", true, mstohz(sc->sc_pwm ? 1000 : 2000), NULL);
    500 	}
    501 	kthread_exit(0);
    502 }
    503