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/clk.h>
19 #include <linux/of_mdio.h>
21 #define XILINX_GMII2RGMII_REG 0x10
22 #define XILINX_GMII2RGMII_SPEED_MASK (BMCR_SPEED1000 | BMCR_SPEED100)
25 struct phy_device
*phy_dev
;
26 const struct phy_driver
*phy_drv
;
27 struct phy_driver conv_phy_drv
;
28 struct mdio_device
*mdio
;
31 static void xgmiitorgmii_configure(struct gmii2rgmii
*priv
, int speed
)
33 struct mii_bus
*bus
= priv
->mdio
->bus
;
34 int addr
= priv
->mdio
->addr
;
37 val
= mdiobus_read(bus
, addr
, XILINX_GMII2RGMII_REG
);
38 val
&= ~XILINX_GMII2RGMII_SPEED_MASK
;
40 if (speed
== SPEED_1000
)
41 val
|= BMCR_SPEED1000
;
42 else if (speed
== SPEED_100
)
47 mdiobus_write(bus
, addr
, XILINX_GMII2RGMII_REG
, val
);
50 static int xgmiitorgmii_read_status(struct phy_device
*phydev
)
52 struct gmii2rgmii
*priv
= mdiodev_get_drvdata(&phydev
->mdio
);
55 if (priv
->phy_drv
->read_status
)
56 err
= priv
->phy_drv
->read_status(phydev
);
58 err
= genphy_read_status(phydev
);
62 xgmiitorgmii_configure(priv
, phydev
->speed
);
67 static int xgmiitorgmii_set_loopback(struct phy_device
*phydev
, bool enable
)
69 struct gmii2rgmii
*priv
= mdiodev_get_drvdata(&phydev
->mdio
);
72 if (priv
->phy_drv
->set_loopback
)
73 err
= priv
->phy_drv
->set_loopback(phydev
, enable
);
75 err
= genphy_loopback(phydev
, enable
);
79 xgmiitorgmii_configure(priv
, phydev
->speed
);
84 static int xgmiitorgmii_probe(struct mdio_device
*mdiodev
)
86 struct device
*dev
= &mdiodev
->dev
;
87 struct device_node
*np
= dev
->of_node
, *phy_node
;
88 struct gmii2rgmii
*priv
;
91 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
95 clkin
= devm_clk_get_optional_enabled(dev
, NULL
);
97 return dev_err_probe(dev
, PTR_ERR(clkin
),
98 "Failed to get and enable clock from Device Tree\n");
100 phy_node
= of_parse_phandle(np
, "phy-handle", 0);
102 dev_err(dev
, "Couldn't parse phy-handle\n");
106 priv
->phy_dev
= of_phy_find_device(phy_node
);
107 of_node_put(phy_node
);
108 if (!priv
->phy_dev
) {
109 dev_info(dev
, "Couldn't find phydev\n");
110 return -EPROBE_DEFER
;
113 if (!priv
->phy_dev
->drv
) {
114 dev_info(dev
, "Attached phy not ready\n");
115 put_device(&priv
->phy_dev
->mdio
.dev
);
116 return -EPROBE_DEFER
;
119 priv
->mdio
= mdiodev
;
120 priv
->phy_drv
= priv
->phy_dev
->drv
;
121 memcpy(&priv
->conv_phy_drv
, priv
->phy_dev
->drv
,
122 sizeof(struct phy_driver
));
123 priv
->conv_phy_drv
.read_status
= xgmiitorgmii_read_status
;
124 priv
->conv_phy_drv
.set_loopback
= xgmiitorgmii_set_loopback
;
125 mdiodev_set_drvdata(&priv
->phy_dev
->mdio
, priv
);
126 priv
->phy_dev
->drv
= &priv
->conv_phy_drv
;
131 static const struct of_device_id xgmiitorgmii_of_match
[] = {
132 { .compatible
= "xlnx,gmii-to-rgmii-1.0" },
135 MODULE_DEVICE_TABLE(of
, xgmiitorgmii_of_match
);
137 static struct mdio_driver xgmiitorgmii_driver
= {
138 .probe
= xgmiitorgmii_probe
,
140 .name
= "xgmiitorgmii",
141 .of_match_table
= xgmiitorgmii_of_match
,
145 mdio_module_driver(xgmiitorgmii_driver
);
147 MODULE_DESCRIPTION("Xilinx GMII2RGMII converter driver");
148 MODULE_LICENSE("GPL");