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, lan8841, lan8814 14 * 100/10 Phys : ksz8001, ksz8721, ksz8737, ksz8041 15 * ksz8021, ksz8031, ksz8051, 16 * ksz8081, ksz8091, 17 * ksz8061, 18 * Switch : ksz8873, ksz886x 19 * ksz9477, lan8804 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_VERSION 0x0248 124 #define PTP_TX_VERSION 0x0288 125 #define PTP_MAX_VERSION(x) (((x) & GENMASK(7, 0)) << 8) 126 #define PTP_MIN_VERSION(x) ((x) & GENMASK(7, 0)) 127 128 #define PTP_RX_MOD 0x024F 129 #define PTP_RX_MOD_BAD_UDPV4_CHKSUM_FORCE_FCS_DIS_ BIT(3) 130 #define PTP_RX_TIMESTAMP_EN 0x024D 131 #define PTP_TX_TIMESTAMP_EN 0x028D 132 133 #define PTP_TIMESTAMP_EN_SYNC_ BIT(0) 134 #define PTP_TIMESTAMP_EN_DREQ_ BIT(1) 135 #define PTP_TIMESTAMP_EN_PDREQ_ BIT(2) 136 #define PTP_TIMESTAMP_EN_PDRES_ BIT(3) 137 138 #define PTP_TX_PARSE_L2_ADDR_EN 0x0284 139 #define PTP_RX_PARSE_L2_ADDR_EN 0x0244 140 141 #define PTP_TX_PARSE_IP_ADDR_EN 0x0285 142 #define PTP_RX_PARSE_IP_ADDR_EN 0x0245 143 #define LTC_HARD_RESET 0x023F 144 #define LTC_HARD_RESET_ BIT(0) 145 146 #define TSU_HARD_RESET 0x02C1 147 #define TSU_HARD_RESET_ BIT(0) 148 149 #define PTP_CMD_CTL 0x0200 150 #define PTP_CMD_CTL_PTP_DISABLE_ BIT(0) 151 #define PTP_CMD_CTL_PTP_ENABLE_ BIT(1) 152 #define PTP_CMD_CTL_PTP_CLOCK_READ_ BIT(3) 153 #define PTP_CMD_CTL_PTP_CLOCK_LOAD_ BIT(4) 154 #define PTP_CMD_CTL_PTP_LTC_STEP_SEC_ BIT(5) 155 #define PTP_CMD_CTL_PTP_LTC_STEP_NSEC_ BIT(6) 156 157 #define PTP_CLOCK_SET_SEC_MID 0x0206 158 #define PTP_CLOCK_SET_SEC_LO 0x0207 159 #define PTP_CLOCK_SET_NS_HI 0x0208 160 #define PTP_CLOCK_SET_NS_LO 0x0209 161 162 #define PTP_CLOCK_READ_SEC_MID 0x022A 163 #define PTP_CLOCK_READ_SEC_LO 0x022B 164 #define PTP_CLOCK_READ_NS_HI 0x022C 165 #define PTP_CLOCK_READ_NS_LO 0x022D 166 167 #define PTP_OPERATING_MODE 0x0241 168 #define PTP_OPERATING_MODE_STANDALONE_ BIT(0) 169 170 #define PTP_TX_MOD 0x028F 171 #define PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_ BIT(12) 172 #define PTP_TX_MOD_BAD_UDPV4_CHKSUM_FORCE_FCS_DIS_ BIT(3) 173 174 #define PTP_RX_PARSE_CONFIG 0x0242 175 #define PTP_RX_PARSE_CONFIG_LAYER2_EN_ BIT(0) 176 #define PTP_RX_PARSE_CONFIG_IPV4_EN_ BIT(1) 177 #define PTP_RX_PARSE_CONFIG_IPV6_EN_ BIT(2) 178 179 #define PTP_TX_PARSE_CONFIG 0x0282 180 #define PTP_TX_PARSE_CONFIG_LAYER2_EN_ BIT(0) 181 #define PTP_TX_PARSE_CONFIG_IPV4_EN_ BIT(1) 182 #define PTP_TX_PARSE_CONFIG_IPV6_EN_ BIT(2) 183 184 #define PTP_CLOCK_RATE_ADJ_HI 0x020C 185 #define PTP_CLOCK_RATE_ADJ_LO 0x020D 186 #define PTP_CLOCK_RATE_ADJ_DIR_ BIT(15) 187 188 #define PTP_LTC_STEP_ADJ_HI 0x0212 189 #define PTP_LTC_STEP_ADJ_LO 0x0213 190 #define PTP_LTC_STEP_ADJ_DIR_ BIT(15) 191 192 #define LAN8814_INTR_STS_REG 0x0033 193 #define LAN8814_INTR_STS_REG_1588_TSU0_ BIT(0) 194 #define LAN8814_INTR_STS_REG_1588_TSU1_ BIT(1) 195 #define LAN8814_INTR_STS_REG_1588_TSU2_ BIT(2) 196 #define LAN8814_INTR_STS_REG_1588_TSU3_ BIT(3) 197 198 #define PTP_CAP_INFO 0x022A 199 #define PTP_CAP_INFO_TX_TS_CNT_GET_(reg_val) (((reg_val) & 0x0f00) >> 8) 200 #define PTP_CAP_INFO_RX_TS_CNT_GET_(reg_val) ((reg_val) & 0x000f) 201 202 #define PTP_TX_EGRESS_SEC_HI 0x0296 203 #define PTP_TX_EGRESS_SEC_LO 0x0297 204 #define PTP_TX_EGRESS_NS_HI 0x0294 205 #define PTP_TX_EGRESS_NS_LO 0x0295 206 #define PTP_TX_MSG_HEADER2 0x0299 207 208 #define PTP_RX_INGRESS_SEC_HI 0x0256 209 #define PTP_RX_INGRESS_SEC_LO 0x0257 210 #define PTP_RX_INGRESS_NS_HI 0x0254 211 #define PTP_RX_INGRESS_NS_LO 0x0255 212 #define PTP_RX_MSG_HEADER2 0x0259 213 214 #define PTP_TSU_INT_EN 0x0200 215 #define PTP_TSU_INT_EN_PTP_TX_TS_OVRFL_EN_ BIT(3) 216 #define PTP_TSU_INT_EN_PTP_TX_TS_EN_ BIT(2) 217 #define PTP_TSU_INT_EN_PTP_RX_TS_OVRFL_EN_ BIT(1) 218 #define PTP_TSU_INT_EN_PTP_RX_TS_EN_ BIT(0) 219 220 #define PTP_TSU_INT_STS 0x0201 221 #define PTP_TSU_INT_STS_PTP_TX_TS_OVRFL_INT_ BIT(3) 222 #define PTP_TSU_INT_STS_PTP_TX_TS_EN_ BIT(2) 223 #define PTP_TSU_INT_STS_PTP_RX_TS_OVRFL_INT_ BIT(1) 224 #define PTP_TSU_INT_STS_PTP_RX_TS_EN_ BIT(0) 225 226 #define LAN8814_LED_CTRL_1 0x0 227 #define LAN8814_LED_CTRL_1_KSZ9031_LED_MODE_ BIT(6) 228 229 /* PHY Control 1 */ 230 #define MII_KSZPHY_CTRL_1 0x1e 231 #define KSZ8081_CTRL1_MDIX_STAT BIT(4) 232 233 /* PHY Control 2 / PHY Control (if no PHY Control 1) */ 234 #define MII_KSZPHY_CTRL_2 0x1f 235 #define MII_KSZPHY_CTRL MII_KSZPHY_CTRL_2 236 /* bitmap of PHY register to set interrupt mode */ 237 #define KSZ8081_CTRL2_HP_MDIX BIT(15) 238 #define KSZ8081_CTRL2_MDI_MDI_X_SELECT BIT(14) 239 #define KSZ8081_CTRL2_DISABLE_AUTO_MDIX BIT(13) 240 #define KSZ8081_CTRL2_FORCE_LINK BIT(11) 241 #define KSZ8081_CTRL2_POWER_SAVING BIT(10) 242 #define KSZPHY_CTRL_INT_ACTIVE_HIGH BIT(9) 243 #define KSZPHY_RMII_REF_CLK_SEL BIT(7) 244 245 /* Write/read to/from extended registers */ 246 #define MII_KSZPHY_EXTREG 0x0b 247 #define KSZPHY_EXTREG_WRITE 0x8000 248 249 #define MII_KSZPHY_EXTREG_WRITE 0x0c 250 #define MII_KSZPHY_EXTREG_READ 0x0d 251 252 /* Extended registers */ 253 #define MII_KSZPHY_CLK_CONTROL_PAD_SKEW 0x104 254 #define MII_KSZPHY_RX_DATA_PAD_SKEW 0x105 255 #define MII_KSZPHY_TX_DATA_PAD_SKEW 0x106 256 257 #define PS_TO_REG 200 258 #define FIFO_SIZE 8 259 260 /* Delay used to get the second part from the LTC */ 261 #define LAN8841_GET_SEC_LTC_DELAY (500 * NSEC_PER_MSEC) 262 263 struct kszphy_hw_stat { 264 const char *string; 265 u8 reg; 266 u8 bits; 267 }; 268 269 static struct kszphy_hw_stat kszphy_hw_stats[] = { 270 { "phy_receive_errors", 21, 16}, 271 { "phy_idle_errors", 10, 8 }, 272 }; 273 274 struct kszphy_type { 275 u32 led_mode_reg; 276 u16 interrupt_level_mask; 277 u16 cable_diag_reg; 278 unsigned long pair_mask; 279 u16 disable_dll_tx_bit; 280 u16 disable_dll_rx_bit; 281 u16 disable_dll_mask; 282 bool has_broadcast_disable; 283 bool has_nand_tree_disable; 284 bool has_rmii_ref_clk_sel; 285 }; 286 287 /* Shared structure between the PHYs of the same package. */ 288 struct lan8814_shared_priv { 289 struct phy_device *phydev; 290 struct ptp_clock *ptp_clock; 291 struct ptp_clock_info ptp_clock_info; 292 293 /* Reference counter to how many ports in the package are enabling the 294 * timestamping 295 */ 296 u8 ref; 297 298 /* Lock for ptp_clock and ref */ 299 struct mutex shared_lock; 300 }; 301 302 struct lan8814_ptp_rx_ts { 303 struct list_head list; 304 u32 seconds; 305 u32 nsec; 306 u16 seq_id; 307 }; 308 309 struct kszphy_ptp_priv { 310 struct mii_timestamper mii_ts; 311 struct phy_device *phydev; 312 313 struct sk_buff_head tx_queue; 314 struct sk_buff_head rx_queue; 315 316 struct list_head rx_ts_list; 317 /* Lock for Rx ts fifo */ 318 spinlock_t rx_ts_lock; 319 320 int hwts_tx_type; 321 enum hwtstamp_rx_filters rx_filter; 322 int layer; 323 int version; 324 325 struct ptp_clock *ptp_clock; 326 struct ptp_clock_info ptp_clock_info; 327 /* Lock for ptp_clock */ 328 struct mutex ptp_lock; 329 struct ptp_pin_desc *pin_config; 330 331 s64 seconds; 332 /* Lock for accessing seconds */ 333 spinlock_t seconds_lock; 334 }; 335 336 struct kszphy_priv { 337 struct kszphy_ptp_priv ptp_priv; 338 const struct kszphy_type *type; 339 int led_mode; 340 u16 vct_ctrl1000; 341 bool rmii_ref_clk_sel; 342 bool rmii_ref_clk_sel_val; 343 u64 stats[ARRAY_SIZE(kszphy_hw_stats)]; 344 }; 345 346 static const struct kszphy_type lan8814_type = { 347 .led_mode_reg = ~LAN8814_LED_CTRL_1, 348 .cable_diag_reg = LAN8814_CABLE_DIAG, 349 .pair_mask = LAN8814_WIRE_PAIR_MASK, 350 }; 351 352 static const struct kszphy_type ksz886x_type = { 353 .cable_diag_reg = KSZ8081_LMD, 354 .pair_mask = KSZPHY_WIRE_PAIR_MASK, 355 }; 356 357 static const struct kszphy_type ksz8021_type = { 358 .led_mode_reg = MII_KSZPHY_CTRL_2, 359 .has_broadcast_disable = true, 360 .has_nand_tree_disable = true, 361 .has_rmii_ref_clk_sel = true, 362 }; 363 364 static const struct kszphy_type ksz8041_type = { 365 .led_mode_reg = MII_KSZPHY_CTRL_1, 366 }; 367 368 static const struct kszphy_type ksz8051_type = { 369 .led_mode_reg = MII_KSZPHY_CTRL_2, 370 .has_nand_tree_disable = true, 371 }; 372 373 static const struct kszphy_type ksz8081_type = { 374 .led_mode_reg = MII_KSZPHY_CTRL_2, 375 .has_broadcast_disable = true, 376 .has_nand_tree_disable = true, 377 .has_rmii_ref_clk_sel = true, 378 }; 379 380 static const struct kszphy_type ks8737_type = { 381 .interrupt_level_mask = BIT(14), 382 }; 383 384 static const struct kszphy_type ksz9021_type = { 385 .interrupt_level_mask = BIT(14), 386 }; 387 388 static const struct kszphy_type ksz9131_type = { 389 .interrupt_level_mask = BIT(14), 390 .disable_dll_tx_bit = BIT(12), 391 .disable_dll_rx_bit = BIT(12), 392 .disable_dll_mask = BIT_MASK(12), 393 }; 394 395 static const struct kszphy_type lan8841_type = { 396 .disable_dll_tx_bit = BIT(14), 397 .disable_dll_rx_bit = BIT(14), 398 .disable_dll_mask = BIT_MASK(14), 399 .cable_diag_reg = LAN8814_CABLE_DIAG, 400 .pair_mask = LAN8814_WIRE_PAIR_MASK, 401 }; 402 403 static int kszphy_extended_write(struct phy_device *phydev, 404 u32 regnum, u16 val) 405 { 406 phy_write(phydev, MII_KSZPHY_EXTREG, KSZPHY_EXTREG_WRITE | regnum); 407 return phy_write(phydev, MII_KSZPHY_EXTREG_WRITE, val); 408 } 409 410 static int kszphy_extended_read(struct phy_device *phydev, 411 u32 regnum) 412 { 413 phy_write(phydev, MII_KSZPHY_EXTREG, regnum); 414 return phy_read(phydev, MII_KSZPHY_EXTREG_READ); 415 } 416 417 static int kszphy_ack_interrupt(struct phy_device *phydev) 418 { 419 /* bit[7..0] int status, which is a read and clear register. */ 420 int rc; 421 422 rc = phy_read(phydev, MII_KSZPHY_INTCS); 423 424 return (rc < 0) ? rc : 0; 425 } 426 427 static int kszphy_config_intr(struct phy_device *phydev) 428 { 429 const struct kszphy_type *type = phydev->drv->driver_data; 430 int temp, err; 431 u16 mask; 432 433 if (type && type->interrupt_level_mask) 434 mask = type->interrupt_level_mask; 435 else 436 mask = KSZPHY_CTRL_INT_ACTIVE_HIGH; 437 438 /* set the interrupt pin active low */ 439 temp = phy_read(phydev, MII_KSZPHY_CTRL); 440 if (temp < 0) 441 return temp; 442 temp &= ~mask; 443 phy_write(phydev, MII_KSZPHY_CTRL, temp); 444 445 /* enable / disable interrupts */ 446 if (phydev->interrupts == PHY_INTERRUPT_ENABLED) { 447 err = kszphy_ack_interrupt(phydev); 448 if (err) 449 return err; 450 451 err = phy_write(phydev, MII_KSZPHY_INTCS, KSZPHY_INTCS_ALL); 452 } else { 453 err = phy_write(phydev, MII_KSZPHY_INTCS, 0); 454 if (err) 455 return err; 456 457 err = kszphy_ack_interrupt(phydev); 458 } 459 460 return err; 461 } 462 463 static irqreturn_t kszphy_handle_interrupt(struct phy_device *phydev) 464 { 465 int irq_status; 466 467 irq_status = phy_read(phydev, MII_KSZPHY_INTCS); 468 if (irq_status < 0) { 469 phy_error(phydev); 470 return IRQ_NONE; 471 } 472 473 if (!(irq_status & KSZPHY_INTCS_STATUS)) 474 return IRQ_NONE; 475 476 phy_trigger_machine(phydev); 477 478 return IRQ_HANDLED; 479 } 480 481 static int kszphy_rmii_clk_sel(struct phy_device *phydev, bool val) 482 { 483 int ctrl; 484 485 ctrl = phy_read(phydev, MII_KSZPHY_CTRL); 486 if (ctrl < 0) 487 return ctrl; 488 489 if (val) 490 ctrl |= KSZPHY_RMII_REF_CLK_SEL; 491 else 492 ctrl &= ~KSZPHY_RMII_REF_CLK_SEL; 493 494 return phy_write(phydev, MII_KSZPHY_CTRL, ctrl); 495 } 496 497 static int kszphy_setup_led(struct phy_device *phydev, u32 reg, int val) 498 { 499 int rc, temp, shift; 500 501 switch (reg) { 502 case MII_KSZPHY_CTRL_1: 503 shift = 14; 504 break; 505 case MII_KSZPHY_CTRL_2: 506 shift = 4; 507 break; 508 default: 509 return -EINVAL; 510 } 511 512 temp = phy_read(phydev, reg); 513 if (temp < 0) { 514 rc = temp; 515 goto out; 516 } 517 518 temp &= ~(3 << shift); 519 temp |= val << shift; 520 rc = phy_write(phydev, reg, temp); 521 out: 522 if (rc < 0) 523 phydev_err(phydev, "failed to set led mode\n"); 524 525 return rc; 526 } 527 528 /* Disable PHY address 0 as the broadcast address, so that it can be used as a 529 * unique (non-broadcast) address on a shared bus. 530 */ 531 static int kszphy_broadcast_disable(struct phy_device *phydev) 532 { 533 int ret; 534 535 ret = phy_read(phydev, MII_KSZPHY_OMSO); 536 if (ret < 0) 537 goto out; 538 539 ret = phy_write(phydev, MII_KSZPHY_OMSO, ret | KSZPHY_OMSO_B_CAST_OFF); 540 out: 541 if (ret) 542 phydev_err(phydev, "failed to disable broadcast address\n"); 543 544 return ret; 545 } 546 547 static int kszphy_nand_tree_disable(struct phy_device *phydev) 548 { 549 int ret; 550 551 ret = phy_read(phydev, MII_KSZPHY_OMSO); 552 if (ret < 0) 553 goto out; 554 555 if (!(ret & KSZPHY_OMSO_NAND_TREE_ON)) 556 return 0; 557 558 ret = phy_write(phydev, MII_KSZPHY_OMSO, 559 ret & ~KSZPHY_OMSO_NAND_TREE_ON); 560 out: 561 if (ret) 562 phydev_err(phydev, "failed to disable NAND tree mode\n"); 563 564 return ret; 565 } 566 567 /* Some config bits need to be set again on resume, handle them here. */ 568 static int kszphy_config_reset(struct phy_device *phydev) 569 { 570 struct kszphy_priv *priv = phydev->priv; 571 int ret; 572 573 if (priv->rmii_ref_clk_sel) { 574 ret = kszphy_rmii_clk_sel(phydev, priv->rmii_ref_clk_sel_val); 575 if (ret) { 576 phydev_err(phydev, 577 "failed to set rmii reference clock\n"); 578 return ret; 579 } 580 } 581 582 if (priv->type && priv->led_mode >= 0) 583 kszphy_setup_led(phydev, priv->type->led_mode_reg, priv->led_mode); 584 585 return 0; 586 } 587 588 static int kszphy_config_init(struct phy_device *phydev) 589 { 590 struct kszphy_priv *priv = phydev->priv; 591 const struct kszphy_type *type; 592 593 if (!priv) 594 return 0; 595 596 type = priv->type; 597 598 if (type && type->has_broadcast_disable) 599 kszphy_broadcast_disable(phydev); 600 601 if (type && type->has_nand_tree_disable) 602 kszphy_nand_tree_disable(phydev); 603 604 return kszphy_config_reset(phydev); 605 } 606 607 static int ksz8041_fiber_mode(struct phy_device *phydev) 608 { 609 struct device_node *of_node = phydev->mdio.dev.of_node; 610 611 return of_property_read_bool(of_node, "micrel,fiber-mode"); 612 } 613 614 static int ksz8041_config_init(struct phy_device *phydev) 615 { 616 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, }; 617 618 /* Limit supported and advertised modes in fiber mode */ 619 if (ksz8041_fiber_mode(phydev)) { 620 phydev->dev_flags |= MICREL_PHY_FXEN; 621 linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, mask); 622 linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, mask); 623 624 linkmode_and(phydev->supported, phydev->supported, mask); 625 linkmode_set_bit(ETHTOOL_LINK_MODE_FIBRE_BIT, 626 phydev->supported); 627 linkmode_and(phydev->advertising, phydev->advertising, mask); 628 linkmode_set_bit(ETHTOOL_LINK_MODE_FIBRE_BIT, 629 phydev->advertising); 630 phydev->autoneg = AUTONEG_DISABLE; 631 } 632 633 return kszphy_config_init(phydev); 634 } 635 636 static int ksz8041_config_aneg(struct phy_device *phydev) 637 { 638 /* Skip auto-negotiation in fiber mode */ 639 if (phydev->dev_flags & MICREL_PHY_FXEN) { 640 phydev->speed = SPEED_100; 641 return 0; 642 } 643 644 return genphy_config_aneg(phydev); 645 } 646 647 static int ksz8051_ksz8795_match_phy_device(struct phy_device *phydev, 648 const bool ksz_8051) 649 { 650 int ret; 651 652 if (!phy_id_compare(phydev->phy_id, PHY_ID_KSZ8051, MICREL_PHY_ID_MASK)) 653 return 0; 654 655 ret = phy_read(phydev, MII_BMSR); 656 if (ret < 0) 657 return ret; 658 659 /* KSZ8051 PHY and KSZ8794/KSZ8795/KSZ8765 switch share the same 660 * exact PHY ID. However, they can be told apart by the extended 661 * capability registers presence. The KSZ8051 PHY has them while 662 * the switch does not. 663 */ 664 ret &= BMSR_ERCAP; 665 if (ksz_8051) 666 return ret; 667 else 668 return !ret; 669 } 670 671 static int ksz8051_match_phy_device(struct phy_device *phydev) 672 { 673 return ksz8051_ksz8795_match_phy_device(phydev, true); 674 } 675 676 static int ksz8081_config_init(struct phy_device *phydev) 677 { 678 /* KSZPHY_OMSO_FACTORY_TEST is set at de-assertion of the reset line 679 * based on the RXER (KSZ8081RNA/RND) or TXC (KSZ8081MNX/RNB) pin. If a 680 * pull-down is missing, the factory test mode should be cleared by 681 * manually writing a 0. 682 */ 683 phy_clear_bits(phydev, MII_KSZPHY_OMSO, KSZPHY_OMSO_FACTORY_TEST); 684 685 return kszphy_config_init(phydev); 686 } 687 688 static int ksz8081_config_mdix(struct phy_device *phydev, u8 ctrl) 689 { 690 u16 val; 691 692 switch (ctrl) { 693 case ETH_TP_MDI: 694 val = KSZ8081_CTRL2_DISABLE_AUTO_MDIX; 695 break; 696 case ETH_TP_MDI_X: 697 val = KSZ8081_CTRL2_DISABLE_AUTO_MDIX | 698 KSZ8081_CTRL2_MDI_MDI_X_SELECT; 699 break; 700 case ETH_TP_MDI_AUTO: 701 val = 0; 702 break; 703 default: 704 return 0; 705 } 706 707 return phy_modify(phydev, MII_KSZPHY_CTRL_2, 708 KSZ8081_CTRL2_HP_MDIX | 709 KSZ8081_CTRL2_MDI_MDI_X_SELECT | 710 KSZ8081_CTRL2_DISABLE_AUTO_MDIX, 711 KSZ8081_CTRL2_HP_MDIX | val); 712 } 713 714 static int ksz8081_config_aneg(struct phy_device *phydev) 715 { 716 int ret; 717 718 ret = genphy_config_aneg(phydev); 719 if (ret) 720 return ret; 721 722 /* The MDI-X configuration is automatically changed by the PHY after 723 * switching from autoneg off to on. So, take MDI-X configuration under 724 * own control and set it after autoneg configuration was done. 725 */ 726 return ksz8081_config_mdix(phydev, phydev->mdix_ctrl); 727 } 728 729 static int ksz8081_mdix_update(struct phy_device *phydev) 730 { 731 int ret; 732 733 ret = phy_read(phydev, MII_KSZPHY_CTRL_2); 734 if (ret < 0) 735 return ret; 736 737 if (ret & KSZ8081_CTRL2_DISABLE_AUTO_MDIX) { 738 if (ret & KSZ8081_CTRL2_MDI_MDI_X_SELECT) 739 phydev->mdix_ctrl = ETH_TP_MDI_X; 740 else 741 phydev->mdix_ctrl = ETH_TP_MDI; 742 } else { 743 phydev->mdix_ctrl = ETH_TP_MDI_AUTO; 744 } 745 746 ret = phy_read(phydev, MII_KSZPHY_CTRL_1); 747 if (ret < 0) 748 return ret; 749 750 if (ret & KSZ8081_CTRL1_MDIX_STAT) 751 phydev->mdix = ETH_TP_MDI; 752 else 753 phydev->mdix = ETH_TP_MDI_X; 754 755 return 0; 756 } 757 758 static int ksz8081_read_status(struct phy_device *phydev) 759 { 760 int ret; 761 762 ret = ksz8081_mdix_update(phydev); 763 if (ret < 0) 764 return ret; 765 766 return genphy_read_status(phydev); 767 } 768 769 static int ksz8061_config_init(struct phy_device *phydev) 770 { 771 int ret; 772 773 ret = phy_write_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_DEVID1, 0xB61A); 774 if (ret) 775 return ret; 776 777 return kszphy_config_init(phydev); 778 } 779 780 static int ksz8795_match_phy_device(struct phy_device *phydev) 781 { 782 return ksz8051_ksz8795_match_phy_device(phydev, false); 783 } 784 785 static int ksz9021_load_values_from_of(struct phy_device *phydev, 786 const struct device_node *of_node, 787 u16 reg, 788 const char *field1, const char *field2, 789 const char *field3, const char *field4) 790 { 791 int val1 = -1; 792 int val2 = -2; 793 int val3 = -3; 794 int val4 = -4; 795 int newval; 796 int matches = 0; 797 798 if (!of_property_read_u32(of_node, field1, &val1)) 799 matches++; 800 801 if (!of_property_read_u32(of_node, field2, &val2)) 802 matches++; 803 804 if (!of_property_read_u32(of_node, field3, &val3)) 805 matches++; 806 807 if (!of_property_read_u32(of_node, field4, &val4)) 808 matches++; 809 810 if (!matches) 811 return 0; 812 813 if (matches < 4) 814 newval = kszphy_extended_read(phydev, reg); 815 else 816 newval = 0; 817 818 if (val1 != -1) 819 newval = ((newval & 0xfff0) | ((val1 / PS_TO_REG) & 0xf) << 0); 820 821 if (val2 != -2) 822 newval = ((newval & 0xff0f) | ((val2 / PS_TO_REG) & 0xf) << 4); 823 824 if (val3 != -3) 825 newval = ((newval & 0xf0ff) | ((val3 / PS_TO_REG) & 0xf) << 8); 826 827 if (val4 != -4) 828 newval = ((newval & 0x0fff) | ((val4 / PS_TO_REG) & 0xf) << 12); 829 830 return kszphy_extended_write(phydev, reg, newval); 831 } 832 833 static int ksz9021_config_init(struct phy_device *phydev) 834 { 835 const struct device_node *of_node; 836 const struct device *dev_walker; 837 838 /* The Micrel driver has a deprecated option to place phy OF 839 * properties in the MAC node. Walk up the tree of devices to 840 * find a device with an OF node. 841 */ 842 dev_walker = &phydev->mdio.dev; 843 do { 844 of_node = dev_walker->of_node; 845 dev_walker = dev_walker->parent; 846 847 } while (!of_node && dev_walker); 848 849 if (of_node) { 850 ksz9021_load_values_from_of(phydev, of_node, 851 MII_KSZPHY_CLK_CONTROL_PAD_SKEW, 852 "txen-skew-ps", "txc-skew-ps", 853 "rxdv-skew-ps", "rxc-skew-ps"); 854 ksz9021_load_values_from_of(phydev, of_node, 855 MII_KSZPHY_RX_DATA_PAD_SKEW, 856 "rxd0-skew-ps", "rxd1-skew-ps", 857 "rxd2-skew-ps", "rxd3-skew-ps"); 858 ksz9021_load_values_from_of(phydev, of_node, 859 MII_KSZPHY_TX_DATA_PAD_SKEW, 860 "txd0-skew-ps", "txd1-skew-ps", 861 "txd2-skew-ps", "txd3-skew-ps"); 862 } 863 return 0; 864 } 865 866 #define KSZ9031_PS_TO_REG 60 867 868 /* Extended registers */ 869 /* MMD Address 0x0 */ 870 #define MII_KSZ9031RN_FLP_BURST_TX_LO 3 871 #define MII_KSZ9031RN_FLP_BURST_TX_HI 4 872 873 /* MMD Address 0x2 */ 874 #define MII_KSZ9031RN_CONTROL_PAD_SKEW 4 875 #define MII_KSZ9031RN_RX_CTL_M GENMASK(7, 4) 876 #define MII_KSZ9031RN_TX_CTL_M GENMASK(3, 0) 877 878 #define MII_KSZ9031RN_RX_DATA_PAD_SKEW 5 879 #define MII_KSZ9031RN_RXD3 GENMASK(15, 12) 880 #define MII_KSZ9031RN_RXD2 GENMASK(11, 8) 881 #define MII_KSZ9031RN_RXD1 GENMASK(7, 4) 882 #define MII_KSZ9031RN_RXD0 GENMASK(3, 0) 883 884 #define MII_KSZ9031RN_TX_DATA_PAD_SKEW 6 885 #define MII_KSZ9031RN_TXD3 GENMASK(15, 12) 886 #define MII_KSZ9031RN_TXD2 GENMASK(11, 8) 887 #define MII_KSZ9031RN_TXD1 GENMASK(7, 4) 888 #define MII_KSZ9031RN_TXD0 GENMASK(3, 0) 889 890 #define MII_KSZ9031RN_CLK_PAD_SKEW 8 891 #define MII_KSZ9031RN_GTX_CLK GENMASK(9, 5) 892 #define MII_KSZ9031RN_RX_CLK GENMASK(4, 0) 893 894 /* KSZ9031 has internal RGMII_IDRX = 1.2ns and RGMII_IDTX = 0ns. To 895 * provide different RGMII options we need to configure delay offset 896 * for each pad relative to build in delay. 897 */ 898 /* keep rx as "No delay adjustment" and set rx_clk to +0.60ns to get delays of 899 * 1.80ns 900 */ 901 #define RX_ID 0x7 902 #define RX_CLK_ID 0x19 903 904 /* set rx to +0.30ns and rx_clk to -0.90ns to compensate the 905 * internal 1.2ns delay. 906 */ 907 #define RX_ND 0xc 908 #define RX_CLK_ND 0x0 909 910 /* set tx to -0.42ns and tx_clk to +0.96ns to get 1.38ns delay */ 911 #define TX_ID 0x0 912 #define TX_CLK_ID 0x1f 913 914 /* set tx and tx_clk to "No delay adjustment" to keep 0ns 915 * dealy 916 */ 917 #define TX_ND 0x7 918 #define TX_CLK_ND 0xf 919 920 /* MMD Address 0x1C */ 921 #define MII_KSZ9031RN_EDPD 0x23 922 #define MII_KSZ9031RN_EDPD_ENABLE BIT(0) 923 924 static int ksz9031_of_load_skew_values(struct phy_device *phydev, 925 const struct device_node *of_node, 926 u16 reg, size_t field_sz, 927 const char *field[], u8 numfields, 928 bool *update) 929 { 930 int val[4] = {-1, -2, -3, -4}; 931 int matches = 0; 932 u16 mask; 933 u16 maxval; 934 u16 newval; 935 int i; 936 937 for (i = 0; i < numfields; i++) 938 if (!of_property_read_u32(of_node, field[i], val + i)) 939 matches++; 940 941 if (!matches) 942 return 0; 943 944 *update |= true; 945 946 if (matches < numfields) 947 newval = phy_read_mmd(phydev, 2, reg); 948 else 949 newval = 0; 950 951 maxval = (field_sz == 4) ? 0xf : 0x1f; 952 for (i = 0; i < numfields; i++) 953 if (val[i] != -(i + 1)) { 954 mask = 0xffff; 955 mask ^= maxval << (field_sz * i); 956 newval = (newval & mask) | 957 (((val[i] / KSZ9031_PS_TO_REG) & maxval) 958 << (field_sz * i)); 959 } 960 961 return phy_write_mmd(phydev, 2, reg, newval); 962 } 963 964 /* Center KSZ9031RNX FLP timing at 16ms. */ 965 static int ksz9031_center_flp_timing(struct phy_device *phydev) 966 { 967 int result; 968 969 result = phy_write_mmd(phydev, 0, MII_KSZ9031RN_FLP_BURST_TX_HI, 970 0x0006); 971 if (result) 972 return result; 973 974 result = phy_write_mmd(phydev, 0, MII_KSZ9031RN_FLP_BURST_TX_LO, 975 0x1A80); 976 if (result) 977 return result; 978 979 return genphy_restart_aneg(phydev); 980 } 981 982 /* Enable energy-detect power-down mode */ 983 static int ksz9031_enable_edpd(struct phy_device *phydev) 984 { 985 int reg; 986 987 reg = phy_read_mmd(phydev, 0x1C, MII_KSZ9031RN_EDPD); 988 if (reg < 0) 989 return reg; 990 return phy_write_mmd(phydev, 0x1C, MII_KSZ9031RN_EDPD, 991 reg | MII_KSZ9031RN_EDPD_ENABLE); 992 } 993 994 static int ksz9031_config_rgmii_delay(struct phy_device *phydev) 995 { 996 u16 rx, tx, rx_clk, tx_clk; 997 int ret; 998 999 switch (phydev->interface) { 1000 case PHY_INTERFACE_MODE_RGMII: 1001 tx = TX_ND; 1002 tx_clk = TX_CLK_ND; 1003 rx = RX_ND; 1004 rx_clk = RX_CLK_ND; 1005 break; 1006 case PHY_INTERFACE_MODE_RGMII_ID: 1007 tx = TX_ID; 1008 tx_clk = TX_CLK_ID; 1009 rx = RX_ID; 1010 rx_clk = RX_CLK_ID; 1011 break; 1012 case PHY_INTERFACE_MODE_RGMII_RXID: 1013 tx = TX_ND; 1014 tx_clk = TX_CLK_ND; 1015 rx = RX_ID; 1016 rx_clk = RX_CLK_ID; 1017 break; 1018 case PHY_INTERFACE_MODE_RGMII_TXID: 1019 tx = TX_ID; 1020 tx_clk = TX_CLK_ID; 1021 rx = RX_ND; 1022 rx_clk = RX_CLK_ND; 1023 break; 1024 default: 1025 return 0; 1026 } 1027 1028 ret = phy_write_mmd(phydev, 2, MII_KSZ9031RN_CONTROL_PAD_SKEW, 1029 FIELD_PREP(MII_KSZ9031RN_RX_CTL_M, rx) | 1030 FIELD_PREP(MII_KSZ9031RN_TX_CTL_M, tx)); 1031 if (ret < 0) 1032 return ret; 1033 1034 ret = phy_write_mmd(phydev, 2, MII_KSZ9031RN_RX_DATA_PAD_SKEW, 1035 FIELD_PREP(MII_KSZ9031RN_RXD3, rx) | 1036 FIELD_PREP(MII_KSZ9031RN_RXD2, rx) | 1037 FIELD_PREP(MII_KSZ9031RN_RXD1, rx) | 1038 FIELD_PREP(MII_KSZ9031RN_RXD0, rx)); 1039 if (ret < 0) 1040 return ret; 1041 1042 ret = phy_write_mmd(phydev, 2, MII_KSZ9031RN_TX_DATA_PAD_SKEW, 1043 FIELD_PREP(MII_KSZ9031RN_TXD3, tx) | 1044 FIELD_PREP(MII_KSZ9031RN_TXD2, tx) | 1045 FIELD_PREP(MII_KSZ9031RN_TXD1, tx) | 1046 FIELD_PREP(MII_KSZ9031RN_TXD0, tx)); 1047 if (ret < 0) 1048 return ret; 1049 1050 return phy_write_mmd(phydev, 2, MII_KSZ9031RN_CLK_PAD_SKEW, 1051 FIELD_PREP(MII_KSZ9031RN_GTX_CLK, tx_clk) | 1052 FIELD_PREP(MII_KSZ9031RN_RX_CLK, rx_clk)); 1053 } 1054 1055 static int ksz9031_config_init(struct phy_device *phydev) 1056 { 1057 const struct device_node *of_node; 1058 static const char *clk_skews[2] = {"rxc-skew-ps", "txc-skew-ps"}; 1059 static const char *rx_data_skews[4] = { 1060 "rxd0-skew-ps", "rxd1-skew-ps", 1061 "rxd2-skew-ps", "rxd3-skew-ps" 1062 }; 1063 static const char *tx_data_skews[4] = { 1064 "txd0-skew-ps", "txd1-skew-ps", 1065 "txd2-skew-ps", "txd3-skew-ps" 1066 }; 1067 static const char *control_skews[2] = {"txen-skew-ps", "rxdv-skew-ps"}; 1068 const struct device *dev_walker; 1069 int result; 1070 1071 result = ksz9031_enable_edpd(phydev); 1072 if (result < 0) 1073 return result; 1074 1075 /* The Micrel driver has a deprecated option to place phy OF 1076 * properties in the MAC node. Walk up the tree of devices to 1077 * find a device with an OF node. 1078 */ 1079 dev_walker = &phydev->mdio.dev; 1080 do { 1081 of_node = dev_walker->of_node; 1082 dev_walker = dev_walker->parent; 1083 } while (!of_node && dev_walker); 1084 1085 if (of_node) { 1086 bool update = false; 1087 1088 if (phy_interface_is_rgmii(phydev)) { 1089 result = ksz9031_config_rgmii_delay(phydev); 1090 if (result < 0) 1091 return result; 1092 } 1093 1094 ksz9031_of_load_skew_values(phydev, of_node, 1095 MII_KSZ9031RN_CLK_PAD_SKEW, 5, 1096 clk_skews, 2, &update); 1097 1098 ksz9031_of_load_skew_values(phydev, of_node, 1099 MII_KSZ9031RN_CONTROL_PAD_SKEW, 4, 1100 control_skews, 2, &update); 1101 1102 ksz9031_of_load_skew_values(phydev, of_node, 1103 MII_KSZ9031RN_RX_DATA_PAD_SKEW, 4, 1104 rx_data_skews, 4, &update); 1105 1106 ksz9031_of_load_skew_values(phydev, of_node, 1107 MII_KSZ9031RN_TX_DATA_PAD_SKEW, 4, 1108 tx_data_skews, 4, &update); 1109 1110 if (update && !phy_interface_is_rgmii(phydev)) 1111 phydev_warn(phydev, 1112 "*-skew-ps values should be used only with RGMII PHY modes\n"); 1113 1114 /* Silicon Errata Sheet (DS80000691D or DS80000692D): 1115 * When the device links in the 1000BASE-T slave mode only, 1116 * the optional 125MHz reference output clock (CLK125_NDO) 1117 * has wide duty cycle variation. 1118 * 1119 * The optional CLK125_NDO clock does not meet the RGMII 1120 * 45/55 percent (min/max) duty cycle requirement and therefore 1121 * cannot be used directly by the MAC side for clocking 1122 * applications that have setup/hold time requirements on 1123 * rising and falling clock edges. 1124 * 1125 * Workaround: 1126 * Force the phy to be the master to receive a stable clock 1127 * which meets the duty cycle requirement. 1128 */ 1129 if (of_property_read_bool(of_node, "micrel,force-master")) { 1130 result = phy_read(phydev, MII_CTRL1000); 1131 if (result < 0) 1132 goto err_force_master; 1133 1134 /* enable master mode, config & prefer master */ 1135 result |= CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER; 1136 result = phy_write(phydev, MII_CTRL1000, result); 1137 if (result < 0) 1138 goto err_force_master; 1139 } 1140 } 1141 1142 return ksz9031_center_flp_timing(phydev); 1143 1144 err_force_master: 1145 phydev_err(phydev, "failed to force the phy to master mode\n"); 1146 return result; 1147 } 1148 1149 #define KSZ9131_SKEW_5BIT_MAX 2400 1150 #define KSZ9131_SKEW_4BIT_MAX 800 1151 #define KSZ9131_OFFSET 700 1152 #define KSZ9131_STEP 100 1153 1154 static int ksz9131_of_load_skew_values(struct phy_device *phydev, 1155 struct device_node *of_node, 1156 u16 reg, size_t field_sz, 1157 char *field[], u8 numfields) 1158 { 1159 int val[4] = {-(1 + KSZ9131_OFFSET), -(2 + KSZ9131_OFFSET), 1160 -(3 + KSZ9131_OFFSET), -(4 + KSZ9131_OFFSET)}; 1161 int skewval, skewmax = 0; 1162 int matches = 0; 1163 u16 maxval; 1164 u16 newval; 1165 u16 mask; 1166 int i; 1167 1168 /* psec properties in dts should mean x pico seconds */ 1169 if (field_sz == 5) 1170 skewmax = KSZ9131_SKEW_5BIT_MAX; 1171 else 1172 skewmax = KSZ9131_SKEW_4BIT_MAX; 1173 1174 for (i = 0; i < numfields; i++) 1175 if (!of_property_read_s32(of_node, field[i], &skewval)) { 1176 if (skewval < -KSZ9131_OFFSET) 1177 skewval = -KSZ9131_OFFSET; 1178 else if (skewval > skewmax) 1179 skewval = skewmax; 1180 1181 val[i] = skewval + KSZ9131_OFFSET; 1182 matches++; 1183 } 1184 1185 if (!matches) 1186 return 0; 1187 1188 if (matches < numfields) 1189 newval = phy_read_mmd(phydev, 2, reg); 1190 else 1191 newval = 0; 1192 1193 maxval = (field_sz == 4) ? 0xf : 0x1f; 1194 for (i = 0; i < numfields; i++) 1195 if (val[i] != -(i + 1 + KSZ9131_OFFSET)) { 1196 mask = 0xffff; 1197 mask ^= maxval << (field_sz * i); 1198 newval = (newval & mask) | 1199 (((val[i] / KSZ9131_STEP) & maxval) 1200 << (field_sz * i)); 1201 } 1202 1203 return phy_write_mmd(phydev, 2, reg, newval); 1204 } 1205 1206 #define KSZ9131RN_MMD_COMMON_CTRL_REG 2 1207 #define KSZ9131RN_RXC_DLL_CTRL 76 1208 #define KSZ9131RN_TXC_DLL_CTRL 77 1209 #define KSZ9131RN_DLL_ENABLE_DELAY 0 1210 1211 static int ksz9131_config_rgmii_delay(struct phy_device *phydev) 1212 { 1213 const struct kszphy_type *type = phydev->drv->driver_data; 1214 u16 rxcdll_val, txcdll_val; 1215 int ret; 1216 1217 switch (phydev->interface) { 1218 case PHY_INTERFACE_MODE_RGMII: 1219 rxcdll_val = type->disable_dll_rx_bit; 1220 txcdll_val = type->disable_dll_tx_bit; 1221 break; 1222 case PHY_INTERFACE_MODE_RGMII_ID: 1223 rxcdll_val = KSZ9131RN_DLL_ENABLE_DELAY; 1224 txcdll_val = KSZ9131RN_DLL_ENABLE_DELAY; 1225 break; 1226 case PHY_INTERFACE_MODE_RGMII_RXID: 1227 rxcdll_val = KSZ9131RN_DLL_ENABLE_DELAY; 1228 txcdll_val = type->disable_dll_tx_bit; 1229 break; 1230 case PHY_INTERFACE_MODE_RGMII_TXID: 1231 rxcdll_val = type->disable_dll_rx_bit; 1232 txcdll_val = KSZ9131RN_DLL_ENABLE_DELAY; 1233 break; 1234 default: 1235 return 0; 1236 } 1237 1238 ret = phy_modify_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG, 1239 KSZ9131RN_RXC_DLL_CTRL, type->disable_dll_mask, 1240 rxcdll_val); 1241 if (ret < 0) 1242 return ret; 1243 1244 return phy_modify_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG, 1245 KSZ9131RN_TXC_DLL_CTRL, type->disable_dll_mask, 1246 txcdll_val); 1247 } 1248 1249 /* Silicon Errata DS80000693B 1250 * 1251 * When LEDs are configured in Individual Mode, LED1 is ON in a no-link 1252 * condition. Workaround is to set register 0x1e, bit 9, this way LED1 behaves 1253 * according to the datasheet (off if there is no link). 1254 */ 1255 static int ksz9131_led_errata(struct phy_device *phydev) 1256 { 1257 int reg; 1258 1259 reg = phy_read_mmd(phydev, 2, 0); 1260 if (reg < 0) 1261 return reg; 1262 1263 if (!(reg & BIT(4))) 1264 return 0; 1265 1266 return phy_set_bits(phydev, 0x1e, BIT(9)); 1267 } 1268 1269 static int ksz9131_config_init(struct phy_device *phydev) 1270 { 1271 struct device_node *of_node; 1272 char *clk_skews[2] = {"rxc-skew-psec", "txc-skew-psec"}; 1273 char *rx_data_skews[4] = { 1274 "rxd0-skew-psec", "rxd1-skew-psec", 1275 "rxd2-skew-psec", "rxd3-skew-psec" 1276 }; 1277 char *tx_data_skews[4] = { 1278 "txd0-skew-psec", "txd1-skew-psec", 1279 "txd2-skew-psec", "txd3-skew-psec" 1280 }; 1281 char *control_skews[2] = {"txen-skew-psec", "rxdv-skew-psec"}; 1282 const struct device *dev_walker; 1283 int ret; 1284 1285 dev_walker = &phydev->mdio.dev; 1286 do { 1287 of_node = dev_walker->of_node; 1288 dev_walker = dev_walker->parent; 1289 } while (!of_node && dev_walker); 1290 1291 if (!of_node) 1292 return 0; 1293 1294 if (phy_interface_is_rgmii(phydev)) { 1295 ret = ksz9131_config_rgmii_delay(phydev); 1296 if (ret < 0) 1297 return ret; 1298 } 1299 1300 ret = ksz9131_of_load_skew_values(phydev, of_node, 1301 MII_KSZ9031RN_CLK_PAD_SKEW, 5, 1302 clk_skews, 2); 1303 if (ret < 0) 1304 return ret; 1305 1306 ret = ksz9131_of_load_skew_values(phydev, of_node, 1307 MII_KSZ9031RN_CONTROL_PAD_SKEW, 4, 1308 control_skews, 2); 1309 if (ret < 0) 1310 return ret; 1311 1312 ret = ksz9131_of_load_skew_values(phydev, of_node, 1313 MII_KSZ9031RN_RX_DATA_PAD_SKEW, 4, 1314 rx_data_skews, 4); 1315 if (ret < 0) 1316 return ret; 1317 1318 ret = ksz9131_of_load_skew_values(phydev, of_node, 1319 MII_KSZ9031RN_TX_DATA_PAD_SKEW, 4, 1320 tx_data_skews, 4); 1321 if (ret < 0) 1322 return ret; 1323 1324 ret = ksz9131_led_errata(phydev); 1325 if (ret < 0) 1326 return ret; 1327 1328 return 0; 1329 } 1330 1331 #define MII_KSZ9131_AUTO_MDIX 0x1C 1332 #define MII_KSZ9131_AUTO_MDI_SET BIT(7) 1333 #define MII_KSZ9131_AUTO_MDIX_SWAP_OFF BIT(6) 1334 1335 static int ksz9131_mdix_update(struct phy_device *phydev) 1336 { 1337 int ret; 1338 1339 ret = phy_read(phydev, MII_KSZ9131_AUTO_MDIX); 1340 if (ret < 0) 1341 return ret; 1342 1343 if (ret & MII_KSZ9131_AUTO_MDIX_SWAP_OFF) { 1344 if (ret & MII_KSZ9131_AUTO_MDI_SET) 1345 phydev->mdix_ctrl = ETH_TP_MDI; 1346 else 1347 phydev->mdix_ctrl = ETH_TP_MDI_X; 1348 } else { 1349 phydev->mdix_ctrl = ETH_TP_MDI_AUTO; 1350 } 1351 1352 if (ret & MII_KSZ9131_AUTO_MDI_SET) 1353 phydev->mdix = ETH_TP_MDI; 1354 else 1355 phydev->mdix = ETH_TP_MDI_X; 1356 1357 return 0; 1358 } 1359 1360 static int ksz9131_config_mdix(struct phy_device *phydev, u8 ctrl) 1361 { 1362 u16 val; 1363 1364 switch (ctrl) { 1365 case ETH_TP_MDI: 1366 val = MII_KSZ9131_AUTO_MDIX_SWAP_OFF | 1367 MII_KSZ9131_AUTO_MDI_SET; 1368 break; 1369 case ETH_TP_MDI_X: 1370 val = MII_KSZ9131_AUTO_MDIX_SWAP_OFF; 1371 break; 1372 case ETH_TP_MDI_AUTO: 1373 val = 0; 1374 break; 1375 default: 1376 return 0; 1377 } 1378 1379 return phy_modify(phydev, MII_KSZ9131_AUTO_MDIX, 1380 MII_KSZ9131_AUTO_MDIX_SWAP_OFF | 1381 MII_KSZ9131_AUTO_MDI_SET, val); 1382 } 1383 1384 static int ksz9131_read_status(struct phy_device *phydev) 1385 { 1386 int ret; 1387 1388 ret = ksz9131_mdix_update(phydev); 1389 if (ret < 0) 1390 return ret; 1391 1392 return genphy_read_status(phydev); 1393 } 1394 1395 static int ksz9131_config_aneg(struct phy_device *phydev) 1396 { 1397 int ret; 1398 1399 ret = ksz9131_config_mdix(phydev, phydev->mdix_ctrl); 1400 if (ret) 1401 return ret; 1402 1403 return genphy_config_aneg(phydev); 1404 } 1405 1406 static int ksz9477_get_features(struct phy_device *phydev) 1407 { 1408 int ret; 1409 1410 ret = genphy_read_abilities(phydev); 1411 if (ret) 1412 return ret; 1413 1414 /* The "EEE control and capability 1" (Register 3.20) seems to be 1415 * influenced by the "EEE advertisement 1" (Register 7.60). Changes 1416 * on the 7.60 will affect 3.20. So, we need to construct our own list 1417 * of caps. 1418 * KSZ8563R should have 100BaseTX/Full only. 1419 */ 1420 linkmode_and(phydev->supported_eee, phydev->supported, 1421 PHY_EEE_CAP1_FEATURES); 1422 1423 return 0; 1424 } 1425 1426 #define KSZ8873MLL_GLOBAL_CONTROL_4 0x06 1427 #define KSZ8873MLL_GLOBAL_CONTROL_4_DUPLEX BIT(6) 1428 #define KSZ8873MLL_GLOBAL_CONTROL_4_SPEED BIT(4) 1429 static int ksz8873mll_read_status(struct phy_device *phydev) 1430 { 1431 int regval; 1432 1433 /* dummy read */ 1434 regval = phy_read(phydev, KSZ8873MLL_GLOBAL_CONTROL_4); 1435 1436 regval = phy_read(phydev, KSZ8873MLL_GLOBAL_CONTROL_4); 1437 1438 if (regval & KSZ8873MLL_GLOBAL_CONTROL_4_DUPLEX) 1439 phydev->duplex = DUPLEX_HALF; 1440 else 1441 phydev->duplex = DUPLEX_FULL; 1442 1443 if (regval & KSZ8873MLL_GLOBAL_CONTROL_4_SPEED) 1444 phydev->speed = SPEED_10; 1445 else 1446 phydev->speed = SPEED_100; 1447 1448 phydev->link = 1; 1449 phydev->pause = phydev->asym_pause = 0; 1450 1451 return 0; 1452 } 1453 1454 static int ksz9031_get_features(struct phy_device *phydev) 1455 { 1456 int ret; 1457 1458 ret = genphy_read_abilities(phydev); 1459 if (ret < 0) 1460 return ret; 1461 1462 /* Silicon Errata Sheet (DS80000691D or DS80000692D): 1463 * Whenever the device's Asymmetric Pause capability is set to 1, 1464 * link-up may fail after a link-up to link-down transition. 1465 * 1466 * The Errata Sheet is for ksz9031, but ksz9021 has the same issue 1467 * 1468 * Workaround: 1469 * Do not enable the Asymmetric Pause capability bit. 1470 */ 1471 linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported); 1472 1473 /* We force setting the Pause capability as the core will force the 1474 * Asymmetric Pause capability to 1 otherwise. 1475 */ 1476 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported); 1477 1478 return 0; 1479 } 1480 1481 static int ksz9031_read_status(struct phy_device *phydev) 1482 { 1483 int err; 1484 int regval; 1485 1486 err = genphy_read_status(phydev); 1487 if (err) 1488 return err; 1489 1490 /* Make sure the PHY is not broken. Read idle error count, 1491 * and reset the PHY if it is maxed out. 1492 */ 1493 regval = phy_read(phydev, MII_STAT1000); 1494 if ((regval & 0xFF) == 0xFF) { 1495 phy_init_hw(phydev); 1496 phydev->link = 0; 1497 if (phydev->drv->config_intr && phy_interrupt_is_valid(phydev)) 1498 phydev->drv->config_intr(phydev); 1499 return genphy_config_aneg(phydev); 1500 } 1501 1502 return 0; 1503 } 1504 1505 static int ksz9x31_cable_test_start(struct phy_device *phydev) 1506 { 1507 struct kszphy_priv *priv = phydev->priv; 1508 int ret; 1509 1510 /* KSZ9131RNX, DS00002841B-page 38, 4.14 LinkMD (R) Cable Diagnostic 1511 * Prior to running the cable diagnostics, Auto-negotiation should 1512 * be disabled, full duplex set and the link speed set to 1000Mbps 1513 * via the Basic Control Register. 1514 */ 1515 ret = phy_modify(phydev, MII_BMCR, 1516 BMCR_SPEED1000 | BMCR_FULLDPLX | 1517 BMCR_ANENABLE | BMCR_SPEED100, 1518 BMCR_SPEED1000 | BMCR_FULLDPLX); 1519 if (ret) 1520 return ret; 1521 1522 /* KSZ9131RNX, DS00002841B-page 38, 4.14 LinkMD (R) Cable Diagnostic 1523 * The Master-Slave configuration should be set to Slave by writing 1524 * a value of 0x1000 to the Auto-Negotiation Master Slave Control 1525 * Register. 1526 */ 1527 ret = phy_read(phydev, MII_CTRL1000); 1528 if (ret < 0) 1529 return ret; 1530 1531 /* Cache these bits, they need to be restored once LinkMD finishes. */ 1532 priv->vct_ctrl1000 = ret & (CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER); 1533 ret &= ~(CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER); 1534 ret |= CTL1000_ENABLE_MASTER; 1535 1536 return phy_write(phydev, MII_CTRL1000, ret); 1537 } 1538 1539 static int ksz9x31_cable_test_result_trans(u16 status) 1540 { 1541 switch (FIELD_GET(KSZ9x31_LMD_VCT_ST_MASK, status)) { 1542 case KSZ9x31_LMD_VCT_ST_NORMAL: 1543 return ETHTOOL_A_CABLE_RESULT_CODE_OK; 1544 case KSZ9x31_LMD_VCT_ST_OPEN: 1545 return ETHTOOL_A_CABLE_RESULT_CODE_OPEN; 1546 case KSZ9x31_LMD_VCT_ST_SHORT: 1547 return ETHTOOL_A_CABLE_RESULT_CODE_SAME_SHORT; 1548 case KSZ9x31_LMD_VCT_ST_FAIL: 1549 fallthrough; 1550 default: 1551 return ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC; 1552 } 1553 } 1554 1555 static bool ksz9x31_cable_test_failed(u16 status) 1556 { 1557 int stat = FIELD_GET(KSZ9x31_LMD_VCT_ST_MASK, status); 1558 1559 return stat == KSZ9x31_LMD_VCT_ST_FAIL; 1560 } 1561 1562 static bool ksz9x31_cable_test_fault_length_valid(u16 status) 1563 { 1564 switch (FIELD_GET(KSZ9x31_LMD_VCT_ST_MASK, status)) { 1565 case KSZ9x31_LMD_VCT_ST_OPEN: 1566 fallthrough; 1567 case KSZ9x31_LMD_VCT_ST_SHORT: 1568 return true; 1569 } 1570 return false; 1571 } 1572 1573 static int ksz9x31_cable_test_fault_length(struct phy_device *phydev, u16 stat) 1574 { 1575 int dt = FIELD_GET(KSZ9x31_LMD_VCT_DATA_MASK, stat); 1576 1577 /* KSZ9131RNX, DS00002841B-page 38, 4.14 LinkMD (R) Cable Diagnostic 1578 * 1579 * distance to fault = (VCT_DATA - 22) * 4 / cable propagation velocity 1580 */ 1581 if (phydev_id_compare(phydev, PHY_ID_KSZ9131)) 1582 dt = clamp(dt - 22, 0, 255); 1583 1584 return (dt * 400) / 10; 1585 } 1586 1587 static int ksz9x31_cable_test_wait_for_completion(struct phy_device *phydev) 1588 { 1589 int val, ret; 1590 1591 ret = phy_read_poll_timeout(phydev, KSZ9x31_LMD, val, 1592 !(val & KSZ9x31_LMD_VCT_EN), 1593 30000, 100000, true); 1594 1595 return ret < 0 ? ret : 0; 1596 } 1597 1598 static int ksz9x31_cable_test_get_pair(int pair) 1599 { 1600 static const int ethtool_pair[] = { 1601 ETHTOOL_A_CABLE_PAIR_A, 1602 ETHTOOL_A_CABLE_PAIR_B, 1603 ETHTOOL_A_CABLE_PAIR_C, 1604 ETHTOOL_A_CABLE_PAIR_D, 1605 }; 1606 1607 return ethtool_pair[pair]; 1608 } 1609 1610 static int ksz9x31_cable_test_one_pair(struct phy_device *phydev, int pair) 1611 { 1612 int ret, val; 1613 1614 /* KSZ9131RNX, DS00002841B-page 38, 4.14 LinkMD (R) Cable Diagnostic 1615 * To test each individual cable pair, set the cable pair in the Cable 1616 * Diagnostics Test Pair (VCT_PAIR[1:0]) field of the LinkMD Cable 1617 * Diagnostic Register, along with setting the Cable Diagnostics Test 1618 * Enable (VCT_EN) bit. The Cable Diagnostics Test Enable (VCT_EN) bit 1619 * will self clear when the test is concluded. 1620 */ 1621 ret = phy_write(phydev, KSZ9x31_LMD, 1622 KSZ9x31_LMD_VCT_EN | KSZ9x31_LMD_VCT_PAIR(pair)); 1623 if (ret) 1624 return ret; 1625 1626 ret = ksz9x31_cable_test_wait_for_completion(phydev); 1627 if (ret) 1628 return ret; 1629 1630 val = phy_read(phydev, KSZ9x31_LMD); 1631 if (val < 0) 1632 return val; 1633 1634 if (ksz9x31_cable_test_failed(val)) 1635 return -EAGAIN; 1636 1637 ret = ethnl_cable_test_result(phydev, 1638 ksz9x31_cable_test_get_pair(pair), 1639 ksz9x31_cable_test_result_trans(val)); 1640 if (ret) 1641 return ret; 1642 1643 if (!ksz9x31_cable_test_fault_length_valid(val)) 1644 return 0; 1645 1646 return ethnl_cable_test_fault_length(phydev, 1647 ksz9x31_cable_test_get_pair(pair), 1648 ksz9x31_cable_test_fault_length(phydev, val)); 1649 } 1650 1651 static int ksz9x31_cable_test_get_status(struct phy_device *phydev, 1652 bool *finished) 1653 { 1654 struct kszphy_priv *priv = phydev->priv; 1655 unsigned long pair_mask = 0xf; 1656 int retries = 20; 1657 int pair, ret, rv; 1658 1659 *finished = false; 1660 1661 /* Try harder if link partner is active */ 1662 while (pair_mask && retries--) { 1663 for_each_set_bit(pair, &pair_mask, 4) { 1664 ret = ksz9x31_cable_test_one_pair(phydev, pair); 1665 if (ret == -EAGAIN) 1666 continue; 1667 if (ret < 0) 1668 return ret; 1669 clear_bit(pair, &pair_mask); 1670 } 1671 /* If link partner is in autonegotiation mode it will send 2ms 1672 * of FLPs with at least 6ms of silence. 1673 * Add 2ms sleep to have better chances to hit this silence. 1674 */ 1675 if (pair_mask) 1676 usleep_range(2000, 3000); 1677 } 1678 1679 /* Report remaining unfinished pair result as unknown. */ 1680 for_each_set_bit(pair, &pair_mask, 4) { 1681 ret = ethnl_cable_test_result(phydev, 1682 ksz9x31_cable_test_get_pair(pair), 1683 ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC); 1684 } 1685 1686 *finished = true; 1687 1688 /* Restore cached bits from before LinkMD got started. */ 1689 rv = phy_modify(phydev, MII_CTRL1000, 1690 CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER, 1691 priv->vct_ctrl1000); 1692 if (rv) 1693 return rv; 1694 1695 return ret; 1696 } 1697 1698 static int ksz8873mll_config_aneg(struct phy_device *phydev) 1699 { 1700 return 0; 1701 } 1702 1703 static int ksz886x_config_mdix(struct phy_device *phydev, u8 ctrl) 1704 { 1705 u16 val; 1706 1707 switch (ctrl) { 1708 case ETH_TP_MDI: 1709 val = KSZ886X_BMCR_DISABLE_AUTO_MDIX; 1710 break; 1711 case ETH_TP_MDI_X: 1712 /* Note: The naming of the bit KSZ886X_BMCR_FORCE_MDI is bit 1713 * counter intuitive, the "-X" in "1 = Force MDI" in the data 1714 * sheet seems to be missing: 1715 * 1 = Force MDI (sic!) (transmit on RX+/RX- pins) 1716 * 0 = Normal operation (transmit on TX+/TX- pins) 1717 */ 1718 val = KSZ886X_BMCR_DISABLE_AUTO_MDIX | KSZ886X_BMCR_FORCE_MDI; 1719 break; 1720 case ETH_TP_MDI_AUTO: 1721 val = 0; 1722 break; 1723 default: 1724 return 0; 1725 } 1726 1727 return phy_modify(phydev, MII_BMCR, 1728 KSZ886X_BMCR_HP_MDIX | KSZ886X_BMCR_FORCE_MDI | 1729 KSZ886X_BMCR_DISABLE_AUTO_MDIX, 1730 KSZ886X_BMCR_HP_MDIX | val); 1731 } 1732 1733 static int ksz886x_config_aneg(struct phy_device *phydev) 1734 { 1735 int ret; 1736 1737 ret = genphy_config_aneg(phydev); 1738 if (ret) 1739 return ret; 1740 1741 /* The MDI-X configuration is automatically changed by the PHY after 1742 * switching from autoneg off to on. So, take MDI-X configuration under 1743 * own control and set it after autoneg configuration was done. 1744 */ 1745 return ksz886x_config_mdix(phydev, phydev->mdix_ctrl); 1746 } 1747 1748 static int ksz886x_mdix_update(struct phy_device *phydev) 1749 { 1750 int ret; 1751 1752 ret = phy_read(phydev, MII_BMCR); 1753 if (ret < 0) 1754 return ret; 1755 1756 if (ret & KSZ886X_BMCR_DISABLE_AUTO_MDIX) { 1757 if (ret & KSZ886X_BMCR_FORCE_MDI) 1758 phydev->mdix_ctrl = ETH_TP_MDI_X; 1759 else 1760 phydev->mdix_ctrl = ETH_TP_MDI; 1761 } else { 1762 phydev->mdix_ctrl = ETH_TP_MDI_AUTO; 1763 } 1764 1765 ret = phy_read(phydev, MII_KSZPHY_CTRL); 1766 if (ret < 0) 1767 return ret; 1768 1769 /* Same reverse logic as KSZ886X_BMCR_FORCE_MDI */ 1770 if (ret & KSZ886X_CTRL_MDIX_STAT) 1771 phydev->mdix = ETH_TP_MDI_X; 1772 else 1773 phydev->mdix = ETH_TP_MDI; 1774 1775 return 0; 1776 } 1777 1778 static int ksz886x_read_status(struct phy_device *phydev) 1779 { 1780 int ret; 1781 1782 ret = ksz886x_mdix_update(phydev); 1783 if (ret < 0) 1784 return ret; 1785 1786 return genphy_read_status(phydev); 1787 } 1788 1789 struct ksz9477_errata_write { 1790 u8 dev_addr; 1791 u8 reg_addr; 1792 u16 val; 1793 }; 1794 1795 static const struct ksz9477_errata_write ksz9477_errata_writes[] = { 1796 /* Register settings are needed to improve PHY receive performance */ 1797 {0x01, 0x6f, 0xdd0b}, 1798 {0x01, 0x8f, 0x6032}, 1799 {0x01, 0x9d, 0x248c}, 1800 {0x01, 0x75, 0x0060}, 1801 {0x01, 0xd3, 0x7777}, 1802 {0x1c, 0x06, 0x3008}, 1803 {0x1c, 0x08, 0x2000}, 1804 1805 /* Transmit waveform amplitude can be improved (1000BASE-T, 100BASE-TX, 10BASE-Te) */ 1806 {0x1c, 0x04, 0x00d0}, 1807 1808 /* Register settings are required to meet data sheet supply current specifications */ 1809 {0x1c, 0x13, 0x6eff}, 1810 {0x1c, 0x14, 0xe6ff}, 1811 {0x1c, 0x15, 0x6eff}, 1812 {0x1c, 0x16, 0xe6ff}, 1813 {0x1c, 0x17, 0x00ff}, 1814 {0x1c, 0x18, 0x43ff}, 1815 {0x1c, 0x19, 0xc3ff}, 1816 {0x1c, 0x1a, 0x6fff}, 1817 {0x1c, 0x1b, 0x07ff}, 1818 {0x1c, 0x1c, 0x0fff}, 1819 {0x1c, 0x1d, 0xe7ff}, 1820 {0x1c, 0x1e, 0xefff}, 1821 {0x1c, 0x20, 0xeeee}, 1822 }; 1823 1824 static int ksz9477_config_init(struct phy_device *phydev) 1825 { 1826 int err; 1827 int i; 1828 1829 /* Apply PHY settings to address errata listed in 1830 * KSZ9477, KSZ9897, KSZ9896, KSZ9567, KSZ8565 1831 * Silicon Errata and Data Sheet Clarification documents. 1832 * 1833 * Document notes: Before configuring the PHY MMD registers, it is 1834 * necessary to set the PHY to 100 Mbps speed with auto-negotiation 1835 * disabled by writing to register 0xN100-0xN101. After writing the 1836 * MMD registers, and after all errata workarounds that involve PHY 1837 * register settings, write register 0xN100-0xN101 again to enable 1838 * and restart auto-negotiation. 1839 */ 1840 err = phy_write(phydev, MII_BMCR, BMCR_SPEED100 | BMCR_FULLDPLX); 1841 if (err) 1842 return err; 1843 1844 for (i = 0; i < ARRAY_SIZE(ksz9477_errata_writes); ++i) { 1845 const struct ksz9477_errata_write *errata = &ksz9477_errata_writes[i]; 1846 1847 err = phy_write_mmd(phydev, errata->dev_addr, errata->reg_addr, errata->val); 1848 if (err) 1849 return err; 1850 } 1851 1852 /* According to KSZ9477 Errata DS80000754C (Module 4) all EEE modes 1853 * in this switch shall be regarded as broken. 1854 */ 1855 if (phydev->dev_flags & MICREL_NO_EEE) 1856 phydev->eee_broken_modes = -1; 1857 1858 err = genphy_restart_aneg(phydev); 1859 if (err) 1860 return err; 1861 1862 return kszphy_config_init(phydev); 1863 } 1864 1865 static int kszphy_get_sset_count(struct phy_device *phydev) 1866 { 1867 return ARRAY_SIZE(kszphy_hw_stats); 1868 } 1869 1870 static void kszphy_get_strings(struct phy_device *phydev, u8 *data) 1871 { 1872 int i; 1873 1874 for (i = 0; i < ARRAY_SIZE(kszphy_hw_stats); i++) { 1875 strscpy(data + i * ETH_GSTRING_LEN, 1876 kszphy_hw_stats[i].string, ETH_GSTRING_LEN); 1877 } 1878 } 1879 1880 static u64 kszphy_get_stat(struct phy_device *phydev, int i) 1881 { 1882 struct kszphy_hw_stat stat = kszphy_hw_stats[i]; 1883 struct kszphy_priv *priv = phydev->priv; 1884 int val; 1885 u64 ret; 1886 1887 val = phy_read(phydev, stat.reg); 1888 if (val < 0) { 1889 ret = U64_MAX; 1890 } else { 1891 val = val & ((1 << stat.bits) - 1); 1892 priv->stats[i] += val; 1893 ret = priv->stats[i]; 1894 } 1895 1896 return ret; 1897 } 1898 1899 static void kszphy_get_stats(struct phy_device *phydev, 1900 struct ethtool_stats *stats, u64 *data) 1901 { 1902 int i; 1903 1904 for (i = 0; i < ARRAY_SIZE(kszphy_hw_stats); i++) 1905 data[i] = kszphy_get_stat(phydev, i); 1906 } 1907 1908 static int kszphy_suspend(struct phy_device *phydev) 1909 { 1910 /* Disable PHY Interrupts */ 1911 if (phy_interrupt_is_valid(phydev)) { 1912 phydev->interrupts = PHY_INTERRUPT_DISABLED; 1913 if (phydev->drv->config_intr) 1914 phydev->drv->config_intr(phydev); 1915 } 1916 1917 return genphy_suspend(phydev); 1918 } 1919 1920 static void kszphy_parse_led_mode(struct phy_device *phydev) 1921 { 1922 const struct kszphy_type *type = phydev->drv->driver_data; 1923 const struct device_node *np = phydev->mdio.dev.of_node; 1924 struct kszphy_priv *priv = phydev->priv; 1925 int ret; 1926 1927 if (type && type->led_mode_reg) { 1928 ret = of_property_read_u32(np, "micrel,led-mode", 1929 &priv->led_mode); 1930 1931 if (ret) 1932 priv->led_mode = -1; 1933 1934 if (priv->led_mode > 3) { 1935 phydev_err(phydev, "invalid led mode: 0x%02x\n", 1936 priv->led_mode); 1937 priv->led_mode = -1; 1938 } 1939 } else { 1940 priv->led_mode = -1; 1941 } 1942 } 1943 1944 static int kszphy_resume(struct phy_device *phydev) 1945 { 1946 int ret; 1947 1948 genphy_resume(phydev); 1949 1950 /* After switching from power-down to normal mode, an internal global 1951 * reset is automatically generated. Wait a minimum of 1 ms before 1952 * read/write access to the PHY registers. 1953 */ 1954 usleep_range(1000, 2000); 1955 1956 ret = kszphy_config_reset(phydev); 1957 if (ret) 1958 return ret; 1959 1960 /* Enable PHY Interrupts */ 1961 if (phy_interrupt_is_valid(phydev)) { 1962 phydev->interrupts = PHY_INTERRUPT_ENABLED; 1963 if (phydev->drv->config_intr) 1964 phydev->drv->config_intr(phydev); 1965 } 1966 1967 return 0; 1968 } 1969 1970 static int kszphy_probe(struct phy_device *phydev) 1971 { 1972 const struct kszphy_type *type = phydev->drv->driver_data; 1973 const struct device_node *np = phydev->mdio.dev.of_node; 1974 struct kszphy_priv *priv; 1975 struct clk *clk; 1976 1977 priv = devm_kzalloc(&phydev->mdio.dev, sizeof(*priv), GFP_KERNEL); 1978 if (!priv) 1979 return -ENOMEM; 1980 1981 phydev->priv = priv; 1982 1983 priv->type = type; 1984 1985 kszphy_parse_led_mode(phydev); 1986 1987 clk = devm_clk_get(&phydev->mdio.dev, "rmii-ref"); 1988 /* NOTE: clk may be NULL if building without CONFIG_HAVE_CLK */ 1989 if (!IS_ERR_OR_NULL(clk)) { 1990 unsigned long rate = clk_get_rate(clk); 1991 bool rmii_ref_clk_sel_25_mhz; 1992 1993 if (type) 1994 priv->rmii_ref_clk_sel = type->has_rmii_ref_clk_sel; 1995 rmii_ref_clk_sel_25_mhz = of_property_read_bool(np, 1996 "micrel,rmii-reference-clock-select-25-mhz"); 1997 1998 if (rate > 24500000 && rate < 25500000) { 1999 priv->rmii_ref_clk_sel_val = rmii_ref_clk_sel_25_mhz; 2000 } else if (rate > 49500000 && rate < 50500000) { 2001 priv->rmii_ref_clk_sel_val = !rmii_ref_clk_sel_25_mhz; 2002 } else { 2003 phydev_err(phydev, "Clock rate out of range: %ld\n", 2004 rate); 2005 return -EINVAL; 2006 } 2007 } 2008 2009 if (ksz8041_fiber_mode(phydev)) 2010 phydev->port = PORT_FIBRE; 2011 2012 /* Support legacy board-file configuration */ 2013 if (phydev->dev_flags & MICREL_PHY_50MHZ_CLK) { 2014 priv->rmii_ref_clk_sel = true; 2015 priv->rmii_ref_clk_sel_val = true; 2016 } 2017 2018 return 0; 2019 } 2020 2021 static int lan8814_cable_test_start(struct phy_device *phydev) 2022 { 2023 /* If autoneg is enabled, we won't be able to test cross pair 2024 * short. In this case, the PHY will "detect" a link and 2025 * confuse the internal state machine - disable auto neg here. 2026 * Set the speed to 1000mbit and full duplex. 2027 */ 2028 return phy_modify(phydev, MII_BMCR, BMCR_ANENABLE | BMCR_SPEED100, 2029 BMCR_SPEED1000 | BMCR_FULLDPLX); 2030 } 2031 2032 static int ksz886x_cable_test_start(struct phy_device *phydev) 2033 { 2034 if (phydev->dev_flags & MICREL_KSZ8_P1_ERRATA) 2035 return -EOPNOTSUPP; 2036 2037 /* If autoneg is enabled, we won't be able to test cross pair 2038 * short. In this case, the PHY will "detect" a link and 2039 * confuse the internal state machine - disable auto neg here. 2040 * If autoneg is disabled, we should set the speed to 10mbit. 2041 */ 2042 return phy_clear_bits(phydev, MII_BMCR, BMCR_ANENABLE | BMCR_SPEED100); 2043 } 2044 2045 static __always_inline int ksz886x_cable_test_result_trans(u16 status, u16 mask) 2046 { 2047 switch (FIELD_GET(mask, status)) { 2048 case KSZ8081_LMD_STAT_NORMAL: 2049 return ETHTOOL_A_CABLE_RESULT_CODE_OK; 2050 case KSZ8081_LMD_STAT_SHORT: 2051 return ETHTOOL_A_CABLE_RESULT_CODE_SAME_SHORT; 2052 case KSZ8081_LMD_STAT_OPEN: 2053 return ETHTOOL_A_CABLE_RESULT_CODE_OPEN; 2054 case KSZ8081_LMD_STAT_FAIL: 2055 fallthrough; 2056 default: 2057 return ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC; 2058 } 2059 } 2060 2061 static __always_inline bool ksz886x_cable_test_failed(u16 status, u16 mask) 2062 { 2063 return FIELD_GET(mask, status) == 2064 KSZ8081_LMD_STAT_FAIL; 2065 } 2066 2067 static __always_inline bool ksz886x_cable_test_fault_length_valid(u16 status, u16 mask) 2068 { 2069 switch (FIELD_GET(mask, status)) { 2070 case KSZ8081_LMD_STAT_OPEN: 2071 fallthrough; 2072 case KSZ8081_LMD_STAT_SHORT: 2073 return true; 2074 } 2075 return false; 2076 } 2077 2078 static __always_inline int ksz886x_cable_test_fault_length(struct phy_device *phydev, 2079 u16 status, u16 data_mask) 2080 { 2081 int dt; 2082 2083 /* According to the data sheet the distance to the fault is 2084 * DELTA_TIME * 0.4 meters for ksz phys. 2085 * (DELTA_TIME - 22) * 0.8 for lan8814 phy. 2086 */ 2087 dt = FIELD_GET(data_mask, status); 2088 2089 if (phydev_id_compare(phydev, PHY_ID_LAN8814)) 2090 return ((dt - 22) * 800) / 10; 2091 else 2092 return (dt * 400) / 10; 2093 } 2094 2095 static int ksz886x_cable_test_wait_for_completion(struct phy_device *phydev) 2096 { 2097 const struct kszphy_type *type = phydev->drv->driver_data; 2098 int val, ret; 2099 2100 ret = phy_read_poll_timeout(phydev, type->cable_diag_reg, val, 2101 !(val & KSZ8081_LMD_ENABLE_TEST), 2102 30000, 100000, true); 2103 2104 return ret < 0 ? ret : 0; 2105 } 2106 2107 static int lan8814_cable_test_one_pair(struct phy_device *phydev, int pair) 2108 { 2109 static const int ethtool_pair[] = { ETHTOOL_A_CABLE_PAIR_A, 2110 ETHTOOL_A_CABLE_PAIR_B, 2111 ETHTOOL_A_CABLE_PAIR_C, 2112 ETHTOOL_A_CABLE_PAIR_D, 2113 }; 2114 u32 fault_length; 2115 int ret; 2116 int val; 2117 2118 val = KSZ8081_LMD_ENABLE_TEST; 2119 val = val | (pair << LAN8814_PAIR_BIT_SHIFT); 2120 2121 ret = phy_write(phydev, LAN8814_CABLE_DIAG, val); 2122 if (ret < 0) 2123 return ret; 2124 2125 ret = ksz886x_cable_test_wait_for_completion(phydev); 2126 if (ret) 2127 return ret; 2128 2129 val = phy_read(phydev, LAN8814_CABLE_DIAG); 2130 if (val < 0) 2131 return val; 2132 2133 if (ksz886x_cable_test_failed(val, LAN8814_CABLE_DIAG_STAT_MASK)) 2134 return -EAGAIN; 2135 2136 ret = ethnl_cable_test_result(phydev, ethtool_pair[pair], 2137 ksz886x_cable_test_result_trans(val, 2138 LAN8814_CABLE_DIAG_STAT_MASK 2139 )); 2140 if (ret) 2141 return ret; 2142 2143 if (!ksz886x_cable_test_fault_length_valid(val, LAN8814_CABLE_DIAG_STAT_MASK)) 2144 return 0; 2145 2146 fault_length = ksz886x_cable_test_fault_length(phydev, val, 2147 LAN8814_CABLE_DIAG_VCT_DATA_MASK); 2148 2149 return ethnl_cable_test_fault_length(phydev, ethtool_pair[pair], fault_length); 2150 } 2151 2152 static int ksz886x_cable_test_one_pair(struct phy_device *phydev, int pair) 2153 { 2154 static const int ethtool_pair[] = { 2155 ETHTOOL_A_CABLE_PAIR_A, 2156 ETHTOOL_A_CABLE_PAIR_B, 2157 }; 2158 int ret, val, mdix; 2159 u32 fault_length; 2160 2161 /* There is no way to choice the pair, like we do one ksz9031. 2162 * We can workaround this limitation by using the MDI-X functionality. 2163 */ 2164 if (pair == 0) 2165 mdix = ETH_TP_MDI; 2166 else 2167 mdix = ETH_TP_MDI_X; 2168 2169 switch (phydev->phy_id & MICREL_PHY_ID_MASK) { 2170 case PHY_ID_KSZ8081: 2171 ret = ksz8081_config_mdix(phydev, mdix); 2172 break; 2173 case PHY_ID_KSZ886X: 2174 ret = ksz886x_config_mdix(phydev, mdix); 2175 break; 2176 default: 2177 ret = -ENODEV; 2178 } 2179 2180 if (ret) 2181 return ret; 2182 2183 /* Now we are ready to fire. This command will send a 100ns pulse 2184 * to the pair. 2185 */ 2186 ret = phy_write(phydev, KSZ8081_LMD, KSZ8081_LMD_ENABLE_TEST); 2187 if (ret) 2188 return ret; 2189 2190 ret = ksz886x_cable_test_wait_for_completion(phydev); 2191 if (ret) 2192 return ret; 2193 2194 val = phy_read(phydev, KSZ8081_LMD); 2195 if (val < 0) 2196 return val; 2197 2198 if (ksz886x_cable_test_failed(val, KSZ8081_LMD_STAT_MASK)) 2199 return -EAGAIN; 2200 2201 ret = ethnl_cable_test_result(phydev, ethtool_pair[pair], 2202 ksz886x_cable_test_result_trans(val, KSZ8081_LMD_STAT_MASK)); 2203 if (ret) 2204 return ret; 2205 2206 if (!ksz886x_cable_test_fault_length_valid(val, KSZ8081_LMD_STAT_MASK)) 2207 return 0; 2208 2209 fault_length = ksz886x_cable_test_fault_length(phydev, val, KSZ8081_LMD_DELTA_TIME_MASK); 2210 2211 return ethnl_cable_test_fault_length(phydev, ethtool_pair[pair], fault_length); 2212 } 2213 2214 static int ksz886x_cable_test_get_status(struct phy_device *phydev, 2215 bool *finished) 2216 { 2217 const struct kszphy_type *type = phydev->drv->driver_data; 2218 unsigned long pair_mask = type->pair_mask; 2219 int retries = 20; 2220 int ret = 0; 2221 int pair; 2222 2223 *finished = false; 2224 2225 /* Try harder if link partner is active */ 2226 while (pair_mask && retries--) { 2227 for_each_set_bit(pair, &pair_mask, 4) { 2228 if (type->cable_diag_reg == LAN8814_CABLE_DIAG) 2229 ret = lan8814_cable_test_one_pair(phydev, pair); 2230 else 2231 ret = ksz886x_cable_test_one_pair(phydev, pair); 2232 if (ret == -EAGAIN) 2233 continue; 2234 if (ret < 0) 2235 return ret; 2236 clear_bit(pair, &pair_mask); 2237 } 2238 /* If link partner is in autonegotiation mode it will send 2ms 2239 * of FLPs with at least 6ms of silence. 2240 * Add 2ms sleep to have better chances to hit this silence. 2241 */ 2242 if (pair_mask) 2243 msleep(2); 2244 } 2245 2246 *finished = true; 2247 2248 return ret; 2249 } 2250 2251 #define LAN_EXT_PAGE_ACCESS_CONTROL 0x16 2252 #define LAN_EXT_PAGE_ACCESS_ADDRESS_DATA 0x17 2253 #define LAN_EXT_PAGE_ACCESS_CTRL_EP_FUNC 0x4000 2254 2255 #define LAN8814_QSGMII_SOFT_RESET 0x43 2256 #define LAN8814_QSGMII_SOFT_RESET_BIT BIT(0) 2257 #define LAN8814_QSGMII_PCS1G_ANEG_CONFIG 0x13 2258 #define LAN8814_QSGMII_PCS1G_ANEG_CONFIG_ANEG_ENA BIT(3) 2259 #define LAN8814_ALIGN_SWAP 0x4a 2260 #define LAN8814_ALIGN_TX_A_B_SWAP 0x1 2261 #define LAN8814_ALIGN_TX_A_B_SWAP_MASK GENMASK(2, 0) 2262 2263 #define LAN8804_ALIGN_SWAP 0x4a 2264 #define LAN8804_ALIGN_TX_A_B_SWAP 0x1 2265 #define LAN8804_ALIGN_TX_A_B_SWAP_MASK GENMASK(2, 0) 2266 #define LAN8814_CLOCK_MANAGEMENT 0xd 2267 #define LAN8814_LINK_QUALITY 0x8e 2268 2269 static int lanphy_read_page_reg(struct phy_device *phydev, int page, u32 addr) 2270 { 2271 int data; 2272 2273 phy_lock_mdio_bus(phydev); 2274 __phy_write(phydev, LAN_EXT_PAGE_ACCESS_CONTROL, page); 2275 __phy_write(phydev, LAN_EXT_PAGE_ACCESS_ADDRESS_DATA, addr); 2276 __phy_write(phydev, LAN_EXT_PAGE_ACCESS_CONTROL, 2277 (page | LAN_EXT_PAGE_ACCESS_CTRL_EP_FUNC)); 2278 data = __phy_read(phydev, LAN_EXT_PAGE_ACCESS_ADDRESS_DATA); 2279 phy_unlock_mdio_bus(phydev); 2280 2281 return data; 2282 } 2283 2284 static int lanphy_write_page_reg(struct phy_device *phydev, int page, u16 addr, 2285 u16 val) 2286 { 2287 phy_lock_mdio_bus(phydev); 2288 __phy_write(phydev, LAN_EXT_PAGE_ACCESS_CONTROL, page); 2289 __phy_write(phydev, LAN_EXT_PAGE_ACCESS_ADDRESS_DATA, addr); 2290 __phy_write(phydev, LAN_EXT_PAGE_ACCESS_CONTROL, 2291 page | LAN_EXT_PAGE_ACCESS_CTRL_EP_FUNC); 2292 2293 val = __phy_write(phydev, LAN_EXT_PAGE_ACCESS_ADDRESS_DATA, val); 2294 if (val != 0) 2295 phydev_err(phydev, "Error: phy_write has returned error %d\n", 2296 val); 2297 phy_unlock_mdio_bus(phydev); 2298 return val; 2299 } 2300 2301 static int lan8814_config_ts_intr(struct phy_device *phydev, bool enable) 2302 { 2303 u16 val = 0; 2304 2305 if (enable) 2306 val = PTP_TSU_INT_EN_PTP_TX_TS_EN_ | 2307 PTP_TSU_INT_EN_PTP_TX_TS_OVRFL_EN_ | 2308 PTP_TSU_INT_EN_PTP_RX_TS_EN_ | 2309 PTP_TSU_INT_EN_PTP_RX_TS_OVRFL_EN_; 2310 2311 return lanphy_write_page_reg(phydev, 5, PTP_TSU_INT_EN, val); 2312 } 2313 2314 static void lan8814_ptp_rx_ts_get(struct phy_device *phydev, 2315 u32 *seconds, u32 *nano_seconds, u16 *seq_id) 2316 { 2317 *seconds = lanphy_read_page_reg(phydev, 5, PTP_RX_INGRESS_SEC_HI); 2318 *seconds = (*seconds << 16) | 2319 lanphy_read_page_reg(phydev, 5, PTP_RX_INGRESS_SEC_LO); 2320 2321 *nano_seconds = lanphy_read_page_reg(phydev, 5, PTP_RX_INGRESS_NS_HI); 2322 *nano_seconds = ((*nano_seconds & 0x3fff) << 16) | 2323 lanphy_read_page_reg(phydev, 5, PTP_RX_INGRESS_NS_LO); 2324 2325 *seq_id = lanphy_read_page_reg(phydev, 5, PTP_RX_MSG_HEADER2); 2326 } 2327 2328 static void lan8814_ptp_tx_ts_get(struct phy_device *phydev, 2329 u32 *seconds, u32 *nano_seconds, u16 *seq_id) 2330 { 2331 *seconds = lanphy_read_page_reg(phydev, 5, PTP_TX_EGRESS_SEC_HI); 2332 *seconds = *seconds << 16 | 2333 lanphy_read_page_reg(phydev, 5, PTP_TX_EGRESS_SEC_LO); 2334 2335 *nano_seconds = lanphy_read_page_reg(phydev, 5, PTP_TX_EGRESS_NS_HI); 2336 *nano_seconds = ((*nano_seconds & 0x3fff) << 16) | 2337 lanphy_read_page_reg(phydev, 5, PTP_TX_EGRESS_NS_LO); 2338 2339 *seq_id = lanphy_read_page_reg(phydev, 5, PTP_TX_MSG_HEADER2); 2340 } 2341 2342 static int lan8814_ts_info(struct mii_timestamper *mii_ts, struct ethtool_ts_info *info) 2343 { 2344 struct kszphy_ptp_priv *ptp_priv = container_of(mii_ts, struct kszphy_ptp_priv, mii_ts); 2345 struct phy_device *phydev = ptp_priv->phydev; 2346 struct lan8814_shared_priv *shared = phydev->shared->priv; 2347 2348 info->so_timestamping = SOF_TIMESTAMPING_TX_HARDWARE | 2349 SOF_TIMESTAMPING_RX_HARDWARE | 2350 SOF_TIMESTAMPING_RAW_HARDWARE; 2351 2352 info->phc_index = ptp_clock_index(shared->ptp_clock); 2353 2354 info->tx_types = 2355 (1 << HWTSTAMP_TX_OFF) | 2356 (1 << HWTSTAMP_TX_ON) | 2357 (1 << HWTSTAMP_TX_ONESTEP_SYNC); 2358 2359 info->rx_filters = 2360 (1 << HWTSTAMP_FILTER_NONE) | 2361 (1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT) | 2362 (1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT) | 2363 (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) | 2364 (1 << HWTSTAMP_FILTER_PTP_V2_EVENT); 2365 2366 return 0; 2367 } 2368 2369 static void lan8814_flush_fifo(struct phy_device *phydev, bool egress) 2370 { 2371 int i; 2372 2373 for (i = 0; i < FIFO_SIZE; ++i) 2374 lanphy_read_page_reg(phydev, 5, 2375 egress ? PTP_TX_MSG_HEADER2 : PTP_RX_MSG_HEADER2); 2376 2377 /* Read to clear overflow status bit */ 2378 lanphy_read_page_reg(phydev, 5, PTP_TSU_INT_STS); 2379 } 2380 2381 static int lan8814_hwtstamp(struct mii_timestamper *mii_ts, struct ifreq *ifr) 2382 { 2383 struct kszphy_ptp_priv *ptp_priv = 2384 container_of(mii_ts, struct kszphy_ptp_priv, mii_ts); 2385 struct phy_device *phydev = ptp_priv->phydev; 2386 struct lan8814_shared_priv *shared = phydev->shared->priv; 2387 struct lan8814_ptp_rx_ts *rx_ts, *tmp; 2388 struct hwtstamp_config config; 2389 int txcfg = 0, rxcfg = 0; 2390 int pkt_ts_enable; 2391 int tx_mod; 2392 2393 if (copy_from_user(&config, ifr->ifr_data, sizeof(config))) 2394 return -EFAULT; 2395 2396 ptp_priv->hwts_tx_type = config.tx_type; 2397 ptp_priv->rx_filter = config.rx_filter; 2398 2399 switch (config.rx_filter) { 2400 case HWTSTAMP_FILTER_NONE: 2401 ptp_priv->layer = 0; 2402 ptp_priv->version = 0; 2403 break; 2404 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT: 2405 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC: 2406 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ: 2407 ptp_priv->layer = PTP_CLASS_L4; 2408 ptp_priv->version = PTP_CLASS_V2; 2409 break; 2410 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT: 2411 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC: 2412 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ: 2413 ptp_priv->layer = PTP_CLASS_L2; 2414 ptp_priv->version = PTP_CLASS_V2; 2415 break; 2416 case HWTSTAMP_FILTER_PTP_V2_EVENT: 2417 case HWTSTAMP_FILTER_PTP_V2_SYNC: 2418 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ: 2419 ptp_priv->layer = PTP_CLASS_L4 | PTP_CLASS_L2; 2420 ptp_priv->version = PTP_CLASS_V2; 2421 break; 2422 default: 2423 return -ERANGE; 2424 } 2425 2426 if (ptp_priv->layer & PTP_CLASS_L2) { 2427 rxcfg = PTP_RX_PARSE_CONFIG_LAYER2_EN_; 2428 txcfg = PTP_TX_PARSE_CONFIG_LAYER2_EN_; 2429 } else if (ptp_priv->layer & PTP_CLASS_L4) { 2430 rxcfg |= PTP_RX_PARSE_CONFIG_IPV4_EN_ | PTP_RX_PARSE_CONFIG_IPV6_EN_; 2431 txcfg |= PTP_TX_PARSE_CONFIG_IPV4_EN_ | PTP_TX_PARSE_CONFIG_IPV6_EN_; 2432 } 2433 lanphy_write_page_reg(ptp_priv->phydev, 5, PTP_RX_PARSE_CONFIG, rxcfg); 2434 lanphy_write_page_reg(ptp_priv->phydev, 5, PTP_TX_PARSE_CONFIG, txcfg); 2435 2436 pkt_ts_enable = PTP_TIMESTAMP_EN_SYNC_ | PTP_TIMESTAMP_EN_DREQ_ | 2437 PTP_TIMESTAMP_EN_PDREQ_ | PTP_TIMESTAMP_EN_PDRES_; 2438 lanphy_write_page_reg(ptp_priv->phydev, 5, PTP_RX_TIMESTAMP_EN, pkt_ts_enable); 2439 lanphy_write_page_reg(ptp_priv->phydev, 5, PTP_TX_TIMESTAMP_EN, pkt_ts_enable); 2440 2441 tx_mod = lanphy_read_page_reg(ptp_priv->phydev, 5, PTP_TX_MOD); 2442 if (ptp_priv->hwts_tx_type == HWTSTAMP_TX_ONESTEP_SYNC) { 2443 lanphy_write_page_reg(ptp_priv->phydev, 5, PTP_TX_MOD, 2444 tx_mod | PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_); 2445 } else if (ptp_priv->hwts_tx_type == HWTSTAMP_TX_ON) { 2446 lanphy_write_page_reg(ptp_priv->phydev, 5, PTP_TX_MOD, 2447 tx_mod & ~PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_); 2448 } 2449 2450 if (config.rx_filter != HWTSTAMP_FILTER_NONE) 2451 lan8814_config_ts_intr(ptp_priv->phydev, true); 2452 else 2453 lan8814_config_ts_intr(ptp_priv->phydev, false); 2454 2455 mutex_lock(&shared->shared_lock); 2456 if (config.rx_filter != HWTSTAMP_FILTER_NONE) 2457 shared->ref++; 2458 else 2459 shared->ref--; 2460 2461 if (shared->ref) 2462 lanphy_write_page_reg(ptp_priv->phydev, 4, PTP_CMD_CTL, 2463 PTP_CMD_CTL_PTP_ENABLE_); 2464 else 2465 lanphy_write_page_reg(ptp_priv->phydev, 4, PTP_CMD_CTL, 2466 PTP_CMD_CTL_PTP_DISABLE_); 2467 mutex_unlock(&shared->shared_lock); 2468 2469 /* In case of multiple starts and stops, these needs to be cleared */ 2470 list_for_each_entry_safe(rx_ts, tmp, &ptp_priv->rx_ts_list, list) { 2471 list_del(&rx_ts->list); 2472 kfree(rx_ts); 2473 } 2474 skb_queue_purge(&ptp_priv->rx_queue); 2475 skb_queue_purge(&ptp_priv->tx_queue); 2476 2477 lan8814_flush_fifo(ptp_priv->phydev, false); 2478 lan8814_flush_fifo(ptp_priv->phydev, true); 2479 2480 return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ? -EFAULT : 0; 2481 } 2482 2483 static void lan8814_txtstamp(struct mii_timestamper *mii_ts, 2484 struct sk_buff *skb, int type) 2485 { 2486 struct kszphy_ptp_priv *ptp_priv = container_of(mii_ts, struct kszphy_ptp_priv, mii_ts); 2487 2488 switch (ptp_priv->hwts_tx_type) { 2489 case HWTSTAMP_TX_ONESTEP_SYNC: 2490 if (ptp_msg_is_sync(skb, type)) { 2491 kfree_skb(skb); 2492 return; 2493 } 2494 fallthrough; 2495 case HWTSTAMP_TX_ON: 2496 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; 2497 skb_queue_tail(&ptp_priv->tx_queue, skb); 2498 break; 2499 case HWTSTAMP_TX_OFF: 2500 default: 2501 kfree_skb(skb); 2502 break; 2503 } 2504 } 2505 2506 static bool lan8814_get_sig_rx(struct sk_buff *skb, u16 *sig) 2507 { 2508 struct ptp_header *ptp_header; 2509 u32 type; 2510 2511 skb_push(skb, ETH_HLEN); 2512 type = ptp_classify_raw(skb); 2513 ptp_header = ptp_parse_header(skb, type); 2514 skb_pull_inline(skb, ETH_HLEN); 2515 2516 if (!ptp_header) 2517 return false; 2518 2519 *sig = (__force u16)(ntohs(ptp_header->sequence_id)); 2520 return true; 2521 } 2522 2523 static bool lan8814_match_rx_skb(struct kszphy_ptp_priv *ptp_priv, 2524 struct sk_buff *skb) 2525 { 2526 struct skb_shared_hwtstamps *shhwtstamps; 2527 struct lan8814_ptp_rx_ts *rx_ts, *tmp; 2528 unsigned long flags; 2529 bool ret = false; 2530 u16 skb_sig; 2531 2532 if (!lan8814_get_sig_rx(skb, &skb_sig)) 2533 return ret; 2534 2535 /* Iterate over all RX timestamps and match it with the received skbs */ 2536 spin_lock_irqsave(&ptp_priv->rx_ts_lock, flags); 2537 list_for_each_entry_safe(rx_ts, tmp, &ptp_priv->rx_ts_list, list) { 2538 /* Check if we found the signature we were looking for. */ 2539 if (memcmp(&skb_sig, &rx_ts->seq_id, sizeof(rx_ts->seq_id))) 2540 continue; 2541 2542 shhwtstamps = skb_hwtstamps(skb); 2543 memset(shhwtstamps, 0, sizeof(*shhwtstamps)); 2544 shhwtstamps->hwtstamp = ktime_set(rx_ts->seconds, 2545 rx_ts->nsec); 2546 list_del(&rx_ts->list); 2547 kfree(rx_ts); 2548 2549 ret = true; 2550 break; 2551 } 2552 spin_unlock_irqrestore(&ptp_priv->rx_ts_lock, flags); 2553 2554 if (ret) 2555 netif_rx(skb); 2556 return ret; 2557 } 2558 2559 static bool lan8814_rxtstamp(struct mii_timestamper *mii_ts, struct sk_buff *skb, int type) 2560 { 2561 struct kszphy_ptp_priv *ptp_priv = 2562 container_of(mii_ts, struct kszphy_ptp_priv, mii_ts); 2563 2564 if (ptp_priv->rx_filter == HWTSTAMP_FILTER_NONE || 2565 type == PTP_CLASS_NONE) 2566 return false; 2567 2568 if ((type & ptp_priv->version) == 0 || (type & ptp_priv->layer) == 0) 2569 return false; 2570 2571 /* If we failed to match then add it to the queue for when the timestamp 2572 * will come 2573 */ 2574 if (!lan8814_match_rx_skb(ptp_priv, skb)) 2575 skb_queue_tail(&ptp_priv->rx_queue, skb); 2576 2577 return true; 2578 } 2579 2580 static void lan8814_ptp_clock_set(struct phy_device *phydev, 2581 u32 seconds, u32 nano_seconds) 2582 { 2583 u32 sec_low, sec_high, nsec_low, nsec_high; 2584 2585 sec_low = seconds & 0xffff; 2586 sec_high = (seconds >> 16) & 0xffff; 2587 nsec_low = nano_seconds & 0xffff; 2588 nsec_high = (nano_seconds >> 16) & 0x3fff; 2589 2590 lanphy_write_page_reg(phydev, 4, PTP_CLOCK_SET_SEC_LO, sec_low); 2591 lanphy_write_page_reg(phydev, 4, PTP_CLOCK_SET_SEC_MID, sec_high); 2592 lanphy_write_page_reg(phydev, 4, PTP_CLOCK_SET_NS_LO, nsec_low); 2593 lanphy_write_page_reg(phydev, 4, PTP_CLOCK_SET_NS_HI, nsec_high); 2594 2595 lanphy_write_page_reg(phydev, 4, PTP_CMD_CTL, PTP_CMD_CTL_PTP_CLOCK_LOAD_); 2596 } 2597 2598 static void lan8814_ptp_clock_get(struct phy_device *phydev, 2599 u32 *seconds, u32 *nano_seconds) 2600 { 2601 lanphy_write_page_reg(phydev, 4, PTP_CMD_CTL, PTP_CMD_CTL_PTP_CLOCK_READ_); 2602 2603 *seconds = lanphy_read_page_reg(phydev, 4, PTP_CLOCK_READ_SEC_MID); 2604 *seconds = (*seconds << 16) | 2605 lanphy_read_page_reg(phydev, 4, PTP_CLOCK_READ_SEC_LO); 2606 2607 *nano_seconds = lanphy_read_page_reg(phydev, 4, PTP_CLOCK_READ_NS_HI); 2608 *nano_seconds = ((*nano_seconds & 0x3fff) << 16) | 2609 lanphy_read_page_reg(phydev, 4, PTP_CLOCK_READ_NS_LO); 2610 } 2611 2612 static int lan8814_ptpci_gettime64(struct ptp_clock_info *ptpci, 2613 struct timespec64 *ts) 2614 { 2615 struct lan8814_shared_priv *shared = container_of(ptpci, struct lan8814_shared_priv, 2616 ptp_clock_info); 2617 struct phy_device *phydev = shared->phydev; 2618 u32 nano_seconds; 2619 u32 seconds; 2620 2621 mutex_lock(&shared->shared_lock); 2622 lan8814_ptp_clock_get(phydev, &seconds, &nano_seconds); 2623 mutex_unlock(&shared->shared_lock); 2624 ts->tv_sec = seconds; 2625 ts->tv_nsec = nano_seconds; 2626 2627 return 0; 2628 } 2629 2630 static int lan8814_ptpci_settime64(struct ptp_clock_info *ptpci, 2631 const struct timespec64 *ts) 2632 { 2633 struct lan8814_shared_priv *shared = container_of(ptpci, struct lan8814_shared_priv, 2634 ptp_clock_info); 2635 struct phy_device *phydev = shared->phydev; 2636 2637 mutex_lock(&shared->shared_lock); 2638 lan8814_ptp_clock_set(phydev, ts->tv_sec, ts->tv_nsec); 2639 mutex_unlock(&shared->shared_lock); 2640 2641 return 0; 2642 } 2643 2644 static void lan8814_ptp_clock_step(struct phy_device *phydev, 2645 s64 time_step_ns) 2646 { 2647 u32 nano_seconds_step; 2648 u64 abs_time_step_ns; 2649 u32 unsigned_seconds; 2650 u32 nano_seconds; 2651 u32 remainder; 2652 s32 seconds; 2653 2654 if (time_step_ns > 15000000000LL) { 2655 /* convert to clock set */ 2656 lan8814_ptp_clock_get(phydev, &unsigned_seconds, &nano_seconds); 2657 unsigned_seconds += div_u64_rem(time_step_ns, 1000000000LL, 2658 &remainder); 2659 nano_seconds += remainder; 2660 if (nano_seconds >= 1000000000) { 2661 unsigned_seconds++; 2662 nano_seconds -= 1000000000; 2663 } 2664 lan8814_ptp_clock_set(phydev, unsigned_seconds, nano_seconds); 2665 return; 2666 } else if (time_step_ns < -15000000000LL) { 2667 /* convert to clock set */ 2668 time_step_ns = -time_step_ns; 2669 2670 lan8814_ptp_clock_get(phydev, &unsigned_seconds, &nano_seconds); 2671 unsigned_seconds -= div_u64_rem(time_step_ns, 1000000000LL, 2672 &remainder); 2673 nano_seconds_step = remainder; 2674 if (nano_seconds < nano_seconds_step) { 2675 unsigned_seconds--; 2676 nano_seconds += 1000000000; 2677 } 2678 nano_seconds -= nano_seconds_step; 2679 lan8814_ptp_clock_set(phydev, unsigned_seconds, 2680 nano_seconds); 2681 return; 2682 } 2683 2684 /* do clock step */ 2685 if (time_step_ns >= 0) { 2686 abs_time_step_ns = (u64)time_step_ns; 2687 seconds = (s32)div_u64_rem(abs_time_step_ns, 1000000000, 2688 &remainder); 2689 nano_seconds = remainder; 2690 } else { 2691 abs_time_step_ns = (u64)(-time_step_ns); 2692 seconds = -((s32)div_u64_rem(abs_time_step_ns, 1000000000, 2693 &remainder)); 2694 nano_seconds = remainder; 2695 if (nano_seconds > 0) { 2696 /* subtracting nano seconds is not allowed 2697 * convert to subtracting from seconds, 2698 * and adding to nanoseconds 2699 */ 2700 seconds--; 2701 nano_seconds = (1000000000 - nano_seconds); 2702 } 2703 } 2704 2705 if (nano_seconds > 0) { 2706 /* add 8 ns to cover the likely normal increment */ 2707 nano_seconds += 8; 2708 } 2709 2710 if (nano_seconds >= 1000000000) { 2711 /* carry into seconds */ 2712 seconds++; 2713 nano_seconds -= 1000000000; 2714 } 2715 2716 while (seconds) { 2717 if (seconds > 0) { 2718 u32 adjustment_value = (u32)seconds; 2719 u16 adjustment_value_lo, adjustment_value_hi; 2720 2721 if (adjustment_value > 0xF) 2722 adjustment_value = 0xF; 2723 2724 adjustment_value_lo = adjustment_value & 0xffff; 2725 adjustment_value_hi = (adjustment_value >> 16) & 0x3fff; 2726 2727 lanphy_write_page_reg(phydev, 4, PTP_LTC_STEP_ADJ_LO, 2728 adjustment_value_lo); 2729 lanphy_write_page_reg(phydev, 4, PTP_LTC_STEP_ADJ_HI, 2730 PTP_LTC_STEP_ADJ_DIR_ | 2731 adjustment_value_hi); 2732 seconds -= ((s32)adjustment_value); 2733 } else { 2734 u32 adjustment_value = (u32)(-seconds); 2735 u16 adjustment_value_lo, adjustment_value_hi; 2736 2737 if (adjustment_value > 0xF) 2738 adjustment_value = 0xF; 2739 2740 adjustment_value_lo = adjustment_value & 0xffff; 2741 adjustment_value_hi = (adjustment_value >> 16) & 0x3fff; 2742 2743 lanphy_write_page_reg(phydev, 4, PTP_LTC_STEP_ADJ_LO, 2744 adjustment_value_lo); 2745 lanphy_write_page_reg(phydev, 4, PTP_LTC_STEP_ADJ_HI, 2746 adjustment_value_hi); 2747 seconds += ((s32)adjustment_value); 2748 } 2749 lanphy_write_page_reg(phydev, 4, PTP_CMD_CTL, 2750 PTP_CMD_CTL_PTP_LTC_STEP_SEC_); 2751 } 2752 if (nano_seconds) { 2753 u16 nano_seconds_lo; 2754 u16 nano_seconds_hi; 2755 2756 nano_seconds_lo = nano_seconds & 0xffff; 2757 nano_seconds_hi = (nano_seconds >> 16) & 0x3fff; 2758 2759 lanphy_write_page_reg(phydev, 4, PTP_LTC_STEP_ADJ_LO, 2760 nano_seconds_lo); 2761 lanphy_write_page_reg(phydev, 4, PTP_LTC_STEP_ADJ_HI, 2762 PTP_LTC_STEP_ADJ_DIR_ | 2763 nano_seconds_hi); 2764 lanphy_write_page_reg(phydev, 4, PTP_CMD_CTL, 2765 PTP_CMD_CTL_PTP_LTC_STEP_NSEC_); 2766 } 2767 } 2768 2769 static int lan8814_ptpci_adjtime(struct ptp_clock_info *ptpci, s64 delta) 2770 { 2771 struct lan8814_shared_priv *shared = container_of(ptpci, struct lan8814_shared_priv, 2772 ptp_clock_info); 2773 struct phy_device *phydev = shared->phydev; 2774 2775 mutex_lock(&shared->shared_lock); 2776 lan8814_ptp_clock_step(phydev, delta); 2777 mutex_unlock(&shared->shared_lock); 2778 2779 return 0; 2780 } 2781 2782 static int lan8814_ptpci_adjfine(struct ptp_clock_info *ptpci, long scaled_ppm) 2783 { 2784 struct lan8814_shared_priv *shared = container_of(ptpci, struct lan8814_shared_priv, 2785 ptp_clock_info); 2786 struct phy_device *phydev = shared->phydev; 2787 u16 kszphy_rate_adj_lo, kszphy_rate_adj_hi; 2788 bool positive = true; 2789 u32 kszphy_rate_adj; 2790 2791 if (scaled_ppm < 0) { 2792 scaled_ppm = -scaled_ppm; 2793 positive = false; 2794 } 2795 2796 kszphy_rate_adj = LAN8814_1PPM_FORMAT * (scaled_ppm >> 16); 2797 kszphy_rate_adj += (LAN8814_1PPM_FORMAT * (0xffff & scaled_ppm)) >> 16; 2798 2799 kszphy_rate_adj_lo = kszphy_rate_adj & 0xffff; 2800 kszphy_rate_adj_hi = (kszphy_rate_adj >> 16) & 0x3fff; 2801 2802 if (positive) 2803 kszphy_rate_adj_hi |= PTP_CLOCK_RATE_ADJ_DIR_; 2804 2805 mutex_lock(&shared->shared_lock); 2806 lanphy_write_page_reg(phydev, 4, PTP_CLOCK_RATE_ADJ_HI, kszphy_rate_adj_hi); 2807 lanphy_write_page_reg(phydev, 4, PTP_CLOCK_RATE_ADJ_LO, kszphy_rate_adj_lo); 2808 mutex_unlock(&shared->shared_lock); 2809 2810 return 0; 2811 } 2812 2813 static bool lan8814_get_sig_tx(struct sk_buff *skb, u16 *sig) 2814 { 2815 struct ptp_header *ptp_header; 2816 u32 type; 2817 2818 type = ptp_classify_raw(skb); 2819 ptp_header = ptp_parse_header(skb, type); 2820 2821 if (!ptp_header) 2822 return false; 2823 2824 *sig = (__force u16)(ntohs(ptp_header->sequence_id)); 2825 return true; 2826 } 2827 2828 static void lan8814_match_tx_skb(struct kszphy_ptp_priv *ptp_priv, 2829 u32 seconds, u32 nsec, u16 seq_id) 2830 { 2831 struct skb_shared_hwtstamps shhwtstamps; 2832 struct sk_buff *skb, *skb_tmp; 2833 unsigned long flags; 2834 bool ret = false; 2835 u16 skb_sig; 2836 2837 spin_lock_irqsave(&ptp_priv->tx_queue.lock, flags); 2838 skb_queue_walk_safe(&ptp_priv->tx_queue, skb, skb_tmp) { 2839 if (!lan8814_get_sig_tx(skb, &skb_sig)) 2840 continue; 2841 2842 if (memcmp(&skb_sig, &seq_id, sizeof(seq_id))) 2843 continue; 2844 2845 __skb_unlink(skb, &ptp_priv->tx_queue); 2846 ret = true; 2847 break; 2848 } 2849 spin_unlock_irqrestore(&ptp_priv->tx_queue.lock, flags); 2850 2851 if (ret) { 2852 memset(&shhwtstamps, 0, sizeof(shhwtstamps)); 2853 shhwtstamps.hwtstamp = ktime_set(seconds, nsec); 2854 skb_complete_tx_timestamp(skb, &shhwtstamps); 2855 } 2856 } 2857 2858 static void lan8814_dequeue_tx_skb(struct kszphy_ptp_priv *ptp_priv) 2859 { 2860 struct phy_device *phydev = ptp_priv->phydev; 2861 u32 seconds, nsec; 2862 u16 seq_id; 2863 2864 lan8814_ptp_tx_ts_get(phydev, &seconds, &nsec, &seq_id); 2865 lan8814_match_tx_skb(ptp_priv, seconds, nsec, seq_id); 2866 } 2867 2868 static void lan8814_get_tx_ts(struct kszphy_ptp_priv *ptp_priv) 2869 { 2870 struct phy_device *phydev = ptp_priv->phydev; 2871 u32 reg; 2872 2873 do { 2874 lan8814_dequeue_tx_skb(ptp_priv); 2875 2876 /* If other timestamps are available in the FIFO, 2877 * process them. 2878 */ 2879 reg = lanphy_read_page_reg(phydev, 5, PTP_CAP_INFO); 2880 } while (PTP_CAP_INFO_TX_TS_CNT_GET_(reg) > 0); 2881 } 2882 2883 static bool lan8814_match_skb(struct kszphy_ptp_priv *ptp_priv, 2884 struct lan8814_ptp_rx_ts *rx_ts) 2885 { 2886 struct skb_shared_hwtstamps *shhwtstamps; 2887 struct sk_buff *skb, *skb_tmp; 2888 unsigned long flags; 2889 bool ret = false; 2890 u16 skb_sig; 2891 2892 spin_lock_irqsave(&ptp_priv->rx_queue.lock, flags); 2893 skb_queue_walk_safe(&ptp_priv->rx_queue, skb, skb_tmp) { 2894 if (!lan8814_get_sig_rx(skb, &skb_sig)) 2895 continue; 2896 2897 if (memcmp(&skb_sig, &rx_ts->seq_id, sizeof(rx_ts->seq_id))) 2898 continue; 2899 2900 __skb_unlink(skb, &ptp_priv->rx_queue); 2901 2902 ret = true; 2903 break; 2904 } 2905 spin_unlock_irqrestore(&ptp_priv->rx_queue.lock, flags); 2906 2907 if (ret) { 2908 shhwtstamps = skb_hwtstamps(skb); 2909 memset(shhwtstamps, 0, sizeof(*shhwtstamps)); 2910 shhwtstamps->hwtstamp = ktime_set(rx_ts->seconds, rx_ts->nsec); 2911 netif_rx(skb); 2912 } 2913 2914 return ret; 2915 } 2916 2917 static void lan8814_match_rx_ts(struct kszphy_ptp_priv *ptp_priv, 2918 struct lan8814_ptp_rx_ts *rx_ts) 2919 { 2920 unsigned long flags; 2921 2922 /* If we failed to match the skb add it to the queue for when 2923 * the frame will come 2924 */ 2925 if (!lan8814_match_skb(ptp_priv, rx_ts)) { 2926 spin_lock_irqsave(&ptp_priv->rx_ts_lock, flags); 2927 list_add(&rx_ts->list, &ptp_priv->rx_ts_list); 2928 spin_unlock_irqrestore(&ptp_priv->rx_ts_lock, flags); 2929 } else { 2930 kfree(rx_ts); 2931 } 2932 } 2933 2934 static void lan8814_get_rx_ts(struct kszphy_ptp_priv *ptp_priv) 2935 { 2936 struct phy_device *phydev = ptp_priv->phydev; 2937 struct lan8814_ptp_rx_ts *rx_ts; 2938 u32 reg; 2939 2940 do { 2941 rx_ts = kzalloc(sizeof(*rx_ts), GFP_KERNEL); 2942 if (!rx_ts) 2943 return; 2944 2945 lan8814_ptp_rx_ts_get(phydev, &rx_ts->seconds, &rx_ts->nsec, 2946 &rx_ts->seq_id); 2947 lan8814_match_rx_ts(ptp_priv, rx_ts); 2948 2949 /* If other timestamps are available in the FIFO, 2950 * process them. 2951 */ 2952 reg = lanphy_read_page_reg(phydev, 5, PTP_CAP_INFO); 2953 } while (PTP_CAP_INFO_RX_TS_CNT_GET_(reg) > 0); 2954 } 2955 2956 static void lan8814_handle_ptp_interrupt(struct phy_device *phydev, u16 status) 2957 { 2958 struct kszphy_priv *priv = phydev->priv; 2959 struct kszphy_ptp_priv *ptp_priv = &priv->ptp_priv; 2960 2961 if (status & PTP_TSU_INT_STS_PTP_TX_TS_EN_) 2962 lan8814_get_tx_ts(ptp_priv); 2963 2964 if (status & PTP_TSU_INT_STS_PTP_RX_TS_EN_) 2965 lan8814_get_rx_ts(ptp_priv); 2966 2967 if (status & PTP_TSU_INT_STS_PTP_TX_TS_OVRFL_INT_) { 2968 lan8814_flush_fifo(phydev, true); 2969 skb_queue_purge(&ptp_priv->tx_queue); 2970 } 2971 2972 if (status & PTP_TSU_INT_STS_PTP_RX_TS_OVRFL_INT_) { 2973 lan8814_flush_fifo(phydev, false); 2974 skb_queue_purge(&ptp_priv->rx_queue); 2975 } 2976 } 2977 2978 static int lan8804_config_init(struct phy_device *phydev) 2979 { 2980 int val; 2981 2982 /* MDI-X setting for swap A,B transmit */ 2983 val = lanphy_read_page_reg(phydev, 2, LAN8804_ALIGN_SWAP); 2984 val &= ~LAN8804_ALIGN_TX_A_B_SWAP_MASK; 2985 val |= LAN8804_ALIGN_TX_A_B_SWAP; 2986 lanphy_write_page_reg(phydev, 2, LAN8804_ALIGN_SWAP, val); 2987 2988 /* Make sure that the PHY will not stop generating the clock when the 2989 * link partner goes down 2990 */ 2991 lanphy_write_page_reg(phydev, 31, LAN8814_CLOCK_MANAGEMENT, 0x27e); 2992 lanphy_read_page_reg(phydev, 1, LAN8814_LINK_QUALITY); 2993 2994 return 0; 2995 } 2996 2997 static irqreturn_t lan8804_handle_interrupt(struct phy_device *phydev) 2998 { 2999 int status; 3000 3001 status = phy_read(phydev, LAN8814_INTS); 3002 if (status < 0) { 3003 phy_error(phydev); 3004 return IRQ_NONE; 3005 } 3006 3007 if (status > 0) 3008 phy_trigger_machine(phydev); 3009 3010 return IRQ_HANDLED; 3011 } 3012 3013 #define LAN8804_OUTPUT_CONTROL 25 3014 #define LAN8804_OUTPUT_CONTROL_INTR_BUFFER BIT(14) 3015 #define LAN8804_CONTROL 31 3016 #define LAN8804_CONTROL_INTR_POLARITY BIT(14) 3017 3018 static int lan8804_config_intr(struct phy_device *phydev) 3019 { 3020 int err; 3021 3022 /* This is an internal PHY of lan966x and is not possible to change the 3023 * polarity on the GIC found in lan966x, therefore change the polarity 3024 * of the interrupt in the PHY from being active low instead of active 3025 * high. 3026 */ 3027 phy_write(phydev, LAN8804_CONTROL, LAN8804_CONTROL_INTR_POLARITY); 3028 3029 /* By default interrupt buffer is open-drain in which case the interrupt 3030 * can be active only low. Therefore change the interrupt buffer to be 3031 * push-pull to be able to change interrupt polarity 3032 */ 3033 phy_write(phydev, LAN8804_OUTPUT_CONTROL, 3034 LAN8804_OUTPUT_CONTROL_INTR_BUFFER); 3035 3036 if (phydev->interrupts == PHY_INTERRUPT_ENABLED) { 3037 err = phy_read(phydev, LAN8814_INTS); 3038 if (err < 0) 3039 return err; 3040 3041 err = phy_write(phydev, LAN8814_INTC, LAN8814_INT_LINK); 3042 if (err) 3043 return err; 3044 } else { 3045 err = phy_write(phydev, LAN8814_INTC, 0); 3046 if (err) 3047 return err; 3048 3049 err = phy_read(phydev, LAN8814_INTS); 3050 if (err < 0) 3051 return err; 3052 } 3053 3054 return 0; 3055 } 3056 3057 static irqreturn_t lan8814_handle_interrupt(struct phy_device *phydev) 3058 { 3059 int ret = IRQ_NONE; 3060 int irq_status; 3061 3062 irq_status = phy_read(phydev, LAN8814_INTS); 3063 if (irq_status < 0) { 3064 phy_error(phydev); 3065 return IRQ_NONE; 3066 } 3067 3068 if (irq_status & LAN8814_INT_LINK) { 3069 phy_trigger_machine(phydev); 3070 ret = IRQ_HANDLED; 3071 } 3072 3073 while (true) { 3074 irq_status = lanphy_read_page_reg(phydev, 5, PTP_TSU_INT_STS); 3075 if (!irq_status) 3076 break; 3077 3078 lan8814_handle_ptp_interrupt(phydev, irq_status); 3079 ret = IRQ_HANDLED; 3080 } 3081 3082 return ret; 3083 } 3084 3085 static int lan8814_ack_interrupt(struct phy_device *phydev) 3086 { 3087 /* bit[12..0] int status, which is a read and clear register. */ 3088 int rc; 3089 3090 rc = phy_read(phydev, LAN8814_INTS); 3091 3092 return (rc < 0) ? rc : 0; 3093 } 3094 3095 static int lan8814_config_intr(struct phy_device *phydev) 3096 { 3097 int err; 3098 3099 lanphy_write_page_reg(phydev, 4, LAN8814_INTR_CTRL_REG, 3100 LAN8814_INTR_CTRL_REG_POLARITY | 3101 LAN8814_INTR_CTRL_REG_INTR_ENABLE); 3102 3103 /* enable / disable interrupts */ 3104 if (phydev->interrupts == PHY_INTERRUPT_ENABLED) { 3105 err = lan8814_ack_interrupt(phydev); 3106 if (err) 3107 return err; 3108 3109 err = phy_write(phydev, LAN8814_INTC, LAN8814_INT_LINK); 3110 } else { 3111 err = phy_write(phydev, LAN8814_INTC, 0); 3112 if (err) 3113 return err; 3114 3115 err = lan8814_ack_interrupt(phydev); 3116 } 3117 3118 return err; 3119 } 3120 3121 static void lan8814_ptp_init(struct phy_device *phydev) 3122 { 3123 struct kszphy_priv *priv = phydev->priv; 3124 struct kszphy_ptp_priv *ptp_priv = &priv->ptp_priv; 3125 u32 temp; 3126 3127 if (!IS_ENABLED(CONFIG_PTP_1588_CLOCK) || 3128 !IS_ENABLED(CONFIG_NETWORK_PHY_TIMESTAMPING)) 3129 return; 3130 3131 lanphy_write_page_reg(phydev, 5, TSU_HARD_RESET, TSU_HARD_RESET_); 3132 3133 temp = lanphy_read_page_reg(phydev, 5, PTP_TX_MOD); 3134 temp |= PTP_TX_MOD_BAD_UDPV4_CHKSUM_FORCE_FCS_DIS_; 3135 lanphy_write_page_reg(phydev, 5, PTP_TX_MOD, temp); 3136 3137 temp = lanphy_read_page_reg(phydev, 5, PTP_RX_MOD); 3138 temp |= PTP_RX_MOD_BAD_UDPV4_CHKSUM_FORCE_FCS_DIS_; 3139 lanphy_write_page_reg(phydev, 5, PTP_RX_MOD, temp); 3140 3141 lanphy_write_page_reg(phydev, 5, PTP_RX_PARSE_CONFIG, 0); 3142 lanphy_write_page_reg(phydev, 5, PTP_TX_PARSE_CONFIG, 0); 3143 3144 /* Removing default registers configs related to L2 and IP */ 3145 lanphy_write_page_reg(phydev, 5, PTP_TX_PARSE_L2_ADDR_EN, 0); 3146 lanphy_write_page_reg(phydev, 5, PTP_RX_PARSE_L2_ADDR_EN, 0); 3147 lanphy_write_page_reg(phydev, 5, PTP_TX_PARSE_IP_ADDR_EN, 0); 3148 lanphy_write_page_reg(phydev, 5, PTP_RX_PARSE_IP_ADDR_EN, 0); 3149 3150 /* Disable checking for minorVersionPTP field */ 3151 lanphy_write_page_reg(phydev, 5, PTP_RX_VERSION, 3152 PTP_MAX_VERSION(0xff) | PTP_MIN_VERSION(0x0)); 3153 lanphy_write_page_reg(phydev, 5, PTP_TX_VERSION, 3154 PTP_MAX_VERSION(0xff) | PTP_MIN_VERSION(0x0)); 3155 3156 skb_queue_head_init(&ptp_priv->tx_queue); 3157 skb_queue_head_init(&ptp_priv->rx_queue); 3158 INIT_LIST_HEAD(&ptp_priv->rx_ts_list); 3159 spin_lock_init(&ptp_priv->rx_ts_lock); 3160 3161 ptp_priv->phydev = phydev; 3162 3163 ptp_priv->mii_ts.rxtstamp = lan8814_rxtstamp; 3164 ptp_priv->mii_ts.txtstamp = lan8814_txtstamp; 3165 ptp_priv->mii_ts.hwtstamp = lan8814_hwtstamp; 3166 ptp_priv->mii_ts.ts_info = lan8814_ts_info; 3167 3168 phydev->mii_ts = &ptp_priv->mii_ts; 3169 } 3170 3171 static int lan8814_ptp_probe_once(struct phy_device *phydev) 3172 { 3173 struct lan8814_shared_priv *shared = phydev->shared->priv; 3174 3175 /* Initialise shared lock for clock*/ 3176 mutex_init(&shared->shared_lock); 3177 3178 shared->ptp_clock_info.owner = THIS_MODULE; 3179 snprintf(shared->ptp_clock_info.name, 30, "%s", phydev->drv->name); 3180 shared->ptp_clock_info.max_adj = 31249999; 3181 shared->ptp_clock_info.n_alarm = 0; 3182 shared->ptp_clock_info.n_ext_ts = 0; 3183 shared->ptp_clock_info.n_pins = 0; 3184 shared->ptp_clock_info.pps = 0; 3185 shared->ptp_clock_info.pin_config = NULL; 3186 shared->ptp_clock_info.adjfine = lan8814_ptpci_adjfine; 3187 shared->ptp_clock_info.adjtime = lan8814_ptpci_adjtime; 3188 shared->ptp_clock_info.gettime64 = lan8814_ptpci_gettime64; 3189 shared->ptp_clock_info.settime64 = lan8814_ptpci_settime64; 3190 shared->ptp_clock_info.getcrosststamp = NULL; 3191 3192 shared->ptp_clock = ptp_clock_register(&shared->ptp_clock_info, 3193 &phydev->mdio.dev); 3194 if (IS_ERR(shared->ptp_clock)) { 3195 phydev_err(phydev, "ptp_clock_register failed %lu\n", 3196 PTR_ERR(shared->ptp_clock)); 3197 return -EINVAL; 3198 } 3199 3200 /* Check if PHC support is missing at the configuration level */ 3201 if (!shared->ptp_clock) 3202 return 0; 3203 3204 phydev_dbg(phydev, "successfully registered ptp clock\n"); 3205 3206 shared->phydev = phydev; 3207 3208 /* The EP.4 is shared between all the PHYs in the package and also it 3209 * can be accessed by any of the PHYs 3210 */ 3211 lanphy_write_page_reg(phydev, 4, LTC_HARD_RESET, LTC_HARD_RESET_); 3212 lanphy_write_page_reg(phydev, 4, PTP_OPERATING_MODE, 3213 PTP_OPERATING_MODE_STANDALONE_); 3214 3215 return 0; 3216 } 3217 3218 static void lan8814_setup_led(struct phy_device *phydev, int val) 3219 { 3220 int temp; 3221 3222 temp = lanphy_read_page_reg(phydev, 5, LAN8814_LED_CTRL_1); 3223 3224 if (val) 3225 temp |= LAN8814_LED_CTRL_1_KSZ9031_LED_MODE_; 3226 else 3227 temp &= ~LAN8814_LED_CTRL_1_KSZ9031_LED_MODE_; 3228 3229 lanphy_write_page_reg(phydev, 5, LAN8814_LED_CTRL_1, temp); 3230 } 3231 3232 static int lan8814_config_init(struct phy_device *phydev) 3233 { 3234 struct kszphy_priv *lan8814 = phydev->priv; 3235 int val; 3236 3237 /* Reset the PHY */ 3238 val = lanphy_read_page_reg(phydev, 4, LAN8814_QSGMII_SOFT_RESET); 3239 val |= LAN8814_QSGMII_SOFT_RESET_BIT; 3240 lanphy_write_page_reg(phydev, 4, LAN8814_QSGMII_SOFT_RESET, val); 3241 3242 /* Disable ANEG with QSGMII PCS Host side */ 3243 val = lanphy_read_page_reg(phydev, 5, LAN8814_QSGMII_PCS1G_ANEG_CONFIG); 3244 val &= ~LAN8814_QSGMII_PCS1G_ANEG_CONFIG_ANEG_ENA; 3245 lanphy_write_page_reg(phydev, 5, LAN8814_QSGMII_PCS1G_ANEG_CONFIG, val); 3246 3247 /* MDI-X setting for swap A,B transmit */ 3248 val = lanphy_read_page_reg(phydev, 2, LAN8814_ALIGN_SWAP); 3249 val &= ~LAN8814_ALIGN_TX_A_B_SWAP_MASK; 3250 val |= LAN8814_ALIGN_TX_A_B_SWAP; 3251 lanphy_write_page_reg(phydev, 2, LAN8814_ALIGN_SWAP, val); 3252 3253 if (lan8814->led_mode >= 0) 3254 lan8814_setup_led(phydev, lan8814->led_mode); 3255 3256 return 0; 3257 } 3258 3259 /* It is expected that there will not be any 'lan8814_take_coma_mode' 3260 * function called in suspend. Because the GPIO line can be shared, so if one of 3261 * the phys goes back in coma mode, then all the other PHYs will go, which is 3262 * wrong. 3263 */ 3264 static int lan8814_release_coma_mode(struct phy_device *phydev) 3265 { 3266 struct gpio_desc *gpiod; 3267 3268 gpiod = devm_gpiod_get_optional(&phydev->mdio.dev, "coma-mode", 3269 GPIOD_OUT_HIGH_OPEN_DRAIN | 3270 GPIOD_FLAGS_BIT_NONEXCLUSIVE); 3271 if (IS_ERR(gpiod)) 3272 return PTR_ERR(gpiod); 3273 3274 gpiod_set_consumer_name(gpiod, "LAN8814 coma mode"); 3275 gpiod_set_value_cansleep(gpiod, 0); 3276 3277 return 0; 3278 } 3279 3280 static int lan8814_probe(struct phy_device *phydev) 3281 { 3282 const struct kszphy_type *type = phydev->drv->driver_data; 3283 struct kszphy_priv *priv; 3284 u16 addr; 3285 int err; 3286 3287 priv = devm_kzalloc(&phydev->mdio.dev, sizeof(*priv), GFP_KERNEL); 3288 if (!priv) 3289 return -ENOMEM; 3290 3291 phydev->priv = priv; 3292 3293 priv->type = type; 3294 3295 kszphy_parse_led_mode(phydev); 3296 3297 /* Strap-in value for PHY address, below register read gives starting 3298 * phy address value 3299 */ 3300 addr = lanphy_read_page_reg(phydev, 4, 0) & 0x1F; 3301 devm_phy_package_join(&phydev->mdio.dev, phydev, 3302 addr, sizeof(struct lan8814_shared_priv)); 3303 3304 if (phy_package_init_once(phydev)) { 3305 err = lan8814_release_coma_mode(phydev); 3306 if (err) 3307 return err; 3308 3309 err = lan8814_ptp_probe_once(phydev); 3310 if (err) 3311 return err; 3312 } 3313 3314 lan8814_ptp_init(phydev); 3315 3316 return 0; 3317 } 3318 3319 #define LAN8841_MMD_TIMER_REG 0 3320 #define LAN8841_MMD0_REGISTER_17 17 3321 #define LAN8841_MMD0_REGISTER_17_DROP_OPT(x) ((x) & 0x3) 3322 #define LAN8841_MMD0_REGISTER_17_XMIT_TOG_TX_DIS BIT(3) 3323 #define LAN8841_OPERATION_MODE_STRAP_OVERRIDE_LOW_REG 2 3324 #define LAN8841_OPERATION_MODE_STRAP_OVERRIDE_LOW_REG_MAGJACK BIT(14) 3325 #define LAN8841_MMD_ANALOG_REG 28 3326 #define LAN8841_ANALOG_CONTROL_1 1 3327 #define LAN8841_ANALOG_CONTROL_1_PLL_TRIM(x) (((x) & 0x3) << 5) 3328 #define LAN8841_ANALOG_CONTROL_10 13 3329 #define LAN8841_ANALOG_CONTROL_10_PLL_DIV(x) ((x) & 0x3) 3330 #define LAN8841_ANALOG_CONTROL_11 14 3331 #define LAN8841_ANALOG_CONTROL_11_LDO_REF(x) (((x) & 0x7) << 12) 3332 #define LAN8841_TX_LOW_I_CH_C_D_POWER_MANAGMENT 69 3333 #define LAN8841_TX_LOW_I_CH_C_D_POWER_MANAGMENT_VAL 0xbffc 3334 #define LAN8841_BTRX_POWER_DOWN 70 3335 #define LAN8841_BTRX_POWER_DOWN_QBIAS_CH_A BIT(0) 3336 #define LAN8841_BTRX_POWER_DOWN_BTRX_CH_A BIT(1) 3337 #define LAN8841_BTRX_POWER_DOWN_QBIAS_CH_B BIT(2) 3338 #define LAN8841_BTRX_POWER_DOWN_BTRX_CH_B BIT(3) 3339 #define LAN8841_BTRX_POWER_DOWN_BTRX_CH_C BIT(5) 3340 #define LAN8841_BTRX_POWER_DOWN_BTRX_CH_D BIT(7) 3341 #define LAN8841_ADC_CHANNEL_MASK 198 3342 #define LAN8841_PTP_RX_PARSE_L2_ADDR_EN 370 3343 #define LAN8841_PTP_RX_PARSE_IP_ADDR_EN 371 3344 #define LAN8841_PTP_RX_VERSION 374 3345 #define LAN8841_PTP_TX_PARSE_L2_ADDR_EN 434 3346 #define LAN8841_PTP_TX_PARSE_IP_ADDR_EN 435 3347 #define LAN8841_PTP_TX_VERSION 438 3348 #define LAN8841_PTP_CMD_CTL 256 3349 #define LAN8841_PTP_CMD_CTL_PTP_ENABLE BIT(2) 3350 #define LAN8841_PTP_CMD_CTL_PTP_DISABLE BIT(1) 3351 #define LAN8841_PTP_CMD_CTL_PTP_RESET BIT(0) 3352 #define LAN8841_PTP_RX_PARSE_CONFIG 368 3353 #define LAN8841_PTP_TX_PARSE_CONFIG 432 3354 #define LAN8841_PTP_RX_MODE 381 3355 #define LAN8841_PTP_INSERT_TS_EN BIT(0) 3356 #define LAN8841_PTP_INSERT_TS_32BIT BIT(1) 3357 3358 static int lan8841_config_init(struct phy_device *phydev) 3359 { 3360 int ret; 3361 3362 ret = ksz9131_config_init(phydev); 3363 if (ret) 3364 return ret; 3365 3366 /* Initialize the HW by resetting everything */ 3367 phy_modify_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG, 3368 LAN8841_PTP_CMD_CTL, 3369 LAN8841_PTP_CMD_CTL_PTP_RESET, 3370 LAN8841_PTP_CMD_CTL_PTP_RESET); 3371 3372 phy_modify_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG, 3373 LAN8841_PTP_CMD_CTL, 3374 LAN8841_PTP_CMD_CTL_PTP_ENABLE, 3375 LAN8841_PTP_CMD_CTL_PTP_ENABLE); 3376 3377 /* Don't process any frames */ 3378 phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG, 3379 LAN8841_PTP_RX_PARSE_CONFIG, 0); 3380 phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG, 3381 LAN8841_PTP_TX_PARSE_CONFIG, 0); 3382 phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG, 3383 LAN8841_PTP_TX_PARSE_L2_ADDR_EN, 0); 3384 phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG, 3385 LAN8841_PTP_RX_PARSE_L2_ADDR_EN, 0); 3386 phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG, 3387 LAN8841_PTP_TX_PARSE_IP_ADDR_EN, 0); 3388 phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG, 3389 LAN8841_PTP_RX_PARSE_IP_ADDR_EN, 0); 3390 3391 /* Disable checking for minorVersionPTP field */ 3392 phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG, 3393 LAN8841_PTP_RX_VERSION, 0xff00); 3394 phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG, 3395 LAN8841_PTP_TX_VERSION, 0xff00); 3396 3397 /* 100BT Clause 40 improvenent errata */ 3398 phy_write_mmd(phydev, LAN8841_MMD_ANALOG_REG, 3399 LAN8841_ANALOG_CONTROL_1, 3400 LAN8841_ANALOG_CONTROL_1_PLL_TRIM(0x2)); 3401 phy_write_mmd(phydev, LAN8841_MMD_ANALOG_REG, 3402 LAN8841_ANALOG_CONTROL_10, 3403 LAN8841_ANALOG_CONTROL_10_PLL_DIV(0x1)); 3404 3405 /* 10M/100M Ethernet Signal Tuning Errata for Shorted-Center Tap 3406 * Magnetics 3407 */ 3408 ret = phy_read_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG, 3409 LAN8841_OPERATION_MODE_STRAP_OVERRIDE_LOW_REG); 3410 if (ret & LAN8841_OPERATION_MODE_STRAP_OVERRIDE_LOW_REG_MAGJACK) { 3411 phy_write_mmd(phydev, LAN8841_MMD_ANALOG_REG, 3412 LAN8841_TX_LOW_I_CH_C_D_POWER_MANAGMENT, 3413 LAN8841_TX_LOW_I_CH_C_D_POWER_MANAGMENT_VAL); 3414 phy_write_mmd(phydev, LAN8841_MMD_ANALOG_REG, 3415 LAN8841_BTRX_POWER_DOWN, 3416 LAN8841_BTRX_POWER_DOWN_QBIAS_CH_A | 3417 LAN8841_BTRX_POWER_DOWN_BTRX_CH_A | 3418 LAN8841_BTRX_POWER_DOWN_QBIAS_CH_B | 3419 LAN8841_BTRX_POWER_DOWN_BTRX_CH_B | 3420 LAN8841_BTRX_POWER_DOWN_BTRX_CH_C | 3421 LAN8841_BTRX_POWER_DOWN_BTRX_CH_D); 3422 } 3423 3424 /* LDO Adjustment errata */ 3425 phy_write_mmd(phydev, LAN8841_MMD_ANALOG_REG, 3426 LAN8841_ANALOG_CONTROL_11, 3427 LAN8841_ANALOG_CONTROL_11_LDO_REF(1)); 3428 3429 /* 100BT RGMII latency tuning errata */ 3430 phy_write_mmd(phydev, MDIO_MMD_PMAPMD, 3431 LAN8841_ADC_CHANNEL_MASK, 0x0); 3432 phy_write_mmd(phydev, LAN8841_MMD_TIMER_REG, 3433 LAN8841_MMD0_REGISTER_17, 3434 LAN8841_MMD0_REGISTER_17_DROP_OPT(2) | 3435 LAN8841_MMD0_REGISTER_17_XMIT_TOG_TX_DIS); 3436 3437 return 0; 3438 } 3439 3440 #define LAN8841_OUTPUT_CTRL 25 3441 #define LAN8841_OUTPUT_CTRL_INT_BUFFER BIT(14) 3442 #define LAN8841_INT_PTP BIT(9) 3443 3444 static int lan8841_config_intr(struct phy_device *phydev) 3445 { 3446 int err; 3447 3448 phy_modify(phydev, LAN8841_OUTPUT_CTRL, 3449 LAN8841_OUTPUT_CTRL_INT_BUFFER, 0); 3450 3451 if (phydev->interrupts == PHY_INTERRUPT_ENABLED) { 3452 err = phy_read(phydev, LAN8814_INTS); 3453 if (err < 0) 3454 return err; 3455 3456 /* Enable / disable interrupts. It is OK to enable PTP interrupt 3457 * even if it PTP is not enabled. Because the underneath blocks 3458 * will not enable the PTP so we will never get the PTP 3459 * interrupt. 3460 */ 3461 err = phy_write(phydev, LAN8814_INTC, 3462 LAN8814_INT_LINK | LAN8841_INT_PTP); 3463 } else { 3464 err = phy_write(phydev, LAN8814_INTC, 0); 3465 if (err) 3466 return err; 3467 3468 err = phy_read(phydev, LAN8814_INTS); 3469 if (err < 0) 3470 return err; 3471 3472 /* Getting a positive value doesn't mean that is an error, it 3473 * just indicates what was the status. Therefore make sure to 3474 * clear the value and say that there is no error. 3475 */ 3476 err = 0; 3477 } 3478 3479 return err; 3480 } 3481 3482 #define LAN8841_PTP_TX_EGRESS_SEC_LO 453 3483 #define LAN8841_PTP_TX_EGRESS_SEC_HI 452 3484 #define LAN8841_PTP_TX_EGRESS_NS_LO 451 3485 #define LAN8841_PTP_TX_EGRESS_NS_HI 450 3486 #define LAN8841_PTP_TX_EGRESS_NSEC_HI_VALID BIT(15) 3487 #define LAN8841_PTP_TX_MSG_HEADER2 455 3488 3489 static bool lan8841_ptp_get_tx_ts(struct kszphy_ptp_priv *ptp_priv, 3490 u32 *sec, u32 *nsec, u16 *seq) 3491 { 3492 struct phy_device *phydev = ptp_priv->phydev; 3493 3494 *nsec = phy_read_mmd(phydev, 2, LAN8841_PTP_TX_EGRESS_NS_HI); 3495 if (!(*nsec & LAN8841_PTP_TX_EGRESS_NSEC_HI_VALID)) 3496 return false; 3497 3498 *nsec = ((*nsec & 0x3fff) << 16); 3499 *nsec = *nsec | phy_read_mmd(phydev, 2, LAN8841_PTP_TX_EGRESS_NS_LO); 3500 3501 *sec = phy_read_mmd(phydev, 2, LAN8841_PTP_TX_EGRESS_SEC_HI); 3502 *sec = *sec << 16; 3503 *sec = *sec | phy_read_mmd(phydev, 2, LAN8841_PTP_TX_EGRESS_SEC_LO); 3504 3505 *seq = phy_read_mmd(phydev, 2, LAN8841_PTP_TX_MSG_HEADER2); 3506 3507 return true; 3508 } 3509 3510 static void lan8841_ptp_process_tx_ts(struct kszphy_ptp_priv *ptp_priv) 3511 { 3512 u32 sec, nsec; 3513 u16 seq; 3514 3515 while (lan8841_ptp_get_tx_ts(ptp_priv, &sec, &nsec, &seq)) 3516 lan8814_match_tx_skb(ptp_priv, sec, nsec, seq); 3517 } 3518 3519 #define LAN8841_PTP_INT_STS 259 3520 #define LAN8841_PTP_INT_STS_PTP_TX_TS_OVRFL_INT BIT(13) 3521 #define LAN8841_PTP_INT_STS_PTP_TX_TS_INT BIT(12) 3522 #define LAN8841_PTP_INT_STS_PTP_GPIO_CAP_INT BIT(2) 3523 3524 static void lan8841_ptp_flush_fifo(struct kszphy_ptp_priv *ptp_priv) 3525 { 3526 struct phy_device *phydev = ptp_priv->phydev; 3527 int i; 3528 3529 for (i = 0; i < FIFO_SIZE; ++i) 3530 phy_read_mmd(phydev, 2, LAN8841_PTP_TX_MSG_HEADER2); 3531 3532 phy_read_mmd(phydev, 2, LAN8841_PTP_INT_STS); 3533 } 3534 3535 #define LAN8841_PTP_GPIO_CAP_STS 506 3536 #define LAN8841_PTP_GPIO_SEL 327 3537 #define LAN8841_PTP_GPIO_SEL_GPIO_SEL(gpio) ((gpio) << 8) 3538 #define LAN8841_PTP_GPIO_RE_LTC_SEC_HI_CAP 498 3539 #define LAN8841_PTP_GPIO_RE_LTC_SEC_LO_CAP 499 3540 #define LAN8841_PTP_GPIO_RE_LTC_NS_HI_CAP 500 3541 #define LAN8841_PTP_GPIO_RE_LTC_NS_LO_CAP 501 3542 #define LAN8841_PTP_GPIO_FE_LTC_SEC_HI_CAP 502 3543 #define LAN8841_PTP_GPIO_FE_LTC_SEC_LO_CAP 503 3544 #define LAN8841_PTP_GPIO_FE_LTC_NS_HI_CAP 504 3545 #define LAN8841_PTP_GPIO_FE_LTC_NS_LO_CAP 505 3546 3547 static void lan8841_gpio_process_cap(struct kszphy_ptp_priv *ptp_priv) 3548 { 3549 struct phy_device *phydev = ptp_priv->phydev; 3550 struct ptp_clock_event ptp_event = {0}; 3551 int pin, ret, tmp; 3552 s32 sec, nsec; 3553 3554 pin = ptp_find_pin_unlocked(ptp_priv->ptp_clock, PTP_PF_EXTTS, 0); 3555 if (pin == -1) 3556 return; 3557 3558 tmp = phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_CAP_STS); 3559 if (tmp < 0) 3560 return; 3561 3562 ret = phy_write_mmd(phydev, 2, LAN8841_PTP_GPIO_SEL, 3563 LAN8841_PTP_GPIO_SEL_GPIO_SEL(pin)); 3564 if (ret) 3565 return; 3566 3567 mutex_lock(&ptp_priv->ptp_lock); 3568 if (tmp & BIT(pin)) { 3569 sec = phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_RE_LTC_SEC_HI_CAP); 3570 sec <<= 16; 3571 sec |= phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_RE_LTC_SEC_LO_CAP); 3572 3573 nsec = phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_RE_LTC_NS_HI_CAP) & 0x3fff; 3574 nsec <<= 16; 3575 nsec |= phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_RE_LTC_NS_LO_CAP); 3576 } else { 3577 sec = phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_FE_LTC_SEC_HI_CAP); 3578 sec <<= 16; 3579 sec |= phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_FE_LTC_SEC_LO_CAP); 3580 3581 nsec = phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_FE_LTC_NS_HI_CAP) & 0x3fff; 3582 nsec <<= 16; 3583 nsec |= phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_FE_LTC_NS_LO_CAP); 3584 } 3585 mutex_unlock(&ptp_priv->ptp_lock); 3586 ret = phy_write_mmd(phydev, 2, LAN8841_PTP_GPIO_SEL, 0); 3587 if (ret) 3588 return; 3589 3590 ptp_event.index = 0; 3591 ptp_event.timestamp = ktime_set(sec, nsec); 3592 ptp_event.type = PTP_CLOCK_EXTTS; 3593 ptp_clock_event(ptp_priv->ptp_clock, &ptp_event); 3594 } 3595 3596 static void lan8841_handle_ptp_interrupt(struct phy_device *phydev) 3597 { 3598 struct kszphy_priv *priv = phydev->priv; 3599 struct kszphy_ptp_priv *ptp_priv = &priv->ptp_priv; 3600 u16 status; 3601 3602 do { 3603 status = phy_read_mmd(phydev, 2, LAN8841_PTP_INT_STS); 3604 3605 if (status & LAN8841_PTP_INT_STS_PTP_TX_TS_INT) 3606 lan8841_ptp_process_tx_ts(ptp_priv); 3607 3608 if (status & LAN8841_PTP_INT_STS_PTP_GPIO_CAP_INT) 3609 lan8841_gpio_process_cap(ptp_priv); 3610 3611 if (status & LAN8841_PTP_INT_STS_PTP_TX_TS_OVRFL_INT) { 3612 lan8841_ptp_flush_fifo(ptp_priv); 3613 skb_queue_purge(&ptp_priv->tx_queue); 3614 } 3615 3616 } while (status & (LAN8841_PTP_INT_STS_PTP_TX_TS_INT | 3617 LAN8841_PTP_INT_STS_PTP_GPIO_CAP_INT | 3618 LAN8841_PTP_INT_STS_PTP_TX_TS_OVRFL_INT)); 3619 } 3620 3621 #define LAN8841_INTS_PTP BIT(9) 3622 3623 static irqreturn_t lan8841_handle_interrupt(struct phy_device *phydev) 3624 { 3625 irqreturn_t ret = IRQ_NONE; 3626 int irq_status; 3627 3628 irq_status = phy_read(phydev, LAN8814_INTS); 3629 if (irq_status < 0) { 3630 phy_error(phydev); 3631 return IRQ_NONE; 3632 } 3633 3634 if (irq_status & LAN8814_INT_LINK) { 3635 phy_trigger_machine(phydev); 3636 ret = IRQ_HANDLED; 3637 } 3638 3639 if (irq_status & LAN8841_INTS_PTP) { 3640 lan8841_handle_ptp_interrupt(phydev); 3641 ret = IRQ_HANDLED; 3642 } 3643 3644 return ret; 3645 } 3646 3647 static int lan8841_ts_info(struct mii_timestamper *mii_ts, 3648 struct ethtool_ts_info *info) 3649 { 3650 struct kszphy_ptp_priv *ptp_priv; 3651 3652 ptp_priv = container_of(mii_ts, struct kszphy_ptp_priv, mii_ts); 3653 3654 info->phc_index = ptp_priv->ptp_clock ? 3655 ptp_clock_index(ptp_priv->ptp_clock) : -1; 3656 if (info->phc_index == -1) 3657 return 0; 3658 3659 info->so_timestamping = SOF_TIMESTAMPING_TX_HARDWARE | 3660 SOF_TIMESTAMPING_RX_HARDWARE | 3661 SOF_TIMESTAMPING_RAW_HARDWARE; 3662 3663 info->tx_types = (1 << HWTSTAMP_TX_OFF) | 3664 (1 << HWTSTAMP_TX_ON) | 3665 (1 << HWTSTAMP_TX_ONESTEP_SYNC); 3666 3667 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) | 3668 (1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT) | 3669 (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) | 3670 (1 << HWTSTAMP_FILTER_PTP_V2_EVENT); 3671 3672 return 0; 3673 } 3674 3675 #define LAN8841_PTP_INT_EN 260 3676 #define LAN8841_PTP_INT_EN_PTP_TX_TS_OVRFL_EN BIT(13) 3677 #define LAN8841_PTP_INT_EN_PTP_TX_TS_EN BIT(12) 3678 3679 static void lan8841_ptp_enable_processing(struct kszphy_ptp_priv *ptp_priv, 3680 bool enable) 3681 { 3682 struct phy_device *phydev = ptp_priv->phydev; 3683 3684 if (enable) { 3685 /* Enable interrupts on the TX side */ 3686 phy_modify_mmd(phydev, 2, LAN8841_PTP_INT_EN, 3687 LAN8841_PTP_INT_EN_PTP_TX_TS_OVRFL_EN | 3688 LAN8841_PTP_INT_EN_PTP_TX_TS_EN, 3689 LAN8841_PTP_INT_EN_PTP_TX_TS_OVRFL_EN | 3690 LAN8841_PTP_INT_EN_PTP_TX_TS_EN); 3691 3692 /* Enable the modification of the frame on RX side, 3693 * this will add the ns and 2 bits of sec in the reserved field 3694 * of the PTP header 3695 */ 3696 phy_modify_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG, 3697 LAN8841_PTP_RX_MODE, 3698 LAN8841_PTP_INSERT_TS_EN | 3699 LAN8841_PTP_INSERT_TS_32BIT, 3700 LAN8841_PTP_INSERT_TS_EN | 3701 LAN8841_PTP_INSERT_TS_32BIT); 3702 3703 ptp_schedule_worker(ptp_priv->ptp_clock, 0); 3704 } else { 3705 /* Disable interrupts on the TX side */ 3706 phy_modify_mmd(phydev, 2, LAN8841_PTP_INT_EN, 3707 LAN8841_PTP_INT_EN_PTP_TX_TS_OVRFL_EN | 3708 LAN8841_PTP_INT_EN_PTP_TX_TS_EN, 0); 3709 3710 /* Disable modification of the RX frames */ 3711 phy_modify_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG, 3712 LAN8841_PTP_RX_MODE, 3713 LAN8841_PTP_INSERT_TS_EN | 3714 LAN8841_PTP_INSERT_TS_32BIT, 0); 3715 3716 ptp_cancel_worker_sync(ptp_priv->ptp_clock); 3717 } 3718 } 3719 3720 #define LAN8841_PTP_RX_TIMESTAMP_EN 379 3721 #define LAN8841_PTP_TX_TIMESTAMP_EN 443 3722 #define LAN8841_PTP_TX_MOD 445 3723 3724 static int lan8841_hwtstamp(struct mii_timestamper *mii_ts, struct ifreq *ifr) 3725 { 3726 struct kszphy_ptp_priv *ptp_priv = container_of(mii_ts, struct kszphy_ptp_priv, mii_ts); 3727 struct phy_device *phydev = ptp_priv->phydev; 3728 struct hwtstamp_config config; 3729 int txcfg = 0, rxcfg = 0; 3730 int pkt_ts_enable; 3731 3732 if (copy_from_user(&config, ifr->ifr_data, sizeof(config))) 3733 return -EFAULT; 3734 3735 ptp_priv->hwts_tx_type = config.tx_type; 3736 ptp_priv->rx_filter = config.rx_filter; 3737 3738 switch (config.rx_filter) { 3739 case HWTSTAMP_FILTER_NONE: 3740 ptp_priv->layer = 0; 3741 ptp_priv->version = 0; 3742 break; 3743 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT: 3744 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC: 3745 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ: 3746 ptp_priv->layer = PTP_CLASS_L4; 3747 ptp_priv->version = PTP_CLASS_V2; 3748 break; 3749 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT: 3750 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC: 3751 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ: 3752 ptp_priv->layer = PTP_CLASS_L2; 3753 ptp_priv->version = PTP_CLASS_V2; 3754 break; 3755 case HWTSTAMP_FILTER_PTP_V2_EVENT: 3756 case HWTSTAMP_FILTER_PTP_V2_SYNC: 3757 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ: 3758 ptp_priv->layer = PTP_CLASS_L4 | PTP_CLASS_L2; 3759 ptp_priv->version = PTP_CLASS_V2; 3760 break; 3761 default: 3762 return -ERANGE; 3763 } 3764 3765 /* Setup parsing of the frames and enable the timestamping for ptp 3766 * frames 3767 */ 3768 if (ptp_priv->layer & PTP_CLASS_L2) { 3769 rxcfg |= PTP_RX_PARSE_CONFIG_LAYER2_EN_; 3770 txcfg |= PTP_TX_PARSE_CONFIG_LAYER2_EN_; 3771 } else if (ptp_priv->layer & PTP_CLASS_L4) { 3772 rxcfg |= PTP_RX_PARSE_CONFIG_IPV4_EN_ | PTP_RX_PARSE_CONFIG_IPV6_EN_; 3773 txcfg |= PTP_TX_PARSE_CONFIG_IPV4_EN_ | PTP_TX_PARSE_CONFIG_IPV6_EN_; 3774 } 3775 3776 phy_write_mmd(phydev, 2, LAN8841_PTP_RX_PARSE_CONFIG, rxcfg); 3777 phy_write_mmd(phydev, 2, LAN8841_PTP_TX_PARSE_CONFIG, txcfg); 3778 3779 pkt_ts_enable = PTP_TIMESTAMP_EN_SYNC_ | PTP_TIMESTAMP_EN_DREQ_ | 3780 PTP_TIMESTAMP_EN_PDREQ_ | PTP_TIMESTAMP_EN_PDRES_; 3781 phy_write_mmd(phydev, 2, LAN8841_PTP_RX_TIMESTAMP_EN, pkt_ts_enable); 3782 phy_write_mmd(phydev, 2, LAN8841_PTP_TX_TIMESTAMP_EN, pkt_ts_enable); 3783 3784 /* Enable / disable of the TX timestamp in the SYNC frames */ 3785 phy_modify_mmd(phydev, 2, LAN8841_PTP_TX_MOD, 3786 PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_, 3787 ptp_priv->hwts_tx_type == HWTSTAMP_TX_ONESTEP_SYNC ? 3788 PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_ : 0); 3789 3790 /* Now enable/disable the timestamping */ 3791 lan8841_ptp_enable_processing(ptp_priv, 3792 config.rx_filter != HWTSTAMP_FILTER_NONE); 3793 3794 skb_queue_purge(&ptp_priv->tx_queue); 3795 3796 lan8841_ptp_flush_fifo(ptp_priv); 3797 3798 return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ? -EFAULT : 0; 3799 } 3800 3801 static bool lan8841_rxtstamp(struct mii_timestamper *mii_ts, 3802 struct sk_buff *skb, int type) 3803 { 3804 struct kszphy_ptp_priv *ptp_priv = 3805 container_of(mii_ts, struct kszphy_ptp_priv, mii_ts); 3806 struct ptp_header *header = ptp_parse_header(skb, type); 3807 struct skb_shared_hwtstamps *shhwtstamps; 3808 struct timespec64 ts; 3809 unsigned long flags; 3810 u32 ts_header; 3811 3812 if (!header) 3813 return false; 3814 3815 if (ptp_priv->rx_filter == HWTSTAMP_FILTER_NONE || 3816 type == PTP_CLASS_NONE) 3817 return false; 3818 3819 if ((type & ptp_priv->version) == 0 || (type & ptp_priv->layer) == 0) 3820 return false; 3821 3822 spin_lock_irqsave(&ptp_priv->seconds_lock, flags); 3823 ts.tv_sec = ptp_priv->seconds; 3824 spin_unlock_irqrestore(&ptp_priv->seconds_lock, flags); 3825 ts_header = __be32_to_cpu(header->reserved2); 3826 3827 shhwtstamps = skb_hwtstamps(skb); 3828 memset(shhwtstamps, 0, sizeof(*shhwtstamps)); 3829 3830 /* Check for any wrap arounds for the second part */ 3831 if ((ts.tv_sec & GENMASK(1, 0)) == 0 && (ts_header >> 30) == 3) 3832 ts.tv_sec -= GENMASK(1, 0) + 1; 3833 else if ((ts.tv_sec & GENMASK(1, 0)) == 3 && (ts_header >> 30) == 0) 3834 ts.tv_sec += 1; 3835 3836 shhwtstamps->hwtstamp = 3837 ktime_set((ts.tv_sec & ~(GENMASK(1, 0))) | ts_header >> 30, 3838 ts_header & GENMASK(29, 0)); 3839 header->reserved2 = 0; 3840 3841 netif_rx(skb); 3842 3843 return true; 3844 } 3845 3846 #define LAN8841_EVENT_A 0 3847 #define LAN8841_EVENT_B 1 3848 #define LAN8841_PTP_LTC_TARGET_SEC_HI(event) ((event) == LAN8841_EVENT_A ? 278 : 288) 3849 #define LAN8841_PTP_LTC_TARGET_SEC_LO(event) ((event) == LAN8841_EVENT_A ? 279 : 289) 3850 #define LAN8841_PTP_LTC_TARGET_NS_HI(event) ((event) == LAN8841_EVENT_A ? 280 : 290) 3851 #define LAN8841_PTP_LTC_TARGET_NS_LO(event) ((event) == LAN8841_EVENT_A ? 281 : 291) 3852 3853 static int lan8841_ptp_set_target(struct kszphy_ptp_priv *ptp_priv, u8 event, 3854 s64 sec, u32 nsec) 3855 { 3856 struct phy_device *phydev = ptp_priv->phydev; 3857 int ret; 3858 3859 ret = phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_TARGET_SEC_HI(event), 3860 upper_16_bits(sec)); 3861 if (ret) 3862 return ret; 3863 3864 ret = phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_TARGET_SEC_LO(event), 3865 lower_16_bits(sec)); 3866 if (ret) 3867 return ret; 3868 3869 ret = phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_TARGET_NS_HI(event) & 0x3fff, 3870 upper_16_bits(nsec)); 3871 if (ret) 3872 return ret; 3873 3874 return phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_TARGET_NS_LO(event), 3875 lower_16_bits(nsec)); 3876 } 3877 3878 #define LAN8841_BUFFER_TIME 2 3879 3880 static int lan8841_ptp_update_target(struct kszphy_ptp_priv *ptp_priv, 3881 const struct timespec64 *ts) 3882 { 3883 return lan8841_ptp_set_target(ptp_priv, LAN8841_EVENT_A, 3884 ts->tv_sec + LAN8841_BUFFER_TIME, 0); 3885 } 3886 3887 #define LAN8841_PTP_LTC_TARGET_RELOAD_SEC_HI(event) ((event) == LAN8841_EVENT_A ? 282 : 292) 3888 #define LAN8841_PTP_LTC_TARGET_RELOAD_SEC_LO(event) ((event) == LAN8841_EVENT_A ? 283 : 293) 3889 #define LAN8841_PTP_LTC_TARGET_RELOAD_NS_HI(event) ((event) == LAN8841_EVENT_A ? 284 : 294) 3890 #define LAN8841_PTP_LTC_TARGET_RELOAD_NS_LO(event) ((event) == LAN8841_EVENT_A ? 285 : 295) 3891 3892 static int lan8841_ptp_set_reload(struct kszphy_ptp_priv *ptp_priv, u8 event, 3893 s64 sec, u32 nsec) 3894 { 3895 struct phy_device *phydev = ptp_priv->phydev; 3896 int ret; 3897 3898 ret = phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_TARGET_RELOAD_SEC_HI(event), 3899 upper_16_bits(sec)); 3900 if (ret) 3901 return ret; 3902 3903 ret = phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_TARGET_RELOAD_SEC_LO(event), 3904 lower_16_bits(sec)); 3905 if (ret) 3906 return ret; 3907 3908 ret = phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_TARGET_RELOAD_NS_HI(event) & 0x3fff, 3909 upper_16_bits(nsec)); 3910 if (ret) 3911 return ret; 3912 3913 return phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_TARGET_RELOAD_NS_LO(event), 3914 lower_16_bits(nsec)); 3915 } 3916 3917 #define LAN8841_PTP_LTC_SET_SEC_HI 262 3918 #define LAN8841_PTP_LTC_SET_SEC_MID 263 3919 #define LAN8841_PTP_LTC_SET_SEC_LO 264 3920 #define LAN8841_PTP_LTC_SET_NS_HI 265 3921 #define LAN8841_PTP_LTC_SET_NS_LO 266 3922 #define LAN8841_PTP_CMD_CTL_PTP_LTC_LOAD BIT(4) 3923 3924 static int lan8841_ptp_settime64(struct ptp_clock_info *ptp, 3925 const struct timespec64 *ts) 3926 { 3927 struct kszphy_ptp_priv *ptp_priv = container_of(ptp, struct kszphy_ptp_priv, 3928 ptp_clock_info); 3929 struct phy_device *phydev = ptp_priv->phydev; 3930 unsigned long flags; 3931 int ret; 3932 3933 /* Set the value to be stored */ 3934 mutex_lock(&ptp_priv->ptp_lock); 3935 phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_SET_SEC_LO, lower_16_bits(ts->tv_sec)); 3936 phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_SET_SEC_MID, upper_16_bits(ts->tv_sec)); 3937 phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_SET_SEC_HI, upper_32_bits(ts->tv_sec) & 0xffff); 3938 phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_SET_NS_LO, lower_16_bits(ts->tv_nsec)); 3939 phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_SET_NS_HI, upper_16_bits(ts->tv_nsec) & 0x3fff); 3940 3941 /* Set the command to load the LTC */ 3942 phy_write_mmd(phydev, 2, LAN8841_PTP_CMD_CTL, 3943 LAN8841_PTP_CMD_CTL_PTP_LTC_LOAD); 3944 ret = lan8841_ptp_update_target(ptp_priv, ts); 3945 mutex_unlock(&ptp_priv->ptp_lock); 3946 3947 spin_lock_irqsave(&ptp_priv->seconds_lock, flags); 3948 ptp_priv->seconds = ts->tv_sec; 3949 spin_unlock_irqrestore(&ptp_priv->seconds_lock, flags); 3950 3951 return ret; 3952 } 3953 3954 #define LAN8841_PTP_LTC_RD_SEC_HI 358 3955 #define LAN8841_PTP_LTC_RD_SEC_MID 359 3956 #define LAN8841_PTP_LTC_RD_SEC_LO 360 3957 #define LAN8841_PTP_LTC_RD_NS_HI 361 3958 #define LAN8841_PTP_LTC_RD_NS_LO 362 3959 #define LAN8841_PTP_CMD_CTL_PTP_LTC_READ BIT(3) 3960 3961 static int lan8841_ptp_gettime64(struct ptp_clock_info *ptp, 3962 struct timespec64 *ts) 3963 { 3964 struct kszphy_ptp_priv *ptp_priv = container_of(ptp, struct kszphy_ptp_priv, 3965 ptp_clock_info); 3966 struct phy_device *phydev = ptp_priv->phydev; 3967 time64_t s; 3968 s64 ns; 3969 3970 mutex_lock(&ptp_priv->ptp_lock); 3971 /* Issue the command to read the LTC */ 3972 phy_write_mmd(phydev, 2, LAN8841_PTP_CMD_CTL, 3973 LAN8841_PTP_CMD_CTL_PTP_LTC_READ); 3974 3975 /* Read the LTC */ 3976 s = phy_read_mmd(phydev, 2, LAN8841_PTP_LTC_RD_SEC_HI); 3977 s <<= 16; 3978 s |= phy_read_mmd(phydev, 2, LAN8841_PTP_LTC_RD_SEC_MID); 3979 s <<= 16; 3980 s |= phy_read_mmd(phydev, 2, LAN8841_PTP_LTC_RD_SEC_LO); 3981 3982 ns = phy_read_mmd(phydev, 2, LAN8841_PTP_LTC_RD_NS_HI) & 0x3fff; 3983 ns <<= 16; 3984 ns |= phy_read_mmd(phydev, 2, LAN8841_PTP_LTC_RD_NS_LO); 3985 mutex_unlock(&ptp_priv->ptp_lock); 3986 3987 set_normalized_timespec64(ts, s, ns); 3988 return 0; 3989 } 3990 3991 static void lan8841_ptp_getseconds(struct ptp_clock_info *ptp, 3992 struct timespec64 *ts) 3993 { 3994 struct kszphy_ptp_priv *ptp_priv = container_of(ptp, struct kszphy_ptp_priv, 3995 ptp_clock_info); 3996 struct phy_device *phydev = ptp_priv->phydev; 3997 time64_t s; 3998 3999 mutex_lock(&ptp_priv->ptp_lock); 4000 /* Issue the command to read the LTC */ 4001 phy_write_mmd(phydev, 2, LAN8841_PTP_CMD_CTL, 4002 LAN8841_PTP_CMD_CTL_PTP_LTC_READ); 4003 4004 /* Read the LTC */ 4005 s = phy_read_mmd(phydev, 2, LAN8841_PTP_LTC_RD_SEC_HI); 4006 s <<= 16; 4007 s |= phy_read_mmd(phydev, 2, LAN8841_PTP_LTC_RD_SEC_MID); 4008 s <<= 16; 4009 s |= phy_read_mmd(phydev, 2, LAN8841_PTP_LTC_RD_SEC_LO); 4010 mutex_unlock(&ptp_priv->ptp_lock); 4011 4012 set_normalized_timespec64(ts, s, 0); 4013 } 4014 4015 #define LAN8841_PTP_LTC_STEP_ADJ_LO 276 4016 #define LAN8841_PTP_LTC_STEP_ADJ_HI 275 4017 #define LAN8841_PTP_LTC_STEP_ADJ_DIR BIT(15) 4018 #define LAN8841_PTP_CMD_CTL_PTP_LTC_STEP_SECONDS BIT(5) 4019 #define LAN8841_PTP_CMD_CTL_PTP_LTC_STEP_NANOSECONDS BIT(6) 4020 4021 static int lan8841_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta) 4022 { 4023 struct kszphy_ptp_priv *ptp_priv = container_of(ptp, struct kszphy_ptp_priv, 4024 ptp_clock_info); 4025 struct phy_device *phydev = ptp_priv->phydev; 4026 struct timespec64 ts; 4027 bool add = true; 4028 u32 nsec; 4029 s32 sec; 4030 int ret; 4031 4032 /* The HW allows up to 15 sec to adjust the time, but here we limit to 4033 * 10 sec the adjustment. The reason is, in case the adjustment is 14 4034 * sec and 999999999 nsec, then we add 8ns to compansate the actual 4035 * increment so the value can be bigger than 15 sec. Therefore limit the 4036 * possible adjustments so we will not have these corner cases 4037 */ 4038 if (delta > 10000000000LL || delta < -10000000000LL) { 4039 /* The timeadjustment is too big, so fall back using set time */ 4040 u64 now; 4041 4042 ptp->gettime64(ptp, &ts); 4043 4044 now = ktime_to_ns(timespec64_to_ktime(ts)); 4045 ts = ns_to_timespec64(now + delta); 4046 4047 ptp->settime64(ptp, &ts); 4048 return 0; 4049 } 4050 4051 sec = div_u64_rem(delta < 0 ? -delta : delta, NSEC_PER_SEC, &nsec); 4052 if (delta < 0 && nsec != 0) { 4053 /* It is not allowed to adjust low the nsec part, therefore 4054 * subtract more from second part and add to nanosecond such 4055 * that would roll over, so the second part will increase 4056 */ 4057 sec--; 4058 nsec = NSEC_PER_SEC - nsec; 4059 } 4060 4061 /* Calculate the adjustments and the direction */ 4062 if (delta < 0) 4063 add = false; 4064 4065 if (nsec > 0) 4066 /* add 8 ns to cover the likely normal increment */ 4067 nsec += 8; 4068 4069 if (nsec >= NSEC_PER_SEC) { 4070 /* carry into seconds */ 4071 sec++; 4072 nsec -= NSEC_PER_SEC; 4073 } 4074 4075 mutex_lock(&ptp_priv->ptp_lock); 4076 if (sec) { 4077 phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_STEP_ADJ_LO, sec); 4078 phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_STEP_ADJ_HI, 4079 add ? LAN8841_PTP_LTC_STEP_ADJ_DIR : 0); 4080 phy_write_mmd(phydev, 2, LAN8841_PTP_CMD_CTL, 4081 LAN8841_PTP_CMD_CTL_PTP_LTC_STEP_SECONDS); 4082 } 4083 4084 if (nsec) { 4085 phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_STEP_ADJ_LO, 4086 nsec & 0xffff); 4087 phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_STEP_ADJ_HI, 4088 (nsec >> 16) & 0x3fff); 4089 phy_write_mmd(phydev, 2, LAN8841_PTP_CMD_CTL, 4090 LAN8841_PTP_CMD_CTL_PTP_LTC_STEP_NANOSECONDS); 4091 } 4092 mutex_unlock(&ptp_priv->ptp_lock); 4093 4094 /* Update the target clock */ 4095 ptp->gettime64(ptp, &ts); 4096 mutex_lock(&ptp_priv->ptp_lock); 4097 ret = lan8841_ptp_update_target(ptp_priv, &ts); 4098 mutex_unlock(&ptp_priv->ptp_lock); 4099 4100 return ret; 4101 } 4102 4103 #define LAN8841_PTP_LTC_RATE_ADJ_HI 269 4104 #define LAN8841_PTP_LTC_RATE_ADJ_HI_DIR BIT(15) 4105 #define LAN8841_PTP_LTC_RATE_ADJ_LO 270 4106 4107 static int lan8841_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm) 4108 { 4109 struct kszphy_ptp_priv *ptp_priv = container_of(ptp, struct kszphy_ptp_priv, 4110 ptp_clock_info); 4111 struct phy_device *phydev = ptp_priv->phydev; 4112 bool faster = true; 4113 u32 rate; 4114 4115 if (!scaled_ppm) 4116 return 0; 4117 4118 if (scaled_ppm < 0) { 4119 scaled_ppm = -scaled_ppm; 4120 faster = false; 4121 } 4122 4123 rate = LAN8814_1PPM_FORMAT * (upper_16_bits(scaled_ppm)); 4124 rate += (LAN8814_1PPM_FORMAT * (lower_16_bits(scaled_ppm))) >> 16; 4125 4126 mutex_lock(&ptp_priv->ptp_lock); 4127 phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_RATE_ADJ_HI, 4128 faster ? LAN8841_PTP_LTC_RATE_ADJ_HI_DIR | (upper_16_bits(rate) & 0x3fff) 4129 : upper_16_bits(rate) & 0x3fff); 4130 phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_RATE_ADJ_LO, lower_16_bits(rate)); 4131 mutex_unlock(&ptp_priv->ptp_lock); 4132 4133 return 0; 4134 } 4135 4136 static int lan8841_ptp_verify(struct ptp_clock_info *ptp, unsigned int pin, 4137 enum ptp_pin_function func, unsigned int chan) 4138 { 4139 switch (func) { 4140 case PTP_PF_NONE: 4141 case PTP_PF_PEROUT: 4142 case PTP_PF_EXTTS: 4143 break; 4144 default: 4145 return -1; 4146 } 4147 4148 return 0; 4149 } 4150 4151 #define LAN8841_PTP_GPIO_NUM 10 4152 #define LAN8841_GPIO_EN 128 4153 #define LAN8841_GPIO_DIR 129 4154 #define LAN8841_GPIO_BUF 130 4155 4156 static int lan8841_ptp_perout_off(struct kszphy_ptp_priv *ptp_priv, int pin) 4157 { 4158 struct phy_device *phydev = ptp_priv->phydev; 4159 int ret; 4160 4161 ret = phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_EN, BIT(pin)); 4162 if (ret) 4163 return ret; 4164 4165 ret = phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_DIR, BIT(pin)); 4166 if (ret) 4167 return ret; 4168 4169 return phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_BUF, BIT(pin)); 4170 } 4171 4172 static int lan8841_ptp_perout_on(struct kszphy_ptp_priv *ptp_priv, int pin) 4173 { 4174 struct phy_device *phydev = ptp_priv->phydev; 4175 int ret; 4176 4177 ret = phy_set_bits_mmd(phydev, 2, LAN8841_GPIO_EN, BIT(pin)); 4178 if (ret) 4179 return ret; 4180 4181 ret = phy_set_bits_mmd(phydev, 2, LAN8841_GPIO_DIR, BIT(pin)); 4182 if (ret) 4183 return ret; 4184 4185 return phy_set_bits_mmd(phydev, 2, LAN8841_GPIO_BUF, BIT(pin)); 4186 } 4187 4188 #define LAN8841_GPIO_DATA_SEL1 131 4189 #define LAN8841_GPIO_DATA_SEL2 132 4190 #define LAN8841_GPIO_DATA_SEL_GPIO_DATA_SEL_EVENT_MASK GENMASK(2, 0) 4191 #define LAN8841_GPIO_DATA_SEL_GPIO_DATA_SEL_EVENT_A 1 4192 #define LAN8841_GPIO_DATA_SEL_GPIO_DATA_SEL_EVENT_B 2 4193 #define LAN8841_PTP_GENERAL_CONFIG 257 4194 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_A BIT(1) 4195 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_B BIT(3) 4196 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_A_MASK GENMASK(7, 4) 4197 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_B_MASK GENMASK(11, 8) 4198 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_A 4 4199 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_B 7 4200 4201 static int lan8841_ptp_remove_event(struct kszphy_ptp_priv *ptp_priv, int pin, 4202 u8 event) 4203 { 4204 struct phy_device *phydev = ptp_priv->phydev; 4205 u16 tmp; 4206 int ret; 4207 4208 /* Now remove pin from the event. GPIO_DATA_SEL1 contains the GPIO 4209 * pins 0-4 while GPIO_DATA_SEL2 contains GPIO pins 5-9, therefore 4210 * depending on the pin, it requires to read a different register 4211 */ 4212 if (pin < 5) { 4213 tmp = LAN8841_GPIO_DATA_SEL_GPIO_DATA_SEL_EVENT_MASK << (3 * pin); 4214 ret = phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_DATA_SEL1, tmp); 4215 } else { 4216 tmp = LAN8841_GPIO_DATA_SEL_GPIO_DATA_SEL_EVENT_MASK << (3 * (pin - 5)); 4217 ret = phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_DATA_SEL2, tmp); 4218 } 4219 if (ret) 4220 return ret; 4221 4222 /* Disable the event */ 4223 if (event == LAN8841_EVENT_A) 4224 tmp = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_A | 4225 LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_A_MASK; 4226 else 4227 tmp = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_B | 4228 LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_B_MASK; 4229 return phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_EN, tmp); 4230 } 4231 4232 static int lan8841_ptp_enable_event(struct kszphy_ptp_priv *ptp_priv, int pin, 4233 u8 event, int pulse_width) 4234 { 4235 struct phy_device *phydev = ptp_priv->phydev; 4236 u16 tmp; 4237 int ret; 4238 4239 /* Enable the event */ 4240 if (event == LAN8841_EVENT_A) 4241 ret = phy_modify_mmd(phydev, 2, LAN8841_PTP_GENERAL_CONFIG, 4242 LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_A | 4243 LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_A_MASK, 4244 LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_A | 4245 pulse_width << LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_A); 4246 else 4247 ret = phy_modify_mmd(phydev, 2, LAN8841_PTP_GENERAL_CONFIG, 4248 LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_B | 4249 LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_B_MASK, 4250 LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_B | 4251 pulse_width << LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_B); 4252 if (ret) 4253 return ret; 4254 4255 /* Now connect the pin to the event. GPIO_DATA_SEL1 contains the GPIO 4256 * pins 0-4 while GPIO_DATA_SEL2 contains GPIO pins 5-9, therefore 4257 * depending on the pin, it requires to read a different register 4258 */ 4259 if (event == LAN8841_EVENT_A) 4260 tmp = LAN8841_GPIO_DATA_SEL_GPIO_DATA_SEL_EVENT_A; 4261 else 4262 tmp = LAN8841_GPIO_DATA_SEL_GPIO_DATA_SEL_EVENT_B; 4263 4264 if (pin < 5) 4265 ret = phy_set_bits_mmd(phydev, 2, LAN8841_GPIO_DATA_SEL1, 4266 tmp << (3 * pin)); 4267 else 4268 ret = phy_set_bits_mmd(phydev, 2, LAN8841_GPIO_DATA_SEL2, 4269 tmp << (3 * (pin - 5))); 4270 4271 return ret; 4272 } 4273 4274 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_200MS 13 4275 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100MS 12 4276 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_50MS 11 4277 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_10MS 10 4278 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_5MS 9 4279 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_1MS 8 4280 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_500US 7 4281 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100US 6 4282 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_50US 5 4283 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_10US 4 4284 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_5US 3 4285 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_1US 2 4286 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_500NS 1 4287 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100NS 0 4288 4289 static int lan8841_ptp_perout(struct ptp_clock_info *ptp, 4290 struct ptp_clock_request *rq, int on) 4291 { 4292 struct kszphy_ptp_priv *ptp_priv = container_of(ptp, struct kszphy_ptp_priv, 4293 ptp_clock_info); 4294 struct phy_device *phydev = ptp_priv->phydev; 4295 struct timespec64 ts_on, ts_period; 4296 s64 on_nsec, period_nsec; 4297 int pulse_width; 4298 int pin; 4299 int ret; 4300 4301 if (rq->perout.flags & ~PTP_PEROUT_DUTY_CYCLE) 4302 return -EOPNOTSUPP; 4303 4304 pin = ptp_find_pin(ptp_priv->ptp_clock, PTP_PF_PEROUT, rq->perout.index); 4305 if (pin == -1 || pin >= LAN8841_PTP_GPIO_NUM) 4306 return -EINVAL; 4307 4308 if (!on) { 4309 ret = lan8841_ptp_perout_off(ptp_priv, pin); 4310 if (ret) 4311 return ret; 4312 4313 return lan8841_ptp_remove_event(ptp_priv, LAN8841_EVENT_A, pin); 4314 } 4315 4316 ts_on.tv_sec = rq->perout.on.sec; 4317 ts_on.tv_nsec = rq->perout.on.nsec; 4318 on_nsec = timespec64_to_ns(&ts_on); 4319 4320 ts_period.tv_sec = rq->perout.period.sec; 4321 ts_period.tv_nsec = rq->perout.period.nsec; 4322 period_nsec = timespec64_to_ns(&ts_period); 4323 4324 if (period_nsec < 200) { 4325 pr_warn_ratelimited("%s: perout period too small, minimum is 200 nsec\n", 4326 phydev_name(phydev)); 4327 return -EOPNOTSUPP; 4328 } 4329 4330 if (on_nsec >= period_nsec) { 4331 pr_warn_ratelimited("%s: pulse width must be smaller than period\n", 4332 phydev_name(phydev)); 4333 return -EINVAL; 4334 } 4335 4336 switch (on_nsec) { 4337 case 200000000: 4338 pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_200MS; 4339 break; 4340 case 100000000: 4341 pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100MS; 4342 break; 4343 case 50000000: 4344 pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_50MS; 4345 break; 4346 case 10000000: 4347 pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_10MS; 4348 break; 4349 case 5000000: 4350 pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_5MS; 4351 break; 4352 case 1000000: 4353 pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_1MS; 4354 break; 4355 case 500000: 4356 pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_500US; 4357 break; 4358 case 100000: 4359 pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100US; 4360 break; 4361 case 50000: 4362 pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_50US; 4363 break; 4364 case 10000: 4365 pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_10US; 4366 break; 4367 case 5000: 4368 pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_5US; 4369 break; 4370 case 1000: 4371 pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_1US; 4372 break; 4373 case 500: 4374 pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_500NS; 4375 break; 4376 case 100: 4377 pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100NS; 4378 break; 4379 default: 4380 pr_warn_ratelimited("%s: Use default duty cycle of 100ns\n", 4381 phydev_name(phydev)); 4382 pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100NS; 4383 break; 4384 } 4385 4386 mutex_lock(&ptp_priv->ptp_lock); 4387 ret = lan8841_ptp_set_target(ptp_priv, LAN8841_EVENT_A, rq->perout.start.sec, 4388 rq->perout.start.nsec); 4389 mutex_unlock(&ptp_priv->ptp_lock); 4390 if (ret) 4391 return ret; 4392 4393 ret = lan8841_ptp_set_reload(ptp_priv, LAN8841_EVENT_A, rq->perout.period.sec, 4394 rq->perout.period.nsec); 4395 if (ret) 4396 return ret; 4397 4398 ret = lan8841_ptp_enable_event(ptp_priv, pin, LAN8841_EVENT_A, 4399 pulse_width); 4400 if (ret) 4401 return ret; 4402 4403 ret = lan8841_ptp_perout_on(ptp_priv, pin); 4404 if (ret) 4405 lan8841_ptp_remove_event(ptp_priv, pin, LAN8841_EVENT_A); 4406 4407 return ret; 4408 } 4409 4410 #define LAN8841_PTP_GPIO_CAP_EN 496 4411 #define LAN8841_PTP_GPIO_CAP_EN_GPIO_RE_CAPTURE_ENABLE(gpio) (BIT(gpio)) 4412 #define LAN8841_PTP_GPIO_CAP_EN_GPIO_FE_CAPTURE_ENABLE(gpio) (BIT(gpio) << 8) 4413 #define LAN8841_PTP_INT_EN_PTP_GPIO_CAP_EN BIT(2) 4414 4415 static int lan8841_ptp_extts_on(struct kszphy_ptp_priv *ptp_priv, int pin, 4416 u32 flags) 4417 { 4418 struct phy_device *phydev = ptp_priv->phydev; 4419 u16 tmp = 0; 4420 int ret; 4421 4422 /* Set GPIO to be intput */ 4423 ret = phy_set_bits_mmd(phydev, 2, LAN8841_GPIO_EN, BIT(pin)); 4424 if (ret) 4425 return ret; 4426 4427 ret = phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_BUF, BIT(pin)); 4428 if (ret) 4429 return ret; 4430 4431 /* Enable capture on the edges of the pin */ 4432 if (flags & PTP_RISING_EDGE) 4433 tmp |= LAN8841_PTP_GPIO_CAP_EN_GPIO_RE_CAPTURE_ENABLE(pin); 4434 if (flags & PTP_FALLING_EDGE) 4435 tmp |= LAN8841_PTP_GPIO_CAP_EN_GPIO_FE_CAPTURE_ENABLE(pin); 4436 ret = phy_write_mmd(phydev, 2, LAN8841_PTP_GPIO_CAP_EN, tmp); 4437 if (ret) 4438 return ret; 4439 4440 /* Enable interrupt */ 4441 return phy_modify_mmd(phydev, 2, LAN8841_PTP_INT_EN, 4442 LAN8841_PTP_INT_EN_PTP_GPIO_CAP_EN, 4443 LAN8841_PTP_INT_EN_PTP_GPIO_CAP_EN); 4444 } 4445 4446 static int lan8841_ptp_extts_off(struct kszphy_ptp_priv *ptp_priv, int pin) 4447 { 4448 struct phy_device *phydev = ptp_priv->phydev; 4449 int ret; 4450 4451 /* Set GPIO to be output */ 4452 ret = phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_EN, BIT(pin)); 4453 if (ret) 4454 return ret; 4455 4456 ret = phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_BUF, BIT(pin)); 4457 if (ret) 4458 return ret; 4459 4460 /* Disable capture on both of the edges */ 4461 ret = phy_modify_mmd(phydev, 2, LAN8841_PTP_GPIO_CAP_EN, 4462 LAN8841_PTP_GPIO_CAP_EN_GPIO_RE_CAPTURE_ENABLE(pin) | 4463 LAN8841_PTP_GPIO_CAP_EN_GPIO_FE_CAPTURE_ENABLE(pin), 4464 0); 4465 if (ret) 4466 return ret; 4467 4468 /* Disable interrupt */ 4469 return phy_modify_mmd(phydev, 2, LAN8841_PTP_INT_EN, 4470 LAN8841_PTP_INT_EN_PTP_GPIO_CAP_EN, 4471 0); 4472 } 4473 4474 static int lan8841_ptp_extts(struct ptp_clock_info *ptp, 4475 struct ptp_clock_request *rq, int on) 4476 { 4477 struct kszphy_ptp_priv *ptp_priv = container_of(ptp, struct kszphy_ptp_priv, 4478 ptp_clock_info); 4479 int pin; 4480 int ret; 4481 4482 /* Reject requests with unsupported flags */ 4483 if (rq->extts.flags & ~(PTP_ENABLE_FEATURE | 4484 PTP_EXTTS_EDGES | 4485 PTP_STRICT_FLAGS)) 4486 return -EOPNOTSUPP; 4487 4488 pin = ptp_find_pin(ptp_priv->ptp_clock, PTP_PF_EXTTS, rq->extts.index); 4489 if (pin == -1 || pin >= LAN8841_PTP_GPIO_NUM) 4490 return -EINVAL; 4491 4492 mutex_lock(&ptp_priv->ptp_lock); 4493 if (on) 4494 ret = lan8841_ptp_extts_on(ptp_priv, pin, rq->extts.flags); 4495 else 4496 ret = lan8841_ptp_extts_off(ptp_priv, pin); 4497 mutex_unlock(&ptp_priv->ptp_lock); 4498 4499 return ret; 4500 } 4501 4502 static int lan8841_ptp_enable(struct ptp_clock_info *ptp, 4503 struct ptp_clock_request *rq, int on) 4504 { 4505 switch (rq->type) { 4506 case PTP_CLK_REQ_EXTTS: 4507 return lan8841_ptp_extts(ptp, rq, on); 4508 case PTP_CLK_REQ_PEROUT: 4509 return lan8841_ptp_perout(ptp, rq, on); 4510 default: 4511 return -EOPNOTSUPP; 4512 } 4513 4514 return 0; 4515 } 4516 4517 static long lan8841_ptp_do_aux_work(struct ptp_clock_info *ptp) 4518 { 4519 struct kszphy_ptp_priv *ptp_priv = container_of(ptp, struct kszphy_ptp_priv, 4520 ptp_clock_info); 4521 struct timespec64 ts; 4522 unsigned long flags; 4523 4524 lan8841_ptp_getseconds(&ptp_priv->ptp_clock_info, &ts); 4525 4526 spin_lock_irqsave(&ptp_priv->seconds_lock, flags); 4527 ptp_priv->seconds = ts.tv_sec; 4528 spin_unlock_irqrestore(&ptp_priv->seconds_lock, flags); 4529 4530 return nsecs_to_jiffies(LAN8841_GET_SEC_LTC_DELAY); 4531 } 4532 4533 static struct ptp_clock_info lan8841_ptp_clock_info = { 4534 .owner = THIS_MODULE, 4535 .name = "lan8841 ptp", 4536 .max_adj = 31249999, 4537 .gettime64 = lan8841_ptp_gettime64, 4538 .settime64 = lan8841_ptp_settime64, 4539 .adjtime = lan8841_ptp_adjtime, 4540 .adjfine = lan8841_ptp_adjfine, 4541 .verify = lan8841_ptp_verify, 4542 .enable = lan8841_ptp_enable, 4543 .do_aux_work = lan8841_ptp_do_aux_work, 4544 .n_per_out = LAN8841_PTP_GPIO_NUM, 4545 .n_ext_ts = LAN8841_PTP_GPIO_NUM, 4546 .n_pins = LAN8841_PTP_GPIO_NUM, 4547 }; 4548 4549 #define LAN8841_OPERATION_MODE_STRAP_LOW_REGISTER 3 4550 #define LAN8841_OPERATION_MODE_STRAP_LOW_REGISTER_STRAP_RGMII_EN BIT(0) 4551 4552 static int lan8841_probe(struct phy_device *phydev) 4553 { 4554 struct kszphy_ptp_priv *ptp_priv; 4555 struct kszphy_priv *priv; 4556 int err; 4557 4558 err = kszphy_probe(phydev); 4559 if (err) 4560 return err; 4561 4562 if (phy_read_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG, 4563 LAN8841_OPERATION_MODE_STRAP_LOW_REGISTER) & 4564 LAN8841_OPERATION_MODE_STRAP_LOW_REGISTER_STRAP_RGMII_EN) 4565 phydev->interface = PHY_INTERFACE_MODE_RGMII_RXID; 4566 4567 /* Register the clock */ 4568 if (!IS_ENABLED(CONFIG_NETWORK_PHY_TIMESTAMPING)) 4569 return 0; 4570 4571 priv = phydev->priv; 4572 ptp_priv = &priv->ptp_priv; 4573 4574 ptp_priv->pin_config = devm_kcalloc(&phydev->mdio.dev, 4575 LAN8841_PTP_GPIO_NUM, 4576 sizeof(*ptp_priv->pin_config), 4577 GFP_KERNEL); 4578 if (!ptp_priv->pin_config) 4579 return -ENOMEM; 4580 4581 for (int i = 0; i < LAN8841_PTP_GPIO_NUM; ++i) { 4582 struct ptp_pin_desc *p = &ptp_priv->pin_config[i]; 4583 4584 snprintf(p->name, sizeof(p->name), "pin%d", i); 4585 p->index = i; 4586 p->func = PTP_PF_NONE; 4587 } 4588 4589 ptp_priv->ptp_clock_info = lan8841_ptp_clock_info; 4590 ptp_priv->ptp_clock_info.pin_config = ptp_priv->pin_config; 4591 ptp_priv->ptp_clock = ptp_clock_register(&ptp_priv->ptp_clock_info, 4592 &phydev->mdio.dev); 4593 if (IS_ERR(ptp_priv->ptp_clock)) { 4594 phydev_err(phydev, "ptp_clock_register failed: %lu\n", 4595 PTR_ERR(ptp_priv->ptp_clock)); 4596 return -EINVAL; 4597 } 4598 4599 if (!ptp_priv->ptp_clock) 4600 return 0; 4601 4602 /* Initialize the SW */ 4603 skb_queue_head_init(&ptp_priv->tx_queue); 4604 ptp_priv->phydev = phydev; 4605 mutex_init(&ptp_priv->ptp_lock); 4606 spin_lock_init(&ptp_priv->seconds_lock); 4607 4608 ptp_priv->mii_ts.rxtstamp = lan8841_rxtstamp; 4609 ptp_priv->mii_ts.txtstamp = lan8814_txtstamp; 4610 ptp_priv->mii_ts.hwtstamp = lan8841_hwtstamp; 4611 ptp_priv->mii_ts.ts_info = lan8841_ts_info; 4612 4613 phydev->mii_ts = &ptp_priv->mii_ts; 4614 4615 return 0; 4616 } 4617 4618 static int lan8841_suspend(struct phy_device *phydev) 4619 { 4620 struct kszphy_priv *priv = phydev->priv; 4621 struct kszphy_ptp_priv *ptp_priv = &priv->ptp_priv; 4622 4623 if (ptp_priv->ptp_clock) 4624 ptp_cancel_worker_sync(ptp_priv->ptp_clock); 4625 4626 return genphy_suspend(phydev); 4627 } 4628 4629 static struct phy_driver ksphy_driver[] = { 4630 { 4631 .phy_id = PHY_ID_KS8737, 4632 .phy_id_mask = MICREL_PHY_ID_MASK, 4633 .name = "Micrel KS8737", 4634 /* PHY_BASIC_FEATURES */ 4635 .driver_data = &ks8737_type, 4636 .probe = kszphy_probe, 4637 .config_init = kszphy_config_init, 4638 .config_intr = kszphy_config_intr, 4639 .handle_interrupt = kszphy_handle_interrupt, 4640 .suspend = kszphy_suspend, 4641 .resume = kszphy_resume, 4642 }, { 4643 .phy_id = PHY_ID_KSZ8021, 4644 .phy_id_mask = 0x00ffffff, 4645 .name = "Micrel KSZ8021 or KSZ8031", 4646 /* PHY_BASIC_FEATURES */ 4647 .driver_data = &ksz8021_type, 4648 .probe = kszphy_probe, 4649 .config_init = kszphy_config_init, 4650 .config_intr = kszphy_config_intr, 4651 .handle_interrupt = kszphy_handle_interrupt, 4652 .get_sset_count = kszphy_get_sset_count, 4653 .get_strings = kszphy_get_strings, 4654 .get_stats = kszphy_get_stats, 4655 .suspend = kszphy_suspend, 4656 .resume = kszphy_resume, 4657 }, { 4658 .phy_id = PHY_ID_KSZ8031, 4659 .phy_id_mask = 0x00ffffff, 4660 .name = "Micrel KSZ8031", 4661 /* PHY_BASIC_FEATURES */ 4662 .driver_data = &ksz8021_type, 4663 .probe = kszphy_probe, 4664 .config_init = kszphy_config_init, 4665 .config_intr = kszphy_config_intr, 4666 .handle_interrupt = kszphy_handle_interrupt, 4667 .get_sset_count = kszphy_get_sset_count, 4668 .get_strings = kszphy_get_strings, 4669 .get_stats = kszphy_get_stats, 4670 .suspend = kszphy_suspend, 4671 .resume = kszphy_resume, 4672 }, { 4673 .phy_id = PHY_ID_KSZ8041, 4674 .phy_id_mask = MICREL_PHY_ID_MASK, 4675 .name = "Micrel KSZ8041", 4676 /* PHY_BASIC_FEATURES */ 4677 .driver_data = &ksz8041_type, 4678 .probe = kszphy_probe, 4679 .config_init = ksz8041_config_init, 4680 .config_aneg = ksz8041_config_aneg, 4681 .config_intr = kszphy_config_intr, 4682 .handle_interrupt = kszphy_handle_interrupt, 4683 .get_sset_count = kszphy_get_sset_count, 4684 .get_strings = kszphy_get_strings, 4685 .get_stats = kszphy_get_stats, 4686 /* No suspend/resume callbacks because of errata DS80000700A, 4687 * receiver error following software power down. 4688 */ 4689 }, { 4690 .phy_id = PHY_ID_KSZ8041RNLI, 4691 .phy_id_mask = MICREL_PHY_ID_MASK, 4692 .name = "Micrel KSZ8041RNLI", 4693 /* PHY_BASIC_FEATURES */ 4694 .driver_data = &ksz8041_type, 4695 .probe = kszphy_probe, 4696 .config_init = kszphy_config_init, 4697 .config_intr = kszphy_config_intr, 4698 .handle_interrupt = kszphy_handle_interrupt, 4699 .get_sset_count = kszphy_get_sset_count, 4700 .get_strings = kszphy_get_strings, 4701 .get_stats = kszphy_get_stats, 4702 .suspend = kszphy_suspend, 4703 .resume = kszphy_resume, 4704 }, { 4705 .name = "Micrel KSZ8051", 4706 /* PHY_BASIC_FEATURES */ 4707 .driver_data = &ksz8051_type, 4708 .probe = kszphy_probe, 4709 .config_init = kszphy_config_init, 4710 .config_intr = kszphy_config_intr, 4711 .handle_interrupt = kszphy_handle_interrupt, 4712 .get_sset_count = kszphy_get_sset_count, 4713 .get_strings = kszphy_get_strings, 4714 .get_stats = kszphy_get_stats, 4715 .match_phy_device = ksz8051_match_phy_device, 4716 .suspend = kszphy_suspend, 4717 .resume = kszphy_resume, 4718 }, { 4719 .phy_id = PHY_ID_KSZ8001, 4720 .name = "Micrel KSZ8001 or KS8721", 4721 .phy_id_mask = 0x00fffffc, 4722 /* PHY_BASIC_FEATURES */ 4723 .driver_data = &ksz8041_type, 4724 .probe = kszphy_probe, 4725 .config_init = kszphy_config_init, 4726 .config_intr = kszphy_config_intr, 4727 .handle_interrupt = kszphy_handle_interrupt, 4728 .get_sset_count = kszphy_get_sset_count, 4729 .get_strings = kszphy_get_strings, 4730 .get_stats = kszphy_get_stats, 4731 .suspend = kszphy_suspend, 4732 .resume = kszphy_resume, 4733 }, { 4734 .phy_id = PHY_ID_KSZ8081, 4735 .name = "Micrel KSZ8081 or KSZ8091", 4736 .phy_id_mask = MICREL_PHY_ID_MASK, 4737 .flags = PHY_POLL_CABLE_TEST, 4738 /* PHY_BASIC_FEATURES */ 4739 .driver_data = &ksz8081_type, 4740 .probe = kszphy_probe, 4741 .config_init = ksz8081_config_init, 4742 .soft_reset = genphy_soft_reset, 4743 .config_aneg = ksz8081_config_aneg, 4744 .read_status = ksz8081_read_status, 4745 .config_intr = kszphy_config_intr, 4746 .handle_interrupt = kszphy_handle_interrupt, 4747 .get_sset_count = kszphy_get_sset_count, 4748 .get_strings = kszphy_get_strings, 4749 .get_stats = kszphy_get_stats, 4750 .suspend = kszphy_suspend, 4751 .resume = kszphy_resume, 4752 .cable_test_start = ksz886x_cable_test_start, 4753 .cable_test_get_status = ksz886x_cable_test_get_status, 4754 }, { 4755 .phy_id = PHY_ID_KSZ8061, 4756 .name = "Micrel KSZ8061", 4757 .phy_id_mask = MICREL_PHY_ID_MASK, 4758 /* PHY_BASIC_FEATURES */ 4759 .probe = kszphy_probe, 4760 .config_init = ksz8061_config_init, 4761 .soft_reset = genphy_soft_reset, 4762 .config_intr = kszphy_config_intr, 4763 .handle_interrupt = kszphy_handle_interrupt, 4764 .suspend = kszphy_suspend, 4765 .resume = kszphy_resume, 4766 }, { 4767 .phy_id = PHY_ID_KSZ9021, 4768 .phy_id_mask = 0x000ffffe, 4769 .name = "Micrel KSZ9021 Gigabit PHY", 4770 /* PHY_GBIT_FEATURES */ 4771 .driver_data = &ksz9021_type, 4772 .probe = kszphy_probe, 4773 .get_features = ksz9031_get_features, 4774 .config_init = ksz9021_config_init, 4775 .config_intr = kszphy_config_intr, 4776 .handle_interrupt = kszphy_handle_interrupt, 4777 .get_sset_count = kszphy_get_sset_count, 4778 .get_strings = kszphy_get_strings, 4779 .get_stats = kszphy_get_stats, 4780 .suspend = kszphy_suspend, 4781 .resume = kszphy_resume, 4782 .read_mmd = genphy_read_mmd_unsupported, 4783 .write_mmd = genphy_write_mmd_unsupported, 4784 }, { 4785 .phy_id = PHY_ID_KSZ9031, 4786 .phy_id_mask = MICREL_PHY_ID_MASK, 4787 .name = "Micrel KSZ9031 Gigabit PHY", 4788 .flags = PHY_POLL_CABLE_TEST, 4789 .driver_data = &ksz9021_type, 4790 .probe = kszphy_probe, 4791 .get_features = ksz9031_get_features, 4792 .config_init = ksz9031_config_init, 4793 .soft_reset = genphy_soft_reset, 4794 .read_status = ksz9031_read_status, 4795 .config_intr = kszphy_config_intr, 4796 .handle_interrupt = kszphy_handle_interrupt, 4797 .get_sset_count = kszphy_get_sset_count, 4798 .get_strings = kszphy_get_strings, 4799 .get_stats = kszphy_get_stats, 4800 .suspend = kszphy_suspend, 4801 .resume = kszphy_resume, 4802 .cable_test_start = ksz9x31_cable_test_start, 4803 .cable_test_get_status = ksz9x31_cable_test_get_status, 4804 }, { 4805 .phy_id = PHY_ID_LAN8814, 4806 .phy_id_mask = MICREL_PHY_ID_MASK, 4807 .name = "Microchip INDY Gigabit Quad PHY", 4808 .flags = PHY_POLL_CABLE_TEST, 4809 .config_init = lan8814_config_init, 4810 .driver_data = &lan8814_type, 4811 .probe = lan8814_probe, 4812 .soft_reset = genphy_soft_reset, 4813 .read_status = ksz9031_read_status, 4814 .get_sset_count = kszphy_get_sset_count, 4815 .get_strings = kszphy_get_strings, 4816 .get_stats = kszphy_get_stats, 4817 .suspend = genphy_suspend, 4818 .resume = kszphy_resume, 4819 .config_intr = lan8814_config_intr, 4820 .handle_interrupt = lan8814_handle_interrupt, 4821 .cable_test_start = lan8814_cable_test_start, 4822 .cable_test_get_status = ksz886x_cable_test_get_status, 4823 }, { 4824 .phy_id = PHY_ID_LAN8804, 4825 .phy_id_mask = MICREL_PHY_ID_MASK, 4826 .name = "Microchip LAN966X Gigabit PHY", 4827 .config_init = lan8804_config_init, 4828 .driver_data = &ksz9021_type, 4829 .probe = kszphy_probe, 4830 .soft_reset = genphy_soft_reset, 4831 .read_status = ksz9031_read_status, 4832 .get_sset_count = kszphy_get_sset_count, 4833 .get_strings = kszphy_get_strings, 4834 .get_stats = kszphy_get_stats, 4835 .suspend = genphy_suspend, 4836 .resume = kszphy_resume, 4837 .config_intr = lan8804_config_intr, 4838 .handle_interrupt = lan8804_handle_interrupt, 4839 }, { 4840 .phy_id = PHY_ID_LAN8841, 4841 .phy_id_mask = MICREL_PHY_ID_MASK, 4842 .name = "Microchip LAN8841 Gigabit PHY", 4843 .flags = PHY_POLL_CABLE_TEST, 4844 .driver_data = &lan8841_type, 4845 .config_init = lan8841_config_init, 4846 .probe = lan8841_probe, 4847 .soft_reset = genphy_soft_reset, 4848 .config_intr = lan8841_config_intr, 4849 .handle_interrupt = lan8841_handle_interrupt, 4850 .get_sset_count = kszphy_get_sset_count, 4851 .get_strings = kszphy_get_strings, 4852 .get_stats = kszphy_get_stats, 4853 .suspend = lan8841_suspend, 4854 .resume = genphy_resume, 4855 .cable_test_start = lan8814_cable_test_start, 4856 .cable_test_get_status = ksz886x_cable_test_get_status, 4857 }, { 4858 .phy_id = PHY_ID_KSZ9131, 4859 .phy_id_mask = MICREL_PHY_ID_MASK, 4860 .name = "Microchip KSZ9131 Gigabit PHY", 4861 /* PHY_GBIT_FEATURES */ 4862 .flags = PHY_POLL_CABLE_TEST, 4863 .driver_data = &ksz9131_type, 4864 .probe = kszphy_probe, 4865 .soft_reset = genphy_soft_reset, 4866 .config_init = ksz9131_config_init, 4867 .config_intr = kszphy_config_intr, 4868 .config_aneg = ksz9131_config_aneg, 4869 .read_status = ksz9131_read_status, 4870 .handle_interrupt = kszphy_handle_interrupt, 4871 .get_sset_count = kszphy_get_sset_count, 4872 .get_strings = kszphy_get_strings, 4873 .get_stats = kszphy_get_stats, 4874 .suspend = kszphy_suspend, 4875 .resume = kszphy_resume, 4876 .cable_test_start = ksz9x31_cable_test_start, 4877 .cable_test_get_status = ksz9x31_cable_test_get_status, 4878 .get_features = ksz9477_get_features, 4879 }, { 4880 .phy_id = PHY_ID_KSZ8873MLL, 4881 .phy_id_mask = MICREL_PHY_ID_MASK, 4882 .name = "Micrel KSZ8873MLL Switch", 4883 /* PHY_BASIC_FEATURES */ 4884 .config_init = kszphy_config_init, 4885 .config_aneg = ksz8873mll_config_aneg, 4886 .read_status = ksz8873mll_read_status, 4887 .suspend = genphy_suspend, 4888 .resume = genphy_resume, 4889 }, { 4890 .phy_id = PHY_ID_KSZ886X, 4891 .phy_id_mask = MICREL_PHY_ID_MASK, 4892 .name = "Micrel KSZ8851 Ethernet MAC or KSZ886X Switch", 4893 .driver_data = &ksz886x_type, 4894 /* PHY_BASIC_FEATURES */ 4895 .flags = PHY_POLL_CABLE_TEST, 4896 .config_init = kszphy_config_init, 4897 .config_aneg = ksz886x_config_aneg, 4898 .read_status = ksz886x_read_status, 4899 .suspend = genphy_suspend, 4900 .resume = genphy_resume, 4901 .cable_test_start = ksz886x_cable_test_start, 4902 .cable_test_get_status = ksz886x_cable_test_get_status, 4903 }, { 4904 .name = "Micrel KSZ87XX Switch", 4905 /* PHY_BASIC_FEATURES */ 4906 .config_init = kszphy_config_init, 4907 .match_phy_device = ksz8795_match_phy_device, 4908 .suspend = genphy_suspend, 4909 .resume = genphy_resume, 4910 }, { 4911 .phy_id = PHY_ID_KSZ9477, 4912 .phy_id_mask = MICREL_PHY_ID_MASK, 4913 .name = "Microchip KSZ9477", 4914 /* PHY_GBIT_FEATURES */ 4915 .config_init = ksz9477_config_init, 4916 .config_intr = kszphy_config_intr, 4917 .handle_interrupt = kszphy_handle_interrupt, 4918 .suspend = genphy_suspend, 4919 .resume = genphy_resume, 4920 .get_features = ksz9477_get_features, 4921 } }; 4922 4923 module_phy_driver(ksphy_driver); 4924 4925 MODULE_DESCRIPTION("Micrel PHY driver"); 4926 MODULE_AUTHOR("David J. Choi"); 4927 MODULE_LICENSE("GPL"); 4928 4929 static struct mdio_device_id __maybe_unused micrel_tbl[] = { 4930 { PHY_ID_KSZ9021, 0x000ffffe }, 4931 { PHY_ID_KSZ9031, MICREL_PHY_ID_MASK }, 4932 { PHY_ID_KSZ9131, MICREL_PHY_ID_MASK }, 4933 { PHY_ID_KSZ8001, 0x00fffffc }, 4934 { PHY_ID_KS8737, MICREL_PHY_ID_MASK }, 4935 { PHY_ID_KSZ8021, 0x00ffffff }, 4936 { PHY_ID_KSZ8031, 0x00ffffff }, 4937 { PHY_ID_KSZ8041, MICREL_PHY_ID_MASK }, 4938 { PHY_ID_KSZ8051, MICREL_PHY_ID_MASK }, 4939 { PHY_ID_KSZ8061, MICREL_PHY_ID_MASK }, 4940 { PHY_ID_KSZ8081, MICREL_PHY_ID_MASK }, 4941 { PHY_ID_KSZ8873MLL, MICREL_PHY_ID_MASK }, 4942 { PHY_ID_KSZ886X, MICREL_PHY_ID_MASK }, 4943 { PHY_ID_LAN8814, MICREL_PHY_ID_MASK }, 4944 { PHY_ID_LAN8804, MICREL_PHY_ID_MASK }, 4945 { PHY_ID_LAN8841, MICREL_PHY_ID_MASK }, 4946 { } 4947 }; 4948 4949 MODULE_DEVICE_TABLE(mdio, micrel_tbl); 4950