net: atlantic: add hwmon getter for MAC temperature
authorMark Starovoytov <mstarovoitov@marvell.com>
Mon, 20 Jul 2020 18:32:44 +0000 (21:32 +0300)
committerDavid S. Miller <davem@davemloft.net>
Tue, 21 Jul 2020 01:07:39 +0000 (18:07 -0700)
This patch adds the possibility to obtain MAC temperature via hwmon.
On A1 there are two separate temperature sensors.
On A2 there's only one temperature sensor, which is used for reporting
both MAC and PHY temperature.

Signed-off-by: Mark Starovoytov <mstarovoitov@marvell.com>
Signed-off-by: Igor Russkikh <irusskikh@marvell.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
drivers/net/ethernet/aquantia/atlantic/aq_drvinfo.c
drivers/net/ethernet/aquantia/atlantic/aq_drvinfo.h
drivers/net/ethernet/aquantia/atlantic/aq_hw.h
drivers/net/ethernet/aquantia/atlantic/hw_atl/hw_atl_b0.c
drivers/net/ethernet/aquantia/atlantic/hw_atl/hw_atl_llh.c
drivers/net/ethernet/aquantia/atlantic/hw_atl/hw_atl_llh.h
drivers/net/ethernet/aquantia/atlantic/hw_atl/hw_atl_llh_internal.h
drivers/net/ethernet/aquantia/atlantic/hw_atl/hw_atl_utils.c
drivers/net/ethernet/aquantia/atlantic/hw_atl/hw_atl_utils_fw2x.c
drivers/net/ethernet/aquantia/atlantic/hw_atl2/hw_atl2_utils_fw.c

index 6da65099047d22f0a865685437ccb2e8100aa748..d3526cd38f3d812f688c1b156200c74c4875b4bb 100644 (file)
@@ -1,5 +1,9 @@
 // SPDX-License-Identifier: GPL-2.0-only
-/* Copyright (C) 2014-2019 aQuantia Corporation. */
+/* Atlantic Network Driver
+ *
+ * Copyright (C) 2014-2019 aQuantia Corporation
+ * Copyright (C) 2019-2020 Marvell International Ltd.
+ */
 
 /* File aq_drvinfo.c: Definition of common code for firmware info in sys.*/
 
 #include <linux/uaccess.h>
 
 #include "aq_drvinfo.h"
+#include "aq_nic.h"
 
 #if IS_REACHABLE(CONFIG_HWMON)
