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 #include <linux/pgtable.h>
44 
45 #include <linux/vmalloc.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 
fs_set_multicast_list(struct net_device * dev)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 
skb_align(struct sk_buff * skb,int align)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 */
fs_enet_napi(struct napi_struct * napi,int budget)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
fs_enet_interrupt(int irq,void * dev_id)317 fs_enet_interrupt(int irq, void *dev_id)
318 {
319 	struct net_device *dev = dev_id;
320 	struct fs_enet_private *fep;
321 	u32 int_events;
322 	u32 int_clr_events;
323 	int nr, napi_ok;
324 	int handled;
325 
326 	fep = netdev_priv(dev);
327 
328 	nr = 0;
329 	while ((int_events = (*fep->ops->get_int_events)(dev)) != 0) {
330 		nr++;
331 
332 		int_clr_events = int_events;
333 		int_clr_events &= ~fep->ev_napi;
334 
335 		(*fep->ops->clear_int_events)(dev, int_clr_events);
336 
337 		if (int_events & fep->ev_err)
338 			(*fep->ops->ev_error)(dev, int_events);
339 
340 		if (int_events & fep->ev) {
341 			napi_ok = napi_schedule_prep(&fep->napi);
342 
343 			(*fep->ops->napi_disable)(dev);
344 			(*fep->ops->clear_int_events)(dev, fep->ev_napi);
345 
346 			/* NOTE: it is possible for FCCs in NAPI mode    */
347 			/* to submit a spurious interrupt while in poll  */
348 			if (napi_ok)
349 				__napi_schedule(&fep->napi);
350 		}
351 
352 	}
353 
354 	handled = nr > 0;
355 	return IRQ_RETVAL(handled);
356 }
357 
fs_init_bds(struct net_device * dev)358 void fs_init_bds(struct net_device *dev)
359 {
360 	struct fs_enet_private *fep = netdev_priv(dev);
361 	cbd_t __iomem *bdp;
362 	struct sk_buff *skb;
363 	int i;
364 
365 	fs_cleanup_bds(dev);
366 
367 	fep->dirty_tx = fep->cur_tx = fep->tx_bd_base;
368 	fep->tx_free = fep->tx_ring;
369 	fep->cur_rx = fep->rx_bd_base;
370 
371 	/*
372 	 * Initialize the receive buffer descriptors.
373 	 */
374 	for (i = 0, bdp = fep->rx_bd_base; i < fep->rx_ring; i++, bdp++) {
375 		skb = netdev_alloc_skb(dev, ENET_RX_FRSIZE);
376 		if (skb == NULL)
377 			break;
378 
379 		skb_align(skb, ENET_RX_ALIGN);
380 		fep->rx_skbuff[i] = skb;
381 		CBDW_BUFADDR(bdp,
382 			dma_map_single(fep->dev, skb->data,
383 				L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
384 				DMA_FROM_DEVICE));
385 		CBDW_DATLEN(bdp, 0);	/* zero */
386 		CBDW_SC(bdp, BD_ENET_RX_EMPTY |
387 			((i < fep->rx_ring - 1) ? 0 : BD_SC_WRAP));
388 	}
389 	/*
390 	 * if we failed, fillup remainder
391 	 */
392 	for (; i < fep->rx_ring; i++, bdp++) {
393 		fep->rx_skbuff[i] = NULL;
394 		CBDW_SC(bdp, (i < fep->rx_ring - 1) ? 0 : BD_SC_WRAP);
395 	}
396 
397 	/*
398 	 * ...and the same for transmit.
399 	 */
400 	for (i = 0, bdp = fep->tx_bd_base; i < fep->tx_ring; i++, bdp++) {
401 		fep->tx_skbuff[i] = NULL;
402 		CBDW_BUFADDR(bdp, 0);
403 		CBDW_DATLEN(bdp, 0);
404 		CBDW_SC(bdp, (i < fep->tx_ring - 1) ? 0 : BD_SC_WRAP);
405 	}
406 }
407 
fs_cleanup_bds(struct net_device * dev)408 void fs_cleanup_bds(struct net_device *dev)
409 {
410 	struct fs_enet_private *fep = netdev_priv(dev);
411 	struct sk_buff *skb;
412 	cbd_t __iomem *bdp;
413 	int i;
414 
415 	/*
416 	 * Reset SKB transmit buffers.
417 	 */
418 	for (i = 0, bdp = fep->tx_bd_base; i < fep->tx_ring; i++, bdp++) {
419 		if ((skb = fep->tx_skbuff[i]) == NULL)
420 			continue;
421 
422 		/* unmap */
423 		dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
424 				skb->len, DMA_TO_DEVICE);
425 
426 		fep->tx_skbuff[i] = NULL;
427 		dev_kfree_skb(skb);
428 	}
429 
430 	/*
431 	 * Reset SKB receive buffers
432 	 */
433 	for (i = 0, bdp = fep->rx_bd_base; i < fep->rx_ring; i++, bdp++) {
434 		if ((skb = fep->rx_skbuff[i]) == NULL)
435 			continue;
436 
437 		/* unmap */
438 		dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
439 			L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
440 			DMA_FROM_DEVICE);
441 
442 		fep->rx_skbuff[i] = NULL;
443 
444 		dev_kfree_skb(skb);
445 	}
446 }
447 
448 /**********************************************************************************/
449 
450 #ifdef CONFIG_FS_ENET_MPC5121_FEC
451 /*
452  * MPC5121 FEC requeries 4-byte alignment for TX data buffer!
453  */
tx_skb_align_workaround(struct net_device * dev,struct sk_buff * skb)454 static struct sk_buff *tx_skb_align_workaround(struct net_device *dev,
455 					       struct sk_buff *skb)
456 {
457 	struct sk_buff *new_skb;
458 
459 	if (skb_linearize(skb))
460 		return NULL;
461 
462 	/* Alloc new skb */
463 	new_skb = netdev_alloc_skb(dev, skb->len + 4);
464 	if (!new_skb)
465 		return NULL;
466 
467 	/* Make sure new skb is properly aligned */
468 	skb_align(new_skb, 4);
469 
470 	/* Copy data to new skb ... */
471 	skb_copy_from_linear_data(skb, new_skb->data, skb->len);
472 	skb_put(new_skb, skb->len);
473 
474 	/* ... and free an old one */
475 	dev_kfree_skb_any(skb);
476 
477 	return new_skb;
478 }
479 #endif
480 
481 static netdev_tx_t
fs_enet_start_xmit(struct sk_buff * skb,struct net_device * dev)482 fs_enet_start_xmit(struct sk_buff *skb, struct net_device *dev)
483 {
484 	struct fs_enet_private *fep = netdev_priv(dev);
485 	cbd_t __iomem *bdp;
486 	int curidx;
487 	u16 sc;
488 	int nr_frags;
489 	skb_frag_t *frag;
490 	int len;
491 #ifdef CONFIG_FS_ENET_MPC5121_FEC
492 	int is_aligned = 1;
493 	int i;
494 
495 	if (!IS_ALIGNED((unsigned long)skb->data, 4)) {
496 		is_aligned = 0;
497 	} else {
498 		nr_frags = skb_shinfo(skb)->nr_frags;
499 		frag = skb_shinfo(skb)->frags;
500 		for (i = 0; i < nr_frags; i++, frag++) {
501 			if (!IS_ALIGNED(skb_frag_off(frag), 4)) {
502 				is_aligned = 0;
503 				break;
504 			}
505 		}
506 	}
507 
508 	if (!is_aligned) {
509 		skb = tx_skb_align_workaround(dev, skb);
510 		if (!skb) {
511 			/*
512 			 * We have lost packet due to memory allocation error
513 			 * in tx_skb_align_workaround(). Hopefully original
514 			 * skb is still valid, so try transmit it later.
515 			 */
516 			return NETDEV_TX_BUSY;
517 		}
518 	}
519 #endif
520 
521 	spin_lock(&fep->tx_lock);
522 
523 	/*
524 	 * Fill in a Tx ring entry
525 	 */
526 	bdp = fep->cur_tx;
527 
528 	nr_frags = skb_shinfo(skb)->nr_frags;
529 	if (fep->tx_free <= nr_frags || (CBDR_SC(bdp) & BD_ENET_TX_READY)) {
530 		netif_stop_queue(dev);
531 		spin_unlock(&fep->tx_lock);
532 
533 		/*
534 		 * Ooops.  All transmit buffers are full.  Bail out.
535 		 * This should not happen, since the tx queue should be stopped.
536 		 */
537 		dev_warn(fep->dev, "tx queue full!.\n");
538 		return NETDEV_TX_BUSY;
539 	}
540 
541 	curidx = bdp - fep->tx_bd_base;
542 
543 	len = skb->len;
544 	dev->stats.tx_bytes += len;
545 	if (nr_frags)
546 		len -= skb->data_len;
547 	fep->tx_free -= nr_frags + 1;
548 	/*
549 	 * Push the data cache so the CPM does not get stale memory data.
550 	 */
551 	CBDW_BUFADDR(bdp, dma_map_single(fep->dev,
552 				skb->data, len, DMA_TO_DEVICE));
553 	CBDW_DATLEN(bdp, len);
554 
555 	fep->mapped_as_page[curidx] = 0;
556 	frag = skb_shinfo(skb)->frags;
557 	while (nr_frags) {
558 		CBDC_SC(bdp,
559 			BD_ENET_TX_STATS | BD_ENET_TX_INTR | BD_ENET_TX_LAST |
560 			BD_ENET_TX_TC);
561 		CBDS_SC(bdp, BD_ENET_TX_READY);
562 
563 		if ((CBDR_SC(bdp) & BD_ENET_TX_WRAP) == 0) {
564 			bdp++;
565 			curidx++;
566 		} else {
567 			bdp = fep->tx_bd_base;
568 			curidx = 0;
569 		}
570 
571 		len = skb_frag_size(frag);
572 		CBDW_BUFADDR(bdp, skb_frag_dma_map(fep->dev, frag, 0, len,
573 						   DMA_TO_DEVICE));
574 		CBDW_DATLEN(bdp, len);
575 
576 		fep->tx_skbuff[curidx] = NULL;
577 		fep->mapped_as_page[curidx] = 1;
578 
579 		frag++;
580 		nr_frags--;
581 	}
582 
583 	/* Trigger transmission start */
584 	sc = BD_ENET_TX_READY | BD_ENET_TX_INTR |
585 	     BD_ENET_TX_LAST | BD_ENET_TX_TC;
586 
587 	/* note that while FEC does not have this bit
588 	 * it marks it as available for software use
589 	 * yay for hw reuse :) */
590 	if (skb->len <= 60)
591 		sc |= BD_ENET_TX_PAD;
592 	CBDC_SC(bdp, BD_ENET_TX_STATS);
593 	CBDS_SC(bdp, sc);
594 
595 	/* Save skb pointer. */
596 	fep->tx_skbuff[curidx] = skb;
597 
598 	/* If this was the last BD in the ring, start at the beginning again. */
599 	if ((CBDR_SC(bdp) & BD_ENET_TX_WRAP) == 0)
600 		bdp++;
601 	else
602 		bdp = fep->tx_bd_base;
603 	fep->cur_tx = bdp;
604 
605 	if (fep->tx_free < MAX_SKB_FRAGS)
606 		netif_stop_queue(dev);
607 
608 	skb_tx_timestamp(skb);
609 
610 	(*fep->ops->tx_kickstart)(dev);
611 
612 	spin_unlock(&fep->tx_lock);
613 
614 	return NETDEV_TX_OK;
615 }
616 
fs_timeout_work(struct work_struct * work)617 static void fs_timeout_work(struct work_struct *work)
618 {
619 	struct fs_enet_private *fep = container_of(work, struct fs_enet_private,
620 						   timeout_work);
621 	struct net_device *dev = fep->ndev;
622 	unsigned long flags;
623 	int wake = 0;
624 
625 	dev->stats.tx_errors++;
626 
627 	spin_lock_irqsave(&fep->lock, flags);
628 
629 	if (dev->flags & IFF_UP) {
630 		phy_stop(dev->phydev);
631 		(*fep->ops->stop)(dev);
632 		(*fep->ops->restart)(dev);
633 	}
634 
635 	phy_start(dev->phydev);
636 	wake = fep->tx_free >= MAX_SKB_FRAGS &&
637 	       !(CBDR_SC(fep->cur_tx) & BD_ENET_TX_READY);
638 	spin_unlock_irqrestore(&fep->lock, flags);
639 
640 	if (wake)
641 		netif_wake_queue(dev);
642 }
643 
fs_timeout(struct net_device * dev,unsigned int txqueue)644 static void fs_timeout(struct net_device *dev, unsigned int txqueue)
645 {
646 	struct fs_enet_private *fep = netdev_priv(dev);
647 
648 	schedule_work(&fep->timeout_work);
649 }
650 
651 /*-----------------------------------------------------------------------------
652  *  generic link-change handler - should be sufficient for most cases
653  *-----------------------------------------------------------------------------*/
generic_adjust_link(struct net_device * dev)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 = dev->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 
fs_adjust_link(struct net_device * dev)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 
fs_init_phy(struct net_device * dev)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 	return 0;
727 }
728 
fs_enet_open(struct net_device * dev)729 static int fs_enet_open(struct net_device *dev)
730 {
731 	struct fs_enet_private *fep = netdev_priv(dev);
732 	int r;
733 	int err;
734 
735 	/* to initialize the fep->cur_rx,... */
736 	/* not doing this, will cause a crash in fs_enet_napi */
737 	fs_init_bds(fep->ndev);
738 
739 	napi_enable(&fep->napi);
740 
741 	/* Install our interrupt handler. */
742 	r = request_irq(fep->interrupt, fs_enet_interrupt, IRQF_SHARED,
743 			"fs_enet-mac", dev);
744 	if (r != 0) {
745 		dev_err(fep->dev, "Could not allocate FS_ENET IRQ!");
746 		napi_disable(&fep->napi);
747 		return -EINVAL;
748 	}
749 
750 	err = fs_init_phy(dev);
751 	if (err) {
752 		free_irq(fep->interrupt, dev);
753 		napi_disable(&fep->napi);
754 		return err;
755 	}
756 	phy_start(dev->phydev);
757 
758 	netif_start_queue(dev);
759 
760 	return 0;
761 }
762 
fs_enet_close(struct net_device * dev)763 static int fs_enet_close(struct net_device *dev)
764 {
765 	struct fs_enet_private *fep = netdev_priv(dev);
766 	unsigned long flags;
767 
768 	netif_stop_queue(dev);
769 	netif_carrier_off(dev);
770 	napi_disable(&fep->napi);
771 	cancel_work_sync(&fep->timeout_work);
772 	phy_stop(dev->phydev);
773 
774 	spin_lock_irqsave(&fep->lock, flags);
775 	spin_lock(&fep->tx_lock);
776 	(*fep->ops->stop)(dev);
777 	spin_unlock(&fep->tx_lock);
778 	spin_unlock_irqrestore(&fep->lock, flags);
779 
780 	/* release any irqs */
781 	phy_disconnect(dev->phydev);
782 	free_irq(fep->interrupt, dev);
783 
784 	return 0;
785 }
786 
787 /*************************************************************************/
788 
fs_get_drvinfo(struct net_device * dev,struct ethtool_drvinfo * info)789 static void fs_get_drvinfo(struct net_device *dev,
790 			    struct ethtool_drvinfo *info)
791 {
792 	strscpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
793 }
794 
fs_get_regs_len(struct net_device * dev)795 static int fs_get_regs_len(struct net_device *dev)
796 {
797 	struct fs_enet_private *fep = netdev_priv(dev);
798 
799 	return (*fep->ops->get_regs_len)(dev);
800 }
801 
fs_get_regs(struct net_device * dev,struct ethtool_regs * regs,void * p)802 static void fs_get_regs(struct net_device *dev, struct ethtool_regs *regs,
803 			 void *p)
804 {
805 	struct fs_enet_private *fep = netdev_priv(dev);
806 	unsigned long flags;
807 	int r, len;
808 
809 	len = regs->len;
810 
811 	spin_lock_irqsave(&fep->lock, flags);
812 	r = (*fep->ops->get_regs)(dev, p, &len);
813 	spin_unlock_irqrestore(&fep->lock, flags);
814 
815 	if (r == 0)
816 		regs->version = 0;
817 }
818 
fs_get_msglevel(struct net_device * dev)819 static u32 fs_get_msglevel(struct net_device *dev)
820 {
821 	struct fs_enet_private *fep = netdev_priv(dev);
822 	return fep->msg_enable;
823 }
824 
fs_set_msglevel(struct net_device * dev,u32 value)825 static void fs_set_msglevel(struct net_device *dev, u32 value)
826 {
827 	struct fs_enet_private *fep = netdev_priv(dev);
828 	fep->msg_enable = value;
829 }
830 
fs_get_tunable(struct net_device * dev,const struct ethtool_tunable * tuna,void * data)831 static int fs_get_tunable(struct net_device *dev,
832 			  const struct ethtool_tunable *tuna, void *data)
833 {
834 	struct fs_enet_private *fep = netdev_priv(dev);
835 	struct fs_platform_info *fpi = fep->fpi;
836 	int ret = 0;
837 
838 	switch (tuna->id) {
839 	case ETHTOOL_RX_COPYBREAK:
840 		*(u32 *)data = fpi->rx_copybreak;
841 		break;
842 	default:
843 		ret = -EINVAL;
844 		break;
845 	}
846 
847 	return ret;
848 }
849 
fs_set_tunable(struct net_device * dev,const struct ethtool_tunable * tuna,const void * data)850 static int fs_set_tunable(struct net_device *dev,
851 			  const struct ethtool_tunable *tuna, const void *data)
852 {
853 	struct fs_enet_private *fep = netdev_priv(dev);
854 	struct fs_platform_info *fpi = fep->fpi;
855 	int ret = 0;
856 
857 	switch (tuna->id) {
858 	case ETHTOOL_RX_COPYBREAK:
859 		fpi->rx_copybreak = *(u32 *)data;
860 		break;
861 	default:
862 		ret = -EINVAL;
863 		break;
864 	}
865 
866 	return ret;
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 	.nway_reset = phy_ethtool_nway_reset,
873 	.get_link = ethtool_op_get_link,
874 	.get_msglevel = fs_get_msglevel,
875 	.set_msglevel = fs_set_msglevel,
876 	.get_regs = fs_get_regs,
877 	.get_ts_info = ethtool_op_get_ts_info,
878 	.get_link_ksettings = phy_ethtool_get_link_ksettings,
879 	.set_link_ksettings = phy_ethtool_set_link_ksettings,
880 	.get_tunable = fs_get_tunable,
881 	.set_tunable = fs_set_tunable,
882 };
883 
884 /**************************************************************************************/
885 
886 #ifdef CONFIG_FS_ENET_HAS_FEC
887 #define IS_FEC(match) ((match)->data == &fs_fec_ops)
888 #else
889 #define IS_FEC(match) 0
890 #endif
891 
892 static const struct net_device_ops fs_enet_netdev_ops = {
893 	.ndo_open		= fs_enet_open,
894 	.ndo_stop		= fs_enet_close,
895 	.ndo_start_xmit		= fs_enet_start_xmit,
896 	.ndo_tx_timeout		= fs_timeout,
897 	.ndo_set_rx_mode	= fs_set_multicast_list,
898 	.ndo_eth_ioctl		= phy_do_ioctl_running,
899 	.ndo_validate_addr	= eth_validate_addr,
900 	.ndo_set_mac_address	= eth_mac_addr,
901 #ifdef CONFIG_NET_POLL_CONTROLLER
902 	.ndo_poll_controller	= fs_enet_netpoll,
903 #endif
904 };
905 
906 static const struct of_device_id fs_enet_match[];
fs_enet_probe(struct platform_device * ofdev)907 static int fs_enet_probe(struct platform_device *ofdev)
908 {
909 	const struct of_device_id *match;
910 	struct net_device *ndev;
911 	struct fs_enet_private *fep;
912 	struct fs_platform_info *fpi;
913 	const u32 *data;
914 	struct clk *clk;
915 	int err;
916 	const char *phy_connection_type;
917 	int privsize, len, ret = -ENODEV;
918 
919 	match = of_match_device(fs_enet_match, &ofdev->dev);
920 	if (!match)
921 		return -EINVAL;
922 
923 	fpi = kzalloc(sizeof(*fpi), GFP_KERNEL);
924 	if (!fpi)
925 		return -ENOMEM;
926 
927 	if (!IS_FEC(match)) {
928 		data = of_get_property(ofdev->dev.of_node, "fsl,cpm-command", &len);
929 		if (!data || len != 4)
930 			goto out_free_fpi;
931 
932 		fpi->cp_command = *data;
933 	}
934 
935 	fpi->rx_ring = RX_RING_SIZE;
936 	fpi->tx_ring = TX_RING_SIZE;
937 	fpi->rx_copybreak = 240;
938 	fpi->napi_weight = 17;
939 	fpi->phy_node = of_parse_phandle(ofdev->dev.of_node, "phy-handle", 0);
940 	if (!fpi->phy_node && of_phy_is_fixed_link(ofdev->dev.of_node)) {
941 		err = of_phy_register_fixed_link(ofdev->dev.of_node);
942 		if (err)
943 			goto out_free_fpi;
944 
945 		/* In the case of a fixed PHY, the DT node associated
946 		 * to the PHY is the Ethernet MAC DT node.
947 		 */
948 		fpi->phy_node = of_node_get(ofdev->dev.of_node);
949 	}
950 
951 	if (of_device_is_compatible(ofdev->dev.of_node, "fsl,mpc5125-fec")) {
952 		phy_connection_type = of_get_property(ofdev->dev.of_node,
953 						"phy-connection-type", NULL);
954 		if (phy_connection_type && !strcmp("rmii", phy_connection_type))
955 			fpi->use_rmii = 1;
956 	}
957 
958 	/* make clock lookup non-fatal (the driver is shared among platforms),
959 	 * but require enable to succeed when a clock was specified/found,
960 	 * keep a reference to the clock upon successful acquisition
961 	 */
962 	clk = devm_clk_get(&ofdev->dev, "per");
963 	if (!IS_ERR(clk)) {
964 		ret = clk_prepare_enable(clk);
965 		if (ret)
966 			goto out_deregister_fixed_link;
967 
968 		fpi->clk_per = clk;
969 	}
970 
971 	privsize = sizeof(*fep) +
972 	           sizeof(struct sk_buff **) *
973 		     (fpi->rx_ring + fpi->tx_ring) +
974 		   sizeof(char) * fpi->tx_ring;
975 
976 	ndev = alloc_etherdev(privsize);
977 	if (!ndev) {
978 		ret = -ENOMEM;
979 		goto out_put;
980 	}
981 
982 	SET_NETDEV_DEV(ndev, &ofdev->dev);
983 	platform_set_drvdata(ofdev, ndev);
984 
985 	fep = netdev_priv(ndev);
986 	fep->dev = &ofdev->dev;
987 	fep->ndev = ndev;
988 	fep->fpi = fpi;
989 	fep->ops = match->data;
990 
991 	ret = fep->ops->setup_data(ndev);
992 	if (ret)
993 		goto out_free_dev;
994 
995 	fep->rx_skbuff = (struct sk_buff **)&fep[1];
996 	fep->tx_skbuff = fep->rx_skbuff + fpi->rx_ring;
997 	fep->mapped_as_page = (char *)(fep->rx_skbuff + fpi->rx_ring +
998 				       fpi->tx_ring);
999 
1000 	spin_lock_init(&fep->lock);
1001 	spin_lock_init(&fep->tx_lock);
1002 
1003 	of_get_ethdev_address(ofdev->dev.of_node, ndev);
1004 
1005 	ret = fep->ops->allocate_bd(ndev);
1006 	if (ret)
1007 		goto out_cleanup_data;
1008 
1009 	fep->rx_bd_base = fep->ring_base;
1010 	fep->tx_bd_base = fep->rx_bd_base + fpi->rx_ring;
1011 
1012 	fep->tx_ring = fpi->tx_ring;
1013 	fep->rx_ring = fpi->rx_ring;
1014 
1015 	ndev->netdev_ops = &fs_enet_netdev_ops;
1016 	ndev->watchdog_timeo = 2 * HZ;
1017 	INIT_WORK(&fep->timeout_work, fs_timeout_work);
1018 	netif_napi_add_weight(ndev, &fep->napi, fs_enet_napi,
1019 			      fpi->napi_weight);
1020 
1021 	ndev->ethtool_ops = &fs_ethtool_ops;
1022 
1023 	netif_carrier_off(ndev);
1024 
1025 	ndev->features |= NETIF_F_SG;
1026 
1027 	ret = register_netdev(ndev);
1028 	if (ret)
1029 		goto out_free_bd;
1030 
1031 	pr_info("%s: fs_enet: %pM\n", ndev->name, ndev->dev_addr);
1032 
1033 	return 0;
1034 
1035 out_free_bd:
1036 	fep->ops->free_bd(ndev);
1037 out_cleanup_data:
1038 	fep->ops->cleanup_data(ndev);
1039 out_free_dev:
1040 	free_netdev(ndev);
1041 out_put:
1042 	clk_disable_unprepare(fpi->clk_per);
1043 out_deregister_fixed_link:
1044 	of_node_put(fpi->phy_node);
1045 	if (of_phy_is_fixed_link(ofdev->dev.of_node))
1046 		of_phy_deregister_fixed_link(ofdev->dev.of_node);
1047 out_free_fpi:
1048 	kfree(fpi);
1049 	return ret;
1050 }
1051 
fs_enet_remove(struct platform_device * ofdev)1052 static void fs_enet_remove(struct platform_device *ofdev)
1053 {
1054 	struct net_device *ndev = platform_get_drvdata(ofdev);
1055 	struct fs_enet_private *fep = netdev_priv(ndev);
1056 
1057 	unregister_netdev(ndev);
1058 
1059 	fep->ops->free_bd(ndev);
1060 	fep->ops->cleanup_data(ndev);
1061 	dev_set_drvdata(fep->dev, NULL);
1062 	of_node_put(fep->fpi->phy_node);
1063 	clk_disable_unprepare(fep->fpi->clk_per);
1064 	if (of_phy_is_fixed_link(ofdev->dev.of_node))
1065 		of_phy_deregister_fixed_link(ofdev->dev.of_node);
1066 	free_netdev(ndev);
1067 }
1068 
1069 static const struct of_device_id fs_enet_match[] = {
1070 #ifdef CONFIG_FS_ENET_HAS_SCC
1071 	{
1072 		.compatible = "fsl,cpm1-scc-enet",
1073 		.data = (void *)&fs_scc_ops,
1074 	},
1075 	{
1076 		.compatible = "fsl,cpm2-scc-enet",
1077 		.data = (void *)&fs_scc_ops,
1078 	},
1079 #endif
1080 #ifdef CONFIG_FS_ENET_HAS_FCC
1081 	{
1082 		.compatible = "fsl,cpm2-fcc-enet",
1083 		.data = (void *)&fs_fcc_ops,
1084 	},
1085 #endif
1086 #ifdef CONFIG_FS_ENET_HAS_FEC
1087 #ifdef CONFIG_FS_ENET_MPC5121_FEC
1088 	{
1089 		.compatible = "fsl,mpc5121-fec",
1090 		.data = (void *)&fs_fec_ops,
1091 	},
1092 	{
1093 		.compatible = "fsl,mpc5125-fec",
1094 		.data = (void *)&fs_fec_ops,
1095 	},
1096 #else
1097 	{
1098 		.compatible = "fsl,pq1-fec-enet",
1099 		.data = (void *)&fs_fec_ops,
1100 	},
1101 #endif
1102 #endif
1103 	{}
1104 };
1105 MODULE_DEVICE_TABLE(of, fs_enet_match);
1106 
1107 static struct platform_driver fs_enet_driver = {
1108 	.driver = {
1109 		.name = "fs_enet",
1110 		.of_match_table = fs_enet_match,
1111 	},
1112 	.probe = fs_enet_probe,
1113 	.remove_new = fs_enet_remove,
1114 };
1115 
1116 #ifdef CONFIG_NET_POLL_CONTROLLER
fs_enet_netpoll(struct net_device * dev)1117 static void fs_enet_netpoll(struct net_device *dev)
1118 {
1119        disable_irq(dev->irq);
1120        fs_enet_interrupt(dev->irq, dev);
1121        enable_irq(dev->irq);
1122 }
1123 #endif
1124 
1125 module_platform_driver(fs_enet_driver);
1126