1 From 86f4ea63e696db996f68d1065b55506e75a7d765 Mon Sep 17 00:00:00 2001
2 From: Hauke Mehrtens <hauke@hauke-m.de>
3 Date: Fri, 20 Dec 2013 02:16:10 +0100
4 Subject: [PATCH 205/208] b44: add phylib support
6 Most of the older home routers based on the Broadcom BCM47XX SoC series
7 are using a MAC that is supported by b44. On most of these routers not
8 the internal PHY of this MAC core is used, but a switch sometimes on an
9 external chip or integrated into the same SoC as the Ethernet core.
10 For this switch a special PHY driver is needed which should not be
11 integrated into b44 as the same switches are also used by other
12 Broadcom home networking SoCs which are using different Ethernet MAC
13 drivers. This was tested with the b53 switch driver which is currently
14 on its way to mainline.
16 If the internal PHY is not used, b44 will now search on the MDIO bus
17 for a phy and use the Linux phylib subsystem to register a driver.
18 Support for the internal PHY must stay here, because there are some
19 device which are suing the internal phy.
21 With this patch we scan the mdio bus when the sprom or nvram says that
22 the PHY address is 30, if a PHY was found at this address b44 uses it.
24 This was tested with a BCM4704, BCM4712 and BCM5354.
26 Signed-off-by: Hauke Mehrtens <hauke@hauke-m.de>
27 Signed-off-by: David S. Miller <davem@davemloft.net>
29 drivers/net/ethernet/broadcom/Kconfig | 1 +
30 drivers/net/ethernet/broadcom/b44.c | 190 ++++++++++++++++++++++++++++++++-
31 drivers/net/ethernet/broadcom/b44.h | 3 +
32 3 files changed, 189 insertions(+), 5 deletions(-)
34 --- a/drivers/net/ethernet/broadcom/Kconfig
35 +++ b/drivers/net/ethernet/broadcom/Kconfig
36 @@ -24,6 +24,7 @@ config B44
42 If you have a network (Ethernet) controller of this type, say Y
43 or M and read the Ethernet-HOWTO, available from
44 --- a/drivers/net/ethernet/broadcom/b44.c
45 +++ b/drivers/net/ethernet/broadcom/b44.c
47 * Copyright (C) 2006 Felix Fietkau (nbd@openwrt.org)
48 * Copyright (C) 2006 Broadcom Corporation.
49 * Copyright (C) 2007 Michael Buesch <m@bues.ch>
50 + * Copyright (C) 2013 Hauke Mehrtens <hauke@hauke-m.de>
52 * Distribute under GPL.
55 #include <linux/dma-mapping.h>
56 #include <linux/ssb/ssb.h>
57 #include <linux/slab.h>
58 +#include <linux/phy.h>
60 #include <asm/uaccess.h>
62 @@ -316,6 +318,23 @@ static void b44_mdio_write_mii(struct ne
63 __b44_writephy(bp, phy_id, location, val);
66 +static int b44_mdio_read_phylib(struct mii_bus *bus, int phy_id, int location)
69 + struct b44 *bp = bus->priv;
70 + int rc = __b44_readphy(bp, phy_id, location, &val);
76 +static int b44_mdio_write_phylib(struct mii_bus *bus, int phy_id, int location,
79 + struct b44 *bp = bus->priv;
80 + return __b44_writephy(bp, phy_id, location, val);
83 static int b44_phy_reset(struct b44 *bp)
86 @@ -523,10 +542,12 @@ static void b44_check_phy(struct b44 *bp
88 if (bp->flags & B44_FLAG_EXTERNAL_PHY) {
89 bp->flags |= B44_FLAG_100_BASE_T;
90 - bp->flags |= B44_FLAG_FULL_DUPLEX;
91 if (!netif_carrier_ok(bp->dev)) {
92 u32 val = br32(bp, B44_TX_CTRL);
93 - val |= TX_CTRL_DUPLEX;
94 + if (bp->flags & B44_FLAG_FULL_DUPLEX)
95 + val |= TX_CTRL_DUPLEX;
97 + val &= ~TX_CTRL_DUPLEX;
98 bw32(bp, B44_TX_CTRL, val);
99 netif_carrier_on(bp->dev);
101 @@ -1805,6 +1826,11 @@ static int b44_get_settings(struct net_d
103 struct b44 *bp = netdev_priv(dev);
105 + if (bp->flags & B44_FLAG_EXTERNAL_PHY) {
106 + BUG_ON(!bp->phydev);
107 + return phy_ethtool_gset(bp->phydev, cmd);
110 cmd->supported = (SUPPORTED_Autoneg);
111 cmd->supported |= (SUPPORTED_100baseT_Half |
112 SUPPORTED_100baseT_Full |
113 @@ -1846,7 +1872,23 @@ static int b44_get_settings(struct net_d
114 static int b44_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
116 struct b44 *bp = netdev_priv(dev);
117 - u32 speed = ethtool_cmd_speed(cmd);
121 + if (bp->flags & B44_FLAG_EXTERNAL_PHY) {
122 + BUG_ON(!bp->phydev);
123 + spin_lock_irq(&bp->lock);
124 + if (netif_running(dev))
127 + ret = phy_ethtool_sset(bp->phydev, cmd);
129 + spin_unlock_irq(&bp->lock);
134 + speed = ethtool_cmd_speed(cmd);
136 /* We do not support gigabit. */
137 if (cmd->autoneg == AUTONEG_ENABLE) {
138 @@ -2076,7 +2118,6 @@ static const struct ethtool_ops b44_etht
140 static int b44_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
142 - struct mii_ioctl_data *data = if_mii(ifr);
143 struct b44 *bp = netdev_priv(dev);
146 @@ -2084,7 +2125,12 @@ static int b44_ioctl(struct net_device *
149 spin_lock_irq(&bp->lock);
150 - err = generic_mii_ioctl(&bp->mii_if, data, cmd, NULL);
151 + if (bp->flags & B44_FLAG_EXTERNAL_PHY) {
152 + BUG_ON(!bp->phydev);
153 + err = phy_mii_ioctl(bp->phydev, ifr, cmd);
155 + err = generic_mii_ioctl(&bp->mii_if, if_mii(ifr), cmd, NULL);
157 spin_unlock_irq(&bp->lock);
160 @@ -2146,6 +2192,127 @@ static const struct net_device_ops b44_n
164 +static void b44_adjust_link(struct net_device *dev)
166 + struct b44 *bp = netdev_priv(dev);
167 + struct phy_device *phydev = bp->phydev;
168 + bool status_changed = 0;
172 + if (bp->old_link != phydev->link) {
173 + status_changed = 1;
174 + bp->old_link = phydev->link;
177 + /* reflect duplex change */
178 + if (phydev->link) {
179 + if ((phydev->duplex == DUPLEX_HALF) &&
180 + (bp->flags & B44_FLAG_FULL_DUPLEX)) {
181 + status_changed = 1;
182 + bp->flags &= ~B44_FLAG_FULL_DUPLEX;
183 + } else if ((phydev->duplex == DUPLEX_FULL) &&
184 + !(bp->flags & B44_FLAG_FULL_DUPLEX)) {
185 + status_changed = 1;
186 + bp->flags |= B44_FLAG_FULL_DUPLEX;
190 + if (status_changed) {
192 + phy_print_status(phydev);
196 +static int b44_register_phy_one(struct b44 *bp)
198 + struct mii_bus *mii_bus;
199 + struct ssb_device *sdev = bp->sdev;
200 + struct phy_device *phydev;
201 + char bus_id[MII_BUS_ID_SIZE + 3];
204 + mii_bus = mdiobus_alloc();
206 + dev_err(sdev->dev, "mdiobus_alloc() failed\n");
211 + mii_bus->priv = bp;
212 + mii_bus->read = b44_mdio_read_phylib;
213 + mii_bus->write = b44_mdio_write_phylib;
214 + mii_bus->name = "b44_eth_mii";
215 + mii_bus->parent = sdev->dev;
216 + mii_bus->phy_mask = ~(1 << bp->phy_addr);
217 + snprintf(mii_bus->id, MII_BUS_ID_SIZE, "%x", instance);
218 + mii_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
219 + if (!mii_bus->irq) {
220 + dev_err(sdev->dev, "mii_bus irq allocation failed\n");
222 + goto err_out_mdiobus;
225 + memset(mii_bus->irq, PHY_POLL, sizeof(int) * PHY_MAX_ADDR);
227 + bp->mii_bus = mii_bus;
229 + err = mdiobus_register(mii_bus);
231 + dev_err(sdev->dev, "failed to register MII bus\n");
232 + goto err_out_mdiobus_irq;
235 + snprintf(bus_id, sizeof(bus_id), PHY_ID_FMT, mii_bus->id, bp->phy_addr);
237 + phydev = phy_connect(bp->dev, bus_id, &b44_adjust_link,
238 + PHY_INTERFACE_MODE_MII);
239 + if (IS_ERR(phydev)) {
240 + dev_err(sdev->dev, "could not attach PHY at %i\n",
242 + err = PTR_ERR(phydev);
243 + goto err_out_mdiobus_unregister;
246 + /* mask with MAC supported features */
247 + phydev->supported &= (SUPPORTED_100baseT_Half |
248 + SUPPORTED_100baseT_Full |
249 + SUPPORTED_Autoneg |
251 + phydev->advertising = phydev->supported;
253 + bp->phydev = phydev;
255 + bp->phy_addr = phydev->addr;
257 + dev_info(sdev->dev, "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
258 + phydev->drv->name, dev_name(&phydev->dev));
262 +err_out_mdiobus_unregister:
263 + mdiobus_unregister(mii_bus);
265 +err_out_mdiobus_irq:
266 + kfree(mii_bus->irq);
269 + mdiobus_free(mii_bus);
275 +static void b44_unregister_phy_one(struct b44 *bp)
277 + struct mii_bus *mii_bus = bp->mii_bus;
279 + phy_disconnect(bp->phydev);
280 + mdiobus_unregister(mii_bus);
281 + kfree(mii_bus->irq);
282 + mdiobus_free(mii_bus);
285 static int b44_init_one(struct ssb_device *sdev,
286 const struct ssb_device_id *ent)
288 @@ -2246,10 +2413,20 @@ static int b44_init_one(struct ssb_devic
289 if (b44_phy_reset(bp) < 0)
290 bp->phy_addr = B44_PHY_ADDR_NO_LOCAL_PHY;
292 + if (bp->flags & B44_FLAG_EXTERNAL_PHY) {
293 + err = b44_register_phy_one(bp);
295 + dev_err(sdev->dev, "Cannot register PHY, aborting\n");
296 + goto err_out_unregister_netdev;
300 netdev_info(dev, "%s %pM\n", DRV_DESCRIPTION, dev->dev_addr);
304 +err_out_unregister_netdev:
305 + unregister_netdev(dev);
307 ssb_bus_may_powerdown(sdev->bus);
309 @@ -2263,8 +2440,11 @@ out:
310 static void b44_remove_one(struct ssb_device *sdev)
312 struct net_device *dev = ssb_get_drvdata(sdev);
313 + struct b44 *bp = netdev_priv(dev);
315 unregister_netdev(dev);
316 + if (bp->flags & B44_FLAG_EXTERNAL_PHY)
317 + b44_unregister_phy_one(bp);
318 ssb_device_disable(sdev, 0);
319 ssb_bus_may_powerdown(sdev->bus);
321 --- a/drivers/net/ethernet/broadcom/b44.h
322 +++ b/drivers/net/ethernet/broadcom/b44.h
323 @@ -397,6 +397,9 @@ struct b44 {
327 + struct phy_device *phydev;
328 + struct mii_bus *mii_bus;
330 struct mii_if_info mii_if;