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