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