]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/net/fec.c
m68knommu: fec: remove FADS
[linux-2.6-omap-h63xx.git] / drivers / net / fec.c
1 /*
2  * Fast Ethernet Controller (FEC) driver for Motorola MPC8xx.
3  * Copyright (c) 1997 Dan Malek (dmalek@jlc.net)
4  *
5  * Right now, I am very wasteful with the buffers.  I allocate memory
6  * pages and then divide them into 2K frame buffers.  This way I know I
7  * have buffers large enough to hold one frame within one buffer descriptor.
8  * Once I get this working, I will use 64 or 128 byte CPM buffers, which
9  * will be much more memory efficient and will easily handle lots of
10  * small packets.
11  *
12  * Much better multiple PHY support by Magnus Damm.
13  * Copyright (c) 2000 Ericsson Radio Systems AB.
14  *
15  * Support for FEC controller of ColdFire processors.
16  * Copyright (c) 2001-2005 Greg Ungerer (gerg@snapgear.com)
17  *
18  * Bug fixes and cleanup by Philippe De Muyter (phdm@macqel.be)
19  * Copyright (c) 2004-2006 Macq Electronique SA.
20  */
21
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/string.h>
25 #include <linux/ptrace.h>
26 #include <linux/errno.h>
27 #include <linux/ioport.h>
28 #include <linux/slab.h>
29 #include <linux/interrupt.h>
30 #include <linux/pci.h>
31 #include <linux/init.h>
32 #include <linux/delay.h>
33 #include <linux/netdevice.h>
34 #include <linux/etherdevice.h>
35 #include <linux/skbuff.h>
36 #include <linux/spinlock.h>
37 #include <linux/workqueue.h>
38 #include <linux/bitops.h>
39
40 #include <asm/irq.h>
41 #include <asm/uaccess.h>
42 #include <asm/io.h>
43 #include <asm/pgtable.h>
44 #include <asm/cacheflush.h>
45
46 #if defined(CONFIG_M523x) || defined(CONFIG_M527x) || \
47     defined(CONFIG_M5272) || defined(CONFIG_M528x) || \
48     defined(CONFIG_M520x) || defined(CONFIG_M532x)
49 #include <asm/coldfire.h>
50 #include <asm/mcfsim.h>
51 #include "fec.h"
52 #else
53 #include <asm/8xx_immap.h>
54 #include <asm/mpc8xx.h>
55 #include "commproc.h"
56 #endif
57
58 #if defined(CONFIG_FEC2)
59 #define FEC_MAX_PORTS   2
60 #else
61 #define FEC_MAX_PORTS   1
62 #endif
63
64 #if defined(CONFIG_FADS) || defined(CONFIG_RPXCLASSIC) || defined(CONFIG_M5272)
65 #define HAVE_mii_link_interrupt
66 #endif
67
68 /*
69  * Define the fixed address of the FEC hardware.
70  */
71 static unsigned int fec_hw[] = {
72 #if defined(CONFIG_M5272)
73         (MCF_MBAR + 0x840),
74 #elif defined(CONFIG_M527x)
75         (MCF_MBAR + 0x1000),
76         (MCF_MBAR + 0x1800),
77 #elif defined(CONFIG_M523x) || defined(CONFIG_M528x)
78         (MCF_MBAR + 0x1000),
79 #elif defined(CONFIG_M520x)
80         (MCF_MBAR+0x30000),
81 #elif defined(CONFIG_M532x)
82         (MCF_MBAR+0xfc030000),
83 #else
84         &(((immap_t *)IMAP_ADDR)->im_cpm.cp_fec),
85 #endif
86 };
87
88 static unsigned char    fec_mac_default[] = {
89         0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
90 };
91
92 /*
93  * Some hardware gets it MAC address out of local flash memory.
94  * if this is non-zero then assume it is the address to get MAC from.
95  */
96 #if defined(CONFIG_NETtel)
97 #define FEC_FLASHMAC    0xf0006006
98 #elif defined(CONFIG_GILBARCONAP) || defined(CONFIG_SCALES)
99 #define FEC_FLASHMAC    0xf0006000
100 #elif defined(CONFIG_CANCam)
101 #define FEC_FLASHMAC    0xf0020000
102 #elif defined (CONFIG_M5272C3)
103 #define FEC_FLASHMAC    (0xffe04000 + 4)
104 #elif defined(CONFIG_MOD5272)
105 #define FEC_FLASHMAC    0xffc0406b
106 #else
107 #define FEC_FLASHMAC    0
108 #endif
109
110 /* Forward declarations of some structures to support different PHYs
111 */
112
113 typedef struct {
114         uint mii_data;
115         void (*funct)(uint mii_reg, struct net_device *dev);
116 } phy_cmd_t;
117
118 typedef struct {
119         uint id;
120         char *name;
121
122         const phy_cmd_t *config;
123         const phy_cmd_t *startup;
124         const phy_cmd_t *ack_int;
125         const phy_cmd_t *shutdown;
126 } phy_info_t;
127
128 /* The number of Tx and Rx buffers.  These are allocated from the page
129  * pool.  The code may assume these are power of two, so it it best
130  * to keep them that size.
131  * We don't need to allocate pages for the transmitter.  We just use
132  * the skbuffer directly.
133  */
134 #define FEC_ENET_RX_PAGES       8
135 #define FEC_ENET_RX_FRSIZE      2048
136 #define FEC_ENET_RX_FRPPG       (PAGE_SIZE / FEC_ENET_RX_FRSIZE)
137 #define RX_RING_SIZE            (FEC_ENET_RX_FRPPG * FEC_ENET_RX_PAGES)
138 #define FEC_ENET_TX_FRSIZE      2048
139 #define FEC_ENET_TX_FRPPG       (PAGE_SIZE / FEC_ENET_TX_FRSIZE)
140 #define TX_RING_SIZE            16      /* Must be power of two */
141 #define TX_RING_MOD_MASK        15      /*   for this to work */
142
143 #if (((RX_RING_SIZE + TX_RING_SIZE) * 8) > PAGE_SIZE)
144 #error "FEC: descriptor ring size constants too large"
145 #endif
146
147 /* Interrupt events/masks.
148 */
149 #define FEC_ENET_HBERR  ((uint)0x80000000)      /* Heartbeat error */
150 #define FEC_ENET_BABR   ((uint)0x40000000)      /* Babbling receiver */
151 #define FEC_ENET_BABT   ((uint)0x20000000)      /* Babbling transmitter */
152 #define FEC_ENET_GRA    ((uint)0x10000000)      /* Graceful stop complete */
153 #define FEC_ENET_TXF    ((uint)0x08000000)      /* Full frame transmitted */
154 #define FEC_ENET_TXB    ((uint)0x04000000)      /* A buffer was transmitted */
155 #define FEC_ENET_RXF    ((uint)0x02000000)      /* Full frame received */
156 #define FEC_ENET_RXB    ((uint)0x01000000)      /* A buffer was received */
157 #define FEC_ENET_MII    ((uint)0x00800000)      /* MII interrupt */
158 #define FEC_ENET_EBERR  ((uint)0x00400000)      /* SDMA bus error */
159
160 /* The FEC stores dest/src/type, data, and checksum for receive packets.
161  */
162 #define PKT_MAXBUF_SIZE         1518
163 #define PKT_MINBUF_SIZE         64
164 #define PKT_MAXBLR_SIZE         1520
165
166
167 /*
168  * The 5270/5271/5280/5282/532x RX control register also contains maximum frame
169  * size bits. Other FEC hardware does not, so we need to take that into
170  * account when setting it.
171  */
172 #if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
173     defined(CONFIG_M520x) || defined(CONFIG_M532x)
174 #define OPT_FRAME_SIZE  (PKT_MAXBUF_SIZE << 16)
175 #else
176 #define OPT_FRAME_SIZE  0
177 #endif
178
179 /* The FEC buffer descriptors track the ring buffers.  The rx_bd_base and
180  * tx_bd_base always point to the base of the buffer descriptors.  The
181  * cur_rx and cur_tx point to the currently available buffer.
182  * The dirty_tx tracks the current buffer that is being sent by the
183  * controller.  The cur_tx and dirty_tx are equal under both completely
184  * empty and completely full conditions.  The empty/ready indicator in
185  * the buffer descriptor determines the actual condition.
186  */
187 struct fec_enet_private {
188         /* Hardware registers of the FEC device */
189         volatile fec_t  *hwp;
190
191         struct net_device *netdev;
192
193         /* The saved address of a sent-in-place packet/buffer, for skfree(). */
194         unsigned char *tx_bounce[TX_RING_SIZE];
195         struct  sk_buff* tx_skbuff[TX_RING_SIZE];
196         ushort  skb_cur;
197         ushort  skb_dirty;
198
199         /* CPM dual port RAM relative addresses.
200         */
201         cbd_t   *rx_bd_base;            /* Address of Rx and Tx buffers. */
202         cbd_t   *tx_bd_base;
203         cbd_t   *cur_rx, *cur_tx;               /* The next free ring entry */
204         cbd_t   *dirty_tx;      /* The ring entries to be free()ed. */
205         uint    tx_full;
206         /* hold while accessing the HW like ringbuffer for tx/rx but not MAC */
207         spinlock_t hw_lock;
208         /* hold while accessing the mii_list_t() elements */
209         spinlock_t mii_lock;
210
211         uint    phy_id;
212         uint    phy_id_done;
213         uint    phy_status;
214         uint    phy_speed;
215         phy_info_t const        *phy;
216         struct work_struct phy_task;
217
218         uint    sequence_done;
219         uint    mii_phy_task_queued;
220
221         uint    phy_addr;
222
223         int     index;
224         int     opened;
225         int     link;
226         int     old_link;
227         int     full_duplex;
228 };
229
230 static int fec_enet_open(struct net_device *dev);
231 static int fec_enet_start_xmit(struct sk_buff *skb, struct net_device *dev);
232 static void fec_enet_mii(struct net_device *dev);
233 static irqreturn_t fec_enet_interrupt(int irq, void * dev_id);
234 static void fec_enet_tx(struct net_device *dev);
235 static void fec_enet_rx(struct net_device *dev);
236 static int fec_enet_close(struct net_device *dev);
237 static void set_multicast_list(struct net_device *dev);
238 static void fec_restart(struct net_device *dev, int duplex);
239 static void fec_stop(struct net_device *dev);
240 static void fec_set_mac_address(struct net_device *dev);
241
242
243 /* MII processing.  We keep this as simple as possible.  Requests are
244  * placed on the list (if there is room).  When the request is finished
245  * by the MII, an optional function may be called.
246  */
247 typedef struct mii_list {
248         uint    mii_regval;
249         void    (*mii_func)(uint val, struct net_device *dev);
250         struct  mii_list *mii_next;
251 } mii_list_t;
252
253 #define         NMII    20
254 static mii_list_t       mii_cmds[NMII];
255 static mii_list_t       *mii_free;
256 static mii_list_t       *mii_head;
257 static mii_list_t       *mii_tail;
258
259 static int      mii_queue(struct net_device *dev, int request,
260                                 void (*func)(uint, struct net_device *));
261
262 /* Make MII read/write commands for the FEC.
263 */
264 #define mk_mii_read(REG)        (0x60020000 | ((REG & 0x1f) << 18))
265 #define mk_mii_write(REG, VAL)  (0x50020000 | ((REG & 0x1f) << 18) | \
266                                                 (VAL & 0xffff))
267 #define mk_mii_end      0
268
269 /* Transmitter timeout.
270 */
271 #define TX_TIMEOUT (2*HZ)
272
273 /* Register definitions for the PHY.
274 */
275
276 #define MII_REG_CR          0  /* Control Register                         */
277 #define MII_REG_SR          1  /* Status Register                          */
278 #define MII_REG_PHYIR1      2  /* PHY Identification Register 1            */
279 #define MII_REG_PHYIR2      3  /* PHY Identification Register 2            */
280 #define MII_REG_ANAR        4  /* A-N Advertisement Register               */
281 #define MII_REG_ANLPAR      5  /* A-N Link Partner Ability Register        */
282 #define MII_REG_ANER        6  /* A-N Expansion Register                   */
283 #define MII_REG_ANNPTR      7  /* A-N Next Page Transmit Register          */
284 #define MII_REG_ANLPRNPR    8  /* A-N Link Partner Received Next Page Reg. */
285
286 /* values for phy_status */
287
288 #define PHY_CONF_ANE    0x0001  /* 1 auto-negotiation enabled */
289 #define PHY_CONF_LOOP   0x0002  /* 1 loopback mode enabled */
290 #define PHY_CONF_SPMASK 0x00f0  /* mask for speed */
291 #define PHY_CONF_10HDX  0x0010  /* 10 Mbit half duplex supported */
292 #define PHY_CONF_10FDX  0x0020  /* 10 Mbit full duplex supported */
293 #define PHY_CONF_100HDX 0x0040  /* 100 Mbit half duplex supported */
294 #define PHY_CONF_100FDX 0x0080  /* 100 Mbit full duplex supported */
295
296 #define PHY_STAT_LINK   0x0100  /* 1 up - 0 down */
297 #define PHY_STAT_FAULT  0x0200  /* 1 remote fault */
298 #define PHY_STAT_ANC    0x0400  /* 1 auto-negotiation complete  */
299 #define PHY_STAT_SPMASK 0xf000  /* mask for speed */
300 #define PHY_STAT_10HDX  0x1000  /* 10 Mbit half duplex selected */
301 #define PHY_STAT_10FDX  0x2000  /* 10 Mbit full duplex selected */
302 #define PHY_STAT_100HDX 0x4000  /* 100 Mbit half duplex selected */
303 #define PHY_STAT_100FDX 0x8000  /* 100 Mbit full duplex selected */
304
305
306 static int
307 fec_enet_start_xmit(struct sk_buff *skb, struct net_device *dev)
308 {
309         struct fec_enet_private *fep;
310         volatile fec_t  *fecp;
311         volatile cbd_t  *bdp;
312         unsigned short  status;
313         unsigned long flags;
314
315         fep = netdev_priv(dev);
316         fecp = (volatile fec_t*)dev->base_addr;
317
318         if (!fep->link) {
319                 /* Link is down or autonegotiation is in progress. */
320                 return 1;
321         }
322
323         spin_lock_irqsave(&fep->hw_lock, flags);
324         /* Fill in a Tx ring entry */
325         bdp = fep->cur_tx;
326
327         status = bdp->cbd_sc;
328 #ifndef final_version
329         if (status & BD_ENET_TX_READY) {
330                 /* Ooops.  All transmit buffers are full.  Bail out.
331                  * This should not happen, since dev->tbusy should be set.
332                  */
333                 printk("%s: tx queue full!.\n", dev->name);
334                 spin_unlock_irqrestore(&fep->hw_lock, flags);
335                 return 1;
336         }
337 #endif
338
339         /* Clear all of the status flags.
340          */
341         status &= ~BD_ENET_TX_STATS;
342
343         /* Set buffer length and buffer pointer.
344         */
345         bdp->cbd_bufaddr = __pa(skb->data);
346         bdp->cbd_datlen = skb->len;
347
348         /*
349          *      On some FEC implementations data must be aligned on
350          *      4-byte boundaries. Use bounce buffers to copy data
351          *      and get it aligned. Ugh.
352          */
353         if (bdp->cbd_bufaddr & 0x3) {
354                 unsigned int index;
355                 index = bdp - fep->tx_bd_base;
356                 memcpy(fep->tx_bounce[index], (void *) bdp->cbd_bufaddr, bdp->cbd_datlen);
357                 bdp->cbd_bufaddr = __pa(fep->tx_bounce[index]);
358         }
359
360         /* Save skb pointer.
361         */
362         fep->tx_skbuff[fep->skb_cur] = skb;
363
364         dev->stats.tx_bytes += skb->len;
365         fep->skb_cur = (fep->skb_cur+1) & TX_RING_MOD_MASK;
366
367         /* Push the data cache so the CPM does not get stale memory
368          * data.
369          */
370         flush_dcache_range((unsigned long)skb->data,
371                            (unsigned long)skb->data + skb->len);
372
373         /* Send it on its way.  Tell FEC it's ready, interrupt when done,
374          * it's the last BD of the frame, and to put the CRC on the end.
375          */
376
377         status |= (BD_ENET_TX_READY | BD_ENET_TX_INTR
378                         | BD_ENET_TX_LAST | BD_ENET_TX_TC);
379         bdp->cbd_sc = status;
380
381         dev->trans_start = jiffies;
382
383         /* Trigger transmission start */
384         fecp->fec_x_des_active = 0;
385
386         /* If this was the last BD in the ring, start at the beginning again.
387         */
388         if (status & BD_ENET_TX_WRAP) {
389                 bdp = fep->tx_bd_base;
390         } else {
391                 bdp++;
392         }
393
394         if (bdp == fep->dirty_tx) {
395                 fep->tx_full = 1;
396                 netif_stop_queue(dev);
397         }
398
399         fep->cur_tx = (cbd_t *)bdp;
400
401         spin_unlock_irqrestore(&fep->hw_lock, flags);
402
403         return 0;
404 }
405
406 static void
407 fec_timeout(struct net_device *dev)
408 {
409         struct fec_enet_private *fep = netdev_priv(dev);
410
411         printk("%s: transmit timed out.\n", dev->name);
412         dev->stats.tx_errors++;
413 #ifndef final_version
414         {
415         int     i;
416         cbd_t   *bdp;
417
418         printk("Ring data dump: cur_tx %lx%s, dirty_tx %lx cur_rx: %lx\n",
419                (unsigned long)fep->cur_tx, fep->tx_full ? " (full)" : "",
420                (unsigned long)fep->dirty_tx,
421                (unsigned long)fep->cur_rx);
422
423         bdp = fep->tx_bd_base;
424         printk(" tx: %u buffers\n",  TX_RING_SIZE);
425         for (i = 0 ; i < TX_RING_SIZE; i++) {
426                 printk("  %08x: %04x %04x %08x\n",
427                        (uint) bdp,
428                        bdp->cbd_sc,
429                        bdp->cbd_datlen,
430                        (int) bdp->cbd_bufaddr);
431                 bdp++;
432         }
433
434         bdp = fep->rx_bd_base;
435         printk(" rx: %lu buffers\n",  (unsigned long) RX_RING_SIZE);
436         for (i = 0 ; i < RX_RING_SIZE; i++) {
437                 printk("  %08x: %04x %04x %08x\n",
438                        (uint) bdp,
439                        bdp->cbd_sc,
440                        bdp->cbd_datlen,
441                        (int) bdp->cbd_bufaddr);
442                 bdp++;
443         }
444         }
445 #endif
446         fec_restart(dev, fep->full_duplex);
447         netif_wake_queue(dev);
448 }
449
450 /* The interrupt handler.
451  * This is called from the MPC core interrupt.
452  */
453 static irqreturn_t
454 fec_enet_interrupt(int irq, void * dev_id)
455 {
456         struct  net_device *dev = dev_id;
457         volatile fec_t  *fecp;
458         uint    int_events;
459         irqreturn_t ret = IRQ_NONE;
460
461         fecp = (volatile fec_t*)dev->base_addr;
462
463         /* Get the interrupt events that caused us to be here.
464         */
465         do {
466                 int_events = fecp->fec_ievent;
467                 fecp->fec_ievent = int_events;
468
469                 /* Handle receive event in its own function.
470                  */
471                 if (int_events & FEC_ENET_RXF) {
472                         ret = IRQ_HANDLED;
473                         fec_enet_rx(dev);
474                 }
475
476                 /* Transmit OK, or non-fatal error. Update the buffer
477                    descriptors. FEC handles all errors, we just discover
478                    them as part of the transmit process.
479                 */
480                 if (int_events & FEC_ENET_TXF) {
481                         ret = IRQ_HANDLED;
482                         fec_enet_tx(dev);
483                 }
484
485                 if (int_events & FEC_ENET_MII) {
486                         ret = IRQ_HANDLED;
487                         fec_enet_mii(dev);
488                 }
489
490         } while (int_events);
491
492         return ret;
493 }
494
495
496 static void
497 fec_enet_tx(struct net_device *dev)
498 {
499         struct  fec_enet_private *fep;
500         volatile cbd_t  *bdp;
501         unsigned short status;
502         struct  sk_buff *skb;
503
504         fep = netdev_priv(dev);
505         spin_lock_irq(&fep->hw_lock);
506         bdp = fep->dirty_tx;
507
508         while (((status = bdp->cbd_sc) & BD_ENET_TX_READY) == 0) {
509                 if (bdp == fep->cur_tx && fep->tx_full == 0) break;
510
511                 skb = fep->tx_skbuff[fep->skb_dirty];
512                 /* Check for errors. */
513                 if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC |
514                                    BD_ENET_TX_RL | BD_ENET_TX_UN |
515                                    BD_ENET_TX_CSL)) {
516                         dev->stats.tx_errors++;
517                         if (status & BD_ENET_TX_HB)  /* No heartbeat */
518                                 dev->stats.tx_heartbeat_errors++;
519                         if (status & BD_ENET_TX_LC)  /* Late collision */
520                                 dev->stats.tx_window_errors++;
521                         if (status & BD_ENET_TX_RL)  /* Retrans limit */
522                                 dev->stats.tx_aborted_errors++;
523                         if (status & BD_ENET_TX_UN)  /* Underrun */
524                                 dev->stats.tx_fifo_errors++;
525                         if (status & BD_ENET_TX_CSL) /* Carrier lost */
526                                 dev->stats.tx_carrier_errors++;
527                 } else {
528                         dev->stats.tx_packets++;
529                 }
530
531 #ifndef final_version
532                 if (status & BD_ENET_TX_READY)
533                         printk("HEY! Enet xmit interrupt and TX_READY.\n");
534 #endif
535                 /* Deferred means some collisions occurred during transmit,
536                  * but we eventually sent the packet OK.
537                  */
538                 if (status & BD_ENET_TX_DEF)
539                         dev->stats.collisions++;
540
541                 /* Free the sk buffer associated with this last transmit.
542                  */
543                 dev_kfree_skb_any(skb);
544                 fep->tx_skbuff[fep->skb_dirty] = NULL;
545                 fep->skb_dirty = (fep->skb_dirty + 1) & TX_RING_MOD_MASK;
546
547                 /* Update pointer to next buffer descriptor to be transmitted.
548                  */
549                 if (status & BD_ENET_TX_WRAP)
550                         bdp = fep->tx_bd_base;
551                 else
552                         bdp++;
553
554                 /* Since we have freed up a buffer, the ring is no longer
555                  * full.
556                  */
557                 if (fep->tx_full) {
558                         fep->tx_full = 0;
559                         if (netif_queue_stopped(dev))
560                                 netif_wake_queue(dev);
561                 }
562         }
563         fep->dirty_tx = (cbd_t *)bdp;
564         spin_unlock_irq(&fep->hw_lock);
565 }
566
567
568 /* During a receive, the cur_rx points to the current incoming buffer.
569  * When we update through the ring, if the next incoming buffer has
570  * not been given to the system, we just set the empty indicator,
571  * effectively tossing the packet.
572  */
573 static void
574 fec_enet_rx(struct net_device *dev)
575 {
576         struct  fec_enet_private *fep;
577         volatile fec_t  *fecp;
578         volatile cbd_t *bdp;
579         unsigned short status;
580         struct  sk_buff *skb;
581         ushort  pkt_len;
582         __u8 *data;
583
584 #ifdef CONFIG_M532x
585         flush_cache_all();
586 #endif
587
588         fep = netdev_priv(dev);
589         fecp = (volatile fec_t*)dev->base_addr;
590
591         spin_lock_irq(&fep->hw_lock);
592
593         /* First, grab all of the stats for the incoming packet.
594          * These get messed up if we get called due to a busy condition.
595          */
596         bdp = fep->cur_rx;
597
598 while (!((status = bdp->cbd_sc) & BD_ENET_RX_EMPTY)) {
599
600 #ifndef final_version
601         /* Since we have allocated space to hold a complete frame,
602          * the last indicator should be set.
603          */
604         if ((status & BD_ENET_RX_LAST) == 0)
605                 printk("FEC ENET: rcv is not +last\n");
606 #endif
607
608         if (!fep->opened)
609                 goto rx_processing_done;
610
611         /* Check for errors. */
612         if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
613                            BD_ENET_RX_CR | BD_ENET_RX_OV)) {
614                 dev->stats.rx_errors++;
615                 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH)) {
616                 /* Frame too long or too short. */
617                         dev->stats.rx_length_errors++;
618                 }
619                 if (status & BD_ENET_RX_NO)     /* Frame alignment */
620                         dev->stats.rx_frame_errors++;
621                 if (status & BD_ENET_RX_CR)     /* CRC Error */
622                         dev->stats.rx_crc_errors++;
623                 if (status & BD_ENET_RX_OV)     /* FIFO overrun */
624                         dev->stats.rx_fifo_errors++;
625         }
626
627         /* Report late collisions as a frame error.
628          * On this error, the BD is closed, but we don't know what we
629          * have in the buffer.  So, just drop this frame on the floor.
630          */
631         if (status & BD_ENET_RX_CL) {
632                 dev->stats.rx_errors++;
633                 dev->stats.rx_frame_errors++;
634                 goto rx_processing_done;
635         }
636
637         /* Process the incoming frame.
638          */
639         dev->stats.rx_packets++;
640         pkt_len = bdp->cbd_datlen;
641         dev->stats.rx_bytes += pkt_len;
642         data = (__u8*)__va(bdp->cbd_bufaddr);
643
644         /* This does 16 byte alignment, exactly what we need.
645          * The packet length includes FCS, but we don't want to
646          * include that when passing upstream as it messes up
647          * bridging applications.
648          */
649         skb = dev_alloc_skb(pkt_len-4);
650
651         if (skb == NULL) {
652                 printk("%s: Memory squeeze, dropping packet.\n", dev->name);
653                 dev->stats.rx_dropped++;
654         } else {
655                 skb_put(skb,pkt_len-4); /* Make room */
656                 skb_copy_to_linear_data(skb, data, pkt_len-4);
657                 skb->protocol=eth_type_trans(skb,dev);
658                 netif_rx(skb);
659         }
660   rx_processing_done:
661
662         /* Clear the status flags for this buffer.
663         */
664         status &= ~BD_ENET_RX_STATS;
665
666         /* Mark the buffer empty.
667         */
668         status |= BD_ENET_RX_EMPTY;
669         bdp->cbd_sc = status;
670
671         /* Update BD pointer to next entry.
672         */
673         if (status & BD_ENET_RX_WRAP)
674                 bdp = fep->rx_bd_base;
675         else
676                 bdp++;
677
678 #if 1
679         /* Doing this here will keep the FEC running while we process
680          * incoming frames.  On a heavily loaded network, we should be
681          * able to keep up at the expense of system resources.
682          */
683         fecp->fec_r_des_active = 0;
684 #endif
685    } /* while (!((status = bdp->cbd_sc) & BD_ENET_RX_EMPTY)) */
686         fep->cur_rx = (cbd_t *)bdp;
687
688 #if 0
689         /* Doing this here will allow us to process all frames in the
690          * ring before the FEC is allowed to put more there.  On a heavily
691          * loaded network, some frames may be lost.  Unfortunately, this
692          * increases the interrupt overhead since we can potentially work
693          * our way back to the interrupt return only to come right back
694          * here.
695          */
696         fecp->fec_r_des_active = 0;
697 #endif
698
699         spin_unlock_irq(&fep->hw_lock);
700 }
701
702
703 /* called from interrupt context */
704 static void
705 fec_enet_mii(struct net_device *dev)
706 {
707         struct  fec_enet_private *fep;
708         volatile fec_t  *ep;
709         mii_list_t      *mip;
710         uint            mii_reg;
711
712         fep = netdev_priv(dev);
713         spin_lock_irq(&fep->mii_lock);
714
715         ep = fep->hwp;
716         mii_reg = ep->fec_mii_data;
717
718         if ((mip = mii_head) == NULL) {
719                 printk("MII and no head!\n");
720                 goto unlock;
721         }
722
723         if (mip->mii_func != NULL)
724                 (*(mip->mii_func))(mii_reg, dev);
725
726         mii_head = mip->mii_next;
727         mip->mii_next = mii_free;
728         mii_free = mip;
729
730         if ((mip = mii_head) != NULL)
731                 ep->fec_mii_data = mip->mii_regval;
732
733 unlock:
734         spin_unlock_irq(&fep->mii_lock);
735 }
736
737 static int
738 mii_queue(struct net_device *dev, int regval, void (*func)(uint, struct net_device *))
739 {
740         struct fec_enet_private *fep;
741         unsigned long   flags;
742         mii_list_t      *mip;
743         int             retval;
744
745         /* Add PHY address to register command.
746         */
747         fep = netdev_priv(dev);
748         spin_lock_irqsave(&fep->mii_lock, flags);
749
750         regval |= fep->phy_addr << 23;
751         retval = 0;
752
753         if ((mip = mii_free) != NULL) {
754                 mii_free = mip->mii_next;
755                 mip->mii_regval = regval;
756                 mip->mii_func = func;
757                 mip->mii_next = NULL;
758                 if (mii_head) {
759                         mii_tail->mii_next = mip;
760                         mii_tail = mip;
761                 } else {
762                         mii_head = mii_tail = mip;
763                         fep->hwp->fec_mii_data = regval;
764                 }
765         } else {
766                 retval = 1;
767         }
768
769         spin_unlock_irqrestore(&fep->mii_lock, flags);
770         return retval;
771 }
772
773 static void mii_do_cmd(struct net_device *dev, const phy_cmd_t *c)
774 {
775         if(!c)
776                 return;
777
778         for (; c->mii_data != mk_mii_end; c++)
779                 mii_queue(dev, c->mii_data, c->funct);
780 }
781
782 static void mii_parse_sr(uint mii_reg, struct net_device *dev)
783 {
784         struct fec_enet_private *fep = netdev_priv(dev);
785         volatile uint *s = &(fep->phy_status);
786         uint status;
787
788         status = *s & ~(PHY_STAT_LINK | PHY_STAT_FAULT | PHY_STAT_ANC);
789
790         if (mii_reg & 0x0004)
791                 status |= PHY_STAT_LINK;
792         if (mii_reg & 0x0010)
793                 status |= PHY_STAT_FAULT;
794         if (mii_reg & 0x0020)
795                 status |= PHY_STAT_ANC;
796         *s = status;
797 }
798
799 static void mii_parse_cr(uint mii_reg, struct net_device *dev)
800 {
801         struct fec_enet_private *fep = netdev_priv(dev);
802         volatile uint *s = &(fep->phy_status);
803         uint status;
804
805         status = *s & ~(PHY_CONF_ANE | PHY_CONF_LOOP);
806
807         if (mii_reg & 0x1000)
808                 status |= PHY_CONF_ANE;
809         if (mii_reg & 0x4000)
810                 status |= PHY_CONF_LOOP;
811         *s = status;
812 }
813
814 static void mii_parse_anar(uint mii_reg, struct net_device *dev)
815 {
816         struct fec_enet_private *fep = netdev_priv(dev);
817         volatile uint *s = &(fep->phy_status);
818         uint status;
819
820         status = *s & ~(PHY_CONF_SPMASK);
821
822         if (mii_reg & 0x0020)
823                 status |= PHY_CONF_10HDX;
824         if (mii_reg & 0x0040)
825                 status |= PHY_CONF_10FDX;
826         if (mii_reg & 0x0080)
827                 status |= PHY_CONF_100HDX;
828         if (mii_reg & 0x00100)
829                 status |= PHY_CONF_100FDX;
830         *s = status;
831 }
832
833 /* ------------------------------------------------------------------------- */
834 /* The Level one LXT970 is used by many boards                               */
835
836 #define MII_LXT970_MIRROR    16  /* Mirror register           */
837 #define MII_LXT970_IER       17  /* Interrupt Enable Register */
838 #define MII_LXT970_ISR       18  /* Interrupt Status Register */
839 #define MII_LXT970_CONFIG    19  /* Configuration Register    */
840 #define MII_LXT970_CSR       20  /* Chip Status Register      */
841
842 static void mii_parse_lxt970_csr(uint mii_reg, struct net_device *dev)
843 {
844         struct fec_enet_private *fep = netdev_priv(dev);
845         volatile uint *s = &(fep->phy_status);
846         uint status;
847
848         status = *s & ~(PHY_STAT_SPMASK);
849         if (mii_reg & 0x0800) {
850                 if (mii_reg & 0x1000)
851                         status |= PHY_STAT_100FDX;
852                 else
853                         status |= PHY_STAT_100HDX;
854         } else {
855                 if (mii_reg & 0x1000)
856                         status |= PHY_STAT_10FDX;
857                 else
858                         status |= PHY_STAT_10HDX;
859         }
860         *s = status;
861 }
862
863 static phy_cmd_t const phy_cmd_lxt970_config[] = {
864                 { mk_mii_read(MII_REG_CR), mii_parse_cr },
865                 { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
866                 { mk_mii_end, }
867         };
868 static phy_cmd_t const phy_cmd_lxt970_startup[] = { /* enable interrupts */
869                 { mk_mii_write(MII_LXT970_IER, 0x0002), NULL },
870                 { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
871                 { mk_mii_end, }
872         };
873 static phy_cmd_t const phy_cmd_lxt970_ack_int[] = {
874                 /* read SR and ISR to acknowledge */
875                 { mk_mii_read(MII_REG_SR), mii_parse_sr },
876                 { mk_mii_read(MII_LXT970_ISR), NULL },
877
878                 /* find out the current status */
879                 { mk_mii_read(MII_LXT970_CSR), mii_parse_lxt970_csr },
880                 { mk_mii_end, }
881         };
882 static phy_cmd_t const phy_cmd_lxt970_shutdown[] = { /* disable interrupts */
883                 { mk_mii_write(MII_LXT970_IER, 0x0000), NULL },
884                 { mk_mii_end, }
885         };
886 static phy_info_t const phy_info_lxt970 = {
887         .id = 0x07810000,
888         .name = "LXT970",
889         .config = phy_cmd_lxt970_config,
890         .startup = phy_cmd_lxt970_startup,
891         .ack_int = phy_cmd_lxt970_ack_int,
892         .shutdown = phy_cmd_lxt970_shutdown
893 };
894
895 /* ------------------------------------------------------------------------- */
896 /* The Level one LXT971 is used on some of my custom boards                  */
897
898 /* register definitions for the 971 */
899
900 #define MII_LXT971_PCR       16  /* Port Control Register     */
901 #define MII_LXT971_SR2       17  /* Status Register 2         */
902 #define MII_LXT971_IER       18  /* Interrupt Enable Register */
903 #define MII_LXT971_ISR       19  /* Interrupt Status Register */
904 #define MII_LXT971_LCR       20  /* LED Control Register      */
905 #define MII_LXT971_TCR       30  /* Transmit Control Register */
906
907 /*
908  * I had some nice ideas of running the MDIO faster...
909  * The 971 should support 8MHz and I tried it, but things acted really
910  * weird, so 2.5 MHz ought to be enough for anyone...
911  */
912
913 static void mii_parse_lxt971_sr2(uint mii_reg, struct net_device *dev)
914 {
915         struct fec_enet_private *fep = netdev_priv(dev);
916         volatile uint *s = &(fep->phy_status);
917         uint status;
918
919         status = *s & ~(PHY_STAT_SPMASK | PHY_STAT_LINK | PHY_STAT_ANC);
920
921         if (mii_reg & 0x0400) {
922                 fep->link = 1;
923                 status |= PHY_STAT_LINK;
924         } else {
925                 fep->link = 0;
926         }
927         if (mii_reg & 0x0080)
928                 status |= PHY_STAT_ANC;
929         if (mii_reg & 0x4000) {
930                 if (mii_reg & 0x0200)
931                         status |= PHY_STAT_100FDX;
932                 else
933                         status |= PHY_STAT_100HDX;
934         } else {
935                 if (mii_reg & 0x0200)
936                         status |= PHY_STAT_10FDX;
937                 else
938                         status |= PHY_STAT_10HDX;
939         }
940         if (mii_reg & 0x0008)
941                 status |= PHY_STAT_FAULT;
942
943         *s = status;
944 }
945
946 static phy_cmd_t const phy_cmd_lxt971_config[] = {
947                 /* limit to 10MBit because my prototype board
948                  * doesn't work with 100. */
949                 { mk_mii_read(MII_REG_CR), mii_parse_cr },
950                 { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
951                 { mk_mii_read(MII_LXT971_SR2), mii_parse_lxt971_sr2 },
952                 { mk_mii_end, }
953         };
954 static phy_cmd_t const phy_cmd_lxt971_startup[] = {  /* enable interrupts */
955                 { mk_mii_write(MII_LXT971_IER, 0x00f2), NULL },
956                 { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
957                 { mk_mii_write(MII_LXT971_LCR, 0xd422), NULL }, /* LED config */
958                 /* Somehow does the 971 tell me that the link is down
959                  * the first read after power-up.
960                  * read here to get a valid value in ack_int */
961                 { mk_mii_read(MII_REG_SR), mii_parse_sr },
962                 { mk_mii_end, }
963         };
964 static phy_cmd_t const phy_cmd_lxt971_ack_int[] = {
965                 /* acknowledge the int before reading status ! */
966                 { mk_mii_read(MII_LXT971_ISR), NULL },
967                 /* find out the current status */
968                 { mk_mii_read(MII_REG_SR), mii_parse_sr },
969                 { mk_mii_read(MII_LXT971_SR2), mii_parse_lxt971_sr2 },
970                 { mk_mii_end, }
971         };
972 static phy_cmd_t const phy_cmd_lxt971_shutdown[] = { /* disable interrupts */
973                 { mk_mii_write(MII_LXT971_IER, 0x0000), NULL },
974                 { mk_mii_end, }
975         };
976 static phy_info_t const phy_info_lxt971 = {
977         .id = 0x0001378e,
978         .name = "LXT971",
979         .config = phy_cmd_lxt971_config,
980         .startup = phy_cmd_lxt971_startup,
981         .ack_int = phy_cmd_lxt971_ack_int,
982         .shutdown = phy_cmd_lxt971_shutdown
983 };
984
985 /* ------------------------------------------------------------------------- */
986 /* The Quality Semiconductor QS6612 is used on the RPX CLLF                  */
987
988 /* register definitions */
989
990 #define MII_QS6612_MCR       17  /* Mode Control Register      */
991 #define MII_QS6612_FTR       27  /* Factory Test Register      */
992 #define MII_QS6612_MCO       28  /* Misc. Control Register     */
993 #define MII_QS6612_ISR       29  /* Interrupt Source Register  */
994 #define MII_QS6612_IMR       30  /* Interrupt Mask Register    */
995 #define MII_QS6612_PCR       31  /* 100BaseTx PHY Control Reg. */
996
997 static void mii_parse_qs6612_pcr(uint mii_reg, struct net_device *dev)
998 {
999         struct fec_enet_private *fep = netdev_priv(dev);
1000         volatile uint *s = &(fep->phy_status);
1001         uint status;
1002
1003         status = *s & ~(PHY_STAT_SPMASK);
1004
1005         switch((mii_reg >> 2) & 7) {
1006         case 1: status |= PHY_STAT_10HDX; break;
1007         case 2: status |= PHY_STAT_100HDX; break;
1008         case 5: status |= PHY_STAT_10FDX; break;
1009         case 6: status |= PHY_STAT_100FDX; break;
1010 }
1011
1012         *s = status;
1013 }
1014
1015 static phy_cmd_t const phy_cmd_qs6612_config[] = {
1016                 /* The PHY powers up isolated on the RPX,
1017                  * so send a command to allow operation.
1018                  */
1019                 { mk_mii_write(MII_QS6612_PCR, 0x0dc0), NULL },
1020
1021                 /* parse cr and anar to get some info */
1022                 { mk_mii_read(MII_REG_CR), mii_parse_cr },
1023                 { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
1024                 { mk_mii_end, }
1025         };
1026 static phy_cmd_t const phy_cmd_qs6612_startup[] = {  /* enable interrupts */
1027                 { mk_mii_write(MII_QS6612_IMR, 0x003a), NULL },
1028                 { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
1029                 { mk_mii_end, }
1030         };
1031 static phy_cmd_t const phy_cmd_qs6612_ack_int[] = {
1032                 /* we need to read ISR, SR and ANER to acknowledge */
1033                 { mk_mii_read(MII_QS6612_ISR), NULL },
1034                 { mk_mii_read(MII_REG_SR), mii_parse_sr },
1035                 { mk_mii_read(MII_REG_ANER), NULL },
1036
1037                 /* read pcr to get info */
1038                 { mk_mii_read(MII_QS6612_PCR), mii_parse_qs6612_pcr },
1039                 { mk_mii_end, }
1040         };
1041 static phy_cmd_t const phy_cmd_qs6612_shutdown[] = { /* disable interrupts */
1042                 { mk_mii_write(MII_QS6612_IMR, 0x0000), NULL },
1043                 { mk_mii_end, }
1044         };
1045 static phy_info_t const phy_info_qs6612 = {
1046         .id = 0x00181440,
1047         .name = "QS6612",
1048         .config = phy_cmd_qs6612_config,
1049         .startup = phy_cmd_qs6612_startup,
1050         .ack_int = phy_cmd_qs6612_ack_int,
1051         .shutdown = phy_cmd_qs6612_shutdown
1052 };
1053
1054 /* ------------------------------------------------------------------------- */
1055 /* AMD AM79C874 phy                                                          */
1056
1057 /* register definitions for the 874 */
1058
1059 #define MII_AM79C874_MFR       16  /* Miscellaneous Feature Register */
1060 #define MII_AM79C874_ICSR      17  /* Interrupt/Status Register      */
1061 #define MII_AM79C874_DR        18  /* Diagnostic Register            */
1062 #define MII_AM79C874_PMLR      19  /* Power and Loopback Register    */
1063 #define MII_AM79C874_MCR       21  /* ModeControl Register           */
1064 #define MII_AM79C874_DC        23  /* Disconnect Counter             */
1065 #define MII_AM79C874_REC       24  /* Recieve Error Counter          */
1066
1067 static void mii_parse_am79c874_dr(uint mii_reg, struct net_device *dev)
1068 {
1069         struct fec_enet_private *fep = netdev_priv(dev);
1070         volatile uint *s = &(fep->phy_status);
1071         uint status;
1072
1073         status = *s & ~(PHY_STAT_SPMASK | PHY_STAT_ANC);
1074
1075         if (mii_reg & 0x0080)
1076                 status |= PHY_STAT_ANC;
1077         if (mii_reg & 0x0400)
1078                 status |= ((mii_reg & 0x0800) ? PHY_STAT_100FDX : PHY_STAT_100HDX);
1079         else
1080                 status |= ((mii_reg & 0x0800) ? PHY_STAT_10FDX : PHY_STAT_10HDX);
1081
1082         *s = status;
1083 }
1084
1085 static phy_cmd_t const phy_cmd_am79c874_config[] = {
1086                 { mk_mii_read(MII_REG_CR), mii_parse_cr },
1087                 { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
1088                 { mk_mii_read(MII_AM79C874_DR), mii_parse_am79c874_dr },
1089                 { mk_mii_end, }
1090         };
1091 static phy_cmd_t const phy_cmd_am79c874_startup[] = {  /* enable interrupts */
1092                 { mk_mii_write(MII_AM79C874_ICSR, 0xff00), NULL },
1093                 { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
1094                 { mk_mii_read(MII_REG_SR), mii_parse_sr },
1095                 { mk_mii_end, }
1096         };
1097 static phy_cmd_t const phy_cmd_am79c874_ack_int[] = {
1098                 /* find out the current status */
1099                 { mk_mii_read(MII_REG_SR), mii_parse_sr },
1100                 { mk_mii_read(MII_AM79C874_DR), mii_parse_am79c874_dr },
1101                 /* we only need to read ISR to acknowledge */
1102                 { mk_mii_read(MII_AM79C874_ICSR), NULL },
1103                 { mk_mii_end, }
1104         };
1105 static phy_cmd_t const phy_cmd_am79c874_shutdown[] = { /* disable interrupts */
1106                 { mk_mii_write(MII_AM79C874_ICSR, 0x0000), NULL },
1107                 { mk_mii_end, }
1108         };
1109 static phy_info_t const phy_info_am79c874 = {
1110         .id = 0x00022561,
1111         .name = "AM79C874",
1112         .config = phy_cmd_am79c874_config,
1113         .startup = phy_cmd_am79c874_startup,
1114         .ack_int = phy_cmd_am79c874_ack_int,
1115         .shutdown = phy_cmd_am79c874_shutdown
1116 };
1117
1118
1119 /* ------------------------------------------------------------------------- */
1120 /* Kendin KS8721BL phy                                                       */
1121
1122 /* register definitions for the 8721 */
1123
1124 #define MII_KS8721BL_RXERCR     21
1125 #define MII_KS8721BL_ICSR       22
1126 #define MII_KS8721BL_PHYCR      31
1127
1128 static phy_cmd_t const phy_cmd_ks8721bl_config[] = {
1129                 { mk_mii_read(MII_REG_CR), mii_parse_cr },
1130                 { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
1131                 { mk_mii_end, }
1132         };
1133 static phy_cmd_t const phy_cmd_ks8721bl_startup[] = {  /* enable interrupts */
1134                 { mk_mii_write(MII_KS8721BL_ICSR, 0xff00), NULL },
1135                 { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
1136                 { mk_mii_read(MII_REG_SR), mii_parse_sr },
1137                 { mk_mii_end, }
1138         };
1139 static phy_cmd_t const phy_cmd_ks8721bl_ack_int[] = {
1140                 /* find out the current status */
1141                 { mk_mii_read(MII_REG_SR), mii_parse_sr },
1142                 /* we only need to read ISR to acknowledge */
1143                 { mk_mii_read(MII_KS8721BL_ICSR), NULL },
1144                 { mk_mii_end, }
1145         };
1146 static phy_cmd_t const phy_cmd_ks8721bl_shutdown[] = { /* disable interrupts */
1147                 { mk_mii_write(MII_KS8721BL_ICSR, 0x0000), NULL },
1148                 { mk_mii_end, }
1149         };
1150 static phy_info_t const phy_info_ks8721bl = {
1151         .id = 0x00022161,
1152         .name = "KS8721BL",
1153         .config = phy_cmd_ks8721bl_config,
1154         .startup = phy_cmd_ks8721bl_startup,
1155         .ack_int = phy_cmd_ks8721bl_ack_int,
1156         .shutdown = phy_cmd_ks8721bl_shutdown
1157 };
1158
1159 /* ------------------------------------------------------------------------- */
1160 /* register definitions for the DP83848 */
1161
1162 #define MII_DP8384X_PHYSTST    16  /* PHY Status Register */
1163
1164 static void mii_parse_dp8384x_sr2(uint mii_reg, struct net_device *dev)
1165 {
1166         struct fec_enet_private *fep = dev->priv;
1167         volatile uint *s = &(fep->phy_status);
1168
1169         *s &= ~(PHY_STAT_SPMASK | PHY_STAT_LINK | PHY_STAT_ANC);
1170
1171         /* Link up */
1172         if (mii_reg & 0x0001) {
1173                 fep->link = 1;
1174                 *s |= PHY_STAT_LINK;
1175         } else
1176                 fep->link = 0;
1177         /* Status of link */
1178         if (mii_reg & 0x0010)   /* Autonegotioation complete */
1179                 *s |= PHY_STAT_ANC;
1180         if (mii_reg & 0x0002) {   /* 10MBps? */
1181                 if (mii_reg & 0x0004)   /* Full Duplex? */
1182                         *s |= PHY_STAT_10FDX;
1183                 else
1184                         *s |= PHY_STAT_10HDX;
1185         } else {                  /* 100 Mbps? */
1186                 if (mii_reg & 0x0004)   /* Full Duplex? */
1187                         *s |= PHY_STAT_100FDX;
1188                 else
1189                         *s |= PHY_STAT_100HDX;
1190         }
1191         if (mii_reg & 0x0008)
1192                 *s |= PHY_STAT_FAULT;
1193 }
1194
1195 static phy_info_t phy_info_dp83848= {
1196         0x020005c9,
1197         "DP83848",
1198
1199         (const phy_cmd_t []) {  /* config */
1200                 { mk_mii_read(MII_REG_CR), mii_parse_cr },
1201                 { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
1202                 { mk_mii_read(MII_DP8384X_PHYSTST), mii_parse_dp8384x_sr2 },
1203                 { mk_mii_end, }
1204         },
1205         (const phy_cmd_t []) {  /* startup - enable interrupts */
1206                 { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
1207                 { mk_mii_read(MII_REG_SR), mii_parse_sr },
1208                 { mk_mii_end, }
1209         },
1210         (const phy_cmd_t []) { /* ack_int - never happens, no interrupt */
1211                 { mk_mii_end, }
1212         },
1213         (const phy_cmd_t []) {  /* shutdown */
1214                 { mk_mii_end, }
1215         },
1216 };
1217
1218 /* ------------------------------------------------------------------------- */
1219
1220 static phy_info_t const * const phy_info[] = {
1221         &phy_info_lxt970,
1222         &phy_info_lxt971,
1223         &phy_info_qs6612,
1224         &phy_info_am79c874,
1225         &phy_info_ks8721bl,
1226         &phy_info_dp83848,
1227         NULL
1228 };
1229
1230 /* ------------------------------------------------------------------------- */
1231 #ifdef HAVE_mii_link_interrupt
1232 #ifdef CONFIG_RPXCLASSIC
1233 static void
1234 mii_link_interrupt(void *dev_id);
1235 #else
1236 static irqreturn_t
1237 mii_link_interrupt(int irq, void * dev_id);
1238 #endif
1239 #endif
1240
1241 #if defined(CONFIG_M5272)
1242 /*
1243  *      Code specific to Coldfire 5272 setup.
1244  */
1245 static void __inline__ fec_request_intrs(struct net_device *dev)
1246 {
1247         volatile unsigned long *icrp;
1248         static const struct idesc {
1249                 char *name;
1250                 unsigned short irq;
1251                 irq_handler_t handler;
1252         } *idp, id[] = {
1253                 { "fec(RX)", 86, fec_enet_interrupt },
1254                 { "fec(TX)", 87, fec_enet_interrupt },
1255                 { "fec(OTHER)", 88, fec_enet_interrupt },
1256                 { "fec(MII)", 66, mii_link_interrupt },
1257                 { NULL },
1258         };
1259
1260         /* Setup interrupt handlers. */
1261         for (idp = id; idp->name; idp++) {
1262                 if (request_irq(idp->irq, idp->handler, IRQF_DISABLED, idp->name, dev) != 0)
1263                         printk("FEC: Could not allocate %s IRQ(%d)!\n", idp->name, idp->irq);
1264         }
1265
1266         /* Unmask interrupt at ColdFire 5272 SIM */
1267         icrp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_ICR3);
1268         *icrp = 0x00000ddd;
1269         icrp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_ICR1);
1270         *icrp = 0x0d000000;
1271 }
1272
1273 static void __inline__ fec_set_mii(struct net_device *dev, struct fec_enet_private *fep)
1274 {
1275         volatile fec_t *fecp;
1276
1277         fecp = fep->hwp;
1278         fecp->fec_r_cntrl = OPT_FRAME_SIZE | 0x04;
1279         fecp->fec_x_cntrl = 0x00;
1280
1281         /*
1282          * Set MII speed to 2.5 MHz
1283          * See 5272 manual section 11.5.8: MSCR
1284          */
1285         fep->phy_speed = ((((MCF_CLK / 4) / (2500000 / 10)) + 5) / 10) * 2;
1286         fecp->fec_mii_speed = fep->phy_speed;
1287
1288         fec_restart(dev, 0);
1289 }
1290
1291 static void __inline__ fec_get_mac(struct net_device *dev)
1292 {
1293         struct fec_enet_private *fep = netdev_priv(dev);
1294         volatile fec_t *fecp;
1295         unsigned char *iap, tmpaddr[ETH_ALEN];
1296
1297         fecp = fep->hwp;
1298
1299         if (FEC_FLASHMAC) {
1300                 /*
1301                  * Get MAC address from FLASH.
1302                  * If it is all 1's or 0's, use the default.
1303                  */
1304                 iap = (unsigned char *)FEC_FLASHMAC;
1305                 if ((iap[0] == 0) && (iap[1] == 0) && (iap[2] == 0) &&
1306                     (iap[3] == 0) && (iap[4] == 0) && (iap[5] == 0))
1307                         iap = fec_mac_default;
1308                 if ((iap[0] == 0xff) && (iap[1] == 0xff) && (iap[2] == 0xff) &&
1309                     (iap[3] == 0xff) && (iap[4] == 0xff) && (iap[5] == 0xff))
1310                         iap = fec_mac_default;
1311         } else {
1312                 *((unsigned long *) &tmpaddr[0]) = fecp->fec_addr_low;
1313                 *((unsigned short *) &tmpaddr[4]) = (fecp->fec_addr_high >> 16);
1314                 iap = &tmpaddr[0];
1315         }
1316
1317         memcpy(dev->dev_addr, iap, ETH_ALEN);
1318
1319         /* Adjust MAC if using default MAC address */
1320         if (iap == fec_mac_default)
1321                  dev->dev_addr[ETH_ALEN-1] = fec_mac_default[ETH_ALEN-1] + fep->index;
1322 }
1323
1324 static void __inline__ fec_enable_phy_intr(void)
1325 {
1326 }
1327
1328 static void __inline__ fec_disable_phy_intr(void)
1329 {
1330         volatile unsigned long *icrp;
1331         icrp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_ICR1);
1332         *icrp = 0x08000000;
1333 }
1334
1335 static void __inline__ fec_phy_ack_intr(void)
1336 {
1337         volatile unsigned long *icrp;
1338         /* Acknowledge the interrupt */
1339         icrp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_ICR1);
1340         *icrp = 0x0d000000;
1341 }
1342
1343 static void __inline__ fec_localhw_setup(void)
1344 {
1345 }
1346
1347 /*
1348  *      Do not need to make region uncached on 5272.
1349  */
1350 static void __inline__ fec_uncache(unsigned long addr)
1351 {
1352 }
1353
1354 /* ------------------------------------------------------------------------- */
1355
1356 #elif defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x)
1357
1358 /*
1359  *      Code specific to Coldfire 5230/5231/5232/5234/5235,
1360  *      the 5270/5271/5274/5275 and 5280/5282 setups.
1361  */
1362 static void __inline__ fec_request_intrs(struct net_device *dev)
1363 {
1364         struct fec_enet_private *fep;
1365         int b;
1366         static const struct idesc {
1367                 char *name;
1368                 unsigned short irq;
1369         } *idp, id[] = {
1370                 { "fec(TXF)", 23 },
1371                 { "fec(RXF)", 27 },
1372                 { "fec(MII)", 29 },
1373                 { NULL },
1374         };
1375
1376         fep = netdev_priv(dev);
1377         b = (fep->index) ? 128 : 64;
1378
1379         /* Setup interrupt handlers. */
1380         for (idp = id; idp->name; idp++) {
1381                 if (request_irq(b+idp->irq, fec_enet_interrupt, IRQF_DISABLED, idp->name, dev) != 0)
1382                         printk("FEC: Could not allocate %s IRQ(%d)!\n", idp->name, b+idp->irq);
1383         }
1384
1385         /* Unmask interrupts at ColdFire 5280/5282 interrupt controller */
1386         {
1387                 volatile unsigned char  *icrp;
1388                 volatile unsigned long  *imrp;
1389                 int i, ilip;
1390
1391                 b = (fep->index) ? MCFICM_INTC1 : MCFICM_INTC0;
1392                 icrp = (volatile unsigned char *) (MCF_IPSBAR + b +
1393                         MCFINTC_ICR0);
1394                 for (i = 23, ilip = 0x28; (i < 36); i++)
1395                         icrp[i] = ilip--;
1396
1397                 imrp = (volatile unsigned long *) (MCF_IPSBAR + b +
1398                         MCFINTC_IMRH);
1399                 *imrp &= ~0x0000000f;
1400                 imrp = (volatile unsigned long *) (MCF_IPSBAR + b +
1401                         MCFINTC_IMRL);
1402                 *imrp &= ~0xff800001;
1403         }
1404
1405 #if defined(CONFIG_M528x)
1406         /* Set up gpio outputs for MII lines */
1407         {
1408                 volatile u16 *gpio_paspar;
1409                 volatile u8 *gpio_pehlpar;
1410
1411                 gpio_paspar = (volatile u16 *) (MCF_IPSBAR + 0x100056);
1412                 gpio_pehlpar = (volatile u16 *) (MCF_IPSBAR + 0x100058);
1413                 *gpio_paspar |= 0x0f00;
1414                 *gpio_pehlpar = 0xc0;
1415         }
1416 #endif
1417
1418 #if defined(CONFIG_M527x)
1419         /* Set up gpio outputs for MII lines */
1420         {
1421                 volatile u8 *gpio_par_fec;
1422                 volatile u16 *gpio_par_feci2c;
1423
1424                 gpio_par_feci2c = (volatile u16 *)(MCF_IPSBAR + 0x100082);
1425                 /* Set up gpio outputs for FEC0 MII lines */
1426                 gpio_par_fec = (volatile u8 *)(MCF_IPSBAR + 0x100078);
1427
1428                 *gpio_par_feci2c |= 0x0f00;
1429                 *gpio_par_fec |= 0xc0;
1430
1431 #if defined(CONFIG_FEC2)
1432                 /* Set up gpio outputs for FEC1 MII lines */
1433                 gpio_par_fec = (volatile u8 *)(MCF_IPSBAR + 0x100079);
1434
1435                 *gpio_par_feci2c |= 0x00a0;
1436                 *gpio_par_fec |= 0xc0;
1437 #endif /* CONFIG_FEC2 */
1438         }
1439 #endif /* CONFIG_M527x */
1440 }
1441
1442 static void __inline__ fec_set_mii(struct net_device *dev, struct fec_enet_private *fep)
1443 {
1444         volatile fec_t *fecp;
1445
1446         fecp = fep->hwp;
1447         fecp->fec_r_cntrl = OPT_FRAME_SIZE | 0x04;
1448         fecp->fec_x_cntrl = 0x00;
1449
1450         /*
1451          * Set MII speed to 2.5 MHz
1452          * See 5282 manual section 17.5.4.7: MSCR
1453          */
1454         fep->phy_speed = ((((MCF_CLK / 2) / (2500000 / 10)) + 5) / 10) * 2;
1455         fecp->fec_mii_speed = fep->phy_speed;
1456
1457         fec_restart(dev, 0);
1458 }
1459
1460 static void __inline__ fec_get_mac(struct net_device *dev)
1461 {
1462         struct fec_enet_private *fep = netdev_priv(dev);
1463         volatile fec_t *fecp;
1464         unsigned char *iap, tmpaddr[ETH_ALEN];
1465
1466         fecp = fep->hwp;
1467
1468         if (FEC_FLASHMAC) {
1469                 /*
1470                  * Get MAC address from FLASH.
1471                  * If it is all 1's or 0's, use the default.
1472                  */
1473                 iap = FEC_FLASHMAC;
1474                 if ((iap[0] == 0) && (iap[1] == 0) && (iap[2] == 0) &&
1475                     (iap[3] == 0) && (iap[4] == 0) && (iap[5] == 0))
1476                         iap = fec_mac_default;
1477                 if ((iap[0] == 0xff) && (iap[1] == 0xff) && (iap[2] == 0xff) &&
1478                     (iap[3] == 0xff) && (iap[4] == 0xff) && (iap[5] == 0xff))
1479                         iap = fec_mac_default;
1480         } else {
1481                 *((unsigned long *) &tmpaddr[0]) = fecp->fec_addr_low;
1482                 *((unsigned short *) &tmpaddr[4]) = (fecp->fec_addr_high >> 16);
1483                 iap = &tmpaddr[0];
1484         }
1485
1486         memcpy(dev->dev_addr, iap, ETH_ALEN);
1487
1488         /* Adjust MAC if using default MAC address */
1489         if (iap == fec_mac_default)
1490                 dev->dev_addr[ETH_ALEN-1] = fec_mac_default[ETH_ALEN-1] + fep->index;
1491 }
1492
1493 static void __inline__ fec_enable_phy_intr(void)
1494 {
1495 }
1496
1497 static void __inline__ fec_disable_phy_intr(void)
1498 {
1499 }
1500
1501 static void __inline__ fec_phy_ack_intr(void)
1502 {
1503 }
1504
1505 static void __inline__ fec_localhw_setup(void)
1506 {
1507 }
1508
1509 /*
1510  *      Do not need to make region uncached on 5272.
1511  */
1512 static void __inline__ fec_uncache(unsigned long addr)
1513 {
1514 }
1515
1516 /* ------------------------------------------------------------------------- */
1517
1518 #elif defined(CONFIG_M520x)
1519
1520 /*
1521  *      Code specific to Coldfire 520x
1522  */
1523 static void __inline__ fec_request_intrs(struct net_device *dev)
1524 {
1525         struct fec_enet_private *fep;
1526         int b;
1527         static const struct idesc {
1528                 char *name;
1529                 unsigned short irq;
1530         } *idp, id[] = {
1531                 { "fec(TXF)", 23 },
1532                 { "fec(RXF)", 27 },
1533                 { "fec(MII)", 29 },
1534                 { NULL },
1535         };
1536
1537         fep = netdev_priv(dev);
1538         b = 64 + 13;
1539
1540         /* Setup interrupt handlers. */
1541         for (idp = id; idp->name; idp++) {
1542                 if (request_irq(b+idp->irq, fec_enet_interrupt, IRQF_DISABLED, idp->name,dev) != 0)
1543                         printk("FEC: Could not allocate %s IRQ(%d)!\n", idp->name, b+idp->irq);
1544         }
1545
1546         /* Unmask interrupts at ColdFire interrupt controller */
1547         {
1548                 volatile unsigned char  *icrp;
1549                 volatile unsigned long  *imrp;
1550
1551                 icrp = (volatile unsigned char *) (MCF_IPSBAR + MCFICM_INTC0 +
1552                         MCFINTC_ICR0);
1553                 for (b = 36; (b < 49); b++)
1554                         icrp[b] = 0x04;
1555                 imrp = (volatile unsigned long *) (MCF_IPSBAR + MCFICM_INTC0 +
1556                         MCFINTC_IMRH);
1557                 *imrp &= ~0x0001FFF0;
1558         }
1559         *(volatile unsigned char *)(MCF_IPSBAR + MCF_GPIO_PAR_FEC) |= 0xf0;
1560         *(volatile unsigned char *)(MCF_IPSBAR + MCF_GPIO_PAR_FECI2C) |= 0x0f;
1561 }
1562
1563 static void __inline__ fec_set_mii(struct net_device *dev, struct fec_enet_private *fep)
1564 {
1565         volatile fec_t *fecp;
1566
1567         fecp = fep->hwp;
1568         fecp->fec_r_cntrl = OPT_FRAME_SIZE | 0x04;
1569         fecp->fec_x_cntrl = 0x00;
1570
1571         /*
1572          * Set MII speed to 2.5 MHz
1573          * See 5282 manual section 17.5.4.7: MSCR
1574          */
1575         fep->phy_speed = ((((MCF_CLK / 2) / (2500000 / 10)) + 5) / 10) * 2;
1576         fecp->fec_mii_speed = fep->phy_speed;
1577
1578         fec_restart(dev, 0);
1579 }
1580
1581 static void __inline__ fec_get_mac(struct net_device *dev)
1582 {
1583         struct fec_enet_private *fep = netdev_priv(dev);
1584         volatile fec_t *fecp;
1585         unsigned char *iap, tmpaddr[ETH_ALEN];
1586
1587         fecp = fep->hwp;
1588
1589         if (FEC_FLASHMAC) {
1590                 /*
1591                  * Get MAC address from FLASH.
1592                  * If it is all 1's or 0's, use the default.
1593                  */
1594                 iap = FEC_FLASHMAC;
1595                 if ((iap[0] == 0) && (iap[1] == 0) && (iap[2] == 0) &&
1596                    (iap[3] == 0) && (iap[4] == 0) && (iap[5] == 0))
1597                         iap = fec_mac_default;
1598                 if ((iap[0] == 0xff) && (iap[1] == 0xff) && (iap[2] == 0xff) &&
1599                    (iap[3] == 0xff) && (iap[4] == 0xff) && (iap[5] == 0xff))
1600                         iap = fec_mac_default;
1601         } else {
1602                 *((unsigned long *) &tmpaddr[0]) = fecp->fec_addr_low;
1603                 *((unsigned short *) &tmpaddr[4]) = (fecp->fec_addr_high >> 16);
1604                 iap = &tmpaddr[0];
1605         }
1606
1607         memcpy(dev->dev_addr, iap, ETH_ALEN);
1608
1609         /* Adjust MAC if using default MAC address */
1610         if (iap == fec_mac_default)
1611                 dev->dev_addr[ETH_ALEN-1] = fec_mac_default[ETH_ALEN-1] + fep->index;
1612 }
1613
1614 static void __inline__ fec_enable_phy_intr(void)
1615 {
1616 }
1617
1618 static void __inline__ fec_disable_phy_intr(void)
1619 {
1620 }
1621
1622 static void __inline__ fec_phy_ack_intr(void)
1623 {
1624 }
1625
1626 static void __inline__ fec_localhw_setup(void)
1627 {
1628 }
1629
1630 static void __inline__ fec_uncache(unsigned long addr)
1631 {
1632 }
1633
1634 /* ------------------------------------------------------------------------- */
1635
1636 #elif defined(CONFIG_M532x)
1637 /*
1638  * Code specific for M532x
1639  */
1640 static void __inline__ fec_request_intrs(struct net_device *dev)
1641 {
1642         struct fec_enet_private *fep;
1643         int b;
1644         static const struct idesc {
1645                 char *name;
1646                 unsigned short irq;
1647         } *idp, id[] = {
1648             { "fec(TXF)", 36 },
1649             { "fec(RXF)", 40 },
1650             { "fec(MII)", 42 },
1651             { NULL },
1652         };
1653
1654         fep = netdev_priv(dev);
1655         b = (fep->index) ? 128 : 64;
1656
1657         /* Setup interrupt handlers. */
1658         for (idp = id; idp->name; idp++) {
1659                 if (request_irq(b+idp->irq, fec_enet_interrupt, IRQF_DISABLED, idp->name,dev) != 0)
1660                         printk("FEC: Could not allocate %s IRQ(%d)!\n",
1661                                 idp->name, b+idp->irq);
1662         }
1663
1664         /* Unmask interrupts */
1665         MCF_INTC0_ICR36 = 0x2;
1666         MCF_INTC0_ICR37 = 0x2;
1667         MCF_INTC0_ICR38 = 0x2;
1668         MCF_INTC0_ICR39 = 0x2;
1669         MCF_INTC0_ICR40 = 0x2;
1670         MCF_INTC0_ICR41 = 0x2;
1671         MCF_INTC0_ICR42 = 0x2;
1672         MCF_INTC0_ICR43 = 0x2;
1673         MCF_INTC0_ICR44 = 0x2;
1674         MCF_INTC0_ICR45 = 0x2;
1675         MCF_INTC0_ICR46 = 0x2;
1676         MCF_INTC0_ICR47 = 0x2;
1677         MCF_INTC0_ICR48 = 0x2;
1678
1679         MCF_INTC0_IMRH &= ~(
1680                 MCF_INTC_IMRH_INT_MASK36 |
1681                 MCF_INTC_IMRH_INT_MASK37 |
1682                 MCF_INTC_IMRH_INT_MASK38 |
1683                 MCF_INTC_IMRH_INT_MASK39 |
1684                 MCF_INTC_IMRH_INT_MASK40 |
1685                 MCF_INTC_IMRH_INT_MASK41 |
1686                 MCF_INTC_IMRH_INT_MASK42 |
1687                 MCF_INTC_IMRH_INT_MASK43 |
1688                 MCF_INTC_IMRH_INT_MASK44 |
1689                 MCF_INTC_IMRH_INT_MASK45 |
1690                 MCF_INTC_IMRH_INT_MASK46 |
1691                 MCF_INTC_IMRH_INT_MASK47 |
1692                 MCF_INTC_IMRH_INT_MASK48 );
1693
1694         /* Set up gpio outputs for MII lines */
1695         MCF_GPIO_PAR_FECI2C |= (0 |
1696                 MCF_GPIO_PAR_FECI2C_PAR_MDC_EMDC |
1697                 MCF_GPIO_PAR_FECI2C_PAR_MDIO_EMDIO);
1698         MCF_GPIO_PAR_FEC = (0 |
1699                 MCF_GPIO_PAR_FEC_PAR_FEC_7W_FEC |
1700                 MCF_GPIO_PAR_FEC_PAR_FEC_MII_FEC);
1701 }
1702
1703 static void __inline__ fec_set_mii(struct net_device *dev, struct fec_enet_private *fep)
1704 {
1705         volatile fec_t *fecp;
1706
1707         fecp = fep->hwp;
1708         fecp->fec_r_cntrl = OPT_FRAME_SIZE | 0x04;
1709         fecp->fec_x_cntrl = 0x00;
1710
1711         /*
1712          * Set MII speed to 2.5 MHz
1713          */
1714         fep->phy_speed = ((((MCF_CLK / 2) / (2500000 / 10)) + 5) / 10) * 2;
1715         fecp->fec_mii_speed = fep->phy_speed;
1716
1717         fec_restart(dev, 0);
1718 }
1719
1720 static void __inline__ fec_get_mac(struct net_device *dev)
1721 {
1722         struct fec_enet_private *fep = netdev_priv(dev);
1723         volatile fec_t *fecp;
1724         unsigned char *iap, tmpaddr[ETH_ALEN];
1725
1726         fecp = fep->hwp;
1727
1728         if (FEC_FLASHMAC) {
1729                 /*
1730                  * Get MAC address from FLASH.
1731                  * If it is all 1's or 0's, use the default.
1732                  */
1733                 iap = FEC_FLASHMAC;
1734                 if ((iap[0] == 0) && (iap[1] == 0) && (iap[2] == 0) &&
1735                     (iap[3] == 0) && (iap[4] == 0) && (iap[5] == 0))
1736                         iap = fec_mac_default;
1737                 if ((iap[0] == 0xff) && (iap[1] == 0xff) && (iap[2] == 0xff) &&
1738                     (iap[3] == 0xff) && (iap[4] == 0xff) && (iap[5] == 0xff))
1739                         iap = fec_mac_default;
1740         } else {
1741                 *((unsigned long *) &tmpaddr[0]) = fecp->fec_addr_low;
1742                 *((unsigned short *) &tmpaddr[4]) = (fecp->fec_addr_high >> 16);
1743                 iap = &tmpaddr[0];
1744         }
1745
1746         memcpy(dev->dev_addr, iap, ETH_ALEN);
1747
1748         /* Adjust MAC if using default MAC address */
1749         if (iap == fec_mac_default)
1750                 dev->dev_addr[ETH_ALEN-1] = fec_mac_default[ETH_ALEN-1] + fep->index;
1751 }
1752
1753 static void __inline__ fec_enable_phy_intr(void)
1754 {
1755 }
1756
1757 static void __inline__ fec_disable_phy_intr(void)
1758 {
1759 }
1760
1761 static void __inline__ fec_phy_ack_intr(void)
1762 {
1763 }
1764
1765 static void __inline__ fec_localhw_setup(void)
1766 {
1767 }
1768
1769 /*
1770  *      Do not need to make region uncached on 532x.
1771  */
1772 static void __inline__ fec_uncache(unsigned long addr)
1773 {
1774 }
1775
1776 /* ------------------------------------------------------------------------- */
1777
1778
1779 #else
1780
1781 /*
1782  *      Code specific to the MPC860T setup.
1783  */
1784 static void __inline__ fec_request_intrs(struct net_device *dev)
1785 {
1786         volatile immap_t *immap;
1787
1788         immap = (immap_t *)IMAP_ADDR;   /* pointer to internal registers */
1789
1790         if (request_8xxirq(FEC_INTERRUPT, fec_enet_interrupt, 0, "fec", dev) != 0)
1791                 panic("Could not allocate FEC IRQ!");
1792
1793 #ifdef CONFIG_RPXCLASSIC
1794         /* Make Port C, bit 15 an input that causes interrupts.
1795         */
1796         immap->im_ioport.iop_pcpar &= ~0x0001;
1797         immap->im_ioport.iop_pcdir &= ~0x0001;
1798         immap->im_ioport.iop_pcso &= ~0x0001;
1799         immap->im_ioport.iop_pcint |= 0x0001;
1800         cpm_install_handler(CPMVEC_PIO_PC15, mii_link_interrupt, dev);
1801
1802         /* Make LEDS reflect Link status.
1803         */
1804         *((uint *) RPX_CSR_ADDR) &= ~BCSR2_FETHLEDMODE;
1805 #endif
1806 }
1807
1808 static void __inline__ fec_get_mac(struct net_device *dev)
1809 {
1810         bd_t *bd;
1811
1812         bd = (bd_t *)__res;
1813         memcpy(dev->dev_addr, bd->bi_enetaddr, ETH_ALEN);
1814
1815 #ifdef CONFIG_RPXCLASSIC
1816         /* The Embedded Planet boards have only one MAC address in
1817          * the EEPROM, but can have two Ethernet ports.  For the
1818          * FEC port, we create another address by setting one of
1819          * the address bits above something that would have (up to
1820          * now) been allocated.
1821          */
1822         dev->dev_adrd[3] |= 0x80;
1823 #endif
1824 }
1825
1826 static void __inline__ fec_set_mii(struct net_device *dev, struct fec_enet_private *fep)
1827 {
1828         extern uint _get_IMMR(void);
1829         volatile immap_t *immap;
1830         volatile fec_t *fecp;
1831
1832         fecp = fep->hwp;
1833         immap = (immap_t *)IMAP_ADDR;   /* pointer to internal registers */
1834
1835         /* Configure all of port D for MII.
1836         */
1837         immap->im_ioport.iop_pdpar = 0x1fff;
1838
1839         /* Bits moved from Rev. D onward.
1840         */
1841         if ((_get_IMMR() & 0xffff) < 0x0501)
1842                 immap->im_ioport.iop_pddir = 0x1c58;    /* Pre rev. D */
1843         else
1844                 immap->im_ioport.iop_pddir = 0x1fff;    /* Rev. D and later */
1845
1846         /* Set MII speed to 2.5 MHz
1847         */
1848         fecp->fec_mii_speed = fep->phy_speed =
1849                 ((bd->bi_busfreq * 1000000) / 2500000) & 0x7e;
1850 }
1851
1852 static void __inline__ fec_enable_phy_intr(void)
1853 {
1854         volatile fec_t *fecp;
1855
1856         fecp = fep->hwp;
1857
1858         /* Enable MII command finished interrupt
1859         */
1860         fecp->fec_ivec = (FEC_INTERRUPT/2) << 29;
1861 }
1862
1863 static void __inline__ fec_disable_phy_intr(void)
1864 {
1865 }
1866
1867 static void __inline__ fec_phy_ack_intr(void)
1868 {
1869 }
1870
1871 static void __inline__ fec_localhw_setup(void)
1872 {
1873         volatile fec_t *fecp;
1874
1875         fecp = fep->hwp;
1876         fecp->fec_r_hash = PKT_MAXBUF_SIZE;
1877         /* Enable big endian and don't care about SDMA FC.
1878         */
1879         fecp->fec_fun_code = 0x78000000;
1880 }
1881
1882 static void __inline__ fec_uncache(unsigned long addr)
1883 {
1884         pte_t *pte;
1885         pte = va_to_pte(mem_addr);
1886         pte_val(*pte) |= _PAGE_NO_CACHE;
1887         flush_tlb_page(init_mm.mmap, mem_addr);
1888 }
1889
1890 #endif
1891
1892 /* ------------------------------------------------------------------------- */
1893
1894 static void mii_display_status(struct net_device *dev)
1895 {
1896         struct fec_enet_private *fep = netdev_priv(dev);
1897         volatile uint *s = &(fep->phy_status);
1898
1899         if (!fep->link && !fep->old_link) {
1900                 /* Link is still down - don't print anything */
1901                 return;
1902         }
1903
1904         printk("%s: status: ", dev->name);
1905
1906         if (!fep->link) {
1907                 printk("link down");
1908         } else {
1909                 printk("link up");
1910
1911                 switch(*s & PHY_STAT_SPMASK) {
1912                 case PHY_STAT_100FDX: printk(", 100MBit Full Duplex"); break;
1913                 case PHY_STAT_100HDX: printk(", 100MBit Half Duplex"); break;
1914                 case PHY_STAT_10FDX: printk(", 10MBit Full Duplex"); break;
1915                 case PHY_STAT_10HDX: printk(", 10MBit Half Duplex"); break;
1916                 default:
1917                         printk(", Unknown speed/duplex");
1918                 }
1919
1920                 if (*s & PHY_STAT_ANC)
1921                         printk(", auto-negotiation complete");
1922         }
1923
1924         if (*s & PHY_STAT_FAULT)
1925                 printk(", remote fault");
1926
1927         printk(".\n");
1928 }
1929
1930 static void mii_display_config(struct work_struct *work)
1931 {
1932         struct fec_enet_private *fep = container_of(work, struct fec_enet_private, phy_task);
1933         struct net_device *dev = fep->netdev;
1934         uint status = fep->phy_status;
1935
1936         /*
1937         ** When we get here, phy_task is already removed from
1938         ** the workqueue.  It is thus safe to allow to reuse it.
1939         */
1940         fep->mii_phy_task_queued = 0;
1941         printk("%s: config: auto-negotiation ", dev->name);
1942
1943         if (status & PHY_CONF_ANE)
1944                 printk("on");
1945         else
1946                 printk("off");
1947
1948         if (status & PHY_CONF_100FDX)
1949                 printk(", 100FDX");
1950         if (status & PHY_CONF_100HDX)
1951                 printk(", 100HDX");
1952         if (status & PHY_CONF_10FDX)
1953                 printk(", 10FDX");
1954         if (status & PHY_CONF_10HDX)
1955                 printk(", 10HDX");
1956         if (!(status & PHY_CONF_SPMASK))
1957                 printk(", No speed/duplex selected?");
1958
1959         if (status & PHY_CONF_LOOP)
1960                 printk(", loopback enabled");
1961
1962         printk(".\n");
1963
1964         fep->sequence_done = 1;
1965 }
1966
1967 static void mii_relink(struct work_struct *work)
1968 {
1969         struct fec_enet_private *fep = container_of(work, struct fec_enet_private, phy_task);
1970         struct net_device *dev = fep->netdev;
1971         int duplex;
1972
1973         /*
1974         ** When we get here, phy_task is already removed from
1975         ** the workqueue.  It is thus safe to allow to reuse it.
1976         */
1977         fep->mii_phy_task_queued = 0;
1978         fep->link = (fep->phy_status & PHY_STAT_LINK) ? 1 : 0;
1979         mii_display_status(dev);
1980         fep->old_link = fep->link;
1981
1982         if (fep->link) {
1983                 duplex = 0;
1984                 if (fep->phy_status
1985                     & (PHY_STAT_100FDX | PHY_STAT_10FDX))
1986                         duplex = 1;
1987                 fec_restart(dev, duplex);
1988         } else
1989                 fec_stop(dev);
1990
1991 #if 0
1992         enable_irq(fep->mii_irq);
1993 #endif
1994
1995 }
1996
1997 /* mii_queue_relink is called in interrupt context from mii_link_interrupt */
1998 static void mii_queue_relink(uint mii_reg, struct net_device *dev)
1999 {
2000         struct fec_enet_private *fep = netdev_priv(dev);
2001
2002         /*
2003         ** We cannot queue phy_task twice in the workqueue.  It
2004         ** would cause an endless loop in the workqueue.
2005         ** Fortunately, if the last mii_relink entry has not yet been
2006         ** executed now, it will do the job for the current interrupt,
2007         ** which is just what we want.
2008         */
2009         if (fep->mii_phy_task_queued)
2010                 return;
2011
2012         fep->mii_phy_task_queued = 1;
2013         INIT_WORK(&fep->phy_task, mii_relink);
2014         schedule_work(&fep->phy_task);
2015 }
2016
2017 /* mii_queue_config is called in interrupt context from fec_enet_mii */
2018 static void mii_queue_config(uint mii_reg, struct net_device *dev)
2019 {
2020         struct fec_enet_private *fep = netdev_priv(dev);
2021
2022         if (fep->mii_phy_task_queued)
2023                 return;
2024
2025         fep->mii_phy_task_queued = 1;
2026         INIT_WORK(&fep->phy_task, mii_display_config);
2027         schedule_work(&fep->phy_task);
2028 }
2029
2030 phy_cmd_t const phy_cmd_relink[] = {
2031         { mk_mii_read(MII_REG_CR), mii_queue_relink },
2032         { mk_mii_end, }
2033         };
2034 phy_cmd_t const phy_cmd_config[] = {
2035         { mk_mii_read(MII_REG_CR), mii_queue_config },
2036         { mk_mii_end, }
2037         };
2038
2039 /* Read remainder of PHY ID.
2040 */
2041 static void
2042 mii_discover_phy3(uint mii_reg, struct net_device *dev)
2043 {
2044         struct fec_enet_private *fep;
2045         int i;
2046
2047         fep = netdev_priv(dev);
2048         fep->phy_id |= (mii_reg & 0xffff);
2049         printk("fec: PHY @ 0x%x, ID 0x%08x", fep->phy_addr, fep->phy_id);
2050
2051         for(i = 0; phy_info[i]; i++) {
2052                 if(phy_info[i]->id == (fep->phy_id >> 4))
2053                         break;
2054         }
2055
2056         if (phy_info[i])
2057                 printk(" -- %s\n", phy_info[i]->name);
2058         else
2059                 printk(" -- unknown PHY!\n");
2060
2061         fep->phy = phy_info[i];
2062         fep->phy_id_done = 1;
2063 }
2064
2065 /* Scan all of the MII PHY addresses looking for someone to respond
2066  * with a valid ID.  This usually happens quickly.
2067  */
2068 static void
2069 mii_discover_phy(uint mii_reg, struct net_device *dev)
2070 {
2071         struct fec_enet_private *fep;
2072         volatile fec_t *fecp;
2073         uint phytype;
2074
2075         fep = netdev_priv(dev);
2076         fecp = fep->hwp;
2077
2078         if (fep->phy_addr < 32) {
2079                 if ((phytype = (mii_reg & 0xffff)) != 0xffff && phytype != 0) {
2080
2081                         /* Got first part of ID, now get remainder.
2082                         */
2083                         fep->phy_id = phytype << 16;
2084                         mii_queue(dev, mk_mii_read(MII_REG_PHYIR2),
2085                                                         mii_discover_phy3);
2086                 } else {
2087                         fep->phy_addr++;
2088                         mii_queue(dev, mk_mii_read(MII_REG_PHYIR1),
2089                                                         mii_discover_phy);
2090                 }
2091         } else {
2092                 printk("FEC: No PHY device found.\n");
2093                 /* Disable external MII interface */
2094                 fecp->fec_mii_speed = fep->phy_speed = 0;
2095                 fec_disable_phy_intr();
2096         }
2097 }
2098
2099 /* This interrupt occurs when the PHY detects a link change.
2100 */
2101 #ifdef HAVE_mii_link_interrupt
2102 #ifdef CONFIG_RPXCLASSIC
2103 static void
2104 mii_link_interrupt(void *dev_id)
2105 #else
2106 static irqreturn_t
2107 mii_link_interrupt(int irq, void * dev_id)
2108 #endif
2109 {
2110         struct  net_device *dev = dev_id;
2111         struct fec_enet_private *fep = netdev_priv(dev);
2112
2113         fec_phy_ack_intr();
2114
2115 #if 0
2116         disable_irq(fep->mii_irq);  /* disable now, enable later */
2117 #endif
2118
2119         mii_do_cmd(dev, fep->phy->ack_int);
2120         mii_do_cmd(dev, phy_cmd_relink);  /* restart and display status */
2121
2122         return IRQ_HANDLED;
2123 }
2124 #endif
2125
2126 static int
2127 fec_enet_open(struct net_device *dev)
2128 {
2129         struct fec_enet_private *fep = netdev_priv(dev);
2130
2131         /* I should reset the ring buffers here, but I don't yet know
2132          * a simple way to do that.
2133          */
2134         fec_set_mac_address(dev);
2135
2136         fep->sequence_done = 0;
2137         fep->link = 0;
2138
2139         if (fep->phy) {
2140                 mii_do_cmd(dev, fep->phy->ack_int);
2141                 mii_do_cmd(dev, fep->phy->config);
2142                 mii_do_cmd(dev, phy_cmd_config);  /* display configuration */
2143
2144                 /* Poll until the PHY tells us its configuration
2145                  * (not link state).
2146                  * Request is initiated by mii_do_cmd above, but answer
2147                  * comes by interrupt.
2148                  * This should take about 25 usec per register at 2.5 MHz,
2149                  * and we read approximately 5 registers.
2150                  */
2151                 while(!fep->sequence_done)
2152                         schedule();
2153
2154                 mii_do_cmd(dev, fep->phy->startup);
2155
2156                 /* Set the initial link state to true. A lot of hardware
2157                  * based on this device does not implement a PHY interrupt,
2158                  * so we are never notified of link change.
2159                  */
2160                 fep->link = 1;
2161         } else {
2162                 fep->link = 1; /* lets just try it and see */
2163                 /* no phy,  go full duplex,  it's most likely a hub chip */
2164                 fec_restart(dev, 1);
2165         }
2166
2167         netif_start_queue(dev);
2168         fep->opened = 1;
2169         return 0;               /* Success */
2170 }
2171
2172 static int
2173 fec_enet_close(struct net_device *dev)
2174 {
2175         struct fec_enet_private *fep = netdev_priv(dev);
2176
2177         /* Don't know what to do yet.
2178         */
2179         fep->opened = 0;
2180         netif_stop_queue(dev);
2181         fec_stop(dev);
2182
2183         return 0;
2184 }
2185
2186 /* Set or clear the multicast filter for this adaptor.
2187  * Skeleton taken from sunlance driver.
2188  * The CPM Ethernet implementation allows Multicast as well as individual
2189  * MAC address filtering.  Some of the drivers check to make sure it is
2190  * a group multicast address, and discard those that are not.  I guess I
2191  * will do the same for now, but just remove the test if you want
2192  * individual filtering as well (do the upper net layers want or support
2193  * this kind of feature?).
2194  */
2195
2196 #define HASH_BITS       6               /* #bits in hash */
2197 #define CRC32_POLY      0xEDB88320
2198
2199 static void set_multicast_list(struct net_device *dev)
2200 {
2201         struct fec_enet_private *fep;
2202         volatile fec_t *ep;
2203         struct dev_mc_list *dmi;
2204         unsigned int i, j, bit, data, crc;
2205         unsigned char hash;
2206
2207         fep = netdev_priv(dev);
2208         ep = fep->hwp;
2209
2210         if (dev->flags&IFF_PROMISC) {
2211                 ep->fec_r_cntrl |= 0x0008;
2212         } else {
2213
2214                 ep->fec_r_cntrl &= ~0x0008;
2215
2216                 if (dev->flags & IFF_ALLMULTI) {
2217                         /* Catch all multicast addresses, so set the
2218                          * filter to all 1's.
2219                          */
2220                         ep->fec_grp_hash_table_high = 0xffffffff;
2221                         ep->fec_grp_hash_table_low = 0xffffffff;
2222                 } else {
2223                         /* Clear filter and add the addresses in hash register.
2224                         */
2225                         ep->fec_grp_hash_table_high = 0;
2226                         ep->fec_grp_hash_table_low = 0;
2227
2228                         dmi = dev->mc_list;
2229
2230                         for (j = 0; j < dev->mc_count; j++, dmi = dmi->next)
2231                         {
2232                                 /* Only support group multicast for now.
2233                                 */
2234                                 if (!(dmi->dmi_addr[0] & 1))
2235                                         continue;
2236
2237                                 /* calculate crc32 value of mac address
2238                                 */
2239                                 crc = 0xffffffff;
2240
2241                                 for (i = 0; i < dmi->dmi_addrlen; i++)
2242                                 {
2243                                         data = dmi->dmi_addr[i];
2244                                         for (bit = 0; bit < 8; bit++, data >>= 1)
2245                                         {
2246                                                 crc = (crc >> 1) ^
2247                                                 (((crc ^ data) & 1) ? CRC32_POLY : 0);
2248                                         }
2249                                 }
2250
2251                                 /* only upper 6 bits (HASH_BITS) are used
2252                                    which point to specific bit in he hash registers
2253                                 */
2254                                 hash = (crc >> (32 - HASH_BITS)) & 0x3f;
2255
2256                                 if (hash > 31)
2257                                         ep->fec_grp_hash_table_high |= 1 << (hash - 32);
2258                                 else
2259                                         ep->fec_grp_hash_table_low |= 1 << hash;
2260                         }
2261                 }
2262         }
2263 }
2264
2265 /* Set a MAC change in hardware.
2266  */
2267 static void
2268 fec_set_mac_address(struct net_device *dev)
2269 {
2270         volatile fec_t *fecp;
2271
2272         fecp = ((struct fec_enet_private *)netdev_priv(dev))->hwp;
2273
2274         /* Set station address. */
2275         fecp->fec_addr_low = dev->dev_addr[3] | (dev->dev_addr[2] << 8) |
2276                 (dev->dev_addr[1] << 16) | (dev->dev_addr[0] << 24);
2277         fecp->fec_addr_high = (dev->dev_addr[5] << 16) |
2278                 (dev->dev_addr[4] << 24);
2279
2280 }
2281
2282 /* Initialize the FEC Ethernet on 860T (or ColdFire 5272).
2283  */
2284  /*
2285   * XXX:  We need to clean up on failure exits here.
2286   */
2287 int __init fec_enet_init(struct net_device *dev)
2288 {
2289         struct fec_enet_private *fep = netdev_priv(dev);
2290         unsigned long   mem_addr;
2291         volatile cbd_t  *bdp;
2292         cbd_t           *cbd_base;
2293         volatile fec_t  *fecp;
2294         int             i, j;
2295         static int      index = 0;
2296
2297         /* Only allow us to be probed once. */
2298         if (index >= FEC_MAX_PORTS)
2299                 return -ENXIO;
2300
2301         /* Allocate memory for buffer descriptors.
2302         */
2303         mem_addr = __get_free_page(GFP_KERNEL);
2304         if (mem_addr == 0) {
2305                 printk("FEC: allocate descriptor memory failed?\n");
2306                 return -ENOMEM;
2307         }
2308
2309         spin_lock_init(&fep->hw_lock);
2310         spin_lock_init(&fep->mii_lock);
2311
2312         /* Create an Ethernet device instance.
2313         */
2314         fecp = (volatile fec_t *) fec_hw[index];
2315
2316         fep->index = index;
2317         fep->hwp = fecp;
2318         fep->netdev = dev;
2319
2320         /* Whack a reset.  We should wait for this.
2321         */
2322         fecp->fec_ecntrl = 1;
2323         udelay(10);
2324
2325         /* Set the Ethernet address.  If using multiple Enets on the 8xx,
2326          * this needs some work to get unique addresses.
2327          *
2328          * This is our default MAC address unless the user changes
2329          * it via eth_mac_addr (our dev->set_mac_addr handler).
2330          */
2331         fec_get_mac(dev);
2332
2333         cbd_base = (cbd_t *)mem_addr;
2334         /* XXX: missing check for allocation failure */
2335
2336         fec_uncache(mem_addr);
2337
2338         /* Set receive and transmit descriptor base.
2339         */
2340         fep->rx_bd_base = cbd_base;
2341         fep->tx_bd_base = cbd_base + RX_RING_SIZE;
2342
2343         fep->dirty_tx = fep->cur_tx = fep->tx_bd_base;
2344         fep->cur_rx = fep->rx_bd_base;
2345
2346         fep->skb_cur = fep->skb_dirty = 0;
2347
2348         /* Initialize the receive buffer descriptors.
2349         */
2350         bdp = fep->rx_bd_base;
2351         for (i=0; i<FEC_ENET_RX_PAGES; i++) {
2352
2353                 /* Allocate a page.
2354                 */
2355                 mem_addr = __get_free_page(GFP_KERNEL);
2356                 /* XXX: missing check for allocation failure */
2357
2358                 fec_uncache(mem_addr);
2359
2360                 /* Initialize the BD for every fragment in the page.
2361                 */
2362                 for (j=0; j<FEC_ENET_RX_FRPPG; j++) {
2363                         bdp->cbd_sc = BD_ENET_RX_EMPTY;
2364                         bdp->cbd_bufaddr = __pa(mem_addr);
2365                         mem_addr += FEC_ENET_RX_FRSIZE;
2366                         bdp++;
2367                 }
2368         }
2369
2370         /* Set the last buffer to wrap.
2371         */
2372         bdp--;
2373         bdp->cbd_sc |= BD_SC_WRAP;
2374
2375         /* ...and the same for transmmit.
2376         */
2377         bdp = fep->tx_bd_base;
2378         for (i=0, j=FEC_ENET_TX_FRPPG; i<TX_RING_SIZE; i++) {
2379                 if (j >= FEC_ENET_TX_FRPPG) {
2380                         mem_addr = __get_free_page(GFP_KERNEL);
2381                         j = 1;
2382                 } else {
2383                         mem_addr += FEC_ENET_TX_FRSIZE;
2384                         j++;
2385                 }
2386                 fep->tx_bounce[i] = (unsigned char *) mem_addr;
2387
2388                 /* Initialize the BD for every fragment in the page.
2389                 */
2390                 bdp->cbd_sc = 0;
2391                 bdp->cbd_bufaddr = 0;
2392                 bdp++;
2393         }
2394
2395         /* Set the last buffer to wrap.
2396         */
2397         bdp--;
2398         bdp->cbd_sc |= BD_SC_WRAP;
2399
2400         /* Set receive and transmit descriptor base.
2401         */
2402         fecp->fec_r_des_start = __pa((uint)(fep->rx_bd_base));
2403         fecp->fec_x_des_start = __pa((uint)(fep->tx_bd_base));
2404
2405         /* Install our interrupt handlers. This varies depending on
2406          * the architecture.
2407         */
2408         fec_request_intrs(dev);
2409
2410         fecp->fec_grp_hash_table_high = 0;
2411         fecp->fec_grp_hash_table_low = 0;
2412         fecp->fec_r_buff_size = PKT_MAXBLR_SIZE;
2413         fecp->fec_ecntrl = 2;
2414         fecp->fec_r_des_active = 0;
2415 #ifndef CONFIG_M5272
2416         fecp->fec_hash_table_high = 0;
2417         fecp->fec_hash_table_low = 0;
2418 #endif
2419
2420         dev->base_addr = (unsigned long)fecp;
2421
2422         /* The FEC Ethernet specific entries in the device structure. */
2423         dev->open = fec_enet_open;
2424         dev->hard_start_xmit = fec_enet_start_xmit;
2425         dev->tx_timeout = fec_timeout;
2426         dev->watchdog_timeo = TX_TIMEOUT;
2427         dev->stop = fec_enet_close;
2428         dev->set_multicast_list = set_multicast_list;
2429
2430         for (i=0; i<NMII-1; i++)
2431                 mii_cmds[i].mii_next = &mii_cmds[i+1];
2432         mii_free = mii_cmds;
2433
2434         /* setup MII interface */
2435         fec_set_mii(dev, fep);
2436
2437         /* Clear and enable interrupts */
2438         fecp->fec_ievent = 0xffc00000;
2439         fecp->fec_imask = (FEC_ENET_TXF | FEC_ENET_RXF | FEC_ENET_MII);
2440
2441         /* Queue up command to detect the PHY and initialize the
2442          * remainder of the interface.
2443          */
2444         fep->phy_id_done = 0;
2445         fep->phy_addr = 0;
2446         mii_queue(dev, mk_mii_read(MII_REG_PHYIR1), mii_discover_phy);
2447
2448         index++;
2449         return 0;
2450 }
2451
2452 /* This function is called to start or restart the FEC during a link
2453  * change.  This only happens when switching between half and full
2454  * duplex.
2455  */
2456 static void
2457 fec_restart(struct net_device *dev, int duplex)
2458 {
2459         struct fec_enet_private *fep;
2460         volatile cbd_t *bdp;
2461         volatile fec_t *fecp;
2462         int i;
2463
2464         fep = netdev_priv(dev);
2465         fecp = fep->hwp;
2466
2467         /* Whack a reset.  We should wait for this.
2468         */
2469         fecp->fec_ecntrl = 1;
2470         udelay(10);
2471
2472         /* Clear any outstanding interrupt.
2473         */
2474         fecp->fec_ievent = 0xffc00000;
2475         fec_enable_phy_intr();
2476
2477         /* Set station address.
2478         */
2479         fec_set_mac_address(dev);
2480
2481         /* Reset all multicast.
2482         */
2483         fecp->fec_grp_hash_table_high = 0;
2484         fecp->fec_grp_hash_table_low = 0;
2485
2486         /* Set maximum receive buffer size.
2487         */
2488         fecp->fec_r_buff_size = PKT_MAXBLR_SIZE;
2489
2490         fec_localhw_setup();
2491
2492         /* Set receive and transmit descriptor base.
2493         */
2494         fecp->fec_r_des_start = __pa((uint)(fep->rx_bd_base));
2495         fecp->fec_x_des_start = __pa((uint)(fep->tx_bd_base));
2496
2497         fep->dirty_tx = fep->cur_tx = fep->tx_bd_base;
2498         fep->cur_rx = fep->rx_bd_base;
2499
2500         /* Reset SKB transmit buffers.
2501         */
2502         fep->skb_cur = fep->skb_dirty = 0;
2503         for (i=0; i<=TX_RING_MOD_MASK; i++) {
2504                 if (fep->tx_skbuff[i] != NULL) {
2505                         dev_kfree_skb_any(fep->tx_skbuff[i]);
2506                         fep->tx_skbuff[i] = NULL;
2507                 }
2508         }
2509
2510         /* Initialize the receive buffer descriptors.
2511         */
2512         bdp = fep->rx_bd_base;
2513         for (i=0; i<RX_RING_SIZE; i++) {
2514
2515                 /* Initialize the BD for every fragment in the page.
2516                 */
2517                 bdp->cbd_sc = BD_ENET_RX_EMPTY;
2518                 bdp++;
2519         }
2520
2521         /* Set the last buffer to wrap.
2522         */
2523         bdp--;
2524         bdp->cbd_sc |= BD_SC_WRAP;
2525
2526         /* ...and the same for transmmit.
2527         */
2528         bdp = fep->tx_bd_base;
2529         for (i=0; i<TX_RING_SIZE; i++) {
2530
2531                 /* Initialize the BD for every fragment in the page.
2532                 */
2533                 bdp->cbd_sc = 0;
2534                 bdp->cbd_bufaddr = 0;
2535                 bdp++;
2536         }
2537
2538         /* Set the last buffer to wrap.
2539         */
2540         bdp--;
2541         bdp->cbd_sc |= BD_SC_WRAP;
2542
2543         /* Enable MII mode.
2544         */
2545         if (duplex) {
2546                 fecp->fec_r_cntrl = OPT_FRAME_SIZE | 0x04;/* MII enable */
2547                 fecp->fec_x_cntrl = 0x04;                 /* FD enable */
2548         } else {
2549                 /* MII enable|No Rcv on Xmit */
2550                 fecp->fec_r_cntrl = OPT_FRAME_SIZE | 0x06;
2551                 fecp->fec_x_cntrl = 0x00;
2552         }
2553         fep->full_duplex = duplex;
2554
2555         /* Set MII speed.
2556         */
2557         fecp->fec_mii_speed = fep->phy_speed;
2558
2559         /* And last, enable the transmit and receive processing.
2560         */
2561         fecp->fec_ecntrl = 2;
2562         fecp->fec_r_des_active = 0;
2563
2564         /* Enable interrupts we wish to service.
2565         */
2566         fecp->fec_imask = (FEC_ENET_TXF | FEC_ENET_RXF | FEC_ENET_MII);
2567 }
2568
2569 static void
2570 fec_stop(struct net_device *dev)
2571 {
2572         volatile fec_t *fecp;
2573         struct fec_enet_private *fep;
2574
2575         fep = netdev_priv(dev);
2576         fecp = fep->hwp;
2577
2578         /*
2579         ** We cannot expect a graceful transmit stop without link !!!
2580         */
2581         if (fep->link)
2582                 {
2583                 fecp->fec_x_cntrl = 0x01;       /* Graceful transmit stop */
2584                 udelay(10);
2585                 if (!(fecp->fec_ievent & FEC_ENET_GRA))
2586                         printk("fec_stop : Graceful transmit stop did not complete !\n");
2587                 }
2588
2589         /* Whack a reset.  We should wait for this.
2590         */
2591         fecp->fec_ecntrl = 1;
2592         udelay(10);
2593
2594         /* Clear outstanding MII command interrupts.
2595         */
2596         fecp->fec_ievent = FEC_ENET_MII;
2597         fec_enable_phy_intr();
2598
2599         fecp->fec_imask = FEC_ENET_MII;
2600         fecp->fec_mii_speed = fep->phy_speed;
2601 }
2602
2603 static int __init fec_enet_module_init(void)
2604 {
2605         struct net_device *dev;
2606         int i, err;
2607         DECLARE_MAC_BUF(mac);
2608
2609         printk("FEC ENET Version 0.2\n");
2610
2611         for (i = 0; (i < FEC_MAX_PORTS); i++) {
2612                 dev = alloc_etherdev(sizeof(struct fec_enet_private));
2613                 if (!dev)
2614                         return -ENOMEM;
2615                 err = fec_enet_init(dev);
2616                 if (err) {
2617                         free_netdev(dev);
2618                         continue;
2619                 }
2620                 if (register_netdev(dev) != 0) {
2621                         /* XXX: missing cleanup here */
2622                         free_netdev(dev);
2623                         return -EIO;
2624                 }
2625
2626                 printk("%s: ethernet %s\n",
2627                        dev->name, print_mac(mac, dev->dev_addr));
2628         }
2629         return 0;
2630 }
2631
2632 module_init(fec_enet_module_init);
2633
2634 MODULE_LICENSE("GPL");