miiphyutil.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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. static int legacy_miiphy_read(struct mii_dev *bus, int addr, int devad, int reg)
  57. {
  58. unsigned short val;
  59. int ret;
  60. struct legacy_mii_dev *ldev = bus->priv;
  61. ret = ldev->read(bus->name, addr, reg, &val);
  62. return ret ? -1 : (int)val;
  63. }
  64. static int legacy_miiphy_write(struct mii_dev *bus, int addr, int devad,
  65. int reg, u16 val)
  66. {
  67. struct legacy_mii_dev *ldev = bus->priv;
  68. return ldev->write(bus->name, addr, reg, val);
  69. }
  70. /*****************************************************************************
  71. *
  72. * Register read and write MII access routines for the device <name>.
  73. * This API is now deprecated. Please use mdio_alloc and mdio_register, instead.
  74. */
  75. void miiphy_register(const char *name,
  76. int (*read)(const char *devname, unsigned char addr,
  77. unsigned char reg, unsigned short *value),
  78. int (*write)(const char *devname, unsigned char addr,
  79. unsigned char reg, unsigned short value))
  80. {
  81. struct mii_dev *new_dev;
  82. struct legacy_mii_dev *ldev;
  83. BUG_ON(strlen(name) >= MDIO_NAME_LEN);
  84. /* check if we have unique name */
  85. new_dev = miiphy_get_dev_by_name(name);
  86. if (new_dev) {
  87. printf("miiphy_register: non unique device name '%s'\n", name);
  88. return;
  89. }
  90. /* allocate memory */
  91. new_dev = mdio_alloc();
  92. ldev = malloc(sizeof(*ldev));
  93. if (new_dev == NULL || ldev == NULL) {
  94. printf("miiphy_register: cannot allocate memory for '%s'\n",
  95. name);
  96. free(ldev);
  97. mdio_free(new_dev);
  98. return;
  99. }
  100. /* initalize mii_dev struct fields */
  101. new_dev->read = legacy_miiphy_read;
  102. new_dev->write = legacy_miiphy_write;
  103. strncpy(new_dev->name, name, MDIO_NAME_LEN);
  104. new_dev->name[MDIO_NAME_LEN - 1] = 0;
  105. ldev->read = read;
  106. ldev->write = write;
  107. new_dev->priv = ldev;
  108. debug("miiphy_register: added '%s', read=0x%08lx, write=0x%08lx\n",
  109. new_dev->name, ldev->read, ldev->write);
  110. /* add it to the list */
  111. list_add_tail(&new_dev->link, &mii_devs);
  112. if (!current_mii)
  113. current_mii = new_dev;
  114. }
  115. struct mii_dev *mdio_alloc(void)
  116. {
  117. struct mii_dev *bus;
  118. bus = malloc(sizeof(*bus));
  119. if (!bus)
  120. return bus;
  121. memset(bus, 0, sizeof(*bus));
  122. /* initalize mii_dev struct fields */
  123. INIT_LIST_HEAD(&bus->link);
  124. return bus;
  125. }
  126. void mdio_free(struct mii_dev *bus)
  127. {
  128. free(bus);
  129. }
  130. int mdio_register(struct mii_dev *bus)
  131. {
  132. if (!bus || !bus->read || !bus->write)
  133. return -1;
  134. /* check if we have unique name */
  135. if (miiphy_get_dev_by_name(bus->name)) {
  136. printf("mdio_register: non unique device name '%s'\n",
  137. bus->name);
  138. return -1;
  139. }
  140. /* add it to the list */
  141. list_add_tail(&bus->link, &mii_devs);
  142. if (!current_mii)
  143. current_mii = bus;
  144. return 0;
  145. }
  146. int mdio_unregister(struct mii_dev *bus)
  147. {
  148. if (!bus)
  149. return 0;
  150. /* delete it from the list */
  151. list_del(&bus->link);
  152. if (current_mii == bus)
  153. current_mii = NULL;
  154. return 0;
  155. }
  156. void mdio_list_devices(void)
  157. {
  158. struct list_head *entry;
  159. list_for_each(entry, &mii_devs) {
  160. int i;
  161. struct mii_dev *bus = list_entry(entry, struct mii_dev, link);
  162. printf("%s:\n", bus->name);
  163. for (i = 0; i < PHY_MAX_ADDR; i++) {
  164. struct phy_device *phydev = bus->phymap[i];
  165. if (phydev) {
  166. printf("%d - %s", i, phydev->drv->name);
  167. if (phydev->dev)
  168. printf(" <--> %s\n", phydev->dev->name);
  169. else
  170. printf("\n");
  171. }
  172. }
  173. }
  174. }
  175. int miiphy_set_current_dev(const char *devname)
  176. {
  177. struct mii_dev *dev;
  178. dev = miiphy_get_dev_by_name(devname);
  179. if (dev) {
  180. current_mii = dev;
  181. return 0;
  182. }
  183. printf("No such device: %s\n", devname);
  184. return 1;
  185. }
  186. struct mii_dev *mdio_get_current_dev(void)
  187. {
  188. return current_mii;
  189. }
  190. struct phy_device *mdio_phydev_for_ethname(const char *ethname)
  191. {
  192. struct list_head *entry;
  193. struct mii_dev *bus;
  194. list_for_each(entry, &mii_devs) {
  195. int i;
  196. bus = list_entry(entry, struct mii_dev, link);
  197. for (i = 0; i < PHY_MAX_ADDR; i++) {
  198. if (!bus->phymap[i] || !bus->phymap[i]->dev)
  199. continue;
  200. if (strcmp(bus->phymap[i]->dev->name, ethname) == 0)
  201. return bus->phymap[i];
  202. }
  203. }
  204. printf("%s is not a known ethernet\n", ethname);
  205. return NULL;
  206. }
  207. const char *miiphy_get_current_dev(void)
  208. {
  209. if (current_mii)
  210. return current_mii->name;
  211. return NULL;
  212. }
  213. static struct mii_dev *miiphy_get_active_dev(const char *devname)
  214. {
  215. /* If the current mii is the one we want, return it */
  216. if (current_mii)
  217. if (strcmp(current_mii->name, devname) == 0)
  218. return current_mii;
  219. /* Otherwise, set the active one to the one we want */
  220. if (miiphy_set_current_dev(devname))
  221. return NULL;
  222. else
  223. return current_mii;
  224. }
  225. /*****************************************************************************
  226. *
  227. * Read to variable <value> from the PHY attached to device <devname>,
  228. * use PHY address <addr> and register <reg>.
  229. *
  230. * This API is deprecated. Use phy_read on a phy_device found via phy_connect
  231. *
  232. * Returns:
  233. * 0 on success
  234. */
  235. int miiphy_read(const char *devname, unsigned char addr, unsigned char reg,
  236. unsigned short *value)
  237. {
  238. struct mii_dev *bus;
  239. int ret;
  240. bus = miiphy_get_active_dev(devname);
  241. if (!bus)
  242. return 1;
  243. ret = bus->read(bus, addr, MDIO_DEVAD_NONE, reg);
  244. if (ret < 0)
  245. return 1;
  246. *value = (unsigned short)ret;
  247. return 0;
  248. }
  249. /*****************************************************************************
  250. *
  251. * Write <value> to the PHY attached to device <devname>,
  252. * use PHY address <addr> and register <reg>.
  253. *
  254. * This API is deprecated. Use phy_write on a phy_device found by phy_connect
  255. *
  256. * Returns:
  257. * 0 on success
  258. */
  259. int miiphy_write(const char *devname, unsigned char addr, unsigned char reg,
  260. unsigned short value)
  261. {
  262. struct mii_dev *bus;
  263. bus = miiphy_get_active_dev(devname);
  264. if (bus)
  265. return bus->write(bus, addr, MDIO_DEVAD_NONE, reg, value);
  266. return 1;
  267. }
  268. /*****************************************************************************
  269. *
  270. * Print out list of registered MII capable devices.
  271. */
  272. void miiphy_listdev(void)
  273. {
  274. struct list_head *entry;
  275. struct mii_dev *dev;
  276. puts("MII devices: ");
  277. list_for_each(entry, &mii_devs) {
  278. dev = list_entry(entry, struct mii_dev, link);
  279. printf("'%s' ", dev->name);
  280. }
  281. puts("\n");
  282. if (current_mii)
  283. printf("Current device: '%s'\n", current_mii->name);
  284. }
  285. /*****************************************************************************
  286. *
  287. * Read the OUI, manufacture's model number, and revision number.
  288. *
  289. * OUI: 22 bits (unsigned int)
  290. * Model: 6 bits (unsigned char)
  291. * Revision: 4 bits (unsigned char)
  292. *
  293. * This API is deprecated.
  294. *
  295. * Returns:
  296. * 0 on success
  297. */
  298. int miiphy_info(const char *devname, unsigned char addr, unsigned int *oui,
  299. unsigned char *model, unsigned char *rev)
  300. {
  301. unsigned int reg = 0;
  302. unsigned short tmp;
  303. if (miiphy_read(devname, addr, MII_PHYSID2, &tmp) != 0) {
  304. debug("PHY ID register 2 read failed\n");
  305. return -1;
  306. }
  307. reg = tmp;
  308. debug("MII_PHYSID2 @ 0x%x = 0x%04x\n", addr, reg);
  309. if (reg == 0xFFFF) {
  310. /* No physical device present at this address */
  311. return -1;
  312. }
  313. if (miiphy_read(devname, addr, MII_PHYSID1, &tmp) != 0) {
  314. debug("PHY ID register 1 read failed\n");
  315. return -1;
  316. }
  317. reg |= tmp << 16;
  318. debug("PHY_PHYIDR[1,2] @ 0x%x = 0x%08x\n", addr, reg);
  319. *oui = (reg >> 10);
  320. *model = (unsigned char)((reg >> 4) & 0x0000003F);
  321. *rev = (unsigned char)(reg & 0x0000000F);
  322. return 0;
  323. }
  324. #ifndef CONFIG_PHYLIB
  325. /*****************************************************************************
  326. *
  327. * Reset the PHY.
  328. *
  329. * This API is deprecated. Use PHYLIB.
  330. *
  331. * Returns:
  332. * 0 on success
  333. */
  334. int miiphy_reset(const char *devname, unsigned char addr)
  335. {
  336. unsigned short reg;
  337. int timeout = 500;
  338. if (miiphy_read(devname, addr, MII_BMCR, &reg) != 0) {
  339. debug("PHY status read failed\n");
  340. return -1;
  341. }
  342. if (miiphy_write(devname, addr, MII_BMCR, reg | BMCR_RESET) != 0) {
  343. debug("PHY reset failed\n");
  344. return -1;
  345. }
  346. #ifdef CONFIG_PHY_RESET_DELAY
  347. udelay(CONFIG_PHY_RESET_DELAY); /* Intel LXT971A needs this */
  348. #endif
  349. /*
  350. * Poll the control register for the reset bit to go to 0 (it is
  351. * auto-clearing). This should happen within 0.5 seconds per the
  352. * IEEE spec.
  353. */
  354. reg = 0x8000;
  355. while (((reg & 0x8000) != 0) && timeout--) {
  356. if (miiphy_read(devname, addr, MII_BMCR, &reg) != 0) {
  357. debug("PHY status read failed\n");
  358. return -1;
  359. }
  360. udelay(1000);
  361. }
  362. if ((reg & 0x8000) == 0) {
  363. return 0;
  364. } else {
  365. puts("PHY reset timed out\n");
  366. return -1;
  367. }
  368. return 0;
  369. }
  370. #endif /* !PHYLIB */
  371. /*****************************************************************************
  372. *
  373. * Determine the ethernet speed (10/100/1000). Return 10 on error.
  374. */
  375. int miiphy_speed(const char *devname, unsigned char addr)
  376. {
  377. u16 bmcr, anlpar;
  378. #if defined(CONFIG_PHY_GIGE)
  379. u16 btsr;
  380. /*
  381. * Check for 1000BASE-X. If it is supported, then assume that the speed
  382. * is 1000.
  383. */
  384. if (miiphy_is_1000base_x(devname, addr))
  385. return _1000BASET;
  386. /*
  387. * No 1000BASE-X, so assume 1000BASE-T/100BASE-TX/10BASE-T register set.
  388. */
  389. /* Check for 1000BASE-T. */
  390. if (miiphy_read(devname, addr, MII_STAT1000, &btsr)) {
  391. printf("PHY 1000BT status");
  392. goto miiphy_read_failed;
  393. }
  394. if (btsr != 0xFFFF &&
  395. (btsr & (PHY_1000BTSR_1000FD | PHY_1000BTSR_1000HD)))
  396. return _1000BASET;
  397. #endif /* CONFIG_PHY_GIGE */
  398. /* Check Basic Management Control Register first. */
  399. if (miiphy_read(devname, addr, MII_BMCR, &bmcr)) {
  400. printf("PHY speed");
  401. goto miiphy_read_failed;
  402. }
  403. /* Check if auto-negotiation is on. */
  404. if (bmcr & BMCR_ANENABLE) {
  405. /* Get auto-negotiation results. */
  406. if (miiphy_read(devname, addr, MII_LPA, &anlpar)) {
  407. printf("PHY AN speed");
  408. goto miiphy_read_failed;
  409. }
  410. return (anlpar & LPA_100) ? _100BASET : _10BASET;
  411. }
  412. /* Get speed from basic control settings. */
  413. return (bmcr & BMCR_SPEED100) ? _100BASET : _10BASET;
  414. miiphy_read_failed:
  415. printf(" read failed, assuming 10BASE-T\n");
  416. return _10BASET;
  417. }
  418. /*****************************************************************************
  419. *
  420. * Determine full/half duplex. Return half on error.
  421. */
  422. int miiphy_duplex(const char *devname, unsigned char addr)
  423. {
  424. u16 bmcr, anlpar;
  425. #if defined(CONFIG_PHY_GIGE)
  426. u16 btsr;
  427. /* Check for 1000BASE-X. */
  428. if (miiphy_is_1000base_x(devname, addr)) {
  429. /* 1000BASE-X */
  430. if (miiphy_read(devname, addr, MII_LPA, &anlpar)) {
  431. printf("1000BASE-X PHY AN duplex");
  432. goto miiphy_read_failed;
  433. }
  434. }
  435. /*
  436. * No 1000BASE-X, so assume 1000BASE-T/100BASE-TX/10BASE-T register set.
  437. */
  438. /* Check for 1000BASE-T. */
  439. if (miiphy_read(devname, addr, MII_STAT1000, &btsr)) {
  440. printf("PHY 1000BT status");
  441. goto miiphy_read_failed;
  442. }
  443. if (btsr != 0xFFFF) {
  444. if (btsr & PHY_1000BTSR_1000FD) {
  445. return FULL;
  446. } else if (btsr & PHY_1000BTSR_1000HD) {
  447. return HALF;
  448. }
  449. }
  450. #endif /* CONFIG_PHY_GIGE */
  451. /* Check Basic Management Control Register first. */
  452. if (miiphy_read(devname, addr, MII_BMCR, &bmcr)) {
  453. puts("PHY duplex");
  454. goto miiphy_read_failed;
  455. }
  456. /* Check if auto-negotiation is on. */
  457. if (bmcr & BMCR_ANENABLE) {
  458. /* Get auto-negotiation results. */
  459. if (miiphy_read(devname, addr, MII_LPA, &anlpar)) {
  460. puts("PHY AN duplex");
  461. goto miiphy_read_failed;
  462. }
  463. return (anlpar & (LPA_10FULL | LPA_100FULL)) ?
  464. FULL : HALF;
  465. }
  466. /* Get speed from basic control settings. */
  467. return (bmcr & BMCR_FULLDPLX) ? FULL : HALF;
  468. miiphy_read_failed:
  469. printf(" read failed, assuming half duplex\n");
  470. return HALF;
  471. }
  472. /*****************************************************************************
  473. *
  474. * Return 1 if PHY supports 1000BASE-X, 0 if PHY supports 10BASE-T/100BASE-TX/
  475. * 1000BASE-T, or on error.
  476. */
  477. int miiphy_is_1000base_x(const char *devname, unsigned char addr)
  478. {
  479. #if defined(CONFIG_PHY_GIGE)
  480. u16 exsr;
  481. if (miiphy_read(devname, addr, MII_ESTATUS, &exsr)) {
  482. printf("PHY extended status read failed, assuming no "
  483. "1000BASE-X\n");
  484. return 0;
  485. }
  486. return 0 != (exsr & (ESTATUS_1000XF | ESTATUS_1000XH));
  487. #else
  488. return 0;
  489. #endif
  490. }
  491. #ifdef CONFIG_SYS_FAULT_ECHO_LINK_DOWN
  492. /*****************************************************************************
  493. *
  494. * Determine link status
  495. */
  496. int miiphy_link(const char *devname, unsigned char addr)
  497. {
  498. unsigned short reg;
  499. /* dummy read; needed to latch some phys */
  500. (void)miiphy_read(devname, addr, MII_BMSR, &reg);
  501. if (miiphy_read(devname, addr, MII_BMSR, &reg)) {
  502. puts("MII_BMSR read failed, assuming no link\n");
  503. return 0;
  504. }
  505. /* Determine if a link is active */
  506. if ((reg & BMSR_LSTATUS) != 0) {
  507. return 1;
  508. } else {
  509. return 0;
  510. }
  511. }
  512. #endif