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