miiphyutil.c 11 KB

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