miiphyutil.c 14 KB

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