phy.c 19 KB

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