Pull kmalloc into release branch
[pandora-kernel.git] / arch / ppc / 8xx_io / fec.c
1 /*
2  * Fast Ethernet Controller (FEC) driver for Motorola MPC8xx.
3  * Copyright (c) 1997 Dan Malek (dmalek@jlc.net)
4  *
5  * This version of the driver is specific to the FADS implementation,
6  * since the board contains control registers external to the processor
7  * for the control of the LevelOne LXT970 transceiver.  The MPC860T manual
8  * describes connections using the internal parallel port I/O, which
9  * is basically all of Port D.
10  *
11  * Includes support for the following PHYs: QS6612, LXT970, LXT971/2.
12  *
13  * Right now, I am very wasteful with the buffers.  I allocate memory
14  * pages and then divide them into 2K frame buffers.  This way I know I
15  * have buffers large enough to hold one frame within one buffer descriptor.
16  * Once I get this working, I will use 64 or 128 byte CPM buffers, which
17  * will be much more memory efficient and will easily handle lots of
18  * small packets.
19  *
20  * Much better multiple PHY support by Magnus Damm.
21  * Copyright (c) 2000 Ericsson Radio Systems AB.
22  *
23  * Make use of MII for PHY control configurable.
24  * Some fixes.
25  * Copyright (c) 2000-2002 Wolfgang Denk, DENX Software Engineering.
26  *
27  * Support for AMD AM79C874 added.
28  * Thomas Lange, thomas@corelatus.com
29  */
30
31 #include <linux/kernel.h>
32 #include <linux/sched.h>
33 #include <linux/string.h>
34 #include <linux/ptrace.h>
35 #include <linux/errno.h>
36 #include <linux/ioport.h>
37 #include <linux/slab.h>
38 #include <linux/interrupt.h>
39 #include <linux/pci.h>
40 #include <linux/init.h>
41 #include <linux/delay.h>
42 #include <linux/netdevice.h>
43 #include <linux/etherdevice.h>
44 #include <linux/skbuff.h>
45 #include <linux/spinlock.h>
46 #include <linux/bitops.h>
47 #ifdef CONFIG_FEC_PACKETHOOK
48 #include <linux/pkthook.h>
49 #endif
50
51 #include <asm/8xx_immap.h>
52 #include <asm/pgtable.h>
53 #include <asm/mpc8xx.h>
54 #include <asm/irq.h>
55 #include <asm/uaccess.h>
56 #include <asm/commproc.h>
57
58 #ifdef  CONFIG_USE_MDIO
59 /* Forward declarations of some structures to support different PHYs
60 */
61
62 typedef struct {
63         uint mii_data;
64         void (*funct)(uint mii_reg, struct net_device *dev);
65 } phy_cmd_t;
66
67 typedef struct {
68         uint id;
69         char *name;
70
71         const phy_cmd_t *config;
72         const phy_cmd_t *startup;
73         const phy_cmd_t *ack_int;
74         const phy_cmd_t *shutdown;
75 } phy_info_t;
76 #endif  /* CONFIG_USE_MDIO */
77
78 /* The number of Tx and Rx buffers.  These are allocated from the page
79  * pool.  The code may assume these are power of two, so it is best
80  * to keep them that size.
81  * We don't need to allocate pages for the transmitter.  We just use
82  * the skbuffer directly.
83  */
84 #ifdef CONFIG_ENET_BIG_BUFFERS
85 #define FEC_ENET_RX_PAGES       16
86 #define FEC_ENET_RX_FRSIZE      2048
87 #define FEC_ENET_RX_FRPPG       (PAGE_SIZE / FEC_ENET_RX_FRSIZE)
88 #define RX_RING_SIZE            (FEC_ENET_RX_FRPPG * FEC_ENET_RX_PAGES)
89 #define TX_RING_SIZE            16      /* Must be power of two */
90 #define TX_RING_MOD_MASK        15      /*   for this to work */
91 #else
92 #define FEC_ENET_RX_PAGES       4
93 #define FEC_ENET_RX_FRSIZE      2048
94 #define FEC_ENET_RX_FRPPG       (PAGE_SIZE / FEC_ENET_RX_FRSIZE)
95 #define RX_RING_SIZE            (FEC_ENET_RX_FRPPG * FEC_ENET_RX_PAGES)
96 #define TX_RING_SIZE            8       /* Must be power of two */
97 #define TX_RING_MOD_MASK        7       /*   for this to work */
98 #endif
99
100 /* Interrupt events/masks.
101 */
102 #define FEC_ENET_HBERR  ((uint)0x80000000)      /* Heartbeat error */
103 #define FEC_ENET_BABR   ((uint)0x40000000)      /* Babbling receiver */
104 #define FEC_ENET_BABT   ((uint)0x20000000)      /* Babbling transmitter */
105 #define FEC_ENET_GRA    ((uint)0x10000000)      /* Graceful stop complete */
106 #define FEC_ENET_TXF    ((uint)0x08000000)      /* Full frame transmitted */
107 #define FEC_ENET_TXB    ((uint)0x04000000)      /* A buffer was transmitted */
108 #define FEC_ENET_RXF    ((uint)0x02000000)      /* Full frame received */
109 #define FEC_ENET_RXB    ((uint)0x01000000)      /* A buffer was received */
110 #define FEC_ENET_MII    ((uint)0x00800000)      /* MII interrupt */
111 #define FEC_ENET_EBERR  ((uint)0x00400000)      /* SDMA bus error */
112
113 /*
114 */
115 #define FEC_ECNTRL_PINMUX       0x00000004
116 #define FEC_ECNTRL_ETHER_EN     0x00000002
117 #define FEC_ECNTRL_RESET        0x00000001
118
119 #define FEC_RCNTRL_BC_REJ       0x00000010
120 #define FEC_RCNTRL_PROM         0x00000008
121 #define FEC_RCNTRL_MII_MODE     0x00000004
122 #define FEC_RCNTRL_DRT          0x00000002
123 #define FEC_RCNTRL_LOOP         0x00000001
124
125 #define FEC_TCNTRL_FDEN         0x00000004
126 #define FEC_TCNTRL_HBC          0x00000002
127 #define FEC_TCNTRL_GTS          0x00000001
128
129 /* Delay to wait for FEC reset command to complete (in us)
130 */
131 #define FEC_RESET_DELAY         50
132
133 /* The FEC stores dest/src/type, data, and checksum for receive packets.
134  */
135 #define PKT_MAXBUF_SIZE         1518
136 #define PKT_MINBUF_SIZE         64
137 #define PKT_MAXBLR_SIZE         1520
138
139 /* The FEC buffer descriptors track the ring buffers.  The rx_bd_base and
140  * tx_bd_base always point to the base of the buffer descriptors.  The
141  * cur_rx and cur_tx point to the currently available buffer.
142  * The dirty_tx tracks the current buffer that is being sent by the
143  * controller.  The cur_tx and dirty_tx are equal under both completely
144  * empty and completely full conditions.  The empty/ready indicator in
145  * the buffer descriptor determines the actual condition.
146  */
147 struct fec_enet_private {
148         /* The saved address of a sent-in-place packet/buffer, for skfree(). */
149         struct  sk_buff* tx_skbuff[TX_RING_SIZE];
150         ushort  skb_cur;
151         ushort  skb_dirty;
152
153         /* CPM dual port RAM relative addresses.
154         */
155         cbd_t   *rx_bd_base;            /* Address of Rx and Tx buffers. */
156         cbd_t   *tx_bd_base;
157         cbd_t   *cur_rx, *cur_tx;               /* The next free ring entry */
158         cbd_t   *dirty_tx;      /* The ring entries to be free()ed. */
159
160         /* Virtual addresses for the receive buffers because we can't
161          * do a __va() on them anymore.
162          */
163         unsigned char *rx_vaddr[RX_RING_SIZE];
164
165         struct  net_device_stats stats;
166         uint    tx_full;
167         spinlock_t lock;
168
169 #ifdef  CONFIG_USE_MDIO
170         uint    phy_id;
171         uint    phy_id_done;
172         uint    phy_status;
173         uint    phy_speed;
174         phy_info_t      *phy;
175         struct work_struct phy_task;
176
177         uint    sequence_done;
178
179         uint    phy_addr;
180 #endif  /* CONFIG_USE_MDIO */
181
182         int     link;
183         int     old_link;
184         int     full_duplex;
185
186 #ifdef CONFIG_FEC_PACKETHOOK
187         unsigned long   ph_lock;
188         fec_ph_func     *ph_rxhandler;
189         fec_ph_func     *ph_txhandler;
190         __u16           ph_proto;
191         volatile __u32  *ph_regaddr;
192         void            *ph_priv;
193 #endif
194 };
195
196 static int fec_enet_open(struct net_device *dev);
197 static int fec_enet_start_xmit(struct sk_buff *skb, struct net_device *dev);
198 #ifdef  CONFIG_USE_MDIO
199 static void fec_enet_mii(struct net_device *dev);
200 #endif  /* CONFIG_USE_MDIO */
201 static irqreturn_t fec_enet_interrupt(int irq, void * dev_id,
202                                                         struct pt_regs * regs);
203 #ifdef CONFIG_FEC_PACKETHOOK
204 static void  fec_enet_tx(struct net_device *dev, __u32 regval);
205 static void  fec_enet_rx(struct net_device *dev, __u32 regval);
206 #else
207 static void  fec_enet_tx(struct net_device *dev);
208 static void  fec_enet_rx(struct net_device *dev);
209 #endif
210 static int fec_enet_close(struct net_device *dev);
211 static struct net_device_stats *fec_enet_get_stats(struct net_device *dev);
212 static void set_multicast_list(struct net_device *dev);
213 static void fec_restart(struct net_device *dev, int duplex);
214 static void fec_stop(struct net_device *dev);
215 static  ushort  my_enet_addr[3];
216
217 #ifdef  CONFIG_USE_MDIO
218 /* MII processing.  We keep this as simple as possible.  Requests are
219  * placed on the list (if there is room).  When the request is finished
220  * by the MII, an optional function may be called.
221  */
222 typedef struct mii_list {
223         uint    mii_regval;
224         void    (*mii_func)(uint val, struct net_device *dev);
225         struct  mii_list *mii_next;
226 } mii_list_t;
227
228 #define         NMII    20
229 mii_list_t      mii_cmds[NMII];
230 mii_list_t      *mii_free;
231 mii_list_t      *mii_head;
232 mii_list_t      *mii_tail;
233
234 static int      mii_queue(struct net_device *dev, int request,
235                                 void (*func)(uint, struct net_device *));
236
237 /* Make MII read/write commands for the FEC.
238 */
239 #define mk_mii_read(REG)        (0x60020000 | ((REG & 0x1f) << 18))
240 #define mk_mii_write(REG, VAL)  (0x50020000 | ((REG & 0x1f) << 18) | \
241                                                 (VAL & 0xffff))
242 #define mk_mii_end      0
243 #endif  /* CONFIG_USE_MDIO */
244
245 /* Transmitter timeout.
246 */
247 #define TX_TIMEOUT (2*HZ)
248
249 #ifdef  CONFIG_USE_MDIO
250 /* Register definitions for the PHY.
251 */
252
253 #define MII_REG_CR          0  /* Control Register                         */
254 #define MII_REG_SR          1  /* Status Register                          */
255 #define MII_REG_PHYIR1      2  /* PHY Identification Register 1            */
256 #define MII_REG_PHYIR2      3  /* PHY Identification Register 2            */
257 #define MII_REG_ANAR        4  /* A-N Advertisement Register               */
258 #define MII_REG_ANLPAR      5  /* A-N Link Partner Ability Register        */
259 #define MII_REG_ANER        6  /* A-N Expansion Register                   */
260 #define MII_REG_ANNPTR      7  /* A-N Next Page Transmit Register          */
261 #define MII_REG_ANLPRNPR    8  /* A-N Link Partner Received Next Page Reg. */
262
263 /* values for phy_status */
264
265 #define PHY_CONF_ANE    0x0001  /* 1 auto-negotiation enabled */
266 #define PHY_CONF_LOOP   0x0002  /* 1 loopback mode enabled */
267 #define PHY_CONF_SPMASK 0x00f0  /* mask for speed */
268 #define PHY_CONF_10HDX  0x0010  /* 10 Mbit half duplex supported */
269 #define PHY_CONF_10FDX  0x0020  /* 10 Mbit full duplex supported */
270 #define PHY_CONF_100HDX 0x0040  /* 100 Mbit half duplex supported */
271 #define PHY_CONF_100FDX 0x0080  /* 100 Mbit full duplex supported */
272
273 #define PHY_STAT_LINK   0x0100  /* 1 up - 0 down */
274 #define PHY_STAT_FAULT  0x0200  /* 1 remote fault */
275 #define PHY_STAT_ANC    0x0400  /* 1 auto-negotiation complete  */
276 #define PHY_STAT_SPMASK 0xf000  /* mask for speed */
277 #define PHY_STAT_10HDX  0x1000  /* 10 Mbit half duplex selected */
278 #define PHY_STAT_10FDX  0x2000  /* 10 Mbit full duplex selected */
279 #define PHY_STAT_100HDX 0x4000  /* 100 Mbit half duplex selected */
280 #define PHY_STAT_100FDX 0x8000  /* 100 Mbit full duplex selected */
281 #endif  /* CONFIG_USE_MDIO */
282
283 #ifdef CONFIG_FEC_PACKETHOOK
284 int
285 fec_register_ph(struct net_device *dev, fec_ph_func *rxfun, fec_ph_func *txfun,
286                 __u16 proto, volatile __u32 *regaddr, void *priv)
287 {
288         struct fec_enet_private *fep;
289         int retval = 0;
290
291         fep = dev->priv;
292
293         if (test_and_set_bit(0, (void*)&fep->ph_lock) != 0) {
294                 /* Someone is messing with the packet hook */
295                 return -EAGAIN;
296         }
297         if (fep->ph_rxhandler != NULL || fep->ph_txhandler != NULL) {
298                 retval = -EBUSY;
299                 goto out;
300         }
301         fep->ph_rxhandler = rxfun;
302         fep->ph_txhandler = txfun;
303         fep->ph_proto = proto;
304         fep->ph_regaddr = regaddr;
305         fep->ph_priv = priv;
306
307         out:
308         fep->ph_lock = 0;
309
310         return retval;
311 }
312
313
314 int
315 fec_unregister_ph(struct net_device *dev)
316 {
317         struct fec_enet_private *fep;
318         int retval = 0;
319
320         fep = dev->priv;
321
322         if (test_and_set_bit(0, (void*)&fep->ph_lock) != 0) {
323                 /* Someone is messing with the packet hook */
324                 return -EAGAIN;
325         }
326
327         fep->ph_rxhandler = fep->ph_txhandler = NULL;
328         fep->ph_proto = 0;
329         fep->ph_regaddr = NULL;
330         fep->ph_priv = NULL;
331
332         fep->ph_lock = 0;
333
334         return retval;
335 }
336
337 EXPORT_SYMBOL(fec_register_ph);
338 EXPORT_SYMBOL(fec_unregister_ph);
339
340 #endif /* CONFIG_FEC_PACKETHOOK */
341
342 static int
343 fec_enet_start_xmit(struct sk_buff *skb, struct net_device *dev)
344 {
345         struct fec_enet_private *fep;
346         volatile fec_t  *fecp;
347         volatile cbd_t  *bdp;
348
349         fep = dev->priv;
350         fecp = (volatile fec_t*)dev->base_addr;
351
352         if (!fep->link) {
353                 /* Link is down or autonegotiation is in progress. */
354                 return 1;
355         }
356
357         /* Fill in a Tx ring entry */
358         bdp = fep->cur_tx;
359
360 #ifndef final_version
361         if (bdp->cbd_sc & BD_ENET_TX_READY) {
362                 /* Ooops.  All transmit buffers are full.  Bail out.
363                  * This should not happen, since dev->tbusy should be set.
364                  */
365                 printk("%s: tx queue full!.\n", dev->name);
366                 return 1;
367         }
368 #endif
369
370         /* Clear all of the status flags.
371          */
372         bdp->cbd_sc &= ~BD_ENET_TX_STATS;
373
374         /* Set buffer length and buffer pointer.
375         */
376         bdp->cbd_bufaddr = __pa(skb->data);
377         bdp->cbd_datlen = skb->len;
378
379         /* Save skb pointer.
380         */
381         fep->tx_skbuff[fep->skb_cur] = skb;
382
383         fep->stats.tx_bytes += skb->len;
384         fep->skb_cur = (fep->skb_cur+1) & TX_RING_MOD_MASK;
385
386         /* Push the data cache so the CPM does not get stale memory
387          * data.
388          */
389         flush_dcache_range((unsigned long)skb->data,
390                            (unsigned long)skb->data + skb->len);
391
392         /* disable interrupts while triggering transmit */
393         spin_lock_irq(&fep->lock);
394
395         /* Send it on its way.  Tell FEC its ready, interrupt when done,
396          * its the last BD of the frame, and to put the CRC on the end.
397          */
398
399         bdp->cbd_sc |= (BD_ENET_TX_READY | BD_ENET_TX_INTR
400                         | BD_ENET_TX_LAST | BD_ENET_TX_TC);
401
402         dev->trans_start = jiffies;
403
404         /* Trigger transmission start */
405         fecp->fec_x_des_active = 0x01000000;
406
407         /* If this was the last BD in the ring, start at the beginning again.
408         */
409         if (bdp->cbd_sc & BD_ENET_TX_WRAP) {
410                 bdp = fep->tx_bd_base;
411         } else {
412                 bdp++;
413         }
414
415         if (bdp->cbd_sc & BD_ENET_TX_READY) {
416                 netif_stop_queue(dev);
417                 fep->tx_full = 1;
418         }
419
420         fep->cur_tx = (cbd_t *)bdp;
421
422         spin_unlock_irq(&fep->lock);
423
424         return 0;
425 }
426
427 static void
428 fec_timeout(struct net_device *dev)
429 {
430         struct fec_enet_private *fep = dev->priv;
431
432         printk("%s: transmit timed out.\n", dev->name);
433         fep->stats.tx_errors++;
434 #ifndef final_version
435         {
436         int     i;
437         cbd_t   *bdp;
438
439         printk("Ring data dump: cur_tx %lx%s, dirty_tx %lx cur_rx: %lx\n",
440                (unsigned long)fep->cur_tx, fep->tx_full ? " (full)" : "",
441                (unsigned long)fep->dirty_tx,
442                (unsigned long)fep->cur_rx);
443
444         bdp = fep->tx_bd_base;
445         printk(" tx: %u buffers\n",  TX_RING_SIZE);
446         for (i = 0 ; i < TX_RING_SIZE; i++) {
447                 printk("  %08x: %04x %04x %08x\n",
448                        (uint) bdp,
449                        bdp->cbd_sc,
450                        bdp->cbd_datlen,
451                        bdp->cbd_bufaddr);
452                 bdp++;
453         }
454
455         bdp = fep->rx_bd_base;
456         printk(" rx: %lu buffers\n",  RX_RING_SIZE);
457         for (i = 0 ; i < RX_RING_SIZE; i++) {
458                 printk("  %08x: %04x %04x %08x\n",
459                        (uint) bdp,
460                        bdp->cbd_sc,
461                        bdp->cbd_datlen,
462                        bdp->cbd_bufaddr);
463                 bdp++;
464         }
465         }
466 #endif
467         if (!fep->tx_full)
468                 netif_wake_queue(dev);
469 }
470
471 /* The interrupt handler.
472  * This is called from the MPC core interrupt.
473  */
474 static  irqreturn_t
475 fec_enet_interrupt(int irq, void * dev_id, struct pt_regs * regs)
476 {
477         struct  net_device *dev = dev_id;
478         volatile fec_t  *fecp;
479         uint    int_events;
480 #ifdef CONFIG_FEC_PACKETHOOK
481         struct  fec_enet_private *fep = dev->priv;
482         __u32 regval;
483
484         if (fep->ph_regaddr) regval = *fep->ph_regaddr;
485 #endif
486         fecp = (volatile fec_t*)dev->base_addr;
487
488         /* Get the interrupt events that caused us to be here.
489         */
490         while ((int_events = fecp->fec_ievent) != 0) {
491                 fecp->fec_ievent = int_events;
492                 if ((int_events & (FEC_ENET_HBERR | FEC_ENET_BABR |
493                                    FEC_ENET_BABT | FEC_ENET_EBERR)) != 0) {
494                         printk("FEC ERROR %x\n", int_events);
495                 }
496
497                 /* Handle receive event in its own function.
498                  */
499                 if (int_events & FEC_ENET_RXF) {
500 #ifdef CONFIG_FEC_PACKETHOOK
501                         fec_enet_rx(dev, regval);
502 #else
503                         fec_enet_rx(dev);
504 #endif
505                 }
506
507                 /* Transmit OK, or non-fatal error. Update the buffer
508                    descriptors. FEC handles all errors, we just discover
509                    them as part of the transmit process.
510                 */
511                 if (int_events & FEC_ENET_TXF) {
512 #ifdef CONFIG_FEC_PACKETHOOK
513                         fec_enet_tx(dev, regval);
514 #else
515                         fec_enet_tx(dev);
516 #endif
517                 }
518
519                 if (int_events & FEC_ENET_MII) {
520 #ifdef  CONFIG_USE_MDIO
521                         fec_enet_mii(dev);
522 #else
523 printk("%s[%d] %s: unexpected FEC_ENET_MII event\n", __FILE__,__LINE__,__FUNCTION__);
524 #endif  /* CONFIG_USE_MDIO */
525                 }
526
527         }
528         return IRQ_RETVAL(IRQ_HANDLED);
529 }
530
531
532 static void
533 #ifdef CONFIG_FEC_PACKETHOOK
534 fec_enet_tx(struct net_device *dev, __u32 regval)
535 #else
536 fec_enet_tx(struct net_device *dev)
537 #endif
538 {
539         struct  fec_enet_private *fep;
540         volatile cbd_t  *bdp;
541         struct  sk_buff *skb;
542
543         fep = dev->priv;
544         /* lock while transmitting */
545         spin_lock(&fep->lock);
546         bdp = fep->dirty_tx;
547
548         while ((bdp->cbd_sc&BD_ENET_TX_READY) == 0) {
549                 if (bdp == fep->cur_tx && fep->tx_full == 0) break;
550
551                 skb = fep->tx_skbuff[fep->skb_dirty];
552                 /* Check for errors. */
553                 if (bdp->cbd_sc & (BD_ENET_TX_HB | BD_ENET_TX_LC |
554                                    BD_ENET_TX_RL | BD_ENET_TX_UN |
555                                    BD_ENET_TX_CSL)) {
556                         fep->stats.tx_errors++;
557                         if (bdp->cbd_sc & BD_ENET_TX_HB)  /* No heartbeat */
558                                 fep->stats.tx_heartbeat_errors++;
559                         if (bdp->cbd_sc & BD_ENET_TX_LC)  /* Late collision */
560                                 fep->stats.tx_window_errors++;
561                         if (bdp->cbd_sc & BD_ENET_TX_RL)  /* Retrans limit */
562                                 fep->stats.tx_aborted_errors++;
563                         if (bdp->cbd_sc & BD_ENET_TX_UN)  /* Underrun */
564                                 fep->stats.tx_fifo_errors++;
565                         if (bdp->cbd_sc & BD_ENET_TX_CSL) /* Carrier lost */
566                                 fep->stats.tx_carrier_errors++;
567                 } else {
568 #ifdef CONFIG_FEC_PACKETHOOK
569                         /* Packet hook ... */
570                         if (fep->ph_txhandler &&
571                             ((struct ethhdr *)skb->data)->h_proto
572                             == fep->ph_proto) {
573                                 fep->ph_txhandler((__u8*)skb->data, skb->len,
574                                                   regval, fep->ph_priv);
575                         }
576 #endif
577                         fep->stats.tx_packets++;
578                 }
579
580 #ifndef final_version
581                 if (bdp->cbd_sc & BD_ENET_TX_READY)
582                         printk("HEY! Enet xmit interrupt and TX_READY.\n");
583 #endif
584                 /* Deferred means some collisions occurred during transmit,
585                  * but we eventually sent the packet OK.
586                  */
587                 if (bdp->cbd_sc & BD_ENET_TX_DEF)
588                         fep->stats.collisions++;
589
590                 /* Free the sk buffer associated with this last transmit.
591                  */
592 #if 0
593 printk("TXI: %x %x %x\n", bdp, skb, fep->skb_dirty);
594 #endif
595                 dev_kfree_skb_irq (skb/*, FREE_WRITE*/);
596                 fep->tx_skbuff[fep->skb_dirty] = NULL;
597                 fep->skb_dirty = (fep->skb_dirty + 1) & TX_RING_MOD_MASK;
598
599                 /* Update pointer to next buffer descriptor to be transmitted.
600                  */
601                 if (bdp->cbd_sc & BD_ENET_TX_WRAP)
602                         bdp = fep->tx_bd_base;
603                 else
604                         bdp++;
605
606                 /* Since we have freed up a buffer, the ring is no longer
607                  * full.
608                  */
609                 if (fep->tx_full) {
610                         fep->tx_full = 0;
611                         if (netif_queue_stopped(dev))
612                                 netif_wake_queue(dev);
613                 }
614 #ifdef CONFIG_FEC_PACKETHOOK
615                 /* Re-read register. Not exactly guaranteed to be correct,
616                    but... */
617                 if (fep->ph_regaddr) regval = *fep->ph_regaddr;
618 #endif
619         }
620         fep->dirty_tx = (cbd_t *)bdp;
621         spin_unlock(&fep->lock);
622 }
623
624
625 /* During a receive, the cur_rx points to the current incoming buffer.
626  * When we update through the ring, if the next incoming buffer has
627  * not been given to the system, we just set the empty indicator,
628  * effectively tossing the packet.
629  */
630 static void
631 #ifdef CONFIG_FEC_PACKETHOOK
632 fec_enet_rx(struct net_device *dev, __u32 regval)
633 #else
634 fec_enet_rx(struct net_device *dev)
635 #endif
636 {
637         struct  fec_enet_private *fep;
638         volatile fec_t  *fecp;
639         volatile cbd_t *bdp;
640         struct  sk_buff *skb;
641         ushort  pkt_len;
642         __u8 *data;
643
644         fep = dev->priv;
645         fecp = (volatile fec_t*)dev->base_addr;
646
647         /* First, grab all of the stats for the incoming packet.
648          * These get messed up if we get called due to a busy condition.
649          */
650         bdp = fep->cur_rx;
651
652 while (!(bdp->cbd_sc & BD_ENET_RX_EMPTY)) {
653
654 #ifndef final_version
655         /* Since we have allocated space to hold a complete frame,
656          * the last indicator should be set.
657          */
658         if ((bdp->cbd_sc & BD_ENET_RX_LAST) == 0)
659                 printk("FEC ENET: rcv is not +last\n");
660 #endif
661
662         /* Check for errors. */
663         if (bdp->cbd_sc & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
664                            BD_ENET_RX_CR | BD_ENET_RX_OV)) {
665                 fep->stats.rx_errors++;
666                 if (bdp->cbd_sc & (BD_ENET_RX_LG | BD_ENET_RX_SH)) {
667                 /* Frame too long or too short. */
668                         fep->stats.rx_length_errors++;
669                 }
670                 if (bdp->cbd_sc & BD_ENET_RX_NO)        /* Frame alignment */
671                         fep->stats.rx_frame_errors++;
672                 if (bdp->cbd_sc & BD_ENET_RX_CR)        /* CRC Error */
673                         fep->stats.rx_crc_errors++;
674                 if (bdp->cbd_sc & BD_ENET_RX_OV)        /* FIFO overrun */
675                         fep->stats.rx_crc_errors++;
676         }
677
678         /* Report late collisions as a frame error.
679          * On this error, the BD is closed, but we don't know what we
680          * have in the buffer.  So, just drop this frame on the floor.
681          */
682         if (bdp->cbd_sc & BD_ENET_RX_CL) {
683                 fep->stats.rx_errors++;
684                 fep->stats.rx_frame_errors++;
685                 goto rx_processing_done;
686         }
687
688         /* Process the incoming frame.
689          */
690         fep->stats.rx_packets++;
691         pkt_len = bdp->cbd_datlen;
692         fep->stats.rx_bytes += pkt_len;
693         data = fep->rx_vaddr[bdp - fep->rx_bd_base];
694
695 #ifdef CONFIG_FEC_PACKETHOOK
696         /* Packet hook ... */
697         if (fep->ph_rxhandler) {
698                 if (((struct ethhdr *)data)->h_proto == fep->ph_proto) {
699                         switch (fep->ph_rxhandler(data, pkt_len, regval,
700                                                   fep->ph_priv)) {
701                         case 1:
702                                 goto rx_processing_done;
703                                 break;
704                         case 0:
705                                 break;
706                         default:
707                                 fep->stats.rx_errors++;
708                                 goto rx_processing_done;
709                         }
710                 }
711         }
712
713         /* If it wasn't filtered - copy it to an sk buffer. */
714 #endif
715
716         /* This does 16 byte alignment, exactly what we need.
717          * The packet length includes FCS, but we don't want to
718          * include that when passing upstream as it messes up
719          * bridging applications.
720          */
721         skb = dev_alloc_skb(pkt_len-4);
722
723         if (skb == NULL) {
724                 printk("%s: Memory squeeze, dropping packet.\n", dev->name);
725                 fep->stats.rx_dropped++;
726         } else {
727                 skb->dev = dev;
728                 skb_put(skb,pkt_len-4); /* Make room */
729                 eth_copy_and_sum(skb, data, pkt_len-4, 0);
730                 skb->protocol=eth_type_trans(skb,dev);
731                 netif_rx(skb);
732         }
733   rx_processing_done:
734
735         /* Clear the status flags for this buffer.
736         */
737         bdp->cbd_sc &= ~BD_ENET_RX_STATS;
738
739         /* Mark the buffer empty.
740         */
741         bdp->cbd_sc |= BD_ENET_RX_EMPTY;
742
743         /* Update BD pointer to next entry.
744         */
745         if (bdp->cbd_sc & BD_ENET_RX_WRAP)
746                 bdp = fep->rx_bd_base;
747         else
748                 bdp++;
749
750 #if 1
751         /* Doing this here will keep the FEC running while we process
752          * incoming frames.  On a heavily loaded network, we should be
753          * able to keep up at the expense of system resources.
754          */
755         fecp->fec_r_des_active = 0x01000000;
756 #endif
757 #ifdef CONFIG_FEC_PACKETHOOK
758         /* Re-read register. Not exactly guaranteed to be correct,
759            but... */
760         if (fep->ph_regaddr) regval = *fep->ph_regaddr;
761 #endif
762    } /* while (!(bdp->cbd_sc & BD_ENET_RX_EMPTY)) */
763         fep->cur_rx = (cbd_t *)bdp;
764
765 #if 0
766         /* Doing this here will allow us to process all frames in the
767          * ring before the FEC is allowed to put more there.  On a heavily
768          * loaded network, some frames may be lost.  Unfortunately, this
769          * increases the interrupt overhead since we can potentially work
770          * our way back to the interrupt return only to come right back
771          * here.
772          */
773         fecp->fec_r_des_active = 0x01000000;
774 #endif
775 }
776
777
778 #ifdef  CONFIG_USE_MDIO
779 static void
780 fec_enet_mii(struct net_device *dev)
781 {
782         struct  fec_enet_private *fep;
783         volatile fec_t  *ep;
784         mii_list_t      *mip;
785         uint            mii_reg;
786
787         fep = (struct fec_enet_private *)dev->priv;
788         ep = &(((immap_t *)IMAP_ADDR)->im_cpm.cp_fec);
789         mii_reg = ep->fec_mii_data;
790
791         if ((mip = mii_head) == NULL) {
792                 printk("MII and no head!\n");
793                 return;
794         }
795
796         if (mip->mii_func != NULL)
797                 (*(mip->mii_func))(mii_reg, dev);
798
799         mii_head = mip->mii_next;
800         mip->mii_next = mii_free;
801         mii_free = mip;
802
803         if ((mip = mii_head) != NULL) {
804                 ep->fec_mii_data = mip->mii_regval;
805
806         }
807 }
808
809 static int
810 mii_queue(struct net_device *dev, int regval, void (*func)(uint, struct net_device *))
811 {
812         struct fec_enet_private *fep;
813         unsigned long   flags;
814         mii_list_t      *mip;
815         int             retval;
816
817         /* Add PHY address to register command.
818         */
819         fep = dev->priv;
820         regval |= fep->phy_addr << 23;
821
822         retval = 0;
823
824         /* lock while modifying mii_list */
825         spin_lock_irqsave(&fep->lock, flags);
826
827         if ((mip = mii_free) != NULL) {
828                 mii_free = mip->mii_next;
829                 mip->mii_regval = regval;
830                 mip->mii_func = func;
831                 mip->mii_next = NULL;
832                 if (mii_head) {
833                         mii_tail->mii_next = mip;
834                         mii_tail = mip;
835                 } else {
836                         mii_head = mii_tail = mip;
837                         (&(((immap_t *)IMAP_ADDR)->im_cpm.cp_fec))->fec_mii_data = regval;
838                 }
839         } else {
840                 retval = 1;
841         }
842
843         spin_unlock_irqrestore(&fep->lock, flags);
844
845         return(retval);
846 }
847
848 static void mii_do_cmd(struct net_device *dev, const phy_cmd_t *c)
849 {
850         int k;
851
852         if(!c)
853                 return;
854
855         for(k = 0; (c+k)->mii_data != mk_mii_end; k++)
856                 mii_queue(dev, (c+k)->mii_data, (c+k)->funct);
857 }
858
859 static void mii_parse_sr(uint mii_reg, struct net_device *dev)
860 {
861         struct fec_enet_private *fep = dev->priv;
862         volatile uint *s = &(fep->phy_status);
863
864         *s &= ~(PHY_STAT_LINK | PHY_STAT_FAULT | PHY_STAT_ANC);
865
866         if (mii_reg & 0x0004)
867                 *s |= PHY_STAT_LINK;
868         if (mii_reg & 0x0010)
869                 *s |= PHY_STAT_FAULT;
870         if (mii_reg & 0x0020)
871                 *s |= PHY_STAT_ANC;
872
873         fep->link = (*s & PHY_STAT_LINK) ? 1 : 0;
874 }
875
876 static void mii_parse_cr(uint mii_reg, struct net_device *dev)
877 {
878         struct fec_enet_private *fep = dev->priv;
879         volatile uint *s = &(fep->phy_status);
880
881         *s &= ~(PHY_CONF_ANE | PHY_CONF_LOOP);
882
883         if (mii_reg & 0x1000)
884                 *s |= PHY_CONF_ANE;
885         if (mii_reg & 0x4000)
886                 *s |= PHY_CONF_LOOP;
887 }
888
889 static void mii_parse_anar(uint mii_reg, struct net_device *dev)
890 {
891         struct fec_enet_private *fep = dev->priv;
892         volatile uint *s = &(fep->phy_status);
893
894         *s &= ~(PHY_CONF_SPMASK);
895
896         if (mii_reg & 0x0020)
897                 *s |= PHY_CONF_10HDX;
898         if (mii_reg & 0x0040)
899                 *s |= PHY_CONF_10FDX;
900         if (mii_reg & 0x0080)
901                 *s |= PHY_CONF_100HDX;
902         if (mii_reg & 0x00100)
903                 *s |= PHY_CONF_100FDX;
904 }
905 #if 0
906 static void mii_disp_reg(uint mii_reg, struct net_device *dev)
907 {
908         printk("reg %u = 0x%04x\n", (mii_reg >> 18) & 0x1f, mii_reg & 0xffff);
909 }
910 #endif
911
912 /* ------------------------------------------------------------------------- */
913 /* The Level one LXT970 is used by many boards                               */
914
915 #ifdef CONFIG_FEC_LXT970
916
917 #define MII_LXT970_MIRROR    16  /* Mirror register           */
918 #define MII_LXT970_IER       17  /* Interrupt Enable Register */
919 #define MII_LXT970_ISR       18  /* Interrupt Status Register */
920 #define MII_LXT970_CONFIG    19  /* Configuration Register    */
921 #define MII_LXT970_CSR       20  /* Chip Status Register      */
922
923 static void mii_parse_lxt970_csr(uint mii_reg, struct net_device *dev)
924 {
925         struct fec_enet_private *fep = dev->priv;
926         volatile uint *s = &(fep->phy_status);
927
928         *s &= ~(PHY_STAT_SPMASK);
929
930         if (mii_reg & 0x0800) {
931                 if (mii_reg & 0x1000)
932                         *s |= PHY_STAT_100FDX;
933                 else
934                         *s |= PHY_STAT_100HDX;
935         }
936         else {
937                 if (mii_reg & 0x1000)
938                         *s |= PHY_STAT_10FDX;
939                 else
940                         *s |= PHY_STAT_10HDX;
941         }
942 }
943
944 static phy_info_t phy_info_lxt970 = {
945         0x07810000,
946         "LXT970",
947
948         (const phy_cmd_t []) {  /* config */
949 #if 0
950 //              { mk_mii_write(MII_REG_ANAR, 0x0021), NULL },
951
952                 /* Set default operation of 100-TX....for some reason
953                  * some of these bits are set on power up, which is wrong.
954                  */
955                 { mk_mii_write(MII_LXT970_CONFIG, 0), NULL },
956 #endif
957                 { mk_mii_read(MII_REG_CR), mii_parse_cr },
958                 { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
959                 { mk_mii_end, }
960         },
961         (const phy_cmd_t []) {  /* startup - enable interrupts */
962                 { mk_mii_write(MII_LXT970_IER, 0x0002), NULL },
963                 { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
964                 { mk_mii_end, }
965         },
966         (const phy_cmd_t []) { /* ack_int */
967                 /* read SR and ISR to acknowledge */
968
969                 { mk_mii_read(MII_REG_SR), mii_parse_sr },
970                 { mk_mii_read(MII_LXT970_ISR), NULL },
971
972                 /* find out the current status */
973
974                 { mk_mii_read(MII_LXT970_CSR), mii_parse_lxt970_csr },
975                 { mk_mii_end, }
976         },
977         (const phy_cmd_t []) {  /* shutdown - disable interrupts */
978                 { mk_mii_write(MII_LXT970_IER, 0x0000), NULL },
979                 { mk_mii_end, }
980         },
981 };
982
983 #endif /* CONFIG_FEC_LXT970 */
984
985 /* ------------------------------------------------------------------------- */
986 /* The Level one LXT971 is used on some of my custom boards                  */
987
988 #ifdef CONFIG_FEC_LXT971
989
990 /* register definitions for the 971 */
991
992 #define MII_LXT971_PCR       16  /* Port Control Register     */
993 #define MII_LXT971_SR2       17  /* Status Register 2         */
994 #define MII_LXT971_IER       18  /* Interrupt Enable Register */
995 #define MII_LXT971_ISR       19  /* Interrupt Status Register */
996 #define MII_LXT971_LCR       20  /* LED Control Register      */
997 #define MII_LXT971_TCR       30  /* Transmit Control Register */
998
999 /*
1000  * I had some nice ideas of running the MDIO faster...
1001  * The 971 should support 8MHz and I tried it, but things acted really
1002  * weird, so 2.5 MHz ought to be enough for anyone...
1003  */
1004
1005 static void mii_parse_lxt971_sr2(uint mii_reg, struct net_device *dev)
1006 {
1007         struct fec_enet_private *fep = dev->priv;
1008         volatile uint *s = &(fep->phy_status);
1009
1010         *s &= ~(PHY_STAT_SPMASK);
1011
1012         if (mii_reg & 0x4000) {
1013                 if (mii_reg & 0x0200)
1014                         *s |= PHY_STAT_100FDX;
1015                 else
1016                         *s |= PHY_STAT_100HDX;
1017         }
1018         else {
1019                 if (mii_reg & 0x0200)
1020                         *s |= PHY_STAT_10FDX;
1021                 else
1022                         *s |= PHY_STAT_10HDX;
1023         }
1024         if (mii_reg & 0x0008)
1025                 *s |= PHY_STAT_FAULT;
1026 }
1027
1028 static phy_info_t phy_info_lxt971 = {
1029         0x0001378e,
1030         "LXT971",
1031
1032         (const phy_cmd_t []) {  /* config */
1033 //              { mk_mii_write(MII_REG_ANAR, 0x021), NULL }, /* 10  Mbps, HD */
1034                 { mk_mii_read(MII_REG_CR), mii_parse_cr },
1035                 { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
1036                 { mk_mii_end, }
1037         },
1038         (const phy_cmd_t []) {  /* startup - enable interrupts */
1039                 { mk_mii_write(MII_LXT971_IER, 0x00f2), NULL },
1040                 { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
1041
1042                 /* Somehow does the 971 tell me that the link is down
1043                  * the first read after power-up.
1044                  * read here to get a valid value in ack_int */
1045
1046                 { mk_mii_read(MII_REG_SR), mii_parse_sr },
1047                 { mk_mii_end, }
1048         },
1049         (const phy_cmd_t []) { /* ack_int */
1050                 /* find out the current status */
1051
1052                 { mk_mii_read(MII_REG_SR), mii_parse_sr },
1053                 { mk_mii_read(MII_LXT971_SR2), mii_parse_lxt971_sr2 },
1054
1055                 /* we only need to read ISR to acknowledge */
1056
1057                 { mk_mii_read(MII_LXT971_ISR), NULL },
1058                 { mk_mii_end, }
1059         },
1060         (const phy_cmd_t []) {  /* shutdown - disable interrupts */
1061                 { mk_mii_write(MII_LXT971_IER, 0x0000), NULL },
1062                 { mk_mii_end, }
1063         },
1064 };
1065
1066 #endif /* CONFIG_FEC_LXT970 */
1067
1068
1069 /* ------------------------------------------------------------------------- */
1070 /* The Quality Semiconductor QS6612 is used on the RPX CLLF                  */
1071
1072 #ifdef CONFIG_FEC_QS6612
1073
1074 /* register definitions */
1075
1076 #define MII_QS6612_MCR       17  /* Mode Control Register      */
1077 #define MII_QS6612_FTR       27  /* Factory Test Register      */
1078 #define MII_QS6612_MCO       28  /* Misc. Control Register     */
1079 #define MII_QS6612_ISR       29  /* Interrupt Source Register  */
1080 #define MII_QS6612_IMR       30  /* Interrupt Mask Register    */
1081 #define MII_QS6612_PCR       31  /* 100BaseTx PHY Control Reg. */
1082
1083 static void mii_parse_qs6612_pcr(uint mii_reg, struct net_device *dev)
1084 {
1085         struct fec_enet_private *fep = dev->priv;
1086         volatile uint *s = &(fep->phy_status);
1087
1088         *s &= ~(PHY_STAT_SPMASK);
1089
1090         switch((mii_reg >> 2) & 7) {
1091         case 1: *s |= PHY_STAT_10HDX; break;
1092         case 2: *s |= PHY_STAT_100HDX; break;
1093         case 5: *s |= PHY_STAT_10FDX; break;
1094         case 6: *s |= PHY_STAT_100FDX; break;
1095         }
1096 }
1097
1098 static phy_info_t phy_info_qs6612 = {
1099         0x00181440,
1100         "QS6612",
1101
1102         (const phy_cmd_t []) {  /* config */
1103 //      { mk_mii_write(MII_REG_ANAR, 0x061), NULL }, /* 10  Mbps */
1104
1105                 /* The PHY powers up isolated on the RPX,
1106                  * so send a command to allow operation.
1107                  */
1108
1109                 { mk_mii_write(MII_QS6612_PCR, 0x0dc0), NULL },
1110
1111                 /* parse cr and anar to get some info */
1112
1113                 { mk_mii_read(MII_REG_CR), mii_parse_cr },
1114                 { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
1115                 { mk_mii_end, }
1116         },
1117         (const phy_cmd_t []) {  /* startup - enable interrupts */
1118                 { mk_mii_write(MII_QS6612_IMR, 0x003a), NULL },
1119                 { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
1120                 { mk_mii_end, }
1121         },
1122         (const phy_cmd_t []) { /* ack_int */
1123
1124                 /* we need to read ISR, SR and ANER to acknowledge */
1125
1126                 { mk_mii_read(MII_QS6612_ISR), NULL },
1127                 { mk_mii_read(MII_REG_SR), mii_parse_sr },
1128                 { mk_mii_read(MII_REG_ANER), NULL },
1129
1130                 /* read pcr to get info */
1131
1132                 { mk_mii_read(MII_QS6612_PCR), mii_parse_qs6612_pcr },
1133                 { mk_mii_end, }
1134         },
1135         (const phy_cmd_t []) {  /* shutdown - disable interrupts */
1136                 { mk_mii_write(MII_QS6612_IMR, 0x0000), NULL },
1137                 { mk_mii_end, }
1138         },
1139 };
1140
1141 #endif /* CONFIG_FEC_QS6612 */
1142
1143 /* ------------------------------------------------------------------------- */
1144 /* The Advanced Micro Devices AM79C874 is used on the ICU862                 */
1145
1146 #ifdef CONFIG_FEC_AM79C874
1147
1148 /* register definitions for the 79C874 */
1149
1150 #define MII_AM79C874_MFR        16  /* Miscellaneous Features Register      */
1151 #define MII_AM79C874_ICSR       17  /* Interrupt Control/Status Register    */
1152 #define MII_AM79C874_DR         18  /* Diagnostic Register                  */
1153 #define MII_AM79C874_PMLR       19  /* Power Management & Loopback Register */
1154 #define MII_AM79C874_MCR        21  /* Mode Control Register                */
1155 #define MII_AM79C874_DC         23  /* Disconnect Counter                   */
1156 #define MII_AM79C874_REC        24  /* Receiver Error Counter               */
1157
1158 static void mii_parse_amd79c874_dr(uint mii_reg, struct net_device *dev, uint data)
1159 {
1160         volatile struct fec_enet_private *fep = dev->priv;
1161         uint s = fep->phy_status;
1162
1163         s &= ~(PHY_STAT_SPMASK);
1164
1165         /* Register 18: Bit 10 is data rate, 11 is Duplex */
1166         switch ((mii_reg >> 10) & 3) {
1167         case 0: s |= PHY_STAT_10HDX;    break;
1168         case 1: s |= PHY_STAT_100HDX;   break;
1169         case 2: s |= PHY_STAT_10FDX;    break;
1170         case 3: s |= PHY_STAT_100FDX;   break;
1171         }
1172
1173         fep->phy_status = s;
1174 }
1175
1176 static phy_info_t phy_info_amd79c874 = {
1177         0x00022561,
1178         "AM79C874",
1179
1180         (const phy_cmd_t []) {  /* config */
1181 //              { mk_mii_write(MII_REG_ANAR, 0x021), NULL }, /* 10  Mbps, HD */
1182                 { mk_mii_read(MII_REG_CR), mii_parse_cr },
1183                 { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
1184                 { mk_mii_end, }
1185         },
1186         (const phy_cmd_t []) {  /* startup - enable interrupts */
1187                 { mk_mii_write(MII_AM79C874_ICSR, 0xff00), NULL },
1188                 { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
1189                 { mk_mii_end, }
1190         },
1191         (const phy_cmd_t []) { /* ack_int */
1192                 /* find out the current status */
1193
1194                 { mk_mii_read(MII_REG_SR), mii_parse_sr },
1195                 { mk_mii_read(MII_AM79C874_DR), mii_parse_amd79c874_dr },
1196
1197                 /* we only need to read ICSR to acknowledge */
1198
1199                 { mk_mii_read(MII_AM79C874_ICSR), NULL },
1200                 { mk_mii_end, }
1201         },
1202         (const phy_cmd_t []) {  /* shutdown - disable interrupts */
1203                 { mk_mii_write(MII_AM79C874_ICSR, 0x0000), NULL },
1204                 { mk_mii_end, }
1205         },
1206 };
1207
1208 #endif /* CONFIG_FEC_AM79C874 */
1209
1210 static phy_info_t *phy_info[] = {
1211
1212 #ifdef CONFIG_FEC_LXT970
1213         &phy_info_lxt970,
1214 #endif /* CONFIG_FEC_LXT970 */
1215
1216 #ifdef CONFIG_FEC_LXT971
1217         &phy_info_lxt971,
1218 #endif /* CONFIG_FEC_LXT971 */
1219
1220 #ifdef CONFIG_FEC_QS6612
1221         &phy_info_qs6612,
1222 #endif /* CONFIG_FEC_QS6612 */
1223
1224 #ifdef CONFIG_FEC_AM79C874
1225         &phy_info_amd79c874,
1226 #endif /* CONFIG_FEC_AM79C874 */
1227
1228         NULL
1229 };
1230
1231 static void mii_display_status(struct net_device *dev)
1232 {
1233         struct fec_enet_private *fep = dev->priv;
1234         volatile uint *s = &(fep->phy_status);
1235
1236         if (!fep->link && !fep->old_link) {
1237                 /* Link is still down - don't print anything */
1238                 return;
1239         }
1240
1241         printk("%s: status: ", dev->name);
1242
1243         if (!fep->link) {
1244                 printk("link down");
1245         } else {
1246                 printk("link up");
1247
1248                 switch(*s & PHY_STAT_SPMASK) {
1249                 case PHY_STAT_100FDX: printk(", 100 Mbps Full Duplex"); break;
1250                 case PHY_STAT_100HDX: printk(", 100 Mbps Half Duplex"); break;
1251                 case PHY_STAT_10FDX: printk(", 10 Mbps Full Duplex"); break;
1252                 case PHY_STAT_10HDX: printk(", 10 Mbps Half Duplex"); break;
1253                 default:
1254                         printk(", Unknown speed/duplex");
1255                 }
1256
1257                 if (*s & PHY_STAT_ANC)
1258                         printk(", auto-negotiation complete");
1259         }
1260
1261         if (*s & PHY_STAT_FAULT)
1262                 printk(", remote fault");
1263
1264         printk(".\n");
1265 }
1266
1267 static void mii_display_config(void *priv)
1268 {
1269         struct net_device *dev = (struct net_device *)priv;
1270         struct fec_enet_private *fep = dev->priv;
1271         volatile uint *s = &(fep->phy_status);
1272
1273         printk("%s: config: auto-negotiation ", dev->name);
1274
1275         if (*s & PHY_CONF_ANE)
1276                 printk("on");
1277         else
1278                 printk("off");
1279
1280         if (*s & PHY_CONF_100FDX)
1281                 printk(", 100FDX");
1282         if (*s & PHY_CONF_100HDX)
1283                 printk(", 100HDX");
1284         if (*s & PHY_CONF_10FDX)
1285                 printk(", 10FDX");
1286         if (*s & PHY_CONF_10HDX)
1287                 printk(", 10HDX");
1288         if (!(*s & PHY_CONF_SPMASK))
1289                 printk(", No speed/duplex selected?");
1290
1291         if (*s & PHY_CONF_LOOP)
1292                 printk(", loopback enabled");
1293
1294         printk(".\n");
1295
1296         fep->sequence_done = 1;
1297 }
1298
1299 static void mii_relink(void *priv)
1300 {
1301         struct net_device *dev = (struct net_device *)priv;
1302         struct fec_enet_private *fep = dev->priv;
1303         int duplex;
1304
1305         fep->link = (fep->phy_status & PHY_STAT_LINK) ? 1 : 0;
1306         mii_display_status(dev);
1307         fep->old_link = fep->link;
1308
1309         if (fep->link) {
1310                 duplex = 0;
1311                 if (fep->phy_status
1312                     & (PHY_STAT_100FDX | PHY_STAT_10FDX))
1313                         duplex = 1;
1314                 fec_restart(dev, duplex);
1315         }
1316         else
1317                 fec_stop(dev);
1318
1319 #if 0
1320         enable_irq(fep->mii_irq);
1321 #endif
1322
1323 }
1324
1325 static void mii_queue_relink(uint mii_reg, struct net_device *dev)
1326 {
1327         struct fec_enet_private *fep = dev->priv;
1328
1329         INIT_WORK(&fep->phy_task, mii_relink, (void *)dev);
1330         schedule_work(&fep->phy_task);
1331 }
1332
1333 static void mii_queue_config(uint mii_reg, struct net_device *dev)
1334 {
1335         struct fec_enet_private *fep = dev->priv;
1336
1337         INIT_WORK(&fep->phy_task, mii_display_config, (void *)dev);
1338         schedule_work(&fep->phy_task);
1339 }
1340
1341
1342
1343 phy_cmd_t phy_cmd_relink[] = { { mk_mii_read(MII_REG_CR), mii_queue_relink },
1344                                { mk_mii_end, } };
1345 phy_cmd_t phy_cmd_config[] = { { mk_mii_read(MII_REG_CR), mii_queue_config },
1346                                { mk_mii_end, } };
1347
1348
1349
1350 /* Read remainder of PHY ID.
1351 */
1352 static void
1353 mii_discover_phy3(uint mii_reg, struct net_device *dev)
1354 {
1355         struct fec_enet_private *fep;
1356         int     i;
1357
1358         fep = dev->priv;
1359         fep->phy_id |= (mii_reg & 0xffff);
1360
1361         for(i = 0; phy_info[i]; i++)
1362                 if(phy_info[i]->id == (fep->phy_id >> 4))
1363                         break;
1364
1365         if(!phy_info[i])
1366                 panic("%s: PHY id 0x%08x is not supported!\n",
1367                       dev->name, fep->phy_id);
1368
1369         fep->phy = phy_info[i];
1370         fep->phy_id_done = 1;
1371
1372         printk("%s: Phy @ 0x%x, type %s (0x%08x)\n",
1373                 dev->name, fep->phy_addr, fep->phy->name, fep->phy_id);
1374 }
1375
1376 /* Scan all of the MII PHY addresses looking for someone to respond
1377  * with a valid ID.  This usually happens quickly.
1378  */
1379 static void
1380 mii_discover_phy(uint mii_reg, struct net_device *dev)
1381 {
1382         struct fec_enet_private *fep;
1383         uint    phytype;
1384
1385         fep = dev->priv;
1386
1387         if ((phytype = (mii_reg & 0xffff)) != 0xffff) {
1388
1389                 /* Got first part of ID, now get remainder.
1390                 */
1391                 fep->phy_id = phytype << 16;
1392                 mii_queue(dev, mk_mii_read(MII_REG_PHYIR2), mii_discover_phy3);
1393         } else {
1394                 fep->phy_addr++;
1395                 if (fep->phy_addr < 32) {
1396                         mii_queue(dev, mk_mii_read(MII_REG_PHYIR1),
1397                                                         mii_discover_phy);
1398                 } else {
1399                         printk("fec: No PHY device found.\n");
1400                 }
1401         }
1402 }
1403 #endif  /* CONFIG_USE_MDIO */
1404
1405 /* This interrupt occurs when the PHY detects a link change.
1406 */
1407 static
1408 #ifdef CONFIG_RPXCLASSIC
1409 void mii_link_interrupt(void *dev_id)
1410 #else
1411 irqreturn_t mii_link_interrupt(int irq, void * dev_id, struct pt_regs * regs)
1412 #endif
1413 {
1414 #ifdef  CONFIG_USE_MDIO
1415         struct  net_device *dev = dev_id;
1416         struct fec_enet_private *fep = dev->priv;
1417         volatile immap_t *immap = (immap_t *)IMAP_ADDR;
1418         volatile fec_t *fecp = &(immap->im_cpm.cp_fec);
1419         unsigned int ecntrl = fecp->fec_ecntrl;
1420
1421         /* We need the FEC enabled to access the MII
1422         */
1423         if ((ecntrl & FEC_ECNTRL_ETHER_EN) == 0) {
1424                 fecp->fec_ecntrl |= FEC_ECNTRL_ETHER_EN;
1425         }
1426 #endif  /* CONFIG_USE_MDIO */
1427
1428 #if 0
1429         disable_irq(fep->mii_irq);  /* disable now, enable later */
1430 #endif
1431
1432
1433 #ifdef  CONFIG_USE_MDIO
1434         mii_do_cmd(dev, fep->phy->ack_int);
1435         mii_do_cmd(dev, phy_cmd_relink);  /* restart and display status */
1436
1437         if ((ecntrl & FEC_ECNTRL_ETHER_EN) == 0) {
1438                 fecp->fec_ecntrl = ecntrl;      /* restore old settings */
1439         }
1440 #else
1441 printk("%s[%d] %s: unexpected Link interrupt\n", __FILE__,__LINE__,__FUNCTION__);
1442 #endif  /* CONFIG_USE_MDIO */
1443
1444 #ifndef CONFIG_RPXCLASSIC
1445         return IRQ_RETVAL(IRQ_HANDLED);
1446 #endif  /* CONFIG_RPXCLASSIC */
1447 }
1448
1449 static int
1450 fec_enet_open(struct net_device *dev)
1451 {
1452         struct fec_enet_private *fep = dev->priv;
1453
1454         /* I should reset the ring buffers here, but I don't yet know
1455          * a simple way to do that.
1456          */
1457
1458 #ifdef  CONFIG_USE_MDIO
1459         fep->sequence_done = 0;
1460         fep->link = 0;
1461
1462         if (fep->phy) {
1463                 mii_do_cmd(dev, fep->phy->ack_int);
1464                 mii_do_cmd(dev, fep->phy->config);
1465                 mii_do_cmd(dev, phy_cmd_config);  /* display configuration */
1466                 while(!fep->sequence_done)
1467                         schedule();
1468
1469                 mii_do_cmd(dev, fep->phy->startup);
1470                 netif_start_queue(dev);
1471                 return 0;               /* Success */
1472         }
1473         return -ENODEV;         /* No PHY we understand */
1474 #else
1475         fep->link = 1;
1476         netif_start_queue(dev);
1477         return 0;       /* Success */
1478 #endif  /* CONFIG_USE_MDIO */
1479
1480 }
1481
1482 static int
1483 fec_enet_close(struct net_device *dev)
1484 {
1485         /* Don't know what to do yet.
1486         */
1487         netif_stop_queue(dev);
1488         fec_stop(dev);
1489
1490         return 0;
1491 }
1492
1493 static struct net_device_stats *fec_enet_get_stats(struct net_device *dev)
1494 {
1495         struct fec_enet_private *fep = (struct fec_enet_private *)dev->priv;
1496
1497         return &fep->stats;
1498 }
1499
1500 /* Set or clear the multicast filter for this adaptor.
1501  * Skeleton taken from sunlance driver.
1502  * The CPM Ethernet implementation allows Multicast as well as individual
1503  * MAC address filtering.  Some of the drivers check to make sure it is
1504  * a group multicast address, and discard those that are not.  I guess I
1505  * will do the same for now, but just remove the test if you want
1506  * individual filtering as well (do the upper net layers want or support
1507  * this kind of feature?).
1508  */
1509
1510 static void set_multicast_list(struct net_device *dev)
1511 {
1512         struct  fec_enet_private *fep;
1513         volatile fec_t *ep;
1514
1515         fep = (struct fec_enet_private *)dev->priv;
1516         ep = &(((immap_t *)IMAP_ADDR)->im_cpm.cp_fec);
1517
1518         if (dev->flags&IFF_PROMISC) {
1519
1520                 /* Log any net taps. */
1521                 printk("%s: Promiscuous mode enabled.\n", dev->name);
1522                 ep->fec_r_cntrl |= FEC_RCNTRL_PROM;
1523         } else {
1524
1525                 ep->fec_r_cntrl &= ~FEC_RCNTRL_PROM;
1526
1527                 if (dev->flags & IFF_ALLMULTI) {
1528                         /* Catch all multicast addresses, so set the
1529                          * filter to all 1's.
1530                          */
1531                         ep->fec_hash_table_high = 0xffffffff;
1532                         ep->fec_hash_table_low = 0xffffffff;
1533                 }
1534 #if 0
1535                 else {
1536                         /* Clear filter and add the addresses in the list.
1537                         */
1538                         ep->sen_gaddr1 = 0;
1539                         ep->sen_gaddr2 = 0;
1540                         ep->sen_gaddr3 = 0;
1541                         ep->sen_gaddr4 = 0;
1542
1543                         dmi = dev->mc_list;
1544
1545                         for (i=0; i<dev->mc_count; i++) {
1546
1547                                 /* Only support group multicast for now.
1548                                 */
1549                                 if (!(dmi->dmi_addr[0] & 1))
1550                                         continue;
1551
1552                                 /* The address in dmi_addr is LSB first,
1553                                  * and taddr is MSB first.  We have to
1554                                  * copy bytes MSB first from dmi_addr.
1555                                  */
1556                                 mcptr = (u_char *)dmi->dmi_addr + 5;
1557                                 tdptr = (u_char *)&ep->sen_taddrh;
1558                                 for (j=0; j<6; j++)
1559                                         *tdptr++ = *mcptr--;
1560
1561                                 /* Ask CPM to run CRC and set bit in
1562                                  * filter mask.
1563                                  */
1564                                 cpmp->cp_cpcr = mk_cr_cmd(CPM_CR_CH_SCC1, CPM_CR_SET_GADDR) | CPM_CR_FLG;
1565                                 /* this delay is necessary here -- Cort */
1566                                 udelay(10);
1567                                 while (cpmp->cp_cpcr & CPM_CR_FLG);
1568                         }
1569                 }
1570 #endif
1571         }
1572 }
1573
1574 /* Initialize the FEC Ethernet on 860T.
1575  */
1576 static int __init fec_enet_init(void)
1577 {
1578         struct net_device *dev;
1579         struct fec_enet_private *fep;
1580         int i, j, k, err;
1581         unsigned char   *eap, *iap, *ba;
1582         dma_addr_t      mem_addr;
1583         volatile        cbd_t   *bdp;
1584         cbd_t           *cbd_base;
1585         volatile        immap_t *immap;
1586         volatile        fec_t   *fecp;
1587         bd_t            *bd;
1588 #ifdef CONFIG_SCC_ENET
1589         unsigned char   tmpaddr[6];
1590 #endif
1591
1592         immap = (immap_t *)IMAP_ADDR;   /* pointer to internal registers */
1593
1594         bd = (bd_t *)__res;
1595
1596         dev = alloc_etherdev(sizeof(*fep));
1597         if (!dev)
1598                 return -ENOMEM;
1599
1600         fep = dev->priv;
1601
1602         fecp = &(immap->im_cpm.cp_fec);
1603
1604         /* Whack a reset.  We should wait for this.
1605         */
1606         fecp->fec_ecntrl = FEC_ECNTRL_PINMUX | FEC_ECNTRL_RESET;
1607         for (i = 0;
1608              (fecp->fec_ecntrl & FEC_ECNTRL_RESET) && (i < FEC_RESET_DELAY);
1609              ++i) {
1610                 udelay(1);
1611         }
1612         if (i == FEC_RESET_DELAY) {
1613                 printk ("FEC Reset timeout!\n");
1614         }
1615
1616         /* Set the Ethernet address.  If using multiple Enets on the 8xx,
1617          * this needs some work to get unique addresses.
1618          */
1619         eap = (unsigned char *)my_enet_addr;
1620         iap = bd->bi_enetaddr;
1621
1622 #ifdef CONFIG_SCC_ENET
1623         /*
1624          * If a board has Ethernet configured both on a SCC and the
1625          * FEC, it needs (at least) 2 MAC addresses (we know that Sun
1626          * disagrees, but anyway). For the FEC port, we create
1627          * another address by setting one of the address bits above
1628          * something that would have (up to now) been allocated.
1629          */
1630         for (i=0; i<6; i++)
1631                 tmpaddr[i] = *iap++;
1632         tmpaddr[3] |= 0x80;
1633         iap = tmpaddr;
1634 #endif
1635
1636         for (i=0; i<6; i++) {
1637                 dev->dev_addr[i] = *eap++ = *iap++;
1638         }
1639
1640         /* Allocate memory for buffer descriptors.
1641         */
1642         if (((RX_RING_SIZE + TX_RING_SIZE) * sizeof(cbd_t)) > PAGE_SIZE) {
1643                 printk("FEC init error.  Need more space.\n");
1644                 printk("FEC initialization failed.\n");
1645                 return 1;
1646         }
1647         cbd_base = (cbd_t *)dma_alloc_coherent(dev->class_dev.dev, PAGE_SIZE,
1648                                                &mem_addr, GFP_KERNEL);
1649
1650         /* Set receive and transmit descriptor base.
1651         */
1652         fep->rx_bd_base = cbd_base;
1653         fep->tx_bd_base = cbd_base + RX_RING_SIZE;
1654
1655         fep->skb_cur = fep->skb_dirty = 0;
1656
1657         /* Initialize the receive buffer descriptors.
1658         */
1659         bdp = fep->rx_bd_base;
1660         k = 0;
1661         for (i=0; i<FEC_ENET_RX_PAGES; i++) {
1662
1663                 /* Allocate a page.
1664                 */
1665                 ba = (unsigned char *)dma_alloc_coherent(dev->class_dev.dev,
1666                                                          PAGE_SIZE,
1667                                                          &mem_addr,
1668                                                          GFP_KERNEL);
1669                 /* BUG: no check for failure */
1670
1671                 /* Initialize the BD for every fragment in the page.
1672                 */
1673                 for (j=0; j<FEC_ENET_RX_FRPPG; j++) {
1674                         bdp->cbd_sc = BD_ENET_RX_EMPTY;
1675                         bdp->cbd_bufaddr = mem_addr;
1676                         fep->rx_vaddr[k++] = ba;
1677                         mem_addr += FEC_ENET_RX_FRSIZE;
1678                         ba += FEC_ENET_RX_FRSIZE;
1679                         bdp++;
1680                 }
1681         }
1682
1683         /* Set the last buffer to wrap.
1684         */
1685         bdp--;
1686         bdp->cbd_sc |= BD_SC_WRAP;
1687
1688 #ifdef CONFIG_FEC_PACKETHOOK
1689         fep->ph_lock = 0;
1690         fep->ph_rxhandler = fep->ph_txhandler = NULL;
1691         fep->ph_proto = 0;
1692         fep->ph_regaddr = NULL;
1693         fep->ph_priv = NULL;
1694 #endif
1695
1696         /* Install our interrupt handler.
1697         */
1698         if (request_irq(FEC_INTERRUPT, fec_enet_interrupt, 0, "fec", dev) != 0)
1699                 panic("Could not allocate FEC IRQ!");
1700
1701 #ifdef CONFIG_RPXCLASSIC
1702         /* Make Port C, bit 15 an input that causes interrupts.
1703         */
1704         immap->im_ioport.iop_pcpar &= ~0x0001;
1705         immap->im_ioport.iop_pcdir &= ~0x0001;
1706         immap->im_ioport.iop_pcso  &= ~0x0001;
1707         immap->im_ioport.iop_pcint |=  0x0001;
1708         cpm_install_handler(CPMVEC_PIO_PC15, mii_link_interrupt, dev);
1709
1710         /* Make LEDS reflect Link status.
1711         */
1712         *((uint *) RPX_CSR_ADDR) &= ~BCSR2_FETHLEDMODE;
1713 #endif
1714
1715 #ifdef PHY_INTERRUPT
1716         ((immap_t *)IMAP_ADDR)->im_siu_conf.sc_siel |=
1717                 (0x80000000 >> PHY_INTERRUPT);
1718
1719         if (request_irq(PHY_INTERRUPT, mii_link_interrupt, 0, "mii", dev) != 0)
1720                 panic("Could not allocate MII IRQ!");
1721 #endif
1722
1723         dev->base_addr = (unsigned long)fecp;
1724
1725         /* The FEC Ethernet specific entries in the device structure. */
1726         dev->open = fec_enet_open;
1727         dev->hard_start_xmit = fec_enet_start_xmit;
1728         dev->tx_timeout = fec_timeout;
1729         dev->watchdog_timeo = TX_TIMEOUT;
1730         dev->stop = fec_enet_close;
1731         dev->get_stats = fec_enet_get_stats;
1732         dev->set_multicast_list = set_multicast_list;
1733
1734 #ifdef  CONFIG_USE_MDIO
1735         for (i=0; i<NMII-1; i++)
1736                 mii_cmds[i].mii_next = &mii_cmds[i+1];
1737         mii_free = mii_cmds;
1738 #endif  /* CONFIG_USE_MDIO */
1739
1740         /* Configure all of port D for MII.
1741         */
1742         immap->im_ioport.iop_pdpar = 0x1fff;
1743
1744         /* Bits moved from Rev. D onward.
1745         */
1746         if ((mfspr(SPRN_IMMR) & 0xffff) < 0x0501)
1747                 immap->im_ioport.iop_pddir = 0x1c58;    /* Pre rev. D */
1748         else
1749                 immap->im_ioport.iop_pddir = 0x1fff;    /* Rev. D and later */
1750
1751 #ifdef  CONFIG_USE_MDIO
1752         /* Set MII speed to 2.5 MHz
1753         */
1754         fecp->fec_mii_speed = fep->phy_speed =
1755                 (( (bd->bi_intfreq + 500000) / 2500000 / 2 ) & 0x3F ) << 1;
1756 #else
1757         fecp->fec_mii_speed = 0;        /* turn off MDIO */
1758 #endif  /* CONFIG_USE_MDIO */
1759
1760         err = register_netdev(dev);
1761         if (err) {
1762                 free_netdev(dev);
1763                 return err;
1764         }
1765
1766         printk ("%s: FEC ENET Version 0.2, FEC irq %d"
1767 #ifdef PHY_INTERRUPT
1768                 ", MII irq %d"
1769 #endif
1770                 ", addr ",
1771                 dev->name, FEC_INTERRUPT
1772 #ifdef PHY_INTERRUPT
1773                 , PHY_INTERRUPT
1774 #endif
1775         );
1776         for (i=0; i<6; i++)
1777                 printk("%02x%c", dev->dev_addr[i], (i==5) ? '\n' : ':');
1778
1779 #ifdef  CONFIG_USE_MDIO /* start in full duplex mode, and negotiate speed */
1780         fec_restart (dev, 1);
1781 #else                   /* always use half duplex mode only */
1782         fec_restart (dev, 0);
1783 #endif
1784
1785 #ifdef  CONFIG_USE_MDIO
1786         /* Queue up command to detect the PHY and initialize the
1787          * remainder of the interface.
1788          */
1789         fep->phy_id_done = 0;
1790         fep->phy_addr = 0;
1791         mii_queue(dev, mk_mii_read(MII_REG_PHYIR1), mii_discover_phy);
1792 #endif  /* CONFIG_USE_MDIO */
1793
1794         return 0;
1795 }
1796 module_init(fec_enet_init);
1797
1798 /* This function is called to start or restart the FEC during a link
1799  * change.  This only happens when switching between half and full
1800  * duplex.
1801  */
1802 static void
1803 fec_restart(struct net_device *dev, int duplex)
1804 {
1805         struct fec_enet_private *fep;
1806         int i;
1807         volatile        cbd_t   *bdp;
1808         volatile        immap_t *immap;
1809         volatile        fec_t   *fecp;
1810
1811         immap = (immap_t *)IMAP_ADDR;   /* pointer to internal registers */
1812
1813         fecp = &(immap->im_cpm.cp_fec);
1814
1815         fep = dev->priv;
1816
1817         /* Whack a reset.  We should wait for this.
1818         */
1819         fecp->fec_ecntrl = FEC_ECNTRL_PINMUX | FEC_ECNTRL_RESET;
1820         for (i = 0;
1821              (fecp->fec_ecntrl & FEC_ECNTRL_RESET) && (i < FEC_RESET_DELAY);
1822              ++i) {
1823                 udelay(1);
1824         }
1825         if (i == FEC_RESET_DELAY) {
1826                 printk ("FEC Reset timeout!\n");
1827         }
1828
1829         /* Set station address.
1830         */
1831         fecp->fec_addr_low  = (my_enet_addr[0] << 16) | my_enet_addr[1];
1832         fecp->fec_addr_high =  my_enet_addr[2];
1833
1834         /* Reset all multicast.
1835         */
1836         fecp->fec_hash_table_high = 0;
1837         fecp->fec_hash_table_low  = 0;
1838
1839         /* Set maximum receive buffer size.
1840         */
1841         fecp->fec_r_buff_size = PKT_MAXBLR_SIZE;
1842         fecp->fec_r_hash = PKT_MAXBUF_SIZE;
1843
1844         /* Set receive and transmit descriptor base.
1845         */
1846         fecp->fec_r_des_start = iopa((uint)(fep->rx_bd_base));
1847         fecp->fec_x_des_start = iopa((uint)(fep->tx_bd_base));
1848
1849         fep->dirty_tx = fep->cur_tx = fep->tx_bd_base;
1850         fep->cur_rx = fep->rx_bd_base;
1851
1852         /* Reset SKB transmit buffers.
1853         */
1854         fep->skb_cur = fep->skb_dirty = 0;
1855         for (i=0; i<=TX_RING_MOD_MASK; i++) {
1856                 if (fep->tx_skbuff[i] != NULL) {
1857                         dev_kfree_skb(fep->tx_skbuff[i]);
1858                         fep->tx_skbuff[i] = NULL;
1859                 }
1860         }
1861
1862         /* Initialize the receive buffer descriptors.
1863         */
1864         bdp = fep->rx_bd_base;
1865         for (i=0; i<RX_RING_SIZE; i++) {
1866
1867                 /* Initialize the BD for every fragment in the page.
1868                 */
1869                 bdp->cbd_sc = BD_ENET_RX_EMPTY;
1870                 bdp++;
1871         }
1872
1873         /* Set the last buffer to wrap.
1874         */
1875         bdp--;
1876         bdp->cbd_sc |= BD_SC_WRAP;
1877
1878         /* ...and the same for transmmit.
1879         */
1880         bdp = fep->tx_bd_base;
1881         for (i=0; i<TX_RING_SIZE; i++) {
1882
1883                 /* Initialize the BD for every fragment in the page.
1884                 */
1885                 bdp->cbd_sc = 0;
1886                 bdp->cbd_bufaddr = 0;
1887                 bdp++;
1888         }
1889
1890         /* Set the last buffer to wrap.
1891         */
1892         bdp--;
1893         bdp->cbd_sc |= BD_SC_WRAP;
1894
1895         /* Enable MII mode.
1896         */
1897         if (duplex) {
1898                 fecp->fec_r_cntrl = FEC_RCNTRL_MII_MODE;        /* MII enable */
1899                 fecp->fec_x_cntrl = FEC_TCNTRL_FDEN;            /* FD enable */
1900         }
1901         else {
1902                 fecp->fec_r_cntrl = FEC_RCNTRL_MII_MODE | FEC_RCNTRL_DRT;
1903                 fecp->fec_x_cntrl = 0;
1904         }
1905         fep->full_duplex = duplex;
1906
1907         /* Enable big endian and don't care about SDMA FC.
1908         */
1909         fecp->fec_fun_code = 0x78000000;
1910
1911 #ifdef  CONFIG_USE_MDIO
1912         /* Set MII speed.
1913         */
1914         fecp->fec_mii_speed = fep->phy_speed;
1915 #endif  /* CONFIG_USE_MDIO */
1916
1917         /* Clear any outstanding interrupt.
1918         */
1919         fecp->fec_ievent = 0xffc0;
1920
1921         fecp->fec_ivec = (FEC_INTERRUPT/2) << 29;
1922
1923         /* Enable interrupts we wish to service.
1924         */
1925         fecp->fec_imask = ( FEC_ENET_TXF | FEC_ENET_TXB |
1926                             FEC_ENET_RXF | FEC_ENET_RXB | FEC_ENET_MII );
1927
1928         /* And last, enable the transmit and receive processing.
1929         */
1930         fecp->fec_ecntrl = FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN;
1931         fecp->fec_r_des_active = 0x01000000;
1932 }
1933
1934 static void
1935 fec_stop(struct net_device *dev)
1936 {
1937         volatile        immap_t *immap;
1938         volatile        fec_t   *fecp;
1939         struct fec_enet_private *fep;
1940         int i;
1941
1942         immap = (immap_t *)IMAP_ADDR;   /* pointer to internal registers */
1943
1944         fecp = &(immap->im_cpm.cp_fec);
1945
1946         if ((fecp->fec_ecntrl & FEC_ECNTRL_ETHER_EN) == 0)
1947                 return; /* already down */
1948
1949         fep = dev->priv;
1950
1951
1952         fecp->fec_x_cntrl = 0x01;       /* Graceful transmit stop */
1953
1954         for (i = 0;
1955              ((fecp->fec_ievent & 0x10000000) == 0) && (i < FEC_RESET_DELAY);
1956              ++i) {
1957                 udelay(1);
1958         }
1959         if (i == FEC_RESET_DELAY) {
1960                 printk ("FEC timeout on graceful transmit stop\n");
1961         }
1962
1963         /* Clear outstanding MII command interrupts.
1964         */
1965         fecp->fec_ievent = FEC_ENET_MII;
1966
1967         /* Enable MII command finished interrupt
1968         */
1969         fecp->fec_ivec = (FEC_INTERRUPT/2) << 29;
1970         fecp->fec_imask = FEC_ENET_MII;
1971
1972 #ifdef  CONFIG_USE_MDIO
1973         /* Set MII speed.
1974         */
1975         fecp->fec_mii_speed = fep->phy_speed;
1976 #endif  /* CONFIG_USE_MDIO */
1977
1978         /* Disable FEC
1979         */
1980         fecp->fec_ecntrl &= ~(FEC_ECNTRL_ETHER_EN);
1981 }