miiphyutil.c 12 KB

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