to make u-boot work for fat32 filesystem
[jz_uboot.git] / drivers / tsec.c
blob400e593adfe56ee0a4e85e299b2a382f7f099d00
1 /*
2 * Freescale Three Speed Ethernet Controller driver
4 * This software may be used and distributed according to the
5 * terms of the GNU Public License, Version 2, incorporated
6 * herein by reference.
8 * Copyright 2004 Freescale Semiconductor.
9 * (C) Copyright 2003, Motorola, Inc.
10 * author Andy Fleming
14 #include <config.h>
15 #include <common.h>
16 #include <malloc.h>
17 #include <net.h>
18 #include <command.h>
20 #if defined(CONFIG_TSEC_ENET)
21 #include "tsec.h"
22 #include "miiphy.h"
24 DECLARE_GLOBAL_DATA_PTR;
26 #define TX_BUF_CNT 2
28 static uint rxIdx; /* index of the current RX buffer */
29 static uint txIdx; /* index of the current TX buffer */
31 typedef volatile struct rtxbd {
32 txbd8_t txbd[TX_BUF_CNT];
33 rxbd8_t rxbd[PKTBUFSRX];
34 } RTXBD;
36 struct tsec_info_struct {
37 unsigned int phyaddr;
38 u32 flags;
39 unsigned int phyregidx;
42 /* The tsec_info structure contains 3 values which the
43 * driver uses to determine how to operate a given ethernet
44 * device. The information needed is:
45 * phyaddr - The address of the PHY which is attached to
46 * the given device.
48 * flags - This variable indicates whether the device
49 * supports gigabit speed ethernet, and whether it should be
50 * in reduced mode.
52 * phyregidx - This variable specifies which ethernet device
53 * controls the MII Management registers which are connected
54 * to the PHY. For now, only TSEC1 (index 0) has
55 * access to the PHYs, so all of the entries have "0".
57 * The values specified in the table are taken from the board's
58 * config file in include/configs/. When implementing a new
59 * board with ethernet capability, it is necessary to define:
60 * TSECn_PHY_ADDR
61 * TSECn_PHYIDX
63 * for n = 1,2,3, etc. And for FEC:
64 * FEC_PHY_ADDR
65 * FEC_PHYIDX
67 static struct tsec_info_struct tsec_info[] = {
68 #if defined(CONFIG_MPC85XX_TSEC1) || defined(CONFIG_MPC83XX_TSEC1)
69 {TSEC1_PHY_ADDR, TSEC_GIGABIT, TSEC1_PHYIDX},
70 #elif defined(CONFIG_MPC86XX_TSEC1)
71 {TSEC1_PHY_ADDR, TSEC_GIGABIT | TSEC_REDUCED, TSEC1_PHYIDX},
72 #else
73 {0, 0, 0},
74 #endif
75 #if defined(CONFIG_MPC85XX_TSEC2) || defined(CONFIG_MPC83XX_TSEC2)
76 {TSEC2_PHY_ADDR, TSEC_GIGABIT, TSEC2_PHYIDX},
77 #elif defined(CONFIG_MPC86XX_TSEC2)
78 {TSEC2_PHY_ADDR, TSEC_GIGABIT | TSEC_REDUCED, TSEC2_PHYIDX},
79 #else
80 {0, 0, 0},
81 #endif
82 #ifdef CONFIG_MPC85XX_FEC
83 {FEC_PHY_ADDR, 0, FEC_PHYIDX},
84 #else
85 #if defined(CONFIG_MPC85XX_TSEC3) || defined(CONFIG_MPC83XX_TSEC3) || defined(CONFIG_MPC86XX_TSEC3)
86 {TSEC3_PHY_ADDR, TSEC_GIGABIT | TSEC_REDUCED, TSEC3_PHYIDX},
87 #else
88 {0, 0, 0},
89 #endif
90 #if defined(CONFIG_MPC85XX_TSEC4) || defined(CONFIG_MPC83XX_TSEC4) || defined(CONFIG_MPC86XX_TSEC4)
91 {TSEC4_PHY_ADDR, TSEC_GIGABIT | TSEC_REDUCED, TSEC4_PHYIDX},
92 #else
93 {0, 0, 0},
94 #endif
95 #endif
98 #define MAXCONTROLLERS (4)
100 static int relocated = 0;
102 static struct tsec_private *privlist[MAXCONTROLLERS];
104 #ifdef __GNUC__
105 static RTXBD rtx __attribute__ ((aligned(8)));
106 #else
107 #error "rtx must be 64-bit aligned"
108 #endif
110 static int tsec_send(struct eth_device *dev,
111 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 static int tsec_miiphy_write(char *devname, unsigned char addr,
125 unsigned char reg, unsigned short value);
126 static int tsec_miiphy_read(char *devname, unsigned char addr,
127 unsigned char reg, unsigned short *value);
129 /* Initialize device structure. Returns success if PHY
130 * initialization succeeded (i.e. if it recognizes the PHY)
132 int tsec_initialize(bd_t * bis, int index, char *devname)
134 struct eth_device *dev;
135 int i;
136 struct tsec_private *priv;
138 dev = (struct eth_device *)malloc(sizeof *dev);
140 if (NULL == dev)
141 return 0;
143 memset(dev, 0, sizeof *dev);
145 priv = (struct tsec_private *)malloc(sizeof(*priv));
147 if (NULL == priv)
148 return 0;
150 privlist[index] = priv;
151 priv->regs = (volatile tsec_t *)(TSEC_BASE_ADDR + index * TSEC_SIZE);
152 priv->phyregs = (volatile tsec_t *)(TSEC_BASE_ADDR +
153 tsec_info[index].phyregidx *
154 TSEC_SIZE);
156 priv->phyaddr = tsec_info[index].phyaddr;
157 priv->flags = tsec_info[index].flags;
159 sprintf(dev->name, devname);
160 dev->iobase = 0;
161 dev->priv = priv;
162 dev->init = tsec_init;
163 dev->halt = tsec_halt;
164 dev->send = tsec_send;
165 dev->recv = tsec_recv;
167 /* Tell u-boot to get the addr from the env */
168 for (i = 0; i < 6; i++)
169 dev->enetaddr[i] = 0;
171 eth_register(dev);
173 /* Reset the MAC */
174 priv->regs->maccfg1 |= MACCFG1_SOFT_RESET;
175 priv->regs->maccfg1 &= ~(MACCFG1_SOFT_RESET);
177 #if defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII) \
178 && !defined(BITBANGMII)
179 miiphy_register(dev->name, tsec_miiphy_read, tsec_miiphy_write);
180 #endif
182 /* Try to initialize PHY here, and return */
183 return init_phy(dev);
186 /* Initializes data structures and registers for the controller,
187 * and brings the interface up. Returns the link status, meaning
188 * that it returns success if the link is up, failure otherwise.
189 * This allows u-boot to find the first active controller.
191 int tsec_init(struct eth_device *dev, bd_t * bd)
193 uint tempval;
194 char tmpbuf[MAC_ADDR_LEN];
195 int i;
196 struct tsec_private *priv = (struct tsec_private *)dev->priv;
197 volatile tsec_t *regs = priv->regs;
199 /* Make sure the controller is stopped */
200 tsec_halt(dev);
202 /* Init MACCFG2. Defaults to GMII */
203 regs->maccfg2 = MACCFG2_INIT_SETTINGS;
205 /* Init ECNTRL */
206 regs->ecntrl = ECNTRL_INIT_SETTINGS;
208 /* Copy the station address into the address registers.
209 * Backwards, because little endian MACS are dumb */
210 for (i = 0; i < MAC_ADDR_LEN; i++) {
211 tmpbuf[MAC_ADDR_LEN - 1 - i] = dev->enetaddr[i];
213 regs->macstnaddr1 = *((uint *) (tmpbuf));
215 tempval = *((uint *) (tmpbuf + 4));
217 regs->macstnaddr2 = tempval;
219 /* reset the indices to zero */
220 rxIdx = 0;
221 txIdx = 0;
223 /* Clear out (for the most part) the other registers */
224 init_registers(regs);
226 /* Ready the device for tx/rx */
227 startup_tsec(dev);
229 /* If there's no link, fail */
230 return priv->link;
234 /* Write value to the device's PHY through the registers
235 * specified in priv, modifying the register specified in regnum.
236 * It will wait for the write to be done (or for a timeout to
237 * expire) before exiting
239 void write_phy_reg(struct tsec_private *priv, uint regnum, uint value)
241 volatile tsec_t *regbase = priv->phyregs;
242 uint phyid = priv->phyaddr;
243 int timeout = 1000000;
245 regbase->miimadd = (phyid << 8) | regnum;
246 regbase->miimcon = value;
247 asm("sync");
249 timeout = 1000000;
250 while ((regbase->miimind & MIIMIND_BUSY) && timeout--) ;
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
259 uint read_phy_reg(struct tsec_private *priv, uint regnum)
261 uint value;
262 volatile tsec_t *regbase = priv->phyregs;
263 uint phyid = priv->phyaddr;
265 /* Put the address of the phy, and the register
266 * number into MIIMADD */
267 regbase->miimadd = (phyid << 8) | regnum;
269 /* Clear the command register, and wait */
270 regbase->miimcom = 0;
271 asm("sync");
273 /* Initiate a read command, and wait */
274 regbase->miimcom = MIIM_READ_COMMAND;
275 asm("sync");
277 /* Wait for the the indication that the read is done */
278 while ((regbase->miimind & (MIIMIND_NOTVALID | MIIMIND_BUSY))) ;
280 /* Grab the value read from the PHY */
281 value = regbase->miimstat;
283 return value;
286 /* Discover which PHY is attached to the device, and configure it
287 * properly. If the PHY is not recognized, then return 0
288 * (failure). Otherwise, return 1
290 static int init_phy(struct eth_device *dev)
292 struct tsec_private *priv = (struct tsec_private *)dev->priv;
293 struct phy_info *curphy;
294 volatile tsec_t *regs = (volatile tsec_t *)(TSEC_BASE_ADDR);
296 /* Assign a Physical address to the TBI */
297 regs->tbipa = TBIPA_VALUE;
298 regs = (volatile tsec_t *)(TSEC_BASE_ADDR + TSEC_SIZE);
299 regs->tbipa = TBIPA_VALUE;
300 asm("sync");
302 /* Reset MII (due to new addresses) */
303 priv->phyregs->miimcfg = MIIMCFG_RESET;
304 asm("sync");
305 priv->phyregs->miimcfg = MIIMCFG_INIT_VALUE;
306 asm("sync");
307 while (priv->phyregs->miimind & MIIMIND_BUSY) ;
309 if (0 == relocated)
310 relocate_cmds();
312 /* Get the cmd structure corresponding to the attached
313 * PHY */
314 curphy = get_phy_info(dev);
316 if (curphy == NULL) {
317 priv->phyinfo = NULL;
318 printf("%s: No PHY found\n", dev->name);
320 return 0;
323 priv->phyinfo = curphy;
325 phy_run_commands(priv, priv->phyinfo->config);
327 return 1;
331 * Returns which value to write to the control register.
332 * For 10/100, the value is slightly different
334 uint mii_cr_init(uint mii_reg, struct tsec_private * priv)
336 if (priv->flags & TSEC_GIGABIT)
337 return MIIM_CONTROL_INIT;
338 else
339 return MIIM_CR_INIT;
342 /* Parse the status register for link, and then do
343 * auto-negotiation
345 uint mii_parse_sr(uint mii_reg, struct tsec_private * priv)
348 * Wait if PHY is capable of autonegotiation and autonegotiation
349 * is not complete.
351 mii_reg = read_phy_reg(priv, MIIM_STATUS);
352 if ((mii_reg & PHY_BMSR_AUTN_ABLE)
353 && !(mii_reg & PHY_BMSR_AUTN_COMP)) {
354 int i = 0;
356 puts("Waiting for PHY auto negotiation to complete");
357 while (!((mii_reg & PHY_BMSR_AUTN_COMP)
358 && (mii_reg & MIIM_STATUS_LINK))) {
360 * Timeout reached ?
362 if (i > PHY_AUTONEGOTIATE_TIMEOUT) {
363 puts(" TIMEOUT !\n");
364 priv->link = 0;
365 return 0;
368 if ((i++ % 1000) == 0) {
369 putc('.');
371 udelay(1000); /* 1 ms */
372 mii_reg = read_phy_reg(priv, MIIM_STATUS);
374 puts(" done\n");
375 priv->link = 1;
376 udelay(500000); /* another 500 ms (results in faster booting) */
377 } else {
378 priv->link = 1;
381 return 0;
384 /* Parse the 88E1011's status register for speed and duplex
385 * information
387 uint mii_parse_88E1011_psr(uint mii_reg, struct tsec_private * priv)
389 uint speed;
391 mii_reg = read_phy_reg(priv, MIIM_88E1011_PHY_STATUS);
393 if (!((mii_reg & MIIM_88E1011_PHYSTAT_SPDDONE) &&
394 (mii_reg & MIIM_88E1011_PHYSTAT_LINK))) {
395 int i = 0;
397 puts("Waiting for PHY realtime link");
398 while (!((mii_reg & MIIM_88E1011_PHYSTAT_SPDDONE) &&
399 (mii_reg & MIIM_88E1011_PHYSTAT_LINK))) {
401 * Timeout reached ?
403 if (i > PHY_AUTONEGOTIATE_TIMEOUT) {
404 puts(" TIMEOUT !\n");
405 priv->link = 0;
406 break;
409 if ((i++ % 1000) == 0) {
410 putc('.');
412 udelay(1000); /* 1 ms */
413 mii_reg = read_phy_reg(priv, MIIM_88E1011_PHY_STATUS);
415 puts(" done\n");
416 udelay(500000); /* another 500 ms (results in faster booting) */
419 if (mii_reg & MIIM_88E1011_PHYSTAT_DUPLEX)
420 priv->duplexity = 1;
421 else
422 priv->duplexity = 0;
424 speed = (mii_reg & MIIM_88E1011_PHYSTAT_SPEED);
426 switch (speed) {
427 case MIIM_88E1011_PHYSTAT_GBIT:
428 priv->speed = 1000;
429 break;
430 case MIIM_88E1011_PHYSTAT_100:
431 priv->speed = 100;
432 break;
433 default:
434 priv->speed = 10;
437 return 0;
440 /* Parse the cis8201's status register for speed and duplex
441 * information
443 uint mii_parse_cis8201(uint mii_reg, struct tsec_private * priv)
445 uint speed;
447 if (mii_reg & MIIM_CIS8201_AUXCONSTAT_DUPLEX)
448 priv->duplexity = 1;
449 else
450 priv->duplexity = 0;
452 speed = mii_reg & MIIM_CIS8201_AUXCONSTAT_SPEED;
453 switch (speed) {
454 case MIIM_CIS8201_AUXCONSTAT_GBIT:
455 priv->speed = 1000;
456 break;
457 case MIIM_CIS8201_AUXCONSTAT_100:
458 priv->speed = 100;
459 break;
460 default:
461 priv->speed = 10;
462 break;
465 return 0;
468 /* Parse the vsc8244's status register for speed and duplex
469 * information
471 uint mii_parse_vsc8244(uint mii_reg, struct tsec_private * priv)
473 uint speed;
475 if (mii_reg & MIIM_VSC8244_AUXCONSTAT_DUPLEX)
476 priv->duplexity = 1;
477 else
478 priv->duplexity = 0;
480 speed = mii_reg & MIIM_VSC8244_AUXCONSTAT_SPEED;
481 switch (speed) {
482 case MIIM_VSC8244_AUXCONSTAT_GBIT:
483 priv->speed = 1000;
484 break;
485 case MIIM_VSC8244_AUXCONSTAT_100:
486 priv->speed = 100;
487 break;
488 default:
489 priv->speed = 10;
490 break;
493 return 0;
496 /* Parse the DM9161's status register for speed and duplex
497 * information
499 uint mii_parse_dm9161_scsr(uint mii_reg, struct tsec_private * priv)
501 if (mii_reg & (MIIM_DM9161_SCSR_100F | MIIM_DM9161_SCSR_100H))
502 priv->speed = 100;
503 else
504 priv->speed = 10;
506 if (mii_reg & (MIIM_DM9161_SCSR_100F | MIIM_DM9161_SCSR_10F))
507 priv->duplexity = 1;
508 else
509 priv->duplexity = 0;
511 return 0;
515 * Hack to write all 4 PHYs with the LED values
517 uint mii_cis8204_fixled(uint mii_reg, struct tsec_private * priv)
519 uint phyid;
520 volatile tsec_t *regbase = priv->phyregs;
521 int timeout = 1000000;
523 for (phyid = 0; phyid < 4; phyid++) {
524 regbase->miimadd = (phyid << 8) | mii_reg;
525 regbase->miimcon = MIIM_CIS8204_SLEDCON_INIT;
526 asm("sync");
528 timeout = 1000000;
529 while ((regbase->miimind & MIIMIND_BUSY) && timeout--) ;
532 return MIIM_CIS8204_SLEDCON_INIT;
535 uint mii_cis8204_setmode(uint mii_reg, struct tsec_private * priv)
537 if (priv->flags & TSEC_REDUCED)
538 return MIIM_CIS8204_EPHYCON_INIT | MIIM_CIS8204_EPHYCON_RGMII;
539 else
540 return MIIM_CIS8204_EPHYCON_INIT;
543 /* Initialized required registers to appropriate values, zeroing
544 * those we don't care about (unless zero is bad, in which case,
545 * choose a more appropriate value)
547 static void init_registers(volatile tsec_t * regs)
549 /* Clear IEVENT */
550 regs->ievent = IEVENT_INIT_CLEAR;
552 regs->imask = IMASK_INIT_CLEAR;
554 regs->hash.iaddr0 = 0;
555 regs->hash.iaddr1 = 0;
556 regs->hash.iaddr2 = 0;
557 regs->hash.iaddr3 = 0;
558 regs->hash.iaddr4 = 0;
559 regs->hash.iaddr5 = 0;
560 regs->hash.iaddr6 = 0;
561 regs->hash.iaddr7 = 0;
563 regs->hash.gaddr0 = 0;
564 regs->hash.gaddr1 = 0;
565 regs->hash.gaddr2 = 0;
566 regs->hash.gaddr3 = 0;
567 regs->hash.gaddr4 = 0;
568 regs->hash.gaddr5 = 0;
569 regs->hash.gaddr6 = 0;
570 regs->hash.gaddr7 = 0;
572 regs->rctrl = 0x00000000;
574 /* Init RMON mib registers */
575 memset((void *)&(regs->rmon), 0, sizeof(rmon_mib_t));
577 regs->rmon.cam1 = 0xffffffff;
578 regs->rmon.cam2 = 0xffffffff;
580 regs->mrblr = MRBLR_INIT_SETTINGS;
582 regs->minflr = MINFLR_INIT_SETTINGS;
584 regs->attr = ATTR_INIT_SETTINGS;
585 regs->attreli = ATTRELI_INIT_SETTINGS;
589 /* Configure maccfg2 based on negotiated speed and duplex
590 * reported by PHY handling code
592 static void adjust_link(struct eth_device *dev)
594 struct tsec_private *priv = (struct tsec_private *)dev->priv;
595 volatile tsec_t *regs = priv->regs;
597 if (priv->link) {
598 if (priv->duplexity != 0)
599 regs->maccfg2 |= MACCFG2_FULL_DUPLEX;
600 else
601 regs->maccfg2 &= ~(MACCFG2_FULL_DUPLEX);
603 switch (priv->speed) {
604 case 1000:
605 regs->maccfg2 = ((regs->maccfg2 & ~(MACCFG2_IF))
606 | MACCFG2_GMII);
607 break;
608 case 100:
609 case 10:
610 regs->maccfg2 = ((regs->maccfg2 & ~(MACCFG2_IF))
611 | MACCFG2_MII);
613 /* If We're in reduced mode, we need
614 * to say whether we're 10 or 100 MB.
616 if ((priv->speed == 100)
617 && (priv->flags & TSEC_REDUCED))
618 regs->ecntrl |= ECNTRL_R100;
619 else
620 regs->ecntrl &= ~(ECNTRL_R100);
621 break;
622 default:
623 printf("%s: Speed was bad\n", dev->name);
624 break;
627 printf("Speed: %d, %s duplex\n", priv->speed,
628 (priv->duplexity) ? "full" : "half");
630 } else {
631 printf("%s: No link.\n", dev->name);
635 /* Set up the buffers and their descriptors, and bring up the
636 * interface
638 static void startup_tsec(struct eth_device *dev)
640 int i;
641 struct tsec_private *priv = (struct tsec_private *)dev->priv;
642 volatile tsec_t *regs = priv->regs;
644 /* Point to the buffer descriptors */
645 regs->tbase = (unsigned int)(&rtx.txbd[txIdx]);
646 regs->rbase = (unsigned int)(&rtx.rxbd[rxIdx]);
648 /* Initialize the Rx Buffer descriptors */
649 for (i = 0; i < PKTBUFSRX; i++) {
650 rtx.rxbd[i].status = RXBD_EMPTY;
651 rtx.rxbd[i].length = 0;
652 rtx.rxbd[i].bufPtr = (uint) NetRxPackets[i];
654 rtx.rxbd[PKTBUFSRX - 1].status |= RXBD_WRAP;
656 /* Initialize the TX Buffer Descriptors */
657 for (i = 0; i < TX_BUF_CNT; i++) {
658 rtx.txbd[i].status = 0;
659 rtx.txbd[i].length = 0;
660 rtx.txbd[i].bufPtr = 0;
662 rtx.txbd[TX_BUF_CNT - 1].status |= TXBD_WRAP;
664 /* Start up the PHY */
665 if(priv->phyinfo)
666 phy_run_commands(priv, priv->phyinfo->startup);
667 adjust_link(dev);
669 /* Enable Transmit and Receive */
670 regs->maccfg1 |= (MACCFG1_RX_EN | MACCFG1_TX_EN);
672 /* Tell the DMA it is clear to go */
673 regs->dmactrl |= DMACTRL_INIT_SETTINGS;
674 regs->tstat = TSTAT_CLEAR_THALT;
675 regs->dmactrl &= ~(DMACTRL_GRS | DMACTRL_GTS);
678 /* This returns the status bits of the device. The return value
679 * is never checked, and this is what the 8260 driver did, so we
680 * do the same. Presumably, this would be zero if there were no
681 * errors
683 static int tsec_send(struct eth_device *dev, volatile void *packet, int length)
685 int i;
686 int result = 0;
687 struct tsec_private *priv = (struct tsec_private *)dev->priv;
688 volatile tsec_t *regs = priv->regs;
690 /* Find an empty buffer descriptor */
691 for (i = 0; rtx.txbd[txIdx].status & TXBD_READY; i++) {
692 if (i >= TOUT_LOOP) {
693 debug("%s: tsec: tx buffers full\n", dev->name);
694 return result;
698 rtx.txbd[txIdx].bufPtr = (uint) packet;
699 rtx.txbd[txIdx].length = length;
700 rtx.txbd[txIdx].status |=
701 (TXBD_READY | TXBD_LAST | TXBD_CRC | TXBD_INTERRUPT);
703 /* Tell the DMA to go */
704 regs->tstat = TSTAT_CLEAR_THALT;
706 /* Wait for buffer to be transmitted */
707 for (i = 0; rtx.txbd[txIdx].status & TXBD_READY; i++) {
708 if (i >= TOUT_LOOP) {
709 debug("%s: tsec: tx error\n", dev->name);
710 return result;
714 txIdx = (txIdx + 1) % TX_BUF_CNT;
715 result = rtx.txbd[txIdx].status & TXBD_STATS;
717 return result;
720 static int tsec_recv(struct eth_device *dev)
722 int length;
723 struct tsec_private *priv = (struct tsec_private *)dev->priv;
724 volatile tsec_t *regs = priv->regs;
726 while (!(rtx.rxbd[rxIdx].status & RXBD_EMPTY)) {
728 length = rtx.rxbd[rxIdx].length;
730 /* Send the packet up if there were no errors */
731 if (!(rtx.rxbd[rxIdx].status & RXBD_STATS)) {
732 NetReceive(NetRxPackets[rxIdx], length - 4);
733 } else {
734 printf("Got error %x\n",
735 (rtx.rxbd[rxIdx].status & RXBD_STATS));
738 rtx.rxbd[rxIdx].length = 0;
740 /* Set the wrap bit if this is the last element in the list */
741 rtx.rxbd[rxIdx].status =
742 RXBD_EMPTY | (((rxIdx + 1) == PKTBUFSRX) ? RXBD_WRAP : 0);
744 rxIdx = (rxIdx + 1) % PKTBUFSRX;
747 if (regs->ievent & IEVENT_BSY) {
748 regs->ievent = IEVENT_BSY;
749 regs->rstat = RSTAT_CLEAR_RHALT;
752 return -1;
756 /* Stop the interface */
757 static void tsec_halt(struct eth_device *dev)
759 struct tsec_private *priv = (struct tsec_private *)dev->priv;
760 volatile tsec_t *regs = priv->regs;
762 regs->dmactrl &= ~(DMACTRL_GRS | DMACTRL_GTS);
763 regs->dmactrl |= (DMACTRL_GRS | DMACTRL_GTS);
765 while (!(regs->ievent & (IEVENT_GRSC | IEVENT_GTSC))) ;
767 regs->maccfg1 &= ~(MACCFG1_TX_EN | MACCFG1_RX_EN);
769 /* Shut down the PHY, as needed */
770 if(priv->phyinfo)
771 phy_run_commands(priv, priv->phyinfo->shutdown);
774 struct phy_info phy_info_M88E1011S = {
775 0x01410c6,
776 "Marvell 88E1011S",
778 (struct phy_cmd[]){ /* config */
779 /* Reset and configure the PHY */
780 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
781 {0x1d, 0x1f, NULL},
782 {0x1e, 0x200c, NULL},
783 {0x1d, 0x5, NULL},
784 {0x1e, 0x0, NULL},
785 {0x1e, 0x100, NULL},
786 {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
787 {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
788 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
789 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
790 {miim_end,}
792 (struct phy_cmd[]){ /* startup */
793 /* Status is read once to clear old link state */
794 {MIIM_STATUS, miim_read, NULL},
795 /* Auto-negotiate */
796 {MIIM_STATUS, miim_read, &mii_parse_sr},
797 /* Read the status */
798 {MIIM_88E1011_PHY_STATUS, miim_read,
799 &mii_parse_88E1011_psr},
800 {miim_end,}
802 (struct phy_cmd[]){ /* shutdown */
803 {miim_end,}
807 struct phy_info phy_info_M88E1111S = {
808 0x01410cc,
809 "Marvell 88E1111S",
811 (struct phy_cmd[]){ /* config */
812 /* Reset and configure the PHY */
813 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
814 {0x1d, 0x1f, NULL},
815 {0x1e, 0x200c, NULL},
816 {0x1d, 0x5, NULL},
817 {0x1e, 0x0, NULL},
818 {0x1e, 0x100, NULL},
819 {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
820 {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
821 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
822 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
823 {miim_end,}
825 (struct phy_cmd[]){ /* startup */
826 /* Status is read once to clear old link state */
827 {MIIM_STATUS, miim_read, NULL},
828 /* Auto-negotiate */
829 {MIIM_STATUS, miim_read, &mii_parse_sr},
830 /* Read the status */
831 {MIIM_88E1011_PHY_STATUS, miim_read,
832 &mii_parse_88E1011_psr},
833 {miim_end,}
835 (struct phy_cmd[]){ /* shutdown */
836 {miim_end,}
840 static unsigned int m88e1145_setmode(uint mii_reg, struct tsec_private *priv)
842 uint mii_data = read_phy_reg(priv, mii_reg);
844 /* Setting MIIM_88E1145_PHY_EXT_CR */
845 if (priv->flags & TSEC_REDUCED)
846 return mii_data |
847 MIIM_M88E1145_RGMII_RX_DELAY | MIIM_M88E1145_RGMII_TX_DELAY;
848 else
849 return mii_data;
852 static struct phy_info phy_info_M88E1145 = {
853 0x01410cd,
854 "Marvell 88E1145",
856 (struct phy_cmd[]){ /* config */
857 /* Errata E0, E1 */
858 {29, 0x001b, NULL},
859 {30, 0x418f, NULL},
860 {29, 0x0016, NULL},
861 {30, 0xa2da, NULL},
863 /* Reset and configure the PHY */
864 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
865 {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
866 {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
867 {MIIM_88E1011_PHY_SCR, MIIM_88E1011_PHY_MDI_X_AUTO,
868 NULL},
869 {MIIM_88E1145_PHY_EXT_CR, 0, &m88e1145_setmode},
870 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
871 {MIIM_CONTROL, MIIM_CONTROL_INIT, NULL},
872 {miim_end,}
874 (struct phy_cmd[]){ /* startup */
875 /* Status is read once to clear old link state */
876 {MIIM_STATUS, miim_read, NULL},
877 /* Auto-negotiate */
878 {MIIM_STATUS, miim_read, &mii_parse_sr},
879 {MIIM_88E1111_PHY_LED_CONTROL,
880 MIIM_88E1111_PHY_LED_DIRECT, NULL},
881 /* Read the Status */
882 {MIIM_88E1011_PHY_STATUS, miim_read,
883 &mii_parse_88E1011_psr},
884 {miim_end,}
886 (struct phy_cmd[]){ /* shutdown */
887 {miim_end,}
891 struct phy_info phy_info_cis8204 = {
892 0x3f11,
893 "Cicada Cis8204",
895 (struct phy_cmd[]){ /* config */
896 /* Override PHY config settings */
897 {MIIM_CIS8201_AUX_CONSTAT,
898 MIIM_CIS8201_AUXCONSTAT_INIT, NULL},
899 /* Configure some basic stuff */
900 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
901 {MIIM_CIS8204_SLED_CON, MIIM_CIS8204_SLEDCON_INIT,
902 &mii_cis8204_fixled},
903 {MIIM_CIS8204_EPHY_CON, MIIM_CIS8204_EPHYCON_INIT,
904 &mii_cis8204_setmode},
905 {miim_end,}
907 (struct phy_cmd[]){ /* startup */
908 /* Read the Status (2x to make sure link is right) */
909 {MIIM_STATUS, miim_read, NULL},
910 /* Auto-negotiate */
911 {MIIM_STATUS, miim_read, &mii_parse_sr},
912 /* Read the status */
913 {MIIM_CIS8201_AUX_CONSTAT, miim_read,
914 &mii_parse_cis8201},
915 {miim_end,}
917 (struct phy_cmd[]){ /* shutdown */
918 {miim_end,}
922 /* Cicada 8201 */
923 struct phy_info phy_info_cis8201 = {
924 0xfc41,
925 "CIS8201",
927 (struct phy_cmd[]){ /* config */
928 /* Override PHY config settings */
929 {MIIM_CIS8201_AUX_CONSTAT,
930 MIIM_CIS8201_AUXCONSTAT_INIT, NULL},
931 /* Set up the interface mode */
932 {MIIM_CIS8201_EXT_CON1, MIIM_CIS8201_EXTCON1_INIT,
933 NULL},
934 /* Configure some basic stuff */
935 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
936 {miim_end,}
938 (struct phy_cmd[]){ /* startup */
939 /* Read the Status (2x to make sure link is right) */
940 {MIIM_STATUS, miim_read, NULL},
941 /* Auto-negotiate */
942 {MIIM_STATUS, miim_read, &mii_parse_sr},
943 /* Read the status */
944 {MIIM_CIS8201_AUX_CONSTAT, miim_read,
945 &mii_parse_cis8201},
946 {miim_end,}
948 (struct phy_cmd[]){ /* shutdown */
949 {miim_end,}
952 struct phy_info phy_info_VSC8244 = {
953 0x3f1b,
954 "Vitesse VSC8244",
956 (struct phy_cmd[]){ /* config */
957 /* Override PHY config settings */
958 /* Configure some basic stuff */
959 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
960 {miim_end,}
962 (struct phy_cmd[]){ /* startup */
963 /* Read the Status (2x to make sure link is right) */
964 {MIIM_STATUS, miim_read, NULL},
965 /* Auto-negotiate */
966 {MIIM_STATUS, miim_read, &mii_parse_sr},
967 /* Read the status */
968 {MIIM_VSC8244_AUX_CONSTAT, miim_read,
969 &mii_parse_vsc8244},
970 {miim_end,}
972 (struct phy_cmd[]){ /* shutdown */
973 {miim_end,}
977 struct phy_info phy_info_dm9161 = {
978 0x0181b88,
979 "Davicom DM9161E",
981 (struct phy_cmd[]){ /* config */
982 {MIIM_CONTROL, MIIM_DM9161_CR_STOP, NULL},
983 /* Do not bypass the scrambler/descrambler */
984 {MIIM_DM9161_SCR, MIIM_DM9161_SCR_INIT, NULL},
985 /* Clear 10BTCSR to default */
986 {MIIM_DM9161_10BTCSR, MIIM_DM9161_10BTCSR_INIT,
987 NULL},
988 /* Configure some basic stuff */
989 {MIIM_CONTROL, MIIM_CR_INIT, NULL},
990 /* Restart Auto Negotiation */
991 {MIIM_CONTROL, MIIM_DM9161_CR_RSTAN, NULL},
992 {miim_end,}
994 (struct phy_cmd[]){ /* startup */
995 /* Status is read once to clear old link state */
996 {MIIM_STATUS, miim_read, NULL},
997 /* Auto-negotiate */
998 {MIIM_STATUS, miim_read, &mii_parse_sr},
999 /* Read the status */
1000 {MIIM_DM9161_SCSR, miim_read,
1001 &mii_parse_dm9161_scsr},
1002 {miim_end,}
1004 (struct phy_cmd[]){ /* shutdown */
1005 {miim_end,}
1009 uint mii_parse_lxt971_sr2(uint mii_reg, struct tsec_private *priv)
1011 unsigned int speed;
1012 if (priv->link) {
1013 speed = mii_reg & MIIM_LXT971_SR2_SPEED_MASK;
1015 switch (speed) {
1016 case MIIM_LXT971_SR2_10HDX:
1017 priv->speed = 10;
1018 priv->duplexity = 0;
1019 break;
1020 case MIIM_LXT971_SR2_10FDX:
1021 priv->speed = 10;
1022 priv->duplexity = 1;
1023 break;
1024 case MIIM_LXT971_SR2_100HDX:
1025 priv->speed = 100;
1026 priv->duplexity = 0;
1027 default:
1028 priv->speed = 100;
1029 priv->duplexity = 1;
1030 break;
1032 } else {
1033 priv->speed = 0;
1034 priv->duplexity = 0;
1037 return 0;
1040 static struct phy_info phy_info_lxt971 = {
1041 0x0001378e,
1042 "LXT971",
1044 (struct phy_cmd[]){ /* config */
1045 {MIIM_CR, MIIM_CR_INIT, mii_cr_init}, /* autonegotiate */
1046 {miim_end,}
1048 (struct phy_cmd[]){ /* startup - enable interrupts */
1049 /* { 0x12, 0x00f2, NULL }, */
1050 {MIIM_STATUS, miim_read, NULL},
1051 {MIIM_STATUS, miim_read, &mii_parse_sr},
1052 {MIIM_LXT971_SR2, miim_read, &mii_parse_lxt971_sr2},
1053 {miim_end,}
1055 (struct phy_cmd[]){ /* shutdown - disable interrupts */
1056 {miim_end,}
1060 /* Parse the DP83865's link and auto-neg status register for speed and duplex
1061 * information
1063 uint mii_parse_dp83865_lanr(uint mii_reg, struct tsec_private *priv)
1065 switch (mii_reg & MIIM_DP83865_SPD_MASK) {
1067 case MIIM_DP83865_SPD_1000:
1068 priv->speed = 1000;
1069 break;
1071 case MIIM_DP83865_SPD_100:
1072 priv->speed = 100;
1073 break;
1075 default:
1076 priv->speed = 10;
1077 break;
1081 if (mii_reg & MIIM_DP83865_DPX_FULL)
1082 priv->duplexity = 1;
1083 else
1084 priv->duplexity = 0;
1086 return 0;
1089 struct phy_info phy_info_dp83865 = {
1090 0x20005c7,
1091 "NatSemi DP83865",
1093 (struct phy_cmd[]){ /* config */
1094 {MIIM_CONTROL, MIIM_DP83865_CR_INIT, NULL},
1095 {miim_end,}
1097 (struct phy_cmd[]){ /* startup */
1098 /* Status is read once to clear old link state */
1099 {MIIM_STATUS, miim_read, NULL},
1100 /* Auto-negotiate */
1101 {MIIM_STATUS, miim_read, &mii_parse_sr},
1102 /* Read the link and auto-neg status */
1103 {MIIM_DP83865_LANR, miim_read,
1104 &mii_parse_dp83865_lanr},
1105 {miim_end,}
1107 (struct phy_cmd[]){ /* shutdown */
1108 {miim_end,}
1112 struct phy_info *phy_info[] = {
1113 #if 0
1114 &phy_info_cis8201,
1115 #endif
1116 &phy_info_cis8204,
1117 &phy_info_M88E1011S,
1118 &phy_info_M88E1111S,
1119 &phy_info_M88E1145,
1120 &phy_info_dm9161,
1121 &phy_info_lxt971,
1122 &phy_info_VSC8244,
1123 &phy_info_dp83865,
1124 NULL
1127 /* Grab the identifier of the device's PHY, and search through
1128 * all of the known PHYs to see if one matches. If so, return
1129 * it, if not, return NULL
1131 struct phy_info *get_phy_info(struct eth_device *dev)
1133 struct tsec_private *priv = (struct tsec_private *)dev->priv;
1134 uint phy_reg, phy_ID;
1135 int i;
1136 struct phy_info *theInfo = NULL;
1138 /* Grab the bits from PHYIR1, and put them in the upper half */
1139 phy_reg = read_phy_reg(priv, MIIM_PHYIR1);
1140 phy_ID = (phy_reg & 0xffff) << 16;
1142 /* Grab the bits from PHYIR2, and put them in the lower half */
1143 phy_reg = read_phy_reg(priv, MIIM_PHYIR2);
1144 phy_ID |= (phy_reg & 0xffff);
1146 /* loop through all the known PHY types, and find one that */
1147 /* matches the ID we read from the PHY. */
1148 for (i = 0; phy_info[i]; i++) {
1149 if (phy_info[i]->id == (phy_ID >> phy_info[i]->shift))
1150 theInfo = phy_info[i];
1153 if (theInfo == NULL) {
1154 printf("%s: PHY id %x is not supported!\n", dev->name, phy_ID);
1155 return NULL;
1156 } else {
1157 debug("%s: PHY is %s (%x)\n", dev->name, theInfo->name, phy_ID);
1160 return theInfo;
1163 /* Execute the given series of commands on the given device's
1164 * PHY, running functions as necessary
1166 void phy_run_commands(struct tsec_private *priv, struct phy_cmd *cmd)
1168 int i;
1169 uint result;
1170 volatile tsec_t *phyregs = priv->phyregs;
1172 phyregs->miimcfg = MIIMCFG_RESET;
1174 phyregs->miimcfg = MIIMCFG_INIT_VALUE;
1176 while (phyregs->miimind & MIIMIND_BUSY) ;
1178 for (i = 0; cmd->mii_reg != miim_end; i++) {
1179 if (cmd->mii_data == miim_read) {
1180 result = read_phy_reg(priv, cmd->mii_reg);
1182 if (cmd->funct != NULL)
1183 (*(cmd->funct)) (result, priv);
1185 } else {
1186 if (cmd->funct != NULL)
1187 result = (*(cmd->funct)) (cmd->mii_reg, priv);
1188 else
1189 result = cmd->mii_data;
1191 write_phy_reg(priv, cmd->mii_reg, result);
1194 cmd++;
1198 /* Relocate the function pointers in the phy cmd lists */
1199 static void relocate_cmds(void)
1201 struct phy_cmd **cmdlistptr;
1202 struct phy_cmd *cmd;
1203 int i, j, k;
1205 for (i = 0; phy_info[i]; i++) {
1206 /* First thing's first: relocate the pointers to the
1207 * PHY command structures (the structs were done) */
1208 phy_info[i] = (struct phy_info *)((uint) phy_info[i]
1209 + gd->reloc_off);
1210 phy_info[i]->name += gd->reloc_off;
1211 phy_info[i]->config =
1212 (struct phy_cmd *)((uint) phy_info[i]->config
1213 + gd->reloc_off);
1214 phy_info[i]->startup =
1215 (struct phy_cmd *)((uint) phy_info[i]->startup
1216 + gd->reloc_off);
1217 phy_info[i]->shutdown =
1218 (struct phy_cmd *)((uint) phy_info[i]->shutdown
1219 + gd->reloc_off);
1221 cmdlistptr = &phy_info[i]->config;
1222 j = 0;
1223 for (; cmdlistptr <= &phy_info[i]->shutdown; cmdlistptr++) {
1224 k = 0;
1225 for (cmd = *cmdlistptr;
1226 cmd->mii_reg != miim_end;
1227 cmd++) {
1228 /* Only relocate non-NULL pointers */
1229 if (cmd->funct)
1230 cmd->funct += gd->reloc_off;
1232 k++;
1234 j++;
1238 relocated = 1;
1241 #if defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII) \
1242 && !defined(BITBANGMII)
1244 struct tsec_private *get_priv_for_phy(unsigned char phyaddr)
1246 int i;
1248 for (i = 0; i < MAXCONTROLLERS; i++) {
1249 if (privlist[i]->phyaddr == phyaddr)
1250 return privlist[i];
1253 return NULL;
1257 * Read a MII PHY register.
1259 * Returns:
1260 * 0 on success
1262 static int tsec_miiphy_read(char *devname, unsigned char addr,
1263 unsigned char reg, unsigned short *value)
1265 unsigned short ret;
1266 struct tsec_private *priv = get_priv_for_phy(addr);
1268 if (NULL == priv) {
1269 printf("Can't read PHY at address %d\n", addr);
1270 return -1;
1273 ret = (unsigned short)read_phy_reg(priv, reg);
1274 *value = ret;
1276 return 0;
1280 * Write a MII PHY register.
1282 * Returns:
1283 * 0 on success
1285 static int tsec_miiphy_write(char *devname, unsigned char addr,
1286 unsigned char reg, unsigned short value)
1288 struct tsec_private *priv = get_priv_for_phy(addr);
1290 if (NULL == priv) {
1291 printf("Can't write PHY at address %d\n", addr);
1292 return -1;
1295 write_phy_reg(priv, reg, value);
1297 return 0;
1300 #endif /* defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII)
1301 && !defined(BITBANGMII) */
1303 #endif /* CONFIG_TSEC_ENET */