]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - cpu/ppc4xx/miiphy.c
aa580ed48f99376a68ec0ce77be3cb24aadc0c26
[karo-tx-uboot.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 <ppc_asm.tmpl>
48 #include <commproc.h>
49 #include <ppc4xx_enet.h>
50 #include <405_mal.h>
51 #include <miiphy.h>
52
53 #undef ET_DEBUG
54 /***********************************************************/
55 /* Dump out to the screen PHY regs                         */
56 /***********************************************************/
57
58 void miiphy_dump (char *devname, 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 (devname, 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
75         }                       /* end for loop */
76 }                               /* end dump */
77
78
79 /***********************************************************/
80 /* (Re)start autonegotiation                               */
81 /***********************************************************/
82 int phy_setup_aneg (char *devname, unsigned char addr)
83 {
84         unsigned short ctl, adv;
85
86         /* Setup standard advertise */
87         miiphy_read (devname, 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 (devname, addr, PHY_ANAR, adv);
92
93         miiphy_read (devname, addr, PHY_1000BTCR, &adv);
94         adv |= (0x0300);
95         miiphy_write (devname, addr, PHY_1000BTCR, adv);
96
97         /* Start/Restart aneg */
98         miiphy_read (devname, addr, PHY_BMCR, &ctl);
99         ctl |= (PHY_BMCR_AUTON | PHY_BMCR_RST_NEG);
100         miiphy_write (devname, addr, PHY_BMCR, ctl);
101
102         return 0;
103 }
104
105
106 /***********************************************************/
107 /* read a phy reg and return the value with a rc           */
108 /***********************************************************/
109 unsigned int miiphy_getemac_offset (void)
110 {
111 #if (defined(CONFIG_440) && !defined(CONFIG_440SP) && !defined(CONFIG_440SPE)) && defined(CONFIG_NET_MULTI)
112         unsigned long zmii;
113         unsigned long eoffset;
114
115         /* Need to find out which mdi port we're using */
116         zmii = in32 (ZMII_FER);
117
118         if (zmii & (ZMII_FER_MDI << ZMII_FER_V (0))) {
119                 /* using port 0 */
120                 eoffset = 0;
121         } else if (zmii & (ZMII_FER_MDI << ZMII_FER_V (1))) {
122                 /* using port 1 */
123                 eoffset = 0x100;
124         } else if (zmii & (ZMII_FER_MDI << ZMII_FER_V (2))) {
125                 /* using port 2 */
126                 eoffset = 0x400;
127         } else if (zmii & (ZMII_FER_MDI << ZMII_FER_V (3))) {
128                 /* using port 3 */
129                 eoffset = 0x600;
130         } else {
131                 /* None of the mdi ports are enabled! */
132                 /* enable port 0 */
133                 zmii |= ZMII_FER_MDI << ZMII_FER_V (0);
134                 out32 (ZMII_FER, zmii);
135                 eoffset = 0;
136                 /* need to soft reset port 0 */
137                 zmii = in32 (EMAC_M0);
138                 zmii |= EMAC_M0_SRST;
139                 out32 (EMAC_M0, zmii);
140         }
141
142         return (eoffset);
143 #else
144         return 0;
145 #endif
146 }
147
148
149 int emac4xx_miiphy_read (char *devname, unsigned char addr,
150                 unsigned char reg, unsigned short *value)
151 {
152         unsigned long sta_reg;  /* STA scratch area */
153         unsigned long i;
154         unsigned long emac_reg;
155
156
157         emac_reg = miiphy_getemac_offset ();
158         /* see if it is ready for 1000 nsec */
159         i = 0;
160
161         /* see if it is ready for  sec */
162         while ((in32 (EMAC_STACR + emac_reg) & EMAC_STACR_OC) == EMAC_STACR_OC_MASK) {
163                 udelay (7);
164                 if (i > 5) {
165 #ifdef ET_DEBUG
166                         sta_reg = in32 (EMAC_STACR + emac_reg);
167                         printf ("read : EMAC_STACR=0x%0x\n", sta_reg);  /* test-only */
168                         printf ("read err 1\n");
169 #endif
170                         return -1;
171                 }
172                 i++;
173         }
174         sta_reg = reg;          /* reg address */
175         /* set clock (50Mhz) and read flags */
176 #if defined(CONFIG_440GX) || defined(CONFIG_440SPE)
177 #if defined(CONFIG_IBM_EMAC4_V4)      /* EMAC4 V4 changed bit setting */
178                 sta_reg = (sta_reg & ~EMAC_STACR_OP_MASK) | EMAC_STACR_READ;
179 #else
180                 sta_reg |= EMAC_STACR_READ;
181 #endif
182 #else
183         sta_reg = (sta_reg | EMAC_STACR_READ) & ~EMAC_STACR_CLK_100MHZ;
184 #endif
185
186 #if defined(CONFIG_PHY_CLK_FREQ) && !defined(CONFIG_440GX) && !defined(CONFIG__440SP) && !defined(CONFIG__440SPE)
187         sta_reg = sta_reg | CONFIG_PHY_CLK_FREQ;
188 #endif
189         sta_reg = sta_reg | (addr << 5);        /* Phy address */
190         sta_reg = sta_reg | EMAC_STACR_OC_MASK; /* new IBM emac v4 */
191         out32 (EMAC_STACR + emac_reg, sta_reg);
192 #ifdef ET_DEBUG
193         printf ("a2: write: EMAC_STACR=0x%0x\n", sta_reg);      /* test-only */
194 #endif
195
196         sta_reg = in32 (EMAC_STACR + emac_reg);
197 #ifdef ET_DEBUG
198                 printf ("a21: read : EMAC_STACR=0x%0x\n", sta_reg);     /* test-only */
199 #endif
200         i = 0;
201         while ((sta_reg & EMAC_STACR_OC) == EMAC_STACR_OC_MASK) {
202                 udelay (7);
203                 if (i > 5) {
204                         return -1;
205                 }
206                 i++;
207                 sta_reg = in32 (EMAC_STACR + emac_reg);
208 #ifdef ET_DEBUG
209                 printf ("a22: read : EMAC_STACR=0x%0x\n", sta_reg);     /* test-only */
210 #endif
211         }
212         if ((sta_reg & EMAC_STACR_PHYE) != 0) {
213                 return -1;
214         }
215
216         *value = *(short *) (&sta_reg);
217         return 0;
218
219
220 }                               /* phy_read */
221
222
223 /***********************************************************/
224 /* write a phy reg and return the value with a rc           */
225 /***********************************************************/
226
227 int emac4xx_miiphy_write (char *devname, unsigned char addr,
228                 unsigned char reg, unsigned short value)
229 {
230         unsigned long sta_reg;  /* STA scratch area */
231         unsigned long i;
232         unsigned long emac_reg;
233
234         emac_reg = miiphy_getemac_offset ();
235         /* see if it is ready for 1000 nsec */
236         i = 0;
237
238         while ((in32 (EMAC_STACR + emac_reg) & EMAC_STACR_OC) == EMAC_STACR_OC_MASK) {
239                 if (i > 5)
240                         return -1;
241                 udelay (7);
242                 i++;
243         }
244         sta_reg = 0;
245         sta_reg = reg;          /* reg address */
246         /* set clock (50Mhz) and read flags */
247 #if defined(CONFIG_440GX) || defined(CONFIG_440SPE)
248 #if defined(CONFIG_IBM_EMAC4_V4)      /* EMAC4 V4 changed bit setting */
249                 sta_reg = (sta_reg & ~EMAC_STACR_OP_MASK) | EMAC_STACR_WRITE;
250 #else
251                 sta_reg |= EMAC_STACR_WRITE;
252 #endif
253 #else
254         sta_reg = (sta_reg | EMAC_STACR_WRITE) & ~EMAC_STACR_CLK_100MHZ;
255 #endif
256
257 #if defined(CONFIG_PHY_CLK_FREQ) && !defined(CONFIG_440GX) && !defined(CONFIG__440SP) && !defined(CONFIG__440SPE)
258         sta_reg = sta_reg | CONFIG_PHY_CLK_FREQ;        /* Set clock frequency (PLB freq. dependend) */
259 #endif
260         sta_reg = sta_reg | ((unsigned long) addr << 5);/* Phy address */
261         sta_reg = sta_reg | EMAC_STACR_OC_MASK;         /* new IBM emac v4 */
262         memcpy (&sta_reg, &value, 2);   /* put in data */
263
264         out32 (EMAC_STACR + emac_reg, sta_reg);
265
266         /* wait for completion */
267         i = 0;
268         sta_reg = in32 (EMAC_STACR + emac_reg);
269 #ifdef ET_DEBUG
270                 printf ("a31: read : EMAC_STACR=0x%0x\n", sta_reg);     /* test-only */
271 #endif
272         while ((sta_reg & EMAC_STACR_OC) == EMAC_STACR_OC_MASK) {
273                 udelay (7);
274                 if (i > 5)
275                         return -1;
276                 i++;
277                 sta_reg = in32 (EMAC_STACR + emac_reg);
278 #ifdef ET_DEBUG
279                 printf ("a32: read : EMAC_STACR=0x%0x\n", sta_reg);     /* test-only */
280 #endif
281         }
282
283         if ((sta_reg & EMAC_STACR_PHYE) != 0)
284                 return -1;
285         return 0;
286
287 }                               /* phy_write */