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