miiphyutil.c 14 KB

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