miiphyutil.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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. #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
  30. #include <asm/types.h>
  31. #include <linux/list.h>
  32. #include <malloc.h>
  33. #include <net.h>
  34. /* local debug macro */
  35. #define MII_DEBUG
  36. #undef MII_DEBUG
  37. #undef debug
  38. #ifdef MII_DEBUG
  39. #define debug(fmt,args...) printf (fmt ,##args)
  40. #else
  41. #define debug(fmt,args...)
  42. #endif /* MII_DEBUG */
  43. struct mii_dev {
  44. struct list_head link;
  45. char *name;
  46. int (*read) (char *devname, unsigned char addr,
  47. unsigned char reg, unsigned short *value);
  48. int (*write) (char *devname, unsigned char addr,
  49. unsigned char reg, unsigned short value);
  50. };
  51. static struct list_head mii_devs;
  52. static struct mii_dev *current_mii;
  53. /*****************************************************************************
  54. *
  55. * Initialize global data. Need to be called before any other miiphy routine.
  56. */
  57. void miiphy_init ()
  58. {
  59. INIT_LIST_HEAD (&mii_devs);
  60. current_mii = NULL;
  61. }
  62. /*****************************************************************************
  63. *
  64. * Register read and write MII access routines for the device <name>.
  65. */
  66. void miiphy_register (char *name,
  67. int (*read) (char *devname, unsigned char addr,
  68. unsigned char reg, unsigned short *value),
  69. int (*write) (char *devname, unsigned char addr,
  70. unsigned char reg, unsigned short value))
  71. {
  72. struct list_head *entry;
  73. struct mii_dev *new_dev;
  74. struct mii_dev *miidev;
  75. unsigned int name_len;
  76. /* check if we have unique name */
  77. list_for_each (entry, &mii_devs) {
  78. miidev = list_entry (entry, struct mii_dev, link);
  79. if (strcmp (miidev->name, name) == 0) {
  80. printf ("miiphy_register: non unique device name "
  81. "'%s'\n", name);
  82. return;
  83. }
  84. }
  85. /* allocate memory */
  86. name_len = strlen (name);
  87. new_dev =
  88. (struct mii_dev *)malloc (sizeof (struct mii_dev) + name_len + 1);
  89. if (new_dev == NULL) {
  90. printf ("miiphy_register: cannot allocate memory for '%s'\n",
  91. name);
  92. return;
  93. }
  94. memset (new_dev, 0, sizeof (struct mii_dev) + name_len);
  95. /* initalize mii_dev struct fields */
  96. INIT_LIST_HEAD (&new_dev->link);
  97. new_dev->read = read;
  98. new_dev->write = write;
  99. new_dev->name = (char *)(new_dev + 1);
  100. strncpy (new_dev->name, name, name_len);
  101. new_dev->name[name_len] = '\0';
  102. debug ("miiphy_register: added '%s', read=0x%08lx, write=0x%08lx\n",
  103. new_dev->name, new_dev->read, new_dev->write);
  104. /* add it to the list */
  105. list_add_tail (&new_dev->link, &mii_devs);
  106. if (!current_mii)
  107. current_mii = new_dev;
  108. }
  109. int miiphy_set_current_dev (char *devname)
  110. {
  111. struct list_head *entry;
  112. struct mii_dev *dev;
  113. list_for_each (entry, &mii_devs) {
  114. dev = list_entry (entry, struct mii_dev, link);
  115. if (strcmp (devname, dev->name) == 0) {
  116. current_mii = dev;
  117. return 0;
  118. }
  119. }
  120. printf ("No such device: %s\n", devname);
  121. return 1;
  122. }
  123. char *miiphy_get_current_dev ()
  124. {
  125. if (current_mii)
  126. return current_mii->name;
  127. return NULL;
  128. }
  129. /*****************************************************************************
  130. *
  131. * Read to variable <value> from the PHY attached to device <devname>,
  132. * use PHY address <addr> and register <reg>.
  133. *
  134. * Returns:
  135. * 0 on success
  136. */
  137. int miiphy_read (char *devname, unsigned char addr, unsigned char reg,
  138. unsigned short *value)
  139. {
  140. struct list_head *entry;
  141. struct mii_dev *dev;
  142. int found_dev = 0;
  143. int read_ret = 0;
  144. if (!devname) {
  145. printf ("NULL device name!\n");
  146. return 1;
  147. }
  148. list_for_each (entry, &mii_devs) {
  149. dev = list_entry (entry, struct mii_dev, link);
  150. if (strcmp (devname, dev->name) == 0) {
  151. found_dev = 1;
  152. read_ret = dev->read (devname, addr, reg, value);
  153. break;
  154. }
  155. }
  156. if (found_dev == 0)
  157. printf ("No such device: %s\n", devname);
  158. return ((found_dev) ? read_ret : 1);
  159. }
  160. /*****************************************************************************
  161. *
  162. * Write <value> to the PHY attached to device <devname>,
  163. * use PHY address <addr> and register <reg>.
  164. *
  165. * Returns:
  166. * 0 on success
  167. */
  168. int miiphy_write (char *devname, unsigned char addr, unsigned char reg,
  169. unsigned short value)
  170. {
  171. struct list_head *entry;
  172. struct mii_dev *dev;
  173. int found_dev = 0;
  174. int write_ret = 0;
  175. if (!devname) {
  176. printf ("NULL device name!\n");
  177. return 1;
  178. }
  179. list_for_each (entry, &mii_devs) {
  180. dev = list_entry (entry, struct mii_dev, link);
  181. if (strcmp (devname, dev->name) == 0) {
  182. found_dev = 1;
  183. write_ret = dev->write (devname, addr, reg, value);
  184. break;
  185. }
  186. }
  187. if (found_dev == 0)
  188. printf ("No such device: %s\n", devname);
  189. return ((found_dev) ? write_ret : 1);
  190. }
  191. /*****************************************************************************
  192. *
  193. * Print out list of registered MII capable devices.
  194. */
  195. void miiphy_listdev (void)
  196. {
  197. struct list_head *entry;
  198. struct mii_dev *dev;
  199. puts ("MII devices: ");
  200. list_for_each (entry, &mii_devs) {
  201. dev = list_entry (entry, struct mii_dev, link);
  202. printf ("'%s' ", dev->name);
  203. }
  204. puts ("\n");
  205. if (current_mii)
  206. printf ("Current device: '%s'\n", current_mii->name);
  207. }
  208. /*****************************************************************************
  209. *
  210. * Read the OUI, manufacture's model number, and revision number.
  211. *
  212. * OUI: 22 bits (unsigned int)
  213. * Model: 6 bits (unsigned char)
  214. * Revision: 4 bits (unsigned char)
  215. *
  216. * Returns:
  217. * 0 on success
  218. */
  219. int miiphy_info (char *devname, unsigned char addr, unsigned int *oui,
  220. unsigned char *model, unsigned char *rev)
  221. {
  222. unsigned int reg = 0;
  223. unsigned short tmp;
  224. if (miiphy_read (devname, addr, PHY_PHYIDR2, &tmp) != 0) {
  225. #ifdef DEBUG
  226. puts ("PHY ID register 2 read failed\n");
  227. #endif
  228. return (-1);
  229. }
  230. reg = tmp;
  231. #ifdef DEBUG
  232. printf ("PHY_PHYIDR2 @ 0x%x = 0x%04x\n", addr, reg);
  233. #endif
  234. if (reg == 0xFFFF) {
  235. /* No physical device present at this address */
  236. return (-1);
  237. }
  238. if (miiphy_read (devname, addr, PHY_PHYIDR1, &tmp) != 0) {
  239. #ifdef DEBUG
  240. puts ("PHY ID register 1 read failed\n");
  241. #endif
  242. return (-1);
  243. }
  244. reg |= tmp << 16;
  245. #ifdef DEBUG
  246. printf ("PHY_PHYIDR[1,2] @ 0x%x = 0x%08x\n", addr, reg);
  247. #endif
  248. *oui = (reg >> 10);
  249. *model = (unsigned char)((reg >> 4) & 0x0000003F);
  250. *rev = (unsigned char)(reg & 0x0000000F);
  251. return (0);
  252. }
  253. /*****************************************************************************
  254. *
  255. * Reset the PHY.
  256. * Returns:
  257. * 0 on success
  258. */
  259. int miiphy_reset (char *devname, unsigned char addr)
  260. {
  261. unsigned short reg;
  262. int loop_cnt;
  263. if (miiphy_read (devname, addr, PHY_BMCR, &reg) != 0) {
  264. #ifdef DEBUG
  265. printf ("PHY status read failed\n");
  266. #endif
  267. return (-1);
  268. }
  269. if (miiphy_write (devname, addr, PHY_BMCR, reg | 0x8000) != 0) {
  270. #ifdef DEBUG
  271. puts ("PHY reset failed\n");
  272. #endif
  273. return (-1);
  274. }
  275. #ifdef CONFIG_PHY_RESET_DELAY
  276. udelay (CONFIG_PHY_RESET_DELAY); /* Intel LXT971A needs this */
  277. #endif
  278. /*
  279. * Poll the control register for the reset bit to go to 0 (it is
  280. * auto-clearing). This should happen within 0.5 seconds per the
  281. * IEEE spec.
  282. */
  283. loop_cnt = 0;
  284. reg = 0x8000;
  285. while (((reg & 0x8000) != 0) && (loop_cnt++ < 1000000)) {
  286. if (miiphy_read (devname, addr, PHY_BMCR, &reg) != 0) {
  287. # ifdef DEBUG
  288. puts ("PHY status read failed\n");
  289. # endif
  290. return (-1);
  291. }
  292. }
  293. if ((reg & 0x8000) == 0) {
  294. return (0);
  295. } else {
  296. puts ("PHY reset timed out\n");
  297. return (-1);
  298. }
  299. return (0);
  300. }
  301. /*****************************************************************************
  302. *
  303. * Determine the ethernet speed (10/100/1000). Return 10 on error.
  304. */
  305. int miiphy_speed (char *devname, unsigned char addr)
  306. {
  307. u16 bmcr, anlpar;
  308. #if defined(CONFIG_PHY_GIGE)
  309. u16 btsr;
  310. /*
  311. * Check for 1000BASE-X. If it is supported, then assume that the speed
  312. * is 1000.
  313. */
  314. if (miiphy_is_1000base_x (devname, addr)) {
  315. return _1000BASET;
  316. }
  317. /*
  318. * No 1000BASE-X, so assume 1000BASE-T/100BASE-TX/10BASE-T register set.
  319. */
  320. /* Check for 1000BASE-T. */
  321. if (miiphy_read (devname, addr, PHY_1000BTSR, &btsr)) {
  322. printf ("PHY 1000BT status");
  323. goto miiphy_read_failed;
  324. }
  325. if (btsr != 0xFFFF &&
  326. (btsr & (PHY_1000BTSR_1000FD | PHY_1000BTSR_1000HD))) {
  327. return _1000BASET;
  328. }
  329. #endif /* CONFIG_PHY_GIGE */
  330. /* Check Basic Management Control Register first. */
  331. if (miiphy_read (devname, addr, PHY_BMCR, &bmcr)) {
  332. printf ("PHY speed");
  333. goto miiphy_read_failed;
  334. }
  335. /* Check if auto-negotiation is on. */
  336. if (bmcr & PHY_BMCR_AUTON) {
  337. /* Get auto-negotiation results. */
  338. if (miiphy_read (devname, addr, PHY_ANLPAR, &anlpar)) {
  339. printf ("PHY AN speed");
  340. goto miiphy_read_failed;
  341. }
  342. return (anlpar & PHY_ANLPAR_100) ? _100BASET : _10BASET;
  343. }
  344. /* Get speed from basic control settings. */
  345. return (bmcr & PHY_BMCR_100MB) ? _100BASET : _10BASET;
  346. miiphy_read_failed:
  347. printf (" read failed, assuming 10BASE-T\n");
  348. return _10BASET;
  349. }
  350. /*****************************************************************************
  351. *
  352. * Determine full/half duplex. Return half on error.
  353. */
  354. int miiphy_duplex (char *devname, unsigned char addr)
  355. {
  356. u16 bmcr, anlpar;
  357. #if defined(CONFIG_PHY_GIGE)
  358. u16 btsr;
  359. /* Check for 1000BASE-X. */
  360. if (miiphy_is_1000base_x (devname, addr)) {
  361. /* 1000BASE-X */
  362. if (miiphy_read (devname, addr, PHY_ANLPAR, &anlpar)) {
  363. printf ("1000BASE-X PHY AN duplex");
  364. goto miiphy_read_failed;
  365. }
  366. }
  367. /*
  368. * No 1000BASE-X, so assume 1000BASE-T/100BASE-TX/10BASE-T register set.
  369. */
  370. /* Check for 1000BASE-T. */
  371. if (miiphy_read (devname, addr, PHY_1000BTSR, &btsr)) {
  372. printf ("PHY 1000BT status");
  373. goto miiphy_read_failed;
  374. }
  375. if (btsr != 0xFFFF) {
  376. if (btsr & PHY_1000BTSR_1000FD) {
  377. return FULL;
  378. } else if (btsr & PHY_1000BTSR_1000HD) {
  379. return HALF;
  380. }
  381. }
  382. #endif /* CONFIG_PHY_GIGE */
  383. /* Check Basic Management Control Register first. */
  384. if (miiphy_read (devname, addr, PHY_BMCR, &bmcr)) {
  385. puts ("PHY duplex");
  386. goto miiphy_read_failed;
  387. }
  388. /* Check if auto-negotiation is on. */
  389. if (bmcr & PHY_BMCR_AUTON) {
  390. /* Get auto-negotiation results. */
  391. if (miiphy_read (devname, addr, PHY_ANLPAR, &anlpar)) {
  392. puts ("PHY AN duplex");
  393. goto miiphy_read_failed;
  394. }
  395. return (anlpar & (PHY_ANLPAR_10FD | PHY_ANLPAR_TXFD)) ?
  396. FULL : HALF;
  397. }
  398. /* Get speed from basic control settings. */
  399. return (bmcr & PHY_BMCR_DPLX) ? FULL : HALF;
  400. miiphy_read_failed:
  401. printf (" read failed, assuming half duplex\n");
  402. return HALF;
  403. }
  404. /*****************************************************************************
  405. *
  406. * Return 1 if PHY supports 1000BASE-X, 0 if PHY supports 10BASE-T/100BASE-TX/
  407. * 1000BASE-T, or on error.
  408. */
  409. int miiphy_is_1000base_x (char *devname, unsigned char addr)
  410. {
  411. #if defined(CONFIG_PHY_GIGE)
  412. u16 exsr;
  413. if (miiphy_read (devname, addr, PHY_EXSR, &exsr)) {
  414. printf ("PHY extended status read failed, assuming no "
  415. "1000BASE-X\n");
  416. return 0;
  417. }
  418. return 0 != (exsr & (PHY_EXSR_1000XF | PHY_EXSR_1000XH));
  419. #else
  420. return 0;
  421. #endif
  422. }
  423. #ifdef CFG_FAULT_ECHO_LINK_DOWN
  424. /*****************************************************************************
  425. *
  426. * Determine link status
  427. */
  428. int miiphy_link (char *devname, unsigned char addr)
  429. {
  430. unsigned short reg;
  431. /* dummy read; needed to latch some phys */
  432. (void)miiphy_read (devname, addr, PHY_BMSR, &reg);
  433. if (miiphy_read (devname, addr, PHY_BMSR, &reg)) {
  434. puts ("PHY_BMSR read failed, assuming no link\n");
  435. return (0);
  436. }
  437. /* Determine if a link is active */
  438. if ((reg & PHY_BMSR_LS) != 0) {
  439. return (1);
  440. } else {
  441. return (0);
  442. }
  443. }
  444. #endif
  445. #endif /* CONFIG_MII */