2 * Dave DNET Ethernet Controller driver
4 * Copyright (C) 2008 Dave S.r.l. <www.dave.eu>
5 * Copyright (C) 2009 Ilya Yanok, Emcraft Systems Ltd, <yanok@emcraft.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 #include <linux/version.h>
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
15 #include <linux/kernel.h>
16 #include <linux/types.h>
17 #include <linux/slab.h>
18 #include <linux/delay.h>
19 #include <linux/init.h>
20 #include <linux/netdevice.h>
21 #include <linux/etherdevice.h>
22 #include <linux/dma-mapping.h>
23 #include <linux/platform_device.h>
24 #include <linux/phy.h>
30 /* function for reading internal MAC register */
31 u16 dnet_readw_mac(struct dnet *bp, u16 reg)
36 dnet_writel(bp, reg, MACREG_ADDR);
38 /* since a read/write op to the MAC is very slow,
39 * we must wait before reading the data */
42 /* read data read from the MAC register */
43 data_read = dnet_readl(bp, MACREG_DATA);
49 /* function for writing internal MAC register */
50 void dnet_writew_mac(struct dnet *bp, u16 reg, u16 val)
52 /* load data to write */
53 dnet_writel(bp, val, MACREG_DATA);
56 dnet_writel(bp, reg | DNET_INTERNAL_WRITE, MACREG_ADDR);
58 /* since a read/write op to the MAC is very slow,
59 * we must wait before exiting */
63 static void __dnet_set_hwaddr(struct dnet *bp)
67 tmp = cpu_to_be16(*((u16 *) bp->dev->dev_addr));
68 dnet_writew_mac(bp, DNET_INTERNAL_MAC_ADDR_0_REG, tmp);
69 tmp = cpu_to_be16(*((u16 *) (bp->dev->dev_addr + 2)));
70 dnet_writew_mac(bp, DNET_INTERNAL_MAC_ADDR_1_REG, tmp);
71 tmp = cpu_to_be16(*((u16 *) (bp->dev->dev_addr + 4)));
72 dnet_writew_mac(bp, DNET_INTERNAL_MAC_ADDR_2_REG, tmp);
75 static void __devinit dnet_get_hwaddr(struct dnet *bp)
82 * "Note that the MAC address is stored in the registers in Hexadecimal
83 * form. For example, to set the MAC Address to: AC-DE-48-00-00-80
84 * would require writing 0xAC (octet 0) to address 0x0B (high byte of
85 * Mac_addr[15:0]), 0xDE (octet 1) to address 0x0A (Low byte of
86 * Mac_addr[15:0]), 0x48 (octet 2) to address 0x0D (high byte of
87 * Mac_addr[15:0]), 0x00 (octet 3) to address 0x0C (Low byte of
88 * Mac_addr[15:0]), 0x00 (octet 4) to address 0x0F (high byte of
89 * Mac_addr[15:0]), and 0x80 (octet 5) to address * 0x0E (Low byte of
92 tmp = dnet_readw_mac(bp, DNET_INTERNAL_MAC_ADDR_0_REG);
93 *((u16 *) addr) = be16_to_cpu(tmp);
94 tmp = dnet_readw_mac(bp, DNET_INTERNAL_MAC_ADDR_1_REG);
95 *((u16 *) (addr + 2)) = be16_to_cpu(tmp);
96 tmp = dnet_readw_mac(bp, DNET_INTERNAL_MAC_ADDR_2_REG);
97 *((u16 *) (addr + 4)) = be16_to_cpu(tmp);
99 if (is_valid_ether_addr(addr))
100 memcpy(bp->dev->dev_addr, addr, sizeof(addr));
103 static int dnet_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
105 struct dnet *bp = bus->priv;
108 while (!(dnet_readw_mac(bp, DNET_INTERNAL_GMII_MNG_CTL_REG)
109 & DNET_INTERNAL_GMII_MNG_CMD_FIN))
112 /* only 5 bits allowed for phy-addr and reg_offset */
116 /* prepare reg_value for a read */
117 value = (mii_id << 8);
120 /* write control word */
121 dnet_writew_mac(bp, DNET_INTERNAL_GMII_MNG_CTL_REG, value);
123 /* wait for end of transfer */
124 while (!(dnet_readw_mac(bp, DNET_INTERNAL_GMII_MNG_CTL_REG)
125 & DNET_INTERNAL_GMII_MNG_CMD_FIN))
128 value = dnet_readw_mac(bp, DNET_INTERNAL_GMII_MNG_DAT_REG);
130 pr_debug("mdio_read %02x:%02x <- %04x\n", mii_id, regnum, value);
135 static int dnet_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
138 struct dnet *bp = bus->priv;
141 pr_debug("mdio_write %02x:%02x <- %04x\n", mii_id, regnum, value);
143 while (!(dnet_readw_mac(bp, DNET_INTERNAL_GMII_MNG_CTL_REG)
144 & DNET_INTERNAL_GMII_MNG_CMD_FIN))
147 /* prepare for a write operation */
150 /* only 5 bits allowed for phy-addr and reg_offset */
154 /* only 16 bits on data */
157 /* prepare reg_value for a write */
158 tmp |= (mii_id << 8);
161 /* write data to write first */
162 dnet_writew_mac(bp, DNET_INTERNAL_GMII_MNG_DAT_REG, value);
164 /* write control word */
165 dnet_writew_mac(bp, DNET_INTERNAL_GMII_MNG_CTL_REG, tmp);
167 while (!(dnet_readw_mac(bp, DNET_INTERNAL_GMII_MNG_CTL_REG)
168 & DNET_INTERNAL_GMII_MNG_CMD_FIN))
174 static int dnet_mdio_reset(struct mii_bus *bus)
179 static void dnet_handle_link_change(struct net_device *dev)
181 struct dnet *bp = netdev_priv(dev);
182 struct phy_device *phydev = bp->phy_dev;
184 u32 mode_reg, ctl_reg;
186 int status_change = 0;
188 spin_lock_irqsave(&bp->lock, flags);
190 mode_reg = dnet_readw_mac(bp, DNET_INTERNAL_MODE_REG);
191 ctl_reg = dnet_readw_mac(bp, DNET_INTERNAL_RXTX_CONTROL_REG);
194 if (bp->duplex != phydev->duplex) {
197 ~(DNET_INTERNAL_RXTX_CONTROL_ENABLEHALFDUP);
200 DNET_INTERNAL_RXTX_CONTROL_ENABLEHALFDUP;
202 bp->duplex = phydev->duplex;
206 if (bp->speed != phydev->speed) {
208 switch (phydev->speed) {
210 mode_reg |= DNET_INTERNAL_MODE_GBITEN;
214 mode_reg &= ~DNET_INTERNAL_MODE_GBITEN;
218 "%s: Ack! Speed (%d) is not "
219 "10/100/1000!\n", dev->name,
223 bp->speed = phydev->speed;
227 if (phydev->link != bp->link) {
230 (DNET_INTERNAL_MODE_RXEN | DNET_INTERNAL_MODE_TXEN);
233 ~(DNET_INTERNAL_MODE_RXEN |
234 DNET_INTERNAL_MODE_TXEN);
238 bp->link = phydev->link;
244 dnet_writew_mac(bp, DNET_INTERNAL_RXTX_CONTROL_REG, ctl_reg);
245 dnet_writew_mac(bp, DNET_INTERNAL_MODE_REG, mode_reg);
248 spin_unlock_irqrestore(&bp->lock, flags);
252 printk(KERN_INFO "%s: link up (%d/%s)\n",
253 dev->name, phydev->speed,
254 DUPLEX_FULL == phydev->duplex ? "Full" : "Half");
256 printk(KERN_INFO "%s: link down\n", dev->name);
260 static int dnet_mii_probe(struct net_device *dev)
262 struct dnet *bp = netdev_priv(dev);
263 struct phy_device *phydev = NULL;
266 /* find the first phy */
267 for (phy_addr = 0; phy_addr < PHY_MAX_ADDR; phy_addr++) {
268 if (bp->mii_bus->phy_map[phy_addr]) {
269 phydev = bp->mii_bus->phy_map[phy_addr];
275 printk(KERN_ERR "%s: no PHY found\n", dev->name);
279 /* TODO : add pin_irq */
281 /* attach the mac to the phy */
282 if (bp->capabilities & DNET_HAS_RMII) {
283 phydev = phy_connect(dev, dev_name(&phydev->dev),
284 &dnet_handle_link_change, 0,
285 PHY_INTERFACE_MODE_RMII);
287 phydev = phy_connect(dev, dev_name(&phydev->dev),
288 &dnet_handle_link_change, 0,
289 PHY_INTERFACE_MODE_MII);
292 if (IS_ERR(phydev)) {
293 printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
294 return PTR_ERR(phydev);
297 /* mask with MAC supported features */
298 if (bp->capabilities & DNET_HAS_GIGABIT)
299 phydev->supported &= PHY_GBIT_FEATURES;
301 phydev->supported &= PHY_BASIC_FEATURES;
303 phydev->supported |= SUPPORTED_Asym_Pause | SUPPORTED_Pause;
305 phydev->advertising = phydev->supported;
310 bp->phy_dev = phydev;
315 static int dnet_mii_init(struct dnet *bp)
319 bp->mii_bus = mdiobus_alloc();
320 if (bp->mii_bus == NULL)
323 bp->mii_bus->name = "dnet_mii_bus";
324 bp->mii_bus->read = &dnet_mdio_read;
325 bp->mii_bus->write = &dnet_mdio_write;
326 bp->mii_bus->reset = &dnet_mdio_reset;
328 snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%x", 0);
330 bp->mii_bus->priv = bp;
332 bp->mii_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
333 if (!bp->mii_bus->irq) {
338 for (i = 0; i < PHY_MAX_ADDR; i++)
339 bp->mii_bus->irq[i] = PHY_POLL;
341 platform_set_drvdata(bp->dev, bp->mii_bus);
343 if (mdiobus_register(bp->mii_bus)) {
345 goto err_out_free_mdio_irq;
348 if (dnet_mii_probe(bp->dev) != 0) {
350 goto err_out_unregister_bus;
355 err_out_unregister_bus:
356 mdiobus_unregister(bp->mii_bus);
357 err_out_free_mdio_irq:
358 kfree(bp->mii_bus->irq);
360 mdiobus_free(bp->mii_bus);
364 /* For Neptune board: LINK1000 as Link LED and TX as activity LED */
365 int dnet_phy_marvell_fixup(struct phy_device *phydev)
367 return phy_write(phydev, 0x18, 0x4148);
370 static void dnet_update_stats(struct dnet *bp)
372 u32 __iomem *reg = bp->regs + DNET_RX_PKT_IGNR_CNT;
373 u32 *p = &bp->hw_stats.rx_pkt_ignr;
374 u32 *end = &bp->hw_stats.rx_byte + 1;
376 WARN_ON((unsigned long)(end - p - 1) !=
377 (DNET_RX_BYTE_CNT - DNET_RX_PKT_IGNR_CNT) / 4);
379 for (; p < end; p++, reg++)
382 reg = bp->regs + DNET_TX_UNICAST_CNT;
383 p = &bp->hw_stats.tx_unicast;
384 end = &bp->hw_stats.tx_byte + 1;
386 WARN_ON((unsigned long)(end - p - 1) !=
387 (DNET_TX_BYTE_CNT - DNET_TX_UNICAST_CNT) / 4);
389 for (; p < end; p++, reg++)
393 static int dnet_poll(struct napi_struct *napi, int budget)
395 struct dnet *bp = container_of(napi, struct dnet, napi);
396 struct net_device *dev = bp->dev;
398 unsigned int pkt_len;
400 unsigned int *data_ptr;
405 while (npackets < budget) {
407 * break out of while loop if there are no more
410 if (!(dnet_readl(bp, RX_FIFO_WCNT) >> 16)) {
412 int_enable = dnet_readl(bp, INTR_ENB);
413 int_enable |= DNET_INTR_SRC_RX_CMDFIFOAF;
414 dnet_writel(bp, int_enable, INTR_ENB);
418 cmd_word = dnet_readl(bp, RX_LEN_FIFO);
419 pkt_len = cmd_word & 0xFFFF;
421 if (cmd_word & 0xDF180000)
422 printk(KERN_ERR "%s packet receive error %x\n",
425 skb = dev_alloc_skb(pkt_len + 5);
427 /* Align IP on 16 byte boundaries */
430 * 'skb_put()' points to the start of sk_buff
433 data_ptr = (unsigned int *)skb_put(skb, pkt_len);
434 for (i = 0; i < (pkt_len + 3) >> 2; i++)
435 *data_ptr++ = dnet_readl(bp, RX_DATA_FIFO);
436 skb->protocol = eth_type_trans(skb, dev);
437 netif_receive_skb(skb);
441 "%s: No memory to allocate a sk_buff of "
442 "size %u.\n", dev->name, pkt_len);
447 if (npackets < budget) {
448 /* We processed all packets available. Tell NAPI it can
449 * stop polling then re-enable rx interrupts */
451 int_enable = dnet_readl(bp, INTR_ENB);
452 int_enable |= DNET_INTR_SRC_RX_CMDFIFOAF;
453 dnet_writel(bp, int_enable, INTR_ENB);
457 /* There are still packets waiting */
461 static irqreturn_t dnet_interrupt(int irq, void *dev_id)
463 struct net_device *dev = dev_id;
464 struct dnet *bp = netdev_priv(dev);
465 u32 int_src, int_enable, int_current;
467 unsigned int handled = 0;
469 spin_lock_irqsave(&bp->lock, flags);
471 /* read and clear the DNET irq (clear on read) */
472 int_src = dnet_readl(bp, INTR_SRC);
473 int_enable = dnet_readl(bp, INTR_ENB);
474 int_current = int_src & int_enable;
476 /* restart the queue if we had stopped it for TX fifo almost full */
477 if (int_current & DNET_INTR_SRC_TX_FIFOAE) {
478 int_enable = dnet_readl(bp, INTR_ENB);
479 int_enable &= ~DNET_INTR_ENB_TX_FIFOAE;
480 dnet_writel(bp, int_enable, INTR_ENB);
481 netif_wake_queue(dev);
485 /* RX FIFO error checking */
487 (DNET_INTR_SRC_RX_CMDFIFOFF | DNET_INTR_SRC_RX_DATAFIFOFF)) {
488 printk(KERN_ERR "%s: RX fifo error %x, irq %x\n", __func__,
489 dnet_readl(bp, RX_STATUS), int_current);
490 /* we can only flush the RX FIFOs */
491 dnet_writel(bp, DNET_SYS_CTL_RXFIFOFLUSH, SYS_CTL);
493 dnet_writel(bp, 0, SYS_CTL);
497 /* TX FIFO error checking */
499 (DNET_INTR_SRC_TX_FIFOFULL | DNET_INTR_SRC_TX_DISCFRM)) {
500 printk(KERN_ERR "%s: TX fifo error %x, irq %x\n", __func__,
501 dnet_readl(bp, TX_STATUS), int_current);
502 /* we can only flush the TX FIFOs */
503 dnet_writel(bp, DNET_SYS_CTL_TXFIFOFLUSH, SYS_CTL);
505 dnet_writel(bp, 0, SYS_CTL);
509 if (int_current & DNET_INTR_SRC_RX_CMDFIFOAF) {
510 if (napi_schedule_prep(&bp->napi)) {
512 * There's no point taking any more interrupts
513 * until we have processed the buffers
515 /* Disable Rx interrupts and schedule NAPI poll */
516 int_enable = dnet_readl(bp, INTR_ENB);
517 int_enable &= ~DNET_INTR_SRC_RX_CMDFIFOAF;
518 dnet_writel(bp, int_enable, INTR_ENB);
519 __napi_schedule(&bp->napi);
525 pr_debug("%s: irq %x remains\n", __func__, int_current);
527 spin_unlock_irqrestore(&bp->lock, flags);
529 return IRQ_RETVAL(handled);
533 static inline void dnet_print_skb(struct sk_buff *skb)
536 printk(KERN_DEBUG PFX "data:");
537 for (k = 0; k < skb->len; k++)
538 printk(" %02x", (unsigned int)skb->data[k]);
542 #define dnet_print_skb(skb) do {} while (0)
545 static int dnet_start_xmit(struct sk_buff *skb, struct net_device *dev)
548 struct dnet *bp = netdev_priv(dev);
549 u32 tx_status, irq_enable;
550 unsigned int len, i, tx_cmd, wrsz;
554 tx_status = dnet_readl(bp, TX_STATUS);
556 pr_debug("start_xmit: len %u head %p data %p\n",
557 skb->len, skb->head, skb->data);
560 /* frame size (words) */
561 len = (skb->len + 3) >> 2;
563 spin_lock_irqsave(&bp->lock, flags);
565 tx_status = dnet_readl(bp, TX_STATUS);
567 bufp = (unsigned int *)(((unsigned long) skb->data) & ~0x3UL);
568 wrsz = (u32) skb->len + 3;
569 wrsz += ((unsigned long) skb->data) & 0x3;
571 tx_cmd = ((((unsigned long)(skb->data)) & 0x03) << 16) | (u32) skb->len;
573 /* check if there is enough room for the current frame */
574 if (wrsz < (DNET_FIFO_SIZE - dnet_readl(bp, TX_FIFO_WCNT))) {
575 for (i = 0; i < wrsz; i++)
576 dnet_writel(bp, *bufp++, TX_DATA_FIFO);
579 * inform MAC that a packet's written and ready to be
582 dnet_writel(bp, tx_cmd, TX_LEN_FIFO);
585 if (dnet_readl(bp, TX_FIFO_WCNT) > DNET_FIFO_TX_DATA_AF_TH) {
586 netif_stop_queue(dev);
587 tx_status = dnet_readl(bp, INTR_SRC);
588 irq_enable = dnet_readl(bp, INTR_ENB);
589 irq_enable |= DNET_INTR_ENB_TX_FIFOAE;
590 dnet_writel(bp, irq_enable, INTR_ENB);
593 /* free the buffer */
596 spin_unlock_irqrestore(&bp->lock, flags);
598 dev->trans_start = jiffies;
603 static void dnet_reset_hw(struct dnet *bp)
605 /* put ts_mac in IDLE state i.e. disable rx/tx */
606 dnet_writew_mac(bp, DNET_INTERNAL_MODE_REG, DNET_INTERNAL_MODE_FCEN);
609 * RX FIFO almost full threshold: only cmd FIFO almost full is
610 * implemented for RX side
612 dnet_writel(bp, DNET_FIFO_RX_CMD_AF_TH, RX_FIFO_TH);
614 * TX FIFO almost empty threshold: only data FIFO almost empty
615 * is implemented for TX side
617 dnet_writel(bp, DNET_FIFO_TX_DATA_AE_TH, TX_FIFO_TH);
619 /* flush rx/tx fifos */
620 dnet_writel(bp, DNET_SYS_CTL_RXFIFOFLUSH | DNET_SYS_CTL_TXFIFOFLUSH,
623 dnet_writel(bp, 0, SYS_CTL);
626 static void dnet_init_hw(struct dnet *bp)
631 __dnet_set_hwaddr(bp);
633 config = dnet_readw_mac(bp, DNET_INTERNAL_RXTX_CONTROL_REG);
635 if (bp->dev->flags & IFF_PROMISC)
636 /* Copy All Frames */
637 config |= DNET_INTERNAL_RXTX_CONTROL_ENPROMISC;
638 if (!(bp->dev->flags & IFF_BROADCAST))
640 config |= DNET_INTERNAL_RXTX_CONTROL_RXMULTICAST;
642 config |= DNET_INTERNAL_RXTX_CONTROL_RXPAUSE |
643 DNET_INTERNAL_RXTX_CONTROL_RXBROADCAST |
644 DNET_INTERNAL_RXTX_CONTROL_DROPCONTROL |
645 DNET_INTERNAL_RXTX_CONTROL_DISCFXFCS;
647 dnet_writew_mac(bp, DNET_INTERNAL_RXTX_CONTROL_REG, config);
649 /* clear irq before enabling them */
650 config = dnet_readl(bp, INTR_SRC);
652 /* enable RX/TX interrupt, recv packet ready interrupt */
653 dnet_writel(bp, DNET_INTR_ENB_GLOBAL_ENABLE | DNET_INTR_ENB_RX_SUMMARY |
654 DNET_INTR_ENB_TX_SUMMARY | DNET_INTR_ENB_RX_FIFOERR |
655 DNET_INTR_ENB_RX_ERROR | DNET_INTR_ENB_RX_FIFOFULL |
656 DNET_INTR_ENB_TX_FIFOFULL | DNET_INTR_ENB_TX_DISCFRM |
657 DNET_INTR_ENB_RX_PKTRDY, INTR_ENB);
660 static int dnet_open(struct net_device *dev)
662 struct dnet *bp = netdev_priv(dev);
664 /* if the phy is not yet register, retry later */
668 if (!is_valid_ether_addr(dev->dev_addr))
669 return -EADDRNOTAVAIL;
671 napi_enable(&bp->napi);
674 phy_start_aneg(bp->phy_dev);
676 /* schedule a link state check */
677 phy_start(bp->phy_dev);
679 netif_start_queue(dev);
684 static int dnet_close(struct net_device *dev)
686 struct dnet *bp = netdev_priv(dev);
688 netif_stop_queue(dev);
689 napi_disable(&bp->napi);
692 phy_stop(bp->phy_dev);
695 netif_carrier_off(dev);
700 static inline void dnet_print_pretty_hwstats(struct dnet_stats *hwstat)
702 pr_debug("%s\n", __func__);
703 pr_debug("----------------------------- RX statistics "
704 "-------------------------------\n");
705 pr_debug("RX_PKT_IGNR_CNT %-8x\n", hwstat->rx_pkt_ignr);
706 pr_debug("RX_LEN_CHK_ERR_CNT %-8x\n", hwstat->rx_len_chk_err);
707 pr_debug("RX_LNG_FRM_CNT %-8x\n", hwstat->rx_lng_frm);
708 pr_debug("RX_SHRT_FRM_CNT %-8x\n", hwstat->rx_shrt_frm);
709 pr_debug("RX_IPG_VIOL_CNT %-8x\n", hwstat->rx_ipg_viol);
710 pr_debug("RX_CRC_ERR_CNT %-8x\n", hwstat->rx_crc_err);
711 pr_debug("RX_OK_PKT_CNT %-8x\n", hwstat->rx_ok_pkt);
712 pr_debug("RX_CTL_FRM_CNT %-8x\n", hwstat->rx_ctl_frm);
713 pr_debug("RX_PAUSE_FRM_CNT %-8x\n", hwstat->rx_pause_frm);
714 pr_debug("RX_MULTICAST_CNT %-8x\n", hwstat->rx_multicast);
715 pr_debug("RX_BROADCAST_CNT %-8x\n", hwstat->rx_broadcast);
716 pr_debug("RX_VLAN_TAG_CNT %-8x\n", hwstat->rx_vlan_tag);
717 pr_debug("RX_PRE_SHRINK_CNT %-8x\n", hwstat->rx_pre_shrink);
718 pr_debug("RX_DRIB_NIB_CNT %-8x\n", hwstat->rx_drib_nib);
719 pr_debug("RX_UNSUP_OPCD_CNT %-8x\n", hwstat->rx_unsup_opcd);
720 pr_debug("RX_BYTE_CNT %-8x\n", hwstat->rx_byte);
721 pr_debug("----------------------------- TX statistics "
722 "-------------------------------\n");
723 pr_debug("TX_UNICAST_CNT %-8x\n", hwstat->tx_unicast);
724 pr_debug("TX_PAUSE_FRM_CNT %-8x\n", hwstat->tx_pause_frm);
725 pr_debug("TX_MULTICAST_CNT %-8x\n", hwstat->tx_multicast);
726 pr_debug("TX_BRDCAST_CNT %-8x\n", hwstat->tx_brdcast);
727 pr_debug("TX_VLAN_TAG_CNT %-8x\n", hwstat->tx_vlan_tag);
728 pr_debug("TX_BAD_FCS_CNT %-8x\n", hwstat->tx_bad_fcs);
729 pr_debug("TX_JUMBO_CNT %-8x\n", hwstat->tx_jumbo);
730 pr_debug("TX_BYTE_CNT %-8x\n", hwstat->tx_byte);
733 static struct net_device_stats *dnet_get_stats(struct net_device *dev)
736 struct dnet *bp = netdev_priv(dev);
737 struct net_device_stats *nstat = &dev->stats;
738 struct dnet_stats *hwstat = &bp->hw_stats;
740 /* read stats from hardware */
741 dnet_update_stats(bp);
743 /* Convert HW stats into netdevice stats */
744 nstat->rx_errors = (hwstat->rx_len_chk_err +
745 hwstat->rx_lng_frm + hwstat->rx_shrt_frm +
746 /* ignore IGP violation error
747 hwstat->rx_ipg_viol + */
749 hwstat->rx_pre_shrink +
750 hwstat->rx_drib_nib + hwstat->rx_unsup_opcd);
751 nstat->tx_errors = hwstat->tx_bad_fcs;
752 nstat->rx_length_errors = (hwstat->rx_len_chk_err +
754 hwstat->rx_shrt_frm + hwstat->rx_pre_shrink);
755 nstat->rx_crc_errors = hwstat->rx_crc_err;
756 nstat->rx_frame_errors = hwstat->rx_pre_shrink + hwstat->rx_drib_nib;
757 nstat->rx_packets = hwstat->rx_ok_pkt;
758 nstat->tx_packets = (hwstat->tx_unicast +
759 hwstat->tx_multicast + hwstat->tx_brdcast);
760 nstat->rx_bytes = hwstat->rx_byte;
761 nstat->tx_bytes = hwstat->tx_byte;
762 nstat->multicast = hwstat->rx_multicast;
763 nstat->rx_missed_errors = hwstat->rx_pkt_ignr;
765 dnet_print_pretty_hwstats(hwstat);
770 static int dnet_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
772 struct dnet *bp = netdev_priv(dev);
773 struct phy_device *phydev = bp->phy_dev;
778 return phy_ethtool_gset(phydev, cmd);
781 static int dnet_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
783 struct dnet *bp = netdev_priv(dev);
784 struct phy_device *phydev = bp->phy_dev;
789 return phy_ethtool_sset(phydev, cmd);
792 static int dnet_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
794 struct dnet *bp = netdev_priv(dev);
795 struct phy_device *phydev = bp->phy_dev;
797 if (!netif_running(dev))
803 return phy_mii_ioctl(phydev, if_mii(rq), cmd);
806 static void dnet_get_drvinfo(struct net_device *dev,
807 struct ethtool_drvinfo *info)
809 strcpy(info->driver, DRV_NAME);
810 strcpy(info->version, DRV_VERSION);
811 strcpy(info->bus_info, "0");
814 static const struct ethtool_ops dnet_ethtool_ops = {
815 .get_settings = dnet_get_settings,
816 .set_settings = dnet_set_settings,
817 .get_drvinfo = dnet_get_drvinfo,
818 .get_link = ethtool_op_get_link,
821 static const struct net_device_ops dnet_netdev_ops = {
822 .ndo_open = dnet_open,
823 .ndo_stop = dnet_close,
824 .ndo_get_stats = dnet_get_stats,
825 .ndo_start_xmit = dnet_start_xmit,
826 .ndo_do_ioctl = dnet_ioctl,
827 .ndo_set_mac_address = eth_mac_addr,
828 .ndo_validate_addr = eth_validate_addr,
829 .ndo_change_mtu = eth_change_mtu,
832 static int __devinit dnet_probe(struct platform_device *pdev)
834 struct resource *res;
835 struct net_device *dev;
837 struct phy_device *phydev;
839 unsigned int mem_base, mem_size, irq;
841 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
843 dev_err(&pdev->dev, "no mmio resource defined\n");
846 mem_base = res->start;
847 mem_size = resource_size(res);
848 irq = platform_get_irq(pdev, 0);
850 if (!request_mem_region(mem_base, mem_size, DRV_NAME)) {
851 dev_err(&pdev->dev, "no memory region available\n");
857 dev = alloc_etherdev(sizeof(*bp));
859 dev_err(&pdev->dev, "etherdev alloc failed, aborting.\n");
863 /* TODO: Actually, we have some interesting features... */
866 bp = netdev_priv(dev);
869 SET_NETDEV_DEV(dev, &pdev->dev);
871 spin_lock_init(&bp->lock);
873 bp->regs = ioremap(mem_base, mem_size);
875 dev_err(&pdev->dev, "failed to map registers, aborting.\n");
877 goto err_out_free_dev;
881 err = request_irq(dev->irq, dnet_interrupt, 0, DRV_NAME, dev);
883 dev_err(&pdev->dev, "Unable to request IRQ %d (error %d)\n",
885 goto err_out_iounmap;
888 dev->netdev_ops = &dnet_netdev_ops;
889 netif_napi_add(dev, &bp->napi, dnet_poll, 64);
890 dev->ethtool_ops = &dnet_ethtool_ops;
892 dev->base_addr = (unsigned long)bp->regs;
894 bp->capabilities = dnet_readl(bp, VERCAPS) & DNET_CAPS_MASK;
898 if (!is_valid_ether_addr(dev->dev_addr)) {
899 /* choose a random ethernet address */
900 random_ether_addr(dev->dev_addr);
901 __dnet_set_hwaddr(bp);
904 err = register_netdev(dev);
906 dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
907 goto err_out_free_irq;
910 /* register the PHY board fixup (for Marvell 88E1111) */
911 err = phy_register_fixup_for_uid(0x01410cc0, 0xfffffff0,
912 dnet_phy_marvell_fixup);
913 /* we can live without it, so just issue a warning */
915 dev_warn(&pdev->dev, "Cannot register PHY board fixup.\n");
917 if (dnet_mii_init(bp) != 0)
918 goto err_out_unregister_netdev;
920 dev_info(&pdev->dev, "Dave DNET at 0x%p (0x%08x) irq %d %pM\n",
921 bp->regs, mem_base, dev->irq, dev->dev_addr);
922 dev_info(&pdev->dev, "has %smdio, %sirq, %sgigabit, %sdma \n",
923 (bp->capabilities & DNET_HAS_MDIO) ? "" : "no ",
924 (bp->capabilities & DNET_HAS_IRQ) ? "" : "no ",
925 (bp->capabilities & DNET_HAS_GIGABIT) ? "" : "no ",
926 (bp->capabilities & DNET_HAS_DMA) ? "" : "no ");
927 phydev = bp->phy_dev;
928 dev_info(&pdev->dev, "attached PHY driver [%s] "
929 "(mii_bus:phy_addr=%s, irq=%d)\n",
930 phydev->drv->name, dev_name(&phydev->dev), phydev->irq);
934 err_out_unregister_netdev:
935 unregister_netdev(dev);
937 free_irq(dev->irq, dev);
946 static int __devexit dnet_remove(struct platform_device *pdev)
949 struct net_device *dev;
952 dev = platform_get_drvdata(pdev);
955 bp = netdev_priv(dev);
957 phy_disconnect(bp->phy_dev);
958 mdiobus_unregister(bp->mii_bus);
959 kfree(bp->mii_bus->irq);
960 mdiobus_free(bp->mii_bus);
961 unregister_netdev(dev);
962 free_irq(dev->irq, dev);
970 static struct platform_driver dnet_driver = {
972 .remove = __devexit_p(dnet_remove),
978 static int __init dnet_init(void)
980 return platform_driver_register(&dnet_driver);
983 static void __exit dnet_exit(void)
985 platform_driver_unregister(&dnet_driver);
988 module_init(dnet_init);
989 module_exit(dnet_exit);
991 MODULE_LICENSE("GPL");
992 MODULE_DESCRIPTION("Dave DNET Ethernet driver");
993 MODULE_AUTHOR("Ilya Yanok <yanok@emcraft.com>, "
994 "Matteo Vit <matteo.vit@dave.eu>");