]> git.karo-electronics.de Git - karo-tx-uboot.git/blob - cpu/mpc85xx/tsec.c
0d858ed977bfb62505fbb460ca71fefb64e69632
[karo-tx-uboot.git] / cpu / mpc85xx / tsec.c
1 /*
2  * tsec.c
3  * Freescale Three Speed Ethernet Controller driver
4  *
5  * This software may be used and distributed according to the
6  * terms of the GNU Public License, Version 2, incorporated
7  * herein by reference.
8  *
9  * Copyright 2004 Freescale Semiconductor.
10  * (C) Copyright 2003, Motorola, Inc.
11  * maintained by Jon Loeliger (loeliger@freescale.com)
12  * author Andy Fleming
13  *
14  */
15
16 #include <config.h>
17 #include <mpc85xx.h>
18 #include <common.h>
19 #include <malloc.h>
20 #include <net.h>
21 #include <command.h>
22
23 #if defined(CONFIG_TSEC_ENET)
24 #include "tsec.h"
25
26 #define TX_BUF_CNT 2
27
28 #undef TSEC_DEBUG
29 #ifdef TSEC_DEBUG
30 #define DBGPRINT(x,y) printf(x,y)
31 #else
32 #define DBGPRINT(x,y)
33 #endif
34
35 static uint rxIdx;      /* index of the current RX buffer */
36 static uint txIdx;      /* index of the current TX buffer */
37
38 typedef volatile struct rtxbd {
39         txbd8_t txbd[TX_BUF_CNT];
40         rxbd8_t rxbd[PKTBUFSRX];
41 }  RTXBD;
42
43 struct tsec_info_struct {
44         unsigned int phyaddr;
45         unsigned int gigabit;
46         unsigned int phyregidx;
47 };
48
49
50 /* The tsec_info structure contains 3 values which the
51  * driver uses to determine how to operate a given ethernet
52  * device.  For now, the structure is initialized with the
53  * knowledge that all current implementations have 2 TSEC
54  * devices, and one FEC.  The information needed is:
55  *  phyaddr - The address of the PHY which is attached to
56  *      the given device.
57  *
58  *  gigabit - This variable indicates whether the device
59  *      supports gigabit speed ethernet
60  *
61  *  phyregidx - This variable specifies which ethernet device
62  *      controls the MII Management registers which are connected
63  *      to the PHY.  For 8540/8560, only TSEC1 (index 0) has
64  *      access to the PHYs, so all of the entries have "0".
65  *
66  * The values specified in the table are taken from the board's
67  * config file in include/configs/.  When implementing a new
68  * board with ethernet capability, it is necessary to define:
69  *   TSEC1_PHY_ADDR
70  *   TSEC1_PHYIDX
71  *   TSEC2_PHY_ADDR
72  *   TSEC2_PHYIDX
73  *
74  * and for 8560:
75  *   FEC_PHY_ADDR
76  *   FEC_PHYIDX
77  */
78 static struct tsec_info_struct tsec_info[] = {
79 #ifdef CONFIG_MPC85XX_TSEC1
80         {TSEC1_PHY_ADDR, 1, TSEC1_PHYIDX},
81 #endif
82 #ifdef CONFIG_MPC85XX_TSEC2
83         {TSEC2_PHY_ADDR, 1, TSEC2_PHYIDX},
84 #endif
85 #ifdef CONFIG_MPC85XX_FEC
86         {FEC_PHY_ADDR, 0, FEC_PHYIDX},
87 #endif
88 };
89
90 #define MAXCONTROLLERS 3
91
92 static int relocated = 0;
93
94 static struct tsec_private *privlist[MAXCONTROLLERS];
95
96 #ifdef __GNUC__
97 static RTXBD rtx __attribute__ ((aligned(8)));
98 #else
99 #error "rtx must be 64-bit aligned"
100 #endif
101
102 static int tsec_send(struct eth_device* dev, volatile void *packet, int length);
103 static int tsec_recv(struct eth_device* dev);
104 static int tsec_init(struct eth_device* dev, bd_t * bd);
105 static void tsec_halt(struct eth_device* dev);
106 static void init_registers(volatile tsec_t *regs);
107 static void startup_tsec(struct eth_device *dev);
108 static int init_phy(struct eth_device *dev);
109 void write_phy_reg(struct tsec_private *priv, uint regnum, uint value);
110 uint read_phy_reg(struct tsec_private *priv, uint regnum);
111 struct phy_info * get_phy_info(struct eth_device *dev);
112 void phy_run_commands(struct tsec_private *priv, struct phy_cmd *cmd);
113 static void adjust_link(struct eth_device *dev);
114 static void relocate_cmds(void);
115
116 /* Initialize device structure. Returns success if PHY
117  * initialization succeeded (i.e. if it recognizes the PHY)
118  */
119 int tsec_initialize(bd_t *bis, int index)
120 {
121         struct eth_device* dev;
122         int i;
123         struct tsec_private *priv;
124
125         dev = (struct eth_device*) malloc(sizeof *dev);
126
127         if(NULL == dev)
128                 return 0;
129
130         memset(dev, 0, sizeof *dev);
131
132         priv = (struct tsec_private *) malloc(sizeof(*priv));
133
134         if(NULL == priv)
135                 return 0;
136
137         privlist[index] = priv;
138         priv->regs = (volatile tsec_t *)(TSEC_BASE_ADDR + index*TSEC_SIZE);
139         priv->phyregs = (volatile tsec_t *)(TSEC_BASE_ADDR +
140                         tsec_info[index].phyregidx*TSEC_SIZE);
141
142         priv->phyaddr = tsec_info[index].phyaddr;
143         priv->gigabit = tsec_info[index].gigabit;
144
145         sprintf(dev->name, "MOTO ENET%d", index);
146         dev->iobase = 0;
147         dev->priv   = priv;
148         dev->init   = tsec_init;
149         dev->halt   = tsec_halt;
150         dev->send   = tsec_send;
151         dev->recv   = tsec_recv;
152
153         /* Tell u-boot to get the addr from the env */
154         for(i=0;i<6;i++)
155                 dev->enetaddr[i] = 0;
156
157         eth_register(dev);
158
159
160         /* Reset the MAC */
161         priv->regs->maccfg1 |= MACCFG1_SOFT_RESET;
162         priv->regs->maccfg1 &= ~(MACCFG1_SOFT_RESET);
163
164         /* Try to initialize PHY here, and return */
165         return init_phy(dev);
166 }
167
168
169 /* Initializes data structures and registers for the controller,
170  * and brings the interface up.  Returns the link status, meaning
171  * that it returns success if the link is up, failure otherwise.
172  * This allows u-boot to find the first active controller. */
173 int tsec_init(struct eth_device* dev, bd_t * bd)
174 {
175         uint tempval;
176         char tmpbuf[MAC_ADDR_LEN];
177         int i;
178         struct tsec_private *priv = (struct tsec_private *)dev->priv;
179         volatile tsec_t *regs = priv->regs;
180
181         /* Make sure the controller is stopped */
182         tsec_halt(dev);
183
184         /* Init MACCFG2.  Defaults to GMII */
185         regs->maccfg2 = MACCFG2_INIT_SETTINGS;
186
187         /* Init ECNTRL */
188         regs->ecntrl = ECNTRL_INIT_SETTINGS;
189
190         /* Copy the station address into the address registers.
191          * Backwards, because little endian MACS are dumb */
192         for(i=0;i<MAC_ADDR_LEN;i++) {
193                 tmpbuf[MAC_ADDR_LEN - 1 - i] = dev->enetaddr[i];
194         }
195         (uint)(regs->macstnaddr1) = *((uint *)(tmpbuf));
196
197         tempval = *((uint *)(tmpbuf +4));
198
199         (uint)(regs->macstnaddr2) = tempval;
200
201         /* reset the indices to zero */
202         rxIdx = 0;
203         txIdx = 0;
204
205         /* Clear out (for the most part) the other registers */
206         init_registers(regs);
207
208         /* Ready the device for tx/rx */
209         startup_tsec(dev);
210
211         /* If there's no link, fail */
212         return priv->link;
213
214 }
215
216
217 /* Write value to the device's PHY through the registers
218  * specified in priv, modifying the register specified in regnum.
219  * It will wait for the write to be done (or for a timeout to
220  * expire) before exiting
221  */
222 void write_phy_reg(struct tsec_private *priv, uint regnum, uint value)
223 {
224         volatile tsec_t *regbase = priv->phyregs;
225         uint phyid = priv->phyaddr;
226         int timeout=1000000;
227
228         regbase->miimadd = (phyid << 8) | regnum;
229         regbase->miimcon = value;
230         asm("msync");
231
232         timeout=1000000;
233         while((regbase->miimind & MIIMIND_BUSY) && timeout--);
234 }
235
236
237 /* Reads register regnum on the device's PHY through the
238  * registers specified in priv.  It lowers and raises the read
239  * command, and waits for the data to become valid (miimind
240  * notvalid bit cleared), and the bus to cease activity (miimind
241  * busy bit cleared), and then returns the value
242  */
243 uint read_phy_reg(struct tsec_private *priv, uint regnum)
244 {
245         uint value;
246         volatile tsec_t *regbase = priv->phyregs;
247         uint phyid = priv->phyaddr;
248
249         /* Put the address of the phy, and the register
250          * number into MIIMADD */
251         regbase->miimadd = (phyid << 8) | regnum;
252
253         /* Clear the command register, and wait */
254         regbase->miimcom = 0;
255         asm("msync");
256
257         /* Initiate a read command, and wait */
258         regbase->miimcom = MIIM_READ_COMMAND;
259         asm("msync");
260
261         /* Wait for the the indication that the read is done */
262         while((regbase->miimind & (MIIMIND_NOTVALID | MIIMIND_BUSY)));
263
264         /* Grab the value read from the PHY */
265         value = regbase->miimstat;
266
267         return value;
268 }
269
270
271 /* Discover which PHY is attached to the device, and configure it
272  * properly.  If the PHY is not recognized, then return 0
273  * (failure).  Otherwise, return 1
274  */
275 static int init_phy(struct eth_device *dev)
276 {
277         struct tsec_private *priv = (struct tsec_private *)dev->priv;
278         struct phy_info *curphy;
279
280         /* Assign a Physical address to the TBI */
281         priv->regs->tbipa=TBIPA_VALUE;
282
283         if(0 == relocated)
284                 relocate_cmds();
285
286         /* Get the cmd structure corresponding to the attached
287          * PHY */
288         curphy = get_phy_info(dev);
289
290         if(NULL == curphy) {
291                 printf("%s: No PHY found\n", dev->name);
292
293                 return 0;
294         }
295
296         priv->phyinfo = curphy;
297
298         phy_run_commands(priv, priv->phyinfo->config);
299
300         return 1;
301 }
302
303
304 /* Returns which value to write to the control register. */
305 /* For 10/100, the value is slightly different */
306 uint mii_cr_init(uint mii_reg, struct tsec_private *priv)
307 {
308         if(priv->gigabit)
309                 return MIIM_CONTROL_INIT;
310         else
311                 return MIIM_CR_INIT;
312 }
313
314
315 /* Parse the status register for link, and then do
316  * auto-negotiation */
317 uint mii_parse_sr(uint mii_reg, struct tsec_private *priv)
318 {
319         uint timeout = TSEC_TIMEOUT;
320
321         if(mii_reg & MIIM_STATUS_LINK)
322                 priv->link = 1;
323         else
324                 priv->link = 0;
325
326         if(priv->link) {
327                 while((!(mii_reg & MIIM_STATUS_AN_DONE)) && timeout--)
328                         mii_reg = read_phy_reg(priv, MIIM_STATUS);
329         }
330
331         return 0;
332 }
333
334
335 /* Parse the 88E1011's status register for speed and duplex
336  * information */
337 uint mii_parse_88E1011_psr(uint mii_reg, struct tsec_private *priv)
338 {
339         uint speed;
340
341         if(mii_reg & MIIM_88E1011_PHYSTAT_DUPLEX)
342                 priv->duplexity = 1;
343         else
344                 priv->duplexity = 0;
345
346         speed = (mii_reg &MIIM_88E1011_PHYSTAT_SPEED);
347
348         switch(speed) {
349                 case MIIM_88E1011_PHYSTAT_GBIT:
350                         priv->speed = 1000;
351                         break;
352                 case MIIM_88E1011_PHYSTAT_100:
353                         priv->speed = 100;
354                         break;
355                 default:
356                         priv->speed = 10;
357         }
358
359         return 0;
360 }
361
362
363 /* Parse the cis8201's status register for speed and duplex
364  * information */
365 uint mii_parse_cis8201(uint mii_reg, struct tsec_private *priv)
366 {
367         uint speed;
368
369         if(mii_reg & MIIM_CIS8201_AUXCONSTAT_DUPLEX)
370                 priv->duplexity = 1;
371         else
372                 priv->duplexity = 0;
373
374         speed = mii_reg & MIIM_CIS8201_AUXCONSTAT_SPEED;
375         switch(speed) {
376                 case MIIM_CIS8201_AUXCONSTAT_GBIT:
377                         priv->speed = 1000;
378                         break;
379                 case MIIM_CIS8201_AUXCONSTAT_100:
380                         priv->speed = 100;
381                         break;
382                 default:
383                         priv->speed = 10;
384                         break;
385         }
386
387         return 0;
388 }
389
390
391 /* Parse the DM9161's status register for speed and duplex
392  * information */
393 uint mii_parse_dm9161_scsr(uint mii_reg, struct tsec_private *priv)
394 {
395         if(mii_reg & (MIIM_DM9161_SCSR_100F | MIIM_DM9161_SCSR_100H))
396                 priv->speed = 100;
397         else
398                 priv->speed = 10;
399
400         if(mii_reg & (MIIM_DM9161_SCSR_100F | MIIM_DM9161_SCSR_10F))
401                 priv->duplexity = 1;
402         else
403                 priv->duplexity = 0;
404
405         return 0;
406 }
407
408
409 /* Hack to write all 4 PHYs with the LED values */
410 uint mii_cis8204_fixled(uint mii_reg, struct tsec_private *priv)
411 {
412         uint phyid;
413         volatile tsec_t *regbase = priv->phyregs;
414         int timeout=1000000;
415
416         for(phyid=0;phyid<4;phyid++) {
417                 regbase->miimadd = (phyid << 8) | mii_reg;
418                 regbase->miimcon = MIIM_CIS8204_SLEDCON_INIT;
419                 asm("msync");
420
421                 timeout=1000000;
422                 while((regbase->miimind & MIIMIND_BUSY) && timeout--);
423         }
424
425         return MIIM_CIS8204_SLEDCON_INIT;
426 }
427
428
429 /* Initialized required registers to appropriate values, zeroing
430  * those we don't care about (unless zero is bad, in which case,
431  * choose a more appropriate value) */
432 static void init_registers(volatile tsec_t *regs)
433 {
434         /* Clear IEVENT */
435         regs->ievent = IEVENT_INIT_CLEAR;
436
437         regs->imask = IMASK_INIT_CLEAR;
438
439         regs->hash.iaddr0 = 0;
440         regs->hash.iaddr1 = 0;
441         regs->hash.iaddr2 = 0;
442         regs->hash.iaddr3 = 0;
443         regs->hash.iaddr4 = 0;
444         regs->hash.iaddr5 = 0;
445         regs->hash.iaddr6 = 0;
446         regs->hash.iaddr7 = 0;
447
448         regs->hash.gaddr0 = 0;
449         regs->hash.gaddr1 = 0;
450         regs->hash.gaddr2 = 0;
451         regs->hash.gaddr3 = 0;
452         regs->hash.gaddr4 = 0;
453         regs->hash.gaddr5 = 0;
454         regs->hash.gaddr6 = 0;
455         regs->hash.gaddr7 = 0;
456
457         regs->rctrl = 0x00000000;
458
459         /* Init RMON mib registers */
460         memset((void *)&(regs->rmon), 0, sizeof(rmon_mib_t));
461
462         regs->rmon.cam1 = 0xffffffff;
463         regs->rmon.cam2 = 0xffffffff;
464
465         regs->mrblr = MRBLR_INIT_SETTINGS;
466
467         regs->minflr = MINFLR_INIT_SETTINGS;
468
469         regs->attr = ATTR_INIT_SETTINGS;
470         regs->attreli = ATTRELI_INIT_SETTINGS;
471
472 }
473
474
475 /* Configure maccfg2 based on negotiated speed and duplex
476  * reported by PHY handling code */
477 static void adjust_link(struct eth_device *dev)
478 {
479         struct tsec_private *priv = (struct tsec_private *)dev->priv;
480         volatile tsec_t *regs = priv->regs;
481
482         if(priv->link) {
483                 if(priv->duplexity != 0)
484                         regs->maccfg2 |= MACCFG2_FULL_DUPLEX;
485                 else
486                         regs->maccfg2 &= ~(MACCFG2_FULL_DUPLEX);
487
488                 switch(priv->speed) {
489                         case 1000:
490                                 regs->maccfg2 = ((regs->maccfg2&~(MACCFG2_IF))
491                                         | MACCFG2_GMII);
492                                 break;
493                         case 100:
494                         case 10:
495                                 regs->maccfg2 = ((regs->maccfg2&~(MACCFG2_IF))
496                                         | MACCFG2_MII);
497                                 break;
498                         default:
499                                 printf("%s: Speed was bad\n", dev->name);
500                                 break;
501                 }
502
503                 printf("Speed: %d, %s duplex\n", priv->speed,
504                                 (priv->duplexity) ? "full" : "half");
505
506         } else {
507                 printf("%s: No link.\n", dev->name);
508         }
509 }
510
511
512 /* Set up the buffers and their descriptors, and bring up the
513  * interface */
514 static void startup_tsec(struct eth_device *dev)
515 {
516         int i;
517         struct tsec_private *priv = (struct tsec_private *)dev->priv;
518         volatile tsec_t *regs = priv->regs;
519
520         /* Point to the buffer descriptors */
521         regs->tbase = (unsigned int)(&rtx.txbd[txIdx]);
522         regs->rbase = (unsigned int)(&rtx.rxbd[rxIdx]);
523
524         /* Initialize the Rx Buffer descriptors */
525         for (i = 0; i < PKTBUFSRX; i++) {
526                 rtx.rxbd[i].status = RXBD_EMPTY;
527                 rtx.rxbd[i].length = 0;
528                 rtx.rxbd[i].bufPtr = (uint)NetRxPackets[i];
529         }
530         rtx.rxbd[PKTBUFSRX -1].status |= RXBD_WRAP;
531
532         /* Initialize the TX Buffer Descriptors */
533         for(i=0; i<TX_BUF_CNT; i++) {
534                 rtx.txbd[i].status = 0;
535                 rtx.txbd[i].length = 0;
536                 rtx.txbd[i].bufPtr = 0;
537         }
538         rtx.txbd[TX_BUF_CNT -1].status |= TXBD_WRAP;
539
540         /* Start up the PHY */
541         phy_run_commands(priv, priv->phyinfo->startup);
542         adjust_link(dev);
543
544         /* Enable Transmit and Receive */
545         regs->maccfg1 |= (MACCFG1_RX_EN | MACCFG1_TX_EN);
546
547         /* Tell the DMA it is clear to go */
548         regs->dmactrl |= DMACTRL_INIT_SETTINGS;
549         regs->tstat = TSTAT_CLEAR_THALT;
550         regs->dmactrl &= ~(DMACTRL_GRS | DMACTRL_GTS);
551 }
552
553 /* This returns the status bits of the device.  The return value
554  * is never checked, and this is what the 8260 driver did, so we
555  * do the same.  Presumably, this would be zero if there were no
556  * errors */
557 static int tsec_send(struct eth_device* dev, volatile void *packet, int length)
558 {
559         int i;
560         int result = 0;
561         struct tsec_private *priv = (struct tsec_private *)dev->priv;
562         volatile tsec_t *regs = priv->regs;
563
564         /* Find an empty buffer descriptor */
565         for(i=0; rtx.txbd[txIdx].status & TXBD_READY; i++) {
566                 if (i >= TOUT_LOOP) {
567                         DBGPRINT("%s: tsec: tx buffers full\n", dev->name);
568                         return result;
569                 }
570         }
571
572         rtx.txbd[txIdx].bufPtr = (uint)packet;
573         rtx.txbd[txIdx].length = length;
574         rtx.txbd[txIdx].status |= (TXBD_READY | TXBD_LAST | TXBD_CRC | TXBD_INTERRUPT);
575
576         /* Tell the DMA to go */
577         regs->tstat = TSTAT_CLEAR_THALT;
578
579         /* Wait for buffer to be transmitted */
580         for(i=0; rtx.txbd[txIdx].status & TXBD_READY; i++) {
581                 if (i >= TOUT_LOOP) {
582                         DBGPRINT("%s: tsec: tx error\n", dev->name);
583                         return result;
584                 }
585         }
586
587         txIdx = (txIdx + 1) % TX_BUF_CNT;
588         result = rtx.txbd[txIdx].status & TXBD_STATS;
589
590         return result;
591 }
592
593 static int tsec_recv(struct eth_device* dev)
594 {
595         int length;
596         struct tsec_private *priv = (struct tsec_private *)dev->priv;
597         volatile tsec_t *regs = priv->regs;
598
599         while(!(rtx.rxbd[rxIdx].status & RXBD_EMPTY)) {
600
601                 length = rtx.rxbd[rxIdx].length;
602
603                 /* Send the packet up if there were no errors */
604                 if (!(rtx.rxbd[rxIdx].status & RXBD_STATS)) {
605                         NetReceive(NetRxPackets[rxIdx], length - 4);
606                 } else {
607                         printf("Got error %x\n",
608                                         (rtx.rxbd[rxIdx].status & RXBD_STATS));
609                 }
610
611                 rtx.rxbd[rxIdx].length = 0;
612
613                 /* Set the wrap bit if this is the last element in the list */
614                 rtx.rxbd[rxIdx].status = RXBD_EMPTY | (((rxIdx + 1) == PKTBUFSRX) ? RXBD_WRAP : 0);
615
616                 rxIdx = (rxIdx + 1) % PKTBUFSRX;
617         }
618
619         if(regs->ievent&IEVENT_BSY) {
620                 regs->ievent = IEVENT_BSY;
621                 regs->rstat = RSTAT_CLEAR_RHALT;
622         }
623
624         return -1;
625
626 }
627
628
629 /* Stop the interface */
630 static void tsec_halt(struct eth_device* dev)
631 {
632         struct tsec_private *priv = (struct tsec_private *)dev->priv;
633         volatile tsec_t *regs = priv->regs;
634
635         regs->dmactrl &= ~(DMACTRL_GRS | DMACTRL_GTS);
636         regs->dmactrl |= (DMACTRL_GRS | DMACTRL_GTS);
637
638         while(!(regs->ievent & (IEVENT_GRSC | IEVENT_GTSC)));
639
640         regs->maccfg1 &= ~(MACCFG1_TX_EN | MACCFG1_RX_EN);
641
642         /* Shut down the PHY, as needed */
643         phy_run_commands(priv, priv->phyinfo->shutdown);
644 }
645
646
647 struct phy_info phy_info_M88E1011S = {
648         0x01410c6,
649         "Marvell 88E1011S",
650         4,
651         (struct phy_cmd[]) { /* config */
652                 /* Reset and configure the PHY */
653                 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
654                 {0x1d, 0x1f, NULL},
655                 {0x1e, 0x200c, NULL},
656                 {0x1d, 0x5, NULL},
657                 {0x1e, 0x0, NULL},
658                 {0x1e, 0x100, NULL},
659                 {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
660                 {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
661                 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
662                 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
663                 {miim_end,}
664         },
665         (struct phy_cmd[]) { /* startup */
666                 /* Status is read once to clear old link state */
667                 {MIIM_STATUS, miim_read, NULL},
668                 /* Auto-negotiate */
669                 {MIIM_STATUS, miim_read, &mii_parse_sr},
670                 /* Read the status */
671                 {MIIM_88E1011_PHY_STATUS, miim_read, &mii_parse_88E1011_psr},
672                 {miim_end,}
673         },
674         (struct phy_cmd[]) { /* shutdown */
675                 {miim_end,}
676         },
677 };
678
679 struct phy_info phy_info_cis8204 = {
680         0x3f11,
681         "Cicada Cis8204",
682         6,
683         (struct phy_cmd[]) { /* config */
684                 /* Override PHY config settings */
685                 {MIIM_CIS8201_AUX_CONSTAT, MIIM_CIS8201_AUXCONSTAT_INIT, NULL},
686                 /* Configure some basic stuff */
687                 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
688                 {MIIM_CIS8204_SLED_CON, MIIM_CIS8204_SLEDCON_INIT, &mii_cis8204_fixled},
689                 {MIIM_CIS8204_EPHY_CON, MIIM_CIS8204_EPHYCON_INIT, NULL},
690                 {miim_end,}
691         },
692         (struct phy_cmd[]) { /* startup */
693                 /* Read the Status (2x to make sure link is right) */
694                 {MIIM_STATUS, miim_read, NULL},
695                 /* Auto-negotiate */
696                 {MIIM_STATUS, miim_read, &mii_parse_sr},
697                 /* Read the status */
698                 {MIIM_CIS8201_AUX_CONSTAT, miim_read, &mii_parse_cis8201},
699                 {miim_end,}
700         },
701         (struct phy_cmd[]) { /* shutdown */
702                 {miim_end,}
703         },
704 };
705
706 /* Cicada 8201 */
707 struct phy_info phy_info_cis8201 = {
708         0xfc41,
709         "CIS8201",
710         4,
711         (struct phy_cmd[]) { /* config */
712                 /* Override PHY config settings */
713                 {MIIM_CIS8201_AUX_CONSTAT, MIIM_CIS8201_AUXCONSTAT_INIT, NULL},
714                 /* Set up the interface mode */
715                 {MIIM_CIS8201_EXT_CON1, MIIM_CIS8201_EXTCON1_INIT, NULL},
716                 /* Configure some basic stuff */
717                 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
718                 {miim_end,}
719         },
720         (struct phy_cmd[]) { /* startup */
721                 /* Read the Status (2x to make sure link is right) */
722                 {MIIM_STATUS, miim_read, NULL},
723                 /* Auto-negotiate */
724                 {MIIM_STATUS, miim_read, &mii_parse_sr},
725                 /* Read the status */
726                 {MIIM_CIS8201_AUX_CONSTAT, miim_read, &mii_parse_cis8201},
727                 {miim_end,}
728         },
729         (struct phy_cmd[]) { /* shutdown */
730                 {miim_end,}
731         },
732 };
733
734
735 struct phy_info phy_info_dm9161 = {
736         0x0181b88,
737         "Davicom DM9161E",
738         4,
739         (struct phy_cmd[]) { /* config */
740                 {MIIM_CONTROL, MIIM_DM9161_CR_STOP, NULL},
741                 /* Do not bypass the scrambler/descrambler */
742                 {MIIM_DM9161_SCR, MIIM_DM9161_SCR_INIT, NULL},
743                 /* Clear 10BTCSR to default */
744                 {MIIM_DM9161_10BTCSR, MIIM_DM9161_10BTCSR_INIT, NULL},
745                 /* Configure some basic stuff */
746                 {MIIM_CONTROL, MIIM_CR_INIT, NULL},
747                 /* Restart Auto Negotiation */
748                 {MIIM_CONTROL, MIIM_DM9161_CR_RSTAN, NULL},
749                 {miim_end,}
750         },
751         (struct phy_cmd[]) { /* startup */
752                 /* Status is read once to clear old link state */
753                 {MIIM_STATUS, miim_read, NULL},
754                 /* Auto-negotiate */
755                 {MIIM_STATUS, miim_read, &mii_parse_sr},
756                 /* Read the status */
757                 {MIIM_DM9161_SCSR, miim_read, &mii_parse_dm9161_scsr},
758                 {miim_end,}
759         },
760         (struct phy_cmd[]) { /* shutdown */
761                 {miim_end,}
762         },
763 };
764
765 struct phy_info *phy_info[] = {
766 #if 0
767         &phy_info_cis8201,
768 #endif
769         &phy_info_cis8204,
770         &phy_info_M88E1011S,
771         &phy_info_dm9161,
772         NULL
773 };
774
775
776 /* Grab the identifier of the device's PHY, and search through
777  * all of the known PHYs to see if one matches.  If so, return
778  * it, if not, return NULL */
779 struct phy_info * get_phy_info(struct eth_device *dev)
780 {
781         struct tsec_private *priv = (struct tsec_private *)dev->priv;
782         uint phy_reg, phy_ID;
783         int i;
784         struct phy_info *theInfo = NULL;
785
786         /* Grab the bits from PHYIR1, and put them in the upper half */
787         phy_reg = read_phy_reg(priv, MIIM_PHYIR1);
788         phy_ID = (phy_reg & 0xffff) << 16;
789
790         /* Grab the bits from PHYIR2, and put them in the lower half */
791         phy_reg = read_phy_reg(priv, MIIM_PHYIR2);
792         phy_ID |= (phy_reg & 0xffff);
793
794         /* loop through all the known PHY types, and find one that */
795         /* matches the ID we read from the PHY. */
796         for(i=0; phy_info[i]; i++) {
797                 if(phy_info[i]->id == (phy_ID >> phy_info[i]->shift))
798                         theInfo = phy_info[i];
799         }
800
801         if(theInfo == NULL)
802         {
803                 printf("%s: PHY id %x is not supported!\n", dev->name, phy_ID);
804                 return NULL;
805         } else {
806                 printf("%s: PHY is %s (%x)\n", dev->name, theInfo->name,
807                                 phy_ID);
808         }
809
810         return theInfo;
811 }
812
813
814 /* Execute the given series of commands on the given device's
815  * PHY, running functions as necessary*/
816 void phy_run_commands(struct tsec_private *priv, struct phy_cmd *cmd)
817 {
818         int i;
819         uint result;
820         volatile tsec_t *phyregs = priv->phyregs;
821
822         phyregs->miimcfg = MIIMCFG_RESET;
823
824         phyregs->miimcfg = MIIMCFG_INIT_VALUE;
825
826         while(phyregs->miimind & MIIMIND_BUSY);
827
828         for(i=0;cmd->mii_reg != miim_end;i++) {
829                 if(cmd->mii_data == miim_read) {
830                         result = read_phy_reg(priv, cmd->mii_reg);
831
832                         if(cmd->funct != NULL)
833                                 (*(cmd->funct))(result, priv);
834
835                 } else {
836                         if(cmd->funct != NULL)
837                                 result = (*(cmd->funct))(cmd->mii_reg, priv);
838                         else
839                                 result = cmd->mii_data;
840
841                         write_phy_reg(priv, cmd->mii_reg, result);
842
843                 }
844                 cmd++;
845         }
846 }
847
848
849 /* Relocate the function pointers in the phy cmd lists */
850 static void relocate_cmds(void)
851 {
852         struct phy_cmd **cmdlistptr;
853         struct phy_cmd *cmd;
854         int i,j,k;
855         DECLARE_GLOBAL_DATA_PTR;
856
857         for(i=0; phy_info[i]; i++) {
858                 /* First thing's first: relocate the pointers to the
859                  * PHY command structures (the structs were done) */
860                 phy_info[i] = (struct phy_info *) ((uint)phy_info[i]
861                                 + gd->reloc_off);
862                 phy_info[i]->name += gd->reloc_off;
863                 phy_info[i]->config =
864                         (struct phy_cmd *)((uint)phy_info[i]->config
865                                            + gd->reloc_off);
866                 phy_info[i]->startup =
867                         (struct phy_cmd *)((uint)phy_info[i]->startup
868                                            + gd->reloc_off);
869                 phy_info[i]->shutdown =
870                         (struct phy_cmd *)((uint)phy_info[i]->shutdown
871                                            + gd->reloc_off);
872
873                 cmdlistptr = &phy_info[i]->config;
874                 j=0;
875                 for(;cmdlistptr <= &phy_info[i]->shutdown;cmdlistptr++) {
876                         k=0;
877                         for(cmd=*cmdlistptr;cmd->mii_reg != miim_end;cmd++) {
878                                 /* Only relocate non-NULL pointers */
879                                 if(cmd->funct)
880                                         cmd->funct += gd->reloc_off;
881
882                                 k++;
883                         }
884                         j++;
885                 }
886         }
887
888         relocated = 1;
889 }
890
891
892 #ifndef CONFIG_BITBANGMII
893
894 struct tsec_private * get_priv_for_phy(unsigned char phyaddr)
895 {
896         int i;
897
898         for(i=0;i<MAXCONTROLLERS;i++) {
899                 if(privlist[i]->phyaddr == phyaddr)
900                         return privlist[i];
901         }
902
903         return NULL;
904 }
905
906 /*
907  * Read a MII PHY register.
908  *
909  * Returns:
910  *  0 on success
911  */
912 int miiphy_read(unsigned char addr, unsigned char reg, unsigned short *value)
913 {
914         unsigned short ret;
915         struct tsec_private *priv = get_priv_for_phy(addr);
916
917         if(NULL == priv) {
918                 printf("Can't read PHY at address %d\n", addr);
919                 return -1;
920         }
921
922         ret = (unsigned short)read_phy_reg(priv, reg);
923         *value = ret;
924
925         return 0;
926 }
927
928 /*
929  * Write a MII PHY register.
930  *
931  * Returns:
932  *  0 on success
933  */
934 int miiphy_write(unsigned char addr, unsigned char reg, unsigned short value)
935 {
936         struct tsec_private *priv = get_priv_for_phy(addr);
937
938         if(NULL == priv) {
939                 printf("Can't write PHY at address %d\n", addr);
940                 return -1;
941         }
942
943         write_phy_reg(priv, reg, value);
944
945         return 0;
946 }
947
948 #endif /* CONFIG_BITBANGMII */
949
950 #endif /* CONFIG_TSEC_ENET */