mii.c 8.4 KB

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