diff options
-rw-r--r-- | src/drivers/i2c/lm96000/Kconfig | 5 | ||||
-rw-r--r-- | src/drivers/i2c/lm96000/Makefile.inc | 1 | ||||
-rw-r--r-- | src/drivers/i2c/lm96000/chip.h | 128 | ||||
-rw-r--r-- | src/drivers/i2c/lm96000/lm96000.c | 226 | ||||
-rw-r--r-- | src/drivers/i2c/lm96000/lm96000.h | 65 |
5 files changed, 425 insertions, 0 deletions
diff --git a/src/drivers/i2c/lm96000/Kconfig b/src/drivers/i2c/lm96000/Kconfig new file mode 100644 index 0000000000..e08d449ad7 --- /dev/null +++ b/src/drivers/i2c/lm96000/Kconfig @@ -0,0 +1,5 @@ +config DRIVERS_I2C_LM96000 + depends on HAVE_MONOTONIC_TIMER + bool + help + Hardware-monitoring IC LM96000. diff --git a/src/drivers/i2c/lm96000/Makefile.inc b/src/drivers/i2c/lm96000/Makefile.inc new file mode 100644 index 0000000000..b17ccf88cf --- /dev/null +++ b/src/drivers/i2c/lm96000/Makefile.inc @@ -0,0 +1 @@ +ramstage-$(CONFIG_DRIVERS_I2C_LM96000) += lm96000.c diff --git a/src/drivers/i2c/lm96000/chip.h b/src/drivers/i2c/lm96000/chip.h new file mode 100644 index 0000000000..ab782dbbb5 --- /dev/null +++ b/src/drivers/i2c/lm96000/chip.h @@ -0,0 +1,128 @@ +/* + * This file is part of the coreboot project. + * + * Copyright 2017 secunet Security Networks AG + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#ifndef DRIVERS_I2C_LM96000_CHIP_H +#define DRIVERS_I2C_LM96000_CHIP_H + +#include <stdint.h> + +#define LM96000_VIN_CNT 5 +#define LM96000_TEMP_IN_CNT 3 +#define LM96000_FAN_IN_CNT 4 +#define LM96000_PWM_CTL_CNT 3 +#define LM96000_TEMP_ZONE_CNT 3 + +enum lm96000_vin { + LM96000_2_5V = 0, + LM96000_VCCP = 1, + LM96000_3_3V = 2, + LM96000_5V = 3, + LM96000_12V = 4, +}; + +enum lm96000_fan_mode { + /* Bit 7 merely signifies that the mode is set, so we + can map the lower bits directly to register values. */ + LM96000_FAN_IGNORE = 0x00, + LM96000_FAN_ZONE_1_AUTO = 0x80, + LM96000_FAN_ZONE_2_AUTO = 0x81, + LM96000_FAN_ZONE_3_AUTO = 0x82, + LM96000_FAN_ALWAYS_FULL = 0x83, + LM96000_FAN_DISABLED = 0x84, + LM96000_FAN_HOTTEST_23 = 0x85, + LM96000_FAN_HOTTEST_123 = 0x86, + LM96000_FAN_MANUAL = 0x87, +}; + +enum lm96000_pwm_freq { + LM96000_PWM_10HZ = 0, + LM96000_PWM_15HZ = 1, + LM96000_PWM_23HZ = 2, + LM96000_PWM_30HZ = 3, + LM96000_PWM_38HZ = 4, + LM96000_PWM_47HZ = 5, + LM96000_PWM_61HZ = 6, + LM96000_PWM_94HZ = 7, + LM96000_PWM_22_5KHZ = 8, + LM96000_PWM_24_0KHZ = 9, + LM96000_PWM_25_7KHZ = 10, + LM96000_PWM_27_7KHZ = 12, + LM96000_PWM_30_0KHZ = 14, +}; + +enum lm96000_tach_mode { + /* 0 will be used for kHz frequencies */ + LM96000_TACH_MODE_1 = 1, /* use if TACHx isn't used with PWMx */ + LM96000_TACH_MODE_2 = 2, /* use either 2 or 3 if TACHx matches PWMx */ + LM96000_TACH_MODE_3 = 3, +}; + +enum lm96000_spinup_time { + LM96000_SPINUP_0MS = 0, + LM96000_SPINUP_100MS = 1, + LM96000_SPINUP_250MS = 2, + LM96000_SPINUP_400MS = 3, + LM96000_SPINUP_700MS = 4, + LM96000_SPINUP_1000MS = 5, + LM96000_SPINUP_2000MS = 6, + LM96000_SPINUP_4000MS = 7, +}; + +struct lm96000_fan_config { + enum lm96000_fan_mode mode; + int invert; /* invert PWM signal */ + enum lm96000_spinup_time spinup; + enum lm96000_pwm_freq freq; + enum lm96000_tach_mode tach; + union { + u8 duty_cycle; /* duty cycle in manual mode */ + u8 min_duty; /* minimum duty cycle */ + }; +}; + +struct lm96000_temp_zone { + u8 target_temp; /* temperature for 100% duty cycle (in °C) */ + u8 panic_temp; /* temperature for 100% duty cycle on all fans */ + + /* This is tied to the zone in the implementation I tested + with. (Datasheet clearly states the opposite, that this + is tied to each PWM output so YMMV.) */ + enum { + LM96000_LOW_TEMP_OFF = 0, /* turn fan off below low temp. */ + LM96000_LOW_TEMP_MIN = 1, /* keep PWM at mininum duty cycle */ + } min_off; +}; + +/* Implements only those parts currently used by coreboot mainboards. */ +struct drivers_i2c_lm96000_config { + struct { + u16 low; /* in mV */ + u16 high; + } vin[LM96000_VIN_CNT]; + + struct { + signed char low; /* in °C */ + signed char high; + } temp_in[LM96000_TEMP_IN_CNT]; + + struct { + u16 low; /* in RPM */ + } fan_in[LM96000_FAN_IN_CNT]; + + struct lm96000_fan_config fan[LM96000_PWM_CTL_CNT]; + struct lm96000_temp_zone zone[LM96000_TEMP_ZONE_CNT]; +}; + +#endif /* DRIVERS_I2C_LM96000_CHIP_H */ diff --git a/src/drivers/i2c/lm96000/lm96000.c b/src/drivers/i2c/lm96000/lm96000.c new file mode 100644 index 0000000000..4bb2a4a8b4 --- /dev/null +++ b/src/drivers/i2c/lm96000/lm96000.c @@ -0,0 +1,226 @@ +/* + * This file is part of the coreboot project. + * + * Copyright 2017 secunet Security Networks AG + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#include <delay.h> +#include <timer.h> +#include <console/console.h> +#include <device/device.h> +#include <device/i2c_bus.h> + +#include "lm96000.h" +#include "chip.h" + +static inline int lm96000_read(struct device *const dev, const u8 reg) +{ + return i2c_dev_readb_at(dev, reg); +} + +static inline int lm96000_write(struct device *const dev, + const u8 reg, const u8 value) +{ + return i2c_dev_writeb_at(dev, reg, value); +} + +static inline int lm96000_update(struct device *const dev, const u8 reg, + const u8 clear_mask, const u8 set_mask) +{ + const int val = i2c_dev_readb_at(dev, reg); + if (val < 0) + return val; + return i2c_dev_writeb_at(dev, reg, (val & ~clear_mask) | set_mask); +} + +static const unsigned int ref_mv[] = { 2500, 2250, 3300, 5000, 12000 }; + +static u8 lm96000_to_low_limit(const enum lm96000_vin ref, const u16 limit) +{ + const unsigned int reg = + (unsigned int)limit * 0xc0 / ref_mv[ref]; + return reg < 0xff ? reg : 0xff; +} + +static u8 lm96000_to_high_limit(const enum lm96000_vin ref, const u16 limit) +{ + const unsigned int reg = + DIV_ROUND_UP((unsigned int)limit * 0xc0, ref_mv[ref]); + return reg < 0xff ? reg : 0xff; +} + +static void lm96000_set_vin_limits(struct device *const dev, + const struct drivers_i2c_lm96000_config *const config) +{ + unsigned int i; + + for (i = 0; i < LM96000_VIN_CNT; ++i) { + lm96000_write(dev, LM96000_VIN_LOW_LIMIT(i), + lm96000_to_low_limit(i, config->vin[i].low)); + if (config->vin[i].high > config->vin[i].low) + lm96000_write(dev, LM96000_VIN_HIGH_LIMIT(i), + lm96000_to_high_limit(i, config->vin[i].high)); + else + lm96000_write(dev, LM96000_VIN_HIGH_LIMIT(i), 0xff); + } +} + +static void lm96000_set_temp_limits(struct device *const dev, + const struct drivers_i2c_lm96000_config *const config) +{ + unsigned int i; + + for (i = 0; i < LM96000_TEMP_IN_CNT; ++i) { + lm96000_write(dev, LM96000_TEMP_LOW_LIMIT(i), + config->temp_in[i].low); + if (config->temp_in[i].high > config->temp_in[i].low) + lm96000_write(dev, LM96000_TEMP_HIGH_LIMIT(i), + config->temp_in[i].high); + else + lm96000_write(dev, LM96000_TEMP_HIGH_LIMIT(i), 0x7f); + } +} + +static u16 lm96000_rpm_to_tach(const u16 rpm) +{ + return rpm ? (60 * 90000 / rpm) & 0xfffc : 0xfffc; +} + +static void lm96000_set_fan_limits(struct device *const dev, + const struct drivers_i2c_lm96000_config *const config) +{ + unsigned int i; + + for (i = 0; i < LM96000_FAN_IN_CNT; ++i) { + const u16 tach = lm96000_rpm_to_tach(config->fan_in[i].low); + lm96000_write(dev, LM96000_FAN_LOW_LIMIT(i), tach & 0xff); + lm96000_write(dev, LM96000_FAN_LOW_LIMIT(i) + 1, tach >> 8); + } +} + +static u8 lm96000_to_duty(const u8 duty_cycle) +{ + return duty_cycle * 255 / 100; +} + +static void lm96000_configure_pwm(struct device *const dev, + const unsigned int fan, + const struct lm96000_fan_config *const config) +{ + lm96000_update(dev, LM96000_FAN_CFG(fan), + LM96000_FAN_CFG_MODE_MASK | LM96000_FAN_CFG_PWM_INVERT | + LM96000_FAN_CFG_SPINUP_MASK, + ((config->mode << LM96000_FAN_CFG_MODE_SHIFT) + & LM96000_FAN_CFG_MODE_MASK) | + (config->invert ? LM96000_FAN_CFG_PWM_INVERT : 0) | + config->spinup); + lm96000_update(dev, LM96000_FAN_FREQ(fan), + LM96000_FAN_FREQ_MASK, config->freq); + lm96000_update(dev, LM96000_TACH_MONITOR_MODE, + LM96000_TACH_MODE_FAN_MASK(fan), + config->freq <= LM96000_PWM_94HZ + ? config->tach << LM96000_TACH_MODE_FAN_SHIFT(fan) : 0); + + switch (config->mode) { + case LM96000_FAN_ZONE_1_AUTO: + case LM96000_FAN_ZONE_2_AUTO: + case LM96000_FAN_ZONE_3_AUTO: + case LM96000_FAN_HOTTEST_23: + case LM96000_FAN_HOTTEST_123: + lm96000_write(dev, LM96000_FAN_MIN_PWM(fan), + lm96000_to_duty(config->min_duty)); + break; + case LM96000_FAN_MANUAL: + lm96000_write(dev, LM96000_FAN_DUTY(fan), + lm96000_to_duty(config->duty_cycle)); + break; + default: + break; + } +} + +static void lm96000_configure_temp_zone(struct device *const dev, + const unsigned int zone, + const struct lm96000_temp_zone *const config) +{ + static const u8 temp_range[] = + { 2, 3, 3, 4, 5, 7, 8, 10, 13, 16, 20, 27, 32, 40, 53, 80 }; + unsigned int i; + + /* find longest range that starts from 25°C */ + for (i = ARRAY_SIZE(temp_range) - 1; i > 0; --i) { + if (temp_range[i] + 25 <= config->target_temp) + break; + } + + lm96000_update(dev, LM96000_ZONE_RANGE(zone), + LM96000_ZONE_RANGE_MASK, i << LM96000_ZONE_RANGE_SHIFT); + lm96000_write(dev, LM96000_ZONE_TEMP_LOW(zone), + config->target_temp >= temp_range[i] + ? config->target_temp - temp_range[i] + : 0); + lm96000_write(dev, LM96000_ZONE_TEMP_PANIC(zone), + config->panic_temp ? config->panic_temp : 100); + lm96000_update(dev, LM96000_FAN_MIN_OFF, + LM96000_FAN_MIN(zone), + config->min_off ? LM96000_FAN_MIN(zone) : 0); +} + +static void lm96000_init(struct device *const dev) +{ + const struct drivers_i2c_lm96000_config *const config = dev->chip_info; + unsigned int i, lm_config; + struct stopwatch sw; + + printk(BIOS_DEBUG, "lm96000: Initialization hardware monitoring.\n"); + + stopwatch_init_msecs_expire(&sw, 1000); + lm_config = lm96000_read(dev, LM96000_CONFIG); + while ((lm_config < 0 || !(lm_config & LM96000_READY))) { + mdelay(1); + lm_config = lm96000_read(dev, LM96000_CONFIG); + if (stopwatch_expired(&sw)) + break; + } + if (lm_config < 0 || !(lm_config & LM96000_READY)) { + printk(BIOS_INFO, "lm96000: Not ready after 1s.\n"); + return; + } + + lm96000_set_vin_limits(dev, config); + lm96000_set_temp_limits(dev, config); + lm96000_set_fan_limits(dev, config); + for (i = 0; i < LM96000_PWM_CTL_CNT; ++i) { + if (config->fan[i].mode != LM96000_FAN_IGNORE) + lm96000_configure_pwm(dev, i, config->fan + i); + } + for (i = 0; i < LM96000_TEMP_ZONE_CNT; ++i) + lm96000_configure_temp_zone(dev, i, config->zone + i); + lm96000_update(dev, LM96000_CONFIG, 0, LM96000_START); +} + +static struct device_operations lm96000_ops = { + .read_resources = DEVICE_NOOP, + .set_resources = DEVICE_NOOP, + .enable_resources = DEVICE_NOOP, + .init = lm96000_init, +}; + +static void lm96000_enable(struct device *const dev) +{ + dev->ops = &lm96000_ops; +} + +struct chip_operations drivers_i2c_lm96000_ops = { + CHIP_NAME("LM96000") + .enable_dev = lm96000_enable +}; diff --git a/src/drivers/i2c/lm96000/lm96000.h b/src/drivers/i2c/lm96000/lm96000.h new file mode 100644 index 0000000000..6df4fdeef3 --- /dev/null +++ b/src/drivers/i2c/lm96000/lm96000.h @@ -0,0 +1,65 @@ +/* + * This file is part of the coreboot project. + * + * Copyright (C) 2017 secunet Security Networks AG + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#ifndef DRIVERS_I2C_LM96000_H +#define DRIVERS_I2C_LM96000_H + +#include "chip.h" + +#define LM96000_CONFIG 0x40 +#define LM96000_READY (0x1 << 2) +#define LM96000_START (0x1 << 0) + +#define LM96000_VIN(v) (0x20 + (v)) +#define LM96000_VIN_LOW_LIMIT(v) (0x44 + (v) * 2) +#define LM96000_VIN_HIGH_LIMIT(v) (0x45 + (v) * 2) + +#define LM96000_TEMP_IN(temp) (0x25 + (temp)) +#define LM96000_TEMP_LOW_LIMIT(temp) (0x4e + (temp) * 2) +#define LM96000_TEMP_HIGH_LIMIT(temp) (0x4f + (temp) * 2) + +/* 2B, little endian, MSB is latched upon LSB read */ +#define LM96000_FAN_IN(fan) (0x28 + (fan) * 2) +#define LM96000_FAN_LOW_LIMIT(fan) (0x54 + (fan) * 2) + +#define LM96000_FAN_DUTY(fan) (0x30 + (fan)) +#define LM96000_FAN_CFG(fan) (0x5c + (fan)) +#define LM96000_FAN_CFG_MODE_SHIFT 5 +#define LM96000_FAN_CFG_MODE_MASK (0x7 << LM96000_FAN_CFG_MODE_SHIFT) +#define LM96000_FAN_CFG_PWM_INVERT (0x1 << 4) +#define LM96000_FAN_CFG_SPINUP_MASK (0x7 << 0) +#define LM96000_FAN_FREQ(fan) (0x5f + (fan)) +#define LM96000_FAN_FREQ_MASK (0xf << 0) +#define LM96000_FAN_MIN_OFF 0x62 +#define LM96000_FAN_MIN(fan) (1 << ((fan) + 5)) +#define LM96000_FAN_MIN_PWM(fan) (0x64 + (fan)) +#define LM96000_TACH_MONITOR_MODE 0x74 +#define LM96000_TACH_MODE_FAN_SHIFT(f) ((f) * 2) +#define LM96000_TACH_MODE_FAN_MASK(f) (0x3 << LM96000_TACH_MODE_FAN_SHIFT(f)) + +#define LM96000_ZONE_RANGE(zone) (0x5f + (zone)) +#define LM96000_ZONE_RANGE_SHIFT 4 +#define LM96000_ZONE_RANGE_MASK (0xf << LM96000_ZONE_RANGE_SHIFT) +#define LM96000_ZONE_SMOOTH(zone) (0x62 + ((zone) + 1) / 2) +#define LM96000_ZONE_SMOOTH_EN(zone) (1 << (((zone) % 2) * 4 + 3)) +#define LM96000_ZONE_SMOOTH_SHFT(zone) (((zone) % 2) * 4) +#define LM96000_ZONE_SMOOTH_MASK(zone) (0x7 << LM96000_ZONE_SMOOTH_SHFT(zone)) +#define LM96000_ZONE_TEMP_LOW(zone) (0x67 + (zone)) +#define LM96000_ZONE_TEMP_PANIC(zone) (0x6a + (zone)) +#define LM96000_ZONE_HYSTERESIS(zone) (0x6d + (zone) / 2) +#define LM96000_ZONE_HYST_SHIFT(zone) (4 - ((zone) % 2) * 4) +#define LM96000_ZONE_HYST_MASK(zone) (0xf << LM96000_ZONE_HYST_SHIFT(zone)) + +#endif /* DRIVERS_I2C_LM96000_H */ |