3 * Freescale Three Speed Ethernet Controller driver
5 * This software may be used and distributed according to the
6 * terms of the GNU Public License, Version 2, incorporated
9 * Copyright 2004 Freescale Semiconductor.
10 * (C) Copyright 2003, Motorola, Inc.
22 #if defined(CONFIG_TSEC_ENET)
26 DECLARE_GLOBAL_DATA_PTR;
30 static uint rxIdx; /* index of the current RX buffer */
31 static uint txIdx; /* index of the current TX buffer */
33 typedef volatile struct rtxbd {
34 txbd8_t txbd[TX_BUF_CNT];
35 rxbd8_t rxbd[PKTBUFSRX];
38 struct tsec_info_struct {
41 unsigned int phyregidx;
45 /* The tsec_info structure contains 3 values which the
46 * driver uses to determine how to operate a given ethernet
47 * device. For now, the structure is initialized with the
48 * knowledge that all current implementations have 2 TSEC
49 * devices, and one FEC. The information needed is:
50 * phyaddr - The address of the PHY which is attached to
53 * flags - This variable indicates whether the device
54 * supports gigabit speed ethernet, and whether it should be
57 * phyregidx - This variable specifies which ethernet device
58 * controls the MII Management registers which are connected
59 * to the PHY. For 8540/8560, only TSEC1 (index 0) has
60 * access to the PHYs, so all of the entries have "0".
62 * The values specified in the table are taken from the board's
63 * config file in include/configs/. When implementing a new
64 * board with ethernet capability, it is necessary to define:
74 static struct tsec_info_struct tsec_info[] = {
75 #if defined(CONFIG_MPC85XX_TSEC1) || defined(CONFIG_MPC83XX_TSEC1)
76 {TSEC1_PHY_ADDR, TSEC_GIGABIT, TSEC1_PHYIDX},
80 #if defined(CONFIG_MPC85XX_TSEC2) || defined(CONFIG_MPC83XX_TSEC2)
81 {TSEC2_PHY_ADDR, TSEC_GIGABIT, TSEC2_PHYIDX},
85 #ifdef CONFIG_MPC85XX_FEC
86 {FEC_PHY_ADDR, 0, FEC_PHYIDX},
88 # if defined(CONFIG_MPC85XX_TSEC3) || defined(CONFIG_MPC83XX_TSEC3)
89 {TSEC3_PHY_ADDR, TSEC_GIGABIT | TSEC_REDUCED, TSEC3_PHYIDX},
93 # if defined(CONFIG_MPC85XX_TSEC4) || defined(CONFIG_MPC83XX_TSEC4)
94 {TSEC4_PHY_ADDR, TSEC_REDUCED, TSEC4_PHYIDX},
101 #define MAXCONTROLLERS (4)
103 static int relocated = 0;
105 static struct tsec_private *privlist[MAXCONTROLLERS];
108 static RTXBD rtx __attribute__ ((aligned(8)));
110 #error "rtx must be 64-bit aligned"
113 static int tsec_send(struct eth_device* dev, volatile void *packet, int length);
114 static int tsec_recv(struct eth_device* dev);
115 static int tsec_init(struct eth_device* dev, bd_t * bd);
116 static void tsec_halt(struct eth_device* dev);
117 static void init_registers(volatile tsec_t *regs);
118 static void startup_tsec(struct eth_device *dev);
119 static int init_phy(struct eth_device *dev);
120 void write_phy_reg(struct tsec_private *priv, uint regnum, uint value);
121 uint read_phy_reg(struct tsec_private *priv, uint regnum);
122 struct phy_info * get_phy_info(struct eth_device *dev);
123 void phy_run_commands(struct tsec_private *priv, struct phy_cmd *cmd);
124 static void adjust_link(struct eth_device *dev);
125 static void relocate_cmds(void);
126 static int tsec_miiphy_write(char *devname, unsigned char addr,
127 unsigned char reg, unsigned short value);
128 static int tsec_miiphy_read(char *devname, unsigned char addr,
129 unsigned char reg, unsigned short *value);
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*TSEC_SIZE);
157 priv->phyaddr = tsec_info[index].phyaddr;
158 priv->flags = tsec_info[index].flags;
160 sprintf(dev->name, devname);
163 dev->init = tsec_init;
164 dev->halt = tsec_halt;
165 dev->send = tsec_send;
166 dev->recv = tsec_recv;
168 /* Tell u-boot to get the addr from the env */
170 dev->enetaddr[i] = 0;
176 priv->regs->maccfg1 |= MACCFG1_SOFT_RESET;
177 priv->regs->maccfg1 &= ~(MACCFG1_SOFT_RESET);
179 #if defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_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);
189 /* Initializes data structures and registers for the controller,
190 * and brings the interface up. Returns the link status, meaning
191 * that it returns success if the link is up, failure otherwise.
192 * 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 */
237 /* Write value to the device's PHY through the registers
238 * specified in priv, modifying the register specified in regnum.
239 * It will wait for the write to be done (or for a timeout to
240 * expire) before exiting
242 void write_phy_reg(struct tsec_private *priv, uint regnum, uint value)
244 volatile tsec_t *regbase = priv->phyregs;
245 uint phyid = priv->phyaddr;
248 regbase->miimadd = (phyid << 8) | regnum;
249 regbase->miimcon = value;
253 while((regbase->miimind & MIIMIND_BUSY) && timeout--);
257 /* Reads register regnum on the device's PHY through the
258 * registers specified in priv. It lowers and raises the read
259 * command, and waits for the data to become valid (miimind
260 * notvalid bit cleared), and the bus to cease activity (miimind
261 * busy bit cleared), and then returns the value
263 uint read_phy_reg(struct tsec_private *priv, uint regnum)
266 volatile tsec_t *regbase = priv->phyregs;
267 uint phyid = priv->phyaddr;
269 /* Put the address of the phy, and the register
270 * number into MIIMADD */
271 regbase->miimadd = (phyid << 8) | regnum;
273 /* Clear the command register, and wait */
274 regbase->miimcom = 0;
277 /* Initiate a read command, and wait */
278 regbase->miimcom = MIIM_READ_COMMAND;
281 /* Wait for the the indication that the read is done */
282 while((regbase->miimind & (MIIMIND_NOTVALID | MIIMIND_BUSY)));
284 /* Grab the value read from the PHY */
285 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;
300 /* Assign a Physical address to the TBI */
303 volatile tsec_t *regs = (volatile tsec_t *)(TSEC_BASE_ADDR);
304 regs->tbipa = TBIPA_VALUE;
305 regs = (volatile tsec_t *)(TSEC_BASE_ADDR + TSEC_SIZE);
306 regs->tbipa = TBIPA_VALUE;
310 /* Reset MII (due to new addresses) */
311 priv->phyregs->miimcfg = MIIMCFG_RESET;
313 priv->phyregs->miimcfg = MIIMCFG_INIT_VALUE;
315 while(priv->phyregs->miimind & MIIMIND_BUSY);
320 /* Get the cmd structure corresponding to the attached
322 curphy = get_phy_info(dev);
325 printf("%s: No PHY found\n", dev->name);
330 priv->phyinfo = curphy;
332 phy_run_commands(priv, priv->phyinfo->config);
338 /* Returns which value to write to the control register. */
339 /* For 10/100, the value is slightly different */
340 uint mii_cr_init(uint mii_reg, struct tsec_private *priv)
342 if(priv->flags & TSEC_GIGABIT)
343 return MIIM_CONTROL_INIT;
349 /* Parse the status register for link, and then do
350 * auto-negotiation */
351 uint mii_parse_sr(uint mii_reg, struct tsec_private *priv)
354 * Wait if PHY is capable of autonegotiation and autonegotiation is not complete
356 mii_reg = read_phy_reg(priv, MIIM_STATUS);
357 if ((mii_reg & PHY_BMSR_AUTN_ABLE) && !(mii_reg & PHY_BMSR_AUTN_COMP)) {
360 puts ("Waiting for PHY auto negotiation to complete");
361 while (!((mii_reg & PHY_BMSR_AUTN_COMP) && (mii_reg & MIIM_STATUS_LINK))) {
365 if (i > PHY_AUTONEGOTIATE_TIMEOUT) {
366 puts (" TIMEOUT !\n");
371 if ((i++ % 1000) == 0) {
374 udelay (1000); /* 1 ms */
375 mii_reg = read_phy_reg(priv, MIIM_STATUS);
379 udelay (500000); /* another 500 ms (results in faster booting) */
388 /* Parse the 88E1011's status register for speed and duplex
390 uint mii_parse_88E1011_psr(uint mii_reg, struct tsec_private *priv)
394 mii_reg = read_phy_reg(priv, MIIM_88E1011_PHY_STATUS);
396 if (!((mii_reg & MIIM_88E1011_PHYSTAT_SPDDONE) &&
397 (mii_reg & MIIM_88E1011_PHYSTAT_LINK))) {
400 puts ("Waiting for PHY realtime link");
401 while (!((mii_reg & MIIM_88E1011_PHYSTAT_SPDDONE) &&
402 (mii_reg & MIIM_88E1011_PHYSTAT_LINK))) {
406 if (i > PHY_AUTONEGOTIATE_TIMEOUT) {
407 puts (" TIMEOUT !\n");
412 if ((i++ % 1000) == 0) {
415 udelay (1000); /* 1 ms */
416 mii_reg = read_phy_reg(priv, MIIM_88E1011_PHY_STATUS);
419 udelay (500000); /* another 500 ms (results in faster booting) */
422 if(mii_reg & MIIM_88E1011_PHYSTAT_DUPLEX)
427 speed = (mii_reg &MIIM_88E1011_PHYSTAT_SPEED);
430 case MIIM_88E1011_PHYSTAT_GBIT:
433 case MIIM_88E1011_PHYSTAT_100:
444 /* Parse the cis8201's status register for speed and duplex
446 uint mii_parse_cis8201(uint mii_reg, struct tsec_private *priv)
450 if(mii_reg & MIIM_CIS8201_AUXCONSTAT_DUPLEX)
455 speed = mii_reg & MIIM_CIS8201_AUXCONSTAT_SPEED;
457 case MIIM_CIS8201_AUXCONSTAT_GBIT:
460 case MIIM_CIS8201_AUXCONSTAT_100:
472 /* Parse the DM9161's status register for speed and duplex
474 uint mii_parse_dm9161_scsr(uint mii_reg, struct tsec_private *priv)
476 if(mii_reg & (MIIM_DM9161_SCSR_100F | MIIM_DM9161_SCSR_100H))
481 if(mii_reg & (MIIM_DM9161_SCSR_100F | MIIM_DM9161_SCSR_10F))
490 /* Hack to write all 4 PHYs with the LED values */
491 uint mii_cis8204_fixled(uint mii_reg, struct tsec_private *priv)
494 volatile tsec_t *regbase = priv->phyregs;
497 for(phyid=0;phyid<4;phyid++) {
498 regbase->miimadd = (phyid << 8) | mii_reg;
499 regbase->miimcon = MIIM_CIS8204_SLEDCON_INIT;
503 while((regbase->miimind & MIIMIND_BUSY) && timeout--);
506 return MIIM_CIS8204_SLEDCON_INIT;
509 uint mii_cis8204_setmode(uint mii_reg, struct tsec_private *priv)
511 if (priv->flags & TSEC_REDUCED)
512 return MIIM_CIS8204_EPHYCON_INIT | MIIM_CIS8204_EPHYCON_RGMII;
514 return MIIM_CIS8204_EPHYCON_INIT;
517 /* Initialized required registers to appropriate values, zeroing
518 * those we don't care about (unless zero is bad, in which case,
519 * choose a more appropriate value) */
520 static void init_registers(volatile tsec_t *regs)
523 regs->ievent = IEVENT_INIT_CLEAR;
525 regs->imask = IMASK_INIT_CLEAR;
527 regs->hash.iaddr0 = 0;
528 regs->hash.iaddr1 = 0;
529 regs->hash.iaddr2 = 0;
530 regs->hash.iaddr3 = 0;
531 regs->hash.iaddr4 = 0;
532 regs->hash.iaddr5 = 0;
533 regs->hash.iaddr6 = 0;
534 regs->hash.iaddr7 = 0;
536 regs->hash.gaddr0 = 0;
537 regs->hash.gaddr1 = 0;
538 regs->hash.gaddr2 = 0;
539 regs->hash.gaddr3 = 0;
540 regs->hash.gaddr4 = 0;
541 regs->hash.gaddr5 = 0;
542 regs->hash.gaddr6 = 0;
543 regs->hash.gaddr7 = 0;
545 regs->rctrl = 0x00000000;
547 /* Init RMON mib registers */
548 memset((void *)&(regs->rmon), 0, sizeof(rmon_mib_t));
550 regs->rmon.cam1 = 0xffffffff;
551 regs->rmon.cam2 = 0xffffffff;
553 regs->mrblr = MRBLR_INIT_SETTINGS;
555 regs->minflr = MINFLR_INIT_SETTINGS;
557 regs->attr = ATTR_INIT_SETTINGS;
558 regs->attreli = ATTRELI_INIT_SETTINGS;
563 /* Configure maccfg2 based on negotiated speed and duplex
564 * reported by PHY handling code */
565 static void adjust_link(struct eth_device *dev)
567 struct tsec_private *priv = (struct tsec_private *)dev->priv;
568 volatile tsec_t *regs = priv->regs;
571 if(priv->duplexity != 0)
572 regs->maccfg2 |= MACCFG2_FULL_DUPLEX;
574 regs->maccfg2 &= ~(MACCFG2_FULL_DUPLEX);
576 switch(priv->speed) {
578 regs->maccfg2 = ((regs->maccfg2&~(MACCFG2_IF))
583 regs->maccfg2 = ((regs->maccfg2&~(MACCFG2_IF))
586 /* If We're in reduced mode, we need
587 * to say whether we're 10 or 100 MB.
589 if ((priv->speed == 100)
590 && (priv->flags & TSEC_REDUCED))
591 regs->ecntrl |= ECNTRL_R100;
593 regs->ecntrl &= ~(ECNTRL_R100);
596 printf("%s: Speed was bad\n", dev->name);
600 printf("Speed: %d, %s duplex\n", priv->speed,
601 (priv->duplexity) ? "full" : "half");
604 printf("%s: No link.\n", dev->name);
609 /* Set up the buffers and their descriptors, and bring up the
611 static void startup_tsec(struct eth_device *dev)
614 struct tsec_private *priv = (struct tsec_private *)dev->priv;
615 volatile tsec_t *regs = priv->regs;
617 /* Point to the buffer descriptors */
618 regs->tbase = (unsigned int)(&rtx.txbd[txIdx]);
619 regs->rbase = (unsigned int)(&rtx.rxbd[rxIdx]);
621 /* Initialize the Rx Buffer descriptors */
622 for (i = 0; i < PKTBUFSRX; i++) {
623 rtx.rxbd[i].status = RXBD_EMPTY;
624 rtx.rxbd[i].length = 0;
625 rtx.rxbd[i].bufPtr = (uint)NetRxPackets[i];
627 rtx.rxbd[PKTBUFSRX -1].status |= RXBD_WRAP;
629 /* Initialize the TX Buffer Descriptors */
630 for(i=0; i<TX_BUF_CNT; i++) {
631 rtx.txbd[i].status = 0;
632 rtx.txbd[i].length = 0;
633 rtx.txbd[i].bufPtr = 0;
635 rtx.txbd[TX_BUF_CNT -1].status |= TXBD_WRAP;
637 /* Start up the PHY */
638 phy_run_commands(priv, priv->phyinfo->startup);
641 /* Enable Transmit and Receive */
642 regs->maccfg1 |= (MACCFG1_RX_EN | MACCFG1_TX_EN);
644 /* Tell the DMA it is clear to go */
645 regs->dmactrl |= DMACTRL_INIT_SETTINGS;
646 regs->tstat = TSTAT_CLEAR_THALT;
647 regs->dmactrl &= ~(DMACTRL_GRS | DMACTRL_GTS);
650 /* This returns the status bits of the device. The return value
651 * is never checked, and this is what the 8260 driver did, so we
652 * do the same. Presumably, this would be zero if there were no
654 static int tsec_send(struct eth_device* dev, volatile void *packet, int length)
658 struct tsec_private *priv = (struct tsec_private *)dev->priv;
659 volatile tsec_t *regs = priv->regs;
661 /* Find an empty buffer descriptor */
662 for(i=0; rtx.txbd[txIdx].status & TXBD_READY; i++) {
663 if (i >= TOUT_LOOP) {
664 debug ("%s: tsec: tx buffers full\n", dev->name);
669 rtx.txbd[txIdx].bufPtr = (uint)packet;
670 rtx.txbd[txIdx].length = length;
671 rtx.txbd[txIdx].status |= (TXBD_READY | TXBD_LAST | TXBD_CRC | TXBD_INTERRUPT);
673 /* Tell the DMA to go */
674 regs->tstat = TSTAT_CLEAR_THALT;
676 /* Wait for buffer to be transmitted */
677 for(i=0; rtx.txbd[txIdx].status & TXBD_READY; i++) {
678 if (i >= TOUT_LOOP) {
679 debug ("%s: tsec: tx error\n", dev->name);
684 txIdx = (txIdx + 1) % TX_BUF_CNT;
685 result = rtx.txbd[txIdx].status & TXBD_STATS;
690 static int tsec_recv(struct eth_device* dev)
693 struct tsec_private *priv = (struct tsec_private *)dev->priv;
694 volatile tsec_t *regs = priv->regs;
696 while(!(rtx.rxbd[rxIdx].status & RXBD_EMPTY)) {
698 length = rtx.rxbd[rxIdx].length;
700 /* Send the packet up if there were no errors */
701 if (!(rtx.rxbd[rxIdx].status & RXBD_STATS)) {
702 NetReceive(NetRxPackets[rxIdx], length - 4);
704 printf("Got error %x\n",
705 (rtx.rxbd[rxIdx].status & RXBD_STATS));
708 rtx.rxbd[rxIdx].length = 0;
710 /* Set the wrap bit if this is the last element in the list */
711 rtx.rxbd[rxIdx].status = RXBD_EMPTY | (((rxIdx + 1) == PKTBUFSRX) ? RXBD_WRAP : 0);
713 rxIdx = (rxIdx + 1) % PKTBUFSRX;
716 if(regs->ievent&IEVENT_BSY) {
717 regs->ievent = IEVENT_BSY;
718 regs->rstat = RSTAT_CLEAR_RHALT;
726 /* Stop the interface */
727 static void tsec_halt(struct eth_device* dev)
729 struct tsec_private *priv = (struct tsec_private *)dev->priv;
730 volatile tsec_t *regs = priv->regs;
732 regs->dmactrl &= ~(DMACTRL_GRS | DMACTRL_GTS);
733 regs->dmactrl |= (DMACTRL_GRS | DMACTRL_GTS);
735 while(!(regs->ievent & (IEVENT_GRSC | IEVENT_GTSC)));
737 regs->maccfg1 &= ~(MACCFG1_TX_EN | MACCFG1_RX_EN);
739 /* Shut down the PHY, as needed */
740 phy_run_commands(priv, priv->phyinfo->shutdown);
744 struct phy_info phy_info_M88E1011S = {
748 (struct phy_cmd[]) { /* config */
749 /* Reset and configure the PHY */
750 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
752 {0x1e, 0x200c, NULL},
756 {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
757 {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
758 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
759 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
762 (struct phy_cmd[]) { /* startup */
763 /* Status is read once to clear old link state */
764 {MIIM_STATUS, miim_read, NULL},
766 {MIIM_STATUS, miim_read, &mii_parse_sr},
767 /* Read the status */
768 {MIIM_88E1011_PHY_STATUS, miim_read, &mii_parse_88E1011_psr},
771 (struct phy_cmd[]) { /* shutdown */
776 struct phy_info phy_info_M88E1111S = {
780 (struct phy_cmd[]) { /* config */
781 /* Reset and configure the PHY */
782 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
784 {0x1e, 0x200c, NULL},
788 {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
789 {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
790 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
791 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
794 (struct phy_cmd[]) { /* startup */
795 /* Status is read once to clear old link state */
796 {MIIM_STATUS, miim_read, NULL},
798 {MIIM_STATUS, miim_read, &mii_parse_sr},
799 /* Read the status */
800 {MIIM_88E1011_PHY_STATUS, miim_read, &mii_parse_88E1011_psr},
803 (struct phy_cmd[]) { /* shutdown */
808 struct phy_info phy_info_cis8204 = {
812 (struct phy_cmd[]) { /* config */
813 /* Override PHY config settings */
814 {MIIM_CIS8201_AUX_CONSTAT, MIIM_CIS8201_AUXCONSTAT_INIT, NULL},
815 /* Configure some basic stuff */
816 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
817 {MIIM_CIS8204_SLED_CON, MIIM_CIS8204_SLEDCON_INIT, &mii_cis8204_fixled},
818 {MIIM_CIS8204_EPHY_CON, MIIM_CIS8204_EPHYCON_INIT, &mii_cis8204_setmode},
821 (struct phy_cmd[]) { /* startup */
822 /* Read the Status (2x to make sure link is right) */
823 {MIIM_STATUS, miim_read, NULL},
825 {MIIM_STATUS, miim_read, &mii_parse_sr},
826 /* Read the status */
827 {MIIM_CIS8201_AUX_CONSTAT, miim_read, &mii_parse_cis8201},
830 (struct phy_cmd[]) { /* shutdown */
836 struct phy_info phy_info_cis8201 = {
840 (struct phy_cmd[]) { /* config */
841 /* Override PHY config settings */
842 {MIIM_CIS8201_AUX_CONSTAT, MIIM_CIS8201_AUXCONSTAT_INIT, NULL},
843 /* Set up the interface mode */
844 {MIIM_CIS8201_EXT_CON1, MIIM_CIS8201_EXTCON1_INIT, NULL},
845 /* Configure some basic stuff */
846 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
849 (struct phy_cmd[]) { /* startup */
850 /* Read the Status (2x to make sure link is right) */
851 {MIIM_STATUS, miim_read, NULL},
853 {MIIM_STATUS, miim_read, &mii_parse_sr},
854 /* Read the status */
855 {MIIM_CIS8201_AUX_CONSTAT, miim_read, &mii_parse_cis8201},
858 (struct phy_cmd[]) { /* shutdown */
864 struct phy_info phy_info_dm9161 = {
868 (struct phy_cmd[]) { /* config */
869 {MIIM_CONTROL, MIIM_DM9161_CR_STOP, NULL},
870 /* Do not bypass the scrambler/descrambler */
871 {MIIM_DM9161_SCR, MIIM_DM9161_SCR_INIT, NULL},
872 /* Clear 10BTCSR to default */
873 {MIIM_DM9161_10BTCSR, MIIM_DM9161_10BTCSR_INIT, NULL},
874 /* Configure some basic stuff */
875 {MIIM_CONTROL, MIIM_CR_INIT, NULL},
876 /* Restart Auto Negotiation */
877 {MIIM_CONTROL, MIIM_DM9161_CR_RSTAN, NULL},
880 (struct phy_cmd[]) { /* startup */
881 /* Status is read once to clear old link state */
882 {MIIM_STATUS, miim_read, NULL},
884 {MIIM_STATUS, miim_read, &mii_parse_sr},
885 /* Read the status */
886 {MIIM_DM9161_SCSR, miim_read, &mii_parse_dm9161_scsr},
889 (struct phy_cmd[]) { /* shutdown */
894 uint mii_parse_lxt971_sr2(uint mii_reg, struct tsec_private *priv)
898 speed = mii_reg & MIIM_LXT971_SR2_SPEED_MASK;
901 case MIIM_LXT971_SR2_10HDX:
905 case MIIM_LXT971_SR2_10FDX:
909 case MIIM_LXT971_SR2_100HDX:
925 static struct phy_info phy_info_lxt971 = {
929 (struct phy_cmd []) { /* config */
930 { MIIM_CR, MIIM_CR_INIT, mii_cr_init }, /* autonegotiate */
933 (struct phy_cmd []) { /* startup - enable interrupts */
934 /* { 0x12, 0x00f2, NULL }, */
935 { MIIM_STATUS, miim_read, NULL },
936 { MIIM_STATUS, miim_read, &mii_parse_sr },
937 { MIIM_LXT971_SR2, miim_read, &mii_parse_lxt971_sr2 },
940 (struct phy_cmd []) { /* shutdown - disable interrupts */
945 /* Parse the DP83865's link and auto-neg status register for speed and duplex
947 uint mii_parse_dp83865_lanr(uint mii_reg, struct tsec_private *priv)
949 switch (mii_reg & MIIM_DP83865_SPD_MASK) {
951 case MIIM_DP83865_SPD_1000:
955 case MIIM_DP83865_SPD_100:
965 if (mii_reg & MIIM_DP83865_DPX_FULL)
973 struct phy_info phy_info_dp83865 = {
977 (struct phy_cmd[]) { /* config */
978 {MIIM_CONTROL, MIIM_DP83865_CR_INIT, NULL},
981 (struct phy_cmd[]) { /* startup */
982 /* Status is read once to clear old link state */
983 {MIIM_STATUS, miim_read, NULL},
985 {MIIM_STATUS, miim_read, &mii_parse_sr},
986 /* Read the link and auto-neg status */
987 {MIIM_DP83865_LANR, miim_read, &mii_parse_dp83865_lanr},
990 (struct phy_cmd[]) { /* shutdown */
995 struct phy_info *phy_info[] = {
1000 &phy_info_M88E1011S,
1001 &phy_info_M88E1111S,
1009 /* Grab the identifier of the device's PHY, and search through
1010 * all of the known PHYs to see if one matches. If so, return
1011 * it, if not, return NULL */
1012 struct phy_info * get_phy_info(struct eth_device *dev)
1014 struct tsec_private *priv = (struct tsec_private *)dev->priv;
1015 uint phy_reg, phy_ID;
1017 struct phy_info *theInfo = NULL;
1019 /* Grab the bits from PHYIR1, and put them in the upper half */
1020 phy_reg = read_phy_reg(priv, MIIM_PHYIR1);
1021 phy_ID = (phy_reg & 0xffff) << 16;
1023 /* Grab the bits from PHYIR2, and put them in the lower half */
1024 phy_reg = read_phy_reg(priv, MIIM_PHYIR2);
1025 phy_ID |= (phy_reg & 0xffff);
1027 /* loop through all the known PHY types, and find one that */
1028 /* matches the ID we read from the PHY. */
1029 for(i=0; phy_info[i]; i++) {
1030 if(phy_info[i]->id == (phy_ID >> phy_info[i]->shift))
1031 theInfo = phy_info[i];
1036 printf("%s: PHY id %x is not supported!\n", dev->name, phy_ID);
1039 debug("%s: PHY is %s (%x)\n", dev->name, theInfo->name, phy_ID);
1046 /* Execute the given series of commands on the given device's
1047 * PHY, running functions as necessary*/
1048 void phy_run_commands(struct tsec_private *priv, struct phy_cmd *cmd)
1052 volatile tsec_t *phyregs = priv->phyregs;
1054 phyregs->miimcfg = MIIMCFG_RESET;
1056 phyregs->miimcfg = MIIMCFG_INIT_VALUE;
1058 while(phyregs->miimind & MIIMIND_BUSY);
1060 for(i=0;cmd->mii_reg != miim_end;i++) {
1061 if(cmd->mii_data == miim_read) {
1062 result = read_phy_reg(priv, cmd->mii_reg);
1064 if(cmd->funct != NULL)
1065 (*(cmd->funct))(result, priv);
1068 if(cmd->funct != NULL)
1069 result = (*(cmd->funct))(cmd->mii_reg, priv);
1071 result = cmd->mii_data;
1073 write_phy_reg(priv, cmd->mii_reg, result);
1081 /* Relocate the function pointers in the phy cmd lists */
1082 static void relocate_cmds(void)
1084 struct phy_cmd **cmdlistptr;
1085 struct phy_cmd *cmd;
1088 for(i=0; phy_info[i]; i++) {
1089 /* First thing's first: relocate the pointers to the
1090 * PHY command structures (the structs were done) */
1091 phy_info[i] = (struct phy_info *) ((uint)phy_info[i]
1093 phy_info[i]->name += gd->reloc_off;
1094 phy_info[i]->config =
1095 (struct phy_cmd *)((uint)phy_info[i]->config
1097 phy_info[i]->startup =
1098 (struct phy_cmd *)((uint)phy_info[i]->startup
1100 phy_info[i]->shutdown =
1101 (struct phy_cmd *)((uint)phy_info[i]->shutdown
1104 cmdlistptr = &phy_info[i]->config;
1106 for(;cmdlistptr <= &phy_info[i]->shutdown;cmdlistptr++) {
1108 for(cmd=*cmdlistptr;cmd->mii_reg != miim_end;cmd++) {
1109 /* Only relocate non-NULL pointers */
1111 cmd->funct += gd->reloc_off;
1123 #if defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII) \
1124 && !defined(BITBANGMII)
1126 struct tsec_private * get_priv_for_phy(unsigned char phyaddr)
1130 for(i=0;i<MAXCONTROLLERS;i++) {
1131 if(privlist[i]->phyaddr == phyaddr)
1139 * Read a MII PHY register.
1144 static int tsec_miiphy_read(char *devname, unsigned char addr,
1145 unsigned char reg, unsigned short *value)
1148 struct tsec_private *priv = get_priv_for_phy(addr);
1151 printf("Can't read PHY at address %d\n", addr);
1155 ret = (unsigned short)read_phy_reg(priv, reg);
1162 * Write a MII PHY register.
1167 static int tsec_miiphy_write(char *devname, unsigned char addr,
1168 unsigned char reg, unsigned short value)
1170 struct tsec_private *priv = get_priv_for_phy(addr);
1173 printf("Can't write PHY at address %d\n", addr);
1177 write_phy_reg(priv, reg, value);
1182 #endif /* defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII)
1183 && !defined(BITBANGMII) */
1185 #endif /* CONFIG_TSEC_ENET */