From a3f643a3c0b350703d2531bb54530b102fe68caf Mon Sep 17 00:00:00 2001 From: Nico Huber Date: Wed, 23 Aug 2017 16:53:31 +0200 Subject: drivers/i2c/nct7802y: Add new hardware-monitoring IC Just another hardware-monitoring chip. Only limited fan control and PECI configuration is implemented. Change-Id: I35ea79e12941804e398c6304a08170a776f4ca76 Signed-off-by: Nico Huber Reviewed-on: https://review.coreboot.org/c/coreboot/+/29475 Tested-by: build bot (Jenkins) Reviewed-by: Felix Held --- src/drivers/i2c/nct7802y/Kconfig | 4 ++ src/drivers/i2c/nct7802y/Makefile.inc | 3 + src/drivers/i2c/nct7802y/chip.h | 96 ++++++++++++++++++++++++++ src/drivers/i2c/nct7802y/nct7802y.c | 49 ++++++++++++++ src/drivers/i2c/nct7802y/nct7802y.h | 104 +++++++++++++++++++++++++++++ src/drivers/i2c/nct7802y/nct7802y_fan.c | 111 +++++++++++++++++++++++++++++++ src/drivers/i2c/nct7802y/nct7802y_peci.c | 87 ++++++++++++++++++++++++ 7 files changed, 454 insertions(+) create mode 100644 src/drivers/i2c/nct7802y/Kconfig create mode 100644 src/drivers/i2c/nct7802y/Makefile.inc create mode 100644 src/drivers/i2c/nct7802y/chip.h create mode 100644 src/drivers/i2c/nct7802y/nct7802y.c create mode 100644 src/drivers/i2c/nct7802y/nct7802y.h create mode 100644 src/drivers/i2c/nct7802y/nct7802y_fan.c create mode 100644 src/drivers/i2c/nct7802y/nct7802y_peci.c (limited to 'src') diff --git a/src/drivers/i2c/nct7802y/Kconfig b/src/drivers/i2c/nct7802y/Kconfig new file mode 100644 index 0000000000..a51f384b3f --- /dev/null +++ b/src/drivers/i2c/nct7802y/Kconfig @@ -0,0 +1,4 @@ +config DRIVERS_I2C_NCT7802Y + bool + help + The NCT7802Y by Nuvoton is a Hardware Monitoring IC. diff --git a/src/drivers/i2c/nct7802y/Makefile.inc b/src/drivers/i2c/nct7802y/Makefile.inc new file mode 100644 index 0000000000..9544de0b3e --- /dev/null +++ b/src/drivers/i2c/nct7802y/Makefile.inc @@ -0,0 +1,3 @@ +ramstage-$(CONFIG_DRIVERS_I2C_NCT7802Y) += nct7802y.c +ramstage-$(CONFIG_DRIVERS_I2C_NCT7802Y) += nct7802y_fan.c +ramstage-$(CONFIG_DRIVERS_I2C_NCT7802Y) += nct7802y_peci.c diff --git a/src/drivers/i2c/nct7802y/chip.h b/src/drivers/i2c/nct7802y/chip.h new file mode 100644 index 0000000000..11db12be35 --- /dev/null +++ b/src/drivers/i2c/nct7802y/chip.h @@ -0,0 +1,96 @@ +/* + * 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_NCT7802Y_CHIP_H +#define DRIVERS_I2C_NCT7802Y_CHIP_H + +#include + +#define NCT7802Y_PECI_CNT 2 +#define NCT7802Y_FAN_CNT 3 + +enum nct7802y_peci_mode { + PECI_DISABLED = 0, + PECI_DOMAIN_0, + PECI_DOMAIN_1, + PECI_HIGHEST, +}; + +struct nct7802y_peci_config { + enum nct7802y_peci_mode mode; + u8 base_temp; +}; + +enum nct7802y_fan_mode { + FAN_IGNORE = 0, + FAN_MANUAL, + FAN_SMART, +}; + +enum nct7802y_fan_smartmode { + SMART_FAN_DUTY = 0, /* Target values given in duty cycle %. */ + SMART_FAN_RPM, /* Target valuse given in RPM. */ +}; + +enum nct7802y_fan_speed { + FAN_SPEED_NORMAL = 0, /* RPM values <= 12,750. */ + FAN_SPPED_HIGHSPEED, /* RPM values <= 25,500. */ +}; + +enum nct7802y_fan_pecierror { + PECI_ERROR_KEEP = 0, /* Keep current value. */ + PECI_ERROR_VALUE, /* Use `pecierror_minduty`. */ + PECI_ERROR_FULLSPEED, /* Run PWM at 100% duty cycle. */ +}; + +enum nct7802y_temp_source { + TEMP_SOURCE_REMOTE_1 = 0, + TEMP_SOURCE_REMOTE_2, + TEMP_SOURCE_REMOTE_3, + TEMP_SOURCE_LOCAL, + TEMP_SOURCE_PECI_0, + TEMP_SOURCE_PECI_1, + TEMP_SOURCE_PROGRAMMABLE_0, + TEMP_SOURCE_PROGRAMMABLE_1, +}; + +struct nct7802y_fan_smartconfig { + enum nct7802y_fan_smartmode mode; + enum nct7802y_fan_speed speed; + enum nct7802y_temp_source tempsrc; + struct { + u8 temp; + u16 target; + } table[4]; + u8 critical_temp; +}; + +struct nct7802y_fan_config { + enum nct7802y_fan_mode mode; + union { + u8 duty_cycle; + struct nct7802y_fan_smartconfig smart; + }; +}; + +/* Implements only those parts currently used by coreboot mainboards. */ +struct drivers_i2c_nct7802y_config { + struct nct7802y_peci_config peci[NCT7802Y_PECI_CNT]; + struct nct7802y_fan_config fan[NCT7802Y_FAN_CNT]; + enum nct7802y_fan_pecierror on_pecierror; + u8 pecierror_minduty; +}; + +#endif /* DRIVERS_I2C_NCT7802Y_CHIP_H */ diff --git a/src/drivers/i2c/nct7802y/nct7802y.c b/src/drivers/i2c/nct7802y/nct7802y.c new file mode 100644 index 0000000000..3681383610 --- /dev/null +++ b/src/drivers/i2c/nct7802y/nct7802y.c @@ -0,0 +1,49 @@ +/* + * 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 +#include + +#include "nct7802y.h" +#include "chip.h" + +static void nct7802y_init(struct device *const dev) +{ + if (!dev->chip_info) { + printk(BIOS_WARNING, + "NCT7802Y driver selected but not configured."); + return; + } + + nct7802y_init_peci(dev); + nct7802y_init_fan(dev); +} + +static struct device_operations nct7802y_ops = { + .read_resources = DEVICE_NOOP, + .set_resources = DEVICE_NOOP, + .enable_resources = DEVICE_NOOP, + .init = nct7802y_init, +}; + +static void nct7802y_enable(struct device *const dev) +{ + dev->ops = &nct7802y_ops; +} + +struct chip_operations drivers_i2c_nct7802y_ops = { + CHIP_NAME("NCT7802Y") + .enable_dev = nct7802y_enable +}; diff --git a/src/drivers/i2c/nct7802y/nct7802y.h b/src/drivers/i2c/nct7802y/nct7802y.h new file mode 100644 index 0000000000..16a16840f3 --- /dev/null +++ b/src/drivers/i2c/nct7802y/nct7802y.h @@ -0,0 +1,104 @@ +/* + * 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_NCT7802Y_H +#define DRIVERS_I2C_NCT7802Y_H + +#include +#include +#include +#include + +#define BANK_SELECT 0x00 + +/* Bank 0 */ + +#define PECI_ENABLE 0x23 +#define PECI_ENABLE_AGENTx(x) (1 << (x)) + +#define FAN_ENABLE 0x24 +#define FANx_ENABLE(fan) (1 << (fan)) + +#define FAN_CTRL(fan) (0x60 + (fan)) + +#define CLOSE_LOOP_FAN_RPM_CTRL 0x63 +#define CLOSE_LOOP_FANx_EN(fan) (1 << ((fan) + 5)) +#define CLOSE_LOOP_FANx_HIGH_RPM(fan) (1 << ((fan) + 2)) +#define CLOSE_LOOP_FAN_PECI_ERR_MASK (3 << 0) +#define CLOSE_LOOP_FAN_PECI_ERR_CURR (0 << 0) +#define CLOSE_LOOP_FAN_PECI_ERR_VALUE (1 << 0) +#define CLOSE_LOOP_FAN_PECI_ERR_MAX (2 << 0) + +#define TEMP_SHIFT(temp) (((temp) % 2) * 4) +#define TEMP_TO_FAN_MAP(temp) (0x64 + (temp) / 2) +#define TEMPx_TO_FAN_MAP_MASK(temp) (7 << TEMP_SHIFT(temp)) +#define TEMPx_TO_FANy_MAP(temp, fan) (1 << (TEMP_SHIFT(temp) + (fan))) + +#define FAN_CTRL_TEMP_SRC(temp) (0x68 + (temp) / 2) +#define FAN_CTRL_TEMPx_SRC_MASK(temp) (7 << TEMP_SHIFT(temp)) +#define FAN_CTRL_TEMPx_SRCy(temp, src) ((src) << TEMP_SHIFT(temp)) + +#define FAN_DUTY_ON_PECI_ERROR 0x7a + +#define TABLEx_TEMP_POINTy(fan, pt) (0x80 + (0x10 * (fan)) + (pt)) +#define TABLEx_TARGET_POINTy(fan, pt) (0x85 + (0x10 * (fan)) + (pt)) + +/* Bank 1 */ + +#define PECI_CTRL_1 0x01 +#define PECI_CTRL_1_EN (1 << 7) +#define PECI_CTRL_1_MANUAL_EN (1 << 1) +#define PECI_CTRL_1_ROUTINE_EN (1 << 0) + +#define PECI_CTRL_3 0x03 +#define PECI_CTRL_3_EN_AGENTx(x) (1 << ((x) + 4)) +#define PECI_CTRL_3_HAS_DOM1_AGENTx(x) (1 << (x)) + +#define PECI_REPORT_TEMP_STYLE 0x04 +#define PECI_TEMP_STYLE_DOM0_AGENTx(x) (0 << ((x) + 1)) +#define PECI_TEMP_STYLE_DOM1_AGENTx(x) (1 << ((x) + 1)) +#define PECI_TEMP_STYLE_SINGLE (0 << 0) +#define PECI_TEMP_STYLE_HIGHEST (1 << 0) + +#define PECI_BASE_TEMP_AGENT(x) (0x09 + (x)) +#define PECI_BASE_TEMP_MAX (0x7f << 0) + +static inline int nct7802y_select_bank(struct device *const dev, const u8 bank) +{ + const int ret = i2c_dev_writeb_at(dev, BANK_SELECT, bank); + if (ret != CB_SUCCESS) + printk(BIOS_NOTICE, "nct7802y: Select bank failed: %d\n", ret); + return ret; +} + +static inline int nct7802y_write(struct device *const dev, + const u8 reg, const u8 value) +{ + return i2c_dev_writeb_at(dev, reg, value); +} + +static inline int nct7802y_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); +} + +void nct7802y_init_fan(struct device *dev); +void nct7802y_init_peci(struct device *dev); + +#endif /* DRIVERS_I2C_NCT7802Y_H */ diff --git a/src/drivers/i2c/nct7802y/nct7802y_fan.c b/src/drivers/i2c/nct7802y/nct7802y_fan.c new file mode 100644 index 0000000000..f0f3a3ced8 --- /dev/null +++ b/src/drivers/i2c/nct7802y/nct7802y_fan.c @@ -0,0 +1,111 @@ +/* + * 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 +#include + +#include "nct7802y.h" +#include "chip.h" + +static void init_fan(struct device *const dev, + const struct nct7802y_fan_config *const config, + const unsigned int fan) +{ + unsigned int temp; + unsigned int i; + + nct7802y_update(dev, FAN_ENABLE, 0, FANx_ENABLE(fan)); + + /* By default, do not map any temperature control to the fan. */ + for (temp = 0; temp < NCT7802Y_FAN_CNT; ++temp) { + nct7802y_update(dev, TEMP_TO_FAN_MAP(temp), + TEMPx_TO_FANy_MAP(temp, fan), 0); + } + + if (config->mode == FAN_MANUAL) { + nct7802y_write(dev, FAN_CTRL(fan), config->duty_cycle); + } else { + u8 set = 0, div, mul; + if (config->smart.mode == SMART_FAN_RPM) { + set |= CLOSE_LOOP_FANx_EN(fan); + div = 50; + if (config->smart.speed == FAN_SPPED_HIGHSPEED) { + set |= CLOSE_LOOP_FANx_HIGH_RPM(fan); + div = 100; + } + mul = 1; + } else { + /* SMART_FAN_DUTY is given in %, 100% == 255. */ + div = 100; + mul = 255; + } + nct7802y_update(dev, CLOSE_LOOP_FAN_RPM_CTRL, + CLOSE_LOOP_FANx_EN(fan) | + CLOSE_LOOP_FANx_HIGH_RPM(fan), + set); + + /* Map TEMPx to FANx to make things simple */ + nct7802y_update(dev, TEMP_TO_FAN_MAP(fan), + TEMPx_TO_FAN_MAP_MASK(fan), + TEMPx_TO_FANy_MAP(fan, fan)); + + nct7802y_update(dev, FAN_CTRL_TEMP_SRC(fan), + FAN_CTRL_TEMPx_SRC_MASK(fan), + FAN_CTRL_TEMPx_SRCy( + fan, config->smart.tempsrc)); + + for (i = 0; i < ARRAY_SIZE(config->smart.table); ++i) { + nct7802y_write(dev, TABLEx_TEMP_POINTy(fan, i), + config->smart.table[i].temp); + nct7802y_write(dev, TABLEx_TARGET_POINTy(fan, i), + (config->smart.table[i].target * mul) / div); + } + nct7802y_write(dev, TABLEx_TEMP_POINTy(fan, 4), + config->smart.critical_temp); + } +} + +void nct7802y_init_fan(struct device *const dev) +{ + const struct drivers_i2c_nct7802y_config *const config = dev->chip_info; + unsigned int i; + u8 set; + + if (nct7802y_select_bank(dev, 0) != CB_SUCCESS) + return; + + for (i = 0; i < NCT7802Y_FAN_CNT; ++i) { + if (config->fan[i].mode != FAN_IGNORE) + init_fan(dev, &config->fan[i], i); + } + + switch (config->on_pecierror) { + case PECI_ERROR_KEEP: + set = CLOSE_LOOP_FAN_PECI_ERR_CURR; + break; + case PECI_ERROR_VALUE: + set = CLOSE_LOOP_FAN_PECI_ERR_VALUE; + break; + case PECI_ERROR_FULLSPEED: + set = CLOSE_LOOP_FAN_PECI_ERR_MAX; + break; + default: + set = 0; + break; + } + nct7802y_update(dev, CLOSE_LOOP_FAN_RPM_CTRL, + CLOSE_LOOP_FAN_PECI_ERR_MASK, set); + nct7802y_write(dev, FAN_DUTY_ON_PECI_ERROR, config->pecierror_minduty); +} diff --git a/src/drivers/i2c/nct7802y/nct7802y_peci.c b/src/drivers/i2c/nct7802y/nct7802y_peci.c new file mode 100644 index 0000000000..26e62a3f09 --- /dev/null +++ b/src/drivers/i2c/nct7802y/nct7802y_peci.c @@ -0,0 +1,87 @@ +/* + * 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 +#include + +#include "nct7802y.h" +#include "chip.h" + +void nct7802y_init_peci(struct device *const dev) +{ + const struct drivers_i2c_nct7802y_config *const config = dev->chip_info; + unsigned int i, all_off = 1; + + /* Bank 1 can only be written to if PECI reading is enabled */ + if (nct7802y_select_bank(dev, 0) != CB_SUCCESS) + return; + nct7802y_update(dev, PECI_ENABLE, 0, PECI_ENABLE_AGENTx(0)); + + if (nct7802y_select_bank(dev, 1) != CB_SUCCESS) + return; + + for (i = 0; i < NCT7802Y_PECI_CNT; ++i) { + if (config->peci[i].mode != PECI_DISABLED) { + u8 ctrl3 = 0, style = 0; + switch (config->peci[i].mode) { + case PECI_DOMAIN_0: + ctrl3 = PECI_CTRL_3_EN_AGENTx(i); + style = PECI_TEMP_STYLE_DOM0_AGENTx(i) | + PECI_TEMP_STYLE_SINGLE; + break; + case PECI_DOMAIN_1: + ctrl3 = PECI_CTRL_3_EN_AGENTx(i) | + PECI_CTRL_3_HAS_DOM1_AGENTx(i); + style = PECI_TEMP_STYLE_DOM1_AGENTx(i) | + PECI_TEMP_STYLE_SINGLE; + break; + case PECI_HIGHEST: + ctrl3 = PECI_CTRL_3_EN_AGENTx(i) | + PECI_CTRL_3_HAS_DOM1_AGENTx(i); + style = PECI_TEMP_STYLE_HIGHEST; + break; + default: + break; + } + nct7802y_update(dev, PECI_CTRL_1, + PECI_CTRL_1_MANUAL_EN, + PECI_CTRL_1_EN | + PECI_CTRL_1_ROUTINE_EN); + nct7802y_update(dev, PECI_CTRL_3, + PECI_CTRL_3_HAS_DOM1_AGENTx(i), ctrl3); + nct7802y_update(dev, PECI_REPORT_TEMP_STYLE, + PECI_TEMP_STYLE_DOM1_AGENTx(i) | + PECI_TEMP_STYLE_HIGHEST, + style); + nct7802y_write(dev, PECI_BASE_TEMP_AGENT(i), + config->peci[i].base_temp); + all_off = 0; + } else { + nct7802y_update(dev, PECI_CTRL_3, + PECI_CTRL_3_EN_AGENTx(i), 0); + } + } + + if (all_off) + nct7802y_update(dev, PECI_CTRL_1, PECI_CTRL_1_EN, 0); + + /* Disable PECI #0 reading if we only enabled it to access bank 1 */ + if (config->peci[0].mode == PECI_DISABLED) { + if (nct7802y_select_bank(dev, 0) != CB_SUCCESS) + return; + + nct7802y_update(dev, PECI_ENABLE, PECI_ENABLE_AGENTx(0), 0); + } +} -- cgit v1.2.3