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/init.h>
28#include <linux/delay.h>
29#include <linux/netdevice.h>
30#include <linux/etherdevice.h>
31#include <linux/skbuff.h>
32#include <linux/spinlock.h>
33#include <linux/mii.h>
34#include <linux/ethtool.h>
35#include <linux/bitops.h>
36#include <linux/fs.h>
37#include <linux/platform_device.h>
38#include <linux/phy.h>
39
40#include <linux/vmalloc.h>
41#include <asm/pgtable.h>
42
43#include <asm/pgtable.h>
44#include <asm/irq.h>
45#include <asm/uaccess.h>
46
47#include "fs_enet.h"
48
49/*************************************************/
50
51static char version[] __devinitdata =
52    DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")" "\n";
53
54MODULE_AUTHOR("Pantelis Antoniou <panto@intracom.gr>");
55MODULE_DESCRIPTION("Freescale Ethernet Driver");
56MODULE_LICENSE("GPL");
57MODULE_VERSION(DRV_MODULE_VERSION);
58
59int fs_enet_debug = -1;		/* -1 == use FS_ENET_DEF_MSG_ENABLE as value */
60module_param(fs_enet_debug, int, 0);
61MODULE_PARM_DESC(fs_enet_debug,
62		 "Freescale bitmapped debugging message enable value");
63
64
65static void fs_set_multicast_list(struct net_device *dev)
66{
67	struct fs_enet_private *fep = netdev_priv(dev);
68
69	(*fep->ops->set_multicast_list)(dev);
70}
71
72/* NAPI receive function */
73static int fs_enet_rx_napi(struct net_device *dev, int *budget)
74{
75	struct fs_enet_private *fep = netdev_priv(dev);
76	const struct fs_platform_info *fpi = fep->fpi;
77	cbd_t *bdp;
78	struct sk_buff *skb, *skbn, *skbt;
79	int received = 0;
80	u16 pkt_len, sc;
81	int curidx;
82	int rx_work_limit = 0;	/* pacify gcc */
83
84	rx_work_limit = min(dev->quota, *budget);
85
86	if (!netif_running(dev))
87		return 0;
88
89	/*
90	 * First, grab all of the stats for the incoming packet.
91	 * These get messed up if we get called due to a busy condition.
92	 */
93	bdp = fep->cur_rx;
94
95	/* clear RX status bits for napi*/
96	(*fep->ops->napi_clear_rx_event)(dev);
97
98	while (((sc = CBDR_SC(bdp)) & BD_ENET_RX_EMPTY) == 0) {
99
100		curidx = bdp - fep->rx_bd_base;
101
102		/*
103		 * Since we have allocated space to hold a complete frame,
104		 * the last indicator should be set.
105		 */
106		if ((sc & BD_ENET_RX_LAST) == 0)
107			printk(KERN_WARNING DRV_MODULE_NAME
108			       ": %s rcv is not +last\n",
109			       dev->name);
110
111		/*
112		 * Check for errors.
113		 */
114		if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_CL |
115			  BD_ENET_RX_NO | BD_ENET_RX_CR | BD_ENET_RX_OV)) {
116			fep->stats.rx_errors++;
117			/* Frame too long or too short. */
118			if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH))
119				fep->stats.rx_length_errors++;
120			/* Frame alignment */
121			if (sc & (BD_ENET_RX_NO | BD_ENET_RX_CL))
122				fep->stats.rx_frame_errors++;
123			/* CRC Error */
124			if (sc & BD_ENET_RX_CR)
125				fep->stats.rx_crc_errors++;
126			/* FIFO overrun */
127			if (sc & BD_ENET_RX_OV)
128				fep->stats.rx_crc_errors++;
129
130			skb = fep->rx_skbuff[curidx];
131
132			dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
133				L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
134				DMA_FROM_DEVICE);
135
136			skbn = skb;
137
138		} else {
139
140			/* napi, got packet but no quota */
141			if (--rx_work_limit < 0)
142				break;
143
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 = dev_alloc_skb(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 = dev_alloc_skb(ENET_RX_FRSIZE);
171
172			if (skbn != NULL) {
173				skb_put(skb, pkt_len);	/* Make room */
174				skb->protocol = eth_type_trans(skb, dev);
175				received++;
176				netif_receive_skb(skb);
177			} else {
178				printk(KERN_WARNING DRV_MODULE_NAME
179				       ": %s Memory squeeze, dropping packet.\n",
180				       dev->name);
181				fep->stats.rx_dropped++;
182				skbn = skb;
183			}
184		}
185
186		fep->rx_skbuff[curidx] = skbn;
187		CBDW_BUFADDR(bdp, dma_map_single(fep->dev, skbn->data,
188			     L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
189			     DMA_FROM_DEVICE));
190		CBDW_DATLEN(bdp, 0);
191		CBDW_SC(bdp, (sc & ~BD_ENET_RX_STATS) | BD_ENET_RX_EMPTY);
192
193		/*
194		 * Update BD pointer to next entry.
195		 */
196		if ((sc & BD_ENET_RX_WRAP) == 0)
197			bdp++;
198		else
199			bdp = fep->rx_bd_base;
200
201		(*fep->ops->rx_bd_done)(dev);
202	}
203
204	fep->cur_rx = bdp;
205
206	dev->quota -= received;
207	*budget -= received;
208
209	if (rx_work_limit < 0)
210		return 1;	/* not done */
211
212	/* done */
213	netif_rx_complete(dev);
214
215	(*fep->ops->napi_enable_rx)(dev);
216
217	return 0;
218}
219
220/* non NAPI receive function */
221static int fs_enet_rx_non_napi(struct net_device *dev)
222{
223	struct fs_enet_private *fep = netdev_priv(dev);
224	const struct fs_platform_info *fpi = fep->fpi;
225	cbd_t *bdp;
226	struct sk_buff *skb, *skbn, *skbt;
227	int received = 0;
228	u16 pkt_len, sc;
229	int curidx;
230	/*
231	 * First, grab all of the stats for the incoming packet.
232	 * These get messed up if we get called due to a busy condition.
233	 */
234	bdp = fep->cur_rx;
235
236	while (((sc = CBDR_SC(bdp)) & BD_ENET_RX_EMPTY) == 0) {
237
238		curidx = bdp - fep->rx_bd_base;
239
240		/*
241		 * Since we have allocated space to hold a complete frame,
242		 * the last indicator should be set.
243		 */
244		if ((sc & BD_ENET_RX_LAST) == 0)
245			printk(KERN_WARNING DRV_MODULE_NAME
246			       ": %s rcv is not +last\n",
247			       dev->name);
248
249		/*
250		 * Check for errors.
251		 */
252		if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_CL |
253			  BD_ENET_RX_NO | BD_ENET_RX_CR | BD_ENET_RX_OV)) {
254			fep->stats.rx_errors++;
255			/* Frame too long or too short. */
256			if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH))
257				fep->stats.rx_length_errors++;
258			/* Frame alignment */
259			if (sc & (BD_ENET_RX_NO | BD_ENET_RX_CL))
260				fep->stats.rx_frame_errors++;
261			/* CRC Error */
262			if (sc & BD_ENET_RX_CR)
263				fep->stats.rx_crc_errors++;
264			/* FIFO overrun */
265			if (sc & BD_ENET_RX_OV)
266				fep->stats.rx_crc_errors++;
267
268			skb = fep->rx_skbuff[curidx];
269
270			dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
271				L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
272				DMA_FROM_DEVICE);
273
274			skbn = skb;
275
276		} else {
277
278			skb = fep->rx_skbuff[curidx];
279
280			dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
281				L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
282				DMA_FROM_DEVICE);
283
284			/*
285			 * Process the incoming frame.
286			 */
287			fep->stats.rx_packets++;
288			pkt_len = CBDR_DATLEN(bdp) - 4;	/* remove CRC */
289			fep->stats.rx_bytes += pkt_len + 4;
290
291			if (pkt_len <= fpi->rx_copybreak) {
292				/* +2 to make IP header L1 cache aligned */
293				skbn = dev_alloc_skb(pkt_len + 2);
294				if (skbn != NULL) {
295					skb_reserve(skbn, 2);	/* align IP header */
296					skb_copy_from_linear_data(skb,
297						      skbn->data, pkt_len);
298					/* swap */
299					skbt = skb;
300					skb = skbn;
301					skbn = skbt;
302				}
303			} else
304				skbn = dev_alloc_skb(ENET_RX_FRSIZE);
305
306			if (skbn != NULL) {
307				skb_put(skb, pkt_len);	/* Make room */
308				skb->protocol = eth_type_trans(skb, dev);
309				received++;
310				netif_rx(skb);
311			} else {
312				printk(KERN_WARNING DRV_MODULE_NAME
313				       ": %s Memory squeeze, dropping packet.\n",
314				       dev->name);
315				fep->stats.rx_dropped++;
316				skbn = skb;
317			}
318		}
319
320		fep->rx_skbuff[curidx] = skbn;
321		CBDW_BUFADDR(bdp, dma_map_single(fep->dev, skbn->data,
322			     L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
323			     DMA_FROM_DEVICE));
324		CBDW_DATLEN(bdp, 0);
325		CBDW_SC(bdp, (sc & ~BD_ENET_RX_STATS) | BD_ENET_RX_EMPTY);
326
327		/*
328		 * Update BD pointer to next entry.
329		 */
330		if ((sc & BD_ENET_RX_WRAP) == 0)
331			bdp++;
332		else
333			bdp = fep->rx_bd_base;
334
335		(*fep->ops->rx_bd_done)(dev);
336	}
337
338	fep->cur_rx = bdp;
339
340	return 0;
341}
342
343static void fs_enet_tx(struct net_device *dev)
344{
345	struct fs_enet_private *fep = netdev_priv(dev);
346	cbd_t *bdp;
347	struct sk_buff *skb;
348	int dirtyidx, do_wake, do_restart;
349	u16 sc;
350
351	spin_lock(&fep->lock);
352	bdp = fep->dirty_tx;
353
354	do_wake = do_restart = 0;
355	while (((sc = CBDR_SC(bdp)) & BD_ENET_TX_READY) == 0) {
356
357		dirtyidx = bdp - fep->tx_bd_base;
358
359		if (fep->tx_free == fep->tx_ring)
360			break;
361
362		skb = fep->tx_skbuff[dirtyidx];
363
364		/*
365		 * Check for errors.
366		 */
367		if (sc & (BD_ENET_TX_HB | BD_ENET_TX_LC |
368			  BD_ENET_TX_RL | BD_ENET_TX_UN | BD_ENET_TX_CSL)) {
369
370			if (sc & BD_ENET_TX_HB)	/* No heartbeat */
371				fep->stats.tx_heartbeat_errors++;
372			if (sc & BD_ENET_TX_LC)	/* Late collision */
373				fep->stats.tx_window_errors++;
374			if (sc & BD_ENET_TX_RL)	/* Retrans limit */
375				fep->stats.tx_aborted_errors++;
376			if (sc & BD_ENET_TX_UN)	/* Underrun */
377				fep->stats.tx_fifo_errors++;
378			if (sc & BD_ENET_TX_CSL)	/* Carrier lost */
379				fep->stats.tx_carrier_errors++;
380
381			if (sc & (BD_ENET_TX_LC | BD_ENET_TX_RL | BD_ENET_TX_UN)) {
382				fep->stats.tx_errors++;
383				do_restart = 1;
384			}
385		} else
386			fep->stats.tx_packets++;
387
388		if (sc & BD_ENET_TX_READY)
389			printk(KERN_WARNING DRV_MODULE_NAME
390			       ": %s HEY! Enet xmit interrupt and TX_READY.\n",
391			       dev->name);
392
393		/*
394		 * Deferred means some collisions occurred during transmit,
395		 * but we eventually sent the packet OK.
396		 */
397		if (sc & BD_ENET_TX_DEF)
398			fep->stats.collisions++;
399
400		/* unmap */
401		dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
402				skb->len, DMA_TO_DEVICE);
403
404		/*
405		 * Free the sk buffer associated with this last transmit.
406		 */
407		dev_kfree_skb_irq(skb);
408		fep->tx_skbuff[dirtyidx] = NULL;
409
410		/*
411		 * Update pointer to next buffer descriptor to be transmitted.
412		 */
413		if ((sc & BD_ENET_TX_WRAP) == 0)
414			bdp++;
415		else
416			bdp = fep->tx_bd_base;
417
418		/*
419		 * Since we have freed up a buffer, the ring is no longer
420		 * full.
421		 */
422		if (!fep->tx_free++)
423			do_wake = 1;
424	}
425
426	fep->dirty_tx = bdp;
427
428	if (do_restart)
429		(*fep->ops->tx_restart)(dev);
430
431	spin_unlock(&fep->lock);
432
433	if (do_wake)
434		netif_wake_queue(dev);
435}
436
437/*
438 * The interrupt handler.
439 * This is called from the MPC core interrupt.
440 */
441static irqreturn_t
442fs_enet_interrupt(int irq, void *dev_id)
443{
444	struct net_device *dev = dev_id;
445	struct fs_enet_private *fep;
446	const struct fs_platform_info *fpi;
447	u32 int_events;
448	u32 int_clr_events;
449	int nr, napi_ok;
450	int handled;
451
452	fep = netdev_priv(dev);
453	fpi = fep->fpi;
454
455	nr = 0;
456	while ((int_events = (*fep->ops->get_int_events)(dev)) != 0) {
457
458		nr++;
459
460		int_clr_events = int_events;
461		if (fpi->use_napi)
462			int_clr_events &= ~fep->ev_napi_rx;
463
464		(*fep->ops->clear_int_events)(dev, int_clr_events);
465
466		if (int_events & fep->ev_err)
467			(*fep->ops->ev_error)(dev, int_events);
468
469		if (int_events & fep->ev_rx) {
470			if (!fpi->use_napi)
471				fs_enet_rx_non_napi(dev);
472			else {
473				napi_ok = netif_rx_schedule_prep(dev);
474
475				(*fep->ops->napi_disable_rx)(dev);
476				(*fep->ops->clear_int_events)(dev, fep->ev_napi_rx);
477
478				/* NOTE: it is possible for FCCs in NAPI mode    */
479				/* to submit a spurious interrupt while in poll  */
480				if (napi_ok)
481					__netif_rx_schedule(dev);
482			}
483		}
484
485		if (int_events & fep->ev_tx)
486			fs_enet_tx(dev);
487	}
488
489	handled = nr > 0;
490	return IRQ_RETVAL(handled);
491}
492
493void fs_init_bds(struct net_device *dev)
494{
495	struct fs_enet_private *fep = netdev_priv(dev);
496	cbd_t *bdp;
497	struct sk_buff *skb;
498	int i;
499
500	fs_cleanup_bds(dev);
501
502	fep->dirty_tx = fep->cur_tx = fep->tx_bd_base;
503	fep->tx_free = fep->tx_ring;
504	fep->cur_rx = fep->rx_bd_base;
505
506	/*
507	 * Initialize the receive buffer descriptors.
508	 */
509	for (i = 0, bdp = fep->rx_bd_base; i < fep->rx_ring; i++, bdp++) {
510		skb = dev_alloc_skb(ENET_RX_FRSIZE);
511		if (skb == NULL) {
512			printk(KERN_WARNING DRV_MODULE_NAME
513			       ": %s Memory squeeze, unable to allocate skb\n",
514			       dev->name);
515			break;
516		}
517		fep->rx_skbuff[i] = skb;
518		CBDW_BUFADDR(bdp,
519			dma_map_single(fep->dev, skb->data,
520				L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
521				DMA_FROM_DEVICE));
522		CBDW_DATLEN(bdp, 0);	/* zero */
523		CBDW_SC(bdp, BD_ENET_RX_EMPTY |
524			((i < fep->rx_ring - 1) ? 0 : BD_SC_WRAP));
525	}
526	/*
527	 * if we failed, fillup remainder
528	 */
529	for (; i < fep->rx_ring; i++, bdp++) {
530		fep->rx_skbuff[i] = NULL;
531		CBDW_SC(bdp, (i < fep->rx_ring - 1) ? 0 : BD_SC_WRAP);
532	}
533
534	/*
535	 * ...and the same for transmit.
536	 */
537	for (i = 0, bdp = fep->tx_bd_base; i < fep->tx_ring; i++, bdp++) {
538		fep->tx_skbuff[i] = NULL;
539		CBDW_BUFADDR(bdp, 0);
540		CBDW_DATLEN(bdp, 0);
541		CBDW_SC(bdp, (i < fep->tx_ring - 1) ? 0 : BD_SC_WRAP);
542	}
543}
544
545void fs_cleanup_bds(struct net_device *dev)
546{
547	struct fs_enet_private *fep = netdev_priv(dev);
548	struct sk_buff *skb;
549	cbd_t *bdp;
550	int i;
551
552	/*
553	 * Reset SKB transmit buffers.
554	 */
555	for (i = 0, bdp = fep->tx_bd_base; i < fep->tx_ring; i++, bdp++) {
556		if ((skb = fep->tx_skbuff[i]) == NULL)
557			continue;
558
559		/* unmap */
560		dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
561				skb->len, DMA_TO_DEVICE);
562
563		fep->tx_skbuff[i] = NULL;
564		dev_kfree_skb(skb);
565	}
566
567	/*
568	 * Reset SKB receive buffers
569	 */
570	for (i = 0, bdp = fep->rx_bd_base; i < fep->rx_ring; i++, bdp++) {
571		if ((skb = fep->rx_skbuff[i]) == NULL)
572			continue;
573
574		/* unmap */
575		dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
576			L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
577			DMA_FROM_DEVICE);
578
579		fep->rx_skbuff[i] = NULL;
580
581		dev_kfree_skb(skb);
582	}
583}
584
585/**********************************************************************************/
586
587static int fs_enet_start_xmit(struct sk_buff *skb, struct net_device *dev)
588{
589	struct fs_enet_private *fep = netdev_priv(dev);
590	cbd_t *bdp;
591	int curidx;
592	u16 sc;
593	unsigned long flags;
594
595	spin_lock_irqsave(&fep->tx_lock, flags);
596
597	/*
598	 * Fill in a Tx ring entry
599	 */
600	bdp = fep->cur_tx;
601
602	if (!fep->tx_free || (CBDR_SC(bdp) & BD_ENET_TX_READY)) {
603		netif_stop_queue(dev);
604		spin_unlock_irqrestore(&fep->tx_lock, flags);
605
606		/*
607		 * Ooops.  All transmit buffers are full.  Bail out.
608		 * This should not happen, since the tx queue should be stopped.
609		 */
610		printk(KERN_WARNING DRV_MODULE_NAME
611		       ": %s tx queue full!.\n", dev->name);
612		return NETDEV_TX_BUSY;
613	}
614
615	curidx = bdp - fep->tx_bd_base;
616	/*
617	 * Clear all of the status flags.
618	 */
619	CBDC_SC(bdp, BD_ENET_TX_STATS);
620
621	/*
622	 * Save skb pointer.
623	 */
624	fep->tx_skbuff[curidx] = skb;
625
626	fep->stats.tx_bytes += skb->len;
627
628	/*
629	 * Push the data cache so the CPM does not get stale memory data.
630	 */
631	CBDW_BUFADDR(bdp, dma_map_single(fep->dev,
632				skb->data, skb->len, DMA_TO_DEVICE));
633	CBDW_DATLEN(bdp, skb->len);
634
635	dev->trans_start = jiffies;
636
637	/*
638	 * If this was the last BD in the ring, start at the beginning again.
639	 */
640	if ((CBDR_SC(bdp) & BD_ENET_TX_WRAP) == 0)
641		fep->cur_tx++;
642	else
643		fep->cur_tx = fep->tx_bd_base;
644
645	if (!--fep->tx_free)
646		netif_stop_queue(dev);
647
648	/* Trigger transmission start */
649	sc = BD_ENET_TX_READY | BD_ENET_TX_INTR |
650	     BD_ENET_TX_LAST | BD_ENET_TX_TC;
651
652	/* note that while FEC does not have this bit
653	 * it marks it as available for software use
654	 * yay for hw reuse :) */
655	if (skb->len <= 60)
656		sc |= BD_ENET_TX_PAD;
657	CBDS_SC(bdp, sc);
658
659	(*fep->ops->tx_kickstart)(dev);
660
661	spin_unlock_irqrestore(&fep->tx_lock, flags);
662
663	return NETDEV_TX_OK;
664}
665
666static int fs_request_irq(struct net_device *dev, int irq, const char *name,
667		irq_handler_t irqf)
668{
669	struct fs_enet_private *fep = netdev_priv(dev);
670
671	(*fep->ops->pre_request_irq)(dev, irq);
672	return request_irq(irq, irqf, IRQF_SHARED, name, dev);
673}
674
675static void fs_free_irq(struct net_device *dev, int irq)
676{
677	struct fs_enet_private *fep = netdev_priv(dev);
678
679	free_irq(irq, dev);
680	(*fep->ops->post_free_irq)(dev, irq);
681}
682
683static void fs_timeout(struct net_device *dev)
684{
685	struct fs_enet_private *fep = netdev_priv(dev);
686	unsigned long flags;
687	int wake = 0;
688
689	fep->stats.tx_errors++;
690
691	spin_lock_irqsave(&fep->lock, flags);
692
693	if (dev->flags & IFF_UP) {
694		phy_stop(fep->phydev);
695		(*fep->ops->stop)(dev);
696		(*fep->ops->restart)(dev);
697		phy_start(fep->phydev);
698	}
699
700	phy_start(fep->phydev);
701	wake = fep->tx_free && !(CBDR_SC(fep->cur_tx) & BD_ENET_TX_READY);
702	spin_unlock_irqrestore(&fep->lock, flags);
703
704	if (wake)
705		netif_wake_queue(dev);
706}
707
708/*-----------------------------------------------------------------------------
709 *  generic link-change handler - should be sufficient for most cases
710 *-----------------------------------------------------------------------------*/
711static void generic_adjust_link(struct  net_device *dev)
712{
713       struct fs_enet_private *fep = netdev_priv(dev);
714       struct phy_device *phydev = fep->phydev;
715       int new_state = 0;
716
717       if (phydev->link) {
718
719               /* adjust to duplex mode */
720               if (phydev->duplex != fep->oldduplex){
721                       new_state = 1;
722                       fep->oldduplex = phydev->duplex;
723               }
724
725               if (phydev->speed != fep->oldspeed) {
726                       new_state = 1;
727                       fep->oldspeed = phydev->speed;
728               }
729
730               if (!fep->oldlink) {
731                       new_state = 1;
732                       fep->oldlink = 1;
733                       netif_schedule(dev);
734                       netif_carrier_on(dev);
735                       netif_start_queue(dev);
736               }
737
738               if (new_state)
739                       fep->ops->restart(dev);
740
741       } else if (fep->oldlink) {
742               new_state = 1;
743               fep->oldlink = 0;
744               fep->oldspeed = 0;
745               fep->oldduplex = -1;
746               netif_carrier_off(dev);
747               netif_stop_queue(dev);
748       }
749
750       if (new_state && netif_msg_link(fep))
751               phy_print_status(phydev);
752}
753
754
755static void fs_adjust_link(struct net_device *dev)
756{
757	struct fs_enet_private *fep = netdev_priv(dev);
758	unsigned long flags;
759
760	spin_lock_irqsave(&fep->lock, flags);
761
762	if(fep->ops->adjust_link)
763		fep->ops->adjust_link(dev);
764	else
765		generic_adjust_link(dev);
766
767	spin_unlock_irqrestore(&fep->lock, flags);
768}
769
770static int fs_init_phy(struct net_device *dev)
771{
772	struct fs_enet_private *fep = netdev_priv(dev);
773	struct phy_device *phydev;
774
775	fep->oldlink = 0;
776	fep->oldspeed = 0;
777	fep->oldduplex = -1;
778	if(fep->fpi->bus_id)
779		phydev = phy_connect(dev, fep->fpi->bus_id, &fs_adjust_link, 0,
780				PHY_INTERFACE_MODE_MII);
781	else {
782		printk("No phy bus ID specified in BSP code\n");
783		return -EINVAL;
784	}
785	if (IS_ERR(phydev)) {
786		printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
787		return PTR_ERR(phydev);
788	}
789
790	fep->phydev = phydev;
791
792	return 0;
793}
794
795
796static int fs_enet_open(struct net_device *dev)
797{
798	struct fs_enet_private *fep = netdev_priv(dev);
799	int r;
800	int err;
801
802	/* Install our interrupt handler. */
803	r = fs_request_irq(dev, fep->interrupt, "fs_enet-mac", fs_enet_interrupt);
804	if (r != 0) {
805		printk(KERN_ERR DRV_MODULE_NAME
806		       ": %s Could not allocate FS_ENET IRQ!", dev->name);
807		return -EINVAL;
808	}
809
810	err = fs_init_phy(dev);
811	if(err)
812		return err;
813
814	phy_start(fep->phydev);
815
816	return 0;
817}
818
819static int fs_enet_close(struct net_device *dev)
820{
821	struct fs_enet_private *fep = netdev_priv(dev);
822	unsigned long flags;
823
824	netif_stop_queue(dev);
825	netif_carrier_off(dev);
826	phy_stop(fep->phydev);
827
828	spin_lock_irqsave(&fep->lock, flags);
829	(*fep->ops->stop)(dev);
830	spin_unlock_irqrestore(&fep->lock, flags);
831
832	/* release any irqs */
833	phy_disconnect(fep->phydev);
834	fep->phydev = NULL;
835	fs_free_irq(dev, fep->interrupt);
836
837	return 0;
838}
839
840static struct net_device_stats *fs_enet_get_stats(struct net_device *dev)
841{
842	struct fs_enet_private *fep = netdev_priv(dev);
843	return &fep->stats;
844}
845
846/*************************************************************************/
847
848static void fs_get_drvinfo(struct net_device *dev,
849			    struct ethtool_drvinfo *info)
850{
851	strcpy(info->driver, DRV_MODULE_NAME);
852	strcpy(info->version, DRV_MODULE_VERSION);
853}
854
855static int fs_get_regs_len(struct net_device *dev)
856{
857	struct fs_enet_private *fep = netdev_priv(dev);
858
859	return (*fep->ops->get_regs_len)(dev);
860}
861
862static void fs_get_regs(struct net_device *dev, struct ethtool_regs *regs,
863			 void *p)
864{
865	struct fs_enet_private *fep = netdev_priv(dev);
866	unsigned long flags;
867	int r, len;
868
869	len = regs->len;
870
871	spin_lock_irqsave(&fep->lock, flags);
872	r = (*fep->ops->get_regs)(dev, p, &len);
873	spin_unlock_irqrestore(&fep->lock, flags);
874
875	if (r == 0)
876		regs->version = 0;
877}
878
879static int fs_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
880{
881	struct fs_enet_private *fep = netdev_priv(dev);
882	return phy_ethtool_gset(fep->phydev, cmd);
883}
884
885static int fs_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
886{
887	struct fs_enet_private *fep = netdev_priv(dev);
888	phy_ethtool_sset(fep->phydev, cmd);
889	return 0;
890}
891
892static int fs_nway_reset(struct net_device *dev)
893{
894	return 0;
895}
896
897static u32 fs_get_msglevel(struct net_device *dev)
898{
899	struct fs_enet_private *fep = netdev_priv(dev);
900	return fep->msg_enable;
901}
902
903static void fs_set_msglevel(struct net_device *dev, u32 value)
904{
905	struct fs_enet_private *fep = netdev_priv(dev);
906	fep->msg_enable = value;
907}
908
909static const struct ethtool_ops fs_ethtool_ops = {
910	.get_drvinfo = fs_get_drvinfo,
911	.get_regs_len = fs_get_regs_len,
912	.get_settings = fs_get_settings,
913	.set_settings = fs_set_settings,
914	.nway_reset = fs_nway_reset,
915	.get_link = ethtool_op_get_link,
916	.get_msglevel = fs_get_msglevel,
917	.set_msglevel = fs_set_msglevel,
918	.get_tx_csum = ethtool_op_get_tx_csum,
919	.set_tx_csum = ethtool_op_set_tx_csum,	/* local! */
920	.get_sg = ethtool_op_get_sg,
921	.set_sg = ethtool_op_set_sg,
922	.get_regs = fs_get_regs,
923};
924
925static int fs_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
926{
927	struct fs_enet_private *fep = netdev_priv(dev);
928	struct mii_ioctl_data *mii = (struct mii_ioctl_data *)&rq->ifr_data;
929	unsigned long flags;
930	int rc;
931
932	if (!netif_running(dev))
933		return -EINVAL;
934
935	spin_lock_irqsave(&fep->lock, flags);
936	rc = phy_mii_ioctl(fep->phydev, mii, cmd);
937	spin_unlock_irqrestore(&fep->lock, flags);
938	return rc;
939}
940
941extern int fs_mii_connect(struct net_device *dev);
942extern void fs_mii_disconnect(struct net_device *dev);
943
944static struct net_device *fs_init_instance(struct device *dev,
945		struct fs_platform_info *fpi)
946{
947	struct net_device *ndev = NULL;
948	struct fs_enet_private *fep = NULL;
949	int privsize, i, r, err = 0, registered = 0;
950
951	fpi->fs_no = fs_get_id(fpi);
952	/* guard */
953	if ((unsigned int)fpi->fs_no >= FS_MAX_INDEX)
954		return ERR_PTR(-EINVAL);
955
956	privsize = sizeof(*fep) + (sizeof(struct sk_buff **) *
957			    (fpi->rx_ring + fpi->tx_ring));
958
959	ndev = alloc_etherdev(privsize);
960	if (!ndev) {
961		err = -ENOMEM;
962		goto err;
963	}
964	SET_MODULE_OWNER(ndev);
965
966	fep = netdev_priv(ndev);
967	memset(fep, 0, privsize);	/* clear everything */
968
969	fep->dev = dev;
970	dev_set_drvdata(dev, ndev);
971	fep->fpi = fpi;
972	if (fpi->init_ioports)
973		fpi->init_ioports((struct fs_platform_info *)fpi);
974
975#ifdef CONFIG_FS_ENET_HAS_FEC
976	if (fs_get_fec_index(fpi->fs_no) >= 0)
977		fep->ops = &fs_fec_ops;
978#endif
979
980#ifdef CONFIG_FS_ENET_HAS_SCC
981	if (fs_get_scc_index(fpi->fs_no) >=0 )
982		fep->ops = &fs_scc_ops;
983#endif
984
985#ifdef CONFIG_FS_ENET_HAS_FCC
986	if (fs_get_fcc_index(fpi->fs_no) >= 0)
987		fep->ops = &fs_fcc_ops;
988#endif
989
990	if (fep->ops == NULL) {
991		printk(KERN_ERR DRV_MODULE_NAME
992		       ": %s No matching ops found (%d).\n",
993		       ndev->name, fpi->fs_no);
994		err = -EINVAL;
995		goto err;
996	}
997
998	r = (*fep->ops->setup_data)(ndev);
999	if (r != 0) {
1000		printk(KERN_ERR DRV_MODULE_NAME
1001		       ": %s setup_data failed\n",
1002			ndev->name);
1003		err = r;
1004		goto err;
1005	}
1006
1007	/* point rx_skbuff, tx_skbuff */
1008	fep->rx_skbuff = (struct sk_buff **)&fep[1];
1009	fep->tx_skbuff = fep->rx_skbuff + fpi->rx_ring;
1010
1011	/* init locks */
1012	spin_lock_init(&fep->lock);
1013	spin_lock_init(&fep->tx_lock);
1014
1015	/*
1016	 * Set the Ethernet address.
1017	 */
1018	for (i = 0; i < 6; i++)
1019		ndev->dev_addr[i] = fpi->macaddr[i];
1020
1021	r = (*fep->ops->allocate_bd)(ndev);
1022
1023	if (fep->ring_base == NULL) {
1024		printk(KERN_ERR DRV_MODULE_NAME
1025		       ": %s buffer descriptor alloc failed (%d).\n", ndev->name, r);
1026		err = r;
1027		goto err;
1028	}
1029
1030	/*
1031	 * Set receive and transmit descriptor base.
1032	 */
1033	fep->rx_bd_base = fep->ring_base;
1034	fep->tx_bd_base = fep->rx_bd_base + fpi->rx_ring;
1035
1036	/* initialize ring size variables */
1037	fep->tx_ring = fpi->tx_ring;
1038	fep->rx_ring = fpi->rx_ring;
1039
1040	/*
1041	 * The FEC Ethernet specific entries in the device structure.
1042	 */
1043	ndev->open = fs_enet_open;
1044	ndev->hard_start_xmit = fs_enet_start_xmit;
1045	ndev->tx_timeout = fs_timeout;
1046	ndev->watchdog_timeo = 2 * HZ;
1047	ndev->stop = fs_enet_close;
1048	ndev->get_stats = fs_enet_get_stats;
1049	ndev->set_multicast_list = fs_set_multicast_list;
1050	if (fpi->use_napi) {
1051		ndev->poll = fs_enet_rx_napi;
1052		ndev->weight = fpi->napi_weight;
1053	}
1054	ndev->ethtool_ops = &fs_ethtool_ops;
1055	ndev->do_ioctl = fs_ioctl;
1056
1057	init_timer(&fep->phy_timer_list);
1058
1059	netif_carrier_off(ndev);
1060
1061	err = register_netdev(ndev);
1062	if (err != 0) {
1063		printk(KERN_ERR DRV_MODULE_NAME
1064		       ": %s register_netdev failed.\n", ndev->name);
1065		goto err;
1066	}
1067	registered = 1;
1068
1069
1070	return ndev;
1071
1072      err:
1073	if (ndev != NULL) {
1074
1075		if (registered)
1076			unregister_netdev(ndev);
1077
1078		if (fep != NULL) {
1079			(*fep->ops->free_bd)(ndev);
1080			(*fep->ops->cleanup_data)(ndev);
1081		}
1082
1083		free_netdev(ndev);
1084	}
1085
1086	dev_set_drvdata(dev, NULL);
1087
1088	return ERR_PTR(err);
1089}
1090
1091static int fs_cleanup_instance(struct net_device *ndev)
1092{
1093	struct fs_enet_private *fep;
1094	const struct fs_platform_info *fpi;
1095	struct device *dev;
1096
1097	if (ndev == NULL)
1098		return -EINVAL;
1099
1100	fep = netdev_priv(ndev);
1101	if (fep == NULL)
1102		return -EINVAL;
1103
1104	fpi = fep->fpi;
1105
1106	unregister_netdev(ndev);
1107
1108	dma_free_coherent(fep->dev, (fpi->tx_ring + fpi->rx_ring) * sizeof(cbd_t),
1109			  fep->ring_base, fep->ring_mem_addr);
1110
1111	/* reset it */
1112	(*fep->ops->cleanup_data)(ndev);
1113
1114	dev = fep->dev;
1115	if (dev != NULL) {
1116		dev_set_drvdata(dev, NULL);
1117		fep->dev = NULL;
1118	}
1119
1120	free_netdev(ndev);
1121
1122	return 0;
1123}
1124
1125/**************************************************************************************/
1126
1127/* handy pointer to the immap */
1128void *fs_enet_immap = NULL;
1129
1130static int setup_immap(void)
1131{
1132	phys_addr_t paddr = 0;
1133	unsigned long size = 0;
1134
1135#ifdef CONFIG_CPM1
1136	paddr = IMAP_ADDR;
1137	size = 0x10000;	/* map 64K */
1138#endif
1139
1140#ifdef CONFIG_CPM2
1141	paddr = CPM_MAP_ADDR;
1142	size = 0x40000;	/* map 256 K */
1143#endif
1144	fs_enet_immap = ioremap(paddr, size);
1145	if (fs_enet_immap == NULL)
1146		return -EBADF;
1147
1148	return 0;
1149}
1150
1151static void cleanup_immap(void)
1152{
1153	if (fs_enet_immap != NULL) {
1154		iounmap(fs_enet_immap);
1155		fs_enet_immap = NULL;
1156	}
1157}
1158
1159/**************************************************************************************/
1160
1161static int __devinit fs_enet_probe(struct device *dev)
1162{
1163	struct net_device *ndev;
1164
1165	/* no fixup - no device */
1166	if (dev->platform_data == NULL) {
1167		printk(KERN_INFO "fs_enet: "
1168				"probe called with no platform data; "
1169				"remove unused devices\n");
1170		return -ENODEV;
1171	}
1172
1173	ndev = fs_init_instance(dev, dev->platform_data);
1174	if (IS_ERR(ndev))
1175		return PTR_ERR(ndev);
1176	return 0;
1177}
1178
1179static int fs_enet_remove(struct device *dev)
1180{
1181	return fs_cleanup_instance(dev_get_drvdata(dev));
1182}
1183
1184static struct device_driver fs_enet_fec_driver = {
1185	.name	  	= "fsl-cpm-fec",
1186	.bus		= &platform_bus_type,
1187	.probe		= fs_enet_probe,
1188	.remove		= fs_enet_remove,
1189#ifdef CONFIG_PM
1190/*	.suspend	= fs_enet_suspend,	TODO */
1191/*	.resume		= fs_enet_resume,	TODO */
1192#endif
1193};
1194
1195static struct device_driver fs_enet_scc_driver = {
1196	.name	  	= "fsl-cpm-scc",
1197	.bus		= &platform_bus_type,
1198	.probe		= fs_enet_probe,
1199	.remove		= fs_enet_remove,
1200#ifdef CONFIG_PM
1201/*	.suspend	= fs_enet_suspend,	TODO */
1202/*	.resume		= fs_enet_resume,	TODO */
1203#endif
1204};
1205
1206static struct device_driver fs_enet_fcc_driver = {
1207	.name	  	= "fsl-cpm-fcc",
1208	.bus		= &platform_bus_type,
1209	.probe		= fs_enet_probe,
1210	.remove		= fs_enet_remove,
1211#ifdef CONFIG_PM
1212/*	.suspend	= fs_enet_suspend,	TODO */
1213/*	.resume		= fs_enet_resume,	TODO */
1214#endif
1215};
1216
1217static int __init fs_init(void)
1218{
1219	int r;
1220
1221	printk(KERN_INFO
1222			"%s", version);
1223
1224	r = setup_immap();
1225	if (r != 0)
1226		return r;
1227
1228#ifdef CONFIG_FS_ENET_HAS_FCC
1229	/* let's insert mii stuff */
1230	r = fs_enet_mdio_bb_init();
1231
1232	if (r != 0) {
1233		printk(KERN_ERR DRV_MODULE_NAME
1234			"BB PHY init failed.\n");
1235		return r;
1236	}
1237	r = driver_register(&fs_enet_fcc_driver);
1238	if (r != 0)
1239		goto err;
1240#endif
1241
1242#ifdef CONFIG_FS_ENET_HAS_FEC
1243	r =  fs_enet_mdio_fec_init();
1244	if (r != 0) {
1245		printk(KERN_ERR DRV_MODULE_NAME
1246			"FEC PHY init failed.\n");
1247		return r;
1248	}
1249
1250	r = driver_register(&fs_enet_fec_driver);
1251	if (r != 0)
1252		goto err;
1253#endif
1254
1255#ifdef CONFIG_FS_ENET_HAS_SCC
1256	r = driver_register(&fs_enet_scc_driver);
1257	if (r != 0)
1258		goto err;
1259#endif
1260
1261	return 0;
1262err:
1263	cleanup_immap();
1264	return r;
1265
1266}
1267
1268static void __exit fs_cleanup(void)
1269{
1270	driver_unregister(&fs_enet_fec_driver);
1271	driver_unregister(&fs_enet_fcc_driver);
1272	driver_unregister(&fs_enet_scc_driver);
1273	cleanup_immap();
1274}
1275
1276/**************************************************************************************/
1277
1278module_init(fs_init);
1279module_exit(fs_cleanup);
1280