xref: /openbmc/linux/drivers/net/phy/micrel.c (revision 4a4ce82212ef014d70f486a427005b2b5bab8e34)
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 
95b3ec7248SDivya Koppera /* Lan8814 general Interrupt control/status reg in GPHY specific block. */
96b3ec7248SDivya Koppera #define LAN8814_INTC				0x18
97b3ec7248SDivya Koppera #define LAN8814_INTS				0x1B
98b3ec7248SDivya Koppera 
99b3ec7248SDivya Koppera #define LAN8814_INT_LINK_DOWN			BIT(2)
100b3ec7248SDivya Koppera #define LAN8814_INT_LINK_UP			BIT(0)
101b3ec7248SDivya Koppera #define LAN8814_INT_LINK			(LAN8814_INT_LINK_UP |\
102b3ec7248SDivya Koppera 						 LAN8814_INT_LINK_DOWN)
103b3ec7248SDivya Koppera 
104b3ec7248SDivya Koppera #define LAN8814_INTR_CTRL_REG			0x34
105b3ec7248SDivya Koppera #define LAN8814_INTR_CTRL_REG_POLARITY		BIT(1)
106b3ec7248SDivya Koppera #define LAN8814_INTR_CTRL_REG_INTR_ENABLE	BIT(0)
107b3ec7248SDivya Koppera 
108ece19502SDivya Koppera /* Represents 1ppm adjustment in 2^32 format with
109ece19502SDivya Koppera  * each nsec contains 4 clock cycles.
110ece19502SDivya Koppera  * The value is calculated as following: (1/1000000)/((2^-32)/4)
111ece19502SDivya Koppera  */
112ece19502SDivya Koppera #define LAN8814_1PPM_FORMAT			17179
113ece19502SDivya Koppera 
114ece19502SDivya Koppera #define PTP_RX_MOD				0x024F
115ece19502SDivya Koppera #define PTP_RX_MOD_BAD_UDPV4_CHKSUM_FORCE_FCS_DIS_ BIT(3)
116ece19502SDivya Koppera #define PTP_RX_TIMESTAMP_EN			0x024D
117ece19502SDivya Koppera #define PTP_TX_TIMESTAMP_EN			0x028D
118ece19502SDivya Koppera 
119ece19502SDivya Koppera #define PTP_TIMESTAMP_EN_SYNC_			BIT(0)
120ece19502SDivya Koppera #define PTP_TIMESTAMP_EN_DREQ_			BIT(1)
121ece19502SDivya Koppera #define PTP_TIMESTAMP_EN_PDREQ_			BIT(2)
122ece19502SDivya Koppera #define PTP_TIMESTAMP_EN_PDRES_			BIT(3)
123ece19502SDivya Koppera 
124ece19502SDivya Koppera #define PTP_TX_PARSE_L2_ADDR_EN			0x0284
125ece19502SDivya Koppera #define PTP_RX_PARSE_L2_ADDR_EN			0x0244
126ece19502SDivya Koppera 
127ece19502SDivya Koppera #define PTP_TX_PARSE_IP_ADDR_EN			0x0285
128ece19502SDivya Koppera #define PTP_RX_PARSE_IP_ADDR_EN			0x0245
129ece19502SDivya Koppera #define LTC_HARD_RESET				0x023F
130ece19502SDivya Koppera #define LTC_HARD_RESET_				BIT(0)
131ece19502SDivya Koppera 
132ece19502SDivya Koppera #define TSU_HARD_RESET				0x02C1
133ece19502SDivya Koppera #define TSU_HARD_RESET_				BIT(0)
134ece19502SDivya Koppera 
135ece19502SDivya Koppera #define PTP_CMD_CTL				0x0200
136ece19502SDivya Koppera #define PTP_CMD_CTL_PTP_DISABLE_		BIT(0)
137ece19502SDivya Koppera #define PTP_CMD_CTL_PTP_ENABLE_			BIT(1)
138ece19502SDivya Koppera #define PTP_CMD_CTL_PTP_CLOCK_READ_		BIT(3)
139ece19502SDivya Koppera #define PTP_CMD_CTL_PTP_CLOCK_LOAD_		BIT(4)
140ece19502SDivya Koppera #define PTP_CMD_CTL_PTP_LTC_STEP_SEC_		BIT(5)
141ece19502SDivya Koppera #define PTP_CMD_CTL_PTP_LTC_STEP_NSEC_		BIT(6)
142ece19502SDivya Koppera 
143ece19502SDivya Koppera #define PTP_CLOCK_SET_SEC_MID			0x0206
144ece19502SDivya Koppera #define PTP_CLOCK_SET_SEC_LO			0x0207
145ece19502SDivya Koppera #define PTP_CLOCK_SET_NS_HI			0x0208
146ece19502SDivya Koppera #define PTP_CLOCK_SET_NS_LO			0x0209
147ece19502SDivya Koppera 
148ece19502SDivya Koppera #define PTP_CLOCK_READ_SEC_MID			0x022A
149ece19502SDivya Koppera #define PTP_CLOCK_READ_SEC_LO			0x022B
150ece19502SDivya Koppera #define PTP_CLOCK_READ_NS_HI			0x022C
151ece19502SDivya Koppera #define PTP_CLOCK_READ_NS_LO			0x022D
152ece19502SDivya Koppera 
153ece19502SDivya Koppera #define PTP_OPERATING_MODE			0x0241
154ece19502SDivya Koppera #define PTP_OPERATING_MODE_STANDALONE_		BIT(0)
155ece19502SDivya Koppera 
156ece19502SDivya Koppera #define PTP_TX_MOD				0x028F
157ece19502SDivya Koppera #define PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_	BIT(12)
158ece19502SDivya Koppera #define PTP_TX_MOD_BAD_UDPV4_CHKSUM_FORCE_FCS_DIS_ BIT(3)
159ece19502SDivya Koppera 
160ece19502SDivya Koppera #define PTP_RX_PARSE_CONFIG			0x0242
161ece19502SDivya Koppera #define PTP_RX_PARSE_CONFIG_LAYER2_EN_		BIT(0)
162ece19502SDivya Koppera #define PTP_RX_PARSE_CONFIG_IPV4_EN_		BIT(1)
163ece19502SDivya Koppera #define PTP_RX_PARSE_CONFIG_IPV6_EN_		BIT(2)
164ece19502SDivya Koppera 
165ece19502SDivya Koppera #define PTP_TX_PARSE_CONFIG			0x0282
166ece19502SDivya Koppera #define PTP_TX_PARSE_CONFIG_LAYER2_EN_		BIT(0)
167ece19502SDivya Koppera #define PTP_TX_PARSE_CONFIG_IPV4_EN_		BIT(1)
168ece19502SDivya Koppera #define PTP_TX_PARSE_CONFIG_IPV6_EN_		BIT(2)
169ece19502SDivya Koppera 
170ece19502SDivya Koppera #define PTP_CLOCK_RATE_ADJ_HI			0x020C
171ece19502SDivya Koppera #define PTP_CLOCK_RATE_ADJ_LO			0x020D
172ece19502SDivya Koppera #define PTP_CLOCK_RATE_ADJ_DIR_			BIT(15)
173ece19502SDivya Koppera 
174ece19502SDivya Koppera #define PTP_LTC_STEP_ADJ_HI			0x0212
175ece19502SDivya Koppera #define PTP_LTC_STEP_ADJ_LO			0x0213
176ece19502SDivya Koppera #define PTP_LTC_STEP_ADJ_DIR_			BIT(15)
177ece19502SDivya Koppera 
178ece19502SDivya Koppera #define LAN8814_INTR_STS_REG			0x0033
179ece19502SDivya Koppera #define LAN8814_INTR_STS_REG_1588_TSU0_		BIT(0)
180ece19502SDivya Koppera #define LAN8814_INTR_STS_REG_1588_TSU1_		BIT(1)
181ece19502SDivya Koppera #define LAN8814_INTR_STS_REG_1588_TSU2_		BIT(2)
182ece19502SDivya Koppera #define LAN8814_INTR_STS_REG_1588_TSU3_		BIT(3)
183ece19502SDivya Koppera 
184ece19502SDivya Koppera #define PTP_CAP_INFO				0x022A
185ece19502SDivya Koppera #define PTP_CAP_INFO_TX_TS_CNT_GET_(reg_val)	(((reg_val) & 0x0f00) >> 8)
186ece19502SDivya Koppera #define PTP_CAP_INFO_RX_TS_CNT_GET_(reg_val)	((reg_val) & 0x000f)
187ece19502SDivya Koppera 
188ece19502SDivya Koppera #define PTP_TX_EGRESS_SEC_HI			0x0296
189ece19502SDivya Koppera #define PTP_TX_EGRESS_SEC_LO			0x0297
190ece19502SDivya Koppera #define PTP_TX_EGRESS_NS_HI			0x0294
191ece19502SDivya Koppera #define PTP_TX_EGRESS_NS_LO			0x0295
192ece19502SDivya Koppera #define PTP_TX_MSG_HEADER2			0x0299
193ece19502SDivya Koppera 
194ece19502SDivya Koppera #define PTP_RX_INGRESS_SEC_HI			0x0256
195ece19502SDivya Koppera #define PTP_RX_INGRESS_SEC_LO			0x0257
196ece19502SDivya Koppera #define PTP_RX_INGRESS_NS_HI			0x0254
197ece19502SDivya Koppera #define PTP_RX_INGRESS_NS_LO			0x0255
198ece19502SDivya Koppera #define PTP_RX_MSG_HEADER2			0x0259
199ece19502SDivya Koppera 
200ece19502SDivya Koppera #define PTP_TSU_INT_EN				0x0200
201ece19502SDivya Koppera #define PTP_TSU_INT_EN_PTP_TX_TS_OVRFL_EN_	BIT(3)
202ece19502SDivya Koppera #define PTP_TSU_INT_EN_PTP_TX_TS_EN_		BIT(2)
203ece19502SDivya Koppera #define PTP_TSU_INT_EN_PTP_RX_TS_OVRFL_EN_	BIT(1)
204ece19502SDivya Koppera #define PTP_TSU_INT_EN_PTP_RX_TS_EN_		BIT(0)
205ece19502SDivya Koppera 
206ece19502SDivya Koppera #define PTP_TSU_INT_STS				0x0201
207ece19502SDivya Koppera #define PTP_TSU_INT_STS_PTP_TX_TS_OVRFL_INT_	BIT(3)
208ece19502SDivya Koppera #define PTP_TSU_INT_STS_PTP_TX_TS_EN_		BIT(2)
209ece19502SDivya Koppera #define PTP_TSU_INT_STS_PTP_RX_TS_OVRFL_INT_	BIT(1)
210ece19502SDivya Koppera #define PTP_TSU_INT_STS_PTP_RX_TS_EN_		BIT(0)
211ece19502SDivya Koppera 
212a516b7f7SDivya Koppera #define LAN8814_LED_CTRL_1			0x0
213a516b7f7SDivya Koppera #define LAN8814_LED_CTRL_1_KSZ9031_LED_MODE_	BIT(6)
214a516b7f7SDivya Koppera 
2155a16778eSJohan Hovold /* PHY Control 1 */
2165a16778eSJohan Hovold #define MII_KSZPHY_CTRL_1			0x1e
217f873f112SOleksij Rempel #define KSZ8081_CTRL1_MDIX_STAT			BIT(4)
2185a16778eSJohan Hovold 
2195a16778eSJohan Hovold /* PHY Control 2 / PHY Control (if no PHY Control 1) */
2205a16778eSJohan Hovold #define MII_KSZPHY_CTRL_2			0x1f
2215a16778eSJohan Hovold #define MII_KSZPHY_CTRL				MII_KSZPHY_CTRL_2
22251f932c4SChoi, David /* bitmap of PHY register to set interrupt mode */
223f873f112SOleksij Rempel #define KSZ8081_CTRL2_HP_MDIX			BIT(15)
224f873f112SOleksij Rempel #define KSZ8081_CTRL2_MDI_MDI_X_SELECT		BIT(14)
225f873f112SOleksij Rempel #define KSZ8081_CTRL2_DISABLE_AUTO_MDIX		BIT(13)
226f873f112SOleksij Rempel #define KSZ8081_CTRL2_FORCE_LINK		BIT(11)
227f873f112SOleksij Rempel #define KSZ8081_CTRL2_POWER_SAVING		BIT(10)
22800aee095SJohan Hovold #define KSZPHY_CTRL_INT_ACTIVE_HIGH		BIT(9)
22963f44b2bSJohan Hovold #define KSZPHY_RMII_REF_CLK_SEL			BIT(7)
23051f932c4SChoi, David 
231954c3967SSean Cross /* Write/read to/from extended registers */
232954c3967SSean Cross #define MII_KSZPHY_EXTREG			0x0b
233954c3967SSean Cross #define KSZPHY_EXTREG_WRITE			0x8000
234954c3967SSean Cross 
235954c3967SSean Cross #define MII_KSZPHY_EXTREG_WRITE			0x0c
236954c3967SSean Cross #define MII_KSZPHY_EXTREG_READ			0x0d
237954c3967SSean Cross 
238954c3967SSean Cross /* Extended registers */
239954c3967SSean Cross #define MII_KSZPHY_CLK_CONTROL_PAD_SKEW		0x104
240954c3967SSean Cross #define MII_KSZPHY_RX_DATA_PAD_SKEW		0x105
241954c3967SSean Cross #define MII_KSZPHY_TX_DATA_PAD_SKEW		0x106
242954c3967SSean Cross 
243954c3967SSean Cross #define PS_TO_REG				200
244ece19502SDivya Koppera #define FIFO_SIZE				8
245954c3967SSean Cross 
2462b2427d0SAndrew Lunn struct kszphy_hw_stat {
2472b2427d0SAndrew Lunn 	const char *string;
2482b2427d0SAndrew Lunn 	u8 reg;
2492b2427d0SAndrew Lunn 	u8 bits;
2502b2427d0SAndrew Lunn };
2512b2427d0SAndrew Lunn 
2522b2427d0SAndrew Lunn static struct kszphy_hw_stat kszphy_hw_stats[] = {
2532b2427d0SAndrew Lunn 	{ "phy_receive_errors", 21, 16},
2542b2427d0SAndrew Lunn 	{ "phy_idle_errors", 10, 8 },
2552b2427d0SAndrew Lunn };
2562b2427d0SAndrew Lunn 
257e6a423a8SJohan Hovold struct kszphy_type {
258e6a423a8SJohan Hovold 	u32 led_mode_reg;
259c6f9575cSJohan Hovold 	u16 interrupt_level_mask;
2600f95903eSJohan Hovold 	bool has_broadcast_disable;
2612b0ba96cSSylvain Rochet 	bool has_nand_tree_disable;
26263f44b2bSJohan Hovold 	bool has_rmii_ref_clk_sel;
263e6a423a8SJohan Hovold };
264e6a423a8SJohan Hovold 
265ece19502SDivya Koppera /* Shared structure between the PHYs of the same package. */
266ece19502SDivya Koppera struct lan8814_shared_priv {
267ece19502SDivya Koppera 	struct phy_device *phydev;
268ece19502SDivya Koppera 	struct ptp_clock *ptp_clock;
269ece19502SDivya Koppera 	struct ptp_clock_info ptp_clock_info;
270ece19502SDivya Koppera 
271ece19502SDivya Koppera 	/* Reference counter to how many ports in the package are enabling the
272ece19502SDivya Koppera 	 * timestamping
273ece19502SDivya Koppera 	 */
274ece19502SDivya Koppera 	u8 ref;
275ece19502SDivya Koppera 
276ece19502SDivya Koppera 	/* Lock for ptp_clock and ref */
277ece19502SDivya Koppera 	struct mutex shared_lock;
278ece19502SDivya Koppera };
279ece19502SDivya Koppera 
280ece19502SDivya Koppera struct lan8814_ptp_rx_ts {
281ece19502SDivya Koppera 	struct list_head list;
282ece19502SDivya Koppera 	u32 seconds;
283ece19502SDivya Koppera 	u32 nsec;
284ece19502SDivya Koppera 	u16 seq_id;
285ece19502SDivya Koppera };
286ece19502SDivya Koppera 
287ece19502SDivya Koppera struct kszphy_ptp_priv {
288ece19502SDivya Koppera 	struct mii_timestamper mii_ts;
289ece19502SDivya Koppera 	struct phy_device *phydev;
290ece19502SDivya Koppera 
291ece19502SDivya Koppera 	struct sk_buff_head tx_queue;
292ece19502SDivya Koppera 	struct sk_buff_head rx_queue;
293ece19502SDivya Koppera 
294ece19502SDivya Koppera 	struct list_head rx_ts_list;
295ece19502SDivya Koppera 	/* Lock for Rx ts fifo */
296ece19502SDivya Koppera 	spinlock_t rx_ts_lock;
297ece19502SDivya Koppera 
298ece19502SDivya Koppera 	int hwts_tx_type;
299ece19502SDivya Koppera 	enum hwtstamp_rx_filters rx_filter;
300ece19502SDivya Koppera 	int layer;
301ece19502SDivya Koppera 	int version;
302ece19502SDivya Koppera };
303ece19502SDivya Koppera 
304e6a423a8SJohan Hovold struct kszphy_priv {
305ece19502SDivya Koppera 	struct kszphy_ptp_priv ptp_priv;
306e6a423a8SJohan Hovold 	const struct kszphy_type *type;
307e7a792e9SJohan Hovold 	int led_mode;
30858389c00SMarek Vasut 	u16 vct_ctrl1000;
30963f44b2bSJohan Hovold 	bool rmii_ref_clk_sel;
31063f44b2bSJohan Hovold 	bool rmii_ref_clk_sel_val;
3112b2427d0SAndrew Lunn 	u64 stats[ARRAY_SIZE(kszphy_hw_stats)];
312e6a423a8SJohan Hovold };
313e6a423a8SJohan Hovold 
314a516b7f7SDivya Koppera static const struct kszphy_type lan8814_type = {
315a516b7f7SDivya Koppera 	.led_mode_reg		= ~LAN8814_LED_CTRL_1,
316a516b7f7SDivya Koppera };
317a516b7f7SDivya Koppera 
318e6a423a8SJohan Hovold static const struct kszphy_type ksz8021_type = {
319e6a423a8SJohan Hovold 	.led_mode_reg		= MII_KSZPHY_CTRL_2,
320d0e1df9cSJohan Hovold 	.has_broadcast_disable	= true,
3212b0ba96cSSylvain Rochet 	.has_nand_tree_disable	= true,
32263f44b2bSJohan Hovold 	.has_rmii_ref_clk_sel	= true,
323e6a423a8SJohan Hovold };
324e6a423a8SJohan Hovold 
325e6a423a8SJohan Hovold static const struct kszphy_type ksz8041_type = {
326e6a423a8SJohan Hovold 	.led_mode_reg		= MII_KSZPHY_CTRL_1,
327e6a423a8SJohan Hovold };
328e6a423a8SJohan Hovold 
329e6a423a8SJohan Hovold static const struct kszphy_type ksz8051_type = {
330e6a423a8SJohan Hovold 	.led_mode_reg		= MII_KSZPHY_CTRL_2,
3312b0ba96cSSylvain Rochet 	.has_nand_tree_disable	= true,
332e6a423a8SJohan Hovold };
333e6a423a8SJohan Hovold 
334e6a423a8SJohan Hovold static const struct kszphy_type ksz8081_type = {
335e6a423a8SJohan Hovold 	.led_mode_reg		= MII_KSZPHY_CTRL_2,
3360f95903eSJohan Hovold 	.has_broadcast_disable	= true,
3372b0ba96cSSylvain Rochet 	.has_nand_tree_disable	= true,
33886dc1342SJohan Hovold 	.has_rmii_ref_clk_sel	= true,
339e6a423a8SJohan Hovold };
340e6a423a8SJohan Hovold 
341c6f9575cSJohan Hovold static const struct kszphy_type ks8737_type = {
342c6f9575cSJohan Hovold 	.interrupt_level_mask	= BIT(14),
343c6f9575cSJohan Hovold };
344c6f9575cSJohan Hovold 
345c6f9575cSJohan Hovold static const struct kszphy_type ksz9021_type = {
346c6f9575cSJohan Hovold 	.interrupt_level_mask	= BIT(14),
347c6f9575cSJohan Hovold };
348c6f9575cSJohan Hovold 
349954c3967SSean Cross static int kszphy_extended_write(struct phy_device *phydev,
350954c3967SSean Cross 				u32 regnum, u16 val)
351954c3967SSean Cross {
352954c3967SSean Cross 	phy_write(phydev, MII_KSZPHY_EXTREG, KSZPHY_EXTREG_WRITE | regnum);
353954c3967SSean Cross 	return phy_write(phydev, MII_KSZPHY_EXTREG_WRITE, val);
354954c3967SSean Cross }
355954c3967SSean Cross 
356954c3967SSean Cross static int kszphy_extended_read(struct phy_device *phydev,
357954c3967SSean Cross 				u32 regnum)
358954c3967SSean Cross {
359954c3967SSean Cross 	phy_write(phydev, MII_KSZPHY_EXTREG, regnum);
360954c3967SSean Cross 	return phy_read(phydev, MII_KSZPHY_EXTREG_READ);
361954c3967SSean Cross }
362954c3967SSean Cross 
36351f932c4SChoi, David static int kszphy_ack_interrupt(struct phy_device *phydev)
36451f932c4SChoi, David {
36551f932c4SChoi, David 	/* bit[7..0] int status, which is a read and clear register. */
36651f932c4SChoi, David 	int rc;
36751f932c4SChoi, David 
36851f932c4SChoi, David 	rc = phy_read(phydev, MII_KSZPHY_INTCS);
36951f932c4SChoi, David 
37051f932c4SChoi, David 	return (rc < 0) ? rc : 0;
37151f932c4SChoi, David }
37251f932c4SChoi, David 
37351f932c4SChoi, David static int kszphy_config_intr(struct phy_device *phydev)
37451f932c4SChoi, David {
375c6f9575cSJohan Hovold 	const struct kszphy_type *type = phydev->drv->driver_data;
376c0c99d0cSIoana Ciornei 	int temp, err;
377c6f9575cSJohan Hovold 	u16 mask;
378c6f9575cSJohan Hovold 
379c6f9575cSJohan Hovold 	if (type && type->interrupt_level_mask)
380c6f9575cSJohan Hovold 		mask = type->interrupt_level_mask;
381c6f9575cSJohan Hovold 	else
382c6f9575cSJohan Hovold 		mask = KSZPHY_CTRL_INT_ACTIVE_HIGH;
38351f932c4SChoi, David 
38451f932c4SChoi, David 	/* set the interrupt pin active low */
38551f932c4SChoi, David 	temp = phy_read(phydev, MII_KSZPHY_CTRL);
3865bb8fc0dSJohan Hovold 	if (temp < 0)
3875bb8fc0dSJohan Hovold 		return temp;
388c6f9575cSJohan Hovold 	temp &= ~mask;
38951f932c4SChoi, David 	phy_write(phydev, MII_KSZPHY_CTRL, temp);
39051f932c4SChoi, David 
391c6f9575cSJohan Hovold 	/* enable / disable interrupts */
392c0c99d0cSIoana Ciornei 	if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
393c0c99d0cSIoana Ciornei 		err = kszphy_ack_interrupt(phydev);
394c0c99d0cSIoana Ciornei 		if (err)
395c0c99d0cSIoana Ciornei 			return err;
39651f932c4SChoi, David 
397c0c99d0cSIoana Ciornei 		temp = KSZPHY_INTCS_ALL;
398c0c99d0cSIoana Ciornei 		err = phy_write(phydev, MII_KSZPHY_INTCS, temp);
399c0c99d0cSIoana Ciornei 	} else {
400c0c99d0cSIoana Ciornei 		temp = 0;
401c0c99d0cSIoana Ciornei 		err = phy_write(phydev, MII_KSZPHY_INTCS, temp);
402c0c99d0cSIoana Ciornei 		if (err)
403c0c99d0cSIoana Ciornei 			return err;
404c0c99d0cSIoana Ciornei 
405c0c99d0cSIoana Ciornei 		err = kszphy_ack_interrupt(phydev);
406c0c99d0cSIoana Ciornei 	}
407c0c99d0cSIoana Ciornei 
408c0c99d0cSIoana Ciornei 	return err;
40951f932c4SChoi, David }
410d0507009SDavid J. Choi 
41159ca4e58SIoana Ciornei static irqreturn_t kszphy_handle_interrupt(struct phy_device *phydev)
41259ca4e58SIoana Ciornei {
41359ca4e58SIoana Ciornei 	int irq_status;
41459ca4e58SIoana Ciornei 
41559ca4e58SIoana Ciornei 	irq_status = phy_read(phydev, MII_KSZPHY_INTCS);
41659ca4e58SIoana Ciornei 	if (irq_status < 0) {
41759ca4e58SIoana Ciornei 		phy_error(phydev);
41859ca4e58SIoana Ciornei 		return IRQ_NONE;
41959ca4e58SIoana Ciornei 	}
42059ca4e58SIoana Ciornei 
421fff4c746SOleksij Rempel 	if (!(irq_status & KSZPHY_INTCS_STATUS))
42259ca4e58SIoana Ciornei 		return IRQ_NONE;
42359ca4e58SIoana Ciornei 
42459ca4e58SIoana Ciornei 	phy_trigger_machine(phydev);
42559ca4e58SIoana Ciornei 
42659ca4e58SIoana Ciornei 	return IRQ_HANDLED;
42759ca4e58SIoana Ciornei }
42859ca4e58SIoana Ciornei 
42963f44b2bSJohan Hovold static int kszphy_rmii_clk_sel(struct phy_device *phydev, bool val)
43063f44b2bSJohan Hovold {
43163f44b2bSJohan Hovold 	int ctrl;
43263f44b2bSJohan Hovold 
43363f44b2bSJohan Hovold 	ctrl = phy_read(phydev, MII_KSZPHY_CTRL);
43463f44b2bSJohan Hovold 	if (ctrl < 0)
43563f44b2bSJohan Hovold 		return ctrl;
43663f44b2bSJohan Hovold 
43763f44b2bSJohan Hovold 	if (val)
43863f44b2bSJohan Hovold 		ctrl |= KSZPHY_RMII_REF_CLK_SEL;
43963f44b2bSJohan Hovold 	else
44063f44b2bSJohan Hovold 		ctrl &= ~KSZPHY_RMII_REF_CLK_SEL;
44163f44b2bSJohan Hovold 
44263f44b2bSJohan Hovold 	return phy_write(phydev, MII_KSZPHY_CTRL, ctrl);
44363f44b2bSJohan Hovold }
44463f44b2bSJohan Hovold 
445e7a792e9SJohan Hovold static int kszphy_setup_led(struct phy_device *phydev, u32 reg, int val)
44620d8435aSBen Dooks {
4475a16778eSJohan Hovold 	int rc, temp, shift;
4488620546cSJohan Hovold 
4495a16778eSJohan Hovold 	switch (reg) {
4505a16778eSJohan Hovold 	case MII_KSZPHY_CTRL_1:
4515a16778eSJohan Hovold 		shift = 14;
4525a16778eSJohan Hovold 		break;
4535a16778eSJohan Hovold 	case MII_KSZPHY_CTRL_2:
4545a16778eSJohan Hovold 		shift = 4;
4555a16778eSJohan Hovold 		break;
4565a16778eSJohan Hovold 	default:
4575a16778eSJohan Hovold 		return -EINVAL;
4585a16778eSJohan Hovold 	}
4595a16778eSJohan Hovold 
46020d8435aSBen Dooks 	temp = phy_read(phydev, reg);
461b7035860SJohan Hovold 	if (temp < 0) {
462b7035860SJohan Hovold 		rc = temp;
463b7035860SJohan Hovold 		goto out;
464b7035860SJohan Hovold 	}
46520d8435aSBen Dooks 
46628bdc499SSergei Shtylyov 	temp &= ~(3 << shift);
46720d8435aSBen Dooks 	temp |= val << shift;
46820d8435aSBen Dooks 	rc = phy_write(phydev, reg, temp);
469b7035860SJohan Hovold out:
470b7035860SJohan Hovold 	if (rc < 0)
47172ba48beSAndrew Lunn 		phydev_err(phydev, "failed to set led mode\n");
47220d8435aSBen Dooks 
473b7035860SJohan Hovold 	return rc;
47420d8435aSBen Dooks }
47520d8435aSBen Dooks 
476bde15129SJohan Hovold /* Disable PHY address 0 as the broadcast address, so that it can be used as a
477bde15129SJohan Hovold  * unique (non-broadcast) address on a shared bus.
478bde15129SJohan Hovold  */
479bde15129SJohan Hovold static int kszphy_broadcast_disable(struct phy_device *phydev)
480bde15129SJohan Hovold {
481bde15129SJohan Hovold 	int ret;
482bde15129SJohan Hovold 
483bde15129SJohan Hovold 	ret = phy_read(phydev, MII_KSZPHY_OMSO);
484bde15129SJohan Hovold 	if (ret < 0)
485bde15129SJohan Hovold 		goto out;
486bde15129SJohan Hovold 
487bde15129SJohan Hovold 	ret = phy_write(phydev, MII_KSZPHY_OMSO, ret | KSZPHY_OMSO_B_CAST_OFF);
488bde15129SJohan Hovold out:
489bde15129SJohan Hovold 	if (ret)
49072ba48beSAndrew Lunn 		phydev_err(phydev, "failed to disable broadcast address\n");
491bde15129SJohan Hovold 
492bde15129SJohan Hovold 	return ret;
493bde15129SJohan Hovold }
494bde15129SJohan Hovold 
4952b0ba96cSSylvain Rochet static int kszphy_nand_tree_disable(struct phy_device *phydev)
4962b0ba96cSSylvain Rochet {
4972b0ba96cSSylvain Rochet 	int ret;
4982b0ba96cSSylvain Rochet 
4992b0ba96cSSylvain Rochet 	ret = phy_read(phydev, MII_KSZPHY_OMSO);
5002b0ba96cSSylvain Rochet 	if (ret < 0)
5012b0ba96cSSylvain Rochet 		goto out;
5022b0ba96cSSylvain Rochet 
5032b0ba96cSSylvain Rochet 	if (!(ret & KSZPHY_OMSO_NAND_TREE_ON))
5042b0ba96cSSylvain Rochet 		return 0;
5052b0ba96cSSylvain Rochet 
5062b0ba96cSSylvain Rochet 	ret = phy_write(phydev, MII_KSZPHY_OMSO,
5072b0ba96cSSylvain Rochet 			ret & ~KSZPHY_OMSO_NAND_TREE_ON);
5082b0ba96cSSylvain Rochet out:
5092b0ba96cSSylvain Rochet 	if (ret)
51072ba48beSAndrew Lunn 		phydev_err(phydev, "failed to disable NAND tree mode\n");
5112b0ba96cSSylvain Rochet 
5122b0ba96cSSylvain Rochet 	return ret;
5132b0ba96cSSylvain Rochet }
5142b0ba96cSSylvain Rochet 
51579e498a9SLeonard Crestez /* Some config bits need to be set again on resume, handle them here. */
51679e498a9SLeonard Crestez static int kszphy_config_reset(struct phy_device *phydev)
51779e498a9SLeonard Crestez {
51879e498a9SLeonard Crestez 	struct kszphy_priv *priv = phydev->priv;
51979e498a9SLeonard Crestez 	int ret;
52079e498a9SLeonard Crestez 
52179e498a9SLeonard Crestez 	if (priv->rmii_ref_clk_sel) {
52279e498a9SLeonard Crestez 		ret = kszphy_rmii_clk_sel(phydev, priv->rmii_ref_clk_sel_val);
52379e498a9SLeonard Crestez 		if (ret) {
52479e498a9SLeonard Crestez 			phydev_err(phydev,
52579e498a9SLeonard Crestez 				   "failed to set rmii reference clock\n");
52679e498a9SLeonard Crestez 			return ret;
52779e498a9SLeonard Crestez 		}
52879e498a9SLeonard Crestez 	}
52979e498a9SLeonard Crestez 
530f2ef6f75SFabio Estevam 	if (priv->type && priv->led_mode >= 0)
53179e498a9SLeonard Crestez 		kszphy_setup_led(phydev, priv->type->led_mode_reg, priv->led_mode);
53279e498a9SLeonard Crestez 
53379e498a9SLeonard Crestez 	return 0;
53479e498a9SLeonard Crestez }
53579e498a9SLeonard Crestez 
536d0507009SDavid J. Choi static int kszphy_config_init(struct phy_device *phydev)
537d0507009SDavid J. Choi {
538e6a423a8SJohan Hovold 	struct kszphy_priv *priv = phydev->priv;
539e6a423a8SJohan Hovold 	const struct kszphy_type *type;
540d0507009SDavid J. Choi 
541e6a423a8SJohan Hovold 	if (!priv)
542e6a423a8SJohan Hovold 		return 0;
543e6a423a8SJohan Hovold 
544e6a423a8SJohan Hovold 	type = priv->type;
545e6a423a8SJohan Hovold 
546f2ef6f75SFabio Estevam 	if (type && type->has_broadcast_disable)
5470f95903eSJohan Hovold 		kszphy_broadcast_disable(phydev);
5480f95903eSJohan Hovold 
549f2ef6f75SFabio Estevam 	if (type && type->has_nand_tree_disable)
5502b0ba96cSSylvain Rochet 		kszphy_nand_tree_disable(phydev);
5512b0ba96cSSylvain Rochet 
55279e498a9SLeonard Crestez 	return kszphy_config_reset(phydev);
55320d8435aSBen Dooks }
55420d8435aSBen Dooks 
5554217a64eSMichael Walle static int ksz8041_fiber_mode(struct phy_device *phydev)
5564217a64eSMichael Walle {
5574217a64eSMichael Walle 	struct device_node *of_node = phydev->mdio.dev.of_node;
5584217a64eSMichael Walle 
5594217a64eSMichael Walle 	return of_property_read_bool(of_node, "micrel,fiber-mode");
5604217a64eSMichael Walle }
5614217a64eSMichael Walle 
56277501a79SPhilipp Zabel static int ksz8041_config_init(struct phy_device *phydev)
56377501a79SPhilipp Zabel {
5643c1bcc86SAndrew Lunn 	__ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
5653c1bcc86SAndrew Lunn 
56677501a79SPhilipp Zabel 	/* Limit supported and advertised modes in fiber mode */
5674217a64eSMichael Walle 	if (ksz8041_fiber_mode(phydev)) {
56877501a79SPhilipp Zabel 		phydev->dev_flags |= MICREL_PHY_FXEN;
5693c1bcc86SAndrew Lunn 		linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, mask);
5703c1bcc86SAndrew Lunn 		linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, mask);
5713c1bcc86SAndrew Lunn 
5723c1bcc86SAndrew Lunn 		linkmode_and(phydev->supported, phydev->supported, mask);
5733c1bcc86SAndrew Lunn 		linkmode_set_bit(ETHTOOL_LINK_MODE_FIBRE_BIT,
5743c1bcc86SAndrew Lunn 				 phydev->supported);
5753c1bcc86SAndrew Lunn 		linkmode_and(phydev->advertising, phydev->advertising, mask);
5763c1bcc86SAndrew Lunn 		linkmode_set_bit(ETHTOOL_LINK_MODE_FIBRE_BIT,
5773c1bcc86SAndrew Lunn 				 phydev->advertising);
57877501a79SPhilipp Zabel 		phydev->autoneg = AUTONEG_DISABLE;
57977501a79SPhilipp Zabel 	}
58077501a79SPhilipp Zabel 
58177501a79SPhilipp Zabel 	return kszphy_config_init(phydev);
58277501a79SPhilipp Zabel }
58377501a79SPhilipp Zabel 
58477501a79SPhilipp Zabel static int ksz8041_config_aneg(struct phy_device *phydev)
58577501a79SPhilipp Zabel {
58677501a79SPhilipp Zabel 	/* Skip auto-negotiation in fiber mode */
58777501a79SPhilipp Zabel 	if (phydev->dev_flags & MICREL_PHY_FXEN) {
58877501a79SPhilipp Zabel 		phydev->speed = SPEED_100;
58977501a79SPhilipp Zabel 		return 0;
59077501a79SPhilipp Zabel 	}
59177501a79SPhilipp Zabel 
59277501a79SPhilipp Zabel 	return genphy_config_aneg(phydev);
59377501a79SPhilipp Zabel }
59477501a79SPhilipp Zabel 
5958b95599cSMarek Vasut static int ksz8051_ksz8795_match_phy_device(struct phy_device *phydev,
596a5e63c7dSSteve Bennett 					    const bool ksz_8051)
5978b95599cSMarek Vasut {
5988b95599cSMarek Vasut 	int ret;
5998b95599cSMarek Vasut 
600a5e63c7dSSteve Bennett 	if ((phydev->phy_id & MICREL_PHY_ID_MASK) != PHY_ID_KSZ8051)
6018b95599cSMarek Vasut 		return 0;
6028b95599cSMarek Vasut 
6038b95599cSMarek Vasut 	ret = phy_read(phydev, MII_BMSR);
6048b95599cSMarek Vasut 	if (ret < 0)
6058b95599cSMarek Vasut 		return ret;
6068b95599cSMarek Vasut 
6078b95599cSMarek Vasut 	/* KSZ8051 PHY and KSZ8794/KSZ8795/KSZ8765 switch share the same
6088b95599cSMarek Vasut 	 * exact PHY ID. However, they can be told apart by the extended
6098b95599cSMarek Vasut 	 * capability registers presence. The KSZ8051 PHY has them while
6108b95599cSMarek Vasut 	 * the switch does not.
6118b95599cSMarek Vasut 	 */
6128b95599cSMarek Vasut 	ret &= BMSR_ERCAP;
613a5e63c7dSSteve Bennett 	if (ksz_8051)
6148b95599cSMarek Vasut 		return ret;
6158b95599cSMarek Vasut 	else
6168b95599cSMarek Vasut 		return !ret;
6178b95599cSMarek Vasut }
6188b95599cSMarek Vasut 
6198b95599cSMarek Vasut static int ksz8051_match_phy_device(struct phy_device *phydev)
6208b95599cSMarek Vasut {
621a5e63c7dSSteve Bennett 	return ksz8051_ksz8795_match_phy_device(phydev, true);
6228b95599cSMarek Vasut }
6238b95599cSMarek Vasut 
6247a1d8390SAntoine Tenart static int ksz8081_config_init(struct phy_device *phydev)
6257a1d8390SAntoine Tenart {
6267a1d8390SAntoine Tenart 	/* KSZPHY_OMSO_FACTORY_TEST is set at de-assertion of the reset line
6277a1d8390SAntoine Tenart 	 * based on the RXER (KSZ8081RNA/RND) or TXC (KSZ8081MNX/RNB) pin. If a
6287a1d8390SAntoine Tenart 	 * pull-down is missing, the factory test mode should be cleared by
6297a1d8390SAntoine Tenart 	 * manually writing a 0.
6307a1d8390SAntoine Tenart 	 */
6317a1d8390SAntoine Tenart 	phy_clear_bits(phydev, MII_KSZPHY_OMSO, KSZPHY_OMSO_FACTORY_TEST);
6327a1d8390SAntoine Tenart 
6337a1d8390SAntoine Tenart 	return kszphy_config_init(phydev);
6347a1d8390SAntoine Tenart }
6357a1d8390SAntoine Tenart 
636f873f112SOleksij Rempel static int ksz8081_config_mdix(struct phy_device *phydev, u8 ctrl)
637f873f112SOleksij Rempel {
638f873f112SOleksij Rempel 	u16 val;
639f873f112SOleksij Rempel 
640f873f112SOleksij Rempel 	switch (ctrl) {
641f873f112SOleksij Rempel 	case ETH_TP_MDI:
642f873f112SOleksij Rempel 		val = KSZ8081_CTRL2_DISABLE_AUTO_MDIX;
643f873f112SOleksij Rempel 		break;
644f873f112SOleksij Rempel 	case ETH_TP_MDI_X:
645f873f112SOleksij Rempel 		val = KSZ8081_CTRL2_DISABLE_AUTO_MDIX |
646f873f112SOleksij Rempel 			KSZ8081_CTRL2_MDI_MDI_X_SELECT;
647f873f112SOleksij Rempel 		break;
648f873f112SOleksij Rempel 	case ETH_TP_MDI_AUTO:
649f873f112SOleksij Rempel 		val = 0;
650f873f112SOleksij Rempel 		break;
651f873f112SOleksij Rempel 	default:
652f873f112SOleksij Rempel 		return 0;
653f873f112SOleksij Rempel 	}
654f873f112SOleksij Rempel 
655f873f112SOleksij Rempel 	return phy_modify(phydev, MII_KSZPHY_CTRL_2,
656f873f112SOleksij Rempel 			  KSZ8081_CTRL2_HP_MDIX |
657f873f112SOleksij Rempel 			  KSZ8081_CTRL2_MDI_MDI_X_SELECT |
658f873f112SOleksij Rempel 			  KSZ8081_CTRL2_DISABLE_AUTO_MDIX,
659f873f112SOleksij Rempel 			  KSZ8081_CTRL2_HP_MDIX | val);
660f873f112SOleksij Rempel }
661f873f112SOleksij Rempel 
662f873f112SOleksij Rempel static int ksz8081_config_aneg(struct phy_device *phydev)
663f873f112SOleksij Rempel {
664f873f112SOleksij Rempel 	int ret;
665f873f112SOleksij Rempel 
666f873f112SOleksij Rempel 	ret = genphy_config_aneg(phydev);
667f873f112SOleksij Rempel 	if (ret)
668f873f112SOleksij Rempel 		return ret;
669f873f112SOleksij Rempel 
670f873f112SOleksij Rempel 	/* The MDI-X configuration is automatically changed by the PHY after
671f873f112SOleksij Rempel 	 * switching from autoneg off to on. So, take MDI-X configuration under
672f873f112SOleksij Rempel 	 * own control and set it after autoneg configuration was done.
673f873f112SOleksij Rempel 	 */
674f873f112SOleksij Rempel 	return ksz8081_config_mdix(phydev, phydev->mdix_ctrl);
675f873f112SOleksij Rempel }
676f873f112SOleksij Rempel 
677f873f112SOleksij Rempel static int ksz8081_mdix_update(struct phy_device *phydev)
678f873f112SOleksij Rempel {
679f873f112SOleksij Rempel 	int ret;
680f873f112SOleksij Rempel 
681f873f112SOleksij Rempel 	ret = phy_read(phydev, MII_KSZPHY_CTRL_2);
682f873f112SOleksij Rempel 	if (ret < 0)
683f873f112SOleksij Rempel 		return ret;
684f873f112SOleksij Rempel 
685f873f112SOleksij Rempel 	if (ret & KSZ8081_CTRL2_DISABLE_AUTO_MDIX) {
686f873f112SOleksij Rempel 		if (ret & KSZ8081_CTRL2_MDI_MDI_X_SELECT)
687f873f112SOleksij Rempel 			phydev->mdix_ctrl = ETH_TP_MDI_X;
688f873f112SOleksij Rempel 		else
689f873f112SOleksij Rempel 			phydev->mdix_ctrl = ETH_TP_MDI;
690f873f112SOleksij Rempel 	} else {
691f873f112SOleksij Rempel 		phydev->mdix_ctrl = ETH_TP_MDI_AUTO;
692f873f112SOleksij Rempel 	}
693f873f112SOleksij Rempel 
694f873f112SOleksij Rempel 	ret = phy_read(phydev, MII_KSZPHY_CTRL_1);
695f873f112SOleksij Rempel 	if (ret < 0)
696f873f112SOleksij Rempel 		return ret;
697f873f112SOleksij Rempel 
698f873f112SOleksij Rempel 	if (ret & KSZ8081_CTRL1_MDIX_STAT)
699f873f112SOleksij Rempel 		phydev->mdix = ETH_TP_MDI;
700f873f112SOleksij Rempel 	else
701f873f112SOleksij Rempel 		phydev->mdix = ETH_TP_MDI_X;
702f873f112SOleksij Rempel 
703f873f112SOleksij Rempel 	return 0;
704f873f112SOleksij Rempel }
705f873f112SOleksij Rempel 
706f873f112SOleksij Rempel static int ksz8081_read_status(struct phy_device *phydev)
707f873f112SOleksij Rempel {
708f873f112SOleksij Rempel 	int ret;
709f873f112SOleksij Rempel 
710f873f112SOleksij Rempel 	ret = ksz8081_mdix_update(phydev);
711f873f112SOleksij Rempel 	if (ret < 0)
712f873f112SOleksij Rempel 		return ret;
713f873f112SOleksij Rempel 
714f873f112SOleksij Rempel 	return genphy_read_status(phydev);
715f873f112SOleksij Rempel }
716f873f112SOleksij Rempel 
717232ba3a5SRajasingh Thavamani static int ksz8061_config_init(struct phy_device *phydev)
718232ba3a5SRajasingh Thavamani {
719232ba3a5SRajasingh Thavamani 	int ret;
720232ba3a5SRajasingh Thavamani 
721232ba3a5SRajasingh Thavamani 	ret = phy_write_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_DEVID1, 0xB61A);
722232ba3a5SRajasingh Thavamani 	if (ret)
723232ba3a5SRajasingh Thavamani 		return ret;
724232ba3a5SRajasingh Thavamani 
725232ba3a5SRajasingh Thavamani 	return kszphy_config_init(phydev);
726232ba3a5SRajasingh Thavamani }
727232ba3a5SRajasingh Thavamani 
7288b95599cSMarek Vasut static int ksz8795_match_phy_device(struct phy_device *phydev)
7298b95599cSMarek Vasut {
730a5e63c7dSSteve Bennett 	return ksz8051_ksz8795_match_phy_device(phydev, false);
7318b95599cSMarek Vasut }
7328b95599cSMarek Vasut 
733954c3967SSean Cross static int ksz9021_load_values_from_of(struct phy_device *phydev,
7343c9a9f7fSJaeden Amero 				       const struct device_node *of_node,
7353c9a9f7fSJaeden Amero 				       u16 reg,
7363c9a9f7fSJaeden Amero 				       const char *field1, const char *field2,
7373c9a9f7fSJaeden Amero 				       const char *field3, const char *field4)
738954c3967SSean Cross {
739954c3967SSean Cross 	int val1 = -1;
740954c3967SSean Cross 	int val2 = -2;
741954c3967SSean Cross 	int val3 = -3;
742954c3967SSean Cross 	int val4 = -4;
743954c3967SSean Cross 	int newval;
744954c3967SSean Cross 	int matches = 0;
745954c3967SSean Cross 
746954c3967SSean Cross 	if (!of_property_read_u32(of_node, field1, &val1))
747954c3967SSean Cross 		matches++;
748954c3967SSean Cross 
749954c3967SSean Cross 	if (!of_property_read_u32(of_node, field2, &val2))
750954c3967SSean Cross 		matches++;
751954c3967SSean Cross 
752954c3967SSean Cross 	if (!of_property_read_u32(of_node, field3, &val3))
753954c3967SSean Cross 		matches++;
754954c3967SSean Cross 
755954c3967SSean Cross 	if (!of_property_read_u32(of_node, field4, &val4))
756954c3967SSean Cross 		matches++;
757954c3967SSean Cross 
758954c3967SSean Cross 	if (!matches)
759954c3967SSean Cross 		return 0;
760954c3967SSean Cross 
761954c3967SSean Cross 	if (matches < 4)
762954c3967SSean Cross 		newval = kszphy_extended_read(phydev, reg);
763954c3967SSean Cross 	else
764954c3967SSean Cross 		newval = 0;
765954c3967SSean Cross 
766954c3967SSean Cross 	if (val1 != -1)
767954c3967SSean Cross 		newval = ((newval & 0xfff0) | ((val1 / PS_TO_REG) & 0xf) << 0);
768954c3967SSean Cross 
7696a119745SHubert Chaumette 	if (val2 != -2)
770954c3967SSean Cross 		newval = ((newval & 0xff0f) | ((val2 / PS_TO_REG) & 0xf) << 4);
771954c3967SSean Cross 
7726a119745SHubert Chaumette 	if (val3 != -3)
773954c3967SSean Cross 		newval = ((newval & 0xf0ff) | ((val3 / PS_TO_REG) & 0xf) << 8);
774954c3967SSean Cross 
7756a119745SHubert Chaumette 	if (val4 != -4)
776954c3967SSean Cross 		newval = ((newval & 0x0fff) | ((val4 / PS_TO_REG) & 0xf) << 12);
777954c3967SSean Cross 
778954c3967SSean Cross 	return kszphy_extended_write(phydev, reg, newval);
779954c3967SSean Cross }
780954c3967SSean Cross 
781954c3967SSean Cross static int ksz9021_config_init(struct phy_device *phydev)
782954c3967SSean Cross {
783ce4f8afdSColin Ian King 	const struct device_node *of_node;
784651df218SAndrew Lunn 	const struct device *dev_walker;
785954c3967SSean Cross 
786651df218SAndrew Lunn 	/* The Micrel driver has a deprecated option to place phy OF
787651df218SAndrew Lunn 	 * properties in the MAC node. Walk up the tree of devices to
788651df218SAndrew Lunn 	 * find a device with an OF node.
789651df218SAndrew Lunn 	 */
790e5a03bfdSAndrew Lunn 	dev_walker = &phydev->mdio.dev;
791651df218SAndrew Lunn 	do {
792651df218SAndrew Lunn 		of_node = dev_walker->of_node;
793651df218SAndrew Lunn 		dev_walker = dev_walker->parent;
794651df218SAndrew Lunn 
795651df218SAndrew Lunn 	} while (!of_node && dev_walker);
796954c3967SSean Cross 
797954c3967SSean Cross 	if (of_node) {
798954c3967SSean Cross 		ksz9021_load_values_from_of(phydev, of_node,
799954c3967SSean Cross 				    MII_KSZPHY_CLK_CONTROL_PAD_SKEW,
800954c3967SSean Cross 				    "txen-skew-ps", "txc-skew-ps",
801954c3967SSean Cross 				    "rxdv-skew-ps", "rxc-skew-ps");
802954c3967SSean Cross 		ksz9021_load_values_from_of(phydev, of_node,
803954c3967SSean Cross 				    MII_KSZPHY_RX_DATA_PAD_SKEW,
804954c3967SSean Cross 				    "rxd0-skew-ps", "rxd1-skew-ps",
805954c3967SSean Cross 				    "rxd2-skew-ps", "rxd3-skew-ps");
806954c3967SSean Cross 		ksz9021_load_values_from_of(phydev, of_node,
807954c3967SSean Cross 				    MII_KSZPHY_TX_DATA_PAD_SKEW,
808954c3967SSean Cross 				    "txd0-skew-ps", "txd1-skew-ps",
809954c3967SSean Cross 				    "txd2-skew-ps", "txd3-skew-ps");
810954c3967SSean Cross 	}
811954c3967SSean Cross 	return 0;
812954c3967SSean Cross }
813954c3967SSean Cross 
8146e4b8273SHubert Chaumette #define KSZ9031_PS_TO_REG		60
8156e4b8273SHubert Chaumette 
8166e4b8273SHubert Chaumette /* Extended registers */
8176270e1aeSJaeden Amero /* MMD Address 0x0 */
8186270e1aeSJaeden Amero #define MII_KSZ9031RN_FLP_BURST_TX_LO	3
8196270e1aeSJaeden Amero #define MII_KSZ9031RN_FLP_BURST_TX_HI	4
8206270e1aeSJaeden Amero 
821ae6c97bbSJaeden Amero /* MMD Address 0x2 */
8226e4b8273SHubert Chaumette #define MII_KSZ9031RN_CONTROL_PAD_SKEW	4
823bcf3440cSOleksij Rempel #define MII_KSZ9031RN_RX_CTL_M		GENMASK(7, 4)
824bcf3440cSOleksij Rempel #define MII_KSZ9031RN_TX_CTL_M		GENMASK(3, 0)
825bcf3440cSOleksij Rempel 
8266e4b8273SHubert Chaumette #define MII_KSZ9031RN_RX_DATA_PAD_SKEW	5
827bcf3440cSOleksij Rempel #define MII_KSZ9031RN_RXD3		GENMASK(15, 12)
828bcf3440cSOleksij Rempel #define MII_KSZ9031RN_RXD2		GENMASK(11, 8)
829bcf3440cSOleksij Rempel #define MII_KSZ9031RN_RXD1		GENMASK(7, 4)
830bcf3440cSOleksij Rempel #define MII_KSZ9031RN_RXD0		GENMASK(3, 0)
831bcf3440cSOleksij Rempel 
8326e4b8273SHubert Chaumette #define MII_KSZ9031RN_TX_DATA_PAD_SKEW	6
833bcf3440cSOleksij Rempel #define MII_KSZ9031RN_TXD3		GENMASK(15, 12)
834bcf3440cSOleksij Rempel #define MII_KSZ9031RN_TXD2		GENMASK(11, 8)
835bcf3440cSOleksij Rempel #define MII_KSZ9031RN_TXD1		GENMASK(7, 4)
836bcf3440cSOleksij Rempel #define MII_KSZ9031RN_TXD0		GENMASK(3, 0)
837bcf3440cSOleksij Rempel 
8386e4b8273SHubert Chaumette #define MII_KSZ9031RN_CLK_PAD_SKEW	8
839bcf3440cSOleksij Rempel #define MII_KSZ9031RN_GTX_CLK		GENMASK(9, 5)
840bcf3440cSOleksij Rempel #define MII_KSZ9031RN_RX_CLK		GENMASK(4, 0)
841bcf3440cSOleksij Rempel 
842bcf3440cSOleksij Rempel /* KSZ9031 has internal RGMII_IDRX = 1.2ns and RGMII_IDTX = 0ns. To
843bcf3440cSOleksij Rempel  * provide different RGMII options we need to configure delay offset
844bcf3440cSOleksij Rempel  * for each pad relative to build in delay.
845bcf3440cSOleksij Rempel  */
846bcf3440cSOleksij Rempel /* keep rx as "No delay adjustment" and set rx_clk to +0.60ns to get delays of
847bcf3440cSOleksij Rempel  * 1.80ns
848bcf3440cSOleksij Rempel  */
849bcf3440cSOleksij Rempel #define RX_ID				0x7
850bcf3440cSOleksij Rempel #define RX_CLK_ID			0x19
851bcf3440cSOleksij Rempel 
852bcf3440cSOleksij Rempel /* set rx to +0.30ns and rx_clk to -0.90ns to compensate the
853bcf3440cSOleksij Rempel  * internal 1.2ns delay.
854bcf3440cSOleksij Rempel  */
855bcf3440cSOleksij Rempel #define RX_ND				0xc
856bcf3440cSOleksij Rempel #define RX_CLK_ND			0x0
857bcf3440cSOleksij Rempel 
858bcf3440cSOleksij Rempel /* set tx to -0.42ns and tx_clk to +0.96ns to get 1.38ns delay */
859bcf3440cSOleksij Rempel #define TX_ID				0x0
860bcf3440cSOleksij Rempel #define TX_CLK_ID			0x1f
861bcf3440cSOleksij Rempel 
862bcf3440cSOleksij Rempel /* set tx and tx_clk to "No delay adjustment" to keep 0ns
863bcf3440cSOleksij Rempel  * dealy
864bcf3440cSOleksij Rempel  */
865bcf3440cSOleksij Rempel #define TX_ND				0x7
866bcf3440cSOleksij Rempel #define TX_CLK_ND			0xf
8676e4b8273SHubert Chaumette 
868af70c1f9SMike Looijmans /* MMD Address 0x1C */
869af70c1f9SMike Looijmans #define MII_KSZ9031RN_EDPD		0x23
870af70c1f9SMike Looijmans #define MII_KSZ9031RN_EDPD_ENABLE	BIT(0)
871af70c1f9SMike Looijmans 
8726e4b8273SHubert Chaumette static int ksz9031_of_load_skew_values(struct phy_device *phydev,
8733c9a9f7fSJaeden Amero 				       const struct device_node *of_node,
8746e4b8273SHubert Chaumette 				       u16 reg, size_t field_sz,
875bcf3440cSOleksij Rempel 				       const char *field[], u8 numfields,
876bcf3440cSOleksij Rempel 				       bool *update)
8776e4b8273SHubert Chaumette {
8786e4b8273SHubert Chaumette 	int val[4] = {-1, -2, -3, -4};
8796e4b8273SHubert Chaumette 	int matches = 0;
8806e4b8273SHubert Chaumette 	u16 mask;
8816e4b8273SHubert Chaumette 	u16 maxval;
8826e4b8273SHubert Chaumette 	u16 newval;
8836e4b8273SHubert Chaumette 	int i;
8846e4b8273SHubert Chaumette 
8856e4b8273SHubert Chaumette 	for (i = 0; i < numfields; i++)
8866e4b8273SHubert Chaumette 		if (!of_property_read_u32(of_node, field[i], val + i))
8876e4b8273SHubert Chaumette 			matches++;
8886e4b8273SHubert Chaumette 
8896e4b8273SHubert Chaumette 	if (!matches)
8906e4b8273SHubert Chaumette 		return 0;
8916e4b8273SHubert Chaumette 
892bcf3440cSOleksij Rempel 	*update |= true;
893bcf3440cSOleksij Rempel 
8946e4b8273SHubert Chaumette 	if (matches < numfields)
8959b420effSHeiner Kallweit 		newval = phy_read_mmd(phydev, 2, reg);
8966e4b8273SHubert Chaumette 	else
8976e4b8273SHubert Chaumette 		newval = 0;
8986e4b8273SHubert Chaumette 
8996e4b8273SHubert Chaumette 	maxval = (field_sz == 4) ? 0xf : 0x1f;
9006e4b8273SHubert Chaumette 	for (i = 0; i < numfields; i++)
9016e4b8273SHubert Chaumette 		if (val[i] != -(i + 1)) {
9026e4b8273SHubert Chaumette 			mask = 0xffff;
9036e4b8273SHubert Chaumette 			mask ^= maxval << (field_sz * i);
9046e4b8273SHubert Chaumette 			newval = (newval & mask) |
9056e4b8273SHubert Chaumette 				(((val[i] / KSZ9031_PS_TO_REG) & maxval)
9066e4b8273SHubert Chaumette 					<< (field_sz * i));
9076e4b8273SHubert Chaumette 		}
9086e4b8273SHubert Chaumette 
9099b420effSHeiner Kallweit 	return phy_write_mmd(phydev, 2, reg, newval);
9106e4b8273SHubert Chaumette }
9116e4b8273SHubert Chaumette 
912a0da456bSMax Uvarov /* Center KSZ9031RNX FLP timing at 16ms. */
9136270e1aeSJaeden Amero static int ksz9031_center_flp_timing(struct phy_device *phydev)
9146270e1aeSJaeden Amero {
9156270e1aeSJaeden Amero 	int result;
9166270e1aeSJaeden Amero 
9179b420effSHeiner Kallweit 	result = phy_write_mmd(phydev, 0, MII_KSZ9031RN_FLP_BURST_TX_HI,
9189b420effSHeiner Kallweit 			       0x0006);
919a0da456bSMax Uvarov 	if (result)
920a0da456bSMax Uvarov 		return result;
921a0da456bSMax Uvarov 
9229b420effSHeiner Kallweit 	result = phy_write_mmd(phydev, 0, MII_KSZ9031RN_FLP_BURST_TX_LO,
9239b420effSHeiner Kallweit 			       0x1A80);
9246270e1aeSJaeden Amero 	if (result)
9256270e1aeSJaeden Amero 		return result;
9266270e1aeSJaeden Amero 
9276270e1aeSJaeden Amero 	return genphy_restart_aneg(phydev);
9286270e1aeSJaeden Amero }
9296270e1aeSJaeden Amero 
930af70c1f9SMike Looijmans /* Enable energy-detect power-down mode */
931af70c1f9SMike Looijmans static int ksz9031_enable_edpd(struct phy_device *phydev)
932af70c1f9SMike Looijmans {
933af70c1f9SMike Looijmans 	int reg;
934af70c1f9SMike Looijmans 
9359b420effSHeiner Kallweit 	reg = phy_read_mmd(phydev, 0x1C, MII_KSZ9031RN_EDPD);
936af70c1f9SMike Looijmans 	if (reg < 0)
937af70c1f9SMike Looijmans 		return reg;
9389b420effSHeiner Kallweit 	return phy_write_mmd(phydev, 0x1C, MII_KSZ9031RN_EDPD,
939af70c1f9SMike Looijmans 			     reg | MII_KSZ9031RN_EDPD_ENABLE);
940af70c1f9SMike Looijmans }
941af70c1f9SMike Looijmans 
942bcf3440cSOleksij Rempel static int ksz9031_config_rgmii_delay(struct phy_device *phydev)
943bcf3440cSOleksij Rempel {
944bcf3440cSOleksij Rempel 	u16 rx, tx, rx_clk, tx_clk;
945bcf3440cSOleksij Rempel 	int ret;
946bcf3440cSOleksij Rempel 
947bcf3440cSOleksij Rempel 	switch (phydev->interface) {
948bcf3440cSOleksij Rempel 	case PHY_INTERFACE_MODE_RGMII:
949bcf3440cSOleksij Rempel 		tx = TX_ND;
950bcf3440cSOleksij Rempel 		tx_clk = TX_CLK_ND;
951bcf3440cSOleksij Rempel 		rx = RX_ND;
952bcf3440cSOleksij Rempel 		rx_clk = RX_CLK_ND;
953bcf3440cSOleksij Rempel 		break;
954bcf3440cSOleksij Rempel 	case PHY_INTERFACE_MODE_RGMII_ID:
955bcf3440cSOleksij Rempel 		tx = TX_ID;
956bcf3440cSOleksij Rempel 		tx_clk = TX_CLK_ID;
957bcf3440cSOleksij Rempel 		rx = RX_ID;
958bcf3440cSOleksij Rempel 		rx_clk = RX_CLK_ID;
959bcf3440cSOleksij Rempel 		break;
960bcf3440cSOleksij Rempel 	case PHY_INTERFACE_MODE_RGMII_RXID:
961bcf3440cSOleksij Rempel 		tx = TX_ND;
962bcf3440cSOleksij Rempel 		tx_clk = TX_CLK_ND;
963bcf3440cSOleksij Rempel 		rx = RX_ID;
964bcf3440cSOleksij Rempel 		rx_clk = RX_CLK_ID;
965bcf3440cSOleksij Rempel 		break;
966bcf3440cSOleksij Rempel 	case PHY_INTERFACE_MODE_RGMII_TXID:
967bcf3440cSOleksij Rempel 		tx = TX_ID;
968bcf3440cSOleksij Rempel 		tx_clk = TX_CLK_ID;
969bcf3440cSOleksij Rempel 		rx = RX_ND;
970bcf3440cSOleksij Rempel 		rx_clk = RX_CLK_ND;
971bcf3440cSOleksij Rempel 		break;
972bcf3440cSOleksij Rempel 	default:
973bcf3440cSOleksij Rempel 		return 0;
974bcf3440cSOleksij Rempel 	}
975bcf3440cSOleksij Rempel 
976bcf3440cSOleksij Rempel 	ret = phy_write_mmd(phydev, 2, MII_KSZ9031RN_CONTROL_PAD_SKEW,
977bcf3440cSOleksij Rempel 			    FIELD_PREP(MII_KSZ9031RN_RX_CTL_M, rx) |
978bcf3440cSOleksij Rempel 			    FIELD_PREP(MII_KSZ9031RN_TX_CTL_M, tx));
979bcf3440cSOleksij Rempel 	if (ret < 0)
980bcf3440cSOleksij Rempel 		return ret;
981bcf3440cSOleksij Rempel 
982bcf3440cSOleksij Rempel 	ret = phy_write_mmd(phydev, 2, MII_KSZ9031RN_RX_DATA_PAD_SKEW,
983bcf3440cSOleksij Rempel 			    FIELD_PREP(MII_KSZ9031RN_RXD3, rx) |
984bcf3440cSOleksij Rempel 			    FIELD_PREP(MII_KSZ9031RN_RXD2, rx) |
985bcf3440cSOleksij Rempel 			    FIELD_PREP(MII_KSZ9031RN_RXD1, rx) |
986bcf3440cSOleksij Rempel 			    FIELD_PREP(MII_KSZ9031RN_RXD0, rx));
987bcf3440cSOleksij Rempel 	if (ret < 0)
988bcf3440cSOleksij Rempel 		return ret;
989bcf3440cSOleksij Rempel 
990bcf3440cSOleksij Rempel 	ret = phy_write_mmd(phydev, 2, MII_KSZ9031RN_TX_DATA_PAD_SKEW,
991bcf3440cSOleksij Rempel 			    FIELD_PREP(MII_KSZ9031RN_TXD3, tx) |
992bcf3440cSOleksij Rempel 			    FIELD_PREP(MII_KSZ9031RN_TXD2, tx) |
993bcf3440cSOleksij Rempel 			    FIELD_PREP(MII_KSZ9031RN_TXD1, tx) |
994bcf3440cSOleksij Rempel 			    FIELD_PREP(MII_KSZ9031RN_TXD0, tx));
995bcf3440cSOleksij Rempel 	if (ret < 0)
996bcf3440cSOleksij Rempel 		return ret;
997bcf3440cSOleksij Rempel 
998bcf3440cSOleksij Rempel 	return phy_write_mmd(phydev, 2, MII_KSZ9031RN_CLK_PAD_SKEW,
999bcf3440cSOleksij Rempel 			     FIELD_PREP(MII_KSZ9031RN_GTX_CLK, tx_clk) |
1000bcf3440cSOleksij Rempel 			     FIELD_PREP(MII_KSZ9031RN_RX_CLK, rx_clk));
1001bcf3440cSOleksij Rempel }
1002bcf3440cSOleksij Rempel 
10036e4b8273SHubert Chaumette static int ksz9031_config_init(struct phy_device *phydev)
10046e4b8273SHubert Chaumette {
1005ce4f8afdSColin Ian King 	const struct device_node *of_node;
10063c9a9f7fSJaeden Amero 	static const char *clk_skews[2] = {"rxc-skew-ps", "txc-skew-ps"};
10073c9a9f7fSJaeden Amero 	static const char *rx_data_skews[4] = {
10086e4b8273SHubert Chaumette 		"rxd0-skew-ps", "rxd1-skew-ps",
10096e4b8273SHubert Chaumette 		"rxd2-skew-ps", "rxd3-skew-ps"
10106e4b8273SHubert Chaumette 	};
10113c9a9f7fSJaeden Amero 	static const char *tx_data_skews[4] = {
10126e4b8273SHubert Chaumette 		"txd0-skew-ps", "txd1-skew-ps",
10136e4b8273SHubert Chaumette 		"txd2-skew-ps", "txd3-skew-ps"
10146e4b8273SHubert Chaumette 	};
10153c9a9f7fSJaeden Amero 	static const char *control_skews[2] = {"txen-skew-ps", "rxdv-skew-ps"};
1016b4c19f71SRoosen Henri 	const struct device *dev_walker;
1017af70c1f9SMike Looijmans 	int result;
1018af70c1f9SMike Looijmans 
1019af70c1f9SMike Looijmans 	result = ksz9031_enable_edpd(phydev);
1020af70c1f9SMike Looijmans 	if (result < 0)
1021af70c1f9SMike Looijmans 		return result;
10226e4b8273SHubert Chaumette 
1023b4c19f71SRoosen Henri 	/* The Micrel driver has a deprecated option to place phy OF
1024b4c19f71SRoosen Henri 	 * properties in the MAC node. Walk up the tree of devices to
1025b4c19f71SRoosen Henri 	 * find a device with an OF node.
1026b4c19f71SRoosen Henri 	 */
10279d367eddSDavid S. Miller 	dev_walker = &phydev->mdio.dev;
1028b4c19f71SRoosen Henri 	do {
1029b4c19f71SRoosen Henri 		of_node = dev_walker->of_node;
1030b4c19f71SRoosen Henri 		dev_walker = dev_walker->parent;
1031b4c19f71SRoosen Henri 	} while (!of_node && dev_walker);
10326e4b8273SHubert Chaumette 
10336e4b8273SHubert Chaumette 	if (of_node) {
1034bcf3440cSOleksij Rempel 		bool update = false;
1035bcf3440cSOleksij Rempel 
1036bcf3440cSOleksij Rempel 		if (phy_interface_is_rgmii(phydev)) {
1037bcf3440cSOleksij Rempel 			result = ksz9031_config_rgmii_delay(phydev);
1038bcf3440cSOleksij Rempel 			if (result < 0)
1039bcf3440cSOleksij Rempel 				return result;
1040bcf3440cSOleksij Rempel 		}
1041bcf3440cSOleksij Rempel 
10426e4b8273SHubert Chaumette 		ksz9031_of_load_skew_values(phydev, of_node,
10436e4b8273SHubert Chaumette 				MII_KSZ9031RN_CLK_PAD_SKEW, 5,
1044bcf3440cSOleksij Rempel 				clk_skews, 2, &update);
10456e4b8273SHubert Chaumette 
10466e4b8273SHubert Chaumette 		ksz9031_of_load_skew_values(phydev, of_node,
10476e4b8273SHubert Chaumette 				MII_KSZ9031RN_CONTROL_PAD_SKEW, 4,
1048bcf3440cSOleksij Rempel 				control_skews, 2, &update);
10496e4b8273SHubert Chaumette 
10506e4b8273SHubert Chaumette 		ksz9031_of_load_skew_values(phydev, of_node,
10516e4b8273SHubert Chaumette 				MII_KSZ9031RN_RX_DATA_PAD_SKEW, 4,
1052bcf3440cSOleksij Rempel 				rx_data_skews, 4, &update);
10536e4b8273SHubert Chaumette 
10546e4b8273SHubert Chaumette 		ksz9031_of_load_skew_values(phydev, of_node,
10556e4b8273SHubert Chaumette 				MII_KSZ9031RN_TX_DATA_PAD_SKEW, 4,
1056bcf3440cSOleksij Rempel 				tx_data_skews, 4, &update);
1057bcf3440cSOleksij Rempel 
105867ca5159SMatthias Schiffer 		if (update && !phy_interface_is_rgmii(phydev))
1059bcf3440cSOleksij Rempel 			phydev_warn(phydev,
106067ca5159SMatthias Schiffer 				    "*-skew-ps values should be used only with RGMII PHY modes\n");
1061e1b505a6SMarkus Niebel 
1062e1b505a6SMarkus Niebel 		/* Silicon Errata Sheet (DS80000691D or DS80000692D):
1063e1b505a6SMarkus Niebel 		 * When the device links in the 1000BASE-T slave mode only,
1064e1b505a6SMarkus Niebel 		 * the optional 125MHz reference output clock (CLK125_NDO)
1065e1b505a6SMarkus Niebel 		 * has wide duty cycle variation.
1066e1b505a6SMarkus Niebel 		 *
1067e1b505a6SMarkus Niebel 		 * The optional CLK125_NDO clock does not meet the RGMII
1068e1b505a6SMarkus Niebel 		 * 45/55 percent (min/max) duty cycle requirement and therefore
1069e1b505a6SMarkus Niebel 		 * cannot be used directly by the MAC side for clocking
1070e1b505a6SMarkus Niebel 		 * applications that have setup/hold time requirements on
1071e1b505a6SMarkus Niebel 		 * rising and falling clock edges.
1072e1b505a6SMarkus Niebel 		 *
1073e1b505a6SMarkus Niebel 		 * Workaround:
1074e1b505a6SMarkus Niebel 		 * Force the phy to be the master to receive a stable clock
1075e1b505a6SMarkus Niebel 		 * which meets the duty cycle requirement.
1076e1b505a6SMarkus Niebel 		 */
1077e1b505a6SMarkus Niebel 		if (of_property_read_bool(of_node, "micrel,force-master")) {
1078e1b505a6SMarkus Niebel 			result = phy_read(phydev, MII_CTRL1000);
1079e1b505a6SMarkus Niebel 			if (result < 0)
1080e1b505a6SMarkus Niebel 				goto err_force_master;
1081e1b505a6SMarkus Niebel 
1082e1b505a6SMarkus Niebel 			/* enable master mode, config & prefer master */
1083e1b505a6SMarkus Niebel 			result |= CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER;
1084e1b505a6SMarkus Niebel 			result = phy_write(phydev, MII_CTRL1000, result);
1085e1b505a6SMarkus Niebel 			if (result < 0)
1086e1b505a6SMarkus Niebel 				goto err_force_master;
1087e1b505a6SMarkus Niebel 		}
10886e4b8273SHubert Chaumette 	}
10896270e1aeSJaeden Amero 
10906270e1aeSJaeden Amero 	return ksz9031_center_flp_timing(phydev);
1091e1b505a6SMarkus Niebel 
1092e1b505a6SMarkus Niebel err_force_master:
1093e1b505a6SMarkus Niebel 	phydev_err(phydev, "failed to force the phy to master mode\n");
1094e1b505a6SMarkus Niebel 	return result;
10956e4b8273SHubert Chaumette }
10966e4b8273SHubert Chaumette 
1097bff5b4b3SYuiko Oshino #define KSZ9131_SKEW_5BIT_MAX	2400
1098bff5b4b3SYuiko Oshino #define KSZ9131_SKEW_4BIT_MAX	800
1099bff5b4b3SYuiko Oshino #define KSZ9131_OFFSET		700
1100bff5b4b3SYuiko Oshino #define KSZ9131_STEP		100
1101bff5b4b3SYuiko Oshino 
1102bff5b4b3SYuiko Oshino static int ksz9131_of_load_skew_values(struct phy_device *phydev,
1103bff5b4b3SYuiko Oshino 				       struct device_node *of_node,
1104bff5b4b3SYuiko Oshino 				       u16 reg, size_t field_sz,
1105bff5b4b3SYuiko Oshino 				       char *field[], u8 numfields)
1106bff5b4b3SYuiko Oshino {
1107bff5b4b3SYuiko Oshino 	int val[4] = {-(1 + KSZ9131_OFFSET), -(2 + KSZ9131_OFFSET),
1108bff5b4b3SYuiko Oshino 		      -(3 + KSZ9131_OFFSET), -(4 + KSZ9131_OFFSET)};
1109bff5b4b3SYuiko Oshino 	int skewval, skewmax = 0;
1110bff5b4b3SYuiko Oshino 	int matches = 0;
1111bff5b4b3SYuiko Oshino 	u16 maxval;
1112bff5b4b3SYuiko Oshino 	u16 newval;
1113bff5b4b3SYuiko Oshino 	u16 mask;
1114bff5b4b3SYuiko Oshino 	int i;
1115bff5b4b3SYuiko Oshino 
1116bff5b4b3SYuiko Oshino 	/* psec properties in dts should mean x pico seconds */
1117bff5b4b3SYuiko Oshino 	if (field_sz == 5)
1118bff5b4b3SYuiko Oshino 		skewmax = KSZ9131_SKEW_5BIT_MAX;
1119bff5b4b3SYuiko Oshino 	else
1120bff5b4b3SYuiko Oshino 		skewmax = KSZ9131_SKEW_4BIT_MAX;
1121bff5b4b3SYuiko Oshino 
1122bff5b4b3SYuiko Oshino 	for (i = 0; i < numfields; i++)
1123bff5b4b3SYuiko Oshino 		if (!of_property_read_s32(of_node, field[i], &skewval)) {
1124bff5b4b3SYuiko Oshino 			if (skewval < -KSZ9131_OFFSET)
1125bff5b4b3SYuiko Oshino 				skewval = -KSZ9131_OFFSET;
1126bff5b4b3SYuiko Oshino 			else if (skewval > skewmax)
1127bff5b4b3SYuiko Oshino 				skewval = skewmax;
1128bff5b4b3SYuiko Oshino 
1129bff5b4b3SYuiko Oshino 			val[i] = skewval + KSZ9131_OFFSET;
1130bff5b4b3SYuiko Oshino 			matches++;
1131bff5b4b3SYuiko Oshino 		}
1132bff5b4b3SYuiko Oshino 
1133bff5b4b3SYuiko Oshino 	if (!matches)
1134bff5b4b3SYuiko Oshino 		return 0;
1135bff5b4b3SYuiko Oshino 
1136bff5b4b3SYuiko Oshino 	if (matches < numfields)
11379b420effSHeiner Kallweit 		newval = phy_read_mmd(phydev, 2, reg);
1138bff5b4b3SYuiko Oshino 	else
1139bff5b4b3SYuiko Oshino 		newval = 0;
1140bff5b4b3SYuiko Oshino 
1141bff5b4b3SYuiko Oshino 	maxval = (field_sz == 4) ? 0xf : 0x1f;
1142bff5b4b3SYuiko Oshino 	for (i = 0; i < numfields; i++)
1143bff5b4b3SYuiko Oshino 		if (val[i] != -(i + 1 + KSZ9131_OFFSET)) {
1144bff5b4b3SYuiko Oshino 			mask = 0xffff;
1145bff5b4b3SYuiko Oshino 			mask ^= maxval << (field_sz * i);
1146bff5b4b3SYuiko Oshino 			newval = (newval & mask) |
1147bff5b4b3SYuiko Oshino 				(((val[i] / KSZ9131_STEP) & maxval)
1148bff5b4b3SYuiko Oshino 					<< (field_sz * i));
1149bff5b4b3SYuiko Oshino 		}
1150bff5b4b3SYuiko Oshino 
11519b420effSHeiner Kallweit 	return phy_write_mmd(phydev, 2, reg, newval);
1152bff5b4b3SYuiko Oshino }
1153bff5b4b3SYuiko Oshino 
1154bd734a74SPhilippe Schenker #define KSZ9131RN_MMD_COMMON_CTRL_REG	2
1155bd734a74SPhilippe Schenker #define KSZ9131RN_RXC_DLL_CTRL		76
1156bd734a74SPhilippe Schenker #define KSZ9131RN_TXC_DLL_CTRL		77
1157bd734a74SPhilippe Schenker #define KSZ9131RN_DLL_CTRL_BYPASS	BIT_MASK(12)
1158bd734a74SPhilippe Schenker #define KSZ9131RN_DLL_ENABLE_DELAY	0
1159bd734a74SPhilippe Schenker #define KSZ9131RN_DLL_DISABLE_DELAY	BIT(12)
1160bd734a74SPhilippe Schenker 
1161bd734a74SPhilippe Schenker static int ksz9131_config_rgmii_delay(struct phy_device *phydev)
1162bd734a74SPhilippe Schenker {
1163bd734a74SPhilippe Schenker 	u16 rxcdll_val, txcdll_val;
1164bd734a74SPhilippe Schenker 	int ret;
1165bd734a74SPhilippe Schenker 
1166bd734a74SPhilippe Schenker 	switch (phydev->interface) {
1167bd734a74SPhilippe Schenker 	case PHY_INTERFACE_MODE_RGMII:
1168bd734a74SPhilippe Schenker 		rxcdll_val = KSZ9131RN_DLL_DISABLE_DELAY;
1169bd734a74SPhilippe Schenker 		txcdll_val = KSZ9131RN_DLL_DISABLE_DELAY;
1170bd734a74SPhilippe Schenker 		break;
1171bd734a74SPhilippe Schenker 	case PHY_INTERFACE_MODE_RGMII_ID:
1172bd734a74SPhilippe Schenker 		rxcdll_val = KSZ9131RN_DLL_ENABLE_DELAY;
1173bd734a74SPhilippe Schenker 		txcdll_val = KSZ9131RN_DLL_ENABLE_DELAY;
1174bd734a74SPhilippe Schenker 		break;
1175bd734a74SPhilippe Schenker 	case PHY_INTERFACE_MODE_RGMII_RXID:
1176bd734a74SPhilippe Schenker 		rxcdll_val = KSZ9131RN_DLL_ENABLE_DELAY;
1177bd734a74SPhilippe Schenker 		txcdll_val = KSZ9131RN_DLL_DISABLE_DELAY;
1178bd734a74SPhilippe Schenker 		break;
1179bd734a74SPhilippe Schenker 	case PHY_INTERFACE_MODE_RGMII_TXID:
1180bd734a74SPhilippe Schenker 		rxcdll_val = KSZ9131RN_DLL_DISABLE_DELAY;
1181bd734a74SPhilippe Schenker 		txcdll_val = KSZ9131RN_DLL_ENABLE_DELAY;
1182bd734a74SPhilippe Schenker 		break;
1183bd734a74SPhilippe Schenker 	default:
1184bd734a74SPhilippe Schenker 		return 0;
1185bd734a74SPhilippe Schenker 	}
1186bd734a74SPhilippe Schenker 
1187bd734a74SPhilippe Schenker 	ret = phy_modify_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
1188bd734a74SPhilippe Schenker 			     KSZ9131RN_RXC_DLL_CTRL, KSZ9131RN_DLL_CTRL_BYPASS,
1189bd734a74SPhilippe Schenker 			     rxcdll_val);
1190bd734a74SPhilippe Schenker 	if (ret < 0)
1191bd734a74SPhilippe Schenker 		return ret;
1192bd734a74SPhilippe Schenker 
1193bd734a74SPhilippe Schenker 	return phy_modify_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
1194bd734a74SPhilippe Schenker 			      KSZ9131RN_TXC_DLL_CTRL, KSZ9131RN_DLL_CTRL_BYPASS,
1195bd734a74SPhilippe Schenker 			      txcdll_val);
1196bd734a74SPhilippe Schenker }
1197bd734a74SPhilippe Schenker 
11980316c7e6SFrancesco Dolcini /* Silicon Errata DS80000693B
11990316c7e6SFrancesco Dolcini  *
12000316c7e6SFrancesco Dolcini  * When LEDs are configured in Individual Mode, LED1 is ON in a no-link
12010316c7e6SFrancesco Dolcini  * condition. Workaround is to set register 0x1e, bit 9, this way LED1 behaves
12020316c7e6SFrancesco Dolcini  * according to the datasheet (off if there is no link).
12030316c7e6SFrancesco Dolcini  */
12040316c7e6SFrancesco Dolcini static int ksz9131_led_errata(struct phy_device *phydev)
12050316c7e6SFrancesco Dolcini {
12060316c7e6SFrancesco Dolcini 	int reg;
12070316c7e6SFrancesco Dolcini 
12080316c7e6SFrancesco Dolcini 	reg = phy_read_mmd(phydev, 2, 0);
12090316c7e6SFrancesco Dolcini 	if (reg < 0)
12100316c7e6SFrancesco Dolcini 		return reg;
12110316c7e6SFrancesco Dolcini 
12120316c7e6SFrancesco Dolcini 	if (!(reg & BIT(4)))
12130316c7e6SFrancesco Dolcini 		return 0;
12140316c7e6SFrancesco Dolcini 
12150316c7e6SFrancesco Dolcini 	return phy_set_bits(phydev, 0x1e, BIT(9));
12160316c7e6SFrancesco Dolcini }
12170316c7e6SFrancesco Dolcini 
1218bff5b4b3SYuiko Oshino static int ksz9131_config_init(struct phy_device *phydev)
1219bff5b4b3SYuiko Oshino {
1220ce4f8afdSColin Ian King 	struct device_node *of_node;
1221bff5b4b3SYuiko Oshino 	char *clk_skews[2] = {"rxc-skew-psec", "txc-skew-psec"};
1222bff5b4b3SYuiko Oshino 	char *rx_data_skews[4] = {
1223bff5b4b3SYuiko Oshino 		"rxd0-skew-psec", "rxd1-skew-psec",
1224bff5b4b3SYuiko Oshino 		"rxd2-skew-psec", "rxd3-skew-psec"
1225bff5b4b3SYuiko Oshino 	};
1226bff5b4b3SYuiko Oshino 	char *tx_data_skews[4] = {
1227bff5b4b3SYuiko Oshino 		"txd0-skew-psec", "txd1-skew-psec",
1228bff5b4b3SYuiko Oshino 		"txd2-skew-psec", "txd3-skew-psec"
1229bff5b4b3SYuiko Oshino 	};
1230bff5b4b3SYuiko Oshino 	char *control_skews[2] = {"txen-skew-psec", "rxdv-skew-psec"};
1231bff5b4b3SYuiko Oshino 	const struct device *dev_walker;
1232bff5b4b3SYuiko Oshino 	int ret;
1233bff5b4b3SYuiko Oshino 
1234bff5b4b3SYuiko Oshino 	dev_walker = &phydev->mdio.dev;
1235bff5b4b3SYuiko Oshino 	do {
1236bff5b4b3SYuiko Oshino 		of_node = dev_walker->of_node;
1237bff5b4b3SYuiko Oshino 		dev_walker = dev_walker->parent;
1238bff5b4b3SYuiko Oshino 	} while (!of_node && dev_walker);
1239bff5b4b3SYuiko Oshino 
1240bff5b4b3SYuiko Oshino 	if (!of_node)
1241bff5b4b3SYuiko Oshino 		return 0;
1242bff5b4b3SYuiko Oshino 
1243bd734a74SPhilippe Schenker 	if (phy_interface_is_rgmii(phydev)) {
1244bd734a74SPhilippe Schenker 		ret = ksz9131_config_rgmii_delay(phydev);
1245bd734a74SPhilippe Schenker 		if (ret < 0)
1246bd734a74SPhilippe Schenker 			return ret;
1247bd734a74SPhilippe Schenker 	}
1248bd734a74SPhilippe Schenker 
1249bff5b4b3SYuiko Oshino 	ret = ksz9131_of_load_skew_values(phydev, of_node,
1250bff5b4b3SYuiko Oshino 					  MII_KSZ9031RN_CLK_PAD_SKEW, 5,
1251bff5b4b3SYuiko Oshino 					  clk_skews, 2);
1252bff5b4b3SYuiko Oshino 	if (ret < 0)
1253bff5b4b3SYuiko Oshino 		return ret;
1254bff5b4b3SYuiko Oshino 
1255bff5b4b3SYuiko Oshino 	ret = ksz9131_of_load_skew_values(phydev, of_node,
1256bff5b4b3SYuiko Oshino 					  MII_KSZ9031RN_CONTROL_PAD_SKEW, 4,
1257bff5b4b3SYuiko Oshino 					  control_skews, 2);
1258bff5b4b3SYuiko Oshino 	if (ret < 0)
1259bff5b4b3SYuiko Oshino 		return ret;
1260bff5b4b3SYuiko Oshino 
1261bff5b4b3SYuiko Oshino 	ret = ksz9131_of_load_skew_values(phydev, of_node,
1262bff5b4b3SYuiko Oshino 					  MII_KSZ9031RN_RX_DATA_PAD_SKEW, 4,
1263bff5b4b3SYuiko Oshino 					  rx_data_skews, 4);
1264bff5b4b3SYuiko Oshino 	if (ret < 0)
1265bff5b4b3SYuiko Oshino 		return ret;
1266bff5b4b3SYuiko Oshino 
1267bff5b4b3SYuiko Oshino 	ret = ksz9131_of_load_skew_values(phydev, of_node,
1268bff5b4b3SYuiko Oshino 					  MII_KSZ9031RN_TX_DATA_PAD_SKEW, 4,
1269bff5b4b3SYuiko Oshino 					  tx_data_skews, 4);
1270bff5b4b3SYuiko Oshino 	if (ret < 0)
1271bff5b4b3SYuiko Oshino 		return ret;
1272bff5b4b3SYuiko Oshino 
12730316c7e6SFrancesco Dolcini 	ret = ksz9131_led_errata(phydev);
12740316c7e6SFrancesco Dolcini 	if (ret < 0)
12750316c7e6SFrancesco Dolcini 		return ret;
12760316c7e6SFrancesco Dolcini 
1277bff5b4b3SYuiko Oshino 	return 0;
1278bff5b4b3SYuiko Oshino }
1279bff5b4b3SYuiko Oshino 
128093272e07SJean-Christophe PLAGNIOL-VILLARD #define KSZ8873MLL_GLOBAL_CONTROL_4	0x06
128100aee095SJohan Hovold #define KSZ8873MLL_GLOBAL_CONTROL_4_DUPLEX	BIT(6)
128200aee095SJohan Hovold #define KSZ8873MLL_GLOBAL_CONTROL_4_SPEED	BIT(4)
128332d73b14SJingoo Han static int ksz8873mll_read_status(struct phy_device *phydev)
128493272e07SJean-Christophe PLAGNIOL-VILLARD {
128593272e07SJean-Christophe PLAGNIOL-VILLARD 	int regval;
128693272e07SJean-Christophe PLAGNIOL-VILLARD 
128793272e07SJean-Christophe PLAGNIOL-VILLARD 	/* dummy read */
128893272e07SJean-Christophe PLAGNIOL-VILLARD 	regval = phy_read(phydev, KSZ8873MLL_GLOBAL_CONTROL_4);
128993272e07SJean-Christophe PLAGNIOL-VILLARD 
129093272e07SJean-Christophe PLAGNIOL-VILLARD 	regval = phy_read(phydev, KSZ8873MLL_GLOBAL_CONTROL_4);
129193272e07SJean-Christophe PLAGNIOL-VILLARD 
129293272e07SJean-Christophe PLAGNIOL-VILLARD 	if (regval & KSZ8873MLL_GLOBAL_CONTROL_4_DUPLEX)
129393272e07SJean-Christophe PLAGNIOL-VILLARD 		phydev->duplex = DUPLEX_HALF;
129493272e07SJean-Christophe PLAGNIOL-VILLARD 	else
129593272e07SJean-Christophe PLAGNIOL-VILLARD 		phydev->duplex = DUPLEX_FULL;
129693272e07SJean-Christophe PLAGNIOL-VILLARD 
129793272e07SJean-Christophe PLAGNIOL-VILLARD 	if (regval & KSZ8873MLL_GLOBAL_CONTROL_4_SPEED)
129893272e07SJean-Christophe PLAGNIOL-VILLARD 		phydev->speed = SPEED_10;
129993272e07SJean-Christophe PLAGNIOL-VILLARD 	else
130093272e07SJean-Christophe PLAGNIOL-VILLARD 		phydev->speed = SPEED_100;
130193272e07SJean-Christophe PLAGNIOL-VILLARD 
130293272e07SJean-Christophe PLAGNIOL-VILLARD 	phydev->link = 1;
130393272e07SJean-Christophe PLAGNIOL-VILLARD 	phydev->pause = phydev->asym_pause = 0;
130493272e07SJean-Christophe PLAGNIOL-VILLARD 
130593272e07SJean-Christophe PLAGNIOL-VILLARD 	return 0;
130693272e07SJean-Christophe PLAGNIOL-VILLARD }
130793272e07SJean-Christophe PLAGNIOL-VILLARD 
13083aed3e2aSAntoine Tenart static int ksz9031_get_features(struct phy_device *phydev)
13093aed3e2aSAntoine Tenart {
13103aed3e2aSAntoine Tenart 	int ret;
13113aed3e2aSAntoine Tenart 
13123aed3e2aSAntoine Tenart 	ret = genphy_read_abilities(phydev);
13133aed3e2aSAntoine Tenart 	if (ret < 0)
13143aed3e2aSAntoine Tenart 		return ret;
13153aed3e2aSAntoine Tenart 
13163aed3e2aSAntoine Tenart 	/* Silicon Errata Sheet (DS80000691D or DS80000692D):
13173aed3e2aSAntoine Tenart 	 * Whenever the device's Asymmetric Pause capability is set to 1,
13183aed3e2aSAntoine Tenart 	 * link-up may fail after a link-up to link-down transition.
13193aed3e2aSAntoine Tenart 	 *
1320407d8098SHans Andersson 	 * The Errata Sheet is for ksz9031, but ksz9021 has the same issue
1321407d8098SHans Andersson 	 *
13223aed3e2aSAntoine Tenart 	 * Workaround:
13233aed3e2aSAntoine Tenart 	 * Do not enable the Asymmetric Pause capability bit.
13243aed3e2aSAntoine Tenart 	 */
13253aed3e2aSAntoine Tenart 	linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported);
13263aed3e2aSAntoine Tenart 
13273aed3e2aSAntoine Tenart 	/* We force setting the Pause capability as the core will force the
13283aed3e2aSAntoine Tenart 	 * Asymmetric Pause capability to 1 otherwise.
13293aed3e2aSAntoine Tenart 	 */
13303aed3e2aSAntoine Tenart 	linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported);
13313aed3e2aSAntoine Tenart 
13323aed3e2aSAntoine Tenart 	return 0;
13333aed3e2aSAntoine Tenart }
13343aed3e2aSAntoine Tenart 
1335d2fd719bSNathan Sullivan static int ksz9031_read_status(struct phy_device *phydev)
1336d2fd719bSNathan Sullivan {
1337d2fd719bSNathan Sullivan 	int err;
1338d2fd719bSNathan Sullivan 	int regval;
1339d2fd719bSNathan Sullivan 
1340d2fd719bSNathan Sullivan 	err = genphy_read_status(phydev);
1341d2fd719bSNathan Sullivan 	if (err)
1342d2fd719bSNathan Sullivan 		return err;
1343d2fd719bSNathan Sullivan 
1344d2fd719bSNathan Sullivan 	/* Make sure the PHY is not broken. Read idle error count,
1345d2fd719bSNathan Sullivan 	 * and reset the PHY if it is maxed out.
1346d2fd719bSNathan Sullivan 	 */
1347d2fd719bSNathan Sullivan 	regval = phy_read(phydev, MII_STAT1000);
1348d2fd719bSNathan Sullivan 	if ((regval & 0xFF) == 0xFF) {
1349d2fd719bSNathan Sullivan 		phy_init_hw(phydev);
1350d2fd719bSNathan Sullivan 		phydev->link = 0;
1351b866203dSZach Brown 		if (phydev->drv->config_intr && phy_interrupt_is_valid(phydev))
1352b866203dSZach Brown 			phydev->drv->config_intr(phydev);
1353c1a8d0a3SGrygorii Strashko 		return genphy_config_aneg(phydev);
1354d2fd719bSNathan Sullivan 	}
1355d2fd719bSNathan Sullivan 
1356d2fd719bSNathan Sullivan 	return 0;
1357d2fd719bSNathan Sullivan }
1358d2fd719bSNathan Sullivan 
135958389c00SMarek Vasut static int ksz9x31_cable_test_start(struct phy_device *phydev)
136058389c00SMarek Vasut {
136158389c00SMarek Vasut 	struct kszphy_priv *priv = phydev->priv;
136258389c00SMarek Vasut 	int ret;
136358389c00SMarek Vasut 
136458389c00SMarek Vasut 	/* KSZ9131RNX, DS00002841B-page 38, 4.14 LinkMD (R) Cable Diagnostic
136558389c00SMarek Vasut 	 * Prior to running the cable diagnostics, Auto-negotiation should
136658389c00SMarek Vasut 	 * be disabled, full duplex set and the link speed set to 1000Mbps
136758389c00SMarek Vasut 	 * via the Basic Control Register.
136858389c00SMarek Vasut 	 */
136958389c00SMarek Vasut 	ret = phy_modify(phydev, MII_BMCR,
137058389c00SMarek Vasut 			 BMCR_SPEED1000 | BMCR_FULLDPLX |
137158389c00SMarek Vasut 			 BMCR_ANENABLE | BMCR_SPEED100,
137258389c00SMarek Vasut 			 BMCR_SPEED1000 | BMCR_FULLDPLX);
137358389c00SMarek Vasut 	if (ret)
137458389c00SMarek Vasut 		return ret;
137558389c00SMarek Vasut 
137658389c00SMarek Vasut 	/* KSZ9131RNX, DS00002841B-page 38, 4.14 LinkMD (R) Cable Diagnostic
137758389c00SMarek Vasut 	 * The Master-Slave configuration should be set to Slave by writing
137858389c00SMarek Vasut 	 * a value of 0x1000 to the Auto-Negotiation Master Slave Control
137958389c00SMarek Vasut 	 * Register.
138058389c00SMarek Vasut 	 */
138158389c00SMarek Vasut 	ret = phy_read(phydev, MII_CTRL1000);
138258389c00SMarek Vasut 	if (ret < 0)
138358389c00SMarek Vasut 		return ret;
138458389c00SMarek Vasut 
138558389c00SMarek Vasut 	/* Cache these bits, they need to be restored once LinkMD finishes. */
138658389c00SMarek Vasut 	priv->vct_ctrl1000 = ret & (CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER);
138758389c00SMarek Vasut 	ret &= ~(CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER);
138858389c00SMarek Vasut 	ret |= CTL1000_ENABLE_MASTER;
138958389c00SMarek Vasut 
139058389c00SMarek Vasut 	return phy_write(phydev, MII_CTRL1000, ret);
139158389c00SMarek Vasut }
139258389c00SMarek Vasut 
139358389c00SMarek Vasut static int ksz9x31_cable_test_result_trans(u16 status)
139458389c00SMarek Vasut {
139558389c00SMarek Vasut 	switch (FIELD_GET(KSZ9x31_LMD_VCT_ST_MASK, status)) {
139658389c00SMarek Vasut 	case KSZ9x31_LMD_VCT_ST_NORMAL:
139758389c00SMarek Vasut 		return ETHTOOL_A_CABLE_RESULT_CODE_OK;
139858389c00SMarek Vasut 	case KSZ9x31_LMD_VCT_ST_OPEN:
139958389c00SMarek Vasut 		return ETHTOOL_A_CABLE_RESULT_CODE_OPEN;
140058389c00SMarek Vasut 	case KSZ9x31_LMD_VCT_ST_SHORT:
140158389c00SMarek Vasut 		return ETHTOOL_A_CABLE_RESULT_CODE_SAME_SHORT;
140258389c00SMarek Vasut 	case KSZ9x31_LMD_VCT_ST_FAIL:
140358389c00SMarek Vasut 		fallthrough;
140458389c00SMarek Vasut 	default:
140558389c00SMarek Vasut 		return ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC;
140658389c00SMarek Vasut 	}
140758389c00SMarek Vasut }
140858389c00SMarek Vasut 
140958389c00SMarek Vasut static bool ksz9x31_cable_test_failed(u16 status)
141058389c00SMarek Vasut {
141158389c00SMarek Vasut 	int stat = FIELD_GET(KSZ9x31_LMD_VCT_ST_MASK, status);
141258389c00SMarek Vasut 
141358389c00SMarek Vasut 	return stat == KSZ9x31_LMD_VCT_ST_FAIL;
141458389c00SMarek Vasut }
141558389c00SMarek Vasut 
141658389c00SMarek Vasut static bool ksz9x31_cable_test_fault_length_valid(u16 status)
141758389c00SMarek Vasut {
141858389c00SMarek Vasut 	switch (FIELD_GET(KSZ9x31_LMD_VCT_ST_MASK, status)) {
141958389c00SMarek Vasut 	case KSZ9x31_LMD_VCT_ST_OPEN:
142058389c00SMarek Vasut 		fallthrough;
142158389c00SMarek Vasut 	case KSZ9x31_LMD_VCT_ST_SHORT:
142258389c00SMarek Vasut 		return true;
142358389c00SMarek Vasut 	}
142458389c00SMarek Vasut 	return false;
142558389c00SMarek Vasut }
142658389c00SMarek Vasut 
142758389c00SMarek Vasut static int ksz9x31_cable_test_fault_length(struct phy_device *phydev, u16 stat)
142858389c00SMarek Vasut {
142958389c00SMarek Vasut 	int dt = FIELD_GET(KSZ9x31_LMD_VCT_DATA_MASK, stat);
143058389c00SMarek Vasut 
143158389c00SMarek Vasut 	/* KSZ9131RNX, DS00002841B-page 38, 4.14 LinkMD (R) Cable Diagnostic
143258389c00SMarek Vasut 	 *
143358389c00SMarek Vasut 	 * distance to fault = (VCT_DATA - 22) * 4 / cable propagation velocity
143458389c00SMarek Vasut 	 */
143558389c00SMarek Vasut 	if ((phydev->phy_id & MICREL_PHY_ID_MASK) == PHY_ID_KSZ9131)
143658389c00SMarek Vasut 		dt = clamp(dt - 22, 0, 255);
143758389c00SMarek Vasut 
143858389c00SMarek Vasut 	return (dt * 400) / 10;
143958389c00SMarek Vasut }
144058389c00SMarek Vasut 
144158389c00SMarek Vasut static int ksz9x31_cable_test_wait_for_completion(struct phy_device *phydev)
144258389c00SMarek Vasut {
144358389c00SMarek Vasut 	int val, ret;
144458389c00SMarek Vasut 
144558389c00SMarek Vasut 	ret = phy_read_poll_timeout(phydev, KSZ9x31_LMD, val,
144658389c00SMarek Vasut 				    !(val & KSZ9x31_LMD_VCT_EN),
144758389c00SMarek Vasut 				    30000, 100000, true);
144858389c00SMarek Vasut 
144958389c00SMarek Vasut 	return ret < 0 ? ret : 0;
145058389c00SMarek Vasut }
145158389c00SMarek Vasut 
145258389c00SMarek Vasut static int ksz9x31_cable_test_get_pair(int pair)
145358389c00SMarek Vasut {
145458389c00SMarek Vasut 	static const int ethtool_pair[] = {
145558389c00SMarek Vasut 		ETHTOOL_A_CABLE_PAIR_A,
145658389c00SMarek Vasut 		ETHTOOL_A_CABLE_PAIR_B,
145758389c00SMarek Vasut 		ETHTOOL_A_CABLE_PAIR_C,
145858389c00SMarek Vasut 		ETHTOOL_A_CABLE_PAIR_D,
145958389c00SMarek Vasut 	};
146058389c00SMarek Vasut 
146158389c00SMarek Vasut 	return ethtool_pair[pair];
146258389c00SMarek Vasut }
146358389c00SMarek Vasut 
146458389c00SMarek Vasut static int ksz9x31_cable_test_one_pair(struct phy_device *phydev, int pair)
146558389c00SMarek Vasut {
146658389c00SMarek Vasut 	int ret, val;
146758389c00SMarek Vasut 
146858389c00SMarek Vasut 	/* KSZ9131RNX, DS00002841B-page 38, 4.14 LinkMD (R) Cable Diagnostic
146958389c00SMarek Vasut 	 * To test each individual cable pair, set the cable pair in the Cable
147058389c00SMarek Vasut 	 * Diagnostics Test Pair (VCT_PAIR[1:0]) field of the LinkMD Cable
147158389c00SMarek Vasut 	 * Diagnostic Register, along with setting the Cable Diagnostics Test
147258389c00SMarek Vasut 	 * Enable (VCT_EN) bit. The Cable Diagnostics Test Enable (VCT_EN) bit
147358389c00SMarek Vasut 	 * will self clear when the test is concluded.
147458389c00SMarek Vasut 	 */
147558389c00SMarek Vasut 	ret = phy_write(phydev, KSZ9x31_LMD,
147658389c00SMarek Vasut 			KSZ9x31_LMD_VCT_EN | KSZ9x31_LMD_VCT_PAIR(pair));
147758389c00SMarek Vasut 	if (ret)
147858389c00SMarek Vasut 		return ret;
147958389c00SMarek Vasut 
148058389c00SMarek Vasut 	ret = ksz9x31_cable_test_wait_for_completion(phydev);
148158389c00SMarek Vasut 	if (ret)
148258389c00SMarek Vasut 		return ret;
148358389c00SMarek Vasut 
148458389c00SMarek Vasut 	val = phy_read(phydev, KSZ9x31_LMD);
148558389c00SMarek Vasut 	if (val < 0)
148658389c00SMarek Vasut 		return val;
148758389c00SMarek Vasut 
148858389c00SMarek Vasut 	if (ksz9x31_cable_test_failed(val))
148958389c00SMarek Vasut 		return -EAGAIN;
149058389c00SMarek Vasut 
149158389c00SMarek Vasut 	ret = ethnl_cable_test_result(phydev,
149258389c00SMarek Vasut 				      ksz9x31_cable_test_get_pair(pair),
149358389c00SMarek Vasut 				      ksz9x31_cable_test_result_trans(val));
149458389c00SMarek Vasut 	if (ret)
149558389c00SMarek Vasut 		return ret;
149658389c00SMarek Vasut 
149758389c00SMarek Vasut 	if (!ksz9x31_cable_test_fault_length_valid(val))
149858389c00SMarek Vasut 		return 0;
149958389c00SMarek Vasut 
150058389c00SMarek Vasut 	return ethnl_cable_test_fault_length(phydev,
150158389c00SMarek Vasut 					     ksz9x31_cable_test_get_pair(pair),
150258389c00SMarek Vasut 					     ksz9x31_cable_test_fault_length(phydev, val));
150358389c00SMarek Vasut }
150458389c00SMarek Vasut 
150558389c00SMarek Vasut static int ksz9x31_cable_test_get_status(struct phy_device *phydev,
150658389c00SMarek Vasut 					 bool *finished)
150758389c00SMarek Vasut {
150858389c00SMarek Vasut 	struct kszphy_priv *priv = phydev->priv;
150958389c00SMarek Vasut 	unsigned long pair_mask = 0xf;
151058389c00SMarek Vasut 	int retries = 20;
151158389c00SMarek Vasut 	int pair, ret, rv;
151258389c00SMarek Vasut 
151358389c00SMarek Vasut 	*finished = false;
151458389c00SMarek Vasut 
151558389c00SMarek Vasut 	/* Try harder if link partner is active */
151658389c00SMarek Vasut 	while (pair_mask && retries--) {
151758389c00SMarek Vasut 		for_each_set_bit(pair, &pair_mask, 4) {
151858389c00SMarek Vasut 			ret = ksz9x31_cable_test_one_pair(phydev, pair);
151958389c00SMarek Vasut 			if (ret == -EAGAIN)
152058389c00SMarek Vasut 				continue;
152158389c00SMarek Vasut 			if (ret < 0)
152258389c00SMarek Vasut 				return ret;
152358389c00SMarek Vasut 			clear_bit(pair, &pair_mask);
152458389c00SMarek Vasut 		}
152558389c00SMarek Vasut 		/* If link partner is in autonegotiation mode it will send 2ms
152658389c00SMarek Vasut 		 * of FLPs with at least 6ms of silence.
152758389c00SMarek Vasut 		 * Add 2ms sleep to have better chances to hit this silence.
152858389c00SMarek Vasut 		 */
152958389c00SMarek Vasut 		if (pair_mask)
153058389c00SMarek Vasut 			usleep_range(2000, 3000);
153158389c00SMarek Vasut 	}
153258389c00SMarek Vasut 
153358389c00SMarek Vasut 	/* Report remaining unfinished pair result as unknown. */
153458389c00SMarek Vasut 	for_each_set_bit(pair, &pair_mask, 4) {
153558389c00SMarek Vasut 		ret = ethnl_cable_test_result(phydev,
153658389c00SMarek Vasut 					      ksz9x31_cable_test_get_pair(pair),
153758389c00SMarek Vasut 					      ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC);
153858389c00SMarek Vasut 	}
153958389c00SMarek Vasut 
154058389c00SMarek Vasut 	*finished = true;
154158389c00SMarek Vasut 
154258389c00SMarek Vasut 	/* Restore cached bits from before LinkMD got started. */
154358389c00SMarek Vasut 	rv = phy_modify(phydev, MII_CTRL1000,
154458389c00SMarek Vasut 			CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER,
154558389c00SMarek Vasut 			priv->vct_ctrl1000);
154658389c00SMarek Vasut 	if (rv)
154758389c00SMarek Vasut 		return rv;
154858389c00SMarek Vasut 
154958389c00SMarek Vasut 	return ret;
155058389c00SMarek Vasut }
155158389c00SMarek Vasut 
155293272e07SJean-Christophe PLAGNIOL-VILLARD static int ksz8873mll_config_aneg(struct phy_device *phydev)
155393272e07SJean-Christophe PLAGNIOL-VILLARD {
155493272e07SJean-Christophe PLAGNIOL-VILLARD 	return 0;
155593272e07SJean-Christophe PLAGNIOL-VILLARD }
155693272e07SJean-Christophe PLAGNIOL-VILLARD 
155752939393SOleksij Rempel static int ksz886x_config_mdix(struct phy_device *phydev, u8 ctrl)
155852939393SOleksij Rempel {
155952939393SOleksij Rempel 	u16 val;
156052939393SOleksij Rempel 
156152939393SOleksij Rempel 	switch (ctrl) {
156252939393SOleksij Rempel 	case ETH_TP_MDI:
156352939393SOleksij Rempel 		val = KSZ886X_BMCR_DISABLE_AUTO_MDIX;
156452939393SOleksij Rempel 		break;
156552939393SOleksij Rempel 	case ETH_TP_MDI_X:
156652939393SOleksij Rempel 		/* Note: The naming of the bit KSZ886X_BMCR_FORCE_MDI is bit
156752939393SOleksij Rempel 		 * counter intuitive, the "-X" in "1 = Force MDI" in the data
156852939393SOleksij Rempel 		 * sheet seems to be missing:
156952939393SOleksij Rempel 		 * 1 = Force MDI (sic!) (transmit on RX+/RX- pins)
157052939393SOleksij Rempel 		 * 0 = Normal operation (transmit on TX+/TX- pins)
157152939393SOleksij Rempel 		 */
157252939393SOleksij Rempel 		val = KSZ886X_BMCR_DISABLE_AUTO_MDIX | KSZ886X_BMCR_FORCE_MDI;
157352939393SOleksij Rempel 		break;
157452939393SOleksij Rempel 	case ETH_TP_MDI_AUTO:
157552939393SOleksij Rempel 		val = 0;
157652939393SOleksij Rempel 		break;
157752939393SOleksij Rempel 	default:
157852939393SOleksij Rempel 		return 0;
157952939393SOleksij Rempel 	}
158052939393SOleksij Rempel 
158152939393SOleksij Rempel 	return phy_modify(phydev, MII_BMCR,
158252939393SOleksij Rempel 			  KSZ886X_BMCR_HP_MDIX | KSZ886X_BMCR_FORCE_MDI |
158352939393SOleksij Rempel 			  KSZ886X_BMCR_DISABLE_AUTO_MDIX,
158452939393SOleksij Rempel 			  KSZ886X_BMCR_HP_MDIX | val);
158552939393SOleksij Rempel }
158652939393SOleksij Rempel 
158752939393SOleksij Rempel static int ksz886x_config_aneg(struct phy_device *phydev)
158852939393SOleksij Rempel {
158952939393SOleksij Rempel 	int ret;
159052939393SOleksij Rempel 
159152939393SOleksij Rempel 	ret = genphy_config_aneg(phydev);
159252939393SOleksij Rempel 	if (ret)
159352939393SOleksij Rempel 		return ret;
159452939393SOleksij Rempel 
159552939393SOleksij Rempel 	/* The MDI-X configuration is automatically changed by the PHY after
159652939393SOleksij Rempel 	 * switching from autoneg off to on. So, take MDI-X configuration under
159752939393SOleksij Rempel 	 * own control and set it after autoneg configuration was done.
159852939393SOleksij Rempel 	 */
159952939393SOleksij Rempel 	return ksz886x_config_mdix(phydev, phydev->mdix_ctrl);
160052939393SOleksij Rempel }
160152939393SOleksij Rempel 
160252939393SOleksij Rempel static int ksz886x_mdix_update(struct phy_device *phydev)
160352939393SOleksij Rempel {
160452939393SOleksij Rempel 	int ret;
160552939393SOleksij Rempel 
160652939393SOleksij Rempel 	ret = phy_read(phydev, MII_BMCR);
160752939393SOleksij Rempel 	if (ret < 0)
160852939393SOleksij Rempel 		return ret;
160952939393SOleksij Rempel 
161052939393SOleksij Rempel 	if (ret & KSZ886X_BMCR_DISABLE_AUTO_MDIX) {
161152939393SOleksij Rempel 		if (ret & KSZ886X_BMCR_FORCE_MDI)
161252939393SOleksij Rempel 			phydev->mdix_ctrl = ETH_TP_MDI_X;
161352939393SOleksij Rempel 		else
161452939393SOleksij Rempel 			phydev->mdix_ctrl = ETH_TP_MDI;
161552939393SOleksij Rempel 	} else {
161652939393SOleksij Rempel 		phydev->mdix_ctrl = ETH_TP_MDI_AUTO;
161752939393SOleksij Rempel 	}
161852939393SOleksij Rempel 
161952939393SOleksij Rempel 	ret = phy_read(phydev, MII_KSZPHY_CTRL);
162052939393SOleksij Rempel 	if (ret < 0)
162152939393SOleksij Rempel 		return ret;
162252939393SOleksij Rempel 
162352939393SOleksij Rempel 	/* Same reverse logic as KSZ886X_BMCR_FORCE_MDI */
162452939393SOleksij Rempel 	if (ret & KSZ886X_CTRL_MDIX_STAT)
162552939393SOleksij Rempel 		phydev->mdix = ETH_TP_MDI_X;
162652939393SOleksij Rempel 	else
162752939393SOleksij Rempel 		phydev->mdix = ETH_TP_MDI;
162852939393SOleksij Rempel 
162952939393SOleksij Rempel 	return 0;
163052939393SOleksij Rempel }
163152939393SOleksij Rempel 
163252939393SOleksij Rempel static int ksz886x_read_status(struct phy_device *phydev)
163352939393SOleksij Rempel {
163452939393SOleksij Rempel 	int ret;
163552939393SOleksij Rempel 
163652939393SOleksij Rempel 	ret = ksz886x_mdix_update(phydev);
163752939393SOleksij Rempel 	if (ret < 0)
163852939393SOleksij Rempel 		return ret;
163952939393SOleksij Rempel 
164052939393SOleksij Rempel 	return genphy_read_status(phydev);
164152939393SOleksij Rempel }
164252939393SOleksij Rempel 
16432b2427d0SAndrew Lunn static int kszphy_get_sset_count(struct phy_device *phydev)
16442b2427d0SAndrew Lunn {
16452b2427d0SAndrew Lunn 	return ARRAY_SIZE(kszphy_hw_stats);
16462b2427d0SAndrew Lunn }
16472b2427d0SAndrew Lunn 
16482b2427d0SAndrew Lunn static void kszphy_get_strings(struct phy_device *phydev, u8 *data)
16492b2427d0SAndrew Lunn {
16502b2427d0SAndrew Lunn 	int i;
16512b2427d0SAndrew Lunn 
16522b2427d0SAndrew Lunn 	for (i = 0; i < ARRAY_SIZE(kszphy_hw_stats); i++) {
165355f53567SFlorian Fainelli 		strlcpy(data + i * ETH_GSTRING_LEN,
16542b2427d0SAndrew Lunn 			kszphy_hw_stats[i].string, ETH_GSTRING_LEN);
16552b2427d0SAndrew Lunn 	}
16562b2427d0SAndrew Lunn }
16572b2427d0SAndrew Lunn 
16582b2427d0SAndrew Lunn static u64 kszphy_get_stat(struct phy_device *phydev, int i)
16592b2427d0SAndrew Lunn {
16602b2427d0SAndrew Lunn 	struct kszphy_hw_stat stat = kszphy_hw_stats[i];
16612b2427d0SAndrew Lunn 	struct kszphy_priv *priv = phydev->priv;
1662321b4d4bSAndrew Lunn 	int val;
1663321b4d4bSAndrew Lunn 	u64 ret;
16642b2427d0SAndrew Lunn 
16652b2427d0SAndrew Lunn 	val = phy_read(phydev, stat.reg);
16662b2427d0SAndrew Lunn 	if (val < 0) {
16676c3442f5SJisheng Zhang 		ret = U64_MAX;
16682b2427d0SAndrew Lunn 	} else {
16692b2427d0SAndrew Lunn 		val = val & ((1 << stat.bits) - 1);
16702b2427d0SAndrew Lunn 		priv->stats[i] += val;
1671321b4d4bSAndrew Lunn 		ret = priv->stats[i];
16722b2427d0SAndrew Lunn 	}
16732b2427d0SAndrew Lunn 
1674321b4d4bSAndrew Lunn 	return ret;
16752b2427d0SAndrew Lunn }
16762b2427d0SAndrew Lunn 
16772b2427d0SAndrew Lunn static void kszphy_get_stats(struct phy_device *phydev,
16782b2427d0SAndrew Lunn 			     struct ethtool_stats *stats, u64 *data)
16792b2427d0SAndrew Lunn {
16802b2427d0SAndrew Lunn 	int i;
16812b2427d0SAndrew Lunn 
16822b2427d0SAndrew Lunn 	for (i = 0; i < ARRAY_SIZE(kszphy_hw_stats); i++)
16832b2427d0SAndrew Lunn 		data[i] = kszphy_get_stat(phydev, i);
16842b2427d0SAndrew Lunn }
16852b2427d0SAndrew Lunn 
1686836384d2SWenyou Yang static int kszphy_suspend(struct phy_device *phydev)
1687836384d2SWenyou Yang {
1688836384d2SWenyou Yang 	/* Disable PHY Interrupts */
1689836384d2SWenyou Yang 	if (phy_interrupt_is_valid(phydev)) {
1690836384d2SWenyou Yang 		phydev->interrupts = PHY_INTERRUPT_DISABLED;
1691836384d2SWenyou Yang 		if (phydev->drv->config_intr)
1692836384d2SWenyou Yang 			phydev->drv->config_intr(phydev);
1693836384d2SWenyou Yang 	}
1694836384d2SWenyou Yang 
1695836384d2SWenyou Yang 	return genphy_suspend(phydev);
1696836384d2SWenyou Yang }
1697836384d2SWenyou Yang 
1698a516b7f7SDivya Koppera static void kszphy_parse_led_mode(struct phy_device *phydev)
1699a516b7f7SDivya Koppera {
1700a516b7f7SDivya Koppera 	const struct kszphy_type *type = phydev->drv->driver_data;
1701a516b7f7SDivya Koppera 	const struct device_node *np = phydev->mdio.dev.of_node;
1702a516b7f7SDivya Koppera 	struct kszphy_priv *priv = phydev->priv;
1703a516b7f7SDivya Koppera 	int ret;
1704a516b7f7SDivya Koppera 
1705a516b7f7SDivya Koppera 	if (type && type->led_mode_reg) {
1706a516b7f7SDivya Koppera 		ret = of_property_read_u32(np, "micrel,led-mode",
1707a516b7f7SDivya Koppera 					   &priv->led_mode);
1708a516b7f7SDivya Koppera 
1709a516b7f7SDivya Koppera 		if (ret)
1710a516b7f7SDivya Koppera 			priv->led_mode = -1;
1711a516b7f7SDivya Koppera 
1712a516b7f7SDivya Koppera 		if (priv->led_mode > 3) {
1713a516b7f7SDivya Koppera 			phydev_err(phydev, "invalid led mode: 0x%02x\n",
1714a516b7f7SDivya Koppera 				   priv->led_mode);
1715a516b7f7SDivya Koppera 			priv->led_mode = -1;
1716a516b7f7SDivya Koppera 		}
1717a516b7f7SDivya Koppera 	} else {
1718a516b7f7SDivya Koppera 		priv->led_mode = -1;
1719a516b7f7SDivya Koppera 	}
1720a516b7f7SDivya Koppera }
1721a516b7f7SDivya Koppera 
1722f5aba91dSAlexandre Belloni static int kszphy_resume(struct phy_device *phydev)
1723f5aba91dSAlexandre Belloni {
172479e498a9SLeonard Crestez 	int ret;
172579e498a9SLeonard Crestez 
1726836384d2SWenyou Yang 	genphy_resume(phydev);
1727f5aba91dSAlexandre Belloni 
17286110dff7SOleksij Rempel 	/* After switching from power-down to normal mode, an internal global
17296110dff7SOleksij Rempel 	 * reset is automatically generated. Wait a minimum of 1 ms before
17306110dff7SOleksij Rempel 	 * read/write access to the PHY registers.
17316110dff7SOleksij Rempel 	 */
17326110dff7SOleksij Rempel 	usleep_range(1000, 2000);
17336110dff7SOleksij Rempel 
173479e498a9SLeonard Crestez 	ret = kszphy_config_reset(phydev);
173579e498a9SLeonard Crestez 	if (ret)
173679e498a9SLeonard Crestez 		return ret;
173779e498a9SLeonard Crestez 
1738836384d2SWenyou Yang 	/* Enable PHY Interrupts */
1739836384d2SWenyou Yang 	if (phy_interrupt_is_valid(phydev)) {
1740836384d2SWenyou Yang 		phydev->interrupts = PHY_INTERRUPT_ENABLED;
1741836384d2SWenyou Yang 		if (phydev->drv->config_intr)
1742836384d2SWenyou Yang 			phydev->drv->config_intr(phydev);
1743836384d2SWenyou Yang 	}
1744f5aba91dSAlexandre Belloni 
1745f5aba91dSAlexandre Belloni 	return 0;
1746f5aba91dSAlexandre Belloni }
1747f5aba91dSAlexandre Belloni 
1748e6a423a8SJohan Hovold static int kszphy_probe(struct phy_device *phydev)
1749e6a423a8SJohan Hovold {
1750e6a423a8SJohan Hovold 	const struct kszphy_type *type = phydev->drv->driver_data;
1751e5a03bfdSAndrew Lunn 	const struct device_node *np = phydev->mdio.dev.of_node;
1752e6a423a8SJohan Hovold 	struct kszphy_priv *priv;
175363f44b2bSJohan Hovold 	struct clk *clk;
1754e6a423a8SJohan Hovold 
1755e5a03bfdSAndrew Lunn 	priv = devm_kzalloc(&phydev->mdio.dev, sizeof(*priv), GFP_KERNEL);
1756e6a423a8SJohan Hovold 	if (!priv)
1757e6a423a8SJohan Hovold 		return -ENOMEM;
1758e6a423a8SJohan Hovold 
1759e6a423a8SJohan Hovold 	phydev->priv = priv;
1760e6a423a8SJohan Hovold 
1761e6a423a8SJohan Hovold 	priv->type = type;
1762e6a423a8SJohan Hovold 
1763a516b7f7SDivya Koppera 	kszphy_parse_led_mode(phydev);
1764e7a792e9SJohan Hovold 
1765e5a03bfdSAndrew Lunn 	clk = devm_clk_get(&phydev->mdio.dev, "rmii-ref");
1766bced8701SNiklas Cassel 	/* NOTE: clk may be NULL if building without CONFIG_HAVE_CLK */
1767bced8701SNiklas Cassel 	if (!IS_ERR_OR_NULL(clk)) {
17681fadee0cSSascha Hauer 		unsigned long rate = clk_get_rate(clk);
176986dc1342SJohan Hovold 		bool rmii_ref_clk_sel_25_mhz;
17701fadee0cSSascha Hauer 
1771f2ef6f75SFabio Estevam 		if (type)
177263f44b2bSJohan Hovold 			priv->rmii_ref_clk_sel = type->has_rmii_ref_clk_sel;
177386dc1342SJohan Hovold 		rmii_ref_clk_sel_25_mhz = of_property_read_bool(np,
177486dc1342SJohan Hovold 				"micrel,rmii-reference-clock-select-25-mhz");
177563f44b2bSJohan Hovold 
17761fadee0cSSascha Hauer 		if (rate > 24500000 && rate < 25500000) {
177786dc1342SJohan Hovold 			priv->rmii_ref_clk_sel_val = rmii_ref_clk_sel_25_mhz;
17781fadee0cSSascha Hauer 		} else if (rate > 49500000 && rate < 50500000) {
177986dc1342SJohan Hovold 			priv->rmii_ref_clk_sel_val = !rmii_ref_clk_sel_25_mhz;
17801fadee0cSSascha Hauer 		} else {
178172ba48beSAndrew Lunn 			phydev_err(phydev, "Clock rate out of range: %ld\n",
178272ba48beSAndrew Lunn 				   rate);
17831fadee0cSSascha Hauer 			return -EINVAL;
17841fadee0cSSascha Hauer 		}
17851fadee0cSSascha Hauer 	}
17861fadee0cSSascha Hauer 
17874217a64eSMichael Walle 	if (ksz8041_fiber_mode(phydev))
17884217a64eSMichael Walle 		phydev->port = PORT_FIBRE;
17894217a64eSMichael Walle 
179063f44b2bSJohan Hovold 	/* Support legacy board-file configuration */
179163f44b2bSJohan Hovold 	if (phydev->dev_flags & MICREL_PHY_50MHZ_CLK) {
179263f44b2bSJohan Hovold 		priv->rmii_ref_clk_sel = true;
179363f44b2bSJohan Hovold 		priv->rmii_ref_clk_sel_val = true;
179463f44b2bSJohan Hovold 	}
179563f44b2bSJohan Hovold 
179663f44b2bSJohan Hovold 	return 0;
17971fadee0cSSascha Hauer }
17981fadee0cSSascha Hauer 
179949011e0cSOleksij Rempel static int ksz886x_cable_test_start(struct phy_device *phydev)
180049011e0cSOleksij Rempel {
180149011e0cSOleksij Rempel 	if (phydev->dev_flags & MICREL_KSZ8_P1_ERRATA)
180249011e0cSOleksij Rempel 		return -EOPNOTSUPP;
180349011e0cSOleksij Rempel 
180449011e0cSOleksij Rempel 	/* If autoneg is enabled, we won't be able to test cross pair
180549011e0cSOleksij Rempel 	 * short. In this case, the PHY will "detect" a link and
180649011e0cSOleksij Rempel 	 * confuse the internal state machine - disable auto neg here.
180749011e0cSOleksij Rempel 	 * If autoneg is disabled, we should set the speed to 10mbit.
180849011e0cSOleksij Rempel 	 */
180949011e0cSOleksij Rempel 	return phy_clear_bits(phydev, MII_BMCR, BMCR_ANENABLE | BMCR_SPEED100);
181049011e0cSOleksij Rempel }
181149011e0cSOleksij Rempel 
181249011e0cSOleksij Rempel static int ksz886x_cable_test_result_trans(u16 status)
181349011e0cSOleksij Rempel {
181449011e0cSOleksij Rempel 	switch (FIELD_GET(KSZ8081_LMD_STAT_MASK, status)) {
181549011e0cSOleksij Rempel 	case KSZ8081_LMD_STAT_NORMAL:
181649011e0cSOleksij Rempel 		return ETHTOOL_A_CABLE_RESULT_CODE_OK;
181749011e0cSOleksij Rempel 	case KSZ8081_LMD_STAT_SHORT:
181849011e0cSOleksij Rempel 		return ETHTOOL_A_CABLE_RESULT_CODE_SAME_SHORT;
181949011e0cSOleksij Rempel 	case KSZ8081_LMD_STAT_OPEN:
182049011e0cSOleksij Rempel 		return ETHTOOL_A_CABLE_RESULT_CODE_OPEN;
182149011e0cSOleksij Rempel 	case KSZ8081_LMD_STAT_FAIL:
182249011e0cSOleksij Rempel 		fallthrough;
182349011e0cSOleksij Rempel 	default:
182449011e0cSOleksij Rempel 		return ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC;
182549011e0cSOleksij Rempel 	}
182649011e0cSOleksij Rempel }
182749011e0cSOleksij Rempel 
182849011e0cSOleksij Rempel static bool ksz886x_cable_test_failed(u16 status)
182949011e0cSOleksij Rempel {
183049011e0cSOleksij Rempel 	return FIELD_GET(KSZ8081_LMD_STAT_MASK, status) ==
183149011e0cSOleksij Rempel 		KSZ8081_LMD_STAT_FAIL;
183249011e0cSOleksij Rempel }
183349011e0cSOleksij Rempel 
183449011e0cSOleksij Rempel static bool ksz886x_cable_test_fault_length_valid(u16 status)
183549011e0cSOleksij Rempel {
183649011e0cSOleksij Rempel 	switch (FIELD_GET(KSZ8081_LMD_STAT_MASK, status)) {
183749011e0cSOleksij Rempel 	case KSZ8081_LMD_STAT_OPEN:
183849011e0cSOleksij Rempel 		fallthrough;
183949011e0cSOleksij Rempel 	case KSZ8081_LMD_STAT_SHORT:
184049011e0cSOleksij Rempel 		return true;
184149011e0cSOleksij Rempel 	}
184249011e0cSOleksij Rempel 	return false;
184349011e0cSOleksij Rempel }
184449011e0cSOleksij Rempel 
184549011e0cSOleksij Rempel static int ksz886x_cable_test_fault_length(u16 status)
184649011e0cSOleksij Rempel {
184749011e0cSOleksij Rempel 	int dt;
184849011e0cSOleksij Rempel 
184949011e0cSOleksij Rempel 	/* According to the data sheet the distance to the fault is
185049011e0cSOleksij Rempel 	 * DELTA_TIME * 0.4 meters.
185149011e0cSOleksij Rempel 	 */
185249011e0cSOleksij Rempel 	dt = FIELD_GET(KSZ8081_LMD_DELTA_TIME_MASK, status);
185349011e0cSOleksij Rempel 
185449011e0cSOleksij Rempel 	return (dt * 400) / 10;
185549011e0cSOleksij Rempel }
185649011e0cSOleksij Rempel 
185749011e0cSOleksij Rempel static int ksz886x_cable_test_wait_for_completion(struct phy_device *phydev)
185849011e0cSOleksij Rempel {
185949011e0cSOleksij Rempel 	int val, ret;
186049011e0cSOleksij Rempel 
186149011e0cSOleksij Rempel 	ret = phy_read_poll_timeout(phydev, KSZ8081_LMD, val,
186249011e0cSOleksij Rempel 				    !(val & KSZ8081_LMD_ENABLE_TEST),
186349011e0cSOleksij Rempel 				    30000, 100000, true);
186449011e0cSOleksij Rempel 
186549011e0cSOleksij Rempel 	return ret < 0 ? ret : 0;
186649011e0cSOleksij Rempel }
186749011e0cSOleksij Rempel 
186849011e0cSOleksij Rempel static int ksz886x_cable_test_one_pair(struct phy_device *phydev, int pair)
186949011e0cSOleksij Rempel {
187049011e0cSOleksij Rempel 	static const int ethtool_pair[] = {
187149011e0cSOleksij Rempel 		ETHTOOL_A_CABLE_PAIR_A,
187249011e0cSOleksij Rempel 		ETHTOOL_A_CABLE_PAIR_B,
187349011e0cSOleksij Rempel 	};
187449011e0cSOleksij Rempel 	int ret, val, mdix;
187549011e0cSOleksij Rempel 
187649011e0cSOleksij Rempel 	/* There is no way to choice the pair, like we do one ksz9031.
187749011e0cSOleksij Rempel 	 * We can workaround this limitation by using the MDI-X functionality.
187849011e0cSOleksij Rempel 	 */
187949011e0cSOleksij Rempel 	if (pair == 0)
188049011e0cSOleksij Rempel 		mdix = ETH_TP_MDI;
188149011e0cSOleksij Rempel 	else
188249011e0cSOleksij Rempel 		mdix = ETH_TP_MDI_X;
188349011e0cSOleksij Rempel 
188449011e0cSOleksij Rempel 	switch (phydev->phy_id & MICREL_PHY_ID_MASK) {
188549011e0cSOleksij Rempel 	case PHY_ID_KSZ8081:
188649011e0cSOleksij Rempel 		ret = ksz8081_config_mdix(phydev, mdix);
188749011e0cSOleksij Rempel 		break;
188849011e0cSOleksij Rempel 	case PHY_ID_KSZ886X:
188949011e0cSOleksij Rempel 		ret = ksz886x_config_mdix(phydev, mdix);
189049011e0cSOleksij Rempel 		break;
189149011e0cSOleksij Rempel 	default:
189249011e0cSOleksij Rempel 		ret = -ENODEV;
189349011e0cSOleksij Rempel 	}
189449011e0cSOleksij Rempel 
189549011e0cSOleksij Rempel 	if (ret)
189649011e0cSOleksij Rempel 		return ret;
189749011e0cSOleksij Rempel 
189849011e0cSOleksij Rempel 	/* Now we are ready to fire. This command will send a 100ns pulse
189949011e0cSOleksij Rempel 	 * to the pair.
190049011e0cSOleksij Rempel 	 */
190149011e0cSOleksij Rempel 	ret = phy_write(phydev, KSZ8081_LMD, KSZ8081_LMD_ENABLE_TEST);
190249011e0cSOleksij Rempel 	if (ret)
190349011e0cSOleksij Rempel 		return ret;
190449011e0cSOleksij Rempel 
190549011e0cSOleksij Rempel 	ret = ksz886x_cable_test_wait_for_completion(phydev);
190649011e0cSOleksij Rempel 	if (ret)
190749011e0cSOleksij Rempel 		return ret;
190849011e0cSOleksij Rempel 
190949011e0cSOleksij Rempel 	val = phy_read(phydev, KSZ8081_LMD);
191049011e0cSOleksij Rempel 	if (val < 0)
191149011e0cSOleksij Rempel 		return val;
191249011e0cSOleksij Rempel 
191349011e0cSOleksij Rempel 	if (ksz886x_cable_test_failed(val))
191449011e0cSOleksij Rempel 		return -EAGAIN;
191549011e0cSOleksij Rempel 
191649011e0cSOleksij Rempel 	ret = ethnl_cable_test_result(phydev, ethtool_pair[pair],
191749011e0cSOleksij Rempel 				      ksz886x_cable_test_result_trans(val));
191849011e0cSOleksij Rempel 	if (ret)
191949011e0cSOleksij Rempel 		return ret;
192049011e0cSOleksij Rempel 
192149011e0cSOleksij Rempel 	if (!ksz886x_cable_test_fault_length_valid(val))
192249011e0cSOleksij Rempel 		return 0;
192349011e0cSOleksij Rempel 
192449011e0cSOleksij Rempel 	return ethnl_cable_test_fault_length(phydev, ethtool_pair[pair],
192549011e0cSOleksij Rempel 					     ksz886x_cable_test_fault_length(val));
192649011e0cSOleksij Rempel }
192749011e0cSOleksij Rempel 
192849011e0cSOleksij Rempel static int ksz886x_cable_test_get_status(struct phy_device *phydev,
192949011e0cSOleksij Rempel 					 bool *finished)
193049011e0cSOleksij Rempel {
193149011e0cSOleksij Rempel 	unsigned long pair_mask = 0x3;
193249011e0cSOleksij Rempel 	int retries = 20;
193349011e0cSOleksij Rempel 	int pair, ret;
193449011e0cSOleksij Rempel 
193549011e0cSOleksij Rempel 	*finished = false;
193649011e0cSOleksij Rempel 
193749011e0cSOleksij Rempel 	/* Try harder if link partner is active */
193849011e0cSOleksij Rempel 	while (pair_mask && retries--) {
193949011e0cSOleksij Rempel 		for_each_set_bit(pair, &pair_mask, 4) {
194049011e0cSOleksij Rempel 			ret = ksz886x_cable_test_one_pair(phydev, pair);
194149011e0cSOleksij Rempel 			if (ret == -EAGAIN)
194249011e0cSOleksij Rempel 				continue;
194349011e0cSOleksij Rempel 			if (ret < 0)
194449011e0cSOleksij Rempel 				return ret;
194549011e0cSOleksij Rempel 			clear_bit(pair, &pair_mask);
194649011e0cSOleksij Rempel 		}
194749011e0cSOleksij Rempel 		/* If link partner is in autonegotiation mode it will send 2ms
194849011e0cSOleksij Rempel 		 * of FLPs with at least 6ms of silence.
194949011e0cSOleksij Rempel 		 * Add 2ms sleep to have better chances to hit this silence.
195049011e0cSOleksij Rempel 		 */
195149011e0cSOleksij Rempel 		if (pair_mask)
195249011e0cSOleksij Rempel 			msleep(2);
195349011e0cSOleksij Rempel 	}
195449011e0cSOleksij Rempel 
195549011e0cSOleksij Rempel 	*finished = true;
195649011e0cSOleksij Rempel 
195749011e0cSOleksij Rempel 	return ret;
195849011e0cSOleksij Rempel }
195949011e0cSOleksij Rempel 
19607c2dcfa2SHoratiu Vultur #define LAN_EXT_PAGE_ACCESS_CONTROL			0x16
19617c2dcfa2SHoratiu Vultur #define LAN_EXT_PAGE_ACCESS_ADDRESS_DATA		0x17
19627c2dcfa2SHoratiu Vultur #define LAN_EXT_PAGE_ACCESS_CTRL_EP_FUNC		0x4000
19637c2dcfa2SHoratiu Vultur 
19647467d716SHoratiu Vultur #define LAN8814_QSGMII_SOFT_RESET			0x43
19657467d716SHoratiu Vultur #define LAN8814_QSGMII_SOFT_RESET_BIT			BIT(0)
19667467d716SHoratiu Vultur #define LAN8814_QSGMII_PCS1G_ANEG_CONFIG		0x13
19677467d716SHoratiu Vultur #define LAN8814_QSGMII_PCS1G_ANEG_CONFIG_ANEG_ENA	BIT(3)
19687467d716SHoratiu Vultur #define LAN8814_ALIGN_SWAP				0x4a
19697467d716SHoratiu Vultur #define LAN8814_ALIGN_TX_A_B_SWAP			0x1
19707467d716SHoratiu Vultur #define LAN8814_ALIGN_TX_A_B_SWAP_MASK			GENMASK(2, 0)
19717467d716SHoratiu Vultur 
19727c2dcfa2SHoratiu Vultur #define LAN8804_ALIGN_SWAP				0x4a
19737c2dcfa2SHoratiu Vultur #define LAN8804_ALIGN_TX_A_B_SWAP			0x1
19747c2dcfa2SHoratiu Vultur #define LAN8804_ALIGN_TX_A_B_SWAP_MASK			GENMASK(2, 0)
19757c2dcfa2SHoratiu Vultur #define LAN8814_CLOCK_MANAGEMENT			0xd
19767c2dcfa2SHoratiu Vultur #define LAN8814_LINK_QUALITY				0x8e
19777c2dcfa2SHoratiu Vultur 
19787c2dcfa2SHoratiu Vultur static int lanphy_read_page_reg(struct phy_device *phydev, int page, u32 addr)
19797c2dcfa2SHoratiu Vultur {
198012a4d677SWan Jiabing 	int data;
19817c2dcfa2SHoratiu Vultur 
19824488f6b6SDivya Koppera 	phy_lock_mdio_bus(phydev);
19834488f6b6SDivya Koppera 	__phy_write(phydev, LAN_EXT_PAGE_ACCESS_CONTROL, page);
19844488f6b6SDivya Koppera 	__phy_write(phydev, LAN_EXT_PAGE_ACCESS_ADDRESS_DATA, addr);
19854488f6b6SDivya Koppera 	__phy_write(phydev, LAN_EXT_PAGE_ACCESS_CONTROL,
19867c2dcfa2SHoratiu Vultur 		    (page | LAN_EXT_PAGE_ACCESS_CTRL_EP_FUNC));
19874488f6b6SDivya Koppera 	data = __phy_read(phydev, LAN_EXT_PAGE_ACCESS_ADDRESS_DATA);
19884488f6b6SDivya Koppera 	phy_unlock_mdio_bus(phydev);
19897c2dcfa2SHoratiu Vultur 
19907c2dcfa2SHoratiu Vultur 	return data;
19917c2dcfa2SHoratiu Vultur }
19927c2dcfa2SHoratiu Vultur 
19937c2dcfa2SHoratiu Vultur static int lanphy_write_page_reg(struct phy_device *phydev, int page, u16 addr,
19947c2dcfa2SHoratiu Vultur 				 u16 val)
19957c2dcfa2SHoratiu Vultur {
19964488f6b6SDivya Koppera 	phy_lock_mdio_bus(phydev);
19974488f6b6SDivya Koppera 	__phy_write(phydev, LAN_EXT_PAGE_ACCESS_CONTROL, page);
19984488f6b6SDivya Koppera 	__phy_write(phydev, LAN_EXT_PAGE_ACCESS_ADDRESS_DATA, addr);
19994488f6b6SDivya Koppera 	__phy_write(phydev, LAN_EXT_PAGE_ACCESS_CONTROL,
20004488f6b6SDivya Koppera 		    page | LAN_EXT_PAGE_ACCESS_CTRL_EP_FUNC);
20017c2dcfa2SHoratiu Vultur 
20024488f6b6SDivya Koppera 	val = __phy_write(phydev, LAN_EXT_PAGE_ACCESS_ADDRESS_DATA, val);
20034488f6b6SDivya Koppera 	if (val != 0)
20047c2dcfa2SHoratiu Vultur 		phydev_err(phydev, "Error: phy_write has returned error %d\n",
20057c2dcfa2SHoratiu Vultur 			   val);
20064488f6b6SDivya Koppera 	phy_unlock_mdio_bus(phydev);
20077c2dcfa2SHoratiu Vultur 	return val;
20087c2dcfa2SHoratiu Vultur }
20097c2dcfa2SHoratiu Vultur 
2010ece19502SDivya Koppera static int lan8814_config_ts_intr(struct phy_device *phydev, bool enable)
20117467d716SHoratiu Vultur {
2012ece19502SDivya Koppera 	u16 val = 0;
20137467d716SHoratiu Vultur 
2014ece19502SDivya Koppera 	if (enable)
2015ece19502SDivya Koppera 		val = PTP_TSU_INT_EN_PTP_TX_TS_EN_ |
2016ece19502SDivya Koppera 		      PTP_TSU_INT_EN_PTP_TX_TS_OVRFL_EN_ |
2017ece19502SDivya Koppera 		      PTP_TSU_INT_EN_PTP_RX_TS_EN_ |
2018ece19502SDivya Koppera 		      PTP_TSU_INT_EN_PTP_RX_TS_OVRFL_EN_;
20197467d716SHoratiu Vultur 
2020ece19502SDivya Koppera 	return lanphy_write_page_reg(phydev, 5, PTP_TSU_INT_EN, val);
2021ece19502SDivya Koppera }
20227467d716SHoratiu Vultur 
2023ece19502SDivya Koppera static void lan8814_ptp_rx_ts_get(struct phy_device *phydev,
2024ece19502SDivya Koppera 				  u32 *seconds, u32 *nano_seconds, u16 *seq_id)
2025ece19502SDivya Koppera {
2026ece19502SDivya Koppera 	*seconds = lanphy_read_page_reg(phydev, 5, PTP_RX_INGRESS_SEC_HI);
2027ece19502SDivya Koppera 	*seconds = (*seconds << 16) |
2028ece19502SDivya Koppera 		   lanphy_read_page_reg(phydev, 5, PTP_RX_INGRESS_SEC_LO);
2029ece19502SDivya Koppera 
2030ece19502SDivya Koppera 	*nano_seconds = lanphy_read_page_reg(phydev, 5, PTP_RX_INGRESS_NS_HI);
2031ece19502SDivya Koppera 	*nano_seconds = ((*nano_seconds & 0x3fff) << 16) |
2032ece19502SDivya Koppera 			lanphy_read_page_reg(phydev, 5, PTP_RX_INGRESS_NS_LO);
2033ece19502SDivya Koppera 
2034ece19502SDivya Koppera 	*seq_id = lanphy_read_page_reg(phydev, 5, PTP_RX_MSG_HEADER2);
2035ece19502SDivya Koppera }
2036ece19502SDivya Koppera 
2037ece19502SDivya Koppera static void lan8814_ptp_tx_ts_get(struct phy_device *phydev,
2038ece19502SDivya Koppera 				  u32 *seconds, u32 *nano_seconds, u16 *seq_id)
2039ece19502SDivya Koppera {
2040ece19502SDivya Koppera 	*seconds = lanphy_read_page_reg(phydev, 5, PTP_TX_EGRESS_SEC_HI);
2041ece19502SDivya Koppera 	*seconds = *seconds << 16 |
2042ece19502SDivya Koppera 		   lanphy_read_page_reg(phydev, 5, PTP_TX_EGRESS_SEC_LO);
2043ece19502SDivya Koppera 
2044ece19502SDivya Koppera 	*nano_seconds = lanphy_read_page_reg(phydev, 5, PTP_TX_EGRESS_NS_HI);
2045ece19502SDivya Koppera 	*nano_seconds = ((*nano_seconds & 0x3fff) << 16) |
2046ece19502SDivya Koppera 			lanphy_read_page_reg(phydev, 5, PTP_TX_EGRESS_NS_LO);
2047ece19502SDivya Koppera 
2048ece19502SDivya Koppera 	*seq_id = lanphy_read_page_reg(phydev, 5, PTP_TX_MSG_HEADER2);
2049ece19502SDivya Koppera }
2050ece19502SDivya Koppera 
2051ece19502SDivya Koppera static int lan8814_ts_info(struct mii_timestamper *mii_ts, struct ethtool_ts_info *info)
2052ece19502SDivya Koppera {
2053ece19502SDivya Koppera 	struct kszphy_ptp_priv *ptp_priv = container_of(mii_ts, struct kszphy_ptp_priv, mii_ts);
2054ece19502SDivya Koppera 	struct phy_device *phydev = ptp_priv->phydev;
2055ece19502SDivya Koppera 	struct lan8814_shared_priv *shared = phydev->shared->priv;
2056ece19502SDivya Koppera 
2057ece19502SDivya Koppera 	info->so_timestamping = SOF_TIMESTAMPING_TX_HARDWARE |
2058ece19502SDivya Koppera 				SOF_TIMESTAMPING_RX_HARDWARE |
2059ece19502SDivya Koppera 				SOF_TIMESTAMPING_RAW_HARDWARE;
2060ece19502SDivya Koppera 
2061ece19502SDivya Koppera 	info->phc_index = ptp_clock_index(shared->ptp_clock);
2062ece19502SDivya Koppera 
2063ece19502SDivya Koppera 	info->tx_types =
2064ece19502SDivya Koppera 		(1 << HWTSTAMP_TX_OFF) |
2065ece19502SDivya Koppera 		(1 << HWTSTAMP_TX_ON) |
2066ece19502SDivya Koppera 		(1 << HWTSTAMP_TX_ONESTEP_SYNC);
2067ece19502SDivya Koppera 
2068ece19502SDivya Koppera 	info->rx_filters =
2069ece19502SDivya Koppera 		(1 << HWTSTAMP_FILTER_NONE) |
2070ece19502SDivya Koppera 		(1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT) |
2071ece19502SDivya Koppera 		(1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT) |
2072ece19502SDivya Koppera 		(1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
2073ece19502SDivya Koppera 		(1 << HWTSTAMP_FILTER_PTP_V2_EVENT);
20747467d716SHoratiu Vultur 
20757467d716SHoratiu Vultur 	return 0;
20767467d716SHoratiu Vultur }
20777467d716SHoratiu Vultur 
2078ece19502SDivya Koppera static void lan8814_flush_fifo(struct phy_device *phydev, bool egress)
2079ece19502SDivya Koppera {
2080ece19502SDivya Koppera 	int i;
2081ece19502SDivya Koppera 
2082ece19502SDivya Koppera 	for (i = 0; i < FIFO_SIZE; ++i)
2083ece19502SDivya Koppera 		lanphy_read_page_reg(phydev, 5,
2084ece19502SDivya Koppera 				     egress ? PTP_TX_MSG_HEADER2 : PTP_RX_MSG_HEADER2);
2085ece19502SDivya Koppera 
2086ece19502SDivya Koppera 	/* Read to clear overflow status bit */
2087ece19502SDivya Koppera 	lanphy_read_page_reg(phydev, 5, PTP_TSU_INT_STS);
2088ece19502SDivya Koppera }
2089ece19502SDivya Koppera 
2090ece19502SDivya Koppera static int lan8814_hwtstamp(struct mii_timestamper *mii_ts, struct ifreq *ifr)
2091ece19502SDivya Koppera {
2092ece19502SDivya Koppera 	struct kszphy_ptp_priv *ptp_priv =
2093ece19502SDivya Koppera 			  container_of(mii_ts, struct kszphy_ptp_priv, mii_ts);
2094ece19502SDivya Koppera 	struct phy_device *phydev = ptp_priv->phydev;
2095ece19502SDivya Koppera 	struct lan8814_shared_priv *shared = phydev->shared->priv;
2096ece19502SDivya Koppera 	struct lan8814_ptp_rx_ts *rx_ts, *tmp;
2097ece19502SDivya Koppera 	struct hwtstamp_config config;
2098ece19502SDivya Koppera 	int txcfg = 0, rxcfg = 0;
2099ece19502SDivya Koppera 	int pkt_ts_enable;
2100ece19502SDivya Koppera 
2101ece19502SDivya Koppera 	if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
2102ece19502SDivya Koppera 		return -EFAULT;
2103ece19502SDivya Koppera 
2104ece19502SDivya Koppera 	ptp_priv->hwts_tx_type = config.tx_type;
2105ece19502SDivya Koppera 	ptp_priv->rx_filter = config.rx_filter;
2106ece19502SDivya Koppera 
2107ece19502SDivya Koppera 	switch (config.rx_filter) {
2108ece19502SDivya Koppera 	case HWTSTAMP_FILTER_NONE:
2109ece19502SDivya Koppera 		ptp_priv->layer = 0;
2110ece19502SDivya Koppera 		ptp_priv->version = 0;
2111ece19502SDivya Koppera 		break;
2112ece19502SDivya Koppera 	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
2113ece19502SDivya Koppera 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
2114ece19502SDivya Koppera 	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
2115ece19502SDivya Koppera 		ptp_priv->layer = PTP_CLASS_L4;
2116ece19502SDivya Koppera 		ptp_priv->version = PTP_CLASS_V2;
2117ece19502SDivya Koppera 		break;
2118ece19502SDivya Koppera 	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
2119ece19502SDivya Koppera 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
2120ece19502SDivya Koppera 	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
2121ece19502SDivya Koppera 		ptp_priv->layer = PTP_CLASS_L2;
2122ece19502SDivya Koppera 		ptp_priv->version = PTP_CLASS_V2;
2123ece19502SDivya Koppera 		break;
2124ece19502SDivya Koppera 	case HWTSTAMP_FILTER_PTP_V2_EVENT:
2125ece19502SDivya Koppera 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
2126ece19502SDivya Koppera 	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
2127ece19502SDivya Koppera 		ptp_priv->layer = PTP_CLASS_L4 | PTP_CLASS_L2;
2128ece19502SDivya Koppera 		ptp_priv->version = PTP_CLASS_V2;
2129ece19502SDivya Koppera 		break;
2130ece19502SDivya Koppera 	default:
2131ece19502SDivya Koppera 		return -ERANGE;
2132ece19502SDivya Koppera 	}
2133ece19502SDivya Koppera 
2134ece19502SDivya Koppera 	if (ptp_priv->layer & PTP_CLASS_L2) {
2135ece19502SDivya Koppera 		rxcfg = PTP_RX_PARSE_CONFIG_LAYER2_EN_;
2136ece19502SDivya Koppera 		txcfg = PTP_TX_PARSE_CONFIG_LAYER2_EN_;
2137ece19502SDivya Koppera 	} else if (ptp_priv->layer & PTP_CLASS_L4) {
2138ece19502SDivya Koppera 		rxcfg |= PTP_RX_PARSE_CONFIG_IPV4_EN_ | PTP_RX_PARSE_CONFIG_IPV6_EN_;
2139ece19502SDivya Koppera 		txcfg |= PTP_TX_PARSE_CONFIG_IPV4_EN_ | PTP_TX_PARSE_CONFIG_IPV6_EN_;
2140ece19502SDivya Koppera 	}
2141ece19502SDivya Koppera 	lanphy_write_page_reg(ptp_priv->phydev, 5, PTP_RX_PARSE_CONFIG, rxcfg);
2142ece19502SDivya Koppera 	lanphy_write_page_reg(ptp_priv->phydev, 5, PTP_TX_PARSE_CONFIG, txcfg);
2143ece19502SDivya Koppera 
2144ece19502SDivya Koppera 	pkt_ts_enable = PTP_TIMESTAMP_EN_SYNC_ | PTP_TIMESTAMP_EN_DREQ_ |
2145ece19502SDivya Koppera 			PTP_TIMESTAMP_EN_PDREQ_ | PTP_TIMESTAMP_EN_PDRES_;
2146ece19502SDivya Koppera 	lanphy_write_page_reg(ptp_priv->phydev, 5, PTP_RX_TIMESTAMP_EN, pkt_ts_enable);
2147ece19502SDivya Koppera 	lanphy_write_page_reg(ptp_priv->phydev, 5, PTP_TX_TIMESTAMP_EN, pkt_ts_enable);
2148ece19502SDivya Koppera 
2149ece19502SDivya Koppera 	if (ptp_priv->hwts_tx_type == HWTSTAMP_TX_ONESTEP_SYNC)
2150ece19502SDivya Koppera 		lanphy_write_page_reg(ptp_priv->phydev, 5, PTP_TX_MOD,
2151ece19502SDivya Koppera 				      PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_);
2152ece19502SDivya Koppera 
2153ece19502SDivya Koppera 	if (config.rx_filter != HWTSTAMP_FILTER_NONE)
2154ece19502SDivya Koppera 		lan8814_config_ts_intr(ptp_priv->phydev, true);
2155ece19502SDivya Koppera 	else
2156ece19502SDivya Koppera 		lan8814_config_ts_intr(ptp_priv->phydev, false);
2157ece19502SDivya Koppera 
2158ece19502SDivya Koppera 	mutex_lock(&shared->shared_lock);
2159ece19502SDivya Koppera 	if (config.rx_filter != HWTSTAMP_FILTER_NONE)
2160ece19502SDivya Koppera 		shared->ref++;
2161ece19502SDivya Koppera 	else
2162ece19502SDivya Koppera 		shared->ref--;
2163ece19502SDivya Koppera 
2164ece19502SDivya Koppera 	if (shared->ref)
2165ece19502SDivya Koppera 		lanphy_write_page_reg(ptp_priv->phydev, 4, PTP_CMD_CTL,
2166ece19502SDivya Koppera 				      PTP_CMD_CTL_PTP_ENABLE_);
2167ece19502SDivya Koppera 	else
2168ece19502SDivya Koppera 		lanphy_write_page_reg(ptp_priv->phydev, 4, PTP_CMD_CTL,
2169ece19502SDivya Koppera 				      PTP_CMD_CTL_PTP_DISABLE_);
2170ece19502SDivya Koppera 	mutex_unlock(&shared->shared_lock);
2171ece19502SDivya Koppera 
2172ece19502SDivya Koppera 	/* In case of multiple starts and stops, these needs to be cleared */
2173ece19502SDivya Koppera 	list_for_each_entry_safe(rx_ts, tmp, &ptp_priv->rx_ts_list, list) {
2174ece19502SDivya Koppera 		list_del(&rx_ts->list);
2175ece19502SDivya Koppera 		kfree(rx_ts);
2176ece19502SDivya Koppera 	}
2177ece19502SDivya Koppera 	skb_queue_purge(&ptp_priv->rx_queue);
2178ece19502SDivya Koppera 	skb_queue_purge(&ptp_priv->tx_queue);
2179ece19502SDivya Koppera 
2180ece19502SDivya Koppera 	lan8814_flush_fifo(ptp_priv->phydev, false);
2181ece19502SDivya Koppera 	lan8814_flush_fifo(ptp_priv->phydev, true);
2182ece19502SDivya Koppera 
2183ece19502SDivya Koppera 	return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ? -EFAULT : 0;
2184ece19502SDivya Koppera }
2185ece19502SDivya Koppera 
2186ece19502SDivya Koppera static void lan8814_txtstamp(struct mii_timestamper *mii_ts,
2187ece19502SDivya Koppera 			     struct sk_buff *skb, int type)
2188ece19502SDivya Koppera {
2189ece19502SDivya Koppera 	struct kszphy_ptp_priv *ptp_priv = container_of(mii_ts, struct kszphy_ptp_priv, mii_ts);
2190ece19502SDivya Koppera 
2191ece19502SDivya Koppera 	switch (ptp_priv->hwts_tx_type) {
2192ece19502SDivya Koppera 	case HWTSTAMP_TX_ONESTEP_SYNC:
21933914a9c0SKurt Kanzenbach 		if (ptp_msg_is_sync(skb, type)) {
2194ece19502SDivya Koppera 			kfree_skb(skb);
2195ece19502SDivya Koppera 			return;
2196ece19502SDivya Koppera 		}
2197ece19502SDivya Koppera 		fallthrough;
2198ece19502SDivya Koppera 	case HWTSTAMP_TX_ON:
2199ece19502SDivya Koppera 		skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
2200ece19502SDivya Koppera 		skb_queue_tail(&ptp_priv->tx_queue, skb);
2201ece19502SDivya Koppera 		break;
2202ece19502SDivya Koppera 	case HWTSTAMP_TX_OFF:
2203ece19502SDivya Koppera 	default:
2204ece19502SDivya Koppera 		kfree_skb(skb);
2205ece19502SDivya Koppera 		break;
2206ece19502SDivya Koppera 	}
2207ece19502SDivya Koppera }
2208ece19502SDivya Koppera 
2209ece19502SDivya Koppera static void lan8814_get_sig_rx(struct sk_buff *skb, u16 *sig)
2210ece19502SDivya Koppera {
2211ece19502SDivya Koppera 	struct ptp_header *ptp_header;
2212ece19502SDivya Koppera 	u32 type;
2213ece19502SDivya Koppera 
2214ece19502SDivya Koppera 	skb_push(skb, ETH_HLEN);
2215ece19502SDivya Koppera 	type = ptp_classify_raw(skb);
2216ece19502SDivya Koppera 	ptp_header = ptp_parse_header(skb, type);
2217ece19502SDivya Koppera 	skb_pull_inline(skb, ETH_HLEN);
2218ece19502SDivya Koppera 
2219ece19502SDivya Koppera 	*sig = (__force u16)(ntohs(ptp_header->sequence_id));
2220ece19502SDivya Koppera }
2221ece19502SDivya Koppera 
2222ece19502SDivya Koppera static bool lan8814_match_rx_ts(struct kszphy_ptp_priv *ptp_priv,
2223ece19502SDivya Koppera 				struct sk_buff *skb)
2224ece19502SDivya Koppera {
2225ece19502SDivya Koppera 	struct skb_shared_hwtstamps *shhwtstamps;
2226ece19502SDivya Koppera 	struct lan8814_ptp_rx_ts *rx_ts, *tmp;
2227ece19502SDivya Koppera 	unsigned long flags;
2228ece19502SDivya Koppera 	bool ret = false;
2229ece19502SDivya Koppera 	u16 skb_sig;
2230ece19502SDivya Koppera 
2231ece19502SDivya Koppera 	lan8814_get_sig_rx(skb, &skb_sig);
2232ece19502SDivya Koppera 
2233ece19502SDivya Koppera 	/* Iterate over all RX timestamps and match it with the received skbs */
2234ece19502SDivya Koppera 	spin_lock_irqsave(&ptp_priv->rx_ts_lock, flags);
2235ece19502SDivya Koppera 	list_for_each_entry_safe(rx_ts, tmp, &ptp_priv->rx_ts_list, list) {
2236ece19502SDivya Koppera 		/* Check if we found the signature we were looking for. */
2237ece19502SDivya Koppera 		if (memcmp(&skb_sig, &rx_ts->seq_id, sizeof(rx_ts->seq_id)))
2238ece19502SDivya Koppera 			continue;
2239ece19502SDivya Koppera 
2240ece19502SDivya Koppera 		shhwtstamps = skb_hwtstamps(skb);
2241ece19502SDivya Koppera 		memset(shhwtstamps, 0, sizeof(*shhwtstamps));
2242ece19502SDivya Koppera 		shhwtstamps->hwtstamp = ktime_set(rx_ts->seconds,
2243ece19502SDivya Koppera 						  rx_ts->nsec);
2244ece19502SDivya Koppera 		list_del(&rx_ts->list);
2245ece19502SDivya Koppera 		kfree(rx_ts);
2246ece19502SDivya Koppera 
2247ece19502SDivya Koppera 		ret = true;
2248ece19502SDivya Koppera 		break;
2249ece19502SDivya Koppera 	}
2250ece19502SDivya Koppera 	spin_unlock_irqrestore(&ptp_priv->rx_ts_lock, flags);
2251ece19502SDivya Koppera 
225267dbd6c0SSebastian Andrzej Siewior 	if (ret)
225367dbd6c0SSebastian Andrzej Siewior 		netif_rx(skb);
2254ece19502SDivya Koppera 	return ret;
2255ece19502SDivya Koppera }
2256ece19502SDivya Koppera 
2257ece19502SDivya Koppera static bool lan8814_rxtstamp(struct mii_timestamper *mii_ts, struct sk_buff *skb, int type)
2258ece19502SDivya Koppera {
2259ece19502SDivya Koppera 	struct kszphy_ptp_priv *ptp_priv =
2260ece19502SDivya Koppera 			container_of(mii_ts, struct kszphy_ptp_priv, mii_ts);
2261ece19502SDivya Koppera 
2262ece19502SDivya Koppera 	if (ptp_priv->rx_filter == HWTSTAMP_FILTER_NONE ||
2263ece19502SDivya Koppera 	    type == PTP_CLASS_NONE)
2264ece19502SDivya Koppera 		return false;
2265ece19502SDivya Koppera 
2266ece19502SDivya Koppera 	if ((type & ptp_priv->version) == 0 || (type & ptp_priv->layer) == 0)
2267ece19502SDivya Koppera 		return false;
2268ece19502SDivya Koppera 
2269ece19502SDivya Koppera 	/* If we failed to match then add it to the queue for when the timestamp
2270ece19502SDivya Koppera 	 * will come
2271ece19502SDivya Koppera 	 */
2272ece19502SDivya Koppera 	if (!lan8814_match_rx_ts(ptp_priv, skb))
2273ece19502SDivya Koppera 		skb_queue_tail(&ptp_priv->rx_queue, skb);
2274ece19502SDivya Koppera 
2275ece19502SDivya Koppera 	return true;
2276ece19502SDivya Koppera }
2277ece19502SDivya Koppera 
2278ece19502SDivya Koppera static void lan8814_ptp_clock_set(struct phy_device *phydev,
2279ece19502SDivya Koppera 				  u32 seconds, u32 nano_seconds)
2280ece19502SDivya Koppera {
2281ece19502SDivya Koppera 	u32 sec_low, sec_high, nsec_low, nsec_high;
2282ece19502SDivya Koppera 
2283ece19502SDivya Koppera 	sec_low = seconds & 0xffff;
2284ece19502SDivya Koppera 	sec_high = (seconds >> 16) & 0xffff;
2285ece19502SDivya Koppera 	nsec_low = nano_seconds & 0xffff;
2286ece19502SDivya Koppera 	nsec_high = (nano_seconds >> 16) & 0x3fff;
2287ece19502SDivya Koppera 
2288ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 4, PTP_CLOCK_SET_SEC_LO, sec_low);
2289ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 4, PTP_CLOCK_SET_SEC_MID, sec_high);
2290ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 4, PTP_CLOCK_SET_NS_LO, nsec_low);
2291ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 4, PTP_CLOCK_SET_NS_HI, nsec_high);
2292ece19502SDivya Koppera 
2293ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 4, PTP_CMD_CTL, PTP_CMD_CTL_PTP_CLOCK_LOAD_);
2294ece19502SDivya Koppera }
2295ece19502SDivya Koppera 
2296ece19502SDivya Koppera static void lan8814_ptp_clock_get(struct phy_device *phydev,
2297ece19502SDivya Koppera 				  u32 *seconds, u32 *nano_seconds)
2298ece19502SDivya Koppera {
2299ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 4, PTP_CMD_CTL, PTP_CMD_CTL_PTP_CLOCK_READ_);
2300ece19502SDivya Koppera 
2301ece19502SDivya Koppera 	*seconds = lanphy_read_page_reg(phydev, 4, PTP_CLOCK_READ_SEC_MID);
2302ece19502SDivya Koppera 	*seconds = (*seconds << 16) |
2303ece19502SDivya Koppera 		   lanphy_read_page_reg(phydev, 4, PTP_CLOCK_READ_SEC_LO);
2304ece19502SDivya Koppera 
2305ece19502SDivya Koppera 	*nano_seconds = lanphy_read_page_reg(phydev, 4, PTP_CLOCK_READ_NS_HI);
2306ece19502SDivya Koppera 	*nano_seconds = ((*nano_seconds & 0x3fff) << 16) |
2307ece19502SDivya Koppera 			lanphy_read_page_reg(phydev, 4, PTP_CLOCK_READ_NS_LO);
2308ece19502SDivya Koppera }
2309ece19502SDivya Koppera 
2310ece19502SDivya Koppera static int lan8814_ptpci_gettime64(struct ptp_clock_info *ptpci,
2311ece19502SDivya Koppera 				   struct timespec64 *ts)
2312ece19502SDivya Koppera {
2313ece19502SDivya Koppera 	struct lan8814_shared_priv *shared = container_of(ptpci, struct lan8814_shared_priv,
2314ece19502SDivya Koppera 							  ptp_clock_info);
2315ece19502SDivya Koppera 	struct phy_device *phydev = shared->phydev;
2316ece19502SDivya Koppera 	u32 nano_seconds;
2317ece19502SDivya Koppera 	u32 seconds;
2318ece19502SDivya Koppera 
2319ece19502SDivya Koppera 	mutex_lock(&shared->shared_lock);
2320ece19502SDivya Koppera 	lan8814_ptp_clock_get(phydev, &seconds, &nano_seconds);
2321ece19502SDivya Koppera 	mutex_unlock(&shared->shared_lock);
2322ece19502SDivya Koppera 	ts->tv_sec = seconds;
2323ece19502SDivya Koppera 	ts->tv_nsec = nano_seconds;
2324ece19502SDivya Koppera 
2325ece19502SDivya Koppera 	return 0;
2326ece19502SDivya Koppera }
2327ece19502SDivya Koppera 
2328ece19502SDivya Koppera static int lan8814_ptpci_settime64(struct ptp_clock_info *ptpci,
2329ece19502SDivya Koppera 				   const struct timespec64 *ts)
2330ece19502SDivya Koppera {
2331ece19502SDivya Koppera 	struct lan8814_shared_priv *shared = container_of(ptpci, struct lan8814_shared_priv,
2332ece19502SDivya Koppera 							  ptp_clock_info);
2333ece19502SDivya Koppera 	struct phy_device *phydev = shared->phydev;
2334ece19502SDivya Koppera 
2335ece19502SDivya Koppera 	mutex_lock(&shared->shared_lock);
2336ece19502SDivya Koppera 	lan8814_ptp_clock_set(phydev, ts->tv_sec, ts->tv_nsec);
2337ece19502SDivya Koppera 	mutex_unlock(&shared->shared_lock);
2338ece19502SDivya Koppera 
2339ece19502SDivya Koppera 	return 0;
2340ece19502SDivya Koppera }
2341ece19502SDivya Koppera 
2342ece19502SDivya Koppera static void lan8814_ptp_clock_step(struct phy_device *phydev,
2343ece19502SDivya Koppera 				   s64 time_step_ns)
2344ece19502SDivya Koppera {
2345ece19502SDivya Koppera 	u32 nano_seconds_step;
2346ece19502SDivya Koppera 	u64 abs_time_step_ns;
2347ece19502SDivya Koppera 	u32 unsigned_seconds;
2348ece19502SDivya Koppera 	u32 nano_seconds;
2349ece19502SDivya Koppera 	u32 remainder;
2350ece19502SDivya Koppera 	s32 seconds;
2351ece19502SDivya Koppera 
2352ece19502SDivya Koppera 	if (time_step_ns >  15000000000LL) {
2353ece19502SDivya Koppera 		/* convert to clock set */
2354ece19502SDivya Koppera 		lan8814_ptp_clock_get(phydev, &unsigned_seconds, &nano_seconds);
2355ece19502SDivya Koppera 		unsigned_seconds += div_u64_rem(time_step_ns, 1000000000LL,
2356ece19502SDivya Koppera 						&remainder);
2357ece19502SDivya Koppera 		nano_seconds += remainder;
2358ece19502SDivya Koppera 		if (nano_seconds >= 1000000000) {
2359ece19502SDivya Koppera 			unsigned_seconds++;
2360ece19502SDivya Koppera 			nano_seconds -= 1000000000;
2361ece19502SDivya Koppera 		}
2362ece19502SDivya Koppera 		lan8814_ptp_clock_set(phydev, unsigned_seconds, nano_seconds);
2363ece19502SDivya Koppera 		return;
2364ece19502SDivya Koppera 	} else if (time_step_ns < -15000000000LL) {
2365ece19502SDivya Koppera 		/* convert to clock set */
2366ece19502SDivya Koppera 		time_step_ns = -time_step_ns;
2367ece19502SDivya Koppera 
2368ece19502SDivya Koppera 		lan8814_ptp_clock_get(phydev, &unsigned_seconds, &nano_seconds);
2369ece19502SDivya Koppera 		unsigned_seconds -= div_u64_rem(time_step_ns, 1000000000LL,
2370ece19502SDivya Koppera 						&remainder);
2371ece19502SDivya Koppera 		nano_seconds_step = remainder;
2372ece19502SDivya Koppera 		if (nano_seconds < nano_seconds_step) {
2373ece19502SDivya Koppera 			unsigned_seconds--;
2374ece19502SDivya Koppera 			nano_seconds += 1000000000;
2375ece19502SDivya Koppera 		}
2376ece19502SDivya Koppera 		nano_seconds -= nano_seconds_step;
2377ece19502SDivya Koppera 		lan8814_ptp_clock_set(phydev, unsigned_seconds,
2378ece19502SDivya Koppera 				      nano_seconds);
2379ece19502SDivya Koppera 		return;
2380ece19502SDivya Koppera 	}
2381ece19502SDivya Koppera 
2382ece19502SDivya Koppera 	/* do clock step */
2383ece19502SDivya Koppera 	if (time_step_ns >= 0) {
2384ece19502SDivya Koppera 		abs_time_step_ns = (u64)time_step_ns;
2385ece19502SDivya Koppera 		seconds = (s32)div_u64_rem(abs_time_step_ns, 1000000000,
2386ece19502SDivya Koppera 					   &remainder);
2387ece19502SDivya Koppera 		nano_seconds = remainder;
2388ece19502SDivya Koppera 	} else {
2389ece19502SDivya Koppera 		abs_time_step_ns = (u64)(-time_step_ns);
2390ece19502SDivya Koppera 		seconds = -((s32)div_u64_rem(abs_time_step_ns, 1000000000,
2391ece19502SDivya Koppera 			    &remainder));
2392ece19502SDivya Koppera 		nano_seconds = remainder;
2393ece19502SDivya Koppera 		if (nano_seconds > 0) {
2394ece19502SDivya Koppera 			/* subtracting nano seconds is not allowed
2395ece19502SDivya Koppera 			 * convert to subtracting from seconds,
2396ece19502SDivya Koppera 			 * and adding to nanoseconds
2397ece19502SDivya Koppera 			 */
2398ece19502SDivya Koppera 			seconds--;
2399ece19502SDivya Koppera 			nano_seconds = (1000000000 - nano_seconds);
2400ece19502SDivya Koppera 		}
2401ece19502SDivya Koppera 	}
2402ece19502SDivya Koppera 
2403ece19502SDivya Koppera 	if (nano_seconds > 0) {
2404ece19502SDivya Koppera 		/* add 8 ns to cover the likely normal increment */
2405ece19502SDivya Koppera 		nano_seconds += 8;
2406ece19502SDivya Koppera 	}
2407ece19502SDivya Koppera 
2408ece19502SDivya Koppera 	if (nano_seconds >= 1000000000) {
2409ece19502SDivya Koppera 		/* carry into seconds */
2410ece19502SDivya Koppera 		seconds++;
2411ece19502SDivya Koppera 		nano_seconds -= 1000000000;
2412ece19502SDivya Koppera 	}
2413ece19502SDivya Koppera 
2414ece19502SDivya Koppera 	while (seconds) {
2415ece19502SDivya Koppera 		if (seconds > 0) {
2416ece19502SDivya Koppera 			u32 adjustment_value = (u32)seconds;
2417ece19502SDivya Koppera 			u16 adjustment_value_lo, adjustment_value_hi;
2418ece19502SDivya Koppera 
2419ece19502SDivya Koppera 			if (adjustment_value > 0xF)
2420ece19502SDivya Koppera 				adjustment_value = 0xF;
2421ece19502SDivya Koppera 
2422ece19502SDivya Koppera 			adjustment_value_lo = adjustment_value & 0xffff;
2423ece19502SDivya Koppera 			adjustment_value_hi = (adjustment_value >> 16) & 0x3fff;
2424ece19502SDivya Koppera 
2425ece19502SDivya Koppera 			lanphy_write_page_reg(phydev, 4, PTP_LTC_STEP_ADJ_LO,
2426ece19502SDivya Koppera 					      adjustment_value_lo);
2427ece19502SDivya Koppera 			lanphy_write_page_reg(phydev, 4, PTP_LTC_STEP_ADJ_HI,
2428ece19502SDivya Koppera 					      PTP_LTC_STEP_ADJ_DIR_ |
2429ece19502SDivya Koppera 					      adjustment_value_hi);
2430ece19502SDivya Koppera 			seconds -= ((s32)adjustment_value);
2431ece19502SDivya Koppera 		} else {
2432ece19502SDivya Koppera 			u32 adjustment_value = (u32)(-seconds);
2433ece19502SDivya Koppera 			u16 adjustment_value_lo, adjustment_value_hi;
2434ece19502SDivya Koppera 
2435ece19502SDivya Koppera 			if (adjustment_value > 0xF)
2436ece19502SDivya Koppera 				adjustment_value = 0xF;
2437ece19502SDivya Koppera 
2438ece19502SDivya Koppera 			adjustment_value_lo = adjustment_value & 0xffff;
2439ece19502SDivya Koppera 			adjustment_value_hi = (adjustment_value >> 16) & 0x3fff;
2440ece19502SDivya Koppera 
2441ece19502SDivya Koppera 			lanphy_write_page_reg(phydev, 4, PTP_LTC_STEP_ADJ_LO,
2442ece19502SDivya Koppera 					      adjustment_value_lo);
2443ece19502SDivya Koppera 			lanphy_write_page_reg(phydev, 4, PTP_LTC_STEP_ADJ_HI,
2444ece19502SDivya Koppera 					      adjustment_value_hi);
2445ece19502SDivya Koppera 			seconds += ((s32)adjustment_value);
2446ece19502SDivya Koppera 		}
2447ece19502SDivya Koppera 		lanphy_write_page_reg(phydev, 4, PTP_CMD_CTL,
2448ece19502SDivya Koppera 				      PTP_CMD_CTL_PTP_LTC_STEP_SEC_);
2449ece19502SDivya Koppera 	}
2450ece19502SDivya Koppera 	if (nano_seconds) {
2451ece19502SDivya Koppera 		u16 nano_seconds_lo;
2452ece19502SDivya Koppera 		u16 nano_seconds_hi;
2453ece19502SDivya Koppera 
2454ece19502SDivya Koppera 		nano_seconds_lo = nano_seconds & 0xffff;
2455ece19502SDivya Koppera 		nano_seconds_hi = (nano_seconds >> 16) & 0x3fff;
2456ece19502SDivya Koppera 
2457ece19502SDivya Koppera 		lanphy_write_page_reg(phydev, 4, PTP_LTC_STEP_ADJ_LO,
2458ece19502SDivya Koppera 				      nano_seconds_lo);
2459ece19502SDivya Koppera 		lanphy_write_page_reg(phydev, 4, PTP_LTC_STEP_ADJ_HI,
2460ece19502SDivya Koppera 				      PTP_LTC_STEP_ADJ_DIR_ |
2461ece19502SDivya Koppera 				      nano_seconds_hi);
2462ece19502SDivya Koppera 		lanphy_write_page_reg(phydev, 4, PTP_CMD_CTL,
2463ece19502SDivya Koppera 				      PTP_CMD_CTL_PTP_LTC_STEP_NSEC_);
2464ece19502SDivya Koppera 	}
2465ece19502SDivya Koppera }
2466ece19502SDivya Koppera 
2467ece19502SDivya Koppera static int lan8814_ptpci_adjtime(struct ptp_clock_info *ptpci, s64 delta)
2468ece19502SDivya Koppera {
2469ece19502SDivya Koppera 	struct lan8814_shared_priv *shared = container_of(ptpci, struct lan8814_shared_priv,
2470ece19502SDivya Koppera 							  ptp_clock_info);
2471ece19502SDivya Koppera 	struct phy_device *phydev = shared->phydev;
2472ece19502SDivya Koppera 
2473ece19502SDivya Koppera 	mutex_lock(&shared->shared_lock);
2474ece19502SDivya Koppera 	lan8814_ptp_clock_step(phydev, delta);
2475ece19502SDivya Koppera 	mutex_unlock(&shared->shared_lock);
2476ece19502SDivya Koppera 
2477ece19502SDivya Koppera 	return 0;
2478ece19502SDivya Koppera }
2479ece19502SDivya Koppera 
2480ece19502SDivya Koppera static int lan8814_ptpci_adjfine(struct ptp_clock_info *ptpci, long scaled_ppm)
2481ece19502SDivya Koppera {
2482ece19502SDivya Koppera 	struct lan8814_shared_priv *shared = container_of(ptpci, struct lan8814_shared_priv,
2483ece19502SDivya Koppera 							  ptp_clock_info);
2484ece19502SDivya Koppera 	struct phy_device *phydev = shared->phydev;
2485ece19502SDivya Koppera 	u16 kszphy_rate_adj_lo, kszphy_rate_adj_hi;
2486ece19502SDivya Koppera 	bool positive = true;
2487ece19502SDivya Koppera 	u32 kszphy_rate_adj;
2488ece19502SDivya Koppera 
2489ece19502SDivya Koppera 	if (scaled_ppm < 0) {
2490ece19502SDivya Koppera 		scaled_ppm = -scaled_ppm;
2491ece19502SDivya Koppera 		positive = false;
2492ece19502SDivya Koppera 	}
2493ece19502SDivya Koppera 
2494ece19502SDivya Koppera 	kszphy_rate_adj = LAN8814_1PPM_FORMAT * (scaled_ppm >> 16);
2495ece19502SDivya Koppera 	kszphy_rate_adj += (LAN8814_1PPM_FORMAT * (0xffff & scaled_ppm)) >> 16;
2496ece19502SDivya Koppera 
2497ece19502SDivya Koppera 	kszphy_rate_adj_lo = kszphy_rate_adj & 0xffff;
2498ece19502SDivya Koppera 	kszphy_rate_adj_hi = (kszphy_rate_adj >> 16) & 0x3fff;
2499ece19502SDivya Koppera 
2500ece19502SDivya Koppera 	if (positive)
2501ece19502SDivya Koppera 		kszphy_rate_adj_hi |= PTP_CLOCK_RATE_ADJ_DIR_;
2502ece19502SDivya Koppera 
2503ece19502SDivya Koppera 	mutex_lock(&shared->shared_lock);
2504ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 4, PTP_CLOCK_RATE_ADJ_HI, kszphy_rate_adj_hi);
2505ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 4, PTP_CLOCK_RATE_ADJ_LO, kszphy_rate_adj_lo);
2506ece19502SDivya Koppera 	mutex_unlock(&shared->shared_lock);
2507ece19502SDivya Koppera 
2508ece19502SDivya Koppera 	return 0;
2509ece19502SDivya Koppera }
2510ece19502SDivya Koppera 
2511ece19502SDivya Koppera static void lan8814_get_sig_tx(struct sk_buff *skb, u16 *sig)
2512ece19502SDivya Koppera {
2513ece19502SDivya Koppera 	struct ptp_header *ptp_header;
2514ece19502SDivya Koppera 	u32 type;
2515ece19502SDivya Koppera 
2516ece19502SDivya Koppera 	type = ptp_classify_raw(skb);
2517ece19502SDivya Koppera 	ptp_header = ptp_parse_header(skb, type);
2518ece19502SDivya Koppera 
2519ece19502SDivya Koppera 	*sig = (__force u16)(ntohs(ptp_header->sequence_id));
2520ece19502SDivya Koppera }
2521ece19502SDivya Koppera 
2522ece19502SDivya Koppera static void lan8814_dequeue_tx_skb(struct kszphy_ptp_priv *ptp_priv)
2523ece19502SDivya Koppera {
2524ece19502SDivya Koppera 	struct phy_device *phydev = ptp_priv->phydev;
2525ece19502SDivya Koppera 	struct skb_shared_hwtstamps shhwtstamps;
2526ece19502SDivya Koppera 	struct sk_buff *skb, *skb_tmp;
2527ece19502SDivya Koppera 	unsigned long flags;
2528ece19502SDivya Koppera 	u32 seconds, nsec;
2529ece19502SDivya Koppera 	bool ret = false;
2530ece19502SDivya Koppera 	u16 skb_sig;
2531ece19502SDivya Koppera 	u16 seq_id;
2532ece19502SDivya Koppera 
2533ece19502SDivya Koppera 	lan8814_ptp_tx_ts_get(phydev, &seconds, &nsec, &seq_id);
2534ece19502SDivya Koppera 
2535ece19502SDivya Koppera 	spin_lock_irqsave(&ptp_priv->tx_queue.lock, flags);
2536ece19502SDivya Koppera 	skb_queue_walk_safe(&ptp_priv->tx_queue, skb, skb_tmp) {
2537ece19502SDivya Koppera 		lan8814_get_sig_tx(skb, &skb_sig);
2538ece19502SDivya Koppera 
2539ece19502SDivya Koppera 		if (memcmp(&skb_sig, &seq_id, sizeof(seq_id)))
2540ece19502SDivya Koppera 			continue;
2541ece19502SDivya Koppera 
2542ece19502SDivya Koppera 		__skb_unlink(skb, &ptp_priv->tx_queue);
2543ece19502SDivya Koppera 		ret = true;
2544ece19502SDivya Koppera 		break;
2545ece19502SDivya Koppera 	}
2546ece19502SDivya Koppera 	spin_unlock_irqrestore(&ptp_priv->tx_queue.lock, flags);
2547ece19502SDivya Koppera 
2548ece19502SDivya Koppera 	if (ret) {
2549ece19502SDivya Koppera 		memset(&shhwtstamps, 0, sizeof(shhwtstamps));
2550ece19502SDivya Koppera 		shhwtstamps.hwtstamp = ktime_set(seconds, nsec);
2551ece19502SDivya Koppera 		skb_complete_tx_timestamp(skb, &shhwtstamps);
2552ece19502SDivya Koppera 	}
2553ece19502SDivya Koppera }
2554ece19502SDivya Koppera 
2555ece19502SDivya Koppera static void lan8814_get_tx_ts(struct kszphy_ptp_priv *ptp_priv)
2556ece19502SDivya Koppera {
2557ece19502SDivya Koppera 	struct phy_device *phydev = ptp_priv->phydev;
2558ece19502SDivya Koppera 	u32 reg;
2559ece19502SDivya Koppera 
2560ece19502SDivya Koppera 	do {
2561ece19502SDivya Koppera 		lan8814_dequeue_tx_skb(ptp_priv);
2562ece19502SDivya Koppera 
2563ece19502SDivya Koppera 		/* If other timestamps are available in the FIFO,
2564ece19502SDivya Koppera 		 * process them.
2565ece19502SDivya Koppera 		 */
2566ece19502SDivya Koppera 		reg = lanphy_read_page_reg(phydev, 5, PTP_CAP_INFO);
2567ece19502SDivya Koppera 	} while (PTP_CAP_INFO_TX_TS_CNT_GET_(reg) > 0);
2568ece19502SDivya Koppera }
2569ece19502SDivya Koppera 
2570ece19502SDivya Koppera static bool lan8814_match_skb(struct kszphy_ptp_priv *ptp_priv,
2571ece19502SDivya Koppera 			      struct lan8814_ptp_rx_ts *rx_ts)
2572ece19502SDivya Koppera {
2573ece19502SDivya Koppera 	struct skb_shared_hwtstamps *shhwtstamps;
2574ece19502SDivya Koppera 	struct sk_buff *skb, *skb_tmp;
2575ece19502SDivya Koppera 	unsigned long flags;
2576ece19502SDivya Koppera 	bool ret = false;
2577ece19502SDivya Koppera 	u16 skb_sig;
2578ece19502SDivya Koppera 
2579ece19502SDivya Koppera 	spin_lock_irqsave(&ptp_priv->rx_queue.lock, flags);
2580ece19502SDivya Koppera 	skb_queue_walk_safe(&ptp_priv->rx_queue, skb, skb_tmp) {
2581ece19502SDivya Koppera 		lan8814_get_sig_rx(skb, &skb_sig);
2582ece19502SDivya Koppera 
2583ece19502SDivya Koppera 		if (memcmp(&skb_sig, &rx_ts->seq_id, sizeof(rx_ts->seq_id)))
2584ece19502SDivya Koppera 			continue;
2585ece19502SDivya Koppera 
2586ece19502SDivya Koppera 		__skb_unlink(skb, &ptp_priv->rx_queue);
2587ece19502SDivya Koppera 
2588ece19502SDivya Koppera 		ret = true;
2589ece19502SDivya Koppera 		break;
2590ece19502SDivya Koppera 	}
2591ece19502SDivya Koppera 	spin_unlock_irqrestore(&ptp_priv->rx_queue.lock, flags);
2592ece19502SDivya Koppera 
2593ece19502SDivya Koppera 	if (ret) {
2594ece19502SDivya Koppera 		shhwtstamps = skb_hwtstamps(skb);
2595ece19502SDivya Koppera 		memset(shhwtstamps, 0, sizeof(*shhwtstamps));
2596ece19502SDivya Koppera 		shhwtstamps->hwtstamp = ktime_set(rx_ts->seconds, rx_ts->nsec);
2597e1f9e434SSebastian Andrzej Siewior 		netif_rx(skb);
2598ece19502SDivya Koppera 	}
2599ece19502SDivya Koppera 
2600ece19502SDivya Koppera 	return ret;
2601ece19502SDivya Koppera }
2602ece19502SDivya Koppera 
2603ece19502SDivya Koppera static void lan8814_get_rx_ts(struct kszphy_ptp_priv *ptp_priv)
2604ece19502SDivya Koppera {
2605ece19502SDivya Koppera 	struct phy_device *phydev = ptp_priv->phydev;
2606ece19502SDivya Koppera 	struct lan8814_ptp_rx_ts *rx_ts;
2607ece19502SDivya Koppera 	unsigned long flags;
2608ece19502SDivya Koppera 	u32 reg;
2609ece19502SDivya Koppera 
2610ece19502SDivya Koppera 	do {
2611ece19502SDivya Koppera 		rx_ts = kzalloc(sizeof(*rx_ts), GFP_KERNEL);
2612ece19502SDivya Koppera 		if (!rx_ts)
2613ece19502SDivya Koppera 			return;
2614ece19502SDivya Koppera 
2615ece19502SDivya Koppera 		lan8814_ptp_rx_ts_get(phydev, &rx_ts->seconds, &rx_ts->nsec,
2616ece19502SDivya Koppera 				      &rx_ts->seq_id);
2617ece19502SDivya Koppera 
2618ece19502SDivya Koppera 		/* If we failed to match the skb add it to the queue for when
2619ece19502SDivya Koppera 		 * the frame will come
2620ece19502SDivya Koppera 		 */
2621ece19502SDivya Koppera 		if (!lan8814_match_skb(ptp_priv, rx_ts)) {
2622ece19502SDivya Koppera 			spin_lock_irqsave(&ptp_priv->rx_ts_lock, flags);
2623ece19502SDivya Koppera 			list_add(&rx_ts->list, &ptp_priv->rx_ts_list);
2624ece19502SDivya Koppera 			spin_unlock_irqrestore(&ptp_priv->rx_ts_lock, flags);
2625ece19502SDivya Koppera 		} else {
2626ece19502SDivya Koppera 			kfree(rx_ts);
2627ece19502SDivya Koppera 		}
2628ece19502SDivya Koppera 
2629ece19502SDivya Koppera 		/* If other timestamps are available in the FIFO,
2630ece19502SDivya Koppera 		 * process them.
2631ece19502SDivya Koppera 		 */
2632ece19502SDivya Koppera 		reg = lanphy_read_page_reg(phydev, 5, PTP_CAP_INFO);
2633ece19502SDivya Koppera 	} while (PTP_CAP_INFO_RX_TS_CNT_GET_(reg) > 0);
2634ece19502SDivya Koppera }
2635ece19502SDivya Koppera 
2636ece19502SDivya Koppera static void lan8814_handle_ptp_interrupt(struct phy_device *phydev)
2637ece19502SDivya Koppera {
2638ece19502SDivya Koppera 	struct kszphy_priv *priv = phydev->priv;
2639ece19502SDivya Koppera 	struct kszphy_ptp_priv *ptp_priv = &priv->ptp_priv;
2640ece19502SDivya Koppera 	u16 status;
2641ece19502SDivya Koppera 
2642ece19502SDivya Koppera 	status = lanphy_read_page_reg(phydev, 5, PTP_TSU_INT_STS);
2643ece19502SDivya Koppera 	if (status & PTP_TSU_INT_STS_PTP_TX_TS_EN_)
2644ece19502SDivya Koppera 		lan8814_get_tx_ts(ptp_priv);
2645ece19502SDivya Koppera 
2646ece19502SDivya Koppera 	if (status & PTP_TSU_INT_STS_PTP_RX_TS_EN_)
2647ece19502SDivya Koppera 		lan8814_get_rx_ts(ptp_priv);
2648ece19502SDivya Koppera 
2649ece19502SDivya Koppera 	if (status & PTP_TSU_INT_STS_PTP_TX_TS_OVRFL_INT_) {
2650ece19502SDivya Koppera 		lan8814_flush_fifo(phydev, true);
2651ece19502SDivya Koppera 		skb_queue_purge(&ptp_priv->tx_queue);
2652ece19502SDivya Koppera 	}
2653ece19502SDivya Koppera 
2654ece19502SDivya Koppera 	if (status & PTP_TSU_INT_STS_PTP_RX_TS_OVRFL_INT_) {
2655ece19502SDivya Koppera 		lan8814_flush_fifo(phydev, false);
2656ece19502SDivya Koppera 		skb_queue_purge(&ptp_priv->rx_queue);
2657ece19502SDivya Koppera 	}
2658ece19502SDivya Koppera }
2659ece19502SDivya Koppera 
26607c2dcfa2SHoratiu Vultur static int lan8804_config_init(struct phy_device *phydev)
26617c2dcfa2SHoratiu Vultur {
26627c2dcfa2SHoratiu Vultur 	int val;
26637c2dcfa2SHoratiu Vultur 
26647c2dcfa2SHoratiu Vultur 	/* MDI-X setting for swap A,B transmit */
26657c2dcfa2SHoratiu Vultur 	val = lanphy_read_page_reg(phydev, 2, LAN8804_ALIGN_SWAP);
26667c2dcfa2SHoratiu Vultur 	val &= ~LAN8804_ALIGN_TX_A_B_SWAP_MASK;
26677c2dcfa2SHoratiu Vultur 	val |= LAN8804_ALIGN_TX_A_B_SWAP;
26687c2dcfa2SHoratiu Vultur 	lanphy_write_page_reg(phydev, 2, LAN8804_ALIGN_SWAP, val);
26697c2dcfa2SHoratiu Vultur 
26707c2dcfa2SHoratiu Vultur 	/* Make sure that the PHY will not stop generating the clock when the
26717c2dcfa2SHoratiu Vultur 	 * link partner goes down
26727c2dcfa2SHoratiu Vultur 	 */
26737c2dcfa2SHoratiu Vultur 	lanphy_write_page_reg(phydev, 31, LAN8814_CLOCK_MANAGEMENT, 0x27e);
26747c2dcfa2SHoratiu Vultur 	lanphy_read_page_reg(phydev, 1, LAN8814_LINK_QUALITY);
26757c2dcfa2SHoratiu Vultur 
26767c2dcfa2SHoratiu Vultur 	return 0;
26777c2dcfa2SHoratiu Vultur }
26787c2dcfa2SHoratiu Vultur 
2679b3ec7248SDivya Koppera static irqreturn_t lan8814_handle_interrupt(struct phy_device *phydev)
2680b3ec7248SDivya Koppera {
268112a4d677SWan Jiabing 	int irq_status, tsu_irq_status;
2682b3ec7248SDivya Koppera 
2683b3ec7248SDivya Koppera 	irq_status = phy_read(phydev, LAN8814_INTS);
2684ece19502SDivya Koppera 	if (irq_status > 0 && (irq_status & LAN8814_INT_LINK))
2685b3ec7248SDivya Koppera 		phy_trigger_machine(phydev);
2686b3ec7248SDivya Koppera 
2687ece19502SDivya Koppera 	if (irq_status < 0) {
2688ece19502SDivya Koppera 		phy_error(phydev);
2689ece19502SDivya Koppera 		return IRQ_NONE;
2690ece19502SDivya Koppera 	}
2691ece19502SDivya Koppera 
2692ece19502SDivya Koppera 	while (1) {
2693ece19502SDivya Koppera 		tsu_irq_status = lanphy_read_page_reg(phydev, 4,
2694ece19502SDivya Koppera 						      LAN8814_INTR_STS_REG);
2695ece19502SDivya Koppera 
2696ece19502SDivya Koppera 		if (tsu_irq_status > 0 &&
2697ece19502SDivya Koppera 		    (tsu_irq_status & (LAN8814_INTR_STS_REG_1588_TSU0_ |
2698ece19502SDivya Koppera 				       LAN8814_INTR_STS_REG_1588_TSU1_ |
2699ece19502SDivya Koppera 				       LAN8814_INTR_STS_REG_1588_TSU2_ |
2700ece19502SDivya Koppera 				       LAN8814_INTR_STS_REG_1588_TSU3_)))
2701ece19502SDivya Koppera 			lan8814_handle_ptp_interrupt(phydev);
2702ece19502SDivya Koppera 		else
2703ece19502SDivya Koppera 			break;
2704ece19502SDivya Koppera 	}
2705b3ec7248SDivya Koppera 	return IRQ_HANDLED;
2706b3ec7248SDivya Koppera }
2707b3ec7248SDivya Koppera 
2708b3ec7248SDivya Koppera static int lan8814_ack_interrupt(struct phy_device *phydev)
2709b3ec7248SDivya Koppera {
2710b3ec7248SDivya Koppera 	/* bit[12..0] int status, which is a read and clear register. */
2711b3ec7248SDivya Koppera 	int rc;
2712b3ec7248SDivya Koppera 
2713b3ec7248SDivya Koppera 	rc = phy_read(phydev, LAN8814_INTS);
2714b3ec7248SDivya Koppera 
2715b3ec7248SDivya Koppera 	return (rc < 0) ? rc : 0;
2716b3ec7248SDivya Koppera }
2717b3ec7248SDivya Koppera 
2718b3ec7248SDivya Koppera static int lan8814_config_intr(struct phy_device *phydev)
2719b3ec7248SDivya Koppera {
2720b3ec7248SDivya Koppera 	int err;
2721b3ec7248SDivya Koppera 
2722b3ec7248SDivya Koppera 	lanphy_write_page_reg(phydev, 4, LAN8814_INTR_CTRL_REG,
2723b3ec7248SDivya Koppera 			      LAN8814_INTR_CTRL_REG_POLARITY |
2724b3ec7248SDivya Koppera 			      LAN8814_INTR_CTRL_REG_INTR_ENABLE);
2725b3ec7248SDivya Koppera 
2726b3ec7248SDivya Koppera 	/* enable / disable interrupts */
2727b3ec7248SDivya Koppera 	if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
2728b3ec7248SDivya Koppera 		err = lan8814_ack_interrupt(phydev);
2729b3ec7248SDivya Koppera 		if (err)
2730b3ec7248SDivya Koppera 			return err;
2731b3ec7248SDivya Koppera 
2732b3ec7248SDivya Koppera 		err =  phy_write(phydev, LAN8814_INTC, LAN8814_INT_LINK);
2733b3ec7248SDivya Koppera 	} else {
2734b3ec7248SDivya Koppera 		err =  phy_write(phydev, LAN8814_INTC, 0);
2735b3ec7248SDivya Koppera 		if (err)
2736b3ec7248SDivya Koppera 			return err;
2737b3ec7248SDivya Koppera 
2738b3ec7248SDivya Koppera 		err = lan8814_ack_interrupt(phydev);
2739b3ec7248SDivya Koppera 	}
2740b3ec7248SDivya Koppera 
2741b3ec7248SDivya Koppera 	return err;
2742b3ec7248SDivya Koppera }
2743b3ec7248SDivya Koppera 
2744ece19502SDivya Koppera static void lan8814_ptp_init(struct phy_device *phydev)
2745ece19502SDivya Koppera {
2746ece19502SDivya Koppera 	struct kszphy_priv *priv = phydev->priv;
2747ece19502SDivya Koppera 	struct kszphy_ptp_priv *ptp_priv = &priv->ptp_priv;
2748ece19502SDivya Koppera 	u32 temp;
2749ece19502SDivya Koppera 
275031d00ca4SMichael Walle 	if (!IS_ENABLED(CONFIG_PTP_1588_CLOCK) ||
275131d00ca4SMichael Walle 	    !IS_ENABLED(CONFIG_NETWORK_PHY_TIMESTAMPING))
275231d00ca4SMichael Walle 		return;
275331d00ca4SMichael Walle 
2754ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 5, TSU_HARD_RESET, TSU_HARD_RESET_);
2755ece19502SDivya Koppera 
2756ece19502SDivya Koppera 	temp = lanphy_read_page_reg(phydev, 5, PTP_TX_MOD);
2757ece19502SDivya Koppera 	temp |= PTP_TX_MOD_BAD_UDPV4_CHKSUM_FORCE_FCS_DIS_;
2758ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 5, PTP_TX_MOD, temp);
2759ece19502SDivya Koppera 
2760ece19502SDivya Koppera 	temp = lanphy_read_page_reg(phydev, 5, PTP_RX_MOD);
2761ece19502SDivya Koppera 	temp |= PTP_RX_MOD_BAD_UDPV4_CHKSUM_FORCE_FCS_DIS_;
2762ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 5, PTP_RX_MOD, temp);
2763ece19502SDivya Koppera 
2764ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 5, PTP_RX_PARSE_CONFIG, 0);
2765ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 5, PTP_TX_PARSE_CONFIG, 0);
2766ece19502SDivya Koppera 
2767ece19502SDivya Koppera 	/* Removing default registers configs related to L2 and IP */
2768ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 5, PTP_TX_PARSE_L2_ADDR_EN, 0);
2769ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 5, PTP_RX_PARSE_L2_ADDR_EN, 0);
2770ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 5, PTP_TX_PARSE_IP_ADDR_EN, 0);
2771ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 5, PTP_RX_PARSE_IP_ADDR_EN, 0);
2772ece19502SDivya Koppera 
2773ece19502SDivya Koppera 	skb_queue_head_init(&ptp_priv->tx_queue);
2774ece19502SDivya Koppera 	skb_queue_head_init(&ptp_priv->rx_queue);
2775ece19502SDivya Koppera 	INIT_LIST_HEAD(&ptp_priv->rx_ts_list);
2776ece19502SDivya Koppera 	spin_lock_init(&ptp_priv->rx_ts_lock);
2777ece19502SDivya Koppera 
2778ece19502SDivya Koppera 	ptp_priv->phydev = phydev;
2779ece19502SDivya Koppera 
2780ece19502SDivya Koppera 	ptp_priv->mii_ts.rxtstamp = lan8814_rxtstamp;
2781ece19502SDivya Koppera 	ptp_priv->mii_ts.txtstamp = lan8814_txtstamp;
2782ece19502SDivya Koppera 	ptp_priv->mii_ts.hwtstamp = lan8814_hwtstamp;
2783ece19502SDivya Koppera 	ptp_priv->mii_ts.ts_info  = lan8814_ts_info;
2784ece19502SDivya Koppera 
2785ece19502SDivya Koppera 	phydev->mii_ts = &ptp_priv->mii_ts;
2786ece19502SDivya Koppera }
2787ece19502SDivya Koppera 
2788ece19502SDivya Koppera static int lan8814_ptp_probe_once(struct phy_device *phydev)
2789ece19502SDivya Koppera {
2790ece19502SDivya Koppera 	struct lan8814_shared_priv *shared = phydev->shared->priv;
2791ece19502SDivya Koppera 
279231d00ca4SMichael Walle 	if (!IS_ENABLED(CONFIG_PTP_1588_CLOCK) ||
279331d00ca4SMichael Walle 	    !IS_ENABLED(CONFIG_NETWORK_PHY_TIMESTAMPING))
279431d00ca4SMichael Walle 		return 0;
279531d00ca4SMichael Walle 
2796ece19502SDivya Koppera 	/* Initialise shared lock for clock*/
2797ece19502SDivya Koppera 	mutex_init(&shared->shared_lock);
2798ece19502SDivya Koppera 
2799ece19502SDivya Koppera 	shared->ptp_clock_info.owner = THIS_MODULE;
2800ece19502SDivya Koppera 	snprintf(shared->ptp_clock_info.name, 30, "%s", phydev->drv->name);
2801ece19502SDivya Koppera 	shared->ptp_clock_info.max_adj = 31249999;
2802ece19502SDivya Koppera 	shared->ptp_clock_info.n_alarm = 0;
2803ece19502SDivya Koppera 	shared->ptp_clock_info.n_ext_ts = 0;
2804ece19502SDivya Koppera 	shared->ptp_clock_info.n_pins = 0;
2805ece19502SDivya Koppera 	shared->ptp_clock_info.pps = 0;
2806ece19502SDivya Koppera 	shared->ptp_clock_info.pin_config = NULL;
2807ece19502SDivya Koppera 	shared->ptp_clock_info.adjfine = lan8814_ptpci_adjfine;
2808ece19502SDivya Koppera 	shared->ptp_clock_info.adjtime = lan8814_ptpci_adjtime;
2809ece19502SDivya Koppera 	shared->ptp_clock_info.gettime64 = lan8814_ptpci_gettime64;
2810ece19502SDivya Koppera 	shared->ptp_clock_info.settime64 = lan8814_ptpci_settime64;
2811ece19502SDivya Koppera 	shared->ptp_clock_info.getcrosststamp = NULL;
2812ece19502SDivya Koppera 
2813ece19502SDivya Koppera 	shared->ptp_clock = ptp_clock_register(&shared->ptp_clock_info,
2814ece19502SDivya Koppera 					       &phydev->mdio.dev);
2815ece19502SDivya Koppera 	if (IS_ERR_OR_NULL(shared->ptp_clock)) {
2816ece19502SDivya Koppera 		phydev_err(phydev, "ptp_clock_register failed %lu\n",
2817ece19502SDivya Koppera 			   PTR_ERR(shared->ptp_clock));
2818ece19502SDivya Koppera 		return -EINVAL;
2819ece19502SDivya Koppera 	}
2820ece19502SDivya Koppera 
2821ece19502SDivya Koppera 	phydev_dbg(phydev, "successfully registered ptp clock\n");
2822ece19502SDivya Koppera 
2823ece19502SDivya Koppera 	shared->phydev = phydev;
2824ece19502SDivya Koppera 
2825ece19502SDivya Koppera 	/* The EP.4 is shared between all the PHYs in the package and also it
2826ece19502SDivya Koppera 	 * can be accessed by any of the PHYs
2827ece19502SDivya Koppera 	 */
2828ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 4, LTC_HARD_RESET, LTC_HARD_RESET_);
2829ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 4, PTP_OPERATING_MODE,
2830ece19502SDivya Koppera 			      PTP_OPERATING_MODE_STANDALONE_);
2831ece19502SDivya Koppera 
2832ece19502SDivya Koppera 	return 0;
2833ece19502SDivya Koppera }
2834ece19502SDivya Koppera 
2835a516b7f7SDivya Koppera static void lan8814_setup_led(struct phy_device *phydev, int val)
2836a516b7f7SDivya Koppera {
2837a516b7f7SDivya Koppera 	int temp;
2838a516b7f7SDivya Koppera 
2839a516b7f7SDivya Koppera 	temp = lanphy_read_page_reg(phydev, 5, LAN8814_LED_CTRL_1);
2840a516b7f7SDivya Koppera 
2841a516b7f7SDivya Koppera 	if (val)
2842a516b7f7SDivya Koppera 		temp |= LAN8814_LED_CTRL_1_KSZ9031_LED_MODE_;
2843a516b7f7SDivya Koppera 	else
2844a516b7f7SDivya Koppera 		temp &= ~LAN8814_LED_CTRL_1_KSZ9031_LED_MODE_;
2845a516b7f7SDivya Koppera 
2846a516b7f7SDivya Koppera 	lanphy_write_page_reg(phydev, 5, LAN8814_LED_CTRL_1, temp);
2847a516b7f7SDivya Koppera }
2848a516b7f7SDivya Koppera 
2849ece19502SDivya Koppera static int lan8814_config_init(struct phy_device *phydev)
2850ece19502SDivya Koppera {
2851a516b7f7SDivya Koppera 	struct kszphy_priv *lan8814 = phydev->priv;
2852ece19502SDivya Koppera 	int val;
2853ece19502SDivya Koppera 
2854ece19502SDivya Koppera 	/* Reset the PHY */
2855ece19502SDivya Koppera 	val = lanphy_read_page_reg(phydev, 4, LAN8814_QSGMII_SOFT_RESET);
2856ece19502SDivya Koppera 	val |= LAN8814_QSGMII_SOFT_RESET_BIT;
2857ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 4, LAN8814_QSGMII_SOFT_RESET, val);
2858ece19502SDivya Koppera 
2859ece19502SDivya Koppera 	/* Disable ANEG with QSGMII PCS Host side */
2860ece19502SDivya Koppera 	val = lanphy_read_page_reg(phydev, 5, LAN8814_QSGMII_PCS1G_ANEG_CONFIG);
2861ece19502SDivya Koppera 	val &= ~LAN8814_QSGMII_PCS1G_ANEG_CONFIG_ANEG_ENA;
2862ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 5, LAN8814_QSGMII_PCS1G_ANEG_CONFIG, val);
2863ece19502SDivya Koppera 
2864ece19502SDivya Koppera 	/* MDI-X setting for swap A,B transmit */
2865ece19502SDivya Koppera 	val = lanphy_read_page_reg(phydev, 2, LAN8814_ALIGN_SWAP);
2866ece19502SDivya Koppera 	val &= ~LAN8814_ALIGN_TX_A_B_SWAP_MASK;
2867ece19502SDivya Koppera 	val |= LAN8814_ALIGN_TX_A_B_SWAP;
2868ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 2, LAN8814_ALIGN_SWAP, val);
2869ece19502SDivya Koppera 
2870a516b7f7SDivya Koppera 	if (lan8814->led_mode >= 0)
2871a516b7f7SDivya Koppera 		lan8814_setup_led(phydev, lan8814->led_mode);
2872a516b7f7SDivya Koppera 
2873ece19502SDivya Koppera 	return 0;
2874ece19502SDivya Koppera }
2875ece19502SDivya Koppera 
2876*4a4ce822SHoratiu Vultur /* It is expected that there will not be any 'lan8814_take_coma_mode'
2877*4a4ce822SHoratiu Vultur  * function called in suspend. Because the GPIO line can be shared, so if one of
2878*4a4ce822SHoratiu Vultur  * the phys goes back in coma mode, then all the other PHYs will go, which is
2879*4a4ce822SHoratiu Vultur  * wrong.
2880*4a4ce822SHoratiu Vultur  */
2881738871b0SMichael Walle static int lan8814_release_coma_mode(struct phy_device *phydev)
2882738871b0SMichael Walle {
2883738871b0SMichael Walle 	struct gpio_desc *gpiod;
2884738871b0SMichael Walle 
2885738871b0SMichael Walle 	gpiod = devm_gpiod_get_optional(&phydev->mdio.dev, "coma-mode",
2886*4a4ce822SHoratiu Vultur 					GPIOD_OUT_HIGH_OPEN_DRAIN |
2887*4a4ce822SHoratiu Vultur 					GPIOD_FLAGS_BIT_NONEXCLUSIVE);
2888738871b0SMichael Walle 	if (IS_ERR(gpiod))
2889738871b0SMichael Walle 		return PTR_ERR(gpiod);
2890738871b0SMichael Walle 
2891738871b0SMichael Walle 	gpiod_set_consumer_name(gpiod, "LAN8814 coma mode");
2892738871b0SMichael Walle 	gpiod_set_value_cansleep(gpiod, 0);
2893738871b0SMichael Walle 
2894738871b0SMichael Walle 	return 0;
2895738871b0SMichael Walle }
2896738871b0SMichael Walle 
2897ece19502SDivya Koppera static int lan8814_probe(struct phy_device *phydev)
2898ece19502SDivya Koppera {
2899a516b7f7SDivya Koppera 	const struct kszphy_type *type = phydev->drv->driver_data;
2900ece19502SDivya Koppera 	struct kszphy_priv *priv;
2901ece19502SDivya Koppera 	u16 addr;
2902ece19502SDivya Koppera 	int err;
2903ece19502SDivya Koppera 
2904ece19502SDivya Koppera 	priv = devm_kzalloc(&phydev->mdio.dev, sizeof(*priv), GFP_KERNEL);
2905ece19502SDivya Koppera 	if (!priv)
2906ece19502SDivya Koppera 		return -ENOMEM;
2907ece19502SDivya Koppera 
2908ece19502SDivya Koppera 	phydev->priv = priv;
2909ece19502SDivya Koppera 
2910a516b7f7SDivya Koppera 	priv->type = type;
2911a516b7f7SDivya Koppera 
2912a516b7f7SDivya Koppera 	kszphy_parse_led_mode(phydev);
2913a516b7f7SDivya Koppera 
2914ece19502SDivya Koppera 	/* Strap-in value for PHY address, below register read gives starting
2915ece19502SDivya Koppera 	 * phy address value
2916ece19502SDivya Koppera 	 */
2917ece19502SDivya Koppera 	addr = lanphy_read_page_reg(phydev, 4, 0) & 0x1F;
2918ece19502SDivya Koppera 	devm_phy_package_join(&phydev->mdio.dev, phydev,
2919ece19502SDivya Koppera 			      addr, sizeof(struct lan8814_shared_priv));
2920ece19502SDivya Koppera 
2921ece19502SDivya Koppera 	if (phy_package_init_once(phydev)) {
2922738871b0SMichael Walle 		err = lan8814_release_coma_mode(phydev);
2923738871b0SMichael Walle 		if (err)
2924738871b0SMichael Walle 			return err;
2925738871b0SMichael Walle 
2926ece19502SDivya Koppera 		err = lan8814_ptp_probe_once(phydev);
2927ece19502SDivya Koppera 		if (err)
2928ece19502SDivya Koppera 			return err;
2929ece19502SDivya Koppera 	}
2930ece19502SDivya Koppera 
2931ece19502SDivya Koppera 	lan8814_ptp_init(phydev);
2932ece19502SDivya Koppera 
2933ece19502SDivya Koppera 	return 0;
2934ece19502SDivya Koppera }
2935ece19502SDivya Koppera 
2936d5bf9071SChristian Hohnstaedt static struct phy_driver ksphy_driver[] = {
2937d5bf9071SChristian Hohnstaedt {
293851f932c4SChoi, David 	.phy_id		= PHY_ID_KS8737,
2939f893a99eSFabio Estevam 	.phy_id_mask	= MICREL_PHY_ID_MASK,
294051f932c4SChoi, David 	.name		= "Micrel KS8737",
2941dcdecdcfSHeiner Kallweit 	/* PHY_BASIC_FEATURES */
2942c6f9575cSJohan Hovold 	.driver_data	= &ks8737_type,
294315f03ffeSFabio Estevam 	.probe		= kszphy_probe,
2944d0507009SDavid J. Choi 	.config_init	= kszphy_config_init,
2945c6f9575cSJohan Hovold 	.config_intr	= kszphy_config_intr,
294659ca4e58SIoana Ciornei 	.handle_interrupt = kszphy_handle_interrupt,
2947f1131b9cSClaudiu Beznea 	.suspend	= kszphy_suspend,
2948f1131b9cSClaudiu Beznea 	.resume		= kszphy_resume,
2949d5bf9071SChristian Hohnstaedt }, {
2950212ea99aSMarek Vasut 	.phy_id		= PHY_ID_KSZ8021,
2951212ea99aSMarek Vasut 	.phy_id_mask	= 0x00ffffff,
29527ab59dc1SDavid J. Choi 	.name		= "Micrel KSZ8021 or KSZ8031",
2953dcdecdcfSHeiner Kallweit 	/* PHY_BASIC_FEATURES */
2954e6a423a8SJohan Hovold 	.driver_data	= &ksz8021_type,
295563f44b2bSJohan Hovold 	.probe		= kszphy_probe,
2956d0e1df9cSJohan Hovold 	.config_init	= kszphy_config_init,
2957212ea99aSMarek Vasut 	.config_intr	= kszphy_config_intr,
295859ca4e58SIoana Ciornei 	.handle_interrupt = kszphy_handle_interrupt,
29592b2427d0SAndrew Lunn 	.get_sset_count = kszphy_get_sset_count,
29602b2427d0SAndrew Lunn 	.get_strings	= kszphy_get_strings,
29612b2427d0SAndrew Lunn 	.get_stats	= kszphy_get_stats,
2962f1131b9cSClaudiu Beznea 	.suspend	= kszphy_suspend,
2963f1131b9cSClaudiu Beznea 	.resume		= kszphy_resume,
2964212ea99aSMarek Vasut }, {
2965b818d1a7SHector Palacios 	.phy_id		= PHY_ID_KSZ8031,
2966b818d1a7SHector Palacios 	.phy_id_mask	= 0x00ffffff,
2967b818d1a7SHector Palacios 	.name		= "Micrel KSZ8031",
2968dcdecdcfSHeiner Kallweit 	/* PHY_BASIC_FEATURES */
2969e6a423a8SJohan Hovold 	.driver_data	= &ksz8021_type,
297063f44b2bSJohan Hovold 	.probe		= kszphy_probe,
2971d0e1df9cSJohan Hovold 	.config_init	= kszphy_config_init,
2972b818d1a7SHector Palacios 	.config_intr	= kszphy_config_intr,
297359ca4e58SIoana Ciornei 	.handle_interrupt = kszphy_handle_interrupt,
29742b2427d0SAndrew Lunn 	.get_sset_count = kszphy_get_sset_count,
29752b2427d0SAndrew Lunn 	.get_strings	= kszphy_get_strings,
29762b2427d0SAndrew Lunn 	.get_stats	= kszphy_get_stats,
2977f1131b9cSClaudiu Beznea 	.suspend	= kszphy_suspend,
2978f1131b9cSClaudiu Beznea 	.resume		= kszphy_resume,
2979b818d1a7SHector Palacios }, {
2980510d573fSMarek Vasut 	.phy_id		= PHY_ID_KSZ8041,
2981f893a99eSFabio Estevam 	.phy_id_mask	= MICREL_PHY_ID_MASK,
2982510d573fSMarek Vasut 	.name		= "Micrel KSZ8041",
2983dcdecdcfSHeiner Kallweit 	/* PHY_BASIC_FEATURES */
2984e6a423a8SJohan Hovold 	.driver_data	= &ksz8041_type,
2985e6a423a8SJohan Hovold 	.probe		= kszphy_probe,
298677501a79SPhilipp Zabel 	.config_init	= ksz8041_config_init,
298777501a79SPhilipp Zabel 	.config_aneg	= ksz8041_config_aneg,
298851f932c4SChoi, David 	.config_intr	= kszphy_config_intr,
298959ca4e58SIoana Ciornei 	.handle_interrupt = kszphy_handle_interrupt,
29902b2427d0SAndrew Lunn 	.get_sset_count = kszphy_get_sset_count,
29912b2427d0SAndrew Lunn 	.get_strings	= kszphy_get_strings,
29922b2427d0SAndrew Lunn 	.get_stats	= kszphy_get_stats,
29932641b62dSStefan Agner 	/* No suspend/resume callbacks because of errata DS80000700A,
29942641b62dSStefan Agner 	 * receiver error following software power down.
29952641b62dSStefan Agner 	 */
2996d5bf9071SChristian Hohnstaedt }, {
29974bd7b512SSergei Shtylyov 	.phy_id		= PHY_ID_KSZ8041RNLI,
2998f893a99eSFabio Estevam 	.phy_id_mask	= MICREL_PHY_ID_MASK,
29994bd7b512SSergei Shtylyov 	.name		= "Micrel KSZ8041RNLI",
3000dcdecdcfSHeiner Kallweit 	/* PHY_BASIC_FEATURES */
3001e6a423a8SJohan Hovold 	.driver_data	= &ksz8041_type,
3002e6a423a8SJohan Hovold 	.probe		= kszphy_probe,
3003e6a423a8SJohan Hovold 	.config_init	= kszphy_config_init,
30044bd7b512SSergei Shtylyov 	.config_intr	= kszphy_config_intr,
300559ca4e58SIoana Ciornei 	.handle_interrupt = kszphy_handle_interrupt,
30062b2427d0SAndrew Lunn 	.get_sset_count = kszphy_get_sset_count,
30072b2427d0SAndrew Lunn 	.get_strings	= kszphy_get_strings,
30082b2427d0SAndrew Lunn 	.get_stats	= kszphy_get_stats,
3009f1131b9cSClaudiu Beznea 	.suspend	= kszphy_suspend,
3010f1131b9cSClaudiu Beznea 	.resume		= kszphy_resume,
30114bd7b512SSergei Shtylyov }, {
3012510d573fSMarek Vasut 	.name		= "Micrel KSZ8051",
3013dcdecdcfSHeiner Kallweit 	/* PHY_BASIC_FEATURES */
3014e6a423a8SJohan Hovold 	.driver_data	= &ksz8051_type,
3015e6a423a8SJohan Hovold 	.probe		= kszphy_probe,
301663f44b2bSJohan Hovold 	.config_init	= kszphy_config_init,
301751f932c4SChoi, David 	.config_intr	= kszphy_config_intr,
301859ca4e58SIoana Ciornei 	.handle_interrupt = kszphy_handle_interrupt,
30192b2427d0SAndrew Lunn 	.get_sset_count = kszphy_get_sset_count,
30202b2427d0SAndrew Lunn 	.get_strings	= kszphy_get_strings,
30212b2427d0SAndrew Lunn 	.get_stats	= kszphy_get_stats,
30228b95599cSMarek Vasut 	.match_phy_device = ksz8051_match_phy_device,
3023f1131b9cSClaudiu Beznea 	.suspend	= kszphy_suspend,
3024f1131b9cSClaudiu Beznea 	.resume		= kszphy_resume,
3025d5bf9071SChristian Hohnstaedt }, {
3026510d573fSMarek Vasut 	.phy_id		= PHY_ID_KSZ8001,
3027510d573fSMarek Vasut 	.name		= "Micrel KSZ8001 or KS8721",
3028ecd5a323SAlexander Stein 	.phy_id_mask	= 0x00fffffc,
3029dcdecdcfSHeiner Kallweit 	/* PHY_BASIC_FEATURES */
3030e6a423a8SJohan Hovold 	.driver_data	= &ksz8041_type,
3031e6a423a8SJohan Hovold 	.probe		= kszphy_probe,
3032e6a423a8SJohan Hovold 	.config_init	= kszphy_config_init,
303351f932c4SChoi, David 	.config_intr	= kszphy_config_intr,
303459ca4e58SIoana Ciornei 	.handle_interrupt = kszphy_handle_interrupt,
30352b2427d0SAndrew Lunn 	.get_sset_count = kszphy_get_sset_count,
30362b2427d0SAndrew Lunn 	.get_strings	= kszphy_get_strings,
30372b2427d0SAndrew Lunn 	.get_stats	= kszphy_get_stats,
3038f1131b9cSClaudiu Beznea 	.suspend	= kszphy_suspend,
3039f1131b9cSClaudiu Beznea 	.resume		= kszphy_resume,
3040d5bf9071SChristian Hohnstaedt }, {
30417ab59dc1SDavid J. Choi 	.phy_id		= PHY_ID_KSZ8081,
30427ab59dc1SDavid J. Choi 	.name		= "Micrel KSZ8081 or KSZ8091",
3043f893a99eSFabio Estevam 	.phy_id_mask	= MICREL_PHY_ID_MASK,
304449011e0cSOleksij Rempel 	.flags		= PHY_POLL_CABLE_TEST,
3045dcdecdcfSHeiner Kallweit 	/* PHY_BASIC_FEATURES */
3046e6a423a8SJohan Hovold 	.driver_data	= &ksz8081_type,
3047e6a423a8SJohan Hovold 	.probe		= kszphy_probe,
30487a1d8390SAntoine Tenart 	.config_init	= ksz8081_config_init,
3049764d31caSChristian Melki 	.soft_reset	= genphy_soft_reset,
3050f873f112SOleksij Rempel 	.config_aneg	= ksz8081_config_aneg,
3051f873f112SOleksij Rempel 	.read_status	= ksz8081_read_status,
30527ab59dc1SDavid J. Choi 	.config_intr	= kszphy_config_intr,
305359ca4e58SIoana Ciornei 	.handle_interrupt = kszphy_handle_interrupt,
30542b2427d0SAndrew Lunn 	.get_sset_count = kszphy_get_sset_count,
30552b2427d0SAndrew Lunn 	.get_strings	= kszphy_get_strings,
30562b2427d0SAndrew Lunn 	.get_stats	= kszphy_get_stats,
3057836384d2SWenyou Yang 	.suspend	= kszphy_suspend,
3058f5aba91dSAlexandre Belloni 	.resume		= kszphy_resume,
305949011e0cSOleksij Rempel 	.cable_test_start	= ksz886x_cable_test_start,
306049011e0cSOleksij Rempel 	.cable_test_get_status	= ksz886x_cable_test_get_status,
30617ab59dc1SDavid J. Choi }, {
30627ab59dc1SDavid J. Choi 	.phy_id		= PHY_ID_KSZ8061,
30637ab59dc1SDavid J. Choi 	.name		= "Micrel KSZ8061",
3064f893a99eSFabio Estevam 	.phy_id_mask	= MICREL_PHY_ID_MASK,
3065dcdecdcfSHeiner Kallweit 	/* PHY_BASIC_FEATURES */
30668e6004dfSFabio Estevam 	.probe		= kszphy_probe,
3067232ba3a5SRajasingh Thavamani 	.config_init	= ksz8061_config_init,
30687ab59dc1SDavid J. Choi 	.config_intr	= kszphy_config_intr,
306959ca4e58SIoana Ciornei 	.handle_interrupt = kszphy_handle_interrupt,
30708e6004dfSFabio Estevam 	.suspend	= kszphy_suspend,
30718e6004dfSFabio Estevam 	.resume		= kszphy_resume,
30727ab59dc1SDavid J. Choi }, {
3073d0507009SDavid J. Choi 	.phy_id		= PHY_ID_KSZ9021,
307448d7d0adSJason Wang 	.phy_id_mask	= 0x000ffffe,
3075d0507009SDavid J. Choi 	.name		= "Micrel KSZ9021 Gigabit PHY",
3076dcdecdcfSHeiner Kallweit 	/* PHY_GBIT_FEATURES */
3077c6f9575cSJohan Hovold 	.driver_data	= &ksz9021_type,
3078bfe72442SGrygorii Strashko 	.probe		= kszphy_probe,
3079407d8098SHans Andersson 	.get_features	= ksz9031_get_features,
3080954c3967SSean Cross 	.config_init	= ksz9021_config_init,
3081c6f9575cSJohan Hovold 	.config_intr	= kszphy_config_intr,
308259ca4e58SIoana Ciornei 	.handle_interrupt = kszphy_handle_interrupt,
30832b2427d0SAndrew Lunn 	.get_sset_count = kszphy_get_sset_count,
30842b2427d0SAndrew Lunn 	.get_strings	= kszphy_get_strings,
30852b2427d0SAndrew Lunn 	.get_stats	= kszphy_get_stats,
3086f1131b9cSClaudiu Beznea 	.suspend	= kszphy_suspend,
3087f1131b9cSClaudiu Beznea 	.resume		= kszphy_resume,
3088c846a2b7SKevin Hao 	.read_mmd	= genphy_read_mmd_unsupported,
3089c846a2b7SKevin Hao 	.write_mmd	= genphy_write_mmd_unsupported,
309093272e07SJean-Christophe PLAGNIOL-VILLARD }, {
30917ab59dc1SDavid J. Choi 	.phy_id		= PHY_ID_KSZ9031,
3092f893a99eSFabio Estevam 	.phy_id_mask	= MICREL_PHY_ID_MASK,
30937ab59dc1SDavid J. Choi 	.name		= "Micrel KSZ9031 Gigabit PHY",
309458389c00SMarek Vasut 	.flags		= PHY_POLL_CABLE_TEST,
3095c6f9575cSJohan Hovold 	.driver_data	= &ksz9021_type,
3096bfe72442SGrygorii Strashko 	.probe		= kszphy_probe,
30973aed3e2aSAntoine Tenart 	.get_features	= ksz9031_get_features,
30986e4b8273SHubert Chaumette 	.config_init	= ksz9031_config_init,
30991d16073aSHeiner Kallweit 	.soft_reset	= genphy_soft_reset,
3100d2fd719bSNathan Sullivan 	.read_status	= ksz9031_read_status,
3101c6f9575cSJohan Hovold 	.config_intr	= kszphy_config_intr,
310259ca4e58SIoana Ciornei 	.handle_interrupt = kszphy_handle_interrupt,
31032b2427d0SAndrew Lunn 	.get_sset_count = kszphy_get_sset_count,
31042b2427d0SAndrew Lunn 	.get_strings	= kszphy_get_strings,
31052b2427d0SAndrew Lunn 	.get_stats	= kszphy_get_stats,
3106f1131b9cSClaudiu Beznea 	.suspend	= kszphy_suspend,
3107f64f1482SXander Huff 	.resume		= kszphy_resume,
310858389c00SMarek Vasut 	.cable_test_start	= ksz9x31_cable_test_start,
310958389c00SMarek Vasut 	.cable_test_get_status	= ksz9x31_cable_test_get_status,
31107ab59dc1SDavid J. Choi }, {
31111623ad8eSDivya Koppera 	.phy_id		= PHY_ID_LAN8814,
31121623ad8eSDivya Koppera 	.phy_id_mask	= MICREL_PHY_ID_MASK,
31131623ad8eSDivya Koppera 	.name		= "Microchip INDY Gigabit Quad PHY",
31147467d716SHoratiu Vultur 	.config_init	= lan8814_config_init,
3115a516b7f7SDivya Koppera 	.driver_data	= &lan8814_type,
3116ece19502SDivya Koppera 	.probe		= lan8814_probe,
31171623ad8eSDivya Koppera 	.soft_reset	= genphy_soft_reset,
3118b814403aSHoratiu Vultur 	.read_status	= ksz9031_read_status,
31191623ad8eSDivya Koppera 	.get_sset_count	= kszphy_get_sset_count,
31201623ad8eSDivya Koppera 	.get_strings	= kszphy_get_strings,
31211623ad8eSDivya Koppera 	.get_stats	= kszphy_get_stats,
31221623ad8eSDivya Koppera 	.suspend	= genphy_suspend,
31231623ad8eSDivya Koppera 	.resume		= kszphy_resume,
3124b3ec7248SDivya Koppera 	.config_intr	= lan8814_config_intr,
3125b3ec7248SDivya Koppera 	.handle_interrupt = lan8814_handle_interrupt,
31261623ad8eSDivya Koppera }, {
31277c2dcfa2SHoratiu Vultur 	.phy_id		= PHY_ID_LAN8804,
31287c2dcfa2SHoratiu Vultur 	.phy_id_mask	= MICREL_PHY_ID_MASK,
31297c2dcfa2SHoratiu Vultur 	.name		= "Microchip LAN966X Gigabit PHY",
31307c2dcfa2SHoratiu Vultur 	.config_init	= lan8804_config_init,
31317c2dcfa2SHoratiu Vultur 	.driver_data	= &ksz9021_type,
31327c2dcfa2SHoratiu Vultur 	.probe		= kszphy_probe,
31337c2dcfa2SHoratiu Vultur 	.soft_reset	= genphy_soft_reset,
31347c2dcfa2SHoratiu Vultur 	.read_status	= ksz9031_read_status,
31357c2dcfa2SHoratiu Vultur 	.get_sset_count	= kszphy_get_sset_count,
31367c2dcfa2SHoratiu Vultur 	.get_strings	= kszphy_get_strings,
31377c2dcfa2SHoratiu Vultur 	.get_stats	= kszphy_get_stats,
31387c2dcfa2SHoratiu Vultur 	.suspend	= genphy_suspend,
31397c2dcfa2SHoratiu Vultur 	.resume		= kszphy_resume,
31407c2dcfa2SHoratiu Vultur }, {
3141bff5b4b3SYuiko Oshino 	.phy_id		= PHY_ID_KSZ9131,
3142bff5b4b3SYuiko Oshino 	.phy_id_mask	= MICREL_PHY_ID_MASK,
3143bff5b4b3SYuiko Oshino 	.name		= "Microchip KSZ9131 Gigabit PHY",
3144dcdecdcfSHeiner Kallweit 	/* PHY_GBIT_FEATURES */
314558389c00SMarek Vasut 	.flags		= PHY_POLL_CABLE_TEST,
3146bff5b4b3SYuiko Oshino 	.driver_data	= &ksz9021_type,
3147bff5b4b3SYuiko Oshino 	.probe		= kszphy_probe,
3148bff5b4b3SYuiko Oshino 	.config_init	= ksz9131_config_init,
3149bff5b4b3SYuiko Oshino 	.config_intr	= kszphy_config_intr,
315059ca4e58SIoana Ciornei 	.handle_interrupt = kszphy_handle_interrupt,
3151bff5b4b3SYuiko Oshino 	.get_sset_count = kszphy_get_sset_count,
3152bff5b4b3SYuiko Oshino 	.get_strings	= kszphy_get_strings,
3153bff5b4b3SYuiko Oshino 	.get_stats	= kszphy_get_stats,
3154f1131b9cSClaudiu Beznea 	.suspend	= kszphy_suspend,
3155bff5b4b3SYuiko Oshino 	.resume		= kszphy_resume,
315658389c00SMarek Vasut 	.cable_test_start	= ksz9x31_cable_test_start,
315758389c00SMarek Vasut 	.cable_test_get_status	= ksz9x31_cable_test_get_status,
3158bff5b4b3SYuiko Oshino }, {
315993272e07SJean-Christophe PLAGNIOL-VILLARD 	.phy_id		= PHY_ID_KSZ8873MLL,
3160f893a99eSFabio Estevam 	.phy_id_mask	= MICREL_PHY_ID_MASK,
316193272e07SJean-Christophe PLAGNIOL-VILLARD 	.name		= "Micrel KSZ8873MLL Switch",
3162dcdecdcfSHeiner Kallweit 	/* PHY_BASIC_FEATURES */
316393272e07SJean-Christophe PLAGNIOL-VILLARD 	.config_init	= kszphy_config_init,
316493272e07SJean-Christophe PLAGNIOL-VILLARD 	.config_aneg	= ksz8873mll_config_aneg,
316593272e07SJean-Christophe PLAGNIOL-VILLARD 	.read_status	= ksz8873mll_read_status,
31661a5465f5SPatrice Vilchez 	.suspend	= genphy_suspend,
31671a5465f5SPatrice Vilchez 	.resume		= genphy_resume,
31687ab59dc1SDavid J. Choi }, {
31697ab59dc1SDavid J. Choi 	.phy_id		= PHY_ID_KSZ886X,
3170f893a99eSFabio Estevam 	.phy_id_mask	= MICREL_PHY_ID_MASK,
3171ab36a3a2SMarek Vasut 	.name		= "Micrel KSZ8851 Ethernet MAC or KSZ886X Switch",
3172dcdecdcfSHeiner Kallweit 	/* PHY_BASIC_FEATURES */
317349011e0cSOleksij Rempel 	.flags		= PHY_POLL_CABLE_TEST,
31747ab59dc1SDavid J. Choi 	.config_init	= kszphy_config_init,
317552939393SOleksij Rempel 	.config_aneg	= ksz886x_config_aneg,
317652939393SOleksij Rempel 	.read_status	= ksz886x_read_status,
31771a5465f5SPatrice Vilchez 	.suspend	= genphy_suspend,
31781a5465f5SPatrice Vilchez 	.resume		= genphy_resume,
317949011e0cSOleksij Rempel 	.cable_test_start	= ksz886x_cable_test_start,
318049011e0cSOleksij Rempel 	.cable_test_get_status	= ksz886x_cable_test_get_status,
31819d162ed6SSean Nyekjaer }, {
31821d951ba3SMarek Vasut 	.name		= "Micrel KSZ87XX Switch",
3183dcdecdcfSHeiner Kallweit 	/* PHY_BASIC_FEATURES */
31849d162ed6SSean Nyekjaer 	.config_init	= kszphy_config_init,
31858b95599cSMarek Vasut 	.match_phy_device = ksz8795_match_phy_device,
31869d162ed6SSean Nyekjaer 	.suspend	= genphy_suspend,
31879d162ed6SSean Nyekjaer 	.resume		= genphy_resume,
3188fc3973a1SWoojung Huh }, {
3189fc3973a1SWoojung Huh 	.phy_id		= PHY_ID_KSZ9477,
3190fc3973a1SWoojung Huh 	.phy_id_mask	= MICREL_PHY_ID_MASK,
3191fc3973a1SWoojung Huh 	.name		= "Microchip KSZ9477",
3192dcdecdcfSHeiner Kallweit 	/* PHY_GBIT_FEATURES */
3193fc3973a1SWoojung Huh 	.config_init	= kszphy_config_init,
3194fc3973a1SWoojung Huh 	.suspend	= genphy_suspend,
3195fc3973a1SWoojung Huh 	.resume		= genphy_resume,
3196d5bf9071SChristian Hohnstaedt } };
3197d0507009SDavid J. Choi 
319850fd7150SJohan Hovold module_phy_driver(ksphy_driver);
3199d0507009SDavid J. Choi 
3200d0507009SDavid J. Choi MODULE_DESCRIPTION("Micrel PHY driver");
3201d0507009SDavid J. Choi MODULE_AUTHOR("David J. Choi");
3202d0507009SDavid J. Choi MODULE_LICENSE("GPL");
320352a60ed2SDavid S. Miller 
3204cf93c945SUwe Kleine-König static struct mdio_device_id __maybe_unused micrel_tbl[] = {
320548d7d0adSJason Wang 	{ PHY_ID_KSZ9021, 0x000ffffe },
3206f893a99eSFabio Estevam 	{ PHY_ID_KSZ9031, MICREL_PHY_ID_MASK },
3207bff5b4b3SYuiko Oshino 	{ PHY_ID_KSZ9131, MICREL_PHY_ID_MASK },
3208ecd5a323SAlexander Stein 	{ PHY_ID_KSZ8001, 0x00fffffc },
3209f893a99eSFabio Estevam 	{ PHY_ID_KS8737, MICREL_PHY_ID_MASK },
3210212ea99aSMarek Vasut 	{ PHY_ID_KSZ8021, 0x00ffffff },
3211b818d1a7SHector Palacios 	{ PHY_ID_KSZ8031, 0x00ffffff },
3212f893a99eSFabio Estevam 	{ PHY_ID_KSZ8041, MICREL_PHY_ID_MASK },
3213f893a99eSFabio Estevam 	{ PHY_ID_KSZ8051, MICREL_PHY_ID_MASK },
3214f893a99eSFabio Estevam 	{ PHY_ID_KSZ8061, MICREL_PHY_ID_MASK },
3215f893a99eSFabio Estevam 	{ PHY_ID_KSZ8081, MICREL_PHY_ID_MASK },
3216f893a99eSFabio Estevam 	{ PHY_ID_KSZ8873MLL, MICREL_PHY_ID_MASK },
3217f893a99eSFabio Estevam 	{ PHY_ID_KSZ886X, MICREL_PHY_ID_MASK },
32181623ad8eSDivya Koppera 	{ PHY_ID_LAN8814, MICREL_PHY_ID_MASK },
32197c2dcfa2SHoratiu Vultur 	{ PHY_ID_LAN8804, MICREL_PHY_ID_MASK },
322052a60ed2SDavid S. Miller 	{ }
322152a60ed2SDavid S. Miller };
322252a60ed2SDavid S. Miller 
322352a60ed2SDavid S. Miller MODULE_DEVICE_TABLE(mdio, micrel_tbl);
3224