miiphyutil.c 13 KB

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