phy.c 19 KB

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