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