blob: b307696e0f635817335776eef5d1e35aa19f5c9e [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
Daniel Hellstrombbe9e632011-01-14 03:02:38 +0000359 GRETH_REGSAVE(greth->regs->status, 0xFF);
360
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000361 napi_enable(&greth->napi);
362
363 greth_enable_irqs(greth);
364 greth_enable_tx(greth);
365 greth_enable_rx(greth);
366 return 0;
367
368}
369
370static int greth_close(struct net_device *dev)
371{
372 struct greth_private *greth = netdev_priv(dev);
373
374 napi_disable(&greth->napi);
375
Daniel Hellstrombbe9e632011-01-14 03:02:38 +0000376 greth_disable_irqs(greth);
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000377 greth_disable_tx(greth);
Daniel Hellstrombbe9e632011-01-14 03:02:38 +0000378 greth_disable_rx(greth);
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000379
380 netif_stop_queue(dev);
381
382 free_irq(greth->irq, (void *) dev);
383
384 greth_clean_rings(greth);
385
386 return 0;
387}
388
kirjanov@gmail.com41a655b2010-02-24 10:25:33 +0000389static netdev_tx_t
390greth_start_xmit(struct sk_buff *skb, struct net_device *dev)
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000391{
392 struct greth_private *greth = netdev_priv(dev);
393 struct greth_bd *bdp;
394 int err = NETDEV_TX_OK;
395 u32 status, dma_addr;
396
397 bdp = greth->tx_bd_base + greth->tx_next;
398
399 if (unlikely(greth->tx_free <= 0)) {
400 netif_stop_queue(dev);
401 return NETDEV_TX_BUSY;
402 }
403
404 if (netif_msg_pktdata(greth))
405 greth_print_tx_packet(skb);
406
407
408 if (unlikely(skb->len > MAX_FRAME_SIZE)) {
409 dev->stats.tx_errors++;
410 goto out;
411 }
412
413 dma_addr = greth_read_bd(&bdp->addr);
414
415 memcpy((unsigned char *) phys_to_virt(dma_addr), skb->data, skb->len);
416
417 dma_sync_single_for_device(greth->dev, dma_addr, skb->len, DMA_TO_DEVICE);
418
419 status = GRETH_BD_EN | (skb->len & GRETH_BD_LEN);
420
421 /* Wrap around descriptor ring */
422 if (greth->tx_next == GRETH_TXBD_NUM_MASK) {
423 status |= GRETH_BD_WR;
424 }
425
426 greth->tx_next = NEXT_TX(greth->tx_next);
427 greth->tx_free--;
428
429 /* No more descriptors */
430 if (unlikely(greth->tx_free == 0)) {
431
432 /* Free transmitted descriptors */
433 greth_clean_tx(dev);
434
435 /* If nothing was cleaned, stop queue & wait for irq */
436 if (unlikely(greth->tx_free == 0)) {
437 status |= GRETH_BD_IE;
438 netif_stop_queue(dev);
439 }
440 }
441
442 /* Write descriptor control word and enable transmission */
443 greth_write_bd(&bdp->stat, status);
444 greth_enable_tx(greth);
445
446out:
447 dev_kfree_skb(skb);
448 return err;
449}
450
451
kirjanov@gmail.com41a655b2010-02-24 10:25:33 +0000452static netdev_tx_t
453greth_start_xmit_gbit(struct sk_buff *skb, struct net_device *dev)
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000454{
455 struct greth_private *greth = netdev_priv(dev);
456 struct greth_bd *bdp;
457 u32 status = 0, dma_addr;
458 int curr_tx, nr_frags, i, err = NETDEV_TX_OK;
459
460 nr_frags = skb_shinfo(skb)->nr_frags;
461
462 if (greth->tx_free < nr_frags + 1) {
463 netif_stop_queue(dev);
464 err = NETDEV_TX_BUSY;
465 goto out;
466 }
467
468 if (netif_msg_pktdata(greth))
469 greth_print_tx_packet(skb);
470
471 if (unlikely(skb->len > MAX_FRAME_SIZE)) {
472 dev->stats.tx_errors++;
473 goto out;
474 }
475
476 /* Save skb pointer. */
477 greth->tx_skbuff[greth->tx_next] = skb;
478
479 /* Linear buf */
480 if (nr_frags != 0)
481 status = GRETH_TXBD_MORE;
482
483 status |= GRETH_TXBD_CSALL;
484 status |= skb_headlen(skb) & GRETH_BD_LEN;
485 if (greth->tx_next == GRETH_TXBD_NUM_MASK)
486 status |= GRETH_BD_WR;
487
488
489 bdp = greth->tx_bd_base + greth->tx_next;
490 greth_write_bd(&bdp->stat, status);
491 dma_addr = dma_map_single(greth->dev, skb->data, skb_headlen(skb), DMA_TO_DEVICE);
492
493 if (unlikely(dma_mapping_error(greth->dev, dma_addr)))
494 goto map_error;
495
496 greth_write_bd(&bdp->addr, dma_addr);
497
498 curr_tx = NEXT_TX(greth->tx_next);
499
500 /* Frags */
501 for (i = 0; i < nr_frags; i++) {
502 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
503 greth->tx_skbuff[curr_tx] = NULL;
504 bdp = greth->tx_bd_base + curr_tx;
505
506 status = GRETH_TXBD_CSALL;
507 status |= frag->size & GRETH_BD_LEN;
508
509 /* Wrap around descriptor ring */
510 if (curr_tx == GRETH_TXBD_NUM_MASK)
511 status |= GRETH_BD_WR;
512
513 /* More fragments left */
514 if (i < nr_frags - 1)
515 status |= GRETH_TXBD_MORE;
516
517 /* ... last fragment, check if out of descriptors */
518 else if (greth->tx_free - nr_frags - 1 < (MAX_SKB_FRAGS + 1)) {
519
520 /* Enable interrupts and stop queue */
521 status |= GRETH_BD_IE;
522 netif_stop_queue(dev);
523 }
524
525 greth_write_bd(&bdp->stat, status);
526
527 dma_addr = dma_map_page(greth->dev,
528 frag->page,
529 frag->page_offset,
530 frag->size,
531 DMA_TO_DEVICE);
532
533 if (unlikely(dma_mapping_error(greth->dev, dma_addr)))
534 goto frag_map_error;
535
536 greth_write_bd(&bdp->addr, dma_addr);
537
538 curr_tx = NEXT_TX(curr_tx);
539 }
540
541 wmb();
542
543 /* Enable the descriptors that we configured ... */
544 for (i = 0; i < nr_frags + 1; i++) {
545 bdp = greth->tx_bd_base + greth->tx_next;
546 greth_write_bd(&bdp->stat, greth_read_bd(&bdp->stat) | GRETH_BD_EN);
547 greth->tx_next = NEXT_TX(greth->tx_next);
548 greth->tx_free--;
549 }
550
551 greth_enable_tx(greth);
552
553 return NETDEV_TX_OK;
554
555frag_map_error:
556 /* Unmap SKB mappings that succeeded */
557 for (i = 0; greth->tx_next + i != curr_tx; i++) {
558 bdp = greth->tx_bd_base + greth->tx_next + i;
559 dma_unmap_single(greth->dev,
560 greth_read_bd(&bdp->addr),
561 greth_read_bd(&bdp->stat) & GRETH_BD_LEN,
562 DMA_TO_DEVICE);
563 }
564map_error:
565 if (net_ratelimit())
566 dev_warn(greth->dev, "Could not create TX DMA mapping\n");
567 dev_kfree_skb(skb);
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000568out:
569 return err;
570}
571
572
573static irqreturn_t greth_interrupt(int irq, void *dev_id)
574{
575 struct net_device *dev = dev_id;
576 struct greth_private *greth;
577 u32 status;
578 irqreturn_t retval = IRQ_NONE;
579
580 greth = netdev_priv(dev);
581
582 spin_lock(&greth->devlock);
583
584 /* Get the interrupt events that caused us to be here. */
585 status = GRETH_REGLOAD(greth->regs->status);
586
587 /* Handle rx and tx interrupts through poll */
588 if (status & (GRETH_INT_RX | GRETH_INT_TX)) {
589
590 /* Clear interrupt status */
591 GRETH_REGORIN(greth->regs->status,
592 status & (GRETH_INT_RX | GRETH_INT_TX));
593
594 retval = IRQ_HANDLED;
595
596 /* Disable interrupts and schedule poll() */
597 greth_disable_irqs(greth);
598 napi_schedule(&greth->napi);
599 }
600
601 mmiowb();
602 spin_unlock(&greth->devlock);
603
604 return retval;
605}
606
607static void greth_clean_tx(struct net_device *dev)
608{
609 struct greth_private *greth;
610 struct greth_bd *bdp;
611 u32 stat;
612
613 greth = netdev_priv(dev);
614
615 while (1) {
616 bdp = greth->tx_bd_base + greth->tx_last;
617 stat = greth_read_bd(&bdp->stat);
618
619 if (unlikely(stat & GRETH_BD_EN))
620 break;
621
622 if (greth->tx_free == GRETH_TXBD_NUM)
623 break;
624
625 /* Check status for errors */
626 if (unlikely(stat & GRETH_TXBD_STATUS)) {
627 dev->stats.tx_errors++;
628 if (stat & GRETH_TXBD_ERR_AL)
629 dev->stats.tx_aborted_errors++;
630 if (stat & GRETH_TXBD_ERR_UE)
631 dev->stats.tx_fifo_errors++;
632 }
633 dev->stats.tx_packets++;
634 greth->tx_last = NEXT_TX(greth->tx_last);
635 greth->tx_free++;
636 }
637
638 if (greth->tx_free > 0) {
639 netif_wake_queue(dev);
640 }
641
642}
643
644static inline void greth_update_tx_stats(struct net_device *dev, u32 stat)
645{
646 /* Check status for errors */
647 if (unlikely(stat & GRETH_TXBD_STATUS)) {
648 dev->stats.tx_errors++;
649 if (stat & GRETH_TXBD_ERR_AL)
650 dev->stats.tx_aborted_errors++;
651 if (stat & GRETH_TXBD_ERR_UE)
652 dev->stats.tx_fifo_errors++;
653 if (stat & GRETH_TXBD_ERR_LC)
654 dev->stats.tx_aborted_errors++;
655 }
656 dev->stats.tx_packets++;
657}
658
659static void greth_clean_tx_gbit(struct net_device *dev)
660{
661 struct greth_private *greth;
662 struct greth_bd *bdp, *bdp_last_frag;
663 struct sk_buff *skb;
664 u32 stat;
665 int nr_frags, i;
666
667 greth = netdev_priv(dev);
668
669 while (greth->tx_free < GRETH_TXBD_NUM) {
670
671 skb = greth->tx_skbuff[greth->tx_last];
672
673 nr_frags = skb_shinfo(skb)->nr_frags;
674
675 /* We only clean fully completed SKBs */
676 bdp_last_frag = greth->tx_bd_base + SKIP_TX(greth->tx_last, nr_frags);
677 stat = bdp_last_frag->stat;
678
679 if (stat & GRETH_BD_EN)
680 break;
681
682 greth->tx_skbuff[greth->tx_last] = NULL;
683
684 greth_update_tx_stats(dev, stat);
685
686 bdp = greth->tx_bd_base + greth->tx_last;
687
688 greth->tx_last = NEXT_TX(greth->tx_last);
689
690 dma_unmap_single(greth->dev,
691 greth_read_bd(&bdp->addr),
692 skb_headlen(skb),
693 DMA_TO_DEVICE);
694
695 for (i = 0; i < nr_frags; i++) {
696 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
697 bdp = greth->tx_bd_base + greth->tx_last;
698
699 dma_unmap_page(greth->dev,
700 greth_read_bd(&bdp->addr),
701 frag->size,
702 DMA_TO_DEVICE);
703
704 greth->tx_last = NEXT_TX(greth->tx_last);
705 }
706 greth->tx_free += nr_frags+1;
707 dev_kfree_skb(skb);
708 }
709 if (greth->tx_free > (MAX_SKB_FRAGS + 1)) {
710 netif_wake_queue(dev);
711 }
712}
713
714static int greth_pending_packets(struct greth_private *greth)
715{
716 struct greth_bd *bdp;
717 u32 status;
718 bdp = greth->rx_bd_base + greth->rx_cur;
719 status = greth_read_bd(&bdp->stat);
720 if (status & GRETH_BD_EN)
721 return 0;
722 else
723 return 1;
724}
725
726static int greth_rx(struct net_device *dev, int limit)
727{
728 struct greth_private *greth;
729 struct greth_bd *bdp;
730 struct sk_buff *skb;
731 int pkt_len;
732 int bad, count;
733 u32 status, dma_addr;
734
735 greth = netdev_priv(dev);
736
737 for (count = 0; count < limit; ++count) {
738
739 bdp = greth->rx_bd_base + greth->rx_cur;
740 status = greth_read_bd(&bdp->stat);
741 dma_addr = greth_read_bd(&bdp->addr);
742 bad = 0;
743
744 if (unlikely(status & GRETH_BD_EN)) {
745 break;
746 }
747
748 /* Check status for errors. */
749 if (unlikely(status & GRETH_RXBD_STATUS)) {
750 if (status & GRETH_RXBD_ERR_FT) {
751 dev->stats.rx_length_errors++;
752 bad = 1;
753 }
754 if (status & (GRETH_RXBD_ERR_AE | GRETH_RXBD_ERR_OE)) {
755 dev->stats.rx_frame_errors++;
756 bad = 1;
757 }
758 if (status & GRETH_RXBD_ERR_CRC) {
759 dev->stats.rx_crc_errors++;
760 bad = 1;
761 }
762 }
763 if (unlikely(bad)) {
764 dev->stats.rx_errors++;
765
766 } else {
767
768 pkt_len = status & GRETH_BD_LEN;
769
770 skb = netdev_alloc_skb(dev, pkt_len + NET_IP_ALIGN);
771
772 if (unlikely(skb == NULL)) {
773
774 if (net_ratelimit())
775 dev_warn(&dev->dev, "low on memory - " "packet dropped\n");
776
777 dev->stats.rx_dropped++;
778
779 } else {
780 skb_reserve(skb, NET_IP_ALIGN);
781 skb->dev = dev;
782
783 dma_sync_single_for_cpu(greth->dev,
784 dma_addr,
785 pkt_len,
786 DMA_FROM_DEVICE);
787
788 if (netif_msg_pktdata(greth))
789 greth_print_rx_packet(phys_to_virt(dma_addr), pkt_len);
790
791 memcpy(skb_put(skb, pkt_len), phys_to_virt(dma_addr), pkt_len);
792
793 skb->protocol = eth_type_trans(skb, dev);
794 dev->stats.rx_packets++;
795 netif_receive_skb(skb);
796 }
797 }
798
799 status = GRETH_BD_EN | GRETH_BD_IE;
800 if (greth->rx_cur == GRETH_RXBD_NUM_MASK) {
801 status |= GRETH_BD_WR;
802 }
803
804 wmb();
805 greth_write_bd(&bdp->stat, status);
806
807 dma_sync_single_for_device(greth->dev, dma_addr, MAX_FRAME_SIZE, DMA_FROM_DEVICE);
808
809 greth_enable_rx(greth);
810
811 greth->rx_cur = NEXT_RX(greth->rx_cur);
812 }
813
814 return count;
815}
816
817static inline int hw_checksummed(u32 status)
818{
819
820 if (status & GRETH_RXBD_IP_FRAG)
821 return 0;
822
823 if (status & GRETH_RXBD_IP && status & GRETH_RXBD_IP_CSERR)
824 return 0;
825
826 if (status & GRETH_RXBD_UDP && status & GRETH_RXBD_UDP_CSERR)
827 return 0;
828
829 if (status & GRETH_RXBD_TCP && status & GRETH_RXBD_TCP_CSERR)
830 return 0;
831
832 return 1;
833}
834
835static int greth_rx_gbit(struct net_device *dev, int limit)
836{
837 struct greth_private *greth;
838 struct greth_bd *bdp;
839 struct sk_buff *skb, *newskb;
840 int pkt_len;
841 int bad, count = 0;
842 u32 status, dma_addr;
843
844 greth = netdev_priv(dev);
845
846 for (count = 0; count < limit; ++count) {
847
848 bdp = greth->rx_bd_base + greth->rx_cur;
849 skb = greth->rx_skbuff[greth->rx_cur];
850 status = greth_read_bd(&bdp->stat);
851 bad = 0;
852
853 if (status & GRETH_BD_EN)
854 break;
855
856 /* Check status for errors. */
857 if (unlikely(status & GRETH_RXBD_STATUS)) {
858
859 if (status & GRETH_RXBD_ERR_FT) {
860 dev->stats.rx_length_errors++;
861 bad = 1;
862 } else if (status &
863 (GRETH_RXBD_ERR_AE | GRETH_RXBD_ERR_OE | GRETH_RXBD_ERR_LE)) {
864 dev->stats.rx_frame_errors++;
865 bad = 1;
866 } else if (status & GRETH_RXBD_ERR_CRC) {
867 dev->stats.rx_crc_errors++;
868 bad = 1;
869 }
870 }
871
872 /* Allocate new skb to replace current */
873 newskb = netdev_alloc_skb(dev, MAX_FRAME_SIZE + NET_IP_ALIGN);
874
875 if (!bad && newskb) {
876 skb_reserve(newskb, NET_IP_ALIGN);
877
878 dma_addr = dma_map_single(greth->dev,
879 newskb->data,
880 MAX_FRAME_SIZE + NET_IP_ALIGN,
881 DMA_FROM_DEVICE);
882
883 if (!dma_mapping_error(greth->dev, dma_addr)) {
884 /* Process the incoming frame. */
885 pkt_len = status & GRETH_BD_LEN;
886
887 dma_unmap_single(greth->dev,
888 greth_read_bd(&bdp->addr),
889 MAX_FRAME_SIZE + NET_IP_ALIGN,
890 DMA_FROM_DEVICE);
891
892 if (netif_msg_pktdata(greth))
893 greth_print_rx_packet(phys_to_virt(greth_read_bd(&bdp->addr)), pkt_len);
894
895 skb_put(skb, pkt_len);
896
897 if (greth->flags & GRETH_FLAG_RX_CSUM && hw_checksummed(status))
898 skb->ip_summed = CHECKSUM_UNNECESSARY;
899 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -0700900 skb_checksum_none_assert(skb);
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000901
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000902 skb->protocol = eth_type_trans(skb, dev);
903 dev->stats.rx_packets++;
904 netif_receive_skb(skb);
905
906 greth->rx_skbuff[greth->rx_cur] = newskb;
907 greth_write_bd(&bdp->addr, dma_addr);
908 } else {
909 if (net_ratelimit())
910 dev_warn(greth->dev, "Could not create DMA mapping, dropping packet\n");
911 dev_kfree_skb(newskb);
912 dev->stats.rx_dropped++;
913 }
914 } else {
915 if (net_ratelimit())
916 dev_warn(greth->dev, "Could not allocate SKB, dropping packet\n");
917 dev->stats.rx_dropped++;
918 }
919
920 status = GRETH_BD_EN | GRETH_BD_IE;
921 if (greth->rx_cur == GRETH_RXBD_NUM_MASK) {
922 status |= GRETH_BD_WR;
923 }
924
925 wmb();
926 greth_write_bd(&bdp->stat, status);
927 greth_enable_rx(greth);
928 greth->rx_cur = NEXT_RX(greth->rx_cur);
929 }
930
931 return count;
932
933}
934
935static int greth_poll(struct napi_struct *napi, int budget)
936{
937 struct greth_private *greth;
938 int work_done = 0;
939 greth = container_of(napi, struct greth_private, napi);
940
941 if (greth->gbit_mac) {
942 greth_clean_tx_gbit(greth->netdev);
943 } else {
944 greth_clean_tx(greth->netdev);
945 }
946
947restart_poll:
948 if (greth->gbit_mac) {
949 work_done += greth_rx_gbit(greth->netdev, budget - work_done);
950 } else {
951 work_done += greth_rx(greth->netdev, budget - work_done);
952 }
953
954 if (work_done < budget) {
955
956 napi_complete(napi);
957
958 if (greth_pending_packets(greth)) {
959 napi_reschedule(napi);
960 goto restart_poll;
961 }
962 }
963
964 greth_enable_irqs(greth);
965 return work_done;
966}
967
968static int greth_set_mac_add(struct net_device *dev, void *p)
969{
970 struct sockaddr *addr = p;
971 struct greth_private *greth;
972 struct greth_regs *regs;
973
kirjanov@gmail.com6e037182010-02-19 05:00:52 +0000974 greth = netdev_priv(dev);
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000975 regs = (struct greth_regs *) greth->regs;
976
977 if (!is_valid_ether_addr(addr->sa_data))
978 return -EINVAL;
979
980 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
981
982 GRETH_REGSAVE(regs->esa_msb, addr->sa_data[0] << 8 | addr->sa_data[1]);
983 GRETH_REGSAVE(regs->esa_lsb,
984 addr->sa_data[2] << 24 | addr->
985 sa_data[3] << 16 | addr->sa_data[4] << 8 | addr->sa_data[5]);
986 return 0;
987}
988
989static u32 greth_hash_get_index(__u8 *addr)
990{
991 return (ether_crc(6, addr)) & 0x3F;
992}
993
994static void greth_set_hash_filter(struct net_device *dev)
995{
Jiri Pirko22bedad32010-04-01 21:22:57 +0000996 struct netdev_hw_addr *ha;
kirjanov@gmail.com6e037182010-02-19 05:00:52 +0000997 struct greth_private *greth = netdev_priv(dev);
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000998 struct greth_regs *regs = (struct greth_regs *) greth->regs;
999 u32 mc_filter[2];
kirjanov@gmail.com6e037182010-02-19 05:00:52 +00001000 unsigned int bitnr;
Kristoffer Glembod4c41132010-02-15 03:33:44 +00001001
1002 mc_filter[0] = mc_filter[1] = 0;
1003
Jiri Pirko22bedad32010-04-01 21:22:57 +00001004 netdev_for_each_mc_addr(ha, dev) {
1005 bitnr = greth_hash_get_index(ha->addr);
Kristoffer Glembod4c41132010-02-15 03:33:44 +00001006 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
1007 }
1008
1009 GRETH_REGSAVE(regs->hash_msb, mc_filter[1]);
1010 GRETH_REGSAVE(regs->hash_lsb, mc_filter[0]);
1011}
1012
1013static void greth_set_multicast_list(struct net_device *dev)
1014{
1015 int cfg;
1016 struct greth_private *greth = netdev_priv(dev);
1017 struct greth_regs *regs = (struct greth_regs *) greth->regs;
1018
1019 cfg = GRETH_REGLOAD(regs->control);
1020 if (dev->flags & IFF_PROMISC)
1021 cfg |= GRETH_CTRL_PR;
1022 else
1023 cfg &= ~GRETH_CTRL_PR;
1024
1025 if (greth->multicast) {
1026 if (dev->flags & IFF_ALLMULTI) {
1027 GRETH_REGSAVE(regs->hash_msb, -1);
1028 GRETH_REGSAVE(regs->hash_lsb, -1);
1029 cfg |= GRETH_CTRL_MCEN;
1030 GRETH_REGSAVE(regs->control, cfg);
1031 return;
1032 }
1033
kirjanov@gmail.com6e037182010-02-19 05:00:52 +00001034 if (netdev_mc_empty(dev)) {
Kristoffer Glembod4c41132010-02-15 03:33:44 +00001035 cfg &= ~GRETH_CTRL_MCEN;
1036 GRETH_REGSAVE(regs->control, cfg);
1037 return;
1038 }
1039
1040 /* Setup multicast filter */
1041 greth_set_hash_filter(dev);
1042 cfg |= GRETH_CTRL_MCEN;
1043 }
1044 GRETH_REGSAVE(regs->control, cfg);
1045}
1046
1047static u32 greth_get_msglevel(struct net_device *dev)
1048{
1049 struct greth_private *greth = netdev_priv(dev);
1050 return greth->msg_enable;
1051}
1052
1053static void greth_set_msglevel(struct net_device *dev, u32 value)
1054{
1055 struct greth_private *greth = netdev_priv(dev);
1056 greth->msg_enable = value;
1057}
1058static int greth_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1059{
1060 struct greth_private *greth = netdev_priv(dev);
1061 struct phy_device *phy = greth->phy;
1062
1063 if (!phy)
1064 return -ENODEV;
1065
1066 return phy_ethtool_gset(phy, cmd);
1067}
1068
1069static int greth_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1070{
1071 struct greth_private *greth = netdev_priv(dev);
1072 struct phy_device *phy = greth->phy;
1073
1074 if (!phy)
1075 return -ENODEV;
1076
1077 return phy_ethtool_sset(phy, cmd);
1078}
1079
1080static int greth_get_regs_len(struct net_device *dev)
1081{
1082 return sizeof(struct greth_regs);
1083}
1084
1085static void greth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
1086{
1087 struct greth_private *greth = netdev_priv(dev);
1088
1089 strncpy(info->driver, dev_driver_string(greth->dev), 32);
1090 strncpy(info->version, "revision: 1.0", 32);
1091 strncpy(info->bus_info, greth->dev->bus->name, 32);
1092 strncpy(info->fw_version, "N/A", 32);
1093 info->eedump_len = 0;
1094 info->regdump_len = sizeof(struct greth_regs);
1095}
1096
1097static void greth_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *p)
1098{
1099 int i;
1100 struct greth_private *greth = netdev_priv(dev);
1101 u32 __iomem *greth_regs = (u32 __iomem *) greth->regs;
1102 u32 *buff = p;
1103
1104 for (i = 0; i < sizeof(struct greth_regs) / sizeof(u32); i++)
1105 buff[i] = greth_read_bd(&greth_regs[i]);
1106}
1107
1108static u32 greth_get_rx_csum(struct net_device *dev)
1109{
1110 struct greth_private *greth = netdev_priv(dev);
1111 return (greth->flags & GRETH_FLAG_RX_CSUM) != 0;
1112}
1113
1114static int greth_set_rx_csum(struct net_device *dev, u32 data)
1115{
1116 struct greth_private *greth = netdev_priv(dev);
1117
1118 spin_lock_bh(&greth->devlock);
1119
1120 if (data)
1121 greth->flags |= GRETH_FLAG_RX_CSUM;
1122 else
1123 greth->flags &= ~GRETH_FLAG_RX_CSUM;
1124
1125 spin_unlock_bh(&greth->devlock);
1126
1127 return 0;
1128}
1129
1130static u32 greth_get_tx_csum(struct net_device *dev)
1131{
1132 return (dev->features & NETIF_F_IP_CSUM) != 0;
1133}
1134
1135static int greth_set_tx_csum(struct net_device *dev, u32 data)
1136{
1137 netif_tx_lock_bh(dev);
1138 ethtool_op_set_tx_csum(dev, data);
1139 netif_tx_unlock_bh(dev);
1140 return 0;
1141}
1142
1143static const struct ethtool_ops greth_ethtool_ops = {
1144 .get_msglevel = greth_get_msglevel,
1145 .set_msglevel = greth_set_msglevel,
1146 .get_settings = greth_get_settings,
1147 .set_settings = greth_set_settings,
1148 .get_drvinfo = greth_get_drvinfo,
1149 .get_regs_len = greth_get_regs_len,
1150 .get_regs = greth_get_regs,
1151 .get_rx_csum = greth_get_rx_csum,
1152 .set_rx_csum = greth_set_rx_csum,
1153 .get_tx_csum = greth_get_tx_csum,
1154 .set_tx_csum = greth_set_tx_csum,
1155 .get_link = ethtool_op_get_link,
1156};
1157
1158static struct net_device_ops greth_netdev_ops = {
1159 .ndo_open = greth_open,
1160 .ndo_stop = greth_close,
1161 .ndo_start_xmit = greth_start_xmit,
1162 .ndo_set_mac_address = greth_set_mac_add,
kirjanov@gmail.com6e037182010-02-19 05:00:52 +00001163 .ndo_validate_addr = eth_validate_addr,
Kristoffer Glembod4c41132010-02-15 03:33:44 +00001164};
1165
1166static inline int wait_for_mdio(struct greth_private *greth)
1167{
1168 unsigned long timeout = jiffies + 4*HZ/100;
1169 while (GRETH_REGLOAD(greth->regs->mdio) & GRETH_MII_BUSY) {
1170 if (time_after(jiffies, timeout))
1171 return 0;
1172 }
1173 return 1;
1174}
1175
1176static int greth_mdio_read(struct mii_bus *bus, int phy, int reg)
1177{
1178 struct greth_private *greth = bus->priv;
1179 int data;
1180
1181 if (!wait_for_mdio(greth))
1182 return -EBUSY;
1183
1184 GRETH_REGSAVE(greth->regs->mdio, ((phy & 0x1F) << 11) | ((reg & 0x1F) << 6) | 2);
1185
1186 if (!wait_for_mdio(greth))
1187 return -EBUSY;
1188
1189 if (!(GRETH_REGLOAD(greth->regs->mdio) & GRETH_MII_NVALID)) {
1190 data = (GRETH_REGLOAD(greth->regs->mdio) >> 16) & 0xFFFF;
1191 return data;
1192
1193 } else {
1194 return -1;
1195 }
1196}
1197
1198static int greth_mdio_write(struct mii_bus *bus, int phy, int reg, u16 val)
1199{
1200 struct greth_private *greth = bus->priv;
1201
1202 if (!wait_for_mdio(greth))
1203 return -EBUSY;
1204
1205 GRETH_REGSAVE(greth->regs->mdio,
1206 ((val & 0xFFFF) << 16) | ((phy & 0x1F) << 11) | ((reg & 0x1F) << 6) | 1);
1207
1208 if (!wait_for_mdio(greth))
1209 return -EBUSY;
1210
1211 return 0;
1212}
1213
1214static int greth_mdio_reset(struct mii_bus *bus)
1215{
1216 return 0;
1217}
1218
1219static void greth_link_change(struct net_device *dev)
1220{
1221 struct greth_private *greth = netdev_priv(dev);
1222 struct phy_device *phydev = greth->phy;
1223 unsigned long flags;
1224
1225 int status_change = 0;
1226
1227 spin_lock_irqsave(&greth->devlock, flags);
1228
1229 if (phydev->link) {
1230
1231 if ((greth->speed != phydev->speed) || (greth->duplex != phydev->duplex)) {
1232
1233 GRETH_REGANDIN(greth->regs->control,
1234 ~(GRETH_CTRL_FD | GRETH_CTRL_SP | GRETH_CTRL_GB));
1235
1236 if (phydev->duplex)
1237 GRETH_REGORIN(greth->regs->control, GRETH_CTRL_FD);
1238
1239 if (phydev->speed == SPEED_100) {
1240
1241 GRETH_REGORIN(greth->regs->control, GRETH_CTRL_SP);
1242 }
1243
1244 else if (phydev->speed == SPEED_1000)
1245 GRETH_REGORIN(greth->regs->control, GRETH_CTRL_GB);
1246
1247 greth->speed = phydev->speed;
1248 greth->duplex = phydev->duplex;
1249 status_change = 1;
1250 }
1251 }
1252
1253 if (phydev->link != greth->link) {
1254 if (!phydev->link) {
1255 greth->speed = 0;
1256 greth->duplex = -1;
1257 }
1258 greth->link = phydev->link;
1259
1260 status_change = 1;
1261 }
1262
1263 spin_unlock_irqrestore(&greth->devlock, flags);
1264
1265 if (status_change) {
1266 if (phydev->link)
1267 pr_debug("%s: link up (%d/%s)\n",
1268 dev->name, phydev->speed,
1269 DUPLEX_FULL == phydev->duplex ? "Full" : "Half");
1270 else
1271 pr_debug("%s: link down\n", dev->name);
1272 }
1273}
1274
1275static int greth_mdio_probe(struct net_device *dev)
1276{
1277 struct greth_private *greth = netdev_priv(dev);
1278 struct phy_device *phy = NULL;
kirjanov@gmail.com6e037182010-02-19 05:00:52 +00001279 int ret;
Kristoffer Glembod4c41132010-02-15 03:33:44 +00001280
1281 /* Find the first PHY */
kirjanov@gmail.com6e037182010-02-19 05:00:52 +00001282 phy = phy_find_first(greth->mdio);
1283
Kristoffer Glembod4c41132010-02-15 03:33:44 +00001284 if (!phy) {
1285 if (netif_msg_probe(greth))
1286 dev_err(&dev->dev, "no PHY found\n");
1287 return -ENXIO;
1288 }
1289
kirjanov@gmail.com6e037182010-02-19 05:00:52 +00001290 ret = phy_connect_direct(dev, phy, &greth_link_change,
1291 0, greth->gbit_mac ?
1292 PHY_INTERFACE_MODE_GMII :
1293 PHY_INTERFACE_MODE_MII);
1294 if (ret) {
1295 if (netif_msg_ifup(greth))
1296 dev_err(&dev->dev, "could not attach to PHY\n");
1297 return ret;
1298 }
Kristoffer Glembod4c41132010-02-15 03:33:44 +00001299
1300 if (greth->gbit_mac)
1301 phy->supported &= PHY_GBIT_FEATURES;
1302 else
1303 phy->supported &= PHY_BASIC_FEATURES;
1304
1305 phy->advertising = phy->supported;
1306
Kristoffer Glembod4c41132010-02-15 03:33:44 +00001307 greth->link = 0;
1308 greth->speed = 0;
1309 greth->duplex = -1;
1310 greth->phy = phy;
1311
1312 return 0;
1313}
1314
1315static inline int phy_aneg_done(struct phy_device *phydev)
1316{
1317 int retval;
1318
1319 retval = phy_read(phydev, MII_BMSR);
1320
1321 return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
1322}
1323
1324static int greth_mdio_init(struct greth_private *greth)
1325{
1326 int ret, phy;
1327 unsigned long timeout;
1328
1329 greth->mdio = mdiobus_alloc();
1330 if (!greth->mdio) {
1331 return -ENOMEM;
1332 }
1333
1334 greth->mdio->name = "greth-mdio";
1335 snprintf(greth->mdio->id, MII_BUS_ID_SIZE, "%s-%d", greth->mdio->name, greth->irq);
1336 greth->mdio->read = greth_mdio_read;
1337 greth->mdio->write = greth_mdio_write;
1338 greth->mdio->reset = greth_mdio_reset;
1339 greth->mdio->priv = greth;
1340
1341 greth->mdio->irq = greth->mdio_irqs;
1342
1343 for (phy = 0; phy < PHY_MAX_ADDR; phy++)
1344 greth->mdio->irq[phy] = PHY_POLL;
1345
1346 ret = mdiobus_register(greth->mdio);
1347 if (ret) {
1348 goto error;
1349 }
1350
1351 ret = greth_mdio_probe(greth->netdev);
1352 if (ret) {
1353 if (netif_msg_probe(greth))
1354 dev_err(&greth->netdev->dev, "failed to probe MDIO bus\n");
1355 goto unreg_mdio;
1356 }
1357
1358 phy_start(greth->phy);
1359
1360 /* If Ethernet debug link is used make autoneg happen right away */
1361 if (greth->edcl && greth_edcl == 1) {
1362 phy_start_aneg(greth->phy);
1363 timeout = jiffies + 6*HZ;
1364 while (!phy_aneg_done(greth->phy) && time_before(jiffies, timeout)) {
1365 }
1366 genphy_read_status(greth->phy);
1367 greth_link_change(greth->netdev);
1368 }
1369
1370 return 0;
1371
1372unreg_mdio:
1373 mdiobus_unregister(greth->mdio);
1374error:
1375 mdiobus_free(greth->mdio);
1376 return ret;
1377}
1378
1379/* Initialize the GRETH MAC */
Grant Likely2dc11582010-08-06 09:25:50 -06001380static int __devinit greth_of_probe(struct platform_device *ofdev, const struct of_device_id *match)
Kristoffer Glembod4c41132010-02-15 03:33:44 +00001381{
1382 struct net_device *dev;
1383 struct greth_private *greth;
1384 struct greth_regs *regs;
1385
1386 int i;
1387 int err;
1388 int tmp;
1389 unsigned long timeout;
1390
1391 dev = alloc_etherdev(sizeof(struct greth_private));
1392
1393 if (dev == NULL)
1394 return -ENOMEM;
1395
1396 greth = netdev_priv(dev);
1397 greth->netdev = dev;
1398 greth->dev = &ofdev->dev;
1399
1400 if (greth_debug > 0)
1401 greth->msg_enable = greth_debug;
1402 else
1403 greth->msg_enable = GRETH_DEF_MSG_ENABLE;
1404
1405 spin_lock_init(&greth->devlock);
1406
1407 greth->regs = of_ioremap(&ofdev->resource[0], 0,
1408 resource_size(&ofdev->resource[0]),
1409 "grlib-greth regs");
1410
1411 if (greth->regs == NULL) {
1412 if (netif_msg_probe(greth))
1413 dev_err(greth->dev, "ioremap failure.\n");
1414 err = -EIO;
1415 goto error1;
1416 }
1417
1418 regs = (struct greth_regs *) greth->regs;
Grant Likely19e48752010-08-08 00:23:26 -06001419 greth->irq = ofdev->archdata.irqs[0];
Kristoffer Glembod4c41132010-02-15 03:33:44 +00001420
1421 dev_set_drvdata(greth->dev, dev);
1422 SET_NETDEV_DEV(dev, greth->dev);
1423
1424 if (netif_msg_probe(greth))
1425 dev_dbg(greth->dev, "reseting controller.\n");
1426
1427 /* Reset the controller. */
1428 GRETH_REGSAVE(regs->control, GRETH_RESET);
1429
1430 /* Wait for MAC to reset itself */
1431 timeout = jiffies + HZ/100;
1432 while (GRETH_REGLOAD(regs->control) & GRETH_RESET) {
1433 if (time_after(jiffies, timeout)) {
1434 err = -EIO;
1435 if (netif_msg_probe(greth))
1436 dev_err(greth->dev, "timeout when waiting for reset.\n");
1437 goto error2;
1438 }
1439 }
1440
1441 /* Get default PHY address */
1442 greth->phyaddr = (GRETH_REGLOAD(regs->mdio) >> 11) & 0x1F;
1443
1444 /* Check if we have GBIT capable MAC */
1445 tmp = GRETH_REGLOAD(regs->control);
1446 greth->gbit_mac = (tmp >> 27) & 1;
1447
1448 /* Check for multicast capability */
1449 greth->multicast = (tmp >> 25) & 1;
1450
1451 greth->edcl = (tmp >> 31) & 1;
1452
1453 /* If we have EDCL we disable the EDCL speed-duplex FSM so
1454 * it doesn't interfere with the software */
1455 if (greth->edcl != 0)
1456 GRETH_REGORIN(regs->control, GRETH_CTRL_DISDUPLEX);
1457
1458 /* Check if MAC can handle MDIO interrupts */
1459 greth->mdio_int_en = (tmp >> 26) & 1;
1460
1461 err = greth_mdio_init(greth);
1462 if (err) {
1463 if (netif_msg_probe(greth))
1464 dev_err(greth->dev, "failed to register MDIO bus\n");
1465 goto error2;
1466 }
1467
1468 /* Allocate TX descriptor ring in coherent memory */
1469 greth->tx_bd_base = (struct greth_bd *) dma_alloc_coherent(greth->dev,
1470 1024,
1471 &greth->tx_bd_base_phys,
1472 GFP_KERNEL);
1473
1474 if (!greth->tx_bd_base) {
1475 if (netif_msg_probe(greth))
1476 dev_err(&dev->dev, "could not allocate descriptor memory.\n");
1477 err = -ENOMEM;
1478 goto error3;
1479 }
1480
1481 memset(greth->tx_bd_base, 0, 1024);
1482
1483 /* Allocate RX descriptor ring in coherent memory */
1484 greth->rx_bd_base = (struct greth_bd *) dma_alloc_coherent(greth->dev,
1485 1024,
1486 &greth->rx_bd_base_phys,
1487 GFP_KERNEL);
1488
1489 if (!greth->rx_bd_base) {
1490 if (netif_msg_probe(greth))
1491 dev_err(greth->dev, "could not allocate descriptor memory.\n");
1492 err = -ENOMEM;
1493 goto error4;
1494 }
1495
1496 memset(greth->rx_bd_base, 0, 1024);
1497
1498 /* Get MAC address from: module param, OF property or ID prom */
1499 for (i = 0; i < 6; i++) {
1500 if (macaddr[i] != 0)
1501 break;
1502 }
1503 if (i == 6) {
1504 const unsigned char *addr;
1505 int len;
Grant Likely61c7a082010-04-13 16:12:29 -07001506 addr = of_get_property(ofdev->dev.of_node, "local-mac-address",
1507 &len);
Kristoffer Glembod4c41132010-02-15 03:33:44 +00001508 if (addr != NULL && len == 6) {
1509 for (i = 0; i < 6; i++)
1510 macaddr[i] = (unsigned int) addr[i];
1511 } else {
1512#ifdef CONFIG_SPARC
1513 for (i = 0; i < 6; i++)
1514 macaddr[i] = (unsigned int) idprom->id_ethaddr[i];
1515#endif
1516 }
1517 }
1518
1519 for (i = 0; i < 6; i++)
1520 dev->dev_addr[i] = macaddr[i];
1521
1522 macaddr[5]++;
1523
1524 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
1525 if (netif_msg_probe(greth))
1526 dev_err(greth->dev, "no valid ethernet address, aborting.\n");
1527 err = -EINVAL;
1528 goto error5;
1529 }
1530
1531 GRETH_REGSAVE(regs->esa_msb, dev->dev_addr[0] << 8 | dev->dev_addr[1]);
1532 GRETH_REGSAVE(regs->esa_lsb, dev->dev_addr[2] << 24 | dev->dev_addr[3] << 16 |
1533 dev->dev_addr[4] << 8 | dev->dev_addr[5]);
1534
1535 /* Clear all pending interrupts except PHY irq */
1536 GRETH_REGSAVE(regs->status, 0xFF);
1537
1538 if (greth->gbit_mac) {
1539 dev->features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_HIGHDMA;
1540 greth_netdev_ops.ndo_start_xmit = greth_start_xmit_gbit;
1541 greth->flags = GRETH_FLAG_RX_CSUM;
1542 }
1543
1544 if (greth->multicast) {
1545 greth_netdev_ops.ndo_set_multicast_list = greth_set_multicast_list;
1546 dev->flags |= IFF_MULTICAST;
1547 } else {
1548 dev->flags &= ~IFF_MULTICAST;
1549 }
1550
1551 dev->netdev_ops = &greth_netdev_ops;
1552 dev->ethtool_ops = &greth_ethtool_ops;
1553
Tobias Klausercb5d9912010-08-17 06:11:24 +00001554 err = register_netdev(dev);
1555 if (err) {
Kristoffer Glembod4c41132010-02-15 03:33:44 +00001556 if (netif_msg_probe(greth))
1557 dev_err(greth->dev, "netdevice registration failed.\n");
Kristoffer Glembod4c41132010-02-15 03:33:44 +00001558 goto error5;
1559 }
1560
1561 /* setup NAPI */
Kristoffer Glembod4c41132010-02-15 03:33:44 +00001562 netif_napi_add(dev, &greth->napi, greth_poll, 64);
1563
1564 return 0;
1565
1566error5:
1567 dma_free_coherent(greth->dev, 1024, greth->rx_bd_base, greth->rx_bd_base_phys);
1568error4:
1569 dma_free_coherent(greth->dev, 1024, greth->tx_bd_base, greth->tx_bd_base_phys);
1570error3:
1571 mdiobus_unregister(greth->mdio);
1572error2:
1573 of_iounmap(&ofdev->resource[0], greth->regs, resource_size(&ofdev->resource[0]));
1574error1:
1575 free_netdev(dev);
1576 return err;
1577}
1578
Grant Likely2dc11582010-08-06 09:25:50 -06001579static int __devexit greth_of_remove(struct platform_device *of_dev)
Kristoffer Glembod4c41132010-02-15 03:33:44 +00001580{
1581 struct net_device *ndev = dev_get_drvdata(&of_dev->dev);
1582 struct greth_private *greth = netdev_priv(ndev);
1583
1584 /* Free descriptor areas */
1585 dma_free_coherent(&of_dev->dev, 1024, greth->rx_bd_base, greth->rx_bd_base_phys);
1586
1587 dma_free_coherent(&of_dev->dev, 1024, greth->tx_bd_base, greth->tx_bd_base_phys);
1588
1589 dev_set_drvdata(&of_dev->dev, NULL);
1590
1591 if (greth->phy)
1592 phy_stop(greth->phy);
1593 mdiobus_unregister(greth->mdio);
1594
1595 unregister_netdev(ndev);
1596 free_netdev(ndev);
1597
1598 of_iounmap(&of_dev->resource[0], greth->regs, resource_size(&of_dev->resource[0]));
1599
1600 return 0;
1601}
1602
1603static struct of_device_id greth_of_match[] = {
1604 {
1605 .name = "GAISLER_ETHMAC",
1606 },
Daniel Hellstromad4650a2011-01-14 03:02:37 +00001607 {
1608 .name = "01_01d",
1609 },
Kristoffer Glembod4c41132010-02-15 03:33:44 +00001610 {},
1611};
1612
1613MODULE_DEVICE_TABLE(of, greth_of_match);
1614
1615static struct of_platform_driver greth_of_driver = {
David S. Millerbc284f92010-05-31 05:47:32 -07001616 .driver = {
1617 .name = "grlib-greth",
1618 .owner = THIS_MODULE,
1619 .of_match_table = greth_of_match,
1620 },
Kristoffer Glembod4c41132010-02-15 03:33:44 +00001621 .probe = greth_of_probe,
1622 .remove = __devexit_p(greth_of_remove),
Kristoffer Glembod4c41132010-02-15 03:33:44 +00001623};
1624
1625static int __init greth_init(void)
1626{
1627 return of_register_platform_driver(&greth_of_driver);
1628}
1629
1630static void __exit greth_cleanup(void)
1631{
1632 of_unregister_platform_driver(&greth_of_driver);
1633}
1634
1635module_init(greth_init);
1636module_exit(greth_cleanup);
1637
1638MODULE_AUTHOR("Aeroflex Gaisler AB.");
1639MODULE_DESCRIPTION("Aeroflex Gaisler Ethernet MAC driver");
1640MODULE_LICENSE("GPL");