xref: /linux/drivers/net/phy/icplus.c (revision 4e9d9d1f)
1a2443fd1SAndrew Lunn // SPDX-License-Identifier: GPL-2.0+
20cefeebaSMichael Barkowski /*
30cefeebaSMichael Barkowski  * Driver for ICPlus PHYs
40cefeebaSMichael Barkowski  *
50cefeebaSMichael Barkowski  * Copyright (c) 2007 Freescale Semiconductor, Inc.
60cefeebaSMichael Barkowski  */
70cefeebaSMichael Barkowski #include <linux/kernel.h>
80cefeebaSMichael Barkowski #include <linux/string.h>
90cefeebaSMichael Barkowski #include <linux/errno.h>
100cefeebaSMichael Barkowski #include <linux/unistd.h>
110cefeebaSMichael Barkowski #include <linux/interrupt.h>
120cefeebaSMichael Barkowski #include <linux/init.h>
130cefeebaSMichael Barkowski #include <linux/delay.h>
140cefeebaSMichael Barkowski #include <linux/netdevice.h>
150cefeebaSMichael Barkowski #include <linux/etherdevice.h>
160cefeebaSMichael Barkowski #include <linux/skbuff.h>
170cefeebaSMichael Barkowski #include <linux/spinlock.h>
180cefeebaSMichael Barkowski #include <linux/mm.h>
190cefeebaSMichael Barkowski #include <linux/module.h>
200cefeebaSMichael Barkowski #include <linux/mii.h>
210cefeebaSMichael Barkowski #include <linux/ethtool.h>
220cefeebaSMichael Barkowski #include <linux/phy.h>
23f2f1a847SMartin Blumenstingl #include <linux/property.h>
240cefeebaSMichael Barkowski 
250cefeebaSMichael Barkowski #include <asm/io.h>
260cefeebaSMichael Barkowski #include <asm/irq.h>
277c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
280cefeebaSMichael Barkowski 
29e3e09f26SGiuseppe CAVALLARO MODULE_DESCRIPTION("ICPlus IP175C/IP101A/IP101G/IC1001 PHY drivers");
300cefeebaSMichael Barkowski MODULE_AUTHOR("Michael Barkowski");
310cefeebaSMichael Barkowski MODULE_LICENSE("GPL");
320cefeebaSMichael Barkowski 
33e3e09f26SGiuseppe CAVALLARO /* IP101A/G - IP1001 */
349c9b1f24SGiuseppe CAVALLARO #define IP10XX_SPEC_CTRL_STATUS		16	/* Spec. Control Register */
35ee336140SMartin Blumenstingl #define IP1001_RXPHASE_SEL		BIT(0)	/* Add delay on RX_CLK */
36ee336140SMartin Blumenstingl #define IP1001_TXPHASE_SEL		BIT(1)	/* Add delay on TX_CLK */
379c9b1f24SGiuseppe CAVALLARO #define IP1001_SPEC_CTRL_STATUS_2	20	/* IP1001 Spec. Control Reg 2 */
389c9b1f24SGiuseppe CAVALLARO #define IP1001_APS_ON			11	/* IP1001 APS Mode  bit */
39ee336140SMartin Blumenstingl #define IP101A_G_APS_ON			BIT(1)	/* IP101A/G APS Mode bit */
4032ab60e5SMichael Walle #define IP101A_G_AUTO_MDIX_DIS		BIT(11)
41996f7393SGiuseppe CAVALLARO #define IP101A_G_IRQ_CONF_STATUS	0x11	/* Conf Info IRQ & Status Reg */
42ba2f55b0SHeiner Kallweit #define	IP101A_G_IRQ_PIN_USED		BIT(15) /* INTR pin used */
43a872c388SMartin Blumenstingl #define IP101A_G_IRQ_ALL_MASK		BIT(11) /* IRQ's inactive */
44f7e290fbSMartin Blumenstingl #define IP101A_G_IRQ_SPEED_CHANGE	BIT(2)
45f7e290fbSMartin Blumenstingl #define IP101A_G_IRQ_DUPLEX_CHANGE	BIT(1)
46f7e290fbSMartin Blumenstingl #define IP101A_G_IRQ_LINK_CHANGE	BIT(0)
4732ab60e5SMichael Walle #define IP101A_G_PHY_STATUS		18
4832ab60e5SMichael Walle #define IP101A_G_MDIX			BIT(9)
4932ab60e5SMichael Walle #define IP101A_G_PHY_SPEC_CTRL		30
5032ab60e5SMichael Walle #define IP101A_G_FORCE_MDIX		BIT(3)
519c9b1f24SGiuseppe CAVALLARO 
52675115bfSMichael Walle #define IP101G_PAGE_CONTROL				0x14
53675115bfSMichael Walle #define IP101G_PAGE_CONTROL_MASK			GENMASK(4, 0)
54f2f1a847SMartin Blumenstingl #define IP101G_DIGITAL_IO_SPEC_CTRL			0x1d
55f2f1a847SMartin Blumenstingl #define IP101G_DIGITAL_IO_SPEC_CTRL_SEL_INTR32		BIT(2)
56f2f1a847SMartin Blumenstingl 
57f9bc51e6SMichael Walle #define IP101G_DEFAULT_PAGE			16
58f9bc51e6SMichael Walle 
59a0750d42SMichael Walle #define IP101G_P1_CNT_CTRL		17
60a0750d42SMichael Walle #define CNT_CTRL_RX_EN			BIT(13)
61a0750d42SMichael Walle #define IP101G_P8_CNT_CTRL		17
62a0750d42SMichael Walle #define CNT_CTRL_RDCLR_EN		BIT(15)
63a0750d42SMichael Walle #define IP101G_CNT_REG			18
64a0750d42SMichael Walle 
652ad4758cSMichael Walle #define IP175C_PHY_ID 0x02430d80
662ad4758cSMichael Walle #define IP1001_PHY_ID 0x02430d90
672ad4758cSMichael Walle #define IP101A_PHY_ID 0x02430c54
682ad4758cSMichael Walle 
69f2f1a847SMartin Blumenstingl /* The 32-pin IP101GR package can re-configure the mode of the RXER/INTR_32 pin
70f2f1a847SMartin Blumenstingl  * (pin number 21). The hardware default is RXER (receive error) mode. But it
71f2f1a847SMartin Blumenstingl  * can be configured to interrupt mode manually.
72f2f1a847SMartin Blumenstingl  */
73f2f1a847SMartin Blumenstingl enum ip101gr_sel_intr32 {
74f2f1a847SMartin Blumenstingl 	IP101GR_SEL_INTR32_KEEP,
75f2f1a847SMartin Blumenstingl 	IP101GR_SEL_INTR32_INTR,
76f2f1a847SMartin Blumenstingl 	IP101GR_SEL_INTR32_RXER,
77f2f1a847SMartin Blumenstingl };
78f2f1a847SMartin Blumenstingl 
79a0750d42SMichael Walle struct ip101g_hw_stat {
80a0750d42SMichael Walle 	const char *name;
81a0750d42SMichael Walle 	int page;
82a0750d42SMichael Walle };
83a0750d42SMichael Walle 
84a0750d42SMichael Walle static struct ip101g_hw_stat ip101g_hw_stats[] = {
85a0750d42SMichael Walle 	{ "phy_crc_errors", 1 },
86a0750d42SMichael Walle 	{ "phy_symbol_errors", 11, },
87a0750d42SMichael Walle };
88a0750d42SMichael Walle 
89f2f1a847SMartin Blumenstingl struct ip101a_g_phy_priv {
90f2f1a847SMartin Blumenstingl 	enum ip101gr_sel_intr32 sel_intr32;
91a0750d42SMichael Walle 	u64 stats[ARRAY_SIZE(ip101g_hw_stats)];
92f2f1a847SMartin Blumenstingl };
93f2f1a847SMartin Blumenstingl 
ip175c_config_init(struct phy_device * phydev)940cefeebaSMichael Barkowski static int ip175c_config_init(struct phy_device *phydev)
950cefeebaSMichael Barkowski {
960cefeebaSMichael Barkowski 	int err, i;
979ed66cb5SFlorian Fainelli 	static int full_reset_performed;
980cefeebaSMichael Barkowski 
990cefeebaSMichael Barkowski 	if (full_reset_performed == 0) {
1000cefeebaSMichael Barkowski 
1010cefeebaSMichael Barkowski 		/* master reset */
102e5a03bfdSAndrew Lunn 		err = mdiobus_write(phydev->mdio.bus, 30, 0, 0x175c);
1030cefeebaSMichael Barkowski 		if (err < 0)
1040cefeebaSMichael Barkowski 			return err;
1050cefeebaSMichael Barkowski 
1060cefeebaSMichael Barkowski 		/* ensure no bus delays overlap reset period */
107e5a03bfdSAndrew Lunn 		err = mdiobus_read(phydev->mdio.bus, 30, 0);
1080cefeebaSMichael Barkowski 
1090cefeebaSMichael Barkowski 		/* data sheet specifies reset period is 2 msec */
1100cefeebaSMichael Barkowski 		mdelay(2);
1110cefeebaSMichael Barkowski 
1120cefeebaSMichael Barkowski 		/* enable IP175C mode */
113e5a03bfdSAndrew Lunn 		err = mdiobus_write(phydev->mdio.bus, 29, 31, 0x175c);
1140cefeebaSMichael Barkowski 		if (err < 0)
1150cefeebaSMichael Barkowski 			return err;
1160cefeebaSMichael Barkowski 
1170cefeebaSMichael Barkowski 		/* Set MII0 speed and duplex (in PHY mode) */
118e5a03bfdSAndrew Lunn 		err = mdiobus_write(phydev->mdio.bus, 29, 22, 0x420);
1190cefeebaSMichael Barkowski 		if (err < 0)
1200cefeebaSMichael Barkowski 			return err;
1210cefeebaSMichael Barkowski 
1220cefeebaSMichael Barkowski 		/* reset switch ports */
1230cefeebaSMichael Barkowski 		for (i = 0; i < 5; i++) {
124e5a03bfdSAndrew Lunn 			err = mdiobus_write(phydev->mdio.bus, i,
1250cefeebaSMichael Barkowski 					    MII_BMCR, BMCR_RESET);
1260cefeebaSMichael Barkowski 			if (err < 0)
1270cefeebaSMichael Barkowski 				return err;
1280cefeebaSMichael Barkowski 		}
1290cefeebaSMichael Barkowski 
1300cefeebaSMichael Barkowski 		for (i = 0; i < 5; i++)
131e5a03bfdSAndrew Lunn 			err = mdiobus_read(phydev->mdio.bus, i, MII_BMCR);
1320cefeebaSMichael Barkowski 
1330cefeebaSMichael Barkowski 		mdelay(2);
1340cefeebaSMichael Barkowski 
1350cefeebaSMichael Barkowski 		full_reset_performed = 1;
1360cefeebaSMichael Barkowski 	}
1370cefeebaSMichael Barkowski 
138e5a03bfdSAndrew Lunn 	if (phydev->mdio.addr != 4) {
1390cefeebaSMichael Barkowski 		phydev->state = PHY_RUNNING;
1400cefeebaSMichael Barkowski 		phydev->speed = SPEED_100;
1410cefeebaSMichael Barkowski 		phydev->duplex = DUPLEX_FULL;
1420cefeebaSMichael Barkowski 		phydev->link = 1;
1430cefeebaSMichael Barkowski 		netif_carrier_on(phydev->attached_dev);
1440cefeebaSMichael Barkowski 	}
1450cefeebaSMichael Barkowski 
1460cefeebaSMichael Barkowski 	return 0;
1470cefeebaSMichael Barkowski }
1480cefeebaSMichael Barkowski 
ip1001_config_init(struct phy_device * phydev)1499c9b1f24SGiuseppe CAVALLARO static int ip1001_config_init(struct phy_device *phydev)
1509c9b1f24SGiuseppe CAVALLARO {
1519c9b1f24SGiuseppe CAVALLARO 	int c;
1529c9b1f24SGiuseppe CAVALLARO 
1539c9b1f24SGiuseppe CAVALLARO 	/* Enable Auto Power Saving mode */
1549c9b1f24SGiuseppe CAVALLARO 	c = phy_read(phydev, IP1001_SPEC_CTRL_STATUS_2);
155b8e3995aSDavid McKay 	if (c < 0)
156b8e3995aSDavid McKay 		return c;
1579c9b1f24SGiuseppe CAVALLARO 	c |= IP1001_APS_ON;
158b8e3995aSDavid McKay 	c = phy_write(phydev, IP1001_SPEC_CTRL_STATUS_2, c);
1599c9b1f24SGiuseppe CAVALLARO 	if (c < 0)
1609c9b1f24SGiuseppe CAVALLARO 		return c;
161377ecca9SGiuseppe CAVALLARO 
16232a64161SFlorian Fainelli 	if (phy_interface_is_rgmii(phydev)) {
163b4a49631SStuart Menefy 
1649c9b1f24SGiuseppe CAVALLARO 		c = phy_read(phydev, IP10XX_SPEC_CTRL_STATUS);
165b8e3995aSDavid McKay 		if (c < 0)
166b8e3995aSDavid McKay 			return c;
167b8e3995aSDavid McKay 
168b4a49631SStuart Menefy 		c &= ~(IP1001_RXPHASE_SEL | IP1001_TXPHASE_SEL);
169b4a49631SStuart Menefy 
170b4a49631SStuart Menefy 		if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID)
171b4a49631SStuart Menefy 			c |= (IP1001_RXPHASE_SEL | IP1001_TXPHASE_SEL);
172b4a49631SStuart Menefy 		else if (phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
173b4a49631SStuart Menefy 			c |= IP1001_RXPHASE_SEL;
174b4a49631SStuart Menefy 		else if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
175b4a49631SStuart Menefy 			c |= IP1001_TXPHASE_SEL;
176b4a49631SStuart Menefy 
177a4886d52SGiuseppe CAVALLARO 		c = phy_write(phydev, IP10XX_SPEC_CTRL_STATUS, c);
178b8e3995aSDavid McKay 		if (c < 0)
179b8e3995aSDavid McKay 			return c;
180a4886d52SGiuseppe CAVALLARO 	}
181377ecca9SGiuseppe CAVALLARO 
182b8e3995aSDavid McKay 	return 0;
1839c9b1f24SGiuseppe CAVALLARO }
1849c9b1f24SGiuseppe CAVALLARO 
ip175c_read_status(struct phy_device * phydev)1850cefeebaSMichael Barkowski static int ip175c_read_status(struct phy_device *phydev)
1860cefeebaSMichael Barkowski {
187e5a03bfdSAndrew Lunn 	if (phydev->mdio.addr == 4) /* WAN port */
1880cefeebaSMichael Barkowski 		genphy_read_status(phydev);
1890cefeebaSMichael Barkowski 	else
1900cefeebaSMichael Barkowski 		/* Don't need to read status for switch ports */
19193e8990cSHeiner Kallweit 		phydev->irq = PHY_MAC_INTERRUPT;
1920cefeebaSMichael Barkowski 
1930cefeebaSMichael Barkowski 	return 0;
1940cefeebaSMichael Barkowski }
1950cefeebaSMichael Barkowski 
ip175c_config_aneg(struct phy_device * phydev)1960cefeebaSMichael Barkowski static int ip175c_config_aneg(struct phy_device *phydev)
1970cefeebaSMichael Barkowski {
198e5a03bfdSAndrew Lunn 	if (phydev->mdio.addr == 4) /* WAN port */
1990cefeebaSMichael Barkowski 		genphy_config_aneg(phydev);
2000cefeebaSMichael Barkowski 
2010cefeebaSMichael Barkowski 	return 0;
2020cefeebaSMichael Barkowski }
2030cefeebaSMichael Barkowski 
ip101a_g_probe(struct phy_device * phydev)204f2f1a847SMartin Blumenstingl static int ip101a_g_probe(struct phy_device *phydev)
205f2f1a847SMartin Blumenstingl {
206f2f1a847SMartin Blumenstingl 	struct device *dev = &phydev->mdio.dev;
207f2f1a847SMartin Blumenstingl 	struct ip101a_g_phy_priv *priv;
208f2f1a847SMartin Blumenstingl 
209f2f1a847SMartin Blumenstingl 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
210f2f1a847SMartin Blumenstingl 	if (!priv)
211f2f1a847SMartin Blumenstingl 		return -ENOMEM;
212f2f1a847SMartin Blumenstingl 
213f2f1a847SMartin Blumenstingl 	/* Both functions (RX error and interrupt status) are sharing the same
214f2f1a847SMartin Blumenstingl 	 * pin on the 32-pin IP101GR, so this is an exclusive choice.
215f2f1a847SMartin Blumenstingl 	 */
216f2f1a847SMartin Blumenstingl 	if (device_property_read_bool(dev, "icplus,select-rx-error") &&
217f2f1a847SMartin Blumenstingl 	    device_property_read_bool(dev, "icplus,select-interrupt")) {
218f2f1a847SMartin Blumenstingl 		dev_err(dev,
219f2f1a847SMartin Blumenstingl 			"RXER and INTR mode cannot be selected together\n");
220f2f1a847SMartin Blumenstingl 		return -EINVAL;
221f2f1a847SMartin Blumenstingl 	}
222f2f1a847SMartin Blumenstingl 
223f2f1a847SMartin Blumenstingl 	if (device_property_read_bool(dev, "icplus,select-rx-error"))
224f2f1a847SMartin Blumenstingl 		priv->sel_intr32 = IP101GR_SEL_INTR32_RXER;
225f2f1a847SMartin Blumenstingl 	else if (device_property_read_bool(dev, "icplus,select-interrupt"))
226f2f1a847SMartin Blumenstingl 		priv->sel_intr32 = IP101GR_SEL_INTR32_INTR;
227f2f1a847SMartin Blumenstingl 	else
228f2f1a847SMartin Blumenstingl 		priv->sel_intr32 = IP101GR_SEL_INTR32_KEEP;
229f2f1a847SMartin Blumenstingl 
230f2f1a847SMartin Blumenstingl 	phydev->priv = priv;
231f2f1a847SMartin Blumenstingl 
232f2f1a847SMartin Blumenstingl 	return 0;
233f2f1a847SMartin Blumenstingl }
234f2f1a847SMartin Blumenstingl 
ip101a_g_config_intr_pin(struct phy_device * phydev)235eeac7d43SMichael Walle static int ip101a_g_config_intr_pin(struct phy_device *phydev)
236034289b2SMartin Blumenstingl {
237f2f1a847SMartin Blumenstingl 	struct ip101a_g_phy_priv *priv = phydev->priv;
238f9bc51e6SMichael Walle 	int oldpage, err = 0;
239f9bc51e6SMichael Walle 
240f9bc51e6SMichael Walle 	oldpage = phy_select_page(phydev, IP101G_DEFAULT_PAGE);
241f9bc51e6SMichael Walle 	if (oldpage < 0)
242*4e9d9d1fSDan Carpenter 		goto out;
243034289b2SMartin Blumenstingl 
244f2f1a847SMartin Blumenstingl 	/* configure the RXER/INTR_32 pin of the 32-pin IP101GR if needed: */
245f2f1a847SMartin Blumenstingl 	switch (priv->sel_intr32) {
246f2f1a847SMartin Blumenstingl 	case IP101GR_SEL_INTR32_RXER:
247f9bc51e6SMichael Walle 		err = __phy_modify(phydev, IP101G_DIGITAL_IO_SPEC_CTRL,
248f2f1a847SMartin Blumenstingl 				   IP101G_DIGITAL_IO_SPEC_CTRL_SEL_INTR32, 0);
249f2f1a847SMartin Blumenstingl 		if (err < 0)
250f9bc51e6SMichael Walle 			goto out;
251f2f1a847SMartin Blumenstingl 		break;
252f2f1a847SMartin Blumenstingl 
253f2f1a847SMartin Blumenstingl 	case IP101GR_SEL_INTR32_INTR:
254f9bc51e6SMichael Walle 		err = __phy_modify(phydev, IP101G_DIGITAL_IO_SPEC_CTRL,
255f2f1a847SMartin Blumenstingl 				   IP101G_DIGITAL_IO_SPEC_CTRL_SEL_INTR32,
256f2f1a847SMartin Blumenstingl 				   IP101G_DIGITAL_IO_SPEC_CTRL_SEL_INTR32);
257f2f1a847SMartin Blumenstingl 		if (err < 0)
258f9bc51e6SMichael Walle 			goto out;
259f2f1a847SMartin Blumenstingl 		break;
260f2f1a847SMartin Blumenstingl 
261f2f1a847SMartin Blumenstingl 	default:
262f2f1a847SMartin Blumenstingl 		/* Don't touch IP101G_DIGITAL_IO_SPEC_CTRL because it's not
263f2f1a847SMartin Blumenstingl 		 * documented on IP101A and it's not clear whether this would
264f2f1a847SMartin Blumenstingl 		 * cause problems.
265f2f1a847SMartin Blumenstingl 		 * For the 32-pin IP101GR we simply keep the SEL_INTR32
266f2f1a847SMartin Blumenstingl 		 * configuration as set by the bootloader when not configured
267f2f1a847SMartin Blumenstingl 		 * to one of the special functions.
268f2f1a847SMartin Blumenstingl 		 */
269f2f1a847SMartin Blumenstingl 		break;
270f2f1a847SMartin Blumenstingl 	}
271f2f1a847SMartin Blumenstingl 
272f9bc51e6SMichael Walle out:
273f9bc51e6SMichael Walle 	return phy_restore_page(phydev, oldpage, err);
274eeac7d43SMichael Walle }
275034289b2SMartin Blumenstingl 
ip101a_config_init(struct phy_device * phydev)276eeac7d43SMichael Walle static int ip101a_config_init(struct phy_device *phydev)
277eeac7d43SMichael Walle {
278eeac7d43SMichael Walle 	int ret;
279eeac7d43SMichael Walle 
280eeac7d43SMichael Walle 	/* Enable Auto Power Saving mode */
281eeac7d43SMichael Walle 	ret = phy_set_bits(phydev, IP10XX_SPEC_CTRL_STATUS, IP101A_G_APS_ON);
282eeac7d43SMichael Walle 	if (ret)
283eeac7d43SMichael Walle 		return ret;
284eeac7d43SMichael Walle 
285eeac7d43SMichael Walle 	return ip101a_g_config_intr_pin(phydev);
286eeac7d43SMichael Walle }
287eeac7d43SMichael Walle 
ip101g_config_init(struct phy_device * phydev)288eeac7d43SMichael Walle static int ip101g_config_init(struct phy_device *phydev)
289eeac7d43SMichael Walle {
290a0750d42SMichael Walle 	int ret;
291a0750d42SMichael Walle 
292a0750d42SMichael Walle 	/* Enable the PHY counters */
293a0750d42SMichael Walle 	ret = phy_modify_paged(phydev, 1, IP101G_P1_CNT_CTRL,
294a0750d42SMichael Walle 			       CNT_CTRL_RX_EN, CNT_CTRL_RX_EN);
295a0750d42SMichael Walle 	if (ret)
296a0750d42SMichael Walle 		return ret;
297a0750d42SMichael Walle 
298a0750d42SMichael Walle 	/* Clear error counters on read */
299a0750d42SMichael Walle 	ret = phy_modify_paged(phydev, 8, IP101G_P8_CNT_CTRL,
300a0750d42SMichael Walle 			       CNT_CTRL_RDCLR_EN, CNT_CTRL_RDCLR_EN);
301a0750d42SMichael Walle 	if (ret)
302a0750d42SMichael Walle 		return ret;
303a0750d42SMichael Walle 
304eeac7d43SMichael Walle 	return ip101a_g_config_intr_pin(phydev);
305034289b2SMartin Blumenstingl }
306034289b2SMartin Blumenstingl 
ip101a_g_read_status(struct phy_device * phydev)30732ab60e5SMichael Walle static int ip101a_g_read_status(struct phy_device *phydev)
30832ab60e5SMichael Walle {
30932ab60e5SMichael Walle 	int oldpage, ret, stat1, stat2;
31032ab60e5SMichael Walle 
31132ab60e5SMichael Walle 	ret = genphy_read_status(phydev);
31232ab60e5SMichael Walle 	if (ret)
31332ab60e5SMichael Walle 		return ret;
31432ab60e5SMichael Walle 
31532ab60e5SMichael Walle 	oldpage = phy_select_page(phydev, IP101G_DEFAULT_PAGE);
31632ab60e5SMichael Walle 	if (oldpage < 0)
317*4e9d9d1fSDan Carpenter 		goto out;
31832ab60e5SMichael Walle 
31932ab60e5SMichael Walle 	ret = __phy_read(phydev, IP10XX_SPEC_CTRL_STATUS);
32032ab60e5SMichael Walle 	if (ret < 0)
32132ab60e5SMichael Walle 		goto out;
32232ab60e5SMichael Walle 	stat1 = ret;
32332ab60e5SMichael Walle 
32432ab60e5SMichael Walle 	ret = __phy_read(phydev, IP101A_G_PHY_SPEC_CTRL);
32532ab60e5SMichael Walle 	if (ret < 0)
32632ab60e5SMichael Walle 		goto out;
32732ab60e5SMichael Walle 	stat2 = ret;
32832ab60e5SMichael Walle 
32932ab60e5SMichael Walle 	if (stat1 & IP101A_G_AUTO_MDIX_DIS) {
33032ab60e5SMichael Walle 		if (stat2 & IP101A_G_FORCE_MDIX)
33132ab60e5SMichael Walle 			phydev->mdix_ctrl = ETH_TP_MDI_X;
33232ab60e5SMichael Walle 		else
33332ab60e5SMichael Walle 			phydev->mdix_ctrl = ETH_TP_MDI;
33432ab60e5SMichael Walle 	} else {
33532ab60e5SMichael Walle 		phydev->mdix_ctrl = ETH_TP_MDI_AUTO;
33632ab60e5SMichael Walle 	}
33732ab60e5SMichael Walle 
33832ab60e5SMichael Walle 	if (stat2 & IP101A_G_MDIX)
33932ab60e5SMichael Walle 		phydev->mdix = ETH_TP_MDI_X;
34032ab60e5SMichael Walle 	else
34132ab60e5SMichael Walle 		phydev->mdix = ETH_TP_MDI;
34232ab60e5SMichael Walle 
34332ab60e5SMichael Walle 	ret = 0;
34432ab60e5SMichael Walle 
34532ab60e5SMichael Walle out:
34632ab60e5SMichael Walle 	return phy_restore_page(phydev, oldpage, ret);
34732ab60e5SMichael Walle }
34832ab60e5SMichael Walle 
ip101a_g_config_mdix(struct phy_device * phydev)34932ab60e5SMichael Walle static int ip101a_g_config_mdix(struct phy_device *phydev)
35032ab60e5SMichael Walle {
35132ab60e5SMichael Walle 	u16 ctrl = 0, ctrl2 = 0;
352*4e9d9d1fSDan Carpenter 	int oldpage;
353*4e9d9d1fSDan Carpenter 	int ret = 0;
35432ab60e5SMichael Walle 
35532ab60e5SMichael Walle 	switch (phydev->mdix_ctrl) {
35632ab60e5SMichael Walle 	case ETH_TP_MDI:
35732ab60e5SMichael Walle 		ctrl = IP101A_G_AUTO_MDIX_DIS;
35832ab60e5SMichael Walle 		break;
35932ab60e5SMichael Walle 	case ETH_TP_MDI_X:
36032ab60e5SMichael Walle 		ctrl = IP101A_G_AUTO_MDIX_DIS;
36132ab60e5SMichael Walle 		ctrl2 = IP101A_G_FORCE_MDIX;
36232ab60e5SMichael Walle 		break;
36332ab60e5SMichael Walle 	case ETH_TP_MDI_AUTO:
36432ab60e5SMichael Walle 		break;
36532ab60e5SMichael Walle 	default:
36632ab60e5SMichael Walle 		return 0;
36732ab60e5SMichael Walle 	}
36832ab60e5SMichael Walle 
36932ab60e5SMichael Walle 	oldpage = phy_select_page(phydev, IP101G_DEFAULT_PAGE);
37032ab60e5SMichael Walle 	if (oldpage < 0)
371*4e9d9d1fSDan Carpenter 		goto out;
37232ab60e5SMichael Walle 
37332ab60e5SMichael Walle 	ret = __phy_modify(phydev, IP10XX_SPEC_CTRL_STATUS,
37432ab60e5SMichael Walle 			   IP101A_G_AUTO_MDIX_DIS, ctrl);
37532ab60e5SMichael Walle 	if (ret)
37632ab60e5SMichael Walle 		goto out;
37732ab60e5SMichael Walle 
37832ab60e5SMichael Walle 	ret = __phy_modify(phydev, IP101A_G_PHY_SPEC_CTRL,
37932ab60e5SMichael Walle 			   IP101A_G_FORCE_MDIX, ctrl2);
38032ab60e5SMichael Walle 
38132ab60e5SMichael Walle out:
38232ab60e5SMichael Walle 	return phy_restore_page(phydev, oldpage, ret);
38332ab60e5SMichael Walle }
38432ab60e5SMichael Walle 
ip101a_g_config_aneg(struct phy_device * phydev)38532ab60e5SMichael Walle static int ip101a_g_config_aneg(struct phy_device *phydev)
38632ab60e5SMichael Walle {
38732ab60e5SMichael Walle 	int ret;
38832ab60e5SMichael Walle 
38932ab60e5SMichael Walle 	ret = ip101a_g_config_mdix(phydev);
39032ab60e5SMichael Walle 	if (ret)
39132ab60e5SMichael Walle 		return ret;
39232ab60e5SMichael Walle 
39332ab60e5SMichael Walle 	return genphy_config_aneg(phydev);
39432ab60e5SMichael Walle }
39532ab60e5SMichael Walle 
ip101a_g_ack_interrupt(struct phy_device * phydev)39612ae7ba3SIoana Ciornei static int ip101a_g_ack_interrupt(struct phy_device *phydev)
39712ae7ba3SIoana Ciornei {
398f9bc51e6SMichael Walle 	int err;
39912ae7ba3SIoana Ciornei 
400f9bc51e6SMichael Walle 	err = phy_read_paged(phydev, IP101G_DEFAULT_PAGE,
401f9bc51e6SMichael Walle 			     IP101A_G_IRQ_CONF_STATUS);
40212ae7ba3SIoana Ciornei 	if (err < 0)
40312ae7ba3SIoana Ciornei 		return err;
40412ae7ba3SIoana Ciornei 
40512ae7ba3SIoana Ciornei 	return 0;
40612ae7ba3SIoana Ciornei }
40712ae7ba3SIoana Ciornei 
ip101a_g_config_intr(struct phy_device * phydev)408ba2f55b0SHeiner Kallweit static int ip101a_g_config_intr(struct phy_device *phydev)
409ba2f55b0SHeiner Kallweit {
410ba2f55b0SHeiner Kallweit 	u16 val;
41112ae7ba3SIoana Ciornei 	int err;
412ba2f55b0SHeiner Kallweit 
41312ae7ba3SIoana Ciornei 	if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
41412ae7ba3SIoana Ciornei 		err = ip101a_g_ack_interrupt(phydev);
41512ae7ba3SIoana Ciornei 		if (err)
41612ae7ba3SIoana Ciornei 			return err;
41712ae7ba3SIoana Ciornei 
418ba2f55b0SHeiner Kallweit 		/* INTR pin used: Speed/link/duplex will cause an interrupt */
419ba2f55b0SHeiner Kallweit 		val = IP101A_G_IRQ_PIN_USED;
420f9bc51e6SMichael Walle 		err = phy_write_paged(phydev, IP101G_DEFAULT_PAGE,
421f9bc51e6SMichael Walle 				      IP101A_G_IRQ_CONF_STATUS, val);
42212ae7ba3SIoana Ciornei 	} else {
423a872c388SMartin Blumenstingl 		val = IP101A_G_IRQ_ALL_MASK;
424f9bc51e6SMichael Walle 		err = phy_write_paged(phydev, IP101G_DEFAULT_PAGE,
425f9bc51e6SMichael Walle 				      IP101A_G_IRQ_CONF_STATUS, val);
42612ae7ba3SIoana Ciornei 		if (err)
42712ae7ba3SIoana Ciornei 			return err;
428ba2f55b0SHeiner Kallweit 
42912ae7ba3SIoana Ciornei 		err = ip101a_g_ack_interrupt(phydev);
43012ae7ba3SIoana Ciornei 	}
43112ae7ba3SIoana Ciornei 
43212ae7ba3SIoana Ciornei 	return err;
433ba2f55b0SHeiner Kallweit }
434ba2f55b0SHeiner Kallweit 
ip101a_g_handle_interrupt(struct phy_device * phydev)43525497b7fSIoana Ciornei static irqreturn_t ip101a_g_handle_interrupt(struct phy_device *phydev)
436f7e290fbSMartin Blumenstingl {
43725497b7fSIoana Ciornei 	int irq_status;
438f7e290fbSMartin Blumenstingl 
439f9bc51e6SMichael Walle 	irq_status = phy_read_paged(phydev, IP101G_DEFAULT_PAGE,
440f9bc51e6SMichael Walle 				    IP101A_G_IRQ_CONF_STATUS);
44125497b7fSIoana Ciornei 	if (irq_status < 0) {
44225497b7fSIoana Ciornei 		phy_error(phydev);
44325497b7fSIoana Ciornei 		return IRQ_NONE;
44425497b7fSIoana Ciornei 	}
445f7e290fbSMartin Blumenstingl 
44625497b7fSIoana Ciornei 	if (!(irq_status & (IP101A_G_IRQ_SPEED_CHANGE |
447f7e290fbSMartin Blumenstingl 			    IP101A_G_IRQ_DUPLEX_CHANGE |
44825497b7fSIoana Ciornei 			    IP101A_G_IRQ_LINK_CHANGE)))
44925497b7fSIoana Ciornei 		return IRQ_NONE;
45025497b7fSIoana Ciornei 
45125497b7fSIoana Ciornei 	phy_trigger_machine(phydev);
45225497b7fSIoana Ciornei 
45325497b7fSIoana Ciornei 	return IRQ_HANDLED;
454f7e290fbSMartin Blumenstingl }
455f7e290fbSMartin Blumenstingl 
456f9bc51e6SMichael Walle /* The IP101A doesn't really have a page register. We just pretend to have one
457f9bc51e6SMichael Walle  * so we can use the paged versions of the callbacks of the IP101G.
458f9bc51e6SMichael Walle  */
ip101a_read_page(struct phy_device * phydev)459f9bc51e6SMichael Walle static int ip101a_read_page(struct phy_device *phydev)
460f9bc51e6SMichael Walle {
461f9bc51e6SMichael Walle 	return IP101G_DEFAULT_PAGE;
462f9bc51e6SMichael Walle }
463f9bc51e6SMichael Walle 
ip101a_write_page(struct phy_device * phydev,int page)464f9bc51e6SMichael Walle static int ip101a_write_page(struct phy_device *phydev, int page)
465f9bc51e6SMichael Walle {
466f9bc51e6SMichael Walle 	WARN_ONCE(page != IP101G_DEFAULT_PAGE, "wrong page selected\n");
467f9bc51e6SMichael Walle 
468f9bc51e6SMichael Walle 	return 0;
469f9bc51e6SMichael Walle }
470f9bc51e6SMichael Walle 
ip101g_read_page(struct phy_device * phydev)471f9bc51e6SMichael Walle static int ip101g_read_page(struct phy_device *phydev)
472f9bc51e6SMichael Walle {
473f9bc51e6SMichael Walle 	return __phy_read(phydev, IP101G_PAGE_CONTROL);
474f9bc51e6SMichael Walle }
475f9bc51e6SMichael Walle 
ip101g_write_page(struct phy_device * phydev,int page)476f9bc51e6SMichael Walle static int ip101g_write_page(struct phy_device *phydev, int page)
477f9bc51e6SMichael Walle {
478f9bc51e6SMichael Walle 	return __phy_write(phydev, IP101G_PAGE_CONTROL, page);
479f9bc51e6SMichael Walle }
480f9bc51e6SMichael Walle 
ip101a_g_has_page_register(struct phy_device * phydev)481675115bfSMichael Walle static int ip101a_g_has_page_register(struct phy_device *phydev)
482675115bfSMichael Walle {
483675115bfSMichael Walle 	int oldval, val, ret;
484675115bfSMichael Walle 
485675115bfSMichael Walle 	oldval = phy_read(phydev, IP101G_PAGE_CONTROL);
486675115bfSMichael Walle 	if (oldval < 0)
487675115bfSMichael Walle 		return oldval;
488675115bfSMichael Walle 
489675115bfSMichael Walle 	ret = phy_write(phydev, IP101G_PAGE_CONTROL, 0xffff);
490675115bfSMichael Walle 	if (ret)
491675115bfSMichael Walle 		return ret;
492675115bfSMichael Walle 
493675115bfSMichael Walle 	val = phy_read(phydev, IP101G_PAGE_CONTROL);
494675115bfSMichael Walle 	if (val < 0)
495675115bfSMichael Walle 		return val;
496675115bfSMichael Walle 
497675115bfSMichael Walle 	ret = phy_write(phydev, IP101G_PAGE_CONTROL, oldval);
498675115bfSMichael Walle 	if (ret)
499675115bfSMichael Walle 		return ret;
500675115bfSMichael Walle 
501675115bfSMichael Walle 	return val == IP101G_PAGE_CONTROL_MASK;
502675115bfSMichael Walle }
503675115bfSMichael Walle 
ip101a_g_match_phy_device(struct phy_device * phydev,bool ip101a)504675115bfSMichael Walle static int ip101a_g_match_phy_device(struct phy_device *phydev, bool ip101a)
505675115bfSMichael Walle {
506675115bfSMichael Walle 	int ret;
507675115bfSMichael Walle 
508675115bfSMichael Walle 	if (phydev->phy_id != IP101A_PHY_ID)
509675115bfSMichael Walle 		return 0;
510675115bfSMichael Walle 
511675115bfSMichael Walle 	/* The IP101A and the IP101G share the same PHY identifier.The IP101G
512675115bfSMichael Walle 	 * seems to be a successor of the IP101A and implements more functions.
513675115bfSMichael Walle 	 * Amongst other things there is a page select register, which is not
514675115bfSMichael Walle 	 * available on the IP101A. Use this to distinguish these two.
515675115bfSMichael Walle 	 */
516675115bfSMichael Walle 	ret = ip101a_g_has_page_register(phydev);
517675115bfSMichael Walle 	if (ret < 0)
518675115bfSMichael Walle 		return ret;
519675115bfSMichael Walle 
520675115bfSMichael Walle 	return ip101a == !ret;
521675115bfSMichael Walle }
522675115bfSMichael Walle 
ip101a_match_phy_device(struct phy_device * phydev)523675115bfSMichael Walle static int ip101a_match_phy_device(struct phy_device *phydev)
524675115bfSMichael Walle {
525675115bfSMichael Walle 	return ip101a_g_match_phy_device(phydev, true);
526675115bfSMichael Walle }
527675115bfSMichael Walle 
ip101g_match_phy_device(struct phy_device * phydev)528675115bfSMichael Walle static int ip101g_match_phy_device(struct phy_device *phydev)
529675115bfSMichael Walle {
530675115bfSMichael Walle 	return ip101a_g_match_phy_device(phydev, false);
531675115bfSMichael Walle }
532675115bfSMichael Walle 
ip101g_get_sset_count(struct phy_device * phydev)533a0750d42SMichael Walle static int ip101g_get_sset_count(struct phy_device *phydev)
534a0750d42SMichael Walle {
535a0750d42SMichael Walle 	return ARRAY_SIZE(ip101g_hw_stats);
536a0750d42SMichael Walle }
537a0750d42SMichael Walle 
ip101g_get_strings(struct phy_device * phydev,u8 * data)538a0750d42SMichael Walle static void ip101g_get_strings(struct phy_device *phydev, u8 *data)
539a0750d42SMichael Walle {
540a0750d42SMichael Walle 	int i;
541a0750d42SMichael Walle 
542a0750d42SMichael Walle 	for (i = 0; i < ARRAY_SIZE(ip101g_hw_stats); i++)
543a0750d42SMichael Walle 		strscpy(data + i * ETH_GSTRING_LEN,
544a0750d42SMichael Walle 			ip101g_hw_stats[i].name, ETH_GSTRING_LEN);
545a0750d42SMichael Walle }
546a0750d42SMichael Walle 
ip101g_get_stat(struct phy_device * phydev,int i)547a0750d42SMichael Walle static u64 ip101g_get_stat(struct phy_device *phydev, int i)
548a0750d42SMichael Walle {
549a0750d42SMichael Walle 	struct ip101g_hw_stat stat = ip101g_hw_stats[i];
550a0750d42SMichael Walle 	struct ip101a_g_phy_priv *priv = phydev->priv;
551a0750d42SMichael Walle 	int val;
552a0750d42SMichael Walle 	u64 ret;
553a0750d42SMichael Walle 
554a0750d42SMichael Walle 	val = phy_read_paged(phydev, stat.page, IP101G_CNT_REG);
555a0750d42SMichael Walle 	if (val < 0) {
556a0750d42SMichael Walle 		ret = U64_MAX;
557a0750d42SMichael Walle 	} else {
558a0750d42SMichael Walle 		priv->stats[i] += val;
559a0750d42SMichael Walle 		ret = priv->stats[i];
560a0750d42SMichael Walle 	}
561a0750d42SMichael Walle 
562a0750d42SMichael Walle 	return ret;
563a0750d42SMichael Walle }
564a0750d42SMichael Walle 
ip101g_get_stats(struct phy_device * phydev,struct ethtool_stats * stats,u64 * data)565a0750d42SMichael Walle static void ip101g_get_stats(struct phy_device *phydev,
566a0750d42SMichael Walle 			     struct ethtool_stats *stats, u64 *data)
567a0750d42SMichael Walle {
568a0750d42SMichael Walle 	int i;
569a0750d42SMichael Walle 
570a0750d42SMichael Walle 	for (i = 0; i < ARRAY_SIZE(ip101g_hw_stats); i++)
571a0750d42SMichael Walle 		data[i] = ip101g_get_stat(phydev, i);
572a0750d42SMichael Walle }
573a0750d42SMichael Walle 
574d5bf9071SChristian Hohnstaedt static struct phy_driver icplus_driver[] = {
575d5bf9071SChristian Hohnstaedt {
5762ad4758cSMichael Walle 	PHY_ID_MATCH_MODEL(IP175C_PHY_ID),
5770cefeebaSMichael Barkowski 	.name		= "ICPlus IP175C",
578dcdecdcfSHeiner Kallweit 	/* PHY_BASIC_FEATURES */
5798edf206cSMichael Walle 	.config_init	= ip175c_config_init,
5808edf206cSMichael Walle 	.config_aneg	= ip175c_config_aneg,
5818edf206cSMichael Walle 	.read_status	= ip175c_read_status,
582dab10863SGiuseppe Cavallaro 	.suspend	= genphy_suspend,
583dab10863SGiuseppe Cavallaro 	.resume		= genphy_resume,
584d5bf9071SChristian Hohnstaedt }, {
5852ad4758cSMichael Walle 	PHY_ID_MATCH_MODEL(IP1001_PHY_ID),
586377ecca9SGiuseppe CAVALLARO 	.name		= "ICPlus IP1001",
587dcdecdcfSHeiner Kallweit 	/* PHY_GBIT_FEATURES */
5888edf206cSMichael Walle 	.config_init	= ip1001_config_init,
589df22de9aSMichael Walle 	.soft_reset	= genphy_soft_reset,
590377ecca9SGiuseppe CAVALLARO 	.suspend	= genphy_suspend,
591377ecca9SGiuseppe CAVALLARO 	.resume		= genphy_resume,
592d5bf9071SChristian Hohnstaedt }, {
593675115bfSMichael Walle 	.name		= "ICPlus IP101A",
594675115bfSMichael Walle 	.match_phy_device = ip101a_match_phy_device,
595675115bfSMichael Walle 	.probe		= ip101a_g_probe,
596f9bc51e6SMichael Walle 	.read_page	= ip101a_read_page,
597f9bc51e6SMichael Walle 	.write_page	= ip101a_write_page,
598675115bfSMichael Walle 	.config_intr	= ip101a_g_config_intr,
599675115bfSMichael Walle 	.handle_interrupt = ip101a_g_handle_interrupt,
600eeac7d43SMichael Walle 	.config_init	= ip101a_config_init,
60132ab60e5SMichael Walle 	.config_aneg	= ip101a_g_config_aneg,
60232ab60e5SMichael Walle 	.read_status	= ip101a_g_read_status,
603675115bfSMichael Walle 	.soft_reset	= genphy_soft_reset,
604675115bfSMichael Walle 	.suspend	= genphy_suspend,
605675115bfSMichael Walle 	.resume		= genphy_resume,
606675115bfSMichael Walle }, {
607675115bfSMichael Walle 	.name		= "ICPlus IP101G",
608675115bfSMichael Walle 	.match_phy_device = ip101g_match_phy_device,
609f2f1a847SMartin Blumenstingl 	.probe		= ip101a_g_probe,
610f9bc51e6SMichael Walle 	.read_page	= ip101g_read_page,
611f9bc51e6SMichael Walle 	.write_page	= ip101g_write_page,
612ba2f55b0SHeiner Kallweit 	.config_intr	= ip101a_g_config_intr,
61325497b7fSIoana Ciornei 	.handle_interrupt = ip101a_g_handle_interrupt,
614eeac7d43SMichael Walle 	.config_init	= ip101g_config_init,
61532ab60e5SMichael Walle 	.config_aneg	= ip101a_g_config_aneg,
61632ab60e5SMichael Walle 	.read_status	= ip101a_g_read_status,
617df22de9aSMichael Walle 	.soft_reset	= genphy_soft_reset,
618a0750d42SMichael Walle 	.get_sset_count = ip101g_get_sset_count,
619a0750d42SMichael Walle 	.get_strings	= ip101g_get_strings,
620a0750d42SMichael Walle 	.get_stats	= ip101g_get_stats,
6219c9b1f24SGiuseppe CAVALLARO 	.suspend	= genphy_suspend,
6229c9b1f24SGiuseppe CAVALLARO 	.resume		= genphy_resume,
623d5bf9071SChristian Hohnstaedt } };
6249c9b1f24SGiuseppe CAVALLARO 
62550fd7150SJohan Hovold module_phy_driver(icplus_driver);
6264e4f10f6SDavid Woodhouse 
627cf93c945SUwe Kleine-König static struct mdio_device_id __maybe_unused icplus_tbl[] = {
6282ad4758cSMichael Walle 	{ PHY_ID_MATCH_MODEL(IP175C_PHY_ID) },
6292ad4758cSMichael Walle 	{ PHY_ID_MATCH_MODEL(IP1001_PHY_ID) },
6307360a4deSMichael Walle 	{ PHY_ID_MATCH_EXACT(IP101A_PHY_ID) },
6314e4f10f6SDavid Woodhouse 	{ }
6324e4f10f6SDavid Woodhouse };
6334e4f10f6SDavid Woodhouse 
6344e4f10f6SDavid Woodhouse MODULE_DEVICE_TABLE(mdio, icplus_tbl);
635