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.
16 #define AG71XX_DEFAULT_MSG_ENABLE \
26 static int ag71xx_debug
= -1;
28 module_param(ag71xx_debug
, int, 0);
29 MODULE_PARM_DESC(ag71xx_debug
, "Debug level (-1=defaults,0=none,...,16=all)");
31 static void ag71xx_dump_dma_regs(struct ag71xx
*ag
)
33 DBG("%s: dma_tx_ctrl=%08x, dma_tx_desc=%08x, dma_tx_status=%08x\n",
35 ag71xx_rr(ag
, AG71XX_REG_TX_CTRL
),
36 ag71xx_rr(ag
, AG71XX_REG_TX_DESC
),
37 ag71xx_rr(ag
, AG71XX_REG_TX_STATUS
));
39 DBG("%s: dma_rx_ctrl=%08x, dma_rx_desc=%08x, dma_rx_status=%08x\n",
41 ag71xx_rr(ag
, AG71XX_REG_RX_CTRL
),
42 ag71xx_rr(ag
, AG71XX_REG_RX_DESC
),
43 ag71xx_rr(ag
, AG71XX_REG_RX_STATUS
));
46 static void ag71xx_dump_regs(struct ag71xx
*ag
)
48 DBG("%s: mac_cfg1=%08x, mac_cfg2=%08x, ipg=%08x, hdx=%08x, mfl=%08x\n",
50 ag71xx_rr(ag
, AG71XX_REG_MAC_CFG1
),
51 ag71xx_rr(ag
, AG71XX_REG_MAC_CFG2
),
52 ag71xx_rr(ag
, AG71XX_REG_MAC_IPG
),
53 ag71xx_rr(ag
, AG71XX_REG_MAC_HDX
),
54 ag71xx_rr(ag
, AG71XX_REG_MAC_MFL
));
55 DBG("%s: mac_ifctl=%08x, mac_addr1=%08x, mac_addr2=%08x\n",
57 ag71xx_rr(ag
, AG71XX_REG_MAC_IFCTL
),
58 ag71xx_rr(ag
, AG71XX_REG_MAC_ADDR1
),
59 ag71xx_rr(ag
, AG71XX_REG_MAC_ADDR2
));
60 DBG("%s: fifo_cfg0=%08x, fifo_cfg1=%08x, fifo_cfg2=%08x\n",
62 ag71xx_rr(ag
, AG71XX_REG_FIFO_CFG0
),
63 ag71xx_rr(ag
, AG71XX_REG_FIFO_CFG1
),
64 ag71xx_rr(ag
, AG71XX_REG_FIFO_CFG2
));
65 DBG("%s: fifo_cfg3=%08x, fifo_cfg4=%08x, fifo_cfg5=%08x\n",
67 ag71xx_rr(ag
, AG71XX_REG_FIFO_CFG3
),
68 ag71xx_rr(ag
, AG71XX_REG_FIFO_CFG4
),
69 ag71xx_rr(ag
, AG71XX_REG_FIFO_CFG5
));
72 static inline void ag71xx_dump_intr(struct ag71xx
*ag
, char *label
, u32 intr
)
74 DBG("%s: %s intr=%08x %s%s%s%s%s%s\n",
75 ag
->dev
->name
, label
, intr
,
76 (intr
& AG71XX_INT_TX_PS
) ? "TXPS " : "",
77 (intr
& AG71XX_INT_TX_UR
) ? "TXUR " : "",
78 (intr
& AG71XX_INT_TX_BE
) ? "TXBE " : "",
79 (intr
& AG71XX_INT_RX_PR
) ? "RXPR " : "",
80 (intr
& AG71XX_INT_RX_OF
) ? "RXOF " : "",
81 (intr
& AG71XX_INT_RX_BE
) ? "RXBE " : "");
84 static void ag71xx_ring_free(struct ag71xx_ring
*ring
)
89 dma_free_coherent(NULL
, ring
->size
* sizeof(struct ag71xx_desc
),
90 ring
->descs_cpu
, ring
->descs_dma
);
93 static int ag71xx_ring_alloc(struct ag71xx_ring
*ring
, unsigned int size
)
98 ring
->descs_cpu
= dma_alloc_coherent(NULL
,
99 size
* sizeof(struct ag71xx_desc
),
102 if (!ring
->descs_cpu
) {
109 ring
->buf
= kzalloc(size
* sizeof(*ring
->buf
), GFP_KERNEL
);
115 for (i
= 0; i
< size
; i
++) {
116 struct ag71xx_desc
*descs
= (struct ag71xx_desc
*) ring
->descs_cpu
;
117 ring
->buf
[i
].desc
= &descs
[i
];
126 static void ag71xx_ring_tx_clean(struct ag71xx
*ag
)
128 struct ag71xx_ring
*ring
= &ag
->tx_ring
;
129 struct net_device
*dev
= ag
->dev
;
131 while (ring
->curr
!= ring
->dirty
) {
132 u32 i
= ring
->dirty
% AG71XX_TX_RING_SIZE
;
134 if (!ag71xx_desc_empty(ring
->buf
[i
].desc
)) {
135 ring
->buf
[i
].desc
->ctrl
= 0;
136 dev
->stats
.tx_errors
++;
139 if (ring
->buf
[i
].skb
)
140 dev_kfree_skb_any(ring
->buf
[i
].skb
);
142 ring
->buf
[i
].skb
= NULL
;
147 /* flush descriptors */
152 static void ag71xx_ring_tx_init(struct ag71xx
*ag
)
154 struct ag71xx_ring
*ring
= &ag
->tx_ring
;
157 for (i
= 0; i
< AG71XX_TX_RING_SIZE
; i
++) {
158 ring
->buf
[i
].desc
->next
= (u32
) (ring
->descs_dma
+
159 sizeof(struct ag71xx_desc
) * ((i
+ 1) % AG71XX_TX_RING_SIZE
));
161 ring
->buf
[i
].desc
->ctrl
= DESC_EMPTY
;
162 ring
->buf
[i
].skb
= NULL
;
165 /* flush descriptors */
172 static void ag71xx_ring_rx_clean(struct ag71xx
*ag
)
174 struct ag71xx_ring
*ring
= &ag
->rx_ring
;
180 for (i
= 0; i
< AG71XX_RX_RING_SIZE
; i
++)
181 if (ring
->buf
[i
].skb
)
182 kfree_skb(ring
->buf
[i
].skb
);
186 static int ag71xx_ring_rx_init(struct ag71xx
*ag
)
188 struct ag71xx_ring
*ring
= &ag
->rx_ring
;
193 for (i
= 0; i
< AG71XX_RX_RING_SIZE
; i
++)
194 ring
->buf
[i
].desc
->next
= (u32
) (ring
->descs_dma
+
195 sizeof(struct ag71xx_desc
) * ((i
+ 1) % AG71XX_RX_RING_SIZE
));
197 for (i
= 0; i
< AG71XX_RX_RING_SIZE
; i
++) {
200 skb
= dev_alloc_skb(AG71XX_RX_PKT_SIZE
);
206 dma_map_single(NULL
, skb
->data
, AG71XX_RX_PKT_SIZE
,
210 skb_reserve(skb
, AG71XX_RX_PKT_RESERVE
);
212 ring
->buf
[i
].skb
= skb
;
213 ring
->buf
[i
].desc
->data
= virt_to_phys(skb
->data
);
214 ring
->buf
[i
].desc
->ctrl
= DESC_EMPTY
;
217 /* flush descriptors */
226 static int ag71xx_ring_rx_refill(struct ag71xx
*ag
)
228 struct ag71xx_ring
*ring
= &ag
->rx_ring
;
232 for (; ring
->curr
- ring
->dirty
> 0; ring
->dirty
++) {
235 i
= ring
->dirty
% AG71XX_RX_RING_SIZE
;
237 if (ring
->buf
[i
].skb
== NULL
) {
240 skb
= dev_alloc_skb(AG71XX_RX_PKT_SIZE
);
244 dma_map_single(NULL
, skb
->data
, AG71XX_RX_PKT_SIZE
,
247 skb_reserve(skb
, AG71XX_RX_PKT_RESERVE
);
250 ring
->buf
[i
].skb
= skb
;
251 ring
->buf
[i
].desc
->data
= virt_to_phys(skb
->data
);
254 ring
->buf
[i
].desc
->ctrl
= DESC_EMPTY
;
258 /* flush descriptors */
261 DBG("%s: %u rx descriptors refilled\n", ag
->dev
->name
, count
);
266 static int ag71xx_rings_init(struct ag71xx
*ag
)
270 ret
= ag71xx_ring_alloc(&ag
->tx_ring
, AG71XX_TX_RING_SIZE
);
274 ag71xx_ring_tx_init(ag
);
276 ret
= ag71xx_ring_alloc(&ag
->rx_ring
, AG71XX_RX_RING_SIZE
);
280 ret
= ag71xx_ring_rx_init(ag
);
284 static void ag71xx_rings_cleanup(struct ag71xx
*ag
)
286 ag71xx_ring_rx_clean(ag
);
287 ag71xx_ring_free(&ag
->rx_ring
);
289 ag71xx_ring_tx_clean(ag
);
290 ag71xx_ring_free(&ag
->tx_ring
);
293 static void ag71xx_hw_set_macaddr(struct ag71xx
*ag
, unsigned char *mac
)
297 t
= (((u32
) mac
[0]) << 24) | (((u32
) mac
[1]) << 16)
298 | (((u32
) mac
[2]) << 8) | ((u32
) mac
[3]);
300 ag71xx_wr(ag
, AG71XX_REG_MAC_ADDR1
, t
);
302 t
= (((u32
) mac
[4]) << 24) | (((u32
) mac
[5]) << 16);
303 ag71xx_wr(ag
, AG71XX_REG_MAC_ADDR2
, t
);
306 static void ag71xx_dma_reset(struct ag71xx
*ag
)
310 ag71xx_dump_dma_regs(ag
);
313 ag71xx_wr(ag
, AG71XX_REG_RX_CTRL
, 0);
314 ag71xx_wr(ag
, AG71XX_REG_TX_CTRL
, 0);
316 /* clear descriptor addresses */
317 ag71xx_wr(ag
, AG71XX_REG_TX_DESC
, 0);
318 ag71xx_wr(ag
, AG71XX_REG_RX_DESC
, 0);
320 /* clear pending RX/TX interrupts */
321 for (i
= 0; i
< 256; i
++) {
322 ag71xx_wr(ag
, AG71XX_REG_RX_STATUS
, RX_STATUS_PR
);
323 ag71xx_wr(ag
, AG71XX_REG_TX_STATUS
, TX_STATUS_PS
);
326 /* clear pending errors */
327 ag71xx_wr(ag
, AG71XX_REG_RX_STATUS
, RX_STATUS_BE
| RX_STATUS_OF
);
328 ag71xx_wr(ag
, AG71XX_REG_TX_STATUS
, TX_STATUS_BE
| TX_STATUS_UR
);
330 if (ag71xx_rr(ag
, AG71XX_REG_RX_STATUS
))
331 printk(KERN_ALERT
"%s: unable to clear DMA Rx status\n",
334 if (ag71xx_rr(ag
, AG71XX_REG_TX_STATUS
))
335 printk(KERN_ALERT
"%s: unable to clear DMA Tx status\n",
338 ag71xx_dump_dma_regs(ag
);
341 #define MAC_CFG1_INIT (MAC_CFG1_RXE | MAC_CFG1_TXE | \
342 MAC_CFG1_SRX | MAC_CFG1_STX)
344 #define FIFO_CFG0_INIT (FIFO_CFG0_ALL << FIFO_CFG0_ENABLE_SHIFT)
346 #define FIFO_CFG4_INIT (FIFO_CFG4_DE | FIFO_CFG4_DV | FIFO_CFG4_FC | \
347 FIFO_CFG4_CE | FIFO_CFG4_CR | FIFO_CFG4_LM | \
348 FIFO_CFG4_LO | FIFO_CFG4_OK | FIFO_CFG4_MC | \
349 FIFO_CFG4_BC | FIFO_CFG4_DR | FIFO_CFG4_LE | \
350 FIFO_CFG4_CF | FIFO_CFG4_PF | FIFO_CFG4_UO | \
353 #define FIFO_CFG5_INIT (FIFO_CFG5_DE | FIFO_CFG5_DV | FIFO_CFG5_FC | \
354 FIFO_CFG5_CE | FIFO_CFG5_LO | FIFO_CFG5_OK | \
355 FIFO_CFG5_MC | FIFO_CFG5_BC | FIFO_CFG5_DR | \
356 FIFO_CFG5_CF | FIFO_CFG5_PF | FIFO_CFG5_VT | \
357 FIFO_CFG5_LE | FIFO_CFG5_FT | FIFO_CFG5_16 | \
358 FIFO_CFG5_17 | FIFO_CFG5_SF)
360 static void ag71xx_hw_init(struct ag71xx
*ag
)
362 struct ag71xx_platform_data
*pdata
= ag71xx_get_pdata(ag
);
364 ag71xx_sb(ag
, AG71XX_REG_MAC_CFG1
, MAC_CFG1_SR
);
367 ar71xx_device_stop(pdata
->reset_bit
);
369 ar71xx_device_start(pdata
->reset_bit
);
372 /* setup MAC configuration registers */
373 ag71xx_wr(ag
, AG71XX_REG_MAC_CFG1
, MAC_CFG1_INIT
);
374 ag71xx_sb(ag
, AG71XX_REG_MAC_CFG2
,
375 MAC_CFG2_PAD_CRC_EN
| MAC_CFG2_LEN_CHECK
);
377 /* setup max frame length */
378 ag71xx_wr(ag
, AG71XX_REG_MAC_MFL
, AG71XX_TX_MTU_LEN
);
380 /* setup MII interface type */
381 ag71xx_mii_ctrl_set_if(ag
, pdata
->mii_if
);
383 /* setup FIFO configuration registers */
384 ag71xx_wr(ag
, AG71XX_REG_FIFO_CFG0
, FIFO_CFG0_INIT
);
385 ag71xx_wr(ag
, AG71XX_REG_FIFO_CFG1
, 0x0fff0000);
386 ag71xx_wr(ag
, AG71XX_REG_FIFO_CFG2
, 0x00001fff);
387 ag71xx_wr(ag
, AG71XX_REG_FIFO_CFG4
, FIFO_CFG4_INIT
);
388 ag71xx_wr(ag
, AG71XX_REG_FIFO_CFG5
, FIFO_CFG5_INIT
);
390 ag71xx_dma_reset(ag
);
393 static void ag71xx_hw_start(struct ag71xx
*ag
)
395 /* start RX engine */
396 ag71xx_wr(ag
, AG71XX_REG_RX_CTRL
, RX_CTRL_RXE
);
398 /* enable interrupts */
399 ag71xx_wr(ag
, AG71XX_REG_INT_ENABLE
, AG71XX_INT_INIT
);
402 static void ag71xx_hw_stop(struct ag71xx
*ag
)
404 /* disable all interrupts */
405 ag71xx_wr(ag
, AG71XX_REG_INT_ENABLE
, 0);
407 ag71xx_dma_reset(ag
);
410 static int ag71xx_open(struct net_device
*dev
)
412 struct ag71xx
*ag
= netdev_priv(dev
);
415 ret
= ag71xx_rings_init(ag
);
419 napi_enable(&ag
->napi
);
421 netif_carrier_off(dev
);
422 ag71xx_phy_start(ag
);
424 ag71xx_wr(ag
, AG71XX_REG_TX_DESC
, ag
->tx_ring
.descs_dma
);
425 ag71xx_wr(ag
, AG71XX_REG_RX_DESC
, ag
->rx_ring
.descs_dma
);
427 ag71xx_hw_set_macaddr(ag
, dev
->dev_addr
);
431 netif_start_queue(dev
);
436 ag71xx_rings_cleanup(ag
);
440 static int ag71xx_stop(struct net_device
*dev
)
442 struct ag71xx
*ag
= netdev_priv(dev
);
445 spin_lock_irqsave(&ag
->lock
, flags
);
447 netif_stop_queue(dev
);
451 netif_carrier_off(dev
);
454 napi_disable(&ag
->napi
);
455 del_timer_sync(&ag
->oom_timer
);
457 spin_unlock_irqrestore(&ag
->lock
, flags
);
459 ag71xx_rings_cleanup(ag
);
464 static int ag71xx_hard_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
466 struct ag71xx
*ag
= netdev_priv(dev
);
467 struct ag71xx_ring
*ring
= &ag
->tx_ring
;
468 struct ag71xx_desc
*desc
;
471 i
= ring
->curr
% AG71XX_TX_RING_SIZE
;
472 desc
= ring
->buf
[i
].desc
;
474 if (!ag71xx_desc_empty(desc
))
477 ag71xx_add_ar8216_header(ag
, skb
);
480 DBG("%s: packet len is too small\n", ag
->dev
->name
);
484 dma_map_single(NULL
, skb
->data
, skb
->len
, DMA_TO_DEVICE
);
486 ring
->buf
[i
].skb
= skb
;
488 /* setup descriptor fields */
489 desc
->data
= virt_to_phys(skb
->data
);
490 desc
->ctrl
= (skb
->len
& DESC_PKTLEN_M
);
492 /* flush descriptor */
496 if (ring
->curr
== (ring
->dirty
+ AG71XX_TX_THRES_STOP
)) {
497 DBG("%s: tx queue full\n", ag
->dev
->name
);
498 netif_stop_queue(dev
);
501 DBG("%s: packet injected into TX queue\n", ag
->dev
->name
);
503 /* enable TX engine */
504 ag71xx_wr(ag
, AG71XX_REG_TX_CTRL
, TX_CTRL_TXE
);
506 dev
->trans_start
= jiffies
;
511 dev
->stats
.tx_dropped
++;
517 static int ag71xx_do_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
)
519 struct mii_ioctl_data
*data
= (struct mii_ioctl_data
*) &ifr
->ifr_data
;
520 struct ag71xx
*ag
= netdev_priv(dev
);
525 if (ag
->phy_dev
== NULL
)
528 spin_lock_irq(&ag
->lock
);
529 ret
= phy_ethtool_ioctl(ag
->phy_dev
, (void *) ifr
->ifr_data
);
530 spin_unlock_irq(&ag
->lock
);
535 (dev
->dev_addr
, ifr
->ifr_data
, sizeof(dev
->dev_addr
)))
541 (ifr
->ifr_data
, dev
->dev_addr
, sizeof(dev
->dev_addr
)))
548 if (ag
->phy_dev
== NULL
)
551 return phy_mii_ioctl(ag
->phy_dev
, data
, cmd
);
560 static void ag71xx_oom_timer_handler(unsigned long data
)
562 struct net_device
*dev
= (struct net_device
*) data
;
563 struct ag71xx
*ag
= netdev_priv(dev
);
565 netif_rx_schedule(dev
, &ag
->napi
);
568 static void ag71xx_tx_timeout(struct net_device
*dev
)
570 struct ag71xx
*ag
= netdev_priv(dev
);
572 if (netif_msg_tx_err(ag
))
573 printk(KERN_DEBUG
"%s: tx timeout\n", ag
->dev
->name
);
575 schedule_work(&ag
->restart_work
);
578 static void ag71xx_restart_work_func(struct work_struct
*work
)
580 struct ag71xx
*ag
= container_of(work
, struct ag71xx
, restart_work
);
582 ag71xx_stop(ag
->dev
);
583 ag71xx_open(ag
->dev
);
586 static void ag71xx_tx_packets(struct ag71xx
*ag
)
588 struct ag71xx_ring
*ring
= &ag
->tx_ring
;
591 DBG("%s: processing TX ring\n", ag
->dev
->name
);
594 while (ring
->dirty
!= ring
->curr
) {
595 unsigned int i
= ring
->dirty
% AG71XX_TX_RING_SIZE
;
596 struct ag71xx_desc
*desc
= ring
->buf
[i
].desc
;
597 struct sk_buff
*skb
= ring
->buf
[i
].skb
;
599 if (!ag71xx_desc_empty(desc
))
602 ag71xx_wr(ag
, AG71XX_REG_TX_STATUS
, TX_STATUS_PS
);
604 ag
->dev
->stats
.tx_bytes
+= skb
->len
;
605 ag
->dev
->stats
.tx_packets
++;
607 dev_kfree_skb_any(skb
);
608 ring
->buf
[i
].skb
= NULL
;
614 DBG("%s: %d packets sent out\n", ag
->dev
->name
, sent
);
616 if ((ring
->curr
- ring
->dirty
) < AG71XX_TX_THRES_WAKEUP
)
617 netif_wake_queue(ag
->dev
);
621 static int ag71xx_rx_packets(struct ag71xx
*ag
, int limit
)
623 struct net_device
*dev
= ag
->dev
;
624 struct ag71xx_ring
*ring
= &ag
->rx_ring
;
627 DBG("%s: rx packets, limit=%d, curr=%u, dirty=%u\n",
628 dev
->name
, limit
, ring
->curr
, ring
->dirty
);
630 while (done
< limit
) {
631 unsigned int i
= ring
->curr
% AG71XX_RX_RING_SIZE
;
632 struct ag71xx_desc
*desc
= ring
->buf
[i
].desc
;
636 if (ag71xx_desc_empty(desc
))
639 if ((ring
->dirty
+ AG71XX_RX_RING_SIZE
) == ring
->curr
) {
644 ag71xx_wr(ag
, AG71XX_REG_RX_STATUS
, RX_STATUS_PR
);
646 skb
= ring
->buf
[i
].skb
;
647 pktlen
= ag71xx_desc_pktlen(desc
);
648 pktlen
-= ETH_FCS_LEN
;
650 skb_put(skb
, pktlen
);
653 skb
->ip_summed
= CHECKSUM_NONE
;
655 dev
->last_rx
= jiffies
;
656 dev
->stats
.rx_packets
++;
657 dev
->stats
.rx_bytes
+= pktlen
;
659 if (ag71xx_remove_ar8216_header(ag
, skb
) != 0) {
660 dev
->stats
.rx_dropped
++;
663 skb
->protocol
= eth_type_trans(skb
, dev
);
664 netif_receive_skb(skb
);
667 ring
->buf
[i
].skb
= NULL
;
673 ag71xx_ring_rx_refill(ag
);
675 DBG("%s: rx finish, curr=%u, dirty=%u, done=%d\n",
676 dev
->name
, ring
->curr
, ring
->dirty
, done
);
681 static int ag71xx_poll(struct napi_struct
*napi
, int limit
)
683 struct ag71xx
*ag
= container_of(napi
, struct ag71xx
, napi
);
684 struct ag71xx_platform_data
*pdata
= ag71xx_get_pdata(ag
);
685 struct net_device
*dev
= ag
->dev
;
686 struct ag71xx_ring
*rx_ring
;
692 ag71xx_tx_packets(ag
);
694 DBG("%s: processing RX ring\n", dev
->name
);
695 done
= ag71xx_rx_packets(ag
, limit
);
697 rx_ring
= &ag
->rx_ring
;
698 if (rx_ring
->buf
[rx_ring
->dirty
% AG71XX_RX_RING_SIZE
].skb
== NULL
)
701 status
= ag71xx_rr(ag
, AG71XX_REG_RX_STATUS
);
702 if (unlikely(status
& RX_STATUS_OF
)) {
703 ag71xx_wr(ag
, AG71XX_REG_RX_STATUS
, RX_STATUS_OF
);
704 dev
->stats
.rx_fifo_errors
++;
707 ag71xx_wr(ag
, AG71XX_REG_RX_CTRL
, RX_CTRL_RXE
);
711 if (status
& RX_STATUS_PR
)
714 status
= ag71xx_rr(ag
, AG71XX_REG_TX_STATUS
);
715 if (status
& TX_STATUS_PS
)
718 DBG("%s: disable polling mode, done=%d, limit=%d\n",
719 dev
->name
, done
, limit
);
721 netif_rx_complete(dev
, napi
);
723 /* enable interrupts */
724 spin_lock_irqsave(&ag
->lock
, flags
);
725 ag71xx_int_enable(ag
, AG71XX_INT_POLL
);
726 spin_unlock_irqrestore(&ag
->lock
, flags
);
731 DBG("%s: stay in polling mode, done=%d, limit=%d\n",
732 dev
->name
, done
, limit
);
736 if (netif_msg_rx_err(ag
))
737 printk(KERN_DEBUG
"%s: out of memory\n", dev
->name
);
739 mod_timer(&ag
->oom_timer
, jiffies
+ AG71XX_OOM_REFILL
);
740 netif_rx_complete(dev
, napi
);
744 static irqreturn_t
ag71xx_interrupt(int irq
, void *dev_id
)
746 struct net_device
*dev
= dev_id
;
747 struct ag71xx
*ag
= netdev_priv(dev
);
750 status
= ag71xx_rr(ag
, AG71XX_REG_INT_STATUS
);
751 ag71xx_dump_intr(ag
, "raw", status
);
753 if (unlikely(!status
))
756 if (unlikely(status
& AG71XX_INT_ERR
)) {
757 if (status
& AG71XX_INT_TX_BE
) {
758 ag71xx_wr(ag
, AG71XX_REG_TX_STATUS
, TX_STATUS_BE
);
759 dev_err(&dev
->dev
, "TX BUS error\n");
761 if (status
& AG71XX_INT_RX_BE
) {
762 ag71xx_wr(ag
, AG71XX_REG_RX_STATUS
, RX_STATUS_BE
);
763 dev_err(&dev
->dev
, "RX BUS error\n");
767 if (likely(status
& AG71XX_INT_POLL
)) {
768 ag71xx_int_disable(ag
, AG71XX_INT_POLL
);
769 DBG("%s: enable polling mode\n", dev
->name
);
770 netif_rx_schedule(dev
, &ag
->napi
);
776 static void ag71xx_set_multicast_list(struct net_device
*dev
)
781 static int __init
ag71xx_probe(struct platform_device
*pdev
)
783 struct net_device
*dev
;
784 struct resource
*res
;
786 struct ag71xx_platform_data
*pdata
;
789 pdata
= pdev
->dev
.platform_data
;
791 dev_err(&pdev
->dev
, "no platform data specified\n");
796 dev
= alloc_etherdev(sizeof(*ag
));
798 dev_err(&pdev
->dev
, "alloc_etherdev failed\n");
803 SET_NETDEV_DEV(dev
, &pdev
->dev
);
805 ag
= netdev_priv(dev
);
808 ag
->mii_bus
= ag71xx_mdio_bus
->mii_bus
;
809 ag
->msg_enable
= netif_msg_init(ag71xx_debug
,
810 AG71XX_DEFAULT_MSG_ENABLE
);
811 spin_lock_init(&ag
->lock
);
813 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "mac_base");
815 dev_err(&pdev
->dev
, "no mac_base resource found\n");
820 ag
->mac_base
= ioremap_nocache(res
->start
, res
->end
- res
->start
+ 1);
822 dev_err(&pdev
->dev
, "unable to ioremap mac_base\n");
827 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "mac_base2");
829 dev_err(&pdev
->dev
, "no mac_base2 resource found\n");
831 goto err_unmap_base1
;
834 ag
->mac_base2
= ioremap_nocache(res
->start
, res
->end
- res
->start
+ 1);
836 dev_err(&pdev
->dev
, "unable to ioremap mac_base2\n");
838 goto err_unmap_base1
;
841 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "mii_ctrl");
843 dev_err(&pdev
->dev
, "no mii_ctrl resource found\n");
845 goto err_unmap_base2
;
848 ag
->mii_ctrl
= ioremap_nocache(res
->start
, res
->end
- res
->start
+ 1);
850 dev_err(&pdev
->dev
, "unable to ioremap mii_ctrl\n");
852 goto err_unmap_base2
;
855 dev
->irq
= platform_get_irq(pdev
, 0);
856 err
= request_irq(dev
->irq
, ag71xx_interrupt
,
857 IRQF_DISABLED
| IRQF_SAMPLE_RANDOM
,
860 dev_err(&pdev
->dev
, "unable to request IRQ %d\n", dev
->irq
);
861 goto err_unmap_mii_ctrl
;
864 dev
->base_addr
= (unsigned long)ag
->mac_base
;
865 dev
->open
= ag71xx_open
;
866 dev
->stop
= ag71xx_stop
;
867 dev
->hard_start_xmit
= ag71xx_hard_start_xmit
;
868 dev
->set_multicast_list
= ag71xx_set_multicast_list
;
869 dev
->do_ioctl
= ag71xx_do_ioctl
;
870 dev
->ethtool_ops
= &ag71xx_ethtool_ops
;
872 dev
->tx_timeout
= ag71xx_tx_timeout
;
873 INIT_WORK(&ag
->restart_work
, ag71xx_restart_work_func
);
875 init_timer(&ag
->oom_timer
);
876 ag
->oom_timer
.data
= (unsigned long) dev
;
877 ag
->oom_timer
.function
= ag71xx_oom_timer_handler
;
879 memcpy(dev
->dev_addr
, pdata
->mac_addr
, ETH_ALEN
);
881 netif_napi_add(dev
, &ag
->napi
, ag71xx_poll
, AG71XX_NAPI_WEIGHT
);
883 err
= register_netdev(dev
);
885 dev_err(&pdev
->dev
, "unable to register net device\n");
889 printk(KERN_INFO
"%s: Atheros AG71xx at 0x%08lx, irq %d\n",
890 dev
->name
, dev
->base_addr
, dev
->irq
);
892 ag71xx_dump_regs(ag
);
896 ag71xx_dump_regs(ag
);
898 /* Reset the mdio bus explicitly */
900 mutex_lock(&ag
->mii_bus
->mdio_lock
);
901 ag
->mii_bus
->reset(ag
->mii_bus
);
902 mutex_unlock(&ag
->mii_bus
->mdio_lock
);
905 err
= ag71xx_phy_connect(ag
);
907 goto err_unregister_netdev
;
909 platform_set_drvdata(pdev
, dev
);
913 err_unregister_netdev
:
914 unregister_netdev(dev
);
916 free_irq(dev
->irq
, dev
);
918 iounmap(ag
->mii_ctrl
);
920 iounmap(ag
->mac_base2
);
922 iounmap(ag
->mac_base
);
926 platform_set_drvdata(pdev
, NULL
);
930 static int __exit
ag71xx_remove(struct platform_device
*pdev
)
932 struct net_device
*dev
= platform_get_drvdata(pdev
);
935 struct ag71xx
*ag
= netdev_priv(dev
);
937 ag71xx_phy_disconnect(ag
);
938 unregister_netdev(dev
);
939 free_irq(dev
->irq
, dev
);
940 iounmap(ag
->mii_ctrl
);
941 iounmap(ag
->mac_base2
);
942 iounmap(ag
->mac_base
);
944 platform_set_drvdata(pdev
, NULL
);
950 static struct platform_driver ag71xx_driver
= {
951 .probe
= ag71xx_probe
,
952 .remove
= __exit_p(ag71xx_remove
),
954 .name
= AG71XX_DRV_NAME
,
958 static int __init
ag71xx_module_init(void)
962 ret
= ag71xx_mdio_driver_init();
966 ret
= platform_driver_register(&ag71xx_driver
);
973 ag71xx_mdio_driver_exit();
978 static void __exit
ag71xx_module_exit(void)
980 platform_driver_unregister(&ag71xx_driver
);
981 ag71xx_mdio_driver_exit();
984 module_init(ag71xx_module_init
);
985 module_exit(ag71xx_module_exit
);
987 MODULE_VERSION(AG71XX_DRV_VERSION
);
988 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
989 MODULE_AUTHOR("Imre Kaloz <kaloz@openwrt.org>");
990 MODULE_LICENSE("GPL v2");
991 MODULE_ALIAS("platform:" AG71XX_DRV_NAME
);