1 // SPDX-License-Identifier: GPL-2.0+
3 * Hisilicon Fast Ethernet MDIO Bus Driver
5 * Copyright (c) 2016 HiSilicon Technologies Co., Ltd.
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
{
25 void __iomem
*membase
;
28 static int hisi_femac_mdio_wait_ready(struct hisi_femac_mdio_data
*data
)
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
;
41 ret
= hisi_femac_mdio_wait_ready(data
);
45 writel((mii_id
<< BIT_PHY_ADDR_OFFSET
) | regnum
,
46 data
->membase
+ MDIO_RWCTRL
);
48 ret
= hisi_femac_mdio_wait_ready(data
);
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
,
58 struct hisi_femac_mdio_data
*data
= bus
->priv
;
61 ret
= hisi_femac_mdio_wait_ready(data
);
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
;
76 struct hisi_femac_mdio_data
*data
;
79 bus
= mdiobus_alloc_size(sizeof(*data
));
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
;
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
);
104 goto err_out_free_mdiobus
;
106 ret
= of_mdiobus_register(bus
, np
);
108 goto err_out_disable_clk
;
110 platform_set_drvdata(pdev
, bus
);
115 clk_disable_unprepare(data
->clk
);
116 err_out_free_mdiobus
:
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
);
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
,
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");