Resguardo inicial

This commit is contained in:
lastdragon
2026-09-06 12:53:21 -06:00
parent 662efe3c2a
commit ad2088d178
94853 changed files with 43200404 additions and 0 deletions
@@ -0,0 +1,6 @@
# SPDX-License-Identifier: GPL-2.0-only
config AQUANTIA_PHY
tristate "Aquantia PHYs"
select CRC_ITU_T
help
Currently supports the Aquantia AQ1202, AQ2104, AQR105, AQR405
@@ -0,0 +1,6 @@
# SPDX-License-Identifier: GPL-2.0
aquantia-objs += aquantia_main.o aquantia_firmware.o aquantia_leds.o
ifdef CONFIG_HWMON
aquantia-objs += aquantia_hwmon.o
endif
obj-$(CONFIG_AQUANTIA_PHY) += aquantia.o
@@ -0,0 +1,259 @@
/* SPDX-License-Identifier: GPL-2.0 */
/* HWMON driver for Aquantia PHY
*
* Author: Nikita Yushchenko <nikita.yoush@cogentembedded.com>
* Author: Andrew Lunn <andrew@lunn.ch>
* Author: Heiner Kallweit <hkallweit1@gmail.com>
*/
#ifndef AQUANTIA_H
#define AQUANTIA_H
#include <linux/device.h>
#include <linux/phy.h>
/* Vendor specific 1, MDIO_MMD_VEND1 */
#define VEND1_GLOBAL_SC 0x0
#define VEND1_GLOBAL_SC_SOFT_RESET BIT(15)
#define VEND1_GLOBAL_SC_LOW_POWER BIT(11)
#define VEND1_GLOBAL_FW_ID 0x0020
#define VEND1_GLOBAL_FW_ID_MAJOR GENMASK(15, 8)
#define VEND1_GLOBAL_FW_ID_MINOR GENMASK(7, 0)
#define VEND1_GLOBAL_MAILBOX_INTERFACE1 0x0200
#define VEND1_GLOBAL_MAILBOX_INTERFACE1_EXECUTE BIT(15)
#define VEND1_GLOBAL_MAILBOX_INTERFACE1_WRITE BIT(14)
#define VEND1_GLOBAL_MAILBOX_INTERFACE1_CRC_RESET BIT(12)
#define VEND1_GLOBAL_MAILBOX_INTERFACE1_BUSY BIT(8)
#define VEND1_GLOBAL_MAILBOX_INTERFACE2 0x0201
#define VEND1_GLOBAL_MAILBOX_INTERFACE3 0x0202
#define VEND1_GLOBAL_MAILBOX_INTERFACE3_MSW_ADDR_MASK GENMASK(15, 0)
#define VEND1_GLOBAL_MAILBOX_INTERFACE3_MSW_ADDR(x) FIELD_PREP(VEND1_GLOBAL_MAILBOX_INTERFACE3_MSW_ADDR_MASK, (u16)((x) >> 16))
#define VEND1_GLOBAL_MAILBOX_INTERFACE4 0x0203
#define VEND1_GLOBAL_MAILBOX_INTERFACE4_LSW_ADDR_MASK GENMASK(15, 2)
#define VEND1_GLOBAL_MAILBOX_INTERFACE4_LSW_ADDR(x) FIELD_PREP(VEND1_GLOBAL_MAILBOX_INTERFACE4_LSW_ADDR_MASK, (u16)(x))
#define VEND1_GLOBAL_MAILBOX_INTERFACE5 0x0204
#define VEND1_GLOBAL_MAILBOX_INTERFACE5_MSW_DATA_MASK GENMASK(15, 0)
#define VEND1_GLOBAL_MAILBOX_INTERFACE5_MSW_DATA(x) FIELD_PREP(VEND1_GLOBAL_MAILBOX_INTERFACE5_MSW_DATA_MASK, (u16)((x) >> 16))
#define VEND1_GLOBAL_MAILBOX_INTERFACE6 0x0205
#define VEND1_GLOBAL_MAILBOX_INTERFACE6_LSW_DATA_MASK GENMASK(15, 0)
#define VEND1_GLOBAL_MAILBOX_INTERFACE6_LSW_DATA(x) FIELD_PREP(VEND1_GLOBAL_MAILBOX_INTERFACE6_LSW_DATA_MASK, (u16)(x))
/* The following registers all have similar layouts; first the registers... */
#define VEND1_GLOBAL_CFG_10M 0x0310
#define VEND1_GLOBAL_CFG_100M 0x031b
#define VEND1_GLOBAL_CFG_1G 0x031c
#define VEND1_GLOBAL_CFG_2_5G 0x031d
#define VEND1_GLOBAL_CFG_5G 0x031e
#define VEND1_GLOBAL_CFG_10G 0x031f
/* ...and now the fields */
#define VEND1_GLOBAL_CFG_SERDES_MODE GENMASK(2, 0)
#define VEND1_GLOBAL_CFG_SERDES_MODE_XFI 0
#define VEND1_GLOBAL_CFG_SERDES_MODE_SGMII 3
#define VEND1_GLOBAL_CFG_SERDES_MODE_OCSGMII 4
#define VEND1_GLOBAL_CFG_SERDES_MODE_XFI5G 6
#define VEND1_GLOBAL_CFG_AUTONEG_ENA BIT(3)
#define VEND1_GLOBAL_CFG_RATE_ADAPT GENMASK(8, 7)
#define VEND1_GLOBAL_CFG_RATE_ADAPT_NONE 0
#define VEND1_GLOBAL_CFG_RATE_ADAPT_USX 1
#define VEND1_GLOBAL_CFG_RATE_ADAPT_PAUSE 2
/* Vendor specific 1, MDIO_MMD_VEND2 */
#define VEND1_GLOBAL_CONTROL2 0xc001
#define VEND1_GLOBAL_CONTROL2_UP_RUN_STALL_RST BIT(15)
#define VEND1_GLOBAL_CONTROL2_UP_RUN_STALL_OVD BIT(6)
#define VEND1_GLOBAL_CONTROL2_UP_RUN_STALL BIT(0)
#define VEND1_GLOBAL_LED_PROV 0xc430
#define AQR_LED_PROV(x) (VEND1_GLOBAL_LED_PROV + (x))
#define VEND1_GLOBAL_LED_PROV_LINK2500 BIT(14)
#define VEND1_GLOBAL_LED_PROV_LINK5000 BIT(15)
#define VEND1_GLOBAL_LED_PROV_FORCE_ON BIT(8)
#define VEND1_GLOBAL_LED_PROV_LINK10000 BIT(7)
#define VEND1_GLOBAL_LED_PROV_LINK1000 BIT(6)
#define VEND1_GLOBAL_LED_PROV_LINK100 BIT(5)
#define VEND1_GLOBAL_LED_PROV_RX_ACT BIT(3)
#define VEND1_GLOBAL_LED_PROV_TX_ACT BIT(2)
#define VEND1_GLOBAL_LED_PROV_ACT_STRETCH GENMASK(0, 1)
#define VEND1_GLOBAL_LED_PROV_LINK_MASK (VEND1_GLOBAL_LED_PROV_LINK100 | \
VEND1_GLOBAL_LED_PROV_LINK1000 | \
VEND1_GLOBAL_LED_PROV_LINK10000 | \
VEND1_GLOBAL_LED_PROV_LINK5000 | \
VEND1_GLOBAL_LED_PROV_LINK2500)
#define VEND1_GLOBAL_LED_DRIVE 0xc438
#define VEND1_GLOBAL_LED_DRIVE_VDD BIT(1)
#define AQR_LED_DRIVE(x) (VEND1_GLOBAL_LED_DRIVE + (x))
#define VEND1_THERMAL_PROV_HIGH_TEMP_FAIL 0xc421
#define VEND1_THERMAL_PROV_LOW_TEMP_FAIL 0xc422
#define VEND1_THERMAL_PROV_HIGH_TEMP_WARN 0xc423
#define VEND1_THERMAL_PROV_LOW_TEMP_WARN 0xc424
#define VEND1_THERMAL_STAT1 0xc820
#define VEND1_THERMAL_STAT2 0xc821
#define VEND1_THERMAL_STAT2_VALID BIT(0)
#define VEND1_GENERAL_STAT1 0xc830
#define VEND1_GENERAL_STAT1_HIGH_TEMP_FAIL BIT(14)
#define VEND1_GENERAL_STAT1_LOW_TEMP_FAIL BIT(13)
#define VEND1_GENERAL_STAT1_HIGH_TEMP_WARN BIT(12)
#define VEND1_GENERAL_STAT1_LOW_TEMP_WARN BIT(11)
#define VEND1_GLOBAL_GEN_STAT2 0xc831
#define VEND1_GLOBAL_GEN_STAT2_OP_IN_PROG BIT(15)
#define VEND1_GLOBAL_RSVD_STAT1 0xc885
#define VEND1_GLOBAL_RSVD_STAT1_FW_BUILD_ID GENMASK(7, 4)
#define VEND1_GLOBAL_RSVD_STAT1_PROV_ID GENMASK(3, 0)
#define VEND1_GLOBAL_RSVD_STAT9 0xc88d
#define VEND1_GLOBAL_RSVD_STAT9_MODE GENMASK(7, 0)
#define VEND1_GLOBAL_RSVD_STAT9_1000BT2 0x23
/* MDIO_MMD_C22EXT */
#define MDIO_C22EXT_STAT_SGMII_RX_GOOD_FRAMES 0xd292
#define MDIO_C22EXT_STAT_SGMII_RX_BAD_FRAMES 0xd294
#define MDIO_C22EXT_STAT_SGMII_RX_FALSE_CARRIER 0xd297
#define MDIO_C22EXT_STAT_SGMII_TX_GOOD_FRAMES 0xd313
#define MDIO_C22EXT_STAT_SGMII_TX_BAD_FRAMES 0xd315
#define MDIO_C22EXT_STAT_SGMII_TX_FALSE_CARRIER 0xd317
#define MDIO_C22EXT_STAT_SGMII_TX_COLLISIONS 0xd318
#define MDIO_C22EXT_STAT_SGMII_TX_LINE_COLLISIONS 0xd319
#define MDIO_C22EXT_STAT_SGMII_TX_FRAME_ALIGN_ERR 0xd31a
#define MDIO_C22EXT_STAT_SGMII_TX_RUNT_FRAMES 0xd31b
#define VEND1_GLOBAL_INT_STD_STATUS 0xfc00
#define VEND1_GLOBAL_INT_VEND_STATUS 0xfc01
#define VEND1_GLOBAL_INT_STD_MASK 0xff00
#define VEND1_GLOBAL_INT_STD_MASK_PMA1 BIT(15)
#define VEND1_GLOBAL_INT_STD_MASK_PMA2 BIT(14)
#define VEND1_GLOBAL_INT_STD_MASK_PCS1 BIT(13)
#define VEND1_GLOBAL_INT_STD_MASK_PCS2 BIT(12)
#define VEND1_GLOBAL_INT_STD_MASK_PCS3 BIT(11)
#define VEND1_GLOBAL_INT_STD_MASK_PHY_XS1 BIT(10)
#define VEND1_GLOBAL_INT_STD_MASK_PHY_XS2 BIT(9)
#define VEND1_GLOBAL_INT_STD_MASK_AN1 BIT(8)
#define VEND1_GLOBAL_INT_STD_MASK_AN2 BIT(7)
#define VEND1_GLOBAL_INT_STD_MASK_GBE BIT(6)
#define VEND1_GLOBAL_INT_STD_MASK_ALL BIT(0)
#define VEND1_GLOBAL_INT_VEND_MASK 0xff01
#define VEND1_GLOBAL_INT_VEND_MASK_PMA BIT(15)
#define VEND1_GLOBAL_INT_VEND_MASK_PCS BIT(14)
#define VEND1_GLOBAL_INT_VEND_MASK_PHY_XS BIT(13)
#define VEND1_GLOBAL_INT_VEND_MASK_AN BIT(12)
#define VEND1_GLOBAL_INT_VEND_MASK_GBE BIT(11)
#define VEND1_GLOBAL_INT_VEND_MASK_GLOBAL1 BIT(2)
#define VEND1_GLOBAL_INT_VEND_MASK_GLOBAL2 BIT(1)
#define VEND1_GLOBAL_INT_VEND_MASK_GLOBAL3 BIT(0)
#define AQR_MAX_LEDS 3
/* Custom driver definitions for constructing a single variable out of
* aggregate firmware build information. These do not represent hardware
* fields.
*/
#define AQR_FW_FINGERPRINT_MAJOR GENMASK_ULL(63, 56)
#define AQR_FW_FINGERPRINT_MINOR GENMASK_ULL(55, 48)
#define AQR_FW_FINGERPRINT_BUILD_ID GENMASK_ULL(47, 40)
#define AQR_FW_FINGERPRINT_PROV_ID GENMASK_ULL(39, 32)
#define AQR_FW_FINGERPRINT_MISC_ID GENMASK_ULL(31, 16)
#define AQR_FW_FINGERPRINT_MISC_VER GENMASK_ULL(15, 0)
#define AQR_FW_FINGERPRINT(major, minor, build_id, prov_id, misc_id, misc_ver) \
(FIELD_PREP(AQR_FW_FINGERPRINT_MAJOR, major) | \
FIELD_PREP(AQR_FW_FINGERPRINT_MINOR, minor) | \
FIELD_PREP(AQR_FW_FINGERPRINT_BUILD_ID, build_id) | \
FIELD_PREP(AQR_FW_FINGERPRINT_PROV_ID, prov_id) | \
FIELD_PREP(AQR_FW_FINGERPRINT_MISC_ID, misc_id) | \
FIELD_PREP(AQR_FW_FINGERPRINT_MISC_VER, misc_ver))
/* 10G-QXGMII firmware for NXP SPF-30841 riser board (AQR412C) */
#define AQR_G3_V4_3_C_AQR_NXP_SPF_30841_MUSX_ID40019_VER1198 \
AQR_FW_FINGERPRINT(4, 3, 0xc, 1, 40019, 1198)
struct aqr107_hw_stat {
const char *name;
int reg;
int size;
};
#define SGMII_STAT(n, r, s) { n, MDIO_C22EXT_STAT_SGMII_ ## r, s }
static const struct aqr107_hw_stat aqr107_hw_stats[] = {
SGMII_STAT("sgmii_rx_good_frames", RX_GOOD_FRAMES, 26),
SGMII_STAT("sgmii_rx_bad_frames", RX_BAD_FRAMES, 26),
SGMII_STAT("sgmii_rx_false_carrier_events", RX_FALSE_CARRIER, 8),
SGMII_STAT("sgmii_tx_good_frames", TX_GOOD_FRAMES, 26),
SGMII_STAT("sgmii_tx_bad_frames", TX_BAD_FRAMES, 26),
SGMII_STAT("sgmii_tx_false_carrier_events", TX_FALSE_CARRIER, 8),
SGMII_STAT("sgmii_tx_collisions", TX_COLLISIONS, 8),
SGMII_STAT("sgmii_tx_line_collisions", TX_LINE_COLLISIONS, 8),
SGMII_STAT("sgmii_tx_frame_alignment_err", TX_FRAME_ALIGN_ERR, 16),
SGMII_STAT("sgmii_tx_runt_frames", TX_RUNT_FRAMES, 22),
};
#define AQR107_SGMII_STAT_SZ ARRAY_SIZE(aqr107_hw_stats)
static const struct {
int speed;
u16 reg;
} aqr_global_cfg_regs[] = {
{ SPEED_10, VEND1_GLOBAL_CFG_10M, },
{ SPEED_100, VEND1_GLOBAL_CFG_100M, },
{ SPEED_1000, VEND1_GLOBAL_CFG_1G, },
{ SPEED_2500, VEND1_GLOBAL_CFG_2_5G, },
{ SPEED_5000, VEND1_GLOBAL_CFG_5G, },
{ SPEED_10000, VEND1_GLOBAL_CFG_10G, },
};
#define AQR_NUM_GLOBAL_CFG ARRAY_SIZE(aqr_global_cfg_regs)
enum aqr_rate_adaptation {
AQR_RATE_ADAPT_NONE,
AQR_RATE_ADAPT_USX,
AQR_RATE_ADAPT_PAUSE,
};
struct aqr_global_syscfg {
int speed;
phy_interface_t interface;
enum aqr_rate_adaptation rate_adapt;
};
struct aqr107_priv {
u64 sgmii_stats[AQR107_SGMII_STAT_SZ];
u64 fingerprint;
unsigned long leds_active_low;
unsigned long leds_active_high;
bool wait_on_global_cfg;
struct aqr_global_syscfg global_cfg[AQR_NUM_GLOBAL_CFG];
};
#if IS_REACHABLE(CONFIG_HWMON)
int aqr_hwmon_probe(struct phy_device *phydev);
#else
static inline int aqr_hwmon_probe(struct phy_device *phydev) { return 0; }
#endif
int aqr_firmware_load(struct phy_device *phydev);
int aqr_phy_led_blink_set(struct phy_device *phydev, u8 index,
unsigned long *delay_on,
unsigned long *delay_off);
int aqr_phy_led_brightness_set(struct phy_device *phydev,
u8 index, enum led_brightness value);
int aqr_phy_led_hw_is_supported(struct phy_device *phydev, u8 index,
unsigned long rules);
int aqr_phy_led_hw_control_get(struct phy_device *phydev, u8 index,
unsigned long *rules);
int aqr_phy_led_hw_control_set(struct phy_device *phydev, u8 index,
unsigned long rules);
int aqr_phy_led_active_low_set(struct phy_device *phydev, int index, bool enable);
int aqr_phy_led_polarity_set(struct phy_device *phydev, int index,
unsigned long modes);
int aqr_wait_reset_complete(struct phy_device *phydev);
#endif /* AQUANTIA_H */
@@ -0,0 +1,385 @@
// SPDX-License-Identifier: GPL-2.0
#include <linux/bitfield.h>
#include <linux/of.h>
#include <linux/firmware.h>
#include <linux/crc-itu-t.h>
#include <linux/nvmem-consumer.h>
#include <linux/unaligned.h>
#include "aquantia.h"
#define UP_RESET_SLEEP 100
/* addresses of memory segments in the phy */
#define DRAM_BASE_ADDR 0x3FFE0000
#define IRAM_BASE_ADDR 0x40000000
/* firmware image format constants */
#define VERSION_STRING_SIZE 0x40
#define VERSION_STRING_OFFSET 0x0200
/* primary offset is written at an offset from the start of the fw blob */
#define PRIMARY_OFFSET_OFFSET 0x8
/* primary offset needs to be then added to a base offset */
#define PRIMARY_OFFSET_SHIFT 12
#define PRIMARY_OFFSET(x) ((x) << PRIMARY_OFFSET_SHIFT)
#define HEADER_OFFSET 0x300
struct aqr_fw_header {
u32 padding;
u8 iram_offset[3];
u8 iram_size[3];
u8 dram_offset[3];
u8 dram_size[3];
} __packed;
enum aqr_fw_src {
AQR_FW_SRC_NVMEM = 0,
AQR_FW_SRC_FS,
};
static const char * const aqr_fw_src_string[] = {
[AQR_FW_SRC_NVMEM] = "NVMEM",
[AQR_FW_SRC_FS] = "FS",
};
/* AQR firmware doesn't have fixed offsets for iram and dram section
* but instead provide an header with the offset to use on reading
* and parsing the firmware.
*
* AQR firmware can't be trusted and each offset is validated to be
* not negative and be in the size of the firmware itself.
*/
static bool aqr_fw_validate_get(size_t size, size_t offset, size_t get_size)
{
return offset + get_size <= size;
}
static int aqr_fw_get_be16(const u8 *data, size_t offset, size_t size, u16 *value)
{
if (!aqr_fw_validate_get(size, offset, sizeof(u16)))
return -EINVAL;
*value = get_unaligned_be16(data + offset);
return 0;
}
static int aqr_fw_get_le16(const u8 *data, size_t offset, size_t size, u16 *value)
{
if (!aqr_fw_validate_get(size, offset, sizeof(u16)))
return -EINVAL;
*value = get_unaligned_le16(data + offset);
return 0;
}
static int aqr_fw_get_le24(const u8 *data, size_t offset, size_t size, u32 *value)
{
if (!aqr_fw_validate_get(size, offset, sizeof(u8) * 3))
return -EINVAL;
*value = get_unaligned_le24(data + offset);
return 0;
}
/* load data into the phy's memory */
static int aqr_fw_load_memory(struct phy_device *phydev, u32 addr,
const u8 *data, size_t len)
{
u16 crc = 0, up_crc;
size_t pos;
phy_write_mmd(phydev, MDIO_MMD_VEND1,
VEND1_GLOBAL_MAILBOX_INTERFACE1,
VEND1_GLOBAL_MAILBOX_INTERFACE1_CRC_RESET);
phy_write_mmd(phydev, MDIO_MMD_VEND1,
VEND1_GLOBAL_MAILBOX_INTERFACE3,
VEND1_GLOBAL_MAILBOX_INTERFACE3_MSW_ADDR(addr));
phy_write_mmd(phydev, MDIO_MMD_VEND1,
VEND1_GLOBAL_MAILBOX_INTERFACE4,
VEND1_GLOBAL_MAILBOX_INTERFACE4_LSW_ADDR(addr));
/* We assume and enforce the size to be word aligned.
* If a firmware that is not word aligned is found, please report upstream.
*/
for (pos = 0; pos < len; pos += sizeof(u32)) {
u8 crc_data[4];
u32 word;
/* FW data is always stored in little-endian */
word = get_unaligned_le32((const u32 *)(data + pos));
phy_write_mmd(phydev, MDIO_MMD_VEND1, VEND1_GLOBAL_MAILBOX_INTERFACE5,
VEND1_GLOBAL_MAILBOX_INTERFACE5_MSW_DATA(word));
phy_write_mmd(phydev, MDIO_MMD_VEND1, VEND1_GLOBAL_MAILBOX_INTERFACE6,
VEND1_GLOBAL_MAILBOX_INTERFACE6_LSW_DATA(word));
phy_write_mmd(phydev, MDIO_MMD_VEND1, VEND1_GLOBAL_MAILBOX_INTERFACE1,
VEND1_GLOBAL_MAILBOX_INTERFACE1_EXECUTE |
VEND1_GLOBAL_MAILBOX_INTERFACE1_WRITE);
/* Word is swapped internally and MAILBOX CRC is calculated
* using big-endian order. Mimic what the PHY does to have a
* matching CRC...
*/
crc_data[0] = word >> 24;
crc_data[1] = word >> 16;
crc_data[2] = word >> 8;
crc_data[3] = word;
/* ...calculate CRC as we load data... */
crc = crc_itu_t(crc, crc_data, sizeof(crc_data));
}
/* ...gets CRC from MAILBOX after we have loaded the entire section... */
up_crc = phy_read_mmd(phydev, MDIO_MMD_VEND1, VEND1_GLOBAL_MAILBOX_INTERFACE2);
/* ...and make sure it does match our calculated CRC */
if (crc != up_crc) {
phydev_err(phydev, "CRC mismatch: calculated 0x%04x PHY 0x%04x\n",
crc, up_crc);
return -EINVAL;
}
return 0;
}
static int aqr_fw_boot(struct phy_device *phydev, const u8 *data, size_t size,
enum aqr_fw_src fw_src)
{
u16 calculated_crc, read_crc, read_primary_offset;
u32 iram_offset = 0, iram_size = 0;
u32 dram_offset = 0, dram_size = 0;
char version[VERSION_STRING_SIZE];
u32 primary_offset = 0;
int ret;
/* extract saved CRC at the end of the fw
* CRC is saved in big-endian as PHY is BE
*/
ret = aqr_fw_get_be16(data, size - sizeof(u16), size, &read_crc);
if (ret) {
phydev_err(phydev, "bad firmware CRC in firmware\n");
return ret;
}
calculated_crc = crc_itu_t(0, data, size - sizeof(u16));
if (read_crc != calculated_crc) {
phydev_err(phydev, "bad firmware CRC: file 0x%04x calculated 0x%04x\n",
read_crc, calculated_crc);
return -EINVAL;
}
/* Get the primary offset to extract DRAM and IRAM sections. */
ret = aqr_fw_get_le16(data, PRIMARY_OFFSET_OFFSET, size, &read_primary_offset);
if (ret) {
phydev_err(phydev, "bad primary offset in firmware\n");
return ret;
}
primary_offset = PRIMARY_OFFSET(read_primary_offset);
/* Find the DRAM and IRAM sections within the firmware file.
* Make sure the fw_header is correctly in the firmware.
*/
if (!aqr_fw_validate_get(size, primary_offset + HEADER_OFFSET,
sizeof(struct aqr_fw_header))) {
phydev_err(phydev, "bad fw_header in firmware\n");
return -EINVAL;
}
/* offset are in LE and values needs to be converted to cpu endian */
ret = aqr_fw_get_le24(data, primary_offset + HEADER_OFFSET +
offsetof(struct aqr_fw_header, iram_offset),
size, &iram_offset);
if (ret) {
phydev_err(phydev, "bad iram offset in firmware\n");
return ret;
}
ret = aqr_fw_get_le24(data, primary_offset + HEADER_OFFSET +
offsetof(struct aqr_fw_header, iram_size),
size, &iram_size);
if (ret) {
phydev_err(phydev, "invalid iram size in firmware\n");
return ret;
}
ret = aqr_fw_get_le24(data, primary_offset + HEADER_OFFSET +
offsetof(struct aqr_fw_header, dram_offset),
size, &dram_offset);
if (ret) {
phydev_err(phydev, "bad dram offset in firmware\n");
return ret;
}
ret = aqr_fw_get_le24(data, primary_offset + HEADER_OFFSET +
offsetof(struct aqr_fw_header, dram_size),
size, &dram_size);
if (ret) {
phydev_err(phydev, "invalid dram size in firmware\n");
return ret;
}
/* Increment the offset with the primary offset.
* Validate iram/dram offset and size.
*/
iram_offset += primary_offset;
if (iram_size % sizeof(u32)) {
phydev_err(phydev, "iram size if not aligned to word size. Please report this upstream!\n");
return -EINVAL;
}
if (!aqr_fw_validate_get(size, iram_offset, iram_size)) {
phydev_err(phydev, "invalid iram offset for iram size\n");
return -EINVAL;
}
dram_offset += primary_offset;
if (dram_size % sizeof(u32)) {
phydev_err(phydev, "dram size if not aligned to word size. Please report this upstream!\n");
return -EINVAL;
}
if (!aqr_fw_validate_get(size, dram_offset, dram_size)) {
phydev_err(phydev, "invalid iram offset for iram size\n");
return -EINVAL;
}
phydev_dbg(phydev, "primary %d IRAM offset=%d size=%d DRAM offset=%d size=%d\n",
primary_offset, iram_offset, iram_size, dram_offset, dram_size);
if (!aqr_fw_validate_get(size, dram_offset + VERSION_STRING_OFFSET,
VERSION_STRING_SIZE)) {
phydev_err(phydev, "invalid version in firmware\n");
return -EINVAL;
}
strscpy(version, (char *)data + dram_offset + VERSION_STRING_OFFSET,
VERSION_STRING_SIZE);
if (version[0] == '\0') {
phydev_err(phydev, "invalid version in firmware\n");
return -EINVAL;
}
phydev_info(phydev, "loading firmware version '%s' from '%s'\n", version,
aqr_fw_src_string[fw_src]);
/* stall the microcprocessor */
phy_write_mmd(phydev, MDIO_MMD_VEND1, VEND1_GLOBAL_CONTROL2,
VEND1_GLOBAL_CONTROL2_UP_RUN_STALL | VEND1_GLOBAL_CONTROL2_UP_RUN_STALL_OVD);
phydev_dbg(phydev, "loading DRAM 0x%08x from offset=%d size=%d\n",
DRAM_BASE_ADDR, dram_offset, dram_size);
ret = aqr_fw_load_memory(phydev, DRAM_BASE_ADDR, data + dram_offset,
dram_size);
if (ret)
return ret;
phydev_dbg(phydev, "loading IRAM 0x%08x from offset=%d size=%d\n",
IRAM_BASE_ADDR, iram_offset, iram_size);
ret = aqr_fw_load_memory(phydev, IRAM_BASE_ADDR, data + iram_offset,
iram_size);
if (ret)
return ret;
/* make sure soft reset and low power mode are clear */
phy_clear_bits_mmd(phydev, MDIO_MMD_VEND1, VEND1_GLOBAL_SC,
VEND1_GLOBAL_SC_SOFT_RESET | VEND1_GLOBAL_SC_LOW_POWER);
/* Release the microprocessor. UP_RESET must be held for 100 usec. */
phy_write_mmd(phydev, MDIO_MMD_VEND1, VEND1_GLOBAL_CONTROL2,
VEND1_GLOBAL_CONTROL2_UP_RUN_STALL |
VEND1_GLOBAL_CONTROL2_UP_RUN_STALL_OVD |
VEND1_GLOBAL_CONTROL2_UP_RUN_STALL_RST);
usleep_range(UP_RESET_SLEEP, UP_RESET_SLEEP * 2);
phy_write_mmd(phydev, MDIO_MMD_VEND1, VEND1_GLOBAL_CONTROL2,
VEND1_GLOBAL_CONTROL2_UP_RUN_STALL_OVD);
return 0;
}
static int aqr_firmware_load_nvmem(struct phy_device *phydev)
{
struct nvmem_cell *cell;
size_t size;
u8 *buf;
int ret;
cell = nvmem_cell_get(&phydev->mdio.dev, "firmware");
if (IS_ERR(cell))
return PTR_ERR(cell);
buf = nvmem_cell_read(cell, &size);
if (IS_ERR(buf)) {
ret = PTR_ERR(buf);
goto exit;
}
ret = aqr_fw_boot(phydev, buf, size, AQR_FW_SRC_NVMEM);
if (ret)
phydev_err(phydev, "firmware loading failed: %d\n", ret);
kfree(buf);
exit:
nvmem_cell_put(cell);
return ret;
}
static int aqr_firmware_load_fs(struct phy_device *phydev)
{
struct device *dev = &phydev->mdio.dev;
const struct firmware *fw;
const char *fw_name;
int ret;
ret = device_property_read_string(dev, "firmware-name", &fw_name);
if (ret) {
phydev_err(phydev, "failed to read firmware-name: %d\n", ret);
return ret;
}
ret = request_firmware(&fw, fw_name, dev);
if (ret) {
phydev_err(phydev, "failed to find FW file %s (%d)\n",
fw_name, ret);
return ret;
}
ret = aqr_fw_boot(phydev, fw->data, fw->size, AQR_FW_SRC_FS);
if (ret)
phydev_err(phydev, "firmware loading failed: %d\n", ret);
release_firmware(fw);
return ret;
}
int aqr_firmware_load(struct phy_device *phydev)
{
int ret;
/* Check if the firmware is not already loaded by polling
* the current version returned by the PHY.
*/
ret = aqr_wait_reset_complete(phydev);
switch (ret) {
case 0:
/* Some firmware is loaded => do nothing */
return 0;
case -ETIMEDOUT:
/* VEND1_GLOBAL_FW_ID still reads 0 after 2 seconds of polling.
* We don't have full confidence that no firmware is loaded (in
* theory it might just not have loaded yet), but we will
* assume that, and load a new image.
*/
ret = aqr_firmware_load_nvmem(phydev);
if (ret == -EPROBE_DEFER || !ret)
return ret;
ret = aqr_firmware_load_fs(phydev);
if (ret)
return ret;
break;
default:
/* PHY read error, propagate it to the caller */
return ret;
}
return 0;
}
@@ -0,0 +1,216 @@
// SPDX-License-Identifier: GPL-2.0
/* HWMON driver for Aquantia PHY
*
* Author: Nikita Yushchenko <nikita.yoush@cogentembedded.com>
* Author: Andrew Lunn <andrew@lunn.ch>
* Author: Heiner Kallweit <hkallweit1@gmail.com>
*/
#include <linux/phy.h>
#include <linux/device.h>
#include <linux/ctype.h>
#include <linux/hwmon.h>
#include "aquantia.h"
#if IS_REACHABLE(CONFIG_HWMON)
static umode_t aqr_hwmon_is_visible(const void *data,
enum hwmon_sensor_types type,
u32 attr, int channel)
{
if (type != hwmon_temp)
return 0;
switch (attr) {
case hwmon_temp_input:
case hwmon_temp_min_alarm:
case hwmon_temp_max_alarm:
case hwmon_temp_lcrit_alarm:
case hwmon_temp_crit_alarm:
return 0444;
case hwmon_temp_min:
case hwmon_temp_max:
case hwmon_temp_lcrit:
case hwmon_temp_crit:
return 0644;
default:
return 0;
}
}
static int aqr_hwmon_get(struct phy_device *phydev, int reg, long *value)
{
int temp = phy_read_mmd(phydev, MDIO_MMD_VEND1, reg);
if (temp < 0)
return temp;
/* 16 bit value is 2's complement with LSB = 1/256th degree Celsius */
*value = (s16)temp * 1000 / 256;
return 0;
}
static int aqr_hwmon_set(struct phy_device *phydev, int reg, long value)
{
int temp;
if (value >= 128000 || value < -128000)
return -ERANGE;
temp = value * 256 / 1000;
/* temp is in s16 range and we're interested in lower 16 bits only */
return phy_write_mmd(phydev, MDIO_MMD_VEND1, reg, (u16)temp);
}
static int aqr_hwmon_test_bit(struct phy_device *phydev, int reg, int bit)
{
int val = phy_read_mmd(phydev, MDIO_MMD_VEND1, reg);
if (val < 0)
return val;
return !!(val & bit);
}
static int aqr_hwmon_status1(struct phy_device *phydev, int bit, long *value)
{
int val = aqr_hwmon_test_bit(phydev, VEND1_GENERAL_STAT1, bit);
if (val < 0)
return val;
*value = val;
return 0;
}
static int aqr_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
u32 attr, int channel, long *value)
{
struct phy_device *phydev = dev_get_drvdata(dev);
int reg;
if (type != hwmon_temp)
return -EOPNOTSUPP;
switch (attr) {
case hwmon_temp_input:
reg = aqr_hwmon_test_bit(phydev, VEND1_THERMAL_STAT2,
VEND1_THERMAL_STAT2_VALID);
if (reg < 0)
return reg;
if (!reg)
return -EBUSY;
return aqr_hwmon_get(phydev, VEND1_THERMAL_STAT1, value);
case hwmon_temp_lcrit:
return aqr_hwmon_get(phydev, VEND1_THERMAL_PROV_LOW_TEMP_FAIL,
value);
case hwmon_temp_min:
return aqr_hwmon_get(phydev, VEND1_THERMAL_PROV_LOW_TEMP_WARN,
value);
case hwmon_temp_max:
return aqr_hwmon_get(phydev, VEND1_THERMAL_PROV_HIGH_TEMP_WARN,
value);
case hwmon_temp_crit:
return aqr_hwmon_get(phydev, VEND1_THERMAL_PROV_HIGH_TEMP_FAIL,
value);
case hwmon_temp_lcrit_alarm:
return aqr_hwmon_status1(phydev,
VEND1_GENERAL_STAT1_LOW_TEMP_FAIL,
value);
case hwmon_temp_min_alarm:
return aqr_hwmon_status1(phydev,
VEND1_GENERAL_STAT1_LOW_TEMP_WARN,
value);
case hwmon_temp_max_alarm:
return aqr_hwmon_status1(phydev,
VEND1_GENERAL_STAT1_HIGH_TEMP_WARN,
value);
case hwmon_temp_crit_alarm:
return aqr_hwmon_status1(phydev,
VEND1_GENERAL_STAT1_HIGH_TEMP_FAIL,
value);
default:
return -EOPNOTSUPP;
}
}
static int aqr_hwmon_write(struct device *dev, enum hwmon_sensor_types type,
u32 attr, int channel, long value)
{
struct phy_device *phydev = dev_get_drvdata(dev);
if (type != hwmon_temp)
return -EOPNOTSUPP;
switch (attr) {
case hwmon_temp_lcrit:
return aqr_hwmon_set(phydev, VEND1_THERMAL_PROV_LOW_TEMP_FAIL,
value);
case hwmon_temp_min:
return aqr_hwmon_set(phydev, VEND1_THERMAL_PROV_LOW_TEMP_WARN,
value);
case hwmon_temp_max:
return aqr_hwmon_set(phydev, VEND1_THERMAL_PROV_HIGH_TEMP_WARN,
value);
case hwmon_temp_crit:
return aqr_hwmon_set(phydev, VEND1_THERMAL_PROV_HIGH_TEMP_FAIL,
value);
default:
return -EOPNOTSUPP;
}
}
static const struct hwmon_ops aqr_hwmon_ops = {
.is_visible = aqr_hwmon_is_visible,
.read = aqr_hwmon_read,
.write = aqr_hwmon_write,
};
static const struct hwmon_channel_info * const aqr_hwmon_info[] = {
HWMON_CHANNEL_INFO(chip, HWMON_C_REGISTER_TZ),
HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT |
HWMON_T_MAX | HWMON_T_MIN |
HWMON_T_MAX_ALARM | HWMON_T_MIN_ALARM |
HWMON_T_CRIT | HWMON_T_LCRIT |
HWMON_T_CRIT_ALARM | HWMON_T_LCRIT_ALARM),
NULL,
};
static const struct hwmon_chip_info aqr_hwmon_chip_info = {
.ops = &aqr_hwmon_ops,
.info = aqr_hwmon_info,
};
int aqr_hwmon_probe(struct phy_device *phydev)
{
struct device *dev = &phydev->mdio.dev;
struct device *hwmon_dev;
char *hwmon_name;
int i, j;
hwmon_name = devm_kstrdup(dev, dev_name(dev), GFP_KERNEL);
if (!hwmon_name)
return -ENOMEM;
for (i = j = 0; hwmon_name[i]; i++) {
if (isalnum(hwmon_name[i])) {
if (i != j)
hwmon_name[j] = hwmon_name[i];
j++;
}
}
hwmon_name[j] = '\0';
hwmon_dev = devm_hwmon_device_register_with_info(dev, hwmon_name,
phydev, &aqr_hwmon_chip_info, NULL);
return PTR_ERR_OR_ZERO(hwmon_dev);
}
#endif
@@ -0,0 +1,160 @@
// SPDX-License-Identifier: GPL-2.0
/* LED driver for Aquantia PHY
*
* Author: Daniel Golle <daniel@makrotopia.org>
*/
#include <linux/phy.h>
#include "aquantia.h"
int aqr_phy_led_brightness_set(struct phy_device *phydev,
u8 index, enum led_brightness value)
{
if (index >= AQR_MAX_LEDS)
return -EINVAL;
return phy_modify_mmd(phydev, MDIO_MMD_VEND1, AQR_LED_PROV(index),
VEND1_GLOBAL_LED_PROV_LINK_MASK |
VEND1_GLOBAL_LED_PROV_FORCE_ON |
VEND1_GLOBAL_LED_PROV_RX_ACT |
VEND1_GLOBAL_LED_PROV_TX_ACT,
value ? VEND1_GLOBAL_LED_PROV_FORCE_ON : 0);
}
static const unsigned long supported_triggers = (BIT(TRIGGER_NETDEV_LINK) |
BIT(TRIGGER_NETDEV_LINK_100) |
BIT(TRIGGER_NETDEV_LINK_1000) |
BIT(TRIGGER_NETDEV_LINK_2500) |
BIT(TRIGGER_NETDEV_LINK_5000) |
BIT(TRIGGER_NETDEV_LINK_10000) |
BIT(TRIGGER_NETDEV_RX) |
BIT(TRIGGER_NETDEV_TX));
int aqr_phy_led_hw_is_supported(struct phy_device *phydev, u8 index,
unsigned long rules)
{
if (index >= AQR_MAX_LEDS)
return -EINVAL;
/* All combinations of the supported triggers are allowed */
if (rules & ~supported_triggers)
return -EOPNOTSUPP;
return 0;
}
int aqr_phy_led_hw_control_get(struct phy_device *phydev, u8 index,
unsigned long *rules)
{
int val;
if (index >= AQR_MAX_LEDS)
return -EINVAL;
val = phy_read_mmd(phydev, MDIO_MMD_VEND1, AQR_LED_PROV(index));
if (val < 0)
return val;
*rules = 0;
if (val & VEND1_GLOBAL_LED_PROV_LINK100)
*rules |= BIT(TRIGGER_NETDEV_LINK_100);
if (val & VEND1_GLOBAL_LED_PROV_LINK1000)
*rules |= BIT(TRIGGER_NETDEV_LINK_1000);
if (val & VEND1_GLOBAL_LED_PROV_LINK2500)
*rules |= BIT(TRIGGER_NETDEV_LINK_2500);
if (val & VEND1_GLOBAL_LED_PROV_LINK5000)
*rules |= BIT(TRIGGER_NETDEV_LINK_5000);
if (val & VEND1_GLOBAL_LED_PROV_LINK10000)
*rules |= BIT(TRIGGER_NETDEV_LINK_10000);
if (val & VEND1_GLOBAL_LED_PROV_RX_ACT)
*rules |= BIT(TRIGGER_NETDEV_RX);
if (val & VEND1_GLOBAL_LED_PROV_TX_ACT)
*rules |= BIT(TRIGGER_NETDEV_TX);
return 0;
}
int aqr_phy_led_hw_control_set(struct phy_device *phydev, u8 index,
unsigned long rules)
{
u16 val = 0;
if (index >= AQR_MAX_LEDS)
return -EINVAL;
if (rules & (BIT(TRIGGER_NETDEV_LINK_100) | BIT(TRIGGER_NETDEV_LINK)))
val |= VEND1_GLOBAL_LED_PROV_LINK100;
if (rules & (BIT(TRIGGER_NETDEV_LINK_1000) | BIT(TRIGGER_NETDEV_LINK)))
val |= VEND1_GLOBAL_LED_PROV_LINK1000;
if (rules & (BIT(TRIGGER_NETDEV_LINK_2500) | BIT(TRIGGER_NETDEV_LINK)))
val |= VEND1_GLOBAL_LED_PROV_LINK2500;
if (rules & (BIT(TRIGGER_NETDEV_LINK_5000) | BIT(TRIGGER_NETDEV_LINK)))
val |= VEND1_GLOBAL_LED_PROV_LINK5000;
if (rules & (BIT(TRIGGER_NETDEV_LINK_10000) | BIT(TRIGGER_NETDEV_LINK)))
val |= VEND1_GLOBAL_LED_PROV_LINK10000;
if (rules & BIT(TRIGGER_NETDEV_RX))
val |= VEND1_GLOBAL_LED_PROV_RX_ACT;
if (rules & BIT(TRIGGER_NETDEV_TX))
val |= VEND1_GLOBAL_LED_PROV_TX_ACT;
return phy_modify_mmd(phydev, MDIO_MMD_VEND1, AQR_LED_PROV(index),
VEND1_GLOBAL_LED_PROV_LINK_MASK |
VEND1_GLOBAL_LED_PROV_FORCE_ON |
VEND1_GLOBAL_LED_PROV_RX_ACT |
VEND1_GLOBAL_LED_PROV_TX_ACT, val);
}
int aqr_phy_led_active_low_set(struct phy_device *phydev, int index, bool enable)
{
return phy_modify_mmd(phydev, MDIO_MMD_VEND1, AQR_LED_DRIVE(index),
VEND1_GLOBAL_LED_DRIVE_VDD,
enable ? 0 : VEND1_GLOBAL_LED_DRIVE_VDD);
}
int aqr_phy_led_polarity_set(struct phy_device *phydev, int index, unsigned long modes)
{
bool force_active_low = false, force_active_high = false;
struct aqr107_priv *priv = phydev->priv;
u32 mode;
if (index >= AQR_MAX_LEDS)
return -EINVAL;
for_each_set_bit(mode, &modes, __PHY_LED_MODES_NUM) {
switch (mode) {
case PHY_LED_ACTIVE_LOW:
force_active_low = true;
break;
case PHY_LED_ACTIVE_HIGH:
force_active_high = true;
break;
default:
return -EINVAL;
}
}
/* Save LED driver vdd state to restore on SW reset */
if (force_active_low)
priv->leds_active_low |= BIT(index);
if (force_active_high)
priv->leds_active_high |= BIT(index);
if (force_active_high || force_active_low)
return aqr_phy_led_active_low_set(phydev, index, force_active_low);
return -EINVAL;
}
File diff suppressed because it is too large Load Diff