1 --- a/drivers/net/r6040.c
2 +++ b/drivers/net/r6040.c
6 #include <linux/uaccess.h>
7 +#include <linux/phy.h>
9 #include <asm/processor.h>
11 @@ -180,7 +181,6 @@ struct r6040_descriptor {
13 struct r6040_private {
14 spinlock_t lock; /* driver lock */
15 - struct timer_list timer;
17 struct r6040_descriptor *rx_insert_ptr;
18 struct r6040_descriptor *rx_remove_ptr;
19 @@ -190,13 +190,15 @@ struct r6040_private {
20 struct r6040_descriptor *tx_ring;
21 dma_addr_t rx_ring_dma;
22 dma_addr_t tx_ring_dma;
23 - u16 tx_free_desc, phy_addr, phy_mode;
24 + u16 tx_free_desc, phy_addr;
27 struct net_device *dev;
28 - struct mii_if_info mii_if;
29 + struct mii_bus *mii_bus;
30 struct napi_struct napi;
32 + struct phy_device *phydev;
37 static char version[] __devinitdata = KERN_INFO DRV_NAME
38 @@ -239,20 +241,29 @@ static void r6040_phy_write(void __iomem
42 -static int r6040_mdio_read(struct net_device *dev, int mii_id, int reg)
43 +static int r6040_mdiobus_read(struct mii_bus *bus, int phy_addr, int reg)
45 + struct net_device *dev = bus->priv;
46 struct r6040_private *lp = netdev_priv(dev);
47 void __iomem *ioaddr = lp->base;
49 - return (r6040_phy_read(ioaddr, lp->phy_addr, reg));
50 + return r6040_phy_read(ioaddr, phy_addr, reg);
53 -static void r6040_mdio_write(struct net_device *dev, int mii_id, int reg, int val)
54 +static int r6040_mdiobus_write(struct mii_bus *bus, int phy_addr, int reg, u16 value)
56 + struct net_device *dev = bus->priv;
57 struct r6040_private *lp = netdev_priv(dev);
58 void __iomem *ioaddr = lp->base;
60 - r6040_phy_write(ioaddr, lp->phy_addr, reg, val);
61 + r6040_phy_write(ioaddr, phy_addr, reg, value);
66 +static int r6040_mdiobus_reset(struct mii_bus *bus)
71 static void r6040_free_txbufs(struct net_device *dev)
72 @@ -409,10 +420,9 @@ static void r6040_tx_timeout(struct net_
73 void __iomem *ioaddr = priv->base;
75 printk(KERN_WARNING "%s: transmit timed out, int enable %4.4x "
76 - "status %4.4x, PHY status %4.4x\n",
78 dev->name, ioread16(ioaddr + MIER),
79 - ioread16(ioaddr + MISR),
80 - r6040_mdio_read(dev, priv->mii_if.phy_id, MII_BMSR));
81 + ioread16(ioaddr + MISR));
83 dev->stats.tx_errors++;
85 @@ -464,9 +474,6 @@ static int r6040_close(struct net_device
86 struct r6040_private *lp = netdev_priv(dev);
87 struct pci_dev *pdev = lp->pdev;
90 - del_timer_sync(&lp->timer);
92 spin_lock_irq(&lp->lock);
93 napi_disable(&lp->napi);
94 netif_stop_queue(dev);
95 @@ -496,64 +503,14 @@ static int r6040_close(struct net_device
99 -/* Status of PHY CHIP */
100 -static int r6040_phy_mode_chk(struct net_device *dev)
102 - struct r6040_private *lp = netdev_priv(dev);
103 - void __iomem *ioaddr = lp->base;
106 - /* PHY Link Status Check */
107 - phy_dat = r6040_phy_read(ioaddr, lp->phy_addr, 1);
108 - if (!(phy_dat & 0x4))
109 - phy_dat = 0x8000; /* Link Failed, full duplex */
111 - /* PHY Chip Auto-Negotiation Status */
112 - phy_dat = r6040_phy_read(ioaddr, lp->phy_addr, 1);
113 - if (phy_dat & 0x0020) {
114 - /* Auto Negotiation Mode */
115 - phy_dat = r6040_phy_read(ioaddr, lp->phy_addr, 5);
116 - phy_dat &= r6040_phy_read(ioaddr, lp->phy_addr, 4);
117 - if (phy_dat & 0x140)
118 - /* Force full duplex */
124 - phy_dat = r6040_phy_read(ioaddr, lp->phy_addr, 0);
125 - if (phy_dat & 0x100)
134 -static void r6040_set_carrier(struct mii_if_info *mii)
136 - if (r6040_phy_mode_chk(mii->dev)) {
137 - /* autoneg is off: Link is always assumed to be up */
138 - if (!netif_carrier_ok(mii->dev))
139 - netif_carrier_on(mii->dev);
141 - r6040_phy_mode_chk(mii->dev);
144 static int r6040_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
146 struct r6040_private *lp = netdev_priv(dev);
147 - struct mii_ioctl_data *data = if_mii(rq);
150 - if (!netif_running(dev))
153 - spin_lock_irq(&lp->lock);
154 - rc = generic_mii_ioctl(&lp->mii_if, data, cmd, NULL);
155 - spin_unlock_irq(&lp->lock);
156 - r6040_set_carrier(&lp->mii_if);
159 + return phy_mii_ioctl(lp->phydev, if_mii(rq), cmd);
162 static int r6040_rx(struct net_device *dev, int limit)
163 @@ -752,26 +709,6 @@ static int r6040_up(struct net_device *d
167 - /* Read the PHY ID */
168 - lp->switch_sig = r6040_phy_read(ioaddr, 0, 2);
170 - if (lp->switch_sig == ICPLUS_PHY_ID) {
171 - r6040_phy_write(ioaddr, 29, 31, 0x175C); /* Enable registers */
172 - lp->phy_mode = 0x8000;
174 - /* PHY Mode Check */
175 - r6040_phy_write(ioaddr, lp->phy_addr, 4, PHY_CAP);
176 - r6040_phy_write(ioaddr, lp->phy_addr, 0, PHY_MODE);
178 - if (PHY_MODE == 0x3100)
179 - lp->phy_mode = r6040_phy_mode_chk(dev);
181 - lp->phy_mode = (PHY_MODE & 0x0100) ? 0x8000:0x0;
184 - /* Set duplex mode */
185 - lp->mcr0 |= lp->phy_mode;
187 /* improve performance (by RDC guys) */
188 r6040_phy_write(ioaddr, 30, 17, (r6040_phy_read(ioaddr, 30, 17) | 0x4000));
189 r6040_phy_write(ioaddr, 30, 17, ~((~r6040_phy_read(ioaddr, 30, 17)) | 0x2000));
190 @@ -784,35 +721,6 @@ static int r6040_up(struct net_device *d
195 - A periodic timer routine
196 - Polling PHY Chip Link Status
198 -static void r6040_timer(unsigned long data)
200 - struct net_device *dev = (struct net_device *)data;
201 - struct r6040_private *lp = netdev_priv(dev);
202 - void __iomem *ioaddr = lp->base;
205 - /* Polling PHY Chip Status */
206 - if (PHY_MODE == 0x3100)
207 - phy_mode = r6040_phy_mode_chk(dev);
209 - phy_mode = (PHY_MODE & 0x0100) ? 0x8000:0x0;
211 - if (phy_mode != lp->phy_mode) {
212 - lp->phy_mode = phy_mode;
213 - lp->mcr0 = (lp->mcr0 & 0x7fff) | phy_mode;
214 - iowrite16(lp->mcr0, ioaddr);
217 - /* Timer active again */
218 - mod_timer(&lp->timer, round_jiffies(jiffies + HZ));
221 - mii_check_media(&lp->mii_if, 1, 1);
224 /* Read/set MAC address routines */
225 static void r6040_mac_address(struct net_device *dev)
226 @@ -874,10 +782,6 @@ static int r6040_open(struct net_device
227 napi_enable(&lp->napi);
228 netif_start_queue(dev);
230 - /* set and active a timer process */
231 - setup_timer(&lp->timer, r6040_timer, (unsigned long) dev);
232 - if (lp->switch_sig != ICPLUS_PHY_ID)
233 - mod_timer(&lp->timer, jiffies + HZ);
237 @@ -1020,40 +924,22 @@ static void netdev_get_drvinfo(struct ne
238 static int netdev_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
240 struct r6040_private *rp = netdev_priv(dev);
243 - spin_lock_irq(&rp->lock);
244 - rc = mii_ethtool_gset(&rp->mii_if, cmd);
245 - spin_unlock_irq(&rp->lock);
248 + return phy_ethtool_gset(rp->phydev, cmd);
251 static int netdev_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
253 struct r6040_private *rp = netdev_priv(dev);
256 - spin_lock_irq(&rp->lock);
257 - rc = mii_ethtool_sset(&rp->mii_if, cmd);
258 - spin_unlock_irq(&rp->lock);
259 - r6040_set_carrier(&rp->mii_if);
264 -static u32 netdev_get_link(struct net_device *dev)
266 - struct r6040_private *rp = netdev_priv(dev);
268 - return mii_link_ok(&rp->mii_if);
269 + return phy_ethtool_sset(rp->phydev, cmd);
272 static const struct ethtool_ops netdev_ethtool_ops = {
273 .get_drvinfo = netdev_get_drvinfo,
274 .get_settings = netdev_get_settings,
275 .set_settings = netdev_set_settings,
276 - .get_link = netdev_get_link,
277 + .get_link = ethtool_op_get_link,
280 static const struct net_device_ops r6040_netdev_ops = {
281 @@ -1072,6 +958,86 @@ static const struct net_device_ops r6040
285 +static void r6040_adjust_link(struct net_device *dev)
287 + struct r6040_private *lp = netdev_priv(dev);
288 + struct phy_device *phydev = lp->phydev;
289 + int status_changed = 0;
290 + void __iomem *ioaddr = lp->base;
294 + if (lp->old_link != phydev->link) {
295 + status_changed = 1;
296 + lp->old_link = phydev->link;
299 + /* reflect duplex change */
300 + if (phydev->link && (lp->old_duplex != phydev->duplex)) {
301 + lp->mcr0 |= (phydev->duplex == DUPLEX_FULL ? 0x8000 : 0);
302 + iowrite16(lp->mcr0, ioaddr);
304 + status_changed = 1;
305 + lp->old_duplex = phydev->duplex;
308 + if (status_changed) {
309 + pr_info("%s: link %s", dev->name, phydev->link ?
312 + pr_cont(" - %d/%s", phydev->speed,
313 + DUPLEX_FULL == phydev->duplex ? "full" : "half");
318 +static int r6040_mii_probe(struct net_device *dev)
320 + struct r6040_private *lp = netdev_priv(dev);
321 + struct phy_device *phydev = NULL;
324 + for (phy_addr = 0; phy_addr < PHY_MAX_ADDR; phy_addr++) {
325 + if (lp->mii_bus->phy_map[phy_addr]) {
326 + phydev = lp->mii_bus->phy_map[phy_addr];
332 + printk(KERN_ERR DRV_NAME "no PHY found\n");
336 + phydev = phy_connect(dev, dev_name(&phydev->dev), &r6040_adjust_link,
337 + 0, PHY_INTERFACE_MODE_MII);
339 + if (IS_ERR(phydev)) {
340 + printk(KERN_ERR DRV_NAME "could not attach to PHY\n");
341 + return PTR_ERR(phydev);
344 + /* mask with MAC supported features */
345 + phydev->supported &= (SUPPORTED_10baseT_Half
346 + | SUPPORTED_10baseT_Full
347 + | SUPPORTED_100baseT_Half
348 + | SUPPORTED_100baseT_Full
349 + | SUPPORTED_Autoneg
353 + phydev->advertising = phydev->supported;
354 + lp->phydev = phydev;
356 + lp->old_duplex = -1;
358 + printk(KERN_INFO "%s: attached PHY driver [%s] "
359 + "(mii_bus:phy_addr=%s)\n", dev->name,
360 + phydev->drv->name, dev_name(&phydev->dev));
365 static int __devinit r6040_init_one(struct pci_dev *pdev,
366 const struct pci_device_id *ent)
368 @@ -1082,6 +1048,7 @@ static int __devinit r6040_init_one(stru
369 static int card_idx = -1;
374 printk("%s\n", version);
376 @@ -1169,7 +1136,6 @@ static int __devinit r6040_init_one(stru
377 /* Init RDC private data */
379 lp->phy_addr = phy_table[card_idx];
380 - lp->switch_sig = 0;
382 /* The RDC-specific entries in the device structure. */
383 dev->netdev_ops = &r6040_netdev_ops;
384 @@ -1177,28 +1143,61 @@ static int __devinit r6040_init_one(stru
385 dev->watchdog_timeo = TX_TIMEOUT;
387 netif_napi_add(dev, &lp->napi, r6040_poll, 64);
388 - lp->mii_if.dev = dev;
389 - lp->mii_if.mdio_read = r6040_mdio_read;
390 - lp->mii_if.mdio_write = r6040_mdio_write;
391 - lp->mii_if.phy_id = lp->phy_addr;
392 - lp->mii_if.phy_id_mask = 0x1f;
393 - lp->mii_if.reg_num_mask = 0x1f;
395 + lp->mii_bus = mdiobus_alloc();
396 + if (!lp->mii_bus) {
397 + printk(KERN_ERR DRV_NAME ": mdiobus_alloc failed\n");
398 + goto err_out_unmap;
401 + lp->mii_bus->priv = dev;
402 + lp->mii_bus->read = r6040_mdiobus_read;
403 + lp->mii_bus->write = r6040_mdiobus_write;
404 + lp->mii_bus->reset = r6040_mdiobus_reset;
405 + lp->mii_bus->name = "r6040_eth_mii";
406 + snprintf(lp->mii_bus->id, MII_BUS_ID_SIZE, "%x", card_idx);
407 + lp->mii_bus->irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
408 + if (!lp->mii_bus->irq) {
409 + printk(KERN_ERR DRV_NAME ": allocation failed\n");
413 + for (i = 0; i < PHY_MAX_ADDR; i++)
414 + lp->mii_bus->irq[i] = PHY_POLL;
416 + err = mdiobus_register(lp->mii_bus);
418 + printk(KERN_ERR DRV_NAME ": failed to register MII bus\n");
419 + goto err_out_mdio_irq;
422 + err = r6040_mii_probe(dev);
424 + printk(KERN_ERR DRV_NAME ": failed to probe MII bus\n");
425 + goto err_out_mdio_unregister;
428 /* Check the vendor ID on the PHY, if 0xffff assume none attached */
429 if (r6040_phy_read(ioaddr, lp->phy_addr, 2) == 0xffff) {
430 printk(KERN_ERR DRV_NAME ": Failed to detect an attached PHY\n");
432 - goto err_out_unmap;
433 + goto err_out_mdio_unregister;
436 /* Register net device. After this dev->name assign */
437 err = register_netdev(dev);
439 printk(KERN_ERR DRV_NAME ": Failed to register net device\n");
440 - goto err_out_unmap;
441 + goto err_out_mdio_unregister;
445 +err_out_mdio_unregister:
446 + mdiobus_unregister(lp->mii_bus);
448 + kfree(lp->mii_bus->irq);
450 + mdiobus_free(lp->mii_bus);
452 pci_iounmap(pdev, ioaddr);
454 @@ -1212,8 +1211,12 @@ err_out:
455 static void __devexit r6040_remove_one(struct pci_dev *pdev)
457 struct net_device *dev = pci_get_drvdata(pdev);
458 + struct r6040_private *lp = netdev_priv(dev);
460 unregister_netdev(dev);
461 + mdiobus_unregister(lp->mii_bus);
462 + kfree(lp->mii_bus->irq);
463 + mdiobus_free(lp->mii_bus);
464 pci_release_regions(pdev);
466 pci_disable_device(pdev);