1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#include <linux/dma-mapping.h>
26#include <linux/module.h>
27#include <linux/uaccess.h>
28#include <linux/init.h>
29#include <linux/interrupt.h>
30#include <linux/netdevice.h>
31#include <linux/etherdevice.h>
32#include <linux/ethtool.h>
33#include <linux/skbuff.h>
34#include <linux/io.h>
35#include <linux/crc32.h>
36#include <linux/mii.h>
37#include <linux/of_device.h>
38#include <linux/of_platform.h>
39#include <linux/slab.h>
40#include <asm/cacheflush.h>
41#include <asm/byteorder.h>
42
43#ifdef CONFIG_SPARC
44#include <asm/idprom.h>
45#endif
46
47#include "greth.h"
48
49#define GRETH_DEF_MSG_ENABLE \
50 (NETIF_MSG_DRV | \
51 NETIF_MSG_PROBE | \
52 NETIF_MSG_LINK | \
53 NETIF_MSG_IFDOWN | \
54 NETIF_MSG_IFUP | \
55 NETIF_MSG_RX_ERR | \
56 NETIF_MSG_TX_ERR)
57
58static int greth_debug = -1;
59module_param(greth_debug, int, 0);
60MODULE_PARM_DESC(greth_debug, "GRETH bitmapped debugging message enable value");
61
62
63static int macaddr[6];
64module_param_array(macaddr, int, NULL, 0);
65MODULE_PARM_DESC(macaddr, "GRETH Ethernet MAC address");
66
67static int greth_edcl = 1;
68module_param(greth_edcl, int, 0);
69MODULE_PARM_DESC(greth_edcl, "GRETH EDCL usage indicator. Set to 1 if EDCL is used.");
70
71static int greth_open(struct net_device *dev);
72static netdev_tx_t greth_start_xmit(struct sk_buff *skb,
73 struct net_device *dev);
74static netdev_tx_t greth_start_xmit_gbit(struct sk_buff *skb,
75 struct net_device *dev);
76static int greth_rx(struct net_device *dev, int limit);
77static int greth_rx_gbit(struct net_device *dev, int limit);
78static void greth_clean_tx(struct net_device *dev);
79static void greth_clean_tx_gbit(struct net_device *dev);
80static irqreturn_t greth_interrupt(int irq, void *dev_id);
81static int greth_close(struct net_device *dev);
82static int greth_set_mac_add(struct net_device *dev, void *p);
83static void greth_set_multicast_list(struct net_device *dev);
84
85#define GRETH_REGLOAD(a) (be32_to_cpu(__raw_readl(&(a))))
86#define GRETH_REGSAVE(a, v) (__raw_writel(cpu_to_be32(v), &(a)))
87#define GRETH_REGORIN(a, v) (GRETH_REGSAVE(a, (GRETH_REGLOAD(a) | (v))))
88#define GRETH_REGANDIN(a, v) (GRETH_REGSAVE(a, (GRETH_REGLOAD(a) & (v))))
89
90#define NEXT_TX(N) (((N) + 1) & GRETH_TXBD_NUM_MASK)
91#define SKIP_TX(N, C) (((N) + C) & GRETH_TXBD_NUM_MASK)
92#define NEXT_RX(N) (((N) + 1) & GRETH_RXBD_NUM_MASK)
93
94static void greth_print_rx_packet(void *addr, int len)
95{
96 print_hex_dump(KERN_DEBUG, "RX: ", DUMP_PREFIX_OFFSET, 16, 1,
97 addr, len, true);
98}
99
100static void greth_print_tx_packet(struct sk_buff *skb)
101{
102 int i;
103 int length;
104
105 if (skb_shinfo(skb)->nr_frags == 0)
106 length = skb->len;
107 else
108 length = skb_headlen(skb);
109
110 print_hex_dump(KERN_DEBUG, "TX: ", DUMP_PREFIX_OFFSET, 16, 1,
111 skb->data, length, true);
112
113 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
114
115 print_hex_dump(KERN_DEBUG, "TX: ", DUMP_PREFIX_OFFSET, 16, 1,
116 skb_frag_address(&skb_shinfo(skb)->frags[i]),
117 skb_shinfo(skb)->frags[i].size, true);
118 }
119}
120
121static inline void greth_enable_tx(struct greth_private *greth)
122{
123 wmb();
124 GRETH_REGORIN(greth->regs->control, GRETH_TXEN);
125}
126
127static inline void greth_disable_tx(struct greth_private *greth)
128{
129 GRETH_REGANDIN(greth->regs->control, ~GRETH_TXEN);
130}
131
132static inline void greth_enable_rx(struct greth_private *greth)
133{
134 wmb();
135 GRETH_REGORIN(greth->regs->control, GRETH_RXEN);
136}
137
138static inline void greth_disable_rx(struct greth_private *greth)
139{
140 GRETH_REGANDIN(greth->regs->control, ~GRETH_RXEN);
141}
142
143static inline void greth_enable_irqs(struct greth_private *greth)
144{
145 GRETH_REGORIN(greth->regs->control, GRETH_RXI | GRETH_TXI);
146}
147
148static inline void greth_disable_irqs(struct greth_private *greth)
149{
150 GRETH_REGANDIN(greth->regs->control, ~(GRETH_RXI|GRETH_TXI));
151}
152
153static inline void greth_write_bd(u32 *bd, u32 val)
154{
155 __raw_writel(cpu_to_be32(val), bd);
156}
157
158static inline u32 greth_read_bd(u32 *bd)
159{
160 return be32_to_cpu(__raw_readl(bd));
161}
162
163static void greth_clean_rings(struct greth_private *greth)
164{
165 int i;
166 struct greth_bd *rx_bdp = greth->rx_bd_base;
167 struct greth_bd *tx_bdp = greth->tx_bd_base;
168
169 if (greth->gbit_mac) {
170
171
172 for (i = 0; i < GRETH_RXBD_NUM; i++, rx_bdp++) {
173 if (greth->rx_skbuff[i] != NULL) {
174 dev_kfree_skb(greth->rx_skbuff[i]);
175 dma_unmap_single(greth->dev,
176 greth_read_bd(&rx_bdp->addr),
177 MAX_FRAME_SIZE+NET_IP_ALIGN,
178 DMA_FROM_DEVICE);
179 }
180 }
181
182
183 while (greth->tx_free < GRETH_TXBD_NUM) {
184
185 struct sk_buff *skb = greth->tx_skbuff[greth->tx_last];
186 int nr_frags = skb_shinfo(skb)->nr_frags;
187 tx_bdp = greth->tx_bd_base + greth->tx_last;
188 greth->tx_last = NEXT_TX(greth->tx_last);
189
190 dma_unmap_single(greth->dev,
191 greth_read_bd(&tx_bdp->addr),
192 skb_headlen(skb),
193 DMA_TO_DEVICE);
194
195 for (i = 0; i < nr_frags; i++) {
196 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
197 tx_bdp = greth->tx_bd_base + greth->tx_last;
198
199 dma_unmap_page(greth->dev,
200 greth_read_bd(&tx_bdp->addr),
201 skb_frag_size(frag),
202 DMA_TO_DEVICE);
203
204 greth->tx_last = NEXT_TX(greth->tx_last);
205 }
206 greth->tx_free += nr_frags+1;
207 dev_kfree_skb(skb);
208 }
209
210
211 } else {
212
213 for (i = 0; i < GRETH_RXBD_NUM; i++, rx_bdp++) {
214 kfree(greth->rx_bufs[i]);
215 dma_unmap_single(greth->dev,
216 greth_read_bd(&rx_bdp->addr),
217 MAX_FRAME_SIZE,
218 DMA_FROM_DEVICE);
219 }
220 for (i = 0; i < GRETH_TXBD_NUM; i++, tx_bdp++) {
221 kfree(greth->tx_bufs[i]);
222 dma_unmap_single(greth->dev,
223 greth_read_bd(&tx_bdp->addr),
224 MAX_FRAME_SIZE,
225 DMA_TO_DEVICE);
226 }
227 }
228}
229
230static int greth_init_rings(struct greth_private *greth)
231{
232 struct sk_buff *skb;
233 struct greth_bd *rx_bd, *tx_bd;
234 u32 dma_addr;
235 int i;
236
237 rx_bd = greth->rx_bd_base;
238 tx_bd = greth->tx_bd_base;
239
240
241 if (greth->gbit_mac) {
242
243 for (i = 0; i < GRETH_RXBD_NUM; i++) {
244 skb = netdev_alloc_skb(greth->netdev, MAX_FRAME_SIZE+NET_IP_ALIGN);
245 if (skb == NULL) {
246 if (netif_msg_ifup(greth))
247 dev_err(greth->dev, "Error allocating DMA ring.\n");
248 goto cleanup;
249 }
250 skb_reserve(skb, NET_IP_ALIGN);
251 dma_addr = dma_map_single(greth->dev,
252 skb->data,
253 MAX_FRAME_SIZE+NET_IP_ALIGN,
254 DMA_FROM_DEVICE);
255
256 if (dma_mapping_error(greth->dev, dma_addr)) {
257 if (netif_msg_ifup(greth))
258 dev_err(greth->dev, "Could not create initial DMA mapping\n");
259 goto cleanup;
260 }
261 greth->rx_skbuff[i] = skb;
262 greth_write_bd(&rx_bd[i].addr, dma_addr);
263 greth_write_bd(&rx_bd[i].stat, GRETH_BD_EN | GRETH_BD_IE);
264 }
265
266 } else {
267
268
269 for (i = 0; i < GRETH_RXBD_NUM; i++) {
270
271 greth->rx_bufs[i] = kmalloc(MAX_FRAME_SIZE, GFP_KERNEL);
272
273 if (greth->rx_bufs[i] == NULL) {
274 if (netif_msg_ifup(greth))
275 dev_err(greth->dev, "Error allocating DMA ring.\n");
276 goto cleanup;
277 }
278
279 dma_addr = dma_map_single(greth->dev,
280 greth->rx_bufs[i],
281 MAX_FRAME_SIZE,
282 DMA_FROM_DEVICE);
283
284 if (dma_mapping_error(greth->dev, dma_addr)) {
285 if (netif_msg_ifup(greth))
286 dev_err(greth->dev, "Could not create initial DMA mapping\n");
287 goto cleanup;
288 }
289 greth_write_bd(&rx_bd[i].addr, dma_addr);
290 greth_write_bd(&rx_bd[i].stat, GRETH_BD_EN | GRETH_BD_IE);
291 }
292 for (i = 0; i < GRETH_TXBD_NUM; i++) {
293
294 greth->tx_bufs[i] = kmalloc(MAX_FRAME_SIZE, GFP_KERNEL);
295
296 if (greth->tx_bufs[i] == NULL) {
297 if (netif_msg_ifup(greth))
298 dev_err(greth->dev, "Error allocating DMA ring.\n");
299 goto cleanup;
300 }
301
302 dma_addr = dma_map_single(greth->dev,
303 greth->tx_bufs[i],
304 MAX_FRAME_SIZE,
305 DMA_TO_DEVICE);
306
307 if (dma_mapping_error(greth->dev, dma_addr)) {
308 if (netif_msg_ifup(greth))
309 dev_err(greth->dev, "Could not create initial DMA mapping\n");
310 goto cleanup;
311 }
312 greth_write_bd(&tx_bd[i].addr, dma_addr);
313 greth_write_bd(&tx_bd[i].stat, 0);
314 }
315 }
316 greth_write_bd(&rx_bd[GRETH_RXBD_NUM - 1].stat,
317 greth_read_bd(&rx_bd[GRETH_RXBD_NUM - 1].stat) | GRETH_BD_WR);
318
319
320 greth->rx_cur = 0;
321 greth->tx_next = 0;
322 greth->tx_last = 0;
323 greth->tx_free = GRETH_TXBD_NUM;
324
325
326 GRETH_REGSAVE(greth->regs->tx_desc_p, greth->tx_bd_base_phys);
327 GRETH_REGSAVE(greth->regs->rx_desc_p, greth->rx_bd_base_phys);
328
329 return 0;
330
331cleanup:
332 greth_clean_rings(greth);
333 return -ENOMEM;
334}
335
336static int greth_open(struct net_device *dev)
337{
338 struct greth_private *greth = netdev_priv(dev);
339 int err;
340
341 err = greth_init_rings(greth);
342 if (err) {
343 if (netif_msg_ifup(greth))
344 dev_err(&dev->dev, "Could not allocate memory for DMA rings\n");
345 return err;
346 }
347
348 err = request_irq(greth->irq, greth_interrupt, 0, "eth", (void *) dev);
349 if (err) {
350 if (netif_msg_ifup(greth))
351 dev_err(&dev->dev, "Could not allocate interrupt %d\n", dev->irq);
352 greth_clean_rings(greth);
353 return err;
354 }
355
356 if (netif_msg_ifup(greth))
357 dev_dbg(&dev->dev, " starting queue\n");
358 netif_start_queue(dev);
359
360 GRETH_REGSAVE(greth->regs->status, 0xFF);
361
362 napi_enable(&greth->napi);
363
364 greth_enable_irqs(greth);
365 greth_enable_tx(greth);
366 greth_enable_rx(greth);
367 return 0;
368
369}
370
371static int greth_close(struct net_device *dev)
372{
373 struct greth_private *greth = netdev_priv(dev);
374
375 napi_disable(&greth->napi);
376
377 greth_disable_irqs(greth);
378 greth_disable_tx(greth);
379 greth_disable_rx(greth);
380
381 netif_stop_queue(dev);
382
383 free_irq(greth->irq, (void *) dev);
384
385 greth_clean_rings(greth);
386
387 return 0;
388}
389
390static netdev_tx_t
391greth_start_xmit(struct sk_buff *skb, struct net_device *dev)
392{
393 struct greth_private *greth = netdev_priv(dev);
394 struct greth_bd *bdp;
395 int err = NETDEV_TX_OK;
396 u32 status, dma_addr, ctrl;
397 unsigned long flags;
398
399
400 greth_clean_tx(greth->netdev);
401
402 if (unlikely(greth->tx_free <= 0)) {
403 spin_lock_irqsave(&greth->devlock, flags);
404 ctrl = GRETH_REGLOAD(greth->regs->control);
405
406 if (ctrl & GRETH_RXI)
407 GRETH_REGSAVE(greth->regs->control, ctrl | GRETH_TXI);
408 netif_stop_queue(dev);
409 spin_unlock_irqrestore(&greth->devlock, flags);
410 return NETDEV_TX_BUSY;
411 }
412
413 if (netif_msg_pktdata(greth))
414 greth_print_tx_packet(skb);
415
416
417 if (unlikely(skb->len > MAX_FRAME_SIZE)) {
418 dev->stats.tx_errors++;
419 goto out;
420 }
421
422 bdp = greth->tx_bd_base + greth->tx_next;
423 dma_addr = greth_read_bd(&bdp->addr);
424
425 memcpy((unsigned char *) phys_to_virt(dma_addr), skb->data, skb->len);
426
427 dma_sync_single_for_device(greth->dev, dma_addr, skb->len, DMA_TO_DEVICE);
428
429 status = GRETH_BD_EN | GRETH_BD_IE | (skb->len & GRETH_BD_LEN);
430 greth->tx_bufs_length[greth->tx_next] = skb->len & GRETH_BD_LEN;
431
432
433 if (greth->tx_next == GRETH_TXBD_NUM_MASK) {
434 status |= GRETH_BD_WR;
435 }
436
437 greth->tx_next = NEXT_TX(greth->tx_next);
438 greth->tx_free--;
439
440
441 greth_write_bd(&bdp->stat, status);
442 spin_lock_irqsave(&greth->devlock, flags);
443 greth_enable_tx(greth);
444 spin_unlock_irqrestore(&greth->devlock, flags);
445
446out:
447 dev_kfree_skb(skb);
448 return err;
449}
450
451
452static netdev_tx_t
453greth_start_xmit_gbit(struct sk_buff *skb, struct net_device *dev)
454{
455 struct greth_private *greth = netdev_priv(dev);
456 struct greth_bd *bdp;
457 u32 status = 0, dma_addr, ctrl;
458 int curr_tx, nr_frags, i, err = NETDEV_TX_OK;
459 unsigned long flags;
460
461 nr_frags = skb_shinfo(skb)->nr_frags;
462
463
464 greth_clean_tx_gbit(dev);
465
466 if (greth->tx_free < nr_frags + 1) {
467 spin_lock_irqsave(&greth->devlock, flags);
468 ctrl = GRETH_REGLOAD(greth->regs->control);
469
470 if (ctrl & GRETH_RXI)
471 GRETH_REGSAVE(greth->regs->control, ctrl | GRETH_TXI);
472 netif_stop_queue(dev);
473 spin_unlock_irqrestore(&greth->devlock, flags);
474 err = NETDEV_TX_BUSY;
475 goto out;
476 }
477
478 if (netif_msg_pktdata(greth))
479 greth_print_tx_packet(skb);
480
481 if (unlikely(skb->len > MAX_FRAME_SIZE)) {
482 dev->stats.tx_errors++;
483 goto out;
484 }
485
486
487 greth->tx_skbuff[greth->tx_next] = skb;
488
489
490 if (nr_frags != 0)
491 status = GRETH_TXBD_MORE;
492
493 if (skb->ip_summed == CHECKSUM_PARTIAL)
494 status |= GRETH_TXBD_CSALL;
495 status |= skb_headlen(skb) & GRETH_BD_LEN;
496 if (greth->tx_next == GRETH_TXBD_NUM_MASK)
497 status |= GRETH_BD_WR;
498
499
500 bdp = greth->tx_bd_base + greth->tx_next;
501 greth_write_bd(&bdp->stat, status);
502 dma_addr = dma_map_single(greth->dev, skb->data, skb_headlen(skb), DMA_TO_DEVICE);
503
504 if (unlikely(dma_mapping_error(greth->dev, dma_addr)))
505 goto map_error;
506
507 greth_write_bd(&bdp->addr, dma_addr);
508
509 curr_tx = NEXT_TX(greth->tx_next);
510
511
512 for (i = 0; i < nr_frags; i++) {
513 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
514 greth->tx_skbuff[curr_tx] = NULL;
515 bdp = greth->tx_bd_base + curr_tx;
516
517 status = GRETH_BD_EN;
518 if (skb->ip_summed == CHECKSUM_PARTIAL)
519 status |= GRETH_TXBD_CSALL;
520 status |= skb_frag_size(frag) & GRETH_BD_LEN;
521
522
523 if (curr_tx == GRETH_TXBD_NUM_MASK)
524 status |= GRETH_BD_WR;
525
526
527 if (i < nr_frags - 1)
528 status |= GRETH_TXBD_MORE;
529 else
530 status |= GRETH_BD_IE;
531
532 greth_write_bd(&bdp->stat, status);
533
534 dma_addr = skb_frag_dma_map(greth->dev, frag, 0, skb_frag_size(frag),
535 DMA_TO_DEVICE);
536
537 if (unlikely(dma_mapping_error(greth->dev, dma_addr)))
538 goto frag_map_error;
539
540 greth_write_bd(&bdp->addr, dma_addr);
541
542 curr_tx = NEXT_TX(curr_tx);
543 }
544
545 wmb();
546
547
548 bdp = greth->tx_bd_base + greth->tx_next;
549 greth_write_bd(&bdp->stat, greth_read_bd(&bdp->stat) | GRETH_BD_EN);
550 greth->tx_next = curr_tx;
551 greth->tx_free -= nr_frags + 1;
552
553 wmb();
554
555 spin_lock_irqsave(&greth->devlock, flags);
556 greth_enable_tx(greth);
557 spin_unlock_irqrestore(&greth->devlock, flags);
558
559 return NETDEV_TX_OK;
560
561frag_map_error:
562
563 for (i = 0; greth->tx_next + i != curr_tx; i++) {
564 bdp = greth->tx_bd_base + greth->tx_next + i;
565 dma_unmap_single(greth->dev,
566 greth_read_bd(&bdp->addr),
567 greth_read_bd(&bdp->stat) & GRETH_BD_LEN,
568 DMA_TO_DEVICE);
569 greth_write_bd(&bdp->stat, 0);
570 }
571map_error:
572 if (net_ratelimit())
573 dev_warn(greth->dev, "Could not create TX DMA mapping\n");
574 dev_kfree_skb(skb);
575out:
576 return err;
577}
578
579static irqreturn_t greth_interrupt(int irq, void *dev_id)
580{
581 struct net_device *dev = dev_id;
582 struct greth_private *greth;
583 u32 status, ctrl;
584 irqreturn_t retval = IRQ_NONE;
585
586 greth = netdev_priv(dev);
587
588 spin_lock(&greth->devlock);
589
590
591 status = GRETH_REGLOAD(greth->regs->status);
592
593
594
595
596
597 ctrl = GRETH_REGLOAD(greth->regs->control);
598
599
600 if (((status & (GRETH_INT_RE | GRETH_INT_RX)) && (ctrl & GRETH_RXI)) ||
601 ((status & (GRETH_INT_TE | GRETH_INT_TX)) && (ctrl & GRETH_TXI))) {
602 retval = IRQ_HANDLED;
603
604
605 greth_disable_irqs(greth);
606 napi_schedule(&greth->napi);
607 }
608
609 mmiowb();
610 spin_unlock(&greth->devlock);
611
612 return retval;
613}
614
615static void greth_clean_tx(struct net_device *dev)
616{
617 struct greth_private *greth;
618 struct greth_bd *bdp;
619 u32 stat;
620
621 greth = netdev_priv(dev);
622
623 while (1) {
624 bdp = greth->tx_bd_base + greth->tx_last;
625 GRETH_REGSAVE(greth->regs->status, GRETH_INT_TE | GRETH_INT_TX);
626 mb();
627 stat = greth_read_bd(&bdp->stat);
628
629 if (unlikely(stat & GRETH_BD_EN))
630 break;
631
632 if (greth->tx_free == GRETH_TXBD_NUM)
633 break;
634
635
636 if (unlikely(stat & GRETH_TXBD_STATUS)) {
637 dev->stats.tx_errors++;
638 if (stat & GRETH_TXBD_ERR_AL)
639 dev->stats.tx_aborted_errors++;
640 if (stat & GRETH_TXBD_ERR_UE)
641 dev->stats.tx_fifo_errors++;
642 }
643 dev->stats.tx_packets++;
644 dev->stats.tx_bytes += greth->tx_bufs_length[greth->tx_last];
645 greth->tx_last = NEXT_TX(greth->tx_last);
646 greth->tx_free++;
647 }
648
649 if (greth->tx_free > 0) {
650 netif_wake_queue(dev);
651 }
652
653}
654
655static inline void greth_update_tx_stats(struct net_device *dev, u32 stat)
656{
657
658 if (unlikely(stat & GRETH_TXBD_STATUS)) {
659 dev->stats.tx_errors++;
660 if (stat & GRETH_TXBD_ERR_AL)
661 dev->stats.tx_aborted_errors++;
662 if (stat & GRETH_TXBD_ERR_UE)
663 dev->stats.tx_fifo_errors++;
664 if (stat & GRETH_TXBD_ERR_LC)
665 dev->stats.tx_aborted_errors++;
666 }
667 dev->stats.tx_packets++;
668}
669
670static void greth_clean_tx_gbit(struct net_device *dev)
671{
672 struct greth_private *greth;
673 struct greth_bd *bdp, *bdp_last_frag;
674 struct sk_buff *skb;
675 u32 stat;
676 int nr_frags, i;
677
678 greth = netdev_priv(dev);
679
680 while (greth->tx_free < GRETH_TXBD_NUM) {
681
682 skb = greth->tx_skbuff[greth->tx_last];
683
684 nr_frags = skb_shinfo(skb)->nr_frags;
685
686
687 bdp_last_frag = greth->tx_bd_base + SKIP_TX(greth->tx_last, nr_frags);
688
689 GRETH_REGSAVE(greth->regs->status, GRETH_INT_TE | GRETH_INT_TX);
690 mb();
691 stat = greth_read_bd(&bdp_last_frag->stat);
692
693 if (stat & GRETH_BD_EN)
694 break;
695
696 greth->tx_skbuff[greth->tx_last] = NULL;
697
698 greth_update_tx_stats(dev, stat);
699 dev->stats.tx_bytes += skb->len;
700
701 bdp = greth->tx_bd_base + greth->tx_last;
702
703 greth->tx_last = NEXT_TX(greth->tx_last);
704
705 dma_unmap_single(greth->dev,
706 greth_read_bd(&bdp->addr),
707 skb_headlen(skb),
708 DMA_TO_DEVICE);
709
710 for (i = 0; i < nr_frags; i++) {
711 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
712 bdp = greth->tx_bd_base + greth->tx_last;
713
714 dma_unmap_page(greth->dev,
715 greth_read_bd(&bdp->addr),
716 skb_frag_size(frag),
717 DMA_TO_DEVICE);
718
719 greth->tx_last = NEXT_TX(greth->tx_last);
720 }
721 greth->tx_free += nr_frags+1;
722 dev_kfree_skb(skb);
723 }
724
725 if (netif_queue_stopped(dev) && (greth->tx_free > (MAX_SKB_FRAGS+1)))
726 netif_wake_queue(dev);
727}
728
729static int greth_rx(struct net_device *dev, int limit)
730{
731 struct greth_private *greth;
732 struct greth_bd *bdp;
733 struct sk_buff *skb;
734 int pkt_len;
735 int bad, count;
736 u32 status, dma_addr;
737 unsigned long flags;
738
739 greth = netdev_priv(dev);
740
741 for (count = 0; count < limit; ++count) {
742
743 bdp = greth->rx_bd_base + greth->rx_cur;
744 GRETH_REGSAVE(greth->regs->status, GRETH_INT_RE | GRETH_INT_RX);
745 mb();
746 status = greth_read_bd(&bdp->stat);
747
748 if (unlikely(status & GRETH_BD_EN)) {
749 break;
750 }
751
752 dma_addr = greth_read_bd(&bdp->addr);
753 bad = 0;
754
755
756 if (unlikely(status & GRETH_RXBD_STATUS)) {
757 if (status & GRETH_RXBD_ERR_FT) {
758 dev->stats.rx_length_errors++;
759 bad = 1;
760 }
761 if (status & (GRETH_RXBD_ERR_AE | GRETH_RXBD_ERR_OE)) {
762 dev->stats.rx_frame_errors++;
763 bad = 1;
764 }
765 if (status & GRETH_RXBD_ERR_CRC) {
766 dev->stats.rx_crc_errors++;
767 bad = 1;
768 }
769 }
770 if (unlikely(bad)) {
771 dev->stats.rx_errors++;
772
773 } else {
774
775 pkt_len = status & GRETH_BD_LEN;
776
777 skb = netdev_alloc_skb(dev, pkt_len + NET_IP_ALIGN);
778
779 if (unlikely(skb == NULL)) {
780
781 if (net_ratelimit())
782 dev_warn(&dev->dev, "low on memory - " "packet dropped\n");
783
784 dev->stats.rx_dropped++;
785
786 } else {
787 skb_reserve(skb, NET_IP_ALIGN);
788
789 dma_sync_single_for_cpu(greth->dev,
790 dma_addr,
791 pkt_len,
792 DMA_FROM_DEVICE);
793
794 if (netif_msg_pktdata(greth))
795 greth_print_rx_packet(phys_to_virt(dma_addr), pkt_len);
796
797 memcpy(skb_put(skb, pkt_len), phys_to_virt(dma_addr), pkt_len);
798
799 skb->protocol = eth_type_trans(skb, dev);
800 dev->stats.rx_bytes += pkt_len;
801 dev->stats.rx_packets++;
802 netif_receive_skb(skb);
803 }
804 }
805
806 status = GRETH_BD_EN | GRETH_BD_IE;
807 if (greth->rx_cur == GRETH_RXBD_NUM_MASK) {
808 status |= GRETH_BD_WR;
809 }
810
811 wmb();
812 greth_write_bd(&bdp->stat, status);
813
814 dma_sync_single_for_device(greth->dev, dma_addr, MAX_FRAME_SIZE, DMA_FROM_DEVICE);
815
816 spin_lock_irqsave(&greth->devlock, flags);
817 greth_enable_rx(greth);
818 spin_unlock_irqrestore(&greth->devlock, flags);
819
820 greth->rx_cur = NEXT_RX(greth->rx_cur);
821 }
822
823 return count;
824}
825
826static inline int hw_checksummed(u32 status)
827{
828
829 if (status & GRETH_RXBD_IP_FRAG)
830 return 0;
831
832 if (status & GRETH_RXBD_IP && status & GRETH_RXBD_IP_CSERR)
833 return 0;
834
835 if (status & GRETH_RXBD_UDP && status & GRETH_RXBD_UDP_CSERR)
836 return 0;
837
838 if (status & GRETH_RXBD_TCP && status & GRETH_RXBD_TCP_CSERR)
839 return 0;
840
841 return 1;
842}
843
844static int greth_rx_gbit(struct net_device *dev, int limit)
845{
846 struct greth_private *greth;
847 struct greth_bd *bdp;
848 struct sk_buff *skb, *newskb;
849 int pkt_len;
850 int bad, count = 0;
851 u32 status, dma_addr;
852 unsigned long flags;
853
854 greth = netdev_priv(dev);
855
856 for (count = 0; count < limit; ++count) {
857
858 bdp = greth->rx_bd_base + greth->rx_cur;
859 skb = greth->rx_skbuff[greth->rx_cur];
860 GRETH_REGSAVE(greth->regs->status, GRETH_INT_RE | GRETH_INT_RX);
861 mb();
862 status = greth_read_bd(&bdp->stat);
863 bad = 0;
864
865 if (status & GRETH_BD_EN)
866 break;
867
868
869 if (unlikely(status & GRETH_RXBD_STATUS)) {
870
871 if (status & GRETH_RXBD_ERR_FT) {
872 dev->stats.rx_length_errors++;
873 bad = 1;
874 } else if (status &
875 (GRETH_RXBD_ERR_AE | GRETH_RXBD_ERR_OE | GRETH_RXBD_ERR_LE)) {
876 dev->stats.rx_frame_errors++;
877 bad = 1;
878 } else if (status & GRETH_RXBD_ERR_CRC) {
879 dev->stats.rx_crc_errors++;
880 bad = 1;
881 }
882 }
883
884
885
886 if (!bad && (newskb=netdev_alloc_skb(dev, MAX_FRAME_SIZE + NET_IP_ALIGN))) {
887 skb_reserve(newskb, NET_IP_ALIGN);
888
889 dma_addr = dma_map_single(greth->dev,
890 newskb->data,
891 MAX_FRAME_SIZE + NET_IP_ALIGN,
892 DMA_FROM_DEVICE);
893
894 if (!dma_mapping_error(greth->dev, dma_addr)) {
895
896 pkt_len = status & GRETH_BD_LEN;
897
898 dma_unmap_single(greth->dev,
899 greth_read_bd(&bdp->addr),
900 MAX_FRAME_SIZE + NET_IP_ALIGN,
901 DMA_FROM_DEVICE);
902
903 if (netif_msg_pktdata(greth))
904 greth_print_rx_packet(phys_to_virt(greth_read_bd(&bdp->addr)), pkt_len);
905
906 skb_put(skb, pkt_len);
907
908 if (dev->features & NETIF_F_RXCSUM && hw_checksummed(status))
909 skb->ip_summed = CHECKSUM_UNNECESSARY;
910 else
911 skb_checksum_none_assert(skb);
912
913 skb->protocol = eth_type_trans(skb, dev);
914 dev->stats.rx_packets++;
915 dev->stats.rx_bytes += pkt_len;
916 netif_receive_skb(skb);
917
918 greth->rx_skbuff[greth->rx_cur] = newskb;
919 greth_write_bd(&bdp->addr, dma_addr);
920 } else {
921 if (net_ratelimit())
922 dev_warn(greth->dev, "Could not create DMA mapping, dropping packet\n");
923 dev_kfree_skb(newskb);
924
925 dev->stats.rx_dropped++;
926 }
927 } else if (bad) {
928
929 dev->stats.rx_dropped++;
930 } else {
931
932
933
934
935
936
937 if (net_ratelimit())
938 dev_warn(greth->dev, "Could not allocate SKB, dropping packet\n");
939
940 dev->stats.rx_dropped++;
941 }
942
943 status = GRETH_BD_EN | GRETH_BD_IE;
944 if (greth->rx_cur == GRETH_RXBD_NUM_MASK) {
945 status |= GRETH_BD_WR;
946 }
947
948 wmb();
949 greth_write_bd(&bdp->stat, status);
950 spin_lock_irqsave(&greth->devlock, flags);
951 greth_enable_rx(greth);
952 spin_unlock_irqrestore(&greth->devlock, flags);
953 greth->rx_cur = NEXT_RX(greth->rx_cur);
954 }
955
956 return count;
957
958}
959
960static int greth_poll(struct napi_struct *napi, int budget)
961{
962 struct greth_private *greth;
963 int work_done = 0;
964 unsigned long flags;
965 u32 mask, ctrl;
966 greth = container_of(napi, struct greth_private, napi);
967
968restart_txrx_poll:
969 if (netif_queue_stopped(greth->netdev)) {
970 if (greth->gbit_mac)
971 greth_clean_tx_gbit(greth->netdev);
972 else
973 greth_clean_tx(greth->netdev);
974 }
975
976 if (greth->gbit_mac) {
977 work_done += greth_rx_gbit(greth->netdev, budget - work_done);
978 } else {
979 work_done += greth_rx(greth->netdev, budget - work_done);
980 }
981
982 if (work_done < budget) {
983
984 spin_lock_irqsave(&greth->devlock, flags);
985
986 ctrl = GRETH_REGLOAD(greth->regs->control);
987 if (netif_queue_stopped(greth->netdev)) {
988 GRETH_REGSAVE(greth->regs->control,
989 ctrl | GRETH_TXI | GRETH_RXI);
990 mask = GRETH_INT_RX | GRETH_INT_RE |
991 GRETH_INT_TX | GRETH_INT_TE;
992 } else {
993 GRETH_REGSAVE(greth->regs->control, ctrl | GRETH_RXI);
994 mask = GRETH_INT_RX | GRETH_INT_RE;
995 }
996
997 if (GRETH_REGLOAD(greth->regs->status) & mask) {
998 GRETH_REGSAVE(greth->regs->control, ctrl);
999 spin_unlock_irqrestore(&greth->devlock, flags);
1000 goto restart_txrx_poll;
1001 } else {
1002 __napi_complete(napi);
1003 spin_unlock_irqrestore(&greth->devlock, flags);
1004 }
1005 }
1006
1007 return work_done;
1008}
1009
1010static int greth_set_mac_add(struct net_device *dev, void *p)
1011{
1012 struct sockaddr *addr = p;
1013 struct greth_private *greth;
1014 struct greth_regs *regs;
1015
1016 greth = netdev_priv(dev);
1017 regs = greth->regs;
1018
1019 if (!is_valid_ether_addr(addr->sa_data))
1020 return -EADDRNOTAVAIL;
1021
1022 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
1023 GRETH_REGSAVE(regs->esa_msb, dev->dev_addr[0] << 8 | dev->dev_addr[1]);
1024 GRETH_REGSAVE(regs->esa_lsb, dev->dev_addr[2] << 24 | dev->dev_addr[3] << 16 |
1025 dev->dev_addr[4] << 8 | dev->dev_addr[5]);
1026
1027 return 0;
1028}
1029
1030static u32 greth_hash_get_index(__u8 *addr)
1031{
1032 return (ether_crc(6, addr)) & 0x3F;
1033}
1034
1035static void greth_set_hash_filter(struct net_device *dev)
1036{
1037 struct netdev_hw_addr *ha;
1038 struct greth_private *greth = netdev_priv(dev);
1039 struct greth_regs *regs = greth->regs;
1040 u32 mc_filter[2];
1041 unsigned int bitnr;
1042
1043 mc_filter[0] = mc_filter[1] = 0;
1044
1045 netdev_for_each_mc_addr(ha, dev) {
1046 bitnr = greth_hash_get_index(ha->addr);
1047 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
1048 }
1049
1050 GRETH_REGSAVE(regs->hash_msb, mc_filter[1]);
1051 GRETH_REGSAVE(regs->hash_lsb, mc_filter[0]);
1052}
1053
1054static void greth_set_multicast_list(struct net_device *dev)
1055{
1056 int cfg;
1057 struct greth_private *greth = netdev_priv(dev);
1058 struct greth_regs *regs = greth->regs;
1059
1060 cfg = GRETH_REGLOAD(regs->control);
1061 if (dev->flags & IFF_PROMISC)
1062 cfg |= GRETH_CTRL_PR;
1063 else
1064 cfg &= ~GRETH_CTRL_PR;
1065
1066 if (greth->multicast) {
1067 if (dev->flags & IFF_ALLMULTI) {
1068 GRETH_REGSAVE(regs->hash_msb, -1);
1069 GRETH_REGSAVE(regs->hash_lsb, -1);
1070 cfg |= GRETH_CTRL_MCEN;
1071 GRETH_REGSAVE(regs->control, cfg);
1072 return;
1073 }
1074
1075 if (netdev_mc_empty(dev)) {
1076 cfg &= ~GRETH_CTRL_MCEN;
1077 GRETH_REGSAVE(regs->control, cfg);
1078 return;
1079 }
1080
1081
1082 greth_set_hash_filter(dev);
1083 cfg |= GRETH_CTRL_MCEN;
1084 }
1085 GRETH_REGSAVE(regs->control, cfg);
1086}
1087
1088static u32 greth_get_msglevel(struct net_device *dev)
1089{
1090 struct greth_private *greth = netdev_priv(dev);
1091 return greth->msg_enable;
1092}
1093
1094static void greth_set_msglevel(struct net_device *dev, u32 value)
1095{
1096 struct greth_private *greth = netdev_priv(dev);
1097 greth->msg_enable = value;
1098}
1099static int greth_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1100{
1101 struct greth_private *greth = netdev_priv(dev);
1102 struct phy_device *phy = greth->phy;
1103
1104 if (!phy)
1105 return -ENODEV;
1106
1107 return phy_ethtool_gset(phy, cmd);
1108}
1109
1110static int greth_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1111{
1112 struct greth_private *greth = netdev_priv(dev);
1113 struct phy_device *phy = greth->phy;
1114
1115 if (!phy)
1116 return -ENODEV;
1117
1118 return phy_ethtool_sset(phy, cmd);
1119}
1120
1121static int greth_get_regs_len(struct net_device *dev)
1122{
1123 return sizeof(struct greth_regs);
1124}
1125
1126static void greth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
1127{
1128 struct greth_private *greth = netdev_priv(dev);
1129
1130 strlcpy(info->driver, dev_driver_string(greth->dev),
1131 sizeof(info->driver));
1132 strlcpy(info->version, "revision: 1.0", sizeof(info->version));
1133 strlcpy(info->bus_info, greth->dev->bus->name, sizeof(info->bus_info));
1134 strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
1135}
1136
1137static void greth_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *p)
1138{
1139 int i;
1140 struct greth_private *greth = netdev_priv(dev);
1141 u32 __iomem *greth_regs = (u32 __iomem *) greth->regs;
1142 u32 *buff = p;
1143
1144 for (i = 0; i < sizeof(struct greth_regs) / sizeof(u32); i++)
1145 buff[i] = greth_read_bd(&greth_regs[i]);
1146}
1147
1148static const struct ethtool_ops greth_ethtool_ops = {
1149 .get_msglevel = greth_get_msglevel,
1150 .set_msglevel = greth_set_msglevel,
1151 .get_settings = greth_get_settings,
1152 .set_settings = greth_set_settings,
1153 .get_drvinfo = greth_get_drvinfo,
1154 .get_regs_len = greth_get_regs_len,
1155 .get_regs = greth_get_regs,
1156 .get_link = ethtool_op_get_link,
1157};
1158
1159static struct net_device_ops greth_netdev_ops = {
1160 .ndo_open = greth_open,
1161 .ndo_stop = greth_close,
1162 .ndo_start_xmit = greth_start_xmit,
1163 .ndo_set_mac_address = greth_set_mac_add,
1164 .ndo_validate_addr = eth_validate_addr,
1165};
1166
1167static inline int wait_for_mdio(struct greth_private *greth)
1168{
1169 unsigned long timeout = jiffies + 4*HZ/100;
1170 while (GRETH_REGLOAD(greth->regs->mdio) & GRETH_MII_BUSY) {
1171 if (time_after(jiffies, timeout))
1172 return 0;
1173 }
1174 return 1;
1175}
1176
1177static int greth_mdio_read(struct mii_bus *bus, int phy, int reg)
1178{
1179 struct greth_private *greth = bus->priv;
1180 int data;
1181
1182 if (!wait_for_mdio(greth))
1183 return -EBUSY;
1184
1185 GRETH_REGSAVE(greth->regs->mdio, ((phy & 0x1F) << 11) | ((reg & 0x1F) << 6) | 2);
1186
1187 if (!wait_for_mdio(greth))
1188 return -EBUSY;
1189
1190 if (!(GRETH_REGLOAD(greth->regs->mdio) & GRETH_MII_NVALID)) {
1191 data = (GRETH_REGLOAD(greth->regs->mdio) >> 16) & 0xFFFF;
1192 return data;
1193
1194 } else {
1195 return -1;
1196 }
1197}
1198
1199static int greth_mdio_write(struct mii_bus *bus, int phy, int reg, u16 val)
1200{
1201 struct greth_private *greth = bus->priv;
1202
1203 if (!wait_for_mdio(greth))
1204 return -EBUSY;
1205
1206 GRETH_REGSAVE(greth->regs->mdio,
1207 ((val & 0xFFFF) << 16) | ((phy & 0x1F) << 11) | ((reg & 0x1F) << 6) | 1);
1208
1209 if (!wait_for_mdio(greth))
1210 return -EBUSY;
1211
1212 return 0;
1213}
1214
1215static int greth_mdio_reset(struct mii_bus *bus)
1216{
1217 return 0;
1218}
1219
1220static void greth_link_change(struct net_device *dev)
1221{
1222 struct greth_private *greth = netdev_priv(dev);
1223 struct phy_device *phydev = greth->phy;
1224 unsigned long flags;
1225 int status_change = 0;
1226 u32 ctrl;
1227
1228 spin_lock_irqsave(&greth->devlock, flags);
1229
1230 if (phydev->link) {
1231
1232 if ((greth->speed != phydev->speed) || (greth->duplex != phydev->duplex)) {
1233 ctrl = GRETH_REGLOAD(greth->regs->control) &
1234 ~(GRETH_CTRL_FD | GRETH_CTRL_SP | GRETH_CTRL_GB);
1235
1236 if (phydev->duplex)
1237 ctrl |= GRETH_CTRL_FD;
1238
1239 if (phydev->speed == SPEED_100)
1240 ctrl |= GRETH_CTRL_SP;
1241 else if (phydev->speed == SPEED_1000)
1242 ctrl |= GRETH_CTRL_GB;
1243
1244 GRETH_REGSAVE(greth->regs->control, ctrl);
1245 greth->speed = phydev->speed;
1246 greth->duplex = phydev->duplex;
1247 status_change = 1;
1248 }
1249 }
1250
1251 if (phydev->link != greth->link) {
1252 if (!phydev->link) {
1253 greth->speed = 0;
1254 greth->duplex = -1;
1255 }
1256 greth->link = phydev->link;
1257
1258 status_change = 1;
1259 }
1260
1261 spin_unlock_irqrestore(&greth->devlock, flags);
1262
1263 if (status_change) {
1264 if (phydev->link)
1265 pr_debug("%s: link up (%d/%s)\n",
1266 dev->name, phydev->speed,
1267 DUPLEX_FULL == phydev->duplex ? "Full" : "Half");
1268 else
1269 pr_debug("%s: link down\n", dev->name);
1270 }
1271}
1272
1273static int greth_mdio_probe(struct net_device *dev)
1274{
1275 struct greth_private *greth = netdev_priv(dev);
1276 struct phy_device *phy = NULL;
1277 int ret;
1278
1279
1280 phy = phy_find_first(greth->mdio);
1281
1282 if (!phy) {
1283 if (netif_msg_probe(greth))
1284 dev_err(&dev->dev, "no PHY found\n");
1285 return -ENXIO;
1286 }
1287
1288 ret = phy_connect_direct(dev, phy, &greth_link_change,
1289 greth->gbit_mac ? PHY_INTERFACE_MODE_GMII : PHY_INTERFACE_MODE_MII);
1290 if (ret) {
1291 if (netif_msg_ifup(greth))
1292 dev_err(&dev->dev, "could not attach to PHY\n");
1293 return ret;
1294 }
1295
1296 if (greth->gbit_mac)
1297 phy->supported &= PHY_GBIT_FEATURES;
1298 else
1299 phy->supported &= PHY_BASIC_FEATURES;
1300
1301 phy->advertising = phy->supported;
1302
1303 greth->link = 0;
1304 greth->speed = 0;
1305 greth->duplex = -1;
1306 greth->phy = phy;
1307
1308 return 0;
1309}
1310
1311static int greth_mdio_init(struct greth_private *greth)
1312{
1313 int ret, phy;
1314 unsigned long timeout;
1315
1316 greth->mdio = mdiobus_alloc();
1317 if (!greth->mdio) {
1318 return -ENOMEM;
1319 }
1320
1321 greth->mdio->name = "greth-mdio";
1322 snprintf(greth->mdio->id, MII_BUS_ID_SIZE, "%s-%d", greth->mdio->name, greth->irq);
1323 greth->mdio->read = greth_mdio_read;
1324 greth->mdio->write = greth_mdio_write;
1325 greth->mdio->reset = greth_mdio_reset;
1326 greth->mdio->priv = greth;
1327
1328 greth->mdio->irq = greth->mdio_irqs;
1329
1330 for (phy = 0; phy < PHY_MAX_ADDR; phy++)
1331 greth->mdio->irq[phy] = PHY_POLL;
1332
1333 ret = mdiobus_register(greth->mdio);
1334 if (ret) {
1335 goto error;
1336 }
1337
1338 ret = greth_mdio_probe(greth->netdev);
1339 if (ret) {
1340 if (netif_msg_probe(greth))
1341 dev_err(&greth->netdev->dev, "failed to probe MDIO bus\n");
1342 goto unreg_mdio;
1343 }
1344
1345 phy_start(greth->phy);
1346
1347
1348 if (greth->edcl && greth_edcl == 1) {
1349 phy_start_aneg(greth->phy);
1350 timeout = jiffies + 6*HZ;
1351 while (!phy_aneg_done(greth->phy) && time_before(jiffies, timeout)) {
1352 }
1353 genphy_read_status(greth->phy);
1354 greth_link_change(greth->netdev);
1355 }
1356
1357 return 0;
1358
1359unreg_mdio:
1360 mdiobus_unregister(greth->mdio);
1361error:
1362 mdiobus_free(greth->mdio);
1363 return ret;
1364}
1365
1366
1367static int greth_of_probe(struct platform_device *ofdev)
1368{
1369 struct net_device *dev;
1370 struct greth_private *greth;
1371 struct greth_regs *regs;
1372
1373 int i;
1374 int err;
1375 int tmp;
1376 unsigned long timeout;
1377
1378 dev = alloc_etherdev(sizeof(struct greth_private));
1379
1380 if (dev == NULL)
1381 return -ENOMEM;
1382
1383 greth = netdev_priv(dev);
1384 greth->netdev = dev;
1385 greth->dev = &ofdev->dev;
1386
1387 if (greth_debug > 0)
1388 greth->msg_enable = greth_debug;
1389 else
1390 greth->msg_enable = GRETH_DEF_MSG_ENABLE;
1391
1392 spin_lock_init(&greth->devlock);
1393
1394 greth->regs = of_ioremap(&ofdev->resource[0], 0,
1395 resource_size(&ofdev->resource[0]),
1396 "grlib-greth regs");
1397
1398 if (greth->regs == NULL) {
1399 if (netif_msg_probe(greth))
1400 dev_err(greth->dev, "ioremap failure.\n");
1401 err = -EIO;
1402 goto error1;
1403 }
1404
1405 regs = greth->regs;
1406 greth->irq = ofdev->archdata.irqs[0];
1407
1408 dev_set_drvdata(greth->dev, dev);
1409 SET_NETDEV_DEV(dev, greth->dev);
1410
1411 if (netif_msg_probe(greth))
1412 dev_dbg(greth->dev, "resetting controller.\n");
1413
1414
1415 GRETH_REGSAVE(regs->control, GRETH_RESET);
1416
1417
1418 timeout = jiffies + HZ/100;
1419 while (GRETH_REGLOAD(regs->control) & GRETH_RESET) {
1420 if (time_after(jiffies, timeout)) {
1421 err = -EIO;
1422 if (netif_msg_probe(greth))
1423 dev_err(greth->dev, "timeout when waiting for reset.\n");
1424 goto error2;
1425 }
1426 }
1427
1428
1429 greth->phyaddr = (GRETH_REGLOAD(regs->mdio) >> 11) & 0x1F;
1430
1431
1432 tmp = GRETH_REGLOAD(regs->control);
1433 greth->gbit_mac = (tmp >> 27) & 1;
1434
1435
1436 greth->multicast = (tmp >> 25) & 1;
1437
1438 greth->edcl = (tmp >> 31) & 1;
1439
1440
1441
1442 if (greth->edcl != 0)
1443 GRETH_REGORIN(regs->control, GRETH_CTRL_DISDUPLEX);
1444
1445
1446 greth->mdio_int_en = (tmp >> 26) & 1;
1447
1448 err = greth_mdio_init(greth);
1449 if (err) {
1450 if (netif_msg_probe(greth))
1451 dev_err(greth->dev, "failed to register MDIO bus\n");
1452 goto error2;
1453 }
1454
1455
1456 greth->tx_bd_base = dma_alloc_coherent(greth->dev, 1024,
1457 &greth->tx_bd_base_phys,
1458 GFP_KERNEL | __GFP_ZERO);
1459 if (!greth->tx_bd_base) {
1460 err = -ENOMEM;
1461 goto error3;
1462 }
1463
1464
1465 greth->rx_bd_base = dma_alloc_coherent(greth->dev, 1024,
1466 &greth->rx_bd_base_phys,
1467 GFP_KERNEL | __GFP_ZERO);
1468 if (!greth->rx_bd_base) {
1469 err = -ENOMEM;
1470 goto error4;
1471 }
1472
1473
1474 for (i = 0; i < 6; i++) {
1475 if (macaddr[i] != 0)
1476 break;
1477 }
1478 if (i == 6) {
1479 const unsigned char *addr;
1480 int len;
1481 addr = of_get_property(ofdev->dev.of_node, "local-mac-address",
1482 &len);
1483 if (addr != NULL && len == 6) {
1484 for (i = 0; i < 6; i++)
1485 macaddr[i] = (unsigned int) addr[i];
1486 } else {
1487#ifdef CONFIG_SPARC
1488 for (i = 0; i < 6; i++)
1489 macaddr[i] = (unsigned int) idprom->id_ethaddr[i];
1490#endif
1491 }
1492 }
1493
1494 for (i = 0; i < 6; i++)
1495 dev->dev_addr[i] = macaddr[i];
1496
1497 macaddr[5]++;
1498
1499 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
1500 if (netif_msg_probe(greth))
1501 dev_err(greth->dev, "no valid ethernet address, aborting.\n");
1502 err = -EINVAL;
1503 goto error5;
1504 }
1505
1506 GRETH_REGSAVE(regs->esa_msb, dev->dev_addr[0] << 8 | dev->dev_addr[1]);
1507 GRETH_REGSAVE(regs->esa_lsb, dev->dev_addr[2] << 24 | dev->dev_addr[3] << 16 |
1508 dev->dev_addr[4] << 8 | dev->dev_addr[5]);
1509
1510
1511 GRETH_REGSAVE(regs->status, 0xFF);
1512
1513 if (greth->gbit_mac) {
1514 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM |
1515 NETIF_F_RXCSUM;
1516 dev->features = dev->hw_features | NETIF_F_HIGHDMA;
1517 greth_netdev_ops.ndo_start_xmit = greth_start_xmit_gbit;
1518 }
1519
1520 if (greth->multicast) {
1521 greth_netdev_ops.ndo_set_rx_mode = greth_set_multicast_list;
1522 dev->flags |= IFF_MULTICAST;
1523 } else {
1524 dev->flags &= ~IFF_MULTICAST;
1525 }
1526
1527 dev->netdev_ops = &greth_netdev_ops;
1528 dev->ethtool_ops = &greth_ethtool_ops;
1529
1530 err = register_netdev(dev);
1531 if (err) {
1532 if (netif_msg_probe(greth))
1533 dev_err(greth->dev, "netdevice registration failed.\n");
1534 goto error5;
1535 }
1536
1537
1538 netif_napi_add(dev, &greth->napi, greth_poll, 64);
1539
1540 return 0;
1541
1542error5:
1543 dma_free_coherent(greth->dev, 1024, greth->rx_bd_base, greth->rx_bd_base_phys);
1544error4:
1545 dma_free_coherent(greth->dev, 1024, greth->tx_bd_base, greth->tx_bd_base_phys);
1546error3:
1547 mdiobus_unregister(greth->mdio);
1548error2:
1549 of_iounmap(&ofdev->resource[0], greth->regs, resource_size(&ofdev->resource[0]));
1550error1:
1551 free_netdev(dev);
1552 return err;
1553}
1554
1555static int greth_of_remove(struct platform_device *of_dev)
1556{
1557 struct net_device *ndev = dev_get_drvdata(&of_dev->dev);
1558 struct greth_private *greth = netdev_priv(ndev);
1559
1560
1561 dma_free_coherent(&of_dev->dev, 1024, greth->rx_bd_base, greth->rx_bd_base_phys);
1562
1563 dma_free_coherent(&of_dev->dev, 1024, greth->tx_bd_base, greth->tx_bd_base_phys);
1564
1565 dev_set_drvdata(&of_dev->dev, NULL);
1566
1567 if (greth->phy)
1568 phy_stop(greth->phy);
1569 mdiobus_unregister(greth->mdio);
1570
1571 unregister_netdev(ndev);
1572 free_netdev(ndev);
1573
1574 of_iounmap(&of_dev->resource[0], greth->regs, resource_size(&of_dev->resource[0]));
1575
1576 return 0;
1577}
1578
1579static struct of_device_id greth_of_match[] = {
1580 {
1581 .name = "GAISLER_ETHMAC",
1582 },
1583 {
1584 .name = "01_01d",
1585 },
1586 {},
1587};
1588
1589MODULE_DEVICE_TABLE(of, greth_of_match);
1590
1591static struct platform_driver greth_of_driver = {
1592 .driver = {
1593 .name = "grlib-greth",
1594 .owner = THIS_MODULE,
1595 .of_match_table = greth_of_match,
1596 },
1597 .probe = greth_of_probe,
1598 .remove = greth_of_remove,
1599};
1600
1601module_platform_driver(greth_of_driver);
1602
1603MODULE_AUTHOR("Aeroflex Gaisler AB.");
1604MODULE_DESCRIPTION("Aeroflex Gaisler Ethernet MAC driver");
1605MODULE_LICENSE("GPL");
1606