History

net: mdio: mdio-bcm-unimac: Manage clock around I/O accesses

Up until now we have managed not to have the mdio-bcm-unimac manage its clock except during probe and suspend/resume. This works most of the time, except where it does not. With a fully modular build, we can get into a situation whereby the GENET driver is fully registered, and so is the mdio-bcm-unimac driver, however the Ethernet PHY driver is not yet, because it depends on a resource that is not yet available (e.g.: GPIO provider). In that state, the network device is not usable yet, and so to conserve power, the GENET driver will have turned off its "main" clock which feeds its MDIO controller. When the PHY driver finally probes however, we make an access to the PHY registers to e.g.: disable interrupts, and this causes a bus error within the MDIO controller space because the MDIO controller clock(s) are turned off. To remedy that, we manage the clock around all of the I/O accesses to the hardware which are done exclusively during read, write and clock divider configuration. This ensures that the register space is accessible, and this also ensures that there are not unnecessarily elevated reference counts keeping the clocks active when the network device is administratively turned off. It would be the case with the previous way of managing the clock.

Florian Fainelli committed 2 years ago

Commit ee975351 parent 78b88ef2 files · +53 −43Browse files

Unified · Split

2 files
  1. drivers/net/mdio/mdio-bcm-unimac.c +50 −43
  2. include/linux/platform_data/mdio-bcm-unimac.h +3 −0
