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 */ 40*32ab60e5SMichael 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) 47*32ab60e5SMichael Walle #define IP101A_G_PHY_STATUS 18 48*32ab60e5SMichael Walle #define IP101A_G_MDIX BIT(9) 49*32ab60e5SMichael Walle #define IP101A_G_PHY_SPEC_CTRL 30 50*32ab60e5SMichael 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 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 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 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 */ 1910cefeebaSMichael Barkowski phydev->irq = PHY_IGNORE_INTERRUPT; 1920cefeebaSMichael Barkowski 1930cefeebaSMichael Barkowski return 0; 1940cefeebaSMichael Barkowski } 1950cefeebaSMichael Barkowski 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 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 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) 242f9bc51e6SMichael Walle return oldpage; 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 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 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 307*32ab60e5SMichael Walle static int ip101a_g_read_status(struct phy_device *phydev) 308*32ab60e5SMichael Walle { 309*32ab60e5SMichael Walle int oldpage, ret, stat1, stat2; 310*32ab60e5SMichael Walle 311*32ab60e5SMichael Walle ret = genphy_read_status(phydev); 312*32ab60e5SMichael Walle if (ret) 313*32ab60e5SMichael Walle return ret; 314*32ab60e5SMichael Walle 315*32ab60e5SMichael Walle oldpage = phy_select_page(phydev, IP101G_DEFAULT_PAGE); 316*32ab60e5SMichael Walle if (oldpage < 0) 317*32ab60e5SMichael Walle return oldpage; 318*32ab60e5SMichael Walle 319*32ab60e5SMichael Walle ret = __phy_read(phydev, IP10XX_SPEC_CTRL_STATUS); 320*32ab60e5SMichael Walle if (ret < 0) 321*32ab60e5SMichael Walle goto out; 322*32ab60e5SMichael Walle stat1 = ret; 323*32ab60e5SMichael Walle 324*32ab60e5SMichael Walle ret = __phy_read(phydev, IP101A_G_PHY_SPEC_CTRL); 325*32ab60e5SMichael Walle if (ret < 0) 326*32ab60e5SMichael Walle goto out; 327*32ab60e5SMichael Walle stat2 = ret; 328*32ab60e5SMichael Walle 329*32ab60e5SMichael Walle if (stat1 & IP101A_G_AUTO_MDIX_DIS) { 330*32ab60e5SMichael Walle if (stat2 & IP101A_G_FORCE_MDIX) 331*32ab60e5SMichael Walle phydev->mdix_ctrl = ETH_TP_MDI_X; 332*32ab60e5SMichael Walle else 333*32ab60e5SMichael Walle phydev->mdix_ctrl = ETH_TP_MDI; 334*32ab60e5SMichael Walle } else { 335*32ab60e5SMichael Walle phydev->mdix_ctrl = ETH_TP_MDI_AUTO; 336*32ab60e5SMichael Walle } 337*32ab60e5SMichael Walle 338*32ab60e5SMichael Walle if (stat2 & IP101A_G_MDIX) 339*32ab60e5SMichael Walle phydev->mdix = ETH_TP_MDI_X; 340*32ab60e5SMichael Walle else 341*32ab60e5SMichael Walle phydev->mdix = ETH_TP_MDI; 342*32ab60e5SMichael Walle 343*32ab60e5SMichael Walle ret = 0; 344*32ab60e5SMichael Walle 345*32ab60e5SMichael Walle out: 346*32ab60e5SMichael Walle return phy_restore_page(phydev, oldpage, ret); 347*32ab60e5SMichael Walle } 348*32ab60e5SMichael Walle 349*32ab60e5SMichael Walle static int ip101a_g_config_mdix(struct phy_device *phydev) 350*32ab60e5SMichael Walle { 351*32ab60e5SMichael Walle u16 ctrl = 0, ctrl2 = 0; 352*32ab60e5SMichael Walle int oldpage, ret; 353*32ab60e5SMichael Walle 354*32ab60e5SMichael Walle switch (phydev->mdix_ctrl) { 355*32ab60e5SMichael Walle case ETH_TP_MDI: 356*32ab60e5SMichael Walle ctrl = IP101A_G_AUTO_MDIX_DIS; 357*32ab60e5SMichael Walle break; 358*32ab60e5SMichael Walle case ETH_TP_MDI_X: 359*32ab60e5SMichael Walle ctrl = IP101A_G_AUTO_MDIX_DIS; 360*32ab60e5SMichael Walle ctrl2 = IP101A_G_FORCE_MDIX; 361*32ab60e5SMichael Walle break; 362*32ab60e5SMichael Walle case ETH_TP_MDI_AUTO: 363*32ab60e5SMichael Walle break; 364*32ab60e5SMichael Walle default: 365*32ab60e5SMichael Walle return 0; 366*32ab60e5SMichael Walle } 367*32ab60e5SMichael Walle 368*32ab60e5SMichael Walle oldpage = phy_select_page(phydev, IP101G_DEFAULT_PAGE); 369*32ab60e5SMichael Walle if (oldpage < 0) 370*32ab60e5SMichael Walle return oldpage; 371*32ab60e5SMichael Walle 372*32ab60e5SMichael Walle ret = __phy_modify(phydev, IP10XX_SPEC_CTRL_STATUS, 373*32ab60e5SMichael Walle IP101A_G_AUTO_MDIX_DIS, ctrl); 374*32ab60e5SMichael Walle if (ret) 375*32ab60e5SMichael Walle goto out; 376*32ab60e5SMichael Walle 377*32ab60e5SMichael Walle ret = __phy_modify(phydev, IP101A_G_PHY_SPEC_CTRL, 378*32ab60e5SMichael Walle IP101A_G_FORCE_MDIX, ctrl2); 379*32ab60e5SMichael Walle 380*32ab60e5SMichael Walle out: 381*32ab60e5SMichael Walle return phy_restore_page(phydev, oldpage, ret); 382*32ab60e5SMichael Walle } 383*32ab60e5SMichael Walle 384*32ab60e5SMichael Walle static int ip101a_g_config_aneg(struct phy_device *phydev) 385*32ab60e5SMichael Walle { 386*32ab60e5SMichael Walle int ret; 387*32ab60e5SMichael Walle 388*32ab60e5SMichael Walle ret = ip101a_g_config_mdix(phydev); 389*32ab60e5SMichael Walle if (ret) 390*32ab60e5SMichael Walle return ret; 391*32ab60e5SMichael Walle 392*32ab60e5SMichael Walle return genphy_config_aneg(phydev); 393*32ab60e5SMichael Walle } 394*32ab60e5SMichael Walle 39512ae7ba3SIoana Ciornei static int ip101a_g_ack_interrupt(struct phy_device *phydev) 39612ae7ba3SIoana Ciornei { 397f9bc51e6SMichael Walle int err; 39812ae7ba3SIoana Ciornei 399f9bc51e6SMichael Walle err = phy_read_paged(phydev, IP101G_DEFAULT_PAGE, 400f9bc51e6SMichael Walle IP101A_G_IRQ_CONF_STATUS); 40112ae7ba3SIoana Ciornei if (err < 0) 40212ae7ba3SIoana Ciornei return err; 40312ae7ba3SIoana Ciornei 40412ae7ba3SIoana Ciornei return 0; 40512ae7ba3SIoana Ciornei } 40612ae7ba3SIoana Ciornei 407ba2f55b0SHeiner Kallweit static int ip101a_g_config_intr(struct phy_device *phydev) 408ba2f55b0SHeiner Kallweit { 409ba2f55b0SHeiner Kallweit u16 val; 41012ae7ba3SIoana Ciornei int err; 411ba2f55b0SHeiner Kallweit 41212ae7ba3SIoana Ciornei if (phydev->interrupts == PHY_INTERRUPT_ENABLED) { 41312ae7ba3SIoana Ciornei err = ip101a_g_ack_interrupt(phydev); 41412ae7ba3SIoana Ciornei if (err) 41512ae7ba3SIoana Ciornei return err; 41612ae7ba3SIoana Ciornei 417ba2f55b0SHeiner Kallweit /* INTR pin used: Speed/link/duplex will cause an interrupt */ 418ba2f55b0SHeiner Kallweit val = IP101A_G_IRQ_PIN_USED; 419f9bc51e6SMichael Walle err = phy_write_paged(phydev, IP101G_DEFAULT_PAGE, 420f9bc51e6SMichael Walle IP101A_G_IRQ_CONF_STATUS, val); 42112ae7ba3SIoana Ciornei } else { 422a872c388SMartin Blumenstingl val = IP101A_G_IRQ_ALL_MASK; 423f9bc51e6SMichael Walle err = phy_write_paged(phydev, IP101G_DEFAULT_PAGE, 424f9bc51e6SMichael Walle IP101A_G_IRQ_CONF_STATUS, val); 42512ae7ba3SIoana Ciornei if (err) 42612ae7ba3SIoana Ciornei return err; 427ba2f55b0SHeiner Kallweit 42812ae7ba3SIoana Ciornei err = ip101a_g_ack_interrupt(phydev); 42912ae7ba3SIoana Ciornei } 43012ae7ba3SIoana Ciornei 43112ae7ba3SIoana Ciornei return err; 432ba2f55b0SHeiner Kallweit } 433ba2f55b0SHeiner Kallweit 43425497b7fSIoana Ciornei static irqreturn_t ip101a_g_handle_interrupt(struct phy_device *phydev) 435f7e290fbSMartin Blumenstingl { 43625497b7fSIoana Ciornei int irq_status; 437f7e290fbSMartin Blumenstingl 438f9bc51e6SMichael Walle irq_status = phy_read_paged(phydev, IP101G_DEFAULT_PAGE, 439f9bc51e6SMichael Walle IP101A_G_IRQ_CONF_STATUS); 44025497b7fSIoana Ciornei if (irq_status < 0) { 44125497b7fSIoana Ciornei phy_error(phydev); 44225497b7fSIoana Ciornei return IRQ_NONE; 44325497b7fSIoana Ciornei } 444f7e290fbSMartin Blumenstingl 44525497b7fSIoana Ciornei if (!(irq_status & (IP101A_G_IRQ_SPEED_CHANGE | 446f7e290fbSMartin Blumenstingl IP101A_G_IRQ_DUPLEX_CHANGE | 44725497b7fSIoana Ciornei IP101A_G_IRQ_LINK_CHANGE))) 44825497b7fSIoana Ciornei return IRQ_NONE; 44925497b7fSIoana Ciornei 45025497b7fSIoana Ciornei phy_trigger_machine(phydev); 45125497b7fSIoana Ciornei 45225497b7fSIoana Ciornei return IRQ_HANDLED; 453f7e290fbSMartin Blumenstingl } 454f7e290fbSMartin Blumenstingl 455f9bc51e6SMichael Walle /* The IP101A doesn't really have a page register. We just pretend to have one 456f9bc51e6SMichael Walle * so we can use the paged versions of the callbacks of the IP101G. 457f9bc51e6SMichael Walle */ 458f9bc51e6SMichael Walle static int ip101a_read_page(struct phy_device *phydev) 459f9bc51e6SMichael Walle { 460f9bc51e6SMichael Walle return IP101G_DEFAULT_PAGE; 461f9bc51e6SMichael Walle } 462f9bc51e6SMichael Walle 463f9bc51e6SMichael Walle static int ip101a_write_page(struct phy_device *phydev, int page) 464f9bc51e6SMichael Walle { 465f9bc51e6SMichael Walle WARN_ONCE(page != IP101G_DEFAULT_PAGE, "wrong page selected\n"); 466f9bc51e6SMichael Walle 467f9bc51e6SMichael Walle return 0; 468f9bc51e6SMichael Walle } 469f9bc51e6SMichael Walle 470f9bc51e6SMichael Walle static int ip101g_read_page(struct phy_device *phydev) 471f9bc51e6SMichael Walle { 472f9bc51e6SMichael Walle return __phy_read(phydev, IP101G_PAGE_CONTROL); 473f9bc51e6SMichael Walle } 474f9bc51e6SMichael Walle 475f9bc51e6SMichael Walle static int ip101g_write_page(struct phy_device *phydev, int page) 476f9bc51e6SMichael Walle { 477f9bc51e6SMichael Walle return __phy_write(phydev, IP101G_PAGE_CONTROL, page); 478f9bc51e6SMichael Walle } 479f9bc51e6SMichael Walle 480675115bfSMichael Walle static int ip101a_g_has_page_register(struct phy_device *phydev) 481675115bfSMichael Walle { 482675115bfSMichael Walle int oldval, val, ret; 483675115bfSMichael Walle 484675115bfSMichael Walle oldval = phy_read(phydev, IP101G_PAGE_CONTROL); 485675115bfSMichael Walle if (oldval < 0) 486675115bfSMichael Walle return oldval; 487675115bfSMichael Walle 488675115bfSMichael Walle ret = phy_write(phydev, IP101G_PAGE_CONTROL, 0xffff); 489675115bfSMichael Walle if (ret) 490675115bfSMichael Walle return ret; 491675115bfSMichael Walle 492675115bfSMichael Walle val = phy_read(phydev, IP101G_PAGE_CONTROL); 493675115bfSMichael Walle if (val < 0) 494675115bfSMichael Walle return val; 495675115bfSMichael Walle 496675115bfSMichael Walle ret = phy_write(phydev, IP101G_PAGE_CONTROL, oldval); 497675115bfSMichael Walle if (ret) 498675115bfSMichael Walle return ret; 499675115bfSMichael Walle 500675115bfSMichael Walle return val == IP101G_PAGE_CONTROL_MASK; 501675115bfSMichael Walle } 502675115bfSMichael Walle 503675115bfSMichael Walle static int ip101a_g_match_phy_device(struct phy_device *phydev, bool ip101a) 504675115bfSMichael Walle { 505675115bfSMichael Walle int ret; 506675115bfSMichael Walle 507675115bfSMichael Walle if (phydev->phy_id != IP101A_PHY_ID) 508675115bfSMichael Walle return 0; 509675115bfSMichael Walle 510675115bfSMichael Walle /* The IP101A and the IP101G share the same PHY identifier.The IP101G 511675115bfSMichael Walle * seems to be a successor of the IP101A and implements more functions. 512675115bfSMichael Walle * Amongst other things there is a page select register, which is not 513675115bfSMichael Walle * available on the IP101A. Use this to distinguish these two. 514675115bfSMichael Walle */ 515675115bfSMichael Walle ret = ip101a_g_has_page_register(phydev); 516675115bfSMichael Walle if (ret < 0) 517675115bfSMichael Walle return ret; 518675115bfSMichael Walle 519675115bfSMichael Walle return ip101a == !ret; 520675115bfSMichael Walle } 521675115bfSMichael Walle 522675115bfSMichael Walle static int ip101a_match_phy_device(struct phy_device *phydev) 523675115bfSMichael Walle { 524675115bfSMichael Walle return ip101a_g_match_phy_device(phydev, true); 525675115bfSMichael Walle } 526675115bfSMichael Walle 527675115bfSMichael Walle static int ip101g_match_phy_device(struct phy_device *phydev) 528675115bfSMichael Walle { 529675115bfSMichael Walle return ip101a_g_match_phy_device(phydev, false); 530675115bfSMichael Walle } 531675115bfSMichael Walle 532a0750d42SMichael Walle static int ip101g_get_sset_count(struct phy_device *phydev) 533a0750d42SMichael Walle { 534a0750d42SMichael Walle return ARRAY_SIZE(ip101g_hw_stats); 535a0750d42SMichael Walle } 536a0750d42SMichael Walle 537a0750d42SMichael Walle static void ip101g_get_strings(struct phy_device *phydev, u8 *data) 538a0750d42SMichael Walle { 539a0750d42SMichael Walle int i; 540a0750d42SMichael Walle 541a0750d42SMichael Walle for (i = 0; i < ARRAY_SIZE(ip101g_hw_stats); i++) 542a0750d42SMichael Walle strscpy(data + i * ETH_GSTRING_LEN, 543a0750d42SMichael Walle ip101g_hw_stats[i].name, ETH_GSTRING_LEN); 544a0750d42SMichael Walle } 545a0750d42SMichael Walle 546a0750d42SMichael Walle static u64 ip101g_get_stat(struct phy_device *phydev, int i) 547a0750d42SMichael Walle { 548a0750d42SMichael Walle struct ip101g_hw_stat stat = ip101g_hw_stats[i]; 549a0750d42SMichael Walle struct ip101a_g_phy_priv *priv = phydev->priv; 550a0750d42SMichael Walle int val; 551a0750d42SMichael Walle u64 ret; 552a0750d42SMichael Walle 553a0750d42SMichael Walle val = phy_read_paged(phydev, stat.page, IP101G_CNT_REG); 554a0750d42SMichael Walle if (val < 0) { 555a0750d42SMichael Walle ret = U64_MAX; 556a0750d42SMichael Walle } else { 557a0750d42SMichael Walle priv->stats[i] += val; 558a0750d42SMichael Walle ret = priv->stats[i]; 559a0750d42SMichael Walle } 560a0750d42SMichael Walle 561a0750d42SMichael Walle return ret; 562a0750d42SMichael Walle } 563a0750d42SMichael Walle 564a0750d42SMichael Walle static void ip101g_get_stats(struct phy_device *phydev, 565a0750d42SMichael Walle struct ethtool_stats *stats, u64 *data) 566a0750d42SMichael Walle { 567a0750d42SMichael Walle int i; 568a0750d42SMichael Walle 569a0750d42SMichael Walle for (i = 0; i < ARRAY_SIZE(ip101g_hw_stats); i++) 570a0750d42SMichael Walle data[i] = ip101g_get_stat(phydev, i); 571a0750d42SMichael Walle } 572a0750d42SMichael Walle 573d5bf9071SChristian Hohnstaedt static struct phy_driver icplus_driver[] = { 574d5bf9071SChristian Hohnstaedt { 5752ad4758cSMichael Walle PHY_ID_MATCH_MODEL(IP175C_PHY_ID), 5760cefeebaSMichael Barkowski .name = "ICPlus IP175C", 577dcdecdcfSHeiner Kallweit /* PHY_BASIC_FEATURES */ 5788edf206cSMichael Walle .config_init = ip175c_config_init, 5798edf206cSMichael Walle .config_aneg = ip175c_config_aneg, 5808edf206cSMichael Walle .read_status = ip175c_read_status, 581dab10863SGiuseppe Cavallaro .suspend = genphy_suspend, 582dab10863SGiuseppe Cavallaro .resume = genphy_resume, 583d5bf9071SChristian Hohnstaedt }, { 5842ad4758cSMichael Walle PHY_ID_MATCH_MODEL(IP1001_PHY_ID), 585377ecca9SGiuseppe CAVALLARO .name = "ICPlus IP1001", 586dcdecdcfSHeiner Kallweit /* PHY_GBIT_FEATURES */ 5878edf206cSMichael Walle .config_init = ip1001_config_init, 588df22de9aSMichael Walle .soft_reset = genphy_soft_reset, 589377ecca9SGiuseppe CAVALLARO .suspend = genphy_suspend, 590377ecca9SGiuseppe CAVALLARO .resume = genphy_resume, 591d5bf9071SChristian Hohnstaedt }, { 592675115bfSMichael Walle .name = "ICPlus IP101A", 593675115bfSMichael Walle .match_phy_device = ip101a_match_phy_device, 594675115bfSMichael Walle .probe = ip101a_g_probe, 595f9bc51e6SMichael Walle .read_page = ip101a_read_page, 596f9bc51e6SMichael Walle .write_page = ip101a_write_page, 597675115bfSMichael Walle .config_intr = ip101a_g_config_intr, 598675115bfSMichael Walle .handle_interrupt = ip101a_g_handle_interrupt, 599eeac7d43SMichael Walle .config_init = ip101a_config_init, 600*32ab60e5SMichael Walle .config_aneg = ip101a_g_config_aneg, 601*32ab60e5SMichael Walle .read_status = ip101a_g_read_status, 602675115bfSMichael Walle .soft_reset = genphy_soft_reset, 603675115bfSMichael Walle .suspend = genphy_suspend, 604675115bfSMichael Walle .resume = genphy_resume, 605675115bfSMichael Walle }, { 606675115bfSMichael Walle .name = "ICPlus IP101G", 607675115bfSMichael Walle .match_phy_device = ip101g_match_phy_device, 608f2f1a847SMartin Blumenstingl .probe = ip101a_g_probe, 609f9bc51e6SMichael Walle .read_page = ip101g_read_page, 610f9bc51e6SMichael Walle .write_page = ip101g_write_page, 611ba2f55b0SHeiner Kallweit .config_intr = ip101a_g_config_intr, 61225497b7fSIoana Ciornei .handle_interrupt = ip101a_g_handle_interrupt, 613eeac7d43SMichael Walle .config_init = ip101g_config_init, 614*32ab60e5SMichael Walle .config_aneg = ip101a_g_config_aneg, 615*32ab60e5SMichael Walle .read_status = ip101a_g_read_status, 616df22de9aSMichael Walle .soft_reset = genphy_soft_reset, 617a0750d42SMichael Walle .get_sset_count = ip101g_get_sset_count, 618a0750d42SMichael Walle .get_strings = ip101g_get_strings, 619a0750d42SMichael Walle .get_stats = ip101g_get_stats, 6209c9b1f24SGiuseppe CAVALLARO .suspend = genphy_suspend, 6219c9b1f24SGiuseppe CAVALLARO .resume = genphy_resume, 622d5bf9071SChristian Hohnstaedt } }; 6239c9b1f24SGiuseppe CAVALLARO 62450fd7150SJohan Hovold module_phy_driver(icplus_driver); 6254e4f10f6SDavid Woodhouse 626cf93c945SUwe Kleine-König static struct mdio_device_id __maybe_unused icplus_tbl[] = { 6272ad4758cSMichael Walle { PHY_ID_MATCH_MODEL(IP175C_PHY_ID) }, 6282ad4758cSMichael Walle { PHY_ID_MATCH_MODEL(IP1001_PHY_ID) }, 6297360a4deSMichael Walle { PHY_ID_MATCH_EXACT(IP101A_PHY_ID) }, 6304e4f10f6SDavid Woodhouse { } 6314e4f10f6SDavid Woodhouse }; 6324e4f10f6SDavid Woodhouse 6334e4f10f6SDavid Woodhouse MODULE_DEVICE_TABLE(mdio, icplus_tbl); 634