ppc4xx: Remove compilation warning "pci_async_enabled defined but not used"
[oweals/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   +-----------------------------------------------------------------------------*/
31
32 /* define DEBUG for debugging output (obviously ;-)) */
33 #if 0
34 #define DEBUG
35 #endif
36
37 #include <common.h>
38 #include <asm/processor.h>
39 #include <asm/io.h>
40 #include <ppc_asm.tmpl>
41 #include <commproc.h>
42 #include <ppc4xx_enet.h>
43 #include <405_mal.h>
44 #include <miiphy.h>
45
46 #if !defined(CONFIG_PHY_CLK_FREQ)
47 #define CONFIG_PHY_CLK_FREQ     0
48 #endif
49
50 /***********************************************************/
51 /* Dump out to the screen PHY regs                         */
52 /***********************************************************/
53
54 void miiphy_dump (char *devname, unsigned char addr)
55 {
56         unsigned long i;
57         unsigned short data;
58
59         for (i = 0; i < 0x1A; i++) {
60                 if (miiphy_read (devname, addr, i, &data)) {
61                         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
70         }                       /* end for loop */
71 }                               /* end dump */
72
73 /***********************************************************/
74 /* (Re)start autonegotiation                               */
75 /***********************************************************/
76 int phy_setup_aneg (char *devname, unsigned char addr)
77 {
78         u16 bmcr;
79
80 #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
153         miiphy_read (devname, addr, PHY_ANAR, &adv);
154         adv |= (PHY_ANLPAR_ACK  | PHY_ANLPAR_TXFD | PHY_ANLPAR_TX |
155                 PHY_ANLPAR_10FD | PHY_ANLPAR_10);
156         miiphy_write (devname, addr, PHY_ANAR, adv);
157
158         miiphy_read (devname, addr, PHY_1000BTCR, &adv);
159         adv |= (0x0300);
160         miiphy_write (devname, addr, PHY_1000BTCR, adv);
161
162 #endif /* defined(CONFIG_PHY_DYNAMIC_ANEG) */
163
164         /* Start/Restart aneg */
165         miiphy_read (devname, addr, PHY_BMCR, &bmcr);
166         bmcr |= (PHY_BMCR_AUTON | PHY_BMCR_RST_NEG);
167         miiphy_write (devname, addr, PHY_BMCR, bmcr);
168
169         return 0;
170 }
171
172 /***********************************************************/
173 /* read a phy reg and return the value with a rc           */
174 /***********************************************************/
175 /* 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
183  * vg: For 460EX/460GT if internal GPCS PHY address is specified
184  * return appropriate EMAC offset
185  */
186 unsigned int miiphy_getemac_offset(u8 addr)
187 {
188 #if (defined(CONFIG_440) && \
189     !defined(CONFIG_440SP) && !defined(CONFIG_440SPE) && \
190     !defined(CONFIG_460EX) && !defined(CONFIG_460GT)) && \
191     defined(CONFIG_NET_MULTI)
192         unsigned long zmii;
193         unsigned long eoffset;
194
195         /* Need to find out which mdi port we're using */
196         zmii = in_be32((void *)ZMII_FER);
197
198         if (zmii & (ZMII_FER_MDI << ZMII_FER_V (0)))
199                 /* using port 0 */
200                 eoffset = 0;
201
202         else if (zmii & (ZMII_FER_MDI << ZMII_FER_V (1)))
203                 /* using port 1 */
204                 eoffset = 0x100;
205
206         else if (zmii & (ZMII_FER_MDI << ZMII_FER_V (2)))
207                 /* using port 2 */
208                 eoffset = 0x400;
209
210         else if (zmii & (ZMII_FER_MDI << ZMII_FER_V (3)))
211                 /* using port 3 */
212                 eoffset = 0x600;
213
214         else {
215                 /* None of the mdi ports are enabled! */
216                 /* enable port 0 */
217                 zmii |= ZMII_FER_MDI << ZMII_FER_V (0);
218                 out_be32((void *)ZMII_FER, zmii);
219                 eoffset = 0;
220                 /* need to soft reset port 0 */
221                 zmii = in_be32((void *)EMAC_M0);
222                 zmii |= EMAC_M0_SRST;
223                 out_be32((void *)EMAC_M0, zmii);
224         }
225
226         return (eoffset);
227 #else
228
229 #if defined(CONFIG_NET_MULTI) && defined(CONFIG_405EX)
230         unsigned long rgmii;
231         int devnum = 1;
232
233         rgmii = in_be32((void *)RGMII_FER);
234         if (rgmii & (1 << (19 - devnum)))
235                 return 0x100;
236 #endif
237
238 #if defined(CONFIG_460EX) || defined(CONFIG_460GT)
239         u32 eoffset = 0;
240
241         switch (addr) {
242 #if defined(CONFIG_HAS_ETH1) && defined(CONFIG_GPCS_PHY1_ADDR)
243         case CONFIG_GPCS_PHY1_ADDR:
244                 if (addr == EMAC_M1_IPPA_GET(in_be32((void *)EMAC_M1 + 0x100)))
245                         eoffset = 0x100;
246                 break;
247 #endif
248 #if defined(CONFIG_HAS_ETH2) && defined(CONFIG_GPCS_PHY2_ADDR)
249         case CONFIG_GPCS_PHY2_ADDR:
250                 if (addr == EMAC_M1_IPPA_GET(in_be32((void *)EMAC_M1 + 0x300)))
251                         eoffset = 0x300;
252                 break;
253 #endif
254 #if defined(CONFIG_HAS_ETH3) && defined(CONFIG_GPCS_PHY3_ADDR)
255         case CONFIG_GPCS_PHY3_ADDR:
256                 if (addr == EMAC_M1_IPPA_GET(in_be32((void *)EMAC_M1 + 0x400)))
257                         eoffset = 0x400;
258                 break;
259 #endif
260         default:
261                 eoffset = 0;
262                 break;
263         }
264         return eoffset;
265 #endif
266
267         return 0;
268 #endif
269 }
270
271 static int emac_miiphy_wait(u32 emac_reg)
272 {
273         u32 sta_reg;
274         int i;
275
276         /* wait for completion */
277         i = 0;
278         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);
283                         return -1;
284                 }
285                 udelay(10);
286         } while ((sta_reg & EMAC_STACR_OC) == EMAC_STACR_OC_MASK);
287
288         return 0;
289 }
290
291 static int emac_miiphy_command(u8 addr, u8 reg, int cmd, u16 value)
292 {
293         u32 emac_reg;
294         u32 sta_reg;
295
296         emac_reg = miiphy_getemac_offset(addr);
297
298         /* wait for completion */
299         if (emac_miiphy_wait(emac_reg) != 0)
300                 return -1;
301
302         sta_reg = reg;          /* reg address */
303
304         /* set clock (50MHz) and read flags */
305 #if defined(CONFIG_440GX) || defined(CONFIG_440SPE) || \
306     defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
307     defined(CONFIG_460EX) || defined(CONFIG_460GT) || \
308     defined(CONFIG_405EX)
309 #if defined(CONFIG_IBM_EMAC4_V4)        /* EMAC4 V4 changed bit setting */
310         sta_reg = (sta_reg & ~EMAC_STACR_OP_MASK) | cmd;
311 #else
312         sta_reg |= cmd;
313 #endif
314 #else
315         sta_reg = (sta_reg | cmd) & ~EMAC_STACR_CLK_100MHZ;
316 #endif
317
318         /* Some boards (mainly 405EP based) define the PHY clock freqency fixed */
319         sta_reg = sta_reg | CONFIG_PHY_CLK_FREQ;
320         sta_reg = sta_reg | ((u32)addr << 5);   /* Phy address */
321         sta_reg = sta_reg | EMAC_STACR_OC_MASK; /* new IBM emac v4 */
322         if (cmd == EMAC_STACR_WRITE)
323                 memcpy(&sta_reg, &value, 2);    /* put in data */
324
325         out_be32((void *)EMAC_STACR + emac_reg, sta_reg);
326         debug("%s [%d]: sta_reg=%08x\n", __func__, __LINE__, sta_reg);
327
328         /* wait for completion */
329         if (emac_miiphy_wait(emac_reg) != 0)
330                 return -1;
331
332         debug("%s [%d]: sta_reg=%08x\n", __func__, __LINE__, sta_reg);
333         if ((sta_reg & EMAC_STACR_PHYE) != 0)
334                 return -1;
335
336         return 0;
337 }
338
339 int 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
345         emac_reg = miiphy_getemac_offset(addr);
346
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 }
355
356 /***********************************************************/
357 /* write a phy reg and return the value with a rc           */
358 /***********************************************************/
359
360 int emac4xx_miiphy_write (char *devname, unsigned char addr, unsigned char reg,
361                           unsigned short value)
362 {
363         return emac_miiphy_command(addr, reg, EMAC_STACR_WRITE, value);
364 }