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#include <common.h>
30#include <miiphy.h>
31
32#include <asm/types.h>
33#include <linux/list.h>
34#include <malloc.h>
35#include <net.h>
36
37
38#undef MII_DEBUG
39
40#undef debug
41#ifdef MII_DEBUG
42#define debug(fmt,args...) printf (fmt ,##args)
43#else
44#define debug(fmt,args...)
45#endif
46
47struct mii_dev {
48 struct list_head link;
49 char *name;
50 int (*read) (char *devname, unsigned char addr,
51 unsigned char reg, unsigned short *value);
52 int (*write) (char *devname, unsigned char addr,
53 unsigned char reg, unsigned short value);
54};
55
56static struct list_head mii_devs;
57static struct mii_dev *current_mii;
58
59
60
61
62
63void miiphy_init ()
64{
65 INIT_LIST_HEAD (&mii_devs);
66 current_mii = NULL;
67}
68
69
70
71
72
73void miiphy_register (char *name,
74 int (*read) (char *devname, unsigned char addr,
75 unsigned char reg, unsigned short *value),
76 int (*write) (char *devname, unsigned char addr,
77 unsigned char reg, unsigned short value))
78{
79 struct list_head *entry;
80 struct mii_dev *new_dev;
81 struct mii_dev *miidev;
82 unsigned int name_len;
83
84
85 list_for_each (entry, &mii_devs) {
86 miidev = list_entry (entry, struct mii_dev, link);
87 if (strcmp (miidev->name, name) == 0) {
88 printf ("miiphy_register: non unique device name "
89 "'%s'\n", name);
90 return;
91 }
92 }
93
94
95 name_len = strlen (name);
96 new_dev =
97 (struct mii_dev *)malloc (sizeof (struct mii_dev) + name_len + 1);
98
99 if (new_dev == NULL) {
100 printf ("miiphy_register: cannot allocate memory for '%s'\n",
101 name);
102 return;
103 }
104 memset (new_dev, 0, sizeof (struct mii_dev) + name_len);
105
106
107 INIT_LIST_HEAD (&new_dev->link);
108 new_dev->read = read;
109 new_dev->write = write;
110 new_dev->name = (char *)(new_dev + 1);
111 strncpy (new_dev->name, name, name_len);
112 new_dev->name[name_len] = '\0';
113
114 debug ("miiphy_register: added '%s', read=0x%08lx, write=0x%08lx\n",
115 new_dev->name, new_dev->read, new_dev->write);
116
117
118 list_add_tail (&new_dev->link, &mii_devs);
119
120 if (!current_mii)
121 current_mii = new_dev;
122}
123
124int miiphy_set_current_dev (char *devname)
125{
126 struct list_head *entry;
127 struct mii_dev *dev;
128
129 list_for_each (entry, &mii_devs) {
130 dev = list_entry (entry, struct mii_dev, link);
131
132 if (strcmp (devname, dev->name) == 0) {
133 current_mii = dev;
134 return 0;
135 }
136 }
137
138 printf ("No such device: %s\n", devname);
139 return 1;
140}
141
142char *miiphy_get_current_dev ()
143{
144 if (current_mii)
145 return current_mii->name;
146
147 return NULL;
148}
149
150
151
152
153
154
155
156
157
158int miiphy_read (char *devname, unsigned char addr, unsigned char reg,
159 unsigned short *value)
160{
161 struct list_head *entry;
162 struct mii_dev *dev;
163 int found_dev = 0;
164 int read_ret = 0;
165
166 if (!devname) {
167 printf ("NULL device name!\n");
168 return 1;
169 }
170
171 list_for_each (entry, &mii_devs) {
172 dev = list_entry (entry, struct mii_dev, link);
173
174 if (strcmp (devname, dev->name) == 0) {
175 found_dev = 1;
176 read_ret = dev->read (devname, addr, reg, value);
177 break;
178 }
179 }
180
181 if (found_dev == 0)
182 printf ("No such device: %s\n", devname);
183
184 return ((found_dev) ? read_ret : 1);
185}
186
187
188
189
190
191
192
193
194
195int miiphy_write (char *devname, unsigned char addr, unsigned char reg,
196 unsigned short value)
197{
198 struct list_head *entry;
199 struct mii_dev *dev;
200 int found_dev = 0;
201 int write_ret = 0;
202
203 if (!devname) {
204 printf ("NULL device name!\n");
205 return 1;
206 }
207
208 list_for_each (entry, &mii_devs) {
209 dev = list_entry (entry, struct mii_dev, link);
210
211 if (strcmp (devname, dev->name) == 0) {
212 found_dev = 1;
213 write_ret = dev->write (devname, addr, reg, value);
214 break;
215 }
216 }
217
218 if (found_dev == 0)
219 printf ("No such device: %s\n", devname);
220
221 return ((found_dev) ? write_ret : 1);
222}
223
224
225
226
227
228void miiphy_listdev (void)
229{
230 struct list_head *entry;
231 struct mii_dev *dev;
232
233 puts ("MII devices: ");
234 list_for_each (entry, &mii_devs) {
235 dev = list_entry (entry, struct mii_dev, link);
236 printf ("'%s' ", dev->name);
237 }
238 puts ("\n");
239
240 if (current_mii)
241 printf ("Current device: '%s'\n", current_mii->name);
242}
243
244
245
246
247
248
249
250
251
252
253
254
255int miiphy_info (char *devname, unsigned char addr, unsigned int *oui,
256 unsigned char *model, unsigned char *rev)
257{
258 unsigned int reg = 0;
259 unsigned short tmp;
260
261 if (miiphy_read (devname, addr, PHY_PHYIDR2, &tmp) != 0) {
262 debug ("PHY ID register 2 read failed\n");
263 return (-1);
264 }
265 reg = tmp;
266
267 debug ("PHY_PHYIDR2 @ 0x%x = 0x%04x\n", addr, reg);
268
269 if (reg == 0xFFFF) {
270
271 return (-1);
272 }
273
274 if (miiphy_read (devname, addr, PHY_PHYIDR1, &tmp) != 0) {
275 debug ("PHY ID register 1 read failed\n");
276 return (-1);
277 }
278 reg |= tmp << 16;
279 debug ("PHY_PHYIDR[1,2] @ 0x%x = 0x%08x\n", addr, reg);
280
281 *oui = (reg >> 10);
282 *model = (unsigned char)((reg >> 4) & 0x0000003F);
283 *rev = (unsigned char)(reg & 0x0000000F);
284 return (0);
285}
286
287
288
289
290
291
292
293int miiphy_reset (char *devname, unsigned char addr)
294{
295 unsigned short reg;
296 int loop_cnt;
297
298 if (miiphy_read (devname, addr, PHY_BMCR, ®) != 0) {
299 debug ("PHY status read failed\n");
300 return (-1);
301 }
302 if (miiphy_write (devname, addr, PHY_BMCR, reg | 0x8000) != 0) {
303 debug ("PHY reset failed\n");
304 return (-1);
305 }
306#ifdef CONFIG_PHY_RESET_DELAY
307 udelay (CONFIG_PHY_RESET_DELAY);
308#endif
309
310
311
312
313
314 loop_cnt = 0;
315 reg = 0x8000;
316 while (((reg & 0x8000) != 0) && (loop_cnt++ < 1000000)) {
317 if (miiphy_read (devname, addr, PHY_BMCR, ®) != 0) {
318 debug ("PHY status read failed\n");
319 return (-1);
320 }
321 }
322 if ((reg & 0x8000) == 0) {
323 return (0);
324 } else {
325 puts ("PHY reset timed out\n");
326 return (-1);
327 }
328 return (0);
329}
330
331
332
333
334
335int miiphy_speed (char *devname, unsigned char addr)
336{
337 u16 bmcr, anlpar;
338
339#if defined(CONFIG_PHY_GIGE)
340 u16 btsr;
341
342
343
344
345
346 if (miiphy_is_1000base_x (devname, addr)) {
347 return _1000BASET;
348 }
349
350
351
352
353 if (miiphy_read (devname, addr, PHY_1000BTSR, &btsr)) {
354 printf ("PHY 1000BT status");
355 goto miiphy_read_failed;
356 }
357 if (btsr != 0xFFFF &&
358 (btsr & (PHY_1000BTSR_1000FD | PHY_1000BTSR_1000HD))) {
359 return _1000BASET;
360 }
361#endif
362
363
364 if (miiphy_read (devname, addr, PHY_BMCR, &bmcr)) {
365 printf ("PHY speed");
366 goto miiphy_read_failed;
367 }
368
369 if (bmcr & PHY_BMCR_AUTON) {
370
371 if (miiphy_read (devname, addr, PHY_ANLPAR, &anlpar)) {
372 printf ("PHY AN speed");
373 goto miiphy_read_failed;
374 }
375 return (anlpar & PHY_ANLPAR_100) ? _100BASET : _10BASET;
376 }
377
378 return (bmcr & PHY_BMCR_100MB) ? _100BASET : _10BASET;
379
380 miiphy_read_failed:
381 printf (" read failed, assuming 10BASE-T\n");
382 return _10BASET;
383}
384
385
386
387
388
389int miiphy_duplex (char *devname, unsigned char addr)
390{
391 u16 bmcr, anlpar;
392
393#if defined(CONFIG_PHY_GIGE)
394 u16 btsr;
395
396
397 if (miiphy_is_1000base_x (devname, addr)) {
398
399 if (miiphy_read (devname, addr, PHY_ANLPAR, &anlpar)) {
400 printf ("1000BASE-X PHY AN duplex");
401 goto miiphy_read_failed;
402 }
403 }
404
405
406
407
408 if (miiphy_read (devname, addr, PHY_1000BTSR, &btsr)) {
409 printf ("PHY 1000BT status");
410 goto miiphy_read_failed;
411 }
412 if (btsr != 0xFFFF) {
413 if (btsr & PHY_1000BTSR_1000FD) {
414 return FULL;
415 } else if (btsr & PHY_1000BTSR_1000HD) {
416 return HALF;
417 }
418 }
419#endif
420
421
422 if (miiphy_read (devname, addr, PHY_BMCR, &bmcr)) {
423 puts ("PHY duplex");
424 goto miiphy_read_failed;
425 }
426
427 if (bmcr & PHY_BMCR_AUTON) {
428
429 if (miiphy_read (devname, addr, PHY_ANLPAR, &anlpar)) {
430 puts ("PHY AN duplex");
431 goto miiphy_read_failed;
432 }
433 return (anlpar & (PHY_ANLPAR_10FD | PHY_ANLPAR_TXFD)) ?
434 FULL : HALF;
435 }
436
437 return (bmcr & PHY_BMCR_DPLX) ? FULL : HALF;
438
439 miiphy_read_failed:
440 printf (" read failed, assuming half duplex\n");
441 return HALF;
442}
443
444
445
446
447
448
449int miiphy_is_1000base_x (char *devname, unsigned char addr)
450{
451#if defined(CONFIG_PHY_GIGE)
452 u16 exsr;
453
454 if (miiphy_read (devname, addr, PHY_EXSR, &exsr)) {
455 printf ("PHY extended status read failed, assuming no "
456 "1000BASE-X\n");
457 return 0;
458 }
459 return 0 != (exsr & (PHY_EXSR_1000XF | PHY_EXSR_1000XH));
460#else
461 return 0;
462#endif
463}
464
465#ifdef CONFIG_SYS_FAULT_ECHO_LINK_DOWN
466
467
468
469
470int miiphy_link (char *devname, unsigned char addr)
471{
472 unsigned short reg;
473
474
475 (void)miiphy_read (devname, addr, PHY_BMSR, ®);
476 if (miiphy_read (devname, addr, PHY_BMSR, ®)) {
477 puts ("PHY_BMSR read failed, assuming no link\n");
478 return (0);
479 }
480
481
482 if ((reg & PHY_BMSR_LS) != 0) {
483 return (1);
484 } else {
485 return (0);
486 }
487}
488#endif
489