treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / net / phy / mdio-hisi-femac.c
blobf231c2fbb1de42e3299afa1fa2515126a6186d00
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Hisilicon Fast Ethernet MDIO Bus Driver
5 * Copyright (c) 2016 HiSilicon Technologies Co., Ltd.
6 */
8 #include <linux/clk.h>
9 #include <linux/iopoll.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/of_address.h>
13 #include <linux/of_mdio.h>
14 #include <linux/platform_device.h>
16 #define MDIO_RWCTRL 0x00
17 #define MDIO_RO_DATA 0x04
18 #define MDIO_WRITE BIT(13)
19 #define MDIO_RW_FINISH BIT(15)
20 #define BIT_PHY_ADDR_OFFSET 8
21 #define BIT_WR_DATA_OFFSET 16
23 struct hisi_femac_mdio_data {
24 struct clk *clk;
25 void __iomem *membase;
28 static int hisi_femac_mdio_wait_ready(struct hisi_femac_mdio_data *data)
30 u32 val;
32 return readl_poll_timeout(data->membase + MDIO_RWCTRL,
33 val, val & MDIO_RW_FINISH, 20, 10000);
36 static int hisi_femac_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
38 struct hisi_femac_mdio_data *data = bus->priv;
39 int ret;
41 ret = hisi_femac_mdio_wait_ready(data);
42 if (ret)
43 return ret;
45 writel((mii_id << BIT_PHY_ADDR_OFFSET) | regnum,
46 data->membase + MDIO_RWCTRL);
48 ret = hisi_femac_mdio_wait_ready(data);
49 if (ret)
50 return ret;
52 return readl(data->membase + MDIO_RO_DATA) & 0xFFFF;
55 static int hisi_femac_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
56 u16 value)
58 struct hisi_femac_mdio_data *data = bus->priv;
59 int ret;
61 ret = hisi_femac_mdio_wait_ready(data);
62 if (ret)
63 return ret;
65 writel(MDIO_WRITE | (value << BIT_WR_DATA_OFFSET) |
66 (mii_id << BIT_PHY_ADDR_OFFSET) | regnum,
67 data->membase + MDIO_RWCTRL);
69 return hisi_femac_mdio_wait_ready(data);
72 static int hisi_femac_mdio_probe(struct platform_device *pdev)
74 struct device_node *np = pdev->dev.of_node;
75 struct mii_bus *bus;
76 struct hisi_femac_mdio_data *data;
77 int ret;
79 bus = mdiobus_alloc_size(sizeof(*data));
80 if (!bus)
81 return -ENOMEM;
83 bus->name = "hisi_femac_mii_bus";
84 bus->read = &hisi_femac_mdio_read;
85 bus->write = &hisi_femac_mdio_write;
86 snprintf(bus->id, MII_BUS_ID_SIZE, "%s", pdev->name);
87 bus->parent = &pdev->dev;
89 data = bus->priv;
90 data->membase = devm_platform_ioremap_resource(pdev, 0);
91 if (IS_ERR(data->membase)) {
92 ret = PTR_ERR(data->membase);
93 goto err_out_free_mdiobus;
96 data->clk = devm_clk_get(&pdev->dev, NULL);
97 if (IS_ERR(data->clk)) {
98 ret = PTR_ERR(data->clk);
99 goto err_out_free_mdiobus;
102 ret = clk_prepare_enable(data->clk);
103 if (ret)
104 goto err_out_free_mdiobus;
106 ret = of_mdiobus_register(bus, np);
107 if (ret)
108 goto err_out_disable_clk;
110 platform_set_drvdata(pdev, bus);
112 return 0;
114 err_out_disable_clk:
115 clk_disable_unprepare(data->clk);
116 err_out_free_mdiobus:
117 mdiobus_free(bus);
118 return ret;
121 static int hisi_femac_mdio_remove(struct platform_device *pdev)
123 struct mii_bus *bus = platform_get_drvdata(pdev);
124 struct hisi_femac_mdio_data *data = bus->priv;
126 mdiobus_unregister(bus);
127 clk_disable_unprepare(data->clk);
128 mdiobus_free(bus);
130 return 0;
133 static const struct of_device_id hisi_femac_mdio_dt_ids[] = {
134 { .compatible = "hisilicon,hisi-femac-mdio" },
137 MODULE_DEVICE_TABLE(of, hisi_femac_mdio_dt_ids);
139 static struct platform_driver hisi_femac_mdio_driver = {
140 .probe = hisi_femac_mdio_probe,
141 .remove = hisi_femac_mdio_remove,
142 .driver = {
143 .name = "hisi-femac-mdio",
144 .of_match_table = hisi_femac_mdio_dt_ids,
148 module_platform_driver(hisi_femac_mdio_driver);
150 MODULE_DESCRIPTION("Hisilicon Fast Ethernet MAC MDIO interface driver");
151 MODULE_AUTHOR("Dongpo Li <lidongpo@hisilicon.com>");
152 MODULE_LICENSE("GPL");