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
8 * Copyright 2004, 2007 Freescale Semiconductor, Inc.
9 * (C) Copyright 2003, Motorola, Inc.
20 #if defined(CONFIG_TSEC_ENET)
24 DECLARE_GLOBAL_DATA_PTR;
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];
36 struct tsec_info_struct {
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
48 * flags - This variable indicates whether the device
49 * supports gigabit speed ethernet, and whether it should be
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:
63 * for n = 1,2,3, etc. And for FEC:
67 static struct tsec_info_struct tsec_info[] = {
69 {TSEC1_PHY_ADDR, TSEC1_FLAGS, TSEC1_PHYIDX},
74 {TSEC2_PHY_ADDR, TSEC2_FLAGS, TSEC2_PHYIDX},
78 #ifdef CONFIG_MPC85XX_FEC
79 {FEC_PHY_ADDR, FEC_FLAGS, FEC_PHYIDX},
82 {TSEC3_PHY_ADDR, TSEC3_FLAGS, TSEC3_PHYIDX},
87 {TSEC4_PHY_ADDR, TSEC4_FLAGS, TSEC4_PHYIDX},
90 #endif /* CONFIG_TSEC4 */
91 #endif /* CONFIG_MPC85XX_FEC */
94 #define MAXCONTROLLERS (4)
96 static int relocated = 0;
98 static struct tsec_private *privlist[MAXCONTROLLERS];
101 static RTXBD rtx __attribute__ ((aligned(8)));
103 #error "rtx must be 64-bit aligned"
106 static int tsec_send(struct eth_device *dev,
107 volatile void *packet, int length);
108 static int tsec_recv(struct eth_device *dev);
109 static int tsec_init(struct eth_device *dev, bd_t * bd);
110 static void tsec_halt(struct eth_device *dev);
111 static void init_registers(volatile tsec_t * regs);
112 static void startup_tsec(struct eth_device *dev);
113 static int init_phy(struct eth_device *dev);
114 void write_phy_reg(struct tsec_private *priv, uint regnum, uint value);
115 uint read_phy_reg(struct tsec_private *priv, uint regnum);
116 struct phy_info *get_phy_info(struct eth_device *dev);
117 void phy_run_commands(struct tsec_private *priv, struct phy_cmd *cmd);
118 static void adjust_link(struct eth_device *dev);
119 static void relocate_cmds(void);
120 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) \
121 && !defined(BITBANGMII)
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);
127 #ifdef CONFIG_MCAST_TFTP
128 static int tsec_mcast_addr (struct eth_device *dev, u8 mcast_mac, u8 set);
131 /* Initialize device structure. Returns success if PHY
132 * initialization succeeded (i.e. if it recognizes the PHY)
134 int tsec_initialize(bd_t * bis, int index, char *devname)
136 struct eth_device *dev;
138 struct tsec_private *priv;
140 dev = (struct eth_device *)malloc(sizeof *dev);
145 memset(dev, 0, sizeof *dev);
147 priv = (struct tsec_private *)malloc(sizeof(*priv));
152 privlist[index] = priv;
153 priv->regs = (volatile tsec_t *)(TSEC_BASE_ADDR + index * TSEC_SIZE);
154 priv->phyregs = (volatile tsec_t *)(TSEC_BASE_ADDR +
155 tsec_info[index].phyregidx *
158 priv->phyaddr = tsec_info[index].phyaddr;
159 priv->flags = tsec_info[index].flags;
161 sprintf(dev->name, devname);
164 dev->init = tsec_init;
165 dev->halt = tsec_halt;
166 dev->send = tsec_send;
167 dev->recv = tsec_recv;
168 #ifdef CONFIG_MCAST_TFTP
169 dev->mcast = tsec_mcast_addr;
172 /* Tell u-boot to get the addr from the env */
173 for (i = 0; i < 6; i++)
174 dev->enetaddr[i] = 0;
179 priv->regs->maccfg1 |= MACCFG1_SOFT_RESET;
180 priv->regs->maccfg1 &= ~(MACCFG1_SOFT_RESET);
182 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) \
183 && !defined(BITBANGMII)
184 miiphy_register(dev->name, tsec_miiphy_read, tsec_miiphy_write);
187 /* Try to initialize PHY here, and return */
188 return init_phy(dev);
191 /* Initializes data structures and registers for the controller,
192 * and brings the interface up. Returns the link status, meaning
193 * that it returns success if the link is up, failure otherwise.
194 * This allows u-boot to find the first active controller.
196 int tsec_init(struct eth_device *dev, bd_t * bd)
199 char tmpbuf[MAC_ADDR_LEN];
201 struct tsec_private *priv = (struct tsec_private *)dev->priv;
202 volatile tsec_t *regs = priv->regs;
204 /* Make sure the controller is stopped */
207 /* Init MACCFG2. Defaults to GMII */
208 regs->maccfg2 = MACCFG2_INIT_SETTINGS;
211 regs->ecntrl = ECNTRL_INIT_SETTINGS;
213 /* Copy the station address into the address registers.
214 * Backwards, because little endian MACS are dumb */
215 for (i = 0; i < MAC_ADDR_LEN; i++) {
216 tmpbuf[MAC_ADDR_LEN - 1 - i] = dev->enetaddr[i];
218 regs->macstnaddr1 = *((uint *) (tmpbuf));
220 tempval = *((uint *) (tmpbuf + 4));
222 regs->macstnaddr2 = tempval;
224 /* reset the indices to zero */
228 /* Clear out (for the most part) the other registers */
229 init_registers(regs);
231 /* Ready the device for tx/rx */
234 /* If there's no link, fail */
235 return (priv->link ? 0 : -1);
239 /* Write value to the device's PHY through the registers
240 * specified in priv, modifying the register specified in regnum.
241 * It will wait for the write to be done (or for a timeout to
242 * expire) before exiting
244 void write_any_phy_reg(struct tsec_private *priv, uint phyid, uint regnum, uint value)
246 volatile tsec_t *regbase = priv->phyregs;
247 int timeout = 1000000;
249 regbase->miimadd = (phyid << 8) | regnum;
250 regbase->miimcon = value;
254 while ((regbase->miimind & MIIMIND_BUSY) && timeout--) ;
257 /* #define to provide old write_phy_reg functionality without duplicating code */
258 #define write_phy_reg(priv, regnum, value) write_any_phy_reg(priv,priv->phyaddr,regnum,value)
260 /* Reads register regnum on the device's PHY through the
261 * registers specified in priv. It lowers and raises the read
262 * command, and waits for the data to become valid (miimind
263 * notvalid bit cleared), and the bus to cease activity (miimind
264 * busy bit cleared), and then returns the value
266 uint read_any_phy_reg(struct tsec_private *priv, uint phyid, uint regnum)
269 volatile tsec_t *regbase = priv->phyregs;
271 /* Put the address of the phy, and the register
272 * number into MIIMADD */
273 regbase->miimadd = (phyid << 8) | regnum;
275 /* Clear the command register, and wait */
276 regbase->miimcom = 0;
279 /* Initiate a read command, and wait */
280 regbase->miimcom = MIIM_READ_COMMAND;
283 /* Wait for the the indication that the read is done */
284 while ((regbase->miimind & (MIIMIND_NOTVALID | MIIMIND_BUSY))) ;
286 /* Grab the value read from the PHY */
287 value = regbase->miimstat;
292 /* #define to provide old read_phy_reg functionality without duplicating code */
293 #define read_phy_reg(priv,regnum) read_any_phy_reg(priv,priv->phyaddr,regnum)
295 /* Discover which PHY is attached to the device, and configure it
296 * properly. If the PHY is not recognized, then return 0
297 * (failure). Otherwise, return 1
299 static int init_phy(struct eth_device *dev)
301 struct tsec_private *priv = (struct tsec_private *)dev->priv;
302 struct phy_info *curphy;
303 volatile tsec_t *regs = (volatile tsec_t *)(TSEC_BASE_ADDR);
305 /* Assign a Physical address to the TBI */
306 regs->tbipa = CFG_TBIPA_VALUE;
307 regs = (volatile tsec_t *)(TSEC_BASE_ADDR + TSEC_SIZE);
308 regs->tbipa = CFG_TBIPA_VALUE;
311 /* Reset MII (due to new addresses) */
312 priv->phyregs->miimcfg = MIIMCFG_RESET;
314 priv->phyregs->miimcfg = MIIMCFG_INIT_VALUE;
316 while (priv->phyregs->miimind & MIIMIND_BUSY) ;
321 /* Get the cmd structure corresponding to the attached
323 curphy = get_phy_info(dev);
325 if (curphy == NULL) {
326 priv->phyinfo = NULL;
327 printf("%s: No PHY found\n", dev->name);
332 priv->phyinfo = curphy;
334 phy_run_commands(priv, priv->phyinfo->config);
340 * Returns which value to write to the control register.
341 * For 10/100, the value is slightly different
343 uint mii_cr_init(uint mii_reg, struct tsec_private * priv)
345 if (priv->flags & TSEC_GIGABIT)
346 return MIIM_CONTROL_INIT;
351 /* Parse the status register for link, and then do
354 uint mii_parse_sr(uint mii_reg, struct tsec_private * priv)
357 * Wait if the link is up, and autonegotiation is in progress
358 * (ie - we're capable and it's not done)
360 mii_reg = read_phy_reg(priv, MIIM_STATUS);
361 if ((mii_reg & MIIM_STATUS_LINK) && (mii_reg & PHY_BMSR_AUTN_ABLE)
362 && !(mii_reg & PHY_BMSR_AUTN_COMP)) {
365 puts("Waiting for PHY auto negotiation to complete");
366 while (!(mii_reg & PHY_BMSR_AUTN_COMP)) {
370 if (i > PHY_AUTONEGOTIATE_TIMEOUT) {
371 puts(" TIMEOUT !\n");
376 if ((i++ % 1000) == 0) {
379 udelay(1000); /* 1 ms */
380 mii_reg = read_phy_reg(priv, MIIM_STATUS);
384 udelay(500000); /* another 500 ms (results in faster booting) */
386 if (mii_reg & MIIM_STATUS_LINK)
395 /* Generic function which updates the speed and duplex. If
396 * autonegotiation is enabled, it uses the AND of the link
397 * partner's advertised capabilities and our advertised
398 * capabilities. If autonegotiation is disabled, we use the
399 * appropriate bits in the control register.
401 * Stolen from Linux's mii.c and phy_device.c
403 uint mii_parse_link(uint mii_reg, struct tsec_private *priv)
405 /* We're using autonegotiation */
406 if (mii_reg & PHY_BMSR_AUTN_ABLE) {
410 /* Check for gigabit capability */
411 if (mii_reg & PHY_BMSR_EXT) {
412 /* We want a list of states supported by
413 * both PHYs in the link
415 gblpa = read_phy_reg(priv, PHY_1000BTSR);
416 gblpa &= read_phy_reg(priv, PHY_1000BTCR) << 2;
419 /* Set the baseline so we only have to set them
420 * if they're different
425 /* Check the gigabit fields */
426 if (gblpa & (PHY_1000BTSR_1000FD | PHY_1000BTSR_1000HD)) {
429 if (gblpa & PHY_1000BTSR_1000FD)
436 lpa = read_phy_reg(priv, PHY_ANAR);
437 lpa &= read_phy_reg(priv, PHY_ANLPAR);
439 if (lpa & (PHY_ANLPAR_TXFD | PHY_ANLPAR_TX)) {
442 if (lpa & PHY_ANLPAR_TXFD)
445 } else if (lpa & PHY_ANLPAR_10FD)
448 uint bmcr = read_phy_reg(priv, PHY_BMCR);
453 if (bmcr & PHY_BMCR_DPLX)
456 if (bmcr & PHY_BMCR_1000_MBPS)
458 else if (bmcr & PHY_BMCR_100_MBPS)
466 * Parse the BCM54xx status register for speed and duplex information.
467 * The linux sungem_phy has this information, but in a table format.
469 uint mii_parse_BCM54xx_sr(uint mii_reg, struct tsec_private *priv)
472 switch((mii_reg & MIIM_BCM54xx_AUXSTATUS_LINKMODE_MASK) >> MIIM_BCM54xx_AUXSTATUS_LINKMODE_SHIFT){
475 printf("Enet starting in 10BT/HD\n");
481 printf("Enet starting in 10BT/FD\n");
487 printf("Enet starting in 100BT/HD\n");
493 printf("Enet starting in 100BT/FD\n");
499 printf("Enet starting in 1000BT/HD\n");
505 printf("Enet starting in 1000BT/FD\n");
511 printf("Auto-neg error, defaulting to 10BT/HD\n");
520 /* Parse the 88E1011's status register for speed and duplex
523 uint mii_parse_88E1011_psr(uint mii_reg, struct tsec_private * priv)
527 mii_reg = read_phy_reg(priv, MIIM_88E1011_PHY_STATUS);
529 if ((mii_reg & MIIM_88E1011_PHYSTAT_LINK) &&
530 !(mii_reg & MIIM_88E1011_PHYSTAT_SPDDONE)) {
533 puts("Waiting for PHY realtime link");
534 while (!(mii_reg & MIIM_88E1011_PHYSTAT_SPDDONE)) {
535 /* Timeout reached ? */
536 if (i > PHY_AUTONEGOTIATE_TIMEOUT) {
537 puts(" TIMEOUT !\n");
542 if ((i++ % 1000) == 0) {
545 udelay(1000); /* 1 ms */
546 mii_reg = read_phy_reg(priv, MIIM_88E1011_PHY_STATUS);
549 udelay(500000); /* another 500 ms (results in faster booting) */
551 if (mii_reg & MIIM_88E1011_PHYSTAT_LINK)
557 if (mii_reg & MIIM_88E1011_PHYSTAT_DUPLEX)
562 speed = (mii_reg & MIIM_88E1011_PHYSTAT_SPEED);
565 case MIIM_88E1011_PHYSTAT_GBIT:
568 case MIIM_88E1011_PHYSTAT_100:
578 /* Parse the RTL8211B's status register for speed and duplex
581 uint mii_parse_RTL8211B_sr(uint mii_reg, struct tsec_private * priv)
585 mii_reg = read_phy_reg(priv, MIIM_RTL8211B_PHY_STATUS);
586 if (!(mii_reg & MIIM_RTL8211B_PHYSTAT_SPDDONE)) {
589 /* in case of timeout ->link is cleared */
591 puts("Waiting for PHY realtime link");
592 while (!(mii_reg & MIIM_RTL8211B_PHYSTAT_SPDDONE)) {
593 /* Timeout reached ? */
594 if (i > PHY_AUTONEGOTIATE_TIMEOUT) {
595 puts(" TIMEOUT !\n");
600 if ((i++ % 1000) == 0) {
603 udelay(1000); /* 1 ms */
604 mii_reg = read_phy_reg(priv, MIIM_RTL8211B_PHY_STATUS);
607 udelay(500000); /* another 500 ms (results in faster booting) */
609 if (mii_reg & MIIM_RTL8211B_PHYSTAT_LINK)
615 if (mii_reg & MIIM_RTL8211B_PHYSTAT_DUPLEX)
620 speed = (mii_reg & MIIM_RTL8211B_PHYSTAT_SPEED);
623 case MIIM_RTL8211B_PHYSTAT_GBIT:
626 case MIIM_RTL8211B_PHYSTAT_100:
636 /* Parse the cis8201's status register for speed and duplex
639 uint mii_parse_cis8201(uint mii_reg, struct tsec_private * priv)
643 if (mii_reg & MIIM_CIS8201_AUXCONSTAT_DUPLEX)
648 speed = mii_reg & MIIM_CIS8201_AUXCONSTAT_SPEED;
650 case MIIM_CIS8201_AUXCONSTAT_GBIT:
653 case MIIM_CIS8201_AUXCONSTAT_100:
664 /* Parse the vsc8244's status register for speed and duplex
667 uint mii_parse_vsc8244(uint mii_reg, struct tsec_private * priv)
671 if (mii_reg & MIIM_VSC8244_AUXCONSTAT_DUPLEX)
676 speed = mii_reg & MIIM_VSC8244_AUXCONSTAT_SPEED;
678 case MIIM_VSC8244_AUXCONSTAT_GBIT:
681 case MIIM_VSC8244_AUXCONSTAT_100:
692 /* Parse the DM9161's status register for speed and duplex
695 uint mii_parse_dm9161_scsr(uint mii_reg, struct tsec_private * priv)
697 if (mii_reg & (MIIM_DM9161_SCSR_100F | MIIM_DM9161_SCSR_100H))
702 if (mii_reg & (MIIM_DM9161_SCSR_100F | MIIM_DM9161_SCSR_10F))
711 * Hack to write all 4 PHYs with the LED values
713 uint mii_cis8204_fixled(uint mii_reg, struct tsec_private * priv)
716 volatile tsec_t *regbase = priv->phyregs;
717 int timeout = 1000000;
719 for (phyid = 0; phyid < 4; phyid++) {
720 regbase->miimadd = (phyid << 8) | mii_reg;
721 regbase->miimcon = MIIM_CIS8204_SLEDCON_INIT;
725 while ((regbase->miimind & MIIMIND_BUSY) && timeout--) ;
728 return MIIM_CIS8204_SLEDCON_INIT;
731 uint mii_cis8204_setmode(uint mii_reg, struct tsec_private * priv)
733 if (priv->flags & TSEC_REDUCED)
734 return MIIM_CIS8204_EPHYCON_INIT | MIIM_CIS8204_EPHYCON_RGMII;
736 return MIIM_CIS8204_EPHYCON_INIT;
739 uint mii_m88e1111s_setmode(uint mii_reg, struct tsec_private *priv)
741 uint mii_data = read_phy_reg(priv, mii_reg);
743 if (priv->flags & TSEC_REDUCED)
744 mii_data = (mii_data & 0xfff0) | 0x000b;
748 /* Initialized required registers to appropriate values, zeroing
749 * those we don't care about (unless zero is bad, in which case,
750 * choose a more appropriate value)
752 static void init_registers(volatile tsec_t * regs)
755 regs->ievent = IEVENT_INIT_CLEAR;
757 regs->imask = IMASK_INIT_CLEAR;
759 regs->hash.iaddr0 = 0;
760 regs->hash.iaddr1 = 0;
761 regs->hash.iaddr2 = 0;
762 regs->hash.iaddr3 = 0;
763 regs->hash.iaddr4 = 0;
764 regs->hash.iaddr5 = 0;
765 regs->hash.iaddr6 = 0;
766 regs->hash.iaddr7 = 0;
768 regs->hash.gaddr0 = 0;
769 regs->hash.gaddr1 = 0;
770 regs->hash.gaddr2 = 0;
771 regs->hash.gaddr3 = 0;
772 regs->hash.gaddr4 = 0;
773 regs->hash.gaddr5 = 0;
774 regs->hash.gaddr6 = 0;
775 regs->hash.gaddr7 = 0;
777 regs->rctrl = 0x00000000;
779 /* Init RMON mib registers */
780 memset((void *)&(regs->rmon), 0, sizeof(rmon_mib_t));
782 regs->rmon.cam1 = 0xffffffff;
783 regs->rmon.cam2 = 0xffffffff;
785 regs->mrblr = MRBLR_INIT_SETTINGS;
787 regs->minflr = MINFLR_INIT_SETTINGS;
789 regs->attr = ATTR_INIT_SETTINGS;
790 regs->attreli = ATTRELI_INIT_SETTINGS;
794 /* Configure maccfg2 based on negotiated speed and duplex
795 * reported by PHY handling code
797 static void adjust_link(struct eth_device *dev)
799 struct tsec_private *priv = (struct tsec_private *)dev->priv;
800 volatile tsec_t *regs = priv->regs;
803 if (priv->duplexity != 0)
804 regs->maccfg2 |= MACCFG2_FULL_DUPLEX;
806 regs->maccfg2 &= ~(MACCFG2_FULL_DUPLEX);
808 switch (priv->speed) {
810 regs->maccfg2 = ((regs->maccfg2 & ~(MACCFG2_IF))
815 regs->maccfg2 = ((regs->maccfg2 & ~(MACCFG2_IF))
818 /* Set R100 bit in all modes although
819 * it is only used in RGMII mode
821 if (priv->speed == 100)
822 regs->ecntrl |= ECNTRL_R100;
824 regs->ecntrl &= ~(ECNTRL_R100);
827 printf("%s: Speed was bad\n", dev->name);
831 printf("Speed: %d, %s duplex\n", priv->speed,
832 (priv->duplexity) ? "full" : "half");
835 printf("%s: No link.\n", dev->name);
839 /* Set up the buffers and their descriptors, and bring up the
842 static void startup_tsec(struct eth_device *dev)
845 struct tsec_private *priv = (struct tsec_private *)dev->priv;
846 volatile tsec_t *regs = priv->regs;
848 /* Point to the buffer descriptors */
849 regs->tbase = (unsigned int)(&rtx.txbd[txIdx]);
850 regs->rbase = (unsigned int)(&rtx.rxbd[rxIdx]);
852 /* Initialize the Rx Buffer descriptors */
853 for (i = 0; i < PKTBUFSRX; i++) {
854 rtx.rxbd[i].status = RXBD_EMPTY;
855 rtx.rxbd[i].length = 0;
856 rtx.rxbd[i].bufPtr = (uint) NetRxPackets[i];
858 rtx.rxbd[PKTBUFSRX - 1].status |= RXBD_WRAP;
860 /* Initialize the TX Buffer Descriptors */
861 for (i = 0; i < TX_BUF_CNT; i++) {
862 rtx.txbd[i].status = 0;
863 rtx.txbd[i].length = 0;
864 rtx.txbd[i].bufPtr = 0;
866 rtx.txbd[TX_BUF_CNT - 1].status |= TXBD_WRAP;
868 /* Start up the PHY */
870 phy_run_commands(priv, priv->phyinfo->startup);
874 /* Enable Transmit and Receive */
875 regs->maccfg1 |= (MACCFG1_RX_EN | MACCFG1_TX_EN);
877 /* Tell the DMA it is clear to go */
878 regs->dmactrl |= DMACTRL_INIT_SETTINGS;
879 regs->tstat = TSTAT_CLEAR_THALT;
880 regs->rstat = RSTAT_CLEAR_RHALT;
881 regs->dmactrl &= ~(DMACTRL_GRS | DMACTRL_GTS);
884 /* This returns the status bits of the device. The return value
885 * is never checked, and this is what the 8260 driver did, so we
886 * do the same. Presumably, this would be zero if there were no
889 static int tsec_send(struct eth_device *dev, volatile void *packet, int length)
893 struct tsec_private *priv = (struct tsec_private *)dev->priv;
894 volatile tsec_t *regs = priv->regs;
896 /* Find an empty buffer descriptor */
897 for (i = 0; rtx.txbd[txIdx].status & TXBD_READY; i++) {
898 if (i >= TOUT_LOOP) {
899 debug("%s: tsec: tx buffers full\n", dev->name);
904 rtx.txbd[txIdx].bufPtr = (uint) packet;
905 rtx.txbd[txIdx].length = length;
906 rtx.txbd[txIdx].status |=
907 (TXBD_READY | TXBD_LAST | TXBD_CRC | TXBD_INTERRUPT);
909 /* Tell the DMA to go */
910 regs->tstat = TSTAT_CLEAR_THALT;
912 /* Wait for buffer to be transmitted */
913 for (i = 0; rtx.txbd[txIdx].status & TXBD_READY; i++) {
914 if (i >= TOUT_LOOP) {
915 debug("%s: tsec: tx error\n", dev->name);
920 txIdx = (txIdx + 1) % TX_BUF_CNT;
921 result = rtx.txbd[txIdx].status & TXBD_STATS;
926 static int tsec_recv(struct eth_device *dev)
929 struct tsec_private *priv = (struct tsec_private *)dev->priv;
930 volatile tsec_t *regs = priv->regs;
932 while (!(rtx.rxbd[rxIdx].status & RXBD_EMPTY)) {
934 length = rtx.rxbd[rxIdx].length;
936 /* Send the packet up if there were no errors */
937 if (!(rtx.rxbd[rxIdx].status & RXBD_STATS)) {
938 NetReceive(NetRxPackets[rxIdx], length - 4);
940 printf("Got error %x\n",
941 (rtx.rxbd[rxIdx].status & RXBD_STATS));
944 rtx.rxbd[rxIdx].length = 0;
946 /* Set the wrap bit if this is the last element in the list */
947 rtx.rxbd[rxIdx].status =
948 RXBD_EMPTY | (((rxIdx + 1) == PKTBUFSRX) ? RXBD_WRAP : 0);
950 rxIdx = (rxIdx + 1) % PKTBUFSRX;
953 if (regs->ievent & IEVENT_BSY) {
954 regs->ievent = IEVENT_BSY;
955 regs->rstat = RSTAT_CLEAR_RHALT;
962 /* Stop the interface */
963 static void tsec_halt(struct eth_device *dev)
965 struct tsec_private *priv = (struct tsec_private *)dev->priv;
966 volatile tsec_t *regs = priv->regs;
968 regs->dmactrl &= ~(DMACTRL_GRS | DMACTRL_GTS);
969 regs->dmactrl |= (DMACTRL_GRS | DMACTRL_GTS);
971 while (!(regs->ievent & (IEVENT_GRSC | IEVENT_GTSC))) ;
973 regs->maccfg1 &= ~(MACCFG1_TX_EN | MACCFG1_RX_EN);
975 /* Shut down the PHY, as needed */
977 phy_run_commands(priv, priv->phyinfo->shutdown);
980 struct phy_info phy_info_M88E1149S = {
984 (struct phy_cmd[]){ /* config */
985 /* Reset and configure the PHY */
986 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
988 {0x1e, 0x200c, NULL},
992 {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
993 {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
994 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
995 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
998 (struct phy_cmd[]){ /* startup */
999 /* Status is read once to clear old link state */
1000 {MIIM_STATUS, miim_read, NULL},
1001 /* Auto-negotiate */
1002 {MIIM_STATUS, miim_read, &mii_parse_sr},
1003 /* Read the status */
1004 {MIIM_88E1011_PHY_STATUS, miim_read,
1005 &mii_parse_88E1011_psr},
1008 (struct phy_cmd[]){ /* shutdown */
1013 /* The 5411 id is 0x206070, the 5421 is 0x2060e0 */
1014 struct phy_info phy_info_BCM5461S = {
1015 0x02060c1, /* 5461 ID */
1016 "Broadcom BCM5461S",
1017 0, /* not clear to me what minor revisions we can shift away */
1018 (struct phy_cmd[]) { /* config */
1019 /* Reset and configure the PHY */
1020 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
1021 {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
1022 {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
1023 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
1024 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
1027 (struct phy_cmd[]) { /* startup */
1028 /* Status is read once to clear old link state */
1029 {MIIM_STATUS, miim_read, NULL},
1030 /* Auto-negotiate */
1031 {MIIM_STATUS, miim_read, &mii_parse_sr},
1032 /* Read the status */
1033 {MIIM_BCM54xx_AUXSTATUS, miim_read, &mii_parse_BCM54xx_sr},
1036 (struct phy_cmd[]) { /* shutdown */
1041 struct phy_info phy_info_BCM5464S = {
1042 0x02060b1, /* 5464 ID */
1043 "Broadcom BCM5464S",
1044 0, /* not clear to me what minor revisions we can shift away */
1045 (struct phy_cmd[]) { /* config */
1046 /* Reset and configure the PHY */
1047 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
1048 {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
1049 {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
1050 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
1051 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
1054 (struct phy_cmd[]) { /* startup */
1055 /* Status is read once to clear old link state */
1056 {MIIM_STATUS, miim_read, NULL},
1057 /* Auto-negotiate */
1058 {MIIM_STATUS, miim_read, &mii_parse_sr},
1059 /* Read the status */
1060 {MIIM_BCM54xx_AUXSTATUS, miim_read, &mii_parse_BCM54xx_sr},
1063 (struct phy_cmd[]) { /* shutdown */
1068 struct phy_info phy_info_M88E1011S = {
1072 (struct phy_cmd[]){ /* config */
1073 /* Reset and configure the PHY */
1074 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
1076 {0x1e, 0x200c, NULL},
1079 {0x1e, 0x100, NULL},
1080 {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
1081 {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
1082 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
1083 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
1086 (struct phy_cmd[]){ /* startup */
1087 /* Status is read once to clear old link state */
1088 {MIIM_STATUS, miim_read, NULL},
1089 /* Auto-negotiate */
1090 {MIIM_STATUS, miim_read, &mii_parse_sr},
1091 /* Read the status */
1092 {MIIM_88E1011_PHY_STATUS, miim_read,
1093 &mii_parse_88E1011_psr},
1096 (struct phy_cmd[]){ /* shutdown */
1101 struct phy_info phy_info_M88E1111S = {
1105 (struct phy_cmd[]){ /* config */
1106 /* Reset and configure the PHY */
1107 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
1108 {0x1b, 0x848f, &mii_m88e1111s_setmode},
1109 {0x14, 0x0cd2, NULL}, /* Delay RGMII TX and RX */
1110 {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
1111 {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
1112 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
1113 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
1116 (struct phy_cmd[]){ /* startup */
1117 /* Status is read once to clear old link state */
1118 {MIIM_STATUS, miim_read, NULL},
1119 /* Auto-negotiate */
1120 {MIIM_STATUS, miim_read, &mii_parse_sr},
1121 /* Read the status */
1122 {MIIM_88E1011_PHY_STATUS, miim_read,
1123 &mii_parse_88E1011_psr},
1126 (struct phy_cmd[]){ /* shutdown */
1131 static unsigned int m88e1145_setmode(uint mii_reg, struct tsec_private *priv)
1133 uint mii_data = read_phy_reg(priv, mii_reg);
1135 /* Setting MIIM_88E1145_PHY_EXT_CR */
1136 if (priv->flags & TSEC_REDUCED)
1138 MIIM_M88E1145_RGMII_RX_DELAY | MIIM_M88E1145_RGMII_TX_DELAY;
1143 static struct phy_info phy_info_M88E1145 = {
1147 (struct phy_cmd[]){ /* config */
1149 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
1157 /* Configure the PHY */
1158 {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
1159 {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
1160 {MIIM_88E1011_PHY_SCR, MIIM_88E1011_PHY_MDI_X_AUTO,
1162 {MIIM_88E1145_PHY_EXT_CR, 0, &m88e1145_setmode},
1163 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
1164 {MIIM_CONTROL, MIIM_CONTROL_INIT, NULL},
1167 (struct phy_cmd[]){ /* startup */
1168 /* Status is read once to clear old link state */
1169 {MIIM_STATUS, miim_read, NULL},
1170 /* Auto-negotiate */
1171 {MIIM_STATUS, miim_read, &mii_parse_sr},
1172 {MIIM_88E1111_PHY_LED_CONTROL,
1173 MIIM_88E1111_PHY_LED_DIRECT, NULL},
1174 /* Read the Status */
1175 {MIIM_88E1011_PHY_STATUS, miim_read,
1176 &mii_parse_88E1011_psr},
1179 (struct phy_cmd[]){ /* shutdown */
1184 struct phy_info phy_info_cis8204 = {
1188 (struct phy_cmd[]){ /* config */
1189 /* Override PHY config settings */
1190 {MIIM_CIS8201_AUX_CONSTAT,
1191 MIIM_CIS8201_AUXCONSTAT_INIT, NULL},
1192 /* Configure some basic stuff */
1193 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
1194 {MIIM_CIS8204_SLED_CON, MIIM_CIS8204_SLEDCON_INIT,
1195 &mii_cis8204_fixled},
1196 {MIIM_CIS8204_EPHY_CON, MIIM_CIS8204_EPHYCON_INIT,
1197 &mii_cis8204_setmode},
1200 (struct phy_cmd[]){ /* startup */
1201 /* Read the Status (2x to make sure link is right) */
1202 {MIIM_STATUS, miim_read, NULL},
1203 /* Auto-negotiate */
1204 {MIIM_STATUS, miim_read, &mii_parse_sr},
1205 /* Read the status */
1206 {MIIM_CIS8201_AUX_CONSTAT, miim_read,
1207 &mii_parse_cis8201},
1210 (struct phy_cmd[]){ /* shutdown */
1216 struct phy_info phy_info_cis8201 = {
1220 (struct phy_cmd[]){ /* config */
1221 /* Override PHY config settings */
1222 {MIIM_CIS8201_AUX_CONSTAT,
1223 MIIM_CIS8201_AUXCONSTAT_INIT, NULL},
1224 /* Set up the interface mode */
1225 {MIIM_CIS8201_EXT_CON1, MIIM_CIS8201_EXTCON1_INIT,
1227 /* Configure some basic stuff */
1228 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
1231 (struct phy_cmd[]){ /* startup */
1232 /* Read the Status (2x to make sure link is right) */
1233 {MIIM_STATUS, miim_read, NULL},
1234 /* Auto-negotiate */
1235 {MIIM_STATUS, miim_read, &mii_parse_sr},
1236 /* Read the status */
1237 {MIIM_CIS8201_AUX_CONSTAT, miim_read,
1238 &mii_parse_cis8201},
1241 (struct phy_cmd[]){ /* shutdown */
1245 struct phy_info phy_info_VSC8244 = {
1249 (struct phy_cmd[]){ /* config */
1250 /* Override PHY config settings */
1251 /* Configure some basic stuff */
1252 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
1255 (struct phy_cmd[]){ /* startup */
1256 /* Read the Status (2x to make sure link is right) */
1257 {MIIM_STATUS, miim_read, NULL},
1258 /* Auto-negotiate */
1259 {MIIM_STATUS, miim_read, &mii_parse_sr},
1260 /* Read the status */
1261 {MIIM_VSC8244_AUX_CONSTAT, miim_read,
1262 &mii_parse_vsc8244},
1265 (struct phy_cmd[]){ /* shutdown */
1270 struct phy_info phy_info_dm9161 = {
1274 (struct phy_cmd[]){ /* config */
1275 {MIIM_CONTROL, MIIM_DM9161_CR_STOP, NULL},
1276 /* Do not bypass the scrambler/descrambler */
1277 {MIIM_DM9161_SCR, MIIM_DM9161_SCR_INIT, NULL},
1278 /* Clear 10BTCSR to default */
1279 {MIIM_DM9161_10BTCSR, MIIM_DM9161_10BTCSR_INIT,
1281 /* Configure some basic stuff */
1282 {MIIM_CONTROL, MIIM_CR_INIT, NULL},
1283 /* Restart Auto Negotiation */
1284 {MIIM_CONTROL, MIIM_DM9161_CR_RSTAN, NULL},
1287 (struct phy_cmd[]){ /* startup */
1288 /* Status is read once to clear old link state */
1289 {MIIM_STATUS, miim_read, NULL},
1290 /* Auto-negotiate */
1291 {MIIM_STATUS, miim_read, &mii_parse_sr},
1292 /* Read the status */
1293 {MIIM_DM9161_SCSR, miim_read,
1294 &mii_parse_dm9161_scsr},
1297 (struct phy_cmd[]){ /* shutdown */
1301 /* a generic flavor. */
1302 struct phy_info phy_info_generic = {
1304 "Unknown/Generic PHY",
1306 (struct phy_cmd[]) { /* config */
1307 {PHY_BMCR, PHY_BMCR_RESET, NULL},
1308 {PHY_BMCR, PHY_BMCR_AUTON|PHY_BMCR_RST_NEG, NULL},
1311 (struct phy_cmd[]) { /* startup */
1312 {PHY_BMSR, miim_read, NULL},
1313 {PHY_BMSR, miim_read, &mii_parse_sr},
1314 {PHY_BMSR, miim_read, &mii_parse_link},
1317 (struct phy_cmd[]) { /* shutdown */
1323 uint mii_parse_lxt971_sr2(uint mii_reg, struct tsec_private *priv)
1327 speed = mii_reg & MIIM_LXT971_SR2_SPEED_MASK;
1330 case MIIM_LXT971_SR2_10HDX:
1332 priv->duplexity = 0;
1334 case MIIM_LXT971_SR2_10FDX:
1336 priv->duplexity = 1;
1338 case MIIM_LXT971_SR2_100HDX:
1340 priv->duplexity = 0;
1344 priv->duplexity = 1;
1348 priv->duplexity = 0;
1354 static struct phy_info phy_info_lxt971 = {
1358 (struct phy_cmd[]){ /* config */
1359 {MIIM_CR, MIIM_CR_INIT, mii_cr_init}, /* autonegotiate */
1362 (struct phy_cmd[]){ /* startup - enable interrupts */
1363 /* { 0x12, 0x00f2, NULL }, */
1364 {MIIM_STATUS, miim_read, NULL},
1365 {MIIM_STATUS, miim_read, &mii_parse_sr},
1366 {MIIM_LXT971_SR2, miim_read, &mii_parse_lxt971_sr2},
1369 (struct phy_cmd[]){ /* shutdown - disable interrupts */
1374 /* Parse the DP83865's link and auto-neg status register for speed and duplex
1377 uint mii_parse_dp83865_lanr(uint mii_reg, struct tsec_private *priv)
1379 switch (mii_reg & MIIM_DP83865_SPD_MASK) {
1381 case MIIM_DP83865_SPD_1000:
1385 case MIIM_DP83865_SPD_100:
1395 if (mii_reg & MIIM_DP83865_DPX_FULL)
1396 priv->duplexity = 1;
1398 priv->duplexity = 0;
1403 struct phy_info phy_info_dp83865 = {
1407 (struct phy_cmd[]){ /* config */
1408 {MIIM_CONTROL, MIIM_DP83865_CR_INIT, NULL},
1411 (struct phy_cmd[]){ /* startup */
1412 /* Status is read once to clear old link state */
1413 {MIIM_STATUS, miim_read, NULL},
1414 /* Auto-negotiate */
1415 {MIIM_STATUS, miim_read, &mii_parse_sr},
1416 /* Read the link and auto-neg status */
1417 {MIIM_DP83865_LANR, miim_read,
1418 &mii_parse_dp83865_lanr},
1421 (struct phy_cmd[]){ /* shutdown */
1426 struct phy_info phy_info_rtl8211b = {
1430 (struct phy_cmd[]){ /* config */
1431 /* Reset and configure the PHY */
1432 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
1433 {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
1434 {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
1435 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
1436 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
1439 (struct phy_cmd[]){ /* startup */
1440 /* Status is read once to clear old link state */
1441 {MIIM_STATUS, miim_read, NULL},
1442 /* Auto-negotiate */
1443 {MIIM_STATUS, miim_read, &mii_parse_sr},
1444 /* Read the status */
1445 {MIIM_RTL8211B_PHY_STATUS, miim_read, &mii_parse_RTL8211B_sr},
1448 (struct phy_cmd[]){ /* shutdown */
1453 struct phy_info *phy_info[] = {
1458 &phy_info_M88E1011S,
1459 &phy_info_M88E1111S,
1461 &phy_info_M88E1149S,
1471 /* Grab the identifier of the device's PHY, and search through
1472 * all of the known PHYs to see if one matches. If so, return
1473 * it, if not, return NULL
1475 struct phy_info *get_phy_info(struct eth_device *dev)
1477 struct tsec_private *priv = (struct tsec_private *)dev->priv;
1478 uint phy_reg, phy_ID;
1480 struct phy_info *theInfo = NULL;
1482 /* Grab the bits from PHYIR1, and put them in the upper half */
1483 phy_reg = read_phy_reg(priv, MIIM_PHYIR1);
1484 phy_ID = (phy_reg & 0xffff) << 16;
1486 /* Grab the bits from PHYIR2, and put them in the lower half */
1487 phy_reg = read_phy_reg(priv, MIIM_PHYIR2);
1488 phy_ID |= (phy_reg & 0xffff);
1490 /* loop through all the known PHY types, and find one that */
1491 /* matches the ID we read from the PHY. */
1492 for (i = 0; phy_info[i]; i++) {
1493 if (phy_info[i]->id == (phy_ID >> phy_info[i]->shift)) {
1494 theInfo = phy_info[i];
1499 if (theInfo == NULL) {
1500 printf("%s: PHY id %x is not supported!\n", dev->name, phy_ID);
1503 debug("%s: PHY is %s (%x)\n", dev->name, theInfo->name, phy_ID);
1509 /* Execute the given series of commands on the given device's
1510 * PHY, running functions as necessary
1512 void phy_run_commands(struct tsec_private *priv, struct phy_cmd *cmd)
1516 volatile tsec_t *phyregs = priv->phyregs;
1518 phyregs->miimcfg = MIIMCFG_RESET;
1520 phyregs->miimcfg = MIIMCFG_INIT_VALUE;
1522 while (phyregs->miimind & MIIMIND_BUSY) ;
1524 for (i = 0; cmd->mii_reg != miim_end; i++) {
1525 if (cmd->mii_data == miim_read) {
1526 result = read_phy_reg(priv, cmd->mii_reg);
1528 if (cmd->funct != NULL)
1529 (*(cmd->funct)) (result, priv);
1532 if (cmd->funct != NULL)
1533 result = (*(cmd->funct)) (cmd->mii_reg, priv);
1535 result = cmd->mii_data;
1537 write_phy_reg(priv, cmd->mii_reg, result);
1544 /* Relocate the function pointers in the phy cmd lists */
1545 static void relocate_cmds(void)
1547 struct phy_cmd **cmdlistptr;
1548 struct phy_cmd *cmd;
1551 for (i = 0; phy_info[i]; i++) {
1552 /* First thing's first: relocate the pointers to the
1553 * PHY command structures (the structs were done) */
1554 phy_info[i] = (struct phy_info *)((uint) phy_info[i]
1556 phy_info[i]->name += gd->reloc_off;
1557 phy_info[i]->config =
1558 (struct phy_cmd *)((uint) phy_info[i]->config
1560 phy_info[i]->startup =
1561 (struct phy_cmd *)((uint) phy_info[i]->startup
1563 phy_info[i]->shutdown =
1564 (struct phy_cmd *)((uint) phy_info[i]->shutdown
1567 cmdlistptr = &phy_info[i]->config;
1569 for (; cmdlistptr <= &phy_info[i]->shutdown; cmdlistptr++) {
1571 for (cmd = *cmdlistptr;
1572 cmd->mii_reg != miim_end;
1574 /* Only relocate non-NULL pointers */
1576 cmd->funct += gd->reloc_off;
1587 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) \
1588 && !defined(BITBANGMII)
1591 * Read a MII PHY register.
1596 static int tsec_miiphy_read(char *devname, unsigned char addr,
1597 unsigned char reg, unsigned short *value)
1600 struct tsec_private *priv = privlist[0];
1603 printf("Can't read PHY at address %d\n", addr);
1607 ret = (unsigned short)read_any_phy_reg(priv, addr, reg);
1614 * Write a MII PHY register.
1619 static int tsec_miiphy_write(char *devname, unsigned char addr,
1620 unsigned char reg, unsigned short value)
1622 struct tsec_private *priv = privlist[0];
1625 printf("Can't write PHY at address %d\n", addr);
1629 write_any_phy_reg(priv, addr, reg, value);
1636 #ifdef CONFIG_MCAST_TFTP
1638 /* CREDITS: linux gianfar driver, slightly adjusted... thanx. */
1640 /* Set the appropriate hash bit for the given addr */
1642 /* The algorithm works like so:
1643 * 1) Take the Destination Address (ie the multicast address), and
1644 * do a CRC on it (little endian), and reverse the bits of the
1646 * 2) Use the 8 most significant bits as a hash into a 256-entry
1647 * table. The table is controlled through 8 32-bit registers:
1648 * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is
1649 * gaddr7. This means that the 3 most significant bits in the
1650 * hash index which gaddr register to use, and the 5 other bits
1651 * indicate which bit (assuming an IBM numbering scheme, which
1652 * for PowerPC (tm) is usually the case) in the tregister holds
1655 tsec_mcast_addr (struct eth_device *dev, u8 mcast_mac, u8 set)
1657 struct tsec_private *priv = privlist[1];
1658 volatile tsec_t *regs = priv->regs;
1659 volatile u32 *reg_array, value;
1660 u8 result, whichbit, whichreg;
1662 result = (u8)((ether_crc(MAC_ADDR_LEN,mcast_mac) >> 24) & 0xff);
1663 whichbit = result & 0x1f; /* the 5 LSB = which bit to set */
1664 whichreg = result >> 5; /* the 3 MSB = which reg to set it in */
1665 value = (1 << (31-whichbit));
1667 reg_array = &(regs->hash.gaddr0);
1670 reg_array[whichreg] |= value;
1672 reg_array[whichreg] &= ~value;
1676 #endif /* Multicast TFTP ? */
1678 #endif /* CONFIG_TSEC_ENET */