phy.c 19 KB

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