xref: /openbmc/linux/drivers/net/phy/icplus.c (revision 9ed66cb502e050650c319633f096a1c14b0470e2)
10cefeebaSMichael Barkowski /*
20cefeebaSMichael Barkowski  * Driver for ICPlus PHYs
30cefeebaSMichael Barkowski  *
40cefeebaSMichael Barkowski  * Copyright (c) 2007 Freescale Semiconductor, Inc.
50cefeebaSMichael Barkowski  *
60cefeebaSMichael Barkowski  * This program is free software; you can redistribute  it and/or modify it
70cefeebaSMichael Barkowski  * under  the terms of  the GNU General  Public License as published by the
80cefeebaSMichael Barkowski  * Free Software Foundation;  either version 2 of the  License, or (at your
90cefeebaSMichael Barkowski  * option) any later version.
100cefeebaSMichael Barkowski  *
110cefeebaSMichael Barkowski  */
120cefeebaSMichael Barkowski #include <linux/kernel.h>
130cefeebaSMichael Barkowski #include <linux/string.h>
140cefeebaSMichael Barkowski #include <linux/errno.h>
150cefeebaSMichael Barkowski #include <linux/unistd.h>
160cefeebaSMichael Barkowski #include <linux/interrupt.h>
170cefeebaSMichael Barkowski #include <linux/init.h>
180cefeebaSMichael Barkowski #include <linux/delay.h>
190cefeebaSMichael Barkowski #include <linux/netdevice.h>
200cefeebaSMichael Barkowski #include <linux/etherdevice.h>
210cefeebaSMichael Barkowski #include <linux/skbuff.h>
220cefeebaSMichael Barkowski #include <linux/spinlock.h>
230cefeebaSMichael Barkowski #include <linux/mm.h>
240cefeebaSMichael Barkowski #include <linux/module.h>
250cefeebaSMichael Barkowski #include <linux/mii.h>
260cefeebaSMichael Barkowski #include <linux/ethtool.h>
270cefeebaSMichael Barkowski #include <linux/phy.h>
280cefeebaSMichael Barkowski 
290cefeebaSMichael Barkowski #include <asm/io.h>
300cefeebaSMichael Barkowski #include <asm/irq.h>
310cefeebaSMichael Barkowski #include <asm/uaccess.h>
320cefeebaSMichael Barkowski 
33e3e09f26SGiuseppe CAVALLARO MODULE_DESCRIPTION("ICPlus IP175C/IP101A/IP101G/IC1001 PHY drivers");
340cefeebaSMichael Barkowski MODULE_AUTHOR("Michael Barkowski");
350cefeebaSMichael Barkowski MODULE_LICENSE("GPL");
360cefeebaSMichael Barkowski 
37e3e09f26SGiuseppe CAVALLARO /* IP101A/G - IP1001 */
389c9b1f24SGiuseppe CAVALLARO #define IP10XX_SPEC_CTRL_STATUS		16	/* Spec. Control Register */
39b4a49631SStuart Menefy #define IP1001_RXPHASE_SEL		(1<<0)	/* Add delay on RX_CLK */
40b4a49631SStuart Menefy #define IP1001_TXPHASE_SEL		(1<<1)	/* Add delay on TX_CLK */
419c9b1f24SGiuseppe CAVALLARO #define IP1001_SPEC_CTRL_STATUS_2	20	/* IP1001 Spec. Control Reg 2 */
429c9b1f24SGiuseppe CAVALLARO #define IP1001_APS_ON			11	/* IP1001 APS Mode  bit */
43e3e09f26SGiuseppe CAVALLARO #define IP101A_G_APS_ON			2	/* IP101A/G APS Mode bit */
44996f7393SGiuseppe CAVALLARO #define IP101A_G_IRQ_CONF_STATUS	0x11	/* Conf Info IRQ & Status Reg */
459ec0db71SGiuseppe CAVALLARO #define	IP101A_G_IRQ_PIN_USED		(1<<15) /* INTR pin used */
469ec0db71SGiuseppe CAVALLARO #define	IP101A_G_IRQ_DEFAULT		IP101A_G_IRQ_PIN_USED
479c9b1f24SGiuseppe CAVALLARO 
480cefeebaSMichael Barkowski static int ip175c_config_init(struct phy_device *phydev)
490cefeebaSMichael Barkowski {
500cefeebaSMichael Barkowski 	int err, i;
51*9ed66cb5SFlorian Fainelli 	static int full_reset_performed;
520cefeebaSMichael Barkowski 
530cefeebaSMichael Barkowski 	if (full_reset_performed == 0) {
540cefeebaSMichael Barkowski 
550cefeebaSMichael Barkowski 		/* master reset */
5676231e02SDavid Daney 		err = mdiobus_write(phydev->bus, 30, 0, 0x175c);
570cefeebaSMichael Barkowski 		if (err < 0)
580cefeebaSMichael Barkowski 			return err;
590cefeebaSMichael Barkowski 
600cefeebaSMichael Barkowski 		/* ensure no bus delays overlap reset period */
6176231e02SDavid Daney 		err = mdiobus_read(phydev->bus, 30, 0);
620cefeebaSMichael Barkowski 
630cefeebaSMichael Barkowski 		/* data sheet specifies reset period is 2 msec */
640cefeebaSMichael Barkowski 		mdelay(2);
650cefeebaSMichael Barkowski 
660cefeebaSMichael Barkowski 		/* enable IP175C mode */
6776231e02SDavid Daney 		err = mdiobus_write(phydev->bus, 29, 31, 0x175c);
680cefeebaSMichael Barkowski 		if (err < 0)
690cefeebaSMichael Barkowski 			return err;
700cefeebaSMichael Barkowski 
710cefeebaSMichael Barkowski 		/* Set MII0 speed and duplex (in PHY mode) */
7276231e02SDavid Daney 		err = mdiobus_write(phydev->bus, 29, 22, 0x420);
730cefeebaSMichael Barkowski 		if (err < 0)
740cefeebaSMichael Barkowski 			return err;
750cefeebaSMichael Barkowski 
760cefeebaSMichael Barkowski 		/* reset switch ports */
770cefeebaSMichael Barkowski 		for (i = 0; i < 5; i++) {
7876231e02SDavid Daney 			err = mdiobus_write(phydev->bus, i,
790cefeebaSMichael Barkowski 					    MII_BMCR, BMCR_RESET);
800cefeebaSMichael Barkowski 			if (err < 0)
810cefeebaSMichael Barkowski 				return err;
820cefeebaSMichael Barkowski 		}
830cefeebaSMichael Barkowski 
840cefeebaSMichael Barkowski 		for (i = 0; i < 5; i++)
8576231e02SDavid Daney 			err = mdiobus_read(phydev->bus, i, MII_BMCR);
860cefeebaSMichael Barkowski 
870cefeebaSMichael Barkowski 		mdelay(2);
880cefeebaSMichael Barkowski 
890cefeebaSMichael Barkowski 		full_reset_performed = 1;
900cefeebaSMichael Barkowski 	}
910cefeebaSMichael Barkowski 
920cefeebaSMichael Barkowski 	if (phydev->addr != 4) {
930cefeebaSMichael Barkowski 		phydev->state = PHY_RUNNING;
940cefeebaSMichael Barkowski 		phydev->speed = SPEED_100;
950cefeebaSMichael Barkowski 		phydev->duplex = DUPLEX_FULL;
960cefeebaSMichael Barkowski 		phydev->link = 1;
970cefeebaSMichael Barkowski 		netif_carrier_on(phydev->attached_dev);
980cefeebaSMichael Barkowski 	}
990cefeebaSMichael Barkowski 
1000cefeebaSMichael Barkowski 	return 0;
1010cefeebaSMichael Barkowski }
1020cefeebaSMichael Barkowski 
1039c9b1f24SGiuseppe CAVALLARO static int ip1xx_reset(struct phy_device *phydev)
104377ecca9SGiuseppe CAVALLARO {
105b8e3995aSDavid McKay 	int bmcr;
106377ecca9SGiuseppe CAVALLARO 
107377ecca9SGiuseppe CAVALLARO 	/* Software Reset PHY */
1089c9b1f24SGiuseppe CAVALLARO 	bmcr = phy_read(phydev, MII_BMCR);
109b8e3995aSDavid McKay 	if (bmcr < 0)
110b8e3995aSDavid McKay 		return bmcr;
1119c9b1f24SGiuseppe CAVALLARO 	bmcr |= BMCR_RESET;
112b8e3995aSDavid McKay 	bmcr = phy_write(phydev, MII_BMCR, bmcr);
113b8e3995aSDavid McKay 	if (bmcr < 0)
114b8e3995aSDavid McKay 		return bmcr;
115377ecca9SGiuseppe CAVALLARO 
116377ecca9SGiuseppe CAVALLARO 	do {
1179c9b1f24SGiuseppe CAVALLARO 		bmcr = phy_read(phydev, MII_BMCR);
118b8e3995aSDavid McKay 		if (bmcr < 0)
119b8e3995aSDavid McKay 			return bmcr;
1209c9b1f24SGiuseppe CAVALLARO 	} while (bmcr & BMCR_RESET);
1219c9b1f24SGiuseppe CAVALLARO 
122b8e3995aSDavid McKay 	return 0;
1239c9b1f24SGiuseppe CAVALLARO }
1249c9b1f24SGiuseppe CAVALLARO 
1259c9b1f24SGiuseppe CAVALLARO static int ip1001_config_init(struct phy_device *phydev)
1269c9b1f24SGiuseppe CAVALLARO {
1279c9b1f24SGiuseppe CAVALLARO 	int c;
1289c9b1f24SGiuseppe CAVALLARO 
1299c9b1f24SGiuseppe CAVALLARO 	c = ip1xx_reset(phydev);
1309c9b1f24SGiuseppe CAVALLARO 	if (c < 0)
1319c9b1f24SGiuseppe CAVALLARO 		return c;
1329c9b1f24SGiuseppe CAVALLARO 
1339c9b1f24SGiuseppe CAVALLARO 	/* Enable Auto Power Saving mode */
1349c9b1f24SGiuseppe CAVALLARO 	c = phy_read(phydev, IP1001_SPEC_CTRL_STATUS_2);
135b8e3995aSDavid McKay 	if (c < 0)
136b8e3995aSDavid McKay 		return c;
1379c9b1f24SGiuseppe CAVALLARO 	c |= IP1001_APS_ON;
138b8e3995aSDavid McKay 	c = phy_write(phydev, IP1001_SPEC_CTRL_STATUS_2, c);
1399c9b1f24SGiuseppe CAVALLARO 	if (c < 0)
1409c9b1f24SGiuseppe CAVALLARO 		return c;
141377ecca9SGiuseppe CAVALLARO 
142b4a49631SStuart Menefy 	if ((phydev->interface == PHY_INTERFACE_MODE_RGMII) ||
143b4a49631SStuart Menefy 	    (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID) ||
144b4a49631SStuart Menefy 	    (phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID) ||
145b4a49631SStuart Menefy 	    (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)) {
146b4a49631SStuart Menefy 
1479c9b1f24SGiuseppe CAVALLARO 		c = phy_read(phydev, IP10XX_SPEC_CTRL_STATUS);
148b8e3995aSDavid McKay 		if (c < 0)
149b8e3995aSDavid McKay 			return c;
150b8e3995aSDavid McKay 
151b4a49631SStuart Menefy 		c &= ~(IP1001_RXPHASE_SEL | IP1001_TXPHASE_SEL);
152b4a49631SStuart Menefy 
153b4a49631SStuart Menefy 		if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID)
154b4a49631SStuart Menefy 			c |= (IP1001_RXPHASE_SEL | IP1001_TXPHASE_SEL);
155b4a49631SStuart Menefy 		else if (phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
156b4a49631SStuart Menefy 			c |= IP1001_RXPHASE_SEL;
157b4a49631SStuart Menefy 		else if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
158b4a49631SStuart Menefy 			c |= IP1001_TXPHASE_SEL;
159b4a49631SStuart Menefy 
160a4886d52SGiuseppe CAVALLARO 		c = phy_write(phydev, IP10XX_SPEC_CTRL_STATUS, c);
161b8e3995aSDavid McKay 		if (c < 0)
162b8e3995aSDavid McKay 			return c;
163a4886d52SGiuseppe CAVALLARO 	}
164377ecca9SGiuseppe CAVALLARO 
165b8e3995aSDavid McKay 	return 0;
1669c9b1f24SGiuseppe CAVALLARO }
1679c9b1f24SGiuseppe CAVALLARO 
168e3e09f26SGiuseppe CAVALLARO static int ip101a_g_config_init(struct phy_device *phydev)
1699c9b1f24SGiuseppe CAVALLARO {
1709c9b1f24SGiuseppe CAVALLARO 	int c;
1719c9b1f24SGiuseppe CAVALLARO 
1729c9b1f24SGiuseppe CAVALLARO 	c = ip1xx_reset(phydev);
1739c9b1f24SGiuseppe CAVALLARO 	if (c < 0)
1749c9b1f24SGiuseppe CAVALLARO 		return c;
1759c9b1f24SGiuseppe CAVALLARO 
176014f2ffdSGiuseppe CAVALLARO 	/* INTR pin used: speed/link/duplex will cause an interrupt */
177014f2ffdSGiuseppe CAVALLARO 	c = phy_write(phydev, IP101A_G_IRQ_CONF_STATUS, IP101A_G_IRQ_DEFAULT);
178014f2ffdSGiuseppe CAVALLARO 	if (c < 0)
179014f2ffdSGiuseppe CAVALLARO 		return c;
180014f2ffdSGiuseppe CAVALLARO 
1819c9b1f24SGiuseppe CAVALLARO 	/* Enable Auto Power Saving mode */
1829c9b1f24SGiuseppe CAVALLARO 	c = phy_read(phydev, IP10XX_SPEC_CTRL_STATUS);
183e3e09f26SGiuseppe CAVALLARO 	c |= IP101A_G_APS_ON;
184b3300146SSrinivas Kandagatla 
185b3300146SSrinivas Kandagatla 	return phy_write(phydev, IP10XX_SPEC_CTRL_STATUS, c);
186377ecca9SGiuseppe CAVALLARO }
187377ecca9SGiuseppe CAVALLARO 
1880cefeebaSMichael Barkowski static int ip175c_read_status(struct phy_device *phydev)
1890cefeebaSMichael Barkowski {
1900cefeebaSMichael Barkowski 	if (phydev->addr == 4) /* WAN port */
1910cefeebaSMichael Barkowski 		genphy_read_status(phydev);
1920cefeebaSMichael Barkowski 	else
1930cefeebaSMichael Barkowski 		/* Don't need to read status for switch ports */
1940cefeebaSMichael Barkowski 		phydev->irq = PHY_IGNORE_INTERRUPT;
1950cefeebaSMichael Barkowski 
1960cefeebaSMichael Barkowski 	return 0;
1970cefeebaSMichael Barkowski }
1980cefeebaSMichael Barkowski 
1990cefeebaSMichael Barkowski static int ip175c_config_aneg(struct phy_device *phydev)
2000cefeebaSMichael Barkowski {
2010cefeebaSMichael Barkowski 	if (phydev->addr == 4) /* WAN port */
2020cefeebaSMichael Barkowski 		genphy_config_aneg(phydev);
2030cefeebaSMichael Barkowski 
2040cefeebaSMichael Barkowski 	return 0;
2050cefeebaSMichael Barkowski }
2060cefeebaSMichael Barkowski 
207996f7393SGiuseppe CAVALLARO static int ip101a_g_ack_interrupt(struct phy_device *phydev)
208996f7393SGiuseppe CAVALLARO {
209996f7393SGiuseppe CAVALLARO 	int err = phy_read(phydev, IP101A_G_IRQ_CONF_STATUS);
210996f7393SGiuseppe CAVALLARO 	if (err < 0)
211996f7393SGiuseppe CAVALLARO 		return err;
212996f7393SGiuseppe CAVALLARO 
213996f7393SGiuseppe CAVALLARO 	return 0;
214996f7393SGiuseppe CAVALLARO }
215996f7393SGiuseppe CAVALLARO 
216d5bf9071SChristian Hohnstaedt static struct phy_driver icplus_driver[] = {
217d5bf9071SChristian Hohnstaedt {
2180cefeebaSMichael Barkowski 	.phy_id		= 0x02430d80,
2190cefeebaSMichael Barkowski 	.name		= "ICPlus IP175C",
2200cefeebaSMichael Barkowski 	.phy_id_mask	= 0x0ffffff0,
2210cefeebaSMichael Barkowski 	.features	= PHY_BASIC_FEATURES,
2220cefeebaSMichael Barkowski 	.config_init	= &ip175c_config_init,
2230cefeebaSMichael Barkowski 	.config_aneg	= &ip175c_config_aneg,
2240cefeebaSMichael Barkowski 	.read_status	= &ip175c_read_status,
225dab10863SGiuseppe Cavallaro 	.suspend	= genphy_suspend,
226dab10863SGiuseppe Cavallaro 	.resume		= genphy_resume,
2270cefeebaSMichael Barkowski 	.driver		= { .owner = THIS_MODULE,},
228d5bf9071SChristian Hohnstaedt }, {
229377ecca9SGiuseppe CAVALLARO 	.phy_id		= 0x02430d90,
230377ecca9SGiuseppe CAVALLARO 	.name		= "ICPlus IP1001",
231377ecca9SGiuseppe CAVALLARO 	.phy_id_mask	= 0x0ffffff0,
232377ecca9SGiuseppe CAVALLARO 	.features	= PHY_GBIT_FEATURES | SUPPORTED_Pause |
233377ecca9SGiuseppe CAVALLARO 			  SUPPORTED_Asym_Pause,
234377ecca9SGiuseppe CAVALLARO 	.config_init	= &ip1001_config_init,
235377ecca9SGiuseppe CAVALLARO 	.config_aneg	= &genphy_config_aneg,
236377ecca9SGiuseppe CAVALLARO 	.read_status	= &genphy_read_status,
237377ecca9SGiuseppe CAVALLARO 	.suspend	= genphy_suspend,
238377ecca9SGiuseppe CAVALLARO 	.resume		= genphy_resume,
239377ecca9SGiuseppe CAVALLARO 	.driver		= { .owner = THIS_MODULE,},
240d5bf9071SChristian Hohnstaedt }, {
2419c9b1f24SGiuseppe CAVALLARO 	.phy_id		= 0x02430c54,
242e3e09f26SGiuseppe CAVALLARO 	.name		= "ICPlus IP101A/G",
2439c9b1f24SGiuseppe CAVALLARO 	.phy_id_mask	= 0x0ffffff0,
2449c9b1f24SGiuseppe CAVALLARO 	.features	= PHY_BASIC_FEATURES | SUPPORTED_Pause |
2459c9b1f24SGiuseppe CAVALLARO 			  SUPPORTED_Asym_Pause,
246e3e09f26SGiuseppe CAVALLARO 	.flags		= PHY_HAS_INTERRUPT,
247996f7393SGiuseppe CAVALLARO 	.ack_interrupt	= ip101a_g_ack_interrupt,
248e3e09f26SGiuseppe CAVALLARO 	.config_init	= &ip101a_g_config_init,
2499c9b1f24SGiuseppe CAVALLARO 	.config_aneg	= &genphy_config_aneg,
2509c9b1f24SGiuseppe CAVALLARO 	.read_status	= &genphy_read_status,
2519c9b1f24SGiuseppe CAVALLARO 	.suspend	= genphy_suspend,
2529c9b1f24SGiuseppe CAVALLARO 	.resume		= genphy_resume,
2539c9b1f24SGiuseppe CAVALLARO 	.driver		= { .owner = THIS_MODULE,},
254d5bf9071SChristian Hohnstaedt } };
2559c9b1f24SGiuseppe CAVALLARO 
256377ecca9SGiuseppe CAVALLARO static int __init icplus_init(void)
2570cefeebaSMichael Barkowski {
258d5bf9071SChristian Hohnstaedt 	return phy_drivers_register(icplus_driver,
259d5bf9071SChristian Hohnstaedt 		ARRAY_SIZE(icplus_driver));
2600cefeebaSMichael Barkowski }
2610cefeebaSMichael Barkowski 
262377ecca9SGiuseppe CAVALLARO static void __exit icplus_exit(void)
2630cefeebaSMichael Barkowski {
264d5bf9071SChristian Hohnstaedt 	phy_drivers_unregister(icplus_driver,
265d5bf9071SChristian Hohnstaedt 		ARRAY_SIZE(icplus_driver));
2660cefeebaSMichael Barkowski }
2670cefeebaSMichael Barkowski 
268377ecca9SGiuseppe CAVALLARO module_init(icplus_init);
269377ecca9SGiuseppe CAVALLARO module_exit(icplus_exit);
2704e4f10f6SDavid Woodhouse 
271cf93c945SUwe Kleine-König static struct mdio_device_id __maybe_unused icplus_tbl[] = {
2724e4f10f6SDavid Woodhouse 	{ 0x02430d80, 0x0ffffff0 },
273377ecca9SGiuseppe CAVALLARO 	{ 0x02430d90, 0x0ffffff0 },
274e3e09f26SGiuseppe CAVALLARO 	{ 0x02430c54, 0x0ffffff0 },
2754e4f10f6SDavid Woodhouse 	{ }
2764e4f10f6SDavid Woodhouse };
2774e4f10f6SDavid Woodhouse 
2784e4f10f6SDavid Woodhouse MODULE_DEVICE_TABLE(mdio, icplus_tbl);
279