1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2015 Broadcom Corporation
6 #include <linux/delay.h>
8 #include <linux/kernel.h>
9 #include <linux/module.h>
11 #include <linux/of_platform.h>
12 #include <linux/of_mdio.h>
13 #include <linux/phy.h>
14 #include <linux/platform_device.h>
15 #include <linux/sched.h>
17 #define IPROC_GPHY_MDCDIV 0x1a
19 #define MII_CTRL_OFFSET 0x000
21 #define MII_CTRL_DIV_SHIFT 0
22 #define MII_CTRL_PRE_SHIFT 7
23 #define MII_CTRL_BUSY_SHIFT 8
25 #define MII_DATA_OFFSET 0x004
26 #define MII_DATA_MASK 0xffff
27 #define MII_DATA_TA_SHIFT 16
28 #define MII_DATA_TA_VAL 2
29 #define MII_DATA_RA_SHIFT 18
30 #define MII_DATA_PA_SHIFT 23
31 #define MII_DATA_OP_SHIFT 28
32 #define MII_DATA_OP_WRITE 1
33 #define MII_DATA_OP_READ 2
34 #define MII_DATA_SB_SHIFT 30
36 struct iproc_mdio_priv
{
37 struct mii_bus
*mii_bus
;
41 static inline int iproc_mdio_wait_for_idle(void __iomem
*base
)
44 unsigned int timeout
= 1000; /* loop for 1s */
47 val
= readl(base
+ MII_CTRL_OFFSET
);
48 if ((val
& BIT(MII_CTRL_BUSY_SHIFT
)) == 0)
51 usleep_range(1000, 2000);
57 static inline void iproc_mdio_config_clk(void __iomem
*base
)
61 val
= (IPROC_GPHY_MDCDIV
<< MII_CTRL_DIV_SHIFT
) |
62 BIT(MII_CTRL_PRE_SHIFT
);
63 writel(val
, base
+ MII_CTRL_OFFSET
);
66 static int iproc_mdio_read(struct mii_bus
*bus
, int phy_id
, int reg
)
68 struct iproc_mdio_priv
*priv
= bus
->priv
;
72 rc
= iproc_mdio_wait_for_idle(priv
->base
);
76 /* Prepare the read operation */
77 cmd
= (MII_DATA_TA_VAL
<< MII_DATA_TA_SHIFT
) |
78 (reg
<< MII_DATA_RA_SHIFT
) |
79 (phy_id
<< MII_DATA_PA_SHIFT
) |
80 BIT(MII_DATA_SB_SHIFT
) |
81 (MII_DATA_OP_READ
<< MII_DATA_OP_SHIFT
);
83 writel(cmd
, priv
->base
+ MII_DATA_OFFSET
);
85 rc
= iproc_mdio_wait_for_idle(priv
->base
);
89 cmd
= readl(priv
->base
+ MII_DATA_OFFSET
) & MII_DATA_MASK
;
94 static int iproc_mdio_write(struct mii_bus
*bus
, int phy_id
,
97 struct iproc_mdio_priv
*priv
= bus
->priv
;
101 rc
= iproc_mdio_wait_for_idle(priv
->base
);
105 /* Prepare the write operation */
106 cmd
= (MII_DATA_TA_VAL
<< MII_DATA_TA_SHIFT
) |
107 (reg
<< MII_DATA_RA_SHIFT
) |
108 (phy_id
<< MII_DATA_PA_SHIFT
) |
109 BIT(MII_DATA_SB_SHIFT
) |
110 (MII_DATA_OP_WRITE
<< MII_DATA_OP_SHIFT
) |
111 ((u32
)(val
) & MII_DATA_MASK
);
113 writel(cmd
, priv
->base
+ MII_DATA_OFFSET
);
115 rc
= iproc_mdio_wait_for_idle(priv
->base
);
122 static int iproc_mdio_probe(struct platform_device
*pdev
)
124 struct iproc_mdio_priv
*priv
;
126 struct resource
*res
;
129 priv
= devm_kzalloc(&pdev
->dev
, sizeof(*priv
), GFP_KERNEL
);
133 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
134 priv
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
135 if (IS_ERR(priv
->base
)) {
136 dev_err(&pdev
->dev
, "failed to ioremap register\n");
137 return PTR_ERR(priv
->base
);
140 priv
->mii_bus
= mdiobus_alloc();
141 if (!priv
->mii_bus
) {
142 dev_err(&pdev
->dev
, "MDIO bus alloc failed\n");
148 bus
->name
= "iProc MDIO bus";
149 snprintf(bus
->id
, MII_BUS_ID_SIZE
, "%s-%d", pdev
->name
, pdev
->id
);
150 bus
->parent
= &pdev
->dev
;
151 bus
->read
= iproc_mdio_read
;
152 bus
->write
= iproc_mdio_write
;
154 iproc_mdio_config_clk(priv
->base
);
156 rc
= of_mdiobus_register(bus
, pdev
->dev
.of_node
);
158 dev_err(&pdev
->dev
, "MDIO bus registration failed\n");
162 platform_set_drvdata(pdev
, priv
);
164 dev_info(&pdev
->dev
, "Broadcom iProc MDIO bus at 0x%p\n", priv
->base
);
173 static int iproc_mdio_remove(struct platform_device
*pdev
)
175 struct iproc_mdio_priv
*priv
= platform_get_drvdata(pdev
);
177 mdiobus_unregister(priv
->mii_bus
);
178 mdiobus_free(priv
->mii_bus
);
183 static const struct of_device_id iproc_mdio_of_match
[] = {
184 { .compatible
= "brcm,iproc-mdio", },
187 MODULE_DEVICE_TABLE(of
, iproc_mdio_of_match
);
189 static struct platform_driver iproc_mdio_driver
= {
191 .name
= "iproc-mdio",
192 .of_match_table
= iproc_mdio_of_match
,
194 .probe
= iproc_mdio_probe
,
195 .remove
= iproc_mdio_remove
,
198 module_platform_driver(iproc_mdio_driver
);
200 MODULE_AUTHOR("Broadcom Corporation");
201 MODULE_DESCRIPTION("Broadcom iProc MDIO bus controller");
202 MODULE_LICENSE("GPL v2");
203 MODULE_ALIAS("platform:iproc-mdio");