2 * Broadcom UniMAC MDIO bus controller driver
4 * Copyright (C) 2014-2017 Broadcom
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
12 #include <linux/kernel.h>
13 #include <linux/phy.h>
14 #include <linux/platform_device.h>
15 #include <linux/sched.h>
16 #include <linux/module.h>
18 #include <linux/delay.h>
19 #include <linux/clk.h>
22 #include <linux/of_platform.h>
23 #include <linux/of_mdio.h>
25 #include <linux/platform_data/mdio-bcm-unimac.h>
28 #define MDIO_START_BUSY (1 << 29)
29 #define MDIO_READ_FAIL (1 << 28)
30 #define MDIO_RD (2 << 26)
31 #define MDIO_WR (1 << 26)
32 #define MDIO_PMD_SHIFT 21
33 #define MDIO_PMD_MASK 0x1F
34 #define MDIO_REG_SHIFT 16
35 #define MDIO_REG_MASK 0x1F
38 #define MDIO_C22 (1 << 0)
40 #define MDIO_CLK_DIV_SHIFT 4
41 #define MDIO_CLK_DIV_MASK 0x3F
42 #define MDIO_SUPP_PREAMBLE (1 << 12)
44 struct unimac_mdio_priv {
45 struct mii_bus *mii_bus;
47 int (*wait_func) (void *wait_func_data);
53 static inline u32 unimac_mdio_readl(struct unimac_mdio_priv *priv, u32 offset)
55 /* MIPS chips strapped for BE will automagically configure the
56 * peripheral registers for CPU-native byte order.
58 if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
59 return __raw_readl(priv->base + offset);
61 return readl_relaxed(priv->base + offset);
64 static inline void unimac_mdio_writel(struct unimac_mdio_priv *priv, u32 val,
67 if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
68 __raw_writel(val, priv->base + offset);
70 writel_relaxed(val, priv->base + offset);
73 static inline void unimac_mdio_start(struct unimac_mdio_priv *priv)
77 reg = unimac_mdio_readl(priv, MDIO_CMD);
78 reg |= MDIO_START_BUSY;
79 unimac_mdio_writel(priv, reg, MDIO_CMD);
82 static inline unsigned int unimac_mdio_busy(struct unimac_mdio_priv *priv)
84 return unimac_mdio_readl(priv, MDIO_CMD) & MDIO_START_BUSY;
87 static int unimac_mdio_poll(void *wait_func_data)
89 struct unimac_mdio_priv *priv = wait_func_data;
90 unsigned int timeout = 1000;
93 if (!unimac_mdio_busy(priv))
96 usleep_range(1000, 2000);
105 static int unimac_mdio_read(struct mii_bus *bus, int phy_id, int reg)
107 struct unimac_mdio_priv *priv = bus->priv;
111 /* Prepare the read operation */
112 cmd = MDIO_RD | (phy_id << MDIO_PMD_SHIFT) | (reg << MDIO_REG_SHIFT);
113 unimac_mdio_writel(priv, cmd, MDIO_CMD);
115 /* Start MDIO transaction */
116 unimac_mdio_start(priv);
118 ret = priv->wait_func(priv->wait_func_data);
122 cmd = unimac_mdio_readl(priv, MDIO_CMD);
124 /* Some broken devices are known not to release the line during
125 * turn-around, e.g: Broadcom BCM53125 external switches, so check for
126 * that condition here and ignore the MDIO controller read failure
129 if (!(bus->phy_ignore_ta_mask & 1 << phy_id) && (cmd & MDIO_READ_FAIL))
135 static int unimac_mdio_write(struct mii_bus *bus, int phy_id,
138 struct unimac_mdio_priv *priv = bus->priv;
141 /* Prepare the write operation */
142 cmd = MDIO_WR | (phy_id << MDIO_PMD_SHIFT) |
143 (reg << MDIO_REG_SHIFT) | (0xffff & val);
144 unimac_mdio_writel(priv, cmd, MDIO_CMD);
146 unimac_mdio_start(priv);
148 return priv->wait_func(priv->wait_func_data);
151 /* Workaround for integrated BCM7xxx Gigabit PHYs which have a problem with
152 * their internal MDIO management controller making them fail to successfully
153 * be read from or written to for the first transaction. We insert a dummy
154 * BMSR read here to make sure that phy_get_device() and get_phy_id() can
155 * correctly read the PHY MII_PHYSID1/2 registers and successfully register a
156 * PHY device for this peripheral.
158 * Once the PHY driver is registered, we can workaround subsequent reads from
159 * there (e.g: during system-wide power management).
161 * bus->reset is invoked before mdiobus_scan during mdiobus_register and is
162 * therefore the right location to stick that workaround. Since we do not want
163 * to read from non-existing PHYs, we either use bus->phy_mask or do a manual
164 * Device Tree scan to limit the search area.
166 static int unimac_mdio_reset(struct mii_bus *bus)
168 struct device_node *np = bus->dev.of_node;
169 struct device_node *child;
174 read_mask = ~bus->phy_mask;
176 for_each_available_child_of_node(np, child) {
177 addr = of_mdio_parse_addr(&bus->dev, child);
181 read_mask |= 1 << addr;
185 for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
186 if (read_mask & 1 << addr) {
187 dev_dbg(&bus->dev, "Workaround for PHY @ %d\n", addr);
188 mdiobus_read(bus, addr, MII_BMSR);
195 static void unimac_mdio_clk_set(struct unimac_mdio_priv *priv)
200 /* Keep the hardware default values */
207 rate = clk_get_rate(priv->clk);
209 div = (rate / (2 * priv->clk_freq)) - 1;
210 if (div & ~MDIO_CLK_DIV_MASK) {
211 pr_warn("Incorrect MDIO clock frequency, ignoring\n");
215 /* The MDIO clock is the reference clock (typicaly 250Mhz) divided by
216 * 2 x (MDIO_CLK_DIV + 1)
218 reg = unimac_mdio_readl(priv, MDIO_CFG);
219 reg &= ~(MDIO_CLK_DIV_MASK << MDIO_CLK_DIV_SHIFT);
220 reg |= div << MDIO_CLK_DIV_SHIFT;
221 unimac_mdio_writel(priv, reg, MDIO_CFG);
224 static int unimac_mdio_probe(struct platform_device *pdev)
226 struct unimac_mdio_pdata *pdata = pdev->dev.platform_data;
227 struct unimac_mdio_priv *priv;
228 struct device_node *np;
233 np = pdev->dev.of_node;
235 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
239 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
243 /* Just ioremap, as this MDIO block is usually integrated into an
244 * Ethernet MAC controller register range
246 priv->base = devm_ioremap(&pdev->dev, r->start, resource_size(r));
248 dev_err(&pdev->dev, "failed to remap register\n");
252 priv->clk = devm_clk_get(&pdev->dev, NULL);
253 if (PTR_ERR(priv->clk) == -EPROBE_DEFER)
254 return PTR_ERR(priv->clk);
258 ret = clk_prepare_enable(priv->clk);
262 if (of_property_read_u32(np, "clock-frequency", &priv->clk_freq))
265 unimac_mdio_clk_set(priv);
267 priv->mii_bus = mdiobus_alloc();
268 if (!priv->mii_bus) {
270 goto out_clk_disable;
276 bus->name = pdata->bus_name;
277 priv->wait_func = pdata->wait_func;
278 priv->wait_func_data = pdata->wait_func_data;
279 bus->phy_mask = ~pdata->phy_mask;
281 bus->name = "unimac MII bus";
282 priv->wait_func_data = priv;
283 priv->wait_func = unimac_mdio_poll;
285 bus->parent = &pdev->dev;
286 bus->read = unimac_mdio_read;
287 bus->write = unimac_mdio_write;
288 bus->reset = unimac_mdio_reset;
289 snprintf(bus->id, MII_BUS_ID_SIZE, "%s-%d", pdev->name, pdev->id);
291 ret = of_mdiobus_register(bus, np);
293 dev_err(&pdev->dev, "MDIO bus registration failed\n");
297 platform_set_drvdata(pdev, priv);
299 dev_info(&pdev->dev, "Broadcom UniMAC MDIO bus at 0x%p\n", priv->base);
306 clk_disable_unprepare(priv->clk);
310 static int unimac_mdio_remove(struct platform_device *pdev)
312 struct unimac_mdio_priv *priv = platform_get_drvdata(pdev);
314 mdiobus_unregister(priv->mii_bus);
315 mdiobus_free(priv->mii_bus);
316 clk_disable_unprepare(priv->clk);
321 static int __maybe_unused unimac_mdio_suspend(struct device *d)
323 struct unimac_mdio_priv *priv = dev_get_drvdata(d);
325 clk_disable_unprepare(priv->clk);
330 static int __maybe_unused unimac_mdio_resume(struct device *d)
332 struct unimac_mdio_priv *priv = dev_get_drvdata(d);
335 ret = clk_prepare_enable(priv->clk);
339 unimac_mdio_clk_set(priv);
344 static SIMPLE_DEV_PM_OPS(unimac_mdio_pm_ops,
345 unimac_mdio_suspend, unimac_mdio_resume);
347 static const struct of_device_id unimac_mdio_ids[] = {
348 { .compatible = "brcm,genet-mdio-v5", },
349 { .compatible = "brcm,genet-mdio-v4", },
350 { .compatible = "brcm,genet-mdio-v3", },
351 { .compatible = "brcm,genet-mdio-v2", },
352 { .compatible = "brcm,genet-mdio-v1", },
353 { .compatible = "brcm,unimac-mdio", },
356 MODULE_DEVICE_TABLE(of, unimac_mdio_ids);
358 static struct platform_driver unimac_mdio_driver = {
360 .name = UNIMAC_MDIO_DRV_NAME,
361 .of_match_table = unimac_mdio_ids,
362 .pm = &unimac_mdio_pm_ops,
364 .probe = unimac_mdio_probe,
365 .remove = unimac_mdio_remove,
367 module_platform_driver(unimac_mdio_driver);
369 MODULE_AUTHOR("Broadcom Corporation");
370 MODULE_DESCRIPTION("Broadcom UniMAC MDIO bus controller");
371 MODULE_LICENSE("GPL");
372 MODULE_ALIAS("platform:" UNIMAC_MDIO_DRV_NAME);