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