mcfmii.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2004-2008 Freescale Semiconductor, Inc.
  4. * TsiChung Liew (Tsi-Chung.Liew@freescale.com)
  5. */
  6. #include <common.h>
  7. #include <config.h>
  8. #include <net.h>
  9. #include <netdev.h>
  10. #include <asm/global_data.h>
  11. #include <linux/delay.h>
  12. #include <asm/fec.h>
  13. #include <asm/immap.h>
  14. #include <linux/mii.h>
  15. DECLARE_GLOBAL_DATA_PTR;
  16. #if defined(CONFIG_CMD_NET)
  17. #undef MII_DEBUG
  18. #undef ET_DEBUG
  19. /*extern int fecpin_setclear(struct eth_device *dev, int setclear);*/
  20. #if defined(CONFIG_SYS_DISCOVER_PHY) || defined(CONFIG_CMD_MII)
  21. #include <miiphy.h>
  22. /* Make MII read/write commands for the FEC. */
  23. #define mk_mii_read(ADDR, REG) (0x60020000 | ((ADDR << 23) | \
  24. (REG & 0x1f) << 18))
  25. #define mk_mii_write(ADDR, REG, VAL) (0x50020000 | ((ADDR << 23) | \
  26. (REG & 0x1f) << 18) | (VAL & 0xffff))
  27. #ifndef CFG_SYS_UNSPEC_PHYID
  28. # define CFG_SYS_UNSPEC_PHYID 0
  29. #endif
  30. #ifndef CFG_SYS_UNSPEC_STRID
  31. # define CFG_SYS_UNSPEC_STRID 0
  32. #endif
  33. typedef struct phy_info_struct {
  34. u32 phyid;
  35. char *strid;
  36. } phy_info_t;
  37. phy_info_t phyinfo[] = {
  38. {0x0022561B, "AMD79C784VC"}, /* AMD 79C784VC */
  39. {0x00406322, "BCM5222"}, /* Broadcom 5222 */
  40. {0x02a80150, "Intel82555"}, /* Intel 82555 */
  41. {0x0016f870, "LSI80225"}, /* LSI 80225 */
  42. {0x0016f880, "LSI80225/B"}, /* LSI 80225/B */
  43. {0x78100000, "LXT970"}, /* LXT970 */
  44. {0x001378e0, "LXT971"}, /* LXT971 and 972 */
  45. {0x00221619, "KS8721BL"}, /* Micrel KS8721BL/SL */
  46. {0x00221512, "KSZ8041NL"}, /* Micrel KSZ8041NL */
  47. {0x20005CE1, "N83640"}, /* National 83640 */
  48. {0x20005C90, "N83848"}, /* National 83848 */
  49. {0x20005CA2, "N83849"}, /* National 83849 */
  50. {0x01814400, "QS6612"}, /* QS6612 */
  51. #if defined(CFG_SYS_UNSPEC_PHYID) && defined(CFG_SYS_UNSPEC_STRID)
  52. {CFG_SYS_UNSPEC_PHYID, CFG_SYS_UNSPEC_STRID},
  53. #endif
  54. {0, 0}
  55. };
  56. /*
  57. * mii_init -- Initialize the MII for MII command without ethernet
  58. * This function is a subset of eth_init
  59. */
  60. void mii_reset(fec_info_t *info)
  61. {
  62. volatile FEC_T *fecp = (FEC_T *) (info->miibase);
  63. int i;
  64. fecp->ecr = FEC_ECR_RESET;
  65. for (i = 0; (fecp->ecr & FEC_ECR_RESET) && (i < FEC_RESET_DELAY); ++i) {
  66. udelay(1);
  67. }
  68. if (i == FEC_RESET_DELAY)
  69. printf("FEC_RESET_DELAY timeout\n");
  70. }
  71. /* send command to phy using mii, wait for result */
  72. uint mii_send(uint mii_cmd)
  73. {
  74. struct udevice *dev;
  75. fec_info_t *info;
  76. volatile FEC_T *ep;
  77. uint mii_reply;
  78. int j = 0;
  79. /* retrieve from register structure */
  80. dev = eth_get_dev();
  81. info = dev_get_priv(dev);
  82. ep = (FEC_T *) info->miibase;
  83. ep->mmfr = mii_cmd; /* command to phy */
  84. /* wait for mii complete */
  85. while (!(ep->eir & FEC_EIR_MII) && (j < info->to_loop)) {
  86. udelay(1);
  87. j++;
  88. }
  89. if (j >= info->to_loop) {
  90. printf("MII not complete\n");
  91. return -1;
  92. }
  93. mii_reply = ep->mmfr; /* result from phy */
  94. ep->eir = FEC_EIR_MII; /* clear MII complete */
  95. #ifdef ET_DEBUG
  96. printf("%s[%d] %s: sent=0x%8.8x, reply=0x%8.8x\n",
  97. __FILE__, __LINE__, __FUNCTION__, mii_cmd, mii_reply);
  98. #endif
  99. return (mii_reply & 0xffff); /* data read from phy */
  100. }
  101. #endif /* CONFIG_SYS_DISCOVER_PHY || (CONFIG_MII) */
  102. #if defined(CONFIG_SYS_DISCOVER_PHY)
  103. int mii_discover_phy(fec_info_t *info)
  104. {
  105. #define MAX_PHY_PASSES 11
  106. int phyaddr, pass;
  107. uint phyno, phytype;
  108. int i, found = 0;
  109. if (info->phyname_init)
  110. return info->phy_addr;
  111. phyaddr = -1; /* didn't find a PHY yet */
  112. for (pass = 1; pass <= MAX_PHY_PASSES && phyaddr < 0; ++pass) {
  113. if (pass > 1) {
  114. /* PHY may need more time to recover from reset.
  115. * The LXT970 needs 50ms typical, no maximum is
  116. * specified, so wait 10ms before try again.
  117. * With 11 passes this gives it 100ms to wake up.
  118. */
  119. udelay(10000); /* wait 10ms */
  120. }
  121. for (phyno = 0; phyno < 32 && phyaddr < 0; ++phyno) {
  122. phytype = mii_send(mk_mii_read(phyno, MII_PHYSID1));
  123. #ifdef ET_DEBUG
  124. printf("PHY type 0x%x pass %d\n", phytype, pass);
  125. #endif
  126. if (phytype == 0xffff)
  127. continue;
  128. phyaddr = phyno;
  129. phytype <<= 16;
  130. phytype |=
  131. mii_send(mk_mii_read(phyno, MII_PHYSID2));
  132. #ifdef ET_DEBUG
  133. printf("PHY @ 0x%x pass %d\n", phyno, pass);
  134. #endif
  135. for (i = 0; (i < ARRAY_SIZE(phyinfo))
  136. && (phyinfo[i].phyid != 0); i++) {
  137. if (phyinfo[i].phyid == phytype) {
  138. #ifdef ET_DEBUG
  139. printf("phyid %x - %s\n",
  140. phyinfo[i].phyid,
  141. phyinfo[i].strid);
  142. #endif
  143. strcpy(info->phy_name, phyinfo[i].strid);
  144. info->phyname_init = 1;
  145. found = 1;
  146. break;
  147. }
  148. }
  149. if (!found) {
  150. #ifdef ET_DEBUG
  151. printf("0x%08x\n", phytype);
  152. #endif
  153. strcpy(info->phy_name, "unknown");
  154. info->phyname_init = 1;
  155. break;
  156. }
  157. }
  158. }
  159. if (phyaddr < 0)
  160. printf("No PHY device found.\n");
  161. return phyaddr;
  162. }
  163. #endif /* CONFIG_SYS_DISCOVER_PHY */
  164. __weak void mii_init(void)
  165. {
  166. struct udevice *dev;
  167. fec_info_t *info;
  168. volatile FEC_T *fecp;
  169. int miispd = 0, i = 0;
  170. u16 status = 0;
  171. u16 linkgood = 0;
  172. /* retrieve from register structure */
  173. dev = eth_get_dev();
  174. info = dev_get_priv(dev);
  175. fecp = (FEC_T *) info->miibase;
  176. fecpin_setclear(info, 1);
  177. mii_reset(info);
  178. /* We use strictly polling mode only */
  179. fecp->eimr = 0;
  180. /* Clear any pending interrupt */
  181. fecp->eir = 0xffffffff;
  182. /* Set MII speed */
  183. miispd = (gd->bus_clk / 1000000) / 5;
  184. fecp->mscr = miispd << 1;
  185. #ifdef CONFIG_SYS_DISCOVER_PHY
  186. info->phy_addr = mii_discover_phy(info);
  187. #endif
  188. if (info->phy_addr == -1)
  189. return;
  190. while (i < info->to_loop) {
  191. status = 0;
  192. i++;
  193. /* Read PHY control register */
  194. miiphy_read(dev->name, info->phy_addr, MII_BMCR, &status);
  195. /* If phy set to autonegotiate, wait for autonegotiation done,
  196. * if phy is not autonegotiating, just wait for link up.
  197. */
  198. if ((status & BMCR_ANENABLE) == BMCR_ANENABLE) {
  199. linkgood = (BMSR_ANEGCOMPLETE | BMSR_LSTATUS);
  200. } else {
  201. linkgood = BMSR_LSTATUS;
  202. }
  203. /* Read PHY status register */
  204. miiphy_read(dev->name, info->phy_addr, MII_BMSR, &status);
  205. if ((status & linkgood) == linkgood)
  206. break;
  207. udelay(1);
  208. }
  209. if (i >= info->to_loop)
  210. printf("Link UP timeout\n");
  211. /* adapt to the duplex and speed settings of the phy */
  212. info->dup_spd = miiphy_duplex(dev->name, info->phy_addr) << 16;
  213. info->dup_spd |= miiphy_speed(dev->name, info->phy_addr);
  214. }
  215. /*
  216. * Read and write a MII PHY register, routines used by MII Utilities
  217. *
  218. * FIXME: These routines are expected to return 0 on success, but mii_send
  219. * does _not_ return an error code. Maybe 0xFFFF means error, i.e.
  220. * no PHY connected...
  221. * For now always return 0.
  222. * FIXME: These routines only work after calling eth_init() at least once!
  223. * Otherwise they hang in mii_send() !!! Sorry!
  224. */
  225. int mcffec_miiphy_read(struct mii_dev *bus, int addr, int devad, int reg)
  226. {
  227. short rdreg; /* register working value */
  228. #ifdef MII_DEBUG
  229. printf("miiphy_read(0x%x) @ 0x%x = ", reg, addr);
  230. #endif
  231. rdreg = mii_send(mk_mii_read(addr, reg));
  232. #ifdef MII_DEBUG
  233. printf("0x%04x\n", rdreg);
  234. #endif
  235. return rdreg;
  236. }
  237. int mcffec_miiphy_write(struct mii_dev *bus, int addr, int devad, int reg,
  238. u16 value)
  239. {
  240. #ifdef MII_DEBUG
  241. printf("miiphy_write(0x%x) @ 0x%x = 0x%04x\n", reg, addr, value);
  242. #endif
  243. mii_send(mk_mii_write(addr, reg, value));
  244. return 0;
  245. }
  246. #endif /* CONFIG_CMD_NET */