1 Index: linux-2.6.32.12/drivers/net/r6040.c
2 ===================================================================
3 --- linux-2.6.32.12.orig/drivers/net/r6040.c 2010-05-20 10:19:41.000000000 +0200
4 +++ linux-2.6.32.12/drivers/net/r6040.c 2010-05-20 10:28:48.000000000 +0200
8 #include <linux/uaccess.h>
9 +#include <linux/phy.h>
11 #include <asm/processor.h>
15 struct r6040_private {
16 spinlock_t lock; /* driver lock */
17 - struct timer_list timer;
19 struct r6040_descriptor *rx_insert_ptr;
20 struct r6040_descriptor *rx_remove_ptr;
22 struct r6040_descriptor *tx_ring;
23 dma_addr_t rx_ring_dma;
24 dma_addr_t tx_ring_dma;
25 - u16 tx_free_desc, phy_addr, phy_mode;
26 + u16 tx_free_desc, phy_addr;
29 struct net_device *dev;
30 - struct mii_if_info mii_if;
31 + struct mii_bus *mii_bus;
32 struct napi_struct napi;
34 + struct phy_device *phydev;
39 static char version[] __devinitdata = KERN_INFO DRV_NAME
44 -static int r6040_mdio_read(struct net_device *dev, int mii_id, int reg)
45 +static int r6040_mdiobus_read(struct mii_bus *bus, int phy_addr, int reg)
47 + struct net_device *dev = bus->priv;
48 struct r6040_private *lp = netdev_priv(dev);
49 void __iomem *ioaddr = lp->base;
51 - return (r6040_phy_read(ioaddr, lp->phy_addr, reg));
52 + return r6040_phy_read(ioaddr, phy_addr, reg);
55 -static void r6040_mdio_write(struct net_device *dev, int mii_id, int reg, int val)
56 +static int r6040_mdiobus_write(struct mii_bus *bus, int phy_addr, int reg, u16 value)
58 + struct net_device *dev = bus->priv;
59 struct r6040_private *lp = netdev_priv(dev);
60 void __iomem *ioaddr = lp->base;
62 - r6040_phy_write(ioaddr, lp->phy_addr, reg, val);
63 + r6040_phy_write(ioaddr, phy_addr, reg, value);
68 +static int r6040_mdiobus_reset(struct mii_bus *bus)
73 static void r6040_free_txbufs(struct net_device *dev)
75 void __iomem *ioaddr = priv->base;
77 printk(KERN_WARNING "%s: transmit timed out, int enable %4.4x "
78 - "status %4.4x, PHY status %4.4x\n",
80 dev->name, ioread16(ioaddr + MIER),
81 - ioread16(ioaddr + MISR),
82 - r6040_mdio_read(dev, priv->mii_if.phy_id, MII_BMSR));
83 + ioread16(ioaddr + MISR));
85 dev->stats.tx_errors++;
88 struct r6040_private *lp = netdev_priv(dev);
89 struct pci_dev *pdev = lp->pdev;
92 - del_timer_sync(&lp->timer);
94 spin_lock_irq(&lp->lock);
95 napi_disable(&lp->napi);
96 netif_stop_queue(dev);
101 -/* Status of PHY CHIP */
102 -static int r6040_phy_mode_chk(struct net_device *dev)
104 - struct r6040_private *lp = netdev_priv(dev);
105 - void __iomem *ioaddr = lp->base;
108 - /* PHY Link Status Check */
109 - phy_dat = r6040_phy_read(ioaddr, lp->phy_addr, 1);
110 - if (!(phy_dat & 0x4))
111 - phy_dat = 0x8000; /* Link Failed, full duplex */
113 - /* PHY Chip Auto-Negotiation Status */
114 - phy_dat = r6040_phy_read(ioaddr, lp->phy_addr, 1);
115 - if (phy_dat & 0x0020) {
116 - /* Auto Negotiation Mode */
117 - phy_dat = r6040_phy_read(ioaddr, lp->phy_addr, 5);
118 - phy_dat &= r6040_phy_read(ioaddr, lp->phy_addr, 4);
119 - if (phy_dat & 0x140)
120 - /* Force full duplex */
126 - phy_dat = r6040_phy_read(ioaddr, lp->phy_addr, 0);
127 - if (phy_dat & 0x100)
136 -static void r6040_set_carrier(struct mii_if_info *mii)
138 - if (r6040_phy_mode_chk(mii->dev)) {
139 - /* autoneg is off: Link is always assumed to be up */
140 - if (!netif_carrier_ok(mii->dev))
141 - netif_carrier_on(mii->dev);
143 - r6040_phy_mode_chk(mii->dev);
146 static int r6040_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
148 struct r6040_private *lp = netdev_priv(dev);
149 - struct mii_ioctl_data *data = if_mii(rq);
152 - if (!netif_running(dev))
155 - spin_lock_irq(&lp->lock);
156 - rc = generic_mii_ioctl(&lp->mii_if, data, cmd, NULL);
157 - spin_unlock_irq(&lp->lock);
158 - r6040_set_carrier(&lp->mii_if);
161 + return phy_mii_ioctl(lp->phydev, if_mii(rq), cmd);
164 static int r6040_rx(struct net_device *dev, int limit)
169 - /* Read the PHY ID */
170 - lp->switch_sig = r6040_phy_read(ioaddr, 0, 2);
172 - if (lp->switch_sig == ICPLUS_PHY_ID) {
173 - r6040_phy_write(ioaddr, 29, 31, 0x175C); /* Enable registers */
174 - lp->phy_mode = 0x8000;
176 - /* PHY Mode Check */
177 - r6040_phy_write(ioaddr, lp->phy_addr, 4, PHY_CAP);
178 - r6040_phy_write(ioaddr, lp->phy_addr, 0, PHY_MODE);
180 - if (PHY_MODE == 0x3100)
181 - lp->phy_mode = r6040_phy_mode_chk(dev);
183 - lp->phy_mode = (PHY_MODE & 0x0100) ? 0x8000:0x0;
186 - /* Set duplex mode */
187 - lp->mcr0 |= lp->phy_mode;
189 /* improve performance (by RDC guys) */
190 r6040_phy_write(ioaddr, 30, 17, (r6040_phy_read(ioaddr, 30, 17) | 0x4000));
191 r6040_phy_write(ioaddr, 30, 17, ~((~r6040_phy_read(ioaddr, 30, 17)) | 0x2000));
197 - A periodic timer routine
198 - Polling PHY Chip Link Status
200 -static void r6040_timer(unsigned long data)
202 - struct net_device *dev = (struct net_device *)data;
203 - struct r6040_private *lp = netdev_priv(dev);
204 - void __iomem *ioaddr = lp->base;
207 - /* Polling PHY Chip Status */
208 - if (PHY_MODE == 0x3100)
209 - phy_mode = r6040_phy_mode_chk(dev);
211 - phy_mode = (PHY_MODE & 0x0100) ? 0x8000:0x0;
213 - if (phy_mode != lp->phy_mode) {
214 - lp->phy_mode = phy_mode;
215 - lp->mcr0 = (lp->mcr0 & 0x7fff) | phy_mode;
216 - iowrite16(lp->mcr0, ioaddr);
219 - /* Timer active again */
220 - mod_timer(&lp->timer, round_jiffies(jiffies + HZ));
223 - mii_check_media(&lp->mii_if, 1, 1);
226 /* Read/set MAC address routines */
227 static void r6040_mac_address(struct net_device *dev)
229 napi_enable(&lp->napi);
230 netif_start_queue(dev);
232 - /* set and active a timer process */
233 - setup_timer(&lp->timer, r6040_timer, (unsigned long) dev);
234 - if (lp->switch_sig != ICPLUS_PHY_ID)
235 - mod_timer(&lp->timer, jiffies + HZ);
239 @@ -1020,40 +924,22 @@
240 static int netdev_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
242 struct r6040_private *rp = netdev_priv(dev);
245 - spin_lock_irq(&rp->lock);
246 - rc = mii_ethtool_gset(&rp->mii_if, cmd);
247 - spin_unlock_irq(&rp->lock);
250 + return phy_ethtool_gset(rp->phydev, cmd);
253 static int netdev_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
255 struct r6040_private *rp = netdev_priv(dev);
258 - spin_lock_irq(&rp->lock);
259 - rc = mii_ethtool_sset(&rp->mii_if, cmd);
260 - spin_unlock_irq(&rp->lock);
261 - r6040_set_carrier(&rp->mii_if);
266 -static u32 netdev_get_link(struct net_device *dev)
268 - struct r6040_private *rp = netdev_priv(dev);
270 - return mii_link_ok(&rp->mii_if);
271 + return phy_ethtool_sset(rp->phydev, cmd);
274 static const struct ethtool_ops netdev_ethtool_ops = {
275 .get_drvinfo = netdev_get_drvinfo,
276 .get_settings = netdev_get_settings,
277 .set_settings = netdev_set_settings,
278 - .get_link = netdev_get_link,
279 + .get_link = ethtool_op_get_link,
282 static const struct net_device_ops r6040_netdev_ops = {
283 @@ -1072,6 +958,86 @@
287 +static void r6040_adjust_link(struct net_device *dev)
289 + struct r6040_private *lp = netdev_priv(dev);
290 + struct phy_device *phydev = lp->phydev;
291 + int status_changed = 0;
292 + void __iomem *ioaddr = lp->base;
296 + if (lp->old_link != phydev->link) {
297 + status_changed = 1;
298 + lp->old_link = phydev->link;
301 + /* reflect duplex change */
302 + if (phydev->link && (lp->old_duplex != phydev->duplex)) {
303 + lp->mcr0 |= (phydev->duplex == DUPLEX_FULL ? 0x8000 : 0);
304 + iowrite16(lp->mcr0, ioaddr);
306 + status_changed = 1;
307 + lp->old_duplex = phydev->duplex;
310 + if (status_changed) {
311 + pr_info("%s: link %s", dev->name, phydev->link ?
314 + pr_cont(" - %d/%s", phydev->speed,
315 + DUPLEX_FULL == phydev->duplex ? "full" : "half");
320 +static int r6040_mii_probe(struct net_device *dev)
322 + struct r6040_private *lp = netdev_priv(dev);
323 + struct phy_device *phydev = NULL;
326 + for (phy_addr = 0; phy_addr < PHY_MAX_ADDR; phy_addr++) {
327 + if (lp->mii_bus->phy_map[phy_addr]) {
328 + phydev = lp->mii_bus->phy_map[phy_addr];
334 + printk(KERN_ERR DRV_NAME "no PHY found\n");
338 + phydev = phy_connect(dev, dev_name(&phydev->dev), &r6040_adjust_link,
339 + 0, PHY_INTERFACE_MODE_MII);
341 + if (IS_ERR(phydev)) {
342 + printk(KERN_ERR DRV_NAME "could not attach to PHY\n");
343 + return PTR_ERR(phydev);
346 + /* mask with MAC supported features */
347 + phydev->supported &= (SUPPORTED_10baseT_Half
348 + | SUPPORTED_10baseT_Full
349 + | SUPPORTED_100baseT_Half
350 + | SUPPORTED_100baseT_Full
351 + | SUPPORTED_Autoneg
355 + phydev->advertising = phydev->supported;
356 + lp->phydev = phydev;
358 + lp->old_duplex = -1;
360 + printk(KERN_INFO "%s: attached PHY driver [%s] "
361 + "(mii_bus:phy_addr=%s)\n", dev->name,
362 + phydev->drv->name, dev_name(&phydev->dev));
367 static int __devinit r6040_init_one(struct pci_dev *pdev,
368 const struct pci_device_id *ent)
370 @@ -1082,6 +1048,7 @@
371 static int card_idx = -1;
376 printk("%s\n", version);
378 @@ -1169,7 +1136,6 @@
379 /* Init RDC private data */
381 lp->phy_addr = phy_table[card_idx];
382 - lp->switch_sig = 0;
384 /* The RDC-specific entries in the device structure. */
385 dev->netdev_ops = &r6040_netdev_ops;
386 @@ -1177,28 +1143,61 @@
387 dev->watchdog_timeo = TX_TIMEOUT;
389 netif_napi_add(dev, &lp->napi, r6040_poll, 64);
390 - lp->mii_if.dev = dev;
391 - lp->mii_if.mdio_read = r6040_mdio_read;
392 - lp->mii_if.mdio_write = r6040_mdio_write;
393 - lp->mii_if.phy_id = lp->phy_addr;
394 - lp->mii_if.phy_id_mask = 0x1f;
395 - lp->mii_if.reg_num_mask = 0x1f;
397 + lp->mii_bus = mdiobus_alloc();
398 + if (!lp->mii_bus) {
399 + printk(KERN_ERR DRV_NAME ": mdiobus_alloc failed\n");
400 + goto err_out_unmap;
403 + lp->mii_bus->priv = dev;
404 + lp->mii_bus->read = r6040_mdiobus_read;
405 + lp->mii_bus->write = r6040_mdiobus_write;
406 + lp->mii_bus->reset = r6040_mdiobus_reset;
407 + lp->mii_bus->name = "r6040_eth_mii";
408 + snprintf(lp->mii_bus->id, MII_BUS_ID_SIZE, "%x", card_idx);
409 + lp->mii_bus->irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
410 + if (!lp->mii_bus->irq) {
411 + printk(KERN_ERR DRV_NAME ": allocation failed\n");
415 + for (i = 0; i < PHY_MAX_ADDR; i++)
416 + lp->mii_bus->irq[i] = PHY_POLL;
418 + err = mdiobus_register(lp->mii_bus);
420 + printk(KERN_ERR DRV_NAME ": failed to register MII bus\n");
421 + goto err_out_mdio_irq;
424 + err = r6040_mii_probe(dev);
426 + printk(KERN_ERR DRV_NAME ": failed to probe MII bus\n");
427 + goto err_out_mdio_unregister;
430 /* Check the vendor ID on the PHY, if 0xffff assume none attached */
431 if (r6040_phy_read(ioaddr, lp->phy_addr, 2) == 0xffff) {
432 printk(KERN_ERR DRV_NAME ": Failed to detect an attached PHY\n");
434 - goto err_out_unmap;
435 + goto err_out_mdio_unregister;
438 /* Register net device. After this dev->name assign */
439 err = register_netdev(dev);
441 printk(KERN_ERR DRV_NAME ": Failed to register net device\n");
442 - goto err_out_unmap;
443 + goto err_out_mdio_unregister;
447 +err_out_mdio_unregister:
448 + mdiobus_unregister(lp->mii_bus);
450 + kfree(lp->mii_bus->irq);
452 + mdiobus_free(lp->mii_bus);
454 pci_iounmap(pdev, ioaddr);
456 @@ -1212,8 +1211,12 @@
457 static void __devexit r6040_remove_one(struct pci_dev *pdev)
459 struct net_device *dev = pci_get_drvdata(pdev);
460 + struct r6040_private *lp = netdev_priv(dev);
462 unregister_netdev(dev);
463 + mdiobus_unregister(lp->mii_bus);
464 + kfree(lp->mii_bus->irq);
465 + mdiobus_free(lp->mii_bus);
466 pci_release_regions(pdev);
468 pci_disable_device(pdev);