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