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 static int tsec_miiphy_write(char *devname, unsigned char addr,
121 unsigned char reg, unsigned short value);
122 static int tsec_miiphy_read(char *devname, unsigned char addr,
123 unsigned char reg, unsigned short *value);
124 #ifdef CONFIG_MCAST_TFTP
125 static int tsec_mcast_addr (struct eth_device *dev, u8 mcast_mac, u8 set);
128 /* Initialize device structure. Returns success if PHY
129 * initialization succeeded (i.e. if it recognizes the PHY)
131 int tsec_initialize(bd_t * bis, int index, char *devname)
133 struct eth_device *dev;
135 struct tsec_private *priv;
137 dev = (struct eth_device *)malloc(sizeof *dev);
142 memset(dev, 0, sizeof *dev);
144 priv = (struct tsec_private *)malloc(sizeof(*priv));
149 privlist[index] = priv;
150 priv->regs = (volatile tsec_t *)(TSEC_BASE_ADDR + index * TSEC_SIZE);
151 priv->phyregs = (volatile tsec_t *)(TSEC_BASE_ADDR +
152 tsec_info[index].phyregidx *
155 priv->phyaddr = tsec_info[index].phyaddr;
156 priv->flags = tsec_info[index].flags;
158 sprintf(dev->name, devname);
161 dev->init = tsec_init;
162 dev->halt = tsec_halt;
163 dev->send = tsec_send;
164 dev->recv = tsec_recv;
165 #ifdef CONFIG_MCAST_TFTP
166 dev->mcast = tsec_mcast_addr;
169 /* Tell u-boot to get the addr from the env */
170 for (i = 0; i < 6; i++)
171 dev->enetaddr[i] = 0;
176 priv->regs->maccfg1 |= MACCFG1_SOFT_RESET;
177 priv->regs->maccfg1 &= ~(MACCFG1_SOFT_RESET);
179 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) \
180 && !defined(BITBANGMII)
181 miiphy_register(dev->name, tsec_miiphy_read, tsec_miiphy_write);
184 /* Try to initialize PHY here, and return */
185 return init_phy(dev);
188 /* Initializes data structures and registers for the controller,
189 * and brings the interface up. Returns the link status, meaning
190 * that it returns success if the link is up, failure otherwise.
191 * This allows u-boot to find the first active controller.
193 int tsec_init(struct eth_device *dev, bd_t * bd)
196 char tmpbuf[MAC_ADDR_LEN];
198 struct tsec_private *priv = (struct tsec_private *)dev->priv;
199 volatile tsec_t *regs = priv->regs;
201 /* Make sure the controller is stopped */
204 /* Init MACCFG2. Defaults to GMII */
205 regs->maccfg2 = MACCFG2_INIT_SETTINGS;
208 regs->ecntrl = ECNTRL_INIT_SETTINGS;
210 /* Copy the station address into the address registers.
211 * Backwards, because little endian MACS are dumb */
212 for (i = 0; i < MAC_ADDR_LEN; i++) {
213 tmpbuf[MAC_ADDR_LEN - 1 - i] = dev->enetaddr[i];
215 regs->macstnaddr1 = *((uint *) (tmpbuf));
217 tempval = *((uint *) (tmpbuf + 4));
219 regs->macstnaddr2 = tempval;
221 /* reset the indices to zero */
225 /* Clear out (for the most part) the other registers */
226 init_registers(regs);
228 /* Ready the device for tx/rx */
231 /* If there's no link, fail */
236 /* Write value to the device's PHY through the registers
237 * specified in priv, modifying the register specified in regnum.
238 * It will wait for the write to be done (or for a timeout to
239 * expire) before exiting
241 void write_phy_reg(struct tsec_private *priv, uint regnum, uint value)
243 volatile tsec_t *regbase = priv->phyregs;
244 uint phyid = priv->phyaddr;
245 int timeout = 1000000;
247 regbase->miimadd = (phyid << 8) | regnum;
248 regbase->miimcon = value;
252 while ((regbase->miimind & MIIMIND_BUSY) && timeout--) ;
255 /* Reads register regnum on the device's PHY through the
256 * registers specified in priv. It lowers and raises the read
257 * command, and waits for the data to become valid (miimind
258 * notvalid bit cleared), and the bus to cease activity (miimind
259 * busy bit cleared), and then returns the value
261 uint read_phy_reg(struct tsec_private *priv, uint regnum)
264 volatile tsec_t *regbase = priv->phyregs;
265 uint phyid = priv->phyaddr;
267 /* Put the address of the phy, and the register
268 * number into MIIMADD */
269 regbase->miimadd = (phyid << 8) | regnum;
271 /* Clear the command register, and wait */
272 regbase->miimcom = 0;
275 /* Initiate a read command, and wait */
276 regbase->miimcom = MIIM_READ_COMMAND;
279 /* Wait for the the indication that the read is done */
280 while ((regbase->miimind & (MIIMIND_NOTVALID | MIIMIND_BUSY))) ;
282 /* Grab the value read from the PHY */
283 value = regbase->miimstat;
288 /* Discover which PHY is attached to the device, and configure it
289 * properly. If the PHY is not recognized, then return 0
290 * (failure). Otherwise, return 1
292 static int init_phy(struct eth_device *dev)
294 struct tsec_private *priv = (struct tsec_private *)dev->priv;
295 struct phy_info *curphy;
296 volatile tsec_t *regs = (volatile tsec_t *)(TSEC_BASE_ADDR);
298 /* Assign a Physical address to the TBI */
299 regs->tbipa = CFG_TBIPA_VALUE;
300 regs = (volatile tsec_t *)(TSEC_BASE_ADDR + TSEC_SIZE);
301 regs->tbipa = CFG_TBIPA_VALUE;
304 /* Reset MII (due to new addresses) */
305 priv->phyregs->miimcfg = MIIMCFG_RESET;
307 priv->phyregs->miimcfg = MIIMCFG_INIT_VALUE;
309 while (priv->phyregs->miimind & MIIMIND_BUSY) ;
314 /* Get the cmd structure corresponding to the attached
316 curphy = get_phy_info(dev);
318 if (curphy == NULL) {
319 priv->phyinfo = NULL;
320 printf("%s: No PHY found\n", dev->name);
325 priv->phyinfo = curphy;
327 phy_run_commands(priv, priv->phyinfo->config);
333 * Returns which value to write to the control register.
334 * For 10/100, the value is slightly different
336 uint mii_cr_init(uint mii_reg, struct tsec_private * priv)
338 if (priv->flags & TSEC_GIGABIT)
339 return MIIM_CONTROL_INIT;
344 /* Parse the status register for link, and then do
347 uint mii_parse_sr(uint mii_reg, struct tsec_private * priv)
350 * Wait if the link is up, and autonegotiation is in progress
351 * (ie - we're capable and it's not done)
353 mii_reg = read_phy_reg(priv, MIIM_STATUS);
354 if ((mii_reg & MIIM_STATUS_LINK) && (mii_reg & PHY_BMSR_AUTN_ABLE)
355 && !(mii_reg & PHY_BMSR_AUTN_COMP)) {
358 puts("Waiting for PHY auto negotiation to complete");
359 while (!(mii_reg & PHY_BMSR_AUTN_COMP)) {
363 if (i > PHY_AUTONEGOTIATE_TIMEOUT) {
364 puts(" TIMEOUT !\n");
369 if ((i++ % 1000) == 0) {
372 udelay(1000); /* 1 ms */
373 mii_reg = read_phy_reg(priv, MIIM_STATUS);
377 udelay(500000); /* another 500 ms (results in faster booting) */
379 if (mii_reg & MIIM_STATUS_LINK)
388 /* Generic function which updates the speed and duplex. If
389 * autonegotiation is enabled, it uses the AND of the link
390 * partner's advertised capabilities and our advertised
391 * capabilities. If autonegotiation is disabled, we use the
392 * appropriate bits in the control register.
394 * Stolen from Linux's mii.c and phy_device.c
396 uint mii_parse_link(uint mii_reg, struct tsec_private *priv)
398 /* We're using autonegotiation */
399 if (mii_reg & PHY_BMSR_AUTN_ABLE) {
403 /* Check for gigabit capability */
404 if (mii_reg & PHY_BMSR_EXT) {
405 /* We want a list of states supported by
406 * both PHYs in the link
408 gblpa = read_phy_reg(priv, PHY_1000BTSR);
409 gblpa &= read_phy_reg(priv, PHY_1000BTCR) << 2;
412 /* Set the baseline so we only have to set them
413 * if they're different
418 /* Check the gigabit fields */
419 if (gblpa & (PHY_1000BTSR_1000FD | PHY_1000BTSR_1000HD)) {
422 if (gblpa & PHY_1000BTSR_1000FD)
429 lpa = read_phy_reg(priv, PHY_ANAR);
430 lpa &= read_phy_reg(priv, PHY_ANLPAR);
432 if (lpa & (PHY_ANLPAR_TXFD | PHY_ANLPAR_TX)) {
435 if (lpa & PHY_ANLPAR_TXFD)
438 } else if (lpa & PHY_ANLPAR_10FD)
441 uint bmcr = read_phy_reg(priv, PHY_BMCR);
446 if (bmcr & PHY_BMCR_DPLX)
449 if (bmcr & PHY_BMCR_1000_MBPS)
451 else if (bmcr & PHY_BMCR_100_MBPS)
459 * Parse the BCM54xx status register for speed and duplex information.
460 * The linux sungem_phy has this information, but in a table format.
462 uint mii_parse_BCM54xx_sr(uint mii_reg, struct tsec_private *priv)
465 switch((mii_reg & MIIM_BCM54xx_AUXSTATUS_LINKMODE_MASK) >> MIIM_BCM54xx_AUXSTATUS_LINKMODE_SHIFT){
468 printf("Enet starting in 10BT/HD\n");
474 printf("Enet starting in 10BT/FD\n");
480 printf("Enet starting in 100BT/HD\n");
486 printf("Enet starting in 100BT/FD\n");
492 printf("Enet starting in 1000BT/HD\n");
498 printf("Enet starting in 1000BT/FD\n");
504 printf("Auto-neg error, defaulting to 10BT/HD\n");
513 /* Parse the 88E1011's status register for speed and duplex
516 uint mii_parse_88E1011_psr(uint mii_reg, struct tsec_private * priv)
520 mii_reg = read_phy_reg(priv, MIIM_88E1011_PHY_STATUS);
522 if ((mii_reg & MIIM_88E1011_PHYSTAT_LINK) &&
523 !(mii_reg & MIIM_88E1011_PHYSTAT_SPDDONE)) {
526 puts("Waiting for PHY realtime link");
527 while (!(mii_reg & MIIM_88E1011_PHYSTAT_SPDDONE)) {
528 /* Timeout reached ? */
529 if (i > PHY_AUTONEGOTIATE_TIMEOUT) {
530 puts(" TIMEOUT !\n");
535 if ((i++ % 1000) == 0) {
538 udelay(1000); /* 1 ms */
539 mii_reg = read_phy_reg(priv, MIIM_88E1011_PHY_STATUS);
542 udelay(500000); /* another 500 ms (results in faster booting) */
544 if (mii_reg & MIIM_88E1011_PHYSTAT_LINK)
550 if (mii_reg & MIIM_88E1011_PHYSTAT_DUPLEX)
555 speed = (mii_reg & MIIM_88E1011_PHYSTAT_SPEED);
558 case MIIM_88E1011_PHYSTAT_GBIT:
561 case MIIM_88E1011_PHYSTAT_100:
571 /* Parse the cis8201's status register for speed and duplex
574 uint mii_parse_cis8201(uint mii_reg, struct tsec_private * priv)
578 if (mii_reg & MIIM_CIS8201_AUXCONSTAT_DUPLEX)
583 speed = mii_reg & MIIM_CIS8201_AUXCONSTAT_SPEED;
585 case MIIM_CIS8201_AUXCONSTAT_GBIT:
588 case MIIM_CIS8201_AUXCONSTAT_100:
599 /* Parse the vsc8244's status register for speed and duplex
602 uint mii_parse_vsc8244(uint mii_reg, struct tsec_private * priv)
606 if (mii_reg & MIIM_VSC8244_AUXCONSTAT_DUPLEX)
611 speed = mii_reg & MIIM_VSC8244_AUXCONSTAT_SPEED;
613 case MIIM_VSC8244_AUXCONSTAT_GBIT:
616 case MIIM_VSC8244_AUXCONSTAT_100:
627 /* Parse the DM9161's status register for speed and duplex
630 uint mii_parse_dm9161_scsr(uint mii_reg, struct tsec_private * priv)
632 if (mii_reg & (MIIM_DM9161_SCSR_100F | MIIM_DM9161_SCSR_100H))
637 if (mii_reg & (MIIM_DM9161_SCSR_100F | MIIM_DM9161_SCSR_10F))
646 * Hack to write all 4 PHYs with the LED values
648 uint mii_cis8204_fixled(uint mii_reg, struct tsec_private * priv)
651 volatile tsec_t *regbase = priv->phyregs;
652 int timeout = 1000000;
654 for (phyid = 0; phyid < 4; phyid++) {
655 regbase->miimadd = (phyid << 8) | mii_reg;
656 regbase->miimcon = MIIM_CIS8204_SLEDCON_INIT;
660 while ((regbase->miimind & MIIMIND_BUSY) && timeout--) ;
663 return MIIM_CIS8204_SLEDCON_INIT;
666 uint mii_cis8204_setmode(uint mii_reg, struct tsec_private * priv)
668 if (priv->flags & TSEC_REDUCED)
669 return MIIM_CIS8204_EPHYCON_INIT | MIIM_CIS8204_EPHYCON_RGMII;
671 return MIIM_CIS8204_EPHYCON_INIT;
674 /* Initialized required registers to appropriate values, zeroing
675 * those we don't care about (unless zero is bad, in which case,
676 * choose a more appropriate value)
678 static void init_registers(volatile tsec_t * regs)
681 regs->ievent = IEVENT_INIT_CLEAR;
683 regs->imask = IMASK_INIT_CLEAR;
685 regs->hash.iaddr0 = 0;
686 regs->hash.iaddr1 = 0;
687 regs->hash.iaddr2 = 0;
688 regs->hash.iaddr3 = 0;
689 regs->hash.iaddr4 = 0;
690 regs->hash.iaddr5 = 0;
691 regs->hash.iaddr6 = 0;
692 regs->hash.iaddr7 = 0;
694 regs->hash.gaddr0 = 0;
695 regs->hash.gaddr1 = 0;
696 regs->hash.gaddr2 = 0;
697 regs->hash.gaddr3 = 0;
698 regs->hash.gaddr4 = 0;
699 regs->hash.gaddr5 = 0;
700 regs->hash.gaddr6 = 0;
701 regs->hash.gaddr7 = 0;
703 regs->rctrl = 0x00000000;
705 /* Init RMON mib registers */
706 memset((void *)&(regs->rmon), 0, sizeof(rmon_mib_t));
708 regs->rmon.cam1 = 0xffffffff;
709 regs->rmon.cam2 = 0xffffffff;
711 regs->mrblr = MRBLR_INIT_SETTINGS;
713 regs->minflr = MINFLR_INIT_SETTINGS;
715 regs->attr = ATTR_INIT_SETTINGS;
716 regs->attreli = ATTRELI_INIT_SETTINGS;
720 /* Configure maccfg2 based on negotiated speed and duplex
721 * reported by PHY handling code
723 static void adjust_link(struct eth_device *dev)
725 struct tsec_private *priv = (struct tsec_private *)dev->priv;
726 volatile tsec_t *regs = priv->regs;
729 if (priv->duplexity != 0)
730 regs->maccfg2 |= MACCFG2_FULL_DUPLEX;
732 regs->maccfg2 &= ~(MACCFG2_FULL_DUPLEX);
734 switch (priv->speed) {
736 regs->maccfg2 = ((regs->maccfg2 & ~(MACCFG2_IF))
741 regs->maccfg2 = ((regs->maccfg2 & ~(MACCFG2_IF))
744 /* Set R100 bit in all modes although
745 * it is only used in RGMII mode
747 if (priv->speed == 100)
748 regs->ecntrl |= ECNTRL_R100;
750 regs->ecntrl &= ~(ECNTRL_R100);
753 printf("%s: Speed was bad\n", dev->name);
757 printf("Speed: %d, %s duplex\n", priv->speed,
758 (priv->duplexity) ? "full" : "half");
761 printf("%s: No link.\n", dev->name);
765 /* Set up the buffers and their descriptors, and bring up the
768 static void startup_tsec(struct eth_device *dev)
771 struct tsec_private *priv = (struct tsec_private *)dev->priv;
772 volatile tsec_t *regs = priv->regs;
774 /* Point to the buffer descriptors */
775 regs->tbase = (unsigned int)(&rtx.txbd[txIdx]);
776 regs->rbase = (unsigned int)(&rtx.rxbd[rxIdx]);
778 /* Initialize the Rx Buffer descriptors */
779 for (i = 0; i < PKTBUFSRX; i++) {
780 rtx.rxbd[i].status = RXBD_EMPTY;
781 rtx.rxbd[i].length = 0;
782 rtx.rxbd[i].bufPtr = (uint) NetRxPackets[i];
784 rtx.rxbd[PKTBUFSRX - 1].status |= RXBD_WRAP;
786 /* Initialize the TX Buffer Descriptors */
787 for (i = 0; i < TX_BUF_CNT; i++) {
788 rtx.txbd[i].status = 0;
789 rtx.txbd[i].length = 0;
790 rtx.txbd[i].bufPtr = 0;
792 rtx.txbd[TX_BUF_CNT - 1].status |= TXBD_WRAP;
794 /* Start up the PHY */
796 phy_run_commands(priv, priv->phyinfo->startup);
800 /* Enable Transmit and Receive */
801 regs->maccfg1 |= (MACCFG1_RX_EN | MACCFG1_TX_EN);
803 /* Tell the DMA it is clear to go */
804 regs->dmactrl |= DMACTRL_INIT_SETTINGS;
805 regs->tstat = TSTAT_CLEAR_THALT;
806 regs->dmactrl &= ~(DMACTRL_GRS | DMACTRL_GTS);
809 /* This returns the status bits of the device. The return value
810 * is never checked, and this is what the 8260 driver did, so we
811 * do the same. Presumably, this would be zero if there were no
814 static int tsec_send(struct eth_device *dev, volatile void *packet, int length)
818 struct tsec_private *priv = (struct tsec_private *)dev->priv;
819 volatile tsec_t *regs = priv->regs;
821 /* Find an empty buffer descriptor */
822 for (i = 0; rtx.txbd[txIdx].status & TXBD_READY; i++) {
823 if (i >= TOUT_LOOP) {
824 debug("%s: tsec: tx buffers full\n", dev->name);
829 rtx.txbd[txIdx].bufPtr = (uint) packet;
830 rtx.txbd[txIdx].length = length;
831 rtx.txbd[txIdx].status |=
832 (TXBD_READY | TXBD_LAST | TXBD_CRC | TXBD_INTERRUPT);
834 /* Tell the DMA to go */
835 regs->tstat = TSTAT_CLEAR_THALT;
837 /* Wait for buffer to be transmitted */
838 for (i = 0; rtx.txbd[txIdx].status & TXBD_READY; i++) {
839 if (i >= TOUT_LOOP) {
840 debug("%s: tsec: tx error\n", dev->name);
845 txIdx = (txIdx + 1) % TX_BUF_CNT;
846 result = rtx.txbd[txIdx].status & TXBD_STATS;
851 static int tsec_recv(struct eth_device *dev)
854 struct tsec_private *priv = (struct tsec_private *)dev->priv;
855 volatile tsec_t *regs = priv->regs;
857 while (!(rtx.rxbd[rxIdx].status & RXBD_EMPTY)) {
859 length = rtx.rxbd[rxIdx].length;
861 /* Send the packet up if there were no errors */
862 if (!(rtx.rxbd[rxIdx].status & RXBD_STATS)) {
863 NetReceive(NetRxPackets[rxIdx], length - 4);
865 printf("Got error %x\n",
866 (rtx.rxbd[rxIdx].status & RXBD_STATS));
869 rtx.rxbd[rxIdx].length = 0;
871 /* Set the wrap bit if this is the last element in the list */
872 rtx.rxbd[rxIdx].status =
873 RXBD_EMPTY | (((rxIdx + 1) == PKTBUFSRX) ? RXBD_WRAP : 0);
875 rxIdx = (rxIdx + 1) % PKTBUFSRX;
878 if (regs->ievent & IEVENT_BSY) {
879 regs->ievent = IEVENT_BSY;
880 regs->rstat = RSTAT_CLEAR_RHALT;
887 /* Stop the interface */
888 static void tsec_halt(struct eth_device *dev)
890 struct tsec_private *priv = (struct tsec_private *)dev->priv;
891 volatile tsec_t *regs = priv->regs;
893 regs->dmactrl &= ~(DMACTRL_GRS | DMACTRL_GTS);
894 regs->dmactrl |= (DMACTRL_GRS | DMACTRL_GTS);
896 while (!(regs->ievent & (IEVENT_GRSC | IEVENT_GTSC))) ;
898 regs->maccfg1 &= ~(MACCFG1_TX_EN | MACCFG1_RX_EN);
900 /* Shut down the PHY, as needed */
902 phy_run_commands(priv, priv->phyinfo->shutdown);
905 struct phy_info phy_info_M88E1149S = {
909 (struct phy_cmd[]){ /* config */
910 /* Reset and configure the PHY */
911 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
913 {0x1e, 0x200c, NULL},
917 {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
918 {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
919 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
920 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
923 (struct phy_cmd[]){ /* startup */
924 /* Status is read once to clear old link state */
925 {MIIM_STATUS, miim_read, NULL},
927 {MIIM_STATUS, miim_read, &mii_parse_sr},
928 /* Read the status */
929 {MIIM_88E1011_PHY_STATUS, miim_read,
930 &mii_parse_88E1011_psr},
933 (struct phy_cmd[]){ /* shutdown */
938 /* The 5411 id is 0x206070, the 5421 is 0x2060e0 */
939 struct phy_info phy_info_BCM5461S = {
940 0x02060c1, /* 5461 ID */
942 0, /* not clear to me what minor revisions we can shift away */
943 (struct phy_cmd[]) { /* config */
944 /* Reset and configure the PHY */
945 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
946 {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
947 {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
948 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
949 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
952 (struct phy_cmd[]) { /* startup */
953 /* Status is read once to clear old link state */
954 {MIIM_STATUS, miim_read, NULL},
956 {MIIM_STATUS, miim_read, &mii_parse_sr},
957 /* Read the status */
958 {MIIM_BCM54xx_AUXSTATUS, miim_read, &mii_parse_BCM54xx_sr},
961 (struct phy_cmd[]) { /* shutdown */
966 struct phy_info phy_info_BCM5464S = {
967 0x02060b1, /* 5464 ID */
969 0, /* not clear to me what minor revisions we can shift away */
970 (struct phy_cmd[]) { /* config */
971 /* Reset and configure the PHY */
972 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
973 {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
974 {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
975 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
976 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
979 (struct phy_cmd[]) { /* startup */
980 /* Status is read once to clear old link state */
981 {MIIM_STATUS, miim_read, NULL},
983 {MIIM_STATUS, miim_read, &mii_parse_sr},
984 /* Read the status */
985 {MIIM_BCM54xx_AUXSTATUS, miim_read, &mii_parse_BCM54xx_sr},
988 (struct phy_cmd[]) { /* shutdown */
993 struct phy_info phy_info_M88E1011S = {
997 (struct phy_cmd[]){ /* config */
998 /* Reset and configure the PHY */
999 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
1001 {0x1e, 0x200c, NULL},
1004 {0x1e, 0x100, NULL},
1005 {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
1006 {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
1007 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
1008 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
1011 (struct phy_cmd[]){ /* startup */
1012 /* Status is read once to clear old link state */
1013 {MIIM_STATUS, miim_read, NULL},
1014 /* Auto-negotiate */
1015 {MIIM_STATUS, miim_read, &mii_parse_sr},
1016 /* Read the status */
1017 {MIIM_88E1011_PHY_STATUS, miim_read,
1018 &mii_parse_88E1011_psr},
1021 (struct phy_cmd[]){ /* shutdown */
1026 struct phy_info phy_info_M88E1111S = {
1030 (struct phy_cmd[]){ /* config */
1031 /* Reset and configure the PHY */
1032 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
1033 {0x14, 0x0cd2, NULL}, /* Delay RGMII TX and RX */
1034 {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
1035 {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
1036 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
1037 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
1040 (struct phy_cmd[]){ /* startup */
1041 /* Status is read once to clear old link state */
1042 {MIIM_STATUS, miim_read, NULL},
1043 /* Auto-negotiate */
1044 {MIIM_STATUS, miim_read, &mii_parse_sr},
1045 /* Read the status */
1046 {MIIM_88E1011_PHY_STATUS, miim_read,
1047 &mii_parse_88E1011_psr},
1050 (struct phy_cmd[]){ /* shutdown */
1055 static unsigned int m88e1145_setmode(uint mii_reg, struct tsec_private *priv)
1057 uint mii_data = read_phy_reg(priv, mii_reg);
1059 /* Setting MIIM_88E1145_PHY_EXT_CR */
1060 if (priv->flags & TSEC_REDUCED)
1062 MIIM_M88E1145_RGMII_RX_DELAY | MIIM_M88E1145_RGMII_TX_DELAY;
1067 static struct phy_info phy_info_M88E1145 = {
1071 (struct phy_cmd[]){ /* config */
1073 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
1081 /* Configure the PHY */
1082 {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
1083 {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
1084 {MIIM_88E1011_PHY_SCR, MIIM_88E1011_PHY_MDI_X_AUTO,
1086 {MIIM_88E1145_PHY_EXT_CR, 0, &m88e1145_setmode},
1087 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
1088 {MIIM_CONTROL, MIIM_CONTROL_INIT, NULL},
1091 (struct phy_cmd[]){ /* startup */
1092 /* Status is read once to clear old link state */
1093 {MIIM_STATUS, miim_read, NULL},
1094 /* Auto-negotiate */
1095 {MIIM_STATUS, miim_read, &mii_parse_sr},
1096 {MIIM_88E1111_PHY_LED_CONTROL,
1097 MIIM_88E1111_PHY_LED_DIRECT, NULL},
1098 /* Read the Status */
1099 {MIIM_88E1011_PHY_STATUS, miim_read,
1100 &mii_parse_88E1011_psr},
1103 (struct phy_cmd[]){ /* shutdown */
1108 struct phy_info phy_info_cis8204 = {
1112 (struct phy_cmd[]){ /* config */
1113 /* Override PHY config settings */
1114 {MIIM_CIS8201_AUX_CONSTAT,
1115 MIIM_CIS8201_AUXCONSTAT_INIT, NULL},
1116 /* Configure some basic stuff */
1117 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
1118 {MIIM_CIS8204_SLED_CON, MIIM_CIS8204_SLEDCON_INIT,
1119 &mii_cis8204_fixled},
1120 {MIIM_CIS8204_EPHY_CON, MIIM_CIS8204_EPHYCON_INIT,
1121 &mii_cis8204_setmode},
1124 (struct phy_cmd[]){ /* startup */
1125 /* Read the Status (2x to make sure link is right) */
1126 {MIIM_STATUS, miim_read, NULL},
1127 /* Auto-negotiate */
1128 {MIIM_STATUS, miim_read, &mii_parse_sr},
1129 /* Read the status */
1130 {MIIM_CIS8201_AUX_CONSTAT, miim_read,
1131 &mii_parse_cis8201},
1134 (struct phy_cmd[]){ /* shutdown */
1140 struct phy_info phy_info_cis8201 = {
1144 (struct phy_cmd[]){ /* config */
1145 /* Override PHY config settings */
1146 {MIIM_CIS8201_AUX_CONSTAT,
1147 MIIM_CIS8201_AUXCONSTAT_INIT, NULL},
1148 /* Set up the interface mode */
1149 {MIIM_CIS8201_EXT_CON1, MIIM_CIS8201_EXTCON1_INIT,
1151 /* Configure some basic stuff */
1152 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
1155 (struct phy_cmd[]){ /* startup */
1156 /* Read the Status (2x to make sure link is right) */
1157 {MIIM_STATUS, miim_read, NULL},
1158 /* Auto-negotiate */
1159 {MIIM_STATUS, miim_read, &mii_parse_sr},
1160 /* Read the status */
1161 {MIIM_CIS8201_AUX_CONSTAT, miim_read,
1162 &mii_parse_cis8201},
1165 (struct phy_cmd[]){ /* shutdown */
1169 struct phy_info phy_info_VSC8244 = {
1173 (struct phy_cmd[]){ /* config */
1174 /* Override PHY config settings */
1175 /* Configure some basic stuff */
1176 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
1179 (struct phy_cmd[]){ /* startup */
1180 /* Read the Status (2x to make sure link is right) */
1181 {MIIM_STATUS, miim_read, NULL},
1182 /* Auto-negotiate */
1183 {MIIM_STATUS, miim_read, &mii_parse_sr},
1184 /* Read the status */
1185 {MIIM_VSC8244_AUX_CONSTAT, miim_read,
1186 &mii_parse_vsc8244},
1189 (struct phy_cmd[]){ /* shutdown */
1194 struct phy_info phy_info_dm9161 = {
1198 (struct phy_cmd[]){ /* config */
1199 {MIIM_CONTROL, MIIM_DM9161_CR_STOP, NULL},
1200 /* Do not bypass the scrambler/descrambler */
1201 {MIIM_DM9161_SCR, MIIM_DM9161_SCR_INIT, NULL},
1202 /* Clear 10BTCSR to default */
1203 {MIIM_DM9161_10BTCSR, MIIM_DM9161_10BTCSR_INIT,
1205 /* Configure some basic stuff */
1206 {MIIM_CONTROL, MIIM_CR_INIT, NULL},
1207 /* Restart Auto Negotiation */
1208 {MIIM_CONTROL, MIIM_DM9161_CR_RSTAN, NULL},
1211 (struct phy_cmd[]){ /* startup */
1212 /* Status is read once to clear old link state */
1213 {MIIM_STATUS, miim_read, NULL},
1214 /* Auto-negotiate */
1215 {MIIM_STATUS, miim_read, &mii_parse_sr},
1216 /* Read the status */
1217 {MIIM_DM9161_SCSR, miim_read,
1218 &mii_parse_dm9161_scsr},
1221 (struct phy_cmd[]){ /* shutdown */
1225 /* a generic flavor. */
1226 struct phy_info phy_info_generic = {
1228 "Unknown/Generic PHY",
1230 (struct phy_cmd[]) { /* config */
1231 {PHY_BMCR, PHY_BMCR_RESET, NULL},
1232 {PHY_BMCR, PHY_BMCR_AUTON|PHY_BMCR_RST_NEG, NULL},
1235 (struct phy_cmd[]) { /* startup */
1236 {PHY_BMSR, miim_read, NULL},
1237 {PHY_BMSR, miim_read, &mii_parse_sr},
1238 {PHY_BMSR, miim_read, &mii_parse_link},
1241 (struct phy_cmd[]) { /* shutdown */
1247 uint mii_parse_lxt971_sr2(uint mii_reg, struct tsec_private *priv)
1251 speed = mii_reg & MIIM_LXT971_SR2_SPEED_MASK;
1254 case MIIM_LXT971_SR2_10HDX:
1256 priv->duplexity = 0;
1258 case MIIM_LXT971_SR2_10FDX:
1260 priv->duplexity = 1;
1262 case MIIM_LXT971_SR2_100HDX:
1264 priv->duplexity = 0;
1268 priv->duplexity = 1;
1272 priv->duplexity = 0;
1278 static struct phy_info phy_info_lxt971 = {
1282 (struct phy_cmd[]){ /* config */
1283 {MIIM_CR, MIIM_CR_INIT, mii_cr_init}, /* autonegotiate */
1286 (struct phy_cmd[]){ /* startup - enable interrupts */
1287 /* { 0x12, 0x00f2, NULL }, */
1288 {MIIM_STATUS, miim_read, NULL},
1289 {MIIM_STATUS, miim_read, &mii_parse_sr},
1290 {MIIM_LXT971_SR2, miim_read, &mii_parse_lxt971_sr2},
1293 (struct phy_cmd[]){ /* shutdown - disable interrupts */
1298 /* Parse the DP83865's link and auto-neg status register for speed and duplex
1301 uint mii_parse_dp83865_lanr(uint mii_reg, struct tsec_private *priv)
1303 switch (mii_reg & MIIM_DP83865_SPD_MASK) {
1305 case MIIM_DP83865_SPD_1000:
1309 case MIIM_DP83865_SPD_100:
1319 if (mii_reg & MIIM_DP83865_DPX_FULL)
1320 priv->duplexity = 1;
1322 priv->duplexity = 0;
1327 struct phy_info phy_info_dp83865 = {
1331 (struct phy_cmd[]){ /* config */
1332 {MIIM_CONTROL, MIIM_DP83865_CR_INIT, NULL},
1335 (struct phy_cmd[]){ /* startup */
1336 /* Status is read once to clear old link state */
1337 {MIIM_STATUS, miim_read, NULL},
1338 /* Auto-negotiate */
1339 {MIIM_STATUS, miim_read, &mii_parse_sr},
1340 /* Read the link and auto-neg status */
1341 {MIIM_DP83865_LANR, miim_read,
1342 &mii_parse_dp83865_lanr},
1345 (struct phy_cmd[]){ /* shutdown */
1350 struct phy_info *phy_info[] = {
1355 &phy_info_M88E1011S,
1356 &phy_info_M88E1111S,
1358 &phy_info_M88E1149S,
1367 /* Grab the identifier of the device's PHY, and search through
1368 * all of the known PHYs to see if one matches. If so, return
1369 * it, if not, return NULL
1371 struct phy_info *get_phy_info(struct eth_device *dev)
1373 struct tsec_private *priv = (struct tsec_private *)dev->priv;
1374 uint phy_reg, phy_ID;
1376 struct phy_info *theInfo = NULL;
1378 /* Grab the bits from PHYIR1, and put them in the upper half */
1379 phy_reg = read_phy_reg(priv, MIIM_PHYIR1);
1380 phy_ID = (phy_reg & 0xffff) << 16;
1382 /* Grab the bits from PHYIR2, and put them in the lower half */
1383 phy_reg = read_phy_reg(priv, MIIM_PHYIR2);
1384 phy_ID |= (phy_reg & 0xffff);
1386 /* loop through all the known PHY types, and find one that */
1387 /* matches the ID we read from the PHY. */
1388 for (i = 0; phy_info[i]; i++) {
1389 if (phy_info[i]->id == (phy_ID >> phy_info[i]->shift)) {
1390 theInfo = phy_info[i];
1395 if (theInfo == NULL) {
1396 printf("%s: PHY id %x is not supported!\n", dev->name, phy_ID);
1399 debug("%s: PHY is %s (%x)\n", dev->name, theInfo->name, phy_ID);
1405 /* Execute the given series of commands on the given device's
1406 * PHY, running functions as necessary
1408 void phy_run_commands(struct tsec_private *priv, struct phy_cmd *cmd)
1412 volatile tsec_t *phyregs = priv->phyregs;
1414 phyregs->miimcfg = MIIMCFG_RESET;
1416 phyregs->miimcfg = MIIMCFG_INIT_VALUE;
1418 while (phyregs->miimind & MIIMIND_BUSY) ;
1420 for (i = 0; cmd->mii_reg != miim_end; i++) {
1421 if (cmd->mii_data == miim_read) {
1422 result = read_phy_reg(priv, cmd->mii_reg);
1424 if (cmd->funct != NULL)
1425 (*(cmd->funct)) (result, priv);
1428 if (cmd->funct != NULL)
1429 result = (*(cmd->funct)) (cmd->mii_reg, priv);
1431 result = cmd->mii_data;
1433 write_phy_reg(priv, cmd->mii_reg, result);
1440 /* Relocate the function pointers in the phy cmd lists */
1441 static void relocate_cmds(void)
1443 struct phy_cmd **cmdlistptr;
1444 struct phy_cmd *cmd;
1447 for (i = 0; phy_info[i]; i++) {
1448 /* First thing's first: relocate the pointers to the
1449 * PHY command structures (the structs were done) */
1450 phy_info[i] = (struct phy_info *)((uint) phy_info[i]
1452 phy_info[i]->name += gd->reloc_off;
1453 phy_info[i]->config =
1454 (struct phy_cmd *)((uint) phy_info[i]->config
1456 phy_info[i]->startup =
1457 (struct phy_cmd *)((uint) phy_info[i]->startup
1459 phy_info[i]->shutdown =
1460 (struct phy_cmd *)((uint) phy_info[i]->shutdown
1463 cmdlistptr = &phy_info[i]->config;
1465 for (; cmdlistptr <= &phy_info[i]->shutdown; cmdlistptr++) {
1467 for (cmd = *cmdlistptr;
1468 cmd->mii_reg != miim_end;
1470 /* Only relocate non-NULL pointers */
1472 cmd->funct += gd->reloc_off;
1483 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) \
1484 && !defined(BITBANGMII)
1486 struct tsec_private *get_priv_for_phy(unsigned char phyaddr)
1490 for (i = 0; i < MAXCONTROLLERS; i++) {
1491 if (privlist[i]->phyaddr == phyaddr)
1499 * Read a MII PHY register.
1504 static int tsec_miiphy_read(char *devname, unsigned char addr,
1505 unsigned char reg, unsigned short *value)
1508 struct tsec_private *priv = get_priv_for_phy(addr);
1511 printf("Can't read PHY at address %d\n", addr);
1515 ret = (unsigned short)read_phy_reg(priv, reg);
1522 * Write a MII PHY register.
1527 static int tsec_miiphy_write(char *devname, unsigned char addr,
1528 unsigned char reg, unsigned short value)
1530 struct tsec_private *priv = get_priv_for_phy(addr);
1533 printf("Can't write PHY at address %d\n", addr);
1537 write_phy_reg(priv, reg, value);
1544 #ifdef CONFIG_MCAST_TFTP
1546 /* CREDITS: linux gianfar driver, slightly adjusted... thanx. */
1548 /* Set the appropriate hash bit for the given addr */
1550 /* The algorithm works like so:
1551 * 1) Take the Destination Address (ie the multicast address), and
1552 * do a CRC on it (little endian), and reverse the bits of the
1554 * 2) Use the 8 most significant bits as a hash into a 256-entry
1555 * table. The table is controlled through 8 32-bit registers:
1556 * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is
1557 * gaddr7. This means that the 3 most significant bits in the
1558 * hash index which gaddr register to use, and the 5 other bits
1559 * indicate which bit (assuming an IBM numbering scheme, which
1560 * for PowerPC (tm) is usually the case) in the tregister holds
1563 tsec_mcast_addr (struct eth_device *dev, u8 mcast_mac, u8 set)
1565 struct tsec_private *priv = privlist[1];
1566 volatile tsec_t *regs = priv->regs;
1567 volatile u32 *reg_array, value;
1568 u8 result, whichbit, whichreg;
1570 result = (u8)((ether_crc(MAC_ADDR_LEN,mcast_mac) >> 24) & 0xff);
1571 whichbit = result & 0x1f; /* the 5 LSB = which bit to set */
1572 whichreg = result >> 5; /* the 3 MSB = which reg to set it in */
1573 value = (1 << (31-whichbit));
1575 reg_array = &(regs->hash.gaddr0);
1578 reg_array[whichreg] |= value;
1580 reg_array[whichreg] &= ~value;
1584 #endif /* Multicast TFTP ? */
1586 #endif /* CONFIG_TSEC_ENET */