]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - cpu/mpc8xx/fec.c
Fix Ethernet init() return codes
[karo-tx-uboot.git] / cpu / mpc8xx / fec.c
1 /*
2  * (C) Copyright 2000
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21  * MA 02111-1307 USA
22  */
23
24 #include <common.h>
25 #include <malloc.h>
26 #include <commproc.h>
27 #include <net.h>
28 #include <command.h>
29
30 DECLARE_GLOBAL_DATA_PTR;
31
32 #undef  ET_DEBUG
33
34 #if defined(CONFIG_CMD_NET) && \
35         (defined(FEC_ENET) || defined(CONFIG_ETHER_ON_FEC1) || defined(CONFIG_ETHER_ON_FEC2))
36
37 /* compatibility test, if only FEC_ENET defined assume ETHER on FEC1 */
38 #if defined(FEC_ENET) && !defined(CONFIG_ETHER_ON_FEC1) && !defined(CONFIG_ETHER_ON_FEC2)
39 #define CONFIG_ETHER_ON_FEC1 1
40 #endif
41
42 /* define WANT_MII when MII support is required */
43 #if defined(CFG_DISCOVER_PHY) || defined(CONFIG_FEC1_PHY) || defined(CONFIG_FEC2_PHY)
44 #define WANT_MII
45 #else
46 #undef WANT_MII
47 #endif
48
49 #if defined(WANT_MII)
50 #include <miiphy.h>
51
52 #if !(defined(CONFIG_MII) || defined(CONFIG_CMD_MII))
53 #error "CONFIG_MII has to be defined!"
54 #endif
55
56 #endif
57
58 #if defined(CONFIG_RMII) && !defined(WANT_MII)
59 #error RMII support is unusable without a working PHY.
60 #endif
61
62 #ifdef CFG_DISCOVER_PHY
63 static int mii_discover_phy(struct eth_device *dev);
64 #endif
65
66 int fec8xx_miiphy_read(char *devname, unsigned char addr,
67                 unsigned char  reg, unsigned short *value);
68 int fec8xx_miiphy_write(char *devname, unsigned char  addr,
69                 unsigned char  reg, unsigned short value);
70
71 static struct ether_fcc_info_s
72 {
73         int ether_index;
74         int fecp_offset;
75         int phy_addr;
76         int actual_phy_addr;
77         int initialized;
78 }
79         ether_fcc_info[] = {
80 #if defined(CONFIG_ETHER_ON_FEC1)
81         {
82                 0,
83                 offsetof(immap_t, im_cpm.cp_fec1),
84 #if defined(CONFIG_FEC1_PHY)
85                 CONFIG_FEC1_PHY,
86 #else
87                 -1,     /* discover */
88 #endif
89                 -1,
90                 0,
91
92         },
93 #endif
94 #if defined(CONFIG_ETHER_ON_FEC2)
95         {
96                 1,
97                 offsetof(immap_t, im_cpm.cp_fec2),
98 #if defined(CONFIG_FEC2_PHY)
99                 CONFIG_FEC2_PHY,
100 #else
101                 -1,
102 #endif
103                 -1,
104                 0,
105         },
106 #endif
107 };
108
109 /* Ethernet Transmit and Receive Buffers */
110 #define DBUF_LENGTH  1520
111
112 #define TX_BUF_CNT 2
113
114 #define TOUT_LOOP 100
115
116 #define PKT_MAXBUF_SIZE         1518
117 #define PKT_MINBUF_SIZE         64
118 #define PKT_MAXBLR_SIZE         1520
119
120 #ifdef __GNUC__
121 static char txbuf[DBUF_LENGTH] __attribute__ ((aligned(8)));
122 #else
123 #error txbuf must be aligned.
124 #endif
125
126 static uint rxIdx;      /* index of the current RX buffer */
127 static uint txIdx;      /* index of the current TX buffer */
128
129 /*
130   * FEC Ethernet Tx and Rx buffer descriptors allocated at the
131   *  immr->udata_bd address on Dual-Port RAM
132   * Provide for Double Buffering
133   */
134
135 typedef volatile struct CommonBufferDescriptor {
136     cbd_t rxbd[PKTBUFSRX];              /* Rx BD */
137     cbd_t txbd[TX_BUF_CNT];             /* Tx BD */
138 } RTXBD;
139
140 static RTXBD *rtx = NULL;
141
142 static int fec_send(struct eth_device* dev, volatile void *packet, int length);
143 static int fec_recv(struct eth_device* dev);
144 static int fec_init(struct eth_device* dev, bd_t * bd);
145 static void fec_halt(struct eth_device* dev);
146 static void __mii_init(void);
147
148 int fec_initialize(bd_t *bis)
149 {
150         struct eth_device* dev;
151         struct ether_fcc_info_s *efis;
152         int             i;
153
154         for (i = 0; i < sizeof(ether_fcc_info) / sizeof(ether_fcc_info[0]); i++) {
155
156                 dev = malloc(sizeof(*dev));
157                 if (dev == NULL)
158                         hang();
159
160                 memset(dev, 0, sizeof(*dev));
161
162                 /* for FEC1 make sure that the name of the interface is the same
163                    as the old one for compatibility reasons */
164                 if (i == 0) {
165                         sprintf (dev->name, "FEC ETHERNET");
166                 } else {
167                         sprintf (dev->name, "FEC%d ETHERNET",
168                                 ether_fcc_info[i].ether_index + 1);
169                 }
170
171                 efis = &ether_fcc_info[i];
172
173                 /*
174                  * reset actual phy addr
175                  */
176                 efis->actual_phy_addr = -1;
177
178                 dev->priv = efis;
179                 dev->init = fec_init;
180                 dev->halt = fec_halt;
181                 dev->send = fec_send;
182                 dev->recv = fec_recv;
183
184                 eth_register(dev);
185
186 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
187                 miiphy_register(dev->name,
188                         fec8xx_miiphy_read, fec8xx_miiphy_write);
189 #endif
190         }
191         return 1;
192 }
193
194 static int fec_send(struct eth_device* dev, volatile void *packet, int length)
195 {
196         int j, rc;
197         struct ether_fcc_info_s *efis = dev->priv;
198         volatile fec_t *fecp = (volatile fec_t *)(CFG_IMMR + efis->fecp_offset);
199
200         /* section 16.9.23.3
201          * Wait for ready
202          */
203         j = 0;
204         while ((rtx->txbd[txIdx].cbd_sc & BD_ENET_TX_READY) && (j<TOUT_LOOP)) {
205                 udelay(1);
206                 j++;
207         }
208         if (j>=TOUT_LOOP) {
209                 printf("TX not ready\n");
210         }
211
212         rtx->txbd[txIdx].cbd_bufaddr = (uint)packet;
213         rtx->txbd[txIdx].cbd_datlen  = length;
214         rtx->txbd[txIdx].cbd_sc |= BD_ENET_TX_READY | BD_ENET_TX_LAST;
215         __asm__ ("eieio");
216
217         /* Activate transmit Buffer Descriptor polling */
218         fecp->fec_x_des_active = 0x01000000;    /* Descriptor polling active    */
219
220         j = 0;
221         while ((rtx->txbd[txIdx].cbd_sc & BD_ENET_TX_READY) && (j<TOUT_LOOP)) {
222 #if defined(CONFIG_ICU862)
223                 udelay(10);
224 #else
225                 udelay(1);
226 #endif
227                 j++;
228         }
229         if (j>=TOUT_LOOP) {
230                 printf("TX timeout\n");
231         }
232 #ifdef ET_DEBUG
233         printf("%s[%d] %s: cycles: %d    status: %x  retry cnt: %d\n",
234         __FILE__,__LINE__,__FUNCTION__,j,rtx->txbd[txIdx].cbd_sc,
235         (rtx->txbd[txIdx].cbd_sc & 0x003C)>>2);
236 #endif
237         /* return only status bits */;
238         rc = (rtx->txbd[txIdx].cbd_sc & BD_ENET_TX_STATS);
239
240         txIdx = (txIdx + 1) % TX_BUF_CNT;
241
242         return rc;
243 }
244
245 static int fec_recv (struct eth_device *dev)
246 {
247         struct ether_fcc_info_s *efis = dev->priv;
248         volatile fec_t *fecp =
249                 (volatile fec_t *) (CFG_IMMR + efis->fecp_offset);
250         int length;
251
252         for (;;) {
253                 /* section 16.9.23.2 */
254                 if (rtx->rxbd[rxIdx].cbd_sc & BD_ENET_RX_EMPTY) {
255                         length = -1;
256                         break;  /* nothing received - leave for() loop */
257                 }
258
259                 length = rtx->rxbd[rxIdx].cbd_datlen;
260
261                 if (rtx->rxbd[rxIdx].cbd_sc & 0x003f) {
262 #ifdef ET_DEBUG
263                         printf ("%s[%d] err: %x\n",
264                                 __FUNCTION__, __LINE__,
265                                 rtx->rxbd[rxIdx].cbd_sc);
266 #endif
267                 } else {
268                         volatile uchar *rx = NetRxPackets[rxIdx];
269
270                         length -= 4;
271
272 #if defined(CONFIG_CMD_CDP)
273                         if ((rx[0] & 1) != 0
274                             && memcmp ((uchar *) rx, NetBcastAddr, 6) != 0
275                             && memcmp ((uchar *) rx, NetCDPAddr, 6) != 0)
276                                 rx = NULL;
277 #endif
278                         /*
279                          * Pass the packet up to the protocol layers.
280                          */
281                         if (rx != NULL)
282                                 NetReceive (rx, length);
283                 }
284
285                 /* Give the buffer back to the FEC. */
286                 rtx->rxbd[rxIdx].cbd_datlen = 0;
287
288                 /* wrap around buffer index when necessary */
289                 if ((rxIdx + 1) >= PKTBUFSRX) {
290                         rtx->rxbd[PKTBUFSRX - 1].cbd_sc =
291                                 (BD_ENET_RX_WRAP | BD_ENET_RX_EMPTY);
292                         rxIdx = 0;
293                 } else {
294                         rtx->rxbd[rxIdx].cbd_sc = BD_ENET_RX_EMPTY;
295                         rxIdx++;
296                 }
297
298                 __asm__ ("eieio");
299
300                 /* Try to fill Buffer Descriptors */
301                 fecp->fec_r_des_active = 0x01000000;    /* Descriptor polling active    */
302         }
303
304         return length;
305 }
306
307 /**************************************************************
308  *
309  * FEC Ethernet Initialization Routine
310  *
311  *************************************************************/
312
313 #define FEC_ECNTRL_PINMUX       0x00000004
314 #define FEC_ECNTRL_ETHER_EN     0x00000002
315 #define FEC_ECNTRL_RESET        0x00000001
316
317 #define FEC_RCNTRL_BC_REJ       0x00000010
318 #define FEC_RCNTRL_PROM         0x00000008
319 #define FEC_RCNTRL_MII_MODE     0x00000004
320 #define FEC_RCNTRL_DRT          0x00000002
321 #define FEC_RCNTRL_LOOP         0x00000001
322
323 #define FEC_TCNTRL_FDEN         0x00000004
324 #define FEC_TCNTRL_HBC          0x00000002
325 #define FEC_TCNTRL_GTS          0x00000001
326
327 #define FEC_RESET_DELAY         50
328
329 #if defined(CONFIG_RMII)
330
331 static inline void fec_10Mbps(struct eth_device *dev)
332 {
333         struct ether_fcc_info_s *efis = dev->priv;
334         int fecidx = efis->ether_index;
335         uint mask = (fecidx == 0) ? 0x0000010 : 0x0000008;
336
337         if ((unsigned int)fecidx >= 2)
338                 hang();
339
340         ((volatile immap_t *)CFG_IMMR)->im_cpm.cp_cptr |=  mask;
341 }
342
343 static inline void fec_100Mbps(struct eth_device *dev)
344 {
345         struct ether_fcc_info_s *efis = dev->priv;
346         int fecidx = efis->ether_index;
347         uint mask = (fecidx == 0) ? 0x0000010 : 0x0000008;
348
349         if ((unsigned int)fecidx >= 2)
350                 hang();
351
352         ((volatile immap_t *)CFG_IMMR)->im_cpm.cp_cptr &= ~mask;
353 }
354
355 #endif
356
357 static inline void fec_full_duplex(struct eth_device *dev)
358 {
359         struct ether_fcc_info_s *efis = dev->priv;
360         volatile fec_t *fecp = (volatile fec_t *)(CFG_IMMR + efis->fecp_offset);
361
362         fecp->fec_r_cntrl &= ~FEC_RCNTRL_DRT;
363         fecp->fec_x_cntrl |=  FEC_TCNTRL_FDEN;  /* FD enable */
364 }
365
366 static inline void fec_half_duplex(struct eth_device *dev)
367 {
368         struct ether_fcc_info_s *efis = dev->priv;
369         volatile fec_t *fecp = (volatile fec_t *)(CFG_IMMR + efis->fecp_offset);
370
371         fecp->fec_r_cntrl |=  FEC_RCNTRL_DRT;
372         fecp->fec_x_cntrl &= ~FEC_TCNTRL_FDEN;  /* FD disable */
373 }
374
375 static void fec_pin_init(int fecidx)
376 {
377         bd_t           *bd = gd->bd;
378         volatile immap_t *immr = (immap_t *) CFG_IMMR;
379         volatile fec_t *fecp;
380
381         /*
382          * only two FECs please
383          */
384         if ((unsigned int)fecidx >= 2)
385                 hang();
386
387         if (fecidx == 0)
388                 fecp = &immr->im_cpm.cp_fec1;
389         else
390                 fecp = &immr->im_cpm.cp_fec2;
391
392         /*
393          * Set MII speed to 2.5 MHz or slightly below.
394          * * According to the MPC860T (Rev. D) Fast ethernet controller user
395          * * manual (6.2.14),
396          * * the MII management interface clock must be less than or equal
397          * * to 2.5 MHz.
398          * * This MDC frequency is equal to system clock / (2 * MII_SPEED).
399          * * Then MII_SPEED = system_clock / 2 * 2,5 Mhz.
400          *
401          * All MII configuration is done via FEC1 registers:
402          */
403         immr->im_cpm.cp_fec1.fec_mii_speed = ((bd->bi_intfreq + 4999999) / 5000000) << 1;
404
405 #if defined(CONFIG_NETTA) || defined(CONFIG_NETPHONE) || defined(CONFIG_NETTA2)
406         /* our PHYs are the limit at 2.5 MHz */
407         fecp->fec_mii_speed <<= 1;
408 #endif
409
410 #if defined(CONFIG_MPC885_FAMILY) && defined(WANT_MII)
411         /* use MDC for MII */
412         immr->im_ioport.iop_pdpar |=  0x0080;
413         immr->im_ioport.iop_pddir &= ~0x0080;
414 #endif
415
416         if (fecidx == 0) {
417 #if defined(CONFIG_ETHER_ON_FEC1)
418
419 #if defined(CONFIG_MPC885_FAMILY) /* MPC87x/88x have got 2 FECs and different pinout */
420
421 #if !defined(CONFIG_RMII)
422
423                 immr->im_ioport.iop_papar |=  0xf830;
424                 immr->im_ioport.iop_padir |=  0x0830;
425                 immr->im_ioport.iop_padir &= ~0xf000;
426
427                 immr->im_cpm.cp_pbpar     |=  0x00001001;
428                 immr->im_cpm.cp_pbdir     &= ~0x00001001;
429
430                 immr->im_ioport.iop_pcpar |=  0x000c;
431                 immr->im_ioport.iop_pcdir &= ~0x000c;
432
433                 immr->im_cpm.cp_pepar     |=  0x00000003;
434                 immr->im_cpm.cp_pedir     |=  0x00000003;
435                 immr->im_cpm.cp_peso      &= ~0x00000003;
436
437                 immr->im_cpm.cp_cptr      &= ~0x00000100;
438
439 #else
440
441 #if !defined(CONFIG_FEC1_PHY_NORXERR)
442                 immr->im_ioport.iop_papar |=  0x1000;
443                 immr->im_ioport.iop_padir &= ~0x1000;
444 #endif
445                 immr->im_ioport.iop_papar |=  0xe810;
446                 immr->im_ioport.iop_padir |=  0x0810;
447                 immr->im_ioport.iop_padir &= ~0xe000;
448
449                 immr->im_cpm.cp_pbpar     |=  0x00000001;
450                 immr->im_cpm.cp_pbdir     &= ~0x00000001;
451
452                 immr->im_cpm.cp_cptr      |=  0x00000100;
453                 immr->im_cpm.cp_cptr      &= ~0x00000050;
454
455 #endif /* !CONFIG_RMII */
456
457 #elif !defined(CONFIG_ICU862) && !defined(CONFIG_IAD210)
458                 /*
459                  * Configure all of port D for MII.
460                  */
461                 immr->im_ioport.iop_pdpar = 0x1fff;
462
463                 /*
464                  * Bits moved from Rev. D onward
465                  */
466                 if ((get_immr(0) & 0xffff) < 0x0501)
467                         immr->im_ioport.iop_pddir = 0x1c58;     /* Pre rev. D */
468                 else
469                         immr->im_ioport.iop_pddir = 0x1fff;     /* Rev. D and later */
470 #else
471                 /*
472                  * Configure port A for MII.
473                  */
474
475 #if defined(CONFIG_ICU862) && defined(CFG_DISCOVER_PHY)
476
477                 /*
478                  * On the ICU862 board the MII-MDC pin is routed to PD8 pin
479                  * * of CPU, so for this board we need to configure Utopia and
480                  * * enable PD8 to MII-MDC function
481                  */
482                 immr->im_ioport.iop_pdpar |= 0x4080;
483 #endif
484
485                 /*
486                  * Has Utopia been configured?
487                  */
488                 if (immr->im_ioport.iop_pdpar & (0x8000 >> 1)) {
489                         /*
490                          * YES - Use MUXED mode for UTOPIA bus.
491                          * This frees Port A for use by MII (see 862UM table 41-6).
492                          */
493                         immr->im_ioport.utmode &= ~0x80;
494                 } else {
495                         /*
496                          * NO - set SPLIT mode for UTOPIA bus.
497                          *
498                          * This doesn't really effect UTOPIA (which isn't
499                          * enabled anyway) but just tells the 862
500                          * to use port A for MII (see 862UM table 41-6).
501                          */
502                         immr->im_ioport.utmode |= 0x80;
503                 }
504 #endif                          /* !defined(CONFIG_ICU862) */
505
506 #endif  /* CONFIG_ETHER_ON_FEC1 */
507         } else if (fecidx == 1) {
508
509 #if defined(CONFIG_ETHER_ON_FEC2)
510
511 #if defined(CONFIG_MPC885_FAMILY) /* MPC87x/88x have got 2 FECs and different pinout */
512
513 #if !defined(CONFIG_RMII)
514                 immr->im_cpm.cp_pepar     |=  0x0003fffc;
515                 immr->im_cpm.cp_pedir     |=  0x0003fffc;
516                 immr->im_cpm.cp_peso      &= ~0x000087fc;
517                 immr->im_cpm.cp_peso      |=  0x00037800;
518
519                 immr->im_cpm.cp_cptr      &= ~0x00000080;
520 #else
521
522 #if !defined(CONFIG_FEC2_PHY_NORXERR)
523                 immr->im_cpm.cp_pepar     |=  0x00000010;
524                 immr->im_cpm.cp_pedir     |=  0x00000010;
525                 immr->im_cpm.cp_peso      &= ~0x00000010;
526 #endif
527                 immr->im_cpm.cp_pepar     |=  0x00039620;
528                 immr->im_cpm.cp_pedir     |=  0x00039620;
529                 immr->im_cpm.cp_peso      |=  0x00031000;
530                 immr->im_cpm.cp_peso      &= ~0x00008620;
531
532                 immr->im_cpm.cp_cptr      |=  0x00000080;
533                 immr->im_cpm.cp_cptr      &= ~0x00000028;
534 #endif /* CONFIG_RMII */
535
536 #endif /* CONFIG_MPC885_FAMILY */
537
538 #endif /* CONFIG_ETHER_ON_FEC2 */
539
540         }
541 }
542
543 static int fec_reset(volatile fec_t *fecp)
544 {
545         int i;
546
547         /* Whack a reset.
548          * A delay is required between a reset of the FEC block and
549          * initialization of other FEC registers because the reset takes
550          * some time to complete. If you don't delay, subsequent writes
551          * to FEC registers might get killed by the reset routine which is
552          * still in progress.
553          */
554
555         fecp->fec_ecntrl = FEC_ECNTRL_PINMUX | FEC_ECNTRL_RESET;
556         for (i = 0;
557              (fecp->fec_ecntrl & FEC_ECNTRL_RESET) && (i < FEC_RESET_DELAY);
558              ++i) {
559                 udelay (1);
560         }
561         if (i == FEC_RESET_DELAY)
562                 return -1;
563
564         return 0;
565 }
566
567 static int fec_init (struct eth_device *dev, bd_t * bd)
568 {
569         struct ether_fcc_info_s *efis = dev->priv;
570         volatile immap_t *immr = (immap_t *) CFG_IMMR;
571         volatile fec_t *fecp =
572                 (volatile fec_t *) (CFG_IMMR + efis->fecp_offset);
573         int i;
574
575         if (efis->ether_index == 0) {
576 #if defined(CONFIG_FADS)        /* FADS family uses FPGA (BCSR) to control PHYs */
577 #if defined(CONFIG_MPC885ADS)
578                 *(vu_char *) BCSR5 &= ~(BCSR5_MII1_EN | BCSR5_MII1_RST);
579 #else
580                 /* configure FADS for fast (FEC) ethernet, half-duplex */
581                 /* The LXT970 needs about 50ms to recover from reset, so
582                  * wait for it by discovering the PHY before leaving eth_init().
583                  */
584                 {
585                         volatile uint *bcsr4 = (volatile uint *) BCSR4;
586
587                         *bcsr4 = (*bcsr4 & ~(BCSR4_FETH_EN | BCSR4_FETHCFG1))
588                                 | (BCSR4_FETHCFG0 | BCSR4_FETHFDE |
589                                    BCSR4_FETHRST);
590
591                         /* reset the LXT970 PHY */
592                         *bcsr4 &= ~BCSR4_FETHRST;
593                         udelay (10);
594                         *bcsr4 |= BCSR4_FETHRST;
595                         udelay (10);
596                 }
597 #endif /* CONFIG_MPC885ADS */
598 #endif /* CONFIG_FADS */
599         }
600
601 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
602         /* the MII interface is connected to FEC1
603          * so for the miiphy_xxx function to work we must
604          * call mii_init since fec_halt messes the thing up
605          */
606         if (efis->ether_index != 0)
607                 __mii_init();
608 #endif
609
610         if (fec_reset(fecp) < 0)
611                 printf ("FEC_RESET_DELAY timeout\n");
612
613         /* We use strictly polling mode only
614          */
615         fecp->fec_imask = 0;
616
617         /* Clear any pending interrupt
618          */
619         fecp->fec_ievent = 0xffc0;
620
621         /* No need to set the IVEC register */
622
623         /* Set station address
624          */
625 #define ea dev->enetaddr
626         fecp->fec_addr_low = (ea[0] << 24) | (ea[1] << 16) | (ea[2] << 8) | (ea[3]);
627         fecp->fec_addr_high = (ea[4] << 8) | (ea[5]);
628 #undef ea
629
630 #if defined(CONFIG_CMD_CDP)
631         /*
632          * Turn on multicast address hash table
633          */
634         fecp->fec_hash_table_high = 0xffffffff;
635         fecp->fec_hash_table_low = 0xffffffff;
636 #else
637         /* Clear multicast address hash table
638          */
639         fecp->fec_hash_table_high = 0;
640         fecp->fec_hash_table_low = 0;
641 #endif
642
643         /* Set maximum receive buffer size.
644          */
645         fecp->fec_r_buff_size = PKT_MAXBLR_SIZE;
646
647         /* Set maximum frame length
648          */
649         fecp->fec_r_hash = PKT_MAXBUF_SIZE;
650
651         /*
652          * Setup Buffers and Buffer Desriptors
653          */
654         rxIdx = 0;
655         txIdx = 0;
656
657         if (!rtx) {
658 #ifdef CFG_ALLOC_DPRAM
659                 rtx = (RTXBD *) (immr->im_cpm.cp_dpmem +
660                                  dpram_alloc_align (sizeof (RTXBD), 8));
661 #else
662                 rtx = (RTXBD *) (immr->im_cpm.cp_dpmem + CPM_FEC_BASE);
663 #endif
664         }
665         /*
666          * Setup Receiver Buffer Descriptors (13.14.24.18)
667          * Settings:
668          *     Empty, Wrap
669          */
670         for (i = 0; i < PKTBUFSRX; i++) {
671                 rtx->rxbd[i].cbd_sc = BD_ENET_RX_EMPTY;
672                 rtx->rxbd[i].cbd_datlen = 0;    /* Reset */
673                 rtx->rxbd[i].cbd_bufaddr = (uint) NetRxPackets[i];
674         }
675         rtx->rxbd[PKTBUFSRX - 1].cbd_sc |= BD_ENET_RX_WRAP;
676
677         /*
678          * Setup Ethernet Transmitter Buffer Descriptors (13.14.24.19)
679          * Settings:
680          *    Last, Tx CRC
681          */
682         for (i = 0; i < TX_BUF_CNT; i++) {
683                 rtx->txbd[i].cbd_sc = BD_ENET_TX_LAST | BD_ENET_TX_TC;
684                 rtx->txbd[i].cbd_datlen = 0;    /* Reset */
685                 rtx->txbd[i].cbd_bufaddr = (uint) (&txbuf[0]);
686         }
687         rtx->txbd[TX_BUF_CNT - 1].cbd_sc |= BD_ENET_TX_WRAP;
688
689         /* Set receive and transmit descriptor base
690          */
691         fecp->fec_r_des_start = (unsigned int) (&rtx->rxbd[0]);
692         fecp->fec_x_des_start = (unsigned int) (&rtx->txbd[0]);
693
694         /* Enable MII mode
695          */
696 #if 0                           /* Full duplex mode */
697         fecp->fec_r_cntrl = FEC_RCNTRL_MII_MODE;
698         fecp->fec_x_cntrl = FEC_TCNTRL_FDEN;
699 #else  /* Half duplex mode */
700         fecp->fec_r_cntrl = FEC_RCNTRL_MII_MODE | FEC_RCNTRL_DRT;
701         fecp->fec_x_cntrl = 0;
702 #endif
703
704         /* Enable big endian and don't care about SDMA FC.
705          */
706         fecp->fec_fun_code = 0x78000000;
707
708         /*
709          * Setup the pin configuration of the FEC
710          */
711         fec_pin_init (efis->ether_index);
712
713         rxIdx = 0;
714         txIdx = 0;
715
716         /*
717          * Now enable the transmit and receive processing
718          */
719         fecp->fec_ecntrl = FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN;
720
721         if (efis->phy_addr == -1) {
722 #ifdef CFG_DISCOVER_PHY
723                 /*
724                  * wait for the PHY to wake up after reset
725                  */
726                 efis->actual_phy_addr = mii_discover_phy (dev);
727
728                 if (efis->actual_phy_addr == -1) {
729                         printf ("Unable to discover phy!\n");
730                         return -1;
731                 }
732 #else
733                 efis->actual_phy_addr = -1;
734 #endif
735         } else {
736                 efis->actual_phy_addr = efis->phy_addr;
737         }
738
739 #if defined(CONFIG_MII) && defined(CONFIG_RMII)
740         /*
741          * adapt the RMII speed to the speed of the phy
742          */
743         if (miiphy_speed (dev->name, efis->actual_phy_addr) == _100BASET) {
744                 fec_100Mbps (dev);
745         } else {
746                 fec_10Mbps (dev);
747         }
748 #endif
749
750 #if defined(CONFIG_MII)
751         /*
752          * adapt to the half/full speed settings
753          */
754         if (miiphy_duplex (dev->name, efis->actual_phy_addr) == FULL) {
755                 fec_full_duplex (dev);
756         } else {
757                 fec_half_duplex (dev);
758         }
759 #endif
760
761         /* And last, try to fill Rx Buffer Descriptors */
762         fecp->fec_r_des_active = 0x01000000;    /* Descriptor polling active    */
763
764         efis->initialized = 1;
765
766         return 0;
767 }
768
769
770 static void fec_halt(struct eth_device* dev)
771 {
772         struct ether_fcc_info_s *efis = dev->priv;
773         volatile fec_t *fecp = (volatile fec_t *)(CFG_IMMR + efis->fecp_offset);
774         int i;
775
776         /* avoid halt if initialized; mii gets stuck otherwise */
777         if (!efis->initialized)
778                 return;
779
780         /* Whack a reset.
781          * A delay is required between a reset of the FEC block and
782          * initialization of other FEC registers because the reset takes
783          * some time to complete. If you don't delay, subsequent writes
784          * to FEC registers might get killed by the reset routine which is
785          * still in progress.
786          */
787
788         fecp->fec_ecntrl = FEC_ECNTRL_PINMUX | FEC_ECNTRL_RESET;
789         for (i = 0;
790              (fecp->fec_ecntrl & FEC_ECNTRL_RESET) && (i < FEC_RESET_DELAY);
791              ++i) {
792                 udelay (1);
793         }
794         if (i == FEC_RESET_DELAY) {
795                 printf ("FEC_RESET_DELAY timeout\n");
796                 return;
797         }
798
799         efis->initialized = 0;
800 }
801
802 #if defined(CFG_DISCOVER_PHY) || defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
803
804 /* Make MII read/write commands for the FEC.
805 */
806
807 #define mk_mii_read(ADDR, REG)  (0x60020000 | ((ADDR << 23) | \
808                                                 (REG & 0x1f) << 18))
809
810 #define mk_mii_write(ADDR, REG, VAL)    (0x50020000 | ((ADDR << 23) | \
811                                                 (REG & 0x1f) << 18) | \
812                                                 (VAL & 0xffff))
813
814 /* Interrupt events/masks.
815 */
816 #define FEC_ENET_HBERR  ((uint)0x80000000)      /* Heartbeat error */
817 #define FEC_ENET_BABR   ((uint)0x40000000)      /* Babbling receiver */
818 #define FEC_ENET_BABT   ((uint)0x20000000)      /* Babbling transmitter */
819 #define FEC_ENET_GRA    ((uint)0x10000000)      /* Graceful stop complete */
820 #define FEC_ENET_TXF    ((uint)0x08000000)      /* Full frame transmitted */
821 #define FEC_ENET_TXB    ((uint)0x04000000)      /* A buffer was transmitted */
822 #define FEC_ENET_RXF    ((uint)0x02000000)      /* Full frame received */
823 #define FEC_ENET_RXB    ((uint)0x01000000)      /* A buffer was received */
824 #define FEC_ENET_MII    ((uint)0x00800000)      /* MII interrupt */
825 #define FEC_ENET_EBERR  ((uint)0x00400000)      /* SDMA bus error */
826
827 /* PHY identification
828  */
829 #define PHY_ID_LXT970           0x78100000      /* LXT970 */
830 #define PHY_ID_LXT971           0x001378e0      /* LXT971 and 972 */
831 #define PHY_ID_82555            0x02a80150      /* Intel 82555 */
832 #define PHY_ID_QS6612           0x01814400      /* QS6612 */
833 #define PHY_ID_AMD79C784        0x00225610      /* AMD 79C784 */
834 #define PHY_ID_LSI80225         0x0016f870      /* LSI 80225 */
835 #define PHY_ID_LSI80225B        0x0016f880      /* LSI 80225/B */
836 #define PHY_ID_DM9161           0x0181B880      /* Davicom DM9161 */
837 #define PHY_ID_KSM8995M         0x00221450      /* MICREL KS8995MA */
838
839 /* send command to phy using mii, wait for result */
840 static uint
841 mii_send(uint mii_cmd)
842 {
843         uint mii_reply;
844         volatile fec_t  *ep;
845         int cnt;
846
847         ep = &(((immap_t *)CFG_IMMR)->im_cpm.cp_fec);
848
849         ep->fec_mii_data = mii_cmd;     /* command to phy */
850
851         /* wait for mii complete */
852         cnt = 0;
853         while (!(ep->fec_ievent & FEC_ENET_MII)) {
854                 if (++cnt > 1000) {
855                         printf("mii_send STUCK!\n");
856                         break;
857                 }
858         }
859         mii_reply = ep->fec_mii_data;           /* result from phy */
860         ep->fec_ievent = FEC_ENET_MII;          /* clear MII complete */
861 #if 0
862         printf("%s[%d] %s: sent=0x%8.8x, reply=0x%8.8x\n",
863                 __FILE__,__LINE__,__FUNCTION__,mii_cmd,mii_reply);
864 #endif
865         return (mii_reply & 0xffff);            /* data read from phy */
866 }
867 #endif
868
869 #if defined(CFG_DISCOVER_PHY)
870 static int mii_discover_phy(struct eth_device *dev)
871 {
872 #define MAX_PHY_PASSES 11
873         uint phyno;
874         int  pass;
875         uint phytype;
876         int phyaddr;
877
878         phyaddr = -1;   /* didn't find a PHY yet */
879         for (pass = 1; pass <= MAX_PHY_PASSES && phyaddr < 0; ++pass) {
880                 if (pass > 1) {
881                         /* PHY may need more time to recover from reset.
882                          * The LXT970 needs 50ms typical, no maximum is
883                          * specified, so wait 10ms before try again.
884                          * With 11 passes this gives it 100ms to wake up.
885                          */
886                         udelay(10000);  /* wait 10ms */
887                 }
888                 for (phyno = 0; phyno < 32 && phyaddr < 0; ++phyno) {
889                         phytype = mii_send(mk_mii_read(phyno, PHY_PHYIDR2));
890 #ifdef ET_DEBUG
891                         printf("PHY type 0x%x pass %d type ", phytype, pass);
892 #endif
893                         if (phytype != 0xffff) {
894                                 phyaddr = phyno;
895                                 phytype |= mii_send(mk_mii_read(phyno,
896                                                                 PHY_PHYIDR1)) << 16;
897
898 #ifdef ET_DEBUG
899                                 printf("PHY @ 0x%x pass %d type ",phyno,pass);
900                                 switch (phytype & 0xfffffff0) {
901                                 case PHY_ID_LXT970:
902                                         printf("LXT970\n");
903                                         break;
904                                 case PHY_ID_LXT971:
905                                         printf("LXT971\n");
906                                         break;
907                                 case PHY_ID_82555:
908                                         printf("82555\n");
909                                         break;
910                                 case PHY_ID_QS6612:
911                                         printf("QS6612\n");
912                                         break;
913                                 case PHY_ID_AMD79C784:
914                                         printf("AMD79C784\n");
915                                         break;
916                                 case PHY_ID_LSI80225B:
917                                         printf("LSI L80225/B\n");
918                                         break;
919                                 case PHY_ID_DM9161:
920                                         printf("Davicom DM9161\n");
921                                         break;
922                                 case PHY_ID_KSM8995M:
923                                         printf("MICREL KS8995M\n");
924                                         break;
925                                 default:
926                                         printf("0x%08x\n", phytype);
927                                         break;
928                                 }
929 #endif
930                         }
931                 }
932         }
933         if (phyaddr < 0) {
934                 printf("No PHY device found.\n");
935         }
936         return phyaddr;
937 }
938 #endif  /* CFG_DISCOVER_PHY */
939
940 #if (defined(CONFIG_MII) || defined(CONFIG_CMD_MII)) && !defined(CONFIG_BITBANGMII)
941
942 /****************************************************************************
943  * mii_init -- Initialize the MII via FEC 1 for MII command without ethernet
944  * This function is a subset of eth_init
945  ****************************************************************************
946  */
947 static void __mii_init(void)
948 {
949         volatile immap_t *immr = (immap_t *) CFG_IMMR;
950         volatile fec_t *fecp = &(immr->im_cpm.cp_fec);
951
952         if (fec_reset(fecp) < 0)
953                 printf ("FEC_RESET_DELAY timeout\n");
954
955         /* We use strictly polling mode only
956          */
957         fecp->fec_imask = 0;
958
959         /* Clear any pending interrupt
960          */
961         fecp->fec_ievent = 0xffc0;
962
963         /* Now enable the transmit and receive processing
964          */
965         fecp->fec_ecntrl = FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN;
966 }
967
968 void mii_init (void)
969 {
970         int i;
971
972         __mii_init();
973
974         /* Setup the pin configuration of the FEC(s)
975         */
976         for (i = 0; i < sizeof(ether_fcc_info) / sizeof(ether_fcc_info[0]); i++)
977                 fec_pin_init(ether_fcc_info[i].ether_index);
978 }
979
980 /*****************************************************************************
981  * Read and write a MII PHY register, routines used by MII Utilities
982  *
983  * FIXME: These routines are expected to return 0 on success, but mii_send
984  *        does _not_ return an error code. Maybe 0xFFFF means error, i.e.
985  *        no PHY connected...
986  *        For now always return 0.
987  * FIXME: These routines only work after calling eth_init() at least once!
988  *        Otherwise they hang in mii_send() !!! Sorry!
989  *****************************************************************************/
990
991 int fec8xx_miiphy_read(char *devname, unsigned char addr,
992                 unsigned char  reg, unsigned short *value)
993 {
994         short rdreg;    /* register working value */
995
996 #ifdef MII_DEBUG
997         printf ("miiphy_read(0x%x) @ 0x%x = ", reg, addr);
998 #endif
999         rdreg = mii_send(mk_mii_read(addr, reg));
1000
1001         *value = rdreg;
1002 #ifdef MII_DEBUG
1003         printf ("0x%04x\n", *value);
1004 #endif
1005         return 0;
1006 }
1007
1008 int fec8xx_miiphy_write(char *devname, unsigned char  addr,
1009                 unsigned char  reg, unsigned short value)
1010 {
1011         short rdreg;    /* register working value */
1012 #ifdef MII_DEBUG
1013         printf ("miiphy_write(0x%x) @ 0x%x = ", reg, addr);
1014 #endif
1015         rdreg = mii_send(mk_mii_write(addr, reg, value));
1016
1017 #ifdef MII_DEBUG
1018         printf ("0x%04x\n", value);
1019 #endif
1020         return 0;
1021 }
1022 #endif
1023
1024 #endif