blob: 24f91546c62ee2e31b6460fd4afc8aa8c4bfe756 [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 |
30 | Change Activity-
31 |
Stefan Roesed6c61aa2005-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>
47#include <ppc_asm.tmpl>
48#include <commproc.h>
Stefan Roesed6c61aa2005-08-16 18:18:00 +020049#include <ppc4xx_enet.h>
wdenkaffae2b2002-08-17 09:36:01 +000050#include <405_mal.h>
51#include <miiphy.h>
52
wdenkaffae2b2002-08-17 09:36:01 +000053
54/***********************************************************/
Stefan Roesed6c61aa2005-08-16 18:18:00 +020055/* Dump out to the screen PHY regs */
wdenkaffae2b2002-08-17 09:36:01 +000056/***********************************************************/
57
58void miiphy_dump (unsigned char addr)
59{
60 unsigned long i;
61 unsigned short data;
62
63
64 for (i = 0; i < 0x1A; i++) {
65 if (miiphy_read (addr, i, &data)) {
66 printf ("read error for reg %lx\n", i);
67 return;
68 }
69 printf ("Phy reg %lx ==> %4x\n", i, data);
70
71 /* jump to the next set of regs */
72 if (i == 0x07)
73 i = 0x0f;
74
Stefan Roesed6c61aa2005-08-16 18:18:00 +020075 } /* end for loop */
76} /* end dump */
wdenkaffae2b2002-08-17 09:36:01 +000077
78
wdenkaffae2b2002-08-17 09:36:01 +000079/***********************************************************/
Stefan Roesed6c61aa2005-08-16 18:18:00 +020080/* (Re)start autonegotiation */
wdenkaffae2b2002-08-17 09:36:01 +000081/***********************************************************/
Stefan Roesed6c61aa2005-08-16 18:18:00 +020082int phy_setup_aneg (unsigned char addr)
wdenkaffae2b2002-08-17 09:36:01 +000083{
Stefan Roesed6c61aa2005-08-16 18:18:00 +020084 unsigned short ctl, adv;
wdenkaffae2b2002-08-17 09:36:01 +000085
Stefan Roesed6c61aa2005-08-16 18:18:00 +020086 /* Setup standard advertise */
87 miiphy_read (addr, PHY_ANAR, &adv);
88 adv |= (PHY_ANLPAR_ACK | PHY_ANLPAR_RF | PHY_ANLPAR_T4 |
89 PHY_ANLPAR_TXFD | PHY_ANLPAR_TX | PHY_ANLPAR_10FD |
90 PHY_ANLPAR_10);
91 miiphy_write (addr, PHY_ANAR, adv);
92
93 /* Start/Restart aneg */
94 miiphy_read (addr, PHY_BMCR, &ctl);
95 ctl |= (PHY_BMCR_AUTON | PHY_BMCR_RST_NEG);
96 miiphy_write (addr, PHY_BMCR, ctl);
97
98 return 0;
99}
100
101
102/***********************************************************/
103/* read a phy reg and return the value with a rc */
104/***********************************************************/
105unsigned int miiphy_getemac_offset (void)
106{
107#if defined(CONFIG_440) && defined(CONFIG_NET_MULTI)
108 unsigned long zmii;
109 unsigned long eoffset;
110
111 /* Need to find out which mdi port we're using */
112 zmii = in32 (ZMII_FER);
113
114 if (zmii & (ZMII_FER_MDI << ZMII_FER_V (0))) {
115 /* using port 0 */
116 eoffset = 0;
117 } else if (zmii & (ZMII_FER_MDI << ZMII_FER_V (1))) {
118 /* using port 1 */
119 eoffset = 0x100;
120 } else if (zmii & (ZMII_FER_MDI << ZMII_FER_V (2))) {
121 /* using port 2 */
122 eoffset = 0x400;
123 } else if (zmii & (ZMII_FER_MDI << ZMII_FER_V (3))) {
124 /* using port 3 */
125 eoffset = 0x600;
126 } else {
127 /* None of the mdi ports are enabled! */
128 /* enable port 0 */
129 zmii |= ZMII_FER_MDI << ZMII_FER_V (0);
130 out32 (ZMII_FER, zmii);
131 eoffset = 0;
132 /* need to soft reset port 0 */
133 zmii = in32 (EMAC_M0);
134 zmii |= EMAC_M0_SRST;
135 out32 (EMAC_M0, zmii);
136 }
137
138 return (eoffset);
139#else
140 return 0;
141#endif
142}
143
144
145int miiphy_read (unsigned char addr, unsigned char reg, unsigned short *value)
146{
147 unsigned long sta_reg; /* STA scratch area */
148 unsigned long i;
149 unsigned long emac_reg;
150
151
152 emac_reg = miiphy_getemac_offset ();
wdenkaffae2b2002-08-17 09:36:01 +0000153 /* see if it is ready for 1000 nsec */
154 i = 0;
155
156 /* see if it is ready for sec */
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200157 while ((in32 (EMAC_STACR + emac_reg) & EMAC_STACR_OC) == 0) {
wdenkaffae2b2002-08-17 09:36:01 +0000158 udelay (7);
159 if (i > 5) {
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200160#if 0
wdenkaffae2b2002-08-17 09:36:01 +0000161 printf ("read err 1\n");
stroese38a95192003-12-09 14:57:03 +0000162#endif
wdenkaffae2b2002-08-17 09:36:01 +0000163 return -1;
164 }
165 i++;
166 }
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200167 sta_reg = reg; /* reg address */
wdenkaffae2b2002-08-17 09:36:01 +0000168 /* set clock (50Mhz) and read flags */
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200169#if defined(CONFIG_440GX)
170 sta_reg |= EMAC_STACR_READ;
171#else
wdenkaffae2b2002-08-17 09:36:01 +0000172 sta_reg = (sta_reg | EMAC_STACR_READ) & ~EMAC_STACR_CLK_100MHZ;
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200173#endif
174
175#if defined(CONFIG_PHY_CLK_FREQ) && !defined(CONFIG_440GX)
wdenk12f34242003-09-02 22:48:03 +0000176 sta_reg = sta_reg | CONFIG_PHY_CLK_FREQ;
wdenk093ae272003-09-02 23:08:13 +0000177#endif
wdenkaffae2b2002-08-17 09:36:01 +0000178 sta_reg = sta_reg | (addr << 5); /* Phy address */
179
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200180 out32 (EMAC_STACR + emac_reg, sta_reg);
181#if 0 /* test-only */
wdenkaffae2b2002-08-17 09:36:01 +0000182 printf ("a2: write: EMAC_STACR=0x%0x\n", sta_reg); /* test-only */
183#endif
184
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200185 sta_reg = in32 (EMAC_STACR + emac_reg);
wdenkaffae2b2002-08-17 09:36:01 +0000186 i = 0;
187 while ((sta_reg & EMAC_STACR_OC) == 0) {
188 udelay (7);
189 if (i > 5) {
wdenkaffae2b2002-08-17 09:36:01 +0000190 return -1;
191 }
192 i++;
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200193 sta_reg = in32 (EMAC_STACR + emac_reg);
wdenkaffae2b2002-08-17 09:36:01 +0000194 }
195 if ((sta_reg & EMAC_STACR_PHYE) != 0) {
wdenkaffae2b2002-08-17 09:36:01 +0000196 return -1;
197 }
198
199 *value = *(short *) (&sta_reg);
200 return 0;
201
202
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200203} /* phy_read */
wdenkaffae2b2002-08-17 09:36:01 +0000204
205
206/***********************************************************/
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200207/* write a phy reg and return the value with a rc */
wdenkaffae2b2002-08-17 09:36:01 +0000208/***********************************************************/
209
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200210int miiphy_write (unsigned char addr, unsigned char reg, unsigned short value)
wdenkaffae2b2002-08-17 09:36:01 +0000211{
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200212 unsigned long sta_reg; /* STA scratch area */
wdenkaffae2b2002-08-17 09:36:01 +0000213 unsigned long i;
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200214 unsigned long emac_reg;
wdenkaffae2b2002-08-17 09:36:01 +0000215
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200216 emac_reg = miiphy_getemac_offset ();
wdenkaffae2b2002-08-17 09:36:01 +0000217 /* see if it is ready for 1000 nsec */
218 i = 0;
219
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200220 while ((in32 (EMAC_STACR + emac_reg) & EMAC_STACR_OC) == 0) {
wdenkaffae2b2002-08-17 09:36:01 +0000221 if (i > 5)
222 return -1;
223 udelay (7);
224 i++;
225 }
226 sta_reg = 0;
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200227 sta_reg = reg; /* reg address */
wdenkaffae2b2002-08-17 09:36:01 +0000228 /* set clock (50Mhz) and read flags */
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200229#if defined(CONFIG_440GX)
230 sta_reg |= EMAC_STACR_WRITE;
231#else
wdenkaffae2b2002-08-17 09:36:01 +0000232 sta_reg = (sta_reg | EMAC_STACR_WRITE) & ~EMAC_STACR_CLK_100MHZ;
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200233#endif
234
235#if defined(CONFIG_PHY_CLK_FREQ) && !defined(CONFIG_440GX)
236 sta_reg = sta_reg | CONFIG_PHY_CLK_FREQ; /* Set clock frequency (PLB freq. dependend) */
wdenk093ae272003-09-02 23:08:13 +0000237#endif
wdenkaffae2b2002-08-17 09:36:01 +0000238 sta_reg = sta_reg | ((unsigned long) addr << 5); /* Phy address */
239 memcpy (&sta_reg, &value, 2); /* put in data */
240
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200241 out32 (EMAC_STACR + emac_reg, sta_reg);
wdenkaffae2b2002-08-17 09:36:01 +0000242
wdenkaffae2b2002-08-17 09:36:01 +0000243 /* wait for completion */
244 i = 0;
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200245 sta_reg = in32 (EMAC_STACR + emac_reg);
wdenkaffae2b2002-08-17 09:36:01 +0000246 while ((sta_reg & EMAC_STACR_OC) == 0) {
247 udelay (7);
248 if (i > 5)
249 return -1;
250 i++;
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200251 sta_reg = in32 (EMAC_STACR + emac_reg);
wdenkaffae2b2002-08-17 09:36:01 +0000252 }
253
254 if ((sta_reg & EMAC_STACR_PHYE) != 0)
255 return -1;
256 return 0;
257
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200258} /* phy_write */