PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / net / ethernet / freescale / fs_enet / fs_enet-main.c
blob62f042d4aaa93cc85e79b079272ac26150b263a3
1 /*
2 * Combined Ethernet driver for Motorola MPC8xx and MPC82xx.
4 * Copyright (c) 2003 Intracom S.A.
5 * by Pantelis Antoniou <panto@intracom.gr>
7 * 2005 (c) MontaVista Software, Inc.
8 * Vitaly Bordug <vbordug@ru.mvista.com>
10 * Heavily based on original FEC driver by Dan Malek <dan@embeddededge.com>
11 * and modifications by Joakim Tjernlund <joakim.tjernlund@lumentis.se>
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.
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>
44 #include <linux/vmalloc.h>
45 #include <asm/pgtable.h>
46 #include <asm/irq.h>
47 #include <asm/uaccess.h>
49 #include "fs_enet.h"
51 /*************************************************/
53 MODULE_AUTHOR("Pantelis Antoniou <panto@intracom.gr>");
54 MODULE_DESCRIPTION("Freescale Ethernet Driver");
55 MODULE_LICENSE("GPL");
56 MODULE_VERSION(DRV_MODULE_VERSION);
58 static int fs_enet_debug = -1; /* -1 == use FS_ENET_DEF_MSG_ENABLE as value */
59 module_param(fs_enet_debug, int, 0);
60 MODULE_PARM_DESC(fs_enet_debug,
61 "Freescale bitmapped debugging message enable value");
63 #ifdef CONFIG_NET_POLL_CONTROLLER
64 static void fs_enet_netpoll(struct net_device *dev);
65 #endif
67 static void fs_set_multicast_list(struct net_device *dev)
69 struct fs_enet_private *fep = netdev_priv(dev);
71 (*fep->ops->set_multicast_list)(dev);
74 static void skb_align(struct sk_buff *skb, int align)
76 int off = ((unsigned long)skb->data) & (align - 1);
78 if (off)
79 skb_reserve(skb, align - off);
82 /* NAPI receive function */
83 static int fs_enet_rx_napi(struct napi_struct *napi, int budget)
85 struct fs_enet_private *fep = container_of(napi, struct fs_enet_private, napi);
86 struct net_device *dev = fep->ndev;
87 const struct fs_platform_info *fpi = fep->fpi;
88 cbd_t __iomem *bdp;
89 struct sk_buff *skb, *skbn, *skbt;
90 int received = 0;
91 u16 pkt_len, sc;
92 int curidx;
95 * First, grab all of the stats for the incoming packet.
96 * These get messed up if we get called due to a busy condition.
98 bdp = fep->cur_rx;
100 /* clear RX status bits for napi*/
101 (*fep->ops->napi_clear_rx_event)(dev);
103 while (((sc = CBDR_SC(bdp)) & BD_ENET_RX_EMPTY) == 0) {
104 curidx = bdp - fep->rx_bd_base;
107 * Since we have allocated space to hold a complete frame,
108 * the last indicator should be set.
110 if ((sc & BD_ENET_RX_LAST) == 0)
111 dev_warn(fep->dev, "rcv is not +last\n");
114 * Check for errors.
116 if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_CL |
117 BD_ENET_RX_NO | BD_ENET_RX_CR | BD_ENET_RX_OV)) {
118 fep->stats.rx_errors++;
119 /* Frame too long or too short. */
120 if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH))
121 fep->stats.rx_length_errors++;
122 /* Frame alignment */
123 if (sc & (BD_ENET_RX_NO | BD_ENET_RX_CL))
124 fep->stats.rx_frame_errors++;
125 /* CRC Error */
126 if (sc & BD_ENET_RX_CR)
127 fep->stats.rx_crc_errors++;
128 /* FIFO overrun */
129 if (sc & BD_ENET_RX_OV)
130 fep->stats.rx_crc_errors++;
132 skb = fep->rx_skbuff[curidx];
134 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
135 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
136 DMA_FROM_DEVICE);
138 skbn = skb;
140 } else {
141 skb = fep->rx_skbuff[curidx];
143 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
144 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
145 DMA_FROM_DEVICE);
148 * Process the incoming frame.
150 fep->stats.rx_packets++;
151 pkt_len = CBDR_DATLEN(bdp) - 4; /* remove CRC */
152 fep->stats.rx_bytes += pkt_len + 4;
154 if (pkt_len <= fpi->rx_copybreak) {
155 /* +2 to make IP header L1 cache aligned */
156 skbn = netdev_alloc_skb(dev, pkt_len + 2);
157 if (skbn != NULL) {
158 skb_reserve(skbn, 2); /* align IP header */
159 skb_copy_from_linear_data(skb,
160 skbn->data, pkt_len);
161 /* swap */
162 skbt = skb;
163 skb = skbn;
164 skbn = skbt;
166 } else {
167 skbn = netdev_alloc_skb(dev, ENET_RX_FRSIZE);
169 if (skbn)
170 skb_align(skbn, ENET_RX_ALIGN);
173 if (skbn != NULL) {
174 skb_put(skb, pkt_len); /* Make room */
175 skb->protocol = eth_type_trans(skb, dev);
176 received++;
177 netif_receive_skb(skb);
178 } else {
179 fep->stats.rx_dropped++;
180 skbn = skb;
184 fep->rx_skbuff[curidx] = skbn;
185 CBDW_BUFADDR(bdp, dma_map_single(fep->dev, skbn->data,
186 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
187 DMA_FROM_DEVICE));
188 CBDW_DATLEN(bdp, 0);
189 CBDW_SC(bdp, (sc & ~BD_ENET_RX_STATS) | BD_ENET_RX_EMPTY);
192 * Update BD pointer to next entry.
194 if ((sc & BD_ENET_RX_WRAP) == 0)
195 bdp++;
196 else
197 bdp = fep->rx_bd_base;
199 (*fep->ops->rx_bd_done)(dev);
201 if (received >= budget)
202 break;
205 fep->cur_rx = bdp;
207 if (received < budget) {
208 /* done */
209 napi_complete(napi);
210 (*fep->ops->napi_enable_rx)(dev);
212 return received;
215 /* non NAPI receive function */
216 static int fs_enet_rx_non_napi(struct net_device *dev)
218 struct fs_enet_private *fep = netdev_priv(dev);
219 const struct fs_platform_info *fpi = fep->fpi;
220 cbd_t __iomem *bdp;
221 struct sk_buff *skb, *skbn, *skbt;
222 int received = 0;
223 u16 pkt_len, sc;
224 int curidx;
226 * First, grab all of the stats for the incoming packet.
227 * These get messed up if we get called due to a busy condition.
229 bdp = fep->cur_rx;
231 while (((sc = CBDR_SC(bdp)) & BD_ENET_RX_EMPTY) == 0) {
233 curidx = bdp - fep->rx_bd_base;
236 * Since we have allocated space to hold a complete frame,
237 * the last indicator should be set.
239 if ((sc & BD_ENET_RX_LAST) == 0)
240 dev_warn(fep->dev, "rcv is not +last\n");
243 * Check for errors.
245 if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_CL |
246 BD_ENET_RX_NO | BD_ENET_RX_CR | BD_ENET_RX_OV)) {
247 fep->stats.rx_errors++;
248 /* Frame too long or too short. */
249 if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH))
250 fep->stats.rx_length_errors++;
251 /* Frame alignment */
252 if (sc & (BD_ENET_RX_NO | BD_ENET_RX_CL))
253 fep->stats.rx_frame_errors++;
254 /* CRC Error */
255 if (sc & BD_ENET_RX_CR)
256 fep->stats.rx_crc_errors++;
257 /* FIFO overrun */
258 if (sc & BD_ENET_RX_OV)
259 fep->stats.rx_crc_errors++;
261 skb = fep->rx_skbuff[curidx];
263 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
264 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
265 DMA_FROM_DEVICE);
267 skbn = skb;
269 } else {
271 skb = fep->rx_skbuff[curidx];
273 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
274 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
275 DMA_FROM_DEVICE);
278 * Process the incoming frame.
280 fep->stats.rx_packets++;
281 pkt_len = CBDR_DATLEN(bdp) - 4; /* remove CRC */
282 fep->stats.rx_bytes += pkt_len + 4;
284 if (pkt_len <= fpi->rx_copybreak) {
285 /* +2 to make IP header L1 cache aligned */
286 skbn = netdev_alloc_skb(dev, pkt_len + 2);
287 if (skbn != NULL) {
288 skb_reserve(skbn, 2); /* align IP header */
289 skb_copy_from_linear_data(skb,
290 skbn->data, pkt_len);
291 /* swap */
292 skbt = skb;
293 skb = skbn;
294 skbn = skbt;
296 } else {
297 skbn = netdev_alloc_skb(dev, ENET_RX_FRSIZE);
299 if (skbn)
300 skb_align(skbn, ENET_RX_ALIGN);
303 if (skbn != NULL) {
304 skb_put(skb, pkt_len); /* Make room */
305 skb->protocol = eth_type_trans(skb, dev);
306 received++;
307 netif_rx(skb);
308 } else {
309 fep->stats.rx_dropped++;
310 skbn = skb;
314 fep->rx_skbuff[curidx] = skbn;
315 CBDW_BUFADDR(bdp, dma_map_single(fep->dev, skbn->data,
316 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
317 DMA_FROM_DEVICE));
318 CBDW_DATLEN(bdp, 0);
319 CBDW_SC(bdp, (sc & ~BD_ENET_RX_STATS) | BD_ENET_RX_EMPTY);
322 * Update BD pointer to next entry.
324 if ((sc & BD_ENET_RX_WRAP) == 0)
325 bdp++;
326 else
327 bdp = fep->rx_bd_base;
329 (*fep->ops->rx_bd_done)(dev);
332 fep->cur_rx = bdp;
334 return 0;
337 static void fs_enet_tx(struct net_device *dev)
339 struct fs_enet_private *fep = netdev_priv(dev);
340 cbd_t __iomem *bdp;
341 struct sk_buff *skb;
342 int dirtyidx, do_wake, do_restart;
343 u16 sc;
345 spin_lock(&fep->tx_lock);
346 bdp = fep->dirty_tx;
348 do_wake = do_restart = 0;
349 while (((sc = CBDR_SC(bdp)) & BD_ENET_TX_READY) == 0) {
350 dirtyidx = bdp - fep->tx_bd_base;
352 if (fep->tx_free == fep->tx_ring)
353 break;
355 skb = fep->tx_skbuff[dirtyidx];
358 * Check for errors.
360 if (sc & (BD_ENET_TX_HB | BD_ENET_TX_LC |
361 BD_ENET_TX_RL | BD_ENET_TX_UN | BD_ENET_TX_CSL)) {
363 if (sc & BD_ENET_TX_HB) /* No heartbeat */
364 fep->stats.tx_heartbeat_errors++;
365 if (sc & BD_ENET_TX_LC) /* Late collision */
366 fep->stats.tx_window_errors++;
367 if (sc & BD_ENET_TX_RL) /* Retrans limit */
368 fep->stats.tx_aborted_errors++;
369 if (sc & BD_ENET_TX_UN) /* Underrun */
370 fep->stats.tx_fifo_errors++;
371 if (sc & BD_ENET_TX_CSL) /* Carrier lost */
372 fep->stats.tx_carrier_errors++;
374 if (sc & (BD_ENET_TX_LC | BD_ENET_TX_RL | BD_ENET_TX_UN)) {
375 fep->stats.tx_errors++;
376 do_restart = 1;
378 } else
379 fep->stats.tx_packets++;
381 if (sc & BD_ENET_TX_READY) {
382 dev_warn(fep->dev,
383 "HEY! Enet xmit interrupt and TX_READY.\n");
387 * Deferred means some collisions occurred during transmit,
388 * but we eventually sent the packet OK.
390 if (sc & BD_ENET_TX_DEF)
391 fep->stats.collisions++;
393 /* unmap */
394 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
395 skb->len, DMA_TO_DEVICE);
398 * Free the sk buffer associated with this last transmit.
400 dev_kfree_skb_irq(skb);
401 fep->tx_skbuff[dirtyidx] = NULL;
404 * Update pointer to next buffer descriptor to be transmitted.
406 if ((sc & BD_ENET_TX_WRAP) == 0)
407 bdp++;
408 else
409 bdp = fep->tx_bd_base;
412 * Since we have freed up a buffer, the ring is no longer
413 * full.
415 if (!fep->tx_free++)
416 do_wake = 1;
419 fep->dirty_tx = bdp;
421 if (do_restart)
422 (*fep->ops->tx_restart)(dev);
424 spin_unlock(&fep->tx_lock);
426 if (do_wake)
427 netif_wake_queue(dev);
431 * The interrupt handler.
432 * This is called from the MPC core interrupt.
434 static irqreturn_t
435 fs_enet_interrupt(int irq, void *dev_id)
437 struct net_device *dev = dev_id;
438 struct fs_enet_private *fep;
439 const struct fs_platform_info *fpi;
440 u32 int_events;
441 u32 int_clr_events;
442 int nr, napi_ok;
443 int handled;
445 fep = netdev_priv(dev);
446 fpi = fep->fpi;
448 nr = 0;
449 while ((int_events = (*fep->ops->get_int_events)(dev)) != 0) {
450 nr++;
452 int_clr_events = int_events;
453 if (fpi->use_napi)
454 int_clr_events &= ~fep->ev_napi_rx;
456 (*fep->ops->clear_int_events)(dev, int_clr_events);
458 if (int_events & fep->ev_err)
459 (*fep->ops->ev_error)(dev, int_events);
461 if (int_events & fep->ev_rx) {
462 if (!fpi->use_napi)
463 fs_enet_rx_non_napi(dev);
464 else {
465 napi_ok = napi_schedule_prep(&fep->napi);
467 (*fep->ops->napi_disable_rx)(dev);
468 (*fep->ops->clear_int_events)(dev, fep->ev_napi_rx);
470 /* NOTE: it is possible for FCCs in NAPI mode */
471 /* to submit a spurious interrupt while in poll */
472 if (napi_ok)
473 __napi_schedule(&fep->napi);
477 if (int_events & fep->ev_tx)
478 fs_enet_tx(dev);
481 handled = nr > 0;
482 return IRQ_RETVAL(handled);
485 void fs_init_bds(struct net_device *dev)
487 struct fs_enet_private *fep = netdev_priv(dev);
488 cbd_t __iomem *bdp;
489 struct sk_buff *skb;
490 int i;
492 fs_cleanup_bds(dev);
494 fep->dirty_tx = fep->cur_tx = fep->tx_bd_base;
495 fep->tx_free = fep->tx_ring;
496 fep->cur_rx = fep->rx_bd_base;
499 * Initialize the receive buffer descriptors.
501 for (i = 0, bdp = fep->rx_bd_base; i < fep->rx_ring; i++, bdp++) {
502 skb = netdev_alloc_skb(dev, ENET_RX_FRSIZE);
503 if (skb == NULL)
504 break;
506 skb_align(skb, ENET_RX_ALIGN);
507 fep->rx_skbuff[i] = skb;
508 CBDW_BUFADDR(bdp,
509 dma_map_single(fep->dev, skb->data,
510 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
511 DMA_FROM_DEVICE));
512 CBDW_DATLEN(bdp, 0); /* zero */
513 CBDW_SC(bdp, BD_ENET_RX_EMPTY |
514 ((i < fep->rx_ring - 1) ? 0 : BD_SC_WRAP));
517 * if we failed, fillup remainder
519 for (; i < fep->rx_ring; i++, bdp++) {
520 fep->rx_skbuff[i] = NULL;
521 CBDW_SC(bdp, (i < fep->rx_ring - 1) ? 0 : BD_SC_WRAP);
525 * ...and the same for transmit.
527 for (i = 0, bdp = fep->tx_bd_base; i < fep->tx_ring; i++, bdp++) {
528 fep->tx_skbuff[i] = NULL;
529 CBDW_BUFADDR(bdp, 0);
530 CBDW_DATLEN(bdp, 0);
531 CBDW_SC(bdp, (i < fep->tx_ring - 1) ? 0 : BD_SC_WRAP);
535 void fs_cleanup_bds(struct net_device *dev)
537 struct fs_enet_private *fep = netdev_priv(dev);
538 struct sk_buff *skb;
539 cbd_t __iomem *bdp;
540 int i;
543 * Reset SKB transmit buffers.
545 for (i = 0, bdp = fep->tx_bd_base; i < fep->tx_ring; i++, bdp++) {
546 if ((skb = fep->tx_skbuff[i]) == NULL)
547 continue;
549 /* unmap */
550 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
551 skb->len, DMA_TO_DEVICE);
553 fep->tx_skbuff[i] = NULL;
554 dev_kfree_skb(skb);
558 * Reset SKB receive buffers
560 for (i = 0, bdp = fep->rx_bd_base; i < fep->rx_ring; i++, bdp++) {
561 if ((skb = fep->rx_skbuff[i]) == NULL)
562 continue;
564 /* unmap */
565 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
566 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
567 DMA_FROM_DEVICE);
569 fep->rx_skbuff[i] = NULL;
571 dev_kfree_skb(skb);
575 /**********************************************************************************/
577 #ifdef CONFIG_FS_ENET_MPC5121_FEC
579 * MPC5121 FEC requeries 4-byte alignment for TX data buffer!
581 static struct sk_buff *tx_skb_align_workaround(struct net_device *dev,
582 struct sk_buff *skb)
584 struct sk_buff *new_skb;
586 /* Alloc new skb */
587 new_skb = netdev_alloc_skb(dev, skb->len + 4);
588 if (!new_skb)
589 return NULL;
591 /* Make sure new skb is properly aligned */
592 skb_align(new_skb, 4);
594 /* Copy data to new skb ... */
595 skb_copy_from_linear_data(skb, new_skb->data, skb->len);
596 skb_put(new_skb, skb->len);
598 /* ... and free an old one */
599 dev_kfree_skb_any(skb);
601 return new_skb;
603 #endif
605 static int fs_enet_start_xmit(struct sk_buff *skb, struct net_device *dev)
607 struct fs_enet_private *fep = netdev_priv(dev);
608 cbd_t __iomem *bdp;
609 int curidx;
610 u16 sc;
611 unsigned long flags;
613 #ifdef CONFIG_FS_ENET_MPC5121_FEC
614 if (((unsigned long)skb->data) & 0x3) {
615 skb = tx_skb_align_workaround(dev, skb);
616 if (!skb) {
618 * We have lost packet due to memory allocation error
619 * in tx_skb_align_workaround(). Hopefully original
620 * skb is still valid, so try transmit it later.
622 return NETDEV_TX_BUSY;
625 #endif
626 spin_lock_irqsave(&fep->tx_lock, flags);
629 * Fill in a Tx ring entry
631 bdp = fep->cur_tx;
633 if (!fep->tx_free || (CBDR_SC(bdp) & BD_ENET_TX_READY)) {
634 netif_stop_queue(dev);
635 spin_unlock_irqrestore(&fep->tx_lock, flags);
638 * Ooops. All transmit buffers are full. Bail out.
639 * This should not happen, since the tx queue should be stopped.
641 dev_warn(fep->dev, "tx queue full!.\n");
642 return NETDEV_TX_BUSY;
645 curidx = bdp - fep->tx_bd_base;
647 * Clear all of the status flags.
649 CBDC_SC(bdp, BD_ENET_TX_STATS);
652 * Save skb pointer.
654 fep->tx_skbuff[curidx] = skb;
656 fep->stats.tx_bytes += skb->len;
659 * Push the data cache so the CPM does not get stale memory data.
661 CBDW_BUFADDR(bdp, dma_map_single(fep->dev,
662 skb->data, skb->len, DMA_TO_DEVICE));
663 CBDW_DATLEN(bdp, skb->len);
666 * If this was the last BD in the ring, start at the beginning again.
668 if ((CBDR_SC(bdp) & BD_ENET_TX_WRAP) == 0)
669 fep->cur_tx++;
670 else
671 fep->cur_tx = fep->tx_bd_base;
673 if (!--fep->tx_free)
674 netif_stop_queue(dev);
676 /* Trigger transmission start */
677 sc = BD_ENET_TX_READY | BD_ENET_TX_INTR |
678 BD_ENET_TX_LAST | BD_ENET_TX_TC;
680 /* note that while FEC does not have this bit
681 * it marks it as available for software use
682 * yay for hw reuse :) */
683 if (skb->len <= 60)
684 sc |= BD_ENET_TX_PAD;
685 CBDS_SC(bdp, sc);
687 skb_tx_timestamp(skb);
689 (*fep->ops->tx_kickstart)(dev);
691 spin_unlock_irqrestore(&fep->tx_lock, flags);
693 return NETDEV_TX_OK;
696 static void fs_timeout(struct net_device *dev)
698 struct fs_enet_private *fep = netdev_priv(dev);
699 unsigned long flags;
700 int wake = 0;
702 fep->stats.tx_errors++;
704 spin_lock_irqsave(&fep->lock, flags);
706 if (dev->flags & IFF_UP) {
707 phy_stop(fep->phydev);
708 (*fep->ops->stop)(dev);
709 (*fep->ops->restart)(dev);
710 phy_start(fep->phydev);
713 phy_start(fep->phydev);
714 wake = fep->tx_free && !(CBDR_SC(fep->cur_tx) & BD_ENET_TX_READY);
715 spin_unlock_irqrestore(&fep->lock, flags);
717 if (wake)
718 netif_wake_queue(dev);
721 /*-----------------------------------------------------------------------------
722 * generic link-change handler - should be sufficient for most cases
723 *-----------------------------------------------------------------------------*/
724 static void generic_adjust_link(struct net_device *dev)
726 struct fs_enet_private *fep = netdev_priv(dev);
727 struct phy_device *phydev = fep->phydev;
728 int new_state = 0;
730 if (phydev->link) {
731 /* adjust to duplex mode */
732 if (phydev->duplex != fep->oldduplex) {
733 new_state = 1;
734 fep->oldduplex = phydev->duplex;
737 if (phydev->speed != fep->oldspeed) {
738 new_state = 1;
739 fep->oldspeed = phydev->speed;
742 if (!fep->oldlink) {
743 new_state = 1;
744 fep->oldlink = 1;
747 if (new_state)
748 fep->ops->restart(dev);
749 } else if (fep->oldlink) {
750 new_state = 1;
751 fep->oldlink = 0;
752 fep->oldspeed = 0;
753 fep->oldduplex = -1;
756 if (new_state && netif_msg_link(fep))
757 phy_print_status(phydev);
761 static void fs_adjust_link(struct net_device *dev)
763 struct fs_enet_private *fep = netdev_priv(dev);
764 unsigned long flags;
766 spin_lock_irqsave(&fep->lock, flags);
768 if(fep->ops->adjust_link)
769 fep->ops->adjust_link(dev);
770 else
771 generic_adjust_link(dev);
773 spin_unlock_irqrestore(&fep->lock, flags);
776 static int fs_init_phy(struct net_device *dev)
778 struct fs_enet_private *fep = netdev_priv(dev);
779 struct phy_device *phydev;
780 phy_interface_t iface;
782 fep->oldlink = 0;
783 fep->oldspeed = 0;
784 fep->oldduplex = -1;
786 iface = fep->fpi->use_rmii ?
787 PHY_INTERFACE_MODE_RMII : PHY_INTERFACE_MODE_MII;
789 phydev = of_phy_connect(dev, fep->fpi->phy_node, &fs_adjust_link, 0,
790 iface);
791 if (!phydev) {
792 phydev = of_phy_connect_fixed_link(dev, &fs_adjust_link,
793 iface);
795 if (!phydev) {
796 dev_err(&dev->dev, "Could not attach to PHY\n");
797 return -ENODEV;
800 fep->phydev = phydev;
802 return 0;
805 static int fs_enet_open(struct net_device *dev)
807 struct fs_enet_private *fep = netdev_priv(dev);
808 int r;
809 int err;
811 /* to initialize the fep->cur_rx,... */
812 /* not doing this, will cause a crash in fs_enet_rx_napi */
813 fs_init_bds(fep->ndev);
815 if (fep->fpi->use_napi)
816 napi_enable(&fep->napi);
818 /* Install our interrupt handler. */
819 r = request_irq(fep->interrupt, fs_enet_interrupt, IRQF_SHARED,
820 "fs_enet-mac", dev);
821 if (r != 0) {
822 dev_err(fep->dev, "Could not allocate FS_ENET IRQ!");
823 if (fep->fpi->use_napi)
824 napi_disable(&fep->napi);
825 return -EINVAL;
828 err = fs_init_phy(dev);
829 if (err) {
830 free_irq(fep->interrupt, dev);
831 if (fep->fpi->use_napi)
832 napi_disable(&fep->napi);
833 return err;
835 phy_start(fep->phydev);
837 netif_start_queue(dev);
839 return 0;
842 static int fs_enet_close(struct net_device *dev)
844 struct fs_enet_private *fep = netdev_priv(dev);
845 unsigned long flags;
847 netif_stop_queue(dev);
848 netif_carrier_off(dev);
849 if (fep->fpi->use_napi)
850 napi_disable(&fep->napi);
851 phy_stop(fep->phydev);
853 spin_lock_irqsave(&fep->lock, flags);
854 spin_lock(&fep->tx_lock);
855 (*fep->ops->stop)(dev);
856 spin_unlock(&fep->tx_lock);
857 spin_unlock_irqrestore(&fep->lock, flags);
859 /* release any irqs */
860 phy_disconnect(fep->phydev);
861 fep->phydev = NULL;
862 free_irq(fep->interrupt, dev);
864 return 0;
867 static struct net_device_stats *fs_enet_get_stats(struct net_device *dev)
869 struct fs_enet_private *fep = netdev_priv(dev);
870 return &fep->stats;
873 /*************************************************************************/
875 static void fs_get_drvinfo(struct net_device *dev,
876 struct ethtool_drvinfo *info)
878 strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
879 strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
882 static int fs_get_regs_len(struct net_device *dev)
884 struct fs_enet_private *fep = netdev_priv(dev);
886 return (*fep->ops->get_regs_len)(dev);
889 static void fs_get_regs(struct net_device *dev, struct ethtool_regs *regs,
890 void *p)
892 struct fs_enet_private *fep = netdev_priv(dev);
893 unsigned long flags;
894 int r, len;
896 len = regs->len;
898 spin_lock_irqsave(&fep->lock, flags);
899 r = (*fep->ops->get_regs)(dev, p, &len);
900 spin_unlock_irqrestore(&fep->lock, flags);
902 if (r == 0)
903 regs->version = 0;
906 static int fs_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
908 struct fs_enet_private *fep = netdev_priv(dev);
910 if (!fep->phydev)
911 return -ENODEV;
913 return phy_ethtool_gset(fep->phydev, cmd);
916 static int fs_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
918 struct fs_enet_private *fep = netdev_priv(dev);
920 if (!fep->phydev)
921 return -ENODEV;
923 return phy_ethtool_sset(fep->phydev, cmd);
926 static int fs_nway_reset(struct net_device *dev)
928 return 0;
931 static u32 fs_get_msglevel(struct net_device *dev)
933 struct fs_enet_private *fep = netdev_priv(dev);
934 return fep->msg_enable;
937 static void fs_set_msglevel(struct net_device *dev, u32 value)
939 struct fs_enet_private *fep = netdev_priv(dev);
940 fep->msg_enable = value;
943 static const struct ethtool_ops fs_ethtool_ops = {
944 .get_drvinfo = fs_get_drvinfo,
945 .get_regs_len = fs_get_regs_len,
946 .get_settings = fs_get_settings,
947 .set_settings = fs_set_settings,
948 .nway_reset = fs_nway_reset,
949 .get_link = ethtool_op_get_link,
950 .get_msglevel = fs_get_msglevel,
951 .set_msglevel = fs_set_msglevel,
952 .get_regs = fs_get_regs,
953 .get_ts_info = ethtool_op_get_ts_info,
956 static int fs_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
958 struct fs_enet_private *fep = netdev_priv(dev);
960 if (!netif_running(dev))
961 return -EINVAL;
963 return phy_mii_ioctl(fep->phydev, rq, cmd);
966 extern int fs_mii_connect(struct net_device *dev);
967 extern void fs_mii_disconnect(struct net_device *dev);
969 /**************************************************************************************/
971 #ifdef CONFIG_FS_ENET_HAS_FEC
972 #define IS_FEC(match) ((match)->data == &fs_fec_ops)
973 #else
974 #define IS_FEC(match) 0
975 #endif
977 static const struct net_device_ops fs_enet_netdev_ops = {
978 .ndo_open = fs_enet_open,
979 .ndo_stop = fs_enet_close,
980 .ndo_get_stats = fs_enet_get_stats,
981 .ndo_start_xmit = fs_enet_start_xmit,
982 .ndo_tx_timeout = fs_timeout,
983 .ndo_set_rx_mode = fs_set_multicast_list,
984 .ndo_do_ioctl = fs_ioctl,
985 .ndo_validate_addr = eth_validate_addr,
986 .ndo_set_mac_address = eth_mac_addr,
987 .ndo_change_mtu = eth_change_mtu,
988 #ifdef CONFIG_NET_POLL_CONTROLLER
989 .ndo_poll_controller = fs_enet_netpoll,
990 #endif
993 static struct of_device_id fs_enet_match[];
994 static int fs_enet_probe(struct platform_device *ofdev)
996 const struct of_device_id *match;
997 struct net_device *ndev;
998 struct fs_enet_private *fep;
999 struct fs_platform_info *fpi;
1000 const u32 *data;
1001 struct clk *clk;
1002 int err;
1003 const u8 *mac_addr;
1004 const char *phy_connection_type;
1005 int privsize, len, ret = -ENODEV;
1007 match = of_match_device(fs_enet_match, &ofdev->dev);
1008 if (!match)
1009 return -EINVAL;
1011 fpi = kzalloc(sizeof(*fpi), GFP_KERNEL);
1012 if (!fpi)
1013 return -ENOMEM;
1015 if (!IS_FEC(match)) {
1016 data = of_get_property(ofdev->dev.of_node, "fsl,cpm-command", &len);
1017 if (!data || len != 4)
1018 goto out_free_fpi;
1020 fpi->cp_command = *data;
1023 fpi->rx_ring = 32;
1024 fpi->tx_ring = 32;
1025 fpi->rx_copybreak = 240;
1026 fpi->use_napi = 1;
1027 fpi->napi_weight = 17;
1028 fpi->phy_node = of_parse_phandle(ofdev->dev.of_node, "phy-handle", 0);
1029 if ((!fpi->phy_node) && (!of_get_property(ofdev->dev.of_node, "fixed-link",
1030 NULL)))
1031 goto out_free_fpi;
1033 if (of_device_is_compatible(ofdev->dev.of_node, "fsl,mpc5125-fec")) {
1034 phy_connection_type = of_get_property(ofdev->dev.of_node,
1035 "phy-connection-type", NULL);
1036 if (phy_connection_type && !strcmp("rmii", phy_connection_type))
1037 fpi->use_rmii = 1;
1040 /* make clock lookup non-fatal (the driver is shared among platforms),
1041 * but require enable to succeed when a clock was specified/found,
1042 * keep a reference to the clock upon successful acquisition
1044 clk = devm_clk_get(&ofdev->dev, "per");
1045 if (!IS_ERR(clk)) {
1046 err = clk_prepare_enable(clk);
1047 if (err) {
1048 ret = err;
1049 goto out_free_fpi;
1051 fpi->clk_per = clk;
1054 privsize = sizeof(*fep) +
1055 sizeof(struct sk_buff **) *
1056 (fpi->rx_ring + fpi->tx_ring);
1058 ndev = alloc_etherdev(privsize);
1059 if (!ndev) {
1060 ret = -ENOMEM;
1061 goto out_put;
1064 SET_NETDEV_DEV(ndev, &ofdev->dev);
1065 platform_set_drvdata(ofdev, ndev);
1067 fep = netdev_priv(ndev);
1068 fep->dev = &ofdev->dev;
1069 fep->ndev = ndev;
1070 fep->fpi = fpi;
1071 fep->ops = match->data;
1073 ret = fep->ops->setup_data(ndev);
1074 if (ret)
1075 goto out_free_dev;
1077 fep->rx_skbuff = (struct sk_buff **)&fep[1];
1078 fep->tx_skbuff = fep->rx_skbuff + fpi->rx_ring;
1080 spin_lock_init(&fep->lock);
1081 spin_lock_init(&fep->tx_lock);
1083 mac_addr = of_get_mac_address(ofdev->dev.of_node);
1084 if (mac_addr)
1085 memcpy(ndev->dev_addr, mac_addr, ETH_ALEN);
1087 ret = fep->ops->allocate_bd(ndev);
1088 if (ret)
1089 goto out_cleanup_data;
1091 fep->rx_bd_base = fep->ring_base;
1092 fep->tx_bd_base = fep->rx_bd_base + fpi->rx_ring;
1094 fep->tx_ring = fpi->tx_ring;
1095 fep->rx_ring = fpi->rx_ring;
1097 ndev->netdev_ops = &fs_enet_netdev_ops;
1098 ndev->watchdog_timeo = 2 * HZ;
1099 if (fpi->use_napi)
1100 netif_napi_add(ndev, &fep->napi, fs_enet_rx_napi,
1101 fpi->napi_weight);
1103 ndev->ethtool_ops = &fs_ethtool_ops;
1105 init_timer(&fep->phy_timer_list);
1107 netif_carrier_off(ndev);
1109 ret = register_netdev(ndev);
1110 if (ret)
1111 goto out_free_bd;
1113 pr_info("%s: fs_enet: %pM\n", ndev->name, ndev->dev_addr);
1115 return 0;
1117 out_free_bd:
1118 fep->ops->free_bd(ndev);
1119 out_cleanup_data:
1120 fep->ops->cleanup_data(ndev);
1121 out_free_dev:
1122 free_netdev(ndev);
1123 out_put:
1124 of_node_put(fpi->phy_node);
1125 if (fpi->clk_per)
1126 clk_disable_unprepare(fpi->clk_per);
1127 out_free_fpi:
1128 kfree(fpi);
1129 return ret;
1132 static int fs_enet_remove(struct platform_device *ofdev)
1134 struct net_device *ndev = platform_get_drvdata(ofdev);
1135 struct fs_enet_private *fep = netdev_priv(ndev);
1137 unregister_netdev(ndev);
1139 fep->ops->free_bd(ndev);
1140 fep->ops->cleanup_data(ndev);
1141 dev_set_drvdata(fep->dev, NULL);
1142 of_node_put(fep->fpi->phy_node);
1143 if (fep->fpi->clk_per)
1144 clk_disable_unprepare(fep->fpi->clk_per);
1145 free_netdev(ndev);
1146 return 0;
1149 static struct of_device_id fs_enet_match[] = {
1150 #ifdef CONFIG_FS_ENET_HAS_SCC
1152 .compatible = "fsl,cpm1-scc-enet",
1153 .data = (void *)&fs_scc_ops,
1156 .compatible = "fsl,cpm2-scc-enet",
1157 .data = (void *)&fs_scc_ops,
1159 #endif
1160 #ifdef CONFIG_FS_ENET_HAS_FCC
1162 .compatible = "fsl,cpm2-fcc-enet",
1163 .data = (void *)&fs_fcc_ops,
1165 #endif
1166 #ifdef CONFIG_FS_ENET_HAS_FEC
1167 #ifdef CONFIG_FS_ENET_MPC5121_FEC
1169 .compatible = "fsl,mpc5121-fec",
1170 .data = (void *)&fs_fec_ops,
1173 .compatible = "fsl,mpc5125-fec",
1174 .data = (void *)&fs_fec_ops,
1176 #else
1178 .compatible = "fsl,pq1-fec-enet",
1179 .data = (void *)&fs_fec_ops,
1181 #endif
1182 #endif
1185 MODULE_DEVICE_TABLE(of, fs_enet_match);
1187 static struct platform_driver fs_enet_driver = {
1188 .driver = {
1189 .owner = THIS_MODULE,
1190 .name = "fs_enet",
1191 .of_match_table = fs_enet_match,
1193 .probe = fs_enet_probe,
1194 .remove = fs_enet_remove,
1197 #ifdef CONFIG_NET_POLL_CONTROLLER
1198 static void fs_enet_netpoll(struct net_device *dev)
1200 disable_irq(dev->irq);
1201 fs_enet_interrupt(dev->irq, dev);
1202 enable_irq(dev->irq);
1204 #endif
1206 module_platform_driver(fs_enet_driver);