bc3bd34c43f1377ec11bf64852d44244d9304025
[linux-2.6.git] / drivers / net / ethernet / aeroflex / greth.c
1 /*
2  * Aeroflex Gaisler GRETH 10/100/1G Ethernet MAC.
3  *
4  * 2005-2010 (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/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
58 static int greth_debug = -1;    /* -1 == use GRETH_DEF_MSG_ENABLE as value */
59 module_param(greth_debug, int, 0);
60 MODULE_PARM_DESC(greth_debug, "GRETH bitmapped debugging message enable value");
61
62 /* Accept MAC address of the form macaddr=0x08,0x00,0x20,0x30,0x40,0x50 */
63 static int macaddr[6];
64 module_param_array(macaddr, int, NULL, 0);
65 MODULE_PARM_DESC(macaddr, "GRETH Ethernet MAC address");
66
67 static int greth_edcl = 1;
68 module_param(greth_edcl, int, 0);
69 MODULE_PARM_DESC(greth_edcl, "GRETH EDCL usage indicator. Set to 1 if EDCL is used.");
70
71 static int greth_open(struct net_device *dev);
72 static netdev_tx_t greth_start_xmit(struct sk_buff *skb,
73            struct net_device *dev);
74 static netdev_tx_t greth_start_xmit_gbit(struct sk_buff *skb,
75            struct net_device *dev);
76 static int greth_rx(struct net_device *dev, int limit);
77 static int greth_rx_gbit(struct net_device *dev, int limit);
78 static void greth_clean_tx(struct net_device *dev);
79 static void greth_clean_tx_gbit(struct net_device *dev);
80 static irqreturn_t greth_interrupt(int irq, void *dev_id);
81 static int greth_close(struct net_device *dev);
82 static int greth_set_mac_add(struct net_device *dev, void *p);
83 static 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
94 static 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
100 static 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
121 static inline void greth_enable_tx(struct greth_private *greth)
122 {
123         wmb();
124         GRETH_REGORIN(greth->regs->control, GRETH_TXEN);
125 }
126
127 static inline void greth_disable_tx(struct greth_private *greth)
128 {
129         GRETH_REGANDIN(greth->regs->control, ~GRETH_TXEN);
130 }
131
132 static inline void greth_enable_rx(struct greth_private *greth)
133 {
134         wmb();
135         GRETH_REGORIN(greth->regs->control, GRETH_RXEN);
136 }
137
138 static inline void greth_disable_rx(struct greth_private *greth)
139 {
140         GRETH_REGANDIN(greth->regs->control, ~GRETH_RXEN);
141 }
142
143 static inline void greth_enable_irqs(struct greth_private *greth)
144 {
145         GRETH_REGORIN(greth->regs->control, GRETH_RXI | GRETH_TXI);
146 }
147
148 static inline void greth_disable_irqs(struct greth_private *greth)
149 {
150         GRETH_REGANDIN(greth->regs->control, ~(GRETH_RXI|GRETH_TXI));
151 }
152
153 static inline void greth_write_bd(u32 *bd, u32 val)
154 {
155         __raw_writel(cpu_to_be32(val), bd);
156 }
157
158 static inline u32 greth_read_bd(u32 *bd)
159 {
160         return be32_to_cpu(__raw_readl(bd));
161 }
162
163 static 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                 /* Free and unmap RX buffers */
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                 /* TX buffers */
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                                                frag->size,
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 { /* 10/100 Mbps MAC */
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
230 static 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         /* Initialize descriptor rings and buffers */
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                 /* 10/100 MAC uses a fixed set of buffers and copy to/from SKBs */
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         /* Initialize pointers. */
320         greth->rx_cur = 0;
321         greth->tx_next = 0;
322         greth->tx_last = 0;
323         greth->tx_free = GRETH_TXBD_NUM;
324
325         /* Initialize descriptor base address */
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
331 cleanup:
332         greth_clean_rings(greth);
333         return -ENOMEM;
334 }
335
336 static 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
371 static 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
390 static netdev_tx_t
391 greth_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         /* Clean TX Ring */
400         greth_clean_tx(greth->netdev);
401
402         if (unlikely(greth->tx_free <= 0)) {
403                 spin_lock_irqsave(&greth->devlock, flags);/*save from poll/irq*/
404                 ctrl = GRETH_REGLOAD(greth->regs->control);
405                 /* Enable TX IRQ only if not already in poll() routine */
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
431         /* Wrap around descriptor ring */
432         if (greth->tx_next == GRETH_TXBD_NUM_MASK) {
433                 status |= GRETH_BD_WR;
434         }
435
436         greth->tx_next = NEXT_TX(greth->tx_next);
437         greth->tx_free--;
438
439         /* Write descriptor control word and enable transmission */
440         greth_write_bd(&bdp->stat, status);
441         spin_lock_irqsave(&greth->devlock, flags); /*save from poll/irq*/
442         greth_enable_tx(greth);
443         spin_unlock_irqrestore(&greth->devlock, flags);
444
445 out:
446         dev_kfree_skb(skb);
447         return err;
448 }
449
450
451 static netdev_tx_t
452 greth_start_xmit_gbit(struct sk_buff *skb, struct net_device *dev)
453 {
454         struct greth_private *greth = netdev_priv(dev);
455         struct greth_bd *bdp;
456         u32 status = 0, dma_addr, ctrl;
457         int curr_tx, nr_frags, i, err = NETDEV_TX_OK;
458         unsigned long flags;
459
460         nr_frags = skb_shinfo(skb)->nr_frags;
461
462         /* Clean TX Ring */
463         greth_clean_tx_gbit(dev);
464
465         if (greth->tx_free < nr_frags + 1) {
466                 spin_lock_irqsave(&greth->devlock, flags);/*save from poll/irq*/
467                 ctrl = GRETH_REGLOAD(greth->regs->control);
468                 /* Enable TX IRQ only if not already in poll() routine */
469                 if (ctrl & GRETH_RXI)
470                         GRETH_REGSAVE(greth->regs->control, ctrl | GRETH_TXI);
471                 netif_stop_queue(dev);
472                 spin_unlock_irqrestore(&greth->devlock, flags);
473                 err = NETDEV_TX_BUSY;
474                 goto out;
475         }
476
477         if (netif_msg_pktdata(greth))
478                 greth_print_tx_packet(skb);
479
480         if (unlikely(skb->len > MAX_FRAME_SIZE)) {
481                 dev->stats.tx_errors++;
482                 goto out;
483         }
484
485         /* Save skb pointer. */
486         greth->tx_skbuff[greth->tx_next] = skb;
487
488         /* Linear buf */
489         if (nr_frags != 0)
490                 status = GRETH_TXBD_MORE;
491
492         status |= GRETH_TXBD_CSALL;
493         status |= skb_headlen(skb) & GRETH_BD_LEN;
494         if (greth->tx_next == GRETH_TXBD_NUM_MASK)
495                 status |= GRETH_BD_WR;
496
497
498         bdp = greth->tx_bd_base + greth->tx_next;
499         greth_write_bd(&bdp->stat, status);
500         dma_addr = dma_map_single(greth->dev, skb->data, skb_headlen(skb), DMA_TO_DEVICE);
501
502         if (unlikely(dma_mapping_error(greth->dev, dma_addr)))
503                 goto map_error;
504
505         greth_write_bd(&bdp->addr, dma_addr);
506
507         curr_tx = NEXT_TX(greth->tx_next);
508
509         /* Frags */
510         for (i = 0; i < nr_frags; i++) {
511                 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
512                 greth->tx_skbuff[curr_tx] = NULL;
513                 bdp = greth->tx_bd_base + curr_tx;
514
515                 status = GRETH_TXBD_CSALL | GRETH_BD_EN;
516                 status |= frag->size & GRETH_BD_LEN;
517
518                 /* Wrap around descriptor ring */
519                 if (curr_tx == GRETH_TXBD_NUM_MASK)
520                         status |= GRETH_BD_WR;
521
522                 /* More fragments left */
523                 if (i < nr_frags - 1)
524                         status |= GRETH_TXBD_MORE;
525                 else
526                         status |= GRETH_BD_IE; /* enable IRQ on last fragment */
527
528                 greth_write_bd(&bdp->stat, status);
529
530                 dma_addr = skb_frag_dma_map(greth->dev, frag, 0, frag->size,
531                                             DMA_TO_DEVICE);
532
533                 if (unlikely(dma_mapping_error(greth->dev, dma_addr)))
534                         goto frag_map_error;
535
536                 greth_write_bd(&bdp->addr, dma_addr);
537
538                 curr_tx = NEXT_TX(curr_tx);
539         }
540
541         wmb();
542
543         /* Enable the descriptor chain by enabling the first descriptor */
544         bdp = greth->tx_bd_base + greth->tx_next;
545         greth_write_bd(&bdp->stat, greth_read_bd(&bdp->stat) | GRETH_BD_EN);
546         greth->tx_next = curr_tx;
547         greth->tx_free -= nr_frags + 1;
548
549         wmb();
550
551         spin_lock_irqsave(&greth->devlock, flags); /*save from poll/irq*/
552         greth_enable_tx(greth);
553         spin_unlock_irqrestore(&greth->devlock, flags);
554
555         return NETDEV_TX_OK;
556
557 frag_map_error:
558         /* Unmap SKB mappings that succeeded and disable descriptor */
559         for (i = 0; greth->tx_next + i != curr_tx; i++) {
560                 bdp = greth->tx_bd_base + greth->tx_next + i;
561                 dma_unmap_single(greth->dev,
562                                  greth_read_bd(&bdp->addr),
563                                  greth_read_bd(&bdp->stat) & GRETH_BD_LEN,
564                                  DMA_TO_DEVICE);
565                 greth_write_bd(&bdp->stat, 0);
566         }
567 map_error:
568         if (net_ratelimit())
569                 dev_warn(greth->dev, "Could not create TX DMA mapping\n");
570         dev_kfree_skb(skb);
571 out:
572         return err;
573 }
574
575 static irqreturn_t greth_interrupt(int irq, void *dev_id)
576 {
577         struct net_device *dev = dev_id;
578         struct greth_private *greth;
579         u32 status, ctrl;
580         irqreturn_t retval = IRQ_NONE;
581
582         greth = netdev_priv(dev);
583
584         spin_lock(&greth->devlock);
585
586         /* Get the interrupt events that caused us to be here. */
587         status = GRETH_REGLOAD(greth->regs->status);
588
589         /* Must see if interrupts are enabled also, INT_TX|INT_RX flags may be
590          * set regardless of whether IRQ is enabled or not. Especially
591          * important when shared IRQ.
592          */
593         ctrl = GRETH_REGLOAD(greth->regs->control);
594
595         /* Handle rx and tx interrupts through poll */
596         if (((status & (GRETH_INT_RE | GRETH_INT_RX)) && (ctrl & GRETH_RXI)) ||
597             ((status & (GRETH_INT_TE | GRETH_INT_TX)) && (ctrl & GRETH_TXI))) {
598                 retval = IRQ_HANDLED;
599
600                 /* Disable interrupts and schedule poll() */
601                 greth_disable_irqs(greth);
602                 napi_schedule(&greth->napi);
603         }
604
605         mmiowb();
606         spin_unlock(&greth->devlock);
607
608         return retval;
609 }
610
611 static void greth_clean_tx(struct net_device *dev)
612 {
613         struct greth_private *greth;
614         struct greth_bd *bdp;
615         u32 stat;
616
617         greth = netdev_priv(dev);
618
619         while (1) {
620                 bdp = greth->tx_bd_base + greth->tx_last;
621                 GRETH_REGSAVE(greth->regs->status, GRETH_INT_TE | GRETH_INT_TX);
622                 mb();
623                 stat = greth_read_bd(&bdp->stat);
624
625                 if (unlikely(stat & GRETH_BD_EN))
626                         break;
627
628                 if (greth->tx_free == GRETH_TXBD_NUM)
629                         break;
630
631                 /* Check status for errors */
632                 if (unlikely(stat & GRETH_TXBD_STATUS)) {
633                         dev->stats.tx_errors++;
634                         if (stat & GRETH_TXBD_ERR_AL)
635                                 dev->stats.tx_aborted_errors++;
636                         if (stat & GRETH_TXBD_ERR_UE)
637                                 dev->stats.tx_fifo_errors++;
638                 }
639                 dev->stats.tx_packets++;
640                 greth->tx_last = NEXT_TX(greth->tx_last);
641                 greth->tx_free++;
642         }
643
644         if (greth->tx_free > 0) {
645                 netif_wake_queue(dev);
646         }
647
648 }
649
650 static inline void greth_update_tx_stats(struct net_device *dev, u32 stat)
651 {
652         /* Check status for errors */
653         if (unlikely(stat & GRETH_TXBD_STATUS)) {
654                 dev->stats.tx_errors++;
655                 if (stat & GRETH_TXBD_ERR_AL)
656                         dev->stats.tx_aborted_errors++;
657                 if (stat & GRETH_TXBD_ERR_UE)
658                         dev->stats.tx_fifo_errors++;
659                 if (stat & GRETH_TXBD_ERR_LC)
660                         dev->stats.tx_aborted_errors++;
661         }
662         dev->stats.tx_packets++;
663 }
664
665 static void greth_clean_tx_gbit(struct net_device *dev)
666 {
667         struct greth_private *greth;
668         struct greth_bd *bdp, *bdp_last_frag;
669         struct sk_buff *skb;
670         u32 stat;
671         int nr_frags, i;
672
673         greth = netdev_priv(dev);
674
675         while (greth->tx_free < GRETH_TXBD_NUM) {
676
677                 skb = greth->tx_skbuff[greth->tx_last];
678
679                 nr_frags = skb_shinfo(skb)->nr_frags;
680
681                 /* We only clean fully completed SKBs */
682                 bdp_last_frag = greth->tx_bd_base + SKIP_TX(greth->tx_last, nr_frags);
683
684                 GRETH_REGSAVE(greth->regs->status, GRETH_INT_TE | GRETH_INT_TX);
685                 mb();
686                 stat = greth_read_bd(&bdp_last_frag->stat);
687
688                 if (stat & GRETH_BD_EN)
689                         break;
690
691                 greth->tx_skbuff[greth->tx_last] = NULL;
692
693                 greth_update_tx_stats(dev, stat);
694
695                 bdp = greth->tx_bd_base + greth->tx_last;
696
697                 greth->tx_last = NEXT_TX(greth->tx_last);
698
699                 dma_unmap_single(greth->dev,
700                                  greth_read_bd(&bdp->addr),
701                                  skb_headlen(skb),
702                                  DMA_TO_DEVICE);
703
704                 for (i = 0; i < nr_frags; i++) {
705                         skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
706                         bdp = greth->tx_bd_base + greth->tx_last;
707
708                         dma_unmap_page(greth->dev,
709                                        greth_read_bd(&bdp->addr),
710                                        frag->size,
711                                        DMA_TO_DEVICE);
712
713                         greth->tx_last = NEXT_TX(greth->tx_last);
714                 }
715                 greth->tx_free += nr_frags+1;
716                 dev_kfree_skb(skb);
717         }
718
719         if (netif_queue_stopped(dev) && (greth->tx_free > (MAX_SKB_FRAGS+1)))
720                 netif_wake_queue(dev);
721 }
722
723 static int greth_rx(struct net_device *dev, int limit)
724 {
725         struct greth_private *greth;
726         struct greth_bd *bdp;
727         struct sk_buff *skb;
728         int pkt_len;
729         int bad, count;
730         u32 status, dma_addr;
731         unsigned long flags;
732
733         greth = netdev_priv(dev);
734
735         for (count = 0; count < limit; ++count) {
736
737                 bdp = greth->rx_bd_base + greth->rx_cur;
738                 GRETH_REGSAVE(greth->regs->status, GRETH_INT_RE | GRETH_INT_RX);
739                 mb();
740                 status = greth_read_bd(&bdp->stat);
741
742                 if (unlikely(status & GRETH_BD_EN)) {
743                         break;
744                 }
745
746                 dma_addr = greth_read_bd(&bdp->addr);
747                 bad = 0;
748
749                 /* Check status for errors. */
750                 if (unlikely(status & GRETH_RXBD_STATUS)) {
751                         if (status & GRETH_RXBD_ERR_FT) {
752                                 dev->stats.rx_length_errors++;
753                                 bad = 1;
754                         }
755                         if (status & (GRETH_RXBD_ERR_AE | GRETH_RXBD_ERR_OE)) {
756                                 dev->stats.rx_frame_errors++;
757                                 bad = 1;
758                         }
759                         if (status & GRETH_RXBD_ERR_CRC) {
760                                 dev->stats.rx_crc_errors++;
761                                 bad = 1;
762                         }
763                 }
764                 if (unlikely(bad)) {
765                         dev->stats.rx_errors++;
766
767                 } else {
768
769                         pkt_len = status & GRETH_BD_LEN;
770
771                         skb = netdev_alloc_skb(dev, pkt_len + NET_IP_ALIGN);
772
773                         if (unlikely(skb == NULL)) {
774
775                                 if (net_ratelimit())
776                                         dev_warn(&dev->dev, "low on memory - " "packet dropped\n");
777
778                                 dev->stats.rx_dropped++;
779
780                         } else {
781                                 skb_reserve(skb, NET_IP_ALIGN);
782                                 skb->dev = dev;
783
784                                 dma_sync_single_for_cpu(greth->dev,
785                                                         dma_addr,
786                                                         pkt_len,
787                                                         DMA_FROM_DEVICE);
788
789                                 if (netif_msg_pktdata(greth))
790                                         greth_print_rx_packet(phys_to_virt(dma_addr), pkt_len);
791
792                                 memcpy(skb_put(skb, pkt_len), phys_to_virt(dma_addr), pkt_len);
793
794                                 skb->protocol = eth_type_trans(skb, dev);
795                                 dev->stats.rx_packets++;
796                                 netif_receive_skb(skb);
797                         }
798                 }
799
800                 status = GRETH_BD_EN | GRETH_BD_IE;
801                 if (greth->rx_cur == GRETH_RXBD_NUM_MASK) {
802                         status |= GRETH_BD_WR;
803                 }
804
805                 wmb();
806                 greth_write_bd(&bdp->stat, status);
807
808                 dma_sync_single_for_device(greth->dev, dma_addr, MAX_FRAME_SIZE, DMA_FROM_DEVICE);
809
810                 spin_lock_irqsave(&greth->devlock, flags); /* save from XMIT */
811                 greth_enable_rx(greth);
812                 spin_unlock_irqrestore(&greth->devlock, flags);
813
814                 greth->rx_cur = NEXT_RX(greth->rx_cur);
815         }
816
817         return count;
818 }
819
820 static inline int hw_checksummed(u32 status)
821 {
822
823         if (status & GRETH_RXBD_IP_FRAG)
824                 return 0;
825
826         if (status & GRETH_RXBD_IP && status & GRETH_RXBD_IP_CSERR)
827                 return 0;
828
829         if (status & GRETH_RXBD_UDP && status & GRETH_RXBD_UDP_CSERR)
830                 return 0;
831
832         if (status & GRETH_RXBD_TCP && status & GRETH_RXBD_TCP_CSERR)
833                 return 0;
834
835         return 1;
836 }
837
838 static int greth_rx_gbit(struct net_device *dev, int limit)
839 {
840         struct greth_private *greth;
841         struct greth_bd *bdp;
842         struct sk_buff *skb, *newskb;
843         int pkt_len;
844         int bad, count = 0;
845         u32 status, dma_addr;
846         unsigned long flags;
847
848         greth = netdev_priv(dev);
849
850         for (count = 0; count < limit; ++count) {
851
852                 bdp = greth->rx_bd_base + greth->rx_cur;
853                 skb = greth->rx_skbuff[greth->rx_cur];
854                 GRETH_REGSAVE(greth->regs->status, GRETH_INT_RE | GRETH_INT_RX);
855                 mb();
856                 status = greth_read_bd(&bdp->stat);
857                 bad = 0;
858
859                 if (status & GRETH_BD_EN)
860                         break;
861
862                 /* Check status for errors. */
863                 if (unlikely(status & GRETH_RXBD_STATUS)) {
864
865                         if (status & GRETH_RXBD_ERR_FT) {
866                                 dev->stats.rx_length_errors++;
867                                 bad = 1;
868                         } else if (status &
869                                    (GRETH_RXBD_ERR_AE | GRETH_RXBD_ERR_OE | GRETH_RXBD_ERR_LE)) {
870                                 dev->stats.rx_frame_errors++;
871                                 bad = 1;
872                         } else if (status & GRETH_RXBD_ERR_CRC) {
873                                 dev->stats.rx_crc_errors++;
874                                 bad = 1;
875                         }
876                 }
877
878                 /* Allocate new skb to replace current, not needed if the
879                  * current skb can be reused */
880                 if (!bad && (newskb=netdev_alloc_skb(dev, MAX_FRAME_SIZE + NET_IP_ALIGN))) {
881                         skb_reserve(newskb, NET_IP_ALIGN);
882
883                         dma_addr = dma_map_single(greth->dev,
884                                                       newskb->data,
885                                                       MAX_FRAME_SIZE + NET_IP_ALIGN,
886                                                       DMA_FROM_DEVICE);
887
888                         if (!dma_mapping_error(greth->dev, dma_addr)) {
889                                 /* Process the incoming frame. */
890                                 pkt_len = status & GRETH_BD_LEN;
891
892                                 dma_unmap_single(greth->dev,
893                                                  greth_read_bd(&bdp->addr),
894                                                  MAX_FRAME_SIZE + NET_IP_ALIGN,
895                                                  DMA_FROM_DEVICE);
896
897                                 if (netif_msg_pktdata(greth))
898                                         greth_print_rx_packet(phys_to_virt(greth_read_bd(&bdp->addr)), pkt_len);
899
900                                 skb_put(skb, pkt_len);
901
902                                 if (dev->features & NETIF_F_RXCSUM && hw_checksummed(status))
903                                         skb->ip_summed = CHECKSUM_UNNECESSARY;
904                                 else
905                                         skb_checksum_none_assert(skb);
906
907                                 skb->protocol = eth_type_trans(skb, dev);
908                                 dev->stats.rx_packets++;
909                                 netif_receive_skb(skb);
910
911                                 greth->rx_skbuff[greth->rx_cur] = newskb;
912                                 greth_write_bd(&bdp->addr, dma_addr);
913                         } else {
914                                 if (net_ratelimit())
915                                         dev_warn(greth->dev, "Could not create DMA mapping, dropping packet\n");
916                                 dev_kfree_skb(newskb);
917                                 /* reusing current skb, so it is a drop */
918                                 dev->stats.rx_dropped++;
919                         }
920                 } else if (bad) {
921                         /* Bad Frame transfer, the skb is reused */
922                         dev->stats.rx_dropped++;
923                 } else {
924                         /* Failed Allocating a new skb. This is rather stupid
925                          * but the current "filled" skb is reused, as if
926                          * transfer failure. One could argue that RX descriptor
927                          * table handling should be divided into cleaning and
928                          * filling as the TX part of the driver
929                          */
930                         if (net_ratelimit())
931                                 dev_warn(greth->dev, "Could not allocate SKB, dropping packet\n");
932                         /* reusing current skb, so it is a drop */
933                         dev->stats.rx_dropped++;
934                 }
935
936                 status = GRETH_BD_EN | GRETH_BD_IE;
937                 if (greth->rx_cur == GRETH_RXBD_NUM_MASK) {
938                         status |= GRETH_BD_WR;
939                 }
940
941                 wmb();
942                 greth_write_bd(&bdp->stat, status);
943                 spin_lock_irqsave(&greth->devlock, flags);
944                 greth_enable_rx(greth);
945                 spin_unlock_irqrestore(&greth->devlock, flags);
946                 greth->rx_cur = NEXT_RX(greth->rx_cur);
947         }
948
949         return count;
950
951 }
952
953 static int greth_poll(struct napi_struct *napi, int budget)
954 {
955         struct greth_private *greth;
956         int work_done = 0;
957         unsigned long flags;
958         u32 mask, ctrl;
959         greth = container_of(napi, struct greth_private, napi);
960
961 restart_txrx_poll:
962         if (netif_queue_stopped(greth->netdev)) {
963                 if (greth->gbit_mac)
964                         greth_clean_tx_gbit(greth->netdev);
965                 else
966                         greth_clean_tx(greth->netdev);
967         }
968
969         if (greth->gbit_mac) {
970                 work_done += greth_rx_gbit(greth->netdev, budget - work_done);
971         } else {
972                 work_done += greth_rx(greth->netdev, budget - work_done);
973         }
974
975         if (work_done < budget) {
976
977                 spin_lock_irqsave(&greth->devlock, flags);
978
979                 ctrl = GRETH_REGLOAD(greth->regs->control);
980                 if (netif_queue_stopped(greth->netdev)) {
981                         GRETH_REGSAVE(greth->regs->control,
982                                         ctrl | GRETH_TXI | GRETH_RXI);
983                         mask = GRETH_INT_RX | GRETH_INT_RE |
984                                GRETH_INT_TX | GRETH_INT_TE;
985                 } else {
986                         GRETH_REGSAVE(greth->regs->control, ctrl | GRETH_RXI);
987                         mask = GRETH_INT_RX | GRETH_INT_RE;
988                 }
989
990                 if (GRETH_REGLOAD(greth->regs->status) & mask) {
991                         GRETH_REGSAVE(greth->regs->control, ctrl);
992                         spin_unlock_irqrestore(&greth->devlock, flags);
993                         goto restart_txrx_poll;
994                 } else {
995                         __napi_complete(napi);
996                         spin_unlock_irqrestore(&greth->devlock, flags);
997                 }
998         }
999
1000         return work_done;
1001 }
1002
1003 static int greth_set_mac_add(struct net_device *dev, void *p)
1004 {
1005         struct sockaddr *addr = p;
1006         struct greth_private *greth;
1007         struct greth_regs *regs;
1008
1009         greth = netdev_priv(dev);
1010         regs = (struct greth_regs *) greth->regs;
1011
1012         if (!is_valid_ether_addr(addr->sa_data))
1013                 return -EINVAL;
1014
1015         memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
1016         GRETH_REGSAVE(regs->esa_msb, dev->dev_addr[0] << 8 | dev->dev_addr[1]);
1017         GRETH_REGSAVE(regs->esa_lsb, dev->dev_addr[2] << 24 | dev->dev_addr[3] << 16 |
1018                       dev->dev_addr[4] << 8 | dev->dev_addr[5]);
1019
1020         return 0;
1021 }
1022
1023 static u32 greth_hash_get_index(__u8 *addr)
1024 {
1025         return (ether_crc(6, addr)) & 0x3F;
1026 }
1027
1028 static void greth_set_hash_filter(struct net_device *dev)
1029 {
1030         struct netdev_hw_addr *ha;
1031         struct greth_private *greth = netdev_priv(dev);
1032         struct greth_regs *regs = (struct greth_regs *) greth->regs;
1033         u32 mc_filter[2];
1034         unsigned int bitnr;
1035
1036         mc_filter[0] = mc_filter[1] = 0;
1037
1038         netdev_for_each_mc_addr(ha, dev) {
1039                 bitnr = greth_hash_get_index(ha->addr);
1040                 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
1041         }
1042
1043         GRETH_REGSAVE(regs->hash_msb, mc_filter[1]);
1044         GRETH_REGSAVE(regs->hash_lsb, mc_filter[0]);
1045 }
1046
1047 static void greth_set_multicast_list(struct net_device *dev)
1048 {
1049         int cfg;
1050         struct greth_private *greth = netdev_priv(dev);
1051         struct greth_regs *regs = (struct greth_regs *) greth->regs;
1052
1053         cfg = GRETH_REGLOAD(regs->control);
1054         if (dev->flags & IFF_PROMISC)
1055                 cfg |= GRETH_CTRL_PR;
1056         else
1057                 cfg &= ~GRETH_CTRL_PR;
1058
1059         if (greth->multicast) {
1060                 if (dev->flags & IFF_ALLMULTI) {
1061                         GRETH_REGSAVE(regs->hash_msb, -1);
1062                         GRETH_REGSAVE(regs->hash_lsb, -1);
1063                         cfg |= GRETH_CTRL_MCEN;
1064                         GRETH_REGSAVE(regs->control, cfg);
1065                         return;
1066                 }
1067
1068                 if (netdev_mc_empty(dev)) {
1069                         cfg &= ~GRETH_CTRL_MCEN;
1070                         GRETH_REGSAVE(regs->control, cfg);
1071                         return;
1072                 }
1073
1074                 /* Setup multicast filter */
1075                 greth_set_hash_filter(dev);
1076                 cfg |= GRETH_CTRL_MCEN;
1077         }
1078         GRETH_REGSAVE(regs->control, cfg);
1079 }
1080
1081 static u32 greth_get_msglevel(struct net_device *dev)
1082 {
1083         struct greth_private *greth = netdev_priv(dev);
1084         return greth->msg_enable;
1085 }
1086
1087 static void greth_set_msglevel(struct net_device *dev, u32 value)
1088 {
1089         struct greth_private *greth = netdev_priv(dev);
1090         greth->msg_enable = value;
1091 }
1092 static int greth_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1093 {
1094         struct greth_private *greth = netdev_priv(dev);
1095         struct phy_device *phy = greth->phy;
1096
1097         if (!phy)
1098                 return -ENODEV;
1099
1100         return phy_ethtool_gset(phy, cmd);
1101 }
1102
1103 static int greth_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1104 {
1105         struct greth_private *greth = netdev_priv(dev);
1106         struct phy_device *phy = greth->phy;
1107
1108         if (!phy)
1109                 return -ENODEV;
1110
1111         return phy_ethtool_sset(phy, cmd);
1112 }
1113
1114 static int greth_get_regs_len(struct net_device *dev)
1115 {
1116         return sizeof(struct greth_regs);
1117 }
1118
1119 static void greth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
1120 {
1121         struct greth_private *greth = netdev_priv(dev);
1122
1123         strncpy(info->driver, dev_driver_string(greth->dev), 32);
1124         strncpy(info->version, "revision: 1.0", 32);
1125         strncpy(info->bus_info, greth->dev->bus->name, 32);
1126         strncpy(info->fw_version, "N/A", 32);
1127         info->eedump_len = 0;
1128         info->regdump_len = sizeof(struct greth_regs);
1129 }
1130
1131 static void greth_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *p)
1132 {
1133         int i;
1134         struct greth_private *greth = netdev_priv(dev);
1135         u32 __iomem *greth_regs = (u32 __iomem *) greth->regs;
1136         u32 *buff = p;
1137
1138         for (i = 0; i < sizeof(struct greth_regs) / sizeof(u32); i++)
1139                 buff[i] = greth_read_bd(&greth_regs[i]);
1140 }
1141
1142 static const struct ethtool_ops greth_ethtool_ops = {
1143         .get_msglevel           = greth_get_msglevel,
1144         .set_msglevel           = greth_set_msglevel,
1145         .get_settings           = greth_get_settings,
1146         .set_settings           = greth_set_settings,
1147         .get_drvinfo            = greth_get_drvinfo,
1148         .get_regs_len           = greth_get_regs_len,
1149         .get_regs               = greth_get_regs,
1150         .get_link               = ethtool_op_get_link,
1151 };
1152
1153 static struct net_device_ops greth_netdev_ops = {
1154         .ndo_open               = greth_open,
1155         .ndo_stop               = greth_close,
1156         .ndo_start_xmit         = greth_start_xmit,
1157         .ndo_set_mac_address    = greth_set_mac_add,
1158         .ndo_validate_addr      = eth_validate_addr,
1159 };
1160
1161 static inline int wait_for_mdio(struct greth_private *greth)
1162 {
1163         unsigned long timeout = jiffies + 4*HZ/100;
1164         while (GRETH_REGLOAD(greth->regs->mdio) & GRETH_MII_BUSY) {
1165                 if (time_after(jiffies, timeout))
1166                         return 0;
1167         }
1168         return 1;
1169 }
1170
1171 static int greth_mdio_read(struct mii_bus *bus, int phy, int reg)
1172 {
1173         struct greth_private *greth = bus->priv;
1174         int data;
1175
1176         if (!wait_for_mdio(greth))
1177                 return -EBUSY;
1178
1179         GRETH_REGSAVE(greth->regs->mdio, ((phy & 0x1F) << 11) | ((reg & 0x1F) << 6) | 2);
1180
1181         if (!wait_for_mdio(greth))
1182                 return -EBUSY;
1183
1184         if (!(GRETH_REGLOAD(greth->regs->mdio) & GRETH_MII_NVALID)) {
1185                 data = (GRETH_REGLOAD(greth->regs->mdio) >> 16) & 0xFFFF;
1186                 return data;
1187
1188         } else {
1189                 return -1;
1190         }
1191 }
1192
1193 static int greth_mdio_write(struct mii_bus *bus, int phy, int reg, u16 val)
1194 {
1195         struct greth_private *greth = bus->priv;
1196
1197         if (!wait_for_mdio(greth))
1198                 return -EBUSY;
1199
1200         GRETH_REGSAVE(greth->regs->mdio,
1201                       ((val & 0xFFFF) << 16) | ((phy & 0x1F) << 11) | ((reg & 0x1F) << 6) | 1);
1202
1203         if (!wait_for_mdio(greth))
1204                 return -EBUSY;
1205
1206         return 0;
1207 }
1208
1209 static int greth_mdio_reset(struct mii_bus *bus)
1210 {
1211         return 0;
1212 }
1213
1214 static void greth_link_change(struct net_device *dev)
1215 {
1216         struct greth_private *greth = netdev_priv(dev);
1217         struct phy_device *phydev = greth->phy;
1218         unsigned long flags;
1219         int status_change = 0;
1220         u32 ctrl;
1221
1222         spin_lock_irqsave(&greth->devlock, flags);
1223
1224         if (phydev->link) {
1225
1226                 if ((greth->speed != phydev->speed) || (greth->duplex != phydev->duplex)) {
1227                         ctrl = GRETH_REGLOAD(greth->regs->control) &
1228                                ~(GRETH_CTRL_FD | GRETH_CTRL_SP | GRETH_CTRL_GB);
1229
1230                         if (phydev->duplex)
1231                                 ctrl |= GRETH_CTRL_FD;
1232
1233                         if (phydev->speed == SPEED_100)
1234                                 ctrl |= GRETH_CTRL_SP;
1235                         else if (phydev->speed == SPEED_1000)
1236                                 ctrl |= GRETH_CTRL_GB;
1237
1238                         GRETH_REGSAVE(greth->regs->control, ctrl);
1239                         greth->speed = phydev->speed;
1240                         greth->duplex = phydev->duplex;
1241                         status_change = 1;
1242                 }
1243         }
1244
1245         if (phydev->link != greth->link) {
1246                 if (!phydev->link) {
1247                         greth->speed = 0;
1248                         greth->duplex = -1;
1249                 }
1250                 greth->link = phydev->link;
1251
1252                 status_change = 1;
1253         }
1254
1255         spin_unlock_irqrestore(&greth->devlock, flags);
1256
1257         if (status_change) {
1258                 if (phydev->link)
1259                         pr_debug("%s: link up (%d/%s)\n",
1260                                 dev->name, phydev->speed,
1261                                 DUPLEX_FULL == phydev->duplex ? "Full" : "Half");
1262                 else
1263                         pr_debug("%s: link down\n", dev->name);
1264         }
1265 }
1266
1267 static int greth_mdio_probe(struct net_device *dev)
1268 {
1269         struct greth_private *greth = netdev_priv(dev);
1270         struct phy_device *phy = NULL;
1271         int ret;
1272
1273         /* Find the first PHY */
1274         phy = phy_find_first(greth->mdio);
1275
1276         if (!phy) {
1277                 if (netif_msg_probe(greth))
1278                         dev_err(&dev->dev, "no PHY found\n");
1279                 return -ENXIO;
1280         }
1281
1282         ret = phy_connect_direct(dev, phy, &greth_link_change,
1283                         0, greth->gbit_mac ?
1284                         PHY_INTERFACE_MODE_GMII :
1285                         PHY_INTERFACE_MODE_MII);
1286         if (ret) {
1287                 if (netif_msg_ifup(greth))
1288                         dev_err(&dev->dev, "could not attach to PHY\n");
1289                 return ret;
1290         }
1291
1292         if (greth->gbit_mac)
1293                 phy->supported &= PHY_GBIT_FEATURES;
1294         else
1295                 phy->supported &= PHY_BASIC_FEATURES;
1296
1297         phy->advertising = phy->supported;
1298
1299         greth->link = 0;
1300         greth->speed = 0;
1301         greth->duplex = -1;
1302         greth->phy = phy;
1303
1304         return 0;
1305 }
1306
1307 static inline int phy_aneg_done(struct phy_device *phydev)
1308 {
1309         int retval;
1310
1311         retval = phy_read(phydev, MII_BMSR);
1312
1313         return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
1314 }
1315
1316 static int greth_mdio_init(struct greth_private *greth)
1317 {
1318         int ret, phy;
1319         unsigned long timeout;
1320
1321         greth->mdio = mdiobus_alloc();
1322         if (!greth->mdio) {
1323                 return -ENOMEM;
1324         }
1325
1326         greth->mdio->name = "greth-mdio";
1327         snprintf(greth->mdio->id, MII_BUS_ID_SIZE, "%s-%d", greth->mdio->name, greth->irq);
1328         greth->mdio->read = greth_mdio_read;
1329         greth->mdio->write = greth_mdio_write;
1330         greth->mdio->reset = greth_mdio_reset;
1331         greth->mdio->priv = greth;
1332
1333         greth->mdio->irq = greth->mdio_irqs;
1334
1335         for (phy = 0; phy < PHY_MAX_ADDR; phy++)
1336                 greth->mdio->irq[phy] = PHY_POLL;
1337
1338         ret = mdiobus_register(greth->mdio);
1339         if (ret) {
1340                 goto error;
1341         }
1342
1343         ret = greth_mdio_probe(greth->netdev);
1344         if (ret) {
1345                 if (netif_msg_probe(greth))
1346                         dev_err(&greth->netdev->dev, "failed to probe MDIO bus\n");
1347                 goto unreg_mdio;
1348         }
1349
1350         phy_start(greth->phy);
1351
1352         /* If Ethernet debug link is used make autoneg happen right away */
1353         if (greth->edcl && greth_edcl == 1) {
1354                 phy_start_aneg(greth->phy);
1355                 timeout = jiffies + 6*HZ;
1356                 while (!phy_aneg_done(greth->phy) && time_before(jiffies, timeout)) {
1357                 }
1358                 genphy_read_status(greth->phy);
1359                 greth_link_change(greth->netdev);
1360         }
1361
1362         return 0;
1363
1364 unreg_mdio:
1365         mdiobus_unregister(greth->mdio);
1366 error:
1367         mdiobus_free(greth->mdio);
1368         return ret;
1369 }
1370
1371 /* Initialize the GRETH MAC */
1372 static int __devinit greth_of_probe(struct platform_device *ofdev)
1373 {
1374         struct net_device *dev;
1375         struct greth_private *greth;
1376         struct greth_regs *regs;
1377
1378         int i;
1379         int err;
1380         int tmp;
1381         unsigned long timeout;
1382
1383         dev = alloc_etherdev(sizeof(struct greth_private));
1384
1385         if (dev == NULL)
1386                 return -ENOMEM;
1387
1388         greth = netdev_priv(dev);
1389         greth->netdev = dev;
1390         greth->dev = &ofdev->dev;
1391
1392         if (greth_debug > 0)
1393                 greth->msg_enable = greth_debug;
1394         else
1395                 greth->msg_enable = GRETH_DEF_MSG_ENABLE;
1396
1397         spin_lock_init(&greth->devlock);
1398
1399         greth->regs = of_ioremap(&ofdev->resource[0], 0,
1400                                  resource_size(&ofdev->resource[0]),
1401                                  "grlib-greth regs");
1402
1403         if (greth->regs == NULL) {
1404                 if (netif_msg_probe(greth))
1405                         dev_err(greth->dev, "ioremap failure.\n");
1406                 err = -EIO;
1407                 goto error1;
1408         }
1409
1410         regs = (struct greth_regs *) greth->regs;
1411         greth->irq = ofdev->archdata.irqs[0];
1412
1413         dev_set_drvdata(greth->dev, dev);
1414         SET_NETDEV_DEV(dev, greth->dev);
1415
1416         if (netif_msg_probe(greth))
1417                 dev_dbg(greth->dev, "reseting controller.\n");
1418
1419         /* Reset the controller. */
1420         GRETH_REGSAVE(regs->control, GRETH_RESET);
1421
1422         /* Wait for MAC to reset itself */
1423         timeout = jiffies + HZ/100;
1424         while (GRETH_REGLOAD(regs->control) & GRETH_RESET) {
1425                 if (time_after(jiffies, timeout)) {
1426                         err = -EIO;
1427                         if (netif_msg_probe(greth))
1428                                 dev_err(greth->dev, "timeout when waiting for reset.\n");
1429                         goto error2;
1430                 }
1431         }
1432
1433         /* Get default PHY address  */
1434         greth->phyaddr = (GRETH_REGLOAD(regs->mdio) >> 11) & 0x1F;
1435
1436         /* Check if we have GBIT capable MAC */
1437         tmp = GRETH_REGLOAD(regs->control);
1438         greth->gbit_mac = (tmp >> 27) & 1;
1439
1440         /* Check for multicast capability */
1441         greth->multicast = (tmp >> 25) & 1;
1442
1443         greth->edcl = (tmp >> 31) & 1;
1444
1445         /* If we have EDCL we disable the EDCL speed-duplex FSM so
1446          * it doesn't interfere with the software */
1447         if (greth->edcl != 0)
1448                 GRETH_REGORIN(regs->control, GRETH_CTRL_DISDUPLEX);
1449
1450         /* Check if MAC can handle MDIO interrupts */
1451         greth->mdio_int_en = (tmp >> 26) & 1;
1452
1453         err = greth_mdio_init(greth);
1454         if (err) {
1455                 if (netif_msg_probe(greth))
1456                         dev_err(greth->dev, "failed to register MDIO bus\n");
1457                 goto error2;
1458         }
1459
1460         /* Allocate TX descriptor ring in coherent memory */
1461         greth->tx_bd_base = (struct greth_bd *) dma_alloc_coherent(greth->dev,
1462                                                                    1024,
1463                                                                    &greth->tx_bd_base_phys,
1464                                                                    GFP_KERNEL);
1465
1466         if (!greth->tx_bd_base) {
1467                 if (netif_msg_probe(greth))
1468                         dev_err(&dev->dev, "could not allocate descriptor memory.\n");
1469                 err = -ENOMEM;
1470                 goto error3;
1471         }
1472
1473         memset(greth->tx_bd_base, 0, 1024);
1474
1475         /* Allocate RX descriptor ring in coherent memory */
1476         greth->rx_bd_base = (struct greth_bd *) dma_alloc_coherent(greth->dev,
1477                                                                    1024,
1478                                                                    &greth->rx_bd_base_phys,
1479                                                                    GFP_KERNEL);
1480
1481         if (!greth->rx_bd_base) {
1482                 if (netif_msg_probe(greth))
1483                         dev_err(greth->dev, "could not allocate descriptor memory.\n");
1484                 err = -ENOMEM;
1485                 goto error4;
1486         }
1487
1488         memset(greth->rx_bd_base, 0, 1024);
1489
1490         /* Get MAC address from: module param, OF property or ID prom */
1491         for (i = 0; i < 6; i++) {
1492                 if (macaddr[i] != 0)
1493                         break;
1494         }
1495         if (i == 6) {
1496                 const unsigned char *addr;
1497                 int len;
1498                 addr = of_get_property(ofdev->dev.of_node, "local-mac-address",
1499                                         &len);
1500                 if (addr != NULL && len == 6) {
1501                         for (i = 0; i < 6; i++)
1502                                 macaddr[i] = (unsigned int) addr[i];
1503                 } else {
1504 #ifdef CONFIG_SPARC
1505                         for (i = 0; i < 6; i++)
1506                                 macaddr[i] = (unsigned int) idprom->id_ethaddr[i];
1507 #endif
1508                 }
1509         }
1510
1511         for (i = 0; i < 6; i++)
1512                 dev->dev_addr[i] = macaddr[i];
1513
1514         macaddr[5]++;
1515
1516         if (!is_valid_ether_addr(&dev->dev_addr[0])) {
1517                 if (netif_msg_probe(greth))
1518                         dev_err(greth->dev, "no valid ethernet address, aborting.\n");
1519                 err = -EINVAL;
1520                 goto error5;
1521         }
1522
1523         GRETH_REGSAVE(regs->esa_msb, dev->dev_addr[0] << 8 | dev->dev_addr[1]);
1524         GRETH_REGSAVE(regs->esa_lsb, dev->dev_addr[2] << 24 | dev->dev_addr[3] << 16 |
1525                       dev->dev_addr[4] << 8 | dev->dev_addr[5]);
1526
1527         /* Clear all pending interrupts except PHY irq */
1528         GRETH_REGSAVE(regs->status, 0xFF);
1529
1530         if (greth->gbit_mac) {
1531                 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM |
1532                         NETIF_F_RXCSUM;
1533                 dev->features = dev->hw_features | NETIF_F_HIGHDMA;
1534                 greth_netdev_ops.ndo_start_xmit = greth_start_xmit_gbit;
1535         }
1536
1537         if (greth->multicast) {
1538                 greth_netdev_ops.ndo_set_rx_mode = 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         err = register_netdev(dev);
1548         if (err) {
1549                 if (netif_msg_probe(greth))
1550                         dev_err(greth->dev, "netdevice registration failed.\n");
1551                 goto error5;
1552         }
1553
1554         /* setup NAPI */
1555         netif_napi_add(dev, &greth->napi, greth_poll, 64);
1556
1557         return 0;
1558
1559 error5:
1560         dma_free_coherent(greth->dev, 1024, greth->rx_bd_base, greth->rx_bd_base_phys);
1561 error4:
1562         dma_free_coherent(greth->dev, 1024, greth->tx_bd_base, greth->tx_bd_base_phys);
1563 error3:
1564         mdiobus_unregister(greth->mdio);
1565 error2:
1566         of_iounmap(&ofdev->resource[0], greth->regs, resource_size(&ofdev->resource[0]));
1567 error1:
1568         free_netdev(dev);
1569         return err;
1570 }
1571
1572 static int __devexit greth_of_remove(struct platform_device *of_dev)
1573 {
1574         struct net_device *ndev = dev_get_drvdata(&of_dev->dev);
1575         struct greth_private *greth = netdev_priv(ndev);
1576
1577         /* Free descriptor areas */
1578         dma_free_coherent(&of_dev->dev, 1024, greth->rx_bd_base, greth->rx_bd_base_phys);
1579
1580         dma_free_coherent(&of_dev->dev, 1024, greth->tx_bd_base, greth->tx_bd_base_phys);
1581
1582         dev_set_drvdata(&of_dev->dev, NULL);
1583
1584         if (greth->phy)
1585                 phy_stop(greth->phy);
1586         mdiobus_unregister(greth->mdio);
1587
1588         unregister_netdev(ndev);
1589         free_netdev(ndev);
1590
1591         of_iounmap(&of_dev->resource[0], greth->regs, resource_size(&of_dev->resource[0]));
1592
1593         return 0;
1594 }
1595
1596 static struct of_device_id greth_of_match[] = {
1597         {
1598          .name = "GAISLER_ETHMAC",
1599          },
1600         {
1601          .name = "01_01d",
1602          },
1603         {},
1604 };
1605
1606 MODULE_DEVICE_TABLE(of, greth_of_match);
1607
1608 static struct platform_driver greth_of_driver = {
1609         .driver = {
1610                 .name = "grlib-greth",
1611                 .owner = THIS_MODULE,
1612                 .of_match_table = greth_of_match,
1613         },
1614         .probe = greth_of_probe,
1615         .remove = __devexit_p(greth_of_remove),
1616 };
1617
1618 static int __init greth_init(void)
1619 {
1620         return platform_driver_register(&greth_of_driver);
1621 }
1622
1623 static void __exit greth_cleanup(void)
1624 {
1625         platform_driver_unregister(&greth_of_driver);
1626 }
1627
1628 module_init(greth_init);
1629 module_exit(greth_cleanup);
1630
1631 MODULE_AUTHOR("Aeroflex Gaisler AB.");
1632 MODULE_DESCRIPTION("Aeroflex Gaisler Ethernet MAC driver");
1633 MODULE_LICENSE("GPL");