xref: /openbmc/linux/drivers/net/phy/micrel.c (revision cc75549548482ed653c23f212544e58cb38ea980)
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:
133e9c0700SHoratiu Vultur  *		Giga phys: ksz9021, ksz9031, ksz9131, lan8841, lan8814
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
193e9c0700SHoratiu Vultur  *			 ksz9477, lan8804
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 
255*cc755495SHoratiu Vultur /* Delay used to get the second part from the LTC */
256*cc755495SHoratiu Vultur #define LAN8841_GET_SEC_LTC_DELAY		(500 * NSEC_PER_MSEC)
257*cc755495SHoratiu Vultur 
2582b2427d0SAndrew Lunn struct kszphy_hw_stat {
2592b2427d0SAndrew Lunn 	const char *string;
2602b2427d0SAndrew Lunn 	u8 reg;
2612b2427d0SAndrew Lunn 	u8 bits;
2622b2427d0SAndrew Lunn };
2632b2427d0SAndrew Lunn 
2642b2427d0SAndrew Lunn static struct kszphy_hw_stat kszphy_hw_stats[] = {
2652b2427d0SAndrew Lunn 	{ "phy_receive_errors", 21, 16},
2662b2427d0SAndrew Lunn 	{ "phy_idle_errors", 10, 8 },
2672b2427d0SAndrew Lunn };
2682b2427d0SAndrew Lunn 
269e6a423a8SJohan Hovold struct kszphy_type {
270e6a423a8SJohan Hovold 	u32 led_mode_reg;
271c6f9575cSJohan Hovold 	u16 interrupt_level_mask;
27221b688daSDivya Koppera 	u16 cable_diag_reg;
27321b688daSDivya Koppera 	unsigned long pair_mask;
274a8f1a19dSHoratiu Vultur 	u16 disable_dll_tx_bit;
275a8f1a19dSHoratiu Vultur 	u16 disable_dll_rx_bit;
276a8f1a19dSHoratiu Vultur 	u16 disable_dll_mask;
2770f95903eSJohan Hovold 	bool has_broadcast_disable;
2782b0ba96cSSylvain Rochet 	bool has_nand_tree_disable;
27963f44b2bSJohan Hovold 	bool has_rmii_ref_clk_sel;
280e6a423a8SJohan Hovold };
281e6a423a8SJohan Hovold 
282ece19502SDivya Koppera /* Shared structure between the PHYs of the same package. */
283ece19502SDivya Koppera struct lan8814_shared_priv {
284ece19502SDivya Koppera 	struct phy_device *phydev;
285ece19502SDivya Koppera 	struct ptp_clock *ptp_clock;
286ece19502SDivya Koppera 	struct ptp_clock_info ptp_clock_info;
287ece19502SDivya Koppera 
288ece19502SDivya Koppera 	/* Reference counter to how many ports in the package are enabling the
289ece19502SDivya Koppera 	 * timestamping
290ece19502SDivya Koppera 	 */
291ece19502SDivya Koppera 	u8 ref;
292ece19502SDivya Koppera 
293ece19502SDivya Koppera 	/* Lock for ptp_clock and ref */
294ece19502SDivya Koppera 	struct mutex shared_lock;
295ece19502SDivya Koppera };
296ece19502SDivya Koppera 
297ece19502SDivya Koppera struct lan8814_ptp_rx_ts {
298ece19502SDivya Koppera 	struct list_head list;
299ece19502SDivya Koppera 	u32 seconds;
300ece19502SDivya Koppera 	u32 nsec;
301ece19502SDivya Koppera 	u16 seq_id;
302ece19502SDivya Koppera };
303ece19502SDivya Koppera 
304ece19502SDivya Koppera struct kszphy_ptp_priv {
305ece19502SDivya Koppera 	struct mii_timestamper mii_ts;
306ece19502SDivya Koppera 	struct phy_device *phydev;
307ece19502SDivya Koppera 
308ece19502SDivya Koppera 	struct sk_buff_head tx_queue;
309ece19502SDivya Koppera 	struct sk_buff_head rx_queue;
310ece19502SDivya Koppera 
311ece19502SDivya Koppera 	struct list_head rx_ts_list;
312ece19502SDivya Koppera 	/* Lock for Rx ts fifo */
313ece19502SDivya Koppera 	spinlock_t rx_ts_lock;
314ece19502SDivya Koppera 
315ece19502SDivya Koppera 	int hwts_tx_type;
316ece19502SDivya Koppera 	enum hwtstamp_rx_filters rx_filter;
317ece19502SDivya Koppera 	int layer;
318ece19502SDivya Koppera 	int version;
319cafc3662SHoratiu Vultur 
320cafc3662SHoratiu Vultur 	struct ptp_clock *ptp_clock;
321cafc3662SHoratiu Vultur 	struct ptp_clock_info ptp_clock_info;
322cafc3662SHoratiu Vultur 	/* Lock for ptp_clock */
323cafc3662SHoratiu Vultur 	struct mutex ptp_lock;
324e4ed8ba0SHoratiu Vultur 	struct ptp_pin_desc *pin_config;
325*cc755495SHoratiu Vultur 
326*cc755495SHoratiu Vultur 	s64 seconds;
327*cc755495SHoratiu Vultur 	/* Lock for accessing seconds */
328*cc755495SHoratiu Vultur 	spinlock_t seconds_lock;
329ece19502SDivya Koppera };
330ece19502SDivya Koppera 
331e6a423a8SJohan Hovold struct kszphy_priv {
332ece19502SDivya Koppera 	struct kszphy_ptp_priv ptp_priv;
333e6a423a8SJohan Hovold 	const struct kszphy_type *type;
334e7a792e9SJohan Hovold 	int led_mode;
33558389c00SMarek Vasut 	u16 vct_ctrl1000;
33663f44b2bSJohan Hovold 	bool rmii_ref_clk_sel;
33763f44b2bSJohan Hovold 	bool rmii_ref_clk_sel_val;
3382b2427d0SAndrew Lunn 	u64 stats[ARRAY_SIZE(kszphy_hw_stats)];
339e6a423a8SJohan Hovold };
340e6a423a8SJohan Hovold 
341a516b7f7SDivya Koppera static const struct kszphy_type lan8814_type = {
342a516b7f7SDivya Koppera 	.led_mode_reg		= ~LAN8814_LED_CTRL_1,
34321b688daSDivya Koppera 	.cable_diag_reg		= LAN8814_CABLE_DIAG,
34421b688daSDivya Koppera 	.pair_mask		= LAN8814_WIRE_PAIR_MASK,
34521b688daSDivya Koppera };
34621b688daSDivya Koppera 
34721b688daSDivya Koppera static const struct kszphy_type ksz886x_type = {
34821b688daSDivya Koppera 	.cable_diag_reg		= KSZ8081_LMD,
34921b688daSDivya Koppera 	.pair_mask		= KSZPHY_WIRE_PAIR_MASK,
350a516b7f7SDivya Koppera };
351a516b7f7SDivya Koppera 
352e6a423a8SJohan Hovold static const struct kszphy_type ksz8021_type = {
353e6a423a8SJohan Hovold 	.led_mode_reg		= MII_KSZPHY_CTRL_2,
354d0e1df9cSJohan Hovold 	.has_broadcast_disable	= true,
3552b0ba96cSSylvain Rochet 	.has_nand_tree_disable	= true,
35663f44b2bSJohan Hovold 	.has_rmii_ref_clk_sel	= true,
357e6a423a8SJohan Hovold };
358e6a423a8SJohan Hovold 
359e6a423a8SJohan Hovold static const struct kszphy_type ksz8041_type = {
360e6a423a8SJohan Hovold 	.led_mode_reg		= MII_KSZPHY_CTRL_1,
361e6a423a8SJohan Hovold };
362e6a423a8SJohan Hovold 
363e6a423a8SJohan Hovold static const struct kszphy_type ksz8051_type = {
364e6a423a8SJohan Hovold 	.led_mode_reg		= MII_KSZPHY_CTRL_2,
3652b0ba96cSSylvain Rochet 	.has_nand_tree_disable	= true,
366e6a423a8SJohan Hovold };
367e6a423a8SJohan Hovold 
368e6a423a8SJohan Hovold static const struct kszphy_type ksz8081_type = {
369e6a423a8SJohan Hovold 	.led_mode_reg		= MII_KSZPHY_CTRL_2,
3700f95903eSJohan Hovold 	.has_broadcast_disable	= true,
3712b0ba96cSSylvain Rochet 	.has_nand_tree_disable	= true,
37286dc1342SJohan Hovold 	.has_rmii_ref_clk_sel	= true,
373e6a423a8SJohan Hovold };
374e6a423a8SJohan Hovold 
375c6f9575cSJohan Hovold static const struct kszphy_type ks8737_type = {
376c6f9575cSJohan Hovold 	.interrupt_level_mask	= BIT(14),
377c6f9575cSJohan Hovold };
378c6f9575cSJohan Hovold 
379c6f9575cSJohan Hovold static const struct kszphy_type ksz9021_type = {
380c6f9575cSJohan Hovold 	.interrupt_level_mask	= BIT(14),
381c6f9575cSJohan Hovold };
382c6f9575cSJohan Hovold 
383a8f1a19dSHoratiu Vultur static const struct kszphy_type ksz9131_type = {
384a8f1a19dSHoratiu Vultur 	.interrupt_level_mask	= BIT(14),
385a8f1a19dSHoratiu Vultur 	.disable_dll_tx_bit	= BIT(12),
386a8f1a19dSHoratiu Vultur 	.disable_dll_rx_bit	= BIT(12),
387a8f1a19dSHoratiu Vultur 	.disable_dll_mask	= BIT_MASK(12),
388a8f1a19dSHoratiu Vultur };
389a8f1a19dSHoratiu Vultur 
390a8f1a19dSHoratiu Vultur static const struct kszphy_type lan8841_type = {
391a8f1a19dSHoratiu Vultur 	.disable_dll_tx_bit	= BIT(14),
392a8f1a19dSHoratiu Vultur 	.disable_dll_rx_bit	= BIT(14),
393a8f1a19dSHoratiu Vultur 	.disable_dll_mask	= BIT_MASK(14),
394a136391aSHoratiu Vultur 	.cable_diag_reg		= LAN8814_CABLE_DIAG,
395a136391aSHoratiu Vultur 	.pair_mask		= LAN8814_WIRE_PAIR_MASK,
396a8f1a19dSHoratiu Vultur };
397a8f1a19dSHoratiu Vultur 
398954c3967SSean Cross static int kszphy_extended_write(struct phy_device *phydev,
399954c3967SSean Cross 				u32 regnum, u16 val)
400954c3967SSean Cross {
401954c3967SSean Cross 	phy_write(phydev, MII_KSZPHY_EXTREG, KSZPHY_EXTREG_WRITE | regnum);
402954c3967SSean Cross 	return phy_write(phydev, MII_KSZPHY_EXTREG_WRITE, val);
403954c3967SSean Cross }
404954c3967SSean Cross 
405954c3967SSean Cross static int kszphy_extended_read(struct phy_device *phydev,
406954c3967SSean Cross 				u32 regnum)
407954c3967SSean Cross {
408954c3967SSean Cross 	phy_write(phydev, MII_KSZPHY_EXTREG, regnum);
409954c3967SSean Cross 	return phy_read(phydev, MII_KSZPHY_EXTREG_READ);
410954c3967SSean Cross }
411954c3967SSean Cross 
41251f932c4SChoi, David static int kszphy_ack_interrupt(struct phy_device *phydev)
41351f932c4SChoi, David {
41451f932c4SChoi, David 	/* bit[7..0] int status, which is a read and clear register. */
41551f932c4SChoi, David 	int rc;
41651f932c4SChoi, David 
41751f932c4SChoi, David 	rc = phy_read(phydev, MII_KSZPHY_INTCS);
41851f932c4SChoi, David 
41951f932c4SChoi, David 	return (rc < 0) ? rc : 0;
42051f932c4SChoi, David }
42151f932c4SChoi, David 
42251f932c4SChoi, David static int kszphy_config_intr(struct phy_device *phydev)
42351f932c4SChoi, David {
424c6f9575cSJohan Hovold 	const struct kszphy_type *type = phydev->drv->driver_data;
425c0c99d0cSIoana Ciornei 	int temp, err;
426c6f9575cSJohan Hovold 	u16 mask;
427c6f9575cSJohan Hovold 
428c6f9575cSJohan Hovold 	if (type && type->interrupt_level_mask)
429c6f9575cSJohan Hovold 		mask = type->interrupt_level_mask;
430c6f9575cSJohan Hovold 	else
431c6f9575cSJohan Hovold 		mask = KSZPHY_CTRL_INT_ACTIVE_HIGH;
43251f932c4SChoi, David 
43351f932c4SChoi, David 	/* set the interrupt pin active low */
43451f932c4SChoi, David 	temp = phy_read(phydev, MII_KSZPHY_CTRL);
4355bb8fc0dSJohan Hovold 	if (temp < 0)
4365bb8fc0dSJohan Hovold 		return temp;
437c6f9575cSJohan Hovold 	temp &= ~mask;
43851f932c4SChoi, David 	phy_write(phydev, MII_KSZPHY_CTRL, temp);
43951f932c4SChoi, David 
440c6f9575cSJohan Hovold 	/* enable / disable interrupts */
441c0c99d0cSIoana Ciornei 	if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
442c0c99d0cSIoana Ciornei 		err = kszphy_ack_interrupt(phydev);
443c0c99d0cSIoana Ciornei 		if (err)
444c0c99d0cSIoana Ciornei 			return err;
44551f932c4SChoi, David 
446a57cc54dSWolfram Sang 		err = phy_write(phydev, MII_KSZPHY_INTCS, KSZPHY_INTCS_ALL);
447c0c99d0cSIoana Ciornei 	} else {
448a57cc54dSWolfram Sang 		err = phy_write(phydev, MII_KSZPHY_INTCS, 0);
449c0c99d0cSIoana Ciornei 		if (err)
450c0c99d0cSIoana Ciornei 			return err;
451c0c99d0cSIoana Ciornei 
452c0c99d0cSIoana Ciornei 		err = kszphy_ack_interrupt(phydev);
453c0c99d0cSIoana Ciornei 	}
454c0c99d0cSIoana Ciornei 
455c0c99d0cSIoana Ciornei 	return err;
45651f932c4SChoi, David }
457d0507009SDavid J. Choi 
45859ca4e58SIoana Ciornei static irqreturn_t kszphy_handle_interrupt(struct phy_device *phydev)
45959ca4e58SIoana Ciornei {
46059ca4e58SIoana Ciornei 	int irq_status;
46159ca4e58SIoana Ciornei 
46259ca4e58SIoana Ciornei 	irq_status = phy_read(phydev, MII_KSZPHY_INTCS);
46359ca4e58SIoana Ciornei 	if (irq_status < 0) {
46459ca4e58SIoana Ciornei 		phy_error(phydev);
46559ca4e58SIoana Ciornei 		return IRQ_NONE;
46659ca4e58SIoana Ciornei 	}
46759ca4e58SIoana Ciornei 
468fff4c746SOleksij Rempel 	if (!(irq_status & KSZPHY_INTCS_STATUS))
46959ca4e58SIoana Ciornei 		return IRQ_NONE;
47059ca4e58SIoana Ciornei 
47159ca4e58SIoana Ciornei 	phy_trigger_machine(phydev);
47259ca4e58SIoana Ciornei 
47359ca4e58SIoana Ciornei 	return IRQ_HANDLED;
47459ca4e58SIoana Ciornei }
47559ca4e58SIoana Ciornei 
47663f44b2bSJohan Hovold static int kszphy_rmii_clk_sel(struct phy_device *phydev, bool val)
47763f44b2bSJohan Hovold {
47863f44b2bSJohan Hovold 	int ctrl;
47963f44b2bSJohan Hovold 
48063f44b2bSJohan Hovold 	ctrl = phy_read(phydev, MII_KSZPHY_CTRL);
48163f44b2bSJohan Hovold 	if (ctrl < 0)
48263f44b2bSJohan Hovold 		return ctrl;
48363f44b2bSJohan Hovold 
48463f44b2bSJohan Hovold 	if (val)
48563f44b2bSJohan Hovold 		ctrl |= KSZPHY_RMII_REF_CLK_SEL;
48663f44b2bSJohan Hovold 	else
48763f44b2bSJohan Hovold 		ctrl &= ~KSZPHY_RMII_REF_CLK_SEL;
48863f44b2bSJohan Hovold 
48963f44b2bSJohan Hovold 	return phy_write(phydev, MII_KSZPHY_CTRL, ctrl);
49063f44b2bSJohan Hovold }
49163f44b2bSJohan Hovold 
492e7a792e9SJohan Hovold static int kszphy_setup_led(struct phy_device *phydev, u32 reg, int val)
49320d8435aSBen Dooks {
4945a16778eSJohan Hovold 	int rc, temp, shift;
4958620546cSJohan Hovold 
4965a16778eSJohan Hovold 	switch (reg) {
4975a16778eSJohan Hovold 	case MII_KSZPHY_CTRL_1:
4985a16778eSJohan Hovold 		shift = 14;
4995a16778eSJohan Hovold 		break;
5005a16778eSJohan Hovold 	case MII_KSZPHY_CTRL_2:
5015a16778eSJohan Hovold 		shift = 4;
5025a16778eSJohan Hovold 		break;
5035a16778eSJohan Hovold 	default:
5045a16778eSJohan Hovold 		return -EINVAL;
5055a16778eSJohan Hovold 	}
5065a16778eSJohan Hovold 
50720d8435aSBen Dooks 	temp = phy_read(phydev, reg);
508b7035860SJohan Hovold 	if (temp < 0) {
509b7035860SJohan Hovold 		rc = temp;
510b7035860SJohan Hovold 		goto out;
511b7035860SJohan Hovold 	}
51220d8435aSBen Dooks 
51328bdc499SSergei Shtylyov 	temp &= ~(3 << shift);
51420d8435aSBen Dooks 	temp |= val << shift;
51520d8435aSBen Dooks 	rc = phy_write(phydev, reg, temp);
516b7035860SJohan Hovold out:
517b7035860SJohan Hovold 	if (rc < 0)
51872ba48beSAndrew Lunn 		phydev_err(phydev, "failed to set led mode\n");
51920d8435aSBen Dooks 
520b7035860SJohan Hovold 	return rc;
52120d8435aSBen Dooks }
52220d8435aSBen Dooks 
523bde15129SJohan Hovold /* Disable PHY address 0 as the broadcast address, so that it can be used as a
524bde15129SJohan Hovold  * unique (non-broadcast) address on a shared bus.
525bde15129SJohan Hovold  */
526bde15129SJohan Hovold static int kszphy_broadcast_disable(struct phy_device *phydev)
527bde15129SJohan Hovold {
528bde15129SJohan Hovold 	int ret;
529bde15129SJohan Hovold 
530bde15129SJohan Hovold 	ret = phy_read(phydev, MII_KSZPHY_OMSO);
531bde15129SJohan Hovold 	if (ret < 0)
532bde15129SJohan Hovold 		goto out;
533bde15129SJohan Hovold 
534bde15129SJohan Hovold 	ret = phy_write(phydev, MII_KSZPHY_OMSO, ret | KSZPHY_OMSO_B_CAST_OFF);
535bde15129SJohan Hovold out:
536bde15129SJohan Hovold 	if (ret)
53772ba48beSAndrew Lunn 		phydev_err(phydev, "failed to disable broadcast address\n");
538bde15129SJohan Hovold 
539bde15129SJohan Hovold 	return ret;
540bde15129SJohan Hovold }
541bde15129SJohan Hovold 
5422b0ba96cSSylvain Rochet static int kszphy_nand_tree_disable(struct phy_device *phydev)
5432b0ba96cSSylvain Rochet {
5442b0ba96cSSylvain Rochet 	int ret;
5452b0ba96cSSylvain Rochet 
5462b0ba96cSSylvain Rochet 	ret = phy_read(phydev, MII_KSZPHY_OMSO);
5472b0ba96cSSylvain Rochet 	if (ret < 0)
5482b0ba96cSSylvain Rochet 		goto out;
5492b0ba96cSSylvain Rochet 
5502b0ba96cSSylvain Rochet 	if (!(ret & KSZPHY_OMSO_NAND_TREE_ON))
5512b0ba96cSSylvain Rochet 		return 0;
5522b0ba96cSSylvain Rochet 
5532b0ba96cSSylvain Rochet 	ret = phy_write(phydev, MII_KSZPHY_OMSO,
5542b0ba96cSSylvain Rochet 			ret & ~KSZPHY_OMSO_NAND_TREE_ON);
5552b0ba96cSSylvain Rochet out:
5562b0ba96cSSylvain Rochet 	if (ret)
55772ba48beSAndrew Lunn 		phydev_err(phydev, "failed to disable NAND tree mode\n");
5582b0ba96cSSylvain Rochet 
5592b0ba96cSSylvain Rochet 	return ret;
5602b0ba96cSSylvain Rochet }
5612b0ba96cSSylvain Rochet 
56279e498a9SLeonard Crestez /* Some config bits need to be set again on resume, handle them here. */
56379e498a9SLeonard Crestez static int kszphy_config_reset(struct phy_device *phydev)
56479e498a9SLeonard Crestez {
56579e498a9SLeonard Crestez 	struct kszphy_priv *priv = phydev->priv;
56679e498a9SLeonard Crestez 	int ret;
56779e498a9SLeonard Crestez 
56879e498a9SLeonard Crestez 	if (priv->rmii_ref_clk_sel) {
56979e498a9SLeonard Crestez 		ret = kszphy_rmii_clk_sel(phydev, priv->rmii_ref_clk_sel_val);
57079e498a9SLeonard Crestez 		if (ret) {
57179e498a9SLeonard Crestez 			phydev_err(phydev,
57279e498a9SLeonard Crestez 				   "failed to set rmii reference clock\n");
57379e498a9SLeonard Crestez 			return ret;
57479e498a9SLeonard Crestez 		}
57579e498a9SLeonard Crestez 	}
57679e498a9SLeonard Crestez 
577f2ef6f75SFabio Estevam 	if (priv->type && priv->led_mode >= 0)
57879e498a9SLeonard Crestez 		kszphy_setup_led(phydev, priv->type->led_mode_reg, priv->led_mode);
57979e498a9SLeonard Crestez 
58079e498a9SLeonard Crestez 	return 0;
58179e498a9SLeonard Crestez }
58279e498a9SLeonard Crestez 
583d0507009SDavid J. Choi static int kszphy_config_init(struct phy_device *phydev)
584d0507009SDavid J. Choi {
585e6a423a8SJohan Hovold 	struct kszphy_priv *priv = phydev->priv;
586e6a423a8SJohan Hovold 	const struct kszphy_type *type;
587d0507009SDavid J. Choi 
588e6a423a8SJohan Hovold 	if (!priv)
589e6a423a8SJohan Hovold 		return 0;
590e6a423a8SJohan Hovold 
591e6a423a8SJohan Hovold 	type = priv->type;
592e6a423a8SJohan Hovold 
593f2ef6f75SFabio Estevam 	if (type && type->has_broadcast_disable)
5940f95903eSJohan Hovold 		kszphy_broadcast_disable(phydev);
5950f95903eSJohan Hovold 
596f2ef6f75SFabio Estevam 	if (type && type->has_nand_tree_disable)
5972b0ba96cSSylvain Rochet 		kszphy_nand_tree_disable(phydev);
5982b0ba96cSSylvain Rochet 
59979e498a9SLeonard Crestez 	return kszphy_config_reset(phydev);
60020d8435aSBen Dooks }
60120d8435aSBen Dooks 
6024217a64eSMichael Walle static int ksz8041_fiber_mode(struct phy_device *phydev)
6034217a64eSMichael Walle {
6044217a64eSMichael Walle 	struct device_node *of_node = phydev->mdio.dev.of_node;
6054217a64eSMichael Walle 
6064217a64eSMichael Walle 	return of_property_read_bool(of_node, "micrel,fiber-mode");
6074217a64eSMichael Walle }
6084217a64eSMichael Walle 
60977501a79SPhilipp Zabel static int ksz8041_config_init(struct phy_device *phydev)
61077501a79SPhilipp Zabel {
6113c1bcc86SAndrew Lunn 	__ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
6123c1bcc86SAndrew Lunn 
61377501a79SPhilipp Zabel 	/* Limit supported and advertised modes in fiber mode */
6144217a64eSMichael Walle 	if (ksz8041_fiber_mode(phydev)) {
61577501a79SPhilipp Zabel 		phydev->dev_flags |= MICREL_PHY_FXEN;
6163c1bcc86SAndrew Lunn 		linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, mask);
6173c1bcc86SAndrew Lunn 		linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, mask);
6183c1bcc86SAndrew Lunn 
6193c1bcc86SAndrew Lunn 		linkmode_and(phydev->supported, phydev->supported, mask);
6203c1bcc86SAndrew Lunn 		linkmode_set_bit(ETHTOOL_LINK_MODE_FIBRE_BIT,
6213c1bcc86SAndrew Lunn 				 phydev->supported);
6223c1bcc86SAndrew Lunn 		linkmode_and(phydev->advertising, phydev->advertising, mask);
6233c1bcc86SAndrew Lunn 		linkmode_set_bit(ETHTOOL_LINK_MODE_FIBRE_BIT,
6243c1bcc86SAndrew Lunn 				 phydev->advertising);
62577501a79SPhilipp Zabel 		phydev->autoneg = AUTONEG_DISABLE;
62677501a79SPhilipp Zabel 	}
62777501a79SPhilipp Zabel 
62877501a79SPhilipp Zabel 	return kszphy_config_init(phydev);
62977501a79SPhilipp Zabel }
63077501a79SPhilipp Zabel 
63177501a79SPhilipp Zabel static int ksz8041_config_aneg(struct phy_device *phydev)
63277501a79SPhilipp Zabel {
63377501a79SPhilipp Zabel 	/* Skip auto-negotiation in fiber mode */
63477501a79SPhilipp Zabel 	if (phydev->dev_flags & MICREL_PHY_FXEN) {
63577501a79SPhilipp Zabel 		phydev->speed = SPEED_100;
63677501a79SPhilipp Zabel 		return 0;
63777501a79SPhilipp Zabel 	}
63877501a79SPhilipp Zabel 
63977501a79SPhilipp Zabel 	return genphy_config_aneg(phydev);
64077501a79SPhilipp Zabel }
64177501a79SPhilipp Zabel 
6428b95599cSMarek Vasut static int ksz8051_ksz8795_match_phy_device(struct phy_device *phydev,
643a5e63c7dSSteve Bennett 					    const bool ksz_8051)
6448b95599cSMarek Vasut {
6458b95599cSMarek Vasut 	int ret;
6468b95599cSMarek Vasut 
6474b159f50SRussell King 	if (!phy_id_compare(phydev->phy_id, PHY_ID_KSZ8051, MICREL_PHY_ID_MASK))
6488b95599cSMarek Vasut 		return 0;
6498b95599cSMarek Vasut 
6508b95599cSMarek Vasut 	ret = phy_read(phydev, MII_BMSR);
6518b95599cSMarek Vasut 	if (ret < 0)
6528b95599cSMarek Vasut 		return ret;
6538b95599cSMarek Vasut 
6548b95599cSMarek Vasut 	/* KSZ8051 PHY and KSZ8794/KSZ8795/KSZ8765 switch share the same
6558b95599cSMarek Vasut 	 * exact PHY ID. However, they can be told apart by the extended
6568b95599cSMarek Vasut 	 * capability registers presence. The KSZ8051 PHY has them while
6578b95599cSMarek Vasut 	 * the switch does not.
6588b95599cSMarek Vasut 	 */
6598b95599cSMarek Vasut 	ret &= BMSR_ERCAP;
660a5e63c7dSSteve Bennett 	if (ksz_8051)
6618b95599cSMarek Vasut 		return ret;
6628b95599cSMarek Vasut 	else
6638b95599cSMarek Vasut 		return !ret;
6648b95599cSMarek Vasut }
6658b95599cSMarek Vasut 
6668b95599cSMarek Vasut static int ksz8051_match_phy_device(struct phy_device *phydev)
6678b95599cSMarek Vasut {
668a5e63c7dSSteve Bennett 	return ksz8051_ksz8795_match_phy_device(phydev, true);
6698b95599cSMarek Vasut }
6708b95599cSMarek Vasut 
6717a1d8390SAntoine Tenart static int ksz8081_config_init(struct phy_device *phydev)
6727a1d8390SAntoine Tenart {
6737a1d8390SAntoine Tenart 	/* KSZPHY_OMSO_FACTORY_TEST is set at de-assertion of the reset line
6747a1d8390SAntoine Tenart 	 * based on the RXER (KSZ8081RNA/RND) or TXC (KSZ8081MNX/RNB) pin. If a
6757a1d8390SAntoine Tenart 	 * pull-down is missing, the factory test mode should be cleared by
6767a1d8390SAntoine Tenart 	 * manually writing a 0.
6777a1d8390SAntoine Tenart 	 */
6787a1d8390SAntoine Tenart 	phy_clear_bits(phydev, MII_KSZPHY_OMSO, KSZPHY_OMSO_FACTORY_TEST);
6797a1d8390SAntoine Tenart 
6807a1d8390SAntoine Tenart 	return kszphy_config_init(phydev);
6817a1d8390SAntoine Tenart }
6827a1d8390SAntoine Tenart 
683f873f112SOleksij Rempel static int ksz8081_config_mdix(struct phy_device *phydev, u8 ctrl)
684f873f112SOleksij Rempel {
685f873f112SOleksij Rempel 	u16 val;
686f873f112SOleksij Rempel 
687f873f112SOleksij Rempel 	switch (ctrl) {
688f873f112SOleksij Rempel 	case ETH_TP_MDI:
689f873f112SOleksij Rempel 		val = KSZ8081_CTRL2_DISABLE_AUTO_MDIX;
690f873f112SOleksij Rempel 		break;
691f873f112SOleksij Rempel 	case ETH_TP_MDI_X:
692f873f112SOleksij Rempel 		val = KSZ8081_CTRL2_DISABLE_AUTO_MDIX |
693f873f112SOleksij Rempel 			KSZ8081_CTRL2_MDI_MDI_X_SELECT;
694f873f112SOleksij Rempel 		break;
695f873f112SOleksij Rempel 	case ETH_TP_MDI_AUTO:
696f873f112SOleksij Rempel 		val = 0;
697f873f112SOleksij Rempel 		break;
698f873f112SOleksij Rempel 	default:
699f873f112SOleksij Rempel 		return 0;
700f873f112SOleksij Rempel 	}
701f873f112SOleksij Rempel 
702f873f112SOleksij Rempel 	return phy_modify(phydev, MII_KSZPHY_CTRL_2,
703f873f112SOleksij Rempel 			  KSZ8081_CTRL2_HP_MDIX |
704f873f112SOleksij Rempel 			  KSZ8081_CTRL2_MDI_MDI_X_SELECT |
705f873f112SOleksij Rempel 			  KSZ8081_CTRL2_DISABLE_AUTO_MDIX,
706f873f112SOleksij Rempel 			  KSZ8081_CTRL2_HP_MDIX | val);
707f873f112SOleksij Rempel }
708f873f112SOleksij Rempel 
709f873f112SOleksij Rempel static int ksz8081_config_aneg(struct phy_device *phydev)
710f873f112SOleksij Rempel {
711f873f112SOleksij Rempel 	int ret;
712f873f112SOleksij Rempel 
713f873f112SOleksij Rempel 	ret = genphy_config_aneg(phydev);
714f873f112SOleksij Rempel 	if (ret)
715f873f112SOleksij Rempel 		return ret;
716f873f112SOleksij Rempel 
717f873f112SOleksij Rempel 	/* The MDI-X configuration is automatically changed by the PHY after
718f873f112SOleksij Rempel 	 * switching from autoneg off to on. So, take MDI-X configuration under
719f873f112SOleksij Rempel 	 * own control and set it after autoneg configuration was done.
720f873f112SOleksij Rempel 	 */
721f873f112SOleksij Rempel 	return ksz8081_config_mdix(phydev, phydev->mdix_ctrl);
722f873f112SOleksij Rempel }
723f873f112SOleksij Rempel 
724f873f112SOleksij Rempel static int ksz8081_mdix_update(struct phy_device *phydev)
725f873f112SOleksij Rempel {
726f873f112SOleksij Rempel 	int ret;
727f873f112SOleksij Rempel 
728f873f112SOleksij Rempel 	ret = phy_read(phydev, MII_KSZPHY_CTRL_2);
729f873f112SOleksij Rempel 	if (ret < 0)
730f873f112SOleksij Rempel 		return ret;
731f873f112SOleksij Rempel 
732f873f112SOleksij Rempel 	if (ret & KSZ8081_CTRL2_DISABLE_AUTO_MDIX) {
733f873f112SOleksij Rempel 		if (ret & KSZ8081_CTRL2_MDI_MDI_X_SELECT)
734f873f112SOleksij Rempel 			phydev->mdix_ctrl = ETH_TP_MDI_X;
735f873f112SOleksij Rempel 		else
736f873f112SOleksij Rempel 			phydev->mdix_ctrl = ETH_TP_MDI;
737f873f112SOleksij Rempel 	} else {
738f873f112SOleksij Rempel 		phydev->mdix_ctrl = ETH_TP_MDI_AUTO;
739f873f112SOleksij Rempel 	}
740f873f112SOleksij Rempel 
741f873f112SOleksij Rempel 	ret = phy_read(phydev, MII_KSZPHY_CTRL_1);
742f873f112SOleksij Rempel 	if (ret < 0)
743f873f112SOleksij Rempel 		return ret;
744f873f112SOleksij Rempel 
745f873f112SOleksij Rempel 	if (ret & KSZ8081_CTRL1_MDIX_STAT)
746f873f112SOleksij Rempel 		phydev->mdix = ETH_TP_MDI;
747f873f112SOleksij Rempel 	else
748f873f112SOleksij Rempel 		phydev->mdix = ETH_TP_MDI_X;
749f873f112SOleksij Rempel 
750f873f112SOleksij Rempel 	return 0;
751f873f112SOleksij Rempel }
752f873f112SOleksij Rempel 
753f873f112SOleksij Rempel static int ksz8081_read_status(struct phy_device *phydev)
754f873f112SOleksij Rempel {
755f873f112SOleksij Rempel 	int ret;
756f873f112SOleksij Rempel 
757f873f112SOleksij Rempel 	ret = ksz8081_mdix_update(phydev);
758f873f112SOleksij Rempel 	if (ret < 0)
759f873f112SOleksij Rempel 		return ret;
760f873f112SOleksij Rempel 
761f873f112SOleksij Rempel 	return genphy_read_status(phydev);
762f873f112SOleksij Rempel }
763f873f112SOleksij Rempel 
764232ba3a5SRajasingh Thavamani static int ksz8061_config_init(struct phy_device *phydev)
765232ba3a5SRajasingh Thavamani {
766232ba3a5SRajasingh Thavamani 	int ret;
767232ba3a5SRajasingh Thavamani 
768232ba3a5SRajasingh Thavamani 	ret = phy_write_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_DEVID1, 0xB61A);
769232ba3a5SRajasingh Thavamani 	if (ret)
770232ba3a5SRajasingh Thavamani 		return ret;
771232ba3a5SRajasingh Thavamani 
772232ba3a5SRajasingh Thavamani 	return kszphy_config_init(phydev);
773232ba3a5SRajasingh Thavamani }
774232ba3a5SRajasingh Thavamani 
7758b95599cSMarek Vasut static int ksz8795_match_phy_device(struct phy_device *phydev)
7768b95599cSMarek Vasut {
777a5e63c7dSSteve Bennett 	return ksz8051_ksz8795_match_phy_device(phydev, false);
7788b95599cSMarek Vasut }
7798b95599cSMarek Vasut 
780954c3967SSean Cross static int ksz9021_load_values_from_of(struct phy_device *phydev,
7813c9a9f7fSJaeden Amero 				       const struct device_node *of_node,
7823c9a9f7fSJaeden Amero 				       u16 reg,
7833c9a9f7fSJaeden Amero 				       const char *field1, const char *field2,
7843c9a9f7fSJaeden Amero 				       const char *field3, const char *field4)
785954c3967SSean Cross {
786954c3967SSean Cross 	int val1 = -1;
787954c3967SSean Cross 	int val2 = -2;
788954c3967SSean Cross 	int val3 = -3;
789954c3967SSean Cross 	int val4 = -4;
790954c3967SSean Cross 	int newval;
791954c3967SSean Cross 	int matches = 0;
792954c3967SSean Cross 
793954c3967SSean Cross 	if (!of_property_read_u32(of_node, field1, &val1))
794954c3967SSean Cross 		matches++;
795954c3967SSean Cross 
796954c3967SSean Cross 	if (!of_property_read_u32(of_node, field2, &val2))
797954c3967SSean Cross 		matches++;
798954c3967SSean Cross 
799954c3967SSean Cross 	if (!of_property_read_u32(of_node, field3, &val3))
800954c3967SSean Cross 		matches++;
801954c3967SSean Cross 
802954c3967SSean Cross 	if (!of_property_read_u32(of_node, field4, &val4))
803954c3967SSean Cross 		matches++;
804954c3967SSean Cross 
805954c3967SSean Cross 	if (!matches)
806954c3967SSean Cross 		return 0;
807954c3967SSean Cross 
808954c3967SSean Cross 	if (matches < 4)
809954c3967SSean Cross 		newval = kszphy_extended_read(phydev, reg);
810954c3967SSean Cross 	else
811954c3967SSean Cross 		newval = 0;
812954c3967SSean Cross 
813954c3967SSean Cross 	if (val1 != -1)
814954c3967SSean Cross 		newval = ((newval & 0xfff0) | ((val1 / PS_TO_REG) & 0xf) << 0);
815954c3967SSean Cross 
8166a119745SHubert Chaumette 	if (val2 != -2)
817954c3967SSean Cross 		newval = ((newval & 0xff0f) | ((val2 / PS_TO_REG) & 0xf) << 4);
818954c3967SSean Cross 
8196a119745SHubert Chaumette 	if (val3 != -3)
820954c3967SSean Cross 		newval = ((newval & 0xf0ff) | ((val3 / PS_TO_REG) & 0xf) << 8);
821954c3967SSean Cross 
8226a119745SHubert Chaumette 	if (val4 != -4)
823954c3967SSean Cross 		newval = ((newval & 0x0fff) | ((val4 / PS_TO_REG) & 0xf) << 12);
824954c3967SSean Cross 
825954c3967SSean Cross 	return kszphy_extended_write(phydev, reg, newval);
826954c3967SSean Cross }
827954c3967SSean Cross 
828954c3967SSean Cross static int ksz9021_config_init(struct phy_device *phydev)
829954c3967SSean Cross {
830ce4f8afdSColin Ian King 	const struct device_node *of_node;
831651df218SAndrew Lunn 	const struct device *dev_walker;
832954c3967SSean Cross 
833651df218SAndrew Lunn 	/* The Micrel driver has a deprecated option to place phy OF
834651df218SAndrew Lunn 	 * properties in the MAC node. Walk up the tree of devices to
835651df218SAndrew Lunn 	 * find a device with an OF node.
836651df218SAndrew Lunn 	 */
837e5a03bfdSAndrew Lunn 	dev_walker = &phydev->mdio.dev;
838651df218SAndrew Lunn 	do {
839651df218SAndrew Lunn 		of_node = dev_walker->of_node;
840651df218SAndrew Lunn 		dev_walker = dev_walker->parent;
841651df218SAndrew Lunn 
842651df218SAndrew Lunn 	} while (!of_node && dev_walker);
843954c3967SSean Cross 
844954c3967SSean Cross 	if (of_node) {
845954c3967SSean Cross 		ksz9021_load_values_from_of(phydev, of_node,
846954c3967SSean Cross 				    MII_KSZPHY_CLK_CONTROL_PAD_SKEW,
847954c3967SSean Cross 				    "txen-skew-ps", "txc-skew-ps",
848954c3967SSean Cross 				    "rxdv-skew-ps", "rxc-skew-ps");
849954c3967SSean Cross 		ksz9021_load_values_from_of(phydev, of_node,
850954c3967SSean Cross 				    MII_KSZPHY_RX_DATA_PAD_SKEW,
851954c3967SSean Cross 				    "rxd0-skew-ps", "rxd1-skew-ps",
852954c3967SSean Cross 				    "rxd2-skew-ps", "rxd3-skew-ps");
853954c3967SSean Cross 		ksz9021_load_values_from_of(phydev, of_node,
854954c3967SSean Cross 				    MII_KSZPHY_TX_DATA_PAD_SKEW,
855954c3967SSean Cross 				    "txd0-skew-ps", "txd1-skew-ps",
856954c3967SSean Cross 				    "txd2-skew-ps", "txd3-skew-ps");
857954c3967SSean Cross 	}
858954c3967SSean Cross 	return 0;
859954c3967SSean Cross }
860954c3967SSean Cross 
8616e4b8273SHubert Chaumette #define KSZ9031_PS_TO_REG		60
8626e4b8273SHubert Chaumette 
8636e4b8273SHubert Chaumette /* Extended registers */
8646270e1aeSJaeden Amero /* MMD Address 0x0 */
8656270e1aeSJaeden Amero #define MII_KSZ9031RN_FLP_BURST_TX_LO	3
8666270e1aeSJaeden Amero #define MII_KSZ9031RN_FLP_BURST_TX_HI	4
8676270e1aeSJaeden Amero 
868ae6c97bbSJaeden Amero /* MMD Address 0x2 */
8696e4b8273SHubert Chaumette #define MII_KSZ9031RN_CONTROL_PAD_SKEW	4
870bcf3440cSOleksij Rempel #define MII_KSZ9031RN_RX_CTL_M		GENMASK(7, 4)
871bcf3440cSOleksij Rempel #define MII_KSZ9031RN_TX_CTL_M		GENMASK(3, 0)
872bcf3440cSOleksij Rempel 
8736e4b8273SHubert Chaumette #define MII_KSZ9031RN_RX_DATA_PAD_SKEW	5
874bcf3440cSOleksij Rempel #define MII_KSZ9031RN_RXD3		GENMASK(15, 12)
875bcf3440cSOleksij Rempel #define MII_KSZ9031RN_RXD2		GENMASK(11, 8)
876bcf3440cSOleksij Rempel #define MII_KSZ9031RN_RXD1		GENMASK(7, 4)
877bcf3440cSOleksij Rempel #define MII_KSZ9031RN_RXD0		GENMASK(3, 0)
878bcf3440cSOleksij Rempel 
8796e4b8273SHubert Chaumette #define MII_KSZ9031RN_TX_DATA_PAD_SKEW	6
880bcf3440cSOleksij Rempel #define MII_KSZ9031RN_TXD3		GENMASK(15, 12)
881bcf3440cSOleksij Rempel #define MII_KSZ9031RN_TXD2		GENMASK(11, 8)
882bcf3440cSOleksij Rempel #define MII_KSZ9031RN_TXD1		GENMASK(7, 4)
883bcf3440cSOleksij Rempel #define MII_KSZ9031RN_TXD0		GENMASK(3, 0)
884bcf3440cSOleksij Rempel 
8856e4b8273SHubert Chaumette #define MII_KSZ9031RN_CLK_PAD_SKEW	8
886bcf3440cSOleksij Rempel #define MII_KSZ9031RN_GTX_CLK		GENMASK(9, 5)
887bcf3440cSOleksij Rempel #define MII_KSZ9031RN_RX_CLK		GENMASK(4, 0)
888bcf3440cSOleksij Rempel 
889bcf3440cSOleksij Rempel /* KSZ9031 has internal RGMII_IDRX = 1.2ns and RGMII_IDTX = 0ns. To
890bcf3440cSOleksij Rempel  * provide different RGMII options we need to configure delay offset
891bcf3440cSOleksij Rempel  * for each pad relative to build in delay.
892bcf3440cSOleksij Rempel  */
893bcf3440cSOleksij Rempel /* keep rx as "No delay adjustment" and set rx_clk to +0.60ns to get delays of
894bcf3440cSOleksij Rempel  * 1.80ns
895bcf3440cSOleksij Rempel  */
896bcf3440cSOleksij Rempel #define RX_ID				0x7
897bcf3440cSOleksij Rempel #define RX_CLK_ID			0x19
898bcf3440cSOleksij Rempel 
899bcf3440cSOleksij Rempel /* set rx to +0.30ns and rx_clk to -0.90ns to compensate the
900bcf3440cSOleksij Rempel  * internal 1.2ns delay.
901bcf3440cSOleksij Rempel  */
902bcf3440cSOleksij Rempel #define RX_ND				0xc
903bcf3440cSOleksij Rempel #define RX_CLK_ND			0x0
904bcf3440cSOleksij Rempel 
905bcf3440cSOleksij Rempel /* set tx to -0.42ns and tx_clk to +0.96ns to get 1.38ns delay */
906bcf3440cSOleksij Rempel #define TX_ID				0x0
907bcf3440cSOleksij Rempel #define TX_CLK_ID			0x1f
908bcf3440cSOleksij Rempel 
909bcf3440cSOleksij Rempel /* set tx and tx_clk to "No delay adjustment" to keep 0ns
910bcf3440cSOleksij Rempel  * dealy
911bcf3440cSOleksij Rempel  */
912bcf3440cSOleksij Rempel #define TX_ND				0x7
913bcf3440cSOleksij Rempel #define TX_CLK_ND			0xf
9146e4b8273SHubert Chaumette 
915af70c1f9SMike Looijmans /* MMD Address 0x1C */
916af70c1f9SMike Looijmans #define MII_KSZ9031RN_EDPD		0x23
917af70c1f9SMike Looijmans #define MII_KSZ9031RN_EDPD_ENABLE	BIT(0)
918af70c1f9SMike Looijmans 
9196e4b8273SHubert Chaumette static int ksz9031_of_load_skew_values(struct phy_device *phydev,
9203c9a9f7fSJaeden Amero 				       const struct device_node *of_node,
9216e4b8273SHubert Chaumette 				       u16 reg, size_t field_sz,
922bcf3440cSOleksij Rempel 				       const char *field[], u8 numfields,
923bcf3440cSOleksij Rempel 				       bool *update)
9246e4b8273SHubert Chaumette {
9256e4b8273SHubert Chaumette 	int val[4] = {-1, -2, -3, -4};
9266e4b8273SHubert Chaumette 	int matches = 0;
9276e4b8273SHubert Chaumette 	u16 mask;
9286e4b8273SHubert Chaumette 	u16 maxval;
9296e4b8273SHubert Chaumette 	u16 newval;
9306e4b8273SHubert Chaumette 	int i;
9316e4b8273SHubert Chaumette 
9326e4b8273SHubert Chaumette 	for (i = 0; i < numfields; i++)
9336e4b8273SHubert Chaumette 		if (!of_property_read_u32(of_node, field[i], val + i))
9346e4b8273SHubert Chaumette 			matches++;
9356e4b8273SHubert Chaumette 
9366e4b8273SHubert Chaumette 	if (!matches)
9376e4b8273SHubert Chaumette 		return 0;
9386e4b8273SHubert Chaumette 
939bcf3440cSOleksij Rempel 	*update |= true;
940bcf3440cSOleksij Rempel 
9416e4b8273SHubert Chaumette 	if (matches < numfields)
9429b420effSHeiner Kallweit 		newval = phy_read_mmd(phydev, 2, reg);
9436e4b8273SHubert Chaumette 	else
9446e4b8273SHubert Chaumette 		newval = 0;
9456e4b8273SHubert Chaumette 
9466e4b8273SHubert Chaumette 	maxval = (field_sz == 4) ? 0xf : 0x1f;
9476e4b8273SHubert Chaumette 	for (i = 0; i < numfields; i++)
9486e4b8273SHubert Chaumette 		if (val[i] != -(i + 1)) {
9496e4b8273SHubert Chaumette 			mask = 0xffff;
9506e4b8273SHubert Chaumette 			mask ^= maxval << (field_sz * i);
9516e4b8273SHubert Chaumette 			newval = (newval & mask) |
9526e4b8273SHubert Chaumette 				(((val[i] / KSZ9031_PS_TO_REG) & maxval)
9536e4b8273SHubert Chaumette 					<< (field_sz * i));
9546e4b8273SHubert Chaumette 		}
9556e4b8273SHubert Chaumette 
9569b420effSHeiner Kallweit 	return phy_write_mmd(phydev, 2, reg, newval);
9576e4b8273SHubert Chaumette }
9586e4b8273SHubert Chaumette 
959a0da456bSMax Uvarov /* Center KSZ9031RNX FLP timing at 16ms. */
9606270e1aeSJaeden Amero static int ksz9031_center_flp_timing(struct phy_device *phydev)
9616270e1aeSJaeden Amero {
9626270e1aeSJaeden Amero 	int result;
9636270e1aeSJaeden Amero 
9649b420effSHeiner Kallweit 	result = phy_write_mmd(phydev, 0, MII_KSZ9031RN_FLP_BURST_TX_HI,
9659b420effSHeiner Kallweit 			       0x0006);
966a0da456bSMax Uvarov 	if (result)
967a0da456bSMax Uvarov 		return result;
968a0da456bSMax Uvarov 
9699b420effSHeiner Kallweit 	result = phy_write_mmd(phydev, 0, MII_KSZ9031RN_FLP_BURST_TX_LO,
9709b420effSHeiner Kallweit 			       0x1A80);
9716270e1aeSJaeden Amero 	if (result)
9726270e1aeSJaeden Amero 		return result;
9736270e1aeSJaeden Amero 
9746270e1aeSJaeden Amero 	return genphy_restart_aneg(phydev);
9756270e1aeSJaeden Amero }
9766270e1aeSJaeden Amero 
977af70c1f9SMike Looijmans /* Enable energy-detect power-down mode */
978af70c1f9SMike Looijmans static int ksz9031_enable_edpd(struct phy_device *phydev)
979af70c1f9SMike Looijmans {
980af70c1f9SMike Looijmans 	int reg;
981af70c1f9SMike Looijmans 
9829b420effSHeiner Kallweit 	reg = phy_read_mmd(phydev, 0x1C, MII_KSZ9031RN_EDPD);
983af70c1f9SMike Looijmans 	if (reg < 0)
984af70c1f9SMike Looijmans 		return reg;
9859b420effSHeiner Kallweit 	return phy_write_mmd(phydev, 0x1C, MII_KSZ9031RN_EDPD,
986af70c1f9SMike Looijmans 			     reg | MII_KSZ9031RN_EDPD_ENABLE);
987af70c1f9SMike Looijmans }
988af70c1f9SMike Looijmans 
989bcf3440cSOleksij Rempel static int ksz9031_config_rgmii_delay(struct phy_device *phydev)
990bcf3440cSOleksij Rempel {
991bcf3440cSOleksij Rempel 	u16 rx, tx, rx_clk, tx_clk;
992bcf3440cSOleksij Rempel 	int ret;
993bcf3440cSOleksij Rempel 
994bcf3440cSOleksij Rempel 	switch (phydev->interface) {
995bcf3440cSOleksij Rempel 	case PHY_INTERFACE_MODE_RGMII:
996bcf3440cSOleksij Rempel 		tx = TX_ND;
997bcf3440cSOleksij Rempel 		tx_clk = TX_CLK_ND;
998bcf3440cSOleksij Rempel 		rx = RX_ND;
999bcf3440cSOleksij Rempel 		rx_clk = RX_CLK_ND;
1000bcf3440cSOleksij Rempel 		break;
1001bcf3440cSOleksij Rempel 	case PHY_INTERFACE_MODE_RGMII_ID:
1002bcf3440cSOleksij Rempel 		tx = TX_ID;
1003bcf3440cSOleksij Rempel 		tx_clk = TX_CLK_ID;
1004bcf3440cSOleksij Rempel 		rx = RX_ID;
1005bcf3440cSOleksij Rempel 		rx_clk = RX_CLK_ID;
1006bcf3440cSOleksij Rempel 		break;
1007bcf3440cSOleksij Rempel 	case PHY_INTERFACE_MODE_RGMII_RXID:
1008bcf3440cSOleksij Rempel 		tx = TX_ND;
1009bcf3440cSOleksij Rempel 		tx_clk = TX_CLK_ND;
1010bcf3440cSOleksij Rempel 		rx = RX_ID;
1011bcf3440cSOleksij Rempel 		rx_clk = RX_CLK_ID;
1012bcf3440cSOleksij Rempel 		break;
1013bcf3440cSOleksij Rempel 	case PHY_INTERFACE_MODE_RGMII_TXID:
1014bcf3440cSOleksij Rempel 		tx = TX_ID;
1015bcf3440cSOleksij Rempel 		tx_clk = TX_CLK_ID;
1016bcf3440cSOleksij Rempel 		rx = RX_ND;
1017bcf3440cSOleksij Rempel 		rx_clk = RX_CLK_ND;
1018bcf3440cSOleksij Rempel 		break;
1019bcf3440cSOleksij Rempel 	default:
1020bcf3440cSOleksij Rempel 		return 0;
1021bcf3440cSOleksij Rempel 	}
1022bcf3440cSOleksij Rempel 
1023bcf3440cSOleksij Rempel 	ret = phy_write_mmd(phydev, 2, MII_KSZ9031RN_CONTROL_PAD_SKEW,
1024bcf3440cSOleksij Rempel 			    FIELD_PREP(MII_KSZ9031RN_RX_CTL_M, rx) |
1025bcf3440cSOleksij Rempel 			    FIELD_PREP(MII_KSZ9031RN_TX_CTL_M, tx));
1026bcf3440cSOleksij Rempel 	if (ret < 0)
1027bcf3440cSOleksij Rempel 		return ret;
1028bcf3440cSOleksij Rempel 
1029bcf3440cSOleksij Rempel 	ret = phy_write_mmd(phydev, 2, MII_KSZ9031RN_RX_DATA_PAD_SKEW,
1030bcf3440cSOleksij Rempel 			    FIELD_PREP(MII_KSZ9031RN_RXD3, rx) |
1031bcf3440cSOleksij Rempel 			    FIELD_PREP(MII_KSZ9031RN_RXD2, rx) |
1032bcf3440cSOleksij Rempel 			    FIELD_PREP(MII_KSZ9031RN_RXD1, rx) |
1033bcf3440cSOleksij Rempel 			    FIELD_PREP(MII_KSZ9031RN_RXD0, rx));
1034bcf3440cSOleksij Rempel 	if (ret < 0)
1035bcf3440cSOleksij Rempel 		return ret;
1036bcf3440cSOleksij Rempel 
1037bcf3440cSOleksij Rempel 	ret = phy_write_mmd(phydev, 2, MII_KSZ9031RN_TX_DATA_PAD_SKEW,
1038bcf3440cSOleksij Rempel 			    FIELD_PREP(MII_KSZ9031RN_TXD3, tx) |
1039bcf3440cSOleksij Rempel 			    FIELD_PREP(MII_KSZ9031RN_TXD2, tx) |
1040bcf3440cSOleksij Rempel 			    FIELD_PREP(MII_KSZ9031RN_TXD1, tx) |
1041bcf3440cSOleksij Rempel 			    FIELD_PREP(MII_KSZ9031RN_TXD0, tx));
1042bcf3440cSOleksij Rempel 	if (ret < 0)
1043bcf3440cSOleksij Rempel 		return ret;
1044bcf3440cSOleksij Rempel 
1045bcf3440cSOleksij Rempel 	return phy_write_mmd(phydev, 2, MII_KSZ9031RN_CLK_PAD_SKEW,
1046bcf3440cSOleksij Rempel 			     FIELD_PREP(MII_KSZ9031RN_GTX_CLK, tx_clk) |
1047bcf3440cSOleksij Rempel 			     FIELD_PREP(MII_KSZ9031RN_RX_CLK, rx_clk));
1048bcf3440cSOleksij Rempel }
1049bcf3440cSOleksij Rempel 
10506e4b8273SHubert Chaumette static int ksz9031_config_init(struct phy_device *phydev)
10516e4b8273SHubert Chaumette {
1052ce4f8afdSColin Ian King 	const struct device_node *of_node;
10533c9a9f7fSJaeden Amero 	static const char *clk_skews[2] = {"rxc-skew-ps", "txc-skew-ps"};
10543c9a9f7fSJaeden Amero 	static const char *rx_data_skews[4] = {
10556e4b8273SHubert Chaumette 		"rxd0-skew-ps", "rxd1-skew-ps",
10566e4b8273SHubert Chaumette 		"rxd2-skew-ps", "rxd3-skew-ps"
10576e4b8273SHubert Chaumette 	};
10583c9a9f7fSJaeden Amero 	static const char *tx_data_skews[4] = {
10596e4b8273SHubert Chaumette 		"txd0-skew-ps", "txd1-skew-ps",
10606e4b8273SHubert Chaumette 		"txd2-skew-ps", "txd3-skew-ps"
10616e4b8273SHubert Chaumette 	};
10623c9a9f7fSJaeden Amero 	static const char *control_skews[2] = {"txen-skew-ps", "rxdv-skew-ps"};
1063b4c19f71SRoosen Henri 	const struct device *dev_walker;
1064af70c1f9SMike Looijmans 	int result;
1065af70c1f9SMike Looijmans 
1066af70c1f9SMike Looijmans 	result = ksz9031_enable_edpd(phydev);
1067af70c1f9SMike Looijmans 	if (result < 0)
1068af70c1f9SMike Looijmans 		return result;
10696e4b8273SHubert Chaumette 
1070b4c19f71SRoosen Henri 	/* The Micrel driver has a deprecated option to place phy OF
1071b4c19f71SRoosen Henri 	 * properties in the MAC node. Walk up the tree of devices to
1072b4c19f71SRoosen Henri 	 * find a device with an OF node.
1073b4c19f71SRoosen Henri 	 */
10749d367eddSDavid S. Miller 	dev_walker = &phydev->mdio.dev;
1075b4c19f71SRoosen Henri 	do {
1076b4c19f71SRoosen Henri 		of_node = dev_walker->of_node;
1077b4c19f71SRoosen Henri 		dev_walker = dev_walker->parent;
1078b4c19f71SRoosen Henri 	} while (!of_node && dev_walker);
10796e4b8273SHubert Chaumette 
10806e4b8273SHubert Chaumette 	if (of_node) {
1081bcf3440cSOleksij Rempel 		bool update = false;
1082bcf3440cSOleksij Rempel 
1083bcf3440cSOleksij Rempel 		if (phy_interface_is_rgmii(phydev)) {
1084bcf3440cSOleksij Rempel 			result = ksz9031_config_rgmii_delay(phydev);
1085bcf3440cSOleksij Rempel 			if (result < 0)
1086bcf3440cSOleksij Rempel 				return result;
1087bcf3440cSOleksij Rempel 		}
1088bcf3440cSOleksij Rempel 
10896e4b8273SHubert Chaumette 		ksz9031_of_load_skew_values(phydev, of_node,
10906e4b8273SHubert Chaumette 				MII_KSZ9031RN_CLK_PAD_SKEW, 5,
1091bcf3440cSOleksij Rempel 				clk_skews, 2, &update);
10926e4b8273SHubert Chaumette 
10936e4b8273SHubert Chaumette 		ksz9031_of_load_skew_values(phydev, of_node,
10946e4b8273SHubert Chaumette 				MII_KSZ9031RN_CONTROL_PAD_SKEW, 4,
1095bcf3440cSOleksij Rempel 				control_skews, 2, &update);
10966e4b8273SHubert Chaumette 
10976e4b8273SHubert Chaumette 		ksz9031_of_load_skew_values(phydev, of_node,
10986e4b8273SHubert Chaumette 				MII_KSZ9031RN_RX_DATA_PAD_SKEW, 4,
1099bcf3440cSOleksij Rempel 				rx_data_skews, 4, &update);
11006e4b8273SHubert Chaumette 
11016e4b8273SHubert Chaumette 		ksz9031_of_load_skew_values(phydev, of_node,
11026e4b8273SHubert Chaumette 				MII_KSZ9031RN_TX_DATA_PAD_SKEW, 4,
1103bcf3440cSOleksij Rempel 				tx_data_skews, 4, &update);
1104bcf3440cSOleksij Rempel 
110567ca5159SMatthias Schiffer 		if (update && !phy_interface_is_rgmii(phydev))
1106bcf3440cSOleksij Rempel 			phydev_warn(phydev,
110767ca5159SMatthias Schiffer 				    "*-skew-ps values should be used only with RGMII PHY modes\n");
1108e1b505a6SMarkus Niebel 
1109e1b505a6SMarkus Niebel 		/* Silicon Errata Sheet (DS80000691D or DS80000692D):
1110e1b505a6SMarkus Niebel 		 * When the device links in the 1000BASE-T slave mode only,
1111e1b505a6SMarkus Niebel 		 * the optional 125MHz reference output clock (CLK125_NDO)
1112e1b505a6SMarkus Niebel 		 * has wide duty cycle variation.
1113e1b505a6SMarkus Niebel 		 *
1114e1b505a6SMarkus Niebel 		 * The optional CLK125_NDO clock does not meet the RGMII
1115e1b505a6SMarkus Niebel 		 * 45/55 percent (min/max) duty cycle requirement and therefore
1116e1b505a6SMarkus Niebel 		 * cannot be used directly by the MAC side for clocking
1117e1b505a6SMarkus Niebel 		 * applications that have setup/hold time requirements on
1118e1b505a6SMarkus Niebel 		 * rising and falling clock edges.
1119e1b505a6SMarkus Niebel 		 *
1120e1b505a6SMarkus Niebel 		 * Workaround:
1121e1b505a6SMarkus Niebel 		 * Force the phy to be the master to receive a stable clock
1122e1b505a6SMarkus Niebel 		 * which meets the duty cycle requirement.
1123e1b505a6SMarkus Niebel 		 */
1124e1b505a6SMarkus Niebel 		if (of_property_read_bool(of_node, "micrel,force-master")) {
1125e1b505a6SMarkus Niebel 			result = phy_read(phydev, MII_CTRL1000);
1126e1b505a6SMarkus Niebel 			if (result < 0)
1127e1b505a6SMarkus Niebel 				goto err_force_master;
1128e1b505a6SMarkus Niebel 
1129e1b505a6SMarkus Niebel 			/* enable master mode, config & prefer master */
1130e1b505a6SMarkus Niebel 			result |= CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER;
1131e1b505a6SMarkus Niebel 			result = phy_write(phydev, MII_CTRL1000, result);
1132e1b505a6SMarkus Niebel 			if (result < 0)
1133e1b505a6SMarkus Niebel 				goto err_force_master;
1134e1b505a6SMarkus Niebel 		}
11356e4b8273SHubert Chaumette 	}
11366270e1aeSJaeden Amero 
11376270e1aeSJaeden Amero 	return ksz9031_center_flp_timing(phydev);
1138e1b505a6SMarkus Niebel 
1139e1b505a6SMarkus Niebel err_force_master:
1140e1b505a6SMarkus Niebel 	phydev_err(phydev, "failed to force the phy to master mode\n");
1141e1b505a6SMarkus Niebel 	return result;
11426e4b8273SHubert Chaumette }
11436e4b8273SHubert Chaumette 
1144bff5b4b3SYuiko Oshino #define KSZ9131_SKEW_5BIT_MAX	2400
1145bff5b4b3SYuiko Oshino #define KSZ9131_SKEW_4BIT_MAX	800
1146bff5b4b3SYuiko Oshino #define KSZ9131_OFFSET		700
1147bff5b4b3SYuiko Oshino #define KSZ9131_STEP		100
1148bff5b4b3SYuiko Oshino 
1149bff5b4b3SYuiko Oshino static int ksz9131_of_load_skew_values(struct phy_device *phydev,
1150bff5b4b3SYuiko Oshino 				       struct device_node *of_node,
1151bff5b4b3SYuiko Oshino 				       u16 reg, size_t field_sz,
1152bff5b4b3SYuiko Oshino 				       char *field[], u8 numfields)
1153bff5b4b3SYuiko Oshino {
1154bff5b4b3SYuiko Oshino 	int val[4] = {-(1 + KSZ9131_OFFSET), -(2 + KSZ9131_OFFSET),
1155bff5b4b3SYuiko Oshino 		      -(3 + KSZ9131_OFFSET), -(4 + KSZ9131_OFFSET)};
1156bff5b4b3SYuiko Oshino 	int skewval, skewmax = 0;
1157bff5b4b3SYuiko Oshino 	int matches = 0;
1158bff5b4b3SYuiko Oshino 	u16 maxval;
1159bff5b4b3SYuiko Oshino 	u16 newval;
1160bff5b4b3SYuiko Oshino 	u16 mask;
1161bff5b4b3SYuiko Oshino 	int i;
1162bff5b4b3SYuiko Oshino 
1163bff5b4b3SYuiko Oshino 	/* psec properties in dts should mean x pico seconds */
1164bff5b4b3SYuiko Oshino 	if (field_sz == 5)
1165bff5b4b3SYuiko Oshino 		skewmax = KSZ9131_SKEW_5BIT_MAX;
1166bff5b4b3SYuiko Oshino 	else
1167bff5b4b3SYuiko Oshino 		skewmax = KSZ9131_SKEW_4BIT_MAX;
1168bff5b4b3SYuiko Oshino 
1169bff5b4b3SYuiko Oshino 	for (i = 0; i < numfields; i++)
1170bff5b4b3SYuiko Oshino 		if (!of_property_read_s32(of_node, field[i], &skewval)) {
1171bff5b4b3SYuiko Oshino 			if (skewval < -KSZ9131_OFFSET)
1172bff5b4b3SYuiko Oshino 				skewval = -KSZ9131_OFFSET;
1173bff5b4b3SYuiko Oshino 			else if (skewval > skewmax)
1174bff5b4b3SYuiko Oshino 				skewval = skewmax;
1175bff5b4b3SYuiko Oshino 
1176bff5b4b3SYuiko Oshino 			val[i] = skewval + KSZ9131_OFFSET;
1177bff5b4b3SYuiko Oshino 			matches++;
1178bff5b4b3SYuiko Oshino 		}
1179bff5b4b3SYuiko Oshino 
1180bff5b4b3SYuiko Oshino 	if (!matches)
1181bff5b4b3SYuiko Oshino 		return 0;
1182bff5b4b3SYuiko Oshino 
1183bff5b4b3SYuiko Oshino 	if (matches < numfields)
11849b420effSHeiner Kallweit 		newval = phy_read_mmd(phydev, 2, reg);
1185bff5b4b3SYuiko Oshino 	else
1186bff5b4b3SYuiko Oshino 		newval = 0;
1187bff5b4b3SYuiko Oshino 
1188bff5b4b3SYuiko Oshino 	maxval = (field_sz == 4) ? 0xf : 0x1f;
1189bff5b4b3SYuiko Oshino 	for (i = 0; i < numfields; i++)
1190bff5b4b3SYuiko Oshino 		if (val[i] != -(i + 1 + KSZ9131_OFFSET)) {
1191bff5b4b3SYuiko Oshino 			mask = 0xffff;
1192bff5b4b3SYuiko Oshino 			mask ^= maxval << (field_sz * i);
1193bff5b4b3SYuiko Oshino 			newval = (newval & mask) |
1194bff5b4b3SYuiko Oshino 				(((val[i] / KSZ9131_STEP) & maxval)
1195bff5b4b3SYuiko Oshino 					<< (field_sz * i));
1196bff5b4b3SYuiko Oshino 		}
1197bff5b4b3SYuiko Oshino 
11989b420effSHeiner Kallweit 	return phy_write_mmd(phydev, 2, reg, newval);
1199bff5b4b3SYuiko Oshino }
1200bff5b4b3SYuiko Oshino 
1201bd734a74SPhilippe Schenker #define KSZ9131RN_MMD_COMMON_CTRL_REG	2
1202bd734a74SPhilippe Schenker #define KSZ9131RN_RXC_DLL_CTRL		76
1203bd734a74SPhilippe Schenker #define KSZ9131RN_TXC_DLL_CTRL		77
1204bd734a74SPhilippe Schenker #define KSZ9131RN_DLL_ENABLE_DELAY	0
1205bd734a74SPhilippe Schenker 
1206bd734a74SPhilippe Schenker static int ksz9131_config_rgmii_delay(struct phy_device *phydev)
1207bd734a74SPhilippe Schenker {
1208a8f1a19dSHoratiu Vultur 	const struct kszphy_type *type = phydev->drv->driver_data;
1209bd734a74SPhilippe Schenker 	u16 rxcdll_val, txcdll_val;
1210bd734a74SPhilippe Schenker 	int ret;
1211bd734a74SPhilippe Schenker 
1212bd734a74SPhilippe Schenker 	switch (phydev->interface) {
1213bd734a74SPhilippe Schenker 	case PHY_INTERFACE_MODE_RGMII:
1214a8f1a19dSHoratiu Vultur 		rxcdll_val = type->disable_dll_rx_bit;
1215a8f1a19dSHoratiu Vultur 		txcdll_val = type->disable_dll_tx_bit;
1216bd734a74SPhilippe Schenker 		break;
1217bd734a74SPhilippe Schenker 	case PHY_INTERFACE_MODE_RGMII_ID:
1218bd734a74SPhilippe Schenker 		rxcdll_val = KSZ9131RN_DLL_ENABLE_DELAY;
1219bd734a74SPhilippe Schenker 		txcdll_val = KSZ9131RN_DLL_ENABLE_DELAY;
1220bd734a74SPhilippe Schenker 		break;
1221bd734a74SPhilippe Schenker 	case PHY_INTERFACE_MODE_RGMII_RXID:
1222bd734a74SPhilippe Schenker 		rxcdll_val = KSZ9131RN_DLL_ENABLE_DELAY;
1223a8f1a19dSHoratiu Vultur 		txcdll_val = type->disable_dll_tx_bit;
1224bd734a74SPhilippe Schenker 		break;
1225bd734a74SPhilippe Schenker 	case PHY_INTERFACE_MODE_RGMII_TXID:
1226a8f1a19dSHoratiu Vultur 		rxcdll_val = type->disable_dll_rx_bit;
1227bd734a74SPhilippe Schenker 		txcdll_val = KSZ9131RN_DLL_ENABLE_DELAY;
1228bd734a74SPhilippe Schenker 		break;
1229bd734a74SPhilippe Schenker 	default:
1230bd734a74SPhilippe Schenker 		return 0;
1231bd734a74SPhilippe Schenker 	}
1232bd734a74SPhilippe Schenker 
1233bd734a74SPhilippe Schenker 	ret = phy_modify_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
1234a8f1a19dSHoratiu Vultur 			     KSZ9131RN_RXC_DLL_CTRL, type->disable_dll_mask,
1235bd734a74SPhilippe Schenker 			     rxcdll_val);
1236bd734a74SPhilippe Schenker 	if (ret < 0)
1237bd734a74SPhilippe Schenker 		return ret;
1238bd734a74SPhilippe Schenker 
1239bd734a74SPhilippe Schenker 	return phy_modify_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
1240a8f1a19dSHoratiu Vultur 			      KSZ9131RN_TXC_DLL_CTRL, type->disable_dll_mask,
1241bd734a74SPhilippe Schenker 			      txcdll_val);
1242bd734a74SPhilippe Schenker }
1243bd734a74SPhilippe Schenker 
12440316c7e6SFrancesco Dolcini /* Silicon Errata DS80000693B
12450316c7e6SFrancesco Dolcini  *
12460316c7e6SFrancesco Dolcini  * When LEDs are configured in Individual Mode, LED1 is ON in a no-link
12470316c7e6SFrancesco Dolcini  * condition. Workaround is to set register 0x1e, bit 9, this way LED1 behaves
12480316c7e6SFrancesco Dolcini  * according to the datasheet (off if there is no link).
12490316c7e6SFrancesco Dolcini  */
12500316c7e6SFrancesco Dolcini static int ksz9131_led_errata(struct phy_device *phydev)
12510316c7e6SFrancesco Dolcini {
12520316c7e6SFrancesco Dolcini 	int reg;
12530316c7e6SFrancesco Dolcini 
12540316c7e6SFrancesco Dolcini 	reg = phy_read_mmd(phydev, 2, 0);
12550316c7e6SFrancesco Dolcini 	if (reg < 0)
12560316c7e6SFrancesco Dolcini 		return reg;
12570316c7e6SFrancesco Dolcini 
12580316c7e6SFrancesco Dolcini 	if (!(reg & BIT(4)))
12590316c7e6SFrancesco Dolcini 		return 0;
12600316c7e6SFrancesco Dolcini 
12610316c7e6SFrancesco Dolcini 	return phy_set_bits(phydev, 0x1e, BIT(9));
12620316c7e6SFrancesco Dolcini }
12630316c7e6SFrancesco Dolcini 
1264bff5b4b3SYuiko Oshino static int ksz9131_config_init(struct phy_device *phydev)
1265bff5b4b3SYuiko Oshino {
1266ce4f8afdSColin Ian King 	struct device_node *of_node;
1267bff5b4b3SYuiko Oshino 	char *clk_skews[2] = {"rxc-skew-psec", "txc-skew-psec"};
1268bff5b4b3SYuiko Oshino 	char *rx_data_skews[4] = {
1269bff5b4b3SYuiko Oshino 		"rxd0-skew-psec", "rxd1-skew-psec",
1270bff5b4b3SYuiko Oshino 		"rxd2-skew-psec", "rxd3-skew-psec"
1271bff5b4b3SYuiko Oshino 	};
1272bff5b4b3SYuiko Oshino 	char *tx_data_skews[4] = {
1273bff5b4b3SYuiko Oshino 		"txd0-skew-psec", "txd1-skew-psec",
1274bff5b4b3SYuiko Oshino 		"txd2-skew-psec", "txd3-skew-psec"
1275bff5b4b3SYuiko Oshino 	};
1276bff5b4b3SYuiko Oshino 	char *control_skews[2] = {"txen-skew-psec", "rxdv-skew-psec"};
1277bff5b4b3SYuiko Oshino 	const struct device *dev_walker;
1278bff5b4b3SYuiko Oshino 	int ret;
1279bff5b4b3SYuiko Oshino 
1280bff5b4b3SYuiko Oshino 	dev_walker = &phydev->mdio.dev;
1281bff5b4b3SYuiko Oshino 	do {
1282bff5b4b3SYuiko Oshino 		of_node = dev_walker->of_node;
1283bff5b4b3SYuiko Oshino 		dev_walker = dev_walker->parent;
1284bff5b4b3SYuiko Oshino 	} while (!of_node && dev_walker);
1285bff5b4b3SYuiko Oshino 
1286bff5b4b3SYuiko Oshino 	if (!of_node)
1287bff5b4b3SYuiko Oshino 		return 0;
1288bff5b4b3SYuiko Oshino 
1289bd734a74SPhilippe Schenker 	if (phy_interface_is_rgmii(phydev)) {
1290bd734a74SPhilippe Schenker 		ret = ksz9131_config_rgmii_delay(phydev);
1291bd734a74SPhilippe Schenker 		if (ret < 0)
1292bd734a74SPhilippe Schenker 			return ret;
1293bd734a74SPhilippe Schenker 	}
1294bd734a74SPhilippe Schenker 
1295bff5b4b3SYuiko Oshino 	ret = ksz9131_of_load_skew_values(phydev, of_node,
1296bff5b4b3SYuiko Oshino 					  MII_KSZ9031RN_CLK_PAD_SKEW, 5,
1297bff5b4b3SYuiko Oshino 					  clk_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_CONTROL_PAD_SKEW, 4,
1303bff5b4b3SYuiko Oshino 					  control_skews, 2);
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_RX_DATA_PAD_SKEW, 4,
1309bff5b4b3SYuiko Oshino 					  rx_data_skews, 4);
1310bff5b4b3SYuiko Oshino 	if (ret < 0)
1311bff5b4b3SYuiko Oshino 		return ret;
1312bff5b4b3SYuiko Oshino 
1313bff5b4b3SYuiko Oshino 	ret = ksz9131_of_load_skew_values(phydev, of_node,
1314bff5b4b3SYuiko Oshino 					  MII_KSZ9031RN_TX_DATA_PAD_SKEW, 4,
1315bff5b4b3SYuiko Oshino 					  tx_data_skews, 4);
1316bff5b4b3SYuiko Oshino 	if (ret < 0)
1317bff5b4b3SYuiko Oshino 		return ret;
1318bff5b4b3SYuiko Oshino 
13190316c7e6SFrancesco Dolcini 	ret = ksz9131_led_errata(phydev);
13200316c7e6SFrancesco Dolcini 	if (ret < 0)
13210316c7e6SFrancesco Dolcini 		return ret;
13220316c7e6SFrancesco Dolcini 
1323bff5b4b3SYuiko Oshino 	return 0;
1324bff5b4b3SYuiko Oshino }
1325bff5b4b3SYuiko Oshino 
1326b64e6a87SRaju Lakkaraju #define MII_KSZ9131_AUTO_MDIX		0x1C
1327b64e6a87SRaju Lakkaraju #define MII_KSZ9131_AUTO_MDI_SET	BIT(7)
1328b64e6a87SRaju Lakkaraju #define MII_KSZ9131_AUTO_MDIX_SWAP_OFF	BIT(6)
1329b64e6a87SRaju Lakkaraju 
1330b64e6a87SRaju Lakkaraju static int ksz9131_mdix_update(struct phy_device *phydev)
1331b64e6a87SRaju Lakkaraju {
1332b64e6a87SRaju Lakkaraju 	int ret;
1333b64e6a87SRaju Lakkaraju 
1334b64e6a87SRaju Lakkaraju 	ret = phy_read(phydev, MII_KSZ9131_AUTO_MDIX);
1335b64e6a87SRaju Lakkaraju 	if (ret < 0)
1336b64e6a87SRaju Lakkaraju 		return ret;
1337b64e6a87SRaju Lakkaraju 
1338b64e6a87SRaju Lakkaraju 	if (ret & MII_KSZ9131_AUTO_MDIX_SWAP_OFF) {
1339b64e6a87SRaju Lakkaraju 		if (ret & MII_KSZ9131_AUTO_MDI_SET)
1340b64e6a87SRaju Lakkaraju 			phydev->mdix_ctrl = ETH_TP_MDI;
1341b64e6a87SRaju Lakkaraju 		else
1342b64e6a87SRaju Lakkaraju 			phydev->mdix_ctrl = ETH_TP_MDI_X;
1343b64e6a87SRaju Lakkaraju 	} else {
1344b64e6a87SRaju Lakkaraju 		phydev->mdix_ctrl = ETH_TP_MDI_AUTO;
1345b64e6a87SRaju Lakkaraju 	}
1346b64e6a87SRaju Lakkaraju 
1347b64e6a87SRaju Lakkaraju 	if (ret & MII_KSZ9131_AUTO_MDI_SET)
1348b64e6a87SRaju Lakkaraju 		phydev->mdix = ETH_TP_MDI;
1349b64e6a87SRaju Lakkaraju 	else
1350b64e6a87SRaju Lakkaraju 		phydev->mdix = ETH_TP_MDI_X;
1351b64e6a87SRaju Lakkaraju 
1352b64e6a87SRaju Lakkaraju 	return 0;
1353b64e6a87SRaju Lakkaraju }
1354b64e6a87SRaju Lakkaraju 
1355b64e6a87SRaju Lakkaraju static int ksz9131_config_mdix(struct phy_device *phydev, u8 ctrl)
1356b64e6a87SRaju Lakkaraju {
1357b64e6a87SRaju Lakkaraju 	u16 val;
1358b64e6a87SRaju Lakkaraju 
1359b64e6a87SRaju Lakkaraju 	switch (ctrl) {
1360b64e6a87SRaju Lakkaraju 	case ETH_TP_MDI:
1361b64e6a87SRaju Lakkaraju 		val = MII_KSZ9131_AUTO_MDIX_SWAP_OFF |
1362b64e6a87SRaju Lakkaraju 		      MII_KSZ9131_AUTO_MDI_SET;
1363b64e6a87SRaju Lakkaraju 		break;
1364b64e6a87SRaju Lakkaraju 	case ETH_TP_MDI_X:
1365b64e6a87SRaju Lakkaraju 		val = MII_KSZ9131_AUTO_MDIX_SWAP_OFF;
1366b64e6a87SRaju Lakkaraju 		break;
1367b64e6a87SRaju Lakkaraju 	case ETH_TP_MDI_AUTO:
1368b64e6a87SRaju Lakkaraju 		val = 0;
1369b64e6a87SRaju Lakkaraju 		break;
1370b64e6a87SRaju Lakkaraju 	default:
1371b64e6a87SRaju Lakkaraju 		return 0;
1372b64e6a87SRaju Lakkaraju 	}
1373b64e6a87SRaju Lakkaraju 
1374b64e6a87SRaju Lakkaraju 	return phy_modify(phydev, MII_KSZ9131_AUTO_MDIX,
1375b64e6a87SRaju Lakkaraju 			  MII_KSZ9131_AUTO_MDIX_SWAP_OFF |
1376b64e6a87SRaju Lakkaraju 			  MII_KSZ9131_AUTO_MDI_SET, val);
1377b64e6a87SRaju Lakkaraju }
1378b64e6a87SRaju Lakkaraju 
1379b64e6a87SRaju Lakkaraju static int ksz9131_read_status(struct phy_device *phydev)
1380b64e6a87SRaju Lakkaraju {
1381b64e6a87SRaju Lakkaraju 	int ret;
1382b64e6a87SRaju Lakkaraju 
1383b64e6a87SRaju Lakkaraju 	ret = ksz9131_mdix_update(phydev);
1384b64e6a87SRaju Lakkaraju 	if (ret < 0)
1385b64e6a87SRaju Lakkaraju 		return ret;
1386b64e6a87SRaju Lakkaraju 
1387b64e6a87SRaju Lakkaraju 	return genphy_read_status(phydev);
1388b64e6a87SRaju Lakkaraju }
1389b64e6a87SRaju Lakkaraju 
1390b64e6a87SRaju Lakkaraju static int ksz9131_config_aneg(struct phy_device *phydev)
1391b64e6a87SRaju Lakkaraju {
1392b64e6a87SRaju Lakkaraju 	int ret;
1393b64e6a87SRaju Lakkaraju 
1394b64e6a87SRaju Lakkaraju 	ret = ksz9131_config_mdix(phydev, phydev->mdix_ctrl);
1395b64e6a87SRaju Lakkaraju 	if (ret)
1396b64e6a87SRaju Lakkaraju 		return ret;
1397b64e6a87SRaju Lakkaraju 
1398b64e6a87SRaju Lakkaraju 	return genphy_config_aneg(phydev);
1399b64e6a87SRaju Lakkaraju }
1400b64e6a87SRaju Lakkaraju 
140148fb1994SOleksij Rempel static int ksz9477_get_features(struct phy_device *phydev)
140248fb1994SOleksij Rempel {
140348fb1994SOleksij Rempel 	int ret;
140448fb1994SOleksij Rempel 
140548fb1994SOleksij Rempel 	ret = genphy_read_abilities(phydev);
140648fb1994SOleksij Rempel 	if (ret)
140748fb1994SOleksij Rempel 		return ret;
140848fb1994SOleksij Rempel 
140948fb1994SOleksij Rempel 	/* The "EEE control and capability 1" (Register 3.20) seems to be
141048fb1994SOleksij Rempel 	 * influenced by the "EEE advertisement 1" (Register 7.60). Changes
141148fb1994SOleksij Rempel 	 * on the 7.60 will affect 3.20. So, we need to construct our own list
141248fb1994SOleksij Rempel 	 * of caps.
141348fb1994SOleksij Rempel 	 * KSZ8563R should have 100BaseTX/Full only.
141448fb1994SOleksij Rempel 	 */
141548fb1994SOleksij Rempel 	linkmode_and(phydev->supported_eee, phydev->supported,
141648fb1994SOleksij Rempel 		     PHY_EEE_CAP1_FEATURES);
141748fb1994SOleksij Rempel 
141848fb1994SOleksij Rempel 	return 0;
141948fb1994SOleksij Rempel }
142048fb1994SOleksij Rempel 
142193272e07SJean-Christophe PLAGNIOL-VILLARD #define KSZ8873MLL_GLOBAL_CONTROL_4	0x06
142200aee095SJohan Hovold #define KSZ8873MLL_GLOBAL_CONTROL_4_DUPLEX	BIT(6)
142300aee095SJohan Hovold #define KSZ8873MLL_GLOBAL_CONTROL_4_SPEED	BIT(4)
142432d73b14SJingoo Han static int ksz8873mll_read_status(struct phy_device *phydev)
142593272e07SJean-Christophe PLAGNIOL-VILLARD {
142693272e07SJean-Christophe PLAGNIOL-VILLARD 	int regval;
142793272e07SJean-Christophe PLAGNIOL-VILLARD 
142893272e07SJean-Christophe PLAGNIOL-VILLARD 	/* dummy read */
142993272e07SJean-Christophe PLAGNIOL-VILLARD 	regval = phy_read(phydev, KSZ8873MLL_GLOBAL_CONTROL_4);
143093272e07SJean-Christophe PLAGNIOL-VILLARD 
143193272e07SJean-Christophe PLAGNIOL-VILLARD 	regval = phy_read(phydev, KSZ8873MLL_GLOBAL_CONTROL_4);
143293272e07SJean-Christophe PLAGNIOL-VILLARD 
143393272e07SJean-Christophe PLAGNIOL-VILLARD 	if (regval & KSZ8873MLL_GLOBAL_CONTROL_4_DUPLEX)
143493272e07SJean-Christophe PLAGNIOL-VILLARD 		phydev->duplex = DUPLEX_HALF;
143593272e07SJean-Christophe PLAGNIOL-VILLARD 	else
143693272e07SJean-Christophe PLAGNIOL-VILLARD 		phydev->duplex = DUPLEX_FULL;
143793272e07SJean-Christophe PLAGNIOL-VILLARD 
143893272e07SJean-Christophe PLAGNIOL-VILLARD 	if (regval & KSZ8873MLL_GLOBAL_CONTROL_4_SPEED)
143993272e07SJean-Christophe PLAGNIOL-VILLARD 		phydev->speed = SPEED_10;
144093272e07SJean-Christophe PLAGNIOL-VILLARD 	else
144193272e07SJean-Christophe PLAGNIOL-VILLARD 		phydev->speed = SPEED_100;
144293272e07SJean-Christophe PLAGNIOL-VILLARD 
144393272e07SJean-Christophe PLAGNIOL-VILLARD 	phydev->link = 1;
144493272e07SJean-Christophe PLAGNIOL-VILLARD 	phydev->pause = phydev->asym_pause = 0;
144593272e07SJean-Christophe PLAGNIOL-VILLARD 
144693272e07SJean-Christophe PLAGNIOL-VILLARD 	return 0;
144793272e07SJean-Christophe PLAGNIOL-VILLARD }
144893272e07SJean-Christophe PLAGNIOL-VILLARD 
14493aed3e2aSAntoine Tenart static int ksz9031_get_features(struct phy_device *phydev)
14503aed3e2aSAntoine Tenart {
14513aed3e2aSAntoine Tenart 	int ret;
14523aed3e2aSAntoine Tenart 
14533aed3e2aSAntoine Tenart 	ret = genphy_read_abilities(phydev);
14543aed3e2aSAntoine Tenart 	if (ret < 0)
14553aed3e2aSAntoine Tenart 		return ret;
14563aed3e2aSAntoine Tenart 
14573aed3e2aSAntoine Tenart 	/* Silicon Errata Sheet (DS80000691D or DS80000692D):
14583aed3e2aSAntoine Tenart 	 * Whenever the device's Asymmetric Pause capability is set to 1,
14593aed3e2aSAntoine Tenart 	 * link-up may fail after a link-up to link-down transition.
14603aed3e2aSAntoine Tenart 	 *
1461407d8098SHans Andersson 	 * The Errata Sheet is for ksz9031, but ksz9021 has the same issue
1462407d8098SHans Andersson 	 *
14633aed3e2aSAntoine Tenart 	 * Workaround:
14643aed3e2aSAntoine Tenart 	 * Do not enable the Asymmetric Pause capability bit.
14653aed3e2aSAntoine Tenart 	 */
14663aed3e2aSAntoine Tenart 	linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported);
14673aed3e2aSAntoine Tenart 
14683aed3e2aSAntoine Tenart 	/* We force setting the Pause capability as the core will force the
14693aed3e2aSAntoine Tenart 	 * Asymmetric Pause capability to 1 otherwise.
14703aed3e2aSAntoine Tenart 	 */
14713aed3e2aSAntoine Tenart 	linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported);
14723aed3e2aSAntoine Tenart 
14733aed3e2aSAntoine Tenart 	return 0;
14743aed3e2aSAntoine Tenart }
14753aed3e2aSAntoine Tenart 
1476d2fd719bSNathan Sullivan static int ksz9031_read_status(struct phy_device *phydev)
1477d2fd719bSNathan Sullivan {
1478d2fd719bSNathan Sullivan 	int err;
1479d2fd719bSNathan Sullivan 	int regval;
1480d2fd719bSNathan Sullivan 
1481d2fd719bSNathan Sullivan 	err = genphy_read_status(phydev);
1482d2fd719bSNathan Sullivan 	if (err)
1483d2fd719bSNathan Sullivan 		return err;
1484d2fd719bSNathan Sullivan 
1485d2fd719bSNathan Sullivan 	/* Make sure the PHY is not broken. Read idle error count,
1486d2fd719bSNathan Sullivan 	 * and reset the PHY if it is maxed out.
1487d2fd719bSNathan Sullivan 	 */
1488d2fd719bSNathan Sullivan 	regval = phy_read(phydev, MII_STAT1000);
1489d2fd719bSNathan Sullivan 	if ((regval & 0xFF) == 0xFF) {
1490d2fd719bSNathan Sullivan 		phy_init_hw(phydev);
1491d2fd719bSNathan Sullivan 		phydev->link = 0;
1492b866203dSZach Brown 		if (phydev->drv->config_intr && phy_interrupt_is_valid(phydev))
1493b866203dSZach Brown 			phydev->drv->config_intr(phydev);
1494c1a8d0a3SGrygorii Strashko 		return genphy_config_aneg(phydev);
1495d2fd719bSNathan Sullivan 	}
1496d2fd719bSNathan Sullivan 
1497d2fd719bSNathan Sullivan 	return 0;
1498d2fd719bSNathan Sullivan }
1499d2fd719bSNathan Sullivan 
150058389c00SMarek Vasut static int ksz9x31_cable_test_start(struct phy_device *phydev)
150158389c00SMarek Vasut {
150258389c00SMarek Vasut 	struct kszphy_priv *priv = phydev->priv;
150358389c00SMarek Vasut 	int ret;
150458389c00SMarek Vasut 
150558389c00SMarek Vasut 	/* KSZ9131RNX, DS00002841B-page 38, 4.14 LinkMD (R) Cable Diagnostic
150658389c00SMarek Vasut 	 * Prior to running the cable diagnostics, Auto-negotiation should
150758389c00SMarek Vasut 	 * be disabled, full duplex set and the link speed set to 1000Mbps
150858389c00SMarek Vasut 	 * via the Basic Control Register.
150958389c00SMarek Vasut 	 */
151058389c00SMarek Vasut 	ret = phy_modify(phydev, MII_BMCR,
151158389c00SMarek Vasut 			 BMCR_SPEED1000 | BMCR_FULLDPLX |
151258389c00SMarek Vasut 			 BMCR_ANENABLE | BMCR_SPEED100,
151358389c00SMarek Vasut 			 BMCR_SPEED1000 | BMCR_FULLDPLX);
151458389c00SMarek Vasut 	if (ret)
151558389c00SMarek Vasut 		return ret;
151658389c00SMarek Vasut 
151758389c00SMarek Vasut 	/* KSZ9131RNX, DS00002841B-page 38, 4.14 LinkMD (R) Cable Diagnostic
151858389c00SMarek Vasut 	 * The Master-Slave configuration should be set to Slave by writing
151958389c00SMarek Vasut 	 * a value of 0x1000 to the Auto-Negotiation Master Slave Control
152058389c00SMarek Vasut 	 * Register.
152158389c00SMarek Vasut 	 */
152258389c00SMarek Vasut 	ret = phy_read(phydev, MII_CTRL1000);
152358389c00SMarek Vasut 	if (ret < 0)
152458389c00SMarek Vasut 		return ret;
152558389c00SMarek Vasut 
152658389c00SMarek Vasut 	/* Cache these bits, they need to be restored once LinkMD finishes. */
152758389c00SMarek Vasut 	priv->vct_ctrl1000 = ret & (CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER);
152858389c00SMarek Vasut 	ret &= ~(CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER);
152958389c00SMarek Vasut 	ret |= CTL1000_ENABLE_MASTER;
153058389c00SMarek Vasut 
153158389c00SMarek Vasut 	return phy_write(phydev, MII_CTRL1000, ret);
153258389c00SMarek Vasut }
153358389c00SMarek Vasut 
153458389c00SMarek Vasut static int ksz9x31_cable_test_result_trans(u16 status)
153558389c00SMarek Vasut {
153658389c00SMarek Vasut 	switch (FIELD_GET(KSZ9x31_LMD_VCT_ST_MASK, status)) {
153758389c00SMarek Vasut 	case KSZ9x31_LMD_VCT_ST_NORMAL:
153858389c00SMarek Vasut 		return ETHTOOL_A_CABLE_RESULT_CODE_OK;
153958389c00SMarek Vasut 	case KSZ9x31_LMD_VCT_ST_OPEN:
154058389c00SMarek Vasut 		return ETHTOOL_A_CABLE_RESULT_CODE_OPEN;
154158389c00SMarek Vasut 	case KSZ9x31_LMD_VCT_ST_SHORT:
154258389c00SMarek Vasut 		return ETHTOOL_A_CABLE_RESULT_CODE_SAME_SHORT;
154358389c00SMarek Vasut 	case KSZ9x31_LMD_VCT_ST_FAIL:
154458389c00SMarek Vasut 		fallthrough;
154558389c00SMarek Vasut 	default:
154658389c00SMarek Vasut 		return ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC;
154758389c00SMarek Vasut 	}
154858389c00SMarek Vasut }
154958389c00SMarek Vasut 
155058389c00SMarek Vasut static bool ksz9x31_cable_test_failed(u16 status)
155158389c00SMarek Vasut {
155258389c00SMarek Vasut 	int stat = FIELD_GET(KSZ9x31_LMD_VCT_ST_MASK, status);
155358389c00SMarek Vasut 
155458389c00SMarek Vasut 	return stat == KSZ9x31_LMD_VCT_ST_FAIL;
155558389c00SMarek Vasut }
155658389c00SMarek Vasut 
155758389c00SMarek Vasut static bool ksz9x31_cable_test_fault_length_valid(u16 status)
155858389c00SMarek Vasut {
155958389c00SMarek Vasut 	switch (FIELD_GET(KSZ9x31_LMD_VCT_ST_MASK, status)) {
156058389c00SMarek Vasut 	case KSZ9x31_LMD_VCT_ST_OPEN:
156158389c00SMarek Vasut 		fallthrough;
156258389c00SMarek Vasut 	case KSZ9x31_LMD_VCT_ST_SHORT:
156358389c00SMarek Vasut 		return true;
156458389c00SMarek Vasut 	}
156558389c00SMarek Vasut 	return false;
156658389c00SMarek Vasut }
156758389c00SMarek Vasut 
156858389c00SMarek Vasut static int ksz9x31_cable_test_fault_length(struct phy_device *phydev, u16 stat)
156958389c00SMarek Vasut {
157058389c00SMarek Vasut 	int dt = FIELD_GET(KSZ9x31_LMD_VCT_DATA_MASK, stat);
157158389c00SMarek Vasut 
157258389c00SMarek Vasut 	/* KSZ9131RNX, DS00002841B-page 38, 4.14 LinkMD (R) Cable Diagnostic
157358389c00SMarek Vasut 	 *
157458389c00SMarek Vasut 	 * distance to fault = (VCT_DATA - 22) * 4 / cable propagation velocity
157558389c00SMarek Vasut 	 */
15764b159f50SRussell King 	if (phydev_id_compare(phydev, PHY_ID_KSZ9131))
157758389c00SMarek Vasut 		dt = clamp(dt - 22, 0, 255);
157858389c00SMarek Vasut 
157958389c00SMarek Vasut 	return (dt * 400) / 10;
158058389c00SMarek Vasut }
158158389c00SMarek Vasut 
158258389c00SMarek Vasut static int ksz9x31_cable_test_wait_for_completion(struct phy_device *phydev)
158358389c00SMarek Vasut {
158458389c00SMarek Vasut 	int val, ret;
158558389c00SMarek Vasut 
158658389c00SMarek Vasut 	ret = phy_read_poll_timeout(phydev, KSZ9x31_LMD, val,
158758389c00SMarek Vasut 				    !(val & KSZ9x31_LMD_VCT_EN),
158858389c00SMarek Vasut 				    30000, 100000, true);
158958389c00SMarek Vasut 
159058389c00SMarek Vasut 	return ret < 0 ? ret : 0;
159158389c00SMarek Vasut }
159258389c00SMarek Vasut 
159358389c00SMarek Vasut static int ksz9x31_cable_test_get_pair(int pair)
159458389c00SMarek Vasut {
159558389c00SMarek Vasut 	static const int ethtool_pair[] = {
159658389c00SMarek Vasut 		ETHTOOL_A_CABLE_PAIR_A,
159758389c00SMarek Vasut 		ETHTOOL_A_CABLE_PAIR_B,
159858389c00SMarek Vasut 		ETHTOOL_A_CABLE_PAIR_C,
159958389c00SMarek Vasut 		ETHTOOL_A_CABLE_PAIR_D,
160058389c00SMarek Vasut 	};
160158389c00SMarek Vasut 
160258389c00SMarek Vasut 	return ethtool_pair[pair];
160358389c00SMarek Vasut }
160458389c00SMarek Vasut 
160558389c00SMarek Vasut static int ksz9x31_cable_test_one_pair(struct phy_device *phydev, int pair)
160658389c00SMarek Vasut {
160758389c00SMarek Vasut 	int ret, val;
160858389c00SMarek Vasut 
160958389c00SMarek Vasut 	/* KSZ9131RNX, DS00002841B-page 38, 4.14 LinkMD (R) Cable Diagnostic
161058389c00SMarek Vasut 	 * To test each individual cable pair, set the cable pair in the Cable
161158389c00SMarek Vasut 	 * Diagnostics Test Pair (VCT_PAIR[1:0]) field of the LinkMD Cable
161258389c00SMarek Vasut 	 * Diagnostic Register, along with setting the Cable Diagnostics Test
161358389c00SMarek Vasut 	 * Enable (VCT_EN) bit. The Cable Diagnostics Test Enable (VCT_EN) bit
161458389c00SMarek Vasut 	 * will self clear when the test is concluded.
161558389c00SMarek Vasut 	 */
161658389c00SMarek Vasut 	ret = phy_write(phydev, KSZ9x31_LMD,
161758389c00SMarek Vasut 			KSZ9x31_LMD_VCT_EN | KSZ9x31_LMD_VCT_PAIR(pair));
161858389c00SMarek Vasut 	if (ret)
161958389c00SMarek Vasut 		return ret;
162058389c00SMarek Vasut 
162158389c00SMarek Vasut 	ret = ksz9x31_cable_test_wait_for_completion(phydev);
162258389c00SMarek Vasut 	if (ret)
162358389c00SMarek Vasut 		return ret;
162458389c00SMarek Vasut 
162558389c00SMarek Vasut 	val = phy_read(phydev, KSZ9x31_LMD);
162658389c00SMarek Vasut 	if (val < 0)
162758389c00SMarek Vasut 		return val;
162858389c00SMarek Vasut 
162958389c00SMarek Vasut 	if (ksz9x31_cable_test_failed(val))
163058389c00SMarek Vasut 		return -EAGAIN;
163158389c00SMarek Vasut 
163258389c00SMarek Vasut 	ret = ethnl_cable_test_result(phydev,
163358389c00SMarek Vasut 				      ksz9x31_cable_test_get_pair(pair),
163458389c00SMarek Vasut 				      ksz9x31_cable_test_result_trans(val));
163558389c00SMarek Vasut 	if (ret)
163658389c00SMarek Vasut 		return ret;
163758389c00SMarek Vasut 
163858389c00SMarek Vasut 	if (!ksz9x31_cable_test_fault_length_valid(val))
163958389c00SMarek Vasut 		return 0;
164058389c00SMarek Vasut 
164158389c00SMarek Vasut 	return ethnl_cable_test_fault_length(phydev,
164258389c00SMarek Vasut 					     ksz9x31_cable_test_get_pair(pair),
164358389c00SMarek Vasut 					     ksz9x31_cable_test_fault_length(phydev, val));
164458389c00SMarek Vasut }
164558389c00SMarek Vasut 
164658389c00SMarek Vasut static int ksz9x31_cable_test_get_status(struct phy_device *phydev,
164758389c00SMarek Vasut 					 bool *finished)
164858389c00SMarek Vasut {
164958389c00SMarek Vasut 	struct kszphy_priv *priv = phydev->priv;
165058389c00SMarek Vasut 	unsigned long pair_mask = 0xf;
165158389c00SMarek Vasut 	int retries = 20;
165258389c00SMarek Vasut 	int pair, ret, rv;
165358389c00SMarek Vasut 
165458389c00SMarek Vasut 	*finished = false;
165558389c00SMarek Vasut 
165658389c00SMarek Vasut 	/* Try harder if link partner is active */
165758389c00SMarek Vasut 	while (pair_mask && retries--) {
165858389c00SMarek Vasut 		for_each_set_bit(pair, &pair_mask, 4) {
165958389c00SMarek Vasut 			ret = ksz9x31_cable_test_one_pair(phydev, pair);
166058389c00SMarek Vasut 			if (ret == -EAGAIN)
166158389c00SMarek Vasut 				continue;
166258389c00SMarek Vasut 			if (ret < 0)
166358389c00SMarek Vasut 				return ret;
166458389c00SMarek Vasut 			clear_bit(pair, &pair_mask);
166558389c00SMarek Vasut 		}
166658389c00SMarek Vasut 		/* If link partner is in autonegotiation mode it will send 2ms
166758389c00SMarek Vasut 		 * of FLPs with at least 6ms of silence.
166858389c00SMarek Vasut 		 * Add 2ms sleep to have better chances to hit this silence.
166958389c00SMarek Vasut 		 */
167058389c00SMarek Vasut 		if (pair_mask)
167158389c00SMarek Vasut 			usleep_range(2000, 3000);
167258389c00SMarek Vasut 	}
167358389c00SMarek Vasut 
167458389c00SMarek Vasut 	/* Report remaining unfinished pair result as unknown. */
167558389c00SMarek Vasut 	for_each_set_bit(pair, &pair_mask, 4) {
167658389c00SMarek Vasut 		ret = ethnl_cable_test_result(phydev,
167758389c00SMarek Vasut 					      ksz9x31_cable_test_get_pair(pair),
167858389c00SMarek Vasut 					      ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC);
167958389c00SMarek Vasut 	}
168058389c00SMarek Vasut 
168158389c00SMarek Vasut 	*finished = true;
168258389c00SMarek Vasut 
168358389c00SMarek Vasut 	/* Restore cached bits from before LinkMD got started. */
168458389c00SMarek Vasut 	rv = phy_modify(phydev, MII_CTRL1000,
168558389c00SMarek Vasut 			CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER,
168658389c00SMarek Vasut 			priv->vct_ctrl1000);
168758389c00SMarek Vasut 	if (rv)
168858389c00SMarek Vasut 		return rv;
168958389c00SMarek Vasut 
169058389c00SMarek Vasut 	return ret;
169158389c00SMarek Vasut }
169258389c00SMarek Vasut 
169393272e07SJean-Christophe PLAGNIOL-VILLARD static int ksz8873mll_config_aneg(struct phy_device *phydev)
169493272e07SJean-Christophe PLAGNIOL-VILLARD {
169593272e07SJean-Christophe PLAGNIOL-VILLARD 	return 0;
169693272e07SJean-Christophe PLAGNIOL-VILLARD }
169793272e07SJean-Christophe PLAGNIOL-VILLARD 
169852939393SOleksij Rempel static int ksz886x_config_mdix(struct phy_device *phydev, u8 ctrl)
169952939393SOleksij Rempel {
170052939393SOleksij Rempel 	u16 val;
170152939393SOleksij Rempel 
170252939393SOleksij Rempel 	switch (ctrl) {
170352939393SOleksij Rempel 	case ETH_TP_MDI:
170452939393SOleksij Rempel 		val = KSZ886X_BMCR_DISABLE_AUTO_MDIX;
170552939393SOleksij Rempel 		break;
170652939393SOleksij Rempel 	case ETH_TP_MDI_X:
170752939393SOleksij Rempel 		/* Note: The naming of the bit KSZ886X_BMCR_FORCE_MDI is bit
170852939393SOleksij Rempel 		 * counter intuitive, the "-X" in "1 = Force MDI" in the data
170952939393SOleksij Rempel 		 * sheet seems to be missing:
171052939393SOleksij Rempel 		 * 1 = Force MDI (sic!) (transmit on RX+/RX- pins)
171152939393SOleksij Rempel 		 * 0 = Normal operation (transmit on TX+/TX- pins)
171252939393SOleksij Rempel 		 */
171352939393SOleksij Rempel 		val = KSZ886X_BMCR_DISABLE_AUTO_MDIX | KSZ886X_BMCR_FORCE_MDI;
171452939393SOleksij Rempel 		break;
171552939393SOleksij Rempel 	case ETH_TP_MDI_AUTO:
171652939393SOleksij Rempel 		val = 0;
171752939393SOleksij Rempel 		break;
171852939393SOleksij Rempel 	default:
171952939393SOleksij Rempel 		return 0;
172052939393SOleksij Rempel 	}
172152939393SOleksij Rempel 
172252939393SOleksij Rempel 	return phy_modify(phydev, MII_BMCR,
172352939393SOleksij Rempel 			  KSZ886X_BMCR_HP_MDIX | KSZ886X_BMCR_FORCE_MDI |
172452939393SOleksij Rempel 			  KSZ886X_BMCR_DISABLE_AUTO_MDIX,
172552939393SOleksij Rempel 			  KSZ886X_BMCR_HP_MDIX | val);
172652939393SOleksij Rempel }
172752939393SOleksij Rempel 
172852939393SOleksij Rempel static int ksz886x_config_aneg(struct phy_device *phydev)
172952939393SOleksij Rempel {
173052939393SOleksij Rempel 	int ret;
173152939393SOleksij Rempel 
173252939393SOleksij Rempel 	ret = genphy_config_aneg(phydev);
173352939393SOleksij Rempel 	if (ret)
173452939393SOleksij Rempel 		return ret;
173552939393SOleksij Rempel 
173652939393SOleksij Rempel 	/* The MDI-X configuration is automatically changed by the PHY after
173752939393SOleksij Rempel 	 * switching from autoneg off to on. So, take MDI-X configuration under
173852939393SOleksij Rempel 	 * own control and set it after autoneg configuration was done.
173952939393SOleksij Rempel 	 */
174052939393SOleksij Rempel 	return ksz886x_config_mdix(phydev, phydev->mdix_ctrl);
174152939393SOleksij Rempel }
174252939393SOleksij Rempel 
174352939393SOleksij Rempel static int ksz886x_mdix_update(struct phy_device *phydev)
174452939393SOleksij Rempel {
174552939393SOleksij Rempel 	int ret;
174652939393SOleksij Rempel 
174752939393SOleksij Rempel 	ret = phy_read(phydev, MII_BMCR);
174852939393SOleksij Rempel 	if (ret < 0)
174952939393SOleksij Rempel 		return ret;
175052939393SOleksij Rempel 
175152939393SOleksij Rempel 	if (ret & KSZ886X_BMCR_DISABLE_AUTO_MDIX) {
175252939393SOleksij Rempel 		if (ret & KSZ886X_BMCR_FORCE_MDI)
175352939393SOleksij Rempel 			phydev->mdix_ctrl = ETH_TP_MDI_X;
175452939393SOleksij Rempel 		else
175552939393SOleksij Rempel 			phydev->mdix_ctrl = ETH_TP_MDI;
175652939393SOleksij Rempel 	} else {
175752939393SOleksij Rempel 		phydev->mdix_ctrl = ETH_TP_MDI_AUTO;
175852939393SOleksij Rempel 	}
175952939393SOleksij Rempel 
176052939393SOleksij Rempel 	ret = phy_read(phydev, MII_KSZPHY_CTRL);
176152939393SOleksij Rempel 	if (ret < 0)
176252939393SOleksij Rempel 		return ret;
176352939393SOleksij Rempel 
176452939393SOleksij Rempel 	/* Same reverse logic as KSZ886X_BMCR_FORCE_MDI */
176552939393SOleksij Rempel 	if (ret & KSZ886X_CTRL_MDIX_STAT)
176652939393SOleksij Rempel 		phydev->mdix = ETH_TP_MDI_X;
176752939393SOleksij Rempel 	else
176852939393SOleksij Rempel 		phydev->mdix = ETH_TP_MDI;
176952939393SOleksij Rempel 
177052939393SOleksij Rempel 	return 0;
177152939393SOleksij Rempel }
177252939393SOleksij Rempel 
177352939393SOleksij Rempel static int ksz886x_read_status(struct phy_device *phydev)
177452939393SOleksij Rempel {
177552939393SOleksij Rempel 	int ret;
177652939393SOleksij Rempel 
177752939393SOleksij Rempel 	ret = ksz886x_mdix_update(phydev);
177852939393SOleksij Rempel 	if (ret < 0)
177952939393SOleksij Rempel 		return ret;
178052939393SOleksij Rempel 
178152939393SOleksij Rempel 	return genphy_read_status(phydev);
178252939393SOleksij Rempel }
178352939393SOleksij Rempel 
178426dd2974SRobert Hancock struct ksz9477_errata_write {
178526dd2974SRobert Hancock 	u8 dev_addr;
178626dd2974SRobert Hancock 	u8 reg_addr;
178726dd2974SRobert Hancock 	u16 val;
178826dd2974SRobert Hancock };
178926dd2974SRobert Hancock 
179026dd2974SRobert Hancock static const struct ksz9477_errata_write ksz9477_errata_writes[] = {
179126dd2974SRobert Hancock 	 /* Register settings are needed to improve PHY receive performance */
179226dd2974SRobert Hancock 	{0x01, 0x6f, 0xdd0b},
179326dd2974SRobert Hancock 	{0x01, 0x8f, 0x6032},
179426dd2974SRobert Hancock 	{0x01, 0x9d, 0x248c},
179526dd2974SRobert Hancock 	{0x01, 0x75, 0x0060},
179626dd2974SRobert Hancock 	{0x01, 0xd3, 0x7777},
179726dd2974SRobert Hancock 	{0x1c, 0x06, 0x3008},
179826dd2974SRobert Hancock 	{0x1c, 0x08, 0x2000},
179926dd2974SRobert Hancock 
180026dd2974SRobert Hancock 	/* Transmit waveform amplitude can be improved (1000BASE-T, 100BASE-TX, 10BASE-Te) */
180126dd2974SRobert Hancock 	{0x1c, 0x04, 0x00d0},
180226dd2974SRobert Hancock 
180326dd2974SRobert Hancock 	/* Energy Efficient Ethernet (EEE) feature select must be manually disabled */
180426dd2974SRobert Hancock 	{0x07, 0x3c, 0x0000},
180526dd2974SRobert Hancock 
180626dd2974SRobert Hancock 	/* Register settings are required to meet data sheet supply current specifications */
180726dd2974SRobert Hancock 	{0x1c, 0x13, 0x6eff},
180826dd2974SRobert Hancock 	{0x1c, 0x14, 0xe6ff},
180926dd2974SRobert Hancock 	{0x1c, 0x15, 0x6eff},
181026dd2974SRobert Hancock 	{0x1c, 0x16, 0xe6ff},
181126dd2974SRobert Hancock 	{0x1c, 0x17, 0x00ff},
181226dd2974SRobert Hancock 	{0x1c, 0x18, 0x43ff},
181326dd2974SRobert Hancock 	{0x1c, 0x19, 0xc3ff},
181426dd2974SRobert Hancock 	{0x1c, 0x1a, 0x6fff},
181526dd2974SRobert Hancock 	{0x1c, 0x1b, 0x07ff},
181626dd2974SRobert Hancock 	{0x1c, 0x1c, 0x0fff},
181726dd2974SRobert Hancock 	{0x1c, 0x1d, 0xe7ff},
181826dd2974SRobert Hancock 	{0x1c, 0x1e, 0xefff},
181926dd2974SRobert Hancock 	{0x1c, 0x20, 0xeeee},
182026dd2974SRobert Hancock };
182126dd2974SRobert Hancock 
182226dd2974SRobert Hancock static int ksz9477_config_init(struct phy_device *phydev)
182326dd2974SRobert Hancock {
182426dd2974SRobert Hancock 	int err;
182526dd2974SRobert Hancock 	int i;
182626dd2974SRobert Hancock 
182726dd2974SRobert Hancock 	/* Apply PHY settings to address errata listed in
182826dd2974SRobert Hancock 	 * KSZ9477, KSZ9897, KSZ9896, KSZ9567, KSZ8565
182926dd2974SRobert Hancock 	 * Silicon Errata and Data Sheet Clarification documents.
183026dd2974SRobert Hancock 	 *
183126dd2974SRobert Hancock 	 * Document notes: Before configuring the PHY MMD registers, it is
183226dd2974SRobert Hancock 	 * necessary to set the PHY to 100 Mbps speed with auto-negotiation
183326dd2974SRobert Hancock 	 * disabled by writing to register 0xN100-0xN101. After writing the
183426dd2974SRobert Hancock 	 * MMD registers, and after all errata workarounds that involve PHY
183526dd2974SRobert Hancock 	 * register settings, write register 0xN100-0xN101 again to enable
183626dd2974SRobert Hancock 	 * and restart auto-negotiation.
183726dd2974SRobert Hancock 	 */
183826dd2974SRobert Hancock 	err = phy_write(phydev, MII_BMCR, BMCR_SPEED100 | BMCR_FULLDPLX);
183926dd2974SRobert Hancock 	if (err)
184026dd2974SRobert Hancock 		return err;
184126dd2974SRobert Hancock 
184226dd2974SRobert Hancock 	for (i = 0; i < ARRAY_SIZE(ksz9477_errata_writes); ++i) {
184326dd2974SRobert Hancock 		const struct ksz9477_errata_write *errata = &ksz9477_errata_writes[i];
184426dd2974SRobert Hancock 
184526dd2974SRobert Hancock 		err = phy_write_mmd(phydev, errata->dev_addr, errata->reg_addr, errata->val);
184626dd2974SRobert Hancock 		if (err)
184726dd2974SRobert Hancock 			return err;
184826dd2974SRobert Hancock 	}
184926dd2974SRobert Hancock 
185026dd2974SRobert Hancock 	err = genphy_restart_aneg(phydev);
185126dd2974SRobert Hancock 	if (err)
185226dd2974SRobert Hancock 		return err;
185326dd2974SRobert Hancock 
185426dd2974SRobert Hancock 	return kszphy_config_init(phydev);
185526dd2974SRobert Hancock }
185626dd2974SRobert Hancock 
18572b2427d0SAndrew Lunn static int kszphy_get_sset_count(struct phy_device *phydev)
18582b2427d0SAndrew Lunn {
18592b2427d0SAndrew Lunn 	return ARRAY_SIZE(kszphy_hw_stats);
18602b2427d0SAndrew Lunn }
18612b2427d0SAndrew Lunn 
18622b2427d0SAndrew Lunn static void kszphy_get_strings(struct phy_device *phydev, u8 *data)
18632b2427d0SAndrew Lunn {
18642b2427d0SAndrew Lunn 	int i;
18652b2427d0SAndrew Lunn 
18662b2427d0SAndrew Lunn 	for (i = 0; i < ARRAY_SIZE(kszphy_hw_stats); i++) {
1867fb3ceec1SWolfram Sang 		strscpy(data + i * ETH_GSTRING_LEN,
18682b2427d0SAndrew Lunn 			kszphy_hw_stats[i].string, ETH_GSTRING_LEN);
18692b2427d0SAndrew Lunn 	}
18702b2427d0SAndrew Lunn }
18712b2427d0SAndrew Lunn 
18722b2427d0SAndrew Lunn static u64 kszphy_get_stat(struct phy_device *phydev, int i)
18732b2427d0SAndrew Lunn {
18742b2427d0SAndrew Lunn 	struct kszphy_hw_stat stat = kszphy_hw_stats[i];
18752b2427d0SAndrew Lunn 	struct kszphy_priv *priv = phydev->priv;
1876321b4d4bSAndrew Lunn 	int val;
1877321b4d4bSAndrew Lunn 	u64 ret;
18782b2427d0SAndrew Lunn 
18792b2427d0SAndrew Lunn 	val = phy_read(phydev, stat.reg);
18802b2427d0SAndrew Lunn 	if (val < 0) {
18816c3442f5SJisheng Zhang 		ret = U64_MAX;
18822b2427d0SAndrew Lunn 	} else {
18832b2427d0SAndrew Lunn 		val = val & ((1 << stat.bits) - 1);
18842b2427d0SAndrew Lunn 		priv->stats[i] += val;
1885321b4d4bSAndrew Lunn 		ret = priv->stats[i];
18862b2427d0SAndrew Lunn 	}
18872b2427d0SAndrew Lunn 
1888321b4d4bSAndrew Lunn 	return ret;
18892b2427d0SAndrew Lunn }
18902b2427d0SAndrew Lunn 
18912b2427d0SAndrew Lunn static void kszphy_get_stats(struct phy_device *phydev,
18922b2427d0SAndrew Lunn 			     struct ethtool_stats *stats, u64 *data)
18932b2427d0SAndrew Lunn {
18942b2427d0SAndrew Lunn 	int i;
18952b2427d0SAndrew Lunn 
18962b2427d0SAndrew Lunn 	for (i = 0; i < ARRAY_SIZE(kszphy_hw_stats); i++)
18972b2427d0SAndrew Lunn 		data[i] = kszphy_get_stat(phydev, i);
18982b2427d0SAndrew Lunn }
18992b2427d0SAndrew Lunn 
1900836384d2SWenyou Yang static int kszphy_suspend(struct phy_device *phydev)
1901836384d2SWenyou Yang {
1902836384d2SWenyou Yang 	/* Disable PHY Interrupts */
1903836384d2SWenyou Yang 	if (phy_interrupt_is_valid(phydev)) {
1904836384d2SWenyou Yang 		phydev->interrupts = PHY_INTERRUPT_DISABLED;
1905836384d2SWenyou Yang 		if (phydev->drv->config_intr)
1906836384d2SWenyou Yang 			phydev->drv->config_intr(phydev);
1907836384d2SWenyou Yang 	}
1908836384d2SWenyou Yang 
1909836384d2SWenyou Yang 	return genphy_suspend(phydev);
1910836384d2SWenyou Yang }
1911836384d2SWenyou Yang 
1912a516b7f7SDivya Koppera static void kszphy_parse_led_mode(struct phy_device *phydev)
1913a516b7f7SDivya Koppera {
1914a516b7f7SDivya Koppera 	const struct kszphy_type *type = phydev->drv->driver_data;
1915a516b7f7SDivya Koppera 	const struct device_node *np = phydev->mdio.dev.of_node;
1916a516b7f7SDivya Koppera 	struct kszphy_priv *priv = phydev->priv;
1917a516b7f7SDivya Koppera 	int ret;
1918a516b7f7SDivya Koppera 
1919a516b7f7SDivya Koppera 	if (type && type->led_mode_reg) {
1920a516b7f7SDivya Koppera 		ret = of_property_read_u32(np, "micrel,led-mode",
1921a516b7f7SDivya Koppera 					   &priv->led_mode);
1922a516b7f7SDivya Koppera 
1923a516b7f7SDivya Koppera 		if (ret)
1924a516b7f7SDivya Koppera 			priv->led_mode = -1;
1925a516b7f7SDivya Koppera 
1926a516b7f7SDivya Koppera 		if (priv->led_mode > 3) {
1927a516b7f7SDivya Koppera 			phydev_err(phydev, "invalid led mode: 0x%02x\n",
1928a516b7f7SDivya Koppera 				   priv->led_mode);
1929a516b7f7SDivya Koppera 			priv->led_mode = -1;
1930a516b7f7SDivya Koppera 		}
1931a516b7f7SDivya Koppera 	} else {
1932a516b7f7SDivya Koppera 		priv->led_mode = -1;
1933a516b7f7SDivya Koppera 	}
1934a516b7f7SDivya Koppera }
1935a516b7f7SDivya Koppera 
1936f5aba91dSAlexandre Belloni static int kszphy_resume(struct phy_device *phydev)
1937f5aba91dSAlexandre Belloni {
193879e498a9SLeonard Crestez 	int ret;
193979e498a9SLeonard Crestez 
1940836384d2SWenyou Yang 	genphy_resume(phydev);
1941f5aba91dSAlexandre Belloni 
19426110dff7SOleksij Rempel 	/* After switching from power-down to normal mode, an internal global
19436110dff7SOleksij Rempel 	 * reset is automatically generated. Wait a minimum of 1 ms before
19446110dff7SOleksij Rempel 	 * read/write access to the PHY registers.
19456110dff7SOleksij Rempel 	 */
19466110dff7SOleksij Rempel 	usleep_range(1000, 2000);
19476110dff7SOleksij Rempel 
194879e498a9SLeonard Crestez 	ret = kszphy_config_reset(phydev);
194979e498a9SLeonard Crestez 	if (ret)
195079e498a9SLeonard Crestez 		return ret;
195179e498a9SLeonard Crestez 
1952836384d2SWenyou Yang 	/* Enable PHY Interrupts */
1953836384d2SWenyou Yang 	if (phy_interrupt_is_valid(phydev)) {
1954836384d2SWenyou Yang 		phydev->interrupts = PHY_INTERRUPT_ENABLED;
1955836384d2SWenyou Yang 		if (phydev->drv->config_intr)
1956836384d2SWenyou Yang 			phydev->drv->config_intr(phydev);
1957836384d2SWenyou Yang 	}
1958f5aba91dSAlexandre Belloni 
1959f5aba91dSAlexandre Belloni 	return 0;
1960f5aba91dSAlexandre Belloni }
1961f5aba91dSAlexandre Belloni 
1962e6a423a8SJohan Hovold static int kszphy_probe(struct phy_device *phydev)
1963e6a423a8SJohan Hovold {
1964e6a423a8SJohan Hovold 	const struct kszphy_type *type = phydev->drv->driver_data;
1965e5a03bfdSAndrew Lunn 	const struct device_node *np = phydev->mdio.dev.of_node;
1966e6a423a8SJohan Hovold 	struct kszphy_priv *priv;
196763f44b2bSJohan Hovold 	struct clk *clk;
1968e6a423a8SJohan Hovold 
1969e5a03bfdSAndrew Lunn 	priv = devm_kzalloc(&phydev->mdio.dev, sizeof(*priv), GFP_KERNEL);
1970e6a423a8SJohan Hovold 	if (!priv)
1971e6a423a8SJohan Hovold 		return -ENOMEM;
1972e6a423a8SJohan Hovold 
1973e6a423a8SJohan Hovold 	phydev->priv = priv;
1974e6a423a8SJohan Hovold 
1975e6a423a8SJohan Hovold 	priv->type = type;
1976e6a423a8SJohan Hovold 
1977a516b7f7SDivya Koppera 	kszphy_parse_led_mode(phydev);
1978e7a792e9SJohan Hovold 
1979e5a03bfdSAndrew Lunn 	clk = devm_clk_get(&phydev->mdio.dev, "rmii-ref");
1980bced8701SNiklas Cassel 	/* NOTE: clk may be NULL if building without CONFIG_HAVE_CLK */
1981bced8701SNiklas Cassel 	if (!IS_ERR_OR_NULL(clk)) {
19821fadee0cSSascha Hauer 		unsigned long rate = clk_get_rate(clk);
198386dc1342SJohan Hovold 		bool rmii_ref_clk_sel_25_mhz;
19841fadee0cSSascha Hauer 
1985f2ef6f75SFabio Estevam 		if (type)
198663f44b2bSJohan Hovold 			priv->rmii_ref_clk_sel = type->has_rmii_ref_clk_sel;
198786dc1342SJohan Hovold 		rmii_ref_clk_sel_25_mhz = of_property_read_bool(np,
198886dc1342SJohan Hovold 				"micrel,rmii-reference-clock-select-25-mhz");
198963f44b2bSJohan Hovold 
19901fadee0cSSascha Hauer 		if (rate > 24500000 && rate < 25500000) {
199186dc1342SJohan Hovold 			priv->rmii_ref_clk_sel_val = rmii_ref_clk_sel_25_mhz;
19921fadee0cSSascha Hauer 		} else if (rate > 49500000 && rate < 50500000) {
199386dc1342SJohan Hovold 			priv->rmii_ref_clk_sel_val = !rmii_ref_clk_sel_25_mhz;
19941fadee0cSSascha Hauer 		} else {
199572ba48beSAndrew Lunn 			phydev_err(phydev, "Clock rate out of range: %ld\n",
199672ba48beSAndrew Lunn 				   rate);
19971fadee0cSSascha Hauer 			return -EINVAL;
19981fadee0cSSascha Hauer 		}
19991fadee0cSSascha Hauer 	}
20001fadee0cSSascha Hauer 
20014217a64eSMichael Walle 	if (ksz8041_fiber_mode(phydev))
20024217a64eSMichael Walle 		phydev->port = PORT_FIBRE;
20034217a64eSMichael Walle 
200463f44b2bSJohan Hovold 	/* Support legacy board-file configuration */
200563f44b2bSJohan Hovold 	if (phydev->dev_flags & MICREL_PHY_50MHZ_CLK) {
200663f44b2bSJohan Hovold 		priv->rmii_ref_clk_sel = true;
200763f44b2bSJohan Hovold 		priv->rmii_ref_clk_sel_val = true;
200863f44b2bSJohan Hovold 	}
200963f44b2bSJohan Hovold 
201063f44b2bSJohan Hovold 	return 0;
20111fadee0cSSascha Hauer }
20121fadee0cSSascha Hauer 
201321b688daSDivya Koppera static int lan8814_cable_test_start(struct phy_device *phydev)
201421b688daSDivya Koppera {
201521b688daSDivya Koppera 	/* If autoneg is enabled, we won't be able to test cross pair
201621b688daSDivya Koppera 	 * short. In this case, the PHY will "detect" a link and
201721b688daSDivya Koppera 	 * confuse the internal state machine - disable auto neg here.
201821b688daSDivya Koppera 	 * Set the speed to 1000mbit and full duplex.
201921b688daSDivya Koppera 	 */
202021b688daSDivya Koppera 	return phy_modify(phydev, MII_BMCR, BMCR_ANENABLE | BMCR_SPEED100,
202121b688daSDivya Koppera 			  BMCR_SPEED1000 | BMCR_FULLDPLX);
202221b688daSDivya Koppera }
202321b688daSDivya Koppera 
202449011e0cSOleksij Rempel static int ksz886x_cable_test_start(struct phy_device *phydev)
202549011e0cSOleksij Rempel {
202649011e0cSOleksij Rempel 	if (phydev->dev_flags & MICREL_KSZ8_P1_ERRATA)
202749011e0cSOleksij Rempel 		return -EOPNOTSUPP;
202849011e0cSOleksij Rempel 
202949011e0cSOleksij Rempel 	/* If autoneg is enabled, we won't be able to test cross pair
203049011e0cSOleksij Rempel 	 * short. In this case, the PHY will "detect" a link and
203149011e0cSOleksij Rempel 	 * confuse the internal state machine - disable auto neg here.
203249011e0cSOleksij Rempel 	 * If autoneg is disabled, we should set the speed to 10mbit.
203349011e0cSOleksij Rempel 	 */
203449011e0cSOleksij Rempel 	return phy_clear_bits(phydev, MII_BMCR, BMCR_ANENABLE | BMCR_SPEED100);
203549011e0cSOleksij Rempel }
203649011e0cSOleksij Rempel 
2037fa182ea2SDivya Koppera static __always_inline int ksz886x_cable_test_result_trans(u16 status, u16 mask)
203849011e0cSOleksij Rempel {
203921b688daSDivya Koppera 	switch (FIELD_GET(mask, status)) {
204049011e0cSOleksij Rempel 	case KSZ8081_LMD_STAT_NORMAL:
204149011e0cSOleksij Rempel 		return ETHTOOL_A_CABLE_RESULT_CODE_OK;
204249011e0cSOleksij Rempel 	case KSZ8081_LMD_STAT_SHORT:
204349011e0cSOleksij Rempel 		return ETHTOOL_A_CABLE_RESULT_CODE_SAME_SHORT;
204449011e0cSOleksij Rempel 	case KSZ8081_LMD_STAT_OPEN:
204549011e0cSOleksij Rempel 		return ETHTOOL_A_CABLE_RESULT_CODE_OPEN;
204649011e0cSOleksij Rempel 	case KSZ8081_LMD_STAT_FAIL:
204749011e0cSOleksij Rempel 		fallthrough;
204849011e0cSOleksij Rempel 	default:
204949011e0cSOleksij Rempel 		return ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC;
205049011e0cSOleksij Rempel 	}
205149011e0cSOleksij Rempel }
205249011e0cSOleksij Rempel 
2053fa182ea2SDivya Koppera static __always_inline bool ksz886x_cable_test_failed(u16 status, u16 mask)
205449011e0cSOleksij Rempel {
205521b688daSDivya Koppera 	return FIELD_GET(mask, status) ==
205649011e0cSOleksij Rempel 		KSZ8081_LMD_STAT_FAIL;
205749011e0cSOleksij Rempel }
205849011e0cSOleksij Rempel 
2059fa182ea2SDivya Koppera static __always_inline bool ksz886x_cable_test_fault_length_valid(u16 status, u16 mask)
206049011e0cSOleksij Rempel {
206121b688daSDivya Koppera 	switch (FIELD_GET(mask, status)) {
206249011e0cSOleksij Rempel 	case KSZ8081_LMD_STAT_OPEN:
206349011e0cSOleksij Rempel 		fallthrough;
206449011e0cSOleksij Rempel 	case KSZ8081_LMD_STAT_SHORT:
206549011e0cSOleksij Rempel 		return true;
206649011e0cSOleksij Rempel 	}
206749011e0cSOleksij Rempel 	return false;
206849011e0cSOleksij Rempel }
206949011e0cSOleksij Rempel 
2070fa182ea2SDivya Koppera static __always_inline int ksz886x_cable_test_fault_length(struct phy_device *phydev,
2071fa182ea2SDivya Koppera 							   u16 status, u16 data_mask)
207249011e0cSOleksij Rempel {
207349011e0cSOleksij Rempel 	int dt;
207449011e0cSOleksij Rempel 
207549011e0cSOleksij Rempel 	/* According to the data sheet the distance to the fault is
207621b688daSDivya Koppera 	 * DELTA_TIME * 0.4 meters for ksz phys.
207721b688daSDivya Koppera 	 * (DELTA_TIME - 22) * 0.8 for lan8814 phy.
207849011e0cSOleksij Rempel 	 */
207921b688daSDivya Koppera 	dt = FIELD_GET(data_mask, status);
208049011e0cSOleksij Rempel 
20814b159f50SRussell King 	if (phydev_id_compare(phydev, PHY_ID_LAN8814))
208221b688daSDivya Koppera 		return ((dt - 22) * 800) / 10;
208321b688daSDivya Koppera 	else
208449011e0cSOleksij Rempel 		return (dt * 400) / 10;
208549011e0cSOleksij Rempel }
208649011e0cSOleksij Rempel 
208749011e0cSOleksij Rempel static int ksz886x_cable_test_wait_for_completion(struct phy_device *phydev)
208849011e0cSOleksij Rempel {
208921b688daSDivya Koppera 	const struct kszphy_type *type = phydev->drv->driver_data;
209049011e0cSOleksij Rempel 	int val, ret;
209149011e0cSOleksij Rempel 
209221b688daSDivya Koppera 	ret = phy_read_poll_timeout(phydev, type->cable_diag_reg, val,
209349011e0cSOleksij Rempel 				    !(val & KSZ8081_LMD_ENABLE_TEST),
209449011e0cSOleksij Rempel 				    30000, 100000, true);
209549011e0cSOleksij Rempel 
209649011e0cSOleksij Rempel 	return ret < 0 ? ret : 0;
209749011e0cSOleksij Rempel }
209849011e0cSOleksij Rempel 
209921b688daSDivya Koppera static int lan8814_cable_test_one_pair(struct phy_device *phydev, int pair)
210021b688daSDivya Koppera {
210121b688daSDivya Koppera 	static const int ethtool_pair[] = { ETHTOOL_A_CABLE_PAIR_A,
210221b688daSDivya Koppera 					    ETHTOOL_A_CABLE_PAIR_B,
210321b688daSDivya Koppera 					    ETHTOOL_A_CABLE_PAIR_C,
210421b688daSDivya Koppera 					    ETHTOOL_A_CABLE_PAIR_D,
210521b688daSDivya Koppera 					  };
210621b688daSDivya Koppera 	u32 fault_length;
210721b688daSDivya Koppera 	int ret;
210821b688daSDivya Koppera 	int val;
210921b688daSDivya Koppera 
211021b688daSDivya Koppera 	val = KSZ8081_LMD_ENABLE_TEST;
211121b688daSDivya Koppera 	val = val | (pair << LAN8814_PAIR_BIT_SHIFT);
211221b688daSDivya Koppera 
211321b688daSDivya Koppera 	ret = phy_write(phydev, LAN8814_CABLE_DIAG, val);
211421b688daSDivya Koppera 	if (ret < 0)
211521b688daSDivya Koppera 		return ret;
211621b688daSDivya Koppera 
211721b688daSDivya Koppera 	ret = ksz886x_cable_test_wait_for_completion(phydev);
211821b688daSDivya Koppera 	if (ret)
211921b688daSDivya Koppera 		return ret;
212021b688daSDivya Koppera 
212121b688daSDivya Koppera 	val = phy_read(phydev, LAN8814_CABLE_DIAG);
212221b688daSDivya Koppera 	if (val < 0)
212321b688daSDivya Koppera 		return val;
212421b688daSDivya Koppera 
212521b688daSDivya Koppera 	if (ksz886x_cable_test_failed(val, LAN8814_CABLE_DIAG_STAT_MASK))
212621b688daSDivya Koppera 		return -EAGAIN;
212721b688daSDivya Koppera 
212821b688daSDivya Koppera 	ret = ethnl_cable_test_result(phydev, ethtool_pair[pair],
212921b688daSDivya Koppera 				      ksz886x_cable_test_result_trans(val,
213021b688daSDivya Koppera 								      LAN8814_CABLE_DIAG_STAT_MASK
213121b688daSDivya Koppera 								      ));
213221b688daSDivya Koppera 	if (ret)
213321b688daSDivya Koppera 		return ret;
213421b688daSDivya Koppera 
213521b688daSDivya Koppera 	if (!ksz886x_cable_test_fault_length_valid(val, LAN8814_CABLE_DIAG_STAT_MASK))
213621b688daSDivya Koppera 		return 0;
213721b688daSDivya Koppera 
213821b688daSDivya Koppera 	fault_length = ksz886x_cable_test_fault_length(phydev, val,
213921b688daSDivya Koppera 						       LAN8814_CABLE_DIAG_VCT_DATA_MASK);
214021b688daSDivya Koppera 
214121b688daSDivya Koppera 	return ethnl_cable_test_fault_length(phydev, ethtool_pair[pair], fault_length);
214221b688daSDivya Koppera }
214321b688daSDivya Koppera 
214449011e0cSOleksij Rempel static int ksz886x_cable_test_one_pair(struct phy_device *phydev, int pair)
214549011e0cSOleksij Rempel {
214649011e0cSOleksij Rempel 	static const int ethtool_pair[] = {
214749011e0cSOleksij Rempel 		ETHTOOL_A_CABLE_PAIR_A,
214849011e0cSOleksij Rempel 		ETHTOOL_A_CABLE_PAIR_B,
214949011e0cSOleksij Rempel 	};
215049011e0cSOleksij Rempel 	int ret, val, mdix;
215121b688daSDivya Koppera 	u32 fault_length;
215249011e0cSOleksij Rempel 
215349011e0cSOleksij Rempel 	/* There is no way to choice the pair, like we do one ksz9031.
215449011e0cSOleksij Rempel 	 * We can workaround this limitation by using the MDI-X functionality.
215549011e0cSOleksij Rempel 	 */
215649011e0cSOleksij Rempel 	if (pair == 0)
215749011e0cSOleksij Rempel 		mdix = ETH_TP_MDI;
215849011e0cSOleksij Rempel 	else
215949011e0cSOleksij Rempel 		mdix = ETH_TP_MDI_X;
216049011e0cSOleksij Rempel 
216149011e0cSOleksij Rempel 	switch (phydev->phy_id & MICREL_PHY_ID_MASK) {
216249011e0cSOleksij Rempel 	case PHY_ID_KSZ8081:
216349011e0cSOleksij Rempel 		ret = ksz8081_config_mdix(phydev, mdix);
216449011e0cSOleksij Rempel 		break;
216549011e0cSOleksij Rempel 	case PHY_ID_KSZ886X:
216649011e0cSOleksij Rempel 		ret = ksz886x_config_mdix(phydev, mdix);
216749011e0cSOleksij Rempel 		break;
216849011e0cSOleksij Rempel 	default:
216949011e0cSOleksij Rempel 		ret = -ENODEV;
217049011e0cSOleksij Rempel 	}
217149011e0cSOleksij Rempel 
217249011e0cSOleksij Rempel 	if (ret)
217349011e0cSOleksij Rempel 		return ret;
217449011e0cSOleksij Rempel 
217549011e0cSOleksij Rempel 	/* Now we are ready to fire. This command will send a 100ns pulse
217649011e0cSOleksij Rempel 	 * to the pair.
217749011e0cSOleksij Rempel 	 */
217849011e0cSOleksij Rempel 	ret = phy_write(phydev, KSZ8081_LMD, KSZ8081_LMD_ENABLE_TEST);
217949011e0cSOleksij Rempel 	if (ret)
218049011e0cSOleksij Rempel 		return ret;
218149011e0cSOleksij Rempel 
218249011e0cSOleksij Rempel 	ret = ksz886x_cable_test_wait_for_completion(phydev);
218349011e0cSOleksij Rempel 	if (ret)
218449011e0cSOleksij Rempel 		return ret;
218549011e0cSOleksij Rempel 
218649011e0cSOleksij Rempel 	val = phy_read(phydev, KSZ8081_LMD);
218749011e0cSOleksij Rempel 	if (val < 0)
218849011e0cSOleksij Rempel 		return val;
218949011e0cSOleksij Rempel 
219021b688daSDivya Koppera 	if (ksz886x_cable_test_failed(val, KSZ8081_LMD_STAT_MASK))
219149011e0cSOleksij Rempel 		return -EAGAIN;
219249011e0cSOleksij Rempel 
219349011e0cSOleksij Rempel 	ret = ethnl_cable_test_result(phydev, ethtool_pair[pair],
219421b688daSDivya Koppera 				      ksz886x_cable_test_result_trans(val, KSZ8081_LMD_STAT_MASK));
219549011e0cSOleksij Rempel 	if (ret)
219649011e0cSOleksij Rempel 		return ret;
219749011e0cSOleksij Rempel 
219821b688daSDivya Koppera 	if (!ksz886x_cable_test_fault_length_valid(val, KSZ8081_LMD_STAT_MASK))
219949011e0cSOleksij Rempel 		return 0;
220049011e0cSOleksij Rempel 
220121b688daSDivya Koppera 	fault_length = ksz886x_cable_test_fault_length(phydev, val, KSZ8081_LMD_DELTA_TIME_MASK);
220221b688daSDivya Koppera 
220321b688daSDivya Koppera 	return ethnl_cable_test_fault_length(phydev, ethtool_pair[pair], fault_length);
220449011e0cSOleksij Rempel }
220549011e0cSOleksij Rempel 
220649011e0cSOleksij Rempel static int ksz886x_cable_test_get_status(struct phy_device *phydev,
220749011e0cSOleksij Rempel 					 bool *finished)
220849011e0cSOleksij Rempel {
220921b688daSDivya Koppera 	const struct kszphy_type *type = phydev->drv->driver_data;
221021b688daSDivya Koppera 	unsigned long pair_mask = type->pair_mask;
221149011e0cSOleksij Rempel 	int retries = 20;
2212d50ede4fSDivya Koppera 	int ret = 0;
2213d50ede4fSDivya Koppera 	int pair;
221449011e0cSOleksij Rempel 
221549011e0cSOleksij Rempel 	*finished = false;
221649011e0cSOleksij Rempel 
221749011e0cSOleksij Rempel 	/* Try harder if link partner is active */
221849011e0cSOleksij Rempel 	while (pair_mask && retries--) {
221949011e0cSOleksij Rempel 		for_each_set_bit(pair, &pair_mask, 4) {
222021b688daSDivya Koppera 			if (type->cable_diag_reg == LAN8814_CABLE_DIAG)
222121b688daSDivya Koppera 				ret = lan8814_cable_test_one_pair(phydev, pair);
222221b688daSDivya Koppera 			else
222349011e0cSOleksij Rempel 				ret = ksz886x_cable_test_one_pair(phydev, pair);
222449011e0cSOleksij Rempel 			if (ret == -EAGAIN)
222549011e0cSOleksij Rempel 				continue;
222649011e0cSOleksij Rempel 			if (ret < 0)
222749011e0cSOleksij Rempel 				return ret;
222849011e0cSOleksij Rempel 			clear_bit(pair, &pair_mask);
222949011e0cSOleksij Rempel 		}
223049011e0cSOleksij Rempel 		/* If link partner is in autonegotiation mode it will send 2ms
223149011e0cSOleksij Rempel 		 * of FLPs with at least 6ms of silence.
223249011e0cSOleksij Rempel 		 * Add 2ms sleep to have better chances to hit this silence.
223349011e0cSOleksij Rempel 		 */
223449011e0cSOleksij Rempel 		if (pair_mask)
223549011e0cSOleksij Rempel 			msleep(2);
223649011e0cSOleksij Rempel 	}
223749011e0cSOleksij Rempel 
223849011e0cSOleksij Rempel 	*finished = true;
223949011e0cSOleksij Rempel 
224049011e0cSOleksij Rempel 	return ret;
224149011e0cSOleksij Rempel }
224249011e0cSOleksij Rempel 
22437c2dcfa2SHoratiu Vultur #define LAN_EXT_PAGE_ACCESS_CONTROL			0x16
22447c2dcfa2SHoratiu Vultur #define LAN_EXT_PAGE_ACCESS_ADDRESS_DATA		0x17
22457c2dcfa2SHoratiu Vultur #define LAN_EXT_PAGE_ACCESS_CTRL_EP_FUNC		0x4000
22467c2dcfa2SHoratiu Vultur 
22477467d716SHoratiu Vultur #define LAN8814_QSGMII_SOFT_RESET			0x43
22487467d716SHoratiu Vultur #define LAN8814_QSGMII_SOFT_RESET_BIT			BIT(0)
22497467d716SHoratiu Vultur #define LAN8814_QSGMII_PCS1G_ANEG_CONFIG		0x13
22507467d716SHoratiu Vultur #define LAN8814_QSGMII_PCS1G_ANEG_CONFIG_ANEG_ENA	BIT(3)
22517467d716SHoratiu Vultur #define LAN8814_ALIGN_SWAP				0x4a
22527467d716SHoratiu Vultur #define LAN8814_ALIGN_TX_A_B_SWAP			0x1
22537467d716SHoratiu Vultur #define LAN8814_ALIGN_TX_A_B_SWAP_MASK			GENMASK(2, 0)
22547467d716SHoratiu Vultur 
22557c2dcfa2SHoratiu Vultur #define LAN8804_ALIGN_SWAP				0x4a
22567c2dcfa2SHoratiu Vultur #define LAN8804_ALIGN_TX_A_B_SWAP			0x1
22577c2dcfa2SHoratiu Vultur #define LAN8804_ALIGN_TX_A_B_SWAP_MASK			GENMASK(2, 0)
22587c2dcfa2SHoratiu Vultur #define LAN8814_CLOCK_MANAGEMENT			0xd
22597c2dcfa2SHoratiu Vultur #define LAN8814_LINK_QUALITY				0x8e
22607c2dcfa2SHoratiu Vultur 
22617c2dcfa2SHoratiu Vultur static int lanphy_read_page_reg(struct phy_device *phydev, int page, u32 addr)
22627c2dcfa2SHoratiu Vultur {
226312a4d677SWan Jiabing 	int data;
22647c2dcfa2SHoratiu Vultur 
22654488f6b6SDivya Koppera 	phy_lock_mdio_bus(phydev);
22664488f6b6SDivya Koppera 	__phy_write(phydev, LAN_EXT_PAGE_ACCESS_CONTROL, page);
22674488f6b6SDivya Koppera 	__phy_write(phydev, LAN_EXT_PAGE_ACCESS_ADDRESS_DATA, addr);
22684488f6b6SDivya Koppera 	__phy_write(phydev, LAN_EXT_PAGE_ACCESS_CONTROL,
22697c2dcfa2SHoratiu Vultur 		    (page | LAN_EXT_PAGE_ACCESS_CTRL_EP_FUNC));
22704488f6b6SDivya Koppera 	data = __phy_read(phydev, LAN_EXT_PAGE_ACCESS_ADDRESS_DATA);
22714488f6b6SDivya Koppera 	phy_unlock_mdio_bus(phydev);
22727c2dcfa2SHoratiu Vultur 
22737c2dcfa2SHoratiu Vultur 	return data;
22747c2dcfa2SHoratiu Vultur }
22757c2dcfa2SHoratiu Vultur 
22767c2dcfa2SHoratiu Vultur static int lanphy_write_page_reg(struct phy_device *phydev, int page, u16 addr,
22777c2dcfa2SHoratiu Vultur 				 u16 val)
22787c2dcfa2SHoratiu Vultur {
22794488f6b6SDivya Koppera 	phy_lock_mdio_bus(phydev);
22804488f6b6SDivya Koppera 	__phy_write(phydev, LAN_EXT_PAGE_ACCESS_CONTROL, page);
22814488f6b6SDivya Koppera 	__phy_write(phydev, LAN_EXT_PAGE_ACCESS_ADDRESS_DATA, addr);
22824488f6b6SDivya Koppera 	__phy_write(phydev, LAN_EXT_PAGE_ACCESS_CONTROL,
22834488f6b6SDivya Koppera 		    page | LAN_EXT_PAGE_ACCESS_CTRL_EP_FUNC);
22847c2dcfa2SHoratiu Vultur 
22854488f6b6SDivya Koppera 	val = __phy_write(phydev, LAN_EXT_PAGE_ACCESS_ADDRESS_DATA, val);
22864488f6b6SDivya Koppera 	if (val != 0)
22877c2dcfa2SHoratiu Vultur 		phydev_err(phydev, "Error: phy_write has returned error %d\n",
22887c2dcfa2SHoratiu Vultur 			   val);
22894488f6b6SDivya Koppera 	phy_unlock_mdio_bus(phydev);
22907c2dcfa2SHoratiu Vultur 	return val;
22917c2dcfa2SHoratiu Vultur }
22927c2dcfa2SHoratiu Vultur 
2293ece19502SDivya Koppera static int lan8814_config_ts_intr(struct phy_device *phydev, bool enable)
22947467d716SHoratiu Vultur {
2295ece19502SDivya Koppera 	u16 val = 0;
22967467d716SHoratiu Vultur 
2297ece19502SDivya Koppera 	if (enable)
2298ece19502SDivya Koppera 		val = PTP_TSU_INT_EN_PTP_TX_TS_EN_ |
2299ece19502SDivya Koppera 		      PTP_TSU_INT_EN_PTP_TX_TS_OVRFL_EN_ |
2300ece19502SDivya Koppera 		      PTP_TSU_INT_EN_PTP_RX_TS_EN_ |
2301ece19502SDivya Koppera 		      PTP_TSU_INT_EN_PTP_RX_TS_OVRFL_EN_;
23027467d716SHoratiu Vultur 
2303ece19502SDivya Koppera 	return lanphy_write_page_reg(phydev, 5, PTP_TSU_INT_EN, val);
2304ece19502SDivya Koppera }
23057467d716SHoratiu Vultur 
2306ece19502SDivya Koppera static void lan8814_ptp_rx_ts_get(struct phy_device *phydev,
2307ece19502SDivya Koppera 				  u32 *seconds, u32 *nano_seconds, u16 *seq_id)
2308ece19502SDivya Koppera {
2309ece19502SDivya Koppera 	*seconds = lanphy_read_page_reg(phydev, 5, PTP_RX_INGRESS_SEC_HI);
2310ece19502SDivya Koppera 	*seconds = (*seconds << 16) |
2311ece19502SDivya Koppera 		   lanphy_read_page_reg(phydev, 5, PTP_RX_INGRESS_SEC_LO);
2312ece19502SDivya Koppera 
2313ece19502SDivya Koppera 	*nano_seconds = lanphy_read_page_reg(phydev, 5, PTP_RX_INGRESS_NS_HI);
2314ece19502SDivya Koppera 	*nano_seconds = ((*nano_seconds & 0x3fff) << 16) |
2315ece19502SDivya Koppera 			lanphy_read_page_reg(phydev, 5, PTP_RX_INGRESS_NS_LO);
2316ece19502SDivya Koppera 
2317ece19502SDivya Koppera 	*seq_id = lanphy_read_page_reg(phydev, 5, PTP_RX_MSG_HEADER2);
2318ece19502SDivya Koppera }
2319ece19502SDivya Koppera 
2320ece19502SDivya Koppera static void lan8814_ptp_tx_ts_get(struct phy_device *phydev,
2321ece19502SDivya Koppera 				  u32 *seconds, u32 *nano_seconds, u16 *seq_id)
2322ece19502SDivya Koppera {
2323ece19502SDivya Koppera 	*seconds = lanphy_read_page_reg(phydev, 5, PTP_TX_EGRESS_SEC_HI);
2324ece19502SDivya Koppera 	*seconds = *seconds << 16 |
2325ece19502SDivya Koppera 		   lanphy_read_page_reg(phydev, 5, PTP_TX_EGRESS_SEC_LO);
2326ece19502SDivya Koppera 
2327ece19502SDivya Koppera 	*nano_seconds = lanphy_read_page_reg(phydev, 5, PTP_TX_EGRESS_NS_HI);
2328ece19502SDivya Koppera 	*nano_seconds = ((*nano_seconds & 0x3fff) << 16) |
2329ece19502SDivya Koppera 			lanphy_read_page_reg(phydev, 5, PTP_TX_EGRESS_NS_LO);
2330ece19502SDivya Koppera 
2331ece19502SDivya Koppera 	*seq_id = lanphy_read_page_reg(phydev, 5, PTP_TX_MSG_HEADER2);
2332ece19502SDivya Koppera }
2333ece19502SDivya Koppera 
2334ece19502SDivya Koppera static int lan8814_ts_info(struct mii_timestamper *mii_ts, struct ethtool_ts_info *info)
2335ece19502SDivya Koppera {
2336ece19502SDivya Koppera 	struct kszphy_ptp_priv *ptp_priv = container_of(mii_ts, struct kszphy_ptp_priv, mii_ts);
2337ece19502SDivya Koppera 	struct phy_device *phydev = ptp_priv->phydev;
2338ece19502SDivya Koppera 	struct lan8814_shared_priv *shared = phydev->shared->priv;
2339ece19502SDivya Koppera 
2340ece19502SDivya Koppera 	info->so_timestamping = SOF_TIMESTAMPING_TX_HARDWARE |
2341ece19502SDivya Koppera 				SOF_TIMESTAMPING_RX_HARDWARE |
2342ece19502SDivya Koppera 				SOF_TIMESTAMPING_RAW_HARDWARE;
2343ece19502SDivya Koppera 
2344ece19502SDivya Koppera 	info->phc_index = ptp_clock_index(shared->ptp_clock);
2345ece19502SDivya Koppera 
2346ece19502SDivya Koppera 	info->tx_types =
2347ece19502SDivya Koppera 		(1 << HWTSTAMP_TX_OFF) |
2348ece19502SDivya Koppera 		(1 << HWTSTAMP_TX_ON) |
2349ece19502SDivya Koppera 		(1 << HWTSTAMP_TX_ONESTEP_SYNC);
2350ece19502SDivya Koppera 
2351ece19502SDivya Koppera 	info->rx_filters =
2352ece19502SDivya Koppera 		(1 << HWTSTAMP_FILTER_NONE) |
2353ece19502SDivya Koppera 		(1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT) |
2354ece19502SDivya Koppera 		(1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT) |
2355ece19502SDivya Koppera 		(1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
2356ece19502SDivya Koppera 		(1 << HWTSTAMP_FILTER_PTP_V2_EVENT);
23577467d716SHoratiu Vultur 
23587467d716SHoratiu Vultur 	return 0;
23597467d716SHoratiu Vultur }
23607467d716SHoratiu Vultur 
2361ece19502SDivya Koppera static void lan8814_flush_fifo(struct phy_device *phydev, bool egress)
2362ece19502SDivya Koppera {
2363ece19502SDivya Koppera 	int i;
2364ece19502SDivya Koppera 
2365ece19502SDivya Koppera 	for (i = 0; i < FIFO_SIZE; ++i)
2366ece19502SDivya Koppera 		lanphy_read_page_reg(phydev, 5,
2367ece19502SDivya Koppera 				     egress ? PTP_TX_MSG_HEADER2 : PTP_RX_MSG_HEADER2);
2368ece19502SDivya Koppera 
2369ece19502SDivya Koppera 	/* Read to clear overflow status bit */
2370ece19502SDivya Koppera 	lanphy_read_page_reg(phydev, 5, PTP_TSU_INT_STS);
2371ece19502SDivya Koppera }
2372ece19502SDivya Koppera 
2373ece19502SDivya Koppera static int lan8814_hwtstamp(struct mii_timestamper *mii_ts, struct ifreq *ifr)
2374ece19502SDivya Koppera {
2375ece19502SDivya Koppera 	struct kszphy_ptp_priv *ptp_priv =
2376ece19502SDivya Koppera 			  container_of(mii_ts, struct kszphy_ptp_priv, mii_ts);
2377ece19502SDivya Koppera 	struct phy_device *phydev = ptp_priv->phydev;
2378ece19502SDivya Koppera 	struct lan8814_shared_priv *shared = phydev->shared->priv;
2379ece19502SDivya Koppera 	struct lan8814_ptp_rx_ts *rx_ts, *tmp;
2380ece19502SDivya Koppera 	struct hwtstamp_config config;
2381ece19502SDivya Koppera 	int txcfg = 0, rxcfg = 0;
2382ece19502SDivya Koppera 	int pkt_ts_enable;
2383ece19502SDivya Koppera 
2384ece19502SDivya Koppera 	if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
2385ece19502SDivya Koppera 		return -EFAULT;
2386ece19502SDivya Koppera 
2387ece19502SDivya Koppera 	ptp_priv->hwts_tx_type = config.tx_type;
2388ece19502SDivya Koppera 	ptp_priv->rx_filter = config.rx_filter;
2389ece19502SDivya Koppera 
2390ece19502SDivya Koppera 	switch (config.rx_filter) {
2391ece19502SDivya Koppera 	case HWTSTAMP_FILTER_NONE:
2392ece19502SDivya Koppera 		ptp_priv->layer = 0;
2393ece19502SDivya Koppera 		ptp_priv->version = 0;
2394ece19502SDivya Koppera 		break;
2395ece19502SDivya Koppera 	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
2396ece19502SDivya Koppera 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
2397ece19502SDivya Koppera 	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
2398ece19502SDivya Koppera 		ptp_priv->layer = PTP_CLASS_L4;
2399ece19502SDivya Koppera 		ptp_priv->version = PTP_CLASS_V2;
2400ece19502SDivya Koppera 		break;
2401ece19502SDivya Koppera 	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
2402ece19502SDivya Koppera 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
2403ece19502SDivya Koppera 	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
2404ece19502SDivya Koppera 		ptp_priv->layer = PTP_CLASS_L2;
2405ece19502SDivya Koppera 		ptp_priv->version = PTP_CLASS_V2;
2406ece19502SDivya Koppera 		break;
2407ece19502SDivya Koppera 	case HWTSTAMP_FILTER_PTP_V2_EVENT:
2408ece19502SDivya Koppera 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
2409ece19502SDivya Koppera 	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
2410ece19502SDivya Koppera 		ptp_priv->layer = PTP_CLASS_L4 | PTP_CLASS_L2;
2411ece19502SDivya Koppera 		ptp_priv->version = PTP_CLASS_V2;
2412ece19502SDivya Koppera 		break;
2413ece19502SDivya Koppera 	default:
2414ece19502SDivya Koppera 		return -ERANGE;
2415ece19502SDivya Koppera 	}
2416ece19502SDivya Koppera 
2417ece19502SDivya Koppera 	if (ptp_priv->layer & PTP_CLASS_L2) {
2418ece19502SDivya Koppera 		rxcfg = PTP_RX_PARSE_CONFIG_LAYER2_EN_;
2419ece19502SDivya Koppera 		txcfg = PTP_TX_PARSE_CONFIG_LAYER2_EN_;
2420ece19502SDivya Koppera 	} else if (ptp_priv->layer & PTP_CLASS_L4) {
2421ece19502SDivya Koppera 		rxcfg |= PTP_RX_PARSE_CONFIG_IPV4_EN_ | PTP_RX_PARSE_CONFIG_IPV6_EN_;
2422ece19502SDivya Koppera 		txcfg |= PTP_TX_PARSE_CONFIG_IPV4_EN_ | PTP_TX_PARSE_CONFIG_IPV6_EN_;
2423ece19502SDivya Koppera 	}
2424ece19502SDivya Koppera 	lanphy_write_page_reg(ptp_priv->phydev, 5, PTP_RX_PARSE_CONFIG, rxcfg);
2425ece19502SDivya Koppera 	lanphy_write_page_reg(ptp_priv->phydev, 5, PTP_TX_PARSE_CONFIG, txcfg);
2426ece19502SDivya Koppera 
2427ece19502SDivya Koppera 	pkt_ts_enable = PTP_TIMESTAMP_EN_SYNC_ | PTP_TIMESTAMP_EN_DREQ_ |
2428ece19502SDivya Koppera 			PTP_TIMESTAMP_EN_PDREQ_ | PTP_TIMESTAMP_EN_PDRES_;
2429ece19502SDivya Koppera 	lanphy_write_page_reg(ptp_priv->phydev, 5, PTP_RX_TIMESTAMP_EN, pkt_ts_enable);
2430ece19502SDivya Koppera 	lanphy_write_page_reg(ptp_priv->phydev, 5, PTP_TX_TIMESTAMP_EN, pkt_ts_enable);
2431ece19502SDivya Koppera 
2432ece19502SDivya Koppera 	if (ptp_priv->hwts_tx_type == HWTSTAMP_TX_ONESTEP_SYNC)
2433ece19502SDivya Koppera 		lanphy_write_page_reg(ptp_priv->phydev, 5, PTP_TX_MOD,
2434ece19502SDivya Koppera 				      PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_);
2435ece19502SDivya Koppera 
2436ece19502SDivya Koppera 	if (config.rx_filter != HWTSTAMP_FILTER_NONE)
2437ece19502SDivya Koppera 		lan8814_config_ts_intr(ptp_priv->phydev, true);
2438ece19502SDivya Koppera 	else
2439ece19502SDivya Koppera 		lan8814_config_ts_intr(ptp_priv->phydev, false);
2440ece19502SDivya Koppera 
2441ece19502SDivya Koppera 	mutex_lock(&shared->shared_lock);
2442ece19502SDivya Koppera 	if (config.rx_filter != HWTSTAMP_FILTER_NONE)
2443ece19502SDivya Koppera 		shared->ref++;
2444ece19502SDivya Koppera 	else
2445ece19502SDivya Koppera 		shared->ref--;
2446ece19502SDivya Koppera 
2447ece19502SDivya Koppera 	if (shared->ref)
2448ece19502SDivya Koppera 		lanphy_write_page_reg(ptp_priv->phydev, 4, PTP_CMD_CTL,
2449ece19502SDivya Koppera 				      PTP_CMD_CTL_PTP_ENABLE_);
2450ece19502SDivya Koppera 	else
2451ece19502SDivya Koppera 		lanphy_write_page_reg(ptp_priv->phydev, 4, PTP_CMD_CTL,
2452ece19502SDivya Koppera 				      PTP_CMD_CTL_PTP_DISABLE_);
2453ece19502SDivya Koppera 	mutex_unlock(&shared->shared_lock);
2454ece19502SDivya Koppera 
2455ece19502SDivya Koppera 	/* In case of multiple starts and stops, these needs to be cleared */
2456ece19502SDivya Koppera 	list_for_each_entry_safe(rx_ts, tmp, &ptp_priv->rx_ts_list, list) {
2457ece19502SDivya Koppera 		list_del(&rx_ts->list);
2458ece19502SDivya Koppera 		kfree(rx_ts);
2459ece19502SDivya Koppera 	}
2460ece19502SDivya Koppera 	skb_queue_purge(&ptp_priv->rx_queue);
2461ece19502SDivya Koppera 	skb_queue_purge(&ptp_priv->tx_queue);
2462ece19502SDivya Koppera 
2463ece19502SDivya Koppera 	lan8814_flush_fifo(ptp_priv->phydev, false);
2464ece19502SDivya Koppera 	lan8814_flush_fifo(ptp_priv->phydev, true);
2465ece19502SDivya Koppera 
2466ece19502SDivya Koppera 	return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ? -EFAULT : 0;
2467ece19502SDivya Koppera }
2468ece19502SDivya Koppera 
2469ece19502SDivya Koppera static void lan8814_txtstamp(struct mii_timestamper *mii_ts,
2470ece19502SDivya Koppera 			     struct sk_buff *skb, int type)
2471ece19502SDivya Koppera {
2472ece19502SDivya Koppera 	struct kszphy_ptp_priv *ptp_priv = container_of(mii_ts, struct kszphy_ptp_priv, mii_ts);
2473ece19502SDivya Koppera 
2474ece19502SDivya Koppera 	switch (ptp_priv->hwts_tx_type) {
2475ece19502SDivya Koppera 	case HWTSTAMP_TX_ONESTEP_SYNC:
24763914a9c0SKurt Kanzenbach 		if (ptp_msg_is_sync(skb, type)) {
2477ece19502SDivya Koppera 			kfree_skb(skb);
2478ece19502SDivya Koppera 			return;
2479ece19502SDivya Koppera 		}
2480ece19502SDivya Koppera 		fallthrough;
2481ece19502SDivya Koppera 	case HWTSTAMP_TX_ON:
2482ece19502SDivya Koppera 		skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
2483ece19502SDivya Koppera 		skb_queue_tail(&ptp_priv->tx_queue, skb);
2484ece19502SDivya Koppera 		break;
2485ece19502SDivya Koppera 	case HWTSTAMP_TX_OFF:
2486ece19502SDivya Koppera 	default:
2487ece19502SDivya Koppera 		kfree_skb(skb);
2488ece19502SDivya Koppera 		break;
2489ece19502SDivya Koppera 	}
2490ece19502SDivya Koppera }
2491ece19502SDivya Koppera 
2492ece19502SDivya Koppera static void lan8814_get_sig_rx(struct sk_buff *skb, u16 *sig)
2493ece19502SDivya Koppera {
2494ece19502SDivya Koppera 	struct ptp_header *ptp_header;
2495ece19502SDivya Koppera 	u32 type;
2496ece19502SDivya Koppera 
2497ece19502SDivya Koppera 	skb_push(skb, ETH_HLEN);
2498ece19502SDivya Koppera 	type = ptp_classify_raw(skb);
2499ece19502SDivya Koppera 	ptp_header = ptp_parse_header(skb, type);
2500ece19502SDivya Koppera 	skb_pull_inline(skb, ETH_HLEN);
2501ece19502SDivya Koppera 
2502ece19502SDivya Koppera 	*sig = (__force u16)(ntohs(ptp_header->sequence_id));
2503ece19502SDivya Koppera }
2504ece19502SDivya Koppera 
2505cafc3662SHoratiu Vultur static bool lan8814_match_rx_skb(struct kszphy_ptp_priv *ptp_priv,
2506ece19502SDivya Koppera 				 struct sk_buff *skb)
2507ece19502SDivya Koppera {
2508ece19502SDivya Koppera 	struct skb_shared_hwtstamps *shhwtstamps;
2509ece19502SDivya Koppera 	struct lan8814_ptp_rx_ts *rx_ts, *tmp;
2510ece19502SDivya Koppera 	unsigned long flags;
2511ece19502SDivya Koppera 	bool ret = false;
2512ece19502SDivya Koppera 	u16 skb_sig;
2513ece19502SDivya Koppera 
2514ece19502SDivya Koppera 	lan8814_get_sig_rx(skb, &skb_sig);
2515ece19502SDivya Koppera 
2516ece19502SDivya Koppera 	/* Iterate over all RX timestamps and match it with the received skbs */
2517ece19502SDivya Koppera 	spin_lock_irqsave(&ptp_priv->rx_ts_lock, flags);
2518ece19502SDivya Koppera 	list_for_each_entry_safe(rx_ts, tmp, &ptp_priv->rx_ts_list, list) {
2519ece19502SDivya Koppera 		/* Check if we found the signature we were looking for. */
2520ece19502SDivya Koppera 		if (memcmp(&skb_sig, &rx_ts->seq_id, sizeof(rx_ts->seq_id)))
2521ece19502SDivya Koppera 			continue;
2522ece19502SDivya Koppera 
2523ece19502SDivya Koppera 		shhwtstamps = skb_hwtstamps(skb);
2524ece19502SDivya Koppera 		memset(shhwtstamps, 0, sizeof(*shhwtstamps));
2525ece19502SDivya Koppera 		shhwtstamps->hwtstamp = ktime_set(rx_ts->seconds,
2526ece19502SDivya Koppera 						  rx_ts->nsec);
2527ece19502SDivya Koppera 		list_del(&rx_ts->list);
2528ece19502SDivya Koppera 		kfree(rx_ts);
2529ece19502SDivya Koppera 
2530ece19502SDivya Koppera 		ret = true;
2531ece19502SDivya Koppera 		break;
2532ece19502SDivya Koppera 	}
2533ece19502SDivya Koppera 	spin_unlock_irqrestore(&ptp_priv->rx_ts_lock, flags);
2534ece19502SDivya Koppera 
253567dbd6c0SSebastian Andrzej Siewior 	if (ret)
253667dbd6c0SSebastian Andrzej Siewior 		netif_rx(skb);
2537ece19502SDivya Koppera 	return ret;
2538ece19502SDivya Koppera }
2539ece19502SDivya Koppera 
2540ece19502SDivya Koppera static bool lan8814_rxtstamp(struct mii_timestamper *mii_ts, struct sk_buff *skb, int type)
2541ece19502SDivya Koppera {
2542ece19502SDivya Koppera 	struct kszphy_ptp_priv *ptp_priv =
2543ece19502SDivya Koppera 			container_of(mii_ts, struct kszphy_ptp_priv, mii_ts);
2544ece19502SDivya Koppera 
2545ece19502SDivya Koppera 	if (ptp_priv->rx_filter == HWTSTAMP_FILTER_NONE ||
2546ece19502SDivya Koppera 	    type == PTP_CLASS_NONE)
2547ece19502SDivya Koppera 		return false;
2548ece19502SDivya Koppera 
2549ece19502SDivya Koppera 	if ((type & ptp_priv->version) == 0 || (type & ptp_priv->layer) == 0)
2550ece19502SDivya Koppera 		return false;
2551ece19502SDivya Koppera 
2552ece19502SDivya Koppera 	/* If we failed to match then add it to the queue for when the timestamp
2553ece19502SDivya Koppera 	 * will come
2554ece19502SDivya Koppera 	 */
2555cafc3662SHoratiu Vultur 	if (!lan8814_match_rx_skb(ptp_priv, skb))
2556ece19502SDivya Koppera 		skb_queue_tail(&ptp_priv->rx_queue, skb);
2557ece19502SDivya Koppera 
2558ece19502SDivya Koppera 	return true;
2559ece19502SDivya Koppera }
2560ece19502SDivya Koppera 
2561ece19502SDivya Koppera static void lan8814_ptp_clock_set(struct phy_device *phydev,
2562ece19502SDivya Koppera 				  u32 seconds, u32 nano_seconds)
2563ece19502SDivya Koppera {
2564ece19502SDivya Koppera 	u32 sec_low, sec_high, nsec_low, nsec_high;
2565ece19502SDivya Koppera 
2566ece19502SDivya Koppera 	sec_low = seconds & 0xffff;
2567ece19502SDivya Koppera 	sec_high = (seconds >> 16) & 0xffff;
2568ece19502SDivya Koppera 	nsec_low = nano_seconds & 0xffff;
2569ece19502SDivya Koppera 	nsec_high = (nano_seconds >> 16) & 0x3fff;
2570ece19502SDivya Koppera 
2571ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 4, PTP_CLOCK_SET_SEC_LO, sec_low);
2572ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 4, PTP_CLOCK_SET_SEC_MID, sec_high);
2573ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 4, PTP_CLOCK_SET_NS_LO, nsec_low);
2574ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 4, PTP_CLOCK_SET_NS_HI, nsec_high);
2575ece19502SDivya Koppera 
2576ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 4, PTP_CMD_CTL, PTP_CMD_CTL_PTP_CLOCK_LOAD_);
2577ece19502SDivya Koppera }
2578ece19502SDivya Koppera 
2579ece19502SDivya Koppera static void lan8814_ptp_clock_get(struct phy_device *phydev,
2580ece19502SDivya Koppera 				  u32 *seconds, u32 *nano_seconds)
2581ece19502SDivya Koppera {
2582ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 4, PTP_CMD_CTL, PTP_CMD_CTL_PTP_CLOCK_READ_);
2583ece19502SDivya Koppera 
2584ece19502SDivya Koppera 	*seconds = lanphy_read_page_reg(phydev, 4, PTP_CLOCK_READ_SEC_MID);
2585ece19502SDivya Koppera 	*seconds = (*seconds << 16) |
2586ece19502SDivya Koppera 		   lanphy_read_page_reg(phydev, 4, PTP_CLOCK_READ_SEC_LO);
2587ece19502SDivya Koppera 
2588ece19502SDivya Koppera 	*nano_seconds = lanphy_read_page_reg(phydev, 4, PTP_CLOCK_READ_NS_HI);
2589ece19502SDivya Koppera 	*nano_seconds = ((*nano_seconds & 0x3fff) << 16) |
2590ece19502SDivya Koppera 			lanphy_read_page_reg(phydev, 4, PTP_CLOCK_READ_NS_LO);
2591ece19502SDivya Koppera }
2592ece19502SDivya Koppera 
2593ece19502SDivya Koppera static int lan8814_ptpci_gettime64(struct ptp_clock_info *ptpci,
2594ece19502SDivya Koppera 				   struct timespec64 *ts)
2595ece19502SDivya Koppera {
2596ece19502SDivya Koppera 	struct lan8814_shared_priv *shared = container_of(ptpci, struct lan8814_shared_priv,
2597ece19502SDivya Koppera 							  ptp_clock_info);
2598ece19502SDivya Koppera 	struct phy_device *phydev = shared->phydev;
2599ece19502SDivya Koppera 	u32 nano_seconds;
2600ece19502SDivya Koppera 	u32 seconds;
2601ece19502SDivya Koppera 
2602ece19502SDivya Koppera 	mutex_lock(&shared->shared_lock);
2603ece19502SDivya Koppera 	lan8814_ptp_clock_get(phydev, &seconds, &nano_seconds);
2604ece19502SDivya Koppera 	mutex_unlock(&shared->shared_lock);
2605ece19502SDivya Koppera 	ts->tv_sec = seconds;
2606ece19502SDivya Koppera 	ts->tv_nsec = nano_seconds;
2607ece19502SDivya Koppera 
2608ece19502SDivya Koppera 	return 0;
2609ece19502SDivya Koppera }
2610ece19502SDivya Koppera 
2611ece19502SDivya Koppera static int lan8814_ptpci_settime64(struct ptp_clock_info *ptpci,
2612ece19502SDivya Koppera 				   const struct timespec64 *ts)
2613ece19502SDivya Koppera {
2614ece19502SDivya Koppera 	struct lan8814_shared_priv *shared = container_of(ptpci, struct lan8814_shared_priv,
2615ece19502SDivya Koppera 							  ptp_clock_info);
2616ece19502SDivya Koppera 	struct phy_device *phydev = shared->phydev;
2617ece19502SDivya Koppera 
2618ece19502SDivya Koppera 	mutex_lock(&shared->shared_lock);
2619ece19502SDivya Koppera 	lan8814_ptp_clock_set(phydev, ts->tv_sec, ts->tv_nsec);
2620ece19502SDivya Koppera 	mutex_unlock(&shared->shared_lock);
2621ece19502SDivya Koppera 
2622ece19502SDivya Koppera 	return 0;
2623ece19502SDivya Koppera }
2624ece19502SDivya Koppera 
2625ece19502SDivya Koppera static void lan8814_ptp_clock_step(struct phy_device *phydev,
2626ece19502SDivya Koppera 				   s64 time_step_ns)
2627ece19502SDivya Koppera {
2628ece19502SDivya Koppera 	u32 nano_seconds_step;
2629ece19502SDivya Koppera 	u64 abs_time_step_ns;
2630ece19502SDivya Koppera 	u32 unsigned_seconds;
2631ece19502SDivya Koppera 	u32 nano_seconds;
2632ece19502SDivya Koppera 	u32 remainder;
2633ece19502SDivya Koppera 	s32 seconds;
2634ece19502SDivya Koppera 
2635ece19502SDivya Koppera 	if (time_step_ns >  15000000000LL) {
2636ece19502SDivya Koppera 		/* convert to clock set */
2637ece19502SDivya Koppera 		lan8814_ptp_clock_get(phydev, &unsigned_seconds, &nano_seconds);
2638ece19502SDivya Koppera 		unsigned_seconds += div_u64_rem(time_step_ns, 1000000000LL,
2639ece19502SDivya Koppera 						&remainder);
2640ece19502SDivya Koppera 		nano_seconds += remainder;
2641ece19502SDivya Koppera 		if (nano_seconds >= 1000000000) {
2642ece19502SDivya Koppera 			unsigned_seconds++;
2643ece19502SDivya Koppera 			nano_seconds -= 1000000000;
2644ece19502SDivya Koppera 		}
2645ece19502SDivya Koppera 		lan8814_ptp_clock_set(phydev, unsigned_seconds, nano_seconds);
2646ece19502SDivya Koppera 		return;
2647ece19502SDivya Koppera 	} else if (time_step_ns < -15000000000LL) {
2648ece19502SDivya Koppera 		/* convert to clock set */
2649ece19502SDivya Koppera 		time_step_ns = -time_step_ns;
2650ece19502SDivya Koppera 
2651ece19502SDivya Koppera 		lan8814_ptp_clock_get(phydev, &unsigned_seconds, &nano_seconds);
2652ece19502SDivya Koppera 		unsigned_seconds -= div_u64_rem(time_step_ns, 1000000000LL,
2653ece19502SDivya Koppera 						&remainder);
2654ece19502SDivya Koppera 		nano_seconds_step = remainder;
2655ece19502SDivya Koppera 		if (nano_seconds < nano_seconds_step) {
2656ece19502SDivya Koppera 			unsigned_seconds--;
2657ece19502SDivya Koppera 			nano_seconds += 1000000000;
2658ece19502SDivya Koppera 		}
2659ece19502SDivya Koppera 		nano_seconds -= nano_seconds_step;
2660ece19502SDivya Koppera 		lan8814_ptp_clock_set(phydev, unsigned_seconds,
2661ece19502SDivya Koppera 				      nano_seconds);
2662ece19502SDivya Koppera 		return;
2663ece19502SDivya Koppera 	}
2664ece19502SDivya Koppera 
2665ece19502SDivya Koppera 	/* do clock step */
2666ece19502SDivya Koppera 	if (time_step_ns >= 0) {
2667ece19502SDivya Koppera 		abs_time_step_ns = (u64)time_step_ns;
2668ece19502SDivya Koppera 		seconds = (s32)div_u64_rem(abs_time_step_ns, 1000000000,
2669ece19502SDivya Koppera 					   &remainder);
2670ece19502SDivya Koppera 		nano_seconds = remainder;
2671ece19502SDivya Koppera 	} else {
2672ece19502SDivya Koppera 		abs_time_step_ns = (u64)(-time_step_ns);
2673ece19502SDivya Koppera 		seconds = -((s32)div_u64_rem(abs_time_step_ns, 1000000000,
2674ece19502SDivya Koppera 			    &remainder));
2675ece19502SDivya Koppera 		nano_seconds = remainder;
2676ece19502SDivya Koppera 		if (nano_seconds > 0) {
2677ece19502SDivya Koppera 			/* subtracting nano seconds is not allowed
2678ece19502SDivya Koppera 			 * convert to subtracting from seconds,
2679ece19502SDivya Koppera 			 * and adding to nanoseconds
2680ece19502SDivya Koppera 			 */
2681ece19502SDivya Koppera 			seconds--;
2682ece19502SDivya Koppera 			nano_seconds = (1000000000 - nano_seconds);
2683ece19502SDivya Koppera 		}
2684ece19502SDivya Koppera 	}
2685ece19502SDivya Koppera 
2686ece19502SDivya Koppera 	if (nano_seconds > 0) {
2687ece19502SDivya Koppera 		/* add 8 ns to cover the likely normal increment */
2688ece19502SDivya Koppera 		nano_seconds += 8;
2689ece19502SDivya Koppera 	}
2690ece19502SDivya Koppera 
2691ece19502SDivya Koppera 	if (nano_seconds >= 1000000000) {
2692ece19502SDivya Koppera 		/* carry into seconds */
2693ece19502SDivya Koppera 		seconds++;
2694ece19502SDivya Koppera 		nano_seconds -= 1000000000;
2695ece19502SDivya Koppera 	}
2696ece19502SDivya Koppera 
2697ece19502SDivya Koppera 	while (seconds) {
2698ece19502SDivya Koppera 		if (seconds > 0) {
2699ece19502SDivya Koppera 			u32 adjustment_value = (u32)seconds;
2700ece19502SDivya Koppera 			u16 adjustment_value_lo, adjustment_value_hi;
2701ece19502SDivya Koppera 
2702ece19502SDivya Koppera 			if (adjustment_value > 0xF)
2703ece19502SDivya Koppera 				adjustment_value = 0xF;
2704ece19502SDivya Koppera 
2705ece19502SDivya Koppera 			adjustment_value_lo = adjustment_value & 0xffff;
2706ece19502SDivya Koppera 			adjustment_value_hi = (adjustment_value >> 16) & 0x3fff;
2707ece19502SDivya Koppera 
2708ece19502SDivya Koppera 			lanphy_write_page_reg(phydev, 4, PTP_LTC_STEP_ADJ_LO,
2709ece19502SDivya Koppera 					      adjustment_value_lo);
2710ece19502SDivya Koppera 			lanphy_write_page_reg(phydev, 4, PTP_LTC_STEP_ADJ_HI,
2711ece19502SDivya Koppera 					      PTP_LTC_STEP_ADJ_DIR_ |
2712ece19502SDivya Koppera 					      adjustment_value_hi);
2713ece19502SDivya Koppera 			seconds -= ((s32)adjustment_value);
2714ece19502SDivya Koppera 		} else {
2715ece19502SDivya Koppera 			u32 adjustment_value = (u32)(-seconds);
2716ece19502SDivya Koppera 			u16 adjustment_value_lo, adjustment_value_hi;
2717ece19502SDivya Koppera 
2718ece19502SDivya Koppera 			if (adjustment_value > 0xF)
2719ece19502SDivya Koppera 				adjustment_value = 0xF;
2720ece19502SDivya Koppera 
2721ece19502SDivya Koppera 			adjustment_value_lo = adjustment_value & 0xffff;
2722ece19502SDivya Koppera 			adjustment_value_hi = (adjustment_value >> 16) & 0x3fff;
2723ece19502SDivya Koppera 
2724ece19502SDivya Koppera 			lanphy_write_page_reg(phydev, 4, PTP_LTC_STEP_ADJ_LO,
2725ece19502SDivya Koppera 					      adjustment_value_lo);
2726ece19502SDivya Koppera 			lanphy_write_page_reg(phydev, 4, PTP_LTC_STEP_ADJ_HI,
2727ece19502SDivya Koppera 					      adjustment_value_hi);
2728ece19502SDivya Koppera 			seconds += ((s32)adjustment_value);
2729ece19502SDivya Koppera 		}
2730ece19502SDivya Koppera 		lanphy_write_page_reg(phydev, 4, PTP_CMD_CTL,
2731ece19502SDivya Koppera 				      PTP_CMD_CTL_PTP_LTC_STEP_SEC_);
2732ece19502SDivya Koppera 	}
2733ece19502SDivya Koppera 	if (nano_seconds) {
2734ece19502SDivya Koppera 		u16 nano_seconds_lo;
2735ece19502SDivya Koppera 		u16 nano_seconds_hi;
2736ece19502SDivya Koppera 
2737ece19502SDivya Koppera 		nano_seconds_lo = nano_seconds & 0xffff;
2738ece19502SDivya Koppera 		nano_seconds_hi = (nano_seconds >> 16) & 0x3fff;
2739ece19502SDivya Koppera 
2740ece19502SDivya Koppera 		lanphy_write_page_reg(phydev, 4, PTP_LTC_STEP_ADJ_LO,
2741ece19502SDivya Koppera 				      nano_seconds_lo);
2742ece19502SDivya Koppera 		lanphy_write_page_reg(phydev, 4, PTP_LTC_STEP_ADJ_HI,
2743ece19502SDivya Koppera 				      PTP_LTC_STEP_ADJ_DIR_ |
2744ece19502SDivya Koppera 				      nano_seconds_hi);
2745ece19502SDivya Koppera 		lanphy_write_page_reg(phydev, 4, PTP_CMD_CTL,
2746ece19502SDivya Koppera 				      PTP_CMD_CTL_PTP_LTC_STEP_NSEC_);
2747ece19502SDivya Koppera 	}
2748ece19502SDivya Koppera }
2749ece19502SDivya Koppera 
2750ece19502SDivya Koppera static int lan8814_ptpci_adjtime(struct ptp_clock_info *ptpci, s64 delta)
2751ece19502SDivya Koppera {
2752ece19502SDivya Koppera 	struct lan8814_shared_priv *shared = container_of(ptpci, struct lan8814_shared_priv,
2753ece19502SDivya Koppera 							  ptp_clock_info);
2754ece19502SDivya Koppera 	struct phy_device *phydev = shared->phydev;
2755ece19502SDivya Koppera 
2756ece19502SDivya Koppera 	mutex_lock(&shared->shared_lock);
2757ece19502SDivya Koppera 	lan8814_ptp_clock_step(phydev, delta);
2758ece19502SDivya Koppera 	mutex_unlock(&shared->shared_lock);
2759ece19502SDivya Koppera 
2760ece19502SDivya Koppera 	return 0;
2761ece19502SDivya Koppera }
2762ece19502SDivya Koppera 
2763ece19502SDivya Koppera static int lan8814_ptpci_adjfine(struct ptp_clock_info *ptpci, long scaled_ppm)
2764ece19502SDivya Koppera {
2765ece19502SDivya Koppera 	struct lan8814_shared_priv *shared = container_of(ptpci, struct lan8814_shared_priv,
2766ece19502SDivya Koppera 							  ptp_clock_info);
2767ece19502SDivya Koppera 	struct phy_device *phydev = shared->phydev;
2768ece19502SDivya Koppera 	u16 kszphy_rate_adj_lo, kszphy_rate_adj_hi;
2769ece19502SDivya Koppera 	bool positive = true;
2770ece19502SDivya Koppera 	u32 kszphy_rate_adj;
2771ece19502SDivya Koppera 
2772ece19502SDivya Koppera 	if (scaled_ppm < 0) {
2773ece19502SDivya Koppera 		scaled_ppm = -scaled_ppm;
2774ece19502SDivya Koppera 		positive = false;
2775ece19502SDivya Koppera 	}
2776ece19502SDivya Koppera 
2777ece19502SDivya Koppera 	kszphy_rate_adj = LAN8814_1PPM_FORMAT * (scaled_ppm >> 16);
2778ece19502SDivya Koppera 	kszphy_rate_adj += (LAN8814_1PPM_FORMAT * (0xffff & scaled_ppm)) >> 16;
2779ece19502SDivya Koppera 
2780ece19502SDivya Koppera 	kszphy_rate_adj_lo = kszphy_rate_adj & 0xffff;
2781ece19502SDivya Koppera 	kszphy_rate_adj_hi = (kszphy_rate_adj >> 16) & 0x3fff;
2782ece19502SDivya Koppera 
2783ece19502SDivya Koppera 	if (positive)
2784ece19502SDivya Koppera 		kszphy_rate_adj_hi |= PTP_CLOCK_RATE_ADJ_DIR_;
2785ece19502SDivya Koppera 
2786ece19502SDivya Koppera 	mutex_lock(&shared->shared_lock);
2787ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 4, PTP_CLOCK_RATE_ADJ_HI, kszphy_rate_adj_hi);
2788ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 4, PTP_CLOCK_RATE_ADJ_LO, kszphy_rate_adj_lo);
2789ece19502SDivya Koppera 	mutex_unlock(&shared->shared_lock);
2790ece19502SDivya Koppera 
2791ece19502SDivya Koppera 	return 0;
2792ece19502SDivya Koppera }
2793ece19502SDivya Koppera 
2794ece19502SDivya Koppera static void lan8814_get_sig_tx(struct sk_buff *skb, u16 *sig)
2795ece19502SDivya Koppera {
2796ece19502SDivya Koppera 	struct ptp_header *ptp_header;
2797ece19502SDivya Koppera 	u32 type;
2798ece19502SDivya Koppera 
2799ece19502SDivya Koppera 	type = ptp_classify_raw(skb);
2800ece19502SDivya Koppera 	ptp_header = ptp_parse_header(skb, type);
2801ece19502SDivya Koppera 
2802ece19502SDivya Koppera 	*sig = (__force u16)(ntohs(ptp_header->sequence_id));
2803ece19502SDivya Koppera }
2804ece19502SDivya Koppera 
2805cafc3662SHoratiu Vultur static void lan8814_match_tx_skb(struct kszphy_ptp_priv *ptp_priv,
2806cafc3662SHoratiu Vultur 				 u32 seconds, u32 nsec, u16 seq_id)
2807ece19502SDivya Koppera {
2808ece19502SDivya Koppera 	struct skb_shared_hwtstamps shhwtstamps;
2809ece19502SDivya Koppera 	struct sk_buff *skb, *skb_tmp;
2810ece19502SDivya Koppera 	unsigned long flags;
2811ece19502SDivya Koppera 	bool ret = false;
2812ece19502SDivya Koppera 	u16 skb_sig;
2813ece19502SDivya Koppera 
2814ece19502SDivya Koppera 	spin_lock_irqsave(&ptp_priv->tx_queue.lock, flags);
2815ece19502SDivya Koppera 	skb_queue_walk_safe(&ptp_priv->tx_queue, skb, skb_tmp) {
2816ece19502SDivya Koppera 		lan8814_get_sig_tx(skb, &skb_sig);
2817ece19502SDivya Koppera 
2818ece19502SDivya Koppera 		if (memcmp(&skb_sig, &seq_id, sizeof(seq_id)))
2819ece19502SDivya Koppera 			continue;
2820ece19502SDivya Koppera 
2821ece19502SDivya Koppera 		__skb_unlink(skb, &ptp_priv->tx_queue);
2822ece19502SDivya Koppera 		ret = true;
2823ece19502SDivya Koppera 		break;
2824ece19502SDivya Koppera 	}
2825ece19502SDivya Koppera 	spin_unlock_irqrestore(&ptp_priv->tx_queue.lock, flags);
2826ece19502SDivya Koppera 
2827ece19502SDivya Koppera 	if (ret) {
2828ece19502SDivya Koppera 		memset(&shhwtstamps, 0, sizeof(shhwtstamps));
2829ece19502SDivya Koppera 		shhwtstamps.hwtstamp = ktime_set(seconds, nsec);
2830ece19502SDivya Koppera 		skb_complete_tx_timestamp(skb, &shhwtstamps);
2831ece19502SDivya Koppera 	}
2832ece19502SDivya Koppera }
2833ece19502SDivya Koppera 
2834cafc3662SHoratiu Vultur static void lan8814_dequeue_tx_skb(struct kszphy_ptp_priv *ptp_priv)
2835cafc3662SHoratiu Vultur {
2836cafc3662SHoratiu Vultur 	struct phy_device *phydev = ptp_priv->phydev;
2837cafc3662SHoratiu Vultur 	u32 seconds, nsec;
2838cafc3662SHoratiu Vultur 	u16 seq_id;
2839cafc3662SHoratiu Vultur 
2840cafc3662SHoratiu Vultur 	lan8814_ptp_tx_ts_get(phydev, &seconds, &nsec, &seq_id);
2841cafc3662SHoratiu Vultur 	lan8814_match_tx_skb(ptp_priv, seconds, nsec, seq_id);
2842cafc3662SHoratiu Vultur }
2843cafc3662SHoratiu Vultur 
2844ece19502SDivya Koppera static void lan8814_get_tx_ts(struct kszphy_ptp_priv *ptp_priv)
2845ece19502SDivya Koppera {
2846ece19502SDivya Koppera 	struct phy_device *phydev = ptp_priv->phydev;
2847ece19502SDivya Koppera 	u32 reg;
2848ece19502SDivya Koppera 
2849ece19502SDivya Koppera 	do {
2850ece19502SDivya Koppera 		lan8814_dequeue_tx_skb(ptp_priv);
2851ece19502SDivya Koppera 
2852ece19502SDivya Koppera 		/* If other timestamps are available in the FIFO,
2853ece19502SDivya Koppera 		 * process them.
2854ece19502SDivya Koppera 		 */
2855ece19502SDivya Koppera 		reg = lanphy_read_page_reg(phydev, 5, PTP_CAP_INFO);
2856ece19502SDivya Koppera 	} while (PTP_CAP_INFO_TX_TS_CNT_GET_(reg) > 0);
2857ece19502SDivya Koppera }
2858ece19502SDivya Koppera 
2859ece19502SDivya Koppera static bool lan8814_match_skb(struct kszphy_ptp_priv *ptp_priv,
2860ece19502SDivya Koppera 			      struct lan8814_ptp_rx_ts *rx_ts)
2861ece19502SDivya Koppera {
2862ece19502SDivya Koppera 	struct skb_shared_hwtstamps *shhwtstamps;
2863ece19502SDivya Koppera 	struct sk_buff *skb, *skb_tmp;
2864ece19502SDivya Koppera 	unsigned long flags;
2865ece19502SDivya Koppera 	bool ret = false;
2866ece19502SDivya Koppera 	u16 skb_sig;
2867ece19502SDivya Koppera 
2868ece19502SDivya Koppera 	spin_lock_irqsave(&ptp_priv->rx_queue.lock, flags);
2869ece19502SDivya Koppera 	skb_queue_walk_safe(&ptp_priv->rx_queue, skb, skb_tmp) {
2870ece19502SDivya Koppera 		lan8814_get_sig_rx(skb, &skb_sig);
2871ece19502SDivya Koppera 
2872ece19502SDivya Koppera 		if (memcmp(&skb_sig, &rx_ts->seq_id, sizeof(rx_ts->seq_id)))
2873ece19502SDivya Koppera 			continue;
2874ece19502SDivya Koppera 
2875ece19502SDivya Koppera 		__skb_unlink(skb, &ptp_priv->rx_queue);
2876ece19502SDivya Koppera 
2877ece19502SDivya Koppera 		ret = true;
2878ece19502SDivya Koppera 		break;
2879ece19502SDivya Koppera 	}
2880ece19502SDivya Koppera 	spin_unlock_irqrestore(&ptp_priv->rx_queue.lock, flags);
2881ece19502SDivya Koppera 
2882ece19502SDivya Koppera 	if (ret) {
2883ece19502SDivya Koppera 		shhwtstamps = skb_hwtstamps(skb);
2884ece19502SDivya Koppera 		memset(shhwtstamps, 0, sizeof(*shhwtstamps));
2885ece19502SDivya Koppera 		shhwtstamps->hwtstamp = ktime_set(rx_ts->seconds, rx_ts->nsec);
2886e1f9e434SSebastian Andrzej Siewior 		netif_rx(skb);
2887ece19502SDivya Koppera 	}
2888ece19502SDivya Koppera 
2889ece19502SDivya Koppera 	return ret;
2890ece19502SDivya Koppera }
2891ece19502SDivya Koppera 
2892cafc3662SHoratiu Vultur static void lan8814_match_rx_ts(struct kszphy_ptp_priv *ptp_priv,
2893cafc3662SHoratiu Vultur 				struct lan8814_ptp_rx_ts *rx_ts)
2894ece19502SDivya Koppera {
2895ece19502SDivya Koppera 	unsigned long flags;
2896ece19502SDivya Koppera 
2897ece19502SDivya Koppera 	/* If we failed to match the skb add it to the queue for when
2898ece19502SDivya Koppera 	 * the frame will come
2899ece19502SDivya Koppera 	 */
2900ece19502SDivya Koppera 	if (!lan8814_match_skb(ptp_priv, rx_ts)) {
2901ece19502SDivya Koppera 		spin_lock_irqsave(&ptp_priv->rx_ts_lock, flags);
2902ece19502SDivya Koppera 		list_add(&rx_ts->list, &ptp_priv->rx_ts_list);
2903ece19502SDivya Koppera 		spin_unlock_irqrestore(&ptp_priv->rx_ts_lock, flags);
2904ece19502SDivya Koppera 	} else {
2905ece19502SDivya Koppera 		kfree(rx_ts);
2906ece19502SDivya Koppera 	}
2907cafc3662SHoratiu Vultur }
2908cafc3662SHoratiu Vultur 
2909cafc3662SHoratiu Vultur static void lan8814_get_rx_ts(struct kszphy_ptp_priv *ptp_priv)
2910cafc3662SHoratiu Vultur {
2911cafc3662SHoratiu Vultur 	struct phy_device *phydev = ptp_priv->phydev;
2912cafc3662SHoratiu Vultur 	struct lan8814_ptp_rx_ts *rx_ts;
2913cafc3662SHoratiu Vultur 	u32 reg;
2914cafc3662SHoratiu Vultur 
2915cafc3662SHoratiu Vultur 	do {
2916cafc3662SHoratiu Vultur 		rx_ts = kzalloc(sizeof(*rx_ts), GFP_KERNEL);
2917cafc3662SHoratiu Vultur 		if (!rx_ts)
2918cafc3662SHoratiu Vultur 			return;
2919cafc3662SHoratiu Vultur 
2920cafc3662SHoratiu Vultur 		lan8814_ptp_rx_ts_get(phydev, &rx_ts->seconds, &rx_ts->nsec,
2921cafc3662SHoratiu Vultur 				      &rx_ts->seq_id);
2922cafc3662SHoratiu Vultur 		lan8814_match_rx_ts(ptp_priv, rx_ts);
2923ece19502SDivya Koppera 
2924ece19502SDivya Koppera 		/* If other timestamps are available in the FIFO,
2925ece19502SDivya Koppera 		 * process them.
2926ece19502SDivya Koppera 		 */
2927ece19502SDivya Koppera 		reg = lanphy_read_page_reg(phydev, 5, PTP_CAP_INFO);
2928ece19502SDivya Koppera 	} while (PTP_CAP_INFO_RX_TS_CNT_GET_(reg) > 0);
2929ece19502SDivya Koppera }
2930ece19502SDivya Koppera 
29317abd92a5SHoratiu Vultur static void lan8814_handle_ptp_interrupt(struct phy_device *phydev, u16 status)
2932ece19502SDivya Koppera {
2933ece19502SDivya Koppera 	struct kszphy_priv *priv = phydev->priv;
2934ece19502SDivya Koppera 	struct kszphy_ptp_priv *ptp_priv = &priv->ptp_priv;
2935ece19502SDivya Koppera 
2936ece19502SDivya Koppera 	if (status & PTP_TSU_INT_STS_PTP_TX_TS_EN_)
2937ece19502SDivya Koppera 		lan8814_get_tx_ts(ptp_priv);
2938ece19502SDivya Koppera 
2939ece19502SDivya Koppera 	if (status & PTP_TSU_INT_STS_PTP_RX_TS_EN_)
2940ece19502SDivya Koppera 		lan8814_get_rx_ts(ptp_priv);
2941ece19502SDivya Koppera 
2942ece19502SDivya Koppera 	if (status & PTP_TSU_INT_STS_PTP_TX_TS_OVRFL_INT_) {
2943ece19502SDivya Koppera 		lan8814_flush_fifo(phydev, true);
2944ece19502SDivya Koppera 		skb_queue_purge(&ptp_priv->tx_queue);
2945ece19502SDivya Koppera 	}
2946ece19502SDivya Koppera 
2947ece19502SDivya Koppera 	if (status & PTP_TSU_INT_STS_PTP_RX_TS_OVRFL_INT_) {
2948ece19502SDivya Koppera 		lan8814_flush_fifo(phydev, false);
2949ece19502SDivya Koppera 		skb_queue_purge(&ptp_priv->rx_queue);
2950ece19502SDivya Koppera 	}
2951ece19502SDivya Koppera }
2952ece19502SDivya Koppera 
29537c2dcfa2SHoratiu Vultur static int lan8804_config_init(struct phy_device *phydev)
29547c2dcfa2SHoratiu Vultur {
29557c2dcfa2SHoratiu Vultur 	int val;
29567c2dcfa2SHoratiu Vultur 
29577c2dcfa2SHoratiu Vultur 	/* MDI-X setting for swap A,B transmit */
29587c2dcfa2SHoratiu Vultur 	val = lanphy_read_page_reg(phydev, 2, LAN8804_ALIGN_SWAP);
29597c2dcfa2SHoratiu Vultur 	val &= ~LAN8804_ALIGN_TX_A_B_SWAP_MASK;
29607c2dcfa2SHoratiu Vultur 	val |= LAN8804_ALIGN_TX_A_B_SWAP;
29617c2dcfa2SHoratiu Vultur 	lanphy_write_page_reg(phydev, 2, LAN8804_ALIGN_SWAP, val);
29627c2dcfa2SHoratiu Vultur 
29637c2dcfa2SHoratiu Vultur 	/* Make sure that the PHY will not stop generating the clock when the
29647c2dcfa2SHoratiu Vultur 	 * link partner goes down
29657c2dcfa2SHoratiu Vultur 	 */
29667c2dcfa2SHoratiu Vultur 	lanphy_write_page_reg(phydev, 31, LAN8814_CLOCK_MANAGEMENT, 0x27e);
29677c2dcfa2SHoratiu Vultur 	lanphy_read_page_reg(phydev, 1, LAN8814_LINK_QUALITY);
29687c2dcfa2SHoratiu Vultur 
29697c2dcfa2SHoratiu Vultur 	return 0;
29707c2dcfa2SHoratiu Vultur }
29717c2dcfa2SHoratiu Vultur 
2972b324c6e5SHoratiu Vultur static irqreturn_t lan8804_handle_interrupt(struct phy_device *phydev)
2973b324c6e5SHoratiu Vultur {
2974b324c6e5SHoratiu Vultur 	int status;
2975b324c6e5SHoratiu Vultur 
2976b324c6e5SHoratiu Vultur 	status = phy_read(phydev, LAN8814_INTS);
2977b324c6e5SHoratiu Vultur 	if (status < 0) {
2978b324c6e5SHoratiu Vultur 		phy_error(phydev);
2979b324c6e5SHoratiu Vultur 		return IRQ_NONE;
2980b324c6e5SHoratiu Vultur 	}
2981b324c6e5SHoratiu Vultur 
2982b324c6e5SHoratiu Vultur 	if (status > 0)
2983b324c6e5SHoratiu Vultur 		phy_trigger_machine(phydev);
2984b324c6e5SHoratiu Vultur 
2985b324c6e5SHoratiu Vultur 	return IRQ_HANDLED;
2986b324c6e5SHoratiu Vultur }
2987b324c6e5SHoratiu Vultur 
2988b324c6e5SHoratiu Vultur #define LAN8804_OUTPUT_CONTROL			25
2989b324c6e5SHoratiu Vultur #define LAN8804_OUTPUT_CONTROL_INTR_BUFFER	BIT(14)
2990b324c6e5SHoratiu Vultur #define LAN8804_CONTROL				31
2991b324c6e5SHoratiu Vultur #define LAN8804_CONTROL_INTR_POLARITY		BIT(14)
2992b324c6e5SHoratiu Vultur 
2993b324c6e5SHoratiu Vultur static int lan8804_config_intr(struct phy_device *phydev)
2994b324c6e5SHoratiu Vultur {
2995b324c6e5SHoratiu Vultur 	int err;
2996b324c6e5SHoratiu Vultur 
2997b324c6e5SHoratiu Vultur 	/* This is an internal PHY of lan966x and is not possible to change the
2998b324c6e5SHoratiu Vultur 	 * polarity on the GIC found in lan966x, therefore change the polarity
2999b324c6e5SHoratiu Vultur 	 * of the interrupt in the PHY from being active low instead of active
3000b324c6e5SHoratiu Vultur 	 * high.
3001b324c6e5SHoratiu Vultur 	 */
3002b324c6e5SHoratiu Vultur 	phy_write(phydev, LAN8804_CONTROL, LAN8804_CONTROL_INTR_POLARITY);
3003b324c6e5SHoratiu Vultur 
3004b324c6e5SHoratiu Vultur 	/* By default interrupt buffer is open-drain in which case the interrupt
3005b324c6e5SHoratiu Vultur 	 * can be active only low. Therefore change the interrupt buffer to be
3006b324c6e5SHoratiu Vultur 	 * push-pull to be able to change interrupt polarity
3007b324c6e5SHoratiu Vultur 	 */
3008b324c6e5SHoratiu Vultur 	phy_write(phydev, LAN8804_OUTPUT_CONTROL,
3009b324c6e5SHoratiu Vultur 		  LAN8804_OUTPUT_CONTROL_INTR_BUFFER);
3010b324c6e5SHoratiu Vultur 
3011b324c6e5SHoratiu Vultur 	if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
3012b324c6e5SHoratiu Vultur 		err = phy_read(phydev, LAN8814_INTS);
3013b324c6e5SHoratiu Vultur 		if (err < 0)
3014b324c6e5SHoratiu Vultur 			return err;
3015b324c6e5SHoratiu Vultur 
3016b324c6e5SHoratiu Vultur 		err = phy_write(phydev, LAN8814_INTC, LAN8814_INT_LINK);
3017b324c6e5SHoratiu Vultur 		if (err)
3018b324c6e5SHoratiu Vultur 			return err;
3019b324c6e5SHoratiu Vultur 	} else {
3020b324c6e5SHoratiu Vultur 		err = phy_write(phydev, LAN8814_INTC, 0);
3021b324c6e5SHoratiu Vultur 		if (err)
3022b324c6e5SHoratiu Vultur 			return err;
3023b324c6e5SHoratiu Vultur 
3024b324c6e5SHoratiu Vultur 		err = phy_read(phydev, LAN8814_INTS);
3025b324c6e5SHoratiu Vultur 		if (err < 0)
3026b324c6e5SHoratiu Vultur 			return err;
3027b324c6e5SHoratiu Vultur 	}
3028b324c6e5SHoratiu Vultur 
3029b324c6e5SHoratiu Vultur 	return 0;
3030b324c6e5SHoratiu Vultur }
3031b324c6e5SHoratiu Vultur 
3032b3ec7248SDivya Koppera static irqreturn_t lan8814_handle_interrupt(struct phy_device *phydev)
3033b3ec7248SDivya Koppera {
30342002fbacSMichael Walle 	int ret = IRQ_NONE;
30357abd92a5SHoratiu Vultur 	int irq_status;
3036b3ec7248SDivya Koppera 
3037b3ec7248SDivya Koppera 	irq_status = phy_read(phydev, LAN8814_INTS);
3038ece19502SDivya Koppera 	if (irq_status < 0) {
3039ece19502SDivya Koppera 		phy_error(phydev);
3040ece19502SDivya Koppera 		return IRQ_NONE;
3041ece19502SDivya Koppera 	}
3042ece19502SDivya Koppera 
30432002fbacSMichael Walle 	if (irq_status & LAN8814_INT_LINK) {
30442002fbacSMichael Walle 		phy_trigger_machine(phydev);
30452002fbacSMichael Walle 		ret = IRQ_HANDLED;
30462002fbacSMichael Walle 	}
30472002fbacSMichael Walle 
30487abd92a5SHoratiu Vultur 	while (true) {
30497abd92a5SHoratiu Vultur 		irq_status = lanphy_read_page_reg(phydev, 5, PTP_TSU_INT_STS);
30507abd92a5SHoratiu Vultur 		if (!irq_status)
3051ece19502SDivya Koppera 			break;
30527abd92a5SHoratiu Vultur 
30537abd92a5SHoratiu Vultur 		lan8814_handle_ptp_interrupt(phydev, irq_status);
30547abd92a5SHoratiu Vultur 		ret = IRQ_HANDLED;
30552002fbacSMichael Walle 	}
30562002fbacSMichael Walle 
30572002fbacSMichael Walle 	return ret;
3058b3ec7248SDivya Koppera }
3059b3ec7248SDivya Koppera 
3060b3ec7248SDivya Koppera static int lan8814_ack_interrupt(struct phy_device *phydev)
3061b3ec7248SDivya Koppera {
3062b3ec7248SDivya Koppera 	/* bit[12..0] int status, which is a read and clear register. */
3063b3ec7248SDivya Koppera 	int rc;
3064b3ec7248SDivya Koppera 
3065b3ec7248SDivya Koppera 	rc = phy_read(phydev, LAN8814_INTS);
3066b3ec7248SDivya Koppera 
3067b3ec7248SDivya Koppera 	return (rc < 0) ? rc : 0;
3068b3ec7248SDivya Koppera }
3069b3ec7248SDivya Koppera 
3070b3ec7248SDivya Koppera static int lan8814_config_intr(struct phy_device *phydev)
3071b3ec7248SDivya Koppera {
3072b3ec7248SDivya Koppera 	int err;
3073b3ec7248SDivya Koppera 
3074b3ec7248SDivya Koppera 	lanphy_write_page_reg(phydev, 4, LAN8814_INTR_CTRL_REG,
3075b3ec7248SDivya Koppera 			      LAN8814_INTR_CTRL_REG_POLARITY |
3076b3ec7248SDivya Koppera 			      LAN8814_INTR_CTRL_REG_INTR_ENABLE);
3077b3ec7248SDivya Koppera 
3078b3ec7248SDivya Koppera 	/* enable / disable interrupts */
3079b3ec7248SDivya Koppera 	if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
3080b3ec7248SDivya Koppera 		err = lan8814_ack_interrupt(phydev);
3081b3ec7248SDivya Koppera 		if (err)
3082b3ec7248SDivya Koppera 			return err;
3083b3ec7248SDivya Koppera 
3084b3ec7248SDivya Koppera 		err = phy_write(phydev, LAN8814_INTC, LAN8814_INT_LINK);
3085b3ec7248SDivya Koppera 	} else {
3086b3ec7248SDivya Koppera 		err = phy_write(phydev, LAN8814_INTC, 0);
3087b3ec7248SDivya Koppera 		if (err)
3088b3ec7248SDivya Koppera 			return err;
3089b3ec7248SDivya Koppera 
3090b3ec7248SDivya Koppera 		err = lan8814_ack_interrupt(phydev);
3091b3ec7248SDivya Koppera 	}
3092b3ec7248SDivya Koppera 
3093b3ec7248SDivya Koppera 	return err;
3094b3ec7248SDivya Koppera }
3095b3ec7248SDivya Koppera 
3096ece19502SDivya Koppera static void lan8814_ptp_init(struct phy_device *phydev)
3097ece19502SDivya Koppera {
3098ece19502SDivya Koppera 	struct kszphy_priv *priv = phydev->priv;
3099ece19502SDivya Koppera 	struct kszphy_ptp_priv *ptp_priv = &priv->ptp_priv;
3100ece19502SDivya Koppera 	u32 temp;
3101ece19502SDivya Koppera 
310231d00ca4SMichael Walle 	if (!IS_ENABLED(CONFIG_PTP_1588_CLOCK) ||
310331d00ca4SMichael Walle 	    !IS_ENABLED(CONFIG_NETWORK_PHY_TIMESTAMPING))
310431d00ca4SMichael Walle 		return;
310531d00ca4SMichael Walle 
3106ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 5, TSU_HARD_RESET, TSU_HARD_RESET_);
3107ece19502SDivya Koppera 
3108ece19502SDivya Koppera 	temp = lanphy_read_page_reg(phydev, 5, PTP_TX_MOD);
3109ece19502SDivya Koppera 	temp |= PTP_TX_MOD_BAD_UDPV4_CHKSUM_FORCE_FCS_DIS_;
3110ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 5, PTP_TX_MOD, temp);
3111ece19502SDivya Koppera 
3112ece19502SDivya Koppera 	temp = lanphy_read_page_reg(phydev, 5, PTP_RX_MOD);
3113ece19502SDivya Koppera 	temp |= PTP_RX_MOD_BAD_UDPV4_CHKSUM_FORCE_FCS_DIS_;
3114ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 5, PTP_RX_MOD, temp);
3115ece19502SDivya Koppera 
3116ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 5, PTP_RX_PARSE_CONFIG, 0);
3117ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 5, PTP_TX_PARSE_CONFIG, 0);
3118ece19502SDivya Koppera 
3119ece19502SDivya Koppera 	/* Removing default registers configs related to L2 and IP */
3120ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 5, PTP_TX_PARSE_L2_ADDR_EN, 0);
3121ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 5, PTP_RX_PARSE_L2_ADDR_EN, 0);
3122ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 5, PTP_TX_PARSE_IP_ADDR_EN, 0);
3123ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 5, PTP_RX_PARSE_IP_ADDR_EN, 0);
3124ece19502SDivya Koppera 
3125ece19502SDivya Koppera 	skb_queue_head_init(&ptp_priv->tx_queue);
3126ece19502SDivya Koppera 	skb_queue_head_init(&ptp_priv->rx_queue);
3127ece19502SDivya Koppera 	INIT_LIST_HEAD(&ptp_priv->rx_ts_list);
3128ece19502SDivya Koppera 	spin_lock_init(&ptp_priv->rx_ts_lock);
3129ece19502SDivya Koppera 
3130ece19502SDivya Koppera 	ptp_priv->phydev = phydev;
3131ece19502SDivya Koppera 
3132ece19502SDivya Koppera 	ptp_priv->mii_ts.rxtstamp = lan8814_rxtstamp;
3133ece19502SDivya Koppera 	ptp_priv->mii_ts.txtstamp = lan8814_txtstamp;
3134ece19502SDivya Koppera 	ptp_priv->mii_ts.hwtstamp = lan8814_hwtstamp;
3135ece19502SDivya Koppera 	ptp_priv->mii_ts.ts_info  = lan8814_ts_info;
3136ece19502SDivya Koppera 
3137ece19502SDivya Koppera 	phydev->mii_ts = &ptp_priv->mii_ts;
3138ece19502SDivya Koppera }
3139ece19502SDivya Koppera 
3140ece19502SDivya Koppera static int lan8814_ptp_probe_once(struct phy_device *phydev)
3141ece19502SDivya Koppera {
3142ece19502SDivya Koppera 	struct lan8814_shared_priv *shared = phydev->shared->priv;
3143ece19502SDivya Koppera 
3144ece19502SDivya Koppera 	/* Initialise shared lock for clock*/
3145ece19502SDivya Koppera 	mutex_init(&shared->shared_lock);
3146ece19502SDivya Koppera 
3147ece19502SDivya Koppera 	shared->ptp_clock_info.owner = THIS_MODULE;
3148ece19502SDivya Koppera 	snprintf(shared->ptp_clock_info.name, 30, "%s", phydev->drv->name);
3149ece19502SDivya Koppera 	shared->ptp_clock_info.max_adj = 31249999;
3150ece19502SDivya Koppera 	shared->ptp_clock_info.n_alarm = 0;
3151ece19502SDivya Koppera 	shared->ptp_clock_info.n_ext_ts = 0;
3152ece19502SDivya Koppera 	shared->ptp_clock_info.n_pins = 0;
3153ece19502SDivya Koppera 	shared->ptp_clock_info.pps = 0;
3154ece19502SDivya Koppera 	shared->ptp_clock_info.pin_config = NULL;
3155ece19502SDivya Koppera 	shared->ptp_clock_info.adjfine = lan8814_ptpci_adjfine;
3156ece19502SDivya Koppera 	shared->ptp_clock_info.adjtime = lan8814_ptpci_adjtime;
3157ece19502SDivya Koppera 	shared->ptp_clock_info.gettime64 = lan8814_ptpci_gettime64;
3158ece19502SDivya Koppera 	shared->ptp_clock_info.settime64 = lan8814_ptpci_settime64;
3159ece19502SDivya Koppera 	shared->ptp_clock_info.getcrosststamp = NULL;
3160ece19502SDivya Koppera 
3161ece19502SDivya Koppera 	shared->ptp_clock = ptp_clock_register(&shared->ptp_clock_info,
3162ece19502SDivya Koppera 					       &phydev->mdio.dev);
31633f88d7d1SDivya Koppera 	if (IS_ERR(shared->ptp_clock)) {
3164ece19502SDivya Koppera 		phydev_err(phydev, "ptp_clock_register failed %lu\n",
3165ece19502SDivya Koppera 			   PTR_ERR(shared->ptp_clock));
3166ece19502SDivya Koppera 		return -EINVAL;
3167ece19502SDivya Koppera 	}
3168ece19502SDivya Koppera 
31693f88d7d1SDivya Koppera 	/* Check if PHC support is missing at the configuration level */
31703f88d7d1SDivya Koppera 	if (!shared->ptp_clock)
31713f88d7d1SDivya Koppera 		return 0;
31723f88d7d1SDivya Koppera 
3173ece19502SDivya Koppera 	phydev_dbg(phydev, "successfully registered ptp clock\n");
3174ece19502SDivya Koppera 
3175ece19502SDivya Koppera 	shared->phydev = phydev;
3176ece19502SDivya Koppera 
3177ece19502SDivya Koppera 	/* The EP.4 is shared between all the PHYs in the package and also it
3178ece19502SDivya Koppera 	 * can be accessed by any of the PHYs
3179ece19502SDivya Koppera 	 */
3180ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 4, LTC_HARD_RESET, LTC_HARD_RESET_);
3181ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 4, PTP_OPERATING_MODE,
3182ece19502SDivya Koppera 			      PTP_OPERATING_MODE_STANDALONE_);
3183ece19502SDivya Koppera 
3184ece19502SDivya Koppera 	return 0;
3185ece19502SDivya Koppera }
3186ece19502SDivya Koppera 
3187a516b7f7SDivya Koppera static void lan8814_setup_led(struct phy_device *phydev, int val)
3188a516b7f7SDivya Koppera {
3189a516b7f7SDivya Koppera 	int temp;
3190a516b7f7SDivya Koppera 
3191a516b7f7SDivya Koppera 	temp = lanphy_read_page_reg(phydev, 5, LAN8814_LED_CTRL_1);
3192a516b7f7SDivya Koppera 
3193a516b7f7SDivya Koppera 	if (val)
3194a516b7f7SDivya Koppera 		temp |= LAN8814_LED_CTRL_1_KSZ9031_LED_MODE_;
3195a516b7f7SDivya Koppera 	else
3196a516b7f7SDivya Koppera 		temp &= ~LAN8814_LED_CTRL_1_KSZ9031_LED_MODE_;
3197a516b7f7SDivya Koppera 
3198a516b7f7SDivya Koppera 	lanphy_write_page_reg(phydev, 5, LAN8814_LED_CTRL_1, temp);
3199a516b7f7SDivya Koppera }
3200a516b7f7SDivya Koppera 
3201ece19502SDivya Koppera static int lan8814_config_init(struct phy_device *phydev)
3202ece19502SDivya Koppera {
3203a516b7f7SDivya Koppera 	struct kszphy_priv *lan8814 = phydev->priv;
3204ece19502SDivya Koppera 	int val;
3205ece19502SDivya Koppera 
3206ece19502SDivya Koppera 	/* Reset the PHY */
3207ece19502SDivya Koppera 	val = lanphy_read_page_reg(phydev, 4, LAN8814_QSGMII_SOFT_RESET);
3208ece19502SDivya Koppera 	val |= LAN8814_QSGMII_SOFT_RESET_BIT;
3209ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 4, LAN8814_QSGMII_SOFT_RESET, val);
3210ece19502SDivya Koppera 
3211ece19502SDivya Koppera 	/* Disable ANEG with QSGMII PCS Host side */
3212ece19502SDivya Koppera 	val = lanphy_read_page_reg(phydev, 5, LAN8814_QSGMII_PCS1G_ANEG_CONFIG);
3213ece19502SDivya Koppera 	val &= ~LAN8814_QSGMII_PCS1G_ANEG_CONFIG_ANEG_ENA;
3214ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 5, LAN8814_QSGMII_PCS1G_ANEG_CONFIG, val);
3215ece19502SDivya Koppera 
3216ece19502SDivya Koppera 	/* MDI-X setting for swap A,B transmit */
3217ece19502SDivya Koppera 	val = lanphy_read_page_reg(phydev, 2, LAN8814_ALIGN_SWAP);
3218ece19502SDivya Koppera 	val &= ~LAN8814_ALIGN_TX_A_B_SWAP_MASK;
3219ece19502SDivya Koppera 	val |= LAN8814_ALIGN_TX_A_B_SWAP;
3220ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 2, LAN8814_ALIGN_SWAP, val);
3221ece19502SDivya Koppera 
3222a516b7f7SDivya Koppera 	if (lan8814->led_mode >= 0)
3223a516b7f7SDivya Koppera 		lan8814_setup_led(phydev, lan8814->led_mode);
3224a516b7f7SDivya Koppera 
3225ece19502SDivya Koppera 	return 0;
3226ece19502SDivya Koppera }
3227ece19502SDivya Koppera 
32284a4ce822SHoratiu Vultur /* It is expected that there will not be any 'lan8814_take_coma_mode'
32294a4ce822SHoratiu Vultur  * function called in suspend. Because the GPIO line can be shared, so if one of
32304a4ce822SHoratiu Vultur  * the phys goes back in coma mode, then all the other PHYs will go, which is
32314a4ce822SHoratiu Vultur  * wrong.
32324a4ce822SHoratiu Vultur  */
3233738871b0SMichael Walle static int lan8814_release_coma_mode(struct phy_device *phydev)
3234738871b0SMichael Walle {
3235738871b0SMichael Walle 	struct gpio_desc *gpiod;
3236738871b0SMichael Walle 
3237738871b0SMichael Walle 	gpiod = devm_gpiod_get_optional(&phydev->mdio.dev, "coma-mode",
32384a4ce822SHoratiu Vultur 					GPIOD_OUT_HIGH_OPEN_DRAIN |
32394a4ce822SHoratiu Vultur 					GPIOD_FLAGS_BIT_NONEXCLUSIVE);
3240738871b0SMichael Walle 	if (IS_ERR(gpiod))
3241738871b0SMichael Walle 		return PTR_ERR(gpiod);
3242738871b0SMichael Walle 
3243738871b0SMichael Walle 	gpiod_set_consumer_name(gpiod, "LAN8814 coma mode");
3244738871b0SMichael Walle 	gpiod_set_value_cansleep(gpiod, 0);
3245738871b0SMichael Walle 
3246738871b0SMichael Walle 	return 0;
3247738871b0SMichael Walle }
3248738871b0SMichael Walle 
3249ece19502SDivya Koppera static int lan8814_probe(struct phy_device *phydev)
3250ece19502SDivya Koppera {
3251a516b7f7SDivya Koppera 	const struct kszphy_type *type = phydev->drv->driver_data;
3252ece19502SDivya Koppera 	struct kszphy_priv *priv;
3253ece19502SDivya Koppera 	u16 addr;
3254ece19502SDivya Koppera 	int err;
3255ece19502SDivya Koppera 
3256ece19502SDivya Koppera 	priv = devm_kzalloc(&phydev->mdio.dev, sizeof(*priv), GFP_KERNEL);
3257ece19502SDivya Koppera 	if (!priv)
3258ece19502SDivya Koppera 		return -ENOMEM;
3259ece19502SDivya Koppera 
3260ece19502SDivya Koppera 	phydev->priv = priv;
3261ece19502SDivya Koppera 
3262a516b7f7SDivya Koppera 	priv->type = type;
3263a516b7f7SDivya Koppera 
3264a516b7f7SDivya Koppera 	kszphy_parse_led_mode(phydev);
3265a516b7f7SDivya Koppera 
3266ece19502SDivya Koppera 	/* Strap-in value for PHY address, below register read gives starting
3267ece19502SDivya Koppera 	 * phy address value
3268ece19502SDivya Koppera 	 */
3269ece19502SDivya Koppera 	addr = lanphy_read_page_reg(phydev, 4, 0) & 0x1F;
3270ece19502SDivya Koppera 	devm_phy_package_join(&phydev->mdio.dev, phydev,
3271ece19502SDivya Koppera 			      addr, sizeof(struct lan8814_shared_priv));
3272ece19502SDivya Koppera 
3273ece19502SDivya Koppera 	if (phy_package_init_once(phydev)) {
3274738871b0SMichael Walle 		err = lan8814_release_coma_mode(phydev);
3275738871b0SMichael Walle 		if (err)
3276738871b0SMichael Walle 			return err;
3277738871b0SMichael Walle 
3278ece19502SDivya Koppera 		err = lan8814_ptp_probe_once(phydev);
3279ece19502SDivya Koppera 		if (err)
3280ece19502SDivya Koppera 			return err;
3281ece19502SDivya Koppera 	}
3282ece19502SDivya Koppera 
3283ece19502SDivya Koppera 	lan8814_ptp_init(phydev);
3284ece19502SDivya Koppera 
3285ece19502SDivya Koppera 	return 0;
3286ece19502SDivya Koppera }
3287ece19502SDivya Koppera 
3288a8f1a19dSHoratiu Vultur #define LAN8841_MMD_TIMER_REG			0
3289a8f1a19dSHoratiu Vultur #define LAN8841_MMD0_REGISTER_17		17
3290a8f1a19dSHoratiu Vultur #define LAN8841_MMD0_REGISTER_17_DROP_OPT(x)	((x) & 0x3)
3291a8f1a19dSHoratiu Vultur #define LAN8841_MMD0_REGISTER_17_XMIT_TOG_TX_DIS	BIT(3)
3292a8f1a19dSHoratiu Vultur #define LAN8841_OPERATION_MODE_STRAP_OVERRIDE_LOW_REG	2
3293a8f1a19dSHoratiu Vultur #define LAN8841_OPERATION_MODE_STRAP_OVERRIDE_LOW_REG_MAGJACK	BIT(14)
3294a8f1a19dSHoratiu Vultur #define LAN8841_MMD_ANALOG_REG			28
3295a8f1a19dSHoratiu Vultur #define LAN8841_ANALOG_CONTROL_1		1
3296a8f1a19dSHoratiu Vultur #define LAN8841_ANALOG_CONTROL_1_PLL_TRIM(x)	(((x) & 0x3) << 5)
3297a8f1a19dSHoratiu Vultur #define LAN8841_ANALOG_CONTROL_10		13
3298a8f1a19dSHoratiu Vultur #define LAN8841_ANALOG_CONTROL_10_PLL_DIV(x)	((x) & 0x3)
3299a8f1a19dSHoratiu Vultur #define LAN8841_ANALOG_CONTROL_11		14
3300a8f1a19dSHoratiu Vultur #define LAN8841_ANALOG_CONTROL_11_LDO_REF(x)	(((x) & 0x7) << 12)
3301a8f1a19dSHoratiu Vultur #define LAN8841_TX_LOW_I_CH_C_D_POWER_MANAGMENT	69
3302a8f1a19dSHoratiu Vultur #define LAN8841_TX_LOW_I_CH_C_D_POWER_MANAGMENT_VAL 0xbffc
3303a8f1a19dSHoratiu Vultur #define LAN8841_BTRX_POWER_DOWN			70
3304a8f1a19dSHoratiu Vultur #define LAN8841_BTRX_POWER_DOWN_QBIAS_CH_A	BIT(0)
3305a8f1a19dSHoratiu Vultur #define LAN8841_BTRX_POWER_DOWN_BTRX_CH_A	BIT(1)
3306a8f1a19dSHoratiu Vultur #define LAN8841_BTRX_POWER_DOWN_QBIAS_CH_B	BIT(2)
3307a8f1a19dSHoratiu Vultur #define LAN8841_BTRX_POWER_DOWN_BTRX_CH_B	BIT(3)
3308a8f1a19dSHoratiu Vultur #define LAN8841_BTRX_POWER_DOWN_BTRX_CH_C	BIT(5)
3309a8f1a19dSHoratiu Vultur #define LAN8841_BTRX_POWER_DOWN_BTRX_CH_D	BIT(7)
3310a8f1a19dSHoratiu Vultur #define LAN8841_ADC_CHANNEL_MASK		198
3311cafc3662SHoratiu Vultur #define LAN8841_PTP_RX_PARSE_L2_ADDR_EN		370
3312cafc3662SHoratiu Vultur #define LAN8841_PTP_RX_PARSE_IP_ADDR_EN		371
3313cafc3662SHoratiu Vultur #define LAN8841_PTP_TX_PARSE_L2_ADDR_EN		434
3314cafc3662SHoratiu Vultur #define LAN8841_PTP_TX_PARSE_IP_ADDR_EN		435
3315cafc3662SHoratiu Vultur #define LAN8841_PTP_CMD_CTL			256
3316cafc3662SHoratiu Vultur #define LAN8841_PTP_CMD_CTL_PTP_ENABLE		BIT(2)
3317cafc3662SHoratiu Vultur #define LAN8841_PTP_CMD_CTL_PTP_DISABLE		BIT(1)
3318cafc3662SHoratiu Vultur #define LAN8841_PTP_CMD_CTL_PTP_RESET		BIT(0)
3319cafc3662SHoratiu Vultur #define LAN8841_PTP_RX_PARSE_CONFIG		368
3320cafc3662SHoratiu Vultur #define LAN8841_PTP_TX_PARSE_CONFIG		432
3321*cc755495SHoratiu Vultur #define LAN8841_PTP_RX_MODE			381
3322*cc755495SHoratiu Vultur #define LAN8841_PTP_INSERT_TS_EN		BIT(0)
3323*cc755495SHoratiu Vultur #define LAN8841_PTP_INSERT_TS_32BIT		BIT(1)
3324a8f1a19dSHoratiu Vultur 
3325a8f1a19dSHoratiu Vultur static int lan8841_config_init(struct phy_device *phydev)
3326a8f1a19dSHoratiu Vultur {
3327a8f1a19dSHoratiu Vultur 	int ret;
3328a8f1a19dSHoratiu Vultur 
3329a8f1a19dSHoratiu Vultur 	ret = ksz9131_config_init(phydev);
3330a8f1a19dSHoratiu Vultur 	if (ret)
3331a8f1a19dSHoratiu Vultur 		return ret;
3332a8f1a19dSHoratiu Vultur 
3333cafc3662SHoratiu Vultur 	/* Initialize the HW by resetting everything */
3334cafc3662SHoratiu Vultur 	phy_modify_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
3335cafc3662SHoratiu Vultur 		       LAN8841_PTP_CMD_CTL,
3336cafc3662SHoratiu Vultur 		       LAN8841_PTP_CMD_CTL_PTP_RESET,
3337cafc3662SHoratiu Vultur 		       LAN8841_PTP_CMD_CTL_PTP_RESET);
3338cafc3662SHoratiu Vultur 
3339cafc3662SHoratiu Vultur 	phy_modify_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
3340cafc3662SHoratiu Vultur 		       LAN8841_PTP_CMD_CTL,
3341cafc3662SHoratiu Vultur 		       LAN8841_PTP_CMD_CTL_PTP_ENABLE,
3342cafc3662SHoratiu Vultur 		       LAN8841_PTP_CMD_CTL_PTP_ENABLE);
3343cafc3662SHoratiu Vultur 
3344cafc3662SHoratiu Vultur 	/* Don't process any frames */
3345cafc3662SHoratiu Vultur 	phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
3346cafc3662SHoratiu Vultur 		      LAN8841_PTP_RX_PARSE_CONFIG, 0);
3347cafc3662SHoratiu Vultur 	phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
3348cafc3662SHoratiu Vultur 		      LAN8841_PTP_TX_PARSE_CONFIG, 0);
3349cafc3662SHoratiu Vultur 	phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
3350cafc3662SHoratiu Vultur 		      LAN8841_PTP_TX_PARSE_L2_ADDR_EN, 0);
3351cafc3662SHoratiu Vultur 	phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
3352cafc3662SHoratiu Vultur 		      LAN8841_PTP_RX_PARSE_L2_ADDR_EN, 0);
3353cafc3662SHoratiu Vultur 	phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
3354cafc3662SHoratiu Vultur 		      LAN8841_PTP_TX_PARSE_IP_ADDR_EN, 0);
3355cafc3662SHoratiu Vultur 	phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
3356cafc3662SHoratiu Vultur 		      LAN8841_PTP_RX_PARSE_IP_ADDR_EN, 0);
3357cafc3662SHoratiu Vultur 
3358a8f1a19dSHoratiu Vultur 	/* 100BT Clause 40 improvenent errata */
3359a8f1a19dSHoratiu Vultur 	phy_write_mmd(phydev, LAN8841_MMD_ANALOG_REG,
3360a8f1a19dSHoratiu Vultur 		      LAN8841_ANALOG_CONTROL_1,
3361a8f1a19dSHoratiu Vultur 		      LAN8841_ANALOG_CONTROL_1_PLL_TRIM(0x2));
3362a8f1a19dSHoratiu Vultur 	phy_write_mmd(phydev, LAN8841_MMD_ANALOG_REG,
3363a8f1a19dSHoratiu Vultur 		      LAN8841_ANALOG_CONTROL_10,
3364a8f1a19dSHoratiu Vultur 		      LAN8841_ANALOG_CONTROL_10_PLL_DIV(0x1));
3365a8f1a19dSHoratiu Vultur 
3366a8f1a19dSHoratiu Vultur 	/* 10M/100M Ethernet Signal Tuning Errata for Shorted-Center Tap
3367a8f1a19dSHoratiu Vultur 	 * Magnetics
3368a8f1a19dSHoratiu Vultur 	 */
3369a8f1a19dSHoratiu Vultur 	ret = phy_read_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
3370a8f1a19dSHoratiu Vultur 			   LAN8841_OPERATION_MODE_STRAP_OVERRIDE_LOW_REG);
3371a8f1a19dSHoratiu Vultur 	if (ret & LAN8841_OPERATION_MODE_STRAP_OVERRIDE_LOW_REG_MAGJACK) {
3372a8f1a19dSHoratiu Vultur 		phy_write_mmd(phydev, LAN8841_MMD_ANALOG_REG,
3373a8f1a19dSHoratiu Vultur 			      LAN8841_TX_LOW_I_CH_C_D_POWER_MANAGMENT,
3374a8f1a19dSHoratiu Vultur 			      LAN8841_TX_LOW_I_CH_C_D_POWER_MANAGMENT_VAL);
3375a8f1a19dSHoratiu Vultur 		phy_write_mmd(phydev, LAN8841_MMD_ANALOG_REG,
3376a8f1a19dSHoratiu Vultur 			      LAN8841_BTRX_POWER_DOWN,
3377a8f1a19dSHoratiu Vultur 			      LAN8841_BTRX_POWER_DOWN_QBIAS_CH_A |
3378a8f1a19dSHoratiu Vultur 			      LAN8841_BTRX_POWER_DOWN_BTRX_CH_A |
3379a8f1a19dSHoratiu Vultur 			      LAN8841_BTRX_POWER_DOWN_QBIAS_CH_B |
3380a8f1a19dSHoratiu Vultur 			      LAN8841_BTRX_POWER_DOWN_BTRX_CH_B |
3381a8f1a19dSHoratiu Vultur 			      LAN8841_BTRX_POWER_DOWN_BTRX_CH_C |
3382a8f1a19dSHoratiu Vultur 			      LAN8841_BTRX_POWER_DOWN_BTRX_CH_D);
3383a8f1a19dSHoratiu Vultur 	}
3384a8f1a19dSHoratiu Vultur 
3385a8f1a19dSHoratiu Vultur 	/* LDO Adjustment errata */
3386a8f1a19dSHoratiu Vultur 	phy_write_mmd(phydev, LAN8841_MMD_ANALOG_REG,
3387a8f1a19dSHoratiu Vultur 		      LAN8841_ANALOG_CONTROL_11,
3388a8f1a19dSHoratiu Vultur 		      LAN8841_ANALOG_CONTROL_11_LDO_REF(1));
3389a8f1a19dSHoratiu Vultur 
3390a8f1a19dSHoratiu Vultur 	/* 100BT RGMII latency tuning errata */
3391a8f1a19dSHoratiu Vultur 	phy_write_mmd(phydev, MDIO_MMD_PMAPMD,
3392a8f1a19dSHoratiu Vultur 		      LAN8841_ADC_CHANNEL_MASK, 0x0);
3393a8f1a19dSHoratiu Vultur 	phy_write_mmd(phydev, LAN8841_MMD_TIMER_REG,
3394a8f1a19dSHoratiu Vultur 		      LAN8841_MMD0_REGISTER_17,
3395a8f1a19dSHoratiu Vultur 		      LAN8841_MMD0_REGISTER_17_DROP_OPT(2) |
3396a8f1a19dSHoratiu Vultur 		      LAN8841_MMD0_REGISTER_17_XMIT_TOG_TX_DIS);
3397a8f1a19dSHoratiu Vultur 
3398a8f1a19dSHoratiu Vultur 	return 0;
3399a8f1a19dSHoratiu Vultur }
3400a8f1a19dSHoratiu Vultur 
3401a8f1a19dSHoratiu Vultur #define LAN8841_OUTPUT_CTRL			25
3402a8f1a19dSHoratiu Vultur #define LAN8841_OUTPUT_CTRL_INT_BUFFER		BIT(14)
3403cafc3662SHoratiu Vultur #define LAN8841_INT_PTP				BIT(9)
3404a8f1a19dSHoratiu Vultur 
3405a8f1a19dSHoratiu Vultur static int lan8841_config_intr(struct phy_device *phydev)
3406a8f1a19dSHoratiu Vultur {
3407a8f1a19dSHoratiu Vultur 	int err;
3408a8f1a19dSHoratiu Vultur 
3409a8f1a19dSHoratiu Vultur 	phy_modify(phydev, LAN8841_OUTPUT_CTRL,
3410a8f1a19dSHoratiu Vultur 		   LAN8841_OUTPUT_CTRL_INT_BUFFER, 0);
3411a8f1a19dSHoratiu Vultur 
3412a8f1a19dSHoratiu Vultur 	if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
3413a8f1a19dSHoratiu Vultur 		err = phy_read(phydev, LAN8814_INTS);
3414a8f1a19dSHoratiu Vultur 		if (err)
3415a8f1a19dSHoratiu Vultur 			return err;
3416a8f1a19dSHoratiu Vultur 
3417cafc3662SHoratiu Vultur 		/* Enable / disable interrupts. It is OK to enable PTP interrupt
3418cafc3662SHoratiu Vultur 		 * even if it PTP is not enabled. Because the underneath blocks
3419cafc3662SHoratiu Vultur 		 * will not enable the PTP so we will never get the PTP
3420cafc3662SHoratiu Vultur 		 * interrupt.
3421cafc3662SHoratiu Vultur 		 */
3422a8f1a19dSHoratiu Vultur 		err = phy_write(phydev, LAN8814_INTC,
3423cafc3662SHoratiu Vultur 				LAN8814_INT_LINK | LAN8841_INT_PTP);
3424a8f1a19dSHoratiu Vultur 	} else {
3425a8f1a19dSHoratiu Vultur 		err = phy_write(phydev, LAN8814_INTC, 0);
3426a8f1a19dSHoratiu Vultur 		if (err)
3427a8f1a19dSHoratiu Vultur 			return err;
3428a8f1a19dSHoratiu Vultur 
3429a8f1a19dSHoratiu Vultur 		err = phy_read(phydev, LAN8814_INTS);
3430a8f1a19dSHoratiu Vultur 	}
3431a8f1a19dSHoratiu Vultur 
3432a8f1a19dSHoratiu Vultur 	return err;
3433a8f1a19dSHoratiu Vultur }
3434a8f1a19dSHoratiu Vultur 
3435cafc3662SHoratiu Vultur #define LAN8841_PTP_TX_EGRESS_SEC_LO			453
3436cafc3662SHoratiu Vultur #define LAN8841_PTP_TX_EGRESS_SEC_HI			452
3437cafc3662SHoratiu Vultur #define LAN8841_PTP_TX_EGRESS_NS_LO			451
3438cafc3662SHoratiu Vultur #define LAN8841_PTP_TX_EGRESS_NS_HI			450
3439cafc3662SHoratiu Vultur #define LAN8841_PTP_TX_EGRESS_NSEC_HI_VALID		BIT(15)
3440cafc3662SHoratiu Vultur #define LAN8841_PTP_TX_MSG_HEADER2			455
3441cafc3662SHoratiu Vultur 
3442cafc3662SHoratiu Vultur static bool lan8841_ptp_get_tx_ts(struct kszphy_ptp_priv *ptp_priv,
3443cafc3662SHoratiu Vultur 				  u32 *sec, u32 *nsec, u16 *seq)
3444cafc3662SHoratiu Vultur {
3445cafc3662SHoratiu Vultur 	struct phy_device *phydev = ptp_priv->phydev;
3446cafc3662SHoratiu Vultur 
3447cafc3662SHoratiu Vultur 	*nsec = phy_read_mmd(phydev, 2, LAN8841_PTP_TX_EGRESS_NS_HI);
3448cafc3662SHoratiu Vultur 	if (!(*nsec & LAN8841_PTP_TX_EGRESS_NSEC_HI_VALID))
3449cafc3662SHoratiu Vultur 		return false;
3450cafc3662SHoratiu Vultur 
3451cafc3662SHoratiu Vultur 	*nsec = ((*nsec & 0x3fff) << 16);
3452cafc3662SHoratiu Vultur 	*nsec = *nsec | phy_read_mmd(phydev, 2, LAN8841_PTP_TX_EGRESS_NS_LO);
3453cafc3662SHoratiu Vultur 
3454cafc3662SHoratiu Vultur 	*sec = phy_read_mmd(phydev, 2, LAN8841_PTP_TX_EGRESS_SEC_HI);
3455cafc3662SHoratiu Vultur 	*sec = *sec << 16;
3456cafc3662SHoratiu Vultur 	*sec = *sec | phy_read_mmd(phydev, 2, LAN8841_PTP_TX_EGRESS_SEC_LO);
3457cafc3662SHoratiu Vultur 
3458cafc3662SHoratiu Vultur 	*seq = phy_read_mmd(phydev, 2, LAN8841_PTP_TX_MSG_HEADER2);
3459cafc3662SHoratiu Vultur 
3460cafc3662SHoratiu Vultur 	return true;
3461cafc3662SHoratiu Vultur }
3462cafc3662SHoratiu Vultur 
3463cafc3662SHoratiu Vultur static void lan8841_ptp_process_tx_ts(struct kszphy_ptp_priv *ptp_priv)
3464cafc3662SHoratiu Vultur {
3465cafc3662SHoratiu Vultur 	u32 sec, nsec;
3466cafc3662SHoratiu Vultur 	u16 seq;
3467cafc3662SHoratiu Vultur 
3468cafc3662SHoratiu Vultur 	while (lan8841_ptp_get_tx_ts(ptp_priv, &sec, &nsec, &seq))
3469cafc3662SHoratiu Vultur 		lan8814_match_tx_skb(ptp_priv, sec, nsec, seq);
3470cafc3662SHoratiu Vultur }
3471cafc3662SHoratiu Vultur 
3472cafc3662SHoratiu Vultur #define LAN8841_PTP_INT_STS			259
3473cafc3662SHoratiu Vultur #define LAN8841_PTP_INT_STS_PTP_TX_TS_OVRFL_INT	BIT(13)
3474cafc3662SHoratiu Vultur #define LAN8841_PTP_INT_STS_PTP_TX_TS_INT	BIT(12)
3475fac63186SHoratiu Vultur #define LAN8841_PTP_INT_STS_PTP_GPIO_CAP_INT	BIT(2)
3476cafc3662SHoratiu Vultur 
3477*cc755495SHoratiu Vultur static void lan8841_ptp_flush_fifo(struct kszphy_ptp_priv *ptp_priv)
3478cafc3662SHoratiu Vultur {
3479cafc3662SHoratiu Vultur 	struct phy_device *phydev = ptp_priv->phydev;
3480cafc3662SHoratiu Vultur 	int i;
3481cafc3662SHoratiu Vultur 
3482cafc3662SHoratiu Vultur 	for (i = 0; i < FIFO_SIZE; ++i)
3483*cc755495SHoratiu Vultur 		phy_read_mmd(phydev, 2, LAN8841_PTP_TX_MSG_HEADER2);
3484cafc3662SHoratiu Vultur 
3485cafc3662SHoratiu Vultur 	phy_read_mmd(phydev, 2, LAN8841_PTP_INT_STS);
3486cafc3662SHoratiu Vultur }
3487cafc3662SHoratiu Vultur 
3488fac63186SHoratiu Vultur #define LAN8841_PTP_GPIO_CAP_STS			506
3489fac63186SHoratiu Vultur #define LAN8841_PTP_GPIO_SEL				327
3490fac63186SHoratiu Vultur #define LAN8841_PTP_GPIO_SEL_GPIO_SEL(gpio)		((gpio) << 8)
3491fac63186SHoratiu Vultur #define LAN8841_PTP_GPIO_RE_LTC_SEC_HI_CAP		498
3492fac63186SHoratiu Vultur #define LAN8841_PTP_GPIO_RE_LTC_SEC_LO_CAP		499
3493fac63186SHoratiu Vultur #define LAN8841_PTP_GPIO_RE_LTC_NS_HI_CAP		500
3494fac63186SHoratiu Vultur #define LAN8841_PTP_GPIO_RE_LTC_NS_LO_CAP		501
3495fac63186SHoratiu Vultur #define LAN8841_PTP_GPIO_FE_LTC_SEC_HI_CAP		502
3496fac63186SHoratiu Vultur #define LAN8841_PTP_GPIO_FE_LTC_SEC_LO_CAP		503
3497fac63186SHoratiu Vultur #define LAN8841_PTP_GPIO_FE_LTC_NS_HI_CAP		504
3498fac63186SHoratiu Vultur #define LAN8841_PTP_GPIO_FE_LTC_NS_LO_CAP		505
3499fac63186SHoratiu Vultur 
3500fac63186SHoratiu Vultur static void lan8841_gpio_process_cap(struct kszphy_ptp_priv *ptp_priv)
3501fac63186SHoratiu Vultur {
3502fac63186SHoratiu Vultur 	struct phy_device *phydev = ptp_priv->phydev;
3503fac63186SHoratiu Vultur 	struct ptp_clock_event ptp_event = {0};
3504fac63186SHoratiu Vultur 	int pin, ret, tmp;
3505fac63186SHoratiu Vultur 	s32 sec, nsec;
3506fac63186SHoratiu Vultur 
3507fac63186SHoratiu Vultur 	pin = ptp_find_pin_unlocked(ptp_priv->ptp_clock, PTP_PF_EXTTS, 0);
3508fac63186SHoratiu Vultur 	if (pin == -1)
3509fac63186SHoratiu Vultur 		return;
3510fac63186SHoratiu Vultur 
3511fac63186SHoratiu Vultur 	tmp = phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_CAP_STS);
3512fac63186SHoratiu Vultur 	if (tmp < 0)
3513fac63186SHoratiu Vultur 		return;
3514fac63186SHoratiu Vultur 
3515fac63186SHoratiu Vultur 	ret = phy_write_mmd(phydev, 2, LAN8841_PTP_GPIO_SEL,
3516fac63186SHoratiu Vultur 			    LAN8841_PTP_GPIO_SEL_GPIO_SEL(pin));
3517fac63186SHoratiu Vultur 	if (ret)
3518fac63186SHoratiu Vultur 		return;
3519fac63186SHoratiu Vultur 
3520fac63186SHoratiu Vultur 	mutex_lock(&ptp_priv->ptp_lock);
3521fac63186SHoratiu Vultur 	if (tmp & BIT(pin)) {
3522fac63186SHoratiu Vultur 		sec = phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_RE_LTC_SEC_HI_CAP);
3523fac63186SHoratiu Vultur 		sec <<= 16;
3524fac63186SHoratiu Vultur 		sec |= phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_RE_LTC_SEC_LO_CAP);
3525fac63186SHoratiu Vultur 
3526fac63186SHoratiu Vultur 		nsec = phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_RE_LTC_NS_HI_CAP) & 0x3fff;
3527fac63186SHoratiu Vultur 		nsec <<= 16;
3528fac63186SHoratiu Vultur 		nsec |= phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_RE_LTC_NS_LO_CAP);
3529fac63186SHoratiu Vultur 	} else {
3530fac63186SHoratiu Vultur 		sec = phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_FE_LTC_SEC_HI_CAP);
3531fac63186SHoratiu Vultur 		sec <<= 16;
3532fac63186SHoratiu Vultur 		sec |= phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_FE_LTC_SEC_LO_CAP);
3533fac63186SHoratiu Vultur 
3534fac63186SHoratiu Vultur 		nsec = phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_FE_LTC_NS_HI_CAP) & 0x3fff;
3535fac63186SHoratiu Vultur 		nsec <<= 16;
3536fac63186SHoratiu Vultur 		nsec |= phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_FE_LTC_NS_LO_CAP);
3537fac63186SHoratiu Vultur 	}
3538fac63186SHoratiu Vultur 	mutex_unlock(&ptp_priv->ptp_lock);
3539fac63186SHoratiu Vultur 	ret = phy_write_mmd(phydev, 2, LAN8841_PTP_GPIO_SEL, 0);
3540fac63186SHoratiu Vultur 	if (ret)
3541fac63186SHoratiu Vultur 		return;
3542fac63186SHoratiu Vultur 
3543fac63186SHoratiu Vultur 	ptp_event.index = 0;
3544fac63186SHoratiu Vultur 	ptp_event.timestamp = ktime_set(sec, nsec);
3545fac63186SHoratiu Vultur 	ptp_event.type = PTP_CLOCK_EXTTS;
3546fac63186SHoratiu Vultur 	ptp_clock_event(ptp_priv->ptp_clock, &ptp_event);
3547fac63186SHoratiu Vultur }
3548fac63186SHoratiu Vultur 
3549cafc3662SHoratiu Vultur static void lan8841_handle_ptp_interrupt(struct phy_device *phydev)
3550cafc3662SHoratiu Vultur {
3551cafc3662SHoratiu Vultur 	struct kszphy_priv *priv = phydev->priv;
3552cafc3662SHoratiu Vultur 	struct kszphy_ptp_priv *ptp_priv = &priv->ptp_priv;
3553cafc3662SHoratiu Vultur 	u16 status;
3554cafc3662SHoratiu Vultur 
3555cafc3662SHoratiu Vultur 	do {
3556cafc3662SHoratiu Vultur 		status = phy_read_mmd(phydev, 2, LAN8841_PTP_INT_STS);
3557fac63186SHoratiu Vultur 
3558cafc3662SHoratiu Vultur 		if (status & LAN8841_PTP_INT_STS_PTP_TX_TS_INT)
3559cafc3662SHoratiu Vultur 			lan8841_ptp_process_tx_ts(ptp_priv);
3560cafc3662SHoratiu Vultur 
3561fac63186SHoratiu Vultur 		if (status & LAN8841_PTP_INT_STS_PTP_GPIO_CAP_INT)
3562fac63186SHoratiu Vultur 			lan8841_gpio_process_cap(ptp_priv);
3563fac63186SHoratiu Vultur 
3564cafc3662SHoratiu Vultur 		if (status & LAN8841_PTP_INT_STS_PTP_TX_TS_OVRFL_INT) {
3565*cc755495SHoratiu Vultur 			lan8841_ptp_flush_fifo(ptp_priv);
3566cafc3662SHoratiu Vultur 			skb_queue_purge(&ptp_priv->tx_queue);
3567cafc3662SHoratiu Vultur 		}
3568cafc3662SHoratiu Vultur 
3569*cc755495SHoratiu Vultur 	} while (status & (LAN8841_PTP_INT_STS_PTP_TX_TS_INT |
3570*cc755495SHoratiu Vultur 			   LAN8841_PTP_INT_STS_PTP_GPIO_CAP_INT |
3571*cc755495SHoratiu Vultur 			   LAN8841_PTP_INT_STS_PTP_TX_TS_OVRFL_INT));
3572cafc3662SHoratiu Vultur }
3573cafc3662SHoratiu Vultur 
3574cafc3662SHoratiu Vultur #define LAN8841_INTS_PTP		BIT(9)
3575cafc3662SHoratiu Vultur 
3576a8f1a19dSHoratiu Vultur static irqreturn_t lan8841_handle_interrupt(struct phy_device *phydev)
3577a8f1a19dSHoratiu Vultur {
3578cafc3662SHoratiu Vultur 	irqreturn_t ret = IRQ_NONE;
3579a8f1a19dSHoratiu Vultur 	int irq_status;
3580a8f1a19dSHoratiu Vultur 
3581a8f1a19dSHoratiu Vultur 	irq_status = phy_read(phydev, LAN8814_INTS);
3582a8f1a19dSHoratiu Vultur 	if (irq_status < 0) {
3583a8f1a19dSHoratiu Vultur 		phy_error(phydev);
3584a8f1a19dSHoratiu Vultur 		return IRQ_NONE;
3585a8f1a19dSHoratiu Vultur 	}
3586a8f1a19dSHoratiu Vultur 
3587a8f1a19dSHoratiu Vultur 	if (irq_status & LAN8814_INT_LINK) {
3588a8f1a19dSHoratiu Vultur 		phy_trigger_machine(phydev);
3589cafc3662SHoratiu Vultur 		ret = IRQ_HANDLED;
3590a8f1a19dSHoratiu Vultur 	}
3591a8f1a19dSHoratiu Vultur 
3592cafc3662SHoratiu Vultur 	if (irq_status & LAN8841_INTS_PTP) {
3593cafc3662SHoratiu Vultur 		lan8841_handle_ptp_interrupt(phydev);
3594cafc3662SHoratiu Vultur 		ret = IRQ_HANDLED;
3595a8f1a19dSHoratiu Vultur 	}
3596a8f1a19dSHoratiu Vultur 
3597cafc3662SHoratiu Vultur 	return ret;
3598cafc3662SHoratiu Vultur }
3599cafc3662SHoratiu Vultur 
3600cafc3662SHoratiu Vultur static int lan8841_ts_info(struct mii_timestamper *mii_ts,
3601cafc3662SHoratiu Vultur 			   struct ethtool_ts_info *info)
3602cafc3662SHoratiu Vultur {
3603cafc3662SHoratiu Vultur 	struct kszphy_ptp_priv *ptp_priv;
3604cafc3662SHoratiu Vultur 
3605cafc3662SHoratiu Vultur 	ptp_priv = container_of(mii_ts, struct kszphy_ptp_priv, mii_ts);
3606cafc3662SHoratiu Vultur 
3607cafc3662SHoratiu Vultur 	info->phc_index = ptp_priv->ptp_clock ?
3608cafc3662SHoratiu Vultur 				ptp_clock_index(ptp_priv->ptp_clock) : -1;
3609cafc3662SHoratiu Vultur 	if (info->phc_index == -1) {
3610cafc3662SHoratiu Vultur 		info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
3611cafc3662SHoratiu Vultur 					 SOF_TIMESTAMPING_RX_SOFTWARE |
3612cafc3662SHoratiu Vultur 					 SOF_TIMESTAMPING_SOFTWARE;
3613cafc3662SHoratiu Vultur 		return 0;
3614cafc3662SHoratiu Vultur 	}
3615cafc3662SHoratiu Vultur 
3616cafc3662SHoratiu Vultur 	info->so_timestamping = SOF_TIMESTAMPING_TX_HARDWARE |
3617cafc3662SHoratiu Vultur 				SOF_TIMESTAMPING_RX_HARDWARE |
3618cafc3662SHoratiu Vultur 				SOF_TIMESTAMPING_RAW_HARDWARE;
3619cafc3662SHoratiu Vultur 
3620cafc3662SHoratiu Vultur 	info->tx_types = (1 << HWTSTAMP_TX_OFF) |
3621cafc3662SHoratiu Vultur 			 (1 << HWTSTAMP_TX_ON) |
3622cafc3662SHoratiu Vultur 			 (1 << HWTSTAMP_TX_ONESTEP_SYNC);
3623cafc3662SHoratiu Vultur 
3624cafc3662SHoratiu Vultur 	info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
3625cafc3662SHoratiu Vultur 			   (1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT) |
3626cafc3662SHoratiu Vultur 			   (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
3627cafc3662SHoratiu Vultur 			   (1 << HWTSTAMP_FILTER_PTP_V2_EVENT);
3628cafc3662SHoratiu Vultur 
3629cafc3662SHoratiu Vultur 	return 0;
3630cafc3662SHoratiu Vultur }
3631cafc3662SHoratiu Vultur 
3632cafc3662SHoratiu Vultur #define LAN8841_PTP_INT_EN			260
3633cafc3662SHoratiu Vultur #define LAN8841_PTP_INT_EN_PTP_TX_TS_OVRFL_EN	BIT(13)
3634cafc3662SHoratiu Vultur #define LAN8841_PTP_INT_EN_PTP_TX_TS_EN		BIT(12)
3635cafc3662SHoratiu Vultur 
3636*cc755495SHoratiu Vultur static void lan8841_ptp_enable_processing(struct kszphy_ptp_priv *ptp_priv,
3637cafc3662SHoratiu Vultur 					  bool enable)
3638cafc3662SHoratiu Vultur {
3639cafc3662SHoratiu Vultur 	struct phy_device *phydev = ptp_priv->phydev;
3640cafc3662SHoratiu Vultur 
3641*cc755495SHoratiu Vultur 	if (enable) {
3642*cc755495SHoratiu Vultur 		/* Enable interrupts on the TX side */
3643cafc3662SHoratiu Vultur 		phy_modify_mmd(phydev, 2, LAN8841_PTP_INT_EN,
3644cafc3662SHoratiu Vultur 			       LAN8841_PTP_INT_EN_PTP_TX_TS_OVRFL_EN |
3645*cc755495SHoratiu Vultur 			       LAN8841_PTP_INT_EN_PTP_TX_TS_EN,
3646cafc3662SHoratiu Vultur 			       LAN8841_PTP_INT_EN_PTP_TX_TS_OVRFL_EN |
3647*cc755495SHoratiu Vultur 			       LAN8841_PTP_INT_EN_PTP_TX_TS_EN);
3648*cc755495SHoratiu Vultur 
3649*cc755495SHoratiu Vultur 		/* Enable the modification of the frame on RX side,
3650*cc755495SHoratiu Vultur 		 * this will add the ns and 2 bits of sec in the reserved field
3651*cc755495SHoratiu Vultur 		 * of the PTP header
3652*cc755495SHoratiu Vultur 		 */
3653*cc755495SHoratiu Vultur 		phy_modify_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
3654*cc755495SHoratiu Vultur 			       LAN8841_PTP_RX_MODE,
3655*cc755495SHoratiu Vultur 			       LAN8841_PTP_INSERT_TS_EN |
3656*cc755495SHoratiu Vultur 			       LAN8841_PTP_INSERT_TS_32BIT,
3657*cc755495SHoratiu Vultur 			       LAN8841_PTP_INSERT_TS_EN |
3658*cc755495SHoratiu Vultur 			       LAN8841_PTP_INSERT_TS_32BIT);
3659*cc755495SHoratiu Vultur 
3660*cc755495SHoratiu Vultur 		ptp_schedule_worker(ptp_priv->ptp_clock, 0);
3661*cc755495SHoratiu Vultur 	} else {
3662*cc755495SHoratiu Vultur 		/* Disable interrupts on the TX side */
3663cafc3662SHoratiu Vultur 		phy_modify_mmd(phydev, 2, LAN8841_PTP_INT_EN,
3664cafc3662SHoratiu Vultur 			       LAN8841_PTP_INT_EN_PTP_TX_TS_OVRFL_EN |
3665*cc755495SHoratiu Vultur 			       LAN8841_PTP_INT_EN_PTP_TX_TS_EN, 0);
3666*cc755495SHoratiu Vultur 
3667*cc755495SHoratiu Vultur 		/* Disable modification of the RX frames */
3668*cc755495SHoratiu Vultur 		phy_modify_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
3669*cc755495SHoratiu Vultur 			       LAN8841_PTP_RX_MODE,
3670*cc755495SHoratiu Vultur 			       LAN8841_PTP_INSERT_TS_EN |
3671*cc755495SHoratiu Vultur 			       LAN8841_PTP_INSERT_TS_32BIT, 0);
3672*cc755495SHoratiu Vultur 
3673*cc755495SHoratiu Vultur 		ptp_cancel_worker_sync(ptp_priv->ptp_clock);
3674*cc755495SHoratiu Vultur 	}
3675cafc3662SHoratiu Vultur }
3676cafc3662SHoratiu Vultur 
3677cafc3662SHoratiu Vultur #define LAN8841_PTP_RX_TIMESTAMP_EN		379
3678cafc3662SHoratiu Vultur #define LAN8841_PTP_TX_TIMESTAMP_EN		443
3679cafc3662SHoratiu Vultur #define LAN8841_PTP_TX_MOD			445
3680cafc3662SHoratiu Vultur 
3681cafc3662SHoratiu Vultur static int lan8841_hwtstamp(struct mii_timestamper *mii_ts, struct ifreq *ifr)
3682cafc3662SHoratiu Vultur {
3683cafc3662SHoratiu Vultur 	struct kszphy_ptp_priv *ptp_priv = container_of(mii_ts, struct kszphy_ptp_priv, mii_ts);
3684cafc3662SHoratiu Vultur 	struct phy_device *phydev = ptp_priv->phydev;
3685cafc3662SHoratiu Vultur 	struct hwtstamp_config config;
3686cafc3662SHoratiu Vultur 	int txcfg = 0, rxcfg = 0;
3687cafc3662SHoratiu Vultur 	int pkt_ts_enable;
3688cafc3662SHoratiu Vultur 
3689cafc3662SHoratiu Vultur 	if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
3690cafc3662SHoratiu Vultur 		return -EFAULT;
3691cafc3662SHoratiu Vultur 
3692cafc3662SHoratiu Vultur 	ptp_priv->hwts_tx_type = config.tx_type;
3693cafc3662SHoratiu Vultur 	ptp_priv->rx_filter = config.rx_filter;
3694cafc3662SHoratiu Vultur 
3695cafc3662SHoratiu Vultur 	switch (config.rx_filter) {
3696cafc3662SHoratiu Vultur 	case HWTSTAMP_FILTER_NONE:
3697cafc3662SHoratiu Vultur 		ptp_priv->layer = 0;
3698cafc3662SHoratiu Vultur 		ptp_priv->version = 0;
3699cafc3662SHoratiu Vultur 		break;
3700cafc3662SHoratiu Vultur 	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
3701cafc3662SHoratiu Vultur 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
3702cafc3662SHoratiu Vultur 	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
3703cafc3662SHoratiu Vultur 		ptp_priv->layer = PTP_CLASS_L4;
3704cafc3662SHoratiu Vultur 		ptp_priv->version = PTP_CLASS_V2;
3705cafc3662SHoratiu Vultur 		break;
3706cafc3662SHoratiu Vultur 	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
3707cafc3662SHoratiu Vultur 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
3708cafc3662SHoratiu Vultur 	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
3709cafc3662SHoratiu Vultur 		ptp_priv->layer = PTP_CLASS_L2;
3710cafc3662SHoratiu Vultur 		ptp_priv->version = PTP_CLASS_V2;
3711cafc3662SHoratiu Vultur 		break;
3712cafc3662SHoratiu Vultur 	case HWTSTAMP_FILTER_PTP_V2_EVENT:
3713cafc3662SHoratiu Vultur 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
3714cafc3662SHoratiu Vultur 	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
3715cafc3662SHoratiu Vultur 		ptp_priv->layer = PTP_CLASS_L4 | PTP_CLASS_L2;
3716cafc3662SHoratiu Vultur 		ptp_priv->version = PTP_CLASS_V2;
3717cafc3662SHoratiu Vultur 		break;
3718cafc3662SHoratiu Vultur 	default:
3719cafc3662SHoratiu Vultur 		return -ERANGE;
3720cafc3662SHoratiu Vultur 	}
3721cafc3662SHoratiu Vultur 
3722cafc3662SHoratiu Vultur 	/* Setup parsing of the frames and enable the timestamping for ptp
3723cafc3662SHoratiu Vultur 	 * frames
3724cafc3662SHoratiu Vultur 	 */
3725cafc3662SHoratiu Vultur 	if (ptp_priv->layer & PTP_CLASS_L2) {
3726cafc3662SHoratiu Vultur 		rxcfg |= PTP_RX_PARSE_CONFIG_LAYER2_EN_;
3727cafc3662SHoratiu Vultur 		txcfg |= PTP_TX_PARSE_CONFIG_LAYER2_EN_;
3728cafc3662SHoratiu Vultur 	} else if (ptp_priv->layer & PTP_CLASS_L4) {
3729cafc3662SHoratiu Vultur 		rxcfg |= PTP_RX_PARSE_CONFIG_IPV4_EN_ | PTP_RX_PARSE_CONFIG_IPV6_EN_;
3730cafc3662SHoratiu Vultur 		txcfg |= PTP_TX_PARSE_CONFIG_IPV4_EN_ | PTP_TX_PARSE_CONFIG_IPV6_EN_;
3731cafc3662SHoratiu Vultur 	}
3732cafc3662SHoratiu Vultur 
3733cafc3662SHoratiu Vultur 	phy_write_mmd(phydev, 2, LAN8841_PTP_RX_PARSE_CONFIG, rxcfg);
3734cafc3662SHoratiu Vultur 	phy_write_mmd(phydev, 2, LAN8841_PTP_TX_PARSE_CONFIG, txcfg);
3735cafc3662SHoratiu Vultur 
3736cafc3662SHoratiu Vultur 	pkt_ts_enable = PTP_TIMESTAMP_EN_SYNC_ | PTP_TIMESTAMP_EN_DREQ_ |
3737cafc3662SHoratiu Vultur 			PTP_TIMESTAMP_EN_PDREQ_ | PTP_TIMESTAMP_EN_PDRES_;
3738cafc3662SHoratiu Vultur 	phy_write_mmd(phydev, 2, LAN8841_PTP_RX_TIMESTAMP_EN, pkt_ts_enable);
3739cafc3662SHoratiu Vultur 	phy_write_mmd(phydev, 2, LAN8841_PTP_TX_TIMESTAMP_EN, pkt_ts_enable);
3740cafc3662SHoratiu Vultur 
3741cafc3662SHoratiu Vultur 	/* Enable / disable of the TX timestamp in the SYNC frames */
3742cafc3662SHoratiu Vultur 	phy_modify_mmd(phydev, 2, LAN8841_PTP_TX_MOD,
3743cafc3662SHoratiu Vultur 		       PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_,
3744cafc3662SHoratiu Vultur 		       ptp_priv->hwts_tx_type == HWTSTAMP_TX_ONESTEP_SYNC ?
3745cafc3662SHoratiu Vultur 				PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_ : 0);
3746cafc3662SHoratiu Vultur 
3747cafc3662SHoratiu Vultur 	/* Now enable/disable the timestamping */
3748*cc755495SHoratiu Vultur 	lan8841_ptp_enable_processing(ptp_priv,
3749cafc3662SHoratiu Vultur 				      config.rx_filter != HWTSTAMP_FILTER_NONE);
3750cafc3662SHoratiu Vultur 
3751cafc3662SHoratiu Vultur 	skb_queue_purge(&ptp_priv->tx_queue);
3752cafc3662SHoratiu Vultur 
3753*cc755495SHoratiu Vultur 	lan8841_ptp_flush_fifo(ptp_priv);
3754cafc3662SHoratiu Vultur 
3755cafc3662SHoratiu Vultur 	return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ? -EFAULT : 0;
3756cafc3662SHoratiu Vultur }
3757cafc3662SHoratiu Vultur 
3758*cc755495SHoratiu Vultur static bool lan8841_rxtstamp(struct mii_timestamper *mii_ts,
3759*cc755495SHoratiu Vultur 			     struct sk_buff *skb, int type)
3760*cc755495SHoratiu Vultur {
3761*cc755495SHoratiu Vultur 	struct kszphy_ptp_priv *ptp_priv =
3762*cc755495SHoratiu Vultur 			container_of(mii_ts, struct kszphy_ptp_priv, mii_ts);
3763*cc755495SHoratiu Vultur 	struct ptp_header *header = ptp_parse_header(skb, type);
3764*cc755495SHoratiu Vultur 	struct skb_shared_hwtstamps *shhwtstamps;
3765*cc755495SHoratiu Vultur 	struct timespec64 ts;
3766*cc755495SHoratiu Vultur 	unsigned long flags;
3767*cc755495SHoratiu Vultur 	u32 ts_header;
3768*cc755495SHoratiu Vultur 
3769*cc755495SHoratiu Vultur 	if (!header)
3770*cc755495SHoratiu Vultur 		return false;
3771*cc755495SHoratiu Vultur 
3772*cc755495SHoratiu Vultur 	if (ptp_priv->rx_filter == HWTSTAMP_FILTER_NONE ||
3773*cc755495SHoratiu Vultur 	    type == PTP_CLASS_NONE)
3774*cc755495SHoratiu Vultur 		return false;
3775*cc755495SHoratiu Vultur 
3776*cc755495SHoratiu Vultur 	if ((type & ptp_priv->version) == 0 || (type & ptp_priv->layer) == 0)
3777*cc755495SHoratiu Vultur 		return false;
3778*cc755495SHoratiu Vultur 
3779*cc755495SHoratiu Vultur 	spin_lock_irqsave(&ptp_priv->seconds_lock, flags);
3780*cc755495SHoratiu Vultur 	ts.tv_sec = ptp_priv->seconds;
3781*cc755495SHoratiu Vultur 	spin_unlock_irqrestore(&ptp_priv->seconds_lock, flags);
3782*cc755495SHoratiu Vultur 	ts_header = __be32_to_cpu(header->reserved2);
3783*cc755495SHoratiu Vultur 
3784*cc755495SHoratiu Vultur 	shhwtstamps = skb_hwtstamps(skb);
3785*cc755495SHoratiu Vultur 	memset(shhwtstamps, 0, sizeof(*shhwtstamps));
3786*cc755495SHoratiu Vultur 
3787*cc755495SHoratiu Vultur 	/* Check for any wrap arounds for the second part */
3788*cc755495SHoratiu Vultur 	if ((ts.tv_sec & GENMASK(1, 0)) == 0 && (ts_header >> 30) == 3)
3789*cc755495SHoratiu Vultur 		ts.tv_sec -= GENMASK(1, 0) + 1;
3790*cc755495SHoratiu Vultur 	else if ((ts.tv_sec & GENMASK(1, 0)) == 3 && (ts_header >> 30) == 0)
3791*cc755495SHoratiu Vultur 		ts.tv_sec += 1;
3792*cc755495SHoratiu Vultur 
3793*cc755495SHoratiu Vultur 	shhwtstamps->hwtstamp =
3794*cc755495SHoratiu Vultur 		ktime_set((ts.tv_sec & ~(GENMASK(1, 0))) | ts_header >> 30,
3795*cc755495SHoratiu Vultur 			  ts_header & GENMASK(29, 0));
3796*cc755495SHoratiu Vultur 	header->reserved2 = 0;
3797*cc755495SHoratiu Vultur 
3798*cc755495SHoratiu Vultur 	netif_rx(skb);
3799*cc755495SHoratiu Vultur 
3800*cc755495SHoratiu Vultur 	return true;
3801*cc755495SHoratiu Vultur }
3802*cc755495SHoratiu Vultur 
3803e4ed8ba0SHoratiu Vultur #define LAN8841_EVENT_A		0
3804e4ed8ba0SHoratiu Vultur #define LAN8841_EVENT_B		1
3805e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_LTC_TARGET_SEC_HI(event)	((event) == LAN8841_EVENT_A ? 278 : 288)
3806e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_LTC_TARGET_SEC_LO(event)	((event) == LAN8841_EVENT_A ? 279 : 289)
3807e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_LTC_TARGET_NS_HI(event)	((event) == LAN8841_EVENT_A ? 280 : 290)
3808e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_LTC_TARGET_NS_LO(event)	((event) == LAN8841_EVENT_A ? 281 : 291)
3809e4ed8ba0SHoratiu Vultur 
3810e4ed8ba0SHoratiu Vultur static int lan8841_ptp_set_target(struct kszphy_ptp_priv *ptp_priv, u8 event,
3811e4ed8ba0SHoratiu Vultur 				  s64 sec, u32 nsec)
3812e4ed8ba0SHoratiu Vultur {
3813e4ed8ba0SHoratiu Vultur 	struct phy_device *phydev = ptp_priv->phydev;
3814e4ed8ba0SHoratiu Vultur 	int ret;
3815e4ed8ba0SHoratiu Vultur 
3816e4ed8ba0SHoratiu Vultur 	ret = phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_TARGET_SEC_HI(event),
3817e4ed8ba0SHoratiu Vultur 			    upper_16_bits(sec));
3818e4ed8ba0SHoratiu Vultur 	if (ret)
3819e4ed8ba0SHoratiu Vultur 		return ret;
3820e4ed8ba0SHoratiu Vultur 
3821e4ed8ba0SHoratiu Vultur 	ret = phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_TARGET_SEC_LO(event),
3822e4ed8ba0SHoratiu Vultur 			    lower_16_bits(sec));
3823e4ed8ba0SHoratiu Vultur 	if (ret)
3824e4ed8ba0SHoratiu Vultur 		return ret;
3825e4ed8ba0SHoratiu Vultur 
3826e4ed8ba0SHoratiu Vultur 	ret = phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_TARGET_NS_HI(event) & 0x3fff,
3827e4ed8ba0SHoratiu Vultur 			    upper_16_bits(nsec));
3828e4ed8ba0SHoratiu Vultur 	if (ret)
3829e4ed8ba0SHoratiu Vultur 		return ret;
3830e4ed8ba0SHoratiu Vultur 
3831e4ed8ba0SHoratiu Vultur 	return phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_TARGET_NS_LO(event),
3832e4ed8ba0SHoratiu Vultur 			    lower_16_bits(nsec));
3833e4ed8ba0SHoratiu Vultur }
3834e4ed8ba0SHoratiu Vultur 
3835e4ed8ba0SHoratiu Vultur #define LAN8841_BUFFER_TIME	2
3836e4ed8ba0SHoratiu Vultur 
3837e4ed8ba0SHoratiu Vultur static int lan8841_ptp_update_target(struct kszphy_ptp_priv *ptp_priv,
3838e4ed8ba0SHoratiu Vultur 				     const struct timespec64 *ts)
3839e4ed8ba0SHoratiu Vultur {
3840e4ed8ba0SHoratiu Vultur 	return lan8841_ptp_set_target(ptp_priv, LAN8841_EVENT_A,
3841c6d6ef3eSHoratiu Vultur 				      ts->tv_sec + LAN8841_BUFFER_TIME, 0);
3842e4ed8ba0SHoratiu Vultur }
3843e4ed8ba0SHoratiu Vultur 
3844e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_LTC_TARGET_RELOAD_SEC_HI(event)	((event) == LAN8841_EVENT_A ? 282 : 292)
3845e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_LTC_TARGET_RELOAD_SEC_LO(event)	((event) == LAN8841_EVENT_A ? 283 : 293)
3846e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_LTC_TARGET_RELOAD_NS_HI(event)	((event) == LAN8841_EVENT_A ? 284 : 294)
3847e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_LTC_TARGET_RELOAD_NS_LO(event)	((event) == LAN8841_EVENT_A ? 285 : 295)
3848e4ed8ba0SHoratiu Vultur 
3849e4ed8ba0SHoratiu Vultur static int lan8841_ptp_set_reload(struct kszphy_ptp_priv *ptp_priv, u8 event,
3850e4ed8ba0SHoratiu Vultur 				  s64 sec, u32 nsec)
3851e4ed8ba0SHoratiu Vultur {
3852e4ed8ba0SHoratiu Vultur 	struct phy_device *phydev = ptp_priv->phydev;
3853e4ed8ba0SHoratiu Vultur 	int ret;
3854e4ed8ba0SHoratiu Vultur 
3855e4ed8ba0SHoratiu Vultur 	ret = phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_TARGET_RELOAD_SEC_HI(event),
3856e4ed8ba0SHoratiu Vultur 			    upper_16_bits(sec));
3857e4ed8ba0SHoratiu Vultur 	if (ret)
3858e4ed8ba0SHoratiu Vultur 		return ret;
3859e4ed8ba0SHoratiu Vultur 
3860e4ed8ba0SHoratiu Vultur 	ret = phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_TARGET_RELOAD_SEC_LO(event),
3861e4ed8ba0SHoratiu Vultur 			    lower_16_bits(sec));
3862e4ed8ba0SHoratiu Vultur 	if (ret)
3863e4ed8ba0SHoratiu Vultur 		return ret;
3864e4ed8ba0SHoratiu Vultur 
3865e4ed8ba0SHoratiu Vultur 	ret = phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_TARGET_RELOAD_NS_HI(event) & 0x3fff,
3866e4ed8ba0SHoratiu Vultur 			    upper_16_bits(nsec));
3867e4ed8ba0SHoratiu Vultur 	if (ret)
3868e4ed8ba0SHoratiu Vultur 		return ret;
3869e4ed8ba0SHoratiu Vultur 
3870e4ed8ba0SHoratiu Vultur 	return phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_TARGET_RELOAD_NS_LO(event),
3871e4ed8ba0SHoratiu Vultur 			     lower_16_bits(nsec));
3872e4ed8ba0SHoratiu Vultur }
3873e4ed8ba0SHoratiu Vultur 
3874cafc3662SHoratiu Vultur #define LAN8841_PTP_LTC_SET_SEC_HI	262
3875cafc3662SHoratiu Vultur #define LAN8841_PTP_LTC_SET_SEC_MID	263
3876cafc3662SHoratiu Vultur #define LAN8841_PTP_LTC_SET_SEC_LO	264
3877cafc3662SHoratiu Vultur #define LAN8841_PTP_LTC_SET_NS_HI	265
3878cafc3662SHoratiu Vultur #define LAN8841_PTP_LTC_SET_NS_LO	266
3879cafc3662SHoratiu Vultur #define LAN8841_PTP_CMD_CTL_PTP_LTC_LOAD	BIT(4)
3880cafc3662SHoratiu Vultur 
3881cafc3662SHoratiu Vultur static int lan8841_ptp_settime64(struct ptp_clock_info *ptp,
3882cafc3662SHoratiu Vultur 				 const struct timespec64 *ts)
3883cafc3662SHoratiu Vultur {
3884cafc3662SHoratiu Vultur 	struct kszphy_ptp_priv *ptp_priv = container_of(ptp, struct kszphy_ptp_priv,
3885cafc3662SHoratiu Vultur 							ptp_clock_info);
3886cafc3662SHoratiu Vultur 	struct phy_device *phydev = ptp_priv->phydev;
3887*cc755495SHoratiu Vultur 	unsigned long flags;
3888e4ed8ba0SHoratiu Vultur 	int ret;
3889cafc3662SHoratiu Vultur 
3890cafc3662SHoratiu Vultur 	/* Set the value to be stored */
3891cafc3662SHoratiu Vultur 	mutex_lock(&ptp_priv->ptp_lock);
3892cafc3662SHoratiu Vultur 	phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_SET_SEC_LO, lower_16_bits(ts->tv_sec));
3893cafc3662SHoratiu Vultur 	phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_SET_SEC_MID, upper_16_bits(ts->tv_sec));
3894cafc3662SHoratiu Vultur 	phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_SET_SEC_HI, upper_32_bits(ts->tv_sec) & 0xffff);
3895cafc3662SHoratiu Vultur 	phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_SET_NS_LO, lower_16_bits(ts->tv_nsec));
3896cafc3662SHoratiu Vultur 	phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_SET_NS_HI, upper_16_bits(ts->tv_nsec) & 0x3fff);
3897cafc3662SHoratiu Vultur 
3898cafc3662SHoratiu Vultur 	/* Set the command to load the LTC */
3899cafc3662SHoratiu Vultur 	phy_write_mmd(phydev, 2, LAN8841_PTP_CMD_CTL,
3900cafc3662SHoratiu Vultur 		      LAN8841_PTP_CMD_CTL_PTP_LTC_LOAD);
3901e4ed8ba0SHoratiu Vultur 	ret = lan8841_ptp_update_target(ptp_priv, ts);
3902cafc3662SHoratiu Vultur 	mutex_unlock(&ptp_priv->ptp_lock);
3903cafc3662SHoratiu Vultur 
3904*cc755495SHoratiu Vultur 	spin_lock_irqsave(&ptp_priv->seconds_lock, flags);
3905*cc755495SHoratiu Vultur 	ptp_priv->seconds = ts->tv_sec;
3906*cc755495SHoratiu Vultur 	spin_unlock_irqrestore(&ptp_priv->seconds_lock, flags);
3907*cc755495SHoratiu Vultur 
3908e4ed8ba0SHoratiu Vultur 	return ret;
3909cafc3662SHoratiu Vultur }
3910cafc3662SHoratiu Vultur 
3911cafc3662SHoratiu Vultur #define LAN8841_PTP_LTC_RD_SEC_HI	358
3912cafc3662SHoratiu Vultur #define LAN8841_PTP_LTC_RD_SEC_MID	359
3913cafc3662SHoratiu Vultur #define LAN8841_PTP_LTC_RD_SEC_LO	360
3914cafc3662SHoratiu Vultur #define LAN8841_PTP_LTC_RD_NS_HI	361
3915cafc3662SHoratiu Vultur #define LAN8841_PTP_LTC_RD_NS_LO	362
3916cafc3662SHoratiu Vultur #define LAN8841_PTP_CMD_CTL_PTP_LTC_READ	BIT(3)
3917cafc3662SHoratiu Vultur 
3918cafc3662SHoratiu Vultur static int lan8841_ptp_gettime64(struct ptp_clock_info *ptp,
3919cafc3662SHoratiu Vultur 				 struct timespec64 *ts)
3920cafc3662SHoratiu Vultur {
3921cafc3662SHoratiu Vultur 	struct kszphy_ptp_priv *ptp_priv = container_of(ptp, struct kszphy_ptp_priv,
3922cafc3662SHoratiu Vultur 							ptp_clock_info);
3923cafc3662SHoratiu Vultur 	struct phy_device *phydev = ptp_priv->phydev;
3924cafc3662SHoratiu Vultur 	time64_t s;
3925cafc3662SHoratiu Vultur 	s64 ns;
3926cafc3662SHoratiu Vultur 
3927cafc3662SHoratiu Vultur 	mutex_lock(&ptp_priv->ptp_lock);
3928cafc3662SHoratiu Vultur 	/* Issue the command to read the LTC */
3929cafc3662SHoratiu Vultur 	phy_write_mmd(phydev, 2, LAN8841_PTP_CMD_CTL,
3930cafc3662SHoratiu Vultur 		      LAN8841_PTP_CMD_CTL_PTP_LTC_READ);
3931cafc3662SHoratiu Vultur 
3932cafc3662SHoratiu Vultur 	/* Read the LTC */
3933cafc3662SHoratiu Vultur 	s = phy_read_mmd(phydev, 2, LAN8841_PTP_LTC_RD_SEC_HI);
3934cafc3662SHoratiu Vultur 	s <<= 16;
3935cafc3662SHoratiu Vultur 	s |= phy_read_mmd(phydev, 2, LAN8841_PTP_LTC_RD_SEC_MID);
3936cafc3662SHoratiu Vultur 	s <<= 16;
3937cafc3662SHoratiu Vultur 	s |= phy_read_mmd(phydev, 2, LAN8841_PTP_LTC_RD_SEC_LO);
3938cafc3662SHoratiu Vultur 
3939cafc3662SHoratiu Vultur 	ns = phy_read_mmd(phydev, 2, LAN8841_PTP_LTC_RD_NS_HI) & 0x3fff;
3940cafc3662SHoratiu Vultur 	ns <<= 16;
3941cafc3662SHoratiu Vultur 	ns |= phy_read_mmd(phydev, 2, LAN8841_PTP_LTC_RD_NS_LO);
3942cafc3662SHoratiu Vultur 	mutex_unlock(&ptp_priv->ptp_lock);
3943cafc3662SHoratiu Vultur 
3944cafc3662SHoratiu Vultur 	set_normalized_timespec64(ts, s, ns);
3945cafc3662SHoratiu Vultur 	return 0;
3946cafc3662SHoratiu Vultur }
3947cafc3662SHoratiu Vultur 
3948*cc755495SHoratiu Vultur static void lan8841_ptp_getseconds(struct ptp_clock_info *ptp,
3949*cc755495SHoratiu Vultur 				   struct timespec64 *ts)
3950*cc755495SHoratiu Vultur {
3951*cc755495SHoratiu Vultur 	struct kszphy_ptp_priv *ptp_priv = container_of(ptp, struct kszphy_ptp_priv,
3952*cc755495SHoratiu Vultur 							ptp_clock_info);
3953*cc755495SHoratiu Vultur 	struct phy_device *phydev = ptp_priv->phydev;
3954*cc755495SHoratiu Vultur 	time64_t s;
3955*cc755495SHoratiu Vultur 
3956*cc755495SHoratiu Vultur 	mutex_lock(&ptp_priv->ptp_lock);
3957*cc755495SHoratiu Vultur 	/* Issue the command to read the LTC */
3958*cc755495SHoratiu Vultur 	phy_write_mmd(phydev, 2, LAN8841_PTP_CMD_CTL,
3959*cc755495SHoratiu Vultur 		      LAN8841_PTP_CMD_CTL_PTP_LTC_READ);
3960*cc755495SHoratiu Vultur 
3961*cc755495SHoratiu Vultur 	/* Read the LTC */
3962*cc755495SHoratiu Vultur 	s = phy_read_mmd(phydev, 2, LAN8841_PTP_LTC_RD_SEC_HI);
3963*cc755495SHoratiu Vultur 	s <<= 16;
3964*cc755495SHoratiu Vultur 	s |= phy_read_mmd(phydev, 2, LAN8841_PTP_LTC_RD_SEC_MID);
3965*cc755495SHoratiu Vultur 	s <<= 16;
3966*cc755495SHoratiu Vultur 	s |= phy_read_mmd(phydev, 2, LAN8841_PTP_LTC_RD_SEC_LO);
3967*cc755495SHoratiu Vultur 	mutex_unlock(&ptp_priv->ptp_lock);
3968*cc755495SHoratiu Vultur 
3969*cc755495SHoratiu Vultur 	set_normalized_timespec64(ts, s, 0);
3970*cc755495SHoratiu Vultur }
3971*cc755495SHoratiu Vultur 
3972cafc3662SHoratiu Vultur #define LAN8841_PTP_LTC_STEP_ADJ_LO			276
3973cafc3662SHoratiu Vultur #define LAN8841_PTP_LTC_STEP_ADJ_HI			275
3974cafc3662SHoratiu Vultur #define LAN8841_PTP_LTC_STEP_ADJ_DIR			BIT(15)
3975cafc3662SHoratiu Vultur #define LAN8841_PTP_CMD_CTL_PTP_LTC_STEP_SECONDS	BIT(5)
3976cafc3662SHoratiu Vultur #define LAN8841_PTP_CMD_CTL_PTP_LTC_STEP_NANOSECONDS	BIT(6)
3977cafc3662SHoratiu Vultur 
3978cafc3662SHoratiu Vultur static int lan8841_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
3979cafc3662SHoratiu Vultur {
3980cafc3662SHoratiu Vultur 	struct kszphy_ptp_priv *ptp_priv = container_of(ptp, struct kszphy_ptp_priv,
3981cafc3662SHoratiu Vultur 							ptp_clock_info);
3982cafc3662SHoratiu Vultur 	struct phy_device *phydev = ptp_priv->phydev;
3983cafc3662SHoratiu Vultur 	struct timespec64 ts;
3984cafc3662SHoratiu Vultur 	bool add = true;
3985cafc3662SHoratiu Vultur 	u32 nsec;
3986cafc3662SHoratiu Vultur 	s32 sec;
3987e4ed8ba0SHoratiu Vultur 	int ret;
3988cafc3662SHoratiu Vultur 
3989cafc3662SHoratiu Vultur 	/* The HW allows up to 15 sec to adjust the time, but here we limit to
3990cafc3662SHoratiu Vultur 	 * 10 sec the adjustment. The reason is, in case the adjustment is 14
3991cafc3662SHoratiu Vultur 	 * sec and 999999999 nsec, then we add 8ns to compansate the actual
3992cafc3662SHoratiu Vultur 	 * increment so the value can be bigger than 15 sec. Therefore limit the
3993cafc3662SHoratiu Vultur 	 * possible adjustments so we will not have these corner cases
3994cafc3662SHoratiu Vultur 	 */
3995cafc3662SHoratiu Vultur 	if (delta > 10000000000LL || delta < -10000000000LL) {
3996cafc3662SHoratiu Vultur 		/* The timeadjustment is too big, so fall back using set time */
3997cafc3662SHoratiu Vultur 		u64 now;
3998cafc3662SHoratiu Vultur 
3999cafc3662SHoratiu Vultur 		ptp->gettime64(ptp, &ts);
4000cafc3662SHoratiu Vultur 
4001cafc3662SHoratiu Vultur 		now = ktime_to_ns(timespec64_to_ktime(ts));
4002cafc3662SHoratiu Vultur 		ts = ns_to_timespec64(now + delta);
4003cafc3662SHoratiu Vultur 
4004cafc3662SHoratiu Vultur 		ptp->settime64(ptp, &ts);
4005cafc3662SHoratiu Vultur 		return 0;
4006cafc3662SHoratiu Vultur 	}
4007cafc3662SHoratiu Vultur 
4008cafc3662SHoratiu Vultur 	sec = div_u64_rem(delta < 0 ? -delta : delta, NSEC_PER_SEC, &nsec);
4009cafc3662SHoratiu Vultur 	if (delta < 0 && nsec != 0) {
4010cafc3662SHoratiu Vultur 		/* It is not allowed to adjust low the nsec part, therefore
4011cafc3662SHoratiu Vultur 		 * subtract more from second part and add to nanosecond such
4012cafc3662SHoratiu Vultur 		 * that would roll over, so the second part will increase
4013cafc3662SHoratiu Vultur 		 */
4014cafc3662SHoratiu Vultur 		sec--;
4015cafc3662SHoratiu Vultur 		nsec = NSEC_PER_SEC - nsec;
4016cafc3662SHoratiu Vultur 	}
4017cafc3662SHoratiu Vultur 
4018cafc3662SHoratiu Vultur 	/* Calculate the adjustments and the direction */
4019cafc3662SHoratiu Vultur 	if (delta < 0)
4020cafc3662SHoratiu Vultur 		add = false;
4021cafc3662SHoratiu Vultur 
4022cafc3662SHoratiu Vultur 	if (nsec > 0)
4023cafc3662SHoratiu Vultur 		/* add 8 ns to cover the likely normal increment */
4024cafc3662SHoratiu Vultur 		nsec += 8;
4025cafc3662SHoratiu Vultur 
4026cafc3662SHoratiu Vultur 	if (nsec >= NSEC_PER_SEC) {
4027cafc3662SHoratiu Vultur 		/* carry into seconds */
4028cafc3662SHoratiu Vultur 		sec++;
4029cafc3662SHoratiu Vultur 		nsec -= NSEC_PER_SEC;
4030cafc3662SHoratiu Vultur 	}
4031cafc3662SHoratiu Vultur 
4032cafc3662SHoratiu Vultur 	mutex_lock(&ptp_priv->ptp_lock);
4033cafc3662SHoratiu Vultur 	if (sec) {
4034cafc3662SHoratiu Vultur 		phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_STEP_ADJ_LO, sec);
4035cafc3662SHoratiu Vultur 		phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_STEP_ADJ_HI,
4036cafc3662SHoratiu Vultur 			      add ? LAN8841_PTP_LTC_STEP_ADJ_DIR : 0);
4037cafc3662SHoratiu Vultur 		phy_write_mmd(phydev, 2, LAN8841_PTP_CMD_CTL,
4038cafc3662SHoratiu Vultur 			      LAN8841_PTP_CMD_CTL_PTP_LTC_STEP_SECONDS);
4039cafc3662SHoratiu Vultur 	}
4040cafc3662SHoratiu Vultur 
4041cafc3662SHoratiu Vultur 	if (nsec) {
4042cafc3662SHoratiu Vultur 		phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_STEP_ADJ_LO,
4043cafc3662SHoratiu Vultur 			      nsec & 0xffff);
4044cafc3662SHoratiu Vultur 		phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_STEP_ADJ_HI,
4045cafc3662SHoratiu Vultur 			      (nsec >> 16) & 0x3fff);
4046cafc3662SHoratiu Vultur 		phy_write_mmd(phydev, 2, LAN8841_PTP_CMD_CTL,
4047cafc3662SHoratiu Vultur 			      LAN8841_PTP_CMD_CTL_PTP_LTC_STEP_NANOSECONDS);
4048cafc3662SHoratiu Vultur 	}
4049cafc3662SHoratiu Vultur 	mutex_unlock(&ptp_priv->ptp_lock);
4050cafc3662SHoratiu Vultur 
4051e4ed8ba0SHoratiu Vultur 	/* Update the target clock */
4052e4ed8ba0SHoratiu Vultur 	ptp->gettime64(ptp, &ts);
4053e4ed8ba0SHoratiu Vultur 	mutex_lock(&ptp_priv->ptp_lock);
4054e4ed8ba0SHoratiu Vultur 	ret = lan8841_ptp_update_target(ptp_priv, &ts);
4055e4ed8ba0SHoratiu Vultur 	mutex_unlock(&ptp_priv->ptp_lock);
4056e4ed8ba0SHoratiu Vultur 
4057e4ed8ba0SHoratiu Vultur 	return ret;
4058cafc3662SHoratiu Vultur }
4059cafc3662SHoratiu Vultur 
4060cafc3662SHoratiu Vultur #define LAN8841_PTP_LTC_RATE_ADJ_HI		269
4061cafc3662SHoratiu Vultur #define LAN8841_PTP_LTC_RATE_ADJ_HI_DIR		BIT(15)
4062cafc3662SHoratiu Vultur #define LAN8841_PTP_LTC_RATE_ADJ_LO		270
4063cafc3662SHoratiu Vultur 
4064cafc3662SHoratiu Vultur static int lan8841_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
4065cafc3662SHoratiu Vultur {
4066cafc3662SHoratiu Vultur 	struct kszphy_ptp_priv *ptp_priv = container_of(ptp, struct kszphy_ptp_priv,
4067cafc3662SHoratiu Vultur 							ptp_clock_info);
4068cafc3662SHoratiu Vultur 	struct phy_device *phydev = ptp_priv->phydev;
4069cafc3662SHoratiu Vultur 	bool faster = true;
4070cafc3662SHoratiu Vultur 	u32 rate;
4071cafc3662SHoratiu Vultur 
4072cafc3662SHoratiu Vultur 	if (!scaled_ppm)
4073cafc3662SHoratiu Vultur 		return 0;
4074cafc3662SHoratiu Vultur 
4075cafc3662SHoratiu Vultur 	if (scaled_ppm < 0) {
4076cafc3662SHoratiu Vultur 		scaled_ppm = -scaled_ppm;
4077cafc3662SHoratiu Vultur 		faster = false;
4078cafc3662SHoratiu Vultur 	}
4079cafc3662SHoratiu Vultur 
4080cafc3662SHoratiu Vultur 	rate = LAN8814_1PPM_FORMAT * (upper_16_bits(scaled_ppm));
4081cafc3662SHoratiu Vultur 	rate += (LAN8814_1PPM_FORMAT * (lower_16_bits(scaled_ppm))) >> 16;
4082cafc3662SHoratiu Vultur 
4083cafc3662SHoratiu Vultur 	mutex_lock(&ptp_priv->ptp_lock);
4084cafc3662SHoratiu Vultur 	phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_RATE_ADJ_HI,
4085cafc3662SHoratiu Vultur 		      faster ? LAN8841_PTP_LTC_RATE_ADJ_HI_DIR | (upper_16_bits(rate) & 0x3fff)
4086cafc3662SHoratiu Vultur 			     : upper_16_bits(rate) & 0x3fff);
4087cafc3662SHoratiu Vultur 	phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_RATE_ADJ_LO, lower_16_bits(rate));
4088cafc3662SHoratiu Vultur 	mutex_unlock(&ptp_priv->ptp_lock);
4089cafc3662SHoratiu Vultur 
4090cafc3662SHoratiu Vultur 	return 0;
4091cafc3662SHoratiu Vultur }
4092cafc3662SHoratiu Vultur 
4093e4ed8ba0SHoratiu Vultur static int lan8841_ptp_verify(struct ptp_clock_info *ptp, unsigned int pin,
4094e4ed8ba0SHoratiu Vultur 			      enum ptp_pin_function func, unsigned int chan)
4095e4ed8ba0SHoratiu Vultur {
4096e4ed8ba0SHoratiu Vultur 	switch (func) {
4097e4ed8ba0SHoratiu Vultur 	case PTP_PF_NONE:
4098e4ed8ba0SHoratiu Vultur 	case PTP_PF_PEROUT:
4099fac63186SHoratiu Vultur 	case PTP_PF_EXTTS:
4100e4ed8ba0SHoratiu Vultur 		break;
4101e4ed8ba0SHoratiu Vultur 	default:
4102e4ed8ba0SHoratiu Vultur 		return -1;
4103e4ed8ba0SHoratiu Vultur 	}
4104e4ed8ba0SHoratiu Vultur 
4105e4ed8ba0SHoratiu Vultur 	return 0;
4106e4ed8ba0SHoratiu Vultur }
4107e4ed8ba0SHoratiu Vultur 
4108e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_GPIO_NUM	10
4109e4ed8ba0SHoratiu Vultur #define LAN8841_GPIO_EN		128
4110e4ed8ba0SHoratiu Vultur #define LAN8841_GPIO_DIR	129
4111e4ed8ba0SHoratiu Vultur #define LAN8841_GPIO_BUF	130
4112e4ed8ba0SHoratiu Vultur 
4113e4ed8ba0SHoratiu Vultur static int lan8841_ptp_perout_off(struct kszphy_ptp_priv *ptp_priv, int pin)
4114e4ed8ba0SHoratiu Vultur {
4115e4ed8ba0SHoratiu Vultur 	struct phy_device *phydev = ptp_priv->phydev;
4116e4ed8ba0SHoratiu Vultur 	int ret;
4117e4ed8ba0SHoratiu Vultur 
4118e4ed8ba0SHoratiu Vultur 	ret = phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_EN, BIT(pin));
4119e4ed8ba0SHoratiu Vultur 	if (ret)
4120e4ed8ba0SHoratiu Vultur 		return ret;
4121e4ed8ba0SHoratiu Vultur 
4122e4ed8ba0SHoratiu Vultur 	ret = phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_DIR, BIT(pin));
4123e4ed8ba0SHoratiu Vultur 	if (ret)
4124e4ed8ba0SHoratiu Vultur 		return ret;
4125e4ed8ba0SHoratiu Vultur 
4126e4ed8ba0SHoratiu Vultur 	return phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_BUF, BIT(pin));
4127e4ed8ba0SHoratiu Vultur }
4128e4ed8ba0SHoratiu Vultur 
4129e4ed8ba0SHoratiu Vultur static int lan8841_ptp_perout_on(struct kszphy_ptp_priv *ptp_priv, int pin)
4130e4ed8ba0SHoratiu Vultur {
4131e4ed8ba0SHoratiu Vultur 	struct phy_device *phydev = ptp_priv->phydev;
4132e4ed8ba0SHoratiu Vultur 	int ret;
4133e4ed8ba0SHoratiu Vultur 
4134e4ed8ba0SHoratiu Vultur 	ret = phy_set_bits_mmd(phydev, 2, LAN8841_GPIO_EN, BIT(pin));
4135e4ed8ba0SHoratiu Vultur 	if (ret)
4136e4ed8ba0SHoratiu Vultur 		return ret;
4137e4ed8ba0SHoratiu Vultur 
4138e4ed8ba0SHoratiu Vultur 	ret = phy_set_bits_mmd(phydev, 2, LAN8841_GPIO_DIR, BIT(pin));
4139e4ed8ba0SHoratiu Vultur 	if (ret)
4140e4ed8ba0SHoratiu Vultur 		return ret;
4141e4ed8ba0SHoratiu Vultur 
4142e4ed8ba0SHoratiu Vultur 	return phy_set_bits_mmd(phydev, 2, LAN8841_GPIO_BUF, BIT(pin));
4143e4ed8ba0SHoratiu Vultur }
4144e4ed8ba0SHoratiu Vultur 
4145e4ed8ba0SHoratiu Vultur #define LAN8841_GPIO_DATA_SEL1				131
4146e4ed8ba0SHoratiu Vultur #define LAN8841_GPIO_DATA_SEL2				132
4147e4ed8ba0SHoratiu Vultur #define LAN8841_GPIO_DATA_SEL_GPIO_DATA_SEL_EVENT_MASK	GENMASK(2, 0)
4148e4ed8ba0SHoratiu Vultur #define LAN8841_GPIO_DATA_SEL_GPIO_DATA_SEL_EVENT_A	1
4149e4ed8ba0SHoratiu Vultur #define LAN8841_GPIO_DATA_SEL_GPIO_DATA_SEL_EVENT_B	2
4150e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_GENERAL_CONFIG			257
4151e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_A	BIT(1)
4152e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_B	BIT(3)
4153e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_A_MASK	GENMASK(7, 4)
4154e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_B_MASK	GENMASK(11, 8)
4155e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_A		4
4156e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_B		7
4157e4ed8ba0SHoratiu Vultur 
4158e4ed8ba0SHoratiu Vultur static int lan8841_ptp_remove_event(struct kszphy_ptp_priv *ptp_priv, int pin,
4159e4ed8ba0SHoratiu Vultur 				    u8 event)
4160e4ed8ba0SHoratiu Vultur {
4161e4ed8ba0SHoratiu Vultur 	struct phy_device *phydev = ptp_priv->phydev;
4162e4ed8ba0SHoratiu Vultur 	u16 tmp;
4163e4ed8ba0SHoratiu Vultur 	int ret;
4164e4ed8ba0SHoratiu Vultur 
4165e4ed8ba0SHoratiu Vultur 	/* Now remove pin from the event. GPIO_DATA_SEL1 contains the GPIO
4166e4ed8ba0SHoratiu Vultur 	 * pins 0-4 while GPIO_DATA_SEL2 contains GPIO pins 5-9, therefore
4167e4ed8ba0SHoratiu Vultur 	 * depending on the pin, it requires to read a different register
4168e4ed8ba0SHoratiu Vultur 	 */
4169e4ed8ba0SHoratiu Vultur 	if (pin < 5) {
4170e4ed8ba0SHoratiu Vultur 		tmp = LAN8841_GPIO_DATA_SEL_GPIO_DATA_SEL_EVENT_MASK << (3 * pin);
4171e4ed8ba0SHoratiu Vultur 		ret = phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_DATA_SEL1, tmp);
4172e4ed8ba0SHoratiu Vultur 	} else {
4173e4ed8ba0SHoratiu Vultur 		tmp = LAN8841_GPIO_DATA_SEL_GPIO_DATA_SEL_EVENT_MASK << (3 * (pin - 5));
4174e4ed8ba0SHoratiu Vultur 		ret = phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_DATA_SEL2, tmp);
4175e4ed8ba0SHoratiu Vultur 	}
4176e4ed8ba0SHoratiu Vultur 	if (ret)
4177e4ed8ba0SHoratiu Vultur 		return ret;
4178e4ed8ba0SHoratiu Vultur 
4179e4ed8ba0SHoratiu Vultur 	/* Disable the event */
4180e4ed8ba0SHoratiu Vultur 	if (event == LAN8841_EVENT_A)
4181e4ed8ba0SHoratiu Vultur 		tmp = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_A |
4182e4ed8ba0SHoratiu Vultur 		      LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_A_MASK;
4183e4ed8ba0SHoratiu Vultur 	else
4184e4ed8ba0SHoratiu Vultur 		tmp = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_B |
4185e4ed8ba0SHoratiu Vultur 		      LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_B_MASK;
4186e4ed8ba0SHoratiu Vultur 	return phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_EN, tmp);
4187e4ed8ba0SHoratiu Vultur }
4188e4ed8ba0SHoratiu Vultur 
4189e4ed8ba0SHoratiu Vultur static int lan8841_ptp_enable_event(struct kszphy_ptp_priv *ptp_priv, int pin,
4190e4ed8ba0SHoratiu Vultur 				    u8 event, int pulse_width)
4191e4ed8ba0SHoratiu Vultur {
4192e4ed8ba0SHoratiu Vultur 	struct phy_device *phydev = ptp_priv->phydev;
4193e4ed8ba0SHoratiu Vultur 	u16 tmp;
4194e4ed8ba0SHoratiu Vultur 	int ret;
4195e4ed8ba0SHoratiu Vultur 
4196e4ed8ba0SHoratiu Vultur 	/* Enable the event */
4197e4ed8ba0SHoratiu Vultur 	if (event == LAN8841_EVENT_A)
4198e4ed8ba0SHoratiu Vultur 		ret = phy_modify_mmd(phydev, 2, LAN8841_PTP_GENERAL_CONFIG,
4199e4ed8ba0SHoratiu Vultur 				     LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_A |
4200e4ed8ba0SHoratiu Vultur 				     LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_A_MASK,
4201e4ed8ba0SHoratiu Vultur 				     LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_A |
4202e4ed8ba0SHoratiu Vultur 				     pulse_width << LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_A);
4203e4ed8ba0SHoratiu Vultur 	else
4204e4ed8ba0SHoratiu Vultur 		ret = phy_modify_mmd(phydev, 2, LAN8841_PTP_GENERAL_CONFIG,
4205e4ed8ba0SHoratiu Vultur 				     LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_B |
4206e4ed8ba0SHoratiu Vultur 				     LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_B_MASK,
4207e4ed8ba0SHoratiu Vultur 				     LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_B |
4208e4ed8ba0SHoratiu Vultur 				     pulse_width << LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_B);
4209e4ed8ba0SHoratiu Vultur 	if (ret)
4210e4ed8ba0SHoratiu Vultur 		return ret;
4211e4ed8ba0SHoratiu Vultur 
4212e4ed8ba0SHoratiu Vultur 	/* Now connect the pin to the event. GPIO_DATA_SEL1 contains the GPIO
4213e4ed8ba0SHoratiu Vultur 	 * pins 0-4 while GPIO_DATA_SEL2 contains GPIO pins 5-9, therefore
4214e4ed8ba0SHoratiu Vultur 	 * depending on the pin, it requires to read a different register
4215e4ed8ba0SHoratiu Vultur 	 */
4216e4ed8ba0SHoratiu Vultur 	if (event == LAN8841_EVENT_A)
4217e4ed8ba0SHoratiu Vultur 		tmp = LAN8841_GPIO_DATA_SEL_GPIO_DATA_SEL_EVENT_A;
4218e4ed8ba0SHoratiu Vultur 	else
4219e4ed8ba0SHoratiu Vultur 		tmp = LAN8841_GPIO_DATA_SEL_GPIO_DATA_SEL_EVENT_B;
4220e4ed8ba0SHoratiu Vultur 
4221e4ed8ba0SHoratiu Vultur 	if (pin < 5)
4222e4ed8ba0SHoratiu Vultur 		ret = phy_set_bits_mmd(phydev, 2, LAN8841_GPIO_DATA_SEL1,
4223e4ed8ba0SHoratiu Vultur 				       tmp << (3 * pin));
4224e4ed8ba0SHoratiu Vultur 	else
4225e4ed8ba0SHoratiu Vultur 		ret = phy_set_bits_mmd(phydev, 2, LAN8841_GPIO_DATA_SEL2,
4226e4ed8ba0SHoratiu Vultur 				       tmp << (3 * (pin - 5)));
4227e4ed8ba0SHoratiu Vultur 
4228e4ed8ba0SHoratiu Vultur 	return ret;
4229e4ed8ba0SHoratiu Vultur }
4230e4ed8ba0SHoratiu Vultur 
4231e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_200MS	13
4232e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100MS	12
4233e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_50MS	11
4234e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_10MS	10
4235e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_5MS	9
4236e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_1MS	8
4237e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_500US	7
4238e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100US	6
4239e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_50US	5
4240e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_10US	4
4241e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_5US	3
4242e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_1US	2
4243e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_500NS	1
4244e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100NS	0
4245e4ed8ba0SHoratiu Vultur 
4246e4ed8ba0SHoratiu Vultur static int lan8841_ptp_perout(struct ptp_clock_info *ptp,
4247e4ed8ba0SHoratiu Vultur 			      struct ptp_clock_request *rq, int on)
4248e4ed8ba0SHoratiu Vultur {
4249e4ed8ba0SHoratiu Vultur 	struct kszphy_ptp_priv *ptp_priv = container_of(ptp, struct kszphy_ptp_priv,
4250e4ed8ba0SHoratiu Vultur 							ptp_clock_info);
4251e4ed8ba0SHoratiu Vultur 	struct phy_device *phydev = ptp_priv->phydev;
4252e4ed8ba0SHoratiu Vultur 	struct timespec64 ts_on, ts_period;
4253e4ed8ba0SHoratiu Vultur 	s64 on_nsec, period_nsec;
4254e4ed8ba0SHoratiu Vultur 	int pulse_width;
4255e4ed8ba0SHoratiu Vultur 	int pin;
4256e4ed8ba0SHoratiu Vultur 	int ret;
4257e4ed8ba0SHoratiu Vultur 
4258e4ed8ba0SHoratiu Vultur 	if (rq->perout.flags & ~PTP_PEROUT_DUTY_CYCLE)
4259e4ed8ba0SHoratiu Vultur 		return -EOPNOTSUPP;
4260e4ed8ba0SHoratiu Vultur 
4261e4ed8ba0SHoratiu Vultur 	pin = ptp_find_pin(ptp_priv->ptp_clock, PTP_PF_PEROUT, rq->perout.index);
4262e4ed8ba0SHoratiu Vultur 	if (pin == -1 || pin >= LAN8841_PTP_GPIO_NUM)
4263e4ed8ba0SHoratiu Vultur 		return -EINVAL;
4264e4ed8ba0SHoratiu Vultur 
4265e4ed8ba0SHoratiu Vultur 	if (!on) {
4266e4ed8ba0SHoratiu Vultur 		ret = lan8841_ptp_perout_off(ptp_priv, pin);
4267e4ed8ba0SHoratiu Vultur 		if (ret)
4268e4ed8ba0SHoratiu Vultur 			return ret;
4269e4ed8ba0SHoratiu Vultur 
4270e4ed8ba0SHoratiu Vultur 		return lan8841_ptp_remove_event(ptp_priv, LAN8841_EVENT_A, pin);
4271e4ed8ba0SHoratiu Vultur 	}
4272e4ed8ba0SHoratiu Vultur 
4273e4ed8ba0SHoratiu Vultur 	ts_on.tv_sec = rq->perout.on.sec;
4274e4ed8ba0SHoratiu Vultur 	ts_on.tv_nsec = rq->perout.on.nsec;
4275e4ed8ba0SHoratiu Vultur 	on_nsec = timespec64_to_ns(&ts_on);
4276e4ed8ba0SHoratiu Vultur 
4277e4ed8ba0SHoratiu Vultur 	ts_period.tv_sec = rq->perout.period.sec;
4278e4ed8ba0SHoratiu Vultur 	ts_period.tv_nsec = rq->perout.period.nsec;
4279e4ed8ba0SHoratiu Vultur 	period_nsec = timespec64_to_ns(&ts_period);
4280e4ed8ba0SHoratiu Vultur 
4281e4ed8ba0SHoratiu Vultur 	if (period_nsec < 200) {
42829bdf4489SColin Ian King 		pr_warn_ratelimited("%s: perout period too small, minimum is 200 nsec\n",
4283e4ed8ba0SHoratiu Vultur 				    phydev_name(phydev));
4284e4ed8ba0SHoratiu Vultur 		return -EOPNOTSUPP;
4285e4ed8ba0SHoratiu Vultur 	}
4286e4ed8ba0SHoratiu Vultur 
4287e4ed8ba0SHoratiu Vultur 	if (on_nsec >= period_nsec) {
4288e4ed8ba0SHoratiu Vultur 		pr_warn_ratelimited("%s: pulse width must be smaller than period\n",
4289e4ed8ba0SHoratiu Vultur 				    phydev_name(phydev));
4290e4ed8ba0SHoratiu Vultur 		return -EINVAL;
4291e4ed8ba0SHoratiu Vultur 	}
4292e4ed8ba0SHoratiu Vultur 
4293e4ed8ba0SHoratiu Vultur 	switch (on_nsec) {
4294e4ed8ba0SHoratiu Vultur 	case 200000000:
4295e4ed8ba0SHoratiu Vultur 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_200MS;
4296e4ed8ba0SHoratiu Vultur 		break;
4297e4ed8ba0SHoratiu Vultur 	case 100000000:
4298e4ed8ba0SHoratiu Vultur 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100MS;
4299e4ed8ba0SHoratiu Vultur 		break;
4300e4ed8ba0SHoratiu Vultur 	case 50000000:
4301e4ed8ba0SHoratiu Vultur 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_50MS;
4302e4ed8ba0SHoratiu Vultur 		break;
4303e4ed8ba0SHoratiu Vultur 	case 10000000:
4304e4ed8ba0SHoratiu Vultur 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_10MS;
4305e4ed8ba0SHoratiu Vultur 		break;
4306e4ed8ba0SHoratiu Vultur 	case 5000000:
4307e4ed8ba0SHoratiu Vultur 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_5MS;
4308e4ed8ba0SHoratiu Vultur 		break;
4309e4ed8ba0SHoratiu Vultur 	case 1000000:
4310e4ed8ba0SHoratiu Vultur 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_1MS;
4311e4ed8ba0SHoratiu Vultur 		break;
4312e4ed8ba0SHoratiu Vultur 	case 500000:
4313e4ed8ba0SHoratiu Vultur 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_500US;
4314e4ed8ba0SHoratiu Vultur 		break;
4315e4ed8ba0SHoratiu Vultur 	case 100000:
4316e4ed8ba0SHoratiu Vultur 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100US;
4317e4ed8ba0SHoratiu Vultur 		break;
4318e4ed8ba0SHoratiu Vultur 	case 50000:
4319e4ed8ba0SHoratiu Vultur 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_50US;
4320e4ed8ba0SHoratiu Vultur 		break;
4321e4ed8ba0SHoratiu Vultur 	case 10000:
4322e4ed8ba0SHoratiu Vultur 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_10US;
4323e4ed8ba0SHoratiu Vultur 		break;
4324e4ed8ba0SHoratiu Vultur 	case 5000:
4325e4ed8ba0SHoratiu Vultur 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_5US;
4326e4ed8ba0SHoratiu Vultur 		break;
4327e4ed8ba0SHoratiu Vultur 	case 1000:
4328e4ed8ba0SHoratiu Vultur 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_1US;
4329e4ed8ba0SHoratiu Vultur 		break;
4330e4ed8ba0SHoratiu Vultur 	case 500:
4331e4ed8ba0SHoratiu Vultur 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_500NS;
4332e4ed8ba0SHoratiu Vultur 		break;
4333e4ed8ba0SHoratiu Vultur 	case 100:
4334e4ed8ba0SHoratiu Vultur 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100NS;
4335e4ed8ba0SHoratiu Vultur 		break;
4336e4ed8ba0SHoratiu Vultur 	default:
4337e4ed8ba0SHoratiu Vultur 		pr_warn_ratelimited("%s: Use default duty cycle of 100ns\n",
4338e4ed8ba0SHoratiu Vultur 				    phydev_name(phydev));
4339e4ed8ba0SHoratiu Vultur 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100NS;
4340e4ed8ba0SHoratiu Vultur 		break;
4341e4ed8ba0SHoratiu Vultur 	}
4342e4ed8ba0SHoratiu Vultur 
4343e4ed8ba0SHoratiu Vultur 	mutex_lock(&ptp_priv->ptp_lock);
4344e4ed8ba0SHoratiu Vultur 	ret = lan8841_ptp_set_target(ptp_priv, LAN8841_EVENT_A, rq->perout.start.sec,
4345e4ed8ba0SHoratiu Vultur 				     rq->perout.start.nsec);
4346e4ed8ba0SHoratiu Vultur 	mutex_unlock(&ptp_priv->ptp_lock);
4347e4ed8ba0SHoratiu Vultur 	if (ret)
4348e4ed8ba0SHoratiu Vultur 		return ret;
4349e4ed8ba0SHoratiu Vultur 
4350e4ed8ba0SHoratiu Vultur 	ret = lan8841_ptp_set_reload(ptp_priv, LAN8841_EVENT_A, rq->perout.period.sec,
4351e4ed8ba0SHoratiu Vultur 				     rq->perout.period.nsec);
4352e4ed8ba0SHoratiu Vultur 	if (ret)
4353e4ed8ba0SHoratiu Vultur 		return ret;
4354e4ed8ba0SHoratiu Vultur 
4355e4ed8ba0SHoratiu Vultur 	ret = lan8841_ptp_enable_event(ptp_priv, pin, LAN8841_EVENT_A,
4356e4ed8ba0SHoratiu Vultur 				       pulse_width);
4357e4ed8ba0SHoratiu Vultur 	if (ret)
4358e4ed8ba0SHoratiu Vultur 		return ret;
4359e4ed8ba0SHoratiu Vultur 
4360e4ed8ba0SHoratiu Vultur 	ret = lan8841_ptp_perout_on(ptp_priv, pin);
4361e4ed8ba0SHoratiu Vultur 	if (ret)
4362e4ed8ba0SHoratiu Vultur 		lan8841_ptp_remove_event(ptp_priv, pin, LAN8841_EVENT_A);
4363e4ed8ba0SHoratiu Vultur 
4364e4ed8ba0SHoratiu Vultur 	return ret;
4365e4ed8ba0SHoratiu Vultur }
4366e4ed8ba0SHoratiu Vultur 
4367fac63186SHoratiu Vultur #define LAN8841_PTP_GPIO_CAP_EN			496
4368fac63186SHoratiu Vultur #define LAN8841_PTP_GPIO_CAP_EN_GPIO_RE_CAPTURE_ENABLE(gpio)	(BIT(gpio))
4369fac63186SHoratiu Vultur #define LAN8841_PTP_GPIO_CAP_EN_GPIO_FE_CAPTURE_ENABLE(gpio)	(BIT(gpio) << 8)
4370fac63186SHoratiu Vultur #define LAN8841_PTP_INT_EN_PTP_GPIO_CAP_EN	BIT(2)
4371fac63186SHoratiu Vultur 
4372fac63186SHoratiu Vultur static int lan8841_ptp_extts_on(struct kszphy_ptp_priv *ptp_priv, int pin,
4373fac63186SHoratiu Vultur 				u32 flags)
4374fac63186SHoratiu Vultur {
4375fac63186SHoratiu Vultur 	struct phy_device *phydev = ptp_priv->phydev;
4376fac63186SHoratiu Vultur 	u16 tmp = 0;
4377fac63186SHoratiu Vultur 	int ret;
4378fac63186SHoratiu Vultur 
4379fac63186SHoratiu Vultur 	/* Set GPIO to be intput */
4380fac63186SHoratiu Vultur 	ret = phy_set_bits_mmd(phydev, 2, LAN8841_GPIO_EN, BIT(pin));
4381fac63186SHoratiu Vultur 	if (ret)
4382fac63186SHoratiu Vultur 		return ret;
4383fac63186SHoratiu Vultur 
4384fac63186SHoratiu Vultur 	ret = phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_BUF, BIT(pin));
4385fac63186SHoratiu Vultur 	if (ret)
4386fac63186SHoratiu Vultur 		return ret;
4387fac63186SHoratiu Vultur 
4388fac63186SHoratiu Vultur 	/* Enable capture on the edges of the pin */
4389fac63186SHoratiu Vultur 	if (flags & PTP_RISING_EDGE)
4390fac63186SHoratiu Vultur 		tmp |= LAN8841_PTP_GPIO_CAP_EN_GPIO_RE_CAPTURE_ENABLE(pin);
4391fac63186SHoratiu Vultur 	if (flags & PTP_FALLING_EDGE)
4392fac63186SHoratiu Vultur 		tmp |= LAN8841_PTP_GPIO_CAP_EN_GPIO_FE_CAPTURE_ENABLE(pin);
4393fac63186SHoratiu Vultur 	ret = phy_write_mmd(phydev, 2, LAN8841_PTP_GPIO_CAP_EN, tmp);
4394fac63186SHoratiu Vultur 	if (ret)
4395fac63186SHoratiu Vultur 		return ret;
4396fac63186SHoratiu Vultur 
4397fac63186SHoratiu Vultur 	/* Enable interrupt */
4398fac63186SHoratiu Vultur 	return phy_modify_mmd(phydev, 2, LAN8841_PTP_INT_EN,
4399fac63186SHoratiu Vultur 			      LAN8841_PTP_INT_EN_PTP_GPIO_CAP_EN,
4400fac63186SHoratiu Vultur 			      LAN8841_PTP_INT_EN_PTP_GPIO_CAP_EN);
4401fac63186SHoratiu Vultur }
4402fac63186SHoratiu Vultur 
4403fac63186SHoratiu Vultur static int lan8841_ptp_extts_off(struct kszphy_ptp_priv *ptp_priv, int pin)
4404fac63186SHoratiu Vultur {
4405fac63186SHoratiu Vultur 	struct phy_device *phydev = ptp_priv->phydev;
4406fac63186SHoratiu Vultur 	int ret;
4407fac63186SHoratiu Vultur 
4408fac63186SHoratiu Vultur 	/* Set GPIO to be output */
4409fac63186SHoratiu Vultur 	ret = phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_EN, BIT(pin));
4410fac63186SHoratiu Vultur 	if (ret)
4411fac63186SHoratiu Vultur 		return ret;
4412fac63186SHoratiu Vultur 
4413fac63186SHoratiu Vultur 	ret = phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_BUF, BIT(pin));
4414fac63186SHoratiu Vultur 	if (ret)
4415fac63186SHoratiu Vultur 		return ret;
4416fac63186SHoratiu Vultur 
4417fac63186SHoratiu Vultur 	/* Disable capture on both of the edges */
4418fac63186SHoratiu Vultur 	ret = phy_modify_mmd(phydev, 2, LAN8841_PTP_GPIO_CAP_EN,
4419fac63186SHoratiu Vultur 			     LAN8841_PTP_GPIO_CAP_EN_GPIO_RE_CAPTURE_ENABLE(pin) |
4420fac63186SHoratiu Vultur 			     LAN8841_PTP_GPIO_CAP_EN_GPIO_FE_CAPTURE_ENABLE(pin),
4421fac63186SHoratiu Vultur 			     0);
4422fac63186SHoratiu Vultur 	if (ret)
4423fac63186SHoratiu Vultur 		return ret;
4424fac63186SHoratiu Vultur 
4425fac63186SHoratiu Vultur 	/* Disable interrupt */
4426fac63186SHoratiu Vultur 	return phy_modify_mmd(phydev, 2, LAN8841_PTP_INT_EN,
4427fac63186SHoratiu Vultur 			      LAN8841_PTP_INT_EN_PTP_GPIO_CAP_EN,
4428fac63186SHoratiu Vultur 			      0);
4429fac63186SHoratiu Vultur }
4430fac63186SHoratiu Vultur 
4431fac63186SHoratiu Vultur static int lan8841_ptp_extts(struct ptp_clock_info *ptp,
4432fac63186SHoratiu Vultur 			     struct ptp_clock_request *rq, int on)
4433fac63186SHoratiu Vultur {
4434fac63186SHoratiu Vultur 	struct kszphy_ptp_priv *ptp_priv = container_of(ptp, struct kszphy_ptp_priv,
4435fac63186SHoratiu Vultur 							ptp_clock_info);
4436fac63186SHoratiu Vultur 	int pin;
4437fac63186SHoratiu Vultur 	int ret;
4438fac63186SHoratiu Vultur 
4439fac63186SHoratiu Vultur 	/* Reject requests with unsupported flags */
4440fac63186SHoratiu Vultur 	if (rq->extts.flags & ~(PTP_ENABLE_FEATURE |
4441fac63186SHoratiu Vultur 				PTP_EXTTS_EDGES |
4442fac63186SHoratiu Vultur 				PTP_STRICT_FLAGS))
4443fac63186SHoratiu Vultur 		return -EOPNOTSUPP;
4444fac63186SHoratiu Vultur 
4445fac63186SHoratiu Vultur 	pin = ptp_find_pin(ptp_priv->ptp_clock, PTP_PF_EXTTS, rq->extts.index);
4446fac63186SHoratiu Vultur 	if (pin == -1 || pin >= LAN8841_PTP_GPIO_NUM)
4447fac63186SHoratiu Vultur 		return -EINVAL;
4448fac63186SHoratiu Vultur 
4449fac63186SHoratiu Vultur 	mutex_lock(&ptp_priv->ptp_lock);
4450fac63186SHoratiu Vultur 	if (on)
4451fac63186SHoratiu Vultur 		ret = lan8841_ptp_extts_on(ptp_priv, pin, rq->extts.flags);
4452fac63186SHoratiu Vultur 	else
4453fac63186SHoratiu Vultur 		ret = lan8841_ptp_extts_off(ptp_priv, pin);
4454fac63186SHoratiu Vultur 	mutex_unlock(&ptp_priv->ptp_lock);
4455fac63186SHoratiu Vultur 
4456fac63186SHoratiu Vultur 	return ret;
4457fac63186SHoratiu Vultur }
4458fac63186SHoratiu Vultur 
4459e4ed8ba0SHoratiu Vultur static int lan8841_ptp_enable(struct ptp_clock_info *ptp,
4460e4ed8ba0SHoratiu Vultur 			      struct ptp_clock_request *rq, int on)
4461e4ed8ba0SHoratiu Vultur {
4462e4ed8ba0SHoratiu Vultur 	switch (rq->type) {
4463fac63186SHoratiu Vultur 	case PTP_CLK_REQ_EXTTS:
4464fac63186SHoratiu Vultur 		return lan8841_ptp_extts(ptp, rq, on);
4465e4ed8ba0SHoratiu Vultur 	case PTP_CLK_REQ_PEROUT:
4466e4ed8ba0SHoratiu Vultur 		return lan8841_ptp_perout(ptp, rq, on);
4467e4ed8ba0SHoratiu Vultur 	default:
4468e4ed8ba0SHoratiu Vultur 		return -EOPNOTSUPP;
4469e4ed8ba0SHoratiu Vultur 	}
4470e4ed8ba0SHoratiu Vultur 
4471e4ed8ba0SHoratiu Vultur 	return 0;
4472e4ed8ba0SHoratiu Vultur }
4473e4ed8ba0SHoratiu Vultur 
4474*cc755495SHoratiu Vultur static long lan8841_ptp_do_aux_work(struct ptp_clock_info *ptp)
4475*cc755495SHoratiu Vultur {
4476*cc755495SHoratiu Vultur 	struct kszphy_ptp_priv *ptp_priv = container_of(ptp, struct kszphy_ptp_priv,
4477*cc755495SHoratiu Vultur 							ptp_clock_info);
4478*cc755495SHoratiu Vultur 	struct timespec64 ts;
4479*cc755495SHoratiu Vultur 	unsigned long flags;
4480*cc755495SHoratiu Vultur 
4481*cc755495SHoratiu Vultur 	lan8841_ptp_getseconds(&ptp_priv->ptp_clock_info, &ts);
4482*cc755495SHoratiu Vultur 
4483*cc755495SHoratiu Vultur 	spin_lock_irqsave(&ptp_priv->seconds_lock, flags);
4484*cc755495SHoratiu Vultur 	ptp_priv->seconds = ts.tv_sec;
4485*cc755495SHoratiu Vultur 	spin_unlock_irqrestore(&ptp_priv->seconds_lock, flags);
4486*cc755495SHoratiu Vultur 
4487*cc755495SHoratiu Vultur 	return nsecs_to_jiffies(LAN8841_GET_SEC_LTC_DELAY);
4488*cc755495SHoratiu Vultur }
4489*cc755495SHoratiu Vultur 
4490cafc3662SHoratiu Vultur static struct ptp_clock_info lan8841_ptp_clock_info = {
4491cafc3662SHoratiu Vultur 	.owner		= THIS_MODULE,
4492cafc3662SHoratiu Vultur 	.name		= "lan8841 ptp",
4493cafc3662SHoratiu Vultur 	.max_adj	= 31249999,
4494cafc3662SHoratiu Vultur 	.gettime64	= lan8841_ptp_gettime64,
4495cafc3662SHoratiu Vultur 	.settime64	= lan8841_ptp_settime64,
4496cafc3662SHoratiu Vultur 	.adjtime	= lan8841_ptp_adjtime,
4497cafc3662SHoratiu Vultur 	.adjfine	= lan8841_ptp_adjfine,
4498e4ed8ba0SHoratiu Vultur 	.verify         = lan8841_ptp_verify,
4499e4ed8ba0SHoratiu Vultur 	.enable         = lan8841_ptp_enable,
4500*cc755495SHoratiu Vultur 	.do_aux_work	= lan8841_ptp_do_aux_work,
4501e4ed8ba0SHoratiu Vultur 	.n_per_out      = LAN8841_PTP_GPIO_NUM,
4502fac63186SHoratiu Vultur 	.n_ext_ts       = LAN8841_PTP_GPIO_NUM,
4503e4ed8ba0SHoratiu Vultur 	.n_pins         = LAN8841_PTP_GPIO_NUM,
4504cafc3662SHoratiu Vultur };
4505cafc3662SHoratiu Vultur 
4506a8f1a19dSHoratiu Vultur #define LAN8841_OPERATION_MODE_STRAP_LOW_REGISTER 3
4507a8f1a19dSHoratiu Vultur #define LAN8841_OPERATION_MODE_STRAP_LOW_REGISTER_STRAP_RGMII_EN BIT(0)
4508a8f1a19dSHoratiu Vultur 
4509a8f1a19dSHoratiu Vultur static int lan8841_probe(struct phy_device *phydev)
4510a8f1a19dSHoratiu Vultur {
4511cafc3662SHoratiu Vultur 	struct kszphy_ptp_priv *ptp_priv;
4512cafc3662SHoratiu Vultur 	struct kszphy_priv *priv;
4513a8f1a19dSHoratiu Vultur 	int err;
4514a8f1a19dSHoratiu Vultur 
4515a8f1a19dSHoratiu Vultur 	err = kszphy_probe(phydev);
4516a8f1a19dSHoratiu Vultur 	if (err)
4517a8f1a19dSHoratiu Vultur 		return err;
4518a8f1a19dSHoratiu Vultur 
4519a8f1a19dSHoratiu Vultur 	if (phy_read_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
4520a8f1a19dSHoratiu Vultur 			 LAN8841_OPERATION_MODE_STRAP_LOW_REGISTER) &
4521a8f1a19dSHoratiu Vultur 	    LAN8841_OPERATION_MODE_STRAP_LOW_REGISTER_STRAP_RGMII_EN)
4522a8f1a19dSHoratiu Vultur 		phydev->interface = PHY_INTERFACE_MODE_RGMII_RXID;
4523a8f1a19dSHoratiu Vultur 
4524cafc3662SHoratiu Vultur 	/* Register the clock */
4525cafc3662SHoratiu Vultur 	if (!IS_ENABLED(CONFIG_NETWORK_PHY_TIMESTAMPING))
4526cafc3662SHoratiu Vultur 		return 0;
4527cafc3662SHoratiu Vultur 
4528cafc3662SHoratiu Vultur 	priv = phydev->priv;
4529cafc3662SHoratiu Vultur 	ptp_priv = &priv->ptp_priv;
4530cafc3662SHoratiu Vultur 
4531e4ed8ba0SHoratiu Vultur 	ptp_priv->pin_config = devm_kcalloc(&phydev->mdio.dev,
4532e4ed8ba0SHoratiu Vultur 					    LAN8841_PTP_GPIO_NUM,
4533e4ed8ba0SHoratiu Vultur 					    sizeof(*ptp_priv->pin_config),
4534e4ed8ba0SHoratiu Vultur 					    GFP_KERNEL);
4535e4ed8ba0SHoratiu Vultur 	if (!ptp_priv->pin_config)
4536e4ed8ba0SHoratiu Vultur 		return -ENOMEM;
4537e4ed8ba0SHoratiu Vultur 
4538e4ed8ba0SHoratiu Vultur 	for (int i = 0; i < LAN8841_PTP_GPIO_NUM; ++i) {
4539e4ed8ba0SHoratiu Vultur 		struct ptp_pin_desc *p = &ptp_priv->pin_config[i];
4540e4ed8ba0SHoratiu Vultur 
4541e4ed8ba0SHoratiu Vultur 		snprintf(p->name, sizeof(p->name), "pin%d", i);
4542e4ed8ba0SHoratiu Vultur 		p->index = i;
4543e4ed8ba0SHoratiu Vultur 		p->func = PTP_PF_NONE;
4544e4ed8ba0SHoratiu Vultur 	}
4545e4ed8ba0SHoratiu Vultur 
4546cafc3662SHoratiu Vultur 	ptp_priv->ptp_clock_info = lan8841_ptp_clock_info;
4547e4ed8ba0SHoratiu Vultur 	ptp_priv->ptp_clock_info.pin_config = ptp_priv->pin_config;
4548cafc3662SHoratiu Vultur 	ptp_priv->ptp_clock = ptp_clock_register(&ptp_priv->ptp_clock_info,
4549cafc3662SHoratiu Vultur 						 &phydev->mdio.dev);
4550cafc3662SHoratiu Vultur 	if (IS_ERR(ptp_priv->ptp_clock)) {
4551cafc3662SHoratiu Vultur 		phydev_err(phydev, "ptp_clock_register failed: %lu\n",
4552cafc3662SHoratiu Vultur 			   PTR_ERR(ptp_priv->ptp_clock));
4553cafc3662SHoratiu Vultur 		return -EINVAL;
4554cafc3662SHoratiu Vultur 	}
4555cafc3662SHoratiu Vultur 
4556cafc3662SHoratiu Vultur 	if (!ptp_priv->ptp_clock)
4557cafc3662SHoratiu Vultur 		return 0;
4558cafc3662SHoratiu Vultur 
4559cafc3662SHoratiu Vultur 	/* Initialize the SW */
4560cafc3662SHoratiu Vultur 	skb_queue_head_init(&ptp_priv->tx_queue);
4561cafc3662SHoratiu Vultur 	ptp_priv->phydev = phydev;
4562cafc3662SHoratiu Vultur 	mutex_init(&ptp_priv->ptp_lock);
4563*cc755495SHoratiu Vultur 	spin_lock_init(&ptp_priv->seconds_lock);
4564cafc3662SHoratiu Vultur 
4565*cc755495SHoratiu Vultur 	ptp_priv->mii_ts.rxtstamp = lan8841_rxtstamp;
4566cafc3662SHoratiu Vultur 	ptp_priv->mii_ts.txtstamp = lan8814_txtstamp;
4567cafc3662SHoratiu Vultur 	ptp_priv->mii_ts.hwtstamp = lan8841_hwtstamp;
4568cafc3662SHoratiu Vultur 	ptp_priv->mii_ts.ts_info = lan8841_ts_info;
4569cafc3662SHoratiu Vultur 
4570cafc3662SHoratiu Vultur 	phydev->mii_ts = &ptp_priv->mii_ts;
4571cafc3662SHoratiu Vultur 
4572a8f1a19dSHoratiu Vultur 	return 0;
4573a8f1a19dSHoratiu Vultur }
4574a8f1a19dSHoratiu Vultur 
4575*cc755495SHoratiu Vultur static int lan8841_suspend(struct phy_device *phydev)
4576*cc755495SHoratiu Vultur {
4577*cc755495SHoratiu Vultur 	struct kszphy_priv *priv = phydev->priv;
4578*cc755495SHoratiu Vultur 	struct kszphy_ptp_priv *ptp_priv = &priv->ptp_priv;
4579*cc755495SHoratiu Vultur 
4580*cc755495SHoratiu Vultur 	ptp_cancel_worker_sync(ptp_priv->ptp_clock);
4581*cc755495SHoratiu Vultur 
4582*cc755495SHoratiu Vultur 	return genphy_suspend(phydev);
4583*cc755495SHoratiu Vultur }
4584*cc755495SHoratiu Vultur 
4585d5bf9071SChristian Hohnstaedt static struct phy_driver ksphy_driver[] = {
4586d5bf9071SChristian Hohnstaedt {
458751f932c4SChoi, David 	.phy_id		= PHY_ID_KS8737,
4588f893a99eSFabio Estevam 	.phy_id_mask	= MICREL_PHY_ID_MASK,
458951f932c4SChoi, David 	.name		= "Micrel KS8737",
4590dcdecdcfSHeiner Kallweit 	/* PHY_BASIC_FEATURES */
4591c6f9575cSJohan Hovold 	.driver_data	= &ks8737_type,
459215f03ffeSFabio Estevam 	.probe		= kszphy_probe,
4593d0507009SDavid J. Choi 	.config_init	= kszphy_config_init,
4594c6f9575cSJohan Hovold 	.config_intr	= kszphy_config_intr,
459559ca4e58SIoana Ciornei 	.handle_interrupt = kszphy_handle_interrupt,
4596f1131b9cSClaudiu Beznea 	.suspend	= kszphy_suspend,
4597f1131b9cSClaudiu Beznea 	.resume		= kszphy_resume,
4598d5bf9071SChristian Hohnstaedt }, {
4599212ea99aSMarek Vasut 	.phy_id		= PHY_ID_KSZ8021,
4600212ea99aSMarek Vasut 	.phy_id_mask	= 0x00ffffff,
46017ab59dc1SDavid J. Choi 	.name		= "Micrel KSZ8021 or KSZ8031",
4602dcdecdcfSHeiner Kallweit 	/* PHY_BASIC_FEATURES */
4603e6a423a8SJohan Hovold 	.driver_data	= &ksz8021_type,
460463f44b2bSJohan Hovold 	.probe		= kszphy_probe,
4605d0e1df9cSJohan Hovold 	.config_init	= kszphy_config_init,
4606212ea99aSMarek Vasut 	.config_intr	= kszphy_config_intr,
460759ca4e58SIoana Ciornei 	.handle_interrupt = kszphy_handle_interrupt,
46082b2427d0SAndrew Lunn 	.get_sset_count = kszphy_get_sset_count,
46092b2427d0SAndrew Lunn 	.get_strings	= kszphy_get_strings,
46102b2427d0SAndrew Lunn 	.get_stats	= kszphy_get_stats,
4611f1131b9cSClaudiu Beznea 	.suspend	= kszphy_suspend,
4612f1131b9cSClaudiu Beznea 	.resume		= kszphy_resume,
4613212ea99aSMarek Vasut }, {
4614b818d1a7SHector Palacios 	.phy_id		= PHY_ID_KSZ8031,
4615b818d1a7SHector Palacios 	.phy_id_mask	= 0x00ffffff,
4616b818d1a7SHector Palacios 	.name		= "Micrel KSZ8031",
4617dcdecdcfSHeiner Kallweit 	/* PHY_BASIC_FEATURES */
4618e6a423a8SJohan Hovold 	.driver_data	= &ksz8021_type,
461963f44b2bSJohan Hovold 	.probe		= kszphy_probe,
4620d0e1df9cSJohan Hovold 	.config_init	= kszphy_config_init,
4621b818d1a7SHector Palacios 	.config_intr	= kszphy_config_intr,
462259ca4e58SIoana Ciornei 	.handle_interrupt = kszphy_handle_interrupt,
46232b2427d0SAndrew Lunn 	.get_sset_count = kszphy_get_sset_count,
46242b2427d0SAndrew Lunn 	.get_strings	= kszphy_get_strings,
46252b2427d0SAndrew Lunn 	.get_stats	= kszphy_get_stats,
4626f1131b9cSClaudiu Beznea 	.suspend	= kszphy_suspend,
4627f1131b9cSClaudiu Beznea 	.resume		= kszphy_resume,
4628b818d1a7SHector Palacios }, {
4629510d573fSMarek Vasut 	.phy_id		= PHY_ID_KSZ8041,
4630f893a99eSFabio Estevam 	.phy_id_mask	= MICREL_PHY_ID_MASK,
4631510d573fSMarek Vasut 	.name		= "Micrel KSZ8041",
4632dcdecdcfSHeiner Kallweit 	/* PHY_BASIC_FEATURES */
4633e6a423a8SJohan Hovold 	.driver_data	= &ksz8041_type,
4634e6a423a8SJohan Hovold 	.probe		= kszphy_probe,
463577501a79SPhilipp Zabel 	.config_init	= ksz8041_config_init,
463677501a79SPhilipp Zabel 	.config_aneg	= ksz8041_config_aneg,
463751f932c4SChoi, David 	.config_intr	= kszphy_config_intr,
463859ca4e58SIoana Ciornei 	.handle_interrupt = kszphy_handle_interrupt,
46392b2427d0SAndrew Lunn 	.get_sset_count = kszphy_get_sset_count,
46402b2427d0SAndrew Lunn 	.get_strings	= kszphy_get_strings,
46412b2427d0SAndrew Lunn 	.get_stats	= kszphy_get_stats,
46422641b62dSStefan Agner 	/* No suspend/resume callbacks because of errata DS80000700A,
46432641b62dSStefan Agner 	 * receiver error following software power down.
46442641b62dSStefan Agner 	 */
4645d5bf9071SChristian Hohnstaedt }, {
46464bd7b512SSergei Shtylyov 	.phy_id		= PHY_ID_KSZ8041RNLI,
4647f893a99eSFabio Estevam 	.phy_id_mask	= MICREL_PHY_ID_MASK,
46484bd7b512SSergei Shtylyov 	.name		= "Micrel KSZ8041RNLI",
4649dcdecdcfSHeiner Kallweit 	/* PHY_BASIC_FEATURES */
4650e6a423a8SJohan Hovold 	.driver_data	= &ksz8041_type,
4651e6a423a8SJohan Hovold 	.probe		= kszphy_probe,
4652e6a423a8SJohan Hovold 	.config_init	= kszphy_config_init,
46534bd7b512SSergei Shtylyov 	.config_intr	= kszphy_config_intr,
465459ca4e58SIoana Ciornei 	.handle_interrupt = kszphy_handle_interrupt,
46552b2427d0SAndrew Lunn 	.get_sset_count = kszphy_get_sset_count,
46562b2427d0SAndrew Lunn 	.get_strings	= kszphy_get_strings,
46572b2427d0SAndrew Lunn 	.get_stats	= kszphy_get_stats,
4658f1131b9cSClaudiu Beznea 	.suspend	= kszphy_suspend,
4659f1131b9cSClaudiu Beznea 	.resume		= kszphy_resume,
46604bd7b512SSergei Shtylyov }, {
4661510d573fSMarek Vasut 	.name		= "Micrel KSZ8051",
4662dcdecdcfSHeiner Kallweit 	/* PHY_BASIC_FEATURES */
4663e6a423a8SJohan Hovold 	.driver_data	= &ksz8051_type,
4664e6a423a8SJohan Hovold 	.probe		= kszphy_probe,
466563f44b2bSJohan Hovold 	.config_init	= kszphy_config_init,
466651f932c4SChoi, David 	.config_intr	= kszphy_config_intr,
466759ca4e58SIoana Ciornei 	.handle_interrupt = kszphy_handle_interrupt,
46682b2427d0SAndrew Lunn 	.get_sset_count = kszphy_get_sset_count,
46692b2427d0SAndrew Lunn 	.get_strings	= kszphy_get_strings,
46702b2427d0SAndrew Lunn 	.get_stats	= kszphy_get_stats,
46718b95599cSMarek Vasut 	.match_phy_device = ksz8051_match_phy_device,
4672f1131b9cSClaudiu Beznea 	.suspend	= kszphy_suspend,
4673f1131b9cSClaudiu Beznea 	.resume		= kszphy_resume,
4674d5bf9071SChristian Hohnstaedt }, {
4675510d573fSMarek Vasut 	.phy_id		= PHY_ID_KSZ8001,
4676510d573fSMarek Vasut 	.name		= "Micrel KSZ8001 or KS8721",
4677ecd5a323SAlexander Stein 	.phy_id_mask	= 0x00fffffc,
4678dcdecdcfSHeiner Kallweit 	/* PHY_BASIC_FEATURES */
4679e6a423a8SJohan Hovold 	.driver_data	= &ksz8041_type,
4680e6a423a8SJohan Hovold 	.probe		= kszphy_probe,
4681e6a423a8SJohan Hovold 	.config_init	= kszphy_config_init,
468251f932c4SChoi, David 	.config_intr	= kszphy_config_intr,
468359ca4e58SIoana Ciornei 	.handle_interrupt = kszphy_handle_interrupt,
46842b2427d0SAndrew Lunn 	.get_sset_count = kszphy_get_sset_count,
46852b2427d0SAndrew Lunn 	.get_strings	= kszphy_get_strings,
46862b2427d0SAndrew Lunn 	.get_stats	= kszphy_get_stats,
4687f1131b9cSClaudiu Beznea 	.suspend	= kszphy_suspend,
4688f1131b9cSClaudiu Beznea 	.resume		= kszphy_resume,
4689d5bf9071SChristian Hohnstaedt }, {
46907ab59dc1SDavid J. Choi 	.phy_id		= PHY_ID_KSZ8081,
46917ab59dc1SDavid J. Choi 	.name		= "Micrel KSZ8081 or KSZ8091",
4692f893a99eSFabio Estevam 	.phy_id_mask	= MICREL_PHY_ID_MASK,
469349011e0cSOleksij Rempel 	.flags		= PHY_POLL_CABLE_TEST,
4694dcdecdcfSHeiner Kallweit 	/* PHY_BASIC_FEATURES */
4695e6a423a8SJohan Hovold 	.driver_data	= &ksz8081_type,
4696e6a423a8SJohan Hovold 	.probe		= kszphy_probe,
46977a1d8390SAntoine Tenart 	.config_init	= ksz8081_config_init,
4698764d31caSChristian Melki 	.soft_reset	= genphy_soft_reset,
4699f873f112SOleksij Rempel 	.config_aneg	= ksz8081_config_aneg,
4700f873f112SOleksij Rempel 	.read_status	= ksz8081_read_status,
47017ab59dc1SDavid J. Choi 	.config_intr	= kszphy_config_intr,
470259ca4e58SIoana Ciornei 	.handle_interrupt = kszphy_handle_interrupt,
47032b2427d0SAndrew Lunn 	.get_sset_count = kszphy_get_sset_count,
47042b2427d0SAndrew Lunn 	.get_strings	= kszphy_get_strings,
47052b2427d0SAndrew Lunn 	.get_stats	= kszphy_get_stats,
4706836384d2SWenyou Yang 	.suspend	= kszphy_suspend,
4707f5aba91dSAlexandre Belloni 	.resume		= kszphy_resume,
470849011e0cSOleksij Rempel 	.cable_test_start	= ksz886x_cable_test_start,
470949011e0cSOleksij Rempel 	.cable_test_get_status	= ksz886x_cable_test_get_status,
47107ab59dc1SDavid J. Choi }, {
47117ab59dc1SDavid J. Choi 	.phy_id		= PHY_ID_KSZ8061,
47127ab59dc1SDavid J. Choi 	.name		= "Micrel KSZ8061",
4713f893a99eSFabio Estevam 	.phy_id_mask	= MICREL_PHY_ID_MASK,
4714dcdecdcfSHeiner Kallweit 	/* PHY_BASIC_FEATURES */
47158e6004dfSFabio Estevam 	.probe		= kszphy_probe,
4716232ba3a5SRajasingh Thavamani 	.config_init	= ksz8061_config_init,
47177ab59dc1SDavid J. Choi 	.config_intr	= kszphy_config_intr,
471859ca4e58SIoana Ciornei 	.handle_interrupt = kszphy_handle_interrupt,
47198e6004dfSFabio Estevam 	.suspend	= kszphy_suspend,
47208e6004dfSFabio Estevam 	.resume		= kszphy_resume,
47217ab59dc1SDavid J. Choi }, {
4722d0507009SDavid J. Choi 	.phy_id		= PHY_ID_KSZ9021,
472348d7d0adSJason Wang 	.phy_id_mask	= 0x000ffffe,
4724d0507009SDavid J. Choi 	.name		= "Micrel KSZ9021 Gigabit PHY",
4725dcdecdcfSHeiner Kallweit 	/* PHY_GBIT_FEATURES */
4726c6f9575cSJohan Hovold 	.driver_data	= &ksz9021_type,
4727bfe72442SGrygorii Strashko 	.probe		= kszphy_probe,
4728407d8098SHans Andersson 	.get_features	= ksz9031_get_features,
4729954c3967SSean Cross 	.config_init	= ksz9021_config_init,
4730c6f9575cSJohan Hovold 	.config_intr	= kszphy_config_intr,
473159ca4e58SIoana Ciornei 	.handle_interrupt = kszphy_handle_interrupt,
47322b2427d0SAndrew Lunn 	.get_sset_count = kszphy_get_sset_count,
47332b2427d0SAndrew Lunn 	.get_strings	= kszphy_get_strings,
47342b2427d0SAndrew Lunn 	.get_stats	= kszphy_get_stats,
4735f1131b9cSClaudiu Beznea 	.suspend	= kszphy_suspend,
4736f1131b9cSClaudiu Beznea 	.resume		= kszphy_resume,
4737c846a2b7SKevin Hao 	.read_mmd	= genphy_read_mmd_unsupported,
4738c846a2b7SKevin Hao 	.write_mmd	= genphy_write_mmd_unsupported,
473993272e07SJean-Christophe PLAGNIOL-VILLARD }, {
47407ab59dc1SDavid J. Choi 	.phy_id		= PHY_ID_KSZ9031,
4741f893a99eSFabio Estevam 	.phy_id_mask	= MICREL_PHY_ID_MASK,
47427ab59dc1SDavid J. Choi 	.name		= "Micrel KSZ9031 Gigabit PHY",
474358389c00SMarek Vasut 	.flags		= PHY_POLL_CABLE_TEST,
4744c6f9575cSJohan Hovold 	.driver_data	= &ksz9021_type,
4745bfe72442SGrygorii Strashko 	.probe		= kszphy_probe,
47463aed3e2aSAntoine Tenart 	.get_features	= ksz9031_get_features,
47476e4b8273SHubert Chaumette 	.config_init	= ksz9031_config_init,
47481d16073aSHeiner Kallweit 	.soft_reset	= genphy_soft_reset,
4749d2fd719bSNathan Sullivan 	.read_status	= ksz9031_read_status,
4750c6f9575cSJohan Hovold 	.config_intr	= kszphy_config_intr,
475159ca4e58SIoana Ciornei 	.handle_interrupt = kszphy_handle_interrupt,
47522b2427d0SAndrew Lunn 	.get_sset_count = kszphy_get_sset_count,
47532b2427d0SAndrew Lunn 	.get_strings	= kszphy_get_strings,
47542b2427d0SAndrew Lunn 	.get_stats	= kszphy_get_stats,
4755f1131b9cSClaudiu Beznea 	.suspend	= kszphy_suspend,
4756f64f1482SXander Huff 	.resume		= kszphy_resume,
475758389c00SMarek Vasut 	.cable_test_start	= ksz9x31_cable_test_start,
475858389c00SMarek Vasut 	.cable_test_get_status	= ksz9x31_cable_test_get_status,
47597ab59dc1SDavid J. Choi }, {
47601623ad8eSDivya Koppera 	.phy_id		= PHY_ID_LAN8814,
47611623ad8eSDivya Koppera 	.phy_id_mask	= MICREL_PHY_ID_MASK,
47621623ad8eSDivya Koppera 	.name		= "Microchip INDY Gigabit Quad PHY",
476321b688daSDivya Koppera 	.flags          = PHY_POLL_CABLE_TEST,
47647467d716SHoratiu Vultur 	.config_init	= lan8814_config_init,
4765a516b7f7SDivya Koppera 	.driver_data	= &lan8814_type,
4766ece19502SDivya Koppera 	.probe		= lan8814_probe,
47671623ad8eSDivya Koppera 	.soft_reset	= genphy_soft_reset,
4768b814403aSHoratiu Vultur 	.read_status	= ksz9031_read_status,
47691623ad8eSDivya Koppera 	.get_sset_count	= kszphy_get_sset_count,
47701623ad8eSDivya Koppera 	.get_strings	= kszphy_get_strings,
47711623ad8eSDivya Koppera 	.get_stats	= kszphy_get_stats,
47721623ad8eSDivya Koppera 	.suspend	= genphy_suspend,
47731623ad8eSDivya Koppera 	.resume		= kszphy_resume,
4774b3ec7248SDivya Koppera 	.config_intr	= lan8814_config_intr,
4775b3ec7248SDivya Koppera 	.handle_interrupt = lan8814_handle_interrupt,
477621b688daSDivya Koppera 	.cable_test_start	= lan8814_cable_test_start,
477721b688daSDivya Koppera 	.cable_test_get_status	= ksz886x_cable_test_get_status,
47781623ad8eSDivya Koppera }, {
47797c2dcfa2SHoratiu Vultur 	.phy_id		= PHY_ID_LAN8804,
47807c2dcfa2SHoratiu Vultur 	.phy_id_mask	= MICREL_PHY_ID_MASK,
47817c2dcfa2SHoratiu Vultur 	.name		= "Microchip LAN966X Gigabit PHY",
47827c2dcfa2SHoratiu Vultur 	.config_init	= lan8804_config_init,
47837c2dcfa2SHoratiu Vultur 	.driver_data	= &ksz9021_type,
47847c2dcfa2SHoratiu Vultur 	.probe		= kszphy_probe,
47857c2dcfa2SHoratiu Vultur 	.soft_reset	= genphy_soft_reset,
47867c2dcfa2SHoratiu Vultur 	.read_status	= ksz9031_read_status,
47877c2dcfa2SHoratiu Vultur 	.get_sset_count	= kszphy_get_sset_count,
47887c2dcfa2SHoratiu Vultur 	.get_strings	= kszphy_get_strings,
47897c2dcfa2SHoratiu Vultur 	.get_stats	= kszphy_get_stats,
47907c2dcfa2SHoratiu Vultur 	.suspend	= genphy_suspend,
47917c2dcfa2SHoratiu Vultur 	.resume		= kszphy_resume,
4792b324c6e5SHoratiu Vultur 	.config_intr	= lan8804_config_intr,
4793b324c6e5SHoratiu Vultur 	.handle_interrupt = lan8804_handle_interrupt,
47947c2dcfa2SHoratiu Vultur }, {
4795a8f1a19dSHoratiu Vultur 	.phy_id		= PHY_ID_LAN8841,
4796a8f1a19dSHoratiu Vultur 	.phy_id_mask	= MICREL_PHY_ID_MASK,
4797a8f1a19dSHoratiu Vultur 	.name		= "Microchip LAN8841 Gigabit PHY",
4798a136391aSHoratiu Vultur 	.flags		= PHY_POLL_CABLE_TEST,
4799a8f1a19dSHoratiu Vultur 	.driver_data	= &lan8841_type,
4800a8f1a19dSHoratiu Vultur 	.config_init	= lan8841_config_init,
4801a8f1a19dSHoratiu Vultur 	.probe		= lan8841_probe,
4802a8f1a19dSHoratiu Vultur 	.soft_reset	= genphy_soft_reset,
4803a8f1a19dSHoratiu Vultur 	.config_intr	= lan8841_config_intr,
4804a8f1a19dSHoratiu Vultur 	.handle_interrupt = lan8841_handle_interrupt,
4805a8f1a19dSHoratiu Vultur 	.get_sset_count = kszphy_get_sset_count,
4806a8f1a19dSHoratiu Vultur 	.get_strings	= kszphy_get_strings,
4807a8f1a19dSHoratiu Vultur 	.get_stats	= kszphy_get_stats,
4808*cc755495SHoratiu Vultur 	.suspend	= lan8841_suspend,
4809a8f1a19dSHoratiu Vultur 	.resume		= genphy_resume,
4810a136391aSHoratiu Vultur 	.cable_test_start	= lan8814_cable_test_start,
4811a136391aSHoratiu Vultur 	.cable_test_get_status	= ksz886x_cable_test_get_status,
4812a8f1a19dSHoratiu Vultur }, {
4813bff5b4b3SYuiko Oshino 	.phy_id		= PHY_ID_KSZ9131,
4814bff5b4b3SYuiko Oshino 	.phy_id_mask	= MICREL_PHY_ID_MASK,
4815bff5b4b3SYuiko Oshino 	.name		= "Microchip KSZ9131 Gigabit PHY",
4816dcdecdcfSHeiner Kallweit 	/* PHY_GBIT_FEATURES */
481758389c00SMarek Vasut 	.flags		= PHY_POLL_CABLE_TEST,
4818a8f1a19dSHoratiu Vultur 	.driver_data	= &ksz9131_type,
4819bff5b4b3SYuiko Oshino 	.probe		= kszphy_probe,
4820bff5b4b3SYuiko Oshino 	.config_init	= ksz9131_config_init,
4821bff5b4b3SYuiko Oshino 	.config_intr	= kszphy_config_intr,
4822b64e6a87SRaju Lakkaraju 	.config_aneg	= ksz9131_config_aneg,
4823b64e6a87SRaju Lakkaraju 	.read_status	= ksz9131_read_status,
482459ca4e58SIoana Ciornei 	.handle_interrupt = kszphy_handle_interrupt,
4825bff5b4b3SYuiko Oshino 	.get_sset_count = kszphy_get_sset_count,
4826bff5b4b3SYuiko Oshino 	.get_strings	= kszphy_get_strings,
4827bff5b4b3SYuiko Oshino 	.get_stats	= kszphy_get_stats,
4828f1131b9cSClaudiu Beznea 	.suspend	= kszphy_suspend,
4829bff5b4b3SYuiko Oshino 	.resume		= kszphy_resume,
483058389c00SMarek Vasut 	.cable_test_start	= ksz9x31_cable_test_start,
483158389c00SMarek Vasut 	.cable_test_get_status	= ksz9x31_cable_test_get_status,
4832f2e9d083SOleksij Rempel 	.get_features	= ksz9477_get_features,
4833bff5b4b3SYuiko Oshino }, {
483493272e07SJean-Christophe PLAGNIOL-VILLARD 	.phy_id		= PHY_ID_KSZ8873MLL,
4835f893a99eSFabio Estevam 	.phy_id_mask	= MICREL_PHY_ID_MASK,
483693272e07SJean-Christophe PLAGNIOL-VILLARD 	.name		= "Micrel KSZ8873MLL Switch",
4837dcdecdcfSHeiner Kallweit 	/* PHY_BASIC_FEATURES */
483893272e07SJean-Christophe PLAGNIOL-VILLARD 	.config_init	= kszphy_config_init,
483993272e07SJean-Christophe PLAGNIOL-VILLARD 	.config_aneg	= ksz8873mll_config_aneg,
484093272e07SJean-Christophe PLAGNIOL-VILLARD 	.read_status	= ksz8873mll_read_status,
48411a5465f5SPatrice Vilchez 	.suspend	= genphy_suspend,
48421a5465f5SPatrice Vilchez 	.resume		= genphy_resume,
48437ab59dc1SDavid J. Choi }, {
48447ab59dc1SDavid J. Choi 	.phy_id		= PHY_ID_KSZ886X,
4845f893a99eSFabio Estevam 	.phy_id_mask	= MICREL_PHY_ID_MASK,
4846ab36a3a2SMarek Vasut 	.name		= "Micrel KSZ8851 Ethernet MAC or KSZ886X Switch",
484721b688daSDivya Koppera 	.driver_data	= &ksz886x_type,
4848dcdecdcfSHeiner Kallweit 	/* PHY_BASIC_FEATURES */
484949011e0cSOleksij Rempel 	.flags		= PHY_POLL_CABLE_TEST,
48507ab59dc1SDavid J. Choi 	.config_init	= kszphy_config_init,
485152939393SOleksij Rempel 	.config_aneg	= ksz886x_config_aneg,
485252939393SOleksij Rempel 	.read_status	= ksz886x_read_status,
48531a5465f5SPatrice Vilchez 	.suspend	= genphy_suspend,
48541a5465f5SPatrice Vilchez 	.resume		= genphy_resume,
485549011e0cSOleksij Rempel 	.cable_test_start	= ksz886x_cable_test_start,
485649011e0cSOleksij Rempel 	.cable_test_get_status	= ksz886x_cable_test_get_status,
48579d162ed6SSean Nyekjaer }, {
48581d951ba3SMarek Vasut 	.name		= "Micrel KSZ87XX Switch",
4859dcdecdcfSHeiner Kallweit 	/* PHY_BASIC_FEATURES */
48609d162ed6SSean Nyekjaer 	.config_init	= kszphy_config_init,
48618b95599cSMarek Vasut 	.match_phy_device = ksz8795_match_phy_device,
48629d162ed6SSean Nyekjaer 	.suspend	= genphy_suspend,
48639d162ed6SSean Nyekjaer 	.resume		= genphy_resume,
4864fc3973a1SWoojung Huh }, {
4865fc3973a1SWoojung Huh 	.phy_id		= PHY_ID_KSZ9477,
4866fc3973a1SWoojung Huh 	.phy_id_mask	= MICREL_PHY_ID_MASK,
4867fc3973a1SWoojung Huh 	.name		= "Microchip KSZ9477",
4868dcdecdcfSHeiner Kallweit 	/* PHY_GBIT_FEATURES */
486926dd2974SRobert Hancock 	.config_init	= ksz9477_config_init,
4870db45c76bSArun Ramadoss 	.config_intr	= kszphy_config_intr,
4871db45c76bSArun Ramadoss 	.handle_interrupt = kszphy_handle_interrupt,
4872fc3973a1SWoojung Huh 	.suspend	= genphy_suspend,
4873fc3973a1SWoojung Huh 	.resume		= genphy_resume,
487448fb1994SOleksij Rempel 	.get_features	= ksz9477_get_features,
4875d5bf9071SChristian Hohnstaedt } };
4876d0507009SDavid J. Choi 
487750fd7150SJohan Hovold module_phy_driver(ksphy_driver);
4878d0507009SDavid J. Choi 
4879d0507009SDavid J. Choi MODULE_DESCRIPTION("Micrel PHY driver");
4880d0507009SDavid J. Choi MODULE_AUTHOR("David J. Choi");
4881d0507009SDavid J. Choi MODULE_LICENSE("GPL");
488252a60ed2SDavid S. Miller 
4883cf93c945SUwe Kleine-König static struct mdio_device_id __maybe_unused micrel_tbl[] = {
488448d7d0adSJason Wang 	{ PHY_ID_KSZ9021, 0x000ffffe },
4885f893a99eSFabio Estevam 	{ PHY_ID_KSZ9031, MICREL_PHY_ID_MASK },
4886bff5b4b3SYuiko Oshino 	{ PHY_ID_KSZ9131, MICREL_PHY_ID_MASK },
4887ecd5a323SAlexander Stein 	{ PHY_ID_KSZ8001, 0x00fffffc },
4888f893a99eSFabio Estevam 	{ PHY_ID_KS8737, MICREL_PHY_ID_MASK },
4889212ea99aSMarek Vasut 	{ PHY_ID_KSZ8021, 0x00ffffff },
4890b818d1a7SHector Palacios 	{ PHY_ID_KSZ8031, 0x00ffffff },
4891f893a99eSFabio Estevam 	{ PHY_ID_KSZ8041, MICREL_PHY_ID_MASK },
4892f893a99eSFabio Estevam 	{ PHY_ID_KSZ8051, MICREL_PHY_ID_MASK },
4893f893a99eSFabio Estevam 	{ PHY_ID_KSZ8061, MICREL_PHY_ID_MASK },
4894f893a99eSFabio Estevam 	{ PHY_ID_KSZ8081, MICREL_PHY_ID_MASK },
4895f893a99eSFabio Estevam 	{ PHY_ID_KSZ8873MLL, MICREL_PHY_ID_MASK },
4896f893a99eSFabio Estevam 	{ PHY_ID_KSZ886X, MICREL_PHY_ID_MASK },
48971623ad8eSDivya Koppera 	{ PHY_ID_LAN8814, MICREL_PHY_ID_MASK },
48987c2dcfa2SHoratiu Vultur 	{ PHY_ID_LAN8804, MICREL_PHY_ID_MASK },
4899a8f1a19dSHoratiu Vultur 	{ PHY_ID_LAN8841, MICREL_PHY_ID_MASK },
490052a60ed2SDavid S. Miller 	{ }
490152a60ed2SDavid S. Miller };
490252a60ed2SDavid S. Miller 
490352a60ed2SDavid S. Miller MODULE_DEVICE_TABLE(mdio, micrel_tbl);
4904