2 * Atheros AR71xx built-in ethernet mac driver
4 * Copyright (C) 2008-2010 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_msg_level
= -1;
28 module_param_named(msg_level
, ag71xx_msg_level
, int, 0);
29 MODULE_PARM_DESC(msg_level
, "Message 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
* ring
->desc_size
,
90 ring
->descs_cpu
, ring
->descs_dma
);
93 static int ag71xx_ring_alloc(struct ag71xx_ring
*ring
)
98 ring
->desc_size
= sizeof(struct ag71xx_desc
);
99 if (ring
->desc_size
% cache_line_size()) {
100 DBG("ag71xx: ring %p, desc size %u rounded to %u\n",
101 ring
, ring
->desc_size
,
102 roundup(ring
->desc_size
, cache_line_size()));
103 ring
->desc_size
= roundup(ring
->desc_size
, cache_line_size());
106 ring
->descs_cpu
= dma_alloc_coherent(NULL
, ring
->size
* ring
->desc_size
,
107 &ring
->descs_dma
, GFP_ATOMIC
);
108 if (!ring
->descs_cpu
) {
114 ring
->buf
= kzalloc(ring
->size
* sizeof(*ring
->buf
), GFP_KERNEL
);
120 for (i
= 0; i
< ring
->size
; i
++) {
121 int idx
= i
* ring
->desc_size
;
122 ring
->buf
[i
].desc
= (struct ag71xx_desc
*)&ring
->descs_cpu
[idx
];
123 DBG("ag71xx: ring %p, desc %d at %p\n",
124 ring
, i
, ring
->buf
[i
].desc
);
133 static void ag71xx_ring_tx_clean(struct ag71xx
*ag
)
135 struct ag71xx_ring
*ring
= &ag
->tx_ring
;
136 struct net_device
*dev
= ag
->dev
;
138 while (ring
->curr
!= ring
->dirty
) {
139 u32 i
= ring
->dirty
% ring
->size
;
141 if (!ag71xx_desc_empty(ring
->buf
[i
].desc
)) {
142 ring
->buf
[i
].desc
->ctrl
= 0;
143 dev
->stats
.tx_errors
++;
146 if (ring
->buf
[i
].skb
)
147 dev_kfree_skb_any(ring
->buf
[i
].skb
);
149 ring
->buf
[i
].skb
= NULL
;
154 /* flush descriptors */
159 static void ag71xx_ring_tx_init(struct ag71xx
*ag
)
161 struct ag71xx_ring
*ring
= &ag
->tx_ring
;
164 for (i
= 0; i
< ring
->size
; i
++) {
165 ring
->buf
[i
].desc
->next
= (u32
) (ring
->descs_dma
+
166 ring
->desc_size
* ((i
+ 1) % ring
->size
));
168 ring
->buf
[i
].desc
->ctrl
= DESC_EMPTY
;
169 ring
->buf
[i
].skb
= NULL
;
172 /* flush descriptors */
179 static void ag71xx_ring_rx_clean(struct ag71xx
*ag
)
181 struct ag71xx_ring
*ring
= &ag
->rx_ring
;
187 for (i
= 0; i
< ring
->size
; i
++)
188 if (ring
->buf
[i
].skb
) {
189 dma_unmap_single(&ag
->dev
->dev
, ring
->buf
[i
].dma_addr
,
190 AG71XX_RX_PKT_SIZE
, DMA_FROM_DEVICE
);
191 kfree_skb(ring
->buf
[i
].skb
);
195 static int ag71xx_rx_reserve(struct ag71xx
*ag
)
199 if (ag71xx_get_pdata(ag
)->is_ar724x
) {
200 if (!ag71xx_has_ar8216(ag
))
204 reserve
+= 4 - (ag
->phy_dev
->pkt_align
% 4);
209 return reserve
+ AG71XX_RX_PKT_RESERVE
;
213 static int ag71xx_ring_rx_init(struct ag71xx
*ag
)
215 struct ag71xx_ring
*ring
= &ag
->rx_ring
;
216 unsigned int reserve
= ag71xx_rx_reserve(ag
);
221 for (i
= 0; i
< ring
->size
; i
++) {
222 ring
->buf
[i
].desc
->next
= (u32
) (ring
->descs_dma
+
223 ring
->desc_size
* ((i
+ 1) % ring
->size
));
225 DBG("ag71xx: RX desc at %p, next is %08x\n",
227 ring
->buf
[i
].desc
->next
);
230 for (i
= 0; i
< ring
->size
; i
++) {
234 skb
= dev_alloc_skb(AG71XX_RX_PKT_SIZE
+ reserve
);
241 skb_reserve(skb
, reserve
);
243 dma_addr
= dma_map_single(&ag
->dev
->dev
, skb
->data
,
246 ring
->buf
[i
].skb
= skb
;
247 ring
->buf
[i
].dma_addr
= dma_addr
;
248 ring
->buf
[i
].desc
->data
= (u32
) dma_addr
;
249 ring
->buf
[i
].desc
->ctrl
= DESC_EMPTY
;
252 /* flush descriptors */
261 static int ag71xx_ring_rx_refill(struct ag71xx
*ag
)
263 struct ag71xx_ring
*ring
= &ag
->rx_ring
;
264 unsigned int reserve
= ag71xx_rx_reserve(ag
);
268 for (; ring
->curr
- ring
->dirty
> 0; ring
->dirty
++) {
271 i
= ring
->dirty
% ring
->size
;
273 if (ring
->buf
[i
].skb
== NULL
) {
277 skb
= dev_alloc_skb(AG71XX_RX_PKT_SIZE
+ reserve
);
281 skb_reserve(skb
, reserve
);
284 dma_addr
= dma_map_single(&ag
->dev
->dev
, skb
->data
,
288 ring
->buf
[i
].skb
= skb
;
289 ring
->buf
[i
].dma_addr
= dma_addr
;
290 ring
->buf
[i
].desc
->data
= (u32
) dma_addr
;
293 ring
->buf
[i
].desc
->ctrl
= DESC_EMPTY
;
297 /* flush descriptors */
300 DBG("%s: %u rx descriptors refilled\n", ag
->dev
->name
, count
);
305 static int ag71xx_rings_init(struct ag71xx
*ag
)
309 ret
= ag71xx_ring_alloc(&ag
->tx_ring
);
313 ag71xx_ring_tx_init(ag
);
315 ret
= ag71xx_ring_alloc(&ag
->rx_ring
);
319 ret
= ag71xx_ring_rx_init(ag
);
323 static void ag71xx_rings_cleanup(struct ag71xx
*ag
)
325 ag71xx_ring_rx_clean(ag
);
326 ag71xx_ring_free(&ag
->rx_ring
);
328 ag71xx_ring_tx_clean(ag
);
329 ag71xx_ring_free(&ag
->tx_ring
);
332 static unsigned char *ag71xx_speed_str(struct ag71xx
*ag
)
346 static void ag71xx_hw_set_macaddr(struct ag71xx
*ag
, unsigned char *mac
)
350 t
= (((u32
) mac
[5]) << 24) | (((u32
) mac
[4]) << 16)
351 | (((u32
) mac
[3]) << 8) | ((u32
) mac
[2]);
353 ag71xx_wr(ag
, AG71XX_REG_MAC_ADDR1
, t
);
355 t
= (((u32
) mac
[1]) << 24) | (((u32
) mac
[0]) << 16);
356 ag71xx_wr(ag
, AG71XX_REG_MAC_ADDR2
, t
);
359 static void ag71xx_dma_reset(struct ag71xx
*ag
)
364 ag71xx_dump_dma_regs(ag
);
367 ag71xx_wr(ag
, AG71XX_REG_RX_CTRL
, 0);
368 ag71xx_wr(ag
, AG71XX_REG_TX_CTRL
, 0);
371 * give the hardware some time to really stop all rx/tx activity
372 * clearing the descriptors too early causes random memory corruption
376 /* clear descriptor addresses */
377 ag71xx_wr(ag
, AG71XX_REG_TX_DESC
, ag
->stop_desc_dma
);
378 ag71xx_wr(ag
, AG71XX_REG_RX_DESC
, ag
->stop_desc_dma
);
380 /* clear pending RX/TX interrupts */
381 for (i
= 0; i
< 256; i
++) {
382 ag71xx_wr(ag
, AG71XX_REG_RX_STATUS
, RX_STATUS_PR
);
383 ag71xx_wr(ag
, AG71XX_REG_TX_STATUS
, TX_STATUS_PS
);
386 /* clear pending errors */
387 ag71xx_wr(ag
, AG71XX_REG_RX_STATUS
, RX_STATUS_BE
| RX_STATUS_OF
);
388 ag71xx_wr(ag
, AG71XX_REG_TX_STATUS
, TX_STATUS_BE
| TX_STATUS_UR
);
390 val
= ag71xx_rr(ag
, AG71XX_REG_RX_STATUS
);
392 printk(KERN_ALERT
"%s: unable to clear DMA Rx status: %08x\n",
395 val
= ag71xx_rr(ag
, AG71XX_REG_TX_STATUS
);
397 /* mask out reserved bits */
401 printk(KERN_ALERT
"%s: unable to clear DMA Tx status: %08x\n",
404 ag71xx_dump_dma_regs(ag
);
407 #define MAC_CFG1_INIT (MAC_CFG1_RXE | MAC_CFG1_TXE | \
408 MAC_CFG1_SRX | MAC_CFG1_STX)
410 #define FIFO_CFG0_INIT (FIFO_CFG0_ALL << FIFO_CFG0_ENABLE_SHIFT)
412 #define FIFO_CFG4_INIT (FIFO_CFG4_DE | FIFO_CFG4_DV | FIFO_CFG4_FC | \
413 FIFO_CFG4_CE | FIFO_CFG4_CR | FIFO_CFG4_LM | \
414 FIFO_CFG4_LO | FIFO_CFG4_OK | FIFO_CFG4_MC | \
415 FIFO_CFG4_BC | FIFO_CFG4_DR | FIFO_CFG4_LE | \
416 FIFO_CFG4_CF | FIFO_CFG4_PF | FIFO_CFG4_UO | \
419 #define FIFO_CFG5_INIT (FIFO_CFG5_DE | FIFO_CFG5_DV | FIFO_CFG5_FC | \
420 FIFO_CFG5_CE | FIFO_CFG5_LO | FIFO_CFG5_OK | \
421 FIFO_CFG5_MC | FIFO_CFG5_BC | FIFO_CFG5_DR | \
422 FIFO_CFG5_CF | FIFO_CFG5_PF | FIFO_CFG5_VT | \
423 FIFO_CFG5_LE | FIFO_CFG5_FT | FIFO_CFG5_16 | \
424 FIFO_CFG5_17 | FIFO_CFG5_SF)
426 static void ag71xx_hw_stop(struct ag71xx
*ag
)
428 /* disable all interrupts and stop the rx/tx engine */
429 ag71xx_wr(ag
, AG71XX_REG_INT_ENABLE
, 0);
430 ag71xx_wr(ag
, AG71XX_REG_RX_CTRL
, 0);
431 ag71xx_wr(ag
, AG71XX_REG_TX_CTRL
, 0);
434 static void ag71xx_hw_setup(struct ag71xx
*ag
)
436 struct ag71xx_platform_data
*pdata
= ag71xx_get_pdata(ag
);
438 /* setup MAC configuration registers */
439 ag71xx_wr(ag
, AG71XX_REG_MAC_CFG1
, MAC_CFG1_INIT
);
441 ag71xx_sb(ag
, AG71XX_REG_MAC_CFG2
,
442 MAC_CFG2_PAD_CRC_EN
| MAC_CFG2_LEN_CHECK
);
444 /* setup max frame length */
445 ag71xx_wr(ag
, AG71XX_REG_MAC_MFL
, AG71XX_TX_MTU_LEN
);
447 /* setup FIFO configuration registers */
448 ag71xx_wr(ag
, AG71XX_REG_FIFO_CFG0
, FIFO_CFG0_INIT
);
449 if (pdata
->is_ar724x
) {
450 ag71xx_wr(ag
, AG71XX_REG_FIFO_CFG1
, pdata
->fifo_cfg1
);
451 ag71xx_wr(ag
, AG71XX_REG_FIFO_CFG2
, pdata
->fifo_cfg2
);
453 ag71xx_wr(ag
, AG71XX_REG_FIFO_CFG1
, 0x0fff0000);
454 ag71xx_wr(ag
, AG71XX_REG_FIFO_CFG2
, 0x00001fff);
456 ag71xx_wr(ag
, AG71XX_REG_FIFO_CFG4
, FIFO_CFG4_INIT
);
457 ag71xx_wr(ag
, AG71XX_REG_FIFO_CFG5
, FIFO_CFG5_INIT
);
460 static void ag71xx_hw_init(struct ag71xx
*ag
)
462 struct ag71xx_platform_data
*pdata
= ag71xx_get_pdata(ag
);
463 u32 reset_mask
= pdata
->reset_bit
;
467 if (pdata
->is_ar724x
) {
468 u32 reset_phy
= reset_mask
;
470 reset_phy
&= RESET_MODULE_GE0_PHY
| RESET_MODULE_GE1_PHY
;
471 reset_mask
&= ~(RESET_MODULE_GE0_PHY
| RESET_MODULE_GE1_PHY
);
473 ar71xx_device_stop(reset_phy
);
475 ar71xx_device_start(reset_phy
);
479 ag71xx_sb(ag
, AG71XX_REG_MAC_CFG1
, MAC_CFG1_SR
);
482 ar71xx_device_stop(reset_mask
);
484 ar71xx_device_start(reset_mask
);
489 ag71xx_dma_reset(ag
);
492 static void ag71xx_fast_reset(struct ag71xx
*ag
)
494 struct ag71xx_platform_data
*pdata
= ag71xx_get_pdata(ag
);
495 struct net_device
*dev
= ag
->dev
;
496 u32 reset_mask
= pdata
->reset_bit
;
500 reset_mask
&= RESET_MODULE_GE0_MAC
| RESET_MODULE_GE1_MAC
;
502 mii_reg
= ag71xx_rr(ag
, AG71XX_REG_MII_CFG
);
503 rx_ds
= ag71xx_rr(ag
, AG71XX_REG_RX_DESC
);
504 tx_ds
= ag71xx_rr(ag
, AG71XX_REG_TX_DESC
);
506 ar71xx_device_stop(reset_mask
);
508 ar71xx_device_start(reset_mask
);
511 ag71xx_dma_reset(ag
);
514 ag71xx_wr(ag
, AG71XX_REG_RX_DESC
, rx_ds
);
515 ag71xx_wr(ag
, AG71XX_REG_TX_DESC
, tx_ds
);
516 ag71xx_wr(ag
, AG71XX_REG_MII_CFG
, mii_reg
);
518 ag71xx_hw_set_macaddr(ag
, dev
->dev_addr
);
521 static void ag71xx_hw_start(struct ag71xx
*ag
)
523 /* start RX engine */
524 ag71xx_wr(ag
, AG71XX_REG_RX_CTRL
, RX_CTRL_RXE
);
526 /* enable interrupts */
527 ag71xx_wr(ag
, AG71XX_REG_INT_ENABLE
, AG71XX_INT_INIT
);
530 void ag71xx_link_adjust(struct ag71xx
*ag
)
532 struct ag71xx_platform_data
*pdata
= ag71xx_get_pdata(ag
);
539 netif_carrier_off(ag
->dev
);
540 if (netif_msg_link(ag
))
541 printk(KERN_INFO
"%s: link down\n", ag
->dev
->name
);
545 if (pdata
->is_ar724x
)
546 ag71xx_fast_reset(ag
);
548 cfg2
= ag71xx_rr(ag
, AG71XX_REG_MAC_CFG2
);
549 cfg2
&= ~(MAC_CFG2_IF_1000
| MAC_CFG2_IF_10_100
| MAC_CFG2_FDX
);
550 cfg2
|= (ag
->duplex
) ? MAC_CFG2_FDX
: 0;
552 ifctl
= ag71xx_rr(ag
, AG71XX_REG_MAC_IFCTL
);
553 ifctl
&= ~(MAC_IFCTL_SPEED
);
555 fifo5
= ag71xx_rr(ag
, AG71XX_REG_FIFO_CFG5
);
556 fifo5
&= ~FIFO_CFG5_BM
;
560 cfg2
|= MAC_CFG2_IF_1000
;
561 fifo5
|= FIFO_CFG5_BM
;
564 cfg2
|= MAC_CFG2_IF_10_100
;
565 ifctl
|= MAC_IFCTL_SPEED
;
568 cfg2
|= MAC_CFG2_IF_10_100
;
575 if (pdata
->is_ar91xx
)
576 ag71xx_wr(ag
, AG71XX_REG_FIFO_CFG3
, 0x00780fff);
577 else if (pdata
->is_ar724x
)
578 ag71xx_wr(ag
, AG71XX_REG_FIFO_CFG3
, pdata
->fifo_cfg3
);
580 ag71xx_wr(ag
, AG71XX_REG_FIFO_CFG3
, 0x008001ff);
582 if (pdata
->set_speed
)
583 pdata
->set_speed(ag
->speed
);
585 ag71xx_wr(ag
, AG71XX_REG_MAC_CFG2
, cfg2
);
586 ag71xx_wr(ag
, AG71XX_REG_FIFO_CFG5
, fifo5
);
587 ag71xx_wr(ag
, AG71XX_REG_MAC_IFCTL
, ifctl
);
590 netif_carrier_on(ag
->dev
);
591 if (netif_msg_link(ag
))
592 printk(KERN_INFO
"%s: link up (%sMbps/%s duplex)\n",
594 ag71xx_speed_str(ag
),
595 (DUPLEX_FULL
== ag
->duplex
) ? "Full" : "Half");
597 DBG("%s: fifo_cfg0=%#x, fifo_cfg1=%#x, fifo_cfg2=%#x\n",
599 ag71xx_rr(ag
, AG71XX_REG_FIFO_CFG0
),
600 ag71xx_rr(ag
, AG71XX_REG_FIFO_CFG1
),
601 ag71xx_rr(ag
, AG71XX_REG_FIFO_CFG2
));
603 DBG("%s: fifo_cfg3=%#x, fifo_cfg4=%#x, fifo_cfg5=%#x\n",
605 ag71xx_rr(ag
, AG71XX_REG_FIFO_CFG3
),
606 ag71xx_rr(ag
, AG71XX_REG_FIFO_CFG4
),
607 ag71xx_rr(ag
, AG71XX_REG_FIFO_CFG5
));
609 DBG("%s: mac_cfg2=%#x, mac_ifctl=%#x, mii_ctrl=%#x\n",
611 ag71xx_rr(ag
, AG71XX_REG_MAC_CFG2
),
612 ag71xx_rr(ag
, AG71XX_REG_MAC_IFCTL
),
613 ag71xx_mii_ctrl_rr(ag
));
616 static int ag71xx_open(struct net_device
*dev
)
618 struct ag71xx
*ag
= netdev_priv(dev
);
621 ret
= ag71xx_rings_init(ag
);
625 napi_enable(&ag
->napi
);
627 netif_carrier_off(dev
);
628 ag71xx_phy_start(ag
);
630 ag71xx_wr(ag
, AG71XX_REG_TX_DESC
, ag
->tx_ring
.descs_dma
);
631 ag71xx_wr(ag
, AG71XX_REG_RX_DESC
, ag
->rx_ring
.descs_dma
);
633 ag71xx_hw_set_macaddr(ag
, dev
->dev_addr
);
635 netif_start_queue(dev
);
640 ag71xx_rings_cleanup(ag
);
644 static int ag71xx_stop(struct net_device
*dev
)
646 struct ag71xx
*ag
= netdev_priv(dev
);
649 netif_carrier_off(dev
);
652 spin_lock_irqsave(&ag
->lock
, flags
);
654 netif_stop_queue(dev
);
657 ag71xx_dma_reset(ag
);
659 napi_disable(&ag
->napi
);
660 del_timer_sync(&ag
->oom_timer
);
662 spin_unlock_irqrestore(&ag
->lock
, flags
);
664 ag71xx_rings_cleanup(ag
);
669 static netdev_tx_t
ag71xx_hard_start_xmit(struct sk_buff
*skb
,
670 struct net_device
*dev
)
672 struct ag71xx
*ag
= netdev_priv(dev
);
673 struct ag71xx_ring
*ring
= &ag
->tx_ring
;
674 struct ag71xx_desc
*desc
;
678 i
= ring
->curr
% ring
->size
;
679 desc
= ring
->buf
[i
].desc
;
681 if (!ag71xx_desc_empty(desc
))
684 if (ag71xx_has_ar8216(ag
))
685 ag71xx_add_ar8216_header(ag
, skb
);
688 DBG("%s: packet len is too small\n", ag
->dev
->name
);
692 dma_addr
= dma_map_single(&dev
->dev
, skb
->data
, skb
->len
,
695 ring
->buf
[i
].skb
= skb
;
696 ring
->buf
[i
].timestamp
= jiffies
;
698 /* setup descriptor fields */
699 desc
->data
= (u32
) dma_addr
;
700 desc
->ctrl
= (skb
->len
& DESC_PKTLEN_M
);
702 /* flush descriptor */
706 if (ring
->curr
== (ring
->dirty
+ ring
->size
)) {
707 DBG("%s: tx queue full\n", ag
->dev
->name
);
708 netif_stop_queue(dev
);
711 DBG("%s: packet injected into TX queue\n", ag
->dev
->name
);
713 /* enable TX engine */
714 ag71xx_wr(ag
, AG71XX_REG_TX_CTRL
, TX_CTRL_TXE
);
719 dev
->stats
.tx_dropped
++;
725 static int ag71xx_do_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
)
727 struct ag71xx
*ag
= netdev_priv(dev
);
732 if (ag
->phy_dev
== NULL
)
735 spin_lock_irq(&ag
->lock
);
736 ret
= phy_ethtool_ioctl(ag
->phy_dev
, (void *) ifr
->ifr_data
);
737 spin_unlock_irq(&ag
->lock
);
742 (dev
->dev_addr
, ifr
->ifr_data
, sizeof(dev
->dev_addr
)))
748 (ifr
->ifr_data
, dev
->dev_addr
, sizeof(dev
->dev_addr
)))
755 if (ag
->phy_dev
== NULL
)
758 return phy_mii_ioctl(ag
->phy_dev
, ifr
, cmd
);
767 static void ag71xx_oom_timer_handler(unsigned long data
)
769 struct net_device
*dev
= (struct net_device
*) data
;
770 struct ag71xx
*ag
= netdev_priv(dev
);
772 napi_schedule(&ag
->napi
);
775 static void ag71xx_tx_timeout(struct net_device
*dev
)
777 struct ag71xx
*ag
= netdev_priv(dev
);
779 if (netif_msg_tx_err(ag
))
780 printk(KERN_DEBUG
"%s: tx timeout\n", ag
->dev
->name
);
782 schedule_work(&ag
->restart_work
);
785 static void ag71xx_restart_work_func(struct work_struct
*work
)
787 struct ag71xx
*ag
= container_of(work
, struct ag71xx
, restart_work
);
789 if (ag71xx_get_pdata(ag
)->is_ar724x
) {
791 ag71xx_link_adjust(ag
);
795 ag71xx_stop(ag
->dev
);
796 ag71xx_open(ag
->dev
);
799 static bool ag71xx_check_dma_stuck(struct ag71xx
*ag
, unsigned long timestamp
)
801 u32 rx_sm
, tx_sm
, rx_fd
;
803 if (likely(time_before(jiffies
, timestamp
+ HZ
/10)))
806 if (!netif_carrier_ok(ag
->dev
))
809 rx_sm
= ag71xx_rr(ag
, AG71XX_REG_RX_SM
);
810 if ((rx_sm
& 0x7) == 0x3 && ((rx_sm
>> 4) & 0x7) == 0x6)
813 tx_sm
= ag71xx_rr(ag
, AG71XX_REG_TX_SM
);
814 rx_fd
= ag71xx_rr(ag
, AG71XX_REG_FIFO_DEPTH
);
815 if (((tx_sm
>> 4) & 0x7) == 0 && ((rx_sm
& 0x7) == 0) &&
816 ((rx_sm
>> 4) & 0x7) == 0 && rx_fd
== 0)
822 static int ag71xx_tx_packets(struct ag71xx
*ag
)
824 struct ag71xx_ring
*ring
= &ag
->tx_ring
;
825 struct ag71xx_platform_data
*pdata
= ag71xx_get_pdata(ag
);
828 DBG("%s: processing TX ring\n", ag
->dev
->name
);
831 while (ring
->dirty
!= ring
->curr
) {
832 unsigned int i
= ring
->dirty
% ring
->size
;
833 struct ag71xx_desc
*desc
= ring
->buf
[i
].desc
;
834 struct sk_buff
*skb
= ring
->buf
[i
].skb
;
836 if (!ag71xx_desc_empty(desc
)) {
837 if (pdata
->is_ar7240
&&
838 ag71xx_check_dma_stuck(ag
, ring
->buf
[i
].timestamp
))
839 schedule_work(&ag
->restart_work
);
843 ag71xx_wr(ag
, AG71XX_REG_TX_STATUS
, TX_STATUS_PS
);
845 ag
->dev
->stats
.tx_bytes
+= skb
->len
;
846 ag
->dev
->stats
.tx_packets
++;
848 dev_kfree_skb_any(skb
);
849 ring
->buf
[i
].skb
= NULL
;
855 DBG("%s: %d packets sent out\n", ag
->dev
->name
, sent
);
857 if ((ring
->curr
- ring
->dirty
) < (ring
->size
* 3) / 4)
858 netif_wake_queue(ag
->dev
);
863 static int ag71xx_rx_packets(struct ag71xx
*ag
, int limit
)
865 struct net_device
*dev
= ag
->dev
;
866 struct ag71xx_ring
*ring
= &ag
->rx_ring
;
869 DBG("%s: rx packets, limit=%d, curr=%u, dirty=%u\n",
870 dev
->name
, limit
, ring
->curr
, ring
->dirty
);
872 while (done
< limit
) {
873 unsigned int i
= ring
->curr
% ring
->size
;
874 struct ag71xx_desc
*desc
= ring
->buf
[i
].desc
;
879 if (ag71xx_desc_empty(desc
))
882 if ((ring
->dirty
+ ring
->size
) == ring
->curr
) {
887 ag71xx_wr(ag
, AG71XX_REG_RX_STATUS
, RX_STATUS_PR
);
889 skb
= ring
->buf
[i
].skb
;
890 pktlen
= ag71xx_desc_pktlen(desc
);
891 pktlen
-= ETH_FCS_LEN
;
893 dma_unmap_single(&dev
->dev
, ring
->buf
[i
].dma_addr
,
894 AG71XX_RX_PKT_SIZE
, DMA_FROM_DEVICE
);
896 dev
->last_rx
= jiffies
;
897 dev
->stats
.rx_packets
++;
898 dev
->stats
.rx_bytes
+= pktlen
;
900 skb_put(skb
, pktlen
);
901 if (ag71xx_has_ar8216(ag
))
902 err
= ag71xx_remove_ar8216_header(ag
, skb
, pktlen
);
905 dev
->stats
.rx_dropped
++;
909 skb
->ip_summed
= CHECKSUM_NONE
;
911 ag
->phy_dev
->netif_receive_skb(skb
);
913 skb
->protocol
= eth_type_trans(skb
, dev
);
914 netif_receive_skb(skb
);
918 ring
->buf
[i
].skb
= NULL
;
924 ag71xx_ring_rx_refill(ag
);
926 DBG("%s: rx finish, curr=%u, dirty=%u, done=%d\n",
927 dev
->name
, ring
->curr
, ring
->dirty
, done
);
932 static int ag71xx_poll(struct napi_struct
*napi
, int limit
)
934 struct ag71xx
*ag
= container_of(napi
, struct ag71xx
, napi
);
935 struct ag71xx_platform_data
*pdata
= ag71xx_get_pdata(ag
);
936 struct net_device
*dev
= ag
->dev
;
937 struct ag71xx_ring
*rx_ring
;
944 tx_done
= ag71xx_tx_packets(ag
);
946 DBG("%s: processing RX ring\n", dev
->name
);
947 rx_done
= ag71xx_rx_packets(ag
, limit
);
949 ag71xx_debugfs_update_napi_stats(ag
, rx_done
, tx_done
);
951 rx_ring
= &ag
->rx_ring
;
952 if (rx_ring
->buf
[rx_ring
->dirty
% rx_ring
->size
].skb
== NULL
)
955 status
= ag71xx_rr(ag
, AG71XX_REG_RX_STATUS
);
956 if (unlikely(status
& RX_STATUS_OF
)) {
957 ag71xx_wr(ag
, AG71XX_REG_RX_STATUS
, RX_STATUS_OF
);
958 dev
->stats
.rx_fifo_errors
++;
961 ag71xx_wr(ag
, AG71XX_REG_RX_CTRL
, RX_CTRL_RXE
);
964 if (rx_done
< limit
) {
965 if (status
& RX_STATUS_PR
)
968 status
= ag71xx_rr(ag
, AG71XX_REG_TX_STATUS
);
969 if (status
& TX_STATUS_PS
)
972 DBG("%s: disable polling mode, rx=%d, tx=%d,limit=%d\n",
973 dev
->name
, rx_done
, tx_done
, limit
);
977 /* enable interrupts */
978 spin_lock_irqsave(&ag
->lock
, flags
);
979 ag71xx_int_enable(ag
, AG71XX_INT_POLL
);
980 spin_unlock_irqrestore(&ag
->lock
, flags
);
985 DBG("%s: stay in polling mode, rx=%d, tx=%d, limit=%d\n",
986 dev
->name
, rx_done
, tx_done
, limit
);
990 if (netif_msg_rx_err(ag
))
991 printk(KERN_DEBUG
"%s: out of memory\n", dev
->name
);
993 mod_timer(&ag
->oom_timer
, jiffies
+ AG71XX_OOM_REFILL
);
998 static irqreturn_t
ag71xx_interrupt(int irq
, void *dev_id
)
1000 struct net_device
*dev
= dev_id
;
1001 struct ag71xx
*ag
= netdev_priv(dev
);
1004 status
= ag71xx_rr(ag
, AG71XX_REG_INT_STATUS
);
1005 ag71xx_dump_intr(ag
, "raw", status
);
1007 if (unlikely(!status
))
1010 if (unlikely(status
& AG71XX_INT_ERR
)) {
1011 if (status
& AG71XX_INT_TX_BE
) {
1012 ag71xx_wr(ag
, AG71XX_REG_TX_STATUS
, TX_STATUS_BE
);
1013 dev_err(&dev
->dev
, "TX BUS error\n");
1015 if (status
& AG71XX_INT_RX_BE
) {
1016 ag71xx_wr(ag
, AG71XX_REG_RX_STATUS
, RX_STATUS_BE
);
1017 dev_err(&dev
->dev
, "RX BUS error\n");
1021 if (likely(status
& AG71XX_INT_POLL
)) {
1022 ag71xx_int_disable(ag
, AG71XX_INT_POLL
);
1023 DBG("%s: enable polling mode\n", dev
->name
);
1024 napi_schedule(&ag
->napi
);
1027 ag71xx_debugfs_update_int_stats(ag
, status
);
1032 static void ag71xx_set_multicast_list(struct net_device
*dev
)
1037 #ifdef CONFIG_NET_POLL_CONTROLLER
1039 * Polling 'interrupt' - used by things like netconsole to send skbs
1040 * without having to re-enable interrupts. It's not called while
1041 * the interrupt routine is executing.
1043 static void ag71xx_netpoll(struct net_device
*dev
)
1045 disable_irq(dev
->irq
);
1046 ag71xx_interrupt(dev
->irq
, dev
);
1047 enable_irq(dev
->irq
);
1051 static const struct net_device_ops ag71xx_netdev_ops
= {
1052 .ndo_open
= ag71xx_open
,
1053 .ndo_stop
= ag71xx_stop
,
1054 .ndo_start_xmit
= ag71xx_hard_start_xmit
,
1055 .ndo_set_multicast_list
= ag71xx_set_multicast_list
,
1056 .ndo_do_ioctl
= ag71xx_do_ioctl
,
1057 .ndo_tx_timeout
= ag71xx_tx_timeout
,
1058 .ndo_change_mtu
= eth_change_mtu
,
1059 .ndo_set_mac_address
= eth_mac_addr
,
1060 .ndo_validate_addr
= eth_validate_addr
,
1061 #ifdef CONFIG_NET_POLL_CONTROLLER
1062 .ndo_poll_controller
= ag71xx_netpoll
,
1066 static int __devinit
ag71xx_probe(struct platform_device
*pdev
)
1068 struct net_device
*dev
;
1069 struct resource
*res
;
1071 struct ag71xx_platform_data
*pdata
;
1074 pdata
= pdev
->dev
.platform_data
;
1076 dev_err(&pdev
->dev
, "no platform data specified\n");
1081 if (pdata
->mii_bus_dev
== NULL
) {
1082 dev_err(&pdev
->dev
, "no MII bus device specified\n");
1087 dev
= alloc_etherdev(sizeof(*ag
));
1089 dev_err(&pdev
->dev
, "alloc_etherdev failed\n");
1094 SET_NETDEV_DEV(dev
, &pdev
->dev
);
1096 ag
= netdev_priv(dev
);
1099 ag
->msg_enable
= netif_msg_init(ag71xx_msg_level
,
1100 AG71XX_DEFAULT_MSG_ENABLE
);
1101 spin_lock_init(&ag
->lock
);
1103 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "mac_base");
1105 dev_err(&pdev
->dev
, "no mac_base resource found\n");
1110 ag
->mac_base
= ioremap_nocache(res
->start
, res
->end
- res
->start
+ 1);
1111 if (!ag
->mac_base
) {
1112 dev_err(&pdev
->dev
, "unable to ioremap mac_base\n");
1117 dev
->irq
= platform_get_irq(pdev
, 0);
1118 err
= request_irq(dev
->irq
, ag71xx_interrupt
,
1122 dev_err(&pdev
->dev
, "unable to request IRQ %d\n", dev
->irq
);
1123 goto err_unmap_base
;
1126 dev
->base_addr
= (unsigned long)ag
->mac_base
;
1127 dev
->netdev_ops
= &ag71xx_netdev_ops
;
1128 dev
->ethtool_ops
= &ag71xx_ethtool_ops
;
1130 INIT_WORK(&ag
->restart_work
, ag71xx_restart_work_func
);
1132 init_timer(&ag
->oom_timer
);
1133 ag
->oom_timer
.data
= (unsigned long) dev
;
1134 ag
->oom_timer
.function
= ag71xx_oom_timer_handler
;
1136 ag
->tx_ring
.size
= AG71XX_TX_RING_SIZE_DEFAULT
;
1137 ag
->rx_ring
.size
= AG71XX_RX_RING_SIZE_DEFAULT
;
1139 ag
->stop_desc
= dma_alloc_coherent(NULL
,
1140 sizeof(struct ag71xx_desc
), &ag
->stop_desc_dma
, GFP_KERNEL
);
1145 ag
->stop_desc
->data
= 0;
1146 ag
->stop_desc
->ctrl
= 0;
1147 ag
->stop_desc
->next
= (u32
) ag
->stop_desc_dma
;
1149 memcpy(dev
->dev_addr
, pdata
->mac_addr
, ETH_ALEN
);
1151 netif_napi_add(dev
, &ag
->napi
, ag71xx_poll
, AG71XX_NAPI_WEIGHT
);
1153 err
= register_netdev(dev
);
1155 dev_err(&pdev
->dev
, "unable to register net device\n");
1159 printk(KERN_INFO
"%s: Atheros AG71xx at 0x%08lx, irq %d\n",
1160 dev
->name
, dev
->base_addr
, dev
->irq
);
1162 ag71xx_dump_regs(ag
);
1166 ag71xx_dump_regs(ag
);
1168 err
= ag71xx_phy_connect(ag
);
1170 goto err_unregister_netdev
;
1172 err
= ag71xx_debugfs_init(ag
);
1174 goto err_phy_disconnect
;
1176 platform_set_drvdata(pdev
, dev
);
1181 ag71xx_phy_disconnect(ag
);
1182 err_unregister_netdev
:
1183 unregister_netdev(dev
);
1185 dma_free_coherent(NULL
, sizeof(struct ag71xx_desc
), ag
->stop_desc
,
1188 free_irq(dev
->irq
, dev
);
1190 iounmap(ag
->mac_base
);
1194 platform_set_drvdata(pdev
, NULL
);
1198 static int __devexit
ag71xx_remove(struct platform_device
*pdev
)
1200 struct net_device
*dev
= platform_get_drvdata(pdev
);
1203 struct ag71xx
*ag
= netdev_priv(dev
);
1205 ag71xx_debugfs_exit(ag
);
1206 ag71xx_phy_disconnect(ag
);
1207 unregister_netdev(dev
);
1208 free_irq(dev
->irq
, dev
);
1209 iounmap(ag
->mac_base
);
1211 platform_set_drvdata(pdev
, NULL
);
1217 static struct platform_driver ag71xx_driver
= {
1218 .probe
= ag71xx_probe
,
1219 .remove
= __exit_p(ag71xx_remove
),
1221 .name
= AG71XX_DRV_NAME
,
1225 static int __init
ag71xx_module_init(void)
1229 ret
= ag71xx_debugfs_root_init();
1233 ret
= ag71xx_mdio_driver_init();
1235 goto err_debugfs_exit
;
1237 ret
= platform_driver_register(&ag71xx_driver
);
1244 ag71xx_mdio_driver_exit();
1246 ag71xx_debugfs_root_exit();
1251 static void __exit
ag71xx_module_exit(void)
1253 platform_driver_unregister(&ag71xx_driver
);
1254 ag71xx_mdio_driver_exit();
1255 ag71xx_debugfs_root_exit();
1258 module_init(ag71xx_module_init
);
1259 module_exit(ag71xx_module_exit
);
1261 MODULE_VERSION(AG71XX_DRV_VERSION
);
1262 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
1263 MODULE_AUTHOR("Imre Kaloz <kaloz@openwrt.org>");
1264 MODULE_LICENSE("GPL v2");
1265 MODULE_ALIAS("platform:" AG71XX_DRV_NAME
);