miiphyutil.c 11 KB

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