phy.c 21 KB

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