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