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