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 #include "phylib.h" 38 39 /* Operation Mode Strap Override */ 40 #define MII_KSZPHY_OMSO 0x16 41 #define KSZPHY_OMSO_FACTORY_TEST BIT(15) 42 #define KSZPHY_OMSO_B_CAST_OFF BIT(9) 43 #define KSZPHY_OMSO_NAND_TREE_ON BIT(5) 44 #define KSZPHY_OMSO_RMII_OVERRIDE BIT(1) 45 #define KSZPHY_OMSO_MII_OVERRIDE BIT(0) 46 47 /* general Interrupt control/status reg in vendor specific block. */ 48 #define MII_KSZPHY_INTCS 0x1B 49 #define KSZPHY_INTCS_JABBER BIT(15) 50 #define KSZPHY_INTCS_RECEIVE_ERR BIT(14) 51 #define KSZPHY_INTCS_PAGE_RECEIVE BIT(13) 52 #define KSZPHY_INTCS_PARELLEL BIT(12) 53 #define KSZPHY_INTCS_LINK_PARTNER_ACK BIT(11) 54 #define KSZPHY_INTCS_LINK_DOWN BIT(10) 55 #define KSZPHY_INTCS_REMOTE_FAULT BIT(9) 56 #define KSZPHY_INTCS_LINK_UP BIT(8) 57 #define KSZPHY_INTCS_ALL (KSZPHY_INTCS_LINK_UP |\ 58 KSZPHY_INTCS_LINK_DOWN) 59 #define KSZPHY_INTCS_LINK_DOWN_STATUS BIT(2) 60 #define KSZPHY_INTCS_LINK_UP_STATUS BIT(0) 61 #define KSZPHY_INTCS_STATUS (KSZPHY_INTCS_LINK_DOWN_STATUS |\ 62 KSZPHY_INTCS_LINK_UP_STATUS) 63 64 /* LinkMD Control/Status */ 65 #define KSZ8081_LMD 0x1d 66 #define KSZ8081_LMD_ENABLE_TEST BIT(15) 67 #define KSZ8081_LMD_STAT_NORMAL 0 68 #define KSZ8081_LMD_STAT_OPEN 1 69 #define KSZ8081_LMD_STAT_SHORT 2 70 #define KSZ8081_LMD_STAT_FAIL 3 71 #define KSZ8081_LMD_STAT_MASK GENMASK(14, 13) 72 /* Short cable (<10 meter) has been detected by LinkMD */ 73 #define KSZ8081_LMD_SHORT_INDICATOR BIT(12) 74 #define KSZ8081_LMD_DELTA_TIME_MASK GENMASK(8, 0) 75 76 #define KSZ9x31_LMD 0x12 77 #define KSZ9x31_LMD_VCT_EN BIT(15) 78 #define KSZ9x31_LMD_VCT_DIS_TX BIT(14) 79 #define KSZ9x31_LMD_VCT_PAIR(n) (((n) & 0x3) << 12) 80 #define KSZ9x31_LMD_VCT_SEL_RESULT 0 81 #define KSZ9x31_LMD_VCT_SEL_THRES_HI BIT(10) 82 #define KSZ9x31_LMD_VCT_SEL_THRES_LO BIT(11) 83 #define KSZ9x31_LMD_VCT_SEL_MASK GENMASK(11, 10) 84 #define KSZ9x31_LMD_VCT_ST_NORMAL 0 85 #define KSZ9x31_LMD_VCT_ST_OPEN 1 86 #define KSZ9x31_LMD_VCT_ST_SHORT 2 87 #define KSZ9x31_LMD_VCT_ST_FAIL 3 88 #define KSZ9x31_LMD_VCT_ST_MASK GENMASK(9, 8) 89 #define KSZ9x31_LMD_VCT_DATA_REFLECTED_INVALID BIT(7) 90 #define KSZ9x31_LMD_VCT_DATA_SIG_WAIT_TOO_LONG BIT(6) 91 #define KSZ9x31_LMD_VCT_DATA_MASK100 BIT(5) 92 #define KSZ9x31_LMD_VCT_DATA_NLP_FLP BIT(4) 93 #define KSZ9x31_LMD_VCT_DATA_LO_PULSE_MASK GENMASK(3, 2) 94 #define KSZ9x31_LMD_VCT_DATA_HI_PULSE_MASK GENMASK(1, 0) 95 #define KSZ9x31_LMD_VCT_DATA_MASK GENMASK(7, 0) 96 97 #define KSZPHY_WIRE_PAIR_MASK 0x3 98 99 #define LAN8814_CABLE_DIAG 0x12 100 #define LAN8814_CABLE_DIAG_STAT_MASK GENMASK(9, 8) 101 #define LAN8814_CABLE_DIAG_VCT_DATA_MASK GENMASK(7, 0) 102 #define LAN8814_PAIR_BIT_SHIFT 12 103 104 /* KSZ9x31 remote loopback register */ 105 #define KSZ9x31_REMOTE_LOOPBACK 0x11 106 /* This is an undocumented bit of the KSZ9131RNX. 107 * It was reported by NXP in cooperation with Micrel. 108 */ 109 #define KSZ9x31_REMOTE_LOOPBACK_KEEP_PREAMBLE BIT(2) 110 #define KSZ9x31_REMOTE_LOOPBACK_EN BIT(8) 111 112 #define LAN8814_SKUS 0xB 113 114 #define LAN8814_WIRE_PAIR_MASK 0xF 115 116 /* Lan8814 general Interrupt control/status reg in GPHY specific block. */ 117 #define LAN8814_INTC 0x18 118 #define LAN8814_INTS 0x1B 119 120 #define LAN8814_INT_FLF BIT(15) 121 #define LAN8814_INT_LINK_DOWN BIT(2) 122 #define LAN8814_INT_LINK_UP BIT(0) 123 #define LAN8814_INT_LINK (LAN8814_INT_LINK_UP |\ 124 LAN8814_INT_LINK_DOWN) 125 126 #define LAN8814_INTR_CTRL_REG 0x34 127 #define LAN8814_INTR_CTRL_REG_POLARITY BIT(1) 128 #define LAN8814_INTR_CTRL_REG_INTR_ENABLE BIT(0) 129 130 #define LAN8814_EEE_STATE 0x38 131 #define LAN8814_EEE_STATE_MASK2P5P BIT(10) 132 133 #define LAN8814_PD_CONTROLS 0x9d 134 #define LAN8814_PD_CONTROLS_PD_MEAS_TIME_MASK GENMASK(3, 0) 135 #define LAN8814_PD_CONTROLS_PD_MEAS_TIME_VAL 0xb 136 137 /* Represents 1ppm adjustment in 2^32 format with 138 * each nsec contains 4 clock cycles. 139 * The value is calculated as following: (1/1000000)/((2^-32)/4) 140 */ 141 #define LAN8814_1PPM_FORMAT 17179 142 143 /* Represents 1ppm adjustment in 2^32 format with 144 * each nsec contains 8 clock cycles. 145 * The value is calculated as following: (1/1000000)/((2^-32)/8) 146 */ 147 #define LAN8841_1PPM_FORMAT 34360 148 149 #define PTP_RX_VERSION 0x0248 150 #define PTP_TX_VERSION 0x0288 151 #define PTP_MAX_VERSION(x) (((x) & GENMASK(7, 0)) << 8) 152 #define PTP_MIN_VERSION(x) ((x) & GENMASK(7, 0)) 153 154 #define PTP_RX_MOD 0x024F 155 #define PTP_RX_MOD_BAD_UDPV4_CHKSUM_FORCE_FCS_DIS_ BIT(3) 156 #define PTP_RX_TIMESTAMP_EN 0x024D 157 #define PTP_TX_TIMESTAMP_EN 0x028D 158 159 #define PTP_TIMESTAMP_EN_SYNC_ BIT(0) 160 #define PTP_TIMESTAMP_EN_DREQ_ BIT(1) 161 #define PTP_TIMESTAMP_EN_PDREQ_ BIT(2) 162 #define PTP_TIMESTAMP_EN_PDRES_ BIT(3) 163 164 #define PTP_TX_PARSE_L2_ADDR_EN 0x0284 165 #define PTP_RX_PARSE_L2_ADDR_EN 0x0244 166 167 #define PTP_TX_PARSE_IP_ADDR_EN 0x0285 168 #define PTP_RX_PARSE_IP_ADDR_EN 0x0245 169 #define LTC_HARD_RESET 0x023F 170 #define LTC_HARD_RESET_ BIT(0) 171 172 #define TSU_HARD_RESET 0x02C1 173 #define TSU_HARD_RESET_ BIT(0) 174 175 #define PTP_CMD_CTL 0x0200 176 #define PTP_CMD_CTL_PTP_DISABLE_ BIT(0) 177 #define PTP_CMD_CTL_PTP_ENABLE_ BIT(1) 178 #define PTP_CMD_CTL_PTP_CLOCK_READ_ BIT(3) 179 #define PTP_CMD_CTL_PTP_CLOCK_LOAD_ BIT(4) 180 #define PTP_CMD_CTL_PTP_LTC_STEP_SEC_ BIT(5) 181 #define PTP_CMD_CTL_PTP_LTC_STEP_NSEC_ BIT(6) 182 183 #define PTP_COMMON_INT_ENA 0x0204 184 #define PTP_COMMON_INT_ENA_GPIO_CAP_EN BIT(2) 185 186 #define PTP_CLOCK_SET_SEC_HI 0x0205 187 #define PTP_CLOCK_SET_SEC_MID 0x0206 188 #define PTP_CLOCK_SET_SEC_LO 0x0207 189 #define PTP_CLOCK_SET_NS_HI 0x0208 190 #define PTP_CLOCK_SET_NS_LO 0x0209 191 192 #define PTP_CLOCK_READ_SEC_HI 0x0229 193 #define PTP_CLOCK_READ_SEC_MID 0x022A 194 #define PTP_CLOCK_READ_SEC_LO 0x022B 195 #define PTP_CLOCK_READ_NS_HI 0x022C 196 #define PTP_CLOCK_READ_NS_LO 0x022D 197 198 #define PTP_GPIO_SEL 0x0230 199 #define PTP_GPIO_SEL_GPIO_SEL(pin) ((pin) << 8) 200 #define PTP_GPIO_CAP_MAP_LO 0x0232 201 202 #define PTP_GPIO_CAP_EN 0x0233 203 #define PTP_GPIO_CAP_EN_GPIO_RE_CAPTURE_ENABLE(gpio) BIT(gpio) 204 #define PTP_GPIO_CAP_EN_GPIO_FE_CAPTURE_ENABLE(gpio) (BIT(gpio) << 8) 205 206 #define PTP_GPIO_RE_LTC_SEC_HI_CAP 0x0235 207 #define PTP_GPIO_RE_LTC_SEC_LO_CAP 0x0236 208 #define PTP_GPIO_RE_LTC_NS_HI_CAP 0x0237 209 #define PTP_GPIO_RE_LTC_NS_LO_CAP 0x0238 210 #define PTP_GPIO_FE_LTC_SEC_HI_CAP 0x0239 211 #define PTP_GPIO_FE_LTC_SEC_LO_CAP 0x023A 212 #define PTP_GPIO_FE_LTC_NS_HI_CAP 0x023B 213 #define PTP_GPIO_FE_LTC_NS_LO_CAP 0x023C 214 215 #define PTP_GPIO_CAP_STS 0x023D 216 #define PTP_GPIO_CAP_STS_PTP_GPIO_RE_STS(gpio) BIT(gpio) 217 #define PTP_GPIO_CAP_STS_PTP_GPIO_FE_STS(gpio) (BIT(gpio) << 8) 218 219 #define PTP_OPERATING_MODE 0x0241 220 #define PTP_OPERATING_MODE_STANDALONE_ BIT(0) 221 222 #define PTP_TX_MOD 0x028F 223 #define PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_ BIT(12) 224 #define PTP_TX_MOD_BAD_UDPV4_CHKSUM_FORCE_FCS_DIS_ BIT(3) 225 226 #define PTP_RX_PARSE_CONFIG 0x0242 227 #define PTP_RX_PARSE_CONFIG_LAYER2_EN_ BIT(0) 228 #define PTP_RX_PARSE_CONFIG_IPV4_EN_ BIT(1) 229 #define PTP_RX_PARSE_CONFIG_IPV6_EN_ BIT(2) 230 231 #define PTP_TX_PARSE_CONFIG 0x0282 232 #define PTP_TX_PARSE_CONFIG_LAYER2_EN_ BIT(0) 233 #define PTP_TX_PARSE_CONFIG_IPV4_EN_ BIT(1) 234 #define PTP_TX_PARSE_CONFIG_IPV6_EN_ BIT(2) 235 236 #define PTP_CLOCK_RATE_ADJ_HI 0x020C 237 #define PTP_CLOCK_RATE_ADJ_LO 0x020D 238 #define PTP_CLOCK_RATE_ADJ_DIR_ BIT(15) 239 240 #define PTP_LTC_STEP_ADJ_HI 0x0212 241 #define PTP_LTC_STEP_ADJ_LO 0x0213 242 #define PTP_LTC_STEP_ADJ_DIR_ BIT(15) 243 244 #define LAN8814_INTR_STS_REG 0x0033 245 #define LAN8814_INTR_STS_REG_1588_TSU0_ BIT(0) 246 #define LAN8814_INTR_STS_REG_1588_TSU1_ BIT(1) 247 #define LAN8814_INTR_STS_REG_1588_TSU2_ BIT(2) 248 #define LAN8814_INTR_STS_REG_1588_TSU3_ BIT(3) 249 250 #define PTP_CAP_INFO 0x022A 251 #define PTP_CAP_INFO_TX_TS_CNT_GET_(reg_val) (((reg_val) & 0x0f00) >> 8) 252 #define PTP_CAP_INFO_RX_TS_CNT_GET_(reg_val) ((reg_val) & 0x000f) 253 254 #define PTP_TX_EGRESS_SEC_HI 0x0296 255 #define PTP_TX_EGRESS_SEC_LO 0x0297 256 #define PTP_TX_EGRESS_NS_HI 0x0294 257 #define PTP_TX_EGRESS_NS_LO 0x0295 258 #define PTP_TX_MSG_HEADER2 0x0299 259 260 #define PTP_RX_INGRESS_SEC_HI 0x0256 261 #define PTP_RX_INGRESS_SEC_LO 0x0257 262 #define PTP_RX_INGRESS_NS_HI 0x0254 263 #define PTP_RX_INGRESS_NS_LO 0x0255 264 #define PTP_RX_MSG_HEADER2 0x0259 265 266 #define PTP_TSU_INT_EN 0x0200 267 #define PTP_TSU_INT_EN_PTP_TX_TS_OVRFL_EN_ BIT(3) 268 #define PTP_TSU_INT_EN_PTP_TX_TS_EN_ BIT(2) 269 #define PTP_TSU_INT_EN_PTP_RX_TS_OVRFL_EN_ BIT(1) 270 #define PTP_TSU_INT_EN_PTP_RX_TS_EN_ BIT(0) 271 272 #define PTP_TSU_INT_STS 0x0201 273 #define PTP_TSU_INT_STS_PTP_TX_TS_OVRFL_INT_ BIT(3) 274 #define PTP_TSU_INT_STS_PTP_TX_TS_EN_ BIT(2) 275 #define PTP_TSU_INT_STS_PTP_RX_TS_OVRFL_INT_ BIT(1) 276 #define PTP_TSU_INT_STS_PTP_RX_TS_EN_ BIT(0) 277 278 #define LAN8814_LED_CTRL_1 0x0 279 #define LAN8814_LED_CTRL_1_KSZ9031_LED_MODE_ BIT(6) 280 #define LAN8814_LED_CTRL_2 0x1 281 #define LAN8814_LED_CTRL_2_LED1_COM_DIS BIT(8) 282 283 /* PHY Control 1 */ 284 #define MII_KSZPHY_CTRL_1 0x1e 285 #define KSZ8081_CTRL1_MDIX_STAT BIT(4) 286 287 /* PHY Control 2 / PHY Control (if no PHY Control 1) */ 288 #define MII_KSZPHY_CTRL_2 0x1f 289 #define MII_KSZPHY_CTRL MII_KSZPHY_CTRL_2 290 291 /* Vendor-specific Clause 22 register, virtualized by KSZ87xx embedded PHYs DSA driver */ 292 #define MII_KSZ87XX_SHORT_CABLE 0x1a 293 #define MII_KSZ87XX_LPF_BW 0x1b 294 #define MII_KSZ87XX_EQ_INIT 0x1c 295 296 /* bitmap of PHY register to set interrupt mode */ 297 #define KSZ8081_CTRL2_HP_MDIX BIT(15) 298 #define KSZ8081_CTRL2_MDI_MDI_X_SELECT BIT(14) 299 #define KSZ8081_CTRL2_DISABLE_AUTO_MDIX BIT(13) 300 #define KSZ8081_CTRL2_FORCE_LINK BIT(11) 301 #define KSZ8081_CTRL2_POWER_SAVING BIT(10) 302 #define KSZPHY_CTRL_INT_ACTIVE_HIGH BIT(9) 303 #define KSZPHY_RMII_REF_CLK_SEL BIT(7) 304 305 /* Write/read to/from extended registers */ 306 #define MII_KSZPHY_EXTREG 0x0b 307 #define KSZPHY_EXTREG_WRITE 0x8000 308 309 #define MII_KSZPHY_EXTREG_WRITE 0x0c 310 #define MII_KSZPHY_EXTREG_READ 0x0d 311 312 /* Extended registers */ 313 #define MII_KSZPHY_CLK_CONTROL_PAD_SKEW 0x104 314 #define MII_KSZPHY_RX_DATA_PAD_SKEW 0x105 315 #define MII_KSZPHY_TX_DATA_PAD_SKEW 0x106 316 317 #define PS_TO_REG 200 318 #define FIFO_SIZE 8 319 320 #define LAN8814_PTP_GPIO_NUM 24 321 #define LAN8814_PTP_PEROUT_NUM 2 322 #define LAN8814_PTP_EXTTS_NUM 3 323 324 #define LAN8814_BUFFER_TIME 2 325 326 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_200MS 13 327 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100MS 12 328 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_50MS 11 329 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_10MS 10 330 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_5MS 9 331 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_1MS 8 332 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_500US 7 333 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100US 6 334 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_50US 5 335 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_10US 4 336 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_5US 3 337 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_1US 2 338 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_500NS 1 339 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100NS 0 340 341 #define LAN8814_GPIO_EN1 0x20 342 #define LAN8814_GPIO_EN2 0x21 343 #define LAN8814_GPIO_DIR1 0x22 344 #define LAN8814_GPIO_DIR2 0x23 345 #define LAN8814_GPIO_BUF1 0x24 346 #define LAN8814_GPIO_BUF2 0x25 347 348 #define LAN8814_GPIO_EN_ADDR(pin) \ 349 ((pin) > 15 ? LAN8814_GPIO_EN1 : LAN8814_GPIO_EN2) 350 #define LAN8814_GPIO_EN_BIT(pin) BIT(pin) 351 #define LAN8814_GPIO_DIR_ADDR(pin) \ 352 ((pin) > 15 ? LAN8814_GPIO_DIR1 : LAN8814_GPIO_DIR2) 353 #define LAN8814_GPIO_DIR_BIT(pin) BIT(pin) 354 #define LAN8814_GPIO_BUF_ADDR(pin) \ 355 ((pin) > 15 ? LAN8814_GPIO_BUF1 : LAN8814_GPIO_BUF2) 356 #define LAN8814_GPIO_BUF_BIT(pin) BIT(pin) 357 358 #define LAN8814_EVENT_A 0 359 #define LAN8814_EVENT_B 1 360 361 #define LAN8814_PTP_GENERAL_CONFIG 0x0201 362 #define LAN8814_PTP_GENERAL_CONFIG_LTC_EVENT_MASK(event) \ 363 ((event) ? GENMASK(11, 8) : GENMASK(7, 4)) 364 #define LAN8814_PTP_GENERAL_CONFIG_LTC_EVENT_SET(event, value) \ 365 (((value) & GENMASK(3, 0)) << (4 + ((event) << 2))) 366 #define LAN8814_PTP_GENERAL_CONFIG_RELOAD_ADD_X(event) \ 367 ((event) ? BIT(2) : BIT(0)) 368 #define LAN8814_PTP_GENERAL_CONFIG_POLARITY_X(event) \ 369 ((event) ? BIT(3) : BIT(1)) 370 371 #define LAN8814_PTP_CLOCK_TARGET_SEC_HI(event) ((event) ? 0x21F : 0x215) 372 #define LAN8814_PTP_CLOCK_TARGET_SEC_LO(event) ((event) ? 0x220 : 0x216) 373 #define LAN8814_PTP_CLOCK_TARGET_NS_HI(event) ((event) ? 0x221 : 0x217) 374 #define LAN8814_PTP_CLOCK_TARGET_NS_LO(event) ((event) ? 0x222 : 0x218) 375 376 #define LAN8814_PTP_CLOCK_TARGET_RELOAD_SEC_HI(event) ((event) ? 0x223 : 0x219) 377 #define LAN8814_PTP_CLOCK_TARGET_RELOAD_SEC_LO(event) ((event) ? 0x224 : 0x21A) 378 #define LAN8814_PTP_CLOCK_TARGET_RELOAD_NS_HI(event) ((event) ? 0x225 : 0x21B) 379 #define LAN8814_PTP_CLOCK_TARGET_RELOAD_NS_LO(event) ((event) ? 0x226 : 0x21C) 380 381 /* Delay used to get the second part from the LTC */ 382 #define LAN8841_GET_SEC_LTC_DELAY (500 * NSEC_PER_MSEC) 383 384 #define LAN8842_REV_8832 0x8832 385 386 #define LAN8814_REV_LAN8814 0x8814 387 #define LAN8814_REV_LAN8818 0x8818 388 389 struct kszphy_hw_stat { 390 const char *string; 391 u8 reg; 392 u8 bits; 393 }; 394 395 static struct kszphy_hw_stat kszphy_hw_stats[] = { 396 { "phy_receive_errors", 21, 16}, 397 { "phy_idle_errors", 10, 8 }, 398 }; 399 400 struct kszphy_type { 401 u32 led_mode_reg; 402 u16 interrupt_level_mask; 403 u16 cable_diag_reg; 404 unsigned long pair_mask; 405 u16 disable_dll_tx_bit; 406 u16 disable_dll_rx_bit; 407 u16 disable_dll_mask; 408 bool has_broadcast_disable; 409 bool has_nand_tree_disable; 410 bool has_rmii_ref_clk_sel; 411 }; 412 413 /* Shared structure between the PHYs of the same package. */ 414 struct lan8814_shared_priv { 415 struct phy_device *phydev; 416 struct ptp_clock *ptp_clock; 417 struct ptp_clock_info ptp_clock_info; 418 struct ptp_pin_desc *pin_config; 419 420 /* Lock for ptp_clock */ 421 struct mutex shared_lock; 422 }; 423 424 struct lan8814_ptp_rx_ts { 425 struct list_head list; 426 u32 seconds; 427 u32 nsec; 428 u16 seq_id; 429 }; 430 431 struct kszphy_ptp_priv { 432 struct mii_timestamper mii_ts; 433 struct phy_device *phydev; 434 435 struct sk_buff_head tx_queue; 436 struct sk_buff_head rx_queue; 437 438 struct list_head rx_ts_list; 439 /* Lock for Rx ts fifo */ 440 spinlock_t rx_ts_lock; 441 442 int hwts_tx_type; 443 enum hwtstamp_rx_filters rx_filter; 444 int layer; 445 int version; 446 447 struct ptp_clock *ptp_clock; 448 struct ptp_clock_info ptp_clock_info; 449 /* Lock for ptp_clock */ 450 struct mutex ptp_lock; 451 struct ptp_pin_desc *pin_config; 452 453 s64 seconds; 454 /* Lock for accessing seconds */ 455 spinlock_t seconds_lock; 456 }; 457 458 struct kszphy_phy_stats { 459 u64 rx_err_pkt_cnt; 460 }; 461 462 struct kszphy_priv { 463 struct kszphy_ptp_priv ptp_priv; 464 const struct kszphy_type *type; 465 struct clk *clk; 466 int led_mode; 467 u16 vct_ctrl1000; 468 bool rmii_ref_clk_sel; 469 bool rmii_ref_clk_sel_val; 470 bool clk_enable; 471 bool is_ptp_available; 472 u64 stats[ARRAY_SIZE(kszphy_hw_stats)]; 473 struct kszphy_phy_stats phy_stats; 474 }; 475 476 struct lan8842_phy_stats { 477 u64 rx_packets; 478 u64 rx_errors; 479 u64 tx_packets; 480 u64 tx_errors; 481 }; 482 483 struct lan8842_priv { 484 struct lan8842_phy_stats phy_stats; 485 struct kszphy_ptp_priv ptp_priv; 486 u16 rev; 487 }; 488 489 struct lanphy_reg_data { 490 int page; 491 u16 addr; 492 u16 val; 493 }; 494 495 static const struct kszphy_type lan8814_type = { 496 .led_mode_reg = ~LAN8814_LED_CTRL_1, 497 .cable_diag_reg = LAN8814_CABLE_DIAG, 498 .pair_mask = LAN8814_WIRE_PAIR_MASK, 499 }; 500 501 static const struct kszphy_type ksz886x_type = { 502 .cable_diag_reg = KSZ8081_LMD, 503 .pair_mask = KSZPHY_WIRE_PAIR_MASK, 504 }; 505 506 static const struct kszphy_type ksz8021_type = { 507 .led_mode_reg = MII_KSZPHY_CTRL_2, 508 .has_broadcast_disable = true, 509 .has_nand_tree_disable = true, 510 .has_rmii_ref_clk_sel = true, 511 }; 512 513 static const struct kszphy_type ksz8041_type = { 514 .led_mode_reg = MII_KSZPHY_CTRL_1, 515 }; 516 517 static const struct kszphy_type ksz8051_type = { 518 .led_mode_reg = MII_KSZPHY_CTRL_2, 519 .has_nand_tree_disable = true, 520 }; 521 522 static const struct kszphy_type ksz8081_type = { 523 .led_mode_reg = MII_KSZPHY_CTRL_2, 524 .cable_diag_reg = KSZ8081_LMD, 525 .pair_mask = KSZPHY_WIRE_PAIR_MASK, 526 .has_broadcast_disable = true, 527 .has_nand_tree_disable = true, 528 .has_rmii_ref_clk_sel = true, 529 }; 530 531 static const struct kszphy_type ks8737_type = { 532 .interrupt_level_mask = BIT(14), 533 }; 534 535 static const struct kszphy_type ksz9021_type = { 536 .interrupt_level_mask = BIT(14), 537 }; 538 539 static const struct kszphy_type ksz9131_type = { 540 .interrupt_level_mask = BIT(14), 541 .disable_dll_tx_bit = BIT(12), 542 .disable_dll_rx_bit = BIT(12), 543 .disable_dll_mask = BIT_MASK(12), 544 }; 545 546 static const struct kszphy_type lan8841_type = { 547 .disable_dll_tx_bit = BIT(14), 548 .disable_dll_rx_bit = BIT(14), 549 .disable_dll_mask = BIT_MASK(14), 550 .cable_diag_reg = LAN8814_CABLE_DIAG, 551 .pair_mask = LAN8814_WIRE_PAIR_MASK, 552 }; 553 554 static int kszphy_extended_write(struct phy_device *phydev, 555 u32 regnum, u16 val) 556 { 557 phy_write(phydev, MII_KSZPHY_EXTREG, KSZPHY_EXTREG_WRITE | regnum); 558 return phy_write(phydev, MII_KSZPHY_EXTREG_WRITE, val); 559 } 560 561 static int kszphy_extended_read(struct phy_device *phydev, 562 u32 regnum) 563 { 564 phy_write(phydev, MII_KSZPHY_EXTREG, regnum); 565 return phy_read(phydev, MII_KSZPHY_EXTREG_READ); 566 } 567 568 static int kszphy_ack_interrupt(struct phy_device *phydev) 569 { 570 /* bit[7..0] int status, which is a read and clear register. */ 571 int rc; 572 573 rc = phy_read(phydev, MII_KSZPHY_INTCS); 574 575 return (rc < 0) ? rc : 0; 576 } 577 578 static int kszphy_config_intr(struct phy_device *phydev) 579 { 580 const struct kszphy_type *type = phydev->drv->driver_data; 581 int temp, err; 582 u16 mask; 583 584 if (type && type->interrupt_level_mask) 585 mask = type->interrupt_level_mask; 586 else 587 mask = KSZPHY_CTRL_INT_ACTIVE_HIGH; 588 589 /* set the interrupt pin active low */ 590 temp = phy_read(phydev, MII_KSZPHY_CTRL); 591 if (temp < 0) 592 return temp; 593 temp &= ~mask; 594 phy_write(phydev, MII_KSZPHY_CTRL, temp); 595 596 /* enable / disable interrupts */ 597 if (phydev->interrupts == PHY_INTERRUPT_ENABLED) { 598 err = kszphy_ack_interrupt(phydev); 599 if (err) 600 return err; 601 602 err = phy_write(phydev, MII_KSZPHY_INTCS, KSZPHY_INTCS_ALL); 603 } else { 604 err = phy_write(phydev, MII_KSZPHY_INTCS, 0); 605 if (err) 606 return err; 607 608 err = kszphy_ack_interrupt(phydev); 609 } 610 611 return err; 612 } 613 614 static irqreturn_t kszphy_handle_interrupt(struct phy_device *phydev) 615 { 616 int irq_status; 617 618 irq_status = phy_read(phydev, MII_KSZPHY_INTCS); 619 if (irq_status < 0) { 620 phy_error(phydev); 621 return IRQ_NONE; 622 } 623 624 if (!(irq_status & KSZPHY_INTCS_STATUS)) 625 return IRQ_NONE; 626 627 phy_trigger_machine(phydev); 628 629 return IRQ_HANDLED; 630 } 631 632 static int kszphy_rmii_clk_sel(struct phy_device *phydev, bool val) 633 { 634 int ctrl; 635 636 ctrl = phy_read(phydev, MII_KSZPHY_CTRL); 637 if (ctrl < 0) 638 return ctrl; 639 640 if (val) 641 ctrl |= KSZPHY_RMII_REF_CLK_SEL; 642 else 643 ctrl &= ~KSZPHY_RMII_REF_CLK_SEL; 644 645 return phy_write(phydev, MII_KSZPHY_CTRL, ctrl); 646 } 647 648 static int kszphy_setup_led(struct phy_device *phydev, u32 reg, int val) 649 { 650 int rc, temp, shift; 651 652 switch (reg) { 653 case MII_KSZPHY_CTRL_1: 654 shift = 14; 655 break; 656 case MII_KSZPHY_CTRL_2: 657 shift = 4; 658 break; 659 default: 660 return -EINVAL; 661 } 662 663 temp = phy_read(phydev, reg); 664 if (temp < 0) { 665 rc = temp; 666 goto out; 667 } 668 669 temp &= ~(3 << shift); 670 temp |= val << shift; 671 rc = phy_write(phydev, reg, temp); 672 out: 673 if (rc < 0) 674 phydev_err(phydev, "failed to set led mode\n"); 675 676 return rc; 677 } 678 679 /* Disable PHY address 0 as the broadcast address, so that it can be used as a 680 * unique (non-broadcast) address on a shared bus. 681 */ 682 static int kszphy_broadcast_disable(struct phy_device *phydev) 683 { 684 int ret; 685 686 ret = phy_read(phydev, MII_KSZPHY_OMSO); 687 if (ret < 0) 688 goto out; 689 690 ret = phy_write(phydev, MII_KSZPHY_OMSO, ret | KSZPHY_OMSO_B_CAST_OFF); 691 out: 692 if (ret) 693 phydev_err(phydev, "failed to disable broadcast address\n"); 694 695 return ret; 696 } 697 698 static int kszphy_nand_tree_disable(struct phy_device *phydev) 699 { 700 int ret; 701 702 ret = phy_read(phydev, MII_KSZPHY_OMSO); 703 if (ret < 0) 704 goto out; 705 706 if (!(ret & KSZPHY_OMSO_NAND_TREE_ON)) 707 return 0; 708 709 ret = phy_write(phydev, MII_KSZPHY_OMSO, 710 ret & ~KSZPHY_OMSO_NAND_TREE_ON); 711 out: 712 if (ret) 713 phydev_err(phydev, "failed to disable NAND tree mode\n"); 714 715 return ret; 716 } 717 718 /* Some config bits need to be set again on resume, handle them here. */ 719 static int kszphy_config_reset(struct phy_device *phydev) 720 { 721 struct kszphy_priv *priv = phydev->priv; 722 int ret; 723 724 if (priv->rmii_ref_clk_sel) { 725 ret = kszphy_rmii_clk_sel(phydev, priv->rmii_ref_clk_sel_val); 726 if (ret) { 727 phydev_err(phydev, 728 "failed to set rmii reference clock\n"); 729 return ret; 730 } 731 } 732 733 if (priv->type && priv->led_mode >= 0) 734 kszphy_setup_led(phydev, priv->type->led_mode_reg, priv->led_mode); 735 736 return 0; 737 } 738 739 static int kszphy_config_init(struct phy_device *phydev) 740 { 741 struct kszphy_priv *priv = phydev->priv; 742 const struct kszphy_type *type; 743 744 if (!priv) 745 return 0; 746 747 type = priv->type; 748 749 if (type && type->has_broadcast_disable) 750 kszphy_broadcast_disable(phydev); 751 752 if (type && type->has_nand_tree_disable) 753 kszphy_nand_tree_disable(phydev); 754 755 return kszphy_config_reset(phydev); 756 } 757 758 static int ksz8041_fiber_mode(struct phy_device *phydev) 759 { 760 struct device_node *of_node = phydev->mdio.dev.of_node; 761 762 return of_property_read_bool(of_node, "micrel,fiber-mode"); 763 } 764 765 static int ksz8041_config_init(struct phy_device *phydev) 766 { 767 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, }; 768 769 /* Limit supported and advertised modes in fiber mode */ 770 if (ksz8041_fiber_mode(phydev)) { 771 phydev->dev_flags |= MICREL_PHY_FXEN; 772 linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, mask); 773 linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, mask); 774 775 linkmode_and(phydev->supported, phydev->supported, mask); 776 linkmode_set_bit(ETHTOOL_LINK_MODE_FIBRE_BIT, 777 phydev->supported); 778 linkmode_and(phydev->advertising, phydev->advertising, mask); 779 linkmode_set_bit(ETHTOOL_LINK_MODE_FIBRE_BIT, 780 phydev->advertising); 781 phydev->autoneg = AUTONEG_DISABLE; 782 } 783 784 return kszphy_config_init(phydev); 785 } 786 787 static int ksz8041_config_aneg(struct phy_device *phydev) 788 { 789 /* Skip auto-negotiation in fiber mode */ 790 if (phydev->dev_flags & MICREL_PHY_FXEN) { 791 phydev->speed = SPEED_100; 792 return 0; 793 } 794 795 return genphy_config_aneg(phydev); 796 } 797 798 static int ksz8051_ksz8795_match_phy_device(struct phy_device *phydev, 799 const bool ksz_8051) 800 { 801 int ret; 802 803 if (!phy_id_compare(phydev->phy_id, PHY_ID_KSZ8051, MICREL_PHY_ID_MASK)) 804 return 0; 805 806 ret = phy_read(phydev, MII_BMSR); 807 if (ret < 0) 808 return ret; 809 810 /* KSZ8051 PHY and KSZ8794/KSZ8795/KSZ8765 switch share the same 811 * exact PHY ID. However, they can be told apart by the extended 812 * capability registers presence. The KSZ8051 PHY has them while 813 * the switch does not. 814 */ 815 ret &= BMSR_ERCAP; 816 if (ksz_8051) 817 return ret; 818 else 819 return !ret; 820 } 821 822 static int ksz8051_match_phy_device(struct phy_device *phydev, 823 const struct phy_driver *phydrv) 824 { 825 return ksz8051_ksz8795_match_phy_device(phydev, true); 826 } 827 828 static int ksz8081_config_init(struct phy_device *phydev) 829 { 830 /* KSZPHY_OMSO_FACTORY_TEST is set at de-assertion of the reset line 831 * based on the RXER (KSZ8081RNA/RND) or TXC (KSZ8081MNX/RNB) pin. If a 832 * pull-down is missing, the factory test mode should be cleared by 833 * manually writing a 0. 834 */ 835 phy_clear_bits(phydev, MII_KSZPHY_OMSO, KSZPHY_OMSO_FACTORY_TEST); 836 837 return kszphy_config_init(phydev); 838 } 839 840 static int ksz8081_config_mdix(struct phy_device *phydev, u8 ctrl) 841 { 842 u16 val; 843 844 switch (ctrl) { 845 case ETH_TP_MDI: 846 val = KSZ8081_CTRL2_DISABLE_AUTO_MDIX; 847 break; 848 case ETH_TP_MDI_X: 849 val = KSZ8081_CTRL2_DISABLE_AUTO_MDIX | 850 KSZ8081_CTRL2_MDI_MDI_X_SELECT; 851 break; 852 case ETH_TP_MDI_AUTO: 853 val = 0; 854 break; 855 default: 856 return 0; 857 } 858 859 return phy_modify(phydev, MII_KSZPHY_CTRL_2, 860 KSZ8081_CTRL2_HP_MDIX | 861 KSZ8081_CTRL2_MDI_MDI_X_SELECT | 862 KSZ8081_CTRL2_DISABLE_AUTO_MDIX, 863 KSZ8081_CTRL2_HP_MDIX | val); 864 } 865 866 static int ksz8081_config_aneg(struct phy_device *phydev) 867 { 868 int ret; 869 870 ret = genphy_config_aneg(phydev); 871 if (ret) 872 return ret; 873 874 /* The MDI-X configuration is automatically changed by the PHY after 875 * switching from autoneg off to on. So, take MDI-X configuration under 876 * own control and set it after autoneg configuration was done. 877 */ 878 return ksz8081_config_mdix(phydev, phydev->mdix_ctrl); 879 } 880 881 static int ksz8081_mdix_update(struct phy_device *phydev) 882 { 883 int ret; 884 885 ret = phy_read(phydev, MII_KSZPHY_CTRL_2); 886 if (ret < 0) 887 return ret; 888 889 if (ret & KSZ8081_CTRL2_DISABLE_AUTO_MDIX) { 890 if (ret & KSZ8081_CTRL2_MDI_MDI_X_SELECT) 891 phydev->mdix_ctrl = ETH_TP_MDI_X; 892 else 893 phydev->mdix_ctrl = ETH_TP_MDI; 894 } else { 895 phydev->mdix_ctrl = ETH_TP_MDI_AUTO; 896 } 897 898 ret = phy_read(phydev, MII_KSZPHY_CTRL_1); 899 if (ret < 0) 900 return ret; 901 902 if (ret & KSZ8081_CTRL1_MDIX_STAT) 903 phydev->mdix = ETH_TP_MDI; 904 else 905 phydev->mdix = ETH_TP_MDI_X; 906 907 return 0; 908 } 909 910 static int ksz8081_read_status(struct phy_device *phydev) 911 { 912 int ret; 913 914 ret = ksz8081_mdix_update(phydev); 915 if (ret < 0) 916 return ret; 917 918 return genphy_read_status(phydev); 919 } 920 921 static int ksz8061_config_init(struct phy_device *phydev) 922 { 923 int ret; 924 925 /* Chip can be powered down by the bootstrap code. */ 926 ret = phy_read(phydev, MII_BMCR); 927 if (ret < 0) 928 return ret; 929 if (ret & BMCR_PDOWN) { 930 ret = phy_write(phydev, MII_BMCR, ret & ~BMCR_PDOWN); 931 if (ret < 0) 932 return ret; 933 usleep_range(1000, 2000); 934 } 935 936 ret = phy_write_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_DEVID1, 0xB61A); 937 if (ret) 938 return ret; 939 940 return kszphy_config_init(phydev); 941 } 942 943 static int ksz8795_match_phy_device(struct phy_device *phydev, 944 const struct phy_driver *phydrv) 945 { 946 return ksz8051_ksz8795_match_phy_device(phydev, false); 947 } 948 949 static int ksz8795_get_tunable(struct phy_device *phydev, 950 struct ethtool_tunable *tuna, void *data) 951 { 952 int ret; 953 954 switch (tuna->id) { 955 case ETHTOOL_PHY_SHORT_CABLE_PRESET: 956 ret = phy_read(phydev, MII_KSZ87XX_SHORT_CABLE); 957 if (ret < 0) 958 return ret; 959 *(u8 *)data = ret; 960 return 0; 961 case ETHTOOL_PHY_LPF_BW: 962 ret = phy_read(phydev, MII_KSZ87XX_LPF_BW); 963 if (ret < 0) 964 return ret; 965 *(u32 *)data = ret & 0xff; 966 return 0; 967 case ETHTOOL_PHY_DSP_EQ_INIT_VALUE: 968 ret = phy_read(phydev, MII_KSZ87XX_EQ_INIT); 969 if (ret < 0) 970 return ret; 971 *(u32 *)data = ret & 0xff; 972 return 0; 973 default: 974 return -EOPNOTSUPP; 975 } 976 } 977 978 static int ksz8795_set_tunable(struct phy_device *phydev, 979 struct ethtool_tunable *tuna, const void *data) 980 { 981 u32 val; 982 983 switch (tuna->id) { 984 case ETHTOOL_PHY_SHORT_CABLE_PRESET: 985 return phy_write(phydev, MII_KSZ87XX_SHORT_CABLE, 986 *(const u8 *)data); 987 case ETHTOOL_PHY_LPF_BW: 988 val = *(const u32 *)data; 989 if (val > 0xff) 990 return -EINVAL; 991 return phy_write(phydev, MII_KSZ87XX_LPF_BW, (u8)val); 992 case ETHTOOL_PHY_DSP_EQ_INIT_VALUE: 993 val = *(const u32 *)data; 994 if (val > 0xff) 995 return -EINVAL; 996 return phy_write(phydev, MII_KSZ87XX_EQ_INIT, (u8)val); 997 default: 998 return -EOPNOTSUPP; 999 } 1000 } 1001 1002 static int ksz9021_load_values_from_of(struct phy_device *phydev, 1003 const struct device_node *of_node, 1004 u16 reg, 1005 const char *field1, const char *field2, 1006 const char *field3, const char *field4) 1007 { 1008 int val1 = -1; 1009 int val2 = -2; 1010 int val3 = -3; 1011 int val4 = -4; 1012 int newval; 1013 int matches = 0; 1014 1015 if (!of_property_read_u32(of_node, field1, &val1)) 1016 matches++; 1017 1018 if (!of_property_read_u32(of_node, field2, &val2)) 1019 matches++; 1020 1021 if (!of_property_read_u32(of_node, field3, &val3)) 1022 matches++; 1023 1024 if (!of_property_read_u32(of_node, field4, &val4)) 1025 matches++; 1026 1027 if (!matches) 1028 return 0; 1029 1030 if (matches < 4) 1031 newval = kszphy_extended_read(phydev, reg); 1032 else 1033 newval = 0; 1034 1035 if (val1 != -1) 1036 newval = ((newval & 0xfff0) | ((val1 / PS_TO_REG) & 0xf) << 0); 1037 1038 if (val2 != -2) 1039 newval = ((newval & 0xff0f) | ((val2 / PS_TO_REG) & 0xf) << 4); 1040 1041 if (val3 != -3) 1042 newval = ((newval & 0xf0ff) | ((val3 / PS_TO_REG) & 0xf) << 8); 1043 1044 if (val4 != -4) 1045 newval = ((newval & 0x0fff) | ((val4 / PS_TO_REG) & 0xf) << 12); 1046 1047 return kszphy_extended_write(phydev, reg, newval); 1048 } 1049 1050 static int ksz9021_config_init(struct phy_device *phydev) 1051 { 1052 const struct device_node *of_node; 1053 const struct device *dev_walker; 1054 1055 /* The Micrel driver has a deprecated option to place phy OF 1056 * properties in the MAC node. Walk up the tree of devices to 1057 * find a device with an OF node. 1058 */ 1059 dev_walker = &phydev->mdio.dev; 1060 do { 1061 of_node = dev_walker->of_node; 1062 dev_walker = dev_walker->parent; 1063 1064 } while (!of_node && dev_walker); 1065 1066 if (of_node) { 1067 ksz9021_load_values_from_of(phydev, of_node, 1068 MII_KSZPHY_CLK_CONTROL_PAD_SKEW, 1069 "txen-skew-ps", "txc-skew-ps", 1070 "rxdv-skew-ps", "rxc-skew-ps"); 1071 ksz9021_load_values_from_of(phydev, of_node, 1072 MII_KSZPHY_RX_DATA_PAD_SKEW, 1073 "rxd0-skew-ps", "rxd1-skew-ps", 1074 "rxd2-skew-ps", "rxd3-skew-ps"); 1075 ksz9021_load_values_from_of(phydev, of_node, 1076 MII_KSZPHY_TX_DATA_PAD_SKEW, 1077 "txd0-skew-ps", "txd1-skew-ps", 1078 "txd2-skew-ps", "txd3-skew-ps"); 1079 } 1080 return 0; 1081 } 1082 1083 #define KSZ9031_PS_TO_REG 60 1084 1085 /* Extended registers */ 1086 /* MMD Address 0x0 */ 1087 #define MII_KSZ9031RN_FLP_BURST_TX_LO 3 1088 #define MII_KSZ9031RN_FLP_BURST_TX_HI 4 1089 1090 /* MMD Address 0x2 */ 1091 #define MII_KSZ9031RN_CONTROL_PAD_SKEW 4 1092 #define MII_KSZ9031RN_RX_CTL_M GENMASK(7, 4) 1093 #define MII_KSZ9031RN_TX_CTL_M GENMASK(3, 0) 1094 1095 #define MII_KSZ9031RN_RX_DATA_PAD_SKEW 5 1096 #define MII_KSZ9031RN_RXD3 GENMASK(15, 12) 1097 #define MII_KSZ9031RN_RXD2 GENMASK(11, 8) 1098 #define MII_KSZ9031RN_RXD1 GENMASK(7, 4) 1099 #define MII_KSZ9031RN_RXD0 GENMASK(3, 0) 1100 1101 #define MII_KSZ9031RN_TX_DATA_PAD_SKEW 6 1102 #define MII_KSZ9031RN_TXD3 GENMASK(15, 12) 1103 #define MII_KSZ9031RN_TXD2 GENMASK(11, 8) 1104 #define MII_KSZ9031RN_TXD1 GENMASK(7, 4) 1105 #define MII_KSZ9031RN_TXD0 GENMASK(3, 0) 1106 1107 #define MII_KSZ9031RN_CLK_PAD_SKEW 8 1108 #define MII_KSZ9031RN_GTX_CLK GENMASK(9, 5) 1109 #define MII_KSZ9031RN_RX_CLK GENMASK(4, 0) 1110 1111 /* KSZ9031 has internal RGMII_IDRX = 1.2ns and RGMII_IDTX = 0ns. To 1112 * provide different RGMII options we need to configure delay offset 1113 * for each pad relative to build in delay. 1114 */ 1115 /* keep rx as "No delay adjustment" and set rx_clk to +0.60ns to get delays of 1116 * 1.80ns 1117 */ 1118 #define RX_ID 0x7 1119 #define RX_CLK_ID 0x19 1120 1121 /* set rx to +0.30ns and rx_clk to -0.90ns to compensate the 1122 * internal 1.2ns delay. 1123 */ 1124 #define RX_ND 0xc 1125 #define RX_CLK_ND 0x0 1126 1127 /* set tx to -0.42ns and tx_clk to +0.96ns to get 1.38ns delay */ 1128 #define TX_ID 0x0 1129 #define TX_CLK_ID 0x1f 1130 1131 /* set tx and tx_clk to "No delay adjustment" to keep 0ns 1132 * delay 1133 */ 1134 #define TX_ND 0x7 1135 #define TX_CLK_ND 0xf 1136 1137 /* MMD Address 0x1C */ 1138 #define MII_KSZ9031RN_EDPD 0x23 1139 #define MII_KSZ9031RN_EDPD_ENABLE BIT(0) 1140 1141 static int ksz9031_set_loopback(struct phy_device *phydev, bool enable, 1142 int speed) 1143 { 1144 u16 ctl = BMCR_LOOPBACK; 1145 int val; 1146 1147 if (!enable) 1148 return genphy_loopback(phydev, enable, 0); 1149 1150 if (speed == SPEED_10 || speed == SPEED_100 || speed == SPEED_1000) 1151 phydev->speed = speed; 1152 else if (speed) 1153 return -EINVAL; 1154 phydev->duplex = DUPLEX_FULL; 1155 1156 ctl |= mii_bmcr_encode_fixed(phydev->speed, phydev->duplex); 1157 1158 phy_write(phydev, MII_BMCR, ctl); 1159 1160 return phy_read_poll_timeout(phydev, MII_BMSR, val, val & BMSR_LSTATUS, 1161 5000, 500000, true); 1162 } 1163 1164 /* KSZ9131-specific sequence to enable loopback, registers are undocumented 1165 * in the datasheet but mentionned in the local loopback mode configuration 1166 * steps. 1167 * 1168 * Without taking these steps, the PHY appears to disable its RXC while in 1169 * loopback mode, which may be needed by some MACs such as stmmac. 1170 */ 1171 static int ksz9131_loopback_enable(struct phy_device *phydev) 1172 { 1173 int ret; 1174 1175 ret = phy_write_mmd(phydev, 0x1c, 0x15, 0xeeee); 1176 if (ret) 1177 return ret; 1178 1179 ret = phy_write_mmd(phydev, 0x1c, 0x16, 0xeeee); 1180 if (ret) 1181 return ret; 1182 1183 ret = phy_write_mmd(phydev, 0x1c, 0x18, 0xeeee); 1184 if (ret) 1185 return ret; 1186 1187 return phy_write_mmd(phydev, 0x1c, 0x1b, 0xeeee); 1188 } 1189 1190 /* KSZ9131 datasheet doesn't state how to deal with the MMD 0x1c registers 1191 * when disabling loopback. 1192 * 1193 * Set them back to their measured initial state when disabling loopback, and 1194 * ignore errors while doing so. 1195 */ 1196 static void ksz9131_loopback_disable(struct phy_device *phydev) 1197 { 1198 phy_write_mmd(phydev, 0x1c, 0x15, 0x6eff); 1199 phy_write_mmd(phydev, 0x1c, 0x16, 0xe6ff); 1200 phy_write_mmd(phydev, 0x1c, 0x18, 0x43ff); 1201 phy_write_mmd(phydev, 0x1c, 0x1b, 0x07ff); 1202 } 1203 1204 static int ksz9131_set_loopback(struct phy_device *phydev, bool enable, 1205 int speed) 1206 { 1207 int ret; 1208 1209 if (enable) { 1210 ret = ksz9131_loopback_enable(phydev); 1211 if (ret) 1212 return ret; 1213 } 1214 1215 ret = ksz9031_set_loopback(phydev, enable, speed); 1216 1217 if (ret || !enable) 1218 ksz9131_loopback_disable(phydev); 1219 1220 return ret; 1221 } 1222 1223 static int ksz9031_of_load_skew_values(struct phy_device *phydev, 1224 const struct device_node *of_node, 1225 u16 reg, size_t field_sz, 1226 const char *field[], u8 numfields, 1227 bool *update) 1228 { 1229 int val[4] = {-1, -2, -3, -4}; 1230 int matches = 0; 1231 u16 mask; 1232 u16 maxval; 1233 u16 newval; 1234 int i; 1235 1236 for (i = 0; i < numfields; i++) 1237 if (!of_property_read_u32(of_node, field[i], val + i)) 1238 matches++; 1239 1240 if (!matches) 1241 return 0; 1242 1243 *update |= true; 1244 1245 if (matches < numfields) 1246 newval = phy_read_mmd(phydev, 2, reg); 1247 else 1248 newval = 0; 1249 1250 maxval = (field_sz == 4) ? 0xf : 0x1f; 1251 for (i = 0; i < numfields; i++) 1252 if (val[i] != -(i + 1)) { 1253 mask = 0xffff; 1254 mask ^= maxval << (field_sz * i); 1255 newval = (newval & mask) | 1256 (((val[i] / KSZ9031_PS_TO_REG) & maxval) 1257 << (field_sz * i)); 1258 } 1259 1260 return phy_write_mmd(phydev, 2, reg, newval); 1261 } 1262 1263 /* Center KSZ9031RNX FLP timing at 16ms. */ 1264 static int ksz9031_center_flp_timing(struct phy_device *phydev) 1265 { 1266 int result; 1267 1268 result = phy_write_mmd(phydev, 0, MII_KSZ9031RN_FLP_BURST_TX_HI, 1269 0x0006); 1270 if (result) 1271 return result; 1272 1273 result = phy_write_mmd(phydev, 0, MII_KSZ9031RN_FLP_BURST_TX_LO, 1274 0x1A80); 1275 if (result) 1276 return result; 1277 1278 return genphy_restart_aneg(phydev); 1279 } 1280 1281 /* Enable energy-detect power-down mode */ 1282 static int ksz9031_enable_edpd(struct phy_device *phydev) 1283 { 1284 int reg; 1285 1286 reg = phy_read_mmd(phydev, 0x1C, MII_KSZ9031RN_EDPD); 1287 if (reg < 0) 1288 return reg; 1289 return phy_write_mmd(phydev, 0x1C, MII_KSZ9031RN_EDPD, 1290 reg | MII_KSZ9031RN_EDPD_ENABLE); 1291 } 1292 1293 static int ksz9031_config_rgmii_delay(struct phy_device *phydev) 1294 { 1295 u16 rx, tx, rx_clk, tx_clk; 1296 int ret; 1297 1298 switch (phydev->interface) { 1299 case PHY_INTERFACE_MODE_RGMII: 1300 tx = TX_ND; 1301 tx_clk = TX_CLK_ND; 1302 rx = RX_ND; 1303 rx_clk = RX_CLK_ND; 1304 break; 1305 case PHY_INTERFACE_MODE_RGMII_ID: 1306 tx = TX_ID; 1307 tx_clk = TX_CLK_ID; 1308 rx = RX_ID; 1309 rx_clk = RX_CLK_ID; 1310 break; 1311 case PHY_INTERFACE_MODE_RGMII_RXID: 1312 tx = TX_ND; 1313 tx_clk = TX_CLK_ND; 1314 rx = RX_ID; 1315 rx_clk = RX_CLK_ID; 1316 break; 1317 case PHY_INTERFACE_MODE_RGMII_TXID: 1318 tx = TX_ID; 1319 tx_clk = TX_CLK_ID; 1320 rx = RX_ND; 1321 rx_clk = RX_CLK_ND; 1322 break; 1323 default: 1324 return 0; 1325 } 1326 1327 ret = phy_write_mmd(phydev, 2, MII_KSZ9031RN_CONTROL_PAD_SKEW, 1328 FIELD_PREP(MII_KSZ9031RN_RX_CTL_M, rx) | 1329 FIELD_PREP(MII_KSZ9031RN_TX_CTL_M, tx)); 1330 if (ret < 0) 1331 return ret; 1332 1333 ret = phy_write_mmd(phydev, 2, MII_KSZ9031RN_RX_DATA_PAD_SKEW, 1334 FIELD_PREP(MII_KSZ9031RN_RXD3, rx) | 1335 FIELD_PREP(MII_KSZ9031RN_RXD2, rx) | 1336 FIELD_PREP(MII_KSZ9031RN_RXD1, rx) | 1337 FIELD_PREP(MII_KSZ9031RN_RXD0, rx)); 1338 if (ret < 0) 1339 return ret; 1340 1341 ret = phy_write_mmd(phydev, 2, MII_KSZ9031RN_TX_DATA_PAD_SKEW, 1342 FIELD_PREP(MII_KSZ9031RN_TXD3, tx) | 1343 FIELD_PREP(MII_KSZ9031RN_TXD2, tx) | 1344 FIELD_PREP(MII_KSZ9031RN_TXD1, tx) | 1345 FIELD_PREP(MII_KSZ9031RN_TXD0, tx)); 1346 if (ret < 0) 1347 return ret; 1348 1349 return phy_write_mmd(phydev, 2, MII_KSZ9031RN_CLK_PAD_SKEW, 1350 FIELD_PREP(MII_KSZ9031RN_GTX_CLK, tx_clk) | 1351 FIELD_PREP(MII_KSZ9031RN_RX_CLK, rx_clk)); 1352 } 1353 1354 static int ksz9031_config_init(struct phy_device *phydev) 1355 { 1356 const struct device_node *of_node; 1357 static const char *clk_skews[2] = {"rxc-skew-ps", "txc-skew-ps"}; 1358 static const char *rx_data_skews[4] = { 1359 "rxd0-skew-ps", "rxd1-skew-ps", 1360 "rxd2-skew-ps", "rxd3-skew-ps" 1361 }; 1362 static const char *tx_data_skews[4] = { 1363 "txd0-skew-ps", "txd1-skew-ps", 1364 "txd2-skew-ps", "txd3-skew-ps" 1365 }; 1366 static const char *control_skews[2] = {"txen-skew-ps", "rxdv-skew-ps"}; 1367 const struct device *dev_walker; 1368 int result; 1369 1370 result = ksz9031_enable_edpd(phydev); 1371 if (result < 0) 1372 return result; 1373 1374 /* The Micrel driver has a deprecated option to place phy OF 1375 * properties in the MAC node. Walk up the tree of devices to 1376 * find a device with an OF node. 1377 */ 1378 dev_walker = &phydev->mdio.dev; 1379 do { 1380 of_node = dev_walker->of_node; 1381 dev_walker = dev_walker->parent; 1382 } while (!of_node && dev_walker); 1383 1384 if (of_node) { 1385 bool update = false; 1386 1387 if (phy_interface_is_rgmii(phydev)) { 1388 result = ksz9031_config_rgmii_delay(phydev); 1389 if (result < 0) 1390 return result; 1391 } 1392 1393 ksz9031_of_load_skew_values(phydev, of_node, 1394 MII_KSZ9031RN_CLK_PAD_SKEW, 5, 1395 clk_skews, 2, &update); 1396 1397 ksz9031_of_load_skew_values(phydev, of_node, 1398 MII_KSZ9031RN_CONTROL_PAD_SKEW, 4, 1399 control_skews, 2, &update); 1400 1401 ksz9031_of_load_skew_values(phydev, of_node, 1402 MII_KSZ9031RN_RX_DATA_PAD_SKEW, 4, 1403 rx_data_skews, 4, &update); 1404 1405 ksz9031_of_load_skew_values(phydev, of_node, 1406 MII_KSZ9031RN_TX_DATA_PAD_SKEW, 4, 1407 tx_data_skews, 4, &update); 1408 1409 if (update && !phy_interface_is_rgmii(phydev)) 1410 phydev_warn(phydev, 1411 "*-skew-ps values should be used only with RGMII PHY modes\n"); 1412 1413 /* Silicon Errata Sheet (DS80000691D or DS80000692D): 1414 * When the device links in the 1000BASE-T slave mode only, 1415 * the optional 125MHz reference output clock (CLK125_NDO) 1416 * has wide duty cycle variation. 1417 * 1418 * The optional CLK125_NDO clock does not meet the RGMII 1419 * 45/55 percent (min/max) duty cycle requirement and therefore 1420 * cannot be used directly by the MAC side for clocking 1421 * applications that have setup/hold time requirements on 1422 * rising and falling clock edges. 1423 * 1424 * Workaround: 1425 * Force the phy to be the master to receive a stable clock 1426 * which meets the duty cycle requirement. 1427 */ 1428 if (of_property_read_bool(of_node, "micrel,force-master")) { 1429 result = phy_read(phydev, MII_CTRL1000); 1430 if (result < 0) 1431 goto err_force_master; 1432 1433 /* enable master mode, config & prefer master */ 1434 result |= CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER; 1435 result = phy_write(phydev, MII_CTRL1000, result); 1436 if (result < 0) 1437 goto err_force_master; 1438 } 1439 } 1440 1441 return ksz9031_center_flp_timing(phydev); 1442 1443 err_force_master: 1444 phydev_err(phydev, "failed to force the phy to master mode\n"); 1445 return result; 1446 } 1447 1448 #define KSZ9131_SKEW_5BIT_MAX 2400 1449 #define KSZ9131_SKEW_4BIT_MAX 800 1450 #define KSZ9131_OFFSET 700 1451 #define KSZ9131_STEP 100 1452 1453 static int ksz9131_of_load_skew_values(struct phy_device *phydev, 1454 struct device_node *of_node, 1455 u16 reg, size_t field_sz, 1456 char *field[], u8 numfields) 1457 { 1458 int val[4] = {-(1 + KSZ9131_OFFSET), -(2 + KSZ9131_OFFSET), 1459 -(3 + KSZ9131_OFFSET), -(4 + KSZ9131_OFFSET)}; 1460 int skewval, skewmax = 0; 1461 int matches = 0; 1462 u16 maxval; 1463 u16 newval; 1464 u16 mask; 1465 int i; 1466 1467 /* psec properties in dts should mean x pico seconds */ 1468 if (field_sz == 5) 1469 skewmax = KSZ9131_SKEW_5BIT_MAX; 1470 else 1471 skewmax = KSZ9131_SKEW_4BIT_MAX; 1472 1473 for (i = 0; i < numfields; i++) 1474 if (!of_property_read_s32(of_node, field[i], &skewval)) { 1475 if (skewval < -KSZ9131_OFFSET) 1476 skewval = -KSZ9131_OFFSET; 1477 else if (skewval > skewmax) 1478 skewval = skewmax; 1479 1480 val[i] = skewval + KSZ9131_OFFSET; 1481 matches++; 1482 } 1483 1484 if (!matches) 1485 return 0; 1486 1487 if (matches < numfields) 1488 newval = phy_read_mmd(phydev, 2, reg); 1489 else 1490 newval = 0; 1491 1492 maxval = (field_sz == 4) ? 0xf : 0x1f; 1493 for (i = 0; i < numfields; i++) 1494 if (val[i] != -(i + 1 + KSZ9131_OFFSET)) { 1495 mask = 0xffff; 1496 mask ^= maxval << (field_sz * i); 1497 newval = (newval & mask) | 1498 (((val[i] / KSZ9131_STEP) & maxval) 1499 << (field_sz * i)); 1500 } 1501 1502 return phy_write_mmd(phydev, 2, reg, newval); 1503 } 1504 1505 #define KSZ9131RN_MMD_COMMON_CTRL_REG 2 1506 #define KSZ9131RN_RXC_DLL_CTRL 76 1507 #define KSZ9131RN_TXC_DLL_CTRL 77 1508 #define KSZ9131RN_DLL_ENABLE_DELAY 0 1509 1510 static int ksz9131_config_rgmii_delay(struct phy_device *phydev) 1511 { 1512 const struct kszphy_type *type = phydev->drv->driver_data; 1513 u16 rxcdll_val, txcdll_val; 1514 int ret; 1515 1516 switch (phydev->interface) { 1517 case PHY_INTERFACE_MODE_RGMII: 1518 rxcdll_val = type->disable_dll_rx_bit; 1519 txcdll_val = type->disable_dll_tx_bit; 1520 break; 1521 case PHY_INTERFACE_MODE_RGMII_ID: 1522 rxcdll_val = KSZ9131RN_DLL_ENABLE_DELAY; 1523 txcdll_val = KSZ9131RN_DLL_ENABLE_DELAY; 1524 break; 1525 case PHY_INTERFACE_MODE_RGMII_RXID: 1526 rxcdll_val = KSZ9131RN_DLL_ENABLE_DELAY; 1527 txcdll_val = type->disable_dll_tx_bit; 1528 break; 1529 case PHY_INTERFACE_MODE_RGMII_TXID: 1530 rxcdll_val = type->disable_dll_rx_bit; 1531 txcdll_val = KSZ9131RN_DLL_ENABLE_DELAY; 1532 break; 1533 default: 1534 return 0; 1535 } 1536 1537 ret = phy_modify_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG, 1538 KSZ9131RN_RXC_DLL_CTRL, type->disable_dll_mask, 1539 rxcdll_val); 1540 if (ret < 0) 1541 return ret; 1542 1543 return phy_modify_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG, 1544 KSZ9131RN_TXC_DLL_CTRL, type->disable_dll_mask, 1545 txcdll_val); 1546 } 1547 1548 /* Silicon Errata DS80000693B 1549 * 1550 * When LEDs are configured in Individual Mode, LED1 is ON in a no-link 1551 * condition. Workaround is to set register 0x1e, bit 9, this way LED1 behaves 1552 * according to the datasheet (off if there is no link). 1553 */ 1554 static int ksz9131_led_errata(struct phy_device *phydev) 1555 { 1556 int reg; 1557 1558 reg = phy_read_mmd(phydev, 2, 0); 1559 if (reg < 0) 1560 return reg; 1561 1562 if (!(reg & BIT(4))) 1563 return 0; 1564 1565 return phy_set_bits(phydev, 0x1e, BIT(9)); 1566 } 1567 1568 static int ksz9131_config_init(struct phy_device *phydev) 1569 { 1570 struct device_node *of_node; 1571 char *clk_skews[2] = {"rxc-skew-psec", "txc-skew-psec"}; 1572 char *rx_data_skews[4] = { 1573 "rxd0-skew-psec", "rxd1-skew-psec", 1574 "rxd2-skew-psec", "rxd3-skew-psec" 1575 }; 1576 char *tx_data_skews[4] = { 1577 "txd0-skew-psec", "txd1-skew-psec", 1578 "txd2-skew-psec", "txd3-skew-psec" 1579 }; 1580 char *control_skews[2] = {"txen-skew-psec", "rxdv-skew-psec"}; 1581 const struct device *dev_walker; 1582 int ret; 1583 1584 phydev->mdix_ctrl = ETH_TP_MDI_AUTO; 1585 1586 dev_walker = &phydev->mdio.dev; 1587 do { 1588 of_node = dev_walker->of_node; 1589 dev_walker = dev_walker->parent; 1590 } while (!of_node && dev_walker); 1591 1592 if (!of_node) 1593 return 0; 1594 1595 if (phy_interface_is_rgmii(phydev)) { 1596 ret = ksz9131_config_rgmii_delay(phydev); 1597 if (ret < 0) 1598 return ret; 1599 } 1600 1601 ret = ksz9131_of_load_skew_values(phydev, of_node, 1602 MII_KSZ9031RN_CLK_PAD_SKEW, 5, 1603 clk_skews, 2); 1604 if (ret < 0) 1605 return ret; 1606 1607 ret = ksz9131_of_load_skew_values(phydev, of_node, 1608 MII_KSZ9031RN_CONTROL_PAD_SKEW, 4, 1609 control_skews, 2); 1610 if (ret < 0) 1611 return ret; 1612 1613 ret = ksz9131_of_load_skew_values(phydev, of_node, 1614 MII_KSZ9031RN_RX_DATA_PAD_SKEW, 4, 1615 rx_data_skews, 4); 1616 if (ret < 0) 1617 return ret; 1618 1619 ret = ksz9131_of_load_skew_values(phydev, of_node, 1620 MII_KSZ9031RN_TX_DATA_PAD_SKEW, 4, 1621 tx_data_skews, 4); 1622 if (ret < 0) 1623 return ret; 1624 1625 ret = ksz9131_led_errata(phydev); 1626 if (ret < 0) 1627 return ret; 1628 1629 if (phydev->dev_flags & PHY_F_KEEP_PREAMBLE_BEFORE_SFD) 1630 ret = phy_modify(phydev, KSZ9x31_REMOTE_LOOPBACK, 0, 1631 KSZ9x31_REMOTE_LOOPBACK_KEEP_PREAMBLE); 1632 1633 return ret; 1634 } 1635 1636 #define MII_KSZ9131_AUTO_MDIX 0x1C 1637 #define MII_KSZ9131_AUTO_MDI_SET BIT(7) 1638 #define MII_KSZ9131_AUTO_MDIX_SWAP_OFF BIT(6) 1639 #define MII_KSZ9131_DIG_AXAN_STS 0x14 1640 #define MII_KSZ9131_DIG_AXAN_STS_LINK_DET BIT(14) 1641 #define MII_KSZ9131_DIG_AXAN_STS_A_SELECT BIT(12) 1642 1643 static int ksz9131_mdix_update(struct phy_device *phydev) 1644 { 1645 int ret; 1646 1647 if (phydev->mdix_ctrl != ETH_TP_MDI_AUTO) { 1648 phydev->mdix = phydev->mdix_ctrl; 1649 } else { 1650 ret = phy_read(phydev, MII_KSZ9131_DIG_AXAN_STS); 1651 if (ret < 0) 1652 return ret; 1653 1654 if (ret & MII_KSZ9131_DIG_AXAN_STS_LINK_DET) { 1655 if (ret & MII_KSZ9131_DIG_AXAN_STS_A_SELECT) 1656 phydev->mdix = ETH_TP_MDI; 1657 else 1658 phydev->mdix = ETH_TP_MDI_X; 1659 } else { 1660 phydev->mdix = ETH_TP_MDI_INVALID; 1661 } 1662 } 1663 1664 return 0; 1665 } 1666 1667 static int ksz9131_config_mdix(struct phy_device *phydev, u8 ctrl) 1668 { 1669 u16 val; 1670 1671 switch (ctrl) { 1672 case ETH_TP_MDI: 1673 val = MII_KSZ9131_AUTO_MDIX_SWAP_OFF | 1674 MII_KSZ9131_AUTO_MDI_SET; 1675 break; 1676 case ETH_TP_MDI_X: 1677 val = MII_KSZ9131_AUTO_MDIX_SWAP_OFF; 1678 break; 1679 case ETH_TP_MDI_AUTO: 1680 val = 0; 1681 break; 1682 default: 1683 return 0; 1684 } 1685 1686 return phy_modify(phydev, MII_KSZ9131_AUTO_MDIX, 1687 MII_KSZ9131_AUTO_MDIX_SWAP_OFF | 1688 MII_KSZ9131_AUTO_MDI_SET, val); 1689 } 1690 1691 static int ksz9131_read_status(struct phy_device *phydev) 1692 { 1693 int ret; 1694 1695 ret = ksz9131_mdix_update(phydev); 1696 if (ret < 0) 1697 return ret; 1698 1699 return genphy_read_status(phydev); 1700 } 1701 1702 static int ksz9131_config_aneg(struct phy_device *phydev) 1703 { 1704 int ret; 1705 1706 ret = ksz9131_config_mdix(phydev, phydev->mdix_ctrl); 1707 if (ret) 1708 return ret; 1709 1710 return genphy_config_aneg(phydev); 1711 } 1712 1713 static int ksz9477_get_features(struct phy_device *phydev) 1714 { 1715 int ret; 1716 1717 ret = genphy_read_abilities(phydev); 1718 if (ret) 1719 return ret; 1720 1721 /* The "EEE control and capability 1" (Register 3.20) seems to be 1722 * influenced by the "EEE advertisement 1" (Register 7.60). Changes 1723 * on the 7.60 will affect 3.20. So, we need to construct our own list 1724 * of caps. 1725 * KSZ8563R should have 100BaseTX/Full only. 1726 */ 1727 linkmode_and(phydev->supported_eee, phydev->supported, 1728 PHY_EEE_CAP1_FEATURES); 1729 1730 return 0; 1731 } 1732 1733 #define KSZ8873MLL_GLOBAL_CONTROL_4 0x06 1734 #define KSZ8873MLL_GLOBAL_CONTROL_4_DUPLEX BIT(6) 1735 #define KSZ8873MLL_GLOBAL_CONTROL_4_SPEED BIT(4) 1736 static int ksz8873mll_read_status(struct phy_device *phydev) 1737 { 1738 int regval; 1739 1740 /* dummy read */ 1741 regval = phy_read(phydev, KSZ8873MLL_GLOBAL_CONTROL_4); 1742 1743 regval = phy_read(phydev, KSZ8873MLL_GLOBAL_CONTROL_4); 1744 1745 if (regval & KSZ8873MLL_GLOBAL_CONTROL_4_DUPLEX) 1746 phydev->duplex = DUPLEX_HALF; 1747 else 1748 phydev->duplex = DUPLEX_FULL; 1749 1750 if (regval & KSZ8873MLL_GLOBAL_CONTROL_4_SPEED) 1751 phydev->speed = SPEED_10; 1752 else 1753 phydev->speed = SPEED_100; 1754 1755 phydev->link = 1; 1756 phydev->pause = phydev->asym_pause = 0; 1757 1758 return 0; 1759 } 1760 1761 static int ksz9031_get_features(struct phy_device *phydev) 1762 { 1763 int ret; 1764 1765 ret = genphy_read_abilities(phydev); 1766 if (ret < 0) 1767 return ret; 1768 1769 /* Silicon Errata Sheet (DS80000691D or DS80000692D): 1770 * Whenever the device's Asymmetric Pause capability is set to 1, 1771 * link-up may fail after a link-up to link-down transition. 1772 * 1773 * The Errata Sheet is for ksz9031, but ksz9021 has the same issue 1774 * 1775 * Workaround: 1776 * Do not enable the Asymmetric Pause capability bit. 1777 */ 1778 linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported); 1779 1780 /* We force setting the Pause capability as the core will force the 1781 * Asymmetric Pause capability to 1 otherwise. 1782 */ 1783 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported); 1784 1785 return 0; 1786 } 1787 1788 static int ksz9031_read_status(struct phy_device *phydev) 1789 { 1790 int err; 1791 int regval; 1792 1793 err = genphy_read_status(phydev); 1794 if (err) 1795 return err; 1796 1797 /* Make sure the PHY is not broken. Read idle error count, 1798 * and reset the PHY if it is maxed out. 1799 */ 1800 regval = phy_read(phydev, MII_STAT1000); 1801 if ((regval & 0xFF) == 0xFF) { 1802 phy_init_hw(phydev); 1803 phydev->link = 0; 1804 if (phydev->drv->config_intr && phy_interrupt_is_valid(phydev)) 1805 phydev->drv->config_intr(phydev); 1806 return genphy_config_aneg(phydev); 1807 } 1808 1809 return 0; 1810 } 1811 1812 static int ksz9x31_cable_test_start(struct phy_device *phydev) 1813 { 1814 struct kszphy_priv *priv = phydev->priv; 1815 int ret; 1816 1817 /* KSZ9131RNX, DS00002841B-page 38, 4.14 LinkMD (R) Cable Diagnostic 1818 * Prior to running the cable diagnostics, Auto-negotiation should 1819 * be disabled, full duplex set and the link speed set to 1000Mbps 1820 * via the Basic Control Register. 1821 */ 1822 ret = phy_modify(phydev, MII_BMCR, 1823 BMCR_SPEED1000 | BMCR_FULLDPLX | 1824 BMCR_ANENABLE | BMCR_SPEED100, 1825 BMCR_SPEED1000 | BMCR_FULLDPLX); 1826 if (ret) 1827 return ret; 1828 1829 /* KSZ9131RNX, DS00002841B-page 38, 4.14 LinkMD (R) Cable Diagnostic 1830 * The Master-Slave configuration should be set to Slave by writing 1831 * a value of 0x1000 to the Auto-Negotiation Master Slave Control 1832 * Register. 1833 */ 1834 ret = phy_read(phydev, MII_CTRL1000); 1835 if (ret < 0) 1836 return ret; 1837 1838 /* Cache these bits, they need to be restored once LinkMD finishes. */ 1839 priv->vct_ctrl1000 = ret & (CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER); 1840 ret &= ~(CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER); 1841 ret |= CTL1000_ENABLE_MASTER; 1842 1843 return phy_write(phydev, MII_CTRL1000, ret); 1844 } 1845 1846 static int ksz9x31_cable_test_result_trans(u16 status) 1847 { 1848 switch (FIELD_GET(KSZ9x31_LMD_VCT_ST_MASK, status)) { 1849 case KSZ9x31_LMD_VCT_ST_NORMAL: 1850 return ETHTOOL_A_CABLE_RESULT_CODE_OK; 1851 case KSZ9x31_LMD_VCT_ST_OPEN: 1852 return ETHTOOL_A_CABLE_RESULT_CODE_OPEN; 1853 case KSZ9x31_LMD_VCT_ST_SHORT: 1854 return ETHTOOL_A_CABLE_RESULT_CODE_SAME_SHORT; 1855 case KSZ9x31_LMD_VCT_ST_FAIL: 1856 fallthrough; 1857 default: 1858 return ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC; 1859 } 1860 } 1861 1862 static bool ksz9x31_cable_test_failed(u16 status) 1863 { 1864 int stat = FIELD_GET(KSZ9x31_LMD_VCT_ST_MASK, status); 1865 1866 return stat == KSZ9x31_LMD_VCT_ST_FAIL; 1867 } 1868 1869 static bool ksz9x31_cable_test_fault_length_valid(u16 status) 1870 { 1871 switch (FIELD_GET(KSZ9x31_LMD_VCT_ST_MASK, status)) { 1872 case KSZ9x31_LMD_VCT_ST_OPEN: 1873 fallthrough; 1874 case KSZ9x31_LMD_VCT_ST_SHORT: 1875 return true; 1876 } 1877 return false; 1878 } 1879 1880 static int ksz9x31_cable_test_fault_length(struct phy_device *phydev, u16 stat) 1881 { 1882 int dt = FIELD_GET(KSZ9x31_LMD_VCT_DATA_MASK, stat); 1883 1884 /* KSZ9131RNX, DS00002841B-page 38, 4.14 LinkMD (R) Cable Diagnostic 1885 * 1886 * distance to fault = (VCT_DATA - 22) * 4 / cable propagation velocity 1887 */ 1888 if (phydev_id_compare(phydev, PHY_ID_KSZ9131) || 1889 phydev_id_compare(phydev, PHY_ID_KSZ9477)) 1890 dt = clamp(dt - 22, 0, 255); 1891 1892 return (dt * 400) / 10; 1893 } 1894 1895 static int ksz9x31_cable_test_wait_for_completion(struct phy_device *phydev) 1896 { 1897 int val, ret; 1898 1899 ret = phy_read_poll_timeout(phydev, KSZ9x31_LMD, val, 1900 !(val & KSZ9x31_LMD_VCT_EN), 1901 30000, 100000, true); 1902 1903 return ret < 0 ? ret : 0; 1904 } 1905 1906 static int ksz9x31_cable_test_get_pair(int pair) 1907 { 1908 static const int ethtool_pair[] = { 1909 ETHTOOL_A_CABLE_PAIR_A, 1910 ETHTOOL_A_CABLE_PAIR_B, 1911 ETHTOOL_A_CABLE_PAIR_C, 1912 ETHTOOL_A_CABLE_PAIR_D, 1913 }; 1914 1915 return ethtool_pair[pair]; 1916 } 1917 1918 static int ksz9x31_cable_test_one_pair(struct phy_device *phydev, int pair) 1919 { 1920 int ret, val; 1921 1922 /* KSZ9131RNX, DS00002841B-page 38, 4.14 LinkMD (R) Cable Diagnostic 1923 * To test each individual cable pair, set the cable pair in the Cable 1924 * Diagnostics Test Pair (VCT_PAIR[1:0]) field of the LinkMD Cable 1925 * Diagnostic Register, along with setting the Cable Diagnostics Test 1926 * Enable (VCT_EN) bit. The Cable Diagnostics Test Enable (VCT_EN) bit 1927 * will self clear when the test is concluded. 1928 */ 1929 ret = phy_write(phydev, KSZ9x31_LMD, 1930 KSZ9x31_LMD_VCT_EN | KSZ9x31_LMD_VCT_PAIR(pair)); 1931 if (ret) 1932 return ret; 1933 1934 ret = ksz9x31_cable_test_wait_for_completion(phydev); 1935 if (ret) 1936 return ret; 1937 1938 val = phy_read(phydev, KSZ9x31_LMD); 1939 if (val < 0) 1940 return val; 1941 1942 if (ksz9x31_cable_test_failed(val)) 1943 return -EAGAIN; 1944 1945 ret = ethnl_cable_test_result(phydev, 1946 ksz9x31_cable_test_get_pair(pair), 1947 ksz9x31_cable_test_result_trans(val)); 1948 if (ret) 1949 return ret; 1950 1951 if (!ksz9x31_cable_test_fault_length_valid(val)) 1952 return 0; 1953 1954 return ethnl_cable_test_fault_length(phydev, 1955 ksz9x31_cable_test_get_pair(pair), 1956 ksz9x31_cable_test_fault_length(phydev, val)); 1957 } 1958 1959 static int ksz9x31_cable_test_get_status(struct phy_device *phydev, 1960 bool *finished) 1961 { 1962 struct kszphy_priv *priv = phydev->priv; 1963 unsigned long pair_mask; 1964 int retries = 20; 1965 int pair, ret, rv; 1966 1967 *finished = false; 1968 1969 if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT, 1970 phydev->supported) || 1971 linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT, 1972 phydev->supported)) 1973 pair_mask = 0xf; /* All pairs */ 1974 else 1975 pair_mask = 0x3; /* Pairs A and B only */ 1976 1977 /* Try harder if link partner is active */ 1978 while (pair_mask && retries--) { 1979 for_each_set_bit(pair, &pair_mask, 4) { 1980 ret = ksz9x31_cable_test_one_pair(phydev, pair); 1981 if (ret == -EAGAIN) 1982 continue; 1983 if (ret < 0) 1984 return ret; 1985 clear_bit(pair, &pair_mask); 1986 } 1987 /* If link partner is in autonegotiation mode it will send 2ms 1988 * of FLPs with at least 6ms of silence. 1989 * Add 2ms sleep to have better chances to hit this silence. 1990 */ 1991 if (pair_mask) 1992 usleep_range(2000, 3000); 1993 } 1994 1995 /* Report remaining unfinished pair result as unknown. */ 1996 for_each_set_bit(pair, &pair_mask, 4) { 1997 ret = ethnl_cable_test_result(phydev, 1998 ksz9x31_cable_test_get_pair(pair), 1999 ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC); 2000 } 2001 2002 *finished = true; 2003 2004 /* Restore cached bits from before LinkMD got started. */ 2005 rv = phy_modify(phydev, MII_CTRL1000, 2006 CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER, 2007 priv->vct_ctrl1000); 2008 if (rv) 2009 return rv; 2010 2011 return ret; 2012 } 2013 2014 static int ksz8873mll_config_aneg(struct phy_device *phydev) 2015 { 2016 return 0; 2017 } 2018 2019 static int ksz886x_config_mdix(struct phy_device *phydev, u8 ctrl) 2020 { 2021 u16 val; 2022 2023 switch (ctrl) { 2024 case ETH_TP_MDI: 2025 val = KSZ886X_BMCR_DISABLE_AUTO_MDIX; 2026 break; 2027 case ETH_TP_MDI_X: 2028 /* Note: The naming of the bit KSZ886X_BMCR_FORCE_MDI is bit 2029 * counter intuitive, the "-X" in "1 = Force MDI" in the data 2030 * sheet seems to be missing: 2031 * 1 = Force MDI (sic!) (transmit on RX+/RX- pins) 2032 * 0 = Normal operation (transmit on TX+/TX- pins) 2033 */ 2034 val = KSZ886X_BMCR_DISABLE_AUTO_MDIX | KSZ886X_BMCR_FORCE_MDI; 2035 break; 2036 case ETH_TP_MDI_AUTO: 2037 val = 0; 2038 break; 2039 default: 2040 return 0; 2041 } 2042 2043 return phy_modify(phydev, MII_BMCR, 2044 KSZ886X_BMCR_HP_MDIX | KSZ886X_BMCR_FORCE_MDI | 2045 KSZ886X_BMCR_DISABLE_AUTO_MDIX, 2046 KSZ886X_BMCR_HP_MDIX | val); 2047 } 2048 2049 static int ksz886x_config_aneg(struct phy_device *phydev) 2050 { 2051 int ret; 2052 2053 ret = genphy_config_aneg(phydev); 2054 if (ret) 2055 return ret; 2056 2057 if (phydev->autoneg != AUTONEG_ENABLE) { 2058 /* When autonegotiation is disabled, we need to manually force 2059 * the link state. If we don't do this, the PHY will keep 2060 * sending Fast Link Pulses (FLPs) which are part of the 2061 * autonegotiation process. This is not desired when 2062 * autonegotiation is off. 2063 */ 2064 ret = phy_set_bits(phydev, MII_KSZPHY_CTRL, 2065 KSZ886X_CTRL_FORCE_LINK); 2066 if (ret) 2067 return ret; 2068 } else { 2069 /* If we had previously forced the link state, we need to 2070 * clear KSZ886X_CTRL_FORCE_LINK bit now. Otherwise, the PHY 2071 * will not perform autonegotiation. 2072 */ 2073 ret = phy_clear_bits(phydev, MII_KSZPHY_CTRL, 2074 KSZ886X_CTRL_FORCE_LINK); 2075 if (ret) 2076 return ret; 2077 } 2078 2079 /* The MDI-X configuration is automatically changed by the PHY after 2080 * switching from autoneg off to on. So, take MDI-X configuration under 2081 * own control and set it after autoneg configuration was done. 2082 */ 2083 return ksz886x_config_mdix(phydev, phydev->mdix_ctrl); 2084 } 2085 2086 static int ksz886x_mdix_update(struct phy_device *phydev) 2087 { 2088 int ret; 2089 2090 ret = phy_read(phydev, MII_BMCR); 2091 if (ret < 0) 2092 return ret; 2093 2094 if (ret & KSZ886X_BMCR_DISABLE_AUTO_MDIX) { 2095 if (ret & KSZ886X_BMCR_FORCE_MDI) 2096 phydev->mdix_ctrl = ETH_TP_MDI_X; 2097 else 2098 phydev->mdix_ctrl = ETH_TP_MDI; 2099 } else { 2100 phydev->mdix_ctrl = ETH_TP_MDI_AUTO; 2101 } 2102 2103 ret = phy_read(phydev, MII_KSZPHY_CTRL); 2104 if (ret < 0) 2105 return ret; 2106 2107 /* Same reverse logic as KSZ886X_BMCR_FORCE_MDI */ 2108 if (ret & KSZ886X_CTRL_MDIX_STAT) 2109 phydev->mdix = ETH_TP_MDI_X; 2110 else 2111 phydev->mdix = ETH_TP_MDI; 2112 2113 return 0; 2114 } 2115 2116 static int ksz886x_read_status(struct phy_device *phydev) 2117 { 2118 int ret; 2119 2120 ret = ksz886x_mdix_update(phydev); 2121 if (ret < 0) 2122 return ret; 2123 2124 return genphy_read_status(phydev); 2125 } 2126 2127 static int ksz9477_mdix_update(struct phy_device *phydev) 2128 { 2129 if (phydev->mdix_ctrl != ETH_TP_MDI_AUTO) 2130 phydev->mdix = phydev->mdix_ctrl; 2131 else 2132 phydev->mdix = ETH_TP_MDI_INVALID; 2133 2134 return 0; 2135 } 2136 2137 static int ksz9477_read_mdix_ctrl(struct phy_device *phydev) 2138 { 2139 int val; 2140 2141 val = phy_read(phydev, MII_KSZ9131_AUTO_MDIX); 2142 if (val < 0) 2143 return val; 2144 2145 if (!(val & MII_KSZ9131_AUTO_MDIX_SWAP_OFF)) 2146 phydev->mdix_ctrl = ETH_TP_MDI_AUTO; 2147 else if (val & MII_KSZ9131_AUTO_MDI_SET) 2148 phydev->mdix_ctrl = ETH_TP_MDI; 2149 else 2150 phydev->mdix_ctrl = ETH_TP_MDI_X; 2151 2152 return 0; 2153 } 2154 2155 static int ksz9477_read_status(struct phy_device *phydev) 2156 { 2157 int ret; 2158 2159 ret = ksz9477_mdix_update(phydev); 2160 if (ret) 2161 return ret; 2162 2163 return genphy_read_status(phydev); 2164 } 2165 2166 static int ksz9477_config_aneg(struct phy_device *phydev) 2167 { 2168 int ret; 2169 2170 ret = ksz9131_config_mdix(phydev, phydev->mdix_ctrl); 2171 if (ret) 2172 return ret; 2173 2174 return genphy_config_aneg(phydev); 2175 } 2176 2177 struct ksz9477_errata_write { 2178 u8 dev_addr; 2179 u8 reg_addr; 2180 u16 val; 2181 }; 2182 2183 static const struct ksz9477_errata_write ksz9477_errata_writes[] = { 2184 /* Register settings are needed to improve PHY receive performance */ 2185 {0x01, 0x6f, 0xdd0b}, 2186 {0x01, 0x8f, 0x6032}, 2187 {0x01, 0x9d, 0x248c}, 2188 {0x01, 0x75, 0x0060}, 2189 {0x01, 0xd3, 0x7777}, 2190 {0x1c, 0x06, 0x3008}, 2191 {0x1c, 0x08, 0x2000}, 2192 2193 /* Transmit waveform amplitude can be improved (1000BASE-T, 100BASE-TX, 10BASE-Te) */ 2194 {0x1c, 0x04, 0x00d0}, 2195 2196 /* Register settings are required to meet data sheet supply current specifications */ 2197 {0x1c, 0x13, 0x6eff}, 2198 {0x1c, 0x14, 0xe6ff}, 2199 {0x1c, 0x15, 0x6eff}, 2200 {0x1c, 0x16, 0xe6ff}, 2201 {0x1c, 0x17, 0x00ff}, 2202 {0x1c, 0x18, 0x43ff}, 2203 {0x1c, 0x19, 0xc3ff}, 2204 {0x1c, 0x1a, 0x6fff}, 2205 {0x1c, 0x1b, 0x07ff}, 2206 {0x1c, 0x1c, 0x0fff}, 2207 {0x1c, 0x1d, 0xe7ff}, 2208 {0x1c, 0x1e, 0xefff}, 2209 {0x1c, 0x20, 0xeeee}, 2210 }; 2211 2212 static int ksz9477_phy_errata(struct phy_device *phydev) 2213 { 2214 int err; 2215 int i; 2216 2217 /* Apply PHY settings to address errata listed in 2218 * KSZ9477, KSZ9897, KSZ9896, KSZ9567, KSZ8565 2219 * Silicon Errata and Data Sheet Clarification documents. 2220 * 2221 * Document notes: Before configuring the PHY MMD registers, it is 2222 * necessary to set the PHY to 100 Mbps speed with auto-negotiation 2223 * disabled by writing to register 0xN100-0xN101. After writing the 2224 * MMD registers, and after all errata workarounds that involve PHY 2225 * register settings, write register 0xN100-0xN101 again to enable 2226 * and restart auto-negotiation. 2227 */ 2228 err = phy_write(phydev, MII_BMCR, BMCR_SPEED100 | BMCR_FULLDPLX); 2229 if (err) 2230 return err; 2231 2232 for (i = 0; i < ARRAY_SIZE(ksz9477_errata_writes); ++i) { 2233 const struct ksz9477_errata_write *errata = &ksz9477_errata_writes[i]; 2234 2235 err = phy_write_mmd(phydev, errata->dev_addr, errata->reg_addr, errata->val); 2236 if (err) 2237 return err; 2238 } 2239 2240 return genphy_restart_aneg(phydev); 2241 } 2242 2243 static int ksz9477_config_init(struct phy_device *phydev) 2244 { 2245 int err; 2246 2247 /* Only KSZ9897 family of switches needs this fix. */ 2248 if ((phydev->phy_id & 0xf) == 1) { 2249 err = ksz9477_phy_errata(phydev); 2250 if (err) 2251 return err; 2252 } 2253 2254 /* Read initial MDI-X config state. So, we do not need to poll it 2255 * later on. 2256 */ 2257 err = ksz9477_read_mdix_ctrl(phydev); 2258 if (err) 2259 return err; 2260 2261 return kszphy_config_init(phydev); 2262 } 2263 2264 static int kszphy_get_sset_count(struct phy_device *phydev) 2265 { 2266 return ARRAY_SIZE(kszphy_hw_stats); 2267 } 2268 2269 static void kszphy_get_strings(struct phy_device *phydev, u8 *data) 2270 { 2271 int i; 2272 2273 for (i = 0; i < ARRAY_SIZE(kszphy_hw_stats); i++) 2274 ethtool_puts(&data, kszphy_hw_stats[i].string); 2275 } 2276 2277 static u64 kszphy_get_stat(struct phy_device *phydev, int i) 2278 { 2279 struct kszphy_hw_stat stat = kszphy_hw_stats[i]; 2280 struct kszphy_priv *priv = phydev->priv; 2281 int val; 2282 u64 ret; 2283 2284 val = phy_read(phydev, stat.reg); 2285 if (val < 0) { 2286 ret = U64_MAX; 2287 } else { 2288 val = val & ((1 << stat.bits) - 1); 2289 priv->stats[i] += val; 2290 ret = priv->stats[i]; 2291 } 2292 2293 return ret; 2294 } 2295 2296 static void kszphy_get_stats(struct phy_device *phydev, 2297 struct ethtool_stats *stats, u64 *data) 2298 { 2299 int i; 2300 2301 for (i = 0; i < ARRAY_SIZE(kszphy_hw_stats); i++) 2302 data[i] = kszphy_get_stat(phydev, i); 2303 } 2304 2305 /* KSZ9477 PHY RXER Counter. Probably supported by other PHYs like KSZ9313, 2306 * etc. The counter is incremented when the PHY receives a frame with one or 2307 * more symbol errors. The counter is cleared when the register is read. 2308 */ 2309 #define MII_KSZ9477_PHY_RXER_COUNTER 0x15 2310 2311 static int kszphy_update_stats(struct phy_device *phydev) 2312 { 2313 struct kszphy_priv *priv = phydev->priv; 2314 int ret; 2315 2316 ret = phy_read(phydev, MII_KSZ9477_PHY_RXER_COUNTER); 2317 if (ret < 0) 2318 return ret; 2319 2320 priv->phy_stats.rx_err_pkt_cnt += ret; 2321 2322 return 0; 2323 } 2324 2325 static void kszphy_get_phy_stats(struct phy_device *phydev, 2326 struct ethtool_eth_phy_stats *eth_stats, 2327 struct ethtool_phy_stats *stats) 2328 { 2329 struct kszphy_priv *priv = phydev->priv; 2330 2331 stats->rx_errors = priv->phy_stats.rx_err_pkt_cnt; 2332 } 2333 2334 /* Base register for Signal Quality Indicator (SQI) - Channel A 2335 * 2336 * MMD Address: MDIO_MMD_PMAPMD (0x01) 2337 * Register: 0xAC (Channel A) 2338 * Each channel (pair) has its own register: 2339 * Channel A: 0xAC 2340 * Channel B: 0xAD 2341 * Channel C: 0xAE 2342 * Channel D: 0xAF 2343 */ 2344 #define KSZ9477_MMD_SIGNAL_QUALITY_CHAN_A 0xac 2345 2346 /* SQI field mask for bits [14:8] 2347 * 2348 * SQI indicates relative quality of the signal. 2349 * A lower value indicates better signal quality. 2350 */ 2351 #define KSZ9477_MMD_SQI_MASK GENMASK(14, 8) 2352 2353 #define KSZ9477_MAX_CHANNELS 4 2354 #define KSZ9477_SQI_MAX 7 2355 2356 /* Number of SQI samples to average for a stable result. 2357 * 2358 * Reference: KSZ9477S Datasheet DS00002392C, Section 4.1.11 (page 26) 2359 * For noisy environments, a minimum of 30–50 readings is recommended. 2360 */ 2361 #define KSZ9477_SQI_SAMPLE_COUNT 40 2362 2363 /* The hardware SQI register provides a raw value from 0-127, where a lower 2364 * value indicates better signal quality. However, empirical testing has 2365 * shown that only the 0-7 range is relevant for a functional link. A raw 2366 * value of 8 or higher was measured directly before link drop. This aligns 2367 * with the OPEN Alliance recommendation that SQI=0 should represent the 2368 * pre-failure state. 2369 * 2370 * This table provides a non-linear mapping from the useful raw hardware 2371 * values (0-7) to the standard 0-7 SQI scale, where higher is better. 2372 */ 2373 static const u8 ksz_sqi_mapping[] = { 2374 7, /* raw 0 -> SQI 7 */ 2375 7, /* raw 1 -> SQI 7 */ 2376 6, /* raw 2 -> SQI 6 */ 2377 5, /* raw 3 -> SQI 5 */ 2378 4, /* raw 4 -> SQI 4 */ 2379 3, /* raw 5 -> SQI 3 */ 2380 2, /* raw 6 -> SQI 2 */ 2381 1, /* raw 7 -> SQI 1 */ 2382 }; 2383 2384 /** 2385 * kszphy_get_sqi - Read, average, and map Signal Quality Index (SQI) 2386 * @phydev: the PHY device 2387 * 2388 * This function reads and processes the raw Signal Quality Index from the 2389 * PHY. Based on empirical testing, a raw value of 8 or higher indicates a 2390 * pre-failure state and is mapped to SQI 0. Raw values from 0-7 are 2391 * mapped to the standard 0-7 SQI scale via a lookup table. 2392 * 2393 * Return: SQI value (0–7), or a negative errno on failure. 2394 */ 2395 static int kszphy_get_sqi(struct phy_device *phydev) 2396 { 2397 int sum[KSZ9477_MAX_CHANNELS] = { 0 }; 2398 int worst_sqi = KSZ9477_SQI_MAX; 2399 int i, val, raw_sqi, ch; 2400 u8 channels; 2401 2402 /* Determine applicable channels based on link speed */ 2403 if (phydev->speed == SPEED_1000) 2404 channels = 4; 2405 else if (phydev->speed == SPEED_100) 2406 channels = 1; 2407 else 2408 return -EOPNOTSUPP; 2409 2410 /* Sample and accumulate SQI readings for each pair (currently only one). 2411 * 2412 * Reference: KSZ9477S Datasheet DS00002392C, Section 4.1.11 (page 26) 2413 * - The SQI register is updated every 2 µs. 2414 * - Values may fluctuate significantly, even in low-noise environments. 2415 * - For reliable estimation, average a minimum of 30–50 samples 2416 * (recommended for noisy environments) 2417 * - In noisy environments, individual readings are highly unreliable. 2418 * 2419 * We use 40 samples per pair with a delay of 3 µs between each 2420 * read to ensure new values are captured (2 µs update interval). 2421 */ 2422 for (i = 0; i < KSZ9477_SQI_SAMPLE_COUNT; i++) { 2423 for (ch = 0; ch < channels; ch++) { 2424 val = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, 2425 KSZ9477_MMD_SIGNAL_QUALITY_CHAN_A + ch); 2426 if (val < 0) 2427 return val; 2428 2429 raw_sqi = FIELD_GET(KSZ9477_MMD_SQI_MASK, val); 2430 sum[ch] += raw_sqi; 2431 2432 /* We communicate with the PHY via MDIO via SPI or 2433 * I2C, which is relatively slow. At least slower than 2434 * the update interval of the SQI register. 2435 * So, we can skip the delay between reads. 2436 */ 2437 } 2438 } 2439 2440 /* Calculate average for each channel and find the worst SQI */ 2441 for (ch = 0; ch < channels; ch++) { 2442 int avg_raw_sqi = sum[ch] / KSZ9477_SQI_SAMPLE_COUNT; 2443 int mapped_sqi; 2444 2445 /* Handle the pre-fail/failed state first. */ 2446 if (avg_raw_sqi >= ARRAY_SIZE(ksz_sqi_mapping)) 2447 mapped_sqi = 0; 2448 else 2449 /* Use the lookup table for the good signal range. */ 2450 mapped_sqi = ksz_sqi_mapping[avg_raw_sqi]; 2451 2452 if (mapped_sqi < worst_sqi) 2453 worst_sqi = mapped_sqi; 2454 } 2455 2456 return worst_sqi; 2457 } 2458 2459 static int kszphy_get_sqi_max(struct phy_device *phydev) 2460 { 2461 return KSZ9477_SQI_MAX; 2462 } 2463 2464 static int kszphy_get_mse_capability(struct phy_device *phydev, 2465 struct phy_mse_capability *cap) 2466 { 2467 /* Capabilities depend on link mode: 2468 * - 1000BASE-T: per-pair SQI registers exist => expose A..D 2469 * and a WORST selector. 2470 * - 100BASE-TX: HW provides a single MSE/SQI reading in the "channel A" 2471 * register, but with auto MDI-X there is no MDI-X resolution bit, 2472 * so we cannot map that register to a specific wire pair reliably. 2473 * To avoid misleading per-channel data, advertise only LINK. 2474 * Other speeds: no MSE exposure via this driver. 2475 * 2476 * Note: WORST is *not* a hardware selector on this family. 2477 * We expose it because the driver computes it in software 2478 * by scanning per-channel readouts (A..D) and picking the 2479 * maximum average MSE. 2480 */ 2481 if (phydev->speed == SPEED_1000) 2482 cap->supported_caps = PHY_MSE_CAP_CHANNEL_A | 2483 PHY_MSE_CAP_CHANNEL_B | 2484 PHY_MSE_CAP_CHANNEL_C | 2485 PHY_MSE_CAP_CHANNEL_D | 2486 PHY_MSE_CAP_WORST_CHANNEL; 2487 else if (phydev->speed == SPEED_100) 2488 cap->supported_caps = PHY_MSE_CAP_LINK; 2489 else 2490 return -EOPNOTSUPP; 2491 2492 cap->max_average_mse = FIELD_MAX(KSZ9477_MMD_SQI_MASK); 2493 cap->refresh_rate_ps = 2000000; /* 2 us */ 2494 /* Estimated from link modulation (125 MBd per channel) and documented 2495 * refresh rate of 2 us 2496 */ 2497 cap->num_symbols = 250; 2498 2499 cap->supported_caps |= PHY_MSE_CAP_AVG; 2500 2501 return 0; 2502 } 2503 2504 static int kszphy_get_mse_snapshot(struct phy_device *phydev, 2505 enum phy_mse_channel channel, 2506 struct phy_mse_snapshot *snapshot) 2507 { 2508 u8 num_channels; 2509 int ret; 2510 2511 if (phydev->speed == SPEED_1000) 2512 num_channels = 4; 2513 else if (phydev->speed == SPEED_100) 2514 num_channels = 1; 2515 else 2516 return -EOPNOTSUPP; 2517 2518 if (channel == PHY_MSE_CHANNEL_WORST) { 2519 u32 worst_val = 0; 2520 int i; 2521 2522 /* WORST is implemented in software: select the maximum 2523 * average MSE across the available per-channel registers. 2524 * Only defined when multiple channels exist (1000BASE-T). 2525 */ 2526 if (num_channels < 2) 2527 return -EOPNOTSUPP; 2528 2529 for (i = 0; i < num_channels; i++) { 2530 ret = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, 2531 KSZ9477_MMD_SIGNAL_QUALITY_CHAN_A + i); 2532 if (ret < 0) 2533 return ret; 2534 2535 ret = FIELD_GET(KSZ9477_MMD_SQI_MASK, ret); 2536 if (ret > worst_val) 2537 worst_val = ret; 2538 } 2539 snapshot->average_mse = worst_val; 2540 } else if (channel == PHY_MSE_CHANNEL_LINK && num_channels == 1) { 2541 ret = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, 2542 KSZ9477_MMD_SIGNAL_QUALITY_CHAN_A); 2543 if (ret < 0) 2544 return ret; 2545 snapshot->average_mse = FIELD_GET(KSZ9477_MMD_SQI_MASK, ret); 2546 } else if (channel >= PHY_MSE_CHANNEL_A && 2547 channel <= PHY_MSE_CHANNEL_D) { 2548 /* Per-channel readouts are valid only for 1000BASE-T. */ 2549 if (phydev->speed != SPEED_1000) 2550 return -EOPNOTSUPP; 2551 2552 ret = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, 2553 KSZ9477_MMD_SIGNAL_QUALITY_CHAN_A + channel); 2554 if (ret < 0) 2555 return ret; 2556 snapshot->average_mse = FIELD_GET(KSZ9477_MMD_SQI_MASK, ret); 2557 } else { 2558 return -EOPNOTSUPP; 2559 } 2560 2561 return 0; 2562 } 2563 2564 static void kszphy_enable_clk(struct phy_device *phydev) 2565 { 2566 struct kszphy_priv *priv = phydev->priv; 2567 2568 if (!priv->clk_enable && priv->clk) { 2569 clk_prepare_enable(priv->clk); 2570 priv->clk_enable = true; 2571 } 2572 } 2573 2574 static void kszphy_disable_clk(struct phy_device *phydev) 2575 { 2576 struct kszphy_priv *priv = phydev->priv; 2577 2578 if (priv->clk_enable && priv->clk) { 2579 clk_disable_unprepare(priv->clk); 2580 priv->clk_enable = false; 2581 } 2582 } 2583 2584 static int kszphy_generic_resume(struct phy_device *phydev) 2585 { 2586 kszphy_enable_clk(phydev); 2587 2588 return genphy_resume(phydev); 2589 } 2590 2591 static int kszphy_generic_suspend(struct phy_device *phydev) 2592 { 2593 int ret; 2594 2595 ret = genphy_suspend(phydev); 2596 if (ret) 2597 return ret; 2598 2599 kszphy_disable_clk(phydev); 2600 2601 return 0; 2602 } 2603 2604 static int kszphy_suspend(struct phy_device *phydev) 2605 { 2606 /* Disable PHY Interrupts */ 2607 if (phy_interrupt_is_valid(phydev)) { 2608 phydev->interrupts = PHY_INTERRUPT_DISABLED; 2609 if (phydev->drv->config_intr) 2610 phydev->drv->config_intr(phydev); 2611 } 2612 2613 return kszphy_generic_suspend(phydev); 2614 } 2615 2616 static void kszphy_parse_led_mode(struct phy_device *phydev) 2617 { 2618 const struct kszphy_type *type = phydev->drv->driver_data; 2619 const struct device_node *np = phydev->mdio.dev.of_node; 2620 struct kszphy_priv *priv = phydev->priv; 2621 int ret; 2622 2623 if (type && type->led_mode_reg) { 2624 ret = of_property_read_u32(np, "micrel,led-mode", 2625 &priv->led_mode); 2626 2627 if (ret) 2628 priv->led_mode = -1; 2629 2630 if (priv->led_mode > 3) { 2631 phydev_err(phydev, "invalid led mode: 0x%02x\n", 2632 priv->led_mode); 2633 priv->led_mode = -1; 2634 } 2635 } else { 2636 priv->led_mode = -1; 2637 } 2638 } 2639 2640 static int kszphy_resume(struct phy_device *phydev) 2641 { 2642 int ret; 2643 2644 ret = kszphy_generic_resume(phydev); 2645 if (ret) 2646 return ret; 2647 2648 /* After switching from power-down to normal mode, an internal global 2649 * reset is automatically generated. Wait a minimum of 1 ms before 2650 * read/write access to the PHY registers. 2651 */ 2652 usleep_range(1000, 2000); 2653 2654 ret = kszphy_config_reset(phydev); 2655 if (ret) 2656 return ret; 2657 2658 /* Enable PHY Interrupts */ 2659 if (phy_interrupt_is_valid(phydev)) { 2660 phydev->interrupts = PHY_INTERRUPT_ENABLED; 2661 if (phydev->drv->config_intr) 2662 phydev->drv->config_intr(phydev); 2663 } 2664 2665 return 0; 2666 } 2667 2668 /* Because of errata DS80000700A, receiver error following software 2669 * power down. Suspend and resume callbacks only disable and enable 2670 * external rmii reference clock. 2671 */ 2672 static int ksz8041_resume(struct phy_device *phydev) 2673 { 2674 kszphy_enable_clk(phydev); 2675 2676 return 0; 2677 } 2678 2679 static int ksz8041_suspend(struct phy_device *phydev) 2680 { 2681 kszphy_disable_clk(phydev); 2682 2683 return 0; 2684 } 2685 2686 static int ksz9477_resume(struct phy_device *phydev) 2687 { 2688 int ret; 2689 2690 /* No need to initialize registers if not powered down. */ 2691 ret = phy_read(phydev, MII_BMCR); 2692 if (ret < 0) 2693 return ret; 2694 if (!(ret & BMCR_PDOWN)) 2695 return 0; 2696 2697 genphy_resume(phydev); 2698 2699 /* After switching from power-down to normal mode, an internal global 2700 * reset is automatically generated. Wait a minimum of 1 ms before 2701 * read/write access to the PHY registers. 2702 */ 2703 usleep_range(1000, 2000); 2704 2705 /* Only KSZ9897 family of switches needs this fix. */ 2706 if ((phydev->phy_id & 0xf) == 1) { 2707 ret = ksz9477_phy_errata(phydev); 2708 if (ret) 2709 return ret; 2710 } 2711 2712 /* Enable PHY Interrupts */ 2713 if (phy_interrupt_is_valid(phydev)) { 2714 phydev->interrupts = PHY_INTERRUPT_ENABLED; 2715 if (phydev->drv->config_intr) 2716 phydev->drv->config_intr(phydev); 2717 } 2718 2719 return 0; 2720 } 2721 2722 static int ksz8061_resume(struct phy_device *phydev) 2723 { 2724 int ret; 2725 2726 /* This function can be called twice when the Ethernet device is on. */ 2727 ret = phy_read(phydev, MII_BMCR); 2728 if (ret < 0) 2729 return ret; 2730 if (!(ret & BMCR_PDOWN)) 2731 return 0; 2732 2733 ret = kszphy_generic_resume(phydev); 2734 if (ret) 2735 return ret; 2736 2737 usleep_range(1000, 2000); 2738 2739 /* Re-program the value after chip is reset. */ 2740 ret = phy_write_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_DEVID1, 0xB61A); 2741 if (ret) 2742 return ret; 2743 2744 /* Enable PHY Interrupts */ 2745 if (phy_interrupt_is_valid(phydev)) { 2746 phydev->interrupts = PHY_INTERRUPT_ENABLED; 2747 if (phydev->drv->config_intr) 2748 phydev->drv->config_intr(phydev); 2749 } 2750 2751 return 0; 2752 } 2753 2754 static int ksz8061_suspend(struct phy_device *phydev) 2755 { 2756 return kszphy_suspend(phydev); 2757 } 2758 2759 static int kszphy_probe(struct phy_device *phydev) 2760 { 2761 const struct kszphy_type *type = phydev->drv->driver_data; 2762 const struct device_node *np = phydev->mdio.dev.of_node; 2763 struct kszphy_priv *priv; 2764 struct clk *clk; 2765 2766 priv = devm_kzalloc_obj(&phydev->mdio.dev, *priv); 2767 if (!priv) 2768 return -ENOMEM; 2769 2770 phydev->priv = priv; 2771 2772 priv->type = type; 2773 2774 kszphy_parse_led_mode(phydev); 2775 2776 clk = devm_clk_get_optional(&phydev->mdio.dev, "rmii-ref"); 2777 /* NOTE: clk may be NULL if building without CONFIG_HAVE_CLK */ 2778 if (!IS_ERR_OR_NULL(clk)) { 2779 bool rmii_ref_clk_sel_25_mhz; 2780 unsigned long rate; 2781 int err; 2782 2783 err = clk_prepare_enable(clk); 2784 if (err) { 2785 phydev_err(phydev, "Failed to enable rmii-ref clock\n"); 2786 return err; 2787 } 2788 2789 rate = clk_get_rate(clk); 2790 clk_disable_unprepare(clk); 2791 2792 if (type) 2793 priv->rmii_ref_clk_sel = type->has_rmii_ref_clk_sel; 2794 rmii_ref_clk_sel_25_mhz = of_property_present(np, 2795 "micrel,rmii-reference-clock-select-25-mhz"); 2796 2797 if (rate > 24500000 && rate < 25500000) { 2798 priv->rmii_ref_clk_sel_val = rmii_ref_clk_sel_25_mhz; 2799 } else if (rate > 49500000 && rate < 50500000) { 2800 priv->rmii_ref_clk_sel_val = !rmii_ref_clk_sel_25_mhz; 2801 } else { 2802 phydev_err(phydev, "Clock rate out of range: %ld\n", 2803 rate); 2804 return -EINVAL; 2805 } 2806 } else if (!clk) { 2807 /* unnamed clock from the generic ethernet-phy binding */ 2808 clk = devm_clk_get_optional(&phydev->mdio.dev, NULL); 2809 } 2810 2811 if (IS_ERR(clk)) 2812 return PTR_ERR(clk); 2813 2814 priv->clk = clk; 2815 2816 if (ksz8041_fiber_mode(phydev)) 2817 phydev->port = PORT_FIBRE; 2818 2819 /* Support legacy board-file configuration */ 2820 if (phydev->dev_flags & MICREL_PHY_50MHZ_CLK) { 2821 priv->rmii_ref_clk_sel = true; 2822 priv->rmii_ref_clk_sel_val = true; 2823 } 2824 2825 return 0; 2826 } 2827 2828 static int lan8814_cable_test_start(struct phy_device *phydev) 2829 { 2830 /* If autoneg is enabled, we won't be able to test cross pair 2831 * short. In this case, the PHY will "detect" a link and 2832 * confuse the internal state machine - disable auto neg here. 2833 * Set the speed to 1000mbit and full duplex. 2834 */ 2835 return phy_modify(phydev, MII_BMCR, BMCR_ANENABLE | BMCR_SPEED100, 2836 BMCR_SPEED1000 | BMCR_FULLDPLX); 2837 } 2838 2839 static int ksz886x_cable_test_start(struct phy_device *phydev) 2840 { 2841 if (phydev->dev_flags & MICREL_KSZ8_P1_ERRATA) 2842 return -EOPNOTSUPP; 2843 2844 /* If autoneg is enabled, we won't be able to test cross pair 2845 * short. In this case, the PHY will "detect" a link and 2846 * confuse the internal state machine - disable auto neg here. 2847 * If autoneg is disabled, we should set the speed to 10mbit. 2848 */ 2849 return phy_clear_bits(phydev, MII_BMCR, BMCR_ANENABLE | BMCR_SPEED100); 2850 } 2851 2852 static __always_inline int ksz886x_cable_test_result_trans(u16 status, u16 mask) 2853 { 2854 switch (FIELD_GET(mask, status)) { 2855 case KSZ8081_LMD_STAT_NORMAL: 2856 return ETHTOOL_A_CABLE_RESULT_CODE_OK; 2857 case KSZ8081_LMD_STAT_SHORT: 2858 return ETHTOOL_A_CABLE_RESULT_CODE_SAME_SHORT; 2859 case KSZ8081_LMD_STAT_OPEN: 2860 return ETHTOOL_A_CABLE_RESULT_CODE_OPEN; 2861 case KSZ8081_LMD_STAT_FAIL: 2862 fallthrough; 2863 default: 2864 return ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC; 2865 } 2866 } 2867 2868 static __always_inline bool ksz886x_cable_test_failed(u16 status, u16 mask) 2869 { 2870 return FIELD_GET(mask, status) == 2871 KSZ8081_LMD_STAT_FAIL; 2872 } 2873 2874 static __always_inline bool ksz886x_cable_test_fault_length_valid(u16 status, u16 mask) 2875 { 2876 switch (FIELD_GET(mask, status)) { 2877 case KSZ8081_LMD_STAT_OPEN: 2878 fallthrough; 2879 case KSZ8081_LMD_STAT_SHORT: 2880 return true; 2881 } 2882 return false; 2883 } 2884 2885 static __always_inline int ksz886x_cable_test_fault_length(struct phy_device *phydev, 2886 u16 status, u16 data_mask) 2887 { 2888 int dt; 2889 2890 /* According to the data sheet the distance to the fault is 2891 * DELTA_TIME * 0.4 meters for ksz phys. 2892 * (DELTA_TIME - 22) * 0.8 for lan8814 phy. 2893 */ 2894 dt = FIELD_GET(data_mask, status); 2895 2896 if (phydev_id_compare(phydev, PHY_ID_LAN8814)) 2897 return ((dt - 22) * 800) / 10; 2898 else 2899 return (dt * 400) / 10; 2900 } 2901 2902 static int ksz886x_cable_test_wait_for_completion(struct phy_device *phydev) 2903 { 2904 const struct kszphy_type *type = phydev->drv->driver_data; 2905 int val, ret; 2906 2907 ret = phy_read_poll_timeout(phydev, type->cable_diag_reg, val, 2908 !(val & KSZ8081_LMD_ENABLE_TEST), 2909 30000, 100000, true); 2910 2911 return ret < 0 ? ret : 0; 2912 } 2913 2914 static int lan8814_cable_test_one_pair(struct phy_device *phydev, int pair) 2915 { 2916 static const int ethtool_pair[] = { ETHTOOL_A_CABLE_PAIR_A, 2917 ETHTOOL_A_CABLE_PAIR_B, 2918 ETHTOOL_A_CABLE_PAIR_C, 2919 ETHTOOL_A_CABLE_PAIR_D, 2920 }; 2921 u32 fault_length; 2922 int ret; 2923 int val; 2924 2925 val = KSZ8081_LMD_ENABLE_TEST; 2926 val = val | (pair << LAN8814_PAIR_BIT_SHIFT); 2927 2928 ret = phy_write(phydev, LAN8814_CABLE_DIAG, val); 2929 if (ret < 0) 2930 return ret; 2931 2932 ret = ksz886x_cable_test_wait_for_completion(phydev); 2933 if (ret) 2934 return ret; 2935 2936 val = phy_read(phydev, LAN8814_CABLE_DIAG); 2937 if (val < 0) 2938 return val; 2939 2940 if (ksz886x_cable_test_failed(val, LAN8814_CABLE_DIAG_STAT_MASK)) 2941 return -EAGAIN; 2942 2943 ret = ethnl_cable_test_result(phydev, ethtool_pair[pair], 2944 ksz886x_cable_test_result_trans(val, 2945 LAN8814_CABLE_DIAG_STAT_MASK 2946 )); 2947 if (ret) 2948 return ret; 2949 2950 if (!ksz886x_cable_test_fault_length_valid(val, LAN8814_CABLE_DIAG_STAT_MASK)) 2951 return 0; 2952 2953 fault_length = ksz886x_cable_test_fault_length(phydev, val, 2954 LAN8814_CABLE_DIAG_VCT_DATA_MASK); 2955 2956 return ethnl_cable_test_fault_length(phydev, ethtool_pair[pair], fault_length); 2957 } 2958 2959 static int ksz886x_cable_test_one_pair(struct phy_device *phydev, int pair) 2960 { 2961 static const int ethtool_pair[] = { 2962 ETHTOOL_A_CABLE_PAIR_A, 2963 ETHTOOL_A_CABLE_PAIR_B, 2964 }; 2965 int ret, val, mdix; 2966 u32 fault_length; 2967 2968 /* There is no way to choice the pair, like we do one ksz9031. 2969 * We can workaround this limitation by using the MDI-X functionality. 2970 */ 2971 if (pair == 0) 2972 mdix = ETH_TP_MDI; 2973 else 2974 mdix = ETH_TP_MDI_X; 2975 2976 switch (phydev->phy_id & MICREL_PHY_ID_MASK) { 2977 case PHY_ID_KSZ8081: 2978 ret = ksz8081_config_mdix(phydev, mdix); 2979 break; 2980 case PHY_ID_KSZ886X: 2981 ret = ksz886x_config_mdix(phydev, mdix); 2982 break; 2983 default: 2984 ret = -ENODEV; 2985 } 2986 2987 if (ret) 2988 return ret; 2989 2990 /* Now we are ready to fire. This command will send a 100ns pulse 2991 * to the pair. 2992 */ 2993 ret = phy_write(phydev, KSZ8081_LMD, KSZ8081_LMD_ENABLE_TEST); 2994 if (ret) 2995 return ret; 2996 2997 ret = ksz886x_cable_test_wait_for_completion(phydev); 2998 if (ret) 2999 return ret; 3000 3001 val = phy_read(phydev, KSZ8081_LMD); 3002 if (val < 0) 3003 return val; 3004 3005 if (ksz886x_cable_test_failed(val, KSZ8081_LMD_STAT_MASK)) 3006 return -EAGAIN; 3007 3008 ret = ethnl_cable_test_result(phydev, ethtool_pair[pair], 3009 ksz886x_cable_test_result_trans(val, KSZ8081_LMD_STAT_MASK)); 3010 if (ret) 3011 return ret; 3012 3013 if (!ksz886x_cable_test_fault_length_valid(val, KSZ8081_LMD_STAT_MASK)) 3014 return 0; 3015 3016 fault_length = ksz886x_cable_test_fault_length(phydev, val, KSZ8081_LMD_DELTA_TIME_MASK); 3017 3018 return ethnl_cable_test_fault_length(phydev, ethtool_pair[pair], fault_length); 3019 } 3020 3021 static int ksz886x_cable_test_get_status(struct phy_device *phydev, 3022 bool *finished) 3023 { 3024 const struct kszphy_type *type = phydev->drv->driver_data; 3025 unsigned long pair_mask = type->pair_mask; 3026 int retries = 20; 3027 int ret = 0; 3028 int pair; 3029 3030 *finished = false; 3031 3032 /* Try harder if link partner is active */ 3033 while (pair_mask && retries--) { 3034 for_each_set_bit(pair, &pair_mask, 4) { 3035 if (type->cable_diag_reg == LAN8814_CABLE_DIAG) 3036 ret = lan8814_cable_test_one_pair(phydev, pair); 3037 else 3038 ret = ksz886x_cable_test_one_pair(phydev, pair); 3039 if (ret == -EAGAIN) 3040 continue; 3041 if (ret < 0) 3042 return ret; 3043 clear_bit(pair, &pair_mask); 3044 } 3045 /* If link partner is in autonegotiation mode it will send 2ms 3046 * of FLPs with at least 6ms of silence. 3047 * Add 2ms sleep to have better chances to hit this silence. 3048 */ 3049 if (pair_mask) 3050 msleep(2); 3051 } 3052 3053 *finished = true; 3054 3055 return ret; 3056 } 3057 3058 /** 3059 * LAN8814_PAGE_PCS - Selects Extended Page 0. 3060 * 3061 * This page contains timers used for auto-negotiation, debug registers and 3062 * register to configure fast link failure. 3063 */ 3064 #define LAN8814_PAGE_PCS 0 3065 3066 /** 3067 * LAN8814_PAGE_AFE_PMA - Selects Extended Page 1. 3068 * 3069 * This page appears to control the Analog Front-End (AFE) and Physical 3070 * Medium Attachment (PMA) layers. It is used to access registers like 3071 * LAN8814_PD_CONTROLS and LAN8814_LINK_QUALITY. 3072 */ 3073 #define LAN8814_PAGE_AFE_PMA 1 3074 3075 /** 3076 * LAN8814_PAGE_PCS_DIGITAL - Selects Extended Page 2. 3077 * 3078 * This page seems dedicated to the Physical Coding Sublayer (PCS) and other 3079 * digital logic. It is used for MDI-X alignment (LAN8814_ALIGN_SWAP) and EEE 3080 * state (LAN8814_EEE_STATE) in the LAN8814, and is repurposed for statistics 3081 * and self-test counters in the LAN8842. 3082 */ 3083 #define LAN8814_PAGE_PCS_DIGITAL 2 3084 3085 /** 3086 * LAN8814_PAGE_EEE - Selects Extended Page 3. 3087 * 3088 * This page contains EEE registers 3089 */ 3090 #define LAN8814_PAGE_EEE 3 3091 3092 /** 3093 * LAN8814_PAGE_COMMON_REGS - Selects Extended Page 4. 3094 * 3095 * This page contains device-common registers that affect the entire chip. 3096 * It includes controls for chip-level resets, strap status, GPIO, 3097 * QSGMII, the shared 1588 PTP block, and the PVT monitor. 3098 */ 3099 #define LAN8814_PAGE_COMMON_REGS 4 3100 3101 /** 3102 * LAN8814_PAGE_PORT_REGS - Selects Extended Page 5. 3103 * 3104 * This page contains port-specific registers that must be accessed 3105 * on a per-port basis. It includes controls for port LEDs, QSGMII PCS, 3106 * rate adaptation FIFOs, and the per-port 1588 TSU block. 3107 */ 3108 #define LAN8814_PAGE_PORT_REGS 5 3109 3110 /** 3111 * LAN8814_PAGE_POWER_REGS - Selects Extended Page 28. 3112 * 3113 * This page contains analog control registers and power mode registers. 3114 */ 3115 #define LAN8814_PAGE_POWER_REGS 28 3116 3117 /** 3118 * LAN8814_PAGE_SYSTEM_CTRL - Selects Extended Page 31. 3119 * 3120 * This page appears to hold fundamental system or global controls. In the 3121 * driver, it is used by the related LAN8804 to access the 3122 * LAN8814_CLOCK_MANAGEMENT register. 3123 */ 3124 #define LAN8814_PAGE_SYSTEM_CTRL 31 3125 3126 #define LAN_EXT_PAGE_ACCESS_CONTROL 0x16 3127 #define LAN_EXT_PAGE_ACCESS_ADDRESS_DATA 0x17 3128 #define LAN_EXT_PAGE_ACCESS_CTRL_EP_FUNC 0x4000 3129 3130 #define LAN8814_QSGMII_TX_CONFIG 0x35 3131 #define LAN8814_QSGMII_TX_CONFIG_QSGMII BIT(3) 3132 #define LAN8814_QSGMII_SOFT_RESET 0x43 3133 #define LAN8814_QSGMII_SOFT_RESET_BIT BIT(0) 3134 #define LAN8814_QSGMII_PCS1G_ANEG_CONFIG 0x13 3135 #define LAN8814_QSGMII_PCS1G_ANEG_CONFIG_ANEG_ENA BIT(3) 3136 #define LAN8814_ALIGN_SWAP 0x4a 3137 #define LAN8814_ALIGN_TX_A_B_SWAP 0x1 3138 #define LAN8814_ALIGN_TX_A_B_SWAP_MASK GENMASK(2, 0) 3139 3140 #define LAN8804_ALIGN_SWAP 0x4a 3141 #define LAN8804_ALIGN_TX_A_B_SWAP 0x1 3142 #define LAN8804_ALIGN_TX_A_B_SWAP_MASK GENMASK(2, 0) 3143 #define LAN8814_CLOCK_MANAGEMENT 0xd 3144 #define LAN8814_LINK_QUALITY 0x8e 3145 3146 static int lanphy_read_page_reg(struct phy_device *phydev, int page, u32 addr) 3147 { 3148 int data; 3149 3150 phy_lock_mdio_bus(phydev); 3151 __phy_write(phydev, LAN_EXT_PAGE_ACCESS_CONTROL, page); 3152 __phy_write(phydev, LAN_EXT_PAGE_ACCESS_ADDRESS_DATA, addr); 3153 __phy_write(phydev, LAN_EXT_PAGE_ACCESS_CONTROL, 3154 (page | LAN_EXT_PAGE_ACCESS_CTRL_EP_FUNC)); 3155 data = __phy_read(phydev, LAN_EXT_PAGE_ACCESS_ADDRESS_DATA); 3156 phy_unlock_mdio_bus(phydev); 3157 3158 return data; 3159 } 3160 3161 static int lanphy_write_page_reg(struct phy_device *phydev, int page, u16 addr, 3162 u16 val) 3163 { 3164 phy_lock_mdio_bus(phydev); 3165 __phy_write(phydev, LAN_EXT_PAGE_ACCESS_CONTROL, page); 3166 __phy_write(phydev, LAN_EXT_PAGE_ACCESS_ADDRESS_DATA, addr); 3167 __phy_write(phydev, LAN_EXT_PAGE_ACCESS_CONTROL, 3168 page | LAN_EXT_PAGE_ACCESS_CTRL_EP_FUNC); 3169 3170 val = __phy_write(phydev, LAN_EXT_PAGE_ACCESS_ADDRESS_DATA, val); 3171 if (val != 0) 3172 phydev_err(phydev, "Error: phy_write has returned error %d\n", 3173 val); 3174 phy_unlock_mdio_bus(phydev); 3175 return val; 3176 } 3177 3178 static int lanphy_modify_page_reg(struct phy_device *phydev, int page, u16 addr, 3179 u16 mask, u16 set) 3180 { 3181 int ret; 3182 3183 phy_lock_mdio_bus(phydev); 3184 __phy_write(phydev, LAN_EXT_PAGE_ACCESS_CONTROL, page); 3185 __phy_write(phydev, LAN_EXT_PAGE_ACCESS_ADDRESS_DATA, addr); 3186 __phy_write(phydev, LAN_EXT_PAGE_ACCESS_CONTROL, 3187 (page | LAN_EXT_PAGE_ACCESS_CTRL_EP_FUNC)); 3188 ret = __phy_modify_changed(phydev, LAN_EXT_PAGE_ACCESS_ADDRESS_DATA, 3189 mask, set); 3190 phy_unlock_mdio_bus(phydev); 3191 3192 if (ret < 0) 3193 phydev_err(phydev, "__phy_modify_changed() failed: %pe\n", 3194 ERR_PTR(ret)); 3195 3196 return ret; 3197 } 3198 3199 static int lan8814_config_ts_intr(struct phy_device *phydev, bool enable) 3200 { 3201 u16 val = 0; 3202 3203 if (enable) 3204 val = PTP_TSU_INT_EN_PTP_TX_TS_EN_ | 3205 PTP_TSU_INT_EN_PTP_TX_TS_OVRFL_EN_ | 3206 PTP_TSU_INT_EN_PTP_RX_TS_EN_ | 3207 PTP_TSU_INT_EN_PTP_RX_TS_OVRFL_EN_; 3208 3209 return lanphy_write_page_reg(phydev, LAN8814_PAGE_PORT_REGS, 3210 PTP_TSU_INT_EN, val); 3211 } 3212 3213 static void lan8814_ptp_rx_ts_get(struct phy_device *phydev, 3214 u32 *seconds, u32 *nano_seconds, u16 *seq_id) 3215 { 3216 *seconds = lanphy_read_page_reg(phydev, LAN8814_PAGE_PORT_REGS, 3217 PTP_RX_INGRESS_SEC_HI); 3218 *seconds = (*seconds << 16) | 3219 lanphy_read_page_reg(phydev, LAN8814_PAGE_PORT_REGS, 3220 PTP_RX_INGRESS_SEC_LO); 3221 3222 *nano_seconds = lanphy_read_page_reg(phydev, LAN8814_PAGE_PORT_REGS, 3223 PTP_RX_INGRESS_NS_HI); 3224 *nano_seconds = ((*nano_seconds & 0x3fff) << 16) | 3225 lanphy_read_page_reg(phydev, LAN8814_PAGE_PORT_REGS, 3226 PTP_RX_INGRESS_NS_LO); 3227 3228 *seq_id = lanphy_read_page_reg(phydev, LAN8814_PAGE_PORT_REGS, 3229 PTP_RX_MSG_HEADER2); 3230 } 3231 3232 static void lan8814_ptp_tx_ts_get(struct phy_device *phydev, 3233 u32 *seconds, u32 *nano_seconds, u16 *seq_id) 3234 { 3235 *seconds = lanphy_read_page_reg(phydev, LAN8814_PAGE_PORT_REGS, 3236 PTP_TX_EGRESS_SEC_HI); 3237 *seconds = *seconds << 16 | 3238 lanphy_read_page_reg(phydev, LAN8814_PAGE_PORT_REGS, 3239 PTP_TX_EGRESS_SEC_LO); 3240 3241 *nano_seconds = lanphy_read_page_reg(phydev, LAN8814_PAGE_PORT_REGS, 3242 PTP_TX_EGRESS_NS_HI); 3243 *nano_seconds = ((*nano_seconds & 0x3fff) << 16) | 3244 lanphy_read_page_reg(phydev, LAN8814_PAGE_PORT_REGS, 3245 PTP_TX_EGRESS_NS_LO); 3246 3247 *seq_id = lanphy_read_page_reg(phydev, LAN8814_PAGE_PORT_REGS, 3248 PTP_TX_MSG_HEADER2); 3249 } 3250 3251 static int lan8814_ts_info(struct mii_timestamper *mii_ts, struct kernel_ethtool_ts_info *info) 3252 { 3253 struct kszphy_ptp_priv *ptp_priv = container_of(mii_ts, struct kszphy_ptp_priv, mii_ts); 3254 struct lan8814_shared_priv *shared = phy_package_get_priv(ptp_priv->phydev); 3255 3256 info->so_timestamping = SOF_TIMESTAMPING_TX_HARDWARE | 3257 SOF_TIMESTAMPING_RX_HARDWARE | 3258 SOF_TIMESTAMPING_RAW_HARDWARE; 3259 3260 info->phc_index = ptp_clock_index(shared->ptp_clock); 3261 3262 info->tx_types = 3263 (1 << HWTSTAMP_TX_OFF) | 3264 (1 << HWTSTAMP_TX_ON) | 3265 (1 << HWTSTAMP_TX_ONESTEP_SYNC); 3266 3267 info->rx_filters = 3268 (1 << HWTSTAMP_FILTER_NONE) | 3269 (1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT) | 3270 (1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT) | 3271 (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) | 3272 (1 << HWTSTAMP_FILTER_PTP_V2_EVENT); 3273 3274 return 0; 3275 } 3276 3277 static void lan8814_flush_fifo(struct phy_device *phydev, bool egress) 3278 { 3279 int i; 3280 3281 for (i = 0; i < FIFO_SIZE; ++i) 3282 lanphy_read_page_reg(phydev, LAN8814_PAGE_PORT_REGS, 3283 egress ? PTP_TX_MSG_HEADER2 : PTP_RX_MSG_HEADER2); 3284 3285 /* Read to clear overflow status bit */ 3286 lanphy_read_page_reg(phydev, LAN8814_PAGE_PORT_REGS, PTP_TSU_INT_STS); 3287 } 3288 3289 static int lan8814_hwtstamp_get(struct mii_timestamper *mii_ts, 3290 struct kernel_hwtstamp_config *config) 3291 { 3292 struct kszphy_ptp_priv *ptp_priv = 3293 container_of(mii_ts, struct kszphy_ptp_priv, mii_ts); 3294 3295 config->tx_type = ptp_priv->hwts_tx_type; 3296 config->rx_filter = ptp_priv->rx_filter; 3297 3298 return 0; 3299 } 3300 3301 static int lan8814_hwtstamp_set(struct mii_timestamper *mii_ts, 3302 struct kernel_hwtstamp_config *config, 3303 struct netlink_ext_ack *extack) 3304 { 3305 struct kszphy_ptp_priv *ptp_priv = 3306 container_of(mii_ts, struct kszphy_ptp_priv, mii_ts); 3307 struct lan8814_ptp_rx_ts *rx_ts, *tmp; 3308 int txcfg = 0, rxcfg = 0; 3309 int pkt_ts_enable; 3310 3311 switch (config->rx_filter) { 3312 case HWTSTAMP_FILTER_NONE: 3313 ptp_priv->layer = 0; 3314 ptp_priv->version = 0; 3315 break; 3316 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT: 3317 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC: 3318 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ: 3319 ptp_priv->layer = PTP_CLASS_L4; 3320 ptp_priv->version = PTP_CLASS_V2; 3321 break; 3322 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT: 3323 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC: 3324 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ: 3325 ptp_priv->layer = PTP_CLASS_L2; 3326 ptp_priv->version = PTP_CLASS_V2; 3327 break; 3328 case HWTSTAMP_FILTER_PTP_V2_EVENT: 3329 case HWTSTAMP_FILTER_PTP_V2_SYNC: 3330 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ: 3331 ptp_priv->layer = PTP_CLASS_L4 | PTP_CLASS_L2; 3332 ptp_priv->version = PTP_CLASS_V2; 3333 break; 3334 default: 3335 return -ERANGE; 3336 } 3337 3338 switch (config->tx_type) { 3339 case HWTSTAMP_TX_OFF: 3340 case HWTSTAMP_TX_ON: 3341 case HWTSTAMP_TX_ONESTEP_SYNC: 3342 break; 3343 default: 3344 return -ERANGE; 3345 } 3346 3347 ptp_priv->hwts_tx_type = config->tx_type; 3348 ptp_priv->rx_filter = config->rx_filter; 3349 3350 if (ptp_priv->layer & PTP_CLASS_L2) { 3351 rxcfg = PTP_RX_PARSE_CONFIG_LAYER2_EN_; 3352 txcfg = PTP_TX_PARSE_CONFIG_LAYER2_EN_; 3353 } else if (ptp_priv->layer & PTP_CLASS_L4) { 3354 rxcfg |= PTP_RX_PARSE_CONFIG_IPV4_EN_ | PTP_RX_PARSE_CONFIG_IPV6_EN_; 3355 txcfg |= PTP_TX_PARSE_CONFIG_IPV4_EN_ | PTP_TX_PARSE_CONFIG_IPV6_EN_; 3356 } 3357 lanphy_write_page_reg(ptp_priv->phydev, LAN8814_PAGE_PORT_REGS, 3358 PTP_RX_PARSE_CONFIG, rxcfg); 3359 lanphy_write_page_reg(ptp_priv->phydev, LAN8814_PAGE_PORT_REGS, 3360 PTP_TX_PARSE_CONFIG, txcfg); 3361 3362 pkt_ts_enable = PTP_TIMESTAMP_EN_SYNC_ | PTP_TIMESTAMP_EN_DREQ_ | 3363 PTP_TIMESTAMP_EN_PDREQ_ | PTP_TIMESTAMP_EN_PDRES_; 3364 lanphy_write_page_reg(ptp_priv->phydev, LAN8814_PAGE_PORT_REGS, 3365 PTP_RX_TIMESTAMP_EN, pkt_ts_enable); 3366 lanphy_write_page_reg(ptp_priv->phydev, LAN8814_PAGE_PORT_REGS, 3367 PTP_TX_TIMESTAMP_EN, pkt_ts_enable); 3368 3369 if (ptp_priv->hwts_tx_type == HWTSTAMP_TX_ONESTEP_SYNC) { 3370 lanphy_modify_page_reg(ptp_priv->phydev, LAN8814_PAGE_PORT_REGS, 3371 PTP_TX_MOD, 3372 PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_, 3373 PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_); 3374 } else if (ptp_priv->hwts_tx_type == HWTSTAMP_TX_ON) { 3375 lanphy_modify_page_reg(ptp_priv->phydev, LAN8814_PAGE_PORT_REGS, 3376 PTP_TX_MOD, 3377 PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_, 3378 0); 3379 } 3380 3381 if (config->rx_filter != HWTSTAMP_FILTER_NONE) 3382 lan8814_config_ts_intr(ptp_priv->phydev, true); 3383 else 3384 lan8814_config_ts_intr(ptp_priv->phydev, false); 3385 3386 /* In case of multiple starts and stops, these needs to be cleared */ 3387 list_for_each_entry_safe(rx_ts, tmp, &ptp_priv->rx_ts_list, list) { 3388 list_del(&rx_ts->list); 3389 kfree(rx_ts); 3390 } 3391 skb_queue_purge(&ptp_priv->rx_queue); 3392 skb_queue_purge(&ptp_priv->tx_queue); 3393 3394 lan8814_flush_fifo(ptp_priv->phydev, false); 3395 lan8814_flush_fifo(ptp_priv->phydev, true); 3396 3397 return 0; 3398 } 3399 3400 static void lan8814_txtstamp(struct mii_timestamper *mii_ts, 3401 struct sk_buff *skb, int type) 3402 { 3403 struct kszphy_ptp_priv *ptp_priv = container_of(mii_ts, struct kszphy_ptp_priv, mii_ts); 3404 3405 switch (ptp_priv->hwts_tx_type) { 3406 case HWTSTAMP_TX_ONESTEP_SYNC: 3407 if (ptp_msg_is_sync(skb, type)) { 3408 kfree_skb(skb); 3409 return; 3410 } 3411 fallthrough; 3412 case HWTSTAMP_TX_ON: 3413 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; 3414 skb_queue_tail(&ptp_priv->tx_queue, skb); 3415 break; 3416 case HWTSTAMP_TX_OFF: 3417 default: 3418 kfree_skb(skb); 3419 break; 3420 } 3421 } 3422 3423 static bool lan8814_get_sig_rx(struct sk_buff *skb, u16 *sig) 3424 { 3425 struct ptp_header *ptp_header; 3426 u32 type; 3427 3428 skb_push(skb, ETH_HLEN); 3429 type = ptp_classify_raw(skb); 3430 ptp_header = ptp_parse_header(skb, type); 3431 skb_pull_inline(skb, ETH_HLEN); 3432 3433 if (!ptp_header) 3434 return false; 3435 3436 *sig = (__force u16)(ntohs(ptp_header->sequence_id)); 3437 return true; 3438 } 3439 3440 static bool lan8814_match_rx_skb(struct kszphy_ptp_priv *ptp_priv, 3441 struct sk_buff *skb) 3442 { 3443 struct skb_shared_hwtstamps *shhwtstamps; 3444 struct lan8814_ptp_rx_ts *rx_ts, *tmp; 3445 unsigned long flags; 3446 bool ret = false; 3447 u16 skb_sig; 3448 3449 if (!lan8814_get_sig_rx(skb, &skb_sig)) 3450 return ret; 3451 3452 /* Iterate over all RX timestamps and match it with the received skbs */ 3453 spin_lock_irqsave(&ptp_priv->rx_ts_lock, flags); 3454 list_for_each_entry_safe(rx_ts, tmp, &ptp_priv->rx_ts_list, list) { 3455 /* Check if we found the signature we were looking for. */ 3456 if (memcmp(&skb_sig, &rx_ts->seq_id, sizeof(rx_ts->seq_id))) 3457 continue; 3458 3459 shhwtstamps = skb_hwtstamps(skb); 3460 memset(shhwtstamps, 0, sizeof(*shhwtstamps)); 3461 shhwtstamps->hwtstamp = ktime_set(rx_ts->seconds, 3462 rx_ts->nsec); 3463 list_del(&rx_ts->list); 3464 kfree(rx_ts); 3465 3466 ret = true; 3467 break; 3468 } 3469 spin_unlock_irqrestore(&ptp_priv->rx_ts_lock, flags); 3470 3471 if (ret) 3472 netif_rx(skb); 3473 return ret; 3474 } 3475 3476 static bool lan8814_rxtstamp(struct mii_timestamper *mii_ts, struct sk_buff *skb, int type) 3477 { 3478 struct kszphy_ptp_priv *ptp_priv = 3479 container_of(mii_ts, struct kszphy_ptp_priv, mii_ts); 3480 3481 if (ptp_priv->rx_filter == HWTSTAMP_FILTER_NONE || 3482 type == PTP_CLASS_NONE) 3483 return false; 3484 3485 if ((type & ptp_priv->version) == 0 || (type & ptp_priv->layer) == 0) 3486 return false; 3487 3488 /* If we failed to match then add it to the queue for when the timestamp 3489 * will come 3490 */ 3491 if (!lan8814_match_rx_skb(ptp_priv, skb)) 3492 skb_queue_tail(&ptp_priv->rx_queue, skb); 3493 3494 return true; 3495 } 3496 3497 static void lan8814_ptp_clock_set(struct phy_device *phydev, 3498 time64_t sec, u32 nsec) 3499 { 3500 lanphy_write_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, 3501 PTP_CLOCK_SET_SEC_LO, lower_16_bits(sec)); 3502 lanphy_write_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, 3503 PTP_CLOCK_SET_SEC_MID, upper_16_bits(sec)); 3504 lanphy_write_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, 3505 PTP_CLOCK_SET_SEC_HI, upper_32_bits(sec)); 3506 lanphy_write_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, 3507 PTP_CLOCK_SET_NS_LO, lower_16_bits(nsec)); 3508 lanphy_write_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, 3509 PTP_CLOCK_SET_NS_HI, upper_16_bits(nsec)); 3510 3511 lanphy_write_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, PTP_CMD_CTL, 3512 PTP_CMD_CTL_PTP_CLOCK_LOAD_); 3513 } 3514 3515 static void lan8814_ptp_clock_get(struct phy_device *phydev, 3516 time64_t *sec, u32 *nsec) 3517 { 3518 lanphy_write_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, PTP_CMD_CTL, 3519 PTP_CMD_CTL_PTP_CLOCK_READ_); 3520 3521 *sec = lanphy_read_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, 3522 PTP_CLOCK_READ_SEC_HI); 3523 *sec <<= 16; 3524 *sec |= lanphy_read_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, 3525 PTP_CLOCK_READ_SEC_MID); 3526 *sec <<= 16; 3527 *sec |= lanphy_read_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, 3528 PTP_CLOCK_READ_SEC_LO); 3529 3530 *nsec = lanphy_read_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, 3531 PTP_CLOCK_READ_NS_HI); 3532 *nsec <<= 16; 3533 *nsec |= lanphy_read_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, 3534 PTP_CLOCK_READ_NS_LO); 3535 } 3536 3537 static int lan8814_ptpci_gettime64(struct ptp_clock_info *ptpci, 3538 struct timespec64 *ts) 3539 { 3540 struct lan8814_shared_priv *shared = container_of(ptpci, struct lan8814_shared_priv, 3541 ptp_clock_info); 3542 struct phy_device *phydev = shared->phydev; 3543 u32 nano_seconds; 3544 time64_t seconds; 3545 3546 mutex_lock(&shared->shared_lock); 3547 lan8814_ptp_clock_get(phydev, &seconds, &nano_seconds); 3548 mutex_unlock(&shared->shared_lock); 3549 ts->tv_sec = seconds; 3550 ts->tv_nsec = nano_seconds; 3551 3552 return 0; 3553 } 3554 3555 static int lan8814_ptpci_settime64(struct ptp_clock_info *ptpci, 3556 const struct timespec64 *ts) 3557 { 3558 struct lan8814_shared_priv *shared = container_of(ptpci, struct lan8814_shared_priv, 3559 ptp_clock_info); 3560 struct phy_device *phydev = shared->phydev; 3561 3562 mutex_lock(&shared->shared_lock); 3563 lan8814_ptp_clock_set(phydev, ts->tv_sec, ts->tv_nsec); 3564 mutex_unlock(&shared->shared_lock); 3565 3566 return 0; 3567 } 3568 3569 static void lan8814_ptp_set_target(struct phy_device *phydev, int event, 3570 s64 start_sec, u32 start_nsec) 3571 { 3572 /* Set the start time */ 3573 lanphy_write_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, 3574 LAN8814_PTP_CLOCK_TARGET_SEC_LO(event), 3575 lower_16_bits(start_sec)); 3576 lanphy_write_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, 3577 LAN8814_PTP_CLOCK_TARGET_SEC_HI(event), 3578 upper_16_bits(start_sec)); 3579 3580 lanphy_write_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, 3581 LAN8814_PTP_CLOCK_TARGET_NS_LO(event), 3582 lower_16_bits(start_nsec)); 3583 lanphy_write_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, 3584 LAN8814_PTP_CLOCK_TARGET_NS_HI(event), 3585 upper_16_bits(start_nsec) & 0x3fff); 3586 } 3587 3588 static void lan8814_ptp_update_target(struct phy_device *phydev, time64_t sec) 3589 { 3590 lan8814_ptp_set_target(phydev, LAN8814_EVENT_A, 3591 sec + LAN8814_BUFFER_TIME, 0); 3592 lan8814_ptp_set_target(phydev, LAN8814_EVENT_B, 3593 sec + LAN8814_BUFFER_TIME, 0); 3594 } 3595 3596 static void lan8814_ptp_clock_step(struct phy_device *phydev, 3597 s64 time_step_ns) 3598 { 3599 u32 nano_seconds_step; 3600 u64 abs_time_step_ns; 3601 time64_t set_seconds; 3602 u32 nano_seconds; 3603 u32 remainder; 3604 s32 seconds; 3605 3606 if (time_step_ns > 15000000000LL) { 3607 /* convert to clock set */ 3608 lan8814_ptp_clock_get(phydev, &set_seconds, &nano_seconds); 3609 set_seconds += div_u64_rem(time_step_ns, 1000000000LL, 3610 &remainder); 3611 nano_seconds += remainder; 3612 if (nano_seconds >= 1000000000) { 3613 set_seconds++; 3614 nano_seconds -= 1000000000; 3615 } 3616 lan8814_ptp_clock_set(phydev, set_seconds, nano_seconds); 3617 lan8814_ptp_update_target(phydev, set_seconds); 3618 return; 3619 } else if (time_step_ns < -15000000000LL) { 3620 /* convert to clock set */ 3621 time_step_ns = -time_step_ns; 3622 3623 lan8814_ptp_clock_get(phydev, &set_seconds, &nano_seconds); 3624 set_seconds -= div_u64_rem(time_step_ns, 1000000000LL, 3625 &remainder); 3626 nano_seconds_step = remainder; 3627 if (nano_seconds < nano_seconds_step) { 3628 set_seconds--; 3629 nano_seconds += 1000000000; 3630 } 3631 nano_seconds -= nano_seconds_step; 3632 lan8814_ptp_clock_set(phydev, set_seconds, nano_seconds); 3633 lan8814_ptp_update_target(phydev, set_seconds); 3634 return; 3635 } 3636 3637 /* do clock step */ 3638 if (time_step_ns >= 0) { 3639 abs_time_step_ns = (u64)time_step_ns; 3640 seconds = (s32)div_u64_rem(abs_time_step_ns, 1000000000, 3641 &remainder); 3642 nano_seconds = remainder; 3643 } else { 3644 abs_time_step_ns = (u64)(-time_step_ns); 3645 seconds = -((s32)div_u64_rem(abs_time_step_ns, 1000000000, 3646 &remainder)); 3647 nano_seconds = remainder; 3648 if (nano_seconds > 0) { 3649 /* subtracting nano seconds is not allowed 3650 * convert to subtracting from seconds, 3651 * and adding to nanoseconds 3652 */ 3653 seconds--; 3654 nano_seconds = (1000000000 - nano_seconds); 3655 } 3656 } 3657 3658 if (nano_seconds > 0) { 3659 /* add 8 ns to cover the likely normal increment */ 3660 nano_seconds += 8; 3661 } 3662 3663 if (nano_seconds >= 1000000000) { 3664 /* carry into seconds */ 3665 seconds++; 3666 nano_seconds -= 1000000000; 3667 } 3668 3669 while (seconds) { 3670 u32 nsec; 3671 3672 if (seconds > 0) { 3673 u32 adjustment_value = (u32)seconds; 3674 u16 adjustment_value_lo, adjustment_value_hi; 3675 3676 if (adjustment_value > 0xF) 3677 adjustment_value = 0xF; 3678 3679 adjustment_value_lo = adjustment_value & 0xffff; 3680 adjustment_value_hi = (adjustment_value >> 16) & 0x3fff; 3681 3682 lanphy_write_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, 3683 PTP_LTC_STEP_ADJ_LO, 3684 adjustment_value_lo); 3685 lanphy_write_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, 3686 PTP_LTC_STEP_ADJ_HI, 3687 PTP_LTC_STEP_ADJ_DIR_ | 3688 adjustment_value_hi); 3689 seconds -= ((s32)adjustment_value); 3690 3691 lan8814_ptp_clock_get(phydev, &set_seconds, &nsec); 3692 set_seconds -= adjustment_value; 3693 lan8814_ptp_update_target(phydev, set_seconds); 3694 } else { 3695 u32 adjustment_value = (u32)(-seconds); 3696 u16 adjustment_value_lo, adjustment_value_hi; 3697 3698 if (adjustment_value > 0xF) 3699 adjustment_value = 0xF; 3700 3701 adjustment_value_lo = adjustment_value & 0xffff; 3702 adjustment_value_hi = (adjustment_value >> 16) & 0x3fff; 3703 3704 lanphy_write_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, 3705 PTP_LTC_STEP_ADJ_LO, 3706 adjustment_value_lo); 3707 lanphy_write_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, 3708 PTP_LTC_STEP_ADJ_HI, 3709 adjustment_value_hi); 3710 seconds += ((s32)adjustment_value); 3711 3712 lan8814_ptp_clock_get(phydev, &set_seconds, &nsec); 3713 set_seconds += adjustment_value; 3714 lan8814_ptp_update_target(phydev, set_seconds); 3715 } 3716 lanphy_write_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, 3717 PTP_CMD_CTL, PTP_CMD_CTL_PTP_LTC_STEP_SEC_); 3718 } 3719 if (nano_seconds) { 3720 u16 nano_seconds_lo; 3721 u16 nano_seconds_hi; 3722 3723 nano_seconds_lo = nano_seconds & 0xffff; 3724 nano_seconds_hi = (nano_seconds >> 16) & 0x3fff; 3725 3726 lanphy_write_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, 3727 PTP_LTC_STEP_ADJ_LO, 3728 nano_seconds_lo); 3729 lanphy_write_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, 3730 PTP_LTC_STEP_ADJ_HI, 3731 PTP_LTC_STEP_ADJ_DIR_ | 3732 nano_seconds_hi); 3733 lanphy_write_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, PTP_CMD_CTL, 3734 PTP_CMD_CTL_PTP_LTC_STEP_NSEC_); 3735 } 3736 } 3737 3738 static int lan8814_ptpci_adjtime(struct ptp_clock_info *ptpci, s64 delta) 3739 { 3740 struct lan8814_shared_priv *shared = container_of(ptpci, struct lan8814_shared_priv, 3741 ptp_clock_info); 3742 struct phy_device *phydev = shared->phydev; 3743 3744 mutex_lock(&shared->shared_lock); 3745 lan8814_ptp_clock_step(phydev, delta); 3746 mutex_unlock(&shared->shared_lock); 3747 3748 return 0; 3749 } 3750 3751 static int lan8814_ptpci_adjfine(struct ptp_clock_info *ptpci, long scaled_ppm) 3752 { 3753 struct lan8814_shared_priv *shared = container_of(ptpci, struct lan8814_shared_priv, 3754 ptp_clock_info); 3755 struct phy_device *phydev = shared->phydev; 3756 u16 kszphy_rate_adj_lo, kszphy_rate_adj_hi; 3757 bool positive = true; 3758 u32 kszphy_rate_adj; 3759 3760 if (scaled_ppm < 0) { 3761 scaled_ppm = -scaled_ppm; 3762 positive = false; 3763 } 3764 3765 kszphy_rate_adj = LAN8814_1PPM_FORMAT * (scaled_ppm >> 16); 3766 kszphy_rate_adj += (LAN8814_1PPM_FORMAT * (0xffff & scaled_ppm)) >> 16; 3767 3768 kszphy_rate_adj_lo = kszphy_rate_adj & 0xffff; 3769 kszphy_rate_adj_hi = (kszphy_rate_adj >> 16) & 0x3fff; 3770 3771 if (positive) 3772 kszphy_rate_adj_hi |= PTP_CLOCK_RATE_ADJ_DIR_; 3773 3774 mutex_lock(&shared->shared_lock); 3775 lanphy_write_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, PTP_CLOCK_RATE_ADJ_HI, 3776 kszphy_rate_adj_hi); 3777 lanphy_write_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, PTP_CLOCK_RATE_ADJ_LO, 3778 kszphy_rate_adj_lo); 3779 mutex_unlock(&shared->shared_lock); 3780 3781 return 0; 3782 } 3783 3784 static void lan8814_ptp_set_reload(struct phy_device *phydev, int event, 3785 s64 period_sec, u32 period_nsec) 3786 { 3787 lanphy_write_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, 3788 LAN8814_PTP_CLOCK_TARGET_RELOAD_SEC_LO(event), 3789 lower_16_bits(period_sec)); 3790 lanphy_write_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, 3791 LAN8814_PTP_CLOCK_TARGET_RELOAD_SEC_HI(event), 3792 upper_16_bits(period_sec)); 3793 3794 lanphy_write_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, 3795 LAN8814_PTP_CLOCK_TARGET_RELOAD_NS_LO(event), 3796 lower_16_bits(period_nsec)); 3797 lanphy_write_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, 3798 LAN8814_PTP_CLOCK_TARGET_RELOAD_NS_HI(event), 3799 upper_16_bits(period_nsec) & 0x3fff); 3800 } 3801 3802 static void lan8814_ptp_enable_event(struct phy_device *phydev, int event, 3803 int pulse_width) 3804 { 3805 /* Set the pulse width of the event, 3806 * Make sure that the target clock will be incremented each time when 3807 * local time reaches or pass it 3808 * Set the polarity high 3809 */ 3810 lanphy_modify_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, LAN8814_PTP_GENERAL_CONFIG, 3811 LAN8814_PTP_GENERAL_CONFIG_LTC_EVENT_MASK(event) | 3812 LAN8814_PTP_GENERAL_CONFIG_LTC_EVENT_SET(event, pulse_width) | 3813 LAN8814_PTP_GENERAL_CONFIG_RELOAD_ADD_X(event) | 3814 LAN8814_PTP_GENERAL_CONFIG_POLARITY_X(event), 3815 LAN8814_PTP_GENERAL_CONFIG_LTC_EVENT_SET(event, pulse_width) | 3816 LAN8814_PTP_GENERAL_CONFIG_POLARITY_X(event)); 3817 } 3818 3819 static void lan8814_ptp_disable_event(struct phy_device *phydev, int event) 3820 { 3821 /* Set target to too far in the future, effectively disabling it */ 3822 lan8814_ptp_set_target(phydev, event, 0xFFFFFFFF, 0); 3823 3824 /* And then reload once it reaches the target */ 3825 lanphy_modify_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, LAN8814_PTP_GENERAL_CONFIG, 3826 LAN8814_PTP_GENERAL_CONFIG_RELOAD_ADD_X(event), 3827 LAN8814_PTP_GENERAL_CONFIG_RELOAD_ADD_X(event)); 3828 } 3829 3830 static void lan8814_ptp_perout_off(struct phy_device *phydev, int pin) 3831 { 3832 /* Disable gpio alternate function, 3833 * 1: select as gpio, 3834 * 0: select alt func 3835 */ 3836 lanphy_modify_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, 3837 LAN8814_GPIO_EN_ADDR(pin), 3838 LAN8814_GPIO_EN_BIT(pin), 3839 LAN8814_GPIO_EN_BIT(pin)); 3840 3841 lanphy_modify_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, 3842 LAN8814_GPIO_DIR_ADDR(pin), 3843 LAN8814_GPIO_DIR_BIT(pin), 3844 0); 3845 3846 lanphy_modify_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, 3847 LAN8814_GPIO_BUF_ADDR(pin), 3848 LAN8814_GPIO_BUF_BIT(pin), 3849 0); 3850 } 3851 3852 static void lan8814_ptp_perout_on(struct phy_device *phydev, int pin) 3853 { 3854 /* Set as gpio output */ 3855 lanphy_modify_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, 3856 LAN8814_GPIO_DIR_ADDR(pin), 3857 LAN8814_GPIO_DIR_BIT(pin), 3858 LAN8814_GPIO_DIR_BIT(pin)); 3859 3860 /* Enable gpio 0:for alternate function, 1:gpio */ 3861 lanphy_modify_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, 3862 LAN8814_GPIO_EN_ADDR(pin), 3863 LAN8814_GPIO_EN_BIT(pin), 3864 0); 3865 3866 /* Set buffer type to push pull */ 3867 lanphy_modify_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, 3868 LAN8814_GPIO_BUF_ADDR(pin), 3869 LAN8814_GPIO_BUF_BIT(pin), 3870 LAN8814_GPIO_BUF_BIT(pin)); 3871 } 3872 3873 static int lan8814_ptp_perout(struct ptp_clock_info *ptpci, 3874 struct ptp_clock_request *rq, int on) 3875 { 3876 struct lan8814_shared_priv *shared = container_of(ptpci, struct lan8814_shared_priv, 3877 ptp_clock_info); 3878 struct phy_device *phydev = shared->phydev; 3879 struct timespec64 ts_on, ts_period; 3880 s64 on_nsec, period_nsec; 3881 int pulse_width; 3882 int pin, event; 3883 3884 mutex_lock(&shared->shared_lock); 3885 event = rq->perout.index; 3886 pin = ptp_find_pin(shared->ptp_clock, PTP_PF_PEROUT, event); 3887 if (pin < 0 || pin >= LAN8814_PTP_PEROUT_NUM) { 3888 mutex_unlock(&shared->shared_lock); 3889 return -EBUSY; 3890 } 3891 3892 if (!on) { 3893 lan8814_ptp_perout_off(phydev, pin); 3894 lan8814_ptp_disable_event(phydev, event); 3895 mutex_unlock(&shared->shared_lock); 3896 return 0; 3897 } 3898 3899 ts_on.tv_sec = rq->perout.on.sec; 3900 ts_on.tv_nsec = rq->perout.on.nsec; 3901 on_nsec = timespec64_to_ns(&ts_on); 3902 3903 ts_period.tv_sec = rq->perout.period.sec; 3904 ts_period.tv_nsec = rq->perout.period.nsec; 3905 period_nsec = timespec64_to_ns(&ts_period); 3906 3907 if (period_nsec < 200) { 3908 pr_warn_ratelimited("%s: perout period too small, minimum is 200 nsec\n", 3909 phydev_name(phydev)); 3910 mutex_unlock(&shared->shared_lock); 3911 return -EOPNOTSUPP; 3912 } 3913 3914 if (on_nsec >= period_nsec) { 3915 pr_warn_ratelimited("%s: pulse width must be smaller than period\n", 3916 phydev_name(phydev)); 3917 mutex_unlock(&shared->shared_lock); 3918 return -EINVAL; 3919 } 3920 3921 switch (on_nsec) { 3922 case 200000000: 3923 pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_200MS; 3924 break; 3925 case 100000000: 3926 pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100MS; 3927 break; 3928 case 50000000: 3929 pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_50MS; 3930 break; 3931 case 10000000: 3932 pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_10MS; 3933 break; 3934 case 5000000: 3935 pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_5MS; 3936 break; 3937 case 1000000: 3938 pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_1MS; 3939 break; 3940 case 500000: 3941 pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_500US; 3942 break; 3943 case 100000: 3944 pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100US; 3945 break; 3946 case 50000: 3947 pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_50US; 3948 break; 3949 case 10000: 3950 pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_10US; 3951 break; 3952 case 5000: 3953 pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_5US; 3954 break; 3955 case 1000: 3956 pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_1US; 3957 break; 3958 case 500: 3959 pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_500NS; 3960 break; 3961 case 100: 3962 pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100NS; 3963 break; 3964 default: 3965 pr_warn_ratelimited("%s: Use default duty cycle of 100ns\n", 3966 phydev_name(phydev)); 3967 pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100NS; 3968 break; 3969 } 3970 3971 /* Configure to pulse every period */ 3972 lan8814_ptp_enable_event(phydev, event, pulse_width); 3973 lan8814_ptp_set_target(phydev, event, rq->perout.start.sec, 3974 rq->perout.start.nsec); 3975 lan8814_ptp_set_reload(phydev, event, rq->perout.period.sec, 3976 rq->perout.period.nsec); 3977 lan8814_ptp_perout_on(phydev, pin); 3978 mutex_unlock(&shared->shared_lock); 3979 3980 return 0; 3981 } 3982 3983 static void lan8814_ptp_extts_on(struct phy_device *phydev, int pin, u32 flags) 3984 { 3985 /* Set as gpio input */ 3986 lanphy_modify_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, 3987 LAN8814_GPIO_DIR_ADDR(pin), 3988 LAN8814_GPIO_DIR_BIT(pin), 3989 0); 3990 3991 /* Map the pin to ltc pin 0 of the capture map registers */ 3992 lanphy_modify_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, 3993 PTP_GPIO_CAP_MAP_LO, pin, pin); 3994 3995 /* Enable capture on the edges of the ltc pin */ 3996 if (flags & PTP_RISING_EDGE) 3997 lanphy_modify_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, 3998 PTP_GPIO_CAP_EN, 3999 PTP_GPIO_CAP_EN_GPIO_RE_CAPTURE_ENABLE(0), 4000 PTP_GPIO_CAP_EN_GPIO_RE_CAPTURE_ENABLE(0)); 4001 if (flags & PTP_FALLING_EDGE) 4002 lanphy_modify_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, 4003 PTP_GPIO_CAP_EN, 4004 PTP_GPIO_CAP_EN_GPIO_FE_CAPTURE_ENABLE(0), 4005 PTP_GPIO_CAP_EN_GPIO_FE_CAPTURE_ENABLE(0)); 4006 4007 /* Enable interrupt top interrupt */ 4008 lanphy_modify_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, PTP_COMMON_INT_ENA, 4009 PTP_COMMON_INT_ENA_GPIO_CAP_EN, 4010 PTP_COMMON_INT_ENA_GPIO_CAP_EN); 4011 } 4012 4013 static void lan8814_ptp_extts_off(struct phy_device *phydev, int pin) 4014 { 4015 /* Set as gpio out */ 4016 lanphy_modify_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, 4017 LAN8814_GPIO_DIR_ADDR(pin), 4018 LAN8814_GPIO_DIR_BIT(pin), 4019 LAN8814_GPIO_DIR_BIT(pin)); 4020 4021 /* Enable alternate, 0:for alternate function, 1:gpio */ 4022 lanphy_modify_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, 4023 LAN8814_GPIO_EN_ADDR(pin), 4024 LAN8814_GPIO_EN_BIT(pin), 4025 0); 4026 4027 /* Clear the mapping of pin to registers 0 of the capture registers */ 4028 lanphy_modify_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, 4029 PTP_GPIO_CAP_MAP_LO, 4030 GENMASK(3, 0), 4031 0); 4032 4033 /* Disable capture on both of the edges */ 4034 lanphy_modify_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, PTP_GPIO_CAP_EN, 4035 PTP_GPIO_CAP_EN_GPIO_RE_CAPTURE_ENABLE(pin) | 4036 PTP_GPIO_CAP_EN_GPIO_FE_CAPTURE_ENABLE(pin), 4037 0); 4038 4039 /* Disable interrupt top interrupt */ 4040 lanphy_modify_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, PTP_COMMON_INT_ENA, 4041 PTP_COMMON_INT_ENA_GPIO_CAP_EN, 4042 0); 4043 } 4044 4045 static int lan8814_ptp_extts(struct ptp_clock_info *ptpci, 4046 struct ptp_clock_request *rq, int on) 4047 { 4048 struct lan8814_shared_priv *shared = container_of(ptpci, struct lan8814_shared_priv, 4049 ptp_clock_info); 4050 struct phy_device *phydev = shared->phydev; 4051 int pin; 4052 4053 pin = ptp_find_pin(shared->ptp_clock, PTP_PF_EXTTS, 4054 rq->extts.index); 4055 if (pin == -1 || pin != LAN8814_PTP_EXTTS_NUM) 4056 return -EINVAL; 4057 4058 mutex_lock(&shared->shared_lock); 4059 if (on) 4060 lan8814_ptp_extts_on(phydev, pin, rq->extts.flags); 4061 else 4062 lan8814_ptp_extts_off(phydev, pin); 4063 4064 mutex_unlock(&shared->shared_lock); 4065 4066 return 0; 4067 } 4068 4069 static int lan8814_ptpci_enable(struct ptp_clock_info *ptpci, 4070 struct ptp_clock_request *rq, int on) 4071 { 4072 switch (rq->type) { 4073 case PTP_CLK_REQ_PEROUT: 4074 return lan8814_ptp_perout(ptpci, rq, on); 4075 case PTP_CLK_REQ_EXTTS: 4076 return lan8814_ptp_extts(ptpci, rq, on); 4077 default: 4078 return -EINVAL; 4079 } 4080 } 4081 4082 static int lan8814_ptpci_verify(struct ptp_clock_info *ptp, unsigned int pin, 4083 enum ptp_pin_function func, unsigned int chan) 4084 { 4085 switch (func) { 4086 case PTP_PF_NONE: 4087 case PTP_PF_PEROUT: 4088 /* Only pins 0 and 1 can generate perout signals. And for pin 0 4089 * there is only chan 0 (event A) and for pin 1 there is only 4090 * chan 1 (event B) 4091 */ 4092 if (pin >= LAN8814_PTP_PEROUT_NUM || pin != chan) 4093 return -1; 4094 break; 4095 case PTP_PF_EXTTS: 4096 if (pin != LAN8814_PTP_EXTTS_NUM) 4097 return -1; 4098 break; 4099 default: 4100 return -1; 4101 } 4102 4103 return 0; 4104 } 4105 4106 static bool lan8814_get_sig_tx(struct sk_buff *skb, u16 *sig) 4107 { 4108 struct ptp_header *ptp_header; 4109 u32 type; 4110 4111 type = ptp_classify_raw(skb); 4112 ptp_header = ptp_parse_header(skb, type); 4113 4114 if (!ptp_header) 4115 return false; 4116 4117 *sig = (__force u16)(ntohs(ptp_header->sequence_id)); 4118 return true; 4119 } 4120 4121 static void lan8814_match_tx_skb(struct kszphy_ptp_priv *ptp_priv, 4122 u32 seconds, u32 nsec, u16 seq_id) 4123 { 4124 struct skb_shared_hwtstamps shhwtstamps; 4125 struct sk_buff *skb, *skb_tmp; 4126 unsigned long flags; 4127 bool ret = false; 4128 u16 skb_sig; 4129 4130 spin_lock_irqsave(&ptp_priv->tx_queue.lock, flags); 4131 skb_queue_walk_safe(&ptp_priv->tx_queue, skb, skb_tmp) { 4132 if (!lan8814_get_sig_tx(skb, &skb_sig)) 4133 continue; 4134 4135 if (memcmp(&skb_sig, &seq_id, sizeof(seq_id))) 4136 continue; 4137 4138 __skb_unlink(skb, &ptp_priv->tx_queue); 4139 ret = true; 4140 break; 4141 } 4142 spin_unlock_irqrestore(&ptp_priv->tx_queue.lock, flags); 4143 4144 if (ret) { 4145 memset(&shhwtstamps, 0, sizeof(shhwtstamps)); 4146 shhwtstamps.hwtstamp = ktime_set(seconds, nsec); 4147 skb_complete_tx_timestamp(skb, &shhwtstamps); 4148 } 4149 } 4150 4151 static void lan8814_dequeue_tx_skb(struct kszphy_ptp_priv *ptp_priv) 4152 { 4153 struct phy_device *phydev = ptp_priv->phydev; 4154 u32 seconds, nsec; 4155 u16 seq_id; 4156 4157 lan8814_ptp_tx_ts_get(phydev, &seconds, &nsec, &seq_id); 4158 lan8814_match_tx_skb(ptp_priv, seconds, nsec, seq_id); 4159 } 4160 4161 static void lan8814_get_tx_ts(struct kszphy_ptp_priv *ptp_priv) 4162 { 4163 struct phy_device *phydev = ptp_priv->phydev; 4164 u32 reg; 4165 4166 do { 4167 lan8814_dequeue_tx_skb(ptp_priv); 4168 4169 /* If other timestamps are available in the FIFO, 4170 * process them. 4171 */ 4172 reg = lanphy_read_page_reg(phydev, LAN8814_PAGE_PORT_REGS, 4173 PTP_CAP_INFO); 4174 } while (PTP_CAP_INFO_TX_TS_CNT_GET_(reg) > 0); 4175 } 4176 4177 static bool lan8814_match_skb(struct kszphy_ptp_priv *ptp_priv, 4178 struct lan8814_ptp_rx_ts *rx_ts) 4179 { 4180 struct skb_shared_hwtstamps *shhwtstamps; 4181 struct sk_buff *skb, *skb_tmp; 4182 unsigned long flags; 4183 bool ret = false; 4184 u16 skb_sig; 4185 4186 spin_lock_irqsave(&ptp_priv->rx_queue.lock, flags); 4187 skb_queue_walk_safe(&ptp_priv->rx_queue, skb, skb_tmp) { 4188 if (!lan8814_get_sig_rx(skb, &skb_sig)) 4189 continue; 4190 4191 if (memcmp(&skb_sig, &rx_ts->seq_id, sizeof(rx_ts->seq_id))) 4192 continue; 4193 4194 __skb_unlink(skb, &ptp_priv->rx_queue); 4195 4196 ret = true; 4197 break; 4198 } 4199 spin_unlock_irqrestore(&ptp_priv->rx_queue.lock, flags); 4200 4201 if (ret) { 4202 shhwtstamps = skb_hwtstamps(skb); 4203 memset(shhwtstamps, 0, sizeof(*shhwtstamps)); 4204 shhwtstamps->hwtstamp = ktime_set(rx_ts->seconds, rx_ts->nsec); 4205 netif_rx(skb); 4206 } 4207 4208 return ret; 4209 } 4210 4211 static void lan8814_match_rx_ts(struct kszphy_ptp_priv *ptp_priv, 4212 struct lan8814_ptp_rx_ts *rx_ts) 4213 { 4214 unsigned long flags; 4215 4216 /* If we failed to match the skb add it to the queue for when 4217 * the frame will come 4218 */ 4219 if (!lan8814_match_skb(ptp_priv, rx_ts)) { 4220 spin_lock_irqsave(&ptp_priv->rx_ts_lock, flags); 4221 list_add(&rx_ts->list, &ptp_priv->rx_ts_list); 4222 spin_unlock_irqrestore(&ptp_priv->rx_ts_lock, flags); 4223 } else { 4224 kfree(rx_ts); 4225 } 4226 } 4227 4228 static void lan8814_get_rx_ts(struct kszphy_ptp_priv *ptp_priv) 4229 { 4230 struct phy_device *phydev = ptp_priv->phydev; 4231 struct lan8814_ptp_rx_ts *rx_ts; 4232 u32 reg; 4233 4234 do { 4235 rx_ts = kzalloc_obj(*rx_ts); 4236 if (!rx_ts) 4237 return; 4238 4239 lan8814_ptp_rx_ts_get(phydev, &rx_ts->seconds, &rx_ts->nsec, 4240 &rx_ts->seq_id); 4241 lan8814_match_rx_ts(ptp_priv, rx_ts); 4242 4243 /* If other timestamps are available in the FIFO, 4244 * process them. 4245 */ 4246 reg = lanphy_read_page_reg(phydev, LAN8814_PAGE_PORT_REGS, 4247 PTP_CAP_INFO); 4248 } while (PTP_CAP_INFO_RX_TS_CNT_GET_(reg) > 0); 4249 } 4250 4251 static void lan8814_handle_ptp_interrupt(struct phy_device *phydev, u16 status) 4252 { 4253 struct kszphy_priv *priv = phydev->priv; 4254 struct kszphy_ptp_priv *ptp_priv = &priv->ptp_priv; 4255 4256 if (status & PTP_TSU_INT_STS_PTP_TX_TS_EN_) 4257 lan8814_get_tx_ts(ptp_priv); 4258 4259 if (status & PTP_TSU_INT_STS_PTP_RX_TS_EN_) 4260 lan8814_get_rx_ts(ptp_priv); 4261 4262 if (status & PTP_TSU_INT_STS_PTP_TX_TS_OVRFL_INT_) { 4263 lan8814_flush_fifo(phydev, true); 4264 skb_queue_purge(&ptp_priv->tx_queue); 4265 } 4266 4267 if (status & PTP_TSU_INT_STS_PTP_RX_TS_OVRFL_INT_) { 4268 lan8814_flush_fifo(phydev, false); 4269 skb_queue_purge(&ptp_priv->rx_queue); 4270 } 4271 } 4272 4273 static int lan8814_gpio_process_cap(struct lan8814_shared_priv *shared) 4274 { 4275 struct phy_device *phydev = shared->phydev; 4276 struct ptp_clock_event ptp_event = {0}; 4277 unsigned long nsec; 4278 s64 sec; 4279 u16 tmp; 4280 4281 /* This is 0 because whatever was the input pin it was mapped it to 4282 * ltc gpio pin 0 4283 */ 4284 lanphy_modify_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, PTP_GPIO_SEL, 4285 PTP_GPIO_SEL_GPIO_SEL(0), 4286 PTP_GPIO_SEL_GPIO_SEL(0)); 4287 4288 tmp = lanphy_read_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, 4289 PTP_GPIO_CAP_STS); 4290 if (!(tmp & PTP_GPIO_CAP_STS_PTP_GPIO_RE_STS(0)) && 4291 !(tmp & PTP_GPIO_CAP_STS_PTP_GPIO_FE_STS(0))) 4292 return -1; 4293 4294 if (tmp & BIT(0)) { 4295 sec = lanphy_read_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, 4296 PTP_GPIO_RE_LTC_SEC_HI_CAP); 4297 sec <<= 16; 4298 sec |= lanphy_read_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, 4299 PTP_GPIO_RE_LTC_SEC_LO_CAP); 4300 4301 nsec = lanphy_read_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, 4302 PTP_GPIO_RE_LTC_NS_HI_CAP) & 0x3fff; 4303 nsec <<= 16; 4304 nsec |= lanphy_read_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, 4305 PTP_GPIO_RE_LTC_NS_LO_CAP); 4306 } else { 4307 sec = lanphy_read_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, 4308 PTP_GPIO_FE_LTC_SEC_HI_CAP); 4309 sec <<= 16; 4310 sec |= lanphy_read_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, 4311 PTP_GPIO_FE_LTC_SEC_LO_CAP); 4312 4313 nsec = lanphy_read_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, 4314 PTP_GPIO_FE_LTC_NS_HI_CAP) & 0x3fff; 4315 nsec <<= 16; 4316 nsec |= lanphy_read_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, 4317 PTP_GPIO_RE_LTC_NS_LO_CAP); 4318 } 4319 4320 ptp_event.index = 0; 4321 ptp_event.timestamp = ktime_set(sec, nsec); 4322 ptp_event.type = PTP_CLOCK_EXTTS; 4323 ptp_clock_event(shared->ptp_clock, &ptp_event); 4324 4325 return 0; 4326 } 4327 4328 static int lan8814_handle_gpio_interrupt(struct phy_device *phydev, u16 status) 4329 { 4330 struct lan8814_shared_priv *shared = phy_package_get_priv(phydev); 4331 int ret; 4332 4333 mutex_lock(&shared->shared_lock); 4334 ret = lan8814_gpio_process_cap(shared); 4335 mutex_unlock(&shared->shared_lock); 4336 4337 return ret; 4338 } 4339 4340 static int lan8804_config_init(struct phy_device *phydev) 4341 { 4342 /* MDI-X setting for swap A,B transmit */ 4343 lanphy_modify_page_reg(phydev, LAN8814_PAGE_PCS_DIGITAL, LAN8804_ALIGN_SWAP, 4344 LAN8804_ALIGN_TX_A_B_SWAP_MASK, 4345 LAN8804_ALIGN_TX_A_B_SWAP); 4346 4347 /* Make sure that the PHY will not stop generating the clock when the 4348 * link partner goes down 4349 */ 4350 lanphy_write_page_reg(phydev, LAN8814_PAGE_SYSTEM_CTRL, 4351 LAN8814_CLOCK_MANAGEMENT, 0x27e); 4352 lanphy_read_page_reg(phydev, LAN8814_PAGE_AFE_PMA, LAN8814_LINK_QUALITY); 4353 4354 return 0; 4355 } 4356 4357 static irqreturn_t lan8804_handle_interrupt(struct phy_device *phydev) 4358 { 4359 int status; 4360 4361 status = phy_read(phydev, LAN8814_INTS); 4362 if (status < 0) { 4363 phy_error(phydev); 4364 return IRQ_NONE; 4365 } 4366 4367 if (status > 0) 4368 phy_trigger_machine(phydev); 4369 4370 return IRQ_HANDLED; 4371 } 4372 4373 #define LAN8804_OUTPUT_CONTROL 25 4374 #define LAN8804_OUTPUT_CONTROL_INTR_BUFFER BIT(14) 4375 #define LAN8804_CONTROL 31 4376 #define LAN8804_CONTROL_INTR_POLARITY BIT(14) 4377 4378 static int lan8804_config_intr(struct phy_device *phydev) 4379 { 4380 int err; 4381 4382 /* This is an internal PHY of lan966x and is not possible to change the 4383 * polarity on the GIC found in lan966x, therefore change the polarity 4384 * of the interrupt in the PHY from being active low instead of active 4385 * high. 4386 */ 4387 phy_write(phydev, LAN8804_CONTROL, LAN8804_CONTROL_INTR_POLARITY); 4388 4389 /* By default interrupt buffer is open-drain in which case the interrupt 4390 * can be active only low. Therefore change the interrupt buffer to be 4391 * push-pull to be able to change interrupt polarity 4392 */ 4393 phy_write(phydev, LAN8804_OUTPUT_CONTROL, 4394 LAN8804_OUTPUT_CONTROL_INTR_BUFFER); 4395 4396 if (phydev->interrupts == PHY_INTERRUPT_ENABLED) { 4397 err = phy_read(phydev, LAN8814_INTS); 4398 if (err < 0) 4399 return err; 4400 4401 err = phy_write(phydev, LAN8814_INTC, LAN8814_INT_LINK); 4402 if (err) 4403 return err; 4404 } else { 4405 err = phy_write(phydev, LAN8814_INTC, 0); 4406 if (err) 4407 return err; 4408 4409 err = phy_read(phydev, LAN8814_INTS); 4410 if (err < 0) 4411 return err; 4412 } 4413 4414 return 0; 4415 } 4416 4417 /* Check if the PHY has 1588 support. There are multiple skus of the PHY and 4418 * some of them support PTP while others don't support it. This function will 4419 * return true is the sku supports it, otherwise will return false. 4420 */ 4421 static bool lan8814_has_ptp(struct phy_device *phydev) 4422 { 4423 struct kszphy_priv *priv = phydev->priv; 4424 4425 return priv->is_ptp_available; 4426 } 4427 4428 static irqreturn_t lan8814_handle_interrupt(struct phy_device *phydev) 4429 { 4430 int ret = IRQ_NONE; 4431 int irq_status; 4432 4433 irq_status = phy_read(phydev, LAN8814_INTS); 4434 if (irq_status < 0) { 4435 phy_error(phydev); 4436 return IRQ_NONE; 4437 } 4438 4439 if (irq_status & LAN8814_INT_LINK) { 4440 phy_trigger_machine(phydev); 4441 ret = IRQ_HANDLED; 4442 } 4443 4444 if (!lan8814_has_ptp(phydev)) 4445 return ret; 4446 4447 while (true) { 4448 irq_status = lanphy_read_page_reg(phydev, LAN8814_PAGE_PORT_REGS, 4449 PTP_TSU_INT_STS); 4450 if (!irq_status) 4451 break; 4452 4453 lan8814_handle_ptp_interrupt(phydev, irq_status); 4454 ret = IRQ_HANDLED; 4455 } 4456 4457 if (!lan8814_handle_gpio_interrupt(phydev, irq_status)) 4458 ret = IRQ_HANDLED; 4459 4460 return ret; 4461 } 4462 4463 static int lan8814_ack_interrupt(struct phy_device *phydev) 4464 { 4465 /* bit[12..0] int status, which is a read and clear register. */ 4466 int rc; 4467 4468 rc = phy_read(phydev, LAN8814_INTS); 4469 4470 return (rc < 0) ? rc : 0; 4471 } 4472 4473 static int lan8814_config_intr(struct phy_device *phydev) 4474 { 4475 int err; 4476 4477 lanphy_write_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, LAN8814_INTR_CTRL_REG, 4478 LAN8814_INTR_CTRL_REG_POLARITY | 4479 LAN8814_INTR_CTRL_REG_INTR_ENABLE); 4480 4481 /* enable / disable interrupts */ 4482 if (phydev->interrupts == PHY_INTERRUPT_ENABLED) { 4483 err = lan8814_ack_interrupt(phydev); 4484 if (err) 4485 return err; 4486 4487 err = phy_write(phydev, LAN8814_INTC, LAN8814_INT_LINK); 4488 } else { 4489 err = phy_write(phydev, LAN8814_INTC, 0); 4490 if (err) 4491 return err; 4492 4493 err = lan8814_ack_interrupt(phydev); 4494 } 4495 4496 return err; 4497 } 4498 4499 static void lan8814_ptp_init(struct phy_device *phydev) 4500 { 4501 struct kszphy_priv *priv = phydev->priv; 4502 struct kszphy_ptp_priv *ptp_priv = &priv->ptp_priv; 4503 4504 if (!IS_ENABLED(CONFIG_PTP_1588_CLOCK) || 4505 !IS_ENABLED(CONFIG_NETWORK_PHY_TIMESTAMPING)) 4506 return; 4507 4508 if (!lan8814_has_ptp(phydev)) 4509 return; 4510 4511 lanphy_write_page_reg(phydev, LAN8814_PAGE_PORT_REGS, 4512 TSU_HARD_RESET, TSU_HARD_RESET_); 4513 4514 lanphy_modify_page_reg(phydev, LAN8814_PAGE_PORT_REGS, PTP_TX_MOD, 4515 PTP_TX_MOD_BAD_UDPV4_CHKSUM_FORCE_FCS_DIS_, 4516 PTP_TX_MOD_BAD_UDPV4_CHKSUM_FORCE_FCS_DIS_); 4517 4518 lanphy_modify_page_reg(phydev, LAN8814_PAGE_PORT_REGS, PTP_RX_MOD, 4519 PTP_RX_MOD_BAD_UDPV4_CHKSUM_FORCE_FCS_DIS_, 4520 PTP_RX_MOD_BAD_UDPV4_CHKSUM_FORCE_FCS_DIS_); 4521 4522 lanphy_write_page_reg(phydev, LAN8814_PAGE_PORT_REGS, 4523 PTP_RX_PARSE_CONFIG, 0); 4524 lanphy_write_page_reg(phydev, LAN8814_PAGE_PORT_REGS, 4525 PTP_TX_PARSE_CONFIG, 0); 4526 4527 /* Removing default registers configs related to L2 and IP */ 4528 lanphy_write_page_reg(phydev, LAN8814_PAGE_PORT_REGS, 4529 PTP_TX_PARSE_L2_ADDR_EN, 0); 4530 lanphy_write_page_reg(phydev, LAN8814_PAGE_PORT_REGS, 4531 PTP_RX_PARSE_L2_ADDR_EN, 0); 4532 lanphy_write_page_reg(phydev, LAN8814_PAGE_PORT_REGS, 4533 PTP_TX_PARSE_IP_ADDR_EN, 0); 4534 lanphy_write_page_reg(phydev, LAN8814_PAGE_PORT_REGS, 4535 PTP_RX_PARSE_IP_ADDR_EN, 0); 4536 4537 /* Disable checking for minorVersionPTP field */ 4538 lanphy_write_page_reg(phydev, LAN8814_PAGE_PORT_REGS, PTP_RX_VERSION, 4539 PTP_MAX_VERSION(0xff) | PTP_MIN_VERSION(0x0)); 4540 lanphy_write_page_reg(phydev, LAN8814_PAGE_PORT_REGS, PTP_TX_VERSION, 4541 PTP_MAX_VERSION(0xff) | PTP_MIN_VERSION(0x0)); 4542 4543 skb_queue_head_init(&ptp_priv->tx_queue); 4544 skb_queue_head_init(&ptp_priv->rx_queue); 4545 INIT_LIST_HEAD(&ptp_priv->rx_ts_list); 4546 spin_lock_init(&ptp_priv->rx_ts_lock); 4547 4548 ptp_priv->phydev = phydev; 4549 4550 ptp_priv->mii_ts.rxtstamp = lan8814_rxtstamp; 4551 ptp_priv->mii_ts.txtstamp = lan8814_txtstamp; 4552 ptp_priv->mii_ts.hwtstamp_set = lan8814_hwtstamp_set; 4553 ptp_priv->mii_ts.hwtstamp_get = lan8814_hwtstamp_get; 4554 ptp_priv->mii_ts.ts_info = lan8814_ts_info; 4555 4556 phydev->mii_ts = &ptp_priv->mii_ts; 4557 4558 /* Timestamp selected by default to keep legacy API */ 4559 phydev->default_timestamp = true; 4560 } 4561 4562 static int __lan8814_ptp_probe_once(struct phy_device *phydev, char *pin_name, 4563 int gpios) 4564 { 4565 struct lan8814_shared_priv *shared = phy_package_get_priv(phydev); 4566 4567 shared->phydev = phydev; 4568 4569 /* Initialise shared lock for clock*/ 4570 mutex_init(&shared->shared_lock); 4571 4572 shared->pin_config = devm_kmalloc_objs(&phydev->mdio.dev, 4573 *shared->pin_config, gpios); 4574 if (!shared->pin_config) 4575 return -ENOMEM; 4576 4577 for (int i = 0; i < gpios; i++) { 4578 struct ptp_pin_desc *ptp_pin = &shared->pin_config[i]; 4579 4580 memset(ptp_pin, 0, sizeof(*ptp_pin)); 4581 snprintf(ptp_pin->name, 4582 sizeof(ptp_pin->name), "%s_%02d", pin_name, i); 4583 ptp_pin->index = i; 4584 ptp_pin->func = PTP_PF_NONE; 4585 } 4586 4587 shared->ptp_clock_info.owner = THIS_MODULE; 4588 snprintf(shared->ptp_clock_info.name, 30, "%s", phydev->drv->name); 4589 shared->ptp_clock_info.max_adj = 31249999; 4590 shared->ptp_clock_info.n_alarm = 0; 4591 shared->ptp_clock_info.n_ext_ts = LAN8814_PTP_EXTTS_NUM; 4592 shared->ptp_clock_info.n_pins = gpios; 4593 shared->ptp_clock_info.pps = 0; 4594 shared->ptp_clock_info.supported_extts_flags = PTP_RISING_EDGE | 4595 PTP_FALLING_EDGE | 4596 PTP_STRICT_FLAGS; 4597 shared->ptp_clock_info.supported_perout_flags = PTP_PEROUT_DUTY_CYCLE; 4598 shared->ptp_clock_info.pin_config = shared->pin_config; 4599 shared->ptp_clock_info.n_per_out = LAN8814_PTP_PEROUT_NUM; 4600 shared->ptp_clock_info.adjfine = lan8814_ptpci_adjfine; 4601 shared->ptp_clock_info.adjtime = lan8814_ptpci_adjtime; 4602 shared->ptp_clock_info.gettime64 = lan8814_ptpci_gettime64; 4603 shared->ptp_clock_info.settime64 = lan8814_ptpci_settime64; 4604 shared->ptp_clock_info.getcrosststamp = NULL; 4605 shared->ptp_clock_info.enable = lan8814_ptpci_enable; 4606 shared->ptp_clock_info.verify = lan8814_ptpci_verify; 4607 4608 shared->ptp_clock = ptp_clock_register(&shared->ptp_clock_info, 4609 &phydev->mdio.dev); 4610 if (IS_ERR(shared->ptp_clock)) { 4611 phydev_err(phydev, "ptp_clock_register failed %pe\n", 4612 shared->ptp_clock); 4613 return -EINVAL; 4614 } 4615 4616 /* Check if PHC support is missing at the configuration level */ 4617 if (!shared->ptp_clock) 4618 return 0; 4619 4620 phydev_dbg(phydev, "successfully registered ptp clock\n"); 4621 4622 /* The EP.4 is shared between all the PHYs in the package and also it 4623 * can be accessed by any of the PHYs 4624 */ 4625 lanphy_write_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, 4626 LTC_HARD_RESET, LTC_HARD_RESET_); 4627 lanphy_write_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, PTP_OPERATING_MODE, 4628 PTP_OPERATING_MODE_STANDALONE_); 4629 4630 /* Enable ptp to run LTC clock for ptp and gpio 1PPS operation */ 4631 lanphy_write_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, PTP_CMD_CTL, 4632 PTP_CMD_CTL_PTP_ENABLE_); 4633 4634 return 0; 4635 } 4636 4637 static int lan8814_ptp_probe_once(struct phy_device *phydev) 4638 { 4639 if (!lan8814_has_ptp(phydev)) 4640 return 0; 4641 4642 return __lan8814_ptp_probe_once(phydev, "lan8814_ptp_pin", 4643 LAN8814_PTP_GPIO_NUM); 4644 } 4645 4646 static void lan8814_setup_led(struct phy_device *phydev, int val) 4647 { 4648 int temp; 4649 4650 temp = lanphy_read_page_reg(phydev, LAN8814_PAGE_PORT_REGS, 4651 LAN8814_LED_CTRL_1); 4652 4653 if (val) 4654 temp |= LAN8814_LED_CTRL_1_KSZ9031_LED_MODE_; 4655 else 4656 temp &= ~LAN8814_LED_CTRL_1_KSZ9031_LED_MODE_; 4657 4658 lanphy_write_page_reg(phydev, LAN8814_PAGE_PORT_REGS, 4659 LAN8814_LED_CTRL_1, temp); 4660 } 4661 4662 static int lan8814_config_init(struct phy_device *phydev) 4663 { 4664 struct kszphy_priv *lan8814 = phydev->priv; 4665 int ret; 4666 4667 if (phy_package_init_once(phydev)) 4668 /* Reset the PHY */ 4669 lanphy_modify_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, 4670 LAN8814_QSGMII_SOFT_RESET, 4671 LAN8814_QSGMII_SOFT_RESET_BIT, 4672 LAN8814_QSGMII_SOFT_RESET_BIT); 4673 4674 /* Based on the interface type select how the advertise ability is 4675 * encoded, to set as SGMII or as USGMII. 4676 */ 4677 if (phydev->interface == PHY_INTERFACE_MODE_QSGMII) 4678 ret = lanphy_modify_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, 4679 LAN8814_QSGMII_TX_CONFIG, 4680 LAN8814_QSGMII_TX_CONFIG_QSGMII, 4681 LAN8814_QSGMII_TX_CONFIG_QSGMII); 4682 else 4683 ret = lanphy_modify_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, 4684 LAN8814_QSGMII_TX_CONFIG, 4685 LAN8814_QSGMII_TX_CONFIG_QSGMII, 4686 0); 4687 4688 if (ret < 0) 4689 return ret; 4690 4691 /* MDI-X setting for swap A,B transmit */ 4692 lanphy_modify_page_reg(phydev, LAN8814_PAGE_PCS_DIGITAL, LAN8814_ALIGN_SWAP, 4693 LAN8814_ALIGN_TX_A_B_SWAP_MASK, 4694 LAN8814_ALIGN_TX_A_B_SWAP); 4695 4696 if (lan8814->led_mode >= 0) 4697 lan8814_setup_led(phydev, lan8814->led_mode); 4698 4699 return 0; 4700 } 4701 4702 /* It is expected that there will not be any 'lan8814_take_coma_mode' 4703 * function called in suspend. Because the GPIO line can be shared, so if one of 4704 * the phys goes back in coma mode, then all the other PHYs will go, which is 4705 * wrong. 4706 */ 4707 static int lan8814_release_coma_mode(struct phy_device *phydev) 4708 { 4709 struct gpio_desc *gpiod; 4710 4711 gpiod = devm_gpiod_get_optional(&phydev->mdio.dev, "coma-mode", 4712 GPIOD_OUT_HIGH_OPEN_DRAIN | 4713 GPIOD_FLAGS_BIT_NONEXCLUSIVE); 4714 if (IS_ERR(gpiod)) 4715 return PTR_ERR(gpiod); 4716 4717 gpiod_set_consumer_name(gpiod, "LAN8814 coma mode"); 4718 gpiod_set_value_cansleep(gpiod, 0); 4719 4720 return 0; 4721 } 4722 4723 static void lan8814_clear_2psp_bit(struct phy_device *phydev) 4724 { 4725 /* It was noticed that when traffic is passing through the PHY and the 4726 * cable is removed then the LED was still on even though there is no 4727 * link 4728 */ 4729 lanphy_modify_page_reg(phydev, LAN8814_PAGE_PCS_DIGITAL, LAN8814_EEE_STATE, 4730 LAN8814_EEE_STATE_MASK2P5P, 4731 0); 4732 } 4733 4734 static void lan8814_update_meas_time(struct phy_device *phydev) 4735 { 4736 /* By setting the measure time to a value of 0xb this will allow cables 4737 * longer than 100m to be used. This configuration can be used 4738 * regardless of the mode of operation of the PHY 4739 */ 4740 lanphy_modify_page_reg(phydev, LAN8814_PAGE_AFE_PMA, LAN8814_PD_CONTROLS, 4741 LAN8814_PD_CONTROLS_PD_MEAS_TIME_MASK, 4742 LAN8814_PD_CONTROLS_PD_MEAS_TIME_VAL); 4743 } 4744 4745 static int lan8814_probe(struct phy_device *phydev) 4746 { 4747 const struct kszphy_type *type = phydev->drv->driver_data; 4748 struct kszphy_priv *priv; 4749 u16 addr; 4750 int err; 4751 4752 priv = devm_kzalloc_obj(&phydev->mdio.dev, *priv); 4753 if (!priv) 4754 return -ENOMEM; 4755 4756 phydev->priv = priv; 4757 4758 priv->type = type; 4759 4760 kszphy_parse_led_mode(phydev); 4761 4762 /* Strap-in value for PHY address, below register read gives starting 4763 * phy address value 4764 */ 4765 addr = lanphy_read_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, 0) & 0x1F; 4766 devm_phy_package_join(&phydev->mdio.dev, phydev, 4767 addr, sizeof(struct lan8814_shared_priv)); 4768 4769 /* There are lan8814 SKUs that don't support PTP. Make sure that for 4770 * those skus no PTP device is created. Here we check if the SKU 4771 * supports PTP. 4772 */ 4773 err = lanphy_read_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, 4774 LAN8814_SKUS); 4775 if (err < 0) 4776 return err; 4777 4778 priv->is_ptp_available = err == LAN8814_REV_LAN8814 || 4779 err == LAN8814_REV_LAN8818; 4780 4781 if (phy_package_probe_once(phydev)) { 4782 err = lan8814_release_coma_mode(phydev); 4783 if (err) 4784 return err; 4785 4786 err = lan8814_ptp_probe_once(phydev); 4787 if (err) 4788 return err; 4789 } 4790 4791 lan8814_ptp_init(phydev); 4792 4793 /* Errata workarounds */ 4794 lan8814_clear_2psp_bit(phydev); 4795 lan8814_update_meas_time(phydev); 4796 4797 return 0; 4798 } 4799 4800 #define LAN8841_MMD_TIMER_REG 0 4801 #define LAN8841_MMD0_REGISTER_17 17 4802 #define LAN8841_MMD0_REGISTER_17_DROP_OPT(x) ((x) & 0x3) 4803 #define LAN8841_MMD0_REGISTER_17_XMIT_TOG_TX_DIS BIT(3) 4804 #define LAN8841_OPERATION_MODE_STRAP_OVERRIDE_LOW_REG 2 4805 #define LAN8841_OPERATION_MODE_STRAP_OVERRIDE_LOW_REG_MAGJACK BIT(14) 4806 #define LAN8841_MMD_ANALOG_REG 28 4807 #define LAN8841_ANALOG_CONTROL_1 1 4808 #define LAN8841_ANALOG_CONTROL_1_PLL_TRIM(x) (((x) & 0x3) << 5) 4809 #define LAN8841_ANALOG_CONTROL_10 13 4810 #define LAN8841_ANALOG_CONTROL_10_PLL_DIV(x) ((x) & 0x3) 4811 #define LAN8841_ANALOG_CONTROL_11 14 4812 #define LAN8841_ANALOG_CONTROL_11_LDO_REF(x) (((x) & 0x7) << 12) 4813 #define LAN8841_TX_LOW_I_CH_C_D_POWER_MANAGMENT 69 4814 #define LAN8841_TX_LOW_I_CH_C_D_POWER_MANAGMENT_VAL 0xbffc 4815 #define LAN8841_BTRX_POWER_DOWN 70 4816 #define LAN8841_BTRX_POWER_DOWN_QBIAS_CH_A BIT(0) 4817 #define LAN8841_BTRX_POWER_DOWN_BTRX_CH_A BIT(1) 4818 #define LAN8841_BTRX_POWER_DOWN_QBIAS_CH_B BIT(2) 4819 #define LAN8841_BTRX_POWER_DOWN_BTRX_CH_B BIT(3) 4820 #define LAN8841_BTRX_POWER_DOWN_BTRX_CH_C BIT(5) 4821 #define LAN8841_BTRX_POWER_DOWN_BTRX_CH_D BIT(7) 4822 #define LAN8841_ADC_CHANNEL_MASK 198 4823 #define LAN8841_PTP_RX_PARSE_L2_ADDR_EN 370 4824 #define LAN8841_PTP_RX_PARSE_IP_ADDR_EN 371 4825 #define LAN8841_PTP_RX_VERSION 374 4826 #define LAN8841_PTP_TX_PARSE_L2_ADDR_EN 434 4827 #define LAN8841_PTP_TX_PARSE_IP_ADDR_EN 435 4828 #define LAN8841_PTP_TX_VERSION 438 4829 #define LAN8841_PTP_CMD_CTL 256 4830 #define LAN8841_PTP_CMD_CTL_PTP_ENABLE BIT(2) 4831 #define LAN8841_PTP_CMD_CTL_PTP_DISABLE BIT(1) 4832 #define LAN8841_PTP_CMD_CTL_PTP_RESET BIT(0) 4833 #define LAN8841_PTP_RX_PARSE_CONFIG 368 4834 #define LAN8841_PTP_TX_PARSE_CONFIG 432 4835 #define LAN8841_PTP_RX_MODE 381 4836 #define LAN8841_PTP_INSERT_TS_EN BIT(0) 4837 #define LAN8841_PTP_INSERT_TS_32BIT BIT(1) 4838 4839 static int lan8841_config_init(struct phy_device *phydev) 4840 { 4841 int ret; 4842 4843 ret = ksz9131_config_init(phydev); 4844 if (ret) 4845 return ret; 4846 4847 /* Initialize the HW by resetting everything */ 4848 phy_modify_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG, 4849 LAN8841_PTP_CMD_CTL, 4850 LAN8841_PTP_CMD_CTL_PTP_RESET, 4851 LAN8841_PTP_CMD_CTL_PTP_RESET); 4852 4853 phy_modify_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG, 4854 LAN8841_PTP_CMD_CTL, 4855 LAN8841_PTP_CMD_CTL_PTP_ENABLE, 4856 LAN8841_PTP_CMD_CTL_PTP_ENABLE); 4857 4858 /* Don't process any frames */ 4859 phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG, 4860 LAN8841_PTP_RX_PARSE_CONFIG, 0); 4861 phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG, 4862 LAN8841_PTP_TX_PARSE_CONFIG, 0); 4863 phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG, 4864 LAN8841_PTP_TX_PARSE_L2_ADDR_EN, 0); 4865 phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG, 4866 LAN8841_PTP_RX_PARSE_L2_ADDR_EN, 0); 4867 phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG, 4868 LAN8841_PTP_TX_PARSE_IP_ADDR_EN, 0); 4869 phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG, 4870 LAN8841_PTP_RX_PARSE_IP_ADDR_EN, 0); 4871 4872 /* Disable checking for minorVersionPTP field */ 4873 phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG, 4874 LAN8841_PTP_RX_VERSION, 0xff00); 4875 phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG, 4876 LAN8841_PTP_TX_VERSION, 0xff00); 4877 4878 /* 100BT Clause 40 improvement errata */ 4879 phy_write_mmd(phydev, LAN8841_MMD_ANALOG_REG, 4880 LAN8841_ANALOG_CONTROL_1, 4881 LAN8841_ANALOG_CONTROL_1_PLL_TRIM(0x2)); 4882 phy_write_mmd(phydev, LAN8841_MMD_ANALOG_REG, 4883 LAN8841_ANALOG_CONTROL_10, 4884 LAN8841_ANALOG_CONTROL_10_PLL_DIV(0x1)); 4885 4886 /* 10M/100M Ethernet Signal Tuning Errata for Shorted-Center Tap 4887 * Magnetics 4888 */ 4889 ret = phy_read_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG, 4890 LAN8841_OPERATION_MODE_STRAP_OVERRIDE_LOW_REG); 4891 if (ret & LAN8841_OPERATION_MODE_STRAP_OVERRIDE_LOW_REG_MAGJACK) { 4892 phy_write_mmd(phydev, LAN8841_MMD_ANALOG_REG, 4893 LAN8841_TX_LOW_I_CH_C_D_POWER_MANAGMENT, 4894 LAN8841_TX_LOW_I_CH_C_D_POWER_MANAGMENT_VAL); 4895 phy_write_mmd(phydev, LAN8841_MMD_ANALOG_REG, 4896 LAN8841_BTRX_POWER_DOWN, 4897 LAN8841_BTRX_POWER_DOWN_QBIAS_CH_A | 4898 LAN8841_BTRX_POWER_DOWN_BTRX_CH_A | 4899 LAN8841_BTRX_POWER_DOWN_QBIAS_CH_B | 4900 LAN8841_BTRX_POWER_DOWN_BTRX_CH_B | 4901 LAN8841_BTRX_POWER_DOWN_BTRX_CH_C | 4902 LAN8841_BTRX_POWER_DOWN_BTRX_CH_D); 4903 } 4904 4905 /* LDO Adjustment errata */ 4906 phy_write_mmd(phydev, LAN8841_MMD_ANALOG_REG, 4907 LAN8841_ANALOG_CONTROL_11, 4908 LAN8841_ANALOG_CONTROL_11_LDO_REF(1)); 4909 4910 /* 100BT RGMII latency tuning errata */ 4911 phy_write_mmd(phydev, MDIO_MMD_PMAPMD, 4912 LAN8841_ADC_CHANNEL_MASK, 0x0); 4913 phy_write_mmd(phydev, LAN8841_MMD_TIMER_REG, 4914 LAN8841_MMD0_REGISTER_17, 4915 LAN8841_MMD0_REGISTER_17_DROP_OPT(2) | 4916 LAN8841_MMD0_REGISTER_17_XMIT_TOG_TX_DIS); 4917 4918 return 0; 4919 } 4920 4921 #define LAN8841_OUTPUT_CTRL 25 4922 #define LAN8841_OUTPUT_CTRL_INT_BUFFER BIT(14) 4923 #define LAN8841_INT_PTP BIT(9) 4924 4925 static int lan8841_config_intr(struct phy_device *phydev) 4926 { 4927 int err; 4928 4929 phy_modify(phydev, LAN8841_OUTPUT_CTRL, 4930 LAN8841_OUTPUT_CTRL_INT_BUFFER, 0); 4931 4932 if (phydev->interrupts == PHY_INTERRUPT_ENABLED) { 4933 err = phy_read(phydev, LAN8814_INTS); 4934 if (err < 0) 4935 return err; 4936 4937 /* Enable / disable interrupts. It is OK to enable PTP interrupt 4938 * even if it PTP is not enabled. Because the underneath blocks 4939 * will not enable the PTP so we will never get the PTP 4940 * interrupt. 4941 */ 4942 err = phy_write(phydev, LAN8814_INTC, 4943 LAN8814_INT_LINK | LAN8841_INT_PTP); 4944 } else { 4945 err = phy_write(phydev, LAN8814_INTC, 0); 4946 if (err) 4947 return err; 4948 4949 err = phy_read(phydev, LAN8814_INTS); 4950 if (err < 0) 4951 return err; 4952 4953 /* Getting a positive value doesn't mean that is an error, it 4954 * just indicates what was the status. Therefore make sure to 4955 * clear the value and say that there is no error. 4956 */ 4957 err = 0; 4958 } 4959 4960 return err; 4961 } 4962 4963 #define LAN8841_PTP_TX_EGRESS_SEC_LO 453 4964 #define LAN8841_PTP_TX_EGRESS_SEC_HI 452 4965 #define LAN8841_PTP_TX_EGRESS_NS_LO 451 4966 #define LAN8841_PTP_TX_EGRESS_NS_HI 450 4967 #define LAN8841_PTP_TX_EGRESS_NSEC_HI_VALID BIT(15) 4968 #define LAN8841_PTP_TX_MSG_HEADER2 455 4969 4970 static bool lan8841_ptp_get_tx_ts(struct kszphy_ptp_priv *ptp_priv, 4971 u32 *sec, u32 *nsec, u16 *seq) 4972 { 4973 struct phy_device *phydev = ptp_priv->phydev; 4974 4975 *nsec = phy_read_mmd(phydev, 2, LAN8841_PTP_TX_EGRESS_NS_HI); 4976 if (!(*nsec & LAN8841_PTP_TX_EGRESS_NSEC_HI_VALID)) 4977 return false; 4978 4979 *nsec = ((*nsec & 0x3fff) << 16); 4980 *nsec = *nsec | phy_read_mmd(phydev, 2, LAN8841_PTP_TX_EGRESS_NS_LO); 4981 4982 *sec = phy_read_mmd(phydev, 2, LAN8841_PTP_TX_EGRESS_SEC_HI); 4983 *sec = *sec << 16; 4984 *sec = *sec | phy_read_mmd(phydev, 2, LAN8841_PTP_TX_EGRESS_SEC_LO); 4985 4986 *seq = phy_read_mmd(phydev, 2, LAN8841_PTP_TX_MSG_HEADER2); 4987 4988 return true; 4989 } 4990 4991 static void lan8841_ptp_process_tx_ts(struct kszphy_ptp_priv *ptp_priv) 4992 { 4993 u32 sec, nsec; 4994 u16 seq; 4995 4996 while (lan8841_ptp_get_tx_ts(ptp_priv, &sec, &nsec, &seq)) 4997 lan8814_match_tx_skb(ptp_priv, sec, nsec, seq); 4998 } 4999 5000 #define LAN8841_PTP_INT_STS 259 5001 #define LAN8841_PTP_INT_STS_PTP_TX_TS_OVRFL_INT BIT(13) 5002 #define LAN8841_PTP_INT_STS_PTP_TX_TS_INT BIT(12) 5003 #define LAN8841_PTP_INT_STS_PTP_GPIO_CAP_INT BIT(2) 5004 5005 static void lan8841_ptp_flush_fifo(struct kszphy_ptp_priv *ptp_priv) 5006 { 5007 struct phy_device *phydev = ptp_priv->phydev; 5008 int i; 5009 5010 for (i = 0; i < FIFO_SIZE; ++i) 5011 phy_read_mmd(phydev, 2, LAN8841_PTP_TX_MSG_HEADER2); 5012 5013 phy_read_mmd(phydev, 2, LAN8841_PTP_INT_STS); 5014 } 5015 5016 #define LAN8841_PTP_GPIO_CAP_STS 506 5017 #define LAN8841_PTP_GPIO_SEL 327 5018 #define LAN8841_PTP_GPIO_SEL_GPIO_SEL(gpio) ((gpio) << 8) 5019 #define LAN8841_PTP_GPIO_RE_LTC_SEC_HI_CAP 498 5020 #define LAN8841_PTP_GPIO_RE_LTC_SEC_LO_CAP 499 5021 #define LAN8841_PTP_GPIO_RE_LTC_NS_HI_CAP 500 5022 #define LAN8841_PTP_GPIO_RE_LTC_NS_LO_CAP 501 5023 #define LAN8841_PTP_GPIO_FE_LTC_SEC_HI_CAP 502 5024 #define LAN8841_PTP_GPIO_FE_LTC_SEC_LO_CAP 503 5025 #define LAN8841_PTP_GPIO_FE_LTC_NS_HI_CAP 504 5026 #define LAN8841_PTP_GPIO_FE_LTC_NS_LO_CAP 505 5027 5028 static void lan8841_gpio_process_cap(struct kszphy_ptp_priv *ptp_priv) 5029 { 5030 struct phy_device *phydev = ptp_priv->phydev; 5031 struct ptp_clock_event ptp_event = {0}; 5032 int pin, ret, tmp; 5033 s32 sec, nsec; 5034 5035 pin = ptp_find_pin_unlocked(ptp_priv->ptp_clock, PTP_PF_EXTTS, 0); 5036 if (pin == -1) 5037 return; 5038 5039 tmp = phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_CAP_STS); 5040 if (tmp < 0) 5041 return; 5042 5043 ret = phy_write_mmd(phydev, 2, LAN8841_PTP_GPIO_SEL, 5044 LAN8841_PTP_GPIO_SEL_GPIO_SEL(pin)); 5045 if (ret) 5046 return; 5047 5048 mutex_lock(&ptp_priv->ptp_lock); 5049 if (tmp & BIT(pin)) { 5050 sec = phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_RE_LTC_SEC_HI_CAP); 5051 sec <<= 16; 5052 sec |= phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_RE_LTC_SEC_LO_CAP); 5053 5054 nsec = phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_RE_LTC_NS_HI_CAP) & 0x3fff; 5055 nsec <<= 16; 5056 nsec |= phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_RE_LTC_NS_LO_CAP); 5057 } else { 5058 sec = phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_FE_LTC_SEC_HI_CAP); 5059 sec <<= 16; 5060 sec |= phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_FE_LTC_SEC_LO_CAP); 5061 5062 nsec = phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_FE_LTC_NS_HI_CAP) & 0x3fff; 5063 nsec <<= 16; 5064 nsec |= phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_FE_LTC_NS_LO_CAP); 5065 } 5066 mutex_unlock(&ptp_priv->ptp_lock); 5067 ret = phy_write_mmd(phydev, 2, LAN8841_PTP_GPIO_SEL, 0); 5068 if (ret) 5069 return; 5070 5071 ptp_event.index = 0; 5072 ptp_event.timestamp = ktime_set(sec, nsec); 5073 ptp_event.type = PTP_CLOCK_EXTTS; 5074 ptp_clock_event(ptp_priv->ptp_clock, &ptp_event); 5075 } 5076 5077 static void lan8841_handle_ptp_interrupt(struct phy_device *phydev) 5078 { 5079 struct kszphy_priv *priv = phydev->priv; 5080 struct kszphy_ptp_priv *ptp_priv = &priv->ptp_priv; 5081 u16 status; 5082 5083 do { 5084 status = phy_read_mmd(phydev, 2, LAN8841_PTP_INT_STS); 5085 5086 if (status & LAN8841_PTP_INT_STS_PTP_TX_TS_INT) 5087 lan8841_ptp_process_tx_ts(ptp_priv); 5088 5089 if (status & LAN8841_PTP_INT_STS_PTP_GPIO_CAP_INT) 5090 lan8841_gpio_process_cap(ptp_priv); 5091 5092 if (status & LAN8841_PTP_INT_STS_PTP_TX_TS_OVRFL_INT) { 5093 lan8841_ptp_flush_fifo(ptp_priv); 5094 skb_queue_purge(&ptp_priv->tx_queue); 5095 } 5096 5097 } while (status & (LAN8841_PTP_INT_STS_PTP_TX_TS_INT | 5098 LAN8841_PTP_INT_STS_PTP_GPIO_CAP_INT | 5099 LAN8841_PTP_INT_STS_PTP_TX_TS_OVRFL_INT)); 5100 } 5101 5102 #define LAN8841_INTS_PTP BIT(9) 5103 5104 static irqreturn_t lan8841_handle_interrupt(struct phy_device *phydev) 5105 { 5106 irqreturn_t ret = IRQ_NONE; 5107 int irq_status; 5108 5109 irq_status = phy_read(phydev, LAN8814_INTS); 5110 if (irq_status < 0) { 5111 phy_error(phydev); 5112 return IRQ_NONE; 5113 } 5114 5115 if (irq_status & LAN8814_INT_LINK) { 5116 phy_trigger_machine(phydev); 5117 ret = IRQ_HANDLED; 5118 } 5119 5120 if (irq_status & LAN8841_INTS_PTP) { 5121 lan8841_handle_ptp_interrupt(phydev); 5122 ret = IRQ_HANDLED; 5123 } 5124 5125 return ret; 5126 } 5127 5128 static int lan8841_ts_info(struct mii_timestamper *mii_ts, 5129 struct kernel_ethtool_ts_info *info) 5130 { 5131 struct kszphy_ptp_priv *ptp_priv; 5132 5133 ptp_priv = container_of(mii_ts, struct kszphy_ptp_priv, mii_ts); 5134 5135 info->phc_index = ptp_priv->ptp_clock ? 5136 ptp_clock_index(ptp_priv->ptp_clock) : -1; 5137 if (info->phc_index == -1) 5138 return 0; 5139 5140 info->so_timestamping = SOF_TIMESTAMPING_TX_HARDWARE | 5141 SOF_TIMESTAMPING_RX_HARDWARE | 5142 SOF_TIMESTAMPING_RAW_HARDWARE; 5143 5144 info->tx_types = (1 << HWTSTAMP_TX_OFF) | 5145 (1 << HWTSTAMP_TX_ON) | 5146 (1 << HWTSTAMP_TX_ONESTEP_SYNC); 5147 5148 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) | 5149 (1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT) | 5150 (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) | 5151 (1 << HWTSTAMP_FILTER_PTP_V2_EVENT); 5152 5153 return 0; 5154 } 5155 5156 #define LAN8841_PTP_INT_EN 260 5157 #define LAN8841_PTP_INT_EN_PTP_TX_TS_OVRFL_EN BIT(13) 5158 #define LAN8841_PTP_INT_EN_PTP_TX_TS_EN BIT(12) 5159 5160 static void lan8841_ptp_enable_processing(struct kszphy_ptp_priv *ptp_priv, 5161 bool enable) 5162 { 5163 struct phy_device *phydev = ptp_priv->phydev; 5164 5165 if (enable) { 5166 /* Enable interrupts on the TX side */ 5167 phy_modify_mmd(phydev, 2, LAN8841_PTP_INT_EN, 5168 LAN8841_PTP_INT_EN_PTP_TX_TS_OVRFL_EN | 5169 LAN8841_PTP_INT_EN_PTP_TX_TS_EN, 5170 LAN8841_PTP_INT_EN_PTP_TX_TS_OVRFL_EN | 5171 LAN8841_PTP_INT_EN_PTP_TX_TS_EN); 5172 5173 /* Enable the modification of the frame on RX side, 5174 * this will add the ns and 2 bits of sec in the reserved field 5175 * of the PTP header 5176 */ 5177 phy_modify_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG, 5178 LAN8841_PTP_RX_MODE, 5179 LAN8841_PTP_INSERT_TS_EN | 5180 LAN8841_PTP_INSERT_TS_32BIT, 5181 LAN8841_PTP_INSERT_TS_EN | 5182 LAN8841_PTP_INSERT_TS_32BIT); 5183 5184 ptp_schedule_worker(ptp_priv->ptp_clock, 0); 5185 } else { 5186 /* Disable interrupts on the TX side */ 5187 phy_modify_mmd(phydev, 2, LAN8841_PTP_INT_EN, 5188 LAN8841_PTP_INT_EN_PTP_TX_TS_OVRFL_EN | 5189 LAN8841_PTP_INT_EN_PTP_TX_TS_EN, 0); 5190 5191 /* Disable modification of the RX frames */ 5192 phy_modify_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG, 5193 LAN8841_PTP_RX_MODE, 5194 LAN8841_PTP_INSERT_TS_EN | 5195 LAN8841_PTP_INSERT_TS_32BIT, 0); 5196 5197 ptp_cancel_worker_sync(ptp_priv->ptp_clock); 5198 } 5199 } 5200 5201 #define LAN8841_PTP_RX_TIMESTAMP_EN 379 5202 #define LAN8841_PTP_TX_TIMESTAMP_EN 443 5203 #define LAN8841_PTP_TX_MOD 445 5204 5205 static int lan8841_hwtstamp_set(struct mii_timestamper *mii_ts, 5206 struct kernel_hwtstamp_config *config, 5207 struct netlink_ext_ack *extack) 5208 { 5209 struct kszphy_ptp_priv *ptp_priv = container_of(mii_ts, struct kszphy_ptp_priv, mii_ts); 5210 struct phy_device *phydev = ptp_priv->phydev; 5211 int txcfg = 0, rxcfg = 0; 5212 int pkt_ts_enable; 5213 5214 switch (config->rx_filter) { 5215 case HWTSTAMP_FILTER_NONE: 5216 ptp_priv->layer = 0; 5217 ptp_priv->version = 0; 5218 break; 5219 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT: 5220 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC: 5221 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ: 5222 ptp_priv->layer = PTP_CLASS_L4; 5223 ptp_priv->version = PTP_CLASS_V2; 5224 break; 5225 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT: 5226 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC: 5227 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ: 5228 ptp_priv->layer = PTP_CLASS_L2; 5229 ptp_priv->version = PTP_CLASS_V2; 5230 break; 5231 case HWTSTAMP_FILTER_PTP_V2_EVENT: 5232 case HWTSTAMP_FILTER_PTP_V2_SYNC: 5233 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ: 5234 ptp_priv->layer = PTP_CLASS_L4 | PTP_CLASS_L2; 5235 ptp_priv->version = PTP_CLASS_V2; 5236 break; 5237 default: 5238 return -ERANGE; 5239 } 5240 5241 switch (config->tx_type) { 5242 case HWTSTAMP_TX_OFF: 5243 case HWTSTAMP_TX_ON: 5244 case HWTSTAMP_TX_ONESTEP_SYNC: 5245 break; 5246 default: 5247 return -ERANGE; 5248 } 5249 5250 ptp_priv->hwts_tx_type = config->tx_type; 5251 ptp_priv->rx_filter = config->rx_filter; 5252 5253 /* Setup parsing of the frames and enable the timestamping for ptp 5254 * frames 5255 */ 5256 if (ptp_priv->layer & PTP_CLASS_L2) { 5257 rxcfg |= PTP_RX_PARSE_CONFIG_LAYER2_EN_; 5258 txcfg |= PTP_TX_PARSE_CONFIG_LAYER2_EN_; 5259 } else if (ptp_priv->layer & PTP_CLASS_L4) { 5260 rxcfg |= PTP_RX_PARSE_CONFIG_IPV4_EN_ | PTP_RX_PARSE_CONFIG_IPV6_EN_; 5261 txcfg |= PTP_TX_PARSE_CONFIG_IPV4_EN_ | PTP_TX_PARSE_CONFIG_IPV6_EN_; 5262 } 5263 5264 phy_write_mmd(phydev, 2, LAN8841_PTP_RX_PARSE_CONFIG, rxcfg); 5265 phy_write_mmd(phydev, 2, LAN8841_PTP_TX_PARSE_CONFIG, txcfg); 5266 5267 pkt_ts_enable = PTP_TIMESTAMP_EN_SYNC_ | PTP_TIMESTAMP_EN_DREQ_ | 5268 PTP_TIMESTAMP_EN_PDREQ_ | PTP_TIMESTAMP_EN_PDRES_; 5269 phy_write_mmd(phydev, 2, LAN8841_PTP_RX_TIMESTAMP_EN, pkt_ts_enable); 5270 phy_write_mmd(phydev, 2, LAN8841_PTP_TX_TIMESTAMP_EN, pkt_ts_enable); 5271 5272 /* Enable / disable of the TX timestamp in the SYNC frames */ 5273 phy_modify_mmd(phydev, 2, LAN8841_PTP_TX_MOD, 5274 PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_, 5275 ptp_priv->hwts_tx_type == HWTSTAMP_TX_ONESTEP_SYNC ? 5276 PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_ : 0); 5277 5278 /* Now enable/disable the timestamping */ 5279 lan8841_ptp_enable_processing(ptp_priv, 5280 config->rx_filter != HWTSTAMP_FILTER_NONE); 5281 5282 skb_queue_purge(&ptp_priv->tx_queue); 5283 5284 lan8841_ptp_flush_fifo(ptp_priv); 5285 5286 return 0; 5287 } 5288 5289 static bool lan8841_rxtstamp(struct mii_timestamper *mii_ts, 5290 struct sk_buff *skb, int type) 5291 { 5292 struct kszphy_ptp_priv *ptp_priv = 5293 container_of(mii_ts, struct kszphy_ptp_priv, mii_ts); 5294 struct ptp_header *header = ptp_parse_header(skb, type); 5295 struct skb_shared_hwtstamps *shhwtstamps; 5296 struct timespec64 ts; 5297 unsigned long flags; 5298 u32 ts_header; 5299 5300 if (!header) 5301 return false; 5302 5303 if (ptp_priv->rx_filter == HWTSTAMP_FILTER_NONE || 5304 type == PTP_CLASS_NONE) 5305 return false; 5306 5307 if ((type & ptp_priv->version) == 0 || (type & ptp_priv->layer) == 0) 5308 return false; 5309 5310 spin_lock_irqsave(&ptp_priv->seconds_lock, flags); 5311 ts.tv_sec = ptp_priv->seconds; 5312 spin_unlock_irqrestore(&ptp_priv->seconds_lock, flags); 5313 ts_header = __be32_to_cpu(header->reserved2); 5314 5315 shhwtstamps = skb_hwtstamps(skb); 5316 memset(shhwtstamps, 0, sizeof(*shhwtstamps)); 5317 5318 /* Check for any wrap arounds for the second part */ 5319 if ((ts.tv_sec & GENMASK(1, 0)) == 0 && (ts_header >> 30) == 3) 5320 ts.tv_sec -= GENMASK(1, 0) + 1; 5321 else if ((ts.tv_sec & GENMASK(1, 0)) == 3 && (ts_header >> 30) == 0) 5322 ts.tv_sec += 1; 5323 5324 shhwtstamps->hwtstamp = 5325 ktime_set((ts.tv_sec & ~(GENMASK(1, 0))) | ts_header >> 30, 5326 ts_header & GENMASK(29, 0)); 5327 header->reserved2 = 0; 5328 5329 netif_rx(skb); 5330 5331 return true; 5332 } 5333 5334 #define LAN8841_EVENT_A 0 5335 #define LAN8841_EVENT_B 1 5336 #define LAN8841_PTP_LTC_TARGET_SEC_HI(event) ((event) == LAN8841_EVENT_A ? 278 : 288) 5337 #define LAN8841_PTP_LTC_TARGET_SEC_LO(event) ((event) == LAN8841_EVENT_A ? 279 : 289) 5338 #define LAN8841_PTP_LTC_TARGET_NS_HI(event) ((event) == LAN8841_EVENT_A ? 280 : 290) 5339 #define LAN8841_PTP_LTC_TARGET_NS_LO(event) ((event) == LAN8841_EVENT_A ? 281 : 291) 5340 5341 static int lan8841_ptp_set_target(struct kszphy_ptp_priv *ptp_priv, u8 event, 5342 s64 sec, u32 nsec) 5343 { 5344 struct phy_device *phydev = ptp_priv->phydev; 5345 int ret; 5346 5347 ret = phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_TARGET_SEC_HI(event), 5348 upper_16_bits(sec)); 5349 if (ret) 5350 return ret; 5351 5352 ret = phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_TARGET_SEC_LO(event), 5353 lower_16_bits(sec)); 5354 if (ret) 5355 return ret; 5356 5357 ret = phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_TARGET_NS_HI(event) & 0x3fff, 5358 upper_16_bits(nsec)); 5359 if (ret) 5360 return ret; 5361 5362 return phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_TARGET_NS_LO(event), 5363 lower_16_bits(nsec)); 5364 } 5365 5366 #define LAN8841_BUFFER_TIME 2 5367 5368 static int lan8841_ptp_update_target(struct kszphy_ptp_priv *ptp_priv, 5369 const struct timespec64 *ts) 5370 { 5371 return lan8841_ptp_set_target(ptp_priv, LAN8841_EVENT_A, 5372 ts->tv_sec + LAN8841_BUFFER_TIME, 0); 5373 } 5374 5375 #define LAN8841_PTP_LTC_TARGET_RELOAD_SEC_HI(event) ((event) == LAN8841_EVENT_A ? 282 : 292) 5376 #define LAN8841_PTP_LTC_TARGET_RELOAD_SEC_LO(event) ((event) == LAN8841_EVENT_A ? 283 : 293) 5377 #define LAN8841_PTP_LTC_TARGET_RELOAD_NS_HI(event) ((event) == LAN8841_EVENT_A ? 284 : 294) 5378 #define LAN8841_PTP_LTC_TARGET_RELOAD_NS_LO(event) ((event) == LAN8841_EVENT_A ? 285 : 295) 5379 5380 static int lan8841_ptp_set_reload(struct kszphy_ptp_priv *ptp_priv, u8 event, 5381 s64 sec, u32 nsec) 5382 { 5383 struct phy_device *phydev = ptp_priv->phydev; 5384 int ret; 5385 5386 ret = phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_TARGET_RELOAD_SEC_HI(event), 5387 upper_16_bits(sec)); 5388 if (ret) 5389 return ret; 5390 5391 ret = phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_TARGET_RELOAD_SEC_LO(event), 5392 lower_16_bits(sec)); 5393 if (ret) 5394 return ret; 5395 5396 ret = phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_TARGET_RELOAD_NS_HI(event) & 0x3fff, 5397 upper_16_bits(nsec)); 5398 if (ret) 5399 return ret; 5400 5401 return phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_TARGET_RELOAD_NS_LO(event), 5402 lower_16_bits(nsec)); 5403 } 5404 5405 #define LAN8841_PTP_LTC_SET_SEC_HI 262 5406 #define LAN8841_PTP_LTC_SET_SEC_MID 263 5407 #define LAN8841_PTP_LTC_SET_SEC_LO 264 5408 #define LAN8841_PTP_LTC_SET_NS_HI 265 5409 #define LAN8841_PTP_LTC_SET_NS_LO 266 5410 #define LAN8841_PTP_CMD_CTL_PTP_LTC_LOAD BIT(4) 5411 5412 static int lan8841_ptp_settime64(struct ptp_clock_info *ptp, 5413 const struct timespec64 *ts) 5414 { 5415 struct kszphy_ptp_priv *ptp_priv = container_of(ptp, struct kszphy_ptp_priv, 5416 ptp_clock_info); 5417 struct phy_device *phydev = ptp_priv->phydev; 5418 unsigned long flags; 5419 int ret; 5420 5421 /* Set the value to be stored */ 5422 mutex_lock(&ptp_priv->ptp_lock); 5423 phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_SET_SEC_LO, lower_16_bits(ts->tv_sec)); 5424 phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_SET_SEC_MID, upper_16_bits(ts->tv_sec)); 5425 phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_SET_SEC_HI, upper_32_bits(ts->tv_sec) & 0xffff); 5426 phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_SET_NS_LO, lower_16_bits(ts->tv_nsec)); 5427 phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_SET_NS_HI, upper_16_bits(ts->tv_nsec) & 0x3fff); 5428 5429 /* Set the command to load the LTC */ 5430 phy_write_mmd(phydev, 2, LAN8841_PTP_CMD_CTL, 5431 LAN8841_PTP_CMD_CTL_PTP_LTC_LOAD); 5432 ret = lan8841_ptp_update_target(ptp_priv, ts); 5433 mutex_unlock(&ptp_priv->ptp_lock); 5434 5435 spin_lock_irqsave(&ptp_priv->seconds_lock, flags); 5436 ptp_priv->seconds = ts->tv_sec; 5437 spin_unlock_irqrestore(&ptp_priv->seconds_lock, flags); 5438 5439 return ret; 5440 } 5441 5442 #define LAN8841_PTP_LTC_RD_SEC_HI 358 5443 #define LAN8841_PTP_LTC_RD_SEC_MID 359 5444 #define LAN8841_PTP_LTC_RD_SEC_LO 360 5445 #define LAN8841_PTP_LTC_RD_NS_HI 361 5446 #define LAN8841_PTP_LTC_RD_NS_LO 362 5447 #define LAN8841_PTP_CMD_CTL_PTP_LTC_READ BIT(3) 5448 5449 static int lan8841_ptp_gettime64(struct ptp_clock_info *ptp, 5450 struct timespec64 *ts) 5451 { 5452 struct kszphy_ptp_priv *ptp_priv = container_of(ptp, struct kszphy_ptp_priv, 5453 ptp_clock_info); 5454 struct phy_device *phydev = ptp_priv->phydev; 5455 time64_t s; 5456 s64 ns; 5457 5458 mutex_lock(&ptp_priv->ptp_lock); 5459 /* Issue the command to read the LTC */ 5460 phy_write_mmd(phydev, 2, LAN8841_PTP_CMD_CTL, 5461 LAN8841_PTP_CMD_CTL_PTP_LTC_READ); 5462 5463 /* Read the LTC */ 5464 s = phy_read_mmd(phydev, 2, LAN8841_PTP_LTC_RD_SEC_HI); 5465 s <<= 16; 5466 s |= phy_read_mmd(phydev, 2, LAN8841_PTP_LTC_RD_SEC_MID); 5467 s <<= 16; 5468 s |= phy_read_mmd(phydev, 2, LAN8841_PTP_LTC_RD_SEC_LO); 5469 5470 ns = phy_read_mmd(phydev, 2, LAN8841_PTP_LTC_RD_NS_HI) & 0x3fff; 5471 ns <<= 16; 5472 ns |= phy_read_mmd(phydev, 2, LAN8841_PTP_LTC_RD_NS_LO); 5473 mutex_unlock(&ptp_priv->ptp_lock); 5474 5475 set_normalized_timespec64(ts, s, ns); 5476 return 0; 5477 } 5478 5479 static void lan8841_ptp_getseconds(struct ptp_clock_info *ptp, 5480 struct timespec64 *ts) 5481 { 5482 struct kszphy_ptp_priv *ptp_priv = container_of(ptp, struct kszphy_ptp_priv, 5483 ptp_clock_info); 5484 struct phy_device *phydev = ptp_priv->phydev; 5485 time64_t s; 5486 5487 mutex_lock(&ptp_priv->ptp_lock); 5488 /* Issue the command to read the LTC */ 5489 phy_write_mmd(phydev, 2, LAN8841_PTP_CMD_CTL, 5490 LAN8841_PTP_CMD_CTL_PTP_LTC_READ); 5491 5492 /* Read the LTC */ 5493 s = phy_read_mmd(phydev, 2, LAN8841_PTP_LTC_RD_SEC_HI); 5494 s <<= 16; 5495 s |= phy_read_mmd(phydev, 2, LAN8841_PTP_LTC_RD_SEC_MID); 5496 s <<= 16; 5497 s |= phy_read_mmd(phydev, 2, LAN8841_PTP_LTC_RD_SEC_LO); 5498 mutex_unlock(&ptp_priv->ptp_lock); 5499 5500 set_normalized_timespec64(ts, s, 0); 5501 } 5502 5503 #define LAN8841_PTP_LTC_STEP_ADJ_LO 276 5504 #define LAN8841_PTP_LTC_STEP_ADJ_HI 275 5505 #define LAN8841_PTP_LTC_STEP_ADJ_DIR BIT(15) 5506 #define LAN8841_PTP_CMD_CTL_PTP_LTC_STEP_SECONDS BIT(5) 5507 #define LAN8841_PTP_CMD_CTL_PTP_LTC_STEP_NANOSECONDS BIT(6) 5508 5509 static int lan8841_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta) 5510 { 5511 struct kszphy_ptp_priv *ptp_priv = container_of(ptp, struct kszphy_ptp_priv, 5512 ptp_clock_info); 5513 struct phy_device *phydev = ptp_priv->phydev; 5514 struct timespec64 ts; 5515 bool add = true; 5516 u32 nsec; 5517 s32 sec; 5518 int ret; 5519 5520 /* The HW allows up to 15 sec to adjust the time, but here we limit to 5521 * 10 sec the adjustment. The reason is, in case the adjustment is 14 5522 * sec and 999999999 nsec, then we add 8ns to compansate the actual 5523 * increment so the value can be bigger than 15 sec. Therefore limit the 5524 * possible adjustments so we will not have these corner cases 5525 */ 5526 if (delta > 10000000000LL || delta < -10000000000LL) { 5527 /* The timeadjustment is too big, so fall back using set time */ 5528 u64 now; 5529 5530 ptp->gettime64(ptp, &ts); 5531 5532 now = ktime_to_ns(timespec64_to_ktime(ts)); 5533 ts = ns_to_timespec64(now + delta); 5534 5535 ptp->settime64(ptp, &ts); 5536 return 0; 5537 } 5538 5539 sec = div_u64_rem(delta < 0 ? -delta : delta, NSEC_PER_SEC, &nsec); 5540 if (delta < 0 && nsec != 0) { 5541 /* It is not allowed to adjust low the nsec part, therefore 5542 * subtract more from second part and add to nanosecond such 5543 * that would roll over, so the second part will increase 5544 */ 5545 sec--; 5546 nsec = NSEC_PER_SEC - nsec; 5547 } 5548 5549 /* Calculate the adjustments and the direction */ 5550 if (delta < 0) 5551 add = false; 5552 5553 if (nsec > 0) 5554 /* add 8 ns to cover the likely normal increment */ 5555 nsec += 8; 5556 5557 if (nsec >= NSEC_PER_SEC) { 5558 /* carry into seconds */ 5559 sec++; 5560 nsec -= NSEC_PER_SEC; 5561 } 5562 5563 mutex_lock(&ptp_priv->ptp_lock); 5564 if (sec) { 5565 phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_STEP_ADJ_LO, sec); 5566 phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_STEP_ADJ_HI, 5567 add ? LAN8841_PTP_LTC_STEP_ADJ_DIR : 0); 5568 phy_write_mmd(phydev, 2, LAN8841_PTP_CMD_CTL, 5569 LAN8841_PTP_CMD_CTL_PTP_LTC_STEP_SECONDS); 5570 } 5571 5572 if (nsec) { 5573 phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_STEP_ADJ_LO, 5574 nsec & 0xffff); 5575 phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_STEP_ADJ_HI, 5576 (nsec >> 16) & 0x3fff); 5577 phy_write_mmd(phydev, 2, LAN8841_PTP_CMD_CTL, 5578 LAN8841_PTP_CMD_CTL_PTP_LTC_STEP_NANOSECONDS); 5579 } 5580 mutex_unlock(&ptp_priv->ptp_lock); 5581 5582 /* Update the target clock */ 5583 ptp->gettime64(ptp, &ts); 5584 mutex_lock(&ptp_priv->ptp_lock); 5585 ret = lan8841_ptp_update_target(ptp_priv, &ts); 5586 mutex_unlock(&ptp_priv->ptp_lock); 5587 5588 return ret; 5589 } 5590 5591 #define LAN8841_PTP_LTC_RATE_ADJ_HI 269 5592 #define LAN8841_PTP_LTC_RATE_ADJ_HI_DIR BIT(15) 5593 #define LAN8841_PTP_LTC_RATE_ADJ_LO 270 5594 5595 static int lan8841_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm) 5596 { 5597 struct kszphy_ptp_priv *ptp_priv = container_of(ptp, struct kszphy_ptp_priv, 5598 ptp_clock_info); 5599 struct phy_device *phydev = ptp_priv->phydev; 5600 bool faster = true; 5601 u32 rate; 5602 5603 if (!scaled_ppm) 5604 return 0; 5605 5606 if (scaled_ppm < 0) { 5607 scaled_ppm = -scaled_ppm; 5608 faster = false; 5609 } 5610 5611 rate = LAN8841_1PPM_FORMAT * (upper_16_bits(scaled_ppm)); 5612 rate += (LAN8841_1PPM_FORMAT * (lower_16_bits(scaled_ppm))) >> 16; 5613 5614 mutex_lock(&ptp_priv->ptp_lock); 5615 phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_RATE_ADJ_HI, 5616 faster ? LAN8841_PTP_LTC_RATE_ADJ_HI_DIR | (upper_16_bits(rate) & 0x3fff) 5617 : upper_16_bits(rate) & 0x3fff); 5618 phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_RATE_ADJ_LO, lower_16_bits(rate)); 5619 mutex_unlock(&ptp_priv->ptp_lock); 5620 5621 return 0; 5622 } 5623 5624 static int lan8841_ptp_verify(struct ptp_clock_info *ptp, unsigned int pin, 5625 enum ptp_pin_function func, unsigned int chan) 5626 { 5627 switch (func) { 5628 case PTP_PF_NONE: 5629 case PTP_PF_PEROUT: 5630 case PTP_PF_EXTTS: 5631 break; 5632 default: 5633 return -1; 5634 } 5635 5636 return 0; 5637 } 5638 5639 #define LAN8841_PTP_GPIO_NUM 10 5640 #define LAN8841_GPIO_EN 128 5641 #define LAN8841_GPIO_DIR 129 5642 #define LAN8841_GPIO_BUF 130 5643 5644 static int lan8841_ptp_perout_off(struct kszphy_ptp_priv *ptp_priv, int pin) 5645 { 5646 struct phy_device *phydev = ptp_priv->phydev; 5647 int ret; 5648 5649 ret = phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_EN, BIT(pin)); 5650 if (ret) 5651 return ret; 5652 5653 ret = phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_DIR, BIT(pin)); 5654 if (ret) 5655 return ret; 5656 5657 return phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_BUF, BIT(pin)); 5658 } 5659 5660 static int lan8841_ptp_perout_on(struct kszphy_ptp_priv *ptp_priv, int pin) 5661 { 5662 struct phy_device *phydev = ptp_priv->phydev; 5663 int ret; 5664 5665 ret = phy_set_bits_mmd(phydev, 2, LAN8841_GPIO_EN, BIT(pin)); 5666 if (ret) 5667 return ret; 5668 5669 ret = phy_set_bits_mmd(phydev, 2, LAN8841_GPIO_DIR, BIT(pin)); 5670 if (ret) 5671 return ret; 5672 5673 return phy_set_bits_mmd(phydev, 2, LAN8841_GPIO_BUF, BIT(pin)); 5674 } 5675 5676 #define LAN8841_GPIO_DATA_SEL1 131 5677 #define LAN8841_GPIO_DATA_SEL2 132 5678 #define LAN8841_GPIO_DATA_SEL_GPIO_DATA_SEL_EVENT_MASK GENMASK(2, 0) 5679 #define LAN8841_GPIO_DATA_SEL_GPIO_DATA_SEL_EVENT_A 1 5680 #define LAN8841_GPIO_DATA_SEL_GPIO_DATA_SEL_EVENT_B 2 5681 #define LAN8841_PTP_GENERAL_CONFIG 257 5682 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_A BIT(1) 5683 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_B BIT(3) 5684 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_A_MASK GENMASK(7, 4) 5685 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_B_MASK GENMASK(11, 8) 5686 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_A 4 5687 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_B 7 5688 5689 static int lan8841_ptp_remove_event(struct kszphy_ptp_priv *ptp_priv, int pin, 5690 u8 event) 5691 { 5692 struct phy_device *phydev = ptp_priv->phydev; 5693 u16 tmp; 5694 int ret; 5695 5696 /* Now remove pin from the event. GPIO_DATA_SEL1 contains the GPIO 5697 * pins 0-4 while GPIO_DATA_SEL2 contains GPIO pins 5-9, therefore 5698 * depending on the pin, it requires to read a different register 5699 */ 5700 if (pin < 5) { 5701 tmp = LAN8841_GPIO_DATA_SEL_GPIO_DATA_SEL_EVENT_MASK << (3 * pin); 5702 ret = phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_DATA_SEL1, tmp); 5703 } else { 5704 tmp = LAN8841_GPIO_DATA_SEL_GPIO_DATA_SEL_EVENT_MASK << (3 * (pin - 5)); 5705 ret = phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_DATA_SEL2, tmp); 5706 } 5707 if (ret) 5708 return ret; 5709 5710 /* Disable the event */ 5711 if (event == LAN8841_EVENT_A) 5712 tmp = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_A | 5713 LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_A_MASK; 5714 else 5715 tmp = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_B | 5716 LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_B_MASK; 5717 return phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_EN, tmp); 5718 } 5719 5720 static int lan8841_ptp_enable_event(struct kszphy_ptp_priv *ptp_priv, int pin, 5721 u8 event, int pulse_width) 5722 { 5723 struct phy_device *phydev = ptp_priv->phydev; 5724 u16 tmp; 5725 int ret; 5726 5727 /* Enable the event */ 5728 if (event == LAN8841_EVENT_A) 5729 ret = phy_modify_mmd(phydev, 2, LAN8841_PTP_GENERAL_CONFIG, 5730 LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_A | 5731 LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_A_MASK, 5732 LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_A | 5733 pulse_width << LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_A); 5734 else 5735 ret = phy_modify_mmd(phydev, 2, LAN8841_PTP_GENERAL_CONFIG, 5736 LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_B | 5737 LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_B_MASK, 5738 LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_B | 5739 pulse_width << LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_B); 5740 if (ret) 5741 return ret; 5742 5743 /* Now connect the pin to the event. GPIO_DATA_SEL1 contains the GPIO 5744 * pins 0-4 while GPIO_DATA_SEL2 contains GPIO pins 5-9, therefore 5745 * depending on the pin, it requires to read a different register 5746 */ 5747 if (event == LAN8841_EVENT_A) 5748 tmp = LAN8841_GPIO_DATA_SEL_GPIO_DATA_SEL_EVENT_A; 5749 else 5750 tmp = LAN8841_GPIO_DATA_SEL_GPIO_DATA_SEL_EVENT_B; 5751 5752 if (pin < 5) 5753 ret = phy_set_bits_mmd(phydev, 2, LAN8841_GPIO_DATA_SEL1, 5754 tmp << (3 * pin)); 5755 else 5756 ret = phy_set_bits_mmd(phydev, 2, LAN8841_GPIO_DATA_SEL2, 5757 tmp << (3 * (pin - 5))); 5758 5759 return ret; 5760 } 5761 5762 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_200MS 13 5763 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100MS 12 5764 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_50MS 11 5765 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_10MS 10 5766 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_5MS 9 5767 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_1MS 8 5768 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_500US 7 5769 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100US 6 5770 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_50US 5 5771 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_10US 4 5772 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_5US 3 5773 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_1US 2 5774 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_500NS 1 5775 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100NS 0 5776 5777 static int lan8841_ptp_perout(struct ptp_clock_info *ptp, 5778 struct ptp_clock_request *rq, int on) 5779 { 5780 struct kszphy_ptp_priv *ptp_priv = container_of(ptp, struct kszphy_ptp_priv, 5781 ptp_clock_info); 5782 struct phy_device *phydev = ptp_priv->phydev; 5783 struct timespec64 ts_on, ts_period; 5784 s64 on_nsec, period_nsec; 5785 int pulse_width; 5786 int pin; 5787 int ret; 5788 5789 pin = ptp_find_pin(ptp_priv->ptp_clock, PTP_PF_PEROUT, rq->perout.index); 5790 if (pin == -1 || pin >= LAN8841_PTP_GPIO_NUM) 5791 return -EINVAL; 5792 5793 if (!on) { 5794 ret = lan8841_ptp_perout_off(ptp_priv, pin); 5795 if (ret) 5796 return ret; 5797 5798 return lan8841_ptp_remove_event(ptp_priv, LAN8841_EVENT_A, pin); 5799 } 5800 5801 ts_on.tv_sec = rq->perout.on.sec; 5802 ts_on.tv_nsec = rq->perout.on.nsec; 5803 on_nsec = timespec64_to_ns(&ts_on); 5804 5805 ts_period.tv_sec = rq->perout.period.sec; 5806 ts_period.tv_nsec = rq->perout.period.nsec; 5807 period_nsec = timespec64_to_ns(&ts_period); 5808 5809 if (period_nsec < 200) { 5810 pr_warn_ratelimited("%s: perout period too small, minimum is 200 nsec\n", 5811 phydev_name(phydev)); 5812 return -EOPNOTSUPP; 5813 } 5814 5815 if (on_nsec >= period_nsec) { 5816 pr_warn_ratelimited("%s: pulse width must be smaller than period\n", 5817 phydev_name(phydev)); 5818 return -EINVAL; 5819 } 5820 5821 switch (on_nsec) { 5822 case 200000000: 5823 pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_200MS; 5824 break; 5825 case 100000000: 5826 pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100MS; 5827 break; 5828 case 50000000: 5829 pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_50MS; 5830 break; 5831 case 10000000: 5832 pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_10MS; 5833 break; 5834 case 5000000: 5835 pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_5MS; 5836 break; 5837 case 1000000: 5838 pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_1MS; 5839 break; 5840 case 500000: 5841 pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_500US; 5842 break; 5843 case 100000: 5844 pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100US; 5845 break; 5846 case 50000: 5847 pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_50US; 5848 break; 5849 case 10000: 5850 pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_10US; 5851 break; 5852 case 5000: 5853 pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_5US; 5854 break; 5855 case 1000: 5856 pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_1US; 5857 break; 5858 case 500: 5859 pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_500NS; 5860 break; 5861 case 100: 5862 pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100NS; 5863 break; 5864 default: 5865 pr_warn_ratelimited("%s: Use default duty cycle of 100ns\n", 5866 phydev_name(phydev)); 5867 pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100NS; 5868 break; 5869 } 5870 5871 mutex_lock(&ptp_priv->ptp_lock); 5872 ret = lan8841_ptp_set_target(ptp_priv, LAN8841_EVENT_A, rq->perout.start.sec, 5873 rq->perout.start.nsec); 5874 mutex_unlock(&ptp_priv->ptp_lock); 5875 if (ret) 5876 return ret; 5877 5878 ret = lan8841_ptp_set_reload(ptp_priv, LAN8841_EVENT_A, rq->perout.period.sec, 5879 rq->perout.period.nsec); 5880 if (ret) 5881 return ret; 5882 5883 ret = lan8841_ptp_enable_event(ptp_priv, pin, LAN8841_EVENT_A, 5884 pulse_width); 5885 if (ret) 5886 return ret; 5887 5888 ret = lan8841_ptp_perout_on(ptp_priv, pin); 5889 if (ret) 5890 lan8841_ptp_remove_event(ptp_priv, pin, LAN8841_EVENT_A); 5891 5892 return ret; 5893 } 5894 5895 #define LAN8841_PTP_GPIO_CAP_EN 496 5896 #define LAN8841_PTP_GPIO_CAP_EN_GPIO_RE_CAPTURE_ENABLE(gpio) (BIT(gpio)) 5897 #define LAN8841_PTP_GPIO_CAP_EN_GPIO_FE_CAPTURE_ENABLE(gpio) (BIT(gpio) << 8) 5898 #define LAN8841_PTP_INT_EN_PTP_GPIO_CAP_EN BIT(2) 5899 5900 static int lan8841_ptp_extts_on(struct kszphy_ptp_priv *ptp_priv, int pin, 5901 u32 flags) 5902 { 5903 struct phy_device *phydev = ptp_priv->phydev; 5904 u16 tmp = 0; 5905 int ret; 5906 5907 /* Set GPIO to be input */ 5908 ret = phy_set_bits_mmd(phydev, 2, LAN8841_GPIO_EN, BIT(pin)); 5909 if (ret) 5910 return ret; 5911 5912 ret = phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_BUF, BIT(pin)); 5913 if (ret) 5914 return ret; 5915 5916 /* Enable capture on the edges of the pin */ 5917 if (flags & PTP_RISING_EDGE) 5918 tmp |= LAN8841_PTP_GPIO_CAP_EN_GPIO_RE_CAPTURE_ENABLE(pin); 5919 if (flags & PTP_FALLING_EDGE) 5920 tmp |= LAN8841_PTP_GPIO_CAP_EN_GPIO_FE_CAPTURE_ENABLE(pin); 5921 ret = phy_write_mmd(phydev, 2, LAN8841_PTP_GPIO_CAP_EN, tmp); 5922 if (ret) 5923 return ret; 5924 5925 /* Enable interrupt */ 5926 return phy_modify_mmd(phydev, 2, LAN8841_PTP_INT_EN, 5927 LAN8841_PTP_INT_EN_PTP_GPIO_CAP_EN, 5928 LAN8841_PTP_INT_EN_PTP_GPIO_CAP_EN); 5929 } 5930 5931 static int lan8841_ptp_extts_off(struct kszphy_ptp_priv *ptp_priv, int pin) 5932 { 5933 struct phy_device *phydev = ptp_priv->phydev; 5934 int ret; 5935 5936 /* Set GPIO to be output */ 5937 ret = phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_EN, BIT(pin)); 5938 if (ret) 5939 return ret; 5940 5941 ret = phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_BUF, BIT(pin)); 5942 if (ret) 5943 return ret; 5944 5945 /* Disable capture on both of the edges */ 5946 ret = phy_modify_mmd(phydev, 2, LAN8841_PTP_GPIO_CAP_EN, 5947 LAN8841_PTP_GPIO_CAP_EN_GPIO_RE_CAPTURE_ENABLE(pin) | 5948 LAN8841_PTP_GPIO_CAP_EN_GPIO_FE_CAPTURE_ENABLE(pin), 5949 0); 5950 if (ret) 5951 return ret; 5952 5953 /* Disable interrupt */ 5954 return phy_modify_mmd(phydev, 2, LAN8841_PTP_INT_EN, 5955 LAN8841_PTP_INT_EN_PTP_GPIO_CAP_EN, 5956 0); 5957 } 5958 5959 static int lan8841_ptp_extts(struct ptp_clock_info *ptp, 5960 struct ptp_clock_request *rq, int on) 5961 { 5962 struct kszphy_ptp_priv *ptp_priv = container_of(ptp, struct kszphy_ptp_priv, 5963 ptp_clock_info); 5964 int pin; 5965 int ret; 5966 5967 /* Reject requests with unsupported flags */ 5968 if (rq->extts.flags & ~(PTP_ENABLE_FEATURE | 5969 PTP_EXTTS_EDGES | 5970 PTP_STRICT_FLAGS)) 5971 return -EOPNOTSUPP; 5972 5973 pin = ptp_find_pin(ptp_priv->ptp_clock, PTP_PF_EXTTS, rq->extts.index); 5974 if (pin == -1 || pin >= LAN8841_PTP_GPIO_NUM) 5975 return -EINVAL; 5976 5977 mutex_lock(&ptp_priv->ptp_lock); 5978 if (on) 5979 ret = lan8841_ptp_extts_on(ptp_priv, pin, rq->extts.flags); 5980 else 5981 ret = lan8841_ptp_extts_off(ptp_priv, pin); 5982 mutex_unlock(&ptp_priv->ptp_lock); 5983 5984 return ret; 5985 } 5986 5987 static int lan8841_ptp_enable(struct ptp_clock_info *ptp, 5988 struct ptp_clock_request *rq, int on) 5989 { 5990 switch (rq->type) { 5991 case PTP_CLK_REQ_EXTTS: 5992 return lan8841_ptp_extts(ptp, rq, on); 5993 case PTP_CLK_REQ_PEROUT: 5994 return lan8841_ptp_perout(ptp, rq, on); 5995 default: 5996 return -EOPNOTSUPP; 5997 } 5998 5999 return 0; 6000 } 6001 6002 static long lan8841_ptp_do_aux_work(struct ptp_clock_info *ptp) 6003 { 6004 struct kszphy_ptp_priv *ptp_priv = container_of(ptp, struct kszphy_ptp_priv, 6005 ptp_clock_info); 6006 struct timespec64 ts; 6007 unsigned long flags; 6008 6009 lan8841_ptp_getseconds(&ptp_priv->ptp_clock_info, &ts); 6010 6011 spin_lock_irqsave(&ptp_priv->seconds_lock, flags); 6012 ptp_priv->seconds = ts.tv_sec; 6013 spin_unlock_irqrestore(&ptp_priv->seconds_lock, flags); 6014 6015 return nsecs_to_jiffies(LAN8841_GET_SEC_LTC_DELAY); 6016 } 6017 6018 static struct ptp_clock_info lan8841_ptp_clock_info = { 6019 .owner = THIS_MODULE, 6020 .name = "lan8841 ptp", 6021 .max_adj = 31249999, 6022 .gettime64 = lan8841_ptp_gettime64, 6023 .settime64 = lan8841_ptp_settime64, 6024 .adjtime = lan8841_ptp_adjtime, 6025 .adjfine = lan8841_ptp_adjfine, 6026 .verify = lan8841_ptp_verify, 6027 .enable = lan8841_ptp_enable, 6028 .do_aux_work = lan8841_ptp_do_aux_work, 6029 .n_per_out = LAN8841_PTP_GPIO_NUM, 6030 .n_ext_ts = LAN8841_PTP_GPIO_NUM, 6031 .n_pins = LAN8841_PTP_GPIO_NUM, 6032 .supported_perout_flags = PTP_PEROUT_DUTY_CYCLE, 6033 }; 6034 6035 #define LAN8841_OPERATION_MODE_STRAP_LOW_REGISTER 3 6036 #define LAN8841_OPERATION_MODE_STRAP_LOW_REGISTER_STRAP_RGMII_EN BIT(0) 6037 6038 static int lan8841_probe(struct phy_device *phydev) 6039 { 6040 struct kszphy_ptp_priv *ptp_priv; 6041 struct kszphy_priv *priv; 6042 int err; 6043 6044 err = kszphy_probe(phydev); 6045 if (err) 6046 return err; 6047 6048 if (phy_read_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG, 6049 LAN8841_OPERATION_MODE_STRAP_LOW_REGISTER) & 6050 LAN8841_OPERATION_MODE_STRAP_LOW_REGISTER_STRAP_RGMII_EN) 6051 phydev->interface = PHY_INTERFACE_MODE_RGMII_RXID; 6052 6053 /* Register the clock */ 6054 if (!IS_ENABLED(CONFIG_NETWORK_PHY_TIMESTAMPING)) 6055 return 0; 6056 6057 priv = phydev->priv; 6058 ptp_priv = &priv->ptp_priv; 6059 6060 ptp_priv->pin_config = devm_kzalloc_objs(&phydev->mdio.dev, 6061 *ptp_priv->pin_config, 6062 LAN8841_PTP_GPIO_NUM); 6063 if (!ptp_priv->pin_config) 6064 return -ENOMEM; 6065 6066 for (int i = 0; i < LAN8841_PTP_GPIO_NUM; ++i) { 6067 struct ptp_pin_desc *p = &ptp_priv->pin_config[i]; 6068 6069 snprintf(p->name, sizeof(p->name), "pin%d", i); 6070 p->index = i; 6071 p->func = PTP_PF_NONE; 6072 } 6073 6074 ptp_priv->ptp_clock_info = lan8841_ptp_clock_info; 6075 ptp_priv->ptp_clock_info.pin_config = ptp_priv->pin_config; 6076 ptp_priv->ptp_clock = ptp_clock_register(&ptp_priv->ptp_clock_info, 6077 &phydev->mdio.dev); 6078 if (IS_ERR(ptp_priv->ptp_clock)) { 6079 phydev_err(phydev, "ptp_clock_register failed: %pe\n", 6080 ptp_priv->ptp_clock); 6081 return -EINVAL; 6082 } 6083 6084 if (!ptp_priv->ptp_clock) 6085 return 0; 6086 6087 /* Initialize the SW */ 6088 skb_queue_head_init(&ptp_priv->tx_queue); 6089 ptp_priv->phydev = phydev; 6090 mutex_init(&ptp_priv->ptp_lock); 6091 spin_lock_init(&ptp_priv->seconds_lock); 6092 6093 ptp_priv->mii_ts.rxtstamp = lan8841_rxtstamp; 6094 ptp_priv->mii_ts.txtstamp = lan8814_txtstamp; 6095 ptp_priv->mii_ts.hwtstamp_set = lan8841_hwtstamp_set; 6096 ptp_priv->mii_ts.hwtstamp_get = lan8814_hwtstamp_get; 6097 ptp_priv->mii_ts.ts_info = lan8841_ts_info; 6098 6099 phydev->mii_ts = &ptp_priv->mii_ts; 6100 6101 /* Timestamp selected by default to keep legacy API */ 6102 phydev->default_timestamp = true; 6103 6104 return 0; 6105 } 6106 6107 static int lan8804_resume(struct phy_device *phydev) 6108 { 6109 return kszphy_resume(phydev); 6110 } 6111 6112 static int lan8804_suspend(struct phy_device *phydev) 6113 { 6114 return kszphy_generic_suspend(phydev); 6115 } 6116 6117 static int lan8841_resume(struct phy_device *phydev) 6118 { 6119 return kszphy_generic_resume(phydev); 6120 } 6121 6122 static int lan8841_suspend(struct phy_device *phydev) 6123 { 6124 struct kszphy_priv *priv = phydev->priv; 6125 struct kszphy_ptp_priv *ptp_priv = &priv->ptp_priv; 6126 6127 if (ptp_priv->ptp_clock) 6128 ptp_cancel_worker_sync(ptp_priv->ptp_clock); 6129 6130 return kszphy_generic_suspend(phydev); 6131 } 6132 6133 static int ksz9131_resume(struct phy_device *phydev) 6134 { 6135 if (phydev->suspended && phy_interface_is_rgmii(phydev)) 6136 ksz9131_config_rgmii_delay(phydev); 6137 6138 return kszphy_resume(phydev); 6139 } 6140 6141 #define LAN8842_PTP_GPIO_NUM 16 6142 6143 static int lan8842_ptp_probe_once(struct phy_device *phydev) 6144 { 6145 return __lan8814_ptp_probe_once(phydev, "lan8842_ptp_pin", 6146 LAN8842_PTP_GPIO_NUM); 6147 } 6148 6149 #define LAN8842_STRAP_REG 0 /* 0x0 */ 6150 #define LAN8842_STRAP_REG_PHYADDR_MASK GENMASK(4, 0) 6151 #define LAN8842_SKU_REG 11 /* 0x0b */ 6152 #define LAN8842_SELF_TEST 14 /* 0x0e */ 6153 #define LAN8842_SELF_TEST_RX_CNT_ENA BIT(8) 6154 #define LAN8842_SELF_TEST_TX_CNT_ENA BIT(4) 6155 6156 static int lan8842_probe(struct phy_device *phydev) 6157 { 6158 struct lan8842_priv *priv; 6159 int addr; 6160 int ret; 6161 6162 priv = devm_kzalloc_obj(&phydev->mdio.dev, *priv); 6163 if (!priv) 6164 return -ENOMEM; 6165 6166 phydev->priv = priv; 6167 6168 /* Similar to lan8814 this PHY has a pin which needs to be pulled down 6169 * to enable to pass any traffic through it. Therefore use the same 6170 * function as lan8814 6171 */ 6172 ret = lan8814_release_coma_mode(phydev); 6173 if (ret) 6174 return ret; 6175 6176 /* Enable to count the RX and TX packets */ 6177 ret = lanphy_write_page_reg(phydev, LAN8814_PAGE_PCS_DIGITAL, 6178 LAN8842_SELF_TEST, 6179 LAN8842_SELF_TEST_RX_CNT_ENA | 6180 LAN8842_SELF_TEST_TX_CNT_ENA); 6181 if (ret < 0) 6182 return ret; 6183 6184 /* Revision lan8832 doesn't have support for PTP, therefore don't add 6185 * any PTP clocks 6186 */ 6187 ret = lanphy_read_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, 6188 LAN8842_SKU_REG); 6189 if (ret < 0) 6190 return ret; 6191 6192 priv->rev = ret; 6193 if (priv->rev == LAN8842_REV_8832) 6194 return 0; 6195 6196 /* As the lan8814 and lan8842 has the same IP for the PTP block, the 6197 * only difference is the number of the GPIOs, then make sure that the 6198 * lan8842 initialized also the shared data pointer as this is used in 6199 * all the PTP functions for lan8814. The lan8842 doesn't have multiple 6200 * PHYs in the same package. 6201 */ 6202 addr = lanphy_read_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, 6203 LAN8842_STRAP_REG); 6204 if (addr < 0) 6205 return addr; 6206 addr &= LAN8842_STRAP_REG_PHYADDR_MASK; 6207 6208 ret = devm_phy_package_join(&phydev->mdio.dev, phydev, addr, 6209 sizeof(struct lan8814_shared_priv)); 6210 if (ret) 6211 return ret; 6212 6213 if (phy_package_init_once(phydev)) { 6214 ret = lan8842_ptp_probe_once(phydev); 6215 if (ret) 6216 return ret; 6217 } 6218 6219 lan8814_ptp_init(phydev); 6220 6221 return 0; 6222 } 6223 6224 #define LAN8814_POWER_MGMT_MODE_3_ANEG_MDI 0x13 6225 #define LAN8814_POWER_MGMT_MODE_4_ANEG_MDIX 0x14 6226 #define LAN8814_POWER_MGMT_MODE_5_10BT_MDI 0x15 6227 #define LAN8814_POWER_MGMT_MODE_6_10BT_MDIX 0x16 6228 #define LAN8814_POWER_MGMT_MODE_7_100BT_TRAIN 0x17 6229 #define LAN8814_POWER_MGMT_MODE_8_100BT_MDI 0x18 6230 #define LAN8814_POWER_MGMT_MODE_9_100BT_EEE_MDI_TX 0x19 6231 #define LAN8814_POWER_MGMT_MODE_10_100BT_EEE_MDI_RX 0x1a 6232 #define LAN8814_POWER_MGMT_MODE_11_100BT_MDIX 0x1b 6233 #define LAN8814_POWER_MGMT_MODE_12_100BT_EEE_MDIX_TX 0x1c 6234 #define LAN8814_POWER_MGMT_MODE_13_100BT_EEE_MDIX_RX 0x1d 6235 #define LAN8814_POWER_MGMT_MODE_14_100BTX_EEE_TX_RX 0x1e 6236 6237 #define LAN8814_POWER_MGMT_DLLPD_D BIT(0) 6238 #define LAN8814_POWER_MGMT_ADCPD_D BIT(1) 6239 #define LAN8814_POWER_MGMT_PGAPD_D BIT(2) 6240 #define LAN8814_POWER_MGMT_TXPD_D BIT(3) 6241 #define LAN8814_POWER_MGMT_DLLPD_C BIT(4) 6242 #define LAN8814_POWER_MGMT_ADCPD_C BIT(5) 6243 #define LAN8814_POWER_MGMT_PGAPD_C BIT(6) 6244 #define LAN8814_POWER_MGMT_TXPD_C BIT(7) 6245 #define LAN8814_POWER_MGMT_DLLPD_B BIT(8) 6246 #define LAN8814_POWER_MGMT_ADCPD_B BIT(9) 6247 #define LAN8814_POWER_MGMT_PGAPD_B BIT(10) 6248 #define LAN8814_POWER_MGMT_TXPD_B BIT(11) 6249 #define LAN8814_POWER_MGMT_DLLPD_A BIT(12) 6250 #define LAN8814_POWER_MGMT_ADCPD_A BIT(13) 6251 #define LAN8814_POWER_MGMT_PGAPD_A BIT(14) 6252 #define LAN8814_POWER_MGMT_TXPD_A BIT(15) 6253 6254 #define LAN8814_POWER_MGMT_C_D (LAN8814_POWER_MGMT_DLLPD_D | \ 6255 LAN8814_POWER_MGMT_ADCPD_D | \ 6256 LAN8814_POWER_MGMT_PGAPD_D | \ 6257 LAN8814_POWER_MGMT_DLLPD_C | \ 6258 LAN8814_POWER_MGMT_ADCPD_C | \ 6259 LAN8814_POWER_MGMT_PGAPD_C) 6260 6261 #define LAN8814_POWER_MGMT_B_C_D (LAN8814_POWER_MGMT_C_D | \ 6262 LAN8814_POWER_MGMT_DLLPD_B | \ 6263 LAN8814_POWER_MGMT_ADCPD_B | \ 6264 LAN8814_POWER_MGMT_PGAPD_B) 6265 6266 #define LAN8814_POWER_MGMT_VAL1 (LAN8814_POWER_MGMT_C_D | \ 6267 LAN8814_POWER_MGMT_ADCPD_B | \ 6268 LAN8814_POWER_MGMT_PGAPD_B | \ 6269 LAN8814_POWER_MGMT_ADCPD_A | \ 6270 LAN8814_POWER_MGMT_PGAPD_A) 6271 6272 #define LAN8814_POWER_MGMT_VAL2 LAN8814_POWER_MGMT_C_D 6273 6274 #define LAN8814_POWER_MGMT_VAL3 (LAN8814_POWER_MGMT_C_D | \ 6275 LAN8814_POWER_MGMT_DLLPD_B | \ 6276 LAN8814_POWER_MGMT_ADCPD_B | \ 6277 LAN8814_POWER_MGMT_PGAPD_A) 6278 6279 #define LAN8814_POWER_MGMT_VAL4 (LAN8814_POWER_MGMT_B_C_D | \ 6280 LAN8814_POWER_MGMT_ADCPD_A | \ 6281 LAN8814_POWER_MGMT_PGAPD_A) 6282 6283 #define LAN8814_POWER_MGMT_VAL5 LAN8814_POWER_MGMT_B_C_D 6284 6285 #define LAN8814_EEE_WAKE_TX_TIMER 0x0e 6286 #define LAN8814_EEE_WAKE_TX_TIMER_MAX_VAL 0x1f 6287 6288 static const struct lanphy_reg_data short_center_tap_errata[] = { 6289 { LAN8814_PAGE_POWER_REGS, 6290 LAN8814_POWER_MGMT_MODE_3_ANEG_MDI, 6291 LAN8814_POWER_MGMT_VAL1 }, 6292 { LAN8814_PAGE_POWER_REGS, 6293 LAN8814_POWER_MGMT_MODE_4_ANEG_MDIX, 6294 LAN8814_POWER_MGMT_VAL1 }, 6295 { LAN8814_PAGE_POWER_REGS, 6296 LAN8814_POWER_MGMT_MODE_5_10BT_MDI, 6297 LAN8814_POWER_MGMT_VAL1 }, 6298 { LAN8814_PAGE_POWER_REGS, 6299 LAN8814_POWER_MGMT_MODE_6_10BT_MDIX, 6300 LAN8814_POWER_MGMT_VAL1 }, 6301 { LAN8814_PAGE_POWER_REGS, 6302 LAN8814_POWER_MGMT_MODE_7_100BT_TRAIN, 6303 LAN8814_POWER_MGMT_VAL2 }, 6304 { LAN8814_PAGE_POWER_REGS, 6305 LAN8814_POWER_MGMT_MODE_8_100BT_MDI, 6306 LAN8814_POWER_MGMT_VAL3 }, 6307 { LAN8814_PAGE_POWER_REGS, 6308 LAN8814_POWER_MGMT_MODE_9_100BT_EEE_MDI_TX, 6309 LAN8814_POWER_MGMT_VAL3 }, 6310 { LAN8814_PAGE_POWER_REGS, 6311 LAN8814_POWER_MGMT_MODE_10_100BT_EEE_MDI_RX, 6312 LAN8814_POWER_MGMT_VAL4 }, 6313 { LAN8814_PAGE_POWER_REGS, 6314 LAN8814_POWER_MGMT_MODE_11_100BT_MDIX, 6315 LAN8814_POWER_MGMT_VAL5 }, 6316 { LAN8814_PAGE_POWER_REGS, 6317 LAN8814_POWER_MGMT_MODE_12_100BT_EEE_MDIX_TX, 6318 LAN8814_POWER_MGMT_VAL5 }, 6319 { LAN8814_PAGE_POWER_REGS, 6320 LAN8814_POWER_MGMT_MODE_13_100BT_EEE_MDIX_RX, 6321 LAN8814_POWER_MGMT_VAL4 }, 6322 { LAN8814_PAGE_POWER_REGS, 6323 LAN8814_POWER_MGMT_MODE_14_100BTX_EEE_TX_RX, 6324 LAN8814_POWER_MGMT_VAL4 }, 6325 }; 6326 6327 static const struct lanphy_reg_data waketx_timer_errata[] = { 6328 { LAN8814_PAGE_EEE, 6329 LAN8814_EEE_WAKE_TX_TIMER, 6330 LAN8814_EEE_WAKE_TX_TIMER_MAX_VAL }, 6331 }; 6332 6333 static int lanphy_write_reg_data(struct phy_device *phydev, 6334 const struct lanphy_reg_data *data, 6335 size_t num) 6336 { 6337 int ret = 0; 6338 6339 while (num--) { 6340 ret = lanphy_write_page_reg(phydev, data->page, data->addr, 6341 data->val); 6342 if (ret) 6343 break; 6344 data++; 6345 } 6346 6347 return ret; 6348 } 6349 6350 static int lan8842_erratas(struct phy_device *phydev) 6351 { 6352 int ret; 6353 6354 ret = lanphy_write_reg_data(phydev, short_center_tap_errata, 6355 ARRAY_SIZE(short_center_tap_errata)); 6356 if (ret) 6357 return ret; 6358 6359 return lanphy_write_reg_data(phydev, waketx_timer_errata, 6360 ARRAY_SIZE(waketx_timer_errata)); 6361 } 6362 6363 static int lan8842_config_init(struct phy_device *phydev) 6364 { 6365 int ret; 6366 6367 /* Reset the PHY */ 6368 ret = lanphy_modify_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, 6369 LAN8814_QSGMII_SOFT_RESET, 6370 LAN8814_QSGMII_SOFT_RESET_BIT, 6371 LAN8814_QSGMII_SOFT_RESET_BIT); 6372 if (ret < 0) 6373 return ret; 6374 6375 /* Apply the erratas for this device */ 6376 ret = lan8842_erratas(phydev); 6377 if (ret < 0) 6378 return ret; 6379 6380 /* Even if the GPIOs are set to control the LEDs the behaviour of the 6381 * LEDs is wrong, they are not blinking when there is traffic. 6382 * To fix this it is required to set extended LED mode 6383 */ 6384 ret = lanphy_modify_page_reg(phydev, LAN8814_PAGE_PORT_REGS, 6385 LAN8814_LED_CTRL_1, 6386 LAN8814_LED_CTRL_1_KSZ9031_LED_MODE_, 0); 6387 if (ret < 0) 6388 return ret; 6389 6390 ret = lanphy_modify_page_reg(phydev, LAN8814_PAGE_PORT_REGS, 6391 LAN8814_LED_CTRL_2, 6392 LAN8814_LED_CTRL_2_LED1_COM_DIS, 6393 LAN8814_LED_CTRL_2_LED1_COM_DIS); 6394 if (ret < 0) 6395 return ret; 6396 6397 /* To allow the PHY to control the LEDs the GPIOs of the PHY should have 6398 * a function mode and not the GPIO. Apparently by default the value is 6399 * GPIO and not function even though the datasheet it says that it is 6400 * function. Therefore set this value. 6401 */ 6402 return lanphy_write_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, 6403 LAN8814_GPIO_EN2, 0); 6404 } 6405 6406 #define LAN8842_INTR_CTRL_REG 52 /* 0x34 */ 6407 6408 static int lan8842_config_intr(struct phy_device *phydev) 6409 { 6410 int err; 6411 6412 lanphy_write_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, 6413 LAN8842_INTR_CTRL_REG, 6414 LAN8814_INTR_CTRL_REG_INTR_ENABLE); 6415 6416 /* enable / disable interrupts */ 6417 if (phydev->interrupts == PHY_INTERRUPT_ENABLED) { 6418 err = lan8814_ack_interrupt(phydev); 6419 if (err) 6420 return err; 6421 6422 err = phy_write(phydev, LAN8814_INTC, 6423 LAN8814_INT_LINK | LAN8814_INT_FLF); 6424 } else { 6425 err = phy_write(phydev, LAN8814_INTC, 0); 6426 if (err) 6427 return err; 6428 6429 err = lan8814_ack_interrupt(phydev); 6430 } 6431 6432 return err; 6433 } 6434 6435 static unsigned int lan8842_inband_caps(struct phy_device *phydev, 6436 phy_interface_t interface) 6437 { 6438 /* Inband configuration can be enabled or disabled using the registers 6439 * PCS1G_ANEG_CONFIG. 6440 */ 6441 return LINK_INBAND_DISABLE | LINK_INBAND_ENABLE; 6442 } 6443 6444 static int lan8842_config_inband(struct phy_device *phydev, unsigned int modes) 6445 { 6446 bool enable; 6447 6448 if (modes == LINK_INBAND_DISABLE) 6449 enable = false; 6450 else 6451 enable = true; 6452 6453 /* Disable or enable in-band autoneg with PCS Host side 6454 * It has the same address as lan8814 6455 */ 6456 return lanphy_modify_page_reg(phydev, LAN8814_PAGE_PORT_REGS, 6457 LAN8814_QSGMII_PCS1G_ANEG_CONFIG, 6458 LAN8814_QSGMII_PCS1G_ANEG_CONFIG_ANEG_ENA, 6459 enable ? LAN8814_QSGMII_PCS1G_ANEG_CONFIG_ANEG_ENA : 0); 6460 } 6461 6462 static void lan8842_handle_ptp_interrupt(struct phy_device *phydev, u16 status) 6463 { 6464 struct kszphy_ptp_priv *ptp_priv; 6465 struct lan8842_priv *priv; 6466 6467 priv = phydev->priv; 6468 ptp_priv = &priv->ptp_priv; 6469 6470 if (status & PTP_TSU_INT_STS_PTP_TX_TS_EN_) 6471 lan8814_get_tx_ts(ptp_priv); 6472 6473 if (status & PTP_TSU_INT_STS_PTP_RX_TS_EN_) 6474 lan8814_get_rx_ts(ptp_priv); 6475 6476 if (status & PTP_TSU_INT_STS_PTP_TX_TS_OVRFL_INT_) { 6477 lan8814_flush_fifo(phydev, true); 6478 skb_queue_purge(&ptp_priv->tx_queue); 6479 } 6480 6481 if (status & PTP_TSU_INT_STS_PTP_RX_TS_OVRFL_INT_) { 6482 lan8814_flush_fifo(phydev, false); 6483 skb_queue_purge(&ptp_priv->rx_queue); 6484 } 6485 } 6486 6487 static irqreturn_t lan8842_handle_interrupt(struct phy_device *phydev) 6488 { 6489 struct lan8842_priv *priv = phydev->priv; 6490 int ret = IRQ_NONE; 6491 int irq_status; 6492 6493 irq_status = phy_read(phydev, LAN8814_INTS); 6494 if (irq_status < 0) { 6495 phy_error(phydev); 6496 return IRQ_NONE; 6497 } 6498 6499 if (irq_status & (LAN8814_INT_LINK | LAN8814_INT_FLF)) { 6500 phy_trigger_machine(phydev); 6501 ret = IRQ_HANDLED; 6502 } 6503 6504 /* Phy revision lan8832 doesn't have support for PTP therefore there is 6505 * not need to check the PTP and GPIO interrupts 6506 */ 6507 if (priv->rev == LAN8842_REV_8832) 6508 goto out; 6509 6510 while (true) { 6511 irq_status = lanphy_read_page_reg(phydev, LAN8814_PAGE_PORT_REGS, 6512 PTP_TSU_INT_STS); 6513 if (!irq_status) 6514 break; 6515 6516 lan8842_handle_ptp_interrupt(phydev, irq_status); 6517 ret = IRQ_HANDLED; 6518 } 6519 6520 if (!lan8814_handle_gpio_interrupt(phydev, irq_status)) 6521 ret = IRQ_HANDLED; 6522 6523 out: 6524 return ret; 6525 } 6526 6527 static u64 lan8842_get_stat(struct phy_device *phydev, int count, int *regs) 6528 { 6529 u64 ret = 0; 6530 int val; 6531 6532 for (int j = 0; j < count; ++j) { 6533 val = lanphy_read_page_reg(phydev, LAN8814_PAGE_PCS_DIGITAL, 6534 regs[j]); 6535 if (val < 0) 6536 return U64_MAX; 6537 6538 ret <<= 16; 6539 ret += val; 6540 } 6541 return ret; 6542 } 6543 6544 static int lan8842_update_stats(struct phy_device *phydev) 6545 { 6546 struct lan8842_priv *priv = phydev->priv; 6547 int rx_packets_regs[] = {88, 61, 60}; 6548 int rx_errors_regs[] = {63, 62}; 6549 int tx_packets_regs[] = {89, 85, 84}; 6550 int tx_errors_regs[] = {87, 86}; 6551 6552 priv->phy_stats.rx_packets = lan8842_get_stat(phydev, 6553 ARRAY_SIZE(rx_packets_regs), 6554 rx_packets_regs); 6555 priv->phy_stats.rx_errors = lan8842_get_stat(phydev, 6556 ARRAY_SIZE(rx_errors_regs), 6557 rx_errors_regs); 6558 priv->phy_stats.tx_packets = lan8842_get_stat(phydev, 6559 ARRAY_SIZE(tx_packets_regs), 6560 tx_packets_regs); 6561 priv->phy_stats.tx_errors = lan8842_get_stat(phydev, 6562 ARRAY_SIZE(tx_errors_regs), 6563 tx_errors_regs); 6564 6565 return 0; 6566 } 6567 6568 #define LAN8842_FLF 15 /* 0x0e */ 6569 #define LAN8842_FLF_ENA BIT(1) 6570 #define LAN8842_FLF_ENA_LINK_DOWN BIT(0) 6571 6572 static int lan8842_get_fast_down(struct phy_device *phydev, u8 *msecs) 6573 { 6574 int ret; 6575 6576 ret = lanphy_read_page_reg(phydev, LAN8814_PAGE_PCS, LAN8842_FLF); 6577 if (ret < 0) 6578 return ret; 6579 6580 if (ret & LAN8842_FLF_ENA) 6581 *msecs = ETHTOOL_PHY_FAST_LINK_DOWN_ON; 6582 else 6583 *msecs = ETHTOOL_PHY_FAST_LINK_DOWN_OFF; 6584 6585 return 0; 6586 } 6587 6588 static int lan8842_set_fast_down(struct phy_device *phydev, const u8 *msecs) 6589 { 6590 u16 flf; 6591 6592 switch (*msecs) { 6593 case ETHTOOL_PHY_FAST_LINK_DOWN_OFF: 6594 flf = 0; 6595 break; 6596 case ETHTOOL_PHY_FAST_LINK_DOWN_ON: 6597 flf = LAN8842_FLF_ENA | LAN8842_FLF_ENA_LINK_DOWN; 6598 break; 6599 default: 6600 return -EINVAL; 6601 } 6602 6603 return lanphy_modify_page_reg(phydev, LAN8814_PAGE_PCS, 6604 LAN8842_FLF, 6605 LAN8842_FLF_ENA | 6606 LAN8842_FLF_ENA_LINK_DOWN, flf); 6607 } 6608 6609 static int lan8842_get_tunable(struct phy_device *phydev, 6610 struct ethtool_tunable *tuna, void *data) 6611 { 6612 switch (tuna->id) { 6613 case ETHTOOL_PHY_FAST_LINK_DOWN: 6614 return lan8842_get_fast_down(phydev, data); 6615 default: 6616 return -EOPNOTSUPP; 6617 } 6618 } 6619 6620 static int lan8842_set_tunable(struct phy_device *phydev, 6621 struct ethtool_tunable *tuna, const void *data) 6622 { 6623 switch (tuna->id) { 6624 case ETHTOOL_PHY_FAST_LINK_DOWN: 6625 return lan8842_set_fast_down(phydev, data); 6626 default: 6627 return -EOPNOTSUPP; 6628 } 6629 } 6630 6631 static void lan8842_get_phy_stats(struct phy_device *phydev, 6632 struct ethtool_eth_phy_stats *eth_stats, 6633 struct ethtool_phy_stats *stats) 6634 { 6635 struct lan8842_priv *priv = phydev->priv; 6636 6637 stats->rx_packets = priv->phy_stats.rx_packets; 6638 stats->rx_errors = priv->phy_stats.rx_errors; 6639 stats->tx_packets = priv->phy_stats.tx_packets; 6640 stats->tx_errors = priv->phy_stats.tx_errors; 6641 } 6642 6643 #define LAN9645X_CTRL_REG 0x1f 6644 #define LAN9645X_CTRL_REG_SW_SOFT_RST BIT(1) 6645 6646 #define LAN9645X_DAC_ICAS_AMP_POWER_DOWN 0x47 6647 #define LAN9645X_BTRX_QBIAS_POWER_DOWN 0x46 6648 #define LAN9645X_TX_LOW_I_CH_CD_POWER_MGMT 0x45 6649 #define LAN9645X_TX_LOW_I_CH_B_POWER_MGMT 0x44 6650 #define LAN9645X_TX_LOW_I_CH_A_POWER_MGMT 0x43 6651 6652 static const struct lanphy_reg_data force_dac_tx_errata[] = { 6653 /* Force channel A/B/C/D TX on */ 6654 { LAN8814_PAGE_POWER_REGS, 6655 LAN9645X_DAC_ICAS_AMP_POWER_DOWN, 6656 0 }, 6657 /* Force channel A/B/C/D QBias on */ 6658 { LAN8814_PAGE_POWER_REGS, 6659 LAN9645X_BTRX_QBIAS_POWER_DOWN, 6660 0xaa }, 6661 /* Tx low I on channel C/D overwrite */ 6662 { LAN8814_PAGE_POWER_REGS, 6663 LAN9645X_TX_LOW_I_CH_CD_POWER_MGMT, 6664 0xbfff }, 6665 /* Channel B low I overwrite */ 6666 { LAN8814_PAGE_POWER_REGS, 6667 LAN9645X_TX_LOW_I_CH_B_POWER_MGMT, 6668 0xabbf }, 6669 /* Channel A low I overwrite */ 6670 { LAN8814_PAGE_POWER_REGS, 6671 LAN9645X_TX_LOW_I_CH_A_POWER_MGMT, 6672 0xbd3f }, 6673 }; 6674 6675 static int lan9645x_config_init(struct phy_device *phydev) 6676 { 6677 int ret; 6678 6679 /* Apply erratas from previous generations. */ 6680 ret = lan8842_erratas(phydev); 6681 if (ret < 0) 6682 return ret; 6683 6684 /* Apply errata for an issue where bringing a port down, can cause a few 6685 * CRC errors for traffic flowing through adjacent ports. 6686 */ 6687 return lanphy_write_reg_data(phydev, force_dac_tx_errata, 6688 ARRAY_SIZE(force_dac_tx_errata)); 6689 } 6690 6691 static int lan9645x_suspend(struct phy_device *phydev) 6692 { 6693 int ret, val; 6694 6695 /* Force link down before software power down (SPD), by doing software 6696 * soft reset. This resets the PHY, but keeps all register configuration 6697 * intact. The bit self clears. 6698 * 6699 * This is needed as a workaround for an issue where performing SPD on a 6700 * port can bring adjacent ports down, when there is traffic flowing 6701 * through the ports. 6702 */ 6703 ret = phy_set_bits(phydev, LAN9645X_CTRL_REG, 6704 LAN9645X_CTRL_REG_SW_SOFT_RST); 6705 if (ret) 6706 return ret; 6707 6708 ret = phy_read_poll_timeout(phydev, LAN9645X_CTRL_REG, val, 6709 !(val & LAN9645X_CTRL_REG_SW_SOFT_RST), 6710 3000, 100000, true); 6711 if (ret) 6712 return ret; 6713 6714 return genphy_suspend(phydev); 6715 } 6716 6717 static int lan9645x_config_intr(struct phy_device *phydev) 6718 { 6719 int err; 6720 6721 /* enable / disable interrupts */ 6722 if (phydev->interrupts == PHY_INTERRUPT_ENABLED) { 6723 /* This is an internal PHY of lan9645x and is not possible to 6724 * change the polarity of irq sources in the OIC (CPU_INTR) 6725 * found in lan9645x. Therefore change the polarity of the 6726 * interrupt in the PHY from being active low instead of active 6727 * high. 6728 */ 6729 err = phy_write(phydev, LAN8804_CONTROL, 6730 LAN8804_CONTROL_INTR_POLARITY); 6731 if (err) 6732 return err; 6733 6734 /* By default interrupt buffer is open-drain in which case the 6735 * interrupt can be active only low. Therefore change the 6736 * interrupt buffer to be push-pull to be able to change 6737 * interrupt polarity. 6738 */ 6739 err = phy_write(phydev, LAN8804_OUTPUT_CONTROL, 6740 LAN8804_OUTPUT_CONTROL_INTR_BUFFER); 6741 if (err) 6742 return err; 6743 6744 err = lan8814_ack_interrupt(phydev); 6745 if (err) 6746 return err; 6747 6748 err = phy_write(phydev, LAN8814_INTC, 6749 LAN8814_INT_LINK | LAN8814_INT_FLF); 6750 } else { 6751 err = phy_write(phydev, LAN8814_INTC, 0); 6752 if (err) 6753 return err; 6754 6755 err = lan8814_ack_interrupt(phydev); 6756 } 6757 6758 return err; 6759 } 6760 6761 static irqreturn_t lan9645x_handle_interrupt(struct phy_device *phydev) 6762 { 6763 int status; 6764 6765 status = phy_read(phydev, LAN8814_INTS); 6766 if (status < 0) { 6767 phy_error(phydev); 6768 return IRQ_NONE; 6769 } 6770 6771 if (status & (LAN8814_INT_LINK | LAN8814_INT_FLF)) { 6772 phy_trigger_machine(phydev); 6773 return IRQ_HANDLED; 6774 } 6775 6776 return IRQ_NONE; 6777 } 6778 6779 static struct phy_driver ksphy_driver[] = { 6780 { 6781 PHY_ID_MATCH_MODEL(PHY_ID_KS8737), 6782 .name = "Micrel KS8737", 6783 /* PHY_BASIC_FEATURES */ 6784 .driver_data = &ks8737_type, 6785 .probe = kszphy_probe, 6786 .config_init = kszphy_config_init, 6787 .config_intr = kszphy_config_intr, 6788 .handle_interrupt = kszphy_handle_interrupt, 6789 .suspend = kszphy_suspend, 6790 .resume = kszphy_resume, 6791 }, { 6792 .phy_id = PHY_ID_KSZ8021, 6793 .phy_id_mask = 0x00ffffff, 6794 .name = "Micrel KSZ8021 or KSZ8031", 6795 /* PHY_BASIC_FEATURES */ 6796 .driver_data = &ksz8021_type, 6797 .probe = kszphy_probe, 6798 .config_init = kszphy_config_init, 6799 .config_intr = kszphy_config_intr, 6800 .handle_interrupt = kszphy_handle_interrupt, 6801 .get_sset_count = kszphy_get_sset_count, 6802 .get_strings = kszphy_get_strings, 6803 .get_stats = kszphy_get_stats, 6804 .suspend = kszphy_suspend, 6805 .resume = kszphy_resume, 6806 }, { 6807 .phy_id = PHY_ID_KSZ8031, 6808 .phy_id_mask = 0x00ffffff, 6809 .name = "Micrel KSZ8031", 6810 /* PHY_BASIC_FEATURES */ 6811 .driver_data = &ksz8021_type, 6812 .probe = kszphy_probe, 6813 .config_init = kszphy_config_init, 6814 .config_intr = kszphy_config_intr, 6815 .handle_interrupt = kszphy_handle_interrupt, 6816 .get_sset_count = kszphy_get_sset_count, 6817 .get_strings = kszphy_get_strings, 6818 .get_stats = kszphy_get_stats, 6819 .suspend = kszphy_suspend, 6820 .resume = kszphy_resume, 6821 }, { 6822 PHY_ID_MATCH_MODEL(PHY_ID_KSZ8041), 6823 .name = "Micrel KSZ8041", 6824 /* PHY_BASIC_FEATURES */ 6825 .driver_data = &ksz8041_type, 6826 .probe = kszphy_probe, 6827 .config_init = ksz8041_config_init, 6828 .config_aneg = ksz8041_config_aneg, 6829 .config_intr = kszphy_config_intr, 6830 .handle_interrupt = kszphy_handle_interrupt, 6831 .get_sset_count = kszphy_get_sset_count, 6832 .get_strings = kszphy_get_strings, 6833 .get_stats = kszphy_get_stats, 6834 .suspend = ksz8041_suspend, 6835 .resume = ksz8041_resume, 6836 }, { 6837 PHY_ID_MATCH_MODEL(PHY_ID_KSZ8041RNLI), 6838 .name = "Micrel KSZ8041RNLI", 6839 /* PHY_BASIC_FEATURES */ 6840 .driver_data = &ksz8041_type, 6841 .probe = kszphy_probe, 6842 .config_init = kszphy_config_init, 6843 .config_intr = kszphy_config_intr, 6844 .handle_interrupt = kszphy_handle_interrupt, 6845 .get_sset_count = kszphy_get_sset_count, 6846 .get_strings = kszphy_get_strings, 6847 .get_stats = kszphy_get_stats, 6848 .suspend = kszphy_suspend, 6849 .resume = kszphy_resume, 6850 }, { 6851 .name = "Micrel KSZ8051", 6852 /* PHY_BASIC_FEATURES */ 6853 .driver_data = &ksz8051_type, 6854 .probe = kszphy_probe, 6855 .config_init = kszphy_config_init, 6856 .config_intr = kszphy_config_intr, 6857 .handle_interrupt = kszphy_handle_interrupt, 6858 .get_sset_count = kszphy_get_sset_count, 6859 .get_strings = kszphy_get_strings, 6860 .get_stats = kszphy_get_stats, 6861 .match_phy_device = ksz8051_match_phy_device, 6862 .suspend = kszphy_suspend, 6863 .resume = kszphy_resume, 6864 }, { 6865 .phy_id = PHY_ID_KSZ8001, 6866 .name = "Micrel KSZ8001 or KS8721", 6867 .phy_id_mask = 0x00fffffc, 6868 /* PHY_BASIC_FEATURES */ 6869 .driver_data = &ksz8041_type, 6870 .probe = kszphy_probe, 6871 .config_init = kszphy_config_init, 6872 .config_intr = kszphy_config_intr, 6873 .handle_interrupt = kszphy_handle_interrupt, 6874 .get_sset_count = kszphy_get_sset_count, 6875 .get_strings = kszphy_get_strings, 6876 .get_stats = kszphy_get_stats, 6877 .suspend = kszphy_suspend, 6878 .resume = kszphy_resume, 6879 }, { 6880 PHY_ID_MATCH_MODEL(PHY_ID_KSZ8081), 6881 .name = "Micrel KSZ8081 or KSZ8091", 6882 .flags = PHY_POLL_CABLE_TEST, 6883 /* PHY_BASIC_FEATURES */ 6884 .driver_data = &ksz8081_type, 6885 .probe = kszphy_probe, 6886 .config_init = ksz8081_config_init, 6887 .soft_reset = genphy_soft_reset, 6888 .config_aneg = ksz8081_config_aneg, 6889 .read_status = ksz8081_read_status, 6890 .config_intr = kszphy_config_intr, 6891 .handle_interrupt = kszphy_handle_interrupt, 6892 .get_sset_count = kszphy_get_sset_count, 6893 .get_strings = kszphy_get_strings, 6894 .get_stats = kszphy_get_stats, 6895 .suspend = kszphy_suspend, 6896 .resume = kszphy_resume, 6897 .cable_test_start = ksz886x_cable_test_start, 6898 .cable_test_get_status = ksz886x_cable_test_get_status, 6899 }, { 6900 PHY_ID_MATCH_MODEL(PHY_ID_KSZ8061), 6901 .name = "Micrel KSZ8061", 6902 /* PHY_BASIC_FEATURES */ 6903 .probe = kszphy_probe, 6904 .config_init = ksz8061_config_init, 6905 .soft_reset = genphy_soft_reset, 6906 .config_intr = kszphy_config_intr, 6907 .handle_interrupt = kszphy_handle_interrupt, 6908 .suspend = ksz8061_suspend, 6909 .resume = ksz8061_resume, 6910 }, { 6911 .phy_id = PHY_ID_KSZ9021, 6912 .phy_id_mask = 0x000ffffe, 6913 .name = "Micrel KSZ9021 Gigabit PHY", 6914 /* PHY_GBIT_FEATURES */ 6915 .driver_data = &ksz9021_type, 6916 .probe = kszphy_probe, 6917 .get_features = ksz9031_get_features, 6918 .config_init = ksz9021_config_init, 6919 .config_intr = kszphy_config_intr, 6920 .handle_interrupt = kszphy_handle_interrupt, 6921 .get_sset_count = kszphy_get_sset_count, 6922 .get_strings = kszphy_get_strings, 6923 .get_stats = kszphy_get_stats, 6924 .suspend = kszphy_suspend, 6925 .resume = kszphy_resume, 6926 .read_mmd = genphy_read_mmd_unsupported, 6927 .write_mmd = genphy_write_mmd_unsupported, 6928 }, { 6929 PHY_ID_MATCH_MODEL(PHY_ID_KSZ9031), 6930 .name = "Micrel KSZ9031 Gigabit PHY", 6931 .flags = PHY_POLL_CABLE_TEST, 6932 .driver_data = &ksz9021_type, 6933 .probe = kszphy_probe, 6934 .get_features = ksz9031_get_features, 6935 .config_init = ksz9031_config_init, 6936 .soft_reset = genphy_soft_reset, 6937 .read_status = ksz9031_read_status, 6938 .config_intr = kszphy_config_intr, 6939 .handle_interrupt = kszphy_handle_interrupt, 6940 .get_sset_count = kszphy_get_sset_count, 6941 .get_strings = kszphy_get_strings, 6942 .get_stats = kszphy_get_stats, 6943 .suspend = kszphy_suspend, 6944 .resume = kszphy_resume, 6945 .cable_test_start = ksz9x31_cable_test_start, 6946 .cable_test_get_status = ksz9x31_cable_test_get_status, 6947 .set_loopback = ksz9031_set_loopback, 6948 }, { 6949 PHY_ID_MATCH_MODEL(PHY_ID_LAN8814), 6950 .name = "Microchip INDY Gigabit Quad PHY", 6951 .flags = PHY_POLL_CABLE_TEST, 6952 .config_init = lan8814_config_init, 6953 .driver_data = &lan8814_type, 6954 .probe = lan8814_probe, 6955 .soft_reset = genphy_soft_reset, 6956 .read_status = ksz9031_read_status, 6957 .get_sset_count = kszphy_get_sset_count, 6958 .get_strings = kszphy_get_strings, 6959 .get_stats = kszphy_get_stats, 6960 .suspend = genphy_suspend, 6961 .resume = kszphy_resume, 6962 .config_intr = lan8814_config_intr, 6963 .inband_caps = lan8842_inband_caps, 6964 .config_inband = lan8842_config_inband, 6965 .handle_interrupt = lan8814_handle_interrupt, 6966 .cable_test_start = lan8814_cable_test_start, 6967 .cable_test_get_status = ksz886x_cable_test_get_status, 6968 }, { 6969 PHY_ID_MATCH_MODEL(PHY_ID_LAN8804), 6970 .name = "Microchip LAN966X Gigabit PHY", 6971 .config_init = lan8804_config_init, 6972 .driver_data = &ksz9021_type, 6973 .probe = kszphy_probe, 6974 .soft_reset = genphy_soft_reset, 6975 .read_status = ksz9031_read_status, 6976 .get_sset_count = kszphy_get_sset_count, 6977 .get_strings = kszphy_get_strings, 6978 .get_stats = kszphy_get_stats, 6979 .suspend = lan8804_suspend, 6980 .resume = lan8804_resume, 6981 .config_intr = lan8804_config_intr, 6982 .handle_interrupt = lan8804_handle_interrupt, 6983 }, { 6984 PHY_ID_MATCH_MODEL(PHY_ID_LAN8841), 6985 .name = "Microchip LAN8841 Gigabit PHY", 6986 .flags = PHY_POLL_CABLE_TEST, 6987 .driver_data = &lan8841_type, 6988 .config_init = lan8841_config_init, 6989 .probe = lan8841_probe, 6990 .soft_reset = genphy_soft_reset, 6991 .config_intr = lan8841_config_intr, 6992 .handle_interrupt = lan8841_handle_interrupt, 6993 .get_sset_count = kszphy_get_sset_count, 6994 .get_strings = kszphy_get_strings, 6995 .get_stats = kszphy_get_stats, 6996 .suspend = lan8841_suspend, 6997 .resume = lan8841_resume, 6998 .cable_test_start = lan8814_cable_test_start, 6999 .cable_test_get_status = ksz886x_cable_test_get_status, 7000 }, { 7001 PHY_ID_MATCH_MODEL(PHY_ID_LAN8842), 7002 .name = "Microchip LAN8842 Gigabit PHY", 7003 .flags = PHY_POLL_CABLE_TEST, 7004 .driver_data = &lan8814_type, 7005 .probe = lan8842_probe, 7006 .config_init = lan8842_config_init, 7007 .config_intr = lan8842_config_intr, 7008 .inband_caps = lan8842_inband_caps, 7009 .config_inband = lan8842_config_inband, 7010 .handle_interrupt = lan8842_handle_interrupt, 7011 .get_phy_stats = lan8842_get_phy_stats, 7012 .update_stats = lan8842_update_stats, 7013 .get_tunable = lan8842_get_tunable, 7014 .set_tunable = lan8842_set_tunable, 7015 .cable_test_start = lan8814_cable_test_start, 7016 .cable_test_get_status = ksz886x_cable_test_get_status, 7017 }, { 7018 PHY_ID_MATCH_MODEL(PHY_ID_LAN9645X), 7019 .name = "Microchip LAN9645X Gigabit PHY", 7020 .config_init = lan9645x_config_init, 7021 .driver_data = &ksz9021_type, 7022 .probe = kszphy_probe, 7023 .soft_reset = genphy_soft_reset, 7024 .suspend = lan9645x_suspend, 7025 .resume = genphy_resume, 7026 .config_intr = lan9645x_config_intr, 7027 .handle_interrupt = lan9645x_handle_interrupt, 7028 .get_tunable = lan8842_get_tunable, 7029 .set_tunable = lan8842_set_tunable, 7030 .get_phy_stats = lan8842_get_phy_stats, 7031 .update_stats = lan8842_update_stats, 7032 }, { 7033 PHY_ID_MATCH_MODEL(PHY_ID_KSZ9131), 7034 .name = "Microchip KSZ9131 Gigabit PHY", 7035 /* PHY_GBIT_FEATURES */ 7036 .flags = PHY_POLL_CABLE_TEST, 7037 .driver_data = &ksz9131_type, 7038 .probe = kszphy_probe, 7039 .soft_reset = genphy_soft_reset, 7040 .config_init = ksz9131_config_init, 7041 .config_intr = kszphy_config_intr, 7042 .config_aneg = ksz9131_config_aneg, 7043 .read_status = ksz9131_read_status, 7044 .handle_interrupt = kszphy_handle_interrupt, 7045 .get_sset_count = kszphy_get_sset_count, 7046 .get_strings = kszphy_get_strings, 7047 .get_stats = kszphy_get_stats, 7048 .suspend = kszphy_suspend, 7049 .resume = ksz9131_resume, 7050 .cable_test_start = ksz9x31_cable_test_start, 7051 .cable_test_get_status = ksz9x31_cable_test_get_status, 7052 .get_features = ksz9477_get_features, 7053 .set_loopback = ksz9131_set_loopback, 7054 }, { 7055 PHY_ID_MATCH_MODEL(PHY_ID_KSZ8873MLL), 7056 .name = "Micrel KSZ8873MLL Switch", 7057 /* PHY_BASIC_FEATURES */ 7058 .config_init = kszphy_config_init, 7059 .config_aneg = ksz8873mll_config_aneg, 7060 .read_status = ksz8873mll_read_status, 7061 .suspend = genphy_suspend, 7062 .resume = genphy_resume, 7063 }, { 7064 PHY_ID_MATCH_MODEL(PHY_ID_KSZ886X), 7065 .name = "Micrel KSZ8851 Ethernet MAC or KSZ886X Switch", 7066 .driver_data = &ksz886x_type, 7067 /* PHY_BASIC_FEATURES */ 7068 .flags = PHY_POLL_CABLE_TEST, 7069 .config_init = kszphy_config_init, 7070 .config_aneg = ksz886x_config_aneg, 7071 .read_status = ksz886x_read_status, 7072 .suspend = genphy_suspend, 7073 .resume = genphy_resume, 7074 .cable_test_start = ksz886x_cable_test_start, 7075 .cable_test_get_status = ksz886x_cable_test_get_status, 7076 }, { 7077 .name = "Micrel KSZ87XX Switch", 7078 /* PHY_BASIC_FEATURES */ 7079 .config_init = kszphy_config_init, 7080 .match_phy_device = ksz8795_match_phy_device, 7081 .get_tunable = ksz8795_get_tunable, 7082 .set_tunable = ksz8795_set_tunable, 7083 .suspend = genphy_suspend, 7084 .resume = genphy_resume, 7085 }, { 7086 PHY_ID_MATCH_MODEL(PHY_ID_KSZ9477), 7087 .name = "Microchip KSZ9477", 7088 .probe = kszphy_probe, 7089 /* PHY_GBIT_FEATURES */ 7090 .config_init = ksz9477_config_init, 7091 .config_intr = kszphy_config_intr, 7092 .config_aneg = ksz9477_config_aneg, 7093 .read_status = ksz9477_read_status, 7094 .handle_interrupt = kszphy_handle_interrupt, 7095 .suspend = genphy_suspend, 7096 .resume = ksz9477_resume, 7097 .get_phy_stats = kszphy_get_phy_stats, 7098 .update_stats = kszphy_update_stats, 7099 .cable_test_start = ksz9x31_cable_test_start, 7100 .cable_test_get_status = ksz9x31_cable_test_get_status, 7101 .get_sqi = kszphy_get_sqi, 7102 .get_sqi_max = kszphy_get_sqi_max, 7103 .get_mse_capability = kszphy_get_mse_capability, 7104 .get_mse_snapshot = kszphy_get_mse_snapshot, 7105 } }; 7106 7107 module_phy_driver(ksphy_driver); 7108 7109 MODULE_DESCRIPTION("Micrel PHY driver"); 7110 MODULE_AUTHOR("David J. Choi"); 7111 MODULE_LICENSE("GPL"); 7112 7113 static const struct mdio_device_id __maybe_unused micrel_tbl[] = { 7114 { PHY_ID_KSZ9021, 0x000ffffe }, 7115 { PHY_ID_MATCH_MODEL(PHY_ID_KSZ9031) }, 7116 { PHY_ID_MATCH_MODEL(PHY_ID_KSZ9131) }, 7117 { PHY_ID_KSZ8001, 0x00fffffc }, 7118 { PHY_ID_MATCH_MODEL(PHY_ID_KS8737) }, 7119 { PHY_ID_KSZ8021, 0x00ffffff }, 7120 { PHY_ID_KSZ8031, 0x00ffffff }, 7121 { PHY_ID_MATCH_MODEL(PHY_ID_KSZ8041) }, 7122 { PHY_ID_MATCH_MODEL(PHY_ID_KSZ8041RNLI) }, 7123 { PHY_ID_MATCH_MODEL(PHY_ID_KSZ8051) }, 7124 { PHY_ID_MATCH_MODEL(PHY_ID_KSZ8061) }, 7125 { PHY_ID_MATCH_MODEL(PHY_ID_KSZ8081) }, 7126 { PHY_ID_MATCH_MODEL(PHY_ID_KSZ8873MLL) }, 7127 { PHY_ID_MATCH_MODEL(PHY_ID_KSZ886X) }, 7128 { PHY_ID_MATCH_MODEL(PHY_ID_KSZ9477) }, 7129 { PHY_ID_MATCH_MODEL(PHY_ID_LAN8814) }, 7130 { PHY_ID_MATCH_MODEL(PHY_ID_LAN8804) }, 7131 { PHY_ID_MATCH_MODEL(PHY_ID_LAN8841) }, 7132 { PHY_ID_MATCH_MODEL(PHY_ID_LAN8842) }, 7133 { PHY_ID_MATCH_MODEL(PHY_ID_LAN9645X) }, 7134 { } 7135 }; 7136 7137 MODULE_DEVICE_TABLE(mdio, micrel_tbl); 7138