1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * drivers/net/phy/micrel.c 4 * 5 * Driver for Micrel PHYs 6 * 7 * Author: David J. Choi 8 * 9 * Copyright (c) 2010-2013 Micrel, Inc. 10 * Copyright (c) 2014 Johan Hovold <johan@kernel.org> 11 * 12 * Support : Micrel Phys: 13 * Giga phys: ksz9021, ksz9031, ksz9131 14 * 100/10 Phys : ksz8001, ksz8721, ksz8737, ksz8041 15 * ksz8021, ksz8031, ksz8051, 16 * ksz8081, ksz8091, 17 * ksz8061, 18 * Switch : ksz8873, ksz886x 19 * ksz9477 20 */ 21 22 #include <linux/bitfield.h> 23 #include <linux/ethtool_netlink.h> 24 #include <linux/kernel.h> 25 #include <linux/module.h> 26 #include <linux/phy.h> 27 #include <linux/micrel_phy.h> 28 #include <linux/of.h> 29 #include <linux/clk.h> 30 #include <linux/delay.h> 31 #include <linux/ptp_clock_kernel.h> 32 #include <linux/ptp_clock.h> 33 #include <linux/ptp_classify.h> 34 #include <linux/net_tstamp.h> 35 #include <linux/gpio/consumer.h> 36 37 /* Operation Mode Strap Override */ 38 #define MII_KSZPHY_OMSO 0x16 39 #define KSZPHY_OMSO_FACTORY_TEST BIT(15) 40 #define KSZPHY_OMSO_B_CAST_OFF BIT(9) 41 #define KSZPHY_OMSO_NAND_TREE_ON BIT(5) 42 #define KSZPHY_OMSO_RMII_OVERRIDE BIT(1) 43 #define KSZPHY_OMSO_MII_OVERRIDE BIT(0) 44 45 /* general Interrupt control/status reg in vendor specific block. */ 46 #define MII_KSZPHY_INTCS 0x1B 47 #define KSZPHY_INTCS_JABBER BIT(15) 48 #define KSZPHY_INTCS_RECEIVE_ERR BIT(14) 49 #define KSZPHY_INTCS_PAGE_RECEIVE BIT(13) 50 #define KSZPHY_INTCS_PARELLEL BIT(12) 51 #define KSZPHY_INTCS_LINK_PARTNER_ACK BIT(11) 52 #define KSZPHY_INTCS_LINK_DOWN BIT(10) 53 #define KSZPHY_INTCS_REMOTE_FAULT BIT(9) 54 #define KSZPHY_INTCS_LINK_UP BIT(8) 55 #define KSZPHY_INTCS_ALL (KSZPHY_INTCS_LINK_UP |\ 56 KSZPHY_INTCS_LINK_DOWN) 57 #define KSZPHY_INTCS_LINK_DOWN_STATUS BIT(2) 58 #define KSZPHY_INTCS_LINK_UP_STATUS BIT(0) 59 #define KSZPHY_INTCS_STATUS (KSZPHY_INTCS_LINK_DOWN_STATUS |\ 60 KSZPHY_INTCS_LINK_UP_STATUS) 61 62 /* LinkMD Control/Status */ 63 #define KSZ8081_LMD 0x1d 64 #define KSZ8081_LMD_ENABLE_TEST BIT(15) 65 #define KSZ8081_LMD_STAT_NORMAL 0 66 #define KSZ8081_LMD_STAT_OPEN 1 67 #define KSZ8081_LMD_STAT_SHORT 2 68 #define KSZ8081_LMD_STAT_FAIL 3 69 #define KSZ8081_LMD_STAT_MASK GENMASK(14, 13) 70 /* Short cable (<10 meter) has been detected by LinkMD */ 71 #define KSZ8081_LMD_SHORT_INDICATOR BIT(12) 72 #define KSZ8081_LMD_DELTA_TIME_MASK GENMASK(8, 0) 73 74 #define KSZ9x31_LMD 0x12 75 #define KSZ9x31_LMD_VCT_EN BIT(15) 76 #define KSZ9x31_LMD_VCT_DIS_TX BIT(14) 77 #define KSZ9x31_LMD_VCT_PAIR(n) (((n) & 0x3) << 12) 78 #define KSZ9x31_LMD_VCT_SEL_RESULT 0 79 #define KSZ9x31_LMD_VCT_SEL_THRES_HI BIT(10) 80 #define KSZ9x31_LMD_VCT_SEL_THRES_LO BIT(11) 81 #define KSZ9x31_LMD_VCT_SEL_MASK GENMASK(11, 10) 82 #define KSZ9x31_LMD_VCT_ST_NORMAL 0 83 #define KSZ9x31_LMD_VCT_ST_OPEN 1 84 #define KSZ9x31_LMD_VCT_ST_SHORT 2 85 #define KSZ9x31_LMD_VCT_ST_FAIL 3 86 #define KSZ9x31_LMD_VCT_ST_MASK GENMASK(9, 8) 87 #define KSZ9x31_LMD_VCT_DATA_REFLECTED_INVALID BIT(7) 88 #define KSZ9x31_LMD_VCT_DATA_SIG_WAIT_TOO_LONG BIT(6) 89 #define KSZ9x31_LMD_VCT_DATA_MASK100 BIT(5) 90 #define KSZ9x31_LMD_VCT_DATA_NLP_FLP BIT(4) 91 #define KSZ9x31_LMD_VCT_DATA_LO_PULSE_MASK GENMASK(3, 2) 92 #define KSZ9x31_LMD_VCT_DATA_HI_PULSE_MASK GENMASK(1, 0) 93 #define KSZ9x31_LMD_VCT_DATA_MASK GENMASK(7, 0) 94 95 #define KSZPHY_WIRE_PAIR_MASK 0x3 96 97 #define LAN8814_CABLE_DIAG 0x12 98 #define LAN8814_CABLE_DIAG_STAT_MASK GENMASK(9, 8) 99 #define LAN8814_CABLE_DIAG_VCT_DATA_MASK GENMASK(7, 0) 100 #define LAN8814_PAIR_BIT_SHIFT 12 101 102 #define LAN8814_WIRE_PAIR_MASK 0xF 103 104 /* Lan8814 general Interrupt control/status reg in GPHY specific block. */ 105 #define LAN8814_INTC 0x18 106 #define LAN8814_INTS 0x1B 107 108 #define LAN8814_INT_LINK_DOWN BIT(2) 109 #define LAN8814_INT_LINK_UP BIT(0) 110 #define LAN8814_INT_LINK (LAN8814_INT_LINK_UP |\ 111 LAN8814_INT_LINK_DOWN) 112 113 #define LAN8814_INTR_CTRL_REG 0x34 114 #define LAN8814_INTR_CTRL_REG_POLARITY BIT(1) 115 #define LAN8814_INTR_CTRL_REG_INTR_ENABLE BIT(0) 116 117 /* Represents 1ppm adjustment in 2^32 format with 118 * each nsec contains 4 clock cycles. 119 * The value is calculated as following: (1/1000000)/((2^-32)/4) 120 */ 121 #define LAN8814_1PPM_FORMAT 17179 122 123 #define PTP_RX_MOD 0x024F 124 #define PTP_RX_MOD_BAD_UDPV4_CHKSUM_FORCE_FCS_DIS_ BIT(3) 125 #define PTP_RX_TIMESTAMP_EN 0x024D 126 #define PTP_TX_TIMESTAMP_EN 0x028D 127 128 #define PTP_TIMESTAMP_EN_SYNC_ BIT(0) 129 #define PTP_TIMESTAMP_EN_DREQ_ BIT(1) 130 #define PTP_TIMESTAMP_EN_PDREQ_ BIT(2) 131 #define PTP_TIMESTAMP_EN_PDRES_ BIT(3) 132 133 #define PTP_TX_PARSE_L2_ADDR_EN 0x0284 134 #define PTP_RX_PARSE_L2_ADDR_EN 0x0244 135 136 #define PTP_TX_PARSE_IP_ADDR_EN 0x0285 137 #define PTP_RX_PARSE_IP_ADDR_EN 0x0245 138 #define LTC_HARD_RESET 0x023F 139 #define LTC_HARD_RESET_ BIT(0) 140 141 #define TSU_HARD_RESET 0x02C1 142 #define TSU_HARD_RESET_ BIT(0) 143 144 #define PTP_CMD_CTL 0x0200 145 #define PTP_CMD_CTL_PTP_DISABLE_ BIT(0) 146 #define PTP_CMD_CTL_PTP_ENABLE_ BIT(1) 147 #define PTP_CMD_CTL_PTP_CLOCK_READ_ BIT(3) 148 #define PTP_CMD_CTL_PTP_CLOCK_LOAD_ BIT(4) 149 #define PTP_CMD_CTL_PTP_LTC_STEP_SEC_ BIT(5) 150 #define PTP_CMD_CTL_PTP_LTC_STEP_NSEC_ BIT(6) 151 152 #define PTP_CLOCK_SET_SEC_MID 0x0206 153 #define PTP_CLOCK_SET_SEC_LO 0x0207 154 #define PTP_CLOCK_SET_NS_HI 0x0208 155 #define PTP_CLOCK_SET_NS_LO 0x0209 156 157 #define PTP_CLOCK_READ_SEC_MID 0x022A 158 #define PTP_CLOCK_READ_SEC_LO 0x022B 159 #define PTP_CLOCK_READ_NS_HI 0x022C 160 #define PTP_CLOCK_READ_NS_LO 0x022D 161 162 #define PTP_OPERATING_MODE 0x0241 163 #define PTP_OPERATING_MODE_STANDALONE_ BIT(0) 164 165 #define PTP_TX_MOD 0x028F 166 #define PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_ BIT(12) 167 #define PTP_TX_MOD_BAD_UDPV4_CHKSUM_FORCE_FCS_DIS_ BIT(3) 168 169 #define PTP_RX_PARSE_CONFIG 0x0242 170 #define PTP_RX_PARSE_CONFIG_LAYER2_EN_ BIT(0) 171 #define PTP_RX_PARSE_CONFIG_IPV4_EN_ BIT(1) 172 #define PTP_RX_PARSE_CONFIG_IPV6_EN_ BIT(2) 173 174 #define PTP_TX_PARSE_CONFIG 0x0282 175 #define PTP_TX_PARSE_CONFIG_LAYER2_EN_ BIT(0) 176 #define PTP_TX_PARSE_CONFIG_IPV4_EN_ BIT(1) 177 #define PTP_TX_PARSE_CONFIG_IPV6_EN_ BIT(2) 178 179 #define PTP_CLOCK_RATE_ADJ_HI 0x020C 180 #define PTP_CLOCK_RATE_ADJ_LO 0x020D 181 #define PTP_CLOCK_RATE_ADJ_DIR_ BIT(15) 182 183 #define PTP_LTC_STEP_ADJ_HI 0x0212 184 #define PTP_LTC_STEP_ADJ_LO 0x0213 185 #define PTP_LTC_STEP_ADJ_DIR_ BIT(15) 186 187 #define LAN8814_INTR_STS_REG 0x0033 188 #define LAN8814_INTR_STS_REG_1588_TSU0_ BIT(0) 189 #define LAN8814_INTR_STS_REG_1588_TSU1_ BIT(1) 190 #define LAN8814_INTR_STS_REG_1588_TSU2_ BIT(2) 191 #define LAN8814_INTR_STS_REG_1588_TSU3_ BIT(3) 192 193 #define PTP_CAP_INFO 0x022A 194 #define PTP_CAP_INFO_TX_TS_CNT_GET_(reg_val) (((reg_val) & 0x0f00) >> 8) 195 #define PTP_CAP_INFO_RX_TS_CNT_GET_(reg_val) ((reg_val) & 0x000f) 196 197 #define PTP_TX_EGRESS_SEC_HI 0x0296 198 #define PTP_TX_EGRESS_SEC_LO 0x0297 199 #define PTP_TX_EGRESS_NS_HI 0x0294 200 #define PTP_TX_EGRESS_NS_LO 0x0295 201 #define PTP_TX_MSG_HEADER2 0x0299 202 203 #define PTP_RX_INGRESS_SEC_HI 0x0256 204 #define PTP_RX_INGRESS_SEC_LO 0x0257 205 #define PTP_RX_INGRESS_NS_HI 0x0254 206 #define PTP_RX_INGRESS_NS_LO 0x0255 207 #define PTP_RX_MSG_HEADER2 0x0259 208 209 #define PTP_TSU_INT_EN 0x0200 210 #define PTP_TSU_INT_EN_PTP_TX_TS_OVRFL_EN_ BIT(3) 211 #define PTP_TSU_INT_EN_PTP_TX_TS_EN_ BIT(2) 212 #define PTP_TSU_INT_EN_PTP_RX_TS_OVRFL_EN_ BIT(1) 213 #define PTP_TSU_INT_EN_PTP_RX_TS_EN_ BIT(0) 214 215 #define PTP_TSU_INT_STS 0x0201 216 #define PTP_TSU_INT_STS_PTP_TX_TS_OVRFL_INT_ BIT(3) 217 #define PTP_TSU_INT_STS_PTP_TX_TS_EN_ BIT(2) 218 #define PTP_TSU_INT_STS_PTP_RX_TS_OVRFL_INT_ BIT(1) 219 #define PTP_TSU_INT_STS_PTP_RX_TS_EN_ BIT(0) 220 221 #define LAN8814_LED_CTRL_1 0x0 222 #define LAN8814_LED_CTRL_1_KSZ9031_LED_MODE_ BIT(6) 223 224 /* PHY Control 1 */ 225 #define MII_KSZPHY_CTRL_1 0x1e 226 #define KSZ8081_CTRL1_MDIX_STAT BIT(4) 227 228 /* PHY Control 2 / PHY Control (if no PHY Control 1) */ 229 #define MII_KSZPHY_CTRL_2 0x1f 230 #define MII_KSZPHY_CTRL MII_KSZPHY_CTRL_2 231 /* bitmap of PHY register to set interrupt mode */ 232 #define KSZ8081_CTRL2_HP_MDIX BIT(15) 233 #define KSZ8081_CTRL2_MDI_MDI_X_SELECT BIT(14) 234 #define KSZ8081_CTRL2_DISABLE_AUTO_MDIX BIT(13) 235 #define KSZ8081_CTRL2_FORCE_LINK BIT(11) 236 #define KSZ8081_CTRL2_POWER_SAVING BIT(10) 237 #define KSZPHY_CTRL_INT_ACTIVE_HIGH BIT(9) 238 #define KSZPHY_RMII_REF_CLK_SEL BIT(7) 239 240 /* Write/read to/from extended registers */ 241 #define MII_KSZPHY_EXTREG 0x0b 242 #define KSZPHY_EXTREG_WRITE 0x8000 243 244 #define MII_KSZPHY_EXTREG_WRITE 0x0c 245 #define MII_KSZPHY_EXTREG_READ 0x0d 246 247 /* Extended registers */ 248 #define MII_KSZPHY_CLK_CONTROL_PAD_SKEW 0x104 249 #define MII_KSZPHY_RX_DATA_PAD_SKEW 0x105 250 #define MII_KSZPHY_TX_DATA_PAD_SKEW 0x106 251 252 #define PS_TO_REG 200 253 #define FIFO_SIZE 8 254 255 struct kszphy_hw_stat { 256 const char *string; 257 u8 reg; 258 u8 bits; 259 }; 260 261 static struct kszphy_hw_stat kszphy_hw_stats[] = { 262 { "phy_receive_errors", 21, 16}, 263 { "phy_idle_errors", 10, 8 }, 264 }; 265 266 struct kszphy_type { 267 u32 led_mode_reg; 268 u16 interrupt_level_mask; 269 u16 cable_diag_reg; 270 unsigned long pair_mask; 271 bool has_broadcast_disable; 272 bool has_nand_tree_disable; 273 bool has_rmii_ref_clk_sel; 274 }; 275 276 /* Shared structure between the PHYs of the same package. */ 277 struct lan8814_shared_priv { 278 struct phy_device *phydev; 279 struct ptp_clock *ptp_clock; 280 struct ptp_clock_info ptp_clock_info; 281 282 /* Reference counter to how many ports in the package are enabling the 283 * timestamping 284 */ 285 u8 ref; 286 287 /* Lock for ptp_clock and ref */ 288 struct mutex shared_lock; 289 }; 290 291 struct lan8814_ptp_rx_ts { 292 struct list_head list; 293 u32 seconds; 294 u32 nsec; 295 u16 seq_id; 296 }; 297 298 struct kszphy_ptp_priv { 299 struct mii_timestamper mii_ts; 300 struct phy_device *phydev; 301 302 struct sk_buff_head tx_queue; 303 struct sk_buff_head rx_queue; 304 305 struct list_head rx_ts_list; 306 /* Lock for Rx ts fifo */ 307 spinlock_t rx_ts_lock; 308 309 int hwts_tx_type; 310 enum hwtstamp_rx_filters rx_filter; 311 int layer; 312 int version; 313 }; 314 315 struct kszphy_priv { 316 struct kszphy_ptp_priv ptp_priv; 317 const struct kszphy_type *type; 318 int led_mode; 319 u16 vct_ctrl1000; 320 bool rmii_ref_clk_sel; 321 bool rmii_ref_clk_sel_val; 322 u64 stats[ARRAY_SIZE(kszphy_hw_stats)]; 323 }; 324 325 static const struct kszphy_type lan8814_type = { 326 .led_mode_reg = ~LAN8814_LED_CTRL_1, 327 .cable_diag_reg = LAN8814_CABLE_DIAG, 328 .pair_mask = LAN8814_WIRE_PAIR_MASK, 329 }; 330 331 static const struct kszphy_type ksz886x_type = { 332 .cable_diag_reg = KSZ8081_LMD, 333 .pair_mask = KSZPHY_WIRE_PAIR_MASK, 334 }; 335 336 static const struct kszphy_type ksz8021_type = { 337 .led_mode_reg = MII_KSZPHY_CTRL_2, 338 .has_broadcast_disable = true, 339 .has_nand_tree_disable = true, 340 .has_rmii_ref_clk_sel = true, 341 }; 342 343 static const struct kszphy_type ksz8041_type = { 344 .led_mode_reg = MII_KSZPHY_CTRL_1, 345 }; 346 347 static const struct kszphy_type ksz8051_type = { 348 .led_mode_reg = MII_KSZPHY_CTRL_2, 349 .has_nand_tree_disable = true, 350 }; 351 352 static const struct kszphy_type ksz8081_type = { 353 .led_mode_reg = MII_KSZPHY_CTRL_2, 354 .has_broadcast_disable = true, 355 .has_nand_tree_disable = true, 356 .has_rmii_ref_clk_sel = true, 357 }; 358 359 static const struct kszphy_type ks8737_type = { 360 .interrupt_level_mask = BIT(14), 361 }; 362 363 static const struct kszphy_type ksz9021_type = { 364 .interrupt_level_mask = BIT(14), 365 }; 366 367 static int kszphy_extended_write(struct phy_device *phydev, 368 u32 regnum, u16 val) 369 { 370 phy_write(phydev, MII_KSZPHY_EXTREG, KSZPHY_EXTREG_WRITE | regnum); 371 return phy_write(phydev, MII_KSZPHY_EXTREG_WRITE, val); 372 } 373 374 static int kszphy_extended_read(struct phy_device *phydev, 375 u32 regnum) 376 { 377 phy_write(phydev, MII_KSZPHY_EXTREG, regnum); 378 return phy_read(phydev, MII_KSZPHY_EXTREG_READ); 379 } 380 381 static int kszphy_ack_interrupt(struct phy_device *phydev) 382 { 383 /* bit[7..0] int status, which is a read and clear register. */ 384 int rc; 385 386 rc = phy_read(phydev, MII_KSZPHY_INTCS); 387 388 return (rc < 0) ? rc : 0; 389 } 390 391 static int kszphy_config_intr(struct phy_device *phydev) 392 { 393 const struct kszphy_type *type = phydev->drv->driver_data; 394 int temp, err; 395 u16 mask; 396 397 if (type && type->interrupt_level_mask) 398 mask = type->interrupt_level_mask; 399 else 400 mask = KSZPHY_CTRL_INT_ACTIVE_HIGH; 401 402 /* set the interrupt pin active low */ 403 temp = phy_read(phydev, MII_KSZPHY_CTRL); 404 if (temp < 0) 405 return temp; 406 temp &= ~mask; 407 phy_write(phydev, MII_KSZPHY_CTRL, temp); 408 409 /* enable / disable interrupts */ 410 if (phydev->interrupts == PHY_INTERRUPT_ENABLED) { 411 err = kszphy_ack_interrupt(phydev); 412 if (err) 413 return err; 414 415 temp = KSZPHY_INTCS_ALL; 416 err = phy_write(phydev, MII_KSZPHY_INTCS, temp); 417 } else { 418 temp = 0; 419 err = phy_write(phydev, MII_KSZPHY_INTCS, temp); 420 if (err) 421 return err; 422 423 err = kszphy_ack_interrupt(phydev); 424 } 425 426 return err; 427 } 428 429 static irqreturn_t kszphy_handle_interrupt(struct phy_device *phydev) 430 { 431 int irq_status; 432 433 irq_status = phy_read(phydev, MII_KSZPHY_INTCS); 434 if (irq_status < 0) { 435 phy_error(phydev); 436 return IRQ_NONE; 437 } 438 439 if (!(irq_status & KSZPHY_INTCS_STATUS)) 440 return IRQ_NONE; 441 442 phy_trigger_machine(phydev); 443 444 return IRQ_HANDLED; 445 } 446 447 static int kszphy_rmii_clk_sel(struct phy_device *phydev, bool val) 448 { 449 int ctrl; 450 451 ctrl = phy_read(phydev, MII_KSZPHY_CTRL); 452 if (ctrl < 0) 453 return ctrl; 454 455 if (val) 456 ctrl |= KSZPHY_RMII_REF_CLK_SEL; 457 else 458 ctrl &= ~KSZPHY_RMII_REF_CLK_SEL; 459 460 return phy_write(phydev, MII_KSZPHY_CTRL, ctrl); 461 } 462 463 static int kszphy_setup_led(struct phy_device *phydev, u32 reg, int val) 464 { 465 int rc, temp, shift; 466 467 switch (reg) { 468 case MII_KSZPHY_CTRL_1: 469 shift = 14; 470 break; 471 case MII_KSZPHY_CTRL_2: 472 shift = 4; 473 break; 474 default: 475 return -EINVAL; 476 } 477 478 temp = phy_read(phydev, reg); 479 if (temp < 0) { 480 rc = temp; 481 goto out; 482 } 483 484 temp &= ~(3 << shift); 485 temp |= val << shift; 486 rc = phy_write(phydev, reg, temp); 487 out: 488 if (rc < 0) 489 phydev_err(phydev, "failed to set led mode\n"); 490 491 return rc; 492 } 493 494 /* Disable PHY address 0 as the broadcast address, so that it can be used as a 495 * unique (non-broadcast) address on a shared bus. 496 */ 497 static int kszphy_broadcast_disable(struct phy_device *phydev) 498 { 499 int ret; 500 501 ret = phy_read(phydev, MII_KSZPHY_OMSO); 502 if (ret < 0) 503 goto out; 504 505 ret = phy_write(phydev, MII_KSZPHY_OMSO, ret | KSZPHY_OMSO_B_CAST_OFF); 506 out: 507 if (ret) 508 phydev_err(phydev, "failed to disable broadcast address\n"); 509 510 return ret; 511 } 512 513 static int kszphy_nand_tree_disable(struct phy_device *phydev) 514 { 515 int ret; 516 517 ret = phy_read(phydev, MII_KSZPHY_OMSO); 518 if (ret < 0) 519 goto out; 520 521 if (!(ret & KSZPHY_OMSO_NAND_TREE_ON)) 522 return 0; 523 524 ret = phy_write(phydev, MII_KSZPHY_OMSO, 525 ret & ~KSZPHY_OMSO_NAND_TREE_ON); 526 out: 527 if (ret) 528 phydev_err(phydev, "failed to disable NAND tree mode\n"); 529 530 return ret; 531 } 532 533 /* Some config bits need to be set again on resume, handle them here. */ 534 static int kszphy_config_reset(struct phy_device *phydev) 535 { 536 struct kszphy_priv *priv = phydev->priv; 537 int ret; 538 539 if (priv->rmii_ref_clk_sel) { 540 ret = kszphy_rmii_clk_sel(phydev, priv->rmii_ref_clk_sel_val); 541 if (ret) { 542 phydev_err(phydev, 543 "failed to set rmii reference clock\n"); 544 return ret; 545 } 546 } 547 548 if (priv->type && priv->led_mode >= 0) 549 kszphy_setup_led(phydev, priv->type->led_mode_reg, priv->led_mode); 550 551 return 0; 552 } 553 554 static int kszphy_config_init(struct phy_device *phydev) 555 { 556 struct kszphy_priv *priv = phydev->priv; 557 const struct kszphy_type *type; 558 559 if (!priv) 560 return 0; 561 562 type = priv->type; 563 564 if (type && type->has_broadcast_disable) 565 kszphy_broadcast_disable(phydev); 566 567 if (type && type->has_nand_tree_disable) 568 kszphy_nand_tree_disable(phydev); 569 570 return kszphy_config_reset(phydev); 571 } 572 573 static int ksz8041_fiber_mode(struct phy_device *phydev) 574 { 575 struct device_node *of_node = phydev->mdio.dev.of_node; 576 577 return of_property_read_bool(of_node, "micrel,fiber-mode"); 578 } 579 580 static int ksz8041_config_init(struct phy_device *phydev) 581 { 582 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, }; 583 584 /* Limit supported and advertised modes in fiber mode */ 585 if (ksz8041_fiber_mode(phydev)) { 586 phydev->dev_flags |= MICREL_PHY_FXEN; 587 linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, mask); 588 linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, mask); 589 590 linkmode_and(phydev->supported, phydev->supported, mask); 591 linkmode_set_bit(ETHTOOL_LINK_MODE_FIBRE_BIT, 592 phydev->supported); 593 linkmode_and(phydev->advertising, phydev->advertising, mask); 594 linkmode_set_bit(ETHTOOL_LINK_MODE_FIBRE_BIT, 595 phydev->advertising); 596 phydev->autoneg = AUTONEG_DISABLE; 597 } 598 599 return kszphy_config_init(phydev); 600 } 601 602 static int ksz8041_config_aneg(struct phy_device *phydev) 603 { 604 /* Skip auto-negotiation in fiber mode */ 605 if (phydev->dev_flags & MICREL_PHY_FXEN) { 606 phydev->speed = SPEED_100; 607 return 0; 608 } 609 610 return genphy_config_aneg(phydev); 611 } 612 613 static int ksz8051_ksz8795_match_phy_device(struct phy_device *phydev, 614 const bool ksz_8051) 615 { 616 int ret; 617 618 if ((phydev->phy_id & MICREL_PHY_ID_MASK) != PHY_ID_KSZ8051) 619 return 0; 620 621 ret = phy_read(phydev, MII_BMSR); 622 if (ret < 0) 623 return ret; 624 625 /* KSZ8051 PHY and KSZ8794/KSZ8795/KSZ8765 switch share the same 626 * exact PHY ID. However, they can be told apart by the extended 627 * capability registers presence. The KSZ8051 PHY has them while 628 * the switch does not. 629 */ 630 ret &= BMSR_ERCAP; 631 if (ksz_8051) 632 return ret; 633 else 634 return !ret; 635 } 636 637 static int ksz8051_match_phy_device(struct phy_device *phydev) 638 { 639 return ksz8051_ksz8795_match_phy_device(phydev, true); 640 } 641 642 static int ksz8081_config_init(struct phy_device *phydev) 643 { 644 /* KSZPHY_OMSO_FACTORY_TEST is set at de-assertion of the reset line 645 * based on the RXER (KSZ8081RNA/RND) or TXC (KSZ8081MNX/RNB) pin. If a 646 * pull-down is missing, the factory test mode should be cleared by 647 * manually writing a 0. 648 */ 649 phy_clear_bits(phydev, MII_KSZPHY_OMSO, KSZPHY_OMSO_FACTORY_TEST); 650 651 return kszphy_config_init(phydev); 652 } 653 654 static int ksz8081_config_mdix(struct phy_device *phydev, u8 ctrl) 655 { 656 u16 val; 657 658 switch (ctrl) { 659 case ETH_TP_MDI: 660 val = KSZ8081_CTRL2_DISABLE_AUTO_MDIX; 661 break; 662 case ETH_TP_MDI_X: 663 val = KSZ8081_CTRL2_DISABLE_AUTO_MDIX | 664 KSZ8081_CTRL2_MDI_MDI_X_SELECT; 665 break; 666 case ETH_TP_MDI_AUTO: 667 val = 0; 668 break; 669 default: 670 return 0; 671 } 672 673 return phy_modify(phydev, MII_KSZPHY_CTRL_2, 674 KSZ8081_CTRL2_HP_MDIX | 675 KSZ8081_CTRL2_MDI_MDI_X_SELECT | 676 KSZ8081_CTRL2_DISABLE_AUTO_MDIX, 677 KSZ8081_CTRL2_HP_MDIX | val); 678 } 679 680 static int ksz8081_config_aneg(struct phy_device *phydev) 681 { 682 int ret; 683 684 ret = genphy_config_aneg(phydev); 685 if (ret) 686 return ret; 687 688 /* The MDI-X configuration is automatically changed by the PHY after 689 * switching from autoneg off to on. So, take MDI-X configuration under 690 * own control and set it after autoneg configuration was done. 691 */ 692 return ksz8081_config_mdix(phydev, phydev->mdix_ctrl); 693 } 694 695 static int ksz8081_mdix_update(struct phy_device *phydev) 696 { 697 int ret; 698 699 ret = phy_read(phydev, MII_KSZPHY_CTRL_2); 700 if (ret < 0) 701 return ret; 702 703 if (ret & KSZ8081_CTRL2_DISABLE_AUTO_MDIX) { 704 if (ret & KSZ8081_CTRL2_MDI_MDI_X_SELECT) 705 phydev->mdix_ctrl = ETH_TP_MDI_X; 706 else 707 phydev->mdix_ctrl = ETH_TP_MDI; 708 } else { 709 phydev->mdix_ctrl = ETH_TP_MDI_AUTO; 710 } 711 712 ret = phy_read(phydev, MII_KSZPHY_CTRL_1); 713 if (ret < 0) 714 return ret; 715 716 if (ret & KSZ8081_CTRL1_MDIX_STAT) 717 phydev->mdix = ETH_TP_MDI; 718 else 719 phydev->mdix = ETH_TP_MDI_X; 720 721 return 0; 722 } 723 724 static int ksz8081_read_status(struct phy_device *phydev) 725 { 726 int ret; 727 728 ret = ksz8081_mdix_update(phydev); 729 if (ret < 0) 730 return ret; 731 732 return genphy_read_status(phydev); 733 } 734 735 static int ksz8061_config_init(struct phy_device *phydev) 736 { 737 int ret; 738 739 ret = phy_write_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_DEVID1, 0xB61A); 740 if (ret) 741 return ret; 742 743 return kszphy_config_init(phydev); 744 } 745 746 static int ksz8795_match_phy_device(struct phy_device *phydev) 747 { 748 return ksz8051_ksz8795_match_phy_device(phydev, false); 749 } 750 751 static int ksz9021_load_values_from_of(struct phy_device *phydev, 752 const struct device_node *of_node, 753 u16 reg, 754 const char *field1, const char *field2, 755 const char *field3, const char *field4) 756 { 757 int val1 = -1; 758 int val2 = -2; 759 int val3 = -3; 760 int val4 = -4; 761 int newval; 762 int matches = 0; 763 764 if (!of_property_read_u32(of_node, field1, &val1)) 765 matches++; 766 767 if (!of_property_read_u32(of_node, field2, &val2)) 768 matches++; 769 770 if (!of_property_read_u32(of_node, field3, &val3)) 771 matches++; 772 773 if (!of_property_read_u32(of_node, field4, &val4)) 774 matches++; 775 776 if (!matches) 777 return 0; 778 779 if (matches < 4) 780 newval = kszphy_extended_read(phydev, reg); 781 else 782 newval = 0; 783 784 if (val1 != -1) 785 newval = ((newval & 0xfff0) | ((val1 / PS_TO_REG) & 0xf) << 0); 786 787 if (val2 != -2) 788 newval = ((newval & 0xff0f) | ((val2 / PS_TO_REG) & 0xf) << 4); 789 790 if (val3 != -3) 791 newval = ((newval & 0xf0ff) | ((val3 / PS_TO_REG) & 0xf) << 8); 792 793 if (val4 != -4) 794 newval = ((newval & 0x0fff) | ((val4 / PS_TO_REG) & 0xf) << 12); 795 796 return kszphy_extended_write(phydev, reg, newval); 797 } 798 799 static int ksz9021_config_init(struct phy_device *phydev) 800 { 801 const struct device_node *of_node; 802 const struct device *dev_walker; 803 804 /* The Micrel driver has a deprecated option to place phy OF 805 * properties in the MAC node. Walk up the tree of devices to 806 * find a device with an OF node. 807 */ 808 dev_walker = &phydev->mdio.dev; 809 do { 810 of_node = dev_walker->of_node; 811 dev_walker = dev_walker->parent; 812 813 } while (!of_node && dev_walker); 814 815 if (of_node) { 816 ksz9021_load_values_from_of(phydev, of_node, 817 MII_KSZPHY_CLK_CONTROL_PAD_SKEW, 818 "txen-skew-ps", "txc-skew-ps", 819 "rxdv-skew-ps", "rxc-skew-ps"); 820 ksz9021_load_values_from_of(phydev, of_node, 821 MII_KSZPHY_RX_DATA_PAD_SKEW, 822 "rxd0-skew-ps", "rxd1-skew-ps", 823 "rxd2-skew-ps", "rxd3-skew-ps"); 824 ksz9021_load_values_from_of(phydev, of_node, 825 MII_KSZPHY_TX_DATA_PAD_SKEW, 826 "txd0-skew-ps", "txd1-skew-ps", 827 "txd2-skew-ps", "txd3-skew-ps"); 828 } 829 return 0; 830 } 831 832 #define KSZ9031_PS_TO_REG 60 833 834 /* Extended registers */ 835 /* MMD Address 0x0 */ 836 #define MII_KSZ9031RN_FLP_BURST_TX_LO 3 837 #define MII_KSZ9031RN_FLP_BURST_TX_HI 4 838 839 /* MMD Address 0x2 */ 840 #define MII_KSZ9031RN_CONTROL_PAD_SKEW 4 841 #define MII_KSZ9031RN_RX_CTL_M GENMASK(7, 4) 842 #define MII_KSZ9031RN_TX_CTL_M GENMASK(3, 0) 843 844 #define MII_KSZ9031RN_RX_DATA_PAD_SKEW 5 845 #define MII_KSZ9031RN_RXD3 GENMASK(15, 12) 846 #define MII_KSZ9031RN_RXD2 GENMASK(11, 8) 847 #define MII_KSZ9031RN_RXD1 GENMASK(7, 4) 848 #define MII_KSZ9031RN_RXD0 GENMASK(3, 0) 849 850 #define MII_KSZ9031RN_TX_DATA_PAD_SKEW 6 851 #define MII_KSZ9031RN_TXD3 GENMASK(15, 12) 852 #define MII_KSZ9031RN_TXD2 GENMASK(11, 8) 853 #define MII_KSZ9031RN_TXD1 GENMASK(7, 4) 854 #define MII_KSZ9031RN_TXD0 GENMASK(3, 0) 855 856 #define MII_KSZ9031RN_CLK_PAD_SKEW 8 857 #define MII_KSZ9031RN_GTX_CLK GENMASK(9, 5) 858 #define MII_KSZ9031RN_RX_CLK GENMASK(4, 0) 859 860 /* KSZ9031 has internal RGMII_IDRX = 1.2ns and RGMII_IDTX = 0ns. To 861 * provide different RGMII options we need to configure delay offset 862 * for each pad relative to build in delay. 863 */ 864 /* keep rx as "No delay adjustment" and set rx_clk to +0.60ns to get delays of 865 * 1.80ns 866 */ 867 #define RX_ID 0x7 868 #define RX_CLK_ID 0x19 869 870 /* set rx to +0.30ns and rx_clk to -0.90ns to compensate the 871 * internal 1.2ns delay. 872 */ 873 #define RX_ND 0xc 874 #define RX_CLK_ND 0x0 875 876 /* set tx to -0.42ns and tx_clk to +0.96ns to get 1.38ns delay */ 877 #define TX_ID 0x0 878 #define TX_CLK_ID 0x1f 879 880 /* set tx and tx_clk to "No delay adjustment" to keep 0ns 881 * dealy 882 */ 883 #define TX_ND 0x7 884 #define TX_CLK_ND 0xf 885 886 /* MMD Address 0x1C */ 887 #define MII_KSZ9031RN_EDPD 0x23 888 #define MII_KSZ9031RN_EDPD_ENABLE BIT(0) 889 890 static int ksz9031_of_load_skew_values(struct phy_device *phydev, 891 const struct device_node *of_node, 892 u16 reg, size_t field_sz, 893 const char *field[], u8 numfields, 894 bool *update) 895 { 896 int val[4] = {-1, -2, -3, -4}; 897 int matches = 0; 898 u16 mask; 899 u16 maxval; 900 u16 newval; 901 int i; 902 903 for (i = 0; i < numfields; i++) 904 if (!of_property_read_u32(of_node, field[i], val + i)) 905 matches++; 906 907 if (!matches) 908 return 0; 909 910 *update |= true; 911 912 if (matches < numfields) 913 newval = phy_read_mmd(phydev, 2, reg); 914 else 915 newval = 0; 916 917 maxval = (field_sz == 4) ? 0xf : 0x1f; 918 for (i = 0; i < numfields; i++) 919 if (val[i] != -(i + 1)) { 920 mask = 0xffff; 921 mask ^= maxval << (field_sz * i); 922 newval = (newval & mask) | 923 (((val[i] / KSZ9031_PS_TO_REG) & maxval) 924 << (field_sz * i)); 925 } 926 927 return phy_write_mmd(phydev, 2, reg, newval); 928 } 929 930 /* Center KSZ9031RNX FLP timing at 16ms. */ 931 static int ksz9031_center_flp_timing(struct phy_device *phydev) 932 { 933 int result; 934 935 result = phy_write_mmd(phydev, 0, MII_KSZ9031RN_FLP_BURST_TX_HI, 936 0x0006); 937 if (result) 938 return result; 939 940 result = phy_write_mmd(phydev, 0, MII_KSZ9031RN_FLP_BURST_TX_LO, 941 0x1A80); 942 if (result) 943 return result; 944 945 return genphy_restart_aneg(phydev); 946 } 947 948 /* Enable energy-detect power-down mode */ 949 static int ksz9031_enable_edpd(struct phy_device *phydev) 950 { 951 int reg; 952 953 reg = phy_read_mmd(phydev, 0x1C, MII_KSZ9031RN_EDPD); 954 if (reg < 0) 955 return reg; 956 return phy_write_mmd(phydev, 0x1C, MII_KSZ9031RN_EDPD, 957 reg | MII_KSZ9031RN_EDPD_ENABLE); 958 } 959 960 static int ksz9031_config_rgmii_delay(struct phy_device *phydev) 961 { 962 u16 rx, tx, rx_clk, tx_clk; 963 int ret; 964 965 switch (phydev->interface) { 966 case PHY_INTERFACE_MODE_RGMII: 967 tx = TX_ND; 968 tx_clk = TX_CLK_ND; 969 rx = RX_ND; 970 rx_clk = RX_CLK_ND; 971 break; 972 case PHY_INTERFACE_MODE_RGMII_ID: 973 tx = TX_ID; 974 tx_clk = TX_CLK_ID; 975 rx = RX_ID; 976 rx_clk = RX_CLK_ID; 977 break; 978 case PHY_INTERFACE_MODE_RGMII_RXID: 979 tx = TX_ND; 980 tx_clk = TX_CLK_ND; 981 rx = RX_ID; 982 rx_clk = RX_CLK_ID; 983 break; 984 case PHY_INTERFACE_MODE_RGMII_TXID: 985 tx = TX_ID; 986 tx_clk = TX_CLK_ID; 987 rx = RX_ND; 988 rx_clk = RX_CLK_ND; 989 break; 990 default: 991 return 0; 992 } 993 994 ret = phy_write_mmd(phydev, 2, MII_KSZ9031RN_CONTROL_PAD_SKEW, 995 FIELD_PREP(MII_KSZ9031RN_RX_CTL_M, rx) | 996 FIELD_PREP(MII_KSZ9031RN_TX_CTL_M, tx)); 997 if (ret < 0) 998 return ret; 999 1000 ret = phy_write_mmd(phydev, 2, MII_KSZ9031RN_RX_DATA_PAD_SKEW, 1001 FIELD_PREP(MII_KSZ9031RN_RXD3, rx) | 1002 FIELD_PREP(MII_KSZ9031RN_RXD2, rx) | 1003 FIELD_PREP(MII_KSZ9031RN_RXD1, rx) | 1004 FIELD_PREP(MII_KSZ9031RN_RXD0, rx)); 1005 if (ret < 0) 1006 return ret; 1007 1008 ret = phy_write_mmd(phydev, 2, MII_KSZ9031RN_TX_DATA_PAD_SKEW, 1009 FIELD_PREP(MII_KSZ9031RN_TXD3, tx) | 1010 FIELD_PREP(MII_KSZ9031RN_TXD2, tx) | 1011 FIELD_PREP(MII_KSZ9031RN_TXD1, tx) | 1012 FIELD_PREP(MII_KSZ9031RN_TXD0, tx)); 1013 if (ret < 0) 1014 return ret; 1015 1016 return phy_write_mmd(phydev, 2, MII_KSZ9031RN_CLK_PAD_SKEW, 1017 FIELD_PREP(MII_KSZ9031RN_GTX_CLK, tx_clk) | 1018 FIELD_PREP(MII_KSZ9031RN_RX_CLK, rx_clk)); 1019 } 1020 1021 static int ksz9031_config_init(struct phy_device *phydev) 1022 { 1023 const struct device_node *of_node; 1024 static const char *clk_skews[2] = {"rxc-skew-ps", "txc-skew-ps"}; 1025 static const char *rx_data_skews[4] = { 1026 "rxd0-skew-ps", "rxd1-skew-ps", 1027 "rxd2-skew-ps", "rxd3-skew-ps" 1028 }; 1029 static const char *tx_data_skews[4] = { 1030 "txd0-skew-ps", "txd1-skew-ps", 1031 "txd2-skew-ps", "txd3-skew-ps" 1032 }; 1033 static const char *control_skews[2] = {"txen-skew-ps", "rxdv-skew-ps"}; 1034 const struct device *dev_walker; 1035 int result; 1036 1037 result = ksz9031_enable_edpd(phydev); 1038 if (result < 0) 1039 return result; 1040 1041 /* The Micrel driver has a deprecated option to place phy OF 1042 * properties in the MAC node. Walk up the tree of devices to 1043 * find a device with an OF node. 1044 */ 1045 dev_walker = &phydev->mdio.dev; 1046 do { 1047 of_node = dev_walker->of_node; 1048 dev_walker = dev_walker->parent; 1049 } while (!of_node && dev_walker); 1050 1051 if (of_node) { 1052 bool update = false; 1053 1054 if (phy_interface_is_rgmii(phydev)) { 1055 result = ksz9031_config_rgmii_delay(phydev); 1056 if (result < 0) 1057 return result; 1058 } 1059 1060 ksz9031_of_load_skew_values(phydev, of_node, 1061 MII_KSZ9031RN_CLK_PAD_SKEW, 5, 1062 clk_skews, 2, &update); 1063 1064 ksz9031_of_load_skew_values(phydev, of_node, 1065 MII_KSZ9031RN_CONTROL_PAD_SKEW, 4, 1066 control_skews, 2, &update); 1067 1068 ksz9031_of_load_skew_values(phydev, of_node, 1069 MII_KSZ9031RN_RX_DATA_PAD_SKEW, 4, 1070 rx_data_skews, 4, &update); 1071 1072 ksz9031_of_load_skew_values(phydev, of_node, 1073 MII_KSZ9031RN_TX_DATA_PAD_SKEW, 4, 1074 tx_data_skews, 4, &update); 1075 1076 if (update && !phy_interface_is_rgmii(phydev)) 1077 phydev_warn(phydev, 1078 "*-skew-ps values should be used only with RGMII PHY modes\n"); 1079 1080 /* Silicon Errata Sheet (DS80000691D or DS80000692D): 1081 * When the device links in the 1000BASE-T slave mode only, 1082 * the optional 125MHz reference output clock (CLK125_NDO) 1083 * has wide duty cycle variation. 1084 * 1085 * The optional CLK125_NDO clock does not meet the RGMII 1086 * 45/55 percent (min/max) duty cycle requirement and therefore 1087 * cannot be used directly by the MAC side for clocking 1088 * applications that have setup/hold time requirements on 1089 * rising and falling clock edges. 1090 * 1091 * Workaround: 1092 * Force the phy to be the master to receive a stable clock 1093 * which meets the duty cycle requirement. 1094 */ 1095 if (of_property_read_bool(of_node, "micrel,force-master")) { 1096 result = phy_read(phydev, MII_CTRL1000); 1097 if (result < 0) 1098 goto err_force_master; 1099 1100 /* enable master mode, config & prefer master */ 1101 result |= CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER; 1102 result = phy_write(phydev, MII_CTRL1000, result); 1103 if (result < 0) 1104 goto err_force_master; 1105 } 1106 } 1107 1108 return ksz9031_center_flp_timing(phydev); 1109 1110 err_force_master: 1111 phydev_err(phydev, "failed to force the phy to master mode\n"); 1112 return result; 1113 } 1114 1115 #define KSZ9131_SKEW_5BIT_MAX 2400 1116 #define KSZ9131_SKEW_4BIT_MAX 800 1117 #define KSZ9131_OFFSET 700 1118 #define KSZ9131_STEP 100 1119 1120 static int ksz9131_of_load_skew_values(struct phy_device *phydev, 1121 struct device_node *of_node, 1122 u16 reg, size_t field_sz, 1123 char *field[], u8 numfields) 1124 { 1125 int val[4] = {-(1 + KSZ9131_OFFSET), -(2 + KSZ9131_OFFSET), 1126 -(3 + KSZ9131_OFFSET), -(4 + KSZ9131_OFFSET)}; 1127 int skewval, skewmax = 0; 1128 int matches = 0; 1129 u16 maxval; 1130 u16 newval; 1131 u16 mask; 1132 int i; 1133 1134 /* psec properties in dts should mean x pico seconds */ 1135 if (field_sz == 5) 1136 skewmax = KSZ9131_SKEW_5BIT_MAX; 1137 else 1138 skewmax = KSZ9131_SKEW_4BIT_MAX; 1139 1140 for (i = 0; i < numfields; i++) 1141 if (!of_property_read_s32(of_node, field[i], &skewval)) { 1142 if (skewval < -KSZ9131_OFFSET) 1143 skewval = -KSZ9131_OFFSET; 1144 else if (skewval > skewmax) 1145 skewval = skewmax; 1146 1147 val[i] = skewval + KSZ9131_OFFSET; 1148 matches++; 1149 } 1150 1151 if (!matches) 1152 return 0; 1153 1154 if (matches < numfields) 1155 newval = phy_read_mmd(phydev, 2, reg); 1156 else 1157 newval = 0; 1158 1159 maxval = (field_sz == 4) ? 0xf : 0x1f; 1160 for (i = 0; i < numfields; i++) 1161 if (val[i] != -(i + 1 + KSZ9131_OFFSET)) { 1162 mask = 0xffff; 1163 mask ^= maxval << (field_sz * i); 1164 newval = (newval & mask) | 1165 (((val[i] / KSZ9131_STEP) & maxval) 1166 << (field_sz * i)); 1167 } 1168 1169 return phy_write_mmd(phydev, 2, reg, newval); 1170 } 1171 1172 #define KSZ9131RN_MMD_COMMON_CTRL_REG 2 1173 #define KSZ9131RN_RXC_DLL_CTRL 76 1174 #define KSZ9131RN_TXC_DLL_CTRL 77 1175 #define KSZ9131RN_DLL_CTRL_BYPASS BIT_MASK(12) 1176 #define KSZ9131RN_DLL_ENABLE_DELAY 0 1177 #define KSZ9131RN_DLL_DISABLE_DELAY BIT(12) 1178 1179 static int ksz9131_config_rgmii_delay(struct phy_device *phydev) 1180 { 1181 u16 rxcdll_val, txcdll_val; 1182 int ret; 1183 1184 switch (phydev->interface) { 1185 case PHY_INTERFACE_MODE_RGMII: 1186 rxcdll_val = KSZ9131RN_DLL_DISABLE_DELAY; 1187 txcdll_val = KSZ9131RN_DLL_DISABLE_DELAY; 1188 break; 1189 case PHY_INTERFACE_MODE_RGMII_ID: 1190 rxcdll_val = KSZ9131RN_DLL_ENABLE_DELAY; 1191 txcdll_val = KSZ9131RN_DLL_ENABLE_DELAY; 1192 break; 1193 case PHY_INTERFACE_MODE_RGMII_RXID: 1194 rxcdll_val = KSZ9131RN_DLL_ENABLE_DELAY; 1195 txcdll_val = KSZ9131RN_DLL_DISABLE_DELAY; 1196 break; 1197 case PHY_INTERFACE_MODE_RGMII_TXID: 1198 rxcdll_val = KSZ9131RN_DLL_DISABLE_DELAY; 1199 txcdll_val = KSZ9131RN_DLL_ENABLE_DELAY; 1200 break; 1201 default: 1202 return 0; 1203 } 1204 1205 ret = phy_modify_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG, 1206 KSZ9131RN_RXC_DLL_CTRL, KSZ9131RN_DLL_CTRL_BYPASS, 1207 rxcdll_val); 1208 if (ret < 0) 1209 return ret; 1210 1211 return phy_modify_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG, 1212 KSZ9131RN_TXC_DLL_CTRL, KSZ9131RN_DLL_CTRL_BYPASS, 1213 txcdll_val); 1214 } 1215 1216 /* Silicon Errata DS80000693B 1217 * 1218 * When LEDs are configured in Individual Mode, LED1 is ON in a no-link 1219 * condition. Workaround is to set register 0x1e, bit 9, this way LED1 behaves 1220 * according to the datasheet (off if there is no link). 1221 */ 1222 static int ksz9131_led_errata(struct phy_device *phydev) 1223 { 1224 int reg; 1225 1226 reg = phy_read_mmd(phydev, 2, 0); 1227 if (reg < 0) 1228 return reg; 1229 1230 if (!(reg & BIT(4))) 1231 return 0; 1232 1233 return phy_set_bits(phydev, 0x1e, BIT(9)); 1234 } 1235 1236 static int ksz9131_config_init(struct phy_device *phydev) 1237 { 1238 struct device_node *of_node; 1239 char *clk_skews[2] = {"rxc-skew-psec", "txc-skew-psec"}; 1240 char *rx_data_skews[4] = { 1241 "rxd0-skew-psec", "rxd1-skew-psec", 1242 "rxd2-skew-psec", "rxd3-skew-psec" 1243 }; 1244 char *tx_data_skews[4] = { 1245 "txd0-skew-psec", "txd1-skew-psec", 1246 "txd2-skew-psec", "txd3-skew-psec" 1247 }; 1248 char *control_skews[2] = {"txen-skew-psec", "rxdv-skew-psec"}; 1249 const struct device *dev_walker; 1250 int ret; 1251 1252 dev_walker = &phydev->mdio.dev; 1253 do { 1254 of_node = dev_walker->of_node; 1255 dev_walker = dev_walker->parent; 1256 } while (!of_node && dev_walker); 1257 1258 if (!of_node) 1259 return 0; 1260 1261 if (phy_interface_is_rgmii(phydev)) { 1262 ret = ksz9131_config_rgmii_delay(phydev); 1263 if (ret < 0) 1264 return ret; 1265 } 1266 1267 ret = ksz9131_of_load_skew_values(phydev, of_node, 1268 MII_KSZ9031RN_CLK_PAD_SKEW, 5, 1269 clk_skews, 2); 1270 if (ret < 0) 1271 return ret; 1272 1273 ret = ksz9131_of_load_skew_values(phydev, of_node, 1274 MII_KSZ9031RN_CONTROL_PAD_SKEW, 4, 1275 control_skews, 2); 1276 if (ret < 0) 1277 return ret; 1278 1279 ret = ksz9131_of_load_skew_values(phydev, of_node, 1280 MII_KSZ9031RN_RX_DATA_PAD_SKEW, 4, 1281 rx_data_skews, 4); 1282 if (ret < 0) 1283 return ret; 1284 1285 ret = ksz9131_of_load_skew_values(phydev, of_node, 1286 MII_KSZ9031RN_TX_DATA_PAD_SKEW, 4, 1287 tx_data_skews, 4); 1288 if (ret < 0) 1289 return ret; 1290 1291 ret = ksz9131_led_errata(phydev); 1292 if (ret < 0) 1293 return ret; 1294 1295 return 0; 1296 } 1297 1298 #define KSZ8873MLL_GLOBAL_CONTROL_4 0x06 1299 #define KSZ8873MLL_GLOBAL_CONTROL_4_DUPLEX BIT(6) 1300 #define KSZ8873MLL_GLOBAL_CONTROL_4_SPEED BIT(4) 1301 static int ksz8873mll_read_status(struct phy_device *phydev) 1302 { 1303 int regval; 1304 1305 /* dummy read */ 1306 regval = phy_read(phydev, KSZ8873MLL_GLOBAL_CONTROL_4); 1307 1308 regval = phy_read(phydev, KSZ8873MLL_GLOBAL_CONTROL_4); 1309 1310 if (regval & KSZ8873MLL_GLOBAL_CONTROL_4_DUPLEX) 1311 phydev->duplex = DUPLEX_HALF; 1312 else 1313 phydev->duplex = DUPLEX_FULL; 1314 1315 if (regval & KSZ8873MLL_GLOBAL_CONTROL_4_SPEED) 1316 phydev->speed = SPEED_10; 1317 else 1318 phydev->speed = SPEED_100; 1319 1320 phydev->link = 1; 1321 phydev->pause = phydev->asym_pause = 0; 1322 1323 return 0; 1324 } 1325 1326 static int ksz9031_get_features(struct phy_device *phydev) 1327 { 1328 int ret; 1329 1330 ret = genphy_read_abilities(phydev); 1331 if (ret < 0) 1332 return ret; 1333 1334 /* Silicon Errata Sheet (DS80000691D or DS80000692D): 1335 * Whenever the device's Asymmetric Pause capability is set to 1, 1336 * link-up may fail after a link-up to link-down transition. 1337 * 1338 * The Errata Sheet is for ksz9031, but ksz9021 has the same issue 1339 * 1340 * Workaround: 1341 * Do not enable the Asymmetric Pause capability bit. 1342 */ 1343 linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported); 1344 1345 /* We force setting the Pause capability as the core will force the 1346 * Asymmetric Pause capability to 1 otherwise. 1347 */ 1348 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported); 1349 1350 return 0; 1351 } 1352 1353 static int ksz9031_read_status(struct phy_device *phydev) 1354 { 1355 int err; 1356 int regval; 1357 1358 err = genphy_read_status(phydev); 1359 if (err) 1360 return err; 1361 1362 /* Make sure the PHY is not broken. Read idle error count, 1363 * and reset the PHY if it is maxed out. 1364 */ 1365 regval = phy_read(phydev, MII_STAT1000); 1366 if ((regval & 0xFF) == 0xFF) { 1367 phy_init_hw(phydev); 1368 phydev->link = 0; 1369 if (phydev->drv->config_intr && phy_interrupt_is_valid(phydev)) 1370 phydev->drv->config_intr(phydev); 1371 return genphy_config_aneg(phydev); 1372 } 1373 1374 return 0; 1375 } 1376 1377 static int ksz9x31_cable_test_start(struct phy_device *phydev) 1378 { 1379 struct kszphy_priv *priv = phydev->priv; 1380 int ret; 1381 1382 /* KSZ9131RNX, DS00002841B-page 38, 4.14 LinkMD (R) Cable Diagnostic 1383 * Prior to running the cable diagnostics, Auto-negotiation should 1384 * be disabled, full duplex set and the link speed set to 1000Mbps 1385 * via the Basic Control Register. 1386 */ 1387 ret = phy_modify(phydev, MII_BMCR, 1388 BMCR_SPEED1000 | BMCR_FULLDPLX | 1389 BMCR_ANENABLE | BMCR_SPEED100, 1390 BMCR_SPEED1000 | BMCR_FULLDPLX); 1391 if (ret) 1392 return ret; 1393 1394 /* KSZ9131RNX, DS00002841B-page 38, 4.14 LinkMD (R) Cable Diagnostic 1395 * The Master-Slave configuration should be set to Slave by writing 1396 * a value of 0x1000 to the Auto-Negotiation Master Slave Control 1397 * Register. 1398 */ 1399 ret = phy_read(phydev, MII_CTRL1000); 1400 if (ret < 0) 1401 return ret; 1402 1403 /* Cache these bits, they need to be restored once LinkMD finishes. */ 1404 priv->vct_ctrl1000 = ret & (CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER); 1405 ret &= ~(CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER); 1406 ret |= CTL1000_ENABLE_MASTER; 1407 1408 return phy_write(phydev, MII_CTRL1000, ret); 1409 } 1410 1411 static int ksz9x31_cable_test_result_trans(u16 status) 1412 { 1413 switch (FIELD_GET(KSZ9x31_LMD_VCT_ST_MASK, status)) { 1414 case KSZ9x31_LMD_VCT_ST_NORMAL: 1415 return ETHTOOL_A_CABLE_RESULT_CODE_OK; 1416 case KSZ9x31_LMD_VCT_ST_OPEN: 1417 return ETHTOOL_A_CABLE_RESULT_CODE_OPEN; 1418 case KSZ9x31_LMD_VCT_ST_SHORT: 1419 return ETHTOOL_A_CABLE_RESULT_CODE_SAME_SHORT; 1420 case KSZ9x31_LMD_VCT_ST_FAIL: 1421 fallthrough; 1422 default: 1423 return ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC; 1424 } 1425 } 1426 1427 static bool ksz9x31_cable_test_failed(u16 status) 1428 { 1429 int stat = FIELD_GET(KSZ9x31_LMD_VCT_ST_MASK, status); 1430 1431 return stat == KSZ9x31_LMD_VCT_ST_FAIL; 1432 } 1433 1434 static bool ksz9x31_cable_test_fault_length_valid(u16 status) 1435 { 1436 switch (FIELD_GET(KSZ9x31_LMD_VCT_ST_MASK, status)) { 1437 case KSZ9x31_LMD_VCT_ST_OPEN: 1438 fallthrough; 1439 case KSZ9x31_LMD_VCT_ST_SHORT: 1440 return true; 1441 } 1442 return false; 1443 } 1444 1445 static int ksz9x31_cable_test_fault_length(struct phy_device *phydev, u16 stat) 1446 { 1447 int dt = FIELD_GET(KSZ9x31_LMD_VCT_DATA_MASK, stat); 1448 1449 /* KSZ9131RNX, DS00002841B-page 38, 4.14 LinkMD (R) Cable Diagnostic 1450 * 1451 * distance to fault = (VCT_DATA - 22) * 4 / cable propagation velocity 1452 */ 1453 if ((phydev->phy_id & MICREL_PHY_ID_MASK) == PHY_ID_KSZ9131) 1454 dt = clamp(dt - 22, 0, 255); 1455 1456 return (dt * 400) / 10; 1457 } 1458 1459 static int ksz9x31_cable_test_wait_for_completion(struct phy_device *phydev) 1460 { 1461 int val, ret; 1462 1463 ret = phy_read_poll_timeout(phydev, KSZ9x31_LMD, val, 1464 !(val & KSZ9x31_LMD_VCT_EN), 1465 30000, 100000, true); 1466 1467 return ret < 0 ? ret : 0; 1468 } 1469 1470 static int ksz9x31_cable_test_get_pair(int pair) 1471 { 1472 static const int ethtool_pair[] = { 1473 ETHTOOL_A_CABLE_PAIR_A, 1474 ETHTOOL_A_CABLE_PAIR_B, 1475 ETHTOOL_A_CABLE_PAIR_C, 1476 ETHTOOL_A_CABLE_PAIR_D, 1477 }; 1478 1479 return ethtool_pair[pair]; 1480 } 1481 1482 static int ksz9x31_cable_test_one_pair(struct phy_device *phydev, int pair) 1483 { 1484 int ret, val; 1485 1486 /* KSZ9131RNX, DS00002841B-page 38, 4.14 LinkMD (R) Cable Diagnostic 1487 * To test each individual cable pair, set the cable pair in the Cable 1488 * Diagnostics Test Pair (VCT_PAIR[1:0]) field of the LinkMD Cable 1489 * Diagnostic Register, along with setting the Cable Diagnostics Test 1490 * Enable (VCT_EN) bit. The Cable Diagnostics Test Enable (VCT_EN) bit 1491 * will self clear when the test is concluded. 1492 */ 1493 ret = phy_write(phydev, KSZ9x31_LMD, 1494 KSZ9x31_LMD_VCT_EN | KSZ9x31_LMD_VCT_PAIR(pair)); 1495 if (ret) 1496 return ret; 1497 1498 ret = ksz9x31_cable_test_wait_for_completion(phydev); 1499 if (ret) 1500 return ret; 1501 1502 val = phy_read(phydev, KSZ9x31_LMD); 1503 if (val < 0) 1504 return val; 1505 1506 if (ksz9x31_cable_test_failed(val)) 1507 return -EAGAIN; 1508 1509 ret = ethnl_cable_test_result(phydev, 1510 ksz9x31_cable_test_get_pair(pair), 1511 ksz9x31_cable_test_result_trans(val)); 1512 if (ret) 1513 return ret; 1514 1515 if (!ksz9x31_cable_test_fault_length_valid(val)) 1516 return 0; 1517 1518 return ethnl_cable_test_fault_length(phydev, 1519 ksz9x31_cable_test_get_pair(pair), 1520 ksz9x31_cable_test_fault_length(phydev, val)); 1521 } 1522 1523 static int ksz9x31_cable_test_get_status(struct phy_device *phydev, 1524 bool *finished) 1525 { 1526 struct kszphy_priv *priv = phydev->priv; 1527 unsigned long pair_mask = 0xf; 1528 int retries = 20; 1529 int pair, ret, rv; 1530 1531 *finished = false; 1532 1533 /* Try harder if link partner is active */ 1534 while (pair_mask && retries--) { 1535 for_each_set_bit(pair, &pair_mask, 4) { 1536 ret = ksz9x31_cable_test_one_pair(phydev, pair); 1537 if (ret == -EAGAIN) 1538 continue; 1539 if (ret < 0) 1540 return ret; 1541 clear_bit(pair, &pair_mask); 1542 } 1543 /* If link partner is in autonegotiation mode it will send 2ms 1544 * of FLPs with at least 6ms of silence. 1545 * Add 2ms sleep to have better chances to hit this silence. 1546 */ 1547 if (pair_mask) 1548 usleep_range(2000, 3000); 1549 } 1550 1551 /* Report remaining unfinished pair result as unknown. */ 1552 for_each_set_bit(pair, &pair_mask, 4) { 1553 ret = ethnl_cable_test_result(phydev, 1554 ksz9x31_cable_test_get_pair(pair), 1555 ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC); 1556 } 1557 1558 *finished = true; 1559 1560 /* Restore cached bits from before LinkMD got started. */ 1561 rv = phy_modify(phydev, MII_CTRL1000, 1562 CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER, 1563 priv->vct_ctrl1000); 1564 if (rv) 1565 return rv; 1566 1567 return ret; 1568 } 1569 1570 static int ksz8873mll_config_aneg(struct phy_device *phydev) 1571 { 1572 return 0; 1573 } 1574 1575 static int ksz886x_config_mdix(struct phy_device *phydev, u8 ctrl) 1576 { 1577 u16 val; 1578 1579 switch (ctrl) { 1580 case ETH_TP_MDI: 1581 val = KSZ886X_BMCR_DISABLE_AUTO_MDIX; 1582 break; 1583 case ETH_TP_MDI_X: 1584 /* Note: The naming of the bit KSZ886X_BMCR_FORCE_MDI is bit 1585 * counter intuitive, the "-X" in "1 = Force MDI" in the data 1586 * sheet seems to be missing: 1587 * 1 = Force MDI (sic!) (transmit on RX+/RX- pins) 1588 * 0 = Normal operation (transmit on TX+/TX- pins) 1589 */ 1590 val = KSZ886X_BMCR_DISABLE_AUTO_MDIX | KSZ886X_BMCR_FORCE_MDI; 1591 break; 1592 case ETH_TP_MDI_AUTO: 1593 val = 0; 1594 break; 1595 default: 1596 return 0; 1597 } 1598 1599 return phy_modify(phydev, MII_BMCR, 1600 KSZ886X_BMCR_HP_MDIX | KSZ886X_BMCR_FORCE_MDI | 1601 KSZ886X_BMCR_DISABLE_AUTO_MDIX, 1602 KSZ886X_BMCR_HP_MDIX | val); 1603 } 1604 1605 static int ksz886x_config_aneg(struct phy_device *phydev) 1606 { 1607 int ret; 1608 1609 ret = genphy_config_aneg(phydev); 1610 if (ret) 1611 return ret; 1612 1613 /* The MDI-X configuration is automatically changed by the PHY after 1614 * switching from autoneg off to on. So, take MDI-X configuration under 1615 * own control and set it after autoneg configuration was done. 1616 */ 1617 return ksz886x_config_mdix(phydev, phydev->mdix_ctrl); 1618 } 1619 1620 static int ksz886x_mdix_update(struct phy_device *phydev) 1621 { 1622 int ret; 1623 1624 ret = phy_read(phydev, MII_BMCR); 1625 if (ret < 0) 1626 return ret; 1627 1628 if (ret & KSZ886X_BMCR_DISABLE_AUTO_MDIX) { 1629 if (ret & KSZ886X_BMCR_FORCE_MDI) 1630 phydev->mdix_ctrl = ETH_TP_MDI_X; 1631 else 1632 phydev->mdix_ctrl = ETH_TP_MDI; 1633 } else { 1634 phydev->mdix_ctrl = ETH_TP_MDI_AUTO; 1635 } 1636 1637 ret = phy_read(phydev, MII_KSZPHY_CTRL); 1638 if (ret < 0) 1639 return ret; 1640 1641 /* Same reverse logic as KSZ886X_BMCR_FORCE_MDI */ 1642 if (ret & KSZ886X_CTRL_MDIX_STAT) 1643 phydev->mdix = ETH_TP_MDI_X; 1644 else 1645 phydev->mdix = ETH_TP_MDI; 1646 1647 return 0; 1648 } 1649 1650 static int ksz886x_read_status(struct phy_device *phydev) 1651 { 1652 int ret; 1653 1654 ret = ksz886x_mdix_update(phydev); 1655 if (ret < 0) 1656 return ret; 1657 1658 return genphy_read_status(phydev); 1659 } 1660 1661 static int kszphy_get_sset_count(struct phy_device *phydev) 1662 { 1663 return ARRAY_SIZE(kszphy_hw_stats); 1664 } 1665 1666 static void kszphy_get_strings(struct phy_device *phydev, u8 *data) 1667 { 1668 int i; 1669 1670 for (i = 0; i < ARRAY_SIZE(kszphy_hw_stats); i++) { 1671 strscpy(data + i * ETH_GSTRING_LEN, 1672 kszphy_hw_stats[i].string, ETH_GSTRING_LEN); 1673 } 1674 } 1675 1676 static u64 kszphy_get_stat(struct phy_device *phydev, int i) 1677 { 1678 struct kszphy_hw_stat stat = kszphy_hw_stats[i]; 1679 struct kszphy_priv *priv = phydev->priv; 1680 int val; 1681 u64 ret; 1682 1683 val = phy_read(phydev, stat.reg); 1684 if (val < 0) { 1685 ret = U64_MAX; 1686 } else { 1687 val = val & ((1 << stat.bits) - 1); 1688 priv->stats[i] += val; 1689 ret = priv->stats[i]; 1690 } 1691 1692 return ret; 1693 } 1694 1695 static void kszphy_get_stats(struct phy_device *phydev, 1696 struct ethtool_stats *stats, u64 *data) 1697 { 1698 int i; 1699 1700 for (i = 0; i < ARRAY_SIZE(kszphy_hw_stats); i++) 1701 data[i] = kszphy_get_stat(phydev, i); 1702 } 1703 1704 static int kszphy_suspend(struct phy_device *phydev) 1705 { 1706 /* Disable PHY Interrupts */ 1707 if (phy_interrupt_is_valid(phydev)) { 1708 phydev->interrupts = PHY_INTERRUPT_DISABLED; 1709 if (phydev->drv->config_intr) 1710 phydev->drv->config_intr(phydev); 1711 } 1712 1713 return genphy_suspend(phydev); 1714 } 1715 1716 static void kszphy_parse_led_mode(struct phy_device *phydev) 1717 { 1718 const struct kszphy_type *type = phydev->drv->driver_data; 1719 const struct device_node *np = phydev->mdio.dev.of_node; 1720 struct kszphy_priv *priv = phydev->priv; 1721 int ret; 1722 1723 if (type && type->led_mode_reg) { 1724 ret = of_property_read_u32(np, "micrel,led-mode", 1725 &priv->led_mode); 1726 1727 if (ret) 1728 priv->led_mode = -1; 1729 1730 if (priv->led_mode > 3) { 1731 phydev_err(phydev, "invalid led mode: 0x%02x\n", 1732 priv->led_mode); 1733 priv->led_mode = -1; 1734 } 1735 } else { 1736 priv->led_mode = -1; 1737 } 1738 } 1739 1740 static int kszphy_resume(struct phy_device *phydev) 1741 { 1742 int ret; 1743 1744 genphy_resume(phydev); 1745 1746 /* After switching from power-down to normal mode, an internal global 1747 * reset is automatically generated. Wait a minimum of 1 ms before 1748 * read/write access to the PHY registers. 1749 */ 1750 usleep_range(1000, 2000); 1751 1752 ret = kszphy_config_reset(phydev); 1753 if (ret) 1754 return ret; 1755 1756 /* Enable PHY Interrupts */ 1757 if (phy_interrupt_is_valid(phydev)) { 1758 phydev->interrupts = PHY_INTERRUPT_ENABLED; 1759 if (phydev->drv->config_intr) 1760 phydev->drv->config_intr(phydev); 1761 } 1762 1763 return 0; 1764 } 1765 1766 static int kszphy_probe(struct phy_device *phydev) 1767 { 1768 const struct kszphy_type *type = phydev->drv->driver_data; 1769 const struct device_node *np = phydev->mdio.dev.of_node; 1770 struct kszphy_priv *priv; 1771 struct clk *clk; 1772 1773 priv = devm_kzalloc(&phydev->mdio.dev, sizeof(*priv), GFP_KERNEL); 1774 if (!priv) 1775 return -ENOMEM; 1776 1777 phydev->priv = priv; 1778 1779 priv->type = type; 1780 1781 kszphy_parse_led_mode(phydev); 1782 1783 clk = devm_clk_get(&phydev->mdio.dev, "rmii-ref"); 1784 /* NOTE: clk may be NULL if building without CONFIG_HAVE_CLK */ 1785 if (!IS_ERR_OR_NULL(clk)) { 1786 unsigned long rate = clk_get_rate(clk); 1787 bool rmii_ref_clk_sel_25_mhz; 1788 1789 if (type) 1790 priv->rmii_ref_clk_sel = type->has_rmii_ref_clk_sel; 1791 rmii_ref_clk_sel_25_mhz = of_property_read_bool(np, 1792 "micrel,rmii-reference-clock-select-25-mhz"); 1793 1794 if (rate > 24500000 && rate < 25500000) { 1795 priv->rmii_ref_clk_sel_val = rmii_ref_clk_sel_25_mhz; 1796 } else if (rate > 49500000 && rate < 50500000) { 1797 priv->rmii_ref_clk_sel_val = !rmii_ref_clk_sel_25_mhz; 1798 } else { 1799 phydev_err(phydev, "Clock rate out of range: %ld\n", 1800 rate); 1801 return -EINVAL; 1802 } 1803 } 1804 1805 if (ksz8041_fiber_mode(phydev)) 1806 phydev->port = PORT_FIBRE; 1807 1808 /* Support legacy board-file configuration */ 1809 if (phydev->dev_flags & MICREL_PHY_50MHZ_CLK) { 1810 priv->rmii_ref_clk_sel = true; 1811 priv->rmii_ref_clk_sel_val = true; 1812 } 1813 1814 return 0; 1815 } 1816 1817 static int lan8814_cable_test_start(struct phy_device *phydev) 1818 { 1819 /* If autoneg is enabled, we won't be able to test cross pair 1820 * short. In this case, the PHY will "detect" a link and 1821 * confuse the internal state machine - disable auto neg here. 1822 * Set the speed to 1000mbit and full duplex. 1823 */ 1824 return phy_modify(phydev, MII_BMCR, BMCR_ANENABLE | BMCR_SPEED100, 1825 BMCR_SPEED1000 | BMCR_FULLDPLX); 1826 } 1827 1828 static int ksz886x_cable_test_start(struct phy_device *phydev) 1829 { 1830 if (phydev->dev_flags & MICREL_KSZ8_P1_ERRATA) 1831 return -EOPNOTSUPP; 1832 1833 /* If autoneg is enabled, we won't be able to test cross pair 1834 * short. In this case, the PHY will "detect" a link and 1835 * confuse the internal state machine - disable auto neg here. 1836 * If autoneg is disabled, we should set the speed to 10mbit. 1837 */ 1838 return phy_clear_bits(phydev, MII_BMCR, BMCR_ANENABLE | BMCR_SPEED100); 1839 } 1840 1841 static int ksz886x_cable_test_result_trans(u16 status, u16 mask) 1842 { 1843 switch (FIELD_GET(mask, status)) { 1844 case KSZ8081_LMD_STAT_NORMAL: 1845 return ETHTOOL_A_CABLE_RESULT_CODE_OK; 1846 case KSZ8081_LMD_STAT_SHORT: 1847 return ETHTOOL_A_CABLE_RESULT_CODE_SAME_SHORT; 1848 case KSZ8081_LMD_STAT_OPEN: 1849 return ETHTOOL_A_CABLE_RESULT_CODE_OPEN; 1850 case KSZ8081_LMD_STAT_FAIL: 1851 fallthrough; 1852 default: 1853 return ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC; 1854 } 1855 } 1856 1857 static bool ksz886x_cable_test_failed(u16 status, u16 mask) 1858 { 1859 return FIELD_GET(mask, status) == 1860 KSZ8081_LMD_STAT_FAIL; 1861 } 1862 1863 static bool ksz886x_cable_test_fault_length_valid(u16 status, u16 mask) 1864 { 1865 switch (FIELD_GET(mask, status)) { 1866 case KSZ8081_LMD_STAT_OPEN: 1867 fallthrough; 1868 case KSZ8081_LMD_STAT_SHORT: 1869 return true; 1870 } 1871 return false; 1872 } 1873 1874 static int ksz886x_cable_test_fault_length(struct phy_device *phydev, u16 status, u16 data_mask) 1875 { 1876 int dt; 1877 1878 /* According to the data sheet the distance to the fault is 1879 * DELTA_TIME * 0.4 meters for ksz phys. 1880 * (DELTA_TIME - 22) * 0.8 for lan8814 phy. 1881 */ 1882 dt = FIELD_GET(data_mask, status); 1883 1884 if ((phydev->phy_id & MICREL_PHY_ID_MASK) == PHY_ID_LAN8814) 1885 return ((dt - 22) * 800) / 10; 1886 else 1887 return (dt * 400) / 10; 1888 } 1889 1890 static int ksz886x_cable_test_wait_for_completion(struct phy_device *phydev) 1891 { 1892 const struct kszphy_type *type = phydev->drv->driver_data; 1893 int val, ret; 1894 1895 ret = phy_read_poll_timeout(phydev, type->cable_diag_reg, val, 1896 !(val & KSZ8081_LMD_ENABLE_TEST), 1897 30000, 100000, true); 1898 1899 return ret < 0 ? ret : 0; 1900 } 1901 1902 static int lan8814_cable_test_one_pair(struct phy_device *phydev, int pair) 1903 { 1904 static const int ethtool_pair[] = { ETHTOOL_A_CABLE_PAIR_A, 1905 ETHTOOL_A_CABLE_PAIR_B, 1906 ETHTOOL_A_CABLE_PAIR_C, 1907 ETHTOOL_A_CABLE_PAIR_D, 1908 }; 1909 u32 fault_length; 1910 int ret; 1911 int val; 1912 1913 val = KSZ8081_LMD_ENABLE_TEST; 1914 val = val | (pair << LAN8814_PAIR_BIT_SHIFT); 1915 1916 ret = phy_write(phydev, LAN8814_CABLE_DIAG, val); 1917 if (ret < 0) 1918 return ret; 1919 1920 ret = ksz886x_cable_test_wait_for_completion(phydev); 1921 if (ret) 1922 return ret; 1923 1924 val = phy_read(phydev, LAN8814_CABLE_DIAG); 1925 if (val < 0) 1926 return val; 1927 1928 if (ksz886x_cable_test_failed(val, LAN8814_CABLE_DIAG_STAT_MASK)) 1929 return -EAGAIN; 1930 1931 ret = ethnl_cable_test_result(phydev, ethtool_pair[pair], 1932 ksz886x_cable_test_result_trans(val, 1933 LAN8814_CABLE_DIAG_STAT_MASK 1934 )); 1935 if (ret) 1936 return ret; 1937 1938 if (!ksz886x_cable_test_fault_length_valid(val, LAN8814_CABLE_DIAG_STAT_MASK)) 1939 return 0; 1940 1941 fault_length = ksz886x_cable_test_fault_length(phydev, val, 1942 LAN8814_CABLE_DIAG_VCT_DATA_MASK); 1943 1944 return ethnl_cable_test_fault_length(phydev, ethtool_pair[pair], fault_length); 1945 } 1946 1947 static int ksz886x_cable_test_one_pair(struct phy_device *phydev, int pair) 1948 { 1949 static const int ethtool_pair[] = { 1950 ETHTOOL_A_CABLE_PAIR_A, 1951 ETHTOOL_A_CABLE_PAIR_B, 1952 }; 1953 int ret, val, mdix; 1954 u32 fault_length; 1955 1956 /* There is no way to choice the pair, like we do one ksz9031. 1957 * We can workaround this limitation by using the MDI-X functionality. 1958 */ 1959 if (pair == 0) 1960 mdix = ETH_TP_MDI; 1961 else 1962 mdix = ETH_TP_MDI_X; 1963 1964 switch (phydev->phy_id & MICREL_PHY_ID_MASK) { 1965 case PHY_ID_KSZ8081: 1966 ret = ksz8081_config_mdix(phydev, mdix); 1967 break; 1968 case PHY_ID_KSZ886X: 1969 ret = ksz886x_config_mdix(phydev, mdix); 1970 break; 1971 default: 1972 ret = -ENODEV; 1973 } 1974 1975 if (ret) 1976 return ret; 1977 1978 /* Now we are ready to fire. This command will send a 100ns pulse 1979 * to the pair. 1980 */ 1981 ret = phy_write(phydev, KSZ8081_LMD, KSZ8081_LMD_ENABLE_TEST); 1982 if (ret) 1983 return ret; 1984 1985 ret = ksz886x_cable_test_wait_for_completion(phydev); 1986 if (ret) 1987 return ret; 1988 1989 val = phy_read(phydev, KSZ8081_LMD); 1990 if (val < 0) 1991 return val; 1992 1993 if (ksz886x_cable_test_failed(val, KSZ8081_LMD_STAT_MASK)) 1994 return -EAGAIN; 1995 1996 ret = ethnl_cable_test_result(phydev, ethtool_pair[pair], 1997 ksz886x_cable_test_result_trans(val, KSZ8081_LMD_STAT_MASK)); 1998 if (ret) 1999 return ret; 2000 2001 if (!ksz886x_cable_test_fault_length_valid(val, KSZ8081_LMD_STAT_MASK)) 2002 return 0; 2003 2004 fault_length = ksz886x_cable_test_fault_length(phydev, val, KSZ8081_LMD_DELTA_TIME_MASK); 2005 2006 return ethnl_cable_test_fault_length(phydev, ethtool_pair[pair], fault_length); 2007 } 2008 2009 static int ksz886x_cable_test_get_status(struct phy_device *phydev, 2010 bool *finished) 2011 { 2012 const struct kszphy_type *type = phydev->drv->driver_data; 2013 unsigned long pair_mask = type->pair_mask; 2014 int retries = 20; 2015 int pair, ret; 2016 2017 *finished = false; 2018 2019 /* Try harder if link partner is active */ 2020 while (pair_mask && retries--) { 2021 for_each_set_bit(pair, &pair_mask, 4) { 2022 if (type->cable_diag_reg == LAN8814_CABLE_DIAG) 2023 ret = lan8814_cable_test_one_pair(phydev, pair); 2024 else 2025 ret = ksz886x_cable_test_one_pair(phydev, pair); 2026 if (ret == -EAGAIN) 2027 continue; 2028 if (ret < 0) 2029 return ret; 2030 clear_bit(pair, &pair_mask); 2031 } 2032 /* If link partner is in autonegotiation mode it will send 2ms 2033 * of FLPs with at least 6ms of silence. 2034 * Add 2ms sleep to have better chances to hit this silence. 2035 */ 2036 if (pair_mask) 2037 msleep(2); 2038 } 2039 2040 *finished = true; 2041 2042 return ret; 2043 } 2044 2045 #define LAN_EXT_PAGE_ACCESS_CONTROL 0x16 2046 #define LAN_EXT_PAGE_ACCESS_ADDRESS_DATA 0x17 2047 #define LAN_EXT_PAGE_ACCESS_CTRL_EP_FUNC 0x4000 2048 2049 #define LAN8814_QSGMII_SOFT_RESET 0x43 2050 #define LAN8814_QSGMII_SOFT_RESET_BIT BIT(0) 2051 #define LAN8814_QSGMII_PCS1G_ANEG_CONFIG 0x13 2052 #define LAN8814_QSGMII_PCS1G_ANEG_CONFIG_ANEG_ENA BIT(3) 2053 #define LAN8814_ALIGN_SWAP 0x4a 2054 #define LAN8814_ALIGN_TX_A_B_SWAP 0x1 2055 #define LAN8814_ALIGN_TX_A_B_SWAP_MASK GENMASK(2, 0) 2056 2057 #define LAN8804_ALIGN_SWAP 0x4a 2058 #define LAN8804_ALIGN_TX_A_B_SWAP 0x1 2059 #define LAN8804_ALIGN_TX_A_B_SWAP_MASK GENMASK(2, 0) 2060 #define LAN8814_CLOCK_MANAGEMENT 0xd 2061 #define LAN8814_LINK_QUALITY 0x8e 2062 2063 static int lanphy_read_page_reg(struct phy_device *phydev, int page, u32 addr) 2064 { 2065 int data; 2066 2067 phy_lock_mdio_bus(phydev); 2068 __phy_write(phydev, LAN_EXT_PAGE_ACCESS_CONTROL, page); 2069 __phy_write(phydev, LAN_EXT_PAGE_ACCESS_ADDRESS_DATA, addr); 2070 __phy_write(phydev, LAN_EXT_PAGE_ACCESS_CONTROL, 2071 (page | LAN_EXT_PAGE_ACCESS_CTRL_EP_FUNC)); 2072 data = __phy_read(phydev, LAN_EXT_PAGE_ACCESS_ADDRESS_DATA); 2073 phy_unlock_mdio_bus(phydev); 2074 2075 return data; 2076 } 2077 2078 static int lanphy_write_page_reg(struct phy_device *phydev, int page, u16 addr, 2079 u16 val) 2080 { 2081 phy_lock_mdio_bus(phydev); 2082 __phy_write(phydev, LAN_EXT_PAGE_ACCESS_CONTROL, page); 2083 __phy_write(phydev, LAN_EXT_PAGE_ACCESS_ADDRESS_DATA, addr); 2084 __phy_write(phydev, LAN_EXT_PAGE_ACCESS_CONTROL, 2085 page | LAN_EXT_PAGE_ACCESS_CTRL_EP_FUNC); 2086 2087 val = __phy_write(phydev, LAN_EXT_PAGE_ACCESS_ADDRESS_DATA, val); 2088 if (val != 0) 2089 phydev_err(phydev, "Error: phy_write has returned error %d\n", 2090 val); 2091 phy_unlock_mdio_bus(phydev); 2092 return val; 2093 } 2094 2095 static int lan8814_config_ts_intr(struct phy_device *phydev, bool enable) 2096 { 2097 u16 val = 0; 2098 2099 if (enable) 2100 val = PTP_TSU_INT_EN_PTP_TX_TS_EN_ | 2101 PTP_TSU_INT_EN_PTP_TX_TS_OVRFL_EN_ | 2102 PTP_TSU_INT_EN_PTP_RX_TS_EN_ | 2103 PTP_TSU_INT_EN_PTP_RX_TS_OVRFL_EN_; 2104 2105 return lanphy_write_page_reg(phydev, 5, PTP_TSU_INT_EN, val); 2106 } 2107 2108 static void lan8814_ptp_rx_ts_get(struct phy_device *phydev, 2109 u32 *seconds, u32 *nano_seconds, u16 *seq_id) 2110 { 2111 *seconds = lanphy_read_page_reg(phydev, 5, PTP_RX_INGRESS_SEC_HI); 2112 *seconds = (*seconds << 16) | 2113 lanphy_read_page_reg(phydev, 5, PTP_RX_INGRESS_SEC_LO); 2114 2115 *nano_seconds = lanphy_read_page_reg(phydev, 5, PTP_RX_INGRESS_NS_HI); 2116 *nano_seconds = ((*nano_seconds & 0x3fff) << 16) | 2117 lanphy_read_page_reg(phydev, 5, PTP_RX_INGRESS_NS_LO); 2118 2119 *seq_id = lanphy_read_page_reg(phydev, 5, PTP_RX_MSG_HEADER2); 2120 } 2121 2122 static void lan8814_ptp_tx_ts_get(struct phy_device *phydev, 2123 u32 *seconds, u32 *nano_seconds, u16 *seq_id) 2124 { 2125 *seconds = lanphy_read_page_reg(phydev, 5, PTP_TX_EGRESS_SEC_HI); 2126 *seconds = *seconds << 16 | 2127 lanphy_read_page_reg(phydev, 5, PTP_TX_EGRESS_SEC_LO); 2128 2129 *nano_seconds = lanphy_read_page_reg(phydev, 5, PTP_TX_EGRESS_NS_HI); 2130 *nano_seconds = ((*nano_seconds & 0x3fff) << 16) | 2131 lanphy_read_page_reg(phydev, 5, PTP_TX_EGRESS_NS_LO); 2132 2133 *seq_id = lanphy_read_page_reg(phydev, 5, PTP_TX_MSG_HEADER2); 2134 } 2135 2136 static int lan8814_ts_info(struct mii_timestamper *mii_ts, struct ethtool_ts_info *info) 2137 { 2138 struct kszphy_ptp_priv *ptp_priv = container_of(mii_ts, struct kszphy_ptp_priv, mii_ts); 2139 struct phy_device *phydev = ptp_priv->phydev; 2140 struct lan8814_shared_priv *shared = phydev->shared->priv; 2141 2142 info->so_timestamping = SOF_TIMESTAMPING_TX_HARDWARE | 2143 SOF_TIMESTAMPING_RX_HARDWARE | 2144 SOF_TIMESTAMPING_RAW_HARDWARE; 2145 2146 info->phc_index = ptp_clock_index(shared->ptp_clock); 2147 2148 info->tx_types = 2149 (1 << HWTSTAMP_TX_OFF) | 2150 (1 << HWTSTAMP_TX_ON) | 2151 (1 << HWTSTAMP_TX_ONESTEP_SYNC); 2152 2153 info->rx_filters = 2154 (1 << HWTSTAMP_FILTER_NONE) | 2155 (1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT) | 2156 (1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT) | 2157 (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) | 2158 (1 << HWTSTAMP_FILTER_PTP_V2_EVENT); 2159 2160 return 0; 2161 } 2162 2163 static void lan8814_flush_fifo(struct phy_device *phydev, bool egress) 2164 { 2165 int i; 2166 2167 for (i = 0; i < FIFO_SIZE; ++i) 2168 lanphy_read_page_reg(phydev, 5, 2169 egress ? PTP_TX_MSG_HEADER2 : PTP_RX_MSG_HEADER2); 2170 2171 /* Read to clear overflow status bit */ 2172 lanphy_read_page_reg(phydev, 5, PTP_TSU_INT_STS); 2173 } 2174 2175 static int lan8814_hwtstamp(struct mii_timestamper *mii_ts, struct ifreq *ifr) 2176 { 2177 struct kszphy_ptp_priv *ptp_priv = 2178 container_of(mii_ts, struct kszphy_ptp_priv, mii_ts); 2179 struct phy_device *phydev = ptp_priv->phydev; 2180 struct lan8814_shared_priv *shared = phydev->shared->priv; 2181 struct lan8814_ptp_rx_ts *rx_ts, *tmp; 2182 struct hwtstamp_config config; 2183 int txcfg = 0, rxcfg = 0; 2184 int pkt_ts_enable; 2185 2186 if (copy_from_user(&config, ifr->ifr_data, sizeof(config))) 2187 return -EFAULT; 2188 2189 ptp_priv->hwts_tx_type = config.tx_type; 2190 ptp_priv->rx_filter = config.rx_filter; 2191 2192 switch (config.rx_filter) { 2193 case HWTSTAMP_FILTER_NONE: 2194 ptp_priv->layer = 0; 2195 ptp_priv->version = 0; 2196 break; 2197 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT: 2198 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC: 2199 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ: 2200 ptp_priv->layer = PTP_CLASS_L4; 2201 ptp_priv->version = PTP_CLASS_V2; 2202 break; 2203 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT: 2204 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC: 2205 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ: 2206 ptp_priv->layer = PTP_CLASS_L2; 2207 ptp_priv->version = PTP_CLASS_V2; 2208 break; 2209 case HWTSTAMP_FILTER_PTP_V2_EVENT: 2210 case HWTSTAMP_FILTER_PTP_V2_SYNC: 2211 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ: 2212 ptp_priv->layer = PTP_CLASS_L4 | PTP_CLASS_L2; 2213 ptp_priv->version = PTP_CLASS_V2; 2214 break; 2215 default: 2216 return -ERANGE; 2217 } 2218 2219 if (ptp_priv->layer & PTP_CLASS_L2) { 2220 rxcfg = PTP_RX_PARSE_CONFIG_LAYER2_EN_; 2221 txcfg = PTP_TX_PARSE_CONFIG_LAYER2_EN_; 2222 } else if (ptp_priv->layer & PTP_CLASS_L4) { 2223 rxcfg |= PTP_RX_PARSE_CONFIG_IPV4_EN_ | PTP_RX_PARSE_CONFIG_IPV6_EN_; 2224 txcfg |= PTP_TX_PARSE_CONFIG_IPV4_EN_ | PTP_TX_PARSE_CONFIG_IPV6_EN_; 2225 } 2226 lanphy_write_page_reg(ptp_priv->phydev, 5, PTP_RX_PARSE_CONFIG, rxcfg); 2227 lanphy_write_page_reg(ptp_priv->phydev, 5, PTP_TX_PARSE_CONFIG, txcfg); 2228 2229 pkt_ts_enable = PTP_TIMESTAMP_EN_SYNC_ | PTP_TIMESTAMP_EN_DREQ_ | 2230 PTP_TIMESTAMP_EN_PDREQ_ | PTP_TIMESTAMP_EN_PDRES_; 2231 lanphy_write_page_reg(ptp_priv->phydev, 5, PTP_RX_TIMESTAMP_EN, pkt_ts_enable); 2232 lanphy_write_page_reg(ptp_priv->phydev, 5, PTP_TX_TIMESTAMP_EN, pkt_ts_enable); 2233 2234 if (ptp_priv->hwts_tx_type == HWTSTAMP_TX_ONESTEP_SYNC) 2235 lanphy_write_page_reg(ptp_priv->phydev, 5, PTP_TX_MOD, 2236 PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_); 2237 2238 if (config.rx_filter != HWTSTAMP_FILTER_NONE) 2239 lan8814_config_ts_intr(ptp_priv->phydev, true); 2240 else 2241 lan8814_config_ts_intr(ptp_priv->phydev, false); 2242 2243 mutex_lock(&shared->shared_lock); 2244 if (config.rx_filter != HWTSTAMP_FILTER_NONE) 2245 shared->ref++; 2246 else 2247 shared->ref--; 2248 2249 if (shared->ref) 2250 lanphy_write_page_reg(ptp_priv->phydev, 4, PTP_CMD_CTL, 2251 PTP_CMD_CTL_PTP_ENABLE_); 2252 else 2253 lanphy_write_page_reg(ptp_priv->phydev, 4, PTP_CMD_CTL, 2254 PTP_CMD_CTL_PTP_DISABLE_); 2255 mutex_unlock(&shared->shared_lock); 2256 2257 /* In case of multiple starts and stops, these needs to be cleared */ 2258 list_for_each_entry_safe(rx_ts, tmp, &ptp_priv->rx_ts_list, list) { 2259 list_del(&rx_ts->list); 2260 kfree(rx_ts); 2261 } 2262 skb_queue_purge(&ptp_priv->rx_queue); 2263 skb_queue_purge(&ptp_priv->tx_queue); 2264 2265 lan8814_flush_fifo(ptp_priv->phydev, false); 2266 lan8814_flush_fifo(ptp_priv->phydev, true); 2267 2268 return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ? -EFAULT : 0; 2269 } 2270 2271 static void lan8814_txtstamp(struct mii_timestamper *mii_ts, 2272 struct sk_buff *skb, int type) 2273 { 2274 struct kszphy_ptp_priv *ptp_priv = container_of(mii_ts, struct kszphy_ptp_priv, mii_ts); 2275 2276 switch (ptp_priv->hwts_tx_type) { 2277 case HWTSTAMP_TX_ONESTEP_SYNC: 2278 if (ptp_msg_is_sync(skb, type)) { 2279 kfree_skb(skb); 2280 return; 2281 } 2282 fallthrough; 2283 case HWTSTAMP_TX_ON: 2284 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; 2285 skb_queue_tail(&ptp_priv->tx_queue, skb); 2286 break; 2287 case HWTSTAMP_TX_OFF: 2288 default: 2289 kfree_skb(skb); 2290 break; 2291 } 2292 } 2293 2294 static void lan8814_get_sig_rx(struct sk_buff *skb, u16 *sig) 2295 { 2296 struct ptp_header *ptp_header; 2297 u32 type; 2298 2299 skb_push(skb, ETH_HLEN); 2300 type = ptp_classify_raw(skb); 2301 ptp_header = ptp_parse_header(skb, type); 2302 skb_pull_inline(skb, ETH_HLEN); 2303 2304 *sig = (__force u16)(ntohs(ptp_header->sequence_id)); 2305 } 2306 2307 static bool lan8814_match_rx_ts(struct kszphy_ptp_priv *ptp_priv, 2308 struct sk_buff *skb) 2309 { 2310 struct skb_shared_hwtstamps *shhwtstamps; 2311 struct lan8814_ptp_rx_ts *rx_ts, *tmp; 2312 unsigned long flags; 2313 bool ret = false; 2314 u16 skb_sig; 2315 2316 lan8814_get_sig_rx(skb, &skb_sig); 2317 2318 /* Iterate over all RX timestamps and match it with the received skbs */ 2319 spin_lock_irqsave(&ptp_priv->rx_ts_lock, flags); 2320 list_for_each_entry_safe(rx_ts, tmp, &ptp_priv->rx_ts_list, list) { 2321 /* Check if we found the signature we were looking for. */ 2322 if (memcmp(&skb_sig, &rx_ts->seq_id, sizeof(rx_ts->seq_id))) 2323 continue; 2324 2325 shhwtstamps = skb_hwtstamps(skb); 2326 memset(shhwtstamps, 0, sizeof(*shhwtstamps)); 2327 shhwtstamps->hwtstamp = ktime_set(rx_ts->seconds, 2328 rx_ts->nsec); 2329 list_del(&rx_ts->list); 2330 kfree(rx_ts); 2331 2332 ret = true; 2333 break; 2334 } 2335 spin_unlock_irqrestore(&ptp_priv->rx_ts_lock, flags); 2336 2337 if (ret) 2338 netif_rx(skb); 2339 return ret; 2340 } 2341 2342 static bool lan8814_rxtstamp(struct mii_timestamper *mii_ts, struct sk_buff *skb, int type) 2343 { 2344 struct kszphy_ptp_priv *ptp_priv = 2345 container_of(mii_ts, struct kszphy_ptp_priv, mii_ts); 2346 2347 if (ptp_priv->rx_filter == HWTSTAMP_FILTER_NONE || 2348 type == PTP_CLASS_NONE) 2349 return false; 2350 2351 if ((type & ptp_priv->version) == 0 || (type & ptp_priv->layer) == 0) 2352 return false; 2353 2354 /* If we failed to match then add it to the queue for when the timestamp 2355 * will come 2356 */ 2357 if (!lan8814_match_rx_ts(ptp_priv, skb)) 2358 skb_queue_tail(&ptp_priv->rx_queue, skb); 2359 2360 return true; 2361 } 2362 2363 static void lan8814_ptp_clock_set(struct phy_device *phydev, 2364 u32 seconds, u32 nano_seconds) 2365 { 2366 u32 sec_low, sec_high, nsec_low, nsec_high; 2367 2368 sec_low = seconds & 0xffff; 2369 sec_high = (seconds >> 16) & 0xffff; 2370 nsec_low = nano_seconds & 0xffff; 2371 nsec_high = (nano_seconds >> 16) & 0x3fff; 2372 2373 lanphy_write_page_reg(phydev, 4, PTP_CLOCK_SET_SEC_LO, sec_low); 2374 lanphy_write_page_reg(phydev, 4, PTP_CLOCK_SET_SEC_MID, sec_high); 2375 lanphy_write_page_reg(phydev, 4, PTP_CLOCK_SET_NS_LO, nsec_low); 2376 lanphy_write_page_reg(phydev, 4, PTP_CLOCK_SET_NS_HI, nsec_high); 2377 2378 lanphy_write_page_reg(phydev, 4, PTP_CMD_CTL, PTP_CMD_CTL_PTP_CLOCK_LOAD_); 2379 } 2380 2381 static void lan8814_ptp_clock_get(struct phy_device *phydev, 2382 u32 *seconds, u32 *nano_seconds) 2383 { 2384 lanphy_write_page_reg(phydev, 4, PTP_CMD_CTL, PTP_CMD_CTL_PTP_CLOCK_READ_); 2385 2386 *seconds = lanphy_read_page_reg(phydev, 4, PTP_CLOCK_READ_SEC_MID); 2387 *seconds = (*seconds << 16) | 2388 lanphy_read_page_reg(phydev, 4, PTP_CLOCK_READ_SEC_LO); 2389 2390 *nano_seconds = lanphy_read_page_reg(phydev, 4, PTP_CLOCK_READ_NS_HI); 2391 *nano_seconds = ((*nano_seconds & 0x3fff) << 16) | 2392 lanphy_read_page_reg(phydev, 4, PTP_CLOCK_READ_NS_LO); 2393 } 2394 2395 static int lan8814_ptpci_gettime64(struct ptp_clock_info *ptpci, 2396 struct timespec64 *ts) 2397 { 2398 struct lan8814_shared_priv *shared = container_of(ptpci, struct lan8814_shared_priv, 2399 ptp_clock_info); 2400 struct phy_device *phydev = shared->phydev; 2401 u32 nano_seconds; 2402 u32 seconds; 2403 2404 mutex_lock(&shared->shared_lock); 2405 lan8814_ptp_clock_get(phydev, &seconds, &nano_seconds); 2406 mutex_unlock(&shared->shared_lock); 2407 ts->tv_sec = seconds; 2408 ts->tv_nsec = nano_seconds; 2409 2410 return 0; 2411 } 2412 2413 static int lan8814_ptpci_settime64(struct ptp_clock_info *ptpci, 2414 const struct timespec64 *ts) 2415 { 2416 struct lan8814_shared_priv *shared = container_of(ptpci, struct lan8814_shared_priv, 2417 ptp_clock_info); 2418 struct phy_device *phydev = shared->phydev; 2419 2420 mutex_lock(&shared->shared_lock); 2421 lan8814_ptp_clock_set(phydev, ts->tv_sec, ts->tv_nsec); 2422 mutex_unlock(&shared->shared_lock); 2423 2424 return 0; 2425 } 2426 2427 static void lan8814_ptp_clock_step(struct phy_device *phydev, 2428 s64 time_step_ns) 2429 { 2430 u32 nano_seconds_step; 2431 u64 abs_time_step_ns; 2432 u32 unsigned_seconds; 2433 u32 nano_seconds; 2434 u32 remainder; 2435 s32 seconds; 2436 2437 if (time_step_ns > 15000000000LL) { 2438 /* convert to clock set */ 2439 lan8814_ptp_clock_get(phydev, &unsigned_seconds, &nano_seconds); 2440 unsigned_seconds += div_u64_rem(time_step_ns, 1000000000LL, 2441 &remainder); 2442 nano_seconds += remainder; 2443 if (nano_seconds >= 1000000000) { 2444 unsigned_seconds++; 2445 nano_seconds -= 1000000000; 2446 } 2447 lan8814_ptp_clock_set(phydev, unsigned_seconds, nano_seconds); 2448 return; 2449 } else if (time_step_ns < -15000000000LL) { 2450 /* convert to clock set */ 2451 time_step_ns = -time_step_ns; 2452 2453 lan8814_ptp_clock_get(phydev, &unsigned_seconds, &nano_seconds); 2454 unsigned_seconds -= div_u64_rem(time_step_ns, 1000000000LL, 2455 &remainder); 2456 nano_seconds_step = remainder; 2457 if (nano_seconds < nano_seconds_step) { 2458 unsigned_seconds--; 2459 nano_seconds += 1000000000; 2460 } 2461 nano_seconds -= nano_seconds_step; 2462 lan8814_ptp_clock_set(phydev, unsigned_seconds, 2463 nano_seconds); 2464 return; 2465 } 2466 2467 /* do clock step */ 2468 if (time_step_ns >= 0) { 2469 abs_time_step_ns = (u64)time_step_ns; 2470 seconds = (s32)div_u64_rem(abs_time_step_ns, 1000000000, 2471 &remainder); 2472 nano_seconds = remainder; 2473 } else { 2474 abs_time_step_ns = (u64)(-time_step_ns); 2475 seconds = -((s32)div_u64_rem(abs_time_step_ns, 1000000000, 2476 &remainder)); 2477 nano_seconds = remainder; 2478 if (nano_seconds > 0) { 2479 /* subtracting nano seconds is not allowed 2480 * convert to subtracting from seconds, 2481 * and adding to nanoseconds 2482 */ 2483 seconds--; 2484 nano_seconds = (1000000000 - nano_seconds); 2485 } 2486 } 2487 2488 if (nano_seconds > 0) { 2489 /* add 8 ns to cover the likely normal increment */ 2490 nano_seconds += 8; 2491 } 2492 2493 if (nano_seconds >= 1000000000) { 2494 /* carry into seconds */ 2495 seconds++; 2496 nano_seconds -= 1000000000; 2497 } 2498 2499 while (seconds) { 2500 if (seconds > 0) { 2501 u32 adjustment_value = (u32)seconds; 2502 u16 adjustment_value_lo, adjustment_value_hi; 2503 2504 if (adjustment_value > 0xF) 2505 adjustment_value = 0xF; 2506 2507 adjustment_value_lo = adjustment_value & 0xffff; 2508 adjustment_value_hi = (adjustment_value >> 16) & 0x3fff; 2509 2510 lanphy_write_page_reg(phydev, 4, PTP_LTC_STEP_ADJ_LO, 2511 adjustment_value_lo); 2512 lanphy_write_page_reg(phydev, 4, PTP_LTC_STEP_ADJ_HI, 2513 PTP_LTC_STEP_ADJ_DIR_ | 2514 adjustment_value_hi); 2515 seconds -= ((s32)adjustment_value); 2516 } else { 2517 u32 adjustment_value = (u32)(-seconds); 2518 u16 adjustment_value_lo, adjustment_value_hi; 2519 2520 if (adjustment_value > 0xF) 2521 adjustment_value = 0xF; 2522 2523 adjustment_value_lo = adjustment_value & 0xffff; 2524 adjustment_value_hi = (adjustment_value >> 16) & 0x3fff; 2525 2526 lanphy_write_page_reg(phydev, 4, PTP_LTC_STEP_ADJ_LO, 2527 adjustment_value_lo); 2528 lanphy_write_page_reg(phydev, 4, PTP_LTC_STEP_ADJ_HI, 2529 adjustment_value_hi); 2530 seconds += ((s32)adjustment_value); 2531 } 2532 lanphy_write_page_reg(phydev, 4, PTP_CMD_CTL, 2533 PTP_CMD_CTL_PTP_LTC_STEP_SEC_); 2534 } 2535 if (nano_seconds) { 2536 u16 nano_seconds_lo; 2537 u16 nano_seconds_hi; 2538 2539 nano_seconds_lo = nano_seconds & 0xffff; 2540 nano_seconds_hi = (nano_seconds >> 16) & 0x3fff; 2541 2542 lanphy_write_page_reg(phydev, 4, PTP_LTC_STEP_ADJ_LO, 2543 nano_seconds_lo); 2544 lanphy_write_page_reg(phydev, 4, PTP_LTC_STEP_ADJ_HI, 2545 PTP_LTC_STEP_ADJ_DIR_ | 2546 nano_seconds_hi); 2547 lanphy_write_page_reg(phydev, 4, PTP_CMD_CTL, 2548 PTP_CMD_CTL_PTP_LTC_STEP_NSEC_); 2549 } 2550 } 2551 2552 static int lan8814_ptpci_adjtime(struct ptp_clock_info *ptpci, s64 delta) 2553 { 2554 struct lan8814_shared_priv *shared = container_of(ptpci, struct lan8814_shared_priv, 2555 ptp_clock_info); 2556 struct phy_device *phydev = shared->phydev; 2557 2558 mutex_lock(&shared->shared_lock); 2559 lan8814_ptp_clock_step(phydev, delta); 2560 mutex_unlock(&shared->shared_lock); 2561 2562 return 0; 2563 } 2564 2565 static int lan8814_ptpci_adjfine(struct ptp_clock_info *ptpci, long scaled_ppm) 2566 { 2567 struct lan8814_shared_priv *shared = container_of(ptpci, struct lan8814_shared_priv, 2568 ptp_clock_info); 2569 struct phy_device *phydev = shared->phydev; 2570 u16 kszphy_rate_adj_lo, kszphy_rate_adj_hi; 2571 bool positive = true; 2572 u32 kszphy_rate_adj; 2573 2574 if (scaled_ppm < 0) { 2575 scaled_ppm = -scaled_ppm; 2576 positive = false; 2577 } 2578 2579 kszphy_rate_adj = LAN8814_1PPM_FORMAT * (scaled_ppm >> 16); 2580 kszphy_rate_adj += (LAN8814_1PPM_FORMAT * (0xffff & scaled_ppm)) >> 16; 2581 2582 kszphy_rate_adj_lo = kszphy_rate_adj & 0xffff; 2583 kszphy_rate_adj_hi = (kszphy_rate_adj >> 16) & 0x3fff; 2584 2585 if (positive) 2586 kszphy_rate_adj_hi |= PTP_CLOCK_RATE_ADJ_DIR_; 2587 2588 mutex_lock(&shared->shared_lock); 2589 lanphy_write_page_reg(phydev, 4, PTP_CLOCK_RATE_ADJ_HI, kszphy_rate_adj_hi); 2590 lanphy_write_page_reg(phydev, 4, PTP_CLOCK_RATE_ADJ_LO, kszphy_rate_adj_lo); 2591 mutex_unlock(&shared->shared_lock); 2592 2593 return 0; 2594 } 2595 2596 static void lan8814_get_sig_tx(struct sk_buff *skb, u16 *sig) 2597 { 2598 struct ptp_header *ptp_header; 2599 u32 type; 2600 2601 type = ptp_classify_raw(skb); 2602 ptp_header = ptp_parse_header(skb, type); 2603 2604 *sig = (__force u16)(ntohs(ptp_header->sequence_id)); 2605 } 2606 2607 static void lan8814_dequeue_tx_skb(struct kszphy_ptp_priv *ptp_priv) 2608 { 2609 struct phy_device *phydev = ptp_priv->phydev; 2610 struct skb_shared_hwtstamps shhwtstamps; 2611 struct sk_buff *skb, *skb_tmp; 2612 unsigned long flags; 2613 u32 seconds, nsec; 2614 bool ret = false; 2615 u16 skb_sig; 2616 u16 seq_id; 2617 2618 lan8814_ptp_tx_ts_get(phydev, &seconds, &nsec, &seq_id); 2619 2620 spin_lock_irqsave(&ptp_priv->tx_queue.lock, flags); 2621 skb_queue_walk_safe(&ptp_priv->tx_queue, skb, skb_tmp) { 2622 lan8814_get_sig_tx(skb, &skb_sig); 2623 2624 if (memcmp(&skb_sig, &seq_id, sizeof(seq_id))) 2625 continue; 2626 2627 __skb_unlink(skb, &ptp_priv->tx_queue); 2628 ret = true; 2629 break; 2630 } 2631 spin_unlock_irqrestore(&ptp_priv->tx_queue.lock, flags); 2632 2633 if (ret) { 2634 memset(&shhwtstamps, 0, sizeof(shhwtstamps)); 2635 shhwtstamps.hwtstamp = ktime_set(seconds, nsec); 2636 skb_complete_tx_timestamp(skb, &shhwtstamps); 2637 } 2638 } 2639 2640 static void lan8814_get_tx_ts(struct kszphy_ptp_priv *ptp_priv) 2641 { 2642 struct phy_device *phydev = ptp_priv->phydev; 2643 u32 reg; 2644 2645 do { 2646 lan8814_dequeue_tx_skb(ptp_priv); 2647 2648 /* If other timestamps are available in the FIFO, 2649 * process them. 2650 */ 2651 reg = lanphy_read_page_reg(phydev, 5, PTP_CAP_INFO); 2652 } while (PTP_CAP_INFO_TX_TS_CNT_GET_(reg) > 0); 2653 } 2654 2655 static bool lan8814_match_skb(struct kszphy_ptp_priv *ptp_priv, 2656 struct lan8814_ptp_rx_ts *rx_ts) 2657 { 2658 struct skb_shared_hwtstamps *shhwtstamps; 2659 struct sk_buff *skb, *skb_tmp; 2660 unsigned long flags; 2661 bool ret = false; 2662 u16 skb_sig; 2663 2664 spin_lock_irqsave(&ptp_priv->rx_queue.lock, flags); 2665 skb_queue_walk_safe(&ptp_priv->rx_queue, skb, skb_tmp) { 2666 lan8814_get_sig_rx(skb, &skb_sig); 2667 2668 if (memcmp(&skb_sig, &rx_ts->seq_id, sizeof(rx_ts->seq_id))) 2669 continue; 2670 2671 __skb_unlink(skb, &ptp_priv->rx_queue); 2672 2673 ret = true; 2674 break; 2675 } 2676 spin_unlock_irqrestore(&ptp_priv->rx_queue.lock, flags); 2677 2678 if (ret) { 2679 shhwtstamps = skb_hwtstamps(skb); 2680 memset(shhwtstamps, 0, sizeof(*shhwtstamps)); 2681 shhwtstamps->hwtstamp = ktime_set(rx_ts->seconds, rx_ts->nsec); 2682 netif_rx(skb); 2683 } 2684 2685 return ret; 2686 } 2687 2688 static void lan8814_get_rx_ts(struct kszphy_ptp_priv *ptp_priv) 2689 { 2690 struct phy_device *phydev = ptp_priv->phydev; 2691 struct lan8814_ptp_rx_ts *rx_ts; 2692 unsigned long flags; 2693 u32 reg; 2694 2695 do { 2696 rx_ts = kzalloc(sizeof(*rx_ts), GFP_KERNEL); 2697 if (!rx_ts) 2698 return; 2699 2700 lan8814_ptp_rx_ts_get(phydev, &rx_ts->seconds, &rx_ts->nsec, 2701 &rx_ts->seq_id); 2702 2703 /* If we failed to match the skb add it to the queue for when 2704 * the frame will come 2705 */ 2706 if (!lan8814_match_skb(ptp_priv, rx_ts)) { 2707 spin_lock_irqsave(&ptp_priv->rx_ts_lock, flags); 2708 list_add(&rx_ts->list, &ptp_priv->rx_ts_list); 2709 spin_unlock_irqrestore(&ptp_priv->rx_ts_lock, flags); 2710 } else { 2711 kfree(rx_ts); 2712 } 2713 2714 /* If other timestamps are available in the FIFO, 2715 * process them. 2716 */ 2717 reg = lanphy_read_page_reg(phydev, 5, PTP_CAP_INFO); 2718 } while (PTP_CAP_INFO_RX_TS_CNT_GET_(reg) > 0); 2719 } 2720 2721 static void lan8814_handle_ptp_interrupt(struct phy_device *phydev) 2722 { 2723 struct kszphy_priv *priv = phydev->priv; 2724 struct kszphy_ptp_priv *ptp_priv = &priv->ptp_priv; 2725 u16 status; 2726 2727 status = lanphy_read_page_reg(phydev, 5, PTP_TSU_INT_STS); 2728 if (status & PTP_TSU_INT_STS_PTP_TX_TS_EN_) 2729 lan8814_get_tx_ts(ptp_priv); 2730 2731 if (status & PTP_TSU_INT_STS_PTP_RX_TS_EN_) 2732 lan8814_get_rx_ts(ptp_priv); 2733 2734 if (status & PTP_TSU_INT_STS_PTP_TX_TS_OVRFL_INT_) { 2735 lan8814_flush_fifo(phydev, true); 2736 skb_queue_purge(&ptp_priv->tx_queue); 2737 } 2738 2739 if (status & PTP_TSU_INT_STS_PTP_RX_TS_OVRFL_INT_) { 2740 lan8814_flush_fifo(phydev, false); 2741 skb_queue_purge(&ptp_priv->rx_queue); 2742 } 2743 } 2744 2745 static int lan8804_config_init(struct phy_device *phydev) 2746 { 2747 int val; 2748 2749 /* MDI-X setting for swap A,B transmit */ 2750 val = lanphy_read_page_reg(phydev, 2, LAN8804_ALIGN_SWAP); 2751 val &= ~LAN8804_ALIGN_TX_A_B_SWAP_MASK; 2752 val |= LAN8804_ALIGN_TX_A_B_SWAP; 2753 lanphy_write_page_reg(phydev, 2, LAN8804_ALIGN_SWAP, val); 2754 2755 /* Make sure that the PHY will not stop generating the clock when the 2756 * link partner goes down 2757 */ 2758 lanphy_write_page_reg(phydev, 31, LAN8814_CLOCK_MANAGEMENT, 0x27e); 2759 lanphy_read_page_reg(phydev, 1, LAN8814_LINK_QUALITY); 2760 2761 return 0; 2762 } 2763 2764 static irqreturn_t lan8804_handle_interrupt(struct phy_device *phydev) 2765 { 2766 int status; 2767 2768 status = phy_read(phydev, LAN8814_INTS); 2769 if (status < 0) { 2770 phy_error(phydev); 2771 return IRQ_NONE; 2772 } 2773 2774 if (status > 0) 2775 phy_trigger_machine(phydev); 2776 2777 return IRQ_HANDLED; 2778 } 2779 2780 #define LAN8804_OUTPUT_CONTROL 25 2781 #define LAN8804_OUTPUT_CONTROL_INTR_BUFFER BIT(14) 2782 #define LAN8804_CONTROL 31 2783 #define LAN8804_CONTROL_INTR_POLARITY BIT(14) 2784 2785 static int lan8804_config_intr(struct phy_device *phydev) 2786 { 2787 int err; 2788 2789 /* This is an internal PHY of lan966x and is not possible to change the 2790 * polarity on the GIC found in lan966x, therefore change the polarity 2791 * of the interrupt in the PHY from being active low instead of active 2792 * high. 2793 */ 2794 phy_write(phydev, LAN8804_CONTROL, LAN8804_CONTROL_INTR_POLARITY); 2795 2796 /* By default interrupt buffer is open-drain in which case the interrupt 2797 * can be active only low. Therefore change the interrupt buffer to be 2798 * push-pull to be able to change interrupt polarity 2799 */ 2800 phy_write(phydev, LAN8804_OUTPUT_CONTROL, 2801 LAN8804_OUTPUT_CONTROL_INTR_BUFFER); 2802 2803 if (phydev->interrupts == PHY_INTERRUPT_ENABLED) { 2804 err = phy_read(phydev, LAN8814_INTS); 2805 if (err < 0) 2806 return err; 2807 2808 err = phy_write(phydev, LAN8814_INTC, LAN8814_INT_LINK); 2809 if (err) 2810 return err; 2811 } else { 2812 err = phy_write(phydev, LAN8814_INTC, 0); 2813 if (err) 2814 return err; 2815 2816 err = phy_read(phydev, LAN8814_INTS); 2817 if (err < 0) 2818 return err; 2819 } 2820 2821 return 0; 2822 } 2823 2824 static irqreturn_t lan8814_handle_interrupt(struct phy_device *phydev) 2825 { 2826 int irq_status, tsu_irq_status; 2827 int ret = IRQ_NONE; 2828 2829 irq_status = phy_read(phydev, LAN8814_INTS); 2830 if (irq_status < 0) { 2831 phy_error(phydev); 2832 return IRQ_NONE; 2833 } 2834 2835 if (irq_status & LAN8814_INT_LINK) { 2836 phy_trigger_machine(phydev); 2837 ret = IRQ_HANDLED; 2838 } 2839 2840 while (1) { 2841 tsu_irq_status = lanphy_read_page_reg(phydev, 4, 2842 LAN8814_INTR_STS_REG); 2843 2844 if (tsu_irq_status > 0 && 2845 (tsu_irq_status & (LAN8814_INTR_STS_REG_1588_TSU0_ | 2846 LAN8814_INTR_STS_REG_1588_TSU1_ | 2847 LAN8814_INTR_STS_REG_1588_TSU2_ | 2848 LAN8814_INTR_STS_REG_1588_TSU3_))) { 2849 lan8814_handle_ptp_interrupt(phydev); 2850 ret = IRQ_HANDLED; 2851 } else { 2852 break; 2853 } 2854 } 2855 2856 return ret; 2857 } 2858 2859 static int lan8814_ack_interrupt(struct phy_device *phydev) 2860 { 2861 /* bit[12..0] int status, which is a read and clear register. */ 2862 int rc; 2863 2864 rc = phy_read(phydev, LAN8814_INTS); 2865 2866 return (rc < 0) ? rc : 0; 2867 } 2868 2869 static int lan8814_config_intr(struct phy_device *phydev) 2870 { 2871 int err; 2872 2873 lanphy_write_page_reg(phydev, 4, LAN8814_INTR_CTRL_REG, 2874 LAN8814_INTR_CTRL_REG_POLARITY | 2875 LAN8814_INTR_CTRL_REG_INTR_ENABLE); 2876 2877 /* enable / disable interrupts */ 2878 if (phydev->interrupts == PHY_INTERRUPT_ENABLED) { 2879 err = lan8814_ack_interrupt(phydev); 2880 if (err) 2881 return err; 2882 2883 err = phy_write(phydev, LAN8814_INTC, LAN8814_INT_LINK); 2884 } else { 2885 err = phy_write(phydev, LAN8814_INTC, 0); 2886 if (err) 2887 return err; 2888 2889 err = lan8814_ack_interrupt(phydev); 2890 } 2891 2892 return err; 2893 } 2894 2895 static void lan8814_ptp_init(struct phy_device *phydev) 2896 { 2897 struct kszphy_priv *priv = phydev->priv; 2898 struct kszphy_ptp_priv *ptp_priv = &priv->ptp_priv; 2899 u32 temp; 2900 2901 if (!IS_ENABLED(CONFIG_PTP_1588_CLOCK) || 2902 !IS_ENABLED(CONFIG_NETWORK_PHY_TIMESTAMPING)) 2903 return; 2904 2905 lanphy_write_page_reg(phydev, 5, TSU_HARD_RESET, TSU_HARD_RESET_); 2906 2907 temp = lanphy_read_page_reg(phydev, 5, PTP_TX_MOD); 2908 temp |= PTP_TX_MOD_BAD_UDPV4_CHKSUM_FORCE_FCS_DIS_; 2909 lanphy_write_page_reg(phydev, 5, PTP_TX_MOD, temp); 2910 2911 temp = lanphy_read_page_reg(phydev, 5, PTP_RX_MOD); 2912 temp |= PTP_RX_MOD_BAD_UDPV4_CHKSUM_FORCE_FCS_DIS_; 2913 lanphy_write_page_reg(phydev, 5, PTP_RX_MOD, temp); 2914 2915 lanphy_write_page_reg(phydev, 5, PTP_RX_PARSE_CONFIG, 0); 2916 lanphy_write_page_reg(phydev, 5, PTP_TX_PARSE_CONFIG, 0); 2917 2918 /* Removing default registers configs related to L2 and IP */ 2919 lanphy_write_page_reg(phydev, 5, PTP_TX_PARSE_L2_ADDR_EN, 0); 2920 lanphy_write_page_reg(phydev, 5, PTP_RX_PARSE_L2_ADDR_EN, 0); 2921 lanphy_write_page_reg(phydev, 5, PTP_TX_PARSE_IP_ADDR_EN, 0); 2922 lanphy_write_page_reg(phydev, 5, PTP_RX_PARSE_IP_ADDR_EN, 0); 2923 2924 skb_queue_head_init(&ptp_priv->tx_queue); 2925 skb_queue_head_init(&ptp_priv->rx_queue); 2926 INIT_LIST_HEAD(&ptp_priv->rx_ts_list); 2927 spin_lock_init(&ptp_priv->rx_ts_lock); 2928 2929 ptp_priv->phydev = phydev; 2930 2931 ptp_priv->mii_ts.rxtstamp = lan8814_rxtstamp; 2932 ptp_priv->mii_ts.txtstamp = lan8814_txtstamp; 2933 ptp_priv->mii_ts.hwtstamp = lan8814_hwtstamp; 2934 ptp_priv->mii_ts.ts_info = lan8814_ts_info; 2935 2936 phydev->mii_ts = &ptp_priv->mii_ts; 2937 } 2938 2939 static int lan8814_ptp_probe_once(struct phy_device *phydev) 2940 { 2941 struct lan8814_shared_priv *shared = phydev->shared->priv; 2942 2943 if (!IS_ENABLED(CONFIG_PTP_1588_CLOCK) || 2944 !IS_ENABLED(CONFIG_NETWORK_PHY_TIMESTAMPING)) 2945 return 0; 2946 2947 /* Initialise shared lock for clock*/ 2948 mutex_init(&shared->shared_lock); 2949 2950 shared->ptp_clock_info.owner = THIS_MODULE; 2951 snprintf(shared->ptp_clock_info.name, 30, "%s", phydev->drv->name); 2952 shared->ptp_clock_info.max_adj = 31249999; 2953 shared->ptp_clock_info.n_alarm = 0; 2954 shared->ptp_clock_info.n_ext_ts = 0; 2955 shared->ptp_clock_info.n_pins = 0; 2956 shared->ptp_clock_info.pps = 0; 2957 shared->ptp_clock_info.pin_config = NULL; 2958 shared->ptp_clock_info.adjfine = lan8814_ptpci_adjfine; 2959 shared->ptp_clock_info.adjtime = lan8814_ptpci_adjtime; 2960 shared->ptp_clock_info.gettime64 = lan8814_ptpci_gettime64; 2961 shared->ptp_clock_info.settime64 = lan8814_ptpci_settime64; 2962 shared->ptp_clock_info.getcrosststamp = NULL; 2963 2964 shared->ptp_clock = ptp_clock_register(&shared->ptp_clock_info, 2965 &phydev->mdio.dev); 2966 if (IS_ERR_OR_NULL(shared->ptp_clock)) { 2967 phydev_err(phydev, "ptp_clock_register failed %lu\n", 2968 PTR_ERR(shared->ptp_clock)); 2969 return -EINVAL; 2970 } 2971 2972 phydev_dbg(phydev, "successfully registered ptp clock\n"); 2973 2974 shared->phydev = phydev; 2975 2976 /* The EP.4 is shared between all the PHYs in the package and also it 2977 * can be accessed by any of the PHYs 2978 */ 2979 lanphy_write_page_reg(phydev, 4, LTC_HARD_RESET, LTC_HARD_RESET_); 2980 lanphy_write_page_reg(phydev, 4, PTP_OPERATING_MODE, 2981 PTP_OPERATING_MODE_STANDALONE_); 2982 2983 return 0; 2984 } 2985 2986 static void lan8814_setup_led(struct phy_device *phydev, int val) 2987 { 2988 int temp; 2989 2990 temp = lanphy_read_page_reg(phydev, 5, LAN8814_LED_CTRL_1); 2991 2992 if (val) 2993 temp |= LAN8814_LED_CTRL_1_KSZ9031_LED_MODE_; 2994 else 2995 temp &= ~LAN8814_LED_CTRL_1_KSZ9031_LED_MODE_; 2996 2997 lanphy_write_page_reg(phydev, 5, LAN8814_LED_CTRL_1, temp); 2998 } 2999 3000 static int lan8814_config_init(struct phy_device *phydev) 3001 { 3002 struct kszphy_priv *lan8814 = phydev->priv; 3003 int val; 3004 3005 /* Reset the PHY */ 3006 val = lanphy_read_page_reg(phydev, 4, LAN8814_QSGMII_SOFT_RESET); 3007 val |= LAN8814_QSGMII_SOFT_RESET_BIT; 3008 lanphy_write_page_reg(phydev, 4, LAN8814_QSGMII_SOFT_RESET, val); 3009 3010 /* Disable ANEG with QSGMII PCS Host side */ 3011 val = lanphy_read_page_reg(phydev, 5, LAN8814_QSGMII_PCS1G_ANEG_CONFIG); 3012 val &= ~LAN8814_QSGMII_PCS1G_ANEG_CONFIG_ANEG_ENA; 3013 lanphy_write_page_reg(phydev, 5, LAN8814_QSGMII_PCS1G_ANEG_CONFIG, val); 3014 3015 /* MDI-X setting for swap A,B transmit */ 3016 val = lanphy_read_page_reg(phydev, 2, LAN8814_ALIGN_SWAP); 3017 val &= ~LAN8814_ALIGN_TX_A_B_SWAP_MASK; 3018 val |= LAN8814_ALIGN_TX_A_B_SWAP; 3019 lanphy_write_page_reg(phydev, 2, LAN8814_ALIGN_SWAP, val); 3020 3021 if (lan8814->led_mode >= 0) 3022 lan8814_setup_led(phydev, lan8814->led_mode); 3023 3024 return 0; 3025 } 3026 3027 /* It is expected that there will not be any 'lan8814_take_coma_mode' 3028 * function called in suspend. Because the GPIO line can be shared, so if one of 3029 * the phys goes back in coma mode, then all the other PHYs will go, which is 3030 * wrong. 3031 */ 3032 static int lan8814_release_coma_mode(struct phy_device *phydev) 3033 { 3034 struct gpio_desc *gpiod; 3035 3036 gpiod = devm_gpiod_get_optional(&phydev->mdio.dev, "coma-mode", 3037 GPIOD_OUT_HIGH_OPEN_DRAIN | 3038 GPIOD_FLAGS_BIT_NONEXCLUSIVE); 3039 if (IS_ERR(gpiod)) 3040 return PTR_ERR(gpiod); 3041 3042 gpiod_set_consumer_name(gpiod, "LAN8814 coma mode"); 3043 gpiod_set_value_cansleep(gpiod, 0); 3044 3045 return 0; 3046 } 3047 3048 static int lan8814_probe(struct phy_device *phydev) 3049 { 3050 const struct kszphy_type *type = phydev->drv->driver_data; 3051 struct kszphy_priv *priv; 3052 u16 addr; 3053 int err; 3054 3055 priv = devm_kzalloc(&phydev->mdio.dev, sizeof(*priv), GFP_KERNEL); 3056 if (!priv) 3057 return -ENOMEM; 3058 3059 phydev->priv = priv; 3060 3061 priv->type = type; 3062 3063 kszphy_parse_led_mode(phydev); 3064 3065 /* Strap-in value for PHY address, below register read gives starting 3066 * phy address value 3067 */ 3068 addr = lanphy_read_page_reg(phydev, 4, 0) & 0x1F; 3069 devm_phy_package_join(&phydev->mdio.dev, phydev, 3070 addr, sizeof(struct lan8814_shared_priv)); 3071 3072 if (phy_package_init_once(phydev)) { 3073 err = lan8814_release_coma_mode(phydev); 3074 if (err) 3075 return err; 3076 3077 err = lan8814_ptp_probe_once(phydev); 3078 if (err) 3079 return err; 3080 } 3081 3082 lan8814_ptp_init(phydev); 3083 3084 return 0; 3085 } 3086 3087 static struct phy_driver ksphy_driver[] = { 3088 { 3089 .phy_id = PHY_ID_KS8737, 3090 .phy_id_mask = MICREL_PHY_ID_MASK, 3091 .name = "Micrel KS8737", 3092 /* PHY_BASIC_FEATURES */ 3093 .driver_data = &ks8737_type, 3094 .probe = kszphy_probe, 3095 .config_init = kszphy_config_init, 3096 .config_intr = kszphy_config_intr, 3097 .handle_interrupt = kszphy_handle_interrupt, 3098 .suspend = kszphy_suspend, 3099 .resume = kszphy_resume, 3100 }, { 3101 .phy_id = PHY_ID_KSZ8021, 3102 .phy_id_mask = 0x00ffffff, 3103 .name = "Micrel KSZ8021 or KSZ8031", 3104 /* PHY_BASIC_FEATURES */ 3105 .driver_data = &ksz8021_type, 3106 .probe = kszphy_probe, 3107 .config_init = kszphy_config_init, 3108 .config_intr = kszphy_config_intr, 3109 .handle_interrupt = kszphy_handle_interrupt, 3110 .get_sset_count = kszphy_get_sset_count, 3111 .get_strings = kszphy_get_strings, 3112 .get_stats = kszphy_get_stats, 3113 .suspend = kszphy_suspend, 3114 .resume = kszphy_resume, 3115 }, { 3116 .phy_id = PHY_ID_KSZ8031, 3117 .phy_id_mask = 0x00ffffff, 3118 .name = "Micrel KSZ8031", 3119 /* PHY_BASIC_FEATURES */ 3120 .driver_data = &ksz8021_type, 3121 .probe = kszphy_probe, 3122 .config_init = kszphy_config_init, 3123 .config_intr = kszphy_config_intr, 3124 .handle_interrupt = kszphy_handle_interrupt, 3125 .get_sset_count = kszphy_get_sset_count, 3126 .get_strings = kszphy_get_strings, 3127 .get_stats = kszphy_get_stats, 3128 .suspend = kszphy_suspend, 3129 .resume = kszphy_resume, 3130 }, { 3131 .phy_id = PHY_ID_KSZ8041, 3132 .phy_id_mask = MICREL_PHY_ID_MASK, 3133 .name = "Micrel KSZ8041", 3134 /* PHY_BASIC_FEATURES */ 3135 .driver_data = &ksz8041_type, 3136 .probe = kszphy_probe, 3137 .config_init = ksz8041_config_init, 3138 .config_aneg = ksz8041_config_aneg, 3139 .config_intr = kszphy_config_intr, 3140 .handle_interrupt = kszphy_handle_interrupt, 3141 .get_sset_count = kszphy_get_sset_count, 3142 .get_strings = kszphy_get_strings, 3143 .get_stats = kszphy_get_stats, 3144 /* No suspend/resume callbacks because of errata DS80000700A, 3145 * receiver error following software power down. 3146 */ 3147 }, { 3148 .phy_id = PHY_ID_KSZ8041RNLI, 3149 .phy_id_mask = MICREL_PHY_ID_MASK, 3150 .name = "Micrel KSZ8041RNLI", 3151 /* PHY_BASIC_FEATURES */ 3152 .driver_data = &ksz8041_type, 3153 .probe = kszphy_probe, 3154 .config_init = kszphy_config_init, 3155 .config_intr = kszphy_config_intr, 3156 .handle_interrupt = kszphy_handle_interrupt, 3157 .get_sset_count = kszphy_get_sset_count, 3158 .get_strings = kszphy_get_strings, 3159 .get_stats = kszphy_get_stats, 3160 .suspend = kszphy_suspend, 3161 .resume = kszphy_resume, 3162 }, { 3163 .name = "Micrel KSZ8051", 3164 /* PHY_BASIC_FEATURES */ 3165 .driver_data = &ksz8051_type, 3166 .probe = kszphy_probe, 3167 .config_init = kszphy_config_init, 3168 .config_intr = kszphy_config_intr, 3169 .handle_interrupt = kszphy_handle_interrupt, 3170 .get_sset_count = kszphy_get_sset_count, 3171 .get_strings = kszphy_get_strings, 3172 .get_stats = kszphy_get_stats, 3173 .match_phy_device = ksz8051_match_phy_device, 3174 .suspend = kszphy_suspend, 3175 .resume = kszphy_resume, 3176 }, { 3177 .phy_id = PHY_ID_KSZ8001, 3178 .name = "Micrel KSZ8001 or KS8721", 3179 .phy_id_mask = 0x00fffffc, 3180 /* PHY_BASIC_FEATURES */ 3181 .driver_data = &ksz8041_type, 3182 .probe = kszphy_probe, 3183 .config_init = kszphy_config_init, 3184 .config_intr = kszphy_config_intr, 3185 .handle_interrupt = kszphy_handle_interrupt, 3186 .get_sset_count = kszphy_get_sset_count, 3187 .get_strings = kszphy_get_strings, 3188 .get_stats = kszphy_get_stats, 3189 .suspend = kszphy_suspend, 3190 .resume = kszphy_resume, 3191 }, { 3192 .phy_id = PHY_ID_KSZ8081, 3193 .name = "Micrel KSZ8081 or KSZ8091", 3194 .phy_id_mask = MICREL_PHY_ID_MASK, 3195 .flags = PHY_POLL_CABLE_TEST, 3196 /* PHY_BASIC_FEATURES */ 3197 .driver_data = &ksz8081_type, 3198 .probe = kszphy_probe, 3199 .config_init = ksz8081_config_init, 3200 .soft_reset = genphy_soft_reset, 3201 .config_aneg = ksz8081_config_aneg, 3202 .read_status = ksz8081_read_status, 3203 .config_intr = kszphy_config_intr, 3204 .handle_interrupt = kszphy_handle_interrupt, 3205 .get_sset_count = kszphy_get_sset_count, 3206 .get_strings = kszphy_get_strings, 3207 .get_stats = kszphy_get_stats, 3208 .suspend = kszphy_suspend, 3209 .resume = kszphy_resume, 3210 .cable_test_start = ksz886x_cable_test_start, 3211 .cable_test_get_status = ksz886x_cable_test_get_status, 3212 }, { 3213 .phy_id = PHY_ID_KSZ8061, 3214 .name = "Micrel KSZ8061", 3215 .phy_id_mask = MICREL_PHY_ID_MASK, 3216 /* PHY_BASIC_FEATURES */ 3217 .probe = kszphy_probe, 3218 .config_init = ksz8061_config_init, 3219 .config_intr = kszphy_config_intr, 3220 .handle_interrupt = kszphy_handle_interrupt, 3221 .suspend = kszphy_suspend, 3222 .resume = kszphy_resume, 3223 }, { 3224 .phy_id = PHY_ID_KSZ9021, 3225 .phy_id_mask = 0x000ffffe, 3226 .name = "Micrel KSZ9021 Gigabit PHY", 3227 /* PHY_GBIT_FEATURES */ 3228 .driver_data = &ksz9021_type, 3229 .probe = kszphy_probe, 3230 .get_features = ksz9031_get_features, 3231 .config_init = ksz9021_config_init, 3232 .config_intr = kszphy_config_intr, 3233 .handle_interrupt = kszphy_handle_interrupt, 3234 .get_sset_count = kszphy_get_sset_count, 3235 .get_strings = kszphy_get_strings, 3236 .get_stats = kszphy_get_stats, 3237 .suspend = kszphy_suspend, 3238 .resume = kszphy_resume, 3239 .read_mmd = genphy_read_mmd_unsupported, 3240 .write_mmd = genphy_write_mmd_unsupported, 3241 }, { 3242 .phy_id = PHY_ID_KSZ9031, 3243 .phy_id_mask = MICREL_PHY_ID_MASK, 3244 .name = "Micrel KSZ9031 Gigabit PHY", 3245 .flags = PHY_POLL_CABLE_TEST, 3246 .driver_data = &ksz9021_type, 3247 .probe = kszphy_probe, 3248 .get_features = ksz9031_get_features, 3249 .config_init = ksz9031_config_init, 3250 .soft_reset = genphy_soft_reset, 3251 .read_status = ksz9031_read_status, 3252 .config_intr = kszphy_config_intr, 3253 .handle_interrupt = kszphy_handle_interrupt, 3254 .get_sset_count = kszphy_get_sset_count, 3255 .get_strings = kszphy_get_strings, 3256 .get_stats = kszphy_get_stats, 3257 .suspend = kszphy_suspend, 3258 .resume = kszphy_resume, 3259 .cable_test_start = ksz9x31_cable_test_start, 3260 .cable_test_get_status = ksz9x31_cable_test_get_status, 3261 }, { 3262 .phy_id = PHY_ID_LAN8814, 3263 .phy_id_mask = MICREL_PHY_ID_MASK, 3264 .name = "Microchip INDY Gigabit Quad PHY", 3265 .flags = PHY_POLL_CABLE_TEST, 3266 .config_init = lan8814_config_init, 3267 .driver_data = &lan8814_type, 3268 .probe = lan8814_probe, 3269 .soft_reset = genphy_soft_reset, 3270 .read_status = ksz9031_read_status, 3271 .get_sset_count = kszphy_get_sset_count, 3272 .get_strings = kszphy_get_strings, 3273 .get_stats = kszphy_get_stats, 3274 .suspend = genphy_suspend, 3275 .resume = kszphy_resume, 3276 .config_intr = lan8814_config_intr, 3277 .handle_interrupt = lan8814_handle_interrupt, 3278 .cable_test_start = lan8814_cable_test_start, 3279 .cable_test_get_status = ksz886x_cable_test_get_status, 3280 }, { 3281 .phy_id = PHY_ID_LAN8804, 3282 .phy_id_mask = MICREL_PHY_ID_MASK, 3283 .name = "Microchip LAN966X Gigabit PHY", 3284 .config_init = lan8804_config_init, 3285 .driver_data = &ksz9021_type, 3286 .probe = kszphy_probe, 3287 .soft_reset = genphy_soft_reset, 3288 .read_status = ksz9031_read_status, 3289 .get_sset_count = kszphy_get_sset_count, 3290 .get_strings = kszphy_get_strings, 3291 .get_stats = kszphy_get_stats, 3292 .suspend = genphy_suspend, 3293 .resume = kszphy_resume, 3294 .config_intr = lan8804_config_intr, 3295 .handle_interrupt = lan8804_handle_interrupt, 3296 }, { 3297 .phy_id = PHY_ID_KSZ9131, 3298 .phy_id_mask = MICREL_PHY_ID_MASK, 3299 .name = "Microchip KSZ9131 Gigabit PHY", 3300 /* PHY_GBIT_FEATURES */ 3301 .flags = PHY_POLL_CABLE_TEST, 3302 .driver_data = &ksz9021_type, 3303 .probe = kszphy_probe, 3304 .config_init = ksz9131_config_init, 3305 .config_intr = kszphy_config_intr, 3306 .handle_interrupt = kszphy_handle_interrupt, 3307 .get_sset_count = kszphy_get_sset_count, 3308 .get_strings = kszphy_get_strings, 3309 .get_stats = kszphy_get_stats, 3310 .suspend = kszphy_suspend, 3311 .resume = kszphy_resume, 3312 .cable_test_start = ksz9x31_cable_test_start, 3313 .cable_test_get_status = ksz9x31_cable_test_get_status, 3314 }, { 3315 .phy_id = PHY_ID_KSZ8873MLL, 3316 .phy_id_mask = MICREL_PHY_ID_MASK, 3317 .name = "Micrel KSZ8873MLL Switch", 3318 /* PHY_BASIC_FEATURES */ 3319 .config_init = kszphy_config_init, 3320 .config_aneg = ksz8873mll_config_aneg, 3321 .read_status = ksz8873mll_read_status, 3322 .suspend = genphy_suspend, 3323 .resume = genphy_resume, 3324 }, { 3325 .phy_id = PHY_ID_KSZ886X, 3326 .phy_id_mask = MICREL_PHY_ID_MASK, 3327 .name = "Micrel KSZ8851 Ethernet MAC or KSZ886X Switch", 3328 .driver_data = &ksz886x_type, 3329 /* PHY_BASIC_FEATURES */ 3330 .flags = PHY_POLL_CABLE_TEST, 3331 .config_init = kszphy_config_init, 3332 .config_aneg = ksz886x_config_aneg, 3333 .read_status = ksz886x_read_status, 3334 .suspend = genphy_suspend, 3335 .resume = genphy_resume, 3336 .cable_test_start = ksz886x_cable_test_start, 3337 .cable_test_get_status = ksz886x_cable_test_get_status, 3338 }, { 3339 .name = "Micrel KSZ87XX Switch", 3340 /* PHY_BASIC_FEATURES */ 3341 .config_init = kszphy_config_init, 3342 .match_phy_device = ksz8795_match_phy_device, 3343 .suspend = genphy_suspend, 3344 .resume = genphy_resume, 3345 }, { 3346 .phy_id = PHY_ID_KSZ9477, 3347 .phy_id_mask = MICREL_PHY_ID_MASK, 3348 .name = "Microchip KSZ9477", 3349 /* PHY_GBIT_FEATURES */ 3350 .config_init = kszphy_config_init, 3351 .config_intr = kszphy_config_intr, 3352 .handle_interrupt = kszphy_handle_interrupt, 3353 .suspend = genphy_suspend, 3354 .resume = genphy_resume, 3355 } }; 3356 3357 module_phy_driver(ksphy_driver); 3358 3359 MODULE_DESCRIPTION("Micrel PHY driver"); 3360 MODULE_AUTHOR("David J. Choi"); 3361 MODULE_LICENSE("GPL"); 3362 3363 static struct mdio_device_id __maybe_unused micrel_tbl[] = { 3364 { PHY_ID_KSZ9021, 0x000ffffe }, 3365 { PHY_ID_KSZ9031, MICREL_PHY_ID_MASK }, 3366 { PHY_ID_KSZ9131, MICREL_PHY_ID_MASK }, 3367 { PHY_ID_KSZ8001, 0x00fffffc }, 3368 { PHY_ID_KS8737, MICREL_PHY_ID_MASK }, 3369 { PHY_ID_KSZ8021, 0x00ffffff }, 3370 { PHY_ID_KSZ8031, 0x00ffffff }, 3371 { PHY_ID_KSZ8041, MICREL_PHY_ID_MASK }, 3372 { PHY_ID_KSZ8051, MICREL_PHY_ID_MASK }, 3373 { PHY_ID_KSZ8061, MICREL_PHY_ID_MASK }, 3374 { PHY_ID_KSZ8081, MICREL_PHY_ID_MASK }, 3375 { PHY_ID_KSZ8873MLL, MICREL_PHY_ID_MASK }, 3376 { PHY_ID_KSZ886X, MICREL_PHY_ID_MASK }, 3377 { PHY_ID_LAN8814, MICREL_PHY_ID_MASK }, 3378 { PHY_ID_LAN8804, MICREL_PHY_ID_MASK }, 3379 { } 3380 }; 3381 3382 MODULE_DEVICE_TABLE(mdio, micrel_tbl); 3383