miiphyutil.c 13 KB

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