]>
Commit | Line | Data |
---|---|---|
5f184715 AF |
1 | /* |
2 | * Generic PHY Management code | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or | |
5 | * modify it under the terms of the GNU General Public License as | |
6 | * published by the Free Software Foundation; either version 2 of | |
7 | * the License, or (at your option) any later version. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program; if not, write to the Free Software | |
16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, | |
17 | * MA 02111-1307 USA | |
18 | * | |
19 | * | |
20 | * Copyright 2011 Freescale Semiconductor, Inc. | |
21 | * author Andy Fleming | |
22 | * | |
23 | * Based loosely off of Linux's PHY Lib | |
24 | */ | |
25 | ||
26 | #include <config.h> | |
27 | #include <common.h> | |
28 | #include <malloc.h> | |
29 | #include <net.h> | |
30 | #include <command.h> | |
31 | #include <miiphy.h> | |
32 | #include <phy.h> | |
33 | #include <errno.h> | |
1adb406b | 34 | #include <linux/err.h> |
5f184715 AF |
35 | |
36 | /* Generic PHY support and helper functions */ | |
37 | ||
38 | /** | |
39 | * genphy_config_advert - sanitize and advertise auto-negotation parameters | |
40 | * @phydev: target phy_device struct | |
41 | * | |
42 | * Description: Writes MII_ADVERTISE with the appropriate values, | |
43 | * after sanitizing the values to make sure we only advertise | |
44 | * what is supported. Returns < 0 on error, 0 if the PHY's advertisement | |
45 | * hasn't changed, and > 0 if it has changed. | |
46 | */ | |
960d70c6 | 47 | static int genphy_config_advert(struct phy_device *phydev) |
5f184715 AF |
48 | { |
49 | u32 advertise; | |
50 | int oldadv, adv; | |
51 | int err, changed = 0; | |
52 | ||
53 | /* Only allow advertising what | |
54 | * this PHY supports */ | |
55 | phydev->advertising &= phydev->supported; | |
56 | advertise = phydev->advertising; | |
57 | ||
58 | /* Setup standard advertisement */ | |
59 | oldadv = adv = phy_read(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE); | |
60 | ||
61 | if (adv < 0) | |
62 | return adv; | |
63 | ||
64 | adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP | | |
65 | ADVERTISE_PAUSE_ASYM); | |
66 | if (advertise & ADVERTISED_10baseT_Half) | |
67 | adv |= ADVERTISE_10HALF; | |
68 | if (advertise & ADVERTISED_10baseT_Full) | |
69 | adv |= ADVERTISE_10FULL; | |
70 | if (advertise & ADVERTISED_100baseT_Half) | |
71 | adv |= ADVERTISE_100HALF; | |
72 | if (advertise & ADVERTISED_100baseT_Full) | |
73 | adv |= ADVERTISE_100FULL; | |
74 | if (advertise & ADVERTISED_Pause) | |
75 | adv |= ADVERTISE_PAUSE_CAP; | |
76 | if (advertise & ADVERTISED_Asym_Pause) | |
77 | adv |= ADVERTISE_PAUSE_ASYM; | |
78 | ||
79 | if (adv != oldadv) { | |
80 | err = phy_write(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE, adv); | |
81 | ||
82 | if (err < 0) | |
83 | return err; | |
84 | changed = 1; | |
85 | } | |
86 | ||
87 | /* Configure gigabit if it's supported */ | |
88 | if (phydev->supported & (SUPPORTED_1000baseT_Half | | |
89 | SUPPORTED_1000baseT_Full)) { | |
90 | oldadv = adv = phy_read(phydev, MDIO_DEVAD_NONE, MII_CTRL1000); | |
91 | ||
92 | if (adv < 0) | |
93 | return adv; | |
94 | ||
95 | adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF); | |
96 | if (advertise & SUPPORTED_1000baseT_Half) | |
97 | adv |= ADVERTISE_1000HALF; | |
98 | if (advertise & SUPPORTED_1000baseT_Full) | |
99 | adv |= ADVERTISE_1000FULL; | |
100 | ||
101 | if (adv != oldadv) { | |
102 | err = phy_write(phydev, MDIO_DEVAD_NONE, MII_CTRL1000, | |
103 | adv); | |
104 | ||
105 | if (err < 0) | |
106 | return err; | |
107 | changed = 1; | |
108 | } | |
109 | } | |
110 | ||
111 | return changed; | |
112 | } | |
113 | ||
114 | ||
115 | /** | |
116 | * genphy_setup_forced - configures/forces speed/duplex from @phydev | |
117 | * @phydev: target phy_device struct | |
118 | * | |
119 | * Description: Configures MII_BMCR to force speed/duplex | |
120 | * to the values in phydev. Assumes that the values are valid. | |
121 | */ | |
960d70c6 | 122 | static int genphy_setup_forced(struct phy_device *phydev) |
5f184715 AF |
123 | { |
124 | int err; | |
125 | int ctl = 0; | |
126 | ||
127 | phydev->pause = phydev->asym_pause = 0; | |
128 | ||
129 | if (SPEED_1000 == phydev->speed) | |
130 | ctl |= BMCR_SPEED1000; | |
131 | else if (SPEED_100 == phydev->speed) | |
132 | ctl |= BMCR_SPEED100; | |
133 | ||
134 | if (DUPLEX_FULL == phydev->duplex) | |
135 | ctl |= BMCR_FULLDPLX; | |
136 | ||
137 | err = phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, ctl); | |
138 | ||
139 | return err; | |
140 | } | |
141 | ||
142 | ||
143 | /** | |
144 | * genphy_restart_aneg - Enable and Restart Autonegotiation | |
145 | * @phydev: target phy_device struct | |
146 | */ | |
147 | int genphy_restart_aneg(struct phy_device *phydev) | |
148 | { | |
149 | int ctl; | |
150 | ||
151 | ctl = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR); | |
152 | ||
153 | if (ctl < 0) | |
154 | return ctl; | |
155 | ||
156 | ctl |= (BMCR_ANENABLE | BMCR_ANRESTART); | |
157 | ||
158 | /* Don't isolate the PHY if we're negotiating */ | |
159 | ctl &= ~(BMCR_ISOLATE); | |
160 | ||
161 | ctl = phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, ctl); | |
162 | ||
163 | return ctl; | |
164 | } | |
165 | ||
166 | ||
167 | /** | |
168 | * genphy_config_aneg - restart auto-negotiation or write BMCR | |
169 | * @phydev: target phy_device struct | |
170 | * | |
171 | * Description: If auto-negotiation is enabled, we configure the | |
172 | * advertising, and then restart auto-negotiation. If it is not | |
173 | * enabled, then we write the BMCR. | |
174 | */ | |
175 | int genphy_config_aneg(struct phy_device *phydev) | |
176 | { | |
177 | int result; | |
178 | ||
179 | if (AUTONEG_ENABLE != phydev->autoneg) | |
180 | return genphy_setup_forced(phydev); | |
181 | ||
182 | result = genphy_config_advert(phydev); | |
183 | ||
184 | if (result < 0) /* error */ | |
185 | return result; | |
186 | ||
187 | if (result == 0) { | |
188 | /* Advertisment hasn't changed, but maybe aneg was never on to | |
189 | * begin with? Or maybe phy was isolated? */ | |
190 | int ctl = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR); | |
191 | ||
192 | if (ctl < 0) | |
193 | return ctl; | |
194 | ||
195 | if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE)) | |
196 | result = 1; /* do restart aneg */ | |
197 | } | |
198 | ||
199 | /* Only restart aneg if we are advertising something different | |
200 | * than we were before. */ | |
201 | if (result > 0) | |
202 | result = genphy_restart_aneg(phydev); | |
203 | ||
204 | return result; | |
205 | } | |
206 | ||
207 | /** | |
208 | * genphy_update_link - update link status in @phydev | |
209 | * @phydev: target phy_device struct | |
210 | * | |
211 | * Description: Update the value in phydev->link to reflect the | |
212 | * current link value. In order to do this, we need to read | |
213 | * the status register twice, keeping the second value. | |
214 | */ | |
215 | int genphy_update_link(struct phy_device *phydev) | |
216 | { | |
217 | unsigned int mii_reg; | |
218 | ||
219 | /* | |
220 | * Wait if the link is up, and autonegotiation is in progress | |
221 | * (ie - we're capable and it's not done) | |
222 | */ | |
223 | mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR); | |
224 | ||
225 | /* | |
226 | * If we already saw the link up, and it hasn't gone down, then | |
227 | * we don't need to wait for autoneg again | |
228 | */ | |
229 | if (phydev->link && mii_reg & BMSR_LSTATUS) | |
230 | return 0; | |
231 | ||
232 | if ((mii_reg & BMSR_ANEGCAPABLE) && !(mii_reg & BMSR_ANEGCOMPLETE)) { | |
233 | int i = 0; | |
234 | ||
235 | printf("%s Waiting for PHY auto negotiation to complete", | |
236 | phydev->dev->name); | |
237 | while (!(mii_reg & BMSR_ANEGCOMPLETE)) { | |
238 | /* | |
239 | * Timeout reached ? | |
240 | */ | |
241 | if (i > PHY_ANEG_TIMEOUT) { | |
242 | printf(" TIMEOUT !\n"); | |
243 | phydev->link = 0; | |
244 | return 0; | |
245 | } | |
246 | ||
247 | if (ctrlc()) { | |
248 | puts("user interrupt!\n"); | |
249 | phydev->link = 0; | |
250 | return -EINTR; | |
251 | } | |
252 | ||
253 | if ((i++ % 500) == 0) | |
254 | printf("."); | |
255 | ||
256 | udelay(1000); /* 1 ms */ | |
257 | mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR); | |
258 | } | |
259 | printf(" done\n"); | |
260 | phydev->link = 1; | |
261 | } else { | |
262 | /* Read the link a second time to clear the latched state */ | |
263 | mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR); | |
264 | ||
265 | if (mii_reg & BMSR_LSTATUS) | |
266 | phydev->link = 1; | |
267 | else | |
268 | phydev->link = 0; | |
269 | } | |
270 | ||
271 | return 0; | |
272 | } | |
273 | ||
274 | /* | |
275 | * Generic function which updates the speed and duplex. If | |
276 | * autonegotiation is enabled, it uses the AND of the link | |
277 | * partner's advertised capabilities and our advertised | |
278 | * capabilities. If autonegotiation is disabled, we use the | |
279 | * appropriate bits in the control register. | |
280 | * | |
281 | * Stolen from Linux's mii.c and phy_device.c | |
282 | */ | |
e2043f5c | 283 | int genphy_parse_link(struct phy_device *phydev) |
5f184715 AF |
284 | { |
285 | int mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR); | |
286 | ||
287 | /* We're using autonegotiation */ | |
288 | if (mii_reg & BMSR_ANEGCAPABLE) { | |
289 | u32 lpa = 0; | |
290 | u32 gblpa = 0; | |
291 | ||
292 | /* Check for gigabit capability */ | |
293 | if (mii_reg & BMSR_ERCAP) { | |
294 | /* We want a list of states supported by | |
295 | * both PHYs in the link | |
296 | */ | |
297 | gblpa = phy_read(phydev, MDIO_DEVAD_NONE, MII_STAT1000); | |
298 | gblpa &= phy_read(phydev, | |
299 | MDIO_DEVAD_NONE, MII_CTRL1000) << 2; | |
300 | } | |
301 | ||
302 | /* Set the baseline so we only have to set them | |
303 | * if they're different | |
304 | */ | |
305 | phydev->speed = SPEED_10; | |
306 | phydev->duplex = DUPLEX_HALF; | |
307 | ||
308 | /* Check the gigabit fields */ | |
309 | if (gblpa & (PHY_1000BTSR_1000FD | PHY_1000BTSR_1000HD)) { | |
310 | phydev->speed = SPEED_1000; | |
311 | ||
312 | if (gblpa & PHY_1000BTSR_1000FD) | |
313 | phydev->duplex = DUPLEX_FULL; | |
314 | ||
315 | /* We're done! */ | |
316 | return 0; | |
317 | } | |
318 | ||
319 | lpa = phy_read(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE); | |
320 | lpa &= phy_read(phydev, MDIO_DEVAD_NONE, MII_LPA); | |
321 | ||
0dcfb0fc | 322 | if (lpa & (LPA_100FULL | LPA_100HALF)) { |
5f184715 AF |
323 | phydev->speed = SPEED_100; |
324 | ||
0dcfb0fc WD |
325 | if (lpa & LPA_100FULL) |
326 | phydev->duplex = DUPLEX_FULL; | |
327 | ||
328 | } else if (lpa & LPA_10FULL) | |
5f184715 AF |
329 | phydev->duplex = DUPLEX_FULL; |
330 | } else { | |
331 | u32 bmcr = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR); | |
332 | ||
333 | phydev->speed = SPEED_10; | |
334 | phydev->duplex = DUPLEX_HALF; | |
335 | ||
336 | if (bmcr & BMCR_FULLDPLX) | |
337 | phydev->duplex = DUPLEX_FULL; | |
338 | ||
339 | if (bmcr & BMCR_SPEED1000) | |
340 | phydev->speed = SPEED_1000; | |
341 | else if (bmcr & BMCR_SPEED100) | |
342 | phydev->speed = SPEED_100; | |
343 | } | |
344 | ||
345 | return 0; | |
346 | } | |
347 | ||
348 | int genphy_config(struct phy_device *phydev) | |
349 | { | |
350 | int val; | |
351 | u32 features; | |
352 | ||
353 | /* For now, I'll claim that the generic driver supports | |
354 | * all possible port types */ | |
355 | features = (SUPPORTED_TP | SUPPORTED_MII | |
356 | | SUPPORTED_AUI | SUPPORTED_FIBRE | | |
357 | SUPPORTED_BNC); | |
358 | ||
359 | /* Do we support autonegotiation? */ | |
360 | val = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR); | |
361 | ||
362 | if (val < 0) | |
363 | return val; | |
364 | ||
365 | if (val & BMSR_ANEGCAPABLE) | |
366 | features |= SUPPORTED_Autoneg; | |
367 | ||
368 | if (val & BMSR_100FULL) | |
369 | features |= SUPPORTED_100baseT_Full; | |
370 | if (val & BMSR_100HALF) | |
371 | features |= SUPPORTED_100baseT_Half; | |
372 | if (val & BMSR_10FULL) | |
373 | features |= SUPPORTED_10baseT_Full; | |
374 | if (val & BMSR_10HALF) | |
375 | features |= SUPPORTED_10baseT_Half; | |
376 | ||
377 | if (val & BMSR_ESTATEN) { | |
378 | val = phy_read(phydev, MDIO_DEVAD_NONE, MII_ESTATUS); | |
379 | ||
380 | if (val < 0) | |
381 | return val; | |
382 | ||
383 | if (val & ESTATUS_1000_TFULL) | |
384 | features |= SUPPORTED_1000baseT_Full; | |
385 | if (val & ESTATUS_1000_THALF) | |
386 | features |= SUPPORTED_1000baseT_Half; | |
387 | } | |
388 | ||
389 | phydev->supported = features; | |
390 | phydev->advertising = features; | |
391 | ||
392 | genphy_config_aneg(phydev); | |
393 | ||
394 | return 0; | |
395 | } | |
396 | ||
397 | int genphy_startup(struct phy_device *phydev) | |
398 | { | |
399 | genphy_update_link(phydev); | |
400 | genphy_parse_link(phydev); | |
401 | ||
402 | return 0; | |
403 | } | |
404 | ||
405 | int genphy_shutdown(struct phy_device *phydev) | |
406 | { | |
407 | return 0; | |
408 | } | |
409 | ||
410 | static struct phy_driver genphy_driver = { | |
411 | .uid = 0xffffffff, | |
412 | .mask = 0xffffffff, | |
413 | .name = "Generic PHY", | |
414 | .features = 0, | |
415 | .config = genphy_config, | |
416 | .startup = genphy_startup, | |
417 | .shutdown = genphy_shutdown, | |
418 | }; | |
419 | ||
420 | static LIST_HEAD(phy_drivers); | |
421 | ||
422 | int phy_init(void) | |
423 | { | |
9082eeac AF |
424 | #ifdef CONFIG_PHY_ATHEROS |
425 | phy_atheros_init(); | |
426 | #endif | |
427 | #ifdef CONFIG_PHY_BROADCOM | |
428 | phy_broadcom_init(); | |
429 | #endif | |
430 | #ifdef CONFIG_PHY_DAVICOM | |
431 | phy_davicom_init(); | |
432 | #endif | |
f485c8a3 MP |
433 | #ifdef CONFIG_PHY_ET1011C |
434 | phy_et1011c_init(); | |
435 | #endif | |
0fae2508 YY |
436 | #ifdef CONFIG_PHY_ICPLUS |
437 | phy_icplus_init(); | |
438 | #endif | |
9082eeac AF |
439 | #ifdef CONFIG_PHY_LXT |
440 | phy_lxt_init(); | |
441 | #endif | |
442 | #ifdef CONFIG_PHY_MARVELL | |
443 | phy_marvell_init(); | |
444 | #endif | |
445 | #ifdef CONFIG_PHY_MICREL | |
446 | phy_micrel_init(); | |
447 | #endif | |
448 | #ifdef CONFIG_PHY_NATSEMI | |
449 | phy_natsemi_init(); | |
450 | #endif | |
451 | #ifdef CONFIG_PHY_REALTEK | |
452 | phy_realtek_init(); | |
453 | #endif | |
5751aa2f NI |
454 | #ifdef CONFIG_PHY_SMSC |
455 | phy_smsc_init(); | |
456 | #endif | |
9082eeac AF |
457 | #ifdef CONFIG_PHY_TERANETICS |
458 | phy_teranetics_init(); | |
459 | #endif | |
460 | #ifdef CONFIG_PHY_VITESSE | |
461 | phy_vitesse_init(); | |
462 | #endif | |
463 | ||
5f184715 AF |
464 | return 0; |
465 | } | |
466 | ||
467 | int phy_register(struct phy_driver *drv) | |
468 | { | |
469 | INIT_LIST_HEAD(&drv->list); | |
470 | list_add_tail(&drv->list, &phy_drivers); | |
471 | ||
472 | return 0; | |
473 | } | |
474 | ||
960d70c6 | 475 | static int phy_probe(struct phy_device *phydev) |
5f184715 AF |
476 | { |
477 | int err = 0; | |
478 | ||
479 | phydev->advertising = phydev->supported = phydev->drv->features; | |
480 | phydev->mmds = phydev->drv->mmds; | |
481 | ||
482 | if (phydev->drv->probe) | |
483 | err = phydev->drv->probe(phydev); | |
484 | ||
485 | return err; | |
486 | } | |
487 | ||
488 | static struct phy_driver *generic_for_interface(phy_interface_t interface) | |
489 | { | |
490 | #ifdef CONFIG_PHYLIB_10G | |
491 | if (is_10g_interface(interface)) | |
492 | return &gen10g_driver; | |
493 | #endif | |
494 | ||
495 | return &genphy_driver; | |
496 | } | |
497 | ||
960d70c6 | 498 | static struct phy_driver *get_phy_driver(struct phy_device *phydev, |
5f184715 AF |
499 | phy_interface_t interface) |
500 | { | |
501 | struct list_head *entry; | |
502 | int phy_id = phydev->phy_id; | |
503 | struct phy_driver *drv = NULL; | |
504 | ||
505 | list_for_each(entry, &phy_drivers) { | |
506 | drv = list_entry(entry, struct phy_driver, list); | |
507 | if ((drv->uid & drv->mask) == (phy_id & drv->mask)) | |
508 | return drv; | |
509 | } | |
510 | ||
511 | /* If we made it here, there's no driver for this PHY */ | |
512 | return generic_for_interface(interface); | |
513 | } | |
514 | ||
960d70c6 KP |
515 | static struct phy_device *phy_device_create(struct mii_dev *bus, int addr, |
516 | int phy_id, | |
517 | phy_interface_t interface) | |
5f184715 AF |
518 | { |
519 | struct phy_device *dev; | |
520 | ||
521 | /* We allocate the device, and initialize the | |
522 | * default values */ | |
523 | dev = malloc(sizeof(*dev)); | |
524 | if (!dev) { | |
525 | printf("Failed to allocate PHY device for %s:%d\n", | |
526 | bus->name, addr); | |
527 | return NULL; | |
528 | } | |
529 | ||
530 | memset(dev, 0, sizeof(*dev)); | |
531 | ||
532 | dev->duplex = -1; | |
533 | dev->link = 1; | |
534 | dev->interface = interface; | |
535 | ||
536 | dev->autoneg = AUTONEG_ENABLE; | |
537 | ||
538 | dev->addr = addr; | |
539 | dev->phy_id = phy_id; | |
540 | dev->bus = bus; | |
541 | ||
542 | dev->drv = get_phy_driver(dev, interface); | |
543 | ||
544 | phy_probe(dev); | |
545 | ||
546 | bus->phymap[addr] = dev; | |
547 | ||
548 | return dev; | |
549 | } | |
550 | ||
551 | /** | |
552 | * get_phy_id - reads the specified addr for its ID. | |
553 | * @bus: the target MII bus | |
554 | * @addr: PHY address on the MII bus | |
555 | * @phy_id: where to store the ID retrieved. | |
556 | * | |
557 | * Description: Reads the ID registers of the PHY at @addr on the | |
558 | * @bus, stores it in @phy_id and returns zero on success. | |
559 | */ | |
960d70c6 | 560 | static int get_phy_id(struct mii_dev *bus, int addr, int devad, u32 *phy_id) |
5f184715 AF |
561 | { |
562 | int phy_reg; | |
563 | ||
564 | /* Grab the bits from PHYIR1, and put them | |
565 | * in the upper half */ | |
566 | phy_reg = bus->read(bus, addr, devad, MII_PHYSID1); | |
567 | ||
568 | if (phy_reg < 0) | |
569 | return -EIO; | |
570 | ||
571 | *phy_id = (phy_reg & 0xffff) << 16; | |
572 | ||
573 | /* Grab the bits from PHYIR2, and put them in the lower half */ | |
574 | phy_reg = bus->read(bus, addr, devad, MII_PHYSID2); | |
575 | ||
576 | if (phy_reg < 0) | |
577 | return -EIO; | |
578 | ||
579 | *phy_id |= (phy_reg & 0xffff); | |
580 | ||
581 | return 0; | |
582 | } | |
583 | ||
1adb406b TK |
584 | static struct phy_device *create_phy_by_mask(struct mii_dev *bus, |
585 | unsigned phy_mask, int devad, phy_interface_t interface) | |
586 | { | |
587 | u32 phy_id = 0xffffffff; | |
588 | while (phy_mask) { | |
589 | int addr = ffs(phy_mask) - 1; | |
590 | int r = get_phy_id(bus, addr, devad, &phy_id); | |
591 | if (r < 0) | |
592 | return ERR_PTR(r); | |
593 | /* If the PHY ID is mostly f's, we didn't find anything */ | |
594 | if ((phy_id & 0x1fffffff) != 0x1fffffff) | |
595 | return phy_device_create(bus, addr, phy_id, interface); | |
596 | phy_mask &= ~(1 << addr); | |
597 | } | |
598 | return NULL; | |
599 | } | |
600 | ||
601 | static struct phy_device *search_for_existing_phy(struct mii_dev *bus, | |
602 | unsigned phy_mask, phy_interface_t interface) | |
603 | { | |
604 | /* If we have one, return the existing device, with new interface */ | |
605 | while (phy_mask) { | |
606 | int addr = ffs(phy_mask) - 1; | |
607 | if (bus->phymap[addr]) { | |
608 | bus->phymap[addr]->interface = interface; | |
609 | return bus->phymap[addr]; | |
610 | } | |
611 | phy_mask &= ~(1 << addr); | |
612 | } | |
613 | return NULL; | |
614 | } | |
615 | ||
616 | static struct phy_device *get_phy_device_by_mask(struct mii_dev *bus, | |
617 | unsigned phy_mask, phy_interface_t interface) | |
618 | { | |
619 | int i; | |
620 | struct phy_device *phydev; | |
621 | ||
622 | phydev = search_for_existing_phy(bus, phy_mask, interface); | |
623 | if (phydev) | |
624 | return phydev; | |
625 | /* Try Standard (ie Clause 22) access */ | |
626 | /* Otherwise we have to try Clause 45 */ | |
627 | for (i = 0; i < 5; i++) { | |
628 | phydev = create_phy_by_mask(bus, phy_mask, | |
629 | i ? i : MDIO_DEVAD_NONE, interface); | |
630 | if (IS_ERR(phydev)) | |
631 | return NULL; | |
632 | if (phydev) | |
633 | return phydev; | |
634 | } | |
635 | printf("Phy not found\n"); | |
636 | return phy_device_create(bus, ffs(phy_mask) - 1, 0xffffffff, interface); | |
637 | } | |
638 | ||
5f184715 AF |
639 | /** |
640 | * get_phy_device - reads the specified PHY device and returns its @phy_device struct | |
641 | * @bus: the target MII bus | |
642 | * @addr: PHY address on the MII bus | |
643 | * | |
644 | * Description: Reads the ID registers of the PHY at @addr on the | |
645 | * @bus, then allocates and returns the phy_device to represent it. | |
646 | */ | |
960d70c6 KP |
647 | static struct phy_device *get_phy_device(struct mii_dev *bus, int addr, |
648 | phy_interface_t interface) | |
5f184715 | 649 | { |
1adb406b | 650 | return get_phy_device_by_mask(bus, 1 << addr, interface); |
5f184715 AF |
651 | } |
652 | ||
653 | int phy_reset(struct phy_device *phydev) | |
654 | { | |
655 | int reg; | |
656 | int timeout = 500; | |
657 | int devad = MDIO_DEVAD_NONE; | |
658 | ||
659 | #ifdef CONFIG_PHYLIB_10G | |
660 | /* If it's 10G, we need to issue reset through one of the MMDs */ | |
661 | if (is_10g_interface(phydev->interface)) { | |
662 | if (!phydev->mmds) | |
663 | gen10g_discover_mmds(phydev); | |
664 | ||
665 | devad = ffs(phydev->mmds) - 1; | |
666 | } | |
667 | #endif | |
668 | ||
669 | reg = phy_read(phydev, devad, MII_BMCR); | |
670 | if (reg < 0) { | |
671 | debug("PHY status read failed\n"); | |
672 | return -1; | |
673 | } | |
674 | ||
675 | reg |= BMCR_RESET; | |
676 | ||
677 | if (phy_write(phydev, devad, MII_BMCR, reg) < 0) { | |
678 | debug("PHY reset failed\n"); | |
679 | return -1; | |
680 | } | |
681 | ||
682 | #ifdef CONFIG_PHY_RESET_DELAY | |
683 | udelay(CONFIG_PHY_RESET_DELAY); /* Intel LXT971A needs this */ | |
684 | #endif | |
685 | /* | |
686 | * Poll the control register for the reset bit to go to 0 (it is | |
687 | * auto-clearing). This should happen within 0.5 seconds per the | |
688 | * IEEE spec. | |
689 | */ | |
690 | while ((reg & BMCR_RESET) && timeout--) { | |
691 | reg = phy_read(phydev, devad, MII_BMCR); | |
692 | ||
693 | if (reg < 0) { | |
694 | debug("PHY status read failed\n"); | |
695 | return -1; | |
696 | } | |
697 | udelay(1000); | |
698 | } | |
699 | ||
700 | if (reg & BMCR_RESET) { | |
701 | puts("PHY reset timed out\n"); | |
702 | return -1; | |
703 | } | |
704 | ||
705 | return 0; | |
706 | } | |
707 | ||
708 | int miiphy_reset(const char *devname, unsigned char addr) | |
709 | { | |
710 | struct mii_dev *bus = miiphy_get_dev_by_name(devname); | |
711 | struct phy_device *phydev; | |
712 | ||
713 | /* | |
714 | * miiphy_reset was only used on standard PHYs, so we'll fake it here. | |
715 | * If later code tries to connect with the right interface, this will | |
716 | * be corrected by get_phy_device in phy_connect() | |
717 | */ | |
718 | phydev = get_phy_device(bus, addr, PHY_INTERFACE_MODE_MII); | |
719 | ||
720 | return phy_reset(phydev); | |
721 | } | |
722 | ||
1adb406b TK |
723 | struct phy_device *phy_find_by_mask(struct mii_dev *bus, unsigned phy_mask, |
724 | phy_interface_t interface) | |
5f184715 | 725 | { |
5f184715 | 726 | /* Reset the bus */ |
e3a77218 VZ |
727 | if (bus->reset) |
728 | bus->reset(bus); | |
5f184715 AF |
729 | |
730 | /* Wait 15ms to make sure the PHY has come out of hard reset */ | |
731 | udelay(15000); | |
1adb406b TK |
732 | return get_phy_device_by_mask(bus, phy_mask, interface); |
733 | } | |
5f184715 | 734 | |
1adb406b TK |
735 | void phy_connect_dev(struct phy_device *phydev, struct eth_device *dev) |
736 | { | |
5f184715 AF |
737 | /* Soft Reset the PHY */ |
738 | phy_reset(phydev); | |
1adb406b | 739 | if (phydev->dev) { |
5f184715 | 740 | printf("%s:%d is connected to %s. Reconnecting to %s\n", |
1adb406b TK |
741 | phydev->bus->name, phydev->addr, |
742 | phydev->dev->name, dev->name); | |
743 | } | |
5f184715 | 744 | phydev->dev = dev; |
b91a9d9d | 745 | debug("%s connected to %s\n", dev->name, phydev->drv->name); |
1adb406b TK |
746 | } |
747 | ||
748 | struct phy_device *phy_connect(struct mii_dev *bus, int addr, | |
749 | struct eth_device *dev, phy_interface_t interface) | |
750 | { | |
751 | struct phy_device *phydev; | |
5f184715 | 752 | |
1adb406b TK |
753 | phydev = phy_find_by_mask(bus, 1 << addr, interface); |
754 | if (phydev) | |
755 | phy_connect_dev(phydev, dev); | |
756 | else | |
757 | printf("Could not get PHY for %s: addr %d\n", bus->name, addr); | |
5f184715 AF |
758 | return phydev; |
759 | } | |
760 | ||
6e5b9ac0 TT |
761 | /* |
762 | * Start the PHY. Returns 0 on success, or a negative error code. | |
763 | */ | |
5f184715 AF |
764 | int phy_startup(struct phy_device *phydev) |
765 | { | |
766 | if (phydev->drv->startup) | |
6e5b9ac0 | 767 | return phydev->drv->startup(phydev); |
5f184715 AF |
768 | |
769 | return 0; | |
770 | } | |
771 | ||
772 | static int __board_phy_config(struct phy_device *phydev) | |
773 | { | |
9fafe7da TK |
774 | if (phydev->drv->config) |
775 | return phydev->drv->config(phydev); | |
5f184715 AF |
776 | return 0; |
777 | } | |
778 | ||
779 | int board_phy_config(struct phy_device *phydev) | |
780 | __attribute__((weak, alias("__board_phy_config"))); | |
781 | ||
782 | int phy_config(struct phy_device *phydev) | |
783 | { | |
5f184715 AF |
784 | /* Invoke an optional board-specific helper */ |
785 | board_phy_config(phydev); | |
786 | ||
787 | return 0; | |
788 | } | |
789 | ||
790 | int phy_shutdown(struct phy_device *phydev) | |
791 | { | |
792 | if (phydev->drv->shutdown) | |
793 | phydev->drv->shutdown(phydev); | |
794 | ||
795 | return 0; | |
796 | } |