mii.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. * Copyright (C) 2004-2007 Freescale Semiconductor, Inc.
  3. * TsiChung Liew (Tsi-Chung.Liew@freescale.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. #include <common.h>
  24. #include <asm/fec.h>
  25. #include <asm/immap.h>
  26. #include <config.h>
  27. #include <net.h>
  28. DECLARE_GLOBAL_DATA_PTR;
  29. #if defined(CONFIG_CMD_NET) && defined(CONFIG_NET_MULTI)
  30. #undef MII_DEBUG
  31. #undef ET_DEBUG
  32. int fecpin_setclear(struct eth_device *dev, int setclear)
  33. {
  34. volatile gpio_t *gpio = (gpio_t *) MMAP_GPIO;
  35. if (setclear) {
  36. gpio->gpio_pbcnt |= GPIO_PBCNT_E_MDC | GPIO_PBCNT_E_RXER | GPIO_PBCNT_E_RXD1 | GPIO_PBCNT_E_RXD2 | GPIO_PBCNT_E_RXD3 | GPIO_PBCNT_E_TXD1 | GPIO_PBCNT_E_TXD2 | GPIO_PBCNT_E_TXD3;
  37. } else {
  38. }
  39. return 0;
  40. }
  41. #if defined(CFG_DISCOVER_PHY) || defined(CONFIG_CMD_MII)
  42. #include <miiphy.h>
  43. /* Make MII read/write commands for the FEC. */
  44. #define mk_mii_read(ADDR, REG) (0x60020000 | ((ADDR << 23) | (REG & 0x1f) << 18))
  45. #define mk_mii_write(ADDR, REG, VAL) (0x50020000 | ((ADDR << 23) | (REG & 0x1f) << 18) | (VAL & 0xffff))
  46. /* PHY identification */
  47. #define PHY_ID_LXT970 0x78100000 /* LXT970 */
  48. #define PHY_ID_LXT971 0x001378e0 /* LXT971 and 972 */
  49. #define PHY_ID_82555 0x02a80150 /* Intel 82555 */
  50. #define PHY_ID_QS6612 0x01814400 /* QS6612 */
  51. #define PHY_ID_AMD79C784 0x00225610 /* AMD 79C784 */
  52. #define PHY_ID_AMD79C874VC 0x0022561B /* AMD 79C874 */
  53. #define PHY_ID_LSI80225 0x0016f870 /* LSI 80225 */
  54. #define PHY_ID_LSI80225B 0x0016f880 /* LSI 80225/B */
  55. #define PHY_ID_DP83848VV 0x20005C90 /* National 83848 */
  56. #define PHY_ID_DP83849 0x20005CA2 /* National 82849 */
  57. #define STR_ID_LXT970 "LXT970"
  58. #define STR_ID_LXT971 "LXT971"
  59. #define STR_ID_82555 "Intel82555"
  60. #define STR_ID_QS6612 "QS6612"
  61. #define STR_ID_AMD79C784 "AMD79C784"
  62. #define STR_ID_AMD79C874VC "AMD79C874VC"
  63. #define STR_ID_LSI80225 "LSI80225"
  64. #define STR_ID_LSI80225B "LSI80225/B"
  65. #define STR_ID_DP83848VV "N83848"
  66. #define STR_ID_DP83849 "N83849"
  67. /****************************************************************************
  68. * mii_init -- Initialize the MII for MII command without ethernet
  69. * This function is a subset of eth_init
  70. ****************************************************************************
  71. */
  72. void mii_reset(struct fec_info_s *info)
  73. {
  74. volatile fec_t *fecp = (fec_t *) (info->miibase);
  75. int i;
  76. fecp->ecr = FEC_ECR_RESET;
  77. for (i = 0; (fecp->ecr & FEC_ECR_RESET) && (i < FEC_RESET_DELAY); ++i) {
  78. udelay(1);
  79. }
  80. if (i == FEC_RESET_DELAY) {
  81. printf("FEC_RESET_DELAY timeout\n");
  82. }
  83. }
  84. /* send command to phy using mii, wait for result */
  85. uint mii_send(uint mii_cmd)
  86. {
  87. struct fec_info_s *info;
  88. struct eth_device *dev;
  89. volatile fec_t *ep;
  90. uint mii_reply;
  91. int j = 0;
  92. /* retrieve from register structure */
  93. dev = eth_get_dev();
  94. info = dev->priv;
  95. ep = (fec_t *) info->miibase;
  96. ep->mmfr = mii_cmd; /* command to phy */
  97. /* wait for mii complete */
  98. while (!(ep->eir & FEC_EIR_MII) && (j < MCFFEC_TOUT_LOOP)) {
  99. udelay(1);
  100. j++;
  101. }
  102. if (j >= MCFFEC_TOUT_LOOP) {
  103. printf("MII not complete\n");
  104. return -1;
  105. }
  106. mii_reply = ep->mmfr; /* result from phy */
  107. ep->eir = FEC_EIR_MII; /* clear MII complete */
  108. #ifdef ET_DEBUG
  109. printf("%s[%d] %s: sent=0x%8.8x, reply=0x%8.8x\n",
  110. __FILE__, __LINE__, __FUNCTION__, mii_cmd, mii_reply);
  111. #endif
  112. return (mii_reply & 0xffff); /* data read from phy */
  113. }
  114. #endif /* CFG_DISCOVER_PHY || (CONFIG_COMMANDS & CONFIG_CMD_MII) */
  115. #if defined(CFG_DISCOVER_PHY)
  116. int mii_discover_phy(struct eth_device *dev)
  117. {
  118. #define MAX_PHY_PASSES 11
  119. struct fec_info_s *info = dev->priv;
  120. int phyaddr, pass;
  121. uint phyno, phytype;
  122. if (info->phyname_init)
  123. return info->phy_addr;
  124. phyaddr = -1; /* didn't find a PHY yet */
  125. for (pass = 1; pass <= MAX_PHY_PASSES && phyaddr < 0; ++pass) {
  126. if (pass > 1) {
  127. /* PHY may need more time to recover from reset.
  128. * The LXT970 needs 50ms typical, no maximum is
  129. * specified, so wait 10ms before try again.
  130. * With 11 passes this gives it 100ms to wake up.
  131. */
  132. udelay(10000); /* wait 10ms */
  133. }
  134. for (phyno = 0; phyno < 32 && phyaddr < 0; ++phyno) {
  135. phytype = mii_send(mk_mii_read(phyno, PHY_PHYIDR1));
  136. #ifdef ET_DEBUG
  137. printf("PHY type 0x%x pass %d type\n", phytype, pass);
  138. #endif
  139. if (phytype != 0xffff) {
  140. phyaddr = phyno;
  141. phytype <<= 16;
  142. phytype |=
  143. mii_send(mk_mii_read(phyno, PHY_PHYIDR2));
  144. switch (phytype & 0xffffffff) {
  145. case PHY_ID_AMD79C874VC:
  146. strcpy(info->phy_name,
  147. STR_ID_AMD79C874VC);
  148. info->phyname_init = 1;
  149. break;
  150. default:
  151. strcpy(info->phy_name, "unknown");
  152. info->phyname_init = 1;
  153. break;
  154. }
  155. #ifdef ET_DEBUG
  156. printf("PHY @ 0x%x pass %d type ", phyno, pass);
  157. switch (phytype & 0xffffffff) {
  158. case PHY_ID_AMD79C874VC:
  159. printf(STR_ID_AMD79C874VC);
  160. break;
  161. default:
  162. printf("0x%08x\n", phytype);
  163. break;
  164. }
  165. #endif
  166. }
  167. }
  168. }
  169. if (phyaddr < 0)
  170. printf("No PHY device found.\n");
  171. return phyaddr;
  172. }
  173. #endif /* CFG_DISCOVER_PHY */
  174. int mii_init(void) __attribute__((weak,alias("__mii_init")));
  175. void __mii_init(void)
  176. {
  177. volatile fec_t *fecp;
  178. struct fec_info_s *info;
  179. struct eth_device *dev;
  180. int miispd = 0, i = 0;
  181. u16 autoneg = 0;
  182. /* retrieve from register structure */
  183. dev = eth_get_dev();
  184. info = dev->priv;
  185. fecp = (fec_t *) info->miibase;
  186. fecpin_setclear(dev, 1);
  187. mii_reset(info);
  188. /* We use strictly polling mode only */
  189. fecp->eimr = 0;
  190. /* Clear any pending interrupt */
  191. fecp->eir = 0xffffffff;
  192. /* Set MII speed */
  193. miispd = (gd->bus_clk / 1000000) / 5;
  194. fecp->mscr = miispd << 1;
  195. info->phy_addr = mii_discover_phy(dev);
  196. #define AUTONEGLINK (PHY_BMSR_AUTN_COMP | PHY_BMSR_LS)
  197. while (i < MCFFEC_TOUT_LOOP) {
  198. autoneg = 0;
  199. miiphy_read(dev->name, info->phy_addr, PHY_BMSR, &autoneg);
  200. i++;
  201. if ((autoneg & AUTONEGLINK) == AUTONEGLINK)
  202. break;
  203. udelay(500);
  204. }
  205. if (i >= MCFFEC_TOUT_LOOP) {
  206. printf("Auto Negotiation not complete\n");
  207. }
  208. /* adapt to the half/full speed settings */
  209. info->dup_spd = miiphy_duplex(dev->name, info->phy_addr) << 16;
  210. info->dup_spd |= miiphy_speed(dev->name, info->phy_addr);
  211. }
  212. /*****************************************************************************
  213. * Read and write a MII PHY register, routines used by MII Utilities
  214. *
  215. * FIXME: These routines are expected to return 0 on success, but mii_send
  216. * does _not_ return an error code. Maybe 0xFFFF means error, i.e.
  217. * no PHY connected...
  218. * For now always return 0.
  219. * FIXME: These routines only work after calling eth_init() at least once!
  220. * Otherwise they hang in mii_send() !!! Sorry!
  221. *****************************************************************************/
  222. int mcffec_miiphy_read(char *devname, unsigned char addr, unsigned char reg,
  223. unsigned short *value)
  224. {
  225. short rdreg; /* register working value */
  226. #ifdef MII_DEBUG
  227. printf("miiphy_read(0x%x) @ 0x%x = ", reg, addr);
  228. #endif
  229. rdreg = mii_send(mk_mii_read(addr, reg));
  230. *value = rdreg;
  231. #ifdef MII_DEBUG
  232. printf("0x%04x\n", *value);
  233. #endif
  234. return 0;
  235. }
  236. int mcffec_miiphy_write(char *devname, unsigned char addr, unsigned char reg,
  237. unsigned short value)
  238. {
  239. short rdreg; /* register working value */
  240. #ifdef MII_DEBUG
  241. printf("miiphy_write(0x%x) @ 0x%x = ", reg, addr);
  242. #endif
  243. rdreg = mii_send(mk_mii_write(addr, reg, value));
  244. #ifdef MII_DEBUG
  245. printf("0x%04x\n", value);
  246. #endif
  247. return 0;
  248. }
  249. #endif /* CONFIG_CMD_NET, FEC_ENET & NET_MULTI */