1 // SPDX-License-Identifier: GPL-2.0+ 2 /* Xilinx GMII2RGMII Converter driver 3 * 4 * Copyright (C) 2016 Xilinx, Inc. 5 * Copyright (C) 2016 Andrew Lunn <andrew@lunn.ch> 6 * 7 * Author: Andrew Lunn <andrew@lunn.ch> 8 * Author: Kedareswara rao Appana <appanad@xilinx.com> 9 * 10 * Description: 11 * This driver is developed for Xilinx GMII2RGMII Converter 12 */ 13 #include <linux/module.h> 14 #include <linux/kernel.h> 15 #include <linux/mii.h> 16 #include <linux/mdio.h> 17 #include <linux/phy.h> 18 #include <linux/of_mdio.h> 19 20 #define XILINX_GMII2RGMII_REG 0x10 21 #define XILINX_GMII2RGMII_SPEED_MASK (BMCR_SPEED1000 | BMCR_SPEED100) 22 23 struct gmii2rgmii { 24 struct phy_device *phy_dev; 25 struct phy_driver *phy_drv; 26 struct phy_driver conv_phy_drv; 27 struct mdio_device *mdio; 28 }; 29 30 static int xgmiitorgmii_read_status(struct phy_device *phydev) 31 { 32 struct gmii2rgmii *priv = phydev->priv; 33 struct mii_bus *bus = priv->mdio->bus; 34 int addr = priv->mdio->addr; 35 u16 val = 0; 36 int err; 37 38 if (priv->phy_drv->read_status) 39 err = priv->phy_drv->read_status(phydev); 40 else 41 err = genphy_read_status(phydev); 42 if (err < 0) 43 return err; 44 45 val = mdiobus_read(bus, addr, XILINX_GMII2RGMII_REG); 46 val &= ~XILINX_GMII2RGMII_SPEED_MASK; 47 48 if (phydev->speed == SPEED_1000) 49 val |= BMCR_SPEED1000; 50 else if (phydev->speed == SPEED_100) 51 val |= BMCR_SPEED100; 52 else 53 val |= BMCR_SPEED10; 54 55 mdiobus_write(bus, addr, XILINX_GMII2RGMII_REG, val); 56 57 return 0; 58 } 59 60 static int xgmiitorgmii_probe(struct mdio_device *mdiodev) 61 { 62 struct device *dev = &mdiodev->dev; 63 struct device_node *np = dev->of_node, *phy_node; 64 struct gmii2rgmii *priv; 65 66 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 67 if (!priv) 68 return -ENOMEM; 69 70 phy_node = of_parse_phandle(np, "phy-handle", 0); 71 if (!phy_node) { 72 dev_err(dev, "Couldn't parse phy-handle\n"); 73 return -ENODEV; 74 } 75 76 priv->phy_dev = of_phy_find_device(phy_node); 77 of_node_put(phy_node); 78 if (!priv->phy_dev) { 79 dev_info(dev, "Couldn't find phydev\n"); 80 return -EPROBE_DEFER; 81 } 82 83 if (!priv->phy_dev->drv) { 84 dev_info(dev, "Attached phy not ready\n"); 85 return -EPROBE_DEFER; 86 } 87 88 priv->mdio = mdiodev; 89 priv->phy_drv = priv->phy_dev->drv; 90 memcpy(&priv->conv_phy_drv, priv->phy_dev->drv, 91 sizeof(struct phy_driver)); 92 priv->conv_phy_drv.read_status = xgmiitorgmii_read_status; 93 priv->phy_dev->priv = priv; 94 priv->phy_dev->drv = &priv->conv_phy_drv; 95 96 return 0; 97 } 98 99 static const struct of_device_id xgmiitorgmii_of_match[] = { 100 { .compatible = "xlnx,gmii-to-rgmii-1.0" }, 101 {}, 102 }; 103 MODULE_DEVICE_TABLE(of, xgmiitorgmii_of_match); 104 105 static struct mdio_driver xgmiitorgmii_driver = { 106 .probe = xgmiitorgmii_probe, 107 .mdiodrv.driver = { 108 .name = "xgmiitorgmii", 109 .of_match_table = xgmiitorgmii_of_match, 110 }, 111 }; 112 113 mdio_module_driver(xgmiitorgmii_driver); 114 115 MODULE_DESCRIPTION("Xilinx GMII2RGMII converter driver"); 116 MODULE_LICENSE("GPL"); 117