69b2174ab3d676280f38083026977804da53b202
[oweals/u-boot.git] / drivers / net / mpc8xx_fec.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2000
4  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5  */
6
7 #include <common.h>
8 #include <command.h>
9 #include <hang.h>
10 #include <malloc.h>
11 #include <net.h>
12 #include <netdev.h>
13 #include <asm/cpm_8xx.h>
14 #include <asm/io.h>
15
16 #include <phy.h>
17 #include <linux/mii.h>
18
19 DECLARE_GLOBAL_DATA_PTR;
20
21 /* define WANT_MII when MII support is required */
22 #if defined(CONFIG_SYS_DISCOVER_PHY) || defined(CONFIG_FEC1_PHY) || defined(CONFIG_FEC2_PHY)
23 #define WANT_MII
24 #else
25 #undef WANT_MII
26 #endif
27
28 #if defined(WANT_MII)
29 #include <miiphy.h>
30
31 #if !(defined(CONFIG_MII) || defined(CONFIG_CMD_MII))
32 #error "CONFIG_MII has to be defined!"
33 #endif
34
35 #endif
36
37 #if defined(CONFIG_RMII) && !defined(WANT_MII)
38 #error RMII support is unusable without a working PHY.
39 #endif
40
41 #ifdef CONFIG_SYS_DISCOVER_PHY
42 static int mii_discover_phy(struct eth_device *dev);
43 #endif
44
45 int fec8xx_miiphy_read(struct mii_dev *bus, int addr, int devad, int reg);
46 int fec8xx_miiphy_write(struct mii_dev *bus, int addr, int devad, int reg,
47                         u16 value);
48
49 static struct ether_fcc_info_s
50 {
51         int ether_index;
52         int fecp_offset;
53         int phy_addr;
54         int actual_phy_addr;
55         int initialized;
56 }
57         ether_fcc_info[] = {
58 #if defined(CONFIG_ETHER_ON_FEC1)
59         {
60                 0,
61                 offsetof(immap_t, im_cpm.cp_fec1),
62                 CONFIG_FEC1_PHY,
63                 -1,
64                 0,
65
66         },
67 #endif
68 #if defined(CONFIG_ETHER_ON_FEC2)
69         {
70                 1,
71                 offsetof(immap_t, im_cpm.cp_fec2),
72                 CONFIG_FEC2_PHY,
73                 -1,
74                 0,
75         },
76 #endif
77 };
78
79 /* Ethernet Transmit and Receive Buffers */
80 #define DBUF_LENGTH  1520
81
82 #define TX_BUF_CNT 2
83
84 #define TOUT_LOOP 100
85
86 #define PKT_MAXBUF_SIZE         1518
87 #define PKT_MINBUF_SIZE         64
88 #define PKT_MAXBLR_SIZE         1520
89
90 #ifdef __GNUC__
91 static char txbuf[DBUF_LENGTH] __aligned(8);
92 #else
93 #error txbuf must be aligned.
94 #endif
95
96 static uint rxIdx;      /* index of the current RX buffer */
97 static uint txIdx;      /* index of the current TX buffer */
98
99 /*
100   * FEC Ethernet Tx and Rx buffer descriptors allocated at the
101   *  immr->udata_bd address on Dual-Port RAM
102   * Provide for Double Buffering
103   */
104
105 struct common_buf_desc {
106         cbd_t rxbd[PKTBUFSRX];          /* Rx BD */
107         cbd_t txbd[TX_BUF_CNT];         /* Tx BD */
108 };
109
110 static struct common_buf_desc __iomem *rtx;
111
112 static int fec_send(struct eth_device *dev, void *packet, int length);
113 static int fec_recv(struct eth_device *dev);
114 static int fec_init(struct eth_device *dev, bd_t *bd);
115 static void fec_halt(struct eth_device *dev);
116 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
117 static void __mii_init(void);
118 #endif
119
120 int fec_initialize(bd_t *bis)
121 {
122         struct eth_device *dev;
123         struct ether_fcc_info_s *efis;
124         int             i;
125
126         for (i = 0; i < ARRAY_SIZE(ether_fcc_info); i++) {
127                 dev = malloc(sizeof(*dev));
128                 if (dev == NULL)
129                         hang();
130
131                 memset(dev, 0, sizeof(*dev));
132
133                 /* for FEC1 make sure that the name of the interface is the same
134                    as the old one for compatibility reasons */
135                 if (i == 0)
136                         strcpy(dev->name, "FEC");
137                 else
138                         sprintf(dev->name, "FEC%d",
139                                 ether_fcc_info[i].ether_index + 1);
140
141                 efis = &ether_fcc_info[i];
142
143                 /*
144                  * reset actual phy addr
145                  */
146                 efis->actual_phy_addr = -1;
147
148                 dev->priv = efis;
149                 dev->init = fec_init;
150                 dev->halt = fec_halt;
151                 dev->send = fec_send;
152                 dev->recv = fec_recv;
153
154                 eth_register(dev);
155
156 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
157                 int retval;
158                 struct mii_dev *mdiodev = mdio_alloc();
159                 if (!mdiodev)
160                         return -ENOMEM;
161                 strncpy(mdiodev->name, dev->name, MDIO_NAME_LEN);
162                 mdiodev->read = fec8xx_miiphy_read;
163                 mdiodev->write = fec8xx_miiphy_write;
164
165                 retval = mdio_register(mdiodev);
166                 if (retval < 0)
167                         return retval;
168 #endif
169         }
170         return 1;
171 }
172
173 static int fec_send(struct eth_device *dev, void *packet, int length)
174 {
175         int j, rc;
176         struct ether_fcc_info_s *efis = dev->priv;
177         fec_t __iomem *fecp =
178                         (fec_t __iomem *)(CONFIG_SYS_IMMR + efis->fecp_offset);
179
180         /* section 16.9.23.3
181          * Wait for ready
182          */
183         j = 0;
184         while ((in_be16(&rtx->txbd[txIdx].cbd_sc) & BD_ENET_TX_READY) &&
185                (j < TOUT_LOOP)) {
186                 udelay(1);
187                 j++;
188         }
189         if (j >= TOUT_LOOP)
190                 printf("TX not ready\n");
191
192         out_be32(&rtx->txbd[txIdx].cbd_bufaddr, (uint)packet);
193         out_be16(&rtx->txbd[txIdx].cbd_datlen, length);
194         setbits_be16(&rtx->txbd[txIdx].cbd_sc,
195                      BD_ENET_TX_READY | BD_ENET_TX_LAST);
196
197         /* Activate transmit Buffer Descriptor polling */
198         /* Descriptor polling active    */
199         out_be32(&fecp->fec_x_des_active, 0x01000000);
200
201         j = 0;
202         while ((in_be16(&rtx->txbd[txIdx].cbd_sc) & BD_ENET_TX_READY) &&
203                (j < TOUT_LOOP)) {
204                 udelay(1);
205                 j++;
206         }
207         if (j >= TOUT_LOOP)
208                 printf("TX timeout\n");
209
210         /* return only status bits */;
211         rc = in_be16(&rtx->txbd[txIdx].cbd_sc) & BD_ENET_TX_STATS;
212
213         txIdx = (txIdx + 1) % TX_BUF_CNT;
214
215         return rc;
216 }
217
218 static int fec_recv(struct eth_device *dev)
219 {
220         struct ether_fcc_info_s *efis = dev->priv;
221         fec_t __iomem *fecp =
222                         (fec_t __iomem *)(CONFIG_SYS_IMMR + efis->fecp_offset);
223         int length;
224
225         for (;;) {
226                 /* section 16.9.23.2 */
227                 if (in_be16(&rtx->rxbd[rxIdx].cbd_sc) & BD_ENET_RX_EMPTY) {
228                         length = -1;
229                         break;  /* nothing received - leave for() loop */
230                 }
231
232                 length = in_be16(&rtx->rxbd[rxIdx].cbd_datlen);
233
234                 if (!(in_be16(&rtx->rxbd[rxIdx].cbd_sc) & 0x003f)) {
235                         uchar *rx = net_rx_packets[rxIdx];
236
237                         length -= 4;
238
239 #if defined(CONFIG_CMD_CDP)
240                         if ((rx[0] & 1) != 0 &&
241                             memcmp((uchar *)rx, net_bcast_ethaddr, 6) != 0 &&
242                             !is_cdp_packet((uchar *)rx))
243                                 rx = NULL;
244 #endif
245                         /*
246                          * Pass the packet up to the protocol layers.
247                          */
248                         if (rx != NULL)
249                                 net_process_received_packet(rx, length);
250                 }
251
252                 /* Give the buffer back to the FEC. */
253                 out_be16(&rtx->rxbd[rxIdx].cbd_datlen, 0);
254
255                 /* wrap around buffer index when necessary */
256                 if ((rxIdx + 1) >= PKTBUFSRX) {
257                         out_be16(&rtx->rxbd[PKTBUFSRX - 1].cbd_sc,
258                                  BD_ENET_RX_WRAP | BD_ENET_RX_EMPTY);
259                         rxIdx = 0;
260                 } else {
261                         out_be16(&rtx->rxbd[rxIdx].cbd_sc, BD_ENET_RX_EMPTY);
262                         rxIdx++;
263                 }
264
265                 /* Try to fill Buffer Descriptors */
266                 /* Descriptor polling active    */
267                 out_be32(&fecp->fec_r_des_active, 0x01000000);
268         }
269
270         return length;
271 }
272
273 /**************************************************************
274  *
275  * FEC Ethernet Initialization Routine
276  *
277  *************************************************************/
278
279 #define FEC_ECNTRL_PINMUX       0x00000004
280 #define FEC_ECNTRL_ETHER_EN     0x00000002
281 #define FEC_ECNTRL_RESET        0x00000001
282
283 #define FEC_RCNTRL_BC_REJ       0x00000010
284 #define FEC_RCNTRL_PROM         0x00000008
285 #define FEC_RCNTRL_MII_MODE     0x00000004
286 #define FEC_RCNTRL_DRT          0x00000002
287 #define FEC_RCNTRL_LOOP         0x00000001
288
289 #define FEC_TCNTRL_FDEN         0x00000004
290 #define FEC_TCNTRL_HBC          0x00000002
291 #define FEC_TCNTRL_GTS          0x00000001
292
293 #define FEC_RESET_DELAY         50
294
295 #if defined(CONFIG_RMII)
296
297 static inline void fec_10Mbps(struct eth_device *dev)
298 {
299         struct ether_fcc_info_s *efis = dev->priv;
300         int fecidx = efis->ether_index;
301         uint mask = (fecidx == 0) ? 0x0000010 : 0x0000008;
302         immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR;
303
304         if ((unsigned int)fecidx >= 2)
305                 hang();
306
307         setbits_be32(&immr->im_cpm.cp_cptr, mask);
308 }
309
310 static inline void fec_100Mbps(struct eth_device *dev)
311 {
312         struct ether_fcc_info_s *efis = dev->priv;
313         int fecidx = efis->ether_index;
314         uint mask = (fecidx == 0) ? 0x0000010 : 0x0000008;
315         immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR;
316
317         if ((unsigned int)fecidx >= 2)
318                 hang();
319
320         clrbits_be32(&immr->im_cpm.cp_cptr, mask);
321 }
322
323 #endif
324
325 static inline void fec_full_duplex(struct eth_device *dev)
326 {
327         struct ether_fcc_info_s *efis = dev->priv;
328         fec_t __iomem *fecp =
329                         (fec_t __iomem *)(CONFIG_SYS_IMMR + efis->fecp_offset);
330
331         clrbits_be32(&fecp->fec_r_cntrl, FEC_RCNTRL_DRT);
332         setbits_be32(&fecp->fec_x_cntrl,  FEC_TCNTRL_FDEN);     /* FD enable */
333 }
334
335 static inline void fec_half_duplex(struct eth_device *dev)
336 {
337         struct ether_fcc_info_s *efis = dev->priv;
338         fec_t __iomem *fecp =
339                         (fec_t __iomem *)(CONFIG_SYS_IMMR + efis->fecp_offset);
340
341         setbits_be32(&fecp->fec_r_cntrl, FEC_RCNTRL_DRT);
342         clrbits_be32(&fecp->fec_x_cntrl,  FEC_TCNTRL_FDEN);     /* FD disable */
343 }
344
345 static void fec_pin_init(int fecidx)
346 {
347         bd_t           *bd = gd->bd;
348         immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR;
349
350         /*
351          * Set MII speed to 2.5 MHz or slightly below.
352          *
353          * According to the MPC860T (Rev. D) Fast ethernet controller user
354          * manual (6.2.14),
355          * the MII management interface clock must be less than or equal
356          * to 2.5 MHz.
357          * This MDC frequency is equal to system clock / (2 * MII_SPEED).
358          * Then MII_SPEED = system_clock / 2 * 2,5 MHz.
359          *
360          * All MII configuration is done via FEC1 registers:
361          */
362         out_be32(&immr->im_cpm.cp_fec1.fec_mii_speed,
363                  ((bd->bi_intfreq + 4999999) / 5000000) << 1);
364
365 #if defined(CONFIG_MPC885) && defined(WANT_MII)
366         /* use MDC for MII */
367         setbits_be16(&immr->im_ioport.iop_pdpar, 0x0080);
368         clrbits_be16(&immr->im_ioport.iop_pddir, 0x0080);
369 #endif
370
371         if (fecidx == 0) {
372 #if defined(CONFIG_ETHER_ON_FEC1)
373
374 #if defined(CONFIG_MPC885) /* MPC87x/88x have got 2 FECs and different pinout */
375
376 #if !defined(CONFIG_RMII)
377
378                 setbits_be16(&immr->im_ioport.iop_papar, 0xf830);
379                 setbits_be16(&immr->im_ioport.iop_padir, 0x0830);
380                 clrbits_be16(&immr->im_ioport.iop_padir, 0xf000);
381
382                 setbits_be32(&immr->im_cpm.cp_pbpar, 0x00001001);
383                 clrbits_be32(&immr->im_cpm.cp_pbdir, 0x00001001);
384
385                 setbits_be16(&immr->im_ioport.iop_pcpar, 0x000c);
386                 clrbits_be16(&immr->im_ioport.iop_pcdir, 0x000c);
387
388                 setbits_be32(&immr->im_cpm.cp_pepar, 0x00000003);
389                 setbits_be32(&immr->im_cpm.cp_pedir, 0x00000003);
390                 clrbits_be32(&immr->im_cpm.cp_peso, 0x00000003);
391
392                 clrbits_be32(&immr->im_cpm.cp_cptr, 0x00000100);
393
394 #else
395
396 #if !defined(CONFIG_FEC1_PHY_NORXERR)
397                 setbits_be16(&immr->im_ioport.iop_papar, 0x1000);
398                 clrbits_be16(&immr->im_ioport.iop_padir, 0x1000);
399 #endif
400                 setbits_be16(&immr->im_ioport.iop_papar, 0xe810);
401                 setbits_be16(&immr->im_ioport.iop_padir, 0x0810);
402                 clrbits_be16(&immr->im_ioport.iop_padir, 0xe000);
403
404                 setbits_be32(&immr->im_cpm.cp_pbpar, 0x00000001);
405                 clrbits_be32(&immr->im_cpm.cp_pbdir, 0x00000001);
406
407                 setbits_be32(&immr->im_cpm.cp_cptr, 0x00000100);
408                 clrbits_be32(&immr->im_cpm.cp_cptr, 0x00000050);
409
410 #endif /* !CONFIG_RMII */
411
412 #else
413                 /*
414                  * Configure all of port D for MII.
415                  */
416                 out_be16(&immr->im_ioport.iop_pdpar, 0x1fff);
417                 out_be16(&immr->im_ioport.iop_pddir, 0x1fff);
418
419 #if defined(CONFIG_TARGET_MCR3000)
420                 out_be16(&immr->im_ioport.iop_papar, 0xBBFF);
421                 out_be16(&immr->im_ioport.iop_padir, 0x04F0);
422                 out_be16(&immr->im_ioport.iop_paodr, 0x0000);
423
424                 out_be32(&immr->im_cpm.cp_pbpar, 0x000133FF);
425                 out_be32(&immr->im_cpm.cp_pbdir, 0x0003BF0F);
426                 out_be16(&immr->im_cpm.cp_pbodr, 0x0000);
427
428                 out_be16(&immr->im_ioport.iop_pcpar, 0x0400);
429                 out_be16(&immr->im_ioport.iop_pcdir, 0x0080);
430                 out_be16(&immr->im_ioport.iop_pcso , 0x0D53);
431                 out_be16(&immr->im_ioport.iop_pcint, 0x0000);
432
433                 out_be16(&immr->im_ioport.iop_pdpar, 0x03FE);
434                 out_be16(&immr->im_ioport.iop_pddir, 0x1C09);
435
436                 setbits_be32(&immr->im_ioport.utmode, 0x80);
437 #endif
438 #endif
439
440 #endif  /* CONFIG_ETHER_ON_FEC1 */
441         } else if (fecidx == 1) {
442 #if defined(CONFIG_ETHER_ON_FEC2)
443
444 #if defined(CONFIG_MPC885) /* MPC87x/88x have got 2 FECs and different pinout */
445
446 #if !defined(CONFIG_RMII)
447                 setbits_be32(&immr->im_cpm.cp_pepar, 0x0003fffc);
448                 setbits_be32(&immr->im_cpm.cp_pedir, 0x0003fffc);
449                 clrbits_be32(&immr->im_cpm.cp_peso, 0x000087fc);
450                 setbits_be32(&immr->im_cpm.cp_peso, 0x00037800);
451
452                 clrbits_be32(&immr->im_cpm.cp_cptr, 0x00000080);
453 #else
454
455 #if !defined(CONFIG_FEC2_PHY_NORXERR)
456                 setbits_be32(&immr->im_cpm.cp_pepar, 0x00000010);
457                 setbits_be32(&immr->im_cpm.cp_pedir, 0x00000010);
458                 clrbits_be32(&immr->im_cpm.cp_peso, 0x00000010);
459 #endif
460                 setbits_be32(&immr->im_cpm.cp_pepar, 0x00039620);
461                 setbits_be32(&immr->im_cpm.cp_pedir, 0x00039620);
462                 setbits_be32(&immr->im_cpm.cp_peso, 0x00031000);
463                 clrbits_be32(&immr->im_cpm.cp_peso, 0x00008620);
464
465                 setbits_be32(&immr->im_cpm.cp_cptr, 0x00000080);
466                 clrbits_be32(&immr->im_cpm.cp_cptr, 0x00000028);
467 #endif /* CONFIG_RMII */
468
469 #endif /* CONFIG_MPC885 */
470
471 #endif /* CONFIG_ETHER_ON_FEC2 */
472         }
473 }
474
475 static int fec_reset(fec_t __iomem *fecp)
476 {
477         int i;
478
479         /* Whack a reset.
480          * A delay is required between a reset of the FEC block and
481          * initialization of other FEC registers because the reset takes
482          * some time to complete. If you don't delay, subsequent writes
483          * to FEC registers might get killed by the reset routine which is
484          * still in progress.
485          */
486
487         out_be32(&fecp->fec_ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_RESET);
488         for (i = 0; (in_be32(&fecp->fec_ecntrl) & FEC_ECNTRL_RESET) &&
489              (i < FEC_RESET_DELAY); ++i)
490                 udelay(1);
491
492         if (i == FEC_RESET_DELAY)
493                 return -1;
494
495         return 0;
496 }
497
498 static int fec_init(struct eth_device *dev, bd_t *bd)
499 {
500         struct ether_fcc_info_s *efis = dev->priv;
501         immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR;
502         fec_t __iomem *fecp =
503                         (fec_t __iomem *)(CONFIG_SYS_IMMR + efis->fecp_offset);
504         int i;
505
506 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
507         /* the MII interface is connected to FEC1
508          * so for the miiphy_xxx function to work we must
509          * call mii_init since fec_halt messes the thing up
510          */
511         if (efis->ether_index != 0)
512                 __mii_init();
513 #endif
514
515         if (fec_reset(fecp) < 0)
516                 printf("FEC_RESET_DELAY timeout\n");
517
518         /* We use strictly polling mode only
519          */
520         out_be32(&fecp->fec_imask, 0);
521
522         /* Clear any pending interrupt
523          */
524         out_be32(&fecp->fec_ievent, 0xffc0);
525
526         /* No need to set the IVEC register */
527
528         /* Set station address
529          */
530 #define ea dev->enetaddr
531         out_be32(&fecp->fec_addr_low, (ea[0] << 24) | (ea[1] << 16) |
532                                       (ea[2] << 8) | ea[3]);
533         out_be16(&fecp->fec_addr_high, (ea[4] << 8) | ea[5]);
534 #undef ea
535
536 #if defined(CONFIG_CMD_CDP)
537         /*
538          * Turn on multicast address hash table
539          */
540         out_be32(&fecp->fec_hash_table_high, 0xffffffff);
541         out_be32(&fecp->fec_hash_table_low, 0xffffffff);
542 #else
543         /* Clear multicast address hash table
544          */
545         out_be32(&fecp->fec_hash_table_high, 0);
546         out_be32(&fecp->fec_hash_table_low, 0);
547 #endif
548
549         /* Set maximum receive buffer size.
550          */
551         out_be32(&fecp->fec_r_buff_size, PKT_MAXBLR_SIZE);
552
553         /* Set maximum frame length
554          */
555         out_be32(&fecp->fec_r_hash, PKT_MAXBUF_SIZE);
556
557         /*
558          * Setup Buffers and Buffer Descriptors
559          */
560         rxIdx = 0;
561         txIdx = 0;
562
563         if (!rtx)
564                 rtx = (struct common_buf_desc __iomem *)
565                       (immr->im_cpm.cp_dpmem + CPM_FEC_BASE);
566         /*
567          * Setup Receiver Buffer Descriptors (13.14.24.18)
568          * Settings:
569          *     Empty, Wrap
570          */
571         for (i = 0; i < PKTBUFSRX; i++) {
572                 out_be16(&rtx->rxbd[i].cbd_sc, BD_ENET_RX_EMPTY);
573                 out_be16(&rtx->rxbd[i].cbd_datlen, 0);  /* Reset */
574                 out_be32(&rtx->rxbd[i].cbd_bufaddr, (uint)net_rx_packets[i]);
575         }
576         setbits_be16(&rtx->rxbd[PKTBUFSRX - 1].cbd_sc, BD_ENET_RX_WRAP);
577
578         /*
579          * Setup Ethernet Transmitter Buffer Descriptors (13.14.24.19)
580          * Settings:
581          *    Last, Tx CRC
582          */
583         for (i = 0; i < TX_BUF_CNT; i++) {
584                 out_be16(&rtx->txbd[i].cbd_sc, BD_ENET_TX_LAST | BD_ENET_TX_TC);
585                 out_be16(&rtx->txbd[i].cbd_datlen, 0);  /* Reset */
586                 out_be32(&rtx->txbd[i].cbd_bufaddr, (uint)txbuf);
587         }
588         setbits_be16(&rtx->txbd[TX_BUF_CNT - 1].cbd_sc, BD_ENET_TX_WRAP);
589
590         /* Set receive and transmit descriptor base
591          */
592         out_be32(&fecp->fec_r_des_start, (__force unsigned int)rtx->rxbd);
593         out_be32(&fecp->fec_x_des_start, (__force unsigned int)rtx->txbd);
594
595         /* Enable MII mode
596          */
597         /* Half duplex mode */
598         out_be32(&fecp->fec_r_cntrl, FEC_RCNTRL_MII_MODE | FEC_RCNTRL_DRT);
599         out_be32(&fecp->fec_x_cntrl, 0);
600
601         /* Enable big endian and don't care about SDMA FC.
602          */
603         out_be32(&fecp->fec_fun_code, 0x78000000);
604
605         /*
606          * Setup the pin configuration of the FEC
607          */
608         fec_pin_init(efis->ether_index);
609
610         rxIdx = 0;
611         txIdx = 0;
612
613         /*
614          * Now enable the transmit and receive processing
615          */
616         out_be32(&fecp->fec_ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN);
617
618         if (efis->phy_addr == -1) {
619 #ifdef CONFIG_SYS_DISCOVER_PHY
620                 /*
621                  * wait for the PHY to wake up after reset
622                  */
623                 efis->actual_phy_addr = mii_discover_phy(dev);
624
625                 if (efis->actual_phy_addr == -1) {
626                         printf("Unable to discover phy!\n");
627                         return -1;
628                 }
629 #else
630                 efis->actual_phy_addr = -1;
631 #endif
632         } else {
633                 efis->actual_phy_addr = efis->phy_addr;
634         }
635
636 #if defined(CONFIG_MII) && defined(CONFIG_RMII)
637         /*
638          * adapt the RMII speed to the speed of the phy
639          */
640         if (miiphy_speed(dev->name, efis->actual_phy_addr) == _100BASET)
641                 fec_100Mbps(dev);
642         else
643                 fec_10Mbps(dev);
644 #endif
645
646 #if defined(CONFIG_MII)
647         /*
648          * adapt to the half/full speed settings
649          */
650         if (miiphy_duplex(dev->name, efis->actual_phy_addr) == FULL)
651                 fec_full_duplex(dev);
652         else
653                 fec_half_duplex(dev);
654 #endif
655
656         /* And last, try to fill Rx Buffer Descriptors */
657         /* Descriptor polling active    */
658         out_be32(&fecp->fec_r_des_active, 0x01000000);
659
660         efis->initialized = 1;
661
662         return 0;
663 }
664
665
666 static void fec_halt(struct eth_device *dev)
667 {
668         struct ether_fcc_info_s *efis = dev->priv;
669         fec_t __iomem *fecp =
670                         (fec_t __iomem *)(CONFIG_SYS_IMMR + efis->fecp_offset);
671         int i;
672
673         /* avoid halt if initialized; mii gets stuck otherwise */
674         if (!efis->initialized)
675                 return;
676
677         /* Whack a reset.
678          * A delay is required between a reset of the FEC block and
679          * initialization of other FEC registers because the reset takes
680          * some time to complete. If you don't delay, subsequent writes
681          * to FEC registers might get killed by the reset routine which is
682          * still in progress.
683          */
684
685         out_be32(&fecp->fec_ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_RESET);
686         for (i = 0; (in_be32(&fecp->fec_ecntrl) & FEC_ECNTRL_RESET) &&
687              (i < FEC_RESET_DELAY); ++i)
688                 udelay(1);
689
690         if (i == FEC_RESET_DELAY) {
691                 printf("FEC_RESET_DELAY timeout\n");
692                 return;
693         }
694
695         efis->initialized = 0;
696 }
697
698 #if defined(CONFIG_SYS_DISCOVER_PHY) || defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
699
700 /* Make MII read/write commands for the FEC.
701 */
702
703 #define mk_mii_read(ADDR, REG)  (0x60020000 | ((ADDR << 23) | \
704                                                 (REG & 0x1f) << 18))
705
706 #define mk_mii_write(ADDR, REG, VAL)    (0x50020000 | ((ADDR << 23) | \
707                                                 (REG & 0x1f) << 18) | \
708                                                 (VAL & 0xffff))
709
710 /* Interrupt events/masks.
711 */
712 #define FEC_ENET_HBERR  ((uint)0x80000000)      /* Heartbeat error */
713 #define FEC_ENET_BABR   ((uint)0x40000000)      /* Babbling receiver */
714 #define FEC_ENET_BABT   ((uint)0x20000000)      /* Babbling transmitter */
715 #define FEC_ENET_GRA    ((uint)0x10000000)      /* Graceful stop complete */
716 #define FEC_ENET_TXF    ((uint)0x08000000)      /* Full frame transmitted */
717 #define FEC_ENET_TXB    ((uint)0x04000000)      /* A buffer was transmitted */
718 #define FEC_ENET_RXF    ((uint)0x02000000)      /* Full frame received */
719 #define FEC_ENET_RXB    ((uint)0x01000000)      /* A buffer was received */
720 #define FEC_ENET_MII    ((uint)0x00800000)      /* MII interrupt */
721 #define FEC_ENET_EBERR  ((uint)0x00400000)      /* SDMA bus error */
722
723 /* send command to phy using mii, wait for result */
724 static uint
725 mii_send(uint mii_cmd)
726 {
727         uint mii_reply;
728         fec_t __iomem *ep;
729         int cnt;
730         immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR;
731
732         ep = &immr->im_cpm.cp_fec;
733
734         out_be32(&ep->fec_mii_data, mii_cmd);   /* command to phy */
735
736         /* wait for mii complete */
737         cnt = 0;
738         while (!(in_be32(&ep->fec_ievent) & FEC_ENET_MII)) {
739                 if (++cnt > 1000) {
740                         printf("mii_send STUCK!\n");
741                         break;
742                 }
743         }
744         mii_reply = in_be32(&ep->fec_mii_data);         /* result from phy */
745         out_be32(&ep->fec_ievent, FEC_ENET_MII);        /* clear MII complete */
746         return mii_reply & 0xffff;              /* data read from phy */
747 }
748 #endif
749
750 #if defined(CONFIG_SYS_DISCOVER_PHY)
751 static int mii_discover_phy(struct eth_device *dev)
752 {
753 #define MAX_PHY_PASSES 11
754         uint phyno;
755         int  pass;
756         uint phytype;
757         int phyaddr;
758
759         phyaddr = -1;   /* didn't find a PHY yet */
760         for (pass = 1; pass <= MAX_PHY_PASSES && phyaddr < 0; ++pass) {
761                 if (pass > 1) {
762                         /* PHY may need more time to recover from reset.
763                          * The LXT970 needs 50ms typical, no maximum is
764                          * specified, so wait 10ms before try again.
765                          * With 11 passes this gives it 100ms to wake up.
766                          */
767                         udelay(10000);  /* wait 10ms */
768                 }
769                 for (phyno = 0; phyno < 32 && phyaddr < 0; ++phyno) {
770                         phytype = mii_send(mk_mii_read(phyno, MII_PHYSID2));
771                         if (phytype != 0xffff) {
772                                 phyaddr = phyno;
773                                 phytype |= mii_send(mk_mii_read(phyno,
774                                                                 MII_PHYSID1)) << 16;
775                         }
776                 }
777         }
778         if (phyaddr < 0)
779                 printf("No PHY device found.\n");
780
781         return phyaddr;
782 }
783 #endif  /* CONFIG_SYS_DISCOVER_PHY */
784
785 #if (defined(CONFIG_MII) || defined(CONFIG_CMD_MII)) && !defined(CONFIG_BITBANGMII)
786
787 /****************************************************************************
788  * mii_init -- Initialize the MII via FEC 1 for MII command without ethernet
789  * This function is a subset of eth_init
790  ****************************************************************************
791  */
792 static void __mii_init(void)
793 {
794         immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR;
795         fec_t __iomem *fecp = &immr->im_cpm.cp_fec;
796
797         if (fec_reset(fecp) < 0)
798                 printf("FEC_RESET_DELAY timeout\n");
799
800         /* We use strictly polling mode only
801          */
802         out_be32(&fecp->fec_imask, 0);
803
804         /* Clear any pending interrupt
805          */
806         out_be32(&fecp->fec_ievent, 0xffc0);
807
808         /* Now enable the transmit and receive processing
809          */
810         out_be32(&fecp->fec_ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN);
811 }
812
813 void mii_init(void)
814 {
815         int i;
816
817         __mii_init();
818
819         /* Setup the pin configuration of the FEC(s)
820         */
821         for (i = 0; i < ARRAY_SIZE(ether_fcc_info); i++)
822                 fec_pin_init(ether_fcc_info[i].ether_index);
823 }
824
825 /*****************************************************************************
826  * Read and write a MII PHY register, routines used by MII Utilities
827  *
828  * FIXME: These routines are expected to return 0 on success, but mii_send
829  *        does _not_ return an error code. Maybe 0xFFFF means error, i.e.
830  *        no PHY connected...
831  *        For now always return 0.
832  * FIXME: These routines only work after calling eth_init() at least once!
833  *        Otherwise they hang in mii_send() !!! Sorry!
834  *****************************************************************************/
835
836 int fec8xx_miiphy_read(struct mii_dev *bus, int addr, int devad, int reg)
837 {
838         unsigned short value = 0;
839         short rdreg;    /* register working value */
840
841         rdreg = mii_send(mk_mii_read(addr, reg));
842
843         value = rdreg;
844         return value;
845 }
846
847 int fec8xx_miiphy_write(struct mii_dev *bus, int addr, int devad, int reg,
848                         u16 value)
849 {
850         (void)mii_send(mk_mii_write(addr, reg, value));
851
852         return 0;
853 }
854 #endif