Appana Durga Kedareswara Rao | f411a61 | 2016-08-10 11:20:08 +0530 | [diff] [blame] | 1 | /* Xilinx GMII2RGMII Converter driver |
| 2 | * |
| 3 | * Copyright (C) 2016 Xilinx, Inc. |
Appana Durga Kedareswara Rao | e202d4c | 2016-08-16 11:58:29 +0530 | [diff] [blame] | 4 | * Copyright (C) 2016 Andrew Lunn <andrew@lunn.ch> |
Appana Durga Kedareswara Rao | f411a61 | 2016-08-10 11:20:08 +0530 | [diff] [blame] | 5 | * |
Appana Durga Kedareswara Rao | e202d4c | 2016-08-16 11:58:29 +0530 | [diff] [blame] | 6 | * Author: Andrew Lunn <andrew@lunn.ch> |
Appana Durga Kedareswara Rao | f411a61 | 2016-08-10 11:20:08 +0530 | [diff] [blame] | 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 | int addr; |
| 37 | }; |
| 38 | |
| 39 | static int xgmiitorgmii_read_status(struct phy_device *phydev) |
| 40 | { |
| 41 | struct gmii2rgmii *priv = phydev->priv; |
| 42 | u16 val = 0; |
Brandon Maier | 7223311 | 2018-06-26 12:50:50 -0500 | [diff] [blame] | 43 | int err; |
Appana Durga Kedareswara Rao | f411a61 | 2016-08-10 11:20:08 +0530 | [diff] [blame] | 44 | |
Brandon Maier | 7223311 | 2018-06-26 12:50:50 -0500 | [diff] [blame] | 45 | err = priv->phy_drv->read_status(phydev); |
| 46 | if (err < 0) |
| 47 | return err; |
Appana Durga Kedareswara Rao | f411a61 | 2016-08-10 11:20:08 +0530 | [diff] [blame] | 48 | |
| 49 | val = mdiobus_read(phydev->mdio.bus, priv->addr, XILINX_GMII2RGMII_REG); |
Fahad Kunnathadi | fc2fe7a | 2017-09-15 12:01:58 +0530 | [diff] [blame] | 50 | val &= ~XILINX_GMII2RGMII_SPEED_MASK; |
Appana Durga Kedareswara Rao | f411a61 | 2016-08-10 11:20:08 +0530 | [diff] [blame] | 51 | |
| 52 | if (phydev->speed == SPEED_1000) |
| 53 | val |= BMCR_SPEED1000; |
| 54 | else if (phydev->speed == SPEED_100) |
| 55 | val |= BMCR_SPEED100; |
| 56 | else |
| 57 | val |= BMCR_SPEED10; |
| 58 | |
| 59 | mdiobus_write(phydev->mdio.bus, priv->addr, XILINX_GMII2RGMII_REG, val); |
| 60 | |
| 61 | return 0; |
| 62 | } |
| 63 | |
Wei Yongjun | 2698f85 | 2016-08-23 15:06:05 +0000 | [diff] [blame] | 64 | static int xgmiitorgmii_probe(struct mdio_device *mdiodev) |
Appana Durga Kedareswara Rao | f411a61 | 2016-08-10 11:20:08 +0530 | [diff] [blame] | 65 | { |
| 66 | struct device *dev = &mdiodev->dev; |
| 67 | struct device_node *np = dev->of_node, *phy_node; |
| 68 | struct gmii2rgmii *priv; |
| 69 | |
| 70 | priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); |
| 71 | if (!priv) |
| 72 | return -ENOMEM; |
| 73 | |
| 74 | phy_node = of_parse_phandle(np, "phy-handle", 0); |
Wei Yongjun | 6472109 | 2016-08-15 22:34:57 +0000 | [diff] [blame] | 75 | if (!phy_node) { |
Appana Durga Kedareswara Rao | f411a61 | 2016-08-10 11:20:08 +0530 | [diff] [blame] | 76 | dev_err(dev, "Couldn't parse phy-handle\n"); |
| 77 | return -ENODEV; |
| 78 | } |
| 79 | |
| 80 | priv->phy_dev = of_phy_find_device(phy_node); |
Wei Yongjun | 4d55d01 | 2016-08-21 22:46:15 +0000 | [diff] [blame] | 81 | of_node_put(phy_node); |
Appana Durga Kedareswara Rao | f411a61 | 2016-08-10 11:20:08 +0530 | [diff] [blame] | 82 | if (!priv->phy_dev) { |
| 83 | dev_info(dev, "Couldn't find phydev\n"); |
| 84 | return -EPROBE_DEFER; |
| 85 | } |
| 86 | |
Brandon Maier | a5a849c | 2018-06-26 12:50:48 -0500 | [diff] [blame] | 87 | if (!priv->phy_dev->drv) { |
| 88 | dev_info(dev, "Attached phy not ready\n"); |
| 89 | return -EPROBE_DEFER; |
| 90 | } |
| 91 | |
Appana Durga Kedareswara Rao | f411a61 | 2016-08-10 11:20:08 +0530 | [diff] [blame] | 92 | priv->addr = mdiodev->addr; |
| 93 | priv->phy_drv = priv->phy_dev->drv; |
| 94 | memcpy(&priv->conv_phy_drv, priv->phy_dev->drv, |
| 95 | sizeof(struct phy_driver)); |
| 96 | priv->conv_phy_drv.read_status = xgmiitorgmii_read_status; |
| 97 | priv->phy_dev->priv = priv; |
| 98 | priv->phy_dev->drv = &priv->conv_phy_drv; |
| 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | static const struct of_device_id xgmiitorgmii_of_match[] = { |
| 104 | { .compatible = "xlnx,gmii-to-rgmii-1.0" }, |
| 105 | {}, |
| 106 | }; |
| 107 | MODULE_DEVICE_TABLE(of, xgmiitorgmii_of_match); |
| 108 | |
| 109 | static struct mdio_driver xgmiitorgmii_driver = { |
| 110 | .probe = xgmiitorgmii_probe, |
| 111 | .mdiodrv.driver = { |
| 112 | .name = "xgmiitorgmii", |
| 113 | .of_match_table = xgmiitorgmii_of_match, |
| 114 | }, |
| 115 | }; |
| 116 | |
| 117 | mdio_module_driver(xgmiitorgmii_driver); |
| 118 | |
| 119 | MODULE_DESCRIPTION("Xilinx GMII2RGMII converter driver"); |
| 120 | MODULE_LICENSE("GPL"); |