phy.c 18 KB

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