mcfmii.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*
  2. * Copyright (C) 2004-2008 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 <netdev.h>
  27. #ifdef CONFIG_MCF547x_8x
  28. #include <asm/fsl_mcdmafec.h>
  29. #else
  30. #include <asm/fec.h>
  31. #endif
  32. #include <asm/immap.h>
  33. DECLARE_GLOBAL_DATA_PTR;
  34. #if defined(CONFIG_CMD_NET) && defined(CONFIG_NET_MULTI)
  35. #undef MII_DEBUG
  36. #undef ET_DEBUG
  37. /*extern int fecpin_setclear(struct eth_device *dev, int setclear);*/
  38. #if defined(CONFIG_SYS_DISCOVER_PHY) || defined(CONFIG_CMD_MII)
  39. #include <miiphy.h>
  40. /* Make MII read/write commands for the FEC. */
  41. #define mk_mii_read(ADDR, REG) (0x60020000 | ((ADDR << 23) | \
  42. (REG & 0x1f) << 18))
  43. #define mk_mii_write(ADDR, REG, VAL) (0x50020000 | ((ADDR << 23) | \
  44. (REG & 0x1f) << 18) | (VAL & 0xffff))
  45. #ifndef CONFIG_SYS_UNSPEC_PHYID
  46. # define CONFIG_SYS_UNSPEC_PHYID 0
  47. #endif
  48. #ifndef CONFIG_SYS_UNSPEC_STRID
  49. # define CONFIG_SYS_UNSPEC_STRID 0
  50. #endif
  51. #ifdef CONFIG_MCF547x_8x
  52. typedef struct fec_info_dma FEC_INFO_T;
  53. #define FEC_T fecdma_t
  54. #else
  55. typedef struct fec_info_s FEC_INFO_T;
  56. #define FEC_T fec_t
  57. #endif
  58. typedef struct phy_info_struct {
  59. u32 phyid;
  60. char *strid;
  61. } phy_info_t;
  62. phy_info_t phyinfo[] = {
  63. {0x0022561B, "AMD79C784VC"}, /* AMD 79C784VC */
  64. {0x00406322, "BCM5222"}, /* Broadcom 5222 */
  65. {0x02a80150, "Intel82555"}, /* Intel 82555 */
  66. {0x0016f870, "LSI80225"}, /* LSI 80225 */
  67. {0x0016f880, "LSI80225/B"}, /* LSI 80225/B */
  68. {0x78100000, "LXT970"}, /* LXT970 */
  69. {0x001378e0, "LXT971"}, /* LXT971 and 972 */
  70. {0x00221619, "KS8721BL"}, /* Micrel KS8721BL/SL */
  71. {0x00221512, "KSZ8041NL"}, /* Micrel KSZ8041NL */
  72. {0x20005CE1, "N83640"}, /* National 83640 */
  73. {0x20005C90, "N83848"}, /* National 83848 */
  74. {0x20005CA2, "N83849"}, /* National 83849 */
  75. {0x01814400, "QS6612"}, /* QS6612 */
  76. #if defined(CONFIG_SYS_UNSPEC_PHYID) && defined(CONFIG_SYS_UNSPEC_STRID)
  77. {CONFIG_SYS_UNSPEC_PHYID, CONFIG_SYS_UNSPEC_STRID},
  78. #endif
  79. {0, 0}
  80. };
  81. /*
  82. * mii_init -- Initialize the MII for MII command without ethernet
  83. * This function is a subset of eth_init
  84. */
  85. void mii_reset(FEC_INFO_T *info)
  86. {
  87. volatile FEC_T *fecp = (FEC_T *) (info->miibase);
  88. int i;
  89. fecp->ecr = FEC_ECR_RESET;
  90. for (i = 0; (fecp->ecr & FEC_ECR_RESET) && (i < FEC_RESET_DELAY); ++i) {
  91. udelay(1);
  92. }
  93. if (i == FEC_RESET_DELAY)
  94. printf("FEC_RESET_DELAY timeout\n");
  95. }
  96. /* send command to phy using mii, wait for result */
  97. uint mii_send(uint mii_cmd)
  98. {
  99. FEC_INFO_T *info;
  100. volatile FEC_T *ep;
  101. struct eth_device *dev;
  102. uint mii_reply;
  103. int j = 0;
  104. /* retrieve from register structure */
  105. dev = eth_get_dev();
  106. info = dev->priv;
  107. ep = (FEC_T *) info->miibase;
  108. ep->mmfr = mii_cmd; /* command to phy */
  109. /* wait for mii complete */
  110. while (!(ep->eir & FEC_EIR_MII) && (j < MCFFEC_TOUT_LOOP)) {
  111. udelay(1);
  112. j++;
  113. }
  114. if (j >= MCFFEC_TOUT_LOOP) {
  115. printf("MII not complete\n");
  116. return -1;
  117. }
  118. mii_reply = ep->mmfr; /* result from phy */
  119. ep->eir = FEC_EIR_MII; /* clear MII complete */
  120. #ifdef ET_DEBUG
  121. printf("%s[%d] %s: sent=0x%8.8x, reply=0x%8.8x\n",
  122. __FILE__, __LINE__, __FUNCTION__, mii_cmd, mii_reply);
  123. #endif
  124. return (mii_reply & 0xffff); /* data read from phy */
  125. }
  126. #endif /* CONFIG_SYS_DISCOVER_PHY || (CONFIG_MII) */
  127. #if defined(CONFIG_SYS_DISCOVER_PHY)
  128. int mii_discover_phy(struct eth_device *dev)
  129. {
  130. #define MAX_PHY_PASSES 11
  131. FEC_INFO_T *info = dev->priv;
  132. int phyaddr, pass;
  133. uint phyno, phytype;
  134. int i, found = 0;
  135. if (info->phyname_init)
  136. return info->phy_addr;
  137. phyaddr = -1; /* didn't find a PHY yet */
  138. for (pass = 1; pass <= MAX_PHY_PASSES && phyaddr < 0; ++pass) {
  139. if (pass > 1) {
  140. /* PHY may need more time to recover from reset.
  141. * The LXT970 needs 50ms typical, no maximum is
  142. * specified, so wait 10ms before try again.
  143. * With 11 passes this gives it 100ms to wake up.
  144. */
  145. udelay(10000); /* wait 10ms */
  146. }
  147. for (phyno = 0; phyno < 32 && phyaddr < 0; ++phyno) {
  148. phytype = mii_send(mk_mii_read(phyno, PHY_PHYIDR1));
  149. #ifdef ET_DEBUG
  150. printf("PHY type 0x%x pass %d type\n", phytype, pass);
  151. #endif
  152. if (phytype == 0xffff)
  153. continue;
  154. phyaddr = phyno;
  155. phytype <<= 16;
  156. phytype |=
  157. mii_send(mk_mii_read(phyno, PHY_PHYIDR2));
  158. #ifdef ET_DEBUG
  159. printf("PHY @ 0x%x pass %d\n", phyno, pass);
  160. #endif
  161. for (i = 0; (i < (sizeof(phyinfo) / sizeof(phy_info_t)))
  162. && (phyinfo[i].phyid != 0); i++) {
  163. if (phyinfo[i].phyid == phytype) {
  164. #ifdef ET_DEBUG
  165. printf("phyid %x - %s\n",
  166. phyinfo[i].phyid,
  167. phyinfo[i].strid);
  168. #endif
  169. strcpy(info->phy_name, phyinfo[i].strid);
  170. info->phyname_init = 1;
  171. found = 1;
  172. break;
  173. }
  174. }
  175. if (!found) {
  176. #ifdef ET_DEBUG
  177. printf("0x%08x\n", phytype);
  178. #endif
  179. strcpy(info->phy_name, "unknown");
  180. info->phyname_init = 1;
  181. break;
  182. }
  183. }
  184. }
  185. if (phyaddr < 0)
  186. printf("No PHY device found.\n");
  187. return phyaddr;
  188. }
  189. #endif /* CONFIG_SYS_DISCOVER_PHY */
  190. void mii_init(void) __attribute__((weak,alias("__mii_init")));
  191. void __mii_init(void)
  192. {
  193. FEC_INFO_T *info;
  194. volatile FEC_T *fecp;
  195. struct eth_device *dev;
  196. int miispd = 0, i = 0;
  197. u16 status = 0;
  198. u16 linkgood = 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. while (i < MCFFEC_TOUT_LOOP) {
  214. status = 0;
  215. i++;
  216. /* Read PHY control register */
  217. miiphy_read(dev->name, info->phy_addr, PHY_BMCR, &status);
  218. /* If phy set to autonegotiate, wait for autonegotiation done,
  219. * if phy is not autonegotiating, just wait for link up.
  220. */
  221. if ((status & PHY_BMCR_AUTON) == PHY_BMCR_AUTON) {
  222. linkgood = (PHY_BMSR_AUTN_COMP | PHY_BMSR_LS);
  223. } else {
  224. linkgood = PHY_BMSR_LS;
  225. }
  226. /* Read PHY status register */
  227. miiphy_read(dev->name, info->phy_addr, PHY_BMSR, &status);
  228. if ((status & linkgood) == linkgood)
  229. break;
  230. udelay(1);
  231. }
  232. if (i >= MCFFEC_TOUT_LOOP) {
  233. printf("Link UP timeout\n");
  234. }
  235. /* adapt to the duplex and speed settings of the phy */
  236. info->dup_spd = miiphy_duplex(dev->name, info->phy_addr) << 16;
  237. info->dup_spd |= miiphy_speed(dev->name, info->phy_addr);
  238. }
  239. /*
  240. * Read and write a MII PHY register, routines used by MII Utilities
  241. *
  242. * FIXME: These routines are expected to return 0 on success, but mii_send
  243. * does _not_ return an error code. Maybe 0xFFFF means error, i.e.
  244. * no PHY connected...
  245. * For now always return 0.
  246. * FIXME: These routines only work after calling eth_init() at least once!
  247. * Otherwise they hang in mii_send() !!! Sorry!
  248. */
  249. int mcffec_miiphy_read(const char *devname, unsigned char addr, unsigned char reg,
  250. unsigned short *value)
  251. {
  252. short rdreg; /* register working value */
  253. #ifdef MII_DEBUG
  254. printf("miiphy_read(0x%x) @ 0x%x = ", reg, addr);
  255. #endif
  256. rdreg = mii_send(mk_mii_read(addr, reg));
  257. *value = rdreg;
  258. #ifdef MII_DEBUG
  259. printf("0x%04x\n", *value);
  260. #endif
  261. return 0;
  262. }
  263. int mcffec_miiphy_write(const char *devname, unsigned char addr, unsigned char reg,
  264. unsigned short value)
  265. {
  266. short rdreg; /* register working value */
  267. #ifdef MII_DEBUG
  268. printf("miiphy_write(0x%x) @ 0x%x = ", reg, addr);
  269. #endif
  270. rdreg = mii_send(mk_mii_write(addr, reg, value));
  271. #ifdef MII_DEBUG
  272. printf("0x%04x\n", value);
  273. #endif
  274. return 0;
  275. }
  276. #endif /* CONFIG_CMD_NET, FEC_ENET & NET_MULTI */