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>
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)
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
;
38 if (priv
->phy_drv
->read_status
)
39 err
= priv
->phy_drv
->read_status(phydev
);
41 err
= genphy_read_status(phydev
);
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
)
55 mdiobus_write(bus
, addr
, XILINX_GMII2RGMII_REG
, val
);
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
);
70 phy_node
= of_parse_phandle(np
, "phy-handle", 0);
72 dev_err(dev
, "Couldn't parse phy-handle\n");
76 priv
->phy_dev
= of_phy_find_device(phy_node
);
77 of_node_put(phy_node
);
79 dev_info(dev
, "Couldn't find phydev\n");
83 if (!priv
->phy_dev
->drv
) {
84 dev_info(dev
, "Attached phy not ready\n");
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
;
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
,
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");