2 * Copyright 2016 Broadcom
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2, as
6 * published by the Free Software Foundation (the "GPL").
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License version 2 (GPLv2) for more details.
13 * You should have received a copy of the GNU General Public License
14 * version 2 (GPLv2) along with this source code.
16 #include <linux/clk.h>
17 #include <linux/platform_device.h>
18 #include <linux/device.h>
19 #include <linux/of_mdio.h>
20 #include <linux/module.h>
21 #include <linux/phy.h>
22 #include <linux/mdio-mux.h>
23 #include <linux/delay.h>
25 #define MDIO_RATE_ADJ_EXT_OFFSET 0x000
26 #define MDIO_RATE_ADJ_INT_OFFSET 0x004
27 #define MDIO_RATE_ADJ_DIVIDENT_SHIFT 16
29 #define MDIO_SCAN_CTRL_OFFSET 0x008
30 #define MDIO_SCAN_CTRL_OVRIDE_EXT_MSTR 28
32 #define MDIO_PARAM_OFFSET 0x23c
33 #define MDIO_PARAM_MIIM_CYCLE 29
34 #define MDIO_PARAM_INTERNAL_SEL 25
35 #define MDIO_PARAM_BUS_ID 22
36 #define MDIO_PARAM_C45_SEL 21
37 #define MDIO_PARAM_PHY_ID 16
38 #define MDIO_PARAM_PHY_DATA 0
40 #define MDIO_READ_OFFSET 0x240
41 #define MDIO_READ_DATA_MASK 0xffff
42 #define MDIO_ADDR_OFFSET 0x244
44 #define MDIO_CTRL_OFFSET 0x248
45 #define MDIO_CTRL_WRITE_OP 0x1
46 #define MDIO_CTRL_READ_OP 0x2
48 #define MDIO_STAT_OFFSET 0x24c
49 #define MDIO_STAT_DONE 1
51 #define BUS_MAX_ADDR 32
52 #define EXT_BUS_START_ADDR 16
54 #define MDIO_REG_ADDR_SPACE_SIZE 0x250
56 #define MDIO_OPERATING_FREQUENCY 11000000
57 #define MDIO_RATE_ADJ_DIVIDENT 1
59 struct iproc_mdiomux_desc
{
63 struct mii_bus
*mii_bus
;
67 static void mdio_mux_iproc_config(struct iproc_mdiomux_desc
*md
)
72 /* Disable external mdio master access */
73 val
= readl(md
->base
+ MDIO_SCAN_CTRL_OFFSET
);
74 val
|= BIT(MDIO_SCAN_CTRL_OVRIDE_EXT_MSTR
);
75 writel(val
, md
->base
+ MDIO_SCAN_CTRL_OFFSET
);
78 /* use rate adjust regs to derrive the mdio's operating
79 * frequency from the specified core clock
81 divisor
= clk_get_rate(md
->core_clk
) / MDIO_OPERATING_FREQUENCY
;
82 divisor
= divisor
/ (MDIO_RATE_ADJ_DIVIDENT
+ 1);
84 val
|= MDIO_RATE_ADJ_DIVIDENT
<< MDIO_RATE_ADJ_DIVIDENT_SHIFT
;
85 writel(val
, md
->base
+ MDIO_RATE_ADJ_EXT_OFFSET
);
86 writel(val
, md
->base
+ MDIO_RATE_ADJ_INT_OFFSET
);
90 static int iproc_mdio_wait_for_idle(void __iomem
*base
, bool result
)
92 unsigned int timeout
= 1000; /* loop for 1s */
96 val
= readl(base
+ MDIO_STAT_OFFSET
);
97 if ((val
& MDIO_STAT_DONE
) == result
)
100 usleep_range(1000, 2000);
106 /* start_miim_ops- Program and start MDIO transaction over mdio bus.
107 * @base: Base address
108 * @phyid: phyid of the selected bus.
109 * @reg: register offset to be read/written.
110 * @val :0 if read op else value to be written in @reg;
111 * @op: Operation that need to be carried out.
112 * MDIO_CTRL_READ_OP: Read transaction.
113 * MDIO_CTRL_WRITE_OP: Write transaction.
115 * Return value: Successful Read operation returns read reg values and write
116 * operation returns 0. Failure operation returns negative error code.
118 static int start_miim_ops(void __iomem
*base
,
119 u16 phyid
, u32 reg
, u16 val
, u32 op
)
124 writel(0, base
+ MDIO_CTRL_OFFSET
);
125 ret
= iproc_mdio_wait_for_idle(base
, 0);
129 param
= readl(base
+ MDIO_PARAM_OFFSET
);
130 param
|= phyid
<< MDIO_PARAM_PHY_ID
;
131 param
|= val
<< MDIO_PARAM_PHY_DATA
;
132 if (reg
& MII_ADDR_C45
)
133 param
|= BIT(MDIO_PARAM_C45_SEL
);
135 writel(param
, base
+ MDIO_PARAM_OFFSET
);
137 writel(reg
, base
+ MDIO_ADDR_OFFSET
);
139 writel(op
, base
+ MDIO_CTRL_OFFSET
);
141 ret
= iproc_mdio_wait_for_idle(base
, 1);
145 if (op
== MDIO_CTRL_READ_OP
)
146 ret
= readl(base
+ MDIO_READ_OFFSET
) & MDIO_READ_DATA_MASK
;
151 static int iproc_mdiomux_read(struct mii_bus
*bus
, int phyid
, int reg
)
153 struct iproc_mdiomux_desc
*md
= bus
->priv
;
156 ret
= start_miim_ops(md
->base
, phyid
, reg
, 0, MDIO_CTRL_READ_OP
);
158 dev_err(&bus
->dev
, "mdiomux read operation failed!!!");
163 static int iproc_mdiomux_write(struct mii_bus
*bus
,
164 int phyid
, int reg
, u16 val
)
166 struct iproc_mdiomux_desc
*md
= bus
->priv
;
169 /* Write val at reg offset */
170 ret
= start_miim_ops(md
->base
, phyid
, reg
, val
, MDIO_CTRL_WRITE_OP
);
172 dev_err(&bus
->dev
, "mdiomux write operation failed!!!");
177 static int mdio_mux_iproc_switch_fn(int current_child
, int desired_child
,
180 struct iproc_mdiomux_desc
*md
= data
;
184 /* select bus and its properties */
185 bus_dir
= (desired_child
< EXT_BUS_START_ADDR
);
186 bus_id
= bus_dir
? desired_child
: (desired_child
- EXT_BUS_START_ADDR
);
188 param
= (bus_dir
? 1 : 0) << MDIO_PARAM_INTERNAL_SEL
;
189 param
|= (bus_id
<< MDIO_PARAM_BUS_ID
);
191 writel(param
, md
->base
+ MDIO_PARAM_OFFSET
);
195 static int mdio_mux_iproc_probe(struct platform_device
*pdev
)
197 struct iproc_mdiomux_desc
*md
;
199 struct resource
*res
;
202 md
= devm_kzalloc(&pdev
->dev
, sizeof(*md
), GFP_KERNEL
);
205 md
->dev
= &pdev
->dev
;
207 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
208 if (res
->start
& 0xfff) {
209 /* For backward compatibility in case the
210 * base address is specified with an offset.
212 dev_info(&pdev
->dev
, "fix base address in dt-blob\n");
213 res
->start
&= ~0xfff;
214 res
->end
= res
->start
+ MDIO_REG_ADDR_SPACE_SIZE
- 1;
216 md
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
217 if (IS_ERR(md
->base
)) {
218 dev_err(&pdev
->dev
, "failed to ioremap register\n");
219 return PTR_ERR(md
->base
);
222 md
->mii_bus
= devm_mdiobus_alloc(&pdev
->dev
);
224 dev_err(&pdev
->dev
, "mdiomux bus alloc failed\n");
228 md
->core_clk
= devm_clk_get(&pdev
->dev
, NULL
);
229 if (md
->core_clk
== ERR_PTR(-ENOENT
) ||
230 md
->core_clk
== ERR_PTR(-EINVAL
))
232 else if (IS_ERR(md
->core_clk
))
233 return PTR_ERR(md
->core_clk
);
235 rc
= clk_prepare_enable(md
->core_clk
);
237 dev_err(&pdev
->dev
, "failed to enable core clk\n");
243 bus
->name
= "iProc MDIO mux bus";
244 snprintf(bus
->id
, MII_BUS_ID_SIZE
, "%s-%d", pdev
->name
, pdev
->id
);
245 bus
->parent
= &pdev
->dev
;
246 bus
->read
= iproc_mdiomux_read
;
247 bus
->write
= iproc_mdiomux_write
;
250 bus
->dev
.of_node
= pdev
->dev
.of_node
;
251 rc
= mdiobus_register(bus
);
253 dev_err(&pdev
->dev
, "mdiomux registration failed\n");
257 platform_set_drvdata(pdev
, md
);
259 rc
= mdio_mux_init(md
->dev
, md
->dev
->of_node
, mdio_mux_iproc_switch_fn
,
260 &md
->mux_handle
, md
, md
->mii_bus
);
262 dev_info(md
->dev
, "mdiomux initialization failed\n");
266 mdio_mux_iproc_config(md
);
268 dev_info(md
->dev
, "iProc mdiomux registered\n");
272 mdiobus_unregister(bus
);
274 clk_disable_unprepare(md
->core_clk
);
278 static int mdio_mux_iproc_remove(struct platform_device
*pdev
)
280 struct iproc_mdiomux_desc
*md
= platform_get_drvdata(pdev
);
282 mdio_mux_uninit(md
->mux_handle
);
283 mdiobus_unregister(md
->mii_bus
);
284 clk_disable_unprepare(md
->core_clk
);
289 #ifdef CONFIG_PM_SLEEP
290 static int mdio_mux_iproc_suspend(struct device
*dev
)
292 struct platform_device
*pdev
= to_platform_device(dev
);
293 struct iproc_mdiomux_desc
*md
= platform_get_drvdata(pdev
);
295 clk_disable_unprepare(md
->core_clk
);
300 static int mdio_mux_iproc_resume(struct device
*dev
)
302 struct platform_device
*pdev
= to_platform_device(dev
);
303 struct iproc_mdiomux_desc
*md
= platform_get_drvdata(pdev
);
306 rc
= clk_prepare_enable(md
->core_clk
);
308 dev_err(md
->dev
, "failed to enable core clk\n");
311 mdio_mux_iproc_config(md
);
317 static SIMPLE_DEV_PM_OPS(mdio_mux_iproc_pm_ops
,
318 mdio_mux_iproc_suspend
, mdio_mux_iproc_resume
);
320 static const struct of_device_id mdio_mux_iproc_match
[] = {
322 .compatible
= "brcm,mdio-mux-iproc",
326 MODULE_DEVICE_TABLE(of
, mdio_mux_iproc_match
);
328 static struct platform_driver mdiomux_iproc_driver
= {
330 .name
= "mdio-mux-iproc",
331 .of_match_table
= mdio_mux_iproc_match
,
332 .pm
= &mdio_mux_iproc_pm_ops
,
334 .probe
= mdio_mux_iproc_probe
,
335 .remove
= mdio_mux_iproc_remove
,
338 module_platform_driver(mdiomux_iproc_driver
);
340 MODULE_DESCRIPTION("iProc MDIO Mux Bus Driver");
341 MODULE_AUTHOR("Pramod Kumar <pramod.kumar@broadcom.com>");
342 MODULE_LICENSE("GPL v2");