dm9161.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * (C) Copyright 2003
  3. * Author : Hamid Ikdoumi (Atmel)
  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 <at91rm9200_net.h>
  24. #include <net.h>
  25. #include <dm9161.h>
  26. #ifdef CONFIG_DRIVER_ETHER
  27. #if (CONFIG_COMMANDS & CFG_CMD_NET)
  28. /*
  29. * Name:
  30. * dm9161_IsPhyConnected
  31. * Description:
  32. * Reads the 2 PHY ID registers
  33. * Arguments:
  34. * p_mac - pointer to AT91S_EMAC struct
  35. * Return value:
  36. * TRUE - if id read successfully
  37. * FALSE- if error
  38. */
  39. static unsigned int dm9161_IsPhyConnected (AT91PS_EMAC p_mac)
  40. {
  41. unsigned short Id1, Id2;
  42. at91rm9200_EmacEnableMDIO (p_mac);
  43. at91rm9200_EmacReadPhy (p_mac, DM9161_PHYID1, &Id1);
  44. at91rm9200_EmacReadPhy (p_mac, DM9161_PHYID2, &Id2);
  45. at91rm9200_EmacDisableMDIO (p_mac);
  46. if ((Id1 == (DM9161_PHYID1_OUI >> 6)) &&
  47. ((Id2 >> 10) == (DM9161_PHYID1_OUI & DM9161_LSB_MASK)))
  48. return TRUE;
  49. return FALSE;
  50. }
  51. /*
  52. * Name:
  53. * dm9161_GetLinkSpeed
  54. * Description:
  55. * Link parallel detection status of MAC is checked and set in the
  56. * MAC configuration registers
  57. * Arguments:
  58. * p_mac - pointer to MAC
  59. * Return value:
  60. * TRUE - if link status set succesfully
  61. * FALSE - if link status not set
  62. */
  63. static UCHAR dm9161_GetLinkSpeed (AT91PS_EMAC p_mac)
  64. {
  65. unsigned short stat1, stat2;
  66. if (!at91rm9200_EmacReadPhy (p_mac, DM9161_BMSR, &stat1))
  67. return FALSE;
  68. if (!(stat1 & DM9161_LINK_STATUS)) /* link status up? */
  69. return FALSE;
  70. if (!at91rm9200_EmacReadPhy (p_mac, DM9161_DSCSR, &stat2))
  71. return FALSE;
  72. if ((stat1 & DM9161_100BASE_TX_FD) && (stat2 & DM9161_100FDX)) {
  73. /*set Emac for 100BaseTX and Full Duplex */
  74. p_mac->EMAC_CFG |= AT91C_EMAC_SPD | AT91C_EMAC_FD;
  75. return TRUE;
  76. }
  77. if ((stat1 & DM9161_10BASE_T_FD) && (stat2 & DM9161_10FDX)) {
  78. /*set MII for 10BaseT and Full Duplex */
  79. p_mac->EMAC_CFG = (p_mac->EMAC_CFG &
  80. ~(AT91C_EMAC_SPD | AT91C_EMAC_FD))
  81. | AT91C_EMAC_FD;
  82. return TRUE;
  83. }
  84. if ((stat1 & DM9161_100BASE_T4_HD) && (stat2 & DM9161_100HDX)) {
  85. /*set MII for 100BaseTX and Half Duplex */
  86. p_mac->EMAC_CFG = (p_mac->EMAC_CFG &
  87. ~(AT91C_EMAC_SPD | AT91C_EMAC_FD))
  88. | AT91C_EMAC_SPD;
  89. return TRUE;
  90. }
  91. if ((stat1 & DM9161_10BASE_T_HD) && (stat2 & DM9161_10HDX)) {
  92. /*set MII for 10BaseT and Half Duplex */
  93. p_mac->EMAC_CFG &= ~(AT91C_EMAC_SPD | AT91C_EMAC_FD);
  94. return TRUE;
  95. }
  96. return FALSE;
  97. }
  98. /*
  99. * Name:
  100. * dm9161_InitPhy
  101. * Description:
  102. * MAC starts checking its link by using parallel detection and
  103. * Autonegotiation and the same is set in the MAC configuration registers
  104. * Arguments:
  105. * p_mac - pointer to struct AT91S_EMAC
  106. * Return value:
  107. * TRUE - if link status set succesfully
  108. * FALSE - if link status not set
  109. */
  110. static UCHAR dm9161_InitPhy (AT91PS_EMAC p_mac)
  111. {
  112. UCHAR ret = TRUE;
  113. unsigned short IntValue;
  114. at91rm9200_EmacEnableMDIO (p_mac);
  115. if (!dm9161_GetLinkSpeed (p_mac)) {
  116. /* Try another time */
  117. ret = dm9161_GetLinkSpeed (p_mac);
  118. }
  119. /* Disable PHY Interrupts */
  120. at91rm9200_EmacReadPhy (p_mac, DM9161_MDINTR, &IntValue);
  121. /* clear FDX, SPD, Link, INTR masks */
  122. IntValue &= ~(DM9161_FDX_MASK | DM9161_SPD_MASK |
  123. DM9161_LINK_MASK | DM9161_INTR_MASK);
  124. at91rm9200_EmacWritePhy (p_mac, DM9161_MDINTR, &IntValue);
  125. at91rm9200_EmacDisableMDIO (p_mac);
  126. return (ret);
  127. }
  128. /*
  129. * Name:
  130. * dm9161_AutoNegotiate
  131. * Description:
  132. * MAC Autonegotiates with the partner status of same is set in the
  133. * MAC configuration registers
  134. * Arguments:
  135. * dev - pointer to struct net_device
  136. * Return value:
  137. * TRUE - if link status set successfully
  138. * FALSE - if link status not set
  139. */
  140. static UCHAR dm9161_AutoNegotiate (AT91PS_EMAC p_mac, int *status)
  141. {
  142. unsigned short value;
  143. unsigned short PhyAnar;
  144. unsigned short PhyAnalpar;
  145. /* Set dm9161 control register */
  146. if (!at91rm9200_EmacReadPhy (p_mac, DM9161_BMCR, &value))
  147. return FALSE;
  148. value &= ~DM9161_AUTONEG; /* remove autonegotiation enable */
  149. value |= DM9161_ISOLATE; /* Electrically isolate PHY */
  150. if (!at91rm9200_EmacWritePhy (p_mac, DM9161_BMCR, &value))
  151. return FALSE;
  152. /* Set the Auto_negotiation Advertisement Register */
  153. /* MII advertising for Next page, 100BaseTxFD and HD, 10BaseTFD and HD, IEEE 802.3 */
  154. PhyAnar = DM9161_NP | DM9161_TX_FDX | DM9161_TX_HDX |
  155. DM9161_10_FDX | DM9161_10_HDX | DM9161_AN_IEEE_802_3;
  156. if (!at91rm9200_EmacWritePhy (p_mac, DM9161_ANAR, &PhyAnar))
  157. return FALSE;
  158. /* Read the Control Register */
  159. if (!at91rm9200_EmacReadPhy (p_mac, DM9161_BMCR, &value))
  160. return FALSE;
  161. value |= DM9161_SPEED_SELECT | DM9161_AUTONEG | DM9161_DUPLEX_MODE;
  162. if (!at91rm9200_EmacWritePhy (p_mac, DM9161_BMCR, &value))
  163. return FALSE;
  164. /* Restart Auto_negotiation */
  165. value |= DM9161_RESTART_AUTONEG;
  166. if (!at91rm9200_EmacWritePhy (p_mac, DM9161_BMCR, &value))
  167. return FALSE;
  168. /*check AutoNegotiate complete */
  169. udelay (10000);
  170. at91rm9200_EmacReadPhy (p_mac, DM9161_BMSR, &value);
  171. if (!(value & DM9161_AUTONEG_COMP))
  172. return FALSE;
  173. /* Get the AutoNeg Link partner base page */
  174. if (!at91rm9200_EmacReadPhy (p_mac, DM9161_ANLPAR, &PhyAnalpar))
  175. return FALSE;
  176. if ((PhyAnar & DM9161_TX_FDX) && (PhyAnalpar & DM9161_TX_FDX)) {
  177. /*set MII for 100BaseTX and Full Duplex */
  178. p_mac->EMAC_CFG |= AT91C_EMAC_SPD | AT91C_EMAC_FD;
  179. return TRUE;
  180. }
  181. if ((PhyAnar & DM9161_10_FDX) && (PhyAnalpar & DM9161_10_FDX)) {
  182. /*set MII for 10BaseT and Full Duplex */
  183. p_mac->EMAC_CFG = (p_mac->EMAC_CFG &
  184. ~(AT91C_EMAC_SPD | AT91C_EMAC_FD))
  185. | AT91C_EMAC_FD;
  186. return TRUE;
  187. }
  188. return FALSE;
  189. }
  190. /*
  191. * Name:
  192. * at91rm92000_GetPhyInterface
  193. * Description:
  194. * Initialise the interface functions to the PHY
  195. * Arguments:
  196. * None
  197. * Return value:
  198. * None
  199. */
  200. void at91rm92000_GetPhyInterface(AT91PS_PhyOps p_phyops)
  201. {
  202. p_phyops->Init = dm9161_InitPhy;
  203. p_phyops->IsPhyConnected = dm9161_IsPhyConnected;
  204. p_phyops->GetLinkSpeed = dm9161_GetLinkSpeed;
  205. p_phyops->AutoNegotiate = dm9161_AutoNegotiate;
  206. }
  207. #endif /* CONFIG_COMMANDS & CFG_CMD_NET */
  208. #endif /* CONFIG_DRIVER_ETHER */