xref: /linux/drivers/net/ethernet/freescale/fs_enet/fs_enet-main.c (revision e0bf6c5ca2d3281f231c5f0c9bf145e9513644de)
1 /*
2  * Combined Ethernet driver for Motorola MPC8xx and MPC82xx.
3  *
4  * Copyright (c) 2003 Intracom S.A.
5  *  by Pantelis Antoniou <panto@intracom.gr>
6  *
7  * 2005 (c) MontaVista Software, Inc.
8  * Vitaly Bordug <vbordug@ru.mvista.com>
9  *
10  * Heavily based on original FEC driver by Dan Malek <dan@embeddededge.com>
11  * and modifications by Joakim Tjernlund <joakim.tjernlund@lumentis.se>
12  *
13  * This file is licensed under the terms of the GNU General Public License
14  * version 2. This program is licensed "as is" without any warranty of any
15  * kind, whether express or implied.
16  */
17 
18 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/types.h>
21 #include <linux/string.h>
22 #include <linux/ptrace.h>
23 #include <linux/errno.h>
24 #include <linux/ioport.h>
25 #include <linux/slab.h>
26 #include <linux/interrupt.h>
27 #include <linux/delay.h>
28 #include <linux/netdevice.h>
29 #include <linux/etherdevice.h>
30 #include <linux/skbuff.h>
31 #include <linux/spinlock.h>
32 #include <linux/mii.h>
33 #include <linux/ethtool.h>
34 #include <linux/bitops.h>
35 #include <linux/fs.h>
36 #include <linux/platform_device.h>
37 #include <linux/phy.h>
38 #include <linux/of.h>
39 #include <linux/of_mdio.h>
40 #include <linux/of_platform.h>
41 #include <linux/of_gpio.h>
42 #include <linux/of_net.h>
43 
44 #include <linux/vmalloc.h>
45 #include <asm/pgtable.h>
46 #include <asm/irq.h>
47 #include <asm/uaccess.h>
48 
49 #include "fs_enet.h"
50 
51 /*************************************************/
52 
53 MODULE_AUTHOR("Pantelis Antoniou <panto@intracom.gr>");
54 MODULE_DESCRIPTION("Freescale Ethernet Driver");
55 MODULE_LICENSE("GPL");
56 MODULE_VERSION(DRV_MODULE_VERSION);
57 
58 static int fs_enet_debug = -1; /* -1 == use FS_ENET_DEF_MSG_ENABLE as value */
59 module_param(fs_enet_debug, int, 0);
60 MODULE_PARM_DESC(fs_enet_debug,
61 		 "Freescale bitmapped debugging message enable value");
62 
63 #ifdef CONFIG_NET_POLL_CONTROLLER
64 static void fs_enet_netpoll(struct net_device *dev);
65 #endif
66 
67 static void fs_set_multicast_list(struct net_device *dev)
68 {
69 	struct fs_enet_private *fep = netdev_priv(dev);
70 
71 	(*fep->ops->set_multicast_list)(dev);
72 }
73 
74 static void skb_align(struct sk_buff *skb, int align)
75 {
76 	int off = ((unsigned long)skb->data) & (align - 1);
77 
78 	if (off)
79 		skb_reserve(skb, align - off);
80 }
81 
82 /* NAPI receive function */
83 static int fs_enet_rx_napi(struct napi_struct *napi, int budget)
84 {
85 	struct fs_enet_private *fep = container_of(napi, struct fs_enet_private, napi);
86 	struct net_device *dev = fep->ndev;
87 	const struct fs_platform_info *fpi = fep->fpi;
88 	cbd_t __iomem *bdp;
89 	struct sk_buff *skb, *skbn, *skbt;
90 	int received = 0;
91 	u16 pkt_len, sc;
92 	int curidx;
93 
94 	if (budget <= 0)
95 		return received;
96 
97 	/*
98 	 * First, grab all of the stats for the incoming packet.
99 	 * These get messed up if we get called due to a busy condition.
100 	 */
101 	bdp = fep->cur_rx;
102 
103 	/* clear RX status bits for napi*/
104 	(*fep->ops->napi_clear_rx_event)(dev);
105 
106 	while (((sc = CBDR_SC(bdp)) & BD_ENET_RX_EMPTY) == 0) {
107 		curidx = bdp - fep->rx_bd_base;
108 
109 		/*
110 		 * Since we have allocated space to hold a complete frame,
111 		 * the last indicator should be set.
112 		 */
113 		if ((sc & BD_ENET_RX_LAST) == 0)
114 			dev_warn(fep->dev, "rcv is not +last\n");
115 
116 		/*
117 		 * Check for errors.
118 		 */
119 		if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_CL |
120 			  BD_ENET_RX_NO | BD_ENET_RX_CR | BD_ENET_RX_OV)) {
121 			fep->stats.rx_errors++;
122 			/* Frame too long or too short. */
123 			if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH))
124 				fep->stats.rx_length_errors++;
125 			/* Frame alignment */
126 			if (sc & (BD_ENET_RX_NO | BD_ENET_RX_CL))
127 				fep->stats.rx_frame_errors++;
128 			/* CRC Error */
129 			if (sc & BD_ENET_RX_CR)
130 				fep->stats.rx_crc_errors++;
131 			/* FIFO overrun */
132 			if (sc & BD_ENET_RX_OV)
133 				fep->stats.rx_crc_errors++;
134 
135 			skb = fep->rx_skbuff[curidx];
136 
137 			dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
138 				L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
139 				DMA_FROM_DEVICE);
140 
141 			skbn = skb;
142 
143 		} else {
144 			skb = fep->rx_skbuff[curidx];
145 
146 			dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
147 				L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
148 				DMA_FROM_DEVICE);
149 
150 			/*
151 			 * Process the incoming frame.
152 			 */
153 			fep->stats.rx_packets++;
154 			pkt_len = CBDR_DATLEN(bdp) - 4;	/* remove CRC */
155 			fep->stats.rx_bytes += pkt_len + 4;
156 
157 			if (pkt_len <= fpi->rx_copybreak) {
158 				/* +2 to make IP header L1 cache aligned */
159 				skbn = netdev_alloc_skb(dev, pkt_len + 2);
160 				if (skbn != NULL) {
161 					skb_reserve(skbn, 2);	/* align IP header */
162 					skb_copy_from_linear_data(skb,
163 						      skbn->data, pkt_len);
164 					/* swap */
165 					skbt = skb;
166 					skb = skbn;
167 					skbn = skbt;
168 				}
169 			} else {
170 				skbn = netdev_alloc_skb(dev, ENET_RX_FRSIZE);
171 
172 				if (skbn)
173 					skb_align(skbn, ENET_RX_ALIGN);
174 			}
175 
176 			if (skbn != NULL) {
177 				skb_put(skb, pkt_len);	/* Make room */
178 				skb->protocol = eth_type_trans(skb, dev);
179 				received++;
180 				netif_receive_skb(skb);
181 			} else {
182 				fep->stats.rx_dropped++;
183 				skbn = skb;
184 			}
185 		}
186 
187 		fep->rx_skbuff[curidx] = skbn;
188 		CBDW_BUFADDR(bdp, dma_map_single(fep->dev, skbn->data,
189 			     L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
190 			     DMA_FROM_DEVICE));
191 		CBDW_DATLEN(bdp, 0);
192 		CBDW_SC(bdp, (sc & ~BD_ENET_RX_STATS) | BD_ENET_RX_EMPTY);
193 
194 		/*
195 		 * Update BD pointer to next entry.
196 		 */
197 		if ((sc & BD_ENET_RX_WRAP) == 0)
198 			bdp++;
199 		else
200 			bdp = fep->rx_bd_base;
201 
202 		(*fep->ops->rx_bd_done)(dev);
203 
204 		if (received >= budget)
205 			break;
206 	}
207 
208 	fep->cur_rx = bdp;
209 
210 	if (received < budget) {
211 		/* done */
212 		napi_complete(napi);
213 		(*fep->ops->napi_enable_rx)(dev);
214 	}
215 	return received;
216 }
217 
218 static int fs_enet_tx_napi(struct napi_struct *napi, int budget)
219 {
220 	struct fs_enet_private *fep = container_of(napi, struct fs_enet_private,
221 						   napi_tx);
222 	struct net_device *dev = fep->ndev;
223 	cbd_t __iomem *bdp;
224 	struct sk_buff *skb;
225 	int dirtyidx, do_wake, do_restart;
226 	u16 sc;
227 	int has_tx_work = 0;
228 
229 	spin_lock(&fep->tx_lock);
230 	bdp = fep->dirty_tx;
231 
232 	/* clear TX status bits for napi*/
233 	(*fep->ops->napi_clear_tx_event)(dev);
234 
235 	do_wake = do_restart = 0;
236 	while (((sc = CBDR_SC(bdp)) & BD_ENET_TX_READY) == 0) {
237 		dirtyidx = bdp - fep->tx_bd_base;
238 
239 		if (fep->tx_free == fep->tx_ring)
240 			break;
241 
242 		skb = fep->tx_skbuff[dirtyidx];
243 
244 		/*
245 		 * Check for errors.
246 		 */
247 		if (sc & (BD_ENET_TX_HB | BD_ENET_TX_LC |
248 			  BD_ENET_TX_RL | BD_ENET_TX_UN | BD_ENET_TX_CSL)) {
249 
250 			if (sc & BD_ENET_TX_HB)	/* No heartbeat */
251 				fep->stats.tx_heartbeat_errors++;
252 			if (sc & BD_ENET_TX_LC)	/* Late collision */
253 				fep->stats.tx_window_errors++;
254 			if (sc & BD_ENET_TX_RL)	/* Retrans limit */
255 				fep->stats.tx_aborted_errors++;
256 			if (sc & BD_ENET_TX_UN)	/* Underrun */
257 				fep->stats.tx_fifo_errors++;
258 			if (sc & BD_ENET_TX_CSL)	/* Carrier lost */
259 				fep->stats.tx_carrier_errors++;
260 
261 			if (sc & (BD_ENET_TX_LC | BD_ENET_TX_RL | BD_ENET_TX_UN)) {
262 				fep->stats.tx_errors++;
263 				do_restart = 1;
264 			}
265 		} else
266 			fep->stats.tx_packets++;
267 
268 		if (sc & BD_ENET_TX_READY) {
269 			dev_warn(fep->dev,
270 				 "HEY! Enet xmit interrupt and TX_READY.\n");
271 		}
272 
273 		/*
274 		 * Deferred means some collisions occurred during transmit,
275 		 * but we eventually sent the packet OK.
276 		 */
277 		if (sc & BD_ENET_TX_DEF)
278 			fep->stats.collisions++;
279 
280 		/* unmap */
281 		if (fep->mapped_as_page[dirtyidx])
282 			dma_unmap_page(fep->dev, CBDR_BUFADDR(bdp),
283 				       CBDR_DATLEN(bdp), DMA_TO_DEVICE);
284 		else
285 			dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
286 					 CBDR_DATLEN(bdp), DMA_TO_DEVICE);
287 
288 		/*
289 		 * Free the sk buffer associated with this last transmit.
290 		 */
291 		if (skb) {
292 			dev_kfree_skb(skb);
293 			fep->tx_skbuff[dirtyidx] = NULL;
294 		}
295 
296 		/*
297 		 * Update pointer to next buffer descriptor to be transmitted.
298 		 */
299 		if ((sc & BD_ENET_TX_WRAP) == 0)
300 			bdp++;
301 		else
302 			bdp = fep->tx_bd_base;
303 
304 		/*
305 		 * Since we have freed up a buffer, the ring is no longer
306 		 * full.
307 		 */
308 		if (++fep->tx_free >= MAX_SKB_FRAGS)
309 			do_wake = 1;
310 		has_tx_work = 1;
311 	}
312 
313 	fep->dirty_tx = bdp;
314 
315 	if (do_restart)
316 		(*fep->ops->tx_restart)(dev);
317 
318 	if (!has_tx_work) {
319 		napi_complete(napi);
320 		(*fep->ops->napi_enable_tx)(dev);
321 	}
322 
323 	spin_unlock(&fep->tx_lock);
324 
325 	if (do_wake)
326 		netif_wake_queue(dev);
327 
328 	if (has_tx_work)
329 		return budget;
330 	return 0;
331 }
332 
333 /*
334  * The interrupt handler.
335  * This is called from the MPC core interrupt.
336  */
337 static irqreturn_t
338 fs_enet_interrupt(int irq, void *dev_id)
339 {
340 	struct net_device *dev = dev_id;
341 	struct fs_enet_private *fep;
342 	const struct fs_platform_info *fpi;
343 	u32 int_events;
344 	u32 int_clr_events;
345 	int nr, napi_ok;
346 	int handled;
347 
348 	fep = netdev_priv(dev);
349 	fpi = fep->fpi;
350 
351 	nr = 0;
352 	while ((int_events = (*fep->ops->get_int_events)(dev)) != 0) {
353 		nr++;
354 
355 		int_clr_events = int_events;
356 		int_clr_events &= ~fep->ev_napi_rx;
357 
358 		(*fep->ops->clear_int_events)(dev, int_clr_events);
359 
360 		if (int_events & fep->ev_err)
361 			(*fep->ops->ev_error)(dev, int_events);
362 
363 		if (int_events & fep->ev_rx) {
364 			napi_ok = napi_schedule_prep(&fep->napi);
365 
366 			(*fep->ops->napi_disable_rx)(dev);
367 			(*fep->ops->clear_int_events)(dev, fep->ev_napi_rx);
368 
369 			/* NOTE: it is possible for FCCs in NAPI mode    */
370 			/* to submit a spurious interrupt while in poll  */
371 			if (napi_ok)
372 				__napi_schedule(&fep->napi);
373 		}
374 
375 		if (int_events & fep->ev_tx) {
376 			napi_ok = napi_schedule_prep(&fep->napi_tx);
377 
378 			(*fep->ops->napi_disable_tx)(dev);
379 			(*fep->ops->clear_int_events)(dev, fep->ev_napi_tx);
380 
381 			/* NOTE: it is possible for FCCs in NAPI mode    */
382 			/* to submit a spurious interrupt while in poll  */
383 			if (napi_ok)
384 				__napi_schedule(&fep->napi_tx);
385 		}
386 	}
387 
388 	handled = nr > 0;
389 	return IRQ_RETVAL(handled);
390 }
391 
392 void fs_init_bds(struct net_device *dev)
393 {
394 	struct fs_enet_private *fep = netdev_priv(dev);
395 	cbd_t __iomem *bdp;
396 	struct sk_buff *skb;
397 	int i;
398 
399 	fs_cleanup_bds(dev);
400 
401 	fep->dirty_tx = fep->cur_tx = fep->tx_bd_base;
402 	fep->tx_free = fep->tx_ring;
403 	fep->cur_rx = fep->rx_bd_base;
404 
405 	/*
406 	 * Initialize the receive buffer descriptors.
407 	 */
408 	for (i = 0, bdp = fep->rx_bd_base; i < fep->rx_ring; i++, bdp++) {
409 		skb = netdev_alloc_skb(dev, ENET_RX_FRSIZE);
410 		if (skb == NULL)
411 			break;
412 
413 		skb_align(skb, ENET_RX_ALIGN);
414 		fep->rx_skbuff[i] = skb;
415 		CBDW_BUFADDR(bdp,
416 			dma_map_single(fep->dev, skb->data,
417 				L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
418 				DMA_FROM_DEVICE));
419 		CBDW_DATLEN(bdp, 0);	/* zero */
420 		CBDW_SC(bdp, BD_ENET_RX_EMPTY |
421 			((i < fep->rx_ring - 1) ? 0 : BD_SC_WRAP));
422 	}
423 	/*
424 	 * if we failed, fillup remainder
425 	 */
426 	for (; i < fep->rx_ring; i++, bdp++) {
427 		fep->rx_skbuff[i] = NULL;
428 		CBDW_SC(bdp, (i < fep->rx_ring - 1) ? 0 : BD_SC_WRAP);
429 	}
430 
431 	/*
432 	 * ...and the same for transmit.
433 	 */
434 	for (i = 0, bdp = fep->tx_bd_base; i < fep->tx_ring; i++, bdp++) {
435 		fep->tx_skbuff[i] = NULL;
436 		CBDW_BUFADDR(bdp, 0);
437 		CBDW_DATLEN(bdp, 0);
438 		CBDW_SC(bdp, (i < fep->tx_ring - 1) ? 0 : BD_SC_WRAP);
439 	}
440 }
441 
442 void fs_cleanup_bds(struct net_device *dev)
443 {
444 	struct fs_enet_private *fep = netdev_priv(dev);
445 	struct sk_buff *skb;
446 	cbd_t __iomem *bdp;
447 	int i;
448 
449 	/*
450 	 * Reset SKB transmit buffers.
451 	 */
452 	for (i = 0, bdp = fep->tx_bd_base; i < fep->tx_ring; i++, bdp++) {
453 		if ((skb = fep->tx_skbuff[i]) == NULL)
454 			continue;
455 
456 		/* unmap */
457 		dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
458 				skb->len, DMA_TO_DEVICE);
459 
460 		fep->tx_skbuff[i] = NULL;
461 		dev_kfree_skb(skb);
462 	}
463 
464 	/*
465 	 * Reset SKB receive buffers
466 	 */
467 	for (i = 0, bdp = fep->rx_bd_base; i < fep->rx_ring; i++, bdp++) {
468 		if ((skb = fep->rx_skbuff[i]) == NULL)
469 			continue;
470 
471 		/* unmap */
472 		dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
473 			L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
474 			DMA_FROM_DEVICE);
475 
476 		fep->rx_skbuff[i] = NULL;
477 
478 		dev_kfree_skb(skb);
479 	}
480 }
481 
482 /**********************************************************************************/
483 
484 #ifdef CONFIG_FS_ENET_MPC5121_FEC
485 /*
486  * MPC5121 FEC requeries 4-byte alignment for TX data buffer!
487  */
488 static struct sk_buff *tx_skb_align_workaround(struct net_device *dev,
489 					       struct sk_buff *skb)
490 {
491 	struct sk_buff *new_skb;
492 
493 	/* Alloc new skb */
494 	new_skb = netdev_alloc_skb(dev, skb->len + 4);
495 	if (!new_skb)
496 		return NULL;
497 
498 	/* Make sure new skb is properly aligned */
499 	skb_align(new_skb, 4);
500 
501 	/* Copy data to new skb ... */
502 	skb_copy_from_linear_data(skb, new_skb->data, skb->len);
503 	skb_put(new_skb, skb->len);
504 
505 	/* ... and free an old one */
506 	dev_kfree_skb_any(skb);
507 
508 	return new_skb;
509 }
510 #endif
511 
512 static int fs_enet_start_xmit(struct sk_buff *skb, struct net_device *dev)
513 {
514 	struct fs_enet_private *fep = netdev_priv(dev);
515 	cbd_t __iomem *bdp;
516 	int curidx;
517 	u16 sc;
518 	int nr_frags = skb_shinfo(skb)->nr_frags;
519 	skb_frag_t *frag;
520 	int len;
521 
522 #ifdef CONFIG_FS_ENET_MPC5121_FEC
523 	if (((unsigned long)skb->data) & 0x3) {
524 		skb = tx_skb_align_workaround(dev, skb);
525 		if (!skb) {
526 			/*
527 			 * We have lost packet due to memory allocation error
528 			 * in tx_skb_align_workaround(). Hopefully original
529 			 * skb is still valid, so try transmit it later.
530 			 */
531 			return NETDEV_TX_BUSY;
532 		}
533 	}
534 #endif
535 	spin_lock(&fep->tx_lock);
536 
537 	/*
538 	 * Fill in a Tx ring entry
539 	 */
540 	bdp = fep->cur_tx;
541 
542 	if (fep->tx_free <= nr_frags || (CBDR_SC(bdp) & BD_ENET_TX_READY)) {
543 		netif_stop_queue(dev);
544 		spin_unlock(&fep->tx_lock);
545 
546 		/*
547 		 * Ooops.  All transmit buffers are full.  Bail out.
548 		 * This should not happen, since the tx queue should be stopped.
549 		 */
550 		dev_warn(fep->dev, "tx queue full!.\n");
551 		return NETDEV_TX_BUSY;
552 	}
553 
554 	curidx = bdp - fep->tx_bd_base;
555 
556 	len = skb->len;
557 	fep->stats.tx_bytes += len;
558 	if (nr_frags)
559 		len -= skb->data_len;
560 	fep->tx_free -= nr_frags + 1;
561 	/*
562 	 * Push the data cache so the CPM does not get stale memory data.
563 	 */
564 	CBDW_BUFADDR(bdp, dma_map_single(fep->dev,
565 				skb->data, len, DMA_TO_DEVICE));
566 	CBDW_DATLEN(bdp, len);
567 
568 	fep->mapped_as_page[curidx] = 0;
569 	frag = skb_shinfo(skb)->frags;
570 	while (nr_frags) {
571 		CBDC_SC(bdp,
572 			BD_ENET_TX_STATS | BD_ENET_TX_LAST | BD_ENET_TX_TC);
573 		CBDS_SC(bdp, BD_ENET_TX_READY);
574 
575 		if ((CBDR_SC(bdp) & BD_ENET_TX_WRAP) == 0)
576 			bdp++, curidx++;
577 		else
578 			bdp = fep->tx_bd_base, curidx = 0;
579 
580 		len = skb_frag_size(frag);
581 		CBDW_BUFADDR(bdp, skb_frag_dma_map(fep->dev, frag, 0, len,
582 						   DMA_TO_DEVICE));
583 		CBDW_DATLEN(bdp, len);
584 
585 		fep->tx_skbuff[curidx] = NULL;
586 		fep->mapped_as_page[curidx] = 1;
587 
588 		frag++;
589 		nr_frags--;
590 	}
591 
592 	/* Trigger transmission start */
593 	sc = BD_ENET_TX_READY | BD_ENET_TX_INTR |
594 	     BD_ENET_TX_LAST | BD_ENET_TX_TC;
595 
596 	/* note that while FEC does not have this bit
597 	 * it marks it as available for software use
598 	 * yay for hw reuse :) */
599 	if (skb->len <= 60)
600 		sc |= BD_ENET_TX_PAD;
601 	CBDC_SC(bdp, BD_ENET_TX_STATS);
602 	CBDS_SC(bdp, sc);
603 
604 	/* Save skb pointer. */
605 	fep->tx_skbuff[curidx] = skb;
606 
607 	/* If this was the last BD in the ring, start at the beginning again. */
608 	if ((CBDR_SC(bdp) & BD_ENET_TX_WRAP) == 0)
609 		bdp++;
610 	else
611 		bdp = fep->tx_bd_base;
612 	fep->cur_tx = bdp;
613 
614 	if (fep->tx_free < MAX_SKB_FRAGS)
615 		netif_stop_queue(dev);
616 
617 	skb_tx_timestamp(skb);
618 
619 	(*fep->ops->tx_kickstart)(dev);
620 
621 	spin_unlock(&fep->tx_lock);
622 
623 	return NETDEV_TX_OK;
624 }
625 
626 static void fs_timeout(struct net_device *dev)
627 {
628 	struct fs_enet_private *fep = netdev_priv(dev);
629 	unsigned long flags;
630 	int wake = 0;
631 
632 	fep->stats.tx_errors++;
633 
634 	spin_lock_irqsave(&fep->lock, flags);
635 
636 	if (dev->flags & IFF_UP) {
637 		phy_stop(fep->phydev);
638 		(*fep->ops->stop)(dev);
639 		(*fep->ops->restart)(dev);
640 		phy_start(fep->phydev);
641 	}
642 
643 	phy_start(fep->phydev);
644 	wake = fep->tx_free && !(CBDR_SC(fep->cur_tx) & BD_ENET_TX_READY);
645 	spin_unlock_irqrestore(&fep->lock, flags);
646 
647 	if (wake)
648 		netif_wake_queue(dev);
649 }
650 
651 /*-----------------------------------------------------------------------------
652  *  generic link-change handler - should be sufficient for most cases
653  *-----------------------------------------------------------------------------*/
654 static void generic_adjust_link(struct  net_device *dev)
655 {
656 	struct fs_enet_private *fep = netdev_priv(dev);
657 	struct phy_device *phydev = fep->phydev;
658 	int new_state = 0;
659 
660 	if (phydev->link) {
661 		/* adjust to duplex mode */
662 		if (phydev->duplex != fep->oldduplex) {
663 			new_state = 1;
664 			fep->oldduplex = phydev->duplex;
665 		}
666 
667 		if (phydev->speed != fep->oldspeed) {
668 			new_state = 1;
669 			fep->oldspeed = phydev->speed;
670 		}
671 
672 		if (!fep->oldlink) {
673 			new_state = 1;
674 			fep->oldlink = 1;
675 		}
676 
677 		if (new_state)
678 			fep->ops->restart(dev);
679 	} else if (fep->oldlink) {
680 		new_state = 1;
681 		fep->oldlink = 0;
682 		fep->oldspeed = 0;
683 		fep->oldduplex = -1;
684 	}
685 
686 	if (new_state && netif_msg_link(fep))
687 		phy_print_status(phydev);
688 }
689 
690 
691 static void fs_adjust_link(struct net_device *dev)
692 {
693 	struct fs_enet_private *fep = netdev_priv(dev);
694 	unsigned long flags;
695 
696 	spin_lock_irqsave(&fep->lock, flags);
697 
698 	if(fep->ops->adjust_link)
699 		fep->ops->adjust_link(dev);
700 	else
701 		generic_adjust_link(dev);
702 
703 	spin_unlock_irqrestore(&fep->lock, flags);
704 }
705 
706 static int fs_init_phy(struct net_device *dev)
707 {
708 	struct fs_enet_private *fep = netdev_priv(dev);
709 	struct phy_device *phydev;
710 	phy_interface_t iface;
711 
712 	fep->oldlink = 0;
713 	fep->oldspeed = 0;
714 	fep->oldduplex = -1;
715 
716 	iface = fep->fpi->use_rmii ?
717 		PHY_INTERFACE_MODE_RMII : PHY_INTERFACE_MODE_MII;
718 
719 	phydev = of_phy_connect(dev, fep->fpi->phy_node, &fs_adjust_link, 0,
720 				iface);
721 	if (!phydev) {
722 		dev_err(&dev->dev, "Could not attach to PHY\n");
723 		return -ENODEV;
724 	}
725 
726 	fep->phydev = phydev;
727 
728 	return 0;
729 }
730 
731 static int fs_enet_open(struct net_device *dev)
732 {
733 	struct fs_enet_private *fep = netdev_priv(dev);
734 	int r;
735 	int err;
736 
737 	/* to initialize the fep->cur_rx,... */
738 	/* not doing this, will cause a crash in fs_enet_rx_napi */
739 	fs_init_bds(fep->ndev);
740 
741 	napi_enable(&fep->napi);
742 	napi_enable(&fep->napi_tx);
743 
744 	/* Install our interrupt handler. */
745 	r = request_irq(fep->interrupt, fs_enet_interrupt, IRQF_SHARED,
746 			"fs_enet-mac", dev);
747 	if (r != 0) {
748 		dev_err(fep->dev, "Could not allocate FS_ENET IRQ!");
749 		napi_disable(&fep->napi);
750 		napi_disable(&fep->napi_tx);
751 		return -EINVAL;
752 	}
753 
754 	err = fs_init_phy(dev);
755 	if (err) {
756 		free_irq(fep->interrupt, dev);
757 		napi_disable(&fep->napi);
758 		napi_disable(&fep->napi_tx);
759 		return err;
760 	}
761 	phy_start(fep->phydev);
762 
763 	netif_start_queue(dev);
764 
765 	return 0;
766 }
767 
768 static int fs_enet_close(struct net_device *dev)
769 {
770 	struct fs_enet_private *fep = netdev_priv(dev);
771 	unsigned long flags;
772 
773 	netif_stop_queue(dev);
774 	netif_carrier_off(dev);
775 	napi_disable(&fep->napi);
776 	napi_disable(&fep->napi_tx);
777 	phy_stop(fep->phydev);
778 
779 	spin_lock_irqsave(&fep->lock, flags);
780 	spin_lock(&fep->tx_lock);
781 	(*fep->ops->stop)(dev);
782 	spin_unlock(&fep->tx_lock);
783 	spin_unlock_irqrestore(&fep->lock, flags);
784 
785 	/* release any irqs */
786 	phy_disconnect(fep->phydev);
787 	fep->phydev = NULL;
788 	free_irq(fep->interrupt, dev);
789 
790 	return 0;
791 }
792 
793 static struct net_device_stats *fs_enet_get_stats(struct net_device *dev)
794 {
795 	struct fs_enet_private *fep = netdev_priv(dev);
796 	return &fep->stats;
797 }
798 
799 /*************************************************************************/
800 
801 static void fs_get_drvinfo(struct net_device *dev,
802 			    struct ethtool_drvinfo *info)
803 {
804 	strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
805 	strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
806 }
807 
808 static int fs_get_regs_len(struct net_device *dev)
809 {
810 	struct fs_enet_private *fep = netdev_priv(dev);
811 
812 	return (*fep->ops->get_regs_len)(dev);
813 }
814 
815 static void fs_get_regs(struct net_device *dev, struct ethtool_regs *regs,
816 			 void *p)
817 {
818 	struct fs_enet_private *fep = netdev_priv(dev);
819 	unsigned long flags;
820 	int r, len;
821 
822 	len = regs->len;
823 
824 	spin_lock_irqsave(&fep->lock, flags);
825 	r = (*fep->ops->get_regs)(dev, p, &len);
826 	spin_unlock_irqrestore(&fep->lock, flags);
827 
828 	if (r == 0)
829 		regs->version = 0;
830 }
831 
832 static int fs_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
833 {
834 	struct fs_enet_private *fep = netdev_priv(dev);
835 
836 	if (!fep->phydev)
837 		return -ENODEV;
838 
839 	return phy_ethtool_gset(fep->phydev, cmd);
840 }
841 
842 static int fs_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
843 {
844 	struct fs_enet_private *fep = netdev_priv(dev);
845 
846 	if (!fep->phydev)
847 		return -ENODEV;
848 
849 	return phy_ethtool_sset(fep->phydev, cmd);
850 }
851 
852 static int fs_nway_reset(struct net_device *dev)
853 {
854 	return 0;
855 }
856 
857 static u32 fs_get_msglevel(struct net_device *dev)
858 {
859 	struct fs_enet_private *fep = netdev_priv(dev);
860 	return fep->msg_enable;
861 }
862 
863 static void fs_set_msglevel(struct net_device *dev, u32 value)
864 {
865 	struct fs_enet_private *fep = netdev_priv(dev);
866 	fep->msg_enable = value;
867 }
868 
869 static const struct ethtool_ops fs_ethtool_ops = {
870 	.get_drvinfo = fs_get_drvinfo,
871 	.get_regs_len = fs_get_regs_len,
872 	.get_settings = fs_get_settings,
873 	.set_settings = fs_set_settings,
874 	.nway_reset = fs_nway_reset,
875 	.get_link = ethtool_op_get_link,
876 	.get_msglevel = fs_get_msglevel,
877 	.set_msglevel = fs_set_msglevel,
878 	.get_regs = fs_get_regs,
879 	.get_ts_info = ethtool_op_get_ts_info,
880 };
881 
882 static int fs_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
883 {
884 	struct fs_enet_private *fep = netdev_priv(dev);
885 
886 	if (!netif_running(dev))
887 		return -EINVAL;
888 
889 	return phy_mii_ioctl(fep->phydev, rq, cmd);
890 }
891 
892 extern int fs_mii_connect(struct net_device *dev);
893 extern void fs_mii_disconnect(struct net_device *dev);
894 
895 /**************************************************************************************/
896 
897 #ifdef CONFIG_FS_ENET_HAS_FEC
898 #define IS_FEC(match) ((match)->data == &fs_fec_ops)
899 #else
900 #define IS_FEC(match) 0
901 #endif
902 
903 static const struct net_device_ops fs_enet_netdev_ops = {
904 	.ndo_open		= fs_enet_open,
905 	.ndo_stop		= fs_enet_close,
906 	.ndo_get_stats		= fs_enet_get_stats,
907 	.ndo_start_xmit		= fs_enet_start_xmit,
908 	.ndo_tx_timeout		= fs_timeout,
909 	.ndo_set_rx_mode	= fs_set_multicast_list,
910 	.ndo_do_ioctl		= fs_ioctl,
911 	.ndo_validate_addr	= eth_validate_addr,
912 	.ndo_set_mac_address	= eth_mac_addr,
913 	.ndo_change_mtu		= eth_change_mtu,
914 #ifdef CONFIG_NET_POLL_CONTROLLER
915 	.ndo_poll_controller	= fs_enet_netpoll,
916 #endif
917 };
918 
919 static struct of_device_id fs_enet_match[];
920 static int fs_enet_probe(struct platform_device *ofdev)
921 {
922 	const struct of_device_id *match;
923 	struct net_device *ndev;
924 	struct fs_enet_private *fep;
925 	struct fs_platform_info *fpi;
926 	const u32 *data;
927 	struct clk *clk;
928 	int err;
929 	const u8 *mac_addr;
930 	const char *phy_connection_type;
931 	int privsize, len, ret = -ENODEV;
932 
933 	match = of_match_device(fs_enet_match, &ofdev->dev);
934 	if (!match)
935 		return -EINVAL;
936 
937 	fpi = kzalloc(sizeof(*fpi), GFP_KERNEL);
938 	if (!fpi)
939 		return -ENOMEM;
940 
941 	if (!IS_FEC(match)) {
942 		data = of_get_property(ofdev->dev.of_node, "fsl,cpm-command", &len);
943 		if (!data || len != 4)
944 			goto out_free_fpi;
945 
946 		fpi->cp_command = *data;
947 	}
948 
949 	fpi->rx_ring = 32;
950 	fpi->tx_ring = 64;
951 	fpi->rx_copybreak = 240;
952 	fpi->napi_weight = 17;
953 	fpi->phy_node = of_parse_phandle(ofdev->dev.of_node, "phy-handle", 0);
954 	if (!fpi->phy_node && of_phy_is_fixed_link(ofdev->dev.of_node)) {
955 		err = of_phy_register_fixed_link(ofdev->dev.of_node);
956 		if (err)
957 			goto out_free_fpi;
958 
959 		/* In the case of a fixed PHY, the DT node associated
960 		 * to the PHY is the Ethernet MAC DT node.
961 		 */
962 		fpi->phy_node = of_node_get(ofdev->dev.of_node);
963 	}
964 
965 	if (of_device_is_compatible(ofdev->dev.of_node, "fsl,mpc5125-fec")) {
966 		phy_connection_type = of_get_property(ofdev->dev.of_node,
967 						"phy-connection-type", NULL);
968 		if (phy_connection_type && !strcmp("rmii", phy_connection_type))
969 			fpi->use_rmii = 1;
970 	}
971 
972 	/* make clock lookup non-fatal (the driver is shared among platforms),
973 	 * but require enable to succeed when a clock was specified/found,
974 	 * keep a reference to the clock upon successful acquisition
975 	 */
976 	clk = devm_clk_get(&ofdev->dev, "per");
977 	if (!IS_ERR(clk)) {
978 		err = clk_prepare_enable(clk);
979 		if (err) {
980 			ret = err;
981 			goto out_free_fpi;
982 		}
983 		fpi->clk_per = clk;
984 	}
985 
986 	privsize = sizeof(*fep) +
987 	           sizeof(struct sk_buff **) *
988 		     (fpi->rx_ring + fpi->tx_ring) +
989 		   sizeof(char) * fpi->tx_ring;
990 
991 	ndev = alloc_etherdev(privsize);
992 	if (!ndev) {
993 		ret = -ENOMEM;
994 		goto out_put;
995 	}
996 
997 	SET_NETDEV_DEV(ndev, &ofdev->dev);
998 	platform_set_drvdata(ofdev, ndev);
999 
1000 	fep = netdev_priv(ndev);
1001 	fep->dev = &ofdev->dev;
1002 	fep->ndev = ndev;
1003 	fep->fpi = fpi;
1004 	fep->ops = match->data;
1005 
1006 	ret = fep->ops->setup_data(ndev);
1007 	if (ret)
1008 		goto out_free_dev;
1009 
1010 	fep->rx_skbuff = (struct sk_buff **)&fep[1];
1011 	fep->tx_skbuff = fep->rx_skbuff + fpi->rx_ring;
1012 	fep->mapped_as_page = (char *)(fep->rx_skbuff + fpi->rx_ring +
1013 				       fpi->tx_ring);
1014 
1015 	spin_lock_init(&fep->lock);
1016 	spin_lock_init(&fep->tx_lock);
1017 
1018 	mac_addr = of_get_mac_address(ofdev->dev.of_node);
1019 	if (mac_addr)
1020 		memcpy(ndev->dev_addr, mac_addr, ETH_ALEN);
1021 
1022 	ret = fep->ops->allocate_bd(ndev);
1023 	if (ret)
1024 		goto out_cleanup_data;
1025 
1026 	fep->rx_bd_base = fep->ring_base;
1027 	fep->tx_bd_base = fep->rx_bd_base + fpi->rx_ring;
1028 
1029 	fep->tx_ring = fpi->tx_ring;
1030 	fep->rx_ring = fpi->rx_ring;
1031 
1032 	ndev->netdev_ops = &fs_enet_netdev_ops;
1033 	ndev->watchdog_timeo = 2 * HZ;
1034 	netif_napi_add(ndev, &fep->napi, fs_enet_rx_napi, fpi->napi_weight);
1035 	netif_napi_add(ndev, &fep->napi_tx, fs_enet_tx_napi, 2);
1036 
1037 	ndev->ethtool_ops = &fs_ethtool_ops;
1038 
1039 	init_timer(&fep->phy_timer_list);
1040 
1041 	netif_carrier_off(ndev);
1042 
1043 	ndev->features |= NETIF_F_SG;
1044 
1045 	ret = register_netdev(ndev);
1046 	if (ret)
1047 		goto out_free_bd;
1048 
1049 	pr_info("%s: fs_enet: %pM\n", ndev->name, ndev->dev_addr);
1050 
1051 	return 0;
1052 
1053 out_free_bd:
1054 	fep->ops->free_bd(ndev);
1055 out_cleanup_data:
1056 	fep->ops->cleanup_data(ndev);
1057 out_free_dev:
1058 	free_netdev(ndev);
1059 out_put:
1060 	of_node_put(fpi->phy_node);
1061 	if (fpi->clk_per)
1062 		clk_disable_unprepare(fpi->clk_per);
1063 out_free_fpi:
1064 	kfree(fpi);
1065 	return ret;
1066 }
1067 
1068 static int fs_enet_remove(struct platform_device *ofdev)
1069 {
1070 	struct net_device *ndev = platform_get_drvdata(ofdev);
1071 	struct fs_enet_private *fep = netdev_priv(ndev);
1072 
1073 	unregister_netdev(ndev);
1074 
1075 	fep->ops->free_bd(ndev);
1076 	fep->ops->cleanup_data(ndev);
1077 	dev_set_drvdata(fep->dev, NULL);
1078 	of_node_put(fep->fpi->phy_node);
1079 	if (fep->fpi->clk_per)
1080 		clk_disable_unprepare(fep->fpi->clk_per);
1081 	free_netdev(ndev);
1082 	return 0;
1083 }
1084 
1085 static struct of_device_id fs_enet_match[] = {
1086 #ifdef CONFIG_FS_ENET_HAS_SCC
1087 	{
1088 		.compatible = "fsl,cpm1-scc-enet",
1089 		.data = (void *)&fs_scc_ops,
1090 	},
1091 	{
1092 		.compatible = "fsl,cpm2-scc-enet",
1093 		.data = (void *)&fs_scc_ops,
1094 	},
1095 #endif
1096 #ifdef CONFIG_FS_ENET_HAS_FCC
1097 	{
1098 		.compatible = "fsl,cpm2-fcc-enet",
1099 		.data = (void *)&fs_fcc_ops,
1100 	},
1101 #endif
1102 #ifdef CONFIG_FS_ENET_HAS_FEC
1103 #ifdef CONFIG_FS_ENET_MPC5121_FEC
1104 	{
1105 		.compatible = "fsl,mpc5121-fec",
1106 		.data = (void *)&fs_fec_ops,
1107 	},
1108 	{
1109 		.compatible = "fsl,mpc5125-fec",
1110 		.data = (void *)&fs_fec_ops,
1111 	},
1112 #else
1113 	{
1114 		.compatible = "fsl,pq1-fec-enet",
1115 		.data = (void *)&fs_fec_ops,
1116 	},
1117 #endif
1118 #endif
1119 	{}
1120 };
1121 MODULE_DEVICE_TABLE(of, fs_enet_match);
1122 
1123 static struct platform_driver fs_enet_driver = {
1124 	.driver = {
1125 		.name = "fs_enet",
1126 		.of_match_table = fs_enet_match,
1127 	},
1128 	.probe = fs_enet_probe,
1129 	.remove = fs_enet_remove,
1130 };
1131 
1132 #ifdef CONFIG_NET_POLL_CONTROLLER
1133 static void fs_enet_netpoll(struct net_device *dev)
1134 {
1135        disable_irq(dev->irq);
1136        fs_enet_interrupt(dev->irq, dev);
1137        enable_irq(dev->irq);
1138 }
1139 #endif
1140 
1141 module_platform_driver(fs_enet_driver);
1142