blob: 98ba0a7b356d8e95da9dcc52aab0a456a96604b8 [file] [log] [blame]
wdenkaffae2b2002-08-17 09:36:01 +00001/*-----------------------------------------------------------------------------+
2 |
Stefan Roese0c7ffc02005-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 Roese0c7ffc02005-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 Roese0c7ffc02005-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 Roese0c7ffc02005-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 Roese0c7ffc02005-08-16 18:18:00 +020023 | File Name: miiphy.c
wdenkaffae2b2002-08-17 09:36:01 +000024 |
Stefan Roese0c7ffc02005-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 Roese0c7ffc02005-08-16 18:18:00 +020028 | Author: Mark Wisner
wdenkaffae2b2002-08-17 09:36:01 +000029 |
30 | Change Activity-
31 |
Stefan Roese0c7ffc02005-08-16 18:18:00 +020032 | Date Description of Change BY
33 | --------- --------------------- ---
34 | 05-May-99 Created MKW
35 | 01-Jul-99 Changed clock setting of sta_reg from 66Mhz to 50Mhz to
36 | better match OPB speed. Also modified delay times. JWB
37 | 29-Jul-99 Added Full duplex support MKW
38 | 24-Aug-99 Removed printf from dp83843_duplex() JWB
39 | 19-Jul-00 Ported to esd cpci405 sr
40 | 23-Dec-03 Ported from miiphy.c to 440GX Travis Sawyer TBS
41 | <travis.sawyer@sandburst.com>
wdenkaffae2b2002-08-17 09:36:01 +000042 |
43 +-----------------------------------------------------------------------------*/
44
45#include <common.h>
46#include <asm/processor.h>
Stefan Roese697100952007-10-23 14:03:17 +020047#include <asm/io.h>
wdenkaffae2b2002-08-17 09:36:01 +000048#include <ppc_asm.tmpl>
49#include <commproc.h>
Stefan Roese0c7ffc02005-08-16 18:18:00 +020050#include <ppc4xx_enet.h>
wdenkaffae2b2002-08-17 09:36:01 +000051#include <405_mal.h>
52#include <miiphy.h>
53
Marian Balakowicz49d0eee2006-06-30 16:30:46 +020054#undef ET_DEBUG
wdenkaffae2b2002-08-17 09:36:01 +000055/***********************************************************/
Stefan Roese0c7ffc02005-08-16 18:18:00 +020056/* Dump out to the screen PHY regs */
wdenkaffae2b2002-08-17 09:36:01 +000057/***********************************************************/
58
Marian Balakowiczaab8c492005-10-28 22:30:33 +020059void miiphy_dump (char *devname, unsigned char addr)
wdenkaffae2b2002-08-17 09:36:01 +000060{
61 unsigned long i;
62 unsigned short data;
63
64
65 for (i = 0; i < 0x1A; i++) {
Marian Balakowiczaab8c492005-10-28 22:30:33 +020066 if (miiphy_read (devname, addr, i, &data)) {
wdenkaffae2b2002-08-17 09:36:01 +000067 printf ("read error for reg %lx\n", i);
68 return;
69 }
70 printf ("Phy reg %lx ==> %4x\n", i, data);
71
72 /* jump to the next set of regs */
73 if (i == 0x07)
74 i = 0x0f;
75
Stefan Roese0c7ffc02005-08-16 18:18:00 +020076 } /* end for loop */
77} /* end dump */
wdenkaffae2b2002-08-17 09:36:01 +000078
79
wdenkaffae2b2002-08-17 09:36:01 +000080/***********************************************************/
Stefan Roese0c7ffc02005-08-16 18:18:00 +020081/* (Re)start autonegotiation */
wdenkaffae2b2002-08-17 09:36:01 +000082/***********************************************************/
Marian Balakowiczaab8c492005-10-28 22:30:33 +020083int phy_setup_aneg (char *devname, unsigned char addr)
Stefan Roese0c7ffc02005-08-16 18:18:00 +020084{
85 unsigned short ctl, adv;
86
87 /* Setup standard advertise */
Marian Balakowiczaab8c492005-10-28 22:30:33 +020088 miiphy_read (devname, addr, PHY_ANAR, &adv);
Stefan Roese0c7ffc02005-08-16 18:18:00 +020089 adv |= (PHY_ANLPAR_ACK | PHY_ANLPAR_RF | PHY_ANLPAR_T4 |
90 PHY_ANLPAR_TXFD | PHY_ANLPAR_TX | PHY_ANLPAR_10FD |
91 PHY_ANLPAR_10);
Marian Balakowiczaab8c492005-10-28 22:30:33 +020092 miiphy_write (devname, addr, PHY_ANAR, adv);
wdenkaffae2b2002-08-17 09:36:01 +000093
Marian Balakowicz49d0eee2006-06-30 16:30:46 +020094 miiphy_read (devname, addr, PHY_1000BTCR, &adv);
95 adv |= (0x0300);
96 miiphy_write (devname, addr, PHY_1000BTCR, adv);
97
Stefan Roese0c7ffc02005-08-16 18:18:00 +020098 /* Start/Restart aneg */
Marian Balakowiczaab8c492005-10-28 22:30:33 +020099 miiphy_read (devname, addr, PHY_BMCR, &ctl);
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200100 ctl |= (PHY_BMCR_AUTON | PHY_BMCR_RST_NEG);
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200101 miiphy_write (devname, addr, PHY_BMCR, ctl);
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200102
103 return 0;
104}
105
106
107/***********************************************************/
108/* read a phy reg and return the value with a rc */
109/***********************************************************/
110unsigned int miiphy_getemac_offset (void)
wdenkaffae2b2002-08-17 09:36:01 +0000111{
Marian Balakowicz49d0eee2006-06-30 16:30:46 +0200112#if (defined(CONFIG_440) && !defined(CONFIG_440SP) && !defined(CONFIG_440SPE)) && defined(CONFIG_NET_MULTI)
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200113 unsigned long zmii;
114 unsigned long eoffset;
115
116 /* Need to find out which mdi port we're using */
Stefan Roese697100952007-10-23 14:03:17 +0200117 zmii = in_be32((void *)ZMII_FER);
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200118
119 if (zmii & (ZMII_FER_MDI << ZMII_FER_V (0))) {
120 /* using port 0 */
121 eoffset = 0;
122 } else if (zmii & (ZMII_FER_MDI << ZMII_FER_V (1))) {
123 /* using port 1 */
124 eoffset = 0x100;
125 } else if (zmii & (ZMII_FER_MDI << ZMII_FER_V (2))) {
126 /* using port 2 */
127 eoffset = 0x400;
128 } else if (zmii & (ZMII_FER_MDI << ZMII_FER_V (3))) {
129 /* using port 3 */
130 eoffset = 0x600;
131 } else {
132 /* None of the mdi ports are enabled! */
133 /* enable port 0 */
134 zmii |= ZMII_FER_MDI << ZMII_FER_V (0);
Stefan Roese697100952007-10-23 14:03:17 +0200135 out_be32((void *)ZMII_FER, zmii);
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200136 eoffset = 0;
137 /* need to soft reset port 0 */
Stefan Roese697100952007-10-23 14:03:17 +0200138 zmii = in_be32((void *)EMAC_M0);
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200139 zmii |= EMAC_M0_SRST;
Stefan Roese697100952007-10-23 14:03:17 +0200140 out_be32((void *)EMAC_M0, zmii);
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200141 }
142
143 return (eoffset);
144#else
Stefan Roese153b3e22007-10-05 17:10:59 +0200145
146#if defined(CONFIG_NET_MULTI) && defined(CONFIG_405EX)
147 unsigned long rgmii;
148 int devnum = 1;
149
Stefan Roese697100952007-10-23 14:03:17 +0200150 rgmii = in_be32((void *)RGMII_FER);
Stefan Roese153b3e22007-10-05 17:10:59 +0200151 if (rgmii & (1 << (19 - devnum)))
152 return 0x100;
153#endif
154
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200155 return 0;
156#endif
157}
158
159
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200160int emac4xx_miiphy_read (char *devname, unsigned char addr,
161 unsigned char reg, unsigned short *value)
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200162{
163 unsigned long sta_reg; /* STA scratch area */
wdenkaffae2b2002-08-17 09:36:01 +0000164 unsigned long i;
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200165 unsigned long emac_reg;
wdenkaffae2b2002-08-17 09:36:01 +0000166
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200167
168 emac_reg = miiphy_getemac_offset ();
wdenkaffae2b2002-08-17 09:36:01 +0000169 /* see if it is ready for 1000 nsec */
170 i = 0;
171
172 /* see if it is ready for sec */
Stefan Roese697100952007-10-23 14:03:17 +0200173 while ((in_be32((void *)EMAC_STACR + emac_reg) & EMAC_STACR_OC) == EMAC_STACR_OC_MASK) {
wdenkaffae2b2002-08-17 09:36:01 +0000174 udelay (7);
175 if (i > 5) {
Marian Balakowicz49d0eee2006-06-30 16:30:46 +0200176#ifdef ET_DEBUG
Stefan Roese697100952007-10-23 14:03:17 +0200177 sta_reg = in_be32((void *)EMAC_STACR + emac_reg);
Marian Balakowicz49d0eee2006-06-30 16:30:46 +0200178 printf ("read : EMAC_STACR=0x%0x\n", sta_reg); /* test-only */
wdenkaffae2b2002-08-17 09:36:01 +0000179 printf ("read err 1\n");
stroese86bb6fc2003-12-09 14:57:03 +0000180#endif
wdenkaffae2b2002-08-17 09:36:01 +0000181 return -1;
182 }
183 i++;
184 }
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200185 sta_reg = reg; /* reg address */
wdenkaffae2b2002-08-17 09:36:01 +0000186 /* set clock (50Mhz) and read flags */
Stefan Roese42fbddd2006-09-07 11:51:23 +0200187#if defined(CONFIG_440GX) || defined(CONFIG_440SPE) || \
Stefan Roese153b3e22007-10-05 17:10:59 +0200188 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
189 defined(CONFIG_405EX)
Marian Balakowicz49d0eee2006-06-30 16:30:46 +0200190#if defined(CONFIG_IBM_EMAC4_V4) /* EMAC4 V4 changed bit setting */
191 sta_reg = (sta_reg & ~EMAC_STACR_OP_MASK) | EMAC_STACR_READ;
192#else
193 sta_reg |= EMAC_STACR_READ;
194#endif
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200195#else
wdenkaffae2b2002-08-17 09:36:01 +0000196 sta_reg = (sta_reg | EMAC_STACR_READ) & ~EMAC_STACR_CLK_100MHZ;
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200197#endif
198
Stefan Roese42fbddd2006-09-07 11:51:23 +0200199#if defined(CONFIG_PHY_CLK_FREQ) && !defined(CONFIG_440GX) && \
200 !defined(CONFIG_440SP) && !defined(CONFIG_440SPE) && \
Stefan Roese153b3e22007-10-05 17:10:59 +0200201 !defined(CONFIG_440EPX) && !defined(CONFIG_440GRX) && \
202 !defined(CONFIG_405EX)
wdenk232fe0b2003-09-02 22:48:03 +0000203 sta_reg = sta_reg | CONFIG_PHY_CLK_FREQ;
wdenk19cbaf42003-09-02 23:08:13 +0000204#endif
wdenkaffae2b2002-08-17 09:36:01 +0000205 sta_reg = sta_reg | (addr << 5); /* Phy address */
Marian Balakowicz49d0eee2006-06-30 16:30:46 +0200206 sta_reg = sta_reg | EMAC_STACR_OC_MASK; /* new IBM emac v4 */
Stefan Roese697100952007-10-23 14:03:17 +0200207 out_be32((void *)EMAC_STACR + emac_reg, sta_reg);
Marian Balakowicz49d0eee2006-06-30 16:30:46 +0200208#ifdef ET_DEBUG
wdenkaffae2b2002-08-17 09:36:01 +0000209 printf ("a2: write: EMAC_STACR=0x%0x\n", sta_reg); /* test-only */
210#endif
211
Stefan Roese697100952007-10-23 14:03:17 +0200212 sta_reg = in_be32((void *)EMAC_STACR + emac_reg);
Marian Balakowicz49d0eee2006-06-30 16:30:46 +0200213#ifdef ET_DEBUG
214 printf ("a21: read : EMAC_STACR=0x%0x\n", sta_reg); /* test-only */
215#endif
wdenkaffae2b2002-08-17 09:36:01 +0000216 i = 0;
Marian Balakowicz49d0eee2006-06-30 16:30:46 +0200217 while ((sta_reg & EMAC_STACR_OC) == EMAC_STACR_OC_MASK) {
wdenkaffae2b2002-08-17 09:36:01 +0000218 udelay (7);
219 if (i > 5) {
wdenkaffae2b2002-08-17 09:36:01 +0000220 return -1;
221 }
222 i++;
Stefan Roese697100952007-10-23 14:03:17 +0200223 sta_reg = in_be32((void *)EMAC_STACR + emac_reg);
Marian Balakowicz49d0eee2006-06-30 16:30:46 +0200224#ifdef ET_DEBUG
225 printf ("a22: read : EMAC_STACR=0x%0x\n", sta_reg); /* test-only */
226#endif
wdenkaffae2b2002-08-17 09:36:01 +0000227 }
228 if ((sta_reg & EMAC_STACR_PHYE) != 0) {
wdenkaffae2b2002-08-17 09:36:01 +0000229 return -1;
230 }
231
232 *value = *(short *) (&sta_reg);
233 return 0;
234
235
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200236} /* phy_read */
wdenkaffae2b2002-08-17 09:36:01 +0000237
238
239/***********************************************************/
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200240/* write a phy reg and return the value with a rc */
wdenkaffae2b2002-08-17 09:36:01 +0000241/***********************************************************/
242
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200243int emac4xx_miiphy_write (char *devname, unsigned char addr,
244 unsigned char reg, unsigned short value)
wdenkaffae2b2002-08-17 09:36:01 +0000245{
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200246 unsigned long sta_reg; /* STA scratch area */
wdenkaffae2b2002-08-17 09:36:01 +0000247 unsigned long i;
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200248 unsigned long emac_reg;
wdenkaffae2b2002-08-17 09:36:01 +0000249
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200250 emac_reg = miiphy_getemac_offset ();
wdenkaffae2b2002-08-17 09:36:01 +0000251 /* see if it is ready for 1000 nsec */
252 i = 0;
253
Stefan Roese697100952007-10-23 14:03:17 +0200254 while ((in_be32((void *)EMAC_STACR + emac_reg) & EMAC_STACR_OC) == EMAC_STACR_OC_MASK) {
wdenkaffae2b2002-08-17 09:36:01 +0000255 if (i > 5)
256 return -1;
257 udelay (7);
258 i++;
259 }
260 sta_reg = 0;
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200261 sta_reg = reg; /* reg address */
wdenkaffae2b2002-08-17 09:36:01 +0000262 /* set clock (50Mhz) and read flags */
Stefan Roese42fbddd2006-09-07 11:51:23 +0200263#if defined(CONFIG_440GX) || defined(CONFIG_440SPE) || \
Stefan Roese153b3e22007-10-05 17:10:59 +0200264 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
265 defined(CONFIG_405EX)
Marian Balakowicz49d0eee2006-06-30 16:30:46 +0200266#if defined(CONFIG_IBM_EMAC4_V4) /* EMAC4 V4 changed bit setting */
267 sta_reg = (sta_reg & ~EMAC_STACR_OP_MASK) | EMAC_STACR_WRITE;
268#else
269 sta_reg |= EMAC_STACR_WRITE;
270#endif
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200271#else
wdenkaffae2b2002-08-17 09:36:01 +0000272 sta_reg = (sta_reg | EMAC_STACR_WRITE) & ~EMAC_STACR_CLK_100MHZ;
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200273#endif
274
Stefan Roese42fbddd2006-09-07 11:51:23 +0200275#if defined(CONFIG_PHY_CLK_FREQ) && !defined(CONFIG_440GX) && \
276 !defined(CONFIG_440SP) && !defined(CONFIG_440SPE) && \
Stefan Roese153b3e22007-10-05 17:10:59 +0200277 !defined(CONFIG_440EPX) && !defined(CONFIG_440GRX) && \
278 !defined(CONFIG_405EX)
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200279 sta_reg = sta_reg | CONFIG_PHY_CLK_FREQ; /* Set clock frequency (PLB freq. dependend) */
wdenk19cbaf42003-09-02 23:08:13 +0000280#endif
Marian Balakowicz49d0eee2006-06-30 16:30:46 +0200281 sta_reg = sta_reg | ((unsigned long) addr << 5);/* Phy address */
282 sta_reg = sta_reg | EMAC_STACR_OC_MASK; /* new IBM emac v4 */
wdenkaffae2b2002-08-17 09:36:01 +0000283 memcpy (&sta_reg, &value, 2); /* put in data */
284
Stefan Roese697100952007-10-23 14:03:17 +0200285 out_be32((void *)EMAC_STACR + emac_reg, sta_reg);
wdenkaffae2b2002-08-17 09:36:01 +0000286
wdenkaffae2b2002-08-17 09:36:01 +0000287 /* wait for completion */
288 i = 0;
Stefan Roese697100952007-10-23 14:03:17 +0200289 sta_reg = in_be32((void *)EMAC_STACR + emac_reg);
Marian Balakowicz49d0eee2006-06-30 16:30:46 +0200290#ifdef ET_DEBUG
291 printf ("a31: read : EMAC_STACR=0x%0x\n", sta_reg); /* test-only */
292#endif
293 while ((sta_reg & EMAC_STACR_OC) == EMAC_STACR_OC_MASK) {
wdenkaffae2b2002-08-17 09:36:01 +0000294 udelay (7);
295 if (i > 5)
296 return -1;
297 i++;
Stefan Roese697100952007-10-23 14:03:17 +0200298 sta_reg = in_be32((void *)EMAC_STACR + emac_reg);
Marian Balakowicz49d0eee2006-06-30 16:30:46 +0200299#ifdef ET_DEBUG
300 printf ("a32: read : EMAC_STACR=0x%0x\n", sta_reg); /* test-only */
301#endif
wdenkaffae2b2002-08-17 09:36:01 +0000302 }
303
304 if ((sta_reg & EMAC_STACR_PHYE) != 0)
305 return -1;
306 return 0;
307
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200308} /* phy_write */