mcfmii.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. phyaddr = phyno;
  154. phytype <<= 16;
  155. phytype |=
  156. mii_send(mk_mii_read(phyno, PHY_PHYIDR2));
  157. #ifdef ET_DEBUG
  158. printf("PHY @ 0x%x pass %d\n", phyno, pass);
  159. #endif
  160. for (i = 0; i < (sizeof(phyinfo) / sizeof(phy_info_t)); i++) {
  161. if (phyinfo[i].phyid == phytype) {
  162. #ifdef ET_DEBUG
  163. printf("phyid %x - %s\n",
  164. phyinfo[i].phyid,
  165. phyinfo[i].strid);
  166. #endif
  167. strcpy(info->phy_name, phyinfo[i].strid);
  168. info->phyname_init = 1;
  169. found = 1;
  170. break;
  171. }
  172. }
  173. if (!found) {
  174. #ifdef ET_DEBUG
  175. printf("0x%08x\n", phytype);
  176. #endif
  177. strcpy(info->phy_name, "unknown");
  178. info->phyname_init = 1;
  179. break;
  180. }
  181. }
  182. }
  183. }
  184. if (phyaddr < 0)
  185. printf("No PHY device found.\n");
  186. return phyaddr;
  187. }
  188. #endif /* CONFIG_SYS_DISCOVER_PHY */
  189. void mii_init(void) __attribute__((weak,alias("__mii_init")));
  190. void __mii_init(void)
  191. {
  192. FEC_INFO_T *info;
  193. volatile FEC_T *fecp;
  194. struct eth_device *dev;
  195. int miispd = 0, i = 0;
  196. u16 status = 0;
  197. u16 linkgood = 0;
  198. /* retrieve from register structure */
  199. dev = eth_get_dev();
  200. info = dev->priv;
  201. fecp = (FEC_T *) info->miibase;
  202. fecpin_setclear(dev, 1);
  203. mii_reset(info);
  204. /* We use strictly polling mode only */
  205. fecp->eimr = 0;
  206. /* Clear any pending interrupt */
  207. fecp->eir = 0xffffffff;
  208. /* Set MII speed */
  209. miispd = (gd->bus_clk / 1000000) / 5;
  210. fecp->mscr = miispd << 1;
  211. info->phy_addr = mii_discover_phy(dev);
  212. while (i < MCFFEC_TOUT_LOOP) {
  213. status = 0;
  214. i++;
  215. /* Read PHY control register */
  216. miiphy_read(dev->name, info->phy_addr, PHY_BMCR, &status);
  217. /* If phy set to autonegotiate, wait for autonegotiation done,
  218. * if phy is not autonegotiating, just wait for link up.
  219. */
  220. if ((status & PHY_BMCR_AUTON) == PHY_BMCR_AUTON) {
  221. linkgood = (PHY_BMSR_AUTN_COMP | PHY_BMSR_LS);
  222. } else {
  223. linkgood = PHY_BMSR_LS;
  224. }
  225. /* Read PHY status register */
  226. miiphy_read(dev->name, info->phy_addr, PHY_BMSR, &status);
  227. if ((status & linkgood) == linkgood)
  228. break;
  229. udelay(500);
  230. }
  231. if (i >= MCFFEC_TOUT_LOOP) {
  232. printf("Link UP timeout\n");
  233. }
  234. /* adapt to the duplex and speed settings of the phy */
  235. info->dup_spd = miiphy_duplex(dev->name, info->phy_addr) << 16;
  236. info->dup_spd |= miiphy_speed(dev->name, info->phy_addr);
  237. }
  238. /*
  239. * Read and write a MII PHY register, routines used by MII Utilities
  240. *
  241. * FIXME: These routines are expected to return 0 on success, but mii_send
  242. * does _not_ return an error code. Maybe 0xFFFF means error, i.e.
  243. * no PHY connected...
  244. * For now always return 0.
  245. * FIXME: These routines only work after calling eth_init() at least once!
  246. * Otherwise they hang in mii_send() !!! Sorry!
  247. */
  248. int mcffec_miiphy_read(char *devname, unsigned char addr, unsigned char reg,
  249. unsigned short *value)
  250. {
  251. short rdreg; /* register working value */
  252. #ifdef MII_DEBUG
  253. printf("miiphy_read(0x%x) @ 0x%x = ", reg, addr);
  254. #endif
  255. rdreg = mii_send(mk_mii_read(addr, reg));
  256. *value = rdreg;
  257. #ifdef MII_DEBUG
  258. printf("0x%04x\n", *value);
  259. #endif
  260. return 0;
  261. }
  262. int mcffec_miiphy_write(char *devname, unsigned char addr, unsigned char reg,
  263. unsigned short value)
  264. {
  265. short rdreg; /* register working value */
  266. #ifdef MII_DEBUG
  267. printf("miiphy_write(0x%x) @ 0x%x = ", reg, addr);
  268. #endif
  269. rdreg = mii_send(mk_mii_write(addr, reg, value));
  270. #ifdef MII_DEBUG
  271. printf("0x%04x\n", value);
  272. #endif
  273. return 0;
  274. }
  275. #endif /* CONFIG_CMD_NET, FEC_ENET & NET_MULTI */