drivers/net/mdio/mdio-bcm-unimac.c+50−43
@@ -94,6 +94,10 @@ static int unimac_mdio_read(struct mii_bus *bus, int phy_id, int reg) · 93 unmodified lines
94 int ret;94 int ret;
95 u32 cmd;95 u32 cmd;
96 96
97 ret = clk_prepare_enable(priv->clk);
98 if (ret)
99 return ret;
100
97 /* Prepare the read operation */101 /* Prepare the read operation */
98 cmd = MDIO_RD | (phy_id << MDIO_PMD_SHIFT) | (reg << MDIO_REG_SHIFT);102 cmd = MDIO_RD | (phy_id << MDIO_PMD_SHIFT) | (reg << MDIO_REG_SHIFT);
99 unimac_mdio_writel(priv, cmd, MDIO_CMD);103 unimac_mdio_writel(priv, cmd, MDIO_CMD);
@@ -103,7 +107,7 @@ static int unimac_mdio_read(struct mii_bus *bus, int phy_id, int reg) · 3 unmodified lines
103 107
104 ret = priv->wait_func(priv->wait_func_data);108 ret = priv->wait_func(priv->wait_func_data);
105 if (ret)109 if (ret)
106 return ret;110 goto out;
107 111
108 cmd = unimac_mdio_readl(priv, MDIO_CMD);112 cmd = unimac_mdio_readl(priv, MDIO_CMD);
109 113
@@ -112,10 +116,15 @@ static int unimac_mdio_read(struct mii_bus *bus, int phy_id, int reg) · 2 unmodified lines
112 * that condition here and ignore the MDIO controller read failure116 * that condition here and ignore the MDIO controller read failure
113 * indication.117 * indication.
114 */118 */
115 if (!(bus->phy_ignore_ta_mask & 1 << phy_id) && (cmd & MDIO_READ_FAIL))119 if (!(bus->phy_ignore_ta_mask & 1 << phy_id) && (cmd & MDIO_READ_FAIL)) {
116 return -EIO;120 ret = -EIO;
121 goto out;
122 }
117 123
118 return cmd & 0xffff;124 ret = cmd & 0xffff;
125out:
126 clk_disable_unprepare(priv->clk);
127 return ret;
119}128}
120 129
121static int unimac_mdio_write(struct mii_bus *bus, int phy_id,130static int unimac_mdio_write(struct mii_bus *bus, int phy_id,
@@ -123,6 +132,11 @@ static int unimac_mdio_write(struct mii_bus *bus, int phy_id, · 1 unmodified line
123{132{
124 struct unimac_mdio_priv *priv = bus->priv;133 struct unimac_mdio_priv *priv = bus->priv;
125 u32 cmd;134 u32 cmd;
135 int ret;
136
137 ret = clk_prepare_enable(priv->clk);
138 if (ret)
139 return ret;
126 140
127 /* Prepare the write operation */141 /* Prepare the write operation */
128 cmd = MDIO_WR | (phy_id << MDIO_PMD_SHIFT) |142 cmd = MDIO_WR | (phy_id << MDIO_PMD_SHIFT) |
@@ -131,7 +145,10 @@ static int unimac_mdio_write(struct mii_bus *bus, int phy_id, · 2 unmodified lines
131 145
132 unimac_mdio_start(priv);146 unimac_mdio_start(priv);
133 147
134 return priv->wait_func(priv->wait_func_data);148 ret = priv->wait_func(priv->wait_func_data);
149 clk_disable_unprepare(priv->clk);
150
151 return ret;
135}152}
136 153
137/* Workaround for integrated BCM7xxx Gigabit PHYs which have a problem with154/* Workaround for integrated BCM7xxx Gigabit PHYs which have a problem with
@@ -178,14 +195,19 @@ static int unimac_mdio_reset(struct mii_bus *bus) · 40 unmodified lines
178 return 0;195 return 0;
179}196}
180 197
181static void unimac_mdio_clk_set(struct unimac_mdio_priv *priv)198static int unimac_mdio_clk_set(struct unimac_mdio_priv *priv)
182{199{
183 unsigned long rate;200 unsigned long rate;
184 u32 reg, div;201 u32 reg, div;
202 int ret;
185 203
186 /* Keep the hardware default values */204 /* Keep the hardware default values */
187 if (!priv->clk_freq)205 if (!priv->clk_freq)
188 return;206 return 0;
207
208 ret = clk_prepare_enable(priv->clk);
209 if (ret)
210 return ret;
189 211
190 if (!priv->clk)212 if (!priv->clk)
191 rate = 250000000;213 rate = 250000000;
@@ -195,7 +217,8 @@ static void unimac_mdio_clk_set(struct unimac_mdio_priv *priv) · 3 unmodified lines
195 div = (rate / (2 * priv->clk_freq)) - 1;217 div = (rate / (2 * priv->clk_freq)) - 1;
196 if (div & ~MDIO_CLK_DIV_MASK) {218 if (div & ~MDIO_CLK_DIV_MASK) {
197 pr_warn("Incorrect MDIO clock frequency, ignoring\n");219 pr_warn("Incorrect MDIO clock frequency, ignoring\n");
198 return;220 ret = 0;
221 goto out;
199 }222 }
200 223
201 /* The MDIO clock is the reference clock (typically 250Mhz) divided by224 /* The MDIO clock is the reference clock (typically 250Mhz) divided by
@@ -205,6 +228,9 @@ static void unimac_mdio_clk_set(struct unimac_mdio_priv *priv) · 3 unmodified lines
205 reg &= ~(MDIO_CLK_DIV_MASK << MDIO_CLK_DIV_SHIFT);228 reg &= ~(MDIO_CLK_DIV_MASK << MDIO_CLK_DIV_SHIFT);
206 reg |= div << MDIO_CLK_DIV_SHIFT;229 reg |= div << MDIO_CLK_DIV_SHIFT;
207 unimac_mdio_writel(priv, reg, MDIO_CFG);230 unimac_mdio_writel(priv, reg, MDIO_CFG);
231out:
232 clk_disable_unprepare(priv->clk);
233 return ret;
208}234}
209 235
210static int unimac_mdio_probe(struct platform_device *pdev)236static int unimac_mdio_probe(struct platform_device *pdev)
@@ -235,24 +261,12 @@ static int unimac_mdio_probe(struct platform_device *pdev) · 24 unmodified lines
235 return -ENOMEM;261 return -ENOMEM;
236 }262 }
237 263
238 priv->clk = devm_clk_get_optional(&pdev->dev, NULL);
239 if (IS_ERR(priv->clk))
240 return PTR_ERR(priv->clk);
241
242 ret = clk_prepare_enable(priv->clk);
243 if (ret)
244 return ret;
245
246 if (of_property_read_u32(np, "clock-frequency", &priv->clk_freq))264 if (of_property_read_u32(np, "clock-frequency", &priv->clk_freq))
247 priv->clk_freq = 0;265 priv->clk_freq = 0;
248 266
249 unimac_mdio_clk_set(priv);
250
251 priv->mii_bus = mdiobus_alloc();267 priv->mii_bus = mdiobus_alloc();
252 if (!priv->mii_bus) {268 if (!priv->mii_bus)
253 ret = -ENOMEM;269 return -ENOMEM;
254 goto out_clk_disable;
255 }
256 270
257 bus = priv->mii_bus;271 bus = priv->mii_bus;
258 bus->priv = priv;272 bus->priv = priv;
@@ -261,17 +275,29 @@ static int unimac_mdio_probe(struct platform_device *pdev) · 2 unmodified lines
261 priv->wait_func = pdata->wait_func;275 priv->wait_func = pdata->wait_func;
262 priv->wait_func_data = pdata->wait_func_data;276 priv->wait_func_data = pdata->wait_func_data;
263 bus->phy_mask = ~pdata->phy_mask;277 bus->phy_mask = ~pdata->phy_mask;
278 priv->clk = pdata->clk;
264 } else {279 } else {
265 bus->name = "unimac MII bus";280 bus->name = "unimac MII bus";
266 priv->wait_func_data = priv;281 priv->wait_func_data = priv;
267 priv->wait_func = unimac_mdio_poll;282 priv->wait_func = unimac_mdio_poll;
283 priv->clk = devm_clk_get_optional(&pdev->dev, NULL);
284 }
285
286 if (IS_ERR(priv->clk)) {
287 ret = PTR_ERR(priv->clk);
288 goto out_mdio_free;
268 }289 }
290
269 bus->parent = &pdev->dev;291 bus->parent = &pdev->dev;
270 bus->read = unimac_mdio_read;292 bus->read = unimac_mdio_read;
271 bus->write = unimac_mdio_write;293 bus->write = unimac_mdio_write;
272 bus->reset = unimac_mdio_reset;294 bus->reset = unimac_mdio_reset;
273 snprintf(bus->id, MII_BUS_ID_SIZE, "%s-%d", pdev->name, pdev->id);295 snprintf(bus->id, MII_BUS_ID_SIZE, "%s-%d", pdev->name, pdev->id);
274 296
297 ret = unimac_mdio_clk_set(priv);
298 if (ret)
299 goto out_mdio_free;
300
275 ret = of_mdiobus_register(bus, np);301 ret = of_mdiobus_register(bus, np);
276 if (ret) {302 if (ret) {
277 dev_err(&pdev->dev, "MDIO bus registration failed\n");303 dev_err(&pdev->dev, "MDIO bus registration failed\n");
@@ -286,8 +312,6 @@ static int unimac_mdio_probe(struct platform_device *pdev) · 8 unmodified lines
286 312
287out_mdio_free:313out_mdio_free:
288 mdiobus_free(bus);314 mdiobus_free(bus);
289out_clk_disable:
290 clk_disable_unprepare(priv->clk);
291 return ret;315 return ret;
292}316}
293 317
@@ -297,34 +321,17 @@ static void unimac_mdio_remove(struct platform_device *pdev) · 3 unmodified lines
297 321
298 mdiobus_unregister(priv->mii_bus);322 mdiobus_unregister(priv->mii_bus);
299 mdiobus_free(priv->mii_bus);323 mdiobus_free(priv->mii_bus);
300 clk_disable_unprepare(priv->clk);
301}
302
303static int __maybe_unused unimac_mdio_suspend(struct device *d)
304{
305 struct unimac_mdio_priv *priv = dev_get_drvdata(d);
306
307 clk_disable_unprepare(priv->clk);
308
309 return 0;
310}324}
311 325
312static int __maybe_unused unimac_mdio_resume(struct device *d)326static int __maybe_unused unimac_mdio_resume(struct device *d)
313{327{
314 struct unimac_mdio_priv *priv = dev_get_drvdata(d);328 struct unimac_mdio_priv *priv = dev_get_drvdata(d);
315 int ret;
316 329
317 ret = clk_prepare_enable(priv->clk);330 return unimac_mdio_clk_set(priv);
318 if (ret)
319 return ret;
320
321 unimac_mdio_clk_set(priv);
322
323 return 0;
324}331}
325 332
326static SIMPLE_DEV_PM_OPS(unimac_mdio_pm_ops,333static SIMPLE_DEV_PM_OPS(unimac_mdio_pm_ops,
327 unimac_mdio_suspend, unimac_mdio_resume);334 NULL, unimac_mdio_resume);
328 335
329static const struct of_device_id unimac_mdio_ids[] = {336static const struct of_device_id unimac_mdio_ids[] = {
330 { .compatible = "brcm,asp-v2.1-mdio", },337 { .compatible = "brcm,asp-v2.1-mdio", },
include/linux/platform_data/mdio-bcm-unimac.h+3−0
@@ -1,11 +1,14 @@
1#ifndef __MDIO_BCM_UNIMAC_PDATA_H1#ifndef __MDIO_BCM_UNIMAC_PDATA_H
2#define __MDIO_BCM_UNIMAC_PDATA_H2#define __MDIO_BCM_UNIMAC_PDATA_H
3 3
4struct clk;
5
4struct unimac_mdio_pdata {6struct unimac_mdio_pdata {
5 u32 phy_mask;7 u32 phy_mask;
6 int (*wait_func)(void *data);8 int (*wait_func)(void *data);
7 void *wait_func_data;9 void *wait_func_data;
8 const char *bus_name;10 const char *bus_name;
11 struct clk *clk;
9};12};
10 13
11#define UNIMAC_MDIO_DRV_NAME "unimac-mdio"14#define UNIMAC_MDIO_DRV_NAME "unimac-mdio"

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t