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