Merge commit 'u-boot-fdt/testing'
[platform/kernel/u-boot.git] / cpu / ppc4xx / miiphy.c
1 /*-----------------------------------------------------------------------------+
2   |
3   |       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.
9   |
10   |       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.
13   |
14   |       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.
17   |
18   |       COPYRIGHT   I B M   CORPORATION 1995
19   |       LICENSED MATERIAL  -  PROGRAM PROPERTY OF I B M
20   +-----------------------------------------------------------------------------*/
21 /*-----------------------------------------------------------------------------+
22   |
23   |  File Name:  miiphy.c
24   |
25   |  Function:   This module has utilities for accessing the MII PHY through
26   |            the EMAC3 macro.
27   |
28   |  Author:     Mark Wisner
29   |
30   |  Change Activity-
31   |
32   |  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>
42   |
43   +-----------------------------------------------------------------------------*/
44
45 #include <common.h>
46 #include <asm/processor.h>
47 #include <asm/io.h>
48 #include <ppc_asm.tmpl>
49 #include <commproc.h>
50 #include <ppc4xx_enet.h>
51 #include <405_mal.h>
52 #include <miiphy.h>
53
54 #undef ET_DEBUG
55 /***********************************************************/
56 /* Dump out to the screen PHY regs                         */
57 /***********************************************************/
58
59 void miiphy_dump (char *devname, unsigned char addr)
60 {
61         unsigned long i;
62         unsigned short data;
63
64
65         for (i = 0; i < 0x1A; i++) {
66                 if (miiphy_read (devname, addr, i, &data)) {
67                         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
76         }                       /* end for loop */
77 }                               /* end dump */
78
79
80 /***********************************************************/
81 /* (Re)start autonegotiation                               */
82 /***********************************************************/
83 int phy_setup_aneg (char *devname, unsigned char addr)
84 {
85         unsigned short ctl, adv;
86
87         /* Setup standard advertise */
88         miiphy_read (devname, addr, PHY_ANAR, &adv);
89         adv |= (PHY_ANLPAR_ACK | PHY_ANLPAR_RF | PHY_ANLPAR_T4 |
90                 PHY_ANLPAR_TXFD | PHY_ANLPAR_TX | PHY_ANLPAR_10FD |
91                 PHY_ANLPAR_10);
92         miiphy_write (devname, addr, PHY_ANAR, adv);
93
94         miiphy_read (devname, addr, PHY_1000BTCR, &adv);
95         adv |= (0x0300);
96         miiphy_write (devname, addr, PHY_1000BTCR, adv);
97
98         /* Start/Restart aneg */
99         miiphy_read (devname, addr, PHY_BMCR, &ctl);
100         ctl |= (PHY_BMCR_AUTON | PHY_BMCR_RST_NEG);
101         miiphy_write (devname, addr, PHY_BMCR, ctl);
102
103         return 0;
104 }
105
106
107 /***********************************************************/
108 /* read a phy reg and return the value with a rc           */
109 /***********************************************************/
110 unsigned int miiphy_getemac_offset (void)
111 {
112 #if (defined(CONFIG_440) && !defined(CONFIG_440SP) && !defined(CONFIG_440SPE)) && defined(CONFIG_NET_MULTI)
113         unsigned long zmii;
114         unsigned long eoffset;
115
116         /* Need to find out which mdi port we're using */
117         zmii = in_be32((void *)ZMII_FER);
118
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);
135                 out_be32((void *)ZMII_FER, zmii);
136                 eoffset = 0;
137                 /* need to soft reset port 0 */
138                 zmii = in_be32((void *)EMAC_M0);
139                 zmii |= EMAC_M0_SRST;
140                 out_be32((void *)EMAC_M0, zmii);
141         }
142
143         return (eoffset);
144 #else
145
146 #if defined(CONFIG_NET_MULTI) && defined(CONFIG_405EX)
147         unsigned long rgmii;
148         int devnum = 1;
149
150         rgmii = in_be32((void *)RGMII_FER);
151         if (rgmii & (1 << (19 - devnum)))
152                 return 0x100;
153 #endif
154
155         return 0;
156 #endif
157 }
158
159
160 int emac4xx_miiphy_read (char *devname, unsigned char addr,
161                 unsigned char reg, unsigned short *value)
162 {
163         unsigned long sta_reg;  /* STA scratch area */
164         unsigned long i;
165         unsigned long emac_reg;
166
167
168         emac_reg = miiphy_getemac_offset ();
169         /* see if it is ready for 1000 nsec */
170         i = 0;
171
172         /* see if it is ready for  sec */
173         while ((in_be32((void *)EMAC_STACR + emac_reg) & EMAC_STACR_OC) == EMAC_STACR_OC_MASK) {
174                 udelay (7);
175                 if (i > 5) {
176 #ifdef ET_DEBUG
177                         sta_reg = in_be32((void *)EMAC_STACR + emac_reg);
178                         printf ("read : EMAC_STACR=0x%0x\n", sta_reg);  /* test-only */
179                         printf ("read err 1\n");
180 #endif
181                         return -1;
182                 }
183                 i++;
184         }
185         sta_reg = reg;          /* reg address */
186         /* set clock (50Mhz) and read flags */
187 #if defined(CONFIG_440GX) || defined(CONFIG_440SPE) || \
188     defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
189     defined(CONFIG_405EX)
190 #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
195 #else
196         sta_reg = (sta_reg | EMAC_STACR_READ) & ~EMAC_STACR_CLK_100MHZ;
197 #endif
198
199 #if defined(CONFIG_PHY_CLK_FREQ) && !defined(CONFIG_440GX) && \
200     !defined(CONFIG_440SP) && !defined(CONFIG_440SPE) && \
201     !defined(CONFIG_440EPX) && !defined(CONFIG_440GRX) && \
202     !defined(CONFIG_405EX)
203         sta_reg = sta_reg | CONFIG_PHY_CLK_FREQ;
204 #endif
205         sta_reg = sta_reg | (addr << 5);        /* Phy address */
206         sta_reg = sta_reg | EMAC_STACR_OC_MASK; /* new IBM emac v4 */
207         out_be32((void *)EMAC_STACR + emac_reg, sta_reg);
208 #ifdef ET_DEBUG
209         printf ("a2: write: EMAC_STACR=0x%0x\n", sta_reg);      /* test-only */
210 #endif
211
212         sta_reg = in_be32((void *)EMAC_STACR + emac_reg);
213 #ifdef ET_DEBUG
214                 printf ("a21: read : EMAC_STACR=0x%0x\n", sta_reg);     /* test-only */
215 #endif
216         i = 0;
217         while ((sta_reg & EMAC_STACR_OC) == EMAC_STACR_OC_MASK) {
218                 udelay (7);
219                 if (i > 5) {
220                         return -1;
221                 }
222                 i++;
223                 sta_reg = in_be32((void *)EMAC_STACR + emac_reg);
224 #ifdef ET_DEBUG
225                 printf ("a22: read : EMAC_STACR=0x%0x\n", sta_reg);     /* test-only */
226 #endif
227         }
228         if ((sta_reg & EMAC_STACR_PHYE) != 0) {
229                 return -1;
230         }
231
232         *value = *(short *) (&sta_reg);
233         return 0;
234
235
236 }                               /* phy_read */
237
238
239 /***********************************************************/
240 /* write a phy reg and return the value with a rc           */
241 /***********************************************************/
242
243 int emac4xx_miiphy_write (char *devname, unsigned char addr,
244                 unsigned char reg, unsigned short value)
245 {
246         unsigned long sta_reg;  /* STA scratch area */
247         unsigned long i;
248         unsigned long emac_reg;
249
250         emac_reg = miiphy_getemac_offset ();
251         /* see if it is ready for 1000 nsec */
252         i = 0;
253
254         while ((in_be32((void *)EMAC_STACR + emac_reg) & EMAC_STACR_OC) == EMAC_STACR_OC_MASK) {
255                 if (i > 5)
256                         return -1;
257                 udelay (7);
258                 i++;
259         }
260         sta_reg = 0;
261         sta_reg = reg;          /* reg address */
262         /* set clock (50Mhz) and read flags */
263 #if defined(CONFIG_440GX) || defined(CONFIG_440SPE) || \
264     defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
265     defined(CONFIG_405EX)
266 #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
271 #else
272         sta_reg = (sta_reg | EMAC_STACR_WRITE) & ~EMAC_STACR_CLK_100MHZ;
273 #endif
274
275 #if defined(CONFIG_PHY_CLK_FREQ) && !defined(CONFIG_440GX) && \
276     !defined(CONFIG_440SP) && !defined(CONFIG_440SPE) && \
277     !defined(CONFIG_440EPX) && !defined(CONFIG_440GRX) && \
278     !defined(CONFIG_405EX)
279         sta_reg = sta_reg | CONFIG_PHY_CLK_FREQ;        /* Set clock frequency (PLB freq. dependend) */
280 #endif
281         sta_reg = sta_reg | ((unsigned long) addr << 5);/* Phy address */
282         sta_reg = sta_reg | EMAC_STACR_OC_MASK;         /* new IBM emac v4 */
283         memcpy (&sta_reg, &value, 2);   /* put in data */
284
285         out_be32((void *)EMAC_STACR + emac_reg, sta_reg);
286
287         /* wait for completion */
288         i = 0;
289         sta_reg = in_be32((void *)EMAC_STACR + emac_reg);
290 #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) {
294                 udelay (7);
295                 if (i > 5)
296                         return -1;
297                 i++;
298                 sta_reg = in_be32((void *)EMAC_STACR + emac_reg);
299 #ifdef ET_DEBUG
300                 printf ("a32: read : EMAC_STACR=0x%0x\n", sta_reg);     /* test-only */
301 #endif
302         }
303
304         if ((sta_reg & EMAC_STACR_PHYE) != 0)
305                 return -1;
306         return 0;
307
308 }                               /* phy_write */