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