Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / net / phy / xilinx_gmii2rgmii.c
blob151c2a3f0b3a353d0aa76d1bb0cce177bcbb595f
1 // SPDX-License-Identifier: GPL-2.0+
2 /* Xilinx GMII2RGMII Converter driver
4 * Copyright (C) 2016 Xilinx, Inc.
5 * Copyright (C) 2016 Andrew Lunn <andrew@lunn.ch>
7 * Author: Andrew Lunn <andrew@lunn.ch>
8 * Author: Kedareswara rao Appana <appanad@xilinx.com>
10 * Description:
11 * This driver is developed for Xilinx GMII2RGMII Converter
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>
20 #define XILINX_GMII2RGMII_REG 0x10
21 #define XILINX_GMII2RGMII_SPEED_MASK (BMCR_SPEED1000 | BMCR_SPEED100)
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;
30 static int xgmiitorgmii_read_status(struct phy_device *phydev)
32 struct gmii2rgmii *priv = mdiodev_get_drvdata(&phydev->mdio);
33 struct mii_bus *bus = priv->mdio->bus;
34 int addr = priv->mdio->addr;
35 u16 val = 0;
36 int err;
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;
45 val = mdiobus_read(bus, addr, XILINX_GMII2RGMII_REG);
46 val &= ~XILINX_GMII2RGMII_SPEED_MASK;
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;
55 mdiobus_write(bus, addr, XILINX_GMII2RGMII_REG, val);
57 return 0;
60 static int xgmiitorgmii_probe(struct mdio_device *mdiodev)
62 struct device *dev = &mdiodev->dev;
63 struct device_node *np = dev->of_node, *phy_node;
64 struct gmii2rgmii *priv;
66 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
67 if (!priv)
68 return -ENOMEM;
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;
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;
83 if (!priv->phy_dev->drv) {
84 dev_info(dev, "Attached phy not ready\n");
85 return -EPROBE_DEFER;
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 mdiodev_set_drvdata(&priv->phy_dev->mdio, priv);
94 priv->phy_dev->drv = &priv->conv_phy_drv;
96 return 0;
99 static const struct of_device_id xgmiitorgmii_of_match[] = {
100 { .compatible = "xlnx,gmii-to-rgmii-1.0" },
103 MODULE_DEVICE_TABLE(of, xgmiitorgmii_of_match);
105 static struct mdio_driver xgmiitorgmii_driver = {
106 .probe = xgmiitorgmii_probe,
107 .mdiodrv.driver = {
108 .name = "xgmiitorgmii",
109 .of_match_table = xgmiitorgmii_of_match,
113 mdio_module_driver(xgmiitorgmii_driver);
115 MODULE_DESCRIPTION("Xilinx GMII2RGMII converter driver");
116 MODULE_LICENSE("GPL");