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
;
80 bus
= mdiobus_alloc_size(sizeof(*data
));
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
;
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
);
106 goto err_out_free_mdiobus
;
108 ret
= of_mdiobus_register(bus
, np
);
110 goto err_out_disable_clk
;
112 platform_set_drvdata(pdev
, bus
);
117 clk_disable_unprepare(data
->clk
);
118 err_out_free_mdiobus
:
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
);
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
,
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");