phy.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224
  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 <common.h>
  11. #include <console.h>
  12. #include <dm.h>
  13. #include <log.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 <asm/global_data.h>
  21. #include <dm/of_extra.h>
  22. #include <linux/bitops.h>
  23. #include <linux/delay.h>
  24. #include <linux/err.h>
  25. #include <linux/compiler.h>
  26. DECLARE_GLOBAL_DATA_PTR;
  27. /* Generic PHY support and helper functions */
  28. /**
  29. * genphy_config_advert - sanitize and advertise auto-negotiation parameters
  30. * @phydev: target phy_device struct
  31. *
  32. * Description: Writes MII_ADVERTISE with the appropriate values,
  33. * after sanitizing the values to make sure we only advertise
  34. * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
  35. * hasn't changed, and > 0 if it has changed.
  36. */
  37. static int genphy_config_advert(struct phy_device *phydev)
  38. {
  39. u32 advertise;
  40. int oldadv, adv, bmsr;
  41. int err, changed = 0;
  42. /* Only allow advertising what this PHY supports */
  43. phydev->advertising &= phydev->supported;
  44. advertise = phydev->advertising;
  45. /* Setup standard advertisement */
  46. adv = phy_read(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE);
  47. oldadv = adv;
  48. if (adv < 0)
  49. return adv;
  50. adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
  51. ADVERTISE_PAUSE_ASYM);
  52. if (advertise & ADVERTISED_10baseT_Half)
  53. adv |= ADVERTISE_10HALF;
  54. if (advertise & ADVERTISED_10baseT_Full)
  55. adv |= ADVERTISE_10FULL;
  56. if (advertise & ADVERTISED_100baseT_Half)
  57. adv |= ADVERTISE_100HALF;
  58. if (advertise & ADVERTISED_100baseT_Full)
  59. adv |= ADVERTISE_100FULL;
  60. if (advertise & ADVERTISED_Pause)
  61. adv |= ADVERTISE_PAUSE_CAP;
  62. if (advertise & ADVERTISED_Asym_Pause)
  63. adv |= ADVERTISE_PAUSE_ASYM;
  64. if (advertise & ADVERTISED_1000baseX_Half)
  65. adv |= ADVERTISE_1000XHALF;
  66. if (advertise & ADVERTISED_1000baseX_Full)
  67. adv |= ADVERTISE_1000XFULL;
  68. if (adv != oldadv) {
  69. err = phy_write(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE, adv);
  70. if (err < 0)
  71. return err;
  72. changed = 1;
  73. }
  74. bmsr = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
  75. if (bmsr < 0)
  76. return bmsr;
  77. /* Per 802.3-2008, Section 22.2.4.2.16 Extended status all
  78. * 1000Mbits/sec capable PHYs shall have the BMSR_ESTATEN bit set to a
  79. * logical 1.
  80. */
  81. if (!(bmsr & BMSR_ESTATEN))
  82. return changed;
  83. /* Configure gigabit if it's supported */
  84. adv = phy_read(phydev, MDIO_DEVAD_NONE, MII_CTRL1000);
  85. oldadv = adv;
  86. if (adv < 0)
  87. return adv;
  88. adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
  89. if (phydev->supported & (SUPPORTED_1000baseT_Half |
  90. SUPPORTED_1000baseT_Full)) {
  91. if (advertise & SUPPORTED_1000baseT_Half)
  92. adv |= ADVERTISE_1000HALF;
  93. if (advertise & SUPPORTED_1000baseT_Full)
  94. adv |= ADVERTISE_1000FULL;
  95. }
  96. if (adv != oldadv)
  97. changed = 1;
  98. err = phy_write(phydev, MDIO_DEVAD_NONE, MII_CTRL1000, adv);
  99. if (err < 0)
  100. return err;
  101. return changed;
  102. }
  103. /**
  104. * genphy_setup_forced - configures/forces speed/duplex from @phydev
  105. * @phydev: target phy_device struct
  106. *
  107. * Description: Configures MII_BMCR to force speed/duplex
  108. * to the values in phydev. Assumes that the values are valid.
  109. */
  110. static int genphy_setup_forced(struct phy_device *phydev)
  111. {
  112. int err;
  113. int ctl = BMCR_ANRESTART;
  114. phydev->pause = 0;
  115. phydev->asym_pause = 0;
  116. if (phydev->speed == SPEED_1000)
  117. ctl |= BMCR_SPEED1000;
  118. else if (phydev->speed == SPEED_100)
  119. ctl |= BMCR_SPEED100;
  120. if (phydev->duplex == DUPLEX_FULL)
  121. ctl |= BMCR_FULLDPLX;
  122. err = phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, ctl);
  123. return err;
  124. }
  125. /**
  126. * genphy_restart_aneg - Enable and Restart Autonegotiation
  127. * @phydev: target phy_device struct
  128. */
  129. int genphy_restart_aneg(struct phy_device *phydev)
  130. {
  131. int ctl;
  132. ctl = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
  133. if (ctl < 0)
  134. return ctl;
  135. ctl |= (BMCR_ANENABLE | BMCR_ANRESTART);
  136. /* Don't isolate the PHY if we're negotiating */
  137. ctl &= ~(BMCR_ISOLATE);
  138. ctl = phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, ctl);
  139. return ctl;
  140. }
  141. /**
  142. * genphy_config_aneg - restart auto-negotiation or write BMCR
  143. * @phydev: target phy_device struct
  144. *
  145. * Description: If auto-negotiation is enabled, we configure the
  146. * advertising, and then restart auto-negotiation. If it is not
  147. * enabled, then we write the BMCR.
  148. */
  149. int genphy_config_aneg(struct phy_device *phydev)
  150. {
  151. int result;
  152. if (phydev->autoneg != AUTONEG_ENABLE)
  153. return genphy_setup_forced(phydev);
  154. result = genphy_config_advert(phydev);
  155. if (result < 0) /* error */
  156. return result;
  157. if (result == 0) {
  158. /*
  159. * Advertisment hasn't changed, but maybe aneg was never on to
  160. * begin with? Or maybe phy was isolated?
  161. */
  162. int ctl = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
  163. if (ctl < 0)
  164. return ctl;
  165. if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
  166. result = 1; /* do restart aneg */
  167. }
  168. /*
  169. * Only restart aneg if we are advertising something different
  170. * than we were before.
  171. */
  172. if (result > 0)
  173. result = genphy_restart_aneg(phydev);
  174. return result;
  175. }
  176. /**
  177. * genphy_update_link - update link status in @phydev
  178. * @phydev: target phy_device struct
  179. *
  180. * Description: Update the value in phydev->link to reflect the
  181. * current link value. In order to do this, we need to read
  182. * the status register twice, keeping the second value.
  183. */
  184. int genphy_update_link(struct phy_device *phydev)
  185. {
  186. unsigned int mii_reg;
  187. /*
  188. * Wait if the link is up, and autonegotiation is in progress
  189. * (ie - we're capable and it's not done)
  190. */
  191. mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
  192. /*
  193. * If we already saw the link up, and it hasn't gone down, then
  194. * we don't need to wait for autoneg again
  195. */
  196. if (phydev->link && mii_reg & BMSR_LSTATUS)
  197. return 0;
  198. if ((phydev->autoneg == AUTONEG_ENABLE) &&
  199. !(mii_reg & BMSR_ANEGCOMPLETE)) {
  200. int i = 0;
  201. printf("%s Waiting for PHY auto negotiation to complete",
  202. phydev->dev->name);
  203. while (!(mii_reg & BMSR_ANEGCOMPLETE)) {
  204. /*
  205. * Timeout reached ?
  206. */
  207. if (i > (PHY_ANEG_TIMEOUT / 50)) {
  208. printf(" TIMEOUT !\n");
  209. phydev->link = 0;
  210. return -ETIMEDOUT;
  211. }
  212. if (ctrlc()) {
  213. puts("user interrupt!\n");
  214. phydev->link = 0;
  215. return -EINTR;
  216. }
  217. if ((i++ % 10) == 0)
  218. printf(".");
  219. mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
  220. mdelay(50); /* 50 ms */
  221. }
  222. printf(" done\n");
  223. phydev->link = 1;
  224. } else {
  225. /* Read the link a second time to clear the latched state */
  226. mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
  227. if (mii_reg & BMSR_LSTATUS)
  228. phydev->link = 1;
  229. else
  230. phydev->link = 0;
  231. }
  232. return 0;
  233. }
  234. /*
  235. * Generic function which updates the speed and duplex. If
  236. * autonegotiation is enabled, it uses the AND of the link
  237. * partner's advertised capabilities and our advertised
  238. * capabilities. If autonegotiation is disabled, we use the
  239. * appropriate bits in the control register.
  240. *
  241. * Stolen from Linux's mii.c and phy_device.c
  242. */
  243. int genphy_parse_link(struct phy_device *phydev)
  244. {
  245. int mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
  246. /* We're using autonegotiation */
  247. if (phydev->autoneg == AUTONEG_ENABLE) {
  248. u32 lpa = 0;
  249. int gblpa = 0;
  250. u32 estatus = 0;
  251. /* Check for gigabit capability */
  252. if (phydev->supported & (SUPPORTED_1000baseT_Full |
  253. SUPPORTED_1000baseT_Half)) {
  254. /* We want a list of states supported by
  255. * both PHYs in the link
  256. */
  257. gblpa = phy_read(phydev, MDIO_DEVAD_NONE, MII_STAT1000);
  258. if (gblpa < 0) {
  259. debug("Could not read MII_STAT1000. ");
  260. debug("Ignoring gigabit capability\n");
  261. gblpa = 0;
  262. }
  263. gblpa &= phy_read(phydev,
  264. MDIO_DEVAD_NONE, MII_CTRL1000) << 2;
  265. }
  266. /* Set the baseline so we only have to set them
  267. * if they're different
  268. */
  269. phydev->speed = SPEED_10;
  270. phydev->duplex = DUPLEX_HALF;
  271. /* Check the gigabit fields */
  272. if (gblpa & (PHY_1000BTSR_1000FD | PHY_1000BTSR_1000HD)) {
  273. phydev->speed = SPEED_1000;
  274. if (gblpa & PHY_1000BTSR_1000FD)
  275. phydev->duplex = DUPLEX_FULL;
  276. /* We're done! */
  277. return 0;
  278. }
  279. lpa = phy_read(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE);
  280. lpa &= phy_read(phydev, MDIO_DEVAD_NONE, MII_LPA);
  281. if (lpa & (LPA_100FULL | LPA_100HALF)) {
  282. phydev->speed = SPEED_100;
  283. if (lpa & LPA_100FULL)
  284. phydev->duplex = DUPLEX_FULL;
  285. } else if (lpa & LPA_10FULL) {
  286. phydev->duplex = DUPLEX_FULL;
  287. }
  288. /*
  289. * Extended status may indicate that the PHY supports
  290. * 1000BASE-T/X even though the 1000BASE-T registers
  291. * are missing. In this case we can't tell whether the
  292. * peer also supports it, so we only check extended
  293. * status if the 1000BASE-T registers are actually
  294. * missing.
  295. */
  296. if ((mii_reg & BMSR_ESTATEN) && !(mii_reg & BMSR_ERCAP))
  297. estatus = phy_read(phydev, MDIO_DEVAD_NONE,
  298. MII_ESTATUS);
  299. if (estatus & (ESTATUS_1000_XFULL | ESTATUS_1000_XHALF |
  300. ESTATUS_1000_TFULL | ESTATUS_1000_THALF)) {
  301. phydev->speed = SPEED_1000;
  302. if (estatus & (ESTATUS_1000_XFULL | ESTATUS_1000_TFULL))
  303. phydev->duplex = DUPLEX_FULL;
  304. }
  305. } else {
  306. u32 bmcr = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
  307. phydev->speed = SPEED_10;
  308. phydev->duplex = DUPLEX_HALF;
  309. if (bmcr & BMCR_FULLDPLX)
  310. phydev->duplex = DUPLEX_FULL;
  311. if (bmcr & BMCR_SPEED1000)
  312. phydev->speed = SPEED_1000;
  313. else if (bmcr & BMCR_SPEED100)
  314. phydev->speed = SPEED_100;
  315. }
  316. return 0;
  317. }
  318. int genphy_config(struct phy_device *phydev)
  319. {
  320. int val;
  321. u32 features;
  322. features = (SUPPORTED_TP | SUPPORTED_MII
  323. | SUPPORTED_AUI | SUPPORTED_FIBRE |
  324. SUPPORTED_BNC);
  325. /* Do we support autonegotiation? */
  326. val = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
  327. if (val < 0)
  328. return val;
  329. if (val & BMSR_ANEGCAPABLE)
  330. features |= SUPPORTED_Autoneg;
  331. if (val & BMSR_100FULL)
  332. features |= SUPPORTED_100baseT_Full;
  333. if (val & BMSR_100HALF)
  334. features |= SUPPORTED_100baseT_Half;
  335. if (val & BMSR_10FULL)
  336. features |= SUPPORTED_10baseT_Full;
  337. if (val & BMSR_10HALF)
  338. features |= SUPPORTED_10baseT_Half;
  339. if (val & BMSR_ESTATEN) {
  340. val = phy_read(phydev, MDIO_DEVAD_NONE, MII_ESTATUS);
  341. if (val < 0)
  342. return val;
  343. if (val & ESTATUS_1000_TFULL)
  344. features |= SUPPORTED_1000baseT_Full;
  345. if (val & ESTATUS_1000_THALF)
  346. features |= SUPPORTED_1000baseT_Half;
  347. if (val & ESTATUS_1000_XFULL)
  348. features |= SUPPORTED_1000baseX_Full;
  349. if (val & ESTATUS_1000_XHALF)
  350. features |= SUPPORTED_1000baseX_Half;
  351. }
  352. phydev->supported &= features;
  353. phydev->advertising &= features;
  354. genphy_config_aneg(phydev);
  355. return 0;
  356. }
  357. int genphy_startup(struct phy_device *phydev)
  358. {
  359. int ret;
  360. ret = genphy_update_link(phydev);
  361. if (ret)
  362. return ret;
  363. return genphy_parse_link(phydev);
  364. }
  365. int genphy_shutdown(struct phy_device *phydev)
  366. {
  367. return 0;
  368. }
  369. U_BOOT_PHY_DRIVER(genphy) = {
  370. .uid = 0xffffffff,
  371. .mask = 0xffffffff,
  372. .name = "Generic PHY",
  373. .features = PHY_GBIT_FEATURES | SUPPORTED_MII |
  374. SUPPORTED_AUI | SUPPORTED_FIBRE |
  375. SUPPORTED_BNC,
  376. .config = genphy_config,
  377. .startup = genphy_startup,
  378. .shutdown = genphy_shutdown,
  379. };
  380. #ifdef CONFIG_NEEDS_MANUAL_RELOC
  381. int phy_init(void)
  382. {
  383. const int ll_n_ents = ll_entry_count(struct phy_driver, phy_driver);
  384. struct phy_driver *drv, *ll_entry;
  385. /* Perform manual relocation on linker list based PHY drivers */
  386. ll_entry = ll_entry_start(struct phy_driver, phy_driver);
  387. for (drv = ll_entry; drv != ll_entry + ll_n_ents; drv++) {
  388. if (drv->probe)
  389. drv->probe += gd->reloc_off;
  390. if (drv->config)
  391. drv->config += gd->reloc_off;
  392. if (drv->startup)
  393. drv->startup += gd->reloc_off;
  394. if (drv->shutdown)
  395. drv->shutdown += gd->reloc_off;
  396. if (drv->readext)
  397. drv->readext += gd->reloc_off;
  398. if (drv->writeext)
  399. drv->writeext += gd->reloc_off;
  400. if (drv->read_mmd)
  401. drv->read_mmd += gd->reloc_off;
  402. if (drv->write_mmd)
  403. drv->write_mmd += gd->reloc_off;
  404. }
  405. return 0;
  406. }
  407. #endif
  408. int phy_set_supported(struct phy_device *phydev, u32 max_speed)
  409. {
  410. /* The default values for phydev->supported are provided by the PHY
  411. * driver "features" member, we want to reset to sane defaults first
  412. * before supporting higher speeds.
  413. */
  414. phydev->supported &= PHY_DEFAULT_FEATURES;
  415. switch (max_speed) {
  416. default:
  417. return -ENOTSUPP;
  418. case SPEED_1000:
  419. phydev->supported |= PHY_1000BT_FEATURES;
  420. /* fall through */
  421. case SPEED_100:
  422. phydev->supported |= PHY_100BT_FEATURES;
  423. /* fall through */
  424. case SPEED_10:
  425. phydev->supported |= PHY_10BT_FEATURES;
  426. }
  427. return 0;
  428. }
  429. static int phy_probe(struct phy_device *phydev)
  430. {
  431. int err = 0;
  432. phydev->advertising = phydev->drv->features;
  433. phydev->supported = phydev->drv->features;
  434. phydev->mmds = phydev->drv->mmds;
  435. if (phydev->drv->probe)
  436. err = phydev->drv->probe(phydev);
  437. return err;
  438. }
  439. static struct phy_driver *generic_for_phy(struct phy_device *phydev)
  440. {
  441. #ifdef CONFIG_PHYLIB_10G
  442. if (phydev->is_c45)
  443. return ll_entry_get(struct phy_driver, gen10g, phy_driver);
  444. #endif
  445. return ll_entry_get(struct phy_driver, genphy, phy_driver);
  446. }
  447. static struct phy_driver *get_phy_driver(struct phy_device *phydev)
  448. {
  449. const int ll_n_ents = ll_entry_count(struct phy_driver, phy_driver);
  450. int phy_id = phydev->phy_id;
  451. struct phy_driver *ll_entry;
  452. struct phy_driver *drv;
  453. ll_entry = ll_entry_start(struct phy_driver, phy_driver);
  454. for (drv = ll_entry; drv != ll_entry + ll_n_ents; drv++)
  455. if ((drv->uid & drv->mask) == (phy_id & drv->mask))
  456. return drv;
  457. /* If we made it here, there's no driver for this PHY */
  458. return generic_for_phy(phydev);
  459. }
  460. struct phy_device *phy_device_create(struct mii_dev *bus, int addr,
  461. u32 phy_id, bool is_c45)
  462. {
  463. struct phy_device *dev;
  464. /*
  465. * We allocate the device, and initialize the
  466. * default values
  467. */
  468. dev = malloc(sizeof(*dev));
  469. if (!dev) {
  470. printf("Failed to allocate PHY device for %s:%d\n",
  471. bus ? bus->name : "(null bus)", addr);
  472. return NULL;
  473. }
  474. memset(dev, 0, sizeof(*dev));
  475. dev->duplex = -1;
  476. dev->link = 0;
  477. dev->interface = PHY_INTERFACE_MODE_NA;
  478. dev->node = ofnode_null();
  479. dev->autoneg = AUTONEG_ENABLE;
  480. dev->addr = addr;
  481. dev->phy_id = phy_id;
  482. dev->is_c45 = is_c45;
  483. dev->bus = bus;
  484. dev->drv = get_phy_driver(dev);
  485. if (phy_probe(dev)) {
  486. printf("%s, PHY probe failed\n", __func__);
  487. return NULL;
  488. }
  489. if (addr >= 0 && addr < PHY_MAX_ADDR && phy_id != PHY_FIXED_ID)
  490. bus->phymap[addr] = dev;
  491. return dev;
  492. }
  493. /**
  494. * get_phy_id - reads the specified addr for its ID.
  495. * @bus: the target MII bus
  496. * @addr: PHY address on the MII bus
  497. * @phy_id: where to store the ID retrieved.
  498. *
  499. * Description: Reads the ID registers of the PHY at @addr on the
  500. * @bus, stores it in @phy_id and returns zero on success.
  501. */
  502. int __weak get_phy_id(struct mii_dev *bus, int addr, int devad, u32 *phy_id)
  503. {
  504. int phy_reg;
  505. /*
  506. * Grab the bits from PHYIR1, and put them
  507. * in the upper half
  508. */
  509. phy_reg = bus->read(bus, addr, devad, MII_PHYSID1);
  510. if (phy_reg < 0)
  511. return -EIO;
  512. *phy_id = (phy_reg & 0xffff) << 16;
  513. /* Grab the bits from PHYIR2, and put them in the lower half */
  514. phy_reg = bus->read(bus, addr, devad, MII_PHYSID2);
  515. if (phy_reg < 0)
  516. return -EIO;
  517. *phy_id |= (phy_reg & 0xffff);
  518. return 0;
  519. }
  520. static struct phy_device *create_phy_by_mask(struct mii_dev *bus,
  521. uint phy_mask, int devad)
  522. {
  523. u32 phy_id = 0xffffffff;
  524. bool is_c45;
  525. while (phy_mask) {
  526. int addr = ffs(phy_mask) - 1;
  527. int r = get_phy_id(bus, addr, devad, &phy_id);
  528. /*
  529. * If the PHY ID is flat 0 we ignore it. There are C45 PHYs
  530. * that return all 0s for C22 reads (like Aquantia AQR112) and
  531. * there are C22 PHYs that return all 0s for C45 reads (like
  532. * Atheros AR8035).
  533. */
  534. if (r == 0 && phy_id == 0)
  535. goto next;
  536. /* If the PHY ID is mostly f's, we didn't find anything */
  537. if (r == 0 && (phy_id & 0x1fffffff) != 0x1fffffff) {
  538. is_c45 = (devad == MDIO_DEVAD_NONE) ? false : true;
  539. return phy_device_create(bus, addr, phy_id, is_c45);
  540. }
  541. next:
  542. phy_mask &= ~(1 << addr);
  543. }
  544. return NULL;
  545. }
  546. static struct phy_device *search_for_existing_phy(struct mii_dev *bus,
  547. uint phy_mask)
  548. {
  549. /* If we have one, return the existing device, with new interface */
  550. while (phy_mask) {
  551. int addr = ffs(phy_mask) - 1;
  552. if (bus->phymap[addr])
  553. return bus->phymap[addr];
  554. phy_mask &= ~(1 << addr);
  555. }
  556. return NULL;
  557. }
  558. static struct phy_device *get_phy_device_by_mask(struct mii_dev *bus,
  559. uint phy_mask)
  560. {
  561. struct phy_device *phydev;
  562. int devad[] = {
  563. /* Clause-22 */
  564. MDIO_DEVAD_NONE,
  565. /* Clause-45 */
  566. MDIO_MMD_PMAPMD,
  567. MDIO_MMD_WIS,
  568. MDIO_MMD_PCS,
  569. MDIO_MMD_PHYXS,
  570. MDIO_MMD_VEND1,
  571. };
  572. int i, devad_cnt;
  573. devad_cnt = sizeof(devad)/sizeof(int);
  574. phydev = search_for_existing_phy(bus, phy_mask);
  575. if (phydev)
  576. return phydev;
  577. /* try different access clauses */
  578. for (i = 0; i < devad_cnt; i++) {
  579. phydev = create_phy_by_mask(bus, phy_mask, devad[i]);
  580. if (IS_ERR(phydev))
  581. return NULL;
  582. if (phydev)
  583. return phydev;
  584. }
  585. debug("\n%s PHY: ", bus->name);
  586. while (phy_mask) {
  587. int addr = ffs(phy_mask) - 1;
  588. debug("%d ", addr);
  589. phy_mask &= ~(1 << addr);
  590. }
  591. debug("not found\n");
  592. return NULL;
  593. }
  594. /**
  595. * get_phy_device - reads the specified PHY device and returns its
  596. * @phy_device struct
  597. * @bus: the target MII bus
  598. * @addr: PHY address on the MII bus
  599. *
  600. * Description: Reads the ID registers of the PHY at @addr on the
  601. * @bus, then allocates and returns the phy_device to represent it.
  602. */
  603. static struct phy_device *get_phy_device(struct mii_dev *bus, int addr)
  604. {
  605. return get_phy_device_by_mask(bus, 1 << addr);
  606. }
  607. int phy_reset(struct phy_device *phydev)
  608. {
  609. int reg;
  610. int timeout = 500;
  611. int devad = MDIO_DEVAD_NONE;
  612. if (phydev->flags & PHY_FLAG_BROKEN_RESET)
  613. return 0;
  614. #ifdef CONFIG_PHYLIB_10G
  615. /* If it's 10G, we need to issue reset through one of the MMDs */
  616. if (phydev->is_c45) {
  617. if (!phydev->mmds)
  618. gen10g_discover_mmds(phydev);
  619. devad = ffs(phydev->mmds) - 1;
  620. }
  621. #endif
  622. if (phy_write(phydev, devad, MII_BMCR, BMCR_RESET) < 0) {
  623. debug("PHY reset failed\n");
  624. return -1;
  625. }
  626. #if CONFIG_PHY_RESET_DELAY > 0
  627. udelay(CONFIG_PHY_RESET_DELAY); /* Intel LXT971A needs this */
  628. #endif
  629. /*
  630. * Poll the control register for the reset bit to go to 0 (it is
  631. * auto-clearing). This should happen within 0.5 seconds per the
  632. * IEEE spec.
  633. */
  634. reg = phy_read(phydev, devad, MII_BMCR);
  635. while ((reg & BMCR_RESET) && timeout--) {
  636. reg = phy_read(phydev, devad, MII_BMCR);
  637. if (reg < 0) {
  638. debug("PHY status read failed\n");
  639. return -1;
  640. }
  641. udelay(1000);
  642. }
  643. if (reg & BMCR_RESET) {
  644. puts("PHY reset timed out\n");
  645. return -1;
  646. }
  647. return 0;
  648. }
  649. int miiphy_reset(const char *devname, unsigned char addr)
  650. {
  651. struct mii_dev *bus = miiphy_get_dev_by_name(devname);
  652. struct phy_device *phydev;
  653. phydev = get_phy_device(bus, addr);
  654. return phy_reset(phydev);
  655. }
  656. struct phy_device *phy_find_by_mask(struct mii_dev *bus, uint phy_mask)
  657. {
  658. /* Reset the bus */
  659. if (bus->reset) {
  660. bus->reset(bus);
  661. /* Wait 15ms to make sure the PHY has come out of hard reset */
  662. mdelay(15);
  663. }
  664. return get_phy_device_by_mask(bus, phy_mask);
  665. }
  666. static void phy_connect_dev(struct phy_device *phydev, struct udevice *dev,
  667. phy_interface_t interface)
  668. {
  669. /* Soft Reset the PHY */
  670. phy_reset(phydev);
  671. if (phydev->dev && phydev->dev != dev) {
  672. printf("%s:%d is connected to %s. Reconnecting to %s\n",
  673. phydev->bus->name, phydev->addr,
  674. phydev->dev->name, dev->name);
  675. }
  676. phydev->dev = dev;
  677. phydev->interface = interface;
  678. debug("%s connected to %s mode %s\n", dev->name, phydev->drv->name,
  679. phy_string_for_interface(interface));
  680. }
  681. #ifdef CONFIG_PHY_XILINX_GMII2RGMII
  682. static struct phy_device *phy_connect_gmii2rgmii(struct mii_dev *bus,
  683. struct udevice *dev)
  684. {
  685. struct phy_device *phydev = NULL;
  686. ofnode node;
  687. ofnode_for_each_subnode(node, dev_ofnode(dev)) {
  688. node = ofnode_by_compatible(node, "xlnx,gmii-to-rgmii-1.0");
  689. if (ofnode_valid(node)) {
  690. phydev = phy_device_create(bus, 0,
  691. PHY_GMII2RGMII_ID, false);
  692. if (phydev)
  693. phydev->node = node;
  694. break;
  695. }
  696. node = ofnode_first_subnode(node);
  697. }
  698. return phydev;
  699. }
  700. #endif
  701. #ifdef CONFIG_PHY_FIXED
  702. /**
  703. * fixed_phy_create() - create an unconnected fixed-link pseudo-PHY device
  704. * @node: OF node for the container of the fixed-link node
  705. *
  706. * Description: Creates a struct phy_device based on a fixed-link of_node
  707. * description. Can be used without phy_connect by drivers which do not expose
  708. * a UCLASS_ETH udevice.
  709. */
  710. struct phy_device *fixed_phy_create(ofnode node)
  711. {
  712. struct phy_device *phydev;
  713. ofnode subnode;
  714. subnode = ofnode_find_subnode(node, "fixed-link");
  715. if (!ofnode_valid(subnode)) {
  716. return NULL;
  717. }
  718. phydev = phy_device_create(NULL, 0, PHY_FIXED_ID, false);
  719. if (phydev) {
  720. phydev->node = subnode;
  721. phydev->interface = ofnode_read_phy_mode(node);
  722. }
  723. return phydev;
  724. }
  725. static struct phy_device *phy_connect_fixed(struct mii_dev *bus,
  726. struct udevice *dev)
  727. {
  728. ofnode node = dev_ofnode(dev), subnode;
  729. struct phy_device *phydev = NULL;
  730. if (ofnode_phy_is_fixed_link(node, &subnode)) {
  731. phydev = phy_device_create(bus, 0, PHY_FIXED_ID, false);
  732. if (phydev)
  733. phydev->node = subnode;
  734. }
  735. return phydev;
  736. }
  737. #endif
  738. struct phy_device *phy_connect(struct mii_dev *bus, int addr,
  739. struct udevice *dev,
  740. phy_interface_t interface)
  741. {
  742. struct phy_device *phydev = NULL;
  743. uint mask = (addr >= 0) ? (1 << addr) : 0xffffffff;
  744. #ifdef CONFIG_PHY_FIXED
  745. phydev = phy_connect_fixed(bus, dev);
  746. #endif
  747. #ifdef CONFIG_PHY_NCSI
  748. if (!phydev && interface == PHY_INTERFACE_MODE_NCSI)
  749. phydev = phy_device_create(bus, 0, PHY_NCSI_ID, false);
  750. #endif
  751. #ifdef CONFIG_PHY_ETHERNET_ID
  752. if (!phydev)
  753. phydev = phy_connect_phy_id(bus, dev, addr);
  754. #endif
  755. #ifdef CONFIG_PHY_XILINX_GMII2RGMII
  756. if (!phydev)
  757. phydev = phy_connect_gmii2rgmii(bus, dev);
  758. #endif
  759. if (!phydev)
  760. phydev = phy_find_by_mask(bus, mask);
  761. if (phydev)
  762. phy_connect_dev(phydev, dev, interface);
  763. else
  764. printf("Could not get PHY for %s: addr %d\n", bus->name, addr);
  765. return phydev;
  766. }
  767. /*
  768. * Start the PHY. Returns 0 on success, or a negative error code.
  769. */
  770. int phy_startup(struct phy_device *phydev)
  771. {
  772. if (phydev->drv->startup)
  773. return phydev->drv->startup(phydev);
  774. return 0;
  775. }
  776. __weak int board_phy_config(struct phy_device *phydev)
  777. {
  778. if (phydev->drv->config)
  779. return phydev->drv->config(phydev);
  780. return 0;
  781. }
  782. int phy_config(struct phy_device *phydev)
  783. {
  784. /* Invoke an optional board-specific helper */
  785. return board_phy_config(phydev);
  786. }
  787. int phy_shutdown(struct phy_device *phydev)
  788. {
  789. if (phydev->drv->shutdown)
  790. phydev->drv->shutdown(phydev);
  791. return 0;
  792. }
  793. /**
  794. * phy_modify - Convenience function for modifying a given PHY register
  795. * @phydev: the phy_device struct
  796. * @devad: The MMD to read from
  797. * @regnum: register number to write
  798. * @mask: bit mask of bits to clear
  799. * @set: new value of bits set in mask to write to @regnum
  800. */
  801. int phy_modify(struct phy_device *phydev, int devad, int regnum, u16 mask,
  802. u16 set)
  803. {
  804. int ret;
  805. ret = phy_read(phydev, devad, regnum);
  806. if (ret < 0)
  807. return ret;
  808. return phy_write(phydev, devad, regnum, (ret & ~mask) | set);
  809. }
  810. /**
  811. * phy_read - Convenience function for reading a given PHY register
  812. * @phydev: the phy_device struct
  813. * @devad: The MMD to read from
  814. * @regnum: register number to read
  815. * @return: value for success or negative errno for failure
  816. */
  817. int phy_read(struct phy_device *phydev, int devad, int regnum)
  818. {
  819. struct mii_dev *bus = phydev->bus;
  820. if (!bus || !bus->read) {
  821. debug("%s: No bus configured\n", __func__);
  822. return -1;
  823. }
  824. return bus->read(bus, phydev->addr, devad, regnum);
  825. }
  826. /**
  827. * phy_write - Convenience function for writing a given PHY register
  828. * @phydev: the phy_device struct
  829. * @devad: The MMD to read from
  830. * @regnum: register number to write
  831. * @val: value to write to @regnum
  832. * @return: 0 for success or negative errno for failure
  833. */
  834. int phy_write(struct phy_device *phydev, int devad, int regnum, u16 val)
  835. {
  836. struct mii_dev *bus = phydev->bus;
  837. if (!bus || !bus->write) {
  838. debug("%s: No bus configured\n", __func__);
  839. return -1;
  840. }
  841. return bus->write(bus, phydev->addr, devad, regnum, val);
  842. }
  843. /**
  844. * phy_mmd_start_indirect - Convenience function for writing MMD registers
  845. * @phydev: the phy_device struct
  846. * @devad: The MMD to read from
  847. * @regnum: register number to write
  848. * @return: None
  849. */
  850. void phy_mmd_start_indirect(struct phy_device *phydev, int devad, int regnum)
  851. {
  852. /* Write the desired MMD Devad */
  853. phy_write(phydev, MDIO_DEVAD_NONE, MII_MMD_CTRL, devad);
  854. /* Write the desired MMD register address */
  855. phy_write(phydev, MDIO_DEVAD_NONE, MII_MMD_DATA, regnum);
  856. /* Select the Function : DATA with no post increment */
  857. phy_write(phydev, MDIO_DEVAD_NONE, MII_MMD_CTRL,
  858. (devad | MII_MMD_CTRL_NOINCR));
  859. }
  860. /**
  861. * phy_read_mmd - Convenience function for reading a register
  862. * from an MMD on a given PHY.
  863. * @phydev: The phy_device struct
  864. * @devad: The MMD to read from
  865. * @regnum: The register on the MMD to read
  866. * @return: Value for success or negative errno for failure
  867. */
  868. int phy_read_mmd(struct phy_device *phydev, int devad, int regnum)
  869. {
  870. struct phy_driver *drv = phydev->drv;
  871. if (regnum > (u16)~0 || devad > 32)
  872. return -EINVAL;
  873. /* driver-specific access */
  874. if (drv->read_mmd)
  875. return drv->read_mmd(phydev, devad, regnum);
  876. /* direct C45 / C22 access */
  877. if ((drv->features & PHY_10G_FEATURES) == PHY_10G_FEATURES ||
  878. devad == MDIO_DEVAD_NONE || !devad)
  879. return phy_read(phydev, devad, regnum);
  880. /* indirect C22 access */
  881. phy_mmd_start_indirect(phydev, devad, regnum);
  882. /* Read the content of the MMD's selected register */
  883. return phy_read(phydev, MDIO_DEVAD_NONE, MII_MMD_DATA);
  884. }
  885. /**
  886. * phy_write_mmd - Convenience function for writing a register
  887. * on an MMD on a given PHY.
  888. * @phydev: The phy_device struct
  889. * @devad: The MMD to read from
  890. * @regnum: The register on the MMD to read
  891. * @val: value to write to @regnum
  892. * @return: 0 for success or negative errno for failure
  893. */
  894. int phy_write_mmd(struct phy_device *phydev, int devad, int regnum, u16 val)
  895. {
  896. struct phy_driver *drv = phydev->drv;
  897. if (regnum > (u16)~0 || devad > 32)
  898. return -EINVAL;
  899. /* driver-specific access */
  900. if (drv->write_mmd)
  901. return drv->write_mmd(phydev, devad, regnum, val);
  902. /* direct C45 / C22 access */
  903. if ((drv->features & PHY_10G_FEATURES) == PHY_10G_FEATURES ||
  904. devad == MDIO_DEVAD_NONE || !devad)
  905. return phy_write(phydev, devad, regnum, val);
  906. /* indirect C22 access */
  907. phy_mmd_start_indirect(phydev, devad, regnum);
  908. /* Write the data into MMD's selected register */
  909. return phy_write(phydev, MDIO_DEVAD_NONE, MII_MMD_DATA, val);
  910. }
  911. /**
  912. * phy_set_bits_mmd - Convenience function for setting bits in a register
  913. * on MMD
  914. * @phydev: the phy_device struct
  915. * @devad: the MMD containing register to modify
  916. * @regnum: register number to modify
  917. * @val: bits to set
  918. * @return: 0 for success or negative errno for failure
  919. */
  920. int phy_set_bits_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val)
  921. {
  922. int value, ret;
  923. value = phy_read_mmd(phydev, devad, regnum);
  924. if (value < 0)
  925. return value;
  926. value |= val;
  927. ret = phy_write_mmd(phydev, devad, regnum, value);
  928. if (ret < 0)
  929. return ret;
  930. return 0;
  931. }
  932. /**
  933. * phy_clear_bits_mmd - Convenience function for clearing bits in a register
  934. * on MMD
  935. * @phydev: the phy_device struct
  936. * @devad: the MMD containing register to modify
  937. * @regnum: register number to modify
  938. * @val: bits to clear
  939. * @return: 0 for success or negative errno for failure
  940. */
  941. int phy_clear_bits_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val)
  942. {
  943. int value, ret;
  944. value = phy_read_mmd(phydev, devad, regnum);
  945. if (value < 0)
  946. return value;
  947. value &= ~val;
  948. ret = phy_write_mmd(phydev, devad, regnum, value);
  949. if (ret < 0)
  950. return ret;
  951. return 0;
  952. }
  953. /**
  954. * phy_modify_mmd_changed - Function for modifying a register on MMD
  955. * @phydev: the phy_device struct
  956. * @devad: the MMD containing register to modify
  957. * @regnum: register number to modify
  958. * @mask: bit mask of bits to clear
  959. * @set: new value of bits set in mask to write to @regnum
  960. *
  961. * NOTE: MUST NOT be called from interrupt context,
  962. * because the bus read/write functions may wait for an interrupt
  963. * to conclude the operation.
  964. *
  965. * Returns negative errno, 0 if there was no change, and 1 in case of change
  966. */
  967. int phy_modify_mmd_changed(struct phy_device *phydev, int devad, u32 regnum,
  968. u16 mask, u16 set)
  969. {
  970. int new, ret;
  971. ret = phy_read_mmd(phydev, devad, regnum);
  972. if (ret < 0)
  973. return ret;
  974. new = (ret & ~mask) | set;
  975. if (new == ret)
  976. return 0;
  977. ret = phy_write_mmd(phydev, devad, regnum, new);
  978. return ret < 0 ? ret : 1;
  979. }
  980. /**
  981. * phy_modify_mmd - Convenience function for modifying a register on MMD
  982. * @phydev: the phy_device struct
  983. * @devad: the MMD containing register to modify
  984. * @regnum: register number to modify
  985. * @mask: bit mask of bits to clear
  986. * @set: new value of bits set in mask to write to @regnum
  987. *
  988. * NOTE: MUST NOT be called from interrupt context,
  989. * because the bus read/write functions may wait for an interrupt
  990. * to conclude the operation.
  991. */
  992. int phy_modify_mmd(struct phy_device *phydev, int devad, u32 regnum,
  993. u16 mask, u16 set)
  994. {
  995. int ret;
  996. ret = phy_modify_mmd_changed(phydev, devad, regnum, mask, set);
  997. return ret < 0 ? ret : 0;
  998. }
  999. bool phy_interface_is_ncsi(void)
  1000. {
  1001. #ifdef CONFIG_PHY_NCSI
  1002. struct eth_pdata *pdata = dev_get_plat(eth_get_dev());
  1003. return pdata->phy_interface == PHY_INTERFACE_MODE_NCSI;
  1004. #else
  1005. return 0;
  1006. #endif
  1007. }