aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKristoffer Glembo <kristoffer@gaisler.com>2010-02-14 22:33:44 -0500
committerDavid S. Miller <davem@davemloft.net>2010-02-17 18:59:40 -0500
commitd4c41139df6e74c6fff0cbac43e51cab782133be (patch)
treec0831f0896b680d272e3e20314c8b47efb9ca48c
parentb6ca430599ea37843632b0eaa231dea5414dec25 (diff)
net: Add Aeroflex Gaisler 10/100/1G Ethernet MAC driver
Adds device driver for Aeroflex Gaisler 10/100 and 10/100/1G Ethernet MAC IP cores. Signed-off-by: Kristoffer Glembo <kristoffer@gaisler.com> Signed-off-by: David S. Miller <davem@davemloft.net>
-rw-r--r--MAINTAINERS6
-rw-r--r--drivers/net/Kconfig8
-rw-r--r--drivers/net/Makefile1
-rw-r--r--drivers/net/greth.c1645
-rw-r--r--drivers/net/greth.h143
5 files changed, 1803 insertions, 0 deletions
diff --git a/MAINTAINERS b/MAINTAINERS
index 64a237bb01ab..32f6915ae0eb 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -2373,6 +2373,12 @@ F: Documentation/isdn/README.gigaset
2373F: drivers/isdn/gigaset/ 2373F: drivers/isdn/gigaset/
2374F: include/linux/gigaset_dev.h 2374F: include/linux/gigaset_dev.h
2375 2375
2376GRETH 10/100/1G Ethernet MAC device driver
2377M: Kristoffer Glembo <kristoffer@gaisler.com>
2378L: netdev@vger.kernel.org
2379S: Maintained
2380F: drivers/net/greth*
2381
2376HARD DRIVE ACTIVE PROTECTION SYSTEM (HDAPS) DRIVER 2382HARD DRIVE ACTIVE PROTECTION SYSTEM (HDAPS) DRIVER
2377M: Frank Seidel <frank@f-seidel.de> 2383M: Frank Seidel <frank@f-seidel.de>
2378L: lm-sensors@lm-sensors.org 2384L: lm-sensors@lm-sensors.org
diff --git a/drivers/net/Kconfig b/drivers/net/Kconfig
index 069057796bd7..17ff15f6099a 100644
--- a/drivers/net/Kconfig
+++ b/drivers/net/Kconfig
@@ -995,6 +995,14 @@ config ETHOC
995 help 995 help
996 Say Y here if you want to use the OpenCores 10/100 Mbps Ethernet MAC. 996 Say Y here if you want to use the OpenCores 10/100 Mbps Ethernet MAC.
997 997
998config GRETH
999 tristate "Aeroflex Gaisler GRETH Ethernet MAC support"
1000 depends on OF
1001 select PHYLIB
1002 select CRC32
1003 help
1004 Say Y here if you want to use the Aeroflex Gaisler GRETH Ethernet MAC.
1005
998config SMC911X 1006config SMC911X
999 tristate "SMSC LAN911[5678] support" 1007 tristate "SMSC LAN911[5678] support"
1000 select CRC32 1008 select CRC32
diff --git a/drivers/net/Makefile b/drivers/net/Makefile
index 622cfd450d48..478886234c28 100644
--- a/drivers/net/Makefile
+++ b/drivers/net/Makefile
@@ -250,6 +250,7 @@ pasemi_mac_driver-objs := pasemi_mac.o pasemi_mac_ethtool.o
250obj-$(CONFIG_MLX4_CORE) += mlx4/ 250obj-$(CONFIG_MLX4_CORE) += mlx4/
251obj-$(CONFIG_ENC28J60) += enc28j60.o 251obj-$(CONFIG_ENC28J60) += enc28j60.o
252obj-$(CONFIG_ETHOC) += ethoc.o 252obj-$(CONFIG_ETHOC) += ethoc.o
253obj-$(CONFIG_GRETH) += greth.o
253 254
254obj-$(CONFIG_XTENSA_XT2000_SONIC) += xtsonic.o 255obj-$(CONFIG_XTENSA_XT2000_SONIC) += xtsonic.o
255 256
diff --git a/drivers/net/greth.c b/drivers/net/greth.c
new file mode 100644
index 000000000000..457da1c2383c
--- /dev/null
+++ b/drivers/net/greth.c
@@ -0,0 +1,1645 @@
1/*
2 * Aeroflex Gaisler GRETH 10/100/1G Ethernet MAC.
3 *
4 * 2005-2009 (c) Aeroflex Gaisler AB
5 *
6 * This driver supports GRETH 10/100 and GRETH 10/100/1G Ethernet MACs
7 * available in the GRLIB VHDL IP core library.
8 *
9 * Full documentation of both cores can be found here:
10 * http://www.gaisler.com/products/grlib/grip.pdf
11 *
12 * The Gigabit version supports scatter/gather DMA, any alignment of
13 * buffers and checksum offloading.
14 *
15 * This program is free software; you can redistribute it and/or modify it
16 * under the terms of the GNU General Public License as published by the
17 * Free Software Foundation; either version 2 of the License, or (at your
18 * option) any later version.
19 *
20 * Contributors: Kristoffer Glembo
21 * Daniel Hellstrom
22 * Marko Isomaki
23 */
24
25#include <linux/module.h>
26#include <linux/uaccess.h>
27#include <linux/init.h>
28#include <linux/netdevice.h>
29#include <linux/etherdevice.h>
30#include <linux/ethtool.h>
31#include <linux/skbuff.h>
32#include <linux/io.h>
33#include <linux/crc32.h>
34#include <linux/mii.h>
35#include <linux/of_device.h>
36#include <linux/of_platform.h>
37#include <asm/cacheflush.h>
38#include <asm/byteorder.h>
39
40#ifdef CONFIG_SPARC
41#include <asm/idprom.h>
42#endif
43
44#include "greth.h"
45
46#define GRETH_DEF_MSG_ENABLE \
47 (NETIF_MSG_DRV | \
48 NETIF_MSG_PROBE | \
49 NETIF_MSG_LINK | \
50 NETIF_MSG_IFDOWN | \
51 NETIF_MSG_IFUP | \
52 NETIF_MSG_RX_ERR | \
53 NETIF_MSG_TX_ERR)
54
55static int greth_debug = -1; /* -1 == use GRETH_DEF_MSG_ENABLE as value */
56module_param(greth_debug, int, 0);
57MODULE_PARM_DESC(greth_debug, "GRETH bitmapped debugging message enable value");
58
59/* Accept MAC address of the form macaddr=0x08,0x00,0x20,0x30,0x40,0x50 */
60static int macaddr[6];
61module_param_array(macaddr, int, NULL, 0);
62MODULE_PARM_DESC(macaddr, "GRETH Ethernet MAC address");
63
64static int greth_edcl = 1;
65module_param(greth_edcl, int, 0);
66MODULE_PARM_DESC(greth_edcl, "GRETH EDCL usage indicator. Set to 1 if EDCL is used.");
67
68static int greth_open(struct net_device *dev);
69static int greth_start_xmit(struct sk_buff *skb, struct net_device *dev);
70static int greth_start_xmit_gbit(struct sk_buff *skb, struct net_device *dev);
71static int greth_rx(struct net_device *dev, int limit);
72static int greth_rx_gbit(struct net_device *dev, int limit);
73static void greth_clean_tx(struct net_device *dev);
74static void greth_clean_tx_gbit(struct net_device *dev);
75static irqreturn_t greth_interrupt(int irq, void *dev_id);
76static int greth_close(struct net_device *dev);
77static int greth_set_mac_add(struct net_device *dev, void *p);
78static void greth_set_multicast_list(struct net_device *dev);
79
80#define GRETH_REGLOAD(a) (be32_to_cpu(__raw_readl(&(a))))
81#define GRETH_REGSAVE(a, v) (__raw_writel(cpu_to_be32(v), &(a)))
82#define GRETH_REGORIN(a, v) (GRETH_REGSAVE(a, (GRETH_REGLOAD(a) | (v))))
83#define GRETH_REGANDIN(a, v) (GRETH_REGSAVE(a, (GRETH_REGLOAD(a) & (v))))
84
85#define NEXT_TX(N) (((N) + 1) & GRETH_TXBD_NUM_MASK)
86#define SKIP_TX(N, C) (((N) + C) & GRETH_TXBD_NUM_MASK)
87#define NEXT_RX(N) (((N) + 1) & GRETH_RXBD_NUM_MASK)
88
89static void greth_print_rx_packet(void *addr, int len)
90{
91 print_hex_dump(KERN_DEBUG, "RX: ", DUMP_PREFIX_OFFSET, 16, 1,
92 addr, len, true);
93}
94
95static void greth_print_tx_packet(struct sk_buff *skb)
96{
97 int i;
98 int length;
99
100 if (skb_shinfo(skb)->nr_frags == 0)
101 length = skb->len;
102 else
103 length = skb_headlen(skb);
104
105 print_hex_dump(KERN_DEBUG, "TX: ", DUMP_PREFIX_OFFSET, 16, 1,
106 skb->data, length, true);
107
108 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
109
110 print_hex_dump(KERN_DEBUG, "TX: ", DUMP_PREFIX_OFFSET, 16, 1,
111 phys_to_virt(page_to_phys(skb_shinfo(skb)->frags[i].page)) +
112 skb_shinfo(skb)->frags[i].page_offset,
113 length, true);
114 }
115}
116
117static inline void greth_enable_tx(struct greth_private *greth)
118{
119 wmb();
120 GRETH_REGORIN(greth->regs->control, GRETH_TXEN);
121}
122
123static inline void greth_disable_tx(struct greth_private *greth)
124{
125 GRETH_REGANDIN(greth->regs->control, ~GRETH_TXEN);
126}
127
128static inline void greth_enable_rx(struct greth_private *greth)
129{
130 wmb();
131 GRETH_REGORIN(greth->regs->control, GRETH_RXEN);
132}
133
134static inline void greth_disable_rx(struct greth_private *greth)
135{
136 GRETH_REGANDIN(greth->regs->control, ~GRETH_RXEN);
137}
138
139static inline void greth_enable_irqs(struct greth_private *greth)
140{
141 GRETH_REGORIN(greth->regs->control, GRETH_RXI | GRETH_TXI);
142}
143
144static inline void greth_disable_irqs(struct greth_private *greth)
145{
146 GRETH_REGANDIN(greth->regs->control, ~(GRETH_RXI|GRETH_TXI));
147}
148
149static inline void greth_write_bd(u32 *bd, u32 val)
150{
151 __raw_writel(cpu_to_be32(val), bd);
152}
153
154static inline u32 greth_read_bd(u32 *bd)
155{
156 return be32_to_cpu(__raw_readl(bd));
157}
158
159static void greth_clean_rings(struct greth_private *greth)
160{
161 int i;
162 struct greth_bd *rx_bdp = greth->rx_bd_base;
163 struct greth_bd *tx_bdp = greth->tx_bd_base;
164
165 if (greth->gbit_mac) {
166
167 /* Free and unmap RX buffers */
168 for (i = 0; i < GRETH_RXBD_NUM; i++, rx_bdp++) {
169 if (greth->rx_skbuff[i] != NULL) {
170 dev_kfree_skb(greth->rx_skbuff[i]);
171 dma_unmap_single(greth->dev,
172 greth_read_bd(&rx_bdp->addr),
173 MAX_FRAME_SIZE+NET_IP_ALIGN,
174 DMA_FROM_DEVICE);
175 }
176 }
177
178 /* TX buffers */
179 while (greth->tx_free < GRETH_TXBD_NUM) {
180
181 struct sk_buff *skb = greth->tx_skbuff[greth->tx_last];
182 int nr_frags = skb_shinfo(skb)->nr_frags;
183 tx_bdp = greth->tx_bd_base + greth->tx_last;
184 greth->tx_last = NEXT_TX(greth->tx_last);
185
186 dma_unmap_single(greth->dev,
187 greth_read_bd(&tx_bdp->addr),
188 skb_headlen(skb),
189 DMA_TO_DEVICE);
190
191 for (i = 0; i < nr_frags; i++) {
192 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
193 tx_bdp = greth->tx_bd_base + greth->tx_last;
194
195 dma_unmap_page(greth->dev,
196 greth_read_bd(&tx_bdp->addr),
197 frag->size,
198 DMA_TO_DEVICE);
199
200 greth->tx_last = NEXT_TX(greth->tx_last);
201 }
202 greth->tx_free += nr_frags+1;
203 dev_kfree_skb(skb);
204 }
205
206
207 } else { /* 10/100 Mbps MAC */
208
209 for (i = 0; i < GRETH_RXBD_NUM; i++, rx_bdp++) {
210 kfree(greth->rx_bufs[i]);
211 dma_unmap_single(greth->dev,
212 greth_read_bd(&rx_bdp->addr),
213 MAX_FRAME_SIZE,
214 DMA_FROM_DEVICE);
215 }
216 for (i = 0; i < GRETH_TXBD_NUM; i++, tx_bdp++) {
217 kfree(greth->tx_bufs[i]);
218 dma_unmap_single(greth->dev,
219 greth_read_bd(&tx_bdp->addr),
220 MAX_FRAME_SIZE,
221 DMA_TO_DEVICE);
222 }
223 }
224}
225
226static int greth_init_rings(struct greth_private *greth)
227{
228 struct sk_buff *skb;
229 struct greth_bd *rx_bd, *tx_bd;
230 u32 dma_addr;
231 int i;
232
233 rx_bd = greth->rx_bd_base;
234 tx_bd = greth->tx_bd_base;
235
236 /* Initialize descriptor rings and buffers */
237 if (greth->gbit_mac) {
238
239 for (i = 0; i < GRETH_RXBD_NUM; i++) {
240 skb = netdev_alloc_skb(greth->netdev, MAX_FRAME_SIZE+NET_IP_ALIGN);
241 if (skb == NULL) {
242 if (netif_msg_ifup(greth))
243 dev_err(greth->dev, "Error allocating DMA ring.\n");
244 goto cleanup;
245 }
246 skb_reserve(skb, NET_IP_ALIGN);
247 dma_addr = dma_map_single(greth->dev,
248 skb->data,
249 MAX_FRAME_SIZE+NET_IP_ALIGN,
250 DMA_FROM_DEVICE);
251
252 if (dma_mapping_error(greth->dev, dma_addr)) {
253 if (netif_msg_ifup(greth))
254 dev_err(greth->dev, "Could not create initial DMA mapping\n");
255 goto cleanup;
256 }
257 greth->rx_skbuff[i] = skb;
258 greth_write_bd(&rx_bd[i].addr, dma_addr);
259 greth_write_bd(&rx_bd[i].stat, GRETH_BD_EN | GRETH_BD_IE);
260 }
261
262 } else {
263
264 /* 10/100 MAC uses a fixed set of buffers and copy to/from SKBs */
265 for (i = 0; i < GRETH_RXBD_NUM; i++) {
266
267 greth->rx_bufs[i] = kmalloc(MAX_FRAME_SIZE, GFP_KERNEL);
268
269 if (greth->rx_bufs[i] == NULL) {
270 if (netif_msg_ifup(greth))
271 dev_err(greth->dev, "Error allocating DMA ring.\n");
272 goto cleanup;
273 }
274
275 dma_addr = dma_map_single(greth->dev,
276 greth->rx_bufs[i],
277 MAX_FRAME_SIZE,
278 DMA_FROM_DEVICE);
279
280 if (dma_mapping_error(greth->dev, dma_addr)) {
281 if (netif_msg_ifup(greth))
282 dev_err(greth->dev, "Could not create initial DMA mapping\n");
283 goto cleanup;
284 }
285 greth_write_bd(&rx_bd[i].addr, dma_addr);
286 greth_write_bd(&rx_bd[i].stat, GRETH_BD_EN | GRETH_BD_IE);
287 }
288 for (i = 0; i < GRETH_TXBD_NUM; i++) {
289
290 greth->tx_bufs[i] = kmalloc(MAX_FRAME_SIZE, GFP_KERNEL);
291
292 if (greth->tx_bufs[i] == NULL) {
293 if (netif_msg_ifup(greth))
294 dev_err(greth->dev, "Error allocating DMA ring.\n");
295 goto cleanup;
296 }
297
298 dma_addr = dma_map_single(greth->dev,
299 greth->tx_bufs[i],
300 MAX_FRAME_SIZE,
301 DMA_TO_DEVICE);
302
303 if (dma_mapping_error(greth->dev, dma_addr)) {
304 if (netif_msg_ifup(greth))
305 dev_err(greth->dev, "Could not create initial DMA mapping\n");
306 goto cleanup;
307 }
308 greth_write_bd(&tx_bd[i].addr, dma_addr);
309 greth_write_bd(&tx_bd[i].stat, 0);
310 }
311 }
312 greth_write_bd(&rx_bd[GRETH_RXBD_NUM - 1].stat,
313 greth_read_bd(&rx_bd[GRETH_RXBD_NUM - 1].stat) | GRETH_BD_WR);
314
315 /* Initialize pointers. */
316 greth->rx_cur = 0;
317 greth->tx_next = 0;
318 greth->tx_last = 0;
319 greth->tx_free = GRETH_TXBD_NUM;
320
321 /* Initialize descriptor base address */
322 GRETH_REGSAVE(greth->regs->tx_desc_p, greth->tx_bd_base_phys);
323 GRETH_REGSAVE(greth->regs->rx_desc_p, greth->rx_bd_base_phys);
324
325 return 0;
326
327cleanup:
328 greth_clean_rings(greth);
329 return -ENOMEM;
330}
331
332static int greth_open(struct net_device *dev)
333{
334 struct greth_private *greth = netdev_priv(dev);
335 int err;
336
337 err = greth_init_rings(greth);
338 if (err) {
339 if (netif_msg_ifup(greth))
340 dev_err(&dev->dev, "Could not allocate memory for DMA rings\n");
341 return err;
342 }
343
344 err = request_irq(greth->irq, greth_interrupt, 0, "eth", (void *) dev);
345 if (err) {
346 if (netif_msg_ifup(greth))
347 dev_err(&dev->dev, "Could not allocate interrupt %d\n", dev->irq);
348 greth_clean_rings(greth);
349 return err;
350 }
351
352 if (netif_msg_ifup(greth))
353 dev_dbg(&dev->dev, " starting queue\n");
354 netif_start_queue(dev);
355
356 napi_enable(&greth->napi);
357
358 greth_enable_irqs(greth);
359 greth_enable_tx(greth);
360 greth_enable_rx(greth);
361 return 0;
362
363}
364
365static int greth_close(struct net_device *dev)
366{
367 struct greth_private *greth = netdev_priv(dev);
368
369 napi_disable(&greth->napi);
370
371 greth_disable_tx(greth);
372
373 netif_stop_queue(dev);
374
375 free_irq(greth->irq, (void *) dev);
376
377 greth_clean_rings(greth);
378
379 return 0;
380}
381
382static int greth_start_xmit(struct sk_buff *skb, struct net_device *dev)
383{
384 struct greth_private *greth = netdev_priv(dev);
385 struct greth_bd *bdp;
386 int err = NETDEV_TX_OK;
387 u32 status, dma_addr;
388
389 bdp = greth->tx_bd_base + greth->tx_next;
390
391 if (unlikely(greth->tx_free <= 0)) {
392 netif_stop_queue(dev);
393 return NETDEV_TX_BUSY;
394 }
395
396 if (netif_msg_pktdata(greth))
397 greth_print_tx_packet(skb);
398
399
400 if (unlikely(skb->len > MAX_FRAME_SIZE)) {
401 dev->stats.tx_errors++;
402 goto out;
403 }
404
405 dma_addr = greth_read_bd(&bdp->addr);
406
407 memcpy((unsigned char *) phys_to_virt(dma_addr), skb->data, skb->len);
408
409 dma_sync_single_for_device(greth->dev, dma_addr, skb->len, DMA_TO_DEVICE);
410
411 status = GRETH_BD_EN | (skb->len & GRETH_BD_LEN);
412
413 /* Wrap around descriptor ring */
414 if (greth->tx_next == GRETH_TXBD_NUM_MASK) {
415 status |= GRETH_BD_WR;
416 }
417
418 greth->tx_next = NEXT_TX(greth->tx_next);
419 greth->tx_free--;
420
421 /* No more descriptors */
422 if (unlikely(greth->tx_free == 0)) {
423
424 /* Free transmitted descriptors */
425 greth_clean_tx(dev);
426
427 /* If nothing was cleaned, stop queue & wait for irq */
428 if (unlikely(greth->tx_free == 0)) {
429 status |= GRETH_BD_IE;
430 netif_stop_queue(dev);
431 }
432 }
433
434 /* Write descriptor control word and enable transmission */
435 greth_write_bd(&bdp->stat, status);
436 greth_enable_tx(greth);
437
438out:
439 dev_kfree_skb(skb);
440 return err;
441}
442
443
444static int greth_start_xmit_gbit(struct sk_buff *skb, struct net_device *dev)
445{
446 struct greth_private *greth = netdev_priv(dev);
447 struct greth_bd *bdp;
448 u32 status = 0, dma_addr;
449 int curr_tx, nr_frags, i, err = NETDEV_TX_OK;
450
451 nr_frags = skb_shinfo(skb)->nr_frags;
452
453 if (greth->tx_free < nr_frags + 1) {
454 netif_stop_queue(dev);
455 err = NETDEV_TX_BUSY;
456 goto out;
457 }
458
459 if (netif_msg_pktdata(greth))
460 greth_print_tx_packet(skb);
461
462 if (unlikely(skb->len > MAX_FRAME_SIZE)) {
463 dev->stats.tx_errors++;
464 goto out;
465 }
466
467 /* Save skb pointer. */
468 greth->tx_skbuff[greth->tx_next] = skb;
469
470 /* Linear buf */
471 if (nr_frags != 0)
472 status = GRETH_TXBD_MORE;
473
474 status |= GRETH_TXBD_CSALL;
475 status |= skb_headlen(skb) & GRETH_BD_LEN;
476 if (greth->tx_next == GRETH_TXBD_NUM_MASK)
477 status |= GRETH_BD_WR;
478
479
480 bdp = greth->tx_bd_base + greth->tx_next;
481 greth_write_bd(&bdp->stat, status);
482 dma_addr = dma_map_single(greth->dev, skb->data, skb_headlen(skb), DMA_TO_DEVICE);
483
484 if (unlikely(dma_mapping_error(greth->dev, dma_addr)))
485 goto map_error;
486
487 greth_write_bd(&bdp->addr, dma_addr);
488
489 curr_tx = NEXT_TX(greth->tx_next);
490
491 /* Frags */
492 for (i = 0; i < nr_frags; i++) {
493 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
494 greth->tx_skbuff[curr_tx] = NULL;
495 bdp = greth->tx_bd_base + curr_tx;
496
497 status = GRETH_TXBD_CSALL;
498 status |= frag->size & GRETH_BD_LEN;
499
500 /* Wrap around descriptor ring */
501 if (curr_tx == GRETH_TXBD_NUM_MASK)
502 status |= GRETH_BD_WR;
503
504 /* More fragments left */
505 if (i < nr_frags - 1)
506 status |= GRETH_TXBD_MORE;
507
508 /* ... last fragment, check if out of descriptors */
509 else if (greth->tx_free - nr_frags - 1 < (MAX_SKB_FRAGS + 1)) {
510
511 /* Enable interrupts and stop queue */
512 status |= GRETH_BD_IE;
513 netif_stop_queue(dev);
514 }
515
516 greth_write_bd(&bdp->stat, status);
517
518 dma_addr = dma_map_page(greth->dev,
519 frag->page,
520 frag->page_offset,
521 frag->size,
522 DMA_TO_DEVICE);
523
524 if (unlikely(dma_mapping_error(greth->dev, dma_addr)))
525 goto frag_map_error;
526
527 greth_write_bd(&bdp->addr, dma_addr);
528
529 curr_tx = NEXT_TX(curr_tx);
530 }
531
532 wmb();
533
534 /* Enable the descriptors that we configured ... */
535 for (i = 0; i < nr_frags + 1; i++) {
536 bdp = greth->tx_bd_base + greth->tx_next;
537 greth_write_bd(&bdp->stat, greth_read_bd(&bdp->stat) | GRETH_BD_EN);
538 greth->tx_next = NEXT_TX(greth->tx_next);
539 greth->tx_free--;
540 }
541
542 greth_enable_tx(greth);
543
544 return NETDEV_TX_OK;
545
546frag_map_error:
547 /* Unmap SKB mappings that succeeded */
548 for (i = 0; greth->tx_next + i != curr_tx; i++) {
549 bdp = greth->tx_bd_base + greth->tx_next + i;
550 dma_unmap_single(greth->dev,
551 greth_read_bd(&bdp->addr),
552 greth_read_bd(&bdp->stat) & GRETH_BD_LEN,
553 DMA_TO_DEVICE);
554 }
555map_error:
556 if (net_ratelimit())
557 dev_warn(greth->dev, "Could not create TX DMA mapping\n");
558 dev_kfree_skb(skb);
559 return NETDEV_TX_OK;
560
561out:
562 return err;
563}
564
565
566static irqreturn_t greth_interrupt(int irq, void *dev_id)
567{
568 struct net_device *dev = dev_id;
569 struct greth_private *greth;
570 u32 status;
571 irqreturn_t retval = IRQ_NONE;
572
573 greth = netdev_priv(dev);
574
575 spin_lock(&greth->devlock);
576
577 /* Get the interrupt events that caused us to be here. */
578 status = GRETH_REGLOAD(greth->regs->status);
579
580 /* Handle rx and tx interrupts through poll */
581 if (status & (GRETH_INT_RX | GRETH_INT_TX)) {
582
583 /* Clear interrupt status */
584 GRETH_REGORIN(greth->regs->status,
585 status & (GRETH_INT_RX | GRETH_INT_TX));
586
587 retval = IRQ_HANDLED;
588
589 /* Disable interrupts and schedule poll() */
590 greth_disable_irqs(greth);
591 napi_schedule(&greth->napi);
592 }
593
594 mmiowb();
595 spin_unlock(&greth->devlock);
596
597 return retval;
598}
599
600static void greth_clean_tx(struct net_device *dev)
601{
602 struct greth_private *greth;
603 struct greth_bd *bdp;
604 u32 stat;
605
606 greth = netdev_priv(dev);
607
608 while (1) {
609 bdp = greth->tx_bd_base + greth->tx_last;
610 stat = greth_read_bd(&bdp->stat);
611
612 if (unlikely(stat & GRETH_BD_EN))
613 break;
614
615 if (greth->tx_free == GRETH_TXBD_NUM)
616 break;
617
618 /* Check status for errors */
619 if (unlikely(stat & GRETH_TXBD_STATUS)) {
620 dev->stats.tx_errors++;
621 if (stat & GRETH_TXBD_ERR_AL)
622 dev->stats.tx_aborted_errors++;
623 if (stat & GRETH_TXBD_ERR_UE)
624 dev->stats.tx_fifo_errors++;
625 }
626 dev->stats.tx_packets++;
627 greth->tx_last = NEXT_TX(greth->tx_last);
628 greth->tx_free++;
629 }
630
631 if (greth->tx_free > 0) {
632 netif_wake_queue(dev);
633 }
634
635}
636
637static inline void greth_update_tx_stats(struct net_device *dev, u32 stat)
638{
639 /* Check status for errors */
640 if (unlikely(stat & GRETH_TXBD_STATUS)) {
641 dev->stats.tx_errors++;
642 if (stat & GRETH_TXBD_ERR_AL)
643 dev->stats.tx_aborted_errors++;
644 if (stat & GRETH_TXBD_ERR_UE)
645 dev->stats.tx_fifo_errors++;
646 if (stat & GRETH_TXBD_ERR_LC)
647 dev->stats.tx_aborted_errors++;
648 }
649 dev->stats.tx_packets++;
650}
651
652static void greth_clean_tx_gbit(struct net_device *dev)
653{
654 struct greth_private *greth;
655 struct greth_bd *bdp, *bdp_last_frag;
656 struct sk_buff *skb;
657 u32 stat;
658 int nr_frags, i;
659
660 greth = netdev_priv(dev);
661
662 while (greth->tx_free < GRETH_TXBD_NUM) {
663
664 skb = greth->tx_skbuff[greth->tx_last];
665
666 nr_frags = skb_shinfo(skb)->nr_frags;
667
668 /* We only clean fully completed SKBs */
669 bdp_last_frag = greth->tx_bd_base + SKIP_TX(greth->tx_last, nr_frags);
670 stat = bdp_last_frag->stat;
671
672 if (stat & GRETH_BD_EN)
673 break;
674
675 greth->tx_skbuff[greth->tx_last] = NULL;
676
677 greth_update_tx_stats(dev, stat);
678
679 bdp = greth->tx_bd_base + greth->tx_last;
680
681 greth->tx_last = NEXT_TX(greth->tx_last);
682
683 dma_unmap_single(greth->dev,
684 greth_read_bd(&bdp->addr),
685 skb_headlen(skb),
686 DMA_TO_DEVICE);
687
688 for (i = 0; i < nr_frags; i++) {
689 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
690 bdp = greth->tx_bd_base + greth->tx_last;
691
692 dma_unmap_page(greth->dev,
693 greth_read_bd(&bdp->addr),
694 frag->size,
695 DMA_TO_DEVICE);
696
697 greth->tx_last = NEXT_TX(greth->tx_last);
698 }
699 greth->tx_free += nr_frags+1;
700 dev_kfree_skb(skb);
701 }
702 if (greth->tx_free > (MAX_SKB_FRAGS + 1)) {
703 netif_wake_queue(dev);
704 }
705}
706
707static int greth_pending_packets(struct greth_private *greth)
708{
709 struct greth_bd *bdp;
710 u32 status;
711 bdp = greth->rx_bd_base + greth->rx_cur;
712 status = greth_read_bd(&bdp->stat);
713 if (status & GRETH_BD_EN)
714 return 0;
715 else
716 return 1;
717}
718
719static int greth_rx(struct net_device *dev, int limit)
720{
721 struct greth_private *greth;
722 struct greth_bd *bdp;
723 struct sk_buff *skb;
724 int pkt_len;
725 int bad, count;
726 u32 status, dma_addr;
727
728 greth = netdev_priv(dev);
729
730 for (count = 0; count < limit; ++count) {
731
732 bdp = greth->rx_bd_base + greth->rx_cur;
733 status = greth_read_bd(&bdp->stat);
734 dma_addr = greth_read_bd(&bdp->addr);
735 bad = 0;
736
737 if (unlikely(status & GRETH_BD_EN)) {
738 break;
739 }
740
741 /* Check status for errors. */
742 if (unlikely(status & GRETH_RXBD_STATUS)) {
743 if (status & GRETH_RXBD_ERR_FT) {
744 dev->stats.rx_length_errors++;
745 bad = 1;
746 }
747 if (status & (GRETH_RXBD_ERR_AE | GRETH_RXBD_ERR_OE)) {
748 dev->stats.rx_frame_errors++;
749 bad = 1;
750 }
751 if (status & GRETH_RXBD_ERR_CRC) {
752 dev->stats.rx_crc_errors++;
753 bad = 1;
754 }
755 }
756 if (unlikely(bad)) {
757 dev->stats.rx_errors++;
758
759 } else {
760
761 pkt_len = status & GRETH_BD_LEN;
762
763 skb = netdev_alloc_skb(dev, pkt_len + NET_IP_ALIGN);
764
765 if (unlikely(skb == NULL)) {
766
767 if (net_ratelimit())
768 dev_warn(&dev->dev, "low on memory - " "packet dropped\n");
769
770 dev->stats.rx_dropped++;
771
772 } else {
773 skb_reserve(skb, NET_IP_ALIGN);
774 skb->dev = dev;
775
776 dma_sync_single_for_cpu(greth->dev,
777 dma_addr,
778 pkt_len,
779 DMA_FROM_DEVICE);
780
781 if (netif_msg_pktdata(greth))
782 greth_print_rx_packet(phys_to_virt(dma_addr), pkt_len);
783
784 memcpy(skb_put(skb, pkt_len), phys_to_virt(dma_addr), pkt_len);
785
786 skb->protocol = eth_type_trans(skb, dev);
787 dev->stats.rx_packets++;
788 netif_receive_skb(skb);
789 }
790 }
791
792 status = GRETH_BD_EN | GRETH_BD_IE;
793 if (greth->rx_cur == GRETH_RXBD_NUM_MASK) {
794 status |= GRETH_BD_WR;
795 }
796
797 wmb();
798 greth_write_bd(&bdp->stat, status);
799
800 dma_sync_single_for_device(greth->dev, dma_addr, MAX_FRAME_SIZE, DMA_FROM_DEVICE);
801
802 greth_enable_rx(greth);
803
804 greth->rx_cur = NEXT_RX(greth->rx_cur);
805 }
806
807 return count;
808}
809
810static inline int hw_checksummed(u32 status)
811{
812
813 if (status & GRETH_RXBD_IP_FRAG)
814 return 0;
815
816 if (status & GRETH_RXBD_IP && status & GRETH_RXBD_IP_CSERR)
817 return 0;
818
819 if (status & GRETH_RXBD_UDP && status & GRETH_RXBD_UDP_CSERR)
820 return 0;
821
822 if (status & GRETH_RXBD_TCP && status & GRETH_RXBD_TCP_CSERR)
823 return 0;
824
825 return 1;
826}
827
828static int greth_rx_gbit(struct net_device *dev, int limit)
829{
830 struct greth_private *greth;
831 struct greth_bd *bdp;
832 struct sk_buff *skb, *newskb;
833 int pkt_len;
834 int bad, count = 0;
835 u32 status, dma_addr;
836
837 greth = netdev_priv(dev);
838
839 for (count = 0; count < limit; ++count) {
840
841 bdp = greth->rx_bd_base + greth->rx_cur;
842 skb = greth->rx_skbuff[greth->rx_cur];
843 status = greth_read_bd(&bdp->stat);
844 bad = 0;
845
846 if (status & GRETH_BD_EN)
847 break;
848
849 /* Check status for errors. */
850 if (unlikely(status & GRETH_RXBD_STATUS)) {
851
852 if (status & GRETH_RXBD_ERR_FT) {
853 dev->stats.rx_length_errors++;
854 bad = 1;
855 } else if (status &
856 (GRETH_RXBD_ERR_AE | GRETH_RXBD_ERR_OE | GRETH_RXBD_ERR_LE)) {
857 dev->stats.rx_frame_errors++;
858 bad = 1;
859 } else if (status & GRETH_RXBD_ERR_CRC) {
860 dev->stats.rx_crc_errors++;
861 bad = 1;
862 }
863 }
864
865 /* Allocate new skb to replace current */
866 newskb = netdev_alloc_skb(dev, MAX_FRAME_SIZE + NET_IP_ALIGN);
867
868 if (!bad && newskb) {
869 skb_reserve(newskb, NET_IP_ALIGN);
870
871 dma_addr = dma_map_single(greth->dev,
872 newskb->data,
873 MAX_FRAME_SIZE + NET_IP_ALIGN,
874 DMA_FROM_DEVICE);
875
876 if (!dma_mapping_error(greth->dev, dma_addr)) {
877 /* Process the incoming frame. */
878 pkt_len = status & GRETH_BD_LEN;
879
880 dma_unmap_single(greth->dev,
881 greth_read_bd(&bdp->addr),
882 MAX_FRAME_SIZE + NET_IP_ALIGN,
883 DMA_FROM_DEVICE);
884
885 if (netif_msg_pktdata(greth))
886 greth_print_rx_packet(phys_to_virt(greth_read_bd(&bdp->addr)), pkt_len);
887
888 skb_put(skb, pkt_len);
889
890 if (greth->flags & GRETH_FLAG_RX_CSUM && hw_checksummed(status))
891 skb->ip_summed = CHECKSUM_UNNECESSARY;
892 else
893 skb->ip_summed = CHECKSUM_NONE;
894
895 skb->dev = dev;
896 skb->protocol = eth_type_trans(skb, dev);
897 dev->stats.rx_packets++;
898 netif_receive_skb(skb);
899
900 greth->rx_skbuff[greth->rx_cur] = newskb;
901 greth_write_bd(&bdp->addr, dma_addr);
902 } else {
903 if (net_ratelimit())
904 dev_warn(greth->dev, "Could not create DMA mapping, dropping packet\n");
905 dev_kfree_skb(newskb);
906 dev->stats.rx_dropped++;
907 }
908 } else {
909 if (net_ratelimit())
910 dev_warn(greth->dev, "Could not allocate SKB, dropping packet\n");
911 dev->stats.rx_dropped++;
912 }
913
914 status = GRETH_BD_EN | GRETH_BD_IE;
915 if (greth->rx_cur == GRETH_RXBD_NUM_MASK) {
916 status |= GRETH_BD_WR;
917 }
918
919 wmb();
920 greth_write_bd(&bdp->stat, status);
921 greth_enable_rx(greth);
922 greth->rx_cur = NEXT_RX(greth->rx_cur);
923 }
924
925 return count;
926
927}
928
929static int greth_poll(struct napi_struct *napi, int budget)
930{
931 struct greth_private *greth;
932 int work_done = 0;
933 greth = container_of(napi, struct greth_private, napi);
934
935 if (greth->gbit_mac) {
936 greth_clean_tx_gbit(greth->netdev);
937 } else {
938 greth_clean_tx(greth->netdev);
939 }
940
941restart_poll:
942 if (greth->gbit_mac) {
943 work_done += greth_rx_gbit(greth->netdev, budget - work_done);
944 } else {
945 work_done += greth_rx(greth->netdev, budget - work_done);
946 }
947
948 if (work_done < budget) {
949
950 napi_complete(napi);
951
952 if (greth_pending_packets(greth)) {
953 napi_reschedule(napi);
954 goto restart_poll;
955 }
956 }
957
958 greth_enable_irqs(greth);
959 return work_done;
960}
961
962static int greth_set_mac_add(struct net_device *dev, void *p)
963{
964 struct sockaddr *addr = p;
965 struct greth_private *greth;
966 struct greth_regs *regs;
967
968 greth = (struct greth_private *) netdev_priv(dev);
969 regs = (struct greth_regs *) greth->regs;
970
971 if (!is_valid_ether_addr(addr->sa_data))
972 return -EINVAL;
973
974 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
975
976 GRETH_REGSAVE(regs->esa_msb, addr->sa_data[0] << 8 | addr->sa_data[1]);
977 GRETH_REGSAVE(regs->esa_lsb,
978 addr->sa_data[2] << 24 | addr->
979 sa_data[3] << 16 | addr->sa_data[4] << 8 | addr->sa_data[5]);
980 return 0;
981}
982
983static u32 greth_hash_get_index(__u8 *addr)
984{
985 return (ether_crc(6, addr)) & 0x3F;
986}
987
988static void greth_set_hash_filter(struct net_device *dev)
989{
990 struct dev_mc_list *curr;
991 struct greth_private *greth = (struct greth_private *) netdev_priv(dev);
992 struct greth_regs *regs = (struct greth_regs *) greth->regs;
993 u32 mc_filter[2];
994 unsigned int i, bitnr;
995
996 mc_filter[0] = mc_filter[1] = 0;
997
998 curr = dev->mc_list;
999
1000 for (i = 0; i < dev->mc_count; i++, curr = curr->next) {
1001
1002 if (!curr)
1003 break; /* unexpected end of list */
1004
1005 bitnr = greth_hash_get_index(curr->dmi_addr);
1006 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
1034 if (dev->mc_count == 0) {
1035 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,
1163};
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;
1223
1224 int status_change = 0;
1225
1226 spin_lock_irqsave(&greth->devlock, flags);
1227
1228 if (phydev->link) {
1229
1230 if ((greth->speed != phydev->speed) || (greth->duplex != phydev->duplex)) {
1231
1232 GRETH_REGANDIN(greth->regs->control,
1233 ~(GRETH_CTRL_FD | GRETH_CTRL_SP | GRETH_CTRL_GB));
1234
1235 if (phydev->duplex)
1236 GRETH_REGORIN(greth->regs->control, GRETH_CTRL_FD);
1237
1238 if (phydev->speed == SPEED_100) {
1239
1240 GRETH_REGORIN(greth->regs->control, GRETH_CTRL_SP);
1241 }
1242
1243 else if (phydev->speed == SPEED_1000)
1244 GRETH_REGORIN(greth->regs->control, GRETH_CTRL_GB);
1245
1246 greth->speed = phydev->speed;
1247 greth->duplex = phydev->duplex;
1248 status_change = 1;
1249 }
1250 }
1251
1252 if (phydev->link != greth->link) {
1253 if (!phydev->link) {
1254 greth->speed = 0;
1255 greth->duplex = -1;
1256 }
1257 greth->link = phydev->link;
1258
1259 status_change = 1;
1260 }
1261
1262 spin_unlock_irqrestore(&greth->devlock, flags);
1263
1264 if (status_change) {
1265 if (phydev->link)
1266 pr_debug("%s: link up (%d/%s)\n",
1267 dev->name, phydev->speed,
1268 DUPLEX_FULL == phydev->duplex ? "Full" : "Half");
1269 else
1270 pr_debug("%s: link down\n", dev->name);
1271 }
1272}
1273
1274static int greth_mdio_probe(struct net_device *dev)
1275{
1276 struct greth_private *greth = netdev_priv(dev);
1277 struct phy_device *phy = NULL;
1278 u32 interface;
1279 int i;
1280
1281 /* Find the first PHY */
1282 for (i = 0; i < PHY_MAX_ADDR; i++) {
1283 if (greth->mdio->phy_map[i]) {
1284 phy = greth->mdio->phy_map[i];
1285 break;
1286 }
1287 }
1288 if (!phy) {
1289 if (netif_msg_probe(greth))
1290 dev_err(&dev->dev, "no PHY found\n");
1291 return -ENXIO;
1292 }
1293
1294 if (greth->gbit_mac)
1295 interface = PHY_INTERFACE_MODE_GMII;
1296 else
1297 interface = PHY_INTERFACE_MODE_MII;
1298
1299 phy = phy_connect(dev, dev_name(&phy->dev), &greth_link_change, 0, interface);
1300
1301 if (greth->gbit_mac)
1302 phy->supported &= PHY_GBIT_FEATURES;
1303 else
1304 phy->supported &= PHY_BASIC_FEATURES;
1305
1306 phy->advertising = phy->supported;
1307
1308 if (IS_ERR(phy)) {
1309 if (netif_msg_ifup(greth))
1310 dev_err(&dev->dev, "could not attach to PHY\n");
1311 return PTR_ERR(phy);
1312 }
1313
1314 greth->link = 0;
1315 greth->speed = 0;
1316 greth->duplex = -1;
1317 greth->phy = phy;
1318
1319 return 0;
1320}
1321
1322static inline int phy_aneg_done(struct phy_device *phydev)
1323{
1324 int retval;
1325
1326 retval = phy_read(phydev, MII_BMSR);
1327
1328 return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
1329}
1330
1331static int greth_mdio_init(struct greth_private *greth)
1332{
1333 int ret, phy;
1334 unsigned long timeout;
1335
1336 greth->mdio = mdiobus_alloc();
1337 if (!greth->mdio) {
1338 return -ENOMEM;
1339 }
1340
1341 greth->mdio->name = "greth-mdio";
1342 snprintf(greth->mdio->id, MII_BUS_ID_SIZE, "%s-%d", greth->mdio->name, greth->irq);
1343 greth->mdio->read = greth_mdio_read;
1344 greth->mdio->write = greth_mdio_write;
1345 greth->mdio->reset = greth_mdio_reset;
1346 greth->mdio->priv = greth;
1347
1348 greth->mdio->irq = greth->mdio_irqs;
1349
1350 for (phy = 0; phy < PHY_MAX_ADDR; phy++)
1351 greth->mdio->irq[phy] = PHY_POLL;
1352
1353 ret = mdiobus_register(greth->mdio);
1354 if (ret) {
1355 goto error;
1356 }
1357
1358 ret = greth_mdio_probe(greth->netdev);
1359 if (ret) {
1360 if (netif_msg_probe(greth))
1361 dev_err(&greth->netdev->dev, "failed to probe MDIO bus\n");
1362 goto unreg_mdio;
1363 }
1364
1365 phy_start(greth->phy);
1366
1367 /* If Ethernet debug link is used make autoneg happen right away */
1368 if (greth->edcl && greth_edcl == 1) {
1369 phy_start_aneg(greth->phy);
1370 timeout = jiffies + 6*HZ;
1371 while (!phy_aneg_done(greth->phy) && time_before(jiffies, timeout)) {
1372 }
1373 genphy_read_status(greth->phy);
1374 greth_link_change(greth->netdev);
1375 }
1376
1377 return 0;
1378
1379unreg_mdio:
1380 mdiobus_unregister(greth->mdio);
1381error:
1382 mdiobus_free(greth->mdio);
1383 return ret;
1384}
1385
1386/* Initialize the GRETH MAC */
1387static int __devinit greth_of_probe(struct of_device *ofdev, const struct of_device_id *match)
1388{
1389 struct net_device *dev;
1390 struct greth_private *greth;
1391 struct greth_regs *regs;
1392
1393 int i;
1394 int err;
1395 int tmp;
1396 unsigned long timeout;
1397
1398 dev = alloc_etherdev(sizeof(struct greth_private));
1399
1400 if (dev == NULL)
1401 return -ENOMEM;
1402
1403 greth = netdev_priv(dev);
1404 greth->netdev = dev;
1405 greth->dev = &ofdev->dev;
1406
1407 if (greth_debug > 0)
1408 greth->msg_enable = greth_debug;
1409 else
1410 greth->msg_enable = GRETH_DEF_MSG_ENABLE;
1411
1412 spin_lock_init(&greth->devlock);
1413
1414 greth->regs = of_ioremap(&ofdev->resource[0], 0,
1415 resource_size(&ofdev->resource[0]),
1416 "grlib-greth regs");
1417
1418 if (greth->regs == NULL) {
1419 if (netif_msg_probe(greth))
1420 dev_err(greth->dev, "ioremap failure.\n");
1421 err = -EIO;
1422 goto error1;
1423 }
1424
1425 regs = (struct greth_regs *) greth->regs;
1426 greth->irq = ofdev->irqs[0];
1427
1428 dev_set_drvdata(greth->dev, dev);
1429 SET_NETDEV_DEV(dev, greth->dev);
1430
1431 if (netif_msg_probe(greth))
1432 dev_dbg(greth->dev, "reseting controller.\n");
1433
1434 /* Reset the controller. */
1435 GRETH_REGSAVE(regs->control, GRETH_RESET);
1436
1437 /* Wait for MAC to reset itself */
1438 timeout = jiffies + HZ/100;
1439 while (GRETH_REGLOAD(regs->control) & GRETH_RESET) {
1440 if (time_after(jiffies, timeout)) {
1441 err = -EIO;
1442 if (netif_msg_probe(greth))
1443 dev_err(greth->dev, "timeout when waiting for reset.\n");
1444 goto error2;
1445 }
1446 }
1447
1448 /* Get default PHY address */
1449 greth->phyaddr = (GRETH_REGLOAD(regs->mdio) >> 11) & 0x1F;
1450
1451 /* Check if we have GBIT capable MAC */
1452 tmp = GRETH_REGLOAD(regs->control);
1453 greth->gbit_mac = (tmp >> 27) & 1;
1454
1455 /* Check for multicast capability */
1456 greth->multicast = (tmp >> 25) & 1;
1457
1458 greth->edcl = (tmp >> 31) & 1;
1459
1460 /* If we have EDCL we disable the EDCL speed-duplex FSM so
1461 * it doesn't interfere with the software */
1462 if (greth->edcl != 0)
1463 GRETH_REGORIN(regs->control, GRETH_CTRL_DISDUPLEX);
1464
1465 /* Check if MAC can handle MDIO interrupts */
1466 greth->mdio_int_en = (tmp >> 26) & 1;
1467
1468 err = greth_mdio_init(greth);
1469 if (err) {
1470 if (netif_msg_probe(greth))
1471 dev_err(greth->dev, "failed to register MDIO bus\n");
1472 goto error2;
1473 }
1474
1475 /* Allocate TX descriptor ring in coherent memory */
1476 greth->tx_bd_base = (struct greth_bd *) dma_alloc_coherent(greth->dev,
1477 1024,
1478 &greth->tx_bd_base_phys,
1479 GFP_KERNEL);
1480
1481 if (!greth->tx_bd_base) {
1482 if (netif_msg_probe(greth))
1483 dev_err(&dev->dev, "could not allocate descriptor memory.\n");
1484 err = -ENOMEM;
1485 goto error3;
1486 }
1487
1488 memset(greth->tx_bd_base, 0, 1024);
1489
1490 /* Allocate RX descriptor ring in coherent memory */
1491 greth->rx_bd_base = (struct greth_bd *) dma_alloc_coherent(greth->dev,
1492 1024,
1493 &greth->rx_bd_base_phys,
1494 GFP_KERNEL);
1495
1496 if (!greth->rx_bd_base) {
1497 if (netif_msg_probe(greth))
1498 dev_err(greth->dev, "could not allocate descriptor memory.\n");
1499 err = -ENOMEM;
1500 goto error4;
1501 }
1502
1503 memset(greth->rx_bd_base, 0, 1024);
1504
1505 /* Get MAC address from: module param, OF property or ID prom */
1506 for (i = 0; i < 6; i++) {
1507 if (macaddr[i] != 0)
1508 break;
1509 }
1510 if (i == 6) {
1511 const unsigned char *addr;
1512 int len;
1513 addr = of_get_property(ofdev->node, "local-mac-address", &len);
1514 if (addr != NULL && len == 6) {
1515 for (i = 0; i < 6; i++)
1516 macaddr[i] = (unsigned int) addr[i];
1517 } else {
1518#ifdef CONFIG_SPARC
1519 for (i = 0; i < 6; i++)
1520 macaddr[i] = (unsigned int) idprom->id_ethaddr[i];
1521#endif
1522 }
1523 }
1524
1525 for (i = 0; i < 6; i++)
1526 dev->dev_addr[i] = macaddr[i];
1527
1528 macaddr[5]++;
1529
1530 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
1531 if (netif_msg_probe(greth))
1532 dev_err(greth->dev, "no valid ethernet address, aborting.\n");
1533 err = -EINVAL;
1534 goto error5;
1535 }
1536
1537 GRETH_REGSAVE(regs->esa_msb, dev->dev_addr[0] << 8 | dev->dev_addr[1]);
1538 GRETH_REGSAVE(regs->esa_lsb, dev->dev_addr[2] << 24 | dev->dev_addr[3] << 16 |
1539 dev->dev_addr[4] << 8 | dev->dev_addr[5]);
1540
1541 /* Clear all pending interrupts except PHY irq */
1542 GRETH_REGSAVE(regs->status, 0xFF);
1543
1544 if (greth->gbit_mac) {
1545 dev->features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_HIGHDMA;
1546 greth_netdev_ops.ndo_start_xmit = greth_start_xmit_gbit;
1547 greth->flags = GRETH_FLAG_RX_CSUM;
1548 }
1549
1550 if (greth->multicast) {
1551 greth_netdev_ops.ndo_set_multicast_list = greth_set_multicast_list;
1552 dev->flags |= IFF_MULTICAST;
1553 } else {
1554 dev->flags &= ~IFF_MULTICAST;
1555 }
1556
1557 dev->netdev_ops = &greth_netdev_ops;
1558 dev->ethtool_ops = &greth_ethtool_ops;
1559
1560 if (register_netdev(dev)) {
1561 if (netif_msg_probe(greth))
1562 dev_err(greth->dev, "netdevice registration failed.\n");
1563 err = -ENOMEM;
1564 goto error5;
1565 }
1566
1567 /* setup NAPI */
1568 memset(&greth->napi, 0, sizeof(greth->napi));
1569 netif_napi_add(dev, &greth->napi, greth_poll, 64);
1570
1571 return 0;
1572
1573error5:
1574 dma_free_coherent(greth->dev, 1024, greth->rx_bd_base, greth->rx_bd_base_phys);
1575error4:
1576 dma_free_coherent(greth->dev, 1024, greth->tx_bd_base, greth->tx_bd_base_phys);
1577error3:
1578 mdiobus_unregister(greth->mdio);
1579error2:
1580 of_iounmap(&ofdev->resource[0], greth->regs, resource_size(&ofdev->resource[0]));
1581error1:
1582 free_netdev(dev);
1583 return err;
1584}
1585
1586static int __devexit greth_of_remove(struct of_device *of_dev)
1587{
1588 struct net_device *ndev = dev_get_drvdata(&of_dev->dev);
1589 struct greth_private *greth = netdev_priv(ndev);
1590
1591 /* Free descriptor areas */
1592 dma_free_coherent(&of_dev->dev, 1024, greth->rx_bd_base, greth->rx_bd_base_phys);
1593
1594 dma_free_coherent(&of_dev->dev, 1024, greth->tx_bd_base, greth->tx_bd_base_phys);
1595
1596 dev_set_drvdata(&of_dev->dev, NULL);
1597
1598 if (greth->phy)
1599 phy_stop(greth->phy);
1600 mdiobus_unregister(greth->mdio);
1601
1602 unregister_netdev(ndev);
1603 free_netdev(ndev);
1604
1605 of_iounmap(&of_dev->resource[0], greth->regs, resource_size(&of_dev->resource[0]));
1606
1607 return 0;
1608}
1609
1610static struct of_device_id greth_of_match[] = {
1611 {
1612 .name = "GAISLER_ETHMAC",
1613 },
1614 {},
1615};
1616
1617MODULE_DEVICE_TABLE(of, greth_of_match);
1618
1619static struct of_platform_driver greth_of_driver = {
1620 .name = "grlib-greth",
1621 .match_table = greth_of_match,
1622 .probe = greth_of_probe,
1623 .remove = __devexit_p(greth_of_remove),
1624 .driver = {
1625 .owner = THIS_MODULE,
1626 .name = "grlib-greth",
1627 },
1628};
1629
1630static int __init greth_init(void)
1631{
1632 return of_register_platform_driver(&greth_of_driver);
1633}
1634
1635static void __exit greth_cleanup(void)
1636{
1637 of_unregister_platform_driver(&greth_of_driver);
1638}
1639
1640module_init(greth_init);
1641module_exit(greth_cleanup);
1642
1643MODULE_AUTHOR("Aeroflex Gaisler AB.");
1644MODULE_DESCRIPTION("Aeroflex Gaisler Ethernet MAC driver");
1645MODULE_LICENSE("GPL");
diff --git a/drivers/net/greth.h b/drivers/net/greth.h
new file mode 100644
index 000000000000..973388d6abca
--- /dev/null
+++ b/drivers/net/greth.h
@@ -0,0 +1,143 @@
1#ifndef GRETH_H
2#define GRETH_H
3
4#include <linux/phy.h>
5
6/* Register bits and masks */
7#define GRETH_RESET 0x40
8#define GRETH_MII_BUSY 0x8
9#define GRETH_MII_NVALID 0x10
10
11#define GRETH_CTRL_FD 0x10
12#define GRETH_CTRL_PR 0x20
13#define GRETH_CTRL_SP 0x80
14#define GRETH_CTRL_GB 0x100
15#define GRETH_CTRL_PSTATIEN 0x400
16#define GRETH_CTRL_MCEN 0x800
17#define GRETH_CTRL_DISDUPLEX 0x1000
18#define GRETH_STATUS_PHYSTAT 0x100
19
20#define GRETH_BD_EN 0x800
21#define GRETH_BD_WR 0x1000
22#define GRETH_BD_IE 0x2000
23#define GRETH_BD_LEN 0x7FF
24
25#define GRETH_TXEN 0x1
26#define GRETH_INT_TX 0x8
27#define GRETH_TXI 0x4
28#define GRETH_TXBD_STATUS 0x0001C000
29#define GRETH_TXBD_MORE 0x20000
30#define GRETH_TXBD_IPCS 0x40000
31#define GRETH_TXBD_TCPCS 0x80000
32#define GRETH_TXBD_UDPCS 0x100000
33#define GRETH_TXBD_CSALL (GRETH_TXBD_IPCS | GRETH_TXBD_TCPCS | GRETH_TXBD_UDPCS)
34#define GRETH_TXBD_ERR_LC 0x10000
35#define GRETH_TXBD_ERR_UE 0x4000
36#define GRETH_TXBD_ERR_AL 0x8000
37
38#define GRETH_INT_RX 0x4
39#define GRETH_RXEN 0x2
40#define GRETH_RXI 0x8
41#define GRETH_RXBD_STATUS 0xFFFFC000
42#define GRETH_RXBD_ERR_AE 0x4000
43#define GRETH_RXBD_ERR_FT 0x8000
44#define GRETH_RXBD_ERR_CRC 0x10000
45#define GRETH_RXBD_ERR_OE 0x20000
46#define GRETH_RXBD_ERR_LE 0x40000
47#define GRETH_RXBD_IP 0x80000
48#define GRETH_RXBD_IP_CSERR 0x100000
49#define GRETH_RXBD_UDP 0x200000
50#define GRETH_RXBD_UDP_CSERR 0x400000
51#define GRETH_RXBD_TCP 0x800000
52#define GRETH_RXBD_TCP_CSERR 0x1000000
53#define GRETH_RXBD_IP_FRAG 0x2000000
54#define GRETH_RXBD_MCAST 0x4000000
55
56/* Descriptor parameters */
57#define GRETH_TXBD_NUM 128
58#define GRETH_TXBD_NUM_MASK (GRETH_TXBD_NUM-1)
59#define GRETH_TX_BUF_SIZE 2048
60#define GRETH_RXBD_NUM 128
61#define GRETH_RXBD_NUM_MASK (GRETH_RXBD_NUM-1)
62#define GRETH_RX_BUF_SIZE 2048
63
64/* Buffers per page */
65#define GRETH_RX_BUF_PPGAE (PAGE_SIZE/GRETH_RX_BUF_SIZE)
66#define GRETH_TX_BUF_PPGAE (PAGE_SIZE/GRETH_TX_BUF_SIZE)
67
68/* How many pages are needed for buffers */
69#define GRETH_RX_BUF_PAGE_NUM (GRETH_RXBD_NUM/GRETH_RX_BUF_PPGAE)
70#define GRETH_TX_BUF_PAGE_NUM (GRETH_TXBD_NUM/GRETH_TX_BUF_PPGAE)
71
72/* Buffer size.
73 * Gbit MAC uses tagged maximum frame size which is 1518 excluding CRC.
74 * Set to 1520 to make all buffers word aligned for non-gbit MAC.
75 */
76#define MAX_FRAME_SIZE 1520
77
78/* Flags */
79#define GRETH_FLAG_RX_CSUM 0x1
80
81/* GRETH APB registers */
82struct greth_regs {
83 u32 control;
84 u32 status;
85 u32 esa_msb;
86 u32 esa_lsb;
87 u32 mdio;
88 u32 tx_desc_p;
89 u32 rx_desc_p;
90 u32 edclip;
91 u32 hash_msb;
92 u32 hash_lsb;
93};
94
95/* GRETH buffer descriptor */
96struct greth_bd {
97 u32 stat;
98 u32 addr;
99};
100
101struct greth_private {
102 struct sk_buff *rx_skbuff[GRETH_RXBD_NUM];
103 struct sk_buff *tx_skbuff[GRETH_TXBD_NUM];
104
105 unsigned char *tx_bufs[GRETH_TXBD_NUM];
106 unsigned char *rx_bufs[GRETH_RXBD_NUM];
107
108 u16 tx_next;
109 u16 tx_last;
110 u16 tx_free;
111 u16 rx_cur;
112
113 struct greth_regs *regs; /* Address of controller registers. */
114 struct greth_bd *rx_bd_base; /* Address of Rx BDs. */
115 struct greth_bd *tx_bd_base; /* Address of Tx BDs. */
116 dma_addr_t rx_bd_base_phys;
117 dma_addr_t tx_bd_base_phys;
118
119 int irq;
120
121 struct device *dev; /* Pointer to of_device->dev */
122 struct net_device *netdev;
123 struct napi_struct napi;
124 spinlock_t devlock;
125
126 struct phy_device *phy;
127 struct mii_bus *mdio;
128 int mdio_irqs[PHY_MAX_ADDR];
129 unsigned int link;
130 unsigned int speed;
131 unsigned int duplex;
132
133 u32 msg_enable;
134 u32 flags;
135
136 u8 phyaddr;
137 u8 multicast;
138 u8 gbit_mac;
139 u8 mdio_int_en;
140 u8 edcl;
141};
142
143#endif