GNU Linux-libre 4.19.314-gnu1
[releases.git] / drivers / net / phy / xilinx_gmii2rgmii.c
1 /* Xilinx GMII2RGMII Converter driver
2  *
3  * Copyright (C) 2016 Xilinx, Inc.
4  * Copyright (C) 2016 Andrew Lunn <andrew@lunn.ch>
5  *
6  * Author: Andrew Lunn <andrew@lunn.ch>
7  * Author: Kedareswara rao Appana <appanad@xilinx.com>
8  *
9  * Description:
10  * This driver is developed for Xilinx GMII2RGMII Converter
11  *
12  * This program is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation, either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  */
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/mii.h>
25 #include <linux/mdio.h>
26 #include <linux/phy.h>
27 #include <linux/of_mdio.h>
28
29 #define XILINX_GMII2RGMII_REG           0x10
30 #define XILINX_GMII2RGMII_SPEED_MASK    (BMCR_SPEED1000 | BMCR_SPEED100)
31
32 struct gmii2rgmii {
33         struct phy_device *phy_dev;
34         struct phy_driver *phy_drv;
35         struct phy_driver conv_phy_drv;
36         struct mdio_device *mdio;
37 };
38
39 static int xgmiitorgmii_read_status(struct phy_device *phydev)
40 {
41         struct gmii2rgmii *priv = phydev->priv;
42         struct mii_bus *bus = priv->mdio->bus;
43         int addr = priv->mdio->addr;
44         u16 val = 0;
45         int err;
46
47         if (priv->phy_drv->read_status)
48                 err = priv->phy_drv->read_status(phydev);
49         else
50                 err = genphy_read_status(phydev);
51         if (err < 0)
52                 return err;
53
54         val = mdiobus_read(bus, addr, XILINX_GMII2RGMII_REG);
55         val &= ~XILINX_GMII2RGMII_SPEED_MASK;
56
57         if (phydev->speed == SPEED_1000)
58                 val |= BMCR_SPEED1000;
59         else if (phydev->speed == SPEED_100)
60                 val |= BMCR_SPEED100;
61         else
62                 val |= BMCR_SPEED10;
63
64         mdiobus_write(bus, addr, XILINX_GMII2RGMII_REG, val);
65
66         return 0;
67 }
68
69 static int xgmiitorgmii_probe(struct mdio_device *mdiodev)
70 {
71         struct device *dev = &mdiodev->dev;
72         struct device_node *np = dev->of_node, *phy_node;
73         struct gmii2rgmii *priv;
74
75         priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
76         if (!priv)
77                 return -ENOMEM;
78
79         phy_node = of_parse_phandle(np, "phy-handle", 0);
80         if (!phy_node) {
81                 dev_err(dev, "Couldn't parse phy-handle\n");
82                 return -ENODEV;
83         }
84
85         priv->phy_dev = of_phy_find_device(phy_node);
86         of_node_put(phy_node);
87         if (!priv->phy_dev) {
88                 dev_info(dev, "Couldn't find phydev\n");
89                 return -EPROBE_DEFER;
90         }
91
92         if (!priv->phy_dev->drv) {
93                 dev_info(dev, "Attached phy not ready\n");
94                 put_device(&priv->phy_dev->mdio.dev);
95                 return -EPROBE_DEFER;
96         }
97
98         priv->mdio = mdiodev;
99         priv->phy_drv = priv->phy_dev->drv;
100         memcpy(&priv->conv_phy_drv, priv->phy_dev->drv,
101                sizeof(struct phy_driver));
102         priv->conv_phy_drv.read_status = xgmiitorgmii_read_status;
103         priv->phy_dev->priv = priv;
104         priv->phy_dev->drv = &priv->conv_phy_drv;
105
106         return 0;
107 }
108
109 static const struct of_device_id xgmiitorgmii_of_match[] = {
110         { .compatible = "xlnx,gmii-to-rgmii-1.0" },
111         {},
112 };
113 MODULE_DEVICE_TABLE(of, xgmiitorgmii_of_match);
114
115 static struct mdio_driver xgmiitorgmii_driver = {
116         .probe  = xgmiitorgmii_probe,
117         .mdiodrv.driver = {
118                 .name = "xgmiitorgmii",
119                 .of_match_table = xgmiitorgmii_of_match,
120         },
121 };
122
123 mdio_module_driver(xgmiitorgmii_driver);
124
125 MODULE_DESCRIPTION("Xilinx GMII2RGMII converter driver");
126 MODULE_LICENSE("GPL");