5e31438b301ec56e9da6fd3e553a761faed85cd6
[openwrt/svn-archive/archive.git] / target / linux / ar71xx / files / drivers / net / ag71xx / ag71xx_main.c
1 /*
2 * Atheros AR71xx built-in ethernet mac driver
3 *
4 * Copyright (C) 2008 Gabor Juhos <juhosg@openwrt.org>
5 * Copyright (C) 2008 Imre Kaloz <kaloz@openwrt.org>
6 *
7 * Based on Atheros' AG7100 driver
8 *
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.
12 */
13
14 #include "ag71xx.h"
15
16 #define AG71XX_DEFAULT_MSG_ENABLE \
17 ( NETIF_MSG_DRV \
18 | NETIF_MSG_PROBE \
19 | NETIF_MSG_LINK \
20 | NETIF_MSG_TIMER \
21 | NETIF_MSG_IFDOWN \
22 | NETIF_MSG_IFUP \
23 | NETIF_MSG_RX_ERR \
24 | NETIF_MSG_TX_ERR )
25
26 static int ag71xx_debug = -1;
27
28 module_param(ag71xx_debug, int, 0);
29 MODULE_PARM_DESC(ag71xx_debug, "Debug level (-1=defaults,0=none,...,16=all)");
30
31 static void ag71xx_dump_dma_regs(struct ag71xx *ag)
32 {
33 DBG("%s: dma_tx_ctrl=%08x, dma_tx_desc=%08x, dma_tx_status=%08x\n",
34 ag->dev->name,
35 ag71xx_rr(ag, AG71XX_REG_TX_CTRL),
36 ag71xx_rr(ag, AG71XX_REG_TX_DESC),
37 ag71xx_rr(ag, AG71XX_REG_TX_STATUS));
38
39 DBG("%s: dma_rx_ctrl=%08x, dma_rx_desc=%08x, dma_rx_status=%08x\n",
40 ag->dev->name,
41 ag71xx_rr(ag, AG71XX_REG_RX_CTRL),
42 ag71xx_rr(ag, AG71XX_REG_RX_DESC),
43 ag71xx_rr(ag, AG71XX_REG_RX_STATUS));
44 }
45
46 static void ag71xx_dump_regs(struct ag71xx *ag)
47 {
48 DBG("%s: mac_cfg1=%08x, mac_cfg2=%08x, ipg=%08x, hdx=%08x, mfl=%08x\n",
49 ag->dev->name,
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",
56 ag->dev->name,
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",
61 ag->dev->name,
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",
66 ag->dev->name,
67 ag71xx_rr(ag, AG71XX_REG_FIFO_CFG3),
68 ag71xx_rr(ag, AG71XX_REG_FIFO_CFG4),
69 ag71xx_rr(ag, AG71XX_REG_FIFO_CFG5));
70 }
71
72 static inline void ag71xx_dump_intr(struct ag71xx *ag, char *label, u32 intr)
73 {
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 " : "");
82 }
83
84 static void ag71xx_ring_free(struct ag71xx_ring *ring)
85 {
86 kfree(ring->buf);
87
88 if (ring->descs)
89 dma_free_coherent(NULL, ring->size * sizeof(*ring->descs),
90 ring->descs, ring->descs_dma);
91 }
92
93 static int ag71xx_ring_alloc(struct ag71xx_ring *ring, unsigned int size)
94 {
95 int err;
96
97 ring->descs = dma_alloc_coherent(NULL, size * sizeof(*ring->descs),
98 &ring->descs_dma,
99 GFP_ATOMIC);
100 if (!ring->descs) {
101 err = -ENOMEM;
102 goto err;
103 }
104
105 ring->size = size;
106
107 ring->buf = kzalloc(size * sizeof(*ring->buf), GFP_KERNEL);
108 if (!ring->buf) {
109 err = -ENOMEM;
110 goto err;
111 }
112
113 return 0;
114
115 err:
116 return err;
117 }
118
119 static void ag71xx_ring_tx_clean(struct ag71xx *ag)
120 {
121 struct ag71xx_ring *ring = &ag->tx_ring;
122 struct net_device *dev = ag->dev;
123
124 while (ring->curr != ring->dirty) {
125 u32 i = ring->dirty % AG71XX_TX_RING_SIZE;
126
127 if (!ag71xx_desc_empty(&ring->descs[i])) {
128 ring->descs[i].ctrl = 0;
129 dev->stats.tx_errors++;
130 }
131
132 if (ring->buf[i].skb)
133 dev_kfree_skb_any(ring->buf[i].skb);
134
135 ring->buf[i].skb = NULL;
136
137 ring->dirty++;
138 }
139
140 /* flush descriptors */
141 wmb();
142
143 }
144
145 static void ag71xx_ring_tx_init(struct ag71xx *ag)
146 {
147 struct ag71xx_ring *ring = &ag->tx_ring;
148 int i;
149
150 for (i = 0; i < AG71XX_TX_RING_SIZE; i++) {
151 ring->descs[i].next = (u32) (ring->descs_dma +
152 sizeof(*ring->descs) * ((i + 1) % AG71XX_TX_RING_SIZE));
153
154 ring->descs[i].ctrl = DESC_EMPTY;
155 ring->buf[i].skb = NULL;
156 }
157
158 /* flush descriptors */
159 wmb();
160
161 ring->curr = 0;
162 ring->dirty = 0;
163 }
164
165 static void ag71xx_ring_rx_clean(struct ag71xx *ag)
166 {
167 struct ag71xx_ring *ring = &ag->rx_ring;
168 int i;
169
170 if (!ring->buf)
171 return;
172
173 for (i = 0; i < AG71XX_RX_RING_SIZE; i++)
174 if (ring->buf[i].skb)
175 kfree_skb(ring->buf[i].skb);
176
177 }
178
179 static int ag71xx_ring_rx_init(struct ag71xx *ag)
180 {
181 struct ag71xx_ring *ring = &ag->rx_ring;
182 unsigned int i;
183 int ret;
184
185 ret = 0;
186 for (i = 0; i < AG71XX_RX_RING_SIZE; i++)
187 ring->descs[i].next = (u32) (ring->descs_dma +
188 sizeof(*ring->descs) * ((i + 1) % AG71XX_RX_RING_SIZE));
189
190 for (i = 0; i < AG71XX_RX_RING_SIZE; i++) {
191 struct sk_buff *skb;
192
193 skb = dev_alloc_skb(AG71XX_RX_PKT_SIZE);
194 if (!skb) {
195 ret = -ENOMEM;
196 break;
197 }
198
199 dma_map_single(NULL, skb->data, AG71XX_RX_PKT_SIZE,
200 DMA_FROM_DEVICE);
201
202 skb->dev = ag->dev;
203 skb_reserve(skb, AG71XX_RX_PKT_RESERVE);
204
205 ring->buf[i].skb = skb;
206 ring->descs[i].data = virt_to_phys(skb->data);
207 ring->descs[i].ctrl = DESC_EMPTY;
208 }
209
210 /* flush descriptors */
211 wmb();
212
213 ring->curr = 0;
214 ring->dirty = 0;
215
216 return ret;
217 }
218
219 static int ag71xx_ring_rx_refill(struct ag71xx *ag)
220 {
221 struct ag71xx_ring *ring = &ag->rx_ring;
222 unsigned int count;
223
224 count = 0;
225 for (; ring->curr - ring->dirty > 0; ring->dirty++) {
226 unsigned int i;
227
228 i = ring->dirty % AG71XX_RX_RING_SIZE;
229
230 if (ring->buf[i].skb == NULL) {
231 struct sk_buff *skb;
232
233 skb = dev_alloc_skb(AG71XX_RX_PKT_SIZE);
234 if (skb == NULL) {
235 printk(KERN_ERR "%s: no memory for skb\n",
236 ag->dev->name);
237 break;
238 }
239
240 dma_map_single(NULL, skb->data, AG71XX_RX_PKT_SIZE,
241 DMA_FROM_DEVICE);
242
243 skb_reserve(skb, AG71XX_RX_PKT_RESERVE);
244 skb->dev = ag->dev;
245
246 ring->buf[i].skb = skb;
247 ring->descs[i].data = virt_to_phys(skb->data);
248 }
249
250 ring->descs[i].ctrl = DESC_EMPTY;
251 count++;
252 }
253
254 /* flush descriptors */
255 wmb();
256
257 DBG("%s: %u rx descriptors refilled\n", ag->dev->name, count);
258
259 return count;
260 }
261
262 static int ag71xx_rings_init(struct ag71xx *ag)
263 {
264 int ret;
265
266 ret = ag71xx_ring_alloc(&ag->tx_ring, AG71XX_TX_RING_SIZE);
267 if (ret)
268 return ret;
269
270 ag71xx_ring_tx_init(ag);
271
272 ret = ag71xx_ring_alloc(&ag->rx_ring, AG71XX_RX_RING_SIZE);
273 if (ret)
274 return ret;
275
276 ret = ag71xx_ring_rx_init(ag);
277 return ret;
278 }
279
280 static void ag71xx_rings_cleanup(struct ag71xx *ag)
281 {
282 ag71xx_ring_rx_clean(ag);
283 ag71xx_ring_free(&ag->rx_ring);
284
285 ag71xx_ring_tx_clean(ag);
286 ag71xx_ring_free(&ag->tx_ring);
287 }
288
289 static void ag71xx_hw_set_macaddr(struct ag71xx *ag, unsigned char *mac)
290 {
291 u32 t;
292
293 t = (((u32) mac[0]) << 24) | (((u32) mac[1]) << 16)
294 | (((u32) mac[2]) << 8) | ((u32) mac[3]);
295
296 ag71xx_wr(ag, AG71XX_REG_MAC_ADDR1, t);
297
298 t = (((u32) mac[4]) << 24) | (((u32) mac[5]) << 16);
299 ag71xx_wr(ag, AG71XX_REG_MAC_ADDR2, t);
300 }
301
302 #define AR71XX_MAC_CFG1_INIT (MAC_CFG1_RXE | MAC_CFG1_TXE | \
303 MAC_CFG1_SRX | MAC_CFG1_STX)
304 #define AR71XX_FIFO_CFG5_INIT 0x0007ffef
305
306 #define AR91XX_MAC_CFG1_INIT (MAC_CFG1_RXE | MAC_CFG1_TXE | \
307 MAC_CFG1_SRX | MAC_CFG1_STX | \
308 MAC_CFG1_TFC | MAC_CFG1_RFC)
309 #define AR91XX_FIFO_CFG5_INIT 0x0007efef
310
311 #define FIFO_CFG0_INIT (FIFO_CFG0_ALL << FIFO_CFG0_ENABLE_SHIFT)
312
313 static void ag71xx_dma_reset(struct ag71xx *ag)
314 {
315 int i;
316
317 ag71xx_dump_dma_regs(ag);
318
319 /* stop RX and TX */
320 ag71xx_wr(ag, AG71XX_REG_RX_CTRL, 0);
321 ag71xx_wr(ag, AG71XX_REG_TX_CTRL, 0);
322
323 /* clear descriptor addresses */
324 ag71xx_wr(ag, AG71XX_REG_TX_DESC, 0);
325 ag71xx_wr(ag, AG71XX_REG_RX_DESC, 0);
326
327 /* clear pending RX/TX interrupts */
328 for (i = 0; i < 256; i++) {
329 ag71xx_wr(ag, AG71XX_REG_RX_STATUS, RX_STATUS_PR);
330 ag71xx_wr(ag, AG71XX_REG_TX_STATUS, TX_STATUS_PS);
331 }
332
333 /* clear pending errors */
334 ag71xx_wr(ag, AG71XX_REG_RX_STATUS, RX_STATUS_BE | RX_STATUS_OF);
335 ag71xx_wr(ag, AG71XX_REG_TX_STATUS, TX_STATUS_BE | TX_STATUS_UR);
336
337 if (ag71xx_rr(ag, AG71XX_REG_RX_STATUS))
338 printk(KERN_ALERT "%s: unable to clear DMA Rx status\n",
339 ag->dev->name);
340
341 if (ag71xx_rr(ag, AG71XX_REG_TX_STATUS))
342 printk(KERN_ALERT "%s: unable to clear DMA Tx status\n",
343 ag->dev->name);
344
345 ag71xx_dump_dma_regs(ag);
346 }
347
348 static void ag71xx_hw_init(struct ag71xx *ag)
349 {
350 struct ag71xx_platform_data *pdata = ag71xx_get_pdata(ag);
351
352 ag71xx_sb(ag, AG71XX_REG_MAC_CFG1, MAC_CFG1_SR);
353 udelay(20);
354
355 ar71xx_device_stop(pdata->reset_bit);
356 mdelay(100);
357 ar71xx_device_start(pdata->reset_bit);
358 mdelay(100);
359
360 /* setup MAC configuration registers */
361 ag71xx_wr(ag, AG71XX_REG_MAC_CFG1,
362 pdata->is_ar91xx ? AR91XX_MAC_CFG1_INIT : AR71XX_MAC_CFG1_INIT);
363 ag71xx_sb(ag, AG71XX_REG_MAC_CFG2,
364 MAC_CFG2_PAD_CRC_EN | MAC_CFG2_LEN_CHECK);
365
366 /* setup max frame length */
367 ag71xx_wr(ag, AG71XX_REG_MAC_MFL, AG71XX_TX_MTU_LEN);
368
369 /* setup MII interface type */
370 ag71xx_mii_ctrl_set_if(ag, pdata->mii_if);
371
372 /* setup FIFO configuration registers */
373 ag71xx_wr(ag, AG71XX_REG_FIFO_CFG0, FIFO_CFG0_INIT);
374 ag71xx_wr(ag, AG71XX_REG_FIFO_CFG1, 0x0fff0000);
375 ag71xx_wr(ag, AG71XX_REG_FIFO_CFG2, 0x00001fff);
376 ag71xx_wr(ag, AG71XX_REG_FIFO_CFG4, 0x0000ffff);
377 ag71xx_wr(ag, AG71XX_REG_FIFO_CFG5,
378 pdata->is_ar91xx ? AR91XX_FIFO_CFG5_INIT
379 : AR71XX_FIFO_CFG5_INIT);
380
381 ag71xx_dma_reset(ag);
382 }
383
384 static void ag71xx_hw_start(struct ag71xx *ag)
385 {
386 /* start RX engine */
387 ag71xx_wr(ag, AG71XX_REG_RX_CTRL, RX_CTRL_RXE);
388
389 /* enable interrupts */
390 ag71xx_wr(ag, AG71XX_REG_INT_ENABLE, AG71XX_INT_INIT);
391 }
392
393 static void ag71xx_hw_stop(struct ag71xx *ag)
394 {
395 /* disable all interrupts */
396 ag71xx_wr(ag, AG71XX_REG_INT_ENABLE, 0);
397
398 ag71xx_dma_reset(ag);
399 }
400
401 static int ag71xx_open(struct net_device *dev)
402 {
403 struct ag71xx *ag = netdev_priv(dev);
404 int ret;
405
406 ret = ag71xx_rings_init(ag);
407 if (ret)
408 goto err;
409
410 napi_enable(&ag->napi);
411
412 netif_carrier_off(dev);
413 ag71xx_phy_start(ag);
414
415 ag71xx_wr(ag, AG71XX_REG_TX_DESC, ag->tx_ring.descs_dma);
416 ag71xx_wr(ag, AG71XX_REG_RX_DESC, ag->rx_ring.descs_dma);
417
418 ag71xx_hw_set_macaddr(ag, dev->dev_addr);
419
420 ag71xx_hw_start(ag);
421
422 netif_start_queue(dev);
423
424 return 0;
425
426 err:
427 ag71xx_rings_cleanup(ag);
428 return ret;
429 }
430
431 static int ag71xx_stop(struct net_device *dev)
432 {
433 struct ag71xx *ag = netdev_priv(dev);
434 unsigned long flags;
435
436 spin_lock_irqsave(&ag->lock, flags);
437
438 netif_stop_queue(dev);
439
440 ag71xx_hw_stop(ag);
441
442 netif_carrier_off(dev);
443 ag71xx_phy_stop(ag);
444
445 napi_disable(&ag->napi);
446
447 spin_unlock_irqrestore(&ag->lock, flags);
448
449 ag71xx_rings_cleanup(ag);
450
451 return 0;
452 }
453
454 static int ag71xx_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
455 {
456 struct ag71xx *ag = netdev_priv(dev);
457 struct ag71xx_platform_data *pdata = ag71xx_get_pdata(ag);
458 struct ag71xx_ring *ring = &ag->tx_ring;
459 struct ag71xx_desc *desc;
460 unsigned long flags;
461 int i;
462
463 i = ring->curr % AG71XX_TX_RING_SIZE;
464 desc = &ring->descs[i];
465
466 spin_lock_irqsave(&ag->lock, flags);
467 pdata->ddr_flush();
468 spin_unlock_irqrestore(&ag->lock, flags);
469
470 if (!ag71xx_desc_empty(desc))
471 goto err_drop;
472
473 if (skb->len <= 0) {
474 DBG("%s: packet len is too small\n", ag->dev->name);
475 goto err_drop;
476 }
477
478 dma_map_single(NULL, skb->data, skb->len, DMA_TO_DEVICE);
479
480 ring->buf[i].skb = skb;
481
482 /* setup descriptor fields */
483 desc->data = virt_to_phys(skb->data);
484 desc->ctrl = (skb->len & DESC_PKTLEN_M);
485
486 /* flush descriptor */
487 wmb();
488
489 ring->curr++;
490 if (ring->curr == (ring->dirty + AG71XX_TX_THRES_STOP)) {
491 DBG("%s: tx queue full\n", ag->dev->name);
492 netif_stop_queue(dev);
493 }
494
495 DBG("%s: packet injected into TX queue\n", ag->dev->name);
496
497 /* enable TX engine */
498 ag71xx_wr(ag, AG71XX_REG_TX_CTRL, TX_CTRL_TXE);
499
500 dev->trans_start = jiffies;
501
502 return 0;
503
504 err_drop:
505 dev->stats.tx_dropped++;
506
507 dev_kfree_skb(skb);
508 return 0;
509 }
510
511 static int ag71xx_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
512 {
513 struct mii_ioctl_data *data = (struct mii_ioctl_data *) &ifr->ifr_data;
514 struct ag71xx *ag = netdev_priv(dev);
515 int ret;
516
517 switch (cmd) {
518 case SIOCETHTOOL:
519 if (ag->phy_dev == NULL)
520 break;
521
522 spin_lock_irq(&ag->lock);
523 ret = phy_ethtool_ioctl(ag->phy_dev, (void *) ifr->ifr_data);
524 spin_unlock_irq(&ag->lock);
525 return ret;
526
527 case SIOCSIFHWADDR:
528 if (copy_from_user
529 (dev->dev_addr, ifr->ifr_data, sizeof(dev->dev_addr)))
530 return -EFAULT;
531 return 0;
532
533 case SIOCGIFHWADDR:
534 if (copy_to_user
535 (ifr->ifr_data, dev->dev_addr, sizeof(dev->dev_addr)))
536 return -EFAULT;
537 return 0;
538
539 case SIOCGMIIPHY:
540 case SIOCGMIIREG:
541 case SIOCSMIIREG:
542 if (ag->phy_dev == NULL)
543 break;
544
545 return phy_mii_ioctl(ag->phy_dev, data, cmd);
546
547 default:
548 break;
549 }
550
551 return -EOPNOTSUPP;
552 }
553
554 static void ag71xx_tx_packets(struct ag71xx *ag)
555 {
556 struct ag71xx_platform_data *pdata = ag71xx_get_pdata(ag);
557 struct ag71xx_ring *ring = &ag->tx_ring;
558 unsigned int sent;
559
560 DBG("%s: processing TX ring\n", ag->dev->name);
561
562 #ifdef AG71XX_NAPI_TX
563 pdata->ddr_flush();
564 #endif
565
566 sent = 0;
567 while (ring->dirty != ring->curr) {
568 unsigned int i = ring->dirty % AG71XX_TX_RING_SIZE;
569 struct ag71xx_desc *desc = &ring->descs[i];
570 struct sk_buff *skb = ring->buf[i].skb;
571
572 if (!ag71xx_desc_empty(desc))
573 break;
574
575 ag71xx_wr(ag, AG71XX_REG_TX_STATUS, TX_STATUS_PS);
576
577 ag->dev->stats.tx_bytes += skb->len;
578 ag->dev->stats.tx_packets++;
579
580 dev_kfree_skb_any(skb);
581 ring->buf[i].skb = NULL;
582
583 ring->dirty++;
584 sent++;
585 }
586
587 DBG("%s: %d packets sent out\n", ag->dev->name, sent);
588
589 if ((ring->curr - ring->dirty) < AG71XX_TX_THRES_WAKEUP)
590 netif_wake_queue(ag->dev);
591
592 }
593
594 static int ag71xx_rx_packets(struct ag71xx *ag, int limit)
595 {
596 struct net_device *dev = ag->dev;
597 struct ag71xx_ring *ring = &ag->rx_ring;
598 #ifndef AG71XX_NAPI_TX
599 struct ag71xx_platform_data *pdata = ag71xx_get_pdata(ag);
600 unsigned long flags;
601 #endif
602 int done = 0;
603
604 #ifndef AG71XX_NAPI_TX
605 spin_lock_irqsave(&ag->lock, flags);
606 pdata->ddr_flush();
607 spin_unlock_irqrestore(&ag->lock, flags);
608 #endif
609
610 DBG("%s: rx packets, limit=%d, curr=%u, dirty=%u\n",
611 dev->name, limit, ring->curr, ring->dirty);
612
613 while (done < limit) {
614 unsigned int i = ring->curr % AG71XX_RX_RING_SIZE;
615 struct ag71xx_desc *desc = &ring->descs[i];
616 struct sk_buff *skb;
617 int pktlen;
618
619 if (ag71xx_desc_empty(desc))
620 break;
621
622 if ((ring->dirty + AG71XX_RX_RING_SIZE) == ring->curr) {
623 ag71xx_assert(0);
624 break;
625 }
626
627 ag71xx_wr(ag, AG71XX_REG_RX_STATUS, RX_STATUS_PR);
628
629 skb = ring->buf[i].skb;
630 pktlen = ag71xx_desc_pktlen(desc);
631 pktlen -= ETH_FCS_LEN;
632
633 skb_put(skb, pktlen);
634
635 skb->dev = dev;
636 skb->protocol = eth_type_trans(skb, dev);
637 skb->ip_summed = CHECKSUM_NONE;
638
639 netif_receive_skb(skb);
640
641 dev->last_rx = jiffies;
642 dev->stats.rx_packets++;
643 dev->stats.rx_bytes += pktlen;
644
645 ring->buf[i].skb = NULL;
646 done++;
647
648 ring->curr++;
649 if ((ring->curr - ring->dirty) > (AG71XX_RX_RING_SIZE / 4))
650 ag71xx_ring_rx_refill(ag);
651 }
652
653 ag71xx_ring_rx_refill(ag);
654
655 DBG("%s: rx finish, curr=%u, dirty=%u, done=%d\n",
656 dev->name, ring->curr, ring->dirty, done);
657
658 return done;
659 }
660
661 static int ag71xx_poll(struct napi_struct *napi, int limit)
662 {
663 struct ag71xx *ag = container_of(napi, struct ag71xx, napi);
664 #ifdef AG71XX_NAPI_TX
665 struct ag71xx_platform_data *pdata = ag71xx_get_pdata(ag);
666 #endif
667 struct net_device *dev = ag->dev;
668 unsigned long flags;
669 u32 status;
670 int done;
671
672 #ifdef AG71XX_NAPI_TX
673 pdata->ddr_flush();
674 ag71xx_tx_packets(ag);
675 #endif
676
677 DBG("%s: processing RX ring\n", dev->name);
678 done = ag71xx_rx_packets(ag, limit);
679
680 /* TODO: add OOM handler */
681
682 status = ag71xx_rr(ag, AG71XX_REG_RX_STATUS);
683 if (unlikely(status & RX_STATUS_OF)) {
684 ag71xx_wr(ag, AG71XX_REG_RX_STATUS, RX_STATUS_OF);
685 dev->stats.rx_fifo_errors++;
686
687 /* restart RX */
688 ag71xx_wr(ag, AG71XX_REG_RX_CTRL, RX_CTRL_RXE);
689 }
690
691 if ((done < limit) && ((status & RX_STATUS_PR) == 0)) {
692 DBG("%s: disable polling mode, done=%d, status=%x\n",
693 dev->name, done, status);
694
695 netif_rx_complete(dev, napi);
696
697 /* enable interrupts */
698 spin_lock_irqsave(&ag->lock, flags);
699 ag71xx_int_enable(ag, AG71XX_INT_POLL);
700 spin_unlock_irqrestore(&ag->lock, flags);
701 return 0;
702 }
703
704 DBG("%s: stay in polling mode, done=%d, status=%x\n",
705 dev->name, done, status);
706 return 1;
707 }
708
709 static irqreturn_t ag71xx_interrupt(int irq, void *dev_id)
710 {
711 struct net_device *dev = dev_id;
712 struct ag71xx *ag = netdev_priv(dev);
713 u32 status;
714
715 status = ag71xx_rr(ag, AG71XX_REG_INT_STATUS);
716 ag71xx_dump_intr(ag, "raw", status);
717 status &= ag71xx_rr(ag, AG71XX_REG_INT_ENABLE);
718 ag71xx_dump_intr(ag, "masked", status);
719
720 if (unlikely(!status))
721 return IRQ_NONE;
722
723 if (unlikely(status & AG71XX_INT_ERR)) {
724 if (status & AG71XX_INT_TX_BE) {
725 ag71xx_wr(ag, AG71XX_REG_TX_STATUS, TX_STATUS_BE);
726 dev_err(&dev->dev, "TX BUS error\n");
727 }
728 if (status & AG71XX_INT_RX_BE) {
729 ag71xx_wr(ag, AG71XX_REG_RX_STATUS, RX_STATUS_BE);
730 dev_err(&dev->dev, "RX BUS error\n");
731 }
732 }
733
734 #if 0
735 if (unlikely(status & AG71XX_INT_TX_UR)) {
736 ag71xx_wr(ag, AG71XX_REG_TX_STATUS, TX_STATUS_UR);
737 DBG("%s: TX underrun\n", dev->name);
738 }
739 #endif
740
741 #ifndef AG71XX_NAPI_TX
742 if (likely(status & AG71XX_INT_TX_PS))
743 ag71xx_tx_packets(ag);
744 #endif
745
746 if (likely(status & AG71XX_INT_POLL)) {
747 ag71xx_int_disable(ag, AG71XX_INT_POLL);
748 DBG("%s: enable polling mode\n", dev->name);
749 netif_rx_schedule(dev, &ag->napi);
750 }
751
752 return IRQ_HANDLED;
753 }
754
755 static void ag71xx_set_multicast_list(struct net_device *dev)
756 {
757 /* TODO */
758 }
759
760 static int __init ag71xx_probe(struct platform_device *pdev)
761 {
762 struct net_device *dev;
763 struct resource *res;
764 struct ag71xx *ag;
765 struct ag71xx_platform_data *pdata;
766 int err;
767
768 pdata = pdev->dev.platform_data;
769 if (!pdata) {
770 dev_err(&pdev->dev, "no platform data specified\n");
771 err = -ENXIO;
772 goto err_out;
773 }
774
775 dev = alloc_etherdev(sizeof(*ag));
776 if (!dev) {
777 dev_err(&pdev->dev, "alloc_etherdev failed\n");
778 err = -ENOMEM;
779 goto err_out;
780 }
781
782 SET_NETDEV_DEV(dev, &pdev->dev);
783
784 ag = netdev_priv(dev);
785 ag->pdev = pdev;
786 ag->dev = dev;
787 ag->mii_bus = &ag71xx_mdio_bus->mii_bus;
788 ag->msg_enable = netif_msg_init(ag71xx_debug,
789 AG71XX_DEFAULT_MSG_ENABLE);
790 spin_lock_init(&ag->lock);
791
792 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mac_base");
793 if (!res) {
794 dev_err(&pdev->dev, "no mac_base resource found\n");
795 err = -ENXIO;
796 goto err_out;
797 }
798
799 ag->mac_base = ioremap_nocache(res->start, res->end - res->start + 1);
800 if (!ag->mac_base) {
801 dev_err(&pdev->dev, "unable to ioremap mac_base\n");
802 err = -ENOMEM;
803 goto err_free_dev;
804 }
805
806 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mac_base2");
807 if (!res) {
808 dev_err(&pdev->dev, "no mac_base2 resource found\n");
809 err = -ENXIO;
810 goto err_unmap_base1;
811 }
812
813 ag->mac_base2 = ioremap_nocache(res->start, res->end - res->start + 1);
814 if (!ag->mac_base) {
815 dev_err(&pdev->dev, "unable to ioremap mac_base2\n");
816 err = -ENOMEM;
817 goto err_unmap_base1;
818 }
819
820 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mii_ctrl");
821 if (!res) {
822 dev_err(&pdev->dev, "no mii_ctrl resource found\n");
823 err = -ENXIO;
824 goto err_unmap_base2;
825 }
826
827 ag->mii_ctrl = ioremap_nocache(res->start, res->end - res->start + 1);
828 if (!ag->mii_ctrl) {
829 dev_err(&pdev->dev, "unable to ioremap mii_ctrl\n");
830 err = -ENOMEM;
831 goto err_unmap_base2;
832 }
833
834 dev->irq = platform_get_irq(pdev, 0);
835 err = request_irq(dev->irq, ag71xx_interrupt,
836 IRQF_DISABLED | IRQF_SAMPLE_RANDOM,
837 dev->name, dev);
838 if (err) {
839 dev_err(&pdev->dev, "unable to request IRQ %d\n", dev->irq);
840 goto err_unmap_mii_ctrl;
841 }
842
843 dev->base_addr = (unsigned long)ag->mac_base;
844 dev->open = ag71xx_open;
845 dev->stop = ag71xx_stop;
846 dev->hard_start_xmit = ag71xx_hard_start_xmit;
847 dev->set_multicast_list = ag71xx_set_multicast_list;
848 dev->do_ioctl = ag71xx_do_ioctl;
849 dev->ethtool_ops = &ag71xx_ethtool_ops;
850
851 netif_napi_add(dev, &ag->napi, ag71xx_poll, AG71XX_NAPI_WEIGHT);
852
853 if (is_valid_ether_addr(pdata->mac_addr))
854 memcpy(dev->dev_addr, pdata->mac_addr, ETH_ALEN);
855 else {
856 dev->dev_addr[0] = 0xde;
857 dev->dev_addr[1] = 0xad;
858 get_random_bytes(&dev->dev_addr[2], 3);
859 dev->dev_addr[5] = pdev->id & 0xff;
860 }
861
862 err = register_netdev(dev);
863 if (err) {
864 dev_err(&pdev->dev, "unable to register net device\n");
865 goto err_free_irq;
866 }
867
868 printk(KERN_INFO "%s: Atheros AG71xx at 0x%08lx, irq %d\n",
869 dev->name, dev->base_addr, dev->irq);
870
871 ag71xx_dump_regs(ag);
872
873 ag71xx_hw_init(ag);
874
875 ag71xx_dump_regs(ag);
876
877 /* Reset the mdio bus explicitly */
878 if (ag->mii_bus) {
879 mutex_lock(&ag->mii_bus->mdio_lock);
880 ag->mii_bus->reset(ag->mii_bus);
881 mutex_unlock(&ag->mii_bus->mdio_lock);
882 }
883
884 err = ag71xx_phy_connect(ag);
885 if (err)
886 goto err_unregister_netdev;
887
888 platform_set_drvdata(pdev, dev);
889
890 return 0;
891
892 err_unregister_netdev:
893 unregister_netdev(dev);
894 err_free_irq:
895 free_irq(dev->irq, dev);
896 err_unmap_mii_ctrl:
897 iounmap(ag->mii_ctrl);
898 err_unmap_base2:
899 iounmap(ag->mac_base2);
900 err_unmap_base1:
901 iounmap(ag->mac_base);
902 err_free_dev:
903 kfree(dev);
904 err_out:
905 platform_set_drvdata(pdev, NULL);
906 return err;
907 }
908
909 static int __exit ag71xx_remove(struct platform_device *pdev)
910 {
911 struct net_device *dev = platform_get_drvdata(pdev);
912
913 if (dev) {
914 struct ag71xx *ag = netdev_priv(dev);
915
916 ag71xx_phy_disconnect(ag);
917 unregister_netdev(dev);
918 free_irq(dev->irq, dev);
919 iounmap(ag->mii_ctrl);
920 iounmap(ag->mac_base2);
921 iounmap(ag->mac_base);
922 kfree(dev);
923 platform_set_drvdata(pdev, NULL);
924 }
925
926 return 0;
927 }
928
929 static struct platform_driver ag71xx_driver = {
930 .probe = ag71xx_probe,
931 .remove = __exit_p(ag71xx_remove),
932 .driver = {
933 .name = AG71XX_DRV_NAME,
934 }
935 };
936
937 static int __init ag71xx_module_init(void)
938 {
939 int ret;
940
941 ret = ag71xx_mdio_driver_init();
942 if (ret)
943 goto err_out;
944
945 ret = platform_driver_register(&ag71xx_driver);
946 if (ret)
947 goto err_mdio_exit;
948
949 return 0;
950
951 err_mdio_exit:
952 ag71xx_mdio_driver_exit();
953 err_out:
954 return ret;
955 }
956
957 static void __exit ag71xx_module_exit(void)
958 {
959 platform_driver_unregister(&ag71xx_driver);
960 ag71xx_mdio_driver_exit();
961 }
962
963 module_init(ag71xx_module_init);
964 module_exit(ag71xx_module_exit);
965
966 MODULE_VERSION(AG71XX_DRV_VERSION);
967 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
968 MODULE_AUTHOR("Imre Kaloz <kaloz@openwrt.org>");
969 MODULE_LICENSE("GPL v2");
970 MODULE_ALIAS("platform:" AG71XX_DRV_NAME);