+static const char * const atl_temp_label[] = {
+       "PHY Temperature",
+       "MAC Temperature",
+};
+
 static int aq_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
                         u32 attr, int channel, long *value)
 {
        struct aq_nic_s *aq_nic = dev_get_drvdata(dev);
+       int err = 0;
        int temp;
-       int err;
 
        if (!aq_nic)
                return -EIO;
 
-       if (type != hwmon_temp)
+       if (type != hwmon_temp || attr != hwmon_temp_input)
                return -EOPNOTSUPP;
 
-       if (!aq_nic->aq_fw_ops->get_phy_temp)
-               return -EOPNOTSUPP;
+       switch (channel) {
+       case 0:
+               if (!aq_nic->aq_fw_ops->get_phy_temp)
+                       return -EOPNOTSUPP;
 
-       switch (attr) {
-       case hwmon_temp_input:
                err = aq_nic->aq_fw_ops->get_phy_temp(aq_nic->aq_hw, &temp);
                *value = temp;
-               return err;
+               break;
+       case 1:
+               if (!aq_nic->aq_fw_ops->get_mac_temp &&
+                   !aq_nic->aq_hw_ops->hw_get_mac_temp)
+                       return -EOPNOTSUPP;
+
+               if (aq_nic->aq_fw_ops->get_mac_temp)
+                       err = aq_nic->aq_fw_ops->get_mac_temp(aq_nic->aq_hw, &temp);
+               else
+                       err = aq_nic->aq_hw_ops->hw_get_mac_temp(aq_nic->aq_hw, &temp);
+               *value = temp;
+               break;
        default:
                return -EOPNOTSUPP;
        }
+
+       return err;
 }
 
 static int aq_hwmon_read_string(struct device *dev,
@@ -49,28 +72,32 @@ static int aq_hwmon_read_string(struct device *dev,
        if (!aq_nic)
                return -EIO;
 
-       if (type != hwmon_temp)
+       if (type != hwmon_temp || attr != hwmon_temp_label)
                return -EOPNOTSUPP;
 
-       if (!aq_nic->aq_fw_ops->get_phy_temp)
+       if (channel < ARRAY_SIZE(atl_temp_label))
+               *str = atl_temp_label[channel];
+       else
                return -EOPNOTSUPP;
 
-       switch (attr) {
-       case hwmon_temp_label:
-               *str = "PHY Temperature";
-               return 0;
-       default:
-               return -EOPNOTSUPP;
-       }
+       return 0;
 }
 
 static umode_t aq_hwmon_is_visible(const void *data,
                                   enum hwmon_sensor_types type,
                                   u32 attr, int channel)
 {
+       const struct aq_nic_s *nic = data;
+
        if (type != hwmon_temp)
                return 0;
 
+       if (channel == 0 && !nic->aq_fw_ops->get_phy_temp)
+               return 0;
+       else if (channel == 1 && !nic->aq_fw_ops->get_mac_temp &&
+                !nic->aq_hw_ops->hw_get_mac_temp)
+               return 0;
+
        switch (attr) {
        case hwmon_temp_input:
        case hwmon_temp_label:
@@ -87,6 +114,7 @@ static const struct hwmon_ops aq_hwmon_ops = {
 };
 
 static u32 aq_hwmon_temp_config[] = {
+       HWMON_T_INPUT | HWMON_T_LABEL,
        HWMON_T_INPUT | HWMON_T_LABEL,
        0,
 };
index 23a0487893a7bb95baabbb477d681ee4088ef898..59113a20622a0c71615e87fac7625e516e7ad2cd 100644 (file)
@@ -1,14 +1,16 @@
 /* SPDX-License-Identifier: GPL-2.0-only */
-/* Copyright (C) 2014-2017 aQuantia Corporation. */
+/* Atlantic Network Driver
+ *
+ * Copyright (C) 2014-2019 aQuantia Corporation
+ * Copyright (C) 2019-2020 Marvell International Ltd.
+ */
 
 /* File aq_drvinfo.h: Declaration of common code for firmware info in sys.*/
 
 #ifndef AQ_DRVINFO_H
 #define AQ_DRVINFO_H
 
-#include "aq_nic.h"
-#include "aq_hw.h"
-#include "hw_atl/hw_atl_utils.h"
+struct net_device;
 
 int aq_drvinfo_init(struct net_device *ndev);
 
index 96d59ee3eb7050473e66edf9835f9856bb72e10c..95ee1336ac79bdaeec1312a3e9e5378a3690d47f 100644 (file)
@@ -333,6 +333,8 @@ struct aq_hw_ops {
        int (*hw_set_fc)(struct aq_hw_s *self, u32 fc, u32 tc);
 
        int (*hw_set_loopback)(struct aq_hw_s *self, u32 mode, bool enable);
+
+       int (*hw_get_mac_temp)(struct aq_hw_s *self, u32 *temp);
 };
 
 struct aq_fw_ops {
@@ -355,6 +357,8 @@ struct aq_fw_ops {
 
        int (*update_stats)(struct aq_hw_s *self);
 
+       int (*get_mac_temp)(struct aq_hw_s *self, int *temp);
+
        int (*get_phy_temp)(struct aq_hw_s *self, int *temp);
 
        u32 (*get_flow_control)(struct aq_hw_s *self, u32 *fcmode);
index ee74cad4a168920d056e82c1bcdfdef3738c9b83..34626eef2909f6122d9e499d4391eff7249eed16 100644 (file)
@@ -1581,6 +1581,48 @@ int hw_atl_b0_set_loopback(struct aq_hw_s *self, u32 mode, bool enable)
        return 0;
 }
 
+static u32 hw_atl_b0_ts_ready_and_latch_high_get(struct aq_hw_s *self)
+{
+       if (hw_atl_ts_ready_get(self) && hw_atl_ts_ready_latch_high_get(self))
+               return 1;
+
+       return 0;
+}
+
+static int hw_atl_b0_get_mac_temp(struct aq_hw_s *self, u32 *temp)
+{
+       bool ts_disabled;
+       int err;
+       u32 val;
+       u32 ts;
+
+       ts_disabled = (hw_atl_ts_power_down_get(self) == 1U);
+
+       if (ts_disabled) {
+               // Set AFE Temperature Sensor to on (off by default)
+               hw_atl_ts_power_down_set(self, 0U);
+
+               // Reset internal capacitors, biasing, and counters
+               hw_atl_ts_reset_set(self, 1);
+               hw_atl_ts_reset_set(self, 0);
+       }
+
+       err = readx_poll_timeout_atomic(hw_atl_b0_ts_ready_and_latch_high_get,
+                                       self, val, val == 1, 10000U, 500000U);
+       if (err)
+               return err;
+
+       ts = hw_atl_ts_data_get(self);
+       *temp = ts * ts * 16 / 100000 + 60 * ts - 83410;
+
+       if (ts_disabled) {
+               // Set AFE Temperature Sensor back to off
+               hw_atl_ts_power_down_set(self, 1U);
+       }
+
+       return 0;
+}
+
 const struct aq_hw_ops hw_atl_ops_b0 = {
        .hw_soft_reset        = hw_atl_utils_soft_reset,
        .hw_prepare           = hw_atl_utils_initfw,
@@ -1637,4 +1679,6 @@ const struct aq_hw_ops hw_atl_ops_b0 = {
        .hw_set_offload          = hw_atl_b0_hw_offload_set,
        .hw_set_loopback         = hw_atl_b0_set_loopback,
        .hw_set_fc               = hw_atl_b0_set_fc,
+
+       .hw_get_mac_temp         = hw_atl_b0_get_mac_temp,
 };
index 9c3debae425fc42abfb624677154effbb3e20be5..7b67bdd8a258ef562a0cc136e333e68df186f933 100644 (file)
 #include "hw_atl_llh_internal.h"
 #include "../aq_hw_utils.h"
 
+void hw_atl_ts_reset_set(struct aq_hw_s *aq_hw, u32 val)
+{
+       aq_hw_write_reg_bit(aq_hw, HW_ATL_TS_RESET_ADR,
+                           HW_ATL_TS_RESET_MSK,
+                           HW_ATL_TS_RESET_SHIFT,
+                           val);
+}
+
+void hw_atl_ts_power_down_set(struct aq_hw_s *aq_hw, u32 val)
+{
+       aq_hw_write_reg_bit(aq_hw, HW_ATL_TS_POWER_DOWN_ADR,
+                           HW_ATL_TS_POWER_DOWN_MSK,
+                           HW_ATL_TS_POWER_DOWN_SHIFT,
+                           val);
+}
+
+u32 hw_atl_ts_power_down_get(struct aq_hw_s *aq_hw)
+{
+       return aq_hw_read_reg_bit(aq_hw, HW_ATL_TS_POWER_DOWN_ADR,
+                                 HW_ATL_TS_POWER_DOWN_MSK,
+                                 HW_ATL_TS_POWER_DOWN_SHIFT);
+}
+
+u32 hw_atl_ts_ready_get(struct aq_hw_s *aq_hw)
+{
+       return aq_hw_read_reg_bit(aq_hw, HW_ATL_TS_READY_ADR,
+                                 HW_ATL_TS_READY_MSK,
+                                 HW_ATL_TS_READY_SHIFT);
+}
+
+u32 hw_atl_ts_ready_latch_high_get(struct aq_hw_s *aq_hw)
+{
+       return aq_hw_read_reg_bit(aq_hw, HW_ATL_TS_READY_LATCH_HIGH_ADR,
+                                 HW_ATL_TS_READY_LATCH_HIGH_MSK,
+                                 HW_ATL_TS_READY_LATCH_HIGH_SHIFT);
+}
+
+u32 hw_atl_ts_data_get(struct aq_hw_s *aq_hw)
+{
+       return aq_hw_read_reg_bit(aq_hw, HW_ATL_TS_DATA_OUT_ADR,
+                                 HW_ATL_TS_DATA_OUT_MSK,
+                                 HW_ATL_TS_DATA_OUT_SHIFT);
+}
+
 /* global */
 void hw_atl_reg_glb_cpu_sem_set(struct aq_hw_s *aq_hw, u32 glb_cpu_sem,
                                u32 semaphore)
index f0954711df24c06854a53b3c5d47a0bf68ddf77f..58f5ee0a6214f21f70df8e5731e0f6a4481423fe 100644 (file)
 
 struct aq_hw_s;
 
+/* set temperature sense reset */
+void hw_atl_ts_reset_set(struct aq_hw_s *aq_hw, u32 val);
+
+/* set temperature sense power down */
+void hw_atl_ts_power_down_set(struct aq_hw_s *aq_hw, u32 val);
+
+/* get temperature sense power down */
+u32 hw_atl_ts_power_down_get(struct aq_hw_s *aq_hw);
+
+/* get temperature sense ready */
+u32 hw_atl_ts_ready_get(struct aq_hw_s *aq_hw);
+
+/* get temperature sense ready latch high */
+u32 hw_atl_ts_ready_latch_high_get(struct aq_hw_s *aq_hw);
+
+/* get temperature sense data */
+u32 hw_atl_ts_data_get(struct aq_hw_s *aq_hw);
+
 /* global */
 
 /* set global microprocessor semaphore */
index ee11cb88325e5a729a95b67940d6b5b2482facc8..4a6467031b9edb4102dc059c5eeb9a63196b6b1c 100644 (file)
 #ifndef HW_ATL_LLH_INTERNAL_H
 #define HW_ATL_LLH_INTERNAL_H
 
+/* COM Temperature Sense Reset Bitfield Definitions */
+#define HW_ATL_TS_RESET_ADR 0x00003100
+#define HW_ATL_TS_RESET_MSK 0x00000004
+#define HW_ATL_TS_RESET_SHIFT 2
+#define HW_ATL_TS_RESET_WIDTH 1
+
+/* COM Temperature Sense Power Down Bitfield Definitions */
+#define HW_ATL_TS_POWER_DOWN_ADR 0x00003100
+#define HW_ATL_TS_POWER_DOWN_MSK 0x00000001
+#define HW_ATL_TS_POWER_DOWN_SHIFT 0
+#define HW_ATL_TS_POWER_DOWN_WIDTH 1
+
+/* COM Temperature Sense Ready Bitfield Definitions */
+#define HW_ATL_TS_READY_ADR 0x00003120
+#define HW_ATL_TS_READY_MSK 0x80000000
+#define HW_ATL_TS_READY_SHIFT 31
+#define HW_ATL_TS_READY_WIDTH 1
+
+/*  COM Temperature Sense Ready Latch High Bitfield Definitions */
+#define HW_ATL_TS_READY_LATCH_HIGH_ADR 0x00003120
+#define HW_ATL_TS_READY_LATCH_HIGH_MSK 0x40000000
+#define HW_ATL_TS_READY_LATCH_HIGH_SHIFT 30
+#define HW_ATL_TS_READY_LATCH_HIGH_WIDTH 1
+
+/* COM Temperature Sense Data Out [B:0] Bitfield Definitions */
+#define HW_ATL_TS_DATA_OUT_ADR 0x00003120
+#define HW_ATL_TS_DATA_OUT_MSK 0x00000FFF
+#define HW_ATL_TS_DATA_OUT_SHIFT 0
+#define HW_ATL_TS_DATA_OUT_WIDTH 12
+
 /* global microprocessor semaphore  definitions
  * base address: 0x000003a0
  * parameter: semaphore {s} | stride size 0x4 | range [0, 15]
index cacab3352cb8e2e70dfd2831f67ed0a691e2396e..404cbf60d3f2ffecfc59ffb3308fcabfa2e2949c 100644 (file)
@@ -1066,6 +1066,7 @@ const struct aq_fw_ops aq_fw_1x_ops = {
        .set_state = hw_atl_utils_mpi_set_state,
        .update_link_status = hw_atl_utils_mpi_get_link_status,
        .update_stats = hw_atl_utils_update_stats,
+       .get_mac_temp = NULL,
        .get_phy_temp = NULL,
        .set_power = aq_fw1x_set_power,
        .set_eee_rate = NULL,
index 013676cd38e480bd685a36b668885331dca0061c..93c06dfa6c55400f90229b7c676bd99c9105ecea 100644 (file)
@@ -353,7 +353,7 @@ static int aq_fw2x_get_phy_temp(struct aq_hw_s *self, int *temp)
        /* Convert PHY temperature from 1/256 degree Celsius
         * to 1/1000 degree Celsius.
         */
-       *temp = (temp_res & 0xFFFF) * 1000 / 256;
+       *temp = (int16_t)(temp_res & 0xFFFF) * 1000 / 256;
 
        return 0;
 }
@@ -681,6 +681,7 @@ const struct aq_fw_ops aq_fw_2x_ops = {
        .set_state          = aq_fw2x_set_state,
        .update_link_status = aq_fw2x_update_link_status,
        .update_stats       = aq_fw2x_update_stats,
+       .get_mac_temp       = NULL,
        .get_phy_temp       = aq_fw2x_get_phy_temp,
        .set_power          = aq_fw2x_set_power,
        .set_eee_rate       = aq_fw2x_set_eee_rate,
index a8ce9a2c1c51b85c9919fb90864602827d273faf..85628acbcc1d66ce7771877b0f7fe784ea03f174 100644 (file)
@@ -379,6 +379,25 @@ static int aq_a2_fw_update_stats(struct aq_hw_s *self)
        return 0;
 }
 
+static int aq_a2_fw_get_phy_temp(struct aq_hw_s *self, int *temp)
+{
+       struct phy_health_monitor_s phy_health_monitor;
+
+       hw_atl2_shared_buffer_read_safe(self, phy_health_monitor,
+                                       &phy_health_monitor);
+
+       *temp = (int8_t)phy_health_monitor.phy_temperature * 1000;
+       return 0;
+}
+
+static int aq_a2_fw_get_mac_temp(struct aq_hw_s *self, int *temp)
+{
+       /* There's only one temperature sensor on A2, use it for
+        * both MAC and PHY.
+        */
+       return aq_a2_fw_get_phy_temp(self, temp);
+}
+
 static int aq_a2_fw_set_eee_rate(struct aq_hw_s *self, u32 speed)
 {
        struct link_options_s link_options;
@@ -510,6 +529,8 @@ const struct aq_fw_ops aq_a2_fw_ops = {
        .set_state          = aq_a2_fw_set_state,
        .update_link_status = aq_a2_fw_update_link_status,
        .update_stats       = aq_a2_fw_update_stats,
+       .get_mac_temp       = aq_a2_fw_get_mac_temp,
+       .get_phy_temp       = aq_a2_fw_get_phy_temp,
        .set_eee_rate       = aq_a2_fw_set_eee_rate,
        .get_eee_rate       = aq_a2_fw_get_eee_rate,
        .set_flow_control   = aq_a2_fw_set_flow_control,