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