miiphyutil.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. /*
  2. * (C) Copyright 2001
  3. * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com.
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. /*
  8. * This provides a bit-banged interface to the ethernet MII management
  9. * channel.
  10. */
  11. #include <common.h>
  12. #include <dm.h>
  13. #include <miiphy.h>
  14. #include <phy.h>
  15. #include <asm/types.h>
  16. #include <linux/list.h>
  17. #include <malloc.h>
  18. #include <net.h>
  19. /* local debug macro */
  20. #undef MII_DEBUG
  21. #undef debug
  22. #ifdef MII_DEBUG
  23. #define debug(fmt, args...) printf(fmt, ##args)
  24. #else
  25. #define debug(fmt, args...)
  26. #endif /* MII_DEBUG */
  27. static struct list_head mii_devs;
  28. static struct mii_dev *current_mii;
  29. /*
  30. * Lookup the mii_dev struct by the registered device name.
  31. */
  32. struct mii_dev *miiphy_get_dev_by_name(const char *devname)
  33. {
  34. struct list_head *entry;
  35. struct mii_dev *dev;
  36. if (!devname) {
  37. printf("NULL device name!\n");
  38. return NULL;
  39. }
  40. list_for_each(entry, &mii_devs) {
  41. dev = list_entry(entry, struct mii_dev, link);
  42. if (strcmp(dev->name, devname) == 0)
  43. return dev;
  44. }
  45. return NULL;
  46. }
  47. /*****************************************************************************
  48. *
  49. * Initialize global data. Need to be called before any other miiphy routine.
  50. */
  51. void miiphy_init(void)
  52. {
  53. INIT_LIST_HEAD(&mii_devs);
  54. current_mii = NULL;
  55. }
  56. struct mii_dev *mdio_alloc(void)
  57. {
  58. struct mii_dev *bus;
  59. bus = malloc(sizeof(*bus));
  60. if (!bus)
  61. return bus;
  62. memset(bus, 0, sizeof(*bus));
  63. /* initalize mii_dev struct fields */
  64. INIT_LIST_HEAD(&bus->link);
  65. return bus;
  66. }
  67. void mdio_free(struct mii_dev *bus)
  68. {
  69. free(bus);
  70. }
  71. int mdio_register(struct mii_dev *bus)
  72. {
  73. if (!bus || !bus->read || !bus->write)
  74. return -1;
  75. /* check if we have unique name */
  76. if (miiphy_get_dev_by_name(bus->name)) {
  77. printf("mdio_register: non unique device name '%s'\n",
  78. bus->name);
  79. return -1;
  80. }
  81. /* add it to the list */
  82. list_add_tail(&bus->link, &mii_devs);
  83. if (!current_mii)
  84. current_mii = bus;
  85. return 0;
  86. }
  87. int mdio_unregister(struct mii_dev *bus)
  88. {
  89. if (!bus)
  90. return 0;
  91. /* delete it from the list */
  92. list_del(&bus->link);
  93. if (current_mii == bus)
  94. current_mii = NULL;
  95. return 0;
  96. }
  97. void mdio_list_devices(void)
  98. {
  99. struct list_head *entry;
  100. list_for_each(entry, &mii_devs) {
  101. int i;
  102. struct mii_dev *bus = list_entry(entry, struct mii_dev, link);
  103. printf("%s:\n", bus->name);
  104. for (i = 0; i < PHY_MAX_ADDR; i++) {
  105. struct phy_device *phydev = bus->phymap[i];
  106. if (phydev) {
  107. printf("%d - %s", i, phydev->drv->name);
  108. if (phydev->dev)
  109. printf(" <--> %s\n", phydev->dev->name);
  110. else
  111. printf("\n");
  112. }
  113. }
  114. }
  115. }
  116. int miiphy_set_current_dev(const char *devname)
  117. {
  118. struct mii_dev *dev;
  119. dev = miiphy_get_dev_by_name(devname);
  120. if (dev) {
  121. current_mii = dev;
  122. return 0;
  123. }
  124. printf("No such device: %s\n", devname);
  125. return 1;
  126. }
  127. struct mii_dev *mdio_get_current_dev(void)
  128. {
  129. return current_mii;
  130. }
  131. struct phy_device *mdio_phydev_for_ethname(const char *ethname)
  132. {
  133. struct list_head *entry;
  134. struct mii_dev *bus;
  135. list_for_each(entry, &mii_devs) {
  136. int i;
  137. bus = list_entry(entry, struct mii_dev, link);
  138. for (i = 0; i < PHY_MAX_ADDR; i++) {
  139. if (!bus->phymap[i] || !bus->phymap[i]->dev)
  140. continue;
  141. if (strcmp(bus->phymap[i]->dev->name, ethname) == 0)
  142. return bus->phymap[i];
  143. }
  144. }
  145. printf("%s is not a known ethernet\n", ethname);
  146. return NULL;
  147. }
  148. const char *miiphy_get_current_dev(void)
  149. {
  150. if (current_mii)
  151. return current_mii->name;
  152. return NULL;
  153. }
  154. static struct mii_dev *miiphy_get_active_dev(const char *devname)
  155. {
  156. /* If the current mii is the one we want, return it */
  157. if (current_mii)
  158. if (strcmp(current_mii->name, devname) == 0)
  159. return current_mii;
  160. /* Otherwise, set the active one to the one we want */
  161. if (miiphy_set_current_dev(devname))
  162. return NULL;
  163. else
  164. return current_mii;
  165. }
  166. /*****************************************************************************
  167. *
  168. * Read to variable <value> from the PHY attached to device <devname>,
  169. * use PHY address <addr> and register <reg>.
  170. *
  171. * This API is deprecated. Use phy_read on a phy_device found via phy_connect
  172. *
  173. * Returns:
  174. * 0 on success
  175. */
  176. int miiphy_read(const char *devname, unsigned char addr, unsigned char reg,
  177. unsigned short *value)
  178. {
  179. struct mii_dev *bus;
  180. int ret;
  181. bus = miiphy_get_active_dev(devname);
  182. if (!bus)
  183. return 1;
  184. ret = bus->read(bus, addr, MDIO_DEVAD_NONE, reg);
  185. if (ret < 0)
  186. return 1;
  187. *value = (unsigned short)ret;
  188. return 0;
  189. }
  190. /*****************************************************************************
  191. *
  192. * Write <value> to the PHY attached to device <devname>,
  193. * use PHY address <addr> and register <reg>.
  194. *
  195. * This API is deprecated. Use phy_write on a phy_device found by phy_connect
  196. *
  197. * Returns:
  198. * 0 on success
  199. */
  200. int miiphy_write(const char *devname, unsigned char addr, unsigned char reg,
  201. unsigned short value)
  202. {
  203. struct mii_dev *bus;
  204. bus = miiphy_get_active_dev(devname);
  205. if (bus)
  206. return bus->write(bus, addr, MDIO_DEVAD_NONE, reg, value);
  207. return 1;
  208. }
  209. /*****************************************************************************
  210. *
  211. * Print out list of registered MII capable devices.
  212. */
  213. void miiphy_listdev(void)
  214. {
  215. struct list_head *entry;
  216. struct mii_dev *dev;
  217. puts("MII devices: ");
  218. list_for_each(entry, &mii_devs) {
  219. dev = list_entry(entry, struct mii_dev, link);
  220. printf("'%s' ", dev->name);
  221. }
  222. puts("\n");
  223. if (current_mii)
  224. printf("Current device: '%s'\n", current_mii->name);
  225. }
  226. /*****************************************************************************
  227. *
  228. * Read the OUI, manufacture's model number, and revision number.
  229. *
  230. * OUI: 22 bits (unsigned int)
  231. * Model: 6 bits (unsigned char)
  232. * Revision: 4 bits (unsigned char)
  233. *
  234. * This API is deprecated.
  235. *
  236. * Returns:
  237. * 0 on success
  238. */
  239. int miiphy_info(const char *devname, unsigned char addr, unsigned int *oui,
  240. unsigned char *model, unsigned char *rev)
  241. {
  242. unsigned int reg = 0;
  243. unsigned short tmp;
  244. if (miiphy_read(devname, addr, MII_PHYSID2, &tmp) != 0) {
  245. debug("PHY ID register 2 read failed\n");
  246. return -1;
  247. }
  248. reg = tmp;
  249. debug("MII_PHYSID2 @ 0x%x = 0x%04x\n", addr, reg);
  250. if (reg == 0xFFFF) {
  251. /* No physical device present at this address */
  252. return -1;
  253. }
  254. if (miiphy_read(devname, addr, MII_PHYSID1, &tmp) != 0) {
  255. debug("PHY ID register 1 read failed\n");
  256. return -1;
  257. }
  258. reg |= tmp << 16;
  259. debug("PHY_PHYIDR[1,2] @ 0x%x = 0x%08x\n", addr, reg);
  260. *oui = (reg >> 10);
  261. *model = (unsigned char)((reg >> 4) & 0x0000003F);
  262. *rev = (unsigned char)(reg & 0x0000000F);
  263. return 0;
  264. }
  265. #ifndef CONFIG_PHYLIB
  266. /*****************************************************************************
  267. *
  268. * Reset the PHY.
  269. *
  270. * This API is deprecated. Use PHYLIB.
  271. *
  272. * Returns:
  273. * 0 on success
  274. */
  275. int miiphy_reset(const char *devname, unsigned char addr)
  276. {
  277. unsigned short reg;
  278. int timeout = 500;
  279. if (miiphy_read(devname, addr, MII_BMCR, &reg) != 0) {
  280. debug("PHY status read failed\n");
  281. return -1;
  282. }
  283. if (miiphy_write(devname, addr, MII_BMCR, reg | BMCR_RESET) != 0) {
  284. debug("PHY reset failed\n");
  285. return -1;
  286. }
  287. #ifdef CONFIG_PHY_RESET_DELAY
  288. udelay(CONFIG_PHY_RESET_DELAY); /* Intel LXT971A needs this */
  289. #endif
  290. /*
  291. * Poll the control register for the reset bit to go to 0 (it is
  292. * auto-clearing). This should happen within 0.5 seconds per the
  293. * IEEE spec.
  294. */
  295. reg = 0x8000;
  296. while (((reg & 0x8000) != 0) && timeout--) {
  297. if (miiphy_read(devname, addr, MII_BMCR, &reg) != 0) {
  298. debug("PHY status read failed\n");
  299. return -1;
  300. }
  301. udelay(1000);
  302. }
  303. if ((reg & 0x8000) == 0) {
  304. return 0;
  305. } else {
  306. puts("PHY reset timed out\n");
  307. return -1;
  308. }
  309. return 0;
  310. }
  311. #endif /* !PHYLIB */
  312. /*****************************************************************************
  313. *
  314. * Determine the ethernet speed (10/100/1000). Return 10 on error.
  315. */
  316. int miiphy_speed(const char *devname, unsigned char addr)
  317. {
  318. u16 bmcr, anlpar;
  319. #if defined(CONFIG_PHY_GIGE)
  320. u16 btsr;
  321. /*
  322. * Check for 1000BASE-X. If it is supported, then assume that the speed
  323. * is 1000.
  324. */
  325. if (miiphy_is_1000base_x(devname, addr))
  326. return _1000BASET;
  327. /*
  328. * No 1000BASE-X, so assume 1000BASE-T/100BASE-TX/10BASE-T register set.
  329. */
  330. /* Check for 1000BASE-T. */
  331. if (miiphy_read(devname, addr, MII_STAT1000, &btsr)) {
  332. printf("PHY 1000BT status");
  333. goto miiphy_read_failed;
  334. }
  335. if (btsr != 0xFFFF &&
  336. (btsr & (PHY_1000BTSR_1000FD | PHY_1000BTSR_1000HD)))
  337. return _1000BASET;
  338. #endif /* CONFIG_PHY_GIGE */
  339. /* Check Basic Management Control Register first. */
  340. if (miiphy_read(devname, addr, MII_BMCR, &bmcr)) {
  341. printf("PHY speed");
  342. goto miiphy_read_failed;
  343. }
  344. /* Check if auto-negotiation is on. */
  345. if (bmcr & BMCR_ANENABLE) {
  346. /* Get auto-negotiation results. */
  347. if (miiphy_read(devname, addr, MII_LPA, &anlpar)) {
  348. printf("PHY AN speed");
  349. goto miiphy_read_failed;
  350. }
  351. return (anlpar & LPA_100) ? _100BASET : _10BASET;
  352. }
  353. /* Get speed from basic control settings. */
  354. return (bmcr & BMCR_SPEED100) ? _100BASET : _10BASET;
  355. miiphy_read_failed:
  356. printf(" read failed, assuming 10BASE-T\n");
  357. return _10BASET;
  358. }
  359. /*****************************************************************************
  360. *
  361. * Determine full/half duplex. Return half on error.
  362. */
  363. int miiphy_duplex(const char *devname, unsigned char addr)
  364. {
  365. u16 bmcr, anlpar;
  366. #if defined(CONFIG_PHY_GIGE)
  367. u16 btsr;
  368. /* Check for 1000BASE-X. */
  369. if (miiphy_is_1000base_x(devname, addr)) {
  370. /* 1000BASE-X */
  371. if (miiphy_read(devname, addr, MII_LPA, &anlpar)) {
  372. printf("1000BASE-X PHY AN duplex");
  373. goto miiphy_read_failed;
  374. }
  375. }
  376. /*
  377. * No 1000BASE-X, so assume 1000BASE-T/100BASE-TX/10BASE-T register set.
  378. */
  379. /* Check for 1000BASE-T. */
  380. if (miiphy_read(devname, addr, MII_STAT1000, &btsr)) {
  381. printf("PHY 1000BT status");
  382. goto miiphy_read_failed;
  383. }
  384. if (btsr != 0xFFFF) {
  385. if (btsr & PHY_1000BTSR_1000FD) {
  386. return FULL;
  387. } else if (btsr & PHY_1000BTSR_1000HD) {
  388. return HALF;
  389. }
  390. }
  391. #endif /* CONFIG_PHY_GIGE */
  392. /* Check Basic Management Control Register first. */
  393. if (miiphy_read(devname, addr, MII_BMCR, &bmcr)) {
  394. puts("PHY duplex");
  395. goto miiphy_read_failed;
  396. }
  397. /* Check if auto-negotiation is on. */
  398. if (bmcr & BMCR_ANENABLE) {
  399. /* Get auto-negotiation results. */
  400. if (miiphy_read(devname, addr, MII_LPA, &anlpar)) {
  401. puts("PHY AN duplex");
  402. goto miiphy_read_failed;
  403. }
  404. return (anlpar & (LPA_10FULL | LPA_100FULL)) ?
  405. FULL : HALF;
  406. }
  407. /* Get speed from basic control settings. */
  408. return (bmcr & BMCR_FULLDPLX) ? FULL : HALF;
  409. miiphy_read_failed:
  410. printf(" read failed, assuming half duplex\n");
  411. return HALF;
  412. }
  413. /*****************************************************************************
  414. *
  415. * Return 1 if PHY supports 1000BASE-X, 0 if PHY supports 10BASE-T/100BASE-TX/
  416. * 1000BASE-T, or on error.
  417. */
  418. int miiphy_is_1000base_x(const char *devname, unsigned char addr)
  419. {
  420. #if defined(CONFIG_PHY_GIGE)
  421. u16 exsr;
  422. if (miiphy_read(devname, addr, MII_ESTATUS, &exsr)) {
  423. printf("PHY extended status read failed, assuming no "
  424. "1000BASE-X\n");
  425. return 0;
  426. }
  427. return 0 != (exsr & (ESTATUS_1000XF | ESTATUS_1000XH));
  428. #else
  429. return 0;
  430. #endif
  431. }
  432. #ifdef CONFIG_SYS_FAULT_ECHO_LINK_DOWN
  433. /*****************************************************************************
  434. *
  435. * Determine link status
  436. */
  437. int miiphy_link(const char *devname, unsigned char addr)
  438. {
  439. unsigned short reg;
  440. /* dummy read; needed to latch some phys */
  441. (void)miiphy_read(devname, addr, MII_BMSR, &reg);
  442. if (miiphy_read(devname, addr, MII_BMSR, &reg)) {
  443. puts("MII_BMSR read failed, assuming no link\n");
  444. return 0;
  445. }
  446. /* Determine if a link is active */
  447. if ((reg & BMSR_LSTATUS) != 0) {
  448. return 1;
  449. } else {
  450. return 0;
  451. }
  452. }
  453. #endif