From 07f84ed283b913cbdf87181ae2ed65467d923df5 Mon Sep 17 00:00:00 2001
From: Vincent Chen <vincent.chen@sifive.com>
Date: Mon, 24 Jan 2022 02:57:40 -0800
Subject: [PATCH 2/2] board: sifive: spl: Set remote thermal of TMP451 to 85
 deg C for the unmatched board

For TMP451 on the unmatched board, the default value of the remote
thermal threshold is 108 deg C. This commit initilizes it to 85 deg C at SPL.
---
 board/sifive/unmatched/spl.c       | 29 +++++++++++++++++++++++++++++
 drivers/misc/Kconfig               | 10 ++++++++++
 include/configs/sifive-unmatched.h |  4 ++++
 scripts/config_whitelist.txt       |  1 +
 4 files changed, 44 insertions(+)

--- a/board/sifive/unmatched/spl.c
+++ b/board/sifive/unmatched/spl.c
@@ -10,6 +10,8 @@
 #include <spl.h>
 #include <misc.h>
 #include <log.h>
+#include <config.h>
+#include <i2c.h>
 #include <linux/delay.h>
 #include <linux/io.h>
 #include <asm/gpio.h>
@@ -26,6 +28,27 @@
 #define MODE_SELECT_SD		0xb
 #define MODE_SELECT_MASK	GENMASK(3, 0)
 
+#define TMP451_REMOTE_THERM_LIMIT_REG_OFFSET	0x19
+#define TMP451_REMOTE_THERM_LIMIT_INIT_VALUE	0x55
+
+static inline int init_tmp451_remote_therm_limit(void)
+{
+	struct udevice *dev;
+	unsigned char r_therm_limit = TMP451_REMOTE_THERM_LIMIT_INIT_VALUE;
+	int ret;
+
+	ret = i2c_get_chip_for_busnum(CONFIG_SYS_TMP451_BUS_NUM,
+				      CONFIG_SYS_I2C_TMP451_ADDR,
+				      CONFIG_SYS_I2C_TMP451_ADDR_LEN,
+				      &dev);
+
+	if (!ret)
+		ret = dm_i2c_write(dev, TMP451_REMOTE_THERM_LIMIT_REG_OFFSET,
+				   &r_therm_limit,
+				   sizeof(unsigned char));
+	return ret;
+}
+
 static inline int spl_reset_device_by_gpio(const char *label, int pin, int low_width)
 {
 	int ret;
@@ -92,6 +115,12 @@ int spl_board_init_f(void)
 
 	pwm_device_init();
 
+	ret = init_tmp451_remote_therm_limit();
+	if (ret) {
+		debug("TMP451 remote THERM limit init failed: %d\n", ret);
+		goto end;
+	}
+
 	ret = spl_gemgxl_init();
 	if (ret) {
 		debug("Gigabit ethernet PHY (VSC8541) init failed: %d\n", ret);
--- a/drivers/misc/Kconfig
+++ b/drivers/misc/Kconfig
@@ -536,8 +536,18 @@ config SYS_I2C_EEPROM_ADDR
 	depends on ID_EEPROM || I2C_EEPROM || SPL_I2C_EEPROM || CMD_EEPROM || ENV_IS_IN_EEPROM
 	default 0
 
+config SYS_I2C_TMP451_ADDR
+	hex "Chip address of the TMP451 device"
+	default 0
+
 if I2C_EEPROM
 
+config SYS_I2C_TMP451_ADDR_LEN
+	int "Length in bytes of the TMP451 memory array address"
+	default 1
+	help
+	  Note: This is NOT the chip address length!
+
 config SYS_I2C_EEPROM_ADDR_OVERFLOW
 	hex "EEPROM Address Overflow"
 	default 0x0
--- a/include/configs/sifive-unmatched.h
+++ b/include/configs/sifive-unmatched.h
@@ -15,6 +15,10 @@
 
 #define CONFIG_STANDALONE_LOAD_ADDR	0x80200000
 
+#define CONFIG_SYS_TMP451_BUS_NUM	0
+#define CONFIG_SYS_I2C_TMP451_ADDR	0x4c
+#define CONFIG_SYS_I2C_TMP451_ADDR_LEN	0x1
+
 /* Environment options */
 
 #define BOOT_TARGET_DEVICES(func) \
--- a/scripts/config_whitelist.txt
+++ b/scripts/config_whitelist.txt
@@ -1268,6 +1268,7 @@ CONFIG_SYS_TIMER_BASE
 CONFIG_SYS_TIMER_COUNTER
 CONFIG_SYS_TIMER_COUNTS_DOWN
 CONFIG_SYS_TIMER_RATE
+CONFIG_SYS_TMP451_BUS_NUM
 CONFIG_SYS_TMPVIRT
 CONFIG_SYS_TSEC1_OFFSET
 CONFIG_SYS_TX_ETH_BUFFER
