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