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 */
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_phy_reg(struct tsec_private *priv, uint regnum, uint value)
246 volatile tsec_t *regbase = priv->phyregs;
247 uint phyid = priv->phyaddr;
248 int timeout = 1000000;
250 regbase->miimadd = (phyid << 8) | regnum;
251 regbase->miimcon = value;
255 while ((regbase->miimind & MIIMIND_BUSY) && timeout--) ;
258 /* Reads register regnum on the device's PHY through the
259 * registers specified in priv. It lowers and raises the read
260 * command, and waits for the data to become valid (miimind
261 * notvalid bit cleared), and the bus to cease activity (miimind
262 * busy bit cleared), and then returns the value
264 uint read_phy_reg(struct tsec_private *priv, uint regnum)
267 volatile tsec_t *regbase = priv->phyregs;
268 uint phyid = priv->phyaddr;
270 /* Put the address of the phy, and the register
271 * number into MIIMADD */
272 regbase->miimadd = (phyid << 8) | regnum;
274 /* Clear the command register, and wait */
275 regbase->miimcom = 0;
278 /* Initiate a read command, and wait */
279 regbase->miimcom = MIIM_READ_COMMAND;
282 /* Wait for the the indication that the read is done */
283 while ((regbase->miimind & (MIIMIND_NOTVALID | MIIMIND_BUSY))) ;
285 /* Grab the value read from the PHY */
286 value = regbase->miimstat;
291 /* Discover which PHY is attached to the device, and configure it
292 * properly. If the PHY is not recognized, then return 0
293 * (failure). Otherwise, return 1
295 static int init_phy(struct eth_device *dev)
297 struct tsec_private *priv = (struct tsec_private *)dev->priv;
298 struct phy_info *curphy;
299 volatile tsec_t *regs = (volatile tsec_t *)(TSEC_BASE_ADDR);
301 /* Assign a Physical address to the TBI */
302 regs->tbipa = CFG_TBIPA_VALUE;
303 regs = (volatile tsec_t *)(TSEC_BASE_ADDR + TSEC_SIZE);
304 regs->tbipa = CFG_TBIPA_VALUE;
307 /* Reset MII (due to new addresses) */
308 priv->phyregs->miimcfg = MIIMCFG_RESET;
310 priv->phyregs->miimcfg = MIIMCFG_INIT_VALUE;
312 while (priv->phyregs->miimind & MIIMIND_BUSY) ;
317 /* Get the cmd structure corresponding to the attached
319 curphy = get_phy_info(dev);
321 if (curphy == NULL) {
322 priv->phyinfo = NULL;
323 printf("%s: No PHY found\n", dev->name);
328 priv->phyinfo = curphy;
330 phy_run_commands(priv, priv->phyinfo->config);
336 * Returns which value to write to the control register.
337 * For 10/100, the value is slightly different
339 uint mii_cr_init(uint mii_reg, struct tsec_private * priv)
341 if (priv->flags & TSEC_GIGABIT)
342 return MIIM_CONTROL_INIT;
347 /* Parse the status register for link, and then do
350 uint mii_parse_sr(uint mii_reg, struct tsec_private * priv)
353 * Wait if the link is up, and autonegotiation is in progress
354 * (ie - we're capable and it's not done)
356 mii_reg = read_phy_reg(priv, MIIM_STATUS);
357 if ((mii_reg & MIIM_STATUS_LINK) && (mii_reg & PHY_BMSR_AUTN_ABLE)
358 && !(mii_reg & PHY_BMSR_AUTN_COMP)) {
361 puts("Waiting for PHY auto negotiation to complete");
362 while (!(mii_reg & PHY_BMSR_AUTN_COMP)) {
366 if (i > PHY_AUTONEGOTIATE_TIMEOUT) {
367 puts(" TIMEOUT !\n");
372 if ((i++ % 1000) == 0) {
375 udelay(1000); /* 1 ms */
376 mii_reg = read_phy_reg(priv, MIIM_STATUS);
380 udelay(500000); /* another 500 ms (results in faster booting) */
382 if (mii_reg & MIIM_STATUS_LINK)
391 /* Generic function which updates the speed and duplex. If
392 * autonegotiation is enabled, it uses the AND of the link
393 * partner's advertised capabilities and our advertised
394 * capabilities. If autonegotiation is disabled, we use the
395 * appropriate bits in the control register.
397 * Stolen from Linux's mii.c and phy_device.c
399 uint mii_parse_link(uint mii_reg, struct tsec_private *priv)
401 /* We're using autonegotiation */
402 if (mii_reg & PHY_BMSR_AUTN_ABLE) {
406 /* Check for gigabit capability */
407 if (mii_reg & PHY_BMSR_EXT) {
408 /* We want a list of states supported by
409 * both PHYs in the link
411 gblpa = read_phy_reg(priv, PHY_1000BTSR);
412 gblpa &= read_phy_reg(priv, PHY_1000BTCR) << 2;
415 /* Set the baseline so we only have to set them
416 * if they're different
421 /* Check the gigabit fields */
422 if (gblpa & (PHY_1000BTSR_1000FD | PHY_1000BTSR_1000HD)) {
425 if (gblpa & PHY_1000BTSR_1000FD)
432 lpa = read_phy_reg(priv, PHY_ANAR);
433 lpa &= read_phy_reg(priv, PHY_ANLPAR);
435 if (lpa & (PHY_ANLPAR_TXFD | PHY_ANLPAR_TX)) {
438 if (lpa & PHY_ANLPAR_TXFD)
441 } else if (lpa & PHY_ANLPAR_10FD)
444 uint bmcr = read_phy_reg(priv, PHY_BMCR);
449 if (bmcr & PHY_BMCR_DPLX)
452 if (bmcr & PHY_BMCR_1000_MBPS)
454 else if (bmcr & PHY_BMCR_100_MBPS)
462 * Parse the BCM54xx status register for speed and duplex information.
463 * The linux sungem_phy has this information, but in a table format.
465 uint mii_parse_BCM54xx_sr(uint mii_reg, struct tsec_private *priv)
468 switch((mii_reg & MIIM_BCM54xx_AUXSTATUS_LINKMODE_MASK) >> MIIM_BCM54xx_AUXSTATUS_LINKMODE_SHIFT){
471 printf("Enet starting in 10BT/HD\n");
477 printf("Enet starting in 10BT/FD\n");
483 printf("Enet starting in 100BT/HD\n");
489 printf("Enet starting in 100BT/FD\n");
495 printf("Enet starting in 1000BT/HD\n");
501 printf("Enet starting in 1000BT/FD\n");
507 printf("Auto-neg error, defaulting to 10BT/HD\n");
516 /* Parse the 88E1011's status register for speed and duplex
519 uint mii_parse_88E1011_psr(uint mii_reg, struct tsec_private * priv)
523 mii_reg = read_phy_reg(priv, MIIM_88E1011_PHY_STATUS);
525 if ((mii_reg & MIIM_88E1011_PHYSTAT_LINK) &&
526 !(mii_reg & MIIM_88E1011_PHYSTAT_SPDDONE)) {
529 puts("Waiting for PHY realtime link");
530 while (!(mii_reg & MIIM_88E1011_PHYSTAT_SPDDONE)) {
531 /* Timeout reached ? */
532 if (i > PHY_AUTONEGOTIATE_TIMEOUT) {
533 puts(" TIMEOUT !\n");
538 if ((i++ % 1000) == 0) {
541 udelay(1000); /* 1 ms */
542 mii_reg = read_phy_reg(priv, MIIM_88E1011_PHY_STATUS);
545 udelay(500000); /* another 500 ms (results in faster booting) */
547 if (mii_reg & MIIM_88E1011_PHYSTAT_LINK)
553 if (mii_reg & MIIM_88E1011_PHYSTAT_DUPLEX)
558 speed = (mii_reg & MIIM_88E1011_PHYSTAT_SPEED);
561 case MIIM_88E1011_PHYSTAT_GBIT:
564 case MIIM_88E1011_PHYSTAT_100:
574 /* Parse the cis8201's status register for speed and duplex
577 uint mii_parse_cis8201(uint mii_reg, struct tsec_private * priv)
581 if (mii_reg & MIIM_CIS8201_AUXCONSTAT_DUPLEX)
586 speed = mii_reg & MIIM_CIS8201_AUXCONSTAT_SPEED;
588 case MIIM_CIS8201_AUXCONSTAT_GBIT:
591 case MIIM_CIS8201_AUXCONSTAT_100:
602 /* Parse the vsc8244's status register for speed and duplex
605 uint mii_parse_vsc8244(uint mii_reg, struct tsec_private * priv)
609 if (mii_reg & MIIM_VSC8244_AUXCONSTAT_DUPLEX)
614 speed = mii_reg & MIIM_VSC8244_AUXCONSTAT_SPEED;
616 case MIIM_VSC8244_AUXCONSTAT_GBIT:
619 case MIIM_VSC8244_AUXCONSTAT_100:
630 /* Parse the DM9161's status register for speed and duplex
633 uint mii_parse_dm9161_scsr(uint mii_reg, struct tsec_private * priv)
635 if (mii_reg & (MIIM_DM9161_SCSR_100F | MIIM_DM9161_SCSR_100H))
640 if (mii_reg & (MIIM_DM9161_SCSR_100F | MIIM_DM9161_SCSR_10F))
649 * Hack to write all 4 PHYs with the LED values
651 uint mii_cis8204_fixled(uint mii_reg, struct tsec_private * priv)
654 volatile tsec_t *regbase = priv->phyregs;
655 int timeout = 1000000;
657 for (phyid = 0; phyid < 4; phyid++) {
658 regbase->miimadd = (phyid << 8) | mii_reg;
659 regbase->miimcon = MIIM_CIS8204_SLEDCON_INIT;
663 while ((regbase->miimind & MIIMIND_BUSY) && timeout--) ;
666 return MIIM_CIS8204_SLEDCON_INIT;
669 uint mii_cis8204_setmode(uint mii_reg, struct tsec_private * priv)
671 if (priv->flags & TSEC_REDUCED)
672 return MIIM_CIS8204_EPHYCON_INIT | MIIM_CIS8204_EPHYCON_RGMII;
674 return MIIM_CIS8204_EPHYCON_INIT;
677 uint mii_m88e1111s_setmode(uint mii_reg, struct tsec_private *priv)
679 uint mii_data = read_phy_reg(priv, mii_reg);
681 if (priv->flags & TSEC_REDUCED)
682 mii_data = (mii_data & 0xfff0) | 0x000b;
686 /* Initialized required registers to appropriate values, zeroing
687 * those we don't care about (unless zero is bad, in which case,
688 * choose a more appropriate value)
690 static void init_registers(volatile tsec_t * regs)
693 regs->ievent = IEVENT_INIT_CLEAR;
695 regs->imask = IMASK_INIT_CLEAR;
697 regs->hash.iaddr0 = 0;
698 regs->hash.iaddr1 = 0;
699 regs->hash.iaddr2 = 0;
700 regs->hash.iaddr3 = 0;
701 regs->hash.iaddr4 = 0;
702 regs->hash.iaddr5 = 0;
703 regs->hash.iaddr6 = 0;
704 regs->hash.iaddr7 = 0;
706 regs->hash.gaddr0 = 0;
707 regs->hash.gaddr1 = 0;
708 regs->hash.gaddr2 = 0;
709 regs->hash.gaddr3 = 0;
710 regs->hash.gaddr4 = 0;
711 regs->hash.gaddr5 = 0;
712 regs->hash.gaddr6 = 0;
713 regs->hash.gaddr7 = 0;
715 regs->rctrl = 0x00000000;
717 /* Init RMON mib registers */
718 memset((void *)&(regs->rmon), 0, sizeof(rmon_mib_t));
720 regs->rmon.cam1 = 0xffffffff;
721 regs->rmon.cam2 = 0xffffffff;
723 regs->mrblr = MRBLR_INIT_SETTINGS;
725 regs->minflr = MINFLR_INIT_SETTINGS;
727 regs->attr = ATTR_INIT_SETTINGS;
728 regs->attreli = ATTRELI_INIT_SETTINGS;
732 /* Configure maccfg2 based on negotiated speed and duplex
733 * reported by PHY handling code
735 static void adjust_link(struct eth_device *dev)
737 struct tsec_private *priv = (struct tsec_private *)dev->priv;
738 volatile tsec_t *regs = priv->regs;
741 if (priv->duplexity != 0)
742 regs->maccfg2 |= MACCFG2_FULL_DUPLEX;
744 regs->maccfg2 &= ~(MACCFG2_FULL_DUPLEX);
746 switch (priv->speed) {
748 regs->maccfg2 = ((regs->maccfg2 & ~(MACCFG2_IF))
753 regs->maccfg2 = ((regs->maccfg2 & ~(MACCFG2_IF))
756 /* Set R100 bit in all modes although
757 * it is only used in RGMII mode
759 if (priv->speed == 100)
760 regs->ecntrl |= ECNTRL_R100;
762 regs->ecntrl &= ~(ECNTRL_R100);
765 printf("%s: Speed was bad\n", dev->name);
769 printf("Speed: %d, %s duplex\n", priv->speed,
770 (priv->duplexity) ? "full" : "half");
773 printf("%s: No link.\n", dev->name);
777 /* Set up the buffers and their descriptors, and bring up the
780 static void startup_tsec(struct eth_device *dev)
783 struct tsec_private *priv = (struct tsec_private *)dev->priv;
784 volatile tsec_t *regs = priv->regs;
786 /* Point to the buffer descriptors */
787 regs->tbase = (unsigned int)(&rtx.txbd[txIdx]);
788 regs->rbase = (unsigned int)(&rtx.rxbd[rxIdx]);
790 /* Initialize the Rx Buffer descriptors */
791 for (i = 0; i < PKTBUFSRX; i++) {
792 rtx.rxbd[i].status = RXBD_EMPTY;
793 rtx.rxbd[i].length = 0;
794 rtx.rxbd[i].bufPtr = (uint) NetRxPackets[i];
796 rtx.rxbd[PKTBUFSRX - 1].status |= RXBD_WRAP;
798 /* Initialize the TX Buffer Descriptors */
799 for (i = 0; i < TX_BUF_CNT; i++) {
800 rtx.txbd[i].status = 0;
801 rtx.txbd[i].length = 0;
802 rtx.txbd[i].bufPtr = 0;
804 rtx.txbd[TX_BUF_CNT - 1].status |= TXBD_WRAP;
806 /* Start up the PHY */
808 phy_run_commands(priv, priv->phyinfo->startup);
812 /* Enable Transmit and Receive */
813 regs->maccfg1 |= (MACCFG1_RX_EN | MACCFG1_TX_EN);
815 /* Tell the DMA it is clear to go */
816 regs->dmactrl |= DMACTRL_INIT_SETTINGS;
817 regs->tstat = TSTAT_CLEAR_THALT;
818 regs->rstat = RSTAT_CLEAR_RHALT;
819 regs->dmactrl &= ~(DMACTRL_GRS | DMACTRL_GTS);
822 /* This returns the status bits of the device. The return value
823 * is never checked, and this is what the 8260 driver did, so we
824 * do the same. Presumably, this would be zero if there were no
827 static int tsec_send(struct eth_device *dev, volatile void *packet, int length)
831 struct tsec_private *priv = (struct tsec_private *)dev->priv;
832 volatile tsec_t *regs = priv->regs;
834 /* Find an empty buffer descriptor */
835 for (i = 0; rtx.txbd[txIdx].status & TXBD_READY; i++) {
836 if (i >= TOUT_LOOP) {
837 debug("%s: tsec: tx buffers full\n", dev->name);
842 rtx.txbd[txIdx].bufPtr = (uint) packet;
843 rtx.txbd[txIdx].length = length;
844 rtx.txbd[txIdx].status |=
845 (TXBD_READY | TXBD_LAST | TXBD_CRC | TXBD_INTERRUPT);
847 /* Tell the DMA to go */
848 regs->tstat = TSTAT_CLEAR_THALT;
850 /* Wait for buffer to be transmitted */
851 for (i = 0; rtx.txbd[txIdx].status & TXBD_READY; i++) {
852 if (i >= TOUT_LOOP) {
853 debug("%s: tsec: tx error\n", dev->name);
858 txIdx = (txIdx + 1) % TX_BUF_CNT;
859 result = rtx.txbd[txIdx].status & TXBD_STATS;
864 static int tsec_recv(struct eth_device *dev)
867 struct tsec_private *priv = (struct tsec_private *)dev->priv;
868 volatile tsec_t *regs = priv->regs;
870 while (!(rtx.rxbd[rxIdx].status & RXBD_EMPTY)) {
872 length = rtx.rxbd[rxIdx].length;
874 /* Send the packet up if there were no errors */
875 if (!(rtx.rxbd[rxIdx].status & RXBD_STATS)) {
876 NetReceive(NetRxPackets[rxIdx], length - 4);
878 printf("Got error %x\n",
879 (rtx.rxbd[rxIdx].status & RXBD_STATS));
882 rtx.rxbd[rxIdx].length = 0;
884 /* Set the wrap bit if this is the last element in the list */
885 rtx.rxbd[rxIdx].status =
886 RXBD_EMPTY | (((rxIdx + 1) == PKTBUFSRX) ? RXBD_WRAP : 0);
888 rxIdx = (rxIdx + 1) % PKTBUFSRX;
891 if (regs->ievent & IEVENT_BSY) {
892 regs->ievent = IEVENT_BSY;
893 regs->rstat = RSTAT_CLEAR_RHALT;
900 /* Stop the interface */
901 static void tsec_halt(struct eth_device *dev)
903 struct tsec_private *priv = (struct tsec_private *)dev->priv;
904 volatile tsec_t *regs = priv->regs;
906 regs->dmactrl &= ~(DMACTRL_GRS | DMACTRL_GTS);
907 regs->dmactrl |= (DMACTRL_GRS | DMACTRL_GTS);
909 while (!(regs->ievent & (IEVENT_GRSC | IEVENT_GTSC))) ;
911 regs->maccfg1 &= ~(MACCFG1_TX_EN | MACCFG1_RX_EN);
913 /* Shut down the PHY, as needed */
915 phy_run_commands(priv, priv->phyinfo->shutdown);
918 struct phy_info phy_info_M88E1149S = {
922 (struct phy_cmd[]){ /* config */
923 /* Reset and configure the PHY */
924 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
926 {0x1e, 0x200c, NULL},
930 {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
931 {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
932 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
933 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
936 (struct phy_cmd[]){ /* startup */
937 /* Status is read once to clear old link state */
938 {MIIM_STATUS, miim_read, NULL},
940 {MIIM_STATUS, miim_read, &mii_parse_sr},
941 /* Read the status */
942 {MIIM_88E1011_PHY_STATUS, miim_read,
943 &mii_parse_88E1011_psr},
946 (struct phy_cmd[]){ /* shutdown */
951 /* The 5411 id is 0x206070, the 5421 is 0x2060e0 */
952 struct phy_info phy_info_BCM5461S = {
953 0x02060c1, /* 5461 ID */
955 0, /* not clear to me what minor revisions we can shift away */
956 (struct phy_cmd[]) { /* config */
957 /* Reset and configure the PHY */
958 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
959 {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
960 {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
961 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
962 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
965 (struct phy_cmd[]) { /* startup */
966 /* Status is read once to clear old link state */
967 {MIIM_STATUS, miim_read, NULL},
969 {MIIM_STATUS, miim_read, &mii_parse_sr},
970 /* Read the status */
971 {MIIM_BCM54xx_AUXSTATUS, miim_read, &mii_parse_BCM54xx_sr},
974 (struct phy_cmd[]) { /* shutdown */
979 struct phy_info phy_info_BCM5464S = {
980 0x02060b1, /* 5464 ID */
982 0, /* not clear to me what minor revisions we can shift away */
983 (struct phy_cmd[]) { /* config */
984 /* Reset and configure the PHY */
985 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
986 {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
987 {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
988 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
989 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
992 (struct phy_cmd[]) { /* startup */
993 /* Status is read once to clear old link state */
994 {MIIM_STATUS, miim_read, NULL},
996 {MIIM_STATUS, miim_read, &mii_parse_sr},
997 /* Read the status */
998 {MIIM_BCM54xx_AUXSTATUS, miim_read, &mii_parse_BCM54xx_sr},
1001 (struct phy_cmd[]) { /* shutdown */
1006 struct phy_info phy_info_M88E1011S = {
1010 (struct phy_cmd[]){ /* config */
1011 /* Reset and configure the PHY */
1012 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
1014 {0x1e, 0x200c, NULL},
1017 {0x1e, 0x100, NULL},
1018 {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
1019 {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
1020 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
1021 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
1024 (struct phy_cmd[]){ /* startup */
1025 /* Status is read once to clear old link state */
1026 {MIIM_STATUS, miim_read, NULL},
1027 /* Auto-negotiate */
1028 {MIIM_STATUS, miim_read, &mii_parse_sr},
1029 /* Read the status */
1030 {MIIM_88E1011_PHY_STATUS, miim_read,
1031 &mii_parse_88E1011_psr},
1034 (struct phy_cmd[]){ /* shutdown */
1039 struct phy_info phy_info_M88E1111S = {
1043 (struct phy_cmd[]){ /* config */
1044 /* Reset and configure the PHY */
1045 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
1046 {0x1b, 0x848f, &mii_m88e1111s_setmode},
1047 {0x14, 0x0cd2, NULL}, /* Delay RGMII TX and RX */
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_88E1011_PHY_STATUS, miim_read,
1061 &mii_parse_88E1011_psr},
1064 (struct phy_cmd[]){ /* shutdown */
1069 static unsigned int m88e1145_setmode(uint mii_reg, struct tsec_private *priv)
1071 uint mii_data = read_phy_reg(priv, mii_reg);
1073 /* Setting MIIM_88E1145_PHY_EXT_CR */
1074 if (priv->flags & TSEC_REDUCED)
1076 MIIM_M88E1145_RGMII_RX_DELAY | MIIM_M88E1145_RGMII_TX_DELAY;
1081 static struct phy_info phy_info_M88E1145 = {
1085 (struct phy_cmd[]){ /* config */
1087 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
1095 /* Configure the PHY */
1096 {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
1097 {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
1098 {MIIM_88E1011_PHY_SCR, MIIM_88E1011_PHY_MDI_X_AUTO,
1100 {MIIM_88E1145_PHY_EXT_CR, 0, &m88e1145_setmode},
1101 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
1102 {MIIM_CONTROL, MIIM_CONTROL_INIT, NULL},
1105 (struct phy_cmd[]){ /* startup */
1106 /* Status is read once to clear old link state */
1107 {MIIM_STATUS, miim_read, NULL},
1108 /* Auto-negotiate */
1109 {MIIM_STATUS, miim_read, &mii_parse_sr},
1110 {MIIM_88E1111_PHY_LED_CONTROL,
1111 MIIM_88E1111_PHY_LED_DIRECT, NULL},
1112 /* Read the Status */
1113 {MIIM_88E1011_PHY_STATUS, miim_read,
1114 &mii_parse_88E1011_psr},
1117 (struct phy_cmd[]){ /* shutdown */
1122 struct phy_info phy_info_cis8204 = {
1126 (struct phy_cmd[]){ /* config */
1127 /* Override PHY config settings */
1128 {MIIM_CIS8201_AUX_CONSTAT,
1129 MIIM_CIS8201_AUXCONSTAT_INIT, NULL},
1130 /* Configure some basic stuff */
1131 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
1132 {MIIM_CIS8204_SLED_CON, MIIM_CIS8204_SLEDCON_INIT,
1133 &mii_cis8204_fixled},
1134 {MIIM_CIS8204_EPHY_CON, MIIM_CIS8204_EPHYCON_INIT,
1135 &mii_cis8204_setmode},
1138 (struct phy_cmd[]){ /* startup */
1139 /* Read the Status (2x to make sure link is right) */
1140 {MIIM_STATUS, miim_read, NULL},
1141 /* Auto-negotiate */
1142 {MIIM_STATUS, miim_read, &mii_parse_sr},
1143 /* Read the status */
1144 {MIIM_CIS8201_AUX_CONSTAT, miim_read,
1145 &mii_parse_cis8201},
1148 (struct phy_cmd[]){ /* shutdown */
1154 struct phy_info phy_info_cis8201 = {
1158 (struct phy_cmd[]){ /* config */
1159 /* Override PHY config settings */
1160 {MIIM_CIS8201_AUX_CONSTAT,
1161 MIIM_CIS8201_AUXCONSTAT_INIT, NULL},
1162 /* Set up the interface mode */
1163 {MIIM_CIS8201_EXT_CON1, MIIM_CIS8201_EXTCON1_INIT,
1165 /* Configure some basic stuff */
1166 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
1169 (struct phy_cmd[]){ /* startup */
1170 /* Read the Status (2x to make sure link is right) */
1171 {MIIM_STATUS, miim_read, NULL},
1172 /* Auto-negotiate */
1173 {MIIM_STATUS, miim_read, &mii_parse_sr},
1174 /* Read the status */
1175 {MIIM_CIS8201_AUX_CONSTAT, miim_read,
1176 &mii_parse_cis8201},
1179 (struct phy_cmd[]){ /* shutdown */
1183 struct phy_info phy_info_VSC8244 = {
1187 (struct phy_cmd[]){ /* config */
1188 /* Override PHY config settings */
1189 /* Configure some basic stuff */
1190 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
1193 (struct phy_cmd[]){ /* startup */
1194 /* Read the Status (2x to make sure link is right) */
1195 {MIIM_STATUS, miim_read, NULL},
1196 /* Auto-negotiate */
1197 {MIIM_STATUS, miim_read, &mii_parse_sr},
1198 /* Read the status */
1199 {MIIM_VSC8244_AUX_CONSTAT, miim_read,
1200 &mii_parse_vsc8244},
1203 (struct phy_cmd[]){ /* shutdown */
1208 struct phy_info phy_info_dm9161 = {
1212 (struct phy_cmd[]){ /* config */
1213 {MIIM_CONTROL, MIIM_DM9161_CR_STOP, NULL},
1214 /* Do not bypass the scrambler/descrambler */
1215 {MIIM_DM9161_SCR, MIIM_DM9161_SCR_INIT, NULL},
1216 /* Clear 10BTCSR to default */
1217 {MIIM_DM9161_10BTCSR, MIIM_DM9161_10BTCSR_INIT,
1219 /* Configure some basic stuff */
1220 {MIIM_CONTROL, MIIM_CR_INIT, NULL},
1221 /* Restart Auto Negotiation */
1222 {MIIM_CONTROL, MIIM_DM9161_CR_RSTAN, NULL},
1225 (struct phy_cmd[]){ /* startup */
1226 /* Status is read once to clear old link state */
1227 {MIIM_STATUS, miim_read, NULL},
1228 /* Auto-negotiate */
1229 {MIIM_STATUS, miim_read, &mii_parse_sr},
1230 /* Read the status */
1231 {MIIM_DM9161_SCSR, miim_read,
1232 &mii_parse_dm9161_scsr},
1235 (struct phy_cmd[]){ /* shutdown */
1239 /* a generic flavor. */
1240 struct phy_info phy_info_generic = {
1242 "Unknown/Generic PHY",
1244 (struct phy_cmd[]) { /* config */
1245 {PHY_BMCR, PHY_BMCR_RESET, NULL},
1246 {PHY_BMCR, PHY_BMCR_AUTON|PHY_BMCR_RST_NEG, NULL},
1249 (struct phy_cmd[]) { /* startup */
1250 {PHY_BMSR, miim_read, NULL},
1251 {PHY_BMSR, miim_read, &mii_parse_sr},
1252 {PHY_BMSR, miim_read, &mii_parse_link},
1255 (struct phy_cmd[]) { /* shutdown */
1261 uint mii_parse_lxt971_sr2(uint mii_reg, struct tsec_private *priv)
1265 speed = mii_reg & MIIM_LXT971_SR2_SPEED_MASK;
1268 case MIIM_LXT971_SR2_10HDX:
1270 priv->duplexity = 0;
1272 case MIIM_LXT971_SR2_10FDX:
1274 priv->duplexity = 1;
1276 case MIIM_LXT971_SR2_100HDX:
1278 priv->duplexity = 0;
1282 priv->duplexity = 1;
1286 priv->duplexity = 0;
1292 static struct phy_info phy_info_lxt971 = {
1296 (struct phy_cmd[]){ /* config */
1297 {MIIM_CR, MIIM_CR_INIT, mii_cr_init}, /* autonegotiate */
1300 (struct phy_cmd[]){ /* startup - enable interrupts */
1301 /* { 0x12, 0x00f2, NULL }, */
1302 {MIIM_STATUS, miim_read, NULL},
1303 {MIIM_STATUS, miim_read, &mii_parse_sr},
1304 {MIIM_LXT971_SR2, miim_read, &mii_parse_lxt971_sr2},
1307 (struct phy_cmd[]){ /* shutdown - disable interrupts */
1312 /* Parse the DP83865's link and auto-neg status register for speed and duplex
1315 uint mii_parse_dp83865_lanr(uint mii_reg, struct tsec_private *priv)
1317 switch (mii_reg & MIIM_DP83865_SPD_MASK) {
1319 case MIIM_DP83865_SPD_1000:
1323 case MIIM_DP83865_SPD_100:
1333 if (mii_reg & MIIM_DP83865_DPX_FULL)
1334 priv->duplexity = 1;
1336 priv->duplexity = 0;
1341 struct phy_info phy_info_dp83865 = {
1345 (struct phy_cmd[]){ /* config */
1346 {MIIM_CONTROL, MIIM_DP83865_CR_INIT, NULL},
1349 (struct phy_cmd[]){ /* startup */
1350 /* Status is read once to clear old link state */
1351 {MIIM_STATUS, miim_read, NULL},
1352 /* Auto-negotiate */
1353 {MIIM_STATUS, miim_read, &mii_parse_sr},
1354 /* Read the link and auto-neg status */
1355 {MIIM_DP83865_LANR, miim_read,
1356 &mii_parse_dp83865_lanr},
1359 (struct phy_cmd[]){ /* shutdown */
1364 struct phy_info *phy_info[] = {
1369 &phy_info_M88E1011S,
1370 &phy_info_M88E1111S,
1372 &phy_info_M88E1149S,
1381 /* Grab the identifier of the device's PHY, and search through
1382 * all of the known PHYs to see if one matches. If so, return
1383 * it, if not, return NULL
1385 struct phy_info *get_phy_info(struct eth_device *dev)
1387 struct tsec_private *priv = (struct tsec_private *)dev->priv;
1388 uint phy_reg, phy_ID;
1390 struct phy_info *theInfo = NULL;
1392 /* Grab the bits from PHYIR1, and put them in the upper half */
1393 phy_reg = read_phy_reg(priv, MIIM_PHYIR1);
1394 phy_ID = (phy_reg & 0xffff) << 16;
1396 /* Grab the bits from PHYIR2, and put them in the lower half */
1397 phy_reg = read_phy_reg(priv, MIIM_PHYIR2);
1398 phy_ID |= (phy_reg & 0xffff);
1400 /* loop through all the known PHY types, and find one that */
1401 /* matches the ID we read from the PHY. */
1402 for (i = 0; phy_info[i]; i++) {
1403 if (phy_info[i]->id == (phy_ID >> phy_info[i]->shift)) {
1404 theInfo = phy_info[i];
1409 if (theInfo == NULL) {
1410 printf("%s: PHY id %x is not supported!\n", dev->name, phy_ID);
1413 debug("%s: PHY is %s (%x)\n", dev->name, theInfo->name, phy_ID);
1419 /* Execute the given series of commands on the given device's
1420 * PHY, running functions as necessary
1422 void phy_run_commands(struct tsec_private *priv, struct phy_cmd *cmd)
1426 volatile tsec_t *phyregs = priv->phyregs;
1428 phyregs->miimcfg = MIIMCFG_RESET;
1430 phyregs->miimcfg = MIIMCFG_INIT_VALUE;
1432 while (phyregs->miimind & MIIMIND_BUSY) ;
1434 for (i = 0; cmd->mii_reg != miim_end; i++) {
1435 if (cmd->mii_data == miim_read) {
1436 result = read_phy_reg(priv, cmd->mii_reg);
1438 if (cmd->funct != NULL)
1439 (*(cmd->funct)) (result, priv);
1442 if (cmd->funct != NULL)
1443 result = (*(cmd->funct)) (cmd->mii_reg, priv);
1445 result = cmd->mii_data;
1447 write_phy_reg(priv, cmd->mii_reg, result);
1454 /* Relocate the function pointers in the phy cmd lists */
1455 static void relocate_cmds(void)
1457 struct phy_cmd **cmdlistptr;
1458 struct phy_cmd *cmd;
1461 for (i = 0; phy_info[i]; i++) {
1462 /* First thing's first: relocate the pointers to the
1463 * PHY command structures (the structs were done) */
1464 phy_info[i] = (struct phy_info *)((uint) phy_info[i]
1466 phy_info[i]->name += gd->reloc_off;
1467 phy_info[i]->config =
1468 (struct phy_cmd *)((uint) phy_info[i]->config
1470 phy_info[i]->startup =
1471 (struct phy_cmd *)((uint) phy_info[i]->startup
1473 phy_info[i]->shutdown =
1474 (struct phy_cmd *)((uint) phy_info[i]->shutdown
1477 cmdlistptr = &phy_info[i]->config;
1479 for (; cmdlistptr <= &phy_info[i]->shutdown; cmdlistptr++) {
1481 for (cmd = *cmdlistptr;
1482 cmd->mii_reg != miim_end;
1484 /* Only relocate non-NULL pointers */
1486 cmd->funct += gd->reloc_off;
1497 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) \
1498 && !defined(BITBANGMII)
1500 struct tsec_private *get_priv_for_phy(unsigned char phyaddr)
1504 for (i = 0; i < MAXCONTROLLERS; i++) {
1505 if (privlist[i]->phyaddr == phyaddr)
1513 * Read a MII PHY register.
1518 static int tsec_miiphy_read(char *devname, unsigned char addr,
1519 unsigned char reg, unsigned short *value)
1522 struct tsec_private *priv = get_priv_for_phy(addr);
1525 printf("Can't read PHY at address %d\n", addr);
1529 ret = (unsigned short)read_phy_reg(priv, reg);
1536 * Write a MII PHY register.
1541 static int tsec_miiphy_write(char *devname, unsigned char addr,
1542 unsigned char reg, unsigned short value)
1544 struct tsec_private *priv = get_priv_for_phy(addr);
1547 printf("Can't write PHY at address %d\n", addr);
1551 write_phy_reg(priv, reg, value);
1558 #ifdef CONFIG_MCAST_TFTP
1560 /* CREDITS: linux gianfar driver, slightly adjusted... thanx. */
1562 /* Set the appropriate hash bit for the given addr */
1564 /* The algorithm works like so:
1565 * 1) Take the Destination Address (ie the multicast address), and
1566 * do a CRC on it (little endian), and reverse the bits of the
1568 * 2) Use the 8 most significant bits as a hash into a 256-entry
1569 * table. The table is controlled through 8 32-bit registers:
1570 * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is
1571 * gaddr7. This means that the 3 most significant bits in the
1572 * hash index which gaddr register to use, and the 5 other bits
1573 * indicate which bit (assuming an IBM numbering scheme, which
1574 * for PowerPC (tm) is usually the case) in the tregister holds
1577 tsec_mcast_addr (struct eth_device *dev, u8 mcast_mac, u8 set)
1579 struct tsec_private *priv = privlist[1];
1580 volatile tsec_t *regs = priv->regs;
1581 volatile u32 *reg_array, value;
1582 u8 result, whichbit, whichreg;
1584 result = (u8)((ether_crc(MAC_ADDR_LEN,mcast_mac) >> 24) & 0xff);
1585 whichbit = result & 0x1f; /* the 5 LSB = which bit to set */
1586 whichreg = result >> 5; /* the 3 MSB = which reg to set it in */
1587 value = (1 << (31-whichbit));
1589 reg_array = &(regs->hash.gaddr0);
1592 reg_array[whichreg] |= value;
1594 reg_array[whichreg] &= ~value;
1598 #endif /* Multicast TFTP ? */
1600 #endif /* CONFIG_TSEC_ENET */