xref: /openbmc/linux/drivers/net/phy/micrel.c (revision 0db00e5d86dc793aab9722ad3728d99166eb7d96)
1a2443fd1SAndrew Lunn // SPDX-License-Identifier: GPL-2.0+
2d0507009SDavid J. Choi /*
3d0507009SDavid J. Choi  * drivers/net/phy/micrel.c
4d0507009SDavid J. Choi  *
5d0507009SDavid J. Choi  * Driver for Micrel PHYs
6d0507009SDavid J. Choi  *
7d0507009SDavid J. Choi  * Author: David J. Choi
8d0507009SDavid J. Choi  *
97ab59dc1SDavid J. Choi  * Copyright (c) 2010-2013 Micrel, Inc.
10ee0dc2fbSJohan Hovold  * Copyright (c) 2014 Johan Hovold <johan@kernel.org>
11d0507009SDavid J. Choi  *
127ab59dc1SDavid J. Choi  * Support : Micrel Phys:
133e9c0700SHoratiu Vultur  *		Giga phys: ksz9021, ksz9031, ksz9131, lan8841, lan8814
147ab59dc1SDavid J. Choi  *		100/10 Phys : ksz8001, ksz8721, ksz8737, ksz8041
157ab59dc1SDavid J. Choi  *			   ksz8021, ksz8031, ksz8051,
167ab59dc1SDavid J. Choi  *			   ksz8081, ksz8091,
177ab59dc1SDavid J. Choi  *			   ksz8061,
187ab59dc1SDavid J. Choi  *		Switch : ksz8873, ksz886x
193e9c0700SHoratiu Vultur  *			 ksz9477, lan8804
20d0507009SDavid J. Choi  */
21d0507009SDavid J. Choi 
22bcf3440cSOleksij Rempel #include <linux/bitfield.h>
2349011e0cSOleksij Rempel #include <linux/ethtool_netlink.h>
24d0507009SDavid J. Choi #include <linux/kernel.h>
25d0507009SDavid J. Choi #include <linux/module.h>
26d0507009SDavid J. Choi #include <linux/phy.h>
27d606ef3fSBaruch Siach #include <linux/micrel_phy.h>
28954c3967SSean Cross #include <linux/of.h>
291fadee0cSSascha Hauer #include <linux/clk.h>
306110dff7SOleksij Rempel #include <linux/delay.h>
31ece19502SDivya Koppera #include <linux/ptp_clock_kernel.h>
32ece19502SDivya Koppera #include <linux/ptp_clock.h>
33ece19502SDivya Koppera #include <linux/ptp_classify.h>
34ece19502SDivya Koppera #include <linux/net_tstamp.h>
35738871b0SMichael Walle #include <linux/gpio/consumer.h>
36d0507009SDavid J. Choi 
37212ea99aSMarek Vasut /* Operation Mode Strap Override */
38212ea99aSMarek Vasut #define MII_KSZPHY_OMSO				0x16
397a1d8390SAntoine Tenart #define KSZPHY_OMSO_FACTORY_TEST		BIT(15)
4000aee095SJohan Hovold #define KSZPHY_OMSO_B_CAST_OFF			BIT(9)
412b0ba96cSSylvain Rochet #define KSZPHY_OMSO_NAND_TREE_ON		BIT(5)
4200aee095SJohan Hovold #define KSZPHY_OMSO_RMII_OVERRIDE		BIT(1)
4300aee095SJohan Hovold #define KSZPHY_OMSO_MII_OVERRIDE		BIT(0)
44212ea99aSMarek Vasut 
4551f932c4SChoi, David /* general Interrupt control/status reg in vendor specific block. */
4651f932c4SChoi, David #define MII_KSZPHY_INTCS			0x1B
4700aee095SJohan Hovold #define KSZPHY_INTCS_JABBER			BIT(15)
4800aee095SJohan Hovold #define KSZPHY_INTCS_RECEIVE_ERR		BIT(14)
4900aee095SJohan Hovold #define KSZPHY_INTCS_PAGE_RECEIVE		BIT(13)
5000aee095SJohan Hovold #define KSZPHY_INTCS_PARELLEL			BIT(12)
5100aee095SJohan Hovold #define KSZPHY_INTCS_LINK_PARTNER_ACK		BIT(11)
5200aee095SJohan Hovold #define KSZPHY_INTCS_LINK_DOWN			BIT(10)
5300aee095SJohan Hovold #define KSZPHY_INTCS_REMOTE_FAULT		BIT(9)
5400aee095SJohan Hovold #define KSZPHY_INTCS_LINK_UP			BIT(8)
5551f932c4SChoi, David #define KSZPHY_INTCS_ALL			(KSZPHY_INTCS_LINK_UP |\
5651f932c4SChoi, David 						KSZPHY_INTCS_LINK_DOWN)
5759ca4e58SIoana Ciornei #define KSZPHY_INTCS_LINK_DOWN_STATUS		BIT(2)
5859ca4e58SIoana Ciornei #define KSZPHY_INTCS_LINK_UP_STATUS		BIT(0)
5959ca4e58SIoana Ciornei #define KSZPHY_INTCS_STATUS			(KSZPHY_INTCS_LINK_DOWN_STATUS |\
6059ca4e58SIoana Ciornei 						 KSZPHY_INTCS_LINK_UP_STATUS)
6151f932c4SChoi, David 
6249011e0cSOleksij Rempel /* LinkMD Control/Status */
6349011e0cSOleksij Rempel #define KSZ8081_LMD				0x1d
6449011e0cSOleksij Rempel #define KSZ8081_LMD_ENABLE_TEST			BIT(15)
6549011e0cSOleksij Rempel #define KSZ8081_LMD_STAT_NORMAL			0
6649011e0cSOleksij Rempel #define KSZ8081_LMD_STAT_OPEN			1
6749011e0cSOleksij Rempel #define KSZ8081_LMD_STAT_SHORT			2
6849011e0cSOleksij Rempel #define KSZ8081_LMD_STAT_FAIL			3
6949011e0cSOleksij Rempel #define KSZ8081_LMD_STAT_MASK			GENMASK(14, 13)
7049011e0cSOleksij Rempel /* Short cable (<10 meter) has been detected by LinkMD */
7149011e0cSOleksij Rempel #define KSZ8081_LMD_SHORT_INDICATOR		BIT(12)
7249011e0cSOleksij Rempel #define KSZ8081_LMD_DELTA_TIME_MASK		GENMASK(8, 0)
7349011e0cSOleksij Rempel 
7458389c00SMarek Vasut #define KSZ9x31_LMD				0x12
7558389c00SMarek Vasut #define KSZ9x31_LMD_VCT_EN			BIT(15)
7658389c00SMarek Vasut #define KSZ9x31_LMD_VCT_DIS_TX			BIT(14)
7758389c00SMarek Vasut #define KSZ9x31_LMD_VCT_PAIR(n)			(((n) & 0x3) << 12)
7858389c00SMarek Vasut #define KSZ9x31_LMD_VCT_SEL_RESULT		0
7958389c00SMarek Vasut #define KSZ9x31_LMD_VCT_SEL_THRES_HI		BIT(10)
8058389c00SMarek Vasut #define KSZ9x31_LMD_VCT_SEL_THRES_LO		BIT(11)
8158389c00SMarek Vasut #define KSZ9x31_LMD_VCT_SEL_MASK		GENMASK(11, 10)
8258389c00SMarek Vasut #define KSZ9x31_LMD_VCT_ST_NORMAL		0
8358389c00SMarek Vasut #define KSZ9x31_LMD_VCT_ST_OPEN			1
8458389c00SMarek Vasut #define KSZ9x31_LMD_VCT_ST_SHORT		2
8558389c00SMarek Vasut #define KSZ9x31_LMD_VCT_ST_FAIL			3
8658389c00SMarek Vasut #define KSZ9x31_LMD_VCT_ST_MASK			GENMASK(9, 8)
8758389c00SMarek Vasut #define KSZ9x31_LMD_VCT_DATA_REFLECTED_INVALID	BIT(7)
8858389c00SMarek Vasut #define KSZ9x31_LMD_VCT_DATA_SIG_WAIT_TOO_LONG	BIT(6)
8958389c00SMarek Vasut #define KSZ9x31_LMD_VCT_DATA_MASK100		BIT(5)
9058389c00SMarek Vasut #define KSZ9x31_LMD_VCT_DATA_NLP_FLP		BIT(4)
9158389c00SMarek Vasut #define KSZ9x31_LMD_VCT_DATA_LO_PULSE_MASK	GENMASK(3, 2)
9258389c00SMarek Vasut #define KSZ9x31_LMD_VCT_DATA_HI_PULSE_MASK	GENMASK(1, 0)
9358389c00SMarek Vasut #define KSZ9x31_LMD_VCT_DATA_MASK		GENMASK(7, 0)
9458389c00SMarek Vasut 
9521b688daSDivya Koppera #define KSZPHY_WIRE_PAIR_MASK			0x3
9621b688daSDivya Koppera 
9721b688daSDivya Koppera #define LAN8814_CABLE_DIAG			0x12
9821b688daSDivya Koppera #define LAN8814_CABLE_DIAG_STAT_MASK		GENMASK(9, 8)
9921b688daSDivya Koppera #define LAN8814_CABLE_DIAG_VCT_DATA_MASK	GENMASK(7, 0)
10021b688daSDivya Koppera #define LAN8814_PAIR_BIT_SHIFT			12
10121b688daSDivya Koppera 
10221b688daSDivya Koppera #define LAN8814_WIRE_PAIR_MASK			0xF
10321b688daSDivya Koppera 
104b3ec7248SDivya Koppera /* Lan8814 general Interrupt control/status reg in GPHY specific block. */
105b3ec7248SDivya Koppera #define LAN8814_INTC				0x18
106b3ec7248SDivya Koppera #define LAN8814_INTS				0x1B
107b3ec7248SDivya Koppera 
108b3ec7248SDivya Koppera #define LAN8814_INT_LINK_DOWN			BIT(2)
109b3ec7248SDivya Koppera #define LAN8814_INT_LINK_UP			BIT(0)
110b3ec7248SDivya Koppera #define LAN8814_INT_LINK			(LAN8814_INT_LINK_UP |\
111b3ec7248SDivya Koppera 						 LAN8814_INT_LINK_DOWN)
112b3ec7248SDivya Koppera 
113b3ec7248SDivya Koppera #define LAN8814_INTR_CTRL_REG			0x34
114b3ec7248SDivya Koppera #define LAN8814_INTR_CTRL_REG_POLARITY		BIT(1)
115b3ec7248SDivya Koppera #define LAN8814_INTR_CTRL_REG_INTR_ENABLE	BIT(0)
116b3ec7248SDivya Koppera 
117ece19502SDivya Koppera /* Represents 1ppm adjustment in 2^32 format with
118ece19502SDivya Koppera  * each nsec contains 4 clock cycles.
119ece19502SDivya Koppera  * The value is calculated as following: (1/1000000)/((2^-32)/4)
120ece19502SDivya Koppera  */
121ece19502SDivya Koppera #define LAN8814_1PPM_FORMAT			17179
122ece19502SDivya Koppera 
123784207bdSHoratiu Vultur #define PTP_RX_VERSION				0x0248
124784207bdSHoratiu Vultur #define PTP_TX_VERSION				0x0288
125784207bdSHoratiu Vultur #define PTP_MAX_VERSION(x)			(((x) & GENMASK(7, 0)) << 8)
126784207bdSHoratiu Vultur #define PTP_MIN_VERSION(x)			((x) & GENMASK(7, 0))
127784207bdSHoratiu Vultur 
128ece19502SDivya Koppera #define PTP_RX_MOD				0x024F
129ece19502SDivya Koppera #define PTP_RX_MOD_BAD_UDPV4_CHKSUM_FORCE_FCS_DIS_ BIT(3)
130ece19502SDivya Koppera #define PTP_RX_TIMESTAMP_EN			0x024D
131ece19502SDivya Koppera #define PTP_TX_TIMESTAMP_EN			0x028D
132ece19502SDivya Koppera 
133ece19502SDivya Koppera #define PTP_TIMESTAMP_EN_SYNC_			BIT(0)
134ece19502SDivya Koppera #define PTP_TIMESTAMP_EN_DREQ_			BIT(1)
135ece19502SDivya Koppera #define PTP_TIMESTAMP_EN_PDREQ_			BIT(2)
136ece19502SDivya Koppera #define PTP_TIMESTAMP_EN_PDRES_			BIT(3)
137ece19502SDivya Koppera 
138ece19502SDivya Koppera #define PTP_TX_PARSE_L2_ADDR_EN			0x0284
139ece19502SDivya Koppera #define PTP_RX_PARSE_L2_ADDR_EN			0x0244
140ece19502SDivya Koppera 
141ece19502SDivya Koppera #define PTP_TX_PARSE_IP_ADDR_EN			0x0285
142ece19502SDivya Koppera #define PTP_RX_PARSE_IP_ADDR_EN			0x0245
143ece19502SDivya Koppera #define LTC_HARD_RESET				0x023F
144ece19502SDivya Koppera #define LTC_HARD_RESET_				BIT(0)
145ece19502SDivya Koppera 
146ece19502SDivya Koppera #define TSU_HARD_RESET				0x02C1
147ece19502SDivya Koppera #define TSU_HARD_RESET_				BIT(0)
148ece19502SDivya Koppera 
149ece19502SDivya Koppera #define PTP_CMD_CTL				0x0200
150ece19502SDivya Koppera #define PTP_CMD_CTL_PTP_DISABLE_		BIT(0)
151ece19502SDivya Koppera #define PTP_CMD_CTL_PTP_ENABLE_			BIT(1)
152ece19502SDivya Koppera #define PTP_CMD_CTL_PTP_CLOCK_READ_		BIT(3)
153ece19502SDivya Koppera #define PTP_CMD_CTL_PTP_CLOCK_LOAD_		BIT(4)
154ece19502SDivya Koppera #define PTP_CMD_CTL_PTP_LTC_STEP_SEC_		BIT(5)
155ece19502SDivya Koppera #define PTP_CMD_CTL_PTP_LTC_STEP_NSEC_		BIT(6)
156ece19502SDivya Koppera 
157ece19502SDivya Koppera #define PTP_CLOCK_SET_SEC_MID			0x0206
158ece19502SDivya Koppera #define PTP_CLOCK_SET_SEC_LO			0x0207
159ece19502SDivya Koppera #define PTP_CLOCK_SET_NS_HI			0x0208
160ece19502SDivya Koppera #define PTP_CLOCK_SET_NS_LO			0x0209
161ece19502SDivya Koppera 
162ece19502SDivya Koppera #define PTP_CLOCK_READ_SEC_MID			0x022A
163ece19502SDivya Koppera #define PTP_CLOCK_READ_SEC_LO			0x022B
164ece19502SDivya Koppera #define PTP_CLOCK_READ_NS_HI			0x022C
165ece19502SDivya Koppera #define PTP_CLOCK_READ_NS_LO			0x022D
166ece19502SDivya Koppera 
167ece19502SDivya Koppera #define PTP_OPERATING_MODE			0x0241
168ece19502SDivya Koppera #define PTP_OPERATING_MODE_STANDALONE_		BIT(0)
169ece19502SDivya Koppera 
170ece19502SDivya Koppera #define PTP_TX_MOD				0x028F
171ece19502SDivya Koppera #define PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_	BIT(12)
172ece19502SDivya Koppera #define PTP_TX_MOD_BAD_UDPV4_CHKSUM_FORCE_FCS_DIS_ BIT(3)
173ece19502SDivya Koppera 
174ece19502SDivya Koppera #define PTP_RX_PARSE_CONFIG			0x0242
175ece19502SDivya Koppera #define PTP_RX_PARSE_CONFIG_LAYER2_EN_		BIT(0)
176ece19502SDivya Koppera #define PTP_RX_PARSE_CONFIG_IPV4_EN_		BIT(1)
177ece19502SDivya Koppera #define PTP_RX_PARSE_CONFIG_IPV6_EN_		BIT(2)
178ece19502SDivya Koppera 
179ece19502SDivya Koppera #define PTP_TX_PARSE_CONFIG			0x0282
180ece19502SDivya Koppera #define PTP_TX_PARSE_CONFIG_LAYER2_EN_		BIT(0)
181ece19502SDivya Koppera #define PTP_TX_PARSE_CONFIG_IPV4_EN_		BIT(1)
182ece19502SDivya Koppera #define PTP_TX_PARSE_CONFIG_IPV6_EN_		BIT(2)
183ece19502SDivya Koppera 
184ece19502SDivya Koppera #define PTP_CLOCK_RATE_ADJ_HI			0x020C
185ece19502SDivya Koppera #define PTP_CLOCK_RATE_ADJ_LO			0x020D
186ece19502SDivya Koppera #define PTP_CLOCK_RATE_ADJ_DIR_			BIT(15)
187ece19502SDivya Koppera 
188ece19502SDivya Koppera #define PTP_LTC_STEP_ADJ_HI			0x0212
189ece19502SDivya Koppera #define PTP_LTC_STEP_ADJ_LO			0x0213
190ece19502SDivya Koppera #define PTP_LTC_STEP_ADJ_DIR_			BIT(15)
191ece19502SDivya Koppera 
192ece19502SDivya Koppera #define LAN8814_INTR_STS_REG			0x0033
193ece19502SDivya Koppera #define LAN8814_INTR_STS_REG_1588_TSU0_		BIT(0)
194ece19502SDivya Koppera #define LAN8814_INTR_STS_REG_1588_TSU1_		BIT(1)
195ece19502SDivya Koppera #define LAN8814_INTR_STS_REG_1588_TSU2_		BIT(2)
196ece19502SDivya Koppera #define LAN8814_INTR_STS_REG_1588_TSU3_		BIT(3)
197ece19502SDivya Koppera 
198ece19502SDivya Koppera #define PTP_CAP_INFO				0x022A
199ece19502SDivya Koppera #define PTP_CAP_INFO_TX_TS_CNT_GET_(reg_val)	(((reg_val) & 0x0f00) >> 8)
200ece19502SDivya Koppera #define PTP_CAP_INFO_RX_TS_CNT_GET_(reg_val)	((reg_val) & 0x000f)
201ece19502SDivya Koppera 
202ece19502SDivya Koppera #define PTP_TX_EGRESS_SEC_HI			0x0296
203ece19502SDivya Koppera #define PTP_TX_EGRESS_SEC_LO			0x0297
204ece19502SDivya Koppera #define PTP_TX_EGRESS_NS_HI			0x0294
205ece19502SDivya Koppera #define PTP_TX_EGRESS_NS_LO			0x0295
206ece19502SDivya Koppera #define PTP_TX_MSG_HEADER2			0x0299
207ece19502SDivya Koppera 
208ece19502SDivya Koppera #define PTP_RX_INGRESS_SEC_HI			0x0256
209ece19502SDivya Koppera #define PTP_RX_INGRESS_SEC_LO			0x0257
210ece19502SDivya Koppera #define PTP_RX_INGRESS_NS_HI			0x0254
211ece19502SDivya Koppera #define PTP_RX_INGRESS_NS_LO			0x0255
212ece19502SDivya Koppera #define PTP_RX_MSG_HEADER2			0x0259
213ece19502SDivya Koppera 
214ece19502SDivya Koppera #define PTP_TSU_INT_EN				0x0200
215ece19502SDivya Koppera #define PTP_TSU_INT_EN_PTP_TX_TS_OVRFL_EN_	BIT(3)
216ece19502SDivya Koppera #define PTP_TSU_INT_EN_PTP_TX_TS_EN_		BIT(2)
217ece19502SDivya Koppera #define PTP_TSU_INT_EN_PTP_RX_TS_OVRFL_EN_	BIT(1)
218ece19502SDivya Koppera #define PTP_TSU_INT_EN_PTP_RX_TS_EN_		BIT(0)
219ece19502SDivya Koppera 
220ece19502SDivya Koppera #define PTP_TSU_INT_STS				0x0201
221ece19502SDivya Koppera #define PTP_TSU_INT_STS_PTP_TX_TS_OVRFL_INT_	BIT(3)
222ece19502SDivya Koppera #define PTP_TSU_INT_STS_PTP_TX_TS_EN_		BIT(2)
223ece19502SDivya Koppera #define PTP_TSU_INT_STS_PTP_RX_TS_OVRFL_INT_	BIT(1)
224ece19502SDivya Koppera #define PTP_TSU_INT_STS_PTP_RX_TS_EN_		BIT(0)
225ece19502SDivya Koppera 
226a516b7f7SDivya Koppera #define LAN8814_LED_CTRL_1			0x0
227a516b7f7SDivya Koppera #define LAN8814_LED_CTRL_1_KSZ9031_LED_MODE_	BIT(6)
228a516b7f7SDivya Koppera 
2295a16778eSJohan Hovold /* PHY Control 1 */
2305a16778eSJohan Hovold #define MII_KSZPHY_CTRL_1			0x1e
231f873f112SOleksij Rempel #define KSZ8081_CTRL1_MDIX_STAT			BIT(4)
2325a16778eSJohan Hovold 
2335a16778eSJohan Hovold /* PHY Control 2 / PHY Control (if no PHY Control 1) */
2345a16778eSJohan Hovold #define MII_KSZPHY_CTRL_2			0x1f
2355a16778eSJohan Hovold #define MII_KSZPHY_CTRL				MII_KSZPHY_CTRL_2
23651f932c4SChoi, David /* bitmap of PHY register to set interrupt mode */
237f873f112SOleksij Rempel #define KSZ8081_CTRL2_HP_MDIX			BIT(15)
238f873f112SOleksij Rempel #define KSZ8081_CTRL2_MDI_MDI_X_SELECT		BIT(14)
239f873f112SOleksij Rempel #define KSZ8081_CTRL2_DISABLE_AUTO_MDIX		BIT(13)
240f873f112SOleksij Rempel #define KSZ8081_CTRL2_FORCE_LINK		BIT(11)
241f873f112SOleksij Rempel #define KSZ8081_CTRL2_POWER_SAVING		BIT(10)
24200aee095SJohan Hovold #define KSZPHY_CTRL_INT_ACTIVE_HIGH		BIT(9)
24363f44b2bSJohan Hovold #define KSZPHY_RMII_REF_CLK_SEL			BIT(7)
24451f932c4SChoi, David 
245954c3967SSean Cross /* Write/read to/from extended registers */
246954c3967SSean Cross #define MII_KSZPHY_EXTREG			0x0b
247954c3967SSean Cross #define KSZPHY_EXTREG_WRITE			0x8000
248954c3967SSean Cross 
249954c3967SSean Cross #define MII_KSZPHY_EXTREG_WRITE			0x0c
250954c3967SSean Cross #define MII_KSZPHY_EXTREG_READ			0x0d
251954c3967SSean Cross 
252954c3967SSean Cross /* Extended registers */
253954c3967SSean Cross #define MII_KSZPHY_CLK_CONTROL_PAD_SKEW		0x104
254954c3967SSean Cross #define MII_KSZPHY_RX_DATA_PAD_SKEW		0x105
255954c3967SSean Cross #define MII_KSZPHY_TX_DATA_PAD_SKEW		0x106
256954c3967SSean Cross 
257954c3967SSean Cross #define PS_TO_REG				200
258ece19502SDivya Koppera #define FIFO_SIZE				8
259954c3967SSean Cross 
260cc755495SHoratiu Vultur /* Delay used to get the second part from the LTC */
261cc755495SHoratiu Vultur #define LAN8841_GET_SEC_LTC_DELAY		(500 * NSEC_PER_MSEC)
262cc755495SHoratiu Vultur 
2632b2427d0SAndrew Lunn struct kszphy_hw_stat {
2642b2427d0SAndrew Lunn 	const char *string;
2652b2427d0SAndrew Lunn 	u8 reg;
2662b2427d0SAndrew Lunn 	u8 bits;
2672b2427d0SAndrew Lunn };
2682b2427d0SAndrew Lunn 
2692b2427d0SAndrew Lunn static struct kszphy_hw_stat kszphy_hw_stats[] = {
2702b2427d0SAndrew Lunn 	{ "phy_receive_errors", 21, 16},
2712b2427d0SAndrew Lunn 	{ "phy_idle_errors", 10, 8 },
2722b2427d0SAndrew Lunn };
2732b2427d0SAndrew Lunn 
274e6a423a8SJohan Hovold struct kszphy_type {
275e6a423a8SJohan Hovold 	u32 led_mode_reg;
276c6f9575cSJohan Hovold 	u16 interrupt_level_mask;
27721b688daSDivya Koppera 	u16 cable_diag_reg;
27821b688daSDivya Koppera 	unsigned long pair_mask;
279a8f1a19dSHoratiu Vultur 	u16 disable_dll_tx_bit;
280a8f1a19dSHoratiu Vultur 	u16 disable_dll_rx_bit;
281a8f1a19dSHoratiu Vultur 	u16 disable_dll_mask;
2820f95903eSJohan Hovold 	bool has_broadcast_disable;
2832b0ba96cSSylvain Rochet 	bool has_nand_tree_disable;
28463f44b2bSJohan Hovold 	bool has_rmii_ref_clk_sel;
285e6a423a8SJohan Hovold };
286e6a423a8SJohan Hovold 
287ece19502SDivya Koppera /* Shared structure between the PHYs of the same package. */
288ece19502SDivya Koppera struct lan8814_shared_priv {
289ece19502SDivya Koppera 	struct phy_device *phydev;
290ece19502SDivya Koppera 	struct ptp_clock *ptp_clock;
291ece19502SDivya Koppera 	struct ptp_clock_info ptp_clock_info;
292ece19502SDivya Koppera 
293ece19502SDivya Koppera 	/* Reference counter to how many ports in the package are enabling the
294ece19502SDivya Koppera 	 * timestamping
295ece19502SDivya Koppera 	 */
296ece19502SDivya Koppera 	u8 ref;
297ece19502SDivya Koppera 
298ece19502SDivya Koppera 	/* Lock for ptp_clock and ref */
299ece19502SDivya Koppera 	struct mutex shared_lock;
300ece19502SDivya Koppera };
301ece19502SDivya Koppera 
302ece19502SDivya Koppera struct lan8814_ptp_rx_ts {
303ece19502SDivya Koppera 	struct list_head list;
304ece19502SDivya Koppera 	u32 seconds;
305ece19502SDivya Koppera 	u32 nsec;
306ece19502SDivya Koppera 	u16 seq_id;
307ece19502SDivya Koppera };
308ece19502SDivya Koppera 
309ece19502SDivya Koppera struct kszphy_ptp_priv {
310ece19502SDivya Koppera 	struct mii_timestamper mii_ts;
311ece19502SDivya Koppera 	struct phy_device *phydev;
312ece19502SDivya Koppera 
313ece19502SDivya Koppera 	struct sk_buff_head tx_queue;
314ece19502SDivya Koppera 	struct sk_buff_head rx_queue;
315ece19502SDivya Koppera 
316ece19502SDivya Koppera 	struct list_head rx_ts_list;
317ece19502SDivya Koppera 	/* Lock for Rx ts fifo */
318ece19502SDivya Koppera 	spinlock_t rx_ts_lock;
319ece19502SDivya Koppera 
320ece19502SDivya Koppera 	int hwts_tx_type;
321ece19502SDivya Koppera 	enum hwtstamp_rx_filters rx_filter;
322ece19502SDivya Koppera 	int layer;
323ece19502SDivya Koppera 	int version;
324cafc3662SHoratiu Vultur 
325cafc3662SHoratiu Vultur 	struct ptp_clock *ptp_clock;
326cafc3662SHoratiu Vultur 	struct ptp_clock_info ptp_clock_info;
327cafc3662SHoratiu Vultur 	/* Lock for ptp_clock */
328cafc3662SHoratiu Vultur 	struct mutex ptp_lock;
329e4ed8ba0SHoratiu Vultur 	struct ptp_pin_desc *pin_config;
330cc755495SHoratiu Vultur 
331cc755495SHoratiu Vultur 	s64 seconds;
332cc755495SHoratiu Vultur 	/* Lock for accessing seconds */
333cc755495SHoratiu Vultur 	spinlock_t seconds_lock;
334ece19502SDivya Koppera };
335ece19502SDivya Koppera 
336e6a423a8SJohan Hovold struct kszphy_priv {
337ece19502SDivya Koppera 	struct kszphy_ptp_priv ptp_priv;
338e6a423a8SJohan Hovold 	const struct kszphy_type *type;
339e7a792e9SJohan Hovold 	int led_mode;
34058389c00SMarek Vasut 	u16 vct_ctrl1000;
34163f44b2bSJohan Hovold 	bool rmii_ref_clk_sel;
34263f44b2bSJohan Hovold 	bool rmii_ref_clk_sel_val;
3432b2427d0SAndrew Lunn 	u64 stats[ARRAY_SIZE(kszphy_hw_stats)];
344e6a423a8SJohan Hovold };
345e6a423a8SJohan Hovold 
346a516b7f7SDivya Koppera static const struct kszphy_type lan8814_type = {
347a516b7f7SDivya Koppera 	.led_mode_reg		= ~LAN8814_LED_CTRL_1,
34821b688daSDivya Koppera 	.cable_diag_reg		= LAN8814_CABLE_DIAG,
34921b688daSDivya Koppera 	.pair_mask		= LAN8814_WIRE_PAIR_MASK,
35021b688daSDivya Koppera };
35121b688daSDivya Koppera 
35221b688daSDivya Koppera static const struct kszphy_type ksz886x_type = {
35321b688daSDivya Koppera 	.cable_diag_reg		= KSZ8081_LMD,
35421b688daSDivya Koppera 	.pair_mask		= KSZPHY_WIRE_PAIR_MASK,
355a516b7f7SDivya Koppera };
356a516b7f7SDivya Koppera 
357e6a423a8SJohan Hovold static const struct kszphy_type ksz8021_type = {
358e6a423a8SJohan Hovold 	.led_mode_reg		= MII_KSZPHY_CTRL_2,
359d0e1df9cSJohan Hovold 	.has_broadcast_disable	= true,
3602b0ba96cSSylvain Rochet 	.has_nand_tree_disable	= true,
36163f44b2bSJohan Hovold 	.has_rmii_ref_clk_sel	= true,
362e6a423a8SJohan Hovold };
363e6a423a8SJohan Hovold 
364e6a423a8SJohan Hovold static const struct kszphy_type ksz8041_type = {
365e6a423a8SJohan Hovold 	.led_mode_reg		= MII_KSZPHY_CTRL_1,
366e6a423a8SJohan Hovold };
367e6a423a8SJohan Hovold 
368e6a423a8SJohan Hovold static const struct kszphy_type ksz8051_type = {
369e6a423a8SJohan Hovold 	.led_mode_reg		= MII_KSZPHY_CTRL_2,
3702b0ba96cSSylvain Rochet 	.has_nand_tree_disable	= true,
371e6a423a8SJohan Hovold };
372e6a423a8SJohan Hovold 
373e6a423a8SJohan Hovold static const struct kszphy_type ksz8081_type = {
374e6a423a8SJohan Hovold 	.led_mode_reg		= MII_KSZPHY_CTRL_2,
3750f95903eSJohan Hovold 	.has_broadcast_disable	= true,
3762b0ba96cSSylvain Rochet 	.has_nand_tree_disable	= true,
37786dc1342SJohan Hovold 	.has_rmii_ref_clk_sel	= true,
378e6a423a8SJohan Hovold };
379e6a423a8SJohan Hovold 
380c6f9575cSJohan Hovold static const struct kszphy_type ks8737_type = {
381c6f9575cSJohan Hovold 	.interrupt_level_mask	= BIT(14),
382c6f9575cSJohan Hovold };
383c6f9575cSJohan Hovold 
384c6f9575cSJohan Hovold static const struct kszphy_type ksz9021_type = {
385c6f9575cSJohan Hovold 	.interrupt_level_mask	= BIT(14),
386c6f9575cSJohan Hovold };
387c6f9575cSJohan Hovold 
388a8f1a19dSHoratiu Vultur static const struct kszphy_type ksz9131_type = {
389a8f1a19dSHoratiu Vultur 	.interrupt_level_mask	= BIT(14),
390a8f1a19dSHoratiu Vultur 	.disable_dll_tx_bit	= BIT(12),
391a8f1a19dSHoratiu Vultur 	.disable_dll_rx_bit	= BIT(12),
392a8f1a19dSHoratiu Vultur 	.disable_dll_mask	= BIT_MASK(12),
393a8f1a19dSHoratiu Vultur };
394a8f1a19dSHoratiu Vultur 
395a8f1a19dSHoratiu Vultur static const struct kszphy_type lan8841_type = {
396a8f1a19dSHoratiu Vultur 	.disable_dll_tx_bit	= BIT(14),
397a8f1a19dSHoratiu Vultur 	.disable_dll_rx_bit	= BIT(14),
398a8f1a19dSHoratiu Vultur 	.disable_dll_mask	= BIT_MASK(14),
399a136391aSHoratiu Vultur 	.cable_diag_reg		= LAN8814_CABLE_DIAG,
400a136391aSHoratiu Vultur 	.pair_mask		= LAN8814_WIRE_PAIR_MASK,
401a8f1a19dSHoratiu Vultur };
402a8f1a19dSHoratiu Vultur 
kszphy_extended_write(struct phy_device * phydev,u32 regnum,u16 val)403954c3967SSean Cross static int kszphy_extended_write(struct phy_device *phydev,
404954c3967SSean Cross 				u32 regnum, u16 val)
405954c3967SSean Cross {
406954c3967SSean Cross 	phy_write(phydev, MII_KSZPHY_EXTREG, KSZPHY_EXTREG_WRITE | regnum);
407954c3967SSean Cross 	return phy_write(phydev, MII_KSZPHY_EXTREG_WRITE, val);
408954c3967SSean Cross }
409954c3967SSean Cross 
kszphy_extended_read(struct phy_device * phydev,u32 regnum)410954c3967SSean Cross static int kszphy_extended_read(struct phy_device *phydev,
411954c3967SSean Cross 				u32 regnum)
412954c3967SSean Cross {
413954c3967SSean Cross 	phy_write(phydev, MII_KSZPHY_EXTREG, regnum);
414954c3967SSean Cross 	return phy_read(phydev, MII_KSZPHY_EXTREG_READ);
415954c3967SSean Cross }
416954c3967SSean Cross 
kszphy_ack_interrupt(struct phy_device * phydev)41751f932c4SChoi, David static int kszphy_ack_interrupt(struct phy_device *phydev)
41851f932c4SChoi, David {
41951f932c4SChoi, David 	/* bit[7..0] int status, which is a read and clear register. */
42051f932c4SChoi, David 	int rc;
42151f932c4SChoi, David 
42251f932c4SChoi, David 	rc = phy_read(phydev, MII_KSZPHY_INTCS);
42351f932c4SChoi, David 
42451f932c4SChoi, David 	return (rc < 0) ? rc : 0;
42551f932c4SChoi, David }
42651f932c4SChoi, David 
kszphy_config_intr(struct phy_device * phydev)42751f932c4SChoi, David static int kszphy_config_intr(struct phy_device *phydev)
42851f932c4SChoi, David {
429c6f9575cSJohan Hovold 	const struct kszphy_type *type = phydev->drv->driver_data;
430c0c99d0cSIoana Ciornei 	int temp, err;
431c6f9575cSJohan Hovold 	u16 mask;
432c6f9575cSJohan Hovold 
433c6f9575cSJohan Hovold 	if (type && type->interrupt_level_mask)
434c6f9575cSJohan Hovold 		mask = type->interrupt_level_mask;
435c6f9575cSJohan Hovold 	else
436c6f9575cSJohan Hovold 		mask = KSZPHY_CTRL_INT_ACTIVE_HIGH;
43751f932c4SChoi, David 
43851f932c4SChoi, David 	/* set the interrupt pin active low */
43951f932c4SChoi, David 	temp = phy_read(phydev, MII_KSZPHY_CTRL);
4405bb8fc0dSJohan Hovold 	if (temp < 0)
4415bb8fc0dSJohan Hovold 		return temp;
442c6f9575cSJohan Hovold 	temp &= ~mask;
44351f932c4SChoi, David 	phy_write(phydev, MII_KSZPHY_CTRL, temp);
44451f932c4SChoi, David 
445c6f9575cSJohan Hovold 	/* enable / disable interrupts */
446c0c99d0cSIoana Ciornei 	if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
447c0c99d0cSIoana Ciornei 		err = kszphy_ack_interrupt(phydev);
448c0c99d0cSIoana Ciornei 		if (err)
449c0c99d0cSIoana Ciornei 			return err;
45051f932c4SChoi, David 
451a57cc54dSWolfram Sang 		err = phy_write(phydev, MII_KSZPHY_INTCS, KSZPHY_INTCS_ALL);
452c0c99d0cSIoana Ciornei 	} else {
453a57cc54dSWolfram Sang 		err = phy_write(phydev, MII_KSZPHY_INTCS, 0);
454c0c99d0cSIoana Ciornei 		if (err)
455c0c99d0cSIoana Ciornei 			return err;
456c0c99d0cSIoana Ciornei 
457c0c99d0cSIoana Ciornei 		err = kszphy_ack_interrupt(phydev);
458c0c99d0cSIoana Ciornei 	}
459c0c99d0cSIoana Ciornei 
460c0c99d0cSIoana Ciornei 	return err;
46151f932c4SChoi, David }
462d0507009SDavid J. Choi 
kszphy_handle_interrupt(struct phy_device * phydev)46359ca4e58SIoana Ciornei static irqreturn_t kszphy_handle_interrupt(struct phy_device *phydev)
46459ca4e58SIoana Ciornei {
46559ca4e58SIoana Ciornei 	int irq_status;
46659ca4e58SIoana Ciornei 
46759ca4e58SIoana Ciornei 	irq_status = phy_read(phydev, MII_KSZPHY_INTCS);
46859ca4e58SIoana Ciornei 	if (irq_status < 0) {
46959ca4e58SIoana Ciornei 		phy_error(phydev);
47059ca4e58SIoana Ciornei 		return IRQ_NONE;
47159ca4e58SIoana Ciornei 	}
47259ca4e58SIoana Ciornei 
473fff4c746SOleksij Rempel 	if (!(irq_status & KSZPHY_INTCS_STATUS))
47459ca4e58SIoana Ciornei 		return IRQ_NONE;
47559ca4e58SIoana Ciornei 
47659ca4e58SIoana Ciornei 	phy_trigger_machine(phydev);
47759ca4e58SIoana Ciornei 
47859ca4e58SIoana Ciornei 	return IRQ_HANDLED;
47959ca4e58SIoana Ciornei }
48059ca4e58SIoana Ciornei 
kszphy_rmii_clk_sel(struct phy_device * phydev,bool val)48163f44b2bSJohan Hovold static int kszphy_rmii_clk_sel(struct phy_device *phydev, bool val)
48263f44b2bSJohan Hovold {
48363f44b2bSJohan Hovold 	int ctrl;
48463f44b2bSJohan Hovold 
48563f44b2bSJohan Hovold 	ctrl = phy_read(phydev, MII_KSZPHY_CTRL);
48663f44b2bSJohan Hovold 	if (ctrl < 0)
48763f44b2bSJohan Hovold 		return ctrl;
48863f44b2bSJohan Hovold 
48963f44b2bSJohan Hovold 	if (val)
49063f44b2bSJohan Hovold 		ctrl |= KSZPHY_RMII_REF_CLK_SEL;
49163f44b2bSJohan Hovold 	else
49263f44b2bSJohan Hovold 		ctrl &= ~KSZPHY_RMII_REF_CLK_SEL;
49363f44b2bSJohan Hovold 
49463f44b2bSJohan Hovold 	return phy_write(phydev, MII_KSZPHY_CTRL, ctrl);
49563f44b2bSJohan Hovold }
49663f44b2bSJohan Hovold 
kszphy_setup_led(struct phy_device * phydev,u32 reg,int val)497e7a792e9SJohan Hovold static int kszphy_setup_led(struct phy_device *phydev, u32 reg, int val)
49820d8435aSBen Dooks {
4995a16778eSJohan Hovold 	int rc, temp, shift;
5008620546cSJohan Hovold 
5015a16778eSJohan Hovold 	switch (reg) {
5025a16778eSJohan Hovold 	case MII_KSZPHY_CTRL_1:
5035a16778eSJohan Hovold 		shift = 14;
5045a16778eSJohan Hovold 		break;
5055a16778eSJohan Hovold 	case MII_KSZPHY_CTRL_2:
5065a16778eSJohan Hovold 		shift = 4;
5075a16778eSJohan Hovold 		break;
5085a16778eSJohan Hovold 	default:
5095a16778eSJohan Hovold 		return -EINVAL;
5105a16778eSJohan Hovold 	}
5115a16778eSJohan Hovold 
51220d8435aSBen Dooks 	temp = phy_read(phydev, reg);
513b7035860SJohan Hovold 	if (temp < 0) {
514b7035860SJohan Hovold 		rc = temp;
515b7035860SJohan Hovold 		goto out;
516b7035860SJohan Hovold 	}
51720d8435aSBen Dooks 
51828bdc499SSergei Shtylyov 	temp &= ~(3 << shift);
51920d8435aSBen Dooks 	temp |= val << shift;
52020d8435aSBen Dooks 	rc = phy_write(phydev, reg, temp);
521b7035860SJohan Hovold out:
522b7035860SJohan Hovold 	if (rc < 0)
52372ba48beSAndrew Lunn 		phydev_err(phydev, "failed to set led mode\n");
52420d8435aSBen Dooks 
525b7035860SJohan Hovold 	return rc;
52620d8435aSBen Dooks }
52720d8435aSBen Dooks 
528bde15129SJohan Hovold /* Disable PHY address 0 as the broadcast address, so that it can be used as a
529bde15129SJohan Hovold  * unique (non-broadcast) address on a shared bus.
530bde15129SJohan Hovold  */
kszphy_broadcast_disable(struct phy_device * phydev)531bde15129SJohan Hovold static int kszphy_broadcast_disable(struct phy_device *phydev)
532bde15129SJohan Hovold {
533bde15129SJohan Hovold 	int ret;
534bde15129SJohan Hovold 
535bde15129SJohan Hovold 	ret = phy_read(phydev, MII_KSZPHY_OMSO);
536bde15129SJohan Hovold 	if (ret < 0)
537bde15129SJohan Hovold 		goto out;
538bde15129SJohan Hovold 
539bde15129SJohan Hovold 	ret = phy_write(phydev, MII_KSZPHY_OMSO, ret | KSZPHY_OMSO_B_CAST_OFF);
540bde15129SJohan Hovold out:
541bde15129SJohan Hovold 	if (ret)
54272ba48beSAndrew Lunn 		phydev_err(phydev, "failed to disable broadcast address\n");
543bde15129SJohan Hovold 
544bde15129SJohan Hovold 	return ret;
545bde15129SJohan Hovold }
546bde15129SJohan Hovold 
kszphy_nand_tree_disable(struct phy_device * phydev)5472b0ba96cSSylvain Rochet static int kszphy_nand_tree_disable(struct phy_device *phydev)
5482b0ba96cSSylvain Rochet {
5492b0ba96cSSylvain Rochet 	int ret;
5502b0ba96cSSylvain Rochet 
5512b0ba96cSSylvain Rochet 	ret = phy_read(phydev, MII_KSZPHY_OMSO);
5522b0ba96cSSylvain Rochet 	if (ret < 0)
5532b0ba96cSSylvain Rochet 		goto out;
5542b0ba96cSSylvain Rochet 
5552b0ba96cSSylvain Rochet 	if (!(ret & KSZPHY_OMSO_NAND_TREE_ON))
5562b0ba96cSSylvain Rochet 		return 0;
5572b0ba96cSSylvain Rochet 
5582b0ba96cSSylvain Rochet 	ret = phy_write(phydev, MII_KSZPHY_OMSO,
5592b0ba96cSSylvain Rochet 			ret & ~KSZPHY_OMSO_NAND_TREE_ON);
5602b0ba96cSSylvain Rochet out:
5612b0ba96cSSylvain Rochet 	if (ret)
56272ba48beSAndrew Lunn 		phydev_err(phydev, "failed to disable NAND tree mode\n");
5632b0ba96cSSylvain Rochet 
5642b0ba96cSSylvain Rochet 	return ret;
5652b0ba96cSSylvain Rochet }
5662b0ba96cSSylvain Rochet 
56779e498a9SLeonard Crestez /* Some config bits need to be set again on resume, handle them here. */
kszphy_config_reset(struct phy_device * phydev)56879e498a9SLeonard Crestez static int kszphy_config_reset(struct phy_device *phydev)
56979e498a9SLeonard Crestez {
57079e498a9SLeonard Crestez 	struct kszphy_priv *priv = phydev->priv;
57179e498a9SLeonard Crestez 	int ret;
57279e498a9SLeonard Crestez 
57379e498a9SLeonard Crestez 	if (priv->rmii_ref_clk_sel) {
57479e498a9SLeonard Crestez 		ret = kszphy_rmii_clk_sel(phydev, priv->rmii_ref_clk_sel_val);
57579e498a9SLeonard Crestez 		if (ret) {
57679e498a9SLeonard Crestez 			phydev_err(phydev,
57779e498a9SLeonard Crestez 				   "failed to set rmii reference clock\n");
57879e498a9SLeonard Crestez 			return ret;
57979e498a9SLeonard Crestez 		}
58079e498a9SLeonard Crestez 	}
58179e498a9SLeonard Crestez 
582f2ef6f75SFabio Estevam 	if (priv->type && priv->led_mode >= 0)
58379e498a9SLeonard Crestez 		kszphy_setup_led(phydev, priv->type->led_mode_reg, priv->led_mode);
58479e498a9SLeonard Crestez 
58579e498a9SLeonard Crestez 	return 0;
58679e498a9SLeonard Crestez }
58779e498a9SLeonard Crestez 
kszphy_config_init(struct phy_device * phydev)588d0507009SDavid J. Choi static int kszphy_config_init(struct phy_device *phydev)
589d0507009SDavid J. Choi {
590e6a423a8SJohan Hovold 	struct kszphy_priv *priv = phydev->priv;
591e6a423a8SJohan Hovold 	const struct kszphy_type *type;
592d0507009SDavid J. Choi 
593e6a423a8SJohan Hovold 	if (!priv)
594e6a423a8SJohan Hovold 		return 0;
595e6a423a8SJohan Hovold 
596e6a423a8SJohan Hovold 	type = priv->type;
597e6a423a8SJohan Hovold 
598f2ef6f75SFabio Estevam 	if (type && type->has_broadcast_disable)
5990f95903eSJohan Hovold 		kszphy_broadcast_disable(phydev);
6000f95903eSJohan Hovold 
601f2ef6f75SFabio Estevam 	if (type && type->has_nand_tree_disable)
6022b0ba96cSSylvain Rochet 		kszphy_nand_tree_disable(phydev);
6032b0ba96cSSylvain Rochet 
60479e498a9SLeonard Crestez 	return kszphy_config_reset(phydev);
60520d8435aSBen Dooks }
60620d8435aSBen Dooks 
ksz8041_fiber_mode(struct phy_device * phydev)6074217a64eSMichael Walle static int ksz8041_fiber_mode(struct phy_device *phydev)
6084217a64eSMichael Walle {
6094217a64eSMichael Walle 	struct device_node *of_node = phydev->mdio.dev.of_node;
6104217a64eSMichael Walle 
6114217a64eSMichael Walle 	return of_property_read_bool(of_node, "micrel,fiber-mode");
6124217a64eSMichael Walle }
6134217a64eSMichael Walle 
ksz8041_config_init(struct phy_device * phydev)61477501a79SPhilipp Zabel static int ksz8041_config_init(struct phy_device *phydev)
61577501a79SPhilipp Zabel {
6163c1bcc86SAndrew Lunn 	__ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
6173c1bcc86SAndrew Lunn 
61877501a79SPhilipp Zabel 	/* Limit supported and advertised modes in fiber mode */
6194217a64eSMichael Walle 	if (ksz8041_fiber_mode(phydev)) {
62077501a79SPhilipp Zabel 		phydev->dev_flags |= MICREL_PHY_FXEN;
6213c1bcc86SAndrew Lunn 		linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, mask);
6223c1bcc86SAndrew Lunn 		linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, mask);
6233c1bcc86SAndrew Lunn 
6243c1bcc86SAndrew Lunn 		linkmode_and(phydev->supported, phydev->supported, mask);
6253c1bcc86SAndrew Lunn 		linkmode_set_bit(ETHTOOL_LINK_MODE_FIBRE_BIT,
6263c1bcc86SAndrew Lunn 				 phydev->supported);
6273c1bcc86SAndrew Lunn 		linkmode_and(phydev->advertising, phydev->advertising, mask);
6283c1bcc86SAndrew Lunn 		linkmode_set_bit(ETHTOOL_LINK_MODE_FIBRE_BIT,
6293c1bcc86SAndrew Lunn 				 phydev->advertising);
63077501a79SPhilipp Zabel 		phydev->autoneg = AUTONEG_DISABLE;
63177501a79SPhilipp Zabel 	}
63277501a79SPhilipp Zabel 
63377501a79SPhilipp Zabel 	return kszphy_config_init(phydev);
63477501a79SPhilipp Zabel }
63577501a79SPhilipp Zabel 
ksz8041_config_aneg(struct phy_device * phydev)63677501a79SPhilipp Zabel static int ksz8041_config_aneg(struct phy_device *phydev)
63777501a79SPhilipp Zabel {
63877501a79SPhilipp Zabel 	/* Skip auto-negotiation in fiber mode */
63977501a79SPhilipp Zabel 	if (phydev->dev_flags & MICREL_PHY_FXEN) {
64077501a79SPhilipp Zabel 		phydev->speed = SPEED_100;
64177501a79SPhilipp Zabel 		return 0;
64277501a79SPhilipp Zabel 	}
64377501a79SPhilipp Zabel 
64477501a79SPhilipp Zabel 	return genphy_config_aneg(phydev);
64577501a79SPhilipp Zabel }
64677501a79SPhilipp Zabel 
ksz8051_ksz8795_match_phy_device(struct phy_device * phydev,const bool ksz_8051)6478b95599cSMarek Vasut static int ksz8051_ksz8795_match_phy_device(struct phy_device *phydev,
648a5e63c7dSSteve Bennett 					    const bool ksz_8051)
6498b95599cSMarek Vasut {
6508b95599cSMarek Vasut 	int ret;
6518b95599cSMarek Vasut 
6524b159f50SRussell King 	if (!phy_id_compare(phydev->phy_id, PHY_ID_KSZ8051, MICREL_PHY_ID_MASK))
6538b95599cSMarek Vasut 		return 0;
6548b95599cSMarek Vasut 
6558b95599cSMarek Vasut 	ret = phy_read(phydev, MII_BMSR);
6568b95599cSMarek Vasut 	if (ret < 0)
6578b95599cSMarek Vasut 		return ret;
6588b95599cSMarek Vasut 
6598b95599cSMarek Vasut 	/* KSZ8051 PHY and KSZ8794/KSZ8795/KSZ8765 switch share the same
6608b95599cSMarek Vasut 	 * exact PHY ID. However, they can be told apart by the extended
6618b95599cSMarek Vasut 	 * capability registers presence. The KSZ8051 PHY has them while
6628b95599cSMarek Vasut 	 * the switch does not.
6638b95599cSMarek Vasut 	 */
6648b95599cSMarek Vasut 	ret &= BMSR_ERCAP;
665a5e63c7dSSteve Bennett 	if (ksz_8051)
6668b95599cSMarek Vasut 		return ret;
6678b95599cSMarek Vasut 	else
6688b95599cSMarek Vasut 		return !ret;
6698b95599cSMarek Vasut }
6708b95599cSMarek Vasut 
ksz8051_match_phy_device(struct phy_device * phydev)6718b95599cSMarek Vasut static int ksz8051_match_phy_device(struct phy_device *phydev)
6728b95599cSMarek Vasut {
673a5e63c7dSSteve Bennett 	return ksz8051_ksz8795_match_phy_device(phydev, true);
6748b95599cSMarek Vasut }
6758b95599cSMarek Vasut 
ksz8081_config_init(struct phy_device * phydev)6767a1d8390SAntoine Tenart static int ksz8081_config_init(struct phy_device *phydev)
6777a1d8390SAntoine Tenart {
6787a1d8390SAntoine Tenart 	/* KSZPHY_OMSO_FACTORY_TEST is set at de-assertion of the reset line
6797a1d8390SAntoine Tenart 	 * based on the RXER (KSZ8081RNA/RND) or TXC (KSZ8081MNX/RNB) pin. If a
6807a1d8390SAntoine Tenart 	 * pull-down is missing, the factory test mode should be cleared by
6817a1d8390SAntoine Tenart 	 * manually writing a 0.
6827a1d8390SAntoine Tenart 	 */
6837a1d8390SAntoine Tenart 	phy_clear_bits(phydev, MII_KSZPHY_OMSO, KSZPHY_OMSO_FACTORY_TEST);
6847a1d8390SAntoine Tenart 
6857a1d8390SAntoine Tenart 	return kszphy_config_init(phydev);
6867a1d8390SAntoine Tenart }
6877a1d8390SAntoine Tenart 
ksz8081_config_mdix(struct phy_device * phydev,u8 ctrl)688f873f112SOleksij Rempel static int ksz8081_config_mdix(struct phy_device *phydev, u8 ctrl)
689f873f112SOleksij Rempel {
690f873f112SOleksij Rempel 	u16 val;
691f873f112SOleksij Rempel 
692f873f112SOleksij Rempel 	switch (ctrl) {
693f873f112SOleksij Rempel 	case ETH_TP_MDI:
694f873f112SOleksij Rempel 		val = KSZ8081_CTRL2_DISABLE_AUTO_MDIX;
695f873f112SOleksij Rempel 		break;
696f873f112SOleksij Rempel 	case ETH_TP_MDI_X:
697f873f112SOleksij Rempel 		val = KSZ8081_CTRL2_DISABLE_AUTO_MDIX |
698f873f112SOleksij Rempel 			KSZ8081_CTRL2_MDI_MDI_X_SELECT;
699f873f112SOleksij Rempel 		break;
700f873f112SOleksij Rempel 	case ETH_TP_MDI_AUTO:
701f873f112SOleksij Rempel 		val = 0;
702f873f112SOleksij Rempel 		break;
703f873f112SOleksij Rempel 	default:
704f873f112SOleksij Rempel 		return 0;
705f873f112SOleksij Rempel 	}
706f873f112SOleksij Rempel 
707f873f112SOleksij Rempel 	return phy_modify(phydev, MII_KSZPHY_CTRL_2,
708f873f112SOleksij Rempel 			  KSZ8081_CTRL2_HP_MDIX |
709f873f112SOleksij Rempel 			  KSZ8081_CTRL2_MDI_MDI_X_SELECT |
710f873f112SOleksij Rempel 			  KSZ8081_CTRL2_DISABLE_AUTO_MDIX,
711f873f112SOleksij Rempel 			  KSZ8081_CTRL2_HP_MDIX | val);
712f873f112SOleksij Rempel }
713f873f112SOleksij Rempel 
ksz8081_config_aneg(struct phy_device * phydev)714f873f112SOleksij Rempel static int ksz8081_config_aneg(struct phy_device *phydev)
715f873f112SOleksij Rempel {
716f873f112SOleksij Rempel 	int ret;
717f873f112SOleksij Rempel 
718f873f112SOleksij Rempel 	ret = genphy_config_aneg(phydev);
719f873f112SOleksij Rempel 	if (ret)
720f873f112SOleksij Rempel 		return ret;
721f873f112SOleksij Rempel 
722f873f112SOleksij Rempel 	/* The MDI-X configuration is automatically changed by the PHY after
723f873f112SOleksij Rempel 	 * switching from autoneg off to on. So, take MDI-X configuration under
724f873f112SOleksij Rempel 	 * own control and set it after autoneg configuration was done.
725f873f112SOleksij Rempel 	 */
726f873f112SOleksij Rempel 	return ksz8081_config_mdix(phydev, phydev->mdix_ctrl);
727f873f112SOleksij Rempel }
728f873f112SOleksij Rempel 
ksz8081_mdix_update(struct phy_device * phydev)729f873f112SOleksij Rempel static int ksz8081_mdix_update(struct phy_device *phydev)
730f873f112SOleksij Rempel {
731f873f112SOleksij Rempel 	int ret;
732f873f112SOleksij Rempel 
733f873f112SOleksij Rempel 	ret = phy_read(phydev, MII_KSZPHY_CTRL_2);
734f873f112SOleksij Rempel 	if (ret < 0)
735f873f112SOleksij Rempel 		return ret;
736f873f112SOleksij Rempel 
737f873f112SOleksij Rempel 	if (ret & KSZ8081_CTRL2_DISABLE_AUTO_MDIX) {
738f873f112SOleksij Rempel 		if (ret & KSZ8081_CTRL2_MDI_MDI_X_SELECT)
739f873f112SOleksij Rempel 			phydev->mdix_ctrl = ETH_TP_MDI_X;
740f873f112SOleksij Rempel 		else
741f873f112SOleksij Rempel 			phydev->mdix_ctrl = ETH_TP_MDI;
742f873f112SOleksij Rempel 	} else {
743f873f112SOleksij Rempel 		phydev->mdix_ctrl = ETH_TP_MDI_AUTO;
744f873f112SOleksij Rempel 	}
745f873f112SOleksij Rempel 
746f873f112SOleksij Rempel 	ret = phy_read(phydev, MII_KSZPHY_CTRL_1);
747f873f112SOleksij Rempel 	if (ret < 0)
748f873f112SOleksij Rempel 		return ret;
749f873f112SOleksij Rempel 
750f873f112SOleksij Rempel 	if (ret & KSZ8081_CTRL1_MDIX_STAT)
751f873f112SOleksij Rempel 		phydev->mdix = ETH_TP_MDI;
752f873f112SOleksij Rempel 	else
753f873f112SOleksij Rempel 		phydev->mdix = ETH_TP_MDI_X;
754f873f112SOleksij Rempel 
755f873f112SOleksij Rempel 	return 0;
756f873f112SOleksij Rempel }
757f873f112SOleksij Rempel 
ksz8081_read_status(struct phy_device * phydev)758f873f112SOleksij Rempel static int ksz8081_read_status(struct phy_device *phydev)
759f873f112SOleksij Rempel {
760f873f112SOleksij Rempel 	int ret;
761f873f112SOleksij Rempel 
762f873f112SOleksij Rempel 	ret = ksz8081_mdix_update(phydev);
763f873f112SOleksij Rempel 	if (ret < 0)
764f873f112SOleksij Rempel 		return ret;
765f873f112SOleksij Rempel 
766f873f112SOleksij Rempel 	return genphy_read_status(phydev);
767f873f112SOleksij Rempel }
768f873f112SOleksij Rempel 
ksz8061_config_init(struct phy_device * phydev)769232ba3a5SRajasingh Thavamani static int ksz8061_config_init(struct phy_device *phydev)
770232ba3a5SRajasingh Thavamani {
771232ba3a5SRajasingh Thavamani 	int ret;
772232ba3a5SRajasingh Thavamani 
773cba54674STristram Ha 	/* Chip can be powered down by the bootstrap code. */
774cba54674STristram Ha 	ret = phy_read(phydev, MII_BMCR);
775cba54674STristram Ha 	if (ret < 0)
776cba54674STristram Ha 		return ret;
777cba54674STristram Ha 	if (ret & BMCR_PDOWN) {
778cba54674STristram Ha 		ret = phy_write(phydev, MII_BMCR, ret & ~BMCR_PDOWN);
779cba54674STristram Ha 		if (ret < 0)
780cba54674STristram Ha 			return ret;
781cba54674STristram Ha 		usleep_range(1000, 2000);
782cba54674STristram Ha 	}
783cba54674STristram Ha 
784232ba3a5SRajasingh Thavamani 	ret = phy_write_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_DEVID1, 0xB61A);
785232ba3a5SRajasingh Thavamani 	if (ret)
786232ba3a5SRajasingh Thavamani 		return ret;
787232ba3a5SRajasingh Thavamani 
788232ba3a5SRajasingh Thavamani 	return kszphy_config_init(phydev);
789232ba3a5SRajasingh Thavamani }
790232ba3a5SRajasingh Thavamani 
ksz8795_match_phy_device(struct phy_device * phydev)7918b95599cSMarek Vasut static int ksz8795_match_phy_device(struct phy_device *phydev)
7928b95599cSMarek Vasut {
793a5e63c7dSSteve Bennett 	return ksz8051_ksz8795_match_phy_device(phydev, false);
7948b95599cSMarek Vasut }
7958b95599cSMarek Vasut 
ksz9021_load_values_from_of(struct phy_device * phydev,const struct device_node * of_node,u16 reg,const char * field1,const char * field2,const char * field3,const char * field4)796954c3967SSean Cross static int ksz9021_load_values_from_of(struct phy_device *phydev,
7973c9a9f7fSJaeden Amero 				       const struct device_node *of_node,
7983c9a9f7fSJaeden Amero 				       u16 reg,
7993c9a9f7fSJaeden Amero 				       const char *field1, const char *field2,
8003c9a9f7fSJaeden Amero 				       const char *field3, const char *field4)
801954c3967SSean Cross {
802954c3967SSean Cross 	int val1 = -1;
803954c3967SSean Cross 	int val2 = -2;
804954c3967SSean Cross 	int val3 = -3;
805954c3967SSean Cross 	int val4 = -4;
806954c3967SSean Cross 	int newval;
807954c3967SSean Cross 	int matches = 0;
808954c3967SSean Cross 
809954c3967SSean Cross 	if (!of_property_read_u32(of_node, field1, &val1))
810954c3967SSean Cross 		matches++;
811954c3967SSean Cross 
812954c3967SSean Cross 	if (!of_property_read_u32(of_node, field2, &val2))
813954c3967SSean Cross 		matches++;
814954c3967SSean Cross 
815954c3967SSean Cross 	if (!of_property_read_u32(of_node, field3, &val3))
816954c3967SSean Cross 		matches++;
817954c3967SSean Cross 
818954c3967SSean Cross 	if (!of_property_read_u32(of_node, field4, &val4))
819954c3967SSean Cross 		matches++;
820954c3967SSean Cross 
821954c3967SSean Cross 	if (!matches)
822954c3967SSean Cross 		return 0;
823954c3967SSean Cross 
824954c3967SSean Cross 	if (matches < 4)
825954c3967SSean Cross 		newval = kszphy_extended_read(phydev, reg);
826954c3967SSean Cross 	else
827954c3967SSean Cross 		newval = 0;
828954c3967SSean Cross 
829954c3967SSean Cross 	if (val1 != -1)
830954c3967SSean Cross 		newval = ((newval & 0xfff0) | ((val1 / PS_TO_REG) & 0xf) << 0);
831954c3967SSean Cross 
8326a119745SHubert Chaumette 	if (val2 != -2)
833954c3967SSean Cross 		newval = ((newval & 0xff0f) | ((val2 / PS_TO_REG) & 0xf) << 4);
834954c3967SSean Cross 
8356a119745SHubert Chaumette 	if (val3 != -3)
836954c3967SSean Cross 		newval = ((newval & 0xf0ff) | ((val3 / PS_TO_REG) & 0xf) << 8);
837954c3967SSean Cross 
8386a119745SHubert Chaumette 	if (val4 != -4)
839954c3967SSean Cross 		newval = ((newval & 0x0fff) | ((val4 / PS_TO_REG) & 0xf) << 12);
840954c3967SSean Cross 
841954c3967SSean Cross 	return kszphy_extended_write(phydev, reg, newval);
842954c3967SSean Cross }
843954c3967SSean Cross 
ksz9021_config_init(struct phy_device * phydev)844954c3967SSean Cross static int ksz9021_config_init(struct phy_device *phydev)
845954c3967SSean Cross {
846ce4f8afdSColin Ian King 	const struct device_node *of_node;
847651df218SAndrew Lunn 	const struct device *dev_walker;
848954c3967SSean Cross 
849651df218SAndrew Lunn 	/* The Micrel driver has a deprecated option to place phy OF
850651df218SAndrew Lunn 	 * properties in the MAC node. Walk up the tree of devices to
851651df218SAndrew Lunn 	 * find a device with an OF node.
852651df218SAndrew Lunn 	 */
853e5a03bfdSAndrew Lunn 	dev_walker = &phydev->mdio.dev;
854651df218SAndrew Lunn 	do {
855651df218SAndrew Lunn 		of_node = dev_walker->of_node;
856651df218SAndrew Lunn 		dev_walker = dev_walker->parent;
857651df218SAndrew Lunn 
858651df218SAndrew Lunn 	} while (!of_node && dev_walker);
859954c3967SSean Cross 
860954c3967SSean Cross 	if (of_node) {
861954c3967SSean Cross 		ksz9021_load_values_from_of(phydev, of_node,
862954c3967SSean Cross 				    MII_KSZPHY_CLK_CONTROL_PAD_SKEW,
863954c3967SSean Cross 				    "txen-skew-ps", "txc-skew-ps",
864954c3967SSean Cross 				    "rxdv-skew-ps", "rxc-skew-ps");
865954c3967SSean Cross 		ksz9021_load_values_from_of(phydev, of_node,
866954c3967SSean Cross 				    MII_KSZPHY_RX_DATA_PAD_SKEW,
867954c3967SSean Cross 				    "rxd0-skew-ps", "rxd1-skew-ps",
868954c3967SSean Cross 				    "rxd2-skew-ps", "rxd3-skew-ps");
869954c3967SSean Cross 		ksz9021_load_values_from_of(phydev, of_node,
870954c3967SSean Cross 				    MII_KSZPHY_TX_DATA_PAD_SKEW,
871954c3967SSean Cross 				    "txd0-skew-ps", "txd1-skew-ps",
872954c3967SSean Cross 				    "txd2-skew-ps", "txd3-skew-ps");
873954c3967SSean Cross 	}
874954c3967SSean Cross 	return 0;
875954c3967SSean Cross }
876954c3967SSean Cross 
8776e4b8273SHubert Chaumette #define KSZ9031_PS_TO_REG		60
8786e4b8273SHubert Chaumette 
8796e4b8273SHubert Chaumette /* Extended registers */
8806270e1aeSJaeden Amero /* MMD Address 0x0 */
8816270e1aeSJaeden Amero #define MII_KSZ9031RN_FLP_BURST_TX_LO	3
8826270e1aeSJaeden Amero #define MII_KSZ9031RN_FLP_BURST_TX_HI	4
8836270e1aeSJaeden Amero 
884ae6c97bbSJaeden Amero /* MMD Address 0x2 */
8856e4b8273SHubert Chaumette #define MII_KSZ9031RN_CONTROL_PAD_SKEW	4
886bcf3440cSOleksij Rempel #define MII_KSZ9031RN_RX_CTL_M		GENMASK(7, 4)
887bcf3440cSOleksij Rempel #define MII_KSZ9031RN_TX_CTL_M		GENMASK(3, 0)
888bcf3440cSOleksij Rempel 
8896e4b8273SHubert Chaumette #define MII_KSZ9031RN_RX_DATA_PAD_SKEW	5
890bcf3440cSOleksij Rempel #define MII_KSZ9031RN_RXD3		GENMASK(15, 12)
891bcf3440cSOleksij Rempel #define MII_KSZ9031RN_RXD2		GENMASK(11, 8)
892bcf3440cSOleksij Rempel #define MII_KSZ9031RN_RXD1		GENMASK(7, 4)
893bcf3440cSOleksij Rempel #define MII_KSZ9031RN_RXD0		GENMASK(3, 0)
894bcf3440cSOleksij Rempel 
8956e4b8273SHubert Chaumette #define MII_KSZ9031RN_TX_DATA_PAD_SKEW	6
896bcf3440cSOleksij Rempel #define MII_KSZ9031RN_TXD3		GENMASK(15, 12)
897bcf3440cSOleksij Rempel #define MII_KSZ9031RN_TXD2		GENMASK(11, 8)
898bcf3440cSOleksij Rempel #define MII_KSZ9031RN_TXD1		GENMASK(7, 4)
899bcf3440cSOleksij Rempel #define MII_KSZ9031RN_TXD0		GENMASK(3, 0)
900bcf3440cSOleksij Rempel 
9016e4b8273SHubert Chaumette #define MII_KSZ9031RN_CLK_PAD_SKEW	8
902bcf3440cSOleksij Rempel #define MII_KSZ9031RN_GTX_CLK		GENMASK(9, 5)
903bcf3440cSOleksij Rempel #define MII_KSZ9031RN_RX_CLK		GENMASK(4, 0)
904bcf3440cSOleksij Rempel 
905bcf3440cSOleksij Rempel /* KSZ9031 has internal RGMII_IDRX = 1.2ns and RGMII_IDTX = 0ns. To
906bcf3440cSOleksij Rempel  * provide different RGMII options we need to configure delay offset
907bcf3440cSOleksij Rempel  * for each pad relative to build in delay.
908bcf3440cSOleksij Rempel  */
909bcf3440cSOleksij Rempel /* keep rx as "No delay adjustment" and set rx_clk to +0.60ns to get delays of
910bcf3440cSOleksij Rempel  * 1.80ns
911bcf3440cSOleksij Rempel  */
912bcf3440cSOleksij Rempel #define RX_ID				0x7
913bcf3440cSOleksij Rempel #define RX_CLK_ID			0x19
914bcf3440cSOleksij Rempel 
915bcf3440cSOleksij Rempel /* set rx to +0.30ns and rx_clk to -0.90ns to compensate the
916bcf3440cSOleksij Rempel  * internal 1.2ns delay.
917bcf3440cSOleksij Rempel  */
918bcf3440cSOleksij Rempel #define RX_ND				0xc
919bcf3440cSOleksij Rempel #define RX_CLK_ND			0x0
920bcf3440cSOleksij Rempel 
921bcf3440cSOleksij Rempel /* set tx to -0.42ns and tx_clk to +0.96ns to get 1.38ns delay */
922bcf3440cSOleksij Rempel #define TX_ID				0x0
923bcf3440cSOleksij Rempel #define TX_CLK_ID			0x1f
924bcf3440cSOleksij Rempel 
925bcf3440cSOleksij Rempel /* set tx and tx_clk to "No delay adjustment" to keep 0ns
926bcf3440cSOleksij Rempel  * dealy
927bcf3440cSOleksij Rempel  */
928bcf3440cSOleksij Rempel #define TX_ND				0x7
929bcf3440cSOleksij Rempel #define TX_CLK_ND			0xf
9306e4b8273SHubert Chaumette 
931af70c1f9SMike Looijmans /* MMD Address 0x1C */
932af70c1f9SMike Looijmans #define MII_KSZ9031RN_EDPD		0x23
933af70c1f9SMike Looijmans #define MII_KSZ9031RN_EDPD_ENABLE	BIT(0)
934af70c1f9SMike Looijmans 
ksz9031_of_load_skew_values(struct phy_device * phydev,const struct device_node * of_node,u16 reg,size_t field_sz,const char * field[],u8 numfields,bool * update)9356e4b8273SHubert Chaumette static int ksz9031_of_load_skew_values(struct phy_device *phydev,
9363c9a9f7fSJaeden Amero 				       const struct device_node *of_node,
9376e4b8273SHubert Chaumette 				       u16 reg, size_t field_sz,
938bcf3440cSOleksij Rempel 				       const char *field[], u8 numfields,
939bcf3440cSOleksij Rempel 				       bool *update)
9406e4b8273SHubert Chaumette {
9416e4b8273SHubert Chaumette 	int val[4] = {-1, -2, -3, -4};
9426e4b8273SHubert Chaumette 	int matches = 0;
9436e4b8273SHubert Chaumette 	u16 mask;
9446e4b8273SHubert Chaumette 	u16 maxval;
9456e4b8273SHubert Chaumette 	u16 newval;
9466e4b8273SHubert Chaumette 	int i;
9476e4b8273SHubert Chaumette 
9486e4b8273SHubert Chaumette 	for (i = 0; i < numfields; i++)
9496e4b8273SHubert Chaumette 		if (!of_property_read_u32(of_node, field[i], val + i))
9506e4b8273SHubert Chaumette 			matches++;
9516e4b8273SHubert Chaumette 
9526e4b8273SHubert Chaumette 	if (!matches)
9536e4b8273SHubert Chaumette 		return 0;
9546e4b8273SHubert Chaumette 
955bcf3440cSOleksij Rempel 	*update |= true;
956bcf3440cSOleksij Rempel 
9576e4b8273SHubert Chaumette 	if (matches < numfields)
9589b420effSHeiner Kallweit 		newval = phy_read_mmd(phydev, 2, reg);
9596e4b8273SHubert Chaumette 	else
9606e4b8273SHubert Chaumette 		newval = 0;
9616e4b8273SHubert Chaumette 
9626e4b8273SHubert Chaumette 	maxval = (field_sz == 4) ? 0xf : 0x1f;
9636e4b8273SHubert Chaumette 	for (i = 0; i < numfields; i++)
9646e4b8273SHubert Chaumette 		if (val[i] != -(i + 1)) {
9656e4b8273SHubert Chaumette 			mask = 0xffff;
9666e4b8273SHubert Chaumette 			mask ^= maxval << (field_sz * i);
9676e4b8273SHubert Chaumette 			newval = (newval & mask) |
9686e4b8273SHubert Chaumette 				(((val[i] / KSZ9031_PS_TO_REG) & maxval)
9696e4b8273SHubert Chaumette 					<< (field_sz * i));
9706e4b8273SHubert Chaumette 		}
9716e4b8273SHubert Chaumette 
9729b420effSHeiner Kallweit 	return phy_write_mmd(phydev, 2, reg, newval);
9736e4b8273SHubert Chaumette }
9746e4b8273SHubert Chaumette 
975a0da456bSMax Uvarov /* Center KSZ9031RNX FLP timing at 16ms. */
ksz9031_center_flp_timing(struct phy_device * phydev)9766270e1aeSJaeden Amero static int ksz9031_center_flp_timing(struct phy_device *phydev)
9776270e1aeSJaeden Amero {
9786270e1aeSJaeden Amero 	int result;
9796270e1aeSJaeden Amero 
9809b420effSHeiner Kallweit 	result = phy_write_mmd(phydev, 0, MII_KSZ9031RN_FLP_BURST_TX_HI,
9819b420effSHeiner Kallweit 			       0x0006);
982a0da456bSMax Uvarov 	if (result)
983a0da456bSMax Uvarov 		return result;
984a0da456bSMax Uvarov 
9859b420effSHeiner Kallweit 	result = phy_write_mmd(phydev, 0, MII_KSZ9031RN_FLP_BURST_TX_LO,
9869b420effSHeiner Kallweit 			       0x1A80);
9876270e1aeSJaeden Amero 	if (result)
9886270e1aeSJaeden Amero 		return result;
9896270e1aeSJaeden Amero 
9906270e1aeSJaeden Amero 	return genphy_restart_aneg(phydev);
9916270e1aeSJaeden Amero }
9926270e1aeSJaeden Amero 
993af70c1f9SMike Looijmans /* Enable energy-detect power-down mode */
ksz9031_enable_edpd(struct phy_device * phydev)994af70c1f9SMike Looijmans static int ksz9031_enable_edpd(struct phy_device *phydev)
995af70c1f9SMike Looijmans {
996af70c1f9SMike Looijmans 	int reg;
997af70c1f9SMike Looijmans 
9989b420effSHeiner Kallweit 	reg = phy_read_mmd(phydev, 0x1C, MII_KSZ9031RN_EDPD);
999af70c1f9SMike Looijmans 	if (reg < 0)
1000af70c1f9SMike Looijmans 		return reg;
10019b420effSHeiner Kallweit 	return phy_write_mmd(phydev, 0x1C, MII_KSZ9031RN_EDPD,
1002af70c1f9SMike Looijmans 			     reg | MII_KSZ9031RN_EDPD_ENABLE);
1003af70c1f9SMike Looijmans }
1004af70c1f9SMike Looijmans 
ksz9031_config_rgmii_delay(struct phy_device * phydev)1005bcf3440cSOleksij Rempel static int ksz9031_config_rgmii_delay(struct phy_device *phydev)
1006bcf3440cSOleksij Rempel {
1007bcf3440cSOleksij Rempel 	u16 rx, tx, rx_clk, tx_clk;
1008bcf3440cSOleksij Rempel 	int ret;
1009bcf3440cSOleksij Rempel 
1010bcf3440cSOleksij Rempel 	switch (phydev->interface) {
1011bcf3440cSOleksij Rempel 	case PHY_INTERFACE_MODE_RGMII:
1012bcf3440cSOleksij Rempel 		tx = TX_ND;
1013bcf3440cSOleksij Rempel 		tx_clk = TX_CLK_ND;
1014bcf3440cSOleksij Rempel 		rx = RX_ND;
1015bcf3440cSOleksij Rempel 		rx_clk = RX_CLK_ND;
1016bcf3440cSOleksij Rempel 		break;
1017bcf3440cSOleksij Rempel 	case PHY_INTERFACE_MODE_RGMII_ID:
1018bcf3440cSOleksij Rempel 		tx = TX_ID;
1019bcf3440cSOleksij Rempel 		tx_clk = TX_CLK_ID;
1020bcf3440cSOleksij Rempel 		rx = RX_ID;
1021bcf3440cSOleksij Rempel 		rx_clk = RX_CLK_ID;
1022bcf3440cSOleksij Rempel 		break;
1023bcf3440cSOleksij Rempel 	case PHY_INTERFACE_MODE_RGMII_RXID:
1024bcf3440cSOleksij Rempel 		tx = TX_ND;
1025bcf3440cSOleksij Rempel 		tx_clk = TX_CLK_ND;
1026bcf3440cSOleksij Rempel 		rx = RX_ID;
1027bcf3440cSOleksij Rempel 		rx_clk = RX_CLK_ID;
1028bcf3440cSOleksij Rempel 		break;
1029bcf3440cSOleksij Rempel 	case PHY_INTERFACE_MODE_RGMII_TXID:
1030bcf3440cSOleksij Rempel 		tx = TX_ID;
1031bcf3440cSOleksij Rempel 		tx_clk = TX_CLK_ID;
1032bcf3440cSOleksij Rempel 		rx = RX_ND;
1033bcf3440cSOleksij Rempel 		rx_clk = RX_CLK_ND;
1034bcf3440cSOleksij Rempel 		break;
1035bcf3440cSOleksij Rempel 	default:
1036bcf3440cSOleksij Rempel 		return 0;
1037bcf3440cSOleksij Rempel 	}
1038bcf3440cSOleksij Rempel 
1039bcf3440cSOleksij Rempel 	ret = phy_write_mmd(phydev, 2, MII_KSZ9031RN_CONTROL_PAD_SKEW,
1040bcf3440cSOleksij Rempel 			    FIELD_PREP(MII_KSZ9031RN_RX_CTL_M, rx) |
1041bcf3440cSOleksij Rempel 			    FIELD_PREP(MII_KSZ9031RN_TX_CTL_M, tx));
1042bcf3440cSOleksij Rempel 	if (ret < 0)
1043bcf3440cSOleksij Rempel 		return ret;
1044bcf3440cSOleksij Rempel 
1045bcf3440cSOleksij Rempel 	ret = phy_write_mmd(phydev, 2, MII_KSZ9031RN_RX_DATA_PAD_SKEW,
1046bcf3440cSOleksij Rempel 			    FIELD_PREP(MII_KSZ9031RN_RXD3, rx) |
1047bcf3440cSOleksij Rempel 			    FIELD_PREP(MII_KSZ9031RN_RXD2, rx) |
1048bcf3440cSOleksij Rempel 			    FIELD_PREP(MII_KSZ9031RN_RXD1, rx) |
1049bcf3440cSOleksij Rempel 			    FIELD_PREP(MII_KSZ9031RN_RXD0, rx));
1050bcf3440cSOleksij Rempel 	if (ret < 0)
1051bcf3440cSOleksij Rempel 		return ret;
1052bcf3440cSOleksij Rempel 
1053bcf3440cSOleksij Rempel 	ret = phy_write_mmd(phydev, 2, MII_KSZ9031RN_TX_DATA_PAD_SKEW,
1054bcf3440cSOleksij Rempel 			    FIELD_PREP(MII_KSZ9031RN_TXD3, tx) |
1055bcf3440cSOleksij Rempel 			    FIELD_PREP(MII_KSZ9031RN_TXD2, tx) |
1056bcf3440cSOleksij Rempel 			    FIELD_PREP(MII_KSZ9031RN_TXD1, tx) |
1057bcf3440cSOleksij Rempel 			    FIELD_PREP(MII_KSZ9031RN_TXD0, tx));
1058bcf3440cSOleksij Rempel 	if (ret < 0)
1059bcf3440cSOleksij Rempel 		return ret;
1060bcf3440cSOleksij Rempel 
1061bcf3440cSOleksij Rempel 	return phy_write_mmd(phydev, 2, MII_KSZ9031RN_CLK_PAD_SKEW,
1062bcf3440cSOleksij Rempel 			     FIELD_PREP(MII_KSZ9031RN_GTX_CLK, tx_clk) |
1063bcf3440cSOleksij Rempel 			     FIELD_PREP(MII_KSZ9031RN_RX_CLK, rx_clk));
1064bcf3440cSOleksij Rempel }
1065bcf3440cSOleksij Rempel 
ksz9031_config_init(struct phy_device * phydev)10666e4b8273SHubert Chaumette static int ksz9031_config_init(struct phy_device *phydev)
10676e4b8273SHubert Chaumette {
1068ce4f8afdSColin Ian King 	const struct device_node *of_node;
10693c9a9f7fSJaeden Amero 	static const char *clk_skews[2] = {"rxc-skew-ps", "txc-skew-ps"};
10703c9a9f7fSJaeden Amero 	static const char *rx_data_skews[4] = {
10716e4b8273SHubert Chaumette 		"rxd0-skew-ps", "rxd1-skew-ps",
10726e4b8273SHubert Chaumette 		"rxd2-skew-ps", "rxd3-skew-ps"
10736e4b8273SHubert Chaumette 	};
10743c9a9f7fSJaeden Amero 	static const char *tx_data_skews[4] = {
10756e4b8273SHubert Chaumette 		"txd0-skew-ps", "txd1-skew-ps",
10766e4b8273SHubert Chaumette 		"txd2-skew-ps", "txd3-skew-ps"
10776e4b8273SHubert Chaumette 	};
10783c9a9f7fSJaeden Amero 	static const char *control_skews[2] = {"txen-skew-ps", "rxdv-skew-ps"};
1079b4c19f71SRoosen Henri 	const struct device *dev_walker;
1080af70c1f9SMike Looijmans 	int result;
1081af70c1f9SMike Looijmans 
1082af70c1f9SMike Looijmans 	result = ksz9031_enable_edpd(phydev);
1083af70c1f9SMike Looijmans 	if (result < 0)
1084af70c1f9SMike Looijmans 		return result;
10856e4b8273SHubert Chaumette 
1086b4c19f71SRoosen Henri 	/* The Micrel driver has a deprecated option to place phy OF
1087b4c19f71SRoosen Henri 	 * properties in the MAC node. Walk up the tree of devices to
1088b4c19f71SRoosen Henri 	 * find a device with an OF node.
1089b4c19f71SRoosen Henri 	 */
10909d367eddSDavid S. Miller 	dev_walker = &phydev->mdio.dev;
1091b4c19f71SRoosen Henri 	do {
1092b4c19f71SRoosen Henri 		of_node = dev_walker->of_node;
1093b4c19f71SRoosen Henri 		dev_walker = dev_walker->parent;
1094b4c19f71SRoosen Henri 	} while (!of_node && dev_walker);
10956e4b8273SHubert Chaumette 
10966e4b8273SHubert Chaumette 	if (of_node) {
1097bcf3440cSOleksij Rempel 		bool update = false;
1098bcf3440cSOleksij Rempel 
1099bcf3440cSOleksij Rempel 		if (phy_interface_is_rgmii(phydev)) {
1100bcf3440cSOleksij Rempel 			result = ksz9031_config_rgmii_delay(phydev);
1101bcf3440cSOleksij Rempel 			if (result < 0)
1102bcf3440cSOleksij Rempel 				return result;
1103bcf3440cSOleksij Rempel 		}
1104bcf3440cSOleksij Rempel 
11056e4b8273SHubert Chaumette 		ksz9031_of_load_skew_values(phydev, of_node,
11066e4b8273SHubert Chaumette 				MII_KSZ9031RN_CLK_PAD_SKEW, 5,
1107bcf3440cSOleksij Rempel 				clk_skews, 2, &update);
11086e4b8273SHubert Chaumette 
11096e4b8273SHubert Chaumette 		ksz9031_of_load_skew_values(phydev, of_node,
11106e4b8273SHubert Chaumette 				MII_KSZ9031RN_CONTROL_PAD_SKEW, 4,
1111bcf3440cSOleksij Rempel 				control_skews, 2, &update);
11126e4b8273SHubert Chaumette 
11136e4b8273SHubert Chaumette 		ksz9031_of_load_skew_values(phydev, of_node,
11146e4b8273SHubert Chaumette 				MII_KSZ9031RN_RX_DATA_PAD_SKEW, 4,
1115bcf3440cSOleksij Rempel 				rx_data_skews, 4, &update);
11166e4b8273SHubert Chaumette 
11176e4b8273SHubert Chaumette 		ksz9031_of_load_skew_values(phydev, of_node,
11186e4b8273SHubert Chaumette 				MII_KSZ9031RN_TX_DATA_PAD_SKEW, 4,
1119bcf3440cSOleksij Rempel 				tx_data_skews, 4, &update);
1120bcf3440cSOleksij Rempel 
112167ca5159SMatthias Schiffer 		if (update && !phy_interface_is_rgmii(phydev))
1122bcf3440cSOleksij Rempel 			phydev_warn(phydev,
112367ca5159SMatthias Schiffer 				    "*-skew-ps values should be used only with RGMII PHY modes\n");
1124e1b505a6SMarkus Niebel 
1125e1b505a6SMarkus Niebel 		/* Silicon Errata Sheet (DS80000691D or DS80000692D):
1126e1b505a6SMarkus Niebel 		 * When the device links in the 1000BASE-T slave mode only,
1127e1b505a6SMarkus Niebel 		 * the optional 125MHz reference output clock (CLK125_NDO)
1128e1b505a6SMarkus Niebel 		 * has wide duty cycle variation.
1129e1b505a6SMarkus Niebel 		 *
1130e1b505a6SMarkus Niebel 		 * The optional CLK125_NDO clock does not meet the RGMII
1131e1b505a6SMarkus Niebel 		 * 45/55 percent (min/max) duty cycle requirement and therefore
1132e1b505a6SMarkus Niebel 		 * cannot be used directly by the MAC side for clocking
1133e1b505a6SMarkus Niebel 		 * applications that have setup/hold time requirements on
1134e1b505a6SMarkus Niebel 		 * rising and falling clock edges.
1135e1b505a6SMarkus Niebel 		 *
1136e1b505a6SMarkus Niebel 		 * Workaround:
1137e1b505a6SMarkus Niebel 		 * Force the phy to be the master to receive a stable clock
1138e1b505a6SMarkus Niebel 		 * which meets the duty cycle requirement.
1139e1b505a6SMarkus Niebel 		 */
1140e1b505a6SMarkus Niebel 		if (of_property_read_bool(of_node, "micrel,force-master")) {
1141e1b505a6SMarkus Niebel 			result = phy_read(phydev, MII_CTRL1000);
1142e1b505a6SMarkus Niebel 			if (result < 0)
1143e1b505a6SMarkus Niebel 				goto err_force_master;
1144e1b505a6SMarkus Niebel 
1145e1b505a6SMarkus Niebel 			/* enable master mode, config & prefer master */
1146e1b505a6SMarkus Niebel 			result |= CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER;
1147e1b505a6SMarkus Niebel 			result = phy_write(phydev, MII_CTRL1000, result);
1148e1b505a6SMarkus Niebel 			if (result < 0)
1149e1b505a6SMarkus Niebel 				goto err_force_master;
1150e1b505a6SMarkus Niebel 		}
11516e4b8273SHubert Chaumette 	}
11526270e1aeSJaeden Amero 
11536270e1aeSJaeden Amero 	return ksz9031_center_flp_timing(phydev);
1154e1b505a6SMarkus Niebel 
1155e1b505a6SMarkus Niebel err_force_master:
1156e1b505a6SMarkus Niebel 	phydev_err(phydev, "failed to force the phy to master mode\n");
1157e1b505a6SMarkus Niebel 	return result;
11586e4b8273SHubert Chaumette }
11596e4b8273SHubert Chaumette 
1160bff5b4b3SYuiko Oshino #define KSZ9131_SKEW_5BIT_MAX	2400
1161bff5b4b3SYuiko Oshino #define KSZ9131_SKEW_4BIT_MAX	800
1162bff5b4b3SYuiko Oshino #define KSZ9131_OFFSET		700
1163bff5b4b3SYuiko Oshino #define KSZ9131_STEP		100
1164bff5b4b3SYuiko Oshino 
ksz9131_of_load_skew_values(struct phy_device * phydev,struct device_node * of_node,u16 reg,size_t field_sz,char * field[],u8 numfields)1165bff5b4b3SYuiko Oshino static int ksz9131_of_load_skew_values(struct phy_device *phydev,
1166bff5b4b3SYuiko Oshino 				       struct device_node *of_node,
1167bff5b4b3SYuiko Oshino 				       u16 reg, size_t field_sz,
1168bff5b4b3SYuiko Oshino 				       char *field[], u8 numfields)
1169bff5b4b3SYuiko Oshino {
1170bff5b4b3SYuiko Oshino 	int val[4] = {-(1 + KSZ9131_OFFSET), -(2 + KSZ9131_OFFSET),
1171bff5b4b3SYuiko Oshino 		      -(3 + KSZ9131_OFFSET), -(4 + KSZ9131_OFFSET)};
1172bff5b4b3SYuiko Oshino 	int skewval, skewmax = 0;
1173bff5b4b3SYuiko Oshino 	int matches = 0;
1174bff5b4b3SYuiko Oshino 	u16 maxval;
1175bff5b4b3SYuiko Oshino 	u16 newval;
1176bff5b4b3SYuiko Oshino 	u16 mask;
1177bff5b4b3SYuiko Oshino 	int i;
1178bff5b4b3SYuiko Oshino 
1179bff5b4b3SYuiko Oshino 	/* psec properties in dts should mean x pico seconds */
1180bff5b4b3SYuiko Oshino 	if (field_sz == 5)
1181bff5b4b3SYuiko Oshino 		skewmax = KSZ9131_SKEW_5BIT_MAX;
1182bff5b4b3SYuiko Oshino 	else
1183bff5b4b3SYuiko Oshino 		skewmax = KSZ9131_SKEW_4BIT_MAX;
1184bff5b4b3SYuiko Oshino 
1185bff5b4b3SYuiko Oshino 	for (i = 0; i < numfields; i++)
1186bff5b4b3SYuiko Oshino 		if (!of_property_read_s32(of_node, field[i], &skewval)) {
1187bff5b4b3SYuiko Oshino 			if (skewval < -KSZ9131_OFFSET)
1188bff5b4b3SYuiko Oshino 				skewval = -KSZ9131_OFFSET;
1189bff5b4b3SYuiko Oshino 			else if (skewval > skewmax)
1190bff5b4b3SYuiko Oshino 				skewval = skewmax;
1191bff5b4b3SYuiko Oshino 
1192bff5b4b3SYuiko Oshino 			val[i] = skewval + KSZ9131_OFFSET;
1193bff5b4b3SYuiko Oshino 			matches++;
1194bff5b4b3SYuiko Oshino 		}
1195bff5b4b3SYuiko Oshino 
1196bff5b4b3SYuiko Oshino 	if (!matches)
1197bff5b4b3SYuiko Oshino 		return 0;
1198bff5b4b3SYuiko Oshino 
1199bff5b4b3SYuiko Oshino 	if (matches < numfields)
12009b420effSHeiner Kallweit 		newval = phy_read_mmd(phydev, 2, reg);
1201bff5b4b3SYuiko Oshino 	else
1202bff5b4b3SYuiko Oshino 		newval = 0;
1203bff5b4b3SYuiko Oshino 
1204bff5b4b3SYuiko Oshino 	maxval = (field_sz == 4) ? 0xf : 0x1f;
1205bff5b4b3SYuiko Oshino 	for (i = 0; i < numfields; i++)
1206bff5b4b3SYuiko Oshino 		if (val[i] != -(i + 1 + KSZ9131_OFFSET)) {
1207bff5b4b3SYuiko Oshino 			mask = 0xffff;
1208bff5b4b3SYuiko Oshino 			mask ^= maxval << (field_sz * i);
1209bff5b4b3SYuiko Oshino 			newval = (newval & mask) |
1210bff5b4b3SYuiko Oshino 				(((val[i] / KSZ9131_STEP) & maxval)
1211bff5b4b3SYuiko Oshino 					<< (field_sz * i));
1212bff5b4b3SYuiko Oshino 		}
1213bff5b4b3SYuiko Oshino 
12149b420effSHeiner Kallweit 	return phy_write_mmd(phydev, 2, reg, newval);
1215bff5b4b3SYuiko Oshino }
1216bff5b4b3SYuiko Oshino 
1217bd734a74SPhilippe Schenker #define KSZ9131RN_MMD_COMMON_CTRL_REG	2
1218bd734a74SPhilippe Schenker #define KSZ9131RN_RXC_DLL_CTRL		76
1219bd734a74SPhilippe Schenker #define KSZ9131RN_TXC_DLL_CTRL		77
1220bd734a74SPhilippe Schenker #define KSZ9131RN_DLL_ENABLE_DELAY	0
1221bd734a74SPhilippe Schenker 
ksz9131_config_rgmii_delay(struct phy_device * phydev)1222bd734a74SPhilippe Schenker static int ksz9131_config_rgmii_delay(struct phy_device *phydev)
1223bd734a74SPhilippe Schenker {
1224a8f1a19dSHoratiu Vultur 	const struct kszphy_type *type = phydev->drv->driver_data;
1225bd734a74SPhilippe Schenker 	u16 rxcdll_val, txcdll_val;
1226bd734a74SPhilippe Schenker 	int ret;
1227bd734a74SPhilippe Schenker 
1228bd734a74SPhilippe Schenker 	switch (phydev->interface) {
1229bd734a74SPhilippe Schenker 	case PHY_INTERFACE_MODE_RGMII:
1230a8f1a19dSHoratiu Vultur 		rxcdll_val = type->disable_dll_rx_bit;
1231a8f1a19dSHoratiu Vultur 		txcdll_val = type->disable_dll_tx_bit;
1232bd734a74SPhilippe Schenker 		break;
1233bd734a74SPhilippe Schenker 	case PHY_INTERFACE_MODE_RGMII_ID:
1234bd734a74SPhilippe Schenker 		rxcdll_val = KSZ9131RN_DLL_ENABLE_DELAY;
1235bd734a74SPhilippe Schenker 		txcdll_val = KSZ9131RN_DLL_ENABLE_DELAY;
1236bd734a74SPhilippe Schenker 		break;
1237bd734a74SPhilippe Schenker 	case PHY_INTERFACE_MODE_RGMII_RXID:
1238bd734a74SPhilippe Schenker 		rxcdll_val = KSZ9131RN_DLL_ENABLE_DELAY;
1239a8f1a19dSHoratiu Vultur 		txcdll_val = type->disable_dll_tx_bit;
1240bd734a74SPhilippe Schenker 		break;
1241bd734a74SPhilippe Schenker 	case PHY_INTERFACE_MODE_RGMII_TXID:
1242a8f1a19dSHoratiu Vultur 		rxcdll_val = type->disable_dll_rx_bit;
1243bd734a74SPhilippe Schenker 		txcdll_val = KSZ9131RN_DLL_ENABLE_DELAY;
1244bd734a74SPhilippe Schenker 		break;
1245bd734a74SPhilippe Schenker 	default:
1246bd734a74SPhilippe Schenker 		return 0;
1247bd734a74SPhilippe Schenker 	}
1248bd734a74SPhilippe Schenker 
1249bd734a74SPhilippe Schenker 	ret = phy_modify_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
1250a8f1a19dSHoratiu Vultur 			     KSZ9131RN_RXC_DLL_CTRL, type->disable_dll_mask,
1251bd734a74SPhilippe Schenker 			     rxcdll_val);
1252bd734a74SPhilippe Schenker 	if (ret < 0)
1253bd734a74SPhilippe Schenker 		return ret;
1254bd734a74SPhilippe Schenker 
1255bd734a74SPhilippe Schenker 	return phy_modify_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
1256a8f1a19dSHoratiu Vultur 			      KSZ9131RN_TXC_DLL_CTRL, type->disable_dll_mask,
1257bd734a74SPhilippe Schenker 			      txcdll_val);
1258bd734a74SPhilippe Schenker }
1259bd734a74SPhilippe Schenker 
12600316c7e6SFrancesco Dolcini /* Silicon Errata DS80000693B
12610316c7e6SFrancesco Dolcini  *
12620316c7e6SFrancesco Dolcini  * When LEDs are configured in Individual Mode, LED1 is ON in a no-link
12630316c7e6SFrancesco Dolcini  * condition. Workaround is to set register 0x1e, bit 9, this way LED1 behaves
12640316c7e6SFrancesco Dolcini  * according to the datasheet (off if there is no link).
12650316c7e6SFrancesco Dolcini  */
ksz9131_led_errata(struct phy_device * phydev)12660316c7e6SFrancesco Dolcini static int ksz9131_led_errata(struct phy_device *phydev)
12670316c7e6SFrancesco Dolcini {
12680316c7e6SFrancesco Dolcini 	int reg;
12690316c7e6SFrancesco Dolcini 
12700316c7e6SFrancesco Dolcini 	reg = phy_read_mmd(phydev, 2, 0);
12710316c7e6SFrancesco Dolcini 	if (reg < 0)
12720316c7e6SFrancesco Dolcini 		return reg;
12730316c7e6SFrancesco Dolcini 
12740316c7e6SFrancesco Dolcini 	if (!(reg & BIT(4)))
12750316c7e6SFrancesco Dolcini 		return 0;
12760316c7e6SFrancesco Dolcini 
12770316c7e6SFrancesco Dolcini 	return phy_set_bits(phydev, 0x1e, BIT(9));
12780316c7e6SFrancesco Dolcini }
12790316c7e6SFrancesco Dolcini 
ksz9131_config_init(struct phy_device * phydev)1280bff5b4b3SYuiko Oshino static int ksz9131_config_init(struct phy_device *phydev)
1281bff5b4b3SYuiko Oshino {
1282ce4f8afdSColin Ian King 	struct device_node *of_node;
1283bff5b4b3SYuiko Oshino 	char *clk_skews[2] = {"rxc-skew-psec", "txc-skew-psec"};
1284bff5b4b3SYuiko Oshino 	char *rx_data_skews[4] = {
1285bff5b4b3SYuiko Oshino 		"rxd0-skew-psec", "rxd1-skew-psec",
1286bff5b4b3SYuiko Oshino 		"rxd2-skew-psec", "rxd3-skew-psec"
1287bff5b4b3SYuiko Oshino 	};
1288bff5b4b3SYuiko Oshino 	char *tx_data_skews[4] = {
1289bff5b4b3SYuiko Oshino 		"txd0-skew-psec", "txd1-skew-psec",
1290bff5b4b3SYuiko Oshino 		"txd2-skew-psec", "txd3-skew-psec"
1291bff5b4b3SYuiko Oshino 	};
1292bff5b4b3SYuiko Oshino 	char *control_skews[2] = {"txen-skew-psec", "rxdv-skew-psec"};
1293bff5b4b3SYuiko Oshino 	const struct device *dev_walker;
1294bff5b4b3SYuiko Oshino 	int ret;
1295bff5b4b3SYuiko Oshino 
1296*20949961SRaju Lakkaraju 	phydev->mdix_ctrl = ETH_TP_MDI_AUTO;
1297*20949961SRaju Lakkaraju 
1298bff5b4b3SYuiko Oshino 	dev_walker = &phydev->mdio.dev;
1299bff5b4b3SYuiko Oshino 	do {
1300bff5b4b3SYuiko Oshino 		of_node = dev_walker->of_node;
1301bff5b4b3SYuiko Oshino 		dev_walker = dev_walker->parent;
1302bff5b4b3SYuiko Oshino 	} while (!of_node && dev_walker);
1303bff5b4b3SYuiko Oshino 
1304bff5b4b3SYuiko Oshino 	if (!of_node)
1305bff5b4b3SYuiko Oshino 		return 0;
1306bff5b4b3SYuiko Oshino 
1307bd734a74SPhilippe Schenker 	if (phy_interface_is_rgmii(phydev)) {
1308bd734a74SPhilippe Schenker 		ret = ksz9131_config_rgmii_delay(phydev);
1309bd734a74SPhilippe Schenker 		if (ret < 0)
1310bd734a74SPhilippe Schenker 			return ret;
1311bd734a74SPhilippe Schenker 	}
1312bd734a74SPhilippe Schenker 
1313bff5b4b3SYuiko Oshino 	ret = ksz9131_of_load_skew_values(phydev, of_node,
1314bff5b4b3SYuiko Oshino 					  MII_KSZ9031RN_CLK_PAD_SKEW, 5,
1315bff5b4b3SYuiko Oshino 					  clk_skews, 2);
1316bff5b4b3SYuiko Oshino 	if (ret < 0)
1317bff5b4b3SYuiko Oshino 		return ret;
1318bff5b4b3SYuiko Oshino 
1319bff5b4b3SYuiko Oshino 	ret = ksz9131_of_load_skew_values(phydev, of_node,
1320bff5b4b3SYuiko Oshino 					  MII_KSZ9031RN_CONTROL_PAD_SKEW, 4,
1321bff5b4b3SYuiko Oshino 					  control_skews, 2);
1322bff5b4b3SYuiko Oshino 	if (ret < 0)
1323bff5b4b3SYuiko Oshino 		return ret;
1324bff5b4b3SYuiko Oshino 
1325bff5b4b3SYuiko Oshino 	ret = ksz9131_of_load_skew_values(phydev, of_node,
1326bff5b4b3SYuiko Oshino 					  MII_KSZ9031RN_RX_DATA_PAD_SKEW, 4,
1327bff5b4b3SYuiko Oshino 					  rx_data_skews, 4);
1328bff5b4b3SYuiko Oshino 	if (ret < 0)
1329bff5b4b3SYuiko Oshino 		return ret;
1330bff5b4b3SYuiko Oshino 
1331bff5b4b3SYuiko Oshino 	ret = ksz9131_of_load_skew_values(phydev, of_node,
1332bff5b4b3SYuiko Oshino 					  MII_KSZ9031RN_TX_DATA_PAD_SKEW, 4,
1333bff5b4b3SYuiko Oshino 					  tx_data_skews, 4);
1334bff5b4b3SYuiko Oshino 	if (ret < 0)
1335bff5b4b3SYuiko Oshino 		return ret;
1336bff5b4b3SYuiko Oshino 
13370316c7e6SFrancesco Dolcini 	ret = ksz9131_led_errata(phydev);
13380316c7e6SFrancesco Dolcini 	if (ret < 0)
13390316c7e6SFrancesco Dolcini 		return ret;
13400316c7e6SFrancesco Dolcini 
1341bff5b4b3SYuiko Oshino 	return 0;
1342bff5b4b3SYuiko Oshino }
1343bff5b4b3SYuiko Oshino 
1344b64e6a87SRaju Lakkaraju #define MII_KSZ9131_AUTO_MDIX		0x1C
1345b64e6a87SRaju Lakkaraju #define MII_KSZ9131_AUTO_MDI_SET	BIT(7)
1346b64e6a87SRaju Lakkaraju #define MII_KSZ9131_AUTO_MDIX_SWAP_OFF	BIT(6)
1347*20949961SRaju Lakkaraju #define MII_KSZ9131_DIG_AXAN_STS	0x14
1348*20949961SRaju Lakkaraju #define MII_KSZ9131_DIG_AXAN_STS_LINK_DET	BIT(14)
1349*20949961SRaju Lakkaraju #define MII_KSZ9131_DIG_AXAN_STS_A_SELECT	BIT(12)
1350b64e6a87SRaju Lakkaraju 
ksz9131_mdix_update(struct phy_device * phydev)1351b64e6a87SRaju Lakkaraju static int ksz9131_mdix_update(struct phy_device *phydev)
1352b64e6a87SRaju Lakkaraju {
1353b64e6a87SRaju Lakkaraju 	int ret;
1354b64e6a87SRaju Lakkaraju 
1355*20949961SRaju Lakkaraju 	if (phydev->mdix_ctrl != ETH_TP_MDI_AUTO) {
1356*20949961SRaju Lakkaraju 		phydev->mdix = phydev->mdix_ctrl;
1357*20949961SRaju Lakkaraju 	} else {
1358*20949961SRaju Lakkaraju 		ret = phy_read(phydev, MII_KSZ9131_DIG_AXAN_STS);
1359b64e6a87SRaju Lakkaraju 		if (ret < 0)
1360b64e6a87SRaju Lakkaraju 			return ret;
1361b64e6a87SRaju Lakkaraju 
1362*20949961SRaju Lakkaraju 		if (ret & MII_KSZ9131_DIG_AXAN_STS_LINK_DET) {
1363*20949961SRaju Lakkaraju 			if (ret & MII_KSZ9131_DIG_AXAN_STS_A_SELECT)
1364b64e6a87SRaju Lakkaraju 				phydev->mdix = ETH_TP_MDI;
1365b64e6a87SRaju Lakkaraju 			else
1366b64e6a87SRaju Lakkaraju 				phydev->mdix = ETH_TP_MDI_X;
1367*20949961SRaju Lakkaraju 		} else {
1368*20949961SRaju Lakkaraju 			phydev->mdix = ETH_TP_MDI_INVALID;
1369*20949961SRaju Lakkaraju 		}
1370*20949961SRaju Lakkaraju 	}
1371b64e6a87SRaju Lakkaraju 
1372b64e6a87SRaju Lakkaraju 	return 0;
1373b64e6a87SRaju Lakkaraju }
1374b64e6a87SRaju Lakkaraju 
ksz9131_config_mdix(struct phy_device * phydev,u8 ctrl)1375b64e6a87SRaju Lakkaraju static int ksz9131_config_mdix(struct phy_device *phydev, u8 ctrl)
1376b64e6a87SRaju Lakkaraju {
1377b64e6a87SRaju Lakkaraju 	u16 val;
1378b64e6a87SRaju Lakkaraju 
1379b64e6a87SRaju Lakkaraju 	switch (ctrl) {
1380b64e6a87SRaju Lakkaraju 	case ETH_TP_MDI:
1381b64e6a87SRaju Lakkaraju 		val = MII_KSZ9131_AUTO_MDIX_SWAP_OFF |
1382b64e6a87SRaju Lakkaraju 		      MII_KSZ9131_AUTO_MDI_SET;
1383b64e6a87SRaju Lakkaraju 		break;
1384b64e6a87SRaju Lakkaraju 	case ETH_TP_MDI_X:
1385b64e6a87SRaju Lakkaraju 		val = MII_KSZ9131_AUTO_MDIX_SWAP_OFF;
1386b64e6a87SRaju Lakkaraju 		break;
1387b64e6a87SRaju Lakkaraju 	case ETH_TP_MDI_AUTO:
1388b64e6a87SRaju Lakkaraju 		val = 0;
1389b64e6a87SRaju Lakkaraju 		break;
1390b64e6a87SRaju Lakkaraju 	default:
1391b64e6a87SRaju Lakkaraju 		return 0;
1392b64e6a87SRaju Lakkaraju 	}
1393b64e6a87SRaju Lakkaraju 
1394b64e6a87SRaju Lakkaraju 	return phy_modify(phydev, MII_KSZ9131_AUTO_MDIX,
1395b64e6a87SRaju Lakkaraju 			  MII_KSZ9131_AUTO_MDIX_SWAP_OFF |
1396b64e6a87SRaju Lakkaraju 			  MII_KSZ9131_AUTO_MDI_SET, val);
1397b64e6a87SRaju Lakkaraju }
1398b64e6a87SRaju Lakkaraju 
ksz9131_read_status(struct phy_device * phydev)1399b64e6a87SRaju Lakkaraju static int ksz9131_read_status(struct phy_device *phydev)
1400b64e6a87SRaju Lakkaraju {
1401b64e6a87SRaju Lakkaraju 	int ret;
1402b64e6a87SRaju Lakkaraju 
1403b64e6a87SRaju Lakkaraju 	ret = ksz9131_mdix_update(phydev);
1404b64e6a87SRaju Lakkaraju 	if (ret < 0)
1405b64e6a87SRaju Lakkaraju 		return ret;
1406b64e6a87SRaju Lakkaraju 
1407b64e6a87SRaju Lakkaraju 	return genphy_read_status(phydev);
1408b64e6a87SRaju Lakkaraju }
1409b64e6a87SRaju Lakkaraju 
ksz9131_config_aneg(struct phy_device * phydev)1410b64e6a87SRaju Lakkaraju static int ksz9131_config_aneg(struct phy_device *phydev)
1411b64e6a87SRaju Lakkaraju {
1412b64e6a87SRaju Lakkaraju 	int ret;
1413b64e6a87SRaju Lakkaraju 
1414b64e6a87SRaju Lakkaraju 	ret = ksz9131_config_mdix(phydev, phydev->mdix_ctrl);
1415b64e6a87SRaju Lakkaraju 	if (ret)
1416b64e6a87SRaju Lakkaraju 		return ret;
1417b64e6a87SRaju Lakkaraju 
1418b64e6a87SRaju Lakkaraju 	return genphy_config_aneg(phydev);
1419b64e6a87SRaju Lakkaraju }
1420b64e6a87SRaju Lakkaraju 
ksz9477_get_features(struct phy_device * phydev)142148fb1994SOleksij Rempel static int ksz9477_get_features(struct phy_device *phydev)
142248fb1994SOleksij Rempel {
142348fb1994SOleksij Rempel 	int ret;
142448fb1994SOleksij Rempel 
142548fb1994SOleksij Rempel 	ret = genphy_read_abilities(phydev);
142648fb1994SOleksij Rempel 	if (ret)
142748fb1994SOleksij Rempel 		return ret;
142848fb1994SOleksij Rempel 
142948fb1994SOleksij Rempel 	/* The "EEE control and capability 1" (Register 3.20) seems to be
143048fb1994SOleksij Rempel 	 * influenced by the "EEE advertisement 1" (Register 7.60). Changes
143148fb1994SOleksij Rempel 	 * on the 7.60 will affect 3.20. So, we need to construct our own list
143248fb1994SOleksij Rempel 	 * of caps.
143348fb1994SOleksij Rempel 	 * KSZ8563R should have 100BaseTX/Full only.
143448fb1994SOleksij Rempel 	 */
143548fb1994SOleksij Rempel 	linkmode_and(phydev->supported_eee, phydev->supported,
143648fb1994SOleksij Rempel 		     PHY_EEE_CAP1_FEATURES);
143748fb1994SOleksij Rempel 
143848fb1994SOleksij Rempel 	return 0;
143948fb1994SOleksij Rempel }
144048fb1994SOleksij Rempel 
144193272e07SJean-Christophe PLAGNIOL-VILLARD #define KSZ8873MLL_GLOBAL_CONTROL_4	0x06
144200aee095SJohan Hovold #define KSZ8873MLL_GLOBAL_CONTROL_4_DUPLEX	BIT(6)
144300aee095SJohan Hovold #define KSZ8873MLL_GLOBAL_CONTROL_4_SPEED	BIT(4)
ksz8873mll_read_status(struct phy_device * phydev)144432d73b14SJingoo Han static int ksz8873mll_read_status(struct phy_device *phydev)
144593272e07SJean-Christophe PLAGNIOL-VILLARD {
144693272e07SJean-Christophe PLAGNIOL-VILLARD 	int regval;
144793272e07SJean-Christophe PLAGNIOL-VILLARD 
144893272e07SJean-Christophe PLAGNIOL-VILLARD 	/* dummy read */
144993272e07SJean-Christophe PLAGNIOL-VILLARD 	regval = phy_read(phydev, KSZ8873MLL_GLOBAL_CONTROL_4);
145093272e07SJean-Christophe PLAGNIOL-VILLARD 
145193272e07SJean-Christophe PLAGNIOL-VILLARD 	regval = phy_read(phydev, KSZ8873MLL_GLOBAL_CONTROL_4);
145293272e07SJean-Christophe PLAGNIOL-VILLARD 
145393272e07SJean-Christophe PLAGNIOL-VILLARD 	if (regval & KSZ8873MLL_GLOBAL_CONTROL_4_DUPLEX)
145493272e07SJean-Christophe PLAGNIOL-VILLARD 		phydev->duplex = DUPLEX_HALF;
145593272e07SJean-Christophe PLAGNIOL-VILLARD 	else
145693272e07SJean-Christophe PLAGNIOL-VILLARD 		phydev->duplex = DUPLEX_FULL;
145793272e07SJean-Christophe PLAGNIOL-VILLARD 
145893272e07SJean-Christophe PLAGNIOL-VILLARD 	if (regval & KSZ8873MLL_GLOBAL_CONTROL_4_SPEED)
145993272e07SJean-Christophe PLAGNIOL-VILLARD 		phydev->speed = SPEED_10;
146093272e07SJean-Christophe PLAGNIOL-VILLARD 	else
146193272e07SJean-Christophe PLAGNIOL-VILLARD 		phydev->speed = SPEED_100;
146293272e07SJean-Christophe PLAGNIOL-VILLARD 
146393272e07SJean-Christophe PLAGNIOL-VILLARD 	phydev->link = 1;
146493272e07SJean-Christophe PLAGNIOL-VILLARD 	phydev->pause = phydev->asym_pause = 0;
146593272e07SJean-Christophe PLAGNIOL-VILLARD 
146693272e07SJean-Christophe PLAGNIOL-VILLARD 	return 0;
146793272e07SJean-Christophe PLAGNIOL-VILLARD }
146893272e07SJean-Christophe PLAGNIOL-VILLARD 
ksz9031_get_features(struct phy_device * phydev)14693aed3e2aSAntoine Tenart static int ksz9031_get_features(struct phy_device *phydev)
14703aed3e2aSAntoine Tenart {
14713aed3e2aSAntoine Tenart 	int ret;
14723aed3e2aSAntoine Tenart 
14733aed3e2aSAntoine Tenart 	ret = genphy_read_abilities(phydev);
14743aed3e2aSAntoine Tenart 	if (ret < 0)
14753aed3e2aSAntoine Tenart 		return ret;
14763aed3e2aSAntoine Tenart 
14773aed3e2aSAntoine Tenart 	/* Silicon Errata Sheet (DS80000691D or DS80000692D):
14783aed3e2aSAntoine Tenart 	 * Whenever the device's Asymmetric Pause capability is set to 1,
14793aed3e2aSAntoine Tenart 	 * link-up may fail after a link-up to link-down transition.
14803aed3e2aSAntoine Tenart 	 *
1481407d8098SHans Andersson 	 * The Errata Sheet is for ksz9031, but ksz9021 has the same issue
1482407d8098SHans Andersson 	 *
14833aed3e2aSAntoine Tenart 	 * Workaround:
14843aed3e2aSAntoine Tenart 	 * Do not enable the Asymmetric Pause capability bit.
14853aed3e2aSAntoine Tenart 	 */
14863aed3e2aSAntoine Tenart 	linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported);
14873aed3e2aSAntoine Tenart 
14883aed3e2aSAntoine Tenart 	/* We force setting the Pause capability as the core will force the
14893aed3e2aSAntoine Tenart 	 * Asymmetric Pause capability to 1 otherwise.
14903aed3e2aSAntoine Tenart 	 */
14913aed3e2aSAntoine Tenart 	linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported);
14923aed3e2aSAntoine Tenart 
14933aed3e2aSAntoine Tenart 	return 0;
14943aed3e2aSAntoine Tenart }
14953aed3e2aSAntoine Tenart 
ksz9031_read_status(struct phy_device * phydev)1496d2fd719bSNathan Sullivan static int ksz9031_read_status(struct phy_device *phydev)
1497d2fd719bSNathan Sullivan {
1498d2fd719bSNathan Sullivan 	int err;
1499d2fd719bSNathan Sullivan 	int regval;
1500d2fd719bSNathan Sullivan 
1501d2fd719bSNathan Sullivan 	err = genphy_read_status(phydev);
1502d2fd719bSNathan Sullivan 	if (err)
1503d2fd719bSNathan Sullivan 		return err;
1504d2fd719bSNathan Sullivan 
1505d2fd719bSNathan Sullivan 	/* Make sure the PHY is not broken. Read idle error count,
1506d2fd719bSNathan Sullivan 	 * and reset the PHY if it is maxed out.
1507d2fd719bSNathan Sullivan 	 */
1508d2fd719bSNathan Sullivan 	regval = phy_read(phydev, MII_STAT1000);
1509d2fd719bSNathan Sullivan 	if ((regval & 0xFF) == 0xFF) {
1510d2fd719bSNathan Sullivan 		phy_init_hw(phydev);
1511d2fd719bSNathan Sullivan 		phydev->link = 0;
1512b866203dSZach Brown 		if (phydev->drv->config_intr && phy_interrupt_is_valid(phydev))
1513b866203dSZach Brown 			phydev->drv->config_intr(phydev);
1514c1a8d0a3SGrygorii Strashko 		return genphy_config_aneg(phydev);
1515d2fd719bSNathan Sullivan 	}
1516d2fd719bSNathan Sullivan 
1517d2fd719bSNathan Sullivan 	return 0;
1518d2fd719bSNathan Sullivan }
1519d2fd719bSNathan Sullivan 
ksz9x31_cable_test_start(struct phy_device * phydev)152058389c00SMarek Vasut static int ksz9x31_cable_test_start(struct phy_device *phydev)
152158389c00SMarek Vasut {
152258389c00SMarek Vasut 	struct kszphy_priv *priv = phydev->priv;
152358389c00SMarek Vasut 	int ret;
152458389c00SMarek Vasut 
152558389c00SMarek Vasut 	/* KSZ9131RNX, DS00002841B-page 38, 4.14 LinkMD (R) Cable Diagnostic
152658389c00SMarek Vasut 	 * Prior to running the cable diagnostics, Auto-negotiation should
152758389c00SMarek Vasut 	 * be disabled, full duplex set and the link speed set to 1000Mbps
152858389c00SMarek Vasut 	 * via the Basic Control Register.
152958389c00SMarek Vasut 	 */
153058389c00SMarek Vasut 	ret = phy_modify(phydev, MII_BMCR,
153158389c00SMarek Vasut 			 BMCR_SPEED1000 | BMCR_FULLDPLX |
153258389c00SMarek Vasut 			 BMCR_ANENABLE | BMCR_SPEED100,
153358389c00SMarek Vasut 			 BMCR_SPEED1000 | BMCR_FULLDPLX);
153458389c00SMarek Vasut 	if (ret)
153558389c00SMarek Vasut 		return ret;
153658389c00SMarek Vasut 
153758389c00SMarek Vasut 	/* KSZ9131RNX, DS00002841B-page 38, 4.14 LinkMD (R) Cable Diagnostic
153858389c00SMarek Vasut 	 * The Master-Slave configuration should be set to Slave by writing
153958389c00SMarek Vasut 	 * a value of 0x1000 to the Auto-Negotiation Master Slave Control
154058389c00SMarek Vasut 	 * Register.
154158389c00SMarek Vasut 	 */
154258389c00SMarek Vasut 	ret = phy_read(phydev, MII_CTRL1000);
154358389c00SMarek Vasut 	if (ret < 0)
154458389c00SMarek Vasut 		return ret;
154558389c00SMarek Vasut 
154658389c00SMarek Vasut 	/* Cache these bits, they need to be restored once LinkMD finishes. */
154758389c00SMarek Vasut 	priv->vct_ctrl1000 = ret & (CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER);
154858389c00SMarek Vasut 	ret &= ~(CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER);
154958389c00SMarek Vasut 	ret |= CTL1000_ENABLE_MASTER;
155058389c00SMarek Vasut 
155158389c00SMarek Vasut 	return phy_write(phydev, MII_CTRL1000, ret);
155258389c00SMarek Vasut }
155358389c00SMarek Vasut 
ksz9x31_cable_test_result_trans(u16 status)155458389c00SMarek Vasut static int ksz9x31_cable_test_result_trans(u16 status)
155558389c00SMarek Vasut {
155658389c00SMarek Vasut 	switch (FIELD_GET(KSZ9x31_LMD_VCT_ST_MASK, status)) {
155758389c00SMarek Vasut 	case KSZ9x31_LMD_VCT_ST_NORMAL:
155858389c00SMarek Vasut 		return ETHTOOL_A_CABLE_RESULT_CODE_OK;
155958389c00SMarek Vasut 	case KSZ9x31_LMD_VCT_ST_OPEN:
156058389c00SMarek Vasut 		return ETHTOOL_A_CABLE_RESULT_CODE_OPEN;
156158389c00SMarek Vasut 	case KSZ9x31_LMD_VCT_ST_SHORT:
156258389c00SMarek Vasut 		return ETHTOOL_A_CABLE_RESULT_CODE_SAME_SHORT;
156358389c00SMarek Vasut 	case KSZ9x31_LMD_VCT_ST_FAIL:
156458389c00SMarek Vasut 		fallthrough;
156558389c00SMarek Vasut 	default:
156658389c00SMarek Vasut 		return ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC;
156758389c00SMarek Vasut 	}
156858389c00SMarek Vasut }
156958389c00SMarek Vasut 
ksz9x31_cable_test_failed(u16 status)157058389c00SMarek Vasut static bool ksz9x31_cable_test_failed(u16 status)
157158389c00SMarek Vasut {
157258389c00SMarek Vasut 	int stat = FIELD_GET(KSZ9x31_LMD_VCT_ST_MASK, status);
157358389c00SMarek Vasut 
157458389c00SMarek Vasut 	return stat == KSZ9x31_LMD_VCT_ST_FAIL;
157558389c00SMarek Vasut }
157658389c00SMarek Vasut 
ksz9x31_cable_test_fault_length_valid(u16 status)157758389c00SMarek Vasut static bool ksz9x31_cable_test_fault_length_valid(u16 status)
157858389c00SMarek Vasut {
157958389c00SMarek Vasut 	switch (FIELD_GET(KSZ9x31_LMD_VCT_ST_MASK, status)) {
158058389c00SMarek Vasut 	case KSZ9x31_LMD_VCT_ST_OPEN:
158158389c00SMarek Vasut 		fallthrough;
158258389c00SMarek Vasut 	case KSZ9x31_LMD_VCT_ST_SHORT:
158358389c00SMarek Vasut 		return true;
158458389c00SMarek Vasut 	}
158558389c00SMarek Vasut 	return false;
158658389c00SMarek Vasut }
158758389c00SMarek Vasut 
ksz9x31_cable_test_fault_length(struct phy_device * phydev,u16 stat)158858389c00SMarek Vasut static int ksz9x31_cable_test_fault_length(struct phy_device *phydev, u16 stat)
158958389c00SMarek Vasut {
159058389c00SMarek Vasut 	int dt = FIELD_GET(KSZ9x31_LMD_VCT_DATA_MASK, stat);
159158389c00SMarek Vasut 
159258389c00SMarek Vasut 	/* KSZ9131RNX, DS00002841B-page 38, 4.14 LinkMD (R) Cable Diagnostic
159358389c00SMarek Vasut 	 *
159458389c00SMarek Vasut 	 * distance to fault = (VCT_DATA - 22) * 4 / cable propagation velocity
159558389c00SMarek Vasut 	 */
15964b159f50SRussell King 	if (phydev_id_compare(phydev, PHY_ID_KSZ9131))
159758389c00SMarek Vasut 		dt = clamp(dt - 22, 0, 255);
159858389c00SMarek Vasut 
159958389c00SMarek Vasut 	return (dt * 400) / 10;
160058389c00SMarek Vasut }
160158389c00SMarek Vasut 
ksz9x31_cable_test_wait_for_completion(struct phy_device * phydev)160258389c00SMarek Vasut static int ksz9x31_cable_test_wait_for_completion(struct phy_device *phydev)
160358389c00SMarek Vasut {
160458389c00SMarek Vasut 	int val, ret;
160558389c00SMarek Vasut 
160658389c00SMarek Vasut 	ret = phy_read_poll_timeout(phydev, KSZ9x31_LMD, val,
160758389c00SMarek Vasut 				    !(val & KSZ9x31_LMD_VCT_EN),
160858389c00SMarek Vasut 				    30000, 100000, true);
160958389c00SMarek Vasut 
161058389c00SMarek Vasut 	return ret < 0 ? ret : 0;
161158389c00SMarek Vasut }
161258389c00SMarek Vasut 
ksz9x31_cable_test_get_pair(int pair)161358389c00SMarek Vasut static int ksz9x31_cable_test_get_pair(int pair)
161458389c00SMarek Vasut {
161558389c00SMarek Vasut 	static const int ethtool_pair[] = {
161658389c00SMarek Vasut 		ETHTOOL_A_CABLE_PAIR_A,
161758389c00SMarek Vasut 		ETHTOOL_A_CABLE_PAIR_B,
161858389c00SMarek Vasut 		ETHTOOL_A_CABLE_PAIR_C,
161958389c00SMarek Vasut 		ETHTOOL_A_CABLE_PAIR_D,
162058389c00SMarek Vasut 	};
162158389c00SMarek Vasut 
162258389c00SMarek Vasut 	return ethtool_pair[pair];
162358389c00SMarek Vasut }
162458389c00SMarek Vasut 
ksz9x31_cable_test_one_pair(struct phy_device * phydev,int pair)162558389c00SMarek Vasut static int ksz9x31_cable_test_one_pair(struct phy_device *phydev, int pair)
162658389c00SMarek Vasut {
162758389c00SMarek Vasut 	int ret, val;
162858389c00SMarek Vasut 
162958389c00SMarek Vasut 	/* KSZ9131RNX, DS00002841B-page 38, 4.14 LinkMD (R) Cable Diagnostic
163058389c00SMarek Vasut 	 * To test each individual cable pair, set the cable pair in the Cable
163158389c00SMarek Vasut 	 * Diagnostics Test Pair (VCT_PAIR[1:0]) field of the LinkMD Cable
163258389c00SMarek Vasut 	 * Diagnostic Register, along with setting the Cable Diagnostics Test
163358389c00SMarek Vasut 	 * Enable (VCT_EN) bit. The Cable Diagnostics Test Enable (VCT_EN) bit
163458389c00SMarek Vasut 	 * will self clear when the test is concluded.
163558389c00SMarek Vasut 	 */
163658389c00SMarek Vasut 	ret = phy_write(phydev, KSZ9x31_LMD,
163758389c00SMarek Vasut 			KSZ9x31_LMD_VCT_EN | KSZ9x31_LMD_VCT_PAIR(pair));
163858389c00SMarek Vasut 	if (ret)
163958389c00SMarek Vasut 		return ret;
164058389c00SMarek Vasut 
164158389c00SMarek Vasut 	ret = ksz9x31_cable_test_wait_for_completion(phydev);
164258389c00SMarek Vasut 	if (ret)
164358389c00SMarek Vasut 		return ret;
164458389c00SMarek Vasut 
164558389c00SMarek Vasut 	val = phy_read(phydev, KSZ9x31_LMD);
164658389c00SMarek Vasut 	if (val < 0)
164758389c00SMarek Vasut 		return val;
164858389c00SMarek Vasut 
164958389c00SMarek Vasut 	if (ksz9x31_cable_test_failed(val))
165058389c00SMarek Vasut 		return -EAGAIN;
165158389c00SMarek Vasut 
165258389c00SMarek Vasut 	ret = ethnl_cable_test_result(phydev,
165358389c00SMarek Vasut 				      ksz9x31_cable_test_get_pair(pair),
165458389c00SMarek Vasut 				      ksz9x31_cable_test_result_trans(val));
165558389c00SMarek Vasut 	if (ret)
165658389c00SMarek Vasut 		return ret;
165758389c00SMarek Vasut 
165858389c00SMarek Vasut 	if (!ksz9x31_cable_test_fault_length_valid(val))
165958389c00SMarek Vasut 		return 0;
166058389c00SMarek Vasut 
166158389c00SMarek Vasut 	return ethnl_cable_test_fault_length(phydev,
166258389c00SMarek Vasut 					     ksz9x31_cable_test_get_pair(pair),
166358389c00SMarek Vasut 					     ksz9x31_cable_test_fault_length(phydev, val));
166458389c00SMarek Vasut }
166558389c00SMarek Vasut 
ksz9x31_cable_test_get_status(struct phy_device * phydev,bool * finished)166658389c00SMarek Vasut static int ksz9x31_cable_test_get_status(struct phy_device *phydev,
166758389c00SMarek Vasut 					 bool *finished)
166858389c00SMarek Vasut {
166958389c00SMarek Vasut 	struct kszphy_priv *priv = phydev->priv;
167058389c00SMarek Vasut 	unsigned long pair_mask = 0xf;
167158389c00SMarek Vasut 	int retries = 20;
167258389c00SMarek Vasut 	int pair, ret, rv;
167358389c00SMarek Vasut 
167458389c00SMarek Vasut 	*finished = false;
167558389c00SMarek Vasut 
167658389c00SMarek Vasut 	/* Try harder if link partner is active */
167758389c00SMarek Vasut 	while (pair_mask && retries--) {
167858389c00SMarek Vasut 		for_each_set_bit(pair, &pair_mask, 4) {
167958389c00SMarek Vasut 			ret = ksz9x31_cable_test_one_pair(phydev, pair);
168058389c00SMarek Vasut 			if (ret == -EAGAIN)
168158389c00SMarek Vasut 				continue;
168258389c00SMarek Vasut 			if (ret < 0)
168358389c00SMarek Vasut 				return ret;
168458389c00SMarek Vasut 			clear_bit(pair, &pair_mask);
168558389c00SMarek Vasut 		}
168658389c00SMarek Vasut 		/* If link partner is in autonegotiation mode it will send 2ms
168758389c00SMarek Vasut 		 * of FLPs with at least 6ms of silence.
168858389c00SMarek Vasut 		 * Add 2ms sleep to have better chances to hit this silence.
168958389c00SMarek Vasut 		 */
169058389c00SMarek Vasut 		if (pair_mask)
169158389c00SMarek Vasut 			usleep_range(2000, 3000);
169258389c00SMarek Vasut 	}
169358389c00SMarek Vasut 
169458389c00SMarek Vasut 	/* Report remaining unfinished pair result as unknown. */
169558389c00SMarek Vasut 	for_each_set_bit(pair, &pair_mask, 4) {
169658389c00SMarek Vasut 		ret = ethnl_cable_test_result(phydev,
169758389c00SMarek Vasut 					      ksz9x31_cable_test_get_pair(pair),
169858389c00SMarek Vasut 					      ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC);
169958389c00SMarek Vasut 	}
170058389c00SMarek Vasut 
170158389c00SMarek Vasut 	*finished = true;
170258389c00SMarek Vasut 
170358389c00SMarek Vasut 	/* Restore cached bits from before LinkMD got started. */
170458389c00SMarek Vasut 	rv = phy_modify(phydev, MII_CTRL1000,
170558389c00SMarek Vasut 			CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER,
170658389c00SMarek Vasut 			priv->vct_ctrl1000);
170758389c00SMarek Vasut 	if (rv)
170858389c00SMarek Vasut 		return rv;
170958389c00SMarek Vasut 
171058389c00SMarek Vasut 	return ret;
171158389c00SMarek Vasut }
171258389c00SMarek Vasut 
ksz8873mll_config_aneg(struct phy_device * phydev)171393272e07SJean-Christophe PLAGNIOL-VILLARD static int ksz8873mll_config_aneg(struct phy_device *phydev)
171493272e07SJean-Christophe PLAGNIOL-VILLARD {
171593272e07SJean-Christophe PLAGNIOL-VILLARD 	return 0;
171693272e07SJean-Christophe PLAGNIOL-VILLARD }
171793272e07SJean-Christophe PLAGNIOL-VILLARD 
ksz886x_config_mdix(struct phy_device * phydev,u8 ctrl)171852939393SOleksij Rempel static int ksz886x_config_mdix(struct phy_device *phydev, u8 ctrl)
171952939393SOleksij Rempel {
172052939393SOleksij Rempel 	u16 val;
172152939393SOleksij Rempel 
172252939393SOleksij Rempel 	switch (ctrl) {
172352939393SOleksij Rempel 	case ETH_TP_MDI:
172452939393SOleksij Rempel 		val = KSZ886X_BMCR_DISABLE_AUTO_MDIX;
172552939393SOleksij Rempel 		break;
172652939393SOleksij Rempel 	case ETH_TP_MDI_X:
172752939393SOleksij Rempel 		/* Note: The naming of the bit KSZ886X_BMCR_FORCE_MDI is bit
172852939393SOleksij Rempel 		 * counter intuitive, the "-X" in "1 = Force MDI" in the data
172952939393SOleksij Rempel 		 * sheet seems to be missing:
173052939393SOleksij Rempel 		 * 1 = Force MDI (sic!) (transmit on RX+/RX- pins)
173152939393SOleksij Rempel 		 * 0 = Normal operation (transmit on TX+/TX- pins)
173252939393SOleksij Rempel 		 */
173352939393SOleksij Rempel 		val = KSZ886X_BMCR_DISABLE_AUTO_MDIX | KSZ886X_BMCR_FORCE_MDI;
173452939393SOleksij Rempel 		break;
173552939393SOleksij Rempel 	case ETH_TP_MDI_AUTO:
173652939393SOleksij Rempel 		val = 0;
173752939393SOleksij Rempel 		break;
173852939393SOleksij Rempel 	default:
173952939393SOleksij Rempel 		return 0;
174052939393SOleksij Rempel 	}
174152939393SOleksij Rempel 
174252939393SOleksij Rempel 	return phy_modify(phydev, MII_BMCR,
174352939393SOleksij Rempel 			  KSZ886X_BMCR_HP_MDIX | KSZ886X_BMCR_FORCE_MDI |
174452939393SOleksij Rempel 			  KSZ886X_BMCR_DISABLE_AUTO_MDIX,
174552939393SOleksij Rempel 			  KSZ886X_BMCR_HP_MDIX | val);
174652939393SOleksij Rempel }
174752939393SOleksij Rempel 
ksz886x_config_aneg(struct phy_device * phydev)174852939393SOleksij Rempel static int ksz886x_config_aneg(struct phy_device *phydev)
174952939393SOleksij Rempel {
175052939393SOleksij Rempel 	int ret;
175152939393SOleksij Rempel 
175252939393SOleksij Rempel 	ret = genphy_config_aneg(phydev);
175352939393SOleksij Rempel 	if (ret)
175452939393SOleksij Rempel 		return ret;
175552939393SOleksij Rempel 
175652939393SOleksij Rempel 	/* The MDI-X configuration is automatically changed by the PHY after
175752939393SOleksij Rempel 	 * switching from autoneg off to on. So, take MDI-X configuration under
175852939393SOleksij Rempel 	 * own control and set it after autoneg configuration was done.
175952939393SOleksij Rempel 	 */
176052939393SOleksij Rempel 	return ksz886x_config_mdix(phydev, phydev->mdix_ctrl);
176152939393SOleksij Rempel }
176252939393SOleksij Rempel 
ksz886x_mdix_update(struct phy_device * phydev)176352939393SOleksij Rempel static int ksz886x_mdix_update(struct phy_device *phydev)
176452939393SOleksij Rempel {
176552939393SOleksij Rempel 	int ret;
176652939393SOleksij Rempel 
176752939393SOleksij Rempel 	ret = phy_read(phydev, MII_BMCR);
176852939393SOleksij Rempel 	if (ret < 0)
176952939393SOleksij Rempel 		return ret;
177052939393SOleksij Rempel 
177152939393SOleksij Rempel 	if (ret & KSZ886X_BMCR_DISABLE_AUTO_MDIX) {
177252939393SOleksij Rempel 		if (ret & KSZ886X_BMCR_FORCE_MDI)
177352939393SOleksij Rempel 			phydev->mdix_ctrl = ETH_TP_MDI_X;
177452939393SOleksij Rempel 		else
177552939393SOleksij Rempel 			phydev->mdix_ctrl = ETH_TP_MDI;
177652939393SOleksij Rempel 	} else {
177752939393SOleksij Rempel 		phydev->mdix_ctrl = ETH_TP_MDI_AUTO;
177852939393SOleksij Rempel 	}
177952939393SOleksij Rempel 
178052939393SOleksij Rempel 	ret = phy_read(phydev, MII_KSZPHY_CTRL);
178152939393SOleksij Rempel 	if (ret < 0)
178252939393SOleksij Rempel 		return ret;
178352939393SOleksij Rempel 
178452939393SOleksij Rempel 	/* Same reverse logic as KSZ886X_BMCR_FORCE_MDI */
178552939393SOleksij Rempel 	if (ret & KSZ886X_CTRL_MDIX_STAT)
178652939393SOleksij Rempel 		phydev->mdix = ETH_TP_MDI_X;
178752939393SOleksij Rempel 	else
178852939393SOleksij Rempel 		phydev->mdix = ETH_TP_MDI;
178952939393SOleksij Rempel 
179052939393SOleksij Rempel 	return 0;
179152939393SOleksij Rempel }
179252939393SOleksij Rempel 
ksz886x_read_status(struct phy_device * phydev)179352939393SOleksij Rempel static int ksz886x_read_status(struct phy_device *phydev)
179452939393SOleksij Rempel {
179552939393SOleksij Rempel 	int ret;
179652939393SOleksij Rempel 
179752939393SOleksij Rempel 	ret = ksz886x_mdix_update(phydev);
179852939393SOleksij Rempel 	if (ret < 0)
179952939393SOleksij Rempel 		return ret;
180052939393SOleksij Rempel 
180152939393SOleksij Rempel 	return genphy_read_status(phydev);
180252939393SOleksij Rempel }
180352939393SOleksij Rempel 
180426dd2974SRobert Hancock struct ksz9477_errata_write {
180526dd2974SRobert Hancock 	u8 dev_addr;
180626dd2974SRobert Hancock 	u8 reg_addr;
180726dd2974SRobert Hancock 	u16 val;
180826dd2974SRobert Hancock };
180926dd2974SRobert Hancock 
181026dd2974SRobert Hancock static const struct ksz9477_errata_write ksz9477_errata_writes[] = {
181126dd2974SRobert Hancock 	 /* Register settings are needed to improve PHY receive performance */
181226dd2974SRobert Hancock 	{0x01, 0x6f, 0xdd0b},
181326dd2974SRobert Hancock 	{0x01, 0x8f, 0x6032},
181426dd2974SRobert Hancock 	{0x01, 0x9d, 0x248c},
181526dd2974SRobert Hancock 	{0x01, 0x75, 0x0060},
181626dd2974SRobert Hancock 	{0x01, 0xd3, 0x7777},
181726dd2974SRobert Hancock 	{0x1c, 0x06, 0x3008},
181826dd2974SRobert Hancock 	{0x1c, 0x08, 0x2000},
181926dd2974SRobert Hancock 
182026dd2974SRobert Hancock 	/* Transmit waveform amplitude can be improved (1000BASE-T, 100BASE-TX, 10BASE-Te) */
182126dd2974SRobert Hancock 	{0x1c, 0x04, 0x00d0},
182226dd2974SRobert Hancock 
182326dd2974SRobert Hancock 	/* Register settings are required to meet data sheet supply current specifications */
182426dd2974SRobert Hancock 	{0x1c, 0x13, 0x6eff},
182526dd2974SRobert Hancock 	{0x1c, 0x14, 0xe6ff},
182626dd2974SRobert Hancock 	{0x1c, 0x15, 0x6eff},
182726dd2974SRobert Hancock 	{0x1c, 0x16, 0xe6ff},
182826dd2974SRobert Hancock 	{0x1c, 0x17, 0x00ff},
182926dd2974SRobert Hancock 	{0x1c, 0x18, 0x43ff},
183026dd2974SRobert Hancock 	{0x1c, 0x19, 0xc3ff},
183126dd2974SRobert Hancock 	{0x1c, 0x1a, 0x6fff},
183226dd2974SRobert Hancock 	{0x1c, 0x1b, 0x07ff},
183326dd2974SRobert Hancock 	{0x1c, 0x1c, 0x0fff},
183426dd2974SRobert Hancock 	{0x1c, 0x1d, 0xe7ff},
183526dd2974SRobert Hancock 	{0x1c, 0x1e, 0xefff},
183626dd2974SRobert Hancock 	{0x1c, 0x20, 0xeeee},
183726dd2974SRobert Hancock };
183826dd2974SRobert Hancock 
ksz9477_phy_errata(struct phy_device * phydev)183902a25572STristram Ha static int ksz9477_phy_errata(struct phy_device *phydev)
184026dd2974SRobert Hancock {
184126dd2974SRobert Hancock 	int err;
184226dd2974SRobert Hancock 	int i;
184326dd2974SRobert Hancock 
184426dd2974SRobert Hancock 	/* Apply PHY settings to address errata listed in
184526dd2974SRobert Hancock 	 * KSZ9477, KSZ9897, KSZ9896, KSZ9567, KSZ8565
184626dd2974SRobert Hancock 	 * Silicon Errata and Data Sheet Clarification documents.
184726dd2974SRobert Hancock 	 *
184826dd2974SRobert Hancock 	 * Document notes: Before configuring the PHY MMD registers, it is
184926dd2974SRobert Hancock 	 * necessary to set the PHY to 100 Mbps speed with auto-negotiation
185026dd2974SRobert Hancock 	 * disabled by writing to register 0xN100-0xN101. After writing the
185126dd2974SRobert Hancock 	 * MMD registers, and after all errata workarounds that involve PHY
185226dd2974SRobert Hancock 	 * register settings, write register 0xN100-0xN101 again to enable
185326dd2974SRobert Hancock 	 * and restart auto-negotiation.
185426dd2974SRobert Hancock 	 */
185526dd2974SRobert Hancock 	err = phy_write(phydev, MII_BMCR, BMCR_SPEED100 | BMCR_FULLDPLX);
185626dd2974SRobert Hancock 	if (err)
185726dd2974SRobert Hancock 		return err;
185826dd2974SRobert Hancock 
185926dd2974SRobert Hancock 	for (i = 0; i < ARRAY_SIZE(ksz9477_errata_writes); ++i) {
186026dd2974SRobert Hancock 		const struct ksz9477_errata_write *errata = &ksz9477_errata_writes[i];
186126dd2974SRobert Hancock 
186226dd2974SRobert Hancock 		err = phy_write_mmd(phydev, errata->dev_addr, errata->reg_addr, errata->val);
186326dd2974SRobert Hancock 		if (err)
186426dd2974SRobert Hancock 			return err;
186526dd2974SRobert Hancock 	}
186626dd2974SRobert Hancock 
186702a25572STristram Ha 	err = genphy_restart_aneg(phydev);
186802a25572STristram Ha 	if (err)
186902a25572STristram Ha 		return err;
187002a25572STristram Ha 
187102a25572STristram Ha 	return err;
187202a25572STristram Ha }
187302a25572STristram Ha 
ksz9477_config_init(struct phy_device * phydev)187402a25572STristram Ha static int ksz9477_config_init(struct phy_device *phydev)
187502a25572STristram Ha {
187602a25572STristram Ha 	int err;
187702a25572STristram Ha 
187802a25572STristram Ha 	/* Only KSZ9897 family of switches needs this fix. */
187902a25572STristram Ha 	if ((phydev->phy_id & 0xf) == 1) {
188002a25572STristram Ha 		err = ksz9477_phy_errata(phydev);
188102a25572STristram Ha 		if (err)
188202a25572STristram Ha 			return err;
188302a25572STristram Ha 	}
188402a25572STristram Ha 
188508c6d8baSLukasz Majewski 	/* According to KSZ9477 Errata DS80000754C (Module 4) all EEE modes
188608c6d8baSLukasz Majewski 	 * in this switch shall be regarded as broken.
188708c6d8baSLukasz Majewski 	 */
188808c6d8baSLukasz Majewski 	if (phydev->dev_flags & MICREL_NO_EEE)
188908c6d8baSLukasz Majewski 		phydev->eee_broken_modes = -1;
189008c6d8baSLukasz Majewski 
189126dd2974SRobert Hancock 	return kszphy_config_init(phydev);
189226dd2974SRobert Hancock }
189326dd2974SRobert Hancock 
kszphy_get_sset_count(struct phy_device * phydev)18942b2427d0SAndrew Lunn static int kszphy_get_sset_count(struct phy_device *phydev)
18952b2427d0SAndrew Lunn {
18962b2427d0SAndrew Lunn 	return ARRAY_SIZE(kszphy_hw_stats);
18972b2427d0SAndrew Lunn }
18982b2427d0SAndrew Lunn 
kszphy_get_strings(struct phy_device * phydev,u8 * data)18992b2427d0SAndrew Lunn static void kszphy_get_strings(struct phy_device *phydev, u8 *data)
19002b2427d0SAndrew Lunn {
19012b2427d0SAndrew Lunn 	int i;
19022b2427d0SAndrew Lunn 
19032b2427d0SAndrew Lunn 	for (i = 0; i < ARRAY_SIZE(kszphy_hw_stats); i++) {
1904fb3ceec1SWolfram Sang 		strscpy(data + i * ETH_GSTRING_LEN,
19052b2427d0SAndrew Lunn 			kszphy_hw_stats[i].string, ETH_GSTRING_LEN);
19062b2427d0SAndrew Lunn 	}
19072b2427d0SAndrew Lunn }
19082b2427d0SAndrew Lunn 
kszphy_get_stat(struct phy_device * phydev,int i)19092b2427d0SAndrew Lunn static u64 kszphy_get_stat(struct phy_device *phydev, int i)
19102b2427d0SAndrew Lunn {
19112b2427d0SAndrew Lunn 	struct kszphy_hw_stat stat = kszphy_hw_stats[i];
19122b2427d0SAndrew Lunn 	struct kszphy_priv *priv = phydev->priv;
1913321b4d4bSAndrew Lunn 	int val;
1914321b4d4bSAndrew Lunn 	u64 ret;
19152b2427d0SAndrew Lunn 
19162b2427d0SAndrew Lunn 	val = phy_read(phydev, stat.reg);
19172b2427d0SAndrew Lunn 	if (val < 0) {
19186c3442f5SJisheng Zhang 		ret = U64_MAX;
19192b2427d0SAndrew Lunn 	} else {
19202b2427d0SAndrew Lunn 		val = val & ((1 << stat.bits) - 1);
19212b2427d0SAndrew Lunn 		priv->stats[i] += val;
1922321b4d4bSAndrew Lunn 		ret = priv->stats[i];
19232b2427d0SAndrew Lunn 	}
19242b2427d0SAndrew Lunn 
1925321b4d4bSAndrew Lunn 	return ret;
19262b2427d0SAndrew Lunn }
19272b2427d0SAndrew Lunn 
kszphy_get_stats(struct phy_device * phydev,struct ethtool_stats * stats,u64 * data)19282b2427d0SAndrew Lunn static void kszphy_get_stats(struct phy_device *phydev,
19292b2427d0SAndrew Lunn 			     struct ethtool_stats *stats, u64 *data)
19302b2427d0SAndrew Lunn {
19312b2427d0SAndrew Lunn 	int i;
19322b2427d0SAndrew Lunn 
19332b2427d0SAndrew Lunn 	for (i = 0; i < ARRAY_SIZE(kszphy_hw_stats); i++)
19342b2427d0SAndrew Lunn 		data[i] = kszphy_get_stat(phydev, i);
19352b2427d0SAndrew Lunn }
19362b2427d0SAndrew Lunn 
kszphy_suspend(struct phy_device * phydev)1937836384d2SWenyou Yang static int kszphy_suspend(struct phy_device *phydev)
1938836384d2SWenyou Yang {
1939836384d2SWenyou Yang 	/* Disable PHY Interrupts */
1940836384d2SWenyou Yang 	if (phy_interrupt_is_valid(phydev)) {
1941836384d2SWenyou Yang 		phydev->interrupts = PHY_INTERRUPT_DISABLED;
1942836384d2SWenyou Yang 		if (phydev->drv->config_intr)
1943836384d2SWenyou Yang 			phydev->drv->config_intr(phydev);
1944836384d2SWenyou Yang 	}
1945836384d2SWenyou Yang 
1946836384d2SWenyou Yang 	return genphy_suspend(phydev);
1947836384d2SWenyou Yang }
1948836384d2SWenyou Yang 
kszphy_parse_led_mode(struct phy_device * phydev)1949a516b7f7SDivya Koppera static void kszphy_parse_led_mode(struct phy_device *phydev)
1950a516b7f7SDivya Koppera {
1951a516b7f7SDivya Koppera 	const struct kszphy_type *type = phydev->drv->driver_data;
1952a516b7f7SDivya Koppera 	const struct device_node *np = phydev->mdio.dev.of_node;
1953a516b7f7SDivya Koppera 	struct kszphy_priv *priv = phydev->priv;
1954a516b7f7SDivya Koppera 	int ret;
1955a516b7f7SDivya Koppera 
1956a516b7f7SDivya Koppera 	if (type && type->led_mode_reg) {
1957a516b7f7SDivya Koppera 		ret = of_property_read_u32(np, "micrel,led-mode",
1958a516b7f7SDivya Koppera 					   &priv->led_mode);
1959a516b7f7SDivya Koppera 
1960a516b7f7SDivya Koppera 		if (ret)
1961a516b7f7SDivya Koppera 			priv->led_mode = -1;
1962a516b7f7SDivya Koppera 
1963a516b7f7SDivya Koppera 		if (priv->led_mode > 3) {
1964a516b7f7SDivya Koppera 			phydev_err(phydev, "invalid led mode: 0x%02x\n",
1965a516b7f7SDivya Koppera 				   priv->led_mode);
1966a516b7f7SDivya Koppera 			priv->led_mode = -1;
1967a516b7f7SDivya Koppera 		}
1968a516b7f7SDivya Koppera 	} else {
1969a516b7f7SDivya Koppera 		priv->led_mode = -1;
1970a516b7f7SDivya Koppera 	}
1971a516b7f7SDivya Koppera }
1972a516b7f7SDivya Koppera 
kszphy_resume(struct phy_device * phydev)1973f5aba91dSAlexandre Belloni static int kszphy_resume(struct phy_device *phydev)
1974f5aba91dSAlexandre Belloni {
197579e498a9SLeonard Crestez 	int ret;
197679e498a9SLeonard Crestez 
1977836384d2SWenyou Yang 	genphy_resume(phydev);
1978f5aba91dSAlexandre Belloni 
19796110dff7SOleksij Rempel 	/* After switching from power-down to normal mode, an internal global
19806110dff7SOleksij Rempel 	 * reset is automatically generated. Wait a minimum of 1 ms before
19816110dff7SOleksij Rempel 	 * read/write access to the PHY registers.
19826110dff7SOleksij Rempel 	 */
19836110dff7SOleksij Rempel 	usleep_range(1000, 2000);
19846110dff7SOleksij Rempel 
198579e498a9SLeonard Crestez 	ret = kszphy_config_reset(phydev);
198679e498a9SLeonard Crestez 	if (ret)
198779e498a9SLeonard Crestez 		return ret;
198879e498a9SLeonard Crestez 
1989836384d2SWenyou Yang 	/* Enable PHY Interrupts */
1990836384d2SWenyou Yang 	if (phy_interrupt_is_valid(phydev)) {
1991836384d2SWenyou Yang 		phydev->interrupts = PHY_INTERRUPT_ENABLED;
1992836384d2SWenyou Yang 		if (phydev->drv->config_intr)
1993836384d2SWenyou Yang 			phydev->drv->config_intr(phydev);
1994836384d2SWenyou Yang 	}
1995f5aba91dSAlexandre Belloni 
1996f5aba91dSAlexandre Belloni 	return 0;
1997f5aba91dSAlexandre Belloni }
1998f5aba91dSAlexandre Belloni 
ksz9477_resume(struct phy_device * phydev)199902a25572STristram Ha static int ksz9477_resume(struct phy_device *phydev)
200002a25572STristram Ha {
200102a25572STristram Ha 	int ret;
200202a25572STristram Ha 
200302a25572STristram Ha 	/* No need to initialize registers if not powered down. */
200402a25572STristram Ha 	ret = phy_read(phydev, MII_BMCR);
200502a25572STristram Ha 	if (ret < 0)
200602a25572STristram Ha 		return ret;
200702a25572STristram Ha 	if (!(ret & BMCR_PDOWN))
200802a25572STristram Ha 		return 0;
200902a25572STristram Ha 
201002a25572STristram Ha 	genphy_resume(phydev);
201102a25572STristram Ha 
201202a25572STristram Ha 	/* After switching from power-down to normal mode, an internal global
201302a25572STristram Ha 	 * reset is automatically generated. Wait a minimum of 1 ms before
201402a25572STristram Ha 	 * read/write access to the PHY registers.
201502a25572STristram Ha 	 */
201602a25572STristram Ha 	usleep_range(1000, 2000);
201702a25572STristram Ha 
201802a25572STristram Ha 	/* Only KSZ9897 family of switches needs this fix. */
201902a25572STristram Ha 	if ((phydev->phy_id & 0xf) == 1) {
202002a25572STristram Ha 		ret = ksz9477_phy_errata(phydev);
202102a25572STristram Ha 		if (ret)
202202a25572STristram Ha 			return ret;
202302a25572STristram Ha 	}
202402a25572STristram Ha 
202502a25572STristram Ha 	/* Enable PHY Interrupts */
202602a25572STristram Ha 	if (phy_interrupt_is_valid(phydev)) {
202702a25572STristram Ha 		phydev->interrupts = PHY_INTERRUPT_ENABLED;
202802a25572STristram Ha 		if (phydev->drv->config_intr)
202902a25572STristram Ha 			phydev->drv->config_intr(phydev);
203002a25572STristram Ha 	}
203102a25572STristram Ha 
203202a25572STristram Ha 	return 0;
203302a25572STristram Ha }
203402a25572STristram Ha 
ksz8061_resume(struct phy_device * phydev)2035cba54674STristram Ha static int ksz8061_resume(struct phy_device *phydev)
2036cba54674STristram Ha {
2037cba54674STristram Ha 	int ret;
2038cba54674STristram Ha 
2039cba54674STristram Ha 	/* This function can be called twice when the Ethernet device is on. */
2040cba54674STristram Ha 	ret = phy_read(phydev, MII_BMCR);
2041cba54674STristram Ha 	if (ret < 0)
2042cba54674STristram Ha 		return ret;
2043cba54674STristram Ha 	if (!(ret & BMCR_PDOWN))
2044cba54674STristram Ha 		return 0;
2045cba54674STristram Ha 
2046cba54674STristram Ha 	genphy_resume(phydev);
2047cba54674STristram Ha 	usleep_range(1000, 2000);
2048cba54674STristram Ha 
2049cba54674STristram Ha 	/* Re-program the value after chip is reset. */
2050cba54674STristram Ha 	ret = phy_write_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_DEVID1, 0xB61A);
2051cba54674STristram Ha 	if (ret)
2052cba54674STristram Ha 		return ret;
2053cba54674STristram Ha 
2054cba54674STristram Ha 	/* Enable PHY Interrupts */
2055cba54674STristram Ha 	if (phy_interrupt_is_valid(phydev)) {
2056cba54674STristram Ha 		phydev->interrupts = PHY_INTERRUPT_ENABLED;
2057cba54674STristram Ha 		if (phydev->drv->config_intr)
2058cba54674STristram Ha 			phydev->drv->config_intr(phydev);
2059cba54674STristram Ha 	}
2060cba54674STristram Ha 
2061cba54674STristram Ha 	return 0;
2062cba54674STristram Ha }
2063cba54674STristram Ha 
kszphy_probe(struct phy_device * phydev)2064e6a423a8SJohan Hovold static int kszphy_probe(struct phy_device *phydev)
2065e6a423a8SJohan Hovold {
2066e6a423a8SJohan Hovold 	const struct kszphy_type *type = phydev->drv->driver_data;
2067e5a03bfdSAndrew Lunn 	const struct device_node *np = phydev->mdio.dev.of_node;
2068e6a423a8SJohan Hovold 	struct kszphy_priv *priv;
206963f44b2bSJohan Hovold 	struct clk *clk;
2070e6a423a8SJohan Hovold 
2071e5a03bfdSAndrew Lunn 	priv = devm_kzalloc(&phydev->mdio.dev, sizeof(*priv), GFP_KERNEL);
2072e6a423a8SJohan Hovold 	if (!priv)
2073e6a423a8SJohan Hovold 		return -ENOMEM;
2074e6a423a8SJohan Hovold 
2075e6a423a8SJohan Hovold 	phydev->priv = priv;
2076e6a423a8SJohan Hovold 
2077e6a423a8SJohan Hovold 	priv->type = type;
2078e6a423a8SJohan Hovold 
2079a516b7f7SDivya Koppera 	kszphy_parse_led_mode(phydev);
2080e7a792e9SJohan Hovold 
2081e5a03bfdSAndrew Lunn 	clk = devm_clk_get(&phydev->mdio.dev, "rmii-ref");
2082bced8701SNiklas Cassel 	/* NOTE: clk may be NULL if building without CONFIG_HAVE_CLK */
2083bced8701SNiklas Cassel 	if (!IS_ERR_OR_NULL(clk)) {
20841fadee0cSSascha Hauer 		unsigned long rate = clk_get_rate(clk);
208586dc1342SJohan Hovold 		bool rmii_ref_clk_sel_25_mhz;
20861fadee0cSSascha Hauer 
2087f2ef6f75SFabio Estevam 		if (type)
208863f44b2bSJohan Hovold 			priv->rmii_ref_clk_sel = type->has_rmii_ref_clk_sel;
208986dc1342SJohan Hovold 		rmii_ref_clk_sel_25_mhz = of_property_read_bool(np,
209086dc1342SJohan Hovold 				"micrel,rmii-reference-clock-select-25-mhz");
209163f44b2bSJohan Hovold 
20921fadee0cSSascha Hauer 		if (rate > 24500000 && rate < 25500000) {
209386dc1342SJohan Hovold 			priv->rmii_ref_clk_sel_val = rmii_ref_clk_sel_25_mhz;
20941fadee0cSSascha Hauer 		} else if (rate > 49500000 && rate < 50500000) {
209586dc1342SJohan Hovold 			priv->rmii_ref_clk_sel_val = !rmii_ref_clk_sel_25_mhz;
20961fadee0cSSascha Hauer 		} else {
209772ba48beSAndrew Lunn 			phydev_err(phydev, "Clock rate out of range: %ld\n",
209872ba48beSAndrew Lunn 				   rate);
20991fadee0cSSascha Hauer 			return -EINVAL;
21001fadee0cSSascha Hauer 		}
21011fadee0cSSascha Hauer 	}
21021fadee0cSSascha Hauer 
21034217a64eSMichael Walle 	if (ksz8041_fiber_mode(phydev))
21044217a64eSMichael Walle 		phydev->port = PORT_FIBRE;
21054217a64eSMichael Walle 
210663f44b2bSJohan Hovold 	/* Support legacy board-file configuration */
210763f44b2bSJohan Hovold 	if (phydev->dev_flags & MICREL_PHY_50MHZ_CLK) {
210863f44b2bSJohan Hovold 		priv->rmii_ref_clk_sel = true;
210963f44b2bSJohan Hovold 		priv->rmii_ref_clk_sel_val = true;
211063f44b2bSJohan Hovold 	}
211163f44b2bSJohan Hovold 
211263f44b2bSJohan Hovold 	return 0;
21131fadee0cSSascha Hauer }
21141fadee0cSSascha Hauer 
lan8814_cable_test_start(struct phy_device * phydev)211521b688daSDivya Koppera static int lan8814_cable_test_start(struct phy_device *phydev)
211621b688daSDivya Koppera {
211721b688daSDivya Koppera 	/* If autoneg is enabled, we won't be able to test cross pair
211821b688daSDivya Koppera 	 * short. In this case, the PHY will "detect" a link and
211921b688daSDivya Koppera 	 * confuse the internal state machine - disable auto neg here.
212021b688daSDivya Koppera 	 * Set the speed to 1000mbit and full duplex.
212121b688daSDivya Koppera 	 */
212221b688daSDivya Koppera 	return phy_modify(phydev, MII_BMCR, BMCR_ANENABLE | BMCR_SPEED100,
212321b688daSDivya Koppera 			  BMCR_SPEED1000 | BMCR_FULLDPLX);
212421b688daSDivya Koppera }
212521b688daSDivya Koppera 
ksz886x_cable_test_start(struct phy_device * phydev)212649011e0cSOleksij Rempel static int ksz886x_cable_test_start(struct phy_device *phydev)
212749011e0cSOleksij Rempel {
212849011e0cSOleksij Rempel 	if (phydev->dev_flags & MICREL_KSZ8_P1_ERRATA)
212949011e0cSOleksij Rempel 		return -EOPNOTSUPP;
213049011e0cSOleksij Rempel 
213149011e0cSOleksij Rempel 	/* If autoneg is enabled, we won't be able to test cross pair
213249011e0cSOleksij Rempel 	 * short. In this case, the PHY will "detect" a link and
213349011e0cSOleksij Rempel 	 * confuse the internal state machine - disable auto neg here.
213449011e0cSOleksij Rempel 	 * If autoneg is disabled, we should set the speed to 10mbit.
213549011e0cSOleksij Rempel 	 */
213649011e0cSOleksij Rempel 	return phy_clear_bits(phydev, MII_BMCR, BMCR_ANENABLE | BMCR_SPEED100);
213749011e0cSOleksij Rempel }
213849011e0cSOleksij Rempel 
ksz886x_cable_test_result_trans(u16 status,u16 mask)2139fa182ea2SDivya Koppera static __always_inline int ksz886x_cable_test_result_trans(u16 status, u16 mask)
214049011e0cSOleksij Rempel {
214121b688daSDivya Koppera 	switch (FIELD_GET(mask, status)) {
214249011e0cSOleksij Rempel 	case KSZ8081_LMD_STAT_NORMAL:
214349011e0cSOleksij Rempel 		return ETHTOOL_A_CABLE_RESULT_CODE_OK;
214449011e0cSOleksij Rempel 	case KSZ8081_LMD_STAT_SHORT:
214549011e0cSOleksij Rempel 		return ETHTOOL_A_CABLE_RESULT_CODE_SAME_SHORT;
214649011e0cSOleksij Rempel 	case KSZ8081_LMD_STAT_OPEN:
214749011e0cSOleksij Rempel 		return ETHTOOL_A_CABLE_RESULT_CODE_OPEN;
214849011e0cSOleksij Rempel 	case KSZ8081_LMD_STAT_FAIL:
214949011e0cSOleksij Rempel 		fallthrough;
215049011e0cSOleksij Rempel 	default:
215149011e0cSOleksij Rempel 		return ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC;
215249011e0cSOleksij Rempel 	}
215349011e0cSOleksij Rempel }
215449011e0cSOleksij Rempel 
ksz886x_cable_test_failed(u16 status,u16 mask)2155fa182ea2SDivya Koppera static __always_inline bool ksz886x_cable_test_failed(u16 status, u16 mask)
215649011e0cSOleksij Rempel {
215721b688daSDivya Koppera 	return FIELD_GET(mask, status) ==
215849011e0cSOleksij Rempel 		KSZ8081_LMD_STAT_FAIL;
215949011e0cSOleksij Rempel }
216049011e0cSOleksij Rempel 
ksz886x_cable_test_fault_length_valid(u16 status,u16 mask)2161fa182ea2SDivya Koppera static __always_inline bool ksz886x_cable_test_fault_length_valid(u16 status, u16 mask)
216249011e0cSOleksij Rempel {
216321b688daSDivya Koppera 	switch (FIELD_GET(mask, status)) {
216449011e0cSOleksij Rempel 	case KSZ8081_LMD_STAT_OPEN:
216549011e0cSOleksij Rempel 		fallthrough;
216649011e0cSOleksij Rempel 	case KSZ8081_LMD_STAT_SHORT:
216749011e0cSOleksij Rempel 		return true;
216849011e0cSOleksij Rempel 	}
216949011e0cSOleksij Rempel 	return false;
217049011e0cSOleksij Rempel }
217149011e0cSOleksij Rempel 
ksz886x_cable_test_fault_length(struct phy_device * phydev,u16 status,u16 data_mask)2172fa182ea2SDivya Koppera static __always_inline int ksz886x_cable_test_fault_length(struct phy_device *phydev,
2173fa182ea2SDivya Koppera 							   u16 status, u16 data_mask)
217449011e0cSOleksij Rempel {
217549011e0cSOleksij Rempel 	int dt;
217649011e0cSOleksij Rempel 
217749011e0cSOleksij Rempel 	/* According to the data sheet the distance to the fault is
217821b688daSDivya Koppera 	 * DELTA_TIME * 0.4 meters for ksz phys.
217921b688daSDivya Koppera 	 * (DELTA_TIME - 22) * 0.8 for lan8814 phy.
218049011e0cSOleksij Rempel 	 */
218121b688daSDivya Koppera 	dt = FIELD_GET(data_mask, status);
218249011e0cSOleksij Rempel 
21834b159f50SRussell King 	if (phydev_id_compare(phydev, PHY_ID_LAN8814))
218421b688daSDivya Koppera 		return ((dt - 22) * 800) / 10;
218521b688daSDivya Koppera 	else
218649011e0cSOleksij Rempel 		return (dt * 400) / 10;
218749011e0cSOleksij Rempel }
218849011e0cSOleksij Rempel 
ksz886x_cable_test_wait_for_completion(struct phy_device * phydev)218949011e0cSOleksij Rempel static int ksz886x_cable_test_wait_for_completion(struct phy_device *phydev)
219049011e0cSOleksij Rempel {
219121b688daSDivya Koppera 	const struct kszphy_type *type = phydev->drv->driver_data;
219249011e0cSOleksij Rempel 	int val, ret;
219349011e0cSOleksij Rempel 
219421b688daSDivya Koppera 	ret = phy_read_poll_timeout(phydev, type->cable_diag_reg, val,
219549011e0cSOleksij Rempel 				    !(val & KSZ8081_LMD_ENABLE_TEST),
219649011e0cSOleksij Rempel 				    30000, 100000, true);
219749011e0cSOleksij Rempel 
219849011e0cSOleksij Rempel 	return ret < 0 ? ret : 0;
219949011e0cSOleksij Rempel }
220049011e0cSOleksij Rempel 
lan8814_cable_test_one_pair(struct phy_device * phydev,int pair)220121b688daSDivya Koppera static int lan8814_cable_test_one_pair(struct phy_device *phydev, int pair)
220221b688daSDivya Koppera {
220321b688daSDivya Koppera 	static const int ethtool_pair[] = { ETHTOOL_A_CABLE_PAIR_A,
220421b688daSDivya Koppera 					    ETHTOOL_A_CABLE_PAIR_B,
220521b688daSDivya Koppera 					    ETHTOOL_A_CABLE_PAIR_C,
220621b688daSDivya Koppera 					    ETHTOOL_A_CABLE_PAIR_D,
220721b688daSDivya Koppera 					  };
220821b688daSDivya Koppera 	u32 fault_length;
220921b688daSDivya Koppera 	int ret;
221021b688daSDivya Koppera 	int val;
221121b688daSDivya Koppera 
221221b688daSDivya Koppera 	val = KSZ8081_LMD_ENABLE_TEST;
221321b688daSDivya Koppera 	val = val | (pair << LAN8814_PAIR_BIT_SHIFT);
221421b688daSDivya Koppera 
221521b688daSDivya Koppera 	ret = phy_write(phydev, LAN8814_CABLE_DIAG, val);
221621b688daSDivya Koppera 	if (ret < 0)
221721b688daSDivya Koppera 		return ret;
221821b688daSDivya Koppera 
221921b688daSDivya Koppera 	ret = ksz886x_cable_test_wait_for_completion(phydev);
222021b688daSDivya Koppera 	if (ret)
222121b688daSDivya Koppera 		return ret;
222221b688daSDivya Koppera 
222321b688daSDivya Koppera 	val = phy_read(phydev, LAN8814_CABLE_DIAG);
222421b688daSDivya Koppera 	if (val < 0)
222521b688daSDivya Koppera 		return val;
222621b688daSDivya Koppera 
222721b688daSDivya Koppera 	if (ksz886x_cable_test_failed(val, LAN8814_CABLE_DIAG_STAT_MASK))
222821b688daSDivya Koppera 		return -EAGAIN;
222921b688daSDivya Koppera 
223021b688daSDivya Koppera 	ret = ethnl_cable_test_result(phydev, ethtool_pair[pair],
223121b688daSDivya Koppera 				      ksz886x_cable_test_result_trans(val,
223221b688daSDivya Koppera 								      LAN8814_CABLE_DIAG_STAT_MASK
223321b688daSDivya Koppera 								      ));
223421b688daSDivya Koppera 	if (ret)
223521b688daSDivya Koppera 		return ret;
223621b688daSDivya Koppera 
223721b688daSDivya Koppera 	if (!ksz886x_cable_test_fault_length_valid(val, LAN8814_CABLE_DIAG_STAT_MASK))
223821b688daSDivya Koppera 		return 0;
223921b688daSDivya Koppera 
224021b688daSDivya Koppera 	fault_length = ksz886x_cable_test_fault_length(phydev, val,
224121b688daSDivya Koppera 						       LAN8814_CABLE_DIAG_VCT_DATA_MASK);
224221b688daSDivya Koppera 
224321b688daSDivya Koppera 	return ethnl_cable_test_fault_length(phydev, ethtool_pair[pair], fault_length);
224421b688daSDivya Koppera }
224521b688daSDivya Koppera 
ksz886x_cable_test_one_pair(struct phy_device * phydev,int pair)224649011e0cSOleksij Rempel static int ksz886x_cable_test_one_pair(struct phy_device *phydev, int pair)
224749011e0cSOleksij Rempel {
224849011e0cSOleksij Rempel 	static const int ethtool_pair[] = {
224949011e0cSOleksij Rempel 		ETHTOOL_A_CABLE_PAIR_A,
225049011e0cSOleksij Rempel 		ETHTOOL_A_CABLE_PAIR_B,
225149011e0cSOleksij Rempel 	};
225249011e0cSOleksij Rempel 	int ret, val, mdix;
225321b688daSDivya Koppera 	u32 fault_length;
225449011e0cSOleksij Rempel 
225549011e0cSOleksij Rempel 	/* There is no way to choice the pair, like we do one ksz9031.
225649011e0cSOleksij Rempel 	 * We can workaround this limitation by using the MDI-X functionality.
225749011e0cSOleksij Rempel 	 */
225849011e0cSOleksij Rempel 	if (pair == 0)
225949011e0cSOleksij Rempel 		mdix = ETH_TP_MDI;
226049011e0cSOleksij Rempel 	else
226149011e0cSOleksij Rempel 		mdix = ETH_TP_MDI_X;
226249011e0cSOleksij Rempel 
226349011e0cSOleksij Rempel 	switch (phydev->phy_id & MICREL_PHY_ID_MASK) {
226449011e0cSOleksij Rempel 	case PHY_ID_KSZ8081:
226549011e0cSOleksij Rempel 		ret = ksz8081_config_mdix(phydev, mdix);
226649011e0cSOleksij Rempel 		break;
226749011e0cSOleksij Rempel 	case PHY_ID_KSZ886X:
226849011e0cSOleksij Rempel 		ret = ksz886x_config_mdix(phydev, mdix);
226949011e0cSOleksij Rempel 		break;
227049011e0cSOleksij Rempel 	default:
227149011e0cSOleksij Rempel 		ret = -ENODEV;
227249011e0cSOleksij Rempel 	}
227349011e0cSOleksij Rempel 
227449011e0cSOleksij Rempel 	if (ret)
227549011e0cSOleksij Rempel 		return ret;
227649011e0cSOleksij Rempel 
227749011e0cSOleksij Rempel 	/* Now we are ready to fire. This command will send a 100ns pulse
227849011e0cSOleksij Rempel 	 * to the pair.
227949011e0cSOleksij Rempel 	 */
228049011e0cSOleksij Rempel 	ret = phy_write(phydev, KSZ8081_LMD, KSZ8081_LMD_ENABLE_TEST);
228149011e0cSOleksij Rempel 	if (ret)
228249011e0cSOleksij Rempel 		return ret;
228349011e0cSOleksij Rempel 
228449011e0cSOleksij Rempel 	ret = ksz886x_cable_test_wait_for_completion(phydev);
228549011e0cSOleksij Rempel 	if (ret)
228649011e0cSOleksij Rempel 		return ret;
228749011e0cSOleksij Rempel 
228849011e0cSOleksij Rempel 	val = phy_read(phydev, KSZ8081_LMD);
228949011e0cSOleksij Rempel 	if (val < 0)
229049011e0cSOleksij Rempel 		return val;
229149011e0cSOleksij Rempel 
229221b688daSDivya Koppera 	if (ksz886x_cable_test_failed(val, KSZ8081_LMD_STAT_MASK))
229349011e0cSOleksij Rempel 		return -EAGAIN;
229449011e0cSOleksij Rempel 
229549011e0cSOleksij Rempel 	ret = ethnl_cable_test_result(phydev, ethtool_pair[pair],
229621b688daSDivya Koppera 				      ksz886x_cable_test_result_trans(val, KSZ8081_LMD_STAT_MASK));
229749011e0cSOleksij Rempel 	if (ret)
229849011e0cSOleksij Rempel 		return ret;
229949011e0cSOleksij Rempel 
230021b688daSDivya Koppera 	if (!ksz886x_cable_test_fault_length_valid(val, KSZ8081_LMD_STAT_MASK))
230149011e0cSOleksij Rempel 		return 0;
230249011e0cSOleksij Rempel 
230321b688daSDivya Koppera 	fault_length = ksz886x_cable_test_fault_length(phydev, val, KSZ8081_LMD_DELTA_TIME_MASK);
230421b688daSDivya Koppera 
230521b688daSDivya Koppera 	return ethnl_cable_test_fault_length(phydev, ethtool_pair[pair], fault_length);
230649011e0cSOleksij Rempel }
230749011e0cSOleksij Rempel 
ksz886x_cable_test_get_status(struct phy_device * phydev,bool * finished)230849011e0cSOleksij Rempel static int ksz886x_cable_test_get_status(struct phy_device *phydev,
230949011e0cSOleksij Rempel 					 bool *finished)
231049011e0cSOleksij Rempel {
231121b688daSDivya Koppera 	const struct kszphy_type *type = phydev->drv->driver_data;
231221b688daSDivya Koppera 	unsigned long pair_mask = type->pair_mask;
231349011e0cSOleksij Rempel 	int retries = 20;
2314d50ede4fSDivya Koppera 	int ret = 0;
2315d50ede4fSDivya Koppera 	int pair;
231649011e0cSOleksij Rempel 
231749011e0cSOleksij Rempel 	*finished = false;
231849011e0cSOleksij Rempel 
231949011e0cSOleksij Rempel 	/* Try harder if link partner is active */
232049011e0cSOleksij Rempel 	while (pair_mask && retries--) {
232149011e0cSOleksij Rempel 		for_each_set_bit(pair, &pair_mask, 4) {
232221b688daSDivya Koppera 			if (type->cable_diag_reg == LAN8814_CABLE_DIAG)
232321b688daSDivya Koppera 				ret = lan8814_cable_test_one_pair(phydev, pair);
232421b688daSDivya Koppera 			else
232549011e0cSOleksij Rempel 				ret = ksz886x_cable_test_one_pair(phydev, pair);
232649011e0cSOleksij Rempel 			if (ret == -EAGAIN)
232749011e0cSOleksij Rempel 				continue;
232849011e0cSOleksij Rempel 			if (ret < 0)
232949011e0cSOleksij Rempel 				return ret;
233049011e0cSOleksij Rempel 			clear_bit(pair, &pair_mask);
233149011e0cSOleksij Rempel 		}
233249011e0cSOleksij Rempel 		/* If link partner is in autonegotiation mode it will send 2ms
233349011e0cSOleksij Rempel 		 * of FLPs with at least 6ms of silence.
233449011e0cSOleksij Rempel 		 * Add 2ms sleep to have better chances to hit this silence.
233549011e0cSOleksij Rempel 		 */
233649011e0cSOleksij Rempel 		if (pair_mask)
233749011e0cSOleksij Rempel 			msleep(2);
233849011e0cSOleksij Rempel 	}
233949011e0cSOleksij Rempel 
234049011e0cSOleksij Rempel 	*finished = true;
234149011e0cSOleksij Rempel 
234249011e0cSOleksij Rempel 	return ret;
234349011e0cSOleksij Rempel }
234449011e0cSOleksij Rempel 
23457c2dcfa2SHoratiu Vultur #define LAN_EXT_PAGE_ACCESS_CONTROL			0x16
23467c2dcfa2SHoratiu Vultur #define LAN_EXT_PAGE_ACCESS_ADDRESS_DATA		0x17
23477c2dcfa2SHoratiu Vultur #define LAN_EXT_PAGE_ACCESS_CTRL_EP_FUNC		0x4000
23487c2dcfa2SHoratiu Vultur 
23497467d716SHoratiu Vultur #define LAN8814_QSGMII_SOFT_RESET			0x43
23507467d716SHoratiu Vultur #define LAN8814_QSGMII_SOFT_RESET_BIT			BIT(0)
23517467d716SHoratiu Vultur #define LAN8814_QSGMII_PCS1G_ANEG_CONFIG		0x13
23527467d716SHoratiu Vultur #define LAN8814_QSGMII_PCS1G_ANEG_CONFIG_ANEG_ENA	BIT(3)
23537467d716SHoratiu Vultur #define LAN8814_ALIGN_SWAP				0x4a
23547467d716SHoratiu Vultur #define LAN8814_ALIGN_TX_A_B_SWAP			0x1
23557467d716SHoratiu Vultur #define LAN8814_ALIGN_TX_A_B_SWAP_MASK			GENMASK(2, 0)
23567467d716SHoratiu Vultur 
23577c2dcfa2SHoratiu Vultur #define LAN8804_ALIGN_SWAP				0x4a
23587c2dcfa2SHoratiu Vultur #define LAN8804_ALIGN_TX_A_B_SWAP			0x1
23597c2dcfa2SHoratiu Vultur #define LAN8804_ALIGN_TX_A_B_SWAP_MASK			GENMASK(2, 0)
23607c2dcfa2SHoratiu Vultur #define LAN8814_CLOCK_MANAGEMENT			0xd
23617c2dcfa2SHoratiu Vultur #define LAN8814_LINK_QUALITY				0x8e
23627c2dcfa2SHoratiu Vultur 
lanphy_read_page_reg(struct phy_device * phydev,int page,u32 addr)23637c2dcfa2SHoratiu Vultur static int lanphy_read_page_reg(struct phy_device *phydev, int page, u32 addr)
23647c2dcfa2SHoratiu Vultur {
236512a4d677SWan Jiabing 	int data;
23667c2dcfa2SHoratiu Vultur 
23674488f6b6SDivya Koppera 	phy_lock_mdio_bus(phydev);
23684488f6b6SDivya Koppera 	__phy_write(phydev, LAN_EXT_PAGE_ACCESS_CONTROL, page);
23694488f6b6SDivya Koppera 	__phy_write(phydev, LAN_EXT_PAGE_ACCESS_ADDRESS_DATA, addr);
23704488f6b6SDivya Koppera 	__phy_write(phydev, LAN_EXT_PAGE_ACCESS_CONTROL,
23717c2dcfa2SHoratiu Vultur 		    (page | LAN_EXT_PAGE_ACCESS_CTRL_EP_FUNC));
23724488f6b6SDivya Koppera 	data = __phy_read(phydev, LAN_EXT_PAGE_ACCESS_ADDRESS_DATA);
23734488f6b6SDivya Koppera 	phy_unlock_mdio_bus(phydev);
23747c2dcfa2SHoratiu Vultur 
23757c2dcfa2SHoratiu Vultur 	return data;
23767c2dcfa2SHoratiu Vultur }
23777c2dcfa2SHoratiu Vultur 
lanphy_write_page_reg(struct phy_device * phydev,int page,u16 addr,u16 val)23787c2dcfa2SHoratiu Vultur static int lanphy_write_page_reg(struct phy_device *phydev, int page, u16 addr,
23797c2dcfa2SHoratiu Vultur 				 u16 val)
23807c2dcfa2SHoratiu Vultur {
23814488f6b6SDivya Koppera 	phy_lock_mdio_bus(phydev);
23824488f6b6SDivya Koppera 	__phy_write(phydev, LAN_EXT_PAGE_ACCESS_CONTROL, page);
23834488f6b6SDivya Koppera 	__phy_write(phydev, LAN_EXT_PAGE_ACCESS_ADDRESS_DATA, addr);
23844488f6b6SDivya Koppera 	__phy_write(phydev, LAN_EXT_PAGE_ACCESS_CONTROL,
23854488f6b6SDivya Koppera 		    page | LAN_EXT_PAGE_ACCESS_CTRL_EP_FUNC);
23867c2dcfa2SHoratiu Vultur 
23874488f6b6SDivya Koppera 	val = __phy_write(phydev, LAN_EXT_PAGE_ACCESS_ADDRESS_DATA, val);
23884488f6b6SDivya Koppera 	if (val != 0)
23897c2dcfa2SHoratiu Vultur 		phydev_err(phydev, "Error: phy_write has returned error %d\n",
23907c2dcfa2SHoratiu Vultur 			   val);
23914488f6b6SDivya Koppera 	phy_unlock_mdio_bus(phydev);
23927c2dcfa2SHoratiu Vultur 	return val;
23937c2dcfa2SHoratiu Vultur }
23947c2dcfa2SHoratiu Vultur 
lan8814_config_ts_intr(struct phy_device * phydev,bool enable)2395ece19502SDivya Koppera static int lan8814_config_ts_intr(struct phy_device *phydev, bool enable)
23967467d716SHoratiu Vultur {
2397ece19502SDivya Koppera 	u16 val = 0;
23987467d716SHoratiu Vultur 
2399ece19502SDivya Koppera 	if (enable)
2400ece19502SDivya Koppera 		val = PTP_TSU_INT_EN_PTP_TX_TS_EN_ |
2401ece19502SDivya Koppera 		      PTP_TSU_INT_EN_PTP_TX_TS_OVRFL_EN_ |
2402ece19502SDivya Koppera 		      PTP_TSU_INT_EN_PTP_RX_TS_EN_ |
2403ece19502SDivya Koppera 		      PTP_TSU_INT_EN_PTP_RX_TS_OVRFL_EN_;
24047467d716SHoratiu Vultur 
2405ece19502SDivya Koppera 	return lanphy_write_page_reg(phydev, 5, PTP_TSU_INT_EN, val);
2406ece19502SDivya Koppera }
24077467d716SHoratiu Vultur 
lan8814_ptp_rx_ts_get(struct phy_device * phydev,u32 * seconds,u32 * nano_seconds,u16 * seq_id)2408ece19502SDivya Koppera static void lan8814_ptp_rx_ts_get(struct phy_device *phydev,
2409ece19502SDivya Koppera 				  u32 *seconds, u32 *nano_seconds, u16 *seq_id)
2410ece19502SDivya Koppera {
2411ece19502SDivya Koppera 	*seconds = lanphy_read_page_reg(phydev, 5, PTP_RX_INGRESS_SEC_HI);
2412ece19502SDivya Koppera 	*seconds = (*seconds << 16) |
2413ece19502SDivya Koppera 		   lanphy_read_page_reg(phydev, 5, PTP_RX_INGRESS_SEC_LO);
2414ece19502SDivya Koppera 
2415ece19502SDivya Koppera 	*nano_seconds = lanphy_read_page_reg(phydev, 5, PTP_RX_INGRESS_NS_HI);
2416ece19502SDivya Koppera 	*nano_seconds = ((*nano_seconds & 0x3fff) << 16) |
2417ece19502SDivya Koppera 			lanphy_read_page_reg(phydev, 5, PTP_RX_INGRESS_NS_LO);
2418ece19502SDivya Koppera 
2419ece19502SDivya Koppera 	*seq_id = lanphy_read_page_reg(phydev, 5, PTP_RX_MSG_HEADER2);
2420ece19502SDivya Koppera }
2421ece19502SDivya Koppera 
lan8814_ptp_tx_ts_get(struct phy_device * phydev,u32 * seconds,u32 * nano_seconds,u16 * seq_id)2422ece19502SDivya Koppera static void lan8814_ptp_tx_ts_get(struct phy_device *phydev,
2423ece19502SDivya Koppera 				  u32 *seconds, u32 *nano_seconds, u16 *seq_id)
2424ece19502SDivya Koppera {
2425ece19502SDivya Koppera 	*seconds = lanphy_read_page_reg(phydev, 5, PTP_TX_EGRESS_SEC_HI);
2426ece19502SDivya Koppera 	*seconds = *seconds << 16 |
2427ece19502SDivya Koppera 		   lanphy_read_page_reg(phydev, 5, PTP_TX_EGRESS_SEC_LO);
2428ece19502SDivya Koppera 
2429ece19502SDivya Koppera 	*nano_seconds = lanphy_read_page_reg(phydev, 5, PTP_TX_EGRESS_NS_HI);
2430ece19502SDivya Koppera 	*nano_seconds = ((*nano_seconds & 0x3fff) << 16) |
2431ece19502SDivya Koppera 			lanphy_read_page_reg(phydev, 5, PTP_TX_EGRESS_NS_LO);
2432ece19502SDivya Koppera 
2433ece19502SDivya Koppera 	*seq_id = lanphy_read_page_reg(phydev, 5, PTP_TX_MSG_HEADER2);
2434ece19502SDivya Koppera }
2435ece19502SDivya Koppera 
lan8814_ts_info(struct mii_timestamper * mii_ts,struct ethtool_ts_info * info)2436ece19502SDivya Koppera static int lan8814_ts_info(struct mii_timestamper *mii_ts, struct ethtool_ts_info *info)
2437ece19502SDivya Koppera {
2438ece19502SDivya Koppera 	struct kszphy_ptp_priv *ptp_priv = container_of(mii_ts, struct kszphy_ptp_priv, mii_ts);
2439ece19502SDivya Koppera 	struct phy_device *phydev = ptp_priv->phydev;
2440ece19502SDivya Koppera 	struct lan8814_shared_priv *shared = phydev->shared->priv;
2441ece19502SDivya Koppera 
2442ece19502SDivya Koppera 	info->so_timestamping = SOF_TIMESTAMPING_TX_HARDWARE |
2443ece19502SDivya Koppera 				SOF_TIMESTAMPING_RX_HARDWARE |
2444ece19502SDivya Koppera 				SOF_TIMESTAMPING_RAW_HARDWARE;
2445ece19502SDivya Koppera 
2446ece19502SDivya Koppera 	info->phc_index = ptp_clock_index(shared->ptp_clock);
2447ece19502SDivya Koppera 
2448ece19502SDivya Koppera 	info->tx_types =
2449ece19502SDivya Koppera 		(1 << HWTSTAMP_TX_OFF) |
2450ece19502SDivya Koppera 		(1 << HWTSTAMP_TX_ON) |
2451ece19502SDivya Koppera 		(1 << HWTSTAMP_TX_ONESTEP_SYNC);
2452ece19502SDivya Koppera 
2453ece19502SDivya Koppera 	info->rx_filters =
2454ece19502SDivya Koppera 		(1 << HWTSTAMP_FILTER_NONE) |
2455ece19502SDivya Koppera 		(1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT) |
2456ece19502SDivya Koppera 		(1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT) |
2457ece19502SDivya Koppera 		(1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
2458ece19502SDivya Koppera 		(1 << HWTSTAMP_FILTER_PTP_V2_EVENT);
24597467d716SHoratiu Vultur 
24607467d716SHoratiu Vultur 	return 0;
24617467d716SHoratiu Vultur }
24627467d716SHoratiu Vultur 
lan8814_flush_fifo(struct phy_device * phydev,bool egress)2463ece19502SDivya Koppera static void lan8814_flush_fifo(struct phy_device *phydev, bool egress)
2464ece19502SDivya Koppera {
2465ece19502SDivya Koppera 	int i;
2466ece19502SDivya Koppera 
2467ece19502SDivya Koppera 	for (i = 0; i < FIFO_SIZE; ++i)
2468ece19502SDivya Koppera 		lanphy_read_page_reg(phydev, 5,
2469ece19502SDivya Koppera 				     egress ? PTP_TX_MSG_HEADER2 : PTP_RX_MSG_HEADER2);
2470ece19502SDivya Koppera 
2471ece19502SDivya Koppera 	/* Read to clear overflow status bit */
2472ece19502SDivya Koppera 	lanphy_read_page_reg(phydev, 5, PTP_TSU_INT_STS);
2473ece19502SDivya Koppera }
2474ece19502SDivya Koppera 
lan8814_hwtstamp(struct mii_timestamper * mii_ts,struct ifreq * ifr)2475ece19502SDivya Koppera static int lan8814_hwtstamp(struct mii_timestamper *mii_ts, struct ifreq *ifr)
2476ece19502SDivya Koppera {
2477ece19502SDivya Koppera 	struct kszphy_ptp_priv *ptp_priv =
2478ece19502SDivya Koppera 			  container_of(mii_ts, struct kszphy_ptp_priv, mii_ts);
2479ece19502SDivya Koppera 	struct phy_device *phydev = ptp_priv->phydev;
2480ece19502SDivya Koppera 	struct lan8814_shared_priv *shared = phydev->shared->priv;
2481ece19502SDivya Koppera 	struct lan8814_ptp_rx_ts *rx_ts, *tmp;
2482ece19502SDivya Koppera 	struct hwtstamp_config config;
2483ece19502SDivya Koppera 	int txcfg = 0, rxcfg = 0;
2484ece19502SDivya Koppera 	int pkt_ts_enable;
24851e304328SHoratiu Vultur 	int tx_mod;
2486ece19502SDivya Koppera 
2487ece19502SDivya Koppera 	if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
2488ece19502SDivya Koppera 		return -EFAULT;
2489ece19502SDivya Koppera 
2490ece19502SDivya Koppera 	ptp_priv->hwts_tx_type = config.tx_type;
2491ece19502SDivya Koppera 	ptp_priv->rx_filter = config.rx_filter;
2492ece19502SDivya Koppera 
2493ece19502SDivya Koppera 	switch (config.rx_filter) {
2494ece19502SDivya Koppera 	case HWTSTAMP_FILTER_NONE:
2495ece19502SDivya Koppera 		ptp_priv->layer = 0;
2496ece19502SDivya Koppera 		ptp_priv->version = 0;
2497ece19502SDivya Koppera 		break;
2498ece19502SDivya Koppera 	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
2499ece19502SDivya Koppera 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
2500ece19502SDivya Koppera 	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
2501ece19502SDivya Koppera 		ptp_priv->layer = PTP_CLASS_L4;
2502ece19502SDivya Koppera 		ptp_priv->version = PTP_CLASS_V2;
2503ece19502SDivya Koppera 		break;
2504ece19502SDivya Koppera 	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
2505ece19502SDivya Koppera 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
2506ece19502SDivya Koppera 	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
2507ece19502SDivya Koppera 		ptp_priv->layer = PTP_CLASS_L2;
2508ece19502SDivya Koppera 		ptp_priv->version = PTP_CLASS_V2;
2509ece19502SDivya Koppera 		break;
2510ece19502SDivya Koppera 	case HWTSTAMP_FILTER_PTP_V2_EVENT:
2511ece19502SDivya Koppera 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
2512ece19502SDivya Koppera 	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
2513ece19502SDivya Koppera 		ptp_priv->layer = PTP_CLASS_L4 | PTP_CLASS_L2;
2514ece19502SDivya Koppera 		ptp_priv->version = PTP_CLASS_V2;
2515ece19502SDivya Koppera 		break;
2516ece19502SDivya Koppera 	default:
2517ece19502SDivya Koppera 		return -ERANGE;
2518ece19502SDivya Koppera 	}
2519ece19502SDivya Koppera 
2520ece19502SDivya Koppera 	if (ptp_priv->layer & PTP_CLASS_L2) {
2521ece19502SDivya Koppera 		rxcfg = PTP_RX_PARSE_CONFIG_LAYER2_EN_;
2522ece19502SDivya Koppera 		txcfg = PTP_TX_PARSE_CONFIG_LAYER2_EN_;
2523ece19502SDivya Koppera 	} else if (ptp_priv->layer & PTP_CLASS_L4) {
2524ece19502SDivya Koppera 		rxcfg |= PTP_RX_PARSE_CONFIG_IPV4_EN_ | PTP_RX_PARSE_CONFIG_IPV6_EN_;
2525ece19502SDivya Koppera 		txcfg |= PTP_TX_PARSE_CONFIG_IPV4_EN_ | PTP_TX_PARSE_CONFIG_IPV6_EN_;
2526ece19502SDivya Koppera 	}
2527ece19502SDivya Koppera 	lanphy_write_page_reg(ptp_priv->phydev, 5, PTP_RX_PARSE_CONFIG, rxcfg);
2528ece19502SDivya Koppera 	lanphy_write_page_reg(ptp_priv->phydev, 5, PTP_TX_PARSE_CONFIG, txcfg);
2529ece19502SDivya Koppera 
2530ece19502SDivya Koppera 	pkt_ts_enable = PTP_TIMESTAMP_EN_SYNC_ | PTP_TIMESTAMP_EN_DREQ_ |
2531ece19502SDivya Koppera 			PTP_TIMESTAMP_EN_PDREQ_ | PTP_TIMESTAMP_EN_PDRES_;
2532ece19502SDivya Koppera 	lanphy_write_page_reg(ptp_priv->phydev, 5, PTP_RX_TIMESTAMP_EN, pkt_ts_enable);
2533ece19502SDivya Koppera 	lanphy_write_page_reg(ptp_priv->phydev, 5, PTP_TX_TIMESTAMP_EN, pkt_ts_enable);
2534ece19502SDivya Koppera 
25351e304328SHoratiu Vultur 	tx_mod = lanphy_read_page_reg(ptp_priv->phydev, 5, PTP_TX_MOD);
25361e304328SHoratiu Vultur 	if (ptp_priv->hwts_tx_type == HWTSTAMP_TX_ONESTEP_SYNC) {
2537ece19502SDivya Koppera 		lanphy_write_page_reg(ptp_priv->phydev, 5, PTP_TX_MOD,
25381e304328SHoratiu Vultur 				      tx_mod | PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_);
25391e304328SHoratiu Vultur 	} else if (ptp_priv->hwts_tx_type == HWTSTAMP_TX_ON) {
25401e304328SHoratiu Vultur 		lanphy_write_page_reg(ptp_priv->phydev, 5, PTP_TX_MOD,
25411e304328SHoratiu Vultur 				      tx_mod & ~PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_);
25421e304328SHoratiu Vultur 	}
2543ece19502SDivya Koppera 
2544ece19502SDivya Koppera 	if (config.rx_filter != HWTSTAMP_FILTER_NONE)
2545ece19502SDivya Koppera 		lan8814_config_ts_intr(ptp_priv->phydev, true);
2546ece19502SDivya Koppera 	else
2547ece19502SDivya Koppera 		lan8814_config_ts_intr(ptp_priv->phydev, false);
2548ece19502SDivya Koppera 
2549ece19502SDivya Koppera 	mutex_lock(&shared->shared_lock);
2550ece19502SDivya Koppera 	if (config.rx_filter != HWTSTAMP_FILTER_NONE)
2551ece19502SDivya Koppera 		shared->ref++;
2552ece19502SDivya Koppera 	else
2553ece19502SDivya Koppera 		shared->ref--;
2554ece19502SDivya Koppera 
2555ece19502SDivya Koppera 	if (shared->ref)
2556ece19502SDivya Koppera 		lanphy_write_page_reg(ptp_priv->phydev, 4, PTP_CMD_CTL,
2557ece19502SDivya Koppera 				      PTP_CMD_CTL_PTP_ENABLE_);
2558ece19502SDivya Koppera 	else
2559ece19502SDivya Koppera 		lanphy_write_page_reg(ptp_priv->phydev, 4, PTP_CMD_CTL,
2560ece19502SDivya Koppera 				      PTP_CMD_CTL_PTP_DISABLE_);
2561ece19502SDivya Koppera 	mutex_unlock(&shared->shared_lock);
2562ece19502SDivya Koppera 
2563ece19502SDivya Koppera 	/* In case of multiple starts and stops, these needs to be cleared */
2564ece19502SDivya Koppera 	list_for_each_entry_safe(rx_ts, tmp, &ptp_priv->rx_ts_list, list) {
2565ece19502SDivya Koppera 		list_del(&rx_ts->list);
2566ece19502SDivya Koppera 		kfree(rx_ts);
2567ece19502SDivya Koppera 	}
2568ece19502SDivya Koppera 	skb_queue_purge(&ptp_priv->rx_queue);
2569ece19502SDivya Koppera 	skb_queue_purge(&ptp_priv->tx_queue);
2570ece19502SDivya Koppera 
2571ece19502SDivya Koppera 	lan8814_flush_fifo(ptp_priv->phydev, false);
2572ece19502SDivya Koppera 	lan8814_flush_fifo(ptp_priv->phydev, true);
2573ece19502SDivya Koppera 
2574ece19502SDivya Koppera 	return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ? -EFAULT : 0;
2575ece19502SDivya Koppera }
2576ece19502SDivya Koppera 
lan8814_txtstamp(struct mii_timestamper * mii_ts,struct sk_buff * skb,int type)2577ece19502SDivya Koppera static void lan8814_txtstamp(struct mii_timestamper *mii_ts,
2578ece19502SDivya Koppera 			     struct sk_buff *skb, int type)
2579ece19502SDivya Koppera {
2580ece19502SDivya Koppera 	struct kszphy_ptp_priv *ptp_priv = container_of(mii_ts, struct kszphy_ptp_priv, mii_ts);
2581ece19502SDivya Koppera 
2582ece19502SDivya Koppera 	switch (ptp_priv->hwts_tx_type) {
2583ece19502SDivya Koppera 	case HWTSTAMP_TX_ONESTEP_SYNC:
25843914a9c0SKurt Kanzenbach 		if (ptp_msg_is_sync(skb, type)) {
2585ece19502SDivya Koppera 			kfree_skb(skb);
2586ece19502SDivya Koppera 			return;
2587ece19502SDivya Koppera 		}
2588ece19502SDivya Koppera 		fallthrough;
2589ece19502SDivya Koppera 	case HWTSTAMP_TX_ON:
2590ece19502SDivya Koppera 		skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
2591ece19502SDivya Koppera 		skb_queue_tail(&ptp_priv->tx_queue, skb);
2592ece19502SDivya Koppera 		break;
2593ece19502SDivya Koppera 	case HWTSTAMP_TX_OFF:
2594ece19502SDivya Koppera 	default:
2595ece19502SDivya Koppera 		kfree_skb(skb);
2596ece19502SDivya Koppera 		break;
2597ece19502SDivya Koppera 	}
2598ece19502SDivya Koppera }
2599ece19502SDivya Koppera 
lan8814_get_sig_rx(struct sk_buff * skb,u16 * sig)260095c1016aSAleksandr Mishin static bool lan8814_get_sig_rx(struct sk_buff *skb, u16 *sig)
2601ece19502SDivya Koppera {
2602ece19502SDivya Koppera 	struct ptp_header *ptp_header;
2603ece19502SDivya Koppera 	u32 type;
2604ece19502SDivya Koppera 
2605ece19502SDivya Koppera 	skb_push(skb, ETH_HLEN);
2606ece19502SDivya Koppera 	type = ptp_classify_raw(skb);
2607ece19502SDivya Koppera 	ptp_header = ptp_parse_header(skb, type);
2608ece19502SDivya Koppera 	skb_pull_inline(skb, ETH_HLEN);
2609ece19502SDivya Koppera 
261095c1016aSAleksandr Mishin 	if (!ptp_header)
261195c1016aSAleksandr Mishin 		return false;
261295c1016aSAleksandr Mishin 
2613ece19502SDivya Koppera 	*sig = (__force u16)(ntohs(ptp_header->sequence_id));
261495c1016aSAleksandr Mishin 	return true;
2615ece19502SDivya Koppera }
2616ece19502SDivya Koppera 
lan8814_match_rx_skb(struct kszphy_ptp_priv * ptp_priv,struct sk_buff * skb)2617cafc3662SHoratiu Vultur static bool lan8814_match_rx_skb(struct kszphy_ptp_priv *ptp_priv,
2618ece19502SDivya Koppera 				 struct sk_buff *skb)
2619ece19502SDivya Koppera {
2620ece19502SDivya Koppera 	struct skb_shared_hwtstamps *shhwtstamps;
2621ece19502SDivya Koppera 	struct lan8814_ptp_rx_ts *rx_ts, *tmp;
2622ece19502SDivya Koppera 	unsigned long flags;
2623ece19502SDivya Koppera 	bool ret = false;
2624ece19502SDivya Koppera 	u16 skb_sig;
2625ece19502SDivya Koppera 
262695c1016aSAleksandr Mishin 	if (!lan8814_get_sig_rx(skb, &skb_sig))
262795c1016aSAleksandr Mishin 		return ret;
2628ece19502SDivya Koppera 
2629ece19502SDivya Koppera 	/* Iterate over all RX timestamps and match it with the received skbs */
2630ece19502SDivya Koppera 	spin_lock_irqsave(&ptp_priv->rx_ts_lock, flags);
2631ece19502SDivya Koppera 	list_for_each_entry_safe(rx_ts, tmp, &ptp_priv->rx_ts_list, list) {
2632ece19502SDivya Koppera 		/* Check if we found the signature we were looking for. */
2633ece19502SDivya Koppera 		if (memcmp(&skb_sig, &rx_ts->seq_id, sizeof(rx_ts->seq_id)))
2634ece19502SDivya Koppera 			continue;
2635ece19502SDivya Koppera 
2636ece19502SDivya Koppera 		shhwtstamps = skb_hwtstamps(skb);
2637ece19502SDivya Koppera 		memset(shhwtstamps, 0, sizeof(*shhwtstamps));
2638ece19502SDivya Koppera 		shhwtstamps->hwtstamp = ktime_set(rx_ts->seconds,
2639ece19502SDivya Koppera 						  rx_ts->nsec);
2640ece19502SDivya Koppera 		list_del(&rx_ts->list);
2641ece19502SDivya Koppera 		kfree(rx_ts);
2642ece19502SDivya Koppera 
2643ece19502SDivya Koppera 		ret = true;
2644ece19502SDivya Koppera 		break;
2645ece19502SDivya Koppera 	}
2646ece19502SDivya Koppera 	spin_unlock_irqrestore(&ptp_priv->rx_ts_lock, flags);
2647ece19502SDivya Koppera 
264867dbd6c0SSebastian Andrzej Siewior 	if (ret)
264967dbd6c0SSebastian Andrzej Siewior 		netif_rx(skb);
2650ece19502SDivya Koppera 	return ret;
2651ece19502SDivya Koppera }
2652ece19502SDivya Koppera 
lan8814_rxtstamp(struct mii_timestamper * mii_ts,struct sk_buff * skb,int type)2653ece19502SDivya Koppera static bool lan8814_rxtstamp(struct mii_timestamper *mii_ts, struct sk_buff *skb, int type)
2654ece19502SDivya Koppera {
2655ece19502SDivya Koppera 	struct kszphy_ptp_priv *ptp_priv =
2656ece19502SDivya Koppera 			container_of(mii_ts, struct kszphy_ptp_priv, mii_ts);
2657ece19502SDivya Koppera 
2658ece19502SDivya Koppera 	if (ptp_priv->rx_filter == HWTSTAMP_FILTER_NONE ||
2659ece19502SDivya Koppera 	    type == PTP_CLASS_NONE)
2660ece19502SDivya Koppera 		return false;
2661ece19502SDivya Koppera 
2662ece19502SDivya Koppera 	if ((type & ptp_priv->version) == 0 || (type & ptp_priv->layer) == 0)
2663ece19502SDivya Koppera 		return false;
2664ece19502SDivya Koppera 
2665ece19502SDivya Koppera 	/* If we failed to match then add it to the queue for when the timestamp
2666ece19502SDivya Koppera 	 * will come
2667ece19502SDivya Koppera 	 */
2668cafc3662SHoratiu Vultur 	if (!lan8814_match_rx_skb(ptp_priv, skb))
2669ece19502SDivya Koppera 		skb_queue_tail(&ptp_priv->rx_queue, skb);
2670ece19502SDivya Koppera 
2671ece19502SDivya Koppera 	return true;
2672ece19502SDivya Koppera }
2673ece19502SDivya Koppera 
lan8814_ptp_clock_set(struct phy_device * phydev,u32 seconds,u32 nano_seconds)2674ece19502SDivya Koppera static void lan8814_ptp_clock_set(struct phy_device *phydev,
2675ece19502SDivya Koppera 				  u32 seconds, u32 nano_seconds)
2676ece19502SDivya Koppera {
2677ece19502SDivya Koppera 	u32 sec_low, sec_high, nsec_low, nsec_high;
2678ece19502SDivya Koppera 
2679ece19502SDivya Koppera 	sec_low = seconds & 0xffff;
2680ece19502SDivya Koppera 	sec_high = (seconds >> 16) & 0xffff;
2681ece19502SDivya Koppera 	nsec_low = nano_seconds & 0xffff;
2682ece19502SDivya Koppera 	nsec_high = (nano_seconds >> 16) & 0x3fff;
2683ece19502SDivya Koppera 
2684ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 4, PTP_CLOCK_SET_SEC_LO, sec_low);
2685ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 4, PTP_CLOCK_SET_SEC_MID, sec_high);
2686ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 4, PTP_CLOCK_SET_NS_LO, nsec_low);
2687ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 4, PTP_CLOCK_SET_NS_HI, nsec_high);
2688ece19502SDivya Koppera 
2689ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 4, PTP_CMD_CTL, PTP_CMD_CTL_PTP_CLOCK_LOAD_);
2690ece19502SDivya Koppera }
2691ece19502SDivya Koppera 
lan8814_ptp_clock_get(struct phy_device * phydev,u32 * seconds,u32 * nano_seconds)2692ece19502SDivya Koppera static void lan8814_ptp_clock_get(struct phy_device *phydev,
2693ece19502SDivya Koppera 				  u32 *seconds, u32 *nano_seconds)
2694ece19502SDivya Koppera {
2695ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 4, PTP_CMD_CTL, PTP_CMD_CTL_PTP_CLOCK_READ_);
2696ece19502SDivya Koppera 
2697ece19502SDivya Koppera 	*seconds = lanphy_read_page_reg(phydev, 4, PTP_CLOCK_READ_SEC_MID);
2698ece19502SDivya Koppera 	*seconds = (*seconds << 16) |
2699ece19502SDivya Koppera 		   lanphy_read_page_reg(phydev, 4, PTP_CLOCK_READ_SEC_LO);
2700ece19502SDivya Koppera 
2701ece19502SDivya Koppera 	*nano_seconds = lanphy_read_page_reg(phydev, 4, PTP_CLOCK_READ_NS_HI);
2702ece19502SDivya Koppera 	*nano_seconds = ((*nano_seconds & 0x3fff) << 16) |
2703ece19502SDivya Koppera 			lanphy_read_page_reg(phydev, 4, PTP_CLOCK_READ_NS_LO);
2704ece19502SDivya Koppera }
2705ece19502SDivya Koppera 
lan8814_ptpci_gettime64(struct ptp_clock_info * ptpci,struct timespec64 * ts)2706ece19502SDivya Koppera static int lan8814_ptpci_gettime64(struct ptp_clock_info *ptpci,
2707ece19502SDivya Koppera 				   struct timespec64 *ts)
2708ece19502SDivya Koppera {
2709ece19502SDivya Koppera 	struct lan8814_shared_priv *shared = container_of(ptpci, struct lan8814_shared_priv,
2710ece19502SDivya Koppera 							  ptp_clock_info);
2711ece19502SDivya Koppera 	struct phy_device *phydev = shared->phydev;
2712ece19502SDivya Koppera 	u32 nano_seconds;
2713ece19502SDivya Koppera 	u32 seconds;
2714ece19502SDivya Koppera 
2715ece19502SDivya Koppera 	mutex_lock(&shared->shared_lock);
2716ece19502SDivya Koppera 	lan8814_ptp_clock_get(phydev, &seconds, &nano_seconds);
2717ece19502SDivya Koppera 	mutex_unlock(&shared->shared_lock);
2718ece19502SDivya Koppera 	ts->tv_sec = seconds;
2719ece19502SDivya Koppera 	ts->tv_nsec = nano_seconds;
2720ece19502SDivya Koppera 
2721ece19502SDivya Koppera 	return 0;
2722ece19502SDivya Koppera }
2723ece19502SDivya Koppera 
lan8814_ptpci_settime64(struct ptp_clock_info * ptpci,const struct timespec64 * ts)2724ece19502SDivya Koppera static int lan8814_ptpci_settime64(struct ptp_clock_info *ptpci,
2725ece19502SDivya Koppera 				   const struct timespec64 *ts)
2726ece19502SDivya Koppera {
2727ece19502SDivya Koppera 	struct lan8814_shared_priv *shared = container_of(ptpci, struct lan8814_shared_priv,
2728ece19502SDivya Koppera 							  ptp_clock_info);
2729ece19502SDivya Koppera 	struct phy_device *phydev = shared->phydev;
2730ece19502SDivya Koppera 
2731ece19502SDivya Koppera 	mutex_lock(&shared->shared_lock);
2732ece19502SDivya Koppera 	lan8814_ptp_clock_set(phydev, ts->tv_sec, ts->tv_nsec);
2733ece19502SDivya Koppera 	mutex_unlock(&shared->shared_lock);
2734ece19502SDivya Koppera 
2735ece19502SDivya Koppera 	return 0;
2736ece19502SDivya Koppera }
2737ece19502SDivya Koppera 
lan8814_ptp_clock_step(struct phy_device * phydev,s64 time_step_ns)2738ece19502SDivya Koppera static void lan8814_ptp_clock_step(struct phy_device *phydev,
2739ece19502SDivya Koppera 				   s64 time_step_ns)
2740ece19502SDivya Koppera {
2741ece19502SDivya Koppera 	u32 nano_seconds_step;
2742ece19502SDivya Koppera 	u64 abs_time_step_ns;
2743ece19502SDivya Koppera 	u32 unsigned_seconds;
2744ece19502SDivya Koppera 	u32 nano_seconds;
2745ece19502SDivya Koppera 	u32 remainder;
2746ece19502SDivya Koppera 	s32 seconds;
2747ece19502SDivya Koppera 
2748ece19502SDivya Koppera 	if (time_step_ns >  15000000000LL) {
2749ece19502SDivya Koppera 		/* convert to clock set */
2750ece19502SDivya Koppera 		lan8814_ptp_clock_get(phydev, &unsigned_seconds, &nano_seconds);
2751ece19502SDivya Koppera 		unsigned_seconds += div_u64_rem(time_step_ns, 1000000000LL,
2752ece19502SDivya Koppera 						&remainder);
2753ece19502SDivya Koppera 		nano_seconds += remainder;
2754ece19502SDivya Koppera 		if (nano_seconds >= 1000000000) {
2755ece19502SDivya Koppera 			unsigned_seconds++;
2756ece19502SDivya Koppera 			nano_seconds -= 1000000000;
2757ece19502SDivya Koppera 		}
2758ece19502SDivya Koppera 		lan8814_ptp_clock_set(phydev, unsigned_seconds, nano_seconds);
2759ece19502SDivya Koppera 		return;
2760ece19502SDivya Koppera 	} else if (time_step_ns < -15000000000LL) {
2761ece19502SDivya Koppera 		/* convert to clock set */
2762ece19502SDivya Koppera 		time_step_ns = -time_step_ns;
2763ece19502SDivya Koppera 
2764ece19502SDivya Koppera 		lan8814_ptp_clock_get(phydev, &unsigned_seconds, &nano_seconds);
2765ece19502SDivya Koppera 		unsigned_seconds -= div_u64_rem(time_step_ns, 1000000000LL,
2766ece19502SDivya Koppera 						&remainder);
2767ece19502SDivya Koppera 		nano_seconds_step = remainder;
2768ece19502SDivya Koppera 		if (nano_seconds < nano_seconds_step) {
2769ece19502SDivya Koppera 			unsigned_seconds--;
2770ece19502SDivya Koppera 			nano_seconds += 1000000000;
2771ece19502SDivya Koppera 		}
2772ece19502SDivya Koppera 		nano_seconds -= nano_seconds_step;
2773ece19502SDivya Koppera 		lan8814_ptp_clock_set(phydev, unsigned_seconds,
2774ece19502SDivya Koppera 				      nano_seconds);
2775ece19502SDivya Koppera 		return;
2776ece19502SDivya Koppera 	}
2777ece19502SDivya Koppera 
2778ece19502SDivya Koppera 	/* do clock step */
2779ece19502SDivya Koppera 	if (time_step_ns >= 0) {
2780ece19502SDivya Koppera 		abs_time_step_ns = (u64)time_step_ns;
2781ece19502SDivya Koppera 		seconds = (s32)div_u64_rem(abs_time_step_ns, 1000000000,
2782ece19502SDivya Koppera 					   &remainder);
2783ece19502SDivya Koppera 		nano_seconds = remainder;
2784ece19502SDivya Koppera 	} else {
2785ece19502SDivya Koppera 		abs_time_step_ns = (u64)(-time_step_ns);
2786ece19502SDivya Koppera 		seconds = -((s32)div_u64_rem(abs_time_step_ns, 1000000000,
2787ece19502SDivya Koppera 			    &remainder));
2788ece19502SDivya Koppera 		nano_seconds = remainder;
2789ece19502SDivya Koppera 		if (nano_seconds > 0) {
2790ece19502SDivya Koppera 			/* subtracting nano seconds is not allowed
2791ece19502SDivya Koppera 			 * convert to subtracting from seconds,
2792ece19502SDivya Koppera 			 * and adding to nanoseconds
2793ece19502SDivya Koppera 			 */
2794ece19502SDivya Koppera 			seconds--;
2795ece19502SDivya Koppera 			nano_seconds = (1000000000 - nano_seconds);
2796ece19502SDivya Koppera 		}
2797ece19502SDivya Koppera 	}
2798ece19502SDivya Koppera 
2799ece19502SDivya Koppera 	if (nano_seconds > 0) {
2800ece19502SDivya Koppera 		/* add 8 ns to cover the likely normal increment */
2801ece19502SDivya Koppera 		nano_seconds += 8;
2802ece19502SDivya Koppera 	}
2803ece19502SDivya Koppera 
2804ece19502SDivya Koppera 	if (nano_seconds >= 1000000000) {
2805ece19502SDivya Koppera 		/* carry into seconds */
2806ece19502SDivya Koppera 		seconds++;
2807ece19502SDivya Koppera 		nano_seconds -= 1000000000;
2808ece19502SDivya Koppera 	}
2809ece19502SDivya Koppera 
2810ece19502SDivya Koppera 	while (seconds) {
2811ece19502SDivya Koppera 		if (seconds > 0) {
2812ece19502SDivya Koppera 			u32 adjustment_value = (u32)seconds;
2813ece19502SDivya Koppera 			u16 adjustment_value_lo, adjustment_value_hi;
2814ece19502SDivya Koppera 
2815ece19502SDivya Koppera 			if (adjustment_value > 0xF)
2816ece19502SDivya Koppera 				adjustment_value = 0xF;
2817ece19502SDivya Koppera 
2818ece19502SDivya Koppera 			adjustment_value_lo = adjustment_value & 0xffff;
2819ece19502SDivya Koppera 			adjustment_value_hi = (adjustment_value >> 16) & 0x3fff;
2820ece19502SDivya Koppera 
2821ece19502SDivya Koppera 			lanphy_write_page_reg(phydev, 4, PTP_LTC_STEP_ADJ_LO,
2822ece19502SDivya Koppera 					      adjustment_value_lo);
2823ece19502SDivya Koppera 			lanphy_write_page_reg(phydev, 4, PTP_LTC_STEP_ADJ_HI,
2824ece19502SDivya Koppera 					      PTP_LTC_STEP_ADJ_DIR_ |
2825ece19502SDivya Koppera 					      adjustment_value_hi);
2826ece19502SDivya Koppera 			seconds -= ((s32)adjustment_value);
2827ece19502SDivya Koppera 		} else {
2828ece19502SDivya Koppera 			u32 adjustment_value = (u32)(-seconds);
2829ece19502SDivya Koppera 			u16 adjustment_value_lo, adjustment_value_hi;
2830ece19502SDivya Koppera 
2831ece19502SDivya Koppera 			if (adjustment_value > 0xF)
2832ece19502SDivya Koppera 				adjustment_value = 0xF;
2833ece19502SDivya Koppera 
2834ece19502SDivya Koppera 			adjustment_value_lo = adjustment_value & 0xffff;
2835ece19502SDivya Koppera 			adjustment_value_hi = (adjustment_value >> 16) & 0x3fff;
2836ece19502SDivya Koppera 
2837ece19502SDivya Koppera 			lanphy_write_page_reg(phydev, 4, PTP_LTC_STEP_ADJ_LO,
2838ece19502SDivya Koppera 					      adjustment_value_lo);
2839ece19502SDivya Koppera 			lanphy_write_page_reg(phydev, 4, PTP_LTC_STEP_ADJ_HI,
2840ece19502SDivya Koppera 					      adjustment_value_hi);
2841ece19502SDivya Koppera 			seconds += ((s32)adjustment_value);
2842ece19502SDivya Koppera 		}
2843ece19502SDivya Koppera 		lanphy_write_page_reg(phydev, 4, PTP_CMD_CTL,
2844ece19502SDivya Koppera 				      PTP_CMD_CTL_PTP_LTC_STEP_SEC_);
2845ece19502SDivya Koppera 	}
2846ece19502SDivya Koppera 	if (nano_seconds) {
2847ece19502SDivya Koppera 		u16 nano_seconds_lo;
2848ece19502SDivya Koppera 		u16 nano_seconds_hi;
2849ece19502SDivya Koppera 
2850ece19502SDivya Koppera 		nano_seconds_lo = nano_seconds & 0xffff;
2851ece19502SDivya Koppera 		nano_seconds_hi = (nano_seconds >> 16) & 0x3fff;
2852ece19502SDivya Koppera 
2853ece19502SDivya Koppera 		lanphy_write_page_reg(phydev, 4, PTP_LTC_STEP_ADJ_LO,
2854ece19502SDivya Koppera 				      nano_seconds_lo);
2855ece19502SDivya Koppera 		lanphy_write_page_reg(phydev, 4, PTP_LTC_STEP_ADJ_HI,
2856ece19502SDivya Koppera 				      PTP_LTC_STEP_ADJ_DIR_ |
2857ece19502SDivya Koppera 				      nano_seconds_hi);
2858ece19502SDivya Koppera 		lanphy_write_page_reg(phydev, 4, PTP_CMD_CTL,
2859ece19502SDivya Koppera 				      PTP_CMD_CTL_PTP_LTC_STEP_NSEC_);
2860ece19502SDivya Koppera 	}
2861ece19502SDivya Koppera }
2862ece19502SDivya Koppera 
lan8814_ptpci_adjtime(struct ptp_clock_info * ptpci,s64 delta)2863ece19502SDivya Koppera static int lan8814_ptpci_adjtime(struct ptp_clock_info *ptpci, s64 delta)
2864ece19502SDivya Koppera {
2865ece19502SDivya Koppera 	struct lan8814_shared_priv *shared = container_of(ptpci, struct lan8814_shared_priv,
2866ece19502SDivya Koppera 							  ptp_clock_info);
2867ece19502SDivya Koppera 	struct phy_device *phydev = shared->phydev;
2868ece19502SDivya Koppera 
2869ece19502SDivya Koppera 	mutex_lock(&shared->shared_lock);
2870ece19502SDivya Koppera 	lan8814_ptp_clock_step(phydev, delta);
2871ece19502SDivya Koppera 	mutex_unlock(&shared->shared_lock);
2872ece19502SDivya Koppera 
2873ece19502SDivya Koppera 	return 0;
2874ece19502SDivya Koppera }
2875ece19502SDivya Koppera 
lan8814_ptpci_adjfine(struct ptp_clock_info * ptpci,long scaled_ppm)2876ece19502SDivya Koppera static int lan8814_ptpci_adjfine(struct ptp_clock_info *ptpci, long scaled_ppm)
2877ece19502SDivya Koppera {
2878ece19502SDivya Koppera 	struct lan8814_shared_priv *shared = container_of(ptpci, struct lan8814_shared_priv,
2879ece19502SDivya Koppera 							  ptp_clock_info);
2880ece19502SDivya Koppera 	struct phy_device *phydev = shared->phydev;
2881ece19502SDivya Koppera 	u16 kszphy_rate_adj_lo, kszphy_rate_adj_hi;
2882ece19502SDivya Koppera 	bool positive = true;
2883ece19502SDivya Koppera 	u32 kszphy_rate_adj;
2884ece19502SDivya Koppera 
2885ece19502SDivya Koppera 	if (scaled_ppm < 0) {
2886ece19502SDivya Koppera 		scaled_ppm = -scaled_ppm;
2887ece19502SDivya Koppera 		positive = false;
2888ece19502SDivya Koppera 	}
2889ece19502SDivya Koppera 
2890ece19502SDivya Koppera 	kszphy_rate_adj = LAN8814_1PPM_FORMAT * (scaled_ppm >> 16);
2891ece19502SDivya Koppera 	kszphy_rate_adj += (LAN8814_1PPM_FORMAT * (0xffff & scaled_ppm)) >> 16;
2892ece19502SDivya Koppera 
2893ece19502SDivya Koppera 	kszphy_rate_adj_lo = kszphy_rate_adj & 0xffff;
2894ece19502SDivya Koppera 	kszphy_rate_adj_hi = (kszphy_rate_adj >> 16) & 0x3fff;
2895ece19502SDivya Koppera 
2896ece19502SDivya Koppera 	if (positive)
2897ece19502SDivya Koppera 		kszphy_rate_adj_hi |= PTP_CLOCK_RATE_ADJ_DIR_;
2898ece19502SDivya Koppera 
2899ece19502SDivya Koppera 	mutex_lock(&shared->shared_lock);
2900ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 4, PTP_CLOCK_RATE_ADJ_HI, kszphy_rate_adj_hi);
2901ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 4, PTP_CLOCK_RATE_ADJ_LO, kszphy_rate_adj_lo);
2902ece19502SDivya Koppera 	mutex_unlock(&shared->shared_lock);
2903ece19502SDivya Koppera 
2904ece19502SDivya Koppera 	return 0;
2905ece19502SDivya Koppera }
2906ece19502SDivya Koppera 
lan8814_get_sig_tx(struct sk_buff * skb,u16 * sig)290795c1016aSAleksandr Mishin static bool lan8814_get_sig_tx(struct sk_buff *skb, u16 *sig)
2908ece19502SDivya Koppera {
2909ece19502SDivya Koppera 	struct ptp_header *ptp_header;
2910ece19502SDivya Koppera 	u32 type;
2911ece19502SDivya Koppera 
2912ece19502SDivya Koppera 	type = ptp_classify_raw(skb);
2913ece19502SDivya Koppera 	ptp_header = ptp_parse_header(skb, type);
2914ece19502SDivya Koppera 
291595c1016aSAleksandr Mishin 	if (!ptp_header)
291695c1016aSAleksandr Mishin 		return false;
291795c1016aSAleksandr Mishin 
2918ece19502SDivya Koppera 	*sig = (__force u16)(ntohs(ptp_header->sequence_id));
291995c1016aSAleksandr Mishin 	return true;
2920ece19502SDivya Koppera }
2921ece19502SDivya Koppera 
lan8814_match_tx_skb(struct kszphy_ptp_priv * ptp_priv,u32 seconds,u32 nsec,u16 seq_id)2922cafc3662SHoratiu Vultur static void lan8814_match_tx_skb(struct kszphy_ptp_priv *ptp_priv,
2923cafc3662SHoratiu Vultur 				 u32 seconds, u32 nsec, u16 seq_id)
2924ece19502SDivya Koppera {
2925ece19502SDivya Koppera 	struct skb_shared_hwtstamps shhwtstamps;
2926ece19502SDivya Koppera 	struct sk_buff *skb, *skb_tmp;
2927ece19502SDivya Koppera 	unsigned long flags;
2928ece19502SDivya Koppera 	bool ret = false;
2929ece19502SDivya Koppera 	u16 skb_sig;
2930ece19502SDivya Koppera 
2931ece19502SDivya Koppera 	spin_lock_irqsave(&ptp_priv->tx_queue.lock, flags);
2932ece19502SDivya Koppera 	skb_queue_walk_safe(&ptp_priv->tx_queue, skb, skb_tmp) {
293395c1016aSAleksandr Mishin 		if (!lan8814_get_sig_tx(skb, &skb_sig))
293495c1016aSAleksandr Mishin 			continue;
2935ece19502SDivya Koppera 
2936ece19502SDivya Koppera 		if (memcmp(&skb_sig, &seq_id, sizeof(seq_id)))
2937ece19502SDivya Koppera 			continue;
2938ece19502SDivya Koppera 
2939ece19502SDivya Koppera 		__skb_unlink(skb, &ptp_priv->tx_queue);
2940ece19502SDivya Koppera 		ret = true;
2941ece19502SDivya Koppera 		break;
2942ece19502SDivya Koppera 	}
2943ece19502SDivya Koppera 	spin_unlock_irqrestore(&ptp_priv->tx_queue.lock, flags);
2944ece19502SDivya Koppera 
2945ece19502SDivya Koppera 	if (ret) {
2946ece19502SDivya Koppera 		memset(&shhwtstamps, 0, sizeof(shhwtstamps));
2947ece19502SDivya Koppera 		shhwtstamps.hwtstamp = ktime_set(seconds, nsec);
2948ece19502SDivya Koppera 		skb_complete_tx_timestamp(skb, &shhwtstamps);
2949ece19502SDivya Koppera 	}
2950ece19502SDivya Koppera }
2951ece19502SDivya Koppera 
lan8814_dequeue_tx_skb(struct kszphy_ptp_priv * ptp_priv)2952cafc3662SHoratiu Vultur static void lan8814_dequeue_tx_skb(struct kszphy_ptp_priv *ptp_priv)
2953cafc3662SHoratiu Vultur {
2954cafc3662SHoratiu Vultur 	struct phy_device *phydev = ptp_priv->phydev;
2955cafc3662SHoratiu Vultur 	u32 seconds, nsec;
2956cafc3662SHoratiu Vultur 	u16 seq_id;
2957cafc3662SHoratiu Vultur 
2958cafc3662SHoratiu Vultur 	lan8814_ptp_tx_ts_get(phydev, &seconds, &nsec, &seq_id);
2959cafc3662SHoratiu Vultur 	lan8814_match_tx_skb(ptp_priv, seconds, nsec, seq_id);
2960cafc3662SHoratiu Vultur }
2961cafc3662SHoratiu Vultur 
lan8814_get_tx_ts(struct kszphy_ptp_priv * ptp_priv)2962ece19502SDivya Koppera static void lan8814_get_tx_ts(struct kszphy_ptp_priv *ptp_priv)
2963ece19502SDivya Koppera {
2964ece19502SDivya Koppera 	struct phy_device *phydev = ptp_priv->phydev;
2965ece19502SDivya Koppera 	u32 reg;
2966ece19502SDivya Koppera 
2967ece19502SDivya Koppera 	do {
2968ece19502SDivya Koppera 		lan8814_dequeue_tx_skb(ptp_priv);
2969ece19502SDivya Koppera 
2970ece19502SDivya Koppera 		/* If other timestamps are available in the FIFO,
2971ece19502SDivya Koppera 		 * process them.
2972ece19502SDivya Koppera 		 */
2973ece19502SDivya Koppera 		reg = lanphy_read_page_reg(phydev, 5, PTP_CAP_INFO);
2974ece19502SDivya Koppera 	} while (PTP_CAP_INFO_TX_TS_CNT_GET_(reg) > 0);
2975ece19502SDivya Koppera }
2976ece19502SDivya Koppera 
lan8814_match_skb(struct kszphy_ptp_priv * ptp_priv,struct lan8814_ptp_rx_ts * rx_ts)2977ece19502SDivya Koppera static bool lan8814_match_skb(struct kszphy_ptp_priv *ptp_priv,
2978ece19502SDivya Koppera 			      struct lan8814_ptp_rx_ts *rx_ts)
2979ece19502SDivya Koppera {
2980ece19502SDivya Koppera 	struct skb_shared_hwtstamps *shhwtstamps;
2981ece19502SDivya Koppera 	struct sk_buff *skb, *skb_tmp;
2982ece19502SDivya Koppera 	unsigned long flags;
2983ece19502SDivya Koppera 	bool ret = false;
2984ece19502SDivya Koppera 	u16 skb_sig;
2985ece19502SDivya Koppera 
2986ece19502SDivya Koppera 	spin_lock_irqsave(&ptp_priv->rx_queue.lock, flags);
2987ece19502SDivya Koppera 	skb_queue_walk_safe(&ptp_priv->rx_queue, skb, skb_tmp) {
298895c1016aSAleksandr Mishin 		if (!lan8814_get_sig_rx(skb, &skb_sig))
298995c1016aSAleksandr Mishin 			continue;
2990ece19502SDivya Koppera 
2991ece19502SDivya Koppera 		if (memcmp(&skb_sig, &rx_ts->seq_id, sizeof(rx_ts->seq_id)))
2992ece19502SDivya Koppera 			continue;
2993ece19502SDivya Koppera 
2994ece19502SDivya Koppera 		__skb_unlink(skb, &ptp_priv->rx_queue);
2995ece19502SDivya Koppera 
2996ece19502SDivya Koppera 		ret = true;
2997ece19502SDivya Koppera 		break;
2998ece19502SDivya Koppera 	}
2999ece19502SDivya Koppera 	spin_unlock_irqrestore(&ptp_priv->rx_queue.lock, flags);
3000ece19502SDivya Koppera 
3001ece19502SDivya Koppera 	if (ret) {
3002ece19502SDivya Koppera 		shhwtstamps = skb_hwtstamps(skb);
3003ece19502SDivya Koppera 		memset(shhwtstamps, 0, sizeof(*shhwtstamps));
3004ece19502SDivya Koppera 		shhwtstamps->hwtstamp = ktime_set(rx_ts->seconds, rx_ts->nsec);
3005e1f9e434SSebastian Andrzej Siewior 		netif_rx(skb);
3006ece19502SDivya Koppera 	}
3007ece19502SDivya Koppera 
3008ece19502SDivya Koppera 	return ret;
3009ece19502SDivya Koppera }
3010ece19502SDivya Koppera 
lan8814_match_rx_ts(struct kszphy_ptp_priv * ptp_priv,struct lan8814_ptp_rx_ts * rx_ts)3011cafc3662SHoratiu Vultur static void lan8814_match_rx_ts(struct kszphy_ptp_priv *ptp_priv,
3012cafc3662SHoratiu Vultur 				struct lan8814_ptp_rx_ts *rx_ts)
3013ece19502SDivya Koppera {
3014ece19502SDivya Koppera 	unsigned long flags;
3015ece19502SDivya Koppera 
3016ece19502SDivya Koppera 	/* If we failed to match the skb add it to the queue for when
3017ece19502SDivya Koppera 	 * the frame will come
3018ece19502SDivya Koppera 	 */
3019ece19502SDivya Koppera 	if (!lan8814_match_skb(ptp_priv, rx_ts)) {
3020ece19502SDivya Koppera 		spin_lock_irqsave(&ptp_priv->rx_ts_lock, flags);
3021ece19502SDivya Koppera 		list_add(&rx_ts->list, &ptp_priv->rx_ts_list);
3022ece19502SDivya Koppera 		spin_unlock_irqrestore(&ptp_priv->rx_ts_lock, flags);
3023ece19502SDivya Koppera 	} else {
3024ece19502SDivya Koppera 		kfree(rx_ts);
3025ece19502SDivya Koppera 	}
3026cafc3662SHoratiu Vultur }
3027cafc3662SHoratiu Vultur 
lan8814_get_rx_ts(struct kszphy_ptp_priv * ptp_priv)3028cafc3662SHoratiu Vultur static void lan8814_get_rx_ts(struct kszphy_ptp_priv *ptp_priv)
3029cafc3662SHoratiu Vultur {
3030cafc3662SHoratiu Vultur 	struct phy_device *phydev = ptp_priv->phydev;
3031cafc3662SHoratiu Vultur 	struct lan8814_ptp_rx_ts *rx_ts;
3032cafc3662SHoratiu Vultur 	u32 reg;
3033cafc3662SHoratiu Vultur 
3034cafc3662SHoratiu Vultur 	do {
3035cafc3662SHoratiu Vultur 		rx_ts = kzalloc(sizeof(*rx_ts), GFP_KERNEL);
3036cafc3662SHoratiu Vultur 		if (!rx_ts)
3037cafc3662SHoratiu Vultur 			return;
3038cafc3662SHoratiu Vultur 
3039cafc3662SHoratiu Vultur 		lan8814_ptp_rx_ts_get(phydev, &rx_ts->seconds, &rx_ts->nsec,
3040cafc3662SHoratiu Vultur 				      &rx_ts->seq_id);
3041cafc3662SHoratiu Vultur 		lan8814_match_rx_ts(ptp_priv, rx_ts);
3042ece19502SDivya Koppera 
3043ece19502SDivya Koppera 		/* If other timestamps are available in the FIFO,
3044ece19502SDivya Koppera 		 * process them.
3045ece19502SDivya Koppera 		 */
3046ece19502SDivya Koppera 		reg = lanphy_read_page_reg(phydev, 5, PTP_CAP_INFO);
3047ece19502SDivya Koppera 	} while (PTP_CAP_INFO_RX_TS_CNT_GET_(reg) > 0);
3048ece19502SDivya Koppera }
3049ece19502SDivya Koppera 
lan8814_handle_ptp_interrupt(struct phy_device * phydev,u16 status)30507abd92a5SHoratiu Vultur static void lan8814_handle_ptp_interrupt(struct phy_device *phydev, u16 status)
3051ece19502SDivya Koppera {
3052ece19502SDivya Koppera 	struct kszphy_priv *priv = phydev->priv;
3053ece19502SDivya Koppera 	struct kszphy_ptp_priv *ptp_priv = &priv->ptp_priv;
3054ece19502SDivya Koppera 
3055ece19502SDivya Koppera 	if (status & PTP_TSU_INT_STS_PTP_TX_TS_EN_)
3056ece19502SDivya Koppera 		lan8814_get_tx_ts(ptp_priv);
3057ece19502SDivya Koppera 
3058ece19502SDivya Koppera 	if (status & PTP_TSU_INT_STS_PTP_RX_TS_EN_)
3059ece19502SDivya Koppera 		lan8814_get_rx_ts(ptp_priv);
3060ece19502SDivya Koppera 
3061ece19502SDivya Koppera 	if (status & PTP_TSU_INT_STS_PTP_TX_TS_OVRFL_INT_) {
3062ece19502SDivya Koppera 		lan8814_flush_fifo(phydev, true);
3063ece19502SDivya Koppera 		skb_queue_purge(&ptp_priv->tx_queue);
3064ece19502SDivya Koppera 	}
3065ece19502SDivya Koppera 
3066ece19502SDivya Koppera 	if (status & PTP_TSU_INT_STS_PTP_RX_TS_OVRFL_INT_) {
3067ece19502SDivya Koppera 		lan8814_flush_fifo(phydev, false);
3068ece19502SDivya Koppera 		skb_queue_purge(&ptp_priv->rx_queue);
3069ece19502SDivya Koppera 	}
3070ece19502SDivya Koppera }
3071ece19502SDivya Koppera 
lan8804_config_init(struct phy_device * phydev)30727c2dcfa2SHoratiu Vultur static int lan8804_config_init(struct phy_device *phydev)
30737c2dcfa2SHoratiu Vultur {
30747c2dcfa2SHoratiu Vultur 	int val;
30757c2dcfa2SHoratiu Vultur 
30767c2dcfa2SHoratiu Vultur 	/* MDI-X setting for swap A,B transmit */
30777c2dcfa2SHoratiu Vultur 	val = lanphy_read_page_reg(phydev, 2, LAN8804_ALIGN_SWAP);
30787c2dcfa2SHoratiu Vultur 	val &= ~LAN8804_ALIGN_TX_A_B_SWAP_MASK;
30797c2dcfa2SHoratiu Vultur 	val |= LAN8804_ALIGN_TX_A_B_SWAP;
30807c2dcfa2SHoratiu Vultur 	lanphy_write_page_reg(phydev, 2, LAN8804_ALIGN_SWAP, val);
30817c2dcfa2SHoratiu Vultur 
30827c2dcfa2SHoratiu Vultur 	/* Make sure that the PHY will not stop generating the clock when the
30837c2dcfa2SHoratiu Vultur 	 * link partner goes down
30847c2dcfa2SHoratiu Vultur 	 */
30857c2dcfa2SHoratiu Vultur 	lanphy_write_page_reg(phydev, 31, LAN8814_CLOCK_MANAGEMENT, 0x27e);
30867c2dcfa2SHoratiu Vultur 	lanphy_read_page_reg(phydev, 1, LAN8814_LINK_QUALITY);
30877c2dcfa2SHoratiu Vultur 
30887c2dcfa2SHoratiu Vultur 	return 0;
30897c2dcfa2SHoratiu Vultur }
30907c2dcfa2SHoratiu Vultur 
lan8804_handle_interrupt(struct phy_device * phydev)3091b324c6e5SHoratiu Vultur static irqreturn_t lan8804_handle_interrupt(struct phy_device *phydev)
3092b324c6e5SHoratiu Vultur {
3093b324c6e5SHoratiu Vultur 	int status;
3094b324c6e5SHoratiu Vultur 
3095b324c6e5SHoratiu Vultur 	status = phy_read(phydev, LAN8814_INTS);
3096b324c6e5SHoratiu Vultur 	if (status < 0) {
3097b324c6e5SHoratiu Vultur 		phy_error(phydev);
3098b324c6e5SHoratiu Vultur 		return IRQ_NONE;
3099b324c6e5SHoratiu Vultur 	}
3100b324c6e5SHoratiu Vultur 
3101b324c6e5SHoratiu Vultur 	if (status > 0)
3102b324c6e5SHoratiu Vultur 		phy_trigger_machine(phydev);
3103b324c6e5SHoratiu Vultur 
3104b324c6e5SHoratiu Vultur 	return IRQ_HANDLED;
3105b324c6e5SHoratiu Vultur }
3106b324c6e5SHoratiu Vultur 
3107b324c6e5SHoratiu Vultur #define LAN8804_OUTPUT_CONTROL			25
3108b324c6e5SHoratiu Vultur #define LAN8804_OUTPUT_CONTROL_INTR_BUFFER	BIT(14)
3109b324c6e5SHoratiu Vultur #define LAN8804_CONTROL				31
3110b324c6e5SHoratiu Vultur #define LAN8804_CONTROL_INTR_POLARITY		BIT(14)
3111b324c6e5SHoratiu Vultur 
lan8804_config_intr(struct phy_device * phydev)3112b324c6e5SHoratiu Vultur static int lan8804_config_intr(struct phy_device *phydev)
3113b324c6e5SHoratiu Vultur {
3114b324c6e5SHoratiu Vultur 	int err;
3115b324c6e5SHoratiu Vultur 
3116b324c6e5SHoratiu Vultur 	/* This is an internal PHY of lan966x and is not possible to change the
3117b324c6e5SHoratiu Vultur 	 * polarity on the GIC found in lan966x, therefore change the polarity
3118b324c6e5SHoratiu Vultur 	 * of the interrupt in the PHY from being active low instead of active
3119b324c6e5SHoratiu Vultur 	 * high.
3120b324c6e5SHoratiu Vultur 	 */
3121b324c6e5SHoratiu Vultur 	phy_write(phydev, LAN8804_CONTROL, LAN8804_CONTROL_INTR_POLARITY);
3122b324c6e5SHoratiu Vultur 
3123b324c6e5SHoratiu Vultur 	/* By default interrupt buffer is open-drain in which case the interrupt
3124b324c6e5SHoratiu Vultur 	 * can be active only low. Therefore change the interrupt buffer to be
3125b324c6e5SHoratiu Vultur 	 * push-pull to be able to change interrupt polarity
3126b324c6e5SHoratiu Vultur 	 */
3127b324c6e5SHoratiu Vultur 	phy_write(phydev, LAN8804_OUTPUT_CONTROL,
3128b324c6e5SHoratiu Vultur 		  LAN8804_OUTPUT_CONTROL_INTR_BUFFER);
3129b324c6e5SHoratiu Vultur 
3130b324c6e5SHoratiu Vultur 	if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
3131b324c6e5SHoratiu Vultur 		err = phy_read(phydev, LAN8814_INTS);
3132b324c6e5SHoratiu Vultur 		if (err < 0)
3133b324c6e5SHoratiu Vultur 			return err;
3134b324c6e5SHoratiu Vultur 
3135b324c6e5SHoratiu Vultur 		err = phy_write(phydev, LAN8814_INTC, LAN8814_INT_LINK);
3136b324c6e5SHoratiu Vultur 		if (err)
3137b324c6e5SHoratiu Vultur 			return err;
3138b324c6e5SHoratiu Vultur 	} else {
3139b324c6e5SHoratiu Vultur 		err = phy_write(phydev, LAN8814_INTC, 0);
3140b324c6e5SHoratiu Vultur 		if (err)
3141b324c6e5SHoratiu Vultur 			return err;
3142b324c6e5SHoratiu Vultur 
3143b324c6e5SHoratiu Vultur 		err = phy_read(phydev, LAN8814_INTS);
3144b324c6e5SHoratiu Vultur 		if (err < 0)
3145b324c6e5SHoratiu Vultur 			return err;
3146b324c6e5SHoratiu Vultur 	}
3147b324c6e5SHoratiu Vultur 
3148b324c6e5SHoratiu Vultur 	return 0;
3149b324c6e5SHoratiu Vultur }
3150b324c6e5SHoratiu Vultur 
lan8814_handle_interrupt(struct phy_device * phydev)3151b3ec7248SDivya Koppera static irqreturn_t lan8814_handle_interrupt(struct phy_device *phydev)
3152b3ec7248SDivya Koppera {
31532002fbacSMichael Walle 	int ret = IRQ_NONE;
31547abd92a5SHoratiu Vultur 	int irq_status;
3155b3ec7248SDivya Koppera 
3156b3ec7248SDivya Koppera 	irq_status = phy_read(phydev, LAN8814_INTS);
3157ece19502SDivya Koppera 	if (irq_status < 0) {
3158ece19502SDivya Koppera 		phy_error(phydev);
3159ece19502SDivya Koppera 		return IRQ_NONE;
3160ece19502SDivya Koppera 	}
3161ece19502SDivya Koppera 
31622002fbacSMichael Walle 	if (irq_status & LAN8814_INT_LINK) {
31632002fbacSMichael Walle 		phy_trigger_machine(phydev);
31642002fbacSMichael Walle 		ret = IRQ_HANDLED;
31652002fbacSMichael Walle 	}
31662002fbacSMichael Walle 
31677abd92a5SHoratiu Vultur 	while (true) {
31687abd92a5SHoratiu Vultur 		irq_status = lanphy_read_page_reg(phydev, 5, PTP_TSU_INT_STS);
31697abd92a5SHoratiu Vultur 		if (!irq_status)
3170ece19502SDivya Koppera 			break;
31717abd92a5SHoratiu Vultur 
31727abd92a5SHoratiu Vultur 		lan8814_handle_ptp_interrupt(phydev, irq_status);
31737abd92a5SHoratiu Vultur 		ret = IRQ_HANDLED;
31742002fbacSMichael Walle 	}
31752002fbacSMichael Walle 
31762002fbacSMichael Walle 	return ret;
3177b3ec7248SDivya Koppera }
3178b3ec7248SDivya Koppera 
lan8814_ack_interrupt(struct phy_device * phydev)3179b3ec7248SDivya Koppera static int lan8814_ack_interrupt(struct phy_device *phydev)
3180b3ec7248SDivya Koppera {
3181b3ec7248SDivya Koppera 	/* bit[12..0] int status, which is a read and clear register. */
3182b3ec7248SDivya Koppera 	int rc;
3183b3ec7248SDivya Koppera 
3184b3ec7248SDivya Koppera 	rc = phy_read(phydev, LAN8814_INTS);
3185b3ec7248SDivya Koppera 
3186b3ec7248SDivya Koppera 	return (rc < 0) ? rc : 0;
3187b3ec7248SDivya Koppera }
3188b3ec7248SDivya Koppera 
lan8814_config_intr(struct phy_device * phydev)3189b3ec7248SDivya Koppera static int lan8814_config_intr(struct phy_device *phydev)
3190b3ec7248SDivya Koppera {
3191b3ec7248SDivya Koppera 	int err;
3192b3ec7248SDivya Koppera 
3193b3ec7248SDivya Koppera 	lanphy_write_page_reg(phydev, 4, LAN8814_INTR_CTRL_REG,
3194b3ec7248SDivya Koppera 			      LAN8814_INTR_CTRL_REG_POLARITY |
3195b3ec7248SDivya Koppera 			      LAN8814_INTR_CTRL_REG_INTR_ENABLE);
3196b3ec7248SDivya Koppera 
3197b3ec7248SDivya Koppera 	/* enable / disable interrupts */
3198b3ec7248SDivya Koppera 	if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
3199b3ec7248SDivya Koppera 		err = lan8814_ack_interrupt(phydev);
3200b3ec7248SDivya Koppera 		if (err)
3201b3ec7248SDivya Koppera 			return err;
3202b3ec7248SDivya Koppera 
3203b3ec7248SDivya Koppera 		err = phy_write(phydev, LAN8814_INTC, LAN8814_INT_LINK);
3204b3ec7248SDivya Koppera 	} else {
3205b3ec7248SDivya Koppera 		err = phy_write(phydev, LAN8814_INTC, 0);
3206b3ec7248SDivya Koppera 		if (err)
3207b3ec7248SDivya Koppera 			return err;
3208b3ec7248SDivya Koppera 
3209b3ec7248SDivya Koppera 		err = lan8814_ack_interrupt(phydev);
3210b3ec7248SDivya Koppera 	}
3211b3ec7248SDivya Koppera 
3212b3ec7248SDivya Koppera 	return err;
3213b3ec7248SDivya Koppera }
3214b3ec7248SDivya Koppera 
lan8814_ptp_init(struct phy_device * phydev)3215ece19502SDivya Koppera static void lan8814_ptp_init(struct phy_device *phydev)
3216ece19502SDivya Koppera {
3217ece19502SDivya Koppera 	struct kszphy_priv *priv = phydev->priv;
3218ece19502SDivya Koppera 	struct kszphy_ptp_priv *ptp_priv = &priv->ptp_priv;
3219ece19502SDivya Koppera 	u32 temp;
3220ece19502SDivya Koppera 
322131d00ca4SMichael Walle 	if (!IS_ENABLED(CONFIG_PTP_1588_CLOCK) ||
322231d00ca4SMichael Walle 	    !IS_ENABLED(CONFIG_NETWORK_PHY_TIMESTAMPING))
322331d00ca4SMichael Walle 		return;
322431d00ca4SMichael Walle 
3225ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 5, TSU_HARD_RESET, TSU_HARD_RESET_);
3226ece19502SDivya Koppera 
3227ece19502SDivya Koppera 	temp = lanphy_read_page_reg(phydev, 5, PTP_TX_MOD);
3228ece19502SDivya Koppera 	temp |= PTP_TX_MOD_BAD_UDPV4_CHKSUM_FORCE_FCS_DIS_;
3229ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 5, PTP_TX_MOD, temp);
3230ece19502SDivya Koppera 
3231ece19502SDivya Koppera 	temp = lanphy_read_page_reg(phydev, 5, PTP_RX_MOD);
3232ece19502SDivya Koppera 	temp |= PTP_RX_MOD_BAD_UDPV4_CHKSUM_FORCE_FCS_DIS_;
3233ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 5, PTP_RX_MOD, temp);
3234ece19502SDivya Koppera 
3235ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 5, PTP_RX_PARSE_CONFIG, 0);
3236ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 5, PTP_TX_PARSE_CONFIG, 0);
3237ece19502SDivya Koppera 
3238ece19502SDivya Koppera 	/* Removing default registers configs related to L2 and IP */
3239ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 5, PTP_TX_PARSE_L2_ADDR_EN, 0);
3240ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 5, PTP_RX_PARSE_L2_ADDR_EN, 0);
3241ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 5, PTP_TX_PARSE_IP_ADDR_EN, 0);
3242ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 5, PTP_RX_PARSE_IP_ADDR_EN, 0);
3243ece19502SDivya Koppera 
3244784207bdSHoratiu Vultur 	/* Disable checking for minorVersionPTP field */
3245784207bdSHoratiu Vultur 	lanphy_write_page_reg(phydev, 5, PTP_RX_VERSION,
3246784207bdSHoratiu Vultur 			      PTP_MAX_VERSION(0xff) | PTP_MIN_VERSION(0x0));
3247784207bdSHoratiu Vultur 	lanphy_write_page_reg(phydev, 5, PTP_TX_VERSION,
3248784207bdSHoratiu Vultur 			      PTP_MAX_VERSION(0xff) | PTP_MIN_VERSION(0x0));
3249784207bdSHoratiu Vultur 
3250ece19502SDivya Koppera 	skb_queue_head_init(&ptp_priv->tx_queue);
3251ece19502SDivya Koppera 	skb_queue_head_init(&ptp_priv->rx_queue);
3252ece19502SDivya Koppera 	INIT_LIST_HEAD(&ptp_priv->rx_ts_list);
3253ece19502SDivya Koppera 	spin_lock_init(&ptp_priv->rx_ts_lock);
3254ece19502SDivya Koppera 
3255ece19502SDivya Koppera 	ptp_priv->phydev = phydev;
3256ece19502SDivya Koppera 
3257ece19502SDivya Koppera 	ptp_priv->mii_ts.rxtstamp = lan8814_rxtstamp;
3258ece19502SDivya Koppera 	ptp_priv->mii_ts.txtstamp = lan8814_txtstamp;
3259ece19502SDivya Koppera 	ptp_priv->mii_ts.hwtstamp = lan8814_hwtstamp;
3260ece19502SDivya Koppera 	ptp_priv->mii_ts.ts_info  = lan8814_ts_info;
3261ece19502SDivya Koppera 
3262ece19502SDivya Koppera 	phydev->mii_ts = &ptp_priv->mii_ts;
3263ece19502SDivya Koppera }
3264ece19502SDivya Koppera 
lan8814_ptp_probe_once(struct phy_device * phydev)3265ece19502SDivya Koppera static int lan8814_ptp_probe_once(struct phy_device *phydev)
3266ece19502SDivya Koppera {
3267ece19502SDivya Koppera 	struct lan8814_shared_priv *shared = phydev->shared->priv;
3268ece19502SDivya Koppera 
3269ece19502SDivya Koppera 	/* Initialise shared lock for clock*/
3270ece19502SDivya Koppera 	mutex_init(&shared->shared_lock);
3271ece19502SDivya Koppera 
3272ece19502SDivya Koppera 	shared->ptp_clock_info.owner = THIS_MODULE;
3273ece19502SDivya Koppera 	snprintf(shared->ptp_clock_info.name, 30, "%s", phydev->drv->name);
3274ece19502SDivya Koppera 	shared->ptp_clock_info.max_adj = 31249999;
3275ece19502SDivya Koppera 	shared->ptp_clock_info.n_alarm = 0;
3276ece19502SDivya Koppera 	shared->ptp_clock_info.n_ext_ts = 0;
3277ece19502SDivya Koppera 	shared->ptp_clock_info.n_pins = 0;
3278ece19502SDivya Koppera 	shared->ptp_clock_info.pps = 0;
3279ece19502SDivya Koppera 	shared->ptp_clock_info.pin_config = NULL;
3280ece19502SDivya Koppera 	shared->ptp_clock_info.adjfine = lan8814_ptpci_adjfine;
3281ece19502SDivya Koppera 	shared->ptp_clock_info.adjtime = lan8814_ptpci_adjtime;
3282ece19502SDivya Koppera 	shared->ptp_clock_info.gettime64 = lan8814_ptpci_gettime64;
3283ece19502SDivya Koppera 	shared->ptp_clock_info.settime64 = lan8814_ptpci_settime64;
3284ece19502SDivya Koppera 	shared->ptp_clock_info.getcrosststamp = NULL;
3285ece19502SDivya Koppera 
3286ece19502SDivya Koppera 	shared->ptp_clock = ptp_clock_register(&shared->ptp_clock_info,
3287ece19502SDivya Koppera 					       &phydev->mdio.dev);
32883f88d7d1SDivya Koppera 	if (IS_ERR(shared->ptp_clock)) {
3289ece19502SDivya Koppera 		phydev_err(phydev, "ptp_clock_register failed %lu\n",
3290ece19502SDivya Koppera 			   PTR_ERR(shared->ptp_clock));
3291ece19502SDivya Koppera 		return -EINVAL;
3292ece19502SDivya Koppera 	}
3293ece19502SDivya Koppera 
32943f88d7d1SDivya Koppera 	/* Check if PHC support is missing at the configuration level */
32953f88d7d1SDivya Koppera 	if (!shared->ptp_clock)
32963f88d7d1SDivya Koppera 		return 0;
32973f88d7d1SDivya Koppera 
3298ece19502SDivya Koppera 	phydev_dbg(phydev, "successfully registered ptp clock\n");
3299ece19502SDivya Koppera 
3300ece19502SDivya Koppera 	shared->phydev = phydev;
3301ece19502SDivya Koppera 
3302ece19502SDivya Koppera 	/* The EP.4 is shared between all the PHYs in the package and also it
3303ece19502SDivya Koppera 	 * can be accessed by any of the PHYs
3304ece19502SDivya Koppera 	 */
3305ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 4, LTC_HARD_RESET, LTC_HARD_RESET_);
3306ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 4, PTP_OPERATING_MODE,
3307ece19502SDivya Koppera 			      PTP_OPERATING_MODE_STANDALONE_);
3308ece19502SDivya Koppera 
3309ece19502SDivya Koppera 	return 0;
3310ece19502SDivya Koppera }
3311ece19502SDivya Koppera 
lan8814_setup_led(struct phy_device * phydev,int val)3312a516b7f7SDivya Koppera static void lan8814_setup_led(struct phy_device *phydev, int val)
3313a516b7f7SDivya Koppera {
3314a516b7f7SDivya Koppera 	int temp;
3315a516b7f7SDivya Koppera 
3316a516b7f7SDivya Koppera 	temp = lanphy_read_page_reg(phydev, 5, LAN8814_LED_CTRL_1);
3317a516b7f7SDivya Koppera 
3318a516b7f7SDivya Koppera 	if (val)
3319a516b7f7SDivya Koppera 		temp |= LAN8814_LED_CTRL_1_KSZ9031_LED_MODE_;
3320a516b7f7SDivya Koppera 	else
3321a516b7f7SDivya Koppera 		temp &= ~LAN8814_LED_CTRL_1_KSZ9031_LED_MODE_;
3322a516b7f7SDivya Koppera 
3323a516b7f7SDivya Koppera 	lanphy_write_page_reg(phydev, 5, LAN8814_LED_CTRL_1, temp);
3324a516b7f7SDivya Koppera }
3325a516b7f7SDivya Koppera 
lan8814_config_init(struct phy_device * phydev)3326ece19502SDivya Koppera static int lan8814_config_init(struct phy_device *phydev)
3327ece19502SDivya Koppera {
3328a516b7f7SDivya Koppera 	struct kszphy_priv *lan8814 = phydev->priv;
3329ece19502SDivya Koppera 	int val;
3330ece19502SDivya Koppera 
3331ece19502SDivya Koppera 	/* Reset the PHY */
3332ece19502SDivya Koppera 	val = lanphy_read_page_reg(phydev, 4, LAN8814_QSGMII_SOFT_RESET);
3333ece19502SDivya Koppera 	val |= LAN8814_QSGMII_SOFT_RESET_BIT;
3334ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 4, LAN8814_QSGMII_SOFT_RESET, val);
3335ece19502SDivya Koppera 
3336ece19502SDivya Koppera 	/* Disable ANEG with QSGMII PCS Host side */
3337ece19502SDivya Koppera 	val = lanphy_read_page_reg(phydev, 5, LAN8814_QSGMII_PCS1G_ANEG_CONFIG);
3338ece19502SDivya Koppera 	val &= ~LAN8814_QSGMII_PCS1G_ANEG_CONFIG_ANEG_ENA;
3339ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 5, LAN8814_QSGMII_PCS1G_ANEG_CONFIG, val);
3340ece19502SDivya Koppera 
3341ece19502SDivya Koppera 	/* MDI-X setting for swap A,B transmit */
3342ece19502SDivya Koppera 	val = lanphy_read_page_reg(phydev, 2, LAN8814_ALIGN_SWAP);
3343ece19502SDivya Koppera 	val &= ~LAN8814_ALIGN_TX_A_B_SWAP_MASK;
3344ece19502SDivya Koppera 	val |= LAN8814_ALIGN_TX_A_B_SWAP;
3345ece19502SDivya Koppera 	lanphy_write_page_reg(phydev, 2, LAN8814_ALIGN_SWAP, val);
3346ece19502SDivya Koppera 
3347a516b7f7SDivya Koppera 	if (lan8814->led_mode >= 0)
3348a516b7f7SDivya Koppera 		lan8814_setup_led(phydev, lan8814->led_mode);
3349a516b7f7SDivya Koppera 
3350ece19502SDivya Koppera 	return 0;
3351ece19502SDivya Koppera }
3352ece19502SDivya Koppera 
33534a4ce822SHoratiu Vultur /* It is expected that there will not be any 'lan8814_take_coma_mode'
33544a4ce822SHoratiu Vultur  * function called in suspend. Because the GPIO line can be shared, so if one of
33554a4ce822SHoratiu Vultur  * the phys goes back in coma mode, then all the other PHYs will go, which is
33564a4ce822SHoratiu Vultur  * wrong.
33574a4ce822SHoratiu Vultur  */
lan8814_release_coma_mode(struct phy_device * phydev)3358738871b0SMichael Walle static int lan8814_release_coma_mode(struct phy_device *phydev)
3359738871b0SMichael Walle {
3360738871b0SMichael Walle 	struct gpio_desc *gpiod;
3361738871b0SMichael Walle 
3362738871b0SMichael Walle 	gpiod = devm_gpiod_get_optional(&phydev->mdio.dev, "coma-mode",
33634a4ce822SHoratiu Vultur 					GPIOD_OUT_HIGH_OPEN_DRAIN |
33644a4ce822SHoratiu Vultur 					GPIOD_FLAGS_BIT_NONEXCLUSIVE);
3365738871b0SMichael Walle 	if (IS_ERR(gpiod))
3366738871b0SMichael Walle 		return PTR_ERR(gpiod);
3367738871b0SMichael Walle 
3368738871b0SMichael Walle 	gpiod_set_consumer_name(gpiod, "LAN8814 coma mode");
3369738871b0SMichael Walle 	gpiod_set_value_cansleep(gpiod, 0);
3370738871b0SMichael Walle 
3371738871b0SMichael Walle 	return 0;
3372738871b0SMichael Walle }
3373738871b0SMichael Walle 
lan8814_probe(struct phy_device * phydev)3374ece19502SDivya Koppera static int lan8814_probe(struct phy_device *phydev)
3375ece19502SDivya Koppera {
3376a516b7f7SDivya Koppera 	const struct kszphy_type *type = phydev->drv->driver_data;
3377ece19502SDivya Koppera 	struct kszphy_priv *priv;
3378ece19502SDivya Koppera 	u16 addr;
3379ece19502SDivya Koppera 	int err;
3380ece19502SDivya Koppera 
3381ece19502SDivya Koppera 	priv = devm_kzalloc(&phydev->mdio.dev, sizeof(*priv), GFP_KERNEL);
3382ece19502SDivya Koppera 	if (!priv)
3383ece19502SDivya Koppera 		return -ENOMEM;
3384ece19502SDivya Koppera 
3385ece19502SDivya Koppera 	phydev->priv = priv;
3386ece19502SDivya Koppera 
3387a516b7f7SDivya Koppera 	priv->type = type;
3388a516b7f7SDivya Koppera 
3389a516b7f7SDivya Koppera 	kszphy_parse_led_mode(phydev);
3390a516b7f7SDivya Koppera 
3391ece19502SDivya Koppera 	/* Strap-in value for PHY address, below register read gives starting
3392ece19502SDivya Koppera 	 * phy address value
3393ece19502SDivya Koppera 	 */
3394ece19502SDivya Koppera 	addr = lanphy_read_page_reg(phydev, 4, 0) & 0x1F;
3395ece19502SDivya Koppera 	devm_phy_package_join(&phydev->mdio.dev, phydev,
3396ece19502SDivya Koppera 			      addr, sizeof(struct lan8814_shared_priv));
3397ece19502SDivya Koppera 
3398ece19502SDivya Koppera 	if (phy_package_init_once(phydev)) {
3399738871b0SMichael Walle 		err = lan8814_release_coma_mode(phydev);
3400738871b0SMichael Walle 		if (err)
3401738871b0SMichael Walle 			return err;
3402738871b0SMichael Walle 
3403ece19502SDivya Koppera 		err = lan8814_ptp_probe_once(phydev);
3404ece19502SDivya Koppera 		if (err)
3405ece19502SDivya Koppera 			return err;
3406ece19502SDivya Koppera 	}
3407ece19502SDivya Koppera 
3408ece19502SDivya Koppera 	lan8814_ptp_init(phydev);
3409ece19502SDivya Koppera 
3410ece19502SDivya Koppera 	return 0;
3411ece19502SDivya Koppera }
3412ece19502SDivya Koppera 
3413a8f1a19dSHoratiu Vultur #define LAN8841_MMD_TIMER_REG			0
3414a8f1a19dSHoratiu Vultur #define LAN8841_MMD0_REGISTER_17		17
3415a8f1a19dSHoratiu Vultur #define LAN8841_MMD0_REGISTER_17_DROP_OPT(x)	((x) & 0x3)
3416a8f1a19dSHoratiu Vultur #define LAN8841_MMD0_REGISTER_17_XMIT_TOG_TX_DIS	BIT(3)
3417a8f1a19dSHoratiu Vultur #define LAN8841_OPERATION_MODE_STRAP_OVERRIDE_LOW_REG	2
3418a8f1a19dSHoratiu Vultur #define LAN8841_OPERATION_MODE_STRAP_OVERRIDE_LOW_REG_MAGJACK	BIT(14)
3419a8f1a19dSHoratiu Vultur #define LAN8841_MMD_ANALOG_REG			28
3420a8f1a19dSHoratiu Vultur #define LAN8841_ANALOG_CONTROL_1		1
3421a8f1a19dSHoratiu Vultur #define LAN8841_ANALOG_CONTROL_1_PLL_TRIM(x)	(((x) & 0x3) << 5)
3422a8f1a19dSHoratiu Vultur #define LAN8841_ANALOG_CONTROL_10		13
3423a8f1a19dSHoratiu Vultur #define LAN8841_ANALOG_CONTROL_10_PLL_DIV(x)	((x) & 0x3)
3424a8f1a19dSHoratiu Vultur #define LAN8841_ANALOG_CONTROL_11		14
3425a8f1a19dSHoratiu Vultur #define LAN8841_ANALOG_CONTROL_11_LDO_REF(x)	(((x) & 0x7) << 12)
3426a8f1a19dSHoratiu Vultur #define LAN8841_TX_LOW_I_CH_C_D_POWER_MANAGMENT	69
3427a8f1a19dSHoratiu Vultur #define LAN8841_TX_LOW_I_CH_C_D_POWER_MANAGMENT_VAL 0xbffc
3428a8f1a19dSHoratiu Vultur #define LAN8841_BTRX_POWER_DOWN			70
3429a8f1a19dSHoratiu Vultur #define LAN8841_BTRX_POWER_DOWN_QBIAS_CH_A	BIT(0)
3430a8f1a19dSHoratiu Vultur #define LAN8841_BTRX_POWER_DOWN_BTRX_CH_A	BIT(1)
3431a8f1a19dSHoratiu Vultur #define LAN8841_BTRX_POWER_DOWN_QBIAS_CH_B	BIT(2)
3432a8f1a19dSHoratiu Vultur #define LAN8841_BTRX_POWER_DOWN_BTRX_CH_B	BIT(3)
3433a8f1a19dSHoratiu Vultur #define LAN8841_BTRX_POWER_DOWN_BTRX_CH_C	BIT(5)
3434a8f1a19dSHoratiu Vultur #define LAN8841_BTRX_POWER_DOWN_BTRX_CH_D	BIT(7)
3435a8f1a19dSHoratiu Vultur #define LAN8841_ADC_CHANNEL_MASK		198
3436cafc3662SHoratiu Vultur #define LAN8841_PTP_RX_PARSE_L2_ADDR_EN		370
3437cafc3662SHoratiu Vultur #define LAN8841_PTP_RX_PARSE_IP_ADDR_EN		371
34381cb0cd1eSHoratiu Vultur #define LAN8841_PTP_RX_VERSION			374
3439cafc3662SHoratiu Vultur #define LAN8841_PTP_TX_PARSE_L2_ADDR_EN		434
3440cafc3662SHoratiu Vultur #define LAN8841_PTP_TX_PARSE_IP_ADDR_EN		435
34411cb0cd1eSHoratiu Vultur #define LAN8841_PTP_TX_VERSION			438
3442cafc3662SHoratiu Vultur #define LAN8841_PTP_CMD_CTL			256
3443cafc3662SHoratiu Vultur #define LAN8841_PTP_CMD_CTL_PTP_ENABLE		BIT(2)
3444cafc3662SHoratiu Vultur #define LAN8841_PTP_CMD_CTL_PTP_DISABLE		BIT(1)
3445cafc3662SHoratiu Vultur #define LAN8841_PTP_CMD_CTL_PTP_RESET		BIT(0)
3446cafc3662SHoratiu Vultur #define LAN8841_PTP_RX_PARSE_CONFIG		368
3447cafc3662SHoratiu Vultur #define LAN8841_PTP_TX_PARSE_CONFIG		432
3448cc755495SHoratiu Vultur #define LAN8841_PTP_RX_MODE			381
3449cc755495SHoratiu Vultur #define LAN8841_PTP_INSERT_TS_EN		BIT(0)
3450cc755495SHoratiu Vultur #define LAN8841_PTP_INSERT_TS_32BIT		BIT(1)
3451a8f1a19dSHoratiu Vultur 
lan8841_config_init(struct phy_device * phydev)3452a8f1a19dSHoratiu Vultur static int lan8841_config_init(struct phy_device *phydev)
3453a8f1a19dSHoratiu Vultur {
3454a8f1a19dSHoratiu Vultur 	int ret;
3455a8f1a19dSHoratiu Vultur 
3456a8f1a19dSHoratiu Vultur 	ret = ksz9131_config_init(phydev);
3457a8f1a19dSHoratiu Vultur 	if (ret)
3458a8f1a19dSHoratiu Vultur 		return ret;
3459a8f1a19dSHoratiu Vultur 
3460cafc3662SHoratiu Vultur 	/* Initialize the HW by resetting everything */
3461cafc3662SHoratiu Vultur 	phy_modify_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
3462cafc3662SHoratiu Vultur 		       LAN8841_PTP_CMD_CTL,
3463cafc3662SHoratiu Vultur 		       LAN8841_PTP_CMD_CTL_PTP_RESET,
3464cafc3662SHoratiu Vultur 		       LAN8841_PTP_CMD_CTL_PTP_RESET);
3465cafc3662SHoratiu Vultur 
3466cafc3662SHoratiu Vultur 	phy_modify_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
3467cafc3662SHoratiu Vultur 		       LAN8841_PTP_CMD_CTL,
3468cafc3662SHoratiu Vultur 		       LAN8841_PTP_CMD_CTL_PTP_ENABLE,
3469cafc3662SHoratiu Vultur 		       LAN8841_PTP_CMD_CTL_PTP_ENABLE);
3470cafc3662SHoratiu Vultur 
3471cafc3662SHoratiu Vultur 	/* Don't process any frames */
3472cafc3662SHoratiu Vultur 	phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
3473cafc3662SHoratiu Vultur 		      LAN8841_PTP_RX_PARSE_CONFIG, 0);
3474cafc3662SHoratiu Vultur 	phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
3475cafc3662SHoratiu Vultur 		      LAN8841_PTP_TX_PARSE_CONFIG, 0);
3476cafc3662SHoratiu Vultur 	phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
3477cafc3662SHoratiu Vultur 		      LAN8841_PTP_TX_PARSE_L2_ADDR_EN, 0);
3478cafc3662SHoratiu Vultur 	phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
3479cafc3662SHoratiu Vultur 		      LAN8841_PTP_RX_PARSE_L2_ADDR_EN, 0);
3480cafc3662SHoratiu Vultur 	phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
3481cafc3662SHoratiu Vultur 		      LAN8841_PTP_TX_PARSE_IP_ADDR_EN, 0);
3482cafc3662SHoratiu Vultur 	phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
3483cafc3662SHoratiu Vultur 		      LAN8841_PTP_RX_PARSE_IP_ADDR_EN, 0);
3484cafc3662SHoratiu Vultur 
34851cb0cd1eSHoratiu Vultur 	/* Disable checking for minorVersionPTP field */
34861cb0cd1eSHoratiu Vultur 	phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
34871cb0cd1eSHoratiu Vultur 		      LAN8841_PTP_RX_VERSION, 0xff00);
34881cb0cd1eSHoratiu Vultur 	phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
34891cb0cd1eSHoratiu Vultur 		      LAN8841_PTP_TX_VERSION, 0xff00);
34901cb0cd1eSHoratiu Vultur 
3491a8f1a19dSHoratiu Vultur 	/* 100BT Clause 40 improvenent errata */
3492a8f1a19dSHoratiu Vultur 	phy_write_mmd(phydev, LAN8841_MMD_ANALOG_REG,
3493a8f1a19dSHoratiu Vultur 		      LAN8841_ANALOG_CONTROL_1,
3494a8f1a19dSHoratiu Vultur 		      LAN8841_ANALOG_CONTROL_1_PLL_TRIM(0x2));
3495a8f1a19dSHoratiu Vultur 	phy_write_mmd(phydev, LAN8841_MMD_ANALOG_REG,
3496a8f1a19dSHoratiu Vultur 		      LAN8841_ANALOG_CONTROL_10,
3497a8f1a19dSHoratiu Vultur 		      LAN8841_ANALOG_CONTROL_10_PLL_DIV(0x1));
3498a8f1a19dSHoratiu Vultur 
3499a8f1a19dSHoratiu Vultur 	/* 10M/100M Ethernet Signal Tuning Errata for Shorted-Center Tap
3500a8f1a19dSHoratiu Vultur 	 * Magnetics
3501a8f1a19dSHoratiu Vultur 	 */
3502a8f1a19dSHoratiu Vultur 	ret = phy_read_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
3503a8f1a19dSHoratiu Vultur 			   LAN8841_OPERATION_MODE_STRAP_OVERRIDE_LOW_REG);
3504a8f1a19dSHoratiu Vultur 	if (ret & LAN8841_OPERATION_MODE_STRAP_OVERRIDE_LOW_REG_MAGJACK) {
3505a8f1a19dSHoratiu Vultur 		phy_write_mmd(phydev, LAN8841_MMD_ANALOG_REG,
3506a8f1a19dSHoratiu Vultur 			      LAN8841_TX_LOW_I_CH_C_D_POWER_MANAGMENT,
3507a8f1a19dSHoratiu Vultur 			      LAN8841_TX_LOW_I_CH_C_D_POWER_MANAGMENT_VAL);
3508a8f1a19dSHoratiu Vultur 		phy_write_mmd(phydev, LAN8841_MMD_ANALOG_REG,
3509a8f1a19dSHoratiu Vultur 			      LAN8841_BTRX_POWER_DOWN,
3510a8f1a19dSHoratiu Vultur 			      LAN8841_BTRX_POWER_DOWN_QBIAS_CH_A |
3511a8f1a19dSHoratiu Vultur 			      LAN8841_BTRX_POWER_DOWN_BTRX_CH_A |
3512a8f1a19dSHoratiu Vultur 			      LAN8841_BTRX_POWER_DOWN_QBIAS_CH_B |
3513a8f1a19dSHoratiu Vultur 			      LAN8841_BTRX_POWER_DOWN_BTRX_CH_B |
3514a8f1a19dSHoratiu Vultur 			      LAN8841_BTRX_POWER_DOWN_BTRX_CH_C |
3515a8f1a19dSHoratiu Vultur 			      LAN8841_BTRX_POWER_DOWN_BTRX_CH_D);
3516a8f1a19dSHoratiu Vultur 	}
3517a8f1a19dSHoratiu Vultur 
3518a8f1a19dSHoratiu Vultur 	/* LDO Adjustment errata */
3519a8f1a19dSHoratiu Vultur 	phy_write_mmd(phydev, LAN8841_MMD_ANALOG_REG,
3520a8f1a19dSHoratiu Vultur 		      LAN8841_ANALOG_CONTROL_11,
3521a8f1a19dSHoratiu Vultur 		      LAN8841_ANALOG_CONTROL_11_LDO_REF(1));
3522a8f1a19dSHoratiu Vultur 
3523a8f1a19dSHoratiu Vultur 	/* 100BT RGMII latency tuning errata */
3524a8f1a19dSHoratiu Vultur 	phy_write_mmd(phydev, MDIO_MMD_PMAPMD,
3525a8f1a19dSHoratiu Vultur 		      LAN8841_ADC_CHANNEL_MASK, 0x0);
3526a8f1a19dSHoratiu Vultur 	phy_write_mmd(phydev, LAN8841_MMD_TIMER_REG,
3527a8f1a19dSHoratiu Vultur 		      LAN8841_MMD0_REGISTER_17,
3528a8f1a19dSHoratiu Vultur 		      LAN8841_MMD0_REGISTER_17_DROP_OPT(2) |
3529a8f1a19dSHoratiu Vultur 		      LAN8841_MMD0_REGISTER_17_XMIT_TOG_TX_DIS);
3530a8f1a19dSHoratiu Vultur 
3531a8f1a19dSHoratiu Vultur 	return 0;
3532a8f1a19dSHoratiu Vultur }
3533a8f1a19dSHoratiu Vultur 
3534a8f1a19dSHoratiu Vultur #define LAN8841_OUTPUT_CTRL			25
3535a8f1a19dSHoratiu Vultur #define LAN8841_OUTPUT_CTRL_INT_BUFFER		BIT(14)
3536cafc3662SHoratiu Vultur #define LAN8841_INT_PTP				BIT(9)
3537a8f1a19dSHoratiu Vultur 
lan8841_config_intr(struct phy_device * phydev)3538a8f1a19dSHoratiu Vultur static int lan8841_config_intr(struct phy_device *phydev)
3539a8f1a19dSHoratiu Vultur {
3540a8f1a19dSHoratiu Vultur 	int err;
3541a8f1a19dSHoratiu Vultur 
3542a8f1a19dSHoratiu Vultur 	phy_modify(phydev, LAN8841_OUTPUT_CTRL,
3543a8f1a19dSHoratiu Vultur 		   LAN8841_OUTPUT_CTRL_INT_BUFFER, 0);
3544a8f1a19dSHoratiu Vultur 
3545a8f1a19dSHoratiu Vultur 	if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
3546a8f1a19dSHoratiu Vultur 		err = phy_read(phydev, LAN8814_INTS);
354798101ca2SHoratiu Vultur 		if (err < 0)
3548a8f1a19dSHoratiu Vultur 			return err;
3549a8f1a19dSHoratiu Vultur 
3550cafc3662SHoratiu Vultur 		/* Enable / disable interrupts. It is OK to enable PTP interrupt
3551cafc3662SHoratiu Vultur 		 * even if it PTP is not enabled. Because the underneath blocks
3552cafc3662SHoratiu Vultur 		 * will not enable the PTP so we will never get the PTP
3553cafc3662SHoratiu Vultur 		 * interrupt.
3554cafc3662SHoratiu Vultur 		 */
3555a8f1a19dSHoratiu Vultur 		err = phy_write(phydev, LAN8814_INTC,
3556cafc3662SHoratiu Vultur 				LAN8814_INT_LINK | LAN8841_INT_PTP);
3557a8f1a19dSHoratiu Vultur 	} else {
3558a8f1a19dSHoratiu Vultur 		err = phy_write(phydev, LAN8814_INTC, 0);
3559a8f1a19dSHoratiu Vultur 		if (err)
3560a8f1a19dSHoratiu Vultur 			return err;
3561a8f1a19dSHoratiu Vultur 
3562a8f1a19dSHoratiu Vultur 		err = phy_read(phydev, LAN8814_INTS);
356398101ca2SHoratiu Vultur 		if (err < 0)
356498101ca2SHoratiu Vultur 			return err;
356598101ca2SHoratiu Vultur 
356698101ca2SHoratiu Vultur 		/* Getting a positive value doesn't mean that is an error, it
356798101ca2SHoratiu Vultur 		 * just indicates what was the status. Therefore make sure to
356898101ca2SHoratiu Vultur 		 * clear the value and say that there is no error.
356998101ca2SHoratiu Vultur 		 */
357098101ca2SHoratiu Vultur 		err = 0;
3571a8f1a19dSHoratiu Vultur 	}
3572a8f1a19dSHoratiu Vultur 
3573a8f1a19dSHoratiu Vultur 	return err;
3574a8f1a19dSHoratiu Vultur }
3575a8f1a19dSHoratiu Vultur 
3576cafc3662SHoratiu Vultur #define LAN8841_PTP_TX_EGRESS_SEC_LO			453
3577cafc3662SHoratiu Vultur #define LAN8841_PTP_TX_EGRESS_SEC_HI			452
3578cafc3662SHoratiu Vultur #define LAN8841_PTP_TX_EGRESS_NS_LO			451
3579cafc3662SHoratiu Vultur #define LAN8841_PTP_TX_EGRESS_NS_HI			450
3580cafc3662SHoratiu Vultur #define LAN8841_PTP_TX_EGRESS_NSEC_HI_VALID		BIT(15)
3581cafc3662SHoratiu Vultur #define LAN8841_PTP_TX_MSG_HEADER2			455
3582cafc3662SHoratiu Vultur 
lan8841_ptp_get_tx_ts(struct kszphy_ptp_priv * ptp_priv,u32 * sec,u32 * nsec,u16 * seq)3583cafc3662SHoratiu Vultur static bool lan8841_ptp_get_tx_ts(struct kszphy_ptp_priv *ptp_priv,
3584cafc3662SHoratiu Vultur 				  u32 *sec, u32 *nsec, u16 *seq)
3585cafc3662SHoratiu Vultur {
3586cafc3662SHoratiu Vultur 	struct phy_device *phydev = ptp_priv->phydev;
3587cafc3662SHoratiu Vultur 
3588cafc3662SHoratiu Vultur 	*nsec = phy_read_mmd(phydev, 2, LAN8841_PTP_TX_EGRESS_NS_HI);
3589cafc3662SHoratiu Vultur 	if (!(*nsec & LAN8841_PTP_TX_EGRESS_NSEC_HI_VALID))
3590cafc3662SHoratiu Vultur 		return false;
3591cafc3662SHoratiu Vultur 
3592cafc3662SHoratiu Vultur 	*nsec = ((*nsec & 0x3fff) << 16);
3593cafc3662SHoratiu Vultur 	*nsec = *nsec | phy_read_mmd(phydev, 2, LAN8841_PTP_TX_EGRESS_NS_LO);
3594cafc3662SHoratiu Vultur 
3595cafc3662SHoratiu Vultur 	*sec = phy_read_mmd(phydev, 2, LAN8841_PTP_TX_EGRESS_SEC_HI);
3596cafc3662SHoratiu Vultur 	*sec = *sec << 16;
3597cafc3662SHoratiu Vultur 	*sec = *sec | phy_read_mmd(phydev, 2, LAN8841_PTP_TX_EGRESS_SEC_LO);
3598cafc3662SHoratiu Vultur 
3599cafc3662SHoratiu Vultur 	*seq = phy_read_mmd(phydev, 2, LAN8841_PTP_TX_MSG_HEADER2);
3600cafc3662SHoratiu Vultur 
3601cafc3662SHoratiu Vultur 	return true;
3602cafc3662SHoratiu Vultur }
3603cafc3662SHoratiu Vultur 
lan8841_ptp_process_tx_ts(struct kszphy_ptp_priv * ptp_priv)3604cafc3662SHoratiu Vultur static void lan8841_ptp_process_tx_ts(struct kszphy_ptp_priv *ptp_priv)
3605cafc3662SHoratiu Vultur {
3606cafc3662SHoratiu Vultur 	u32 sec, nsec;
3607cafc3662SHoratiu Vultur 	u16 seq;
3608cafc3662SHoratiu Vultur 
3609cafc3662SHoratiu Vultur 	while (lan8841_ptp_get_tx_ts(ptp_priv, &sec, &nsec, &seq))
3610cafc3662SHoratiu Vultur 		lan8814_match_tx_skb(ptp_priv, sec, nsec, seq);
3611cafc3662SHoratiu Vultur }
3612cafc3662SHoratiu Vultur 
3613cafc3662SHoratiu Vultur #define LAN8841_PTP_INT_STS			259
3614cafc3662SHoratiu Vultur #define LAN8841_PTP_INT_STS_PTP_TX_TS_OVRFL_INT	BIT(13)
3615cafc3662SHoratiu Vultur #define LAN8841_PTP_INT_STS_PTP_TX_TS_INT	BIT(12)
3616fac63186SHoratiu Vultur #define LAN8841_PTP_INT_STS_PTP_GPIO_CAP_INT	BIT(2)
3617cafc3662SHoratiu Vultur 
lan8841_ptp_flush_fifo(struct kszphy_ptp_priv * ptp_priv)3618cc755495SHoratiu Vultur static void lan8841_ptp_flush_fifo(struct kszphy_ptp_priv *ptp_priv)
3619cafc3662SHoratiu Vultur {
3620cafc3662SHoratiu Vultur 	struct phy_device *phydev = ptp_priv->phydev;
3621cafc3662SHoratiu Vultur 	int i;
3622cafc3662SHoratiu Vultur 
3623cafc3662SHoratiu Vultur 	for (i = 0; i < FIFO_SIZE; ++i)
3624cc755495SHoratiu Vultur 		phy_read_mmd(phydev, 2, LAN8841_PTP_TX_MSG_HEADER2);
3625cafc3662SHoratiu Vultur 
3626cafc3662SHoratiu Vultur 	phy_read_mmd(phydev, 2, LAN8841_PTP_INT_STS);
3627cafc3662SHoratiu Vultur }
3628cafc3662SHoratiu Vultur 
3629fac63186SHoratiu Vultur #define LAN8841_PTP_GPIO_CAP_STS			506
3630fac63186SHoratiu Vultur #define LAN8841_PTP_GPIO_SEL				327
3631fac63186SHoratiu Vultur #define LAN8841_PTP_GPIO_SEL_GPIO_SEL(gpio)		((gpio) << 8)
3632fac63186SHoratiu Vultur #define LAN8841_PTP_GPIO_RE_LTC_SEC_HI_CAP		498
3633fac63186SHoratiu Vultur #define LAN8841_PTP_GPIO_RE_LTC_SEC_LO_CAP		499
3634fac63186SHoratiu Vultur #define LAN8841_PTP_GPIO_RE_LTC_NS_HI_CAP		500
3635fac63186SHoratiu Vultur #define LAN8841_PTP_GPIO_RE_LTC_NS_LO_CAP		501
3636fac63186SHoratiu Vultur #define LAN8841_PTP_GPIO_FE_LTC_SEC_HI_CAP		502
3637fac63186SHoratiu Vultur #define LAN8841_PTP_GPIO_FE_LTC_SEC_LO_CAP		503
3638fac63186SHoratiu Vultur #define LAN8841_PTP_GPIO_FE_LTC_NS_HI_CAP		504
3639fac63186SHoratiu Vultur #define LAN8841_PTP_GPIO_FE_LTC_NS_LO_CAP		505
3640fac63186SHoratiu Vultur 
lan8841_gpio_process_cap(struct kszphy_ptp_priv * ptp_priv)3641fac63186SHoratiu Vultur static void lan8841_gpio_process_cap(struct kszphy_ptp_priv *ptp_priv)
3642fac63186SHoratiu Vultur {
3643fac63186SHoratiu Vultur 	struct phy_device *phydev = ptp_priv->phydev;
3644fac63186SHoratiu Vultur 	struct ptp_clock_event ptp_event = {0};
3645fac63186SHoratiu Vultur 	int pin, ret, tmp;
3646fac63186SHoratiu Vultur 	s32 sec, nsec;
3647fac63186SHoratiu Vultur 
3648fac63186SHoratiu Vultur 	pin = ptp_find_pin_unlocked(ptp_priv->ptp_clock, PTP_PF_EXTTS, 0);
3649fac63186SHoratiu Vultur 	if (pin == -1)
3650fac63186SHoratiu Vultur 		return;
3651fac63186SHoratiu Vultur 
3652fac63186SHoratiu Vultur 	tmp = phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_CAP_STS);
3653fac63186SHoratiu Vultur 	if (tmp < 0)
3654fac63186SHoratiu Vultur 		return;
3655fac63186SHoratiu Vultur 
3656fac63186SHoratiu Vultur 	ret = phy_write_mmd(phydev, 2, LAN8841_PTP_GPIO_SEL,
3657fac63186SHoratiu Vultur 			    LAN8841_PTP_GPIO_SEL_GPIO_SEL(pin));
3658fac63186SHoratiu Vultur 	if (ret)
3659fac63186SHoratiu Vultur 		return;
3660fac63186SHoratiu Vultur 
3661fac63186SHoratiu Vultur 	mutex_lock(&ptp_priv->ptp_lock);
3662fac63186SHoratiu Vultur 	if (tmp & BIT(pin)) {
3663fac63186SHoratiu Vultur 		sec = phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_RE_LTC_SEC_HI_CAP);
3664fac63186SHoratiu Vultur 		sec <<= 16;
3665fac63186SHoratiu Vultur 		sec |= phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_RE_LTC_SEC_LO_CAP);
3666fac63186SHoratiu Vultur 
3667fac63186SHoratiu Vultur 		nsec = phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_RE_LTC_NS_HI_CAP) & 0x3fff;
3668fac63186SHoratiu Vultur 		nsec <<= 16;
3669fac63186SHoratiu Vultur 		nsec |= phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_RE_LTC_NS_LO_CAP);
3670fac63186SHoratiu Vultur 	} else {
3671fac63186SHoratiu Vultur 		sec = phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_FE_LTC_SEC_HI_CAP);
3672fac63186SHoratiu Vultur 		sec <<= 16;
3673fac63186SHoratiu Vultur 		sec |= phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_FE_LTC_SEC_LO_CAP);
3674fac63186SHoratiu Vultur 
3675fac63186SHoratiu Vultur 		nsec = phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_FE_LTC_NS_HI_CAP) & 0x3fff;
3676fac63186SHoratiu Vultur 		nsec <<= 16;
3677fac63186SHoratiu Vultur 		nsec |= phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_FE_LTC_NS_LO_CAP);
3678fac63186SHoratiu Vultur 	}
3679fac63186SHoratiu Vultur 	mutex_unlock(&ptp_priv->ptp_lock);
3680fac63186SHoratiu Vultur 	ret = phy_write_mmd(phydev, 2, LAN8841_PTP_GPIO_SEL, 0);
3681fac63186SHoratiu Vultur 	if (ret)
3682fac63186SHoratiu Vultur 		return;
3683fac63186SHoratiu Vultur 
3684fac63186SHoratiu Vultur 	ptp_event.index = 0;
3685fac63186SHoratiu Vultur 	ptp_event.timestamp = ktime_set(sec, nsec);
3686fac63186SHoratiu Vultur 	ptp_event.type = PTP_CLOCK_EXTTS;
3687fac63186SHoratiu Vultur 	ptp_clock_event(ptp_priv->ptp_clock, &ptp_event);
3688fac63186SHoratiu Vultur }
3689fac63186SHoratiu Vultur 
lan8841_handle_ptp_interrupt(struct phy_device * phydev)3690cafc3662SHoratiu Vultur static void lan8841_handle_ptp_interrupt(struct phy_device *phydev)
3691cafc3662SHoratiu Vultur {
3692cafc3662SHoratiu Vultur 	struct kszphy_priv *priv = phydev->priv;
3693cafc3662SHoratiu Vultur 	struct kszphy_ptp_priv *ptp_priv = &priv->ptp_priv;
3694cafc3662SHoratiu Vultur 	u16 status;
3695cafc3662SHoratiu Vultur 
3696cafc3662SHoratiu Vultur 	do {
3697cafc3662SHoratiu Vultur 		status = phy_read_mmd(phydev, 2, LAN8841_PTP_INT_STS);
3698fac63186SHoratiu Vultur 
3699cafc3662SHoratiu Vultur 		if (status & LAN8841_PTP_INT_STS_PTP_TX_TS_INT)
3700cafc3662SHoratiu Vultur 			lan8841_ptp_process_tx_ts(ptp_priv);
3701cafc3662SHoratiu Vultur 
3702fac63186SHoratiu Vultur 		if (status & LAN8841_PTP_INT_STS_PTP_GPIO_CAP_INT)
3703fac63186SHoratiu Vultur 			lan8841_gpio_process_cap(ptp_priv);
3704fac63186SHoratiu Vultur 
3705cafc3662SHoratiu Vultur 		if (status & LAN8841_PTP_INT_STS_PTP_TX_TS_OVRFL_INT) {
3706cc755495SHoratiu Vultur 			lan8841_ptp_flush_fifo(ptp_priv);
3707cafc3662SHoratiu Vultur 			skb_queue_purge(&ptp_priv->tx_queue);
3708cafc3662SHoratiu Vultur 		}
3709cafc3662SHoratiu Vultur 
3710cc755495SHoratiu Vultur 	} while (status & (LAN8841_PTP_INT_STS_PTP_TX_TS_INT |
3711cc755495SHoratiu Vultur 			   LAN8841_PTP_INT_STS_PTP_GPIO_CAP_INT |
3712cc755495SHoratiu Vultur 			   LAN8841_PTP_INT_STS_PTP_TX_TS_OVRFL_INT));
3713cafc3662SHoratiu Vultur }
3714cafc3662SHoratiu Vultur 
3715cafc3662SHoratiu Vultur #define LAN8841_INTS_PTP		BIT(9)
3716cafc3662SHoratiu Vultur 
lan8841_handle_interrupt(struct phy_device * phydev)3717a8f1a19dSHoratiu Vultur static irqreturn_t lan8841_handle_interrupt(struct phy_device *phydev)
3718a8f1a19dSHoratiu Vultur {
3719cafc3662SHoratiu Vultur 	irqreturn_t ret = IRQ_NONE;
3720a8f1a19dSHoratiu Vultur 	int irq_status;
3721a8f1a19dSHoratiu Vultur 
3722a8f1a19dSHoratiu Vultur 	irq_status = phy_read(phydev, LAN8814_INTS);
3723a8f1a19dSHoratiu Vultur 	if (irq_status < 0) {
3724a8f1a19dSHoratiu Vultur 		phy_error(phydev);
3725a8f1a19dSHoratiu Vultur 		return IRQ_NONE;
3726a8f1a19dSHoratiu Vultur 	}
3727a8f1a19dSHoratiu Vultur 
3728a8f1a19dSHoratiu Vultur 	if (irq_status & LAN8814_INT_LINK) {
3729a8f1a19dSHoratiu Vultur 		phy_trigger_machine(phydev);
3730cafc3662SHoratiu Vultur 		ret = IRQ_HANDLED;
3731a8f1a19dSHoratiu Vultur 	}
3732a8f1a19dSHoratiu Vultur 
3733cafc3662SHoratiu Vultur 	if (irq_status & LAN8841_INTS_PTP) {
3734cafc3662SHoratiu Vultur 		lan8841_handle_ptp_interrupt(phydev);
3735cafc3662SHoratiu Vultur 		ret = IRQ_HANDLED;
3736a8f1a19dSHoratiu Vultur 	}
3737a8f1a19dSHoratiu Vultur 
3738cafc3662SHoratiu Vultur 	return ret;
3739cafc3662SHoratiu Vultur }
3740cafc3662SHoratiu Vultur 
lan8841_ts_info(struct mii_timestamper * mii_ts,struct ethtool_ts_info * info)3741cafc3662SHoratiu Vultur static int lan8841_ts_info(struct mii_timestamper *mii_ts,
3742cafc3662SHoratiu Vultur 			   struct ethtool_ts_info *info)
3743cafc3662SHoratiu Vultur {
3744cafc3662SHoratiu Vultur 	struct kszphy_ptp_priv *ptp_priv;
3745cafc3662SHoratiu Vultur 
3746cafc3662SHoratiu Vultur 	ptp_priv = container_of(mii_ts, struct kszphy_ptp_priv, mii_ts);
3747cafc3662SHoratiu Vultur 
3748cafc3662SHoratiu Vultur 	info->phc_index = ptp_priv->ptp_clock ?
3749cafc3662SHoratiu Vultur 				ptp_clock_index(ptp_priv->ptp_clock) : -1;
3750d06b88b0SKory Maincent 	if (info->phc_index == -1)
3751cafc3662SHoratiu Vultur 		return 0;
3752cafc3662SHoratiu Vultur 
3753cafc3662SHoratiu Vultur 	info->so_timestamping = SOF_TIMESTAMPING_TX_HARDWARE |
3754cafc3662SHoratiu Vultur 				SOF_TIMESTAMPING_RX_HARDWARE |
3755cafc3662SHoratiu Vultur 				SOF_TIMESTAMPING_RAW_HARDWARE;
3756cafc3662SHoratiu Vultur 
3757cafc3662SHoratiu Vultur 	info->tx_types = (1 << HWTSTAMP_TX_OFF) |
3758cafc3662SHoratiu Vultur 			 (1 << HWTSTAMP_TX_ON) |
3759cafc3662SHoratiu Vultur 			 (1 << HWTSTAMP_TX_ONESTEP_SYNC);
3760cafc3662SHoratiu Vultur 
3761cafc3662SHoratiu Vultur 	info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
3762cafc3662SHoratiu Vultur 			   (1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT) |
3763cafc3662SHoratiu Vultur 			   (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
3764cafc3662SHoratiu Vultur 			   (1 << HWTSTAMP_FILTER_PTP_V2_EVENT);
3765cafc3662SHoratiu Vultur 
3766cafc3662SHoratiu Vultur 	return 0;
3767cafc3662SHoratiu Vultur }
3768cafc3662SHoratiu Vultur 
3769cafc3662SHoratiu Vultur #define LAN8841_PTP_INT_EN			260
3770cafc3662SHoratiu Vultur #define LAN8841_PTP_INT_EN_PTP_TX_TS_OVRFL_EN	BIT(13)
3771cafc3662SHoratiu Vultur #define LAN8841_PTP_INT_EN_PTP_TX_TS_EN		BIT(12)
3772cafc3662SHoratiu Vultur 
lan8841_ptp_enable_processing(struct kszphy_ptp_priv * ptp_priv,bool enable)3773cc755495SHoratiu Vultur static void lan8841_ptp_enable_processing(struct kszphy_ptp_priv *ptp_priv,
3774cafc3662SHoratiu Vultur 					  bool enable)
3775cafc3662SHoratiu Vultur {
3776cafc3662SHoratiu Vultur 	struct phy_device *phydev = ptp_priv->phydev;
3777cafc3662SHoratiu Vultur 
3778cc755495SHoratiu Vultur 	if (enable) {
3779cc755495SHoratiu Vultur 		/* Enable interrupts on the TX side */
3780cafc3662SHoratiu Vultur 		phy_modify_mmd(phydev, 2, LAN8841_PTP_INT_EN,
3781cafc3662SHoratiu Vultur 			       LAN8841_PTP_INT_EN_PTP_TX_TS_OVRFL_EN |
3782cc755495SHoratiu Vultur 			       LAN8841_PTP_INT_EN_PTP_TX_TS_EN,
3783cafc3662SHoratiu Vultur 			       LAN8841_PTP_INT_EN_PTP_TX_TS_OVRFL_EN |
3784cc755495SHoratiu Vultur 			       LAN8841_PTP_INT_EN_PTP_TX_TS_EN);
3785cc755495SHoratiu Vultur 
3786cc755495SHoratiu Vultur 		/* Enable the modification of the frame on RX side,
3787cc755495SHoratiu Vultur 		 * this will add the ns and 2 bits of sec in the reserved field
3788cc755495SHoratiu Vultur 		 * of the PTP header
3789cc755495SHoratiu Vultur 		 */
3790cc755495SHoratiu Vultur 		phy_modify_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
3791cc755495SHoratiu Vultur 			       LAN8841_PTP_RX_MODE,
3792cc755495SHoratiu Vultur 			       LAN8841_PTP_INSERT_TS_EN |
3793cc755495SHoratiu Vultur 			       LAN8841_PTP_INSERT_TS_32BIT,
3794cc755495SHoratiu Vultur 			       LAN8841_PTP_INSERT_TS_EN |
3795cc755495SHoratiu Vultur 			       LAN8841_PTP_INSERT_TS_32BIT);
3796cc755495SHoratiu Vultur 
3797cc755495SHoratiu Vultur 		ptp_schedule_worker(ptp_priv->ptp_clock, 0);
3798cc755495SHoratiu Vultur 	} else {
3799cc755495SHoratiu Vultur 		/* Disable interrupts on the TX side */
3800cafc3662SHoratiu Vultur 		phy_modify_mmd(phydev, 2, LAN8841_PTP_INT_EN,
3801cafc3662SHoratiu Vultur 			       LAN8841_PTP_INT_EN_PTP_TX_TS_OVRFL_EN |
3802cc755495SHoratiu Vultur 			       LAN8841_PTP_INT_EN_PTP_TX_TS_EN, 0);
3803cc755495SHoratiu Vultur 
3804cc755495SHoratiu Vultur 		/* Disable modification of the RX frames */
3805cc755495SHoratiu Vultur 		phy_modify_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
3806cc755495SHoratiu Vultur 			       LAN8841_PTP_RX_MODE,
3807cc755495SHoratiu Vultur 			       LAN8841_PTP_INSERT_TS_EN |
3808cc755495SHoratiu Vultur 			       LAN8841_PTP_INSERT_TS_32BIT, 0);
3809cc755495SHoratiu Vultur 
3810cc755495SHoratiu Vultur 		ptp_cancel_worker_sync(ptp_priv->ptp_clock);
3811cc755495SHoratiu Vultur 	}
3812cafc3662SHoratiu Vultur }
3813cafc3662SHoratiu Vultur 
3814cafc3662SHoratiu Vultur #define LAN8841_PTP_RX_TIMESTAMP_EN		379
3815cafc3662SHoratiu Vultur #define LAN8841_PTP_TX_TIMESTAMP_EN		443
3816cafc3662SHoratiu Vultur #define LAN8841_PTP_TX_MOD			445
3817cafc3662SHoratiu Vultur 
lan8841_hwtstamp(struct mii_timestamper * mii_ts,struct ifreq * ifr)3818cafc3662SHoratiu Vultur static int lan8841_hwtstamp(struct mii_timestamper *mii_ts, struct ifreq *ifr)
3819cafc3662SHoratiu Vultur {
3820cafc3662SHoratiu Vultur 	struct kszphy_ptp_priv *ptp_priv = container_of(mii_ts, struct kszphy_ptp_priv, mii_ts);
3821cafc3662SHoratiu Vultur 	struct phy_device *phydev = ptp_priv->phydev;
3822cafc3662SHoratiu Vultur 	struct hwtstamp_config config;
3823cafc3662SHoratiu Vultur 	int txcfg = 0, rxcfg = 0;
3824cafc3662SHoratiu Vultur 	int pkt_ts_enable;
3825cafc3662SHoratiu Vultur 
3826cafc3662SHoratiu Vultur 	if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
3827cafc3662SHoratiu Vultur 		return -EFAULT;
3828cafc3662SHoratiu Vultur 
3829cafc3662SHoratiu Vultur 	ptp_priv->hwts_tx_type = config.tx_type;
3830cafc3662SHoratiu Vultur 	ptp_priv->rx_filter = config.rx_filter;
3831cafc3662SHoratiu Vultur 
3832cafc3662SHoratiu Vultur 	switch (config.rx_filter) {
3833cafc3662SHoratiu Vultur 	case HWTSTAMP_FILTER_NONE:
3834cafc3662SHoratiu Vultur 		ptp_priv->layer = 0;
3835cafc3662SHoratiu Vultur 		ptp_priv->version = 0;
3836cafc3662SHoratiu Vultur 		break;
3837cafc3662SHoratiu Vultur 	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
3838cafc3662SHoratiu Vultur 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
3839cafc3662SHoratiu Vultur 	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
3840cafc3662SHoratiu Vultur 		ptp_priv->layer = PTP_CLASS_L4;
3841cafc3662SHoratiu Vultur 		ptp_priv->version = PTP_CLASS_V2;
3842cafc3662SHoratiu Vultur 		break;
3843cafc3662SHoratiu Vultur 	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
3844cafc3662SHoratiu Vultur 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
3845cafc3662SHoratiu Vultur 	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
3846cafc3662SHoratiu Vultur 		ptp_priv->layer = PTP_CLASS_L2;
3847cafc3662SHoratiu Vultur 		ptp_priv->version = PTP_CLASS_V2;
3848cafc3662SHoratiu Vultur 		break;
3849cafc3662SHoratiu Vultur 	case HWTSTAMP_FILTER_PTP_V2_EVENT:
3850cafc3662SHoratiu Vultur 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
3851cafc3662SHoratiu Vultur 	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
3852cafc3662SHoratiu Vultur 		ptp_priv->layer = PTP_CLASS_L4 | PTP_CLASS_L2;
3853cafc3662SHoratiu Vultur 		ptp_priv->version = PTP_CLASS_V2;
3854cafc3662SHoratiu Vultur 		break;
3855cafc3662SHoratiu Vultur 	default:
3856cafc3662SHoratiu Vultur 		return -ERANGE;
3857cafc3662SHoratiu Vultur 	}
3858cafc3662SHoratiu Vultur 
3859cafc3662SHoratiu Vultur 	/* Setup parsing of the frames and enable the timestamping for ptp
3860cafc3662SHoratiu Vultur 	 * frames
3861cafc3662SHoratiu Vultur 	 */
3862cafc3662SHoratiu Vultur 	if (ptp_priv->layer & PTP_CLASS_L2) {
3863cafc3662SHoratiu Vultur 		rxcfg |= PTP_RX_PARSE_CONFIG_LAYER2_EN_;
3864cafc3662SHoratiu Vultur 		txcfg |= PTP_TX_PARSE_CONFIG_LAYER2_EN_;
3865cafc3662SHoratiu Vultur 	} else if (ptp_priv->layer & PTP_CLASS_L4) {
3866cafc3662SHoratiu Vultur 		rxcfg |= PTP_RX_PARSE_CONFIG_IPV4_EN_ | PTP_RX_PARSE_CONFIG_IPV6_EN_;
3867cafc3662SHoratiu Vultur 		txcfg |= PTP_TX_PARSE_CONFIG_IPV4_EN_ | PTP_TX_PARSE_CONFIG_IPV6_EN_;
3868cafc3662SHoratiu Vultur 	}
3869cafc3662SHoratiu Vultur 
3870cafc3662SHoratiu Vultur 	phy_write_mmd(phydev, 2, LAN8841_PTP_RX_PARSE_CONFIG, rxcfg);
3871cafc3662SHoratiu Vultur 	phy_write_mmd(phydev, 2, LAN8841_PTP_TX_PARSE_CONFIG, txcfg);
3872cafc3662SHoratiu Vultur 
3873cafc3662SHoratiu Vultur 	pkt_ts_enable = PTP_TIMESTAMP_EN_SYNC_ | PTP_TIMESTAMP_EN_DREQ_ |
3874cafc3662SHoratiu Vultur 			PTP_TIMESTAMP_EN_PDREQ_ | PTP_TIMESTAMP_EN_PDRES_;
3875cafc3662SHoratiu Vultur 	phy_write_mmd(phydev, 2, LAN8841_PTP_RX_TIMESTAMP_EN, pkt_ts_enable);
3876cafc3662SHoratiu Vultur 	phy_write_mmd(phydev, 2, LAN8841_PTP_TX_TIMESTAMP_EN, pkt_ts_enable);
3877cafc3662SHoratiu Vultur 
3878cafc3662SHoratiu Vultur 	/* Enable / disable of the TX timestamp in the SYNC frames */
3879cafc3662SHoratiu Vultur 	phy_modify_mmd(phydev, 2, LAN8841_PTP_TX_MOD,
3880cafc3662SHoratiu Vultur 		       PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_,
3881cafc3662SHoratiu Vultur 		       ptp_priv->hwts_tx_type == HWTSTAMP_TX_ONESTEP_SYNC ?
3882cafc3662SHoratiu Vultur 				PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_ : 0);
3883cafc3662SHoratiu Vultur 
3884cafc3662SHoratiu Vultur 	/* Now enable/disable the timestamping */
3885cc755495SHoratiu Vultur 	lan8841_ptp_enable_processing(ptp_priv,
3886cafc3662SHoratiu Vultur 				      config.rx_filter != HWTSTAMP_FILTER_NONE);
3887cafc3662SHoratiu Vultur 
3888cafc3662SHoratiu Vultur 	skb_queue_purge(&ptp_priv->tx_queue);
3889cafc3662SHoratiu Vultur 
3890cc755495SHoratiu Vultur 	lan8841_ptp_flush_fifo(ptp_priv);
3891cafc3662SHoratiu Vultur 
3892cafc3662SHoratiu Vultur 	return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ? -EFAULT : 0;
3893cafc3662SHoratiu Vultur }
3894cafc3662SHoratiu Vultur 
lan8841_rxtstamp(struct mii_timestamper * mii_ts,struct sk_buff * skb,int type)3895cc755495SHoratiu Vultur static bool lan8841_rxtstamp(struct mii_timestamper *mii_ts,
3896cc755495SHoratiu Vultur 			     struct sk_buff *skb, int type)
3897cc755495SHoratiu Vultur {
3898cc755495SHoratiu Vultur 	struct kszphy_ptp_priv *ptp_priv =
3899cc755495SHoratiu Vultur 			container_of(mii_ts, struct kszphy_ptp_priv, mii_ts);
3900cc755495SHoratiu Vultur 	struct ptp_header *header = ptp_parse_header(skb, type);
3901cc755495SHoratiu Vultur 	struct skb_shared_hwtstamps *shhwtstamps;
3902cc755495SHoratiu Vultur 	struct timespec64 ts;
3903cc755495SHoratiu Vultur 	unsigned long flags;
3904cc755495SHoratiu Vultur 	u32 ts_header;
3905cc755495SHoratiu Vultur 
3906cc755495SHoratiu Vultur 	if (!header)
3907cc755495SHoratiu Vultur 		return false;
3908cc755495SHoratiu Vultur 
3909cc755495SHoratiu Vultur 	if (ptp_priv->rx_filter == HWTSTAMP_FILTER_NONE ||
3910cc755495SHoratiu Vultur 	    type == PTP_CLASS_NONE)
3911cc755495SHoratiu Vultur 		return false;
3912cc755495SHoratiu Vultur 
3913cc755495SHoratiu Vultur 	if ((type & ptp_priv->version) == 0 || (type & ptp_priv->layer) == 0)
3914cc755495SHoratiu Vultur 		return false;
3915cc755495SHoratiu Vultur 
3916cc755495SHoratiu Vultur 	spin_lock_irqsave(&ptp_priv->seconds_lock, flags);
3917cc755495SHoratiu Vultur 	ts.tv_sec = ptp_priv->seconds;
3918cc755495SHoratiu Vultur 	spin_unlock_irqrestore(&ptp_priv->seconds_lock, flags);
3919cc755495SHoratiu Vultur 	ts_header = __be32_to_cpu(header->reserved2);
3920cc755495SHoratiu Vultur 
3921cc755495SHoratiu Vultur 	shhwtstamps = skb_hwtstamps(skb);
3922cc755495SHoratiu Vultur 	memset(shhwtstamps, 0, sizeof(*shhwtstamps));
3923cc755495SHoratiu Vultur 
3924cc755495SHoratiu Vultur 	/* Check for any wrap arounds for the second part */
3925cc755495SHoratiu Vultur 	if ((ts.tv_sec & GENMASK(1, 0)) == 0 && (ts_header >> 30) == 3)
3926cc755495SHoratiu Vultur 		ts.tv_sec -= GENMASK(1, 0) + 1;
3927cc755495SHoratiu Vultur 	else if ((ts.tv_sec & GENMASK(1, 0)) == 3 && (ts_header >> 30) == 0)
3928cc755495SHoratiu Vultur 		ts.tv_sec += 1;
3929cc755495SHoratiu Vultur 
3930cc755495SHoratiu Vultur 	shhwtstamps->hwtstamp =
3931cc755495SHoratiu Vultur 		ktime_set((ts.tv_sec & ~(GENMASK(1, 0))) | ts_header >> 30,
3932cc755495SHoratiu Vultur 			  ts_header & GENMASK(29, 0));
3933cc755495SHoratiu Vultur 	header->reserved2 = 0;
3934cc755495SHoratiu Vultur 
3935cc755495SHoratiu Vultur 	netif_rx(skb);
3936cc755495SHoratiu Vultur 
3937cc755495SHoratiu Vultur 	return true;
3938cc755495SHoratiu Vultur }
3939cc755495SHoratiu Vultur 
3940e4ed8ba0SHoratiu Vultur #define LAN8841_EVENT_A		0
3941e4ed8ba0SHoratiu Vultur #define LAN8841_EVENT_B		1
3942e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_LTC_TARGET_SEC_HI(event)	((event) == LAN8841_EVENT_A ? 278 : 288)
3943e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_LTC_TARGET_SEC_LO(event)	((event) == LAN8841_EVENT_A ? 279 : 289)
3944e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_LTC_TARGET_NS_HI(event)	((event) == LAN8841_EVENT_A ? 280 : 290)
3945e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_LTC_TARGET_NS_LO(event)	((event) == LAN8841_EVENT_A ? 281 : 291)
3946e4ed8ba0SHoratiu Vultur 
lan8841_ptp_set_target(struct kszphy_ptp_priv * ptp_priv,u8 event,s64 sec,u32 nsec)3947e4ed8ba0SHoratiu Vultur static int lan8841_ptp_set_target(struct kszphy_ptp_priv *ptp_priv, u8 event,
3948e4ed8ba0SHoratiu Vultur 				  s64 sec, u32 nsec)
3949e4ed8ba0SHoratiu Vultur {
3950e4ed8ba0SHoratiu Vultur 	struct phy_device *phydev = ptp_priv->phydev;
3951e4ed8ba0SHoratiu Vultur 	int ret;
3952e4ed8ba0SHoratiu Vultur 
3953e4ed8ba0SHoratiu Vultur 	ret = phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_TARGET_SEC_HI(event),
3954e4ed8ba0SHoratiu Vultur 			    upper_16_bits(sec));
3955e4ed8ba0SHoratiu Vultur 	if (ret)
3956e4ed8ba0SHoratiu Vultur 		return ret;
3957e4ed8ba0SHoratiu Vultur 
3958e4ed8ba0SHoratiu Vultur 	ret = phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_TARGET_SEC_LO(event),
3959e4ed8ba0SHoratiu Vultur 			    lower_16_bits(sec));
3960e4ed8ba0SHoratiu Vultur 	if (ret)
3961e4ed8ba0SHoratiu Vultur 		return ret;
3962e4ed8ba0SHoratiu Vultur 
3963e4ed8ba0SHoratiu Vultur 	ret = phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_TARGET_NS_HI(event) & 0x3fff,
3964e4ed8ba0SHoratiu Vultur 			    upper_16_bits(nsec));
3965e4ed8ba0SHoratiu Vultur 	if (ret)
3966e4ed8ba0SHoratiu Vultur 		return ret;
3967e4ed8ba0SHoratiu Vultur 
3968e4ed8ba0SHoratiu Vultur 	return phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_TARGET_NS_LO(event),
3969e4ed8ba0SHoratiu Vultur 			    lower_16_bits(nsec));
3970e4ed8ba0SHoratiu Vultur }
3971e4ed8ba0SHoratiu Vultur 
3972e4ed8ba0SHoratiu Vultur #define LAN8841_BUFFER_TIME	2
3973e4ed8ba0SHoratiu Vultur 
lan8841_ptp_update_target(struct kszphy_ptp_priv * ptp_priv,const struct timespec64 * ts)3974e4ed8ba0SHoratiu Vultur static int lan8841_ptp_update_target(struct kszphy_ptp_priv *ptp_priv,
3975e4ed8ba0SHoratiu Vultur 				     const struct timespec64 *ts)
3976e4ed8ba0SHoratiu Vultur {
3977e4ed8ba0SHoratiu Vultur 	return lan8841_ptp_set_target(ptp_priv, LAN8841_EVENT_A,
3978c6d6ef3eSHoratiu Vultur 				      ts->tv_sec + LAN8841_BUFFER_TIME, 0);
3979e4ed8ba0SHoratiu Vultur }
3980e4ed8ba0SHoratiu Vultur 
3981e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_LTC_TARGET_RELOAD_SEC_HI(event)	((event) == LAN8841_EVENT_A ? 282 : 292)
3982e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_LTC_TARGET_RELOAD_SEC_LO(event)	((event) == LAN8841_EVENT_A ? 283 : 293)
3983e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_LTC_TARGET_RELOAD_NS_HI(event)	((event) == LAN8841_EVENT_A ? 284 : 294)
3984e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_LTC_TARGET_RELOAD_NS_LO(event)	((event) == LAN8841_EVENT_A ? 285 : 295)
3985e4ed8ba0SHoratiu Vultur 
lan8841_ptp_set_reload(struct kszphy_ptp_priv * ptp_priv,u8 event,s64 sec,u32 nsec)3986e4ed8ba0SHoratiu Vultur static int lan8841_ptp_set_reload(struct kszphy_ptp_priv *ptp_priv, u8 event,
3987e4ed8ba0SHoratiu Vultur 				  s64 sec, u32 nsec)
3988e4ed8ba0SHoratiu Vultur {
3989e4ed8ba0SHoratiu Vultur 	struct phy_device *phydev = ptp_priv->phydev;
3990e4ed8ba0SHoratiu Vultur 	int ret;
3991e4ed8ba0SHoratiu Vultur 
3992e4ed8ba0SHoratiu Vultur 	ret = phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_TARGET_RELOAD_SEC_HI(event),
3993e4ed8ba0SHoratiu Vultur 			    upper_16_bits(sec));
3994e4ed8ba0SHoratiu Vultur 	if (ret)
3995e4ed8ba0SHoratiu Vultur 		return ret;
3996e4ed8ba0SHoratiu Vultur 
3997e4ed8ba0SHoratiu Vultur 	ret = phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_TARGET_RELOAD_SEC_LO(event),
3998e4ed8ba0SHoratiu Vultur 			    lower_16_bits(sec));
3999e4ed8ba0SHoratiu Vultur 	if (ret)
4000e4ed8ba0SHoratiu Vultur 		return ret;
4001e4ed8ba0SHoratiu Vultur 
4002e4ed8ba0SHoratiu Vultur 	ret = phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_TARGET_RELOAD_NS_HI(event) & 0x3fff,
4003e4ed8ba0SHoratiu Vultur 			    upper_16_bits(nsec));
4004e4ed8ba0SHoratiu Vultur 	if (ret)
4005e4ed8ba0SHoratiu Vultur 		return ret;
4006e4ed8ba0SHoratiu Vultur 
4007e4ed8ba0SHoratiu Vultur 	return phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_TARGET_RELOAD_NS_LO(event),
4008e4ed8ba0SHoratiu Vultur 			     lower_16_bits(nsec));
4009e4ed8ba0SHoratiu Vultur }
4010e4ed8ba0SHoratiu Vultur 
4011cafc3662SHoratiu Vultur #define LAN8841_PTP_LTC_SET_SEC_HI	262
4012cafc3662SHoratiu Vultur #define LAN8841_PTP_LTC_SET_SEC_MID	263
4013cafc3662SHoratiu Vultur #define LAN8841_PTP_LTC_SET_SEC_LO	264
4014cafc3662SHoratiu Vultur #define LAN8841_PTP_LTC_SET_NS_HI	265
4015cafc3662SHoratiu Vultur #define LAN8841_PTP_LTC_SET_NS_LO	266
4016cafc3662SHoratiu Vultur #define LAN8841_PTP_CMD_CTL_PTP_LTC_LOAD	BIT(4)
4017cafc3662SHoratiu Vultur 
lan8841_ptp_settime64(struct ptp_clock_info * ptp,const struct timespec64 * ts)4018cafc3662SHoratiu Vultur static int lan8841_ptp_settime64(struct ptp_clock_info *ptp,
4019cafc3662SHoratiu Vultur 				 const struct timespec64 *ts)
4020cafc3662SHoratiu Vultur {
4021cafc3662SHoratiu Vultur 	struct kszphy_ptp_priv *ptp_priv = container_of(ptp, struct kszphy_ptp_priv,
4022cafc3662SHoratiu Vultur 							ptp_clock_info);
4023cafc3662SHoratiu Vultur 	struct phy_device *phydev = ptp_priv->phydev;
4024cc755495SHoratiu Vultur 	unsigned long flags;
4025e4ed8ba0SHoratiu Vultur 	int ret;
4026cafc3662SHoratiu Vultur 
4027cafc3662SHoratiu Vultur 	/* Set the value to be stored */
4028cafc3662SHoratiu Vultur 	mutex_lock(&ptp_priv->ptp_lock);
4029cafc3662SHoratiu Vultur 	phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_SET_SEC_LO, lower_16_bits(ts->tv_sec));
4030cafc3662SHoratiu Vultur 	phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_SET_SEC_MID, upper_16_bits(ts->tv_sec));
4031cafc3662SHoratiu Vultur 	phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_SET_SEC_HI, upper_32_bits(ts->tv_sec) & 0xffff);
4032cafc3662SHoratiu Vultur 	phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_SET_NS_LO, lower_16_bits(ts->tv_nsec));
4033cafc3662SHoratiu Vultur 	phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_SET_NS_HI, upper_16_bits(ts->tv_nsec) & 0x3fff);
4034cafc3662SHoratiu Vultur 
4035cafc3662SHoratiu Vultur 	/* Set the command to load the LTC */
4036cafc3662SHoratiu Vultur 	phy_write_mmd(phydev, 2, LAN8841_PTP_CMD_CTL,
4037cafc3662SHoratiu Vultur 		      LAN8841_PTP_CMD_CTL_PTP_LTC_LOAD);
4038e4ed8ba0SHoratiu Vultur 	ret = lan8841_ptp_update_target(ptp_priv, ts);
4039cafc3662SHoratiu Vultur 	mutex_unlock(&ptp_priv->ptp_lock);
4040cafc3662SHoratiu Vultur 
4041cc755495SHoratiu Vultur 	spin_lock_irqsave(&ptp_priv->seconds_lock, flags);
4042cc755495SHoratiu Vultur 	ptp_priv->seconds = ts->tv_sec;
4043cc755495SHoratiu Vultur 	spin_unlock_irqrestore(&ptp_priv->seconds_lock, flags);
4044cc755495SHoratiu Vultur 
4045e4ed8ba0SHoratiu Vultur 	return ret;
4046cafc3662SHoratiu Vultur }
4047cafc3662SHoratiu Vultur 
4048cafc3662SHoratiu Vultur #define LAN8841_PTP_LTC_RD_SEC_HI	358
4049cafc3662SHoratiu Vultur #define LAN8841_PTP_LTC_RD_SEC_MID	359
4050cafc3662SHoratiu Vultur #define LAN8841_PTP_LTC_RD_SEC_LO	360
4051cafc3662SHoratiu Vultur #define LAN8841_PTP_LTC_RD_NS_HI	361
4052cafc3662SHoratiu Vultur #define LAN8841_PTP_LTC_RD_NS_LO	362
4053cafc3662SHoratiu Vultur #define LAN8841_PTP_CMD_CTL_PTP_LTC_READ	BIT(3)
4054cafc3662SHoratiu Vultur 
lan8841_ptp_gettime64(struct ptp_clock_info * ptp,struct timespec64 * ts)4055cafc3662SHoratiu Vultur static int lan8841_ptp_gettime64(struct ptp_clock_info *ptp,
4056cafc3662SHoratiu Vultur 				 struct timespec64 *ts)
4057cafc3662SHoratiu Vultur {
4058cafc3662SHoratiu Vultur 	struct kszphy_ptp_priv *ptp_priv = container_of(ptp, struct kszphy_ptp_priv,
4059cafc3662SHoratiu Vultur 							ptp_clock_info);
4060cafc3662SHoratiu Vultur 	struct phy_device *phydev = ptp_priv->phydev;
4061cafc3662SHoratiu Vultur 	time64_t s;
4062cafc3662SHoratiu Vultur 	s64 ns;
4063cafc3662SHoratiu Vultur 
4064cafc3662SHoratiu Vultur 	mutex_lock(&ptp_priv->ptp_lock);
4065cafc3662SHoratiu Vultur 	/* Issue the command to read the LTC */
4066cafc3662SHoratiu Vultur 	phy_write_mmd(phydev, 2, LAN8841_PTP_CMD_CTL,
4067cafc3662SHoratiu Vultur 		      LAN8841_PTP_CMD_CTL_PTP_LTC_READ);
4068cafc3662SHoratiu Vultur 
4069cafc3662SHoratiu Vultur 	/* Read the LTC */
4070cafc3662SHoratiu Vultur 	s = phy_read_mmd(phydev, 2, LAN8841_PTP_LTC_RD_SEC_HI);
4071cafc3662SHoratiu Vultur 	s <<= 16;
4072cafc3662SHoratiu Vultur 	s |= phy_read_mmd(phydev, 2, LAN8841_PTP_LTC_RD_SEC_MID);
4073cafc3662SHoratiu Vultur 	s <<= 16;
4074cafc3662SHoratiu Vultur 	s |= phy_read_mmd(phydev, 2, LAN8841_PTP_LTC_RD_SEC_LO);
4075cafc3662SHoratiu Vultur 
4076cafc3662SHoratiu Vultur 	ns = phy_read_mmd(phydev, 2, LAN8841_PTP_LTC_RD_NS_HI) & 0x3fff;
4077cafc3662SHoratiu Vultur 	ns <<= 16;
4078cafc3662SHoratiu Vultur 	ns |= phy_read_mmd(phydev, 2, LAN8841_PTP_LTC_RD_NS_LO);
4079cafc3662SHoratiu Vultur 	mutex_unlock(&ptp_priv->ptp_lock);
4080cafc3662SHoratiu Vultur 
4081cafc3662SHoratiu Vultur 	set_normalized_timespec64(ts, s, ns);
4082cafc3662SHoratiu Vultur 	return 0;
4083cafc3662SHoratiu Vultur }
4084cafc3662SHoratiu Vultur 
lan8841_ptp_getseconds(struct ptp_clock_info * ptp,struct timespec64 * ts)4085cc755495SHoratiu Vultur static void lan8841_ptp_getseconds(struct ptp_clock_info *ptp,
4086cc755495SHoratiu Vultur 				   struct timespec64 *ts)
4087cc755495SHoratiu Vultur {
4088cc755495SHoratiu Vultur 	struct kszphy_ptp_priv *ptp_priv = container_of(ptp, struct kszphy_ptp_priv,
4089cc755495SHoratiu Vultur 							ptp_clock_info);
4090cc755495SHoratiu Vultur 	struct phy_device *phydev = ptp_priv->phydev;
4091cc755495SHoratiu Vultur 	time64_t s;
4092cc755495SHoratiu Vultur 
4093cc755495SHoratiu Vultur 	mutex_lock(&ptp_priv->ptp_lock);
4094cc755495SHoratiu Vultur 	/* Issue the command to read the LTC */
4095cc755495SHoratiu Vultur 	phy_write_mmd(phydev, 2, LAN8841_PTP_CMD_CTL,
4096cc755495SHoratiu Vultur 		      LAN8841_PTP_CMD_CTL_PTP_LTC_READ);
4097cc755495SHoratiu Vultur 
4098cc755495SHoratiu Vultur 	/* Read the LTC */
4099cc755495SHoratiu Vultur 	s = phy_read_mmd(phydev, 2, LAN8841_PTP_LTC_RD_SEC_HI);
4100cc755495SHoratiu Vultur 	s <<= 16;
4101cc755495SHoratiu Vultur 	s |= phy_read_mmd(phydev, 2, LAN8841_PTP_LTC_RD_SEC_MID);
4102cc755495SHoratiu Vultur 	s <<= 16;
4103cc755495SHoratiu Vultur 	s |= phy_read_mmd(phydev, 2, LAN8841_PTP_LTC_RD_SEC_LO);
4104cc755495SHoratiu Vultur 	mutex_unlock(&ptp_priv->ptp_lock);
4105cc755495SHoratiu Vultur 
4106cc755495SHoratiu Vultur 	set_normalized_timespec64(ts, s, 0);
4107cc755495SHoratiu Vultur }
4108cc755495SHoratiu Vultur 
4109cafc3662SHoratiu Vultur #define LAN8841_PTP_LTC_STEP_ADJ_LO			276
4110cafc3662SHoratiu Vultur #define LAN8841_PTP_LTC_STEP_ADJ_HI			275
4111cafc3662SHoratiu Vultur #define LAN8841_PTP_LTC_STEP_ADJ_DIR			BIT(15)
4112cafc3662SHoratiu Vultur #define LAN8841_PTP_CMD_CTL_PTP_LTC_STEP_SECONDS	BIT(5)
4113cafc3662SHoratiu Vultur #define LAN8841_PTP_CMD_CTL_PTP_LTC_STEP_NANOSECONDS	BIT(6)
4114cafc3662SHoratiu Vultur 
lan8841_ptp_adjtime(struct ptp_clock_info * ptp,s64 delta)4115cafc3662SHoratiu Vultur static int lan8841_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
4116cafc3662SHoratiu Vultur {
4117cafc3662SHoratiu Vultur 	struct kszphy_ptp_priv *ptp_priv = container_of(ptp, struct kszphy_ptp_priv,
4118cafc3662SHoratiu Vultur 							ptp_clock_info);
4119cafc3662SHoratiu Vultur 	struct phy_device *phydev = ptp_priv->phydev;
4120cafc3662SHoratiu Vultur 	struct timespec64 ts;
4121cafc3662SHoratiu Vultur 	bool add = true;
4122cafc3662SHoratiu Vultur 	u32 nsec;
4123cafc3662SHoratiu Vultur 	s32 sec;
4124e4ed8ba0SHoratiu Vultur 	int ret;
4125cafc3662SHoratiu Vultur 
4126cafc3662SHoratiu Vultur 	/* The HW allows up to 15 sec to adjust the time, but here we limit to
4127cafc3662SHoratiu Vultur 	 * 10 sec the adjustment. The reason is, in case the adjustment is 14
4128cafc3662SHoratiu Vultur 	 * sec and 999999999 nsec, then we add 8ns to compansate the actual
4129cafc3662SHoratiu Vultur 	 * increment so the value can be bigger than 15 sec. Therefore limit the
4130cafc3662SHoratiu Vultur 	 * possible adjustments so we will not have these corner cases
4131cafc3662SHoratiu Vultur 	 */
4132cafc3662SHoratiu Vultur 	if (delta > 10000000000LL || delta < -10000000000LL) {
4133cafc3662SHoratiu Vultur 		/* The timeadjustment is too big, so fall back using set time */
4134cafc3662SHoratiu Vultur 		u64 now;
4135cafc3662SHoratiu Vultur 
4136cafc3662SHoratiu Vultur 		ptp->gettime64(ptp, &ts);
4137cafc3662SHoratiu Vultur 
4138cafc3662SHoratiu Vultur 		now = ktime_to_ns(timespec64_to_ktime(ts));
4139cafc3662SHoratiu Vultur 		ts = ns_to_timespec64(now + delta);
4140cafc3662SHoratiu Vultur 
4141cafc3662SHoratiu Vultur 		ptp->settime64(ptp, &ts);
4142cafc3662SHoratiu Vultur 		return 0;
4143cafc3662SHoratiu Vultur 	}
4144cafc3662SHoratiu Vultur 
4145cafc3662SHoratiu Vultur 	sec = div_u64_rem(delta < 0 ? -delta : delta, NSEC_PER_SEC, &nsec);
4146cafc3662SHoratiu Vultur 	if (delta < 0 && nsec != 0) {
4147cafc3662SHoratiu Vultur 		/* It is not allowed to adjust low the nsec part, therefore
4148cafc3662SHoratiu Vultur 		 * subtract more from second part and add to nanosecond such
4149cafc3662SHoratiu Vultur 		 * that would roll over, so the second part will increase
4150cafc3662SHoratiu Vultur 		 */
4151cafc3662SHoratiu Vultur 		sec--;
4152cafc3662SHoratiu Vultur 		nsec = NSEC_PER_SEC - nsec;
4153cafc3662SHoratiu Vultur 	}
4154cafc3662SHoratiu Vultur 
4155cafc3662SHoratiu Vultur 	/* Calculate the adjustments and the direction */
4156cafc3662SHoratiu Vultur 	if (delta < 0)
4157cafc3662SHoratiu Vultur 		add = false;
4158cafc3662SHoratiu Vultur 
4159cafc3662SHoratiu Vultur 	if (nsec > 0)
4160cafc3662SHoratiu Vultur 		/* add 8 ns to cover the likely normal increment */
4161cafc3662SHoratiu Vultur 		nsec += 8;
4162cafc3662SHoratiu Vultur 
4163cafc3662SHoratiu Vultur 	if (nsec >= NSEC_PER_SEC) {
4164cafc3662SHoratiu Vultur 		/* carry into seconds */
4165cafc3662SHoratiu Vultur 		sec++;
4166cafc3662SHoratiu Vultur 		nsec -= NSEC_PER_SEC;
4167cafc3662SHoratiu Vultur 	}
4168cafc3662SHoratiu Vultur 
4169cafc3662SHoratiu Vultur 	mutex_lock(&ptp_priv->ptp_lock);
4170cafc3662SHoratiu Vultur 	if (sec) {
4171cafc3662SHoratiu Vultur 		phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_STEP_ADJ_LO, sec);
4172cafc3662SHoratiu Vultur 		phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_STEP_ADJ_HI,
4173cafc3662SHoratiu Vultur 			      add ? LAN8841_PTP_LTC_STEP_ADJ_DIR : 0);
4174cafc3662SHoratiu Vultur 		phy_write_mmd(phydev, 2, LAN8841_PTP_CMD_CTL,
4175cafc3662SHoratiu Vultur 			      LAN8841_PTP_CMD_CTL_PTP_LTC_STEP_SECONDS);
4176cafc3662SHoratiu Vultur 	}
4177cafc3662SHoratiu Vultur 
4178cafc3662SHoratiu Vultur 	if (nsec) {
4179cafc3662SHoratiu Vultur 		phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_STEP_ADJ_LO,
4180cafc3662SHoratiu Vultur 			      nsec & 0xffff);
4181cafc3662SHoratiu Vultur 		phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_STEP_ADJ_HI,
4182cafc3662SHoratiu Vultur 			      (nsec >> 16) & 0x3fff);
4183cafc3662SHoratiu Vultur 		phy_write_mmd(phydev, 2, LAN8841_PTP_CMD_CTL,
4184cafc3662SHoratiu Vultur 			      LAN8841_PTP_CMD_CTL_PTP_LTC_STEP_NANOSECONDS);
4185cafc3662SHoratiu Vultur 	}
4186cafc3662SHoratiu Vultur 	mutex_unlock(&ptp_priv->ptp_lock);
4187cafc3662SHoratiu Vultur 
4188e4ed8ba0SHoratiu Vultur 	/* Update the target clock */
4189e4ed8ba0SHoratiu Vultur 	ptp->gettime64(ptp, &ts);
4190e4ed8ba0SHoratiu Vultur 	mutex_lock(&ptp_priv->ptp_lock);
4191e4ed8ba0SHoratiu Vultur 	ret = lan8841_ptp_update_target(ptp_priv, &ts);
4192e4ed8ba0SHoratiu Vultur 	mutex_unlock(&ptp_priv->ptp_lock);
4193e4ed8ba0SHoratiu Vultur 
4194e4ed8ba0SHoratiu Vultur 	return ret;
4195cafc3662SHoratiu Vultur }
4196cafc3662SHoratiu Vultur 
4197cafc3662SHoratiu Vultur #define LAN8841_PTP_LTC_RATE_ADJ_HI		269
4198cafc3662SHoratiu Vultur #define LAN8841_PTP_LTC_RATE_ADJ_HI_DIR		BIT(15)
4199cafc3662SHoratiu Vultur #define LAN8841_PTP_LTC_RATE_ADJ_LO		270
4200cafc3662SHoratiu Vultur 
lan8841_ptp_adjfine(struct ptp_clock_info * ptp,long scaled_ppm)4201cafc3662SHoratiu Vultur static int lan8841_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
4202cafc3662SHoratiu Vultur {
4203cafc3662SHoratiu Vultur 	struct kszphy_ptp_priv *ptp_priv = container_of(ptp, struct kszphy_ptp_priv,
4204cafc3662SHoratiu Vultur 							ptp_clock_info);
4205cafc3662SHoratiu Vultur 	struct phy_device *phydev = ptp_priv->phydev;
4206cafc3662SHoratiu Vultur 	bool faster = true;
4207cafc3662SHoratiu Vultur 	u32 rate;
4208cafc3662SHoratiu Vultur 
4209cafc3662SHoratiu Vultur 	if (!scaled_ppm)
4210cafc3662SHoratiu Vultur 		return 0;
4211cafc3662SHoratiu Vultur 
4212cafc3662SHoratiu Vultur 	if (scaled_ppm < 0) {
4213cafc3662SHoratiu Vultur 		scaled_ppm = -scaled_ppm;
4214cafc3662SHoratiu Vultur 		faster = false;
4215cafc3662SHoratiu Vultur 	}
4216cafc3662SHoratiu Vultur 
4217cafc3662SHoratiu Vultur 	rate = LAN8814_1PPM_FORMAT * (upper_16_bits(scaled_ppm));
4218cafc3662SHoratiu Vultur 	rate += (LAN8814_1PPM_FORMAT * (lower_16_bits(scaled_ppm))) >> 16;
4219cafc3662SHoratiu Vultur 
4220cafc3662SHoratiu Vultur 	mutex_lock(&ptp_priv->ptp_lock);
4221cafc3662SHoratiu Vultur 	phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_RATE_ADJ_HI,
4222cafc3662SHoratiu Vultur 		      faster ? LAN8841_PTP_LTC_RATE_ADJ_HI_DIR | (upper_16_bits(rate) & 0x3fff)
4223cafc3662SHoratiu Vultur 			     : upper_16_bits(rate) & 0x3fff);
4224cafc3662SHoratiu Vultur 	phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_RATE_ADJ_LO, lower_16_bits(rate));
4225cafc3662SHoratiu Vultur 	mutex_unlock(&ptp_priv->ptp_lock);
4226cafc3662SHoratiu Vultur 
4227cafc3662SHoratiu Vultur 	return 0;
4228cafc3662SHoratiu Vultur }
4229cafc3662SHoratiu Vultur 
lan8841_ptp_verify(struct ptp_clock_info * ptp,unsigned int pin,enum ptp_pin_function func,unsigned int chan)4230e4ed8ba0SHoratiu Vultur static int lan8841_ptp_verify(struct ptp_clock_info *ptp, unsigned int pin,
4231e4ed8ba0SHoratiu Vultur 			      enum ptp_pin_function func, unsigned int chan)
4232e4ed8ba0SHoratiu Vultur {
4233e4ed8ba0SHoratiu Vultur 	switch (func) {
4234e4ed8ba0SHoratiu Vultur 	case PTP_PF_NONE:
4235e4ed8ba0SHoratiu Vultur 	case PTP_PF_PEROUT:
4236fac63186SHoratiu Vultur 	case PTP_PF_EXTTS:
4237e4ed8ba0SHoratiu Vultur 		break;
4238e4ed8ba0SHoratiu Vultur 	default:
4239e4ed8ba0SHoratiu Vultur 		return -1;
4240e4ed8ba0SHoratiu Vultur 	}
4241e4ed8ba0SHoratiu Vultur 
4242e4ed8ba0SHoratiu Vultur 	return 0;
4243e4ed8ba0SHoratiu Vultur }
4244e4ed8ba0SHoratiu Vultur 
4245e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_GPIO_NUM	10
4246e4ed8ba0SHoratiu Vultur #define LAN8841_GPIO_EN		128
4247e4ed8ba0SHoratiu Vultur #define LAN8841_GPIO_DIR	129
4248e4ed8ba0SHoratiu Vultur #define LAN8841_GPIO_BUF	130
4249e4ed8ba0SHoratiu Vultur 
lan8841_ptp_perout_off(struct kszphy_ptp_priv * ptp_priv,int pin)4250e4ed8ba0SHoratiu Vultur static int lan8841_ptp_perout_off(struct kszphy_ptp_priv *ptp_priv, int pin)
4251e4ed8ba0SHoratiu Vultur {
4252e4ed8ba0SHoratiu Vultur 	struct phy_device *phydev = ptp_priv->phydev;
4253e4ed8ba0SHoratiu Vultur 	int ret;
4254e4ed8ba0SHoratiu Vultur 
4255e4ed8ba0SHoratiu Vultur 	ret = phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_EN, BIT(pin));
4256e4ed8ba0SHoratiu Vultur 	if (ret)
4257e4ed8ba0SHoratiu Vultur 		return ret;
4258e4ed8ba0SHoratiu Vultur 
4259e4ed8ba0SHoratiu Vultur 	ret = phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_DIR, BIT(pin));
4260e4ed8ba0SHoratiu Vultur 	if (ret)
4261e4ed8ba0SHoratiu Vultur 		return ret;
4262e4ed8ba0SHoratiu Vultur 
4263e4ed8ba0SHoratiu Vultur 	return phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_BUF, BIT(pin));
4264e4ed8ba0SHoratiu Vultur }
4265e4ed8ba0SHoratiu Vultur 
lan8841_ptp_perout_on(struct kszphy_ptp_priv * ptp_priv,int pin)4266e4ed8ba0SHoratiu Vultur static int lan8841_ptp_perout_on(struct kszphy_ptp_priv *ptp_priv, int pin)
4267e4ed8ba0SHoratiu Vultur {
4268e4ed8ba0SHoratiu Vultur 	struct phy_device *phydev = ptp_priv->phydev;
4269e4ed8ba0SHoratiu Vultur 	int ret;
4270e4ed8ba0SHoratiu Vultur 
4271e4ed8ba0SHoratiu Vultur 	ret = phy_set_bits_mmd(phydev, 2, LAN8841_GPIO_EN, BIT(pin));
4272e4ed8ba0SHoratiu Vultur 	if (ret)
4273e4ed8ba0SHoratiu Vultur 		return ret;
4274e4ed8ba0SHoratiu Vultur 
4275e4ed8ba0SHoratiu Vultur 	ret = phy_set_bits_mmd(phydev, 2, LAN8841_GPIO_DIR, BIT(pin));
4276e4ed8ba0SHoratiu Vultur 	if (ret)
4277e4ed8ba0SHoratiu Vultur 		return ret;
4278e4ed8ba0SHoratiu Vultur 
4279e4ed8ba0SHoratiu Vultur 	return phy_set_bits_mmd(phydev, 2, LAN8841_GPIO_BUF, BIT(pin));
4280e4ed8ba0SHoratiu Vultur }
4281e4ed8ba0SHoratiu Vultur 
4282e4ed8ba0SHoratiu Vultur #define LAN8841_GPIO_DATA_SEL1				131
4283e4ed8ba0SHoratiu Vultur #define LAN8841_GPIO_DATA_SEL2				132
4284e4ed8ba0SHoratiu Vultur #define LAN8841_GPIO_DATA_SEL_GPIO_DATA_SEL_EVENT_MASK	GENMASK(2, 0)
4285e4ed8ba0SHoratiu Vultur #define LAN8841_GPIO_DATA_SEL_GPIO_DATA_SEL_EVENT_A	1
4286e4ed8ba0SHoratiu Vultur #define LAN8841_GPIO_DATA_SEL_GPIO_DATA_SEL_EVENT_B	2
4287e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_GENERAL_CONFIG			257
4288e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_A	BIT(1)
4289e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_B	BIT(3)
4290e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_A_MASK	GENMASK(7, 4)
4291e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_B_MASK	GENMASK(11, 8)
4292e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_A		4
4293e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_B		7
4294e4ed8ba0SHoratiu Vultur 
lan8841_ptp_remove_event(struct kszphy_ptp_priv * ptp_priv,int pin,u8 event)4295e4ed8ba0SHoratiu Vultur static int lan8841_ptp_remove_event(struct kszphy_ptp_priv *ptp_priv, int pin,
4296e4ed8ba0SHoratiu Vultur 				    u8 event)
4297e4ed8ba0SHoratiu Vultur {
4298e4ed8ba0SHoratiu Vultur 	struct phy_device *phydev = ptp_priv->phydev;
4299e4ed8ba0SHoratiu Vultur 	u16 tmp;
4300e4ed8ba0SHoratiu Vultur 	int ret;
4301e4ed8ba0SHoratiu Vultur 
4302e4ed8ba0SHoratiu Vultur 	/* Now remove pin from the event. GPIO_DATA_SEL1 contains the GPIO
4303e4ed8ba0SHoratiu Vultur 	 * pins 0-4 while GPIO_DATA_SEL2 contains GPIO pins 5-9, therefore
4304e4ed8ba0SHoratiu Vultur 	 * depending on the pin, it requires to read a different register
4305e4ed8ba0SHoratiu Vultur 	 */
4306e4ed8ba0SHoratiu Vultur 	if (pin < 5) {
4307e4ed8ba0SHoratiu Vultur 		tmp = LAN8841_GPIO_DATA_SEL_GPIO_DATA_SEL_EVENT_MASK << (3 * pin);
4308e4ed8ba0SHoratiu Vultur 		ret = phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_DATA_SEL1, tmp);
4309e4ed8ba0SHoratiu Vultur 	} else {
4310e4ed8ba0SHoratiu Vultur 		tmp = LAN8841_GPIO_DATA_SEL_GPIO_DATA_SEL_EVENT_MASK << (3 * (pin - 5));
4311e4ed8ba0SHoratiu Vultur 		ret = phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_DATA_SEL2, tmp);
4312e4ed8ba0SHoratiu Vultur 	}
4313e4ed8ba0SHoratiu Vultur 	if (ret)
4314e4ed8ba0SHoratiu Vultur 		return ret;
4315e4ed8ba0SHoratiu Vultur 
4316e4ed8ba0SHoratiu Vultur 	/* Disable the event */
4317e4ed8ba0SHoratiu Vultur 	if (event == LAN8841_EVENT_A)
4318e4ed8ba0SHoratiu Vultur 		tmp = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_A |
4319e4ed8ba0SHoratiu Vultur 		      LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_A_MASK;
4320e4ed8ba0SHoratiu Vultur 	else
4321e4ed8ba0SHoratiu Vultur 		tmp = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_B |
4322e4ed8ba0SHoratiu Vultur 		      LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_B_MASK;
4323e4ed8ba0SHoratiu Vultur 	return phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_EN, tmp);
4324e4ed8ba0SHoratiu Vultur }
4325e4ed8ba0SHoratiu Vultur 
lan8841_ptp_enable_event(struct kszphy_ptp_priv * ptp_priv,int pin,u8 event,int pulse_width)4326e4ed8ba0SHoratiu Vultur static int lan8841_ptp_enable_event(struct kszphy_ptp_priv *ptp_priv, int pin,
4327e4ed8ba0SHoratiu Vultur 				    u8 event, int pulse_width)
4328e4ed8ba0SHoratiu Vultur {
4329e4ed8ba0SHoratiu Vultur 	struct phy_device *phydev = ptp_priv->phydev;
4330e4ed8ba0SHoratiu Vultur 	u16 tmp;
4331e4ed8ba0SHoratiu Vultur 	int ret;
4332e4ed8ba0SHoratiu Vultur 
4333e4ed8ba0SHoratiu Vultur 	/* Enable the event */
4334e4ed8ba0SHoratiu Vultur 	if (event == LAN8841_EVENT_A)
4335e4ed8ba0SHoratiu Vultur 		ret = phy_modify_mmd(phydev, 2, LAN8841_PTP_GENERAL_CONFIG,
4336e4ed8ba0SHoratiu Vultur 				     LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_A |
4337e4ed8ba0SHoratiu Vultur 				     LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_A_MASK,
4338e4ed8ba0SHoratiu Vultur 				     LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_A |
4339e4ed8ba0SHoratiu Vultur 				     pulse_width << LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_A);
4340e4ed8ba0SHoratiu Vultur 	else
4341e4ed8ba0SHoratiu Vultur 		ret = phy_modify_mmd(phydev, 2, LAN8841_PTP_GENERAL_CONFIG,
4342e4ed8ba0SHoratiu Vultur 				     LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_B |
4343e4ed8ba0SHoratiu Vultur 				     LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_B_MASK,
4344e4ed8ba0SHoratiu Vultur 				     LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_B |
4345e4ed8ba0SHoratiu Vultur 				     pulse_width << LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_B);
4346e4ed8ba0SHoratiu Vultur 	if (ret)
4347e4ed8ba0SHoratiu Vultur 		return ret;
4348e4ed8ba0SHoratiu Vultur 
4349e4ed8ba0SHoratiu Vultur 	/* Now connect the pin to the event. GPIO_DATA_SEL1 contains the GPIO
4350e4ed8ba0SHoratiu Vultur 	 * pins 0-4 while GPIO_DATA_SEL2 contains GPIO pins 5-9, therefore
4351e4ed8ba0SHoratiu Vultur 	 * depending on the pin, it requires to read a different register
4352e4ed8ba0SHoratiu Vultur 	 */
4353e4ed8ba0SHoratiu Vultur 	if (event == LAN8841_EVENT_A)
4354e4ed8ba0SHoratiu Vultur 		tmp = LAN8841_GPIO_DATA_SEL_GPIO_DATA_SEL_EVENT_A;
4355e4ed8ba0SHoratiu Vultur 	else
4356e4ed8ba0SHoratiu Vultur 		tmp = LAN8841_GPIO_DATA_SEL_GPIO_DATA_SEL_EVENT_B;
4357e4ed8ba0SHoratiu Vultur 
4358e4ed8ba0SHoratiu Vultur 	if (pin < 5)
4359e4ed8ba0SHoratiu Vultur 		ret = phy_set_bits_mmd(phydev, 2, LAN8841_GPIO_DATA_SEL1,
4360e4ed8ba0SHoratiu Vultur 				       tmp << (3 * pin));
4361e4ed8ba0SHoratiu Vultur 	else
4362e4ed8ba0SHoratiu Vultur 		ret = phy_set_bits_mmd(phydev, 2, LAN8841_GPIO_DATA_SEL2,
4363e4ed8ba0SHoratiu Vultur 				       tmp << (3 * (pin - 5)));
4364e4ed8ba0SHoratiu Vultur 
4365e4ed8ba0SHoratiu Vultur 	return ret;
4366e4ed8ba0SHoratiu Vultur }
4367e4ed8ba0SHoratiu Vultur 
4368e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_200MS	13
4369e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100MS	12
4370e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_50MS	11
4371e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_10MS	10
4372e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_5MS	9
4373e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_1MS	8
4374e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_500US	7
4375e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100US	6
4376e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_50US	5
4377e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_10US	4
4378e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_5US	3
4379e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_1US	2
4380e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_500NS	1
4381e4ed8ba0SHoratiu Vultur #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100NS	0
4382e4ed8ba0SHoratiu Vultur 
lan8841_ptp_perout(struct ptp_clock_info * ptp,struct ptp_clock_request * rq,int on)4383e4ed8ba0SHoratiu Vultur static int lan8841_ptp_perout(struct ptp_clock_info *ptp,
4384e4ed8ba0SHoratiu Vultur 			      struct ptp_clock_request *rq, int on)
4385e4ed8ba0SHoratiu Vultur {
4386e4ed8ba0SHoratiu Vultur 	struct kszphy_ptp_priv *ptp_priv = container_of(ptp, struct kszphy_ptp_priv,
4387e4ed8ba0SHoratiu Vultur 							ptp_clock_info);
4388e4ed8ba0SHoratiu Vultur 	struct phy_device *phydev = ptp_priv->phydev;
4389e4ed8ba0SHoratiu Vultur 	struct timespec64 ts_on, ts_period;
4390e4ed8ba0SHoratiu Vultur 	s64 on_nsec, period_nsec;
4391e4ed8ba0SHoratiu Vultur 	int pulse_width;
4392e4ed8ba0SHoratiu Vultur 	int pin;
4393e4ed8ba0SHoratiu Vultur 	int ret;
4394e4ed8ba0SHoratiu Vultur 
4395e4ed8ba0SHoratiu Vultur 	if (rq->perout.flags & ~PTP_PEROUT_DUTY_CYCLE)
4396e4ed8ba0SHoratiu Vultur 		return -EOPNOTSUPP;
4397e4ed8ba0SHoratiu Vultur 
4398e4ed8ba0SHoratiu Vultur 	pin = ptp_find_pin(ptp_priv->ptp_clock, PTP_PF_PEROUT, rq->perout.index);
4399e4ed8ba0SHoratiu Vultur 	if (pin == -1 || pin >= LAN8841_PTP_GPIO_NUM)
4400e4ed8ba0SHoratiu Vultur 		return -EINVAL;
4401e4ed8ba0SHoratiu Vultur 
4402e4ed8ba0SHoratiu Vultur 	if (!on) {
4403e4ed8ba0SHoratiu Vultur 		ret = lan8841_ptp_perout_off(ptp_priv, pin);
4404e4ed8ba0SHoratiu Vultur 		if (ret)
4405e4ed8ba0SHoratiu Vultur 			return ret;
4406e4ed8ba0SHoratiu Vultur 
4407e4ed8ba0SHoratiu Vultur 		return lan8841_ptp_remove_event(ptp_priv, LAN8841_EVENT_A, pin);
4408e4ed8ba0SHoratiu Vultur 	}
4409e4ed8ba0SHoratiu Vultur 
4410e4ed8ba0SHoratiu Vultur 	ts_on.tv_sec = rq->perout.on.sec;
4411e4ed8ba0SHoratiu Vultur 	ts_on.tv_nsec = rq->perout.on.nsec;
4412e4ed8ba0SHoratiu Vultur 	on_nsec = timespec64_to_ns(&ts_on);
4413e4ed8ba0SHoratiu Vultur 
4414e4ed8ba0SHoratiu Vultur 	ts_period.tv_sec = rq->perout.period.sec;
4415e4ed8ba0SHoratiu Vultur 	ts_period.tv_nsec = rq->perout.period.nsec;
4416e4ed8ba0SHoratiu Vultur 	period_nsec = timespec64_to_ns(&ts_period);
4417e4ed8ba0SHoratiu Vultur 
4418e4ed8ba0SHoratiu Vultur 	if (period_nsec < 200) {
44199bdf4489SColin Ian King 		pr_warn_ratelimited("%s: perout period too small, minimum is 200 nsec\n",
4420e4ed8ba0SHoratiu Vultur 				    phydev_name(phydev));
4421e4ed8ba0SHoratiu Vultur 		return -EOPNOTSUPP;
4422e4ed8ba0SHoratiu Vultur 	}
4423e4ed8ba0SHoratiu Vultur 
4424e4ed8ba0SHoratiu Vultur 	if (on_nsec >= period_nsec) {
4425e4ed8ba0SHoratiu Vultur 		pr_warn_ratelimited("%s: pulse width must be smaller than period\n",
4426e4ed8ba0SHoratiu Vultur 				    phydev_name(phydev));
4427e4ed8ba0SHoratiu Vultur 		return -EINVAL;
4428e4ed8ba0SHoratiu Vultur 	}
4429e4ed8ba0SHoratiu Vultur 
4430e4ed8ba0SHoratiu Vultur 	switch (on_nsec) {
4431e4ed8ba0SHoratiu Vultur 	case 200000000:
4432e4ed8ba0SHoratiu Vultur 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_200MS;
4433e4ed8ba0SHoratiu Vultur 		break;
4434e4ed8ba0SHoratiu Vultur 	case 100000000:
4435e4ed8ba0SHoratiu Vultur 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100MS;
4436e4ed8ba0SHoratiu Vultur 		break;
4437e4ed8ba0SHoratiu Vultur 	case 50000000:
4438e4ed8ba0SHoratiu Vultur 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_50MS;
4439e4ed8ba0SHoratiu Vultur 		break;
4440e4ed8ba0SHoratiu Vultur 	case 10000000:
4441e4ed8ba0SHoratiu Vultur 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_10MS;
4442e4ed8ba0SHoratiu Vultur 		break;
4443e4ed8ba0SHoratiu Vultur 	case 5000000:
4444e4ed8ba0SHoratiu Vultur 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_5MS;
4445e4ed8ba0SHoratiu Vultur 		break;
4446e4ed8ba0SHoratiu Vultur 	case 1000000:
4447e4ed8ba0SHoratiu Vultur 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_1MS;
4448e4ed8ba0SHoratiu Vultur 		break;
4449e4ed8ba0SHoratiu Vultur 	case 500000:
4450e4ed8ba0SHoratiu Vultur 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_500US;
4451e4ed8ba0SHoratiu Vultur 		break;
4452e4ed8ba0SHoratiu Vultur 	case 100000:
4453e4ed8ba0SHoratiu Vultur 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100US;
4454e4ed8ba0SHoratiu Vultur 		break;
4455e4ed8ba0SHoratiu Vultur 	case 50000:
4456e4ed8ba0SHoratiu Vultur 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_50US;
4457e4ed8ba0SHoratiu Vultur 		break;
4458e4ed8ba0SHoratiu Vultur 	case 10000:
4459e4ed8ba0SHoratiu Vultur 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_10US;
4460e4ed8ba0SHoratiu Vultur 		break;
4461e4ed8ba0SHoratiu Vultur 	case 5000:
4462e4ed8ba0SHoratiu Vultur 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_5US;
4463e4ed8ba0SHoratiu Vultur 		break;
4464e4ed8ba0SHoratiu Vultur 	case 1000:
4465e4ed8ba0SHoratiu Vultur 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_1US;
4466e4ed8ba0SHoratiu Vultur 		break;
4467e4ed8ba0SHoratiu Vultur 	case 500:
4468e4ed8ba0SHoratiu Vultur 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_500NS;
4469e4ed8ba0SHoratiu Vultur 		break;
4470e4ed8ba0SHoratiu Vultur 	case 100:
4471e4ed8ba0SHoratiu Vultur 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100NS;
4472e4ed8ba0SHoratiu Vultur 		break;
4473e4ed8ba0SHoratiu Vultur 	default:
4474e4ed8ba0SHoratiu Vultur 		pr_warn_ratelimited("%s: Use default duty cycle of 100ns\n",
4475e4ed8ba0SHoratiu Vultur 				    phydev_name(phydev));
4476e4ed8ba0SHoratiu Vultur 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100NS;
4477e4ed8ba0SHoratiu Vultur 		break;
4478e4ed8ba0SHoratiu Vultur 	}
4479e4ed8ba0SHoratiu Vultur 
4480e4ed8ba0SHoratiu Vultur 	mutex_lock(&ptp_priv->ptp_lock);
4481e4ed8ba0SHoratiu Vultur 	ret = lan8841_ptp_set_target(ptp_priv, LAN8841_EVENT_A, rq->perout.start.sec,
4482e4ed8ba0SHoratiu Vultur 				     rq->perout.start.nsec);
4483e4ed8ba0SHoratiu Vultur 	mutex_unlock(&ptp_priv->ptp_lock);
4484e4ed8ba0SHoratiu Vultur 	if (ret)
4485e4ed8ba0SHoratiu Vultur 		return ret;
4486e4ed8ba0SHoratiu Vultur 
4487e4ed8ba0SHoratiu Vultur 	ret = lan8841_ptp_set_reload(ptp_priv, LAN8841_EVENT_A, rq->perout.period.sec,
4488e4ed8ba0SHoratiu Vultur 				     rq->perout.period.nsec);
4489e4ed8ba0SHoratiu Vultur 	if (ret)
4490e4ed8ba0SHoratiu Vultur 		return ret;
4491e4ed8ba0SHoratiu Vultur 
4492e4ed8ba0SHoratiu Vultur 	ret = lan8841_ptp_enable_event(ptp_priv, pin, LAN8841_EVENT_A,
4493e4ed8ba0SHoratiu Vultur 				       pulse_width);
4494e4ed8ba0SHoratiu Vultur 	if (ret)
4495e4ed8ba0SHoratiu Vultur 		return ret;
4496e4ed8ba0SHoratiu Vultur 
4497e4ed8ba0SHoratiu Vultur 	ret = lan8841_ptp_perout_on(ptp_priv, pin);
4498e4ed8ba0SHoratiu Vultur 	if (ret)
4499e4ed8ba0SHoratiu Vultur 		lan8841_ptp_remove_event(ptp_priv, pin, LAN8841_EVENT_A);
4500e4ed8ba0SHoratiu Vultur 
4501e4ed8ba0SHoratiu Vultur 	return ret;
4502e4ed8ba0SHoratiu Vultur }
4503e4ed8ba0SHoratiu Vultur 
4504fac63186SHoratiu Vultur #define LAN8841_PTP_GPIO_CAP_EN			496
4505fac63186SHoratiu Vultur #define LAN8841_PTP_GPIO_CAP_EN_GPIO_RE_CAPTURE_ENABLE(gpio)	(BIT(gpio))
4506fac63186SHoratiu Vultur #define LAN8841_PTP_GPIO_CAP_EN_GPIO_FE_CAPTURE_ENABLE(gpio)	(BIT(gpio) << 8)
4507fac63186SHoratiu Vultur #define LAN8841_PTP_INT_EN_PTP_GPIO_CAP_EN	BIT(2)
4508fac63186SHoratiu Vultur 
lan8841_ptp_extts_on(struct kszphy_ptp_priv * ptp_priv,int pin,u32 flags)4509fac63186SHoratiu Vultur static int lan8841_ptp_extts_on(struct kszphy_ptp_priv *ptp_priv, int pin,
4510fac63186SHoratiu Vultur 				u32 flags)
4511fac63186SHoratiu Vultur {
4512fac63186SHoratiu Vultur 	struct phy_device *phydev = ptp_priv->phydev;
4513fac63186SHoratiu Vultur 	u16 tmp = 0;
4514fac63186SHoratiu Vultur 	int ret;
4515fac63186SHoratiu Vultur 
4516fac63186SHoratiu Vultur 	/* Set GPIO to be intput */
4517fac63186SHoratiu Vultur 	ret = phy_set_bits_mmd(phydev, 2, LAN8841_GPIO_EN, BIT(pin));
4518fac63186SHoratiu Vultur 	if (ret)
4519fac63186SHoratiu Vultur 		return ret;
4520fac63186SHoratiu Vultur 
4521fac63186SHoratiu Vultur 	ret = phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_BUF, BIT(pin));
4522fac63186SHoratiu Vultur 	if (ret)
4523fac63186SHoratiu Vultur 		return ret;
4524fac63186SHoratiu Vultur 
4525fac63186SHoratiu Vultur 	/* Enable capture on the edges of the pin */
4526fac63186SHoratiu Vultur 	if (flags & PTP_RISING_EDGE)
4527fac63186SHoratiu Vultur 		tmp |= LAN8841_PTP_GPIO_CAP_EN_GPIO_RE_CAPTURE_ENABLE(pin);
4528fac63186SHoratiu Vultur 	if (flags & PTP_FALLING_EDGE)
4529fac63186SHoratiu Vultur 		tmp |= LAN8841_PTP_GPIO_CAP_EN_GPIO_FE_CAPTURE_ENABLE(pin);
4530fac63186SHoratiu Vultur 	ret = phy_write_mmd(phydev, 2, LAN8841_PTP_GPIO_CAP_EN, tmp);
4531fac63186SHoratiu Vultur 	if (ret)
4532fac63186SHoratiu Vultur 		return ret;
4533fac63186SHoratiu Vultur 
4534fac63186SHoratiu Vultur 	/* Enable interrupt */
4535fac63186SHoratiu Vultur 	return phy_modify_mmd(phydev, 2, LAN8841_PTP_INT_EN,
4536fac63186SHoratiu Vultur 			      LAN8841_PTP_INT_EN_PTP_GPIO_CAP_EN,
4537fac63186SHoratiu Vultur 			      LAN8841_PTP_INT_EN_PTP_GPIO_CAP_EN);
4538fac63186SHoratiu Vultur }
4539fac63186SHoratiu Vultur 
lan8841_ptp_extts_off(struct kszphy_ptp_priv * ptp_priv,int pin)4540fac63186SHoratiu Vultur static int lan8841_ptp_extts_off(struct kszphy_ptp_priv *ptp_priv, int pin)
4541fac63186SHoratiu Vultur {
4542fac63186SHoratiu Vultur 	struct phy_device *phydev = ptp_priv->phydev;
4543fac63186SHoratiu Vultur 	int ret;
4544fac63186SHoratiu Vultur 
4545fac63186SHoratiu Vultur 	/* Set GPIO to be output */
4546fac63186SHoratiu Vultur 	ret = phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_EN, BIT(pin));
4547fac63186SHoratiu Vultur 	if (ret)
4548fac63186SHoratiu Vultur 		return ret;
4549fac63186SHoratiu Vultur 
4550fac63186SHoratiu Vultur 	ret = phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_BUF, BIT(pin));
4551fac63186SHoratiu Vultur 	if (ret)
4552fac63186SHoratiu Vultur 		return ret;
4553fac63186SHoratiu Vultur 
4554fac63186SHoratiu Vultur 	/* Disable capture on both of the edges */
4555fac63186SHoratiu Vultur 	ret = phy_modify_mmd(phydev, 2, LAN8841_PTP_GPIO_CAP_EN,
4556fac63186SHoratiu Vultur 			     LAN8841_PTP_GPIO_CAP_EN_GPIO_RE_CAPTURE_ENABLE(pin) |
4557fac63186SHoratiu Vultur 			     LAN8841_PTP_GPIO_CAP_EN_GPIO_FE_CAPTURE_ENABLE(pin),
4558fac63186SHoratiu Vultur 			     0);
4559fac63186SHoratiu Vultur 	if (ret)
4560fac63186SHoratiu Vultur 		return ret;
4561fac63186SHoratiu Vultur 
4562fac63186SHoratiu Vultur 	/* Disable interrupt */
4563fac63186SHoratiu Vultur 	return phy_modify_mmd(phydev, 2, LAN8841_PTP_INT_EN,
4564fac63186SHoratiu Vultur 			      LAN8841_PTP_INT_EN_PTP_GPIO_CAP_EN,
4565fac63186SHoratiu Vultur 			      0);
4566fac63186SHoratiu Vultur }
4567fac63186SHoratiu Vultur 
lan8841_ptp_extts(struct ptp_clock_info * ptp,struct ptp_clock_request * rq,int on)4568fac63186SHoratiu Vultur static int lan8841_ptp_extts(struct ptp_clock_info *ptp,
4569fac63186SHoratiu Vultur 			     struct ptp_clock_request *rq, int on)
4570fac63186SHoratiu Vultur {
4571fac63186SHoratiu Vultur 	struct kszphy_ptp_priv *ptp_priv = container_of(ptp, struct kszphy_ptp_priv,
4572fac63186SHoratiu Vultur 							ptp_clock_info);
4573fac63186SHoratiu Vultur 	int pin;
4574fac63186SHoratiu Vultur 	int ret;
4575fac63186SHoratiu Vultur 
4576fac63186SHoratiu Vultur 	/* Reject requests with unsupported flags */
4577fac63186SHoratiu Vultur 	if (rq->extts.flags & ~(PTP_ENABLE_FEATURE |
4578fac63186SHoratiu Vultur 				PTP_EXTTS_EDGES |
4579fac63186SHoratiu Vultur 				PTP_STRICT_FLAGS))
4580fac63186SHoratiu Vultur 		return -EOPNOTSUPP;
4581fac63186SHoratiu Vultur 
4582fac63186SHoratiu Vultur 	pin = ptp_find_pin(ptp_priv->ptp_clock, PTP_PF_EXTTS, rq->extts.index);
4583fac63186SHoratiu Vultur 	if (pin == -1 || pin >= LAN8841_PTP_GPIO_NUM)
4584fac63186SHoratiu Vultur 		return -EINVAL;
4585fac63186SHoratiu Vultur 
4586fac63186SHoratiu Vultur 	mutex_lock(&ptp_priv->ptp_lock);
4587fac63186SHoratiu Vultur 	if (on)
4588fac63186SHoratiu Vultur 		ret = lan8841_ptp_extts_on(ptp_priv, pin, rq->extts.flags);
4589fac63186SHoratiu Vultur 	else
4590fac63186SHoratiu Vultur 		ret = lan8841_ptp_extts_off(ptp_priv, pin);
4591fac63186SHoratiu Vultur 	mutex_unlock(&ptp_priv->ptp_lock);
4592fac63186SHoratiu Vultur 
4593fac63186SHoratiu Vultur 	return ret;
4594fac63186SHoratiu Vultur }
4595fac63186SHoratiu Vultur 
lan8841_ptp_enable(struct ptp_clock_info * ptp,struct ptp_clock_request * rq,int on)4596e4ed8ba0SHoratiu Vultur static int lan8841_ptp_enable(struct ptp_clock_info *ptp,
4597e4ed8ba0SHoratiu Vultur 			      struct ptp_clock_request *rq, int on)
4598e4ed8ba0SHoratiu Vultur {
4599e4ed8ba0SHoratiu Vultur 	switch (rq->type) {
4600fac63186SHoratiu Vultur 	case PTP_CLK_REQ_EXTTS:
4601fac63186SHoratiu Vultur 		return lan8841_ptp_extts(ptp, rq, on);
4602e4ed8ba0SHoratiu Vultur 	case PTP_CLK_REQ_PEROUT:
4603e4ed8ba0SHoratiu Vultur 		return lan8841_ptp_perout(ptp, rq, on);
4604e4ed8ba0SHoratiu Vultur 	default:
4605e4ed8ba0SHoratiu Vultur 		return -EOPNOTSUPP;
4606e4ed8ba0SHoratiu Vultur 	}
4607e4ed8ba0SHoratiu Vultur 
4608e4ed8ba0SHoratiu Vultur 	return 0;
4609e4ed8ba0SHoratiu Vultur }
4610e4ed8ba0SHoratiu Vultur 
lan8841_ptp_do_aux_work(struct ptp_clock_info * ptp)4611cc755495SHoratiu Vultur static long lan8841_ptp_do_aux_work(struct ptp_clock_info *ptp)
4612cc755495SHoratiu Vultur {
4613cc755495SHoratiu Vultur 	struct kszphy_ptp_priv *ptp_priv = container_of(ptp, struct kszphy_ptp_priv,
4614cc755495SHoratiu Vultur 							ptp_clock_info);
4615cc755495SHoratiu Vultur 	struct timespec64 ts;
4616cc755495SHoratiu Vultur 	unsigned long flags;
4617cc755495SHoratiu Vultur 
4618cc755495SHoratiu Vultur 	lan8841_ptp_getseconds(&ptp_priv->ptp_clock_info, &ts);
4619cc755495SHoratiu Vultur 
4620cc755495SHoratiu Vultur 	spin_lock_irqsave(&ptp_priv->seconds_lock, flags);
4621cc755495SHoratiu Vultur 	ptp_priv->seconds = ts.tv_sec;
4622cc755495SHoratiu Vultur 	spin_unlock_irqrestore(&ptp_priv->seconds_lock, flags);
4623cc755495SHoratiu Vultur 
4624cc755495SHoratiu Vultur 	return nsecs_to_jiffies(LAN8841_GET_SEC_LTC_DELAY);
4625cc755495SHoratiu Vultur }
4626cc755495SHoratiu Vultur 
4627cafc3662SHoratiu Vultur static struct ptp_clock_info lan8841_ptp_clock_info = {
4628cafc3662SHoratiu Vultur 	.owner		= THIS_MODULE,
4629cafc3662SHoratiu Vultur 	.name		= "lan8841 ptp",
4630cafc3662SHoratiu Vultur 	.max_adj	= 31249999,
4631cafc3662SHoratiu Vultur 	.gettime64	= lan8841_ptp_gettime64,
4632cafc3662SHoratiu Vultur 	.settime64	= lan8841_ptp_settime64,
4633cafc3662SHoratiu Vultur 	.adjtime	= lan8841_ptp_adjtime,
4634cafc3662SHoratiu Vultur 	.adjfine	= lan8841_ptp_adjfine,
4635e4ed8ba0SHoratiu Vultur 	.verify         = lan8841_ptp_verify,
4636e4ed8ba0SHoratiu Vultur 	.enable         = lan8841_ptp_enable,
4637cc755495SHoratiu Vultur 	.do_aux_work	= lan8841_ptp_do_aux_work,
4638e4ed8ba0SHoratiu Vultur 	.n_per_out      = LAN8841_PTP_GPIO_NUM,
4639fac63186SHoratiu Vultur 	.n_ext_ts       = LAN8841_PTP_GPIO_NUM,
4640e4ed8ba0SHoratiu Vultur 	.n_pins         = LAN8841_PTP_GPIO_NUM,
4641cafc3662SHoratiu Vultur };
4642cafc3662SHoratiu Vultur 
4643a8f1a19dSHoratiu Vultur #define LAN8841_OPERATION_MODE_STRAP_LOW_REGISTER 3
4644a8f1a19dSHoratiu Vultur #define LAN8841_OPERATION_MODE_STRAP_LOW_REGISTER_STRAP_RGMII_EN BIT(0)
4645a8f1a19dSHoratiu Vultur 
lan8841_probe(struct phy_device * phydev)4646a8f1a19dSHoratiu Vultur static int lan8841_probe(struct phy_device *phydev)
4647a8f1a19dSHoratiu Vultur {
4648cafc3662SHoratiu Vultur 	struct kszphy_ptp_priv *ptp_priv;
4649cafc3662SHoratiu Vultur 	struct kszphy_priv *priv;
4650a8f1a19dSHoratiu Vultur 	int err;
4651a8f1a19dSHoratiu Vultur 
4652a8f1a19dSHoratiu Vultur 	err = kszphy_probe(phydev);
4653a8f1a19dSHoratiu Vultur 	if (err)
4654a8f1a19dSHoratiu Vultur 		return err;
4655a8f1a19dSHoratiu Vultur 
4656a8f1a19dSHoratiu Vultur 	if (phy_read_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
4657a8f1a19dSHoratiu Vultur 			 LAN8841_OPERATION_MODE_STRAP_LOW_REGISTER) &
4658a8f1a19dSHoratiu Vultur 	    LAN8841_OPERATION_MODE_STRAP_LOW_REGISTER_STRAP_RGMII_EN)
4659a8f1a19dSHoratiu Vultur 		phydev->interface = PHY_INTERFACE_MODE_RGMII_RXID;
4660a8f1a19dSHoratiu Vultur 
4661cafc3662SHoratiu Vultur 	/* Register the clock */
4662cafc3662SHoratiu Vultur 	if (!IS_ENABLED(CONFIG_NETWORK_PHY_TIMESTAMPING))
4663cafc3662SHoratiu Vultur 		return 0;
4664cafc3662SHoratiu Vultur 
4665cafc3662SHoratiu Vultur 	priv = phydev->priv;
4666cafc3662SHoratiu Vultur 	ptp_priv = &priv->ptp_priv;
4667cafc3662SHoratiu Vultur 
4668e4ed8ba0SHoratiu Vultur 	ptp_priv->pin_config = devm_kcalloc(&phydev->mdio.dev,
4669e4ed8ba0SHoratiu Vultur 					    LAN8841_PTP_GPIO_NUM,
4670e4ed8ba0SHoratiu Vultur 					    sizeof(*ptp_priv->pin_config),
4671e4ed8ba0SHoratiu Vultur 					    GFP_KERNEL);
4672e4ed8ba0SHoratiu Vultur 	if (!ptp_priv->pin_config)
4673e4ed8ba0SHoratiu Vultur 		return -ENOMEM;
4674e4ed8ba0SHoratiu Vultur 
4675e4ed8ba0SHoratiu Vultur 	for (int i = 0; i < LAN8841_PTP_GPIO_NUM; ++i) {
4676e4ed8ba0SHoratiu Vultur 		struct ptp_pin_desc *p = &ptp_priv->pin_config[i];
4677e4ed8ba0SHoratiu Vultur 
4678e4ed8ba0SHoratiu Vultur 		snprintf(p->name, sizeof(p->name), "pin%d", i);
4679e4ed8ba0SHoratiu Vultur 		p->index = i;
4680e4ed8ba0SHoratiu Vultur 		p->func = PTP_PF_NONE;
4681e4ed8ba0SHoratiu Vultur 	}
4682e4ed8ba0SHoratiu Vultur 
4683cafc3662SHoratiu Vultur 	ptp_priv->ptp_clock_info = lan8841_ptp_clock_info;
4684e4ed8ba0SHoratiu Vultur 	ptp_priv->ptp_clock_info.pin_config = ptp_priv->pin_config;
4685cafc3662SHoratiu Vultur 	ptp_priv->ptp_clock = ptp_clock_register(&ptp_priv->ptp_clock_info,
4686cafc3662SHoratiu Vultur 						 &phydev->mdio.dev);
4687cafc3662SHoratiu Vultur 	if (IS_ERR(ptp_priv->ptp_clock)) {
4688cafc3662SHoratiu Vultur 		phydev_err(phydev, "ptp_clock_register failed: %lu\n",
4689cafc3662SHoratiu Vultur 			   PTR_ERR(ptp_priv->ptp_clock));
4690cafc3662SHoratiu Vultur 		return -EINVAL;
4691cafc3662SHoratiu Vultur 	}
4692cafc3662SHoratiu Vultur 
4693cafc3662SHoratiu Vultur 	if (!ptp_priv->ptp_clock)
4694cafc3662SHoratiu Vultur 		return 0;
4695cafc3662SHoratiu Vultur 
4696cafc3662SHoratiu Vultur 	/* Initialize the SW */
4697cafc3662SHoratiu Vultur 	skb_queue_head_init(&ptp_priv->tx_queue);
4698cafc3662SHoratiu Vultur 	ptp_priv->phydev = phydev;
4699cafc3662SHoratiu Vultur 	mutex_init(&ptp_priv->ptp_lock);
4700cc755495SHoratiu Vultur 	spin_lock_init(&ptp_priv->seconds_lock);
4701cafc3662SHoratiu Vultur 
4702cc755495SHoratiu Vultur 	ptp_priv->mii_ts.rxtstamp = lan8841_rxtstamp;
4703cafc3662SHoratiu Vultur 	ptp_priv->mii_ts.txtstamp = lan8814_txtstamp;
4704cafc3662SHoratiu Vultur 	ptp_priv->mii_ts.hwtstamp = lan8841_hwtstamp;
4705cafc3662SHoratiu Vultur 	ptp_priv->mii_ts.ts_info = lan8841_ts_info;
4706cafc3662SHoratiu Vultur 
4707cafc3662SHoratiu Vultur 	phydev->mii_ts = &ptp_priv->mii_ts;
4708cafc3662SHoratiu Vultur 
4709a8f1a19dSHoratiu Vultur 	return 0;
4710a8f1a19dSHoratiu Vultur }
4711a8f1a19dSHoratiu Vultur 
lan8841_suspend(struct phy_device * phydev)4712cc755495SHoratiu Vultur static int lan8841_suspend(struct phy_device *phydev)
4713cc755495SHoratiu Vultur {
4714cc755495SHoratiu Vultur 	struct kszphy_priv *priv = phydev->priv;
4715cc755495SHoratiu Vultur 	struct kszphy_ptp_priv *ptp_priv = &priv->ptp_priv;
4716cc755495SHoratiu Vultur 
47173ddf170eSHoratiu Vultur 	if (ptp_priv->ptp_clock)
4718cc755495SHoratiu Vultur 		ptp_cancel_worker_sync(ptp_priv->ptp_clock);
4719cc755495SHoratiu Vultur 
4720cc755495SHoratiu Vultur 	return genphy_suspend(phydev);
4721cc755495SHoratiu Vultur }
4722cc755495SHoratiu Vultur 
4723d5bf9071SChristian Hohnstaedt static struct phy_driver ksphy_driver[] = {
4724d5bf9071SChristian Hohnstaedt {
472551f932c4SChoi, David 	.phy_id		= PHY_ID_KS8737,
4726f893a99eSFabio Estevam 	.phy_id_mask	= MICREL_PHY_ID_MASK,
472751f932c4SChoi, David 	.name		= "Micrel KS8737",
4728dcdecdcfSHeiner Kallweit 	/* PHY_BASIC_FEATURES */
4729c6f9575cSJohan Hovold 	.driver_data	= &ks8737_type,
473015f03ffeSFabio Estevam 	.probe		= kszphy_probe,
4731d0507009SDavid J. Choi 	.config_init	= kszphy_config_init,
4732c6f9575cSJohan Hovold 	.config_intr	= kszphy_config_intr,
473359ca4e58SIoana Ciornei 	.handle_interrupt = kszphy_handle_interrupt,
4734f1131b9cSClaudiu Beznea 	.suspend	= kszphy_suspend,
4735f1131b9cSClaudiu Beznea 	.resume		= kszphy_resume,
4736d5bf9071SChristian Hohnstaedt }, {
4737212ea99aSMarek Vasut 	.phy_id		= PHY_ID_KSZ8021,
4738212ea99aSMarek Vasut 	.phy_id_mask	= 0x00ffffff,
47397ab59dc1SDavid J. Choi 	.name		= "Micrel KSZ8021 or KSZ8031",
4740dcdecdcfSHeiner Kallweit 	/* PHY_BASIC_FEATURES */
4741e6a423a8SJohan Hovold 	.driver_data	= &ksz8021_type,
474263f44b2bSJohan Hovold 	.probe		= kszphy_probe,
4743d0e1df9cSJohan Hovold 	.config_init	= kszphy_config_init,
4744212ea99aSMarek Vasut 	.config_intr	= kszphy_config_intr,
474559ca4e58SIoana Ciornei 	.handle_interrupt = kszphy_handle_interrupt,
47462b2427d0SAndrew Lunn 	.get_sset_count = kszphy_get_sset_count,
47472b2427d0SAndrew Lunn 	.get_strings	= kszphy_get_strings,
47482b2427d0SAndrew Lunn 	.get_stats	= kszphy_get_stats,
4749f1131b9cSClaudiu Beznea 	.suspend	= kszphy_suspend,
4750f1131b9cSClaudiu Beznea 	.resume		= kszphy_resume,
4751212ea99aSMarek Vasut }, {
4752b818d1a7SHector Palacios 	.phy_id		= PHY_ID_KSZ8031,
4753b818d1a7SHector Palacios 	.phy_id_mask	= 0x00ffffff,
4754b818d1a7SHector Palacios 	.name		= "Micrel KSZ8031",
4755dcdecdcfSHeiner Kallweit 	/* PHY_BASIC_FEATURES */
4756e6a423a8SJohan Hovold 	.driver_data	= &ksz8021_type,
475763f44b2bSJohan Hovold 	.probe		= kszphy_probe,
4758d0e1df9cSJohan Hovold 	.config_init	= kszphy_config_init,
4759b818d1a7SHector Palacios 	.config_intr	= kszphy_config_intr,
476059ca4e58SIoana Ciornei 	.handle_interrupt = kszphy_handle_interrupt,
47612b2427d0SAndrew Lunn 	.get_sset_count = kszphy_get_sset_count,
47622b2427d0SAndrew Lunn 	.get_strings	= kszphy_get_strings,
47632b2427d0SAndrew Lunn 	.get_stats	= kszphy_get_stats,
4764f1131b9cSClaudiu Beznea 	.suspend	= kszphy_suspend,
4765f1131b9cSClaudiu Beznea 	.resume		= kszphy_resume,
4766b818d1a7SHector Palacios }, {
4767510d573fSMarek Vasut 	.phy_id		= PHY_ID_KSZ8041,
4768f893a99eSFabio Estevam 	.phy_id_mask	= MICREL_PHY_ID_MASK,
4769510d573fSMarek Vasut 	.name		= "Micrel KSZ8041",
4770dcdecdcfSHeiner Kallweit 	/* PHY_BASIC_FEATURES */
4771e6a423a8SJohan Hovold 	.driver_data	= &ksz8041_type,
4772e6a423a8SJohan Hovold 	.probe		= kszphy_probe,
477377501a79SPhilipp Zabel 	.config_init	= ksz8041_config_init,
477477501a79SPhilipp Zabel 	.config_aneg	= ksz8041_config_aneg,
477551f932c4SChoi, David 	.config_intr	= kszphy_config_intr,
477659ca4e58SIoana Ciornei 	.handle_interrupt = kszphy_handle_interrupt,
47772b2427d0SAndrew Lunn 	.get_sset_count = kszphy_get_sset_count,
47782b2427d0SAndrew Lunn 	.get_strings	= kszphy_get_strings,
47792b2427d0SAndrew Lunn 	.get_stats	= kszphy_get_stats,
47802641b62dSStefan Agner 	/* No suspend/resume callbacks because of errata DS80000700A,
47812641b62dSStefan Agner 	 * receiver error following software power down.
47822641b62dSStefan Agner 	 */
4783d5bf9071SChristian Hohnstaedt }, {
47844bd7b512SSergei Shtylyov 	.phy_id		= PHY_ID_KSZ8041RNLI,
4785f893a99eSFabio Estevam 	.phy_id_mask	= MICREL_PHY_ID_MASK,
47864bd7b512SSergei Shtylyov 	.name		= "Micrel KSZ8041RNLI",
4787dcdecdcfSHeiner Kallweit 	/* PHY_BASIC_FEATURES */
4788e6a423a8SJohan Hovold 	.driver_data	= &ksz8041_type,
4789e6a423a8SJohan Hovold 	.probe		= kszphy_probe,
4790e6a423a8SJohan Hovold 	.config_init	= kszphy_config_init,
47914bd7b512SSergei Shtylyov 	.config_intr	= kszphy_config_intr,
479259ca4e58SIoana Ciornei 	.handle_interrupt = kszphy_handle_interrupt,
47932b2427d0SAndrew Lunn 	.get_sset_count = kszphy_get_sset_count,
47942b2427d0SAndrew Lunn 	.get_strings	= kszphy_get_strings,
47952b2427d0SAndrew Lunn 	.get_stats	= kszphy_get_stats,
4796f1131b9cSClaudiu Beznea 	.suspend	= kszphy_suspend,
4797f1131b9cSClaudiu Beznea 	.resume		= kszphy_resume,
47984bd7b512SSergei Shtylyov }, {
4799510d573fSMarek Vasut 	.name		= "Micrel KSZ8051",
4800dcdecdcfSHeiner Kallweit 	/* PHY_BASIC_FEATURES */
4801e6a423a8SJohan Hovold 	.driver_data	= &ksz8051_type,
4802e6a423a8SJohan Hovold 	.probe		= kszphy_probe,
480363f44b2bSJohan Hovold 	.config_init	= kszphy_config_init,
480451f932c4SChoi, David 	.config_intr	= kszphy_config_intr,
480559ca4e58SIoana Ciornei 	.handle_interrupt = kszphy_handle_interrupt,
48062b2427d0SAndrew Lunn 	.get_sset_count = kszphy_get_sset_count,
48072b2427d0SAndrew Lunn 	.get_strings	= kszphy_get_strings,
48082b2427d0SAndrew Lunn 	.get_stats	= kszphy_get_stats,
48098b95599cSMarek Vasut 	.match_phy_device = ksz8051_match_phy_device,
4810f1131b9cSClaudiu Beznea 	.suspend	= kszphy_suspend,
4811f1131b9cSClaudiu Beznea 	.resume		= kszphy_resume,
4812d5bf9071SChristian Hohnstaedt }, {
4813510d573fSMarek Vasut 	.phy_id		= PHY_ID_KSZ8001,
4814510d573fSMarek Vasut 	.name		= "Micrel KSZ8001 or KS8721",
4815ecd5a323SAlexander Stein 	.phy_id_mask	= 0x00fffffc,
4816dcdecdcfSHeiner Kallweit 	/* PHY_BASIC_FEATURES */
4817e6a423a8SJohan Hovold 	.driver_data	= &ksz8041_type,
4818e6a423a8SJohan Hovold 	.probe		= kszphy_probe,
4819e6a423a8SJohan Hovold 	.config_init	= kszphy_config_init,
482051f932c4SChoi, David 	.config_intr	= kszphy_config_intr,
482159ca4e58SIoana Ciornei 	.handle_interrupt = kszphy_handle_interrupt,
48222b2427d0SAndrew Lunn 	.get_sset_count = kszphy_get_sset_count,
48232b2427d0SAndrew Lunn 	.get_strings	= kszphy_get_strings,
48242b2427d0SAndrew Lunn 	.get_stats	= kszphy_get_stats,
4825f1131b9cSClaudiu Beznea 	.suspend	= kszphy_suspend,
4826f1131b9cSClaudiu Beznea 	.resume		= kszphy_resume,
4827d5bf9071SChristian Hohnstaedt }, {
48287ab59dc1SDavid J. Choi 	.phy_id		= PHY_ID_KSZ8081,
48297ab59dc1SDavid J. Choi 	.name		= "Micrel KSZ8081 or KSZ8091",
4830f893a99eSFabio Estevam 	.phy_id_mask	= MICREL_PHY_ID_MASK,
483149011e0cSOleksij Rempel 	.flags		= PHY_POLL_CABLE_TEST,
4832dcdecdcfSHeiner Kallweit 	/* PHY_BASIC_FEATURES */
4833e6a423a8SJohan Hovold 	.driver_data	= &ksz8081_type,
4834e6a423a8SJohan Hovold 	.probe		= kszphy_probe,
48357a1d8390SAntoine Tenart 	.config_init	= ksz8081_config_init,
4836764d31caSChristian Melki 	.soft_reset	= genphy_soft_reset,
4837f873f112SOleksij Rempel 	.config_aneg	= ksz8081_config_aneg,
4838f873f112SOleksij Rempel 	.read_status	= ksz8081_read_status,
48397ab59dc1SDavid J. Choi 	.config_intr	= kszphy_config_intr,
484059ca4e58SIoana Ciornei 	.handle_interrupt = kszphy_handle_interrupt,
48412b2427d0SAndrew Lunn 	.get_sset_count = kszphy_get_sset_count,
48422b2427d0SAndrew Lunn 	.get_strings	= kszphy_get_strings,
48432b2427d0SAndrew Lunn 	.get_stats	= kszphy_get_stats,
4844836384d2SWenyou Yang 	.suspend	= kszphy_suspend,
4845f5aba91dSAlexandre Belloni 	.resume		= kszphy_resume,
484649011e0cSOleksij Rempel 	.cable_test_start	= ksz886x_cable_test_start,
484749011e0cSOleksij Rempel 	.cable_test_get_status	= ksz886x_cable_test_get_status,
48487ab59dc1SDavid J. Choi }, {
48497ab59dc1SDavid J. Choi 	.phy_id		= PHY_ID_KSZ8061,
48507ab59dc1SDavid J. Choi 	.name		= "Micrel KSZ8061",
4851f893a99eSFabio Estevam 	.phy_id_mask	= MICREL_PHY_ID_MASK,
4852dcdecdcfSHeiner Kallweit 	/* PHY_BASIC_FEATURES */
48538e6004dfSFabio Estevam 	.probe		= kszphy_probe,
4854232ba3a5SRajasingh Thavamani 	.config_init	= ksz8061_config_init,
485507327fcbSMathieu Othacehe 	.soft_reset	= genphy_soft_reset,
48567ab59dc1SDavid J. Choi 	.config_intr	= kszphy_config_intr,
485759ca4e58SIoana Ciornei 	.handle_interrupt = kszphy_handle_interrupt,
48588e6004dfSFabio Estevam 	.suspend	= kszphy_suspend,
4859cba54674STristram Ha 	.resume		= ksz8061_resume,
48607ab59dc1SDavid J. Choi }, {
4861d0507009SDavid J. Choi 	.phy_id		= PHY_ID_KSZ9021,
486248d7d0adSJason Wang 	.phy_id_mask	= 0x000ffffe,
4863d0507009SDavid J. Choi 	.name		= "Micrel KSZ9021 Gigabit PHY",
4864dcdecdcfSHeiner Kallweit 	/* PHY_GBIT_FEATURES */
4865c6f9575cSJohan Hovold 	.driver_data	= &ksz9021_type,
4866bfe72442SGrygorii Strashko 	.probe		= kszphy_probe,
4867407d8098SHans Andersson 	.get_features	= ksz9031_get_features,
4868954c3967SSean Cross 	.config_init	= ksz9021_config_init,
4869c6f9575cSJohan Hovold 	.config_intr	= kszphy_config_intr,
487059ca4e58SIoana Ciornei 	.handle_interrupt = kszphy_handle_interrupt,
48712b2427d0SAndrew Lunn 	.get_sset_count = kszphy_get_sset_count,
48722b2427d0SAndrew Lunn 	.get_strings	= kszphy_get_strings,
48732b2427d0SAndrew Lunn 	.get_stats	= kszphy_get_stats,
4874f1131b9cSClaudiu Beznea 	.suspend	= kszphy_suspend,
4875f1131b9cSClaudiu Beznea 	.resume		= kszphy_resume,
4876c846a2b7SKevin Hao 	.read_mmd	= genphy_read_mmd_unsupported,
4877c846a2b7SKevin Hao 	.write_mmd	= genphy_write_mmd_unsupported,
487893272e07SJean-Christophe PLAGNIOL-VILLARD }, {
48797ab59dc1SDavid J. Choi 	.phy_id		= PHY_ID_KSZ9031,
4880f893a99eSFabio Estevam 	.phy_id_mask	= MICREL_PHY_ID_MASK,
48817ab59dc1SDavid J. Choi 	.name		= "Micrel KSZ9031 Gigabit PHY",
488258389c00SMarek Vasut 	.flags		= PHY_POLL_CABLE_TEST,
4883c6f9575cSJohan Hovold 	.driver_data	= &ksz9021_type,
4884bfe72442SGrygorii Strashko 	.probe		= kszphy_probe,
48853aed3e2aSAntoine Tenart 	.get_features	= ksz9031_get_features,
48866e4b8273SHubert Chaumette 	.config_init	= ksz9031_config_init,
48871d16073aSHeiner Kallweit 	.soft_reset	= genphy_soft_reset,
4888d2fd719bSNathan Sullivan 	.read_status	= ksz9031_read_status,
4889c6f9575cSJohan Hovold 	.config_intr	= kszphy_config_intr,
489059ca4e58SIoana Ciornei 	.handle_interrupt = kszphy_handle_interrupt,
48912b2427d0SAndrew Lunn 	.get_sset_count = kszphy_get_sset_count,
48922b2427d0SAndrew Lunn 	.get_strings	= kszphy_get_strings,
48932b2427d0SAndrew Lunn 	.get_stats	= kszphy_get_stats,
4894f1131b9cSClaudiu Beznea 	.suspend	= kszphy_suspend,
4895f64f1482SXander Huff 	.resume		= kszphy_resume,
489658389c00SMarek Vasut 	.cable_test_start	= ksz9x31_cable_test_start,
489758389c00SMarek Vasut 	.cable_test_get_status	= ksz9x31_cable_test_get_status,
48987ab59dc1SDavid J. Choi }, {
48991623ad8eSDivya Koppera 	.phy_id		= PHY_ID_LAN8814,
49001623ad8eSDivya Koppera 	.phy_id_mask	= MICREL_PHY_ID_MASK,
49011623ad8eSDivya Koppera 	.name		= "Microchip INDY Gigabit Quad PHY",
490221b688daSDivya Koppera 	.flags          = PHY_POLL_CABLE_TEST,
49037467d716SHoratiu Vultur 	.config_init	= lan8814_config_init,
4904a516b7f7SDivya Koppera 	.driver_data	= &lan8814_type,
4905ece19502SDivya Koppera 	.probe		= lan8814_probe,
49061623ad8eSDivya Koppera 	.soft_reset	= genphy_soft_reset,
4907b814403aSHoratiu Vultur 	.read_status	= ksz9031_read_status,
49081623ad8eSDivya Koppera 	.get_sset_count	= kszphy_get_sset_count,
49091623ad8eSDivya Koppera 	.get_strings	= kszphy_get_strings,
49101623ad8eSDivya Koppera 	.get_stats	= kszphy_get_stats,
49111623ad8eSDivya Koppera 	.suspend	= genphy_suspend,
49121623ad8eSDivya Koppera 	.resume		= kszphy_resume,
4913b3ec7248SDivya Koppera 	.config_intr	= lan8814_config_intr,
4914b3ec7248SDivya Koppera 	.handle_interrupt = lan8814_handle_interrupt,
491521b688daSDivya Koppera 	.cable_test_start	= lan8814_cable_test_start,
491621b688daSDivya Koppera 	.cable_test_get_status	= ksz886x_cable_test_get_status,
49171623ad8eSDivya Koppera }, {
49187c2dcfa2SHoratiu Vultur 	.phy_id		= PHY_ID_LAN8804,
49197c2dcfa2SHoratiu Vultur 	.phy_id_mask	= MICREL_PHY_ID_MASK,
49207c2dcfa2SHoratiu Vultur 	.name		= "Microchip LAN966X Gigabit PHY",
49217c2dcfa2SHoratiu Vultur 	.config_init	= lan8804_config_init,
49227c2dcfa2SHoratiu Vultur 	.driver_data	= &ksz9021_type,
49237c2dcfa2SHoratiu Vultur 	.probe		= kszphy_probe,
49247c2dcfa2SHoratiu Vultur 	.soft_reset	= genphy_soft_reset,
49257c2dcfa2SHoratiu Vultur 	.read_status	= ksz9031_read_status,
49267c2dcfa2SHoratiu Vultur 	.get_sset_count	= kszphy_get_sset_count,
49277c2dcfa2SHoratiu Vultur 	.get_strings	= kszphy_get_strings,
49287c2dcfa2SHoratiu Vultur 	.get_stats	= kszphy_get_stats,
49297c2dcfa2SHoratiu Vultur 	.suspend	= genphy_suspend,
49307c2dcfa2SHoratiu Vultur 	.resume		= kszphy_resume,
4931b324c6e5SHoratiu Vultur 	.config_intr	= lan8804_config_intr,
4932b324c6e5SHoratiu Vultur 	.handle_interrupt = lan8804_handle_interrupt,
49337c2dcfa2SHoratiu Vultur }, {
4934a8f1a19dSHoratiu Vultur 	.phy_id		= PHY_ID_LAN8841,
4935a8f1a19dSHoratiu Vultur 	.phy_id_mask	= MICREL_PHY_ID_MASK,
4936a8f1a19dSHoratiu Vultur 	.name		= "Microchip LAN8841 Gigabit PHY",
4937a136391aSHoratiu Vultur 	.flags		= PHY_POLL_CABLE_TEST,
4938a8f1a19dSHoratiu Vultur 	.driver_data	= &lan8841_type,
4939a8f1a19dSHoratiu Vultur 	.config_init	= lan8841_config_init,
4940a8f1a19dSHoratiu Vultur 	.probe		= lan8841_probe,
4941a8f1a19dSHoratiu Vultur 	.soft_reset	= genphy_soft_reset,
4942a8f1a19dSHoratiu Vultur 	.config_intr	= lan8841_config_intr,
4943a8f1a19dSHoratiu Vultur 	.handle_interrupt = lan8841_handle_interrupt,
4944a8f1a19dSHoratiu Vultur 	.get_sset_count = kszphy_get_sset_count,
4945a8f1a19dSHoratiu Vultur 	.get_strings	= kszphy_get_strings,
4946a8f1a19dSHoratiu Vultur 	.get_stats	= kszphy_get_stats,
4947cc755495SHoratiu Vultur 	.suspend	= lan8841_suspend,
4948a8f1a19dSHoratiu Vultur 	.resume		= genphy_resume,
4949a136391aSHoratiu Vultur 	.cable_test_start	= lan8814_cable_test_start,
4950a136391aSHoratiu Vultur 	.cable_test_get_status	= ksz886x_cable_test_get_status,
4951a8f1a19dSHoratiu Vultur }, {
4952bff5b4b3SYuiko Oshino 	.phy_id		= PHY_ID_KSZ9131,
4953bff5b4b3SYuiko Oshino 	.phy_id_mask	= MICREL_PHY_ID_MASK,
4954bff5b4b3SYuiko Oshino 	.name		= "Microchip KSZ9131 Gigabit PHY",
4955dcdecdcfSHeiner Kallweit 	/* PHY_GBIT_FEATURES */
495658389c00SMarek Vasut 	.flags		= PHY_POLL_CABLE_TEST,
4957a8f1a19dSHoratiu Vultur 	.driver_data	= &ksz9131_type,
4958bff5b4b3SYuiko Oshino 	.probe		= kszphy_probe,
49597770a438SClaudiu Beznea 	.soft_reset	= genphy_soft_reset,
4960bff5b4b3SYuiko Oshino 	.config_init	= ksz9131_config_init,
4961bff5b4b3SYuiko Oshino 	.config_intr	= kszphy_config_intr,
4962b64e6a87SRaju Lakkaraju 	.config_aneg	= ksz9131_config_aneg,
4963b64e6a87SRaju Lakkaraju 	.read_status	= ksz9131_read_status,
496459ca4e58SIoana Ciornei 	.handle_interrupt = kszphy_handle_interrupt,
4965bff5b4b3SYuiko Oshino 	.get_sset_count = kszphy_get_sset_count,
4966bff5b4b3SYuiko Oshino 	.get_strings	= kszphy_get_strings,
4967bff5b4b3SYuiko Oshino 	.get_stats	= kszphy_get_stats,
4968f1131b9cSClaudiu Beznea 	.suspend	= kszphy_suspend,
4969bff5b4b3SYuiko Oshino 	.resume		= kszphy_resume,
497058389c00SMarek Vasut 	.cable_test_start	= ksz9x31_cable_test_start,
497158389c00SMarek Vasut 	.cable_test_get_status	= ksz9x31_cable_test_get_status,
4972f2e9d083SOleksij Rempel 	.get_features	= ksz9477_get_features,
4973bff5b4b3SYuiko Oshino }, {
497493272e07SJean-Christophe PLAGNIOL-VILLARD 	.phy_id		= PHY_ID_KSZ8873MLL,
4975f893a99eSFabio Estevam 	.phy_id_mask	= MICREL_PHY_ID_MASK,
497693272e07SJean-Christophe PLAGNIOL-VILLARD 	.name		= "Micrel KSZ8873MLL Switch",
4977dcdecdcfSHeiner Kallweit 	/* PHY_BASIC_FEATURES */
497893272e07SJean-Christophe PLAGNIOL-VILLARD 	.config_init	= kszphy_config_init,
497993272e07SJean-Christophe PLAGNIOL-VILLARD 	.config_aneg	= ksz8873mll_config_aneg,
498093272e07SJean-Christophe PLAGNIOL-VILLARD 	.read_status	= ksz8873mll_read_status,
49811a5465f5SPatrice Vilchez 	.suspend	= genphy_suspend,
49821a5465f5SPatrice Vilchez 	.resume		= genphy_resume,
49837ab59dc1SDavid J. Choi }, {
49847ab59dc1SDavid J. Choi 	.phy_id		= PHY_ID_KSZ886X,
4985f893a99eSFabio Estevam 	.phy_id_mask	= MICREL_PHY_ID_MASK,
4986ab36a3a2SMarek Vasut 	.name		= "Micrel KSZ8851 Ethernet MAC or KSZ886X Switch",
498721b688daSDivya Koppera 	.driver_data	= &ksz886x_type,
4988dcdecdcfSHeiner Kallweit 	/* PHY_BASIC_FEATURES */
498949011e0cSOleksij Rempel 	.flags		= PHY_POLL_CABLE_TEST,
49907ab59dc1SDavid J. Choi 	.config_init	= kszphy_config_init,
499152939393SOleksij Rempel 	.config_aneg	= ksz886x_config_aneg,
499252939393SOleksij Rempel 	.read_status	= ksz886x_read_status,
49931a5465f5SPatrice Vilchez 	.suspend	= genphy_suspend,
49941a5465f5SPatrice Vilchez 	.resume		= genphy_resume,
499549011e0cSOleksij Rempel 	.cable_test_start	= ksz886x_cable_test_start,
499649011e0cSOleksij Rempel 	.cable_test_get_status	= ksz886x_cable_test_get_status,
49979d162ed6SSean Nyekjaer }, {
49981d951ba3SMarek Vasut 	.name		= "Micrel KSZ87XX Switch",
4999dcdecdcfSHeiner Kallweit 	/* PHY_BASIC_FEATURES */
50009d162ed6SSean Nyekjaer 	.config_init	= kszphy_config_init,
50018b95599cSMarek Vasut 	.match_phy_device = ksz8795_match_phy_device,
50029d162ed6SSean Nyekjaer 	.suspend	= genphy_suspend,
50039d162ed6SSean Nyekjaer 	.resume		= genphy_resume,
5004fc3973a1SWoojung Huh }, {
5005fc3973a1SWoojung Huh 	.phy_id		= PHY_ID_KSZ9477,
5006fc3973a1SWoojung Huh 	.phy_id_mask	= MICREL_PHY_ID_MASK,
5007fc3973a1SWoojung Huh 	.name		= "Microchip KSZ9477",
5008dcdecdcfSHeiner Kallweit 	/* PHY_GBIT_FEATURES */
500926dd2974SRobert Hancock 	.config_init	= ksz9477_config_init,
5010db45c76bSArun Ramadoss 	.config_intr	= kszphy_config_intr,
5011db45c76bSArun Ramadoss 	.handle_interrupt = kszphy_handle_interrupt,
5012fc3973a1SWoojung Huh 	.suspend	= genphy_suspend,
501302a25572STristram Ha 	.resume		= ksz9477_resume,
501448fb1994SOleksij Rempel 	.get_features	= ksz9477_get_features,
5015d5bf9071SChristian Hohnstaedt } };
5016d0507009SDavid J. Choi 
501750fd7150SJohan Hovold module_phy_driver(ksphy_driver);
5018d0507009SDavid J. Choi 
5019d0507009SDavid J. Choi MODULE_DESCRIPTION("Micrel PHY driver");
5020d0507009SDavid J. Choi MODULE_AUTHOR("David J. Choi");
5021d0507009SDavid J. Choi MODULE_LICENSE("GPL");
502252a60ed2SDavid S. Miller 
5023cf93c945SUwe Kleine-König static struct mdio_device_id __maybe_unused micrel_tbl[] = {
502448d7d0adSJason Wang 	{ PHY_ID_KSZ9021, 0x000ffffe },
5025f893a99eSFabio Estevam 	{ PHY_ID_KSZ9031, MICREL_PHY_ID_MASK },
5026bff5b4b3SYuiko Oshino 	{ PHY_ID_KSZ9131, MICREL_PHY_ID_MASK },
5027ecd5a323SAlexander Stein 	{ PHY_ID_KSZ8001, 0x00fffffc },
5028f893a99eSFabio Estevam 	{ PHY_ID_KS8737, MICREL_PHY_ID_MASK },
5029212ea99aSMarek Vasut 	{ PHY_ID_KSZ8021, 0x00ffffff },
5030b818d1a7SHector Palacios 	{ PHY_ID_KSZ8031, 0x00ffffff },
5031f893a99eSFabio Estevam 	{ PHY_ID_KSZ8041, MICREL_PHY_ID_MASK },
5032f893a99eSFabio Estevam 	{ PHY_ID_KSZ8051, MICREL_PHY_ID_MASK },
5033f893a99eSFabio Estevam 	{ PHY_ID_KSZ8061, MICREL_PHY_ID_MASK },
5034f893a99eSFabio Estevam 	{ PHY_ID_KSZ8081, MICREL_PHY_ID_MASK },
5035f893a99eSFabio Estevam 	{ PHY_ID_KSZ8873MLL, MICREL_PHY_ID_MASK },
5036f893a99eSFabio Estevam 	{ PHY_ID_KSZ886X, MICREL_PHY_ID_MASK },
503796c8693fSEnguerrand de Ribaucourt 	{ PHY_ID_KSZ9477, MICREL_PHY_ID_MASK },
50381623ad8eSDivya Koppera 	{ PHY_ID_LAN8814, MICREL_PHY_ID_MASK },
50397c2dcfa2SHoratiu Vultur 	{ PHY_ID_LAN8804, MICREL_PHY_ID_MASK },
5040a8f1a19dSHoratiu Vultur 	{ PHY_ID_LAN8841, MICREL_PHY_ID_MASK },
504152a60ed2SDavid S. Miller 	{ }
504252a60ed2SDavid S. Miller };
504352a60ed2SDavid S. Miller 
504452a60ed2SDavid S. Miller MODULE_DEVICE_TABLE(mdio, micrel_tbl);
5045