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-2011 Freescale Semiconductor, Inc.
9 * (C) Copyright 2003, Motorola, Inc.
20 #include <asm/errno.h>
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 #define MAXCONTROLLERS (8)
38 static struct tsec_private *privlist[MAXCONTROLLERS];
39 static int num_tsecs = 0;
42 static RTXBD rtx __attribute__ ((aligned(8)));
44 #error "rtx must be 64-bit aligned"
47 /* Default initializations for TSEC controllers. */
49 static struct tsec_info_struct tsec_info[] = {
51 STD_TSEC_INFO(1), /* TSEC1 */
54 STD_TSEC_INFO(2), /* TSEC2 */
56 #ifdef CONFIG_MPC85XX_FEC
58 .regs = (tsec_t *)(TSEC_BASE_ADDR + 0x2000),
59 .miiregs = (tsec_mdio_t *)(MDIO_BASE_ADDR),
60 .devname = CONFIG_MPC85XX_FEC_NAME,
61 .phyaddr = FEC_PHY_ADDR,
66 STD_TSEC_INFO(3), /* TSEC3 */
69 STD_TSEC_INFO(4), /* TSEC4 */
73 /* Writes the given phy's reg with value, using the specified MDIO regs */
74 static void tsec_local_mdio_write(tsec_mdio_t *phyregs, uint addr,
77 int timeout = 1000000;
79 out_be32(&phyregs->miimadd, (addr << 8) | reg);
80 out_be32(&phyregs->miimcon, value);
83 while ((in_be32(&phyregs->miimind) & MIIMIND_BUSY) && timeout--)
87 /* Provide the default behavior of writing the PHY of this ethernet device */
88 #define write_phy_reg(priv, regnum, value) \
89 tsec_local_mdio_write(priv->phyregs,priv->phyaddr,regnum,value)
91 /* Reads register regnum on the device's PHY through the
92 * specified registers. It lowers and raises the read
93 * command, and waits for the data to become valid (miimind
94 * notvalid bit cleared), and the bus to cease activity (miimind
95 * busy bit cleared), and then returns the value
97 static uint tsec_local_mdio_read(tsec_mdio_t *phyregs, uint phyid, uint regnum)
101 /* Put the address of the phy, and the register
102 * number into MIIMADD */
103 out_be32(&phyregs->miimadd, (phyid << 8) | regnum);
105 /* Clear the command register, and wait */
106 out_be32(&phyregs->miimcom, 0);
108 /* Initiate a read command, and wait */
109 out_be32(&phyregs->miimcom, MIIM_READ_COMMAND);
111 /* Wait for the the indication that the read is done */
112 while ((in_be32(&phyregs->miimind) & (MIIMIND_NOTVALID | MIIMIND_BUSY)))
115 /* Grab the value read from the PHY */
116 value = in_be32(&phyregs->miimstat);
121 /* #define to provide old read_phy_reg functionality without duplicating code */
122 #define read_phy_reg(priv,regnum) \
123 tsec_local_mdio_read(priv->phyregs,priv->phyaddr,regnum)
125 #define TBIANA_SETTINGS ( \
126 TBIANA_ASYMMETRIC_PAUSE \
127 | TBIANA_SYMMETRIC_PAUSE \
128 | TBIANA_FULL_DUPLEX \
131 /* By default force the TBI PHY into 1000Mbps full duplex when in SGMII mode */
132 #ifndef CONFIG_TSEC_TBICR_SETTINGS
133 #define CONFIG_TSEC_TBICR_SETTINGS ( \
135 | TBICR_ANEG_ENABLE \
136 | TBICR_FULL_DUPLEX \
139 #endif /* CONFIG_TSEC_TBICR_SETTINGS */
141 /* Configure the TBI for SGMII operation */
142 static void tsec_configure_serdes(struct tsec_private *priv)
144 /* Access TBI PHY registers at given TSEC register offset as opposed
145 * to the register offset used for external PHY accesses */
146 tsec_local_mdio_write(priv->phyregs_sgmii, priv->regs->tbipa, TBI_ANA,
148 tsec_local_mdio_write(priv->phyregs_sgmii, priv->regs->tbipa, TBI_TBICON,
150 tsec_local_mdio_write(priv->phyregs_sgmii, priv->regs->tbipa, TBI_CR,
151 CONFIG_TSEC_TBICR_SETTINGS);
155 * Returns which value to write to the control register.
156 * For 10/100, the value is slightly different
158 static uint mii_cr_init(uint mii_reg, struct tsec_private * priv)
160 if (priv->flags & TSEC_GIGABIT)
161 return MIIM_CONTROL_INIT;
167 * Wait for auto-negotiation to complete, then determine link
169 static uint mii_parse_sr(uint mii_reg, struct tsec_private * priv)
172 * Wait if the link is up, and autonegotiation is in progress
173 * (ie - we're capable and it's not done)
175 mii_reg = read_phy_reg(priv, MIIM_STATUS);
176 if ((mii_reg & BMSR_ANEGCAPABLE) && !(mii_reg & BMSR_ANEGCOMPLETE)) {
179 puts("Waiting for PHY auto negotiation to complete");
180 while (!(mii_reg & BMSR_ANEGCOMPLETE)) {
184 if (i > PHY_AUTONEGOTIATE_TIMEOUT) {
185 puts(" TIMEOUT !\n");
191 puts("user interrupt!\n");
196 if ((i++ % 1000) == 0) {
199 udelay(1000); /* 1 ms */
200 mii_reg = read_phy_reg(priv, MIIM_STATUS);
204 /* Link status bit is latched low, read it again */
205 mii_reg = read_phy_reg(priv, MIIM_STATUS);
207 udelay(500000); /* another 500 ms (results in faster booting) */
210 priv->link = mii_reg & MIIM_STATUS_LINK ? 1 : 0;
215 /* Generic function which updates the speed and duplex. If
216 * autonegotiation is enabled, it uses the AND of the link
217 * partner's advertised capabilities and our advertised
218 * capabilities. If autonegotiation is disabled, we use the
219 * appropriate bits in the control register.
221 * Stolen from Linux's mii.c and phy_device.c
223 static uint mii_parse_link(uint mii_reg, struct tsec_private *priv)
225 /* We're using autonegotiation */
226 if (mii_reg & BMSR_ANEGCAPABLE) {
230 /* Check for gigabit capability */
231 if (mii_reg & BMSR_ERCAP) {
232 /* We want a list of states supported by
233 * both PHYs in the link
235 gblpa = read_phy_reg(priv, MII_STAT1000);
236 gblpa &= read_phy_reg(priv, MII_CTRL1000) << 2;
239 /* Set the baseline so we only have to set them
240 * if they're different
245 /* Check the gigabit fields */
246 if (gblpa & (PHY_1000BTSR_1000FD | PHY_1000BTSR_1000HD)) {
249 if (gblpa & PHY_1000BTSR_1000FD)
256 lpa = read_phy_reg(priv, MII_ADVERTISE);
257 lpa &= read_phy_reg(priv, MII_LPA);
259 if (lpa & (LPA_100FULL | LPA_100HALF)) {
262 if (lpa & LPA_100FULL)
265 } else if (lpa & LPA_10FULL)
268 uint bmcr = read_phy_reg(priv, MII_BMCR);
273 if (bmcr & BMCR_FULLDPLX)
276 if (bmcr & BMCR_SPEED1000)
278 else if (bmcr & BMCR_SPEED100)
286 * "Ethernet@Wirespeed" needs to be enabled to achieve link in certain
287 * circumstances. eg a gigabit TSEC connected to a gigabit switch with
288 * a 4-wire ethernet cable. Both ends advertise gigabit, but can't
289 * link. "Ethernet@Wirespeed" reduces advertised speed until link
292 static uint mii_BCM54xx_wirespeed(uint mii_reg, struct tsec_private *priv)
294 return (read_phy_reg(priv, mii_reg) & 0x8FFF) | 0x8010;
298 * Parse the BCM54xx status register for speed and duplex information.
299 * The linux sungem_phy has this information, but in a table format.
301 static uint mii_parse_BCM54xx_sr(uint mii_reg, struct tsec_private *priv)
303 /* If there is no link, speed and duplex don't matter */
307 switch ((mii_reg & MIIM_BCM54xx_AUXSTATUS_LINKMODE_MASK) >>
308 MIIM_BCM54xx_AUXSTATUS_LINKMODE_SHIFT) {
334 printf("Auto-neg error, defaulting to 10BT/HD\n");
344 * Find out if PHY is in copper or serdes mode by looking at Expansion Reg
345 * 0x42 - "Operating Mode Status Register"
347 static int BCM8482_is_serdes(struct tsec_private *priv)
352 write_phy_reg(priv, MIIM_BCM54XX_EXP_SEL, MIIM_BCM54XX_EXP_SEL_ER | 0x42);
353 val = read_phy_reg(priv, MIIM_BCM54XX_EXP_DATA);
355 switch (val & 0x1f) {
356 case 0x0d: /* RGMII-to-100Base-FX */
357 case 0x0e: /* RGMII-to-SGMII */
358 case 0x0f: /* RGMII-to-SerDes */
359 case 0x12: /* SGMII-to-SerDes */
360 case 0x13: /* SGMII-to-100Base-FX */
361 case 0x16: /* SerDes-to-Serdes */
364 case 0x6: /* RGMII-to-Copper */
365 case 0x14: /* SGMII-to-Copper */
366 case 0x17: /* SerDes-to-Copper */
369 printf("ERROR, invalid PHY mode (0x%x\n)", val);
377 * Determine SerDes link speed and duplex from Expansion reg 0x42 "Operating
378 * Mode Status Register"
380 uint mii_parse_BCM5482_serdes_sr(struct tsec_private *priv)
385 /* Wait 1s for link - Clause 37 autonegotiation happens very fast */
387 write_phy_reg(priv, MIIM_BCM54XX_EXP_SEL,
388 MIIM_BCM54XX_EXP_SEL_ER | 0x42);
389 val = read_phy_reg(priv, MIIM_BCM54XX_EXP_DATA);
399 udelay(1000); /* 1 ms */
403 switch ((val >> 13) & 0x3) {
415 priv->duplexity = (val & 0x1000) == 0x1000;
421 * Figure out if BCM5482 is in serdes or copper mode and determine link
422 * configuration accordingly
424 static uint mii_parse_BCM5482_sr(uint mii_reg, struct tsec_private *priv)
426 if (BCM8482_is_serdes(priv)) {
427 mii_parse_BCM5482_serdes_sr(priv);
428 priv->flags |= TSEC_FIBER;
430 /* Wait for auto-negotiation to complete or fail */
431 mii_parse_sr(mii_reg, priv);
433 /* Parse BCM54xx copper aux status register */
434 mii_reg = read_phy_reg(priv, MIIM_BCM54xx_AUXSTATUS);
435 mii_parse_BCM54xx_sr(mii_reg, priv);
441 /* Parse the 88E1011's status register for speed and duplex
444 static uint mii_parse_88E1011_psr(uint mii_reg, struct tsec_private * priv)
448 mii_reg = read_phy_reg(priv, MIIM_88E1011_PHY_STATUS);
450 if ((mii_reg & MIIM_88E1011_PHYSTAT_LINK) &&
451 !(mii_reg & MIIM_88E1011_PHYSTAT_SPDDONE)) {
454 puts("Waiting for PHY realtime link");
455 while (!(mii_reg & MIIM_88E1011_PHYSTAT_SPDDONE)) {
456 /* Timeout reached ? */
457 if (i > PHY_AUTONEGOTIATE_TIMEOUT) {
458 puts(" TIMEOUT !\n");
463 if ((i++ % 1000) == 0) {
466 udelay(1000); /* 1 ms */
467 mii_reg = read_phy_reg(priv, MIIM_88E1011_PHY_STATUS);
470 udelay(500000); /* another 500 ms (results in faster booting) */
472 if (mii_reg & MIIM_88E1011_PHYSTAT_LINK)
478 if (mii_reg & MIIM_88E1011_PHYSTAT_DUPLEX)
483 speed = (mii_reg & MIIM_88E1011_PHYSTAT_SPEED);
486 case MIIM_88E1011_PHYSTAT_GBIT:
489 case MIIM_88E1011_PHYSTAT_100:
499 /* Parse the RTL8211B's status register for speed and duplex
502 static uint mii_parse_RTL8211B_sr(uint mii_reg, struct tsec_private * priv)
506 mii_reg = read_phy_reg(priv, MIIM_RTL8211B_PHY_STATUS);
507 if (!(mii_reg & MIIM_RTL8211B_PHYSTAT_SPDDONE)) {
510 /* in case of timeout ->link is cleared */
512 puts("Waiting for PHY realtime link");
513 while (!(mii_reg & MIIM_RTL8211B_PHYSTAT_SPDDONE)) {
514 /* Timeout reached ? */
515 if (i > PHY_AUTONEGOTIATE_TIMEOUT) {
516 puts(" TIMEOUT !\n");
521 if ((i++ % 1000) == 0) {
524 udelay(1000); /* 1 ms */
525 mii_reg = read_phy_reg(priv, MIIM_RTL8211B_PHY_STATUS);
528 udelay(500000); /* another 500 ms (results in faster booting) */
530 if (mii_reg & MIIM_RTL8211B_PHYSTAT_LINK)
536 if (mii_reg & MIIM_RTL8211B_PHYSTAT_DUPLEX)
541 speed = (mii_reg & MIIM_RTL8211B_PHYSTAT_SPEED);
544 case MIIM_RTL8211B_PHYSTAT_GBIT:
547 case MIIM_RTL8211B_PHYSTAT_100:
557 /* Parse the cis8201's status register for speed and duplex
560 static uint mii_parse_cis8201(uint mii_reg, struct tsec_private * priv)
564 if (mii_reg & MIIM_CIS8201_AUXCONSTAT_DUPLEX)
569 speed = mii_reg & MIIM_CIS8201_AUXCONSTAT_SPEED;
571 case MIIM_CIS8201_AUXCONSTAT_GBIT:
574 case MIIM_CIS8201_AUXCONSTAT_100:
585 /* Parse the vsc8244's status register for speed and duplex
588 static uint mii_parse_vsc8244(uint mii_reg, struct tsec_private * priv)
592 if (mii_reg & MIIM_VSC8244_AUXCONSTAT_DUPLEX)
597 speed = mii_reg & MIIM_VSC8244_AUXCONSTAT_SPEED;
599 case MIIM_VSC8244_AUXCONSTAT_GBIT:
602 case MIIM_VSC8244_AUXCONSTAT_100:
613 /* Parse the DM9161's status register for speed and duplex
616 static uint mii_parse_dm9161_scsr(uint mii_reg, struct tsec_private * priv)
618 if (mii_reg & (MIIM_DM9161_SCSR_100F | MIIM_DM9161_SCSR_100H))
623 if (mii_reg & (MIIM_DM9161_SCSR_100F | MIIM_DM9161_SCSR_10F))
632 * Hack to write all 4 PHYs with the LED values
634 static uint mii_cis8204_fixled(uint mii_reg, struct tsec_private * priv)
637 tsec_mdio_t *regbase = priv->phyregs;
638 int timeout = 1000000;
640 for (phyid = 0; phyid < 4; phyid++) {
641 out_be32(®base->miimadd, (phyid << 8) | mii_reg);
642 out_be32(®base->miimcon, MIIM_CIS8204_SLEDCON_INIT);
645 while ((in_be32(®base->miimind) & MIIMIND_BUSY) && timeout--)
649 return MIIM_CIS8204_SLEDCON_INIT;
652 static uint mii_cis8204_setmode(uint mii_reg, struct tsec_private * priv)
654 if (priv->flags & TSEC_REDUCED)
655 return MIIM_CIS8204_EPHYCON_INIT | MIIM_CIS8204_EPHYCON_RGMII;
657 return MIIM_CIS8204_EPHYCON_INIT;
660 static uint mii_m88e1111s_setmode(uint mii_reg, struct tsec_private *priv)
662 uint mii_data = read_phy_reg(priv, mii_reg);
664 if (priv->flags & TSEC_REDUCED)
665 mii_data = (mii_data & 0xfff0) | 0x000b;
669 static struct phy_info phy_info_M88E1149S = {
673 (struct phy_cmd[]) { /* config */
674 /* Reset and configure the PHY */
675 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
677 {0x1e, 0x200c, NULL},
681 {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
682 {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
683 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
684 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
687 (struct phy_cmd[]) { /* startup */
688 /* Status is read once to clear old link state */
689 {MIIM_STATUS, miim_read, NULL},
691 {MIIM_STATUS, miim_read, &mii_parse_sr},
692 /* Read the status */
693 {MIIM_88E1011_PHY_STATUS, miim_read, &mii_parse_88E1011_psr},
696 (struct phy_cmd[]) { /* shutdown */
701 /* The 5411 id is 0x206070, the 5421 is 0x2060e0 */
702 static struct phy_info phy_info_BCM5461S = {
703 0x02060c1, /* 5461 ID */
705 0, /* not clear to me what minor revisions we can shift away */
706 (struct phy_cmd[]) { /* config */
707 /* Reset and configure the PHY */
708 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
709 {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
710 {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
711 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
712 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
715 (struct phy_cmd[]) { /* startup */
716 /* Status is read once to clear old link state */
717 {MIIM_STATUS, miim_read, NULL},
719 {MIIM_STATUS, miim_read, &mii_parse_sr},
720 /* Read the status */
721 {MIIM_BCM54xx_AUXSTATUS, miim_read, &mii_parse_BCM54xx_sr},
724 (struct phy_cmd[]) { /* shutdown */
729 static struct phy_info phy_info_BCM5464S = {
730 0x02060b1, /* 5464 ID */
732 0, /* not clear to me what minor revisions we can shift away */
733 (struct phy_cmd[]) { /* config */
734 /* Reset and configure the PHY */
735 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
736 {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
737 {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
738 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
739 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
742 (struct phy_cmd[]) { /* startup */
743 /* Status is read once to clear old link state */
744 {MIIM_STATUS, miim_read, NULL},
746 {MIIM_STATUS, miim_read, &mii_parse_sr},
747 /* Read the status */
748 {MIIM_BCM54xx_AUXSTATUS, miim_read, &mii_parse_BCM54xx_sr},
751 (struct phy_cmd[]) { /* shutdown */
756 static struct phy_info phy_info_BCM5482S = {
760 (struct phy_cmd[]) { /* config */
761 /* Reset and configure the PHY */
762 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
763 /* Setup read from auxilary control shadow register 7 */
764 {MIIM_BCM54xx_AUXCNTL, MIIM_BCM54xx_AUXCNTL_ENCODE(7), NULL},
765 /* Read Misc Control register and or in Ethernet@Wirespeed */
766 {MIIM_BCM54xx_AUXCNTL, 0, &mii_BCM54xx_wirespeed},
767 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
768 /* Initial config/enable of secondary SerDes interface */
769 {MIIM_BCM54XX_SHD, MIIM_BCM54XX_SHD_WR_ENCODE(0x14, 0xf), NULL},
770 /* Write intial value to secondary SerDes Contol */
771 {MIIM_BCM54XX_EXP_SEL, MIIM_BCM54XX_EXP_SEL_SSD | 0, NULL},
772 {MIIM_BCM54XX_EXP_DATA, MIIM_CONTROL_RESTART, NULL},
773 /* Enable copper/fiber auto-detect */
774 {MIIM_BCM54XX_SHD, MIIM_BCM54XX_SHD_WR_ENCODE(0x1e, 0x201)},
777 (struct phy_cmd[]) { /* startup */
778 /* Status is read once to clear old link state */
779 {MIIM_STATUS, miim_read, NULL},
780 /* Determine copper/fiber, auto-negotiate, and read the result */
781 {MIIM_STATUS, miim_read, &mii_parse_BCM5482_sr},
784 (struct phy_cmd[]) { /* shutdown */
789 static struct phy_info phy_info_M88E1011S = {
793 (struct phy_cmd[]) { /* config */
794 /* Reset and configure the PHY */
795 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
797 {0x1e, 0x200c, NULL},
801 {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
802 {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
803 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
804 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
807 (struct phy_cmd[]) { /* startup */
808 /* Status is read once to clear old link state */
809 {MIIM_STATUS, miim_read, NULL},
811 {MIIM_STATUS, miim_read, &mii_parse_sr},
812 /* Read the status */
813 {MIIM_88E1011_PHY_STATUS, miim_read, &mii_parse_88E1011_psr},
816 (struct phy_cmd[]) { /* shutdown */
821 static struct phy_info phy_info_M88E1111S = {
825 (struct phy_cmd[]) { /* config */
826 /* Reset and configure the PHY */
827 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
828 {0x1b, 0x848f, &mii_m88e1111s_setmode},
829 {0x14, 0x0cd2, NULL}, /* Delay RGMII TX and RX */
830 {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
831 {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
832 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
833 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
836 (struct phy_cmd[]) { /* startup */
837 /* Status is read once to clear old link state */
838 {MIIM_STATUS, miim_read, NULL},
840 {MIIM_STATUS, miim_read, &mii_parse_sr},
841 /* Read the status */
842 {MIIM_88E1011_PHY_STATUS, miim_read, &mii_parse_88E1011_psr},
845 (struct phy_cmd[]) { /* shutdown */
850 static struct phy_info phy_info_M88E1118 = {
854 (struct phy_cmd[]) { /* config */
855 /* Reset and configure the PHY */
856 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
857 {0x16, 0x0002, NULL}, /* Change Page Number */
858 {0x15, 0x1070, NULL}, /* Delay RGMII TX and RX */
859 {0x16, 0x0003, NULL}, /* Change Page Number */
860 {0x10, 0x021e, NULL}, /* Adjust LED control */
861 {0x16, 0x0000, NULL}, /* Change Page Number */
862 {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
863 {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
864 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
865 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
868 (struct phy_cmd[]) { /* startup */
869 {0x16, 0x0000, NULL}, /* Change Page Number */
870 /* Status is read once to clear old link state */
871 {MIIM_STATUS, miim_read, NULL},
873 {MIIM_STATUS, miim_read, &mii_parse_sr},
874 /* Read the status */
875 {MIIM_88E1011_PHY_STATUS, miim_read,
876 &mii_parse_88E1011_psr},
879 (struct phy_cmd[]) { /* shutdown */
885 * Since to access LED register we need do switch the page, we
886 * do LED configuring in the miim_read-like function as follows
888 static uint mii_88E1121_set_led (uint mii_reg, struct tsec_private *priv)
892 /* Switch the page to access the led register */
893 pg = read_phy_reg(priv, MIIM_88E1121_PHY_PAGE);
894 write_phy_reg(priv, MIIM_88E1121_PHY_PAGE, MIIM_88E1121_PHY_LED_PAGE);
897 write_phy_reg(priv, MIIM_88E1121_PHY_LED_CTRL,
898 MIIM_88E1121_PHY_LED_DEF);
900 /* Restore the page pointer */
901 write_phy_reg(priv, MIIM_88E1121_PHY_PAGE, pg);
905 static struct phy_info phy_info_M88E1121R = {
909 (struct phy_cmd[]) { /* config */
910 /* Reset and configure the PHY */
911 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
912 {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
913 {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
915 {MIIM_88E1121_PHY_LED_CTRL, miim_read, &mii_88E1121_set_led},
916 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
917 /* Disable IRQs and de-assert interrupt */
918 {MIIM_88E1121_PHY_IRQ_EN, 0, NULL},
919 {MIIM_88E1121_PHY_IRQ_STATUS, miim_read, NULL},
922 (struct phy_cmd[]) { /* startup */
923 /* Status is read once to clear old link state */
924 {MIIM_STATUS, miim_read, NULL},
925 {MIIM_STATUS, miim_read, &mii_parse_sr},
926 {MIIM_STATUS, miim_read, &mii_parse_link},
929 (struct phy_cmd[]) { /* shutdown */
934 static unsigned int m88e1145_setmode(uint mii_reg, struct tsec_private *priv)
936 uint mii_data = read_phy_reg(priv, mii_reg);
938 /* Setting MIIM_88E1145_PHY_EXT_CR */
939 if (priv->flags & TSEC_REDUCED)
941 MIIM_M88E1145_RGMII_RX_DELAY | MIIM_M88E1145_RGMII_TX_DELAY;
946 static struct phy_info phy_info_M88E1145 = {
950 (struct phy_cmd[]) { /* config */
952 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
960 /* Configure the PHY */
961 {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
962 {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
963 {MIIM_88E1011_PHY_SCR, MIIM_88E1011_PHY_MDI_X_AUTO, NULL},
964 {MIIM_88E1145_PHY_EXT_CR, 0, &m88e1145_setmode},
965 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
966 {MIIM_CONTROL, MIIM_CONTROL_INIT, NULL},
969 (struct phy_cmd[]) { /* startup */
970 /* Status is read once to clear old link state */
971 {MIIM_STATUS, miim_read, NULL},
973 {MIIM_STATUS, miim_read, &mii_parse_sr},
974 {MIIM_88E1111_PHY_LED_CONTROL, MIIM_88E1111_PHY_LED_DIRECT, NULL},
975 /* Read the Status */
976 {MIIM_88E1011_PHY_STATUS, miim_read, &mii_parse_88E1011_psr},
979 (struct phy_cmd[]) { /* shutdown */
984 static struct phy_info phy_info_cis8204 = {
988 (struct phy_cmd[]) { /* config */
989 /* Override PHY config settings */
990 {MIIM_CIS8201_AUX_CONSTAT, MIIM_CIS8201_AUXCONSTAT_INIT, NULL},
991 /* Configure some basic stuff */
992 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
993 {MIIM_CIS8204_SLED_CON, MIIM_CIS8204_SLEDCON_INIT,
994 &mii_cis8204_fixled},
995 {MIIM_CIS8204_EPHY_CON, MIIM_CIS8204_EPHYCON_INIT,
996 &mii_cis8204_setmode},
999 (struct phy_cmd[]) { /* startup */
1000 /* Read the Status (2x to make sure link is right) */
1001 {MIIM_STATUS, miim_read, NULL},
1002 /* Auto-negotiate */
1003 {MIIM_STATUS, miim_read, &mii_parse_sr},
1004 /* Read the status */
1005 {MIIM_CIS8201_AUX_CONSTAT, miim_read, &mii_parse_cis8201},
1008 (struct phy_cmd[]) { /* shutdown */
1014 static struct phy_info phy_info_cis8201 = {
1018 (struct phy_cmd[]) { /* config */
1019 /* Override PHY config settings */
1020 {MIIM_CIS8201_AUX_CONSTAT, MIIM_CIS8201_AUXCONSTAT_INIT, NULL},
1021 /* Set up the interface mode */
1022 {MIIM_CIS8201_EXT_CON1, MIIM_CIS8201_EXTCON1_INIT, NULL},
1023 /* Configure some basic stuff */
1024 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
1027 (struct phy_cmd[]) { /* startup */
1028 /* Read the Status (2x to make sure link is right) */
1029 {MIIM_STATUS, miim_read, NULL},
1030 /* Auto-negotiate */
1031 {MIIM_STATUS, miim_read, &mii_parse_sr},
1032 /* Read the status */
1033 {MIIM_CIS8201_AUX_CONSTAT, miim_read, &mii_parse_cis8201},
1036 (struct phy_cmd[]) { /* shutdown */
1041 static struct phy_info phy_info_VSC8211 = {
1045 (struct phy_cmd[]) { /* config */
1046 /* Override PHY config settings */
1047 {MIIM_CIS8201_AUX_CONSTAT, MIIM_CIS8201_AUXCONSTAT_INIT, NULL},
1048 /* Set up the interface mode */
1049 {MIIM_CIS8201_EXT_CON1, MIIM_CIS8201_EXTCON1_INIT, NULL},
1050 /* Configure some basic stuff */
1051 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
1054 (struct phy_cmd[]) { /* startup */
1055 /* Read the Status (2x to make sure link is right) */
1056 {MIIM_STATUS, miim_read, NULL},
1057 /* Auto-negotiate */
1058 {MIIM_STATUS, miim_read, &mii_parse_sr},
1059 /* Read the status */
1060 {MIIM_CIS8201_AUX_CONSTAT, miim_read, &mii_parse_cis8201},
1063 (struct phy_cmd[]) { /* shutdown */
1068 static struct phy_info phy_info_VSC8244 = {
1072 (struct phy_cmd[]) { /* config */
1073 /* Override PHY config settings */
1074 /* Configure some basic stuff */
1075 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
1078 (struct phy_cmd[]) { /* startup */
1079 /* Read the Status (2x to make sure link is right) */
1080 {MIIM_STATUS, miim_read, NULL},
1081 /* Auto-negotiate */
1082 {MIIM_STATUS, miim_read, &mii_parse_sr},
1083 /* Read the status */
1084 {MIIM_VSC8244_AUX_CONSTAT, miim_read, &mii_parse_vsc8244},
1087 (struct phy_cmd[]) { /* shutdown */
1092 static struct phy_info phy_info_VSC8641 = {
1096 (struct phy_cmd[]) { /* config */
1097 /* Configure some basic stuff */
1098 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
1101 (struct phy_cmd[]) { /* startup */
1102 /* Read the Status (2x to make sure link is right) */
1103 {MIIM_STATUS, miim_read, NULL},
1104 /* Auto-negotiate */
1105 {MIIM_STATUS, miim_read, &mii_parse_sr},
1106 /* Read the status */
1107 {MIIM_VSC8244_AUX_CONSTAT, miim_read, &mii_parse_vsc8244},
1110 (struct phy_cmd[]) { /* shutdown */
1115 static struct phy_info phy_info_VSC8221 = {
1119 (struct phy_cmd[]) { /* config */
1120 /* Configure some basic stuff */
1121 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
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_VSC8244_AUX_CONSTAT, miim_read, &mii_parse_vsc8244},
1133 (struct phy_cmd[]) { /* shutdown */
1138 static struct phy_info phy_info_VSC8601 = {
1142 (struct phy_cmd[]) { /* config */
1143 /* Override PHY config settings */
1144 /* Configure some basic stuff */
1145 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
1146 #ifdef CONFIG_SYS_VSC8601_SKEWFIX
1147 {MIIM_VSC8601_EPHY_CON,MIIM_VSC8601_EPHY_CON_INIT_SKEW,NULL},
1148 #if defined(CONFIG_SYS_VSC8601_SKEW_TX) && defined(CONFIG_SYS_VSC8601_SKEW_RX)
1149 {MIIM_EXT_PAGE_ACCESS,1,NULL},
1150 #define VSC8101_SKEW \
1151 (CONFIG_SYS_VSC8601_SKEW_TX << 14) | (CONFIG_SYS_VSC8601_SKEW_RX << 12)
1152 {MIIM_VSC8601_SKEW_CTRL,VSC8101_SKEW,NULL},
1153 {MIIM_EXT_PAGE_ACCESS,0,NULL},
1156 {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
1157 {MIIM_CONTROL, MIIM_CONTROL_RESTART, &mii_cr_init},
1160 (struct phy_cmd[]) { /* startup */
1161 /* Read the Status (2x to make sure link is right) */
1162 {MIIM_STATUS, miim_read, NULL},
1163 /* Auto-negotiate */
1164 {MIIM_STATUS, miim_read, &mii_parse_sr},
1165 /* Read the status */
1166 {MIIM_VSC8244_AUX_CONSTAT, miim_read, &mii_parse_vsc8244},
1169 (struct phy_cmd[]) { /* shutdown */
1174 static struct phy_info phy_info_dm9161 = {
1178 (struct phy_cmd[]) { /* config */
1179 {MIIM_CONTROL, MIIM_DM9161_CR_STOP, NULL},
1180 /* Do not bypass the scrambler/descrambler */
1181 {MIIM_DM9161_SCR, MIIM_DM9161_SCR_INIT, NULL},
1182 /* Clear 10BTCSR to default */
1183 {MIIM_DM9161_10BTCSR, MIIM_DM9161_10BTCSR_INIT, NULL},
1184 /* Configure some basic stuff */
1185 {MIIM_CONTROL, MIIM_CR_INIT, NULL},
1186 /* Restart Auto Negotiation */
1187 {MIIM_CONTROL, MIIM_DM9161_CR_RSTAN, NULL},
1190 (struct phy_cmd[]) { /* startup */
1191 /* Status is read once to clear old link state */
1192 {MIIM_STATUS, miim_read, NULL},
1193 /* Auto-negotiate */
1194 {MIIM_STATUS, miim_read, &mii_parse_sr},
1195 /* Read the status */
1196 {MIIM_DM9161_SCSR, miim_read, &mii_parse_dm9161_scsr},
1199 (struct phy_cmd[]) { /* shutdown */
1205 static struct phy_info phy_info_ksz804 = {
1207 "Micrel KSZ804 PHY",
1209 (struct phy_cmd[]) { /* config */
1210 {MII_BMCR, BMCR_RESET, NULL},
1211 {MII_BMCR, BMCR_ANENABLE|BMCR_ANRESTART, NULL},
1214 (struct phy_cmd[]) { /* startup */
1215 {MII_BMSR, miim_read, NULL},
1216 {MII_BMSR, miim_read, &mii_parse_sr},
1217 {MII_BMSR, miim_read, &mii_parse_link},
1220 (struct phy_cmd[]) { /* shutdown */
1225 /* a generic flavor. */
1226 static struct phy_info phy_info_generic = {
1228 "Unknown/Generic PHY",
1230 (struct phy_cmd[]) { /* config */
1231 {MII_BMCR, BMCR_RESET, NULL},
1232 {MII_BMCR, BMCR_ANENABLE|BMCR_ANRESTART, NULL},
1235 (struct phy_cmd[]) { /* startup */
1236 {MII_BMSR, miim_read, NULL},
1237 {MII_BMSR, miim_read, &mii_parse_sr},
1238 {MII_BMSR, miim_read, &mii_parse_link},
1241 (struct phy_cmd[]) { /* shutdown */
1246 static uint mii_parse_lxt971_sr2(uint mii_reg, struct tsec_private *priv)
1250 speed = mii_reg & MIIM_LXT971_SR2_SPEED_MASK;
1253 case MIIM_LXT971_SR2_10HDX:
1255 priv->duplexity = 0;
1257 case MIIM_LXT971_SR2_10FDX:
1259 priv->duplexity = 1;
1261 case MIIM_LXT971_SR2_100HDX:
1263 priv->duplexity = 0;
1267 priv->duplexity = 1;
1271 priv->duplexity = 0;
1277 static struct phy_info phy_info_lxt971 = {
1281 (struct phy_cmd[]) { /* config */
1282 {MIIM_CR, MIIM_CR_INIT, mii_cr_init}, /* autonegotiate */
1285 (struct phy_cmd[]) { /* startup - enable interrupts */
1286 /* { 0x12, 0x00f2, NULL }, */
1287 {MIIM_STATUS, miim_read, NULL},
1288 {MIIM_STATUS, miim_read, &mii_parse_sr},
1289 {MIIM_LXT971_SR2, miim_read, &mii_parse_lxt971_sr2},
1292 (struct phy_cmd[]) { /* shutdown - disable interrupts */
1297 /* Parse the DP83865's link and auto-neg status register for speed and duplex
1300 static uint mii_parse_dp83865_lanr(uint mii_reg, struct tsec_private *priv)
1302 switch (mii_reg & MIIM_DP83865_SPD_MASK) {
1304 case MIIM_DP83865_SPD_1000:
1308 case MIIM_DP83865_SPD_100:
1318 if (mii_reg & MIIM_DP83865_DPX_FULL)
1319 priv->duplexity = 1;
1321 priv->duplexity = 0;
1326 static struct phy_info phy_info_dp83865 = {
1330 (struct phy_cmd[]) { /* config */
1331 {MIIM_CONTROL, MIIM_DP83865_CR_INIT, NULL},
1334 (struct phy_cmd[]) { /* startup */
1335 /* Status is read once to clear old link state */
1336 {MIIM_STATUS, miim_read, NULL},
1337 /* Auto-negotiate */
1338 {MIIM_STATUS, miim_read, &mii_parse_sr},
1339 /* Read the link and auto-neg status */
1340 {MIIM_DP83865_LANR, miim_read, &mii_parse_dp83865_lanr},
1343 (struct phy_cmd[]) { /* shutdown */
1348 static struct phy_info phy_info_rtl8211b = {
1352 (struct phy_cmd[]) { /* config */
1353 /* Reset and configure the PHY */
1354 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
1355 {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
1356 {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
1357 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
1358 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
1361 (struct phy_cmd[]) { /* startup */
1362 /* Status is read once to clear old link state */
1363 {MIIM_STATUS, miim_read, NULL},
1364 /* Auto-negotiate */
1365 {MIIM_STATUS, miim_read, &mii_parse_sr},
1366 /* Read the status */
1367 {MIIM_RTL8211B_PHY_STATUS, miim_read, &mii_parse_RTL8211B_sr},
1370 (struct phy_cmd[]) { /* shutdown */
1375 struct phy_info phy_info_AR8021 = {
1379 (struct phy_cmd[]) { /* config */
1380 {MII_BMCR, BMCR_RESET, NULL},
1381 {MII_BMCR, BMCR_ANENABLE|BMCR_ANRESTART, NULL},
1383 {0x1e, 0x3D47, NULL},
1386 (struct phy_cmd[]) { /* startup */
1387 {MII_BMSR, miim_read, NULL},
1388 {MII_BMSR, miim_read, &mii_parse_sr},
1389 {MII_BMSR, miim_read, &mii_parse_link},
1392 (struct phy_cmd[]) { /* shutdown */
1397 static struct phy_info *phy_info[] = {
1403 &phy_info_M88E1011S,
1404 &phy_info_M88E1111S,
1406 &phy_info_M88E1121R,
1408 &phy_info_M88E1149S,
1420 &phy_info_generic, /* must be last; has ID 0 and 32 bit mask */
1424 /* Grab the identifier of the device's PHY, and search through
1425 * all of the known PHYs to see if one matches. If so, return
1426 * it, if not, return NULL
1428 static struct phy_info *get_phy_info(struct eth_device *dev)
1430 struct tsec_private *priv = (struct tsec_private *)dev->priv;
1431 uint phy_reg, phy_ID;
1433 struct phy_info *theInfo = NULL;
1435 /* Grab the bits from PHYIR1, and put them in the upper half */
1436 phy_reg = read_phy_reg(priv, MIIM_PHYIR1);
1437 phy_ID = (phy_reg & 0xffff) << 16;
1439 /* Grab the bits from PHYIR2, and put them in the lower half */
1440 phy_reg = read_phy_reg(priv, MIIM_PHYIR2);
1441 phy_ID |= (phy_reg & 0xffff);
1443 /* loop through all the known PHY types, and find one that */
1444 /* matches the ID we read from the PHY. */
1445 for (i = 0; phy_info[i]; i++) {
1446 if (phy_info[i]->id == (phy_ID >> phy_info[i]->shift)) {
1447 theInfo = phy_info[i];
1452 if (theInfo == &phy_info_generic) {
1453 printf("%s: No support for PHY id %x; assuming generic\n",
1456 debug("%s: PHY is %s (%x)\n", dev->name, theInfo->name, phy_ID);
1462 /* Execute the given series of commands on the given device's
1463 * PHY, running functions as necessary
1465 static void phy_run_commands(struct tsec_private *priv, struct phy_cmd *cmd)
1469 tsec_mdio_t *phyregs = priv->phyregs;
1471 out_be32(&phyregs->miimcfg, MIIMCFG_RESET);
1473 out_be32(&phyregs->miimcfg, MIIMCFG_INIT_VALUE);
1475 while (in_be32(&phyregs->miimind) & MIIMIND_BUSY)
1478 for (i = 0; cmd->mii_reg != miim_end; i++) {
1479 if (cmd->mii_data == miim_read) {
1480 result = read_phy_reg(priv, cmd->mii_reg);
1482 if (cmd->funct != NULL)
1483 (*(cmd->funct)) (result, priv);
1486 if (cmd->funct != NULL)
1487 result = (*(cmd->funct)) (cmd->mii_reg, priv);
1489 result = cmd->mii_data;
1491 write_phy_reg(priv, cmd->mii_reg, result);
1498 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) \
1499 && !defined(BITBANGMII)
1502 * Read a MII PHY register.
1507 static int tsec_miiphy_read(const char *devname, unsigned char addr,
1508 unsigned char reg, unsigned short *value)
1511 struct tsec_private *priv = privlist[0];
1514 printf("Can't read PHY at address %d\n", addr);
1518 ret = (unsigned short)tsec_local_mdio_read(priv->phyregs, addr, reg);
1525 * Write a MII PHY register.
1530 static int tsec_miiphy_write(const char *devname, unsigned char addr,
1531 unsigned char reg, unsigned short value)
1533 struct tsec_private *priv = privlist[0];
1536 printf("Can't write PHY at address %d\n", addr);
1540 tsec_local_mdio_write(priv->phyregs, addr, reg, value);
1547 #ifdef CONFIG_MCAST_TFTP
1549 /* CREDITS: linux gianfar driver, slightly adjusted... thanx. */
1551 /* Set the appropriate hash bit for the given addr */
1553 /* The algorithm works like so:
1554 * 1) Take the Destination Address (ie the multicast address), and
1555 * do a CRC on it (little endian), and reverse the bits of the
1557 * 2) Use the 8 most significant bits as a hash into a 256-entry
1558 * table. The table is controlled through 8 32-bit registers:
1559 * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is
1560 * gaddr7. This means that the 3 most significant bits in the
1561 * hash index which gaddr register to use, and the 5 other bits
1562 * indicate which bit (assuming an IBM numbering scheme, which
1563 * for PowerPC (tm) is usually the case) in the tregister holds
1566 tsec_mcast_addr (struct eth_device *dev, u8 mcast_mac, u8 set)
1568 struct tsec_private *priv = privlist[1];
1569 volatile tsec_t *regs = priv->regs;
1570 volatile u32 *reg_array, value;
1571 u8 result, whichbit, whichreg;
1573 result = (u8)((ether_crc(MAC_ADDR_LEN,mcast_mac) >> 24) & 0xff);
1574 whichbit = result & 0x1f; /* the 5 LSB = which bit to set */
1575 whichreg = result >> 5; /* the 3 MSB = which reg to set it in */
1576 value = (1 << (31-whichbit));
1578 reg_array = &(regs->hash.gaddr0);
1581 reg_array[whichreg] |= value;
1583 reg_array[whichreg] &= ~value;
1587 #endif /* Multicast TFTP ? */
1589 /* Initialized required registers to appropriate values, zeroing
1590 * those we don't care about (unless zero is bad, in which case,
1591 * choose a more appropriate value)
1593 static void init_registers(tsec_t *regs)
1596 out_be32(®s->ievent, IEVENT_INIT_CLEAR);
1598 out_be32(®s->imask, IMASK_INIT_CLEAR);
1600 out_be32(®s->hash.iaddr0, 0);
1601 out_be32(®s->hash.iaddr1, 0);
1602 out_be32(®s->hash.iaddr2, 0);
1603 out_be32(®s->hash.iaddr3, 0);
1604 out_be32(®s->hash.iaddr4, 0);
1605 out_be32(®s->hash.iaddr5, 0);
1606 out_be32(®s->hash.iaddr6, 0);
1607 out_be32(®s->hash.iaddr7, 0);
1609 out_be32(®s->hash.gaddr0, 0);
1610 out_be32(®s->hash.gaddr1, 0);
1611 out_be32(®s->hash.gaddr2, 0);
1612 out_be32(®s->hash.gaddr3, 0);
1613 out_be32(®s->hash.gaddr4, 0);
1614 out_be32(®s->hash.gaddr5, 0);
1615 out_be32(®s->hash.gaddr6, 0);
1616 out_be32(®s->hash.gaddr7, 0);
1618 out_be32(®s->rctrl, 0x00000000);
1620 /* Init RMON mib registers */
1621 memset((void *)&(regs->rmon), 0, sizeof(rmon_mib_t));
1623 out_be32(®s->rmon.cam1, 0xffffffff);
1624 out_be32(®s->rmon.cam2, 0xffffffff);
1626 out_be32(®s->mrblr, MRBLR_INIT_SETTINGS);
1628 out_be32(®s->minflr, MINFLR_INIT_SETTINGS);
1630 out_be32(®s->attr, ATTR_INIT_SETTINGS);
1631 out_be32(®s->attreli, ATTRELI_INIT_SETTINGS);
1635 /* Configure maccfg2 based on negotiated speed and duplex
1636 * reported by PHY handling code
1638 static void adjust_link(struct eth_device *dev)
1640 struct tsec_private *priv = (struct tsec_private *)dev->priv;
1641 tsec_t *regs = priv->regs;
1642 u32 ecntrl, maccfg2;
1645 printf("%s: No link.\n", dev->name);
1649 /* clear all bits relative with interface mode */
1650 ecntrl = in_be32(®s->ecntrl);
1651 ecntrl &= ~ECNTRL_R100;
1653 maccfg2 = in_be32(®s->maccfg2);
1654 maccfg2 &= ~(MACCFG2_IF | MACCFG2_FULL_DUPLEX);
1656 if (priv->duplexity)
1657 maccfg2 |= MACCFG2_FULL_DUPLEX;
1659 switch (priv->speed) {
1661 maccfg2 |= MACCFG2_GMII;
1665 maccfg2 |= MACCFG2_MII;
1667 /* Set R100 bit in all modes although
1668 * it is only used in RGMII mode
1670 if (priv->speed == 100)
1671 ecntrl |= ECNTRL_R100;
1674 printf("%s: Speed was bad\n", dev->name);
1678 out_be32(®s->ecntrl, ecntrl);
1679 out_be32(®s->maccfg2, maccfg2);
1681 printf("Speed: %d, %s duplex%s\n", priv->speed,
1682 (priv->duplexity) ? "full" : "half",
1683 (priv->flags & TSEC_FIBER) ? ", fiber mode" : "");
1686 /* Set up the buffers and their descriptors, and bring up the
1689 static void startup_tsec(struct eth_device *dev)
1692 struct tsec_private *priv = (struct tsec_private *)dev->priv;
1693 tsec_t *regs = priv->regs;
1695 /* Point to the buffer descriptors */
1696 out_be32(®s->tbase, (unsigned int)(&rtx.txbd[txIdx]));
1697 out_be32(®s->rbase, (unsigned int)(&rtx.rxbd[rxIdx]));
1699 /* Initialize the Rx Buffer descriptors */
1700 for (i = 0; i < PKTBUFSRX; i++) {
1701 rtx.rxbd[i].status = RXBD_EMPTY;
1702 rtx.rxbd[i].length = 0;
1703 rtx.rxbd[i].bufPtr = (uint) NetRxPackets[i];
1705 rtx.rxbd[PKTBUFSRX - 1].status |= RXBD_WRAP;
1707 /* Initialize the TX Buffer Descriptors */
1708 for (i = 0; i < TX_BUF_CNT; i++) {
1709 rtx.txbd[i].status = 0;
1710 rtx.txbd[i].length = 0;
1711 rtx.txbd[i].bufPtr = 0;
1713 rtx.txbd[TX_BUF_CNT - 1].status |= TXBD_WRAP;
1715 /* Start up the PHY */
1717 phy_run_commands(priv, priv->phyinfo->startup);
1721 /* Enable Transmit and Receive */
1722 setbits_be32(®s->maccfg1, MACCFG1_RX_EN | MACCFG1_TX_EN);
1724 /* Tell the DMA it is clear to go */
1725 setbits_be32(®s->dmactrl, DMACTRL_INIT_SETTINGS);
1726 out_be32(®s->tstat, TSTAT_CLEAR_THALT);
1727 out_be32(®s->rstat, RSTAT_CLEAR_RHALT);
1728 clrbits_be32(®s->dmactrl, DMACTRL_GRS | DMACTRL_GTS);
1731 /* This returns the status bits of the device. The return value
1732 * is never checked, and this is what the 8260 driver did, so we
1733 * do the same. Presumably, this would be zero if there were no
1736 static int tsec_send(struct eth_device *dev, volatile void *packet, int length)
1740 struct tsec_private *priv = (struct tsec_private *)dev->priv;
1741 tsec_t *regs = priv->regs;
1743 /* Find an empty buffer descriptor */
1744 for (i = 0; rtx.txbd[txIdx].status & TXBD_READY; i++) {
1745 if (i >= TOUT_LOOP) {
1746 debug("%s: tsec: tx buffers full\n", dev->name);
1751 rtx.txbd[txIdx].bufPtr = (uint) packet;
1752 rtx.txbd[txIdx].length = length;
1753 rtx.txbd[txIdx].status |=
1754 (TXBD_READY | TXBD_LAST | TXBD_CRC | TXBD_INTERRUPT);
1756 /* Tell the DMA to go */
1757 out_be32(®s->tstat, TSTAT_CLEAR_THALT);
1759 /* Wait for buffer to be transmitted */
1760 for (i = 0; rtx.txbd[txIdx].status & TXBD_READY; i++) {
1761 if (i >= TOUT_LOOP) {
1762 debug("%s: tsec: tx error\n", dev->name);
1767 txIdx = (txIdx + 1) % TX_BUF_CNT;
1768 result = rtx.txbd[txIdx].status & TXBD_STATS;
1773 static int tsec_recv(struct eth_device *dev)
1776 struct tsec_private *priv = (struct tsec_private *)dev->priv;
1777 tsec_t *regs = priv->regs;
1779 while (!(rtx.rxbd[rxIdx].status & RXBD_EMPTY)) {
1781 length = rtx.rxbd[rxIdx].length;
1783 /* Send the packet up if there were no errors */
1784 if (!(rtx.rxbd[rxIdx].status & RXBD_STATS)) {
1785 NetReceive(NetRxPackets[rxIdx], length - 4);
1787 printf("Got error %x\n",
1788 (rtx.rxbd[rxIdx].status & RXBD_STATS));
1791 rtx.rxbd[rxIdx].length = 0;
1793 /* Set the wrap bit if this is the last element in the list */
1794 rtx.rxbd[rxIdx].status =
1795 RXBD_EMPTY | (((rxIdx + 1) == PKTBUFSRX) ? RXBD_WRAP : 0);
1797 rxIdx = (rxIdx + 1) % PKTBUFSRX;
1800 if (in_be32(®s->ievent) & IEVENT_BSY) {
1801 out_be32(®s->ievent, IEVENT_BSY);
1802 out_be32(®s->rstat, RSTAT_CLEAR_RHALT);
1809 /* Stop the interface */
1810 static void tsec_halt(struct eth_device *dev)
1812 struct tsec_private *priv = (struct tsec_private *)dev->priv;
1813 tsec_t *regs = priv->regs;
1815 clrbits_be32(®s->dmactrl, DMACTRL_GRS | DMACTRL_GTS);
1816 setbits_be32(®s->dmactrl, DMACTRL_GRS | DMACTRL_GTS);
1818 while ((in_be32(®s->ievent) & (IEVENT_GRSC | IEVENT_GTSC))
1819 != (IEVENT_GRSC | IEVENT_GTSC))
1822 clrbits_be32(®s->maccfg1, MACCFG1_TX_EN | MACCFG1_RX_EN);
1824 /* Shut down the PHY, as needed */
1826 phy_run_commands(priv, priv->phyinfo->shutdown);
1829 /* Initializes data structures and registers for the controller,
1830 * and brings the interface up. Returns the link status, meaning
1831 * that it returns success if the link is up, failure otherwise.
1832 * This allows u-boot to find the first active controller.
1834 static int tsec_init(struct eth_device *dev, bd_t * bd)
1837 char tmpbuf[MAC_ADDR_LEN];
1839 struct tsec_private *priv = (struct tsec_private *)dev->priv;
1840 tsec_t *regs = priv->regs;
1842 /* Make sure the controller is stopped */
1845 /* Init MACCFG2. Defaults to GMII */
1846 out_be32(®s->maccfg2, MACCFG2_INIT_SETTINGS);
1849 out_be32(®s->ecntrl, ECNTRL_INIT_SETTINGS);
1851 /* Copy the station address into the address registers.
1852 * Backwards, because little endian MACS are dumb */
1853 for (i = 0; i < MAC_ADDR_LEN; i++)
1854 tmpbuf[MAC_ADDR_LEN - 1 - i] = dev->enetaddr[i];
1856 tempval = (tmpbuf[0] << 24) | (tmpbuf[1] << 16) | (tmpbuf[2] << 8) |
1859 out_be32(®s->macstnaddr1, tempval);
1861 tempval = *((uint *) (tmpbuf + 4));
1863 out_be32(®s->macstnaddr2, tempval);
1865 /* reset the indices to zero */
1869 /* Clear out (for the most part) the other registers */
1870 init_registers(regs);
1872 /* Ready the device for tx/rx */
1875 /* If there's no link, fail */
1876 return priv->link ? 0 : -1;
1879 /* Discover which PHY is attached to the device, and configure it
1880 * properly. If the PHY is not recognized, then return 0
1881 * (failure). Otherwise, return 1
1883 static int init_phy(struct eth_device *dev)
1885 struct tsec_private *priv = (struct tsec_private *)dev->priv;
1886 struct phy_info *curphy;
1887 tsec_t *regs = priv->regs;
1889 /* Assign a Physical address to the TBI */
1890 out_be32(®s->tbipa, CONFIG_SYS_TBIPA_VALUE);
1892 /* Reset MII (due to new addresses) */
1893 out_be32(&priv->phyregs->miimcfg, MIIMCFG_RESET);
1894 out_be32(&priv->phyregs->miimcfg, MIIMCFG_INIT_VALUE);
1895 while (in_be32(&priv->phyregs->miimind) & MIIMIND_BUSY)
1898 /* Get the cmd structure corresponding to the attached
1900 curphy = get_phy_info(dev);
1902 if (curphy == NULL) {
1903 priv->phyinfo = NULL;
1904 printf("%s: No PHY found\n", dev->name);
1909 if (in_be32(®s->ecntrl) & ECNTRL_SGMII_MODE)
1910 tsec_configure_serdes(priv);
1912 priv->phyinfo = curphy;
1914 phy_run_commands(priv, priv->phyinfo->config);
1919 /* Initialize device structure. Returns success if PHY
1920 * initialization succeeded (i.e. if it recognizes the PHY)
1922 static int tsec_initialize(bd_t *bis, struct tsec_info_struct *tsec_info)
1924 struct eth_device *dev;
1926 struct tsec_private *priv;
1928 dev = (struct eth_device *)malloc(sizeof *dev);
1933 memset(dev, 0, sizeof *dev);
1935 priv = (struct tsec_private *)malloc(sizeof(*priv));
1940 privlist[num_tsecs++] = priv;
1941 priv->regs = tsec_info->regs;
1942 priv->phyregs = tsec_info->miiregs;
1943 priv->phyregs_sgmii = tsec_info->miiregs_sgmii;
1945 priv->phyaddr = tsec_info->phyaddr;
1946 priv->flags = tsec_info->flags;
1948 sprintf(dev->name, tsec_info->devname);
1951 dev->init = tsec_init;
1952 dev->halt = tsec_halt;
1953 dev->send = tsec_send;
1954 dev->recv = tsec_recv;
1955 #ifdef CONFIG_MCAST_TFTP
1956 dev->mcast = tsec_mcast_addr;
1959 /* Tell u-boot to get the addr from the env */
1960 for (i = 0; i < 6; i++)
1961 dev->enetaddr[i] = 0;
1966 setbits_be32(&priv->regs->maccfg1, MACCFG1_SOFT_RESET);
1967 udelay(2); /* Soft Reset must be asserted for 3 TX clocks */
1968 clrbits_be32(&priv->regs->maccfg1, MACCFG1_SOFT_RESET);
1970 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) \
1971 && !defined(BITBANGMII)
1972 miiphy_register(dev->name, tsec_miiphy_read, tsec_miiphy_write);
1975 /* Try to initialize PHY here, and return */
1976 return init_phy(dev);
1980 * Initialize all the TSEC devices
1982 * Returns the number of TSEC devices that were initialized
1984 int tsec_eth_init(bd_t *bis, struct tsec_info_struct *tsecs, int num)
1989 for (i = 0; i < num; i++) {
1990 ret = tsec_initialize(bis, &tsecs[i]);
1998 int tsec_standard_init(bd_t *bis)
2000 return tsec_eth_init(bis, tsec_info, ARRAY_SIZE(tsec_info));