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