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.
12 #include <linux/module.h>
13 #include <linux/moduleparam.h>
14 #include <linux/kernel.h>
15 #include <linux/types.h>
16 #include <linux/slab.h>
17 #include <linux/delay.h>
18 #include <linux/interrupt.h>
19 #include <linux/netdevice.h>
20 #include <linux/etherdevice.h>
21 #include <linux/dma-mapping.h>
22 #include <linux/platform_device.h>
23 #include <linux/phy.h>
29 /* function for reading internal MAC register */
30 static u16
dnet_readw_mac(struct dnet
*bp
, u16 reg
)
35 dnet_writel(bp
, reg
, MACREG_ADDR
);
37 /* since a read/write op to the MAC is very slow,
38 * we must wait before reading the data */
41 /* read data read from the MAC register */
42 data_read
= dnet_readl(bp
, MACREG_DATA
);
48 /* function for writing internal MAC register */
49 static void dnet_writew_mac(struct dnet
*bp
, u16 reg
, u16 val
)
51 /* load data to write */
52 dnet_writel(bp
, val
, MACREG_DATA
);
55 dnet_writel(bp
, reg
| DNET_INTERNAL_WRITE
, MACREG_ADDR
);
57 /* since a read/write op to the MAC is very slow,
58 * we must wait before exiting */
62 static void __dnet_set_hwaddr(struct dnet
*bp
)
66 tmp
= be16_to_cpup((__be16
*)bp
->dev
->dev_addr
);
67 dnet_writew_mac(bp
, DNET_INTERNAL_MAC_ADDR_0_REG
, tmp
);
68 tmp
= be16_to_cpup((__be16
*)(bp
->dev
->dev_addr
+ 2));
69 dnet_writew_mac(bp
, DNET_INTERNAL_MAC_ADDR_1_REG
, tmp
);
70 tmp
= be16_to_cpup((__be16
*)(bp
->dev
->dev_addr
+ 4));
71 dnet_writew_mac(bp
, DNET_INTERNAL_MAC_ADDR_2_REG
, tmp
);
74 static void dnet_get_hwaddr(struct dnet
*bp
)
81 * "Note that the MAC address is stored in the registers in Hexadecimal
82 * form. For example, to set the MAC Address to: AC-DE-48-00-00-80
83 * would require writing 0xAC (octet 0) to address 0x0B (high byte of
84 * Mac_addr[15:0]), 0xDE (octet 1) to address 0x0A (Low byte of
85 * Mac_addr[15:0]), 0x48 (octet 2) to address 0x0D (high byte of
86 * Mac_addr[15:0]), 0x00 (octet 3) to address 0x0C (Low byte of
87 * Mac_addr[15:0]), 0x00 (octet 4) to address 0x0F (high byte of
88 * Mac_addr[15:0]), and 0x80 (octet 5) to address * 0x0E (Low byte of
91 tmp
= dnet_readw_mac(bp
, DNET_INTERNAL_MAC_ADDR_0_REG
);
92 *((__be16
*)addr
) = cpu_to_be16(tmp
);
93 tmp
= dnet_readw_mac(bp
, DNET_INTERNAL_MAC_ADDR_1_REG
);
94 *((__be16
*)(addr
+ 2)) = cpu_to_be16(tmp
);
95 tmp
= dnet_readw_mac(bp
, DNET_INTERNAL_MAC_ADDR_2_REG
);
96 *((__be16
*)(addr
+ 4)) = cpu_to_be16(tmp
);
98 if (is_valid_ether_addr(addr
))
99 memcpy(bp
->dev
->dev_addr
, addr
, sizeof(addr
));
102 static int dnet_mdio_read(struct mii_bus
*bus
, int mii_id
, int regnum
)
104 struct dnet
*bp
= bus
->priv
;
107 while (!(dnet_readw_mac(bp
, DNET_INTERNAL_GMII_MNG_CTL_REG
)
108 & DNET_INTERNAL_GMII_MNG_CMD_FIN
))
111 /* only 5 bits allowed for phy-addr and reg_offset */
115 /* prepare reg_value for a read */
116 value
= (mii_id
<< 8);
119 /* write control word */
120 dnet_writew_mac(bp
, DNET_INTERNAL_GMII_MNG_CTL_REG
, value
);
122 /* wait for end of transfer */
123 while (!(dnet_readw_mac(bp
, DNET_INTERNAL_GMII_MNG_CTL_REG
)
124 & DNET_INTERNAL_GMII_MNG_CMD_FIN
))
127 value
= dnet_readw_mac(bp
, DNET_INTERNAL_GMII_MNG_DAT_REG
);
129 pr_debug("mdio_read %02x:%02x <- %04x\n", mii_id
, regnum
, value
);
134 static int dnet_mdio_write(struct mii_bus
*bus
, int mii_id
, int regnum
,
137 struct dnet
*bp
= bus
->priv
;
140 pr_debug("mdio_write %02x:%02x <- %04x\n", mii_id
, regnum
, value
);
142 while (!(dnet_readw_mac(bp
, DNET_INTERNAL_GMII_MNG_CTL_REG
)
143 & DNET_INTERNAL_GMII_MNG_CMD_FIN
))
146 /* prepare for a write operation */
149 /* only 5 bits allowed for phy-addr and reg_offset */
153 /* only 16 bits on data */
156 /* prepare reg_value for a write */
157 tmp
|= (mii_id
<< 8);
160 /* write data to write first */
161 dnet_writew_mac(bp
, DNET_INTERNAL_GMII_MNG_DAT_REG
, value
);
163 /* write control word */
164 dnet_writew_mac(bp
, DNET_INTERNAL_GMII_MNG_CTL_REG
, tmp
);
166 while (!(dnet_readw_mac(bp
, DNET_INTERNAL_GMII_MNG_CTL_REG
)
167 & DNET_INTERNAL_GMII_MNG_CMD_FIN
))
173 static void dnet_handle_link_change(struct net_device
*dev
)
175 struct dnet
*bp
= netdev_priv(dev
);
176 struct phy_device
*phydev
= bp
->phy_dev
;
178 u32 mode_reg
, ctl_reg
;
180 int status_change
= 0;
182 spin_lock_irqsave(&bp
->lock
, flags
);
184 mode_reg
= dnet_readw_mac(bp
, DNET_INTERNAL_MODE_REG
);
185 ctl_reg
= dnet_readw_mac(bp
, DNET_INTERNAL_RXTX_CONTROL_REG
);
188 if (bp
->duplex
!= phydev
->duplex
) {
191 ~(DNET_INTERNAL_RXTX_CONTROL_ENABLEHALFDUP
);
194 DNET_INTERNAL_RXTX_CONTROL_ENABLEHALFDUP
;
196 bp
->duplex
= phydev
->duplex
;
200 if (bp
->speed
!= phydev
->speed
) {
202 switch (phydev
->speed
) {
204 mode_reg
|= DNET_INTERNAL_MODE_GBITEN
;
208 mode_reg
&= ~DNET_INTERNAL_MODE_GBITEN
;
212 "%s: Ack! Speed (%d) is not "
213 "10/100/1000!\n", dev
->name
,
217 bp
->speed
= phydev
->speed
;
221 if (phydev
->link
!= bp
->link
) {
224 (DNET_INTERNAL_MODE_RXEN
| DNET_INTERNAL_MODE_TXEN
);
227 ~(DNET_INTERNAL_MODE_RXEN
|
228 DNET_INTERNAL_MODE_TXEN
);
232 bp
->link
= phydev
->link
;
238 dnet_writew_mac(bp
, DNET_INTERNAL_RXTX_CONTROL_REG
, ctl_reg
);
239 dnet_writew_mac(bp
, DNET_INTERNAL_MODE_REG
, mode_reg
);
242 spin_unlock_irqrestore(&bp
->lock
, flags
);
246 printk(KERN_INFO
"%s: link up (%d/%s)\n",
247 dev
->name
, phydev
->speed
,
248 DUPLEX_FULL
== phydev
->duplex
? "Full" : "Half");
250 printk(KERN_INFO
"%s: link down\n", dev
->name
);
254 static int dnet_mii_probe(struct net_device
*dev
)
256 struct dnet
*bp
= netdev_priv(dev
);
257 struct phy_device
*phydev
= NULL
;
260 /* find the first phy */
261 for (phy_addr
= 0; phy_addr
< PHY_MAX_ADDR
; phy_addr
++) {
262 if (bp
->mii_bus
->phy_map
[phy_addr
]) {
263 phydev
= bp
->mii_bus
->phy_map
[phy_addr
];
269 printk(KERN_ERR
"%s: no PHY found\n", dev
->name
);
273 /* TODO : add pin_irq */
275 /* attach the mac to the phy */
276 if (bp
->capabilities
& DNET_HAS_RMII
) {
277 phydev
= phy_connect(dev
, dev_name(&phydev
->dev
),
278 &dnet_handle_link_change
,
279 PHY_INTERFACE_MODE_RMII
);
281 phydev
= phy_connect(dev
, dev_name(&phydev
->dev
),
282 &dnet_handle_link_change
,
283 PHY_INTERFACE_MODE_MII
);
286 if (IS_ERR(phydev
)) {
287 printk(KERN_ERR
"%s: Could not attach to PHY\n", dev
->name
);
288 return PTR_ERR(phydev
);
291 /* mask with MAC supported features */
292 if (bp
->capabilities
& DNET_HAS_GIGABIT
)
293 phydev
->supported
&= PHY_GBIT_FEATURES
;
295 phydev
->supported
&= PHY_BASIC_FEATURES
;
297 phydev
->supported
|= SUPPORTED_Asym_Pause
| SUPPORTED_Pause
;
299 phydev
->advertising
= phydev
->supported
;
304 bp
->phy_dev
= phydev
;
309 static int dnet_mii_init(struct dnet
*bp
)
313 bp
->mii_bus
= mdiobus_alloc();
314 if (bp
->mii_bus
== NULL
)
317 bp
->mii_bus
->name
= "dnet_mii_bus";
318 bp
->mii_bus
->read
= &dnet_mdio_read
;
319 bp
->mii_bus
->write
= &dnet_mdio_write
;
321 snprintf(bp
->mii_bus
->id
, MII_BUS_ID_SIZE
, "%s-%x",
322 bp
->pdev
->name
, bp
->pdev
->id
);
324 bp
->mii_bus
->priv
= bp
;
326 bp
->mii_bus
->irq
= devm_kmalloc(&bp
->pdev
->dev
,
327 sizeof(int) * PHY_MAX_ADDR
, GFP_KERNEL
);
328 if (!bp
->mii_bus
->irq
) {
333 for (i
= 0; i
< PHY_MAX_ADDR
; i
++)
334 bp
->mii_bus
->irq
[i
] = PHY_POLL
;
336 if (mdiobus_register(bp
->mii_bus
)) {
341 if (dnet_mii_probe(bp
->dev
) != 0) {
343 goto err_out_unregister_bus
;
348 err_out_unregister_bus
:
349 mdiobus_unregister(bp
->mii_bus
);
351 mdiobus_free(bp
->mii_bus
);
355 /* For Neptune board: LINK1000 as Link LED and TX as activity LED */
356 static int dnet_phy_marvell_fixup(struct phy_device
*phydev
)
358 return phy_write(phydev
, 0x18, 0x4148);
361 static void dnet_update_stats(struct dnet
*bp
)
363 u32 __iomem
*reg
= bp
->regs
+ DNET_RX_PKT_IGNR_CNT
;
364 u32
*p
= &bp
->hw_stats
.rx_pkt_ignr
;
365 u32
*end
= &bp
->hw_stats
.rx_byte
+ 1;
367 WARN_ON((unsigned long)(end
- p
- 1) !=
368 (DNET_RX_BYTE_CNT
- DNET_RX_PKT_IGNR_CNT
) / 4);
370 for (; p
< end
; p
++, reg
++)
373 reg
= bp
->regs
+ DNET_TX_UNICAST_CNT
;
374 p
= &bp
->hw_stats
.tx_unicast
;
375 end
= &bp
->hw_stats
.tx_byte
+ 1;
377 WARN_ON((unsigned long)(end
- p
- 1) !=
378 (DNET_TX_BYTE_CNT
- DNET_TX_UNICAST_CNT
) / 4);
380 for (; p
< end
; p
++, reg
++)
384 static int dnet_poll(struct napi_struct
*napi
, int budget
)
386 struct dnet
*bp
= container_of(napi
, struct dnet
, napi
);
387 struct net_device
*dev
= bp
->dev
;
389 unsigned int pkt_len
;
391 unsigned int *data_ptr
;
396 while (npackets
< budget
) {
398 * break out of while loop if there are no more
401 if (!(dnet_readl(bp
, RX_FIFO_WCNT
) >> 16)) {
403 int_enable
= dnet_readl(bp
, INTR_ENB
);
404 int_enable
|= DNET_INTR_SRC_RX_CMDFIFOAF
;
405 dnet_writel(bp
, int_enable
, INTR_ENB
);
409 cmd_word
= dnet_readl(bp
, RX_LEN_FIFO
);
410 pkt_len
= cmd_word
& 0xFFFF;
412 if (cmd_word
& 0xDF180000)
413 printk(KERN_ERR
"%s packet receive error %x\n",
416 skb
= netdev_alloc_skb(dev
, pkt_len
+ 5);
418 /* Align IP on 16 byte boundaries */
421 * 'skb_put()' points to the start of sk_buff
424 data_ptr
= (unsigned int *)skb_put(skb
, pkt_len
);
425 for (i
= 0; i
< (pkt_len
+ 3) >> 2; i
++)
426 *data_ptr
++ = dnet_readl(bp
, RX_DATA_FIFO
);
427 skb
->protocol
= eth_type_trans(skb
, dev
);
428 netif_receive_skb(skb
);
432 "%s: No memory to allocate a sk_buff of "
433 "size %u.\n", dev
->name
, pkt_len
);
438 if (npackets
< budget
) {
439 /* We processed all packets available. Tell NAPI it can
440 * stop polling then re-enable rx interrupts */
442 int_enable
= dnet_readl(bp
, INTR_ENB
);
443 int_enable
|= DNET_INTR_SRC_RX_CMDFIFOAF
;
444 dnet_writel(bp
, int_enable
, INTR_ENB
);
448 /* There are still packets waiting */
452 static irqreturn_t
dnet_interrupt(int irq
, void *dev_id
)
454 struct net_device
*dev
= dev_id
;
455 struct dnet
*bp
= netdev_priv(dev
);
456 u32 int_src
, int_enable
, int_current
;
458 unsigned int handled
= 0;
460 spin_lock_irqsave(&bp
->lock
, flags
);
462 /* read and clear the DNET irq (clear on read) */
463 int_src
= dnet_readl(bp
, INTR_SRC
);
464 int_enable
= dnet_readl(bp
, INTR_ENB
);
465 int_current
= int_src
& int_enable
;
467 /* restart the queue if we had stopped it for TX fifo almost full */
468 if (int_current
& DNET_INTR_SRC_TX_FIFOAE
) {
469 int_enable
= dnet_readl(bp
, INTR_ENB
);
470 int_enable
&= ~DNET_INTR_ENB_TX_FIFOAE
;
471 dnet_writel(bp
, int_enable
, INTR_ENB
);
472 netif_wake_queue(dev
);
476 /* RX FIFO error checking */
478 (DNET_INTR_SRC_RX_CMDFIFOFF
| DNET_INTR_SRC_RX_DATAFIFOFF
)) {
479 printk(KERN_ERR
"%s: RX fifo error %x, irq %x\n", __func__
,
480 dnet_readl(bp
, RX_STATUS
), int_current
);
481 /* we can only flush the RX FIFOs */
482 dnet_writel(bp
, DNET_SYS_CTL_RXFIFOFLUSH
, SYS_CTL
);
484 dnet_writel(bp
, 0, SYS_CTL
);
488 /* TX FIFO error checking */
490 (DNET_INTR_SRC_TX_FIFOFULL
| DNET_INTR_SRC_TX_DISCFRM
)) {
491 printk(KERN_ERR
"%s: TX fifo error %x, irq %x\n", __func__
,
492 dnet_readl(bp
, TX_STATUS
), int_current
);
493 /* we can only flush the TX FIFOs */
494 dnet_writel(bp
, DNET_SYS_CTL_TXFIFOFLUSH
, SYS_CTL
);
496 dnet_writel(bp
, 0, SYS_CTL
);
500 if (int_current
& DNET_INTR_SRC_RX_CMDFIFOAF
) {
501 if (napi_schedule_prep(&bp
->napi
)) {
503 * There's no point taking any more interrupts
504 * until we have processed the buffers
506 /* Disable Rx interrupts and schedule NAPI poll */
507 int_enable
= dnet_readl(bp
, INTR_ENB
);
508 int_enable
&= ~DNET_INTR_SRC_RX_CMDFIFOAF
;
509 dnet_writel(bp
, int_enable
, INTR_ENB
);
510 __napi_schedule(&bp
->napi
);
516 pr_debug("%s: irq %x remains\n", __func__
, int_current
);
518 spin_unlock_irqrestore(&bp
->lock
, flags
);
520 return IRQ_RETVAL(handled
);
524 static inline void dnet_print_skb(struct sk_buff
*skb
)
527 printk(KERN_DEBUG PFX
"data:");
528 for (k
= 0; k
< skb
->len
; k
++)
529 printk(" %02x", (unsigned int)skb
->data
[k
]);
533 #define dnet_print_skb(skb) do {} while (0)
536 static netdev_tx_t
dnet_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
539 struct dnet
*bp
= netdev_priv(dev
);
540 u32 tx_status
, irq_enable
;
541 unsigned int len
, i
, tx_cmd
, wrsz
;
545 tx_status
= dnet_readl(bp
, TX_STATUS
);
547 pr_debug("start_xmit: len %u head %p data %p\n",
548 skb
->len
, skb
->head
, skb
->data
);
551 /* frame size (words) */
552 len
= (skb
->len
+ 3) >> 2;
554 spin_lock_irqsave(&bp
->lock
, flags
);
556 tx_status
= dnet_readl(bp
, TX_STATUS
);
558 bufp
= (unsigned int *)(((unsigned long) skb
->data
) & ~0x3UL
);
559 wrsz
= (u32
) skb
->len
+ 3;
560 wrsz
+= ((unsigned long) skb
->data
) & 0x3;
562 tx_cmd
= ((((unsigned long)(skb
->data
)) & 0x03) << 16) | (u32
) skb
->len
;
564 /* check if there is enough room for the current frame */
565 if (wrsz
< (DNET_FIFO_SIZE
- dnet_readl(bp
, TX_FIFO_WCNT
))) {
566 for (i
= 0; i
< wrsz
; i
++)
567 dnet_writel(bp
, *bufp
++, TX_DATA_FIFO
);
570 * inform MAC that a packet's written and ready to be
573 dnet_writel(bp
, tx_cmd
, TX_LEN_FIFO
);
576 if (dnet_readl(bp
, TX_FIFO_WCNT
) > DNET_FIFO_TX_DATA_AF_TH
) {
577 netif_stop_queue(dev
);
578 tx_status
= dnet_readl(bp
, INTR_SRC
);
579 irq_enable
= dnet_readl(bp
, INTR_ENB
);
580 irq_enable
|= DNET_INTR_ENB_TX_FIFOAE
;
581 dnet_writel(bp
, irq_enable
, INTR_ENB
);
584 skb_tx_timestamp(skb
);
586 /* free the buffer */
589 spin_unlock_irqrestore(&bp
->lock
, flags
);
594 static void dnet_reset_hw(struct dnet
*bp
)
596 /* put ts_mac in IDLE state i.e. disable rx/tx */
597 dnet_writew_mac(bp
, DNET_INTERNAL_MODE_REG
, DNET_INTERNAL_MODE_FCEN
);
600 * RX FIFO almost full threshold: only cmd FIFO almost full is
601 * implemented for RX side
603 dnet_writel(bp
, DNET_FIFO_RX_CMD_AF_TH
, RX_FIFO_TH
);
605 * TX FIFO almost empty threshold: only data FIFO almost empty
606 * is implemented for TX side
608 dnet_writel(bp
, DNET_FIFO_TX_DATA_AE_TH
, TX_FIFO_TH
);
610 /* flush rx/tx fifos */
611 dnet_writel(bp
, DNET_SYS_CTL_RXFIFOFLUSH
| DNET_SYS_CTL_TXFIFOFLUSH
,
614 dnet_writel(bp
, 0, SYS_CTL
);
617 static void dnet_init_hw(struct dnet
*bp
)
622 __dnet_set_hwaddr(bp
);
624 config
= dnet_readw_mac(bp
, DNET_INTERNAL_RXTX_CONTROL_REG
);
626 if (bp
->dev
->flags
& IFF_PROMISC
)
627 /* Copy All Frames */
628 config
|= DNET_INTERNAL_RXTX_CONTROL_ENPROMISC
;
629 if (!(bp
->dev
->flags
& IFF_BROADCAST
))
631 config
|= DNET_INTERNAL_RXTX_CONTROL_RXMULTICAST
;
633 config
|= DNET_INTERNAL_RXTX_CONTROL_RXPAUSE
|
634 DNET_INTERNAL_RXTX_CONTROL_RXBROADCAST
|
635 DNET_INTERNAL_RXTX_CONTROL_DROPCONTROL
|
636 DNET_INTERNAL_RXTX_CONTROL_DISCFXFCS
;
638 dnet_writew_mac(bp
, DNET_INTERNAL_RXTX_CONTROL_REG
, config
);
640 /* clear irq before enabling them */
641 config
= dnet_readl(bp
, INTR_SRC
);
643 /* enable RX/TX interrupt, recv packet ready interrupt */
644 dnet_writel(bp
, DNET_INTR_ENB_GLOBAL_ENABLE
| DNET_INTR_ENB_RX_SUMMARY
|
645 DNET_INTR_ENB_TX_SUMMARY
| DNET_INTR_ENB_RX_FIFOERR
|
646 DNET_INTR_ENB_RX_ERROR
| DNET_INTR_ENB_RX_FIFOFULL
|
647 DNET_INTR_ENB_TX_FIFOFULL
| DNET_INTR_ENB_TX_DISCFRM
|
648 DNET_INTR_ENB_RX_PKTRDY
, INTR_ENB
);
651 static int dnet_open(struct net_device
*dev
)
653 struct dnet
*bp
= netdev_priv(dev
);
655 /* if the phy is not yet register, retry later */
659 napi_enable(&bp
->napi
);
662 phy_start_aneg(bp
->phy_dev
);
664 /* schedule a link state check */
665 phy_start(bp
->phy_dev
);
667 netif_start_queue(dev
);
672 static int dnet_close(struct net_device
*dev
)
674 struct dnet
*bp
= netdev_priv(dev
);
676 netif_stop_queue(dev
);
677 napi_disable(&bp
->napi
);
680 phy_stop(bp
->phy_dev
);
683 netif_carrier_off(dev
);
688 static inline void dnet_print_pretty_hwstats(struct dnet_stats
*hwstat
)
690 pr_debug("%s\n", __func__
);
691 pr_debug("----------------------------- RX statistics "
692 "-------------------------------\n");
693 pr_debug("RX_PKT_IGNR_CNT %-8x\n", hwstat
->rx_pkt_ignr
);
694 pr_debug("RX_LEN_CHK_ERR_CNT %-8x\n", hwstat
->rx_len_chk_err
);
695 pr_debug("RX_LNG_FRM_CNT %-8x\n", hwstat
->rx_lng_frm
);
696 pr_debug("RX_SHRT_FRM_CNT %-8x\n", hwstat
->rx_shrt_frm
);
697 pr_debug("RX_IPG_VIOL_CNT %-8x\n", hwstat
->rx_ipg_viol
);
698 pr_debug("RX_CRC_ERR_CNT %-8x\n", hwstat
->rx_crc_err
);
699 pr_debug("RX_OK_PKT_CNT %-8x\n", hwstat
->rx_ok_pkt
);
700 pr_debug("RX_CTL_FRM_CNT %-8x\n", hwstat
->rx_ctl_frm
);
701 pr_debug("RX_PAUSE_FRM_CNT %-8x\n", hwstat
->rx_pause_frm
);
702 pr_debug("RX_MULTICAST_CNT %-8x\n", hwstat
->rx_multicast
);
703 pr_debug("RX_BROADCAST_CNT %-8x\n", hwstat
->rx_broadcast
);
704 pr_debug("RX_VLAN_TAG_CNT %-8x\n", hwstat
->rx_vlan_tag
);
705 pr_debug("RX_PRE_SHRINK_CNT %-8x\n", hwstat
->rx_pre_shrink
);
706 pr_debug("RX_DRIB_NIB_CNT %-8x\n", hwstat
->rx_drib_nib
);
707 pr_debug("RX_UNSUP_OPCD_CNT %-8x\n", hwstat
->rx_unsup_opcd
);
708 pr_debug("RX_BYTE_CNT %-8x\n", hwstat
->rx_byte
);
709 pr_debug("----------------------------- TX statistics "
710 "-------------------------------\n");
711 pr_debug("TX_UNICAST_CNT %-8x\n", hwstat
->tx_unicast
);
712 pr_debug("TX_PAUSE_FRM_CNT %-8x\n", hwstat
->tx_pause_frm
);
713 pr_debug("TX_MULTICAST_CNT %-8x\n", hwstat
->tx_multicast
);
714 pr_debug("TX_BRDCAST_CNT %-8x\n", hwstat
->tx_brdcast
);
715 pr_debug("TX_VLAN_TAG_CNT %-8x\n", hwstat
->tx_vlan_tag
);
716 pr_debug("TX_BAD_FCS_CNT %-8x\n", hwstat
->tx_bad_fcs
);
717 pr_debug("TX_JUMBO_CNT %-8x\n", hwstat
->tx_jumbo
);
718 pr_debug("TX_BYTE_CNT %-8x\n", hwstat
->tx_byte
);
721 static struct net_device_stats
*dnet_get_stats(struct net_device
*dev
)
724 struct dnet
*bp
= netdev_priv(dev
);
725 struct net_device_stats
*nstat
= &dev
->stats
;
726 struct dnet_stats
*hwstat
= &bp
->hw_stats
;
728 /* read stats from hardware */
729 dnet_update_stats(bp
);
731 /* Convert HW stats into netdevice stats */
732 nstat
->rx_errors
= (hwstat
->rx_len_chk_err
+
733 hwstat
->rx_lng_frm
+ hwstat
->rx_shrt_frm
+
734 /* ignore IGP violation error
735 hwstat->rx_ipg_viol + */
737 hwstat
->rx_pre_shrink
+
738 hwstat
->rx_drib_nib
+ hwstat
->rx_unsup_opcd
);
739 nstat
->tx_errors
= hwstat
->tx_bad_fcs
;
740 nstat
->rx_length_errors
= (hwstat
->rx_len_chk_err
+
742 hwstat
->rx_shrt_frm
+ hwstat
->rx_pre_shrink
);
743 nstat
->rx_crc_errors
= hwstat
->rx_crc_err
;
744 nstat
->rx_frame_errors
= hwstat
->rx_pre_shrink
+ hwstat
->rx_drib_nib
;
745 nstat
->rx_packets
= hwstat
->rx_ok_pkt
;
746 nstat
->tx_packets
= (hwstat
->tx_unicast
+
747 hwstat
->tx_multicast
+ hwstat
->tx_brdcast
);
748 nstat
->rx_bytes
= hwstat
->rx_byte
;
749 nstat
->tx_bytes
= hwstat
->tx_byte
;
750 nstat
->multicast
= hwstat
->rx_multicast
;
751 nstat
->rx_missed_errors
= hwstat
->rx_pkt_ignr
;
753 dnet_print_pretty_hwstats(hwstat
);
758 static int dnet_get_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
760 struct dnet
*bp
= netdev_priv(dev
);
761 struct phy_device
*phydev
= bp
->phy_dev
;
766 return phy_ethtool_gset(phydev
, cmd
);
769 static int dnet_set_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
771 struct dnet
*bp
= netdev_priv(dev
);
772 struct phy_device
*phydev
= bp
->phy_dev
;
777 return phy_ethtool_sset(phydev
, cmd
);
780 static int dnet_ioctl(struct net_device
*dev
, struct ifreq
*rq
, int cmd
)
782 struct dnet
*bp
= netdev_priv(dev
);
783 struct phy_device
*phydev
= bp
->phy_dev
;
785 if (!netif_running(dev
))
791 return phy_mii_ioctl(phydev
, rq
, cmd
);
794 static void dnet_get_drvinfo(struct net_device
*dev
,
795 struct ethtool_drvinfo
*info
)
797 strlcpy(info
->driver
, DRV_NAME
, sizeof(info
->driver
));
798 strlcpy(info
->version
, DRV_VERSION
, sizeof(info
->version
));
799 strlcpy(info
->bus_info
, "0", sizeof(info
->bus_info
));
802 static const struct ethtool_ops dnet_ethtool_ops
= {
803 .get_settings
= dnet_get_settings
,
804 .set_settings
= dnet_set_settings
,
805 .get_drvinfo
= dnet_get_drvinfo
,
806 .get_link
= ethtool_op_get_link
,
807 .get_ts_info
= ethtool_op_get_ts_info
,
810 static const struct net_device_ops dnet_netdev_ops
= {
811 .ndo_open
= dnet_open
,
812 .ndo_stop
= dnet_close
,
813 .ndo_get_stats
= dnet_get_stats
,
814 .ndo_start_xmit
= dnet_start_xmit
,
815 .ndo_do_ioctl
= dnet_ioctl
,
816 .ndo_set_mac_address
= eth_mac_addr
,
817 .ndo_validate_addr
= eth_validate_addr
,
818 .ndo_change_mtu
= eth_change_mtu
,
821 static int dnet_probe(struct platform_device
*pdev
)
823 struct resource
*res
;
824 struct net_device
*dev
;
826 struct phy_device
*phydev
;
830 irq
= platform_get_irq(pdev
, 0);
832 dev
= alloc_etherdev(sizeof(*bp
));
836 /* TODO: Actually, we have some interesting features... */
839 bp
= netdev_priv(dev
);
842 platform_set_drvdata(pdev
, dev
);
843 SET_NETDEV_DEV(dev
, &pdev
->dev
);
845 spin_lock_init(&bp
->lock
);
847 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
848 bp
->regs
= devm_ioremap_resource(&pdev
->dev
, res
);
849 if (IS_ERR(bp
->regs
)) {
850 err
= PTR_ERR(bp
->regs
);
851 goto err_out_free_dev
;
855 err
= request_irq(dev
->irq
, dnet_interrupt
, 0, DRV_NAME
, dev
);
857 dev_err(&pdev
->dev
, "Unable to request IRQ %d (error %d)\n",
859 goto err_out_free_dev
;
862 dev
->netdev_ops
= &dnet_netdev_ops
;
863 netif_napi_add(dev
, &bp
->napi
, dnet_poll
, 64);
864 dev
->ethtool_ops
= &dnet_ethtool_ops
;
866 dev
->base_addr
= (unsigned long)bp
->regs
;
868 bp
->capabilities
= dnet_readl(bp
, VERCAPS
) & DNET_CAPS_MASK
;
872 if (!is_valid_ether_addr(dev
->dev_addr
)) {
873 /* choose a random ethernet address */
874 eth_hw_addr_random(dev
);
875 __dnet_set_hwaddr(bp
);
878 err
= register_netdev(dev
);
880 dev_err(&pdev
->dev
, "Cannot register net device, aborting.\n");
881 goto err_out_free_irq
;
884 /* register the PHY board fixup (for Marvell 88E1111) */
885 err
= phy_register_fixup_for_uid(0x01410cc0, 0xfffffff0,
886 dnet_phy_marvell_fixup
);
887 /* we can live without it, so just issue a warning */
889 dev_warn(&pdev
->dev
, "Cannot register PHY board fixup.\n");
891 err
= dnet_mii_init(bp
);
893 goto err_out_unregister_netdev
;
895 dev_info(&pdev
->dev
, "Dave DNET at 0x%p (0x%08x) irq %d %pM\n",
896 bp
->regs
, (unsigned int)res
->start
, dev
->irq
, dev
->dev_addr
);
897 dev_info(&pdev
->dev
, "has %smdio, %sirq, %sgigabit, %sdma\n",
898 (bp
->capabilities
& DNET_HAS_MDIO
) ? "" : "no ",
899 (bp
->capabilities
& DNET_HAS_IRQ
) ? "" : "no ",
900 (bp
->capabilities
& DNET_HAS_GIGABIT
) ? "" : "no ",
901 (bp
->capabilities
& DNET_HAS_DMA
) ? "" : "no ");
902 phydev
= bp
->phy_dev
;
903 dev_info(&pdev
->dev
, "attached PHY driver [%s] "
904 "(mii_bus:phy_addr=%s, irq=%d)\n",
905 phydev
->drv
->name
, dev_name(&phydev
->dev
), phydev
->irq
);
909 err_out_unregister_netdev
:
910 unregister_netdev(dev
);
912 free_irq(dev
->irq
, dev
);
918 static int dnet_remove(struct platform_device
*pdev
)
921 struct net_device
*dev
;
924 dev
= platform_get_drvdata(pdev
);
927 bp
= netdev_priv(dev
);
929 phy_disconnect(bp
->phy_dev
);
930 mdiobus_unregister(bp
->mii_bus
);
931 mdiobus_free(bp
->mii_bus
);
932 unregister_netdev(dev
);
933 free_irq(dev
->irq
, dev
);
940 static struct platform_driver dnet_driver
= {
942 .remove
= dnet_remove
,
948 module_platform_driver(dnet_driver
);
950 MODULE_LICENSE("GPL");
951 MODULE_DESCRIPTION("Dave DNET Ethernet driver");
952 MODULE_AUTHOR("Ilya Yanok <yanok@emcraft.com>, "
953 "Matteo Vit <matteo.vit@dave.eu>");