ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / ppc / 8260_io / fcc_enet.c
1 /*
2  * Fast Ethernet Controller (FCC) driver for Motorola MPC8260.
3  * Copyright (c) 2000 MontaVista Software, Inc.   Dan Malek (dmalek@jlc.net)
4  *
5  * This version of the driver is a combination of the 8xx fec and
6  * 8260 SCC Ethernet drivers.  This version has some additional
7  * configuration options, which should probably be moved out of
8  * here.  This driver currently works for the EST SBC8260,
9  * SBS Diablo/BCM, Embedded Planet RPX6, TQM8260, and others.
10  *
11  * Right now, I am very watseful with the buffers.  I allocate memory
12  * pages and then divide them into 2K frame buffers.  This way I know I
13  * have buffers large enough to hold one frame within one buffer descriptor.
14  * Once I get this working, I will use 64 or 128 byte CPM buffers, which
15  * will be much more memory efficient and will easily handle lots of
16  * small packets.  Since this is a cache coherent processor and CPM,
17  * I could also preallocate SKB's and use them directly on the interface.
18  *
19  */
20
21 #include <linux/config.h>
22 #include <linux/kernel.h>
23 #include <linux/sched.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
38 #include <asm/immap_8260.h>
39 #include <asm/pgtable.h>
40 #include <asm/mpc8260.h>
41 #include <asm/irq.h>
42 #include <asm/bitops.h>
43 #include <asm/uaccess.h>
44 #include <asm/cpm_8260.h>
45
46 /* The transmitter timeout
47  */
48 #define TX_TIMEOUT      (2*HZ)
49
50 #ifdef  CONFIG_USE_MDIO
51 /* Forward declarations of some structures to support different PHYs */
52
53 typedef struct {
54         uint mii_data;
55         void (*funct)(uint mii_reg, struct net_device *dev);
56 } phy_cmd_t;
57
58 typedef struct {
59         uint id;
60         char *name;
61
62         const phy_cmd_t *config;
63         const phy_cmd_t *startup;
64         const phy_cmd_t *ack_int;
65         const phy_cmd_t *shutdown;
66 } phy_info_t;
67
68 /* Register definitions for the PHY. */
69
70 #define MII_REG_CR          0  /* Control Register                         */
71 #define MII_REG_SR          1  /* Status Register                          */
72 #define MII_REG_PHYIR1      2  /* PHY Identification Register 1            */
73 #define MII_REG_PHYIR2      3  /* PHY Identification Register 2            */
74 #define MII_REG_ANAR        4  /* A-N Advertisement Register               */
75 #define MII_REG_ANLPAR      5  /* A-N Link Partner Ability Register        */
76 #define MII_REG_ANER        6  /* A-N Expansion Register                   */
77 #define MII_REG_ANNPTR      7  /* A-N Next Page Transmit Register          */
78 #define MII_REG_ANLPRNPR    8  /* A-N Link Partner Received Next Page Reg. */
79
80 /* values for phy_status */
81
82 #define PHY_CONF_ANE    0x0001  /* 1 auto-negotiation enabled */
83 #define PHY_CONF_LOOP   0x0002  /* 1 loopback mode enabled */
84 #define PHY_CONF_SPMASK 0x00f0  /* mask for speed */
85 #define PHY_CONF_10HDX  0x0010  /* 10 Mbit half duplex supported */
86 #define PHY_CONF_10FDX  0x0020  /* 10 Mbit full duplex supported */
87 #define PHY_CONF_100HDX 0x0040  /* 100 Mbit half duplex supported */
88 #define PHY_CONF_100FDX 0x0080  /* 100 Mbit full duplex supported */
89
90 #define PHY_STAT_LINK   0x0100  /* 1 up - 0 down */
91 #define PHY_STAT_FAULT  0x0200  /* 1 remote fault */
92 #define PHY_STAT_ANC    0x0400  /* 1 auto-negotiation complete  */
93 #define PHY_STAT_SPMASK 0xf000  /* mask for speed */
94 #define PHY_STAT_10HDX  0x1000  /* 10 Mbit half duplex selected */
95 #define PHY_STAT_10FDX  0x2000  /* 10 Mbit full duplex selected */
96 #define PHY_STAT_100HDX 0x4000  /* 100 Mbit half duplex selected */
97 #define PHY_STAT_100FDX 0x8000  /* 100 Mbit full duplex selected */
98 #endif  /* CONFIG_USE_MDIO */
99
100 /* The number of Tx and Rx buffers.  These are allocated from the page
101  * pool.  The code may assume these are power of two, so it is best
102  * to keep them that size.
103  * We don't need to allocate pages for the transmitter.  We just use
104  * the skbuffer directly.
105  */
106 #define FCC_ENET_RX_PAGES       16
107 #define FCC_ENET_RX_FRSIZE      2048
108 #define FCC_ENET_RX_FRPPG       (PAGE_SIZE / FCC_ENET_RX_FRSIZE)
109 #define RX_RING_SIZE            (FCC_ENET_RX_FRPPG * FCC_ENET_RX_PAGES)
110 #define TX_RING_SIZE            16      /* Must be power of two */
111 #define TX_RING_MOD_MASK        15      /*   for this to work */
112
113 /* The FCC stores dest/src/type, data, and checksum for receive packets.
114  */
115 #define PKT_MAXBUF_SIZE         1518
116 #define PKT_MINBUF_SIZE         64
117
118 /* Maximum input DMA size.  Must be a should(?) be a multiple of 4.
119 */
120 #define PKT_MAXDMA_SIZE         1520
121
122 /* Maximum input buffer size.  Must be a multiple of 32.
123 */
124 #define PKT_MAXBLR_SIZE         1536
125
126 static int fcc_enet_open(struct net_device *dev);
127 static int fcc_enet_start_xmit(struct sk_buff *skb, struct net_device *dev);
128 static int fcc_enet_rx(struct net_device *dev);
129 static irqreturn_t fcc_enet_interrupt(int irq, void *dev_id, struct pt_regs *);
130 static int fcc_enet_close(struct net_device *dev);
131 static struct net_device_stats *fcc_enet_get_stats(struct net_device *dev);
132 static void set_multicast_list(struct net_device *dev);
133 static void fcc_restart(struct net_device *dev, int duplex);
134 static int fcc_enet_set_mac_address(struct net_device *dev, void *addr);
135
136 /* These will be configurable for the FCC choice.
137  * Multiple ports can be configured.  There is little choice among the
138  * I/O pins to the PHY, except the clocks.  We will need some board
139  * dependent clock selection.
140  * Why in the hell did I put these inside #ifdef's?  I dunno, maybe to
141  * help show what pins are used for each device.
142  */
143
144 /* I/O Pin assignment for FCC1.  I don't yet know the best way to do this,
145  * but there is little variation among the choices.
146  */
147 #define PA1_COL         ((uint)0x00000001)
148 #define PA1_CRS         ((uint)0x00000002)
149 #define PA1_TXER        ((uint)0x00000004)
150 #define PA1_TXEN        ((uint)0x00000008)
151 #define PA1_RXDV        ((uint)0x00000010)
152 #define PA1_RXER        ((uint)0x00000020)
153 #define PA1_TXDAT       ((uint)0x00003c00)
154 #define PA1_RXDAT       ((uint)0x0003c000)
155 #define PA1_PSORA0      (PA1_RXDAT | PA1_TXDAT)
156 #define PA1_PSORA1      (PA1_COL | PA1_CRS | PA1_TXER | PA1_TXEN | \
157                                 PA1_RXDV | PA1_RXER)
158 #define PA1_DIRA0       (PA1_RXDAT | PA1_CRS | PA1_COL | PA1_RXER | PA1_RXDV)
159 #define PA1_DIRA1       (PA1_TXDAT | PA1_TXEN | PA1_TXER)
160
161 /* CLK12 is receive, CLK11 is transmit.  These are board specific.
162 */
163 #define PC_F1RXCLK      ((uint)0x00000800)
164 #define PC_F1TXCLK      ((uint)0x00000400)
165 #define CMX1_CLK_ROUTE  ((uint)0x3e000000)
166 #define CMX1_CLK_MASK   ((uint)0xff000000)
167
168 /* I/O Pin assignment for FCC2.  I don't yet know the best way to do this,
169  * but there is little variation among the choices.
170  */
171 #define PB2_TXER        ((uint)0x00000001)
172 #define PB2_RXDV        ((uint)0x00000002)
173 #define PB2_TXEN        ((uint)0x00000004)
174 #define PB2_RXER        ((uint)0x00000008)
175 #define PB2_COL         ((uint)0x00000010)
176 #define PB2_CRS         ((uint)0x00000020)
177 #define PB2_TXDAT       ((uint)0x000003c0)
178 #define PB2_RXDAT       ((uint)0x00003c00)
179 #define PB2_PSORB0      (PB2_RXDAT | PB2_TXDAT | PB2_CRS | PB2_COL | \
180                                 PB2_RXER | PB2_RXDV | PB2_TXER)
181 #define PB2_PSORB1      (PB2_TXEN)
182 #define PB2_DIRB0       (PB2_RXDAT | PB2_CRS | PB2_COL | PB2_RXER | PB2_RXDV)
183 #define PB2_DIRB1       (PB2_TXDAT | PB2_TXEN | PB2_TXER)
184
185 /* CLK13 is receive, CLK14 is transmit.  These are board dependent.
186 */
187 #define PC_F2RXCLK      ((uint)0x00001000)
188 #define PC_F2TXCLK      ((uint)0x00002000)
189 #define CMX2_CLK_ROUTE  ((uint)0x00250000)
190 #define CMX2_CLK_MASK   ((uint)0x00ff0000)
191
192 /* I/O Pin assignment for FCC3.  I don't yet know the best way to do this,
193  * but there is little variation among the choices.
194  */
195 #define PB3_RXDV        ((uint)0x00004000)
196 #define PB3_RXER        ((uint)0x00008000)
197 #define PB3_TXER        ((uint)0x00010000)
198 #define PB3_TXEN        ((uint)0x00020000)
199 #define PB3_COL         ((uint)0x00040000)
200 #define PB3_CRS         ((uint)0x00080000)
201 #define PB3_TXDAT       ((uint)0x0f000000)
202 #define PB3_RXDAT       ((uint)0x00f00000)
203 #define PB3_PSORB0      (PB3_RXDAT | PB3_TXDAT | PB3_CRS | PB3_COL | \
204                                 PB3_RXER | PB3_RXDV | PB3_TXER | PB3_TXEN)
205 #define PB3_PSORB1      (0)
206 #define PB3_DIRB0       (PB3_RXDAT | PB3_CRS | PB3_COL | PB3_RXER | PB3_RXDV)
207 #define PB3_DIRB1       (PB3_TXDAT | PB3_TXEN | PB3_TXER)
208
209 /* CLK15 is receive, CLK16 is transmit.  These are board dependent.
210 */
211 #define PC_F3RXCLK      ((uint)0x00004000)
212 #define PC_F3TXCLK      ((uint)0x00008000)
213 #define CMX3_CLK_ROUTE  ((uint)0x00003700)
214 #define CMX3_CLK_MASK   ((uint)0x0000ff00)
215
216 /* MII status/control serial interface.
217 */
218 #ifdef  CONFIG_TQM8260
219 /* TQM8260 has MDIO and MDCK on PC30 and PC31 respectively */
220 #define PC_MDIO         ((uint)0x00000002)
221 #define PC_MDCK         ((uint)0x00000001)
222 #else
223 #define PC_MDIO         ((uint)0x00000004)
224 #define PC_MDCK         ((uint)0x00000020)
225 #endif
226
227 /* A table of information for supporting FCCs.  This does two things.
228  * First, we know how many FCCs we have and they are always externally
229  * numbered from zero.  Second, it holds control register and I/O
230  * information that could be different among board designs.
231  */
232 typedef struct fcc_info {
233         uint    fc_fccnum;
234         uint    fc_cpmblock;
235         uint    fc_cpmpage;
236         uint    fc_proff;
237         uint    fc_interrupt;
238         uint    fc_trxclocks;
239         uint    fc_clockroute;
240         uint    fc_clockmask;
241         uint    fc_mdio;
242         uint    fc_mdck;
243 } fcc_info_t;
244
245 static fcc_info_t fcc_ports[] = {
246 #ifdef CONFIG_FCC1_ENET
247         { 0, CPM_CR_FCC1_SBLOCK, CPM_CR_FCC1_PAGE, PROFF_FCC1, SIU_INT_FCC1,
248                 (PC_F1RXCLK | PC_F1TXCLK), CMX1_CLK_ROUTE, CMX1_CLK_MASK,
249 # if defined(CONFIG_TQM8260)
250                 PC_MDIO, PC_MDCK },
251 # else
252                 0x00000004, 0x00000100 },
253 # endif
254 #endif
255 #ifdef CONFIG_FCC2_ENET
256         { 1, CPM_CR_FCC2_SBLOCK, CPM_CR_FCC2_PAGE, PROFF_FCC2, SIU_INT_FCC2,
257                 (PC_F2RXCLK | PC_F2TXCLK), CMX2_CLK_ROUTE, CMX2_CLK_MASK,
258 # if defined(CONFIG_TQM8260)
259                 PC_MDIO, PC_MDCK },
260 # elif defined(CONFIG_EST8260) || defined(CONFIG_ADS8260)
261                 0x00400000, 0x00200000 },
262 # else
263                 0x00000002, 0x00000080 },
264 # endif
265 #endif
266 #ifdef CONFIG_FCC3_ENET
267         { 2, CPM_CR_FCC3_SBLOCK, CPM_CR_FCC3_PAGE, PROFF_FCC3, SIU_INT_FCC3,
268                 (PC_F3RXCLK | PC_F3TXCLK), CMX3_CLK_ROUTE, CMX3_CLK_MASK,
269 # if defined(CONFIG_TQM8260)
270                 PC_MDIO, PC_MDCK },
271 # else
272                 0x00000001, 0x00000040 },
273 # endif
274 #endif
275 };
276
277 /* The FCC buffer descriptors track the ring buffers.  The rx_bd_base and
278  * tx_bd_base always point to the base of the buffer descriptors.  The
279  * cur_rx and cur_tx point to the currently available buffer.
280  * The dirty_tx tracks the current buffer that is being sent by the
281  * controller.  The cur_tx and dirty_tx are equal under both completely
282  * empty and completely full conditions.  The empty/ready indicator in
283  * the buffer descriptor determines the actual condition.
284  */
285 struct fcc_enet_private {
286         /* The saved address of a sent-in-place packet/buffer, for skfree(). */
287         struct  sk_buff* tx_skbuff[TX_RING_SIZE];
288         ushort  skb_cur;
289         ushort  skb_dirty;
290
291         /* CPM dual port RAM relative addresses.
292         */
293         cbd_t   *rx_bd_base;            /* Address of Rx and Tx buffers. */
294         cbd_t   *tx_bd_base;
295         cbd_t   *cur_rx, *cur_tx;               /* The next free ring entry */
296         cbd_t   *dirty_tx;      /* The ring entries to be free()ed. */
297         volatile fcc_t  *fccp;
298         volatile fcc_enet_t     *ep;
299         struct  net_device_stats stats;
300         uint    tx_full;
301         spinlock_t lock;
302
303 #ifdef  CONFIG_USE_MDIO
304         uint    phy_id;
305         uint    phy_id_done;
306         uint    phy_status;
307         phy_info_t      *phy;
308         struct tq_struct phy_task;
309
310         uint    sequence_done;
311
312         uint    phy_addr;
313 #endif  /* CONFIG_USE_MDIO */
314
315         int     link;
316         int     old_link;
317         int     full_duplex;
318
319         fcc_info_t      *fip;
320 };
321
322 static void init_fcc_shutdown(fcc_info_t *fip, struct fcc_enet_private *cep,
323         volatile immap_t *immap);
324 static void init_fcc_startup(fcc_info_t *fip, struct net_device *dev);
325 static void init_fcc_ioports(fcc_info_t *fip, volatile iop8260_t *io,
326         volatile immap_t *immap);
327 static void init_fcc_param(fcc_info_t *fip, struct net_device *dev,
328         volatile immap_t *immap);
329
330 #ifdef  CONFIG_USE_MDIO
331 static int      mii_queue(struct net_device *dev, int request, void (*func)(uint, struct net_device *));
332 static uint     mii_send_receive(fcc_info_t *fip, uint cmd);
333
334 static void     fcc_stop(struct net_device *dev);
335
336 /* Make MII read/write commands for the FCC.
337 */
338 #define mk_mii_read(REG)        (0x60020000 | ((REG & 0x1f) << 18))
339 #define mk_mii_write(REG, VAL)  (0x50020000 | ((REG & 0x1f) << 18) | \
340                                                 (VAL & 0xffff))
341 #define mk_mii_end      0
342 #endif  /* CONFIG_USE_MDIO */
343
344
345 static int
346 fcc_enet_start_xmit(struct sk_buff *skb, struct net_device *dev)
347 {
348         struct fcc_enet_private *cep = (struct fcc_enet_private *)dev->priv;
349         volatile cbd_t  *bdp;
350
351         if (!cep->link) {
352                 /* Link is down or autonegotiation is in progress. */
353                 return 1;
354         }
355
356         /* Fill in a Tx ring entry */
357         bdp = cep->cur_tx;
358
359 #ifndef final_version
360         if (bdp->cbd_sc & BD_ENET_TX_READY) {
361                 /* Ooops.  All transmit buffers are full.  Bail out.
362                  * This should not happen, since cep->tx_full should be set.
363                  */
364                 printk("%s: tx queue full!.\n", dev->name);
365                 return 1;
366         }
367 #endif
368
369         /* Clear all of the status flags. */
370         bdp->cbd_sc &= ~BD_ENET_TX_STATS;
371
372         /* If the frame is short, tell CPM to pad it. */
373         if (skb->len <= ETH_ZLEN)
374                 bdp->cbd_sc |= BD_ENET_TX_PAD;
375         else
376                 bdp->cbd_sc &= ~BD_ENET_TX_PAD;
377
378         /* Set buffer length and buffer pointer. */
379         bdp->cbd_datlen = skb->len;
380         bdp->cbd_bufaddr = __pa(skb->data);
381
382         /* Save skb pointer. */
383         cep->tx_skbuff[cep->skb_cur] = skb;
384
385         cep->stats.tx_bytes += skb->len;
386         cep->skb_cur = (cep->skb_cur+1) & TX_RING_MOD_MASK;
387
388         spin_lock_irq(&cep->lock);
389
390         /* Send it on its way.  Tell CPM its ready, interrupt when done,
391          * its the last BD of the frame, and to put the CRC on the end.
392          */
393         bdp->cbd_sc |= (BD_ENET_TX_READY | BD_ENET_TX_INTR | BD_ENET_TX_LAST | BD_ENET_TX_TC);
394
395 #if 0
396         /* Errata says don't do this. */
397         cep->fccp->fcc_ftodr = 0x8000;
398 #endif
399         dev->trans_start = jiffies;
400
401         /* If this was the last BD in the ring, start at the beginning again. */
402         if (bdp->cbd_sc & BD_ENET_TX_WRAP)
403                 bdp = cep->tx_bd_base;
404         else
405                 bdp++;
406
407         if (bdp->cbd_sc & BD_ENET_TX_READY) {
408                 netif_stop_queue(dev);
409                 cep->tx_full = 1;
410         }
411
412         cep->cur_tx = (cbd_t *)bdp;
413
414         spin_unlock_irq(&cep->lock);
415
416         return 0;
417 }
418
419
420 static void
421 fcc_enet_timeout(struct net_device *dev)
422 {
423         struct fcc_enet_private *cep = (struct fcc_enet_private *)dev->priv;
424
425         printk("%s: transmit timed out.\n", dev->name);
426         cep->stats.tx_errors++;
427 #ifndef final_version
428         {
429                 int     i;
430                 cbd_t   *bdp;
431                 printk(" Ring data dump: cur_tx %p%s cur_rx %p.\n",
432                        cep->cur_tx, cep->tx_full ? " (full)" : "",
433                        cep->cur_rx);
434                 bdp = cep->tx_bd_base;
435                 printk(" Tx @base %p :\n", bdp);
436                 for (i = 0 ; i < TX_RING_SIZE; i++, bdp++)
437                         printk("%04x %04x %08x\n",
438                                bdp->cbd_sc,
439                                bdp->cbd_datlen,
440                                bdp->cbd_bufaddr);
441                 bdp = cep->rx_bd_base;
442                 printk(" Rx @base %p :\n", bdp);
443                 for (i = 0 ; i < RX_RING_SIZE; i++, bdp++)
444                         printk("%04x %04x %08x\n",
445                                bdp->cbd_sc,
446                                bdp->cbd_datlen,
447                                bdp->cbd_bufaddr);
448         }
449 #endif
450         if (!cep->tx_full)
451                 netif_wake_queue(dev);
452 }
453
454 /* The interrupt handler. */
455 static irqreturn_t
456 fcc_enet_interrupt(int irq, void * dev_id, struct pt_regs * regs)
457 {
458         struct  net_device *dev = dev_id;
459         volatile struct fcc_enet_private *cep;
460         volatile cbd_t  *bdp;
461         ushort  int_events;
462         int     must_restart;
463
464         cep = (struct fcc_enet_private *)dev->priv;
465
466         /* Get the interrupt events that caused us to be here.
467         */
468         int_events = cep->fccp->fcc_fcce;
469         cep->fccp->fcc_fcce = int_events;
470         must_restart = 0;
471
472         /* Handle receive event in its own function.
473         */
474         if (int_events & FCC_ENET_RXF)
475                 fcc_enet_rx(dev_id);
476
477         /* Check for a transmit error.  The manual is a little unclear
478          * about this, so the debug code until I get it figured out.  It
479          * appears that if TXE is set, then TXB is not set.  However,
480          * if carrier sense is lost during frame transmission, the TXE
481          * bit is set, "and continues the buffer transmission normally."
482          * I don't know if "normally" implies TXB is set when the buffer
483          * descriptor is closed.....trial and error :-).
484          */
485
486         /* Transmit OK, or non-fatal error.  Update the buffer descriptors.
487         */
488         if (int_events & (FCC_ENET_TXE | FCC_ENET_TXB)) {
489             spin_lock(&cep->lock);
490             bdp = cep->dirty_tx;
491             while ((bdp->cbd_sc&BD_ENET_TX_READY)==0) {
492                 if ((bdp==cep->cur_tx) && (cep->tx_full == 0))
493                     break;
494
495                 if (bdp->cbd_sc & BD_ENET_TX_HB)        /* No heartbeat */
496                         cep->stats.tx_heartbeat_errors++;
497                 if (bdp->cbd_sc & BD_ENET_TX_LC)        /* Late collision */
498                         cep->stats.tx_window_errors++;
499                 if (bdp->cbd_sc & BD_ENET_TX_RL)        /* Retrans limit */
500                         cep->stats.tx_aborted_errors++;
501                 if (bdp->cbd_sc & BD_ENET_TX_UN)        /* Underrun */
502                         cep->stats.tx_fifo_errors++;
503                 if (bdp->cbd_sc & BD_ENET_TX_CSL)       /* Carrier lost */
504                         cep->stats.tx_carrier_errors++;
505
506
507                 /* No heartbeat or Lost carrier are not really bad errors.
508                  * The others require a restart transmit command.
509                  */
510                 if (bdp->cbd_sc &
511                     (BD_ENET_TX_LC | BD_ENET_TX_RL | BD_ENET_TX_UN)) {
512                         must_restart = 1;
513                         cep->stats.tx_errors++;
514                 }
515
516                 cep->stats.tx_packets++;
517
518                 /* Deferred means some collisions occurred during transmit,
519                  * but we eventually sent the packet OK.
520                  */
521                 if (bdp->cbd_sc & BD_ENET_TX_DEF)
522                         cep->stats.collisions++;
523
524                 /* Free the sk buffer associated with this last transmit. */
525                 dev_kfree_skb_irq(cep->tx_skbuff[cep->skb_dirty]);
526                 cep->skb_dirty = (cep->skb_dirty + 1) & TX_RING_MOD_MASK;
527
528                 /* Update pointer to next buffer descriptor to be transmitted. */
529                 if (bdp->cbd_sc & BD_ENET_TX_WRAP)
530                         bdp = cep->tx_bd_base;
531                 else
532                         bdp++;
533
534                 /* I don't know if we can be held off from processing these
535                  * interrupts for more than one frame time.  I really hope
536                  * not.  In such a case, we would now want to check the
537                  * currently available BD (cur_tx) and determine if any
538                  * buffers between the dirty_tx and cur_tx have also been
539                  * sent.  We would want to process anything in between that
540                  * does not have BD_ENET_TX_READY set.
541                  */
542
543                 /* Since we have freed up a buffer, the ring is no longer
544                  * full.
545                  */
546                 if (cep->tx_full) {
547                         cep->tx_full = 0;
548                         if (netif_queue_stopped(dev)) {
549                                 netif_wake_queue(dev);
550                         }
551                 }
552
553                 cep->dirty_tx = (cbd_t *)bdp;
554             }
555
556             if (must_restart) {
557                 volatile cpm8260_t *cp;
558
559                 /* Some transmit errors cause the transmitter to shut
560                  * down.  We now issue a restart transmit.  Since the
561                  * errors close the BD and update the pointers, the restart
562                  * _should_ pick up without having to reset any of our
563                  * pointers either.  Also, To workaround 8260 device erratum
564                  * CPM37, we must disable and then re-enable the transmitter
565                  * following a Late Collision, Underrun, or Retry Limit error.
566                  */
567                 cep->fccp->fcc_gfmr &= ~FCC_GFMR_ENT;
568                 udelay(10); /* wait a few microseconds just on principle */
569                 cep->fccp->fcc_gfmr |=  FCC_GFMR_ENT;
570
571                 cp = cpmp;
572                 cp->cp_cpcr =
573                     mk_cr_cmd(cep->fip->fc_cpmpage, cep->fip->fc_cpmblock,
574                                 0x0c, CPM_CR_RESTART_TX) | CPM_CR_FLG;
575                 while (cp->cp_cpcr & CPM_CR_FLG);
576             }
577             spin_unlock(&cep->lock);
578         }
579
580         /* Check for receive busy, i.e. packets coming but no place to
581          * put them.
582          */
583         if (int_events & FCC_ENET_BSY) {
584                 cep->stats.rx_dropped++;
585         }
586         return IRQ_HANDLED;
587 }
588
589 /* During a receive, the cur_rx points to the current incoming buffer.
590  * When we update through the ring, if the next incoming buffer has
591  * not been given to the system, we just set the empty indicator,
592  * effectively tossing the packet.
593  */
594 static int
595 fcc_enet_rx(struct net_device *dev)
596 {
597         struct  fcc_enet_private *cep;
598         volatile cbd_t  *bdp;
599         struct  sk_buff *skb;
600         ushort  pkt_len;
601
602         cep = (struct fcc_enet_private *)dev->priv;
603
604         /* First, grab all of the stats for the incoming packet.
605          * These get messed up if we get called due to a busy condition.
606          */
607         bdp = cep->cur_rx;
608
609 for (;;) {
610         if (bdp->cbd_sc & BD_ENET_RX_EMPTY)
611                 break;
612
613 #ifndef final_version
614         /* Since we have allocated space to hold a complete frame, both
615          * the first and last indicators should be set.
616          */
617         if ((bdp->cbd_sc & (BD_ENET_RX_FIRST | BD_ENET_RX_LAST)) !=
618                 (BD_ENET_RX_FIRST | BD_ENET_RX_LAST))
619                         printk("CPM ENET: rcv is not first+last\n");
620 #endif
621
622         /* Frame too long or too short. */
623         if (bdp->cbd_sc & (BD_ENET_RX_LG | BD_ENET_RX_SH))
624                 cep->stats.rx_length_errors++;
625         if (bdp->cbd_sc & BD_ENET_RX_NO)        /* Frame alignment */
626                 cep->stats.rx_frame_errors++;
627         if (bdp->cbd_sc & BD_ENET_RX_CR)        /* CRC Error */
628                 cep->stats.rx_crc_errors++;
629         if (bdp->cbd_sc & BD_ENET_RX_OV)        /* FIFO overrun */
630                 cep->stats.rx_crc_errors++;
631         if (bdp->cbd_sc & BD_ENET_RX_CL)        /* Late Collision */
632                 cep->stats.rx_frame_errors++;
633
634         if (!(bdp->cbd_sc &
635               (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO | BD_ENET_RX_CR
636                | BD_ENET_RX_OV | BD_ENET_RX_CL)))
637         {
638                 /* Process the incoming frame. */
639                 cep->stats.rx_packets++;
640
641                 /* Remove the FCS from the packet length. */
642                 pkt_len = bdp->cbd_datlen - 4;
643                 cep->stats.rx_bytes += pkt_len;
644
645                 /* This does 16 byte alignment, much more than we need. */
646                 skb = dev_alloc_skb(pkt_len);
647
648                 if (skb == NULL) {
649                         printk("%s: Memory squeeze, dropping packet.\n", dev->name);
650                         cep->stats.rx_dropped++;
651                 }
652                 else {
653                         skb->dev = dev;
654                         skb_put(skb,pkt_len);   /* Make room */
655                         eth_copy_and_sum(skb,
656                                 (unsigned char *)__va(bdp->cbd_bufaddr),
657                                 pkt_len, 0);
658                         skb->protocol=eth_type_trans(skb,dev);
659                         netif_rx(skb);
660                 }
661         }
662
663         /* Clear the status flags for this buffer. */
664         bdp->cbd_sc &= ~BD_ENET_RX_STATS;
665
666         /* Mark the buffer empty. */
667         bdp->cbd_sc |= BD_ENET_RX_EMPTY;
668
669         /* Update BD pointer to next entry. */
670         if (bdp->cbd_sc & BD_ENET_RX_WRAP)
671                 bdp = cep->rx_bd_base;
672         else
673                 bdp++;
674
675    }
676         cep->cur_rx = (cbd_t *)bdp;
677
678         return 0;
679 }
680
681 static int
682 fcc_enet_close(struct net_device *dev)
683 {
684         /* Don't know what to do yet. */
685         netif_stop_queue(dev);
686
687         return 0;
688 }
689
690 static struct net_device_stats *fcc_enet_get_stats(struct net_device *dev)
691 {
692         struct fcc_enet_private *cep = (struct fcc_enet_private *)dev->priv;
693
694         return &cep->stats;
695 }
696
697 #ifdef  CONFIG_USE_MDIO
698
699 /* NOTE: Most of the following comes from the FEC driver for 860. The
700  * overall structure of MII code has been retained (as it's proved stable
701  * and well-tested), but actual transfer requests are processed "at once"
702  * instead of being queued (there's no interrupt-driven MII transfer
703  * mechanism, one has to toggle the data/clock bits manually).
704  */
705 static int
706 mii_queue(struct net_device *dev, int regval, void (*func)(uint, struct net_device *))
707 {
708         struct fcc_enet_private *fep;
709         int             retval, tmp;
710
711         /* Add PHY address to register command. */
712         fep = dev->priv;
713         regval |= fep->phy_addr << 23;
714
715         retval = 0;
716
717         tmp = mii_send_receive(fep->fip, regval);
718         if (func)
719                 func(tmp, dev);
720
721         return retval;
722 }
723
724 static void mii_do_cmd(struct net_device *dev, const phy_cmd_t *c)
725 {
726         int k;
727
728         if(!c)
729                 return;
730
731         for(k = 0; (c+k)->mii_data != mk_mii_end; k++)
732                 mii_queue(dev, (c+k)->mii_data, (c+k)->funct);
733 }
734
735 static void mii_parse_sr(uint mii_reg, struct net_device *dev)
736 {
737         volatile struct fcc_enet_private *fep = dev->priv;
738         uint s = fep->phy_status;
739
740         s &= ~(PHY_STAT_LINK | PHY_STAT_FAULT | PHY_STAT_ANC);
741
742         if (mii_reg & 0x0004)
743                 s |= PHY_STAT_LINK;
744         if (mii_reg & 0x0010)
745                 s |= PHY_STAT_FAULT;
746         if (mii_reg & 0x0020)
747                 s |= PHY_STAT_ANC;
748
749         fep->phy_status = s;
750         fep->link = (s & PHY_STAT_LINK) ? 1 : 0;
751 }
752
753 static void mii_parse_cr(uint mii_reg, struct net_device *dev)
754 {
755         volatile struct fcc_enet_private *fep = dev->priv;
756         uint s = fep->phy_status;
757
758         s &= ~(PHY_CONF_ANE | PHY_CONF_LOOP);
759
760         if (mii_reg & 0x1000)
761                 s |= PHY_CONF_ANE;
762         if (mii_reg & 0x4000)
763                 s |= PHY_CONF_LOOP;
764
765         fep->phy_status = s;
766 }
767
768 static void mii_parse_anar(uint mii_reg, struct net_device *dev)
769 {
770         volatile struct fcc_enet_private *fep = dev->priv;
771         uint s = fep->phy_status;
772
773         s &= ~(PHY_CONF_SPMASK);
774
775         if (mii_reg & 0x0020)
776                 s |= PHY_CONF_10HDX;
777         if (mii_reg & 0x0040)
778                 s |= PHY_CONF_10FDX;
779         if (mii_reg & 0x0080)
780                 s |= PHY_CONF_100HDX;
781         if (mii_reg & 0x00100)
782                 s |= PHY_CONF_100FDX;
783
784         fep->phy_status = s;
785 }
786 /* ------------------------------------------------------------------------- */
787 /* The Level one LXT970 is used by many boards                               */
788
789 #ifdef CONFIG_FCC_LXT970
790
791 #define MII_LXT970_MIRROR    16  /* Mirror register           */
792 #define MII_LXT970_IER       17  /* Interrupt Enable Register */
793 #define MII_LXT970_ISR       18  /* Interrupt Status Register */
794 #define MII_LXT970_CONFIG    19  /* Configuration Register    */
795 #define MII_LXT970_CSR       20  /* Chip Status Register      */
796
797 static void mii_parse_lxt970_csr(uint mii_reg, struct net_device *dev)
798 {
799         volatile struct fcc_enet_private *fep = dev->priv;
800         uint s = fep->phy_status;
801
802         s &= ~(PHY_STAT_SPMASK);
803
804         if (mii_reg & 0x0800) {
805                 if (mii_reg & 0x1000)
806                         s |= PHY_STAT_100FDX;
807                 else
808                         s |= PHY_STAT_100HDX;
809         } else {
810                 if (mii_reg & 0x1000)
811                         s |= PHY_STAT_10FDX;
812                 else
813                         s |= PHY_STAT_10HDX;
814         }
815
816         fep->phy_status = s;
817 }
818
819 static phy_info_t phy_info_lxt970 = {
820         0x07810000,
821         "LXT970",
822
823         (const phy_cmd_t []) {  /* config */
824 #if 0
825 //              { mk_mii_write(MII_REG_ANAR, 0x0021), NULL },
826
827                 /* Set default operation of 100-TX....for some reason
828                  * some of these bits are set on power up, which is wrong.
829                  */
830                 { mk_mii_write(MII_LXT970_CONFIG, 0), NULL },
831 #endif
832                 { mk_mii_read(MII_REG_CR), mii_parse_cr },
833                 { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
834                 { mk_mii_end, }
835         },
836         (const phy_cmd_t []) {  /* startup - enable interrupts */
837                 { mk_mii_write(MII_LXT970_IER, 0x0002), NULL },
838                 { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
839                 { mk_mii_end, }
840         },
841         (const phy_cmd_t []) { /* ack_int */
842                 /* read SR and ISR to acknowledge */
843
844                 { mk_mii_read(MII_REG_SR), mii_parse_sr },
845                 { mk_mii_read(MII_LXT970_ISR), NULL },
846
847                 /* find out the current status */
848
849                 { mk_mii_read(MII_LXT970_CSR), mii_parse_lxt970_csr },
850                 { mk_mii_end, }
851         },
852         (const phy_cmd_t []) {  /* shutdown - disable interrupts */
853                 { mk_mii_write(MII_LXT970_IER, 0x0000), NULL },
854                 { mk_mii_end, }
855         },
856 };
857
858 #endif /* CONFIG_FEC_LXT970 */
859
860 /* ------------------------------------------------------------------------- */
861 /* The Level one LXT971 is used on some of my custom boards                  */
862
863 #ifdef CONFIG_FCC_LXT971
864
865 /* register definitions for the 971 */
866
867 #define MII_LXT971_PCR       16  /* Port Control Register     */
868 #define MII_LXT971_SR2       17  /* Status Register 2         */
869 #define MII_LXT971_IER       18  /* Interrupt Enable Register */
870 #define MII_LXT971_ISR       19  /* Interrupt Status Register */
871 #define MII_LXT971_LCR       20  /* LED Control Register      */
872 #define MII_LXT971_TCR       30  /* Transmit Control Register */
873
874 /*
875  * I had some nice ideas of running the MDIO faster...
876  * The 971 should support 8MHz and I tried it, but things acted really
877  * weird, so 2.5 MHz ought to be enough for anyone...
878  */
879
880 static void mii_parse_lxt971_sr2(uint mii_reg, struct net_device *dev)
881 {
882         volatile struct fcc_enet_private *fep = dev->priv;
883         uint s = fep->phy_status;
884
885         s &= ~(PHY_STAT_SPMASK);
886
887         if (mii_reg & 0x4000) {
888                 if (mii_reg & 0x0200)
889                         s |= PHY_STAT_100FDX;
890                 else
891                         s |= PHY_STAT_100HDX;
892         } else {
893                 if (mii_reg & 0x0200)
894                         s |= PHY_STAT_10FDX;
895                 else
896                         s |= PHY_STAT_10HDX;
897         }
898         if (mii_reg & 0x0008)
899                 s |= PHY_STAT_FAULT;
900
901         fep->phy_status = s;
902 }
903
904 static phy_info_t phy_info_lxt971 = {
905         0x0001378e,
906         "LXT971",
907
908         (const phy_cmd_t []) {  /* config */
909 //              { mk_mii_write(MII_REG_ANAR, 0x021), NULL }, /* 10  Mbps, HD */
910                 { mk_mii_read(MII_REG_CR), mii_parse_cr },
911                 { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
912                 { mk_mii_end, }
913         },
914         (const phy_cmd_t []) {  /* startup - enable interrupts */
915                 { mk_mii_write(MII_LXT971_IER, 0x00f2), NULL },
916                 { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
917
918                 /* Somehow does the 971 tell me that the link is down
919                  * the first read after power-up.
920                  * read here to get a valid value in ack_int */
921
922                 { mk_mii_read(MII_REG_SR), mii_parse_sr },
923                 { mk_mii_end, }
924         },
925         (const phy_cmd_t []) { /* ack_int */
926                 /* find out the current status */
927
928                 { mk_mii_read(MII_REG_SR), mii_parse_sr },
929                 { mk_mii_read(MII_LXT971_SR2), mii_parse_lxt971_sr2 },
930
931                 /* we only need to read ISR to acknowledge */
932
933                 { mk_mii_read(MII_LXT971_ISR), NULL },
934                 { mk_mii_end, }
935         },
936         (const phy_cmd_t []) {  /* shutdown - disable interrupts */
937                 { mk_mii_write(MII_LXT971_IER, 0x0000), NULL },
938                 { mk_mii_end, }
939         },
940 };
941
942 #endif /* CONFIG_FEC_LXT970 */
943
944
945 /* ------------------------------------------------------------------------- */
946 /* The Quality Semiconductor QS6612 is used on the RPX CLLF                  */
947
948 #ifdef CONFIG_FCC_QS6612
949
950 /* register definitions */
951
952 #define MII_QS6612_MCR       17  /* Mode Control Register      */
953 #define MII_QS6612_FTR       27  /* Factory Test Register      */
954 #define MII_QS6612_MCO       28  /* Misc. Control Register     */
955 #define MII_QS6612_ISR       29  /* Interrupt Source Register  */
956 #define MII_QS6612_IMR       30  /* Interrupt Mask Register    */
957 #define MII_QS6612_PCR       31  /* 100BaseTx PHY Control Reg. */
958
959 static void mii_parse_qs6612_pcr(uint mii_reg, struct net_device *dev)
960 {
961         volatile struct fcc_enet_private *fep = dev->priv;
962         uint s = fep->phy_status;
963
964         s &= ~(PHY_STAT_SPMASK);
965
966         switch((mii_reg >> 2) & 7) {
967         case 1: s |= PHY_STAT_10HDX;  break;
968         case 2: s |= PHY_STAT_100HDX; break;
969         case 5: s |= PHY_STAT_10FDX;  break;
970         case 6: s |= PHY_STAT_100FDX; break;
971         }
972
973         fep->phy_status = s;
974 }
975
976 static phy_info_t phy_info_qs6612 = {
977         0x00181440,
978         "QS6612",
979
980         (const phy_cmd_t []) {  /* config */
981 //      { mk_mii_write(MII_REG_ANAR, 0x061), NULL }, /* 10  Mbps */
982
983                 /* The PHY powers up isolated on the RPX,
984                  * so send a command to allow operation.
985                  */
986
987                 { mk_mii_write(MII_QS6612_PCR, 0x0dc0), NULL },
988
989                 /* parse cr and anar to get some info */
990
991                 { mk_mii_read(MII_REG_CR), mii_parse_cr },
992                 { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
993                 { mk_mii_end, }
994         },
995         (const phy_cmd_t []) {  /* startup - enable interrupts */
996                 { mk_mii_write(MII_QS6612_IMR, 0x003a), NULL },
997                 { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
998                 { mk_mii_end, }
999         },
1000         (const phy_cmd_t []) { /* ack_int */
1001
1002                 /* we need to read ISR, SR and ANER to acknowledge */
1003
1004                 { mk_mii_read(MII_QS6612_ISR), NULL },
1005                 { mk_mii_read(MII_REG_SR), mii_parse_sr },
1006                 { mk_mii_read(MII_REG_ANER), NULL },
1007
1008                 /* read pcr to get info */
1009
1010                 { mk_mii_read(MII_QS6612_PCR), mii_parse_qs6612_pcr },
1011                 { mk_mii_end, }
1012         },
1013         (const phy_cmd_t []) {  /* shutdown - disable interrupts */
1014                 { mk_mii_write(MII_QS6612_IMR, 0x0000), NULL },
1015                 { mk_mii_end, }
1016         },
1017 };
1018
1019
1020 #endif /* CONFIG_FEC_QS6612 */
1021
1022
1023 static phy_info_t *phy_info[] = {
1024
1025 #ifdef CONFIG_FCC_LXT970
1026         &phy_info_lxt970,
1027 #endif /* CONFIG_FEC_LXT970 */
1028
1029 #ifdef CONFIG_FCC_LXT971
1030         &phy_info_lxt971,
1031 #endif /* CONFIG_FEC_LXT971 */
1032
1033 #ifdef CONFIG_FCC_QS6612
1034         &phy_info_qs6612,
1035 #endif /* CONFIG_FEC_LXT971 */
1036
1037         NULL
1038 };
1039
1040 static void mii_display_status(struct net_device *dev)
1041 {
1042         volatile struct fcc_enet_private *fep = dev->priv;
1043         uint s = fep->phy_status;
1044
1045         if (!fep->link && !fep->old_link) {
1046                 /* Link is still down - don't print anything */
1047                 return;
1048         }
1049
1050         printk("%s: status: ", dev->name);
1051
1052         if (!fep->link) {
1053                 printk("link down");
1054         } else {
1055                 printk("link up");
1056
1057                 switch(s & PHY_STAT_SPMASK) {
1058                 case PHY_STAT_100FDX: printk(", 100 Mbps Full Duplex"); break;
1059                 case PHY_STAT_100HDX: printk(", 100 Mbps Half Duplex"); break;
1060                 case PHY_STAT_10FDX:  printk(", 10 Mbps Full Duplex");  break;
1061                 case PHY_STAT_10HDX:  printk(", 10 Mbps Half Duplex");  break;
1062                 default:
1063                         printk(", Unknown speed/duplex");
1064                 }
1065
1066                 if (s & PHY_STAT_ANC)
1067                         printk(", auto-negotiation complete");
1068         }
1069
1070         if (s & PHY_STAT_FAULT)
1071                 printk(", remote fault");
1072
1073         printk(".\n");
1074 }
1075
1076 static void mii_display_config(struct net_device *dev)
1077 {
1078         volatile struct fcc_enet_private *fep = dev->priv;
1079         uint s = fep->phy_status;
1080
1081         printk("%s: config: auto-negotiation ", dev->name);
1082
1083         if (s & PHY_CONF_ANE)
1084                 printk("on");
1085         else
1086                 printk("off");
1087
1088         if (s & PHY_CONF_100FDX)
1089                 printk(", 100FDX");
1090         if (s & PHY_CONF_100HDX)
1091                 printk(", 100HDX");
1092         if (s & PHY_CONF_10FDX)
1093                 printk(", 10FDX");
1094         if (s & PHY_CONF_10HDX)
1095                 printk(", 10HDX");
1096         if (!(s & PHY_CONF_SPMASK))
1097                 printk(", No speed/duplex selected?");
1098
1099         if (s & PHY_CONF_LOOP)
1100                 printk(", loopback enabled");
1101
1102         printk(".\n");
1103
1104         fep->sequence_done = 1;
1105 }
1106
1107 static void mii_relink(struct net_device *dev)
1108 {
1109         struct fcc_enet_private *fep = dev->priv;
1110         int duplex;
1111
1112         fep->link = (fep->phy_status & PHY_STAT_LINK) ? 1 : 0;
1113         mii_display_status(dev);
1114         fep->old_link = fep->link;
1115
1116         if (fep->link) {
1117                 duplex = 0;
1118                 if (fep->phy_status
1119                     & (PHY_STAT_100FDX | PHY_STAT_10FDX))
1120                         duplex = 1;
1121                 fcc_restart(dev, duplex);
1122         } else {
1123                 fcc_stop(dev);
1124         }
1125 }
1126
1127 static void mii_queue_relink(uint mii_reg, struct net_device *dev)
1128 {
1129         struct fcc_enet_private *fep = dev->priv;
1130
1131         fep->phy_task.routine = (void *)mii_relink;
1132         fep->phy_task.data = dev;
1133         schedule_task(&fep->phy_task);
1134 }
1135
1136 static void mii_queue_config(uint mii_reg, struct net_device *dev)
1137 {
1138         struct fcc_enet_private *fep = dev->priv;
1139
1140         fep->phy_task.routine = (void *)mii_display_config;
1141         fep->phy_task.data = dev;
1142         schedule_task(&fep->phy_task);
1143 }
1144
1145
1146
1147 phy_cmd_t phy_cmd_relink[] = { { mk_mii_read(MII_REG_CR), mii_queue_relink },
1148                                { mk_mii_end, } };
1149 phy_cmd_t phy_cmd_config[] = { { mk_mii_read(MII_REG_CR), mii_queue_config },
1150                                { mk_mii_end, } };
1151
1152
1153 /* Read remainder of PHY ID.
1154 */
1155 static void
1156 mii_discover_phy3(uint mii_reg, struct net_device *dev)
1157 {
1158         struct fcc_enet_private *fep;
1159         int     i;
1160
1161         fep = dev->priv;
1162         fep->phy_id |= (mii_reg & 0xffff);
1163
1164         for(i = 0; phy_info[i]; i++)
1165                 if(phy_info[i]->id == (fep->phy_id >> 4))
1166                         break;
1167
1168         if(!phy_info[i])
1169                 panic("%s: PHY id 0x%08x is not supported!\n",
1170                       dev->name, fep->phy_id);
1171
1172         fep->phy = phy_info[i];
1173
1174         printk("%s: Phy @ 0x%x, type %s (0x%08x)\n",
1175                 dev->name, fep->phy_addr, fep->phy->name, fep->phy_id);
1176 }
1177
1178 /* Scan all of the MII PHY addresses looking for someone to respond
1179  * with a valid ID.  This usually happens quickly.
1180  */
1181 static void
1182 mii_discover_phy(uint mii_reg, struct net_device *dev)
1183 {
1184         struct fcc_enet_private *fep;
1185         uint    phytype;
1186
1187         fep = dev->priv;
1188
1189         if ((phytype = (mii_reg & 0xfff)) != 0xfff) {
1190
1191                 /* Got first part of ID, now get remainder. */
1192                 fep->phy_id = phytype << 16;
1193                 mii_queue(dev, mk_mii_read(MII_REG_PHYIR2), mii_discover_phy3);
1194         } else {
1195                 fep->phy_addr++;
1196                 if (fep->phy_addr < 32) {
1197                         mii_queue(dev, mk_mii_read(MII_REG_PHYIR1),
1198                                                         mii_discover_phy);
1199                 } else {
1200                         printk("fec: No PHY device found.\n");
1201                 }
1202         }
1203 }
1204
1205 /* This interrupt occurs when the PHY detects a link change. */
1206 static irqreturn_t
1207 mii_link_interrupt(int irq, void * dev_id, struct pt_regs * regs)
1208 {
1209         struct  net_device *dev = dev_id;
1210         struct fcc_enet_private *fep = dev->priv;
1211
1212         mii_do_cmd(dev, fep->phy->ack_int);
1213         mii_do_cmd(dev, phy_cmd_relink);  /* restart and display status */
1214         return IRQ_HANDLED;
1215 }
1216
1217 #endif  /* CONFIG_USE_MDIO */
1218
1219 /* Set or clear the multicast filter for this adaptor.
1220  * Skeleton taken from sunlance driver.
1221  * The CPM Ethernet implementation allows Multicast as well as individual
1222  * MAC address filtering.  Some of the drivers check to make sure it is
1223  * a group multicast address, and discard those that are not.  I guess I
1224  * will do the same for now, but just remove the test if you want
1225  * individual filtering as well (do the upper net layers want or support
1226  * this kind of feature?).
1227  */
1228 static void
1229 set_multicast_list(struct net_device *dev)
1230 {
1231         struct  fcc_enet_private *cep;
1232         struct  dev_mc_list *dmi;
1233         u_char  *mcptr, *tdptr;
1234         volatile fcc_enet_t *ep;
1235         int     i, j;
1236
1237         cep = (struct fcc_enet_private *)dev->priv;
1238
1239 return;
1240         /* Get pointer to FCC area in parameter RAM.
1241         */
1242         ep = (fcc_enet_t *)dev->base_addr;
1243
1244         if (dev->flags&IFF_PROMISC) {
1245         
1246                 /* Log any net taps. */
1247                 printk("%s: Promiscuous mode enabled.\n", dev->name);
1248                 cep->fccp->fcc_fpsmr |= FCC_PSMR_PRO;
1249         } else {
1250
1251                 cep->fccp->fcc_fpsmr &= ~FCC_PSMR_PRO;
1252
1253                 if (dev->flags & IFF_ALLMULTI) {
1254                         /* Catch all multicast addresses, so set the
1255                          * filter to all 1's.
1256                          */
1257                         ep->fen_gaddrh = 0xffffffff;
1258                         ep->fen_gaddrl = 0xffffffff;
1259                 }
1260                 else {
1261                         /* Clear filter and add the addresses in the list.
1262                         */
1263                         ep->fen_gaddrh = 0;
1264                         ep->fen_gaddrl = 0;
1265
1266                         dmi = dev->mc_list;
1267
1268                         for (i=0; i<dev->mc_count; i++) {
1269                 
1270                                 /* Only support group multicast for now.
1271                                 */
1272                                 if (!(dmi->dmi_addr[0] & 1))
1273                                         continue;
1274
1275                                 /* The address in dmi_addr is LSB first,
1276                                  * and taddr is MSB first.  We have to
1277                                  * copy bytes MSB first from dmi_addr.
1278                                  */
1279                                 mcptr = (u_char *)dmi->dmi_addr + 5;
1280                                 tdptr = (u_char *)&ep->fen_taddrh;
1281                                 for (j=0; j<6; j++)
1282                                         *tdptr++ = *mcptr--;
1283
1284                                 /* Ask CPM to run CRC and set bit in
1285                                  * filter mask.
1286                                  */
1287                                 cpmp->cp_cpcr = mk_cr_cmd(cep->fip->fc_cpmpage,
1288                                                 cep->fip->fc_cpmblock, 0x0c,
1289                                                 CPM_CR_SET_GADDR) | CPM_CR_FLG;
1290                                 udelay(10);
1291                                 while (cpmp->cp_cpcr & CPM_CR_FLG);
1292                         }
1293                 }
1294         }
1295 }
1296
1297
1298 /* Set the individual MAC address.
1299  */
1300 int fcc_enet_set_mac_address(struct net_device *dev, void *p)
1301 {
1302         struct sockaddr *addr= (struct sockaddr *) p;
1303         struct fcc_enet_private *cep;
1304         volatile fcc_enet_t *ep;
1305         unsigned char *eap;
1306         int i;
1307
1308         cep = (struct fcc_enet_private *)(dev->priv);
1309         ep = cep->ep;
1310
1311         if (netif_running(dev))
1312                 return -EBUSY;
1313
1314         memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
1315
1316         eap = (unsigned char *) &(ep->fen_paddrh);
1317         for (i=5; i>=0; i--)
1318                 *eap++ = addr->sa_data[i];
1319
1320         return 0;
1321 }
1322
1323
1324 /* Initialize the CPM Ethernet on FCC.
1325  */
1326 static int __init fec_enet_init(void)
1327 {
1328         struct net_device *dev;
1329         struct fcc_enet_private *cep;
1330         fcc_info_t      *fip;
1331         int     i, np, err;
1332         volatile        immap_t         *immap;
1333         volatile        iop8260_t       *io;
1334
1335         immap = (immap_t *)IMAP_ADDR;   /* and to internal registers */
1336         io = &immap->im_ioport;
1337
1338         np = sizeof(fcc_ports) / sizeof(fcc_info_t);
1339         fip = fcc_ports;
1340
1341         while (np-- > 0) {
1342                 /* Create an Ethernet device instance.
1343                 */
1344                 dev = alloc_etherdev(sizeof(*cep));
1345                 if (!dev)
1346                         return -ENOMEM;
1347
1348                 cep = dev->priv;
1349                 spin_lock_init(&cep->lock);
1350                 cep->fip = fip;
1351
1352                 init_fcc_shutdown(fip, cep, immap);
1353                 init_fcc_ioports(fip, io, immap);
1354                 init_fcc_param(fip, dev, immap);
1355
1356                 dev->base_addr = (unsigned long)(cep->ep);
1357
1358                 /* The CPM Ethernet specific entries in the device
1359                  * structure.
1360                  */
1361                 dev->open = fcc_enet_open;
1362                 dev->hard_start_xmit = fcc_enet_start_xmit;
1363                 dev->tx_timeout = fcc_enet_timeout;
1364                 dev->watchdog_timeo = TX_TIMEOUT;
1365                 dev->stop = fcc_enet_close;
1366                 dev->get_stats = fcc_enet_get_stats;
1367                 dev->set_multicast_list = set_multicast_list;
1368                 dev->set_mac_address = fcc_enet_set_mac_address;
1369
1370                 init_fcc_startup(fip, dev);
1371
1372                 err = register_netdev(dev);
1373                 if (err) {
1374                         free_netdev(dev);
1375                         return err;
1376                 }
1377
1378                 printk("%s: FCC ENET Version 0.3, ", dev->name);
1379                 for (i=0; i<5; i++)
1380                         printk("%02x:", dev->dev_addr[i]);
1381                 printk("%02x\n", dev->dev_addr[5]);
1382
1383 #ifdef  CONFIG_USE_MDIO
1384                 /* Queue up command to detect the PHY and initialize the
1385                 * remainder of the interface.
1386                 */
1387                 cep->phy_addr = 0;
1388                 mii_queue(dev, mk_mii_read(MII_REG_PHYIR1), mii_discover_phy);
1389 #endif  /* CONFIG_USE_MDIO */
1390
1391                 fip++;
1392         }
1393
1394         return 0;
1395 }
1396 module_init(fec_enet_init);
1397
1398 /* Make sure the device is shut down during initialization.
1399 */
1400 static void __init
1401 init_fcc_shutdown(fcc_info_t *fip, struct fcc_enet_private *cep,
1402                                                 volatile immap_t *immap)
1403 {
1404         volatile        fcc_enet_t      *ep;
1405         volatile        fcc_t           *fccp;
1406
1407         /* Get pointer to FCC area in parameter RAM.
1408         */
1409         ep = (fcc_enet_t *)(&immap->im_dprambase[fip->fc_proff]);
1410
1411         /* And another to the FCC register area.
1412         */
1413         fccp = (volatile fcc_t *)(&immap->im_fcc[fip->fc_fccnum]);
1414         cep->fccp = fccp;               /* Keep the pointers handy */
1415         cep->ep = ep;
1416
1417         /* Disable receive and transmit in case someone left it running.
1418         */
1419         fccp->fcc_gfmr &= ~(FCC_GFMR_ENR | FCC_GFMR_ENT);
1420 }
1421
1422 /* Initialize the I/O pins for the FCC Ethernet.
1423 */
1424 static void __init
1425 init_fcc_ioports(fcc_info_t *fip, volatile iop8260_t *io,
1426                                                 volatile immap_t *immap)
1427 {
1428
1429         /* FCC1 pins are on port A/C.  FCC2/3 are port B/C.
1430         */
1431         if (fip->fc_proff == PROFF_FCC1) {
1432                 /* Configure port A and C pins for FCC1 Ethernet.
1433                  */
1434                 io->iop_pdira &= ~PA1_DIRA0;
1435                 io->iop_pdira |= PA1_DIRA1;
1436                 io->iop_psora &= ~PA1_PSORA0;
1437                 io->iop_psora |= PA1_PSORA1;
1438                 io->iop_ppara |= (PA1_DIRA0 | PA1_DIRA1);
1439         }
1440         if (fip->fc_proff == PROFF_FCC2) {
1441                 /* Configure port B and C pins for FCC Ethernet.
1442                  */
1443                 io->iop_pdirb &= ~PB2_DIRB0;
1444                 io->iop_pdirb |= PB2_DIRB1;
1445                 io->iop_psorb &= ~PB2_PSORB0;
1446                 io->iop_psorb |= PB2_PSORB1;
1447                 io->iop_pparb |= (PB2_DIRB0 | PB2_DIRB1);
1448         }
1449         if (fip->fc_proff == PROFF_FCC3) {
1450                 /* Configure port B and C pins for FCC Ethernet.
1451                  */
1452                 io->iop_pdirb &= ~PB3_DIRB0;
1453                 io->iop_pdirb |= PB3_DIRB1;
1454                 io->iop_psorb &= ~PB3_PSORB0;
1455                 io->iop_psorb |= PB3_PSORB1;
1456                 io->iop_pparb |= (PB3_DIRB0 | PB3_DIRB1);
1457         }
1458
1459         /* Port C has clocks......
1460         */
1461         io->iop_psorc &= ~(fip->fc_trxclocks);
1462         io->iop_pdirc &= ~(fip->fc_trxclocks);
1463         io->iop_pparc |= fip->fc_trxclocks;
1464
1465 #ifdef  CONFIG_USE_MDIO
1466         /* ....and the MII serial clock/data.
1467         */
1468         io->iop_pdatc |= (fip->fc_mdio | fip->fc_mdck);
1469         io->iop_podrc &= ~(fip->fc_mdio | fip->fc_mdck);
1470         io->iop_pdirc |= (fip->fc_mdio | fip->fc_mdck);
1471         io->iop_pparc &= ~(fip->fc_mdio | fip->fc_mdck);
1472 #endif  /* CONFIG_USE_MDIO */
1473
1474         /* Configure Serial Interface clock routing.
1475          * First, clear all FCC bits to zero,
1476          * then set the ones we want.
1477          */
1478         immap->im_cpmux.cmx_fcr &= ~(fip->fc_clockmask);
1479         immap->im_cpmux.cmx_fcr |= fip->fc_clockroute;
1480 }
1481
1482 static void __init
1483 init_fcc_param(fcc_info_t *fip, struct net_device *dev,
1484                                                 volatile immap_t *immap)
1485 {
1486         unsigned char   *eap;
1487         unsigned long   mem_addr;
1488         bd_t            *bd;
1489         int             i, j;
1490         struct          fcc_enet_private *cep;
1491         volatile        fcc_enet_t      *ep;
1492         volatile        cbd_t           *bdp;
1493         volatile        cpm8260_t       *cp;
1494
1495         cep = (struct fcc_enet_private *)(dev->priv);
1496         ep = cep->ep;
1497         cp = cpmp;
1498
1499         bd = (bd_t *)__res;
1500
1501         /* Zero the whole thing.....I must have missed some individually.
1502          * It works when I do this.
1503          */
1504         memset((char *)ep, 0, sizeof(fcc_enet_t));
1505
1506         /* Allocate space for the buffer descriptors in the DP ram.
1507          * These are relative offsets in the DP ram address space.
1508          * Initialize base addresses for the buffer descriptors.
1509          */
1510 #if 0
1511         /* I really want to do this, but for some reason it doesn't
1512          * work with the data cache enabled, so I allocate from the
1513          * main memory instead.
1514          */
1515         i = m8260_cpm_dpalloc(sizeof(cbd_t) * RX_RING_SIZE, 8);
1516         ep->fen_genfcc.fcc_rbase = (uint)&immap->im_dprambase[i];
1517         cep->rx_bd_base = (cbd_t *)&immap->im_dprambase[i];
1518
1519         i = m8260_cpm_dpalloc(sizeof(cbd_t) * TX_RING_SIZE, 8);
1520         ep->fen_genfcc.fcc_tbase = (uint)&immap->im_dprambase[i];
1521         cep->tx_bd_base = (cbd_t *)&immap->im_dprambase[i];
1522 #else
1523         cep->rx_bd_base = (cbd_t *)m8260_cpm_hostalloc(sizeof(cbd_t) * RX_RING_SIZE, 8);
1524         ep->fen_genfcc.fcc_rbase = __pa(cep->rx_bd_base);
1525         cep->tx_bd_base = (cbd_t *)m8260_cpm_hostalloc(sizeof(cbd_t) * TX_RING_SIZE, 8);
1526         ep->fen_genfcc.fcc_tbase = __pa(cep->tx_bd_base);
1527 #endif
1528
1529         cep->dirty_tx = cep->cur_tx = cep->tx_bd_base;
1530         cep->cur_rx = cep->rx_bd_base;
1531
1532         ep->fen_genfcc.fcc_rstate = (CPMFCR_GBL | CPMFCR_EB) << 24;
1533         ep->fen_genfcc.fcc_tstate = (CPMFCR_GBL | CPMFCR_EB) << 24;
1534
1535         /* Set maximum bytes per receive buffer.
1536          * It must be a multiple of 32.
1537          */
1538         ep->fen_genfcc.fcc_mrblr = PKT_MAXBLR_SIZE;
1539
1540         /* Allocate space in the reserved FCC area of DPRAM for the
1541          * internal buffers.  No one uses this space (yet), so we
1542          * can do this.  Later, we will add resource management for
1543          * this area.
1544          */
1545         mem_addr = CPM_FCC_SPECIAL_BASE + (fip->fc_fccnum * 128);
1546         ep->fen_genfcc.fcc_riptr = mem_addr;
1547         ep->fen_genfcc.fcc_tiptr = mem_addr+32;
1548         ep->fen_padptr = mem_addr+64;
1549         memset((char *)(&(immap->im_dprambase[(mem_addr+64)])), 0x88, 32);
1550
1551         ep->fen_genfcc.fcc_rbptr = 0;
1552         ep->fen_genfcc.fcc_tbptr = 0;
1553         ep->fen_genfcc.fcc_rcrc = 0;
1554         ep->fen_genfcc.fcc_tcrc = 0;
1555         ep->fen_genfcc.fcc_res1 = 0;
1556         ep->fen_genfcc.fcc_res2 = 0;
1557
1558         ep->fen_camptr = 0;     /* CAM isn't used in this driver */
1559
1560         /* Set CRC preset and mask.
1561         */
1562         ep->fen_cmask = 0xdebb20e3;
1563         ep->fen_cpres = 0xffffffff;
1564
1565         ep->fen_crcec = 0;      /* CRC Error counter */
1566         ep->fen_alec = 0;       /* alignment error counter */
1567         ep->fen_disfc = 0;      /* discard frame counter */
1568         ep->fen_retlim = 15;    /* Retry limit threshold */
1569         ep->fen_pper = 0;       /* Normal persistence */
1570
1571         /* Clear hash filter tables.
1572         */
1573         ep->fen_gaddrh = 0;
1574         ep->fen_gaddrl = 0;
1575         ep->fen_iaddrh = 0;
1576         ep->fen_iaddrl = 0;
1577
1578         /* Clear the Out-of-sequence TxBD.
1579         */
1580         ep->fen_tfcstat = 0;
1581         ep->fen_tfclen = 0;
1582         ep->fen_tfcptr = 0;
1583
1584         ep->fen_mflr = PKT_MAXBUF_SIZE;   /* maximum frame length register */
1585         ep->fen_minflr = PKT_MINBUF_SIZE;  /* minimum frame length register */
1586
1587         /* Set Ethernet station address.
1588          *
1589          * This is supplied in the board information structure, so we
1590          * copy that into the controller.
1591          * So, far we have only been given one Ethernet address. We make
1592          * it unique by setting a few bits in the upper byte of the
1593          * non-static part of the address.
1594          */
1595         eap = (unsigned char *)&(ep->fen_paddrh);
1596         for (i=5; i>=0; i--) {
1597                 if (i == 3) {
1598                         dev->dev_addr[i] = bd->bi_enetaddr[i];
1599                         dev->dev_addr[i] |= (1 << (7 - fip->fc_fccnum));
1600                         *eap++ = dev->dev_addr[i];
1601                 }
1602                 else {
1603                         *eap++ = dev->dev_addr[i] = bd->bi_enetaddr[i];
1604                 }
1605         }
1606
1607         ep->fen_taddrh = 0;
1608         ep->fen_taddrm = 0;
1609         ep->fen_taddrl = 0;
1610
1611         ep->fen_maxd1 = PKT_MAXDMA_SIZE;        /* maximum DMA1 length */
1612         ep->fen_maxd2 = PKT_MAXDMA_SIZE;        /* maximum DMA2 length */
1613
1614         /* Clear stat counters, in case we ever enable RMON.
1615         */
1616         ep->fen_octc = 0;
1617         ep->fen_colc = 0;
1618         ep->fen_broc = 0;
1619         ep->fen_mulc = 0;
1620         ep->fen_uspc = 0;
1621         ep->fen_frgc = 0;
1622         ep->fen_ospc = 0;
1623         ep->fen_jbrc = 0;
1624         ep->fen_p64c = 0;
1625         ep->fen_p65c = 0;
1626         ep->fen_p128c = 0;
1627         ep->fen_p256c = 0;
1628         ep->fen_p512c = 0;
1629         ep->fen_p1024c = 0;
1630
1631         ep->fen_rfthr = 0;      /* Suggested by manual */
1632         ep->fen_rfcnt = 0;
1633         ep->fen_cftype = 0;
1634
1635         /* Now allocate the host memory pages and initialize the
1636          * buffer descriptors.
1637          */
1638         bdp = cep->tx_bd_base;
1639         for (i=0; i<TX_RING_SIZE; i++) {
1640
1641                 /* Initialize the BD for every fragment in the page.
1642                 */
1643                 bdp->cbd_sc = 0;
1644                 bdp->cbd_datlen = 0;
1645                 bdp->cbd_bufaddr = 0;
1646                 bdp++;
1647         }
1648
1649         /* Set the last buffer to wrap.
1650         */
1651         bdp--;
1652         bdp->cbd_sc |= BD_SC_WRAP;
1653
1654         bdp = cep->rx_bd_base;
1655         for (i=0; i<FCC_ENET_RX_PAGES; i++) {
1656
1657                 /* Allocate a page.
1658                 */
1659                 mem_addr = __get_free_page(GFP_KERNEL);
1660
1661                 /* Initialize the BD for every fragment in the page.
1662                 */
1663                 for (j=0; j<FCC_ENET_RX_FRPPG; j++) {
1664                         bdp->cbd_sc = BD_ENET_RX_EMPTY | BD_ENET_RX_INTR;
1665                         bdp->cbd_datlen = 0;
1666                         bdp->cbd_bufaddr = __pa(mem_addr);
1667                         mem_addr += FCC_ENET_RX_FRSIZE;
1668                         bdp++;
1669                 }
1670         }
1671
1672         /* Set the last buffer to wrap.
1673         */
1674         bdp--;
1675         bdp->cbd_sc |= BD_SC_WRAP;
1676
1677         /* Let's re-initialize the channel now.  We have to do it later
1678          * than the manual describes because we have just now finished
1679          * the BD initialization.
1680          */
1681         cp->cp_cpcr = mk_cr_cmd(fip->fc_cpmpage, fip->fc_cpmblock, 0x0c,
1682                         CPM_CR_INIT_TRX) | CPM_CR_FLG;
1683         while (cp->cp_cpcr & CPM_CR_FLG);
1684
1685         cep->skb_cur = cep->skb_dirty = 0;
1686 }
1687
1688 /* Let 'er rip.
1689 */
1690 static void __init
1691 init_fcc_startup(fcc_info_t *fip, struct net_device *dev)
1692 {
1693         volatile fcc_t  *fccp;
1694         struct fcc_enet_private *cep;
1695
1696         cep = (struct fcc_enet_private *)(dev->priv);
1697         fccp = cep->fccp;
1698
1699         fccp->fcc_fcce = 0xffff;        /* Clear any pending events */
1700
1701         /* Enable interrupts for transmit error, complete frame
1702          * received, and any transmit buffer we have also set the
1703          * interrupt flag.
1704          */
1705         fccp->fcc_fccm = (FCC_ENET_TXE | FCC_ENET_RXF | FCC_ENET_TXB);
1706
1707         /* Install our interrupt handler.
1708         */
1709         if (request_irq(fip->fc_interrupt, fcc_enet_interrupt, 0,
1710                                                         "fenet", dev) < 0)
1711                 printk("Can't get FCC IRQ %d\n", fip->fc_interrupt);
1712
1713 #ifdef  CONFIG_USE_MDIO
1714         if (request_irq(PHY_INTERRUPT, mii_link_interrupt, 0,
1715                                                         "mii", dev) < 0)
1716                 printk("Can't get MII IRQ %d\n", fip->fc_interrupt);
1717 #endif  /* CONFIG_USE_MDIO */
1718
1719         /* Set GFMR to enable Ethernet operating mode.
1720          */
1721         fccp->fcc_gfmr = (FCC_GFMR_TCI | FCC_GFMR_MODE_ENET);
1722
1723         /* Set sync/delimiters.
1724         */
1725         fccp->fcc_fdsr = 0xd555;
1726
1727         /* Set protocol specific processing mode for Ethernet.
1728          * This has to be adjusted for Full Duplex operation after we can
1729          * determine how to detect that.
1730          */
1731         fccp->fcc_fpsmr = FCC_PSMR_ENCRC;
1732
1733 #ifdef CONFIG_ADS8260
1734         /* Enable the PHY.
1735         */
1736         ads_csr_addr[1] |= BCSR1_FETH_RST;      /* Remove reset */
1737         ads_csr_addr[1] &= ~BCSR1_FETHIEN;      /* Enable */
1738 #endif
1739
1740 #if defined(CONFIG_USE_MDIO) || defined(CONFIG_TQM8260)
1741         /* start in full duplex mode, and negotiate speed
1742          */
1743         fcc_restart (dev, 1);
1744 #else
1745         /* start in half duplex mode
1746          */
1747         fcc_restart (dev, 0);
1748 #endif
1749 }
1750
1751 #ifdef  CONFIG_USE_MDIO
1752 /* MII command/status interface.
1753  * I'm not going to describe all of the details.  You can find the
1754  * protocol definition in many other places, including the data sheet
1755  * of most PHY parts.
1756  * I wonder what "they" were thinking (maybe weren't) when they leave
1757  * the I2C in the CPM but I have to toggle these bits......
1758  */
1759
1760 #define FCC_PDATC_MDIO(bit)                                     \
1761         if (bit)                                                \
1762                 io->iop_pdatc |= fip->fc_mdio;                  \
1763         else                                                    \
1764                 io->iop_pdatc &= ~fip->fc_mdio;
1765
1766 #define FCC_PDATC_MDC(bit)                                      \
1767         if (bit)                                                \
1768                 io->iop_pdatc |= fip->fc_mdck;                  \
1769         else                                                    \
1770                 io->iop_pdatc &= ~fip->fc_mdck;
1771
1772 static uint
1773 mii_send_receive(fcc_info_t *fip, uint cmd)
1774 {
1775         uint            retval;
1776         int             read_op, i, off;
1777         volatile        immap_t         *immap;
1778         volatile        iop8260_t       *io;
1779
1780         immap = (immap_t *)IMAP_ADDR;
1781         io = &immap->im_ioport;
1782
1783         io->iop_pdirc |= (fip->fc_mdio | fip->fc_mdck);
1784
1785         read_op = ((cmd & 0xf0000000) == 0x60000000);
1786
1787         /* Write preamble
1788          */
1789         for (i = 0; i < 32; i++)
1790         {
1791                 FCC_PDATC_MDC(0);
1792                 FCC_PDATC_MDIO(1);
1793                 udelay(1);
1794                 FCC_PDATC_MDC(1);
1795                 udelay(1);
1796         }
1797
1798         /* Write data
1799          */
1800         for (i = 0, off = 31; i < (read_op ? 14 : 32); i++, --off)
1801         {
1802                 FCC_PDATC_MDC(0);
1803                 FCC_PDATC_MDIO((cmd >> off) & 0x00000001);
1804                 udelay(1);
1805                 FCC_PDATC_MDC(1);
1806                 udelay(1);
1807         }
1808
1809         retval = cmd;
1810
1811         if (read_op)
1812         {
1813                 retval >>= 16;
1814
1815                 FCC_PDATC_MDC(0);
1816                 io->iop_pdirc &= ~fip->fc_mdio;
1817                 udelay(1);
1818                 FCC_PDATC_MDC(1);
1819                 udelay(1);
1820                 FCC_PDATC_MDC(0);
1821                 udelay(1);
1822
1823                 for (i = 0, off = 15; i < 16; i++, off--)
1824                 {
1825                         FCC_PDATC_MDC(1);
1826                         retval <<= 1;
1827                         if (io->iop_pdatc & fip->fc_mdio)
1828                                 retval++;
1829                         udelay(1);
1830                         FCC_PDATC_MDC(0);
1831                         udelay(1);
1832                 }
1833         }
1834
1835         io->iop_pdirc |= (fip->fc_mdio | fip->fc_mdck);
1836
1837         for (i = 0; i < 32; i++)
1838         {
1839                 FCC_PDATC_MDC(0);
1840                 FCC_PDATC_MDIO(1);
1841                 udelay(1);
1842                 FCC_PDATC_MDC(1);
1843                 udelay(1);
1844         }
1845
1846         return retval;
1847 }
1848
1849 static void
1850 fcc_stop(struct net_device *dev)
1851 {
1852         volatile fcc_t  *fccp;
1853         struct fcc_enet_private *fcp;
1854
1855         fcp = (struct fcc_enet_private *)(dev->priv);
1856         fccp = fcp->fccp;
1857
1858         /* Disable transmit/receive */
1859         fccp->fcc_gfmr &= ~(FCC_GFMR_ENR | FCC_GFMR_ENT);
1860 }
1861 #endif  /* CONFIG_USE_MDIO */
1862
1863 static void
1864 fcc_restart(struct net_device *dev, int duplex)
1865 {
1866         volatile fcc_t  *fccp;
1867         struct fcc_enet_private *fcp;
1868
1869         fcp = (struct fcc_enet_private *)(dev->priv);
1870         fccp = fcp->fccp;
1871
1872         if (duplex)
1873                 fccp->fcc_fpsmr |= FCC_PSMR_FDE;
1874         else
1875                 fccp->fcc_fpsmr &= ~FCC_PSMR_FDE;
1876
1877         /* Enable transmit/receive */
1878         fccp->fcc_gfmr |= FCC_GFMR_ENR | FCC_GFMR_ENT;
1879 }
1880
1881 static int
1882 fcc_enet_open(struct net_device *dev)
1883 {
1884         struct fcc_enet_private *fep = dev->priv;
1885
1886 #ifdef  CONFIG_USE_MDIO
1887         fep->sequence_done = 0;
1888         fep->link = 0;
1889
1890         if (fep->phy) {
1891                 mii_do_cmd(dev, fep->phy->ack_int);
1892                 mii_do_cmd(dev, fep->phy->config);
1893                 mii_do_cmd(dev, phy_cmd_config);  /* display configuration */
1894                 while(!fep->sequence_done)
1895                         schedule();
1896
1897                 mii_do_cmd(dev, fep->phy->startup);
1898                 netif_start_queue(dev);
1899                 return 0;               /* Success */
1900         }
1901         return -ENODEV;         /* No PHY we understand */
1902 #else
1903         fep->link = 1;
1904         netif_start_queue(dev);
1905         return 0;                                       /* Always succeed */
1906 #endif  /* CONFIG_USE_MDIO */
1907 }
1908