@@ -160,3 +160,14 @@ config SCSI_UFS_BSG
Select this if you need a bsg device node for your UFS controller.
If unsure, say N.
+
+config THERMAL_UFS
+ bool "Thermal UFS"
+ depends on THERMAL && SCSI_UFSHCD
+ help
+ A UFS3.0 feature that allows using the ufs device as a temperature
+ sensor. it provide notification to the host when the UFS device
+ case temperature approaches its pre-defined boundaries.
+
+ Select Y to enable this feature, otherwise say N.
+ If unsure, say N.
\ No newline at end of file
@@ -12,3 +12,4 @@ obj-$(CONFIG_SCSI_UFSHCD_PLATFORM) += ufshcd-pltfrm.o
obj-$(CONFIG_SCSI_UFS_HISI) += ufs-hisi.o
obj-$(CONFIG_SCSI_UFS_MEDIATEK) += ufs-mediatek.o
obj-$(CONFIG_SCSI_UFS_TI_J721E) += ti-j721e-ufs.o
+obj-$(CONFIG_THERMAL_UFS) += ufs-thermal.o
new file mode 100644
@@ -0,0 +1,123 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * thermal ufs
+ *
+ * Copyright (C) 2020 Western Digital Corporation
+ */
+#include <linux/thermal.h>
+#include "ufs-thermal.h"
+
+enum {
+ UFS_THERM_MAX_TEMP,
+ UFS_THERM_HIGH_TEMP,
+ UFS_THERM_LOW_TEMP,
+ UFS_THERM_MIN_TEMP,
+
+ /* keep last */
+ UFS_THERM_MAX_TRIPS
+};
+
+/**
+ *struct ufs_thermal - thermal zone related data
+ * @tzone: thermal zone device data
+ */
+static struct ufs_thermal {
+ struct thermal_zone_device *zone;
+} thermal;
+
+static struct thermal_zone_device_ops ufs_thermal_ops = {
+ .get_temp = NULL,
+ .get_trip_temp = NULL,
+ .get_trip_type = NULL,
+};
+
+static int ufs_thermal_enable_ee(struct ufs_hba *hba)
+{
+ /* later */
+ return -EINVAL;
+}
+
+static void ufs_thermal_zone_unregister(struct ufs_hba *hba)
+{
+ if (thermal.zone) {
+ dev_dbg(hba->dev, "Thermal zone device unregister\n");
+ thermal_zone_device_unregister(thermal.zone);
+ thermal.zone = NULL;
+ }
+}
+
+static int ufs_thermal_register(struct ufs_hba *hba)
+{
+ int err = 0;
+ char name[THERMAL_NAME_LENGTH] = {};
+
+ snprintf(name, THERMAL_NAME_LENGTH, "ufs_storage_%d",
+ hba->host->host_no);
+
+ thermal.zone = thermal_zone_device_register(name, UFS_THERM_MAX_TRIPS,
+ 0, hba, &ufs_thermal_ops, NULL, 0, 0);
+ if (IS_ERR(thermal.zone)) {
+ err = PTR_ERR(thermal.zone);
+ dev_err(hba->dev, "Failed to register to thermal zone, err %d\n",
+ err);
+ thermal.zone = NULL;
+ goto out;
+ }
+
+ /* thermal support is enabled only after successful
+ * enablement of thermal exception
+ */
+ if (ufs_thermal_enable_ee(hba)) {
+ dev_info(hba->dev, "Failed to enable thermal exception\n");
+ ufs_thermal_zone_unregister(hba);
+ err = -EINVAL;
+ }
+
+out:
+ return err;
+}
+
+int ufs_thermal_probe(struct ufs_hba *hba)
+{
+ u8 ufs_features;
+ u8 *desc_buf = NULL;
+ int err = -EINVAL;
+
+ if (!ufshcd_thermal_management_enabled(hba))
+ goto out;
+
+ desc_buf = kzalloc(hba->desc_size.dev_desc, GFP_KERNEL);
+ if (!desc_buf) {
+ err = -ENOMEM;
+ goto out;
+ }
+
+ if (ufshcd_read_desc_param(hba, QUERY_DESC_IDN_DEVICE, 0, 0, desc_buf,
+ hba->desc_size.dev_desc))
+ goto out;
+
+
+ ufs_features = desc_buf[DEVICE_DESC_PARAM_UFS_FEAT] &
+ (UFS_FEATURE_HTEMP | UFS_FEATURE_LTEMP);
+ if (!ufs_features)
+ goto out;
+
+ err = ufs_thermal_register(hba);
+ if (err)
+ goto out;
+
+ hba->thermal_features = ufs_features;
+
+out:
+ kfree(desc_buf);
+ return err;
+}
+
+void ufs_thermal_remove(struct ufs_hba *hba)
+{
+ if (!ufshcd_thermal_management_enabled(hba))
+ return;
+
+ ufs_thermal_zone_unregister(hba);
+ hba->thermal_features = 0;
+}
new file mode 100644
@@ -0,0 +1,19 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (C) 2018 Western Digital Corporation
+ */
+#ifndef UFS_THERMAL_H
+#define UFS_THERMAL_H
+
+#include "ufshcd.h"
+#include "ufs.h"
+
+#ifdef CONFIG_THERMAL_UFS
+void ufs_thermal_remove(struct ufs_hba *hba);
+int ufs_thermal_probe(struct ufs_hba *hba);
+#else
+static inline void ufs_thermal_remove(struct ufs_hba *hba) {}
+static inline int ufs_thermal_probe(struct ufs_hba *hba) {return 0; }
+#endif /* CONFIG_THERMAL_UFS */
+
+#endif /* UFS_THERMAL_H */
@@ -332,6 +332,17 @@ enum {
UFSHCD_AMP = 3,
};
+/* UFS Features - to decode bUFSFeaturesSupport */
+enum {
+ UFS_FEATURE_FFU = BIT(0),
+ UFS_FEATURE_PSA = BIT(1),
+ UFS_FEATURE_LIFE = BIT(2),
+ UFS_FEATURE_REFRESH = BIT(3),
+ UFS_FEATURE_HTEMP = BIT(4),
+ UFS_FEATURE_LTEMP = BIT(5),
+ UFS_FEATURE_ETEMP = BIT(6),
+};
+
#define POWER_DESC_MAX_SIZE 0x62
#define POWER_DESC_MAX_ACTV_ICC_LVLS 16
@@ -47,6 +47,7 @@
#include "unipro.h"
#include "ufs-sysfs.h"
#include "ufs_bsg.h"
+#include "ufs-thermal.h"
#define CREATE_TRACE_POINTS
#include <trace/events/ufs.h>
@@ -7111,6 +7112,7 @@ static int ufshcd_probe_hba(struct ufs_hba *hba, bool async)
/* Enable Auto-Hibernate if configured */
ufshcd_auto_hibern8_enable(hba);
+ ufs_thermal_probe(hba);
out:
@@ -8278,6 +8280,7 @@ int ufshcd_shutdown(struct ufs_hba *hba)
*/
void ufshcd_remove(struct ufs_hba *hba)
{
+ ufs_thermal_remove(hba);
ufs_bsg_remove(hba);
ufs_sysfs_remove_nodes(hba->dev);
blk_cleanup_queue(hba->tmf_queue);
@@ -730,6 +730,11 @@ struct ufs_hba {
struct device bsg_dev;
struct request_queue *bsg_queue;
+
+#define UFSHCD_CAP_THERMAL_MANAGEMENT (1 << 7)
+
+ u8 thermal_features;
+
};
/* Returns true if clocks can be gated. Otherwise false */
@@ -754,6 +759,11 @@ static inline bool ufshcd_is_rpm_autosuspend_allowed(struct ufs_hba *hba)
return hba->caps & UFSHCD_CAP_RPM_AUTOSUSPEND;
}
+static inline bool ufshcd_thermal_management_enabled(struct ufs_hba *hba)
+{
+ return hba->caps & UFSHCD_CAP_THERMAL_MANAGEMENT;
+}
+
static inline bool ufshcd_is_intr_aggr_allowed(struct ufs_hba *hba)
{
/* DWC UFS Core has the Interrupt aggregation feature but is not detectable*/