dt-bindings: mtd: ingenic: Use standard ecc-engine property
[linux/fpc-iii.git] / drivers / net / phy / mdio-hisi-femac.c
blob287f3ccf1da1deb649f2852ec8ed73ff573bd219
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 struct resource *res;
78 int ret;
80 bus = mdiobus_alloc_size(sizeof(*data));
81 if (!bus)
82 return -ENOMEM;
84 bus->name = "hisi_femac_mii_bus";
85 bus->read = &hisi_femac_mdio_read;
86 bus->write = &hisi_femac_mdio_write;
87 snprintf(bus->id, MII_BUS_ID_SIZE, "%s", pdev->name);
88 bus->parent = &pdev->dev;
90 data = bus->priv;
91 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
92 data->membase = devm_ioremap_resource(&pdev->dev, res);
93 if (IS_ERR(data->membase)) {
94 ret = PTR_ERR(data->membase);
95 goto err_out_free_mdiobus;
98 data->clk = devm_clk_get(&pdev->dev, NULL);
99 if (IS_ERR(data->clk)) {
100 ret = PTR_ERR(data->clk);
101 goto err_out_free_mdiobus;
104 ret = clk_prepare_enable(data->clk);
105 if (ret)
106 goto err_out_free_mdiobus;
108 ret = of_mdiobus_register(bus, np);
109 if (ret)
110 goto err_out_disable_clk;
112 platform_set_drvdata(pdev, bus);
114 return 0;
116 err_out_disable_clk:
117 clk_disable_unprepare(data->clk);
118 err_out_free_mdiobus:
119 mdiobus_free(bus);
120 return ret;
123 static int hisi_femac_mdio_remove(struct platform_device *pdev)
125 struct mii_bus *bus = platform_get_drvdata(pdev);
126 struct hisi_femac_mdio_data *data = bus->priv;
128 mdiobus_unregister(bus);
129 clk_disable_unprepare(data->clk);
130 mdiobus_free(bus);
132 return 0;
135 static const struct of_device_id hisi_femac_mdio_dt_ids[] = {
136 { .compatible = "hisilicon,hisi-femac-mdio" },
139 MODULE_DEVICE_TABLE(of, hisi_femac_mdio_dt_ids);
141 static struct platform_driver hisi_femac_mdio_driver = {
142 .probe = hisi_femac_mdio_probe,
143 .remove = hisi_femac_mdio_remove,
144 .driver = {
145 .name = "hisi-femac-mdio",
146 .of_match_table = hisi_femac_mdio_dt_ids,
150 module_platform_driver(hisi_femac_mdio_driver);
152 MODULE_DESCRIPTION("Hisilicon Fast Ethernet MAC MDIO interface driver");
153 MODULE_AUTHOR("Dongpo Li <lidongpo@hisilicon.com>");
154 MODULE_LICENSE("GPL");