1a2443fd1SAndrew Lunn // SPDX-License-Identifier: GPL-2.0+ 20ca7111aSMatus Ujhelyi /* 30ca7111aSMatus Ujhelyi * drivers/net/phy/at803x.c 40ca7111aSMatus Ujhelyi * 596c36712SMichael Walle * Driver for Qualcomm Atheros AR803x PHY 60ca7111aSMatus Ujhelyi * 70ca7111aSMatus Ujhelyi * Author: Matus Ujhelyi <ujhelyi.m@gmail.com> 80ca7111aSMatus Ujhelyi */ 90ca7111aSMatus Ujhelyi 100ca7111aSMatus Ujhelyi #include <linux/phy.h> 110ca7111aSMatus Ujhelyi #include <linux/module.h> 120ca7111aSMatus Ujhelyi #include <linux/string.h> 130ca7111aSMatus Ujhelyi #include <linux/netdevice.h> 140ca7111aSMatus Ujhelyi #include <linux/etherdevice.h> 156cb75767SMichael Walle #include <linux/ethtool_netlink.h> 1613a56b44SDaniel Mack #include <linux/of_gpio.h> 172f664823SMichael Walle #include <linux/bitfield.h> 1813a56b44SDaniel Mack #include <linux/gpio/consumer.h> 192f664823SMichael Walle #include <linux/regulator/of_regulator.h> 202f664823SMichael Walle #include <linux/regulator/driver.h> 212f664823SMichael Walle #include <linux/regulator/consumer.h> 222f664823SMichael Walle #include <dt-bindings/net/qca-ar803x.h> 230ca7111aSMatus Ujhelyi 247dce80c2SOleksij Rempel #define AT803X_SPECIFIC_FUNCTION_CONTROL 0x10 257dce80c2SOleksij Rempel #define AT803X_SFC_ASSERT_CRS BIT(11) 267dce80c2SOleksij Rempel #define AT803X_SFC_FORCE_LINK BIT(10) 277dce80c2SOleksij Rempel #define AT803X_SFC_MDI_CROSSOVER_MODE_M GENMASK(6, 5) 287dce80c2SOleksij Rempel #define AT803X_SFC_AUTOMATIC_CROSSOVER 0x3 297dce80c2SOleksij Rempel #define AT803X_SFC_MANUAL_MDIX 0x1 307dce80c2SOleksij Rempel #define AT803X_SFC_MANUAL_MDI 0x0 317dce80c2SOleksij Rempel #define AT803X_SFC_SQE_TEST BIT(2) 327dce80c2SOleksij Rempel #define AT803X_SFC_POLARITY_REVERSAL BIT(1) 337dce80c2SOleksij Rempel #define AT803X_SFC_DISABLE_JABBER BIT(0) 347dce80c2SOleksij Rempel 3506d5f344SRussell King #define AT803X_SPECIFIC_STATUS 0x11 369540cddaSLuo Jie #define AT803X_SS_SPEED_MASK GENMASK(15, 14) 379540cddaSLuo Jie #define AT803X_SS_SPEED_1000 2 389540cddaSLuo Jie #define AT803X_SS_SPEED_100 1 399540cddaSLuo Jie #define AT803X_SS_SPEED_10 0 4006d5f344SRussell King #define AT803X_SS_DUPLEX BIT(13) 4106d5f344SRussell King #define AT803X_SS_SPEED_DUPLEX_RESOLVED BIT(11) 4206d5f344SRussell King #define AT803X_SS_MDIX BIT(6) 4306d5f344SRussell King 4479c7bc05SLuo Jie #define QCA808X_SS_SPEED_MASK GENMASK(9, 7) 4579c7bc05SLuo Jie #define QCA808X_SS_SPEED_2500 4 4679c7bc05SLuo Jie 470ca7111aSMatus Ujhelyi #define AT803X_INTR_ENABLE 0x12 48e6e4a556SMartin Blumenstingl #define AT803X_INTR_ENABLE_AUTONEG_ERR BIT(15) 49e6e4a556SMartin Blumenstingl #define AT803X_INTR_ENABLE_SPEED_CHANGED BIT(14) 50e6e4a556SMartin Blumenstingl #define AT803X_INTR_ENABLE_DUPLEX_CHANGED BIT(13) 51e6e4a556SMartin Blumenstingl #define AT803X_INTR_ENABLE_PAGE_RECEIVED BIT(12) 52e6e4a556SMartin Blumenstingl #define AT803X_INTR_ENABLE_LINK_FAIL BIT(11) 53e6e4a556SMartin Blumenstingl #define AT803X_INTR_ENABLE_LINK_SUCCESS BIT(10) 54e6e4a556SMartin Blumenstingl #define AT803X_INTR_ENABLE_WIRESPEED_DOWNGRADE BIT(5) 55e6e4a556SMartin Blumenstingl #define AT803X_INTR_ENABLE_POLARITY_CHANGED BIT(1) 56e6e4a556SMartin Blumenstingl #define AT803X_INTR_ENABLE_WOL BIT(0) 57e6e4a556SMartin Blumenstingl 580ca7111aSMatus Ujhelyi #define AT803X_INTR_STATUS 0x13 59a46bd63bSMartin Blumenstingl 6013a56b44SDaniel Mack #define AT803X_SMART_SPEED 0x14 61cde0f4f8SMichael Walle #define AT803X_SMART_SPEED_ENABLE BIT(5) 62cde0f4f8SMichael Walle #define AT803X_SMART_SPEED_RETRY_LIMIT_MASK GENMASK(4, 2) 63cde0f4f8SMichael Walle #define AT803X_SMART_SPEED_BYPASS_TIMER BIT(1) 646cb75767SMichael Walle #define AT803X_CDT 0x16 656cb75767SMichael Walle #define AT803X_CDT_MDI_PAIR_MASK GENMASK(9, 8) 666cb75767SMichael Walle #define AT803X_CDT_ENABLE_TEST BIT(0) 676cb75767SMichael Walle #define AT803X_CDT_STATUS 0x1c 686cb75767SMichael Walle #define AT803X_CDT_STATUS_STAT_NORMAL 0 696cb75767SMichael Walle #define AT803X_CDT_STATUS_STAT_SHORT 1 706cb75767SMichael Walle #define AT803X_CDT_STATUS_STAT_OPEN 2 716cb75767SMichael Walle #define AT803X_CDT_STATUS_STAT_FAIL 3 726cb75767SMichael Walle #define AT803X_CDT_STATUS_STAT_MASK GENMASK(9, 8) 736cb75767SMichael Walle #define AT803X_CDT_STATUS_DELTA_TIME_MASK GENMASK(7, 0) 7413a56b44SDaniel Mack #define AT803X_LED_CONTROL 0x18 75a46bd63bSMartin Blumenstingl 767beecaf7SLuo Jie #define AT803X_PHY_MMD3_WOL_CTRL 0x8012 777beecaf7SLuo Jie #define AT803X_WOL_EN BIT(5) 780ca7111aSMatus Ujhelyi #define AT803X_LOC_MAC_ADDR_0_15_OFFSET 0x804C 790ca7111aSMatus Ujhelyi #define AT803X_LOC_MAC_ADDR_16_31_OFFSET 0x804B 800ca7111aSMatus Ujhelyi #define AT803X_LOC_MAC_ADDR_32_47_OFFSET 0x804A 81f62265b5SZefir Kurtisi #define AT803X_REG_CHIP_CONFIG 0x1f 82f62265b5SZefir Kurtisi #define AT803X_BT_BX_REG_SEL 0x8000 83a46bd63bSMartin Blumenstingl 841ca6d1b1SMugunthan V N #define AT803X_DEBUG_ADDR 0x1D 851ca6d1b1SMugunthan V N #define AT803X_DEBUG_DATA 0x1E 86a46bd63bSMartin Blumenstingl 87f62265b5SZefir Kurtisi #define AT803X_MODE_CFG_MASK 0x0F 88f62265b5SZefir Kurtisi #define AT803X_MODE_CFG_SGMII 0x01 89f62265b5SZefir Kurtisi 90f62265b5SZefir Kurtisi #define AT803X_PSSR 0x11 /*PHY-Specific Status Register*/ 91f62265b5SZefir Kurtisi #define AT803X_PSSR_MR_AN_COMPLETE 0x0200 92f62265b5SZefir Kurtisi 9367999555SAnsuel Smith #define AT803X_DEBUG_ANALOG_TEST_CTRL 0x00 941ca83119SAnsuel Smith #define QCA8327_DEBUG_MANU_CTRL_EN BIT(2) 951ca83119SAnsuel Smith #define QCA8337_DEBUG_MANU_CTRL_EN GENMASK(3, 2) 962e5f9f28SMartin Blumenstingl #define AT803X_DEBUG_RX_CLK_DLY_EN BIT(15) 97a46bd63bSMartin Blumenstingl 9867999555SAnsuel Smith #define AT803X_DEBUG_SYSTEM_CTRL_MODE 0x05 992e5f9f28SMartin Blumenstingl #define AT803X_DEBUG_TX_CLK_DLY_EN BIT(8) 1000ca7111aSMatus Ujhelyi 101ba3c01eeSAnsuel Smith #define AT803X_DEBUG_REG_HIB_CTRL 0x0b 102ba3c01eeSAnsuel Smith #define AT803X_DEBUG_HIB_CTRL_SEL_RST_80U BIT(10) 103ba3c01eeSAnsuel Smith #define AT803X_DEBUG_HIB_CTRL_EN_ANY_CHANGE BIT(13) 104ba3c01eeSAnsuel Smith 105272833b9SAnsuel Smith #define AT803X_DEBUG_REG_3C 0x3C 106272833b9SAnsuel Smith 10767999555SAnsuel Smith #define AT803X_DEBUG_REG_GREEN 0x3D 108ba3c01eeSAnsuel Smith #define AT803X_DEBUG_GATE_CLK_IN1000 BIT(6) 109272833b9SAnsuel Smith 1102f664823SMichael Walle #define AT803X_DEBUG_REG_1F 0x1F 1112f664823SMichael Walle #define AT803X_DEBUG_PLL_ON BIT(2) 1122f664823SMichael Walle #define AT803X_DEBUG_RGMII_1V8 BIT(3) 1132f664823SMichael Walle 114272833b9SAnsuel Smith #define MDIO_AZ_DEBUG 0x800D 115272833b9SAnsuel Smith 1162f664823SMichael Walle /* AT803x supports either the XTAL input pad, an internal PLL or the 1172f664823SMichael Walle * DSP as clock reference for the clock output pad. The XTAL reference 1182f664823SMichael Walle * is only used for 25 MHz output, all other frequencies need the PLL. 1192f664823SMichael Walle * The DSP as a clock reference is used in synchronous ethernet 1202f664823SMichael Walle * applications. 1212f664823SMichael Walle * 1222f664823SMichael Walle * By default the PLL is only enabled if there is a link. Otherwise 1232f664823SMichael Walle * the PHY will go into low power state and disabled the PLL. You can 1242f664823SMichael Walle * set the PLL_ON bit (see debug register 0x1f) to keep the PLL always 1252f664823SMichael Walle * enabled. 1262f664823SMichael Walle */ 1272f664823SMichael Walle #define AT803X_MMD7_CLK25M 0x8016 1282f664823SMichael Walle #define AT803X_CLK_OUT_MASK GENMASK(4, 2) 1292f664823SMichael Walle #define AT803X_CLK_OUT_25MHZ_XTAL 0 1302f664823SMichael Walle #define AT803X_CLK_OUT_25MHZ_DSP 1 1312f664823SMichael Walle #define AT803X_CLK_OUT_50MHZ_PLL 2 1322f664823SMichael Walle #define AT803X_CLK_OUT_50MHZ_DSP 3 1332f664823SMichael Walle #define AT803X_CLK_OUT_62_5MHZ_PLL 4 1342f664823SMichael Walle #define AT803X_CLK_OUT_62_5MHZ_DSP 5 1352f664823SMichael Walle #define AT803X_CLK_OUT_125MHZ_PLL 6 1362f664823SMichael Walle #define AT803X_CLK_OUT_125MHZ_DSP 7 1372f664823SMichael Walle 138428061f7SMichael Walle /* The AR8035 has another mask which is compatible with the AR8031/AR8033 mask 139428061f7SMichael Walle * but doesn't support choosing between XTAL/PLL and DSP. 1402f664823SMichael Walle */ 1412f664823SMichael Walle #define AT8035_CLK_OUT_MASK GENMASK(4, 3) 1422f664823SMichael Walle 1432f664823SMichael Walle #define AT803X_CLK_OUT_STRENGTH_MASK GENMASK(8, 7) 1442f664823SMichael Walle #define AT803X_CLK_OUT_STRENGTH_FULL 0 1452f664823SMichael Walle #define AT803X_CLK_OUT_STRENGTH_HALF 1 1462f664823SMichael Walle #define AT803X_CLK_OUT_STRENGTH_QUARTER 2 1472f664823SMichael Walle 148cde0f4f8SMichael Walle #define AT803X_DEFAULT_DOWNSHIFT 5 149cde0f4f8SMichael Walle #define AT803X_MIN_DOWNSHIFT 2 150cde0f4f8SMichael Walle #define AT803X_MAX_DOWNSHIFT 9 151cde0f4f8SMichael Walle 152390b4cadSRussell King #define AT803X_MMD3_SMARTEEE_CTL1 0x805b 153390b4cadSRussell King #define AT803X_MMD3_SMARTEEE_CTL2 0x805c 154390b4cadSRussell King #define AT803X_MMD3_SMARTEEE_CTL3 0x805d 155390b4cadSRussell King #define AT803X_MMD3_SMARTEEE_CTL3_LPI_EN BIT(8) 156390b4cadSRussell King 1577908d2ceSOleksij Rempel #define ATH9331_PHY_ID 0x004dd041 158bd8ca17fSDaniel Mack #define ATH8030_PHY_ID 0x004dd076 159bd8ca17fSDaniel Mack #define ATH8031_PHY_ID 0x004dd074 1605800091aSDavid Bauer #define ATH8032_PHY_ID 0x004dd023 161bd8ca17fSDaniel Mack #define ATH8035_PHY_ID 0x004dd072 1620465d8f8SMichael Walle #define AT8030_PHY_ID_MASK 0xffffffef 163bd8ca17fSDaniel Mack 164daf61732SLuo Jie #define QCA8081_PHY_ID 0x004dd101 165daf61732SLuo Jie 166b4df02b5SAnsuel Smith #define QCA8327_A_PHY_ID 0x004dd033 167b4df02b5SAnsuel Smith #define QCA8327_B_PHY_ID 0x004dd034 168272833b9SAnsuel Smith #define QCA8337_PHY_ID 0x004dd036 169fada2ce0SDavid Bauer #define QCA9561_PHY_ID 0x004dd042 170272833b9SAnsuel Smith #define QCA8K_PHY_ID_MASK 0xffffffff 171272833b9SAnsuel Smith 172272833b9SAnsuel Smith #define QCA8K_DEVFLAGS_REVISION_MASK GENMASK(2, 0) 173272833b9SAnsuel Smith 174c329e5afSDavid Bauer #define AT803X_PAGE_FIBER 0 175c329e5afSDavid Bauer #define AT803X_PAGE_COPPER 1 176c329e5afSDavid Bauer 177d0e13fd5SAnsuel Smith /* don't turn off internal PLL */ 178d0e13fd5SAnsuel Smith #define AT803X_KEEP_PLL_ENABLED BIT(0) 179d0e13fd5SAnsuel Smith #define AT803X_DISABLE_SMARTEEE BIT(1) 180d0e13fd5SAnsuel Smith 1812acdd43fSLuo Jie /* ADC threshold */ 1822acdd43fSLuo Jie #define QCA808X_PHY_DEBUG_ADC_THRESHOLD 0x2c80 1832acdd43fSLuo Jie #define QCA808X_ADC_THRESHOLD_MASK GENMASK(7, 0) 1842acdd43fSLuo Jie #define QCA808X_ADC_THRESHOLD_80MV 0 1852acdd43fSLuo Jie #define QCA808X_ADC_THRESHOLD_100MV 0xf0 1862acdd43fSLuo Jie #define QCA808X_ADC_THRESHOLD_200MV 0x0f 1872acdd43fSLuo Jie #define QCA808X_ADC_THRESHOLD_300MV 0xff 1882acdd43fSLuo Jie 1892acdd43fSLuo Jie /* CLD control */ 1902acdd43fSLuo Jie #define QCA808X_PHY_MMD3_ADDR_CLD_CTRL7 0x8007 1912acdd43fSLuo Jie #define QCA808X_8023AZ_AFE_CTRL_MASK GENMASK(8, 4) 1922acdd43fSLuo Jie #define QCA808X_8023AZ_AFE_EN 0x90 1932acdd43fSLuo Jie 1942acdd43fSLuo Jie /* AZ control */ 1952acdd43fSLuo Jie #define QCA808X_PHY_MMD3_AZ_TRAINING_CTRL 0x8008 1962acdd43fSLuo Jie #define QCA808X_MMD3_AZ_TRAINING_VAL 0x1c32 1972acdd43fSLuo Jie 1982acdd43fSLuo Jie #define QCA808X_PHY_MMD1_MSE_THRESHOLD_20DB 0x8014 1992acdd43fSLuo Jie #define QCA808X_MSE_THRESHOLD_20DB_VALUE 0x529 2002acdd43fSLuo Jie 2012acdd43fSLuo Jie #define QCA808X_PHY_MMD1_MSE_THRESHOLD_17DB 0x800E 2022acdd43fSLuo Jie #define QCA808X_MSE_THRESHOLD_17DB_VALUE 0x341 2032acdd43fSLuo Jie 2042acdd43fSLuo Jie #define QCA808X_PHY_MMD1_MSE_THRESHOLD_27DB 0x801E 2052acdd43fSLuo Jie #define QCA808X_MSE_THRESHOLD_27DB_VALUE 0x419 2062acdd43fSLuo Jie 2072acdd43fSLuo Jie #define QCA808X_PHY_MMD1_MSE_THRESHOLD_28DB 0x8020 2082acdd43fSLuo Jie #define QCA808X_MSE_THRESHOLD_28DB_VALUE 0x341 2092acdd43fSLuo Jie 2102acdd43fSLuo Jie #define QCA808X_PHY_MMD7_TOP_OPTION1 0x901c 2112acdd43fSLuo Jie #define QCA808X_TOP_OPTION1_DATA 0x0 2122acdd43fSLuo Jie 2132acdd43fSLuo Jie #define QCA808X_PHY_MMD3_DEBUG_1 0xa100 2142acdd43fSLuo Jie #define QCA808X_MMD3_DEBUG_1_VALUE 0x9203 2152acdd43fSLuo Jie #define QCA808X_PHY_MMD3_DEBUG_2 0xa101 2162acdd43fSLuo Jie #define QCA808X_MMD3_DEBUG_2_VALUE 0x48ad 2172acdd43fSLuo Jie #define QCA808X_PHY_MMD3_DEBUG_3 0xa103 2182acdd43fSLuo Jie #define QCA808X_MMD3_DEBUG_3_VALUE 0x1698 2192acdd43fSLuo Jie #define QCA808X_PHY_MMD3_DEBUG_4 0xa105 2202acdd43fSLuo Jie #define QCA808X_MMD3_DEBUG_4_VALUE 0x8001 2212acdd43fSLuo Jie #define QCA808X_PHY_MMD3_DEBUG_5 0xa106 2222acdd43fSLuo Jie #define QCA808X_MMD3_DEBUG_5_VALUE 0x1111 2232acdd43fSLuo Jie #define QCA808X_PHY_MMD3_DEBUG_6 0xa011 2242acdd43fSLuo Jie #define QCA808X_MMD3_DEBUG_6_VALUE 0x5f85 2252acdd43fSLuo Jie 2269d4dae29SLuo Jie /* master/slave seed config */ 2279d4dae29SLuo Jie #define QCA808X_PHY_DEBUG_LOCAL_SEED 9 2289d4dae29SLuo Jie #define QCA808X_MASTER_SLAVE_SEED_ENABLE BIT(1) 2299d4dae29SLuo Jie #define QCA808X_MASTER_SLAVE_SEED_CFG GENMASK(12, 2) 2309d4dae29SLuo Jie #define QCA808X_MASTER_SLAVE_SEED_RANGE 0x32 2319d4dae29SLuo Jie 232*8c84d752SLuo Jie /* Hibernation yields lower power consumpiton in contrast with normal operation mode. 233*8c84d752SLuo Jie * when the copper cable is unplugged, the PHY enters into hibernation mode in about 10s. 234*8c84d752SLuo Jie */ 235*8c84d752SLuo Jie #define QCA808X_DBG_AN_TEST 0xb 236*8c84d752SLuo Jie #define QCA808X_HIBERNATION_EN BIT(15) 237*8c84d752SLuo Jie 238*8c84d752SLuo Jie #define QCA808X_CDT_ENABLE_TEST BIT(15) 239*8c84d752SLuo Jie #define QCA808X_CDT_INTER_CHECK_DIS BIT(13) 240*8c84d752SLuo Jie #define QCA808X_CDT_LENGTH_UNIT BIT(10) 241*8c84d752SLuo Jie 242*8c84d752SLuo Jie #define QCA808X_MMD3_CDT_STATUS 0x8064 243*8c84d752SLuo Jie #define QCA808X_MMD3_CDT_DIAG_PAIR_A 0x8065 244*8c84d752SLuo Jie #define QCA808X_MMD3_CDT_DIAG_PAIR_B 0x8066 245*8c84d752SLuo Jie #define QCA808X_MMD3_CDT_DIAG_PAIR_C 0x8067 246*8c84d752SLuo Jie #define QCA808X_MMD3_CDT_DIAG_PAIR_D 0x8068 247*8c84d752SLuo Jie #define QCA808X_CDT_DIAG_LENGTH GENMASK(7, 0) 248*8c84d752SLuo Jie 249*8c84d752SLuo Jie #define QCA808X_CDT_CODE_PAIR_A GENMASK(15, 12) 250*8c84d752SLuo Jie #define QCA808X_CDT_CODE_PAIR_B GENMASK(11, 8) 251*8c84d752SLuo Jie #define QCA808X_CDT_CODE_PAIR_C GENMASK(7, 4) 252*8c84d752SLuo Jie #define QCA808X_CDT_CODE_PAIR_D GENMASK(3, 0) 253*8c84d752SLuo Jie #define QCA808X_CDT_STATUS_STAT_FAIL 0 254*8c84d752SLuo Jie #define QCA808X_CDT_STATUS_STAT_NORMAL 1 255*8c84d752SLuo Jie #define QCA808X_CDT_STATUS_STAT_OPEN 2 256*8c84d752SLuo Jie #define QCA808X_CDT_STATUS_STAT_SHORT 3 257*8c84d752SLuo Jie 258daf61732SLuo Jie MODULE_DESCRIPTION("Qualcomm Atheros AR803x and QCA808X PHY driver"); 2590ca7111aSMatus Ujhelyi MODULE_AUTHOR("Matus Ujhelyi"); 2600ca7111aSMatus Ujhelyi MODULE_LICENSE("GPL"); 2610ca7111aSMatus Ujhelyi 262272833b9SAnsuel Smith enum stat_access_type { 263272833b9SAnsuel Smith PHY, 264272833b9SAnsuel Smith MMD 265272833b9SAnsuel Smith }; 266272833b9SAnsuel Smith 267272833b9SAnsuel Smith struct at803x_hw_stat { 268272833b9SAnsuel Smith const char *string; 269272833b9SAnsuel Smith u8 reg; 270272833b9SAnsuel Smith u32 mask; 271272833b9SAnsuel Smith enum stat_access_type access_type; 272272833b9SAnsuel Smith }; 273272833b9SAnsuel Smith 274272833b9SAnsuel Smith static struct at803x_hw_stat at803x_hw_stats[] = { 275272833b9SAnsuel Smith { "phy_idle_errors", 0xa, GENMASK(7, 0), PHY}, 276272833b9SAnsuel Smith { "phy_receive_errors", 0x15, GENMASK(15, 0), PHY}, 277272833b9SAnsuel Smith { "eee_wake_errors", 0x16, GENMASK(15, 0), MMD}, 278272833b9SAnsuel Smith }; 279272833b9SAnsuel Smith 2802f664823SMichael Walle struct at803x_priv { 2812f664823SMichael Walle int flags; 2822f664823SMichael Walle u16 clk_25m_reg; 2832f664823SMichael Walle u16 clk_25m_mask; 284390b4cadSRussell King u8 smarteee_lpi_tw_1g; 285390b4cadSRussell King u8 smarteee_lpi_tw_100m; 2862f664823SMichael Walle struct regulator_dev *vddio_rdev; 2872f664823SMichael Walle struct regulator_dev *vddh_rdev; 2882f664823SMichael Walle struct regulator *vddio; 289272833b9SAnsuel Smith u64 stats[ARRAY_SIZE(at803x_hw_stats)]; 2902f664823SMichael Walle }; 2912f664823SMichael Walle 29213a56b44SDaniel Mack struct at803x_context { 29313a56b44SDaniel Mack u16 bmcr; 29413a56b44SDaniel Mack u16 advertise; 29513a56b44SDaniel Mack u16 control1000; 29613a56b44SDaniel Mack u16 int_enable; 29713a56b44SDaniel Mack u16 smart_speed; 29813a56b44SDaniel Mack u16 led_control; 29913a56b44SDaniel Mack }; 30013a56b44SDaniel Mack 301272833b9SAnsuel Smith static int at803x_debug_reg_write(struct phy_device *phydev, u16 reg, u16 data) 302272833b9SAnsuel Smith { 303272833b9SAnsuel Smith int ret; 304272833b9SAnsuel Smith 305272833b9SAnsuel Smith ret = phy_write(phydev, AT803X_DEBUG_ADDR, reg); 306272833b9SAnsuel Smith if (ret < 0) 307272833b9SAnsuel Smith return ret; 308272833b9SAnsuel Smith 309272833b9SAnsuel Smith return phy_write(phydev, AT803X_DEBUG_DATA, data); 310272833b9SAnsuel Smith } 311272833b9SAnsuel Smith 3122e5f9f28SMartin Blumenstingl static int at803x_debug_reg_read(struct phy_device *phydev, u16 reg) 3132e5f9f28SMartin Blumenstingl { 3142e5f9f28SMartin Blumenstingl int ret; 3152e5f9f28SMartin Blumenstingl 3162e5f9f28SMartin Blumenstingl ret = phy_write(phydev, AT803X_DEBUG_ADDR, reg); 3172e5f9f28SMartin Blumenstingl if (ret < 0) 3182e5f9f28SMartin Blumenstingl return ret; 3192e5f9f28SMartin Blumenstingl 3202e5f9f28SMartin Blumenstingl return phy_read(phydev, AT803X_DEBUG_DATA); 3212e5f9f28SMartin Blumenstingl } 3222e5f9f28SMartin Blumenstingl 3232e5f9f28SMartin Blumenstingl static int at803x_debug_reg_mask(struct phy_device *phydev, u16 reg, 3242e5f9f28SMartin Blumenstingl u16 clear, u16 set) 3252e5f9f28SMartin Blumenstingl { 3262e5f9f28SMartin Blumenstingl u16 val; 3272e5f9f28SMartin Blumenstingl int ret; 3282e5f9f28SMartin Blumenstingl 3292e5f9f28SMartin Blumenstingl ret = at803x_debug_reg_read(phydev, reg); 3302e5f9f28SMartin Blumenstingl if (ret < 0) 3312e5f9f28SMartin Blumenstingl return ret; 3322e5f9f28SMartin Blumenstingl 3332e5f9f28SMartin Blumenstingl val = ret & 0xffff; 3342e5f9f28SMartin Blumenstingl val &= ~clear; 3352e5f9f28SMartin Blumenstingl val |= set; 3362e5f9f28SMartin Blumenstingl 3372e5f9f28SMartin Blumenstingl return phy_write(phydev, AT803X_DEBUG_DATA, val); 3382e5f9f28SMartin Blumenstingl } 3392e5f9f28SMartin Blumenstingl 340c329e5afSDavid Bauer static int at803x_write_page(struct phy_device *phydev, int page) 341c329e5afSDavid Bauer { 342c329e5afSDavid Bauer int mask; 343c329e5afSDavid Bauer int set; 344c329e5afSDavid Bauer 345c329e5afSDavid Bauer if (page == AT803X_PAGE_COPPER) { 346c329e5afSDavid Bauer set = AT803X_BT_BX_REG_SEL; 347c329e5afSDavid Bauer mask = 0; 348c329e5afSDavid Bauer } else { 349c329e5afSDavid Bauer set = 0; 350c329e5afSDavid Bauer mask = AT803X_BT_BX_REG_SEL; 351c329e5afSDavid Bauer } 352c329e5afSDavid Bauer 353c329e5afSDavid Bauer return __phy_modify(phydev, AT803X_REG_CHIP_CONFIG, mask, set); 354c329e5afSDavid Bauer } 355c329e5afSDavid Bauer 356c329e5afSDavid Bauer static int at803x_read_page(struct phy_device *phydev) 357c329e5afSDavid Bauer { 358c329e5afSDavid Bauer int ccr = __phy_read(phydev, AT803X_REG_CHIP_CONFIG); 359c329e5afSDavid Bauer 360c329e5afSDavid Bauer if (ccr < 0) 361c329e5afSDavid Bauer return ccr; 362c329e5afSDavid Bauer 363c329e5afSDavid Bauer if (ccr & AT803X_BT_BX_REG_SEL) 364c329e5afSDavid Bauer return AT803X_PAGE_COPPER; 365c329e5afSDavid Bauer 366c329e5afSDavid Bauer return AT803X_PAGE_FIBER; 367c329e5afSDavid Bauer } 368c329e5afSDavid Bauer 3696d4cd041SVinod Koul static int at803x_enable_rx_delay(struct phy_device *phydev) 3706d4cd041SVinod Koul { 37167999555SAnsuel Smith return at803x_debug_reg_mask(phydev, AT803X_DEBUG_ANALOG_TEST_CTRL, 0, 3726d4cd041SVinod Koul AT803X_DEBUG_RX_CLK_DLY_EN); 3736d4cd041SVinod Koul } 3746d4cd041SVinod Koul 3756d4cd041SVinod Koul static int at803x_enable_tx_delay(struct phy_device *phydev) 3766d4cd041SVinod Koul { 37767999555SAnsuel Smith return at803x_debug_reg_mask(phydev, AT803X_DEBUG_SYSTEM_CTRL_MODE, 0, 3786d4cd041SVinod Koul AT803X_DEBUG_TX_CLK_DLY_EN); 3796d4cd041SVinod Koul } 3806d4cd041SVinod Koul 38143f2ebd5SVinod Koul static int at803x_disable_rx_delay(struct phy_device *phydev) 3822e5f9f28SMartin Blumenstingl { 38367999555SAnsuel Smith return at803x_debug_reg_mask(phydev, AT803X_DEBUG_ANALOG_TEST_CTRL, 384cd28d1d6SVinod Koul AT803X_DEBUG_RX_CLK_DLY_EN, 0); 3852e5f9f28SMartin Blumenstingl } 3862e5f9f28SMartin Blumenstingl 38743f2ebd5SVinod Koul static int at803x_disable_tx_delay(struct phy_device *phydev) 3882e5f9f28SMartin Blumenstingl { 38967999555SAnsuel Smith return at803x_debug_reg_mask(phydev, AT803X_DEBUG_SYSTEM_CTRL_MODE, 390cd28d1d6SVinod Koul AT803X_DEBUG_TX_CLK_DLY_EN, 0); 3912e5f9f28SMartin Blumenstingl } 3922e5f9f28SMartin Blumenstingl 39313a56b44SDaniel Mack /* save relevant PHY registers to private copy */ 39413a56b44SDaniel Mack static void at803x_context_save(struct phy_device *phydev, 39513a56b44SDaniel Mack struct at803x_context *context) 39613a56b44SDaniel Mack { 39713a56b44SDaniel Mack context->bmcr = phy_read(phydev, MII_BMCR); 39813a56b44SDaniel Mack context->advertise = phy_read(phydev, MII_ADVERTISE); 39913a56b44SDaniel Mack context->control1000 = phy_read(phydev, MII_CTRL1000); 40013a56b44SDaniel Mack context->int_enable = phy_read(phydev, AT803X_INTR_ENABLE); 40113a56b44SDaniel Mack context->smart_speed = phy_read(phydev, AT803X_SMART_SPEED); 40213a56b44SDaniel Mack context->led_control = phy_read(phydev, AT803X_LED_CONTROL); 40313a56b44SDaniel Mack } 40413a56b44SDaniel Mack 40513a56b44SDaniel Mack /* restore relevant PHY registers from private copy */ 40613a56b44SDaniel Mack static void at803x_context_restore(struct phy_device *phydev, 40713a56b44SDaniel Mack const struct at803x_context *context) 40813a56b44SDaniel Mack { 40913a56b44SDaniel Mack phy_write(phydev, MII_BMCR, context->bmcr); 41013a56b44SDaniel Mack phy_write(phydev, MII_ADVERTISE, context->advertise); 41113a56b44SDaniel Mack phy_write(phydev, MII_CTRL1000, context->control1000); 41213a56b44SDaniel Mack phy_write(phydev, AT803X_INTR_ENABLE, context->int_enable); 41313a56b44SDaniel Mack phy_write(phydev, AT803X_SMART_SPEED, context->smart_speed); 41413a56b44SDaniel Mack phy_write(phydev, AT803X_LED_CONTROL, context->led_control); 41513a56b44SDaniel Mack } 41613a56b44SDaniel Mack 417ea13c9eeSMugunthan V N static int at803x_set_wol(struct phy_device *phydev, 418ea13c9eeSMugunthan V N struct ethtool_wolinfo *wol) 4190ca7111aSMatus Ujhelyi { 4200ca7111aSMatus Ujhelyi struct net_device *ndev = phydev->attached_dev; 4210ca7111aSMatus Ujhelyi const u8 *mac; 4227beecaf7SLuo Jie int ret, irq_enabled; 423c0f0b563SLuo Jie unsigned int i; 424c0f0b563SLuo Jie const unsigned int offsets[] = { 4250ca7111aSMatus Ujhelyi AT803X_LOC_MAC_ADDR_32_47_OFFSET, 4260ca7111aSMatus Ujhelyi AT803X_LOC_MAC_ADDR_16_31_OFFSET, 4270ca7111aSMatus Ujhelyi AT803X_LOC_MAC_ADDR_0_15_OFFSET, 4280ca7111aSMatus Ujhelyi }; 4290ca7111aSMatus Ujhelyi 4300ca7111aSMatus Ujhelyi if (!ndev) 431ea13c9eeSMugunthan V N return -ENODEV; 4320ca7111aSMatus Ujhelyi 433ea13c9eeSMugunthan V N if (wol->wolopts & WAKE_MAGIC) { 4340ca7111aSMatus Ujhelyi mac = (const u8 *) ndev->dev_addr; 4350ca7111aSMatus Ujhelyi 4360ca7111aSMatus Ujhelyi if (!is_valid_ether_addr(mac)) 437fc755687SDan Murphy return -EINVAL; 4380ca7111aSMatus Ujhelyi 4390e021396SCarlo Caione for (i = 0; i < 3; i++) 440c0f0b563SLuo Jie phy_write_mmd(phydev, MDIO_MMD_PCS, offsets[i], 4410ca7111aSMatus Ujhelyi mac[(i * 2) + 1] | (mac[(i * 2)] << 8)); 442ea13c9eeSMugunthan V N 4437beecaf7SLuo Jie /* Enable WOL function */ 4447beecaf7SLuo Jie ret = phy_modify_mmd(phydev, MDIO_MMD_PCS, AT803X_PHY_MMD3_WOL_CTRL, 4457beecaf7SLuo Jie 0, AT803X_WOL_EN); 4467beecaf7SLuo Jie if (ret) 4477beecaf7SLuo Jie return ret; 4487beecaf7SLuo Jie /* Enable WOL interrupt */ 4492d4284e8SLuo Jie ret = phy_modify(phydev, AT803X_INTR_ENABLE, 0, AT803X_INTR_ENABLE_WOL); 450ea13c9eeSMugunthan V N if (ret) 451ea13c9eeSMugunthan V N return ret; 452ea13c9eeSMugunthan V N } else { 4537beecaf7SLuo Jie /* Disable WoL function */ 4547beecaf7SLuo Jie ret = phy_modify_mmd(phydev, MDIO_MMD_PCS, AT803X_PHY_MMD3_WOL_CTRL, 4557beecaf7SLuo Jie AT803X_WOL_EN, 0); 4567beecaf7SLuo Jie if (ret) 4577beecaf7SLuo Jie return ret; 4587beecaf7SLuo Jie /* Disable WOL interrupt */ 4592d4284e8SLuo Jie ret = phy_modify(phydev, AT803X_INTR_ENABLE, AT803X_INTR_ENABLE_WOL, 0); 460ea13c9eeSMugunthan V N if (ret) 461ea13c9eeSMugunthan V N return ret; 462ea13c9eeSMugunthan V N } 463ea13c9eeSMugunthan V N 4647beecaf7SLuo Jie /* Clear WOL status */ 4657beecaf7SLuo Jie ret = phy_read(phydev, AT803X_INTR_STATUS); 4667beecaf7SLuo Jie if (ret < 0) 467ea13c9eeSMugunthan V N return ret; 4687beecaf7SLuo Jie 4697beecaf7SLuo Jie /* Check if there are other interrupts except for WOL triggered when PHY is 4707beecaf7SLuo Jie * in interrupt mode, only the interrupts enabled by AT803X_INTR_ENABLE can 4717beecaf7SLuo Jie * be passed up to the interrupt PIN. 4727beecaf7SLuo Jie */ 4737beecaf7SLuo Jie irq_enabled = phy_read(phydev, AT803X_INTR_ENABLE); 4747beecaf7SLuo Jie if (irq_enabled < 0) 4757beecaf7SLuo Jie return irq_enabled; 4767beecaf7SLuo Jie 4777beecaf7SLuo Jie irq_enabled &= ~AT803X_INTR_ENABLE_WOL; 4787beecaf7SLuo Jie if (ret & irq_enabled && !phy_polling_mode(phydev)) 4797beecaf7SLuo Jie phy_trigger_machine(phydev); 4807beecaf7SLuo Jie 4817beecaf7SLuo Jie return 0; 482ea13c9eeSMugunthan V N } 483ea13c9eeSMugunthan V N 484ea13c9eeSMugunthan V N static void at803x_get_wol(struct phy_device *phydev, 485ea13c9eeSMugunthan V N struct ethtool_wolinfo *wol) 486ea13c9eeSMugunthan V N { 487ea13c9eeSMugunthan V N u32 value; 488ea13c9eeSMugunthan V N 489ea13c9eeSMugunthan V N wol->supported = WAKE_MAGIC; 490ea13c9eeSMugunthan V N wol->wolopts = 0; 491ea13c9eeSMugunthan V N 4927beecaf7SLuo Jie value = phy_read_mmd(phydev, MDIO_MMD_PCS, AT803X_PHY_MMD3_WOL_CTRL); 4937beecaf7SLuo Jie if (value < 0) 4947beecaf7SLuo Jie return; 4957beecaf7SLuo Jie 4967beecaf7SLuo Jie if (value & AT803X_WOL_EN) 497ea13c9eeSMugunthan V N wol->wolopts |= WAKE_MAGIC; 4980ca7111aSMatus Ujhelyi } 4990ca7111aSMatus Ujhelyi 500272833b9SAnsuel Smith static int at803x_get_sset_count(struct phy_device *phydev) 501272833b9SAnsuel Smith { 502272833b9SAnsuel Smith return ARRAY_SIZE(at803x_hw_stats); 503272833b9SAnsuel Smith } 504272833b9SAnsuel Smith 505272833b9SAnsuel Smith static void at803x_get_strings(struct phy_device *phydev, u8 *data) 506272833b9SAnsuel Smith { 507272833b9SAnsuel Smith int i; 508272833b9SAnsuel Smith 509272833b9SAnsuel Smith for (i = 0; i < ARRAY_SIZE(at803x_hw_stats); i++) { 510272833b9SAnsuel Smith strscpy(data + i * ETH_GSTRING_LEN, 511272833b9SAnsuel Smith at803x_hw_stats[i].string, ETH_GSTRING_LEN); 512272833b9SAnsuel Smith } 513272833b9SAnsuel Smith } 514272833b9SAnsuel Smith 515272833b9SAnsuel Smith static u64 at803x_get_stat(struct phy_device *phydev, int i) 516272833b9SAnsuel Smith { 517272833b9SAnsuel Smith struct at803x_hw_stat stat = at803x_hw_stats[i]; 518272833b9SAnsuel Smith struct at803x_priv *priv = phydev->priv; 519272833b9SAnsuel Smith int val; 520272833b9SAnsuel Smith u64 ret; 521272833b9SAnsuel Smith 522272833b9SAnsuel Smith if (stat.access_type == MMD) 523272833b9SAnsuel Smith val = phy_read_mmd(phydev, MDIO_MMD_PCS, stat.reg); 524272833b9SAnsuel Smith else 525272833b9SAnsuel Smith val = phy_read(phydev, stat.reg); 526272833b9SAnsuel Smith 527272833b9SAnsuel Smith if (val < 0) { 528272833b9SAnsuel Smith ret = U64_MAX; 529272833b9SAnsuel Smith } else { 530272833b9SAnsuel Smith val = val & stat.mask; 531272833b9SAnsuel Smith priv->stats[i] += val; 532272833b9SAnsuel Smith ret = priv->stats[i]; 533272833b9SAnsuel Smith } 534272833b9SAnsuel Smith 535272833b9SAnsuel Smith return ret; 536272833b9SAnsuel Smith } 537272833b9SAnsuel Smith 538272833b9SAnsuel Smith static void at803x_get_stats(struct phy_device *phydev, 539272833b9SAnsuel Smith struct ethtool_stats *stats, u64 *data) 540272833b9SAnsuel Smith { 541272833b9SAnsuel Smith int i; 542272833b9SAnsuel Smith 543272833b9SAnsuel Smith for (i = 0; i < ARRAY_SIZE(at803x_hw_stats); i++) 544272833b9SAnsuel Smith data[i] = at803x_get_stat(phydev, i); 545272833b9SAnsuel Smith } 546272833b9SAnsuel Smith 5476229ed1fSDaniel Mack static int at803x_suspend(struct phy_device *phydev) 5486229ed1fSDaniel Mack { 5496229ed1fSDaniel Mack int value; 5506229ed1fSDaniel Mack int wol_enabled; 5516229ed1fSDaniel Mack 5526229ed1fSDaniel Mack value = phy_read(phydev, AT803X_INTR_ENABLE); 553e6e4a556SMartin Blumenstingl wol_enabled = value & AT803X_INTR_ENABLE_WOL; 5546229ed1fSDaniel Mack 5556229ed1fSDaniel Mack if (wol_enabled) 556fea23fb5SRussell King value = BMCR_ISOLATE; 5576229ed1fSDaniel Mack else 558fea23fb5SRussell King value = BMCR_PDOWN; 5596229ed1fSDaniel Mack 560fea23fb5SRussell King phy_modify(phydev, MII_BMCR, 0, value); 5616229ed1fSDaniel Mack 5626229ed1fSDaniel Mack return 0; 5636229ed1fSDaniel Mack } 5646229ed1fSDaniel Mack 5656229ed1fSDaniel Mack static int at803x_resume(struct phy_device *phydev) 5666229ed1fSDaniel Mack { 567f102852fSRussell King return phy_modify(phydev, MII_BMCR, BMCR_PDOWN | BMCR_ISOLATE, 0); 5686229ed1fSDaniel Mack } 5696229ed1fSDaniel Mack 5702f664823SMichael Walle static int at803x_rgmii_reg_set_voltage_sel(struct regulator_dev *rdev, 5712f664823SMichael Walle unsigned int selector) 5722f664823SMichael Walle { 5732f664823SMichael Walle struct phy_device *phydev = rdev_get_drvdata(rdev); 5742f664823SMichael Walle 5752f664823SMichael Walle if (selector) 5762f664823SMichael Walle return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_1F, 5772f664823SMichael Walle 0, AT803X_DEBUG_RGMII_1V8); 5782f664823SMichael Walle else 5792f664823SMichael Walle return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_1F, 5802f664823SMichael Walle AT803X_DEBUG_RGMII_1V8, 0); 5812f664823SMichael Walle } 5822f664823SMichael Walle 5832f664823SMichael Walle static int at803x_rgmii_reg_get_voltage_sel(struct regulator_dev *rdev) 5842f664823SMichael Walle { 5852f664823SMichael Walle struct phy_device *phydev = rdev_get_drvdata(rdev); 5862f664823SMichael Walle int val; 5872f664823SMichael Walle 5882f664823SMichael Walle val = at803x_debug_reg_read(phydev, AT803X_DEBUG_REG_1F); 5892f664823SMichael Walle if (val < 0) 5902f664823SMichael Walle return val; 5912f664823SMichael Walle 5922f664823SMichael Walle return (val & AT803X_DEBUG_RGMII_1V8) ? 1 : 0; 5932f664823SMichael Walle } 5942f664823SMichael Walle 5953faaf539SRikard Falkeborn static const struct regulator_ops vddio_regulator_ops = { 5962f664823SMichael Walle .list_voltage = regulator_list_voltage_table, 5972f664823SMichael Walle .set_voltage_sel = at803x_rgmii_reg_set_voltage_sel, 5982f664823SMichael Walle .get_voltage_sel = at803x_rgmii_reg_get_voltage_sel, 5992f664823SMichael Walle }; 6002f664823SMichael Walle 6012f664823SMichael Walle static const unsigned int vddio_voltage_table[] = { 6022f664823SMichael Walle 1500000, 6032f664823SMichael Walle 1800000, 6042f664823SMichael Walle }; 6052f664823SMichael Walle 6062f664823SMichael Walle static const struct regulator_desc vddio_desc = { 6072f664823SMichael Walle .name = "vddio", 6082f664823SMichael Walle .of_match = of_match_ptr("vddio-regulator"), 6092f664823SMichael Walle .n_voltages = ARRAY_SIZE(vddio_voltage_table), 6102f664823SMichael Walle .volt_table = vddio_voltage_table, 6112f664823SMichael Walle .ops = &vddio_regulator_ops, 6122f664823SMichael Walle .type = REGULATOR_VOLTAGE, 6132f664823SMichael Walle .owner = THIS_MODULE, 6142f664823SMichael Walle }; 6152f664823SMichael Walle 6163faaf539SRikard Falkeborn static const struct regulator_ops vddh_regulator_ops = { 6172f664823SMichael Walle }; 6182f664823SMichael Walle 6192f664823SMichael Walle static const struct regulator_desc vddh_desc = { 6202f664823SMichael Walle .name = "vddh", 6212f664823SMichael Walle .of_match = of_match_ptr("vddh-regulator"), 6222f664823SMichael Walle .n_voltages = 1, 6232f664823SMichael Walle .fixed_uV = 2500000, 6242f664823SMichael Walle .ops = &vddh_regulator_ops, 6252f664823SMichael Walle .type = REGULATOR_VOLTAGE, 6262f664823SMichael Walle .owner = THIS_MODULE, 6272f664823SMichael Walle }; 6282f664823SMichael Walle 6292f664823SMichael Walle static int at8031_register_regulators(struct phy_device *phydev) 6302f664823SMichael Walle { 6312f664823SMichael Walle struct at803x_priv *priv = phydev->priv; 6322f664823SMichael Walle struct device *dev = &phydev->mdio.dev; 6332f664823SMichael Walle struct regulator_config config = { }; 6342f664823SMichael Walle 6352f664823SMichael Walle config.dev = dev; 6362f664823SMichael Walle config.driver_data = phydev; 6372f664823SMichael Walle 6382f664823SMichael Walle priv->vddio_rdev = devm_regulator_register(dev, &vddio_desc, &config); 6392f664823SMichael Walle if (IS_ERR(priv->vddio_rdev)) { 6402f664823SMichael Walle phydev_err(phydev, "failed to register VDDIO regulator\n"); 6412f664823SMichael Walle return PTR_ERR(priv->vddio_rdev); 6422f664823SMichael Walle } 6432f664823SMichael Walle 6442f664823SMichael Walle priv->vddh_rdev = devm_regulator_register(dev, &vddh_desc, &config); 6452f664823SMichael Walle if (IS_ERR(priv->vddh_rdev)) { 6462f664823SMichael Walle phydev_err(phydev, "failed to register VDDH regulator\n"); 6472f664823SMichael Walle return PTR_ERR(priv->vddh_rdev); 6482f664823SMichael Walle } 6492f664823SMichael Walle 6502f664823SMichael Walle return 0; 6512f664823SMichael Walle } 6522f664823SMichael Walle 6532f664823SMichael Walle static int at803x_parse_dt(struct phy_device *phydev) 6542f664823SMichael Walle { 6552f664823SMichael Walle struct device_node *node = phydev->mdio.dev.of_node; 6562f664823SMichael Walle struct at803x_priv *priv = phydev->priv; 657390b4cadSRussell King u32 freq, strength, tw; 6583f2edd30SAndrew Lunn unsigned int sel; 6592f664823SMichael Walle int ret; 6602f664823SMichael Walle 6612f664823SMichael Walle if (!IS_ENABLED(CONFIG_OF_MDIO)) 6622f664823SMichael Walle return 0; 6632f664823SMichael Walle 664390b4cadSRussell King if (of_property_read_bool(node, "qca,disable-smarteee")) 665390b4cadSRussell King priv->flags |= AT803X_DISABLE_SMARTEEE; 666390b4cadSRussell King 667390b4cadSRussell King if (!of_property_read_u32(node, "qca,smarteee-tw-us-1g", &tw)) { 668390b4cadSRussell King if (!tw || tw > 255) { 669390b4cadSRussell King phydev_err(phydev, "invalid qca,smarteee-tw-us-1g\n"); 670390b4cadSRussell King return -EINVAL; 671390b4cadSRussell King } 672390b4cadSRussell King priv->smarteee_lpi_tw_1g = tw; 673390b4cadSRussell King } 674390b4cadSRussell King 675390b4cadSRussell King if (!of_property_read_u32(node, "qca,smarteee-tw-us-100m", &tw)) { 676390b4cadSRussell King if (!tw || tw > 255) { 677390b4cadSRussell King phydev_err(phydev, "invalid qca,smarteee-tw-us-100m\n"); 678390b4cadSRussell King return -EINVAL; 679390b4cadSRussell King } 680390b4cadSRussell King priv->smarteee_lpi_tw_100m = tw; 681390b4cadSRussell King } 682390b4cadSRussell King 6832f664823SMichael Walle ret = of_property_read_u32(node, "qca,clk-out-frequency", &freq); 6842f664823SMichael Walle if (!ret) { 6852f664823SMichael Walle switch (freq) { 6862f664823SMichael Walle case 25000000: 6872f664823SMichael Walle sel = AT803X_CLK_OUT_25MHZ_XTAL; 6882f664823SMichael Walle break; 6892f664823SMichael Walle case 50000000: 6902f664823SMichael Walle sel = AT803X_CLK_OUT_50MHZ_PLL; 6912f664823SMichael Walle break; 6922f664823SMichael Walle case 62500000: 6932f664823SMichael Walle sel = AT803X_CLK_OUT_62_5MHZ_PLL; 6942f664823SMichael Walle break; 6952f664823SMichael Walle case 125000000: 6962f664823SMichael Walle sel = AT803X_CLK_OUT_125MHZ_PLL; 6972f664823SMichael Walle break; 6982f664823SMichael Walle default: 6992f664823SMichael Walle phydev_err(phydev, "invalid qca,clk-out-frequency\n"); 7002f664823SMichael Walle return -EINVAL; 7012f664823SMichael Walle } 7022f664823SMichael Walle 7033f2edd30SAndrew Lunn priv->clk_25m_reg |= FIELD_PREP(AT803X_CLK_OUT_MASK, sel); 7043f2edd30SAndrew Lunn priv->clk_25m_mask |= AT803X_CLK_OUT_MASK; 7052f664823SMichael Walle 7062f664823SMichael Walle /* Fixup for the AR8030/AR8035. This chip has another mask and 7072f664823SMichael Walle * doesn't support the DSP reference. Eg. the lowest bit of the 7082f664823SMichael Walle * mask. The upper two bits select the same frequencies. Mask 7092f664823SMichael Walle * the lowest bit here. 7102f664823SMichael Walle * 7112f664823SMichael Walle * Warning: 7122f664823SMichael Walle * There was no datasheet for the AR8030 available so this is 7132f664823SMichael Walle * just a guess. But the AR8035 is listed as pin compatible 7142f664823SMichael Walle * to the AR8030 so there might be a good chance it works on 7152f664823SMichael Walle * the AR8030 too. 7162f664823SMichael Walle */ 7178887ca54SRussell King if (phydev->drv->phy_id == ATH8030_PHY_ID || 7188887ca54SRussell King phydev->drv->phy_id == ATH8035_PHY_ID) { 719b1f4c209SOleksij Rempel priv->clk_25m_reg &= AT8035_CLK_OUT_MASK; 720b1f4c209SOleksij Rempel priv->clk_25m_mask &= AT8035_CLK_OUT_MASK; 7212f664823SMichael Walle } 7222f664823SMichael Walle } 7232f664823SMichael Walle 7242f664823SMichael Walle ret = of_property_read_u32(node, "qca,clk-out-strength", &strength); 7252f664823SMichael Walle if (!ret) { 7262f664823SMichael Walle priv->clk_25m_mask |= AT803X_CLK_OUT_STRENGTH_MASK; 7272f664823SMichael Walle switch (strength) { 7282f664823SMichael Walle case AR803X_STRENGTH_FULL: 7292f664823SMichael Walle priv->clk_25m_reg |= AT803X_CLK_OUT_STRENGTH_FULL; 7302f664823SMichael Walle break; 7312f664823SMichael Walle case AR803X_STRENGTH_HALF: 7322f664823SMichael Walle priv->clk_25m_reg |= AT803X_CLK_OUT_STRENGTH_HALF; 7332f664823SMichael Walle break; 7342f664823SMichael Walle case AR803X_STRENGTH_QUARTER: 7352f664823SMichael Walle priv->clk_25m_reg |= AT803X_CLK_OUT_STRENGTH_QUARTER; 7362f664823SMichael Walle break; 7372f664823SMichael Walle default: 7382f664823SMichael Walle phydev_err(phydev, "invalid qca,clk-out-strength\n"); 7392f664823SMichael Walle return -EINVAL; 7402f664823SMichael Walle } 7412f664823SMichael Walle } 7422f664823SMichael Walle 743428061f7SMichael Walle /* Only supported on AR8031/AR8033, the AR8030/AR8035 use strapping 744428061f7SMichael Walle * options. 745428061f7SMichael Walle */ 7468887ca54SRussell King if (phydev->drv->phy_id == ATH8031_PHY_ID) { 7472f664823SMichael Walle if (of_property_read_bool(node, "qca,keep-pll-enabled")) 7482f664823SMichael Walle priv->flags |= AT803X_KEEP_PLL_ENABLED; 7492f664823SMichael Walle 7502f664823SMichael Walle ret = at8031_register_regulators(phydev); 7512f664823SMichael Walle if (ret < 0) 7522f664823SMichael Walle return ret; 7532f664823SMichael Walle 7542f664823SMichael Walle priv->vddio = devm_regulator_get_optional(&phydev->mdio.dev, 7552f664823SMichael Walle "vddio"); 7562f664823SMichael Walle if (IS_ERR(priv->vddio)) { 7572f664823SMichael Walle phydev_err(phydev, "failed to get VDDIO regulator\n"); 7582f664823SMichael Walle return PTR_ERR(priv->vddio); 7592f664823SMichael Walle } 7602f664823SMichael Walle } 7612f664823SMichael Walle 7622f664823SMichael Walle return 0; 7632f664823SMichael Walle } 7642f664823SMichael Walle 7652f664823SMichael Walle static int at803x_probe(struct phy_device *phydev) 7662f664823SMichael Walle { 7672f664823SMichael Walle struct device *dev = &phydev->mdio.dev; 7682f664823SMichael Walle struct at803x_priv *priv; 769c329e5afSDavid Bauer int ret; 7702f664823SMichael Walle 7712f664823SMichael Walle priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 7722f664823SMichael Walle if (!priv) 7732f664823SMichael Walle return -ENOMEM; 7742f664823SMichael Walle 7752f664823SMichael Walle phydev->priv = priv; 7762f664823SMichael Walle 777c329e5afSDavid Bauer ret = at803x_parse_dt(phydev); 778c329e5afSDavid Bauer if (ret) 779c329e5afSDavid Bauer return ret; 780c329e5afSDavid Bauer 7818f7e8762SMichael Walle if (priv->vddio) { 7828f7e8762SMichael Walle ret = regulator_enable(priv->vddio); 7838f7e8762SMichael Walle if (ret < 0) 7848f7e8762SMichael Walle return ret; 7858f7e8762SMichael Walle } 7868f7e8762SMichael Walle 787c329e5afSDavid Bauer /* Some bootloaders leave the fiber page selected. 788c329e5afSDavid Bauer * Switch to the copper page, as otherwise we read 789c329e5afSDavid Bauer * the PHY capabilities from the fiber side. 790c329e5afSDavid Bauer */ 7918887ca54SRussell King if (phydev->drv->phy_id == ATH8031_PHY_ID) { 7928f7e8762SMichael Walle phy_lock_mdio_bus(phydev); 7938f7e8762SMichael Walle ret = at803x_write_page(phydev, AT803X_PAGE_COPPER); 7948f7e8762SMichael Walle phy_unlock_mdio_bus(phydev); 7958f7e8762SMichael Walle if (ret) 7968f7e8762SMichael Walle goto err; 797c329e5afSDavid Bauer } 798c329e5afSDavid Bauer 7998f7e8762SMichael Walle return 0; 8008f7e8762SMichael Walle 8018f7e8762SMichael Walle err: 8028f7e8762SMichael Walle if (priv->vddio) 8038f7e8762SMichael Walle regulator_disable(priv->vddio); 8048f7e8762SMichael Walle 805c329e5afSDavid Bauer return ret; 8062f664823SMichael Walle } 8072f664823SMichael Walle 8082318ca8aSMichael Walle static void at803x_remove(struct phy_device *phydev) 8092318ca8aSMichael Walle { 8102318ca8aSMichael Walle struct at803x_priv *priv = phydev->priv; 8112318ca8aSMichael Walle 8122318ca8aSMichael Walle if (priv->vddio) 8132318ca8aSMichael Walle regulator_disable(priv->vddio); 8142318ca8aSMichael Walle } 8152318ca8aSMichael Walle 816b856150cSDavid Bauer static int at803x_get_features(struct phy_device *phydev) 817b856150cSDavid Bauer { 818b856150cSDavid Bauer int err; 819b856150cSDavid Bauer 820b856150cSDavid Bauer err = genphy_read_abilities(phydev); 821b856150cSDavid Bauer if (err) 822b856150cSDavid Bauer return err; 823b856150cSDavid Bauer 824765c22aaSLuo Jie if (phydev->drv->phy_id == QCA8081_PHY_ID) { 825765c22aaSLuo Jie err = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_PMA_NG_EXTABLE); 826765c22aaSLuo Jie if (err < 0) 827765c22aaSLuo Jie return err; 828765c22aaSLuo Jie 829765c22aaSLuo Jie linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT, phydev->supported, 830765c22aaSLuo Jie err & MDIO_PMA_NG_EXTABLE_2_5GBT); 831765c22aaSLuo Jie } 832765c22aaSLuo Jie 833f5621a01SVladimir Oltean if (phydev->drv->phy_id != ATH8031_PHY_ID) 834b856150cSDavid Bauer return 0; 835b856150cSDavid Bauer 836b856150cSDavid Bauer /* AR8031/AR8033 have different status registers 837b856150cSDavid Bauer * for copper and fiber operation. However, the 838b856150cSDavid Bauer * extended status register is the same for both 839b856150cSDavid Bauer * operation modes. 840b856150cSDavid Bauer * 841b856150cSDavid Bauer * As a result of that, ESTATUS_1000_XFULL is set 842b856150cSDavid Bauer * to 1 even when operating in copper TP mode. 843b856150cSDavid Bauer * 844b856150cSDavid Bauer * Remove this mode from the supported link modes, 845b856150cSDavid Bauer * as this driver currently only supports copper 846b856150cSDavid Bauer * operation. 847b856150cSDavid Bauer */ 848b856150cSDavid Bauer linkmode_clear_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT, 849b856150cSDavid Bauer phydev->supported); 850b856150cSDavid Bauer return 0; 851b856150cSDavid Bauer } 852b856150cSDavid Bauer 853390b4cadSRussell King static int at803x_smarteee_config(struct phy_device *phydev) 854390b4cadSRussell King { 855390b4cadSRussell King struct at803x_priv *priv = phydev->priv; 856390b4cadSRussell King u16 mask = 0, val = 0; 857390b4cadSRussell King int ret; 858390b4cadSRussell King 859390b4cadSRussell King if (priv->flags & AT803X_DISABLE_SMARTEEE) 860390b4cadSRussell King return phy_modify_mmd(phydev, MDIO_MMD_PCS, 861390b4cadSRussell King AT803X_MMD3_SMARTEEE_CTL3, 862390b4cadSRussell King AT803X_MMD3_SMARTEEE_CTL3_LPI_EN, 0); 863390b4cadSRussell King 864390b4cadSRussell King if (priv->smarteee_lpi_tw_1g) { 865390b4cadSRussell King mask |= 0xff00; 866390b4cadSRussell King val |= priv->smarteee_lpi_tw_1g << 8; 867390b4cadSRussell King } 868390b4cadSRussell King if (priv->smarteee_lpi_tw_100m) { 869390b4cadSRussell King mask |= 0x00ff; 870390b4cadSRussell King val |= priv->smarteee_lpi_tw_100m; 871390b4cadSRussell King } 872390b4cadSRussell King if (!mask) 873390b4cadSRussell King return 0; 874390b4cadSRussell King 875390b4cadSRussell King ret = phy_modify_mmd(phydev, MDIO_MMD_PCS, AT803X_MMD3_SMARTEEE_CTL1, 876390b4cadSRussell King mask, val); 877390b4cadSRussell King if (ret) 878390b4cadSRussell King return ret; 879390b4cadSRussell King 880390b4cadSRussell King return phy_modify_mmd(phydev, MDIO_MMD_PCS, AT803X_MMD3_SMARTEEE_CTL3, 881390b4cadSRussell King AT803X_MMD3_SMARTEEE_CTL3_LPI_EN, 882390b4cadSRussell King AT803X_MMD3_SMARTEEE_CTL3_LPI_EN); 883390b4cadSRussell King } 884390b4cadSRussell King 8852f664823SMichael Walle static int at803x_clk_out_config(struct phy_device *phydev) 8862f664823SMichael Walle { 8872f664823SMichael Walle struct at803x_priv *priv = phydev->priv; 8882f664823SMichael Walle 8892f664823SMichael Walle if (!priv->clk_25m_mask) 8902f664823SMichael Walle return 0; 8912f664823SMichael Walle 892a45c1c10SRussell King return phy_modify_mmd(phydev, MDIO_MMD_AN, AT803X_MMD7_CLK25M, 893a45c1c10SRussell King priv->clk_25m_mask, priv->clk_25m_reg); 8942f664823SMichael Walle } 8952f664823SMichael Walle 8962f664823SMichael Walle static int at8031_pll_config(struct phy_device *phydev) 8972f664823SMichael Walle { 8982f664823SMichael Walle struct at803x_priv *priv = phydev->priv; 8992f664823SMichael Walle 9002f664823SMichael Walle /* The default after hardware reset is PLL OFF. After a soft reset, the 9012f664823SMichael Walle * values are retained. 9022f664823SMichael Walle */ 9032f664823SMichael Walle if (priv->flags & AT803X_KEEP_PLL_ENABLED) 9042f664823SMichael Walle return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_1F, 9052f664823SMichael Walle 0, AT803X_DEBUG_PLL_ON); 9062f664823SMichael Walle else 9072f664823SMichael Walle return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_1F, 9082f664823SMichael Walle AT803X_DEBUG_PLL_ON, 0); 9092f664823SMichael Walle } 9102f664823SMichael Walle 9110ca7111aSMatus Ujhelyi static int at803x_config_init(struct phy_device *phydev) 9120ca7111aSMatus Ujhelyi { 9131ca6d1b1SMugunthan V N int ret; 9140ca7111aSMatus Ujhelyi 9156d4cd041SVinod Koul /* The RX and TX delay default is: 9166d4cd041SVinod Koul * after HW reset: RX delay enabled and TX delay disabled 9176d4cd041SVinod Koul * after SW reset: RX delay enabled, while TX delay retains the 9186d4cd041SVinod Koul * value before reset. 9196d4cd041SVinod Koul */ 920bb0ce4c1SAndré Draszik if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID || 921bb0ce4c1SAndré Draszik phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID) 922bb0ce4c1SAndré Draszik ret = at803x_enable_rx_delay(phydev); 923bb0ce4c1SAndré Draszik else 924cd28d1d6SVinod Koul ret = at803x_disable_rx_delay(phydev); 9252e5f9f28SMartin Blumenstingl if (ret < 0) 9261ca6d1b1SMugunthan V N return ret; 9276d4cd041SVinod Koul 9286d4cd041SVinod Koul if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID || 929bb0ce4c1SAndré Draszik phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID) 9306d4cd041SVinod Koul ret = at803x_enable_tx_delay(phydev); 931bb0ce4c1SAndré Draszik else 932bb0ce4c1SAndré Draszik ret = at803x_disable_tx_delay(phydev); 9332f664823SMichael Walle if (ret < 0) 9346d4cd041SVinod Koul return ret; 9352f664823SMichael Walle 936390b4cadSRussell King ret = at803x_smarteee_config(phydev); 937390b4cadSRussell King if (ret < 0) 938390b4cadSRussell King return ret; 939390b4cadSRussell King 9402f664823SMichael Walle ret = at803x_clk_out_config(phydev); 9412f664823SMichael Walle if (ret < 0) 9422f664823SMichael Walle return ret; 9432f664823SMichael Walle 9448887ca54SRussell King if (phydev->drv->phy_id == ATH8031_PHY_ID) { 9452f664823SMichael Walle ret = at8031_pll_config(phydev); 9462f664823SMichael Walle if (ret < 0) 9472f664823SMichael Walle return ret; 9482f664823SMichael Walle } 9492f664823SMichael Walle 9503c51fa5dSRussell King /* Ar803x extended next page bit is enabled by default. Cisco 9513c51fa5dSRussell King * multigig switches read this bit and attempt to negotiate 10Gbps 9523c51fa5dSRussell King * rates even if the next page bit is disabled. This is incorrect 9533c51fa5dSRussell King * behaviour but we still need to accommodate it. XNP is only needed 9543c51fa5dSRussell King * for 10Gbps support, so disable XNP. 9553c51fa5dSRussell King */ 9563c51fa5dSRussell King return phy_modify(phydev, MII_ADVERTISE, MDIO_AN_CTRL1_XNP, 0); 9570ca7111aSMatus Ujhelyi } 9580ca7111aSMatus Ujhelyi 95977a99394SZhao Qiang static int at803x_ack_interrupt(struct phy_device *phydev) 96077a99394SZhao Qiang { 96177a99394SZhao Qiang int err; 96277a99394SZhao Qiang 963a46bd63bSMartin Blumenstingl err = phy_read(phydev, AT803X_INTR_STATUS); 96477a99394SZhao Qiang 96577a99394SZhao Qiang return (err < 0) ? err : 0; 96677a99394SZhao Qiang } 96777a99394SZhao Qiang 96877a99394SZhao Qiang static int at803x_config_intr(struct phy_device *phydev) 96977a99394SZhao Qiang { 97077a99394SZhao Qiang int err; 97177a99394SZhao Qiang int value; 97277a99394SZhao Qiang 973a46bd63bSMartin Blumenstingl value = phy_read(phydev, AT803X_INTR_ENABLE); 97477a99394SZhao Qiang 975e6e4a556SMartin Blumenstingl if (phydev->interrupts == PHY_INTERRUPT_ENABLED) { 976a3417885SIoana Ciornei /* Clear any pending interrupts */ 977a3417885SIoana Ciornei err = at803x_ack_interrupt(phydev); 978a3417885SIoana Ciornei if (err) 979a3417885SIoana Ciornei return err; 980a3417885SIoana Ciornei 981e6e4a556SMartin Blumenstingl value |= AT803X_INTR_ENABLE_AUTONEG_ERR; 982e6e4a556SMartin Blumenstingl value |= AT803X_INTR_ENABLE_SPEED_CHANGED; 983e6e4a556SMartin Blumenstingl value |= AT803X_INTR_ENABLE_DUPLEX_CHANGED; 984e6e4a556SMartin Blumenstingl value |= AT803X_INTR_ENABLE_LINK_FAIL; 985e6e4a556SMartin Blumenstingl value |= AT803X_INTR_ENABLE_LINK_SUCCESS; 986e6e4a556SMartin Blumenstingl 987e6e4a556SMartin Blumenstingl err = phy_write(phydev, AT803X_INTR_ENABLE, value); 988a3417885SIoana Ciornei } else { 989a46bd63bSMartin Blumenstingl err = phy_write(phydev, AT803X_INTR_ENABLE, 0); 990a3417885SIoana Ciornei if (err) 991a3417885SIoana Ciornei return err; 992a3417885SIoana Ciornei 993a3417885SIoana Ciornei /* Clear any pending interrupts */ 994a3417885SIoana Ciornei err = at803x_ack_interrupt(phydev); 995a3417885SIoana Ciornei } 99677a99394SZhao Qiang 99777a99394SZhao Qiang return err; 99877a99394SZhao Qiang } 99977a99394SZhao Qiang 100029773097SIoana Ciornei static irqreturn_t at803x_handle_interrupt(struct phy_device *phydev) 100129773097SIoana Ciornei { 100229773097SIoana Ciornei int irq_status, int_enabled; 100329773097SIoana Ciornei 100429773097SIoana Ciornei irq_status = phy_read(phydev, AT803X_INTR_STATUS); 100529773097SIoana Ciornei if (irq_status < 0) { 100629773097SIoana Ciornei phy_error(phydev); 100729773097SIoana Ciornei return IRQ_NONE; 100829773097SIoana Ciornei } 100929773097SIoana Ciornei 101029773097SIoana Ciornei /* Read the current enabled interrupts */ 101129773097SIoana Ciornei int_enabled = phy_read(phydev, AT803X_INTR_ENABLE); 101229773097SIoana Ciornei if (int_enabled < 0) { 101329773097SIoana Ciornei phy_error(phydev); 101429773097SIoana Ciornei return IRQ_NONE; 101529773097SIoana Ciornei } 101629773097SIoana Ciornei 101729773097SIoana Ciornei /* See if this was one of our enabled interrupts */ 101829773097SIoana Ciornei if (!(irq_status & int_enabled)) 101929773097SIoana Ciornei return IRQ_NONE; 102029773097SIoana Ciornei 102129773097SIoana Ciornei phy_trigger_machine(phydev); 102229773097SIoana Ciornei 102329773097SIoana Ciornei return IRQ_HANDLED; 102429773097SIoana Ciornei } 102529773097SIoana Ciornei 102613a56b44SDaniel Mack static void at803x_link_change_notify(struct phy_device *phydev) 102713a56b44SDaniel Mack { 102813a56b44SDaniel Mack /* 102913a56b44SDaniel Mack * Conduct a hardware reset for AT8030 every time a link loss is 103013a56b44SDaniel Mack * signalled. This is necessary to circumvent a hardware bug that 103113a56b44SDaniel Mack * occurs when the cable is unplugged while TX packets are pending 103213a56b44SDaniel Mack * in the FIFO. In such cases, the FIFO enters an error mode it 103313a56b44SDaniel Mack * cannot recover from by software. 103413a56b44SDaniel Mack */ 10356110ed2dSDavid Bauer if (phydev->state == PHY_NOLINK && phydev->mdio.reset_gpio) { 103613a56b44SDaniel Mack struct at803x_context context; 103713a56b44SDaniel Mack 103813a56b44SDaniel Mack at803x_context_save(phydev, &context); 103913a56b44SDaniel Mack 1040bafbdd52SSergei Shtylyov phy_device_reset(phydev, 1); 104113a56b44SDaniel Mack msleep(1); 1042bafbdd52SSergei Shtylyov phy_device_reset(phydev, 0); 1043d57019d1SSergei Shtylyov msleep(1); 104413a56b44SDaniel Mack 104513a56b44SDaniel Mack at803x_context_restore(phydev, &context); 104613a56b44SDaniel Mack 10475c5f626bSHeiner Kallweit phydev_dbg(phydev, "%s(): phy was reset\n", __func__); 104813a56b44SDaniel Mack } 104913a56b44SDaniel Mack } 105013a56b44SDaniel Mack 105179c7bc05SLuo Jie static int at803x_read_specific_status(struct phy_device *phydev) 105206d5f344SRussell King { 105379c7bc05SLuo Jie int ss; 105406d5f344SRussell King 105506d5f344SRussell King /* Read the AT8035 PHY-Specific Status register, which indicates the 105606d5f344SRussell King * speed and duplex that the PHY is actually using, irrespective of 105706d5f344SRussell King * whether we are in autoneg mode or not. 105806d5f344SRussell King */ 105906d5f344SRussell King ss = phy_read(phydev, AT803X_SPECIFIC_STATUS); 106006d5f344SRussell King if (ss < 0) 106106d5f344SRussell King return ss; 106206d5f344SRussell King 106306d5f344SRussell King if (ss & AT803X_SS_SPEED_DUPLEX_RESOLVED) { 106479c7bc05SLuo Jie int sfc, speed; 10657dce80c2SOleksij Rempel 10667dce80c2SOleksij Rempel sfc = phy_read(phydev, AT803X_SPECIFIC_FUNCTION_CONTROL); 10677dce80c2SOleksij Rempel if (sfc < 0) 10687dce80c2SOleksij Rempel return sfc; 10697dce80c2SOleksij Rempel 107079c7bc05SLuo Jie /* qca8081 takes the different bits for speed value from at803x */ 107179c7bc05SLuo Jie if (phydev->drv->phy_id == QCA8081_PHY_ID) 107279c7bc05SLuo Jie speed = FIELD_GET(QCA808X_SS_SPEED_MASK, ss); 107379c7bc05SLuo Jie else 107479c7bc05SLuo Jie speed = FIELD_GET(AT803X_SS_SPEED_MASK, ss); 107579c7bc05SLuo Jie 107679c7bc05SLuo Jie switch (speed) { 107706d5f344SRussell King case AT803X_SS_SPEED_10: 107806d5f344SRussell King phydev->speed = SPEED_10; 107906d5f344SRussell King break; 108006d5f344SRussell King case AT803X_SS_SPEED_100: 108106d5f344SRussell King phydev->speed = SPEED_100; 108206d5f344SRussell King break; 108306d5f344SRussell King case AT803X_SS_SPEED_1000: 108406d5f344SRussell King phydev->speed = SPEED_1000; 108506d5f344SRussell King break; 108679c7bc05SLuo Jie case QCA808X_SS_SPEED_2500: 108779c7bc05SLuo Jie phydev->speed = SPEED_2500; 108879c7bc05SLuo Jie break; 108906d5f344SRussell King } 109006d5f344SRussell King if (ss & AT803X_SS_DUPLEX) 109106d5f344SRussell King phydev->duplex = DUPLEX_FULL; 109206d5f344SRussell King else 109306d5f344SRussell King phydev->duplex = DUPLEX_HALF; 10947dce80c2SOleksij Rempel 109506d5f344SRussell King if (ss & AT803X_SS_MDIX) 109606d5f344SRussell King phydev->mdix = ETH_TP_MDI_X; 109706d5f344SRussell King else 109806d5f344SRussell King phydev->mdix = ETH_TP_MDI; 10997dce80c2SOleksij Rempel 11007dce80c2SOleksij Rempel switch (FIELD_GET(AT803X_SFC_MDI_CROSSOVER_MODE_M, sfc)) { 11017dce80c2SOleksij Rempel case AT803X_SFC_MANUAL_MDI: 11027dce80c2SOleksij Rempel phydev->mdix_ctrl = ETH_TP_MDI; 11037dce80c2SOleksij Rempel break; 11047dce80c2SOleksij Rempel case AT803X_SFC_MANUAL_MDIX: 11057dce80c2SOleksij Rempel phydev->mdix_ctrl = ETH_TP_MDI_X; 11067dce80c2SOleksij Rempel break; 11077dce80c2SOleksij Rempel case AT803X_SFC_AUTOMATIC_CROSSOVER: 11087dce80c2SOleksij Rempel phydev->mdix_ctrl = ETH_TP_MDI_AUTO; 11097dce80c2SOleksij Rempel break; 11107dce80c2SOleksij Rempel } 111106d5f344SRussell King } 111206d5f344SRussell King 111379c7bc05SLuo Jie return 0; 111479c7bc05SLuo Jie } 111579c7bc05SLuo Jie 111679c7bc05SLuo Jie static int at803x_read_status(struct phy_device *phydev) 111779c7bc05SLuo Jie { 111879c7bc05SLuo Jie int err, old_link = phydev->link; 111979c7bc05SLuo Jie 112079c7bc05SLuo Jie /* Update the link, but return if there was an error */ 112179c7bc05SLuo Jie err = genphy_update_link(phydev); 112279c7bc05SLuo Jie if (err) 112379c7bc05SLuo Jie return err; 112479c7bc05SLuo Jie 112579c7bc05SLuo Jie /* why bother the PHY if nothing can have changed */ 112679c7bc05SLuo Jie if (phydev->autoneg == AUTONEG_ENABLE && old_link && phydev->link) 112779c7bc05SLuo Jie return 0; 112879c7bc05SLuo Jie 112979c7bc05SLuo Jie phydev->speed = SPEED_UNKNOWN; 113079c7bc05SLuo Jie phydev->duplex = DUPLEX_UNKNOWN; 113179c7bc05SLuo Jie phydev->pause = 0; 113279c7bc05SLuo Jie phydev->asym_pause = 0; 113379c7bc05SLuo Jie 113479c7bc05SLuo Jie err = genphy_read_lpa(phydev); 113579c7bc05SLuo Jie if (err < 0) 113679c7bc05SLuo Jie return err; 113779c7bc05SLuo Jie 113879c7bc05SLuo Jie err = at803x_read_specific_status(phydev); 113979c7bc05SLuo Jie if (err < 0) 114079c7bc05SLuo Jie return err; 114179c7bc05SLuo Jie 114206d5f344SRussell King if (phydev->autoneg == AUTONEG_ENABLE && phydev->autoneg_complete) 114306d5f344SRussell King phy_resolve_aneg_pause(phydev); 114406d5f344SRussell King 114506d5f344SRussell King return 0; 114606d5f344SRussell King } 114706d5f344SRussell King 11487dce80c2SOleksij Rempel static int at803x_config_mdix(struct phy_device *phydev, u8 ctrl) 11497dce80c2SOleksij Rempel { 11507dce80c2SOleksij Rempel u16 val; 11517dce80c2SOleksij Rempel 11527dce80c2SOleksij Rempel switch (ctrl) { 11537dce80c2SOleksij Rempel case ETH_TP_MDI: 11547dce80c2SOleksij Rempel val = AT803X_SFC_MANUAL_MDI; 11557dce80c2SOleksij Rempel break; 11567dce80c2SOleksij Rempel case ETH_TP_MDI_X: 11577dce80c2SOleksij Rempel val = AT803X_SFC_MANUAL_MDIX; 11587dce80c2SOleksij Rempel break; 11597dce80c2SOleksij Rempel case ETH_TP_MDI_AUTO: 11607dce80c2SOleksij Rempel val = AT803X_SFC_AUTOMATIC_CROSSOVER; 11617dce80c2SOleksij Rempel break; 11627dce80c2SOleksij Rempel default: 11637dce80c2SOleksij Rempel return 0; 11647dce80c2SOleksij Rempel } 11657dce80c2SOleksij Rempel 11667dce80c2SOleksij Rempel return phy_modify_changed(phydev, AT803X_SPECIFIC_FUNCTION_CONTROL, 11677dce80c2SOleksij Rempel AT803X_SFC_MDI_CROSSOVER_MODE_M, 11687dce80c2SOleksij Rempel FIELD_PREP(AT803X_SFC_MDI_CROSSOVER_MODE_M, val)); 11697dce80c2SOleksij Rempel } 11707dce80c2SOleksij Rempel 11717dce80c2SOleksij Rempel static int at803x_config_aneg(struct phy_device *phydev) 11727dce80c2SOleksij Rempel { 11737dce80c2SOleksij Rempel int ret; 11747dce80c2SOleksij Rempel 11757dce80c2SOleksij Rempel ret = at803x_config_mdix(phydev, phydev->mdix_ctrl); 11767dce80c2SOleksij Rempel if (ret < 0) 11777dce80c2SOleksij Rempel return ret; 11787dce80c2SOleksij Rempel 11797dce80c2SOleksij Rempel /* Changes of the midx bits are disruptive to the normal operation; 11807dce80c2SOleksij Rempel * therefore any changes to these registers must be followed by a 11817dce80c2SOleksij Rempel * software reset to take effect. 11827dce80c2SOleksij Rempel */ 11837dce80c2SOleksij Rempel if (ret == 1) { 11847dce80c2SOleksij Rempel ret = genphy_soft_reset(phydev); 11857dce80c2SOleksij Rempel if (ret < 0) 11867dce80c2SOleksij Rempel return ret; 11877dce80c2SOleksij Rempel } 11887dce80c2SOleksij Rempel 1189f884d449SLuo Jie /* Do not restart auto-negotiation by setting ret to 0 defautly, 1190f884d449SLuo Jie * when calling __genphy_config_aneg later. 1191f884d449SLuo Jie */ 1192f884d449SLuo Jie ret = 0; 1193f884d449SLuo Jie 1194f884d449SLuo Jie if (phydev->drv->phy_id == QCA8081_PHY_ID) { 1195f884d449SLuo Jie int phy_ctrl = 0; 1196f884d449SLuo Jie 1197f884d449SLuo Jie /* The reg MII_BMCR also needs to be configured for force mode, the 1198f884d449SLuo Jie * genphy_config_aneg is also needed. 1199f884d449SLuo Jie */ 1200f884d449SLuo Jie if (phydev->autoneg == AUTONEG_DISABLE) 1201f884d449SLuo Jie genphy_c45_pma_setup_forced(phydev); 1202f884d449SLuo Jie 1203f884d449SLuo Jie if (linkmode_test_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT, phydev->advertising)) 1204f884d449SLuo Jie phy_ctrl = MDIO_AN_10GBT_CTRL_ADV2_5G; 1205f884d449SLuo Jie 1206f884d449SLuo Jie ret = phy_modify_mmd_changed(phydev, MDIO_MMD_AN, MDIO_AN_10GBT_CTRL, 1207f884d449SLuo Jie MDIO_AN_10GBT_CTRL_ADV2_5G, phy_ctrl); 1208f884d449SLuo Jie if (ret < 0) 1209f884d449SLuo Jie return ret; 1210f884d449SLuo Jie } 1211f884d449SLuo Jie 1212f884d449SLuo Jie return __genphy_config_aneg(phydev, ret); 12137dce80c2SOleksij Rempel } 12147dce80c2SOleksij Rempel 1215cde0f4f8SMichael Walle static int at803x_get_downshift(struct phy_device *phydev, u8 *d) 1216cde0f4f8SMichael Walle { 1217cde0f4f8SMichael Walle int val; 1218cde0f4f8SMichael Walle 1219cde0f4f8SMichael Walle val = phy_read(phydev, AT803X_SMART_SPEED); 1220cde0f4f8SMichael Walle if (val < 0) 1221cde0f4f8SMichael Walle return val; 1222cde0f4f8SMichael Walle 1223cde0f4f8SMichael Walle if (val & AT803X_SMART_SPEED_ENABLE) 1224cde0f4f8SMichael Walle *d = FIELD_GET(AT803X_SMART_SPEED_RETRY_LIMIT_MASK, val) + 2; 1225cde0f4f8SMichael Walle else 1226cde0f4f8SMichael Walle *d = DOWNSHIFT_DEV_DISABLE; 1227cde0f4f8SMichael Walle 1228cde0f4f8SMichael Walle return 0; 1229cde0f4f8SMichael Walle } 1230cde0f4f8SMichael Walle 1231cde0f4f8SMichael Walle static int at803x_set_downshift(struct phy_device *phydev, u8 cnt) 1232cde0f4f8SMichael Walle { 1233cde0f4f8SMichael Walle u16 mask, set; 1234cde0f4f8SMichael Walle int ret; 1235cde0f4f8SMichael Walle 1236cde0f4f8SMichael Walle switch (cnt) { 1237cde0f4f8SMichael Walle case DOWNSHIFT_DEV_DEFAULT_COUNT: 1238cde0f4f8SMichael Walle cnt = AT803X_DEFAULT_DOWNSHIFT; 1239cde0f4f8SMichael Walle fallthrough; 1240cde0f4f8SMichael Walle case AT803X_MIN_DOWNSHIFT ... AT803X_MAX_DOWNSHIFT: 1241cde0f4f8SMichael Walle set = AT803X_SMART_SPEED_ENABLE | 1242cde0f4f8SMichael Walle AT803X_SMART_SPEED_BYPASS_TIMER | 1243cde0f4f8SMichael Walle FIELD_PREP(AT803X_SMART_SPEED_RETRY_LIMIT_MASK, cnt - 2); 1244cde0f4f8SMichael Walle mask = AT803X_SMART_SPEED_RETRY_LIMIT_MASK; 1245cde0f4f8SMichael Walle break; 1246cde0f4f8SMichael Walle case DOWNSHIFT_DEV_DISABLE: 1247cde0f4f8SMichael Walle set = 0; 1248cde0f4f8SMichael Walle mask = AT803X_SMART_SPEED_ENABLE | 1249cde0f4f8SMichael Walle AT803X_SMART_SPEED_BYPASS_TIMER; 1250cde0f4f8SMichael Walle break; 1251cde0f4f8SMichael Walle default: 1252cde0f4f8SMichael Walle return -EINVAL; 1253cde0f4f8SMichael Walle } 1254cde0f4f8SMichael Walle 1255cde0f4f8SMichael Walle ret = phy_modify_changed(phydev, AT803X_SMART_SPEED, mask, set); 1256cde0f4f8SMichael Walle 1257cde0f4f8SMichael Walle /* After changing the smart speed settings, we need to perform a 1258cde0f4f8SMichael Walle * software reset, use phy_init_hw() to make sure we set the 1259cde0f4f8SMichael Walle * reapply any values which might got lost during software reset. 1260cde0f4f8SMichael Walle */ 1261cde0f4f8SMichael Walle if (ret == 1) 1262cde0f4f8SMichael Walle ret = phy_init_hw(phydev); 1263cde0f4f8SMichael Walle 1264cde0f4f8SMichael Walle return ret; 1265cde0f4f8SMichael Walle } 1266cde0f4f8SMichael Walle 1267cde0f4f8SMichael Walle static int at803x_get_tunable(struct phy_device *phydev, 1268cde0f4f8SMichael Walle struct ethtool_tunable *tuna, void *data) 1269cde0f4f8SMichael Walle { 1270cde0f4f8SMichael Walle switch (tuna->id) { 1271cde0f4f8SMichael Walle case ETHTOOL_PHY_DOWNSHIFT: 1272cde0f4f8SMichael Walle return at803x_get_downshift(phydev, data); 1273cde0f4f8SMichael Walle default: 1274cde0f4f8SMichael Walle return -EOPNOTSUPP; 1275cde0f4f8SMichael Walle } 1276cde0f4f8SMichael Walle } 1277cde0f4f8SMichael Walle 1278cde0f4f8SMichael Walle static int at803x_set_tunable(struct phy_device *phydev, 1279cde0f4f8SMichael Walle struct ethtool_tunable *tuna, const void *data) 1280cde0f4f8SMichael Walle { 1281cde0f4f8SMichael Walle switch (tuna->id) { 1282cde0f4f8SMichael Walle case ETHTOOL_PHY_DOWNSHIFT: 1283cde0f4f8SMichael Walle return at803x_set_downshift(phydev, *(const u8 *)data); 1284cde0f4f8SMichael Walle default: 1285cde0f4f8SMichael Walle return -EOPNOTSUPP; 1286cde0f4f8SMichael Walle } 1287cde0f4f8SMichael Walle } 1288cde0f4f8SMichael Walle 12896cb75767SMichael Walle static int at803x_cable_test_result_trans(u16 status) 12906cb75767SMichael Walle { 12916cb75767SMichael Walle switch (FIELD_GET(AT803X_CDT_STATUS_STAT_MASK, status)) { 12926cb75767SMichael Walle case AT803X_CDT_STATUS_STAT_NORMAL: 12936cb75767SMichael Walle return ETHTOOL_A_CABLE_RESULT_CODE_OK; 12946cb75767SMichael Walle case AT803X_CDT_STATUS_STAT_SHORT: 12956cb75767SMichael Walle return ETHTOOL_A_CABLE_RESULT_CODE_SAME_SHORT; 12966cb75767SMichael Walle case AT803X_CDT_STATUS_STAT_OPEN: 12976cb75767SMichael Walle return ETHTOOL_A_CABLE_RESULT_CODE_OPEN; 12986cb75767SMichael Walle case AT803X_CDT_STATUS_STAT_FAIL: 12996cb75767SMichael Walle default: 13006cb75767SMichael Walle return ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC; 13016cb75767SMichael Walle } 13026cb75767SMichael Walle } 13036cb75767SMichael Walle 13046cb75767SMichael Walle static bool at803x_cdt_test_failed(u16 status) 13056cb75767SMichael Walle { 13066cb75767SMichael Walle return FIELD_GET(AT803X_CDT_STATUS_STAT_MASK, status) == 13076cb75767SMichael Walle AT803X_CDT_STATUS_STAT_FAIL; 13086cb75767SMichael Walle } 13096cb75767SMichael Walle 13106cb75767SMichael Walle static bool at803x_cdt_fault_length_valid(u16 status) 13116cb75767SMichael Walle { 13126cb75767SMichael Walle switch (FIELD_GET(AT803X_CDT_STATUS_STAT_MASK, status)) { 13136cb75767SMichael Walle case AT803X_CDT_STATUS_STAT_OPEN: 13146cb75767SMichael Walle case AT803X_CDT_STATUS_STAT_SHORT: 13156cb75767SMichael Walle return true; 13166cb75767SMichael Walle } 13176cb75767SMichael Walle return false; 13186cb75767SMichael Walle } 13196cb75767SMichael Walle 13206cb75767SMichael Walle static int at803x_cdt_fault_length(u16 status) 13216cb75767SMichael Walle { 13226cb75767SMichael Walle int dt; 13236cb75767SMichael Walle 13246cb75767SMichael Walle /* According to the datasheet the distance to the fault is 13256cb75767SMichael Walle * DELTA_TIME * 0.824 meters. 13266cb75767SMichael Walle * 13276cb75767SMichael Walle * The author suspect the correct formula is: 13286cb75767SMichael Walle * 13296cb75767SMichael Walle * fault_distance = DELTA_TIME * (c * VF) / 125MHz / 2 13306cb75767SMichael Walle * 13316cb75767SMichael Walle * where c is the speed of light, VF is the velocity factor of 13326cb75767SMichael Walle * the twisted pair cable, 125MHz the counter frequency and 13336cb75767SMichael Walle * we need to divide by 2 because the hardware will measure the 13346cb75767SMichael Walle * round trip time to the fault and back to the PHY. 13356cb75767SMichael Walle * 13366cb75767SMichael Walle * With a VF of 0.69 we get the factor 0.824 mentioned in the 13376cb75767SMichael Walle * datasheet. 13386cb75767SMichael Walle */ 13396cb75767SMichael Walle dt = FIELD_GET(AT803X_CDT_STATUS_DELTA_TIME_MASK, status); 13406cb75767SMichael Walle 13416cb75767SMichael Walle return (dt * 824) / 10; 13426cb75767SMichael Walle } 13436cb75767SMichael Walle 13446cb75767SMichael Walle static int at803x_cdt_start(struct phy_device *phydev, int pair) 13456cb75767SMichael Walle { 13466cb75767SMichael Walle u16 cdt; 13476cb75767SMichael Walle 1348*8c84d752SLuo Jie /* qca8081 takes the different bit 15 to enable CDT test */ 1349*8c84d752SLuo Jie if (phydev->drv->phy_id == QCA8081_PHY_ID) 1350*8c84d752SLuo Jie cdt = QCA808X_CDT_ENABLE_TEST | 1351*8c84d752SLuo Jie QCA808X_CDT_LENGTH_UNIT | 1352*8c84d752SLuo Jie QCA808X_CDT_INTER_CHECK_DIS; 1353*8c84d752SLuo Jie else 13546cb75767SMichael Walle cdt = FIELD_PREP(AT803X_CDT_MDI_PAIR_MASK, pair) | 13556cb75767SMichael Walle AT803X_CDT_ENABLE_TEST; 13566cb75767SMichael Walle 13576cb75767SMichael Walle return phy_write(phydev, AT803X_CDT, cdt); 13586cb75767SMichael Walle } 13596cb75767SMichael Walle 13606cb75767SMichael Walle static int at803x_cdt_wait_for_completion(struct phy_device *phydev) 13616cb75767SMichael Walle { 13626cb75767SMichael Walle int val, ret; 1363*8c84d752SLuo Jie u16 cdt_en; 1364*8c84d752SLuo Jie 1365*8c84d752SLuo Jie if (phydev->drv->phy_id == QCA8081_PHY_ID) 1366*8c84d752SLuo Jie cdt_en = QCA808X_CDT_ENABLE_TEST; 1367*8c84d752SLuo Jie else 1368*8c84d752SLuo Jie cdt_en = AT803X_CDT_ENABLE_TEST; 13696cb75767SMichael Walle 13706cb75767SMichael Walle /* One test run takes about 25ms */ 13716cb75767SMichael Walle ret = phy_read_poll_timeout(phydev, AT803X_CDT, val, 1372*8c84d752SLuo Jie !(val & cdt_en), 13736cb75767SMichael Walle 30000, 100000, true); 13746cb75767SMichael Walle 13756cb75767SMichael Walle return ret < 0 ? ret : 0; 13766cb75767SMichael Walle } 13776cb75767SMichael Walle 13786cb75767SMichael Walle static int at803x_cable_test_one_pair(struct phy_device *phydev, int pair) 13796cb75767SMichael Walle { 13806cb75767SMichael Walle static const int ethtool_pair[] = { 13816cb75767SMichael Walle ETHTOOL_A_CABLE_PAIR_A, 13826cb75767SMichael Walle ETHTOOL_A_CABLE_PAIR_B, 13836cb75767SMichael Walle ETHTOOL_A_CABLE_PAIR_C, 13846cb75767SMichael Walle ETHTOOL_A_CABLE_PAIR_D, 13856cb75767SMichael Walle }; 13866cb75767SMichael Walle int ret, val; 13876cb75767SMichael Walle 13886cb75767SMichael Walle ret = at803x_cdt_start(phydev, pair); 13896cb75767SMichael Walle if (ret) 13906cb75767SMichael Walle return ret; 13916cb75767SMichael Walle 13926cb75767SMichael Walle ret = at803x_cdt_wait_for_completion(phydev); 13936cb75767SMichael Walle if (ret) 13946cb75767SMichael Walle return ret; 13956cb75767SMichael Walle 13966cb75767SMichael Walle val = phy_read(phydev, AT803X_CDT_STATUS); 13976cb75767SMichael Walle if (val < 0) 13986cb75767SMichael Walle return val; 13996cb75767SMichael Walle 14006cb75767SMichael Walle if (at803x_cdt_test_failed(val)) 14016cb75767SMichael Walle return 0; 14026cb75767SMichael Walle 14036cb75767SMichael Walle ethnl_cable_test_result(phydev, ethtool_pair[pair], 14046cb75767SMichael Walle at803x_cable_test_result_trans(val)); 14056cb75767SMichael Walle 14066cb75767SMichael Walle if (at803x_cdt_fault_length_valid(val)) 14076cb75767SMichael Walle ethnl_cable_test_fault_length(phydev, ethtool_pair[pair], 14086cb75767SMichael Walle at803x_cdt_fault_length(val)); 14096cb75767SMichael Walle 14106cb75767SMichael Walle return 1; 14116cb75767SMichael Walle } 14126cb75767SMichael Walle 14136cb75767SMichael Walle static int at803x_cable_test_get_status(struct phy_device *phydev, 14146cb75767SMichael Walle bool *finished) 14156cb75767SMichael Walle { 1416dc0f3ed1SOleksij Rempel unsigned long pair_mask; 14176cb75767SMichael Walle int retries = 20; 14186cb75767SMichael Walle int pair, ret; 14196cb75767SMichael Walle 1420dc0f3ed1SOleksij Rempel if (phydev->phy_id == ATH9331_PHY_ID || 1421fada2ce0SDavid Bauer phydev->phy_id == ATH8032_PHY_ID || 1422fada2ce0SDavid Bauer phydev->phy_id == QCA9561_PHY_ID) 1423dc0f3ed1SOleksij Rempel pair_mask = 0x3; 1424dc0f3ed1SOleksij Rempel else 1425dc0f3ed1SOleksij Rempel pair_mask = 0xf; 1426dc0f3ed1SOleksij Rempel 14276cb75767SMichael Walle *finished = false; 14286cb75767SMichael Walle 14296cb75767SMichael Walle /* According to the datasheet the CDT can be performed when 14306cb75767SMichael Walle * there is no link partner or when the link partner is 14316cb75767SMichael Walle * auto-negotiating. Starting the test will restart the AN 14326cb75767SMichael Walle * automatically. It seems that doing this repeatedly we will 14336cb75767SMichael Walle * get a slot where our link partner won't disturb our 14346cb75767SMichael Walle * measurement. 14356cb75767SMichael Walle */ 14366cb75767SMichael Walle while (pair_mask && retries--) { 14376cb75767SMichael Walle for_each_set_bit(pair, &pair_mask, 4) { 14386cb75767SMichael Walle ret = at803x_cable_test_one_pair(phydev, pair); 14396cb75767SMichael Walle if (ret < 0) 14406cb75767SMichael Walle return ret; 14416cb75767SMichael Walle if (ret) 14426cb75767SMichael Walle clear_bit(pair, &pair_mask); 14436cb75767SMichael Walle } 14446cb75767SMichael Walle if (pair_mask) 14456cb75767SMichael Walle msleep(250); 14466cb75767SMichael Walle } 14476cb75767SMichael Walle 14486cb75767SMichael Walle *finished = true; 14496cb75767SMichael Walle 14506cb75767SMichael Walle return 0; 14516cb75767SMichael Walle } 14526cb75767SMichael Walle 14536cb75767SMichael Walle static int at803x_cable_test_start(struct phy_device *phydev) 14546cb75767SMichael Walle { 14556cb75767SMichael Walle /* Enable auto-negotiation, but advertise no capabilities, no link 14566cb75767SMichael Walle * will be established. A restart of the auto-negotiation is not 14576cb75767SMichael Walle * required, because the cable test will automatically break the link. 14586cb75767SMichael Walle */ 14596cb75767SMichael Walle phy_write(phydev, MII_BMCR, BMCR_ANENABLE); 14606cb75767SMichael Walle phy_write(phydev, MII_ADVERTISE, ADVERTISE_CSMA); 1461dc0f3ed1SOleksij Rempel if (phydev->phy_id != ATH9331_PHY_ID && 1462fada2ce0SDavid Bauer phydev->phy_id != ATH8032_PHY_ID && 1463fada2ce0SDavid Bauer phydev->phy_id != QCA9561_PHY_ID) 14646cb75767SMichael Walle phy_write(phydev, MII_CTRL1000, 0); 14656cb75767SMichael Walle 14666cb75767SMichael Walle /* we do all the (time consuming) work later */ 14676cb75767SMichael Walle return 0; 14686cb75767SMichael Walle } 14696cb75767SMichael Walle 1470272833b9SAnsuel Smith static int qca83xx_config_init(struct phy_device *phydev) 1471272833b9SAnsuel Smith { 1472272833b9SAnsuel Smith u8 switch_revision; 1473272833b9SAnsuel Smith 1474272833b9SAnsuel Smith switch_revision = phydev->dev_flags & QCA8K_DEVFLAGS_REVISION_MASK; 1475272833b9SAnsuel Smith 1476272833b9SAnsuel Smith switch (switch_revision) { 1477272833b9SAnsuel Smith case 1: 1478272833b9SAnsuel Smith /* For 100M waveform */ 147967999555SAnsuel Smith at803x_debug_reg_write(phydev, AT803X_DEBUG_ANALOG_TEST_CTRL, 0x02ea); 1480272833b9SAnsuel Smith /* Turn on Gigabit clock */ 148167999555SAnsuel Smith at803x_debug_reg_write(phydev, AT803X_DEBUG_REG_GREEN, 0x68a0); 1482272833b9SAnsuel Smith break; 1483272833b9SAnsuel Smith 1484272833b9SAnsuel Smith case 2: 1485272833b9SAnsuel Smith phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV, 0x0); 1486272833b9SAnsuel Smith fallthrough; 1487272833b9SAnsuel Smith case 4: 1488272833b9SAnsuel Smith phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_AZ_DEBUG, 0x803f); 148967999555SAnsuel Smith at803x_debug_reg_write(phydev, AT803X_DEBUG_REG_GREEN, 0x6860); 149067999555SAnsuel Smith at803x_debug_reg_write(phydev, AT803X_DEBUG_SYSTEM_CTRL_MODE, 0x2c46); 1491272833b9SAnsuel Smith at803x_debug_reg_write(phydev, AT803X_DEBUG_REG_3C, 0x6000); 1492272833b9SAnsuel Smith break; 1493272833b9SAnsuel Smith } 1494272833b9SAnsuel Smith 14951ca83119SAnsuel Smith /* QCA8327 require DAC amplitude adjustment for 100m set to +6%. 14961ca83119SAnsuel Smith * Disable on init and enable only with 100m speed following 14971ca83119SAnsuel Smith * qca original source code. 14981ca83119SAnsuel Smith */ 14991ca83119SAnsuel Smith if (phydev->drv->phy_id == QCA8327_A_PHY_ID || 15001ca83119SAnsuel Smith phydev->drv->phy_id == QCA8327_B_PHY_ID) 150167999555SAnsuel Smith at803x_debug_reg_mask(phydev, AT803X_DEBUG_ANALOG_TEST_CTRL, 15021ca83119SAnsuel Smith QCA8327_DEBUG_MANU_CTRL_EN, 0); 15031ca83119SAnsuel Smith 15049d1c29b4SAnsuel Smith /* Following original QCA sourcecode set port to prefer master */ 15059d1c29b4SAnsuel Smith phy_set_bits(phydev, MII_CTRL1000, CTL1000_PREFER_MASTER); 15069d1c29b4SAnsuel Smith 1507272833b9SAnsuel Smith return 0; 1508272833b9SAnsuel Smith } 1509272833b9SAnsuel Smith 15101ca83119SAnsuel Smith static void qca83xx_link_change_notify(struct phy_device *phydev) 15111ca83119SAnsuel Smith { 15121ca83119SAnsuel Smith /* QCA8337 doesn't require DAC Amplitude adjustement */ 15131ca83119SAnsuel Smith if (phydev->drv->phy_id == QCA8337_PHY_ID) 15141ca83119SAnsuel Smith return; 15151ca83119SAnsuel Smith 15161ca83119SAnsuel Smith /* Set DAC Amplitude adjustment to +6% for 100m on link running */ 15171ca83119SAnsuel Smith if (phydev->state == PHY_RUNNING) { 15181ca83119SAnsuel Smith if (phydev->speed == SPEED_100) 151967999555SAnsuel Smith at803x_debug_reg_mask(phydev, AT803X_DEBUG_ANALOG_TEST_CTRL, 15201ca83119SAnsuel Smith QCA8327_DEBUG_MANU_CTRL_EN, 15211ca83119SAnsuel Smith QCA8327_DEBUG_MANU_CTRL_EN); 15221ca83119SAnsuel Smith } else { 15231ca83119SAnsuel Smith /* Reset DAC Amplitude adjustment */ 152467999555SAnsuel Smith at803x_debug_reg_mask(phydev, AT803X_DEBUG_ANALOG_TEST_CTRL, 15251ca83119SAnsuel Smith QCA8327_DEBUG_MANU_CTRL_EN, 0); 15261ca83119SAnsuel Smith } 15271ca83119SAnsuel Smith } 15281ca83119SAnsuel Smith 1529ba3c01eeSAnsuel Smith static int qca83xx_resume(struct phy_device *phydev) 1530ba3c01eeSAnsuel Smith { 1531ba3c01eeSAnsuel Smith int ret, val; 1532ba3c01eeSAnsuel Smith 1533ba3c01eeSAnsuel Smith /* Skip reset if not suspended */ 1534ba3c01eeSAnsuel Smith if (!phydev->suspended) 1535ba3c01eeSAnsuel Smith return 0; 1536ba3c01eeSAnsuel Smith 1537ba3c01eeSAnsuel Smith /* Reinit the port, reset values set by suspend */ 1538ba3c01eeSAnsuel Smith qca83xx_config_init(phydev); 1539ba3c01eeSAnsuel Smith 1540ba3c01eeSAnsuel Smith /* Reset the port on port resume */ 1541ba3c01eeSAnsuel Smith phy_set_bits(phydev, MII_BMCR, BMCR_RESET | BMCR_ANENABLE); 1542ba3c01eeSAnsuel Smith 1543ba3c01eeSAnsuel Smith /* On resume from suspend the switch execute a reset and 1544ba3c01eeSAnsuel Smith * restart auto-negotiation. Wait for reset to complete. 1545ba3c01eeSAnsuel Smith */ 1546ba3c01eeSAnsuel Smith ret = phy_read_poll_timeout(phydev, MII_BMCR, val, !(val & BMCR_RESET), 1547ba3c01eeSAnsuel Smith 50000, 600000, true); 1548ba3c01eeSAnsuel Smith if (ret) 1549ba3c01eeSAnsuel Smith return ret; 1550ba3c01eeSAnsuel Smith 1551ba3c01eeSAnsuel Smith msleep(1); 1552ba3c01eeSAnsuel Smith 1553ba3c01eeSAnsuel Smith return 0; 1554ba3c01eeSAnsuel Smith } 1555ba3c01eeSAnsuel Smith 1556ba3c01eeSAnsuel Smith static int qca83xx_suspend(struct phy_device *phydev) 1557ba3c01eeSAnsuel Smith { 1558ba3c01eeSAnsuel Smith u16 mask = 0; 1559ba3c01eeSAnsuel Smith 1560ba3c01eeSAnsuel Smith /* Only QCA8337 support actual suspend. 1561ba3c01eeSAnsuel Smith * QCA8327 cause port unreliability when phy suspend 1562ba3c01eeSAnsuel Smith * is set. 1563ba3c01eeSAnsuel Smith */ 1564ba3c01eeSAnsuel Smith if (phydev->drv->phy_id == QCA8337_PHY_ID) { 1565ba3c01eeSAnsuel Smith genphy_suspend(phydev); 1566ba3c01eeSAnsuel Smith } else { 1567ba3c01eeSAnsuel Smith mask |= ~(BMCR_SPEED1000 | BMCR_FULLDPLX); 1568ba3c01eeSAnsuel Smith phy_modify(phydev, MII_BMCR, mask, 0); 1569ba3c01eeSAnsuel Smith } 1570ba3c01eeSAnsuel Smith 157167999555SAnsuel Smith at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_GREEN, 1572ba3c01eeSAnsuel Smith AT803X_DEBUG_GATE_CLK_IN1000, 0); 1573ba3c01eeSAnsuel Smith 1574ba3c01eeSAnsuel Smith at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_HIB_CTRL, 1575ba3c01eeSAnsuel Smith AT803X_DEBUG_HIB_CTRL_EN_ANY_CHANGE | 1576ba3c01eeSAnsuel Smith AT803X_DEBUG_HIB_CTRL_SEL_RST_80U, 0); 1577ba3c01eeSAnsuel Smith 1578ba3c01eeSAnsuel Smith return 0; 1579ba3c01eeSAnsuel Smith } 1580ba3c01eeSAnsuel Smith 15812acdd43fSLuo Jie static int qca808x_phy_fast_retrain_config(struct phy_device *phydev) 15822acdd43fSLuo Jie { 15832acdd43fSLuo Jie int ret; 15842acdd43fSLuo Jie 15852acdd43fSLuo Jie /* Enable fast retrain */ 15862acdd43fSLuo Jie ret = genphy_c45_fast_retrain(phydev, true); 15872acdd43fSLuo Jie if (ret) 15882acdd43fSLuo Jie return ret; 15892acdd43fSLuo Jie 15902acdd43fSLuo Jie phy_write_mmd(phydev, MDIO_MMD_AN, QCA808X_PHY_MMD7_TOP_OPTION1, 15912acdd43fSLuo Jie QCA808X_TOP_OPTION1_DATA); 15922acdd43fSLuo Jie phy_write_mmd(phydev, MDIO_MMD_PMAPMD, QCA808X_PHY_MMD1_MSE_THRESHOLD_20DB, 15932acdd43fSLuo Jie QCA808X_MSE_THRESHOLD_20DB_VALUE); 15942acdd43fSLuo Jie phy_write_mmd(phydev, MDIO_MMD_PMAPMD, QCA808X_PHY_MMD1_MSE_THRESHOLD_17DB, 15952acdd43fSLuo Jie QCA808X_MSE_THRESHOLD_17DB_VALUE); 15962acdd43fSLuo Jie phy_write_mmd(phydev, MDIO_MMD_PMAPMD, QCA808X_PHY_MMD1_MSE_THRESHOLD_27DB, 15972acdd43fSLuo Jie QCA808X_MSE_THRESHOLD_27DB_VALUE); 15982acdd43fSLuo Jie phy_write_mmd(phydev, MDIO_MMD_PMAPMD, QCA808X_PHY_MMD1_MSE_THRESHOLD_28DB, 15992acdd43fSLuo Jie QCA808X_MSE_THRESHOLD_28DB_VALUE); 16002acdd43fSLuo Jie phy_write_mmd(phydev, MDIO_MMD_PCS, QCA808X_PHY_MMD3_DEBUG_1, 16012acdd43fSLuo Jie QCA808X_MMD3_DEBUG_1_VALUE); 16022acdd43fSLuo Jie phy_write_mmd(phydev, MDIO_MMD_PCS, QCA808X_PHY_MMD3_DEBUG_4, 16032acdd43fSLuo Jie QCA808X_MMD3_DEBUG_4_VALUE); 16042acdd43fSLuo Jie phy_write_mmd(phydev, MDIO_MMD_PCS, QCA808X_PHY_MMD3_DEBUG_5, 16052acdd43fSLuo Jie QCA808X_MMD3_DEBUG_5_VALUE); 16062acdd43fSLuo Jie phy_write_mmd(phydev, MDIO_MMD_PCS, QCA808X_PHY_MMD3_DEBUG_3, 16072acdd43fSLuo Jie QCA808X_MMD3_DEBUG_3_VALUE); 16082acdd43fSLuo Jie phy_write_mmd(phydev, MDIO_MMD_PCS, QCA808X_PHY_MMD3_DEBUG_6, 16092acdd43fSLuo Jie QCA808X_MMD3_DEBUG_6_VALUE); 16102acdd43fSLuo Jie phy_write_mmd(phydev, MDIO_MMD_PCS, QCA808X_PHY_MMD3_DEBUG_2, 16112acdd43fSLuo Jie QCA808X_MMD3_DEBUG_2_VALUE); 16122acdd43fSLuo Jie 16132acdd43fSLuo Jie return 0; 16142acdd43fSLuo Jie } 16152acdd43fSLuo Jie 16169d4dae29SLuo Jie static int qca808x_phy_ms_random_seed_set(struct phy_device *phydev) 16179d4dae29SLuo Jie { 16189d4dae29SLuo Jie u16 seed_value = (prandom_u32() % QCA808X_MASTER_SLAVE_SEED_RANGE); 16199d4dae29SLuo Jie 16209d4dae29SLuo Jie return at803x_debug_reg_mask(phydev, QCA808X_PHY_DEBUG_LOCAL_SEED, 16219d4dae29SLuo Jie QCA808X_MASTER_SLAVE_SEED_CFG, 16229d4dae29SLuo Jie FIELD_PREP(QCA808X_MASTER_SLAVE_SEED_CFG, seed_value)); 16239d4dae29SLuo Jie } 16249d4dae29SLuo Jie 16259d4dae29SLuo Jie static int qca808x_phy_ms_seed_enable(struct phy_device *phydev, bool enable) 16269d4dae29SLuo Jie { 16279d4dae29SLuo Jie u16 seed_enable = 0; 16289d4dae29SLuo Jie 16299d4dae29SLuo Jie if (enable) 16309d4dae29SLuo Jie seed_enable = QCA808X_MASTER_SLAVE_SEED_ENABLE; 16319d4dae29SLuo Jie 16329d4dae29SLuo Jie return at803x_debug_reg_mask(phydev, QCA808X_PHY_DEBUG_LOCAL_SEED, 16339d4dae29SLuo Jie QCA808X_MASTER_SLAVE_SEED_ENABLE, seed_enable); 16349d4dae29SLuo Jie } 16359d4dae29SLuo Jie 16362acdd43fSLuo Jie static int qca808x_config_init(struct phy_device *phydev) 16372acdd43fSLuo Jie { 16382acdd43fSLuo Jie int ret; 16392acdd43fSLuo Jie 16402acdd43fSLuo Jie /* Active adc&vga on 802.3az for the link 1000M and 100M */ 16412acdd43fSLuo Jie ret = phy_modify_mmd(phydev, MDIO_MMD_PCS, QCA808X_PHY_MMD3_ADDR_CLD_CTRL7, 16422acdd43fSLuo Jie QCA808X_8023AZ_AFE_CTRL_MASK, QCA808X_8023AZ_AFE_EN); 16432acdd43fSLuo Jie if (ret) 16442acdd43fSLuo Jie return ret; 16452acdd43fSLuo Jie 16462acdd43fSLuo Jie /* Adjust the threshold on 802.3az for the link 1000M */ 16472acdd43fSLuo Jie ret = phy_write_mmd(phydev, MDIO_MMD_PCS, 16482acdd43fSLuo Jie QCA808X_PHY_MMD3_AZ_TRAINING_CTRL, QCA808X_MMD3_AZ_TRAINING_VAL); 16492acdd43fSLuo Jie if (ret) 16502acdd43fSLuo Jie return ret; 16512acdd43fSLuo Jie 16522acdd43fSLuo Jie /* Config the fast retrain for the link 2500M */ 16532acdd43fSLuo Jie ret = qca808x_phy_fast_retrain_config(phydev); 16542acdd43fSLuo Jie if (ret) 16552acdd43fSLuo Jie return ret; 16562acdd43fSLuo Jie 16579d4dae29SLuo Jie /* Configure lower ramdom seed to make phy linked as slave mode */ 16589d4dae29SLuo Jie ret = qca808x_phy_ms_random_seed_set(phydev); 16599d4dae29SLuo Jie if (ret) 16609d4dae29SLuo Jie return ret; 16619d4dae29SLuo Jie 16629d4dae29SLuo Jie /* Enable seed */ 16639d4dae29SLuo Jie ret = qca808x_phy_ms_seed_enable(phydev, true); 16649d4dae29SLuo Jie if (ret) 16659d4dae29SLuo Jie return ret; 16669d4dae29SLuo Jie 16672acdd43fSLuo Jie /* Configure adc threshold as 100mv for the link 10M */ 16682acdd43fSLuo Jie return at803x_debug_reg_mask(phydev, QCA808X_PHY_DEBUG_ADC_THRESHOLD, 16692acdd43fSLuo Jie QCA808X_ADC_THRESHOLD_MASK, QCA808X_ADC_THRESHOLD_100MV); 16702acdd43fSLuo Jie } 16712acdd43fSLuo Jie 167279c7bc05SLuo Jie static int qca808x_read_status(struct phy_device *phydev) 167379c7bc05SLuo Jie { 167479c7bc05SLuo Jie int ret; 167579c7bc05SLuo Jie 167679c7bc05SLuo Jie ret = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_10GBT_STAT); 167779c7bc05SLuo Jie if (ret < 0) 167879c7bc05SLuo Jie return ret; 167979c7bc05SLuo Jie 168079c7bc05SLuo Jie linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT, phydev->lp_advertising, 168179c7bc05SLuo Jie ret & MDIO_AN_10GBT_STAT_LP2_5G); 168279c7bc05SLuo Jie 168379c7bc05SLuo Jie ret = genphy_read_status(phydev); 168479c7bc05SLuo Jie if (ret) 168579c7bc05SLuo Jie return ret; 168679c7bc05SLuo Jie 168779c7bc05SLuo Jie ret = at803x_read_specific_status(phydev); 168879c7bc05SLuo Jie if (ret < 0) 168979c7bc05SLuo Jie return ret; 169079c7bc05SLuo Jie 169179c7bc05SLuo Jie if (phydev->link && phydev->speed == SPEED_2500) 169279c7bc05SLuo Jie phydev->interface = PHY_INTERFACE_MODE_2500BASEX; 169379c7bc05SLuo Jie else 169479c7bc05SLuo Jie phydev->interface = PHY_INTERFACE_MODE_SMII; 169579c7bc05SLuo Jie 16968bc1c543SLuo Jie /* generate seed as a lower random value to make PHY linked as SLAVE easily, 16978bc1c543SLuo Jie * except for master/slave configuration fault detected. 16988bc1c543SLuo Jie * the reason for not putting this code into the function link_change_notify is 16998bc1c543SLuo Jie * the corner case where the link partner is also the qca8081 PHY and the seed 17008bc1c543SLuo Jie * value is configured as the same value, the link can't be up and no link change 17018bc1c543SLuo Jie * occurs. 17028bc1c543SLuo Jie */ 17038bc1c543SLuo Jie if (!phydev->link) { 17048bc1c543SLuo Jie if (phydev->master_slave_state == MASTER_SLAVE_STATE_ERR) { 17058bc1c543SLuo Jie qca808x_phy_ms_seed_enable(phydev, false); 17068bc1c543SLuo Jie } else { 17078bc1c543SLuo Jie qca808x_phy_ms_random_seed_set(phydev); 17088bc1c543SLuo Jie qca808x_phy_ms_seed_enable(phydev, true); 17098bc1c543SLuo Jie } 17108bc1c543SLuo Jie } 17118bc1c543SLuo Jie 171279c7bc05SLuo Jie return 0; 171379c7bc05SLuo Jie } 171479c7bc05SLuo Jie 17159d4dae29SLuo Jie static int qca808x_soft_reset(struct phy_device *phydev) 17169d4dae29SLuo Jie { 17179d4dae29SLuo Jie int ret; 17189d4dae29SLuo Jie 17199d4dae29SLuo Jie ret = genphy_soft_reset(phydev); 17209d4dae29SLuo Jie if (ret < 0) 17219d4dae29SLuo Jie return ret; 17229d4dae29SLuo Jie 17239d4dae29SLuo Jie return qca808x_phy_ms_seed_enable(phydev, true); 17249d4dae29SLuo Jie } 17259d4dae29SLuo Jie 1726*8c84d752SLuo Jie static bool qca808x_cdt_fault_length_valid(int cdt_code) 1727*8c84d752SLuo Jie { 1728*8c84d752SLuo Jie switch (cdt_code) { 1729*8c84d752SLuo Jie case QCA808X_CDT_STATUS_STAT_SHORT: 1730*8c84d752SLuo Jie case QCA808X_CDT_STATUS_STAT_OPEN: 1731*8c84d752SLuo Jie return true; 1732*8c84d752SLuo Jie default: 1733*8c84d752SLuo Jie return false; 1734*8c84d752SLuo Jie } 1735*8c84d752SLuo Jie } 1736*8c84d752SLuo Jie 1737*8c84d752SLuo Jie static int qca808x_cable_test_result_trans(int cdt_code) 1738*8c84d752SLuo Jie { 1739*8c84d752SLuo Jie switch (cdt_code) { 1740*8c84d752SLuo Jie case QCA808X_CDT_STATUS_STAT_NORMAL: 1741*8c84d752SLuo Jie return ETHTOOL_A_CABLE_RESULT_CODE_OK; 1742*8c84d752SLuo Jie case QCA808X_CDT_STATUS_STAT_SHORT: 1743*8c84d752SLuo Jie return ETHTOOL_A_CABLE_RESULT_CODE_SAME_SHORT; 1744*8c84d752SLuo Jie case QCA808X_CDT_STATUS_STAT_OPEN: 1745*8c84d752SLuo Jie return ETHTOOL_A_CABLE_RESULT_CODE_OPEN; 1746*8c84d752SLuo Jie case QCA808X_CDT_STATUS_STAT_FAIL: 1747*8c84d752SLuo Jie default: 1748*8c84d752SLuo Jie return ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC; 1749*8c84d752SLuo Jie } 1750*8c84d752SLuo Jie } 1751*8c84d752SLuo Jie 1752*8c84d752SLuo Jie static int qca808x_cdt_fault_length(struct phy_device *phydev, int pair) 1753*8c84d752SLuo Jie { 1754*8c84d752SLuo Jie int val; 1755*8c84d752SLuo Jie u32 cdt_length_reg = 0; 1756*8c84d752SLuo Jie 1757*8c84d752SLuo Jie switch (pair) { 1758*8c84d752SLuo Jie case ETHTOOL_A_CABLE_PAIR_A: 1759*8c84d752SLuo Jie cdt_length_reg = QCA808X_MMD3_CDT_DIAG_PAIR_A; 1760*8c84d752SLuo Jie break; 1761*8c84d752SLuo Jie case ETHTOOL_A_CABLE_PAIR_B: 1762*8c84d752SLuo Jie cdt_length_reg = QCA808X_MMD3_CDT_DIAG_PAIR_B; 1763*8c84d752SLuo Jie break; 1764*8c84d752SLuo Jie case ETHTOOL_A_CABLE_PAIR_C: 1765*8c84d752SLuo Jie cdt_length_reg = QCA808X_MMD3_CDT_DIAG_PAIR_C; 1766*8c84d752SLuo Jie break; 1767*8c84d752SLuo Jie case ETHTOOL_A_CABLE_PAIR_D: 1768*8c84d752SLuo Jie cdt_length_reg = QCA808X_MMD3_CDT_DIAG_PAIR_D; 1769*8c84d752SLuo Jie break; 1770*8c84d752SLuo Jie default: 1771*8c84d752SLuo Jie return -EINVAL; 1772*8c84d752SLuo Jie } 1773*8c84d752SLuo Jie 1774*8c84d752SLuo Jie val = phy_read_mmd(phydev, MDIO_MMD_PCS, cdt_length_reg); 1775*8c84d752SLuo Jie if (val < 0) 1776*8c84d752SLuo Jie return val; 1777*8c84d752SLuo Jie 1778*8c84d752SLuo Jie return (FIELD_GET(QCA808X_CDT_DIAG_LENGTH, val) * 824) / 10; 1779*8c84d752SLuo Jie } 1780*8c84d752SLuo Jie 1781*8c84d752SLuo Jie static int qca808x_cable_test_start(struct phy_device *phydev) 1782*8c84d752SLuo Jie { 1783*8c84d752SLuo Jie int ret; 1784*8c84d752SLuo Jie 1785*8c84d752SLuo Jie /* perform CDT with the following configs: 1786*8c84d752SLuo Jie * 1. disable hibernation. 1787*8c84d752SLuo Jie * 2. force PHY working in MDI mode. 1788*8c84d752SLuo Jie * 3. for PHY working in 1000BaseT. 1789*8c84d752SLuo Jie * 4. configure the threshold. 1790*8c84d752SLuo Jie */ 1791*8c84d752SLuo Jie 1792*8c84d752SLuo Jie ret = at803x_debug_reg_mask(phydev, QCA808X_DBG_AN_TEST, QCA808X_HIBERNATION_EN, 0); 1793*8c84d752SLuo Jie if (ret < 0) 1794*8c84d752SLuo Jie return ret; 1795*8c84d752SLuo Jie 1796*8c84d752SLuo Jie ret = at803x_config_mdix(phydev, ETH_TP_MDI); 1797*8c84d752SLuo Jie if (ret < 0) 1798*8c84d752SLuo Jie return ret; 1799*8c84d752SLuo Jie 1800*8c84d752SLuo Jie /* Force 1000base-T needs to configure PMA/PMD and MII_BMCR */ 1801*8c84d752SLuo Jie phydev->duplex = DUPLEX_FULL; 1802*8c84d752SLuo Jie phydev->speed = SPEED_1000; 1803*8c84d752SLuo Jie ret = genphy_c45_pma_setup_forced(phydev); 1804*8c84d752SLuo Jie if (ret < 0) 1805*8c84d752SLuo Jie return ret; 1806*8c84d752SLuo Jie 1807*8c84d752SLuo Jie ret = genphy_setup_forced(phydev); 1808*8c84d752SLuo Jie if (ret < 0) 1809*8c84d752SLuo Jie return ret; 1810*8c84d752SLuo Jie 1811*8c84d752SLuo Jie /* configure the thresholds for open, short, pair ok test */ 1812*8c84d752SLuo Jie phy_write_mmd(phydev, MDIO_MMD_PCS, 0x8074, 0xc040); 1813*8c84d752SLuo Jie phy_write_mmd(phydev, MDIO_MMD_PCS, 0x8076, 0xc040); 1814*8c84d752SLuo Jie phy_write_mmd(phydev, MDIO_MMD_PCS, 0x8077, 0xa060); 1815*8c84d752SLuo Jie phy_write_mmd(phydev, MDIO_MMD_PCS, 0x8078, 0xc050); 1816*8c84d752SLuo Jie phy_write_mmd(phydev, MDIO_MMD_PCS, 0x807a, 0xc060); 1817*8c84d752SLuo Jie phy_write_mmd(phydev, MDIO_MMD_PCS, 0x807e, 0xb060); 1818*8c84d752SLuo Jie 1819*8c84d752SLuo Jie return 0; 1820*8c84d752SLuo Jie } 1821*8c84d752SLuo Jie 1822*8c84d752SLuo Jie static int qca808x_cable_test_get_status(struct phy_device *phydev, bool *finished) 1823*8c84d752SLuo Jie { 1824*8c84d752SLuo Jie int ret, val; 1825*8c84d752SLuo Jie int pair_a, pair_b, pair_c, pair_d; 1826*8c84d752SLuo Jie 1827*8c84d752SLuo Jie *finished = false; 1828*8c84d752SLuo Jie 1829*8c84d752SLuo Jie ret = at803x_cdt_start(phydev, 0); 1830*8c84d752SLuo Jie if (ret) 1831*8c84d752SLuo Jie return ret; 1832*8c84d752SLuo Jie 1833*8c84d752SLuo Jie ret = at803x_cdt_wait_for_completion(phydev); 1834*8c84d752SLuo Jie if (ret) 1835*8c84d752SLuo Jie return ret; 1836*8c84d752SLuo Jie 1837*8c84d752SLuo Jie val = phy_read_mmd(phydev, MDIO_MMD_PCS, QCA808X_MMD3_CDT_STATUS); 1838*8c84d752SLuo Jie if (val < 0) 1839*8c84d752SLuo Jie return val; 1840*8c84d752SLuo Jie 1841*8c84d752SLuo Jie pair_a = FIELD_GET(QCA808X_CDT_CODE_PAIR_A, val); 1842*8c84d752SLuo Jie pair_b = FIELD_GET(QCA808X_CDT_CODE_PAIR_B, val); 1843*8c84d752SLuo Jie pair_c = FIELD_GET(QCA808X_CDT_CODE_PAIR_C, val); 1844*8c84d752SLuo Jie pair_d = FIELD_GET(QCA808X_CDT_CODE_PAIR_D, val); 1845*8c84d752SLuo Jie 1846*8c84d752SLuo Jie ethnl_cable_test_result(phydev, ETHTOOL_A_CABLE_PAIR_A, 1847*8c84d752SLuo Jie qca808x_cable_test_result_trans(pair_a)); 1848*8c84d752SLuo Jie ethnl_cable_test_result(phydev, ETHTOOL_A_CABLE_PAIR_B, 1849*8c84d752SLuo Jie qca808x_cable_test_result_trans(pair_b)); 1850*8c84d752SLuo Jie ethnl_cable_test_result(phydev, ETHTOOL_A_CABLE_PAIR_C, 1851*8c84d752SLuo Jie qca808x_cable_test_result_trans(pair_c)); 1852*8c84d752SLuo Jie ethnl_cable_test_result(phydev, ETHTOOL_A_CABLE_PAIR_D, 1853*8c84d752SLuo Jie qca808x_cable_test_result_trans(pair_d)); 1854*8c84d752SLuo Jie 1855*8c84d752SLuo Jie if (qca808x_cdt_fault_length_valid(pair_a)) 1856*8c84d752SLuo Jie ethnl_cable_test_fault_length(phydev, ETHTOOL_A_CABLE_PAIR_A, 1857*8c84d752SLuo Jie qca808x_cdt_fault_length(phydev, ETHTOOL_A_CABLE_PAIR_A)); 1858*8c84d752SLuo Jie if (qca808x_cdt_fault_length_valid(pair_b)) 1859*8c84d752SLuo Jie ethnl_cable_test_fault_length(phydev, ETHTOOL_A_CABLE_PAIR_B, 1860*8c84d752SLuo Jie qca808x_cdt_fault_length(phydev, ETHTOOL_A_CABLE_PAIR_B)); 1861*8c84d752SLuo Jie if (qca808x_cdt_fault_length_valid(pair_c)) 1862*8c84d752SLuo Jie ethnl_cable_test_fault_length(phydev, ETHTOOL_A_CABLE_PAIR_C, 1863*8c84d752SLuo Jie qca808x_cdt_fault_length(phydev, ETHTOOL_A_CABLE_PAIR_C)); 1864*8c84d752SLuo Jie if (qca808x_cdt_fault_length_valid(pair_d)) 1865*8c84d752SLuo Jie ethnl_cable_test_fault_length(phydev, ETHTOOL_A_CABLE_PAIR_D, 1866*8c84d752SLuo Jie qca808x_cdt_fault_length(phydev, ETHTOOL_A_CABLE_PAIR_D)); 1867*8c84d752SLuo Jie 1868*8c84d752SLuo Jie *finished = true; 1869*8c84d752SLuo Jie 1870*8c84d752SLuo Jie return 0; 1871*8c84d752SLuo Jie } 1872*8c84d752SLuo Jie 1873317420abSMugunthan V N static struct phy_driver at803x_driver[] = { 1874317420abSMugunthan V N { 187596c36712SMichael Walle /* Qualcomm Atheros AR8035 */ 18760465d8f8SMichael Walle PHY_ID_MATCH_EXACT(ATH8035_PHY_ID), 187796c36712SMichael Walle .name = "Qualcomm Atheros AR8035", 18786cb75767SMichael Walle .flags = PHY_POLL_CABLE_TEST, 18792f664823SMichael Walle .probe = at803x_probe, 18802318ca8aSMichael Walle .remove = at803x_remove, 18817dce80c2SOleksij Rempel .config_aneg = at803x_config_aneg, 18820ca7111aSMatus Ujhelyi .config_init = at803x_config_init, 1883cde0f4f8SMichael Walle .soft_reset = genphy_soft_reset, 1884ea13c9eeSMugunthan V N .set_wol = at803x_set_wol, 1885ea13c9eeSMugunthan V N .get_wol = at803x_get_wol, 18866229ed1fSDaniel Mack .suspend = at803x_suspend, 18876229ed1fSDaniel Mack .resume = at803x_resume, 1888dcdecdcfSHeiner Kallweit /* PHY_GBIT_FEATURES */ 188906d5f344SRussell King .read_status = at803x_read_status, 18900eae5982SMåns Rullgård .config_intr = at803x_config_intr, 189129773097SIoana Ciornei .handle_interrupt = at803x_handle_interrupt, 1892cde0f4f8SMichael Walle .get_tunable = at803x_get_tunable, 1893cde0f4f8SMichael Walle .set_tunable = at803x_set_tunable, 18946cb75767SMichael Walle .cable_test_start = at803x_cable_test_start, 18956cb75767SMichael Walle .cable_test_get_status = at803x_cable_test_get_status, 1896317420abSMugunthan V N }, { 189796c36712SMichael Walle /* Qualcomm Atheros AR8030 */ 1898bd8ca17fSDaniel Mack .phy_id = ATH8030_PHY_ID, 189996c36712SMichael Walle .name = "Qualcomm Atheros AR8030", 19000465d8f8SMichael Walle .phy_id_mask = AT8030_PHY_ID_MASK, 19012f664823SMichael Walle .probe = at803x_probe, 19022318ca8aSMichael Walle .remove = at803x_remove, 19030ca7111aSMatus Ujhelyi .config_init = at803x_config_init, 190413a56b44SDaniel Mack .link_change_notify = at803x_link_change_notify, 1905ea13c9eeSMugunthan V N .set_wol = at803x_set_wol, 1906ea13c9eeSMugunthan V N .get_wol = at803x_get_wol, 19076229ed1fSDaniel Mack .suspend = at803x_suspend, 19086229ed1fSDaniel Mack .resume = at803x_resume, 1909dcdecdcfSHeiner Kallweit /* PHY_BASIC_FEATURES */ 19100eae5982SMåns Rullgård .config_intr = at803x_config_intr, 191129773097SIoana Ciornei .handle_interrupt = at803x_handle_interrupt, 191205d7cce8SMugunthan V N }, { 191396c36712SMichael Walle /* Qualcomm Atheros AR8031/AR8033 */ 19140465d8f8SMichael Walle PHY_ID_MATCH_EXACT(ATH8031_PHY_ID), 191596c36712SMichael Walle .name = "Qualcomm Atheros AR8031/AR8033", 19166cb75767SMichael Walle .flags = PHY_POLL_CABLE_TEST, 19172f664823SMichael Walle .probe = at803x_probe, 19182318ca8aSMichael Walle .remove = at803x_remove, 191905d7cce8SMugunthan V N .config_init = at803x_config_init, 192063477a5dSMichael Walle .config_aneg = at803x_config_aneg, 1921cde0f4f8SMichael Walle .soft_reset = genphy_soft_reset, 192205d7cce8SMugunthan V N .set_wol = at803x_set_wol, 192305d7cce8SMugunthan V N .get_wol = at803x_get_wol, 19246229ed1fSDaniel Mack .suspend = at803x_suspend, 19256229ed1fSDaniel Mack .resume = at803x_resume, 1926c329e5afSDavid Bauer .read_page = at803x_read_page, 1927c329e5afSDavid Bauer .write_page = at803x_write_page, 1928b856150cSDavid Bauer .get_features = at803x_get_features, 192906d5f344SRussell King .read_status = at803x_read_status, 193077a99394SZhao Qiang .config_intr = &at803x_config_intr, 193129773097SIoana Ciornei .handle_interrupt = at803x_handle_interrupt, 1932cde0f4f8SMichael Walle .get_tunable = at803x_get_tunable, 1933cde0f4f8SMichael Walle .set_tunable = at803x_set_tunable, 19346cb75767SMichael Walle .cable_test_start = at803x_cable_test_start, 19356cb75767SMichael Walle .cable_test_get_status = at803x_cable_test_get_status, 19367908d2ceSOleksij Rempel }, { 19375800091aSDavid Bauer /* Qualcomm Atheros AR8032 */ 19385800091aSDavid Bauer PHY_ID_MATCH_EXACT(ATH8032_PHY_ID), 19395800091aSDavid Bauer .name = "Qualcomm Atheros AR8032", 19405800091aSDavid Bauer .probe = at803x_probe, 19415800091aSDavid Bauer .remove = at803x_remove, 1942dc0f3ed1SOleksij Rempel .flags = PHY_POLL_CABLE_TEST, 19435800091aSDavid Bauer .config_init = at803x_config_init, 19445800091aSDavid Bauer .link_change_notify = at803x_link_change_notify, 19455800091aSDavid Bauer .set_wol = at803x_set_wol, 19465800091aSDavid Bauer .get_wol = at803x_get_wol, 19475800091aSDavid Bauer .suspend = at803x_suspend, 19485800091aSDavid Bauer .resume = at803x_resume, 19495800091aSDavid Bauer /* PHY_BASIC_FEATURES */ 19505800091aSDavid Bauer .config_intr = at803x_config_intr, 195129773097SIoana Ciornei .handle_interrupt = at803x_handle_interrupt, 1952dc0f3ed1SOleksij Rempel .cable_test_start = at803x_cable_test_start, 1953dc0f3ed1SOleksij Rempel .cable_test_get_status = at803x_cable_test_get_status, 19545800091aSDavid Bauer }, { 19557908d2ceSOleksij Rempel /* ATHEROS AR9331 */ 19567908d2ceSOleksij Rempel PHY_ID_MATCH_EXACT(ATH9331_PHY_ID), 195796c36712SMichael Walle .name = "Qualcomm Atheros AR9331 built-in PHY", 19587908d2ceSOleksij Rempel .suspend = at803x_suspend, 19597908d2ceSOleksij Rempel .resume = at803x_resume, 1960dc0f3ed1SOleksij Rempel .flags = PHY_POLL_CABLE_TEST, 19617908d2ceSOleksij Rempel /* PHY_BASIC_FEATURES */ 19627908d2ceSOleksij Rempel .config_intr = &at803x_config_intr, 196329773097SIoana Ciornei .handle_interrupt = at803x_handle_interrupt, 1964dc0f3ed1SOleksij Rempel .cable_test_start = at803x_cable_test_start, 1965dc0f3ed1SOleksij Rempel .cable_test_get_status = at803x_cable_test_get_status, 19667dce80c2SOleksij Rempel .read_status = at803x_read_status, 19677dce80c2SOleksij Rempel .soft_reset = genphy_soft_reset, 19687dce80c2SOleksij Rempel .config_aneg = at803x_config_aneg, 1969272833b9SAnsuel Smith }, { 1970fada2ce0SDavid Bauer /* Qualcomm Atheros QCA9561 */ 1971fada2ce0SDavid Bauer PHY_ID_MATCH_EXACT(QCA9561_PHY_ID), 1972fada2ce0SDavid Bauer .name = "Qualcomm Atheros QCA9561 built-in PHY", 1973fada2ce0SDavid Bauer .suspend = at803x_suspend, 1974fada2ce0SDavid Bauer .resume = at803x_resume, 1975fada2ce0SDavid Bauer .flags = PHY_POLL_CABLE_TEST, 1976fada2ce0SDavid Bauer /* PHY_BASIC_FEATURES */ 1977fada2ce0SDavid Bauer .config_intr = &at803x_config_intr, 1978fada2ce0SDavid Bauer .handle_interrupt = at803x_handle_interrupt, 1979fada2ce0SDavid Bauer .cable_test_start = at803x_cable_test_start, 1980fada2ce0SDavid Bauer .cable_test_get_status = at803x_cable_test_get_status, 1981fada2ce0SDavid Bauer .read_status = at803x_read_status, 1982fada2ce0SDavid Bauer .soft_reset = genphy_soft_reset, 1983fada2ce0SDavid Bauer .config_aneg = at803x_config_aneg, 1984fada2ce0SDavid Bauer }, { 1985272833b9SAnsuel Smith /* QCA8337 */ 1986272833b9SAnsuel Smith .phy_id = QCA8337_PHY_ID, 1987272833b9SAnsuel Smith .phy_id_mask = QCA8K_PHY_ID_MASK, 1988d44fd860SAnsuel Smith .name = "Qualcomm Atheros 8337 internal PHY", 1989272833b9SAnsuel Smith /* PHY_GBIT_FEATURES */ 19901ca83119SAnsuel Smith .link_change_notify = qca83xx_link_change_notify, 1991272833b9SAnsuel Smith .probe = at803x_probe, 1992272833b9SAnsuel Smith .flags = PHY_IS_INTERNAL, 1993272833b9SAnsuel Smith .config_init = qca83xx_config_init, 1994272833b9SAnsuel Smith .soft_reset = genphy_soft_reset, 1995272833b9SAnsuel Smith .get_sset_count = at803x_get_sset_count, 1996272833b9SAnsuel Smith .get_strings = at803x_get_strings, 1997272833b9SAnsuel Smith .get_stats = at803x_get_stats, 1998ba3c01eeSAnsuel Smith .suspend = qca83xx_suspend, 1999ba3c01eeSAnsuel Smith .resume = qca83xx_resume, 20000ccf8511SAnsuel Smith }, { 2001b4df02b5SAnsuel Smith /* QCA8327-A from switch QCA8327-AL1A */ 2002b4df02b5SAnsuel Smith .phy_id = QCA8327_A_PHY_ID, 20030ccf8511SAnsuel Smith .phy_id_mask = QCA8K_PHY_ID_MASK, 2004d44fd860SAnsuel Smith .name = "Qualcomm Atheros 8327-A internal PHY", 2005b4df02b5SAnsuel Smith /* PHY_GBIT_FEATURES */ 20061ca83119SAnsuel Smith .link_change_notify = qca83xx_link_change_notify, 2007b4df02b5SAnsuel Smith .probe = at803x_probe, 2008b4df02b5SAnsuel Smith .flags = PHY_IS_INTERNAL, 2009b4df02b5SAnsuel Smith .config_init = qca83xx_config_init, 2010b4df02b5SAnsuel Smith .soft_reset = genphy_soft_reset, 2011b4df02b5SAnsuel Smith .get_sset_count = at803x_get_sset_count, 2012b4df02b5SAnsuel Smith .get_strings = at803x_get_strings, 2013b4df02b5SAnsuel Smith .get_stats = at803x_get_stats, 2014ba3c01eeSAnsuel Smith .suspend = qca83xx_suspend, 2015ba3c01eeSAnsuel Smith .resume = qca83xx_resume, 2016b4df02b5SAnsuel Smith }, { 2017b4df02b5SAnsuel Smith /* QCA8327-B from switch QCA8327-BL1A */ 2018b4df02b5SAnsuel Smith .phy_id = QCA8327_B_PHY_ID, 2019b4df02b5SAnsuel Smith .phy_id_mask = QCA8K_PHY_ID_MASK, 2020d44fd860SAnsuel Smith .name = "Qualcomm Atheros 8327-B internal PHY", 20210ccf8511SAnsuel Smith /* PHY_GBIT_FEATURES */ 20221ca83119SAnsuel Smith .link_change_notify = qca83xx_link_change_notify, 20230ccf8511SAnsuel Smith .probe = at803x_probe, 20240ccf8511SAnsuel Smith .flags = PHY_IS_INTERNAL, 20250ccf8511SAnsuel Smith .config_init = qca83xx_config_init, 20260ccf8511SAnsuel Smith .soft_reset = genphy_soft_reset, 20270ccf8511SAnsuel Smith .get_sset_count = at803x_get_sset_count, 20280ccf8511SAnsuel Smith .get_strings = at803x_get_strings, 20290ccf8511SAnsuel Smith .get_stats = at803x_get_stats, 2030ba3c01eeSAnsuel Smith .suspend = qca83xx_suspend, 2031ba3c01eeSAnsuel Smith .resume = qca83xx_resume, 2032daf61732SLuo Jie }, { 2033daf61732SLuo Jie /* Qualcomm QCA8081 */ 2034daf61732SLuo Jie PHY_ID_MATCH_EXACT(QCA8081_PHY_ID), 2035daf61732SLuo Jie .name = "Qualcomm QCA8081", 2036*8c84d752SLuo Jie .flags = PHY_POLL_CABLE_TEST, 2037daf61732SLuo Jie .config_intr = at803x_config_intr, 2038daf61732SLuo Jie .handle_interrupt = at803x_handle_interrupt, 2039daf61732SLuo Jie .get_tunable = at803x_get_tunable, 2040daf61732SLuo Jie .set_tunable = at803x_set_tunable, 2041daf61732SLuo Jie .set_wol = at803x_set_wol, 2042daf61732SLuo Jie .get_wol = at803x_get_wol, 2043765c22aaSLuo Jie .get_features = at803x_get_features, 2044f884d449SLuo Jie .config_aneg = at803x_config_aneg, 2045daf61732SLuo Jie .suspend = genphy_suspend, 2046daf61732SLuo Jie .resume = genphy_resume, 204779c7bc05SLuo Jie .read_status = qca808x_read_status, 20482acdd43fSLuo Jie .config_init = qca808x_config_init, 20499d4dae29SLuo Jie .soft_reset = qca808x_soft_reset, 2050*8c84d752SLuo Jie .cable_test_start = qca808x_cable_test_start, 2051*8c84d752SLuo Jie .cable_test_get_status = qca808x_cable_test_get_status, 2052272833b9SAnsuel Smith }, }; 20530ca7111aSMatus Ujhelyi 205450fd7150SJohan Hovold module_phy_driver(at803x_driver); 20550ca7111aSMatus Ujhelyi 20560ca7111aSMatus Ujhelyi static struct mdio_device_id __maybe_unused atheros_tbl[] = { 20570465d8f8SMichael Walle { ATH8030_PHY_ID, AT8030_PHY_ID_MASK }, 20580465d8f8SMichael Walle { PHY_ID_MATCH_EXACT(ATH8031_PHY_ID) }, 20595800091aSDavid Bauer { PHY_ID_MATCH_EXACT(ATH8032_PHY_ID) }, 20600465d8f8SMichael Walle { PHY_ID_MATCH_EXACT(ATH8035_PHY_ID) }, 20617908d2ceSOleksij Rempel { PHY_ID_MATCH_EXACT(ATH9331_PHY_ID) }, 20620ccf8511SAnsuel Smith { PHY_ID_MATCH_EXACT(QCA8337_PHY_ID) }, 2063b4df02b5SAnsuel Smith { PHY_ID_MATCH_EXACT(QCA8327_A_PHY_ID) }, 2064b4df02b5SAnsuel Smith { PHY_ID_MATCH_EXACT(QCA8327_B_PHY_ID) }, 2065fada2ce0SDavid Bauer { PHY_ID_MATCH_EXACT(QCA9561_PHY_ID) }, 2066daf61732SLuo Jie { PHY_ID_MATCH_EXACT(QCA8081_PHY_ID) }, 20670ca7111aSMatus Ujhelyi { } 20680ca7111aSMatus Ujhelyi }; 20690ca7111aSMatus Ujhelyi 20700ca7111aSMatus Ujhelyi MODULE_DEVICE_TABLE(mdio, atheros_tbl); 2071