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