Skip to content

Commit 113ce10

Browse files
Gavin Shandavem330
authored andcommitted
net/faraday: Read MAC address from chip
The device is assigned with random MAC address. It isn't reasonable. An valid MAC address might have been provided by (uboot) firmware by device-tree or in chip. It's reasonable to use it to maintain consistency. This uses the MAC address from device-tree or that in the chip if it's valid. Otherwise, a random MAC address is given as before. Signed-off-by: Gavin Shan <gwshan@linux.vnet.ibm.com> Acked-by: Joel Stanley <joel@jms.id.au> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent eb41818 commit 113ce10

File tree

1 file changed

+62
-7
lines changed

1 file changed

+62
-7
lines changed

drivers/net/ethernet/faraday/ftgmac100.c

Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,64 @@ static void ftgmac100_set_mac(struct ftgmac100 *priv, const unsigned char *mac)
141141
iowrite32(laddr, priv->base + FTGMAC100_OFFSET_MAC_LADR);
142142
}
143143

144+
static void ftgmac100_setup_mac(struct ftgmac100 *priv)
145+
{
146+
u8 mac[ETH_ALEN];
147+
unsigned int m;
148+
unsigned int l;
149+
void *addr;
150+
151+
addr = device_get_mac_address(priv->dev, mac, ETH_ALEN);
152+
if (addr) {
153+
ether_addr_copy(priv->netdev->dev_addr, mac);
154+
dev_info(priv->dev, "Read MAC address %pM from device tree\n",
155+
mac);
156+
return;
157+
}
158+
159+
m = ioread32(priv->base + FTGMAC100_OFFSET_MAC_MADR);
160+
l = ioread32(priv->base + FTGMAC100_OFFSET_MAC_LADR);
161+
162+
mac[0] = (m >> 8) & 0xff;
163+
mac[1] = m & 0xff;
164+
mac[2] = (l >> 24) & 0xff;
165+
mac[3] = (l >> 16) & 0xff;
166+
mac[4] = (l >> 8) & 0xff;
167+
mac[5] = l & 0xff;
168+
169+
if (!is_valid_ether_addr(mac)) {
170+
mac[5] = (m >> 8) & 0xff;
171+
mac[4] = m & 0xff;
172+
mac[3] = (l >> 24) & 0xff;
173+
mac[2] = (l >> 16) & 0xff;
174+
mac[1] = (l >> 8) & 0xff;
175+
mac[0] = l & 0xff;
176+
}
177+
178+
if (is_valid_ether_addr(mac)) {
179+
ether_addr_copy(priv->netdev->dev_addr, mac);
180+
dev_info(priv->dev, "Read MAC address %pM from chip\n", mac);
181+
} else {
182+
eth_hw_addr_random(priv->netdev);
183+
dev_info(priv->dev, "Generated random MAC address %pM\n",
184+
priv->netdev->dev_addr);
185+
}
186+
}
187+
188+
static int ftgmac100_set_mac_addr(struct net_device *dev, void *p)
189+
{
190+
int ret;
191+
192+
ret = eth_prepare_mac_addr_change(dev, p);
193+
if (ret < 0)
194+
return ret;
195+
196+
eth_commit_mac_addr_change(dev, p);
197+
ftgmac100_set_mac(netdev_priv(dev), dev->dev_addr);
198+
199+
return 0;
200+
}
201+
144202
static void ftgmac100_init_hw(struct ftgmac100 *priv)
145203
{
146204
/* setup ring buffer base registers */
@@ -1141,7 +1199,7 @@ static const struct net_device_ops ftgmac100_netdev_ops = {
11411199
.ndo_open = ftgmac100_open,
11421200
.ndo_stop = ftgmac100_stop,
11431201
.ndo_start_xmit = ftgmac100_hard_start_xmit,
1144-
.ndo_set_mac_address = eth_mac_addr,
1202+
.ndo_set_mac_address = ftgmac100_set_mac_addr,
11451203
.ndo_validate_addr = eth_validate_addr,
11461204
.ndo_do_ioctl = ftgmac100_do_ioctl,
11471205
};
@@ -1265,6 +1323,9 @@ static int ftgmac100_probe(struct platform_device *pdev)
12651323

12661324
priv->irq = irq;
12671325

1326+
/* MAC address from chip or random one */
1327+
ftgmac100_setup_mac(priv);
1328+
12681329
err = ftgmac100_setup_mdio(netdev);
12691330
if (err)
12701331
goto err_setup_mdio;
@@ -1278,12 +1339,6 @@ static int ftgmac100_probe(struct platform_device *pdev)
12781339

12791340
netdev_info(netdev, "irq %d, mapped at %p\n", priv->irq, priv->base);
12801341

1281-
if (!is_valid_ether_addr(netdev->dev_addr)) {
1282-
eth_hw_addr_random(netdev);
1283-
netdev_info(netdev, "generated random MAC address %pM\n",
1284-
netdev->dev_addr);
1285-
}
1286-
12871342
return 0;
12881343

12891344
err_register_netdev:

0 commit comments

Comments
 (0)