1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * drivers/net/phy/at803x.c 4 * 5 * Driver for Qualcomm Atheros AR803x PHY 6 * 7 * Author: Matus Ujhelyi <ujhelyi.m@gmail.com> 8 */ 9 10 #include <linux/phy.h> 11 #include <linux/module.h> 12 #include <linux/string.h> 13 #include <linux/netdevice.h> 14 #include <linux/etherdevice.h> 15 #include <linux/ethtool_netlink.h> 16 #include <linux/of_gpio.h> 17 #include <linux/bitfield.h> 18 #include <linux/gpio/consumer.h> 19 #include <linux/regulator/of_regulator.h> 20 #include <linux/regulator/driver.h> 21 #include <linux/regulator/consumer.h> 22 #include <linux/phylink.h> 23 #include <linux/sfp.h> 24 #include <dt-bindings/net/qca-ar803x.h> 25 26 #define AT803X_SPECIFIC_FUNCTION_CONTROL 0x10 27 #define AT803X_SFC_ASSERT_CRS BIT(11) 28 #define AT803X_SFC_FORCE_LINK BIT(10) 29 #define AT803X_SFC_MDI_CROSSOVER_MODE_M GENMASK(6, 5) 30 #define AT803X_SFC_AUTOMATIC_CROSSOVER 0x3 31 #define AT803X_SFC_MANUAL_MDIX 0x1 32 #define AT803X_SFC_MANUAL_MDI 0x0 33 #define AT803X_SFC_SQE_TEST BIT(2) 34 #define AT803X_SFC_POLARITY_REVERSAL BIT(1) 35 #define AT803X_SFC_DISABLE_JABBER BIT(0) 36 37 #define AT803X_SPECIFIC_STATUS 0x11 38 #define AT803X_SS_SPEED_MASK GENMASK(15, 14) 39 #define AT803X_SS_SPEED_1000 2 40 #define AT803X_SS_SPEED_100 1 41 #define AT803X_SS_SPEED_10 0 42 #define AT803X_SS_DUPLEX BIT(13) 43 #define AT803X_SS_SPEED_DUPLEX_RESOLVED BIT(11) 44 #define AT803X_SS_MDIX BIT(6) 45 46 #define QCA808X_SS_SPEED_MASK GENMASK(9, 7) 47 #define QCA808X_SS_SPEED_2500 4 48 49 #define AT803X_INTR_ENABLE 0x12 50 #define AT803X_INTR_ENABLE_AUTONEG_ERR BIT(15) 51 #define AT803X_INTR_ENABLE_SPEED_CHANGED BIT(14) 52 #define AT803X_INTR_ENABLE_DUPLEX_CHANGED BIT(13) 53 #define AT803X_INTR_ENABLE_PAGE_RECEIVED BIT(12) 54 #define AT803X_INTR_ENABLE_LINK_FAIL BIT(11) 55 #define AT803X_INTR_ENABLE_LINK_SUCCESS BIT(10) 56 #define AT803X_INTR_ENABLE_LINK_FAIL_BX BIT(8) 57 #define AT803X_INTR_ENABLE_LINK_SUCCESS_BX BIT(7) 58 #define AT803X_INTR_ENABLE_WIRESPEED_DOWNGRADE BIT(5) 59 #define AT803X_INTR_ENABLE_POLARITY_CHANGED BIT(1) 60 #define AT803X_INTR_ENABLE_WOL BIT(0) 61 62 #define AT803X_INTR_STATUS 0x13 63 64 #define AT803X_SMART_SPEED 0x14 65 #define AT803X_SMART_SPEED_ENABLE BIT(5) 66 #define AT803X_SMART_SPEED_RETRY_LIMIT_MASK GENMASK(4, 2) 67 #define AT803X_SMART_SPEED_BYPASS_TIMER BIT(1) 68 #define AT803X_CDT 0x16 69 #define AT803X_CDT_MDI_PAIR_MASK GENMASK(9, 8) 70 #define AT803X_CDT_ENABLE_TEST BIT(0) 71 #define AT803X_CDT_STATUS 0x1c 72 #define AT803X_CDT_STATUS_STAT_NORMAL 0 73 #define AT803X_CDT_STATUS_STAT_SHORT 1 74 #define AT803X_CDT_STATUS_STAT_OPEN 2 75 #define AT803X_CDT_STATUS_STAT_FAIL 3 76 #define AT803X_CDT_STATUS_STAT_MASK GENMASK(9, 8) 77 #define AT803X_CDT_STATUS_DELTA_TIME_MASK GENMASK(7, 0) 78 #define AT803X_LED_CONTROL 0x18 79 80 #define AT803X_PHY_MMD3_WOL_CTRL 0x8012 81 #define AT803X_WOL_EN BIT(5) 82 #define AT803X_LOC_MAC_ADDR_0_15_OFFSET 0x804C 83 #define AT803X_LOC_MAC_ADDR_16_31_OFFSET 0x804B 84 #define AT803X_LOC_MAC_ADDR_32_47_OFFSET 0x804A 85 #define AT803X_REG_CHIP_CONFIG 0x1f 86 #define AT803X_BT_BX_REG_SEL 0x8000 87 88 #define AT803X_DEBUG_ADDR 0x1D 89 #define AT803X_DEBUG_DATA 0x1E 90 91 #define AT803X_MODE_CFG_MASK 0x0F 92 #define AT803X_MODE_CFG_BASET_RGMII 0x00 93 #define AT803X_MODE_CFG_BASET_SGMII 0x01 94 #define AT803X_MODE_CFG_BX1000_RGMII_50OHM 0x02 95 #define AT803X_MODE_CFG_BX1000_RGMII_75OHM 0x03 96 #define AT803X_MODE_CFG_BX1000_CONV_50OHM 0x04 97 #define AT803X_MODE_CFG_BX1000_CONV_75OHM 0x05 98 #define AT803X_MODE_CFG_FX100_RGMII_50OHM 0x06 99 #define AT803X_MODE_CFG_FX100_CONV_50OHM 0x07 100 #define AT803X_MODE_CFG_RGMII_AUTO_MDET 0x0B 101 #define AT803X_MODE_CFG_FX100_RGMII_75OHM 0x0E 102 #define AT803X_MODE_CFG_FX100_CONV_75OHM 0x0F 103 104 #define AT803X_PSSR 0x11 /*PHY-Specific Status Register*/ 105 #define AT803X_PSSR_MR_AN_COMPLETE 0x0200 106 107 #define AT803X_DEBUG_ANALOG_TEST_CTRL 0x00 108 #define QCA8327_DEBUG_MANU_CTRL_EN BIT(2) 109 #define QCA8337_DEBUG_MANU_CTRL_EN GENMASK(3, 2) 110 #define AT803X_DEBUG_RX_CLK_DLY_EN BIT(15) 111 112 #define AT803X_DEBUG_SYSTEM_CTRL_MODE 0x05 113 #define AT803X_DEBUG_TX_CLK_DLY_EN BIT(8) 114 115 #define AT803X_DEBUG_REG_HIB_CTRL 0x0b 116 #define AT803X_DEBUG_HIB_CTRL_SEL_RST_80U BIT(10) 117 #define AT803X_DEBUG_HIB_CTRL_EN_ANY_CHANGE BIT(13) 118 #define AT803X_DEBUG_HIB_CTRL_PS_HIB_EN BIT(15) 119 120 #define AT803X_DEBUG_REG_3C 0x3C 121 122 #define AT803X_DEBUG_REG_GREEN 0x3D 123 #define AT803X_DEBUG_GATE_CLK_IN1000 BIT(6) 124 125 #define AT803X_DEBUG_REG_1F 0x1F 126 #define AT803X_DEBUG_PLL_ON BIT(2) 127 #define AT803X_DEBUG_RGMII_1V8 BIT(3) 128 129 #define MDIO_AZ_DEBUG 0x800D 130 131 /* AT803x supports either the XTAL input pad, an internal PLL or the 132 * DSP as clock reference for the clock output pad. The XTAL reference 133 * is only used for 25 MHz output, all other frequencies need the PLL. 134 * The DSP as a clock reference is used in synchronous ethernet 135 * applications. 136 * 137 * By default the PLL is only enabled if there is a link. Otherwise 138 * the PHY will go into low power state and disabled the PLL. You can 139 * set the PLL_ON bit (see debug register 0x1f) to keep the PLL always 140 * enabled. 141 */ 142 #define AT803X_MMD7_CLK25M 0x8016 143 #define AT803X_CLK_OUT_MASK GENMASK(4, 2) 144 #define AT803X_CLK_OUT_25MHZ_XTAL 0 145 #define AT803X_CLK_OUT_25MHZ_DSP 1 146 #define AT803X_CLK_OUT_50MHZ_PLL 2 147 #define AT803X_CLK_OUT_50MHZ_DSP 3 148 #define AT803X_CLK_OUT_62_5MHZ_PLL 4 149 #define AT803X_CLK_OUT_62_5MHZ_DSP 5 150 #define AT803X_CLK_OUT_125MHZ_PLL 6 151 #define AT803X_CLK_OUT_125MHZ_DSP 7 152 153 /* The AR8035 has another mask which is compatible with the AR8031/AR8033 mask 154 * but doesn't support choosing between XTAL/PLL and DSP. 155 */ 156 #define AT8035_CLK_OUT_MASK GENMASK(4, 3) 157 158 #define AT803X_CLK_OUT_STRENGTH_MASK GENMASK(8, 7) 159 #define AT803X_CLK_OUT_STRENGTH_FULL 0 160 #define AT803X_CLK_OUT_STRENGTH_HALF 1 161 #define AT803X_CLK_OUT_STRENGTH_QUARTER 2 162 163 #define AT803X_DEFAULT_DOWNSHIFT 5 164 #define AT803X_MIN_DOWNSHIFT 2 165 #define AT803X_MAX_DOWNSHIFT 9 166 167 #define AT803X_MMD3_SMARTEEE_CTL1 0x805b 168 #define AT803X_MMD3_SMARTEEE_CTL2 0x805c 169 #define AT803X_MMD3_SMARTEEE_CTL3 0x805d 170 #define AT803X_MMD3_SMARTEEE_CTL3_LPI_EN BIT(8) 171 172 #define ATH9331_PHY_ID 0x004dd041 173 #define ATH8030_PHY_ID 0x004dd076 174 #define ATH8031_PHY_ID 0x004dd074 175 #define ATH8032_PHY_ID 0x004dd023 176 #define ATH8035_PHY_ID 0x004dd072 177 #define AT8030_PHY_ID_MASK 0xffffffef 178 179 #define QCA8081_PHY_ID 0x004dd101 180 181 #define QCA8327_A_PHY_ID 0x004dd033 182 #define QCA8327_B_PHY_ID 0x004dd034 183 #define QCA8337_PHY_ID 0x004dd036 184 #define QCA9561_PHY_ID 0x004dd042 185 #define QCA8K_PHY_ID_MASK 0xffffffff 186 187 #define QCA8K_DEVFLAGS_REVISION_MASK GENMASK(2, 0) 188 189 #define AT803X_PAGE_FIBER 0 190 #define AT803X_PAGE_COPPER 1 191 192 /* don't turn off internal PLL */ 193 #define AT803X_KEEP_PLL_ENABLED BIT(0) 194 #define AT803X_DISABLE_SMARTEEE BIT(1) 195 196 /* disable hibernation mode */ 197 #define AT803X_DISABLE_HIBERNATION_MODE BIT(2) 198 199 /* ADC threshold */ 200 #define QCA808X_PHY_DEBUG_ADC_THRESHOLD 0x2c80 201 #define QCA808X_ADC_THRESHOLD_MASK GENMASK(7, 0) 202 #define QCA808X_ADC_THRESHOLD_80MV 0 203 #define QCA808X_ADC_THRESHOLD_100MV 0xf0 204 #define QCA808X_ADC_THRESHOLD_200MV 0x0f 205 #define QCA808X_ADC_THRESHOLD_300MV 0xff 206 207 /* CLD control */ 208 #define QCA808X_PHY_MMD3_ADDR_CLD_CTRL7 0x8007 209 #define QCA808X_8023AZ_AFE_CTRL_MASK GENMASK(8, 4) 210 #define QCA808X_8023AZ_AFE_EN 0x90 211 212 /* AZ control */ 213 #define QCA808X_PHY_MMD3_AZ_TRAINING_CTRL 0x8008 214 #define QCA808X_MMD3_AZ_TRAINING_VAL 0x1c32 215 216 #define QCA808X_PHY_MMD1_MSE_THRESHOLD_20DB 0x8014 217 #define QCA808X_MSE_THRESHOLD_20DB_VALUE 0x529 218 219 #define QCA808X_PHY_MMD1_MSE_THRESHOLD_17DB 0x800E 220 #define QCA808X_MSE_THRESHOLD_17DB_VALUE 0x341 221 222 #define QCA808X_PHY_MMD1_MSE_THRESHOLD_27DB 0x801E 223 #define QCA808X_MSE_THRESHOLD_27DB_VALUE 0x419 224 225 #define QCA808X_PHY_MMD1_MSE_THRESHOLD_28DB 0x8020 226 #define QCA808X_MSE_THRESHOLD_28DB_VALUE 0x341 227 228 #define QCA808X_PHY_MMD7_TOP_OPTION1 0x901c 229 #define QCA808X_TOP_OPTION1_DATA 0x0 230 231 #define QCA808X_PHY_MMD3_DEBUG_1 0xa100 232 #define QCA808X_MMD3_DEBUG_1_VALUE 0x9203 233 #define QCA808X_PHY_MMD3_DEBUG_2 0xa101 234 #define QCA808X_MMD3_DEBUG_2_VALUE 0x48ad 235 #define QCA808X_PHY_MMD3_DEBUG_3 0xa103 236 #define QCA808X_MMD3_DEBUG_3_VALUE 0x1698 237 #define QCA808X_PHY_MMD3_DEBUG_4 0xa105 238 #define QCA808X_MMD3_DEBUG_4_VALUE 0x8001 239 #define QCA808X_PHY_MMD3_DEBUG_5 0xa106 240 #define QCA808X_MMD3_DEBUG_5_VALUE 0x1111 241 #define QCA808X_PHY_MMD3_DEBUG_6 0xa011 242 #define QCA808X_MMD3_DEBUG_6_VALUE 0x5f85 243 244 /* master/slave seed config */ 245 #define QCA808X_PHY_DEBUG_LOCAL_SEED 9 246 #define QCA808X_MASTER_SLAVE_SEED_ENABLE BIT(1) 247 #define QCA808X_MASTER_SLAVE_SEED_CFG GENMASK(12, 2) 248 #define QCA808X_MASTER_SLAVE_SEED_RANGE 0x32 249 250 /* Hibernation yields lower power consumpiton in contrast with normal operation mode. 251 * when the copper cable is unplugged, the PHY enters into hibernation mode in about 10s. 252 */ 253 #define QCA808X_DBG_AN_TEST 0xb 254 #define QCA808X_HIBERNATION_EN BIT(15) 255 256 #define QCA808X_CDT_ENABLE_TEST BIT(15) 257 #define QCA808X_CDT_INTER_CHECK_DIS BIT(13) 258 #define QCA808X_CDT_LENGTH_UNIT BIT(10) 259 260 #define QCA808X_MMD3_CDT_STATUS 0x8064 261 #define QCA808X_MMD3_CDT_DIAG_PAIR_A 0x8065 262 #define QCA808X_MMD3_CDT_DIAG_PAIR_B 0x8066 263 #define QCA808X_MMD3_CDT_DIAG_PAIR_C 0x8067 264 #define QCA808X_MMD3_CDT_DIAG_PAIR_D 0x8068 265 #define QCA808X_CDT_DIAG_LENGTH GENMASK(7, 0) 266 267 #define QCA808X_CDT_CODE_PAIR_A GENMASK(15, 12) 268 #define QCA808X_CDT_CODE_PAIR_B GENMASK(11, 8) 269 #define QCA808X_CDT_CODE_PAIR_C GENMASK(7, 4) 270 #define QCA808X_CDT_CODE_PAIR_D GENMASK(3, 0) 271 #define QCA808X_CDT_STATUS_STAT_FAIL 0 272 #define QCA808X_CDT_STATUS_STAT_NORMAL 1 273 #define QCA808X_CDT_STATUS_STAT_OPEN 2 274 #define QCA808X_CDT_STATUS_STAT_SHORT 3 275 276 MODULE_DESCRIPTION("Qualcomm Atheros AR803x and QCA808X PHY driver"); 277 MODULE_AUTHOR("Matus Ujhelyi"); 278 MODULE_LICENSE("GPL"); 279 280 enum stat_access_type { 281 PHY, 282 MMD 283 }; 284 285 struct at803x_hw_stat { 286 const char *string; 287 u8 reg; 288 u32 mask; 289 enum stat_access_type access_type; 290 }; 291 292 static struct at803x_hw_stat at803x_hw_stats[] = { 293 { "phy_idle_errors", 0xa, GENMASK(7, 0), PHY}, 294 { "phy_receive_errors", 0x15, GENMASK(15, 0), PHY}, 295 { "eee_wake_errors", 0x16, GENMASK(15, 0), MMD}, 296 }; 297 298 struct at803x_priv { 299 int flags; 300 u16 clk_25m_reg; 301 u16 clk_25m_mask; 302 u8 smarteee_lpi_tw_1g; 303 u8 smarteee_lpi_tw_100m; 304 bool is_fiber; 305 bool is_1000basex; 306 struct regulator_dev *vddio_rdev; 307 struct regulator_dev *vddh_rdev; 308 struct regulator *vddio; 309 u64 stats[ARRAY_SIZE(at803x_hw_stats)]; 310 }; 311 312 struct at803x_context { 313 u16 bmcr; 314 u16 advertise; 315 u16 control1000; 316 u16 int_enable; 317 u16 smart_speed; 318 u16 led_control; 319 }; 320 321 static int at803x_debug_reg_write(struct phy_device *phydev, u16 reg, u16 data) 322 { 323 int ret; 324 325 ret = phy_write(phydev, AT803X_DEBUG_ADDR, reg); 326 if (ret < 0) 327 return ret; 328 329 return phy_write(phydev, AT803X_DEBUG_DATA, data); 330 } 331 332 static int at803x_debug_reg_read(struct phy_device *phydev, u16 reg) 333 { 334 int ret; 335 336 ret = phy_write(phydev, AT803X_DEBUG_ADDR, reg); 337 if (ret < 0) 338 return ret; 339 340 return phy_read(phydev, AT803X_DEBUG_DATA); 341 } 342 343 static int at803x_debug_reg_mask(struct phy_device *phydev, u16 reg, 344 u16 clear, u16 set) 345 { 346 u16 val; 347 int ret; 348 349 ret = at803x_debug_reg_read(phydev, reg); 350 if (ret < 0) 351 return ret; 352 353 val = ret & 0xffff; 354 val &= ~clear; 355 val |= set; 356 357 return phy_write(phydev, AT803X_DEBUG_DATA, val); 358 } 359 360 static int at803x_write_page(struct phy_device *phydev, int page) 361 { 362 int mask; 363 int set; 364 365 if (page == AT803X_PAGE_COPPER) { 366 set = AT803X_BT_BX_REG_SEL; 367 mask = 0; 368 } else { 369 set = 0; 370 mask = AT803X_BT_BX_REG_SEL; 371 } 372 373 return __phy_modify(phydev, AT803X_REG_CHIP_CONFIG, mask, set); 374 } 375 376 static int at803x_read_page(struct phy_device *phydev) 377 { 378 int ccr = __phy_read(phydev, AT803X_REG_CHIP_CONFIG); 379 380 if (ccr < 0) 381 return ccr; 382 383 if (ccr & AT803X_BT_BX_REG_SEL) 384 return AT803X_PAGE_COPPER; 385 386 return AT803X_PAGE_FIBER; 387 } 388 389 static int at803x_enable_rx_delay(struct phy_device *phydev) 390 { 391 return at803x_debug_reg_mask(phydev, AT803X_DEBUG_ANALOG_TEST_CTRL, 0, 392 AT803X_DEBUG_RX_CLK_DLY_EN); 393 } 394 395 static int at803x_enable_tx_delay(struct phy_device *phydev) 396 { 397 return at803x_debug_reg_mask(phydev, AT803X_DEBUG_SYSTEM_CTRL_MODE, 0, 398 AT803X_DEBUG_TX_CLK_DLY_EN); 399 } 400 401 static int at803x_disable_rx_delay(struct phy_device *phydev) 402 { 403 return at803x_debug_reg_mask(phydev, AT803X_DEBUG_ANALOG_TEST_CTRL, 404 AT803X_DEBUG_RX_CLK_DLY_EN, 0); 405 } 406 407 static int at803x_disable_tx_delay(struct phy_device *phydev) 408 { 409 return at803x_debug_reg_mask(phydev, AT803X_DEBUG_SYSTEM_CTRL_MODE, 410 AT803X_DEBUG_TX_CLK_DLY_EN, 0); 411 } 412 413 /* save relevant PHY registers to private copy */ 414 static void at803x_context_save(struct phy_device *phydev, 415 struct at803x_context *context) 416 { 417 context->bmcr = phy_read(phydev, MII_BMCR); 418 context->advertise = phy_read(phydev, MII_ADVERTISE); 419 context->control1000 = phy_read(phydev, MII_CTRL1000); 420 context->int_enable = phy_read(phydev, AT803X_INTR_ENABLE); 421 context->smart_speed = phy_read(phydev, AT803X_SMART_SPEED); 422 context->led_control = phy_read(phydev, AT803X_LED_CONTROL); 423 } 424 425 /* restore relevant PHY registers from private copy */ 426 static void at803x_context_restore(struct phy_device *phydev, 427 const struct at803x_context *context) 428 { 429 phy_write(phydev, MII_BMCR, context->bmcr); 430 phy_write(phydev, MII_ADVERTISE, context->advertise); 431 phy_write(phydev, MII_CTRL1000, context->control1000); 432 phy_write(phydev, AT803X_INTR_ENABLE, context->int_enable); 433 phy_write(phydev, AT803X_SMART_SPEED, context->smart_speed); 434 phy_write(phydev, AT803X_LED_CONTROL, context->led_control); 435 } 436 437 static int at803x_set_wol(struct phy_device *phydev, 438 struct ethtool_wolinfo *wol) 439 { 440 int ret, irq_enabled; 441 442 if (wol->wolopts & WAKE_MAGIC) { 443 struct net_device *ndev = phydev->attached_dev; 444 const u8 *mac; 445 unsigned int i; 446 static const unsigned int offsets[] = { 447 AT803X_LOC_MAC_ADDR_32_47_OFFSET, 448 AT803X_LOC_MAC_ADDR_16_31_OFFSET, 449 AT803X_LOC_MAC_ADDR_0_15_OFFSET, 450 }; 451 452 if (!ndev) 453 return -ENODEV; 454 455 mac = (const u8 *) ndev->dev_addr; 456 457 if (!is_valid_ether_addr(mac)) 458 return -EINVAL; 459 460 for (i = 0; i < 3; i++) 461 phy_write_mmd(phydev, MDIO_MMD_PCS, offsets[i], 462 mac[(i * 2) + 1] | (mac[(i * 2)] << 8)); 463 464 /* Enable WOL function */ 465 ret = phy_modify_mmd(phydev, MDIO_MMD_PCS, AT803X_PHY_MMD3_WOL_CTRL, 466 0, AT803X_WOL_EN); 467 if (ret) 468 return ret; 469 /* Enable WOL interrupt */ 470 ret = phy_modify(phydev, AT803X_INTR_ENABLE, 0, AT803X_INTR_ENABLE_WOL); 471 if (ret) 472 return ret; 473 } else { 474 /* Disable WoL function */ 475 ret = phy_modify_mmd(phydev, MDIO_MMD_PCS, AT803X_PHY_MMD3_WOL_CTRL, 476 AT803X_WOL_EN, 0); 477 if (ret) 478 return ret; 479 /* Disable WOL interrupt */ 480 ret = phy_modify(phydev, AT803X_INTR_ENABLE, AT803X_INTR_ENABLE_WOL, 0); 481 if (ret) 482 return ret; 483 } 484 485 /* Clear WOL status */ 486 ret = phy_read(phydev, AT803X_INTR_STATUS); 487 if (ret < 0) 488 return ret; 489 490 /* Check if there are other interrupts except for WOL triggered when PHY is 491 * in interrupt mode, only the interrupts enabled by AT803X_INTR_ENABLE can 492 * be passed up to the interrupt PIN. 493 */ 494 irq_enabled = phy_read(phydev, AT803X_INTR_ENABLE); 495 if (irq_enabled < 0) 496 return irq_enabled; 497 498 irq_enabled &= ~AT803X_INTR_ENABLE_WOL; 499 if (ret & irq_enabled && !phy_polling_mode(phydev)) 500 phy_trigger_machine(phydev); 501 502 return 0; 503 } 504 505 static void at803x_get_wol(struct phy_device *phydev, 506 struct ethtool_wolinfo *wol) 507 { 508 int value; 509 510 wol->supported = WAKE_MAGIC; 511 wol->wolopts = 0; 512 513 value = phy_read_mmd(phydev, MDIO_MMD_PCS, AT803X_PHY_MMD3_WOL_CTRL); 514 if (value < 0) 515 return; 516 517 if (value & AT803X_WOL_EN) 518 wol->wolopts |= WAKE_MAGIC; 519 } 520 521 static int at803x_get_sset_count(struct phy_device *phydev) 522 { 523 return ARRAY_SIZE(at803x_hw_stats); 524 } 525 526 static void at803x_get_strings(struct phy_device *phydev, u8 *data) 527 { 528 int i; 529 530 for (i = 0; i < ARRAY_SIZE(at803x_hw_stats); i++) { 531 strscpy(data + i * ETH_GSTRING_LEN, 532 at803x_hw_stats[i].string, ETH_GSTRING_LEN); 533 } 534 } 535 536 static u64 at803x_get_stat(struct phy_device *phydev, int i) 537 { 538 struct at803x_hw_stat stat = at803x_hw_stats[i]; 539 struct at803x_priv *priv = phydev->priv; 540 int val; 541 u64 ret; 542 543 if (stat.access_type == MMD) 544 val = phy_read_mmd(phydev, MDIO_MMD_PCS, stat.reg); 545 else 546 val = phy_read(phydev, stat.reg); 547 548 if (val < 0) { 549 ret = U64_MAX; 550 } else { 551 val = val & stat.mask; 552 priv->stats[i] += val; 553 ret = priv->stats[i]; 554 } 555 556 return ret; 557 } 558 559 static void at803x_get_stats(struct phy_device *phydev, 560 struct ethtool_stats *stats, u64 *data) 561 { 562 int i; 563 564 for (i = 0; i < ARRAY_SIZE(at803x_hw_stats); i++) 565 data[i] = at803x_get_stat(phydev, i); 566 } 567 568 static int at803x_suspend(struct phy_device *phydev) 569 { 570 int value; 571 int wol_enabled; 572 573 value = phy_read(phydev, AT803X_INTR_ENABLE); 574 wol_enabled = value & AT803X_INTR_ENABLE_WOL; 575 576 if (wol_enabled) 577 value = BMCR_ISOLATE; 578 else 579 value = BMCR_PDOWN; 580 581 phy_modify(phydev, MII_BMCR, 0, value); 582 583 return 0; 584 } 585 586 static int at803x_resume(struct phy_device *phydev) 587 { 588 return phy_modify(phydev, MII_BMCR, BMCR_PDOWN | BMCR_ISOLATE, 0); 589 } 590 591 static int at803x_rgmii_reg_set_voltage_sel(struct regulator_dev *rdev, 592 unsigned int selector) 593 { 594 struct phy_device *phydev = rdev_get_drvdata(rdev); 595 596 if (selector) 597 return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_1F, 598 0, AT803X_DEBUG_RGMII_1V8); 599 else 600 return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_1F, 601 AT803X_DEBUG_RGMII_1V8, 0); 602 } 603 604 static int at803x_rgmii_reg_get_voltage_sel(struct regulator_dev *rdev) 605 { 606 struct phy_device *phydev = rdev_get_drvdata(rdev); 607 int val; 608 609 val = at803x_debug_reg_read(phydev, AT803X_DEBUG_REG_1F); 610 if (val < 0) 611 return val; 612 613 return (val & AT803X_DEBUG_RGMII_1V8) ? 1 : 0; 614 } 615 616 static const struct regulator_ops vddio_regulator_ops = { 617 .list_voltage = regulator_list_voltage_table, 618 .set_voltage_sel = at803x_rgmii_reg_set_voltage_sel, 619 .get_voltage_sel = at803x_rgmii_reg_get_voltage_sel, 620 }; 621 622 static const unsigned int vddio_voltage_table[] = { 623 1500000, 624 1800000, 625 }; 626 627 static const struct regulator_desc vddio_desc = { 628 .name = "vddio", 629 .of_match = of_match_ptr("vddio-regulator"), 630 .n_voltages = ARRAY_SIZE(vddio_voltage_table), 631 .volt_table = vddio_voltage_table, 632 .ops = &vddio_regulator_ops, 633 .type = REGULATOR_VOLTAGE, 634 .owner = THIS_MODULE, 635 }; 636 637 static const struct regulator_ops vddh_regulator_ops = { 638 }; 639 640 static const struct regulator_desc vddh_desc = { 641 .name = "vddh", 642 .of_match = of_match_ptr("vddh-regulator"), 643 .n_voltages = 1, 644 .fixed_uV = 2500000, 645 .ops = &vddh_regulator_ops, 646 .type = REGULATOR_VOLTAGE, 647 .owner = THIS_MODULE, 648 }; 649 650 static int at8031_register_regulators(struct phy_device *phydev) 651 { 652 struct at803x_priv *priv = phydev->priv; 653 struct device *dev = &phydev->mdio.dev; 654 struct regulator_config config = { }; 655 656 config.dev = dev; 657 config.driver_data = phydev; 658 659 priv->vddio_rdev = devm_regulator_register(dev, &vddio_desc, &config); 660 if (IS_ERR(priv->vddio_rdev)) { 661 phydev_err(phydev, "failed to register VDDIO regulator\n"); 662 return PTR_ERR(priv->vddio_rdev); 663 } 664 665 priv->vddh_rdev = devm_regulator_register(dev, &vddh_desc, &config); 666 if (IS_ERR(priv->vddh_rdev)) { 667 phydev_err(phydev, "failed to register VDDH regulator\n"); 668 return PTR_ERR(priv->vddh_rdev); 669 } 670 671 return 0; 672 } 673 674 static int at803x_sfp_insert(void *upstream, const struct sfp_eeprom_id *id) 675 { 676 struct phy_device *phydev = upstream; 677 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_support); 678 __ETHTOOL_DECLARE_LINK_MODE_MASK(sfp_support); 679 DECLARE_PHY_INTERFACE_MASK(interfaces); 680 phy_interface_t iface; 681 682 linkmode_zero(phy_support); 683 phylink_set(phy_support, 1000baseX_Full); 684 phylink_set(phy_support, 1000baseT_Full); 685 phylink_set(phy_support, Autoneg); 686 phylink_set(phy_support, Pause); 687 phylink_set(phy_support, Asym_Pause); 688 689 linkmode_zero(sfp_support); 690 sfp_parse_support(phydev->sfp_bus, id, sfp_support, interfaces); 691 /* Some modules support 10G modes as well as others we support. 692 * Mask out non-supported modes so the correct interface is picked. 693 */ 694 linkmode_and(sfp_support, phy_support, sfp_support); 695 696 if (linkmode_empty(sfp_support)) { 697 dev_err(&phydev->mdio.dev, "incompatible SFP module inserted\n"); 698 return -EINVAL; 699 } 700 701 iface = sfp_select_interface(phydev->sfp_bus, sfp_support); 702 703 /* Only 1000Base-X is supported by AR8031/8033 as the downstream SerDes 704 * interface for use with SFP modules. 705 * However, some copper modules detected as having a preferred SGMII 706 * interface do default to and function in 1000Base-X mode, so just 707 * print a warning and allow such modules, as they may have some chance 708 * of working. 709 */ 710 if (iface == PHY_INTERFACE_MODE_SGMII) 711 dev_warn(&phydev->mdio.dev, "module may not function if 1000Base-X not supported\n"); 712 else if (iface != PHY_INTERFACE_MODE_1000BASEX) 713 return -EINVAL; 714 715 return 0; 716 } 717 718 static const struct sfp_upstream_ops at803x_sfp_ops = { 719 .attach = phy_sfp_attach, 720 .detach = phy_sfp_detach, 721 .module_insert = at803x_sfp_insert, 722 }; 723 724 static int at803x_parse_dt(struct phy_device *phydev) 725 { 726 struct device_node *node = phydev->mdio.dev.of_node; 727 struct at803x_priv *priv = phydev->priv; 728 u32 freq, strength, tw; 729 unsigned int sel; 730 int ret; 731 732 if (!IS_ENABLED(CONFIG_OF_MDIO)) 733 return 0; 734 735 if (of_property_read_bool(node, "qca,disable-smarteee")) 736 priv->flags |= AT803X_DISABLE_SMARTEEE; 737 738 if (of_property_read_bool(node, "qca,disable-hibernation-mode")) 739 priv->flags |= AT803X_DISABLE_HIBERNATION_MODE; 740 741 if (!of_property_read_u32(node, "qca,smarteee-tw-us-1g", &tw)) { 742 if (!tw || tw > 255) { 743 phydev_err(phydev, "invalid qca,smarteee-tw-us-1g\n"); 744 return -EINVAL; 745 } 746 priv->smarteee_lpi_tw_1g = tw; 747 } 748 749 if (!of_property_read_u32(node, "qca,smarteee-tw-us-100m", &tw)) { 750 if (!tw || tw > 255) { 751 phydev_err(phydev, "invalid qca,smarteee-tw-us-100m\n"); 752 return -EINVAL; 753 } 754 priv->smarteee_lpi_tw_100m = tw; 755 } 756 757 ret = of_property_read_u32(node, "qca,clk-out-frequency", &freq); 758 if (!ret) { 759 switch (freq) { 760 case 25000000: 761 sel = AT803X_CLK_OUT_25MHZ_XTAL; 762 break; 763 case 50000000: 764 sel = AT803X_CLK_OUT_50MHZ_PLL; 765 break; 766 case 62500000: 767 sel = AT803X_CLK_OUT_62_5MHZ_PLL; 768 break; 769 case 125000000: 770 sel = AT803X_CLK_OUT_125MHZ_PLL; 771 break; 772 default: 773 phydev_err(phydev, "invalid qca,clk-out-frequency\n"); 774 return -EINVAL; 775 } 776 777 priv->clk_25m_reg |= FIELD_PREP(AT803X_CLK_OUT_MASK, sel); 778 priv->clk_25m_mask |= AT803X_CLK_OUT_MASK; 779 780 /* Fixup for the AR8030/AR8035. This chip has another mask and 781 * doesn't support the DSP reference. Eg. the lowest bit of the 782 * mask. The upper two bits select the same frequencies. Mask 783 * the lowest bit here. 784 * 785 * Warning: 786 * There was no datasheet for the AR8030 available so this is 787 * just a guess. But the AR8035 is listed as pin compatible 788 * to the AR8030 so there might be a good chance it works on 789 * the AR8030 too. 790 */ 791 if (phydev->drv->phy_id == ATH8030_PHY_ID || 792 phydev->drv->phy_id == ATH8035_PHY_ID) { 793 priv->clk_25m_reg &= AT8035_CLK_OUT_MASK; 794 priv->clk_25m_mask &= AT8035_CLK_OUT_MASK; 795 } 796 } 797 798 ret = of_property_read_u32(node, "qca,clk-out-strength", &strength); 799 if (!ret) { 800 priv->clk_25m_mask |= AT803X_CLK_OUT_STRENGTH_MASK; 801 switch (strength) { 802 case AR803X_STRENGTH_FULL: 803 priv->clk_25m_reg |= AT803X_CLK_OUT_STRENGTH_FULL; 804 break; 805 case AR803X_STRENGTH_HALF: 806 priv->clk_25m_reg |= AT803X_CLK_OUT_STRENGTH_HALF; 807 break; 808 case AR803X_STRENGTH_QUARTER: 809 priv->clk_25m_reg |= AT803X_CLK_OUT_STRENGTH_QUARTER; 810 break; 811 default: 812 phydev_err(phydev, "invalid qca,clk-out-strength\n"); 813 return -EINVAL; 814 } 815 } 816 817 /* Only supported on AR8031/AR8033, the AR8030/AR8035 use strapping 818 * options. 819 */ 820 if (phydev->drv->phy_id == ATH8031_PHY_ID) { 821 if (of_property_read_bool(node, "qca,keep-pll-enabled")) 822 priv->flags |= AT803X_KEEP_PLL_ENABLED; 823 824 ret = at8031_register_regulators(phydev); 825 if (ret < 0) 826 return ret; 827 828 priv->vddio = devm_regulator_get_optional(&phydev->mdio.dev, 829 "vddio"); 830 if (IS_ERR(priv->vddio)) { 831 phydev_err(phydev, "failed to get VDDIO regulator\n"); 832 return PTR_ERR(priv->vddio); 833 } 834 835 /* Only AR8031/8033 support 1000Base-X for SFP modules */ 836 ret = phy_sfp_probe(phydev, &at803x_sfp_ops); 837 if (ret < 0) 838 return ret; 839 } 840 841 return 0; 842 } 843 844 static int at803x_probe(struct phy_device *phydev) 845 { 846 struct device *dev = &phydev->mdio.dev; 847 struct at803x_priv *priv; 848 int ret; 849 850 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 851 if (!priv) 852 return -ENOMEM; 853 854 phydev->priv = priv; 855 856 ret = at803x_parse_dt(phydev); 857 if (ret) 858 return ret; 859 860 if (priv->vddio) { 861 ret = regulator_enable(priv->vddio); 862 if (ret < 0) 863 return ret; 864 } 865 866 if (phydev->drv->phy_id == ATH8031_PHY_ID) { 867 int ccr = phy_read(phydev, AT803X_REG_CHIP_CONFIG); 868 int mode_cfg; 869 struct ethtool_wolinfo wol = { 870 .wolopts = 0, 871 }; 872 873 if (ccr < 0) 874 goto err; 875 mode_cfg = ccr & AT803X_MODE_CFG_MASK; 876 877 switch (mode_cfg) { 878 case AT803X_MODE_CFG_BX1000_RGMII_50OHM: 879 case AT803X_MODE_CFG_BX1000_RGMII_75OHM: 880 priv->is_1000basex = true; 881 fallthrough; 882 case AT803X_MODE_CFG_FX100_RGMII_50OHM: 883 case AT803X_MODE_CFG_FX100_RGMII_75OHM: 884 priv->is_fiber = true; 885 break; 886 } 887 888 /* Disable WOL by default */ 889 ret = at803x_set_wol(phydev, &wol); 890 if (ret < 0) { 891 phydev_err(phydev, "failed to disable WOL on probe: %d\n", ret); 892 goto err; 893 } 894 } 895 896 return 0; 897 898 err: 899 if (priv->vddio) 900 regulator_disable(priv->vddio); 901 902 return ret; 903 } 904 905 static void at803x_remove(struct phy_device *phydev) 906 { 907 struct at803x_priv *priv = phydev->priv; 908 909 if (priv->vddio) 910 regulator_disable(priv->vddio); 911 } 912 913 static int at803x_get_features(struct phy_device *phydev) 914 { 915 struct at803x_priv *priv = phydev->priv; 916 int err; 917 918 err = genphy_read_abilities(phydev); 919 if (err) 920 return err; 921 922 if (phydev->drv->phy_id == QCA8081_PHY_ID) { 923 err = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_PMA_NG_EXTABLE); 924 if (err < 0) 925 return err; 926 927 linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT, phydev->supported, 928 err & MDIO_PMA_NG_EXTABLE_2_5GBT); 929 } 930 931 if (phydev->drv->phy_id != ATH8031_PHY_ID) 932 return 0; 933 934 /* AR8031/AR8033 have different status registers 935 * for copper and fiber operation. However, the 936 * extended status register is the same for both 937 * operation modes. 938 * 939 * As a result of that, ESTATUS_1000_XFULL is set 940 * to 1 even when operating in copper TP mode. 941 * 942 * Remove this mode from the supported link modes 943 * when not operating in 1000BaseX mode. 944 */ 945 if (!priv->is_1000basex) 946 linkmode_clear_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT, 947 phydev->supported); 948 949 return 0; 950 } 951 952 static int at803x_smarteee_config(struct phy_device *phydev) 953 { 954 struct at803x_priv *priv = phydev->priv; 955 u16 mask = 0, val = 0; 956 int ret; 957 958 if (priv->flags & AT803X_DISABLE_SMARTEEE) 959 return phy_modify_mmd(phydev, MDIO_MMD_PCS, 960 AT803X_MMD3_SMARTEEE_CTL3, 961 AT803X_MMD3_SMARTEEE_CTL3_LPI_EN, 0); 962 963 if (priv->smarteee_lpi_tw_1g) { 964 mask |= 0xff00; 965 val |= priv->smarteee_lpi_tw_1g << 8; 966 } 967 if (priv->smarteee_lpi_tw_100m) { 968 mask |= 0x00ff; 969 val |= priv->smarteee_lpi_tw_100m; 970 } 971 if (!mask) 972 return 0; 973 974 ret = phy_modify_mmd(phydev, MDIO_MMD_PCS, AT803X_MMD3_SMARTEEE_CTL1, 975 mask, val); 976 if (ret) 977 return ret; 978 979 return phy_modify_mmd(phydev, MDIO_MMD_PCS, AT803X_MMD3_SMARTEEE_CTL3, 980 AT803X_MMD3_SMARTEEE_CTL3_LPI_EN, 981 AT803X_MMD3_SMARTEEE_CTL3_LPI_EN); 982 } 983 984 static int at803x_clk_out_config(struct phy_device *phydev) 985 { 986 struct at803x_priv *priv = phydev->priv; 987 988 if (!priv->clk_25m_mask) 989 return 0; 990 991 return phy_modify_mmd(phydev, MDIO_MMD_AN, AT803X_MMD7_CLK25M, 992 priv->clk_25m_mask, priv->clk_25m_reg); 993 } 994 995 static int at8031_pll_config(struct phy_device *phydev) 996 { 997 struct at803x_priv *priv = phydev->priv; 998 999 /* The default after hardware reset is PLL OFF. After a soft reset, the 1000 * values are retained. 1001 */ 1002 if (priv->flags & AT803X_KEEP_PLL_ENABLED) 1003 return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_1F, 1004 0, AT803X_DEBUG_PLL_ON); 1005 else 1006 return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_1F, 1007 AT803X_DEBUG_PLL_ON, 0); 1008 } 1009 1010 static int at803x_hibernation_mode_config(struct phy_device *phydev) 1011 { 1012 struct at803x_priv *priv = phydev->priv; 1013 1014 /* The default after hardware reset is hibernation mode enabled. After 1015 * software reset, the value is retained. 1016 */ 1017 if (!(priv->flags & AT803X_DISABLE_HIBERNATION_MODE)) 1018 return 0; 1019 1020 return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_HIB_CTRL, 1021 AT803X_DEBUG_HIB_CTRL_PS_HIB_EN, 0); 1022 } 1023 1024 static int at803x_config_init(struct phy_device *phydev) 1025 { 1026 struct at803x_priv *priv = phydev->priv; 1027 int ret; 1028 1029 if (phydev->drv->phy_id == ATH8031_PHY_ID) { 1030 /* Some bootloaders leave the fiber page selected. 1031 * Switch to the appropriate page (fiber or copper), as otherwise we 1032 * read the PHY capabilities from the wrong page. 1033 */ 1034 phy_lock_mdio_bus(phydev); 1035 ret = at803x_write_page(phydev, 1036 priv->is_fiber ? AT803X_PAGE_FIBER : 1037 AT803X_PAGE_COPPER); 1038 phy_unlock_mdio_bus(phydev); 1039 if (ret) 1040 return ret; 1041 1042 ret = at8031_pll_config(phydev); 1043 if (ret < 0) 1044 return ret; 1045 } 1046 1047 /* The RX and TX delay default is: 1048 * after HW reset: RX delay enabled and TX delay disabled 1049 * after SW reset: RX delay enabled, while TX delay retains the 1050 * value before reset. 1051 */ 1052 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID || 1053 phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID) 1054 ret = at803x_enable_rx_delay(phydev); 1055 else 1056 ret = at803x_disable_rx_delay(phydev); 1057 if (ret < 0) 1058 return ret; 1059 1060 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID || 1061 phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID) 1062 ret = at803x_enable_tx_delay(phydev); 1063 else 1064 ret = at803x_disable_tx_delay(phydev); 1065 if (ret < 0) 1066 return ret; 1067 1068 ret = at803x_smarteee_config(phydev); 1069 if (ret < 0) 1070 return ret; 1071 1072 ret = at803x_clk_out_config(phydev); 1073 if (ret < 0) 1074 return ret; 1075 1076 ret = at803x_hibernation_mode_config(phydev); 1077 if (ret < 0) 1078 return ret; 1079 1080 /* Ar803x extended next page bit is enabled by default. Cisco 1081 * multigig switches read this bit and attempt to negotiate 10Gbps 1082 * rates even if the next page bit is disabled. This is incorrect 1083 * behaviour but we still need to accommodate it. XNP is only needed 1084 * for 10Gbps support, so disable XNP. 1085 */ 1086 return phy_modify(phydev, MII_ADVERTISE, MDIO_AN_CTRL1_XNP, 0); 1087 } 1088 1089 static int at803x_ack_interrupt(struct phy_device *phydev) 1090 { 1091 int err; 1092 1093 err = phy_read(phydev, AT803X_INTR_STATUS); 1094 1095 return (err < 0) ? err : 0; 1096 } 1097 1098 static int at803x_config_intr(struct phy_device *phydev) 1099 { 1100 struct at803x_priv *priv = phydev->priv; 1101 int err; 1102 int value; 1103 1104 value = phy_read(phydev, AT803X_INTR_ENABLE); 1105 1106 if (phydev->interrupts == PHY_INTERRUPT_ENABLED) { 1107 /* Clear any pending interrupts */ 1108 err = at803x_ack_interrupt(phydev); 1109 if (err) 1110 return err; 1111 1112 value |= AT803X_INTR_ENABLE_AUTONEG_ERR; 1113 value |= AT803X_INTR_ENABLE_SPEED_CHANGED; 1114 value |= AT803X_INTR_ENABLE_DUPLEX_CHANGED; 1115 value |= AT803X_INTR_ENABLE_LINK_FAIL; 1116 value |= AT803X_INTR_ENABLE_LINK_SUCCESS; 1117 if (priv->is_fiber) { 1118 value |= AT803X_INTR_ENABLE_LINK_FAIL_BX; 1119 value |= AT803X_INTR_ENABLE_LINK_SUCCESS_BX; 1120 } 1121 1122 err = phy_write(phydev, AT803X_INTR_ENABLE, value); 1123 } else { 1124 err = phy_write(phydev, AT803X_INTR_ENABLE, 0); 1125 if (err) 1126 return err; 1127 1128 /* Clear any pending interrupts */ 1129 err = at803x_ack_interrupt(phydev); 1130 } 1131 1132 return err; 1133 } 1134 1135 static irqreturn_t at803x_handle_interrupt(struct phy_device *phydev) 1136 { 1137 int irq_status, int_enabled; 1138 1139 irq_status = phy_read(phydev, AT803X_INTR_STATUS); 1140 if (irq_status < 0) { 1141 phy_error(phydev); 1142 return IRQ_NONE; 1143 } 1144 1145 /* Read the current enabled interrupts */ 1146 int_enabled = phy_read(phydev, AT803X_INTR_ENABLE); 1147 if (int_enabled < 0) { 1148 phy_error(phydev); 1149 return IRQ_NONE; 1150 } 1151 1152 /* See if this was one of our enabled interrupts */ 1153 if (!(irq_status & int_enabled)) 1154 return IRQ_NONE; 1155 1156 phy_trigger_machine(phydev); 1157 1158 return IRQ_HANDLED; 1159 } 1160 1161 static void at803x_link_change_notify(struct phy_device *phydev) 1162 { 1163 /* 1164 * Conduct a hardware reset for AT8030 every time a link loss is 1165 * signalled. This is necessary to circumvent a hardware bug that 1166 * occurs when the cable is unplugged while TX packets are pending 1167 * in the FIFO. In such cases, the FIFO enters an error mode it 1168 * cannot recover from by software. 1169 */ 1170 if (phydev->state == PHY_NOLINK && phydev->mdio.reset_gpio) { 1171 struct at803x_context context; 1172 1173 at803x_context_save(phydev, &context); 1174 1175 phy_device_reset(phydev, 1); 1176 msleep(1); 1177 phy_device_reset(phydev, 0); 1178 msleep(1); 1179 1180 at803x_context_restore(phydev, &context); 1181 1182 phydev_dbg(phydev, "%s(): phy was reset\n", __func__); 1183 } 1184 } 1185 1186 static int at803x_read_specific_status(struct phy_device *phydev) 1187 { 1188 int ss; 1189 1190 /* Read the AT8035 PHY-Specific Status register, which indicates the 1191 * speed and duplex that the PHY is actually using, irrespective of 1192 * whether we are in autoneg mode or not. 1193 */ 1194 ss = phy_read(phydev, AT803X_SPECIFIC_STATUS); 1195 if (ss < 0) 1196 return ss; 1197 1198 if (ss & AT803X_SS_SPEED_DUPLEX_RESOLVED) { 1199 int sfc, speed; 1200 1201 sfc = phy_read(phydev, AT803X_SPECIFIC_FUNCTION_CONTROL); 1202 if (sfc < 0) 1203 return sfc; 1204 1205 /* qca8081 takes the different bits for speed value from at803x */ 1206 if (phydev->drv->phy_id == QCA8081_PHY_ID) 1207 speed = FIELD_GET(QCA808X_SS_SPEED_MASK, ss); 1208 else 1209 speed = FIELD_GET(AT803X_SS_SPEED_MASK, ss); 1210 1211 switch (speed) { 1212 case AT803X_SS_SPEED_10: 1213 phydev->speed = SPEED_10; 1214 break; 1215 case AT803X_SS_SPEED_100: 1216 phydev->speed = SPEED_100; 1217 break; 1218 case AT803X_SS_SPEED_1000: 1219 phydev->speed = SPEED_1000; 1220 break; 1221 case QCA808X_SS_SPEED_2500: 1222 phydev->speed = SPEED_2500; 1223 break; 1224 } 1225 if (ss & AT803X_SS_DUPLEX) 1226 phydev->duplex = DUPLEX_FULL; 1227 else 1228 phydev->duplex = DUPLEX_HALF; 1229 1230 if (ss & AT803X_SS_MDIX) 1231 phydev->mdix = ETH_TP_MDI_X; 1232 else 1233 phydev->mdix = ETH_TP_MDI; 1234 1235 switch (FIELD_GET(AT803X_SFC_MDI_CROSSOVER_MODE_M, sfc)) { 1236 case AT803X_SFC_MANUAL_MDI: 1237 phydev->mdix_ctrl = ETH_TP_MDI; 1238 break; 1239 case AT803X_SFC_MANUAL_MDIX: 1240 phydev->mdix_ctrl = ETH_TP_MDI_X; 1241 break; 1242 case AT803X_SFC_AUTOMATIC_CROSSOVER: 1243 phydev->mdix_ctrl = ETH_TP_MDI_AUTO; 1244 break; 1245 } 1246 } 1247 1248 return 0; 1249 } 1250 1251 static int at803x_read_status(struct phy_device *phydev) 1252 { 1253 struct at803x_priv *priv = phydev->priv; 1254 int err, old_link = phydev->link; 1255 1256 if (priv->is_1000basex) 1257 return genphy_c37_read_status(phydev); 1258 1259 /* Update the link, but return if there was an error */ 1260 err = genphy_update_link(phydev); 1261 if (err) 1262 return err; 1263 1264 /* why bother the PHY if nothing can have changed */ 1265 if (phydev->autoneg == AUTONEG_ENABLE && old_link && phydev->link) 1266 return 0; 1267 1268 phydev->speed = SPEED_UNKNOWN; 1269 phydev->duplex = DUPLEX_UNKNOWN; 1270 phydev->pause = 0; 1271 phydev->asym_pause = 0; 1272 1273 err = genphy_read_lpa(phydev); 1274 if (err < 0) 1275 return err; 1276 1277 err = at803x_read_specific_status(phydev); 1278 if (err < 0) 1279 return err; 1280 1281 if (phydev->autoneg == AUTONEG_ENABLE && phydev->autoneg_complete) 1282 phy_resolve_aneg_pause(phydev); 1283 1284 return 0; 1285 } 1286 1287 static int at803x_config_mdix(struct phy_device *phydev, u8 ctrl) 1288 { 1289 u16 val; 1290 1291 switch (ctrl) { 1292 case ETH_TP_MDI: 1293 val = AT803X_SFC_MANUAL_MDI; 1294 break; 1295 case ETH_TP_MDI_X: 1296 val = AT803X_SFC_MANUAL_MDIX; 1297 break; 1298 case ETH_TP_MDI_AUTO: 1299 val = AT803X_SFC_AUTOMATIC_CROSSOVER; 1300 break; 1301 default: 1302 return 0; 1303 } 1304 1305 return phy_modify_changed(phydev, AT803X_SPECIFIC_FUNCTION_CONTROL, 1306 AT803X_SFC_MDI_CROSSOVER_MODE_M, 1307 FIELD_PREP(AT803X_SFC_MDI_CROSSOVER_MODE_M, val)); 1308 } 1309 1310 static int at803x_config_aneg(struct phy_device *phydev) 1311 { 1312 struct at803x_priv *priv = phydev->priv; 1313 int ret; 1314 1315 ret = at803x_config_mdix(phydev, phydev->mdix_ctrl); 1316 if (ret < 0) 1317 return ret; 1318 1319 /* Changes of the midx bits are disruptive to the normal operation; 1320 * therefore any changes to these registers must be followed by a 1321 * software reset to take effect. 1322 */ 1323 if (ret == 1) { 1324 ret = genphy_soft_reset(phydev); 1325 if (ret < 0) 1326 return ret; 1327 } 1328 1329 if (priv->is_1000basex) 1330 return genphy_c37_config_aneg(phydev); 1331 1332 /* Do not restart auto-negotiation by setting ret to 0 defautly, 1333 * when calling __genphy_config_aneg later. 1334 */ 1335 ret = 0; 1336 1337 if (phydev->drv->phy_id == QCA8081_PHY_ID) { 1338 int phy_ctrl = 0; 1339 1340 /* The reg MII_BMCR also needs to be configured for force mode, the 1341 * genphy_config_aneg is also needed. 1342 */ 1343 if (phydev->autoneg == AUTONEG_DISABLE) 1344 genphy_c45_pma_setup_forced(phydev); 1345 1346 if (linkmode_test_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT, phydev->advertising)) 1347 phy_ctrl = MDIO_AN_10GBT_CTRL_ADV2_5G; 1348 1349 ret = phy_modify_mmd_changed(phydev, MDIO_MMD_AN, MDIO_AN_10GBT_CTRL, 1350 MDIO_AN_10GBT_CTRL_ADV2_5G, phy_ctrl); 1351 if (ret < 0) 1352 return ret; 1353 } 1354 1355 return __genphy_config_aneg(phydev, ret); 1356 } 1357 1358 static int at803x_get_downshift(struct phy_device *phydev, u8 *d) 1359 { 1360 int val; 1361 1362 val = phy_read(phydev, AT803X_SMART_SPEED); 1363 if (val < 0) 1364 return val; 1365 1366 if (val & AT803X_SMART_SPEED_ENABLE) 1367 *d = FIELD_GET(AT803X_SMART_SPEED_RETRY_LIMIT_MASK, val) + 2; 1368 else 1369 *d = DOWNSHIFT_DEV_DISABLE; 1370 1371 return 0; 1372 } 1373 1374 static int at803x_set_downshift(struct phy_device *phydev, u8 cnt) 1375 { 1376 u16 mask, set; 1377 int ret; 1378 1379 switch (cnt) { 1380 case DOWNSHIFT_DEV_DEFAULT_COUNT: 1381 cnt = AT803X_DEFAULT_DOWNSHIFT; 1382 fallthrough; 1383 case AT803X_MIN_DOWNSHIFT ... AT803X_MAX_DOWNSHIFT: 1384 set = AT803X_SMART_SPEED_ENABLE | 1385 AT803X_SMART_SPEED_BYPASS_TIMER | 1386 FIELD_PREP(AT803X_SMART_SPEED_RETRY_LIMIT_MASK, cnt - 2); 1387 mask = AT803X_SMART_SPEED_RETRY_LIMIT_MASK; 1388 break; 1389 case DOWNSHIFT_DEV_DISABLE: 1390 set = 0; 1391 mask = AT803X_SMART_SPEED_ENABLE | 1392 AT803X_SMART_SPEED_BYPASS_TIMER; 1393 break; 1394 default: 1395 return -EINVAL; 1396 } 1397 1398 ret = phy_modify_changed(phydev, AT803X_SMART_SPEED, mask, set); 1399 1400 /* After changing the smart speed settings, we need to perform a 1401 * software reset, use phy_init_hw() to make sure we set the 1402 * reapply any values which might got lost during software reset. 1403 */ 1404 if (ret == 1) 1405 ret = phy_init_hw(phydev); 1406 1407 return ret; 1408 } 1409 1410 static int at803x_get_tunable(struct phy_device *phydev, 1411 struct ethtool_tunable *tuna, void *data) 1412 { 1413 switch (tuna->id) { 1414 case ETHTOOL_PHY_DOWNSHIFT: 1415 return at803x_get_downshift(phydev, data); 1416 default: 1417 return -EOPNOTSUPP; 1418 } 1419 } 1420 1421 static int at803x_set_tunable(struct phy_device *phydev, 1422 struct ethtool_tunable *tuna, const void *data) 1423 { 1424 switch (tuna->id) { 1425 case ETHTOOL_PHY_DOWNSHIFT: 1426 return at803x_set_downshift(phydev, *(const u8 *)data); 1427 default: 1428 return -EOPNOTSUPP; 1429 } 1430 } 1431 1432 static int at803x_cable_test_result_trans(u16 status) 1433 { 1434 switch (FIELD_GET(AT803X_CDT_STATUS_STAT_MASK, status)) { 1435 case AT803X_CDT_STATUS_STAT_NORMAL: 1436 return ETHTOOL_A_CABLE_RESULT_CODE_OK; 1437 case AT803X_CDT_STATUS_STAT_SHORT: 1438 return ETHTOOL_A_CABLE_RESULT_CODE_SAME_SHORT; 1439 case AT803X_CDT_STATUS_STAT_OPEN: 1440 return ETHTOOL_A_CABLE_RESULT_CODE_OPEN; 1441 case AT803X_CDT_STATUS_STAT_FAIL: 1442 default: 1443 return ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC; 1444 } 1445 } 1446 1447 static bool at803x_cdt_test_failed(u16 status) 1448 { 1449 return FIELD_GET(AT803X_CDT_STATUS_STAT_MASK, status) == 1450 AT803X_CDT_STATUS_STAT_FAIL; 1451 } 1452 1453 static bool at803x_cdt_fault_length_valid(u16 status) 1454 { 1455 switch (FIELD_GET(AT803X_CDT_STATUS_STAT_MASK, status)) { 1456 case AT803X_CDT_STATUS_STAT_OPEN: 1457 case AT803X_CDT_STATUS_STAT_SHORT: 1458 return true; 1459 } 1460 return false; 1461 } 1462 1463 static int at803x_cdt_fault_length(u16 status) 1464 { 1465 int dt; 1466 1467 /* According to the datasheet the distance to the fault is 1468 * DELTA_TIME * 0.824 meters. 1469 * 1470 * The author suspect the correct formula is: 1471 * 1472 * fault_distance = DELTA_TIME * (c * VF) / 125MHz / 2 1473 * 1474 * where c is the speed of light, VF is the velocity factor of 1475 * the twisted pair cable, 125MHz the counter frequency and 1476 * we need to divide by 2 because the hardware will measure the 1477 * round trip time to the fault and back to the PHY. 1478 * 1479 * With a VF of 0.69 we get the factor 0.824 mentioned in the 1480 * datasheet. 1481 */ 1482 dt = FIELD_GET(AT803X_CDT_STATUS_DELTA_TIME_MASK, status); 1483 1484 return (dt * 824) / 10; 1485 } 1486 1487 static int at803x_cdt_start(struct phy_device *phydev, int pair) 1488 { 1489 u16 cdt; 1490 1491 /* qca8081 takes the different bit 15 to enable CDT test */ 1492 if (phydev->drv->phy_id == QCA8081_PHY_ID) 1493 cdt = QCA808X_CDT_ENABLE_TEST | 1494 QCA808X_CDT_LENGTH_UNIT | 1495 QCA808X_CDT_INTER_CHECK_DIS; 1496 else 1497 cdt = FIELD_PREP(AT803X_CDT_MDI_PAIR_MASK, pair) | 1498 AT803X_CDT_ENABLE_TEST; 1499 1500 return phy_write(phydev, AT803X_CDT, cdt); 1501 } 1502 1503 static int at803x_cdt_wait_for_completion(struct phy_device *phydev) 1504 { 1505 int val, ret; 1506 u16 cdt_en; 1507 1508 if (phydev->drv->phy_id == QCA8081_PHY_ID) 1509 cdt_en = QCA808X_CDT_ENABLE_TEST; 1510 else 1511 cdt_en = AT803X_CDT_ENABLE_TEST; 1512 1513 /* One test run takes about 25ms */ 1514 ret = phy_read_poll_timeout(phydev, AT803X_CDT, val, 1515 !(val & cdt_en), 1516 30000, 100000, true); 1517 1518 return ret < 0 ? ret : 0; 1519 } 1520 1521 static int at803x_cable_test_one_pair(struct phy_device *phydev, int pair) 1522 { 1523 static const int ethtool_pair[] = { 1524 ETHTOOL_A_CABLE_PAIR_A, 1525 ETHTOOL_A_CABLE_PAIR_B, 1526 ETHTOOL_A_CABLE_PAIR_C, 1527 ETHTOOL_A_CABLE_PAIR_D, 1528 }; 1529 int ret, val; 1530 1531 ret = at803x_cdt_start(phydev, pair); 1532 if (ret) 1533 return ret; 1534 1535 ret = at803x_cdt_wait_for_completion(phydev); 1536 if (ret) 1537 return ret; 1538 1539 val = phy_read(phydev, AT803X_CDT_STATUS); 1540 if (val < 0) 1541 return val; 1542 1543 if (at803x_cdt_test_failed(val)) 1544 return 0; 1545 1546 ethnl_cable_test_result(phydev, ethtool_pair[pair], 1547 at803x_cable_test_result_trans(val)); 1548 1549 if (at803x_cdt_fault_length_valid(val)) 1550 ethnl_cable_test_fault_length(phydev, ethtool_pair[pair], 1551 at803x_cdt_fault_length(val)); 1552 1553 return 1; 1554 } 1555 1556 static int at803x_cable_test_get_status(struct phy_device *phydev, 1557 bool *finished) 1558 { 1559 unsigned long pair_mask; 1560 int retries = 20; 1561 int pair, ret; 1562 1563 if (phydev->phy_id == ATH9331_PHY_ID || 1564 phydev->phy_id == ATH8032_PHY_ID || 1565 phydev->phy_id == QCA9561_PHY_ID) 1566 pair_mask = 0x3; 1567 else 1568 pair_mask = 0xf; 1569 1570 *finished = false; 1571 1572 /* According to the datasheet the CDT can be performed when 1573 * there is no link partner or when the link partner is 1574 * auto-negotiating. Starting the test will restart the AN 1575 * automatically. It seems that doing this repeatedly we will 1576 * get a slot where our link partner won't disturb our 1577 * measurement. 1578 */ 1579 while (pair_mask && retries--) { 1580 for_each_set_bit(pair, &pair_mask, 4) { 1581 ret = at803x_cable_test_one_pair(phydev, pair); 1582 if (ret < 0) 1583 return ret; 1584 if (ret) 1585 clear_bit(pair, &pair_mask); 1586 } 1587 if (pair_mask) 1588 msleep(250); 1589 } 1590 1591 *finished = true; 1592 1593 return 0; 1594 } 1595 1596 static int at803x_cable_test_start(struct phy_device *phydev) 1597 { 1598 /* Enable auto-negotiation, but advertise no capabilities, no link 1599 * will be established. A restart of the auto-negotiation is not 1600 * required, because the cable test will automatically break the link. 1601 */ 1602 phy_write(phydev, MII_BMCR, BMCR_ANENABLE); 1603 phy_write(phydev, MII_ADVERTISE, ADVERTISE_CSMA); 1604 if (phydev->phy_id != ATH9331_PHY_ID && 1605 phydev->phy_id != ATH8032_PHY_ID && 1606 phydev->phy_id != QCA9561_PHY_ID) 1607 phy_write(phydev, MII_CTRL1000, 0); 1608 1609 /* we do all the (time consuming) work later */ 1610 return 0; 1611 } 1612 1613 static int qca83xx_config_init(struct phy_device *phydev) 1614 { 1615 u8 switch_revision; 1616 1617 switch_revision = phydev->dev_flags & QCA8K_DEVFLAGS_REVISION_MASK; 1618 1619 switch (switch_revision) { 1620 case 1: 1621 /* For 100M waveform */ 1622 at803x_debug_reg_write(phydev, AT803X_DEBUG_ANALOG_TEST_CTRL, 0x02ea); 1623 /* Turn on Gigabit clock */ 1624 at803x_debug_reg_write(phydev, AT803X_DEBUG_REG_GREEN, 0x68a0); 1625 break; 1626 1627 case 2: 1628 phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV, 0x0); 1629 fallthrough; 1630 case 4: 1631 phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_AZ_DEBUG, 0x803f); 1632 at803x_debug_reg_write(phydev, AT803X_DEBUG_REG_GREEN, 0x6860); 1633 at803x_debug_reg_write(phydev, AT803X_DEBUG_SYSTEM_CTRL_MODE, 0x2c46); 1634 at803x_debug_reg_write(phydev, AT803X_DEBUG_REG_3C, 0x6000); 1635 break; 1636 } 1637 1638 /* QCA8327 require DAC amplitude adjustment for 100m set to +6%. 1639 * Disable on init and enable only with 100m speed following 1640 * qca original source code. 1641 */ 1642 if (phydev->drv->phy_id == QCA8327_A_PHY_ID || 1643 phydev->drv->phy_id == QCA8327_B_PHY_ID) 1644 at803x_debug_reg_mask(phydev, AT803X_DEBUG_ANALOG_TEST_CTRL, 1645 QCA8327_DEBUG_MANU_CTRL_EN, 0); 1646 1647 /* Following original QCA sourcecode set port to prefer master */ 1648 phy_set_bits(phydev, MII_CTRL1000, CTL1000_PREFER_MASTER); 1649 1650 return 0; 1651 } 1652 1653 static void qca83xx_link_change_notify(struct phy_device *phydev) 1654 { 1655 /* QCA8337 doesn't require DAC Amplitude adjustement */ 1656 if (phydev->drv->phy_id == QCA8337_PHY_ID) 1657 return; 1658 1659 /* Set DAC Amplitude adjustment to +6% for 100m on link running */ 1660 if (phydev->state == PHY_RUNNING) { 1661 if (phydev->speed == SPEED_100) 1662 at803x_debug_reg_mask(phydev, AT803X_DEBUG_ANALOG_TEST_CTRL, 1663 QCA8327_DEBUG_MANU_CTRL_EN, 1664 QCA8327_DEBUG_MANU_CTRL_EN); 1665 } else { 1666 /* Reset DAC Amplitude adjustment */ 1667 at803x_debug_reg_mask(phydev, AT803X_DEBUG_ANALOG_TEST_CTRL, 1668 QCA8327_DEBUG_MANU_CTRL_EN, 0); 1669 } 1670 } 1671 1672 static int qca83xx_resume(struct phy_device *phydev) 1673 { 1674 int ret, val; 1675 1676 /* Skip reset if not suspended */ 1677 if (!phydev->suspended) 1678 return 0; 1679 1680 /* Reinit the port, reset values set by suspend */ 1681 qca83xx_config_init(phydev); 1682 1683 /* Reset the port on port resume */ 1684 phy_set_bits(phydev, MII_BMCR, BMCR_RESET | BMCR_ANENABLE); 1685 1686 /* On resume from suspend the switch execute a reset and 1687 * restart auto-negotiation. Wait for reset to complete. 1688 */ 1689 ret = phy_read_poll_timeout(phydev, MII_BMCR, val, !(val & BMCR_RESET), 1690 50000, 600000, true); 1691 if (ret) 1692 return ret; 1693 1694 msleep(1); 1695 1696 return 0; 1697 } 1698 1699 static int qca83xx_suspend(struct phy_device *phydev) 1700 { 1701 u16 mask = 0; 1702 1703 /* Only QCA8337 support actual suspend. 1704 * QCA8327 cause port unreliability when phy suspend 1705 * is set. 1706 */ 1707 if (phydev->drv->phy_id == QCA8337_PHY_ID) { 1708 genphy_suspend(phydev); 1709 } else { 1710 mask |= ~(BMCR_SPEED1000 | BMCR_FULLDPLX); 1711 phy_modify(phydev, MII_BMCR, mask, 0); 1712 } 1713 1714 at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_GREEN, 1715 AT803X_DEBUG_GATE_CLK_IN1000, 0); 1716 1717 at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_HIB_CTRL, 1718 AT803X_DEBUG_HIB_CTRL_EN_ANY_CHANGE | 1719 AT803X_DEBUG_HIB_CTRL_SEL_RST_80U, 0); 1720 1721 return 0; 1722 } 1723 1724 static int qca808x_phy_fast_retrain_config(struct phy_device *phydev) 1725 { 1726 int ret; 1727 1728 /* Enable fast retrain */ 1729 ret = genphy_c45_fast_retrain(phydev, true); 1730 if (ret) 1731 return ret; 1732 1733 phy_write_mmd(phydev, MDIO_MMD_AN, QCA808X_PHY_MMD7_TOP_OPTION1, 1734 QCA808X_TOP_OPTION1_DATA); 1735 phy_write_mmd(phydev, MDIO_MMD_PMAPMD, QCA808X_PHY_MMD1_MSE_THRESHOLD_20DB, 1736 QCA808X_MSE_THRESHOLD_20DB_VALUE); 1737 phy_write_mmd(phydev, MDIO_MMD_PMAPMD, QCA808X_PHY_MMD1_MSE_THRESHOLD_17DB, 1738 QCA808X_MSE_THRESHOLD_17DB_VALUE); 1739 phy_write_mmd(phydev, MDIO_MMD_PMAPMD, QCA808X_PHY_MMD1_MSE_THRESHOLD_27DB, 1740 QCA808X_MSE_THRESHOLD_27DB_VALUE); 1741 phy_write_mmd(phydev, MDIO_MMD_PMAPMD, QCA808X_PHY_MMD1_MSE_THRESHOLD_28DB, 1742 QCA808X_MSE_THRESHOLD_28DB_VALUE); 1743 phy_write_mmd(phydev, MDIO_MMD_PCS, QCA808X_PHY_MMD3_DEBUG_1, 1744 QCA808X_MMD3_DEBUG_1_VALUE); 1745 phy_write_mmd(phydev, MDIO_MMD_PCS, QCA808X_PHY_MMD3_DEBUG_4, 1746 QCA808X_MMD3_DEBUG_4_VALUE); 1747 phy_write_mmd(phydev, MDIO_MMD_PCS, QCA808X_PHY_MMD3_DEBUG_5, 1748 QCA808X_MMD3_DEBUG_5_VALUE); 1749 phy_write_mmd(phydev, MDIO_MMD_PCS, QCA808X_PHY_MMD3_DEBUG_3, 1750 QCA808X_MMD3_DEBUG_3_VALUE); 1751 phy_write_mmd(phydev, MDIO_MMD_PCS, QCA808X_PHY_MMD3_DEBUG_6, 1752 QCA808X_MMD3_DEBUG_6_VALUE); 1753 phy_write_mmd(phydev, MDIO_MMD_PCS, QCA808X_PHY_MMD3_DEBUG_2, 1754 QCA808X_MMD3_DEBUG_2_VALUE); 1755 1756 return 0; 1757 } 1758 1759 static int qca808x_phy_ms_random_seed_set(struct phy_device *phydev) 1760 { 1761 u16 seed_value = prandom_u32_max(QCA808X_MASTER_SLAVE_SEED_RANGE); 1762 1763 return at803x_debug_reg_mask(phydev, QCA808X_PHY_DEBUG_LOCAL_SEED, 1764 QCA808X_MASTER_SLAVE_SEED_CFG, 1765 FIELD_PREP(QCA808X_MASTER_SLAVE_SEED_CFG, seed_value)); 1766 } 1767 1768 static int qca808x_phy_ms_seed_enable(struct phy_device *phydev, bool enable) 1769 { 1770 u16 seed_enable = 0; 1771 1772 if (enable) 1773 seed_enable = QCA808X_MASTER_SLAVE_SEED_ENABLE; 1774 1775 return at803x_debug_reg_mask(phydev, QCA808X_PHY_DEBUG_LOCAL_SEED, 1776 QCA808X_MASTER_SLAVE_SEED_ENABLE, seed_enable); 1777 } 1778 1779 static int qca808x_config_init(struct phy_device *phydev) 1780 { 1781 int ret; 1782 1783 /* Active adc&vga on 802.3az for the link 1000M and 100M */ 1784 ret = phy_modify_mmd(phydev, MDIO_MMD_PCS, QCA808X_PHY_MMD3_ADDR_CLD_CTRL7, 1785 QCA808X_8023AZ_AFE_CTRL_MASK, QCA808X_8023AZ_AFE_EN); 1786 if (ret) 1787 return ret; 1788 1789 /* Adjust the threshold on 802.3az for the link 1000M */ 1790 ret = phy_write_mmd(phydev, MDIO_MMD_PCS, 1791 QCA808X_PHY_MMD3_AZ_TRAINING_CTRL, QCA808X_MMD3_AZ_TRAINING_VAL); 1792 if (ret) 1793 return ret; 1794 1795 /* Config the fast retrain for the link 2500M */ 1796 ret = qca808x_phy_fast_retrain_config(phydev); 1797 if (ret) 1798 return ret; 1799 1800 /* Configure lower ramdom seed to make phy linked as slave mode */ 1801 ret = qca808x_phy_ms_random_seed_set(phydev); 1802 if (ret) 1803 return ret; 1804 1805 /* Enable seed */ 1806 ret = qca808x_phy_ms_seed_enable(phydev, true); 1807 if (ret) 1808 return ret; 1809 1810 /* Configure adc threshold as 100mv for the link 10M */ 1811 return at803x_debug_reg_mask(phydev, QCA808X_PHY_DEBUG_ADC_THRESHOLD, 1812 QCA808X_ADC_THRESHOLD_MASK, QCA808X_ADC_THRESHOLD_100MV); 1813 } 1814 1815 static int qca808x_read_status(struct phy_device *phydev) 1816 { 1817 int ret; 1818 1819 ret = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_10GBT_STAT); 1820 if (ret < 0) 1821 return ret; 1822 1823 linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT, phydev->lp_advertising, 1824 ret & MDIO_AN_10GBT_STAT_LP2_5G); 1825 1826 ret = genphy_read_status(phydev); 1827 if (ret) 1828 return ret; 1829 1830 ret = at803x_read_specific_status(phydev); 1831 if (ret < 0) 1832 return ret; 1833 1834 if (phydev->link) { 1835 if (phydev->speed == SPEED_2500) 1836 phydev->interface = PHY_INTERFACE_MODE_2500BASEX; 1837 else 1838 phydev->interface = PHY_INTERFACE_MODE_SGMII; 1839 } else { 1840 /* generate seed as a lower random value to make PHY linked as SLAVE easily, 1841 * except for master/slave configuration fault detected. 1842 * the reason for not putting this code into the function link_change_notify is 1843 * the corner case where the link partner is also the qca8081 PHY and the seed 1844 * value is configured as the same value, the link can't be up and no link change 1845 * occurs. 1846 */ 1847 if (phydev->master_slave_state == MASTER_SLAVE_STATE_ERR) { 1848 qca808x_phy_ms_seed_enable(phydev, false); 1849 } else { 1850 qca808x_phy_ms_random_seed_set(phydev); 1851 qca808x_phy_ms_seed_enable(phydev, true); 1852 } 1853 } 1854 1855 return 0; 1856 } 1857 1858 static int qca808x_soft_reset(struct phy_device *phydev) 1859 { 1860 int ret; 1861 1862 ret = genphy_soft_reset(phydev); 1863 if (ret < 0) 1864 return ret; 1865 1866 return qca808x_phy_ms_seed_enable(phydev, true); 1867 } 1868 1869 static bool qca808x_cdt_fault_length_valid(int cdt_code) 1870 { 1871 switch (cdt_code) { 1872 case QCA808X_CDT_STATUS_STAT_SHORT: 1873 case QCA808X_CDT_STATUS_STAT_OPEN: 1874 return true; 1875 default: 1876 return false; 1877 } 1878 } 1879 1880 static int qca808x_cable_test_result_trans(int cdt_code) 1881 { 1882 switch (cdt_code) { 1883 case QCA808X_CDT_STATUS_STAT_NORMAL: 1884 return ETHTOOL_A_CABLE_RESULT_CODE_OK; 1885 case QCA808X_CDT_STATUS_STAT_SHORT: 1886 return ETHTOOL_A_CABLE_RESULT_CODE_SAME_SHORT; 1887 case QCA808X_CDT_STATUS_STAT_OPEN: 1888 return ETHTOOL_A_CABLE_RESULT_CODE_OPEN; 1889 case QCA808X_CDT_STATUS_STAT_FAIL: 1890 default: 1891 return ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC; 1892 } 1893 } 1894 1895 static int qca808x_cdt_fault_length(struct phy_device *phydev, int pair) 1896 { 1897 int val; 1898 u32 cdt_length_reg = 0; 1899 1900 switch (pair) { 1901 case ETHTOOL_A_CABLE_PAIR_A: 1902 cdt_length_reg = QCA808X_MMD3_CDT_DIAG_PAIR_A; 1903 break; 1904 case ETHTOOL_A_CABLE_PAIR_B: 1905 cdt_length_reg = QCA808X_MMD3_CDT_DIAG_PAIR_B; 1906 break; 1907 case ETHTOOL_A_CABLE_PAIR_C: 1908 cdt_length_reg = QCA808X_MMD3_CDT_DIAG_PAIR_C; 1909 break; 1910 case ETHTOOL_A_CABLE_PAIR_D: 1911 cdt_length_reg = QCA808X_MMD3_CDT_DIAG_PAIR_D; 1912 break; 1913 default: 1914 return -EINVAL; 1915 } 1916 1917 val = phy_read_mmd(phydev, MDIO_MMD_PCS, cdt_length_reg); 1918 if (val < 0) 1919 return val; 1920 1921 return (FIELD_GET(QCA808X_CDT_DIAG_LENGTH, val) * 824) / 10; 1922 } 1923 1924 static int qca808x_cable_test_start(struct phy_device *phydev) 1925 { 1926 int ret; 1927 1928 /* perform CDT with the following configs: 1929 * 1. disable hibernation. 1930 * 2. force PHY working in MDI mode. 1931 * 3. for PHY working in 1000BaseT. 1932 * 4. configure the threshold. 1933 */ 1934 1935 ret = at803x_debug_reg_mask(phydev, QCA808X_DBG_AN_TEST, QCA808X_HIBERNATION_EN, 0); 1936 if (ret < 0) 1937 return ret; 1938 1939 ret = at803x_config_mdix(phydev, ETH_TP_MDI); 1940 if (ret < 0) 1941 return ret; 1942 1943 /* Force 1000base-T needs to configure PMA/PMD and MII_BMCR */ 1944 phydev->duplex = DUPLEX_FULL; 1945 phydev->speed = SPEED_1000; 1946 ret = genphy_c45_pma_setup_forced(phydev); 1947 if (ret < 0) 1948 return ret; 1949 1950 ret = genphy_setup_forced(phydev); 1951 if (ret < 0) 1952 return ret; 1953 1954 /* configure the thresholds for open, short, pair ok test */ 1955 phy_write_mmd(phydev, MDIO_MMD_PCS, 0x8074, 0xc040); 1956 phy_write_mmd(phydev, MDIO_MMD_PCS, 0x8076, 0xc040); 1957 phy_write_mmd(phydev, MDIO_MMD_PCS, 0x8077, 0xa060); 1958 phy_write_mmd(phydev, MDIO_MMD_PCS, 0x8078, 0xc050); 1959 phy_write_mmd(phydev, MDIO_MMD_PCS, 0x807a, 0xc060); 1960 phy_write_mmd(phydev, MDIO_MMD_PCS, 0x807e, 0xb060); 1961 1962 return 0; 1963 } 1964 1965 static int qca808x_cable_test_get_status(struct phy_device *phydev, bool *finished) 1966 { 1967 int ret, val; 1968 int pair_a, pair_b, pair_c, pair_d; 1969 1970 *finished = false; 1971 1972 ret = at803x_cdt_start(phydev, 0); 1973 if (ret) 1974 return ret; 1975 1976 ret = at803x_cdt_wait_for_completion(phydev); 1977 if (ret) 1978 return ret; 1979 1980 val = phy_read_mmd(phydev, MDIO_MMD_PCS, QCA808X_MMD3_CDT_STATUS); 1981 if (val < 0) 1982 return val; 1983 1984 pair_a = FIELD_GET(QCA808X_CDT_CODE_PAIR_A, val); 1985 pair_b = FIELD_GET(QCA808X_CDT_CODE_PAIR_B, val); 1986 pair_c = FIELD_GET(QCA808X_CDT_CODE_PAIR_C, val); 1987 pair_d = FIELD_GET(QCA808X_CDT_CODE_PAIR_D, val); 1988 1989 ethnl_cable_test_result(phydev, ETHTOOL_A_CABLE_PAIR_A, 1990 qca808x_cable_test_result_trans(pair_a)); 1991 ethnl_cable_test_result(phydev, ETHTOOL_A_CABLE_PAIR_B, 1992 qca808x_cable_test_result_trans(pair_b)); 1993 ethnl_cable_test_result(phydev, ETHTOOL_A_CABLE_PAIR_C, 1994 qca808x_cable_test_result_trans(pair_c)); 1995 ethnl_cable_test_result(phydev, ETHTOOL_A_CABLE_PAIR_D, 1996 qca808x_cable_test_result_trans(pair_d)); 1997 1998 if (qca808x_cdt_fault_length_valid(pair_a)) 1999 ethnl_cable_test_fault_length(phydev, ETHTOOL_A_CABLE_PAIR_A, 2000 qca808x_cdt_fault_length(phydev, ETHTOOL_A_CABLE_PAIR_A)); 2001 if (qca808x_cdt_fault_length_valid(pair_b)) 2002 ethnl_cable_test_fault_length(phydev, ETHTOOL_A_CABLE_PAIR_B, 2003 qca808x_cdt_fault_length(phydev, ETHTOOL_A_CABLE_PAIR_B)); 2004 if (qca808x_cdt_fault_length_valid(pair_c)) 2005 ethnl_cable_test_fault_length(phydev, ETHTOOL_A_CABLE_PAIR_C, 2006 qca808x_cdt_fault_length(phydev, ETHTOOL_A_CABLE_PAIR_C)); 2007 if (qca808x_cdt_fault_length_valid(pair_d)) 2008 ethnl_cable_test_fault_length(phydev, ETHTOOL_A_CABLE_PAIR_D, 2009 qca808x_cdt_fault_length(phydev, ETHTOOL_A_CABLE_PAIR_D)); 2010 2011 *finished = true; 2012 2013 return 0; 2014 } 2015 2016 static struct phy_driver at803x_driver[] = { 2017 { 2018 /* Qualcomm Atheros AR8035 */ 2019 PHY_ID_MATCH_EXACT(ATH8035_PHY_ID), 2020 .name = "Qualcomm Atheros AR8035", 2021 .flags = PHY_POLL_CABLE_TEST, 2022 .probe = at803x_probe, 2023 .remove = at803x_remove, 2024 .config_aneg = at803x_config_aneg, 2025 .config_init = at803x_config_init, 2026 .soft_reset = genphy_soft_reset, 2027 .set_wol = at803x_set_wol, 2028 .get_wol = at803x_get_wol, 2029 .suspend = at803x_suspend, 2030 .resume = at803x_resume, 2031 /* PHY_GBIT_FEATURES */ 2032 .read_status = at803x_read_status, 2033 .config_intr = at803x_config_intr, 2034 .handle_interrupt = at803x_handle_interrupt, 2035 .get_tunable = at803x_get_tunable, 2036 .set_tunable = at803x_set_tunable, 2037 .cable_test_start = at803x_cable_test_start, 2038 .cable_test_get_status = at803x_cable_test_get_status, 2039 }, { 2040 /* Qualcomm Atheros AR8030 */ 2041 .phy_id = ATH8030_PHY_ID, 2042 .name = "Qualcomm Atheros AR8030", 2043 .phy_id_mask = AT8030_PHY_ID_MASK, 2044 .probe = at803x_probe, 2045 .remove = at803x_remove, 2046 .config_init = at803x_config_init, 2047 .link_change_notify = at803x_link_change_notify, 2048 .set_wol = at803x_set_wol, 2049 .get_wol = at803x_get_wol, 2050 .suspend = at803x_suspend, 2051 .resume = at803x_resume, 2052 /* PHY_BASIC_FEATURES */ 2053 .config_intr = at803x_config_intr, 2054 .handle_interrupt = at803x_handle_interrupt, 2055 }, { 2056 /* Qualcomm Atheros AR8031/AR8033 */ 2057 PHY_ID_MATCH_EXACT(ATH8031_PHY_ID), 2058 .name = "Qualcomm Atheros AR8031/AR8033", 2059 .flags = PHY_POLL_CABLE_TEST, 2060 .probe = at803x_probe, 2061 .remove = at803x_remove, 2062 .config_init = at803x_config_init, 2063 .config_aneg = at803x_config_aneg, 2064 .soft_reset = genphy_soft_reset, 2065 .set_wol = at803x_set_wol, 2066 .get_wol = at803x_get_wol, 2067 .suspend = at803x_suspend, 2068 .resume = at803x_resume, 2069 .read_page = at803x_read_page, 2070 .write_page = at803x_write_page, 2071 .get_features = at803x_get_features, 2072 .read_status = at803x_read_status, 2073 .config_intr = &at803x_config_intr, 2074 .handle_interrupt = at803x_handle_interrupt, 2075 .get_tunable = at803x_get_tunable, 2076 .set_tunable = at803x_set_tunable, 2077 .cable_test_start = at803x_cable_test_start, 2078 .cable_test_get_status = at803x_cable_test_get_status, 2079 }, { 2080 /* Qualcomm Atheros AR8032 */ 2081 PHY_ID_MATCH_EXACT(ATH8032_PHY_ID), 2082 .name = "Qualcomm Atheros AR8032", 2083 .probe = at803x_probe, 2084 .remove = at803x_remove, 2085 .flags = PHY_POLL_CABLE_TEST, 2086 .config_init = at803x_config_init, 2087 .link_change_notify = at803x_link_change_notify, 2088 .set_wol = at803x_set_wol, 2089 .get_wol = at803x_get_wol, 2090 .suspend = at803x_suspend, 2091 .resume = at803x_resume, 2092 /* PHY_BASIC_FEATURES */ 2093 .config_intr = at803x_config_intr, 2094 .handle_interrupt = at803x_handle_interrupt, 2095 .cable_test_start = at803x_cable_test_start, 2096 .cable_test_get_status = at803x_cable_test_get_status, 2097 }, { 2098 /* ATHEROS AR9331 */ 2099 PHY_ID_MATCH_EXACT(ATH9331_PHY_ID), 2100 .name = "Qualcomm Atheros AR9331 built-in PHY", 2101 .probe = at803x_probe, 2102 .remove = at803x_remove, 2103 .suspend = at803x_suspend, 2104 .resume = at803x_resume, 2105 .flags = PHY_POLL_CABLE_TEST, 2106 /* PHY_BASIC_FEATURES */ 2107 .config_intr = &at803x_config_intr, 2108 .handle_interrupt = at803x_handle_interrupt, 2109 .cable_test_start = at803x_cable_test_start, 2110 .cable_test_get_status = at803x_cable_test_get_status, 2111 .read_status = at803x_read_status, 2112 .soft_reset = genphy_soft_reset, 2113 .config_aneg = at803x_config_aneg, 2114 }, { 2115 /* Qualcomm Atheros QCA9561 */ 2116 PHY_ID_MATCH_EXACT(QCA9561_PHY_ID), 2117 .name = "Qualcomm Atheros QCA9561 built-in PHY", 2118 .probe = at803x_probe, 2119 .remove = at803x_remove, 2120 .suspend = at803x_suspend, 2121 .resume = at803x_resume, 2122 .flags = PHY_POLL_CABLE_TEST, 2123 /* PHY_BASIC_FEATURES */ 2124 .config_intr = &at803x_config_intr, 2125 .handle_interrupt = at803x_handle_interrupt, 2126 .cable_test_start = at803x_cable_test_start, 2127 .cable_test_get_status = at803x_cable_test_get_status, 2128 .read_status = at803x_read_status, 2129 .soft_reset = genphy_soft_reset, 2130 .config_aneg = at803x_config_aneg, 2131 }, { 2132 /* QCA8337 */ 2133 .phy_id = QCA8337_PHY_ID, 2134 .phy_id_mask = QCA8K_PHY_ID_MASK, 2135 .name = "Qualcomm Atheros 8337 internal PHY", 2136 /* PHY_GBIT_FEATURES */ 2137 .link_change_notify = qca83xx_link_change_notify, 2138 .probe = at803x_probe, 2139 .flags = PHY_IS_INTERNAL, 2140 .config_init = qca83xx_config_init, 2141 .soft_reset = genphy_soft_reset, 2142 .get_sset_count = at803x_get_sset_count, 2143 .get_strings = at803x_get_strings, 2144 .get_stats = at803x_get_stats, 2145 .suspend = qca83xx_suspend, 2146 .resume = qca83xx_resume, 2147 }, { 2148 /* QCA8327-A from switch QCA8327-AL1A */ 2149 .phy_id = QCA8327_A_PHY_ID, 2150 .phy_id_mask = QCA8K_PHY_ID_MASK, 2151 .name = "Qualcomm Atheros 8327-A internal PHY", 2152 /* PHY_GBIT_FEATURES */ 2153 .link_change_notify = qca83xx_link_change_notify, 2154 .probe = at803x_probe, 2155 .flags = PHY_IS_INTERNAL, 2156 .config_init = qca83xx_config_init, 2157 .soft_reset = genphy_soft_reset, 2158 .get_sset_count = at803x_get_sset_count, 2159 .get_strings = at803x_get_strings, 2160 .get_stats = at803x_get_stats, 2161 .suspend = qca83xx_suspend, 2162 .resume = qca83xx_resume, 2163 }, { 2164 /* QCA8327-B from switch QCA8327-BL1A */ 2165 .phy_id = QCA8327_B_PHY_ID, 2166 .phy_id_mask = QCA8K_PHY_ID_MASK, 2167 .name = "Qualcomm Atheros 8327-B internal PHY", 2168 /* PHY_GBIT_FEATURES */ 2169 .link_change_notify = qca83xx_link_change_notify, 2170 .probe = at803x_probe, 2171 .flags = PHY_IS_INTERNAL, 2172 .config_init = qca83xx_config_init, 2173 .soft_reset = genphy_soft_reset, 2174 .get_sset_count = at803x_get_sset_count, 2175 .get_strings = at803x_get_strings, 2176 .get_stats = at803x_get_stats, 2177 .suspend = qca83xx_suspend, 2178 .resume = qca83xx_resume, 2179 }, { 2180 /* Qualcomm QCA8081 */ 2181 PHY_ID_MATCH_EXACT(QCA8081_PHY_ID), 2182 .name = "Qualcomm QCA8081", 2183 .flags = PHY_POLL_CABLE_TEST, 2184 .probe = at803x_probe, 2185 .remove = at803x_remove, 2186 .config_intr = at803x_config_intr, 2187 .handle_interrupt = at803x_handle_interrupt, 2188 .get_tunable = at803x_get_tunable, 2189 .set_tunable = at803x_set_tunable, 2190 .set_wol = at803x_set_wol, 2191 .get_wol = at803x_get_wol, 2192 .get_features = at803x_get_features, 2193 .config_aneg = at803x_config_aneg, 2194 .suspend = genphy_suspend, 2195 .resume = genphy_resume, 2196 .read_status = qca808x_read_status, 2197 .config_init = qca808x_config_init, 2198 .soft_reset = qca808x_soft_reset, 2199 .cable_test_start = qca808x_cable_test_start, 2200 .cable_test_get_status = qca808x_cable_test_get_status, 2201 }, }; 2202 2203 module_phy_driver(at803x_driver); 2204 2205 static struct mdio_device_id __maybe_unused atheros_tbl[] = { 2206 { ATH8030_PHY_ID, AT8030_PHY_ID_MASK }, 2207 { PHY_ID_MATCH_EXACT(ATH8031_PHY_ID) }, 2208 { PHY_ID_MATCH_EXACT(ATH8032_PHY_ID) }, 2209 { PHY_ID_MATCH_EXACT(ATH8035_PHY_ID) }, 2210 { PHY_ID_MATCH_EXACT(ATH9331_PHY_ID) }, 2211 { PHY_ID_MATCH_EXACT(QCA8337_PHY_ID) }, 2212 { PHY_ID_MATCH_EXACT(QCA8327_A_PHY_ID) }, 2213 { PHY_ID_MATCH_EXACT(QCA8327_B_PHY_ID) }, 2214 { PHY_ID_MATCH_EXACT(QCA9561_PHY_ID) }, 2215 { PHY_ID_MATCH_EXACT(QCA8081_PHY_ID) }, 2216 { } 2217 }; 2218 2219 MODULE_DEVICE_TABLE(mdio, atheros_tbl); 2220