blob: 84b1bbe54d5cca642bbe729d3a9a5dc1aa02fc69 [file] [log] [blame]
wdenkaffae2b2002-08-17 09:36:01 +00001/*-----------------------------------------------------------------------------+
2 |
Stefan Roesed6c61aa2005-08-16 18:18:00 +02003 | This source code has been made available to you by IBM on an AS-IS
4 | basis. Anyone receiving this source is licensed under IBM
5 | copyrights to use it in any way he or she deems fit, including
6 | copying it, modifying it, compiling it, and redistributing it either
7 | with or without modifications. No license under IBM patents or
8 | patent applications is to be implied by the copyright license.
wdenkaffae2b2002-08-17 09:36:01 +00009 |
Stefan Roesed6c61aa2005-08-16 18:18:00 +020010 | Any user of this software should understand that IBM cannot provide
11 | technical support for this software and will not be responsible for
12 | any consequences resulting from the use of this software.
wdenkaffae2b2002-08-17 09:36:01 +000013 |
Stefan Roesed6c61aa2005-08-16 18:18:00 +020014 | Any person who transfers this source code or any derivative work
15 | must include the IBM copyright notice, this paragraph, and the
16 | preceding two paragraphs in the transferred software.
wdenkaffae2b2002-08-17 09:36:01 +000017 |
Stefan Roesed6c61aa2005-08-16 18:18:00 +020018 | COPYRIGHT I B M CORPORATION 1995
19 | LICENSED MATERIAL - PROGRAM PROPERTY OF I B M
wdenkaffae2b2002-08-17 09:36:01 +000020 +-----------------------------------------------------------------------------*/
21/*-----------------------------------------------------------------------------+
22 |
Stefan Roesed6c61aa2005-08-16 18:18:00 +020023 | File Name: miiphy.c
wdenkaffae2b2002-08-17 09:36:01 +000024 |
Stefan Roesed6c61aa2005-08-16 18:18:00 +020025 | Function: This module has utilities for accessing the MII PHY through
wdenkaffae2b2002-08-17 09:36:01 +000026 | the EMAC3 macro.
27 |
Stefan Roesed6c61aa2005-08-16 18:18:00 +020028 | Author: Mark Wisner
wdenkaffae2b2002-08-17 09:36:01 +000029 |
wdenkaffae2b2002-08-17 09:36:01 +000030 +-----------------------------------------------------------------------------*/
31
Stefan Roesec3307fa2008-02-19 21:58:25 +010032/* define DEBUG for debugging output (obviously ;-)) */
33#if 0
34#define DEBUG
35#endif
36
wdenkaffae2b2002-08-17 09:36:01 +000037#include <common.h>
38#include <asm/processor.h>
Stefan Roese2d834762007-10-23 14:03:17 +020039#include <asm/io.h>
wdenkaffae2b2002-08-17 09:36:01 +000040#include <ppc_asm.tmpl>
41#include <commproc.h>
Stefan Roesed6c61aa2005-08-16 18:18:00 +020042#include <ppc4xx_enet.h>
wdenkaffae2b2002-08-17 09:36:01 +000043#include <405_mal.h>
44#include <miiphy.h>
45
Stefan Roesec3307fa2008-02-19 21:58:25 +010046#if !defined(CONFIG_PHY_CLK_FREQ)
47#define CONFIG_PHY_CLK_FREQ 0
48#endif
49
wdenkaffae2b2002-08-17 09:36:01 +000050/***********************************************************/
Stefan Roesed6c61aa2005-08-16 18:18:00 +020051/* Dump out to the screen PHY regs */
wdenkaffae2b2002-08-17 09:36:01 +000052/***********************************************************/
53
Marian Balakowicz63ff0042005-10-28 22:30:33 +020054void miiphy_dump (char *devname, unsigned char addr)
wdenkaffae2b2002-08-17 09:36:01 +000055{
56 unsigned long i;
57 unsigned short data;
58
wdenkaffae2b2002-08-17 09:36:01 +000059 for (i = 0; i < 0x1A; i++) {
Marian Balakowicz63ff0042005-10-28 22:30:33 +020060 if (miiphy_read (devname, addr, i, &data)) {
wdenkaffae2b2002-08-17 09:36:01 +000061 printf ("read error for reg %lx\n", i);
62 return;
63 }
64 printf ("Phy reg %lx ==> %4x\n", i, data);
65
66 /* jump to the next set of regs */
67 if (i == 0x07)
68 i = 0x0f;
69
Stefan Roesed6c61aa2005-08-16 18:18:00 +020070 } /* end for loop */
71} /* end dump */
wdenkaffae2b2002-08-17 09:36:01 +000072
wdenkaffae2b2002-08-17 09:36:01 +000073/***********************************************************/
Stefan Roesed6c61aa2005-08-16 18:18:00 +020074/* (Re)start autonegotiation */
wdenkaffae2b2002-08-17 09:36:01 +000075/***********************************************************/
Marian Balakowicz63ff0042005-10-28 22:30:33 +020076int phy_setup_aneg (char *devname, unsigned char addr)
wdenkaffae2b2002-08-17 09:36:01 +000077{
Larry Johnsonc3485782007-12-27 10:50:55 -050078 u16 bmcr;
wdenkaffae2b2002-08-17 09:36:01 +000079
Larry Johnsonc3485782007-12-27 10:50:55 -050080#if defined(CONFIG_PHY_DYNAMIC_ANEG)
81 /*
82 * Set up advertisement based on capablilities reported by the PHY.
83 * This should work for both copper and fiber.
84 */
85 u16 bmsr;
86#if defined(CONFIG_PHY_GIGE)
87 u16 exsr = 0x0000;
88#endif
89
90 miiphy_read (devname, addr, PHY_BMSR, &bmsr);
91
92#if defined(CONFIG_PHY_GIGE)
93 if (bmsr & PHY_BMSR_EXT_STAT)
94 miiphy_read (devname, addr, PHY_EXSR, &exsr);
95
96 if (exsr & (PHY_EXSR_1000XF | PHY_EXSR_1000XH)) {
97 /* 1000BASE-X */
98 u16 anar = 0x0000;
99
100 if (exsr & PHY_EXSR_1000XF)
101 anar |= PHY_X_ANLPAR_FD;
102
103 if (exsr & PHY_EXSR_1000XH)
104 anar |= PHY_X_ANLPAR_HD;
105
106 miiphy_write (devname, addr, PHY_ANAR, anar);
107 } else
108#endif
109 {
110 u16 anar, btcr;
111
112 miiphy_read (devname, addr, PHY_ANAR, &anar);
113 anar &= ~(0x5000 | PHY_ANLPAR_T4 | PHY_ANLPAR_TXFD |
114 PHY_ANLPAR_TX | PHY_ANLPAR_10FD | PHY_ANLPAR_10);
115
116 miiphy_read (devname, addr, PHY_1000BTCR, &btcr);
117 btcr &= ~(0x00FF | PHY_1000BTCR_1000FD | PHY_1000BTCR_1000HD);
118
119 if (bmsr & PHY_BMSR_100T4)
120 anar |= PHY_ANLPAR_T4;
121
122 if (bmsr & PHY_BMSR_100TXF)
123 anar |= PHY_ANLPAR_TXFD;
124
125 if (bmsr & PHY_BMSR_100TXH)
126 anar |= PHY_ANLPAR_TX;
127
128 if (bmsr & PHY_BMSR_10TF)
129 anar |= PHY_ANLPAR_10FD;
130
131 if (bmsr & PHY_BMSR_10TH)
132 anar |= PHY_ANLPAR_10;
133
134 miiphy_write (devname, addr, PHY_ANAR, anar);
135
136#if defined(CONFIG_PHY_GIGE)
137 if (exsr & PHY_EXSR_1000TF)
138 btcr |= PHY_1000BTCR_1000FD;
139
140 if (exsr & PHY_EXSR_1000TH)
141 btcr |= PHY_1000BTCR_1000HD;
142
143 miiphy_write (devname, addr, PHY_1000BTCR, btcr);
144#endif
145 }
146
147#else /* defined(CONFIG_PHY_DYNAMIC_ANEG) */
148 /*
149 * Set up standard advertisement
150 */
151 u16 adv;
152
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200153 miiphy_read (devname, addr, PHY_ANAR, &adv);
Mike Nuss74eb0222008-03-03 15:27:05 -0500154 adv |= (PHY_ANLPAR_ACK | PHY_ANLPAR_TXFD | PHY_ANLPAR_TX |
155 PHY_ANLPAR_10FD | PHY_ANLPAR_10);
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200156 miiphy_write (devname, addr, PHY_ANAR, adv);
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200157
Marian Balakowicz6c5879f2006-06-30 16:30:46 +0200158 miiphy_read (devname, addr, PHY_1000BTCR, &adv);
159 adv |= (0x0300);
160 miiphy_write (devname, addr, PHY_1000BTCR, adv);
161
Larry Johnsonc3485782007-12-27 10:50:55 -0500162#endif /* defined(CONFIG_PHY_DYNAMIC_ANEG) */
163
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200164 /* Start/Restart aneg */
Larry Johnsonc3485782007-12-27 10:50:55 -0500165 miiphy_read (devname, addr, PHY_BMCR, &bmcr);
166 bmcr |= (PHY_BMCR_AUTON | PHY_BMCR_RST_NEG);
167 miiphy_write (devname, addr, PHY_BMCR, bmcr);
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200168
169 return 0;
170}
171
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200172/***********************************************************/
173/* read a phy reg and return the value with a rc */
174/***********************************************************/
Stefan Roesec3307fa2008-02-19 21:58:25 +0100175/* AMCC_TODO:
176 * Find out of the choice for the emac for MDIO is from the bridges,
177 * i.e. ZMII or RGMII as approporiate. If the bridges are not used
178 * to determine the emac for MDIO, then is the SDR0_ETH_CFG[MDIO_SEL]
179 * used? If so, then this routine below does not apply to the 460EX/GT.
180 *
181 * sr: Currently on 460EX only EMAC0 works with MDIO, so we always
182 * return EMAC0 offset here
Victor Gallardo78d78232008-09-04 23:49:36 -0700183 * vg: For 460EX/460GT if internal GPCS PHY address is specified
184 * return appropriate EMAC offset
Stefan Roesec3307fa2008-02-19 21:58:25 +0100185 */
Victor Gallardo78d78232008-09-04 23:49:36 -0700186unsigned int miiphy_getemac_offset(u8 addr)
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200187{
Stefan Roesec3307fa2008-02-19 21:58:25 +0100188#if (defined(CONFIG_440) && \
189 !defined(CONFIG_440SP) && !defined(CONFIG_440SPE) && \
190 !defined(CONFIG_460EX) && !defined(CONFIG_460GT)) && \
191 defined(CONFIG_NET_MULTI)
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200192 unsigned long zmii;
193 unsigned long eoffset;
194
195 /* Need to find out which mdi port we're using */
Stefan Roese2d834762007-10-23 14:03:17 +0200196 zmii = in_be32((void *)ZMII_FER);
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200197
Larry Johnsonc3485782007-12-27 10:50:55 -0500198 if (zmii & (ZMII_FER_MDI << ZMII_FER_V (0)))
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200199 /* using port 0 */
200 eoffset = 0;
Larry Johnsonc3485782007-12-27 10:50:55 -0500201
202 else if (zmii & (ZMII_FER_MDI << ZMII_FER_V (1)))
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200203 /* using port 1 */
204 eoffset = 0x100;
Larry Johnsonc3485782007-12-27 10:50:55 -0500205
206 else if (zmii & (ZMII_FER_MDI << ZMII_FER_V (2)))
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200207 /* using port 2 */
208 eoffset = 0x400;
Larry Johnsonc3485782007-12-27 10:50:55 -0500209
210 else if (zmii & (ZMII_FER_MDI << ZMII_FER_V (3)))
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200211 /* using port 3 */
212 eoffset = 0x600;
Larry Johnsonc3485782007-12-27 10:50:55 -0500213
214 else {
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200215 /* None of the mdi ports are enabled! */
216 /* enable port 0 */
217 zmii |= ZMII_FER_MDI << ZMII_FER_V (0);
Stefan Roese2d834762007-10-23 14:03:17 +0200218 out_be32((void *)ZMII_FER, zmii);
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200219 eoffset = 0;
220 /* need to soft reset port 0 */
Stefan Roese2d834762007-10-23 14:03:17 +0200221 zmii = in_be32((void *)EMAC_M0);
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200222 zmii |= EMAC_M0_SRST;
Stefan Roese2d834762007-10-23 14:03:17 +0200223 out_be32((void *)EMAC_M0, zmii);
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200224 }
225
226 return (eoffset);
227#else
Stefan Roesedbbd1252007-10-05 17:10:59 +0200228
229#if defined(CONFIG_NET_MULTI) && defined(CONFIG_405EX)
230 unsigned long rgmii;
231 int devnum = 1;
232
Stefan Roese2d834762007-10-23 14:03:17 +0200233 rgmii = in_be32((void *)RGMII_FER);
Stefan Roesedbbd1252007-10-05 17:10:59 +0200234 if (rgmii & (1 << (19 - devnum)))
235 return 0x100;
236#endif
237
Victor Gallardo78d78232008-09-04 23:49:36 -0700238#if defined(CONFIG_460EX) || defined(CONFIG_460GT)
Victor Gallardo78d78232008-09-04 23:49:36 -0700239 u32 eoffset = 0;
240
241 switch (addr) {
242#if defined(CONFIG_HAS_ETH1) && defined(CONFIG_GPCS_PHY1_ADDR)
243 case CONFIG_GPCS_PHY1_ADDR:
Stefan Roese725b53a2008-09-05 14:09:09 +0200244 if (addr == EMAC_M1_IPPA_GET(in_be32((void *)EMAC_M1 + 0x100)))
Victor Gallardo78d78232008-09-04 23:49:36 -0700245 eoffset = 0x100;
246 break;
247#endif
248#if defined(CONFIG_HAS_ETH2) && defined(CONFIG_GPCS_PHY2_ADDR)
249 case CONFIG_GPCS_PHY2_ADDR:
Stefan Roese725b53a2008-09-05 14:09:09 +0200250 if (addr == EMAC_M1_IPPA_GET(in_be32((void *)EMAC_M1 + 0x300)))
Victor Gallardo78d78232008-09-04 23:49:36 -0700251 eoffset = 0x300;
252 break;
253#endif
254#if defined(CONFIG_HAS_ETH3) && defined(CONFIG_GPCS_PHY3_ADDR)
255 case CONFIG_GPCS_PHY3_ADDR:
Stefan Roese725b53a2008-09-05 14:09:09 +0200256 if (addr == EMAC_M1_IPPA_GET(in_be32((void *)EMAC_M1 + 0x400)))
Victor Gallardo78d78232008-09-04 23:49:36 -0700257 eoffset = 0x400;
258 break;
259#endif
260 default:
261 eoffset = 0;
262 break;
263 }
264 return eoffset;
265#endif
266
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200267 return 0;
268#endif
269}
270
Stefan Roesec3307fa2008-02-19 21:58:25 +0100271static int emac_miiphy_wait(u32 emac_reg)
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200272{
Stefan Roesec3307fa2008-02-19 21:58:25 +0100273 u32 sta_reg;
274 int i;
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200275
Stefan Roesec3307fa2008-02-19 21:58:25 +0100276 /* wait for completion */
wdenkaffae2b2002-08-17 09:36:01 +0000277 i = 0;
Stefan Roesec3307fa2008-02-19 21:58:25 +0100278 do {
279 sta_reg = in_be32((void *)EMAC_STACR + emac_reg);
280 if (i++ > 5) {
281 debug("%s [%d]: Timeout! EMAC_STACR=0x%0x\n", __func__,
282 __LINE__, sta_reg);
wdenkaffae2b2002-08-17 09:36:01 +0000283 return -1;
284 }
Stefan Roesec3307fa2008-02-19 21:58:25 +0100285 udelay(10);
286 } while ((sta_reg & EMAC_STACR_OC) == EMAC_STACR_OC_MASK);
287
288 return 0;
289}
290
291static int emac_miiphy_command(u8 addr, u8 reg, int cmd, u16 value)
292{
293 u32 emac_reg;
294 u32 sta_reg;
295
Victor Gallardo78d78232008-09-04 23:49:36 -0700296 emac_reg = miiphy_getemac_offset(addr);
Stefan Roesec3307fa2008-02-19 21:58:25 +0100297
298 /* wait for completion */
299 if (emac_miiphy_wait(emac_reg) != 0)
300 return -1;
301
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200302 sta_reg = reg; /* reg address */
Stefan Roesec3307fa2008-02-19 21:58:25 +0100303
wdenkaffae2b2002-08-17 09:36:01 +0000304 /* set clock (50Mhz) and read flags */
Stefan Roese887e2ec2006-09-07 11:51:23 +0200305#if defined(CONFIG_440GX) || defined(CONFIG_440SPE) || \
Stefan Roesedbbd1252007-10-05 17:10:59 +0200306 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
Stefan Roesec3307fa2008-02-19 21:58:25 +0100307 defined(CONFIG_460EX) || defined(CONFIG_460GT) || \
Stefan Roesedbbd1252007-10-05 17:10:59 +0200308 defined(CONFIG_405EX)
Larry Johnsonc3485782007-12-27 10:50:55 -0500309#if defined(CONFIG_IBM_EMAC4_V4) /* EMAC4 V4 changed bit setting */
Stefan Roesec3307fa2008-02-19 21:58:25 +0100310 sta_reg = (sta_reg & ~EMAC_STACR_OP_MASK) | cmd;
Marian Balakowicz6c5879f2006-06-30 16:30:46 +0200311#else
Stefan Roesec3307fa2008-02-19 21:58:25 +0100312 sta_reg |= cmd;
Marian Balakowicz6c5879f2006-06-30 16:30:46 +0200313#endif
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200314#else
Stefan Roesec3307fa2008-02-19 21:58:25 +0100315 sta_reg = (sta_reg | cmd) & ~EMAC_STACR_CLK_100MHZ;
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200316#endif
317
Stefan Roesec3307fa2008-02-19 21:58:25 +0100318 /* Some boards (mainly 405EP based) define the PHY clock freqency fixed */
wdenk12f34242003-09-02 22:48:03 +0000319 sta_reg = sta_reg | CONFIG_PHY_CLK_FREQ;
Stefan Roesec3307fa2008-02-19 21:58:25 +0100320 sta_reg = sta_reg | ((u32)addr << 5); /* Phy address */
Marian Balakowicz6c5879f2006-06-30 16:30:46 +0200321 sta_reg = sta_reg | EMAC_STACR_OC_MASK; /* new IBM emac v4 */
Stefan Roesec3307fa2008-02-19 21:58:25 +0100322 if (cmd == EMAC_STACR_WRITE)
323 memcpy(&sta_reg, &value, 2); /* put in data */
324
Stefan Roese2d834762007-10-23 14:03:17 +0200325 out_be32((void *)EMAC_STACR + emac_reg, sta_reg);
Stefan Roesec3307fa2008-02-19 21:58:25 +0100326 debug("%s [%d]: sta_reg=%08x\n", __func__, __LINE__, sta_reg);
wdenkaffae2b2002-08-17 09:36:01 +0000327
Stefan Roesec3307fa2008-02-19 21:58:25 +0100328 /* wait for completion */
329 if (emac_miiphy_wait(emac_reg) != 0)
330 return -1;
Larry Johnsonc3485782007-12-27 10:50:55 -0500331
Stefan Roesec3307fa2008-02-19 21:58:25 +0100332 debug("%s [%d]: sta_reg=%08x\n", __func__, __LINE__, sta_reg);
Larry Johnsonc3485782007-12-27 10:50:55 -0500333 if ((sta_reg & EMAC_STACR_PHYE) != 0)
wdenkaffae2b2002-08-17 09:36:01 +0000334 return -1;
wdenkaffae2b2002-08-17 09:36:01 +0000335
wdenkaffae2b2002-08-17 09:36:01 +0000336 return 0;
Stefan Roesec3307fa2008-02-19 21:58:25 +0100337}
wdenkaffae2b2002-08-17 09:36:01 +0000338
Stefan Roesec3307fa2008-02-19 21:58:25 +0100339int emac4xx_miiphy_read (char *devname, unsigned char addr, unsigned char reg,
340 unsigned short *value)
341{
342 unsigned long sta_reg;
343 unsigned long emac_reg;
344
Victor Gallardo78d78232008-09-04 23:49:36 -0700345 emac_reg = miiphy_getemac_offset(addr);
Stefan Roesec3307fa2008-02-19 21:58:25 +0100346
347 if (emac_miiphy_command(addr, reg, EMAC_STACR_READ, 0) != 0)
348 return -1;
349
350 sta_reg = in_be32((void *)EMAC_STACR + emac_reg);
351 *value = *(u16 *)(&sta_reg);
352
353 return 0;
354}
wdenkaffae2b2002-08-17 09:36:01 +0000355
wdenkaffae2b2002-08-17 09:36:01 +0000356/***********************************************************/
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200357/* write a phy reg and return the value with a rc */
wdenkaffae2b2002-08-17 09:36:01 +0000358/***********************************************************/
359
Larry Johnsonc3485782007-12-27 10:50:55 -0500360int emac4xx_miiphy_write (char *devname, unsigned char addr, unsigned char reg,
361 unsigned short value)
wdenkaffae2b2002-08-17 09:36:01 +0000362{
Stefan Roesec3307fa2008-02-19 21:58:25 +0100363 return emac_miiphy_command(addr, reg, EMAC_STACR_WRITE, value);
364}