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