mii.c 8.4 KB

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