Skip to content

Commit a13aff0

Browse files
jcmvbkbcdavem330
authored andcommitted
net: ethoc: set up MII management bus clock
MII management bus clock is derived from the MAC clock by dividing it by MIIMODER register CLKDIV field value. This value may need to be set up in case it is undefined or its default value is too high (and communication with PHY is too slow) or too low (and communication with PHY is impossible). The value of CLKDIV is not specified directly, but is derived from the MAC clock for the default MII management bus frequency of 2.5MHz. The MAC clock may be specified in the platform data, or in the 'clocks' device tree attribute. Signed-off-by: Max Filippov <jcmvbkbc@gmail.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 445a48c commit a13aff0

2 files changed

Lines changed: 31 additions & 2 deletions

File tree

‎drivers/net/ethernet/ethoc.c‎

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include <linux/dma-mapping.h>
1515
#include <linux/etherdevice.h>
16+
#include <linux/clk.h>
1617
#include <linux/crc32.h>
1718
#include <linux/interrupt.h>
1819
#include <linux/io.h>
@@ -219,6 +220,7 @@ struct ethoc {
219220

220221
struct phy_device *phy;
221222
struct mii_bus *mdio;
223+
struct clk *clk;
222224
s8 phy_id;
223225
};
224226

@@ -1021,6 +1023,8 @@ static int ethoc_probe(struct platform_device *pdev)
10211023
int num_bd;
10221024
int ret = 0;
10231025
bool random_mac = false;
1026+
struct ethoc_platform_data *pdata = dev_get_platdata(&pdev->dev);
1027+
u32 eth_clkfreq = pdata ? pdata->eth_clkfreq : 0;
10241028

10251029
/* allocate networking device */
10261030
netdev = alloc_etherdev(sizeof(struct ethoc));
@@ -1135,8 +1139,7 @@ static int ethoc_probe(struct platform_device *pdev)
11351139
}
11361140

11371141
/* Allow the platform setup code to pass in a MAC address. */
1138-
if (dev_get_platdata(&pdev->dev)) {
1139-
struct ethoc_platform_data *pdata = dev_get_platdata(&pdev->dev);
1142+
if (pdata) {
11401143
memcpy(netdev->dev_addr, pdata->hwaddr, IFHWADDRLEN);
11411144
priv->phy_id = pdata->phy_id;
11421145
} else {
@@ -1174,6 +1177,27 @@ static int ethoc_probe(struct platform_device *pdev)
11741177
if (random_mac)
11751178
netdev->addr_assign_type = NET_ADDR_RANDOM;
11761179

1180+
/* Allow the platform setup code to adjust MII management bus clock. */
1181+
if (!eth_clkfreq) {
1182+
struct clk *clk = devm_clk_get(&pdev->dev, NULL);
1183+
1184+
if (!IS_ERR(clk)) {
1185+
priv->clk = clk;
1186+
clk_prepare_enable(clk);
1187+
eth_clkfreq = clk_get_rate(clk);
1188+
}
1189+
}
1190+
if (eth_clkfreq) {
1191+
u32 clkdiv = MIIMODER_CLKDIV(eth_clkfreq / 2500000 + 1);
1192+
1193+
if (!clkdiv)
1194+
clkdiv = 2;
1195+
dev_dbg(&pdev->dev, "setting MII clkdiv to %u\n", clkdiv);
1196+
ethoc_write(priv, MIIMODER,
1197+
(ethoc_read(priv, MIIMODER) & MIIMODER_NOPRE) |
1198+
clkdiv);
1199+
}
1200+
11771201
/* register MII bus */
11781202
priv->mdio = mdiobus_alloc();
11791203
if (!priv->mdio) {
@@ -1239,6 +1263,8 @@ static int ethoc_probe(struct platform_device *pdev)
12391263
kfree(priv->mdio->irq);
12401264
mdiobus_free(priv->mdio);
12411265
free:
1266+
if (priv->clk)
1267+
clk_disable_unprepare(priv->clk);
12421268
free_netdev(netdev);
12431269
out:
12441270
return ret;
@@ -1263,6 +1289,8 @@ static int ethoc_remove(struct platform_device *pdev)
12631289
kfree(priv->mdio->irq);
12641290
mdiobus_free(priv->mdio);
12651291
}
1292+
if (priv->clk)
1293+
clk_disable_unprepare(priv->clk);
12661294
unregister_netdev(netdev);
12671295
free_netdev(netdev);
12681296
}

‎include/net/ethoc.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
struct ethoc_platform_data {
1717
u8 hwaddr[IFHWADDRLEN];
1818
s8 phy_id;
19+
u32 eth_clkfreq;
1920
};
2021

2122
#endif /* !LINUX_NET_ETHOC_H */

0 commit comments

Comments
 (0)