2 * Atheros AR71xx built-in ethernet mac driver
4 * Copyright (C) 2008-2009 Gabor Juhos <juhosg@openwrt.org>
5 * Copyright (C) 2008 Imre Kaloz <kaloz@openwrt.org>
7 * Based on Atheros' AG7100 driver
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License version 2 as published
11 * by the Free Software Foundation.
14 #include <linux/cache.h>
17 #define AG71XX_DEFAULT_MSG_ENABLE \
27 static int ag71xx_msg_level
= -1;
29 module_param_named(msg_level
, ag71xx_msg_level
, int, 0);
30 MODULE_PARM_DESC(msg_level
, "Message level (-1=defaults,0=none,...,16=all)");
32 static void ag71xx_dump_dma_regs(struct ag71xx
*ag
)
34 DBG("%s: dma_tx_ctrl=%08x, dma_tx_desc=%08x, dma_tx_status=%08x\n",
36 ag71xx_rr(ag
, AG71XX_REG_TX_CTRL
),
37 ag71xx_rr(ag
, AG71XX_REG_TX_DESC
),
38 ag71xx_rr(ag
, AG71XX_REG_TX_STATUS
));
40 DBG("%s: dma_rx_ctrl=%08x, dma_rx_desc=%08x, dma_rx_status=%08x\n",
42 ag71xx_rr(ag
, AG71XX_REG_RX_CTRL
),
43 ag71xx_rr(ag
, AG71XX_REG_RX_DESC
),
44 ag71xx_rr(ag
, AG71XX_REG_RX_STATUS
));
47 static void ag71xx_dump_regs(struct ag71xx
*ag
)
49 DBG("%s: mac_cfg1=%08x, mac_cfg2=%08x, ipg=%08x, hdx=%08x, mfl=%08x\n",
51 ag71xx_rr(ag
, AG71XX_REG_MAC_CFG1
),
52 ag71xx_rr(ag
, AG71XX_REG_MAC_CFG2
),
53 ag71xx_rr(ag
, AG71XX_REG_MAC_IPG
),
54 ag71xx_rr(ag
, AG71XX_REG_MAC_HDX
),
55 ag71xx_rr(ag
, AG71XX_REG_MAC_MFL
));
56 DBG("%s: mac_ifctl=%08x, mac_addr1=%08x, mac_addr2=%08x\n",
58 ag71xx_rr(ag
, AG71XX_REG_MAC_IFCTL
),
59 ag71xx_rr(ag
, AG71XX_REG_MAC_ADDR1
),
60 ag71xx_rr(ag
, AG71XX_REG_MAC_ADDR2
));
61 DBG("%s: fifo_cfg0=%08x, fifo_cfg1=%08x, fifo_cfg2=%08x\n",
63 ag71xx_rr(ag
, AG71XX_REG_FIFO_CFG0
),
64 ag71xx_rr(ag
, AG71XX_REG_FIFO_CFG1
),
65 ag71xx_rr(ag
, AG71XX_REG_FIFO_CFG2
));
66 DBG("%s: fifo_cfg3=%08x, fifo_cfg4=%08x, fifo_cfg5=%08x\n",
68 ag71xx_rr(ag
, AG71XX_REG_FIFO_CFG3
),
69 ag71xx_rr(ag
, AG71XX_REG_FIFO_CFG4
),
70 ag71xx_rr(ag
, AG71XX_REG_FIFO_CFG5
));
73 static inline void ag71xx_dump_intr(struct ag71xx
*ag
, char *label
, u32 intr
)
75 DBG("%s: %s intr=%08x %s%s%s%s%s%s\n",
76 ag
->dev
->name
, label
, intr
,
77 (intr
& AG71XX_INT_TX_PS
) ? "TXPS " : "",
78 (intr
& AG71XX_INT_TX_UR
) ? "TXUR " : "",
79 (intr
& AG71XX_INT_TX_BE
) ? "TXBE " : "",
80 (intr
& AG71XX_INT_RX_PR
) ? "RXPR " : "",
81 (intr
& AG71XX_INT_RX_OF
) ? "RXOF " : "",
82 (intr
& AG71XX_INT_RX_BE
) ? "RXBE " : "");
85 static void ag71xx_ring_free(struct ag71xx_ring
*ring
)
90 dma_free_coherent(NULL
, ring
->size
* ring
->desc_size
,
91 ring
->descs_cpu
, ring
->descs_dma
);
94 static int ag71xx_ring_alloc(struct ag71xx_ring
*ring
, unsigned int size
)
99 ring
->desc_size
= sizeof(struct ag71xx_desc
);
100 if (ring
->desc_size
% cache_line_size()) {
101 DBG("ag71xx: ring %p, desc size %u rounded to %u\n",
102 ring
, ring
->desc_size
,
103 roundup(ring
->desc_size
, cache_line_size()));
104 ring
->desc_size
= roundup(ring
->desc_size
, cache_line_size());
107 ring
->descs_cpu
= dma_alloc_coherent(NULL
, size
* ring
->desc_size
,
108 &ring
->descs_dma
, GFP_ATOMIC
);
109 if (!ring
->descs_cpu
) {
116 ring
->buf
= kzalloc(size
* sizeof(*ring
->buf
), GFP_KERNEL
);
122 for (i
= 0; i
< size
; i
++) {
123 ring
->buf
[i
].desc
= (struct ag71xx_desc
*)&ring
->descs_cpu
[i
* ring
->desc_size
];
124 DBG("ag71xx: ring %p, desc %d at %p\n",
125 ring
, i
, ring
->buf
[i
].desc
);
134 static void ag71xx_ring_tx_clean(struct ag71xx
*ag
)
136 struct ag71xx_ring
*ring
= &ag
->tx_ring
;
137 struct net_device
*dev
= ag
->dev
;
139 while (ring
->curr
!= ring
->dirty
) {
140 u32 i
= ring
->dirty
% AG71XX_TX_RING_SIZE
;
142 if (!ag71xx_desc_empty(ring
->buf
[i
].desc
)) {
143 ring
->buf
[i
].desc
->ctrl
= 0;
144 dev
->stats
.tx_errors
++;
147 if (ring
->buf
[i
].skb
)
148 dev_kfree_skb_any(ring
->buf
[i
].skb
);
150 ring
->buf
[i
].skb
= NULL
;
155 /* flush descriptors */
160 static void ag71xx_ring_tx_init(struct ag71xx
*ag
)
162 struct ag71xx_ring
*ring
= &ag
->tx_ring
;
165 for (i
= 0; i
< AG71XX_TX_RING_SIZE
; i
++) {
166 ring
->buf
[i
].desc
->next
= (u32
) (ring
->descs_dma
+
167 ring
->desc_size
* ((i
+ 1) % AG71XX_TX_RING_SIZE
));
169 ring
->buf
[i
].desc
->ctrl
= DESC_EMPTY
;
170 ring
->buf
[i
].skb
= NULL
;
173 /* flush descriptors */
180 static void ag71xx_ring_rx_clean(struct ag71xx
*ag
)
182 struct ag71xx_ring
*ring
= &ag
->rx_ring
;
188 for (i
= 0; i
< AG71XX_RX_RING_SIZE
; i
++)
189 if (ring
->buf
[i
].skb
)
190 kfree_skb(ring
->buf
[i
].skb
);
194 static int ag71xx_ring_rx_init(struct ag71xx
*ag
)
196 struct ag71xx_ring
*ring
= &ag
->rx_ring
;
201 for (i
= 0; i
< AG71XX_RX_RING_SIZE
; i
++) {
202 ring
->buf
[i
].desc
->next
= (u32
) (ring
->descs_dma
+
203 ring
->desc_size
* ((i
+ 1) % AG71XX_RX_RING_SIZE
));
205 DBG("ag71xx: RX desc at %p, next is %08x\n",
207 ring
->buf
[i
].desc
->next
);
210 for (i
= 0; i
< AG71XX_RX_RING_SIZE
; i
++) {
213 skb
= dev_alloc_skb(AG71XX_RX_PKT_SIZE
);
219 dma_map_single(NULL
, skb
->data
, AG71XX_RX_PKT_SIZE
,
223 skb_reserve(skb
, AG71XX_RX_PKT_RESERVE
);
225 ring
->buf
[i
].skb
= skb
;
226 ring
->buf
[i
].desc
->data
= virt_to_phys(skb
->data
);
227 ring
->buf
[i
].desc
->ctrl
= DESC_EMPTY
;
230 /* flush descriptors */
239 static int ag71xx_ring_rx_refill(struct ag71xx
*ag
)
241 struct ag71xx_ring
*ring
= &ag
->rx_ring
;
245 for (; ring
->curr
- ring
->dirty
> 0; ring
->dirty
++) {
248 i
= ring
->dirty
% AG71XX_RX_RING_SIZE
;
250 if (ring
->buf
[i
].skb
== NULL
) {
253 skb
= dev_alloc_skb(AG71XX_RX_PKT_SIZE
);
257 dma_map_single(NULL
, skb
->data
, AG71XX_RX_PKT_SIZE
,
260 skb_reserve(skb
, AG71XX_RX_PKT_RESERVE
);
263 ring
->buf
[i
].skb
= skb
;
264 ring
->buf
[i
].desc
->data
= virt_to_phys(skb
->data
);
267 ring
->buf
[i
].desc
->ctrl
= DESC_EMPTY
;
271 /* flush descriptors */
274 DBG("%s: %u rx descriptors refilled\n", ag
->dev
->name
, count
);
279 static int ag71xx_rings_init(struct ag71xx
*ag
)
283 ret
= ag71xx_ring_alloc(&ag
->tx_ring
, AG71XX_TX_RING_SIZE
);
287 ag71xx_ring_tx_init(ag
);
289 ret
= ag71xx_ring_alloc(&ag
->rx_ring
, AG71XX_RX_RING_SIZE
);
293 ret
= ag71xx_ring_rx_init(ag
);
297 static void ag71xx_rings_cleanup(struct ag71xx
*ag
)
299 ag71xx_ring_rx_clean(ag
);
300 ag71xx_ring_free(&ag
->rx_ring
);
302 ag71xx_ring_tx_clean(ag
);
303 ag71xx_ring_free(&ag
->tx_ring
);
306 static void ag71xx_hw_set_macaddr(struct ag71xx
*ag
, unsigned char *mac
)
310 t
= (((u32
) mac
[0]) << 24) | (((u32
) mac
[1]) << 16)
311 | (((u32
) mac
[2]) << 8) | ((u32
) mac
[3]);
313 ag71xx_wr(ag
, AG71XX_REG_MAC_ADDR1
, t
);
315 t
= (((u32
) mac
[4]) << 24) | (((u32
) mac
[5]) << 16);
316 ag71xx_wr(ag
, AG71XX_REG_MAC_ADDR2
, t
);
319 static void ag71xx_dma_reset(struct ag71xx
*ag
)
324 ag71xx_dump_dma_regs(ag
);
327 ag71xx_wr(ag
, AG71XX_REG_RX_CTRL
, 0);
328 ag71xx_wr(ag
, AG71XX_REG_TX_CTRL
, 0);
330 /* clear descriptor addresses */
331 ag71xx_wr(ag
, AG71XX_REG_TX_DESC
, 0);
332 ag71xx_wr(ag
, AG71XX_REG_RX_DESC
, 0);
334 /* clear pending RX/TX interrupts */
335 for (i
= 0; i
< 256; i
++) {
336 ag71xx_wr(ag
, AG71XX_REG_RX_STATUS
, RX_STATUS_PR
);
337 ag71xx_wr(ag
, AG71XX_REG_TX_STATUS
, TX_STATUS_PS
);
340 /* clear pending errors */
341 ag71xx_wr(ag
, AG71XX_REG_RX_STATUS
, RX_STATUS_BE
| RX_STATUS_OF
);
342 ag71xx_wr(ag
, AG71XX_REG_TX_STATUS
, TX_STATUS_BE
| TX_STATUS_UR
);
344 val
= ag71xx_rr(ag
, AG71XX_REG_RX_STATUS
);
346 printk(KERN_ALERT
"%s: unable to clear DMA Rx status: %08x\n",
349 val
= ag71xx_rr(ag
, AG71XX_REG_TX_STATUS
);
351 /* mask out reserved bits */
355 printk(KERN_ALERT
"%s: unable to clear DMA Tx status: %08x\n",
358 ag71xx_dump_dma_regs(ag
);
361 #define MAC_CFG1_INIT (MAC_CFG1_RXE | MAC_CFG1_TXE | \
362 MAC_CFG1_SRX | MAC_CFG1_STX)
364 #define FIFO_CFG0_INIT (FIFO_CFG0_ALL << FIFO_CFG0_ENABLE_SHIFT)
366 #define FIFO_CFG4_INIT (FIFO_CFG4_DE | FIFO_CFG4_DV | FIFO_CFG4_FC | \
367 FIFO_CFG4_CE | FIFO_CFG4_CR | FIFO_CFG4_LM | \
368 FIFO_CFG4_LO | FIFO_CFG4_OK | FIFO_CFG4_MC | \
369 FIFO_CFG4_BC | FIFO_CFG4_DR | FIFO_CFG4_LE | \
370 FIFO_CFG4_CF | FIFO_CFG4_PF | FIFO_CFG4_UO | \
373 #define FIFO_CFG5_INIT (FIFO_CFG5_DE | FIFO_CFG5_DV | FIFO_CFG5_FC | \
374 FIFO_CFG5_CE | FIFO_CFG5_LO | FIFO_CFG5_OK | \
375 FIFO_CFG5_MC | FIFO_CFG5_BC | FIFO_CFG5_DR | \
376 FIFO_CFG5_CF | FIFO_CFG5_PF | FIFO_CFG5_VT | \
377 FIFO_CFG5_LE | FIFO_CFG5_FT | FIFO_CFG5_16 | \
378 FIFO_CFG5_17 | FIFO_CFG5_SF)
380 static void ag71xx_hw_init(struct ag71xx
*ag
)
382 struct ag71xx_platform_data
*pdata
= ag71xx_get_pdata(ag
);
384 ag71xx_sb(ag
, AG71XX_REG_MAC_CFG1
, MAC_CFG1_SR
);
387 ar71xx_device_stop(pdata
->reset_bit
);
389 ar71xx_device_start(pdata
->reset_bit
);
392 /* setup MAC configuration registers */
393 ag71xx_wr(ag
, AG71XX_REG_MAC_CFG1
, MAC_CFG1_INIT
);
394 ag71xx_sb(ag
, AG71XX_REG_MAC_CFG2
,
395 MAC_CFG2_PAD_CRC_EN
| MAC_CFG2_LEN_CHECK
);
397 /* setup max frame length */
398 ag71xx_wr(ag
, AG71XX_REG_MAC_MFL
, AG71XX_TX_MTU_LEN
);
400 /* setup MII interface type */
401 ag71xx_mii_ctrl_set_if(ag
, pdata
->mii_if
);
403 /* setup FIFO configuration registers */
404 ag71xx_wr(ag
, AG71XX_REG_FIFO_CFG0
, FIFO_CFG0_INIT
);
405 if (pdata
->is_ar724x
) {
406 ag71xx_wr(ag
, AG71XX_REG_FIFO_CFG1
, pdata
->fifo_cfg1
);
407 ag71xx_wr(ag
, AG71XX_REG_FIFO_CFG2
, pdata
->fifo_cfg2
);
409 ag71xx_wr(ag
, AG71XX_REG_FIFO_CFG1
, 0x0fff0000);
410 ag71xx_wr(ag
, AG71XX_REG_FIFO_CFG2
, 0x00001fff);
412 ag71xx_wr(ag
, AG71XX_REG_FIFO_CFG4
, FIFO_CFG4_INIT
);
413 ag71xx_wr(ag
, AG71XX_REG_FIFO_CFG5
, FIFO_CFG5_INIT
);
415 ag71xx_dma_reset(ag
);
418 static void ag71xx_hw_start(struct ag71xx
*ag
)
420 /* start RX engine */
421 ag71xx_wr(ag
, AG71XX_REG_RX_CTRL
, RX_CTRL_RXE
);
423 /* enable interrupts */
424 ag71xx_wr(ag
, AG71XX_REG_INT_ENABLE
, AG71XX_INT_INIT
);
427 static void ag71xx_hw_stop(struct ag71xx
*ag
)
429 /* disable all interrupts */
430 ag71xx_wr(ag
, AG71XX_REG_INT_ENABLE
, 0);
432 ag71xx_dma_reset(ag
);
435 static int ag71xx_open(struct net_device
*dev
)
437 struct ag71xx
*ag
= netdev_priv(dev
);
440 ret
= ag71xx_rings_init(ag
);
444 napi_enable(&ag
->napi
);
446 netif_carrier_off(dev
);
447 ag71xx_phy_start(ag
);
449 ag71xx_wr(ag
, AG71XX_REG_TX_DESC
, ag
->tx_ring
.descs_dma
);
450 ag71xx_wr(ag
, AG71XX_REG_RX_DESC
, ag
->rx_ring
.descs_dma
);
452 ag71xx_hw_set_macaddr(ag
, dev
->dev_addr
);
456 netif_start_queue(dev
);
461 ag71xx_rings_cleanup(ag
);
465 static int ag71xx_stop(struct net_device
*dev
)
467 struct ag71xx
*ag
= netdev_priv(dev
);
470 spin_lock_irqsave(&ag
->lock
, flags
);
472 netif_stop_queue(dev
);
476 netif_carrier_off(dev
);
479 napi_disable(&ag
->napi
);
480 del_timer_sync(&ag
->oom_timer
);
482 spin_unlock_irqrestore(&ag
->lock
, flags
);
484 ag71xx_rings_cleanup(ag
);
489 static int ag71xx_hard_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
491 struct ag71xx
*ag
= netdev_priv(dev
);
492 struct ag71xx_ring
*ring
= &ag
->tx_ring
;
493 struct ag71xx_desc
*desc
;
496 i
= ring
->curr
% AG71XX_TX_RING_SIZE
;
497 desc
= ring
->buf
[i
].desc
;
499 if (!ag71xx_desc_empty(desc
))
502 ag71xx_add_ar8216_header(ag
, skb
);
505 DBG("%s: packet len is too small\n", ag
->dev
->name
);
509 dma_map_single(NULL
, skb
->data
, skb
->len
, DMA_TO_DEVICE
);
511 ring
->buf
[i
].skb
= skb
;
513 /* setup descriptor fields */
514 desc
->data
= virt_to_phys(skb
->data
);
515 desc
->ctrl
= (skb
->len
& DESC_PKTLEN_M
);
517 /* flush descriptor */
521 if (ring
->curr
== (ring
->dirty
+ AG71XX_TX_THRES_STOP
)) {
522 DBG("%s: tx queue full\n", ag
->dev
->name
);
523 netif_stop_queue(dev
);
526 DBG("%s: packet injected into TX queue\n", ag
->dev
->name
);
528 /* enable TX engine */
529 ag71xx_wr(ag
, AG71XX_REG_TX_CTRL
, TX_CTRL_TXE
);
531 dev
->trans_start
= jiffies
;
536 dev
->stats
.tx_dropped
++;
542 static int ag71xx_do_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
)
544 struct mii_ioctl_data
*data
= (struct mii_ioctl_data
*) &ifr
->ifr_data
;
545 struct ag71xx
*ag
= netdev_priv(dev
);
550 if (ag
->phy_dev
== NULL
)
553 spin_lock_irq(&ag
->lock
);
554 ret
= phy_ethtool_ioctl(ag
->phy_dev
, (void *) ifr
->ifr_data
);
555 spin_unlock_irq(&ag
->lock
);
560 (dev
->dev_addr
, ifr
->ifr_data
, sizeof(dev
->dev_addr
)))
566 (ifr
->ifr_data
, dev
->dev_addr
, sizeof(dev
->dev_addr
)))
573 if (ag
->phy_dev
== NULL
)
576 return phy_mii_ioctl(ag
->phy_dev
, data
, cmd
);
585 static void ag71xx_oom_timer_handler(unsigned long data
)
587 struct net_device
*dev
= (struct net_device
*) data
;
588 struct ag71xx
*ag
= netdev_priv(dev
);
590 napi_schedule(&ag
->napi
);
593 static void ag71xx_tx_timeout(struct net_device
*dev
)
595 struct ag71xx
*ag
= netdev_priv(dev
);
597 if (netif_msg_tx_err(ag
))
598 printk(KERN_DEBUG
"%s: tx timeout\n", ag
->dev
->name
);
600 schedule_work(&ag
->restart_work
);
603 static void ag71xx_restart_work_func(struct work_struct
*work
)
605 struct ag71xx
*ag
= container_of(work
, struct ag71xx
, restart_work
);
607 ag71xx_stop(ag
->dev
);
608 ag71xx_open(ag
->dev
);
611 static void ag71xx_tx_packets(struct ag71xx
*ag
)
613 struct ag71xx_ring
*ring
= &ag
->tx_ring
;
616 DBG("%s: processing TX ring\n", ag
->dev
->name
);
619 while (ring
->dirty
!= ring
->curr
) {
620 unsigned int i
= ring
->dirty
% AG71XX_TX_RING_SIZE
;
621 struct ag71xx_desc
*desc
= ring
->buf
[i
].desc
;
622 struct sk_buff
*skb
= ring
->buf
[i
].skb
;
624 if (!ag71xx_desc_empty(desc
))
627 ag71xx_wr(ag
, AG71XX_REG_TX_STATUS
, TX_STATUS_PS
);
629 ag
->dev
->stats
.tx_bytes
+= skb
->len
;
630 ag
->dev
->stats
.tx_packets
++;
632 dev_kfree_skb_any(skb
);
633 ring
->buf
[i
].skb
= NULL
;
639 DBG("%s: %d packets sent out\n", ag
->dev
->name
, sent
);
641 if ((ring
->curr
- ring
->dirty
) < AG71XX_TX_THRES_WAKEUP
)
642 netif_wake_queue(ag
->dev
);
646 static int ag71xx_rx_packets(struct ag71xx
*ag
, int limit
)
648 struct net_device
*dev
= ag
->dev
;
649 struct ag71xx_ring
*ring
= &ag
->rx_ring
;
652 DBG("%s: rx packets, limit=%d, curr=%u, dirty=%u\n",
653 dev
->name
, limit
, ring
->curr
, ring
->dirty
);
655 while (done
< limit
) {
656 unsigned int i
= ring
->curr
% AG71XX_RX_RING_SIZE
;
657 struct ag71xx_desc
*desc
= ring
->buf
[i
].desc
;
661 if (ag71xx_desc_empty(desc
))
664 if ((ring
->dirty
+ AG71XX_RX_RING_SIZE
) == ring
->curr
) {
669 ag71xx_wr(ag
, AG71XX_REG_RX_STATUS
, RX_STATUS_PR
);
671 skb
= ring
->buf
[i
].skb
;
672 pktlen
= ag71xx_desc_pktlen(desc
);
673 pktlen
-= ETH_FCS_LEN
;
675 skb_put(skb
, pktlen
);
678 skb
->ip_summed
= CHECKSUM_NONE
;
680 dev
->last_rx
= jiffies
;
681 dev
->stats
.rx_packets
++;
682 dev
->stats
.rx_bytes
+= pktlen
;
684 if (ag71xx_remove_ar8216_header(ag
, skb
) != 0) {
685 dev
->stats
.rx_dropped
++;
688 skb
->protocol
= eth_type_trans(skb
, dev
);
689 netif_receive_skb(skb
);
692 ring
->buf
[i
].skb
= NULL
;
698 ag71xx_ring_rx_refill(ag
);
700 DBG("%s: rx finish, curr=%u, dirty=%u, done=%d\n",
701 dev
->name
, ring
->curr
, ring
->dirty
, done
);
706 static int ag71xx_poll(struct napi_struct
*napi
, int limit
)
708 struct ag71xx
*ag
= container_of(napi
, struct ag71xx
, napi
);
709 struct ag71xx_platform_data
*pdata
= ag71xx_get_pdata(ag
);
710 struct net_device
*dev
= ag
->dev
;
711 struct ag71xx_ring
*rx_ring
;
717 ag71xx_tx_packets(ag
);
719 DBG("%s: processing RX ring\n", dev
->name
);
720 done
= ag71xx_rx_packets(ag
, limit
);
722 rx_ring
= &ag
->rx_ring
;
723 if (rx_ring
->buf
[rx_ring
->dirty
% AG71XX_RX_RING_SIZE
].skb
== NULL
)
726 status
= ag71xx_rr(ag
, AG71XX_REG_RX_STATUS
);
727 if (unlikely(status
& RX_STATUS_OF
)) {
728 ag71xx_wr(ag
, AG71XX_REG_RX_STATUS
, RX_STATUS_OF
);
729 dev
->stats
.rx_fifo_errors
++;
732 ag71xx_wr(ag
, AG71XX_REG_RX_CTRL
, RX_CTRL_RXE
);
736 if (status
& RX_STATUS_PR
)
739 status
= ag71xx_rr(ag
, AG71XX_REG_TX_STATUS
);
740 if (status
& TX_STATUS_PS
)
743 DBG("%s: disable polling mode, done=%d, limit=%d\n",
744 dev
->name
, done
, limit
);
748 /* enable interrupts */
749 spin_lock_irqsave(&ag
->lock
, flags
);
750 ag71xx_int_enable(ag
, AG71XX_INT_POLL
);
751 spin_unlock_irqrestore(&ag
->lock
, flags
);
756 DBG("%s: stay in polling mode, done=%d, limit=%d\n",
757 dev
->name
, done
, limit
);
761 if (netif_msg_rx_err(ag
))
762 printk(KERN_DEBUG
"%s: out of memory\n", dev
->name
);
764 mod_timer(&ag
->oom_timer
, jiffies
+ AG71XX_OOM_REFILL
);
769 static irqreturn_t
ag71xx_interrupt(int irq
, void *dev_id
)
771 struct net_device
*dev
= dev_id
;
772 struct ag71xx
*ag
= netdev_priv(dev
);
775 status
= ag71xx_rr(ag
, AG71XX_REG_INT_STATUS
);
776 ag71xx_dump_intr(ag
, "raw", status
);
778 if (unlikely(!status
))
781 if (unlikely(status
& AG71XX_INT_ERR
)) {
782 if (status
& AG71XX_INT_TX_BE
) {
783 ag71xx_wr(ag
, AG71XX_REG_TX_STATUS
, TX_STATUS_BE
);
784 dev_err(&dev
->dev
, "TX BUS error\n");
786 if (status
& AG71XX_INT_RX_BE
) {
787 ag71xx_wr(ag
, AG71XX_REG_RX_STATUS
, RX_STATUS_BE
);
788 dev_err(&dev
->dev
, "RX BUS error\n");
792 if (likely(status
& AG71XX_INT_POLL
)) {
793 ag71xx_int_disable(ag
, AG71XX_INT_POLL
);
794 DBG("%s: enable polling mode\n", dev
->name
);
795 napi_schedule(&ag
->napi
);
801 static void ag71xx_set_multicast_list(struct net_device
*dev
)
806 static const struct net_device_ops ag71xx_netdev_ops
= {
807 .ndo_open
= ag71xx_open
,
808 .ndo_stop
= ag71xx_stop
,
809 .ndo_start_xmit
= ag71xx_hard_start_xmit
,
810 .ndo_set_multicast_list
= ag71xx_set_multicast_list
,
811 .ndo_do_ioctl
= ag71xx_do_ioctl
,
812 .ndo_tx_timeout
= ag71xx_tx_timeout
,
813 .ndo_change_mtu
= eth_change_mtu
,
814 .ndo_set_mac_address
= eth_mac_addr
,
815 .ndo_validate_addr
= eth_validate_addr
,
818 static int __init
ag71xx_probe(struct platform_device
*pdev
)
820 struct net_device
*dev
;
821 struct resource
*res
;
823 struct ag71xx_platform_data
*pdata
;
826 pdata
= pdev
->dev
.platform_data
;
828 dev_err(&pdev
->dev
, "no platform data specified\n");
833 if (pdata
->mii_bus_dev
== NULL
) {
834 dev_err(&pdev
->dev
, "no MII bus device specified\n");
839 dev
= alloc_etherdev(sizeof(*ag
));
841 dev_err(&pdev
->dev
, "alloc_etherdev failed\n");
846 SET_NETDEV_DEV(dev
, &pdev
->dev
);
848 ag
= netdev_priv(dev
);
851 ag
->msg_enable
= netif_msg_init(ag71xx_msg_level
,
852 AG71XX_DEFAULT_MSG_ENABLE
);
853 spin_lock_init(&ag
->lock
);
855 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "mac_base");
857 dev_err(&pdev
->dev
, "no mac_base resource found\n");
862 ag
->mac_base
= ioremap_nocache(res
->start
, res
->end
- res
->start
+ 1);
864 dev_err(&pdev
->dev
, "unable to ioremap mac_base\n");
869 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "mii_ctrl");
871 dev_err(&pdev
->dev
, "no mii_ctrl resource found\n");
876 ag
->mii_ctrl
= ioremap_nocache(res
->start
, res
->end
- res
->start
+ 1);
878 dev_err(&pdev
->dev
, "unable to ioremap mii_ctrl\n");
883 dev
->irq
= platform_get_irq(pdev
, 0);
884 err
= request_irq(dev
->irq
, ag71xx_interrupt
,
885 IRQF_DISABLED
| IRQF_SAMPLE_RANDOM
,
888 dev_err(&pdev
->dev
, "unable to request IRQ %d\n", dev
->irq
);
889 goto err_unmap_mii_ctrl
;
892 dev
->base_addr
= (unsigned long)ag
->mac_base
;
893 dev
->netdev_ops
= &ag71xx_netdev_ops
;
894 dev
->ethtool_ops
= &ag71xx_ethtool_ops
;
896 INIT_WORK(&ag
->restart_work
, ag71xx_restart_work_func
);
898 init_timer(&ag
->oom_timer
);
899 ag
->oom_timer
.data
= (unsigned long) dev
;
900 ag
->oom_timer
.function
= ag71xx_oom_timer_handler
;
902 memcpy(dev
->dev_addr
, pdata
->mac_addr
, ETH_ALEN
);
904 netif_napi_add(dev
, &ag
->napi
, ag71xx_poll
, AG71XX_NAPI_WEIGHT
);
906 err
= register_netdev(dev
);
908 dev_err(&pdev
->dev
, "unable to register net device\n");
912 printk(KERN_INFO
"%s: Atheros AG71xx at 0x%08lx, irq %d\n",
913 dev
->name
, dev
->base_addr
, dev
->irq
);
915 ag71xx_dump_regs(ag
);
919 ag71xx_dump_regs(ag
);
921 err
= ag71xx_phy_connect(ag
);
923 goto err_unregister_netdev
;
925 platform_set_drvdata(pdev
, dev
);
929 err_unregister_netdev
:
930 unregister_netdev(dev
);
932 free_irq(dev
->irq
, dev
);
934 iounmap(ag
->mii_ctrl
);
936 iounmap(ag
->mac_base
);
940 platform_set_drvdata(pdev
, NULL
);
944 static int __exit
ag71xx_remove(struct platform_device
*pdev
)
946 struct net_device
*dev
= platform_get_drvdata(pdev
);
949 struct ag71xx
*ag
= netdev_priv(dev
);
951 ag71xx_phy_disconnect(ag
);
952 unregister_netdev(dev
);
953 free_irq(dev
->irq
, dev
);
954 iounmap(ag
->mii_ctrl
);
955 iounmap(ag
->mac_base
);
957 platform_set_drvdata(pdev
, NULL
);
963 static struct platform_driver ag71xx_driver
= {
964 .probe
= ag71xx_probe
,
965 .remove
= __exit_p(ag71xx_remove
),
967 .name
= AG71XX_DRV_NAME
,
971 static int __init
ag71xx_module_init(void)
975 ret
= ag71xx_mdio_driver_init();
979 ret
= platform_driver_register(&ag71xx_driver
);
986 ag71xx_mdio_driver_exit();
991 static void __exit
ag71xx_module_exit(void)
993 platform_driver_unregister(&ag71xx_driver
);
994 ag71xx_mdio_driver_exit();
997 module_init(ag71xx_module_init
);
998 module_exit(ag71xx_module_exit
);
1000 MODULE_VERSION(AG71XX_DRV_VERSION
);
1001 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
1002 MODULE_AUTHOR("Imre Kaloz <kaloz@openwrt.org>");
1003 MODULE_LICENSE("GPL v2");
1004 MODULE_ALIAS("platform:" AG71XX_DRV_NAME
);