1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35#if 0
36#define DEBUG
37#endif
38
39#include <common.h>
40#include <asm/processor.h>
41#include <asm/io.h>
42#include <ppc_asm.tmpl>
43#include <commproc.h>
44#include <ppc4xx_enet.h>
45#include <405_mal.h>
46#include <miiphy.h>
47
48#if !defined(CONFIG_PHY_CLK_FREQ)
49#define CONFIG_PHY_CLK_FREQ 0
50#endif
51
52
53
54
55
56void miiphy_dump (char *devname, unsigned char addr)
57{
58 unsigned long i;
59 unsigned short data;
60
61 for (i = 0; i < 0x1A; i++) {
62 if (miiphy_read (devname, addr, i, &data)) {
63 printf ("read error for reg %lx\n", i);
64 return;
65 }
66 printf ("Phy reg %lx ==> %4x\n", i, data);
67
68
69 if (i == 0x07)
70 i = 0x0f;
71
72 }
73}
74
75
76
77
78int phy_setup_aneg (char *devname, unsigned char addr)
79{
80 u16 bmcr;
81
82#if defined(CONFIG_PHY_DYNAMIC_ANEG)
83
84
85
86
87 u16 bmsr;
88#if defined(CONFIG_PHY_GIGE)
89 u16 exsr = 0x0000;
90#endif
91
92 miiphy_read (devname, addr, PHY_BMSR, &bmsr);
93
94#if defined(CONFIG_PHY_GIGE)
95 if (bmsr & PHY_BMSR_EXT_STAT)
96 miiphy_read (devname, addr, PHY_EXSR, &exsr);
97
98 if (exsr & (PHY_EXSR_1000XF | PHY_EXSR_1000XH)) {
99
100 u16 anar = 0x0000;
101
102 if (exsr & PHY_EXSR_1000XF)
103 anar |= PHY_X_ANLPAR_FD;
104
105 if (exsr & PHY_EXSR_1000XH)
106 anar |= PHY_X_ANLPAR_HD;
107
108 miiphy_write (devname, addr, PHY_ANAR, anar);
109 } else
110#endif
111 {
112 u16 anar, btcr;
113
114 miiphy_read (devname, addr, PHY_ANAR, &anar);
115 anar &= ~(0x5000 | PHY_ANLPAR_T4 | PHY_ANLPAR_TXFD |
116 PHY_ANLPAR_TX | PHY_ANLPAR_10FD | PHY_ANLPAR_10);
117
118 miiphy_read (devname, addr, PHY_1000BTCR, &btcr);
119 btcr &= ~(0x00FF | PHY_1000BTCR_1000FD | PHY_1000BTCR_1000HD);
120
121 if (bmsr & PHY_BMSR_100T4)
122 anar |= PHY_ANLPAR_T4;
123
124 if (bmsr & PHY_BMSR_100TXF)
125 anar |= PHY_ANLPAR_TXFD;
126
127 if (bmsr & PHY_BMSR_100TXH)
128 anar |= PHY_ANLPAR_TX;
129
130 if (bmsr & PHY_BMSR_10TF)
131 anar |= PHY_ANLPAR_10FD;
132
133 if (bmsr & PHY_BMSR_10TH)
134 anar |= PHY_ANLPAR_10;
135
136 miiphy_write (devname, addr, PHY_ANAR, anar);
137
138#if defined(CONFIG_PHY_GIGE)
139 if (exsr & PHY_EXSR_1000TF)
140 btcr |= PHY_1000BTCR_1000FD;
141
142 if (exsr & PHY_EXSR_1000TH)
143 btcr |= PHY_1000BTCR_1000HD;
144
145 miiphy_write (devname, addr, PHY_1000BTCR, btcr);
146#endif
147 }
148
149#else
150
151
152
153 u16 adv;
154
155 miiphy_read (devname, addr, PHY_ANAR, &adv);
156 adv |= (PHY_ANLPAR_ACK | PHY_ANLPAR_TXFD | PHY_ANLPAR_TX |
157 PHY_ANLPAR_10FD | PHY_ANLPAR_10);
158 miiphy_write (devname, addr, PHY_ANAR, adv);
159
160 miiphy_read (devname, addr, PHY_1000BTCR, &adv);
161 adv |= (0x0300);
162 miiphy_write (devname, addr, PHY_1000BTCR, adv);
163
164#endif
165
166
167 miiphy_read (devname, addr, PHY_BMCR, &bmcr);
168 bmcr |= (PHY_BMCR_AUTON | PHY_BMCR_RST_NEG);
169 miiphy_write (devname, addr, PHY_BMCR, bmcr);
170
171 return 0;
172}
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188unsigned int miiphy_getemac_offset(u8 addr)
189{
190#if (defined(CONFIG_440) && \
191 !defined(CONFIG_440SP) && !defined(CONFIG_440SPE) && \
192 !defined(CONFIG_460EX) && !defined(CONFIG_460GT)) && \
193 defined(CONFIG_NET_MULTI)
194 unsigned long zmii;
195 unsigned long eoffset;
196
197
198 zmii = in_be32((void *)ZMII0_FER);
199
200 if (zmii & (ZMII_FER_MDI << ZMII_FER_V (0)))
201
202 eoffset = 0;
203
204 else if (zmii & (ZMII_FER_MDI << ZMII_FER_V (1)))
205
206 eoffset = 0x100;
207
208 else if (zmii & (ZMII_FER_MDI << ZMII_FER_V (2)))
209
210 eoffset = 0x400;
211
212 else if (zmii & (ZMII_FER_MDI << ZMII_FER_V (3)))
213
214 eoffset = 0x600;
215
216 else {
217
218
219 zmii |= ZMII_FER_MDI << ZMII_FER_V (0);
220 out_be32((void *)ZMII0_FER, zmii);
221 eoffset = 0;
222
223 zmii = in_be32((void *)EMAC0_MR0);
224 zmii |= EMAC_MR0_SRST;
225 out_be32((void *)EMAC0_MR0, zmii);
226 }
227
228 return (eoffset);
229#else
230
231#if defined(CONFIG_NET_MULTI) && defined(CONFIG_405EX)
232 unsigned long rgmii;
233 int devnum = 1;
234
235 rgmii = in_be32((void *)RGMII_FER);
236 if (rgmii & (1 << (19 - devnum)))
237 return 0x100;
238#endif
239
240#if defined(CONFIG_460EX) || defined(CONFIG_460GT)
241 u32 eoffset = 0;
242
243 switch (addr) {
244#if defined(CONFIG_HAS_ETH1) && defined(CONFIG_GPCS_PHY1_ADDR)
245 case CONFIG_GPCS_PHY1_ADDR:
246 if (addr == EMAC_MR1_IPPA_GET(in_be32((void *)EMAC0_MR1 + 0x100)))
247 eoffset = 0x100;
248 break;
249#endif
250#if defined(CONFIG_HAS_ETH2) && defined(CONFIG_GPCS_PHY2_ADDR)
251 case CONFIG_GPCS_PHY2_ADDR:
252 if (addr == EMAC_MR1_IPPA_GET(in_be32((void *)EMAC0_MR1 + 0x300)))
253 eoffset = 0x300;
254 break;
255#endif
256#if defined(CONFIG_HAS_ETH3) && defined(CONFIG_GPCS_PHY3_ADDR)
257 case CONFIG_GPCS_PHY3_ADDR:
258 if (addr == EMAC_MR1_IPPA_GET(in_be32((void *)EMAC0_MR1 + 0x400)))
259 eoffset = 0x400;
260 break;
261#endif
262 default:
263 eoffset = 0;
264 break;
265 }
266 return eoffset;
267#endif
268
269 return 0;
270#endif
271}
272
273static int emac_miiphy_wait(u32 emac_reg)
274{
275 u32 sta_reg;
276 int i;
277
278
279 i = 0;
280 do {
281 sta_reg = in_be32((void *)EMAC0_STACR + emac_reg);
282 if (i++ > 5) {
283 debug("%s [%d]: Timeout! EMAC0_STACR=0x%0x\n", __func__,
284 __LINE__, sta_reg);
285 return -1;
286 }
287 udelay(10);
288 } while ((sta_reg & EMAC_STACR_OC) == EMAC_STACR_OC_MASK);
289
290 return 0;
291}
292
293static int emac_miiphy_command(u8 addr, u8 reg, int cmd, u16 value)
294{
295 u32 emac_reg;
296 u32 sta_reg;
297
298 emac_reg = miiphy_getemac_offset(addr);
299
300
301 if (emac_miiphy_wait(emac_reg) != 0)
302 return -1;
303
304 sta_reg = reg;
305
306
307#if defined(CONFIG_440GX) || defined(CONFIG_440SPE) || \
308 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
309 defined(CONFIG_460EX) || defined(CONFIG_460GT) || \
310 defined(CONFIG_405EX)
311#if defined(CONFIG_IBM_EMAC4_V4)
312 sta_reg = (sta_reg & ~EMAC_STACR_OP_MASK) | cmd;
313#else
314 sta_reg |= cmd;
315#endif
316#else
317 sta_reg = (sta_reg | cmd) & ~EMAC_STACR_CLK_100MHZ;
318#endif
319
320
321 sta_reg = sta_reg | CONFIG_PHY_CLK_FREQ;
322 sta_reg = sta_reg | ((u32)addr << 5);
323 sta_reg = sta_reg | EMAC_STACR_OC_MASK;
324 if (cmd == EMAC_STACR_WRITE)
325 memcpy(&sta_reg, &value, 2);
326
327 out_be32((void *)EMAC0_STACR + emac_reg, sta_reg);
328 debug("%s [%d]: sta_reg=%08x\n", __func__, __LINE__, sta_reg);
329
330
331 if (emac_miiphy_wait(emac_reg) != 0)
332 return -1;
333
334 debug("%s [%d]: sta_reg=%08x\n", __func__, __LINE__, sta_reg);
335 if ((sta_reg & EMAC_STACR_PHYE) != 0)
336 return -1;
337
338 return 0;
339}
340
341int emac4xx_miiphy_read (char *devname, unsigned char addr, unsigned char reg,
342 unsigned short *value)
343{
344 unsigned long sta_reg;
345 unsigned long emac_reg;
346
347 emac_reg = miiphy_getemac_offset(addr);
348
349 if (emac_miiphy_command(addr, reg, EMAC_STACR_READ, 0) != 0)
350 return -1;
351
352 sta_reg = in_be32((void *)EMAC0_STACR + emac_reg);
353 *value = sta_reg >> 16;
354
355 return 0;
356}
357
358
359
360
361
362int emac4xx_miiphy_write (char *devname, unsigned char addr, unsigned char reg,
363 unsigned short value)
364{
365 return emac_miiphy_command(addr, reg, EMAC_STACR_WRITE, value);
366}
367