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