1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright 2016 Broadcom
5 #include <linux/align.h>
7 #include <linux/delay.h>
8 #include <linux/device.h>
9 #include <linux/iopoll.h>
10 #include <linux/mdio-mux.h>
11 #include <linux/module.h>
12 #include <linux/of_mdio.h>
13 #include <linux/phy.h>
14 #include <linux/platform_device.h>
15 #include <linux/sizes.h>
17 #define MDIO_RATE_ADJ_EXT_OFFSET 0x000
18 #define MDIO_RATE_ADJ_INT_OFFSET 0x004
19 #define MDIO_RATE_ADJ_DIVIDENT_SHIFT 16
21 #define MDIO_SCAN_CTRL_OFFSET 0x008
22 #define MDIO_SCAN_CTRL_OVRIDE_EXT_MSTR 28
24 #define MDIO_PARAM_OFFSET 0x23c
25 #define MDIO_PARAM_MIIM_CYCLE 29
26 #define MDIO_PARAM_INTERNAL_SEL 25
27 #define MDIO_PARAM_BUS_ID 22
28 #define MDIO_PARAM_C45_SEL 21
29 #define MDIO_PARAM_PHY_ID 16
30 #define MDIO_PARAM_PHY_DATA 0
32 #define MDIO_READ_OFFSET 0x240
33 #define MDIO_READ_DATA_MASK 0xffff
34 #define MDIO_ADDR_OFFSET 0x244
36 #define MDIO_CTRL_OFFSET 0x248
37 #define MDIO_CTRL_WRITE_OP 0x1
38 #define MDIO_CTRL_READ_OP 0x2
40 #define MDIO_STAT_OFFSET 0x24c
41 #define MDIO_STAT_DONE 1
43 #define BUS_MAX_ADDR 32
44 #define EXT_BUS_START_ADDR 16
46 #define MDIO_REG_ADDR_SPACE_SIZE 0x250
48 #define MDIO_OPERATING_FREQUENCY 11000000
49 #define MDIO_RATE_ADJ_DIVIDENT 1
51 struct iproc_mdiomux_desc
{
55 struct mii_bus
*mii_bus
;
59 static void mdio_mux_iproc_config(struct iproc_mdiomux_desc
*md
)
64 /* Disable external mdio master access */
65 val
= readl(md
->base
+ MDIO_SCAN_CTRL_OFFSET
);
66 val
|= BIT(MDIO_SCAN_CTRL_OVRIDE_EXT_MSTR
);
67 writel(val
, md
->base
+ MDIO_SCAN_CTRL_OFFSET
);
70 /* use rate adjust regs to derive the mdio's operating
71 * frequency from the specified core clock
73 divisor
= clk_get_rate(md
->core_clk
) / MDIO_OPERATING_FREQUENCY
;
74 divisor
= divisor
/ (MDIO_RATE_ADJ_DIVIDENT
+ 1);
76 val
|= MDIO_RATE_ADJ_DIVIDENT
<< MDIO_RATE_ADJ_DIVIDENT_SHIFT
;
77 writel(val
, md
->base
+ MDIO_RATE_ADJ_EXT_OFFSET
);
78 writel(val
, md
->base
+ MDIO_RATE_ADJ_INT_OFFSET
);
82 static int iproc_mdio_wait_for_idle(void __iomem
*base
, bool result
)
86 return readl_poll_timeout(base
+ MDIO_STAT_OFFSET
, val
,
87 (val
& MDIO_STAT_DONE
) == result
,
91 /* start_miim_ops- Program and start MDIO transaction over mdio bus.
93 * @phyid: phyid of the selected bus.
94 * @reg: register offset to be read/written.
95 * @val :0 if read op else value to be written in @reg;
96 * @op: Operation that need to be carried out.
97 * MDIO_CTRL_READ_OP: Read transaction.
98 * MDIO_CTRL_WRITE_OP: Write transaction.
100 * Return value: Successful Read operation returns read reg values and write
101 * operation returns 0. Failure operation returns negative error code.
103 static int start_miim_ops(void __iomem
*base
, bool c45
,
104 u16 phyid
, u32 reg
, u16 val
, u32 op
)
109 writel(0, base
+ MDIO_CTRL_OFFSET
);
110 ret
= iproc_mdio_wait_for_idle(base
, 0);
114 param
= readl(base
+ MDIO_PARAM_OFFSET
);
115 param
|= phyid
<< MDIO_PARAM_PHY_ID
;
116 param
|= val
<< MDIO_PARAM_PHY_DATA
;
118 param
|= BIT(MDIO_PARAM_C45_SEL
);
120 writel(param
, base
+ MDIO_PARAM_OFFSET
);
122 writel(reg
, base
+ MDIO_ADDR_OFFSET
);
124 writel(op
, base
+ MDIO_CTRL_OFFSET
);
126 ret
= iproc_mdio_wait_for_idle(base
, 1);
130 if (op
== MDIO_CTRL_READ_OP
)
131 ret
= readl(base
+ MDIO_READ_OFFSET
) & MDIO_READ_DATA_MASK
;
136 static int iproc_mdiomux_read_c22(struct mii_bus
*bus
, int phyid
, int reg
)
138 struct iproc_mdiomux_desc
*md
= bus
->priv
;
141 ret
= start_miim_ops(md
->base
, false, phyid
, reg
, 0, MDIO_CTRL_READ_OP
);
143 dev_err(&bus
->dev
, "mdiomux c22 read operation failed!!!");
148 static int iproc_mdiomux_read_c45(struct mii_bus
*bus
, int phyid
, int devad
,
151 struct iproc_mdiomux_desc
*md
= bus
->priv
;
154 ret
= start_miim_ops(md
->base
, true, phyid
, reg
| devad
<< 16, 0,
157 dev_err(&bus
->dev
, "mdiomux read c45 operation failed!!!");
162 static int iproc_mdiomux_write_c22(struct mii_bus
*bus
,
163 int phyid
, int reg
, u16 val
)
165 struct iproc_mdiomux_desc
*md
= bus
->priv
;
168 /* Write val at reg offset */
169 ret
= start_miim_ops(md
->base
, false, phyid
, reg
, val
,
172 dev_err(&bus
->dev
, "mdiomux write c22 operation failed!!!");
177 static int iproc_mdiomux_write_c45(struct mii_bus
*bus
,
178 int phyid
, int devad
, int reg
, u16 val
)
180 struct iproc_mdiomux_desc
*md
= bus
->priv
;
183 /* Write val at reg offset */
184 ret
= start_miim_ops(md
->base
, true, phyid
, reg
| devad
<< 16, val
,
187 dev_err(&bus
->dev
, "mdiomux write c45 operation failed!!!");
192 static int mdio_mux_iproc_switch_fn(int current_child
, int desired_child
,
195 struct iproc_mdiomux_desc
*md
= data
;
199 /* select bus and its properties */
200 bus_dir
= (desired_child
< EXT_BUS_START_ADDR
);
201 bus_id
= bus_dir
? desired_child
: (desired_child
- EXT_BUS_START_ADDR
);
203 param
= (bus_dir
? 1 : 0) << MDIO_PARAM_INTERNAL_SEL
;
204 param
|= (bus_id
<< MDIO_PARAM_BUS_ID
);
206 writel(param
, md
->base
+ MDIO_PARAM_OFFSET
);
210 static int mdio_mux_iproc_probe(struct platform_device
*pdev
)
212 struct iproc_mdiomux_desc
*md
;
214 struct resource
*res
;
217 md
= devm_kzalloc(&pdev
->dev
, sizeof(*md
), GFP_KERNEL
);
220 md
->dev
= &pdev
->dev
;
222 md
->base
= devm_platform_get_and_ioremap_resource(pdev
, 0, &res
);
223 if (IS_ERR(md
->base
))
224 return PTR_ERR(md
->base
);
225 if (!IS_ALIGNED(res
->start
, SZ_4K
)) {
226 /* For backward compatibility in case the
227 * base address is specified with an offset.
229 dev_info(&pdev
->dev
, "fix base address in dt-blob\n");
230 res
->start
= ALIGN_DOWN(res
->start
, SZ_4K
);
231 res
->end
= res
->start
+ MDIO_REG_ADDR_SPACE_SIZE
- 1;
234 md
->mii_bus
= devm_mdiobus_alloc(&pdev
->dev
);
236 dev_err(&pdev
->dev
, "mdiomux bus alloc failed\n");
240 md
->core_clk
= devm_clk_get(&pdev
->dev
, NULL
);
241 if (md
->core_clk
== ERR_PTR(-ENOENT
) ||
242 md
->core_clk
== ERR_PTR(-EINVAL
))
244 else if (IS_ERR(md
->core_clk
))
245 return PTR_ERR(md
->core_clk
);
247 rc
= clk_prepare_enable(md
->core_clk
);
249 dev_err(&pdev
->dev
, "failed to enable core clk\n");
255 bus
->name
= "iProc MDIO mux bus";
256 snprintf(bus
->id
, MII_BUS_ID_SIZE
, "%s-%d", pdev
->name
, pdev
->id
);
257 bus
->parent
= &pdev
->dev
;
258 bus
->read
= iproc_mdiomux_read_c22
;
259 bus
->write
= iproc_mdiomux_write_c22
;
260 bus
->read_c45
= iproc_mdiomux_read_c45
;
261 bus
->write_c45
= iproc_mdiomux_write_c45
;
264 bus
->dev
.of_node
= pdev
->dev
.of_node
;
265 rc
= mdiobus_register(bus
);
267 dev_err(&pdev
->dev
, "mdiomux registration failed\n");
271 platform_set_drvdata(pdev
, md
);
273 rc
= mdio_mux_init(md
->dev
, md
->dev
->of_node
, mdio_mux_iproc_switch_fn
,
274 &md
->mux_handle
, md
, md
->mii_bus
);
276 dev_info(md
->dev
, "mdiomux initialization failed\n");
280 mdio_mux_iproc_config(md
);
282 dev_info(md
->dev
, "iProc mdiomux registered\n");
286 mdiobus_unregister(bus
);
288 clk_disable_unprepare(md
->core_clk
);
292 static void mdio_mux_iproc_remove(struct platform_device
*pdev
)
294 struct iproc_mdiomux_desc
*md
= platform_get_drvdata(pdev
);
296 mdio_mux_uninit(md
->mux_handle
);
297 mdiobus_unregister(md
->mii_bus
);
298 clk_disable_unprepare(md
->core_clk
);
301 #ifdef CONFIG_PM_SLEEP
302 static int mdio_mux_iproc_suspend(struct device
*dev
)
304 struct iproc_mdiomux_desc
*md
= dev_get_drvdata(dev
);
306 clk_disable_unprepare(md
->core_clk
);
311 static int mdio_mux_iproc_resume(struct device
*dev
)
313 struct iproc_mdiomux_desc
*md
= dev_get_drvdata(dev
);
316 rc
= clk_prepare_enable(md
->core_clk
);
318 dev_err(md
->dev
, "failed to enable core clk\n");
321 mdio_mux_iproc_config(md
);
327 static SIMPLE_DEV_PM_OPS(mdio_mux_iproc_pm_ops
,
328 mdio_mux_iproc_suspend
, mdio_mux_iproc_resume
);
330 static const struct of_device_id mdio_mux_iproc_match
[] = {
332 .compatible
= "brcm,mdio-mux-iproc",
336 MODULE_DEVICE_TABLE(of
, mdio_mux_iproc_match
);
338 static struct platform_driver mdiomux_iproc_driver
= {
340 .name
= "mdio-mux-iproc",
341 .of_match_table
= mdio_mux_iproc_match
,
342 .pm
= &mdio_mux_iproc_pm_ops
,
344 .probe
= mdio_mux_iproc_probe
,
345 .remove
= mdio_mux_iproc_remove
,
348 module_platform_driver(mdiomux_iproc_driver
);
350 MODULE_DESCRIPTION("iProc MDIO Mux Bus Driver");
351 MODULE_AUTHOR("Pramod Kumar <pramod.kumar@broadcom.com>");
352 MODULE_LICENSE("GPL v2");