xref: /openbmc/linux/drivers/net/phy/micrel.c (revision c6d6ef3ee3b6118dae6b32816e43c790f81079b2)
1a2443fd1SAndrew Lunn // SPDX-License-Identifier: GPL-2.0+
2d0507009SDavid J. Choi /*
3d0507009SDavid J. Choi  * drivers/net/phy/micrel.c
4d0507009SDavid J. Choi  *
5d0507009SDavid J. Choi  * Driver for Micrel PHYs
6d0507009SDavid J. Choi  *
7d0507009SDavid J. Choi  * Author: David J. Choi
8d0507009SDavid J. Choi  *
97ab59dc1SDavid J. Choi  * Copyright (c) 2010-2013 Micrel, Inc.
10ee0dc2fbSJohan Hovold  * Copyright (c) 2014 Johan Hovold <johan@kernel.org>
11d0507009SDavid J. Choi  *
127ab59dc1SDavid J. Choi  * Support : Micrel Phys:
13bff5b4b3SYuiko Oshino  *		Giga phys: ksz9021, ksz9031, ksz9131
147ab59dc1SDavid J. Choi  *		100/10 Phys : ksz8001, ksz8721, ksz8737, ksz8041
157ab59dc1SDavid J. Choi  *			   ksz8021, ksz8031, ksz8051,
167ab59dc1SDavid J. Choi  *			   ksz8081, ksz8091,
177ab59dc1SDavid J. Choi  *			   ksz8061,
187ab59dc1SDavid J. Choi  *		Switch : ksz8873, ksz886x
19fc3973a1SWoojung Huh  *			 ksz9477
20d0507009SDavid J. Choi  */
21d0507009SDavid J. Choi 
22bcf3440cSOleksij Rempel #include <linux/bitfield.h>
2349011e0cSOleksij Rempel #include <linux/ethtool_netlink.h>
24d0507009SDavid J. Choi #include <linux/kernel.h>
25d0507009SDavid J. Choi #include <linux/module.h>
26d0507009SDavid J. Choi #include <linux/phy.h>
27d606ef3fSBaruch Siach #include <linux/micrel_phy.h>
28954c3967SSean Cross #include <linux/of.h>
291fadee0cSSascha Hauer #include <linux/clk.h>
306110dff7SOleksij Rempel #include <linux/delay.h>
31ece19502SDivya Koppera #include <linux/ptp_clock_kernel.h>
32ece19502SDivya Koppera #include <linux/ptp_clock.h>
33ece19502SDivya Koppera #include <linux/ptp_classify.h>
34ece19502SDivya Koppera #include <linux/net_tstamp.h>
35738871b0SMichael Walle #include <linux/gpio/consumer.h>
36d0507009SDavid J. Choi 
37212ea99aSMarek Vasut /* Operation Mode Strap Override */
38212ea99aSMarek Vasut #define MII_KSZPHY_OMSO				0x16
397a1d8390SAntoine Tenart #define KSZPHY_OMSO_FACTORY_TEST		BIT(15)
4000aee095SJohan Hovold #define KSZPHY_OMSO_B_CAST_OFF			BIT(9)
412b0ba96cSSylvain Rochet #define KSZPHY_OMSO_NAND_TREE_ON		BIT(5)
4200aee095SJohan Hovold #define KSZPHY_OMSO_RMII_OVERRIDE		BIT(1)
4300aee095SJohan Hovold #define KSZPHY_OMSO_MII_OVERRIDE		BIT(0)
44212ea99aSMarek Vasut 
4551f932c4SChoi, David /* general Interrupt control/status reg in vendor specific block. */
4651f932c4SChoi, David #define MII_KSZPHY_INTCS			0x1B
4700aee095SJohan Hovold #define KSZPHY_INTCS_JABBER			BIT(15)
4800aee095SJohan Hovold #define KSZPHY_INTCS_RECEIVE_ERR		BIT(14)
4900aee095SJohan Hovold #define KSZPHY_INTCS_PAGE_RECEIVE		BIT(13)
5000aee095SJohan Hovold #define KSZPHY_INTCS_PARELLEL			BIT(12)
5100aee095SJohan Hovold #define KSZPHY_INTCS_LINK_PARTNER_ACK		BIT(11)
5200aee095SJohan Hovold #define KSZPHY_INTCS_LINK_DOWN			BIT(10)
5300aee095SJohan Hovold #define KSZPHY_INTCS_REMOTE_FAULT		BIT(9)
5400aee095SJohan Hovold #define KSZPHY_INTCS_LINK_UP			BIT(8)
5551f932c4SChoi, David #define KSZPHY_INTCS_ALL			(KSZPHY_INTCS_LINK_UP |\
5651f932c4SChoi, David 						KSZPHY_INTCS_LINK_DOWN)
5759ca4e58SIoana Ciornei #define KSZPHY_INTCS_LINK_DOWN_STATUS		BIT(2)
5859ca4e58SIoana Ciornei #define KSZPHY_INTCS_LINK_UP_STATUS		BIT(0)
5959ca4e58SIoana Ciornei #define KSZPHY_INTCS_STATUS			(KSZPHY_INTCS_LINK_DOWN_STATUS |\
6059ca4e58SIoana Ciornei 						 KSZPHY_INTCS_LINK_UP_STATUS)
6151f932c4SChoi, David 
6249011e0cSOleksij Rempel /* LinkMD Control/Status */
6349011e0cSOleksij Rempel #define KSZ8081_LMD				0x1d
6449011e0cSOleksij Rempel #define KSZ8081_LMD_ENABLE_TEST			BIT(15)
6549011e0cSOleksij Rempel #define KSZ8081_LMD_STAT_NORMAL			0
6649011e0cSOleksij Rempel #define KSZ8081_LMD_STAT_OPEN			1
6749011e0cSOleksij Rempel #define KSZ8081_LMD_STAT_SHORT			2
6849011e0cSOleksij Rempel #define KSZ8081_LMD_STAT_FAIL			3
6949011e0cSOleksij Rempel #define KSZ8081_LMD_STAT_MASK			GENMASK(14, 13)
7049011e0cSOleksij Rempel /* Short cable (<10 meter) has been detected by LinkMD */
7149011e0cSOleksij Rempel #define KSZ8081_LMD_SHORT_INDICATOR		BIT(12)
7249011e0cSOleksij Rempel #define KSZ8081_LMD_DELTA_TIME_MASK		GENMASK(8, 0)
7349011e0cSOleksij Rempel 
7458389c00SMarek Vasut #define KSZ9x31_LMD				0x12
7558389c00SMarek Vasut #define KSZ9x31_LMD_VCT_EN			BIT(15)
7658389c00SMarek Vasut #define KSZ9x31_LMD_VCT_DIS_TX			BIT(14)
7758389c00SMarek Vasut #define KSZ9x31_LMD_VCT_PAIR(n)			(((n) & 0x3) << 12)
7858389c00SMarek Vasut #define KSZ9x31_LMD_VCT_SEL_RESULT		0
7958389c00SMarek Vasut #define KSZ9x31_LMD_VCT_SEL_THRES_HI		BIT(10)
8058389c00SMarek Vasut #define KSZ9x31_LMD_VCT_SEL_THRES_LO		BIT(11)
8158389c00SMarek Vasut #define KSZ9x31_LMD_VCT_SEL_MASK		GENMASK(11, 10)
8258389c00SMarek Vasut #define KSZ9x31_LMD_VCT_ST_NORMAL		0
8358389c00SMarek Vasut #define KSZ9x31_LMD_VCT_ST_OPEN			1
8458389c00SMarek Vasut #define KSZ9x31_LMD_VCT_ST_SHORT		2
8558389c00SMarek Vasut #define KSZ9x31_LMD_VCT_ST_FAIL			3
8658389c00SMarek Vasut #define KSZ9x31_LMD_VCT_ST_MASK			GENMASK(9, 8)
8758389c00SMarek Vasut #define KSZ9x31_LMD_VCT_DATA_REFLECTED_INVALID	BIT(7)
8858389c00SMarek Vasut #define KSZ9x31_LMD_VCT_DATA_SIG_WAIT_TOO_LONG	BIT(6)
8958389c00SMarek Vasut #define KSZ9x31_LMD_VCT_DATA_MASK100		BIT(5)
9058389c00SMarek Vasut #define KSZ9x31_LMD_VCT_DATA_NLP_FLP		BIT(4)
9158389c00SMarek Vasut #define KSZ9x31_LMD_VCT_DATA_LO_PULSE_MASK	GENMASK(3, 2)
9258389c00SMarek Vasut #define KSZ9x31_LMD_VCT_DATA_HI_PULSE_MASK	GENMASK(1, 0)
9358389c00SMarek Vasut #define KSZ9x31_LMD_VCT_DATA_MASK		GENMASK(7, 0)
9458389c00SMarek Vasut 
9521b688daSDivya Koppera #define KSZPHY_WIRE_PAIR_MASK			0x3
9621b688daSDivya Koppera 
9721b688daSDivya Koppera #define LAN8814_CABLE_DIAG			0x12
9821b688daSDivya Koppera #define LAN8814_CABLE_DIAG_STAT_MASK		GENMASK(9, 8)
9921b688daSDivya Koppera #define LAN8814_CABLE_DIAG_VCT_DATA_MASK	GENMASK(7, 0)
10021b688daSDivya Koppera #define LAN8814_PAIR_BIT_SHIFT			12
10121b688daSDivya Koppera 
10221b688daSDivya Koppera #define LAN8814_WIRE_PAIR_MASK			0xF
10321b688daSDivya Koppera 
104b3ec7248SDivya Koppera /* Lan8814 general Interrupt control/status reg in GPHY specific block. */
105b3ec7248SDivya Koppera #define LAN8814_INTC				0x18
106b3ec7248SDivya Koppera #define LAN8814_INTS				0x1B
107b3ec7248SDivya Koppera 
108b3ec7248SDivya Koppera #define LAN8814_INT_LINK_DOWN			BIT(2)
109b3ec7248SDivya Koppera #define LAN8814_INT_LINK_UP			BIT(0)
110b3ec7248SDivya Koppera #define LAN8814_INT_LINK			(LAN8814_INT_LINK_UP |\
111b3ec7248SDivya Koppera 						 LAN8814_INT_LINK_DOWN)
112b3ec7248SDivya Koppera 
113b3ec7248SDivya Koppera #define LAN8814_INTR_CTRL_REG			0x34
114b3ec7248SDivya Koppera #define LAN8814_INTR_CTRL_REG_POLARITY		BIT(1)
115b3ec7248SDivya Koppera #define LAN8814_INTR_CTRL_REG_INTR_ENABLE	BIT(0)
116b3ec7248SDivya Koppera 
117ece19502SDivya Koppera /* Represents 1ppm adjustment in 2^32 format with
118ece19502SDivya Koppera  * each nsec contains 4 clock cycles.
119ece19502SDivya Koppera  * The value is calculated as following: (1/1000000)/((2^-32)/4)
120ece19502SDivya Koppera  */
121ece19502SDivya Koppera #define LAN8814_1PPM_FORMAT			17179
122ece19502SDivya Koppera 
123ece19502SDivya Koppera #define PTP_RX_MOD				0x024F
124ece19502SDivya Koppera #define PTP_RX_MOD_BAD_UDPV4_CHKSUM_FORCE_FCS_DIS_ BIT(3)
125ece19502SDivya Koppera #define PTP_RX_TIMESTAMP_EN			0x024D
126ece19502SDivya Koppera #define PTP_TX_TIMESTAMP_EN			0x028D
127ece19502SDivya Koppera 
128ece19502SDivya Koppera #define PTP_TIMESTAMP_EN_SYNC_			BIT(0)
129ece19502SDivya Koppera #define PTP_TIMESTAMP_EN_DREQ_			BIT(1)
130ece19502SDivya Koppera #define PTP_TIMESTAMP_EN_PDREQ_			BIT(2)
131ece19502SDivya Koppera #define PTP_TIMESTAMP_EN_PDRES_			BIT(3)
132ece19502SDivya Koppera 
133ece19502SDivya Koppera #define PTP_TX_PARSE_L2_ADDR_EN			0x0284
134ece19502SDivya Koppera #define PTP_RX_PARSE_L2_ADDR_EN			0x0244
135ece19502SDivya Koppera 
136ece19502SDivya Koppera #define PTP_TX_PARSE_IP_ADDR_EN			0x0285
137ece19502SDivya Koppera #define PTP_RX_PARSE_IP_ADDR_EN			0x0245
138ece19502SDivya Koppera #define LTC_HARD_RESET				0x023F
139ece19502SDivya Koppera #define LTC_HARD_RESET_				BIT(0)
140ece19502SDivya Koppera 
141ece19502SDivya Koppera #define TSU_HARD_RESET				0x02C1
142ece19502SDivya Koppera #define TSU_HARD_RESET_				BIT(0)
143ece19502SDivya Koppera 
144ece19502SDivya Koppera #define PTP_CMD_CTL				0x0200
145ece19502SDivya Koppera #define PTP_CMD_CTL_PTP_DISABLE_		BIT(0)
146ece19502SDivya Koppera #define PTP_CMD_CTL_PTP_ENABLE_			BIT(1)
147ece19502SDivya Koppera #define PTP_CMD_CTL_PTP_CLOCK_READ_		BIT(3)
148ece19502SDivya Koppera #define PTP_CMD_CTL_PTP_CLOCK_LOAD_		BIT(4)
149ece19502SDivya Koppera #define PTP_CMD_CTL_PTP_LTC_STEP_SEC_		BIT(5)
150ece19502SDivya Koppera #define PTP_CMD_CTL_PTP_LTC_STEP_NSEC_		BIT(6)
151ece19502SDivya Koppera 
152ece19502SDivya Koppera #define PTP_CLOCK_SET_SEC_MID			0x0206
153ece19502SDivya Koppera #define PTP_CLOCK_SET_SEC_LO			0x0207
154ece19502SDivya Koppera #define PTP_CLOCK_SET_NS_HI			0x0208
155ece19502SDivya Koppera #define PTP_CLOCK_SET_NS_LO			0x0209
156ece19502SDivya Koppera 
157ece19502SDivya Koppera #define PTP_CLOCK_READ_SEC_MID			0x022A
158ece19502SDivya Koppera #define PTP_CLOCK_READ_SEC_LO			0x022B
159ece19502SDivya Koppera #define PTP_CLOCK_READ_NS_HI			0x022C
160ece19502SDivya Koppera #define PTP_CLOCK_READ_NS_LO			0x022D
161ece19502SDivya Koppera 
162ece19502SDivya Koppera #define PTP_OPERATING_MODE			0x0241
163ece19502SDivya Koppera #define PTP_OPERATING_MODE_STANDALONE_		BIT(0)
164ece19502SDivya Koppera 
165ece19502SDivya Koppera #define PTP_TX_MOD				0x028F
166ece19502SDivya Koppera #define PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_	BIT(12)
167ece19502SDivya Koppera #define PTP_TX_MOD_BAD_UDPV4_CHKSUM_FORCE_FCS_DIS_ BIT(3)
168ece19502SDivya Koppera 
169ece19502SDivya Koppera #define PTP_RX_PARSE_CONFIG			0x0242
170ece19502SDivya Koppera #define PTP_RX_PARSE_CONFIG_LAYER2_EN_		BIT(0)
171ece19502SDivya Koppera #define PTP_RX_PARSE_CONFIG_IPV4_EN_		BIT(1)
172ece19502SDivya Koppera #define PTP_RX_PARSE_CONFIG_IPV6_EN_		BIT(2)
173ece19502SDivya Koppera 
174ece19502SDivya Koppera #define PTP_TX_PARSE_CONFIG			0x0282
175ece19502SDivya Koppera #define PTP_TX_PARSE_CONFIG_LAYER2_EN_		BIT(0)
176ece19502SDivya Koppera #define PTP_TX_PARSE_CONFIG_IPV4_EN_		BIT(1)
177ece19502SDivya Koppera #define PTP_TX_PARSE_CONFIG_IPV6_EN_		BIT(2)
178ece19502SDivya Koppera 
179ece19502SDivya Koppera #define PTP_CLOCK_RATE_ADJ_HI			0x020C
180ece19502SDivya Koppera #define PTP_CLOCK_RATE_ADJ_LO			0x020D
181ece19502SDivya Koppera #define PTP_CLOCK_RATE_ADJ_DIR_			BIT(15)
182ece19502SDivya Koppera 
183ece19502SDivya Koppera #define PTP_LTC_STEP_ADJ_HI			0x0212
184ece19502SDivya Koppera #define PTP_LTC_STEP_ADJ_LO			0x0213
185ece19502SDivya Koppera #define PTP_LTC_STEP_ADJ_DIR_			BIT(15)
186ece19502SDivya Koppera 
187ece19502SDivya Koppera #define LAN8814_INTR_STS_REG			0x0033
188ece19502SDivya Koppera #define LAN8814_INTR_STS_REG_1588_TSU0_		BIT(0)
189ece19502SDivya Koppera #define LAN8814_INTR_STS_REG_1588_TSU1_		BIT(1)
190ece19502SDivya Koppera #define LAN8814_INTR_STS_REG_1588_TSU2_		BIT(2)
191ece19502SDivya Koppera #define LAN8814_INTR_STS_REG_1588_TSU3_		BIT(3)
192ece19502SDivya Koppera 
193ece19502SDivya Koppera #define PTP_CAP_INFO				0x022A
194ece19502SDivya Koppera #define PTP_CAP_INFO_TX_TS_CNT_GET_(reg_val)	(((reg_val) & 0x0f00) >> 8)
195ece19502SDivya Koppera #define PTP_CAP_INFO_RX_TS_CNT_GET_(reg_val)	((reg_val) & 0x000f)
196ece19502SDivya Koppera 
197ece19502SDivya Koppera #define PTP_TX_EGRESS_SEC_HI			0x0296
198ece19502SDivya Koppera #define PTP_TX_EGRESS_SEC_LO			0x0297
199ece19502SDivya Koppera #define PTP_TX_EGRESS_NS_HI			0x0294
200ece19502SDivya Koppera #define PTP_TX_EGRESS_NS_LO			0x0295
201ece19502SDivya Koppera #define PTP_TX_MSG_HEADER2			0x0299
202ece19502SDivya Koppera 
203ece19502SDivya Koppera #define PTP_RX_INGRESS_SEC_HI			0x0256
204ece19502SDivya Koppera #define PTP_RX_INGRESS_SEC_LO			0x0257
205ece19502SDivya Koppera #define PTP_RX_INGRESS_NS_HI			0x0254
206ece19502SDivya Koppera #define PTP_RX_INGRESS_NS_LO			0x0255
207ece19502SDivya Koppera #define PTP_RX_MSG_HEADER2			0x0259
208ece19502SDivya Koppera 
209ece19502SDivya Koppera #define PTP_TSU_INT_EN				0x0200
210ece19502SDivya Koppera #define PTP_TSU_INT_EN_PTP_TX_TS_OVRFL_EN_	BIT(3)
211ece19502SDivya Koppera #define PTP_TSU_INT_EN_PTP_TX_TS_EN_		BIT(2)
212ece19502SDivya Koppera #define PTP_TSU_INT_EN_PTP_RX_TS_OVRFL_EN_	BIT(1)
213ece19502SDivya Koppera #define PTP_TSU_INT_EN_PTP_RX_TS_EN_		BIT(0)
214ece19502SDivya Koppera 
215ece19502SDivya Koppera #define PTP_TSU_INT_STS				0x0201
216ece19502SDivya Koppera #define PTP_TSU_INT_STS_PTP_TX_TS_OVRFL_INT_	BIT(3)
217ece19502SDivya Koppera #define PTP_TSU_INT_STS_PTP_TX_TS_EN_		BIT(2)
218ece19502SDivya Koppera #define PTP_TSU_INT_STS_PTP_RX_TS_OVRFL_INT_	BIT(1)
219ece19502SDivya Koppera #define PTP_TSU_INT_STS_PTP_RX_TS_EN_		BIT(0)
220ece19502SDivya Koppera 
221a516b7f7SDivya Koppera #define LAN8814_LED_CTRL_1			0x0
222a516b7f7SDivya Koppera #define LAN8814_LED_CTRL_1_KSZ9031_LED_MODE_	BIT(6)
223a516b7f7SDivya Koppera 
2245a16778eSJohan Hovold /* PHY Control 1 */
2255a16778eSJohan Hovold #define MII_KSZPHY_CTRL_1			0x1e
226f873f112SOleksij Rempel #define KSZ8081_CTRL1_MDIX_STAT			BIT(4)
2275a16778eSJohan Hovold 
2285a16778eSJohan Hovold /* PHY Control 2 / PHY Control (if no PHY Control 1) */
2295a16778eSJohan Hovold #define MII_KSZPHY_CTRL_2			0x1f
2305a16778eSJohan Hovold #define MII_KSZPHY_CTRL				MII_KSZPHY_CTRL_2
23151f932c4SChoi, David /* bitmap of PHY register to set interrupt mode */
232f873f112SOleksij Rempel #define KSZ8081_CTRL2_HP_MDIX			BIT(15)
233f873f112SOleksij Rempel #define KSZ8081_CTRL2_MDI_MDI_X_SELECT		BIT(14)
234f873f112SOleksij Rempel #define KSZ8081_CTRL2_DISABLE_AUTO_MDIX		BIT(13)
235f873f112SOleksij Rempel #define KSZ8081_CTRL2_FORCE_LINK		BIT(11)
236f873f112SOleksij Rempel #define KSZ8081_CTRL2_POWER_SAVING		BIT(10)
23700aee095SJohan Hovold #define KSZPHY_CTRL_INT_ACTIVE_HIGH		BIT(9)
23863f44b2bSJohan Hovold #define KSZPHY_RMII_REF_CLK_SEL			BIT(7)
23951f932c4SChoi, David 
240954c3967SSean Cross /* Write/read to/from extended registers */
241954c3967SSean Cross #define MII_KSZPHY_EXTREG			0x0b
242954c3967SSean Cross #define KSZPHY_EXTREG_WRITE			0x8000
243954c3967SSean Cross 
244954c3967SSean Cross #define MII_KSZPHY_EXTREG_WRITE			0x0c
245954c3967SSean Cross #define MII_KSZPHY_EXTREG_READ			0x0d
246954c3967SSean Cross 
247954c3967SSean Cross /* Extended registers */
248954c3967SSean Cross #define MII_KSZPHY_CLK_CONTROL_PAD_SKEW		0x104
249954c3967SSean Cross #define MII_KSZPHY_RX_DATA_PAD_SKEW		0x105
250954c3967SSean Cross #define MII_KSZPHY_TX_DATA_PAD_SKEW		0x106
251954c3967SSean Cross 
252954c3967SSean Cross #define PS_TO_REG				200
253ece19502SDivya Koppera #define FIFO_SIZE				8
254954c3967SSean Cross 
2552b2427d0SAndrew Lunn struct kszphy_hw_stat {
2562b2427d0SAndrew Lunn 	const char *string;
2572b2427d0SAndrew Lunn 	u8 reg;
2582b2427d0SAndrew Lunn 	u8 bits;
2592b2427d0SAndrew Lunn };
2602b2427d0SAndrew Lunn 
2612b2427d0SAndrew Lunn static struct kszphy_hw_stat kszphy_hw_stats[] = {
2622b2427d0SAndrew Lunn 	{ "phy_receive_errors", 21, 16},
2632b2427d0SAndrew Lunn 	{ "phy_idle_errors", 10, 8 },
2642b2427d0SAndrew Lunn };
2652b2427d0SAndrew Lunn 
266e6a423a8SJohan Hovold struct kszphy_type {
267e6a423a8SJohan Hovold 	u32 led_mode_reg;
268c6f9575cSJohan Hovold 	u16 interrupt_level_mask;
26921b688daSDivya Koppera 	u16 cable_diag_reg;
27021b688daSDivya Koppera 	unsigned long pair_mask;
271a8f1a19dSHoratiu Vultur 	u16 disable_dll_tx_bit;
272a8f1a19dSHoratiu Vultur 	u16 disable_dll_rx_bit;
273a8f1a19dSHoratiu Vultur 	u16 disable_dll_mask;
2740f95903eSJohan Hovold 	bool has_broadcast_disable;
2752b0ba96cSSylvain Rochet 	bool has_nand_tree_disable;
27663f44b2bSJohan Hovold 	bool has_rmii_ref_clk_sel;
277e6a423a8SJohan Hovold };
278e6a423a8SJohan Hovold 
279ece19502SDivya Koppera /* Shared structure between the PHYs of the same package. */
280ece19502SDivya Koppera struct lan8814_shared_priv {
281ece19502SDivya Koppera 	struct phy_device *phydev;
282ece19502SDivya Koppera 	struct ptp_clock *ptp_clock;
283ece19502SDivya Koppera 	struct ptp_clock_info ptp_clock_info;
284ece19502SDivya Koppera 
285ece19502SDivya Koppera 	/* Reference counter to how many ports in the package are enabling the
286ece19502SDivya Koppera 	 * timestamping
287ece19502SDivya Koppera 	 */
288ece19502SDivya Koppera 	u8 ref;
289ece19502SDivya Koppera 
290ece19502SDivya Koppera 	/* Lock for ptp_clock and ref */
291ece19502SDivya Koppera 	struct mutex shared_lock;
292ece19502SDivya Koppera };
293ece19502SDivya Koppera 
294ece19502SDivya Koppera struct lan8814_ptp_rx_ts {
295ece19502SDivya Koppera 	struct list_head list;
296ece19502SDivya Koppera 	u32 seconds;
297ece19502SDivya Koppera 	u32 nsec;
298ece19502SDivya Koppera 	u16 seq_id;
299ece19502SDivya Koppera };
300ece19502SDivya Koppera 
301ece19502SDivya Koppera struct kszphy_ptp_priv {
302ece19502SDivya Koppera 	struct mii_timestamper mii_ts;
303ece19502SDivya Koppera 	struct phy_device *phydev;
304ece19502SDivya Koppera 
305ece19502SDivya Koppera 	struct sk_buff_head tx_queue;
306ece19502SDivya Koppera 	struct sk_buff_head rx_queue;
307ece19502SDivya Koppera 
308ece19502SDivya Koppera 	struct list_head rx_ts_list;
309ece19502SDivya Koppera 	/* Lock for Rx ts fifo */
310ece19502SDivya Koppera 	spinlock_t rx_ts_lock;
311ece19502SDivya Koppera 
312ece19502SDivya Koppera 	int hwts_tx_type;
313ece19502SDivya Koppera 	enum hwtstamp_rx_filters rx_filter;
314ece19502SDivya Koppera 	int layer;
315ece19502SDivya Koppera 	int version;
316cafc3662SHoratiu Vultur 
317cafc3662SHoratiu Vultur 	struct ptp_clock *ptp_clock;
318cafc3662SHoratiu Vultur 	struct ptp_clock_info ptp_clock_info;
319cafc3662SHoratiu Vultur 	/* Lock for ptp_clock */
320cafc3662SHoratiu Vultur 	struct mutex ptp_lock;
321e4ed8ba0SHoratiu Vultur 	struct ptp_pin_desc *pin_config;
322ece19502SDivya Koppera };
323ece19502SDivya Koppera 
324e6a423a8SJohan Hovold struct kszphy_priv {
325ece19502SDivya Koppera 	struct kszphy_ptp_priv ptp_priv;
326e6a423a8SJohan Hovold 	const struct kszphy_type *type;
327e7a792e9SJohan Hovold 	int led_mode;
32858389c00SMarek Vasut 	u16 vct_ctrl1000;
32963f44b2bSJohan Hovold 	bool rmii_ref_clk_sel;
33063f44b2bSJohan Hovold 	bool rmii_ref_clk_sel_val;
3312b2427d0SAndrew Lunn 	u64 stats[ARRAY_SIZE(kszphy_hw_stats)];
332e6a423a8SJohan Hovold };
333e6a423a8SJohan Hovold 
334a516b7f7SDivya Koppera static const struct kszphy_type lan8814_type = {
335a516b7f7SDivya Koppera 	.led_mode_reg		= ~LAN8814_LED_CTRL_1,
33621b688daSDivya Koppera 	.cable_diag_reg		= LAN8814_CABLE_DIAG,
33721b688daSDivya Koppera 	.pair_mask		= LAN8814_WIRE_PAIR_MASK,
33821b688daSDivya Koppera };
33921b688daSDivya Koppera 
34021b688daSDivya Koppera static const struct kszphy_type ksz886x_type = {
34121b688daSDivya Koppera 	.cable_diag_reg		= KSZ8081_LMD,
34221b688daSDivya Koppera 	.pair_mask		= KSZPHY_WIRE_PAIR_MASK,
343a516b7f7SDivya Koppera };
344a516b7f7SDivya Koppera 
345e6a423a8SJohan Hovold static const struct kszphy_type ksz8021_type = {
346e6a423a8SJohan Hovold 	.led_mode_reg		= MII_KSZPHY_CTRL_2,
347d0e1df9cSJohan Hovold 	.has_broadcast_disable	= true,
3482b0ba96cSSylvain Rochet 	.has_nand_tree_disable	= true,
34963f44b2bSJohan Hovold 	.has_rmii_ref_clk_sel	= true,
350e6a423a8SJohan Hovold };
351e6a423a8SJohan Hovold 
352e6a423a8SJohan Hovold static const struct kszphy_type ksz8041_type = {
353e6a423a8SJohan Hovold 	.led_mode_reg		= MII_KSZPHY_CTRL_1,
354e6a423a8SJohan Hovold };
355e6a423a8SJohan Hovold 
356e6a423a8SJohan Hovold static const struct kszphy_type ksz8051_type = {
357e6a423a8SJohan Hovold 	.led_mode_reg		= MII_KSZPHY_CTRL_2,
3582b0ba96cSSylvain Rochet 	.has_nand_tree_disable	= true,
359e6a423a8SJohan Hovold };
360e6a423a8SJohan Hovold 
361e6a423a8SJohan Hovold static const struct kszphy_type ksz8081_type = {
362e6a423a8SJohan Hovold 	.led_mode_reg		= MII_KSZPHY_CTRL_2,
3630f95903eSJohan Hovold 	.has_broadcast_disable	= true,
3642b0ba96cSSylvain Rochet 	.has_nand_tree_disable	= true,
36586dc1342SJohan Hovold 	.has_rmii_ref_clk_sel	= true,
366e6a423a8SJohan Hovold };
367e6a423a8SJohan Hovold 
368c6f9575cSJohan Hovold static const struct kszphy_type ks8737_type = {
369c6f9575cSJohan Hovold 	.interrupt_level_mask	= BIT(14),
370c6f9575cSJohan Hovold };
371c6f9575cSJohan Hovold 
372c6f9575cSJohan Hovold static const struct kszphy_type ksz9021_type = {
373c6f9575cSJohan Hovold 	.interrupt_level_mask	= BIT(14),
374c6f9575cSJohan Hovold };
375c6f9575cSJohan Hovold 
376a8f1a19dSHoratiu Vultur static const struct kszphy_type ksz9131_type = {
377a8f1a19dSHoratiu Vultur 	.interrupt_level_mask	= BIT(14),
378a8f1a19dSHoratiu Vultur 	.disable_dll_tx_bit	= BIT(12),
379a8f1a19dSHoratiu Vultur 	.disable_dll_rx_bit	= BIT(12),
380a8f1a19dSHoratiu Vultur 	.disable_dll_mask	= BIT_MASK(12),
381a8f1a19dSHoratiu Vultur };
382a8f1a19dSHoratiu Vultur 
383a8f1a19dSHoratiu Vultur static const struct kszphy_type lan8841_type = {
384a8f1a19dSHoratiu Vultur 	.disable_dll_tx_bit	= BIT(14),
385a8f1a19dSHoratiu Vultur 	.disable_dll_rx_bit	= BIT(14),
386a8f1a19dSHoratiu Vultur 	.disable_dll_mask	= BIT_MASK(14),
387a136391aSHoratiu Vultur 	.cable_diag_reg		= LAN8814_CABLE_DIAG,
388a136391aSHoratiu Vultur 	.pair_mask		= LAN8814_WIRE_PAIR_MASK,
389a8f1a19dSHoratiu Vultur };
390a8f1a19dSHoratiu Vultur 
391954c3967SSean Cross static int kszphy_extended_write(struct phy_device *phydev,
392954c3967SSean Cross 				u32 regnum, u16 val)
393954c3967SSean Cross {
394954c3967SSean Cross 	phy_write(phydev, MII_KSZPHY_EXTREG, KSZPHY_EXTREG_WRITE | regnum);
395954c3967SSean Cross 	return phy_write(phydev, MII_KSZPHY_EXTREG_WRITE, val);
396954c3967SSean Cross }
397954c3967SSean Cross 
398954c3967SSean Cross static int kszphy_extended_read(struct phy_device *phydev,
399954c3967SSean Cross 				u32 regnum)
400954c3967SSean Cross {
401954c3967SSean Cross 	phy_write(phydev, MII_KSZPHY_EXTREG, regnum);
402954c3967SSean Cross 	return phy_read(phydev, MII_KSZPHY_EXTREG_READ);
403954c3967SSean Cross }
404954c3967SSean Cross 
40551f932c4SChoi, David static int kszphy_ack_interrupt(struct phy_device *phydev)
40651f932c4SChoi, David {
40751f932c4SChoi, David 	/* bit[7..0] int status, which is a read and clear register. */
40851f932c4SChoi, David 	int rc;
40951f932c4SChoi, David 
41051f932c4SChoi, David 	rc = phy_read(phydev, MII_KSZPHY_INTCS);
41151f932c4SChoi, David 
41251f932c4SChoi, David 	return (rc < 0) ? rc : 0;
41351f932c4SChoi, David }
41451f932c4SChoi, David 
41551f932c4SChoi, David static int kszphy_config_intr(struct phy_device *phydev)
41651f932c4SChoi, David {
417c6f9575cSJohan Hovold 	const struct kszphy_type *type = phydev->drv->driver_data;
418c0c99d0cSIoana Ciornei 	int temp, err;
419c6f9575cSJohan Hovold 	u16 mask;
420c6f9575cSJohan Hovold 
421c6f9575cSJohan Hovold 	if (type && type->interrupt_level_mask)
422c6f9575cSJohan Hovold 		mask = type->interrupt_level_mask;
423c6f9575cSJohan Hovold 	else
424c6f9575cSJohan Hovold 		mask = KSZPHY_CTRL_INT_ACTIVE_HIGH;
42551f932c4SChoi, David 
42651f932c4SChoi, David 	/* set the interrupt pin active low */
42751f932c4SChoi, David 	temp = phy_read(phydev, MII_KSZPHY_CTRL);
4285bb8fc0dSJohan Hovold 	if (temp < 0)
4295bb8fc0dSJohan Hovold 		return temp;
430c6f9575cSJohan Hovold 	temp &= ~mask;
43151f932c4SChoi, David 	phy_write(phydev, MII_KSZPHY_CTRL, temp);
43251f932c4SChoi, David 
433c6f9575cSJohan Hovold 	/* enable / disable interrupts */
434c0c99d0cSIoana Ciornei 	if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
435c0c99d0cSIoana Ciornei 		err = kszphy_ack_interrupt(phydev);
436c0c99d0cSIoana Ciornei 		if (err)
437c0c99d0cSIoana Ciornei 			return err;
43851f932c4SChoi, David 
439a57cc54dSWolfram Sang 		err = phy_write(phydev, MII_KSZPHY_INTCS, KSZPHY_INTCS_ALL);
440c0c99d0cSIoana Ciornei 	} else {
441a57cc54dSWolfram Sang 		err = phy_write(phydev, MII_KSZPHY_INTCS, 0);
442c0c99d0cSIoana Ciornei 		if (err)
443c0c99d0cSIoana Ciornei 			return err;
444c0c99d0cSIoana Ciornei 
445c0c99d0cSIoana Ciornei 		err = kszphy_ack_interrupt(phydev);
446c0c99d0cSIoana Ciornei 	}
447c0c99d0cSIoana Ciornei 
448c0c99d0cSIoana Ciornei 	return err;
44951f932c4SChoi, David }
450d0507009SDavid J. Choi 
45159ca4e58SIoana Ciornei static irqreturn_t kszphy_handle_interrupt(struct phy_device *phydev)
45259ca4e58SIoana Ciornei {
45359ca4e58SIoana Ciornei 	int irq_status;
45459ca4e58SIoana Ciornei 
45559ca4e58SIoana Ciornei 	irq_status = phy_read(phydev, MII_KSZPHY_INTCS);
45659ca4e58SIoana Ciornei 	if (irq_status < 0) {
45759ca4e58SIoana Ciornei 		phy_error(phydev);
45859ca4e58SIoana Ciornei 		return IRQ_NONE;
45959ca4e58SIoana Ciornei 	}
46059ca4e58SIoana Ciornei 
461fff4c746SOleksij Rempel 	if (!(irq_status & KSZPHY_INTCS_STATUS))
46259ca4e58SIoana Ciornei 		return IRQ_NONE;
46359ca4e58SIoana Ciornei 
46459ca4e58SIoana Ciornei 	phy_trigger_machine(phydev);
46559ca4e58SIoana Ciornei 
46659ca4e58SIoana Ciornei 	return IRQ_HANDLED;
46759ca4e58SIoana Ciornei }
46859ca4e58SIoana Ciornei 
46963f44b2bSJohan Hovold static int kszphy_rmii_clk_sel(struct phy_device *phydev, bool val)
47063f44b2bSJohan Hovold {
47163f44b2bSJohan Hovold 	int ctrl;
47263f44b2bSJohan Hovold 
47363f44b2bSJohan Hovold 	ctrl = phy_read(phydev, MII_KSZPHY_CTRL);
47463f44b2bSJohan Hovold 	if (ctrl < 0)
47563f44b2bSJohan Hovold 		return ctrl;
47663f44b2bSJohan Hovold 
47763f44b2bSJohan Hovold 	if (val)
47863f44b2bSJohan Hovold 		ctrl |= KSZPHY_RMII_REF_CLK_SEL;
47963f44b2bSJohan Hovold 	else
48063f44b2bSJohan Hovold 		ctrl &= ~KSZPHY_RMII_REF_CLK_SEL;
48163f44b2bSJohan Hovold 
48263f44b2bSJohan Hovold 	return phy_write(phydev, MII_KSZPHY_CTRL, ctrl);
48363f44b2bSJohan Hovold }
48463f44b2bSJohan Hovold 
485e7a792e9SJohan Hovold static int kszphy_setup_led(struct phy_device *phydev, u32 reg, int val)
48620d8435aSBen Dooks {
4875a16778eSJohan Hovold 	int rc, temp, shift;
4888620546cSJohan Hovold 
4895a16778eSJohan Hovold 	switch (reg) {
4905a16778eSJohan Hovold 	case MII_KSZPHY_CTRL_1:
4915a16778eSJohan Hovold 		shift = 14;
4925a16778eSJohan Hovold 		break;
4935a16778eSJohan Hovold 	case MII_KSZPHY_CTRL_2:
4945a16778eSJohan Hovold 		shift = 4;
4955a16778eSJohan Hovold 		break;
4965a16778eSJohan Hovold 	default:
4975a16778eSJohan Hovold 		return -EINVAL;
4985a16778eSJohan Hovold 	}
4995a16778eSJohan Hovold 
50020d8435aSBen Dooks 	temp = phy_read(phydev, reg);
501b7035860SJohan Hovold 	if (temp < 0) {
502b7035860SJohan Hovold 		rc = temp;
503b7035860SJohan Hovold 		goto out;
504b7035860SJohan Hovold 	}
50520d8435aSBen Dooks 
50628bdc499SSergei Shtylyov 	temp &= ~(3 << shift);
50720d8435aSBen Dooks 	temp |= val << shift;
50820d8435aSBen Dooks 	rc = phy_write(phydev, reg, temp);
509b7035860SJohan Hovold out:
510b7035860SJohan Hovold 	if (rc < 0)
51172ba48beSAndrew Lunn 		phydev_err(phydev, "failed to set led mode\n");
51220d8435aSBen Dooks 
513b7035860SJohan Hovold 	return rc;
51420d8435aSBen Dooks }
51520d8435aSBen Dooks 
516bde15129SJohan Hovold /* Disable PHY address 0 as the broadcast address, so that it can be used as a
517bde15129SJohan Hovold  * unique (non-broadcast) address on a shared bus.
518bde15129SJohan Hovold  */
519bde15129SJohan Hovold static int kszphy_broadcast_disable(struct phy_device *phydev)
520bde15129SJohan Hovold {
521bde15129SJohan Hovold 	int ret;
522bde15129SJohan Hovold 
523bde15129SJohan Hovold 	ret = phy_read(phydev, MII_KSZPHY_OMSO);
524bde15129SJohan Hovold 	if (ret < 0)
525bde15129SJohan Hovold 		goto out;
526bde15129SJohan Hovold 
527bde15129SJohan Hovold 	ret = phy_write(phydev, MII_KSZPHY_OMSO, ret | KSZPHY_OMSO_B_CAST_OFF);
528bde15129SJohan Hovold out:
529bde15129SJohan Hovold 	if (ret)
53072ba48beSAndrew Lunn 		phydev_err(phydev, "failed to disable broadcast address\n");
531bde15129SJohan Hovold 
532bde15129SJohan Hovold 	return ret;
533bde15129SJohan Hovold }
534bde15129SJohan Hovold 
5352b0ba96cSSylvain Rochet static int kszphy_nand_tree_disable(struct phy_device *phydev)
5362b0ba96cSSylvain Rochet {
5372b0ba96cSSylvain Rochet 	int ret;
5382b0ba96cSSylvain Rochet 
5392b0ba96cSSylvain Rochet 	ret = phy_read(phydev, MII_KSZPHY_OMSO);
5402b0ba96cSSylvain Rochet 	if (ret < 0)
5412b0ba96cSSylvain Rochet 		goto out;
5422b0ba96cSSylvain Rochet 
5432b0ba96cSSylvain Rochet 	if (!(ret & KSZPHY_OMSO_NAND_TREE_ON))
5442b0ba96cSSylvain Rochet 		return 0;
5452b0ba96cSSylvain Rochet 
5462b0ba96cSSylvain Rochet 	ret = phy_write(phydev, MII_KSZPHY_OMSO,
5472b0ba96cSSylvain Rochet 			ret & ~KSZPHY_OMSO_NAND_TREE_ON);
5482b0ba96cSSylvain Rochet out:
5492b0ba96cSSylvain Rochet 	if (ret)
55072ba48beSAndrew Lunn 		phydev_err(phydev, "failed to disable NAND tree mode\n");
5512b0ba96cSSylvain Rochet 
5522b0ba96cSSylvain Rochet 	return ret;
5532b0ba96cSSylvain Rochet }
5542b0ba96cSSylvain Rochet 
55579e498a9SLeonard Crestez /* Some config bits need to be set again on resume, handle them here. */
55679e498a9SLeonard Crestez static int kszphy_config_reset(struct phy_device *phydev)
55779e498a9SLeonard Crestez {
55879e498a9SLeonard Crestez 	struct kszphy_priv *priv = phydev->priv;
55979e498a9SLeonard Crestez 	int ret;
56079e498a9SLeonard Crestez 
56179e498a9SLeonard Crestez 	if (priv->rmii_ref_clk_sel) {
56279e498a9SLeonard Crestez 		ret = kszphy_rmii_clk_sel(phydev, priv->rmii_ref_clk_sel_val);
56379e498a9SLeonard Crestez 		if (ret) {
56479e498a9SLeonard Crestez 			phydev_err(phydev,
56579e498a9SLeonard Crestez 				   "failed to set rmii reference clock\n");
56679e498a9SLeonard Crestez 			return ret;
56779e498a9SLeonard Crestez 		}
56879e498a9SLeonard Crestez 	}
56979e498a9SLeonard Crestez 
570f2ef6f75SFabio Estevam 	if (priv->type && priv->led_mode >= 0)
57179e498a9SLeonard Crestez 		kszphy_setup_led(phydev, priv->type->led_mode_reg, priv->led_mode);
57279e498a9SLeonard Crestez 
57379e498a9SLeonard Crestez 	return 0;
57479e498a9SLeonard Crestez }
57579e498a9SLeonard Crestez 
576d0507009SDavid J. Choi static int kszphy_config_init(struct phy_device *phydev)
577d0507009SDavid J. Choi {
578e6a423a8SJohan Hovold 	struct kszphy_priv *priv = phydev->priv;
579e6a423a8SJohan Hovold 	const struct kszphy_type *type;
580d0507009SDavid J. Choi 
581e6a423a8SJohan Hovold 	if (!priv)
582e6a423a8SJohan Hovold 		return 0;
583e6a423a8SJohan Hovold 
584e6a423a8SJohan Hovold 	type = priv->type;
585e6a423a8SJohan Hovold 
586f2ef6f75SFabio Estevam 	if (type && type->has_broadcast_disable)
5870f95903eSJohan Hovold 		kszphy_broadcast_disable(phydev);
5880f95903eSJohan Hovold 
589f2ef6f75SFabio Estevam 	if (type && type->has_nand_tree_disable)
5902b0ba96cSSylvain Rochet 		kszphy_nand_tree_disable(phydev);
5912b0ba96cSSylvain Rochet 
59279e498a9SLeonard Crestez 	return kszphy_config_reset(phydev);
59320d8435aSBen Dooks }
59420d8435aSBen Dooks 
5954217a64eSMichael Walle static int ksz8041_fiber_mode(struct phy_device *phydev)
5964217a64eSMichael Walle {
5974217a64eSMichael Walle 	struct device_node *of_node = phydev->mdio.dev.of_node;
5984217a64eSMichael Walle 
5994217a64eSMichael Walle 	return of_property_read_bool(of_node, "micrel,fiber-mode");
6004217a64eSMichael Walle }
6014217a64eSMichael Walle 
60277501a79SPhilipp Zabel static int ksz8041_config_init(struct phy_device *phydev)
60377501a79SPhilipp Zabel {
6043c1bcc86SAndrew Lunn 	__ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
6053c1bcc86SAndrew Lunn 
60677501a79SPhilipp Zabel 	/* Limit supported and advertised modes in fiber mode */
6074217a64eSMichael Walle 	if (ksz8041_fiber_mode(phydev)) {
60877501a79SPhilipp Zabel 		phydev->dev_flags |= MICREL_PHY_FXEN;
6093c1bcc86SAndrew Lunn 		linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, mask);
6103c1bcc86SAndrew Lunn 		linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, mask);
6113c1bcc86SAndrew Lunn 
6123c1bcc86SAndrew Lunn 		linkmode_and(phydev->supported, phydev->supported, mask);
6133c1bcc86SAndrew Lunn 		linkmode_set_bit(ETHTOOL_LINK_MODE_FIBRE_BIT,
6143c1bcc86SAndrew Lunn 				 phydev->supported);
6153c1bcc86SAndrew Lunn 		linkmode_and(phydev->advertising, phydev->advertising, mask);
6163c1bcc86SAndrew Lunn 		linkmode_set_bit(ETHTOOL_LINK_MODE_FIBRE_BIT,
6173c1bcc86SAndrew Lunn 				 phydev->advertising);
61877501a79SPhilipp Zabel 		phydev->autoneg = AUTONEG_DISABLE;
61977501a79SPhilipp Zabel 	}
62077501a79SPhilipp Zabel 
62177501a79SPhilipp Zabel 	return kszphy_config_init(phydev);
62277501a79SPhilipp Zabel }
62377501a79SPhilipp Zabel 
62477501a79SPhilipp Zabel static int ksz8041_config_aneg(struct phy_device *phydev)
62577501a79SPhilipp Zabel {
62677501a79SPhilipp Zabel 	/* Skip auto-negotiation in fiber mode */
62777501a79SPhilipp Zabel 	if (phydev->dev_flags & MICREL_PHY_FXEN) {
62877501a79SPhilipp Zabel 		phydev->speed = SPEED_100;
62977501a79SPhilipp Zabel 		return 0;
63077501a79SPhilipp Zabel 	}
63177501a79SPhilipp Zabel 
63277501a79SPhilipp Zabel 	return genphy_config_aneg(phydev);
63377501a79SPhilipp Zabel }
63477501a79SPhilipp Zabel 
6358b95599cSMarek Vasut static int ksz8051_ksz8795_match_phy_device(struct phy_device *phydev,
636a5e63c7dSSteve Bennett 					    const bool ksz_8051)
6378b95599cSMarek Vasut {
6388b95599cSMarek Vasut 	int ret;
6398b95599cSMarek Vasut 
640a5e63c7dSSteve Bennett 	if ((phydev->phy_id & MICREL_PHY_ID_MASK) != PHY_ID_KSZ8051)
6418b95599cSMarek Vasut 		return 0;
6428b95599cSMarek Vasut 
6438b95599cSMarek Vasut 	ret = phy_read(phydev, MII_BMSR);
6448b95599cSMarek Vasut 	if (ret < 0)
6458b95599cSMarek Vasut 		return ret;
6468b95599cSMarek Vasut 
6478b95599cSMarek Vasut 	/* KSZ8051 PHY and KSZ8794/KSZ8795/KSZ8765 switch share the same
6488b95599cSMarek Vasut 	 * exact PHY ID. However, they can be told apart by the extended
6498b95599cSMarek Vasut 	 * capability registers presence. The KSZ8051 PHY has them while
6508b95599cSMarek Vasut 	 * the switch does not.
6518b95599cSMarek Vasut 	 */
6528b95599cSMarek Vasut 	ret &= BMSR_ERCAP;
653a5e63c7dSSteve Bennett 	if (ksz_8051)
6548b95599cSMarek Vasut 		return ret;
6558b95599cSMarek Vasut 	else
6568b95599cSMarek Vasut 		return !ret;
6578b95599cSMarek Vasut }
6588b95599cSMarek Vasut 
6598b95599cSMarek Vasut static int ksz8051_match_phy_device(struct phy_device *phydev)
6608b95599cSMarek Vasut {
661a5e63c7dSSteve Bennett 	return ksz8051_ksz8795_match_phy_device(phydev, true);
6628b95599cSMarek Vasut }
6638b95599cSMarek Vasut 
6647a1d8390SAntoine Tenart static int ksz8081_config_init(struct phy_device *phydev)
6657a1d8390SAntoine Tenart {
6667a1d8390SAntoine Tenart 	/* KSZPHY_OMSO_FACTORY_TEST is set at de-assertion of the reset line
6677a1d8390SAntoine Tenart 	 * based on the RXER (KSZ8081RNA/RND) or TXC (KSZ8081MNX/RNB) pin. If a
6687a1d8390SAntoine Tenart 	 * pull-down is missing, the factory test mode should be cleared by
6697a1d8390SAntoine Tenart 	 * manually writing a 0.
6707a1d8390SAntoine Tenart 	 */
6717a1d8390SAntoine Tenart 	phy_clear_bits(phydev, MII_KSZPHY_OMSO, KSZPHY_OMSO_FACTORY_TEST);
6727a1d8390SAntoine Tenart 
6737a1d8390SAntoine Tenart 	return kszphy_config_init(phydev);
6747a1d8390SAntoine Tenart }
6757a1d8390SAntoine Tenart 
676f873f112SOleksij Rempel static int ksz8081_config_mdix(struct phy_device *phydev, u8 ctrl)
677f873f112SOleksij Rempel {
678f873f112SOleksij Rempel 	u16 val;
679f873f112SOleksij Rempel 
680f873f112SOleksij Rempel 	switch (ctrl) {
681f873f112SOleksij Rempel 	case ETH_TP_MDI:
682f873f112SOleksij Rempel 		val = KSZ8081_CTRL2_DISABLE_AUTO_MDIX;
683f873f112SOleksij Rempel 		break;
684f873f112SOleksij Rempel 	case ETH_TP_MDI_X:
685f873f112SOleksij Rempel 		val = KSZ8081_CTRL2_DISABLE_AUTO_MDIX |
686f873f112SOleksij Rempel 			KSZ8081_CTRL2_MDI_MDI_X_SELECT;
687f873f112SOleksij Rempel 		break;
688f873f112SOleksij Rempel 	case ETH_TP_MDI_AUTO:
689f873f112SOleksij Rempel 		val = 0;
690f873f112SOleksij Rempel 		break;
691f873f112SOleksij Rempel 	default:
692f873f112SOleksij Rempel 		return 0;
693f873f112SOleksij Rempel 	}
694f873f112SOleksij Rempel 
695f873f112SOleksij Rempel 	return phy_modify(phydev, MII_KSZPHY_CTRL_2,
696f873f112SOleksij Rempel 			  KSZ8081_CTRL2_HP_MDIX |
697f873f112SOleksij Rempel 			  KSZ8081_CTRL2_MDI_MDI_X_SELECT |
698f873f112SOleksij Rempel 			  KSZ8081_CTRL2_DISABLE_AUTO_MDIX,
699f873f112SOleksij Rempel 			  KSZ8081_CTRL2_HP_MDIX | val);
700f873f112SOleksij Rempel }
701f873f112SOleksij Rempel 
702f873f112SOleksij Rempel static int ksz8081_config_aneg(struct phy_device *phydev)
703f873f112SOleksij Rempel {
704f873f112SOleksij Rempel 	int ret;
705f873f112SOleksij Rempel 
706f873f112SOleksij Rempel 	ret = genphy_config_aneg(phydev);
707f873f112SOleksij Rempel 	if (ret)
708f873f112SOleksij Rempel 		return ret;
709f873f112SOleksij Rempel 
710f873f112SOleksij Rempel 	/* The MDI-X configuration is automatically changed by the PHY after
711f873f112SOleksij Rempel 	 * switching from autoneg off to on. So, take MDI-X configuration under
712f873f112SOleksij Rempel 	 * own control and set it after autoneg configuration was done.
713f873f112SOleksij Rempel 	 */
714f873f112SOleksij Rempel 	return ksz8081_config_mdix(phydev, phydev->mdix_ctrl);
715f873f112SOleksij Rempel }
716f873f112SOleksij Rempel 
717f873f112SOleksij Rempel static int ksz8081_mdix_update(struct phy_device *phydev)
718f873f112SOleksij Rempel {
719f873f112SOleksij Rempel 	int ret;
720f873f112SOleksij Rempel 
721f873f112SOleksij Rempel 	ret = phy_read(phydev, MII_KSZPHY_CTRL_2);
722f873f112SOleksij Rempel 	if (ret < 0)
723f873f112SOleksij Rempel 		return ret;
724f873f112SOleksij Rempel 
725f873f112SOleksij Rempel 	if (ret & KSZ8081_CTRL2_DISABLE_AUTO_MDIX) {
726f873f112SOleksij Rempel 		if (ret & KSZ8081_CTRL2_MDI_MDI_X_SELECT)
727f873f112SOleksij Rempel 			phydev->mdix_ctrl = ETH_TP_MDI_X;
728f873f112SOleksij Rempel 		else
729f873f112SOleksij Rempel 			phydev->mdix_ctrl = ETH_TP_MDI;
730f873f112SOleksij Rempel 	} else {
731f873f112SOleksij Rempel 		phydev->mdix_ctrl = ETH_TP_MDI_AUTO;
732f873f112SOleksij Rempel 	}
733f873f112SOleksij Rempel 
734f873f112SOleksij Rempel 	ret = phy_read(phydev, MII_KSZPHY_CTRL_1);
735f873f112SOleksij Rempel 	if (ret < 0)
736f873f112SOleksij Rempel 		return ret;
737f873f112SOleksij Rempel 
738f873f112SOleksij Rempel 	if (ret & KSZ8081_CTRL1_MDIX_STAT)
739f873f112SOleksij Rempel 		phydev->mdix = ETH_TP_MDI;
740f873f112SOleksij Rempel 	else
741f873f112SOleksij Rempel 		phydev->mdix = ETH_TP_MDI_X;
742f873f112SOleksij Rempel 
743f873f112SOleksij Rempel 	return 0;
744f873f112SOleksij Rempel }
745f873f112SOleksij Rempel 
746f873f112SOleksij Rempel static int ksz8081_read_status(struct phy_device *phydev)
747f873f112SOleksij Rempel {
748f873f112SOleksij Rempel 	int ret;
749f873f112SOleksij Rempel 
750f873f112SOleksij Rempel 	ret = ksz8081_mdix_update(phydev);
751f873f112SOleksij Rempel 	if (ret < 0)
752f873f112SOleksij Rempel 		return ret;
753f873f112SOleksij Rempel 
754f873f112SOleksij Rempel 	return genphy_read_status(phydev);
755f873f112SOleksij Rempel }
756f873f112SOleksij Rempel 
757232ba3a5SRajasingh Thavamani static int ksz8061_config_init(struct phy_device *phydev)
758232ba3a5SRajasingh Thavamani {
759232ba3a5SRajasingh Thavamani 	int ret;
760232ba3a5SRajasingh Thavamani 
761232ba3a5SRajasingh Thavamani 	ret = phy_write_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_DEVID1, 0xB61A);
762232ba3a5SRajasingh Thavamani 	if (ret)
763232ba3a5SRajasingh Thavamani 		return ret;
764232ba3a5SRajasingh Thavamani 
765232ba3a5SRajasingh Thavamani 	return kszphy_config_init(phydev);
766232ba3a5SRajasingh Thavamani }
767232ba3a5SRajasingh Thavamani 
7688b95599cSMarek Vasut static int ksz8795_match_phy_device(struct phy_device *phydev)
7698b95599cSMarek Vasut {
770a5e63c7dSSteve Bennett 	return ksz8051_ksz8795_match_phy_device(phydev, false);
7718b95599cSMarek Vasut }
7728b95599cSMarek Vasut 
773954c3967SSean Cross static int ksz9021_load_values_from_of(struct phy_device *phydev,
7743c9a9f7fSJaeden Amero 				       const struct device_node *of_node,
7753c9a9f7fSJaeden Amero 				       u16 reg,
7763c9a9f7fSJaeden Amero 				       const char *field1, const char *field2,
7773c9a9f7fSJaeden Amero 				       const char *field3, const char *field4)
778954c3967SSean Cross {
779954c3967SSean Cross 	int val1 = -1;
780954c3967SSean Cross 	int val2 = -2;
781954c3967SSean Cross 	int val3 = -3;
782954c3967SSean Cross 	int val4 = -4;
783954c3967SSean Cross 	int newval;
784954c3967SSean Cross 	int matches = 0;
785954c3967SSean Cross 
786954c3967SSean Cross 	if (!of_property_read_u32(of_node, field1, &val1))
787954c3967SSean Cross 		matches++;
788954c3967SSean Cross 
789954c3967SSean Cross 	if (!of_property_read_u32(of_node, field2, &val2))
790954c3967SSean Cross 		matches++;
791954c3967SSean Cross 
792954c3967SSean Cross 	if (!of_property_read_u32(of_node, field3, &val3))
793954c3967SSean Cross 		matches++;
794954c3967SSean Cross 
795954c3967SSean Cross 	if (!of_property_read_u32(of_node, field4, &val4))
796954c3967SSean Cross 		matches++;
797954c3967SSean Cross 
798954c3967SSean Cross 	if (!matches)
799954c3967SSean Cross 		return 0;
800954c3967SSean Cross 
801954c3967SSean Cross 	if (matches < 4)
802954c3967SSean Cross 		newval = kszphy_extended_read(phydev, reg);
803954c3967SSean Cross 	else
804954c3967SSean Cross 		newval = 0;
805954c3967SSean Cross 
806954c3967SSean Cross 	if (val1 != -1)
807954c3967SSean Cross 		newval = ((newval & 0xfff0) | ((val1 / PS_TO_REG) & 0xf) << 0);
808954c3967SSean Cross 
8096a119745SHubert Chaumette 	if (val2 != -2)
810954c3967SSean Cross 		newval = ((newval & 0xff0f) | ((val2 / PS_TO_REG) & 0xf) << 4);
811954c3967SSean Cross 
8126a119745SHubert Chaumette 	if (val3 != -3)
813954c3967SSean Cross 		newval = ((newval & 0xf0ff) | ((val3 / PS_TO_REG) & 0xf) << 8);
814954c3967SSean Cross 
8156a119745SHubert Chaumette 	if (val4 != -4)
816954c3967SSean Cross 		newval = ((newval & 0x0fff) | ((val4 / PS_TO_REG) & 0xf) << 12);
817954c3967SSean Cross 
818954c3967SSean Cross 	return kszphy_extended_write(phydev, reg, newval);
819954c3967SSean Cross }
820954c3967SSean Cross 
821954c3967SSean Cross static int ksz9021_config_init(struct phy_device *phydev)
822954c3967SSean Cross {
823ce4f8afdSColin Ian King 	const struct device_node *of_node;
824651df218SAndrew Lunn 	const struct device *dev_walker;
825954c3967SSean Cross 
826651df218SAndrew Lunn 	/* The Micrel driver has a deprecated option to place phy OF
827651df218SAndrew Lunn 	 * properties in the MAC node. Walk up the tree of devices to
828651df218SAndrew Lunn 	 * find a device with an OF node.
829651df218SAndrew Lunn 	 */
830e5a03bfdSAndrew Lunn 	dev_walker = &phydev->mdio.dev;
831651df218SAndrew Lunn 	do {
832651df218SAndrew Lunn 		of_node = dev_walker->of_node;
833651df218SAndrew Lunn 		dev_walker = dev_walker->parent;
834651df218SAndrew Lunn 
835651df218SAndrew Lunn 	} while (!of_node && dev_walker);
836954c3967SSean Cross 
837954c3967SSean Cross 	if (of_node) {
838954c3967SSean Cross 		ksz9021_load_values_from_of(phydev, of_node,
839954c3967SSean Cross 				    MII_KSZPHY_CLK_CONTROL_PAD_SKEW,
840954c3967SSean Cross 				    "txen-skew-ps", "txc-skew-ps",
841954c3967SSean Cross 				    "rxdv-skew-ps", "rxc-skew-ps");
842954c3967SSean Cross 		ksz9021_load_values_from_of(phydev, of_node,
843954c3967SSean Cross 				    MII_KSZPHY_RX_DATA_PAD_SKEW,
844954c3967SSean Cross 				    "rxd0-skew-ps", "rxd1-skew-ps",
845954c3967SSean Cross 				    "rxd2-skew-ps", "rxd3-skew-ps");
846954c3967SSean Cross 		ksz9021_load_values_from_of(phydev, of_node,
847954c3967SSean Cross 				    MII_KSZPHY_TX_DATA_PAD_SKEW,
848954c3967SSean Cross 				    "txd0-skew-ps", "txd1-skew-ps",
849954c3967SSean Cross 				    "txd2-skew-ps", "txd3-skew-ps");
850954c3967SSean Cross 	}
851954c3967SSean Cross 	return 0;
852954c3967SSean Cross }
853954c3967SSean Cross 
8546e4b8273SHubert Chaumette #define KSZ9031_PS_TO_REG		60
8556e4b8273SHubert Chaumette 
8566e4b8273SHubert Chaumette /* Extended registers */
8576270e1aeSJaeden Amero /* MMD Address 0x0 */
8586270e1aeSJaeden Amero #define MII_KSZ9031RN_FLP_BURST_TX_LO	3
8596270e1aeSJaeden Amero #define MII_KSZ9031RN_FLP_BURST_TX_HI	4
8606270e1aeSJaeden Amero 
861ae6c97bbSJaeden Amero /* MMD Address 0x2 */
8626e4b8273SHubert Chaumette #define MII_KSZ9031RN_CONTROL_PAD_SKEW	4
863bcf3440cSOleksij Rempel #define MII_KSZ9031RN_RX_CTL_M		GENMASK(7, 4)
864bcf3440cSOleksij Rempel #define MII_KSZ9031RN_TX_CTL_M		GENMASK(3, 0)
865bcf3440cSOleksij Rempel 
8666e4b8273SHubert Chaumette #define MII_KSZ9031RN_RX_DATA_PAD_SKEW	5
867bcf3440cSOleksij Rempel #define MII_KSZ9031RN_RXD3		GENMASK(15, 12)
868bcf3440cSOleksij Rempel #define MII_KSZ9031RN_RXD2		GENMASK(11, 8)
869bcf3440cSOleksij Rempel #define MII_KSZ9031RN_RXD1		GENMASK(7, 4)
870bcf3440cSOleksij Rempel #define MII_KSZ9031RN_RXD0		GENMASK(3, 0)
871bcf3440cSOleksij Rempel 
8726e4b8273SHubert Chaumette #define MII_KSZ9031RN_TX_DATA_PAD_SKEW	6
873bcf3440cSOleksij Rempel #define MII_KSZ9031RN_TXD3		GENMASK(15, 12)
874bcf3440cSOleksij Rempel #define MII_KSZ9031RN_TXD2		GENMASK(11, 8)
875bcf3440cSOleksij Rempel #define MII_KSZ9031RN_TXD1		GENMASK(7, 4)
876bcf3440cSOleksij Rempel #define MII_KSZ9031RN_TXD0		GENMASK(3, 0)
877bcf3440cSOleksij Rempel 
8786e4b8273SHubert Chaumette #define MII_KSZ9031RN_CLK_PAD_SKEW	8
879bcf3440cSOleksij Rempel #define MII_KSZ9031RN_GTX_CLK		GENMASK(9, 5)
880bcf3440cSOleksij Rempel #define MII_KSZ9031RN_RX_CLK		GENMASK(4, 0)
881bcf3440cSOleksij Rempel 
882bcf3440cSOleksij Rempel /* KSZ9031 has internal RGMII_IDRX = 1.2ns and RGMII_IDTX = 0ns. To
883bcf3440cSOleksij Rempel  * provide different RGMII options we need to configure delay offset
884bcf3440cSOleksij Rempel  * for each pad relative to build in delay.
885bcf3440cSOleksij Rempel  */
886bcf3440cSOleksij Rempel /* keep rx as "No delay adjustment" and set rx_clk to +0.60ns to get delays of
887bcf3440cSOleksij Rempel  * 1.80ns
888bcf3440cSOleksij Rempel  */
889bcf3440cSOleksij Rempel #define RX_ID				0x7
890bcf3440cSOleksij Rempel #define RX_CLK_ID			0x19
891bcf3440cSOleksij Rempel 
892bcf3440cSOleksij Rempel /* set rx to +0.30ns and rx_clk to -0.90ns to compensate the
893bcf3440cSOleksij Rempel  * internal 1.2ns delay.
894bcf3440cSOleksij Rempel  */
895bcf3440cSOleksij Rempel #define RX_ND				0xc
896bcf3440cSOleksij Rempel #define RX_CLK_ND			0x0
897bcf3440cSOleksij Rempel 
898bcf3440cSOleksij Rempel /* set tx to -0.42ns and tx_clk to +0.96ns to get 1.38ns delay */
899bcf3440cSOleksij Rempel #define TX_ID				0x0
900bcf3440cSOleksij Rempel #define TX_CLK_ID			0x1f
901bcf3440cSOleksij Rempel 
902bcf3440cSOleksij Rempel /* set tx and tx_clk to "No delay adjustment" to keep 0ns
903bcf3440cSOleksij Rempel  * dealy
904bcf3440cSOleksij Rempel  */
905bcf3440cSOleksij Rempel #define TX_ND				0x7
906bcf3440cSOleksij Rempel #define TX_CLK_ND			0xf
9076e4b8273SHubert Chaumette 
908af70c1f9SMike Looijmans /* MMD Address 0x1C */
909af70c1f9SMike Looijmans #define MII_KSZ9031RN_EDPD		0x23
910af70c1f9SMike Looijmans #define MII_KSZ9031RN_EDPD_ENABLE	BIT(0)
911af70c1f9SMike Looijmans 
9126e4b8273SHubert Chaumette static int ksz9031_of_load_skew_values(struct phy_device *phydev,
9133c9a9f7fSJaeden Amero 				       const struct device_node *of_node,
9146e4b8273SHubert Chaumette 				       u16 reg, size_t field_sz,
915bcf3440cSOleksij Rempel 				       const char *field[], u8 numfields,
916bcf3440cSOleksij Rempel 				       bool *update)
9176e4b8273SHubert Chaumette {
9186e4b8273SHubert Chaumette 	int val[4] = {-1, -2, -3, -4};
9196e4b8273SHubert Chaumette 	int matches = 0;
9206e4b8273SHubert Chaumette 	u16 mask;
9216e4b8273SHubert Chaumette 	u16 maxval;
9226e4b8273SHubert Chaumette 	u16 newval;
9236e4b8273SHubert Chaumette 	int i;
9246e4b8273SHubert Chaumette 
9256e4b8273SHubert Chaumette 	for (i = 0; i < numfields; i++)
9266e4b8273SHubert Chaumette 		if (!of_property_read_u32(of_node, field[i], val + i))
9276e4b8273SHubert Chaumette 			matches++;
9286e4b8273SHubert Chaumette 
9296e4b8273SHubert Chaumette 	if (!matches)
9306e4b8273SHubert Chaumette 		return 0;
9316e4b8273SHubert Chaumette 
932bcf3440cSOleksij Rempel 	*update |= true;
933bcf3440cSOleksij Rempel 
9346e4b8273SHubert Chaumette 	if (matches < numfields)
9359b420effSHeiner Kallweit 		newval = phy_read_mmd(phydev, 2, reg);
9366e4b8273SHubert Chaumette 	else
9376e4b8273SHubert Chaumette 		newval = 0;
9386e4b8273SHubert Chaumette 
9396e4b8273SHubert Chaumette 	maxval = (field_sz == 4) ? 0xf : 0x1f;
9406e4b8273SHubert Chaumette 	for (i = 0; i < numfields; i++)
9416e4b8273SHubert Chaumette 		if (val[i] != -(i + 1)) {
9426e4b8273SHubert Chaumette 			mask = 0xffff;
9436e4b8273SHubert Chaumette 			mask ^= maxval << (field_sz * i);
9446e4b8273SHubert Chaumette 			newval = (newval & mask) |
9456e4b8273SHubert Chaumette 				(((val[i] / KSZ9031_PS_TO_REG) & maxval)
9466e4b8273SHubert Chaumette 					<< (field_sz * i));
9476e4b8273SHubert Chaumette 		}
9486e4b8273SHubert Chaumette 
9499b420effSHeiner Kallweit 	return phy_write_mmd(phydev, 2, reg, newval);
9506e4b8273SHubert Chaumette }
9516e4b8273SHubert Chaumette 
952a0da456bSMax Uvarov /* Center KSZ9031RNX FLP timing at 16ms. */
9536270e1aeSJaeden Amero static int ksz9031_center_flp_timing(struct phy_device *phydev)
9546270e1aeSJaeden Amero {
9556270e1aeSJaeden Amero 	int result;
9566270e1aeSJaeden Amero 
9579b420effSHeiner Kallweit 	result = phy_write_mmd(phydev, 0, MII_KSZ9031RN_FLP_BURST_TX_HI,
9589b420effSHeiner Kallweit 			       0x0006);
959a0da456bSMax Uvarov 	if (result)
960a0da456bSMax Uvarov 		return result;
961a0da456bSMax Uvarov 
9629b420effSHeiner Kallweit 	result = phy_write_mmd(phydev, 0, MII_KSZ9031RN_FLP_BURST_TX_LO,
9639b420effSHeiner Kallweit 			       0x1A80);
9646270e1aeSJaeden Amero 	if (result)
9656270e1aeSJaeden Amero 		return result;
9666270e1aeSJaeden Amero 
9676270e1aeSJaeden Amero 	return genphy_restart_aneg(phydev);
9686270e1aeSJaeden Amero }
9696270e1aeSJaeden Amero 
970af70c1f9SMike Looijmans /* Enable energy-detect power-down mode */
971af70c1f9SMike Looijmans static int ksz9031_enable_edpd(struct phy_device *phydev)
972af70c1f9SMike Looijmans {
973af70c1f9SMike Looijmans 	int reg;
974af70c1f9SMike Looijmans 
9759b420effSHeiner Kallweit 	reg = phy_read_mmd(phydev, 0x1C, MII_KSZ9031RN_EDPD);
976af70c1f9SMike Looijmans 	if (reg < 0)
977af70c1f9SMike Looijmans 		return reg;
9789b420effSHeiner Kallweit 	return phy_write_mmd(phydev, 0x1C, MII_KSZ9031RN_EDPD,
979af70c1f9SMike Looijmans 			     reg | MII_KSZ9031RN_EDPD_ENABLE);
980af70c1f9SMike Looijmans }
981af70c1f9SMike Looijmans 
982bcf3440cSOleksij Rempel static int ksz9031_config_rgmii_delay(struct phy_device *phydev)
983bcf3440cSOleksij Rempel {
984bcf3440cSOleksij Rempel 	u16 rx, tx, rx_clk, tx_clk;
985bcf3440cSOleksij Rempel 	int ret;
986bcf3440cSOleksij Rempel 
987bcf3440cSOleksij Rempel 	switch (phydev->interface) {
988bcf3440cSOleksij Rempel 	case PHY_INTERFACE_MODE_RGMII:
989bcf3440cSOleksij Rempel 		tx = TX_ND;
990bcf3440cSOleksij Rempel 		tx_clk = TX_CLK_ND;
991bcf3440cSOleksij Rempel 		rx = RX_ND;
992bcf3440cSOleksij Rempel 		rx_clk = RX_CLK_ND;
993bcf3440cSOleksij Rempel 		break;
994bcf3440cSOleksij Rempel 	case PHY_INTERFACE_MODE_RGMII_ID:
995bcf3440cSOleksij Rempel 		tx = TX_ID;
996bcf3440cSOleksij Rempel 		tx_clk = TX_CLK_ID;
997bcf3440cSOleksij Rempel 		rx = RX_ID;
998bcf3440cSOleksij Rempel 		rx_clk = RX_CLK_ID;
999bcf3440cSOleksij Rempel 		break;
1000bcf3440cSOleksij Rempel 	case PHY_INTERFACE_MODE_RGMII_RXID:
1001bcf3440cSOleksij Rempel 		tx = TX_ND;
1002bcf3440cSOleksij Rempel 		tx_clk = TX_CLK_ND;
1003bcf3440cSOleksij Rempel 		rx = RX_ID;
1004bcf3440cSOleksij Rempel 		rx_clk = RX_CLK_ID;
1005bcf3440cSOleksij Rempel 		break;
1006bcf3440cSOleksij Rempel 	case PHY_INTERFACE_MODE_RGMII_TXID:
1007bcf3440cSOleksij Rempel 		tx = TX_ID;
1008bcf3440cSOleksij Rempel 		tx_clk = TX_CLK_ID;
1009bcf3440cSOleksij Rempel 		rx = RX_ND;
1010bcf3440cSOleksij Rempel 		rx_clk = RX_CLK_ND;
1011bcf3440cSOleksij Rempel 		break;
1012bcf3440cSOleksij Rempel 	default:
1013bcf3440cSOleksij Rempel 		return 0;
1014bcf3440cSOleksij Rempel 	}
1015bcf3440cSOleksij Rempel 
1016bcf3440cSOleksij Rempel 	ret = phy_write_mmd(phydev, 2, MII_KSZ9031RN_CONTROL_PAD_SKEW,
1017bcf3440cSOleksij Rempel 			    FIELD_PREP(MII_KSZ9031RN_RX_CTL_M, rx) |
1018bcf3440cSOleksij Rempel 			    FIELD_PREP(MII_KSZ9031RN_TX_CTL_M, tx));
1019bcf3440cSOleksij Rempel 	if (ret < 0)
1020bcf3440cSOleksij Rempel 		return ret;
1021bcf3440cSOleksij Rempel 
1022bcf3440cSOleksij Rempel 	ret = phy_write_mmd(phydev, 2, MII_KSZ9031RN_RX_DATA_PAD_SKEW,
1023bcf3440cSOleksij Rempel 			    FIELD_PREP(MII_KSZ9031RN_RXD3, rx) |
1024bcf3440cSOleksij Rempel 			    FIELD_PREP(MII_KSZ9031RN_RXD2, rx) |
1025bcf3440cSOleksij Rempel 			    FIELD_PREP(MII_KSZ9031RN_RXD1, rx) |
1026bcf3440cSOleksij Rempel 			    FIELD_PREP(MII_KSZ9031RN_RXD0, rx));
1027bcf3440cSOleksij Rempel 	if (ret < 0)
1028bcf3440cSOleksij Rempel 		return ret;
1029bcf3440cSOleksij Rempel 
1030bcf3440cSOleksij Rempel 	ret = phy_write_mmd(phydev, 2, MII_KSZ9031RN_TX_DATA_PAD_SKEW,
1031bcf3440cSOleksij Rempel 			    FIELD_PREP(MII_KSZ9031RN_TXD3, tx) |
1032bcf3440cSOleksij Rempel 			    FIELD_PREP(MII_KSZ9031RN_TXD2, tx) |
1033bcf3440cSOleksij Rempel 			    FIELD_PREP(MII_KSZ9031RN_TXD1, tx) |
1034bcf3440cSOleksij Rempel 			    FIELD_PREP(MII_KSZ9031RN_TXD0, tx));
1035bcf3440cSOleksij Rempel 	if (ret < 0)
1036bcf3440cSOleksij Rempel 		return ret;
1037bcf3440cSOleksij Rempel 
1038bcf3440cSOleksij Rempel 	return phy_write_mmd(phydev, 2, MII_KSZ9031RN_CLK_PAD_SKEW,
1039bcf3440cSOleksij Rempel 			     FIELD_PREP(MII_KSZ9031RN_GTX_CLK, tx_clk) |
1040bcf3440cSOleksij Rempel 			     FIELD_PREP(MII_KSZ9031RN_RX_CLK, rx_clk));
1041bcf3440cSOleksij Rempel }
1042bcf3440cSOleksij Rempel 
10436e4b8273SHubert Chaumette static int ksz9031_config_init(struct phy_device *phydev)
10446e4b8273SHubert Chaumette {
1045ce4f8afdSColin Ian King 	const struct device_node *of_node;
10463c9a9f7fSJaeden Amero 	static const char *clk_skews[2] = {"rxc-skew-ps", "txc-skew-ps"};
10473c9a9f7fSJaeden Amero 	static const char *rx_data_skews[4] = {
10486e4b8273SHubert Chaumette 		"rxd0-skew-ps", "rxd1-skew-ps",
10496e4b8273SHubert Chaumette 		"rxd2-skew-ps", "rxd3-skew-ps"
10506e4b8273SHubert Chaumette 	};
10513c9a9f7fSJaeden Amero 	static const char *tx_data_skews[4] = {
10526e4b8273SHubert Chaumette 		"txd0-skew-ps", "txd1-skew-ps",
10536e4b8273SHubert Chaumette 		"txd2-skew-ps", "txd3-skew-ps"
10546e4b8273SHubert Chaumette 	};
10553c9a9f7fSJaeden Amero 	static const char *control_skews[2] = {"txen-skew-ps", "rxdv-skew-ps"};
1056b4c19f71SRoosen Henri 	const struct device *dev_walker;
1057af70c1f9SMike Looijmans 	int result;
1058af70c1f9SMike Looijmans 
1059af70c1f9SMike Looijmans 	result = ksz9031_enable_edpd(phydev);
1060af70c1f9SMike Looijmans 	if (result < 0)
1061af70c1f9SMike Looijmans 		return result;
10626e4b8273SHubert Chaumette 
1063b4c19f71SRoosen Henri 	/* The Micrel driver has a deprecated option to place phy OF
1064b4c19f71SRoosen Henri 	 * properties in the MAC node. Walk up the tree of devices to
1065b4c19f71SRoosen Henri 	 * find a device with an OF node.
1066b4c19f71SRoosen Henri 	 */
10679d367eddSDavid S. Miller 	dev_walker = &phydev->mdio.dev;
1068b4c19f71SRoosen Henri 	do {
1069b4c19f71SRoosen Henri 		of_node = dev_walker->of_node;
1070b4c19f71SRoosen Henri 		dev_walker = dev_walker->parent;
1071b4c19f71SRoosen Henri 	} while (!of_node && dev_walker);
10726e4b8273SHubert Chaumette 
10736e4b8273SHubert Chaumette 	if (of_node) {
1074bcf3440cSOleksij Rempel 		bool update = false;
1075bcf3440cSOleksij Rempel 
1076bcf3440cSOleksij Rempel 		if (phy_interface_is_rgmii(phydev)) {
1077bcf3440cSOleksij Rempel 			result = ksz9031_config_rgmii_delay(phydev);
1078bcf3440cSOleksij Rempel 			if (result < 0)
1079bcf3440cSOleksij Rempel 				return result;
1080bcf3440cSOleksij Rempel 		}
1081bcf3440cSOleksij Rempel 
10826e4b8273SHubert Chaumette 		ksz9031_of_load_skew_values(phydev, of_node,
10836e4b8273SHubert Chaumette 				MII_KSZ9031RN_CLK_PAD_SKEW, 5,
1084bcf3440cSOleksij Rempel 				clk_skews, 2, &update);
10856e4b8273SHubert Chaumette 
10866e4b8273SHubert Chaumette 		ksz9031_of_load_skew_values(phydev, of_node,
10876e4b8273SHubert Chaumette 				MII_KSZ9031RN_CONTROL_PAD_SKEW, 4,
1088bcf3440cSOleksij Rempel 				control_skews, 2, &update);
10896e4b8273SHubert Chaumette 
10906e4b8273SHubert Chaumette 		ksz9031_of_load_skew_values(phydev, of_node,
10916e4b8273SHubert Chaumette 				MII_KSZ9031RN_RX_DATA_PAD_SKEW, 4,
1092bcf3440cSOleksij Rempel 				rx_data_skews, 4, &update);
10936e4b8273SHubert Chaumette 
10946e4b8273SHubert Chaumette 		ksz9031_of_load_skew_values(phydev, of_node,
10956e4b8273SHubert Chaumette 				MII_KSZ9031RN_TX_DATA_PAD_SKEW, 4,
1096bcf3440cSOleksij Rempel 				tx_data_skews, 4, &update);
1097bcf3440cSOleksij Rempel 
109867ca5159SMatthias Schiffer 		if (update && !phy_interface_is_rgmii(phydev))
1099bcf3440cSOleksij Rempel 			phydev_warn(phydev,
110067ca5159SMatthias Schiffer 				    "*-skew-ps values should be used only with RGMII PHY modes\n");
1101e1b505a6SMarkus Niebel 
1102e1b505a6SMarkus Niebel 		/* Silicon Errata Sheet (DS80000691D or DS80000692D):
1103e1b505a6SMarkus Niebel 		 * When the device links in the 1000BASE-T slave mode only,
1104e1b505a6SMarkus Niebel 		 * the optional 125MHz reference output clock (CLK125_NDO)
1105e1b505a6SMarkus Niebel 		 * has wide duty cycle variation.
1106e1b505a6SMarkus Niebel 		 *
1107e1b505a6SMarkus Niebel 		 * The optional CLK125_NDO clock does not meet the RGMII
1108e1b505a6SMarkus Niebel 		 * 45/55 percent (min/max) duty cycle requirement and therefore
1109e1b505a6SMarkus Niebel 		 * cannot be used directly by the MAC side for clocking
1110e1b505a6SMarkus Niebel 		 * applications that have setup/hold time requirements on
1111e1b505a6SMarkus Niebel 		 * rising and falling clock edges.
1112e1b505a6SMarkus Niebel 		 *
1113e1b505a6SMarkus Niebel 		 * Workaround:
1114e1b505a6SMarkus Niebel 		 * Force the phy to be the master to receive a stable clock
1115e1b505a6SMarkus Niebel 		 * which meets the duty cycle requirement.
1116e1b505a6SMarkus Niebel 		 */
1117e1b505a6SMarkus Niebel 		if (of_property_read_bool(of_node, "micrel,force-master")) {
1118e1b505a6SMarkus Niebel 			result = phy_read(phydev, MII_CTRL1000);
1119e1b505a6SMarkus Niebel 			if (result < 0)
1120e1b505a6SMarkus Niebel 				goto err_force_master;
1121e1b505a6SMarkus Niebel 
1122e1b505a6SMarkus Niebel 			/* enable master mode, config & prefer master */
1123e1b505a6SMarkus Niebel 			result |= CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER;
1124e1b505a6SMarkus Niebel 			result = phy_write(phydev, MII_CTRL1000, result);
1125e1b505a6SMarkus Niebel 			if (result < 0)
1126e1b505a6SMarkus Niebel 				goto err_force_master;
1127e1b505a6SMarkus Niebel 		}
11286e4b8273SHubert Chaumette 	}
11296270e1aeSJaeden Amero 
11306270e1aeSJaeden Amero 	return ksz9031_center_flp_timing(phydev);
1131e1b505a6SMarkus Niebel 
1132e1b505a6SMarkus Niebel err_force_master:
1133e1b505a6SMarkus Niebel 	phydev_err(phydev, "failed to force the phy to master mode\n");
1134e1b505a6SMarkus Niebel 	return result;
11356e4b8273SHubert Chaumette }
11366e4b8273SHubert Chaumette 
1137bff5b4b3SYuiko Oshino #define KSZ9131_SKEW_5BIT_MAX	2400
1138bff5b4b3SYuiko Oshino #define KSZ9131_SKEW_4BIT_MAX	800
1139bff5b4b3SYuiko Oshino #define KSZ9131_OFFSET		700
1140bff5b4b3SYuiko Oshino #define KSZ9131_STEP		100
1141bff5b4b3SYuiko Oshino 
1142bff5b4b3SYuiko Oshino static int ksz9131_of_load_skew_values(struct phy_device *phydev,
1143bff5b4b3SYuiko Oshino 				       struct device_node *of_node,
1144bff5b4b3SYuiko Oshino 				       u16 reg, size_t field_sz,
1145bff5b4b3SYuiko Oshino 				       char *field[], u8 numfields)
1146bff5b4b3SYuiko Oshino {
1147bff5b4b3SYuiko Oshino 	int val[4] = {-(1 + KSZ9131_OFFSET), -(2 + KSZ9131_OFFSET),
1148bff5b4b3SYuiko Oshino 		      -(3 + KSZ9131_OFFSET), -(4 + KSZ9131_OFFSET)};
1149bff5b4b3SYuiko Oshino 	int skewval, skewmax = 0;
1150bff5b4b3SYuiko Oshino 	int matches = 0;
1151bff5b4b3SYuiko Oshino 	u16 maxval;
1152bff5b4b3SYuiko Oshino 	u16 newval;
1153bff5b4b3SYuiko Oshino 	u16 mask;
1154bff5b4b3SYuiko Oshino 	int i;
1155bff5b4b3SYuiko Oshino 
1156bff5b4b3SYuiko Oshino 	/* psec properties in dts should mean x pico seconds */
1157bff5b4b3SYuiko Oshino 	if (field_sz == 5)
1158bff5b4b3SYuiko Oshino 		skewmax = KSZ9131_SKEW_5BIT_MAX;
1159bff5b4b3SYuiko Oshino 	else
1160bff5b4b3SYuiko Oshino 		skewmax = KSZ9131_SKEW_4BIT_MAX;
1161bff5b4b3SYuiko Oshino 
1162bff5b4b3SYuiko Oshino 	for (i = 0; i < numfields; i++)
1163bff5b4b3SYuiko Oshino 		if (!of_property_read_s32(of_node, field[i], &skewval)) {
1164bff5b4b3SYuiko Oshino 			if (skewval < -KSZ9131_OFFSET)
1165bff5b4b3SYuiko Oshino 				skewval = -KSZ9131_OFFSET;
1166bff5b4b3SYuiko Oshino 			else if (skewval > skewmax)
1167bff5b4b3SYuiko Oshino 				skewval = skewmax;
1168bff5b4b3SYuiko Oshino 
1169bff5b4b3SYuiko Oshino 			val[i] = skewval + KSZ9131_OFFSET;
1170bff5b4b3SYuiko Oshino 			matches++;
1171bff5b4b3SYuiko Oshino 		}
1172bff5b4b3SYuiko Oshino 
1173bff5b4b3SYuiko Oshino 	if (!matches)
1174bff5b4b3SYuiko Oshino 		return 0;
1175bff5b4b3SYuiko Oshino 
1176bff5b4b3SYuiko Oshino 	if (matches < numfields)
11779b420effSHeiner Kallweit 		newval = phy_read_mmd(phydev, 2, reg);
1178bff5b4b3SYuiko Oshino 	else
1179bff5b4b3SYuiko Oshino 		newval = 0;
1180bff5b4b3SYuiko Oshino 
1181bff5b4b3SYuiko Oshino 	maxval = (field_sz == 4) ? 0xf : 0x1f;
1182bff5b4b3SYuiko Oshino 	for (i = 0; i < numfields; i++)
1183bff5b4b3SYuiko Oshino 		if (val[i] != -(i + 1 + KSZ9131_OFFSET)) {
1184bff5b4b3SYuiko Oshino 			mask = 0xffff;
1185bff5b4b3SYuiko Oshino 			mask ^= maxval << (field_sz * i);
1186bff5b4b3SYuiko Oshino 			newval = (newval & mask) |
1187bff5b4b3SYuiko Oshino 				(((val[i] / KSZ9131_STEP) & maxval)
1188bff5b4b3SYuiko Oshino 					<< (field_sz * i));
1189bff5b4b3SYuiko Oshino 		}
1190bff5b4b3SYuiko Oshino 
11919b420effSHeiner Kallweit 	return phy_write_mmd(phydev, 2, reg, newval);
1192bff5b4b3SYuiko Oshino }
1193bff5b4b3SYuiko Oshino 
1194bd734a74SPhilippe Schenker #define KSZ9131RN_MMD_COMMON_CTRL_REG	2
1195bd734a74SPhilippe Schenker #define KSZ9131RN_RXC_DLL_CTRL		76
1196bd734a74SPhilippe Schenker #define KSZ9131RN_TXC_DLL_CTRL		77
1197bd734a74SPhilippe Schenker #define KSZ9131RN_DLL_ENABLE_DELAY	0
1198bd734a74SPhilippe Schenker 
1199bd734a74SPhilippe Schenker static int ksz9131_config_rgmii_delay(struct phy_device *phydev)
1200bd734a74SPhilippe Schenker {
1201a8f1a19dSHoratiu Vultur 	const struct kszphy_type *type = phydev->drv->driver_data;
1202bd734a74SPhilippe Schenker 	u16 rxcdll_val, txcdll_val;
1203bd734a74SPhilippe Schenker 	int ret;
1204bd734a74SPhilippe Schenker 
1205bd734a74SPhilippe Schenker 	switch (phydev->interface) {
1206bd734a74SPhilippe Schenker 	case PHY_INTERFACE_MODE_RGMII:
1207a8f1a19dSHoratiu Vultur 		rxcdll_val = type->disable_dll_rx_bit;
1208a8f1a19dSHoratiu Vultur 		txcdll_val = type->disable_dll_tx_bit;
1209bd734a74SPhilippe Schenker 		break;
1210bd734a74SPhilippe Schenker 	case PHY_INTERFACE_MODE_RGMII_ID:
1211bd734a74SPhilippe Schenker 		rxcdll_val = KSZ9131RN_DLL_ENABLE_DELAY;
1212bd734a74SPhilippe Schenker 		txcdll_val = KSZ9131RN_DLL_ENABLE_DELAY;
1213bd734a74SPhilippe Schenker 		break;
1214bd734a74SPhilippe Schenker 	case PHY_INTERFACE_MODE_RGMII_RXID:
1215bd734a74SPhilippe Schenker 		rxcdll_val = KSZ9131RN_DLL_ENABLE_DELAY;
1216a8f1a19dSHoratiu Vultur 		txcdll_val = type->disable_dll_tx_bit;
1217bd734a74SPhilippe Schenker 		break;
1218bd734a74SPhilippe Schenker 	case PHY_INTERFACE_MODE_RGMII_TXID:
1219a8f1a19dSHoratiu Vultur 		rxcdll_val = type->disable_dll_rx_bit;
1220bd734a74SPhilippe Schenker 		txcdll_val = KSZ9131RN_DLL_ENABLE_DELAY;
1221bd734a74SPhilippe Schenker 		break;
1222bd734a74SPhilippe Schenker 	default:
1223bd734a74SPhilippe Schenker 		return 0;
1224bd734a74SPhilippe Schenker 	}
1225bd734a74SPhilippe Schenker 
1226bd734a74SPhilippe Schenker 	ret = phy_modify_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
1227a8f1a19dSHoratiu Vultur 			     KSZ9131RN_RXC_DLL_CTRL, type->disable_dll_mask,
1228bd734a74SPhilippe Schenker 			     rxcdll_val);
1229bd734a74SPhilippe Schenker 	if (ret < 0)
1230bd734a74SPhilippe Schenker 		return ret;
1231bd734a74SPhilippe Schenker 
1232bd734a74SPhilippe Schenker 	return phy_modify_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
1233a8f1a19dSHoratiu Vultur 			      KSZ9131RN_TXC_DLL_CTRL, type->disable_dll_mask,
1234bd734a74SPhilippe Schenker 			      txcdll_val);
1235bd734a74SPhilippe Schenker }
1236bd734a74SPhilippe Schenker 
12370316c7e6SFrancesco Dolcini /* Silicon Errata DS80000693B
12380316c7e6SFrancesco Dolcini  *
12390316c7e6SFrancesco Dolcini  * When LEDs are configured in Individual Mode, LED1 is ON in a no-link
12400316c7e6SFrancesco Dolcini  * condition. Workaround is to set register 0x1e, bit 9, this way LED1 behaves
12410316c7e6SFrancesco Dolcini  * according to the datasheet (off if there is no link).
12420316c7e6SFrancesco Dolcini  */
12430316c7e6SFrancesco Dolcini static int ksz9131_led_errata(struct phy_device *phydev)
12440316c7e6SFrancesco Dolcini {
12450316c7e6SFrancesco Dolcini 	int reg;
12460316c7e6SFrancesco Dolcini 
12470316c7e6SFrancesco Dolcini 	reg = phy_read_mmd(phydev, 2, 0);
12480316c7e6SFrancesco Dolcini 	if (reg < 0)
12490316c7e6SFrancesco Dolcini 		return reg;
12500316c7e6SFrancesco Dolcini 
12510316c7e6SFrancesco Dolcini 	if (!(reg & BIT(4)))
12520316c7e6SFrancesco Dolcini 		return 0;
12530316c7e6SFrancesco Dolcini 
12540316c7e6SFrancesco Dolcini 	return phy_set_bits(phydev, 0x1e, BIT(9));
12550316c7e6SFrancesco Dolcini }
12560316c7e6SFrancesco Dolcini 
1257bff5b4b3SYuiko Oshino static int ksz9131_config_init(struct phy_device *phydev)
1258bff5b4b3SYuiko Oshino {
1259ce4f8afdSColin Ian King 	struct device_node *of_node;
1260bff5b4b3SYuiko Oshino 	char *clk_skews[2] = {"rxc-skew-psec", "txc-skew-psec"};
1261bff5b4b3SYuiko Oshino 	char *rx_data_skews[4] = {
1262bff5b4b3SYuiko Oshino 		"rxd0-skew-psec", "rxd1-skew-psec",
1263bff5b4b3SYuiko Oshino 		"rxd2-skew-psec", "rxd3-skew-psec"
1264bff5b4b3SYuiko Oshino 	};
1265bff5b4b3SYuiko Oshino 	char *tx_data_skews[4] = {
1266bff5b4b3SYuiko Oshino 		"txd0-skew-psec", "txd1-skew-psec",
1267bff5b4b3SYuiko Oshino 		"txd2-skew-psec", "txd3-skew-psec"
1268bff5b4b3SYuiko Oshino 	};
1269bff5b4b3SYuiko Oshino 	char *control_skews[2] = {"txen-skew-psec", "rxdv-skew-psec"};
1270bff5b4b3SYuiko Oshino 	const struct device *dev_walker;
1271bff5b4b3SYuiko Oshino 	int ret;
1272bff5b4b3SYuiko Oshino 
1273bff5b4b3SYuiko Oshino 	dev_walker = &phydev->mdio.dev;
1274bff5b4b3SYuiko Oshino 	do {
1275bff5b4b3SYuiko Oshino 		of_node = dev_walker->of_node;
1276bff5b4b3SYuiko Oshino 		dev_walker = dev_walker->parent;
1277bff5b4b3SYuiko Oshino 	} while (!of_node && dev_walker);
1278bff5b4b3SYuiko Oshino 
1279bff5b4b3SYuiko Oshino 	if (!of_node)
1280bff5b4b3SYuiko Oshino 		return 0;
1281bff5b4b3SYuiko Oshino 
1282bd734a74SPhilippe Schenker 	if (phy_interface_is_rgmii(phydev)) {
1283bd734a74SPhilippe Schenker 		ret = ksz9131_config_rgmii_delay(phydev);
1284bd734a74SPhilippe Schenker 		if (ret < 0)
1285bd734a74SPhilippe Schenker 			return ret;
1286bd734a74SPhilippe Schenker 	}
1287bd734a74SPhilippe Schenker 
1288bff5b4b3SYuiko Oshino 	ret = ksz9131_of_load_skew_values(phydev, of_node,
1289bff5b4b3SYuiko Oshino 					  MII_KSZ9031RN_CLK_PAD_SKEW, 5,
1290bff5b4b3SYuiko Oshino 					  clk_skews, 2);
1291bff5b4b3SYuiko Oshino 	if (ret < 0)
1292bff5b4b3SYuiko Oshino 		return ret;
1293bff5b4b3SYuiko Oshino 
1294bff5b4b3SYuiko Oshino 	ret = ksz9131_of_load_skew_values(phydev, of_node,
1295bff5b4b3SYuiko Oshino 					  MII_KSZ9031RN_CONTROL_PAD_SKEW, 4,
1296bff5b4b3SYuiko Oshino 					  control_skews, 2);
1297bff5b4b3SYuiko Oshino 	if (ret < 0)
1298bff5b4b3SYuiko Oshino 		return ret;
1299bff5b4b3SYuiko Oshino 
1300bff5b4b3SYuiko Oshino 	ret = ksz9131_of_load_skew_values(phydev, of_node,
1301bff5b4b3SYuiko Oshino 					  MII_KSZ9031RN_RX_DATA_PAD_SKEW, 4,
1302bff5b4b3SYuiko Oshino 					  rx_data_skews, 4);
1303bff5b4b3SYuiko Oshino 	if (ret < 0)
1304bff5b4b3SYuiko Oshino 		return ret;
1305bff5b4b3SYuiko Oshino 
1306bff5b4b3SYuiko Oshino 	ret = ksz9131_of_load_skew_values(phydev, of_node,
1307bff5b4b3SYuiko Oshino 					  MII_KSZ9031RN_TX_DATA_PAD_SKEW, 4,
1308bff5b4b3SYuiko Oshino 					  tx_data_skews, 4);
1309bff5b4b3SYuiko Oshino 	if (ret < 0)
1310bff5b4b3SYuiko Oshino 		return ret;
1311bff5b4b3SYuiko Oshino 
13120316c7e6SFrancesco Dolcini 	ret = ksz9131_led_errata(phydev);
13130316c7e6SFrancesco Dolcini 	if (ret < 0)
13140316c7e6SFrancesco Dolcini 		return ret;
13150316c7e6SFrancesco Dolcini 
1316bff5b4b3SYuiko Oshino 	return 0;
1317bff5b4b3SYuiko Oshino }
1318bff5b4b3SYuiko Oshino 
1319b64e6a87SRaju Lakkaraju #define MII_KSZ9131_AUTO_MDIX		0x1C
1320b64e6a87SRaju Lakkaraju #define MII_KSZ9131_AUTO_MDI_SET	BIT(7)
1321b64e6a87SRaju Lakkaraju #define MII_KSZ9131_AUTO_MDIX_SWAP_OFF	BIT(6)
1322b64e6a87SRaju Lakkaraju 
1323b64e6a87SRaju Lakkaraju static int ksz9131_mdix_update(struct phy_device *phydev)
1324b64e6a87SRaju Lakkaraju {
1325b64e6a87SRaju Lakkaraju 	int ret;
1326b64e6a87SRaju Lakkaraju 
1327b64e6a87SRaju Lakkaraju 	ret = phy_read(phydev, MII_KSZ9131_AUTO_MDIX);
1328b64e6a87SRaju Lakkaraju 	if (ret < 0)
1329b64e6a87SRaju Lakkaraju 		return ret;
1330b64e6a87SRaju Lakkaraju 
1331b64e6a87SRaju Lakkaraju 	if (ret & MII_KSZ9131_AUTO_MDIX_SWAP_OFF) {
1332b64e6a87SRaju Lakkaraju 		if (ret & MII_KSZ9131_AUTO_MDI_SET)
1333b64e6a87SRaju Lakkaraju 			phydev->mdix_ctrl = ETH_TP_MDI;
1334b64e6a87SRaju Lakkaraju 		else
1335b64e6a87SRaju Lakkaraju 			phydev->mdix_ctrl = ETH_TP_MDI_X;
1336b64e6a87SRaju Lakkaraju 	} else {
1337b64e6a87SRaju Lakkaraju 		phydev->mdix_ctrl = ETH_TP_MDI_AUTO;
1338b64e6a87SRaju Lakkaraju 	}
1339b64e6a87SRaju Lakkaraju 
1340b64e6a87SRaju Lakkaraju 	if (ret & MII_KSZ9131_AUTO_MDI_SET)
1341b64e6a87SRaju Lakkaraju 		phydev->mdix = ETH_TP_MDI;
1342b64e6a87SRaju Lakkaraju 	else
1343b64e6a87SRaju Lakkaraju 		phydev->mdix = ETH_TP_MDI_X;
1344b64e6a87SRaju Lakkaraju 
1345b64e6a87SRaju Lakkaraju 	return 0;
1346b64e6a87SRaju Lakkaraju }
1347b64e6a87SRaju Lakkaraju 
1348b64e6a87SRaju Lakkaraju static int ksz9131_config_mdix(struct phy_device *phydev, u8 ctrl)
1349b64e6a87SRaju Lakkaraju {
1350b64e6a87SRaju Lakkaraju 	u16 val;
1351b64e6a87SRaju Lakkaraju 
1352b64e6a87SRaju Lakkaraju 	switch (ctrl) {
1353b64e6a87SRaju Lakkaraju 	case ETH_TP_MDI:
1354b64e6a87SRaju Lakkaraju 		val = MII_KSZ9131_AUTO_MDIX_SWAP_OFF |
1355b64e6a87SRaju Lakkaraju 		      MII_KSZ9131_AUTO_MDI_SET;
1356b64e6a87SRaju Lakkaraju 		break;
1357b64e6a87SRaju Lakkaraju 	case ETH_TP_MDI_X:
1358b64e6a87SRaju Lakkaraju 		val = MII_KSZ9131_AUTO_MDIX_SWAP_OFF;
1359b64e6a87SRaju Lakkaraju 		break;
1360b64e6a87SRaju Lakkaraju 	case ETH_TP_MDI_AUTO:
1361b64e6a87SRaju Lakkaraju 		val = 0;
1362b64e6a87SRaju Lakkaraju 		break;
1363b64e6a87SRaju Lakkaraju 	default:
1364b64e6a87SRaju Lakkaraju 		return 0;
1365b64e6a87SRaju Lakkaraju 	}
1366b64e6a87SRaju Lakkaraju 
1367b64e6a87SRaju Lakkaraju 	return phy_modify(phydev, MII_KSZ9131_AUTO_MDIX,
1368b64e6a87SRaju Lakkaraju 			  MII_KSZ9131_AUTO_MDIX_SWAP_OFF |
1369b64e6a87SRaju Lakkaraju 			  MII_KSZ9131_AUTO_MDI_SET, val);
1370b64e6a87SRaju Lakkaraju }
1371b64e6a87SRaju Lakkaraju 
1372b64e6a87SRaju Lakkaraju static int ksz9131_read_status(struct phy_device *phydev)
1373b64e6a87SRaju Lakkaraju {
1374b64e6a87SRaju Lakkaraju 	int ret;
1375b64e6a87SRaju Lakkaraju 
1376b64e6a87SRaju Lakkaraju 	ret = ksz9131_mdix_update(phydev);
1377b64e6a87SRaju Lakkaraju 	if (ret < 0)
1378b64e6a87SRaju Lakkaraju 		return ret;
1379b64e6a87SRaju Lakkaraju 
1380b64e6a87SRaju Lakkaraju 	return genphy_read_status(phydev);
1381b64e6a87SRaju Lakkaraju }
1382b64e6a87SRaju Lakkaraju 
1383b64e6a87SRaju Lakkaraju static int ksz9131_config_aneg(struct phy_device *phydev)
1384b64e6a87SRaju Lakkaraju {
1385b64e6a87SRaju Lakkaraju 	int ret;
1386b64e6a87SRaju Lakkaraju 
1387b64e6a87SRaju Lakkaraju 	ret = ksz9131_config_mdix(phydev, phydev->mdix_ctrl);
1388b64e6a87SRaju Lakkaraju 	if (ret)
1389b64e6a87SRaju Lakkaraju 		return ret;
1390b64e6a87SRaju Lakkaraju 
1391b64e6a87SRaju Lakkaraju 	return genphy_config_aneg(phydev);
1392b64e6a87SRaju Lakkaraju }
1393b64e6a87SRaju Lakkaraju 
139448fb1994SOleksij Rempel static int ksz9477_get_features(struct phy_device *phydev)
139548fb1994SOleksij Rempel {
139648fb1994SOleksij Rempel 	int ret;
139748fb1994SOleksij Rempel 
139848fb1994SOleksij Rempel 	ret = genphy_read_abilities(phydev);
139948fb1994SOleksij Rempel 	if (ret)
140048fb1994SOleksij Rempel 		return ret;
140148fb1994SOleksij Rempel 
140248fb1994SOleksij Rempel 	/* The "EEE control and capability 1" (Register 3.20) seems to be
140348fb1994SOleksij Rempel 	 * influenced by the "EEE advertisement 1" (Register 7.60). Changes
140448fb1994SOleksij Rempel 	 * on the 7.60 will affect 3.20. So, we need to construct our own list
140548fb1994SOleksij Rempel 	 * of caps.
140648fb1994SOleksij Rempel 	 * KSZ8563R should have 100BaseTX/Full only.
140748fb1994SOleksij Rempel 	 */
140848fb1994SOleksij Rempel 	linkmode_and(phydev->supported_eee, phydev->supported,
140948fb1994SOleksij Rempel 		     PHY_EEE_CAP1_FEATURES);
141048fb1994SOleksij Rempel 
141148fb1994SOleksij Rempel 	return 0;
141248fb1994SOleksij Rempel }
141348fb1994SOleksij Rempel 
141493272e07SJean-Christophe PLAGNIOL-VILLARD #define KSZ8873MLL_GLOBAL_CONTROL_4	0x06
141500aee095SJohan Hovold #define KSZ8873MLL_GLOBAL_CONTROL_4_DUPLEX	BIT(6)
141600aee095SJohan Hovold #define KSZ8873MLL_GLOBAL_CONTROL_4_SPEED	BIT(4)
141732d73b14SJingoo Han static int ksz8873mll_read_status(struct phy_device *phydev)
141893272e07SJean-Christophe PLAGNIOL-VILLARD {
141993272e07SJean-Christophe PLAGNIOL-VILLARD 	int regval;
142093272e07SJean-Christophe PLAGNIOL-VILLARD 
142193272e07SJean-Christophe PLAGNIOL-VILLARD 	/* dummy read */
142293272e07SJean-Christophe PLAGNIOL-VILLARD 	regval = phy_read(phydev, KSZ8873MLL_GLOBAL_CONTROL_4);
142393272e07SJean-Christophe PLAGNIOL-VILLARD 
142493272e07SJean-Christophe PLAGNIOL-VILLARD 	regval = phy_read(phydev, KSZ8873MLL_GLOBAL_CONTROL_4);
142593272e07SJean-Christophe PLAGNIOL-VILLARD 
142693272e07SJean-Christophe PLAGNIOL-VILLARD 	if (regval & KSZ8873MLL_GLOBAL_CONTROL_4_DUPLEX)
142793272e07SJean-Christophe PLAGNIOL-VILLARD 		phydev->duplex = DUPLEX_HALF;
142893272e07SJean-Christophe PLAGNIOL-VILLARD 	else
142993272e07SJean-Christophe PLAGNIOL-VILLARD 		phydev->duplex = DUPLEX_FULL;
143093272e07SJean-Christophe PLAGNIOL-VILLARD 
143193272e07SJean-Christophe PLAGNIOL-VILLARD 	if (regval & KSZ8873MLL_GLOBAL_CONTROL_4_SPEED)
143293272e07SJean-Christophe PLAGNIOL-VILLARD 		phydev->speed = SPEED_10;
143393272e07SJean-Christophe PLAGNIOL-VILLARD 	else
143493272e07SJean-Christophe PLAGNIOL-VILLARD 		phydev->speed = SPEED_100;
143593272e07SJean-Christophe PLAGNIOL-VILLARD 
143693272e07SJean-Christophe PLAGNIOL-VILLARD 	phydev->link = 1;
143793272e07SJean-Christophe PLAGNIOL-VILLARD 	phydev->pause = phydev->asym_pause = 0;
143893272e07SJean-Christophe PLAGNIOL-VILLARD 
143993272e07SJean-Christophe PLAGNIOL-VILLARD 	return 0;
144093272e07SJean-Christophe PLAGNIOL-VILLARD }
144193272e07SJean-Christophe PLAGNIOL-VILLARD 
14423aed3e2aSAntoine Tenart static int ksz9031_get_features(struct phy_device *phydev)
14433aed3e2aSAntoine Tenart {
14443aed3e2aSAntoine Tenart 	int ret;
14453aed3e2aSAntoine Tenart 
14463aed3e2aSAntoine Tenart 	ret = genphy_read_abilities(phydev);
14473aed3e2aSAntoine Tenart 	if (ret < 0)
14483aed3e2aSAntoine Tenart 		return ret;
14493aed3e2aSAntoine Tenart 
14503aed3e2aSAntoine Tenart 	/* Silicon Errata Sheet (DS80000691D or DS80000692D):
14513aed3e2aSAntoine Tenart 	 * Whenever the device's Asymmetric Pause capability is set to 1,
14523aed3e2aSAntoine Tenart 	 * link-up may fail after a link-up to link-down transition.
14533aed3e2aSAntoine Tenart 	 *
1454407d8098SHans Andersson 	 * The Errata Sheet is for ksz9031, but ksz9021 has the same issue
1455407d8098SHans Andersson 	 *
14563aed3e2aSAntoine Tenart 	 * Workaround:
14573aed3e2aSAntoine Tenart 	 * Do not enable the Asymmetric Pause capability bit.
14583aed3e2aSAntoine Tenart 	 */
14593aed3e2aSAntoine Tenart 	linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported);
14603aed3e2aSAntoine Tenart 
14613aed3e2aSAntoine Tenart 	/* We force setting the Pause capability as the core will force the
14623aed3e2aSAntoine Tenart 	 * Asymmetric Pause capability to 1 otherwise.
14633aed3e2aSAntoine Tenart 	 */
14643aed3e2aSAntoine Tenart 	linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported);
14653aed3e2aSAntoine Tenart 
14663aed3e2aSAntoine Tenart 	return 0;
14673aed3e2aSAntoine Tenart }
14683aed3e2aSAntoine Tenart 
1469d2fd719bSNathan Sullivan static int ksz9031_read_status(struct phy_device *phydev)
1470d2fd719bSNathan Sullivan {
1471d2fd719bSNathan Sullivan 	int err;
1472d2fd719bSNathan Sullivan 	int regval;
1473d2fd719bSNathan Sullivan 
1474d2fd719bSNathan Sullivan 	err = genphy_read_status(phydev);
1475d2fd719bSNathan Sullivan 	if (err)
1476d2fd719bSNathan Sullivan 		return err;
1477d2fd719bSNathan Sullivan 
1478d2fd719bSNathan Sullivan 	/* Make sure the PHY is not broken. Read idle error count,
1479d2fd719bSNathan Sullivan 	 * and reset the PHY if it is maxed out.
1480d2fd719bSNathan Sullivan 	 */
1481d2fd719bSNathan Sullivan 	regval = phy_read(phydev, MII_STAT1000);
1482d2fd719bSNathan Sullivan 	if ((regval & 0xFF) == 0xFF) {
1483d2fd719bSNathan Sullivan 		phy_init_hw(phydev);
1484d2fd719bSNathan Sullivan 		phydev->link = 0;
1485b866203dSZach Brown 		if (phydev->drv->config_intr && phy_interrupt_is_valid(phydev))
1486b866203dSZach Brown 			phydev->drv->config_intr(phydev);
1487c1a8d0a3SGrygorii Strashko 		return genphy_config_aneg(phydev);
1488d2fd719bSNathan Sullivan 	}
1489d2fd719bSNathan Sullivan 
1490d2fd719bSNathan Sullivan 	return 0;
1491d2fd719bSNathan Sullivan }
1492d2fd719bSNathan Sullivan 
149358389c00SMarek Vasut static int ksz9x31_cable_test_start(struct phy_device *phydev)
149458389c00SMarek Vasut {
149558389c00SMarek Vasut 	struct kszphy_priv *priv = phydev->priv;
149658389c00SMarek Vasut 	int ret;
149758389c00SMarek Vasut 
149858389c00SMarek Vasut 	/* KSZ9131RNX, DS00002841B-page 38, 4.14 LinkMD (R) Cable Diagnostic
149958389c00SMarek Vasut 	 * Prior to running the cable diagnostics, Auto-negotiation should
150058389c00SMarek Vasut 	 * be disabled, full duplex set and the link speed set to 1000Mbps
150158389c00SMarek Vasut 	 * via the Basic Control Register.
150258389c00SMarek Vasut 	 */
150358389c00SMarek Vasut 	ret = phy_modify(phydev, MII_BMCR,
150458389c00SMarek Vasut 			 BMCR_SPEED1000 | BMCR_FULLDPLX |
150558389c00SMarek Vasut 			 BMCR_ANENABLE | BMCR_SPEED100,
150658389c00SMarek Vasut 			 BMCR_SPEED1000 | BMCR_FULLDPLX);
150758389c00SMarek Vasut 	if (ret)
150858389c00SMarek Vasut 		return ret;
150958389c00SMarek Vasut 
151058389c00SMarek Vasut 	/* KSZ9131RNX, DS00002841B-page 38, 4.14 LinkMD (R) Cable Diagnostic
151158389c00SMarek Vasut 	 * The Master-Slave configuration should be set to Slave by writing
151258389c00SMarek Vasut 	 * a value of 0x1000 to the Auto-Negotiation Master Slave Control
151358389c00SMarek Vasut 	 * Register.
151458389c00SMarek Vasut 	 */
151558389c00SMarek Vasut 	ret = phy_read(phydev, MII_CTRL1000);
151658389c00SMarek Vasut 	if (ret < 0)
151758389c00SMarek Vasut 		return ret;
151858389c00SMarek Vasut 
151958389c00SMarek Vasut 	/* Cache these bits, they need to be restored once LinkMD finishes. */
152058389c00SMarek Vasut 	priv->vct_ctrl1000 = ret & (CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER);
152158389c00SMarek Vasut 	ret &= ~(CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER);
152258389c00SMarek Vasut 	ret |= CTL1000_ENABLE_MASTER;
152358389c00SMarek Vasut 
152458389c00SMarek Vasut 	return phy_write(phydev, MII_CTRL1000, ret);
152558389c00SMarek Vasut }
152658389c00SMarek Vasut 
152758389c00SMarek Vasut static int ksz9x31_cable_test_result_trans(u16 status)
152858389c00SMarek Vasut {
152958389c00SMarek Vasut 	switch (FIELD_GET(KSZ9x31_LMD_VCT_ST_MASK, status)) {
153058389c00SMarek Vasut 	case KSZ9x31_LMD_VCT_ST_NORMAL:
153158389c00SMarek Vasut 		return ETHTOOL_A_CABLE_RESULT_CODE_OK;
153258389c00SMarek Vasut 	case KSZ9x31_LMD_VCT_ST_OPEN:
153358389c00SMarek Vasut 		return ETHTOOL_A_CABLE_RESULT_CODE_OPEN;
153458389c00SMarek Vasut 	case KSZ9x31_LMD_VCT_ST_SHORT:
153558389c00SMarek Vasut 		return ETHTOOL_A_CABLE_RESULT_CODE_SAME_SHORT;
153658389c00SMarek Vasut 	case KSZ9x31_LMD_VCT_ST_FAIL:
153758389c00SMarek Vasut 		fallthrough;
153858389c00SMarek Vasut 	default:
153958389c00SMarek Vasut 		return ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC;
154058389c00SMarek Vasut 	}
154158389c00SMarek Vasut }
154258389c00SMarek Vasut 
154358389c00SMarek Vasut static bool ksz9x31_cable_test_failed(u16 status)
154458389c00SMarek Vasut {
154558389c00SMarek Vasut 	int stat = FIELD_GET(KSZ9x31_LMD_VCT_ST_MASK, status);
154658389c00SMarek Vasut 
154758389c00SMarek Vasut 	return stat == KSZ9x31_LMD_VCT_ST_FAIL;
154858389c00SMarek Vasut }
154958389c00SMarek Vasut 
155058389c00SMarek Vasut static bool ksz9x31_cable_test_fault_length_valid(u16 status)
155158389c00SMarek Vasut {
155258389c00SMarek Vasut 	switch (FIELD_GET(KSZ9x31_LMD_VCT_ST_MASK, status)) {
155358389c00SMarek Vasut 	case KSZ9x31_LMD_VCT_ST_OPEN:
155458389c00SMarek Vasut 		fallthrough;
155558389c00SMarek Vasut 	case KSZ9x31_LMD_VCT_ST_SHORT:
155658389c00SMarek Vasut 		return true;
155758389c00SMarek Vasut 	}
155858389c00SMarek Vasut 	return false;
155958389c00SMarek Vasut }
156058389c00SMarek Vasut 
156158389c00SMarek Vasut static int ksz9x31_cable_test_fault_length(struct phy_device *phydev, u16 stat)
156258389c00SMarek Vasut {
156358389c00SMarek Vasut 	int dt = FIELD_GET(KSZ9x31_LMD_VCT_DATA_MASK, stat);
156458389c00SMarek Vasut 
156558389c00SMarek Vasut 	/* KSZ9131RNX, DS00002841B-page 38, 4.14 LinkMD (R) Cable Diagnostic
156658389c00SMarek Vasut 	 *
156758389c00SMarek Vasut 	 * distance to fault = (VCT_DATA - 22) * 4 / cable propagation velocity
156858389c00SMarek Vasut 	 */
156958389c00SMarek Vasut 	if ((phydev->phy_id & MICREL_PHY_ID_MASK) == PHY_ID_KSZ9131)
157058389c00SMarek Vasut 		dt = clamp(dt - 22, 0, 255);
157158389c00SMarek Vasut 
157258389c00SMarek Vasut 	return (dt * 400) / 10;
157358389c00SMarek Vasut }
157458389c00SMarek Vasut 
157558389c00SMarek Vasut static int ksz9x31_cable_test_wait_for_completion(struct phy_device *phydev)
157658389c00SMarek Vasut {
157758389c00SMarek Vasut 	int val, ret;
157858389c00SMarek Vasut 
157958389c00SMarek Vasut 	ret = phy_read_poll_timeout(phydev, KSZ9x31_LMD, val,
158058389c00SMarek Vasut 				    !(val & KSZ9x31_LMD_VCT_EN),
158158389c00SMarek Vasut 				    30000, 100000, true);
158258389c00SMarek Vasut 
158358389c00SMarek Vasut 	return ret < 0 ? ret : 0;
158458389c00SMarek Vasut }
158558389c00SMarek Vasut 
158658389c00SMarek Vasut static int ksz9x31_cable_test_get_pair(int pair)
158758389c00SMarek Vasut {
158858389c00SMarek Vasut 	static const int ethtool_pair[] = {
158958389c00SMarek Vasut 		ETHTOOL_A_CABLE_PAIR_A,
159058389c00SMarek Vasut 		ETHTOOL_A_CABLE_PAIR_B,
159158389c00SMarek Vasut 		ETHTOOL_A_CABLE_PAIR_C,
159258389c00SMarek Vasut 		ETHTOOL_A_CABLE_PAIR_D,
159358389c00SMarek Vasut 	};
159458389c00SMarek Vasut 
159558389c00SMarek Vasut 	return ethtool_pair[pair];
159658389c00SMarek Vasut }
159758389c00SMarek Vasut 
159858389c00SMarek Vasut static int ksz9x31_cable_test_one_pair(struct phy_device *phydev, int pair)
159958389c00SMarek Vasut {
160058389c00SMarek Vasut 	int ret, val;
160158389c00SMarek Vasut 
160258389c00SMarek Vasut 	/* KSZ9131RNX, DS00002841B-page 38, 4.14 LinkMD (R) Cable Diagnostic
160358389c00SMarek Vasut 	 * To test each individual cable pair, set the cable pair in the Cable
160458389c00SMarek Vasut 	 * Diagnostics Test Pair (VCT_PAIR[1:0]) field of the LinkMD Cable
160558389c00SMarek Vasut 	 * Diagnostic Register, along with setting the Cable Diagnostics Test
160658389c00SMarek Vasut 	 * Enable (VCT_EN) bit. The Cable Diagnostics Test Enable (VCT_EN) bit
160758389c00SMarek Vasut 	 * will self clear when the test is concluded.
160858389c00SMarek Vasut 	 */
160958389c00SMarek Vasut 	ret = phy_write(phydev, KSZ9x31_LMD,
161058389c00SMarek Vasut 			KSZ9x31_LMD_VCT_EN | KSZ9x31_LMD_VCT_PAIR(pair));
161158389c00SMarek Vasut 	if (ret)
161258389c00SMarek Vasut 		return ret;
161358389c00SMarek Vasut 
161458389c00SMarek Vasut 	ret = ksz9x31_cable_test_wait_for_completion(phydev);
161558389c00SMarek Vasut 	if (ret)
161658389c00SMarek Vasut 		return ret;
161758389c00SMarek Vasut 
161858389c00SMarek Vasut 	val = phy_read(phydev, KSZ9x31_LMD);
161958389c00SMarek Vasut 	if (val < 0)
162058389c00SMarek Vasut 		return val;
162158389c00SMarek Vasut 
162258389c00SMarek Vasut 	if (ksz9x31_cable_test_failed(val))
162358389c00SMarek Vasut 		return -EAGAIN;
162458389c00SMarek Vasut 
162558389c00SMarek Vasut 	ret = ethnl_cable_test_result(phydev,
162658389c00SMarek Vasut 				      ksz9x31_cable_test_get_pair(pair),
162758389c00SMarek Vasut 				      ksz9x31_cable_test_result_trans(val));
162858389c00SMarek Vasut 	if (ret)
162958389c00SMarek Vasut 		return ret;
163058389c00SMarek Vasut 
163158389c00SMarek Vasut 	if (!ksz9x31_cable_test_fault_length_valid(val))
163258389c00SMarek Vasut 		return 0;
163358389c00SMarek Vasut 
163458389c00SMarek Vasut 	return ethnl_cable_test_fault_length(phydev,
163558389c00SMarek Vasut 					     ksz9x31_cable_test_get_pair(pair),
163658389c00SMarek Vasut 					     ksz9x31_cable_test_fault_length(phydev, val));
163758389c00SMarek Vasut }
163858389c00SMarek Vasut 
163958389c00SMarek Vasut static int ksz9x31_cable_test_get_status(struct phy_device *phydev,
164058389c00SMarek Vasut 					 bool *finished)
164158389c00SMarek Vasut {
164258389c00SMarek Vasut 	struct kszphy_priv *priv = phydev->priv;
164358389c00SMarek Vasut 	unsigned long pair_mask = 0xf;
164458389c00SMarek Vasut 	int retries = 20;
164558389c00SMarek Vasut 	int pair, ret, rv;
164658389c00SMarek Vasut 
164758389c00SMarek Vasut 	*finished = false;
164858389c00SMarek Vasut 
164958389c00SMarek Vasut 	/* Try harder if link partner is active */
165058389c00SMarek Vasut 	while (pair_mask && retries--) {
165158389c00SMarek Vasut 		for_each_set_bit(pair, &pair_mask, 4) {
165258389c00SMarek Vasut 			ret = ksz9x31_cable_test_one_pair(phydev, pair);
165358389c00SMarek Vasut 			if (ret == -EAGAIN)
165458389c00SMarek Vasut 				continue;
165558389c00SMarek Vasut 			if (ret < 0)
165658389c00SMarek Vasut 				return ret;
165758389c00SMarek Vasut 			clear_bit(pair, &pair_mask);
165858389c00SMarek Vasut 		}
165958389c00SMarek Vasut 		/* If link partner is in autonegotiation mode it will send 2ms
166058389c00SMarek Vasut 		 * of FLPs with at least 6ms of silence.
166158389c00SMarek Vasut 		 * Add 2ms sleep to have better chances to hit this silence.
166258389c00SMarek Vasut 		 */
166358389c00SMarek Vasut 		if (pair_mask)
166458389c00SMarek Vasut 			usleep_range(2000, 3000);
166558389c00SMarek Vasut 	}
166658389c00SMarek Vasut 
166758389c00SMarek Vasut 	/* Report remaining unfinished pair result as unknown. */
166858389c00SMarek Vasut 	for_each_set_bit(pair, &pair_mask, 4) {
166958389c00SMarek Vasut 		ret = ethnl_cable_test_result(phydev,
167058389c00SMarek Vasut 					      ksz9x31_cable_test_get_pair(pair),
167158389c00SMarek Vasut 					      ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC);
167258389c00SMarek Vasut 	}
167358389c00SMarek Vasut 
167458389c00SMarek Vasut 	*finished = true;
167558389c00SMarek Vasut 
167658389c00SMarek Vasut 	/* Restore cached bits from before LinkMD got started. */
167758389c00SMarek Vasut 	rv = phy_modify(phydev, MII_CTRL1000,
167858389c00SMarek Vasut 			CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER,
167958389c00SMarek Vasut 			priv->vct_ctrl1000);
168058389c00SMarek Vasut 	if (rv)
168158389c00SMarek Vasut 		return rv;
168258389c00SMarek Vasut 
168358389c00SMarek Vasut 	return ret;
168458389c00SMarek Vasut }
168558389c00SMarek Vasut 
168693272e07SJean-Christophe PLAGNIOL-VILLARD static int ksz8873mll_config_aneg(struct phy_device *phydev)
168793272e07SJean-Christophe PLAGNIOL-VILLARD {
168893272e07SJean-Christophe PLAGNIOL-VILLARD 	return 0;
168993272e07SJean-Christophe PLAGNIOL-VILLARD }
169093272e07SJean-Christophe PLAGNIOL-VILLARD 
169152939393SOleksij Rempel static int ksz886x_config_mdix(struct phy_device *phydev, u8 ctrl)
169252939393SOleksij Rempel {
169352939393SOleksij Rempel 	u16 val;
169452939393SOleksij Rempel 
169552939393SOleksij Rempel 	switch (ctrl) {
169652939393SOleksij Rempel 	case ETH_TP_MDI:
169752939393SOleksij Rempel 		val = KSZ886X_BMCR_DISABLE_AUTO_MDIX;
169852939393SOleksij Rempel 		break;
169952939393SOleksij Rempel 	case ETH_TP_MDI_X:
170052939393SOleksij Rempel 		/* Note: The naming of the bit KSZ886X_BMCR_FORCE_MDI is bit
170152939393SOleksij Rempel 		 * counter intuitive, the "-X" in "1 = Force MDI" in the data
170252939393SOleksij Rempel 		 * sheet seems to be missing:
170352939393SOleksij Rempel 		 * 1 = Force MDI (sic!) (transmit on RX+/RX- pins)
170452939393SOleksij Rempel 		 * 0 = Normal operation (transmit on TX+/TX- pins)
170552939393SOleksij Rempel 		 */
170652939393SOleksij Rempel 		val = KSZ886X_BMCR_DISABLE_AUTO_MDIX | KSZ886X_BMCR_FORCE_MDI;
170752939393SOleksij Rempel 		break;
170852939393SOleksij Rempel 	case ETH_TP_MDI_AUTO:
170952939393SOleksij Rempel 		val = 0;
171052939393SOleksij Rempel 		break;
171152939393SOleksij Rempel 	default:
171252939393SOleksij Rempel 		return 0;
171352939393SOleksij Rempel 	}
171452939393SOleksij Rempel 
171552939393SOleksij Rempel 	return phy_modify(phydev, MII_BMCR,
171652939393SOleksij Rempel 			  KSZ886X_BMCR_HP_MDIX | KSZ886X_BMCR_FORCE_MDI |
171752939393SOleksij Rempel 			  KSZ886X_BMCR_DISABLE_AUTO_MDIX,
171852939393SOleksij Rempel 			  KSZ886X_BMCR_HP_MDIX | val);
171952939393SOleksij Rempel }
172052939393SOleksij Rempel 
172152939393SOleksij Rempel static int ksz886x_config_aneg(struct phy_device *phydev)
172252939393SOleksij Rempel {
172352939393SOleksij Rempel 	int ret;
172452939393SOleksij Rempel 
172552939393SOleksij Rempel 	ret = genphy_config_aneg(phydev);
172652939393SOleksij Rempel 	if (ret)
172752939393SOleksij Rempel 		return ret;
172852939393SOleksij Rempel 
172952939393SOleksij Rempel 	/* The MDI-X configuration is automatically changed by the PHY after
173052939393SOleksij Rempel 	 * switching from autoneg off to on. So, take MDI-X configuration under
173152939393SOleksij Rempel 	 * own control and set it after autoneg configuration was done.
173252939393SOleksij Rempel 	 */
173352939393SOleksij Rempel 	return ksz886x_config_mdix(phydev, phydev->mdix_ctrl);
173452939393SOleksij Rempel }
173552939393SOleksij Rempel 
173652939393SOleksij Rempel static int ksz886x_mdix_update(struct phy_device *phydev)
173752939393SOleksij Rempel {
173852939393SOleksij Rempel 	int ret;
173952939393SOleksij Rempel 
174052939393SOleksij Rempel 	ret = phy_read(phydev, MII_BMCR);
174152939393SOleksij Rempel 	if (ret < 0)
174252939393SOleksij Rempel 		return ret;
174352939393SOleksij Rempel 
174452939393SOleksij Rempel 	if (ret & KSZ886X_BMCR_DISABLE_AUTO_MDIX) {
174552939393SOleksij Rempel 		if (ret & KSZ886X_BMCR_FORCE_MDI)
174652939393SOleksij Rempel 			phydev->mdix_ctrl = ETH_TP_MDI_X;
174752939393SOleksij Rempel 		else
174852939393SOleksij Rempel 			phydev->mdix_ctrl = ETH_TP_MDI;
174952939393SOleksij Rempel 	} else {
175052939393SOleksij Rempel 		phydev->mdix_ctrl = ETH_TP_MDI_AUTO;
175152939393SOleksij Rempel 	}
175252939393SOleksij Rempel 
175352939393SOleksij Rempel 	ret = phy_read(phydev, MII_KSZPHY_CTRL);
175452939393SOleksij Rempel 	if (ret < 0)
175552939393SOleksij Rempel 		return ret;
175652939393SOleksij Rempel 
175752939393SOleksij Rempel 	/* Same reverse logic as KSZ886X_BMCR_FORCE_MDI */
175852939393SOleksij Rempel 	if (ret & KSZ886X_CTRL_MDIX_STAT)
175952939393SOleksij Rempel 		phydev->mdix = ETH_TP_MDI_X;
176052939393SOleksij Rempel 	else
176152939393SOleksij Rempel 		phydev->mdix = ETH_TP_MDI;
176252939393SOleksij Rempel 
176352939393SOleksij Rempel 	return 0;
176452939393SOleksij Rempel }
176552939393SOleksij Rempel 
176652939393SOleksij Rempel static int ksz886x_read_status(struct phy_device *phydev)
176752939393SOleksij Rempel {
176852939393SOleksij Rempel 	int ret;
176952939393SOleksij Rempel 
177052939393SOleksij Rempel 	ret = ksz886x_mdix_update(phydev);
177152939393SOleksij Rempel 	if (ret < 0)
177252939393SOleksij Rempel 		return ret;
177352939393SOleksij Rempel 
177452939393SOleksij Rempel 	return genphy_read_status(phydev);
177552939393SOleksij Rempel }
177652939393SOleksij Rempel 
17772b2427d0SAndrew Lunn static int kszphy_get_sset_count(struct phy_device *phydev)
17782b2427d0SAndrew Lunn {
17792b2427d0SAndrew Lunn 	return ARRAY_SIZE(kszphy_hw_stats);
17802b2427d0SAndrew Lunn }
17812b2427d0SAndrew Lunn 
17822b2427d0SAndrew Lunn static void kszphy_get_strings(struct phy_device *phydev, u8 *data)
17832b2427d0SAndrew Lunn {
17842b2427d0SAndrew Lunn 	int i;
17852b2427d0SAndrew Lunn 
17862b2427d0SAndrew Lunn 	for (i = 0; i < ARRAY_SIZE(kszphy_hw_stats); i++) {
1787fb3ceec1SWolfram Sang 		strscpy(data + i * ETH_GSTRING_LEN,
17882b2427d0SAndrew Lunn 			kszphy_hw_stats[i].string, ETH_GSTRING_LEN);
17892b2427d0SAndrew Lunn 	}
17902b2427d0SAndrew Lunn }
17912b2427d0SAndrew Lunn 
17922b2427d0SAndrew Lunn static u64 kszphy_get_stat(struct phy_device *phydev, int i)
17932b2427d0SAndrew Lunn {
17942b2427d0SAndrew Lunn 	struct kszphy_hw_stat stat = kszphy_hw_stats[i];
17952b2427d0SAndrew Lunn 	struct kszphy_priv *priv = phydev->priv;
1796321b4d4bSAndrew Lunn 	int val;
1797321b4d4bSAndrew Lunn 	u64 ret;
17982b2427d0SAndrew Lunn 
17992b2427d0SAndrew Lunn 	val = phy_read(phydev, stat.reg);
18002b2427d0SAndrew Lunn 	if (val < 0) {
18016c3442f5SJisheng Zhang 		ret = U64_MAX;
18022b2427d0SAndrew Lunn 	} else {
18032b2427d0SAndrew Lunn 		val = val & ((1 << stat.bits) - 1);
18042b2427d0SAndrew Lunn 		priv->stats[i] += val;
1805321b4d4bSAndrew Lunn 		ret = priv->stats[i];
18062b2427d0SAndrew Lunn 	}
18072b2427d0SAndrew Lunn 
1808321b4d4bSAndrew Lunn 	return ret;
18092b2427d0SAndrew Lunn }
18102b2427d0SAndrew Lunn 
18112b2427d0SAndrew Lunn static void kszphy_get_stats(struct phy_device *phydev,
18122b2427d0SAndrew Lunn 			     struct ethtool_stats *stats, u64 *data)
18132b2427d0SAndrew Lunn {
18142b2427d0SAndrew Lunn 	int i;
18152b2427d0SAndrew Lunn 
18162b2427d0SAndrew Lunn 	for (i = 0; i < ARRAY_SIZE(kszphy_hw_stats); i++)
18172b2427d0SAndrew Lunn 		data[i] = kszphy_get_stat(phydev, i);
18182b2427d0SAndrew Lunn }
18192b2427d0SAndrew Lunn 
1820836384d2SWenyou Yang static int kszphy_suspend(struct phy_device *phydev)
1821836384d2SWenyou Yang {
1822836384d2SWenyou Yang 	/* Disable PHY Interrupts */
1823836384d2SWenyou Yang 	if (phy_interrupt_is_valid(phydev)) {
1824836384d2SWenyou Yang 		phydev->interrupts = PHY_INTERRUPT_DISABLED;
1825836384d2SWenyou Yang 		if (phydev->drv->config_intr)
1826836384d2SWenyou Yang 			phydev->drv->config_intr(phydev);
1827836384d2SWenyou Yang 	}
1828836384d2SWenyou Yang 
1829836384d2SWenyou Yang 	return genphy_suspend(phydev);
1830836384d2SWenyou Yang }
1831836384d2SWenyou Yang 
1832a516b7f7SDivya Koppera static void kszphy_parse_led_mode(struct phy_device *phydev)
1833a516b7f7SDivya Koppera {
1834a516b7f7SDivya Koppera 	const struct kszphy_type *type = phydev->drv->driver_data;
1835a516b7f7SDivya Koppera 	const struct device_node *np = phydev->mdio.dev.of_node;
1836a516b7f7SDivya Koppera 	struct kszphy_priv *priv = phydev->priv;
1837a516b7f7SDivya Koppera 	int ret;
1838a516b7f7SDivya Koppera 
1839a516b7f7SDivya Koppera 	if (type && type->led_mode_reg) {
1840a516b7f7SDivya Koppera 		ret = of_property_read_u32(np, "micrel,led-mode",
1841a516b7f7SDivya Koppera 					   &priv->led_mode);
1842a516b7f7SDivya Koppera 
1843a516b7f7SDivya Koppera 		if (ret)
1844a516b7f7SDivya Koppera 			priv->led_mode = -1;
1845a516b7f7SDivya Koppera 
1846a516b7f7SDivya Koppera 		if (priv->led_mode > 3) {
1847a516b7f7SDivya Koppera 			phydev_err(phydev, "invalid led mode: 0x%02x\n",
1848a516b7f7SDivya Koppera 				   priv->led_mode);
1849a516b7f7SDivya Koppera 			priv->led_mode = -1;
1850a516b7f7SDivya Koppera 		}
1851a516b7f7SDivya Koppera 	} else {
1852a516b7f7SDivya Koppera 		priv->led_mode = -1;
1853a516b7f7SDivya Koppera 	}
1854a516b7f7SDivya Koppera }
1855a516b7f7SDivya Koppera 
1856f5aba91dSAlexandre Belloni static int kszphy_resume(struct phy_device *phydev)
1857f5aba91dSAlexandre Belloni {
185879e498a9SLeonard Crestez 	int ret;
185979e498a9SLeonard Crestez 
1860836384d2SWenyou Yang 	genphy_resume(phydev);
1861f5aba91dSAlexandre Belloni 
18626110dff7SOleksij Rempel 	/* After switching from power-down to normal mode, an internal global
18636110dff7SOleksij Rempel 	 * reset is automatically generated. Wait a minimum of 1 ms before
18646110dff7SOleksij Rempel 	 * read/write access to the PHY registers.
18656110dff7SOleksij Rempel 	 */
18666110dff7SOleksij Rempel 	usleep_range(1000, 2000);
18676110dff7SOleksij Rempel 
186879e498a9SLeonard Crestez 	ret = kszphy_config_reset(phydev);
186979e498a9SLeonard Crestez 	if (ret)
187079e498a9SLeonard Crestez 		return ret;
187179e498a9SLeonard Crestez 
1872836384d2SWenyou Yang 	/* Enable PHY Interrupts */
1873836384d2SWenyou Yang 	if (phy_interrupt_is_valid(phydev)) {
1874836384d2SWenyou Yang 		phydev->interrupts = PHY_INTERRUPT_ENABLED;
1875836384d2SWenyou Yang 		if (phydev->drv->config_intr)
1876836384d2SWenyou Yang 			phydev->drv->config_intr(phydev);
1877836384d2SWenyou Yang 	}
1878f5aba91dSAlexandre Belloni 
1879f5aba91dSAlexandre Belloni 	return 0;
1880f5aba91dSAlexandre Belloni }
1881f5aba91dSAlexandre Belloni 
1882e6a423a8SJohan Hovold static int kszphy_probe(struct phy_device *phydev)
1883e6a423a8SJohan Hovold {
1884e6a423a8SJohan Hovold 	const struct kszphy_type *type = phydev->drv->driver_data;
1885e5a03bfdSAndrew Lunn 	const struct device_node *np = phydev->mdio.dev.of_node;
1886e6a423a8SJohan Hovold 	struct kszphy_priv *priv;
188763f44b2bSJohan Hovold 	struct clk *clk;
1888e6a423a8SJohan Hovold 
1889e5a03bfdSAndrew Lunn 	priv = devm_kzalloc(&phydev->mdio.dev, sizeof(*priv), GFP_KERNEL);
1890e6a423a8SJohan Hovold 	if (!priv)
1891e6a423a8SJohan Hovold 		return -ENOMEM;
1892e6a423a8SJohan Hovold 
1893e6a423a8SJohan Hovold 	phydev->priv = priv;
1894e6a423a8SJohan Hovold 
1895e6a423a8SJohan Hovold 	priv->type = type;
1896e6a423a8SJohan Hovold 
1897a516b7f7SDivya Koppera 	kszphy_parse_led_mode(phydev);
1898e7a792e9SJohan Hovold 
1899e5a03bfdSAndrew Lunn 	clk = devm_clk_get(&phydev->mdio.dev, "rmii-ref");
1900bced8701SNiklas Cassel 	/* NOTE: clk may be NULL if building without CONFIG_HAVE_CLK */
1901bced8701SNiklas Cassel 	if (!IS_ERR_OR_NULL(clk)) {
19021fadee0cSSascha Hauer 		unsigned long rate = clk_get_rate(clk);
190386dc1342SJohan Hovold 		bool rmii_ref_clk_sel_25_mhz;
19041fadee0cSSascha Hauer 
1905f2ef6f75SFabio Estevam 		if (type)
190663f44b2bSJohan Hovold 			priv->rmii_ref_clk_sel = type->has_rmii_ref_clk_sel;
190786dc1342SJohan Hovold 		rmii_ref_clk_sel_25_mhz = of_property_read_bool(np,
190886dc1342SJohan Hovold 				"micrel,rmii-reference-clock-select-25-mhz");
190963f44b2bSJohan Hovold 
19101fadee0cSSascha Hauer 		if (rate > 24500000 && rate < 25500000) {
191186dc1342SJohan Hovold 			priv->rmii_ref_clk_sel_val = rmii_ref_clk_sel_25_mhz;
19121fadee0cSSascha Hauer 		} else if (rate > 49500000 && rate < 50500000) {
191386dc1342SJohan Hovold 			priv->rmii_ref_clk_sel_val = !rmii_ref_clk_sel_25_mhz;
19141fadee0cSSascha Hauer 		} else {
191572ba48beSAndrew Lunn 			phydev_err(phydev, "Clock rate out of range: %ld\n",
191672ba48beSAndrew Lunn 				   rate);
19171fadee0cSSascha Hauer 			return -EINVAL;
19181fadee0cSSascha Hauer 		}
19191fadee0cSSascha Hauer 	}
19201fadee0cSSascha Hauer 
19214217a64eSMichael Walle 	if (ksz8041_fiber_mode(phydev))
19224217a64eSMichael Walle 		phydev->port = PORT_FIBRE;
19234217a64eSMichael Walle 
192463f44b2bSJohan Hovold 	/* Support legacy board-file configuration */
192563f44b2bSJohan Hovold 	if (phydev->dev_flags & MICREL_PHY_50MHZ_CLK) {
192663f44b2bSJohan Hovold 		priv->rmii_ref_clk_sel = true;
192763f44b2bSJohan Hovold 		priv->rmii_ref_clk_sel_val = true;
192863f44b2bSJohan Hovold 	}
192963f44b2bSJohan Hovold 
193063f44b2bSJohan Hovold 	return 0;
19311fadee0cSSascha Hauer }
19321fadee0cSSascha Hauer 
193321b688daSDivya Koppera static int lan8814_cable_test_start(struct phy_device *phydev)
193421b688daSDivya Koppera {
193521b688daSDivya Koppera 	/* If autoneg is enabled, we won't be able to test cross pair
193621b688daSDivya Koppera 	 * short. In this case, the PHY will "detect" a link and
193721b688daSDivya Koppera 	 * confuse the internal state machine - disable auto neg here.
193821b688daSDivya Koppera 	 * Set the speed to 1000mbit and full duplex.
193921b688daSDivya Koppera 	 */
194021b688daSDivya Koppera 	return phy_modify(phydev, MII_BMCR, BMCR_ANENABLE | BMCR_SPEED100,
194121b688daSDivya Koppera 			  BMCR_SPEED1000 | BMCR_FULLDPLX);
194221b688daSDivya Koppera }
194321b688daSDivya Koppera 
194449011e0cSOleksij Rempel static int ksz886x_cable_test_start(struct phy_device *phydev)
194549011e0cSOleksij Rempel {
194649011e0cSOleksij Rempel 	if (phydev->dev_flags & MICREL_KSZ8_P1_ERRATA)
194749011e0cSOleksij Rempel 		return -EOPNOTSUPP;
194849011e0cSOleksij Rempel 
194949011e0cSOleksij Rempel 	/* If autoneg is enabled, we won't be able to test cross pair
195049011e0cSOleksij Rempel 	 * short. In this case, the PHY will "detect" a link and
195149011e0cSOleksij Rempel 	 * confuse the internal state machine - disable auto neg here.
195249011e0cSOleksij Rempel 	 * If autoneg is disabled, we should set the speed to 10mbit.
195349011e0cSOleksij Rempel 	 */
195449011e0cSOleksij Rempel 	return phy_clear_bits(phydev, MII_BMCR, BMCR_ANENABLE | BMCR_SPEED100);
195549011e0cSOleksij Rempel }
195649011e0cSOleksij Rempel 
1957fa182ea2SDivya Koppera static __always_inline int ksz886x_cable_test_result_trans(u16 status, u16 mask)
195849011e0cSOleksij Rempel {
195921b688daSDivya Koppera 	switch (FIELD_GET(mask, status)) {
196049011e0cSOleksij Rempel 	case KSZ8081_LMD_STAT_NORMAL:
196149011e0cSOleksij Rempel 		return ETHTOOL_A_CABLE_RESULT_CODE_OK;
196249011e0cSOleksij Rempel 	case KSZ8081_LMD_STAT_SHORT:
196349011e0cSOleksij Rempel 		return ETHTOOL_A_CABLE_RESULT_CODE_SAME_SHORT;
196449011e0cSOleksij Rempel 	case KSZ8081_LMD_STAT_OPEN:
196549011e0cSOleksij Rempel 		return ETHTOOL_A_CABLE_RESULT_CODE_OPEN;
196649011e0cSOleksij Rempel 	case KSZ8081_LMD_STAT_FAIL:
196749011e0cSOleksij Rempel 		fallthrough;
196849011e0cSOleksij Rempel 	default:
196949011e0cSOleksij Rempel 		return ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC;
197049011e0cSOleksij Rempel 	}
197149011e0cSOleksij Rempel }
197249011e0cSOleksij Rempel 
1973fa182ea2SDivya Koppera static __always_inline bool ksz886x_cable_test_failed(u16 status, u16 mask)
197449011e0cSOleksij Rempel {
197521b688daSDivya Koppera 	return FIELD_GET(mask, status) ==
197649011e0cSOleksij Rempel 		KSZ8081_LMD_STAT_FAIL;
197749011e0cSOleksij Rempel }
197849011e0cSOleksij Rempel 
1979fa182ea2SDivya Koppera static __always_inline bool ksz886x_cable_test_fault_length_valid(u16 status, u16 mask)
198049011e0cSOleksij Rempel {
198121b688daSDivya Koppera 	switch (FIELD_GET(mask, status)) {
198249011e0cSOleksij Rempel 	case KSZ8081_LMD_STAT_OPEN:
198349011e0cSOleksij Rempel 		fallthrough;
198449011e0cSOleksij Rempel 	case KSZ8081_LMD_STAT_SHORT:
198549011e0cSOleksij Rempel 		return true;
198649011e0cSOleksij Rempel 	}
198749011e0cSOleksij Rempel 	return false;
198849011e0cSOleksij Rempel }
198949011e0cSOleksij Rempel 
1990fa182ea2SDivya Koppera static __always_inline int ksz886x_cable_test_fault_length(struct phy_device *phydev,
1991fa182ea2SDivya Koppera 							   u16 status, u16 data_mask)
199249011e0cSOleksij Rempel {
199349011e0cSOleksij Rempel 	int dt;
199449011e0cSOleksij Rempel 
199549011e0cSOleksij Rempel 	/* According to the data sheet the distance to the fault is
199621b688daSDivya Koppera 	 * DELTA_TIME * 0.4 meters for ksz phys.
199721b688daSDivya Koppera 	 * (DELTA_TIME - 22) * 0.8 for lan8814 phy.
199849011e0cSOleksij Rempel 	 */
199921b688daSDivya Koppera 	dt = FIELD_GET(data_mask, status);
200049011e0cSOleksij Rempel 
200121b688daSDivya Koppera 	if ((phydev->phy_id & MICREL_PHY_ID_MASK) == PHY_ID_LAN8814)
200221b688daSDivya Koppera 		return ((dt - 22) * 800) / 10;
200321b688daSDivya Koppera 	else
200449011e0cSOleksij Rempel 		return (dt * 400) / 10;
200549011e0cSOleksij Rempel }
200649011e0cSOleksij Rempel 
200749011e0cSOleksij Rempel static int ksz886x_cable_test_wait_for_completion(struct phy_device *phydev)
200849011e0cSOleksij Rempel {
200921b688daSDivya Koppera 	const struct kszphy_type *type = phydev->drv->driver_data;
201049011e0cSOleksij Rempel 	int val, ret;
201149011e0cSOleksij Rempel 
201221b688daSDivya Koppera 	ret = phy_read_poll_timeout(phydev, type->cable_diag_reg, val,
201349011e0cSOleksij Rempel 				    !(val & KSZ8081_LMD_ENABLE_TEST),
201449011e0cSOleksij Rempel 				    30000, 100000, true);
201549011e0cSOleksij Rempel 
201649011e0cSOleksij Rempel 	return ret < 0 ? ret : 0;
201749011e0cSOleksij Rempel }
201849011e0cSOleksij Rempel 
201921b688daSDivya Koppera static int lan8814_cable_test_one_pair(struct phy_device *phydev, int pair)
202021b688daSDivya Koppera {
202121b688daSDivya Koppera 	static const int ethtool_pair[] = { ETHTOOL_A_CABLE_PAIR_A,
202221b688daSDivya Koppera 					    ETHTOOL_A_CABLE_PAIR_B,
202321b688daSDivya Koppera 					    ETHTOOL_A_CABLE_PAIR_C,
202421b688daSDivya Koppera 					    ETHTOOL_A_CABLE_PAIR_D,
202521b688daSDivya Koppera 					  };
202621b688daSDivya Koppera 	u32 fault_length;
202721b688daSDivya Koppera 	int ret;
202821b688daSDivya Koppera 	int val;
202921b688daSDivya Koppera 
203021b688daSDivya Koppera 	val = KSZ8081_LMD_ENABLE_TEST;
203121b688daSDivya Koppera 	val = val | (pair << LAN8814_PAIR_BIT_SHIFT);
203221b688daSDivya Koppera 
203321b688daSDivya Koppera 	ret = phy_write(phydev, LAN8814_CABLE_DIAG, val);
203421b688daSDivya Koppera 	if (ret < 0)
203521b688daSDivya Koppera 		return ret;
203621b688daSDivya Koppera 
203721b688daSDivya Koppera 	ret = ksz886x_cable_test_wait_for_completion(phydev);
203821b688daSDivya Koppera 	if (ret)
203921b688daSDivya Koppera 		return ret;
204021b688daSDivya Koppera 
204121b688daSDivya Koppera 	val = phy_read(phydev, LAN8814_CABLE_DIAG);
204221b688daSDivya Koppera 	if (val < 0)
204321b688daSDivya Koppera 		return val;
204421b688daSDivya Koppera 
204521b688daSDivya Koppera 	if (ksz886x_cable_test_failed(val, LAN8814_CABLE_DIAG_STAT_MASK))
204621b688daSDivya Koppera 		return -EAGAIN;
204721b688daSDivya Koppera 
204821b688daSDivya Koppera 	ret = ethnl_cable_test_result(phydev, ethtool_pair[pair],
204921b688daSDivya Koppera 				      ksz886x_cable_test_result_trans(val,
205021b688daSDivya Koppera 								      LAN8814_CABLE_DIAG_STAT_MASK
205121b688daSDivya Koppera 								      ));
205221b688daSDivya Koppera 	if (ret)
205321b688daSDivya Koppera 		return ret;
205421b688daSDivya Koppera 
205521b688daSDivya Koppera 	if (!ksz886x_cable_test_fault_length_valid(val, LAN8814_CABLE_DIAG_STAT_MASK))
205621b688daSDivya Koppera 		return 0;
205721b688daSDivya Koppera 
205821b688daSDivya Koppera 	fault_length = ksz886x_cable_test_fault_length(phydev, val,
205921b688daSDivya Koppera 						       LAN8814_CABLE_DIAG_VCT_DATA_MASK);
206021b688daSDivya Koppera 
206121b688daSDivya Koppera 	return ethnl_cable_test_fault_length(phydev, ethtool_pair[pair], fault_length);
206221b688daSDivya Koppera }
206321b688daSDivya Koppera 
206449011e0cSOleksij Rempel static int ksz886x_cable_test_one_pair(struct phy_device *phydev, int pair)
206549011e0cSOleksij Rempel {
206649011e0cSOleksij Rempel 	static const int ethtool_pair[] = {
206749011e0cSOleksij Rempel 		ETHTOOL_A_CABLE_PAIR_A,
206849011e0cSOleksij Rempel 		ETHTOOL_A_CABLE_PAIR_B,
206949011e0cSOleksij Rempel 	};
207049011e0cSOleksij Rempel 	int ret, val, mdix;
207121b688daSDivya Koppera 	u32 fault_length;
207249011e0cSOleksij Rempel 
207349011e0cSOleksij Rempel 	/* There is no way to choice the pair, like we do one ksz9031.
207449011e0cSOleksij Rempel 	 * We can workaround this limitation by using the MDI-X functionality.
207549011e0cSOleksij Rempel 	 */
207649011e0cSOleksij Rempel 	if (pair == 0)
207749011e0cSOleksij Rempel 		mdix = ETH_TP_MDI;
207849011e0cSOleksij Rempel 	else
207949011e0cSOleksij Rempel 		mdix = ETH_TP_MDI_X;
208049011e0cSOleksij Rempel 
208149011e0cSOleksij Rempel 	switch (phydev->phy_id & MICREL_PHY_ID_MASK) {
208249011e0cSOleksij Rempel 	case PHY_ID_KSZ8081:
208349011e0cSOleksij Rempel 		ret = ksz8081_config_mdix(phydev, mdix);
208449011e0cSOleksij Rempel 		break;
208549011e0cSOleksij Rempel 	case PHY_ID_KSZ886X:
208649011e0cSOleksij Rempel 		ret = ksz886x_config_mdix(phydev, mdix);
208749011e0cSOleksij Rempel 		break;
208849011e0cSOleksij Rempel 	default:
208949011e0cSOleksij Rempel 		ret = -ENODEV;
209049011e0cSOleksij Rempel 	}
209149011e0cSOleksij Rempel 
209249011e0cSOleksij Rempel 	if (ret)
209349011e0cSOleksij Rempel 		return ret;
209449011e0cSOleksij Rempel 
209549011e0cSOleksij Rempel 	/* Now we are ready to fire. This command will send a 100ns pulse
209649011e0cSOleksij Rempel 	 * to the pair.
209749011e0cSOleksij Rempel 	 */
209849011e0cSOleksij Rempel 	ret = phy_write(phydev, KSZ8081_LMD, KSZ8081_LMD_ENABLE_TEST);
209949011e0cSOleksij Rempel 	if (ret)
210049011e0cSOleksij Rempel 		return ret;
210149011e0cSOleksij Rempel 
210249011e0cSOleksij Rempel 	ret = ksz886x_cable_test_wait_for_completion(phydev);
210349011e0cSOleksij Rempel 	if (ret)
210449011e0cSOleksij Rempel 		return ret;
210549011e0cSOleksij Rempel 
210649011e0cSOleksij Rempel 	val = phy_read(phydev, KSZ8081_LMD);
210749011e0cSOleksij Rempel 	if (val < 0)
210849011e0cSOleksij Rempel 		return val;
210949011e0cSOleksij Rempel 
211021b688daSDivya Koppera 	if (ksz886x_cable_test_failed(val, KSZ8081_LMD_STAT_MASK))
211149011e0cSOleksij Rempel 		return -EAGAIN;
211249011e0cSOleksij Rempel 
211349011e0cSOleksij Rempel 	ret = ethnl_cable_test_result(phydev, ethtool_pair[pair],
211421b688daSDivya Koppera 				      ksz886x_cable_test_result_trans(val, KSZ8081_LMD_STAT_MASK));
211549011e0cSOleksij Rempel 	if (ret)
211649011e0cSOleksij Rempel 		return ret;
211749011e0cSOleksij Rempel 
211821b688daSDivya Koppera 	if (!ksz886x_cable_test_fault_length_valid(val, KSZ8081_LMD_STAT_MASK))
211949011e0cSOleksij Rempel 		return 0;
212049011e0cSOleksij Rempel 
212121b688daSDivya Koppera 	fault_length = ksz886x_cable_test_fault_length(phydev, val, KSZ8081_LMD_DELTA_TIME_MASK);
212221b688daSDivya Koppera 
212321b688daSDivya Koppera 	return ethnl_cable_test_fault_length(phydev, ethtool_pair[pair], fault_length);
212449011e0cSOleksij Rempel }
212549011e0cSOleksij Rempel 
212649011e0cSOleksij Rempel static int ksz886x_cable_test_get_status(struct phy_device *phydev,
212749011e0cSOleksij Rempel 					 bool *finished)
212849011e0cSOleksij Rempel {
212921b688daSDivya Koppera 	const struct kszphy_type *type = phydev->drv->driver_data;
213021b688daSDivya Koppera 	unsigned long pair_mask = type->pair_mask;
213149011e0cSOleksij Rempel 	int retries = 20;
2132d50ede4fSDivya Koppera 	int ret = 0;
2133d50ede4fSDivya Koppera 	int pair;
213449011e0cSOleksij Rempel 
213549011e0cSOleksij Rempel 	*finished = false;
213649011e0cSOleksij Rempel 
213749011e0cSOleksij Rempel 	/* Try harder if link partner is active */
213849011e0cSOleksij Rempel 	while (pair_mask && retries--) {
213949011e0cSOleksij Rempel 		for_each_set_bit(pair, &pair_mask, 4) {
214021b688daSDivya Koppera 			if (type->cable_diag_reg == LAN8814_CABLE_DIAG)
214121b688daSDivya Koppera 				ret = lan8814_cable_test_one_pair(phydev, pair);
214221b688daSDivya Koppera 			else
214349011e0cSOleksij Rempel 				ret = ksz886x_cable_test_one_pair(phydev, pair);
214449011e0cSOleksij Rempel 			if (ret == -EAGAIN)
214549011e0cSOleksij Rempel 				continue;
214649011e0cSOleksij Rempel 			if (ret < 0)
214749011e0cSOleksij Rempel 				return ret;
214849011e0cSOleksij Rempel 			clear_bit(pair, &pair_mask);
214949011e0cSOleksij Rempel 		}
215049011e0cSOleksij Rempel 		/* If link partner is in autonegotiation mode it will send 2ms
215149011e0cSOleksij Rempel 		 * of FLPs with at least 6ms of silence.
215249011e0cSOleksij Rempel 		 * Add 2ms sleep to have better chances to hit this silence.
215349011e0cSOleksij Rempel 		 */
215449011e0cSOleksij Rempel 		if (pair_mask)
215549011e0cSOleksij Rempel 			msleep(2);
215649011e0cSOleksij Rempel 	}
215749011e0cSOleksij Rempel 
215849011e0cSOleksij Rempel 	*finished = true;
215949011e0cSOleksij Rempel 
216049011e0cSOleksij Rempel 	return ret;
216149011e0cSOleksij Rempel }
216249011e0cSOleksij Rempel 
21637c2dcfa2SHoratiu Vultur #define LAN_EXT_PAGE_ACCESS_CONTROL			0x16
21647c2dcfa2SHoratiu Vultur #define LAN_EXT_PAGE_ACCESS_ADDRESS_DATA		0x17
21657c2dcfa2SHoratiu Vultur #define LAN_EXT_PAGE_ACCESS_CTRL_EP_FUNC		0x4000
21667c2dcfa2SHoratiu Vultur 
21677467d716SHoratiu Vultur #define LAN8814_QSGMII_SOFT_RESET			0x43
21687467d716SHoratiu Vultur #define LAN8814_QSGMII_SOFT_RESET_BIT			BIT(0)
21697467d716SHoratiu Vultur #define LAN8814_QSGMII_PCS1G_ANEG_CONFIG		0x13
21707467d716SHoratiu Vultur #define LAN8814_QSGMII_PCS1G_ANEG_CONFIG_ANEG_ENA	BIT(3)
21717467d716SHoratiu Vultur #define LAN8814_ALIGN_SWAP				0x4a
21727467d716SHoratiu Vultur #define LAN8814_ALIGN_TX_A_B_SWAP			0x1
21737467d716SHoratiu Vultur #define LAN8814_ALIGN_TX_A_B_SWAP_MASK			GENMASK(2, 0)
21747467d716SHoratiu Vultur 
21757c2dcfa2SHoratiu Vultur #define LAN8804_ALIGN_SWAP				0x4a
21767c2dcfa2SHoratiu Vultur #define LAN8804_ALIGN_TX_A_B_SWAP			0x1
21777c2dcfa2SHoratiu Vultur #define LAN8804_ALIGN_TX_A_B_SWAP_MASK			GENMASK(2, 0)
21787c2dcfa2SHoratiu Vultur #define LAN8814_CLOCK_MANAGEMENT			0xd
21797c2dcfa2SHoratiu Vultur #define LAN8814_LINK_QUALITY				0x8e
21807c2dcfa2SHoratiu Vultur 
21817c2dcfa2SHoratiu Vultur static int lanphy_read_page_reg(struct phy_device *phydev, int page, u32 addr)
21827c2dcfa2SHoratiu Vultur {
218312a4d677SWan Jiabing 	int data;
21847c2dcfa2SHoratiu Vultur 
21854488f6b6SDivya Koppera 	phy_lock_mdio_bus(phydev);
21864488f6b6SDivya Koppera 	__phy_write(phydev, LAN_EXT_PAGE_ACCESS_CONTROL, page);
21874488f6b6SDivya Koppera 	__phy_write(phydev, LAN_EXT_PAGE_ACCESS_ADDRESS_DATA, addr);
21884488f6b6SDivya Koppera 	__phy_write(phydev, LAN_EXT_PAGE_ACCESS_CONTROL,
21897c2dcfa2SHoratiu Vultur 		    (page | LAN_EXT_PAGE_ACCESS_CTRL_EP_FUNC));
21904488f6b6SDivya Koppera 	data = __phy_read(phydev, LAN_EXT_PAGE_ACCESS_ADDRESS_DATA);
21914488f6b6SDivya Koppera 	phy_unlock_mdio_bus(phydev);
21927c2dcfa2SHoratiu Vultur 
21937c2dcfa2SHoratiu Vultur 	return data;
21947c2dcfa2SHoratiu Vultur }
21957c2dcfa2SHoratiu Vultur 
21967c2dcfa2SHoratiu Vultur static int lanphy_write_page_reg(struct phy_device *phydev, int page, u16 addr,
21977c2dcfa2SHoratiu Vultur 				 u16 val)
21987c2dcfa2SHoratiu Vultur {
21994488f6b6SDivya Koppera 	phy_lock_mdio_bus(phydev);
22004488f6b6SDivya Koppera 	__phy_write(phydev, LAN_EXT_PAGE_ACCESS_CONTROL, page);
22014488f6b6SDivya Koppera 	__phy_write(phydev, LAN_EXT_PAGE_ACCESS_ADDRESS_DATA, addr);
22024488f6b6SDivya Koppera 	__phy_write(phydev, LAN_EXT_PAGE_ACCESS_CONTROL,
22034488f6b6SDivya Koppera 		    page | LAN_EXT_PAGE_ACCESS_CTRL_EP_FUNC);
22047c2dcfa2SHoratiu Vultur 
22054488f6b6SDivya Koppera 	val = __phy_write(phydev, LAN_EXT_PAGE_ACCESS_ADDRESS_DATA, val);
22064488f6b6SDivya Koppera 	if (val != 0)
22077c2dcfa2SHoratiu Vultur 		phydev_err(phydev, "Error: phy_write has returned error %d\n",
22087c2dcfa2SHoratiu Vultur 			   val);
22094488f6b6SDivya Koppera 	phy_unlock_mdio_bus(phydev);
22107c2dcfa2SHoratiu Vultur 	return val;
22117c2dcfa2SHoratiu Vultur }
22127c2dcfa2SHoratiu Vultur 
2213ece19502SDivya Koppera static int lan8814_config_ts_intr(struct phy_device *phydev, bool enable)
22147467d716SHoratiu Vultur {
2215ece19502SDivya Koppera 	u16 val = 0;
22167467d716SHoratiu Vultur 
2217ece19502SDivya Koppera 	if (enable)
2218ece19502SDivya Koppera 		val = PTP_TSU_INT_EN_PTP_TX_TS_EN_ |
2219ece19502SDivya Koppera 		      PTP_TSU_INT_EN_PTP_TX_TS_OVRFL_EN_ |
2220ece19502SDivya Koppera 		      PTP_TSU_INT_EN_PTP_RX_TS_EN_ |
2221ece19502SDivya Koppera 		      PTP_TSU_INT_EN_PTP_RX_TS_OVRFL_EN_;
22227467d716SHoratiu Vultur 
2223ece19502SDivya Koppera 	return lanphy_write_page_reg(phydev, 5, PTP_TSU_INT_EN, val);
2224ece19502SDivya Koppera }
22257467d716SHoratiu Vultur 
2226ece19502SDivya Koppera static void lan8814_ptp_rx_ts_get(struct phy_device *phydev,
2227ece19502SDivya Koppera 				  u32 *seconds, u32 *nano_seconds, u16 *seq_id)
2228ece19502SDivya Koppera {
2229ece19502SDivya Koppera 	*seconds = lanphy_read_page_reg(phydev, 5, PTP_RX_INGRESS_SEC_HI);
2230ece19502SDivya Koppera 	*seconds = (*seconds << 16) |
2231ece19502SDivya Koppera 		   lanphy_read_page_reg(phydev, 5, PTP_RX_INGRESS_SEC_LO);
2232ece19502SDivya Koppera 
2233ece19502SDivya Koppera 	*nano_seconds = lanphy_read_page_reg(phydev, 5, PTP_RX_INGRESS_NS_HI);
2234ece19502SDivya Koppera 	*nano_seconds = ((*nano_seconds & 0x3fff) << 16) |
2235ece19502SDivya Koppera 			lanphy_read_page_reg(phydev, 5, PTP_RX_INGRESS_NS_LO);
2236ece19502SDivya Koppera 
2237ece19502SDivya Koppera 	*seq_id = lanphy_read_page_reg(phydev, 5, PTP_RX_MSG_HEADER2);
2238ece19502SDivya Koppera }
2239ece19502SDivya Koppera 
2240ece19502SDivya Koppera static void lan8814_ptp_tx_ts_get(struct phy_device *phydev,
2241ece19502SDivya Koppera 				  u32 *seconds, u32 *nano_seconds, u16 *seq_id)
2242ece19502SDivya Koppera {
2243ece19502SDivya Koppera 	*seconds = lanphy_read_page_reg(phydev, 5, PTP_TX_EGRESS_SEC_HI);
2244ece19502SDivya Koppera 	*seconds = *seconds << 16 |
2245ece19502SDivya Koppera 		   lanphy_read_page_reg(phydev, 5, PTP_TX_EGRESS_SEC_LO);
2246ece19502SDivya Koppera 
2247ece19502SDivya Koppera 	*nano_seconds = lanphy_read_page_reg(phydev, 5, PTP_TX_EGRESS_NS_HI);
2248ece19502SDivya Koppera 	*nano_seconds = ((*nano_seconds & 0x3fff) << 16) |
2249ece19502SDivya Koppera 			lanphy_read_page_reg(phydev, 5, PTP_TX_EGRESS_NS_LO);
2250ece19502SDivya Koppera 
2251ece19502SDivya Koppera 	*seq_id = lanphy_read_page_reg(phydev, 5, PTP_TX_MSG_HEADER2);
2252ece19502SDivya Koppera }
2253ece19502SDivya Koppera 
2254ece19502SDivya Koppera static int lan8814_ts_info(struct mii_timestamper *mii_ts, struct ethtool_ts_info *info)
2255ece19502SDivya Koppera {
2256ece19502SDivya Koppera 	struct kszphy_ptp_priv *ptp_priv = container_of(mii_ts, struct kszphy_ptp_priv, mii_ts);
2257ece19502SDivya Koppera 	struct phy_device *phydev = ptp_priv->phydev;
2258ece19502SDivya Koppera 	struct lan8814_shared_priv *shared = phydev->shared->priv;
2259ece19502SDivya Koppera 
2260ece19502SDivya Koppera 	info->so_timestamping = SOF_TIMESTAMPING_TX_HARDWARE |
2261ece19502SDivya Koppera 				SOF_TIMESTAMPING_RX_HARDWARE |
2262ece19502SDivya Koppera 				SOF_TIMESTAMPING_RAW_HARDWARE;
2263ece19502SDivya Koppera 
2264ece19502SDivya Koppera 	info->phc_index = ptp_clock_index(shared->ptp_clock);
2265ece19502SDivya Koppera 
2266ece19502SDivya Koppera 	info->tx_types =
2267ece19502SDivya Koppera 		(1 << HWTSTAMP_TX_OFF) |
2268ece19502SDivya Koppera 		(1 << HWTSTAMP_TX_ON) |
2269ece19502SDivya Koppera 		(1 << HWTSTAMP_TX_ONESTEP_SYNC);
2270ece19502SDivya Koppera 
2271ece19502SDivya Koppera 	info->rx_filters =
2272ece19502SDivya Koppera 		(1 << HWTSTAMP_FILTER_NONE) |
2273ece19502SDivya Koppera 		(1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT) |
2274ece19502SDivya Koppera 		(1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT) |
2275ece19502SDivya Koppera 		(1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
2276ece19502SDivya Koppera 		(1 << HWTSTAMP_FILTER_PTP_V2_EVENT);
22777467d716SHoratiu Vultur 
22787467d716SHoratiu Vultur 	return 0;
22797467d716SHoratiu Vultur }
22807467d716SHoratiu Vultur 
2281ece19502SDivya Koppera static void lan8814_flush_fifo(struct phy_device *phydev, bool egress)
2282ece19502SDivya Koppera {
2283ece19502SDivya Koppera 	int i;
2284ece19502SDivya Koppera 
2285ece19502SDivya Koppera 	for (i = 0; i < FIFO_SIZE; ++i)
2286ece19502SDivya Koppera 		lanphy_read_page_reg(phydev, 5,
2287ece19502SDivya Koppera 				     egress ? PTP_TX_MSG_HEADER2 : PTP_RX_MSG_HEADER2);
2288ece19502SDivya Koppera 
2289ece19502SDivya Koppera 	/* Read to clear overflow status bit */
2290ece19502SDivya Koppera 	lanphy_read_page_reg(phydev, 5, PTP_TSU_INT_STS);
2291ece19502SDivya Koppera }
2292ece19502SDivya Koppera 
2293ece19502SDivya Koppera static int lan8814_hwtstamp(struct mii_timestamper *mii_ts, struct ifreq *ifr)
2294ece19502SDivya Koppera {
2295ece19502SDivya Koppera 	struct kszphy_ptp_priv *ptp_priv =
2296ece19502SDivya Koppera 			  container_of(mii_ts, struct kszphy_ptp_priv, mii_ts);
2297ece19502SDivya Koppera 	struct phy_device *phydev = ptp_priv->phydev;
2298ece19502SDivya Koppera 	struct lan8814_shared_priv *shared = phydev->shared->priv;
2299ece19502SDivya Koppera 	struct lan8814_ptp_rx_ts *rx_ts, *tmp;
2300ece19502SDivya Koppera 	struct hwtstamp_config config;
2301ece19502SDivya Koppera 	int txcfg = 0, rxcfg = 0;
2302ece19502SDivya Koppera 	int pkt_ts_enable;
2303ece19502SDivya Koppera 
2304ece19502SDivya Koppera 	if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
2305ece19502SDivya Koppera 		return -EFAULT;
2306ece19502SDivya Koppera 
2307ece19502SDivya Koppera 	ptp_priv->hwts_tx_type = config.tx_type;
2308ece19502SDivya Koppera 	ptp_priv->rx_filter = config.rx_filter;
2309ece19502SDivya Koppera 
2310ece19502SDivya Koppera 	switch (config.rx_filter) {
2311ece19502SDivya Koppera 	case HWTSTAMP_FILTER_NONE:
2312ece19502SDivya Koppera 		ptp_priv->layer = 0;
2313ece19502SDivya Koppera 		ptp_priv->version = 0;
2314ece19502SDivya Koppera 		break;
2315ece19502SDivya Koppera 	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
2316ece19502SDivya Koppera 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
2317ece19502SDivya Koppera 	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
2318ece19502SDivya Koppera 		ptp_priv->layer = PTP_CLASS_L4;
2319ece19502SDivya Koppera 		ptp_priv->version = PTP_CLASS_V2;
2320ece19502SDivya Koppera 		break;
2321ece19502SDivya Koppera 	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
2322ece19502SDivya Koppera 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
2323ece19502SDivya Koppera 	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
2324ece19502SDivya Koppera 		ptp_priv->layer = PTP_CLASS_L2;
2325ece19502SDivya Koppera 		ptp_priv->version = PTP_CLASS_V2;
2326ece19502SDivya Koppera 		break;
2327ece19502SDivya Koppera 	case HWTSTAMP_FILTER_PTP_V2_EVENT:
2328ece19502SDivya Koppera 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
2329ece19502SDivya Koppera 	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
2330ece19502SDivya Koppera 		ptp_priv->layer = PTP_CLASS_L4 | PTP_CLASS_L2;
2331ece19502SDivya Koppera 		ptp_priv->version = PTP_CLASS_V2;
2332ece19502SDivya Koppera 		break;
2333ece19502SDivya Koppera 	default:
2334ece19502SDivya Koppera 		return -ERANGE;
2335ece19502SDivya Koppera 	}
2336ece19502SDivya Koppera 
2337ece19502SDivya Koppera 	if (ptp_priv->layer & PTP_CLASS_L2) {
2338ece19502SDivya Koppera 		rxcfg = PTP_RX_PARSE_CONFIG_LAYER2_EN_;
2339ece19502SDivya Koppera 		txcfg = PTP_TX_PARSE_CONFIG_LAYER2_EN_;
2340ece19502SDivya Koppera 	} else if (ptp_priv->layer & PTP_CLASS_L4) {
2341ece19502SDivya Koppera 		rxcfg |= PTP_RX_PARSE_CONFIG_IPV4_EN_ | PTP_RX_PARSE_CONFIG_IPV6_EN_;
2342ece19502SDivya Koppera 		txcfg |= PTP_TX_PARSE_CONFIG_IPV4_EN_ | PTP_TX_PARSE_CONFIG_IPV6_EN_;
2343ece19502SDivya Koppera 	}
2344ece19502SDivya Koppera 	lanphy_write_page_reg(ptp_priv->phydev, 5, PTP_RX_PARSE_CONFIG, rxcfg);
2345ece19502SDivya Koppera 	lanphy_write_page_reg(ptp_priv->phydev, 5, PTP_TX_PARSE_CONFIG, txcfg);
2346ece19502SDivya Koppera 
2347ece19502SDivya Koppera 	pkt_ts_enable = PTP_TIMESTAMP_EN_SYNC_ | PTP_TIMESTAMP_EN_DREQ_ |
2348ece19502SDivya Koppera 			PTP_TIMESTAMP_EN_PDREQ_ | PTP_TIMESTAMP_EN_PDRES_;
2349ece19502SDivya Koppera 	lanphy_write_page_reg(ptp_priv->phydev, 5, PTP_RX_TIMESTAMP_EN, pkt_ts_enable);
2350ece19502SDivya Koppera 	lanphy_write_page_reg(ptp_priv->phydev, 5, PTP_TX_TIMESTAMP_EN, pkt_ts_enable);
2351ece19502SDivya Koppera 
2352ece19502SDivya Koppera 	if (ptp_priv->hwts_tx_type == HWTSTAMP_TX_ONESTEP_SYNC)
2353ece19502SDivya Koppera 		lanphy_write_page_reg(ptp_priv->phydev, 5, PTP_TX_MOD,
2354ece19502SDivya Koppera 				      PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_);
2355ece19502SDivya Koppera 
2356ece19502SDivya Koppera 	if (config.rx_filter != HWTSTAMP_FILTER_NONE)
2357ece19502SDivya Koppera 		lan8814_config_ts_intr(ptp_priv->phydev, true);
2358ece19502SDivya Koppera 	else
2359ece19502SDivya Koppera 		lan8814_config_ts_intr(ptp_priv->phydev, false);
2360ece19502SDivya Koppera 
2361ece19502SDivya Koppera 	mutex_lock(&shared->shared_lock);
2362ece19502SDivya Koppera 	if (config.rx_filter != HWTSTAMP_FILTER_NONE)
2363ece19502SDivya Koppera 		shared->ref++;
2364ece19502SDivya Koppera 	else
2365ece19502SDivya Koppera 		shared->ref--;
2366ece19502SDivya Koppera 
2367ece19502SDivya Koppera 	if (shared->ref)
2368ece19502SDivya Koppera 		lanphy_write_page_reg(ptp_priv->phydev, 4, PTP_CMD_CTL,
2369ece19502SDivya Koppera 				      PTP_CMD_CTL_PTP_ENABLE_);
2370ece19502SDivya Koppera 	else
2371ece19502SDivya Koppera 		lanphy_write_page_reg(ptp_priv->phydev, 4, PTP_CMD_CTL,
2372ece19502SDivya Koppera 				      PTP_CMD_CTL_PTP_DISABLE_);
2373ece19502SDivya Koppera 	mutex_unlock(&shared->shared_lock);
2374ece19502SDivya Koppera 
2375ece19502SDivya Koppera 	/* In case of multiple starts and stops, these needs to be cleared */
2376ece19502SDivya Koppera 	list_for_each_entry_safe(rx_ts, tmp, &ptp_priv->rx_ts_list, list) {
2377ece19502SDivya Koppera 		list_del(&rx_ts->list);
2378ece19502SDivya Koppera 		kfree(rx_ts);
2379ece19502SDivya Koppera 	}
2380ece19502SDivya Koppera 	skb_queue_purge(&ptp_priv->rx_queue);
2381ece19502SDivya Koppera 	skb_queue_purge(&ptp_priv->tx_queue);
2382ece19502SDivya Koppera 
2383ece19502SDivya Koppera 	lan8814_flush_fifo(ptp_priv->phydev, false);
2384ece19502SDivya Koppera 	lan8814_flush_fifo(ptp_priv->phydev, true);
2385ece19502SDivya Koppera 
2386ece19502SDivya Koppera 	return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ? -EFAULT : 0;
2387ece19502SDivya Koppera }
2388ece19502SDivya Koppera 
2389ece19502SDivya Koppera static void lan8814_txtstamp(struct mii_timestamper *mii_ts,
2390ece19502SDivya Koppera 			     struct sk_buff *skb, int type)
2391ece19502SDivya Koppera {
2392ece19502SDivya Koppera 	struct kszphy_ptp_priv *ptp_priv = container_of(mii_ts, struct kszphy_ptp_priv, mii_ts);
2393ece19502SDivya Koppera 
2394ece19502SDivya Koppera 	switch (ptp_priv->hwts_tx_type) {
2395ece19502SDivya Koppera 	case HWTSTAMP_TX_ONESTEP_SYNC:
23963914a9c0SKurt Kanzenbach 		if (ptp_msg_is_sync(skb, type)) {
2397ece19502SDivya Koppera 			kfree_skb(skb);
2398ece19502SDivya Koppera 			return;
2399ece19502SDivya Koppera 		}
2400ece19502SDivya Koppera 		fallthrough;
2401ece19502SDivya Koppera 	case HWTSTAMP_TX_ON:
2402ece19502SDivya Koppera 		skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
2403ece19502SDivya Koppera 		skb_queue_tail(&ptp_priv->tx_queue, skb);
2404ece19502SDivya Koppera 		break;
2405ece19502SDivya Koppera 	case HWTSTAMP_TX_OFF:
2406ece19502SDivya Koppera 	default:
2407ece19502SDivya Koppera 		kfree_skb(skb);
2408ece19502SDivya Koppera 		break;
2409ece19502SDivya Koppera 	}
2410ece19502SDivya Koppera }
2411ece19502SDivya Koppera 
2412ece19502SDivya Koppera static void lan8814_get_sig_rx(struct sk_buff *skb, u16 *sig)
2413ece19502SDivya Koppera {
2414ece19502SDivya Koppera 	struct ptp_header *ptp_header;
2415ece19502SDivya Koppera 	u32 type;
2416ece19502SDivya Koppera 
2417ece19502SDivya Koppera 	skb_push(skb, ETH_HLEN);
2418ece19502SDivya Koppera 	type = ptp_classify_raw(skb);
2419ece19502SDivya Koppera 	ptp_header = ptp_parse_header(skb, type);
2420ece19502SDivya Koppera 	skb_pull_inline(skb, ETH_HLEN);
2421ece19502SDivya Koppera 
2422ece19502SDivya Koppera 	*sig = (__force u16)(ntohs(ptp_header->sequence_id));
2423ece19502SDivya Koppera }
2424ece19502SDivya Koppera 
2425cafc3662SHoratiu Vultur static bool lan8814_match_rx_skb(struct kszphy_ptp_priv *ptp_priv,
2426ece19502SDivya Koppera 				 struct sk_buff *skb)
2427ece19502SDivya Koppera {
2428ece19502SDivya Koppera 	struct skb_shared_hwtstamps *shhwtstamps;
2429ece19502SDivya Koppera 	struct lan8814_ptp_rx_ts *rx_ts, *tmp;
2430ece19502SDivya Koppera 	unsigned long flags;
2431ece19502SDivya Koppera 	bool ret = false;
2432ece19502SDivya Koppera 	u16 skb_sig;
2433ece19502SDivya Koppera 
2434ece19502SDivya Koppera 	lan8814_get_sig_rx(skb, &skb_sig);
2435ece19502SDivya Koppera 
2436ece19502SDivya Koppera 	/* Iterate over all RX timestamps and match it with the received skbs */
2437ece19502SDivya Koppera 	spin_lock_irqsave(&ptp_priv->rx_ts_lock, flags);
2438ece19502SDivya Koppera 	list_for_each_entry_safe(rx_ts, tmp, &ptp_priv->rx_ts_list, list) {
2439ece19502SDivya Koppera 		/* Check if we found the signature we were looking for. */
2440ece19502SDivya Koppera 		if (memcmp(&skb_sig, &rx_ts->seq_id, sizeof(rx_ts->seq_id)))
2441ece19502SDivya Koppera 			continue;
2442ece19502SDivya Koppera 
2443ece19502SDivya Koppera 		shhwtstamps = skb_hwtstamps(skb);
2444ece19502SDivya Koppera 		memset(shhwtstamps, 0, sizeof(*shhwtstamps));
2445ece19502SDivya Koppera 		shhwtstamps->hwtstamp = ktime_set(rx_ts->seconds,
2446ece19502SDivya Koppera 						  rx_ts->nsec);
2447ece19502SDivya Koppera 		list_del(&rx_ts->list);
2448ece19502SDivya Koppera 		kfree(rx_ts);
2449ece19502SDivya Koppera 
2450ece19502SDivya Koppera 		ret = true;
2451ece19502SDivya Koppera 		break;
2452ece19502SDivya Koppera 	}
2453ece19502SDivya Koppera 	spin_unlock_irqrestore(&ptp_priv->rx_ts_lock, flags);
2454ece19502SDivya Koppera 
245567dbd6c0SSebastian Andrzej Siewior 	if (ret)
245667dbd6c0SSebastian Andrzej Siewior 		netif_rx(skb);
2457ece19502SDivya Koppera 	return ret;
2458ece19502SDivya Koppera }
2459ece19502SDivya Koppera 
2460ece19502SDivya Koppera static bool lan8814_rxtstamp(struct mii_timestamper *mii_ts, struct sk_buff *skb, int type)
2461ece19502SDivya Koppera {
2462ece19502SDivya Koppera 	struct kszphy_ptp_priv *ptp_priv =
2463ece19502SDivya Koppera 			container_of(mii_ts, struct kszphy_ptp_priv, mii_ts);
2464ece19502SDivya Koppera 
2465ece19502SDivya Koppera 	if (ptp_priv->rx_filter == HWTSTAMP_FILTER_NONE ||
2466ece19502SDivya Koppera 	    type == PTP_CLASS_NONE)
2467ece19502SDivya Koppera 		return false;
2468ece19502SDivya Koppera 
2469ece19502SDivya Koppera 	if ((type & ptp_priv->version) == 0 || (type & ptp_priv->layer) == 0)
2470ece19502SDivya Koppera 		return false;
2471ece19502SDivya Koppera 
2472ece19502SDivya Koppera 	/* If we failed to match then add it to the queue for when the timestamp
2473ece19502SDivya Koppera 	 * will come
2474ece19502SDivya Koppera 	 */
2475cafc3662SHoratiu Vultur 	if (!lan8814_match_rx_skb(ptp_priv, skb))
2476ece19502SDivya Koppera 		skb_queue_tail(&ptp_priv->rx_queue, skb);
2477ece19502SDivya Koppera 
2478ece19502SDivya Koppera 	return true;
2479ece19502SDivya Koppera }
2480ece19502SDivya Koppera 
2481ece19502SDivya Koppera static void lan8814_ptp_clock_set(struct phy_device *phydev,
2482ece19502SDivya Koppera 				  u32 seconds, u32 nano_seconds)
2483ece19502SDivya Koppera {
2484ece19502SDivya Koppera 	u32 sec_low, sec_high, nsec_low, nsec_high;
2485ece19502SDivya Koppera 
2486ece19502SDivya Koppera 	sec_low = seconds & 0xffff;
2487ece19502SDivya Koppera 	sec_high = (seconds >> 16) & 0xffff;
2488ece19502SDivya Koppera 	nsec_low = nano_seconds & 0xffff;
2489ece19502SDivya Koppera 	nsec_high = (nano_seconds >> 16) & 0x3fff;
2490ece19502SDivya Koppera 
2491ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 4, PTP_CLOCK_SET_SEC_LO, sec_low);
2492ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 4, PTP_CLOCK_SET_SEC_MID, sec_high);
2493ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 4, PTP_CLOCK_SET_NS_LO, nsec_low);
2494ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 4, PTP_CLOCK_SET_NS_HI, nsec_high);
2495ece19502SDivya Koppera 
2496ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 4, PTP_CMD_CTL, PTP_CMD_CTL_PTP_CLOCK_LOAD_);
2497ece19502SDivya Koppera }
2498ece19502SDivya Koppera 
2499ece19502SDivya Koppera static void lan8814_ptp_clock_get(struct phy_device *phydev,
2500ece19502SDivya Koppera 				  u32 *seconds, u32 *nano_seconds)
2501ece19502SDivya Koppera {
2502ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 4, PTP_CMD_CTL, PTP_CMD_CTL_PTP_CLOCK_READ_);
2503ece19502SDivya Koppera 
2504ece19502SDivya Koppera 	*seconds = lanphy_read_page_reg(phydev, 4, PTP_CLOCK_READ_SEC_MID);
2505ece19502SDivya Koppera 	*seconds = (*seconds << 16) |
2506ece19502SDivya Koppera 		   lanphy_read_page_reg(phydev, 4, PTP_CLOCK_READ_SEC_LO);
2507ece19502SDivya Koppera 
2508ece19502SDivya Koppera 	*nano_seconds = lanphy_read_page_reg(phydev, 4, PTP_CLOCK_READ_NS_HI);
2509ece19502SDivya Koppera 	*nano_seconds = ((*nano_seconds & 0x3fff) << 16) |
2510ece19502SDivya Koppera 			lanphy_read_page_reg(phydev, 4, PTP_CLOCK_READ_NS_LO);
2511ece19502SDivya Koppera }
2512ece19502SDivya Koppera 
2513ece19502SDivya Koppera static int lan8814_ptpci_gettime64(struct ptp_clock_info *ptpci,
2514ece19502SDivya Koppera 				   struct timespec64 *ts)
2515ece19502SDivya Koppera {
2516ece19502SDivya Koppera 	struct lan8814_shared_priv *shared = container_of(ptpci, struct lan8814_shared_priv,
2517ece19502SDivya Koppera 							  ptp_clock_info);
2518ece19502SDivya Koppera 	struct phy_device *phydev = shared->phydev;
2519ece19502SDivya Koppera 	u32 nano_seconds;
2520ece19502SDivya Koppera 	u32 seconds;
2521ece19502SDivya Koppera 
2522ece19502SDivya Koppera 	mutex_lock(&shared->shared_lock);
2523ece19502SDivya Koppera 	lan8814_ptp_clock_get(phydev, &seconds, &nano_seconds);
2524ece19502SDivya Koppera 	mutex_unlock(&shared->shared_lock);
2525ece19502SDivya Koppera 	ts->tv_sec = seconds;
2526ece19502SDivya Koppera 	ts->tv_nsec = nano_seconds;
2527ece19502SDivya Koppera 
2528ece19502SDivya Koppera 	return 0;
2529ece19502SDivya Koppera }
2530ece19502SDivya Koppera 
2531ece19502SDivya Koppera static int lan8814_ptpci_settime64(struct ptp_clock_info *ptpci,
2532ece19502SDivya Koppera 				   const struct timespec64 *ts)
2533ece19502SDivya Koppera {
2534ece19502SDivya Koppera 	struct lan8814_shared_priv *shared = container_of(ptpci, struct lan8814_shared_priv,
2535ece19502SDivya Koppera 							  ptp_clock_info);
2536ece19502SDivya Koppera 	struct phy_device *phydev = shared->phydev;
2537ece19502SDivya Koppera 
2538ece19502SDivya Koppera 	mutex_lock(&shared->shared_lock);
2539ece19502SDivya Koppera 	lan8814_ptp_clock_set(phydev, ts->tv_sec, ts->tv_nsec);
2540ece19502SDivya Koppera 	mutex_unlock(&shared->shared_lock);
2541ece19502SDivya Koppera 
2542ece19502SDivya Koppera 	return 0;
2543ece19502SDivya Koppera }
2544ece19502SDivya Koppera 
2545ece19502SDivya Koppera static void lan8814_ptp_clock_step(struct phy_device *phydev,
2546ece19502SDivya Koppera 				   s64 time_step_ns)
2547ece19502SDivya Koppera {
2548ece19502SDivya Koppera 	u32 nano_seconds_step;
2549ece19502SDivya Koppera 	u64 abs_time_step_ns;
2550ece19502SDivya Koppera 	u32 unsigned_seconds;
2551ece19502SDivya Koppera 	u32 nano_seconds;
2552ece19502SDivya Koppera 	u32 remainder;
2553ece19502SDivya Koppera 	s32 seconds;
2554ece19502SDivya Koppera 
2555ece19502SDivya Koppera 	if (time_step_ns >  15000000000LL) {
2556ece19502SDivya Koppera 		/* convert to clock set */
2557ece19502SDivya Koppera 		lan8814_ptp_clock_get(phydev, &unsigned_seconds, &nano_seconds);
2558ece19502SDivya Koppera 		unsigned_seconds += div_u64_rem(time_step_ns, 1000000000LL,
2559ece19502SDivya Koppera 						&remainder);
2560ece19502SDivya Koppera 		nano_seconds += remainder;
2561ece19502SDivya Koppera 		if (nano_seconds >= 1000000000) {
2562ece19502SDivya Koppera 			unsigned_seconds++;
2563ece19502SDivya Koppera 			nano_seconds -= 1000000000;
2564ece19502SDivya Koppera 		}
2565ece19502SDivya Koppera 		lan8814_ptp_clock_set(phydev, unsigned_seconds, nano_seconds);
2566ece19502SDivya Koppera 		return;
2567ece19502SDivya Koppera 	} else if (time_step_ns < -15000000000LL) {
2568ece19502SDivya Koppera 		/* convert to clock set */
2569ece19502SDivya Koppera 		time_step_ns = -time_step_ns;
2570ece19502SDivya Koppera 
2571ece19502SDivya Koppera 		lan8814_ptp_clock_get(phydev, &unsigned_seconds, &nano_seconds);
2572ece19502SDivya Koppera 		unsigned_seconds -= div_u64_rem(time_step_ns, 1000000000LL,
2573ece19502SDivya Koppera 						&remainder);
2574ece19502SDivya Koppera 		nano_seconds_step = remainder;
2575ece19502SDivya Koppera 		if (nano_seconds < nano_seconds_step) {
2576ece19502SDivya Koppera 			unsigned_seconds--;
2577ece19502SDivya Koppera 			nano_seconds += 1000000000;
2578ece19502SDivya Koppera 		}
2579ece19502SDivya Koppera 		nano_seconds -= nano_seconds_step;
2580ece19502SDivya Koppera 		lan8814_ptp_clock_set(phydev, unsigned_seconds,
2581ece19502SDivya Koppera 				      nano_seconds);
2582ece19502SDivya Koppera 		return;
2583ece19502SDivya Koppera 	}
2584ece19502SDivya Koppera 
2585ece19502SDivya Koppera 	/* do clock step */
2586ece19502SDivya Koppera 	if (time_step_ns >= 0) {
2587ece19502SDivya Koppera 		abs_time_step_ns = (u64)time_step_ns;
2588ece19502SDivya Koppera 		seconds = (s32)div_u64_rem(abs_time_step_ns, 1000000000,
2589ece19502SDivya Koppera 					   &remainder);
2590ece19502SDivya Koppera 		nano_seconds = remainder;
2591ece19502SDivya Koppera 	} else {
2592ece19502SDivya Koppera 		abs_time_step_ns = (u64)(-time_step_ns);
2593ece19502SDivya Koppera 		seconds = -((s32)div_u64_rem(abs_time_step_ns, 1000000000,
2594ece19502SDivya Koppera 			    &remainder));
2595ece19502SDivya Koppera 		nano_seconds = remainder;
2596ece19502SDivya Koppera 		if (nano_seconds > 0) {
2597ece19502SDivya Koppera 			/* subtracting nano seconds is not allowed
2598ece19502SDivya Koppera 			 * convert to subtracting from seconds,
2599ece19502SDivya Koppera 			 * and adding to nanoseconds
2600ece19502SDivya Koppera 			 */
2601ece19502SDivya Koppera 			seconds--;
2602ece19502SDivya Koppera 			nano_seconds = (1000000000 - nano_seconds);
2603ece19502SDivya Koppera 		}
2604ece19502SDivya Koppera 	}
2605ece19502SDivya Koppera 
2606ece19502SDivya Koppera 	if (nano_seconds > 0) {
2607ece19502SDivya Koppera 		/* add 8 ns to cover the likely normal increment */
2608ece19502SDivya Koppera 		nano_seconds += 8;
2609ece19502SDivya Koppera 	}
2610ece19502SDivya Koppera 
2611ece19502SDivya Koppera 	if (nano_seconds >= 1000000000) {
2612ece19502SDivya Koppera 		/* carry into seconds */
2613ece19502SDivya Koppera 		seconds++;
2614ece19502SDivya Koppera 		nano_seconds -= 1000000000;
2615ece19502SDivya Koppera 	}
2616ece19502SDivya Koppera 
2617ece19502SDivya Koppera 	while (seconds) {
2618ece19502SDivya Koppera 		if (seconds > 0) {
2619ece19502SDivya Koppera 			u32 adjustment_value = (u32)seconds;
2620ece19502SDivya Koppera 			u16 adjustment_value_lo, adjustment_value_hi;
2621ece19502SDivya Koppera 
2622ece19502SDivya Koppera 			if (adjustment_value > 0xF)
2623ece19502SDivya Koppera 				adjustment_value = 0xF;
2624ece19502SDivya Koppera 
2625ece19502SDivya Koppera 			adjustment_value_lo = adjustment_value & 0xffff;
2626ece19502SDivya Koppera 			adjustment_value_hi = (adjustment_value >> 16) & 0x3fff;
2627ece19502SDivya Koppera 
2628ece19502SDivya Koppera 			lanphy_write_page_reg(phydev, 4, PTP_LTC_STEP_ADJ_LO,
2629ece19502SDivya Koppera 					      adjustment_value_lo);
2630ece19502SDivya Koppera 			lanphy_write_page_reg(phydev, 4, PTP_LTC_STEP_ADJ_HI,
2631ece19502SDivya Koppera 					      PTP_LTC_STEP_ADJ_DIR_ |
2632ece19502SDivya Koppera 					      adjustment_value_hi);
2633ece19502SDivya Koppera 			seconds -= ((s32)adjustment_value);
2634ece19502SDivya Koppera 		} else {
2635ece19502SDivya Koppera 			u32 adjustment_value = (u32)(-seconds);
2636ece19502SDivya Koppera 			u16 adjustment_value_lo, adjustment_value_hi;
2637ece19502SDivya Koppera 
2638ece19502SDivya Koppera 			if (adjustment_value > 0xF)
2639ece19502SDivya Koppera 				adjustment_value = 0xF;
2640ece19502SDivya Koppera 
2641ece19502SDivya Koppera 			adjustment_value_lo = adjustment_value & 0xffff;
2642ece19502SDivya Koppera 			adjustment_value_hi = (adjustment_value >> 16) & 0x3fff;
2643ece19502SDivya Koppera 
2644ece19502SDivya Koppera 			lanphy_write_page_reg(phydev, 4, PTP_LTC_STEP_ADJ_LO,
2645ece19502SDivya Koppera 					      adjustment_value_lo);
2646ece19502SDivya Koppera 			lanphy_write_page_reg(phydev, 4, PTP_LTC_STEP_ADJ_HI,
2647ece19502SDivya Koppera 					      adjustment_value_hi);
2648ece19502SDivya Koppera 			seconds += ((s32)adjustment_value);
2649ece19502SDivya Koppera 		}
2650ece19502SDivya Koppera 		lanphy_write_page_reg(phydev, 4, PTP_CMD_CTL,
2651ece19502SDivya Koppera 				      PTP_CMD_CTL_PTP_LTC_STEP_SEC_);
2652ece19502SDivya Koppera 	}
2653ece19502SDivya Koppera 	if (nano_seconds) {
2654ece19502SDivya Koppera 		u16 nano_seconds_lo;
2655ece19502SDivya Koppera 		u16 nano_seconds_hi;
2656ece19502SDivya Koppera 
2657ece19502SDivya Koppera 		nano_seconds_lo = nano_seconds & 0xffff;
2658ece19502SDivya Koppera 		nano_seconds_hi = (nano_seconds >> 16) & 0x3fff;
2659ece19502SDivya Koppera 
2660ece19502SDivya Koppera 		lanphy_write_page_reg(phydev, 4, PTP_LTC_STEP_ADJ_LO,
2661ece19502SDivya Koppera 				      nano_seconds_lo);
2662ece19502SDivya Koppera 		lanphy_write_page_reg(phydev, 4, PTP_LTC_STEP_ADJ_HI,
2663ece19502SDivya Koppera 				      PTP_LTC_STEP_ADJ_DIR_ |
2664ece19502SDivya Koppera 				      nano_seconds_hi);
2665ece19502SDivya Koppera 		lanphy_write_page_reg(phydev, 4, PTP_CMD_CTL,
2666ece19502SDivya Koppera 				      PTP_CMD_CTL_PTP_LTC_STEP_NSEC_);
2667ece19502SDivya Koppera 	}
2668ece19502SDivya Koppera }
2669ece19502SDivya Koppera 
2670ece19502SDivya Koppera static int lan8814_ptpci_adjtime(struct ptp_clock_info *ptpci, s64 delta)
2671ece19502SDivya Koppera {
2672ece19502SDivya Koppera 	struct lan8814_shared_priv *shared = container_of(ptpci, struct lan8814_shared_priv,
2673ece19502SDivya Koppera 							  ptp_clock_info);
2674ece19502SDivya Koppera 	struct phy_device *phydev = shared->phydev;
2675ece19502SDivya Koppera 
2676ece19502SDivya Koppera 	mutex_lock(&shared->shared_lock);
2677ece19502SDivya Koppera 	lan8814_ptp_clock_step(phydev, delta);
2678ece19502SDivya Koppera 	mutex_unlock(&shared->shared_lock);
2679ece19502SDivya Koppera 
2680ece19502SDivya Koppera 	return 0;
2681ece19502SDivya Koppera }
2682ece19502SDivya Koppera 
2683ece19502SDivya Koppera static int lan8814_ptpci_adjfine(struct ptp_clock_info *ptpci, long scaled_ppm)
2684ece19502SDivya Koppera {
2685ece19502SDivya Koppera 	struct lan8814_shared_priv *shared = container_of(ptpci, struct lan8814_shared_priv,
2686ece19502SDivya Koppera 							  ptp_clock_info);
2687ece19502SDivya Koppera 	struct phy_device *phydev = shared->phydev;
2688ece19502SDivya Koppera 	u16 kszphy_rate_adj_lo, kszphy_rate_adj_hi;
2689ece19502SDivya Koppera 	bool positive = true;
2690ece19502SDivya Koppera 	u32 kszphy_rate_adj;
2691ece19502SDivya Koppera 
2692ece19502SDivya Koppera 	if (scaled_ppm < 0) {
2693ece19502SDivya Koppera 		scaled_ppm = -scaled_ppm;
2694ece19502SDivya Koppera 		positive = false;
2695ece19502SDivya Koppera 	}
2696ece19502SDivya Koppera 
2697ece19502SDivya Koppera 	kszphy_rate_adj = LAN8814_1PPM_FORMAT * (scaled_ppm >> 16);
2698ece19502SDivya Koppera 	kszphy_rate_adj += (LAN8814_1PPM_FORMAT * (0xffff & scaled_ppm)) >> 16;
2699ece19502SDivya Koppera 
2700ece19502SDivya Koppera 	kszphy_rate_adj_lo = kszphy_rate_adj & 0xffff;
2701ece19502SDivya Koppera 	kszphy_rate_adj_hi = (kszphy_rate_adj >> 16) & 0x3fff;
2702ece19502SDivya Koppera 
2703ece19502SDivya Koppera 	if (positive)
2704ece19502SDivya Koppera 		kszphy_rate_adj_hi |= PTP_CLOCK_RATE_ADJ_DIR_;
2705ece19502SDivya Koppera 
2706ece19502SDivya Koppera 	mutex_lock(&shared->shared_lock);
2707ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 4, PTP_CLOCK_RATE_ADJ_HI, kszphy_rate_adj_hi);
2708ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 4, PTP_CLOCK_RATE_ADJ_LO, kszphy_rate_adj_lo);
2709ece19502SDivya Koppera 	mutex_unlock(&shared->shared_lock);
2710ece19502SDivya Koppera 
2711ece19502SDivya Koppera 	return 0;
2712ece19502SDivya Koppera }
2713ece19502SDivya Koppera 
2714ece19502SDivya Koppera static void lan8814_get_sig_tx(struct sk_buff *skb, u16 *sig)
2715ece19502SDivya Koppera {
2716ece19502SDivya Koppera 	struct ptp_header *ptp_header;
2717ece19502SDivya Koppera 	u32 type;
2718ece19502SDivya Koppera 
2719ece19502SDivya Koppera 	type = ptp_classify_raw(skb);
2720ece19502SDivya Koppera 	ptp_header = ptp_parse_header(skb, type);
2721ece19502SDivya Koppera 
2722ece19502SDivya Koppera 	*sig = (__force u16)(ntohs(ptp_header->sequence_id));
2723ece19502SDivya Koppera }
2724ece19502SDivya Koppera 
2725cafc3662SHoratiu Vultur static void lan8814_match_tx_skb(struct kszphy_ptp_priv *ptp_priv,
2726cafc3662SHoratiu Vultur 				 u32 seconds, u32 nsec, u16 seq_id)
2727ece19502SDivya Koppera {
2728ece19502SDivya Koppera 	struct skb_shared_hwtstamps shhwtstamps;
2729ece19502SDivya Koppera 	struct sk_buff *skb, *skb_tmp;
2730ece19502SDivya Koppera 	unsigned long flags;
2731ece19502SDivya Koppera 	bool ret = false;
2732ece19502SDivya Koppera 	u16 skb_sig;
2733ece19502SDivya Koppera 
2734ece19502SDivya Koppera 	spin_lock_irqsave(&ptp_priv->tx_queue.lock, flags);
2735ece19502SDivya Koppera 	skb_queue_walk_safe(&ptp_priv->tx_queue, skb, skb_tmp) {
2736ece19502SDivya Koppera 		lan8814_get_sig_tx(skb, &skb_sig);
2737ece19502SDivya Koppera 
2738ece19502SDivya Koppera 		if (memcmp(&skb_sig, &seq_id, sizeof(seq_id)))
2739ece19502SDivya Koppera 			continue;
2740ece19502SDivya Koppera 
2741ece19502SDivya Koppera 		__skb_unlink(skb, &ptp_priv->tx_queue);
2742ece19502SDivya Koppera 		ret = true;
2743ece19502SDivya Koppera 		break;
2744ece19502SDivya Koppera 	}
2745ece19502SDivya Koppera 	spin_unlock_irqrestore(&ptp_priv->tx_queue.lock, flags);
2746ece19502SDivya Koppera 
2747ece19502SDivya Koppera 	if (ret) {
2748ece19502SDivya Koppera 		memset(&shhwtstamps, 0, sizeof(shhwtstamps));
2749ece19502SDivya Koppera 		shhwtstamps.hwtstamp = ktime_set(seconds, nsec);
2750ece19502SDivya Koppera 		skb_complete_tx_timestamp(skb, &shhwtstamps);
2751ece19502SDivya Koppera 	}
2752ece19502SDivya Koppera }
2753ece19502SDivya Koppera 
2754cafc3662SHoratiu Vultur static void lan8814_dequeue_tx_skb(struct kszphy_ptp_priv *ptp_priv)
2755cafc3662SHoratiu Vultur {
2756cafc3662SHoratiu Vultur 	struct phy_device *phydev = ptp_priv->phydev;
2757cafc3662SHoratiu Vultur 	u32 seconds, nsec;
2758cafc3662SHoratiu Vultur 	u16 seq_id;
2759cafc3662SHoratiu Vultur 
2760cafc3662SHoratiu Vultur 	lan8814_ptp_tx_ts_get(phydev, &seconds, &nsec, &seq_id);
2761cafc3662SHoratiu Vultur 	lan8814_match_tx_skb(ptp_priv, seconds, nsec, seq_id);
2762cafc3662SHoratiu Vultur }
2763cafc3662SHoratiu Vultur 
2764ece19502SDivya Koppera static void lan8814_get_tx_ts(struct kszphy_ptp_priv *ptp_priv)
2765ece19502SDivya Koppera {
2766ece19502SDivya Koppera 	struct phy_device *phydev = ptp_priv->phydev;
2767ece19502SDivya Koppera 	u32 reg;
2768ece19502SDivya Koppera 
2769ece19502SDivya Koppera 	do {
2770ece19502SDivya Koppera 		lan8814_dequeue_tx_skb(ptp_priv);
2771ece19502SDivya Koppera 
2772ece19502SDivya Koppera 		/* If other timestamps are available in the FIFO,
2773ece19502SDivya Koppera 		 * process them.
2774ece19502SDivya Koppera 		 */
2775ece19502SDivya Koppera 		reg = lanphy_read_page_reg(phydev, 5, PTP_CAP_INFO);
2776ece19502SDivya Koppera 	} while (PTP_CAP_INFO_TX_TS_CNT_GET_(reg) > 0);
2777ece19502SDivya Koppera }
2778ece19502SDivya Koppera 
2779ece19502SDivya Koppera static bool lan8814_match_skb(struct kszphy_ptp_priv *ptp_priv,
2780ece19502SDivya Koppera 			      struct lan8814_ptp_rx_ts *rx_ts)
2781ece19502SDivya Koppera {
2782ece19502SDivya Koppera 	struct skb_shared_hwtstamps *shhwtstamps;
2783ece19502SDivya Koppera 	struct sk_buff *skb, *skb_tmp;
2784ece19502SDivya Koppera 	unsigned long flags;
2785ece19502SDivya Koppera 	bool ret = false;
2786ece19502SDivya Koppera 	u16 skb_sig;
2787ece19502SDivya Koppera 
2788ece19502SDivya Koppera 	spin_lock_irqsave(&ptp_priv->rx_queue.lock, flags);
2789ece19502SDivya Koppera 	skb_queue_walk_safe(&ptp_priv->rx_queue, skb, skb_tmp) {
2790ece19502SDivya Koppera 		lan8814_get_sig_rx(skb, &skb_sig);
2791ece19502SDivya Koppera 
2792ece19502SDivya Koppera 		if (memcmp(&skb_sig, &rx_ts->seq_id, sizeof(rx_ts->seq_id)))
2793ece19502SDivya Koppera 			continue;
2794ece19502SDivya Koppera 
2795ece19502SDivya Koppera 		__skb_unlink(skb, &ptp_priv->rx_queue);
2796ece19502SDivya Koppera 
2797ece19502SDivya Koppera 		ret = true;
2798ece19502SDivya Koppera 		break;
2799ece19502SDivya Koppera 	}
2800ece19502SDivya Koppera 	spin_unlock_irqrestore(&ptp_priv->rx_queue.lock, flags);
2801ece19502SDivya Koppera 
2802ece19502SDivya Koppera 	if (ret) {
2803ece19502SDivya Koppera 		shhwtstamps = skb_hwtstamps(skb);
2804ece19502SDivya Koppera 		memset(shhwtstamps, 0, sizeof(*shhwtstamps));
2805ece19502SDivya Koppera 		shhwtstamps->hwtstamp = ktime_set(rx_ts->seconds, rx_ts->nsec);
2806e1f9e434SSebastian Andrzej Siewior 		netif_rx(skb);
2807ece19502SDivya Koppera 	}
2808ece19502SDivya Koppera 
2809ece19502SDivya Koppera 	return ret;
2810ece19502SDivya Koppera }
2811ece19502SDivya Koppera 
2812cafc3662SHoratiu Vultur static void lan8814_match_rx_ts(struct kszphy_ptp_priv *ptp_priv,
2813cafc3662SHoratiu Vultur 				struct lan8814_ptp_rx_ts *rx_ts)
2814ece19502SDivya Koppera {
2815ece19502SDivya Koppera 	unsigned long flags;
2816ece19502SDivya Koppera 
2817ece19502SDivya Koppera 	/* If we failed to match the skb add it to the queue for when
2818ece19502SDivya Koppera 	 * the frame will come
2819ece19502SDivya Koppera 	 */
2820ece19502SDivya Koppera 	if (!lan8814_match_skb(ptp_priv, rx_ts)) {
2821ece19502SDivya Koppera 		spin_lock_irqsave(&ptp_priv->rx_ts_lock, flags);
2822ece19502SDivya Koppera 		list_add(&rx_ts->list, &ptp_priv->rx_ts_list);
2823ece19502SDivya Koppera 		spin_unlock_irqrestore(&ptp_priv->rx_ts_lock, flags);
2824ece19502SDivya Koppera 	} else {
2825ece19502SDivya Koppera 		kfree(rx_ts);
2826ece19502SDivya Koppera 	}
2827cafc3662SHoratiu Vultur }
2828cafc3662SHoratiu Vultur 
2829cafc3662SHoratiu Vultur static void lan8814_get_rx_ts(struct kszphy_ptp_priv *ptp_priv)
2830cafc3662SHoratiu Vultur {
2831cafc3662SHoratiu Vultur 	struct phy_device *phydev = ptp_priv->phydev;
2832cafc3662SHoratiu Vultur 	struct lan8814_ptp_rx_ts *rx_ts;
2833cafc3662SHoratiu Vultur 	u32 reg;
2834cafc3662SHoratiu Vultur 
2835cafc3662SHoratiu Vultur 	do {
2836cafc3662SHoratiu Vultur 		rx_ts = kzalloc(sizeof(*rx_ts), GFP_KERNEL);
2837cafc3662SHoratiu Vultur 		if (!rx_ts)
2838cafc3662SHoratiu Vultur 			return;
2839cafc3662SHoratiu Vultur 
2840cafc3662SHoratiu Vultur 		lan8814_ptp_rx_ts_get(phydev, &rx_ts->seconds, &rx_ts->nsec,
2841cafc3662SHoratiu Vultur 				      &rx_ts->seq_id);
2842cafc3662SHoratiu Vultur 		lan8814_match_rx_ts(ptp_priv, rx_ts);
2843ece19502SDivya Koppera 
2844ece19502SDivya Koppera 		/* If other timestamps are available in the FIFO,
2845ece19502SDivya Koppera 		 * process them.
2846ece19502SDivya Koppera 		 */
2847ece19502SDivya Koppera 		reg = lanphy_read_page_reg(phydev, 5, PTP_CAP_INFO);
2848ece19502SDivya Koppera 	} while (PTP_CAP_INFO_RX_TS_CNT_GET_(reg) > 0);
2849ece19502SDivya Koppera }
2850ece19502SDivya Koppera 
28517abd92a5SHoratiu Vultur static void lan8814_handle_ptp_interrupt(struct phy_device *phydev, u16 status)
2852ece19502SDivya Koppera {
2853ece19502SDivya Koppera 	struct kszphy_priv *priv = phydev->priv;
2854ece19502SDivya Koppera 	struct kszphy_ptp_priv *ptp_priv = &priv->ptp_priv;
2855ece19502SDivya Koppera 
2856ece19502SDivya Koppera 	if (status & PTP_TSU_INT_STS_PTP_TX_TS_EN_)
2857ece19502SDivya Koppera 		lan8814_get_tx_ts(ptp_priv);
2858ece19502SDivya Koppera 
2859ece19502SDivya Koppera 	if (status & PTP_TSU_INT_STS_PTP_RX_TS_EN_)
2860ece19502SDivya Koppera 		lan8814_get_rx_ts(ptp_priv);
2861ece19502SDivya Koppera 
2862ece19502SDivya Koppera 	if (status & PTP_TSU_INT_STS_PTP_TX_TS_OVRFL_INT_) {
2863ece19502SDivya Koppera 		lan8814_flush_fifo(phydev, true);
2864ece19502SDivya Koppera 		skb_queue_purge(&ptp_priv->tx_queue);
2865ece19502SDivya Koppera 	}
2866ece19502SDivya Koppera 
2867ece19502SDivya Koppera 	if (status & PTP_TSU_INT_STS_PTP_RX_TS_OVRFL_INT_) {
2868ece19502SDivya Koppera 		lan8814_flush_fifo(phydev, false);
2869ece19502SDivya Koppera 		skb_queue_purge(&ptp_priv->rx_queue);
2870ece19502SDivya Koppera 	}
2871ece19502SDivya Koppera }
2872ece19502SDivya Koppera 
28737c2dcfa2SHoratiu Vultur static int lan8804_config_init(struct phy_device *phydev)
28747c2dcfa2SHoratiu Vultur {
28757c2dcfa2SHoratiu Vultur 	int val;
28767c2dcfa2SHoratiu Vultur 
28777c2dcfa2SHoratiu Vultur 	/* MDI-X setting for swap A,B transmit */
28787c2dcfa2SHoratiu Vultur 	val = lanphy_read_page_reg(phydev, 2, LAN8804_ALIGN_SWAP);
28797c2dcfa2SHoratiu Vultur 	val &= ~LAN8804_ALIGN_TX_A_B_SWAP_MASK;
28807c2dcfa2SHoratiu Vultur 	val |= LAN8804_ALIGN_TX_A_B_SWAP;
28817c2dcfa2SHoratiu Vultur 	lanphy_write_page_reg(phydev, 2, LAN8804_ALIGN_SWAP, val);
28827c2dcfa2SHoratiu Vultur 
28837c2dcfa2SHoratiu Vultur 	/* Make sure that the PHY will not stop generating the clock when the
28847c2dcfa2SHoratiu Vultur 	 * link partner goes down
28857c2dcfa2SHoratiu Vultur 	 */
28867c2dcfa2SHoratiu Vultur 	lanphy_write_page_reg(phydev, 31, LAN8814_CLOCK_MANAGEMENT, 0x27e);
28877c2dcfa2SHoratiu Vultur 	lanphy_read_page_reg(phydev, 1, LAN8814_LINK_QUALITY);
28887c2dcfa2SHoratiu Vultur 
28897c2dcfa2SHoratiu Vultur 	return 0;
28907c2dcfa2SHoratiu Vultur }
28917c2dcfa2SHoratiu Vultur 
2892b324c6e5SHoratiu Vultur static irqreturn_t lan8804_handle_interrupt(struct phy_device *phydev)
2893b324c6e5SHoratiu Vultur {
2894b324c6e5SHoratiu Vultur 	int status;
2895b324c6e5SHoratiu Vultur 
2896b324c6e5SHoratiu Vultur 	status = phy_read(phydev, LAN8814_INTS);
2897b324c6e5SHoratiu Vultur 	if (status < 0) {
2898b324c6e5SHoratiu Vultur 		phy_error(phydev);
2899b324c6e5SHoratiu Vultur 		return IRQ_NONE;
2900b324c6e5SHoratiu Vultur 	}
2901b324c6e5SHoratiu Vultur 
2902b324c6e5SHoratiu Vultur 	if (status > 0)
2903b324c6e5SHoratiu Vultur 		phy_trigger_machine(phydev);
2904b324c6e5SHoratiu Vultur 
2905b324c6e5SHoratiu Vultur 	return IRQ_HANDLED;
2906b324c6e5SHoratiu Vultur }
2907b324c6e5SHoratiu Vultur 
2908b324c6e5SHoratiu Vultur #define LAN8804_OUTPUT_CONTROL			25
2909b324c6e5SHoratiu Vultur #define LAN8804_OUTPUT_CONTROL_INTR_BUFFER	BIT(14)
2910b324c6e5SHoratiu Vultur #define LAN8804_CONTROL				31
2911b324c6e5SHoratiu Vultur #define LAN8804_CONTROL_INTR_POLARITY		BIT(14)
2912b324c6e5SHoratiu Vultur 
2913b324c6e5SHoratiu Vultur static int lan8804_config_intr(struct phy_device *phydev)
2914b324c6e5SHoratiu Vultur {
2915b324c6e5SHoratiu Vultur 	int err;
2916b324c6e5SHoratiu Vultur 
2917b324c6e5SHoratiu Vultur 	/* This is an internal PHY of lan966x and is not possible to change the
2918b324c6e5SHoratiu Vultur 	 * polarity on the GIC found in lan966x, therefore change the polarity
2919b324c6e5SHoratiu Vultur 	 * of the interrupt in the PHY from being active low instead of active
2920b324c6e5SHoratiu Vultur 	 * high.
2921b324c6e5SHoratiu Vultur 	 */
2922b324c6e5SHoratiu Vultur 	phy_write(phydev, LAN8804_CONTROL, LAN8804_CONTROL_INTR_POLARITY);
2923b324c6e5SHoratiu Vultur 
2924b324c6e5SHoratiu Vultur 	/* By default interrupt buffer is open-drain in which case the interrupt
2925b324c6e5SHoratiu Vultur 	 * can be active only low. Therefore change the interrupt buffer to be
2926b324c6e5SHoratiu Vultur 	 * push-pull to be able to change interrupt polarity
2927b324c6e5SHoratiu Vultur 	 */
2928b324c6e5SHoratiu Vultur 	phy_write(phydev, LAN8804_OUTPUT_CONTROL,
2929b324c6e5SHoratiu Vultur 		  LAN8804_OUTPUT_CONTROL_INTR_BUFFER);
2930b324c6e5SHoratiu Vultur 
2931b324c6e5SHoratiu Vultur 	if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
2932b324c6e5SHoratiu Vultur 		err = phy_read(phydev, LAN8814_INTS);
2933b324c6e5SHoratiu Vultur 		if (err < 0)
2934b324c6e5SHoratiu Vultur 			return err;
2935b324c6e5SHoratiu Vultur 
2936b324c6e5SHoratiu Vultur 		err = phy_write(phydev, LAN8814_INTC, LAN8814_INT_LINK);
2937b324c6e5SHoratiu Vultur 		if (err)
2938b324c6e5SHoratiu Vultur 			return err;
2939b324c6e5SHoratiu Vultur 	} else {
2940b324c6e5SHoratiu Vultur 		err = phy_write(phydev, LAN8814_INTC, 0);
2941b324c6e5SHoratiu Vultur 		if (err)
2942b324c6e5SHoratiu Vultur 			return err;
2943b324c6e5SHoratiu Vultur 
2944b324c6e5SHoratiu Vultur 		err = phy_read(phydev, LAN8814_INTS);
2945b324c6e5SHoratiu Vultur 		if (err < 0)
2946b324c6e5SHoratiu Vultur 			return err;
2947b324c6e5SHoratiu Vultur 	}
2948b324c6e5SHoratiu Vultur 
2949b324c6e5SHoratiu Vultur 	return 0;
2950b324c6e5SHoratiu Vultur }
2951b324c6e5SHoratiu Vultur 
2952b3ec7248SDivya Koppera static irqreturn_t lan8814_handle_interrupt(struct phy_device *phydev)
2953b3ec7248SDivya Koppera {
29542002fbacSMichael Walle 	int ret = IRQ_NONE;
29557abd92a5SHoratiu Vultur 	int irq_status;
2956b3ec7248SDivya Koppera 
2957b3ec7248SDivya Koppera 	irq_status = phy_read(phydev, LAN8814_INTS);
2958ece19502SDivya Koppera 	if (irq_status < 0) {
2959ece19502SDivya Koppera 		phy_error(phydev);
2960ece19502SDivya Koppera 		return IRQ_NONE;
2961ece19502SDivya Koppera 	}
2962ece19502SDivya Koppera 
29632002fbacSMichael Walle 	if (irq_status & LAN8814_INT_LINK) {
29642002fbacSMichael Walle 		phy_trigger_machine(phydev);
29652002fbacSMichael Walle 		ret = IRQ_HANDLED;
29662002fbacSMichael Walle 	}
29672002fbacSMichael Walle 
29687abd92a5SHoratiu Vultur 	while (true) {
29697abd92a5SHoratiu Vultur 		irq_status = lanphy_read_page_reg(phydev, 5, PTP_TSU_INT_STS);
29707abd92a5SHoratiu Vultur 		if (!irq_status)
2971ece19502SDivya Koppera 			break;
29727abd92a5SHoratiu Vultur 
29737abd92a5SHoratiu Vultur 		lan8814_handle_ptp_interrupt(phydev, irq_status);
29747abd92a5SHoratiu Vultur 		ret = IRQ_HANDLED;
29752002fbacSMichael Walle 	}
29762002fbacSMichael Walle 
29772002fbacSMichael Walle 	return ret;
2978b3ec7248SDivya Koppera }
2979b3ec7248SDivya Koppera 
2980b3ec7248SDivya Koppera static int lan8814_ack_interrupt(struct phy_device *phydev)
2981b3ec7248SDivya Koppera {
2982b3ec7248SDivya Koppera 	/* bit[12..0] int status, which is a read and clear register. */
2983b3ec7248SDivya Koppera 	int rc;
2984b3ec7248SDivya Koppera 
2985b3ec7248SDivya Koppera 	rc = phy_read(phydev, LAN8814_INTS);
2986b3ec7248SDivya Koppera 
2987b3ec7248SDivya Koppera 	return (rc < 0) ? rc : 0;
2988b3ec7248SDivya Koppera }
2989b3ec7248SDivya Koppera 
2990b3ec7248SDivya Koppera static int lan8814_config_intr(struct phy_device *phydev)
2991b3ec7248SDivya Koppera {
2992b3ec7248SDivya Koppera 	int err;
2993b3ec7248SDivya Koppera 
2994b3ec7248SDivya Koppera 	lanphy_write_page_reg(phydev, 4, LAN8814_INTR_CTRL_REG,
2995b3ec7248SDivya Koppera 			      LAN8814_INTR_CTRL_REG_POLARITY |
2996b3ec7248SDivya Koppera 			      LAN8814_INTR_CTRL_REG_INTR_ENABLE);
2997b3ec7248SDivya Koppera 
2998b3ec7248SDivya Koppera 	/* enable / disable interrupts */
2999b3ec7248SDivya Koppera 	if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
3000b3ec7248SDivya Koppera 		err = lan8814_ack_interrupt(phydev);
3001b3ec7248SDivya Koppera 		if (err)
3002b3ec7248SDivya Koppera 			return err;
3003b3ec7248SDivya Koppera 
3004b3ec7248SDivya Koppera 		err = phy_write(phydev, LAN8814_INTC, LAN8814_INT_LINK);
3005b3ec7248SDivya Koppera 	} else {
3006b3ec7248SDivya Koppera 		err = phy_write(phydev, LAN8814_INTC, 0);
3007b3ec7248SDivya Koppera 		if (err)
3008b3ec7248SDivya Koppera 			return err;
3009b3ec7248SDivya Koppera 
3010b3ec7248SDivya Koppera 		err = lan8814_ack_interrupt(phydev);
3011b3ec7248SDivya Koppera 	}
3012b3ec7248SDivya Koppera 
3013b3ec7248SDivya Koppera 	return err;
3014b3ec7248SDivya Koppera }
3015b3ec7248SDivya Koppera 
3016ece19502SDivya Koppera static void lan8814_ptp_init(struct phy_device *phydev)
3017ece19502SDivya Koppera {
3018ece19502SDivya Koppera 	struct kszphy_priv *priv = phydev->priv;
3019ece19502SDivya Koppera 	struct kszphy_ptp_priv *ptp_priv = &priv->ptp_priv;
3020ece19502SDivya Koppera 	u32 temp;
3021ece19502SDivya Koppera 
302231d00ca4SMichael Walle 	if (!IS_ENABLED(CONFIG_PTP_1588_CLOCK) ||
302331d00ca4SMichael Walle 	    !IS_ENABLED(CONFIG_NETWORK_PHY_TIMESTAMPING))
302431d00ca4SMichael Walle 		return;
302531d00ca4SMichael Walle 
3026ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 5, TSU_HARD_RESET, TSU_HARD_RESET_);
3027ece19502SDivya Koppera 
3028ece19502SDivya Koppera 	temp = lanphy_read_page_reg(phydev, 5, PTP_TX_MOD);
3029ece19502SDivya Koppera 	temp |= PTP_TX_MOD_BAD_UDPV4_CHKSUM_FORCE_FCS_DIS_;
3030ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 5, PTP_TX_MOD, temp);
3031ece19502SDivya Koppera 
3032ece19502SDivya Koppera 	temp = lanphy_read_page_reg(phydev, 5, PTP_RX_MOD);
3033ece19502SDivya Koppera 	temp |= PTP_RX_MOD_BAD_UDPV4_CHKSUM_FORCE_FCS_DIS_;
3034ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 5, PTP_RX_MOD, temp);
3035ece19502SDivya Koppera 
3036ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 5, PTP_RX_PARSE_CONFIG, 0);
3037ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 5, PTP_TX_PARSE_CONFIG, 0);
3038ece19502SDivya Koppera 
3039ece19502SDivya Koppera 	/* Removing default registers configs related to L2 and IP */
3040ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 5, PTP_TX_PARSE_L2_ADDR_EN, 0);
3041ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 5, PTP_RX_PARSE_L2_ADDR_EN, 0);
3042ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 5, PTP_TX_PARSE_IP_ADDR_EN, 0);
3043ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 5, PTP_RX_PARSE_IP_ADDR_EN, 0);
3044ece19502SDivya Koppera 
3045ece19502SDivya Koppera 	skb_queue_head_init(&ptp_priv->tx_queue);
3046ece19502SDivya Koppera 	skb_queue_head_init(&ptp_priv->rx_queue);
3047ece19502SDivya Koppera 	INIT_LIST_HEAD(&ptp_priv->rx_ts_list);
3048ece19502SDivya Koppera 	spin_lock_init(&ptp_priv->rx_ts_lock);
3049ece19502SDivya Koppera 
3050ece19502SDivya Koppera 	ptp_priv->phydev = phydev;
3051ece19502SDivya Koppera 
3052ece19502SDivya Koppera 	ptp_priv->mii_ts.rxtstamp = lan8814_rxtstamp;
3053ece19502SDivya Koppera 	ptp_priv->mii_ts.txtstamp = lan8814_txtstamp;
3054ece19502SDivya Koppera 	ptp_priv->mii_ts.hwtstamp = lan8814_hwtstamp;
3055ece19502SDivya Koppera 	ptp_priv->mii_ts.ts_info  = lan8814_ts_info;
3056ece19502SDivya Koppera 
3057ece19502SDivya Koppera 	phydev->mii_ts = &ptp_priv->mii_ts;
3058ece19502SDivya Koppera }
3059ece19502SDivya Koppera 
3060ece19502SDivya Koppera static int lan8814_ptp_probe_once(struct phy_device *phydev)
3061ece19502SDivya Koppera {
3062ece19502SDivya Koppera 	struct lan8814_shared_priv *shared = phydev->shared->priv;
3063ece19502SDivya Koppera 
3064ece19502SDivya Koppera 	/* Initialise shared lock for clock*/
3065ece19502SDivya Koppera 	mutex_init(&shared->shared_lock);
3066ece19502SDivya Koppera 
3067ece19502SDivya Koppera 	shared->ptp_clock_info.owner = THIS_MODULE;
3068ece19502SDivya Koppera 	snprintf(shared->ptp_clock_info.name, 30, "%s", phydev->drv->name);
3069ece19502SDivya Koppera 	shared->ptp_clock_info.max_adj = 31249999;
3070ece19502SDivya Koppera 	shared->ptp_clock_info.n_alarm = 0;
3071ece19502SDivya Koppera 	shared->ptp_clock_info.n_ext_ts = 0;
3072ece19502SDivya Koppera 	shared->ptp_clock_info.n_pins = 0;
3073ece19502SDivya Koppera 	shared->ptp_clock_info.pps = 0;
3074ece19502SDivya Koppera 	shared->ptp_clock_info.pin_config = NULL;
3075ece19502SDivya Koppera 	shared->ptp_clock_info.adjfine = lan8814_ptpci_adjfine;
3076ece19502SDivya Koppera 	shared->ptp_clock_info.adjtime = lan8814_ptpci_adjtime;
3077ece19502SDivya Koppera 	shared->ptp_clock_info.gettime64 = lan8814_ptpci_gettime64;
3078ece19502SDivya Koppera 	shared->ptp_clock_info.settime64 = lan8814_ptpci_settime64;
3079ece19502SDivya Koppera 	shared->ptp_clock_info.getcrosststamp = NULL;
3080ece19502SDivya Koppera 
3081ece19502SDivya Koppera 	shared->ptp_clock = ptp_clock_register(&shared->ptp_clock_info,
3082ece19502SDivya Koppera 					       &phydev->mdio.dev);
30833f88d7d1SDivya Koppera 	if (IS_ERR(shared->ptp_clock)) {
3084ece19502SDivya Koppera 		phydev_err(phydev, "ptp_clock_register failed %lu\n",
3085ece19502SDivya Koppera 			   PTR_ERR(shared->ptp_clock));
3086ece19502SDivya Koppera 		return -EINVAL;
3087ece19502SDivya Koppera 	}
3088ece19502SDivya Koppera 
30893f88d7d1SDivya Koppera 	/* Check if PHC support is missing at the configuration level */
30903f88d7d1SDivya Koppera 	if (!shared->ptp_clock)
30913f88d7d1SDivya Koppera 		return 0;
30923f88d7d1SDivya Koppera 
3093ece19502SDivya Koppera 	phydev_dbg(phydev, "successfully registered ptp clock\n");
3094ece19502SDivya Koppera 
3095ece19502SDivya Koppera 	shared->phydev = phydev;
3096ece19502SDivya Koppera 
3097ece19502SDivya Koppera 	/* The EP.4 is shared between all the PHYs in the package and also it
3098ece19502SDivya Koppera 	 * can be accessed by any of the PHYs
3099ece19502SDivya Koppera 	 */
3100ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 4, LTC_HARD_RESET, LTC_HARD_RESET_);
3101ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 4, PTP_OPERATING_MODE,
3102ece19502SDivya Koppera 			      PTP_OPERATING_MODE_STANDALONE_);
3103ece19502SDivya Koppera 
3104ece19502SDivya Koppera 	return 0;
3105ece19502SDivya Koppera }
3106ece19502SDivya Koppera 
3107a516b7f7SDivya Koppera static void lan8814_setup_led(struct phy_device *phydev, int val)
3108a516b7f7SDivya Koppera {
3109a516b7f7SDivya Koppera 	int temp;
3110a516b7f7SDivya Koppera 
3111a516b7f7SDivya Koppera 	temp = lanphy_read_page_reg(phydev, 5, LAN8814_LED_CTRL_1);
3112a516b7f7SDivya Koppera 
3113a516b7f7SDivya Koppera 	if (val)
3114a516b7f7SDivya Koppera 		temp |= LAN8814_LED_CTRL_1_KSZ9031_LED_MODE_;
3115a516b7f7SDivya Koppera 	else
3116a516b7f7SDivya Koppera 		temp &= ~LAN8814_LED_CTRL_1_KSZ9031_LED_MODE_;
3117a516b7f7SDivya Koppera 
3118a516b7f7SDivya Koppera 	lanphy_write_page_reg(phydev, 5, LAN8814_LED_CTRL_1, temp);
3119a516b7f7SDivya Koppera }
3120a516b7f7SDivya Koppera 
3121ece19502SDivya Koppera static int lan8814_config_init(struct phy_device *phydev)
3122ece19502SDivya Koppera {
3123a516b7f7SDivya Koppera 	struct kszphy_priv *lan8814 = phydev->priv;
3124ece19502SDivya Koppera 	int val;
3125ece19502SDivya Koppera 
3126ece19502SDivya Koppera 	/* Reset the PHY */
3127ece19502SDivya Koppera 	val = lanphy_read_page_reg(phydev, 4, LAN8814_QSGMII_SOFT_RESET);
3128ece19502SDivya Koppera 	val |= LAN8814_QSGMII_SOFT_RESET_BIT;
3129ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 4, LAN8814_QSGMII_SOFT_RESET, val);
3130ece19502SDivya Koppera 
3131ece19502SDivya Koppera 	/* Disable ANEG with QSGMII PCS Host side */
3132ece19502SDivya Koppera 	val = lanphy_read_page_reg(phydev, 5, LAN8814_QSGMII_PCS1G_ANEG_CONFIG);
3133ece19502SDivya Koppera 	val &= ~LAN8814_QSGMII_PCS1G_ANEG_CONFIG_ANEG_ENA;
3134ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 5, LAN8814_QSGMII_PCS1G_ANEG_CONFIG, val);
3135ece19502SDivya Koppera 
3136ece19502SDivya Koppera 	/* MDI-X setting for swap A,B transmit */
3137ece19502SDivya Koppera 	val = lanphy_read_page_reg(phydev, 2, LAN8814_ALIGN_SWAP);
3138ece19502SDivya Koppera 	val &= ~LAN8814_ALIGN_TX_A_B_SWAP_MASK;
3139ece19502SDivya Koppera 	val |= LAN8814_ALIGN_TX_A_B_SWAP;
3140ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 2, LAN8814_ALIGN_SWAP, val);
3141ece19502SDivya Koppera 
3142a516b7f7SDivya Koppera 	if (lan8814->led_mode >= 0)
3143a516b7f7SDivya Koppera 		lan8814_setup_led(phydev, lan8814->led_mode);
3144a516b7f7SDivya Koppera 
3145ece19502SDivya Koppera 	return 0;
3146ece19502SDivya Koppera }
3147ece19502SDivya Koppera 
31484a4ce822SHoratiu Vultur /* It is expected that there will not be any 'lan8814_take_coma_mode'
31494a4ce822SHoratiu Vultur  * function called in suspend. Because the GPIO line can be shared, so if one of
31504a4ce822SHoratiu Vultur  * the phys goes back in coma mode, then all the other PHYs will go, which is
31514a4ce822SHoratiu Vultur  * wrong.
31524a4ce822SHoratiu Vultur  */
3153738871b0SMichael Walle static int lan8814_release_coma_mode(struct phy_device *phydev)
3154738871b0SMichael Walle {
3155738871b0SMichael Walle 	struct gpio_desc *gpiod;
3156738871b0SMichael Walle 
3157738871b0SMichael Walle 	gpiod = devm_gpiod_get_optional(&phydev->mdio.dev, "coma-mode",
31584a4ce822SHoratiu Vultur 					GPIOD_OUT_HIGH_OPEN_DRAIN |
31594a4ce822SHoratiu Vultur 					GPIOD_FLAGS_BIT_NONEXCLUSIVE);
3160738871b0SMichael Walle 	if (IS_ERR(gpiod))
3161738871b0SMichael Walle 		return PTR_ERR(gpiod);
3162738871b0SMichael Walle 
3163738871b0SMichael Walle 	gpiod_set_consumer_name(gpiod, "LAN8814 coma mode");
3164738871b0SMichael Walle 	gpiod_set_value_cansleep(gpiod, 0);
3165738871b0SMichael Walle 
3166738871b0SMichael Walle 	return 0;
3167738871b0SMichael Walle }
3168738871b0SMichael Walle 
3169ece19502SDivya Koppera static int lan8814_probe(struct phy_device *phydev)
3170ece19502SDivya Koppera {
3171a516b7f7SDivya Koppera 	const struct kszphy_type *type = phydev->drv->driver_data;
3172ece19502SDivya Koppera 	struct kszphy_priv *priv;
3173ece19502SDivya Koppera 	u16 addr;
3174ece19502SDivya Koppera 	int err;
3175ece19502SDivya Koppera 
3176ece19502SDivya Koppera 	priv = devm_kzalloc(&phydev->mdio.dev, sizeof(*priv), GFP_KERNEL);
3177ece19502SDivya Koppera 	if (!priv)
3178ece19502SDivya Koppera 		return -ENOMEM;
3179ece19502SDivya Koppera 
3180ece19502SDivya Koppera 	phydev->priv = priv;
3181ece19502SDivya Koppera 
3182a516b7f7SDivya Koppera 	priv->type = type;
3183a516b7f7SDivya Koppera 
3184a516b7f7SDivya Koppera 	kszphy_parse_led_mode(phydev);
3185a516b7f7SDivya Koppera 
3186ece19502SDivya Koppera 	/* Strap-in value for PHY address, below register read gives starting
3187ece19502SDivya Koppera 	 * phy address value
3188ece19502SDivya Koppera 	 */
3189ece19502SDivya Koppera 	addr = lanphy_read_page_reg(phydev, 4, 0) & 0x1F;
3190ece19502SDivya Koppera 	devm_phy_package_join(&phydev->mdio.dev, phydev,
3191ece19502SDivya Koppera 			      addr, sizeof(struct lan8814_shared_priv));
3192ece19502SDivya Koppera 
3193ece19502SDivya Koppera 	if (phy_package_init_once(phydev)) {
3194738871b0SMichael Walle 		err = lan8814_release_coma_mode(phydev);
3195738871b0SMichael Walle 		if (err)
3196738871b0SMichael Walle 			return err;
3197738871b0SMichael Walle 
3198ece19502SDivya Koppera 		err = lan8814_ptp_probe_once(phydev);
3199ece19502SDivya Koppera 		if (err)
3200ece19502SDivya Koppera 			return err;
3201ece19502SDivya Koppera 	}
3202ece19502SDivya Koppera 
3203ece19502SDivya Koppera 	lan8814_ptp_init(phydev);
3204ece19502SDivya Koppera 
3205ece19502SDivya Koppera 	return 0;
3206ece19502SDivya Koppera }
3207ece19502SDivya Koppera 
3208a8f1a19dSHoratiu Vultur #define LAN8841_MMD_TIMER_REG			0
3209a8f1a19dSHoratiu Vultur #define LAN8841_MMD0_REGISTER_17		17
3210a8f1a19dSHoratiu Vultur #define LAN8841_MMD0_REGISTER_17_DROP_OPT(x)	((x) & 0x3)
3211a8f1a19dSHoratiu Vultur #define LAN8841_MMD0_REGISTER_17_XMIT_TOG_TX_DIS	BIT(3)
3212a8f1a19dSHoratiu Vultur #define LAN8841_OPERATION_MODE_STRAP_OVERRIDE_LOW_REG	2
3213a8f1a19dSHoratiu Vultur #define LAN8841_OPERATION_MODE_STRAP_OVERRIDE_LOW_REG_MAGJACK	BIT(14)
3214a8f1a19dSHoratiu Vultur #define LAN8841_MMD_ANALOG_REG			28
3215a8f1a19dSHoratiu Vultur #define LAN8841_ANALOG_CONTROL_1		1
3216a8f1a19dSHoratiu Vultur #define LAN8841_ANALOG_CONTROL_1_PLL_TRIM(x)	(((x) & 0x3) << 5)
3217a8f1a19dSHoratiu Vultur #define LAN8841_ANALOG_CONTROL_10		13
3218a8f1a19dSHoratiu Vultur #define LAN8841_ANALOG_CONTROL_10_PLL_DIV(x)	((x) & 0x3)
3219a8f1a19dSHoratiu Vultur #define LAN8841_ANALOG_CONTROL_11		14
3220a8f1a19dSHoratiu Vultur #define LAN8841_ANALOG_CONTROL_11_LDO_REF(x)	(((x) & 0x7) << 12)
3221a8f1a19dSHoratiu Vultur #define LAN8841_TX_LOW_I_CH_C_D_POWER_MANAGMENT	69
3222a8f1a19dSHoratiu Vultur #define LAN8841_TX_LOW_I_CH_C_D_POWER_MANAGMENT_VAL 0xbffc
3223a8f1a19dSHoratiu Vultur #define LAN8841_BTRX_POWER_DOWN			70
3224a8f1a19dSHoratiu Vultur #define LAN8841_BTRX_POWER_DOWN_QBIAS_CH_A	BIT(0)
3225a8f1a19dSHoratiu Vultur #define LAN8841_BTRX_POWER_DOWN_BTRX_CH_A	BIT(1)
3226a8f1a19dSHoratiu Vultur #define LAN8841_BTRX_POWER_DOWN_QBIAS_CH_B	BIT(2)
3227a8f1a19dSHoratiu Vultur #define LAN8841_BTRX_POWER_DOWN_BTRX_CH_B	BIT(3)
3228a8f1a19dSHoratiu Vultur #define LAN8841_BTRX_POWER_DOWN_BTRX_CH_C	BIT(5)
3229a8f1a19dSHoratiu Vultur #define LAN8841_BTRX_POWER_DOWN_BTRX_CH_D	BIT(7)
3230a8f1a19dSHoratiu Vultur #define LAN8841_ADC_CHANNEL_MASK		198
3231cafc3662SHoratiu Vultur #define LAN8841_PTP_RX_PARSE_L2_ADDR_EN		370
3232cafc3662SHoratiu Vultur #define LAN8841_PTP_RX_PARSE_IP_ADDR_EN		371
3233cafc3662SHoratiu Vultur #define LAN8841_PTP_TX_PARSE_L2_ADDR_EN		434
3234cafc3662SHoratiu Vultur #define LAN8841_PTP_TX_PARSE_IP_ADDR_EN		435
3235cafc3662SHoratiu Vultur #define LAN8841_PTP_CMD_CTL			256
3236cafc3662SHoratiu Vultur #define LAN8841_PTP_CMD_CTL_PTP_ENABLE		BIT(2)
3237cafc3662SHoratiu Vultur #define LAN8841_PTP_CMD_CTL_PTP_DISABLE		BIT(1)
3238cafc3662SHoratiu Vultur #define LAN8841_PTP_CMD_CTL_PTP_RESET		BIT(0)
3239cafc3662SHoratiu Vultur #define LAN8841_PTP_RX_PARSE_CONFIG		368
3240cafc3662SHoratiu Vultur #define LAN8841_PTP_TX_PARSE_CONFIG		432
3241a8f1a19dSHoratiu Vultur 
3242a8f1a19dSHoratiu Vultur static int lan8841_config_init(struct phy_device *phydev)
3243a8f1a19dSHoratiu Vultur {
3244a8f1a19dSHoratiu Vultur 	int ret;
3245a8f1a19dSHoratiu Vultur 
3246a8f1a19dSHoratiu Vultur 	ret = ksz9131_config_init(phydev);
3247a8f1a19dSHoratiu Vultur 	if (ret)
3248a8f1a19dSHoratiu Vultur 		return ret;
3249a8f1a19dSHoratiu Vultur 
3250cafc3662SHoratiu Vultur 	/* Initialize the HW by resetting everything */
3251cafc3662SHoratiu Vultur 	phy_modify_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
3252cafc3662SHoratiu Vultur 		       LAN8841_PTP_CMD_CTL,
3253cafc3662SHoratiu Vultur 		       LAN8841_PTP_CMD_CTL_PTP_RESET,
3254cafc3662SHoratiu Vultur 		       LAN8841_PTP_CMD_CTL_PTP_RESET);
3255cafc3662SHoratiu Vultur 
3256cafc3662SHoratiu Vultur 	phy_modify_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
3257cafc3662SHoratiu Vultur 		       LAN8841_PTP_CMD_CTL,
3258cafc3662SHoratiu Vultur 		       LAN8841_PTP_CMD_CTL_PTP_ENABLE,
3259cafc3662SHoratiu Vultur 		       LAN8841_PTP_CMD_CTL_PTP_ENABLE);
3260cafc3662SHoratiu Vultur 
3261cafc3662SHoratiu Vultur 	/* Don't process any frames */
3262cafc3662SHoratiu Vultur 	phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
3263cafc3662SHoratiu Vultur 		      LAN8841_PTP_RX_PARSE_CONFIG, 0);
3264cafc3662SHoratiu Vultur 	phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
3265cafc3662SHoratiu Vultur 		      LAN8841_PTP_TX_PARSE_CONFIG, 0);
3266cafc3662SHoratiu Vultur 	phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
3267cafc3662SHoratiu Vultur 		      LAN8841_PTP_TX_PARSE_L2_ADDR_EN, 0);
3268cafc3662SHoratiu Vultur 	phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
3269cafc3662SHoratiu Vultur 		      LAN8841_PTP_RX_PARSE_L2_ADDR_EN, 0);
3270cafc3662SHoratiu Vultur 	phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
3271cafc3662SHoratiu Vultur 		      LAN8841_PTP_TX_PARSE_IP_ADDR_EN, 0);
3272cafc3662SHoratiu Vultur 	phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
3273cafc3662SHoratiu Vultur 		      LAN8841_PTP_RX_PARSE_IP_ADDR_EN, 0);
3274cafc3662SHoratiu Vultur 
3275a8f1a19dSHoratiu Vultur 	/* 100BT Clause 40 improvenent errata */
3276a8f1a19dSHoratiu Vultur 	phy_write_mmd(phydev, LAN8841_MMD_ANALOG_REG,
3277a8f1a19dSHoratiu Vultur 		      LAN8841_ANALOG_CONTROL_1,
3278a8f1a19dSHoratiu Vultur 		      LAN8841_ANALOG_CONTROL_1_PLL_TRIM(0x2));
3279a8f1a19dSHoratiu Vultur 	phy_write_mmd(phydev, LAN8841_MMD_ANALOG_REG,
3280a8f1a19dSHoratiu Vultur 		      LAN8841_ANALOG_CONTROL_10,
3281a8f1a19dSHoratiu Vultur 		      LAN8841_ANALOG_CONTROL_10_PLL_DIV(0x1));
3282a8f1a19dSHoratiu Vultur 
3283a8f1a19dSHoratiu Vultur 	/* 10M/100M Ethernet Signal Tuning Errata for Shorted-Center Tap
3284a8f1a19dSHoratiu Vultur 	 * Magnetics
3285a8f1a19dSHoratiu Vultur 	 */
3286a8f1a19dSHoratiu Vultur 	ret = phy_read_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
3287a8f1a19dSHoratiu Vultur 			   LAN8841_OPERATION_MODE_STRAP_OVERRIDE_LOW_REG);
3288a8f1a19dSHoratiu Vultur 	if (ret & LAN8841_OPERATION_MODE_STRAP_OVERRIDE_LOW_REG_MAGJACK) {
3289a8f1a19dSHoratiu Vultur 		phy_write_mmd(phydev, LAN8841_MMD_ANALOG_REG,
3290a8f1a19dSHoratiu Vultur 			      LAN8841_TX_LOW_I_CH_C_D_POWER_MANAGMENT,
3291a8f1a19dSHoratiu Vultur 			      LAN8841_TX_LOW_I_CH_C_D_POWER_MANAGMENT_VAL);
3292a8f1a19dSHoratiu Vultur 		phy_write_mmd(phydev, LAN8841_MMD_ANALOG_REG,
3293a8f1a19dSHoratiu Vultur 			      LAN8841_BTRX_POWER_DOWN,
3294a8f1a19dSHoratiu Vultur 			      LAN8841_BTRX_POWER_DOWN_QBIAS_CH_A |
3295a8f1a19dSHoratiu Vultur 			      LAN8841_BTRX_POWER_DOWN_BTRX_CH_A |
3296a8f1a19dSHoratiu Vultur 			      LAN8841_BTRX_POWER_DOWN_QBIAS_CH_B |
3297a8f1a19dSHoratiu Vultur 			      LAN8841_BTRX_POWER_DOWN_BTRX_CH_B |
3298a8f1a19dSHoratiu Vultur 			      LAN8841_BTRX_POWER_DOWN_BTRX_CH_C |
3299a8f1a19dSHoratiu Vultur 			      LAN8841_BTRX_POWER_DOWN_BTRX_CH_D);
3300a8f1a19dSHoratiu Vultur 	}
3301a8f1a19dSHoratiu Vultur 
3302a8f1a19dSHoratiu Vultur 	/* LDO Adjustment errata */
3303a8f1a19dSHoratiu Vultur 	phy_write_mmd(phydev, LAN8841_MMD_ANALOG_REG,
3304a8f1a19dSHoratiu Vultur 		      LAN8841_ANALOG_CONTROL_11,
3305a8f1a19dSHoratiu Vultur 		      LAN8841_ANALOG_CONTROL_11_LDO_REF(1));
3306a8f1a19dSHoratiu Vultur 
3307a8f1a19dSHoratiu Vultur 	/* 100BT RGMII latency tuning errata */
3308a8f1a19dSHoratiu Vultur 	phy_write_mmd(phydev, MDIO_MMD_PMAPMD,
3309a8f1a19dSHoratiu Vultur 		      LAN8841_ADC_CHANNEL_MASK, 0x0);
3310a8f1a19dSHoratiu Vultur 	phy_write_mmd(phydev, LAN8841_MMD_TIMER_REG,
3311a8f1a19dSHoratiu Vultur 		      LAN8841_MMD0_REGISTER_17,
3312a8f1a19dSHoratiu Vultur 		      LAN8841_MMD0_REGISTER_17_DROP_OPT(2) |
3313a8f1a19dSHoratiu Vultur 		      LAN8841_MMD0_REGISTER_17_XMIT_TOG_TX_DIS);
3314a8f1a19dSHoratiu Vultur 
3315a8f1a19dSHoratiu Vultur 	return 0;
3316a8f1a19dSHoratiu Vultur }
3317a8f1a19dSHoratiu Vultur 
3318a8f1a19dSHoratiu Vultur #define LAN8841_OUTPUT_CTRL			25
3319a8f1a19dSHoratiu Vultur #define LAN8841_OUTPUT_CTRL_INT_BUFFER		BIT(14)
3320cafc3662SHoratiu Vultur #define LAN8841_INT_PTP				BIT(9)
3321a8f1a19dSHoratiu Vultur 
3322a8f1a19dSHoratiu Vultur static int lan8841_config_intr(struct phy_device *phydev)
3323a8f1a19dSHoratiu Vultur {
3324a8f1a19dSHoratiu Vultur 	int err;
3325a8f1a19dSHoratiu Vultur 
3326a8f1a19dSHoratiu Vultur 	phy_modify(phydev, LAN8841_OUTPUT_CTRL,
3327a8f1a19dSHoratiu Vultur 		   LAN8841_OUTPUT_CTRL_INT_BUFFER, 0);
3328a8f1a19dSHoratiu Vultur 
3329a8f1a19dSHoratiu Vultur 	if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
3330a8f1a19dSHoratiu Vultur 		err = phy_read(phydev, LAN8814_INTS);
3331a8f1a19dSHoratiu Vultur 		if (err)
3332a8f1a19dSHoratiu Vultur 			return err;
3333a8f1a19dSHoratiu Vultur 
3334cafc3662SHoratiu Vultur 		/* Enable / disable interrupts. It is OK to enable PTP interrupt
3335cafc3662SHoratiu Vultur 		 * even if it PTP is not enabled. Because the underneath blocks
3336cafc3662SHoratiu Vultur 		 * will not enable the PTP so we will never get the PTP
3337cafc3662SHoratiu Vultur 		 * interrupt.
3338cafc3662SHoratiu Vultur 		 */
3339a8f1a19dSHoratiu Vultur 		err = phy_write(phydev, LAN8814_INTC,
3340cafc3662SHoratiu Vultur 				LAN8814_INT_LINK | LAN8841_INT_PTP);
3341a8f1a19dSHoratiu Vultur 	} else {
3342a8f1a19dSHoratiu Vultur 		err = phy_write(phydev, LAN8814_INTC, 0);
3343a8f1a19dSHoratiu Vultur 		if (err)
3344a8f1a19dSHoratiu Vultur 			return err;
3345a8f1a19dSHoratiu Vultur 
3346a8f1a19dSHoratiu Vultur 		err = phy_read(phydev, LAN8814_INTS);
3347a8f1a19dSHoratiu Vultur 	}
3348a8f1a19dSHoratiu Vultur 
3349a8f1a19dSHoratiu Vultur 	return err;
3350a8f1a19dSHoratiu Vultur }
3351a8f1a19dSHoratiu Vultur 
3352cafc3662SHoratiu Vultur #define LAN8841_PTP_TX_EGRESS_SEC_LO			453
3353cafc3662SHoratiu Vultur #define LAN8841_PTP_TX_EGRESS_SEC_HI			452
3354cafc3662SHoratiu Vultur #define LAN8841_PTP_TX_EGRESS_NS_LO			451
3355cafc3662SHoratiu Vultur #define LAN8841_PTP_TX_EGRESS_NS_HI			450
3356cafc3662SHoratiu Vultur #define LAN8841_PTP_TX_EGRESS_NSEC_HI_VALID		BIT(15)
3357cafc3662SHoratiu Vultur #define LAN8841_PTP_TX_MSG_HEADER2			455
3358cafc3662SHoratiu Vultur 
3359cafc3662SHoratiu Vultur static bool lan8841_ptp_get_tx_ts(struct kszphy_ptp_priv *ptp_priv,
3360cafc3662SHoratiu Vultur 				  u32 *sec, u32 *nsec, u16 *seq)
3361cafc3662SHoratiu Vultur {
3362cafc3662SHoratiu Vultur 	struct phy_device *phydev = ptp_priv->phydev;
3363cafc3662SHoratiu Vultur 
3364cafc3662SHoratiu Vultur 	*nsec = phy_read_mmd(phydev, 2, LAN8841_PTP_TX_EGRESS_NS_HI);
3365cafc3662SHoratiu Vultur 	if (!(*nsec & LAN8841_PTP_TX_EGRESS_NSEC_HI_VALID))
3366cafc3662SHoratiu Vultur 		return false;
3367cafc3662SHoratiu Vultur 
3368cafc3662SHoratiu Vultur 	*nsec = ((*nsec & 0x3fff) << 16);
3369cafc3662SHoratiu Vultur 	*nsec = *nsec | phy_read_mmd(phydev, 2, LAN8841_PTP_TX_EGRESS_NS_LO);
3370cafc3662SHoratiu Vultur 
3371cafc3662SHoratiu Vultur 	*sec = phy_read_mmd(phydev, 2, LAN8841_PTP_TX_EGRESS_SEC_HI);
3372cafc3662SHoratiu Vultur 	*sec = *sec << 16;
3373cafc3662SHoratiu Vultur 	*sec = *sec | phy_read_mmd(phydev, 2, LAN8841_PTP_TX_EGRESS_SEC_LO);
3374cafc3662SHoratiu Vultur 
3375cafc3662SHoratiu Vultur 	*seq = phy_read_mmd(phydev, 2, LAN8841_PTP_TX_MSG_HEADER2);
3376cafc3662SHoratiu Vultur 
3377cafc3662SHoratiu Vultur 	return true;
3378cafc3662SHoratiu Vultur }
3379cafc3662SHoratiu Vultur 
3380cafc3662SHoratiu Vultur static void lan8841_ptp_process_tx_ts(struct kszphy_ptp_priv *ptp_priv)
3381cafc3662SHoratiu Vultur {
3382cafc3662SHoratiu Vultur 	u32 sec, nsec;
3383cafc3662SHoratiu Vultur 	u16 seq;
3384cafc3662SHoratiu Vultur 
3385cafc3662SHoratiu Vultur 	while (lan8841_ptp_get_tx_ts(ptp_priv, &sec, &nsec, &seq))
3386cafc3662SHoratiu Vultur 		lan8814_match_tx_skb(ptp_priv, sec, nsec, seq);
3387cafc3662SHoratiu Vultur }
3388cafc3662SHoratiu Vultur 
3389cafc3662SHoratiu Vultur #define LAN8841_PTP_RX_INGRESS_SEC_LO			389
3390cafc3662SHoratiu Vultur #define LAN8841_PTP_RX_INGRESS_SEC_HI			388
3391cafc3662SHoratiu Vultur #define LAN8841_PTP_RX_INGRESS_NS_LO			387
3392cafc3662SHoratiu Vultur #define LAN8841_PTP_RX_INGRESS_NS_HI			386
3393cafc3662SHoratiu Vultur #define LAN8841_PTP_RX_INGRESS_NSEC_HI_VALID		BIT(15)
3394cafc3662SHoratiu Vultur #define LAN8841_PTP_RX_MSG_HEADER2			391
3395cafc3662SHoratiu Vultur 
3396cafc3662SHoratiu Vultur static struct lan8814_ptp_rx_ts *lan8841_ptp_get_rx_ts(struct kszphy_ptp_priv *ptp_priv)
3397cafc3662SHoratiu Vultur {
3398cafc3662SHoratiu Vultur 	struct phy_device *phydev = ptp_priv->phydev;
3399cafc3662SHoratiu Vultur 	struct lan8814_ptp_rx_ts *rx_ts;
3400cafc3662SHoratiu Vultur 	u32 sec, nsec;
3401cafc3662SHoratiu Vultur 	u16 seq;
3402cafc3662SHoratiu Vultur 
3403cafc3662SHoratiu Vultur 	nsec = phy_read_mmd(phydev, 2, LAN8841_PTP_RX_INGRESS_NS_HI);
3404cafc3662SHoratiu Vultur 	if (!(nsec & LAN8841_PTP_RX_INGRESS_NSEC_HI_VALID))
3405cafc3662SHoratiu Vultur 		return NULL;
3406cafc3662SHoratiu Vultur 
3407cafc3662SHoratiu Vultur 	nsec = ((nsec & 0x3fff) << 16);
3408cafc3662SHoratiu Vultur 	nsec = nsec | phy_read_mmd(phydev, 2, LAN8841_PTP_RX_INGRESS_NS_LO);
3409cafc3662SHoratiu Vultur 
3410cafc3662SHoratiu Vultur 	sec = phy_read_mmd(phydev, 2, LAN8841_PTP_RX_INGRESS_SEC_HI);
3411cafc3662SHoratiu Vultur 	sec = sec << 16;
3412cafc3662SHoratiu Vultur 	sec = sec | phy_read_mmd(phydev, 2, LAN8841_PTP_RX_INGRESS_SEC_LO);
3413cafc3662SHoratiu Vultur 
3414cafc3662SHoratiu Vultur 	seq = phy_read_mmd(phydev, 2, LAN8841_PTP_RX_MSG_HEADER2);
3415cafc3662SHoratiu Vultur 
3416cafc3662SHoratiu Vultur 	rx_ts = kzalloc(sizeof(*rx_ts), GFP_KERNEL);
3417cafc3662SHoratiu Vultur 	if (!rx_ts)
3418cafc3662SHoratiu Vultur 		return NULL;
3419cafc3662SHoratiu Vultur 
3420cafc3662SHoratiu Vultur 	rx_ts->seconds = sec;
3421cafc3662SHoratiu Vultur 	rx_ts->nsec = nsec;
3422cafc3662SHoratiu Vultur 	rx_ts->seq_id = seq;
3423cafc3662SHoratiu Vultur 
3424cafc3662SHoratiu Vultur 	return rx_ts;
3425cafc3662SHoratiu Vultur }
3426cafc3662SHoratiu Vultur 
3427cafc3662SHoratiu Vultur static void lan8841_ptp_process_rx_ts(struct kszphy_ptp_priv *ptp_priv)
3428cafc3662SHoratiu Vultur {
3429cafc3662SHoratiu Vultur 	struct lan8814_ptp_rx_ts *rx_ts;
3430cafc3662SHoratiu Vultur 
3431cafc3662SHoratiu Vultur 	while ((rx_ts = lan8841_ptp_get_rx_ts(ptp_priv)) != NULL)
3432cafc3662SHoratiu Vultur 		lan8814_match_rx_ts(ptp_priv, rx_ts);
3433cafc3662SHoratiu Vultur }
3434cafc3662SHoratiu Vultur 
3435cafc3662SHoratiu Vultur #define LAN8841_PTP_INT_STS			259
3436cafc3662SHoratiu Vultur #define LAN8841_PTP_INT_STS_PTP_TX_TS_OVRFL_INT	BIT(13)
3437cafc3662SHoratiu Vultur #define LAN8841_PTP_INT_STS_PTP_TX_TS_INT	BIT(12)
3438cafc3662SHoratiu Vultur #define LAN8841_PTP_INT_STS_PTP_RX_TS_OVRFL_INT	BIT(9)
3439cafc3662SHoratiu Vultur #define LAN8841_PTP_INT_STS_PTP_RX_TS_INT	BIT(8)
3440fac63186SHoratiu Vultur #define LAN8841_PTP_INT_STS_PTP_GPIO_CAP_INT	BIT(2)
3441cafc3662SHoratiu Vultur 
3442cafc3662SHoratiu Vultur static void lan8841_ptp_flush_fifo(struct kszphy_ptp_priv *ptp_priv, bool egress)
3443cafc3662SHoratiu Vultur {
3444cafc3662SHoratiu Vultur 	struct phy_device *phydev = ptp_priv->phydev;
3445cafc3662SHoratiu Vultur 	int i;
3446cafc3662SHoratiu Vultur 
3447cafc3662SHoratiu Vultur 	for (i = 0; i < FIFO_SIZE; ++i)
3448cafc3662SHoratiu Vultur 		phy_read_mmd(phydev, 2,
3449cafc3662SHoratiu Vultur 			     egress ? LAN8841_PTP_TX_MSG_HEADER2 :
3450cafc3662SHoratiu Vultur 				      LAN8841_PTP_RX_MSG_HEADER2);
3451cafc3662SHoratiu Vultur 
3452cafc3662SHoratiu Vultur 	phy_read_mmd(phydev, 2, LAN8841_PTP_INT_STS);
3453cafc3662SHoratiu Vultur }
3454cafc3662SHoratiu Vultur 
3455fac63186SHoratiu Vultur #define LAN8841_PTP_GPIO_CAP_STS			506
3456fac63186SHoratiu Vultur #define LAN8841_PTP_GPIO_SEL				327
3457fac63186SHoratiu Vultur #define LAN8841_PTP_GPIO_SEL_GPIO_SEL(gpio)		((gpio) << 8)
3458fac63186SHoratiu Vultur #define LAN8841_PTP_GPIO_RE_LTC_SEC_HI_CAP		498
3459fac63186SHoratiu Vultur #define LAN8841_PTP_GPIO_RE_LTC_SEC_LO_CAP		499
3460fac63186SHoratiu Vultur #define LAN8841_PTP_GPIO_RE_LTC_NS_HI_CAP		500
3461fac63186SHoratiu Vultur #define LAN8841_PTP_GPIO_RE_LTC_NS_LO_CAP		501
3462fac63186SHoratiu Vultur #define LAN8841_PTP_GPIO_FE_LTC_SEC_HI_CAP		502
3463fac63186SHoratiu Vultur #define LAN8841_PTP_GPIO_FE_LTC_SEC_LO_CAP		503
3464fac63186SHoratiu Vultur #define LAN8841_PTP_GPIO_FE_LTC_NS_HI_CAP		504
3465fac63186SHoratiu Vultur #define LAN8841_PTP_GPIO_FE_LTC_NS_LO_CAP		505
3466fac63186SHoratiu Vultur 
3467fac63186SHoratiu Vultur static void lan8841_gpio_process_cap(struct kszphy_ptp_priv *ptp_priv)
3468fac63186SHoratiu Vultur {
3469fac63186SHoratiu Vultur 	struct phy_device *phydev = ptp_priv->phydev;
3470fac63186SHoratiu Vultur 	struct ptp_clock_event ptp_event = {0};
3471fac63186SHoratiu Vultur 	int pin, ret, tmp;
3472fac63186SHoratiu Vultur 	s32 sec, nsec;
3473fac63186SHoratiu Vultur 
3474fac63186SHoratiu Vultur 	pin = ptp_find_pin_unlocked(ptp_priv->ptp_clock, PTP_PF_EXTTS, 0);
3475fac63186SHoratiu Vultur 	if (pin == -1)
3476fac63186SHoratiu Vultur 		return;
3477fac63186SHoratiu Vultur 
3478fac63186SHoratiu Vultur 	tmp = phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_CAP_STS);
3479fac63186SHoratiu Vultur 	if (tmp < 0)
3480fac63186SHoratiu Vultur 		return;
3481fac63186SHoratiu Vultur 
3482fac63186SHoratiu Vultur 	ret = phy_write_mmd(phydev, 2, LAN8841_PTP_GPIO_SEL,
3483fac63186SHoratiu Vultur 			    LAN8841_PTP_GPIO_SEL_GPIO_SEL(pin));
3484fac63186SHoratiu Vultur 	if (ret)
3485fac63186SHoratiu Vultur 		return;
3486fac63186SHoratiu Vultur 
3487fac63186SHoratiu Vultur 	mutex_lock(&ptp_priv->ptp_lock);
3488fac63186SHoratiu Vultur 	if (tmp & BIT(pin)) {
3489fac63186SHoratiu Vultur 		sec = phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_RE_LTC_SEC_HI_CAP);
3490fac63186SHoratiu Vultur 		sec <<= 16;
3491fac63186SHoratiu Vultur 		sec |= phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_RE_LTC_SEC_LO_CAP);
3492fac63186SHoratiu Vultur 
3493fac63186SHoratiu Vultur 		nsec = phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_RE_LTC_NS_HI_CAP) & 0x3fff;
3494fac63186SHoratiu Vultur 		nsec <<= 16;
3495fac63186SHoratiu Vultur 		nsec |= phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_RE_LTC_NS_LO_CAP);
3496fac63186SHoratiu Vultur 	} else {
3497fac63186SHoratiu Vultur 		sec = phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_FE_LTC_SEC_HI_CAP);
3498fac63186SHoratiu Vultur 		sec <<= 16;
3499fac63186SHoratiu Vultur 		sec |= phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_FE_LTC_SEC_LO_CAP);
3500fac63186SHoratiu Vultur 
3501fac63186SHoratiu Vultur 		nsec = phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_FE_LTC_NS_HI_CAP) & 0x3fff;
3502fac63186SHoratiu Vultur 		nsec <<= 16;
3503fac63186SHoratiu Vultur 		nsec |= phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_FE_LTC_NS_LO_CAP);
3504fac63186SHoratiu Vultur 	}
3505fac63186SHoratiu Vultur 	mutex_unlock(&ptp_priv->ptp_lock);
3506fac63186SHoratiu Vultur 	ret = phy_write_mmd(phydev, 2, LAN8841_PTP_GPIO_SEL, 0);
3507fac63186SHoratiu Vultur 	if (ret)
3508fac63186SHoratiu Vultur 		return;
3509fac63186SHoratiu Vultur 
3510fac63186SHoratiu Vultur 	ptp_event.index = 0;
3511fac63186SHoratiu Vultur 	ptp_event.timestamp = ktime_set(sec, nsec);
3512fac63186SHoratiu Vultur 	ptp_event.type = PTP_CLOCK_EXTTS;
3513fac63186SHoratiu Vultur 	ptp_clock_event(ptp_priv->ptp_clock, &ptp_event);
3514fac63186SHoratiu Vultur }
3515fac63186SHoratiu Vultur 
3516cafc3662SHoratiu Vultur static void lan8841_handle_ptp_interrupt(struct phy_device *phydev)
3517cafc3662SHoratiu Vultur {
3518cafc3662SHoratiu Vultur 	struct kszphy_priv *priv = phydev->priv;
3519cafc3662SHoratiu Vultur 	struct kszphy_ptp_priv *ptp_priv = &priv->ptp_priv;
3520cafc3662SHoratiu Vultur 	u16 status;
3521cafc3662SHoratiu Vultur 
3522cafc3662SHoratiu Vultur 	do {
3523cafc3662SHoratiu Vultur 		status = phy_read_mmd(phydev, 2, LAN8841_PTP_INT_STS);
3524fac63186SHoratiu Vultur 
3525cafc3662SHoratiu Vultur 		if (status & LAN8841_PTP_INT_STS_PTP_TX_TS_INT)
3526cafc3662SHoratiu Vultur 			lan8841_ptp_process_tx_ts(ptp_priv);
3527cafc3662SHoratiu Vultur 
3528cafc3662SHoratiu Vultur 		if (status & LAN8841_PTP_INT_STS_PTP_RX_TS_INT)
3529cafc3662SHoratiu Vultur 			lan8841_ptp_process_rx_ts(ptp_priv);
3530cafc3662SHoratiu Vultur 
3531fac63186SHoratiu Vultur 		if (status & LAN8841_PTP_INT_STS_PTP_GPIO_CAP_INT)
3532fac63186SHoratiu Vultur 			lan8841_gpio_process_cap(ptp_priv);
3533fac63186SHoratiu Vultur 
3534cafc3662SHoratiu Vultur 		if (status & LAN8841_PTP_INT_STS_PTP_TX_TS_OVRFL_INT) {
3535cafc3662SHoratiu Vultur 			lan8841_ptp_flush_fifo(ptp_priv, true);
3536cafc3662SHoratiu Vultur 			skb_queue_purge(&ptp_priv->tx_queue);
3537cafc3662SHoratiu Vultur 		}
3538cafc3662SHoratiu Vultur 
3539cafc3662SHoratiu Vultur 		if (status & LAN8841_PTP_INT_STS_PTP_RX_TS_OVRFL_INT) {
3540cafc3662SHoratiu Vultur 			lan8841_ptp_flush_fifo(ptp_priv, false);
3541cafc3662SHoratiu Vultur 			skb_queue_purge(&ptp_priv->rx_queue);
3542cafc3662SHoratiu Vultur 		}
3543cafc3662SHoratiu Vultur 
3544cafc3662SHoratiu Vultur 	} while (status);
3545cafc3662SHoratiu Vultur }
3546cafc3662SHoratiu Vultur 
3547cafc3662SHoratiu Vultur #define LAN8841_INTS_PTP		BIT(9)
3548cafc3662SHoratiu Vultur 
3549a8f1a19dSHoratiu Vultur static irqreturn_t lan8841_handle_interrupt(struct phy_device *phydev)
3550a8f1a19dSHoratiu Vultur {
3551cafc3662SHoratiu Vultur 	irqreturn_t ret = IRQ_NONE;
3552a8f1a19dSHoratiu Vultur 	int irq_status;
3553a8f1a19dSHoratiu Vultur 
3554a8f1a19dSHoratiu Vultur 	irq_status = phy_read(phydev, LAN8814_INTS);
3555a8f1a19dSHoratiu Vultur 	if (irq_status < 0) {
3556a8f1a19dSHoratiu Vultur 		phy_error(phydev);
3557a8f1a19dSHoratiu Vultur 		return IRQ_NONE;
3558a8f1a19dSHoratiu Vultur 	}
3559a8f1a19dSHoratiu Vultur 
3560a8f1a19dSHoratiu Vultur 	if (irq_status & LAN8814_INT_LINK) {
3561a8f1a19dSHoratiu Vultur 		phy_trigger_machine(phydev);
3562cafc3662SHoratiu Vultur 		ret = IRQ_HANDLED;
3563a8f1a19dSHoratiu Vultur 	}
3564a8f1a19dSHoratiu Vultur 
3565cafc3662SHoratiu Vultur 	if (irq_status & LAN8841_INTS_PTP) {
3566cafc3662SHoratiu Vultur 		lan8841_handle_ptp_interrupt(phydev);
3567cafc3662SHoratiu Vultur 		ret = IRQ_HANDLED;
3568a8f1a19dSHoratiu Vultur 	}
3569a8f1a19dSHoratiu Vultur 
3570cafc3662SHoratiu Vultur 	return ret;
3571cafc3662SHoratiu Vultur }
3572cafc3662SHoratiu Vultur 
3573cafc3662SHoratiu Vultur static int lan8841_ts_info(struct mii_timestamper *mii_ts,
3574cafc3662SHoratiu Vultur 			   struct ethtool_ts_info *info)
3575cafc3662SHoratiu Vultur {
3576cafc3662SHoratiu Vultur 	struct kszphy_ptp_priv *ptp_priv;
3577cafc3662SHoratiu Vultur 
3578cafc3662SHoratiu Vultur 	ptp_priv = container_of(mii_ts, struct kszphy_ptp_priv, mii_ts);
3579cafc3662SHoratiu Vultur 
3580cafc3662SHoratiu Vultur 	info->phc_index = ptp_priv->ptp_clock ?
3581cafc3662SHoratiu Vultur 				ptp_clock_index(ptp_priv->ptp_clock) : -1;
3582cafc3662SHoratiu Vultur 	if (info->phc_index == -1) {
3583cafc3662SHoratiu Vultur 		info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
3584cafc3662SHoratiu Vultur 					 SOF_TIMESTAMPING_RX_SOFTWARE |
3585cafc3662SHoratiu Vultur 					 SOF_TIMESTAMPING_SOFTWARE;
3586cafc3662SHoratiu Vultur 		return 0;
3587cafc3662SHoratiu Vultur 	}
3588cafc3662SHoratiu Vultur 
3589cafc3662SHoratiu Vultur 	info->so_timestamping = SOF_TIMESTAMPING_TX_HARDWARE |
3590cafc3662SHoratiu Vultur 				SOF_TIMESTAMPING_RX_HARDWARE |
3591cafc3662SHoratiu Vultur 				SOF_TIMESTAMPING_RAW_HARDWARE;
3592cafc3662SHoratiu Vultur 
3593cafc3662SHoratiu Vultur 	info->tx_types = (1 << HWTSTAMP_TX_OFF) |
3594cafc3662SHoratiu Vultur 			 (1 << HWTSTAMP_TX_ON) |
3595cafc3662SHoratiu Vultur 			 (1 << HWTSTAMP_TX_ONESTEP_SYNC);
3596cafc3662SHoratiu Vultur 
3597cafc3662SHoratiu Vultur 	info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
3598cafc3662SHoratiu Vultur 			   (1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT) |
3599cafc3662SHoratiu Vultur 			   (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
3600cafc3662SHoratiu Vultur 			   (1 << HWTSTAMP_FILTER_PTP_V2_EVENT);
3601cafc3662SHoratiu Vultur 
3602cafc3662SHoratiu Vultur 	return 0;
3603cafc3662SHoratiu Vultur }
3604cafc3662SHoratiu Vultur 
3605cafc3662SHoratiu Vultur #define LAN8841_PTP_INT_EN			260
3606cafc3662SHoratiu Vultur #define LAN8841_PTP_INT_EN_PTP_TX_TS_OVRFL_EN	BIT(13)
3607cafc3662SHoratiu Vultur #define LAN8841_PTP_INT_EN_PTP_TX_TS_EN		BIT(12)
3608cafc3662SHoratiu Vultur #define LAN8841_PTP_INT_EN_PTP_RX_TS_OVRFL_EN	BIT(9)
3609cafc3662SHoratiu Vultur #define LAN8841_PTP_INT_EN_PTP_RX_TS_EN		BIT(8)
3610cafc3662SHoratiu Vultur 
3611cafc3662SHoratiu Vultur static void lan8841_ptp_enable_int(struct kszphy_ptp_priv *ptp_priv,
3612cafc3662SHoratiu Vultur 				   bool enable)
3613cafc3662SHoratiu Vultur {
3614cafc3662SHoratiu Vultur 	struct phy_device *phydev = ptp_priv->phydev;
3615cafc3662SHoratiu Vultur 
3616cafc3662SHoratiu Vultur 	if (enable)
3617cafc3662SHoratiu Vultur 		/* Enable interrupts */
3618cafc3662SHoratiu Vultur 		phy_modify_mmd(phydev, 2, LAN8841_PTP_INT_EN,
3619cafc3662SHoratiu Vultur 			       LAN8841_PTP_INT_EN_PTP_TX_TS_OVRFL_EN |
3620cafc3662SHoratiu Vultur 			       LAN8841_PTP_INT_EN_PTP_RX_TS_OVRFL_EN |
3621cafc3662SHoratiu Vultur 			       LAN8841_PTP_INT_EN_PTP_TX_TS_EN |
3622cafc3662SHoratiu Vultur 			       LAN8841_PTP_INT_EN_PTP_RX_TS_EN,
3623cafc3662SHoratiu Vultur 			       LAN8841_PTP_INT_EN_PTP_TX_TS_OVRFL_EN |
3624cafc3662SHoratiu Vultur 			       LAN8841_PTP_INT_EN_PTP_RX_TS_OVRFL_EN |
3625cafc3662SHoratiu Vultur 			       LAN8841_PTP_INT_EN_PTP_TX_TS_EN |
3626cafc3662SHoratiu Vultur 			       LAN8841_PTP_INT_EN_PTP_RX_TS_EN);
3627cafc3662SHoratiu Vultur 	else
3628cafc3662SHoratiu Vultur 		/* Disable interrupts */
3629cafc3662SHoratiu Vultur 		phy_modify_mmd(phydev, 2, LAN8841_PTP_INT_EN,
3630cafc3662SHoratiu Vultur 			       LAN8841_PTP_INT_EN_PTP_TX_TS_OVRFL_EN |
3631cafc3662SHoratiu Vultur 			       LAN8841_PTP_INT_EN_PTP_RX_TS_OVRFL_EN |
3632cafc3662SHoratiu Vultur 			       LAN8841_PTP_INT_EN_PTP_TX_TS_EN |
3633cafc3662SHoratiu Vultur 			       LAN8841_PTP_INT_EN_PTP_RX_TS_EN, 0);
3634cafc3662SHoratiu Vultur }
3635cafc3662SHoratiu Vultur 
3636cafc3662SHoratiu Vultur #define LAN8841_PTP_RX_TIMESTAMP_EN		379
3637cafc3662SHoratiu Vultur #define LAN8841_PTP_TX_TIMESTAMP_EN		443
3638cafc3662SHoratiu Vultur #define LAN8841_PTP_TX_MOD			445
3639cafc3662SHoratiu Vultur 
3640cafc3662SHoratiu Vultur static int lan8841_hwtstamp(struct mii_timestamper *mii_ts, struct ifreq *ifr)
3641cafc3662SHoratiu Vultur {
3642cafc3662SHoratiu Vultur 	struct kszphy_ptp_priv *ptp_priv = container_of(mii_ts, struct kszphy_ptp_priv, mii_ts);
3643cafc3662SHoratiu Vultur 	struct phy_device *phydev = ptp_priv->phydev;
3644cafc3662SHoratiu Vultur 	struct lan8814_ptp_rx_ts *rx_ts, *tmp;
3645cafc3662SHoratiu Vultur 	struct hwtstamp_config config;
3646cafc3662SHoratiu Vultur 	int txcfg = 0, rxcfg = 0;
3647cafc3662SHoratiu Vultur 	int pkt_ts_enable;
3648cafc3662SHoratiu Vultur 
3649cafc3662SHoratiu Vultur 	if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
3650cafc3662SHoratiu Vultur 		return -EFAULT;
3651cafc3662SHoratiu Vultur 
3652cafc3662SHoratiu Vultur 	ptp_priv->hwts_tx_type = config.tx_type;
3653cafc3662SHoratiu Vultur 	ptp_priv->rx_filter = config.rx_filter;
3654cafc3662SHoratiu Vultur 
3655cafc3662SHoratiu Vultur 	switch (config.rx_filter) {
3656cafc3662SHoratiu Vultur 	case HWTSTAMP_FILTER_NONE:
3657cafc3662SHoratiu Vultur 		ptp_priv->layer = 0;
3658cafc3662SHoratiu Vultur 		ptp_priv->version = 0;
3659cafc3662SHoratiu Vultur 		break;
3660cafc3662SHoratiu Vultur 	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
3661cafc3662SHoratiu Vultur 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
3662cafc3662SHoratiu Vultur 	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
3663cafc3662SHoratiu Vultur 		ptp_priv->layer = PTP_CLASS_L4;
3664cafc3662SHoratiu Vultur 		ptp_priv->version = PTP_CLASS_V2;
3665cafc3662SHoratiu Vultur 		break;
3666cafc3662SHoratiu Vultur 	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
3667cafc3662SHoratiu Vultur 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
3668cafc3662SHoratiu Vultur 	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
3669cafc3662SHoratiu Vultur 		ptp_priv->layer = PTP_CLASS_L2;
3670cafc3662SHoratiu Vultur 		ptp_priv->version = PTP_CLASS_V2;
3671cafc3662SHoratiu Vultur 		break;
3672cafc3662SHoratiu Vultur 	case HWTSTAMP_FILTER_PTP_V2_EVENT:
3673cafc3662SHoratiu Vultur 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
3674cafc3662SHoratiu Vultur 	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
3675cafc3662SHoratiu Vultur 		ptp_priv->layer = PTP_CLASS_L4 | PTP_CLASS_L2;
3676cafc3662SHoratiu Vultur 		ptp_priv->version = PTP_CLASS_V2;
3677cafc3662SHoratiu Vultur 		break;
3678cafc3662SHoratiu Vultur 	default:
3679cafc3662SHoratiu Vultur 		return -ERANGE;
3680cafc3662SHoratiu Vultur 	}
3681cafc3662SHoratiu Vultur 
3682cafc3662SHoratiu Vultur 	/* Setup parsing of the frames and enable the timestamping for ptp
3683cafc3662SHoratiu Vultur 	 * frames
3684cafc3662SHoratiu Vultur 	 */
3685cafc3662SHoratiu Vultur 	if (ptp_priv->layer & PTP_CLASS_L2) {
3686cafc3662SHoratiu Vultur 		rxcfg |= PTP_RX_PARSE_CONFIG_LAYER2_EN_;
3687cafc3662SHoratiu Vultur 		txcfg |= PTP_TX_PARSE_CONFIG_LAYER2_EN_;
3688cafc3662SHoratiu Vultur 	} else if (ptp_priv->layer & PTP_CLASS_L4) {
3689cafc3662SHoratiu Vultur 		rxcfg |= PTP_RX_PARSE_CONFIG_IPV4_EN_ | PTP_RX_PARSE_CONFIG_IPV6_EN_;
3690cafc3662SHoratiu Vultur 		txcfg |= PTP_TX_PARSE_CONFIG_IPV4_EN_ | PTP_TX_PARSE_CONFIG_IPV6_EN_;
3691cafc3662SHoratiu Vultur 	}
3692cafc3662SHoratiu Vultur 
3693cafc3662SHoratiu Vultur 	phy_write_mmd(phydev, 2, LAN8841_PTP_RX_PARSE_CONFIG, rxcfg);
3694cafc3662SHoratiu Vultur 	phy_write_mmd(phydev, 2, LAN8841_PTP_TX_PARSE_CONFIG, txcfg);
3695cafc3662SHoratiu Vultur 
3696cafc3662SHoratiu Vultur 	pkt_ts_enable = PTP_TIMESTAMP_EN_SYNC_ | PTP_TIMESTAMP_EN_DREQ_ |
3697cafc3662SHoratiu Vultur 			PTP_TIMESTAMP_EN_PDREQ_ | PTP_TIMESTAMP_EN_PDRES_;
3698cafc3662SHoratiu Vultur 	phy_write_mmd(phydev, 2, LAN8841_PTP_RX_TIMESTAMP_EN, pkt_ts_enable);
3699cafc3662SHoratiu Vultur 	phy_write_mmd(phydev, 2, LAN8841_PTP_TX_TIMESTAMP_EN, pkt_ts_enable);
3700cafc3662SHoratiu Vultur 
3701cafc3662SHoratiu Vultur 	/* Enable / disable of the TX timestamp in the SYNC frames */
3702cafc3662SHoratiu Vultur 	phy_modify_mmd(phydev, 2, LAN8841_PTP_TX_MOD,
3703cafc3662SHoratiu Vultur 		       PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_,
3704cafc3662SHoratiu Vultur 		       ptp_priv->hwts_tx_type == HWTSTAMP_TX_ONESTEP_SYNC ?
3705cafc3662SHoratiu Vultur 				PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_ : 0);
3706cafc3662SHoratiu Vultur 
3707cafc3662SHoratiu Vultur 	/* Now enable/disable the timestamping */
3708cafc3662SHoratiu Vultur 	lan8841_ptp_enable_int(ptp_priv,
3709cafc3662SHoratiu Vultur 			       config.rx_filter != HWTSTAMP_FILTER_NONE);
3710cafc3662SHoratiu Vultur 
3711cafc3662SHoratiu Vultur 	/* In case of multiple starts and stops, these needs to be cleared */
3712cafc3662SHoratiu Vultur 	list_for_each_entry_safe(rx_ts, tmp, &ptp_priv->rx_ts_list, list) {
3713cafc3662SHoratiu Vultur 		list_del(&rx_ts->list);
3714cafc3662SHoratiu Vultur 		kfree(rx_ts);
3715cafc3662SHoratiu Vultur 	}
3716cafc3662SHoratiu Vultur 
3717cafc3662SHoratiu Vultur 	skb_queue_purge(&ptp_priv->rx_queue);
3718cafc3662SHoratiu Vultur 	skb_queue_purge(&ptp_priv->tx_queue);
3719cafc3662SHoratiu Vultur 
3720cafc3662SHoratiu Vultur 	lan8841_ptp_flush_fifo(ptp_priv, false);
3721cafc3662SHoratiu Vultur 	lan8841_ptp_flush_fifo(ptp_priv, true);
3722cafc3662SHoratiu Vultur 
3723cafc3662SHoratiu Vultur 	return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ? -EFAULT : 0;
3724cafc3662SHoratiu Vultur }
3725cafc3662SHoratiu Vultur 
3726e4ed8ba0SHoratiu Vultur #define LAN8841_EVENT_A		0
3727e4ed8ba0SHoratiu Vultur #define LAN8841_EVENT_B		1
3728e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_LTC_TARGET_SEC_HI(event)	((event) == LAN8841_EVENT_A ? 278 : 288)
3729e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_LTC_TARGET_SEC_LO(event)	((event) == LAN8841_EVENT_A ? 279 : 289)
3730e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_LTC_TARGET_NS_HI(event)	((event) == LAN8841_EVENT_A ? 280 : 290)
3731e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_LTC_TARGET_NS_LO(event)	((event) == LAN8841_EVENT_A ? 281 : 291)
3732e4ed8ba0SHoratiu Vultur 
3733e4ed8ba0SHoratiu Vultur static int lan8841_ptp_set_target(struct kszphy_ptp_priv *ptp_priv, u8 event,
3734e4ed8ba0SHoratiu Vultur 				  s64 sec, u32 nsec)
3735e4ed8ba0SHoratiu Vultur {
3736e4ed8ba0SHoratiu Vultur 	struct phy_device *phydev = ptp_priv->phydev;
3737e4ed8ba0SHoratiu Vultur 	int ret;
3738e4ed8ba0SHoratiu Vultur 
3739e4ed8ba0SHoratiu Vultur 	ret = phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_TARGET_SEC_HI(event),
3740e4ed8ba0SHoratiu Vultur 			    upper_16_bits(sec));
3741e4ed8ba0SHoratiu Vultur 	if (ret)
3742e4ed8ba0SHoratiu Vultur 		return ret;
3743e4ed8ba0SHoratiu Vultur 
3744e4ed8ba0SHoratiu Vultur 	ret = phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_TARGET_SEC_LO(event),
3745e4ed8ba0SHoratiu Vultur 			    lower_16_bits(sec));
3746e4ed8ba0SHoratiu Vultur 	if (ret)
3747e4ed8ba0SHoratiu Vultur 		return ret;
3748e4ed8ba0SHoratiu Vultur 
3749e4ed8ba0SHoratiu Vultur 	ret = phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_TARGET_NS_HI(event) & 0x3fff,
3750e4ed8ba0SHoratiu Vultur 			    upper_16_bits(nsec));
3751e4ed8ba0SHoratiu Vultur 	if (ret)
3752e4ed8ba0SHoratiu Vultur 		return ret;
3753e4ed8ba0SHoratiu Vultur 
3754e4ed8ba0SHoratiu Vultur 	return phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_TARGET_NS_LO(event),
3755e4ed8ba0SHoratiu Vultur 			    lower_16_bits(nsec));
3756e4ed8ba0SHoratiu Vultur }
3757e4ed8ba0SHoratiu Vultur 
3758e4ed8ba0SHoratiu Vultur #define LAN8841_BUFFER_TIME	2
3759e4ed8ba0SHoratiu Vultur 
3760e4ed8ba0SHoratiu Vultur static int lan8841_ptp_update_target(struct kszphy_ptp_priv *ptp_priv,
3761e4ed8ba0SHoratiu Vultur 				     const struct timespec64 *ts)
3762e4ed8ba0SHoratiu Vultur {
3763e4ed8ba0SHoratiu Vultur 	return lan8841_ptp_set_target(ptp_priv, LAN8841_EVENT_A,
3764*c6d6ef3eSHoratiu Vultur 				      ts->tv_sec + LAN8841_BUFFER_TIME, 0);
3765e4ed8ba0SHoratiu Vultur }
3766e4ed8ba0SHoratiu Vultur 
3767e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_LTC_TARGET_RELOAD_SEC_HI(event)	((event) == LAN8841_EVENT_A ? 282 : 292)
3768e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_LTC_TARGET_RELOAD_SEC_LO(event)	((event) == LAN8841_EVENT_A ? 283 : 293)
3769e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_LTC_TARGET_RELOAD_NS_HI(event)	((event) == LAN8841_EVENT_A ? 284 : 294)
3770e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_LTC_TARGET_RELOAD_NS_LO(event)	((event) == LAN8841_EVENT_A ? 285 : 295)
3771e4ed8ba0SHoratiu Vultur 
3772e4ed8ba0SHoratiu Vultur static int lan8841_ptp_set_reload(struct kszphy_ptp_priv *ptp_priv, u8 event,
3773e4ed8ba0SHoratiu Vultur 				  s64 sec, u32 nsec)
3774e4ed8ba0SHoratiu Vultur {
3775e4ed8ba0SHoratiu Vultur 	struct phy_device *phydev = ptp_priv->phydev;
3776e4ed8ba0SHoratiu Vultur 	int ret;
3777e4ed8ba0SHoratiu Vultur 
3778e4ed8ba0SHoratiu Vultur 	ret = phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_TARGET_RELOAD_SEC_HI(event),
3779e4ed8ba0SHoratiu Vultur 			    upper_16_bits(sec));
3780e4ed8ba0SHoratiu Vultur 	if (ret)
3781e4ed8ba0SHoratiu Vultur 		return ret;
3782e4ed8ba0SHoratiu Vultur 
3783e4ed8ba0SHoratiu Vultur 	ret = phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_TARGET_RELOAD_SEC_LO(event),
3784e4ed8ba0SHoratiu Vultur 			    lower_16_bits(sec));
3785e4ed8ba0SHoratiu Vultur 	if (ret)
3786e4ed8ba0SHoratiu Vultur 		return ret;
3787e4ed8ba0SHoratiu Vultur 
3788e4ed8ba0SHoratiu Vultur 	ret = phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_TARGET_RELOAD_NS_HI(event) & 0x3fff,
3789e4ed8ba0SHoratiu Vultur 			    upper_16_bits(nsec));
3790e4ed8ba0SHoratiu Vultur 	if (ret)
3791e4ed8ba0SHoratiu Vultur 		return ret;
3792e4ed8ba0SHoratiu Vultur 
3793e4ed8ba0SHoratiu Vultur 	return phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_TARGET_RELOAD_NS_LO(event),
3794e4ed8ba0SHoratiu Vultur 			     lower_16_bits(nsec));
3795e4ed8ba0SHoratiu Vultur }
3796e4ed8ba0SHoratiu Vultur 
3797cafc3662SHoratiu Vultur #define LAN8841_PTP_LTC_SET_SEC_HI	262
3798cafc3662SHoratiu Vultur #define LAN8841_PTP_LTC_SET_SEC_MID	263
3799cafc3662SHoratiu Vultur #define LAN8841_PTP_LTC_SET_SEC_LO	264
3800cafc3662SHoratiu Vultur #define LAN8841_PTP_LTC_SET_NS_HI	265
3801cafc3662SHoratiu Vultur #define LAN8841_PTP_LTC_SET_NS_LO	266
3802cafc3662SHoratiu Vultur #define LAN8841_PTP_CMD_CTL_PTP_LTC_LOAD	BIT(4)
3803cafc3662SHoratiu Vultur 
3804cafc3662SHoratiu Vultur static int lan8841_ptp_settime64(struct ptp_clock_info *ptp,
3805cafc3662SHoratiu Vultur 				 const struct timespec64 *ts)
3806cafc3662SHoratiu Vultur {
3807cafc3662SHoratiu Vultur 	struct kszphy_ptp_priv *ptp_priv = container_of(ptp, struct kszphy_ptp_priv,
3808cafc3662SHoratiu Vultur 							ptp_clock_info);
3809cafc3662SHoratiu Vultur 	struct phy_device *phydev = ptp_priv->phydev;
3810e4ed8ba0SHoratiu Vultur 	int ret;
3811cafc3662SHoratiu Vultur 
3812cafc3662SHoratiu Vultur 	/* Set the value to be stored */
3813cafc3662SHoratiu Vultur 	mutex_lock(&ptp_priv->ptp_lock);
3814cafc3662SHoratiu Vultur 	phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_SET_SEC_LO, lower_16_bits(ts->tv_sec));
3815cafc3662SHoratiu Vultur 	phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_SET_SEC_MID, upper_16_bits(ts->tv_sec));
3816cafc3662SHoratiu Vultur 	phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_SET_SEC_HI, upper_32_bits(ts->tv_sec) & 0xffff);
3817cafc3662SHoratiu Vultur 	phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_SET_NS_LO, lower_16_bits(ts->tv_nsec));
3818cafc3662SHoratiu Vultur 	phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_SET_NS_HI, upper_16_bits(ts->tv_nsec) & 0x3fff);
3819cafc3662SHoratiu Vultur 
3820cafc3662SHoratiu Vultur 	/* Set the command to load the LTC */
3821cafc3662SHoratiu Vultur 	phy_write_mmd(phydev, 2, LAN8841_PTP_CMD_CTL,
3822cafc3662SHoratiu Vultur 		      LAN8841_PTP_CMD_CTL_PTP_LTC_LOAD);
3823e4ed8ba0SHoratiu Vultur 	ret = lan8841_ptp_update_target(ptp_priv, ts);
3824cafc3662SHoratiu Vultur 	mutex_unlock(&ptp_priv->ptp_lock);
3825cafc3662SHoratiu Vultur 
3826e4ed8ba0SHoratiu Vultur 	return ret;
3827cafc3662SHoratiu Vultur }
3828cafc3662SHoratiu Vultur 
3829cafc3662SHoratiu Vultur #define LAN8841_PTP_LTC_RD_SEC_HI	358
3830cafc3662SHoratiu Vultur #define LAN8841_PTP_LTC_RD_SEC_MID	359
3831cafc3662SHoratiu Vultur #define LAN8841_PTP_LTC_RD_SEC_LO	360
3832cafc3662SHoratiu Vultur #define LAN8841_PTP_LTC_RD_NS_HI	361
3833cafc3662SHoratiu Vultur #define LAN8841_PTP_LTC_RD_NS_LO	362
3834cafc3662SHoratiu Vultur #define LAN8841_PTP_CMD_CTL_PTP_LTC_READ	BIT(3)
3835cafc3662SHoratiu Vultur 
3836cafc3662SHoratiu Vultur static int lan8841_ptp_gettime64(struct ptp_clock_info *ptp,
3837cafc3662SHoratiu Vultur 				 struct timespec64 *ts)
3838cafc3662SHoratiu Vultur {
3839cafc3662SHoratiu Vultur 	struct kszphy_ptp_priv *ptp_priv = container_of(ptp, struct kszphy_ptp_priv,
3840cafc3662SHoratiu Vultur 							ptp_clock_info);
3841cafc3662SHoratiu Vultur 	struct phy_device *phydev = ptp_priv->phydev;
3842cafc3662SHoratiu Vultur 	time64_t s;
3843cafc3662SHoratiu Vultur 	s64 ns;
3844cafc3662SHoratiu Vultur 
3845cafc3662SHoratiu Vultur 	mutex_lock(&ptp_priv->ptp_lock);
3846cafc3662SHoratiu Vultur 	/* Issue the command to read the LTC */
3847cafc3662SHoratiu Vultur 	phy_write_mmd(phydev, 2, LAN8841_PTP_CMD_CTL,
3848cafc3662SHoratiu Vultur 		      LAN8841_PTP_CMD_CTL_PTP_LTC_READ);
3849cafc3662SHoratiu Vultur 
3850cafc3662SHoratiu Vultur 	/* Read the LTC */
3851cafc3662SHoratiu Vultur 	s = phy_read_mmd(phydev, 2, LAN8841_PTP_LTC_RD_SEC_HI);
3852cafc3662SHoratiu Vultur 	s <<= 16;
3853cafc3662SHoratiu Vultur 	s |= phy_read_mmd(phydev, 2, LAN8841_PTP_LTC_RD_SEC_MID);
3854cafc3662SHoratiu Vultur 	s <<= 16;
3855cafc3662SHoratiu Vultur 	s |= phy_read_mmd(phydev, 2, LAN8841_PTP_LTC_RD_SEC_LO);
3856cafc3662SHoratiu Vultur 
3857cafc3662SHoratiu Vultur 	ns = phy_read_mmd(phydev, 2, LAN8841_PTP_LTC_RD_NS_HI) & 0x3fff;
3858cafc3662SHoratiu Vultur 	ns <<= 16;
3859cafc3662SHoratiu Vultur 	ns |= phy_read_mmd(phydev, 2, LAN8841_PTP_LTC_RD_NS_LO);
3860cafc3662SHoratiu Vultur 	mutex_unlock(&ptp_priv->ptp_lock);
3861cafc3662SHoratiu Vultur 
3862cafc3662SHoratiu Vultur 	set_normalized_timespec64(ts, s, ns);
3863cafc3662SHoratiu Vultur 	return 0;
3864cafc3662SHoratiu Vultur }
3865cafc3662SHoratiu Vultur 
3866cafc3662SHoratiu Vultur #define LAN8841_PTP_LTC_STEP_ADJ_LO			276
3867cafc3662SHoratiu Vultur #define LAN8841_PTP_LTC_STEP_ADJ_HI			275
3868cafc3662SHoratiu Vultur #define LAN8841_PTP_LTC_STEP_ADJ_DIR			BIT(15)
3869cafc3662SHoratiu Vultur #define LAN8841_PTP_CMD_CTL_PTP_LTC_STEP_SECONDS	BIT(5)
3870cafc3662SHoratiu Vultur #define LAN8841_PTP_CMD_CTL_PTP_LTC_STEP_NANOSECONDS	BIT(6)
3871cafc3662SHoratiu Vultur 
3872cafc3662SHoratiu Vultur static int lan8841_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
3873cafc3662SHoratiu Vultur {
3874cafc3662SHoratiu Vultur 	struct kszphy_ptp_priv *ptp_priv = container_of(ptp, struct kszphy_ptp_priv,
3875cafc3662SHoratiu Vultur 							ptp_clock_info);
3876cafc3662SHoratiu Vultur 	struct phy_device *phydev = ptp_priv->phydev;
3877cafc3662SHoratiu Vultur 	struct timespec64 ts;
3878cafc3662SHoratiu Vultur 	bool add = true;
3879cafc3662SHoratiu Vultur 	u32 nsec;
3880cafc3662SHoratiu Vultur 	s32 sec;
3881e4ed8ba0SHoratiu Vultur 	int ret;
3882cafc3662SHoratiu Vultur 
3883cafc3662SHoratiu Vultur 	/* The HW allows up to 15 sec to adjust the time, but here we limit to
3884cafc3662SHoratiu Vultur 	 * 10 sec the adjustment. The reason is, in case the adjustment is 14
3885cafc3662SHoratiu Vultur 	 * sec and 999999999 nsec, then we add 8ns to compansate the actual
3886cafc3662SHoratiu Vultur 	 * increment so the value can be bigger than 15 sec. Therefore limit the
3887cafc3662SHoratiu Vultur 	 * possible adjustments so we will not have these corner cases
3888cafc3662SHoratiu Vultur 	 */
3889cafc3662SHoratiu Vultur 	if (delta > 10000000000LL || delta < -10000000000LL) {
3890cafc3662SHoratiu Vultur 		/* The timeadjustment is too big, so fall back using set time */
3891cafc3662SHoratiu Vultur 		u64 now;
3892cafc3662SHoratiu Vultur 
3893cafc3662SHoratiu Vultur 		ptp->gettime64(ptp, &ts);
3894cafc3662SHoratiu Vultur 
3895cafc3662SHoratiu Vultur 		now = ktime_to_ns(timespec64_to_ktime(ts));
3896cafc3662SHoratiu Vultur 		ts = ns_to_timespec64(now + delta);
3897cafc3662SHoratiu Vultur 
3898cafc3662SHoratiu Vultur 		ptp->settime64(ptp, &ts);
3899cafc3662SHoratiu Vultur 		return 0;
3900cafc3662SHoratiu Vultur 	}
3901cafc3662SHoratiu Vultur 
3902cafc3662SHoratiu Vultur 	sec = div_u64_rem(delta < 0 ? -delta : delta, NSEC_PER_SEC, &nsec);
3903cafc3662SHoratiu Vultur 	if (delta < 0 && nsec != 0) {
3904cafc3662SHoratiu Vultur 		/* It is not allowed to adjust low the nsec part, therefore
3905cafc3662SHoratiu Vultur 		 * subtract more from second part and add to nanosecond such
3906cafc3662SHoratiu Vultur 		 * that would roll over, so the second part will increase
3907cafc3662SHoratiu Vultur 		 */
3908cafc3662SHoratiu Vultur 		sec--;
3909cafc3662SHoratiu Vultur 		nsec = NSEC_PER_SEC - nsec;
3910cafc3662SHoratiu Vultur 	}
3911cafc3662SHoratiu Vultur 
3912cafc3662SHoratiu Vultur 	/* Calculate the adjustments and the direction */
3913cafc3662SHoratiu Vultur 	if (delta < 0)
3914cafc3662SHoratiu Vultur 		add = false;
3915cafc3662SHoratiu Vultur 
3916cafc3662SHoratiu Vultur 	if (nsec > 0)
3917cafc3662SHoratiu Vultur 		/* add 8 ns to cover the likely normal increment */
3918cafc3662SHoratiu Vultur 		nsec += 8;
3919cafc3662SHoratiu Vultur 
3920cafc3662SHoratiu Vultur 	if (nsec >= NSEC_PER_SEC) {
3921cafc3662SHoratiu Vultur 		/* carry into seconds */
3922cafc3662SHoratiu Vultur 		sec++;
3923cafc3662SHoratiu Vultur 		nsec -= NSEC_PER_SEC;
3924cafc3662SHoratiu Vultur 	}
3925cafc3662SHoratiu Vultur 
3926cafc3662SHoratiu Vultur 	mutex_lock(&ptp_priv->ptp_lock);
3927cafc3662SHoratiu Vultur 	if (sec) {
3928cafc3662SHoratiu Vultur 		phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_STEP_ADJ_LO, sec);
3929cafc3662SHoratiu Vultur 		phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_STEP_ADJ_HI,
3930cafc3662SHoratiu Vultur 			      add ? LAN8841_PTP_LTC_STEP_ADJ_DIR : 0);
3931cafc3662SHoratiu Vultur 		phy_write_mmd(phydev, 2, LAN8841_PTP_CMD_CTL,
3932cafc3662SHoratiu Vultur 			      LAN8841_PTP_CMD_CTL_PTP_LTC_STEP_SECONDS);
3933cafc3662SHoratiu Vultur 	}
3934cafc3662SHoratiu Vultur 
3935cafc3662SHoratiu Vultur 	if (nsec) {
3936cafc3662SHoratiu Vultur 		phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_STEP_ADJ_LO,
3937cafc3662SHoratiu Vultur 			      nsec & 0xffff);
3938cafc3662SHoratiu Vultur 		phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_STEP_ADJ_HI,
3939cafc3662SHoratiu Vultur 			      (nsec >> 16) & 0x3fff);
3940cafc3662SHoratiu Vultur 		phy_write_mmd(phydev, 2, LAN8841_PTP_CMD_CTL,
3941cafc3662SHoratiu Vultur 			      LAN8841_PTP_CMD_CTL_PTP_LTC_STEP_NANOSECONDS);
3942cafc3662SHoratiu Vultur 	}
3943cafc3662SHoratiu Vultur 	mutex_unlock(&ptp_priv->ptp_lock);
3944cafc3662SHoratiu Vultur 
3945e4ed8ba0SHoratiu Vultur 	/* Update the target clock */
3946e4ed8ba0SHoratiu Vultur 	ptp->gettime64(ptp, &ts);
3947e4ed8ba0SHoratiu Vultur 	mutex_lock(&ptp_priv->ptp_lock);
3948e4ed8ba0SHoratiu Vultur 	ret = lan8841_ptp_update_target(ptp_priv, &ts);
3949e4ed8ba0SHoratiu Vultur 	mutex_unlock(&ptp_priv->ptp_lock);
3950e4ed8ba0SHoratiu Vultur 
3951e4ed8ba0SHoratiu Vultur 	return ret;
3952cafc3662SHoratiu Vultur }
3953cafc3662SHoratiu Vultur 
3954cafc3662SHoratiu Vultur #define LAN8841_PTP_LTC_RATE_ADJ_HI		269
3955cafc3662SHoratiu Vultur #define LAN8841_PTP_LTC_RATE_ADJ_HI_DIR		BIT(15)
3956cafc3662SHoratiu Vultur #define LAN8841_PTP_LTC_RATE_ADJ_LO		270
3957cafc3662SHoratiu Vultur 
3958cafc3662SHoratiu Vultur static int lan8841_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
3959cafc3662SHoratiu Vultur {
3960cafc3662SHoratiu Vultur 	struct kszphy_ptp_priv *ptp_priv = container_of(ptp, struct kszphy_ptp_priv,
3961cafc3662SHoratiu Vultur 							ptp_clock_info);
3962cafc3662SHoratiu Vultur 	struct phy_device *phydev = ptp_priv->phydev;
3963cafc3662SHoratiu Vultur 	bool faster = true;
3964cafc3662SHoratiu Vultur 	u32 rate;
3965cafc3662SHoratiu Vultur 
3966cafc3662SHoratiu Vultur 	if (!scaled_ppm)
3967cafc3662SHoratiu Vultur 		return 0;
3968cafc3662SHoratiu Vultur 
3969cafc3662SHoratiu Vultur 	if (scaled_ppm < 0) {
3970cafc3662SHoratiu Vultur 		scaled_ppm = -scaled_ppm;
3971cafc3662SHoratiu Vultur 		faster = false;
3972cafc3662SHoratiu Vultur 	}
3973cafc3662SHoratiu Vultur 
3974cafc3662SHoratiu Vultur 	rate = LAN8814_1PPM_FORMAT * (upper_16_bits(scaled_ppm));
3975cafc3662SHoratiu Vultur 	rate += (LAN8814_1PPM_FORMAT * (lower_16_bits(scaled_ppm))) >> 16;
3976cafc3662SHoratiu Vultur 
3977cafc3662SHoratiu Vultur 	mutex_lock(&ptp_priv->ptp_lock);
3978cafc3662SHoratiu Vultur 	phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_RATE_ADJ_HI,
3979cafc3662SHoratiu Vultur 		      faster ? LAN8841_PTP_LTC_RATE_ADJ_HI_DIR | (upper_16_bits(rate) & 0x3fff)
3980cafc3662SHoratiu Vultur 			     : upper_16_bits(rate) & 0x3fff);
3981cafc3662SHoratiu Vultur 	phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_RATE_ADJ_LO, lower_16_bits(rate));
3982cafc3662SHoratiu Vultur 	mutex_unlock(&ptp_priv->ptp_lock);
3983cafc3662SHoratiu Vultur 
3984cafc3662SHoratiu Vultur 	return 0;
3985cafc3662SHoratiu Vultur }
3986cafc3662SHoratiu Vultur 
3987e4ed8ba0SHoratiu Vultur static int lan8841_ptp_verify(struct ptp_clock_info *ptp, unsigned int pin,
3988e4ed8ba0SHoratiu Vultur 			      enum ptp_pin_function func, unsigned int chan)
3989e4ed8ba0SHoratiu Vultur {
3990e4ed8ba0SHoratiu Vultur 	switch (func) {
3991e4ed8ba0SHoratiu Vultur 	case PTP_PF_NONE:
3992e4ed8ba0SHoratiu Vultur 	case PTP_PF_PEROUT:
3993fac63186SHoratiu Vultur 	case PTP_PF_EXTTS:
3994e4ed8ba0SHoratiu Vultur 		break;
3995e4ed8ba0SHoratiu Vultur 	default:
3996e4ed8ba0SHoratiu Vultur 		return -1;
3997e4ed8ba0SHoratiu Vultur 	}
3998e4ed8ba0SHoratiu Vultur 
3999e4ed8ba0SHoratiu Vultur 	return 0;
4000e4ed8ba0SHoratiu Vultur }
4001e4ed8ba0SHoratiu Vultur 
4002e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_GPIO_NUM	10
4003e4ed8ba0SHoratiu Vultur #define LAN8841_GPIO_EN		128
4004e4ed8ba0SHoratiu Vultur #define LAN8841_GPIO_DIR	129
4005e4ed8ba0SHoratiu Vultur #define LAN8841_GPIO_BUF	130
4006e4ed8ba0SHoratiu Vultur 
4007e4ed8ba0SHoratiu Vultur static int lan8841_ptp_perout_off(struct kszphy_ptp_priv *ptp_priv, int pin)
4008e4ed8ba0SHoratiu Vultur {
4009e4ed8ba0SHoratiu Vultur 	struct phy_device *phydev = ptp_priv->phydev;
4010e4ed8ba0SHoratiu Vultur 	int ret;
4011e4ed8ba0SHoratiu Vultur 
4012e4ed8ba0SHoratiu Vultur 	ret = phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_EN, BIT(pin));
4013e4ed8ba0SHoratiu Vultur 	if (ret)
4014e4ed8ba0SHoratiu Vultur 		return ret;
4015e4ed8ba0SHoratiu Vultur 
4016e4ed8ba0SHoratiu Vultur 	ret = phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_DIR, BIT(pin));
4017e4ed8ba0SHoratiu Vultur 	if (ret)
4018e4ed8ba0SHoratiu Vultur 		return ret;
4019e4ed8ba0SHoratiu Vultur 
4020e4ed8ba0SHoratiu Vultur 	return phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_BUF, BIT(pin));
4021e4ed8ba0SHoratiu Vultur }
4022e4ed8ba0SHoratiu Vultur 
4023e4ed8ba0SHoratiu Vultur static int lan8841_ptp_perout_on(struct kszphy_ptp_priv *ptp_priv, int pin)
4024e4ed8ba0SHoratiu Vultur {
4025e4ed8ba0SHoratiu Vultur 	struct phy_device *phydev = ptp_priv->phydev;
4026e4ed8ba0SHoratiu Vultur 	int ret;
4027e4ed8ba0SHoratiu Vultur 
4028e4ed8ba0SHoratiu Vultur 	ret = phy_set_bits_mmd(phydev, 2, LAN8841_GPIO_EN, BIT(pin));
4029e4ed8ba0SHoratiu Vultur 	if (ret)
4030e4ed8ba0SHoratiu Vultur 		return ret;
4031e4ed8ba0SHoratiu Vultur 
4032e4ed8ba0SHoratiu Vultur 	ret = phy_set_bits_mmd(phydev, 2, LAN8841_GPIO_DIR, BIT(pin));
4033e4ed8ba0SHoratiu Vultur 	if (ret)
4034e4ed8ba0SHoratiu Vultur 		return ret;
4035e4ed8ba0SHoratiu Vultur 
4036e4ed8ba0SHoratiu Vultur 	return phy_set_bits_mmd(phydev, 2, LAN8841_GPIO_BUF, BIT(pin));
4037e4ed8ba0SHoratiu Vultur }
4038e4ed8ba0SHoratiu Vultur 
4039e4ed8ba0SHoratiu Vultur #define LAN8841_GPIO_DATA_SEL1				131
4040e4ed8ba0SHoratiu Vultur #define LAN8841_GPIO_DATA_SEL2				132
4041e4ed8ba0SHoratiu Vultur #define LAN8841_GPIO_DATA_SEL_GPIO_DATA_SEL_EVENT_MASK	GENMASK(2, 0)
4042e4ed8ba0SHoratiu Vultur #define LAN8841_GPIO_DATA_SEL_GPIO_DATA_SEL_EVENT_A	1
4043e4ed8ba0SHoratiu Vultur #define LAN8841_GPIO_DATA_SEL_GPIO_DATA_SEL_EVENT_B	2
4044e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_GENERAL_CONFIG			257
4045e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_A	BIT(1)
4046e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_B	BIT(3)
4047e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_A_MASK	GENMASK(7, 4)
4048e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_B_MASK	GENMASK(11, 8)
4049e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_A		4
4050e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_B		7
4051e4ed8ba0SHoratiu Vultur 
4052e4ed8ba0SHoratiu Vultur static int lan8841_ptp_remove_event(struct kszphy_ptp_priv *ptp_priv, int pin,
4053e4ed8ba0SHoratiu Vultur 				    u8 event)
4054e4ed8ba0SHoratiu Vultur {
4055e4ed8ba0SHoratiu Vultur 	struct phy_device *phydev = ptp_priv->phydev;
4056e4ed8ba0SHoratiu Vultur 	u16 tmp;
4057e4ed8ba0SHoratiu Vultur 	int ret;
4058e4ed8ba0SHoratiu Vultur 
4059e4ed8ba0SHoratiu Vultur 	/* Now remove pin from the event. GPIO_DATA_SEL1 contains the GPIO
4060e4ed8ba0SHoratiu Vultur 	 * pins 0-4 while GPIO_DATA_SEL2 contains GPIO pins 5-9, therefore
4061e4ed8ba0SHoratiu Vultur 	 * depending on the pin, it requires to read a different register
4062e4ed8ba0SHoratiu Vultur 	 */
4063e4ed8ba0SHoratiu Vultur 	if (pin < 5) {
4064e4ed8ba0SHoratiu Vultur 		tmp = LAN8841_GPIO_DATA_SEL_GPIO_DATA_SEL_EVENT_MASK << (3 * pin);
4065e4ed8ba0SHoratiu Vultur 		ret = phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_DATA_SEL1, tmp);
4066e4ed8ba0SHoratiu Vultur 	} else {
4067e4ed8ba0SHoratiu Vultur 		tmp = LAN8841_GPIO_DATA_SEL_GPIO_DATA_SEL_EVENT_MASK << (3 * (pin - 5));
4068e4ed8ba0SHoratiu Vultur 		ret = phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_DATA_SEL2, tmp);
4069e4ed8ba0SHoratiu Vultur 	}
4070e4ed8ba0SHoratiu Vultur 	if (ret)
4071e4ed8ba0SHoratiu Vultur 		return ret;
4072e4ed8ba0SHoratiu Vultur 
4073e4ed8ba0SHoratiu Vultur 	/* Disable the event */
4074e4ed8ba0SHoratiu Vultur 	if (event == LAN8841_EVENT_A)
4075e4ed8ba0SHoratiu Vultur 		tmp = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_A |
4076e4ed8ba0SHoratiu Vultur 		      LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_A_MASK;
4077e4ed8ba0SHoratiu Vultur 	else
4078e4ed8ba0SHoratiu Vultur 		tmp = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_B |
4079e4ed8ba0SHoratiu Vultur 		      LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_B_MASK;
4080e4ed8ba0SHoratiu Vultur 	return phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_EN, tmp);
4081e4ed8ba0SHoratiu Vultur }
4082e4ed8ba0SHoratiu Vultur 
4083e4ed8ba0SHoratiu Vultur static int lan8841_ptp_enable_event(struct kszphy_ptp_priv *ptp_priv, int pin,
4084e4ed8ba0SHoratiu Vultur 				    u8 event, int pulse_width)
4085e4ed8ba0SHoratiu Vultur {
4086e4ed8ba0SHoratiu Vultur 	struct phy_device *phydev = ptp_priv->phydev;
4087e4ed8ba0SHoratiu Vultur 	u16 tmp;
4088e4ed8ba0SHoratiu Vultur 	int ret;
4089e4ed8ba0SHoratiu Vultur 
4090e4ed8ba0SHoratiu Vultur 	/* Enable the event */
4091e4ed8ba0SHoratiu Vultur 	if (event == LAN8841_EVENT_A)
4092e4ed8ba0SHoratiu Vultur 		ret = phy_modify_mmd(phydev, 2, LAN8841_PTP_GENERAL_CONFIG,
4093e4ed8ba0SHoratiu Vultur 				     LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_A |
4094e4ed8ba0SHoratiu Vultur 				     LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_A_MASK,
4095e4ed8ba0SHoratiu Vultur 				     LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_A |
4096e4ed8ba0SHoratiu Vultur 				     pulse_width << LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_A);
4097e4ed8ba0SHoratiu Vultur 	else
4098e4ed8ba0SHoratiu Vultur 		ret = phy_modify_mmd(phydev, 2, LAN8841_PTP_GENERAL_CONFIG,
4099e4ed8ba0SHoratiu Vultur 				     LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_B |
4100e4ed8ba0SHoratiu Vultur 				     LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_B_MASK,
4101e4ed8ba0SHoratiu Vultur 				     LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_B |
4102e4ed8ba0SHoratiu Vultur 				     pulse_width << LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_B);
4103e4ed8ba0SHoratiu Vultur 	if (ret)
4104e4ed8ba0SHoratiu Vultur 		return ret;
4105e4ed8ba0SHoratiu Vultur 
4106e4ed8ba0SHoratiu Vultur 	/* Now connect the pin to the event. GPIO_DATA_SEL1 contains the GPIO
4107e4ed8ba0SHoratiu Vultur 	 * pins 0-4 while GPIO_DATA_SEL2 contains GPIO pins 5-9, therefore
4108e4ed8ba0SHoratiu Vultur 	 * depending on the pin, it requires to read a different register
4109e4ed8ba0SHoratiu Vultur 	 */
4110e4ed8ba0SHoratiu Vultur 	if (event == LAN8841_EVENT_A)
4111e4ed8ba0SHoratiu Vultur 		tmp = LAN8841_GPIO_DATA_SEL_GPIO_DATA_SEL_EVENT_A;
4112e4ed8ba0SHoratiu Vultur 	else
4113e4ed8ba0SHoratiu Vultur 		tmp = LAN8841_GPIO_DATA_SEL_GPIO_DATA_SEL_EVENT_B;
4114e4ed8ba0SHoratiu Vultur 
4115e4ed8ba0SHoratiu Vultur 	if (pin < 5)
4116e4ed8ba0SHoratiu Vultur 		ret = phy_set_bits_mmd(phydev, 2, LAN8841_GPIO_DATA_SEL1,
4117e4ed8ba0SHoratiu Vultur 				       tmp << (3 * pin));
4118e4ed8ba0SHoratiu Vultur 	else
4119e4ed8ba0SHoratiu Vultur 		ret = phy_set_bits_mmd(phydev, 2, LAN8841_GPIO_DATA_SEL2,
4120e4ed8ba0SHoratiu Vultur 				       tmp << (3 * (pin - 5)));
4121e4ed8ba0SHoratiu Vultur 
4122e4ed8ba0SHoratiu Vultur 	return ret;
4123e4ed8ba0SHoratiu Vultur }
4124e4ed8ba0SHoratiu Vultur 
4125e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_200MS	13
4126e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100MS	12
4127e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_50MS	11
4128e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_10MS	10
4129e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_5MS	9
4130e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_1MS	8
4131e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_500US	7
4132e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100US	6
4133e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_50US	5
4134e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_10US	4
4135e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_5US	3
4136e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_1US	2
4137e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_500NS	1
4138e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100NS	0
4139e4ed8ba0SHoratiu Vultur 
4140e4ed8ba0SHoratiu Vultur static int lan8841_ptp_perout(struct ptp_clock_info *ptp,
4141e4ed8ba0SHoratiu Vultur 			      struct ptp_clock_request *rq, int on)
4142e4ed8ba0SHoratiu Vultur {
4143e4ed8ba0SHoratiu Vultur 	struct kszphy_ptp_priv *ptp_priv = container_of(ptp, struct kszphy_ptp_priv,
4144e4ed8ba0SHoratiu Vultur 							ptp_clock_info);
4145e4ed8ba0SHoratiu Vultur 	struct phy_device *phydev = ptp_priv->phydev;
4146e4ed8ba0SHoratiu Vultur 	struct timespec64 ts_on, ts_period;
4147e4ed8ba0SHoratiu Vultur 	s64 on_nsec, period_nsec;
4148e4ed8ba0SHoratiu Vultur 	int pulse_width;
4149e4ed8ba0SHoratiu Vultur 	int pin;
4150e4ed8ba0SHoratiu Vultur 	int ret;
4151e4ed8ba0SHoratiu Vultur 
4152e4ed8ba0SHoratiu Vultur 	if (rq->perout.flags & ~PTP_PEROUT_DUTY_CYCLE)
4153e4ed8ba0SHoratiu Vultur 		return -EOPNOTSUPP;
4154e4ed8ba0SHoratiu Vultur 
4155e4ed8ba0SHoratiu Vultur 	pin = ptp_find_pin(ptp_priv->ptp_clock, PTP_PF_PEROUT, rq->perout.index);
4156e4ed8ba0SHoratiu Vultur 	if (pin == -1 || pin >= LAN8841_PTP_GPIO_NUM)
4157e4ed8ba0SHoratiu Vultur 		return -EINVAL;
4158e4ed8ba0SHoratiu Vultur 
4159e4ed8ba0SHoratiu Vultur 	if (!on) {
4160e4ed8ba0SHoratiu Vultur 		ret = lan8841_ptp_perout_off(ptp_priv, pin);
4161e4ed8ba0SHoratiu Vultur 		if (ret)
4162e4ed8ba0SHoratiu Vultur 			return ret;
4163e4ed8ba0SHoratiu Vultur 
4164e4ed8ba0SHoratiu Vultur 		return lan8841_ptp_remove_event(ptp_priv, LAN8841_EVENT_A, pin);
4165e4ed8ba0SHoratiu Vultur 	}
4166e4ed8ba0SHoratiu Vultur 
4167e4ed8ba0SHoratiu Vultur 	ts_on.tv_sec = rq->perout.on.sec;
4168e4ed8ba0SHoratiu Vultur 	ts_on.tv_nsec = rq->perout.on.nsec;
4169e4ed8ba0SHoratiu Vultur 	on_nsec = timespec64_to_ns(&ts_on);
4170e4ed8ba0SHoratiu Vultur 
4171e4ed8ba0SHoratiu Vultur 	ts_period.tv_sec = rq->perout.period.sec;
4172e4ed8ba0SHoratiu Vultur 	ts_period.tv_nsec = rq->perout.period.nsec;
4173e4ed8ba0SHoratiu Vultur 	period_nsec = timespec64_to_ns(&ts_period);
4174e4ed8ba0SHoratiu Vultur 
4175e4ed8ba0SHoratiu Vultur 	if (period_nsec < 200) {
41769bdf4489SColin Ian King 		pr_warn_ratelimited("%s: perout period too small, minimum is 200 nsec\n",
4177e4ed8ba0SHoratiu Vultur 				    phydev_name(phydev));
4178e4ed8ba0SHoratiu Vultur 		return -EOPNOTSUPP;
4179e4ed8ba0SHoratiu Vultur 	}
4180e4ed8ba0SHoratiu Vultur 
4181e4ed8ba0SHoratiu Vultur 	if (on_nsec >= period_nsec) {
4182e4ed8ba0SHoratiu Vultur 		pr_warn_ratelimited("%s: pulse width must be smaller than period\n",
4183e4ed8ba0SHoratiu Vultur 				    phydev_name(phydev));
4184e4ed8ba0SHoratiu Vultur 		return -EINVAL;
4185e4ed8ba0SHoratiu Vultur 	}
4186e4ed8ba0SHoratiu Vultur 
4187e4ed8ba0SHoratiu Vultur 	switch (on_nsec) {
4188e4ed8ba0SHoratiu Vultur 	case 200000000:
4189e4ed8ba0SHoratiu Vultur 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_200MS;
4190e4ed8ba0SHoratiu Vultur 		break;
4191e4ed8ba0SHoratiu Vultur 	case 100000000:
4192e4ed8ba0SHoratiu Vultur 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100MS;
4193e4ed8ba0SHoratiu Vultur 		break;
4194e4ed8ba0SHoratiu Vultur 	case 50000000:
4195e4ed8ba0SHoratiu Vultur 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_50MS;
4196e4ed8ba0SHoratiu Vultur 		break;
4197e4ed8ba0SHoratiu Vultur 	case 10000000:
4198e4ed8ba0SHoratiu Vultur 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_10MS;
4199e4ed8ba0SHoratiu Vultur 		break;
4200e4ed8ba0SHoratiu Vultur 	case 5000000:
4201e4ed8ba0SHoratiu Vultur 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_5MS;
4202e4ed8ba0SHoratiu Vultur 		break;
4203e4ed8ba0SHoratiu Vultur 	case 1000000:
4204e4ed8ba0SHoratiu Vultur 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_1MS;
4205e4ed8ba0SHoratiu Vultur 		break;
4206e4ed8ba0SHoratiu Vultur 	case 500000:
4207e4ed8ba0SHoratiu Vultur 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_500US;
4208e4ed8ba0SHoratiu Vultur 		break;
4209e4ed8ba0SHoratiu Vultur 	case 100000:
4210e4ed8ba0SHoratiu Vultur 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100US;
4211e4ed8ba0SHoratiu Vultur 		break;
4212e4ed8ba0SHoratiu Vultur 	case 50000:
4213e4ed8ba0SHoratiu Vultur 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_50US;
4214e4ed8ba0SHoratiu Vultur 		break;
4215e4ed8ba0SHoratiu Vultur 	case 10000:
4216e4ed8ba0SHoratiu Vultur 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_10US;
4217e4ed8ba0SHoratiu Vultur 		break;
4218e4ed8ba0SHoratiu Vultur 	case 5000:
4219e4ed8ba0SHoratiu Vultur 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_5US;
4220e4ed8ba0SHoratiu Vultur 		break;
4221e4ed8ba0SHoratiu Vultur 	case 1000:
4222e4ed8ba0SHoratiu Vultur 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_1US;
4223e4ed8ba0SHoratiu Vultur 		break;
4224e4ed8ba0SHoratiu Vultur 	case 500:
4225e4ed8ba0SHoratiu Vultur 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_500NS;
4226e4ed8ba0SHoratiu Vultur 		break;
4227e4ed8ba0SHoratiu Vultur 	case 100:
4228e4ed8ba0SHoratiu Vultur 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100NS;
4229e4ed8ba0SHoratiu Vultur 		break;
4230e4ed8ba0SHoratiu Vultur 	default:
4231e4ed8ba0SHoratiu Vultur 		pr_warn_ratelimited("%s: Use default duty cycle of 100ns\n",
4232e4ed8ba0SHoratiu Vultur 				    phydev_name(phydev));
4233e4ed8ba0SHoratiu Vultur 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100NS;
4234e4ed8ba0SHoratiu Vultur 		break;
4235e4ed8ba0SHoratiu Vultur 	}
4236e4ed8ba0SHoratiu Vultur 
4237e4ed8ba0SHoratiu Vultur 	mutex_lock(&ptp_priv->ptp_lock);
4238e4ed8ba0SHoratiu Vultur 	ret = lan8841_ptp_set_target(ptp_priv, LAN8841_EVENT_A, rq->perout.start.sec,
4239e4ed8ba0SHoratiu Vultur 				     rq->perout.start.nsec);
4240e4ed8ba0SHoratiu Vultur 	mutex_unlock(&ptp_priv->ptp_lock);
4241e4ed8ba0SHoratiu Vultur 	if (ret)
4242e4ed8ba0SHoratiu Vultur 		return ret;
4243e4ed8ba0SHoratiu Vultur 
4244e4ed8ba0SHoratiu Vultur 	ret = lan8841_ptp_set_reload(ptp_priv, LAN8841_EVENT_A, rq->perout.period.sec,
4245e4ed8ba0SHoratiu Vultur 				     rq->perout.period.nsec);
4246e4ed8ba0SHoratiu Vultur 	if (ret)
4247e4ed8ba0SHoratiu Vultur 		return ret;
4248e4ed8ba0SHoratiu Vultur 
4249e4ed8ba0SHoratiu Vultur 	ret = lan8841_ptp_enable_event(ptp_priv, pin, LAN8841_EVENT_A,
4250e4ed8ba0SHoratiu Vultur 				       pulse_width);
4251e4ed8ba0SHoratiu Vultur 	if (ret)
4252e4ed8ba0SHoratiu Vultur 		return ret;
4253e4ed8ba0SHoratiu Vultur 
4254e4ed8ba0SHoratiu Vultur 	ret = lan8841_ptp_perout_on(ptp_priv, pin);
4255e4ed8ba0SHoratiu Vultur 	if (ret)
4256e4ed8ba0SHoratiu Vultur 		lan8841_ptp_remove_event(ptp_priv, pin, LAN8841_EVENT_A);
4257e4ed8ba0SHoratiu Vultur 
4258e4ed8ba0SHoratiu Vultur 	return ret;
4259e4ed8ba0SHoratiu Vultur }
4260e4ed8ba0SHoratiu Vultur 
4261fac63186SHoratiu Vultur #define LAN8841_PTP_GPIO_CAP_EN			496
4262fac63186SHoratiu Vultur #define LAN8841_PTP_GPIO_CAP_EN_GPIO_RE_CAPTURE_ENABLE(gpio)	(BIT(gpio))
4263fac63186SHoratiu Vultur #define LAN8841_PTP_GPIO_CAP_EN_GPIO_FE_CAPTURE_ENABLE(gpio)	(BIT(gpio) << 8)
4264fac63186SHoratiu Vultur #define LAN8841_PTP_INT_EN_PTP_GPIO_CAP_EN	BIT(2)
4265fac63186SHoratiu Vultur 
4266fac63186SHoratiu Vultur static int lan8841_ptp_extts_on(struct kszphy_ptp_priv *ptp_priv, int pin,
4267fac63186SHoratiu Vultur 				u32 flags)
4268fac63186SHoratiu Vultur {
4269fac63186SHoratiu Vultur 	struct phy_device *phydev = ptp_priv->phydev;
4270fac63186SHoratiu Vultur 	u16 tmp = 0;
4271fac63186SHoratiu Vultur 	int ret;
4272fac63186SHoratiu Vultur 
4273fac63186SHoratiu Vultur 	/* Set GPIO to be intput */
4274fac63186SHoratiu Vultur 	ret = phy_set_bits_mmd(phydev, 2, LAN8841_GPIO_EN, BIT(pin));
4275fac63186SHoratiu Vultur 	if (ret)
4276fac63186SHoratiu Vultur 		return ret;
4277fac63186SHoratiu Vultur 
4278fac63186SHoratiu Vultur 	ret = phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_BUF, BIT(pin));
4279fac63186SHoratiu Vultur 	if (ret)
4280fac63186SHoratiu Vultur 		return ret;
4281fac63186SHoratiu Vultur 
4282fac63186SHoratiu Vultur 	/* Enable capture on the edges of the pin */
4283fac63186SHoratiu Vultur 	if (flags & PTP_RISING_EDGE)
4284fac63186SHoratiu Vultur 		tmp |= LAN8841_PTP_GPIO_CAP_EN_GPIO_RE_CAPTURE_ENABLE(pin);
4285fac63186SHoratiu Vultur 	if (flags & PTP_FALLING_EDGE)
4286fac63186SHoratiu Vultur 		tmp |= LAN8841_PTP_GPIO_CAP_EN_GPIO_FE_CAPTURE_ENABLE(pin);
4287fac63186SHoratiu Vultur 	ret = phy_write_mmd(phydev, 2, LAN8841_PTP_GPIO_CAP_EN, tmp);
4288fac63186SHoratiu Vultur 	if (ret)
4289fac63186SHoratiu Vultur 		return ret;
4290fac63186SHoratiu Vultur 
4291fac63186SHoratiu Vultur 	/* Enable interrupt */
4292fac63186SHoratiu Vultur 	return phy_modify_mmd(phydev, 2, LAN8841_PTP_INT_EN,
4293fac63186SHoratiu Vultur 			      LAN8841_PTP_INT_EN_PTP_GPIO_CAP_EN,
4294fac63186SHoratiu Vultur 			      LAN8841_PTP_INT_EN_PTP_GPIO_CAP_EN);
4295fac63186SHoratiu Vultur }
4296fac63186SHoratiu Vultur 
4297fac63186SHoratiu Vultur static int lan8841_ptp_extts_off(struct kszphy_ptp_priv *ptp_priv, int pin)
4298fac63186SHoratiu Vultur {
4299fac63186SHoratiu Vultur 	struct phy_device *phydev = ptp_priv->phydev;
4300fac63186SHoratiu Vultur 	int ret;
4301fac63186SHoratiu Vultur 
4302fac63186SHoratiu Vultur 	/* Set GPIO to be output */
4303fac63186SHoratiu Vultur 	ret = phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_EN, BIT(pin));
4304fac63186SHoratiu Vultur 	if (ret)
4305fac63186SHoratiu Vultur 		return ret;
4306fac63186SHoratiu Vultur 
4307fac63186SHoratiu Vultur 	ret = phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_BUF, BIT(pin));
4308fac63186SHoratiu Vultur 	if (ret)
4309fac63186SHoratiu Vultur 		return ret;
4310fac63186SHoratiu Vultur 
4311fac63186SHoratiu Vultur 	/* Disable capture on both of the edges */
4312fac63186SHoratiu Vultur 	ret = phy_modify_mmd(phydev, 2, LAN8841_PTP_GPIO_CAP_EN,
4313fac63186SHoratiu Vultur 			     LAN8841_PTP_GPIO_CAP_EN_GPIO_RE_CAPTURE_ENABLE(pin) |
4314fac63186SHoratiu Vultur 			     LAN8841_PTP_GPIO_CAP_EN_GPIO_FE_CAPTURE_ENABLE(pin),
4315fac63186SHoratiu Vultur 			     0);
4316fac63186SHoratiu Vultur 	if (ret)
4317fac63186SHoratiu Vultur 		return ret;
4318fac63186SHoratiu Vultur 
4319fac63186SHoratiu Vultur 	/* Disable interrupt */
4320fac63186SHoratiu Vultur 	return phy_modify_mmd(phydev, 2, LAN8841_PTP_INT_EN,
4321fac63186SHoratiu Vultur 			      LAN8841_PTP_INT_EN_PTP_GPIO_CAP_EN,
4322fac63186SHoratiu Vultur 			      0);
4323fac63186SHoratiu Vultur }
4324fac63186SHoratiu Vultur 
4325fac63186SHoratiu Vultur static int lan8841_ptp_extts(struct ptp_clock_info *ptp,
4326fac63186SHoratiu Vultur 			     struct ptp_clock_request *rq, int on)
4327fac63186SHoratiu Vultur {
4328fac63186SHoratiu Vultur 	struct kszphy_ptp_priv *ptp_priv = container_of(ptp, struct kszphy_ptp_priv,
4329fac63186SHoratiu Vultur 							ptp_clock_info);
4330fac63186SHoratiu Vultur 	int pin;
4331fac63186SHoratiu Vultur 	int ret;
4332fac63186SHoratiu Vultur 
4333fac63186SHoratiu Vultur 	/* Reject requests with unsupported flags */
4334fac63186SHoratiu Vultur 	if (rq->extts.flags & ~(PTP_ENABLE_FEATURE |
4335fac63186SHoratiu Vultur 				PTP_EXTTS_EDGES |
4336fac63186SHoratiu Vultur 				PTP_STRICT_FLAGS))
4337fac63186SHoratiu Vultur 		return -EOPNOTSUPP;
4338fac63186SHoratiu Vultur 
4339fac63186SHoratiu Vultur 	pin = ptp_find_pin(ptp_priv->ptp_clock, PTP_PF_EXTTS, rq->extts.index);
4340fac63186SHoratiu Vultur 	if (pin == -1 || pin >= LAN8841_PTP_GPIO_NUM)
4341fac63186SHoratiu Vultur 		return -EINVAL;
4342fac63186SHoratiu Vultur 
4343fac63186SHoratiu Vultur 	mutex_lock(&ptp_priv->ptp_lock);
4344fac63186SHoratiu Vultur 	if (on)
4345fac63186SHoratiu Vultur 		ret = lan8841_ptp_extts_on(ptp_priv, pin, rq->extts.flags);
4346fac63186SHoratiu Vultur 	else
4347fac63186SHoratiu Vultur 		ret = lan8841_ptp_extts_off(ptp_priv, pin);
4348fac63186SHoratiu Vultur 	mutex_unlock(&ptp_priv->ptp_lock);
4349fac63186SHoratiu Vultur 
4350fac63186SHoratiu Vultur 	return ret;
4351fac63186SHoratiu Vultur }
4352fac63186SHoratiu Vultur 
4353e4ed8ba0SHoratiu Vultur static int lan8841_ptp_enable(struct ptp_clock_info *ptp,
4354e4ed8ba0SHoratiu Vultur 			      struct ptp_clock_request *rq, int on)
4355e4ed8ba0SHoratiu Vultur {
4356e4ed8ba0SHoratiu Vultur 	switch (rq->type) {
4357fac63186SHoratiu Vultur 	case PTP_CLK_REQ_EXTTS:
4358fac63186SHoratiu Vultur 		return lan8841_ptp_extts(ptp, rq, on);
4359e4ed8ba0SHoratiu Vultur 	case PTP_CLK_REQ_PEROUT:
4360e4ed8ba0SHoratiu Vultur 		return lan8841_ptp_perout(ptp, rq, on);
4361e4ed8ba0SHoratiu Vultur 	default:
4362e4ed8ba0SHoratiu Vultur 		return -EOPNOTSUPP;
4363e4ed8ba0SHoratiu Vultur 	}
4364e4ed8ba0SHoratiu Vultur 
4365e4ed8ba0SHoratiu Vultur 	return 0;
4366e4ed8ba0SHoratiu Vultur }
4367e4ed8ba0SHoratiu Vultur 
4368cafc3662SHoratiu Vultur static struct ptp_clock_info lan8841_ptp_clock_info = {
4369cafc3662SHoratiu Vultur 	.owner		= THIS_MODULE,
4370cafc3662SHoratiu Vultur 	.name		= "lan8841 ptp",
4371cafc3662SHoratiu Vultur 	.max_adj	= 31249999,
4372cafc3662SHoratiu Vultur 	.gettime64	= lan8841_ptp_gettime64,
4373cafc3662SHoratiu Vultur 	.settime64	= lan8841_ptp_settime64,
4374cafc3662SHoratiu Vultur 	.adjtime	= lan8841_ptp_adjtime,
4375cafc3662SHoratiu Vultur 	.adjfine	= lan8841_ptp_adjfine,
4376e4ed8ba0SHoratiu Vultur 	.verify         = lan8841_ptp_verify,
4377e4ed8ba0SHoratiu Vultur 	.enable         = lan8841_ptp_enable,
4378e4ed8ba0SHoratiu Vultur 	.n_per_out      = LAN8841_PTP_GPIO_NUM,
4379fac63186SHoratiu Vultur 	.n_ext_ts       = LAN8841_PTP_GPIO_NUM,
4380e4ed8ba0SHoratiu Vultur 	.n_pins         = LAN8841_PTP_GPIO_NUM,
4381cafc3662SHoratiu Vultur };
4382cafc3662SHoratiu Vultur 
4383a8f1a19dSHoratiu Vultur #define LAN8841_OPERATION_MODE_STRAP_LOW_REGISTER 3
4384a8f1a19dSHoratiu Vultur #define LAN8841_OPERATION_MODE_STRAP_LOW_REGISTER_STRAP_RGMII_EN BIT(0)
4385a8f1a19dSHoratiu Vultur 
4386a8f1a19dSHoratiu Vultur static int lan8841_probe(struct phy_device *phydev)
4387a8f1a19dSHoratiu Vultur {
4388cafc3662SHoratiu Vultur 	struct kszphy_ptp_priv *ptp_priv;
4389cafc3662SHoratiu Vultur 	struct kszphy_priv *priv;
4390a8f1a19dSHoratiu Vultur 	int err;
4391a8f1a19dSHoratiu Vultur 
4392a8f1a19dSHoratiu Vultur 	err = kszphy_probe(phydev);
4393a8f1a19dSHoratiu Vultur 	if (err)
4394a8f1a19dSHoratiu Vultur 		return err;
4395a8f1a19dSHoratiu Vultur 
4396a8f1a19dSHoratiu Vultur 	if (phy_read_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
4397a8f1a19dSHoratiu Vultur 			 LAN8841_OPERATION_MODE_STRAP_LOW_REGISTER) &
4398a8f1a19dSHoratiu Vultur 	    LAN8841_OPERATION_MODE_STRAP_LOW_REGISTER_STRAP_RGMII_EN)
4399a8f1a19dSHoratiu Vultur 		phydev->interface = PHY_INTERFACE_MODE_RGMII_RXID;
4400a8f1a19dSHoratiu Vultur 
4401cafc3662SHoratiu Vultur 	/* Register the clock */
4402cafc3662SHoratiu Vultur 	if (!IS_ENABLED(CONFIG_NETWORK_PHY_TIMESTAMPING))
4403cafc3662SHoratiu Vultur 		return 0;
4404cafc3662SHoratiu Vultur 
4405cafc3662SHoratiu Vultur 	priv = phydev->priv;
4406cafc3662SHoratiu Vultur 	ptp_priv = &priv->ptp_priv;
4407cafc3662SHoratiu Vultur 
4408e4ed8ba0SHoratiu Vultur 	ptp_priv->pin_config = devm_kcalloc(&phydev->mdio.dev,
4409e4ed8ba0SHoratiu Vultur 					    LAN8841_PTP_GPIO_NUM,
4410e4ed8ba0SHoratiu Vultur 					    sizeof(*ptp_priv->pin_config),
4411e4ed8ba0SHoratiu Vultur 					    GFP_KERNEL);
4412e4ed8ba0SHoratiu Vultur 	if (!ptp_priv->pin_config)
4413e4ed8ba0SHoratiu Vultur 		return -ENOMEM;
4414e4ed8ba0SHoratiu Vultur 
4415e4ed8ba0SHoratiu Vultur 	for (int i = 0; i < LAN8841_PTP_GPIO_NUM; ++i) {
4416e4ed8ba0SHoratiu Vultur 		struct ptp_pin_desc *p = &ptp_priv->pin_config[i];
4417e4ed8ba0SHoratiu Vultur 
4418e4ed8ba0SHoratiu Vultur 		snprintf(p->name, sizeof(p->name), "pin%d", i);
4419e4ed8ba0SHoratiu Vultur 		p->index = i;
4420e4ed8ba0SHoratiu Vultur 		p->func = PTP_PF_NONE;
4421e4ed8ba0SHoratiu Vultur 	}
4422e4ed8ba0SHoratiu Vultur 
4423cafc3662SHoratiu Vultur 	ptp_priv->ptp_clock_info = lan8841_ptp_clock_info;
4424e4ed8ba0SHoratiu Vultur 	ptp_priv->ptp_clock_info.pin_config = ptp_priv->pin_config;
4425cafc3662SHoratiu Vultur 	ptp_priv->ptp_clock = ptp_clock_register(&ptp_priv->ptp_clock_info,
4426cafc3662SHoratiu Vultur 						 &phydev->mdio.dev);
4427cafc3662SHoratiu Vultur 	if (IS_ERR(ptp_priv->ptp_clock)) {
4428cafc3662SHoratiu Vultur 		phydev_err(phydev, "ptp_clock_register failed: %lu\n",
4429cafc3662SHoratiu Vultur 			   PTR_ERR(ptp_priv->ptp_clock));
4430cafc3662SHoratiu Vultur 		return -EINVAL;
4431cafc3662SHoratiu Vultur 	}
4432cafc3662SHoratiu Vultur 
4433cafc3662SHoratiu Vultur 	if (!ptp_priv->ptp_clock)
4434cafc3662SHoratiu Vultur 		return 0;
4435cafc3662SHoratiu Vultur 
4436cafc3662SHoratiu Vultur 	/* Initialize the SW */
4437cafc3662SHoratiu Vultur 	skb_queue_head_init(&ptp_priv->tx_queue);
4438cafc3662SHoratiu Vultur 	skb_queue_head_init(&ptp_priv->rx_queue);
4439cafc3662SHoratiu Vultur 	INIT_LIST_HEAD(&ptp_priv->rx_ts_list);
4440cafc3662SHoratiu Vultur 	spin_lock_init(&ptp_priv->rx_ts_lock);
4441cafc3662SHoratiu Vultur 	ptp_priv->phydev = phydev;
4442cafc3662SHoratiu Vultur 	mutex_init(&ptp_priv->ptp_lock);
4443cafc3662SHoratiu Vultur 
4444cafc3662SHoratiu Vultur 	ptp_priv->mii_ts.rxtstamp = lan8814_rxtstamp;
4445cafc3662SHoratiu Vultur 	ptp_priv->mii_ts.txtstamp = lan8814_txtstamp;
4446cafc3662SHoratiu Vultur 	ptp_priv->mii_ts.hwtstamp = lan8841_hwtstamp;
4447cafc3662SHoratiu Vultur 	ptp_priv->mii_ts.ts_info = lan8841_ts_info;
4448cafc3662SHoratiu Vultur 
4449cafc3662SHoratiu Vultur 	phydev->mii_ts = &ptp_priv->mii_ts;
4450cafc3662SHoratiu Vultur 
4451a8f1a19dSHoratiu Vultur 	return 0;
4452a8f1a19dSHoratiu Vultur }
4453a8f1a19dSHoratiu Vultur 
4454d5bf9071SChristian Hohnstaedt static struct phy_driver ksphy_driver[] = {
4455d5bf9071SChristian Hohnstaedt {
445651f932c4SChoi, David 	.phy_id		= PHY_ID_KS8737,
4457f893a99eSFabio Estevam 	.phy_id_mask	= MICREL_PHY_ID_MASK,
445851f932c4SChoi, David 	.name		= "Micrel KS8737",
4459dcdecdcfSHeiner Kallweit 	/* PHY_BASIC_FEATURES */
4460c6f9575cSJohan Hovold 	.driver_data	= &ks8737_type,
446115f03ffeSFabio Estevam 	.probe		= kszphy_probe,
4462d0507009SDavid J. Choi 	.config_init	= kszphy_config_init,
4463c6f9575cSJohan Hovold 	.config_intr	= kszphy_config_intr,
446459ca4e58SIoana Ciornei 	.handle_interrupt = kszphy_handle_interrupt,
4465f1131b9cSClaudiu Beznea 	.suspend	= kszphy_suspend,
4466f1131b9cSClaudiu Beznea 	.resume		= kszphy_resume,
4467d5bf9071SChristian Hohnstaedt }, {
4468212ea99aSMarek Vasut 	.phy_id		= PHY_ID_KSZ8021,
4469212ea99aSMarek Vasut 	.phy_id_mask	= 0x00ffffff,
44707ab59dc1SDavid J. Choi 	.name		= "Micrel KSZ8021 or KSZ8031",
4471dcdecdcfSHeiner Kallweit 	/* PHY_BASIC_FEATURES */
4472e6a423a8SJohan Hovold 	.driver_data	= &ksz8021_type,
447363f44b2bSJohan Hovold 	.probe		= kszphy_probe,
4474d0e1df9cSJohan Hovold 	.config_init	= kszphy_config_init,
4475212ea99aSMarek Vasut 	.config_intr	= kszphy_config_intr,
447659ca4e58SIoana Ciornei 	.handle_interrupt = kszphy_handle_interrupt,
44772b2427d0SAndrew Lunn 	.get_sset_count = kszphy_get_sset_count,
44782b2427d0SAndrew Lunn 	.get_strings	= kszphy_get_strings,
44792b2427d0SAndrew Lunn 	.get_stats	= kszphy_get_stats,
4480f1131b9cSClaudiu Beznea 	.suspend	= kszphy_suspend,
4481f1131b9cSClaudiu Beznea 	.resume		= kszphy_resume,
4482212ea99aSMarek Vasut }, {
4483b818d1a7SHector Palacios 	.phy_id		= PHY_ID_KSZ8031,
4484b818d1a7SHector Palacios 	.phy_id_mask	= 0x00ffffff,
4485b818d1a7SHector Palacios 	.name		= "Micrel KSZ8031",
4486dcdecdcfSHeiner Kallweit 	/* PHY_BASIC_FEATURES */
4487e6a423a8SJohan Hovold 	.driver_data	= &ksz8021_type,
448863f44b2bSJohan Hovold 	.probe		= kszphy_probe,
4489d0e1df9cSJohan Hovold 	.config_init	= kszphy_config_init,
4490b818d1a7SHector Palacios 	.config_intr	= kszphy_config_intr,
449159ca4e58SIoana Ciornei 	.handle_interrupt = kszphy_handle_interrupt,
44922b2427d0SAndrew Lunn 	.get_sset_count = kszphy_get_sset_count,
44932b2427d0SAndrew Lunn 	.get_strings	= kszphy_get_strings,
44942b2427d0SAndrew Lunn 	.get_stats	= kszphy_get_stats,
4495f1131b9cSClaudiu Beznea 	.suspend	= kszphy_suspend,
4496f1131b9cSClaudiu Beznea 	.resume		= kszphy_resume,
4497b818d1a7SHector Palacios }, {
4498510d573fSMarek Vasut 	.phy_id		= PHY_ID_KSZ8041,
4499f893a99eSFabio Estevam 	.phy_id_mask	= MICREL_PHY_ID_MASK,
4500510d573fSMarek Vasut 	.name		= "Micrel KSZ8041",
4501dcdecdcfSHeiner Kallweit 	/* PHY_BASIC_FEATURES */
4502e6a423a8SJohan Hovold 	.driver_data	= &ksz8041_type,
4503e6a423a8SJohan Hovold 	.probe		= kszphy_probe,
450477501a79SPhilipp Zabel 	.config_init	= ksz8041_config_init,
450577501a79SPhilipp Zabel 	.config_aneg	= ksz8041_config_aneg,
450651f932c4SChoi, David 	.config_intr	= kszphy_config_intr,
450759ca4e58SIoana Ciornei 	.handle_interrupt = kszphy_handle_interrupt,
45082b2427d0SAndrew Lunn 	.get_sset_count = kszphy_get_sset_count,
45092b2427d0SAndrew Lunn 	.get_strings	= kszphy_get_strings,
45102b2427d0SAndrew Lunn 	.get_stats	= kszphy_get_stats,
45112641b62dSStefan Agner 	/* No suspend/resume callbacks because of errata DS80000700A,
45122641b62dSStefan Agner 	 * receiver error following software power down.
45132641b62dSStefan Agner 	 */
4514d5bf9071SChristian Hohnstaedt }, {
45154bd7b512SSergei Shtylyov 	.phy_id		= PHY_ID_KSZ8041RNLI,
4516f893a99eSFabio Estevam 	.phy_id_mask	= MICREL_PHY_ID_MASK,
45174bd7b512SSergei Shtylyov 	.name		= "Micrel KSZ8041RNLI",
4518dcdecdcfSHeiner Kallweit 	/* PHY_BASIC_FEATURES */
4519e6a423a8SJohan Hovold 	.driver_data	= &ksz8041_type,
4520e6a423a8SJohan Hovold 	.probe		= kszphy_probe,
4521e6a423a8SJohan Hovold 	.config_init	= kszphy_config_init,
45224bd7b512SSergei Shtylyov 	.config_intr	= kszphy_config_intr,
452359ca4e58SIoana Ciornei 	.handle_interrupt = kszphy_handle_interrupt,
45242b2427d0SAndrew Lunn 	.get_sset_count = kszphy_get_sset_count,
45252b2427d0SAndrew Lunn 	.get_strings	= kszphy_get_strings,
45262b2427d0SAndrew Lunn 	.get_stats	= kszphy_get_stats,
4527f1131b9cSClaudiu Beznea 	.suspend	= kszphy_suspend,
4528f1131b9cSClaudiu Beznea 	.resume		= kszphy_resume,
45294bd7b512SSergei Shtylyov }, {
4530510d573fSMarek Vasut 	.name		= "Micrel KSZ8051",
4531dcdecdcfSHeiner Kallweit 	/* PHY_BASIC_FEATURES */
4532e6a423a8SJohan Hovold 	.driver_data	= &ksz8051_type,
4533e6a423a8SJohan Hovold 	.probe		= kszphy_probe,
453463f44b2bSJohan Hovold 	.config_init	= kszphy_config_init,
453551f932c4SChoi, David 	.config_intr	= kszphy_config_intr,
453659ca4e58SIoana Ciornei 	.handle_interrupt = kszphy_handle_interrupt,
45372b2427d0SAndrew Lunn 	.get_sset_count = kszphy_get_sset_count,
45382b2427d0SAndrew Lunn 	.get_strings	= kszphy_get_strings,
45392b2427d0SAndrew Lunn 	.get_stats	= kszphy_get_stats,
45408b95599cSMarek Vasut 	.match_phy_device = ksz8051_match_phy_device,
4541f1131b9cSClaudiu Beznea 	.suspend	= kszphy_suspend,
4542f1131b9cSClaudiu Beznea 	.resume		= kszphy_resume,
4543d5bf9071SChristian Hohnstaedt }, {
4544510d573fSMarek Vasut 	.phy_id		= PHY_ID_KSZ8001,
4545510d573fSMarek Vasut 	.name		= "Micrel KSZ8001 or KS8721",
4546ecd5a323SAlexander Stein 	.phy_id_mask	= 0x00fffffc,
4547dcdecdcfSHeiner Kallweit 	/* PHY_BASIC_FEATURES */
4548e6a423a8SJohan Hovold 	.driver_data	= &ksz8041_type,
4549e6a423a8SJohan Hovold 	.probe		= kszphy_probe,
4550e6a423a8SJohan Hovold 	.config_init	= kszphy_config_init,
455151f932c4SChoi, David 	.config_intr	= kszphy_config_intr,
455259ca4e58SIoana Ciornei 	.handle_interrupt = kszphy_handle_interrupt,
45532b2427d0SAndrew Lunn 	.get_sset_count = kszphy_get_sset_count,
45542b2427d0SAndrew Lunn 	.get_strings	= kszphy_get_strings,
45552b2427d0SAndrew Lunn 	.get_stats	= kszphy_get_stats,
4556f1131b9cSClaudiu Beznea 	.suspend	= kszphy_suspend,
4557f1131b9cSClaudiu Beznea 	.resume		= kszphy_resume,
4558d5bf9071SChristian Hohnstaedt }, {
45597ab59dc1SDavid J. Choi 	.phy_id		= PHY_ID_KSZ8081,
45607ab59dc1SDavid J. Choi 	.name		= "Micrel KSZ8081 or KSZ8091",
4561f893a99eSFabio Estevam 	.phy_id_mask	= MICREL_PHY_ID_MASK,
456249011e0cSOleksij Rempel 	.flags		= PHY_POLL_CABLE_TEST,
4563dcdecdcfSHeiner Kallweit 	/* PHY_BASIC_FEATURES */
4564e6a423a8SJohan Hovold 	.driver_data	= &ksz8081_type,
4565e6a423a8SJohan Hovold 	.probe		= kszphy_probe,
45667a1d8390SAntoine Tenart 	.config_init	= ksz8081_config_init,
4567764d31caSChristian Melki 	.soft_reset	= genphy_soft_reset,
4568f873f112SOleksij Rempel 	.config_aneg	= ksz8081_config_aneg,
4569f873f112SOleksij Rempel 	.read_status	= ksz8081_read_status,
45707ab59dc1SDavid J. Choi 	.config_intr	= kszphy_config_intr,
457159ca4e58SIoana Ciornei 	.handle_interrupt = kszphy_handle_interrupt,
45722b2427d0SAndrew Lunn 	.get_sset_count = kszphy_get_sset_count,
45732b2427d0SAndrew Lunn 	.get_strings	= kszphy_get_strings,
45742b2427d0SAndrew Lunn 	.get_stats	= kszphy_get_stats,
4575836384d2SWenyou Yang 	.suspend	= kszphy_suspend,
4576f5aba91dSAlexandre Belloni 	.resume		= kszphy_resume,
457749011e0cSOleksij Rempel 	.cable_test_start	= ksz886x_cable_test_start,
457849011e0cSOleksij Rempel 	.cable_test_get_status	= ksz886x_cable_test_get_status,
45797ab59dc1SDavid J. Choi }, {
45807ab59dc1SDavid J. Choi 	.phy_id		= PHY_ID_KSZ8061,
45817ab59dc1SDavid J. Choi 	.name		= "Micrel KSZ8061",
4582f893a99eSFabio Estevam 	.phy_id_mask	= MICREL_PHY_ID_MASK,
4583dcdecdcfSHeiner Kallweit 	/* PHY_BASIC_FEATURES */
45848e6004dfSFabio Estevam 	.probe		= kszphy_probe,
4585232ba3a5SRajasingh Thavamani 	.config_init	= ksz8061_config_init,
45867ab59dc1SDavid J. Choi 	.config_intr	= kszphy_config_intr,
458759ca4e58SIoana Ciornei 	.handle_interrupt = kszphy_handle_interrupt,
45888e6004dfSFabio Estevam 	.suspend	= kszphy_suspend,
45898e6004dfSFabio Estevam 	.resume		= kszphy_resume,
45907ab59dc1SDavid J. Choi }, {
4591d0507009SDavid J. Choi 	.phy_id		= PHY_ID_KSZ9021,
459248d7d0adSJason Wang 	.phy_id_mask	= 0x000ffffe,
4593d0507009SDavid J. Choi 	.name		= "Micrel KSZ9021 Gigabit PHY",
4594dcdecdcfSHeiner Kallweit 	/* PHY_GBIT_FEATURES */
4595c6f9575cSJohan Hovold 	.driver_data	= &ksz9021_type,
4596bfe72442SGrygorii Strashko 	.probe		= kszphy_probe,
4597407d8098SHans Andersson 	.get_features	= ksz9031_get_features,
4598954c3967SSean Cross 	.config_init	= ksz9021_config_init,
4599c6f9575cSJohan Hovold 	.config_intr	= kszphy_config_intr,
460059ca4e58SIoana Ciornei 	.handle_interrupt = kszphy_handle_interrupt,
46012b2427d0SAndrew Lunn 	.get_sset_count = kszphy_get_sset_count,
46022b2427d0SAndrew Lunn 	.get_strings	= kszphy_get_strings,
46032b2427d0SAndrew Lunn 	.get_stats	= kszphy_get_stats,
4604f1131b9cSClaudiu Beznea 	.suspend	= kszphy_suspend,
4605f1131b9cSClaudiu Beznea 	.resume		= kszphy_resume,
4606c846a2b7SKevin Hao 	.read_mmd	= genphy_read_mmd_unsupported,
4607c846a2b7SKevin Hao 	.write_mmd	= genphy_write_mmd_unsupported,
460893272e07SJean-Christophe PLAGNIOL-VILLARD }, {
46097ab59dc1SDavid J. Choi 	.phy_id		= PHY_ID_KSZ9031,
4610f893a99eSFabio Estevam 	.phy_id_mask	= MICREL_PHY_ID_MASK,
46117ab59dc1SDavid J. Choi 	.name		= "Micrel KSZ9031 Gigabit PHY",
461258389c00SMarek Vasut 	.flags		= PHY_POLL_CABLE_TEST,
4613c6f9575cSJohan Hovold 	.driver_data	= &ksz9021_type,
4614bfe72442SGrygorii Strashko 	.probe		= kszphy_probe,
46153aed3e2aSAntoine Tenart 	.get_features	= ksz9031_get_features,
46166e4b8273SHubert Chaumette 	.config_init	= ksz9031_config_init,
46171d16073aSHeiner Kallweit 	.soft_reset	= genphy_soft_reset,
4618d2fd719bSNathan Sullivan 	.read_status	= ksz9031_read_status,
4619c6f9575cSJohan Hovold 	.config_intr	= kszphy_config_intr,
462059ca4e58SIoana Ciornei 	.handle_interrupt = kszphy_handle_interrupt,
46212b2427d0SAndrew Lunn 	.get_sset_count = kszphy_get_sset_count,
46222b2427d0SAndrew Lunn 	.get_strings	= kszphy_get_strings,
46232b2427d0SAndrew Lunn 	.get_stats	= kszphy_get_stats,
4624f1131b9cSClaudiu Beznea 	.suspend	= kszphy_suspend,
4625f64f1482SXander Huff 	.resume		= kszphy_resume,
462658389c00SMarek Vasut 	.cable_test_start	= ksz9x31_cable_test_start,
462758389c00SMarek Vasut 	.cable_test_get_status	= ksz9x31_cable_test_get_status,
46287ab59dc1SDavid J. Choi }, {
46291623ad8eSDivya Koppera 	.phy_id		= PHY_ID_LAN8814,
46301623ad8eSDivya Koppera 	.phy_id_mask	= MICREL_PHY_ID_MASK,
46311623ad8eSDivya Koppera 	.name		= "Microchip INDY Gigabit Quad PHY",
463221b688daSDivya Koppera 	.flags          = PHY_POLL_CABLE_TEST,
46337467d716SHoratiu Vultur 	.config_init	= lan8814_config_init,
4634a516b7f7SDivya Koppera 	.driver_data	= &lan8814_type,
4635ece19502SDivya Koppera 	.probe		= lan8814_probe,
46361623ad8eSDivya Koppera 	.soft_reset	= genphy_soft_reset,
4637b814403aSHoratiu Vultur 	.read_status	= ksz9031_read_status,
46381623ad8eSDivya Koppera 	.get_sset_count	= kszphy_get_sset_count,
46391623ad8eSDivya Koppera 	.get_strings	= kszphy_get_strings,
46401623ad8eSDivya Koppera 	.get_stats	= kszphy_get_stats,
46411623ad8eSDivya Koppera 	.suspend	= genphy_suspend,
46421623ad8eSDivya Koppera 	.resume		= kszphy_resume,
4643b3ec7248SDivya Koppera 	.config_intr	= lan8814_config_intr,
4644b3ec7248SDivya Koppera 	.handle_interrupt = lan8814_handle_interrupt,
464521b688daSDivya Koppera 	.cable_test_start	= lan8814_cable_test_start,
464621b688daSDivya Koppera 	.cable_test_get_status	= ksz886x_cable_test_get_status,
46471623ad8eSDivya Koppera }, {
46487c2dcfa2SHoratiu Vultur 	.phy_id		= PHY_ID_LAN8804,
46497c2dcfa2SHoratiu Vultur 	.phy_id_mask	= MICREL_PHY_ID_MASK,
46507c2dcfa2SHoratiu Vultur 	.name		= "Microchip LAN966X Gigabit PHY",
46517c2dcfa2SHoratiu Vultur 	.config_init	= lan8804_config_init,
46527c2dcfa2SHoratiu Vultur 	.driver_data	= &ksz9021_type,
46537c2dcfa2SHoratiu Vultur 	.probe		= kszphy_probe,
46547c2dcfa2SHoratiu Vultur 	.soft_reset	= genphy_soft_reset,
46557c2dcfa2SHoratiu Vultur 	.read_status	= ksz9031_read_status,
46567c2dcfa2SHoratiu Vultur 	.get_sset_count	= kszphy_get_sset_count,
46577c2dcfa2SHoratiu Vultur 	.get_strings	= kszphy_get_strings,
46587c2dcfa2SHoratiu Vultur 	.get_stats	= kszphy_get_stats,
46597c2dcfa2SHoratiu Vultur 	.suspend	= genphy_suspend,
46607c2dcfa2SHoratiu Vultur 	.resume		= kszphy_resume,
4661b324c6e5SHoratiu Vultur 	.config_intr	= lan8804_config_intr,
4662b324c6e5SHoratiu Vultur 	.handle_interrupt = lan8804_handle_interrupt,
46637c2dcfa2SHoratiu Vultur }, {
4664a8f1a19dSHoratiu Vultur 	.phy_id		= PHY_ID_LAN8841,
4665a8f1a19dSHoratiu Vultur 	.phy_id_mask	= MICREL_PHY_ID_MASK,
4666a8f1a19dSHoratiu Vultur 	.name		= "Microchip LAN8841 Gigabit PHY",
4667a136391aSHoratiu Vultur 	.flags		= PHY_POLL_CABLE_TEST,
4668a8f1a19dSHoratiu Vultur 	.driver_data	= &lan8841_type,
4669a8f1a19dSHoratiu Vultur 	.config_init	= lan8841_config_init,
4670a8f1a19dSHoratiu Vultur 	.probe		= lan8841_probe,
4671a8f1a19dSHoratiu Vultur 	.soft_reset	= genphy_soft_reset,
4672a8f1a19dSHoratiu Vultur 	.config_intr	= lan8841_config_intr,
4673a8f1a19dSHoratiu Vultur 	.handle_interrupt = lan8841_handle_interrupt,
4674a8f1a19dSHoratiu Vultur 	.get_sset_count = kszphy_get_sset_count,
4675a8f1a19dSHoratiu Vultur 	.get_strings	= kszphy_get_strings,
4676a8f1a19dSHoratiu Vultur 	.get_stats	= kszphy_get_stats,
4677a8f1a19dSHoratiu Vultur 	.suspend	= genphy_suspend,
4678a8f1a19dSHoratiu Vultur 	.resume		= genphy_resume,
4679a136391aSHoratiu Vultur 	.cable_test_start	= lan8814_cable_test_start,
4680a136391aSHoratiu Vultur 	.cable_test_get_status	= ksz886x_cable_test_get_status,
4681a8f1a19dSHoratiu Vultur }, {
4682bff5b4b3SYuiko Oshino 	.phy_id		= PHY_ID_KSZ9131,
4683bff5b4b3SYuiko Oshino 	.phy_id_mask	= MICREL_PHY_ID_MASK,
4684bff5b4b3SYuiko Oshino 	.name		= "Microchip KSZ9131 Gigabit PHY",
4685dcdecdcfSHeiner Kallweit 	/* PHY_GBIT_FEATURES */
468658389c00SMarek Vasut 	.flags		= PHY_POLL_CABLE_TEST,
4687a8f1a19dSHoratiu Vultur 	.driver_data	= &ksz9131_type,
4688bff5b4b3SYuiko Oshino 	.probe		= kszphy_probe,
4689bff5b4b3SYuiko Oshino 	.config_init	= ksz9131_config_init,
4690bff5b4b3SYuiko Oshino 	.config_intr	= kszphy_config_intr,
4691b64e6a87SRaju Lakkaraju 	.config_aneg	= ksz9131_config_aneg,
4692b64e6a87SRaju Lakkaraju 	.read_status	= ksz9131_read_status,
469359ca4e58SIoana Ciornei 	.handle_interrupt = kszphy_handle_interrupt,
4694bff5b4b3SYuiko Oshino 	.get_sset_count = kszphy_get_sset_count,
4695bff5b4b3SYuiko Oshino 	.get_strings	= kszphy_get_strings,
4696bff5b4b3SYuiko Oshino 	.get_stats	= kszphy_get_stats,
4697f1131b9cSClaudiu Beznea 	.suspend	= kszphy_suspend,
4698bff5b4b3SYuiko Oshino 	.resume		= kszphy_resume,
469958389c00SMarek Vasut 	.cable_test_start	= ksz9x31_cable_test_start,
470058389c00SMarek Vasut 	.cable_test_get_status	= ksz9x31_cable_test_get_status,
4701f2e9d083SOleksij Rempel 	.get_features	= ksz9477_get_features,
4702bff5b4b3SYuiko Oshino }, {
470393272e07SJean-Christophe PLAGNIOL-VILLARD 	.phy_id		= PHY_ID_KSZ8873MLL,
4704f893a99eSFabio Estevam 	.phy_id_mask	= MICREL_PHY_ID_MASK,
470593272e07SJean-Christophe PLAGNIOL-VILLARD 	.name		= "Micrel KSZ8873MLL Switch",
4706dcdecdcfSHeiner Kallweit 	/* PHY_BASIC_FEATURES */
470793272e07SJean-Christophe PLAGNIOL-VILLARD 	.config_init	= kszphy_config_init,
470893272e07SJean-Christophe PLAGNIOL-VILLARD 	.config_aneg	= ksz8873mll_config_aneg,
470993272e07SJean-Christophe PLAGNIOL-VILLARD 	.read_status	= ksz8873mll_read_status,
47101a5465f5SPatrice Vilchez 	.suspend	= genphy_suspend,
47111a5465f5SPatrice Vilchez 	.resume		= genphy_resume,
47127ab59dc1SDavid J. Choi }, {
47137ab59dc1SDavid J. Choi 	.phy_id		= PHY_ID_KSZ886X,
4714f893a99eSFabio Estevam 	.phy_id_mask	= MICREL_PHY_ID_MASK,
4715ab36a3a2SMarek Vasut 	.name		= "Micrel KSZ8851 Ethernet MAC or KSZ886X Switch",
471621b688daSDivya Koppera 	.driver_data	= &ksz886x_type,
4717dcdecdcfSHeiner Kallweit 	/* PHY_BASIC_FEATURES */
471849011e0cSOleksij Rempel 	.flags		= PHY_POLL_CABLE_TEST,
47197ab59dc1SDavid J. Choi 	.config_init	= kszphy_config_init,
472052939393SOleksij Rempel 	.config_aneg	= ksz886x_config_aneg,
472152939393SOleksij Rempel 	.read_status	= ksz886x_read_status,
47221a5465f5SPatrice Vilchez 	.suspend	= genphy_suspend,
47231a5465f5SPatrice Vilchez 	.resume		= genphy_resume,
472449011e0cSOleksij Rempel 	.cable_test_start	= ksz886x_cable_test_start,
472549011e0cSOleksij Rempel 	.cable_test_get_status	= ksz886x_cable_test_get_status,
47269d162ed6SSean Nyekjaer }, {
47271d951ba3SMarek Vasut 	.name		= "Micrel KSZ87XX Switch",
4728dcdecdcfSHeiner Kallweit 	/* PHY_BASIC_FEATURES */
47299d162ed6SSean Nyekjaer 	.config_init	= kszphy_config_init,
47308b95599cSMarek Vasut 	.match_phy_device = ksz8795_match_phy_device,
47319d162ed6SSean Nyekjaer 	.suspend	= genphy_suspend,
47329d162ed6SSean Nyekjaer 	.resume		= genphy_resume,
4733fc3973a1SWoojung Huh }, {
4734fc3973a1SWoojung Huh 	.phy_id		= PHY_ID_KSZ9477,
4735fc3973a1SWoojung Huh 	.phy_id_mask	= MICREL_PHY_ID_MASK,
4736fc3973a1SWoojung Huh 	.name		= "Microchip KSZ9477",
4737dcdecdcfSHeiner Kallweit 	/* PHY_GBIT_FEATURES */
4738fc3973a1SWoojung Huh 	.config_init	= kszphy_config_init,
4739db45c76bSArun Ramadoss 	.config_intr	= kszphy_config_intr,
4740db45c76bSArun Ramadoss 	.handle_interrupt = kszphy_handle_interrupt,
4741fc3973a1SWoojung Huh 	.suspend	= genphy_suspend,
4742fc3973a1SWoojung Huh 	.resume		= genphy_resume,
474348fb1994SOleksij Rempel 	.get_features	= ksz9477_get_features,
4744d5bf9071SChristian Hohnstaedt } };
4745d0507009SDavid J. Choi 
474650fd7150SJohan Hovold module_phy_driver(ksphy_driver);
4747d0507009SDavid J. Choi 
4748d0507009SDavid J. Choi MODULE_DESCRIPTION("Micrel PHY driver");
4749d0507009SDavid J. Choi MODULE_AUTHOR("David J. Choi");
4750d0507009SDavid J. Choi MODULE_LICENSE("GPL");
475152a60ed2SDavid S. Miller 
4752cf93c945SUwe Kleine-König static struct mdio_device_id __maybe_unused micrel_tbl[] = {
475348d7d0adSJason Wang 	{ PHY_ID_KSZ9021, 0x000ffffe },
4754f893a99eSFabio Estevam 	{ PHY_ID_KSZ9031, MICREL_PHY_ID_MASK },
4755bff5b4b3SYuiko Oshino 	{ PHY_ID_KSZ9131, MICREL_PHY_ID_MASK },
4756ecd5a323SAlexander Stein 	{ PHY_ID_KSZ8001, 0x00fffffc },
4757f893a99eSFabio Estevam 	{ PHY_ID_KS8737, MICREL_PHY_ID_MASK },
4758212ea99aSMarek Vasut 	{ PHY_ID_KSZ8021, 0x00ffffff },
4759b818d1a7SHector Palacios 	{ PHY_ID_KSZ8031, 0x00ffffff },
4760f893a99eSFabio Estevam 	{ PHY_ID_KSZ8041, MICREL_PHY_ID_MASK },
4761f893a99eSFabio Estevam 	{ PHY_ID_KSZ8051, MICREL_PHY_ID_MASK },
4762f893a99eSFabio Estevam 	{ PHY_ID_KSZ8061, MICREL_PHY_ID_MASK },
4763f893a99eSFabio Estevam 	{ PHY_ID_KSZ8081, MICREL_PHY_ID_MASK },
4764f893a99eSFabio Estevam 	{ PHY_ID_KSZ8873MLL, MICREL_PHY_ID_MASK },
4765f893a99eSFabio Estevam 	{ PHY_ID_KSZ886X, MICREL_PHY_ID_MASK },
47661623ad8eSDivya Koppera 	{ PHY_ID_LAN8814, MICREL_PHY_ID_MASK },
47677c2dcfa2SHoratiu Vultur 	{ PHY_ID_LAN8804, MICREL_PHY_ID_MASK },
4768a8f1a19dSHoratiu Vultur 	{ PHY_ID_LAN8841, MICREL_PHY_ID_MASK },
476952a60ed2SDavid S. Miller 	{ }
477052a60ed2SDavid S. Miller };
477152a60ed2SDavid S. Miller 
477252a60ed2SDavid S. Miller MODULE_DEVICE_TABLE(mdio, micrel_tbl);
4773