xref: /openbmc/linux/drivers/net/phy/micrel.c (revision a5e63c7d38d548b8dab6c6205e0b6af76899dbf5)
1a2443fd1SAndrew Lunn // SPDX-License-Identifier: GPL-2.0+
2d0507009SDavid J. Choi /*
3d0507009SDavid J. Choi  * drivers/net/phy/micrel.c
4d0507009SDavid J. Choi  *
5d0507009SDavid J. Choi  * Driver for Micrel PHYs
6d0507009SDavid J. Choi  *
7d0507009SDavid J. Choi  * Author: David J. Choi
8d0507009SDavid J. Choi  *
97ab59dc1SDavid J. Choi  * Copyright (c) 2010-2013 Micrel, Inc.
10ee0dc2fbSJohan Hovold  * Copyright (c) 2014 Johan Hovold <johan@kernel.org>
11d0507009SDavid J. Choi  *
127ab59dc1SDavid J. Choi  * Support : Micrel Phys:
13bff5b4b3SYuiko Oshino  *		Giga phys: ksz9021, ksz9031, ksz9131
147ab59dc1SDavid J. Choi  *		100/10 Phys : ksz8001, ksz8721, ksz8737, ksz8041
157ab59dc1SDavid J. Choi  *			   ksz8021, ksz8031, ksz8051,
167ab59dc1SDavid J. Choi  *			   ksz8081, ksz8091,
177ab59dc1SDavid J. Choi  *			   ksz8061,
187ab59dc1SDavid J. Choi  *		Switch : ksz8873, ksz886x
19fc3973a1SWoojung Huh  *			 ksz9477
20d0507009SDavid J. Choi  */
21d0507009SDavid J. Choi 
22bcf3440cSOleksij Rempel #include <linux/bitfield.h>
2349011e0cSOleksij Rempel #include <linux/ethtool_netlink.h>
24d0507009SDavid J. Choi #include <linux/kernel.h>
25d0507009SDavid J. Choi #include <linux/module.h>
26d0507009SDavid J. Choi #include <linux/phy.h>
27d606ef3fSBaruch Siach #include <linux/micrel_phy.h>
28954c3967SSean Cross #include <linux/of.h>
291fadee0cSSascha Hauer #include <linux/clk.h>
306110dff7SOleksij Rempel #include <linux/delay.h>
31d0507009SDavid J. Choi 
32212ea99aSMarek Vasut /* Operation Mode Strap Override */
33212ea99aSMarek Vasut #define MII_KSZPHY_OMSO				0x16
347a1d8390SAntoine Tenart #define KSZPHY_OMSO_FACTORY_TEST		BIT(15)
3500aee095SJohan Hovold #define KSZPHY_OMSO_B_CAST_OFF			BIT(9)
362b0ba96cSSylvain Rochet #define KSZPHY_OMSO_NAND_TREE_ON		BIT(5)
3700aee095SJohan Hovold #define KSZPHY_OMSO_RMII_OVERRIDE		BIT(1)
3800aee095SJohan Hovold #define KSZPHY_OMSO_MII_OVERRIDE		BIT(0)
39212ea99aSMarek Vasut 
4051f932c4SChoi, David /* general Interrupt control/status reg in vendor specific block. */
4151f932c4SChoi, David #define MII_KSZPHY_INTCS			0x1B
4200aee095SJohan Hovold #define KSZPHY_INTCS_JABBER			BIT(15)
4300aee095SJohan Hovold #define KSZPHY_INTCS_RECEIVE_ERR		BIT(14)
4400aee095SJohan Hovold #define KSZPHY_INTCS_PAGE_RECEIVE		BIT(13)
4500aee095SJohan Hovold #define KSZPHY_INTCS_PARELLEL			BIT(12)
4600aee095SJohan Hovold #define KSZPHY_INTCS_LINK_PARTNER_ACK		BIT(11)
4700aee095SJohan Hovold #define KSZPHY_INTCS_LINK_DOWN			BIT(10)
4800aee095SJohan Hovold #define KSZPHY_INTCS_REMOTE_FAULT		BIT(9)
4900aee095SJohan Hovold #define KSZPHY_INTCS_LINK_UP			BIT(8)
5051f932c4SChoi, David #define KSZPHY_INTCS_ALL			(KSZPHY_INTCS_LINK_UP |\
5151f932c4SChoi, David 						KSZPHY_INTCS_LINK_DOWN)
5259ca4e58SIoana Ciornei #define KSZPHY_INTCS_LINK_DOWN_STATUS		BIT(2)
5359ca4e58SIoana Ciornei #define KSZPHY_INTCS_LINK_UP_STATUS		BIT(0)
5459ca4e58SIoana Ciornei #define KSZPHY_INTCS_STATUS			(KSZPHY_INTCS_LINK_DOWN_STATUS |\
5559ca4e58SIoana Ciornei 						 KSZPHY_INTCS_LINK_UP_STATUS)
5651f932c4SChoi, David 
5749011e0cSOleksij Rempel /* LinkMD Control/Status */
5849011e0cSOleksij Rempel #define KSZ8081_LMD				0x1d
5949011e0cSOleksij Rempel #define KSZ8081_LMD_ENABLE_TEST			BIT(15)
6049011e0cSOleksij Rempel #define KSZ8081_LMD_STAT_NORMAL			0
6149011e0cSOleksij Rempel #define KSZ8081_LMD_STAT_OPEN			1
6249011e0cSOleksij Rempel #define KSZ8081_LMD_STAT_SHORT			2
6349011e0cSOleksij Rempel #define KSZ8081_LMD_STAT_FAIL			3
6449011e0cSOleksij Rempel #define KSZ8081_LMD_STAT_MASK			GENMASK(14, 13)
6549011e0cSOleksij Rempel /* Short cable (<10 meter) has been detected by LinkMD */
6649011e0cSOleksij Rempel #define KSZ8081_LMD_SHORT_INDICATOR		BIT(12)
6749011e0cSOleksij Rempel #define KSZ8081_LMD_DELTA_TIME_MASK		GENMASK(8, 0)
6849011e0cSOleksij Rempel 
695a16778eSJohan Hovold /* PHY Control 1 */
705a16778eSJohan Hovold #define MII_KSZPHY_CTRL_1			0x1e
71f873f112SOleksij Rempel #define KSZ8081_CTRL1_MDIX_STAT			BIT(4)
725a16778eSJohan Hovold 
735a16778eSJohan Hovold /* PHY Control 2 / PHY Control (if no PHY Control 1) */
745a16778eSJohan Hovold #define MII_KSZPHY_CTRL_2			0x1f
755a16778eSJohan Hovold #define MII_KSZPHY_CTRL				MII_KSZPHY_CTRL_2
7651f932c4SChoi, David /* bitmap of PHY register to set interrupt mode */
77f873f112SOleksij Rempel #define KSZ8081_CTRL2_HP_MDIX			BIT(15)
78f873f112SOleksij Rempel #define KSZ8081_CTRL2_MDI_MDI_X_SELECT		BIT(14)
79f873f112SOleksij Rempel #define KSZ8081_CTRL2_DISABLE_AUTO_MDIX		BIT(13)
80f873f112SOleksij Rempel #define KSZ8081_CTRL2_FORCE_LINK		BIT(11)
81f873f112SOleksij Rempel #define KSZ8081_CTRL2_POWER_SAVING		BIT(10)
8200aee095SJohan Hovold #define KSZPHY_CTRL_INT_ACTIVE_HIGH		BIT(9)
8363f44b2bSJohan Hovold #define KSZPHY_RMII_REF_CLK_SEL			BIT(7)
8451f932c4SChoi, David 
85954c3967SSean Cross /* Write/read to/from extended registers */
86954c3967SSean Cross #define MII_KSZPHY_EXTREG			0x0b
87954c3967SSean Cross #define KSZPHY_EXTREG_WRITE			0x8000
88954c3967SSean Cross 
89954c3967SSean Cross #define MII_KSZPHY_EXTREG_WRITE			0x0c
90954c3967SSean Cross #define MII_KSZPHY_EXTREG_READ			0x0d
91954c3967SSean Cross 
92954c3967SSean Cross /* Extended registers */
93954c3967SSean Cross #define MII_KSZPHY_CLK_CONTROL_PAD_SKEW		0x104
94954c3967SSean Cross #define MII_KSZPHY_RX_DATA_PAD_SKEW		0x105
95954c3967SSean Cross #define MII_KSZPHY_TX_DATA_PAD_SKEW		0x106
96954c3967SSean Cross 
97954c3967SSean Cross #define PS_TO_REG				200
98954c3967SSean Cross 
992b2427d0SAndrew Lunn struct kszphy_hw_stat {
1002b2427d0SAndrew Lunn 	const char *string;
1012b2427d0SAndrew Lunn 	u8 reg;
1022b2427d0SAndrew Lunn 	u8 bits;
1032b2427d0SAndrew Lunn };
1042b2427d0SAndrew Lunn 
1052b2427d0SAndrew Lunn static struct kszphy_hw_stat kszphy_hw_stats[] = {
1062b2427d0SAndrew Lunn 	{ "phy_receive_errors", 21, 16},
1072b2427d0SAndrew Lunn 	{ "phy_idle_errors", 10, 8 },
1082b2427d0SAndrew Lunn };
1092b2427d0SAndrew Lunn 
110e6a423a8SJohan Hovold struct kszphy_type {
111e6a423a8SJohan Hovold 	u32 led_mode_reg;
112c6f9575cSJohan Hovold 	u16 interrupt_level_mask;
1130f95903eSJohan Hovold 	bool has_broadcast_disable;
1142b0ba96cSSylvain Rochet 	bool has_nand_tree_disable;
11563f44b2bSJohan Hovold 	bool has_rmii_ref_clk_sel;
116e6a423a8SJohan Hovold };
117e6a423a8SJohan Hovold 
118e6a423a8SJohan Hovold struct kszphy_priv {
119e6a423a8SJohan Hovold 	const struct kszphy_type *type;
120e7a792e9SJohan Hovold 	int led_mode;
12163f44b2bSJohan Hovold 	bool rmii_ref_clk_sel;
12263f44b2bSJohan Hovold 	bool rmii_ref_clk_sel_val;
1232b2427d0SAndrew Lunn 	u64 stats[ARRAY_SIZE(kszphy_hw_stats)];
124e6a423a8SJohan Hovold };
125e6a423a8SJohan Hovold 
126e6a423a8SJohan Hovold static const struct kszphy_type ksz8021_type = {
127e6a423a8SJohan Hovold 	.led_mode_reg		= MII_KSZPHY_CTRL_2,
128d0e1df9cSJohan Hovold 	.has_broadcast_disable	= true,
1292b0ba96cSSylvain Rochet 	.has_nand_tree_disable	= true,
13063f44b2bSJohan Hovold 	.has_rmii_ref_clk_sel	= true,
131e6a423a8SJohan Hovold };
132e6a423a8SJohan Hovold 
133e6a423a8SJohan Hovold static const struct kszphy_type ksz8041_type = {
134e6a423a8SJohan Hovold 	.led_mode_reg		= MII_KSZPHY_CTRL_1,
135e6a423a8SJohan Hovold };
136e6a423a8SJohan Hovold 
137e6a423a8SJohan Hovold static const struct kszphy_type ksz8051_type = {
138e6a423a8SJohan Hovold 	.led_mode_reg		= MII_KSZPHY_CTRL_2,
1392b0ba96cSSylvain Rochet 	.has_nand_tree_disable	= true,
140e6a423a8SJohan Hovold };
141e6a423a8SJohan Hovold 
142e6a423a8SJohan Hovold static const struct kszphy_type ksz8081_type = {
143e6a423a8SJohan Hovold 	.led_mode_reg		= MII_KSZPHY_CTRL_2,
1440f95903eSJohan Hovold 	.has_broadcast_disable	= true,
1452b0ba96cSSylvain Rochet 	.has_nand_tree_disable	= true,
14686dc1342SJohan Hovold 	.has_rmii_ref_clk_sel	= true,
147e6a423a8SJohan Hovold };
148e6a423a8SJohan Hovold 
149c6f9575cSJohan Hovold static const struct kszphy_type ks8737_type = {
150c6f9575cSJohan Hovold 	.interrupt_level_mask	= BIT(14),
151c6f9575cSJohan Hovold };
152c6f9575cSJohan Hovold 
153c6f9575cSJohan Hovold static const struct kszphy_type ksz9021_type = {
154c6f9575cSJohan Hovold 	.interrupt_level_mask	= BIT(14),
155c6f9575cSJohan Hovold };
156c6f9575cSJohan Hovold 
157954c3967SSean Cross static int kszphy_extended_write(struct phy_device *phydev,
158954c3967SSean Cross 				u32 regnum, u16 val)
159954c3967SSean Cross {
160954c3967SSean Cross 	phy_write(phydev, MII_KSZPHY_EXTREG, KSZPHY_EXTREG_WRITE | regnum);
161954c3967SSean Cross 	return phy_write(phydev, MII_KSZPHY_EXTREG_WRITE, val);
162954c3967SSean Cross }
163954c3967SSean Cross 
164954c3967SSean Cross static int kszphy_extended_read(struct phy_device *phydev,
165954c3967SSean Cross 				u32 regnum)
166954c3967SSean Cross {
167954c3967SSean Cross 	phy_write(phydev, MII_KSZPHY_EXTREG, regnum);
168954c3967SSean Cross 	return phy_read(phydev, MII_KSZPHY_EXTREG_READ);
169954c3967SSean Cross }
170954c3967SSean Cross 
17151f932c4SChoi, David static int kszphy_ack_interrupt(struct phy_device *phydev)
17251f932c4SChoi, David {
17351f932c4SChoi, David 	/* bit[7..0] int status, which is a read and clear register. */
17451f932c4SChoi, David 	int rc;
17551f932c4SChoi, David 
17651f932c4SChoi, David 	rc = phy_read(phydev, MII_KSZPHY_INTCS);
17751f932c4SChoi, David 
17851f932c4SChoi, David 	return (rc < 0) ? rc : 0;
17951f932c4SChoi, David }
18051f932c4SChoi, David 
18151f932c4SChoi, David static int kszphy_config_intr(struct phy_device *phydev)
18251f932c4SChoi, David {
183c6f9575cSJohan Hovold 	const struct kszphy_type *type = phydev->drv->driver_data;
184c0c99d0cSIoana Ciornei 	int temp, err;
185c6f9575cSJohan Hovold 	u16 mask;
186c6f9575cSJohan Hovold 
187c6f9575cSJohan Hovold 	if (type && type->interrupt_level_mask)
188c6f9575cSJohan Hovold 		mask = type->interrupt_level_mask;
189c6f9575cSJohan Hovold 	else
190c6f9575cSJohan Hovold 		mask = KSZPHY_CTRL_INT_ACTIVE_HIGH;
19151f932c4SChoi, David 
19251f932c4SChoi, David 	/* set the interrupt pin active low */
19351f932c4SChoi, David 	temp = phy_read(phydev, MII_KSZPHY_CTRL);
1945bb8fc0dSJohan Hovold 	if (temp < 0)
1955bb8fc0dSJohan Hovold 		return temp;
196c6f9575cSJohan Hovold 	temp &= ~mask;
19751f932c4SChoi, David 	phy_write(phydev, MII_KSZPHY_CTRL, temp);
19851f932c4SChoi, David 
199c6f9575cSJohan Hovold 	/* enable / disable interrupts */
200c0c99d0cSIoana Ciornei 	if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
201c0c99d0cSIoana Ciornei 		err = kszphy_ack_interrupt(phydev);
202c0c99d0cSIoana Ciornei 		if (err)
203c0c99d0cSIoana Ciornei 			return err;
20451f932c4SChoi, David 
205c0c99d0cSIoana Ciornei 		temp = KSZPHY_INTCS_ALL;
206c0c99d0cSIoana Ciornei 		err = phy_write(phydev, MII_KSZPHY_INTCS, temp);
207c0c99d0cSIoana Ciornei 	} else {
208c0c99d0cSIoana Ciornei 		temp = 0;
209c0c99d0cSIoana Ciornei 		err = phy_write(phydev, MII_KSZPHY_INTCS, temp);
210c0c99d0cSIoana Ciornei 		if (err)
211c0c99d0cSIoana Ciornei 			return err;
212c0c99d0cSIoana Ciornei 
213c0c99d0cSIoana Ciornei 		err = kszphy_ack_interrupt(phydev);
214c0c99d0cSIoana Ciornei 	}
215c0c99d0cSIoana Ciornei 
216c0c99d0cSIoana Ciornei 	return err;
21751f932c4SChoi, David }
218d0507009SDavid J. Choi 
21959ca4e58SIoana Ciornei static irqreturn_t kszphy_handle_interrupt(struct phy_device *phydev)
22059ca4e58SIoana Ciornei {
22159ca4e58SIoana Ciornei 	int irq_status;
22259ca4e58SIoana Ciornei 
22359ca4e58SIoana Ciornei 	irq_status = phy_read(phydev, MII_KSZPHY_INTCS);
22459ca4e58SIoana Ciornei 	if (irq_status < 0) {
22559ca4e58SIoana Ciornei 		phy_error(phydev);
22659ca4e58SIoana Ciornei 		return IRQ_NONE;
22759ca4e58SIoana Ciornei 	}
22859ca4e58SIoana Ciornei 
229fff4c746SOleksij Rempel 	if (!(irq_status & KSZPHY_INTCS_STATUS))
23059ca4e58SIoana Ciornei 		return IRQ_NONE;
23159ca4e58SIoana Ciornei 
23259ca4e58SIoana Ciornei 	phy_trigger_machine(phydev);
23359ca4e58SIoana Ciornei 
23459ca4e58SIoana Ciornei 	return IRQ_HANDLED;
23559ca4e58SIoana Ciornei }
23659ca4e58SIoana Ciornei 
23763f44b2bSJohan Hovold static int kszphy_rmii_clk_sel(struct phy_device *phydev, bool val)
23863f44b2bSJohan Hovold {
23963f44b2bSJohan Hovold 	int ctrl;
24063f44b2bSJohan Hovold 
24163f44b2bSJohan Hovold 	ctrl = phy_read(phydev, MII_KSZPHY_CTRL);
24263f44b2bSJohan Hovold 	if (ctrl < 0)
24363f44b2bSJohan Hovold 		return ctrl;
24463f44b2bSJohan Hovold 
24563f44b2bSJohan Hovold 	if (val)
24663f44b2bSJohan Hovold 		ctrl |= KSZPHY_RMII_REF_CLK_SEL;
24763f44b2bSJohan Hovold 	else
24863f44b2bSJohan Hovold 		ctrl &= ~KSZPHY_RMII_REF_CLK_SEL;
24963f44b2bSJohan Hovold 
25063f44b2bSJohan Hovold 	return phy_write(phydev, MII_KSZPHY_CTRL, ctrl);
25163f44b2bSJohan Hovold }
25263f44b2bSJohan Hovold 
253e7a792e9SJohan Hovold static int kszphy_setup_led(struct phy_device *phydev, u32 reg, int val)
25420d8435aSBen Dooks {
2555a16778eSJohan Hovold 	int rc, temp, shift;
2568620546cSJohan Hovold 
2575a16778eSJohan Hovold 	switch (reg) {
2585a16778eSJohan Hovold 	case MII_KSZPHY_CTRL_1:
2595a16778eSJohan Hovold 		shift = 14;
2605a16778eSJohan Hovold 		break;
2615a16778eSJohan Hovold 	case MII_KSZPHY_CTRL_2:
2625a16778eSJohan Hovold 		shift = 4;
2635a16778eSJohan Hovold 		break;
2645a16778eSJohan Hovold 	default:
2655a16778eSJohan Hovold 		return -EINVAL;
2665a16778eSJohan Hovold 	}
2675a16778eSJohan Hovold 
26820d8435aSBen Dooks 	temp = phy_read(phydev, reg);
269b7035860SJohan Hovold 	if (temp < 0) {
270b7035860SJohan Hovold 		rc = temp;
271b7035860SJohan Hovold 		goto out;
272b7035860SJohan Hovold 	}
27320d8435aSBen Dooks 
27428bdc499SSergei Shtylyov 	temp &= ~(3 << shift);
27520d8435aSBen Dooks 	temp |= val << shift;
27620d8435aSBen Dooks 	rc = phy_write(phydev, reg, temp);
277b7035860SJohan Hovold out:
278b7035860SJohan Hovold 	if (rc < 0)
27972ba48beSAndrew Lunn 		phydev_err(phydev, "failed to set led mode\n");
28020d8435aSBen Dooks 
281b7035860SJohan Hovold 	return rc;
28220d8435aSBen Dooks }
28320d8435aSBen Dooks 
284bde15129SJohan Hovold /* Disable PHY address 0 as the broadcast address, so that it can be used as a
285bde15129SJohan Hovold  * unique (non-broadcast) address on a shared bus.
286bde15129SJohan Hovold  */
287bde15129SJohan Hovold static int kszphy_broadcast_disable(struct phy_device *phydev)
288bde15129SJohan Hovold {
289bde15129SJohan Hovold 	int ret;
290bde15129SJohan Hovold 
291bde15129SJohan Hovold 	ret = phy_read(phydev, MII_KSZPHY_OMSO);
292bde15129SJohan Hovold 	if (ret < 0)
293bde15129SJohan Hovold 		goto out;
294bde15129SJohan Hovold 
295bde15129SJohan Hovold 	ret = phy_write(phydev, MII_KSZPHY_OMSO, ret | KSZPHY_OMSO_B_CAST_OFF);
296bde15129SJohan Hovold out:
297bde15129SJohan Hovold 	if (ret)
29872ba48beSAndrew Lunn 		phydev_err(phydev, "failed to disable broadcast address\n");
299bde15129SJohan Hovold 
300bde15129SJohan Hovold 	return ret;
301bde15129SJohan Hovold }
302bde15129SJohan Hovold 
3032b0ba96cSSylvain Rochet static int kszphy_nand_tree_disable(struct phy_device *phydev)
3042b0ba96cSSylvain Rochet {
3052b0ba96cSSylvain Rochet 	int ret;
3062b0ba96cSSylvain Rochet 
3072b0ba96cSSylvain Rochet 	ret = phy_read(phydev, MII_KSZPHY_OMSO);
3082b0ba96cSSylvain Rochet 	if (ret < 0)
3092b0ba96cSSylvain Rochet 		goto out;
3102b0ba96cSSylvain Rochet 
3112b0ba96cSSylvain Rochet 	if (!(ret & KSZPHY_OMSO_NAND_TREE_ON))
3122b0ba96cSSylvain Rochet 		return 0;
3132b0ba96cSSylvain Rochet 
3142b0ba96cSSylvain Rochet 	ret = phy_write(phydev, MII_KSZPHY_OMSO,
3152b0ba96cSSylvain Rochet 			ret & ~KSZPHY_OMSO_NAND_TREE_ON);
3162b0ba96cSSylvain Rochet out:
3172b0ba96cSSylvain Rochet 	if (ret)
31872ba48beSAndrew Lunn 		phydev_err(phydev, "failed to disable NAND tree mode\n");
3192b0ba96cSSylvain Rochet 
3202b0ba96cSSylvain Rochet 	return ret;
3212b0ba96cSSylvain Rochet }
3222b0ba96cSSylvain Rochet 
32379e498a9SLeonard Crestez /* Some config bits need to be set again on resume, handle them here. */
32479e498a9SLeonard Crestez static int kszphy_config_reset(struct phy_device *phydev)
32579e498a9SLeonard Crestez {
32679e498a9SLeonard Crestez 	struct kszphy_priv *priv = phydev->priv;
32779e498a9SLeonard Crestez 	int ret;
32879e498a9SLeonard Crestez 
32979e498a9SLeonard Crestez 	if (priv->rmii_ref_clk_sel) {
33079e498a9SLeonard Crestez 		ret = kszphy_rmii_clk_sel(phydev, priv->rmii_ref_clk_sel_val);
33179e498a9SLeonard Crestez 		if (ret) {
33279e498a9SLeonard Crestez 			phydev_err(phydev,
33379e498a9SLeonard Crestez 				   "failed to set rmii reference clock\n");
33479e498a9SLeonard Crestez 			return ret;
33579e498a9SLeonard Crestez 		}
33679e498a9SLeonard Crestez 	}
33779e498a9SLeonard Crestez 
33879e498a9SLeonard Crestez 	if (priv->led_mode >= 0)
33979e498a9SLeonard Crestez 		kszphy_setup_led(phydev, priv->type->led_mode_reg, priv->led_mode);
34079e498a9SLeonard Crestez 
34179e498a9SLeonard Crestez 	return 0;
34279e498a9SLeonard Crestez }
34379e498a9SLeonard Crestez 
344d0507009SDavid J. Choi static int kszphy_config_init(struct phy_device *phydev)
345d0507009SDavid J. Choi {
346e6a423a8SJohan Hovold 	struct kszphy_priv *priv = phydev->priv;
347e6a423a8SJohan Hovold 	const struct kszphy_type *type;
348d0507009SDavid J. Choi 
349e6a423a8SJohan Hovold 	if (!priv)
350e6a423a8SJohan Hovold 		return 0;
351e6a423a8SJohan Hovold 
352e6a423a8SJohan Hovold 	type = priv->type;
353e6a423a8SJohan Hovold 
3540f95903eSJohan Hovold 	if (type->has_broadcast_disable)
3550f95903eSJohan Hovold 		kszphy_broadcast_disable(phydev);
3560f95903eSJohan Hovold 
3572b0ba96cSSylvain Rochet 	if (type->has_nand_tree_disable)
3582b0ba96cSSylvain Rochet 		kszphy_nand_tree_disable(phydev);
3592b0ba96cSSylvain Rochet 
36079e498a9SLeonard Crestez 	return kszphy_config_reset(phydev);
36120d8435aSBen Dooks }
36220d8435aSBen Dooks 
3634217a64eSMichael Walle static int ksz8041_fiber_mode(struct phy_device *phydev)
3644217a64eSMichael Walle {
3654217a64eSMichael Walle 	struct device_node *of_node = phydev->mdio.dev.of_node;
3664217a64eSMichael Walle 
3674217a64eSMichael Walle 	return of_property_read_bool(of_node, "micrel,fiber-mode");
3684217a64eSMichael Walle }
3694217a64eSMichael Walle 
37077501a79SPhilipp Zabel static int ksz8041_config_init(struct phy_device *phydev)
37177501a79SPhilipp Zabel {
3723c1bcc86SAndrew Lunn 	__ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
3733c1bcc86SAndrew Lunn 
37477501a79SPhilipp Zabel 	/* Limit supported and advertised modes in fiber mode */
3754217a64eSMichael Walle 	if (ksz8041_fiber_mode(phydev)) {
37677501a79SPhilipp Zabel 		phydev->dev_flags |= MICREL_PHY_FXEN;
3773c1bcc86SAndrew Lunn 		linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, mask);
3783c1bcc86SAndrew Lunn 		linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, mask);
3793c1bcc86SAndrew Lunn 
3803c1bcc86SAndrew Lunn 		linkmode_and(phydev->supported, phydev->supported, mask);
3813c1bcc86SAndrew Lunn 		linkmode_set_bit(ETHTOOL_LINK_MODE_FIBRE_BIT,
3823c1bcc86SAndrew Lunn 				 phydev->supported);
3833c1bcc86SAndrew Lunn 		linkmode_and(phydev->advertising, phydev->advertising, mask);
3843c1bcc86SAndrew Lunn 		linkmode_set_bit(ETHTOOL_LINK_MODE_FIBRE_BIT,
3853c1bcc86SAndrew Lunn 				 phydev->advertising);
38677501a79SPhilipp Zabel 		phydev->autoneg = AUTONEG_DISABLE;
38777501a79SPhilipp Zabel 	}
38877501a79SPhilipp Zabel 
38977501a79SPhilipp Zabel 	return kszphy_config_init(phydev);
39077501a79SPhilipp Zabel }
39177501a79SPhilipp Zabel 
39277501a79SPhilipp Zabel static int ksz8041_config_aneg(struct phy_device *phydev)
39377501a79SPhilipp Zabel {
39477501a79SPhilipp Zabel 	/* Skip auto-negotiation in fiber mode */
39577501a79SPhilipp Zabel 	if (phydev->dev_flags & MICREL_PHY_FXEN) {
39677501a79SPhilipp Zabel 		phydev->speed = SPEED_100;
39777501a79SPhilipp Zabel 		return 0;
39877501a79SPhilipp Zabel 	}
39977501a79SPhilipp Zabel 
40077501a79SPhilipp Zabel 	return genphy_config_aneg(phydev);
40177501a79SPhilipp Zabel }
40277501a79SPhilipp Zabel 
4038b95599cSMarek Vasut static int ksz8051_ksz8795_match_phy_device(struct phy_device *phydev,
404*a5e63c7dSSteve Bennett 					    const bool ksz_8051)
4058b95599cSMarek Vasut {
4068b95599cSMarek Vasut 	int ret;
4078b95599cSMarek Vasut 
408*a5e63c7dSSteve Bennett 	if ((phydev->phy_id & MICREL_PHY_ID_MASK) != PHY_ID_KSZ8051)
4098b95599cSMarek Vasut 		return 0;
4108b95599cSMarek Vasut 
4118b95599cSMarek Vasut 	ret = phy_read(phydev, MII_BMSR);
4128b95599cSMarek Vasut 	if (ret < 0)
4138b95599cSMarek Vasut 		return ret;
4148b95599cSMarek Vasut 
4158b95599cSMarek Vasut 	/* KSZ8051 PHY and KSZ8794/KSZ8795/KSZ8765 switch share the same
4168b95599cSMarek Vasut 	 * exact PHY ID. However, they can be told apart by the extended
4178b95599cSMarek Vasut 	 * capability registers presence. The KSZ8051 PHY has them while
4188b95599cSMarek Vasut 	 * the switch does not.
4198b95599cSMarek Vasut 	 */
4208b95599cSMarek Vasut 	ret &= BMSR_ERCAP;
421*a5e63c7dSSteve Bennett 	if (ksz_8051)
4228b95599cSMarek Vasut 		return ret;
4238b95599cSMarek Vasut 	else
4248b95599cSMarek Vasut 		return !ret;
4258b95599cSMarek Vasut }
4268b95599cSMarek Vasut 
4278b95599cSMarek Vasut static int ksz8051_match_phy_device(struct phy_device *phydev)
4288b95599cSMarek Vasut {
429*a5e63c7dSSteve Bennett 	return ksz8051_ksz8795_match_phy_device(phydev, true);
4308b95599cSMarek Vasut }
4318b95599cSMarek Vasut 
4327a1d8390SAntoine Tenart static int ksz8081_config_init(struct phy_device *phydev)
4337a1d8390SAntoine Tenart {
4347a1d8390SAntoine Tenart 	/* KSZPHY_OMSO_FACTORY_TEST is set at de-assertion of the reset line
4357a1d8390SAntoine Tenart 	 * based on the RXER (KSZ8081RNA/RND) or TXC (KSZ8081MNX/RNB) pin. If a
4367a1d8390SAntoine Tenart 	 * pull-down is missing, the factory test mode should be cleared by
4377a1d8390SAntoine Tenart 	 * manually writing a 0.
4387a1d8390SAntoine Tenart 	 */
4397a1d8390SAntoine Tenart 	phy_clear_bits(phydev, MII_KSZPHY_OMSO, KSZPHY_OMSO_FACTORY_TEST);
4407a1d8390SAntoine Tenart 
4417a1d8390SAntoine Tenart 	return kszphy_config_init(phydev);
4427a1d8390SAntoine Tenart }
4437a1d8390SAntoine Tenart 
444f873f112SOleksij Rempel static int ksz8081_config_mdix(struct phy_device *phydev, u8 ctrl)
445f873f112SOleksij Rempel {
446f873f112SOleksij Rempel 	u16 val;
447f873f112SOleksij Rempel 
448f873f112SOleksij Rempel 	switch (ctrl) {
449f873f112SOleksij Rempel 	case ETH_TP_MDI:
450f873f112SOleksij Rempel 		val = KSZ8081_CTRL2_DISABLE_AUTO_MDIX;
451f873f112SOleksij Rempel 		break;
452f873f112SOleksij Rempel 	case ETH_TP_MDI_X:
453f873f112SOleksij Rempel 		val = KSZ8081_CTRL2_DISABLE_AUTO_MDIX |
454f873f112SOleksij Rempel 			KSZ8081_CTRL2_MDI_MDI_X_SELECT;
455f873f112SOleksij Rempel 		break;
456f873f112SOleksij Rempel 	case ETH_TP_MDI_AUTO:
457f873f112SOleksij Rempel 		val = 0;
458f873f112SOleksij Rempel 		break;
459f873f112SOleksij Rempel 	default:
460f873f112SOleksij Rempel 		return 0;
461f873f112SOleksij Rempel 	}
462f873f112SOleksij Rempel 
463f873f112SOleksij Rempel 	return phy_modify(phydev, MII_KSZPHY_CTRL_2,
464f873f112SOleksij Rempel 			  KSZ8081_CTRL2_HP_MDIX |
465f873f112SOleksij Rempel 			  KSZ8081_CTRL2_MDI_MDI_X_SELECT |
466f873f112SOleksij Rempel 			  KSZ8081_CTRL2_DISABLE_AUTO_MDIX,
467f873f112SOleksij Rempel 			  KSZ8081_CTRL2_HP_MDIX | val);
468f873f112SOleksij Rempel }
469f873f112SOleksij Rempel 
470f873f112SOleksij Rempel static int ksz8081_config_aneg(struct phy_device *phydev)
471f873f112SOleksij Rempel {
472f873f112SOleksij Rempel 	int ret;
473f873f112SOleksij Rempel 
474f873f112SOleksij Rempel 	ret = genphy_config_aneg(phydev);
475f873f112SOleksij Rempel 	if (ret)
476f873f112SOleksij Rempel 		return ret;
477f873f112SOleksij Rempel 
478f873f112SOleksij Rempel 	/* The MDI-X configuration is automatically changed by the PHY after
479f873f112SOleksij Rempel 	 * switching from autoneg off to on. So, take MDI-X configuration under
480f873f112SOleksij Rempel 	 * own control and set it after autoneg configuration was done.
481f873f112SOleksij Rempel 	 */
482f873f112SOleksij Rempel 	return ksz8081_config_mdix(phydev, phydev->mdix_ctrl);
483f873f112SOleksij Rempel }
484f873f112SOleksij Rempel 
485f873f112SOleksij Rempel static int ksz8081_mdix_update(struct phy_device *phydev)
486f873f112SOleksij Rempel {
487f873f112SOleksij Rempel 	int ret;
488f873f112SOleksij Rempel 
489f873f112SOleksij Rempel 	ret = phy_read(phydev, MII_KSZPHY_CTRL_2);
490f873f112SOleksij Rempel 	if (ret < 0)
491f873f112SOleksij Rempel 		return ret;
492f873f112SOleksij Rempel 
493f873f112SOleksij Rempel 	if (ret & KSZ8081_CTRL2_DISABLE_AUTO_MDIX) {
494f873f112SOleksij Rempel 		if (ret & KSZ8081_CTRL2_MDI_MDI_X_SELECT)
495f873f112SOleksij Rempel 			phydev->mdix_ctrl = ETH_TP_MDI_X;
496f873f112SOleksij Rempel 		else
497f873f112SOleksij Rempel 			phydev->mdix_ctrl = ETH_TP_MDI;
498f873f112SOleksij Rempel 	} else {
499f873f112SOleksij Rempel 		phydev->mdix_ctrl = ETH_TP_MDI_AUTO;
500f873f112SOleksij Rempel 	}
501f873f112SOleksij Rempel 
502f873f112SOleksij Rempel 	ret = phy_read(phydev, MII_KSZPHY_CTRL_1);
503f873f112SOleksij Rempel 	if (ret < 0)
504f873f112SOleksij Rempel 		return ret;
505f873f112SOleksij Rempel 
506f873f112SOleksij Rempel 	if (ret & KSZ8081_CTRL1_MDIX_STAT)
507f873f112SOleksij Rempel 		phydev->mdix = ETH_TP_MDI;
508f873f112SOleksij Rempel 	else
509f873f112SOleksij Rempel 		phydev->mdix = ETH_TP_MDI_X;
510f873f112SOleksij Rempel 
511f873f112SOleksij Rempel 	return 0;
512f873f112SOleksij Rempel }
513f873f112SOleksij Rempel 
514f873f112SOleksij Rempel static int ksz8081_read_status(struct phy_device *phydev)
515f873f112SOleksij Rempel {
516f873f112SOleksij Rempel 	int ret;
517f873f112SOleksij Rempel 
518f873f112SOleksij Rempel 	ret = ksz8081_mdix_update(phydev);
519f873f112SOleksij Rempel 	if (ret < 0)
520f873f112SOleksij Rempel 		return ret;
521f873f112SOleksij Rempel 
522f873f112SOleksij Rempel 	return genphy_read_status(phydev);
523f873f112SOleksij Rempel }
524f873f112SOleksij Rempel 
525232ba3a5SRajasingh Thavamani static int ksz8061_config_init(struct phy_device *phydev)
526232ba3a5SRajasingh Thavamani {
527232ba3a5SRajasingh Thavamani 	int ret;
528232ba3a5SRajasingh Thavamani 
529232ba3a5SRajasingh Thavamani 	ret = phy_write_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_DEVID1, 0xB61A);
530232ba3a5SRajasingh Thavamani 	if (ret)
531232ba3a5SRajasingh Thavamani 		return ret;
532232ba3a5SRajasingh Thavamani 
533232ba3a5SRajasingh Thavamani 	return kszphy_config_init(phydev);
534232ba3a5SRajasingh Thavamani }
535232ba3a5SRajasingh Thavamani 
5368b95599cSMarek Vasut static int ksz8795_match_phy_device(struct phy_device *phydev)
5378b95599cSMarek Vasut {
538*a5e63c7dSSteve Bennett 	return ksz8051_ksz8795_match_phy_device(phydev, false);
5398b95599cSMarek Vasut }
5408b95599cSMarek Vasut 
541954c3967SSean Cross static int ksz9021_load_values_from_of(struct phy_device *phydev,
5423c9a9f7fSJaeden Amero 				       const struct device_node *of_node,
5433c9a9f7fSJaeden Amero 				       u16 reg,
5443c9a9f7fSJaeden Amero 				       const char *field1, const char *field2,
5453c9a9f7fSJaeden Amero 				       const char *field3, const char *field4)
546954c3967SSean Cross {
547954c3967SSean Cross 	int val1 = -1;
548954c3967SSean Cross 	int val2 = -2;
549954c3967SSean Cross 	int val3 = -3;
550954c3967SSean Cross 	int val4 = -4;
551954c3967SSean Cross 	int newval;
552954c3967SSean Cross 	int matches = 0;
553954c3967SSean Cross 
554954c3967SSean Cross 	if (!of_property_read_u32(of_node, field1, &val1))
555954c3967SSean Cross 		matches++;
556954c3967SSean Cross 
557954c3967SSean Cross 	if (!of_property_read_u32(of_node, field2, &val2))
558954c3967SSean Cross 		matches++;
559954c3967SSean Cross 
560954c3967SSean Cross 	if (!of_property_read_u32(of_node, field3, &val3))
561954c3967SSean Cross 		matches++;
562954c3967SSean Cross 
563954c3967SSean Cross 	if (!of_property_read_u32(of_node, field4, &val4))
564954c3967SSean Cross 		matches++;
565954c3967SSean Cross 
566954c3967SSean Cross 	if (!matches)
567954c3967SSean Cross 		return 0;
568954c3967SSean Cross 
569954c3967SSean Cross 	if (matches < 4)
570954c3967SSean Cross 		newval = kszphy_extended_read(phydev, reg);
571954c3967SSean Cross 	else
572954c3967SSean Cross 		newval = 0;
573954c3967SSean Cross 
574954c3967SSean Cross 	if (val1 != -1)
575954c3967SSean Cross 		newval = ((newval & 0xfff0) | ((val1 / PS_TO_REG) & 0xf) << 0);
576954c3967SSean Cross 
5776a119745SHubert Chaumette 	if (val2 != -2)
578954c3967SSean Cross 		newval = ((newval & 0xff0f) | ((val2 / PS_TO_REG) & 0xf) << 4);
579954c3967SSean Cross 
5806a119745SHubert Chaumette 	if (val3 != -3)
581954c3967SSean Cross 		newval = ((newval & 0xf0ff) | ((val3 / PS_TO_REG) & 0xf) << 8);
582954c3967SSean Cross 
5836a119745SHubert Chaumette 	if (val4 != -4)
584954c3967SSean Cross 		newval = ((newval & 0x0fff) | ((val4 / PS_TO_REG) & 0xf) << 12);
585954c3967SSean Cross 
586954c3967SSean Cross 	return kszphy_extended_write(phydev, reg, newval);
587954c3967SSean Cross }
588954c3967SSean Cross 
589954c3967SSean Cross static int ksz9021_config_init(struct phy_device *phydev)
590954c3967SSean Cross {
591ce4f8afdSColin Ian King 	const struct device_node *of_node;
592651df218SAndrew Lunn 	const struct device *dev_walker;
593954c3967SSean Cross 
594651df218SAndrew Lunn 	/* The Micrel driver has a deprecated option to place phy OF
595651df218SAndrew Lunn 	 * properties in the MAC node. Walk up the tree of devices to
596651df218SAndrew Lunn 	 * find a device with an OF node.
597651df218SAndrew Lunn 	 */
598e5a03bfdSAndrew Lunn 	dev_walker = &phydev->mdio.dev;
599651df218SAndrew Lunn 	do {
600651df218SAndrew Lunn 		of_node = dev_walker->of_node;
601651df218SAndrew Lunn 		dev_walker = dev_walker->parent;
602651df218SAndrew Lunn 
603651df218SAndrew Lunn 	} while (!of_node && dev_walker);
604954c3967SSean Cross 
605954c3967SSean Cross 	if (of_node) {
606954c3967SSean Cross 		ksz9021_load_values_from_of(phydev, of_node,
607954c3967SSean Cross 				    MII_KSZPHY_CLK_CONTROL_PAD_SKEW,
608954c3967SSean Cross 				    "txen-skew-ps", "txc-skew-ps",
609954c3967SSean Cross 				    "rxdv-skew-ps", "rxc-skew-ps");
610954c3967SSean Cross 		ksz9021_load_values_from_of(phydev, of_node,
611954c3967SSean Cross 				    MII_KSZPHY_RX_DATA_PAD_SKEW,
612954c3967SSean Cross 				    "rxd0-skew-ps", "rxd1-skew-ps",
613954c3967SSean Cross 				    "rxd2-skew-ps", "rxd3-skew-ps");
614954c3967SSean Cross 		ksz9021_load_values_from_of(phydev, of_node,
615954c3967SSean Cross 				    MII_KSZPHY_TX_DATA_PAD_SKEW,
616954c3967SSean Cross 				    "txd0-skew-ps", "txd1-skew-ps",
617954c3967SSean Cross 				    "txd2-skew-ps", "txd3-skew-ps");
618954c3967SSean Cross 	}
619954c3967SSean Cross 	return 0;
620954c3967SSean Cross }
621954c3967SSean Cross 
6226e4b8273SHubert Chaumette #define KSZ9031_PS_TO_REG		60
6236e4b8273SHubert Chaumette 
6246e4b8273SHubert Chaumette /* Extended registers */
6256270e1aeSJaeden Amero /* MMD Address 0x0 */
6266270e1aeSJaeden Amero #define MII_KSZ9031RN_FLP_BURST_TX_LO	3
6276270e1aeSJaeden Amero #define MII_KSZ9031RN_FLP_BURST_TX_HI	4
6286270e1aeSJaeden Amero 
629ae6c97bbSJaeden Amero /* MMD Address 0x2 */
6306e4b8273SHubert Chaumette #define MII_KSZ9031RN_CONTROL_PAD_SKEW	4
631bcf3440cSOleksij Rempel #define MII_KSZ9031RN_RX_CTL_M		GENMASK(7, 4)
632bcf3440cSOleksij Rempel #define MII_KSZ9031RN_TX_CTL_M		GENMASK(3, 0)
633bcf3440cSOleksij Rempel 
6346e4b8273SHubert Chaumette #define MII_KSZ9031RN_RX_DATA_PAD_SKEW	5
635bcf3440cSOleksij Rempel #define MII_KSZ9031RN_RXD3		GENMASK(15, 12)
636bcf3440cSOleksij Rempel #define MII_KSZ9031RN_RXD2		GENMASK(11, 8)
637bcf3440cSOleksij Rempel #define MII_KSZ9031RN_RXD1		GENMASK(7, 4)
638bcf3440cSOleksij Rempel #define MII_KSZ9031RN_RXD0		GENMASK(3, 0)
639bcf3440cSOleksij Rempel 
6406e4b8273SHubert Chaumette #define MII_KSZ9031RN_TX_DATA_PAD_SKEW	6
641bcf3440cSOleksij Rempel #define MII_KSZ9031RN_TXD3		GENMASK(15, 12)
642bcf3440cSOleksij Rempel #define MII_KSZ9031RN_TXD2		GENMASK(11, 8)
643bcf3440cSOleksij Rempel #define MII_KSZ9031RN_TXD1		GENMASK(7, 4)
644bcf3440cSOleksij Rempel #define MII_KSZ9031RN_TXD0		GENMASK(3, 0)
645bcf3440cSOleksij Rempel 
6466e4b8273SHubert Chaumette #define MII_KSZ9031RN_CLK_PAD_SKEW	8
647bcf3440cSOleksij Rempel #define MII_KSZ9031RN_GTX_CLK		GENMASK(9, 5)
648bcf3440cSOleksij Rempel #define MII_KSZ9031RN_RX_CLK		GENMASK(4, 0)
649bcf3440cSOleksij Rempel 
650bcf3440cSOleksij Rempel /* KSZ9031 has internal RGMII_IDRX = 1.2ns and RGMII_IDTX = 0ns. To
651bcf3440cSOleksij Rempel  * provide different RGMII options we need to configure delay offset
652bcf3440cSOleksij Rempel  * for each pad relative to build in delay.
653bcf3440cSOleksij Rempel  */
654bcf3440cSOleksij Rempel /* keep rx as "No delay adjustment" and set rx_clk to +0.60ns to get delays of
655bcf3440cSOleksij Rempel  * 1.80ns
656bcf3440cSOleksij Rempel  */
657bcf3440cSOleksij Rempel #define RX_ID				0x7
658bcf3440cSOleksij Rempel #define RX_CLK_ID			0x19
659bcf3440cSOleksij Rempel 
660bcf3440cSOleksij Rempel /* set rx to +0.30ns and rx_clk to -0.90ns to compensate the
661bcf3440cSOleksij Rempel  * internal 1.2ns delay.
662bcf3440cSOleksij Rempel  */
663bcf3440cSOleksij Rempel #define RX_ND				0xc
664bcf3440cSOleksij Rempel #define RX_CLK_ND			0x0
665bcf3440cSOleksij Rempel 
666bcf3440cSOleksij Rempel /* set tx to -0.42ns and tx_clk to +0.96ns to get 1.38ns delay */
667bcf3440cSOleksij Rempel #define TX_ID				0x0
668bcf3440cSOleksij Rempel #define TX_CLK_ID			0x1f
669bcf3440cSOleksij Rempel 
670bcf3440cSOleksij Rempel /* set tx and tx_clk to "No delay adjustment" to keep 0ns
671bcf3440cSOleksij Rempel  * dealy
672bcf3440cSOleksij Rempel  */
673bcf3440cSOleksij Rempel #define TX_ND				0x7
674bcf3440cSOleksij Rempel #define TX_CLK_ND			0xf
6756e4b8273SHubert Chaumette 
676af70c1f9SMike Looijmans /* MMD Address 0x1C */
677af70c1f9SMike Looijmans #define MII_KSZ9031RN_EDPD		0x23
678af70c1f9SMike Looijmans #define MII_KSZ9031RN_EDPD_ENABLE	BIT(0)
679af70c1f9SMike Looijmans 
6806e4b8273SHubert Chaumette static int ksz9031_of_load_skew_values(struct phy_device *phydev,
6813c9a9f7fSJaeden Amero 				       const struct device_node *of_node,
6826e4b8273SHubert Chaumette 				       u16 reg, size_t field_sz,
683bcf3440cSOleksij Rempel 				       const char *field[], u8 numfields,
684bcf3440cSOleksij Rempel 				       bool *update)
6856e4b8273SHubert Chaumette {
6866e4b8273SHubert Chaumette 	int val[4] = {-1, -2, -3, -4};
6876e4b8273SHubert Chaumette 	int matches = 0;
6886e4b8273SHubert Chaumette 	u16 mask;
6896e4b8273SHubert Chaumette 	u16 maxval;
6906e4b8273SHubert Chaumette 	u16 newval;
6916e4b8273SHubert Chaumette 	int i;
6926e4b8273SHubert Chaumette 
6936e4b8273SHubert Chaumette 	for (i = 0; i < numfields; i++)
6946e4b8273SHubert Chaumette 		if (!of_property_read_u32(of_node, field[i], val + i))
6956e4b8273SHubert Chaumette 			matches++;
6966e4b8273SHubert Chaumette 
6976e4b8273SHubert Chaumette 	if (!matches)
6986e4b8273SHubert Chaumette 		return 0;
6996e4b8273SHubert Chaumette 
700bcf3440cSOleksij Rempel 	*update |= true;
701bcf3440cSOleksij Rempel 
7026e4b8273SHubert Chaumette 	if (matches < numfields)
7039b420effSHeiner Kallweit 		newval = phy_read_mmd(phydev, 2, reg);
7046e4b8273SHubert Chaumette 	else
7056e4b8273SHubert Chaumette 		newval = 0;
7066e4b8273SHubert Chaumette 
7076e4b8273SHubert Chaumette 	maxval = (field_sz == 4) ? 0xf : 0x1f;
7086e4b8273SHubert Chaumette 	for (i = 0; i < numfields; i++)
7096e4b8273SHubert Chaumette 		if (val[i] != -(i + 1)) {
7106e4b8273SHubert Chaumette 			mask = 0xffff;
7116e4b8273SHubert Chaumette 			mask ^= maxval << (field_sz * i);
7126e4b8273SHubert Chaumette 			newval = (newval & mask) |
7136e4b8273SHubert Chaumette 				(((val[i] / KSZ9031_PS_TO_REG) & maxval)
7146e4b8273SHubert Chaumette 					<< (field_sz * i));
7156e4b8273SHubert Chaumette 		}
7166e4b8273SHubert Chaumette 
7179b420effSHeiner Kallweit 	return phy_write_mmd(phydev, 2, reg, newval);
7186e4b8273SHubert Chaumette }
7196e4b8273SHubert Chaumette 
720a0da456bSMax Uvarov /* Center KSZ9031RNX FLP timing at 16ms. */
7216270e1aeSJaeden Amero static int ksz9031_center_flp_timing(struct phy_device *phydev)
7226270e1aeSJaeden Amero {
7236270e1aeSJaeden Amero 	int result;
7246270e1aeSJaeden Amero 
7259b420effSHeiner Kallweit 	result = phy_write_mmd(phydev, 0, MII_KSZ9031RN_FLP_BURST_TX_HI,
7269b420effSHeiner Kallweit 			       0x0006);
727a0da456bSMax Uvarov 	if (result)
728a0da456bSMax Uvarov 		return result;
729a0da456bSMax Uvarov 
7309b420effSHeiner Kallweit 	result = phy_write_mmd(phydev, 0, MII_KSZ9031RN_FLP_BURST_TX_LO,
7319b420effSHeiner Kallweit 			       0x1A80);
7326270e1aeSJaeden Amero 	if (result)
7336270e1aeSJaeden Amero 		return result;
7346270e1aeSJaeden Amero 
7356270e1aeSJaeden Amero 	return genphy_restart_aneg(phydev);
7366270e1aeSJaeden Amero }
7376270e1aeSJaeden Amero 
738af70c1f9SMike Looijmans /* Enable energy-detect power-down mode */
739af70c1f9SMike Looijmans static int ksz9031_enable_edpd(struct phy_device *phydev)
740af70c1f9SMike Looijmans {
741af70c1f9SMike Looijmans 	int reg;
742af70c1f9SMike Looijmans 
7439b420effSHeiner Kallweit 	reg = phy_read_mmd(phydev, 0x1C, MII_KSZ9031RN_EDPD);
744af70c1f9SMike Looijmans 	if (reg < 0)
745af70c1f9SMike Looijmans 		return reg;
7469b420effSHeiner Kallweit 	return phy_write_mmd(phydev, 0x1C, MII_KSZ9031RN_EDPD,
747af70c1f9SMike Looijmans 			     reg | MII_KSZ9031RN_EDPD_ENABLE);
748af70c1f9SMike Looijmans }
749af70c1f9SMike Looijmans 
750bcf3440cSOleksij Rempel static int ksz9031_config_rgmii_delay(struct phy_device *phydev)
751bcf3440cSOleksij Rempel {
752bcf3440cSOleksij Rempel 	u16 rx, tx, rx_clk, tx_clk;
753bcf3440cSOleksij Rempel 	int ret;
754bcf3440cSOleksij Rempel 
755bcf3440cSOleksij Rempel 	switch (phydev->interface) {
756bcf3440cSOleksij Rempel 	case PHY_INTERFACE_MODE_RGMII:
757bcf3440cSOleksij Rempel 		tx = TX_ND;
758bcf3440cSOleksij Rempel 		tx_clk = TX_CLK_ND;
759bcf3440cSOleksij Rempel 		rx = RX_ND;
760bcf3440cSOleksij Rempel 		rx_clk = RX_CLK_ND;
761bcf3440cSOleksij Rempel 		break;
762bcf3440cSOleksij Rempel 	case PHY_INTERFACE_MODE_RGMII_ID:
763bcf3440cSOleksij Rempel 		tx = TX_ID;
764bcf3440cSOleksij Rempel 		tx_clk = TX_CLK_ID;
765bcf3440cSOleksij Rempel 		rx = RX_ID;
766bcf3440cSOleksij Rempel 		rx_clk = RX_CLK_ID;
767bcf3440cSOleksij Rempel 		break;
768bcf3440cSOleksij Rempel 	case PHY_INTERFACE_MODE_RGMII_RXID:
769bcf3440cSOleksij Rempel 		tx = TX_ND;
770bcf3440cSOleksij Rempel 		tx_clk = TX_CLK_ND;
771bcf3440cSOleksij Rempel 		rx = RX_ID;
772bcf3440cSOleksij Rempel 		rx_clk = RX_CLK_ID;
773bcf3440cSOleksij Rempel 		break;
774bcf3440cSOleksij Rempel 	case PHY_INTERFACE_MODE_RGMII_TXID:
775bcf3440cSOleksij Rempel 		tx = TX_ID;
776bcf3440cSOleksij Rempel 		tx_clk = TX_CLK_ID;
777bcf3440cSOleksij Rempel 		rx = RX_ND;
778bcf3440cSOleksij Rempel 		rx_clk = RX_CLK_ND;
779bcf3440cSOleksij Rempel 		break;
780bcf3440cSOleksij Rempel 	default:
781bcf3440cSOleksij Rempel 		return 0;
782bcf3440cSOleksij Rempel 	}
783bcf3440cSOleksij Rempel 
784bcf3440cSOleksij Rempel 	ret = phy_write_mmd(phydev, 2, MII_KSZ9031RN_CONTROL_PAD_SKEW,
785bcf3440cSOleksij Rempel 			    FIELD_PREP(MII_KSZ9031RN_RX_CTL_M, rx) |
786bcf3440cSOleksij Rempel 			    FIELD_PREP(MII_KSZ9031RN_TX_CTL_M, tx));
787bcf3440cSOleksij Rempel 	if (ret < 0)
788bcf3440cSOleksij Rempel 		return ret;
789bcf3440cSOleksij Rempel 
790bcf3440cSOleksij Rempel 	ret = phy_write_mmd(phydev, 2, MII_KSZ9031RN_RX_DATA_PAD_SKEW,
791bcf3440cSOleksij Rempel 			    FIELD_PREP(MII_KSZ9031RN_RXD3, rx) |
792bcf3440cSOleksij Rempel 			    FIELD_PREP(MII_KSZ9031RN_RXD2, rx) |
793bcf3440cSOleksij Rempel 			    FIELD_PREP(MII_KSZ9031RN_RXD1, rx) |
794bcf3440cSOleksij Rempel 			    FIELD_PREP(MII_KSZ9031RN_RXD0, rx));
795bcf3440cSOleksij Rempel 	if (ret < 0)
796bcf3440cSOleksij Rempel 		return ret;
797bcf3440cSOleksij Rempel 
798bcf3440cSOleksij Rempel 	ret = phy_write_mmd(phydev, 2, MII_KSZ9031RN_TX_DATA_PAD_SKEW,
799bcf3440cSOleksij Rempel 			    FIELD_PREP(MII_KSZ9031RN_TXD3, tx) |
800bcf3440cSOleksij Rempel 			    FIELD_PREP(MII_KSZ9031RN_TXD2, tx) |
801bcf3440cSOleksij Rempel 			    FIELD_PREP(MII_KSZ9031RN_TXD1, tx) |
802bcf3440cSOleksij Rempel 			    FIELD_PREP(MII_KSZ9031RN_TXD0, tx));
803bcf3440cSOleksij Rempel 	if (ret < 0)
804bcf3440cSOleksij Rempel 		return ret;
805bcf3440cSOleksij Rempel 
806bcf3440cSOleksij Rempel 	return phy_write_mmd(phydev, 2, MII_KSZ9031RN_CLK_PAD_SKEW,
807bcf3440cSOleksij Rempel 			     FIELD_PREP(MII_KSZ9031RN_GTX_CLK, tx_clk) |
808bcf3440cSOleksij Rempel 			     FIELD_PREP(MII_KSZ9031RN_RX_CLK, rx_clk));
809bcf3440cSOleksij Rempel }
810bcf3440cSOleksij Rempel 
8116e4b8273SHubert Chaumette static int ksz9031_config_init(struct phy_device *phydev)
8126e4b8273SHubert Chaumette {
813ce4f8afdSColin Ian King 	const struct device_node *of_node;
8143c9a9f7fSJaeden Amero 	static const char *clk_skews[2] = {"rxc-skew-ps", "txc-skew-ps"};
8153c9a9f7fSJaeden Amero 	static const char *rx_data_skews[4] = {
8166e4b8273SHubert Chaumette 		"rxd0-skew-ps", "rxd1-skew-ps",
8176e4b8273SHubert Chaumette 		"rxd2-skew-ps", "rxd3-skew-ps"
8186e4b8273SHubert Chaumette 	};
8193c9a9f7fSJaeden Amero 	static const char *tx_data_skews[4] = {
8206e4b8273SHubert Chaumette 		"txd0-skew-ps", "txd1-skew-ps",
8216e4b8273SHubert Chaumette 		"txd2-skew-ps", "txd3-skew-ps"
8226e4b8273SHubert Chaumette 	};
8233c9a9f7fSJaeden Amero 	static const char *control_skews[2] = {"txen-skew-ps", "rxdv-skew-ps"};
824b4c19f71SRoosen Henri 	const struct device *dev_walker;
825af70c1f9SMike Looijmans 	int result;
826af70c1f9SMike Looijmans 
827af70c1f9SMike Looijmans 	result = ksz9031_enable_edpd(phydev);
828af70c1f9SMike Looijmans 	if (result < 0)
829af70c1f9SMike Looijmans 		return result;
8306e4b8273SHubert Chaumette 
831b4c19f71SRoosen Henri 	/* The Micrel driver has a deprecated option to place phy OF
832b4c19f71SRoosen Henri 	 * properties in the MAC node. Walk up the tree of devices to
833b4c19f71SRoosen Henri 	 * find a device with an OF node.
834b4c19f71SRoosen Henri 	 */
8359d367eddSDavid S. Miller 	dev_walker = &phydev->mdio.dev;
836b4c19f71SRoosen Henri 	do {
837b4c19f71SRoosen Henri 		of_node = dev_walker->of_node;
838b4c19f71SRoosen Henri 		dev_walker = dev_walker->parent;
839b4c19f71SRoosen Henri 	} while (!of_node && dev_walker);
8406e4b8273SHubert Chaumette 
8416e4b8273SHubert Chaumette 	if (of_node) {
842bcf3440cSOleksij Rempel 		bool update = false;
843bcf3440cSOleksij Rempel 
844bcf3440cSOleksij Rempel 		if (phy_interface_is_rgmii(phydev)) {
845bcf3440cSOleksij Rempel 			result = ksz9031_config_rgmii_delay(phydev);
846bcf3440cSOleksij Rempel 			if (result < 0)
847bcf3440cSOleksij Rempel 				return result;
848bcf3440cSOleksij Rempel 		}
849bcf3440cSOleksij Rempel 
8506e4b8273SHubert Chaumette 		ksz9031_of_load_skew_values(phydev, of_node,
8516e4b8273SHubert Chaumette 				MII_KSZ9031RN_CLK_PAD_SKEW, 5,
852bcf3440cSOleksij Rempel 				clk_skews, 2, &update);
8536e4b8273SHubert Chaumette 
8546e4b8273SHubert Chaumette 		ksz9031_of_load_skew_values(phydev, of_node,
8556e4b8273SHubert Chaumette 				MII_KSZ9031RN_CONTROL_PAD_SKEW, 4,
856bcf3440cSOleksij Rempel 				control_skews, 2, &update);
8576e4b8273SHubert Chaumette 
8586e4b8273SHubert Chaumette 		ksz9031_of_load_skew_values(phydev, of_node,
8596e4b8273SHubert Chaumette 				MII_KSZ9031RN_RX_DATA_PAD_SKEW, 4,
860bcf3440cSOleksij Rempel 				rx_data_skews, 4, &update);
8616e4b8273SHubert Chaumette 
8626e4b8273SHubert Chaumette 		ksz9031_of_load_skew_values(phydev, of_node,
8636e4b8273SHubert Chaumette 				MII_KSZ9031RN_TX_DATA_PAD_SKEW, 4,
864bcf3440cSOleksij Rempel 				tx_data_skews, 4, &update);
865bcf3440cSOleksij Rempel 
866bcf3440cSOleksij Rempel 		if (update && phydev->interface != PHY_INTERFACE_MODE_RGMII)
867bcf3440cSOleksij Rempel 			phydev_warn(phydev,
868bcf3440cSOleksij Rempel 				    "*-skew-ps values should be used only with phy-mode = \"rgmii\"\n");
869e1b505a6SMarkus Niebel 
870e1b505a6SMarkus Niebel 		/* Silicon Errata Sheet (DS80000691D or DS80000692D):
871e1b505a6SMarkus Niebel 		 * When the device links in the 1000BASE-T slave mode only,
872e1b505a6SMarkus Niebel 		 * the optional 125MHz reference output clock (CLK125_NDO)
873e1b505a6SMarkus Niebel 		 * has wide duty cycle variation.
874e1b505a6SMarkus Niebel 		 *
875e1b505a6SMarkus Niebel 		 * The optional CLK125_NDO clock does not meet the RGMII
876e1b505a6SMarkus Niebel 		 * 45/55 percent (min/max) duty cycle requirement and therefore
877e1b505a6SMarkus Niebel 		 * cannot be used directly by the MAC side for clocking
878e1b505a6SMarkus Niebel 		 * applications that have setup/hold time requirements on
879e1b505a6SMarkus Niebel 		 * rising and falling clock edges.
880e1b505a6SMarkus Niebel 		 *
881e1b505a6SMarkus Niebel 		 * Workaround:
882e1b505a6SMarkus Niebel 		 * Force the phy to be the master to receive a stable clock
883e1b505a6SMarkus Niebel 		 * which meets the duty cycle requirement.
884e1b505a6SMarkus Niebel 		 */
885e1b505a6SMarkus Niebel 		if (of_property_read_bool(of_node, "micrel,force-master")) {
886e1b505a6SMarkus Niebel 			result = phy_read(phydev, MII_CTRL1000);
887e1b505a6SMarkus Niebel 			if (result < 0)
888e1b505a6SMarkus Niebel 				goto err_force_master;
889e1b505a6SMarkus Niebel 
890e1b505a6SMarkus Niebel 			/* enable master mode, config & prefer master */
891e1b505a6SMarkus Niebel 			result |= CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER;
892e1b505a6SMarkus Niebel 			result = phy_write(phydev, MII_CTRL1000, result);
893e1b505a6SMarkus Niebel 			if (result < 0)
894e1b505a6SMarkus Niebel 				goto err_force_master;
895e1b505a6SMarkus Niebel 		}
8966e4b8273SHubert Chaumette 	}
8976270e1aeSJaeden Amero 
8986270e1aeSJaeden Amero 	return ksz9031_center_flp_timing(phydev);
899e1b505a6SMarkus Niebel 
900e1b505a6SMarkus Niebel err_force_master:
901e1b505a6SMarkus Niebel 	phydev_err(phydev, "failed to force the phy to master mode\n");
902e1b505a6SMarkus Niebel 	return result;
9036e4b8273SHubert Chaumette }
9046e4b8273SHubert Chaumette 
905bff5b4b3SYuiko Oshino #define KSZ9131_SKEW_5BIT_MAX	2400
906bff5b4b3SYuiko Oshino #define KSZ9131_SKEW_4BIT_MAX	800
907bff5b4b3SYuiko Oshino #define KSZ9131_OFFSET		700
908bff5b4b3SYuiko Oshino #define KSZ9131_STEP		100
909bff5b4b3SYuiko Oshino 
910bff5b4b3SYuiko Oshino static int ksz9131_of_load_skew_values(struct phy_device *phydev,
911bff5b4b3SYuiko Oshino 				       struct device_node *of_node,
912bff5b4b3SYuiko Oshino 				       u16 reg, size_t field_sz,
913bff5b4b3SYuiko Oshino 				       char *field[], u8 numfields)
914bff5b4b3SYuiko Oshino {
915bff5b4b3SYuiko Oshino 	int val[4] = {-(1 + KSZ9131_OFFSET), -(2 + KSZ9131_OFFSET),
916bff5b4b3SYuiko Oshino 		      -(3 + KSZ9131_OFFSET), -(4 + KSZ9131_OFFSET)};
917bff5b4b3SYuiko Oshino 	int skewval, skewmax = 0;
918bff5b4b3SYuiko Oshino 	int matches = 0;
919bff5b4b3SYuiko Oshino 	u16 maxval;
920bff5b4b3SYuiko Oshino 	u16 newval;
921bff5b4b3SYuiko Oshino 	u16 mask;
922bff5b4b3SYuiko Oshino 	int i;
923bff5b4b3SYuiko Oshino 
924bff5b4b3SYuiko Oshino 	/* psec properties in dts should mean x pico seconds */
925bff5b4b3SYuiko Oshino 	if (field_sz == 5)
926bff5b4b3SYuiko Oshino 		skewmax = KSZ9131_SKEW_5BIT_MAX;
927bff5b4b3SYuiko Oshino 	else
928bff5b4b3SYuiko Oshino 		skewmax = KSZ9131_SKEW_4BIT_MAX;
929bff5b4b3SYuiko Oshino 
930bff5b4b3SYuiko Oshino 	for (i = 0; i < numfields; i++)
931bff5b4b3SYuiko Oshino 		if (!of_property_read_s32(of_node, field[i], &skewval)) {
932bff5b4b3SYuiko Oshino 			if (skewval < -KSZ9131_OFFSET)
933bff5b4b3SYuiko Oshino 				skewval = -KSZ9131_OFFSET;
934bff5b4b3SYuiko Oshino 			else if (skewval > skewmax)
935bff5b4b3SYuiko Oshino 				skewval = skewmax;
936bff5b4b3SYuiko Oshino 
937bff5b4b3SYuiko Oshino 			val[i] = skewval + KSZ9131_OFFSET;
938bff5b4b3SYuiko Oshino 			matches++;
939bff5b4b3SYuiko Oshino 		}
940bff5b4b3SYuiko Oshino 
941bff5b4b3SYuiko Oshino 	if (!matches)
942bff5b4b3SYuiko Oshino 		return 0;
943bff5b4b3SYuiko Oshino 
944bff5b4b3SYuiko Oshino 	if (matches < numfields)
9459b420effSHeiner Kallweit 		newval = phy_read_mmd(phydev, 2, reg);
946bff5b4b3SYuiko Oshino 	else
947bff5b4b3SYuiko Oshino 		newval = 0;
948bff5b4b3SYuiko Oshino 
949bff5b4b3SYuiko Oshino 	maxval = (field_sz == 4) ? 0xf : 0x1f;
950bff5b4b3SYuiko Oshino 	for (i = 0; i < numfields; i++)
951bff5b4b3SYuiko Oshino 		if (val[i] != -(i + 1 + KSZ9131_OFFSET)) {
952bff5b4b3SYuiko Oshino 			mask = 0xffff;
953bff5b4b3SYuiko Oshino 			mask ^= maxval << (field_sz * i);
954bff5b4b3SYuiko Oshino 			newval = (newval & mask) |
955bff5b4b3SYuiko Oshino 				(((val[i] / KSZ9131_STEP) & maxval)
956bff5b4b3SYuiko Oshino 					<< (field_sz * i));
957bff5b4b3SYuiko Oshino 		}
958bff5b4b3SYuiko Oshino 
9599b420effSHeiner Kallweit 	return phy_write_mmd(phydev, 2, reg, newval);
960bff5b4b3SYuiko Oshino }
961bff5b4b3SYuiko Oshino 
962bd734a74SPhilippe Schenker #define KSZ9131RN_MMD_COMMON_CTRL_REG	2
963bd734a74SPhilippe Schenker #define KSZ9131RN_RXC_DLL_CTRL		76
964bd734a74SPhilippe Schenker #define KSZ9131RN_TXC_DLL_CTRL		77
965bd734a74SPhilippe Schenker #define KSZ9131RN_DLL_CTRL_BYPASS	BIT_MASK(12)
966bd734a74SPhilippe Schenker #define KSZ9131RN_DLL_ENABLE_DELAY	0
967bd734a74SPhilippe Schenker #define KSZ9131RN_DLL_DISABLE_DELAY	BIT(12)
968bd734a74SPhilippe Schenker 
969bd734a74SPhilippe Schenker static int ksz9131_config_rgmii_delay(struct phy_device *phydev)
970bd734a74SPhilippe Schenker {
971bd734a74SPhilippe Schenker 	u16 rxcdll_val, txcdll_val;
972bd734a74SPhilippe Schenker 	int ret;
973bd734a74SPhilippe Schenker 
974bd734a74SPhilippe Schenker 	switch (phydev->interface) {
975bd734a74SPhilippe Schenker 	case PHY_INTERFACE_MODE_RGMII:
976bd734a74SPhilippe Schenker 		rxcdll_val = KSZ9131RN_DLL_DISABLE_DELAY;
977bd734a74SPhilippe Schenker 		txcdll_val = KSZ9131RN_DLL_DISABLE_DELAY;
978bd734a74SPhilippe Schenker 		break;
979bd734a74SPhilippe Schenker 	case PHY_INTERFACE_MODE_RGMII_ID:
980bd734a74SPhilippe Schenker 		rxcdll_val = KSZ9131RN_DLL_ENABLE_DELAY;
981bd734a74SPhilippe Schenker 		txcdll_val = KSZ9131RN_DLL_ENABLE_DELAY;
982bd734a74SPhilippe Schenker 		break;
983bd734a74SPhilippe Schenker 	case PHY_INTERFACE_MODE_RGMII_RXID:
984bd734a74SPhilippe Schenker 		rxcdll_val = KSZ9131RN_DLL_ENABLE_DELAY;
985bd734a74SPhilippe Schenker 		txcdll_val = KSZ9131RN_DLL_DISABLE_DELAY;
986bd734a74SPhilippe Schenker 		break;
987bd734a74SPhilippe Schenker 	case PHY_INTERFACE_MODE_RGMII_TXID:
988bd734a74SPhilippe Schenker 		rxcdll_val = KSZ9131RN_DLL_DISABLE_DELAY;
989bd734a74SPhilippe Schenker 		txcdll_val = KSZ9131RN_DLL_ENABLE_DELAY;
990bd734a74SPhilippe Schenker 		break;
991bd734a74SPhilippe Schenker 	default:
992bd734a74SPhilippe Schenker 		return 0;
993bd734a74SPhilippe Schenker 	}
994bd734a74SPhilippe Schenker 
995bd734a74SPhilippe Schenker 	ret = phy_modify_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
996bd734a74SPhilippe Schenker 			     KSZ9131RN_RXC_DLL_CTRL, KSZ9131RN_DLL_CTRL_BYPASS,
997bd734a74SPhilippe Schenker 			     rxcdll_val);
998bd734a74SPhilippe Schenker 	if (ret < 0)
999bd734a74SPhilippe Schenker 		return ret;
1000bd734a74SPhilippe Schenker 
1001bd734a74SPhilippe Schenker 	return phy_modify_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
1002bd734a74SPhilippe Schenker 			      KSZ9131RN_TXC_DLL_CTRL, KSZ9131RN_DLL_CTRL_BYPASS,
1003bd734a74SPhilippe Schenker 			      txcdll_val);
1004bd734a74SPhilippe Schenker }
1005bd734a74SPhilippe Schenker 
1006bff5b4b3SYuiko Oshino static int ksz9131_config_init(struct phy_device *phydev)
1007bff5b4b3SYuiko Oshino {
1008ce4f8afdSColin Ian King 	struct device_node *of_node;
1009bff5b4b3SYuiko Oshino 	char *clk_skews[2] = {"rxc-skew-psec", "txc-skew-psec"};
1010bff5b4b3SYuiko Oshino 	char *rx_data_skews[4] = {
1011bff5b4b3SYuiko Oshino 		"rxd0-skew-psec", "rxd1-skew-psec",
1012bff5b4b3SYuiko Oshino 		"rxd2-skew-psec", "rxd3-skew-psec"
1013bff5b4b3SYuiko Oshino 	};
1014bff5b4b3SYuiko Oshino 	char *tx_data_skews[4] = {
1015bff5b4b3SYuiko Oshino 		"txd0-skew-psec", "txd1-skew-psec",
1016bff5b4b3SYuiko Oshino 		"txd2-skew-psec", "txd3-skew-psec"
1017bff5b4b3SYuiko Oshino 	};
1018bff5b4b3SYuiko Oshino 	char *control_skews[2] = {"txen-skew-psec", "rxdv-skew-psec"};
1019bff5b4b3SYuiko Oshino 	const struct device *dev_walker;
1020bff5b4b3SYuiko Oshino 	int ret;
1021bff5b4b3SYuiko Oshino 
1022bff5b4b3SYuiko Oshino 	dev_walker = &phydev->mdio.dev;
1023bff5b4b3SYuiko Oshino 	do {
1024bff5b4b3SYuiko Oshino 		of_node = dev_walker->of_node;
1025bff5b4b3SYuiko Oshino 		dev_walker = dev_walker->parent;
1026bff5b4b3SYuiko Oshino 	} while (!of_node && dev_walker);
1027bff5b4b3SYuiko Oshino 
1028bff5b4b3SYuiko Oshino 	if (!of_node)
1029bff5b4b3SYuiko Oshino 		return 0;
1030bff5b4b3SYuiko Oshino 
1031bd734a74SPhilippe Schenker 	if (phy_interface_is_rgmii(phydev)) {
1032bd734a74SPhilippe Schenker 		ret = ksz9131_config_rgmii_delay(phydev);
1033bd734a74SPhilippe Schenker 		if (ret < 0)
1034bd734a74SPhilippe Schenker 			return ret;
1035bd734a74SPhilippe Schenker 	}
1036bd734a74SPhilippe Schenker 
1037bff5b4b3SYuiko Oshino 	ret = ksz9131_of_load_skew_values(phydev, of_node,
1038bff5b4b3SYuiko Oshino 					  MII_KSZ9031RN_CLK_PAD_SKEW, 5,
1039bff5b4b3SYuiko Oshino 					  clk_skews, 2);
1040bff5b4b3SYuiko Oshino 	if (ret < 0)
1041bff5b4b3SYuiko Oshino 		return ret;
1042bff5b4b3SYuiko Oshino 
1043bff5b4b3SYuiko Oshino 	ret = ksz9131_of_load_skew_values(phydev, of_node,
1044bff5b4b3SYuiko Oshino 					  MII_KSZ9031RN_CONTROL_PAD_SKEW, 4,
1045bff5b4b3SYuiko Oshino 					  control_skews, 2);
1046bff5b4b3SYuiko Oshino 	if (ret < 0)
1047bff5b4b3SYuiko Oshino 		return ret;
1048bff5b4b3SYuiko Oshino 
1049bff5b4b3SYuiko Oshino 	ret = ksz9131_of_load_skew_values(phydev, of_node,
1050bff5b4b3SYuiko Oshino 					  MII_KSZ9031RN_RX_DATA_PAD_SKEW, 4,
1051bff5b4b3SYuiko Oshino 					  rx_data_skews, 4);
1052bff5b4b3SYuiko Oshino 	if (ret < 0)
1053bff5b4b3SYuiko Oshino 		return ret;
1054bff5b4b3SYuiko Oshino 
1055bff5b4b3SYuiko Oshino 	ret = ksz9131_of_load_skew_values(phydev, of_node,
1056bff5b4b3SYuiko Oshino 					  MII_KSZ9031RN_TX_DATA_PAD_SKEW, 4,
1057bff5b4b3SYuiko Oshino 					  tx_data_skews, 4);
1058bff5b4b3SYuiko Oshino 	if (ret < 0)
1059bff5b4b3SYuiko Oshino 		return ret;
1060bff5b4b3SYuiko Oshino 
1061bff5b4b3SYuiko Oshino 	return 0;
1062bff5b4b3SYuiko Oshino }
1063bff5b4b3SYuiko Oshino 
106493272e07SJean-Christophe PLAGNIOL-VILLARD #define KSZ8873MLL_GLOBAL_CONTROL_4	0x06
106500aee095SJohan Hovold #define KSZ8873MLL_GLOBAL_CONTROL_4_DUPLEX	BIT(6)
106600aee095SJohan Hovold #define KSZ8873MLL_GLOBAL_CONTROL_4_SPEED	BIT(4)
106732d73b14SJingoo Han static int ksz8873mll_read_status(struct phy_device *phydev)
106893272e07SJean-Christophe PLAGNIOL-VILLARD {
106993272e07SJean-Christophe PLAGNIOL-VILLARD 	int regval;
107093272e07SJean-Christophe PLAGNIOL-VILLARD 
107193272e07SJean-Christophe PLAGNIOL-VILLARD 	/* dummy read */
107293272e07SJean-Christophe PLAGNIOL-VILLARD 	regval = phy_read(phydev, KSZ8873MLL_GLOBAL_CONTROL_4);
107393272e07SJean-Christophe PLAGNIOL-VILLARD 
107493272e07SJean-Christophe PLAGNIOL-VILLARD 	regval = phy_read(phydev, KSZ8873MLL_GLOBAL_CONTROL_4);
107593272e07SJean-Christophe PLAGNIOL-VILLARD 
107693272e07SJean-Christophe PLAGNIOL-VILLARD 	if (regval & KSZ8873MLL_GLOBAL_CONTROL_4_DUPLEX)
107793272e07SJean-Christophe PLAGNIOL-VILLARD 		phydev->duplex = DUPLEX_HALF;
107893272e07SJean-Christophe PLAGNIOL-VILLARD 	else
107993272e07SJean-Christophe PLAGNIOL-VILLARD 		phydev->duplex = DUPLEX_FULL;
108093272e07SJean-Christophe PLAGNIOL-VILLARD 
108193272e07SJean-Christophe PLAGNIOL-VILLARD 	if (regval & KSZ8873MLL_GLOBAL_CONTROL_4_SPEED)
108293272e07SJean-Christophe PLAGNIOL-VILLARD 		phydev->speed = SPEED_10;
108393272e07SJean-Christophe PLAGNIOL-VILLARD 	else
108493272e07SJean-Christophe PLAGNIOL-VILLARD 		phydev->speed = SPEED_100;
108593272e07SJean-Christophe PLAGNIOL-VILLARD 
108693272e07SJean-Christophe PLAGNIOL-VILLARD 	phydev->link = 1;
108793272e07SJean-Christophe PLAGNIOL-VILLARD 	phydev->pause = phydev->asym_pause = 0;
108893272e07SJean-Christophe PLAGNIOL-VILLARD 
108993272e07SJean-Christophe PLAGNIOL-VILLARD 	return 0;
109093272e07SJean-Christophe PLAGNIOL-VILLARD }
109193272e07SJean-Christophe PLAGNIOL-VILLARD 
10923aed3e2aSAntoine Tenart static int ksz9031_get_features(struct phy_device *phydev)
10933aed3e2aSAntoine Tenart {
10943aed3e2aSAntoine Tenart 	int ret;
10953aed3e2aSAntoine Tenart 
10963aed3e2aSAntoine Tenart 	ret = genphy_read_abilities(phydev);
10973aed3e2aSAntoine Tenart 	if (ret < 0)
10983aed3e2aSAntoine Tenart 		return ret;
10993aed3e2aSAntoine Tenart 
11003aed3e2aSAntoine Tenart 	/* Silicon Errata Sheet (DS80000691D or DS80000692D):
11013aed3e2aSAntoine Tenart 	 * Whenever the device's Asymmetric Pause capability is set to 1,
11023aed3e2aSAntoine Tenart 	 * link-up may fail after a link-up to link-down transition.
11033aed3e2aSAntoine Tenart 	 *
1104407d8098SHans Andersson 	 * The Errata Sheet is for ksz9031, but ksz9021 has the same issue
1105407d8098SHans Andersson 	 *
11063aed3e2aSAntoine Tenart 	 * Workaround:
11073aed3e2aSAntoine Tenart 	 * Do not enable the Asymmetric Pause capability bit.
11083aed3e2aSAntoine Tenart 	 */
11093aed3e2aSAntoine Tenart 	linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported);
11103aed3e2aSAntoine Tenart 
11113aed3e2aSAntoine Tenart 	/* We force setting the Pause capability as the core will force the
11123aed3e2aSAntoine Tenart 	 * Asymmetric Pause capability to 1 otherwise.
11133aed3e2aSAntoine Tenart 	 */
11143aed3e2aSAntoine Tenart 	linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported);
11153aed3e2aSAntoine Tenart 
11163aed3e2aSAntoine Tenart 	return 0;
11173aed3e2aSAntoine Tenart }
11183aed3e2aSAntoine Tenart 
1119d2fd719bSNathan Sullivan static int ksz9031_read_status(struct phy_device *phydev)
1120d2fd719bSNathan Sullivan {
1121d2fd719bSNathan Sullivan 	int err;
1122d2fd719bSNathan Sullivan 	int regval;
1123d2fd719bSNathan Sullivan 
1124d2fd719bSNathan Sullivan 	err = genphy_read_status(phydev);
1125d2fd719bSNathan Sullivan 	if (err)
1126d2fd719bSNathan Sullivan 		return err;
1127d2fd719bSNathan Sullivan 
1128d2fd719bSNathan Sullivan 	/* Make sure the PHY is not broken. Read idle error count,
1129d2fd719bSNathan Sullivan 	 * and reset the PHY if it is maxed out.
1130d2fd719bSNathan Sullivan 	 */
1131d2fd719bSNathan Sullivan 	regval = phy_read(phydev, MII_STAT1000);
1132d2fd719bSNathan Sullivan 	if ((regval & 0xFF) == 0xFF) {
1133d2fd719bSNathan Sullivan 		phy_init_hw(phydev);
1134d2fd719bSNathan Sullivan 		phydev->link = 0;
1135b866203dSZach Brown 		if (phydev->drv->config_intr && phy_interrupt_is_valid(phydev))
1136b866203dSZach Brown 			phydev->drv->config_intr(phydev);
1137c1a8d0a3SGrygorii Strashko 		return genphy_config_aneg(phydev);
1138d2fd719bSNathan Sullivan 	}
1139d2fd719bSNathan Sullivan 
1140d2fd719bSNathan Sullivan 	return 0;
1141d2fd719bSNathan Sullivan }
1142d2fd719bSNathan Sullivan 
114393272e07SJean-Christophe PLAGNIOL-VILLARD static int ksz8873mll_config_aneg(struct phy_device *phydev)
114493272e07SJean-Christophe PLAGNIOL-VILLARD {
114593272e07SJean-Christophe PLAGNIOL-VILLARD 	return 0;
114693272e07SJean-Christophe PLAGNIOL-VILLARD }
114793272e07SJean-Christophe PLAGNIOL-VILLARD 
114852939393SOleksij Rempel static int ksz886x_config_mdix(struct phy_device *phydev, u8 ctrl)
114952939393SOleksij Rempel {
115052939393SOleksij Rempel 	u16 val;
115152939393SOleksij Rempel 
115252939393SOleksij Rempel 	switch (ctrl) {
115352939393SOleksij Rempel 	case ETH_TP_MDI:
115452939393SOleksij Rempel 		val = KSZ886X_BMCR_DISABLE_AUTO_MDIX;
115552939393SOleksij Rempel 		break;
115652939393SOleksij Rempel 	case ETH_TP_MDI_X:
115752939393SOleksij Rempel 		/* Note: The naming of the bit KSZ886X_BMCR_FORCE_MDI is bit
115852939393SOleksij Rempel 		 * counter intuitive, the "-X" in "1 = Force MDI" in the data
115952939393SOleksij Rempel 		 * sheet seems to be missing:
116052939393SOleksij Rempel 		 * 1 = Force MDI (sic!) (transmit on RX+/RX- pins)
116152939393SOleksij Rempel 		 * 0 = Normal operation (transmit on TX+/TX- pins)
116252939393SOleksij Rempel 		 */
116352939393SOleksij Rempel 		val = KSZ886X_BMCR_DISABLE_AUTO_MDIX | KSZ886X_BMCR_FORCE_MDI;
116452939393SOleksij Rempel 		break;
116552939393SOleksij Rempel 	case ETH_TP_MDI_AUTO:
116652939393SOleksij Rempel 		val = 0;
116752939393SOleksij Rempel 		break;
116852939393SOleksij Rempel 	default:
116952939393SOleksij Rempel 		return 0;
117052939393SOleksij Rempel 	}
117152939393SOleksij Rempel 
117252939393SOleksij Rempel 	return phy_modify(phydev, MII_BMCR,
117352939393SOleksij Rempel 			  KSZ886X_BMCR_HP_MDIX | KSZ886X_BMCR_FORCE_MDI |
117452939393SOleksij Rempel 			  KSZ886X_BMCR_DISABLE_AUTO_MDIX,
117552939393SOleksij Rempel 			  KSZ886X_BMCR_HP_MDIX | val);
117652939393SOleksij Rempel }
117752939393SOleksij Rempel 
117852939393SOleksij Rempel static int ksz886x_config_aneg(struct phy_device *phydev)
117952939393SOleksij Rempel {
118052939393SOleksij Rempel 	int ret;
118152939393SOleksij Rempel 
118252939393SOleksij Rempel 	ret = genphy_config_aneg(phydev);
118352939393SOleksij Rempel 	if (ret)
118452939393SOleksij Rempel 		return ret;
118552939393SOleksij Rempel 
118652939393SOleksij Rempel 	/* The MDI-X configuration is automatically changed by the PHY after
118752939393SOleksij Rempel 	 * switching from autoneg off to on. So, take MDI-X configuration under
118852939393SOleksij Rempel 	 * own control and set it after autoneg configuration was done.
118952939393SOleksij Rempel 	 */
119052939393SOleksij Rempel 	return ksz886x_config_mdix(phydev, phydev->mdix_ctrl);
119152939393SOleksij Rempel }
119252939393SOleksij Rempel 
119352939393SOleksij Rempel static int ksz886x_mdix_update(struct phy_device *phydev)
119452939393SOleksij Rempel {
119552939393SOleksij Rempel 	int ret;
119652939393SOleksij Rempel 
119752939393SOleksij Rempel 	ret = phy_read(phydev, MII_BMCR);
119852939393SOleksij Rempel 	if (ret < 0)
119952939393SOleksij Rempel 		return ret;
120052939393SOleksij Rempel 
120152939393SOleksij Rempel 	if (ret & KSZ886X_BMCR_DISABLE_AUTO_MDIX) {
120252939393SOleksij Rempel 		if (ret & KSZ886X_BMCR_FORCE_MDI)
120352939393SOleksij Rempel 			phydev->mdix_ctrl = ETH_TP_MDI_X;
120452939393SOleksij Rempel 		else
120552939393SOleksij Rempel 			phydev->mdix_ctrl = ETH_TP_MDI;
120652939393SOleksij Rempel 	} else {
120752939393SOleksij Rempel 		phydev->mdix_ctrl = ETH_TP_MDI_AUTO;
120852939393SOleksij Rempel 	}
120952939393SOleksij Rempel 
121052939393SOleksij Rempel 	ret = phy_read(phydev, MII_KSZPHY_CTRL);
121152939393SOleksij Rempel 	if (ret < 0)
121252939393SOleksij Rempel 		return ret;
121352939393SOleksij Rempel 
121452939393SOleksij Rempel 	/* Same reverse logic as KSZ886X_BMCR_FORCE_MDI */
121552939393SOleksij Rempel 	if (ret & KSZ886X_CTRL_MDIX_STAT)
121652939393SOleksij Rempel 		phydev->mdix = ETH_TP_MDI_X;
121752939393SOleksij Rempel 	else
121852939393SOleksij Rempel 		phydev->mdix = ETH_TP_MDI;
121952939393SOleksij Rempel 
122052939393SOleksij Rempel 	return 0;
122152939393SOleksij Rempel }
122252939393SOleksij Rempel 
122352939393SOleksij Rempel static int ksz886x_read_status(struct phy_device *phydev)
122452939393SOleksij Rempel {
122552939393SOleksij Rempel 	int ret;
122652939393SOleksij Rempel 
122752939393SOleksij Rempel 	ret = ksz886x_mdix_update(phydev);
122852939393SOleksij Rempel 	if (ret < 0)
122952939393SOleksij Rempel 		return ret;
123052939393SOleksij Rempel 
123152939393SOleksij Rempel 	return genphy_read_status(phydev);
123252939393SOleksij Rempel }
123352939393SOleksij Rempel 
12342b2427d0SAndrew Lunn static int kszphy_get_sset_count(struct phy_device *phydev)
12352b2427d0SAndrew Lunn {
12362b2427d0SAndrew Lunn 	return ARRAY_SIZE(kszphy_hw_stats);
12372b2427d0SAndrew Lunn }
12382b2427d0SAndrew Lunn 
12392b2427d0SAndrew Lunn static void kszphy_get_strings(struct phy_device *phydev, u8 *data)
12402b2427d0SAndrew Lunn {
12412b2427d0SAndrew Lunn 	int i;
12422b2427d0SAndrew Lunn 
12432b2427d0SAndrew Lunn 	for (i = 0; i < ARRAY_SIZE(kszphy_hw_stats); i++) {
124455f53567SFlorian Fainelli 		strlcpy(data + i * ETH_GSTRING_LEN,
12452b2427d0SAndrew Lunn 			kszphy_hw_stats[i].string, ETH_GSTRING_LEN);
12462b2427d0SAndrew Lunn 	}
12472b2427d0SAndrew Lunn }
12482b2427d0SAndrew Lunn 
12492b2427d0SAndrew Lunn static u64 kszphy_get_stat(struct phy_device *phydev, int i)
12502b2427d0SAndrew Lunn {
12512b2427d0SAndrew Lunn 	struct kszphy_hw_stat stat = kszphy_hw_stats[i];
12522b2427d0SAndrew Lunn 	struct kszphy_priv *priv = phydev->priv;
1253321b4d4bSAndrew Lunn 	int val;
1254321b4d4bSAndrew Lunn 	u64 ret;
12552b2427d0SAndrew Lunn 
12562b2427d0SAndrew Lunn 	val = phy_read(phydev, stat.reg);
12572b2427d0SAndrew Lunn 	if (val < 0) {
12586c3442f5SJisheng Zhang 		ret = U64_MAX;
12592b2427d0SAndrew Lunn 	} else {
12602b2427d0SAndrew Lunn 		val = val & ((1 << stat.bits) - 1);
12612b2427d0SAndrew Lunn 		priv->stats[i] += val;
1262321b4d4bSAndrew Lunn 		ret = priv->stats[i];
12632b2427d0SAndrew Lunn 	}
12642b2427d0SAndrew Lunn 
1265321b4d4bSAndrew Lunn 	return ret;
12662b2427d0SAndrew Lunn }
12672b2427d0SAndrew Lunn 
12682b2427d0SAndrew Lunn static void kszphy_get_stats(struct phy_device *phydev,
12692b2427d0SAndrew Lunn 			     struct ethtool_stats *stats, u64 *data)
12702b2427d0SAndrew Lunn {
12712b2427d0SAndrew Lunn 	int i;
12722b2427d0SAndrew Lunn 
12732b2427d0SAndrew Lunn 	for (i = 0; i < ARRAY_SIZE(kszphy_hw_stats); i++)
12742b2427d0SAndrew Lunn 		data[i] = kszphy_get_stat(phydev, i);
12752b2427d0SAndrew Lunn }
12762b2427d0SAndrew Lunn 
1277836384d2SWenyou Yang static int kszphy_suspend(struct phy_device *phydev)
1278836384d2SWenyou Yang {
1279836384d2SWenyou Yang 	/* Disable PHY Interrupts */
1280836384d2SWenyou Yang 	if (phy_interrupt_is_valid(phydev)) {
1281836384d2SWenyou Yang 		phydev->interrupts = PHY_INTERRUPT_DISABLED;
1282836384d2SWenyou Yang 		if (phydev->drv->config_intr)
1283836384d2SWenyou Yang 			phydev->drv->config_intr(phydev);
1284836384d2SWenyou Yang 	}
1285836384d2SWenyou Yang 
1286836384d2SWenyou Yang 	return genphy_suspend(phydev);
1287836384d2SWenyou Yang }
1288836384d2SWenyou Yang 
1289f5aba91dSAlexandre Belloni static int kszphy_resume(struct phy_device *phydev)
1290f5aba91dSAlexandre Belloni {
129179e498a9SLeonard Crestez 	int ret;
129279e498a9SLeonard Crestez 
1293836384d2SWenyou Yang 	genphy_resume(phydev);
1294f5aba91dSAlexandre Belloni 
12956110dff7SOleksij Rempel 	/* After switching from power-down to normal mode, an internal global
12966110dff7SOleksij Rempel 	 * reset is automatically generated. Wait a minimum of 1 ms before
12976110dff7SOleksij Rempel 	 * read/write access to the PHY registers.
12986110dff7SOleksij Rempel 	 */
12996110dff7SOleksij Rempel 	usleep_range(1000, 2000);
13006110dff7SOleksij Rempel 
130179e498a9SLeonard Crestez 	ret = kszphy_config_reset(phydev);
130279e498a9SLeonard Crestez 	if (ret)
130379e498a9SLeonard Crestez 		return ret;
130479e498a9SLeonard Crestez 
1305836384d2SWenyou Yang 	/* Enable PHY Interrupts */
1306836384d2SWenyou Yang 	if (phy_interrupt_is_valid(phydev)) {
1307836384d2SWenyou Yang 		phydev->interrupts = PHY_INTERRUPT_ENABLED;
1308836384d2SWenyou Yang 		if (phydev->drv->config_intr)
1309836384d2SWenyou Yang 			phydev->drv->config_intr(phydev);
1310836384d2SWenyou Yang 	}
1311f5aba91dSAlexandre Belloni 
1312f5aba91dSAlexandre Belloni 	return 0;
1313f5aba91dSAlexandre Belloni }
1314f5aba91dSAlexandre Belloni 
1315e6a423a8SJohan Hovold static int kszphy_probe(struct phy_device *phydev)
1316e6a423a8SJohan Hovold {
1317e6a423a8SJohan Hovold 	const struct kszphy_type *type = phydev->drv->driver_data;
1318e5a03bfdSAndrew Lunn 	const struct device_node *np = phydev->mdio.dev.of_node;
1319e6a423a8SJohan Hovold 	struct kszphy_priv *priv;
132063f44b2bSJohan Hovold 	struct clk *clk;
1321e7a792e9SJohan Hovold 	int ret;
1322e6a423a8SJohan Hovold 
1323e5a03bfdSAndrew Lunn 	priv = devm_kzalloc(&phydev->mdio.dev, sizeof(*priv), GFP_KERNEL);
1324e6a423a8SJohan Hovold 	if (!priv)
1325e6a423a8SJohan Hovold 		return -ENOMEM;
1326e6a423a8SJohan Hovold 
1327e6a423a8SJohan Hovold 	phydev->priv = priv;
1328e6a423a8SJohan Hovold 
1329e6a423a8SJohan Hovold 	priv->type = type;
1330e6a423a8SJohan Hovold 
1331e7a792e9SJohan Hovold 	if (type->led_mode_reg) {
1332e7a792e9SJohan Hovold 		ret = of_property_read_u32(np, "micrel,led-mode",
1333e7a792e9SJohan Hovold 				&priv->led_mode);
1334e7a792e9SJohan Hovold 		if (ret)
1335e7a792e9SJohan Hovold 			priv->led_mode = -1;
1336e7a792e9SJohan Hovold 
1337e7a792e9SJohan Hovold 		if (priv->led_mode > 3) {
133872ba48beSAndrew Lunn 			phydev_err(phydev, "invalid led mode: 0x%02x\n",
1339e7a792e9SJohan Hovold 				   priv->led_mode);
1340e7a792e9SJohan Hovold 			priv->led_mode = -1;
1341e7a792e9SJohan Hovold 		}
1342e7a792e9SJohan Hovold 	} else {
1343e7a792e9SJohan Hovold 		priv->led_mode = -1;
1344e7a792e9SJohan Hovold 	}
1345e7a792e9SJohan Hovold 
1346e5a03bfdSAndrew Lunn 	clk = devm_clk_get(&phydev->mdio.dev, "rmii-ref");
1347bced8701SNiklas Cassel 	/* NOTE: clk may be NULL if building without CONFIG_HAVE_CLK */
1348bced8701SNiklas Cassel 	if (!IS_ERR_OR_NULL(clk)) {
13491fadee0cSSascha Hauer 		unsigned long rate = clk_get_rate(clk);
135086dc1342SJohan Hovold 		bool rmii_ref_clk_sel_25_mhz;
13511fadee0cSSascha Hauer 
135263f44b2bSJohan Hovold 		priv->rmii_ref_clk_sel = type->has_rmii_ref_clk_sel;
135386dc1342SJohan Hovold 		rmii_ref_clk_sel_25_mhz = of_property_read_bool(np,
135486dc1342SJohan Hovold 				"micrel,rmii-reference-clock-select-25-mhz");
135563f44b2bSJohan Hovold 
13561fadee0cSSascha Hauer 		if (rate > 24500000 && rate < 25500000) {
135786dc1342SJohan Hovold 			priv->rmii_ref_clk_sel_val = rmii_ref_clk_sel_25_mhz;
13581fadee0cSSascha Hauer 		} else if (rate > 49500000 && rate < 50500000) {
135986dc1342SJohan Hovold 			priv->rmii_ref_clk_sel_val = !rmii_ref_clk_sel_25_mhz;
13601fadee0cSSascha Hauer 		} else {
136172ba48beSAndrew Lunn 			phydev_err(phydev, "Clock rate out of range: %ld\n",
136272ba48beSAndrew Lunn 				   rate);
13631fadee0cSSascha Hauer 			return -EINVAL;
13641fadee0cSSascha Hauer 		}
13651fadee0cSSascha Hauer 	}
13661fadee0cSSascha Hauer 
13674217a64eSMichael Walle 	if (ksz8041_fiber_mode(phydev))
13684217a64eSMichael Walle 		phydev->port = PORT_FIBRE;
13694217a64eSMichael Walle 
137063f44b2bSJohan Hovold 	/* Support legacy board-file configuration */
137163f44b2bSJohan Hovold 	if (phydev->dev_flags & MICREL_PHY_50MHZ_CLK) {
137263f44b2bSJohan Hovold 		priv->rmii_ref_clk_sel = true;
137363f44b2bSJohan Hovold 		priv->rmii_ref_clk_sel_val = true;
137463f44b2bSJohan Hovold 	}
137563f44b2bSJohan Hovold 
137663f44b2bSJohan Hovold 	return 0;
13771fadee0cSSascha Hauer }
13781fadee0cSSascha Hauer 
137949011e0cSOleksij Rempel static int ksz886x_cable_test_start(struct phy_device *phydev)
138049011e0cSOleksij Rempel {
138149011e0cSOleksij Rempel 	if (phydev->dev_flags & MICREL_KSZ8_P1_ERRATA)
138249011e0cSOleksij Rempel 		return -EOPNOTSUPP;
138349011e0cSOleksij Rempel 
138449011e0cSOleksij Rempel 	/* If autoneg is enabled, we won't be able to test cross pair
138549011e0cSOleksij Rempel 	 * short. In this case, the PHY will "detect" a link and
138649011e0cSOleksij Rempel 	 * confuse the internal state machine - disable auto neg here.
138749011e0cSOleksij Rempel 	 * If autoneg is disabled, we should set the speed to 10mbit.
138849011e0cSOleksij Rempel 	 */
138949011e0cSOleksij Rempel 	return phy_clear_bits(phydev, MII_BMCR, BMCR_ANENABLE | BMCR_SPEED100);
139049011e0cSOleksij Rempel }
139149011e0cSOleksij Rempel 
139249011e0cSOleksij Rempel static int ksz886x_cable_test_result_trans(u16 status)
139349011e0cSOleksij Rempel {
139449011e0cSOleksij Rempel 	switch (FIELD_GET(KSZ8081_LMD_STAT_MASK, status)) {
139549011e0cSOleksij Rempel 	case KSZ8081_LMD_STAT_NORMAL:
139649011e0cSOleksij Rempel 		return ETHTOOL_A_CABLE_RESULT_CODE_OK;
139749011e0cSOleksij Rempel 	case KSZ8081_LMD_STAT_SHORT:
139849011e0cSOleksij Rempel 		return ETHTOOL_A_CABLE_RESULT_CODE_SAME_SHORT;
139949011e0cSOleksij Rempel 	case KSZ8081_LMD_STAT_OPEN:
140049011e0cSOleksij Rempel 		return ETHTOOL_A_CABLE_RESULT_CODE_OPEN;
140149011e0cSOleksij Rempel 	case KSZ8081_LMD_STAT_FAIL:
140249011e0cSOleksij Rempel 		fallthrough;
140349011e0cSOleksij Rempel 	default:
140449011e0cSOleksij Rempel 		return ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC;
140549011e0cSOleksij Rempel 	}
140649011e0cSOleksij Rempel }
140749011e0cSOleksij Rempel 
140849011e0cSOleksij Rempel static bool ksz886x_cable_test_failed(u16 status)
140949011e0cSOleksij Rempel {
141049011e0cSOleksij Rempel 	return FIELD_GET(KSZ8081_LMD_STAT_MASK, status) ==
141149011e0cSOleksij Rempel 		KSZ8081_LMD_STAT_FAIL;
141249011e0cSOleksij Rempel }
141349011e0cSOleksij Rempel 
141449011e0cSOleksij Rempel static bool ksz886x_cable_test_fault_length_valid(u16 status)
141549011e0cSOleksij Rempel {
141649011e0cSOleksij Rempel 	switch (FIELD_GET(KSZ8081_LMD_STAT_MASK, status)) {
141749011e0cSOleksij Rempel 	case KSZ8081_LMD_STAT_OPEN:
141849011e0cSOleksij Rempel 		fallthrough;
141949011e0cSOleksij Rempel 	case KSZ8081_LMD_STAT_SHORT:
142049011e0cSOleksij Rempel 		return true;
142149011e0cSOleksij Rempel 	}
142249011e0cSOleksij Rempel 	return false;
142349011e0cSOleksij Rempel }
142449011e0cSOleksij Rempel 
142549011e0cSOleksij Rempel static int ksz886x_cable_test_fault_length(u16 status)
142649011e0cSOleksij Rempel {
142749011e0cSOleksij Rempel 	int dt;
142849011e0cSOleksij Rempel 
142949011e0cSOleksij Rempel 	/* According to the data sheet the distance to the fault is
143049011e0cSOleksij Rempel 	 * DELTA_TIME * 0.4 meters.
143149011e0cSOleksij Rempel 	 */
143249011e0cSOleksij Rempel 	dt = FIELD_GET(KSZ8081_LMD_DELTA_TIME_MASK, status);
143349011e0cSOleksij Rempel 
143449011e0cSOleksij Rempel 	return (dt * 400) / 10;
143549011e0cSOleksij Rempel }
143649011e0cSOleksij Rempel 
143749011e0cSOleksij Rempel static int ksz886x_cable_test_wait_for_completion(struct phy_device *phydev)
143849011e0cSOleksij Rempel {
143949011e0cSOleksij Rempel 	int val, ret;
144049011e0cSOleksij Rempel 
144149011e0cSOleksij Rempel 	ret = phy_read_poll_timeout(phydev, KSZ8081_LMD, val,
144249011e0cSOleksij Rempel 				    !(val & KSZ8081_LMD_ENABLE_TEST),
144349011e0cSOleksij Rempel 				    30000, 100000, true);
144449011e0cSOleksij Rempel 
144549011e0cSOleksij Rempel 	return ret < 0 ? ret : 0;
144649011e0cSOleksij Rempel }
144749011e0cSOleksij Rempel 
144849011e0cSOleksij Rempel static int ksz886x_cable_test_one_pair(struct phy_device *phydev, int pair)
144949011e0cSOleksij Rempel {
145049011e0cSOleksij Rempel 	static const int ethtool_pair[] = {
145149011e0cSOleksij Rempel 		ETHTOOL_A_CABLE_PAIR_A,
145249011e0cSOleksij Rempel 		ETHTOOL_A_CABLE_PAIR_B,
145349011e0cSOleksij Rempel 	};
145449011e0cSOleksij Rempel 	int ret, val, mdix;
145549011e0cSOleksij Rempel 
145649011e0cSOleksij Rempel 	/* There is no way to choice the pair, like we do one ksz9031.
145749011e0cSOleksij Rempel 	 * We can workaround this limitation by using the MDI-X functionality.
145849011e0cSOleksij Rempel 	 */
145949011e0cSOleksij Rempel 	if (pair == 0)
146049011e0cSOleksij Rempel 		mdix = ETH_TP_MDI;
146149011e0cSOleksij Rempel 	else
146249011e0cSOleksij Rempel 		mdix = ETH_TP_MDI_X;
146349011e0cSOleksij Rempel 
146449011e0cSOleksij Rempel 	switch (phydev->phy_id & MICREL_PHY_ID_MASK) {
146549011e0cSOleksij Rempel 	case PHY_ID_KSZ8081:
146649011e0cSOleksij Rempel 		ret = ksz8081_config_mdix(phydev, mdix);
146749011e0cSOleksij Rempel 		break;
146849011e0cSOleksij Rempel 	case PHY_ID_KSZ886X:
146949011e0cSOleksij Rempel 		ret = ksz886x_config_mdix(phydev, mdix);
147049011e0cSOleksij Rempel 		break;
147149011e0cSOleksij Rempel 	default:
147249011e0cSOleksij Rempel 		ret = -ENODEV;
147349011e0cSOleksij Rempel 	}
147449011e0cSOleksij Rempel 
147549011e0cSOleksij Rempel 	if (ret)
147649011e0cSOleksij Rempel 		return ret;
147749011e0cSOleksij Rempel 
147849011e0cSOleksij Rempel 	/* Now we are ready to fire. This command will send a 100ns pulse
147949011e0cSOleksij Rempel 	 * to the pair.
148049011e0cSOleksij Rempel 	 */
148149011e0cSOleksij Rempel 	ret = phy_write(phydev, KSZ8081_LMD, KSZ8081_LMD_ENABLE_TEST);
148249011e0cSOleksij Rempel 	if (ret)
148349011e0cSOleksij Rempel 		return ret;
148449011e0cSOleksij Rempel 
148549011e0cSOleksij Rempel 	ret = ksz886x_cable_test_wait_for_completion(phydev);
148649011e0cSOleksij Rempel 	if (ret)
148749011e0cSOleksij Rempel 		return ret;
148849011e0cSOleksij Rempel 
148949011e0cSOleksij Rempel 	val = phy_read(phydev, KSZ8081_LMD);
149049011e0cSOleksij Rempel 	if (val < 0)
149149011e0cSOleksij Rempel 		return val;
149249011e0cSOleksij Rempel 
149349011e0cSOleksij Rempel 	if (ksz886x_cable_test_failed(val))
149449011e0cSOleksij Rempel 		return -EAGAIN;
149549011e0cSOleksij Rempel 
149649011e0cSOleksij Rempel 	ret = ethnl_cable_test_result(phydev, ethtool_pair[pair],
149749011e0cSOleksij Rempel 				      ksz886x_cable_test_result_trans(val));
149849011e0cSOleksij Rempel 	if (ret)
149949011e0cSOleksij Rempel 		return ret;
150049011e0cSOleksij Rempel 
150149011e0cSOleksij Rempel 	if (!ksz886x_cable_test_fault_length_valid(val))
150249011e0cSOleksij Rempel 		return 0;
150349011e0cSOleksij Rempel 
150449011e0cSOleksij Rempel 	return ethnl_cable_test_fault_length(phydev, ethtool_pair[pair],
150549011e0cSOleksij Rempel 					     ksz886x_cable_test_fault_length(val));
150649011e0cSOleksij Rempel }
150749011e0cSOleksij Rempel 
150849011e0cSOleksij Rempel static int ksz886x_cable_test_get_status(struct phy_device *phydev,
150949011e0cSOleksij Rempel 					 bool *finished)
151049011e0cSOleksij Rempel {
151149011e0cSOleksij Rempel 	unsigned long pair_mask = 0x3;
151249011e0cSOleksij Rempel 	int retries = 20;
151349011e0cSOleksij Rempel 	int pair, ret;
151449011e0cSOleksij Rempel 
151549011e0cSOleksij Rempel 	*finished = false;
151649011e0cSOleksij Rempel 
151749011e0cSOleksij Rempel 	/* Try harder if link partner is active */
151849011e0cSOleksij Rempel 	while (pair_mask && retries--) {
151949011e0cSOleksij Rempel 		for_each_set_bit(pair, &pair_mask, 4) {
152049011e0cSOleksij Rempel 			ret = ksz886x_cable_test_one_pair(phydev, pair);
152149011e0cSOleksij Rempel 			if (ret == -EAGAIN)
152249011e0cSOleksij Rempel 				continue;
152349011e0cSOleksij Rempel 			if (ret < 0)
152449011e0cSOleksij Rempel 				return ret;
152549011e0cSOleksij Rempel 			clear_bit(pair, &pair_mask);
152649011e0cSOleksij Rempel 		}
152749011e0cSOleksij Rempel 		/* If link partner is in autonegotiation mode it will send 2ms
152849011e0cSOleksij Rempel 		 * of FLPs with at least 6ms of silence.
152949011e0cSOleksij Rempel 		 * Add 2ms sleep to have better chances to hit this silence.
153049011e0cSOleksij Rempel 		 */
153149011e0cSOleksij Rempel 		if (pair_mask)
153249011e0cSOleksij Rempel 			msleep(2);
153349011e0cSOleksij Rempel 	}
153449011e0cSOleksij Rempel 
153549011e0cSOleksij Rempel 	*finished = true;
153649011e0cSOleksij Rempel 
153749011e0cSOleksij Rempel 	return ret;
153849011e0cSOleksij Rempel }
153949011e0cSOleksij Rempel 
1540d5bf9071SChristian Hohnstaedt static struct phy_driver ksphy_driver[] = {
1541d5bf9071SChristian Hohnstaedt {
154251f932c4SChoi, David 	.phy_id		= PHY_ID_KS8737,
1543f893a99eSFabio Estevam 	.phy_id_mask	= MICREL_PHY_ID_MASK,
154451f932c4SChoi, David 	.name		= "Micrel KS8737",
1545dcdecdcfSHeiner Kallweit 	/* PHY_BASIC_FEATURES */
1546c6f9575cSJohan Hovold 	.driver_data	= &ks8737_type,
1547d0507009SDavid J. Choi 	.config_init	= kszphy_config_init,
1548c6f9575cSJohan Hovold 	.config_intr	= kszphy_config_intr,
154959ca4e58SIoana Ciornei 	.handle_interrupt = kszphy_handle_interrupt,
15501a5465f5SPatrice Vilchez 	.suspend	= genphy_suspend,
15511a5465f5SPatrice Vilchez 	.resume		= genphy_resume,
1552d5bf9071SChristian Hohnstaedt }, {
1553212ea99aSMarek Vasut 	.phy_id		= PHY_ID_KSZ8021,
1554212ea99aSMarek Vasut 	.phy_id_mask	= 0x00ffffff,
15557ab59dc1SDavid J. Choi 	.name		= "Micrel KSZ8021 or KSZ8031",
1556dcdecdcfSHeiner Kallweit 	/* PHY_BASIC_FEATURES */
1557e6a423a8SJohan Hovold 	.driver_data	= &ksz8021_type,
155863f44b2bSJohan Hovold 	.probe		= kszphy_probe,
1559d0e1df9cSJohan Hovold 	.config_init	= kszphy_config_init,
1560212ea99aSMarek Vasut 	.config_intr	= kszphy_config_intr,
156159ca4e58SIoana Ciornei 	.handle_interrupt = kszphy_handle_interrupt,
15622b2427d0SAndrew Lunn 	.get_sset_count = kszphy_get_sset_count,
15632b2427d0SAndrew Lunn 	.get_strings	= kszphy_get_strings,
15642b2427d0SAndrew Lunn 	.get_stats	= kszphy_get_stats,
15651a5465f5SPatrice Vilchez 	.suspend	= genphy_suspend,
15661a5465f5SPatrice Vilchez 	.resume		= genphy_resume,
1567212ea99aSMarek Vasut }, {
1568b818d1a7SHector Palacios 	.phy_id		= PHY_ID_KSZ8031,
1569b818d1a7SHector Palacios 	.phy_id_mask	= 0x00ffffff,
1570b818d1a7SHector Palacios 	.name		= "Micrel KSZ8031",
1571dcdecdcfSHeiner Kallweit 	/* PHY_BASIC_FEATURES */
1572e6a423a8SJohan Hovold 	.driver_data	= &ksz8021_type,
157363f44b2bSJohan Hovold 	.probe		= kszphy_probe,
1574d0e1df9cSJohan Hovold 	.config_init	= kszphy_config_init,
1575b818d1a7SHector Palacios 	.config_intr	= kszphy_config_intr,
157659ca4e58SIoana Ciornei 	.handle_interrupt = kszphy_handle_interrupt,
15772b2427d0SAndrew Lunn 	.get_sset_count = kszphy_get_sset_count,
15782b2427d0SAndrew Lunn 	.get_strings	= kszphy_get_strings,
15792b2427d0SAndrew Lunn 	.get_stats	= kszphy_get_stats,
15801a5465f5SPatrice Vilchez 	.suspend	= genphy_suspend,
15811a5465f5SPatrice Vilchez 	.resume		= genphy_resume,
1582b818d1a7SHector Palacios }, {
1583510d573fSMarek Vasut 	.phy_id		= PHY_ID_KSZ8041,
1584f893a99eSFabio Estevam 	.phy_id_mask	= MICREL_PHY_ID_MASK,
1585510d573fSMarek Vasut 	.name		= "Micrel KSZ8041",
1586dcdecdcfSHeiner Kallweit 	/* PHY_BASIC_FEATURES */
1587e6a423a8SJohan Hovold 	.driver_data	= &ksz8041_type,
1588e6a423a8SJohan Hovold 	.probe		= kszphy_probe,
158977501a79SPhilipp Zabel 	.config_init	= ksz8041_config_init,
159077501a79SPhilipp Zabel 	.config_aneg	= ksz8041_config_aneg,
159151f932c4SChoi, David 	.config_intr	= kszphy_config_intr,
159259ca4e58SIoana Ciornei 	.handle_interrupt = kszphy_handle_interrupt,
15932b2427d0SAndrew Lunn 	.get_sset_count = kszphy_get_sset_count,
15942b2427d0SAndrew Lunn 	.get_strings	= kszphy_get_strings,
15952b2427d0SAndrew Lunn 	.get_stats	= kszphy_get_stats,
15961a5465f5SPatrice Vilchez 	.suspend	= genphy_suspend,
15971a5465f5SPatrice Vilchez 	.resume		= genphy_resume,
1598d5bf9071SChristian Hohnstaedt }, {
15994bd7b512SSergei Shtylyov 	.phy_id		= PHY_ID_KSZ8041RNLI,
1600f893a99eSFabio Estevam 	.phy_id_mask	= MICREL_PHY_ID_MASK,
16014bd7b512SSergei Shtylyov 	.name		= "Micrel KSZ8041RNLI",
1602dcdecdcfSHeiner Kallweit 	/* PHY_BASIC_FEATURES */
1603e6a423a8SJohan Hovold 	.driver_data	= &ksz8041_type,
1604e6a423a8SJohan Hovold 	.probe		= kszphy_probe,
1605e6a423a8SJohan Hovold 	.config_init	= kszphy_config_init,
16064bd7b512SSergei Shtylyov 	.config_intr	= kszphy_config_intr,
160759ca4e58SIoana Ciornei 	.handle_interrupt = kszphy_handle_interrupt,
16082b2427d0SAndrew Lunn 	.get_sset_count = kszphy_get_sset_count,
16092b2427d0SAndrew Lunn 	.get_strings	= kszphy_get_strings,
16102b2427d0SAndrew Lunn 	.get_stats	= kszphy_get_stats,
16114bd7b512SSergei Shtylyov 	.suspend	= genphy_suspend,
16124bd7b512SSergei Shtylyov 	.resume		= genphy_resume,
16134bd7b512SSergei Shtylyov }, {
1614510d573fSMarek Vasut 	.name		= "Micrel KSZ8051",
1615dcdecdcfSHeiner Kallweit 	/* PHY_BASIC_FEATURES */
1616e6a423a8SJohan Hovold 	.driver_data	= &ksz8051_type,
1617e6a423a8SJohan Hovold 	.probe		= kszphy_probe,
161863f44b2bSJohan Hovold 	.config_init	= kszphy_config_init,
161951f932c4SChoi, David 	.config_intr	= kszphy_config_intr,
162059ca4e58SIoana Ciornei 	.handle_interrupt = kszphy_handle_interrupt,
16212b2427d0SAndrew Lunn 	.get_sset_count = kszphy_get_sset_count,
16222b2427d0SAndrew Lunn 	.get_strings	= kszphy_get_strings,
16232b2427d0SAndrew Lunn 	.get_stats	= kszphy_get_stats,
16248b95599cSMarek Vasut 	.match_phy_device = ksz8051_match_phy_device,
16251a5465f5SPatrice Vilchez 	.suspend	= genphy_suspend,
16261a5465f5SPatrice Vilchez 	.resume		= genphy_resume,
1627d5bf9071SChristian Hohnstaedt }, {
1628510d573fSMarek Vasut 	.phy_id		= PHY_ID_KSZ8001,
1629510d573fSMarek Vasut 	.name		= "Micrel KSZ8001 or KS8721",
1630ecd5a323SAlexander Stein 	.phy_id_mask	= 0x00fffffc,
1631dcdecdcfSHeiner Kallweit 	/* PHY_BASIC_FEATURES */
1632e6a423a8SJohan Hovold 	.driver_data	= &ksz8041_type,
1633e6a423a8SJohan Hovold 	.probe		= kszphy_probe,
1634e6a423a8SJohan Hovold 	.config_init	= kszphy_config_init,
163551f932c4SChoi, David 	.config_intr	= kszphy_config_intr,
163659ca4e58SIoana Ciornei 	.handle_interrupt = kszphy_handle_interrupt,
16372b2427d0SAndrew Lunn 	.get_sset_count = kszphy_get_sset_count,
16382b2427d0SAndrew Lunn 	.get_strings	= kszphy_get_strings,
16392b2427d0SAndrew Lunn 	.get_stats	= kszphy_get_stats,
16401a5465f5SPatrice Vilchez 	.suspend	= genphy_suspend,
16411a5465f5SPatrice Vilchez 	.resume		= genphy_resume,
1642d5bf9071SChristian Hohnstaedt }, {
16437ab59dc1SDavid J. Choi 	.phy_id		= PHY_ID_KSZ8081,
16447ab59dc1SDavid J. Choi 	.name		= "Micrel KSZ8081 or KSZ8091",
1645f893a99eSFabio Estevam 	.phy_id_mask	= MICREL_PHY_ID_MASK,
164649011e0cSOleksij Rempel 	.flags		= PHY_POLL_CABLE_TEST,
1647dcdecdcfSHeiner Kallweit 	/* PHY_BASIC_FEATURES */
1648e6a423a8SJohan Hovold 	.driver_data	= &ksz8081_type,
1649e6a423a8SJohan Hovold 	.probe		= kszphy_probe,
16507a1d8390SAntoine Tenart 	.config_init	= ksz8081_config_init,
1651764d31caSChristian Melki 	.soft_reset	= genphy_soft_reset,
1652f873f112SOleksij Rempel 	.config_aneg	= ksz8081_config_aneg,
1653f873f112SOleksij Rempel 	.read_status	= ksz8081_read_status,
16547ab59dc1SDavid J. Choi 	.config_intr	= kszphy_config_intr,
165559ca4e58SIoana Ciornei 	.handle_interrupt = kszphy_handle_interrupt,
16562b2427d0SAndrew Lunn 	.get_sset_count = kszphy_get_sset_count,
16572b2427d0SAndrew Lunn 	.get_strings	= kszphy_get_strings,
16582b2427d0SAndrew Lunn 	.get_stats	= kszphy_get_stats,
1659836384d2SWenyou Yang 	.suspend	= kszphy_suspend,
1660f5aba91dSAlexandre Belloni 	.resume		= kszphy_resume,
166149011e0cSOleksij Rempel 	.cable_test_start	= ksz886x_cable_test_start,
166249011e0cSOleksij Rempel 	.cable_test_get_status	= ksz886x_cable_test_get_status,
16637ab59dc1SDavid J. Choi }, {
16647ab59dc1SDavid J. Choi 	.phy_id		= PHY_ID_KSZ8061,
16657ab59dc1SDavid J. Choi 	.name		= "Micrel KSZ8061",
1666f893a99eSFabio Estevam 	.phy_id_mask	= MICREL_PHY_ID_MASK,
1667dcdecdcfSHeiner Kallweit 	/* PHY_BASIC_FEATURES */
1668232ba3a5SRajasingh Thavamani 	.config_init	= ksz8061_config_init,
16697ab59dc1SDavid J. Choi 	.config_intr	= kszphy_config_intr,
167059ca4e58SIoana Ciornei 	.handle_interrupt = kszphy_handle_interrupt,
16711a5465f5SPatrice Vilchez 	.suspend	= genphy_suspend,
16721a5465f5SPatrice Vilchez 	.resume		= genphy_resume,
16737ab59dc1SDavid J. Choi }, {
1674d0507009SDavid J. Choi 	.phy_id		= PHY_ID_KSZ9021,
167548d7d0adSJason Wang 	.phy_id_mask	= 0x000ffffe,
1676d0507009SDavid J. Choi 	.name		= "Micrel KSZ9021 Gigabit PHY",
1677dcdecdcfSHeiner Kallweit 	/* PHY_GBIT_FEATURES */
1678c6f9575cSJohan Hovold 	.driver_data	= &ksz9021_type,
1679bfe72442SGrygorii Strashko 	.probe		= kszphy_probe,
1680407d8098SHans Andersson 	.get_features	= ksz9031_get_features,
1681954c3967SSean Cross 	.config_init	= ksz9021_config_init,
1682c6f9575cSJohan Hovold 	.config_intr	= kszphy_config_intr,
168359ca4e58SIoana Ciornei 	.handle_interrupt = kszphy_handle_interrupt,
16842b2427d0SAndrew Lunn 	.get_sset_count = kszphy_get_sset_count,
16852b2427d0SAndrew Lunn 	.get_strings	= kszphy_get_strings,
16862b2427d0SAndrew Lunn 	.get_stats	= kszphy_get_stats,
16871a5465f5SPatrice Vilchez 	.suspend	= genphy_suspend,
16881a5465f5SPatrice Vilchez 	.resume		= genphy_resume,
1689c846a2b7SKevin Hao 	.read_mmd	= genphy_read_mmd_unsupported,
1690c846a2b7SKevin Hao 	.write_mmd	= genphy_write_mmd_unsupported,
169193272e07SJean-Christophe PLAGNIOL-VILLARD }, {
16927ab59dc1SDavid J. Choi 	.phy_id		= PHY_ID_KSZ9031,
1693f893a99eSFabio Estevam 	.phy_id_mask	= MICREL_PHY_ID_MASK,
16947ab59dc1SDavid J. Choi 	.name		= "Micrel KSZ9031 Gigabit PHY",
1695c6f9575cSJohan Hovold 	.driver_data	= &ksz9021_type,
1696bfe72442SGrygorii Strashko 	.probe		= kszphy_probe,
16973aed3e2aSAntoine Tenart 	.get_features	= ksz9031_get_features,
16986e4b8273SHubert Chaumette 	.config_init	= ksz9031_config_init,
16991d16073aSHeiner Kallweit 	.soft_reset	= genphy_soft_reset,
1700d2fd719bSNathan Sullivan 	.read_status	= ksz9031_read_status,
1701c6f9575cSJohan Hovold 	.config_intr	= kszphy_config_intr,
170259ca4e58SIoana Ciornei 	.handle_interrupt = kszphy_handle_interrupt,
17032b2427d0SAndrew Lunn 	.get_sset_count = kszphy_get_sset_count,
17042b2427d0SAndrew Lunn 	.get_strings	= kszphy_get_strings,
17052b2427d0SAndrew Lunn 	.get_stats	= kszphy_get_stats,
17061a5465f5SPatrice Vilchez 	.suspend	= genphy_suspend,
1707f64f1482SXander Huff 	.resume		= kszphy_resume,
17087ab59dc1SDavid J. Choi }, {
17091623ad8eSDivya Koppera 	.phy_id		= PHY_ID_LAN8814,
17101623ad8eSDivya Koppera 	.phy_id_mask	= MICREL_PHY_ID_MASK,
17111623ad8eSDivya Koppera 	.name		= "Microchip INDY Gigabit Quad PHY",
17121623ad8eSDivya Koppera 	.driver_data	= &ksz9021_type,
17131623ad8eSDivya Koppera 	.probe		= kszphy_probe,
17141623ad8eSDivya Koppera 	.soft_reset	= genphy_soft_reset,
17151623ad8eSDivya Koppera 	.read_status	= ksz9031_read_status,
17161623ad8eSDivya Koppera 	.get_sset_count	= kszphy_get_sset_count,
17171623ad8eSDivya Koppera 	.get_strings	= kszphy_get_strings,
17181623ad8eSDivya Koppera 	.get_stats	= kszphy_get_stats,
17191623ad8eSDivya Koppera 	.suspend	= genphy_suspend,
17201623ad8eSDivya Koppera 	.resume		= kszphy_resume,
17211623ad8eSDivya Koppera }, {
1722bff5b4b3SYuiko Oshino 	.phy_id		= PHY_ID_KSZ9131,
1723bff5b4b3SYuiko Oshino 	.phy_id_mask	= MICREL_PHY_ID_MASK,
1724bff5b4b3SYuiko Oshino 	.name		= "Microchip KSZ9131 Gigabit PHY",
1725dcdecdcfSHeiner Kallweit 	/* PHY_GBIT_FEATURES */
1726bff5b4b3SYuiko Oshino 	.driver_data	= &ksz9021_type,
1727bff5b4b3SYuiko Oshino 	.probe		= kszphy_probe,
1728bff5b4b3SYuiko Oshino 	.config_init	= ksz9131_config_init,
1729bff5b4b3SYuiko Oshino 	.config_intr	= kszphy_config_intr,
173059ca4e58SIoana Ciornei 	.handle_interrupt = kszphy_handle_interrupt,
1731bff5b4b3SYuiko Oshino 	.get_sset_count = kszphy_get_sset_count,
1732bff5b4b3SYuiko Oshino 	.get_strings	= kszphy_get_strings,
1733bff5b4b3SYuiko Oshino 	.get_stats	= kszphy_get_stats,
1734bff5b4b3SYuiko Oshino 	.suspend	= genphy_suspend,
1735bff5b4b3SYuiko Oshino 	.resume		= kszphy_resume,
1736bff5b4b3SYuiko Oshino }, {
173793272e07SJean-Christophe PLAGNIOL-VILLARD 	.phy_id		= PHY_ID_KSZ8873MLL,
1738f893a99eSFabio Estevam 	.phy_id_mask	= MICREL_PHY_ID_MASK,
173993272e07SJean-Christophe PLAGNIOL-VILLARD 	.name		= "Micrel KSZ8873MLL Switch",
1740dcdecdcfSHeiner Kallweit 	/* PHY_BASIC_FEATURES */
174193272e07SJean-Christophe PLAGNIOL-VILLARD 	.config_init	= kszphy_config_init,
174293272e07SJean-Christophe PLAGNIOL-VILLARD 	.config_aneg	= ksz8873mll_config_aneg,
174393272e07SJean-Christophe PLAGNIOL-VILLARD 	.read_status	= ksz8873mll_read_status,
17441a5465f5SPatrice Vilchez 	.suspend	= genphy_suspend,
17451a5465f5SPatrice Vilchez 	.resume		= genphy_resume,
17467ab59dc1SDavid J. Choi }, {
17477ab59dc1SDavid J. Choi 	.phy_id		= PHY_ID_KSZ886X,
1748f893a99eSFabio Estevam 	.phy_id_mask	= MICREL_PHY_ID_MASK,
1749ab36a3a2SMarek Vasut 	.name		= "Micrel KSZ8851 Ethernet MAC or KSZ886X Switch",
1750dcdecdcfSHeiner Kallweit 	/* PHY_BASIC_FEATURES */
175149011e0cSOleksij Rempel 	.flags		= PHY_POLL_CABLE_TEST,
17527ab59dc1SDavid J. Choi 	.config_init	= kszphy_config_init,
175352939393SOleksij Rempel 	.config_aneg	= ksz886x_config_aneg,
175452939393SOleksij Rempel 	.read_status	= ksz886x_read_status,
17551a5465f5SPatrice Vilchez 	.suspend	= genphy_suspend,
17561a5465f5SPatrice Vilchez 	.resume		= genphy_resume,
175749011e0cSOleksij Rempel 	.cable_test_start	= ksz886x_cable_test_start,
175849011e0cSOleksij Rempel 	.cable_test_get_status	= ksz886x_cable_test_get_status,
17599d162ed6SSean Nyekjaer }, {
17601d951ba3SMarek Vasut 	.name		= "Micrel KSZ87XX Switch",
1761dcdecdcfSHeiner Kallweit 	/* PHY_BASIC_FEATURES */
17629d162ed6SSean Nyekjaer 	.config_init	= kszphy_config_init,
17639d162ed6SSean Nyekjaer 	.config_aneg	= ksz8873mll_config_aneg,
17649d162ed6SSean Nyekjaer 	.read_status	= ksz8873mll_read_status,
17658b95599cSMarek Vasut 	.match_phy_device = ksz8795_match_phy_device,
17669d162ed6SSean Nyekjaer 	.suspend	= genphy_suspend,
17679d162ed6SSean Nyekjaer 	.resume		= genphy_resume,
1768fc3973a1SWoojung Huh }, {
1769fc3973a1SWoojung Huh 	.phy_id		= PHY_ID_KSZ9477,
1770fc3973a1SWoojung Huh 	.phy_id_mask	= MICREL_PHY_ID_MASK,
1771fc3973a1SWoojung Huh 	.name		= "Microchip KSZ9477",
1772dcdecdcfSHeiner Kallweit 	/* PHY_GBIT_FEATURES */
1773fc3973a1SWoojung Huh 	.config_init	= kszphy_config_init,
1774fc3973a1SWoojung Huh 	.suspend	= genphy_suspend,
1775fc3973a1SWoojung Huh 	.resume		= genphy_resume,
1776d5bf9071SChristian Hohnstaedt } };
1777d0507009SDavid J. Choi 
177850fd7150SJohan Hovold module_phy_driver(ksphy_driver);
1779d0507009SDavid J. Choi 
1780d0507009SDavid J. Choi MODULE_DESCRIPTION("Micrel PHY driver");
1781d0507009SDavid J. Choi MODULE_AUTHOR("David J. Choi");
1782d0507009SDavid J. Choi MODULE_LICENSE("GPL");
178352a60ed2SDavid S. Miller 
1784cf93c945SUwe Kleine-König static struct mdio_device_id __maybe_unused micrel_tbl[] = {
178548d7d0adSJason Wang 	{ PHY_ID_KSZ9021, 0x000ffffe },
1786f893a99eSFabio Estevam 	{ PHY_ID_KSZ9031, MICREL_PHY_ID_MASK },
1787bff5b4b3SYuiko Oshino 	{ PHY_ID_KSZ9131, MICREL_PHY_ID_MASK },
1788ecd5a323SAlexander Stein 	{ PHY_ID_KSZ8001, 0x00fffffc },
1789f893a99eSFabio Estevam 	{ PHY_ID_KS8737, MICREL_PHY_ID_MASK },
1790212ea99aSMarek Vasut 	{ PHY_ID_KSZ8021, 0x00ffffff },
1791b818d1a7SHector Palacios 	{ PHY_ID_KSZ8031, 0x00ffffff },
1792f893a99eSFabio Estevam 	{ PHY_ID_KSZ8041, MICREL_PHY_ID_MASK },
1793f893a99eSFabio Estevam 	{ PHY_ID_KSZ8051, MICREL_PHY_ID_MASK },
1794f893a99eSFabio Estevam 	{ PHY_ID_KSZ8061, MICREL_PHY_ID_MASK },
1795f893a99eSFabio Estevam 	{ PHY_ID_KSZ8081, MICREL_PHY_ID_MASK },
1796f893a99eSFabio Estevam 	{ PHY_ID_KSZ8873MLL, MICREL_PHY_ID_MASK },
1797f893a99eSFabio Estevam 	{ PHY_ID_KSZ886X, MICREL_PHY_ID_MASK },
17981623ad8eSDivya Koppera 	{ PHY_ID_LAN8814, MICREL_PHY_ID_MASK },
179952a60ed2SDavid S. Miller 	{ }
180052a60ed2SDavid S. Miller };
180152a60ed2SDavid S. Miller 
180252a60ed2SDavid S. Miller MODULE_DEVICE_TABLE(mdio, micrel_tbl);
1803