blob: 69b86d7fac857ed14d4c82b630597ffb64280d20 [file] [log] [blame]
Kristoffer Glembod4c41132010-02-15 03:33:44 +00001/*
2 * Aeroflex Gaisler GRETH 10/100/1G Ethernet MAC.
3 *
Daniel Hellstrom0f73f2c2011-01-14 03:02:43 +00004 * 2005-2010 (c) Aeroflex Gaisler AB
Kristoffer Glembod4c41132010-02-15 03:33:44 +00005 *
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>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000028#include <linux/interrupt.h>
Kristoffer Glembod4c41132010-02-15 03:33:44 +000029#include <linux/netdevice.h>
30#include <linux/etherdevice.h>
31#include <linux/ethtool.h>
32#include <linux/skbuff.h>
33#include <linux/io.h>
34#include <linux/crc32.h>
35#include <linux/mii.h>
36#include <linux/of_device.h>
37#include <linux/of_platform.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
Kristoffer Glembod4c41132010-02-15 03:33:44 +000039#include <asm/cacheflush.h>
40#include <asm/byteorder.h>
41
42#ifdef CONFIG_SPARC
43#include <asm/idprom.h>
44#endif
45
46#include "greth.h"
47
48#define GRETH_DEF_MSG_ENABLE \
49 (NETIF_MSG_DRV | \
50 NETIF_MSG_PROBE | \
51 NETIF_MSG_LINK | \
52 NETIF_MSG_IFDOWN | \
53 NETIF_MSG_IFUP | \
54 NETIF_MSG_RX_ERR | \
55 NETIF_MSG_TX_ERR)
56
57static int greth_debug = -1; /* -1 == use GRETH_DEF_MSG_ENABLE as value */
58module_param(greth_debug, int, 0);
59MODULE_PARM_DESC(greth_debug, "GRETH bitmapped debugging message enable value");
60
61/* Accept MAC address of the form macaddr=0x08,0x00,0x20,0x30,0x40,0x50 */
62static int macaddr[6];
63module_param_array(macaddr, int, NULL, 0);
64MODULE_PARM_DESC(macaddr, "GRETH Ethernet MAC address");
65
66static int greth_edcl = 1;
67module_param(greth_edcl, int, 0);
68MODULE_PARM_DESC(greth_edcl, "GRETH EDCL usage indicator. Set to 1 if EDCL is used.");
69
70static int greth_open(struct net_device *dev);
kirjanov@gmail.com41a655b2010-02-24 10:25:33 +000071static netdev_tx_t greth_start_xmit(struct sk_buff *skb,
72 struct net_device *dev);
73static netdev_tx_t greth_start_xmit_gbit(struct sk_buff *skb,
74 struct net_device *dev);
Kristoffer Glembod4c41132010-02-15 03:33:44 +000075static int greth_rx(struct net_device *dev, int limit);
76static int greth_rx_gbit(struct net_device *dev, int limit);
77static void greth_clean_tx(struct net_device *dev);
78static void greth_clean_tx_gbit(struct net_device *dev);
79static irqreturn_t greth_interrupt(int irq, void *dev_id);
80static int greth_close(struct net_device *dev);
81static int greth_set_mac_add(struct net_device *dev, void *p);
82static void greth_set_multicast_list(struct net_device *dev);
83
84#define GRETH_REGLOAD(a) (be32_to_cpu(__raw_readl(&(a))))
85#define GRETH_REGSAVE(a, v) (__raw_writel(cpu_to_be32(v), &(a)))
86#define GRETH_REGORIN(a, v) (GRETH_REGSAVE(a, (GRETH_REGLOAD(a) | (v))))
87#define GRETH_REGANDIN(a, v) (GRETH_REGSAVE(a, (GRETH_REGLOAD(a) & (v))))
88
89#define NEXT_TX(N) (((N) + 1) & GRETH_TXBD_NUM_MASK)
90#define SKIP_TX(N, C) (((N) + C) & GRETH_TXBD_NUM_MASK)
91#define NEXT_RX(N) (((N) + 1) & GRETH_RXBD_NUM_MASK)
92
93static void greth_print_rx_packet(void *addr, int len)
94{
95 print_hex_dump(KERN_DEBUG, "RX: ", DUMP_PREFIX_OFFSET, 16, 1,
96 addr, len, true);
97}
98
99static void greth_print_tx_packet(struct sk_buff *skb)
100{
101 int i;
102 int length;
103
104 if (skb_shinfo(skb)->nr_frags == 0)
105 length = skb->len;
106 else
107 length = skb_headlen(skb);
108
109 print_hex_dump(KERN_DEBUG, "TX: ", DUMP_PREFIX_OFFSET, 16, 1,
110 skb->data, length, true);
111
112 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
113
114 print_hex_dump(KERN_DEBUG, "TX: ", DUMP_PREFIX_OFFSET, 16, 1,
115 phys_to_virt(page_to_phys(skb_shinfo(skb)->frags[i].page)) +
116 skb_shinfo(skb)->frags[i].page_offset,
117 length, true);
118 }
119}
120
121static inline void greth_enable_tx(struct greth_private *greth)
122{
123 wmb();
124 GRETH_REGORIN(greth->regs->control, GRETH_TXEN);
125}
126
127static inline void greth_disable_tx(struct greth_private *greth)
128{
129 GRETH_REGANDIN(greth->regs->control, ~GRETH_TXEN);
130}
131
132static inline void greth_enable_rx(struct greth_private *greth)
133{
134 wmb();
135 GRETH_REGORIN(greth->regs->control, GRETH_RXEN);
136}
137
138static inline void greth_disable_rx(struct greth_private *greth)
139{
140 GRETH_REGANDIN(greth->regs->control, ~GRETH_RXEN);
141}
142
143static inline void greth_enable_irqs(struct greth_private *greth)
144{
145 GRETH_REGORIN(greth->regs->control, GRETH_RXI | GRETH_TXI);
146}
147
148static inline void greth_disable_irqs(struct greth_private *greth)
149{
150 GRETH_REGANDIN(greth->regs->control, ~(GRETH_RXI|GRETH_TXI));
151}
152
153static inline void greth_write_bd(u32 *bd, u32 val)
154{
155 __raw_writel(cpu_to_be32(val), bd);
156}
157
158static inline u32 greth_read_bd(u32 *bd)
159{
160 return be32_to_cpu(__raw_readl(bd));
161}
162
163static void greth_clean_rings(struct greth_private *greth)
164{
165 int i;
166 struct greth_bd *rx_bdp = greth->rx_bd_base;
167 struct greth_bd *tx_bdp = greth->tx_bd_base;
168
169 if (greth->gbit_mac) {
170
171 /* Free and unmap RX buffers */
172 for (i = 0; i < GRETH_RXBD_NUM; i++, rx_bdp++) {
173 if (greth->rx_skbuff[i] != NULL) {
174 dev_kfree_skb(greth->rx_skbuff[i]);
175 dma_unmap_single(greth->dev,
176 greth_read_bd(&rx_bdp->addr),
177 MAX_FRAME_SIZE+NET_IP_ALIGN,
178 DMA_FROM_DEVICE);
179 }
180 }
181
182 /* TX buffers */
183 while (greth->tx_free < GRETH_TXBD_NUM) {
184
185 struct sk_buff *skb = greth->tx_skbuff[greth->tx_last];
186 int nr_frags = skb_shinfo(skb)->nr_frags;
187 tx_bdp = greth->tx_bd_base + greth->tx_last;
188 greth->tx_last = NEXT_TX(greth->tx_last);
189
190 dma_unmap_single(greth->dev,
191 greth_read_bd(&tx_bdp->addr),
192 skb_headlen(skb),
193 DMA_TO_DEVICE);
194
195 for (i = 0; i < nr_frags; i++) {
196 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
197 tx_bdp = greth->tx_bd_base + greth->tx_last;
198
199 dma_unmap_page(greth->dev,
200 greth_read_bd(&tx_bdp->addr),
201 frag->size,
202 DMA_TO_DEVICE);
203
204 greth->tx_last = NEXT_TX(greth->tx_last);
205 }
206 greth->tx_free += nr_frags+1;
207 dev_kfree_skb(skb);
208 }
209
210
211 } else { /* 10/100 Mbps MAC */
212
213 for (i = 0; i < GRETH_RXBD_NUM; i++, rx_bdp++) {
214 kfree(greth->rx_bufs[i]);
215 dma_unmap_single(greth->dev,
216 greth_read_bd(&rx_bdp->addr),
217 MAX_FRAME_SIZE,
218 DMA_FROM_DEVICE);
219 }
220 for (i = 0; i < GRETH_TXBD_NUM; i++, tx_bdp++) {
221 kfree(greth->tx_bufs[i]);
222 dma_unmap_single(greth->dev,
223 greth_read_bd(&tx_bdp->addr),
224 MAX_FRAME_SIZE,
225 DMA_TO_DEVICE);
226 }
227 }
228}
229
230static int greth_init_rings(struct greth_private *greth)
231{
232 struct sk_buff *skb;
233 struct greth_bd *rx_bd, *tx_bd;
234 u32 dma_addr;
235 int i;
236
237 rx_bd = greth->rx_bd_base;
238 tx_bd = greth->tx_bd_base;
239
240 /* Initialize descriptor rings and buffers */
241 if (greth->gbit_mac) {
242
243 for (i = 0; i < GRETH_RXBD_NUM; i++) {
244 skb = netdev_alloc_skb(greth->netdev, MAX_FRAME_SIZE+NET_IP_ALIGN);
245 if (skb == NULL) {
246 if (netif_msg_ifup(greth))
247 dev_err(greth->dev, "Error allocating DMA ring.\n");
248 goto cleanup;
249 }
250 skb_reserve(skb, NET_IP_ALIGN);
251 dma_addr = dma_map_single(greth->dev,
252 skb->data,
253 MAX_FRAME_SIZE+NET_IP_ALIGN,
254 DMA_FROM_DEVICE);
255
256 if (dma_mapping_error(greth->dev, dma_addr)) {
257 if (netif_msg_ifup(greth))
258 dev_err(greth->dev, "Could not create initial DMA mapping\n");
259 goto cleanup;
260 }
261 greth->rx_skbuff[i] = skb;
262 greth_write_bd(&rx_bd[i].addr, dma_addr);
263 greth_write_bd(&rx_bd[i].stat, GRETH_BD_EN | GRETH_BD_IE);
264 }
265
266 } else {
267
268 /* 10/100 MAC uses a fixed set of buffers and copy to/from SKBs */
269 for (i = 0; i < GRETH_RXBD_NUM; i++) {
270
271 greth->rx_bufs[i] = kmalloc(MAX_FRAME_SIZE, GFP_KERNEL);
272
273 if (greth->rx_bufs[i] == NULL) {
274 if (netif_msg_ifup(greth))
275 dev_err(greth->dev, "Error allocating DMA ring.\n");
276 goto cleanup;
277 }
278
279 dma_addr = dma_map_single(greth->dev,
280 greth->rx_bufs[i],
281 MAX_FRAME_SIZE,
282 DMA_FROM_DEVICE);
283
284 if (dma_mapping_error(greth->dev, dma_addr)) {
285 if (netif_msg_ifup(greth))
286 dev_err(greth->dev, "Could not create initial DMA mapping\n");
287 goto cleanup;
288 }
289 greth_write_bd(&rx_bd[i].addr, dma_addr);
290 greth_write_bd(&rx_bd[i].stat, GRETH_BD_EN | GRETH_BD_IE);
291 }
292 for (i = 0; i < GRETH_TXBD_NUM; i++) {
293
294 greth->tx_bufs[i] = kmalloc(MAX_FRAME_SIZE, GFP_KERNEL);
295
296 if (greth->tx_bufs[i] == NULL) {
297 if (netif_msg_ifup(greth))
298 dev_err(greth->dev, "Error allocating DMA ring.\n");
299 goto cleanup;
300 }
301
302 dma_addr = dma_map_single(greth->dev,
303 greth->tx_bufs[i],
304 MAX_FRAME_SIZE,
305 DMA_TO_DEVICE);
306
307 if (dma_mapping_error(greth->dev, dma_addr)) {
308 if (netif_msg_ifup(greth))
309 dev_err(greth->dev, "Could not create initial DMA mapping\n");
310 goto cleanup;
311 }
312 greth_write_bd(&tx_bd[i].addr, dma_addr);
313 greth_write_bd(&tx_bd[i].stat, 0);
314 }
315 }
316 greth_write_bd(&rx_bd[GRETH_RXBD_NUM - 1].stat,
317 greth_read_bd(&rx_bd[GRETH_RXBD_NUM - 1].stat) | GRETH_BD_WR);
318
319 /* Initialize pointers. */
320 greth->rx_cur = 0;
321 greth->tx_next = 0;
322 greth->tx_last = 0;
323 greth->tx_free = GRETH_TXBD_NUM;
324
325 /* Initialize descriptor base address */
326 GRETH_REGSAVE(greth->regs->tx_desc_p, greth->tx_bd_base_phys);
327 GRETH_REGSAVE(greth->regs->rx_desc_p, greth->rx_bd_base_phys);
328
329 return 0;
330
331cleanup:
332 greth_clean_rings(greth);
333 return -ENOMEM;
334}
335
336static int greth_open(struct net_device *dev)
337{
338 struct greth_private *greth = netdev_priv(dev);
339 int err;
340
341 err = greth_init_rings(greth);
342 if (err) {
343 if (netif_msg_ifup(greth))
344 dev_err(&dev->dev, "Could not allocate memory for DMA rings\n");
345 return err;
346 }
347
348 err = request_irq(greth->irq, greth_interrupt, 0, "eth", (void *) dev);
349 if (err) {
350 if (netif_msg_ifup(greth))
351 dev_err(&dev->dev, "Could not allocate interrupt %d\n", dev->irq);
352 greth_clean_rings(greth);
353 return err;
354 }
355
356 if (netif_msg_ifup(greth))
357 dev_dbg(&dev->dev, " starting queue\n");
358 netif_start_queue(dev);
359
Daniel Hellstrombbe9e632011-01-14 03:02:38 +0000360 GRETH_REGSAVE(greth->regs->status, 0xFF);
361
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000362 napi_enable(&greth->napi);
363
364 greth_enable_irqs(greth);
365 greth_enable_tx(greth);
366 greth_enable_rx(greth);
367 return 0;
368
369}
370
371static int greth_close(struct net_device *dev)
372{
373 struct greth_private *greth = netdev_priv(dev);
374
375 napi_disable(&greth->napi);
376
Daniel Hellstrombbe9e632011-01-14 03:02:38 +0000377 greth_disable_irqs(greth);
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000378 greth_disable_tx(greth);
Daniel Hellstrombbe9e632011-01-14 03:02:38 +0000379 greth_disable_rx(greth);
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000380
381 netif_stop_queue(dev);
382
383 free_irq(greth->irq, (void *) dev);
384
385 greth_clean_rings(greth);
386
387 return 0;
388}
389
kirjanov@gmail.com41a655b2010-02-24 10:25:33 +0000390static netdev_tx_t
391greth_start_xmit(struct sk_buff *skb, struct net_device *dev)
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000392{
393 struct greth_private *greth = netdev_priv(dev);
394 struct greth_bd *bdp;
395 int err = NETDEV_TX_OK;
Daniel Hellstrom0f73f2c2011-01-14 03:02:43 +0000396 u32 status, dma_addr, ctrl;
397 unsigned long flags;
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000398
Daniel Hellstrom0f73f2c2011-01-14 03:02:43 +0000399 /* Clean TX Ring */
400 greth_clean_tx(greth->netdev);
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000401
402 if (unlikely(greth->tx_free <= 0)) {
Daniel Hellstrom0f73f2c2011-01-14 03:02:43 +0000403 spin_lock_irqsave(&greth->devlock, flags);/*save from poll/irq*/
404 ctrl = GRETH_REGLOAD(greth->regs->control);
405 /* Enable TX IRQ only if not already in poll() routine */
406 if (ctrl & GRETH_RXI)
407 GRETH_REGSAVE(greth->regs->control, ctrl | GRETH_TXI);
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000408 netif_stop_queue(dev);
Daniel Hellstrom0f73f2c2011-01-14 03:02:43 +0000409 spin_unlock_irqrestore(&greth->devlock, flags);
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000410 return NETDEV_TX_BUSY;
411 }
412
413 if (netif_msg_pktdata(greth))
414 greth_print_tx_packet(skb);
415
416
417 if (unlikely(skb->len > MAX_FRAME_SIZE)) {
418 dev->stats.tx_errors++;
419 goto out;
420 }
421
Daniel Hellstrom0f73f2c2011-01-14 03:02:43 +0000422 bdp = greth->tx_bd_base + greth->tx_next;
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000423 dma_addr = greth_read_bd(&bdp->addr);
424
425 memcpy((unsigned char *) phys_to_virt(dma_addr), skb->data, skb->len);
426
427 dma_sync_single_for_device(greth->dev, dma_addr, skb->len, DMA_TO_DEVICE);
428
Daniel Hellstrom0f73f2c2011-01-14 03:02:43 +0000429 status = GRETH_BD_EN | GRETH_BD_IE | (skb->len & GRETH_BD_LEN);
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000430
431 /* Wrap around descriptor ring */
432 if (greth->tx_next == GRETH_TXBD_NUM_MASK) {
433 status |= GRETH_BD_WR;
434 }
435
436 greth->tx_next = NEXT_TX(greth->tx_next);
437 greth->tx_free--;
438
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000439 /* Write descriptor control word and enable transmission */
440 greth_write_bd(&bdp->stat, status);
Daniel Hellstrom0f73f2c2011-01-14 03:02:43 +0000441 spin_lock_irqsave(&greth->devlock, flags); /*save from poll/irq*/
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000442 greth_enable_tx(greth);
Daniel Hellstrom0f73f2c2011-01-14 03:02:43 +0000443 spin_unlock_irqrestore(&greth->devlock, flags);
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000444
445out:
446 dev_kfree_skb(skb);
447 return err;
448}
449
450
kirjanov@gmail.com41a655b2010-02-24 10:25:33 +0000451static netdev_tx_t
452greth_start_xmit_gbit(struct sk_buff *skb, struct net_device *dev)
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000453{
454 struct greth_private *greth = netdev_priv(dev);
455 struct greth_bd *bdp;
Daniel Hellstrom0f73f2c2011-01-14 03:02:43 +0000456 u32 status = 0, dma_addr, ctrl;
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000457 int curr_tx, nr_frags, i, err = NETDEV_TX_OK;
Daniel Hellstrom0f73f2c2011-01-14 03:02:43 +0000458 unsigned long flags;
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000459
460 nr_frags = skb_shinfo(skb)->nr_frags;
461
Daniel Hellstrom0f73f2c2011-01-14 03:02:43 +0000462 /* Clean TX Ring */
463 greth_clean_tx_gbit(dev);
464
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000465 if (greth->tx_free < nr_frags + 1) {
Daniel Hellstrom0f73f2c2011-01-14 03:02:43 +0000466 spin_lock_irqsave(&greth->devlock, flags);/*save from poll/irq*/
467 ctrl = GRETH_REGLOAD(greth->regs->control);
468 /* Enable TX IRQ only if not already in poll() routine */
469 if (ctrl & GRETH_RXI)
470 GRETH_REGSAVE(greth->regs->control, ctrl | GRETH_TXI);
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000471 netif_stop_queue(dev);
Daniel Hellstrom0f73f2c2011-01-14 03:02:43 +0000472 spin_unlock_irqrestore(&greth->devlock, flags);
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000473 err = NETDEV_TX_BUSY;
474 goto out;
475 }
476
477 if (netif_msg_pktdata(greth))
478 greth_print_tx_packet(skb);
479
480 if (unlikely(skb->len > MAX_FRAME_SIZE)) {
481 dev->stats.tx_errors++;
482 goto out;
483 }
484
485 /* Save skb pointer. */
486 greth->tx_skbuff[greth->tx_next] = skb;
487
488 /* Linear buf */
489 if (nr_frags != 0)
490 status = GRETH_TXBD_MORE;
491
492 status |= GRETH_TXBD_CSALL;
493 status |= skb_headlen(skb) & GRETH_BD_LEN;
494 if (greth->tx_next == GRETH_TXBD_NUM_MASK)
495 status |= GRETH_BD_WR;
496
497
498 bdp = greth->tx_bd_base + greth->tx_next;
499 greth_write_bd(&bdp->stat, status);
500 dma_addr = dma_map_single(greth->dev, skb->data, skb_headlen(skb), DMA_TO_DEVICE);
501
502 if (unlikely(dma_mapping_error(greth->dev, dma_addr)))
503 goto map_error;
504
505 greth_write_bd(&bdp->addr, dma_addr);
506
507 curr_tx = NEXT_TX(greth->tx_next);
508
509 /* Frags */
510 for (i = 0; i < nr_frags; i++) {
511 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
512 greth->tx_skbuff[curr_tx] = NULL;
513 bdp = greth->tx_bd_base + curr_tx;
514
Daniel Hellstrom2a2bc012011-01-14 03:02:39 +0000515 status = GRETH_TXBD_CSALL | GRETH_BD_EN;
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000516 status |= frag->size & GRETH_BD_LEN;
517
518 /* Wrap around descriptor ring */
519 if (curr_tx == GRETH_TXBD_NUM_MASK)
520 status |= GRETH_BD_WR;
521
522 /* More fragments left */
523 if (i < nr_frags - 1)
524 status |= GRETH_TXBD_MORE;
Daniel Hellstrom0f73f2c2011-01-14 03:02:43 +0000525 else
526 status |= GRETH_BD_IE; /* enable IRQ on last fragment */
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000527
528 greth_write_bd(&bdp->stat, status);
529
530 dma_addr = dma_map_page(greth->dev,
531 frag->page,
532 frag->page_offset,
533 frag->size,
534 DMA_TO_DEVICE);
535
536 if (unlikely(dma_mapping_error(greth->dev, dma_addr)))
537 goto frag_map_error;
538
539 greth_write_bd(&bdp->addr, dma_addr);
540
541 curr_tx = NEXT_TX(curr_tx);
542 }
543
544 wmb();
545
Daniel Hellstrom2a2bc012011-01-14 03:02:39 +0000546 /* Enable the descriptor chain by enabling the first descriptor */
547 bdp = greth->tx_bd_base + greth->tx_next;
548 greth_write_bd(&bdp->stat, greth_read_bd(&bdp->stat) | GRETH_BD_EN);
549 greth->tx_next = curr_tx;
550 greth->tx_free -= nr_frags + 1;
551
552 wmb();
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000553
Daniel Hellstrom0f73f2c2011-01-14 03:02:43 +0000554 spin_lock_irqsave(&greth->devlock, flags); /*save from poll/irq*/
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000555 greth_enable_tx(greth);
Daniel Hellstrom0f73f2c2011-01-14 03:02:43 +0000556 spin_unlock_irqrestore(&greth->devlock, flags);
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000557
558 return NETDEV_TX_OK;
559
560frag_map_error:
Daniel Hellstrom2a2bc012011-01-14 03:02:39 +0000561 /* Unmap SKB mappings that succeeded and disable descriptor */
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000562 for (i = 0; greth->tx_next + i != curr_tx; i++) {
563 bdp = greth->tx_bd_base + greth->tx_next + i;
564 dma_unmap_single(greth->dev,
565 greth_read_bd(&bdp->addr),
566 greth_read_bd(&bdp->stat) & GRETH_BD_LEN,
567 DMA_TO_DEVICE);
Daniel Hellstrom2a2bc012011-01-14 03:02:39 +0000568 greth_write_bd(&bdp->stat, 0);
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000569 }
570map_error:
571 if (net_ratelimit())
572 dev_warn(greth->dev, "Could not create TX DMA mapping\n");
573 dev_kfree_skb(skb);
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000574out:
575 return err;
576}
577
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000578static irqreturn_t greth_interrupt(int irq, void *dev_id)
579{
580 struct net_device *dev = dev_id;
581 struct greth_private *greth;
Daniel Hellstrom0f73f2c2011-01-14 03:02:43 +0000582 u32 status, ctrl;
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000583 irqreturn_t retval = IRQ_NONE;
584
585 greth = netdev_priv(dev);
586
587 spin_lock(&greth->devlock);
588
589 /* Get the interrupt events that caused us to be here. */
590 status = GRETH_REGLOAD(greth->regs->status);
591
Daniel Hellstrom0f73f2c2011-01-14 03:02:43 +0000592 /* Must see if interrupts are enabled also, INT_TX|INT_RX flags may be
593 * set regardless of whether IRQ is enabled or not. Especially
594 * important when shared IRQ.
595 */
596 ctrl = GRETH_REGLOAD(greth->regs->control);
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000597
Daniel Hellstrom0f73f2c2011-01-14 03:02:43 +0000598 /* Handle rx and tx interrupts through poll */
599 if (((status & (GRETH_INT_RE | GRETH_INT_RX)) && (ctrl & GRETH_RXI)) ||
600 ((status & (GRETH_INT_TE | GRETH_INT_TX)) && (ctrl & GRETH_TXI))) {
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000601 retval = IRQ_HANDLED;
602
603 /* Disable interrupts and schedule poll() */
604 greth_disable_irqs(greth);
605 napi_schedule(&greth->napi);
606 }
607
608 mmiowb();
609 spin_unlock(&greth->devlock);
610
611 return retval;
612}
613
614static void greth_clean_tx(struct net_device *dev)
615{
616 struct greth_private *greth;
617 struct greth_bd *bdp;
618 u32 stat;
619
620 greth = netdev_priv(dev);
621
622 while (1) {
623 bdp = greth->tx_bd_base + greth->tx_last;
Daniel Hellstrom0f73f2c2011-01-14 03:02:43 +0000624 GRETH_REGSAVE(greth->regs->status, GRETH_INT_TE | GRETH_INT_TX);
625 mb();
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000626 stat = greth_read_bd(&bdp->stat);
627
628 if (unlikely(stat & GRETH_BD_EN))
629 break;
630
631 if (greth->tx_free == GRETH_TXBD_NUM)
632 break;
633
634 /* Check status for errors */
635 if (unlikely(stat & GRETH_TXBD_STATUS)) {
636 dev->stats.tx_errors++;
637 if (stat & GRETH_TXBD_ERR_AL)
638 dev->stats.tx_aborted_errors++;
639 if (stat & GRETH_TXBD_ERR_UE)
640 dev->stats.tx_fifo_errors++;
641 }
642 dev->stats.tx_packets++;
643 greth->tx_last = NEXT_TX(greth->tx_last);
644 greth->tx_free++;
645 }
646
647 if (greth->tx_free > 0) {
648 netif_wake_queue(dev);
649 }
650
651}
652
653static inline void greth_update_tx_stats(struct net_device *dev, u32 stat)
654{
655 /* Check status for errors */
656 if (unlikely(stat & GRETH_TXBD_STATUS)) {
657 dev->stats.tx_errors++;
658 if (stat & GRETH_TXBD_ERR_AL)
659 dev->stats.tx_aborted_errors++;
660 if (stat & GRETH_TXBD_ERR_UE)
661 dev->stats.tx_fifo_errors++;
662 if (stat & GRETH_TXBD_ERR_LC)
663 dev->stats.tx_aborted_errors++;
664 }
665 dev->stats.tx_packets++;
666}
667
668static void greth_clean_tx_gbit(struct net_device *dev)
669{
670 struct greth_private *greth;
671 struct greth_bd *bdp, *bdp_last_frag;
672 struct sk_buff *skb;
673 u32 stat;
674 int nr_frags, i;
675
676 greth = netdev_priv(dev);
677
678 while (greth->tx_free < GRETH_TXBD_NUM) {
679
680 skb = greth->tx_skbuff[greth->tx_last];
681
682 nr_frags = skb_shinfo(skb)->nr_frags;
683
684 /* We only clean fully completed SKBs */
685 bdp_last_frag = greth->tx_bd_base + SKIP_TX(greth->tx_last, nr_frags);
Daniel Hellstrom0f73f2c2011-01-14 03:02:43 +0000686
687 GRETH_REGSAVE(greth->regs->status, GRETH_INT_TE | GRETH_INT_TX);
688 mb();
689 stat = greth_read_bd(&bdp_last_frag->stat);
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000690
691 if (stat & GRETH_BD_EN)
692 break;
693
694 greth->tx_skbuff[greth->tx_last] = NULL;
695
696 greth_update_tx_stats(dev, stat);
697
698 bdp = greth->tx_bd_base + greth->tx_last;
699
700 greth->tx_last = NEXT_TX(greth->tx_last);
701
702 dma_unmap_single(greth->dev,
703 greth_read_bd(&bdp->addr),
704 skb_headlen(skb),
705 DMA_TO_DEVICE);
706
707 for (i = 0; i < nr_frags; i++) {
708 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
709 bdp = greth->tx_bd_base + greth->tx_last;
710
711 dma_unmap_page(greth->dev,
712 greth_read_bd(&bdp->addr),
713 frag->size,
714 DMA_TO_DEVICE);
715
716 greth->tx_last = NEXT_TX(greth->tx_last);
717 }
718 greth->tx_free += nr_frags+1;
719 dev_kfree_skb(skb);
720 }
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000721
Daniel Hellstrom0f73f2c2011-01-14 03:02:43 +0000722 if (netif_queue_stopped(dev) && (greth->tx_free > (MAX_SKB_FRAGS+1)))
723 netif_wake_queue(dev);
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000724}
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;
Daniel Hellstrom0f73f2c2011-01-14 03:02:43 +0000734 unsigned long flags;
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000735
736 greth = netdev_priv(dev);
737
738 for (count = 0; count < limit; ++count) {
739
740 bdp = greth->rx_bd_base + greth->rx_cur;
Daniel Hellstrom0f73f2c2011-01-14 03:02:43 +0000741 GRETH_REGSAVE(greth->regs->status, GRETH_INT_RE | GRETH_INT_RX);
742 mb();
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000743 status = greth_read_bd(&bdp->stat);
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000744
745 if (unlikely(status & GRETH_BD_EN)) {
746 break;
747 }
748
Daniel Hellstrom0f73f2c2011-01-14 03:02:43 +0000749 dma_addr = greth_read_bd(&bdp->addr);
750 bad = 0;
751
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000752 /* Check status for errors. */
753 if (unlikely(status & GRETH_RXBD_STATUS)) {
754 if (status & GRETH_RXBD_ERR_FT) {
755 dev->stats.rx_length_errors++;
756 bad = 1;
757 }
758 if (status & (GRETH_RXBD_ERR_AE | GRETH_RXBD_ERR_OE)) {
759 dev->stats.rx_frame_errors++;
760 bad = 1;
761 }
762 if (status & GRETH_RXBD_ERR_CRC) {
763 dev->stats.rx_crc_errors++;
764 bad = 1;
765 }
766 }
767 if (unlikely(bad)) {
768 dev->stats.rx_errors++;
769
770 } else {
771
772 pkt_len = status & GRETH_BD_LEN;
773
774 skb = netdev_alloc_skb(dev, pkt_len + NET_IP_ALIGN);
775
776 if (unlikely(skb == NULL)) {
777
778 if (net_ratelimit())
779 dev_warn(&dev->dev, "low on memory - " "packet dropped\n");
780
781 dev->stats.rx_dropped++;
782
783 } else {
784 skb_reserve(skb, NET_IP_ALIGN);
785 skb->dev = dev;
786
787 dma_sync_single_for_cpu(greth->dev,
788 dma_addr,
789 pkt_len,
790 DMA_FROM_DEVICE);
791
792 if (netif_msg_pktdata(greth))
793 greth_print_rx_packet(phys_to_virt(dma_addr), pkt_len);
794
795 memcpy(skb_put(skb, pkt_len), phys_to_virt(dma_addr), pkt_len);
796
797 skb->protocol = eth_type_trans(skb, dev);
798 dev->stats.rx_packets++;
799 netif_receive_skb(skb);
800 }
801 }
802
803 status = GRETH_BD_EN | GRETH_BD_IE;
804 if (greth->rx_cur == GRETH_RXBD_NUM_MASK) {
805 status |= GRETH_BD_WR;
806 }
807
808 wmb();
809 greth_write_bd(&bdp->stat, status);
810
811 dma_sync_single_for_device(greth->dev, dma_addr, MAX_FRAME_SIZE, DMA_FROM_DEVICE);
812
Daniel Hellstrom0f73f2c2011-01-14 03:02:43 +0000813 spin_lock_irqsave(&greth->devlock, flags); /* save from XMIT */
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000814 greth_enable_rx(greth);
Daniel Hellstrom0f73f2c2011-01-14 03:02:43 +0000815 spin_unlock_irqrestore(&greth->devlock, flags);
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000816
817 greth->rx_cur = NEXT_RX(greth->rx_cur);
818 }
819
820 return count;
821}
822
823static inline int hw_checksummed(u32 status)
824{
825
826 if (status & GRETH_RXBD_IP_FRAG)
827 return 0;
828
829 if (status & GRETH_RXBD_IP && status & GRETH_RXBD_IP_CSERR)
830 return 0;
831
832 if (status & GRETH_RXBD_UDP && status & GRETH_RXBD_UDP_CSERR)
833 return 0;
834
835 if (status & GRETH_RXBD_TCP && status & GRETH_RXBD_TCP_CSERR)
836 return 0;
837
838 return 1;
839}
840
841static int greth_rx_gbit(struct net_device *dev, int limit)
842{
843 struct greth_private *greth;
844 struct greth_bd *bdp;
845 struct sk_buff *skb, *newskb;
846 int pkt_len;
847 int bad, count = 0;
848 u32 status, dma_addr;
Daniel Hellstrom0f73f2c2011-01-14 03:02:43 +0000849 unsigned long flags;
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000850
851 greth = netdev_priv(dev);
852
853 for (count = 0; count < limit; ++count) {
854
855 bdp = greth->rx_bd_base + greth->rx_cur;
856 skb = greth->rx_skbuff[greth->rx_cur];
Daniel Hellstrom0f73f2c2011-01-14 03:02:43 +0000857 GRETH_REGSAVE(greth->regs->status, GRETH_INT_RE | GRETH_INT_RX);
858 mb();
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000859 status = greth_read_bd(&bdp->stat);
860 bad = 0;
861
862 if (status & GRETH_BD_EN)
863 break;
864
865 /* Check status for errors. */
866 if (unlikely(status & GRETH_RXBD_STATUS)) {
867
868 if (status & GRETH_RXBD_ERR_FT) {
869 dev->stats.rx_length_errors++;
870 bad = 1;
871 } else if (status &
872 (GRETH_RXBD_ERR_AE | GRETH_RXBD_ERR_OE | GRETH_RXBD_ERR_LE)) {
873 dev->stats.rx_frame_errors++;
874 bad = 1;
875 } else if (status & GRETH_RXBD_ERR_CRC) {
876 dev->stats.rx_crc_errors++;
877 bad = 1;
878 }
879 }
880
Daniel Hellstromb669e7f2011-01-14 03:02:40 +0000881 /* Allocate new skb to replace current, not needed if the
882 * current skb can be reused */
883 if (!bad && (newskb=netdev_alloc_skb(dev, MAX_FRAME_SIZE + NET_IP_ALIGN))) {
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000884 skb_reserve(newskb, NET_IP_ALIGN);
885
886 dma_addr = dma_map_single(greth->dev,
887 newskb->data,
888 MAX_FRAME_SIZE + NET_IP_ALIGN,
889 DMA_FROM_DEVICE);
890
891 if (!dma_mapping_error(greth->dev, dma_addr)) {
892 /* Process the incoming frame. */
893 pkt_len = status & GRETH_BD_LEN;
894
895 dma_unmap_single(greth->dev,
896 greth_read_bd(&bdp->addr),
897 MAX_FRAME_SIZE + NET_IP_ALIGN,
898 DMA_FROM_DEVICE);
899
900 if (netif_msg_pktdata(greth))
901 greth_print_rx_packet(phys_to_virt(greth_read_bd(&bdp->addr)), pkt_len);
902
903 skb_put(skb, pkt_len);
904
Michał Mirosław131ae322011-04-17 00:15:47 +0000905 if (dev->features & NETIF_F_RXCSUM && hw_checksummed(status))
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000906 skb->ip_summed = CHECKSUM_UNNECESSARY;
907 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -0700908 skb_checksum_none_assert(skb);
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000909
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000910 skb->protocol = eth_type_trans(skb, dev);
911 dev->stats.rx_packets++;
912 netif_receive_skb(skb);
913
914 greth->rx_skbuff[greth->rx_cur] = newskb;
915 greth_write_bd(&bdp->addr, dma_addr);
916 } else {
917 if (net_ratelimit())
918 dev_warn(greth->dev, "Could not create DMA mapping, dropping packet\n");
919 dev_kfree_skb(newskb);
Daniel Hellstromb669e7f2011-01-14 03:02:40 +0000920 /* reusing current skb, so it is a drop */
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000921 dev->stats.rx_dropped++;
922 }
Daniel Hellstromb669e7f2011-01-14 03:02:40 +0000923 } else if (bad) {
924 /* Bad Frame transfer, the skb is reused */
925 dev->stats.rx_dropped++;
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000926 } else {
Daniel Hellstromb669e7f2011-01-14 03:02:40 +0000927 /* Failed Allocating a new skb. This is rather stupid
928 * but the current "filled" skb is reused, as if
929 * transfer failure. One could argue that RX descriptor
930 * table handling should be divided into cleaning and
931 * filling as the TX part of the driver
932 */
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000933 if (net_ratelimit())
934 dev_warn(greth->dev, "Could not allocate SKB, dropping packet\n");
Daniel Hellstromb669e7f2011-01-14 03:02:40 +0000935 /* reusing current skb, so it is a drop */
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000936 dev->stats.rx_dropped++;
937 }
938
939 status = GRETH_BD_EN | GRETH_BD_IE;
940 if (greth->rx_cur == GRETH_RXBD_NUM_MASK) {
941 status |= GRETH_BD_WR;
942 }
943
944 wmb();
945 greth_write_bd(&bdp->stat, status);
Daniel Hellstrom0f73f2c2011-01-14 03:02:43 +0000946 spin_lock_irqsave(&greth->devlock, flags);
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000947 greth_enable_rx(greth);
Daniel Hellstrom0f73f2c2011-01-14 03:02:43 +0000948 spin_unlock_irqrestore(&greth->devlock, flags);
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000949 greth->rx_cur = NEXT_RX(greth->rx_cur);
950 }
951
952 return count;
953
954}
955
956static int greth_poll(struct napi_struct *napi, int budget)
957{
958 struct greth_private *greth;
959 int work_done = 0;
Daniel Hellstrom0f73f2c2011-01-14 03:02:43 +0000960 unsigned long flags;
961 u32 mask, ctrl;
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000962 greth = container_of(napi, struct greth_private, napi);
963
Daniel Hellstrom0f73f2c2011-01-14 03:02:43 +0000964restart_txrx_poll:
965 if (netif_queue_stopped(greth->netdev)) {
966 if (greth->gbit_mac)
967 greth_clean_tx_gbit(greth->netdev);
968 else
969 greth_clean_tx(greth->netdev);
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000970 }
971
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000972 if (greth->gbit_mac) {
973 work_done += greth_rx_gbit(greth->netdev, budget - work_done);
974 } else {
975 work_done += greth_rx(greth->netdev, budget - work_done);
976 }
977
978 if (work_done < budget) {
979
Daniel Hellstrom0f73f2c2011-01-14 03:02:43 +0000980 spin_lock_irqsave(&greth->devlock, flags);
Kristoffer Glembod4c41132010-02-15 03:33:44 +0000981
Daniel Hellstrom0f73f2c2011-01-14 03:02:43 +0000982 ctrl = GRETH_REGLOAD(greth->regs->control);
983 if (netif_queue_stopped(greth->netdev)) {
984 GRETH_REGSAVE(greth->regs->control,
985 ctrl | GRETH_TXI | GRETH_RXI);
986 mask = GRETH_INT_RX | GRETH_INT_RE |
987 GRETH_INT_TX | GRETH_INT_TE;
988 } else {
989 GRETH_REGSAVE(greth->regs->control, ctrl | GRETH_RXI);
990 mask = GRETH_INT_RX | GRETH_INT_RE;
991 }
992
993 if (GRETH_REGLOAD(greth->regs->status) & mask) {
994 GRETH_REGSAVE(greth->regs->control, ctrl);
995 spin_unlock_irqrestore(&greth->devlock, flags);
996 goto restart_txrx_poll;
997 } else {
998 __napi_complete(napi);
999 spin_unlock_irqrestore(&greth->devlock, flags);
Kristoffer Glembod4c41132010-02-15 03:33:44 +00001000 }
1001 }
1002
Kristoffer Glembod4c41132010-02-15 03:33:44 +00001003 return work_done;
1004}
1005
1006static int greth_set_mac_add(struct net_device *dev, void *p)
1007{
1008 struct sockaddr *addr = p;
1009 struct greth_private *greth;
1010 struct greth_regs *regs;
1011
kirjanov@gmail.com6e037182010-02-19 05:00:52 +00001012 greth = netdev_priv(dev);
Kristoffer Glembod4c41132010-02-15 03:33:44 +00001013 regs = (struct greth_regs *) greth->regs;
1014
1015 if (!is_valid_ether_addr(addr->sa_data))
1016 return -EINVAL;
1017
1018 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
1019
1020 GRETH_REGSAVE(regs->esa_msb, addr->sa_data[0] << 8 | addr->sa_data[1]);
1021 GRETH_REGSAVE(regs->esa_lsb,
1022 addr->sa_data[2] << 24 | addr->
1023 sa_data[3] << 16 | addr->sa_data[4] << 8 | addr->sa_data[5]);
1024 return 0;
1025}
1026
1027static u32 greth_hash_get_index(__u8 *addr)
1028{
1029 return (ether_crc(6, addr)) & 0x3F;
1030}
1031
1032static void greth_set_hash_filter(struct net_device *dev)
1033{
Jiri Pirko22bedad32010-04-01 21:22:57 +00001034 struct netdev_hw_addr *ha;
kirjanov@gmail.com6e037182010-02-19 05:00:52 +00001035 struct greth_private *greth = netdev_priv(dev);
Kristoffer Glembod4c41132010-02-15 03:33:44 +00001036 struct greth_regs *regs = (struct greth_regs *) greth->regs;
1037 u32 mc_filter[2];
kirjanov@gmail.com6e037182010-02-19 05:00:52 +00001038 unsigned int bitnr;
Kristoffer Glembod4c41132010-02-15 03:33:44 +00001039
1040 mc_filter[0] = mc_filter[1] = 0;
1041
Jiri Pirko22bedad32010-04-01 21:22:57 +00001042 netdev_for_each_mc_addr(ha, dev) {
1043 bitnr = greth_hash_get_index(ha->addr);
Kristoffer Glembod4c41132010-02-15 03:33:44 +00001044 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
1045 }
1046
1047 GRETH_REGSAVE(regs->hash_msb, mc_filter[1]);
1048 GRETH_REGSAVE(regs->hash_lsb, mc_filter[0]);
1049}
1050
1051static void greth_set_multicast_list(struct net_device *dev)
1052{
1053 int cfg;
1054 struct greth_private *greth = netdev_priv(dev);
1055 struct greth_regs *regs = (struct greth_regs *) greth->regs;
1056
1057 cfg = GRETH_REGLOAD(regs->control);
1058 if (dev->flags & IFF_PROMISC)
1059 cfg |= GRETH_CTRL_PR;
1060 else
1061 cfg &= ~GRETH_CTRL_PR;
1062
1063 if (greth->multicast) {
1064 if (dev->flags & IFF_ALLMULTI) {
1065 GRETH_REGSAVE(regs->hash_msb, -1);
1066 GRETH_REGSAVE(regs->hash_lsb, -1);
1067 cfg |= GRETH_CTRL_MCEN;
1068 GRETH_REGSAVE(regs->control, cfg);
1069 return;
1070 }
1071
kirjanov@gmail.com6e037182010-02-19 05:00:52 +00001072 if (netdev_mc_empty(dev)) {
Kristoffer Glembod4c41132010-02-15 03:33:44 +00001073 cfg &= ~GRETH_CTRL_MCEN;
1074 GRETH_REGSAVE(regs->control, cfg);
1075 return;
1076 }
1077
1078 /* Setup multicast filter */
1079 greth_set_hash_filter(dev);
1080 cfg |= GRETH_CTRL_MCEN;
1081 }
1082 GRETH_REGSAVE(regs->control, cfg);
1083}
1084
1085static u32 greth_get_msglevel(struct net_device *dev)
1086{
1087 struct greth_private *greth = netdev_priv(dev);
1088 return greth->msg_enable;
1089}
1090
1091static void greth_set_msglevel(struct net_device *dev, u32 value)
1092{
1093 struct greth_private *greth = netdev_priv(dev);
1094 greth->msg_enable = value;
1095}
1096static int greth_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1097{
1098 struct greth_private *greth = netdev_priv(dev);
1099 struct phy_device *phy = greth->phy;
1100
1101 if (!phy)
1102 return -ENODEV;
1103
1104 return phy_ethtool_gset(phy, cmd);
1105}
1106
1107static int greth_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1108{
1109 struct greth_private *greth = netdev_priv(dev);
1110 struct phy_device *phy = greth->phy;
1111
1112 if (!phy)
1113 return -ENODEV;
1114
1115 return phy_ethtool_sset(phy, cmd);
1116}
1117
1118static int greth_get_regs_len(struct net_device *dev)
1119{
1120 return sizeof(struct greth_regs);
1121}
1122
1123static void greth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
1124{
1125 struct greth_private *greth = netdev_priv(dev);
1126
1127 strncpy(info->driver, dev_driver_string(greth->dev), 32);
1128 strncpy(info->version, "revision: 1.0", 32);
1129 strncpy(info->bus_info, greth->dev->bus->name, 32);
1130 strncpy(info->fw_version, "N/A", 32);
1131 info->eedump_len = 0;
1132 info->regdump_len = sizeof(struct greth_regs);
1133}
1134
1135static void greth_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *p)
1136{
1137 int i;
1138 struct greth_private *greth = netdev_priv(dev);
1139 u32 __iomem *greth_regs = (u32 __iomem *) greth->regs;
1140 u32 *buff = p;
1141
1142 for (i = 0; i < sizeof(struct greth_regs) / sizeof(u32); i++)
1143 buff[i] = greth_read_bd(&greth_regs[i]);
1144}
1145
Kristoffer Glembod4c41132010-02-15 03:33:44 +00001146static const struct ethtool_ops greth_ethtool_ops = {
1147 .get_msglevel = greth_get_msglevel,
1148 .set_msglevel = greth_set_msglevel,
1149 .get_settings = greth_get_settings,
1150 .set_settings = greth_set_settings,
1151 .get_drvinfo = greth_get_drvinfo,
1152 .get_regs_len = greth_get_regs_len,
1153 .get_regs = greth_get_regs,
Kristoffer Glembod4c41132010-02-15 03:33:44 +00001154 .get_link = ethtool_op_get_link,
1155};
1156
1157static struct net_device_ops greth_netdev_ops = {
Daniel Hellstrom0f73f2c2011-01-14 03:02:43 +00001158 .ndo_open = greth_open,
1159 .ndo_stop = greth_close,
1160 .ndo_start_xmit = greth_start_xmit,
1161 .ndo_set_mac_address = greth_set_mac_add,
1162 .ndo_validate_addr = eth_validate_addr,
Kristoffer Glembod4c41132010-02-15 03:33:44 +00001163};
1164
1165static inline int wait_for_mdio(struct greth_private *greth)
1166{
1167 unsigned long timeout = jiffies + 4*HZ/100;
1168 while (GRETH_REGLOAD(greth->regs->mdio) & GRETH_MII_BUSY) {
1169 if (time_after(jiffies, timeout))
1170 return 0;
1171 }
1172 return 1;
1173}
1174
1175static int greth_mdio_read(struct mii_bus *bus, int phy, int reg)
1176{
1177 struct greth_private *greth = bus->priv;
1178 int data;
1179
1180 if (!wait_for_mdio(greth))
1181 return -EBUSY;
1182
1183 GRETH_REGSAVE(greth->regs->mdio, ((phy & 0x1F) << 11) | ((reg & 0x1F) << 6) | 2);
1184
1185 if (!wait_for_mdio(greth))
1186 return -EBUSY;
1187
1188 if (!(GRETH_REGLOAD(greth->regs->mdio) & GRETH_MII_NVALID)) {
1189 data = (GRETH_REGLOAD(greth->regs->mdio) >> 16) & 0xFFFF;
1190 return data;
1191
1192 } else {
1193 return -1;
1194 }
1195}
1196
1197static int greth_mdio_write(struct mii_bus *bus, int phy, int reg, u16 val)
1198{
1199 struct greth_private *greth = bus->priv;
1200
1201 if (!wait_for_mdio(greth))
1202 return -EBUSY;
1203
1204 GRETH_REGSAVE(greth->regs->mdio,
1205 ((val & 0xFFFF) << 16) | ((phy & 0x1F) << 11) | ((reg & 0x1F) << 6) | 1);
1206
1207 if (!wait_for_mdio(greth))
1208 return -EBUSY;
1209
1210 return 0;
1211}
1212
1213static int greth_mdio_reset(struct mii_bus *bus)
1214{
1215 return 0;
1216}
1217
1218static void greth_link_change(struct net_device *dev)
1219{
1220 struct greth_private *greth = netdev_priv(dev);
1221 struct phy_device *phydev = greth->phy;
1222 unsigned long flags;
Kristoffer Glembod4c41132010-02-15 03:33:44 +00001223 int status_change = 0;
Daniel Hellstrom2436af82011-01-14 03:02:41 +00001224 u32 ctrl;
Kristoffer Glembod4c41132010-02-15 03:33:44 +00001225
1226 spin_lock_irqsave(&greth->devlock, flags);
1227
1228 if (phydev->link) {
1229
1230 if ((greth->speed != phydev->speed) || (greth->duplex != phydev->duplex)) {
Daniel Hellstrom2436af82011-01-14 03:02:41 +00001231 ctrl = GRETH_REGLOAD(greth->regs->control) &
1232 ~(GRETH_CTRL_FD | GRETH_CTRL_SP | GRETH_CTRL_GB);
Kristoffer Glembod4c41132010-02-15 03:33:44 +00001233
1234 if (phydev->duplex)
Daniel Hellstrom2436af82011-01-14 03:02:41 +00001235 ctrl |= GRETH_CTRL_FD;
Kristoffer Glembod4c41132010-02-15 03:33:44 +00001236
Daniel Hellstrom2436af82011-01-14 03:02:41 +00001237 if (phydev->speed == SPEED_100)
1238 ctrl |= GRETH_CTRL_SP;
Kristoffer Glembod4c41132010-02-15 03:33:44 +00001239 else if (phydev->speed == SPEED_1000)
Daniel Hellstrom2436af82011-01-14 03:02:41 +00001240 ctrl |= GRETH_CTRL_GB;
Kristoffer Glembod4c41132010-02-15 03:33:44 +00001241
Daniel Hellstrom2436af82011-01-14 03:02:41 +00001242 GRETH_REGSAVE(greth->regs->control, ctrl);
Kristoffer Glembod4c41132010-02-15 03:33:44 +00001243 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 */
Grant Likely74888762011-02-22 21:05:51 -07001376static int __devinit greth_of_probe(struct platform_device *ofdev)
Kristoffer Glembod4c41132010-02-15 03:33:44 +00001377{
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;
Grant Likely19e48752010-08-08 00:23:26 -06001415 greth->irq = ofdev->archdata.irqs[0];
Kristoffer Glembod4c41132010-02-15 03:33:44 +00001416
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;
Grant Likely61c7a082010-04-13 16:12:29 -07001502 addr = of_get_property(ofdev->dev.of_node, "local-mac-address",
1503 &len);
Kristoffer Glembod4c41132010-02-15 03:33:44 +00001504 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) {
Michał Mirosław131ae322011-04-17 00:15:47 +00001535 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM |
1536 NETIF_F_RXCSUM;
1537 dev->features = dev->hw_features | NETIF_F_HIGHDMA;
Kristoffer Glembod4c41132010-02-15 03:33:44 +00001538 greth_netdev_ops.ndo_start_xmit = greth_start_xmit_gbit;
Kristoffer Glembod4c41132010-02-15 03:33:44 +00001539 }
1540
1541 if (greth->multicast) {
1542 greth_netdev_ops.ndo_set_multicast_list = greth_set_multicast_list;
1543 dev->flags |= IFF_MULTICAST;
1544 } else {
1545 dev->flags &= ~IFF_MULTICAST;
1546 }
1547
1548 dev->netdev_ops = &greth_netdev_ops;
1549 dev->ethtool_ops = &greth_ethtool_ops;
1550
Tobias Klausercb5d9912010-08-17 06:11:24 +00001551 err = register_netdev(dev);
1552 if (err) {
Kristoffer Glembod4c41132010-02-15 03:33:44 +00001553 if (netif_msg_probe(greth))
1554 dev_err(greth->dev, "netdevice registration failed.\n");
Kristoffer Glembod4c41132010-02-15 03:33:44 +00001555 goto error5;
1556 }
1557
1558 /* setup NAPI */
Kristoffer Glembod4c41132010-02-15 03:33:44 +00001559 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
Grant Likely2dc11582010-08-06 09:25:50 -06001576static int __devexit greth_of_remove(struct platform_device *of_dev)
Kristoffer Glembod4c41132010-02-15 03:33:44 +00001577{
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 },
Daniel Hellstromad4650a2011-01-14 03:02:37 +00001604 {
1605 .name = "01_01d",
1606 },
Kristoffer Glembod4c41132010-02-15 03:33:44 +00001607 {},
1608};
1609
1610MODULE_DEVICE_TABLE(of, greth_of_match);
1611
Grant Likely74888762011-02-22 21:05:51 -07001612static struct platform_driver greth_of_driver = {
David S. Millerbc284f92010-05-31 05:47:32 -07001613 .driver = {
1614 .name = "grlib-greth",
1615 .owner = THIS_MODULE,
1616 .of_match_table = greth_of_match,
1617 },
Kristoffer Glembod4c41132010-02-15 03:33:44 +00001618 .probe = greth_of_probe,
1619 .remove = __devexit_p(greth_of_remove),
Kristoffer Glembod4c41132010-02-15 03:33:44 +00001620};
1621
1622static int __init greth_init(void)
1623{
Grant Likely74888762011-02-22 21:05:51 -07001624 return platform_driver_register(&greth_of_driver);
Kristoffer Glembod4c41132010-02-15 03:33:44 +00001625}
1626
1627static void __exit greth_cleanup(void)
1628{
Grant Likely74888762011-02-22 21:05:51 -07001629 platform_driver_unregister(&greth_of_driver);
Kristoffer Glembod4c41132010-02-15 03:33:44 +00001630}
1631
1632module_init(greth_init);
1633module_exit(greth_cleanup);
1634
1635MODULE_AUTHOR("Aeroflex Gaisler AB.");
1636MODULE_DESCRIPTION("Aeroflex Gaisler Ethernet MAC driver");
1637MODULE_LICENSE("GPL");