1a2443fd1SAndrew Lunn // SPDX-License-Identifier: GPL-2.0+ 2d0507009SDavid J. Choi /* 3d0507009SDavid J. Choi * drivers/net/phy/micrel.c 4d0507009SDavid J. Choi * 5d0507009SDavid J. Choi * Driver for Micrel PHYs 6d0507009SDavid J. Choi * 7d0507009SDavid J. Choi * Author: David J. Choi 8d0507009SDavid J. Choi * 97ab59dc1SDavid J. Choi * Copyright (c) 2010-2013 Micrel, Inc. 10ee0dc2fbSJohan Hovold * Copyright (c) 2014 Johan Hovold <johan@kernel.org> 11d0507009SDavid J. Choi * 127ab59dc1SDavid J. Choi * Support : Micrel Phys: 13bff5b4b3SYuiko Oshino * Giga phys: ksz9021, ksz9031, ksz9131 147ab59dc1SDavid J. Choi * 100/10 Phys : ksz8001, ksz8721, ksz8737, ksz8041 157ab59dc1SDavid J. Choi * ksz8021, ksz8031, ksz8051, 167ab59dc1SDavid J. Choi * ksz8081, ksz8091, 177ab59dc1SDavid J. Choi * ksz8061, 187ab59dc1SDavid J. Choi * Switch : ksz8873, ksz886x 19fc3973a1SWoojung Huh * ksz9477 20d0507009SDavid J. Choi */ 21d0507009SDavid J. Choi 22bcf3440cSOleksij Rempel #include <linux/bitfield.h> 2349011e0cSOleksij Rempel #include <linux/ethtool_netlink.h> 24d0507009SDavid J. Choi #include <linux/kernel.h> 25d0507009SDavid J. Choi #include <linux/module.h> 26d0507009SDavid J. Choi #include <linux/phy.h> 27d606ef3fSBaruch Siach #include <linux/micrel_phy.h> 28954c3967SSean Cross #include <linux/of.h> 291fadee0cSSascha Hauer #include <linux/clk.h> 306110dff7SOleksij Rempel #include <linux/delay.h> 31ece19502SDivya Koppera #include <linux/ptp_clock_kernel.h> 32ece19502SDivya Koppera #include <linux/ptp_clock.h> 33ece19502SDivya Koppera #include <linux/ptp_classify.h> 34ece19502SDivya Koppera #include <linux/net_tstamp.h> 35738871b0SMichael Walle #include <linux/gpio/consumer.h> 36d0507009SDavid J. Choi 37212ea99aSMarek Vasut /* Operation Mode Strap Override */ 38212ea99aSMarek Vasut #define MII_KSZPHY_OMSO 0x16 397a1d8390SAntoine Tenart #define KSZPHY_OMSO_FACTORY_TEST BIT(15) 4000aee095SJohan Hovold #define KSZPHY_OMSO_B_CAST_OFF BIT(9) 412b0ba96cSSylvain Rochet #define KSZPHY_OMSO_NAND_TREE_ON BIT(5) 4200aee095SJohan Hovold #define KSZPHY_OMSO_RMII_OVERRIDE BIT(1) 4300aee095SJohan Hovold #define KSZPHY_OMSO_MII_OVERRIDE BIT(0) 44212ea99aSMarek Vasut 4551f932c4SChoi, David /* general Interrupt control/status reg in vendor specific block. */ 4651f932c4SChoi, David #define MII_KSZPHY_INTCS 0x1B 4700aee095SJohan Hovold #define KSZPHY_INTCS_JABBER BIT(15) 4800aee095SJohan Hovold #define KSZPHY_INTCS_RECEIVE_ERR BIT(14) 4900aee095SJohan Hovold #define KSZPHY_INTCS_PAGE_RECEIVE BIT(13) 5000aee095SJohan Hovold #define KSZPHY_INTCS_PARELLEL BIT(12) 5100aee095SJohan Hovold #define KSZPHY_INTCS_LINK_PARTNER_ACK BIT(11) 5200aee095SJohan Hovold #define KSZPHY_INTCS_LINK_DOWN BIT(10) 5300aee095SJohan Hovold #define KSZPHY_INTCS_REMOTE_FAULT BIT(9) 5400aee095SJohan Hovold #define KSZPHY_INTCS_LINK_UP BIT(8) 5551f932c4SChoi, David #define KSZPHY_INTCS_ALL (KSZPHY_INTCS_LINK_UP |\ 5651f932c4SChoi, David KSZPHY_INTCS_LINK_DOWN) 5759ca4e58SIoana Ciornei #define KSZPHY_INTCS_LINK_DOWN_STATUS BIT(2) 5859ca4e58SIoana Ciornei #define KSZPHY_INTCS_LINK_UP_STATUS BIT(0) 5959ca4e58SIoana Ciornei #define KSZPHY_INTCS_STATUS (KSZPHY_INTCS_LINK_DOWN_STATUS |\ 6059ca4e58SIoana Ciornei KSZPHY_INTCS_LINK_UP_STATUS) 6151f932c4SChoi, David 6249011e0cSOleksij Rempel /* LinkMD Control/Status */ 6349011e0cSOleksij Rempel #define KSZ8081_LMD 0x1d 6449011e0cSOleksij Rempel #define KSZ8081_LMD_ENABLE_TEST BIT(15) 6549011e0cSOleksij Rempel #define KSZ8081_LMD_STAT_NORMAL 0 6649011e0cSOleksij Rempel #define KSZ8081_LMD_STAT_OPEN 1 6749011e0cSOleksij Rempel #define KSZ8081_LMD_STAT_SHORT 2 6849011e0cSOleksij Rempel #define KSZ8081_LMD_STAT_FAIL 3 6949011e0cSOleksij Rempel #define KSZ8081_LMD_STAT_MASK GENMASK(14, 13) 7049011e0cSOleksij Rempel /* Short cable (<10 meter) has been detected by LinkMD */ 7149011e0cSOleksij Rempel #define KSZ8081_LMD_SHORT_INDICATOR BIT(12) 7249011e0cSOleksij Rempel #define KSZ8081_LMD_DELTA_TIME_MASK GENMASK(8, 0) 7349011e0cSOleksij Rempel 7458389c00SMarek Vasut #define KSZ9x31_LMD 0x12 7558389c00SMarek Vasut #define KSZ9x31_LMD_VCT_EN BIT(15) 7658389c00SMarek Vasut #define KSZ9x31_LMD_VCT_DIS_TX BIT(14) 7758389c00SMarek Vasut #define KSZ9x31_LMD_VCT_PAIR(n) (((n) & 0x3) << 12) 7858389c00SMarek Vasut #define KSZ9x31_LMD_VCT_SEL_RESULT 0 7958389c00SMarek Vasut #define KSZ9x31_LMD_VCT_SEL_THRES_HI BIT(10) 8058389c00SMarek Vasut #define KSZ9x31_LMD_VCT_SEL_THRES_LO BIT(11) 8158389c00SMarek Vasut #define KSZ9x31_LMD_VCT_SEL_MASK GENMASK(11, 10) 8258389c00SMarek Vasut #define KSZ9x31_LMD_VCT_ST_NORMAL 0 8358389c00SMarek Vasut #define KSZ9x31_LMD_VCT_ST_OPEN 1 8458389c00SMarek Vasut #define KSZ9x31_LMD_VCT_ST_SHORT 2 8558389c00SMarek Vasut #define KSZ9x31_LMD_VCT_ST_FAIL 3 8658389c00SMarek Vasut #define KSZ9x31_LMD_VCT_ST_MASK GENMASK(9, 8) 8758389c00SMarek Vasut #define KSZ9x31_LMD_VCT_DATA_REFLECTED_INVALID BIT(7) 8858389c00SMarek Vasut #define KSZ9x31_LMD_VCT_DATA_SIG_WAIT_TOO_LONG BIT(6) 8958389c00SMarek Vasut #define KSZ9x31_LMD_VCT_DATA_MASK100 BIT(5) 9058389c00SMarek Vasut #define KSZ9x31_LMD_VCT_DATA_NLP_FLP BIT(4) 9158389c00SMarek Vasut #define KSZ9x31_LMD_VCT_DATA_LO_PULSE_MASK GENMASK(3, 2) 9258389c00SMarek Vasut #define KSZ9x31_LMD_VCT_DATA_HI_PULSE_MASK GENMASK(1, 0) 9358389c00SMarek Vasut #define KSZ9x31_LMD_VCT_DATA_MASK GENMASK(7, 0) 9458389c00SMarek Vasut 9521b688daSDivya Koppera #define KSZPHY_WIRE_PAIR_MASK 0x3 9621b688daSDivya Koppera 9721b688daSDivya Koppera #define LAN8814_CABLE_DIAG 0x12 9821b688daSDivya Koppera #define LAN8814_CABLE_DIAG_STAT_MASK GENMASK(9, 8) 9921b688daSDivya Koppera #define LAN8814_CABLE_DIAG_VCT_DATA_MASK GENMASK(7, 0) 10021b688daSDivya Koppera #define LAN8814_PAIR_BIT_SHIFT 12 10121b688daSDivya Koppera 10221b688daSDivya Koppera #define LAN8814_WIRE_PAIR_MASK 0xF 10321b688daSDivya Koppera 104b3ec7248SDivya Koppera /* Lan8814 general Interrupt control/status reg in GPHY specific block. */ 105b3ec7248SDivya Koppera #define LAN8814_INTC 0x18 106b3ec7248SDivya Koppera #define LAN8814_INTS 0x1B 107b3ec7248SDivya Koppera 108b3ec7248SDivya Koppera #define LAN8814_INT_LINK_DOWN BIT(2) 109b3ec7248SDivya Koppera #define LAN8814_INT_LINK_UP BIT(0) 110b3ec7248SDivya Koppera #define LAN8814_INT_LINK (LAN8814_INT_LINK_UP |\ 111b3ec7248SDivya Koppera LAN8814_INT_LINK_DOWN) 112b3ec7248SDivya Koppera 113b3ec7248SDivya Koppera #define LAN8814_INTR_CTRL_REG 0x34 114b3ec7248SDivya Koppera #define LAN8814_INTR_CTRL_REG_POLARITY BIT(1) 115b3ec7248SDivya Koppera #define LAN8814_INTR_CTRL_REG_INTR_ENABLE BIT(0) 116b3ec7248SDivya Koppera 117ece19502SDivya Koppera /* Represents 1ppm adjustment in 2^32 format with 118ece19502SDivya Koppera * each nsec contains 4 clock cycles. 119ece19502SDivya Koppera * The value is calculated as following: (1/1000000)/((2^-32)/4) 120ece19502SDivya Koppera */ 121ece19502SDivya Koppera #define LAN8814_1PPM_FORMAT 17179 122ece19502SDivya Koppera 123ece19502SDivya Koppera #define PTP_RX_MOD 0x024F 124ece19502SDivya Koppera #define PTP_RX_MOD_BAD_UDPV4_CHKSUM_FORCE_FCS_DIS_ BIT(3) 125ece19502SDivya Koppera #define PTP_RX_TIMESTAMP_EN 0x024D 126ece19502SDivya Koppera #define PTP_TX_TIMESTAMP_EN 0x028D 127ece19502SDivya Koppera 128ece19502SDivya Koppera #define PTP_TIMESTAMP_EN_SYNC_ BIT(0) 129ece19502SDivya Koppera #define PTP_TIMESTAMP_EN_DREQ_ BIT(1) 130ece19502SDivya Koppera #define PTP_TIMESTAMP_EN_PDREQ_ BIT(2) 131ece19502SDivya Koppera #define PTP_TIMESTAMP_EN_PDRES_ BIT(3) 132ece19502SDivya Koppera 133ece19502SDivya Koppera #define PTP_TX_PARSE_L2_ADDR_EN 0x0284 134ece19502SDivya Koppera #define PTP_RX_PARSE_L2_ADDR_EN 0x0244 135ece19502SDivya Koppera 136ece19502SDivya Koppera #define PTP_TX_PARSE_IP_ADDR_EN 0x0285 137ece19502SDivya Koppera #define PTP_RX_PARSE_IP_ADDR_EN 0x0245 138ece19502SDivya Koppera #define LTC_HARD_RESET 0x023F 139ece19502SDivya Koppera #define LTC_HARD_RESET_ BIT(0) 140ece19502SDivya Koppera 141ece19502SDivya Koppera #define TSU_HARD_RESET 0x02C1 142ece19502SDivya Koppera #define TSU_HARD_RESET_ BIT(0) 143ece19502SDivya Koppera 144ece19502SDivya Koppera #define PTP_CMD_CTL 0x0200 145ece19502SDivya Koppera #define PTP_CMD_CTL_PTP_DISABLE_ BIT(0) 146ece19502SDivya Koppera #define PTP_CMD_CTL_PTP_ENABLE_ BIT(1) 147ece19502SDivya Koppera #define PTP_CMD_CTL_PTP_CLOCK_READ_ BIT(3) 148ece19502SDivya Koppera #define PTP_CMD_CTL_PTP_CLOCK_LOAD_ BIT(4) 149ece19502SDivya Koppera #define PTP_CMD_CTL_PTP_LTC_STEP_SEC_ BIT(5) 150ece19502SDivya Koppera #define PTP_CMD_CTL_PTP_LTC_STEP_NSEC_ BIT(6) 151ece19502SDivya Koppera 152ece19502SDivya Koppera #define PTP_CLOCK_SET_SEC_MID 0x0206 153ece19502SDivya Koppera #define PTP_CLOCK_SET_SEC_LO 0x0207 154ece19502SDivya Koppera #define PTP_CLOCK_SET_NS_HI 0x0208 155ece19502SDivya Koppera #define PTP_CLOCK_SET_NS_LO 0x0209 156ece19502SDivya Koppera 157ece19502SDivya Koppera #define PTP_CLOCK_READ_SEC_MID 0x022A 158ece19502SDivya Koppera #define PTP_CLOCK_READ_SEC_LO 0x022B 159ece19502SDivya Koppera #define PTP_CLOCK_READ_NS_HI 0x022C 160ece19502SDivya Koppera #define PTP_CLOCK_READ_NS_LO 0x022D 161ece19502SDivya Koppera 162ece19502SDivya Koppera #define PTP_OPERATING_MODE 0x0241 163ece19502SDivya Koppera #define PTP_OPERATING_MODE_STANDALONE_ BIT(0) 164ece19502SDivya Koppera 165ece19502SDivya Koppera #define PTP_TX_MOD 0x028F 166ece19502SDivya Koppera #define PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_ BIT(12) 167ece19502SDivya Koppera #define PTP_TX_MOD_BAD_UDPV4_CHKSUM_FORCE_FCS_DIS_ BIT(3) 168ece19502SDivya Koppera 169ece19502SDivya Koppera #define PTP_RX_PARSE_CONFIG 0x0242 170ece19502SDivya Koppera #define PTP_RX_PARSE_CONFIG_LAYER2_EN_ BIT(0) 171ece19502SDivya Koppera #define PTP_RX_PARSE_CONFIG_IPV4_EN_ BIT(1) 172ece19502SDivya Koppera #define PTP_RX_PARSE_CONFIG_IPV6_EN_ BIT(2) 173ece19502SDivya Koppera 174ece19502SDivya Koppera #define PTP_TX_PARSE_CONFIG 0x0282 175ece19502SDivya Koppera #define PTP_TX_PARSE_CONFIG_LAYER2_EN_ BIT(0) 176ece19502SDivya Koppera #define PTP_TX_PARSE_CONFIG_IPV4_EN_ BIT(1) 177ece19502SDivya Koppera #define PTP_TX_PARSE_CONFIG_IPV6_EN_ BIT(2) 178ece19502SDivya Koppera 179ece19502SDivya Koppera #define PTP_CLOCK_RATE_ADJ_HI 0x020C 180ece19502SDivya Koppera #define PTP_CLOCK_RATE_ADJ_LO 0x020D 181ece19502SDivya Koppera #define PTP_CLOCK_RATE_ADJ_DIR_ BIT(15) 182ece19502SDivya Koppera 183ece19502SDivya Koppera #define PTP_LTC_STEP_ADJ_HI 0x0212 184ece19502SDivya Koppera #define PTP_LTC_STEP_ADJ_LO 0x0213 185ece19502SDivya Koppera #define PTP_LTC_STEP_ADJ_DIR_ BIT(15) 186ece19502SDivya Koppera 187ece19502SDivya Koppera #define LAN8814_INTR_STS_REG 0x0033 188ece19502SDivya Koppera #define LAN8814_INTR_STS_REG_1588_TSU0_ BIT(0) 189ece19502SDivya Koppera #define LAN8814_INTR_STS_REG_1588_TSU1_ BIT(1) 190ece19502SDivya Koppera #define LAN8814_INTR_STS_REG_1588_TSU2_ BIT(2) 191ece19502SDivya Koppera #define LAN8814_INTR_STS_REG_1588_TSU3_ BIT(3) 192ece19502SDivya Koppera 193ece19502SDivya Koppera #define PTP_CAP_INFO 0x022A 194ece19502SDivya Koppera #define PTP_CAP_INFO_TX_TS_CNT_GET_(reg_val) (((reg_val) & 0x0f00) >> 8) 195ece19502SDivya Koppera #define PTP_CAP_INFO_RX_TS_CNT_GET_(reg_val) ((reg_val) & 0x000f) 196ece19502SDivya Koppera 197ece19502SDivya Koppera #define PTP_TX_EGRESS_SEC_HI 0x0296 198ece19502SDivya Koppera #define PTP_TX_EGRESS_SEC_LO 0x0297 199ece19502SDivya Koppera #define PTP_TX_EGRESS_NS_HI 0x0294 200ece19502SDivya Koppera #define PTP_TX_EGRESS_NS_LO 0x0295 201ece19502SDivya Koppera #define PTP_TX_MSG_HEADER2 0x0299 202ece19502SDivya Koppera 203ece19502SDivya Koppera #define PTP_RX_INGRESS_SEC_HI 0x0256 204ece19502SDivya Koppera #define PTP_RX_INGRESS_SEC_LO 0x0257 205ece19502SDivya Koppera #define PTP_RX_INGRESS_NS_HI 0x0254 206ece19502SDivya Koppera #define PTP_RX_INGRESS_NS_LO 0x0255 207ece19502SDivya Koppera #define PTP_RX_MSG_HEADER2 0x0259 208ece19502SDivya Koppera 209ece19502SDivya Koppera #define PTP_TSU_INT_EN 0x0200 210ece19502SDivya Koppera #define PTP_TSU_INT_EN_PTP_TX_TS_OVRFL_EN_ BIT(3) 211ece19502SDivya Koppera #define PTP_TSU_INT_EN_PTP_TX_TS_EN_ BIT(2) 212ece19502SDivya Koppera #define PTP_TSU_INT_EN_PTP_RX_TS_OVRFL_EN_ BIT(1) 213ece19502SDivya Koppera #define PTP_TSU_INT_EN_PTP_RX_TS_EN_ BIT(0) 214ece19502SDivya Koppera 215ece19502SDivya Koppera #define PTP_TSU_INT_STS 0x0201 216ece19502SDivya Koppera #define PTP_TSU_INT_STS_PTP_TX_TS_OVRFL_INT_ BIT(3) 217ece19502SDivya Koppera #define PTP_TSU_INT_STS_PTP_TX_TS_EN_ BIT(2) 218ece19502SDivya Koppera #define PTP_TSU_INT_STS_PTP_RX_TS_OVRFL_INT_ BIT(1) 219ece19502SDivya Koppera #define PTP_TSU_INT_STS_PTP_RX_TS_EN_ BIT(0) 220ece19502SDivya Koppera 221a516b7f7SDivya Koppera #define LAN8814_LED_CTRL_1 0x0 222a516b7f7SDivya Koppera #define LAN8814_LED_CTRL_1_KSZ9031_LED_MODE_ BIT(6) 223a516b7f7SDivya Koppera 2245a16778eSJohan Hovold /* PHY Control 1 */ 2255a16778eSJohan Hovold #define MII_KSZPHY_CTRL_1 0x1e 226f873f112SOleksij Rempel #define KSZ8081_CTRL1_MDIX_STAT BIT(4) 2275a16778eSJohan Hovold 2285a16778eSJohan Hovold /* PHY Control 2 / PHY Control (if no PHY Control 1) */ 2295a16778eSJohan Hovold #define MII_KSZPHY_CTRL_2 0x1f 2305a16778eSJohan Hovold #define MII_KSZPHY_CTRL MII_KSZPHY_CTRL_2 23151f932c4SChoi, David /* bitmap of PHY register to set interrupt mode */ 232f873f112SOleksij Rempel #define KSZ8081_CTRL2_HP_MDIX BIT(15) 233f873f112SOleksij Rempel #define KSZ8081_CTRL2_MDI_MDI_X_SELECT BIT(14) 234f873f112SOleksij Rempel #define KSZ8081_CTRL2_DISABLE_AUTO_MDIX BIT(13) 235f873f112SOleksij Rempel #define KSZ8081_CTRL2_FORCE_LINK BIT(11) 236f873f112SOleksij Rempel #define KSZ8081_CTRL2_POWER_SAVING BIT(10) 23700aee095SJohan Hovold #define KSZPHY_CTRL_INT_ACTIVE_HIGH BIT(9) 23863f44b2bSJohan Hovold #define KSZPHY_RMII_REF_CLK_SEL BIT(7) 23951f932c4SChoi, David 240954c3967SSean Cross /* Write/read to/from extended registers */ 241954c3967SSean Cross #define MII_KSZPHY_EXTREG 0x0b 242954c3967SSean Cross #define KSZPHY_EXTREG_WRITE 0x8000 243954c3967SSean Cross 244954c3967SSean Cross #define MII_KSZPHY_EXTREG_WRITE 0x0c 245954c3967SSean Cross #define MII_KSZPHY_EXTREG_READ 0x0d 246954c3967SSean Cross 247954c3967SSean Cross /* Extended registers */ 248954c3967SSean Cross #define MII_KSZPHY_CLK_CONTROL_PAD_SKEW 0x104 249954c3967SSean Cross #define MII_KSZPHY_RX_DATA_PAD_SKEW 0x105 250954c3967SSean Cross #define MII_KSZPHY_TX_DATA_PAD_SKEW 0x106 251954c3967SSean Cross 252954c3967SSean Cross #define PS_TO_REG 200 253ece19502SDivya Koppera #define FIFO_SIZE 8 254954c3967SSean Cross 2552b2427d0SAndrew Lunn struct kszphy_hw_stat { 2562b2427d0SAndrew Lunn const char *string; 2572b2427d0SAndrew Lunn u8 reg; 2582b2427d0SAndrew Lunn u8 bits; 2592b2427d0SAndrew Lunn }; 2602b2427d0SAndrew Lunn 2612b2427d0SAndrew Lunn static struct kszphy_hw_stat kszphy_hw_stats[] = { 2622b2427d0SAndrew Lunn { "phy_receive_errors", 21, 16}, 2632b2427d0SAndrew Lunn { "phy_idle_errors", 10, 8 }, 2642b2427d0SAndrew Lunn }; 2652b2427d0SAndrew Lunn 266e6a423a8SJohan Hovold struct kszphy_type { 267e6a423a8SJohan Hovold u32 led_mode_reg; 268c6f9575cSJohan Hovold u16 interrupt_level_mask; 26921b688daSDivya Koppera u16 cable_diag_reg; 27021b688daSDivya Koppera unsigned long pair_mask; 271a8f1a19dSHoratiu Vultur u16 disable_dll_tx_bit; 272a8f1a19dSHoratiu Vultur u16 disable_dll_rx_bit; 273a8f1a19dSHoratiu Vultur u16 disable_dll_mask; 2740f95903eSJohan Hovold bool has_broadcast_disable; 2752b0ba96cSSylvain Rochet bool has_nand_tree_disable; 27663f44b2bSJohan Hovold bool has_rmii_ref_clk_sel; 277e6a423a8SJohan Hovold }; 278e6a423a8SJohan Hovold 279ece19502SDivya Koppera /* Shared structure between the PHYs of the same package. */ 280ece19502SDivya Koppera struct lan8814_shared_priv { 281ece19502SDivya Koppera struct phy_device *phydev; 282ece19502SDivya Koppera struct ptp_clock *ptp_clock; 283ece19502SDivya Koppera struct ptp_clock_info ptp_clock_info; 284ece19502SDivya Koppera 285ece19502SDivya Koppera /* Reference counter to how many ports in the package are enabling the 286ece19502SDivya Koppera * timestamping 287ece19502SDivya Koppera */ 288ece19502SDivya Koppera u8 ref; 289ece19502SDivya Koppera 290ece19502SDivya Koppera /* Lock for ptp_clock and ref */ 291ece19502SDivya Koppera struct mutex shared_lock; 292ece19502SDivya Koppera }; 293ece19502SDivya Koppera 294ece19502SDivya Koppera struct lan8814_ptp_rx_ts { 295ece19502SDivya Koppera struct list_head list; 296ece19502SDivya Koppera u32 seconds; 297ece19502SDivya Koppera u32 nsec; 298ece19502SDivya Koppera u16 seq_id; 299ece19502SDivya Koppera }; 300ece19502SDivya Koppera 301ece19502SDivya Koppera struct kszphy_ptp_priv { 302ece19502SDivya Koppera struct mii_timestamper mii_ts; 303ece19502SDivya Koppera struct phy_device *phydev; 304ece19502SDivya Koppera 305ece19502SDivya Koppera struct sk_buff_head tx_queue; 306ece19502SDivya Koppera struct sk_buff_head rx_queue; 307ece19502SDivya Koppera 308ece19502SDivya Koppera struct list_head rx_ts_list; 309ece19502SDivya Koppera /* Lock for Rx ts fifo */ 310ece19502SDivya Koppera spinlock_t rx_ts_lock; 311ece19502SDivya Koppera 312ece19502SDivya Koppera int hwts_tx_type; 313ece19502SDivya Koppera enum hwtstamp_rx_filters rx_filter; 314ece19502SDivya Koppera int layer; 315ece19502SDivya Koppera int version; 316cafc3662SHoratiu Vultur 317cafc3662SHoratiu Vultur struct ptp_clock *ptp_clock; 318cafc3662SHoratiu Vultur struct ptp_clock_info ptp_clock_info; 319cafc3662SHoratiu Vultur /* Lock for ptp_clock */ 320cafc3662SHoratiu Vultur struct mutex ptp_lock; 321ece19502SDivya Koppera }; 322ece19502SDivya Koppera 323e6a423a8SJohan Hovold struct kszphy_priv { 324ece19502SDivya Koppera struct kszphy_ptp_priv ptp_priv; 325e6a423a8SJohan Hovold const struct kszphy_type *type; 326e7a792e9SJohan Hovold int led_mode; 32758389c00SMarek Vasut u16 vct_ctrl1000; 32863f44b2bSJohan Hovold bool rmii_ref_clk_sel; 32963f44b2bSJohan Hovold bool rmii_ref_clk_sel_val; 3302b2427d0SAndrew Lunn u64 stats[ARRAY_SIZE(kszphy_hw_stats)]; 331e6a423a8SJohan Hovold }; 332e6a423a8SJohan Hovold 333a516b7f7SDivya Koppera static const struct kszphy_type lan8814_type = { 334a516b7f7SDivya Koppera .led_mode_reg = ~LAN8814_LED_CTRL_1, 33521b688daSDivya Koppera .cable_diag_reg = LAN8814_CABLE_DIAG, 33621b688daSDivya Koppera .pair_mask = LAN8814_WIRE_PAIR_MASK, 33721b688daSDivya Koppera }; 33821b688daSDivya Koppera 33921b688daSDivya Koppera static const struct kszphy_type ksz886x_type = { 34021b688daSDivya Koppera .cable_diag_reg = KSZ8081_LMD, 34121b688daSDivya Koppera .pair_mask = KSZPHY_WIRE_PAIR_MASK, 342a516b7f7SDivya Koppera }; 343a516b7f7SDivya Koppera 344e6a423a8SJohan Hovold static const struct kszphy_type ksz8021_type = { 345e6a423a8SJohan Hovold .led_mode_reg = MII_KSZPHY_CTRL_2, 346d0e1df9cSJohan Hovold .has_broadcast_disable = true, 3472b0ba96cSSylvain Rochet .has_nand_tree_disable = true, 34863f44b2bSJohan Hovold .has_rmii_ref_clk_sel = true, 349e6a423a8SJohan Hovold }; 350e6a423a8SJohan Hovold 351e6a423a8SJohan Hovold static const struct kszphy_type ksz8041_type = { 352e6a423a8SJohan Hovold .led_mode_reg = MII_KSZPHY_CTRL_1, 353e6a423a8SJohan Hovold }; 354e6a423a8SJohan Hovold 355e6a423a8SJohan Hovold static const struct kszphy_type ksz8051_type = { 356e6a423a8SJohan Hovold .led_mode_reg = MII_KSZPHY_CTRL_2, 3572b0ba96cSSylvain Rochet .has_nand_tree_disable = true, 358e6a423a8SJohan Hovold }; 359e6a423a8SJohan Hovold 360e6a423a8SJohan Hovold static const struct kszphy_type ksz8081_type = { 361e6a423a8SJohan Hovold .led_mode_reg = MII_KSZPHY_CTRL_2, 3620f95903eSJohan Hovold .has_broadcast_disable = true, 3632b0ba96cSSylvain Rochet .has_nand_tree_disable = true, 36486dc1342SJohan Hovold .has_rmii_ref_clk_sel = true, 365e6a423a8SJohan Hovold }; 366e6a423a8SJohan Hovold 367c6f9575cSJohan Hovold static const struct kszphy_type ks8737_type = { 368c6f9575cSJohan Hovold .interrupt_level_mask = BIT(14), 369c6f9575cSJohan Hovold }; 370c6f9575cSJohan Hovold 371c6f9575cSJohan Hovold static const struct kszphy_type ksz9021_type = { 372c6f9575cSJohan Hovold .interrupt_level_mask = BIT(14), 373c6f9575cSJohan Hovold }; 374c6f9575cSJohan Hovold 375a8f1a19dSHoratiu Vultur static const struct kszphy_type ksz9131_type = { 376a8f1a19dSHoratiu Vultur .interrupt_level_mask = BIT(14), 377a8f1a19dSHoratiu Vultur .disable_dll_tx_bit = BIT(12), 378a8f1a19dSHoratiu Vultur .disable_dll_rx_bit = BIT(12), 379a8f1a19dSHoratiu Vultur .disable_dll_mask = BIT_MASK(12), 380a8f1a19dSHoratiu Vultur }; 381a8f1a19dSHoratiu Vultur 382a8f1a19dSHoratiu Vultur static const struct kszphy_type lan8841_type = { 383a8f1a19dSHoratiu Vultur .disable_dll_tx_bit = BIT(14), 384a8f1a19dSHoratiu Vultur .disable_dll_rx_bit = BIT(14), 385a8f1a19dSHoratiu Vultur .disable_dll_mask = BIT_MASK(14), 386a136391aSHoratiu Vultur .cable_diag_reg = LAN8814_CABLE_DIAG, 387a136391aSHoratiu Vultur .pair_mask = LAN8814_WIRE_PAIR_MASK, 388a8f1a19dSHoratiu Vultur }; 389a8f1a19dSHoratiu Vultur 390954c3967SSean Cross static int kszphy_extended_write(struct phy_device *phydev, 391954c3967SSean Cross u32 regnum, u16 val) 392954c3967SSean Cross { 393954c3967SSean Cross phy_write(phydev, MII_KSZPHY_EXTREG, KSZPHY_EXTREG_WRITE | regnum); 394954c3967SSean Cross return phy_write(phydev, MII_KSZPHY_EXTREG_WRITE, val); 395954c3967SSean Cross } 396954c3967SSean Cross 397954c3967SSean Cross static int kszphy_extended_read(struct phy_device *phydev, 398954c3967SSean Cross u32 regnum) 399954c3967SSean Cross { 400954c3967SSean Cross phy_write(phydev, MII_KSZPHY_EXTREG, regnum); 401954c3967SSean Cross return phy_read(phydev, MII_KSZPHY_EXTREG_READ); 402954c3967SSean Cross } 403954c3967SSean Cross 40451f932c4SChoi, David static int kszphy_ack_interrupt(struct phy_device *phydev) 40551f932c4SChoi, David { 40651f932c4SChoi, David /* bit[7..0] int status, which is a read and clear register. */ 40751f932c4SChoi, David int rc; 40851f932c4SChoi, David 40951f932c4SChoi, David rc = phy_read(phydev, MII_KSZPHY_INTCS); 41051f932c4SChoi, David 41151f932c4SChoi, David return (rc < 0) ? rc : 0; 41251f932c4SChoi, David } 41351f932c4SChoi, David 41451f932c4SChoi, David static int kszphy_config_intr(struct phy_device *phydev) 41551f932c4SChoi, David { 416c6f9575cSJohan Hovold const struct kszphy_type *type = phydev->drv->driver_data; 417c0c99d0cSIoana Ciornei int temp, err; 418c6f9575cSJohan Hovold u16 mask; 419c6f9575cSJohan Hovold 420c6f9575cSJohan Hovold if (type && type->interrupt_level_mask) 421c6f9575cSJohan Hovold mask = type->interrupt_level_mask; 422c6f9575cSJohan Hovold else 423c6f9575cSJohan Hovold mask = KSZPHY_CTRL_INT_ACTIVE_HIGH; 42451f932c4SChoi, David 42551f932c4SChoi, David /* set the interrupt pin active low */ 42651f932c4SChoi, David temp = phy_read(phydev, MII_KSZPHY_CTRL); 4275bb8fc0dSJohan Hovold if (temp < 0) 4285bb8fc0dSJohan Hovold return temp; 429c6f9575cSJohan Hovold temp &= ~mask; 43051f932c4SChoi, David phy_write(phydev, MII_KSZPHY_CTRL, temp); 43151f932c4SChoi, David 432c6f9575cSJohan Hovold /* enable / disable interrupts */ 433c0c99d0cSIoana Ciornei if (phydev->interrupts == PHY_INTERRUPT_ENABLED) { 434c0c99d0cSIoana Ciornei err = kszphy_ack_interrupt(phydev); 435c0c99d0cSIoana Ciornei if (err) 436c0c99d0cSIoana Ciornei return err; 43751f932c4SChoi, David 438c0c99d0cSIoana Ciornei temp = KSZPHY_INTCS_ALL; 439c0c99d0cSIoana Ciornei err = phy_write(phydev, MII_KSZPHY_INTCS, temp); 440c0c99d0cSIoana Ciornei } else { 441c0c99d0cSIoana Ciornei temp = 0; 442c0c99d0cSIoana Ciornei err = phy_write(phydev, MII_KSZPHY_INTCS, temp); 443c0c99d0cSIoana Ciornei if (err) 444c0c99d0cSIoana Ciornei return err; 445c0c99d0cSIoana Ciornei 446c0c99d0cSIoana Ciornei err = kszphy_ack_interrupt(phydev); 447c0c99d0cSIoana Ciornei } 448c0c99d0cSIoana Ciornei 449c0c99d0cSIoana Ciornei return err; 45051f932c4SChoi, David } 451d0507009SDavid J. Choi 45259ca4e58SIoana Ciornei static irqreturn_t kszphy_handle_interrupt(struct phy_device *phydev) 45359ca4e58SIoana Ciornei { 45459ca4e58SIoana Ciornei int irq_status; 45559ca4e58SIoana Ciornei 45659ca4e58SIoana Ciornei irq_status = phy_read(phydev, MII_KSZPHY_INTCS); 45759ca4e58SIoana Ciornei if (irq_status < 0) { 45859ca4e58SIoana Ciornei phy_error(phydev); 45959ca4e58SIoana Ciornei return IRQ_NONE; 46059ca4e58SIoana Ciornei } 46159ca4e58SIoana Ciornei 462fff4c746SOleksij Rempel if (!(irq_status & KSZPHY_INTCS_STATUS)) 46359ca4e58SIoana Ciornei return IRQ_NONE; 46459ca4e58SIoana Ciornei 46559ca4e58SIoana Ciornei phy_trigger_machine(phydev); 46659ca4e58SIoana Ciornei 46759ca4e58SIoana Ciornei return IRQ_HANDLED; 46859ca4e58SIoana Ciornei } 46959ca4e58SIoana Ciornei 47063f44b2bSJohan Hovold static int kszphy_rmii_clk_sel(struct phy_device *phydev, bool val) 47163f44b2bSJohan Hovold { 47263f44b2bSJohan Hovold int ctrl; 47363f44b2bSJohan Hovold 47463f44b2bSJohan Hovold ctrl = phy_read(phydev, MII_KSZPHY_CTRL); 47563f44b2bSJohan Hovold if (ctrl < 0) 47663f44b2bSJohan Hovold return ctrl; 47763f44b2bSJohan Hovold 47863f44b2bSJohan Hovold if (val) 47963f44b2bSJohan Hovold ctrl |= KSZPHY_RMII_REF_CLK_SEL; 48063f44b2bSJohan Hovold else 48163f44b2bSJohan Hovold ctrl &= ~KSZPHY_RMII_REF_CLK_SEL; 48263f44b2bSJohan Hovold 48363f44b2bSJohan Hovold return phy_write(phydev, MII_KSZPHY_CTRL, ctrl); 48463f44b2bSJohan Hovold } 48563f44b2bSJohan Hovold 486e7a792e9SJohan Hovold static int kszphy_setup_led(struct phy_device *phydev, u32 reg, int val) 48720d8435aSBen Dooks { 4885a16778eSJohan Hovold int rc, temp, shift; 4898620546cSJohan Hovold 4905a16778eSJohan Hovold switch (reg) { 4915a16778eSJohan Hovold case MII_KSZPHY_CTRL_1: 4925a16778eSJohan Hovold shift = 14; 4935a16778eSJohan Hovold break; 4945a16778eSJohan Hovold case MII_KSZPHY_CTRL_2: 4955a16778eSJohan Hovold shift = 4; 4965a16778eSJohan Hovold break; 4975a16778eSJohan Hovold default: 4985a16778eSJohan Hovold return -EINVAL; 4995a16778eSJohan Hovold } 5005a16778eSJohan Hovold 50120d8435aSBen Dooks temp = phy_read(phydev, reg); 502b7035860SJohan Hovold if (temp < 0) { 503b7035860SJohan Hovold rc = temp; 504b7035860SJohan Hovold goto out; 505b7035860SJohan Hovold } 50620d8435aSBen Dooks 50728bdc499SSergei Shtylyov temp &= ~(3 << shift); 50820d8435aSBen Dooks temp |= val << shift; 50920d8435aSBen Dooks rc = phy_write(phydev, reg, temp); 510b7035860SJohan Hovold out: 511b7035860SJohan Hovold if (rc < 0) 51272ba48beSAndrew Lunn phydev_err(phydev, "failed to set led mode\n"); 51320d8435aSBen Dooks 514b7035860SJohan Hovold return rc; 51520d8435aSBen Dooks } 51620d8435aSBen Dooks 517bde15129SJohan Hovold /* Disable PHY address 0 as the broadcast address, so that it can be used as a 518bde15129SJohan Hovold * unique (non-broadcast) address on a shared bus. 519bde15129SJohan Hovold */ 520bde15129SJohan Hovold static int kszphy_broadcast_disable(struct phy_device *phydev) 521bde15129SJohan Hovold { 522bde15129SJohan Hovold int ret; 523bde15129SJohan Hovold 524bde15129SJohan Hovold ret = phy_read(phydev, MII_KSZPHY_OMSO); 525bde15129SJohan Hovold if (ret < 0) 526bde15129SJohan Hovold goto out; 527bde15129SJohan Hovold 528bde15129SJohan Hovold ret = phy_write(phydev, MII_KSZPHY_OMSO, ret | KSZPHY_OMSO_B_CAST_OFF); 529bde15129SJohan Hovold out: 530bde15129SJohan Hovold if (ret) 53172ba48beSAndrew Lunn phydev_err(phydev, "failed to disable broadcast address\n"); 532bde15129SJohan Hovold 533bde15129SJohan Hovold return ret; 534bde15129SJohan Hovold } 535bde15129SJohan Hovold 5362b0ba96cSSylvain Rochet static int kszphy_nand_tree_disable(struct phy_device *phydev) 5372b0ba96cSSylvain Rochet { 5382b0ba96cSSylvain Rochet int ret; 5392b0ba96cSSylvain Rochet 5402b0ba96cSSylvain Rochet ret = phy_read(phydev, MII_KSZPHY_OMSO); 5412b0ba96cSSylvain Rochet if (ret < 0) 5422b0ba96cSSylvain Rochet goto out; 5432b0ba96cSSylvain Rochet 5442b0ba96cSSylvain Rochet if (!(ret & KSZPHY_OMSO_NAND_TREE_ON)) 5452b0ba96cSSylvain Rochet return 0; 5462b0ba96cSSylvain Rochet 5472b0ba96cSSylvain Rochet ret = phy_write(phydev, MII_KSZPHY_OMSO, 5482b0ba96cSSylvain Rochet ret & ~KSZPHY_OMSO_NAND_TREE_ON); 5492b0ba96cSSylvain Rochet out: 5502b0ba96cSSylvain Rochet if (ret) 55172ba48beSAndrew Lunn phydev_err(phydev, "failed to disable NAND tree mode\n"); 5522b0ba96cSSylvain Rochet 5532b0ba96cSSylvain Rochet return ret; 5542b0ba96cSSylvain Rochet } 5552b0ba96cSSylvain Rochet 55679e498a9SLeonard Crestez /* Some config bits need to be set again on resume, handle them here. */ 55779e498a9SLeonard Crestez static int kszphy_config_reset(struct phy_device *phydev) 55879e498a9SLeonard Crestez { 55979e498a9SLeonard Crestez struct kszphy_priv *priv = phydev->priv; 56079e498a9SLeonard Crestez int ret; 56179e498a9SLeonard Crestez 56279e498a9SLeonard Crestez if (priv->rmii_ref_clk_sel) { 56379e498a9SLeonard Crestez ret = kszphy_rmii_clk_sel(phydev, priv->rmii_ref_clk_sel_val); 56479e498a9SLeonard Crestez if (ret) { 56579e498a9SLeonard Crestez phydev_err(phydev, 56679e498a9SLeonard Crestez "failed to set rmii reference clock\n"); 56779e498a9SLeonard Crestez return ret; 56879e498a9SLeonard Crestez } 56979e498a9SLeonard Crestez } 57079e498a9SLeonard Crestez 571f2ef6f75SFabio Estevam if (priv->type && priv->led_mode >= 0) 57279e498a9SLeonard Crestez kszphy_setup_led(phydev, priv->type->led_mode_reg, priv->led_mode); 57379e498a9SLeonard Crestez 57479e498a9SLeonard Crestez return 0; 57579e498a9SLeonard Crestez } 57679e498a9SLeonard Crestez 577d0507009SDavid J. Choi static int kszphy_config_init(struct phy_device *phydev) 578d0507009SDavid J. Choi { 579e6a423a8SJohan Hovold struct kszphy_priv *priv = phydev->priv; 580e6a423a8SJohan Hovold const struct kszphy_type *type; 581d0507009SDavid J. Choi 582e6a423a8SJohan Hovold if (!priv) 583e6a423a8SJohan Hovold return 0; 584e6a423a8SJohan Hovold 585e6a423a8SJohan Hovold type = priv->type; 586e6a423a8SJohan Hovold 587f2ef6f75SFabio Estevam if (type && type->has_broadcast_disable) 5880f95903eSJohan Hovold kszphy_broadcast_disable(phydev); 5890f95903eSJohan Hovold 590f2ef6f75SFabio Estevam if (type && type->has_nand_tree_disable) 5912b0ba96cSSylvain Rochet kszphy_nand_tree_disable(phydev); 5922b0ba96cSSylvain Rochet 59379e498a9SLeonard Crestez return kszphy_config_reset(phydev); 59420d8435aSBen Dooks } 59520d8435aSBen Dooks 5964217a64eSMichael Walle static int ksz8041_fiber_mode(struct phy_device *phydev) 5974217a64eSMichael Walle { 5984217a64eSMichael Walle struct device_node *of_node = phydev->mdio.dev.of_node; 5994217a64eSMichael Walle 6004217a64eSMichael Walle return of_property_read_bool(of_node, "micrel,fiber-mode"); 6014217a64eSMichael Walle } 6024217a64eSMichael Walle 60377501a79SPhilipp Zabel static int ksz8041_config_init(struct phy_device *phydev) 60477501a79SPhilipp Zabel { 6053c1bcc86SAndrew Lunn __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, }; 6063c1bcc86SAndrew Lunn 60777501a79SPhilipp Zabel /* Limit supported and advertised modes in fiber mode */ 6084217a64eSMichael Walle if (ksz8041_fiber_mode(phydev)) { 60977501a79SPhilipp Zabel phydev->dev_flags |= MICREL_PHY_FXEN; 6103c1bcc86SAndrew Lunn linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, mask); 6113c1bcc86SAndrew Lunn linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, mask); 6123c1bcc86SAndrew Lunn 6133c1bcc86SAndrew Lunn linkmode_and(phydev->supported, phydev->supported, mask); 6143c1bcc86SAndrew Lunn linkmode_set_bit(ETHTOOL_LINK_MODE_FIBRE_BIT, 6153c1bcc86SAndrew Lunn phydev->supported); 6163c1bcc86SAndrew Lunn linkmode_and(phydev->advertising, phydev->advertising, mask); 6173c1bcc86SAndrew Lunn linkmode_set_bit(ETHTOOL_LINK_MODE_FIBRE_BIT, 6183c1bcc86SAndrew Lunn phydev->advertising); 61977501a79SPhilipp Zabel phydev->autoneg = AUTONEG_DISABLE; 62077501a79SPhilipp Zabel } 62177501a79SPhilipp Zabel 62277501a79SPhilipp Zabel return kszphy_config_init(phydev); 62377501a79SPhilipp Zabel } 62477501a79SPhilipp Zabel 62577501a79SPhilipp Zabel static int ksz8041_config_aneg(struct phy_device *phydev) 62677501a79SPhilipp Zabel { 62777501a79SPhilipp Zabel /* Skip auto-negotiation in fiber mode */ 62877501a79SPhilipp Zabel if (phydev->dev_flags & MICREL_PHY_FXEN) { 62977501a79SPhilipp Zabel phydev->speed = SPEED_100; 63077501a79SPhilipp Zabel return 0; 63177501a79SPhilipp Zabel } 63277501a79SPhilipp Zabel 63377501a79SPhilipp Zabel return genphy_config_aneg(phydev); 63477501a79SPhilipp Zabel } 63577501a79SPhilipp Zabel 6368b95599cSMarek Vasut static int ksz8051_ksz8795_match_phy_device(struct phy_device *phydev, 637a5e63c7dSSteve Bennett const bool ksz_8051) 6388b95599cSMarek Vasut { 6398b95599cSMarek Vasut int ret; 6408b95599cSMarek Vasut 641a5e63c7dSSteve Bennett if ((phydev->phy_id & MICREL_PHY_ID_MASK) != PHY_ID_KSZ8051) 6428b95599cSMarek Vasut return 0; 6438b95599cSMarek Vasut 6448b95599cSMarek Vasut ret = phy_read(phydev, MII_BMSR); 6458b95599cSMarek Vasut if (ret < 0) 6468b95599cSMarek Vasut return ret; 6478b95599cSMarek Vasut 6488b95599cSMarek Vasut /* KSZ8051 PHY and KSZ8794/KSZ8795/KSZ8765 switch share the same 6498b95599cSMarek Vasut * exact PHY ID. However, they can be told apart by the extended 6508b95599cSMarek Vasut * capability registers presence. The KSZ8051 PHY has them while 6518b95599cSMarek Vasut * the switch does not. 6528b95599cSMarek Vasut */ 6538b95599cSMarek Vasut ret &= BMSR_ERCAP; 654a5e63c7dSSteve Bennett if (ksz_8051) 6558b95599cSMarek Vasut return ret; 6568b95599cSMarek Vasut else 6578b95599cSMarek Vasut return !ret; 6588b95599cSMarek Vasut } 6598b95599cSMarek Vasut 6608b95599cSMarek Vasut static int ksz8051_match_phy_device(struct phy_device *phydev) 6618b95599cSMarek Vasut { 662a5e63c7dSSteve Bennett return ksz8051_ksz8795_match_phy_device(phydev, true); 6638b95599cSMarek Vasut } 6648b95599cSMarek Vasut 6657a1d8390SAntoine Tenart static int ksz8081_config_init(struct phy_device *phydev) 6667a1d8390SAntoine Tenart { 6677a1d8390SAntoine Tenart /* KSZPHY_OMSO_FACTORY_TEST is set at de-assertion of the reset line 6687a1d8390SAntoine Tenart * based on the RXER (KSZ8081RNA/RND) or TXC (KSZ8081MNX/RNB) pin. If a 6697a1d8390SAntoine Tenart * pull-down is missing, the factory test mode should be cleared by 6707a1d8390SAntoine Tenart * manually writing a 0. 6717a1d8390SAntoine Tenart */ 6727a1d8390SAntoine Tenart phy_clear_bits(phydev, MII_KSZPHY_OMSO, KSZPHY_OMSO_FACTORY_TEST); 6737a1d8390SAntoine Tenart 6747a1d8390SAntoine Tenart return kszphy_config_init(phydev); 6757a1d8390SAntoine Tenart } 6767a1d8390SAntoine Tenart 677f873f112SOleksij Rempel static int ksz8081_config_mdix(struct phy_device *phydev, u8 ctrl) 678f873f112SOleksij Rempel { 679f873f112SOleksij Rempel u16 val; 680f873f112SOleksij Rempel 681f873f112SOleksij Rempel switch (ctrl) { 682f873f112SOleksij Rempel case ETH_TP_MDI: 683f873f112SOleksij Rempel val = KSZ8081_CTRL2_DISABLE_AUTO_MDIX; 684f873f112SOleksij Rempel break; 685f873f112SOleksij Rempel case ETH_TP_MDI_X: 686f873f112SOleksij Rempel val = KSZ8081_CTRL2_DISABLE_AUTO_MDIX | 687f873f112SOleksij Rempel KSZ8081_CTRL2_MDI_MDI_X_SELECT; 688f873f112SOleksij Rempel break; 689f873f112SOleksij Rempel case ETH_TP_MDI_AUTO: 690f873f112SOleksij Rempel val = 0; 691f873f112SOleksij Rempel break; 692f873f112SOleksij Rempel default: 693f873f112SOleksij Rempel return 0; 694f873f112SOleksij Rempel } 695f873f112SOleksij Rempel 696f873f112SOleksij Rempel return phy_modify(phydev, MII_KSZPHY_CTRL_2, 697f873f112SOleksij Rempel KSZ8081_CTRL2_HP_MDIX | 698f873f112SOleksij Rempel KSZ8081_CTRL2_MDI_MDI_X_SELECT | 699f873f112SOleksij Rempel KSZ8081_CTRL2_DISABLE_AUTO_MDIX, 700f873f112SOleksij Rempel KSZ8081_CTRL2_HP_MDIX | val); 701f873f112SOleksij Rempel } 702f873f112SOleksij Rempel 703f873f112SOleksij Rempel static int ksz8081_config_aneg(struct phy_device *phydev) 704f873f112SOleksij Rempel { 705f873f112SOleksij Rempel int ret; 706f873f112SOleksij Rempel 707f873f112SOleksij Rempel ret = genphy_config_aneg(phydev); 708f873f112SOleksij Rempel if (ret) 709f873f112SOleksij Rempel return ret; 710f873f112SOleksij Rempel 711f873f112SOleksij Rempel /* The MDI-X configuration is automatically changed by the PHY after 712f873f112SOleksij Rempel * switching from autoneg off to on. So, take MDI-X configuration under 713f873f112SOleksij Rempel * own control and set it after autoneg configuration was done. 714f873f112SOleksij Rempel */ 715f873f112SOleksij Rempel return ksz8081_config_mdix(phydev, phydev->mdix_ctrl); 716f873f112SOleksij Rempel } 717f873f112SOleksij Rempel 718f873f112SOleksij Rempel static int ksz8081_mdix_update(struct phy_device *phydev) 719f873f112SOleksij Rempel { 720f873f112SOleksij Rempel int ret; 721f873f112SOleksij Rempel 722f873f112SOleksij Rempel ret = phy_read(phydev, MII_KSZPHY_CTRL_2); 723f873f112SOleksij Rempel if (ret < 0) 724f873f112SOleksij Rempel return ret; 725f873f112SOleksij Rempel 726f873f112SOleksij Rempel if (ret & KSZ8081_CTRL2_DISABLE_AUTO_MDIX) { 727f873f112SOleksij Rempel if (ret & KSZ8081_CTRL2_MDI_MDI_X_SELECT) 728f873f112SOleksij Rempel phydev->mdix_ctrl = ETH_TP_MDI_X; 729f873f112SOleksij Rempel else 730f873f112SOleksij Rempel phydev->mdix_ctrl = ETH_TP_MDI; 731f873f112SOleksij Rempel } else { 732f873f112SOleksij Rempel phydev->mdix_ctrl = ETH_TP_MDI_AUTO; 733f873f112SOleksij Rempel } 734f873f112SOleksij Rempel 735f873f112SOleksij Rempel ret = phy_read(phydev, MII_KSZPHY_CTRL_1); 736f873f112SOleksij Rempel if (ret < 0) 737f873f112SOleksij Rempel return ret; 738f873f112SOleksij Rempel 739f873f112SOleksij Rempel if (ret & KSZ8081_CTRL1_MDIX_STAT) 740f873f112SOleksij Rempel phydev->mdix = ETH_TP_MDI; 741f873f112SOleksij Rempel else 742f873f112SOleksij Rempel phydev->mdix = ETH_TP_MDI_X; 743f873f112SOleksij Rempel 744f873f112SOleksij Rempel return 0; 745f873f112SOleksij Rempel } 746f873f112SOleksij Rempel 747f873f112SOleksij Rempel static int ksz8081_read_status(struct phy_device *phydev) 748f873f112SOleksij Rempel { 749f873f112SOleksij Rempel int ret; 750f873f112SOleksij Rempel 751f873f112SOleksij Rempel ret = ksz8081_mdix_update(phydev); 752f873f112SOleksij Rempel if (ret < 0) 753f873f112SOleksij Rempel return ret; 754f873f112SOleksij Rempel 755f873f112SOleksij Rempel return genphy_read_status(phydev); 756f873f112SOleksij Rempel } 757f873f112SOleksij Rempel 758232ba3a5SRajasingh Thavamani static int ksz8061_config_init(struct phy_device *phydev) 759232ba3a5SRajasingh Thavamani { 760232ba3a5SRajasingh Thavamani int ret; 761232ba3a5SRajasingh Thavamani 762232ba3a5SRajasingh Thavamani ret = phy_write_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_DEVID1, 0xB61A); 763232ba3a5SRajasingh Thavamani if (ret) 764232ba3a5SRajasingh Thavamani return ret; 765232ba3a5SRajasingh Thavamani 766232ba3a5SRajasingh Thavamani return kszphy_config_init(phydev); 767232ba3a5SRajasingh Thavamani } 768232ba3a5SRajasingh Thavamani 7698b95599cSMarek Vasut static int ksz8795_match_phy_device(struct phy_device *phydev) 7708b95599cSMarek Vasut { 771a5e63c7dSSteve Bennett return ksz8051_ksz8795_match_phy_device(phydev, false); 7728b95599cSMarek Vasut } 7738b95599cSMarek Vasut 774954c3967SSean Cross static int ksz9021_load_values_from_of(struct phy_device *phydev, 7753c9a9f7fSJaeden Amero const struct device_node *of_node, 7763c9a9f7fSJaeden Amero u16 reg, 7773c9a9f7fSJaeden Amero const char *field1, const char *field2, 7783c9a9f7fSJaeden Amero const char *field3, const char *field4) 779954c3967SSean Cross { 780954c3967SSean Cross int val1 = -1; 781954c3967SSean Cross int val2 = -2; 782954c3967SSean Cross int val3 = -3; 783954c3967SSean Cross int val4 = -4; 784954c3967SSean Cross int newval; 785954c3967SSean Cross int matches = 0; 786954c3967SSean Cross 787954c3967SSean Cross if (!of_property_read_u32(of_node, field1, &val1)) 788954c3967SSean Cross matches++; 789954c3967SSean Cross 790954c3967SSean Cross if (!of_property_read_u32(of_node, field2, &val2)) 791954c3967SSean Cross matches++; 792954c3967SSean Cross 793954c3967SSean Cross if (!of_property_read_u32(of_node, field3, &val3)) 794954c3967SSean Cross matches++; 795954c3967SSean Cross 796954c3967SSean Cross if (!of_property_read_u32(of_node, field4, &val4)) 797954c3967SSean Cross matches++; 798954c3967SSean Cross 799954c3967SSean Cross if (!matches) 800954c3967SSean Cross return 0; 801954c3967SSean Cross 802954c3967SSean Cross if (matches < 4) 803954c3967SSean Cross newval = kszphy_extended_read(phydev, reg); 804954c3967SSean Cross else 805954c3967SSean Cross newval = 0; 806954c3967SSean Cross 807954c3967SSean Cross if (val1 != -1) 808954c3967SSean Cross newval = ((newval & 0xfff0) | ((val1 / PS_TO_REG) & 0xf) << 0); 809954c3967SSean Cross 8106a119745SHubert Chaumette if (val2 != -2) 811954c3967SSean Cross newval = ((newval & 0xff0f) | ((val2 / PS_TO_REG) & 0xf) << 4); 812954c3967SSean Cross 8136a119745SHubert Chaumette if (val3 != -3) 814954c3967SSean Cross newval = ((newval & 0xf0ff) | ((val3 / PS_TO_REG) & 0xf) << 8); 815954c3967SSean Cross 8166a119745SHubert Chaumette if (val4 != -4) 817954c3967SSean Cross newval = ((newval & 0x0fff) | ((val4 / PS_TO_REG) & 0xf) << 12); 818954c3967SSean Cross 819954c3967SSean Cross return kszphy_extended_write(phydev, reg, newval); 820954c3967SSean Cross } 821954c3967SSean Cross 822954c3967SSean Cross static int ksz9021_config_init(struct phy_device *phydev) 823954c3967SSean Cross { 824ce4f8afdSColin Ian King const struct device_node *of_node; 825651df218SAndrew Lunn const struct device *dev_walker; 826954c3967SSean Cross 827651df218SAndrew Lunn /* The Micrel driver has a deprecated option to place phy OF 828651df218SAndrew Lunn * properties in the MAC node. Walk up the tree of devices to 829651df218SAndrew Lunn * find a device with an OF node. 830651df218SAndrew Lunn */ 831e5a03bfdSAndrew Lunn dev_walker = &phydev->mdio.dev; 832651df218SAndrew Lunn do { 833651df218SAndrew Lunn of_node = dev_walker->of_node; 834651df218SAndrew Lunn dev_walker = dev_walker->parent; 835651df218SAndrew Lunn 836651df218SAndrew Lunn } while (!of_node && dev_walker); 837954c3967SSean Cross 838954c3967SSean Cross if (of_node) { 839954c3967SSean Cross ksz9021_load_values_from_of(phydev, of_node, 840954c3967SSean Cross MII_KSZPHY_CLK_CONTROL_PAD_SKEW, 841954c3967SSean Cross "txen-skew-ps", "txc-skew-ps", 842954c3967SSean Cross "rxdv-skew-ps", "rxc-skew-ps"); 843954c3967SSean Cross ksz9021_load_values_from_of(phydev, of_node, 844954c3967SSean Cross MII_KSZPHY_RX_DATA_PAD_SKEW, 845954c3967SSean Cross "rxd0-skew-ps", "rxd1-skew-ps", 846954c3967SSean Cross "rxd2-skew-ps", "rxd3-skew-ps"); 847954c3967SSean Cross ksz9021_load_values_from_of(phydev, of_node, 848954c3967SSean Cross MII_KSZPHY_TX_DATA_PAD_SKEW, 849954c3967SSean Cross "txd0-skew-ps", "txd1-skew-ps", 850954c3967SSean Cross "txd2-skew-ps", "txd3-skew-ps"); 851954c3967SSean Cross } 852954c3967SSean Cross return 0; 853954c3967SSean Cross } 854954c3967SSean Cross 8556e4b8273SHubert Chaumette #define KSZ9031_PS_TO_REG 60 8566e4b8273SHubert Chaumette 8576e4b8273SHubert Chaumette /* Extended registers */ 8586270e1aeSJaeden Amero /* MMD Address 0x0 */ 8596270e1aeSJaeden Amero #define MII_KSZ9031RN_FLP_BURST_TX_LO 3 8606270e1aeSJaeden Amero #define MII_KSZ9031RN_FLP_BURST_TX_HI 4 8616270e1aeSJaeden Amero 862ae6c97bbSJaeden Amero /* MMD Address 0x2 */ 8636e4b8273SHubert Chaumette #define MII_KSZ9031RN_CONTROL_PAD_SKEW 4 864bcf3440cSOleksij Rempel #define MII_KSZ9031RN_RX_CTL_M GENMASK(7, 4) 865bcf3440cSOleksij Rempel #define MII_KSZ9031RN_TX_CTL_M GENMASK(3, 0) 866bcf3440cSOleksij Rempel 8676e4b8273SHubert Chaumette #define MII_KSZ9031RN_RX_DATA_PAD_SKEW 5 868bcf3440cSOleksij Rempel #define MII_KSZ9031RN_RXD3 GENMASK(15, 12) 869bcf3440cSOleksij Rempel #define MII_KSZ9031RN_RXD2 GENMASK(11, 8) 870bcf3440cSOleksij Rempel #define MII_KSZ9031RN_RXD1 GENMASK(7, 4) 871bcf3440cSOleksij Rempel #define MII_KSZ9031RN_RXD0 GENMASK(3, 0) 872bcf3440cSOleksij Rempel 8736e4b8273SHubert Chaumette #define MII_KSZ9031RN_TX_DATA_PAD_SKEW 6 874bcf3440cSOleksij Rempel #define MII_KSZ9031RN_TXD3 GENMASK(15, 12) 875bcf3440cSOleksij Rempel #define MII_KSZ9031RN_TXD2 GENMASK(11, 8) 876bcf3440cSOleksij Rempel #define MII_KSZ9031RN_TXD1 GENMASK(7, 4) 877bcf3440cSOleksij Rempel #define MII_KSZ9031RN_TXD0 GENMASK(3, 0) 878bcf3440cSOleksij Rempel 8796e4b8273SHubert Chaumette #define MII_KSZ9031RN_CLK_PAD_SKEW 8 880bcf3440cSOleksij Rempel #define MII_KSZ9031RN_GTX_CLK GENMASK(9, 5) 881bcf3440cSOleksij Rempel #define MII_KSZ9031RN_RX_CLK GENMASK(4, 0) 882bcf3440cSOleksij Rempel 883bcf3440cSOleksij Rempel /* KSZ9031 has internal RGMII_IDRX = 1.2ns and RGMII_IDTX = 0ns. To 884bcf3440cSOleksij Rempel * provide different RGMII options we need to configure delay offset 885bcf3440cSOleksij Rempel * for each pad relative to build in delay. 886bcf3440cSOleksij Rempel */ 887bcf3440cSOleksij Rempel /* keep rx as "No delay adjustment" and set rx_clk to +0.60ns to get delays of 888bcf3440cSOleksij Rempel * 1.80ns 889bcf3440cSOleksij Rempel */ 890bcf3440cSOleksij Rempel #define RX_ID 0x7 891bcf3440cSOleksij Rempel #define RX_CLK_ID 0x19 892bcf3440cSOleksij Rempel 893bcf3440cSOleksij Rempel /* set rx to +0.30ns and rx_clk to -0.90ns to compensate the 894bcf3440cSOleksij Rempel * internal 1.2ns delay. 895bcf3440cSOleksij Rempel */ 896bcf3440cSOleksij Rempel #define RX_ND 0xc 897bcf3440cSOleksij Rempel #define RX_CLK_ND 0x0 898bcf3440cSOleksij Rempel 899bcf3440cSOleksij Rempel /* set tx to -0.42ns and tx_clk to +0.96ns to get 1.38ns delay */ 900bcf3440cSOleksij Rempel #define TX_ID 0x0 901bcf3440cSOleksij Rempel #define TX_CLK_ID 0x1f 902bcf3440cSOleksij Rempel 903bcf3440cSOleksij Rempel /* set tx and tx_clk to "No delay adjustment" to keep 0ns 904bcf3440cSOleksij Rempel * dealy 905bcf3440cSOleksij Rempel */ 906bcf3440cSOleksij Rempel #define TX_ND 0x7 907bcf3440cSOleksij Rempel #define TX_CLK_ND 0xf 9086e4b8273SHubert Chaumette 909af70c1f9SMike Looijmans /* MMD Address 0x1C */ 910af70c1f9SMike Looijmans #define MII_KSZ9031RN_EDPD 0x23 911af70c1f9SMike Looijmans #define MII_KSZ9031RN_EDPD_ENABLE BIT(0) 912af70c1f9SMike Looijmans 9136e4b8273SHubert Chaumette static int ksz9031_of_load_skew_values(struct phy_device *phydev, 9143c9a9f7fSJaeden Amero const struct device_node *of_node, 9156e4b8273SHubert Chaumette u16 reg, size_t field_sz, 916bcf3440cSOleksij Rempel const char *field[], u8 numfields, 917bcf3440cSOleksij Rempel bool *update) 9186e4b8273SHubert Chaumette { 9196e4b8273SHubert Chaumette int val[4] = {-1, -2, -3, -4}; 9206e4b8273SHubert Chaumette int matches = 0; 9216e4b8273SHubert Chaumette u16 mask; 9226e4b8273SHubert Chaumette u16 maxval; 9236e4b8273SHubert Chaumette u16 newval; 9246e4b8273SHubert Chaumette int i; 9256e4b8273SHubert Chaumette 9266e4b8273SHubert Chaumette for (i = 0; i < numfields; i++) 9276e4b8273SHubert Chaumette if (!of_property_read_u32(of_node, field[i], val + i)) 9286e4b8273SHubert Chaumette matches++; 9296e4b8273SHubert Chaumette 9306e4b8273SHubert Chaumette if (!matches) 9316e4b8273SHubert Chaumette return 0; 9326e4b8273SHubert Chaumette 933bcf3440cSOleksij Rempel *update |= true; 934bcf3440cSOleksij Rempel 9356e4b8273SHubert Chaumette if (matches < numfields) 9369b420effSHeiner Kallweit newval = phy_read_mmd(phydev, 2, reg); 9376e4b8273SHubert Chaumette else 9386e4b8273SHubert Chaumette newval = 0; 9396e4b8273SHubert Chaumette 9406e4b8273SHubert Chaumette maxval = (field_sz == 4) ? 0xf : 0x1f; 9416e4b8273SHubert Chaumette for (i = 0; i < numfields; i++) 9426e4b8273SHubert Chaumette if (val[i] != -(i + 1)) { 9436e4b8273SHubert Chaumette mask = 0xffff; 9446e4b8273SHubert Chaumette mask ^= maxval << (field_sz * i); 9456e4b8273SHubert Chaumette newval = (newval & mask) | 9466e4b8273SHubert Chaumette (((val[i] / KSZ9031_PS_TO_REG) & maxval) 9476e4b8273SHubert Chaumette << (field_sz * i)); 9486e4b8273SHubert Chaumette } 9496e4b8273SHubert Chaumette 9509b420effSHeiner Kallweit return phy_write_mmd(phydev, 2, reg, newval); 9516e4b8273SHubert Chaumette } 9526e4b8273SHubert Chaumette 953a0da456bSMax Uvarov /* Center KSZ9031RNX FLP timing at 16ms. */ 9546270e1aeSJaeden Amero static int ksz9031_center_flp_timing(struct phy_device *phydev) 9556270e1aeSJaeden Amero { 9566270e1aeSJaeden Amero int result; 9576270e1aeSJaeden Amero 9589b420effSHeiner Kallweit result = phy_write_mmd(phydev, 0, MII_KSZ9031RN_FLP_BURST_TX_HI, 9599b420effSHeiner Kallweit 0x0006); 960a0da456bSMax Uvarov if (result) 961a0da456bSMax Uvarov return result; 962a0da456bSMax Uvarov 9639b420effSHeiner Kallweit result = phy_write_mmd(phydev, 0, MII_KSZ9031RN_FLP_BURST_TX_LO, 9649b420effSHeiner Kallweit 0x1A80); 9656270e1aeSJaeden Amero if (result) 9666270e1aeSJaeden Amero return result; 9676270e1aeSJaeden Amero 9686270e1aeSJaeden Amero return genphy_restart_aneg(phydev); 9696270e1aeSJaeden Amero } 9706270e1aeSJaeden Amero 971af70c1f9SMike Looijmans /* Enable energy-detect power-down mode */ 972af70c1f9SMike Looijmans static int ksz9031_enable_edpd(struct phy_device *phydev) 973af70c1f9SMike Looijmans { 974af70c1f9SMike Looijmans int reg; 975af70c1f9SMike Looijmans 9769b420effSHeiner Kallweit reg = phy_read_mmd(phydev, 0x1C, MII_KSZ9031RN_EDPD); 977af70c1f9SMike Looijmans if (reg < 0) 978af70c1f9SMike Looijmans return reg; 9799b420effSHeiner Kallweit return phy_write_mmd(phydev, 0x1C, MII_KSZ9031RN_EDPD, 980af70c1f9SMike Looijmans reg | MII_KSZ9031RN_EDPD_ENABLE); 981af70c1f9SMike Looijmans } 982af70c1f9SMike Looijmans 983bcf3440cSOleksij Rempel static int ksz9031_config_rgmii_delay(struct phy_device *phydev) 984bcf3440cSOleksij Rempel { 985bcf3440cSOleksij Rempel u16 rx, tx, rx_clk, tx_clk; 986bcf3440cSOleksij Rempel int ret; 987bcf3440cSOleksij Rempel 988bcf3440cSOleksij Rempel switch (phydev->interface) { 989bcf3440cSOleksij Rempel case PHY_INTERFACE_MODE_RGMII: 990bcf3440cSOleksij Rempel tx = TX_ND; 991bcf3440cSOleksij Rempel tx_clk = TX_CLK_ND; 992bcf3440cSOleksij Rempel rx = RX_ND; 993bcf3440cSOleksij Rempel rx_clk = RX_CLK_ND; 994bcf3440cSOleksij Rempel break; 995bcf3440cSOleksij Rempel case PHY_INTERFACE_MODE_RGMII_ID: 996bcf3440cSOleksij Rempel tx = TX_ID; 997bcf3440cSOleksij Rempel tx_clk = TX_CLK_ID; 998bcf3440cSOleksij Rempel rx = RX_ID; 999bcf3440cSOleksij Rempel rx_clk = RX_CLK_ID; 1000bcf3440cSOleksij Rempel break; 1001bcf3440cSOleksij Rempel case PHY_INTERFACE_MODE_RGMII_RXID: 1002bcf3440cSOleksij Rempel tx = TX_ND; 1003bcf3440cSOleksij Rempel tx_clk = TX_CLK_ND; 1004bcf3440cSOleksij Rempel rx = RX_ID; 1005bcf3440cSOleksij Rempel rx_clk = RX_CLK_ID; 1006bcf3440cSOleksij Rempel break; 1007bcf3440cSOleksij Rempel case PHY_INTERFACE_MODE_RGMII_TXID: 1008bcf3440cSOleksij Rempel tx = TX_ID; 1009bcf3440cSOleksij Rempel tx_clk = TX_CLK_ID; 1010bcf3440cSOleksij Rempel rx = RX_ND; 1011bcf3440cSOleksij Rempel rx_clk = RX_CLK_ND; 1012bcf3440cSOleksij Rempel break; 1013bcf3440cSOleksij Rempel default: 1014bcf3440cSOleksij Rempel return 0; 1015bcf3440cSOleksij Rempel } 1016bcf3440cSOleksij Rempel 1017bcf3440cSOleksij Rempel ret = phy_write_mmd(phydev, 2, MII_KSZ9031RN_CONTROL_PAD_SKEW, 1018bcf3440cSOleksij Rempel FIELD_PREP(MII_KSZ9031RN_RX_CTL_M, rx) | 1019bcf3440cSOleksij Rempel FIELD_PREP(MII_KSZ9031RN_TX_CTL_M, tx)); 1020bcf3440cSOleksij Rempel if (ret < 0) 1021bcf3440cSOleksij Rempel return ret; 1022bcf3440cSOleksij Rempel 1023bcf3440cSOleksij Rempel ret = phy_write_mmd(phydev, 2, MII_KSZ9031RN_RX_DATA_PAD_SKEW, 1024bcf3440cSOleksij Rempel FIELD_PREP(MII_KSZ9031RN_RXD3, rx) | 1025bcf3440cSOleksij Rempel FIELD_PREP(MII_KSZ9031RN_RXD2, rx) | 1026bcf3440cSOleksij Rempel FIELD_PREP(MII_KSZ9031RN_RXD1, rx) | 1027bcf3440cSOleksij Rempel FIELD_PREP(MII_KSZ9031RN_RXD0, rx)); 1028bcf3440cSOleksij Rempel if (ret < 0) 1029bcf3440cSOleksij Rempel return ret; 1030bcf3440cSOleksij Rempel 1031bcf3440cSOleksij Rempel ret = phy_write_mmd(phydev, 2, MII_KSZ9031RN_TX_DATA_PAD_SKEW, 1032bcf3440cSOleksij Rempel FIELD_PREP(MII_KSZ9031RN_TXD3, tx) | 1033bcf3440cSOleksij Rempel FIELD_PREP(MII_KSZ9031RN_TXD2, tx) | 1034bcf3440cSOleksij Rempel FIELD_PREP(MII_KSZ9031RN_TXD1, tx) | 1035bcf3440cSOleksij Rempel FIELD_PREP(MII_KSZ9031RN_TXD0, tx)); 1036bcf3440cSOleksij Rempel if (ret < 0) 1037bcf3440cSOleksij Rempel return ret; 1038bcf3440cSOleksij Rempel 1039bcf3440cSOleksij Rempel return phy_write_mmd(phydev, 2, MII_KSZ9031RN_CLK_PAD_SKEW, 1040bcf3440cSOleksij Rempel FIELD_PREP(MII_KSZ9031RN_GTX_CLK, tx_clk) | 1041bcf3440cSOleksij Rempel FIELD_PREP(MII_KSZ9031RN_RX_CLK, rx_clk)); 1042bcf3440cSOleksij Rempel } 1043bcf3440cSOleksij Rempel 10446e4b8273SHubert Chaumette static int ksz9031_config_init(struct phy_device *phydev) 10456e4b8273SHubert Chaumette { 1046ce4f8afdSColin Ian King const struct device_node *of_node; 10473c9a9f7fSJaeden Amero static const char *clk_skews[2] = {"rxc-skew-ps", "txc-skew-ps"}; 10483c9a9f7fSJaeden Amero static const char *rx_data_skews[4] = { 10496e4b8273SHubert Chaumette "rxd0-skew-ps", "rxd1-skew-ps", 10506e4b8273SHubert Chaumette "rxd2-skew-ps", "rxd3-skew-ps" 10516e4b8273SHubert Chaumette }; 10523c9a9f7fSJaeden Amero static const char *tx_data_skews[4] = { 10536e4b8273SHubert Chaumette "txd0-skew-ps", "txd1-skew-ps", 10546e4b8273SHubert Chaumette "txd2-skew-ps", "txd3-skew-ps" 10556e4b8273SHubert Chaumette }; 10563c9a9f7fSJaeden Amero static const char *control_skews[2] = {"txen-skew-ps", "rxdv-skew-ps"}; 1057b4c19f71SRoosen Henri const struct device *dev_walker; 1058af70c1f9SMike Looijmans int result; 1059af70c1f9SMike Looijmans 1060af70c1f9SMike Looijmans result = ksz9031_enable_edpd(phydev); 1061af70c1f9SMike Looijmans if (result < 0) 1062af70c1f9SMike Looijmans return result; 10636e4b8273SHubert Chaumette 1064b4c19f71SRoosen Henri /* The Micrel driver has a deprecated option to place phy OF 1065b4c19f71SRoosen Henri * properties in the MAC node. Walk up the tree of devices to 1066b4c19f71SRoosen Henri * find a device with an OF node. 1067b4c19f71SRoosen Henri */ 10689d367eddSDavid S. Miller dev_walker = &phydev->mdio.dev; 1069b4c19f71SRoosen Henri do { 1070b4c19f71SRoosen Henri of_node = dev_walker->of_node; 1071b4c19f71SRoosen Henri dev_walker = dev_walker->parent; 1072b4c19f71SRoosen Henri } while (!of_node && dev_walker); 10736e4b8273SHubert Chaumette 10746e4b8273SHubert Chaumette if (of_node) { 1075bcf3440cSOleksij Rempel bool update = false; 1076bcf3440cSOleksij Rempel 1077bcf3440cSOleksij Rempel if (phy_interface_is_rgmii(phydev)) { 1078bcf3440cSOleksij Rempel result = ksz9031_config_rgmii_delay(phydev); 1079bcf3440cSOleksij Rempel if (result < 0) 1080bcf3440cSOleksij Rempel return result; 1081bcf3440cSOleksij Rempel } 1082bcf3440cSOleksij Rempel 10836e4b8273SHubert Chaumette ksz9031_of_load_skew_values(phydev, of_node, 10846e4b8273SHubert Chaumette MII_KSZ9031RN_CLK_PAD_SKEW, 5, 1085bcf3440cSOleksij Rempel clk_skews, 2, &update); 10866e4b8273SHubert Chaumette 10876e4b8273SHubert Chaumette ksz9031_of_load_skew_values(phydev, of_node, 10886e4b8273SHubert Chaumette MII_KSZ9031RN_CONTROL_PAD_SKEW, 4, 1089bcf3440cSOleksij Rempel control_skews, 2, &update); 10906e4b8273SHubert Chaumette 10916e4b8273SHubert Chaumette ksz9031_of_load_skew_values(phydev, of_node, 10926e4b8273SHubert Chaumette MII_KSZ9031RN_RX_DATA_PAD_SKEW, 4, 1093bcf3440cSOleksij Rempel rx_data_skews, 4, &update); 10946e4b8273SHubert Chaumette 10956e4b8273SHubert Chaumette ksz9031_of_load_skew_values(phydev, of_node, 10966e4b8273SHubert Chaumette MII_KSZ9031RN_TX_DATA_PAD_SKEW, 4, 1097bcf3440cSOleksij Rempel tx_data_skews, 4, &update); 1098bcf3440cSOleksij Rempel 109967ca5159SMatthias Schiffer if (update && !phy_interface_is_rgmii(phydev)) 1100bcf3440cSOleksij Rempel phydev_warn(phydev, 110167ca5159SMatthias Schiffer "*-skew-ps values should be used only with RGMII PHY modes\n"); 1102e1b505a6SMarkus Niebel 1103e1b505a6SMarkus Niebel /* Silicon Errata Sheet (DS80000691D or DS80000692D): 1104e1b505a6SMarkus Niebel * When the device links in the 1000BASE-T slave mode only, 1105e1b505a6SMarkus Niebel * the optional 125MHz reference output clock (CLK125_NDO) 1106e1b505a6SMarkus Niebel * has wide duty cycle variation. 1107e1b505a6SMarkus Niebel * 1108e1b505a6SMarkus Niebel * The optional CLK125_NDO clock does not meet the RGMII 1109e1b505a6SMarkus Niebel * 45/55 percent (min/max) duty cycle requirement and therefore 1110e1b505a6SMarkus Niebel * cannot be used directly by the MAC side for clocking 1111e1b505a6SMarkus Niebel * applications that have setup/hold time requirements on 1112e1b505a6SMarkus Niebel * rising and falling clock edges. 1113e1b505a6SMarkus Niebel * 1114e1b505a6SMarkus Niebel * Workaround: 1115e1b505a6SMarkus Niebel * Force the phy to be the master to receive a stable clock 1116e1b505a6SMarkus Niebel * which meets the duty cycle requirement. 1117e1b505a6SMarkus Niebel */ 1118e1b505a6SMarkus Niebel if (of_property_read_bool(of_node, "micrel,force-master")) { 1119e1b505a6SMarkus Niebel result = phy_read(phydev, MII_CTRL1000); 1120e1b505a6SMarkus Niebel if (result < 0) 1121e1b505a6SMarkus Niebel goto err_force_master; 1122e1b505a6SMarkus Niebel 1123e1b505a6SMarkus Niebel /* enable master mode, config & prefer master */ 1124e1b505a6SMarkus Niebel result |= CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER; 1125e1b505a6SMarkus Niebel result = phy_write(phydev, MII_CTRL1000, result); 1126e1b505a6SMarkus Niebel if (result < 0) 1127e1b505a6SMarkus Niebel goto err_force_master; 1128e1b505a6SMarkus Niebel } 11296e4b8273SHubert Chaumette } 11306270e1aeSJaeden Amero 11316270e1aeSJaeden Amero return ksz9031_center_flp_timing(phydev); 1132e1b505a6SMarkus Niebel 1133e1b505a6SMarkus Niebel err_force_master: 1134e1b505a6SMarkus Niebel phydev_err(phydev, "failed to force the phy to master mode\n"); 1135e1b505a6SMarkus Niebel return result; 11366e4b8273SHubert Chaumette } 11376e4b8273SHubert Chaumette 1138bff5b4b3SYuiko Oshino #define KSZ9131_SKEW_5BIT_MAX 2400 1139bff5b4b3SYuiko Oshino #define KSZ9131_SKEW_4BIT_MAX 800 1140bff5b4b3SYuiko Oshino #define KSZ9131_OFFSET 700 1141bff5b4b3SYuiko Oshino #define KSZ9131_STEP 100 1142bff5b4b3SYuiko Oshino 1143bff5b4b3SYuiko Oshino static int ksz9131_of_load_skew_values(struct phy_device *phydev, 1144bff5b4b3SYuiko Oshino struct device_node *of_node, 1145bff5b4b3SYuiko Oshino u16 reg, size_t field_sz, 1146bff5b4b3SYuiko Oshino char *field[], u8 numfields) 1147bff5b4b3SYuiko Oshino { 1148bff5b4b3SYuiko Oshino int val[4] = {-(1 + KSZ9131_OFFSET), -(2 + KSZ9131_OFFSET), 1149bff5b4b3SYuiko Oshino -(3 + KSZ9131_OFFSET), -(4 + KSZ9131_OFFSET)}; 1150bff5b4b3SYuiko Oshino int skewval, skewmax = 0; 1151bff5b4b3SYuiko Oshino int matches = 0; 1152bff5b4b3SYuiko Oshino u16 maxval; 1153bff5b4b3SYuiko Oshino u16 newval; 1154bff5b4b3SYuiko Oshino u16 mask; 1155bff5b4b3SYuiko Oshino int i; 1156bff5b4b3SYuiko Oshino 1157bff5b4b3SYuiko Oshino /* psec properties in dts should mean x pico seconds */ 1158bff5b4b3SYuiko Oshino if (field_sz == 5) 1159bff5b4b3SYuiko Oshino skewmax = KSZ9131_SKEW_5BIT_MAX; 1160bff5b4b3SYuiko Oshino else 1161bff5b4b3SYuiko Oshino skewmax = KSZ9131_SKEW_4BIT_MAX; 1162bff5b4b3SYuiko Oshino 1163bff5b4b3SYuiko Oshino for (i = 0; i < numfields; i++) 1164bff5b4b3SYuiko Oshino if (!of_property_read_s32(of_node, field[i], &skewval)) { 1165bff5b4b3SYuiko Oshino if (skewval < -KSZ9131_OFFSET) 1166bff5b4b3SYuiko Oshino skewval = -KSZ9131_OFFSET; 1167bff5b4b3SYuiko Oshino else if (skewval > skewmax) 1168bff5b4b3SYuiko Oshino skewval = skewmax; 1169bff5b4b3SYuiko Oshino 1170bff5b4b3SYuiko Oshino val[i] = skewval + KSZ9131_OFFSET; 1171bff5b4b3SYuiko Oshino matches++; 1172bff5b4b3SYuiko Oshino } 1173bff5b4b3SYuiko Oshino 1174bff5b4b3SYuiko Oshino if (!matches) 1175bff5b4b3SYuiko Oshino return 0; 1176bff5b4b3SYuiko Oshino 1177bff5b4b3SYuiko Oshino if (matches < numfields) 11789b420effSHeiner Kallweit newval = phy_read_mmd(phydev, 2, reg); 1179bff5b4b3SYuiko Oshino else 1180bff5b4b3SYuiko Oshino newval = 0; 1181bff5b4b3SYuiko Oshino 1182bff5b4b3SYuiko Oshino maxval = (field_sz == 4) ? 0xf : 0x1f; 1183bff5b4b3SYuiko Oshino for (i = 0; i < numfields; i++) 1184bff5b4b3SYuiko Oshino if (val[i] != -(i + 1 + KSZ9131_OFFSET)) { 1185bff5b4b3SYuiko Oshino mask = 0xffff; 1186bff5b4b3SYuiko Oshino mask ^= maxval << (field_sz * i); 1187bff5b4b3SYuiko Oshino newval = (newval & mask) | 1188bff5b4b3SYuiko Oshino (((val[i] / KSZ9131_STEP) & maxval) 1189bff5b4b3SYuiko Oshino << (field_sz * i)); 1190bff5b4b3SYuiko Oshino } 1191bff5b4b3SYuiko Oshino 11929b420effSHeiner Kallweit return phy_write_mmd(phydev, 2, reg, newval); 1193bff5b4b3SYuiko Oshino } 1194bff5b4b3SYuiko Oshino 1195bd734a74SPhilippe Schenker #define KSZ9131RN_MMD_COMMON_CTRL_REG 2 1196bd734a74SPhilippe Schenker #define KSZ9131RN_RXC_DLL_CTRL 76 1197bd734a74SPhilippe Schenker #define KSZ9131RN_TXC_DLL_CTRL 77 1198bd734a74SPhilippe Schenker #define KSZ9131RN_DLL_ENABLE_DELAY 0 1199bd734a74SPhilippe Schenker 1200bd734a74SPhilippe Schenker static int ksz9131_config_rgmii_delay(struct phy_device *phydev) 1201bd734a74SPhilippe Schenker { 1202a8f1a19dSHoratiu Vultur const struct kszphy_type *type = phydev->drv->driver_data; 1203bd734a74SPhilippe Schenker u16 rxcdll_val, txcdll_val; 1204bd734a74SPhilippe Schenker int ret; 1205bd734a74SPhilippe Schenker 1206bd734a74SPhilippe Schenker switch (phydev->interface) { 1207bd734a74SPhilippe Schenker case PHY_INTERFACE_MODE_RGMII: 1208a8f1a19dSHoratiu Vultur rxcdll_val = type->disable_dll_rx_bit; 1209a8f1a19dSHoratiu Vultur txcdll_val = type->disable_dll_tx_bit; 1210bd734a74SPhilippe Schenker break; 1211bd734a74SPhilippe Schenker case PHY_INTERFACE_MODE_RGMII_ID: 1212bd734a74SPhilippe Schenker rxcdll_val = KSZ9131RN_DLL_ENABLE_DELAY; 1213bd734a74SPhilippe Schenker txcdll_val = KSZ9131RN_DLL_ENABLE_DELAY; 1214bd734a74SPhilippe Schenker break; 1215bd734a74SPhilippe Schenker case PHY_INTERFACE_MODE_RGMII_RXID: 1216bd734a74SPhilippe Schenker rxcdll_val = KSZ9131RN_DLL_ENABLE_DELAY; 1217a8f1a19dSHoratiu Vultur txcdll_val = type->disable_dll_tx_bit; 1218bd734a74SPhilippe Schenker break; 1219bd734a74SPhilippe Schenker case PHY_INTERFACE_MODE_RGMII_TXID: 1220a8f1a19dSHoratiu Vultur rxcdll_val = type->disable_dll_rx_bit; 1221bd734a74SPhilippe Schenker txcdll_val = KSZ9131RN_DLL_ENABLE_DELAY; 1222bd734a74SPhilippe Schenker break; 1223bd734a74SPhilippe Schenker default: 1224bd734a74SPhilippe Schenker return 0; 1225bd734a74SPhilippe Schenker } 1226bd734a74SPhilippe Schenker 1227bd734a74SPhilippe Schenker ret = phy_modify_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG, 1228a8f1a19dSHoratiu Vultur KSZ9131RN_RXC_DLL_CTRL, type->disable_dll_mask, 1229bd734a74SPhilippe Schenker rxcdll_val); 1230bd734a74SPhilippe Schenker if (ret < 0) 1231bd734a74SPhilippe Schenker return ret; 1232bd734a74SPhilippe Schenker 1233bd734a74SPhilippe Schenker return phy_modify_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG, 1234a8f1a19dSHoratiu Vultur KSZ9131RN_TXC_DLL_CTRL, type->disable_dll_mask, 1235bd734a74SPhilippe Schenker txcdll_val); 1236bd734a74SPhilippe Schenker } 1237bd734a74SPhilippe Schenker 12380316c7e6SFrancesco Dolcini /* Silicon Errata DS80000693B 12390316c7e6SFrancesco Dolcini * 12400316c7e6SFrancesco Dolcini * When LEDs are configured in Individual Mode, LED1 is ON in a no-link 12410316c7e6SFrancesco Dolcini * condition. Workaround is to set register 0x1e, bit 9, this way LED1 behaves 12420316c7e6SFrancesco Dolcini * according to the datasheet (off if there is no link). 12430316c7e6SFrancesco Dolcini */ 12440316c7e6SFrancesco Dolcini static int ksz9131_led_errata(struct phy_device *phydev) 12450316c7e6SFrancesco Dolcini { 12460316c7e6SFrancesco Dolcini int reg; 12470316c7e6SFrancesco Dolcini 12480316c7e6SFrancesco Dolcini reg = phy_read_mmd(phydev, 2, 0); 12490316c7e6SFrancesco Dolcini if (reg < 0) 12500316c7e6SFrancesco Dolcini return reg; 12510316c7e6SFrancesco Dolcini 12520316c7e6SFrancesco Dolcini if (!(reg & BIT(4))) 12530316c7e6SFrancesco Dolcini return 0; 12540316c7e6SFrancesco Dolcini 12550316c7e6SFrancesco Dolcini return phy_set_bits(phydev, 0x1e, BIT(9)); 12560316c7e6SFrancesco Dolcini } 12570316c7e6SFrancesco Dolcini 1258bff5b4b3SYuiko Oshino static int ksz9131_config_init(struct phy_device *phydev) 1259bff5b4b3SYuiko Oshino { 1260ce4f8afdSColin Ian King struct device_node *of_node; 1261bff5b4b3SYuiko Oshino char *clk_skews[2] = {"rxc-skew-psec", "txc-skew-psec"}; 1262bff5b4b3SYuiko Oshino char *rx_data_skews[4] = { 1263bff5b4b3SYuiko Oshino "rxd0-skew-psec", "rxd1-skew-psec", 1264bff5b4b3SYuiko Oshino "rxd2-skew-psec", "rxd3-skew-psec" 1265bff5b4b3SYuiko Oshino }; 1266bff5b4b3SYuiko Oshino char *tx_data_skews[4] = { 1267bff5b4b3SYuiko Oshino "txd0-skew-psec", "txd1-skew-psec", 1268bff5b4b3SYuiko Oshino "txd2-skew-psec", "txd3-skew-psec" 1269bff5b4b3SYuiko Oshino }; 1270bff5b4b3SYuiko Oshino char *control_skews[2] = {"txen-skew-psec", "rxdv-skew-psec"}; 1271bff5b4b3SYuiko Oshino const struct device *dev_walker; 1272bff5b4b3SYuiko Oshino int ret; 1273bff5b4b3SYuiko Oshino 1274bff5b4b3SYuiko Oshino dev_walker = &phydev->mdio.dev; 1275bff5b4b3SYuiko Oshino do { 1276bff5b4b3SYuiko Oshino of_node = dev_walker->of_node; 1277bff5b4b3SYuiko Oshino dev_walker = dev_walker->parent; 1278bff5b4b3SYuiko Oshino } while (!of_node && dev_walker); 1279bff5b4b3SYuiko Oshino 1280bff5b4b3SYuiko Oshino if (!of_node) 1281bff5b4b3SYuiko Oshino return 0; 1282bff5b4b3SYuiko Oshino 1283bd734a74SPhilippe Schenker if (phy_interface_is_rgmii(phydev)) { 1284bd734a74SPhilippe Schenker ret = ksz9131_config_rgmii_delay(phydev); 1285bd734a74SPhilippe Schenker if (ret < 0) 1286bd734a74SPhilippe Schenker return ret; 1287bd734a74SPhilippe Schenker } 1288bd734a74SPhilippe Schenker 1289bff5b4b3SYuiko Oshino ret = ksz9131_of_load_skew_values(phydev, of_node, 1290bff5b4b3SYuiko Oshino MII_KSZ9031RN_CLK_PAD_SKEW, 5, 1291bff5b4b3SYuiko Oshino clk_skews, 2); 1292bff5b4b3SYuiko Oshino if (ret < 0) 1293bff5b4b3SYuiko Oshino return ret; 1294bff5b4b3SYuiko Oshino 1295bff5b4b3SYuiko Oshino ret = ksz9131_of_load_skew_values(phydev, of_node, 1296bff5b4b3SYuiko Oshino MII_KSZ9031RN_CONTROL_PAD_SKEW, 4, 1297bff5b4b3SYuiko Oshino control_skews, 2); 1298bff5b4b3SYuiko Oshino if (ret < 0) 1299bff5b4b3SYuiko Oshino return ret; 1300bff5b4b3SYuiko Oshino 1301bff5b4b3SYuiko Oshino ret = ksz9131_of_load_skew_values(phydev, of_node, 1302bff5b4b3SYuiko Oshino MII_KSZ9031RN_RX_DATA_PAD_SKEW, 4, 1303bff5b4b3SYuiko Oshino rx_data_skews, 4); 1304bff5b4b3SYuiko Oshino if (ret < 0) 1305bff5b4b3SYuiko Oshino return ret; 1306bff5b4b3SYuiko Oshino 1307bff5b4b3SYuiko Oshino ret = ksz9131_of_load_skew_values(phydev, of_node, 1308bff5b4b3SYuiko Oshino MII_KSZ9031RN_TX_DATA_PAD_SKEW, 4, 1309bff5b4b3SYuiko Oshino tx_data_skews, 4); 1310bff5b4b3SYuiko Oshino if (ret < 0) 1311bff5b4b3SYuiko Oshino return ret; 1312bff5b4b3SYuiko Oshino 13130316c7e6SFrancesco Dolcini ret = ksz9131_led_errata(phydev); 13140316c7e6SFrancesco Dolcini if (ret < 0) 13150316c7e6SFrancesco Dolcini return ret; 13160316c7e6SFrancesco Dolcini 1317bff5b4b3SYuiko Oshino return 0; 1318bff5b4b3SYuiko Oshino } 1319bff5b4b3SYuiko Oshino 1320b64e6a87SRaju Lakkaraju #define MII_KSZ9131_AUTO_MDIX 0x1C 1321b64e6a87SRaju Lakkaraju #define MII_KSZ9131_AUTO_MDI_SET BIT(7) 1322b64e6a87SRaju Lakkaraju #define MII_KSZ9131_AUTO_MDIX_SWAP_OFF BIT(6) 1323b64e6a87SRaju Lakkaraju 1324b64e6a87SRaju Lakkaraju static int ksz9131_mdix_update(struct phy_device *phydev) 1325b64e6a87SRaju Lakkaraju { 1326b64e6a87SRaju Lakkaraju int ret; 1327b64e6a87SRaju Lakkaraju 1328b64e6a87SRaju Lakkaraju ret = phy_read(phydev, MII_KSZ9131_AUTO_MDIX); 1329b64e6a87SRaju Lakkaraju if (ret < 0) 1330b64e6a87SRaju Lakkaraju return ret; 1331b64e6a87SRaju Lakkaraju 1332b64e6a87SRaju Lakkaraju if (ret & MII_KSZ9131_AUTO_MDIX_SWAP_OFF) { 1333b64e6a87SRaju Lakkaraju if (ret & MII_KSZ9131_AUTO_MDI_SET) 1334b64e6a87SRaju Lakkaraju phydev->mdix_ctrl = ETH_TP_MDI; 1335b64e6a87SRaju Lakkaraju else 1336b64e6a87SRaju Lakkaraju phydev->mdix_ctrl = ETH_TP_MDI_X; 1337b64e6a87SRaju Lakkaraju } else { 1338b64e6a87SRaju Lakkaraju phydev->mdix_ctrl = ETH_TP_MDI_AUTO; 1339b64e6a87SRaju Lakkaraju } 1340b64e6a87SRaju Lakkaraju 1341b64e6a87SRaju Lakkaraju if (ret & MII_KSZ9131_AUTO_MDI_SET) 1342b64e6a87SRaju Lakkaraju phydev->mdix = ETH_TP_MDI; 1343b64e6a87SRaju Lakkaraju else 1344b64e6a87SRaju Lakkaraju phydev->mdix = ETH_TP_MDI_X; 1345b64e6a87SRaju Lakkaraju 1346b64e6a87SRaju Lakkaraju return 0; 1347b64e6a87SRaju Lakkaraju } 1348b64e6a87SRaju Lakkaraju 1349b64e6a87SRaju Lakkaraju static int ksz9131_config_mdix(struct phy_device *phydev, u8 ctrl) 1350b64e6a87SRaju Lakkaraju { 1351b64e6a87SRaju Lakkaraju u16 val; 1352b64e6a87SRaju Lakkaraju 1353b64e6a87SRaju Lakkaraju switch (ctrl) { 1354b64e6a87SRaju Lakkaraju case ETH_TP_MDI: 1355b64e6a87SRaju Lakkaraju val = MII_KSZ9131_AUTO_MDIX_SWAP_OFF | 1356b64e6a87SRaju Lakkaraju MII_KSZ9131_AUTO_MDI_SET; 1357b64e6a87SRaju Lakkaraju break; 1358b64e6a87SRaju Lakkaraju case ETH_TP_MDI_X: 1359b64e6a87SRaju Lakkaraju val = MII_KSZ9131_AUTO_MDIX_SWAP_OFF; 1360b64e6a87SRaju Lakkaraju break; 1361b64e6a87SRaju Lakkaraju case ETH_TP_MDI_AUTO: 1362b64e6a87SRaju Lakkaraju val = 0; 1363b64e6a87SRaju Lakkaraju break; 1364b64e6a87SRaju Lakkaraju default: 1365b64e6a87SRaju Lakkaraju return 0; 1366b64e6a87SRaju Lakkaraju } 1367b64e6a87SRaju Lakkaraju 1368b64e6a87SRaju Lakkaraju return phy_modify(phydev, MII_KSZ9131_AUTO_MDIX, 1369b64e6a87SRaju Lakkaraju MII_KSZ9131_AUTO_MDIX_SWAP_OFF | 1370b64e6a87SRaju Lakkaraju MII_KSZ9131_AUTO_MDI_SET, val); 1371b64e6a87SRaju Lakkaraju } 1372b64e6a87SRaju Lakkaraju 1373b64e6a87SRaju Lakkaraju static int ksz9131_read_status(struct phy_device *phydev) 1374b64e6a87SRaju Lakkaraju { 1375b64e6a87SRaju Lakkaraju int ret; 1376b64e6a87SRaju Lakkaraju 1377b64e6a87SRaju Lakkaraju ret = ksz9131_mdix_update(phydev); 1378b64e6a87SRaju Lakkaraju if (ret < 0) 1379b64e6a87SRaju Lakkaraju return ret; 1380b64e6a87SRaju Lakkaraju 1381b64e6a87SRaju Lakkaraju return genphy_read_status(phydev); 1382b64e6a87SRaju Lakkaraju } 1383b64e6a87SRaju Lakkaraju 1384b64e6a87SRaju Lakkaraju static int ksz9131_config_aneg(struct phy_device *phydev) 1385b64e6a87SRaju Lakkaraju { 1386b64e6a87SRaju Lakkaraju int ret; 1387b64e6a87SRaju Lakkaraju 1388b64e6a87SRaju Lakkaraju ret = ksz9131_config_mdix(phydev, phydev->mdix_ctrl); 1389b64e6a87SRaju Lakkaraju if (ret) 1390b64e6a87SRaju Lakkaraju return ret; 1391b64e6a87SRaju Lakkaraju 1392b64e6a87SRaju Lakkaraju return genphy_config_aneg(phydev); 1393b64e6a87SRaju Lakkaraju } 1394b64e6a87SRaju Lakkaraju 139548fb1994SOleksij Rempel static int ksz9477_get_features(struct phy_device *phydev) 139648fb1994SOleksij Rempel { 139748fb1994SOleksij Rempel int ret; 139848fb1994SOleksij Rempel 139948fb1994SOleksij Rempel ret = genphy_read_abilities(phydev); 140048fb1994SOleksij Rempel if (ret) 140148fb1994SOleksij Rempel return ret; 140248fb1994SOleksij Rempel 140348fb1994SOleksij Rempel /* The "EEE control and capability 1" (Register 3.20) seems to be 140448fb1994SOleksij Rempel * influenced by the "EEE advertisement 1" (Register 7.60). Changes 140548fb1994SOleksij Rempel * on the 7.60 will affect 3.20. So, we need to construct our own list 140648fb1994SOleksij Rempel * of caps. 140748fb1994SOleksij Rempel * KSZ8563R should have 100BaseTX/Full only. 140848fb1994SOleksij Rempel */ 140948fb1994SOleksij Rempel linkmode_and(phydev->supported_eee, phydev->supported, 141048fb1994SOleksij Rempel PHY_EEE_CAP1_FEATURES); 141148fb1994SOleksij Rempel 141248fb1994SOleksij Rempel return 0; 141348fb1994SOleksij Rempel } 141448fb1994SOleksij Rempel 141593272e07SJean-Christophe PLAGNIOL-VILLARD #define KSZ8873MLL_GLOBAL_CONTROL_4 0x06 141600aee095SJohan Hovold #define KSZ8873MLL_GLOBAL_CONTROL_4_DUPLEX BIT(6) 141700aee095SJohan Hovold #define KSZ8873MLL_GLOBAL_CONTROL_4_SPEED BIT(4) 141832d73b14SJingoo Han static int ksz8873mll_read_status(struct phy_device *phydev) 141993272e07SJean-Christophe PLAGNIOL-VILLARD { 142093272e07SJean-Christophe PLAGNIOL-VILLARD int regval; 142193272e07SJean-Christophe PLAGNIOL-VILLARD 142293272e07SJean-Christophe PLAGNIOL-VILLARD /* dummy read */ 142393272e07SJean-Christophe PLAGNIOL-VILLARD regval = phy_read(phydev, KSZ8873MLL_GLOBAL_CONTROL_4); 142493272e07SJean-Christophe PLAGNIOL-VILLARD 142593272e07SJean-Christophe PLAGNIOL-VILLARD regval = phy_read(phydev, KSZ8873MLL_GLOBAL_CONTROL_4); 142693272e07SJean-Christophe PLAGNIOL-VILLARD 142793272e07SJean-Christophe PLAGNIOL-VILLARD if (regval & KSZ8873MLL_GLOBAL_CONTROL_4_DUPLEX) 142893272e07SJean-Christophe PLAGNIOL-VILLARD phydev->duplex = DUPLEX_HALF; 142993272e07SJean-Christophe PLAGNIOL-VILLARD else 143093272e07SJean-Christophe PLAGNIOL-VILLARD phydev->duplex = DUPLEX_FULL; 143193272e07SJean-Christophe PLAGNIOL-VILLARD 143293272e07SJean-Christophe PLAGNIOL-VILLARD if (regval & KSZ8873MLL_GLOBAL_CONTROL_4_SPEED) 143393272e07SJean-Christophe PLAGNIOL-VILLARD phydev->speed = SPEED_10; 143493272e07SJean-Christophe PLAGNIOL-VILLARD else 143593272e07SJean-Christophe PLAGNIOL-VILLARD phydev->speed = SPEED_100; 143693272e07SJean-Christophe PLAGNIOL-VILLARD 143793272e07SJean-Christophe PLAGNIOL-VILLARD phydev->link = 1; 143893272e07SJean-Christophe PLAGNIOL-VILLARD phydev->pause = phydev->asym_pause = 0; 143993272e07SJean-Christophe PLAGNIOL-VILLARD 144093272e07SJean-Christophe PLAGNIOL-VILLARD return 0; 144193272e07SJean-Christophe PLAGNIOL-VILLARD } 144293272e07SJean-Christophe PLAGNIOL-VILLARD 14433aed3e2aSAntoine Tenart static int ksz9031_get_features(struct phy_device *phydev) 14443aed3e2aSAntoine Tenart { 14453aed3e2aSAntoine Tenart int ret; 14463aed3e2aSAntoine Tenart 14473aed3e2aSAntoine Tenart ret = genphy_read_abilities(phydev); 14483aed3e2aSAntoine Tenart if (ret < 0) 14493aed3e2aSAntoine Tenart return ret; 14503aed3e2aSAntoine Tenart 14513aed3e2aSAntoine Tenart /* Silicon Errata Sheet (DS80000691D or DS80000692D): 14523aed3e2aSAntoine Tenart * Whenever the device's Asymmetric Pause capability is set to 1, 14533aed3e2aSAntoine Tenart * link-up may fail after a link-up to link-down transition. 14543aed3e2aSAntoine Tenart * 1455407d8098SHans Andersson * The Errata Sheet is for ksz9031, but ksz9021 has the same issue 1456407d8098SHans Andersson * 14573aed3e2aSAntoine Tenart * Workaround: 14583aed3e2aSAntoine Tenart * Do not enable the Asymmetric Pause capability bit. 14593aed3e2aSAntoine Tenart */ 14603aed3e2aSAntoine Tenart linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported); 14613aed3e2aSAntoine Tenart 14623aed3e2aSAntoine Tenart /* We force setting the Pause capability as the core will force the 14633aed3e2aSAntoine Tenart * Asymmetric Pause capability to 1 otherwise. 14643aed3e2aSAntoine Tenart */ 14653aed3e2aSAntoine Tenart linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported); 14663aed3e2aSAntoine Tenart 14673aed3e2aSAntoine Tenart return 0; 14683aed3e2aSAntoine Tenart } 14693aed3e2aSAntoine Tenart 1470d2fd719bSNathan Sullivan static int ksz9031_read_status(struct phy_device *phydev) 1471d2fd719bSNathan Sullivan { 1472d2fd719bSNathan Sullivan int err; 1473d2fd719bSNathan Sullivan int regval; 1474d2fd719bSNathan Sullivan 1475d2fd719bSNathan Sullivan err = genphy_read_status(phydev); 1476d2fd719bSNathan Sullivan if (err) 1477d2fd719bSNathan Sullivan return err; 1478d2fd719bSNathan Sullivan 1479d2fd719bSNathan Sullivan /* Make sure the PHY is not broken. Read idle error count, 1480d2fd719bSNathan Sullivan * and reset the PHY if it is maxed out. 1481d2fd719bSNathan Sullivan */ 1482d2fd719bSNathan Sullivan regval = phy_read(phydev, MII_STAT1000); 1483d2fd719bSNathan Sullivan if ((regval & 0xFF) == 0xFF) { 1484d2fd719bSNathan Sullivan phy_init_hw(phydev); 1485d2fd719bSNathan Sullivan phydev->link = 0; 1486b866203dSZach Brown if (phydev->drv->config_intr && phy_interrupt_is_valid(phydev)) 1487b866203dSZach Brown phydev->drv->config_intr(phydev); 1488c1a8d0a3SGrygorii Strashko return genphy_config_aneg(phydev); 1489d2fd719bSNathan Sullivan } 1490d2fd719bSNathan Sullivan 1491d2fd719bSNathan Sullivan return 0; 1492d2fd719bSNathan Sullivan } 1493d2fd719bSNathan Sullivan 149458389c00SMarek Vasut static int ksz9x31_cable_test_start(struct phy_device *phydev) 149558389c00SMarek Vasut { 149658389c00SMarek Vasut struct kszphy_priv *priv = phydev->priv; 149758389c00SMarek Vasut int ret; 149858389c00SMarek Vasut 149958389c00SMarek Vasut /* KSZ9131RNX, DS00002841B-page 38, 4.14 LinkMD (R) Cable Diagnostic 150058389c00SMarek Vasut * Prior to running the cable diagnostics, Auto-negotiation should 150158389c00SMarek Vasut * be disabled, full duplex set and the link speed set to 1000Mbps 150258389c00SMarek Vasut * via the Basic Control Register. 150358389c00SMarek Vasut */ 150458389c00SMarek Vasut ret = phy_modify(phydev, MII_BMCR, 150558389c00SMarek Vasut BMCR_SPEED1000 | BMCR_FULLDPLX | 150658389c00SMarek Vasut BMCR_ANENABLE | BMCR_SPEED100, 150758389c00SMarek Vasut BMCR_SPEED1000 | BMCR_FULLDPLX); 150858389c00SMarek Vasut if (ret) 150958389c00SMarek Vasut return ret; 151058389c00SMarek Vasut 151158389c00SMarek Vasut /* KSZ9131RNX, DS00002841B-page 38, 4.14 LinkMD (R) Cable Diagnostic 151258389c00SMarek Vasut * The Master-Slave configuration should be set to Slave by writing 151358389c00SMarek Vasut * a value of 0x1000 to the Auto-Negotiation Master Slave Control 151458389c00SMarek Vasut * Register. 151558389c00SMarek Vasut */ 151658389c00SMarek Vasut ret = phy_read(phydev, MII_CTRL1000); 151758389c00SMarek Vasut if (ret < 0) 151858389c00SMarek Vasut return ret; 151958389c00SMarek Vasut 152058389c00SMarek Vasut /* Cache these bits, they need to be restored once LinkMD finishes. */ 152158389c00SMarek Vasut priv->vct_ctrl1000 = ret & (CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER); 152258389c00SMarek Vasut ret &= ~(CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER); 152358389c00SMarek Vasut ret |= CTL1000_ENABLE_MASTER; 152458389c00SMarek Vasut 152558389c00SMarek Vasut return phy_write(phydev, MII_CTRL1000, ret); 152658389c00SMarek Vasut } 152758389c00SMarek Vasut 152858389c00SMarek Vasut static int ksz9x31_cable_test_result_trans(u16 status) 152958389c00SMarek Vasut { 153058389c00SMarek Vasut switch (FIELD_GET(KSZ9x31_LMD_VCT_ST_MASK, status)) { 153158389c00SMarek Vasut case KSZ9x31_LMD_VCT_ST_NORMAL: 153258389c00SMarek Vasut return ETHTOOL_A_CABLE_RESULT_CODE_OK; 153358389c00SMarek Vasut case KSZ9x31_LMD_VCT_ST_OPEN: 153458389c00SMarek Vasut return ETHTOOL_A_CABLE_RESULT_CODE_OPEN; 153558389c00SMarek Vasut case KSZ9x31_LMD_VCT_ST_SHORT: 153658389c00SMarek Vasut return ETHTOOL_A_CABLE_RESULT_CODE_SAME_SHORT; 153758389c00SMarek Vasut case KSZ9x31_LMD_VCT_ST_FAIL: 153858389c00SMarek Vasut fallthrough; 153958389c00SMarek Vasut default: 154058389c00SMarek Vasut return ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC; 154158389c00SMarek Vasut } 154258389c00SMarek Vasut } 154358389c00SMarek Vasut 154458389c00SMarek Vasut static bool ksz9x31_cable_test_failed(u16 status) 154558389c00SMarek Vasut { 154658389c00SMarek Vasut int stat = FIELD_GET(KSZ9x31_LMD_VCT_ST_MASK, status); 154758389c00SMarek Vasut 154858389c00SMarek Vasut return stat == KSZ9x31_LMD_VCT_ST_FAIL; 154958389c00SMarek Vasut } 155058389c00SMarek Vasut 155158389c00SMarek Vasut static bool ksz9x31_cable_test_fault_length_valid(u16 status) 155258389c00SMarek Vasut { 155358389c00SMarek Vasut switch (FIELD_GET(KSZ9x31_LMD_VCT_ST_MASK, status)) { 155458389c00SMarek Vasut case KSZ9x31_LMD_VCT_ST_OPEN: 155558389c00SMarek Vasut fallthrough; 155658389c00SMarek Vasut case KSZ9x31_LMD_VCT_ST_SHORT: 155758389c00SMarek Vasut return true; 155858389c00SMarek Vasut } 155958389c00SMarek Vasut return false; 156058389c00SMarek Vasut } 156158389c00SMarek Vasut 156258389c00SMarek Vasut static int ksz9x31_cable_test_fault_length(struct phy_device *phydev, u16 stat) 156358389c00SMarek Vasut { 156458389c00SMarek Vasut int dt = FIELD_GET(KSZ9x31_LMD_VCT_DATA_MASK, stat); 156558389c00SMarek Vasut 156658389c00SMarek Vasut /* KSZ9131RNX, DS00002841B-page 38, 4.14 LinkMD (R) Cable Diagnostic 156758389c00SMarek Vasut * 156858389c00SMarek Vasut * distance to fault = (VCT_DATA - 22) * 4 / cable propagation velocity 156958389c00SMarek Vasut */ 157058389c00SMarek Vasut if ((phydev->phy_id & MICREL_PHY_ID_MASK) == PHY_ID_KSZ9131) 157158389c00SMarek Vasut dt = clamp(dt - 22, 0, 255); 157258389c00SMarek Vasut 157358389c00SMarek Vasut return (dt * 400) / 10; 157458389c00SMarek Vasut } 157558389c00SMarek Vasut 157658389c00SMarek Vasut static int ksz9x31_cable_test_wait_for_completion(struct phy_device *phydev) 157758389c00SMarek Vasut { 157858389c00SMarek Vasut int val, ret; 157958389c00SMarek Vasut 158058389c00SMarek Vasut ret = phy_read_poll_timeout(phydev, KSZ9x31_LMD, val, 158158389c00SMarek Vasut !(val & KSZ9x31_LMD_VCT_EN), 158258389c00SMarek Vasut 30000, 100000, true); 158358389c00SMarek Vasut 158458389c00SMarek Vasut return ret < 0 ? ret : 0; 158558389c00SMarek Vasut } 158658389c00SMarek Vasut 158758389c00SMarek Vasut static int ksz9x31_cable_test_get_pair(int pair) 158858389c00SMarek Vasut { 158958389c00SMarek Vasut static const int ethtool_pair[] = { 159058389c00SMarek Vasut ETHTOOL_A_CABLE_PAIR_A, 159158389c00SMarek Vasut ETHTOOL_A_CABLE_PAIR_B, 159258389c00SMarek Vasut ETHTOOL_A_CABLE_PAIR_C, 159358389c00SMarek Vasut ETHTOOL_A_CABLE_PAIR_D, 159458389c00SMarek Vasut }; 159558389c00SMarek Vasut 159658389c00SMarek Vasut return ethtool_pair[pair]; 159758389c00SMarek Vasut } 159858389c00SMarek Vasut 159958389c00SMarek Vasut static int ksz9x31_cable_test_one_pair(struct phy_device *phydev, int pair) 160058389c00SMarek Vasut { 160158389c00SMarek Vasut int ret, val; 160258389c00SMarek Vasut 160358389c00SMarek Vasut /* KSZ9131RNX, DS00002841B-page 38, 4.14 LinkMD (R) Cable Diagnostic 160458389c00SMarek Vasut * To test each individual cable pair, set the cable pair in the Cable 160558389c00SMarek Vasut * Diagnostics Test Pair (VCT_PAIR[1:0]) field of the LinkMD Cable 160658389c00SMarek Vasut * Diagnostic Register, along with setting the Cable Diagnostics Test 160758389c00SMarek Vasut * Enable (VCT_EN) bit. The Cable Diagnostics Test Enable (VCT_EN) bit 160858389c00SMarek Vasut * will self clear when the test is concluded. 160958389c00SMarek Vasut */ 161058389c00SMarek Vasut ret = phy_write(phydev, KSZ9x31_LMD, 161158389c00SMarek Vasut KSZ9x31_LMD_VCT_EN | KSZ9x31_LMD_VCT_PAIR(pair)); 161258389c00SMarek Vasut if (ret) 161358389c00SMarek Vasut return ret; 161458389c00SMarek Vasut 161558389c00SMarek Vasut ret = ksz9x31_cable_test_wait_for_completion(phydev); 161658389c00SMarek Vasut if (ret) 161758389c00SMarek Vasut return ret; 161858389c00SMarek Vasut 161958389c00SMarek Vasut val = phy_read(phydev, KSZ9x31_LMD); 162058389c00SMarek Vasut if (val < 0) 162158389c00SMarek Vasut return val; 162258389c00SMarek Vasut 162358389c00SMarek Vasut if (ksz9x31_cable_test_failed(val)) 162458389c00SMarek Vasut return -EAGAIN; 162558389c00SMarek Vasut 162658389c00SMarek Vasut ret = ethnl_cable_test_result(phydev, 162758389c00SMarek Vasut ksz9x31_cable_test_get_pair(pair), 162858389c00SMarek Vasut ksz9x31_cable_test_result_trans(val)); 162958389c00SMarek Vasut if (ret) 163058389c00SMarek Vasut return ret; 163158389c00SMarek Vasut 163258389c00SMarek Vasut if (!ksz9x31_cable_test_fault_length_valid(val)) 163358389c00SMarek Vasut return 0; 163458389c00SMarek Vasut 163558389c00SMarek Vasut return ethnl_cable_test_fault_length(phydev, 163658389c00SMarek Vasut ksz9x31_cable_test_get_pair(pair), 163758389c00SMarek Vasut ksz9x31_cable_test_fault_length(phydev, val)); 163858389c00SMarek Vasut } 163958389c00SMarek Vasut 164058389c00SMarek Vasut static int ksz9x31_cable_test_get_status(struct phy_device *phydev, 164158389c00SMarek Vasut bool *finished) 164258389c00SMarek Vasut { 164358389c00SMarek Vasut struct kszphy_priv *priv = phydev->priv; 164458389c00SMarek Vasut unsigned long pair_mask = 0xf; 164558389c00SMarek Vasut int retries = 20; 164658389c00SMarek Vasut int pair, ret, rv; 164758389c00SMarek Vasut 164858389c00SMarek Vasut *finished = false; 164958389c00SMarek Vasut 165058389c00SMarek Vasut /* Try harder if link partner is active */ 165158389c00SMarek Vasut while (pair_mask && retries--) { 165258389c00SMarek Vasut for_each_set_bit(pair, &pair_mask, 4) { 165358389c00SMarek Vasut ret = ksz9x31_cable_test_one_pair(phydev, pair); 165458389c00SMarek Vasut if (ret == -EAGAIN) 165558389c00SMarek Vasut continue; 165658389c00SMarek Vasut if (ret < 0) 165758389c00SMarek Vasut return ret; 165858389c00SMarek Vasut clear_bit(pair, &pair_mask); 165958389c00SMarek Vasut } 166058389c00SMarek Vasut /* If link partner is in autonegotiation mode it will send 2ms 166158389c00SMarek Vasut * of FLPs with at least 6ms of silence. 166258389c00SMarek Vasut * Add 2ms sleep to have better chances to hit this silence. 166358389c00SMarek Vasut */ 166458389c00SMarek Vasut if (pair_mask) 166558389c00SMarek Vasut usleep_range(2000, 3000); 166658389c00SMarek Vasut } 166758389c00SMarek Vasut 166858389c00SMarek Vasut /* Report remaining unfinished pair result as unknown. */ 166958389c00SMarek Vasut for_each_set_bit(pair, &pair_mask, 4) { 167058389c00SMarek Vasut ret = ethnl_cable_test_result(phydev, 167158389c00SMarek Vasut ksz9x31_cable_test_get_pair(pair), 167258389c00SMarek Vasut ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC); 167358389c00SMarek Vasut } 167458389c00SMarek Vasut 167558389c00SMarek Vasut *finished = true; 167658389c00SMarek Vasut 167758389c00SMarek Vasut /* Restore cached bits from before LinkMD got started. */ 167858389c00SMarek Vasut rv = phy_modify(phydev, MII_CTRL1000, 167958389c00SMarek Vasut CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER, 168058389c00SMarek Vasut priv->vct_ctrl1000); 168158389c00SMarek Vasut if (rv) 168258389c00SMarek Vasut return rv; 168358389c00SMarek Vasut 168458389c00SMarek Vasut return ret; 168558389c00SMarek Vasut } 168658389c00SMarek Vasut 168793272e07SJean-Christophe PLAGNIOL-VILLARD static int ksz8873mll_config_aneg(struct phy_device *phydev) 168893272e07SJean-Christophe PLAGNIOL-VILLARD { 168993272e07SJean-Christophe PLAGNIOL-VILLARD return 0; 169093272e07SJean-Christophe PLAGNIOL-VILLARD } 169193272e07SJean-Christophe PLAGNIOL-VILLARD 169252939393SOleksij Rempel static int ksz886x_config_mdix(struct phy_device *phydev, u8 ctrl) 169352939393SOleksij Rempel { 169452939393SOleksij Rempel u16 val; 169552939393SOleksij Rempel 169652939393SOleksij Rempel switch (ctrl) { 169752939393SOleksij Rempel case ETH_TP_MDI: 169852939393SOleksij Rempel val = KSZ886X_BMCR_DISABLE_AUTO_MDIX; 169952939393SOleksij Rempel break; 170052939393SOleksij Rempel case ETH_TP_MDI_X: 170152939393SOleksij Rempel /* Note: The naming of the bit KSZ886X_BMCR_FORCE_MDI is bit 170252939393SOleksij Rempel * counter intuitive, the "-X" in "1 = Force MDI" in the data 170352939393SOleksij Rempel * sheet seems to be missing: 170452939393SOleksij Rempel * 1 = Force MDI (sic!) (transmit on RX+/RX- pins) 170552939393SOleksij Rempel * 0 = Normal operation (transmit on TX+/TX- pins) 170652939393SOleksij Rempel */ 170752939393SOleksij Rempel val = KSZ886X_BMCR_DISABLE_AUTO_MDIX | KSZ886X_BMCR_FORCE_MDI; 170852939393SOleksij Rempel break; 170952939393SOleksij Rempel case ETH_TP_MDI_AUTO: 171052939393SOleksij Rempel val = 0; 171152939393SOleksij Rempel break; 171252939393SOleksij Rempel default: 171352939393SOleksij Rempel return 0; 171452939393SOleksij Rempel } 171552939393SOleksij Rempel 171652939393SOleksij Rempel return phy_modify(phydev, MII_BMCR, 171752939393SOleksij Rempel KSZ886X_BMCR_HP_MDIX | KSZ886X_BMCR_FORCE_MDI | 171852939393SOleksij Rempel KSZ886X_BMCR_DISABLE_AUTO_MDIX, 171952939393SOleksij Rempel KSZ886X_BMCR_HP_MDIX | val); 172052939393SOleksij Rempel } 172152939393SOleksij Rempel 172252939393SOleksij Rempel static int ksz886x_config_aneg(struct phy_device *phydev) 172352939393SOleksij Rempel { 172452939393SOleksij Rempel int ret; 172552939393SOleksij Rempel 172652939393SOleksij Rempel ret = genphy_config_aneg(phydev); 172752939393SOleksij Rempel if (ret) 172852939393SOleksij Rempel return ret; 172952939393SOleksij Rempel 173052939393SOleksij Rempel /* The MDI-X configuration is automatically changed by the PHY after 173152939393SOleksij Rempel * switching from autoneg off to on. So, take MDI-X configuration under 173252939393SOleksij Rempel * own control and set it after autoneg configuration was done. 173352939393SOleksij Rempel */ 173452939393SOleksij Rempel return ksz886x_config_mdix(phydev, phydev->mdix_ctrl); 173552939393SOleksij Rempel } 173652939393SOleksij Rempel 173752939393SOleksij Rempel static int ksz886x_mdix_update(struct phy_device *phydev) 173852939393SOleksij Rempel { 173952939393SOleksij Rempel int ret; 174052939393SOleksij Rempel 174152939393SOleksij Rempel ret = phy_read(phydev, MII_BMCR); 174252939393SOleksij Rempel if (ret < 0) 174352939393SOleksij Rempel return ret; 174452939393SOleksij Rempel 174552939393SOleksij Rempel if (ret & KSZ886X_BMCR_DISABLE_AUTO_MDIX) { 174652939393SOleksij Rempel if (ret & KSZ886X_BMCR_FORCE_MDI) 174752939393SOleksij Rempel phydev->mdix_ctrl = ETH_TP_MDI_X; 174852939393SOleksij Rempel else 174952939393SOleksij Rempel phydev->mdix_ctrl = ETH_TP_MDI; 175052939393SOleksij Rempel } else { 175152939393SOleksij Rempel phydev->mdix_ctrl = ETH_TP_MDI_AUTO; 175252939393SOleksij Rempel } 175352939393SOleksij Rempel 175452939393SOleksij Rempel ret = phy_read(phydev, MII_KSZPHY_CTRL); 175552939393SOleksij Rempel if (ret < 0) 175652939393SOleksij Rempel return ret; 175752939393SOleksij Rempel 175852939393SOleksij Rempel /* Same reverse logic as KSZ886X_BMCR_FORCE_MDI */ 175952939393SOleksij Rempel if (ret & KSZ886X_CTRL_MDIX_STAT) 176052939393SOleksij Rempel phydev->mdix = ETH_TP_MDI_X; 176152939393SOleksij Rempel else 176252939393SOleksij Rempel phydev->mdix = ETH_TP_MDI; 176352939393SOleksij Rempel 176452939393SOleksij Rempel return 0; 176552939393SOleksij Rempel } 176652939393SOleksij Rempel 176752939393SOleksij Rempel static int ksz886x_read_status(struct phy_device *phydev) 176852939393SOleksij Rempel { 176952939393SOleksij Rempel int ret; 177052939393SOleksij Rempel 177152939393SOleksij Rempel ret = ksz886x_mdix_update(phydev); 177252939393SOleksij Rempel if (ret < 0) 177352939393SOleksij Rempel return ret; 177452939393SOleksij Rempel 177552939393SOleksij Rempel return genphy_read_status(phydev); 177652939393SOleksij Rempel } 177752939393SOleksij Rempel 17782b2427d0SAndrew Lunn static int kszphy_get_sset_count(struct phy_device *phydev) 17792b2427d0SAndrew Lunn { 17802b2427d0SAndrew Lunn return ARRAY_SIZE(kszphy_hw_stats); 17812b2427d0SAndrew Lunn } 17822b2427d0SAndrew Lunn 17832b2427d0SAndrew Lunn static void kszphy_get_strings(struct phy_device *phydev, u8 *data) 17842b2427d0SAndrew Lunn { 17852b2427d0SAndrew Lunn int i; 17862b2427d0SAndrew Lunn 17872b2427d0SAndrew Lunn for (i = 0; i < ARRAY_SIZE(kszphy_hw_stats); i++) { 1788fb3ceec1SWolfram Sang strscpy(data + i * ETH_GSTRING_LEN, 17892b2427d0SAndrew Lunn kszphy_hw_stats[i].string, ETH_GSTRING_LEN); 17902b2427d0SAndrew Lunn } 17912b2427d0SAndrew Lunn } 17922b2427d0SAndrew Lunn 17932b2427d0SAndrew Lunn static u64 kszphy_get_stat(struct phy_device *phydev, int i) 17942b2427d0SAndrew Lunn { 17952b2427d0SAndrew Lunn struct kszphy_hw_stat stat = kszphy_hw_stats[i]; 17962b2427d0SAndrew Lunn struct kszphy_priv *priv = phydev->priv; 1797321b4d4bSAndrew Lunn int val; 1798321b4d4bSAndrew Lunn u64 ret; 17992b2427d0SAndrew Lunn 18002b2427d0SAndrew Lunn val = phy_read(phydev, stat.reg); 18012b2427d0SAndrew Lunn if (val < 0) { 18026c3442f5SJisheng Zhang ret = U64_MAX; 18032b2427d0SAndrew Lunn } else { 18042b2427d0SAndrew Lunn val = val & ((1 << stat.bits) - 1); 18052b2427d0SAndrew Lunn priv->stats[i] += val; 1806321b4d4bSAndrew Lunn ret = priv->stats[i]; 18072b2427d0SAndrew Lunn } 18082b2427d0SAndrew Lunn 1809321b4d4bSAndrew Lunn return ret; 18102b2427d0SAndrew Lunn } 18112b2427d0SAndrew Lunn 18122b2427d0SAndrew Lunn static void kszphy_get_stats(struct phy_device *phydev, 18132b2427d0SAndrew Lunn struct ethtool_stats *stats, u64 *data) 18142b2427d0SAndrew Lunn { 18152b2427d0SAndrew Lunn int i; 18162b2427d0SAndrew Lunn 18172b2427d0SAndrew Lunn for (i = 0; i < ARRAY_SIZE(kszphy_hw_stats); i++) 18182b2427d0SAndrew Lunn data[i] = kszphy_get_stat(phydev, i); 18192b2427d0SAndrew Lunn } 18202b2427d0SAndrew Lunn 1821836384d2SWenyou Yang static int kszphy_suspend(struct phy_device *phydev) 1822836384d2SWenyou Yang { 1823836384d2SWenyou Yang /* Disable PHY Interrupts */ 1824836384d2SWenyou Yang if (phy_interrupt_is_valid(phydev)) { 1825836384d2SWenyou Yang phydev->interrupts = PHY_INTERRUPT_DISABLED; 1826836384d2SWenyou Yang if (phydev->drv->config_intr) 1827836384d2SWenyou Yang phydev->drv->config_intr(phydev); 1828836384d2SWenyou Yang } 1829836384d2SWenyou Yang 1830836384d2SWenyou Yang return genphy_suspend(phydev); 1831836384d2SWenyou Yang } 1832836384d2SWenyou Yang 1833a516b7f7SDivya Koppera static void kszphy_parse_led_mode(struct phy_device *phydev) 1834a516b7f7SDivya Koppera { 1835a516b7f7SDivya Koppera const struct kszphy_type *type = phydev->drv->driver_data; 1836a516b7f7SDivya Koppera const struct device_node *np = phydev->mdio.dev.of_node; 1837a516b7f7SDivya Koppera struct kszphy_priv *priv = phydev->priv; 1838a516b7f7SDivya Koppera int ret; 1839a516b7f7SDivya Koppera 1840a516b7f7SDivya Koppera if (type && type->led_mode_reg) { 1841a516b7f7SDivya Koppera ret = of_property_read_u32(np, "micrel,led-mode", 1842a516b7f7SDivya Koppera &priv->led_mode); 1843a516b7f7SDivya Koppera 1844a516b7f7SDivya Koppera if (ret) 1845a516b7f7SDivya Koppera priv->led_mode = -1; 1846a516b7f7SDivya Koppera 1847a516b7f7SDivya Koppera if (priv->led_mode > 3) { 1848a516b7f7SDivya Koppera phydev_err(phydev, "invalid led mode: 0x%02x\n", 1849a516b7f7SDivya Koppera priv->led_mode); 1850a516b7f7SDivya Koppera priv->led_mode = -1; 1851a516b7f7SDivya Koppera } 1852a516b7f7SDivya Koppera } else { 1853a516b7f7SDivya Koppera priv->led_mode = -1; 1854a516b7f7SDivya Koppera } 1855a516b7f7SDivya Koppera } 1856a516b7f7SDivya Koppera 1857f5aba91dSAlexandre Belloni static int kszphy_resume(struct phy_device *phydev) 1858f5aba91dSAlexandre Belloni { 185979e498a9SLeonard Crestez int ret; 186079e498a9SLeonard Crestez 1861836384d2SWenyou Yang genphy_resume(phydev); 1862f5aba91dSAlexandre Belloni 18636110dff7SOleksij Rempel /* After switching from power-down to normal mode, an internal global 18646110dff7SOleksij Rempel * reset is automatically generated. Wait a minimum of 1 ms before 18656110dff7SOleksij Rempel * read/write access to the PHY registers. 18666110dff7SOleksij Rempel */ 18676110dff7SOleksij Rempel usleep_range(1000, 2000); 18686110dff7SOleksij Rempel 186979e498a9SLeonard Crestez ret = kszphy_config_reset(phydev); 187079e498a9SLeonard Crestez if (ret) 187179e498a9SLeonard Crestez return ret; 187279e498a9SLeonard Crestez 1873836384d2SWenyou Yang /* Enable PHY Interrupts */ 1874836384d2SWenyou Yang if (phy_interrupt_is_valid(phydev)) { 1875836384d2SWenyou Yang phydev->interrupts = PHY_INTERRUPT_ENABLED; 1876836384d2SWenyou Yang if (phydev->drv->config_intr) 1877836384d2SWenyou Yang phydev->drv->config_intr(phydev); 1878836384d2SWenyou Yang } 1879f5aba91dSAlexandre Belloni 1880f5aba91dSAlexandre Belloni return 0; 1881f5aba91dSAlexandre Belloni } 1882f5aba91dSAlexandre Belloni 1883e6a423a8SJohan Hovold static int kszphy_probe(struct phy_device *phydev) 1884e6a423a8SJohan Hovold { 1885e6a423a8SJohan Hovold const struct kszphy_type *type = phydev->drv->driver_data; 1886e5a03bfdSAndrew Lunn const struct device_node *np = phydev->mdio.dev.of_node; 1887e6a423a8SJohan Hovold struct kszphy_priv *priv; 188863f44b2bSJohan Hovold struct clk *clk; 1889e6a423a8SJohan Hovold 1890e5a03bfdSAndrew Lunn priv = devm_kzalloc(&phydev->mdio.dev, sizeof(*priv), GFP_KERNEL); 1891e6a423a8SJohan Hovold if (!priv) 1892e6a423a8SJohan Hovold return -ENOMEM; 1893e6a423a8SJohan Hovold 1894e6a423a8SJohan Hovold phydev->priv = priv; 1895e6a423a8SJohan Hovold 1896e6a423a8SJohan Hovold priv->type = type; 1897e6a423a8SJohan Hovold 1898a516b7f7SDivya Koppera kszphy_parse_led_mode(phydev); 1899e7a792e9SJohan Hovold 1900e5a03bfdSAndrew Lunn clk = devm_clk_get(&phydev->mdio.dev, "rmii-ref"); 1901bced8701SNiklas Cassel /* NOTE: clk may be NULL if building without CONFIG_HAVE_CLK */ 1902bced8701SNiklas Cassel if (!IS_ERR_OR_NULL(clk)) { 19031fadee0cSSascha Hauer unsigned long rate = clk_get_rate(clk); 190486dc1342SJohan Hovold bool rmii_ref_clk_sel_25_mhz; 19051fadee0cSSascha Hauer 1906f2ef6f75SFabio Estevam if (type) 190763f44b2bSJohan Hovold priv->rmii_ref_clk_sel = type->has_rmii_ref_clk_sel; 190886dc1342SJohan Hovold rmii_ref_clk_sel_25_mhz = of_property_read_bool(np, 190986dc1342SJohan Hovold "micrel,rmii-reference-clock-select-25-mhz"); 191063f44b2bSJohan Hovold 19111fadee0cSSascha Hauer if (rate > 24500000 && rate < 25500000) { 191286dc1342SJohan Hovold priv->rmii_ref_clk_sel_val = rmii_ref_clk_sel_25_mhz; 19131fadee0cSSascha Hauer } else if (rate > 49500000 && rate < 50500000) { 191486dc1342SJohan Hovold priv->rmii_ref_clk_sel_val = !rmii_ref_clk_sel_25_mhz; 19151fadee0cSSascha Hauer } else { 191672ba48beSAndrew Lunn phydev_err(phydev, "Clock rate out of range: %ld\n", 191772ba48beSAndrew Lunn rate); 19181fadee0cSSascha Hauer return -EINVAL; 19191fadee0cSSascha Hauer } 19201fadee0cSSascha Hauer } 19211fadee0cSSascha Hauer 19224217a64eSMichael Walle if (ksz8041_fiber_mode(phydev)) 19234217a64eSMichael Walle phydev->port = PORT_FIBRE; 19244217a64eSMichael Walle 192563f44b2bSJohan Hovold /* Support legacy board-file configuration */ 192663f44b2bSJohan Hovold if (phydev->dev_flags & MICREL_PHY_50MHZ_CLK) { 192763f44b2bSJohan Hovold priv->rmii_ref_clk_sel = true; 192863f44b2bSJohan Hovold priv->rmii_ref_clk_sel_val = true; 192963f44b2bSJohan Hovold } 193063f44b2bSJohan Hovold 193163f44b2bSJohan Hovold return 0; 19321fadee0cSSascha Hauer } 19331fadee0cSSascha Hauer 193421b688daSDivya Koppera static int lan8814_cable_test_start(struct phy_device *phydev) 193521b688daSDivya Koppera { 193621b688daSDivya Koppera /* If autoneg is enabled, we won't be able to test cross pair 193721b688daSDivya Koppera * short. In this case, the PHY will "detect" a link and 193821b688daSDivya Koppera * confuse the internal state machine - disable auto neg here. 193921b688daSDivya Koppera * Set the speed to 1000mbit and full duplex. 194021b688daSDivya Koppera */ 194121b688daSDivya Koppera return phy_modify(phydev, MII_BMCR, BMCR_ANENABLE | BMCR_SPEED100, 194221b688daSDivya Koppera BMCR_SPEED1000 | BMCR_FULLDPLX); 194321b688daSDivya Koppera } 194421b688daSDivya Koppera 194549011e0cSOleksij Rempel static int ksz886x_cable_test_start(struct phy_device *phydev) 194649011e0cSOleksij Rempel { 194749011e0cSOleksij Rempel if (phydev->dev_flags & MICREL_KSZ8_P1_ERRATA) 194849011e0cSOleksij Rempel return -EOPNOTSUPP; 194949011e0cSOleksij Rempel 195049011e0cSOleksij Rempel /* If autoneg is enabled, we won't be able to test cross pair 195149011e0cSOleksij Rempel * short. In this case, the PHY will "detect" a link and 195249011e0cSOleksij Rempel * confuse the internal state machine - disable auto neg here. 195349011e0cSOleksij Rempel * If autoneg is disabled, we should set the speed to 10mbit. 195449011e0cSOleksij Rempel */ 195549011e0cSOleksij Rempel return phy_clear_bits(phydev, MII_BMCR, BMCR_ANENABLE | BMCR_SPEED100); 195649011e0cSOleksij Rempel } 195749011e0cSOleksij Rempel 1958fa182ea2SDivya Koppera static __always_inline int ksz886x_cable_test_result_trans(u16 status, u16 mask) 195949011e0cSOleksij Rempel { 196021b688daSDivya Koppera switch (FIELD_GET(mask, status)) { 196149011e0cSOleksij Rempel case KSZ8081_LMD_STAT_NORMAL: 196249011e0cSOleksij Rempel return ETHTOOL_A_CABLE_RESULT_CODE_OK; 196349011e0cSOleksij Rempel case KSZ8081_LMD_STAT_SHORT: 196449011e0cSOleksij Rempel return ETHTOOL_A_CABLE_RESULT_CODE_SAME_SHORT; 196549011e0cSOleksij Rempel case KSZ8081_LMD_STAT_OPEN: 196649011e0cSOleksij Rempel return ETHTOOL_A_CABLE_RESULT_CODE_OPEN; 196749011e0cSOleksij Rempel case KSZ8081_LMD_STAT_FAIL: 196849011e0cSOleksij Rempel fallthrough; 196949011e0cSOleksij Rempel default: 197049011e0cSOleksij Rempel return ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC; 197149011e0cSOleksij Rempel } 197249011e0cSOleksij Rempel } 197349011e0cSOleksij Rempel 1974fa182ea2SDivya Koppera static __always_inline bool ksz886x_cable_test_failed(u16 status, u16 mask) 197549011e0cSOleksij Rempel { 197621b688daSDivya Koppera return FIELD_GET(mask, status) == 197749011e0cSOleksij Rempel KSZ8081_LMD_STAT_FAIL; 197849011e0cSOleksij Rempel } 197949011e0cSOleksij Rempel 1980fa182ea2SDivya Koppera static __always_inline bool ksz886x_cable_test_fault_length_valid(u16 status, u16 mask) 198149011e0cSOleksij Rempel { 198221b688daSDivya Koppera switch (FIELD_GET(mask, status)) { 198349011e0cSOleksij Rempel case KSZ8081_LMD_STAT_OPEN: 198449011e0cSOleksij Rempel fallthrough; 198549011e0cSOleksij Rempel case KSZ8081_LMD_STAT_SHORT: 198649011e0cSOleksij Rempel return true; 198749011e0cSOleksij Rempel } 198849011e0cSOleksij Rempel return false; 198949011e0cSOleksij Rempel } 199049011e0cSOleksij Rempel 1991fa182ea2SDivya Koppera static __always_inline int ksz886x_cable_test_fault_length(struct phy_device *phydev, 1992fa182ea2SDivya Koppera u16 status, u16 data_mask) 199349011e0cSOleksij Rempel { 199449011e0cSOleksij Rempel int dt; 199549011e0cSOleksij Rempel 199649011e0cSOleksij Rempel /* According to the data sheet the distance to the fault is 199721b688daSDivya Koppera * DELTA_TIME * 0.4 meters for ksz phys. 199821b688daSDivya Koppera * (DELTA_TIME - 22) * 0.8 for lan8814 phy. 199949011e0cSOleksij Rempel */ 200021b688daSDivya Koppera dt = FIELD_GET(data_mask, status); 200149011e0cSOleksij Rempel 200221b688daSDivya Koppera if ((phydev->phy_id & MICREL_PHY_ID_MASK) == PHY_ID_LAN8814) 200321b688daSDivya Koppera return ((dt - 22) * 800) / 10; 200421b688daSDivya Koppera else 200549011e0cSOleksij Rempel return (dt * 400) / 10; 200649011e0cSOleksij Rempel } 200749011e0cSOleksij Rempel 200849011e0cSOleksij Rempel static int ksz886x_cable_test_wait_for_completion(struct phy_device *phydev) 200949011e0cSOleksij Rempel { 201021b688daSDivya Koppera const struct kszphy_type *type = phydev->drv->driver_data; 201149011e0cSOleksij Rempel int val, ret; 201249011e0cSOleksij Rempel 201321b688daSDivya Koppera ret = phy_read_poll_timeout(phydev, type->cable_diag_reg, val, 201449011e0cSOleksij Rempel !(val & KSZ8081_LMD_ENABLE_TEST), 201549011e0cSOleksij Rempel 30000, 100000, true); 201649011e0cSOleksij Rempel 201749011e0cSOleksij Rempel return ret < 0 ? ret : 0; 201849011e0cSOleksij Rempel } 201949011e0cSOleksij Rempel 202021b688daSDivya Koppera static int lan8814_cable_test_one_pair(struct phy_device *phydev, int pair) 202121b688daSDivya Koppera { 202221b688daSDivya Koppera static const int ethtool_pair[] = { ETHTOOL_A_CABLE_PAIR_A, 202321b688daSDivya Koppera ETHTOOL_A_CABLE_PAIR_B, 202421b688daSDivya Koppera ETHTOOL_A_CABLE_PAIR_C, 202521b688daSDivya Koppera ETHTOOL_A_CABLE_PAIR_D, 202621b688daSDivya Koppera }; 202721b688daSDivya Koppera u32 fault_length; 202821b688daSDivya Koppera int ret; 202921b688daSDivya Koppera int val; 203021b688daSDivya Koppera 203121b688daSDivya Koppera val = KSZ8081_LMD_ENABLE_TEST; 203221b688daSDivya Koppera val = val | (pair << LAN8814_PAIR_BIT_SHIFT); 203321b688daSDivya Koppera 203421b688daSDivya Koppera ret = phy_write(phydev, LAN8814_CABLE_DIAG, val); 203521b688daSDivya Koppera if (ret < 0) 203621b688daSDivya Koppera return ret; 203721b688daSDivya Koppera 203821b688daSDivya Koppera ret = ksz886x_cable_test_wait_for_completion(phydev); 203921b688daSDivya Koppera if (ret) 204021b688daSDivya Koppera return ret; 204121b688daSDivya Koppera 204221b688daSDivya Koppera val = phy_read(phydev, LAN8814_CABLE_DIAG); 204321b688daSDivya Koppera if (val < 0) 204421b688daSDivya Koppera return val; 204521b688daSDivya Koppera 204621b688daSDivya Koppera if (ksz886x_cable_test_failed(val, LAN8814_CABLE_DIAG_STAT_MASK)) 204721b688daSDivya Koppera return -EAGAIN; 204821b688daSDivya Koppera 204921b688daSDivya Koppera ret = ethnl_cable_test_result(phydev, ethtool_pair[pair], 205021b688daSDivya Koppera ksz886x_cable_test_result_trans(val, 205121b688daSDivya Koppera LAN8814_CABLE_DIAG_STAT_MASK 205221b688daSDivya Koppera )); 205321b688daSDivya Koppera if (ret) 205421b688daSDivya Koppera return ret; 205521b688daSDivya Koppera 205621b688daSDivya Koppera if (!ksz886x_cable_test_fault_length_valid(val, LAN8814_CABLE_DIAG_STAT_MASK)) 205721b688daSDivya Koppera return 0; 205821b688daSDivya Koppera 205921b688daSDivya Koppera fault_length = ksz886x_cable_test_fault_length(phydev, val, 206021b688daSDivya Koppera LAN8814_CABLE_DIAG_VCT_DATA_MASK); 206121b688daSDivya Koppera 206221b688daSDivya Koppera return ethnl_cable_test_fault_length(phydev, ethtool_pair[pair], fault_length); 206321b688daSDivya Koppera } 206421b688daSDivya Koppera 206549011e0cSOleksij Rempel static int ksz886x_cable_test_one_pair(struct phy_device *phydev, int pair) 206649011e0cSOleksij Rempel { 206749011e0cSOleksij Rempel static const int ethtool_pair[] = { 206849011e0cSOleksij Rempel ETHTOOL_A_CABLE_PAIR_A, 206949011e0cSOleksij Rempel ETHTOOL_A_CABLE_PAIR_B, 207049011e0cSOleksij Rempel }; 207149011e0cSOleksij Rempel int ret, val, mdix; 207221b688daSDivya Koppera u32 fault_length; 207349011e0cSOleksij Rempel 207449011e0cSOleksij Rempel /* There is no way to choice the pair, like we do one ksz9031. 207549011e0cSOleksij Rempel * We can workaround this limitation by using the MDI-X functionality. 207649011e0cSOleksij Rempel */ 207749011e0cSOleksij Rempel if (pair == 0) 207849011e0cSOleksij Rempel mdix = ETH_TP_MDI; 207949011e0cSOleksij Rempel else 208049011e0cSOleksij Rempel mdix = ETH_TP_MDI_X; 208149011e0cSOleksij Rempel 208249011e0cSOleksij Rempel switch (phydev->phy_id & MICREL_PHY_ID_MASK) { 208349011e0cSOleksij Rempel case PHY_ID_KSZ8081: 208449011e0cSOleksij Rempel ret = ksz8081_config_mdix(phydev, mdix); 208549011e0cSOleksij Rempel break; 208649011e0cSOleksij Rempel case PHY_ID_KSZ886X: 208749011e0cSOleksij Rempel ret = ksz886x_config_mdix(phydev, mdix); 208849011e0cSOleksij Rempel break; 208949011e0cSOleksij Rempel default: 209049011e0cSOleksij Rempel ret = -ENODEV; 209149011e0cSOleksij Rempel } 209249011e0cSOleksij Rempel 209349011e0cSOleksij Rempel if (ret) 209449011e0cSOleksij Rempel return ret; 209549011e0cSOleksij Rempel 209649011e0cSOleksij Rempel /* Now we are ready to fire. This command will send a 100ns pulse 209749011e0cSOleksij Rempel * to the pair. 209849011e0cSOleksij Rempel */ 209949011e0cSOleksij Rempel ret = phy_write(phydev, KSZ8081_LMD, KSZ8081_LMD_ENABLE_TEST); 210049011e0cSOleksij Rempel if (ret) 210149011e0cSOleksij Rempel return ret; 210249011e0cSOleksij Rempel 210349011e0cSOleksij Rempel ret = ksz886x_cable_test_wait_for_completion(phydev); 210449011e0cSOleksij Rempel if (ret) 210549011e0cSOleksij Rempel return ret; 210649011e0cSOleksij Rempel 210749011e0cSOleksij Rempel val = phy_read(phydev, KSZ8081_LMD); 210849011e0cSOleksij Rempel if (val < 0) 210949011e0cSOleksij Rempel return val; 211049011e0cSOleksij Rempel 211121b688daSDivya Koppera if (ksz886x_cable_test_failed(val, KSZ8081_LMD_STAT_MASK)) 211249011e0cSOleksij Rempel return -EAGAIN; 211349011e0cSOleksij Rempel 211449011e0cSOleksij Rempel ret = ethnl_cable_test_result(phydev, ethtool_pair[pair], 211521b688daSDivya Koppera ksz886x_cable_test_result_trans(val, KSZ8081_LMD_STAT_MASK)); 211649011e0cSOleksij Rempel if (ret) 211749011e0cSOleksij Rempel return ret; 211849011e0cSOleksij Rempel 211921b688daSDivya Koppera if (!ksz886x_cable_test_fault_length_valid(val, KSZ8081_LMD_STAT_MASK)) 212049011e0cSOleksij Rempel return 0; 212149011e0cSOleksij Rempel 212221b688daSDivya Koppera fault_length = ksz886x_cable_test_fault_length(phydev, val, KSZ8081_LMD_DELTA_TIME_MASK); 212321b688daSDivya Koppera 212421b688daSDivya Koppera return ethnl_cable_test_fault_length(phydev, ethtool_pair[pair], fault_length); 212549011e0cSOleksij Rempel } 212649011e0cSOleksij Rempel 212749011e0cSOleksij Rempel static int ksz886x_cable_test_get_status(struct phy_device *phydev, 212849011e0cSOleksij Rempel bool *finished) 212949011e0cSOleksij Rempel { 213021b688daSDivya Koppera const struct kszphy_type *type = phydev->drv->driver_data; 213121b688daSDivya Koppera unsigned long pair_mask = type->pair_mask; 213249011e0cSOleksij Rempel int retries = 20; 2133d50ede4fSDivya Koppera int ret = 0; 2134d50ede4fSDivya Koppera int pair; 213549011e0cSOleksij Rempel 213649011e0cSOleksij Rempel *finished = false; 213749011e0cSOleksij Rempel 213849011e0cSOleksij Rempel /* Try harder if link partner is active */ 213949011e0cSOleksij Rempel while (pair_mask && retries--) { 214049011e0cSOleksij Rempel for_each_set_bit(pair, &pair_mask, 4) { 214121b688daSDivya Koppera if (type->cable_diag_reg == LAN8814_CABLE_DIAG) 214221b688daSDivya Koppera ret = lan8814_cable_test_one_pair(phydev, pair); 214321b688daSDivya Koppera else 214449011e0cSOleksij Rempel ret = ksz886x_cable_test_one_pair(phydev, pair); 214549011e0cSOleksij Rempel if (ret == -EAGAIN) 214649011e0cSOleksij Rempel continue; 214749011e0cSOleksij Rempel if (ret < 0) 214849011e0cSOleksij Rempel return ret; 214949011e0cSOleksij Rempel clear_bit(pair, &pair_mask); 215049011e0cSOleksij Rempel } 215149011e0cSOleksij Rempel /* If link partner is in autonegotiation mode it will send 2ms 215249011e0cSOleksij Rempel * of FLPs with at least 6ms of silence. 215349011e0cSOleksij Rempel * Add 2ms sleep to have better chances to hit this silence. 215449011e0cSOleksij Rempel */ 215549011e0cSOleksij Rempel if (pair_mask) 215649011e0cSOleksij Rempel msleep(2); 215749011e0cSOleksij Rempel } 215849011e0cSOleksij Rempel 215949011e0cSOleksij Rempel *finished = true; 216049011e0cSOleksij Rempel 216149011e0cSOleksij Rempel return ret; 216249011e0cSOleksij Rempel } 216349011e0cSOleksij Rempel 21647c2dcfa2SHoratiu Vultur #define LAN_EXT_PAGE_ACCESS_CONTROL 0x16 21657c2dcfa2SHoratiu Vultur #define LAN_EXT_PAGE_ACCESS_ADDRESS_DATA 0x17 21667c2dcfa2SHoratiu Vultur #define LAN_EXT_PAGE_ACCESS_CTRL_EP_FUNC 0x4000 21677c2dcfa2SHoratiu Vultur 21687467d716SHoratiu Vultur #define LAN8814_QSGMII_SOFT_RESET 0x43 21697467d716SHoratiu Vultur #define LAN8814_QSGMII_SOFT_RESET_BIT BIT(0) 21707467d716SHoratiu Vultur #define LAN8814_QSGMII_PCS1G_ANEG_CONFIG 0x13 21717467d716SHoratiu Vultur #define LAN8814_QSGMII_PCS1G_ANEG_CONFIG_ANEG_ENA BIT(3) 21727467d716SHoratiu Vultur #define LAN8814_ALIGN_SWAP 0x4a 21737467d716SHoratiu Vultur #define LAN8814_ALIGN_TX_A_B_SWAP 0x1 21747467d716SHoratiu Vultur #define LAN8814_ALIGN_TX_A_B_SWAP_MASK GENMASK(2, 0) 21757467d716SHoratiu Vultur 21767c2dcfa2SHoratiu Vultur #define LAN8804_ALIGN_SWAP 0x4a 21777c2dcfa2SHoratiu Vultur #define LAN8804_ALIGN_TX_A_B_SWAP 0x1 21787c2dcfa2SHoratiu Vultur #define LAN8804_ALIGN_TX_A_B_SWAP_MASK GENMASK(2, 0) 21797c2dcfa2SHoratiu Vultur #define LAN8814_CLOCK_MANAGEMENT 0xd 21807c2dcfa2SHoratiu Vultur #define LAN8814_LINK_QUALITY 0x8e 21817c2dcfa2SHoratiu Vultur 21827c2dcfa2SHoratiu Vultur static int lanphy_read_page_reg(struct phy_device *phydev, int page, u32 addr) 21837c2dcfa2SHoratiu Vultur { 218412a4d677SWan Jiabing int data; 21857c2dcfa2SHoratiu Vultur 21864488f6b6SDivya Koppera phy_lock_mdio_bus(phydev); 21874488f6b6SDivya Koppera __phy_write(phydev, LAN_EXT_PAGE_ACCESS_CONTROL, page); 21884488f6b6SDivya Koppera __phy_write(phydev, LAN_EXT_PAGE_ACCESS_ADDRESS_DATA, addr); 21894488f6b6SDivya Koppera __phy_write(phydev, LAN_EXT_PAGE_ACCESS_CONTROL, 21907c2dcfa2SHoratiu Vultur (page | LAN_EXT_PAGE_ACCESS_CTRL_EP_FUNC)); 21914488f6b6SDivya Koppera data = __phy_read(phydev, LAN_EXT_PAGE_ACCESS_ADDRESS_DATA); 21924488f6b6SDivya Koppera phy_unlock_mdio_bus(phydev); 21937c2dcfa2SHoratiu Vultur 21947c2dcfa2SHoratiu Vultur return data; 21957c2dcfa2SHoratiu Vultur } 21967c2dcfa2SHoratiu Vultur 21977c2dcfa2SHoratiu Vultur static int lanphy_write_page_reg(struct phy_device *phydev, int page, u16 addr, 21987c2dcfa2SHoratiu Vultur u16 val) 21997c2dcfa2SHoratiu Vultur { 22004488f6b6SDivya Koppera phy_lock_mdio_bus(phydev); 22014488f6b6SDivya Koppera __phy_write(phydev, LAN_EXT_PAGE_ACCESS_CONTROL, page); 22024488f6b6SDivya Koppera __phy_write(phydev, LAN_EXT_PAGE_ACCESS_ADDRESS_DATA, addr); 22034488f6b6SDivya Koppera __phy_write(phydev, LAN_EXT_PAGE_ACCESS_CONTROL, 22044488f6b6SDivya Koppera page | LAN_EXT_PAGE_ACCESS_CTRL_EP_FUNC); 22057c2dcfa2SHoratiu Vultur 22064488f6b6SDivya Koppera val = __phy_write(phydev, LAN_EXT_PAGE_ACCESS_ADDRESS_DATA, val); 22074488f6b6SDivya Koppera if (val != 0) 22087c2dcfa2SHoratiu Vultur phydev_err(phydev, "Error: phy_write has returned error %d\n", 22097c2dcfa2SHoratiu Vultur val); 22104488f6b6SDivya Koppera phy_unlock_mdio_bus(phydev); 22117c2dcfa2SHoratiu Vultur return val; 22127c2dcfa2SHoratiu Vultur } 22137c2dcfa2SHoratiu Vultur 2214ece19502SDivya Koppera static int lan8814_config_ts_intr(struct phy_device *phydev, bool enable) 22157467d716SHoratiu Vultur { 2216ece19502SDivya Koppera u16 val = 0; 22177467d716SHoratiu Vultur 2218ece19502SDivya Koppera if (enable) 2219ece19502SDivya Koppera val = PTP_TSU_INT_EN_PTP_TX_TS_EN_ | 2220ece19502SDivya Koppera PTP_TSU_INT_EN_PTP_TX_TS_OVRFL_EN_ | 2221ece19502SDivya Koppera PTP_TSU_INT_EN_PTP_RX_TS_EN_ | 2222ece19502SDivya Koppera PTP_TSU_INT_EN_PTP_RX_TS_OVRFL_EN_; 22237467d716SHoratiu Vultur 2224ece19502SDivya Koppera return lanphy_write_page_reg(phydev, 5, PTP_TSU_INT_EN, val); 2225ece19502SDivya Koppera } 22267467d716SHoratiu Vultur 2227ece19502SDivya Koppera static void lan8814_ptp_rx_ts_get(struct phy_device *phydev, 2228ece19502SDivya Koppera u32 *seconds, u32 *nano_seconds, u16 *seq_id) 2229ece19502SDivya Koppera { 2230ece19502SDivya Koppera *seconds = lanphy_read_page_reg(phydev, 5, PTP_RX_INGRESS_SEC_HI); 2231ece19502SDivya Koppera *seconds = (*seconds << 16) | 2232ece19502SDivya Koppera lanphy_read_page_reg(phydev, 5, PTP_RX_INGRESS_SEC_LO); 2233ece19502SDivya Koppera 2234ece19502SDivya Koppera *nano_seconds = lanphy_read_page_reg(phydev, 5, PTP_RX_INGRESS_NS_HI); 2235ece19502SDivya Koppera *nano_seconds = ((*nano_seconds & 0x3fff) << 16) | 2236ece19502SDivya Koppera lanphy_read_page_reg(phydev, 5, PTP_RX_INGRESS_NS_LO); 2237ece19502SDivya Koppera 2238ece19502SDivya Koppera *seq_id = lanphy_read_page_reg(phydev, 5, PTP_RX_MSG_HEADER2); 2239ece19502SDivya Koppera } 2240ece19502SDivya Koppera 2241ece19502SDivya Koppera static void lan8814_ptp_tx_ts_get(struct phy_device *phydev, 2242ece19502SDivya Koppera u32 *seconds, u32 *nano_seconds, u16 *seq_id) 2243ece19502SDivya Koppera { 2244ece19502SDivya Koppera *seconds = lanphy_read_page_reg(phydev, 5, PTP_TX_EGRESS_SEC_HI); 2245ece19502SDivya Koppera *seconds = *seconds << 16 | 2246ece19502SDivya Koppera lanphy_read_page_reg(phydev, 5, PTP_TX_EGRESS_SEC_LO); 2247ece19502SDivya Koppera 2248ece19502SDivya Koppera *nano_seconds = lanphy_read_page_reg(phydev, 5, PTP_TX_EGRESS_NS_HI); 2249ece19502SDivya Koppera *nano_seconds = ((*nano_seconds & 0x3fff) << 16) | 2250ece19502SDivya Koppera lanphy_read_page_reg(phydev, 5, PTP_TX_EGRESS_NS_LO); 2251ece19502SDivya Koppera 2252ece19502SDivya Koppera *seq_id = lanphy_read_page_reg(phydev, 5, PTP_TX_MSG_HEADER2); 2253ece19502SDivya Koppera } 2254ece19502SDivya Koppera 2255ece19502SDivya Koppera static int lan8814_ts_info(struct mii_timestamper *mii_ts, struct ethtool_ts_info *info) 2256ece19502SDivya Koppera { 2257ece19502SDivya Koppera struct kszphy_ptp_priv *ptp_priv = container_of(mii_ts, struct kszphy_ptp_priv, mii_ts); 2258ece19502SDivya Koppera struct phy_device *phydev = ptp_priv->phydev; 2259ece19502SDivya Koppera struct lan8814_shared_priv *shared = phydev->shared->priv; 2260ece19502SDivya Koppera 2261ece19502SDivya Koppera info->so_timestamping = SOF_TIMESTAMPING_TX_HARDWARE | 2262ece19502SDivya Koppera SOF_TIMESTAMPING_RX_HARDWARE | 2263ece19502SDivya Koppera SOF_TIMESTAMPING_RAW_HARDWARE; 2264ece19502SDivya Koppera 2265ece19502SDivya Koppera info->phc_index = ptp_clock_index(shared->ptp_clock); 2266ece19502SDivya Koppera 2267ece19502SDivya Koppera info->tx_types = 2268ece19502SDivya Koppera (1 << HWTSTAMP_TX_OFF) | 2269ece19502SDivya Koppera (1 << HWTSTAMP_TX_ON) | 2270ece19502SDivya Koppera (1 << HWTSTAMP_TX_ONESTEP_SYNC); 2271ece19502SDivya Koppera 2272ece19502SDivya Koppera info->rx_filters = 2273ece19502SDivya Koppera (1 << HWTSTAMP_FILTER_NONE) | 2274ece19502SDivya Koppera (1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT) | 2275ece19502SDivya Koppera (1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT) | 2276ece19502SDivya Koppera (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) | 2277ece19502SDivya Koppera (1 << HWTSTAMP_FILTER_PTP_V2_EVENT); 22787467d716SHoratiu Vultur 22797467d716SHoratiu Vultur return 0; 22807467d716SHoratiu Vultur } 22817467d716SHoratiu Vultur 2282ece19502SDivya Koppera static void lan8814_flush_fifo(struct phy_device *phydev, bool egress) 2283ece19502SDivya Koppera { 2284ece19502SDivya Koppera int i; 2285ece19502SDivya Koppera 2286ece19502SDivya Koppera for (i = 0; i < FIFO_SIZE; ++i) 2287ece19502SDivya Koppera lanphy_read_page_reg(phydev, 5, 2288ece19502SDivya Koppera egress ? PTP_TX_MSG_HEADER2 : PTP_RX_MSG_HEADER2); 2289ece19502SDivya Koppera 2290ece19502SDivya Koppera /* Read to clear overflow status bit */ 2291ece19502SDivya Koppera lanphy_read_page_reg(phydev, 5, PTP_TSU_INT_STS); 2292ece19502SDivya Koppera } 2293ece19502SDivya Koppera 2294ece19502SDivya Koppera static int lan8814_hwtstamp(struct mii_timestamper *mii_ts, struct ifreq *ifr) 2295ece19502SDivya Koppera { 2296ece19502SDivya Koppera struct kszphy_ptp_priv *ptp_priv = 2297ece19502SDivya Koppera container_of(mii_ts, struct kszphy_ptp_priv, mii_ts); 2298ece19502SDivya Koppera struct phy_device *phydev = ptp_priv->phydev; 2299ece19502SDivya Koppera struct lan8814_shared_priv *shared = phydev->shared->priv; 2300ece19502SDivya Koppera struct lan8814_ptp_rx_ts *rx_ts, *tmp; 2301ece19502SDivya Koppera struct hwtstamp_config config; 2302ece19502SDivya Koppera int txcfg = 0, rxcfg = 0; 2303ece19502SDivya Koppera int pkt_ts_enable; 2304ece19502SDivya Koppera 2305ece19502SDivya Koppera if (copy_from_user(&config, ifr->ifr_data, sizeof(config))) 2306ece19502SDivya Koppera return -EFAULT; 2307ece19502SDivya Koppera 2308ece19502SDivya Koppera ptp_priv->hwts_tx_type = config.tx_type; 2309ece19502SDivya Koppera ptp_priv->rx_filter = config.rx_filter; 2310ece19502SDivya Koppera 2311ece19502SDivya Koppera switch (config.rx_filter) { 2312ece19502SDivya Koppera case HWTSTAMP_FILTER_NONE: 2313ece19502SDivya Koppera ptp_priv->layer = 0; 2314ece19502SDivya Koppera ptp_priv->version = 0; 2315ece19502SDivya Koppera break; 2316ece19502SDivya Koppera case HWTSTAMP_FILTER_PTP_V2_L4_EVENT: 2317ece19502SDivya Koppera case HWTSTAMP_FILTER_PTP_V2_L4_SYNC: 2318ece19502SDivya Koppera case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ: 2319ece19502SDivya Koppera ptp_priv->layer = PTP_CLASS_L4; 2320ece19502SDivya Koppera ptp_priv->version = PTP_CLASS_V2; 2321ece19502SDivya Koppera break; 2322ece19502SDivya Koppera case HWTSTAMP_FILTER_PTP_V2_L2_EVENT: 2323ece19502SDivya Koppera case HWTSTAMP_FILTER_PTP_V2_L2_SYNC: 2324ece19502SDivya Koppera case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ: 2325ece19502SDivya Koppera ptp_priv->layer = PTP_CLASS_L2; 2326ece19502SDivya Koppera ptp_priv->version = PTP_CLASS_V2; 2327ece19502SDivya Koppera break; 2328ece19502SDivya Koppera case HWTSTAMP_FILTER_PTP_V2_EVENT: 2329ece19502SDivya Koppera case HWTSTAMP_FILTER_PTP_V2_SYNC: 2330ece19502SDivya Koppera case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ: 2331ece19502SDivya Koppera ptp_priv->layer = PTP_CLASS_L4 | PTP_CLASS_L2; 2332ece19502SDivya Koppera ptp_priv->version = PTP_CLASS_V2; 2333ece19502SDivya Koppera break; 2334ece19502SDivya Koppera default: 2335ece19502SDivya Koppera return -ERANGE; 2336ece19502SDivya Koppera } 2337ece19502SDivya Koppera 2338ece19502SDivya Koppera if (ptp_priv->layer & PTP_CLASS_L2) { 2339ece19502SDivya Koppera rxcfg = PTP_RX_PARSE_CONFIG_LAYER2_EN_; 2340ece19502SDivya Koppera txcfg = PTP_TX_PARSE_CONFIG_LAYER2_EN_; 2341ece19502SDivya Koppera } else if (ptp_priv->layer & PTP_CLASS_L4) { 2342ece19502SDivya Koppera rxcfg |= PTP_RX_PARSE_CONFIG_IPV4_EN_ | PTP_RX_PARSE_CONFIG_IPV6_EN_; 2343ece19502SDivya Koppera txcfg |= PTP_TX_PARSE_CONFIG_IPV4_EN_ | PTP_TX_PARSE_CONFIG_IPV6_EN_; 2344ece19502SDivya Koppera } 2345ece19502SDivya Koppera lanphy_write_page_reg(ptp_priv->phydev, 5, PTP_RX_PARSE_CONFIG, rxcfg); 2346ece19502SDivya Koppera lanphy_write_page_reg(ptp_priv->phydev, 5, PTP_TX_PARSE_CONFIG, txcfg); 2347ece19502SDivya Koppera 2348ece19502SDivya Koppera pkt_ts_enable = PTP_TIMESTAMP_EN_SYNC_ | PTP_TIMESTAMP_EN_DREQ_ | 2349ece19502SDivya Koppera PTP_TIMESTAMP_EN_PDREQ_ | PTP_TIMESTAMP_EN_PDRES_; 2350ece19502SDivya Koppera lanphy_write_page_reg(ptp_priv->phydev, 5, PTP_RX_TIMESTAMP_EN, pkt_ts_enable); 2351ece19502SDivya Koppera lanphy_write_page_reg(ptp_priv->phydev, 5, PTP_TX_TIMESTAMP_EN, pkt_ts_enable); 2352ece19502SDivya Koppera 2353ece19502SDivya Koppera if (ptp_priv->hwts_tx_type == HWTSTAMP_TX_ONESTEP_SYNC) 2354ece19502SDivya Koppera lanphy_write_page_reg(ptp_priv->phydev, 5, PTP_TX_MOD, 2355ece19502SDivya Koppera PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_); 2356ece19502SDivya Koppera 2357ece19502SDivya Koppera if (config.rx_filter != HWTSTAMP_FILTER_NONE) 2358ece19502SDivya Koppera lan8814_config_ts_intr(ptp_priv->phydev, true); 2359ece19502SDivya Koppera else 2360ece19502SDivya Koppera lan8814_config_ts_intr(ptp_priv->phydev, false); 2361ece19502SDivya Koppera 2362ece19502SDivya Koppera mutex_lock(&shared->shared_lock); 2363ece19502SDivya Koppera if (config.rx_filter != HWTSTAMP_FILTER_NONE) 2364ece19502SDivya Koppera shared->ref++; 2365ece19502SDivya Koppera else 2366ece19502SDivya Koppera shared->ref--; 2367ece19502SDivya Koppera 2368ece19502SDivya Koppera if (shared->ref) 2369ece19502SDivya Koppera lanphy_write_page_reg(ptp_priv->phydev, 4, PTP_CMD_CTL, 2370ece19502SDivya Koppera PTP_CMD_CTL_PTP_ENABLE_); 2371ece19502SDivya Koppera else 2372ece19502SDivya Koppera lanphy_write_page_reg(ptp_priv->phydev, 4, PTP_CMD_CTL, 2373ece19502SDivya Koppera PTP_CMD_CTL_PTP_DISABLE_); 2374ece19502SDivya Koppera mutex_unlock(&shared->shared_lock); 2375ece19502SDivya Koppera 2376ece19502SDivya Koppera /* In case of multiple starts and stops, these needs to be cleared */ 2377ece19502SDivya Koppera list_for_each_entry_safe(rx_ts, tmp, &ptp_priv->rx_ts_list, list) { 2378ece19502SDivya Koppera list_del(&rx_ts->list); 2379ece19502SDivya Koppera kfree(rx_ts); 2380ece19502SDivya Koppera } 2381ece19502SDivya Koppera skb_queue_purge(&ptp_priv->rx_queue); 2382ece19502SDivya Koppera skb_queue_purge(&ptp_priv->tx_queue); 2383ece19502SDivya Koppera 2384ece19502SDivya Koppera lan8814_flush_fifo(ptp_priv->phydev, false); 2385ece19502SDivya Koppera lan8814_flush_fifo(ptp_priv->phydev, true); 2386ece19502SDivya Koppera 2387ece19502SDivya Koppera return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ? -EFAULT : 0; 2388ece19502SDivya Koppera } 2389ece19502SDivya Koppera 2390ece19502SDivya Koppera static void lan8814_txtstamp(struct mii_timestamper *mii_ts, 2391ece19502SDivya Koppera struct sk_buff *skb, int type) 2392ece19502SDivya Koppera { 2393ece19502SDivya Koppera struct kszphy_ptp_priv *ptp_priv = container_of(mii_ts, struct kszphy_ptp_priv, mii_ts); 2394ece19502SDivya Koppera 2395ece19502SDivya Koppera switch (ptp_priv->hwts_tx_type) { 2396ece19502SDivya Koppera case HWTSTAMP_TX_ONESTEP_SYNC: 23973914a9c0SKurt Kanzenbach if (ptp_msg_is_sync(skb, type)) { 2398ece19502SDivya Koppera kfree_skb(skb); 2399ece19502SDivya Koppera return; 2400ece19502SDivya Koppera } 2401ece19502SDivya Koppera fallthrough; 2402ece19502SDivya Koppera case HWTSTAMP_TX_ON: 2403ece19502SDivya Koppera skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; 2404ece19502SDivya Koppera skb_queue_tail(&ptp_priv->tx_queue, skb); 2405ece19502SDivya Koppera break; 2406ece19502SDivya Koppera case HWTSTAMP_TX_OFF: 2407ece19502SDivya Koppera default: 2408ece19502SDivya Koppera kfree_skb(skb); 2409ece19502SDivya Koppera break; 2410ece19502SDivya Koppera } 2411ece19502SDivya Koppera } 2412ece19502SDivya Koppera 2413ece19502SDivya Koppera static void lan8814_get_sig_rx(struct sk_buff *skb, u16 *sig) 2414ece19502SDivya Koppera { 2415ece19502SDivya Koppera struct ptp_header *ptp_header; 2416ece19502SDivya Koppera u32 type; 2417ece19502SDivya Koppera 2418ece19502SDivya Koppera skb_push(skb, ETH_HLEN); 2419ece19502SDivya Koppera type = ptp_classify_raw(skb); 2420ece19502SDivya Koppera ptp_header = ptp_parse_header(skb, type); 2421ece19502SDivya Koppera skb_pull_inline(skb, ETH_HLEN); 2422ece19502SDivya Koppera 2423ece19502SDivya Koppera *sig = (__force u16)(ntohs(ptp_header->sequence_id)); 2424ece19502SDivya Koppera } 2425ece19502SDivya Koppera 2426cafc3662SHoratiu Vultur static bool lan8814_match_rx_skb(struct kszphy_ptp_priv *ptp_priv, 2427ece19502SDivya Koppera struct sk_buff *skb) 2428ece19502SDivya Koppera { 2429ece19502SDivya Koppera struct skb_shared_hwtstamps *shhwtstamps; 2430ece19502SDivya Koppera struct lan8814_ptp_rx_ts *rx_ts, *tmp; 2431ece19502SDivya Koppera unsigned long flags; 2432ece19502SDivya Koppera bool ret = false; 2433ece19502SDivya Koppera u16 skb_sig; 2434ece19502SDivya Koppera 2435ece19502SDivya Koppera lan8814_get_sig_rx(skb, &skb_sig); 2436ece19502SDivya Koppera 2437ece19502SDivya Koppera /* Iterate over all RX timestamps and match it with the received skbs */ 2438ece19502SDivya Koppera spin_lock_irqsave(&ptp_priv->rx_ts_lock, flags); 2439ece19502SDivya Koppera list_for_each_entry_safe(rx_ts, tmp, &ptp_priv->rx_ts_list, list) { 2440ece19502SDivya Koppera /* Check if we found the signature we were looking for. */ 2441ece19502SDivya Koppera if (memcmp(&skb_sig, &rx_ts->seq_id, sizeof(rx_ts->seq_id))) 2442ece19502SDivya Koppera continue; 2443ece19502SDivya Koppera 2444ece19502SDivya Koppera shhwtstamps = skb_hwtstamps(skb); 2445ece19502SDivya Koppera memset(shhwtstamps, 0, sizeof(*shhwtstamps)); 2446ece19502SDivya Koppera shhwtstamps->hwtstamp = ktime_set(rx_ts->seconds, 2447ece19502SDivya Koppera rx_ts->nsec); 2448ece19502SDivya Koppera list_del(&rx_ts->list); 2449ece19502SDivya Koppera kfree(rx_ts); 2450ece19502SDivya Koppera 2451ece19502SDivya Koppera ret = true; 2452ece19502SDivya Koppera break; 2453ece19502SDivya Koppera } 2454ece19502SDivya Koppera spin_unlock_irqrestore(&ptp_priv->rx_ts_lock, flags); 2455ece19502SDivya Koppera 245667dbd6c0SSebastian Andrzej Siewior if (ret) 245767dbd6c0SSebastian Andrzej Siewior netif_rx(skb); 2458ece19502SDivya Koppera return ret; 2459ece19502SDivya Koppera } 2460ece19502SDivya Koppera 2461ece19502SDivya Koppera static bool lan8814_rxtstamp(struct mii_timestamper *mii_ts, struct sk_buff *skb, int type) 2462ece19502SDivya Koppera { 2463ece19502SDivya Koppera struct kszphy_ptp_priv *ptp_priv = 2464ece19502SDivya Koppera container_of(mii_ts, struct kszphy_ptp_priv, mii_ts); 2465ece19502SDivya Koppera 2466ece19502SDivya Koppera if (ptp_priv->rx_filter == HWTSTAMP_FILTER_NONE || 2467ece19502SDivya Koppera type == PTP_CLASS_NONE) 2468ece19502SDivya Koppera return false; 2469ece19502SDivya Koppera 2470ece19502SDivya Koppera if ((type & ptp_priv->version) == 0 || (type & ptp_priv->layer) == 0) 2471ece19502SDivya Koppera return false; 2472ece19502SDivya Koppera 2473ece19502SDivya Koppera /* If we failed to match then add it to the queue for when the timestamp 2474ece19502SDivya Koppera * will come 2475ece19502SDivya Koppera */ 2476cafc3662SHoratiu Vultur if (!lan8814_match_rx_skb(ptp_priv, skb)) 2477ece19502SDivya Koppera skb_queue_tail(&ptp_priv->rx_queue, skb); 2478ece19502SDivya Koppera 2479ece19502SDivya Koppera return true; 2480ece19502SDivya Koppera } 2481ece19502SDivya Koppera 2482ece19502SDivya Koppera static void lan8814_ptp_clock_set(struct phy_device *phydev, 2483ece19502SDivya Koppera u32 seconds, u32 nano_seconds) 2484ece19502SDivya Koppera { 2485ece19502SDivya Koppera u32 sec_low, sec_high, nsec_low, nsec_high; 2486ece19502SDivya Koppera 2487ece19502SDivya Koppera sec_low = seconds & 0xffff; 2488ece19502SDivya Koppera sec_high = (seconds >> 16) & 0xffff; 2489ece19502SDivya Koppera nsec_low = nano_seconds & 0xffff; 2490ece19502SDivya Koppera nsec_high = (nano_seconds >> 16) & 0x3fff; 2491ece19502SDivya Koppera 2492ece19502SDivya Koppera lanphy_write_page_reg(phydev, 4, PTP_CLOCK_SET_SEC_LO, sec_low); 2493ece19502SDivya Koppera lanphy_write_page_reg(phydev, 4, PTP_CLOCK_SET_SEC_MID, sec_high); 2494ece19502SDivya Koppera lanphy_write_page_reg(phydev, 4, PTP_CLOCK_SET_NS_LO, nsec_low); 2495ece19502SDivya Koppera lanphy_write_page_reg(phydev, 4, PTP_CLOCK_SET_NS_HI, nsec_high); 2496ece19502SDivya Koppera 2497ece19502SDivya Koppera lanphy_write_page_reg(phydev, 4, PTP_CMD_CTL, PTP_CMD_CTL_PTP_CLOCK_LOAD_); 2498ece19502SDivya Koppera } 2499ece19502SDivya Koppera 2500ece19502SDivya Koppera static void lan8814_ptp_clock_get(struct phy_device *phydev, 2501ece19502SDivya Koppera u32 *seconds, u32 *nano_seconds) 2502ece19502SDivya Koppera { 2503ece19502SDivya Koppera lanphy_write_page_reg(phydev, 4, PTP_CMD_CTL, PTP_CMD_CTL_PTP_CLOCK_READ_); 2504ece19502SDivya Koppera 2505ece19502SDivya Koppera *seconds = lanphy_read_page_reg(phydev, 4, PTP_CLOCK_READ_SEC_MID); 2506ece19502SDivya Koppera *seconds = (*seconds << 16) | 2507ece19502SDivya Koppera lanphy_read_page_reg(phydev, 4, PTP_CLOCK_READ_SEC_LO); 2508ece19502SDivya Koppera 2509ece19502SDivya Koppera *nano_seconds = lanphy_read_page_reg(phydev, 4, PTP_CLOCK_READ_NS_HI); 2510ece19502SDivya Koppera *nano_seconds = ((*nano_seconds & 0x3fff) << 16) | 2511ece19502SDivya Koppera lanphy_read_page_reg(phydev, 4, PTP_CLOCK_READ_NS_LO); 2512ece19502SDivya Koppera } 2513ece19502SDivya Koppera 2514ece19502SDivya Koppera static int lan8814_ptpci_gettime64(struct ptp_clock_info *ptpci, 2515ece19502SDivya Koppera struct timespec64 *ts) 2516ece19502SDivya Koppera { 2517ece19502SDivya Koppera struct lan8814_shared_priv *shared = container_of(ptpci, struct lan8814_shared_priv, 2518ece19502SDivya Koppera ptp_clock_info); 2519ece19502SDivya Koppera struct phy_device *phydev = shared->phydev; 2520ece19502SDivya Koppera u32 nano_seconds; 2521ece19502SDivya Koppera u32 seconds; 2522ece19502SDivya Koppera 2523ece19502SDivya Koppera mutex_lock(&shared->shared_lock); 2524ece19502SDivya Koppera lan8814_ptp_clock_get(phydev, &seconds, &nano_seconds); 2525ece19502SDivya Koppera mutex_unlock(&shared->shared_lock); 2526ece19502SDivya Koppera ts->tv_sec = seconds; 2527ece19502SDivya Koppera ts->tv_nsec = nano_seconds; 2528ece19502SDivya Koppera 2529ece19502SDivya Koppera return 0; 2530ece19502SDivya Koppera } 2531ece19502SDivya Koppera 2532ece19502SDivya Koppera static int lan8814_ptpci_settime64(struct ptp_clock_info *ptpci, 2533ece19502SDivya Koppera const struct timespec64 *ts) 2534ece19502SDivya Koppera { 2535ece19502SDivya Koppera struct lan8814_shared_priv *shared = container_of(ptpci, struct lan8814_shared_priv, 2536ece19502SDivya Koppera ptp_clock_info); 2537ece19502SDivya Koppera struct phy_device *phydev = shared->phydev; 2538ece19502SDivya Koppera 2539ece19502SDivya Koppera mutex_lock(&shared->shared_lock); 2540ece19502SDivya Koppera lan8814_ptp_clock_set(phydev, ts->tv_sec, ts->tv_nsec); 2541ece19502SDivya Koppera mutex_unlock(&shared->shared_lock); 2542ece19502SDivya Koppera 2543ece19502SDivya Koppera return 0; 2544ece19502SDivya Koppera } 2545ece19502SDivya Koppera 2546ece19502SDivya Koppera static void lan8814_ptp_clock_step(struct phy_device *phydev, 2547ece19502SDivya Koppera s64 time_step_ns) 2548ece19502SDivya Koppera { 2549ece19502SDivya Koppera u32 nano_seconds_step; 2550ece19502SDivya Koppera u64 abs_time_step_ns; 2551ece19502SDivya Koppera u32 unsigned_seconds; 2552ece19502SDivya Koppera u32 nano_seconds; 2553ece19502SDivya Koppera u32 remainder; 2554ece19502SDivya Koppera s32 seconds; 2555ece19502SDivya Koppera 2556ece19502SDivya Koppera if (time_step_ns > 15000000000LL) { 2557ece19502SDivya Koppera /* convert to clock set */ 2558ece19502SDivya Koppera lan8814_ptp_clock_get(phydev, &unsigned_seconds, &nano_seconds); 2559ece19502SDivya Koppera unsigned_seconds += div_u64_rem(time_step_ns, 1000000000LL, 2560ece19502SDivya Koppera &remainder); 2561ece19502SDivya Koppera nano_seconds += remainder; 2562ece19502SDivya Koppera if (nano_seconds >= 1000000000) { 2563ece19502SDivya Koppera unsigned_seconds++; 2564ece19502SDivya Koppera nano_seconds -= 1000000000; 2565ece19502SDivya Koppera } 2566ece19502SDivya Koppera lan8814_ptp_clock_set(phydev, unsigned_seconds, nano_seconds); 2567ece19502SDivya Koppera return; 2568ece19502SDivya Koppera } else if (time_step_ns < -15000000000LL) { 2569ece19502SDivya Koppera /* convert to clock set */ 2570ece19502SDivya Koppera time_step_ns = -time_step_ns; 2571ece19502SDivya Koppera 2572ece19502SDivya Koppera lan8814_ptp_clock_get(phydev, &unsigned_seconds, &nano_seconds); 2573ece19502SDivya Koppera unsigned_seconds -= div_u64_rem(time_step_ns, 1000000000LL, 2574ece19502SDivya Koppera &remainder); 2575ece19502SDivya Koppera nano_seconds_step = remainder; 2576ece19502SDivya Koppera if (nano_seconds < nano_seconds_step) { 2577ece19502SDivya Koppera unsigned_seconds--; 2578ece19502SDivya Koppera nano_seconds += 1000000000; 2579ece19502SDivya Koppera } 2580ece19502SDivya Koppera nano_seconds -= nano_seconds_step; 2581ece19502SDivya Koppera lan8814_ptp_clock_set(phydev, unsigned_seconds, 2582ece19502SDivya Koppera nano_seconds); 2583ece19502SDivya Koppera return; 2584ece19502SDivya Koppera } 2585ece19502SDivya Koppera 2586ece19502SDivya Koppera /* do clock step */ 2587ece19502SDivya Koppera if (time_step_ns >= 0) { 2588ece19502SDivya Koppera abs_time_step_ns = (u64)time_step_ns; 2589ece19502SDivya Koppera seconds = (s32)div_u64_rem(abs_time_step_ns, 1000000000, 2590ece19502SDivya Koppera &remainder); 2591ece19502SDivya Koppera nano_seconds = remainder; 2592ece19502SDivya Koppera } else { 2593ece19502SDivya Koppera abs_time_step_ns = (u64)(-time_step_ns); 2594ece19502SDivya Koppera seconds = -((s32)div_u64_rem(abs_time_step_ns, 1000000000, 2595ece19502SDivya Koppera &remainder)); 2596ece19502SDivya Koppera nano_seconds = remainder; 2597ece19502SDivya Koppera if (nano_seconds > 0) { 2598ece19502SDivya Koppera /* subtracting nano seconds is not allowed 2599ece19502SDivya Koppera * convert to subtracting from seconds, 2600ece19502SDivya Koppera * and adding to nanoseconds 2601ece19502SDivya Koppera */ 2602ece19502SDivya Koppera seconds--; 2603ece19502SDivya Koppera nano_seconds = (1000000000 - nano_seconds); 2604ece19502SDivya Koppera } 2605ece19502SDivya Koppera } 2606ece19502SDivya Koppera 2607ece19502SDivya Koppera if (nano_seconds > 0) { 2608ece19502SDivya Koppera /* add 8 ns to cover the likely normal increment */ 2609ece19502SDivya Koppera nano_seconds += 8; 2610ece19502SDivya Koppera } 2611ece19502SDivya Koppera 2612ece19502SDivya Koppera if (nano_seconds >= 1000000000) { 2613ece19502SDivya Koppera /* carry into seconds */ 2614ece19502SDivya Koppera seconds++; 2615ece19502SDivya Koppera nano_seconds -= 1000000000; 2616ece19502SDivya Koppera } 2617ece19502SDivya Koppera 2618ece19502SDivya Koppera while (seconds) { 2619ece19502SDivya Koppera if (seconds > 0) { 2620ece19502SDivya Koppera u32 adjustment_value = (u32)seconds; 2621ece19502SDivya Koppera u16 adjustment_value_lo, adjustment_value_hi; 2622ece19502SDivya Koppera 2623ece19502SDivya Koppera if (adjustment_value > 0xF) 2624ece19502SDivya Koppera adjustment_value = 0xF; 2625ece19502SDivya Koppera 2626ece19502SDivya Koppera adjustment_value_lo = adjustment_value & 0xffff; 2627ece19502SDivya Koppera adjustment_value_hi = (adjustment_value >> 16) & 0x3fff; 2628ece19502SDivya Koppera 2629ece19502SDivya Koppera lanphy_write_page_reg(phydev, 4, PTP_LTC_STEP_ADJ_LO, 2630ece19502SDivya Koppera adjustment_value_lo); 2631ece19502SDivya Koppera lanphy_write_page_reg(phydev, 4, PTP_LTC_STEP_ADJ_HI, 2632ece19502SDivya Koppera PTP_LTC_STEP_ADJ_DIR_ | 2633ece19502SDivya Koppera adjustment_value_hi); 2634ece19502SDivya Koppera seconds -= ((s32)adjustment_value); 2635ece19502SDivya Koppera } else { 2636ece19502SDivya Koppera u32 adjustment_value = (u32)(-seconds); 2637ece19502SDivya Koppera u16 adjustment_value_lo, adjustment_value_hi; 2638ece19502SDivya Koppera 2639ece19502SDivya Koppera if (adjustment_value > 0xF) 2640ece19502SDivya Koppera adjustment_value = 0xF; 2641ece19502SDivya Koppera 2642ece19502SDivya Koppera adjustment_value_lo = adjustment_value & 0xffff; 2643ece19502SDivya Koppera adjustment_value_hi = (adjustment_value >> 16) & 0x3fff; 2644ece19502SDivya Koppera 2645ece19502SDivya Koppera lanphy_write_page_reg(phydev, 4, PTP_LTC_STEP_ADJ_LO, 2646ece19502SDivya Koppera adjustment_value_lo); 2647ece19502SDivya Koppera lanphy_write_page_reg(phydev, 4, PTP_LTC_STEP_ADJ_HI, 2648ece19502SDivya Koppera adjustment_value_hi); 2649ece19502SDivya Koppera seconds += ((s32)adjustment_value); 2650ece19502SDivya Koppera } 2651ece19502SDivya Koppera lanphy_write_page_reg(phydev, 4, PTP_CMD_CTL, 2652ece19502SDivya Koppera PTP_CMD_CTL_PTP_LTC_STEP_SEC_); 2653ece19502SDivya Koppera } 2654ece19502SDivya Koppera if (nano_seconds) { 2655ece19502SDivya Koppera u16 nano_seconds_lo; 2656ece19502SDivya Koppera u16 nano_seconds_hi; 2657ece19502SDivya Koppera 2658ece19502SDivya Koppera nano_seconds_lo = nano_seconds & 0xffff; 2659ece19502SDivya Koppera nano_seconds_hi = (nano_seconds >> 16) & 0x3fff; 2660ece19502SDivya Koppera 2661ece19502SDivya Koppera lanphy_write_page_reg(phydev, 4, PTP_LTC_STEP_ADJ_LO, 2662ece19502SDivya Koppera nano_seconds_lo); 2663ece19502SDivya Koppera lanphy_write_page_reg(phydev, 4, PTP_LTC_STEP_ADJ_HI, 2664ece19502SDivya Koppera PTP_LTC_STEP_ADJ_DIR_ | 2665ece19502SDivya Koppera nano_seconds_hi); 2666ece19502SDivya Koppera lanphy_write_page_reg(phydev, 4, PTP_CMD_CTL, 2667ece19502SDivya Koppera PTP_CMD_CTL_PTP_LTC_STEP_NSEC_); 2668ece19502SDivya Koppera } 2669ece19502SDivya Koppera } 2670ece19502SDivya Koppera 2671ece19502SDivya Koppera static int lan8814_ptpci_adjtime(struct ptp_clock_info *ptpci, s64 delta) 2672ece19502SDivya Koppera { 2673ece19502SDivya Koppera struct lan8814_shared_priv *shared = container_of(ptpci, struct lan8814_shared_priv, 2674ece19502SDivya Koppera ptp_clock_info); 2675ece19502SDivya Koppera struct phy_device *phydev = shared->phydev; 2676ece19502SDivya Koppera 2677ece19502SDivya Koppera mutex_lock(&shared->shared_lock); 2678ece19502SDivya Koppera lan8814_ptp_clock_step(phydev, delta); 2679ece19502SDivya Koppera mutex_unlock(&shared->shared_lock); 2680ece19502SDivya Koppera 2681ece19502SDivya Koppera return 0; 2682ece19502SDivya Koppera } 2683ece19502SDivya Koppera 2684ece19502SDivya Koppera static int lan8814_ptpci_adjfine(struct ptp_clock_info *ptpci, long scaled_ppm) 2685ece19502SDivya Koppera { 2686ece19502SDivya Koppera struct lan8814_shared_priv *shared = container_of(ptpci, struct lan8814_shared_priv, 2687ece19502SDivya Koppera ptp_clock_info); 2688ece19502SDivya Koppera struct phy_device *phydev = shared->phydev; 2689ece19502SDivya Koppera u16 kszphy_rate_adj_lo, kszphy_rate_adj_hi; 2690ece19502SDivya Koppera bool positive = true; 2691ece19502SDivya Koppera u32 kszphy_rate_adj; 2692ece19502SDivya Koppera 2693ece19502SDivya Koppera if (scaled_ppm < 0) { 2694ece19502SDivya Koppera scaled_ppm = -scaled_ppm; 2695ece19502SDivya Koppera positive = false; 2696ece19502SDivya Koppera } 2697ece19502SDivya Koppera 2698ece19502SDivya Koppera kszphy_rate_adj = LAN8814_1PPM_FORMAT * (scaled_ppm >> 16); 2699ece19502SDivya Koppera kszphy_rate_adj += (LAN8814_1PPM_FORMAT * (0xffff & scaled_ppm)) >> 16; 2700ece19502SDivya Koppera 2701ece19502SDivya Koppera kszphy_rate_adj_lo = kszphy_rate_adj & 0xffff; 2702ece19502SDivya Koppera kszphy_rate_adj_hi = (kszphy_rate_adj >> 16) & 0x3fff; 2703ece19502SDivya Koppera 2704ece19502SDivya Koppera if (positive) 2705ece19502SDivya Koppera kszphy_rate_adj_hi |= PTP_CLOCK_RATE_ADJ_DIR_; 2706ece19502SDivya Koppera 2707ece19502SDivya Koppera mutex_lock(&shared->shared_lock); 2708ece19502SDivya Koppera lanphy_write_page_reg(phydev, 4, PTP_CLOCK_RATE_ADJ_HI, kszphy_rate_adj_hi); 2709ece19502SDivya Koppera lanphy_write_page_reg(phydev, 4, PTP_CLOCK_RATE_ADJ_LO, kszphy_rate_adj_lo); 2710ece19502SDivya Koppera mutex_unlock(&shared->shared_lock); 2711ece19502SDivya Koppera 2712ece19502SDivya Koppera return 0; 2713ece19502SDivya Koppera } 2714ece19502SDivya Koppera 2715ece19502SDivya Koppera static void lan8814_get_sig_tx(struct sk_buff *skb, u16 *sig) 2716ece19502SDivya Koppera { 2717ece19502SDivya Koppera struct ptp_header *ptp_header; 2718ece19502SDivya Koppera u32 type; 2719ece19502SDivya Koppera 2720ece19502SDivya Koppera type = ptp_classify_raw(skb); 2721ece19502SDivya Koppera ptp_header = ptp_parse_header(skb, type); 2722ece19502SDivya Koppera 2723ece19502SDivya Koppera *sig = (__force u16)(ntohs(ptp_header->sequence_id)); 2724ece19502SDivya Koppera } 2725ece19502SDivya Koppera 2726cafc3662SHoratiu Vultur static void lan8814_match_tx_skb(struct kszphy_ptp_priv *ptp_priv, 2727cafc3662SHoratiu Vultur u32 seconds, u32 nsec, u16 seq_id) 2728ece19502SDivya Koppera { 2729ece19502SDivya Koppera struct skb_shared_hwtstamps shhwtstamps; 2730ece19502SDivya Koppera struct sk_buff *skb, *skb_tmp; 2731ece19502SDivya Koppera unsigned long flags; 2732ece19502SDivya Koppera bool ret = false; 2733ece19502SDivya Koppera u16 skb_sig; 2734ece19502SDivya Koppera 2735ece19502SDivya Koppera spin_lock_irqsave(&ptp_priv->tx_queue.lock, flags); 2736ece19502SDivya Koppera skb_queue_walk_safe(&ptp_priv->tx_queue, skb, skb_tmp) { 2737ece19502SDivya Koppera lan8814_get_sig_tx(skb, &skb_sig); 2738ece19502SDivya Koppera 2739ece19502SDivya Koppera if (memcmp(&skb_sig, &seq_id, sizeof(seq_id))) 2740ece19502SDivya Koppera continue; 2741ece19502SDivya Koppera 2742ece19502SDivya Koppera __skb_unlink(skb, &ptp_priv->tx_queue); 2743ece19502SDivya Koppera ret = true; 2744ece19502SDivya Koppera break; 2745ece19502SDivya Koppera } 2746ece19502SDivya Koppera spin_unlock_irqrestore(&ptp_priv->tx_queue.lock, flags); 2747ece19502SDivya Koppera 2748ece19502SDivya Koppera if (ret) { 2749ece19502SDivya Koppera memset(&shhwtstamps, 0, sizeof(shhwtstamps)); 2750ece19502SDivya Koppera shhwtstamps.hwtstamp = ktime_set(seconds, nsec); 2751ece19502SDivya Koppera skb_complete_tx_timestamp(skb, &shhwtstamps); 2752ece19502SDivya Koppera } 2753ece19502SDivya Koppera } 2754ece19502SDivya Koppera 2755cafc3662SHoratiu Vultur static void lan8814_dequeue_tx_skb(struct kszphy_ptp_priv *ptp_priv) 2756cafc3662SHoratiu Vultur { 2757cafc3662SHoratiu Vultur struct phy_device *phydev = ptp_priv->phydev; 2758cafc3662SHoratiu Vultur u32 seconds, nsec; 2759cafc3662SHoratiu Vultur u16 seq_id; 2760cafc3662SHoratiu Vultur 2761cafc3662SHoratiu Vultur lan8814_ptp_tx_ts_get(phydev, &seconds, &nsec, &seq_id); 2762cafc3662SHoratiu Vultur lan8814_match_tx_skb(ptp_priv, seconds, nsec, seq_id); 2763cafc3662SHoratiu Vultur } 2764cafc3662SHoratiu Vultur 2765ece19502SDivya Koppera static void lan8814_get_tx_ts(struct kszphy_ptp_priv *ptp_priv) 2766ece19502SDivya Koppera { 2767ece19502SDivya Koppera struct phy_device *phydev = ptp_priv->phydev; 2768ece19502SDivya Koppera u32 reg; 2769ece19502SDivya Koppera 2770ece19502SDivya Koppera do { 2771ece19502SDivya Koppera lan8814_dequeue_tx_skb(ptp_priv); 2772ece19502SDivya Koppera 2773ece19502SDivya Koppera /* If other timestamps are available in the FIFO, 2774ece19502SDivya Koppera * process them. 2775ece19502SDivya Koppera */ 2776ece19502SDivya Koppera reg = lanphy_read_page_reg(phydev, 5, PTP_CAP_INFO); 2777ece19502SDivya Koppera } while (PTP_CAP_INFO_TX_TS_CNT_GET_(reg) > 0); 2778ece19502SDivya Koppera } 2779ece19502SDivya Koppera 2780ece19502SDivya Koppera static bool lan8814_match_skb(struct kszphy_ptp_priv *ptp_priv, 2781ece19502SDivya Koppera struct lan8814_ptp_rx_ts *rx_ts) 2782ece19502SDivya Koppera { 2783ece19502SDivya Koppera struct skb_shared_hwtstamps *shhwtstamps; 2784ece19502SDivya Koppera struct sk_buff *skb, *skb_tmp; 2785ece19502SDivya Koppera unsigned long flags; 2786ece19502SDivya Koppera bool ret = false; 2787ece19502SDivya Koppera u16 skb_sig; 2788ece19502SDivya Koppera 2789ece19502SDivya Koppera spin_lock_irqsave(&ptp_priv->rx_queue.lock, flags); 2790ece19502SDivya Koppera skb_queue_walk_safe(&ptp_priv->rx_queue, skb, skb_tmp) { 2791ece19502SDivya Koppera lan8814_get_sig_rx(skb, &skb_sig); 2792ece19502SDivya Koppera 2793ece19502SDivya Koppera if (memcmp(&skb_sig, &rx_ts->seq_id, sizeof(rx_ts->seq_id))) 2794ece19502SDivya Koppera continue; 2795ece19502SDivya Koppera 2796ece19502SDivya Koppera __skb_unlink(skb, &ptp_priv->rx_queue); 2797ece19502SDivya Koppera 2798ece19502SDivya Koppera ret = true; 2799ece19502SDivya Koppera break; 2800ece19502SDivya Koppera } 2801ece19502SDivya Koppera spin_unlock_irqrestore(&ptp_priv->rx_queue.lock, flags); 2802ece19502SDivya Koppera 2803ece19502SDivya Koppera if (ret) { 2804ece19502SDivya Koppera shhwtstamps = skb_hwtstamps(skb); 2805ece19502SDivya Koppera memset(shhwtstamps, 0, sizeof(*shhwtstamps)); 2806ece19502SDivya Koppera shhwtstamps->hwtstamp = ktime_set(rx_ts->seconds, rx_ts->nsec); 2807e1f9e434SSebastian Andrzej Siewior netif_rx(skb); 2808ece19502SDivya Koppera } 2809ece19502SDivya Koppera 2810ece19502SDivya Koppera return ret; 2811ece19502SDivya Koppera } 2812ece19502SDivya Koppera 2813cafc3662SHoratiu Vultur static void lan8814_match_rx_ts(struct kszphy_ptp_priv *ptp_priv, 2814cafc3662SHoratiu Vultur struct lan8814_ptp_rx_ts *rx_ts) 2815ece19502SDivya Koppera { 2816ece19502SDivya Koppera unsigned long flags; 2817ece19502SDivya Koppera 2818ece19502SDivya Koppera /* If we failed to match the skb add it to the queue for when 2819ece19502SDivya Koppera * the frame will come 2820ece19502SDivya Koppera */ 2821ece19502SDivya Koppera if (!lan8814_match_skb(ptp_priv, rx_ts)) { 2822ece19502SDivya Koppera spin_lock_irqsave(&ptp_priv->rx_ts_lock, flags); 2823ece19502SDivya Koppera list_add(&rx_ts->list, &ptp_priv->rx_ts_list); 2824ece19502SDivya Koppera spin_unlock_irqrestore(&ptp_priv->rx_ts_lock, flags); 2825ece19502SDivya Koppera } else { 2826ece19502SDivya Koppera kfree(rx_ts); 2827ece19502SDivya Koppera } 2828cafc3662SHoratiu Vultur } 2829cafc3662SHoratiu Vultur 2830cafc3662SHoratiu Vultur static void lan8814_get_rx_ts(struct kszphy_ptp_priv *ptp_priv) 2831cafc3662SHoratiu Vultur { 2832cafc3662SHoratiu Vultur struct phy_device *phydev = ptp_priv->phydev; 2833cafc3662SHoratiu Vultur struct lan8814_ptp_rx_ts *rx_ts; 2834cafc3662SHoratiu Vultur u32 reg; 2835cafc3662SHoratiu Vultur 2836cafc3662SHoratiu Vultur do { 2837cafc3662SHoratiu Vultur rx_ts = kzalloc(sizeof(*rx_ts), GFP_KERNEL); 2838cafc3662SHoratiu Vultur if (!rx_ts) 2839cafc3662SHoratiu Vultur return; 2840cafc3662SHoratiu Vultur 2841cafc3662SHoratiu Vultur lan8814_ptp_rx_ts_get(phydev, &rx_ts->seconds, &rx_ts->nsec, 2842cafc3662SHoratiu Vultur &rx_ts->seq_id); 2843cafc3662SHoratiu Vultur lan8814_match_rx_ts(ptp_priv, rx_ts); 2844ece19502SDivya Koppera 2845ece19502SDivya Koppera /* If other timestamps are available in the FIFO, 2846ece19502SDivya Koppera * process them. 2847ece19502SDivya Koppera */ 2848ece19502SDivya Koppera reg = lanphy_read_page_reg(phydev, 5, PTP_CAP_INFO); 2849ece19502SDivya Koppera } while (PTP_CAP_INFO_RX_TS_CNT_GET_(reg) > 0); 2850ece19502SDivya Koppera } 2851ece19502SDivya Koppera 28527abd92a5SHoratiu Vultur static void lan8814_handle_ptp_interrupt(struct phy_device *phydev, u16 status) 2853ece19502SDivya Koppera { 2854ece19502SDivya Koppera struct kszphy_priv *priv = phydev->priv; 2855ece19502SDivya Koppera struct kszphy_ptp_priv *ptp_priv = &priv->ptp_priv; 2856ece19502SDivya Koppera 2857ece19502SDivya Koppera if (status & PTP_TSU_INT_STS_PTP_TX_TS_EN_) 2858ece19502SDivya Koppera lan8814_get_tx_ts(ptp_priv); 2859ece19502SDivya Koppera 2860ece19502SDivya Koppera if (status & PTP_TSU_INT_STS_PTP_RX_TS_EN_) 2861ece19502SDivya Koppera lan8814_get_rx_ts(ptp_priv); 2862ece19502SDivya Koppera 2863ece19502SDivya Koppera if (status & PTP_TSU_INT_STS_PTP_TX_TS_OVRFL_INT_) { 2864ece19502SDivya Koppera lan8814_flush_fifo(phydev, true); 2865ece19502SDivya Koppera skb_queue_purge(&ptp_priv->tx_queue); 2866ece19502SDivya Koppera } 2867ece19502SDivya Koppera 2868ece19502SDivya Koppera if (status & PTP_TSU_INT_STS_PTP_RX_TS_OVRFL_INT_) { 2869ece19502SDivya Koppera lan8814_flush_fifo(phydev, false); 2870ece19502SDivya Koppera skb_queue_purge(&ptp_priv->rx_queue); 2871ece19502SDivya Koppera } 2872ece19502SDivya Koppera } 2873ece19502SDivya Koppera 28747c2dcfa2SHoratiu Vultur static int lan8804_config_init(struct phy_device *phydev) 28757c2dcfa2SHoratiu Vultur { 28767c2dcfa2SHoratiu Vultur int val; 28777c2dcfa2SHoratiu Vultur 28787c2dcfa2SHoratiu Vultur /* MDI-X setting for swap A,B transmit */ 28797c2dcfa2SHoratiu Vultur val = lanphy_read_page_reg(phydev, 2, LAN8804_ALIGN_SWAP); 28807c2dcfa2SHoratiu Vultur val &= ~LAN8804_ALIGN_TX_A_B_SWAP_MASK; 28817c2dcfa2SHoratiu Vultur val |= LAN8804_ALIGN_TX_A_B_SWAP; 28827c2dcfa2SHoratiu Vultur lanphy_write_page_reg(phydev, 2, LAN8804_ALIGN_SWAP, val); 28837c2dcfa2SHoratiu Vultur 28847c2dcfa2SHoratiu Vultur /* Make sure that the PHY will not stop generating the clock when the 28857c2dcfa2SHoratiu Vultur * link partner goes down 28867c2dcfa2SHoratiu Vultur */ 28877c2dcfa2SHoratiu Vultur lanphy_write_page_reg(phydev, 31, LAN8814_CLOCK_MANAGEMENT, 0x27e); 28887c2dcfa2SHoratiu Vultur lanphy_read_page_reg(phydev, 1, LAN8814_LINK_QUALITY); 28897c2dcfa2SHoratiu Vultur 28907c2dcfa2SHoratiu Vultur return 0; 28917c2dcfa2SHoratiu Vultur } 28927c2dcfa2SHoratiu Vultur 2893b324c6e5SHoratiu Vultur static irqreturn_t lan8804_handle_interrupt(struct phy_device *phydev) 2894b324c6e5SHoratiu Vultur { 2895b324c6e5SHoratiu Vultur int status; 2896b324c6e5SHoratiu Vultur 2897b324c6e5SHoratiu Vultur status = phy_read(phydev, LAN8814_INTS); 2898b324c6e5SHoratiu Vultur if (status < 0) { 2899b324c6e5SHoratiu Vultur phy_error(phydev); 2900b324c6e5SHoratiu Vultur return IRQ_NONE; 2901b324c6e5SHoratiu Vultur } 2902b324c6e5SHoratiu Vultur 2903b324c6e5SHoratiu Vultur if (status > 0) 2904b324c6e5SHoratiu Vultur phy_trigger_machine(phydev); 2905b324c6e5SHoratiu Vultur 2906b324c6e5SHoratiu Vultur return IRQ_HANDLED; 2907b324c6e5SHoratiu Vultur } 2908b324c6e5SHoratiu Vultur 2909b324c6e5SHoratiu Vultur #define LAN8804_OUTPUT_CONTROL 25 2910b324c6e5SHoratiu Vultur #define LAN8804_OUTPUT_CONTROL_INTR_BUFFER BIT(14) 2911b324c6e5SHoratiu Vultur #define LAN8804_CONTROL 31 2912b324c6e5SHoratiu Vultur #define LAN8804_CONTROL_INTR_POLARITY BIT(14) 2913b324c6e5SHoratiu Vultur 2914b324c6e5SHoratiu Vultur static int lan8804_config_intr(struct phy_device *phydev) 2915b324c6e5SHoratiu Vultur { 2916b324c6e5SHoratiu Vultur int err; 2917b324c6e5SHoratiu Vultur 2918b324c6e5SHoratiu Vultur /* This is an internal PHY of lan966x and is not possible to change the 2919b324c6e5SHoratiu Vultur * polarity on the GIC found in lan966x, therefore change the polarity 2920b324c6e5SHoratiu Vultur * of the interrupt in the PHY from being active low instead of active 2921b324c6e5SHoratiu Vultur * high. 2922b324c6e5SHoratiu Vultur */ 2923b324c6e5SHoratiu Vultur phy_write(phydev, LAN8804_CONTROL, LAN8804_CONTROL_INTR_POLARITY); 2924b324c6e5SHoratiu Vultur 2925b324c6e5SHoratiu Vultur /* By default interrupt buffer is open-drain in which case the interrupt 2926b324c6e5SHoratiu Vultur * can be active only low. Therefore change the interrupt buffer to be 2927b324c6e5SHoratiu Vultur * push-pull to be able to change interrupt polarity 2928b324c6e5SHoratiu Vultur */ 2929b324c6e5SHoratiu Vultur phy_write(phydev, LAN8804_OUTPUT_CONTROL, 2930b324c6e5SHoratiu Vultur LAN8804_OUTPUT_CONTROL_INTR_BUFFER); 2931b324c6e5SHoratiu Vultur 2932b324c6e5SHoratiu Vultur if (phydev->interrupts == PHY_INTERRUPT_ENABLED) { 2933b324c6e5SHoratiu Vultur err = phy_read(phydev, LAN8814_INTS); 2934b324c6e5SHoratiu Vultur if (err < 0) 2935b324c6e5SHoratiu Vultur return err; 2936b324c6e5SHoratiu Vultur 2937b324c6e5SHoratiu Vultur err = phy_write(phydev, LAN8814_INTC, LAN8814_INT_LINK); 2938b324c6e5SHoratiu Vultur if (err) 2939b324c6e5SHoratiu Vultur return err; 2940b324c6e5SHoratiu Vultur } else { 2941b324c6e5SHoratiu Vultur err = phy_write(phydev, LAN8814_INTC, 0); 2942b324c6e5SHoratiu Vultur if (err) 2943b324c6e5SHoratiu Vultur return err; 2944b324c6e5SHoratiu Vultur 2945b324c6e5SHoratiu Vultur err = phy_read(phydev, LAN8814_INTS); 2946b324c6e5SHoratiu Vultur if (err < 0) 2947b324c6e5SHoratiu Vultur return err; 2948b324c6e5SHoratiu Vultur } 2949b324c6e5SHoratiu Vultur 2950b324c6e5SHoratiu Vultur return 0; 2951b324c6e5SHoratiu Vultur } 2952b324c6e5SHoratiu Vultur 2953b3ec7248SDivya Koppera static irqreturn_t lan8814_handle_interrupt(struct phy_device *phydev) 2954b3ec7248SDivya Koppera { 29552002fbacSMichael Walle int ret = IRQ_NONE; 29567abd92a5SHoratiu Vultur int irq_status; 2957b3ec7248SDivya Koppera 2958b3ec7248SDivya Koppera irq_status = phy_read(phydev, LAN8814_INTS); 2959ece19502SDivya Koppera if (irq_status < 0) { 2960ece19502SDivya Koppera phy_error(phydev); 2961ece19502SDivya Koppera return IRQ_NONE; 2962ece19502SDivya Koppera } 2963ece19502SDivya Koppera 29642002fbacSMichael Walle if (irq_status & LAN8814_INT_LINK) { 29652002fbacSMichael Walle phy_trigger_machine(phydev); 29662002fbacSMichael Walle ret = IRQ_HANDLED; 29672002fbacSMichael Walle } 29682002fbacSMichael Walle 29697abd92a5SHoratiu Vultur while (true) { 29707abd92a5SHoratiu Vultur irq_status = lanphy_read_page_reg(phydev, 5, PTP_TSU_INT_STS); 29717abd92a5SHoratiu Vultur if (!irq_status) 2972ece19502SDivya Koppera break; 29737abd92a5SHoratiu Vultur 29747abd92a5SHoratiu Vultur lan8814_handle_ptp_interrupt(phydev, irq_status); 29757abd92a5SHoratiu Vultur ret = IRQ_HANDLED; 29762002fbacSMichael Walle } 29772002fbacSMichael Walle 29782002fbacSMichael Walle return ret; 2979b3ec7248SDivya Koppera } 2980b3ec7248SDivya Koppera 2981b3ec7248SDivya Koppera static int lan8814_ack_interrupt(struct phy_device *phydev) 2982b3ec7248SDivya Koppera { 2983b3ec7248SDivya Koppera /* bit[12..0] int status, which is a read and clear register. */ 2984b3ec7248SDivya Koppera int rc; 2985b3ec7248SDivya Koppera 2986b3ec7248SDivya Koppera rc = phy_read(phydev, LAN8814_INTS); 2987b3ec7248SDivya Koppera 2988b3ec7248SDivya Koppera return (rc < 0) ? rc : 0; 2989b3ec7248SDivya Koppera } 2990b3ec7248SDivya Koppera 2991b3ec7248SDivya Koppera static int lan8814_config_intr(struct phy_device *phydev) 2992b3ec7248SDivya Koppera { 2993b3ec7248SDivya Koppera int err; 2994b3ec7248SDivya Koppera 2995b3ec7248SDivya Koppera lanphy_write_page_reg(phydev, 4, LAN8814_INTR_CTRL_REG, 2996b3ec7248SDivya Koppera LAN8814_INTR_CTRL_REG_POLARITY | 2997b3ec7248SDivya Koppera LAN8814_INTR_CTRL_REG_INTR_ENABLE); 2998b3ec7248SDivya Koppera 2999b3ec7248SDivya Koppera /* enable / disable interrupts */ 3000b3ec7248SDivya Koppera if (phydev->interrupts == PHY_INTERRUPT_ENABLED) { 3001b3ec7248SDivya Koppera err = lan8814_ack_interrupt(phydev); 3002b3ec7248SDivya Koppera if (err) 3003b3ec7248SDivya Koppera return err; 3004b3ec7248SDivya Koppera 3005b3ec7248SDivya Koppera err = phy_write(phydev, LAN8814_INTC, LAN8814_INT_LINK); 3006b3ec7248SDivya Koppera } else { 3007b3ec7248SDivya Koppera err = phy_write(phydev, LAN8814_INTC, 0); 3008b3ec7248SDivya Koppera if (err) 3009b3ec7248SDivya Koppera return err; 3010b3ec7248SDivya Koppera 3011b3ec7248SDivya Koppera err = lan8814_ack_interrupt(phydev); 3012b3ec7248SDivya Koppera } 3013b3ec7248SDivya Koppera 3014b3ec7248SDivya Koppera return err; 3015b3ec7248SDivya Koppera } 3016b3ec7248SDivya Koppera 3017ece19502SDivya Koppera static void lan8814_ptp_init(struct phy_device *phydev) 3018ece19502SDivya Koppera { 3019ece19502SDivya Koppera struct kszphy_priv *priv = phydev->priv; 3020ece19502SDivya Koppera struct kszphy_ptp_priv *ptp_priv = &priv->ptp_priv; 3021ece19502SDivya Koppera u32 temp; 3022ece19502SDivya Koppera 302331d00ca4SMichael Walle if (!IS_ENABLED(CONFIG_PTP_1588_CLOCK) || 302431d00ca4SMichael Walle !IS_ENABLED(CONFIG_NETWORK_PHY_TIMESTAMPING)) 302531d00ca4SMichael Walle return; 302631d00ca4SMichael Walle 3027ece19502SDivya Koppera lanphy_write_page_reg(phydev, 5, TSU_HARD_RESET, TSU_HARD_RESET_); 3028ece19502SDivya Koppera 3029ece19502SDivya Koppera temp = lanphy_read_page_reg(phydev, 5, PTP_TX_MOD); 3030ece19502SDivya Koppera temp |= PTP_TX_MOD_BAD_UDPV4_CHKSUM_FORCE_FCS_DIS_; 3031ece19502SDivya Koppera lanphy_write_page_reg(phydev, 5, PTP_TX_MOD, temp); 3032ece19502SDivya Koppera 3033ece19502SDivya Koppera temp = lanphy_read_page_reg(phydev, 5, PTP_RX_MOD); 3034ece19502SDivya Koppera temp |= PTP_RX_MOD_BAD_UDPV4_CHKSUM_FORCE_FCS_DIS_; 3035ece19502SDivya Koppera lanphy_write_page_reg(phydev, 5, PTP_RX_MOD, temp); 3036ece19502SDivya Koppera 3037ece19502SDivya Koppera lanphy_write_page_reg(phydev, 5, PTP_RX_PARSE_CONFIG, 0); 3038ece19502SDivya Koppera lanphy_write_page_reg(phydev, 5, PTP_TX_PARSE_CONFIG, 0); 3039ece19502SDivya Koppera 3040ece19502SDivya Koppera /* Removing default registers configs related to L2 and IP */ 3041ece19502SDivya Koppera lanphy_write_page_reg(phydev, 5, PTP_TX_PARSE_L2_ADDR_EN, 0); 3042ece19502SDivya Koppera lanphy_write_page_reg(phydev, 5, PTP_RX_PARSE_L2_ADDR_EN, 0); 3043ece19502SDivya Koppera lanphy_write_page_reg(phydev, 5, PTP_TX_PARSE_IP_ADDR_EN, 0); 3044ece19502SDivya Koppera lanphy_write_page_reg(phydev, 5, PTP_RX_PARSE_IP_ADDR_EN, 0); 3045ece19502SDivya Koppera 3046ece19502SDivya Koppera skb_queue_head_init(&ptp_priv->tx_queue); 3047ece19502SDivya Koppera skb_queue_head_init(&ptp_priv->rx_queue); 3048ece19502SDivya Koppera INIT_LIST_HEAD(&ptp_priv->rx_ts_list); 3049ece19502SDivya Koppera spin_lock_init(&ptp_priv->rx_ts_lock); 3050ece19502SDivya Koppera 3051ece19502SDivya Koppera ptp_priv->phydev = phydev; 3052ece19502SDivya Koppera 3053ece19502SDivya Koppera ptp_priv->mii_ts.rxtstamp = lan8814_rxtstamp; 3054ece19502SDivya Koppera ptp_priv->mii_ts.txtstamp = lan8814_txtstamp; 3055ece19502SDivya Koppera ptp_priv->mii_ts.hwtstamp = lan8814_hwtstamp; 3056ece19502SDivya Koppera ptp_priv->mii_ts.ts_info = lan8814_ts_info; 3057ece19502SDivya Koppera 3058ece19502SDivya Koppera phydev->mii_ts = &ptp_priv->mii_ts; 3059ece19502SDivya Koppera } 3060ece19502SDivya Koppera 3061ece19502SDivya Koppera static int lan8814_ptp_probe_once(struct phy_device *phydev) 3062ece19502SDivya Koppera { 3063ece19502SDivya Koppera struct lan8814_shared_priv *shared = phydev->shared->priv; 3064ece19502SDivya Koppera 3065ece19502SDivya Koppera /* Initialise shared lock for clock*/ 3066ece19502SDivya Koppera mutex_init(&shared->shared_lock); 3067ece19502SDivya Koppera 3068ece19502SDivya Koppera shared->ptp_clock_info.owner = THIS_MODULE; 3069ece19502SDivya Koppera snprintf(shared->ptp_clock_info.name, 30, "%s", phydev->drv->name); 3070ece19502SDivya Koppera shared->ptp_clock_info.max_adj = 31249999; 3071ece19502SDivya Koppera shared->ptp_clock_info.n_alarm = 0; 3072ece19502SDivya Koppera shared->ptp_clock_info.n_ext_ts = 0; 3073ece19502SDivya Koppera shared->ptp_clock_info.n_pins = 0; 3074ece19502SDivya Koppera shared->ptp_clock_info.pps = 0; 3075ece19502SDivya Koppera shared->ptp_clock_info.pin_config = NULL; 3076ece19502SDivya Koppera shared->ptp_clock_info.adjfine = lan8814_ptpci_adjfine; 3077ece19502SDivya Koppera shared->ptp_clock_info.adjtime = lan8814_ptpci_adjtime; 3078ece19502SDivya Koppera shared->ptp_clock_info.gettime64 = lan8814_ptpci_gettime64; 3079ece19502SDivya Koppera shared->ptp_clock_info.settime64 = lan8814_ptpci_settime64; 3080ece19502SDivya Koppera shared->ptp_clock_info.getcrosststamp = NULL; 3081ece19502SDivya Koppera 3082ece19502SDivya Koppera shared->ptp_clock = ptp_clock_register(&shared->ptp_clock_info, 3083ece19502SDivya Koppera &phydev->mdio.dev); 30843f88d7d1SDivya Koppera if (IS_ERR(shared->ptp_clock)) { 3085ece19502SDivya Koppera phydev_err(phydev, "ptp_clock_register failed %lu\n", 3086ece19502SDivya Koppera PTR_ERR(shared->ptp_clock)); 3087ece19502SDivya Koppera return -EINVAL; 3088ece19502SDivya Koppera } 3089ece19502SDivya Koppera 30903f88d7d1SDivya Koppera /* Check if PHC support is missing at the configuration level */ 30913f88d7d1SDivya Koppera if (!shared->ptp_clock) 30923f88d7d1SDivya Koppera return 0; 30933f88d7d1SDivya Koppera 3094ece19502SDivya Koppera phydev_dbg(phydev, "successfully registered ptp clock\n"); 3095ece19502SDivya Koppera 3096ece19502SDivya Koppera shared->phydev = phydev; 3097ece19502SDivya Koppera 3098ece19502SDivya Koppera /* The EP.4 is shared between all the PHYs in the package and also it 3099ece19502SDivya Koppera * can be accessed by any of the PHYs 3100ece19502SDivya Koppera */ 3101ece19502SDivya Koppera lanphy_write_page_reg(phydev, 4, LTC_HARD_RESET, LTC_HARD_RESET_); 3102ece19502SDivya Koppera lanphy_write_page_reg(phydev, 4, PTP_OPERATING_MODE, 3103ece19502SDivya Koppera PTP_OPERATING_MODE_STANDALONE_); 3104ece19502SDivya Koppera 3105ece19502SDivya Koppera return 0; 3106ece19502SDivya Koppera } 3107ece19502SDivya Koppera 3108a516b7f7SDivya Koppera static void lan8814_setup_led(struct phy_device *phydev, int val) 3109a516b7f7SDivya Koppera { 3110a516b7f7SDivya Koppera int temp; 3111a516b7f7SDivya Koppera 3112a516b7f7SDivya Koppera temp = lanphy_read_page_reg(phydev, 5, LAN8814_LED_CTRL_1); 3113a516b7f7SDivya Koppera 3114a516b7f7SDivya Koppera if (val) 3115a516b7f7SDivya Koppera temp |= LAN8814_LED_CTRL_1_KSZ9031_LED_MODE_; 3116a516b7f7SDivya Koppera else 3117a516b7f7SDivya Koppera temp &= ~LAN8814_LED_CTRL_1_KSZ9031_LED_MODE_; 3118a516b7f7SDivya Koppera 3119a516b7f7SDivya Koppera lanphy_write_page_reg(phydev, 5, LAN8814_LED_CTRL_1, temp); 3120a516b7f7SDivya Koppera } 3121a516b7f7SDivya Koppera 3122ece19502SDivya Koppera static int lan8814_config_init(struct phy_device *phydev) 3123ece19502SDivya Koppera { 3124a516b7f7SDivya Koppera struct kszphy_priv *lan8814 = phydev->priv; 3125ece19502SDivya Koppera int val; 3126ece19502SDivya Koppera 3127ece19502SDivya Koppera /* Reset the PHY */ 3128ece19502SDivya Koppera val = lanphy_read_page_reg(phydev, 4, LAN8814_QSGMII_SOFT_RESET); 3129ece19502SDivya Koppera val |= LAN8814_QSGMII_SOFT_RESET_BIT; 3130ece19502SDivya Koppera lanphy_write_page_reg(phydev, 4, LAN8814_QSGMII_SOFT_RESET, val); 3131ece19502SDivya Koppera 3132ece19502SDivya Koppera /* Disable ANEG with QSGMII PCS Host side */ 3133ece19502SDivya Koppera val = lanphy_read_page_reg(phydev, 5, LAN8814_QSGMII_PCS1G_ANEG_CONFIG); 3134ece19502SDivya Koppera val &= ~LAN8814_QSGMII_PCS1G_ANEG_CONFIG_ANEG_ENA; 3135ece19502SDivya Koppera lanphy_write_page_reg(phydev, 5, LAN8814_QSGMII_PCS1G_ANEG_CONFIG, val); 3136ece19502SDivya Koppera 3137ece19502SDivya Koppera /* MDI-X setting for swap A,B transmit */ 3138ece19502SDivya Koppera val = lanphy_read_page_reg(phydev, 2, LAN8814_ALIGN_SWAP); 3139ece19502SDivya Koppera val &= ~LAN8814_ALIGN_TX_A_B_SWAP_MASK; 3140ece19502SDivya Koppera val |= LAN8814_ALIGN_TX_A_B_SWAP; 3141ece19502SDivya Koppera lanphy_write_page_reg(phydev, 2, LAN8814_ALIGN_SWAP, val); 3142ece19502SDivya Koppera 3143a516b7f7SDivya Koppera if (lan8814->led_mode >= 0) 3144a516b7f7SDivya Koppera lan8814_setup_led(phydev, lan8814->led_mode); 3145a516b7f7SDivya Koppera 3146ece19502SDivya Koppera return 0; 3147ece19502SDivya Koppera } 3148ece19502SDivya Koppera 31494a4ce822SHoratiu Vultur /* It is expected that there will not be any 'lan8814_take_coma_mode' 31504a4ce822SHoratiu Vultur * function called in suspend. Because the GPIO line can be shared, so if one of 31514a4ce822SHoratiu Vultur * the phys goes back in coma mode, then all the other PHYs will go, which is 31524a4ce822SHoratiu Vultur * wrong. 31534a4ce822SHoratiu Vultur */ 3154738871b0SMichael Walle static int lan8814_release_coma_mode(struct phy_device *phydev) 3155738871b0SMichael Walle { 3156738871b0SMichael Walle struct gpio_desc *gpiod; 3157738871b0SMichael Walle 3158738871b0SMichael Walle gpiod = devm_gpiod_get_optional(&phydev->mdio.dev, "coma-mode", 31594a4ce822SHoratiu Vultur GPIOD_OUT_HIGH_OPEN_DRAIN | 31604a4ce822SHoratiu Vultur GPIOD_FLAGS_BIT_NONEXCLUSIVE); 3161738871b0SMichael Walle if (IS_ERR(gpiod)) 3162738871b0SMichael Walle return PTR_ERR(gpiod); 3163738871b0SMichael Walle 3164738871b0SMichael Walle gpiod_set_consumer_name(gpiod, "LAN8814 coma mode"); 3165738871b0SMichael Walle gpiod_set_value_cansleep(gpiod, 0); 3166738871b0SMichael Walle 3167738871b0SMichael Walle return 0; 3168738871b0SMichael Walle } 3169738871b0SMichael Walle 3170ece19502SDivya Koppera static int lan8814_probe(struct phy_device *phydev) 3171ece19502SDivya Koppera { 3172a516b7f7SDivya Koppera const struct kszphy_type *type = phydev->drv->driver_data; 3173ece19502SDivya Koppera struct kszphy_priv *priv; 3174ece19502SDivya Koppera u16 addr; 3175ece19502SDivya Koppera int err; 3176ece19502SDivya Koppera 3177ece19502SDivya Koppera priv = devm_kzalloc(&phydev->mdio.dev, sizeof(*priv), GFP_KERNEL); 3178ece19502SDivya Koppera if (!priv) 3179ece19502SDivya Koppera return -ENOMEM; 3180ece19502SDivya Koppera 3181ece19502SDivya Koppera phydev->priv = priv; 3182ece19502SDivya Koppera 3183a516b7f7SDivya Koppera priv->type = type; 3184a516b7f7SDivya Koppera 3185a516b7f7SDivya Koppera kszphy_parse_led_mode(phydev); 3186a516b7f7SDivya Koppera 3187ece19502SDivya Koppera /* Strap-in value for PHY address, below register read gives starting 3188ece19502SDivya Koppera * phy address value 3189ece19502SDivya Koppera */ 3190ece19502SDivya Koppera addr = lanphy_read_page_reg(phydev, 4, 0) & 0x1F; 3191ece19502SDivya Koppera devm_phy_package_join(&phydev->mdio.dev, phydev, 3192ece19502SDivya Koppera addr, sizeof(struct lan8814_shared_priv)); 3193ece19502SDivya Koppera 3194ece19502SDivya Koppera if (phy_package_init_once(phydev)) { 3195738871b0SMichael Walle err = lan8814_release_coma_mode(phydev); 3196738871b0SMichael Walle if (err) 3197738871b0SMichael Walle return err; 3198738871b0SMichael Walle 3199ece19502SDivya Koppera err = lan8814_ptp_probe_once(phydev); 3200ece19502SDivya Koppera if (err) 3201ece19502SDivya Koppera return err; 3202ece19502SDivya Koppera } 3203ece19502SDivya Koppera 3204ece19502SDivya Koppera lan8814_ptp_init(phydev); 3205ece19502SDivya Koppera 3206ece19502SDivya Koppera return 0; 3207ece19502SDivya Koppera } 3208ece19502SDivya Koppera 3209a8f1a19dSHoratiu Vultur #define LAN8841_MMD_TIMER_REG 0 3210a8f1a19dSHoratiu Vultur #define LAN8841_MMD0_REGISTER_17 17 3211a8f1a19dSHoratiu Vultur #define LAN8841_MMD0_REGISTER_17_DROP_OPT(x) ((x) & 0x3) 3212a8f1a19dSHoratiu Vultur #define LAN8841_MMD0_REGISTER_17_XMIT_TOG_TX_DIS BIT(3) 3213a8f1a19dSHoratiu Vultur #define LAN8841_OPERATION_MODE_STRAP_OVERRIDE_LOW_REG 2 3214a8f1a19dSHoratiu Vultur #define LAN8841_OPERATION_MODE_STRAP_OVERRIDE_LOW_REG_MAGJACK BIT(14) 3215a8f1a19dSHoratiu Vultur #define LAN8841_MMD_ANALOG_REG 28 3216a8f1a19dSHoratiu Vultur #define LAN8841_ANALOG_CONTROL_1 1 3217a8f1a19dSHoratiu Vultur #define LAN8841_ANALOG_CONTROL_1_PLL_TRIM(x) (((x) & 0x3) << 5) 3218a8f1a19dSHoratiu Vultur #define LAN8841_ANALOG_CONTROL_10 13 3219a8f1a19dSHoratiu Vultur #define LAN8841_ANALOG_CONTROL_10_PLL_DIV(x) ((x) & 0x3) 3220a8f1a19dSHoratiu Vultur #define LAN8841_ANALOG_CONTROL_11 14 3221a8f1a19dSHoratiu Vultur #define LAN8841_ANALOG_CONTROL_11_LDO_REF(x) (((x) & 0x7) << 12) 3222a8f1a19dSHoratiu Vultur #define LAN8841_TX_LOW_I_CH_C_D_POWER_MANAGMENT 69 3223a8f1a19dSHoratiu Vultur #define LAN8841_TX_LOW_I_CH_C_D_POWER_MANAGMENT_VAL 0xbffc 3224a8f1a19dSHoratiu Vultur #define LAN8841_BTRX_POWER_DOWN 70 3225a8f1a19dSHoratiu Vultur #define LAN8841_BTRX_POWER_DOWN_QBIAS_CH_A BIT(0) 3226a8f1a19dSHoratiu Vultur #define LAN8841_BTRX_POWER_DOWN_BTRX_CH_A BIT(1) 3227a8f1a19dSHoratiu Vultur #define LAN8841_BTRX_POWER_DOWN_QBIAS_CH_B BIT(2) 3228a8f1a19dSHoratiu Vultur #define LAN8841_BTRX_POWER_DOWN_BTRX_CH_B BIT(3) 3229a8f1a19dSHoratiu Vultur #define LAN8841_BTRX_POWER_DOWN_BTRX_CH_C BIT(5) 3230a8f1a19dSHoratiu Vultur #define LAN8841_BTRX_POWER_DOWN_BTRX_CH_D BIT(7) 3231a8f1a19dSHoratiu Vultur #define LAN8841_ADC_CHANNEL_MASK 198 3232cafc3662SHoratiu Vultur #define LAN8841_PTP_RX_PARSE_L2_ADDR_EN 370 3233cafc3662SHoratiu Vultur #define LAN8841_PTP_RX_PARSE_IP_ADDR_EN 371 3234cafc3662SHoratiu Vultur #define LAN8841_PTP_TX_PARSE_L2_ADDR_EN 434 3235cafc3662SHoratiu Vultur #define LAN8841_PTP_TX_PARSE_IP_ADDR_EN 435 3236cafc3662SHoratiu Vultur #define LAN8841_PTP_CMD_CTL 256 3237cafc3662SHoratiu Vultur #define LAN8841_PTP_CMD_CTL_PTP_ENABLE BIT(2) 3238cafc3662SHoratiu Vultur #define LAN8841_PTP_CMD_CTL_PTP_DISABLE BIT(1) 3239cafc3662SHoratiu Vultur #define LAN8841_PTP_CMD_CTL_PTP_RESET BIT(0) 3240cafc3662SHoratiu Vultur #define LAN8841_PTP_RX_PARSE_CONFIG 368 3241cafc3662SHoratiu Vultur #define LAN8841_PTP_TX_PARSE_CONFIG 432 3242a8f1a19dSHoratiu Vultur 3243a8f1a19dSHoratiu Vultur static int lan8841_config_init(struct phy_device *phydev) 3244a8f1a19dSHoratiu Vultur { 3245a8f1a19dSHoratiu Vultur int ret; 3246a8f1a19dSHoratiu Vultur 3247a8f1a19dSHoratiu Vultur ret = ksz9131_config_init(phydev); 3248a8f1a19dSHoratiu Vultur if (ret) 3249a8f1a19dSHoratiu Vultur return ret; 3250a8f1a19dSHoratiu Vultur 3251cafc3662SHoratiu Vultur /* Initialize the HW by resetting everything */ 3252cafc3662SHoratiu Vultur phy_modify_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG, 3253cafc3662SHoratiu Vultur LAN8841_PTP_CMD_CTL, 3254cafc3662SHoratiu Vultur LAN8841_PTP_CMD_CTL_PTP_RESET, 3255cafc3662SHoratiu Vultur LAN8841_PTP_CMD_CTL_PTP_RESET); 3256cafc3662SHoratiu Vultur 3257cafc3662SHoratiu Vultur phy_modify_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG, 3258cafc3662SHoratiu Vultur LAN8841_PTP_CMD_CTL, 3259cafc3662SHoratiu Vultur LAN8841_PTP_CMD_CTL_PTP_ENABLE, 3260cafc3662SHoratiu Vultur LAN8841_PTP_CMD_CTL_PTP_ENABLE); 3261cafc3662SHoratiu Vultur 3262cafc3662SHoratiu Vultur /* Don't process any frames */ 3263cafc3662SHoratiu Vultur phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG, 3264cafc3662SHoratiu Vultur LAN8841_PTP_RX_PARSE_CONFIG, 0); 3265cafc3662SHoratiu Vultur phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG, 3266cafc3662SHoratiu Vultur LAN8841_PTP_TX_PARSE_CONFIG, 0); 3267cafc3662SHoratiu Vultur phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG, 3268cafc3662SHoratiu Vultur LAN8841_PTP_TX_PARSE_L2_ADDR_EN, 0); 3269cafc3662SHoratiu Vultur phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG, 3270cafc3662SHoratiu Vultur LAN8841_PTP_RX_PARSE_L2_ADDR_EN, 0); 3271cafc3662SHoratiu Vultur phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG, 3272cafc3662SHoratiu Vultur LAN8841_PTP_TX_PARSE_IP_ADDR_EN, 0); 3273cafc3662SHoratiu Vultur phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG, 3274cafc3662SHoratiu Vultur LAN8841_PTP_RX_PARSE_IP_ADDR_EN, 0); 3275cafc3662SHoratiu Vultur 3276a8f1a19dSHoratiu Vultur /* 100BT Clause 40 improvenent errata */ 3277a8f1a19dSHoratiu Vultur phy_write_mmd(phydev, LAN8841_MMD_ANALOG_REG, 3278a8f1a19dSHoratiu Vultur LAN8841_ANALOG_CONTROL_1, 3279a8f1a19dSHoratiu Vultur LAN8841_ANALOG_CONTROL_1_PLL_TRIM(0x2)); 3280a8f1a19dSHoratiu Vultur phy_write_mmd(phydev, LAN8841_MMD_ANALOG_REG, 3281a8f1a19dSHoratiu Vultur LAN8841_ANALOG_CONTROL_10, 3282a8f1a19dSHoratiu Vultur LAN8841_ANALOG_CONTROL_10_PLL_DIV(0x1)); 3283a8f1a19dSHoratiu Vultur 3284a8f1a19dSHoratiu Vultur /* 10M/100M Ethernet Signal Tuning Errata for Shorted-Center Tap 3285a8f1a19dSHoratiu Vultur * Magnetics 3286a8f1a19dSHoratiu Vultur */ 3287a8f1a19dSHoratiu Vultur ret = phy_read_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG, 3288a8f1a19dSHoratiu Vultur LAN8841_OPERATION_MODE_STRAP_OVERRIDE_LOW_REG); 3289a8f1a19dSHoratiu Vultur if (ret & LAN8841_OPERATION_MODE_STRAP_OVERRIDE_LOW_REG_MAGJACK) { 3290a8f1a19dSHoratiu Vultur phy_write_mmd(phydev, LAN8841_MMD_ANALOG_REG, 3291a8f1a19dSHoratiu Vultur LAN8841_TX_LOW_I_CH_C_D_POWER_MANAGMENT, 3292a8f1a19dSHoratiu Vultur LAN8841_TX_LOW_I_CH_C_D_POWER_MANAGMENT_VAL); 3293a8f1a19dSHoratiu Vultur phy_write_mmd(phydev, LAN8841_MMD_ANALOG_REG, 3294a8f1a19dSHoratiu Vultur LAN8841_BTRX_POWER_DOWN, 3295a8f1a19dSHoratiu Vultur LAN8841_BTRX_POWER_DOWN_QBIAS_CH_A | 3296a8f1a19dSHoratiu Vultur LAN8841_BTRX_POWER_DOWN_BTRX_CH_A | 3297a8f1a19dSHoratiu Vultur LAN8841_BTRX_POWER_DOWN_QBIAS_CH_B | 3298a8f1a19dSHoratiu Vultur LAN8841_BTRX_POWER_DOWN_BTRX_CH_B | 3299a8f1a19dSHoratiu Vultur LAN8841_BTRX_POWER_DOWN_BTRX_CH_C | 3300a8f1a19dSHoratiu Vultur LAN8841_BTRX_POWER_DOWN_BTRX_CH_D); 3301a8f1a19dSHoratiu Vultur } 3302a8f1a19dSHoratiu Vultur 3303a8f1a19dSHoratiu Vultur /* LDO Adjustment errata */ 3304a8f1a19dSHoratiu Vultur phy_write_mmd(phydev, LAN8841_MMD_ANALOG_REG, 3305a8f1a19dSHoratiu Vultur LAN8841_ANALOG_CONTROL_11, 3306a8f1a19dSHoratiu Vultur LAN8841_ANALOG_CONTROL_11_LDO_REF(1)); 3307a8f1a19dSHoratiu Vultur 3308a8f1a19dSHoratiu Vultur /* 100BT RGMII latency tuning errata */ 3309a8f1a19dSHoratiu Vultur phy_write_mmd(phydev, MDIO_MMD_PMAPMD, 3310a8f1a19dSHoratiu Vultur LAN8841_ADC_CHANNEL_MASK, 0x0); 3311a8f1a19dSHoratiu Vultur phy_write_mmd(phydev, LAN8841_MMD_TIMER_REG, 3312a8f1a19dSHoratiu Vultur LAN8841_MMD0_REGISTER_17, 3313a8f1a19dSHoratiu Vultur LAN8841_MMD0_REGISTER_17_DROP_OPT(2) | 3314a8f1a19dSHoratiu Vultur LAN8841_MMD0_REGISTER_17_XMIT_TOG_TX_DIS); 3315a8f1a19dSHoratiu Vultur 3316a8f1a19dSHoratiu Vultur return 0; 3317a8f1a19dSHoratiu Vultur } 3318a8f1a19dSHoratiu Vultur 3319a8f1a19dSHoratiu Vultur #define LAN8841_OUTPUT_CTRL 25 3320a8f1a19dSHoratiu Vultur #define LAN8841_OUTPUT_CTRL_INT_BUFFER BIT(14) 3321cafc3662SHoratiu Vultur #define LAN8841_INT_PTP BIT(9) 3322a8f1a19dSHoratiu Vultur 3323a8f1a19dSHoratiu Vultur static int lan8841_config_intr(struct phy_device *phydev) 3324a8f1a19dSHoratiu Vultur { 3325a8f1a19dSHoratiu Vultur int err; 3326a8f1a19dSHoratiu Vultur 3327a8f1a19dSHoratiu Vultur phy_modify(phydev, LAN8841_OUTPUT_CTRL, 3328a8f1a19dSHoratiu Vultur LAN8841_OUTPUT_CTRL_INT_BUFFER, 0); 3329a8f1a19dSHoratiu Vultur 3330a8f1a19dSHoratiu Vultur if (phydev->interrupts == PHY_INTERRUPT_ENABLED) { 3331a8f1a19dSHoratiu Vultur err = phy_read(phydev, LAN8814_INTS); 3332a8f1a19dSHoratiu Vultur if (err) 3333a8f1a19dSHoratiu Vultur return err; 3334a8f1a19dSHoratiu Vultur 3335cafc3662SHoratiu Vultur /* Enable / disable interrupts. It is OK to enable PTP interrupt 3336cafc3662SHoratiu Vultur * even if it PTP is not enabled. Because the underneath blocks 3337cafc3662SHoratiu Vultur * will not enable the PTP so we will never get the PTP 3338cafc3662SHoratiu Vultur * interrupt. 3339cafc3662SHoratiu Vultur */ 3340a8f1a19dSHoratiu Vultur err = phy_write(phydev, LAN8814_INTC, 3341cafc3662SHoratiu Vultur LAN8814_INT_LINK | LAN8841_INT_PTP); 3342a8f1a19dSHoratiu Vultur } else { 3343a8f1a19dSHoratiu Vultur err = phy_write(phydev, LAN8814_INTC, 0); 3344a8f1a19dSHoratiu Vultur if (err) 3345a8f1a19dSHoratiu Vultur return err; 3346a8f1a19dSHoratiu Vultur 3347a8f1a19dSHoratiu Vultur err = phy_read(phydev, LAN8814_INTS); 3348a8f1a19dSHoratiu Vultur } 3349a8f1a19dSHoratiu Vultur 3350a8f1a19dSHoratiu Vultur return err; 3351a8f1a19dSHoratiu Vultur } 3352a8f1a19dSHoratiu Vultur 3353cafc3662SHoratiu Vultur #define LAN8841_PTP_TX_EGRESS_SEC_LO 453 3354cafc3662SHoratiu Vultur #define LAN8841_PTP_TX_EGRESS_SEC_HI 452 3355cafc3662SHoratiu Vultur #define LAN8841_PTP_TX_EGRESS_NS_LO 451 3356cafc3662SHoratiu Vultur #define LAN8841_PTP_TX_EGRESS_NS_HI 450 3357cafc3662SHoratiu Vultur #define LAN8841_PTP_TX_EGRESS_NSEC_HI_VALID BIT(15) 3358cafc3662SHoratiu Vultur #define LAN8841_PTP_TX_MSG_HEADER2 455 3359cafc3662SHoratiu Vultur 3360cafc3662SHoratiu Vultur static bool lan8841_ptp_get_tx_ts(struct kszphy_ptp_priv *ptp_priv, 3361cafc3662SHoratiu Vultur u32 *sec, u32 *nsec, u16 *seq) 3362cafc3662SHoratiu Vultur { 3363cafc3662SHoratiu Vultur struct phy_device *phydev = ptp_priv->phydev; 3364cafc3662SHoratiu Vultur 3365cafc3662SHoratiu Vultur *nsec = phy_read_mmd(phydev, 2, LAN8841_PTP_TX_EGRESS_NS_HI); 3366cafc3662SHoratiu Vultur if (!(*nsec & LAN8841_PTP_TX_EGRESS_NSEC_HI_VALID)) 3367cafc3662SHoratiu Vultur return false; 3368cafc3662SHoratiu Vultur 3369cafc3662SHoratiu Vultur *nsec = ((*nsec & 0x3fff) << 16); 3370cafc3662SHoratiu Vultur *nsec = *nsec | phy_read_mmd(phydev, 2, LAN8841_PTP_TX_EGRESS_NS_LO); 3371cafc3662SHoratiu Vultur 3372cafc3662SHoratiu Vultur *sec = phy_read_mmd(phydev, 2, LAN8841_PTP_TX_EGRESS_SEC_HI); 3373cafc3662SHoratiu Vultur *sec = *sec << 16; 3374cafc3662SHoratiu Vultur *sec = *sec | phy_read_mmd(phydev, 2, LAN8841_PTP_TX_EGRESS_SEC_LO); 3375cafc3662SHoratiu Vultur 3376cafc3662SHoratiu Vultur *seq = phy_read_mmd(phydev, 2, LAN8841_PTP_TX_MSG_HEADER2); 3377cafc3662SHoratiu Vultur 3378cafc3662SHoratiu Vultur return true; 3379cafc3662SHoratiu Vultur } 3380cafc3662SHoratiu Vultur 3381cafc3662SHoratiu Vultur static void lan8841_ptp_process_tx_ts(struct kszphy_ptp_priv *ptp_priv) 3382cafc3662SHoratiu Vultur { 3383cafc3662SHoratiu Vultur u32 sec, nsec; 3384cafc3662SHoratiu Vultur u16 seq; 3385cafc3662SHoratiu Vultur 3386cafc3662SHoratiu Vultur while (lan8841_ptp_get_tx_ts(ptp_priv, &sec, &nsec, &seq)) 3387cafc3662SHoratiu Vultur lan8814_match_tx_skb(ptp_priv, sec, nsec, seq); 3388cafc3662SHoratiu Vultur } 3389cafc3662SHoratiu Vultur 3390cafc3662SHoratiu Vultur #define LAN8841_PTP_RX_INGRESS_SEC_LO 389 3391cafc3662SHoratiu Vultur #define LAN8841_PTP_RX_INGRESS_SEC_HI 388 3392cafc3662SHoratiu Vultur #define LAN8841_PTP_RX_INGRESS_NS_LO 387 3393cafc3662SHoratiu Vultur #define LAN8841_PTP_RX_INGRESS_NS_HI 386 3394cafc3662SHoratiu Vultur #define LAN8841_PTP_RX_INGRESS_NSEC_HI_VALID BIT(15) 3395cafc3662SHoratiu Vultur #define LAN8841_PTP_RX_MSG_HEADER2 391 3396cafc3662SHoratiu Vultur 3397cafc3662SHoratiu Vultur static struct lan8814_ptp_rx_ts *lan8841_ptp_get_rx_ts(struct kszphy_ptp_priv *ptp_priv) 3398cafc3662SHoratiu Vultur { 3399cafc3662SHoratiu Vultur struct phy_device *phydev = ptp_priv->phydev; 3400cafc3662SHoratiu Vultur struct lan8814_ptp_rx_ts *rx_ts; 3401cafc3662SHoratiu Vultur u32 sec, nsec; 3402cafc3662SHoratiu Vultur u16 seq; 3403cafc3662SHoratiu Vultur 3404cafc3662SHoratiu Vultur nsec = phy_read_mmd(phydev, 2, LAN8841_PTP_RX_INGRESS_NS_HI); 3405cafc3662SHoratiu Vultur if (!(nsec & LAN8841_PTP_RX_INGRESS_NSEC_HI_VALID)) 3406cafc3662SHoratiu Vultur return NULL; 3407cafc3662SHoratiu Vultur 3408cafc3662SHoratiu Vultur nsec = ((nsec & 0x3fff) << 16); 3409cafc3662SHoratiu Vultur nsec = nsec | phy_read_mmd(phydev, 2, LAN8841_PTP_RX_INGRESS_NS_LO); 3410cafc3662SHoratiu Vultur 3411cafc3662SHoratiu Vultur sec = phy_read_mmd(phydev, 2, LAN8841_PTP_RX_INGRESS_SEC_HI); 3412cafc3662SHoratiu Vultur sec = sec << 16; 3413cafc3662SHoratiu Vultur sec = sec | phy_read_mmd(phydev, 2, LAN8841_PTP_RX_INGRESS_SEC_LO); 3414cafc3662SHoratiu Vultur 3415cafc3662SHoratiu Vultur seq = phy_read_mmd(phydev, 2, LAN8841_PTP_RX_MSG_HEADER2); 3416cafc3662SHoratiu Vultur 3417cafc3662SHoratiu Vultur rx_ts = kzalloc(sizeof(*rx_ts), GFP_KERNEL); 3418cafc3662SHoratiu Vultur if (!rx_ts) 3419cafc3662SHoratiu Vultur return NULL; 3420cafc3662SHoratiu Vultur 3421cafc3662SHoratiu Vultur rx_ts->seconds = sec; 3422cafc3662SHoratiu Vultur rx_ts->nsec = nsec; 3423cafc3662SHoratiu Vultur rx_ts->seq_id = seq; 3424cafc3662SHoratiu Vultur 3425cafc3662SHoratiu Vultur return rx_ts; 3426cafc3662SHoratiu Vultur } 3427cafc3662SHoratiu Vultur 3428cafc3662SHoratiu Vultur static void lan8841_ptp_process_rx_ts(struct kszphy_ptp_priv *ptp_priv) 3429cafc3662SHoratiu Vultur { 3430cafc3662SHoratiu Vultur struct lan8814_ptp_rx_ts *rx_ts; 3431cafc3662SHoratiu Vultur 3432cafc3662SHoratiu Vultur while ((rx_ts = lan8841_ptp_get_rx_ts(ptp_priv)) != NULL) 3433cafc3662SHoratiu Vultur lan8814_match_rx_ts(ptp_priv, rx_ts); 3434cafc3662SHoratiu Vultur } 3435cafc3662SHoratiu Vultur 3436cafc3662SHoratiu Vultur #define LAN8841_PTP_INT_STS 259 3437cafc3662SHoratiu Vultur #define LAN8841_PTP_INT_STS_PTP_TX_TS_OVRFL_INT BIT(13) 3438cafc3662SHoratiu Vultur #define LAN8841_PTP_INT_STS_PTP_TX_TS_INT BIT(12) 3439cafc3662SHoratiu Vultur #define LAN8841_PTP_INT_STS_PTP_RX_TS_OVRFL_INT BIT(9) 3440cafc3662SHoratiu Vultur #define LAN8841_PTP_INT_STS_PTP_RX_TS_INT BIT(8) 3441cafc3662SHoratiu Vultur 3442cafc3662SHoratiu Vultur static void lan8841_ptp_flush_fifo(struct kszphy_ptp_priv *ptp_priv, bool egress) 3443cafc3662SHoratiu Vultur { 3444cafc3662SHoratiu Vultur struct phy_device *phydev = ptp_priv->phydev; 3445cafc3662SHoratiu Vultur int i; 3446cafc3662SHoratiu Vultur 3447cafc3662SHoratiu Vultur for (i = 0; i < FIFO_SIZE; ++i) 3448cafc3662SHoratiu Vultur phy_read_mmd(phydev, 2, 3449cafc3662SHoratiu Vultur egress ? LAN8841_PTP_TX_MSG_HEADER2 : 3450cafc3662SHoratiu Vultur LAN8841_PTP_RX_MSG_HEADER2); 3451cafc3662SHoratiu Vultur 3452cafc3662SHoratiu Vultur phy_read_mmd(phydev, 2, LAN8841_PTP_INT_STS); 3453cafc3662SHoratiu Vultur } 3454cafc3662SHoratiu Vultur 3455cafc3662SHoratiu Vultur static void lan8841_handle_ptp_interrupt(struct phy_device *phydev) 3456cafc3662SHoratiu Vultur { 3457cafc3662SHoratiu Vultur struct kszphy_priv *priv = phydev->priv; 3458cafc3662SHoratiu Vultur struct kszphy_ptp_priv *ptp_priv = &priv->ptp_priv; 3459cafc3662SHoratiu Vultur u16 status; 3460cafc3662SHoratiu Vultur 3461cafc3662SHoratiu Vultur do { 3462cafc3662SHoratiu Vultur status = phy_read_mmd(phydev, 2, LAN8841_PTP_INT_STS); 3463cafc3662SHoratiu Vultur if (status & LAN8841_PTP_INT_STS_PTP_TX_TS_INT) 3464cafc3662SHoratiu Vultur lan8841_ptp_process_tx_ts(ptp_priv); 3465cafc3662SHoratiu Vultur 3466cafc3662SHoratiu Vultur if (status & LAN8841_PTP_INT_STS_PTP_RX_TS_INT) 3467cafc3662SHoratiu Vultur lan8841_ptp_process_rx_ts(ptp_priv); 3468cafc3662SHoratiu Vultur 3469cafc3662SHoratiu Vultur if (status & LAN8841_PTP_INT_STS_PTP_TX_TS_OVRFL_INT) { 3470cafc3662SHoratiu Vultur lan8841_ptp_flush_fifo(ptp_priv, true); 3471cafc3662SHoratiu Vultur skb_queue_purge(&ptp_priv->tx_queue); 3472cafc3662SHoratiu Vultur } 3473cafc3662SHoratiu Vultur 3474cafc3662SHoratiu Vultur if (status & LAN8841_PTP_INT_STS_PTP_RX_TS_OVRFL_INT) { 3475cafc3662SHoratiu Vultur lan8841_ptp_flush_fifo(ptp_priv, false); 3476cafc3662SHoratiu Vultur skb_queue_purge(&ptp_priv->rx_queue); 3477cafc3662SHoratiu Vultur } 3478cafc3662SHoratiu Vultur 3479cafc3662SHoratiu Vultur } while (status); 3480cafc3662SHoratiu Vultur } 3481cafc3662SHoratiu Vultur 3482cafc3662SHoratiu Vultur #define LAN8841_INTS_PTP BIT(9) 3483cafc3662SHoratiu Vultur 3484a8f1a19dSHoratiu Vultur static irqreturn_t lan8841_handle_interrupt(struct phy_device *phydev) 3485a8f1a19dSHoratiu Vultur { 3486cafc3662SHoratiu Vultur irqreturn_t ret = IRQ_NONE; 3487a8f1a19dSHoratiu Vultur int irq_status; 3488a8f1a19dSHoratiu Vultur 3489a8f1a19dSHoratiu Vultur irq_status = phy_read(phydev, LAN8814_INTS); 3490a8f1a19dSHoratiu Vultur if (irq_status < 0) { 3491a8f1a19dSHoratiu Vultur phy_error(phydev); 3492a8f1a19dSHoratiu Vultur return IRQ_NONE; 3493a8f1a19dSHoratiu Vultur } 3494a8f1a19dSHoratiu Vultur 3495a8f1a19dSHoratiu Vultur if (irq_status & LAN8814_INT_LINK) { 3496a8f1a19dSHoratiu Vultur phy_trigger_machine(phydev); 3497cafc3662SHoratiu Vultur ret = IRQ_HANDLED; 3498a8f1a19dSHoratiu Vultur } 3499a8f1a19dSHoratiu Vultur 3500cafc3662SHoratiu Vultur if (irq_status & LAN8841_INTS_PTP) { 3501cafc3662SHoratiu Vultur lan8841_handle_ptp_interrupt(phydev); 3502cafc3662SHoratiu Vultur ret = IRQ_HANDLED; 3503a8f1a19dSHoratiu Vultur } 3504a8f1a19dSHoratiu Vultur 3505cafc3662SHoratiu Vultur return ret; 3506cafc3662SHoratiu Vultur } 3507cafc3662SHoratiu Vultur 3508cafc3662SHoratiu Vultur static int lan8841_ts_info(struct mii_timestamper *mii_ts, 3509cafc3662SHoratiu Vultur struct ethtool_ts_info *info) 3510cafc3662SHoratiu Vultur { 3511cafc3662SHoratiu Vultur struct kszphy_ptp_priv *ptp_priv; 3512cafc3662SHoratiu Vultur 3513cafc3662SHoratiu Vultur ptp_priv = container_of(mii_ts, struct kszphy_ptp_priv, mii_ts); 3514cafc3662SHoratiu Vultur 3515cafc3662SHoratiu Vultur info->phc_index = ptp_priv->ptp_clock ? 3516cafc3662SHoratiu Vultur ptp_clock_index(ptp_priv->ptp_clock) : -1; 3517cafc3662SHoratiu Vultur if (info->phc_index == -1) { 3518cafc3662SHoratiu Vultur info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE | 3519cafc3662SHoratiu Vultur SOF_TIMESTAMPING_RX_SOFTWARE | 3520cafc3662SHoratiu Vultur SOF_TIMESTAMPING_SOFTWARE; 3521cafc3662SHoratiu Vultur return 0; 3522cafc3662SHoratiu Vultur } 3523cafc3662SHoratiu Vultur 3524cafc3662SHoratiu Vultur info->so_timestamping = SOF_TIMESTAMPING_TX_HARDWARE | 3525cafc3662SHoratiu Vultur SOF_TIMESTAMPING_RX_HARDWARE | 3526cafc3662SHoratiu Vultur SOF_TIMESTAMPING_RAW_HARDWARE; 3527cafc3662SHoratiu Vultur 3528cafc3662SHoratiu Vultur info->tx_types = (1 << HWTSTAMP_TX_OFF) | 3529cafc3662SHoratiu Vultur (1 << HWTSTAMP_TX_ON) | 3530cafc3662SHoratiu Vultur (1 << HWTSTAMP_TX_ONESTEP_SYNC); 3531cafc3662SHoratiu Vultur 3532cafc3662SHoratiu Vultur info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) | 3533cafc3662SHoratiu Vultur (1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT) | 3534cafc3662SHoratiu Vultur (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) | 3535cafc3662SHoratiu Vultur (1 << HWTSTAMP_FILTER_PTP_V2_EVENT); 3536cafc3662SHoratiu Vultur 3537cafc3662SHoratiu Vultur return 0; 3538cafc3662SHoratiu Vultur } 3539cafc3662SHoratiu Vultur 3540cafc3662SHoratiu Vultur #define LAN8841_PTP_INT_EN 260 3541cafc3662SHoratiu Vultur #define LAN8841_PTP_INT_EN_PTP_TX_TS_OVRFL_EN BIT(13) 3542cafc3662SHoratiu Vultur #define LAN8841_PTP_INT_EN_PTP_TX_TS_EN BIT(12) 3543cafc3662SHoratiu Vultur #define LAN8841_PTP_INT_EN_PTP_RX_TS_OVRFL_EN BIT(9) 3544cafc3662SHoratiu Vultur #define LAN8841_PTP_INT_EN_PTP_RX_TS_EN BIT(8) 3545cafc3662SHoratiu Vultur 3546cafc3662SHoratiu Vultur static void lan8841_ptp_enable_int(struct kszphy_ptp_priv *ptp_priv, 3547cafc3662SHoratiu Vultur bool enable) 3548cafc3662SHoratiu Vultur { 3549cafc3662SHoratiu Vultur struct phy_device *phydev = ptp_priv->phydev; 3550cafc3662SHoratiu Vultur 3551cafc3662SHoratiu Vultur if (enable) 3552cafc3662SHoratiu Vultur /* Enable interrupts */ 3553cafc3662SHoratiu Vultur phy_modify_mmd(phydev, 2, LAN8841_PTP_INT_EN, 3554cafc3662SHoratiu Vultur LAN8841_PTP_INT_EN_PTP_TX_TS_OVRFL_EN | 3555cafc3662SHoratiu Vultur LAN8841_PTP_INT_EN_PTP_RX_TS_OVRFL_EN | 3556cafc3662SHoratiu Vultur LAN8841_PTP_INT_EN_PTP_TX_TS_EN | 3557cafc3662SHoratiu Vultur LAN8841_PTP_INT_EN_PTP_RX_TS_EN, 3558cafc3662SHoratiu Vultur LAN8841_PTP_INT_EN_PTP_TX_TS_OVRFL_EN | 3559cafc3662SHoratiu Vultur LAN8841_PTP_INT_EN_PTP_RX_TS_OVRFL_EN | 3560cafc3662SHoratiu Vultur LAN8841_PTP_INT_EN_PTP_TX_TS_EN | 3561cafc3662SHoratiu Vultur LAN8841_PTP_INT_EN_PTP_RX_TS_EN); 3562cafc3662SHoratiu Vultur else 3563cafc3662SHoratiu Vultur /* Disable interrupts */ 3564cafc3662SHoratiu Vultur phy_modify_mmd(phydev, 2, LAN8841_PTP_INT_EN, 3565cafc3662SHoratiu Vultur LAN8841_PTP_INT_EN_PTP_TX_TS_OVRFL_EN | 3566cafc3662SHoratiu Vultur LAN8841_PTP_INT_EN_PTP_RX_TS_OVRFL_EN | 3567cafc3662SHoratiu Vultur LAN8841_PTP_INT_EN_PTP_TX_TS_EN | 3568cafc3662SHoratiu Vultur LAN8841_PTP_INT_EN_PTP_RX_TS_EN, 0); 3569cafc3662SHoratiu Vultur } 3570cafc3662SHoratiu Vultur 3571cafc3662SHoratiu Vultur #define LAN8841_PTP_RX_TIMESTAMP_EN 379 3572cafc3662SHoratiu Vultur #define LAN8841_PTP_TX_TIMESTAMP_EN 443 3573cafc3662SHoratiu Vultur #define LAN8841_PTP_TX_MOD 445 3574cafc3662SHoratiu Vultur 3575cafc3662SHoratiu Vultur static int lan8841_hwtstamp(struct mii_timestamper *mii_ts, struct ifreq *ifr) 3576cafc3662SHoratiu Vultur { 3577cafc3662SHoratiu Vultur struct kszphy_ptp_priv *ptp_priv = container_of(mii_ts, struct kszphy_ptp_priv, mii_ts); 3578cafc3662SHoratiu Vultur struct phy_device *phydev = ptp_priv->phydev; 3579cafc3662SHoratiu Vultur struct lan8814_ptp_rx_ts *rx_ts, *tmp; 3580cafc3662SHoratiu Vultur struct hwtstamp_config config; 3581cafc3662SHoratiu Vultur int txcfg = 0, rxcfg = 0; 3582cafc3662SHoratiu Vultur int pkt_ts_enable; 3583cafc3662SHoratiu Vultur 3584cafc3662SHoratiu Vultur if (copy_from_user(&config, ifr->ifr_data, sizeof(config))) 3585cafc3662SHoratiu Vultur return -EFAULT; 3586cafc3662SHoratiu Vultur 3587cafc3662SHoratiu Vultur ptp_priv->hwts_tx_type = config.tx_type; 3588cafc3662SHoratiu Vultur ptp_priv->rx_filter = config.rx_filter; 3589cafc3662SHoratiu Vultur 3590cafc3662SHoratiu Vultur switch (config.rx_filter) { 3591cafc3662SHoratiu Vultur case HWTSTAMP_FILTER_NONE: 3592cafc3662SHoratiu Vultur ptp_priv->layer = 0; 3593cafc3662SHoratiu Vultur ptp_priv->version = 0; 3594cafc3662SHoratiu Vultur break; 3595cafc3662SHoratiu Vultur case HWTSTAMP_FILTER_PTP_V2_L4_EVENT: 3596cafc3662SHoratiu Vultur case HWTSTAMP_FILTER_PTP_V2_L4_SYNC: 3597cafc3662SHoratiu Vultur case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ: 3598cafc3662SHoratiu Vultur ptp_priv->layer = PTP_CLASS_L4; 3599cafc3662SHoratiu Vultur ptp_priv->version = PTP_CLASS_V2; 3600cafc3662SHoratiu Vultur break; 3601cafc3662SHoratiu Vultur case HWTSTAMP_FILTER_PTP_V2_L2_EVENT: 3602cafc3662SHoratiu Vultur case HWTSTAMP_FILTER_PTP_V2_L2_SYNC: 3603cafc3662SHoratiu Vultur case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ: 3604cafc3662SHoratiu Vultur ptp_priv->layer = PTP_CLASS_L2; 3605cafc3662SHoratiu Vultur ptp_priv->version = PTP_CLASS_V2; 3606cafc3662SHoratiu Vultur break; 3607cafc3662SHoratiu Vultur case HWTSTAMP_FILTER_PTP_V2_EVENT: 3608cafc3662SHoratiu Vultur case HWTSTAMP_FILTER_PTP_V2_SYNC: 3609cafc3662SHoratiu Vultur case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ: 3610cafc3662SHoratiu Vultur ptp_priv->layer = PTP_CLASS_L4 | PTP_CLASS_L2; 3611cafc3662SHoratiu Vultur ptp_priv->version = PTP_CLASS_V2; 3612cafc3662SHoratiu Vultur break; 3613cafc3662SHoratiu Vultur default: 3614cafc3662SHoratiu Vultur return -ERANGE; 3615cafc3662SHoratiu Vultur } 3616cafc3662SHoratiu Vultur 3617cafc3662SHoratiu Vultur /* Setup parsing of the frames and enable the timestamping for ptp 3618cafc3662SHoratiu Vultur * frames 3619cafc3662SHoratiu Vultur */ 3620cafc3662SHoratiu Vultur if (ptp_priv->layer & PTP_CLASS_L2) { 3621cafc3662SHoratiu Vultur rxcfg |= PTP_RX_PARSE_CONFIG_LAYER2_EN_; 3622cafc3662SHoratiu Vultur txcfg |= PTP_TX_PARSE_CONFIG_LAYER2_EN_; 3623cafc3662SHoratiu Vultur } else if (ptp_priv->layer & PTP_CLASS_L4) { 3624cafc3662SHoratiu Vultur rxcfg |= PTP_RX_PARSE_CONFIG_IPV4_EN_ | PTP_RX_PARSE_CONFIG_IPV6_EN_; 3625cafc3662SHoratiu Vultur txcfg |= PTP_TX_PARSE_CONFIG_IPV4_EN_ | PTP_TX_PARSE_CONFIG_IPV6_EN_; 3626cafc3662SHoratiu Vultur } 3627cafc3662SHoratiu Vultur 3628cafc3662SHoratiu Vultur phy_write_mmd(phydev, 2, LAN8841_PTP_RX_PARSE_CONFIG, rxcfg); 3629cafc3662SHoratiu Vultur phy_write_mmd(phydev, 2, LAN8841_PTP_TX_PARSE_CONFIG, txcfg); 3630cafc3662SHoratiu Vultur 3631cafc3662SHoratiu Vultur pkt_ts_enable = PTP_TIMESTAMP_EN_SYNC_ | PTP_TIMESTAMP_EN_DREQ_ | 3632cafc3662SHoratiu Vultur PTP_TIMESTAMP_EN_PDREQ_ | PTP_TIMESTAMP_EN_PDRES_; 3633cafc3662SHoratiu Vultur phy_write_mmd(phydev, 2, LAN8841_PTP_RX_TIMESTAMP_EN, pkt_ts_enable); 3634cafc3662SHoratiu Vultur phy_write_mmd(phydev, 2, LAN8841_PTP_TX_TIMESTAMP_EN, pkt_ts_enable); 3635cafc3662SHoratiu Vultur 3636cafc3662SHoratiu Vultur /* Enable / disable of the TX timestamp in the SYNC frames */ 3637cafc3662SHoratiu Vultur phy_modify_mmd(phydev, 2, LAN8841_PTP_TX_MOD, 3638cafc3662SHoratiu Vultur PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_, 3639cafc3662SHoratiu Vultur ptp_priv->hwts_tx_type == HWTSTAMP_TX_ONESTEP_SYNC ? 3640cafc3662SHoratiu Vultur PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_ : 0); 3641cafc3662SHoratiu Vultur 3642cafc3662SHoratiu Vultur /* Now enable/disable the timestamping */ 3643cafc3662SHoratiu Vultur lan8841_ptp_enable_int(ptp_priv, 3644cafc3662SHoratiu Vultur config.rx_filter != HWTSTAMP_FILTER_NONE); 3645cafc3662SHoratiu Vultur 3646cafc3662SHoratiu Vultur /* In case of multiple starts and stops, these needs to be cleared */ 3647cafc3662SHoratiu Vultur list_for_each_entry_safe(rx_ts, tmp, &ptp_priv->rx_ts_list, list) { 3648cafc3662SHoratiu Vultur list_del(&rx_ts->list); 3649cafc3662SHoratiu Vultur kfree(rx_ts); 3650cafc3662SHoratiu Vultur } 3651cafc3662SHoratiu Vultur 3652cafc3662SHoratiu Vultur skb_queue_purge(&ptp_priv->rx_queue); 3653cafc3662SHoratiu Vultur skb_queue_purge(&ptp_priv->tx_queue); 3654cafc3662SHoratiu Vultur 3655cafc3662SHoratiu Vultur lan8841_ptp_flush_fifo(ptp_priv, false); 3656cafc3662SHoratiu Vultur lan8841_ptp_flush_fifo(ptp_priv, true); 3657cafc3662SHoratiu Vultur 3658cafc3662SHoratiu Vultur return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ? -EFAULT : 0; 3659cafc3662SHoratiu Vultur } 3660cafc3662SHoratiu Vultur 3661cafc3662SHoratiu Vultur #define LAN8841_PTP_LTC_SET_SEC_HI 262 3662cafc3662SHoratiu Vultur #define LAN8841_PTP_LTC_SET_SEC_MID 263 3663cafc3662SHoratiu Vultur #define LAN8841_PTP_LTC_SET_SEC_LO 264 3664cafc3662SHoratiu Vultur #define LAN8841_PTP_LTC_SET_NS_HI 265 3665cafc3662SHoratiu Vultur #define LAN8841_PTP_LTC_SET_NS_LO 266 3666cafc3662SHoratiu Vultur #define LAN8841_PTP_CMD_CTL_PTP_LTC_LOAD BIT(4) 3667cafc3662SHoratiu Vultur 3668cafc3662SHoratiu Vultur static int lan8841_ptp_settime64(struct ptp_clock_info *ptp, 3669cafc3662SHoratiu Vultur const struct timespec64 *ts) 3670cafc3662SHoratiu Vultur { 3671cafc3662SHoratiu Vultur struct kszphy_ptp_priv *ptp_priv = container_of(ptp, struct kszphy_ptp_priv, 3672cafc3662SHoratiu Vultur ptp_clock_info); 3673cafc3662SHoratiu Vultur struct phy_device *phydev = ptp_priv->phydev; 3674cafc3662SHoratiu Vultur 3675cafc3662SHoratiu Vultur /* Set the value to be stored */ 3676cafc3662SHoratiu Vultur mutex_lock(&ptp_priv->ptp_lock); 3677cafc3662SHoratiu Vultur phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_SET_SEC_LO, lower_16_bits(ts->tv_sec)); 3678cafc3662SHoratiu Vultur phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_SET_SEC_MID, upper_16_bits(ts->tv_sec)); 3679cafc3662SHoratiu Vultur phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_SET_SEC_HI, upper_32_bits(ts->tv_sec) & 0xffff); 3680cafc3662SHoratiu Vultur phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_SET_NS_LO, lower_16_bits(ts->tv_nsec)); 3681cafc3662SHoratiu Vultur phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_SET_NS_HI, upper_16_bits(ts->tv_nsec) & 0x3fff); 3682cafc3662SHoratiu Vultur 3683cafc3662SHoratiu Vultur /* Set the command to load the LTC */ 3684cafc3662SHoratiu Vultur phy_write_mmd(phydev, 2, LAN8841_PTP_CMD_CTL, 3685cafc3662SHoratiu Vultur LAN8841_PTP_CMD_CTL_PTP_LTC_LOAD); 3686cafc3662SHoratiu Vultur mutex_unlock(&ptp_priv->ptp_lock); 3687cafc3662SHoratiu Vultur 3688cafc3662SHoratiu Vultur return 0; 3689cafc3662SHoratiu Vultur } 3690cafc3662SHoratiu Vultur 3691cafc3662SHoratiu Vultur #define LAN8841_PTP_LTC_RD_SEC_HI 358 3692cafc3662SHoratiu Vultur #define LAN8841_PTP_LTC_RD_SEC_MID 359 3693cafc3662SHoratiu Vultur #define LAN8841_PTP_LTC_RD_SEC_LO 360 3694cafc3662SHoratiu Vultur #define LAN8841_PTP_LTC_RD_NS_HI 361 3695cafc3662SHoratiu Vultur #define LAN8841_PTP_LTC_RD_NS_LO 362 3696cafc3662SHoratiu Vultur #define LAN8841_PTP_CMD_CTL_PTP_LTC_READ BIT(3) 3697cafc3662SHoratiu Vultur 3698cafc3662SHoratiu Vultur static int lan8841_ptp_gettime64(struct ptp_clock_info *ptp, 3699cafc3662SHoratiu Vultur struct timespec64 *ts) 3700cafc3662SHoratiu Vultur { 3701cafc3662SHoratiu Vultur struct kszphy_ptp_priv *ptp_priv = container_of(ptp, struct kszphy_ptp_priv, 3702cafc3662SHoratiu Vultur ptp_clock_info); 3703cafc3662SHoratiu Vultur struct phy_device *phydev = ptp_priv->phydev; 3704cafc3662SHoratiu Vultur time64_t s; 3705cafc3662SHoratiu Vultur s64 ns; 3706cafc3662SHoratiu Vultur 3707cafc3662SHoratiu Vultur mutex_lock(&ptp_priv->ptp_lock); 3708cafc3662SHoratiu Vultur /* Issue the command to read the LTC */ 3709cafc3662SHoratiu Vultur phy_write_mmd(phydev, 2, LAN8841_PTP_CMD_CTL, 3710cafc3662SHoratiu Vultur LAN8841_PTP_CMD_CTL_PTP_LTC_READ); 3711cafc3662SHoratiu Vultur 3712cafc3662SHoratiu Vultur /* Read the LTC */ 3713cafc3662SHoratiu Vultur s = phy_read_mmd(phydev, 2, LAN8841_PTP_LTC_RD_SEC_HI); 3714cafc3662SHoratiu Vultur s <<= 16; 3715cafc3662SHoratiu Vultur s |= phy_read_mmd(phydev, 2, LAN8841_PTP_LTC_RD_SEC_MID); 3716cafc3662SHoratiu Vultur s <<= 16; 3717cafc3662SHoratiu Vultur s |= phy_read_mmd(phydev, 2, LAN8841_PTP_LTC_RD_SEC_LO); 3718cafc3662SHoratiu Vultur 3719cafc3662SHoratiu Vultur ns = phy_read_mmd(phydev, 2, LAN8841_PTP_LTC_RD_NS_HI) & 0x3fff; 3720cafc3662SHoratiu Vultur ns <<= 16; 3721cafc3662SHoratiu Vultur ns |= phy_read_mmd(phydev, 2, LAN8841_PTP_LTC_RD_NS_LO); 3722cafc3662SHoratiu Vultur mutex_unlock(&ptp_priv->ptp_lock); 3723cafc3662SHoratiu Vultur 3724cafc3662SHoratiu Vultur set_normalized_timespec64(ts, s, ns); 3725cafc3662SHoratiu Vultur return 0; 3726cafc3662SHoratiu Vultur } 3727cafc3662SHoratiu Vultur 3728cafc3662SHoratiu Vultur #define LAN8841_PTP_LTC_STEP_ADJ_LO 276 3729cafc3662SHoratiu Vultur #define LAN8841_PTP_LTC_STEP_ADJ_HI 275 3730cafc3662SHoratiu Vultur #define LAN8841_PTP_LTC_STEP_ADJ_DIR BIT(15) 3731cafc3662SHoratiu Vultur #define LAN8841_PTP_CMD_CTL_PTP_LTC_STEP_SECONDS BIT(5) 3732cafc3662SHoratiu Vultur #define LAN8841_PTP_CMD_CTL_PTP_LTC_STEP_NANOSECONDS BIT(6) 3733cafc3662SHoratiu Vultur 3734cafc3662SHoratiu Vultur static int lan8841_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta) 3735cafc3662SHoratiu Vultur { 3736cafc3662SHoratiu Vultur struct kszphy_ptp_priv *ptp_priv = container_of(ptp, struct kszphy_ptp_priv, 3737cafc3662SHoratiu Vultur ptp_clock_info); 3738cafc3662SHoratiu Vultur struct phy_device *phydev = ptp_priv->phydev; 3739cafc3662SHoratiu Vultur struct timespec64 ts; 3740cafc3662SHoratiu Vultur bool add = true; 3741cafc3662SHoratiu Vultur u32 nsec; 3742cafc3662SHoratiu Vultur s32 sec; 3743cafc3662SHoratiu Vultur 3744cafc3662SHoratiu Vultur /* The HW allows up to 15 sec to adjust the time, but here we limit to 3745cafc3662SHoratiu Vultur * 10 sec the adjustment. The reason is, in case the adjustment is 14 3746cafc3662SHoratiu Vultur * sec and 999999999 nsec, then we add 8ns to compansate the actual 3747cafc3662SHoratiu Vultur * increment so the value can be bigger than 15 sec. Therefore limit the 3748cafc3662SHoratiu Vultur * possible adjustments so we will not have these corner cases 3749cafc3662SHoratiu Vultur */ 3750cafc3662SHoratiu Vultur if (delta > 10000000000LL || delta < -10000000000LL) { 3751cafc3662SHoratiu Vultur /* The timeadjustment is too big, so fall back using set time */ 3752cafc3662SHoratiu Vultur u64 now; 3753cafc3662SHoratiu Vultur 3754cafc3662SHoratiu Vultur ptp->gettime64(ptp, &ts); 3755cafc3662SHoratiu Vultur 3756cafc3662SHoratiu Vultur now = ktime_to_ns(timespec64_to_ktime(ts)); 3757cafc3662SHoratiu Vultur ts = ns_to_timespec64(now + delta); 3758cafc3662SHoratiu Vultur 3759cafc3662SHoratiu Vultur ptp->settime64(ptp, &ts); 3760cafc3662SHoratiu Vultur return 0; 3761cafc3662SHoratiu Vultur } 3762cafc3662SHoratiu Vultur 3763cafc3662SHoratiu Vultur sec = div_u64_rem(delta < 0 ? -delta : delta, NSEC_PER_SEC, &nsec); 3764cafc3662SHoratiu Vultur if (delta < 0 && nsec != 0) { 3765cafc3662SHoratiu Vultur /* It is not allowed to adjust low the nsec part, therefore 3766cafc3662SHoratiu Vultur * subtract more from second part and add to nanosecond such 3767cafc3662SHoratiu Vultur * that would roll over, so the second part will increase 3768cafc3662SHoratiu Vultur */ 3769cafc3662SHoratiu Vultur sec--; 3770cafc3662SHoratiu Vultur nsec = NSEC_PER_SEC - nsec; 3771cafc3662SHoratiu Vultur } 3772cafc3662SHoratiu Vultur 3773cafc3662SHoratiu Vultur /* Calculate the adjustments and the direction */ 3774cafc3662SHoratiu Vultur if (delta < 0) 3775cafc3662SHoratiu Vultur add = false; 3776cafc3662SHoratiu Vultur 3777cafc3662SHoratiu Vultur if (nsec > 0) 3778cafc3662SHoratiu Vultur /* add 8 ns to cover the likely normal increment */ 3779cafc3662SHoratiu Vultur nsec += 8; 3780cafc3662SHoratiu Vultur 3781cafc3662SHoratiu Vultur if (nsec >= NSEC_PER_SEC) { 3782cafc3662SHoratiu Vultur /* carry into seconds */ 3783cafc3662SHoratiu Vultur sec++; 3784cafc3662SHoratiu Vultur nsec -= NSEC_PER_SEC; 3785cafc3662SHoratiu Vultur } 3786cafc3662SHoratiu Vultur 3787cafc3662SHoratiu Vultur mutex_lock(&ptp_priv->ptp_lock); 3788cafc3662SHoratiu Vultur if (sec) { 3789cafc3662SHoratiu Vultur phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_STEP_ADJ_LO, sec); 3790cafc3662SHoratiu Vultur phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_STEP_ADJ_HI, 3791cafc3662SHoratiu Vultur add ? LAN8841_PTP_LTC_STEP_ADJ_DIR : 0); 3792cafc3662SHoratiu Vultur phy_write_mmd(phydev, 2, LAN8841_PTP_CMD_CTL, 3793cafc3662SHoratiu Vultur LAN8841_PTP_CMD_CTL_PTP_LTC_STEP_SECONDS); 3794cafc3662SHoratiu Vultur } 3795cafc3662SHoratiu Vultur 3796cafc3662SHoratiu Vultur if (nsec) { 3797cafc3662SHoratiu Vultur phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_STEP_ADJ_LO, 3798cafc3662SHoratiu Vultur nsec & 0xffff); 3799cafc3662SHoratiu Vultur phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_STEP_ADJ_HI, 3800cafc3662SHoratiu Vultur (nsec >> 16) & 0x3fff); 3801cafc3662SHoratiu Vultur phy_write_mmd(phydev, 2, LAN8841_PTP_CMD_CTL, 3802cafc3662SHoratiu Vultur LAN8841_PTP_CMD_CTL_PTP_LTC_STEP_NANOSECONDS); 3803cafc3662SHoratiu Vultur } 3804cafc3662SHoratiu Vultur mutex_unlock(&ptp_priv->ptp_lock); 3805cafc3662SHoratiu Vultur 3806cafc3662SHoratiu Vultur return 0; 3807cafc3662SHoratiu Vultur } 3808cafc3662SHoratiu Vultur 3809cafc3662SHoratiu Vultur #define LAN8841_PTP_LTC_RATE_ADJ_HI 269 3810cafc3662SHoratiu Vultur #define LAN8841_PTP_LTC_RATE_ADJ_HI_DIR BIT(15) 3811cafc3662SHoratiu Vultur #define LAN8841_PTP_LTC_RATE_ADJ_LO 270 3812cafc3662SHoratiu Vultur 3813cafc3662SHoratiu Vultur static int lan8841_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm) 3814cafc3662SHoratiu Vultur { 3815cafc3662SHoratiu Vultur struct kszphy_ptp_priv *ptp_priv = container_of(ptp, struct kszphy_ptp_priv, 3816cafc3662SHoratiu Vultur ptp_clock_info); 3817cafc3662SHoratiu Vultur struct phy_device *phydev = ptp_priv->phydev; 3818cafc3662SHoratiu Vultur bool faster = true; 3819cafc3662SHoratiu Vultur u32 rate; 3820cafc3662SHoratiu Vultur 3821cafc3662SHoratiu Vultur if (!scaled_ppm) 3822cafc3662SHoratiu Vultur return 0; 3823cafc3662SHoratiu Vultur 3824cafc3662SHoratiu Vultur if (scaled_ppm < 0) { 3825cafc3662SHoratiu Vultur scaled_ppm = -scaled_ppm; 3826cafc3662SHoratiu Vultur faster = false; 3827cafc3662SHoratiu Vultur } 3828cafc3662SHoratiu Vultur 3829cafc3662SHoratiu Vultur rate = LAN8814_1PPM_FORMAT * (upper_16_bits(scaled_ppm)); 3830cafc3662SHoratiu Vultur rate += (LAN8814_1PPM_FORMAT * (lower_16_bits(scaled_ppm))) >> 16; 3831cafc3662SHoratiu Vultur 3832cafc3662SHoratiu Vultur mutex_lock(&ptp_priv->ptp_lock); 3833cafc3662SHoratiu Vultur phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_RATE_ADJ_HI, 3834cafc3662SHoratiu Vultur faster ? LAN8841_PTP_LTC_RATE_ADJ_HI_DIR | (upper_16_bits(rate) & 0x3fff) 3835cafc3662SHoratiu Vultur : upper_16_bits(rate) & 0x3fff); 3836cafc3662SHoratiu Vultur phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_RATE_ADJ_LO, lower_16_bits(rate)); 3837cafc3662SHoratiu Vultur mutex_unlock(&ptp_priv->ptp_lock); 3838cafc3662SHoratiu Vultur 3839cafc3662SHoratiu Vultur return 0; 3840cafc3662SHoratiu Vultur } 3841cafc3662SHoratiu Vultur 3842cafc3662SHoratiu Vultur static struct ptp_clock_info lan8841_ptp_clock_info = { 3843cafc3662SHoratiu Vultur .owner = THIS_MODULE, 3844cafc3662SHoratiu Vultur .name = "lan8841 ptp", 3845cafc3662SHoratiu Vultur .max_adj = 31249999, 3846cafc3662SHoratiu Vultur .gettime64 = lan8841_ptp_gettime64, 3847cafc3662SHoratiu Vultur .settime64 = lan8841_ptp_settime64, 3848cafc3662SHoratiu Vultur .adjtime = lan8841_ptp_adjtime, 3849cafc3662SHoratiu Vultur .adjfine = lan8841_ptp_adjfine, 3850cafc3662SHoratiu Vultur }; 3851cafc3662SHoratiu Vultur 3852a8f1a19dSHoratiu Vultur #define LAN8841_OPERATION_MODE_STRAP_LOW_REGISTER 3 3853a8f1a19dSHoratiu Vultur #define LAN8841_OPERATION_MODE_STRAP_LOW_REGISTER_STRAP_RGMII_EN BIT(0) 3854a8f1a19dSHoratiu Vultur 3855a8f1a19dSHoratiu Vultur static int lan8841_probe(struct phy_device *phydev) 3856a8f1a19dSHoratiu Vultur { 3857cafc3662SHoratiu Vultur struct kszphy_ptp_priv *ptp_priv; 3858cafc3662SHoratiu Vultur struct kszphy_priv *priv; 3859a8f1a19dSHoratiu Vultur int err; 3860a8f1a19dSHoratiu Vultur 3861a8f1a19dSHoratiu Vultur err = kszphy_probe(phydev); 3862a8f1a19dSHoratiu Vultur if (err) 3863a8f1a19dSHoratiu Vultur return err; 3864a8f1a19dSHoratiu Vultur 3865a8f1a19dSHoratiu Vultur if (phy_read_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG, 3866a8f1a19dSHoratiu Vultur LAN8841_OPERATION_MODE_STRAP_LOW_REGISTER) & 3867a8f1a19dSHoratiu Vultur LAN8841_OPERATION_MODE_STRAP_LOW_REGISTER_STRAP_RGMII_EN) 3868a8f1a19dSHoratiu Vultur phydev->interface = PHY_INTERFACE_MODE_RGMII_RXID; 3869a8f1a19dSHoratiu Vultur 3870cafc3662SHoratiu Vultur /* Register the clock */ 3871cafc3662SHoratiu Vultur if (!IS_ENABLED(CONFIG_NETWORK_PHY_TIMESTAMPING)) 3872cafc3662SHoratiu Vultur return 0; 3873cafc3662SHoratiu Vultur 3874cafc3662SHoratiu Vultur priv = phydev->priv; 3875cafc3662SHoratiu Vultur ptp_priv = &priv->ptp_priv; 3876cafc3662SHoratiu Vultur 3877cafc3662SHoratiu Vultur ptp_priv->ptp_clock_info = lan8841_ptp_clock_info; 3878cafc3662SHoratiu Vultur ptp_priv->ptp_clock = ptp_clock_register(&ptp_priv->ptp_clock_info, 3879cafc3662SHoratiu Vultur &phydev->mdio.dev); 3880cafc3662SHoratiu Vultur if (IS_ERR(ptp_priv->ptp_clock)) { 3881cafc3662SHoratiu Vultur phydev_err(phydev, "ptp_clock_register failed: %lu\n", 3882cafc3662SHoratiu Vultur PTR_ERR(ptp_priv->ptp_clock)); 3883cafc3662SHoratiu Vultur return -EINVAL; 3884cafc3662SHoratiu Vultur } 3885cafc3662SHoratiu Vultur 3886cafc3662SHoratiu Vultur if (!ptp_priv->ptp_clock) 3887cafc3662SHoratiu Vultur return 0; 3888cafc3662SHoratiu Vultur 3889cafc3662SHoratiu Vultur /* Initialize the SW */ 3890cafc3662SHoratiu Vultur skb_queue_head_init(&ptp_priv->tx_queue); 3891cafc3662SHoratiu Vultur skb_queue_head_init(&ptp_priv->rx_queue); 3892cafc3662SHoratiu Vultur INIT_LIST_HEAD(&ptp_priv->rx_ts_list); 3893cafc3662SHoratiu Vultur spin_lock_init(&ptp_priv->rx_ts_lock); 3894cafc3662SHoratiu Vultur ptp_priv->phydev = phydev; 3895cafc3662SHoratiu Vultur mutex_init(&ptp_priv->ptp_lock); 3896cafc3662SHoratiu Vultur 3897cafc3662SHoratiu Vultur ptp_priv->mii_ts.rxtstamp = lan8814_rxtstamp; 3898cafc3662SHoratiu Vultur ptp_priv->mii_ts.txtstamp = lan8814_txtstamp; 3899cafc3662SHoratiu Vultur ptp_priv->mii_ts.hwtstamp = lan8841_hwtstamp; 3900cafc3662SHoratiu Vultur ptp_priv->mii_ts.ts_info = lan8841_ts_info; 3901cafc3662SHoratiu Vultur 3902cafc3662SHoratiu Vultur phydev->mii_ts = &ptp_priv->mii_ts; 3903cafc3662SHoratiu Vultur 3904a8f1a19dSHoratiu Vultur return 0; 3905a8f1a19dSHoratiu Vultur } 3906a8f1a19dSHoratiu Vultur 3907d5bf9071SChristian Hohnstaedt static struct phy_driver ksphy_driver[] = { 3908d5bf9071SChristian Hohnstaedt { 390951f932c4SChoi, David .phy_id = PHY_ID_KS8737, 3910f893a99eSFabio Estevam .phy_id_mask = MICREL_PHY_ID_MASK, 391151f932c4SChoi, David .name = "Micrel KS8737", 3912dcdecdcfSHeiner Kallweit /* PHY_BASIC_FEATURES */ 3913c6f9575cSJohan Hovold .driver_data = &ks8737_type, 391415f03ffeSFabio Estevam .probe = kszphy_probe, 3915d0507009SDavid J. Choi .config_init = kszphy_config_init, 3916c6f9575cSJohan Hovold .config_intr = kszphy_config_intr, 391759ca4e58SIoana Ciornei .handle_interrupt = kszphy_handle_interrupt, 3918f1131b9cSClaudiu Beznea .suspend = kszphy_suspend, 3919f1131b9cSClaudiu Beznea .resume = kszphy_resume, 3920d5bf9071SChristian Hohnstaedt }, { 3921212ea99aSMarek Vasut .phy_id = PHY_ID_KSZ8021, 3922212ea99aSMarek Vasut .phy_id_mask = 0x00ffffff, 39237ab59dc1SDavid J. Choi .name = "Micrel KSZ8021 or KSZ8031", 3924dcdecdcfSHeiner Kallweit /* PHY_BASIC_FEATURES */ 3925e6a423a8SJohan Hovold .driver_data = &ksz8021_type, 392663f44b2bSJohan Hovold .probe = kszphy_probe, 3927d0e1df9cSJohan Hovold .config_init = kszphy_config_init, 3928212ea99aSMarek Vasut .config_intr = kszphy_config_intr, 392959ca4e58SIoana Ciornei .handle_interrupt = kszphy_handle_interrupt, 39302b2427d0SAndrew Lunn .get_sset_count = kszphy_get_sset_count, 39312b2427d0SAndrew Lunn .get_strings = kszphy_get_strings, 39322b2427d0SAndrew Lunn .get_stats = kszphy_get_stats, 3933f1131b9cSClaudiu Beznea .suspend = kszphy_suspend, 3934f1131b9cSClaudiu Beznea .resume = kszphy_resume, 3935212ea99aSMarek Vasut }, { 3936b818d1a7SHector Palacios .phy_id = PHY_ID_KSZ8031, 3937b818d1a7SHector Palacios .phy_id_mask = 0x00ffffff, 3938b818d1a7SHector Palacios .name = "Micrel KSZ8031", 3939dcdecdcfSHeiner Kallweit /* PHY_BASIC_FEATURES */ 3940e6a423a8SJohan Hovold .driver_data = &ksz8021_type, 394163f44b2bSJohan Hovold .probe = kszphy_probe, 3942d0e1df9cSJohan Hovold .config_init = kszphy_config_init, 3943b818d1a7SHector Palacios .config_intr = kszphy_config_intr, 394459ca4e58SIoana Ciornei .handle_interrupt = kszphy_handle_interrupt, 39452b2427d0SAndrew Lunn .get_sset_count = kszphy_get_sset_count, 39462b2427d0SAndrew Lunn .get_strings = kszphy_get_strings, 39472b2427d0SAndrew Lunn .get_stats = kszphy_get_stats, 3948f1131b9cSClaudiu Beznea .suspend = kszphy_suspend, 3949f1131b9cSClaudiu Beznea .resume = kszphy_resume, 3950b818d1a7SHector Palacios }, { 3951510d573fSMarek Vasut .phy_id = PHY_ID_KSZ8041, 3952f893a99eSFabio Estevam .phy_id_mask = MICREL_PHY_ID_MASK, 3953510d573fSMarek Vasut .name = "Micrel KSZ8041", 3954dcdecdcfSHeiner Kallweit /* PHY_BASIC_FEATURES */ 3955e6a423a8SJohan Hovold .driver_data = &ksz8041_type, 3956e6a423a8SJohan Hovold .probe = kszphy_probe, 395777501a79SPhilipp Zabel .config_init = ksz8041_config_init, 395877501a79SPhilipp Zabel .config_aneg = ksz8041_config_aneg, 395951f932c4SChoi, David .config_intr = kszphy_config_intr, 396059ca4e58SIoana Ciornei .handle_interrupt = kszphy_handle_interrupt, 39612b2427d0SAndrew Lunn .get_sset_count = kszphy_get_sset_count, 39622b2427d0SAndrew Lunn .get_strings = kszphy_get_strings, 39632b2427d0SAndrew Lunn .get_stats = kszphy_get_stats, 39642641b62dSStefan Agner /* No suspend/resume callbacks because of errata DS80000700A, 39652641b62dSStefan Agner * receiver error following software power down. 39662641b62dSStefan Agner */ 3967d5bf9071SChristian Hohnstaedt }, { 39684bd7b512SSergei Shtylyov .phy_id = PHY_ID_KSZ8041RNLI, 3969f893a99eSFabio Estevam .phy_id_mask = MICREL_PHY_ID_MASK, 39704bd7b512SSergei Shtylyov .name = "Micrel KSZ8041RNLI", 3971dcdecdcfSHeiner Kallweit /* PHY_BASIC_FEATURES */ 3972e6a423a8SJohan Hovold .driver_data = &ksz8041_type, 3973e6a423a8SJohan Hovold .probe = kszphy_probe, 3974e6a423a8SJohan Hovold .config_init = kszphy_config_init, 39754bd7b512SSergei Shtylyov .config_intr = kszphy_config_intr, 397659ca4e58SIoana Ciornei .handle_interrupt = kszphy_handle_interrupt, 39772b2427d0SAndrew Lunn .get_sset_count = kszphy_get_sset_count, 39782b2427d0SAndrew Lunn .get_strings = kszphy_get_strings, 39792b2427d0SAndrew Lunn .get_stats = kszphy_get_stats, 3980f1131b9cSClaudiu Beznea .suspend = kszphy_suspend, 3981f1131b9cSClaudiu Beznea .resume = kszphy_resume, 39824bd7b512SSergei Shtylyov }, { 3983510d573fSMarek Vasut .name = "Micrel KSZ8051", 3984dcdecdcfSHeiner Kallweit /* PHY_BASIC_FEATURES */ 3985e6a423a8SJohan Hovold .driver_data = &ksz8051_type, 3986e6a423a8SJohan Hovold .probe = kszphy_probe, 398763f44b2bSJohan Hovold .config_init = kszphy_config_init, 398851f932c4SChoi, David .config_intr = kszphy_config_intr, 398959ca4e58SIoana Ciornei .handle_interrupt = kszphy_handle_interrupt, 39902b2427d0SAndrew Lunn .get_sset_count = kszphy_get_sset_count, 39912b2427d0SAndrew Lunn .get_strings = kszphy_get_strings, 39922b2427d0SAndrew Lunn .get_stats = kszphy_get_stats, 39938b95599cSMarek Vasut .match_phy_device = ksz8051_match_phy_device, 3994f1131b9cSClaudiu Beznea .suspend = kszphy_suspend, 3995f1131b9cSClaudiu Beznea .resume = kszphy_resume, 3996d5bf9071SChristian Hohnstaedt }, { 3997510d573fSMarek Vasut .phy_id = PHY_ID_KSZ8001, 3998510d573fSMarek Vasut .name = "Micrel KSZ8001 or KS8721", 3999ecd5a323SAlexander Stein .phy_id_mask = 0x00fffffc, 4000dcdecdcfSHeiner Kallweit /* PHY_BASIC_FEATURES */ 4001e6a423a8SJohan Hovold .driver_data = &ksz8041_type, 4002e6a423a8SJohan Hovold .probe = kszphy_probe, 4003e6a423a8SJohan Hovold .config_init = kszphy_config_init, 400451f932c4SChoi, David .config_intr = kszphy_config_intr, 400559ca4e58SIoana Ciornei .handle_interrupt = kszphy_handle_interrupt, 40062b2427d0SAndrew Lunn .get_sset_count = kszphy_get_sset_count, 40072b2427d0SAndrew Lunn .get_strings = kszphy_get_strings, 40082b2427d0SAndrew Lunn .get_stats = kszphy_get_stats, 4009f1131b9cSClaudiu Beznea .suspend = kszphy_suspend, 4010f1131b9cSClaudiu Beznea .resume = kszphy_resume, 4011d5bf9071SChristian Hohnstaedt }, { 40127ab59dc1SDavid J. Choi .phy_id = PHY_ID_KSZ8081, 40137ab59dc1SDavid J. Choi .name = "Micrel KSZ8081 or KSZ8091", 4014f893a99eSFabio Estevam .phy_id_mask = MICREL_PHY_ID_MASK, 401549011e0cSOleksij Rempel .flags = PHY_POLL_CABLE_TEST, 4016dcdecdcfSHeiner Kallweit /* PHY_BASIC_FEATURES */ 4017e6a423a8SJohan Hovold .driver_data = &ksz8081_type, 4018e6a423a8SJohan Hovold .probe = kszphy_probe, 40197a1d8390SAntoine Tenart .config_init = ksz8081_config_init, 4020764d31caSChristian Melki .soft_reset = genphy_soft_reset, 4021f873f112SOleksij Rempel .config_aneg = ksz8081_config_aneg, 4022f873f112SOleksij Rempel .read_status = ksz8081_read_status, 40237ab59dc1SDavid J. Choi .config_intr = kszphy_config_intr, 402459ca4e58SIoana Ciornei .handle_interrupt = kszphy_handle_interrupt, 40252b2427d0SAndrew Lunn .get_sset_count = kszphy_get_sset_count, 40262b2427d0SAndrew Lunn .get_strings = kszphy_get_strings, 40272b2427d0SAndrew Lunn .get_stats = kszphy_get_stats, 4028836384d2SWenyou Yang .suspend = kszphy_suspend, 4029f5aba91dSAlexandre Belloni .resume = kszphy_resume, 403049011e0cSOleksij Rempel .cable_test_start = ksz886x_cable_test_start, 403149011e0cSOleksij Rempel .cable_test_get_status = ksz886x_cable_test_get_status, 40327ab59dc1SDavid J. Choi }, { 40337ab59dc1SDavid J. Choi .phy_id = PHY_ID_KSZ8061, 40347ab59dc1SDavid J. Choi .name = "Micrel KSZ8061", 4035f893a99eSFabio Estevam .phy_id_mask = MICREL_PHY_ID_MASK, 4036dcdecdcfSHeiner Kallweit /* PHY_BASIC_FEATURES */ 40378e6004dfSFabio Estevam .probe = kszphy_probe, 4038232ba3a5SRajasingh Thavamani .config_init = ksz8061_config_init, 40397ab59dc1SDavid J. Choi .config_intr = kszphy_config_intr, 404059ca4e58SIoana Ciornei .handle_interrupt = kszphy_handle_interrupt, 40418e6004dfSFabio Estevam .suspend = kszphy_suspend, 40428e6004dfSFabio Estevam .resume = kszphy_resume, 40437ab59dc1SDavid J. Choi }, { 4044d0507009SDavid J. Choi .phy_id = PHY_ID_KSZ9021, 404548d7d0adSJason Wang .phy_id_mask = 0x000ffffe, 4046d0507009SDavid J. Choi .name = "Micrel KSZ9021 Gigabit PHY", 4047dcdecdcfSHeiner Kallweit /* PHY_GBIT_FEATURES */ 4048c6f9575cSJohan Hovold .driver_data = &ksz9021_type, 4049bfe72442SGrygorii Strashko .probe = kszphy_probe, 4050407d8098SHans Andersson .get_features = ksz9031_get_features, 4051954c3967SSean Cross .config_init = ksz9021_config_init, 4052c6f9575cSJohan Hovold .config_intr = kszphy_config_intr, 405359ca4e58SIoana Ciornei .handle_interrupt = kszphy_handle_interrupt, 40542b2427d0SAndrew Lunn .get_sset_count = kszphy_get_sset_count, 40552b2427d0SAndrew Lunn .get_strings = kszphy_get_strings, 40562b2427d0SAndrew Lunn .get_stats = kszphy_get_stats, 4057f1131b9cSClaudiu Beznea .suspend = kszphy_suspend, 4058f1131b9cSClaudiu Beznea .resume = kszphy_resume, 4059c846a2b7SKevin Hao .read_mmd = genphy_read_mmd_unsupported, 4060c846a2b7SKevin Hao .write_mmd = genphy_write_mmd_unsupported, 406193272e07SJean-Christophe PLAGNIOL-VILLARD }, { 40627ab59dc1SDavid J. Choi .phy_id = PHY_ID_KSZ9031, 4063f893a99eSFabio Estevam .phy_id_mask = MICREL_PHY_ID_MASK, 40647ab59dc1SDavid J. Choi .name = "Micrel KSZ9031 Gigabit PHY", 406558389c00SMarek Vasut .flags = PHY_POLL_CABLE_TEST, 4066c6f9575cSJohan Hovold .driver_data = &ksz9021_type, 4067bfe72442SGrygorii Strashko .probe = kszphy_probe, 40683aed3e2aSAntoine Tenart .get_features = ksz9031_get_features, 40696e4b8273SHubert Chaumette .config_init = ksz9031_config_init, 40701d16073aSHeiner Kallweit .soft_reset = genphy_soft_reset, 4071d2fd719bSNathan Sullivan .read_status = ksz9031_read_status, 4072c6f9575cSJohan Hovold .config_intr = kszphy_config_intr, 407359ca4e58SIoana Ciornei .handle_interrupt = kszphy_handle_interrupt, 40742b2427d0SAndrew Lunn .get_sset_count = kszphy_get_sset_count, 40752b2427d0SAndrew Lunn .get_strings = kszphy_get_strings, 40762b2427d0SAndrew Lunn .get_stats = kszphy_get_stats, 4077f1131b9cSClaudiu Beznea .suspend = kszphy_suspend, 4078f64f1482SXander Huff .resume = kszphy_resume, 407958389c00SMarek Vasut .cable_test_start = ksz9x31_cable_test_start, 408058389c00SMarek Vasut .cable_test_get_status = ksz9x31_cable_test_get_status, 40817ab59dc1SDavid J. Choi }, { 40821623ad8eSDivya Koppera .phy_id = PHY_ID_LAN8814, 40831623ad8eSDivya Koppera .phy_id_mask = MICREL_PHY_ID_MASK, 40841623ad8eSDivya Koppera .name = "Microchip INDY Gigabit Quad PHY", 408521b688daSDivya Koppera .flags = PHY_POLL_CABLE_TEST, 40867467d716SHoratiu Vultur .config_init = lan8814_config_init, 4087a516b7f7SDivya Koppera .driver_data = &lan8814_type, 4088ece19502SDivya Koppera .probe = lan8814_probe, 40891623ad8eSDivya Koppera .soft_reset = genphy_soft_reset, 4090b814403aSHoratiu Vultur .read_status = ksz9031_read_status, 40911623ad8eSDivya Koppera .get_sset_count = kszphy_get_sset_count, 40921623ad8eSDivya Koppera .get_strings = kszphy_get_strings, 40931623ad8eSDivya Koppera .get_stats = kszphy_get_stats, 40941623ad8eSDivya Koppera .suspend = genphy_suspend, 40951623ad8eSDivya Koppera .resume = kszphy_resume, 4096b3ec7248SDivya Koppera .config_intr = lan8814_config_intr, 4097b3ec7248SDivya Koppera .handle_interrupt = lan8814_handle_interrupt, 409821b688daSDivya Koppera .cable_test_start = lan8814_cable_test_start, 409921b688daSDivya Koppera .cable_test_get_status = ksz886x_cable_test_get_status, 41001623ad8eSDivya Koppera }, { 41017c2dcfa2SHoratiu Vultur .phy_id = PHY_ID_LAN8804, 41027c2dcfa2SHoratiu Vultur .phy_id_mask = MICREL_PHY_ID_MASK, 41037c2dcfa2SHoratiu Vultur .name = "Microchip LAN966X Gigabit PHY", 41047c2dcfa2SHoratiu Vultur .config_init = lan8804_config_init, 41057c2dcfa2SHoratiu Vultur .driver_data = &ksz9021_type, 41067c2dcfa2SHoratiu Vultur .probe = kszphy_probe, 41077c2dcfa2SHoratiu Vultur .soft_reset = genphy_soft_reset, 41087c2dcfa2SHoratiu Vultur .read_status = ksz9031_read_status, 41097c2dcfa2SHoratiu Vultur .get_sset_count = kszphy_get_sset_count, 41107c2dcfa2SHoratiu Vultur .get_strings = kszphy_get_strings, 41117c2dcfa2SHoratiu Vultur .get_stats = kszphy_get_stats, 41127c2dcfa2SHoratiu Vultur .suspend = genphy_suspend, 41137c2dcfa2SHoratiu Vultur .resume = kszphy_resume, 4114b324c6e5SHoratiu Vultur .config_intr = lan8804_config_intr, 4115b324c6e5SHoratiu Vultur .handle_interrupt = lan8804_handle_interrupt, 41167c2dcfa2SHoratiu Vultur }, { 4117a8f1a19dSHoratiu Vultur .phy_id = PHY_ID_LAN8841, 4118a8f1a19dSHoratiu Vultur .phy_id_mask = MICREL_PHY_ID_MASK, 4119a8f1a19dSHoratiu Vultur .name = "Microchip LAN8841 Gigabit PHY", 4120a136391aSHoratiu Vultur .flags = PHY_POLL_CABLE_TEST, 4121a8f1a19dSHoratiu Vultur .driver_data = &lan8841_type, 4122a8f1a19dSHoratiu Vultur .config_init = lan8841_config_init, 4123a8f1a19dSHoratiu Vultur .probe = lan8841_probe, 4124a8f1a19dSHoratiu Vultur .soft_reset = genphy_soft_reset, 4125a8f1a19dSHoratiu Vultur .config_intr = lan8841_config_intr, 4126a8f1a19dSHoratiu Vultur .handle_interrupt = lan8841_handle_interrupt, 4127a8f1a19dSHoratiu Vultur .get_sset_count = kszphy_get_sset_count, 4128a8f1a19dSHoratiu Vultur .get_strings = kszphy_get_strings, 4129a8f1a19dSHoratiu Vultur .get_stats = kszphy_get_stats, 4130a8f1a19dSHoratiu Vultur .suspend = genphy_suspend, 4131a8f1a19dSHoratiu Vultur .resume = genphy_resume, 4132a136391aSHoratiu Vultur .cable_test_start = lan8814_cable_test_start, 4133a136391aSHoratiu Vultur .cable_test_get_status = ksz886x_cable_test_get_status, 4134a8f1a19dSHoratiu Vultur }, { 4135bff5b4b3SYuiko Oshino .phy_id = PHY_ID_KSZ9131, 4136bff5b4b3SYuiko Oshino .phy_id_mask = MICREL_PHY_ID_MASK, 4137bff5b4b3SYuiko Oshino .name = "Microchip KSZ9131 Gigabit PHY", 4138dcdecdcfSHeiner Kallweit /* PHY_GBIT_FEATURES */ 413958389c00SMarek Vasut .flags = PHY_POLL_CABLE_TEST, 4140a8f1a19dSHoratiu Vultur .driver_data = &ksz9131_type, 4141bff5b4b3SYuiko Oshino .probe = kszphy_probe, 4142bff5b4b3SYuiko Oshino .config_init = ksz9131_config_init, 4143bff5b4b3SYuiko Oshino .config_intr = kszphy_config_intr, 4144b64e6a87SRaju Lakkaraju .config_aneg = ksz9131_config_aneg, 4145b64e6a87SRaju Lakkaraju .read_status = ksz9131_read_status, 414659ca4e58SIoana Ciornei .handle_interrupt = kszphy_handle_interrupt, 4147bff5b4b3SYuiko Oshino .get_sset_count = kszphy_get_sset_count, 4148bff5b4b3SYuiko Oshino .get_strings = kszphy_get_strings, 4149bff5b4b3SYuiko Oshino .get_stats = kszphy_get_stats, 4150f1131b9cSClaudiu Beznea .suspend = kszphy_suspend, 4151bff5b4b3SYuiko Oshino .resume = kszphy_resume, 415258389c00SMarek Vasut .cable_test_start = ksz9x31_cable_test_start, 415358389c00SMarek Vasut .cable_test_get_status = ksz9x31_cable_test_get_status, 4154*f2e9d083SOleksij Rempel .get_features = ksz9477_get_features, 4155bff5b4b3SYuiko Oshino }, { 415693272e07SJean-Christophe PLAGNIOL-VILLARD .phy_id = PHY_ID_KSZ8873MLL, 4157f893a99eSFabio Estevam .phy_id_mask = MICREL_PHY_ID_MASK, 415893272e07SJean-Christophe PLAGNIOL-VILLARD .name = "Micrel KSZ8873MLL Switch", 4159dcdecdcfSHeiner Kallweit /* PHY_BASIC_FEATURES */ 416093272e07SJean-Christophe PLAGNIOL-VILLARD .config_init = kszphy_config_init, 416193272e07SJean-Christophe PLAGNIOL-VILLARD .config_aneg = ksz8873mll_config_aneg, 416293272e07SJean-Christophe PLAGNIOL-VILLARD .read_status = ksz8873mll_read_status, 41631a5465f5SPatrice Vilchez .suspend = genphy_suspend, 41641a5465f5SPatrice Vilchez .resume = genphy_resume, 41657ab59dc1SDavid J. Choi }, { 41667ab59dc1SDavid J. Choi .phy_id = PHY_ID_KSZ886X, 4167f893a99eSFabio Estevam .phy_id_mask = MICREL_PHY_ID_MASK, 4168ab36a3a2SMarek Vasut .name = "Micrel KSZ8851 Ethernet MAC or KSZ886X Switch", 416921b688daSDivya Koppera .driver_data = &ksz886x_type, 4170dcdecdcfSHeiner Kallweit /* PHY_BASIC_FEATURES */ 417149011e0cSOleksij Rempel .flags = PHY_POLL_CABLE_TEST, 41727ab59dc1SDavid J. Choi .config_init = kszphy_config_init, 417352939393SOleksij Rempel .config_aneg = ksz886x_config_aneg, 417452939393SOleksij Rempel .read_status = ksz886x_read_status, 41751a5465f5SPatrice Vilchez .suspend = genphy_suspend, 41761a5465f5SPatrice Vilchez .resume = genphy_resume, 417749011e0cSOleksij Rempel .cable_test_start = ksz886x_cable_test_start, 417849011e0cSOleksij Rempel .cable_test_get_status = ksz886x_cable_test_get_status, 41799d162ed6SSean Nyekjaer }, { 41801d951ba3SMarek Vasut .name = "Micrel KSZ87XX Switch", 4181dcdecdcfSHeiner Kallweit /* PHY_BASIC_FEATURES */ 41829d162ed6SSean Nyekjaer .config_init = kszphy_config_init, 41838b95599cSMarek Vasut .match_phy_device = ksz8795_match_phy_device, 41849d162ed6SSean Nyekjaer .suspend = genphy_suspend, 41859d162ed6SSean Nyekjaer .resume = genphy_resume, 4186fc3973a1SWoojung Huh }, { 4187fc3973a1SWoojung Huh .phy_id = PHY_ID_KSZ9477, 4188fc3973a1SWoojung Huh .phy_id_mask = MICREL_PHY_ID_MASK, 4189fc3973a1SWoojung Huh .name = "Microchip KSZ9477", 4190dcdecdcfSHeiner Kallweit /* PHY_GBIT_FEATURES */ 4191fc3973a1SWoojung Huh .config_init = kszphy_config_init, 4192db45c76bSArun Ramadoss .config_intr = kszphy_config_intr, 4193db45c76bSArun Ramadoss .handle_interrupt = kszphy_handle_interrupt, 4194fc3973a1SWoojung Huh .suspend = genphy_suspend, 4195fc3973a1SWoojung Huh .resume = genphy_resume, 419648fb1994SOleksij Rempel .get_features = ksz9477_get_features, 4197d5bf9071SChristian Hohnstaedt } }; 4198d0507009SDavid J. Choi 419950fd7150SJohan Hovold module_phy_driver(ksphy_driver); 4200d0507009SDavid J. Choi 4201d0507009SDavid J. Choi MODULE_DESCRIPTION("Micrel PHY driver"); 4202d0507009SDavid J. Choi MODULE_AUTHOR("David J. Choi"); 4203d0507009SDavid J. Choi MODULE_LICENSE("GPL"); 420452a60ed2SDavid S. Miller 4205cf93c945SUwe Kleine-König static struct mdio_device_id __maybe_unused micrel_tbl[] = { 420648d7d0adSJason Wang { PHY_ID_KSZ9021, 0x000ffffe }, 4207f893a99eSFabio Estevam { PHY_ID_KSZ9031, MICREL_PHY_ID_MASK }, 4208bff5b4b3SYuiko Oshino { PHY_ID_KSZ9131, MICREL_PHY_ID_MASK }, 4209ecd5a323SAlexander Stein { PHY_ID_KSZ8001, 0x00fffffc }, 4210f893a99eSFabio Estevam { PHY_ID_KS8737, MICREL_PHY_ID_MASK }, 4211212ea99aSMarek Vasut { PHY_ID_KSZ8021, 0x00ffffff }, 4212b818d1a7SHector Palacios { PHY_ID_KSZ8031, 0x00ffffff }, 4213f893a99eSFabio Estevam { PHY_ID_KSZ8041, MICREL_PHY_ID_MASK }, 4214f893a99eSFabio Estevam { PHY_ID_KSZ8051, MICREL_PHY_ID_MASK }, 4215f893a99eSFabio Estevam { PHY_ID_KSZ8061, MICREL_PHY_ID_MASK }, 4216f893a99eSFabio Estevam { PHY_ID_KSZ8081, MICREL_PHY_ID_MASK }, 4217f893a99eSFabio Estevam { PHY_ID_KSZ8873MLL, MICREL_PHY_ID_MASK }, 4218f893a99eSFabio Estevam { PHY_ID_KSZ886X, MICREL_PHY_ID_MASK }, 42191623ad8eSDivya Koppera { PHY_ID_LAN8814, MICREL_PHY_ID_MASK }, 42207c2dcfa2SHoratiu Vultur { PHY_ID_LAN8804, MICREL_PHY_ID_MASK }, 4221a8f1a19dSHoratiu Vultur { PHY_ID_LAN8841, MICREL_PHY_ID_MASK }, 422252a60ed2SDavid S. Miller { } 422352a60ed2SDavid S. Miller }; 422452a60ed2SDavid S. Miller 422552a60ed2SDavid S. Miller MODULE_DEVICE_TABLE(mdio, micrel_tbl); 4226