ether.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 <miiphy.h>
  26. /* ----- Ethernet Buffer definitions ----- */
  27. typedef struct {
  28. unsigned long addr, size;
  29. } rbf_t;
  30. #define RBF_ADDR 0xfffffffc
  31. #define RBF_OWNER (1<<0)
  32. #define RBF_WRAP (1<<1)
  33. #define RBF_BROADCAST (1<<31)
  34. #define RBF_MULTICAST (1<<30)
  35. #define RBF_UNICAST (1<<29)
  36. #define RBF_EXTERNAL (1<<28)
  37. #define RBF_UNKOWN (1<<27)
  38. #define RBF_SIZE 0x07ff
  39. #define RBF_LOCAL4 (1<<26)
  40. #define RBF_LOCAL3 (1<<25)
  41. #define RBF_LOCAL2 (1<<24)
  42. #define RBF_LOCAL1 (1<<23)
  43. #define RBF_FRAMEMAX 64
  44. #define RBF_FRAMELEN 0x600
  45. #ifdef CONFIG_DRIVER_ETHER
  46. #if defined(CONFIG_CMD_NET)
  47. /* alignment as per Errata #11 (64 bytes) is insufficient! */
  48. rbf_t rbfdt[RBF_FRAMEMAX] __attribute__((aligned(512)));
  49. rbf_t *rbfp;
  50. unsigned char rbf_framebuf[RBF_FRAMEMAX][RBF_FRAMELEN]
  51. __attribute__((aligned(4)));
  52. /* structure to interface the PHY */
  53. AT91S_PhyOps PhyOps;
  54. AT91PS_EMAC p_mac;
  55. /*********** EMAC Phy layer Management functions *************************/
  56. /*
  57. * Name:
  58. * at91rm9200_EmacEnableMDIO
  59. * Description:
  60. * Enables the MDIO bit in MAC control register
  61. * Arguments:
  62. * p_mac - pointer to struct AT91S_EMAC
  63. * Return value:
  64. * none
  65. */
  66. void at91rm9200_EmacEnableMDIO (AT91PS_EMAC p_mac)
  67. {
  68. /* Mac CTRL reg set for MDIO enable */
  69. p_mac->EMAC_CTL |= AT91C_EMAC_MPE; /* Management port enable */
  70. }
  71. /*
  72. * Name:
  73. * at91rm9200_EmacDisableMDIO
  74. * Description:
  75. * Disables the MDIO bit in MAC control register
  76. * Arguments:
  77. * p_mac - pointer to struct AT91S_EMAC
  78. * Return value:
  79. * none
  80. */
  81. void at91rm9200_EmacDisableMDIO (AT91PS_EMAC p_mac)
  82. {
  83. /* Mac CTRL reg set for MDIO disable */
  84. p_mac->EMAC_CTL &= ~AT91C_EMAC_MPE; /* Management port disable */
  85. }
  86. /*
  87. * Name:
  88. * at91rm9200_EmacReadPhy
  89. * Description:
  90. * Reads data from the PHY register
  91. * Arguments:
  92. * dev - pointer to struct net_device
  93. * RegisterAddress - unsigned char
  94. * pInput - pointer to value read from register
  95. * Return value:
  96. * TRUE - if data read successfully
  97. */
  98. UCHAR at91rm9200_EmacReadPhy (AT91PS_EMAC p_mac,
  99. unsigned char RegisterAddress,
  100. unsigned short *pInput)
  101. {
  102. p_mac->EMAC_MAN = (AT91C_EMAC_HIGH & ~AT91C_EMAC_LOW) |
  103. (AT91C_EMAC_RW_R) |
  104. (RegisterAddress << 18) |
  105. (AT91C_EMAC_CODE_802_3);
  106. udelay (10000);
  107. *pInput = (unsigned short) p_mac->EMAC_MAN;
  108. return TRUE;
  109. }
  110. /*
  111. * Name:
  112. * at91rm9200_EmacWritePhy
  113. * Description:
  114. * Writes data to the PHY register
  115. * Arguments:
  116. * dev - pointer to struct net_device
  117. * RegisterAddress - unsigned char
  118. * pOutput - pointer to value to be written in the register
  119. * Return value:
  120. * TRUE - if data read successfully
  121. */
  122. UCHAR at91rm9200_EmacWritePhy (AT91PS_EMAC p_mac,
  123. unsigned char RegisterAddress,
  124. unsigned short *pOutput)
  125. {
  126. p_mac->EMAC_MAN = (AT91C_EMAC_HIGH & ~AT91C_EMAC_LOW) |
  127. AT91C_EMAC_CODE_802_3 | AT91C_EMAC_RW_W |
  128. (RegisterAddress << 18) | *pOutput;
  129. udelay (10000);
  130. return TRUE;
  131. }
  132. int eth_init (bd_t * bd)
  133. {
  134. int ret;
  135. int i;
  136. uchar enetaddr[6];
  137. p_mac = AT91C_BASE_EMAC;
  138. /* PIO Disable Register */
  139. *AT91C_PIOA_PDR = AT91C_PA16_EMDIO | AT91C_PA15_EMDC | AT91C_PA14_ERXER |
  140. AT91C_PA13_ERX1 | AT91C_PA12_ERX0 | AT91C_PA11_ECRS_ECRSDV |
  141. AT91C_PA10_ETX1 | AT91C_PA9_ETX0 | AT91C_PA8_ETXEN |
  142. AT91C_PA7_ETXCK_EREFCK;
  143. #ifdef CONFIG_AT91C_USE_RMII
  144. *AT91C_PIOB_PDR = AT91C_PB19_ERXCK;
  145. *AT91C_PIOB_BSR = AT91C_PB19_ERXCK;
  146. #else
  147. *AT91C_PIOB_PDR = AT91C_PB19_ERXCK | AT91C_PB18_ECOL | AT91C_PB17_ERXDV |
  148. AT91C_PB16_ERX3 | AT91C_PB15_ERX2 | AT91C_PB14_ETXER |
  149. AT91C_PB13_ETX3 | AT91C_PB12_ETX2;
  150. /* Select B Register */
  151. *AT91C_PIOB_BSR = AT91C_PB19_ERXCK | AT91C_PB18_ECOL |
  152. AT91C_PB17_ERXDV | AT91C_PB16_ERX3 | AT91C_PB15_ERX2 |
  153. AT91C_PB14_ETXER | AT91C_PB13_ETX3 | AT91C_PB12_ETX2;
  154. #endif
  155. *AT91C_PMC_PCER = 1 << AT91C_ID_EMAC; /* Peripheral Clock Enable Register */
  156. p_mac->EMAC_CFG |= AT91C_EMAC_CSR; /* Clear statistics */
  157. /* Init Ehternet buffers */
  158. for (i = 0; i < RBF_FRAMEMAX; i++) {
  159. rbfdt[i].addr = (unsigned long)rbf_framebuf[i];
  160. rbfdt[i].size = 0;
  161. }
  162. rbfdt[RBF_FRAMEMAX - 1].addr |= RBF_WRAP;
  163. rbfp = &rbfdt[0];
  164. eth_getenv_enetaddr("ethaddr", enetaddr);
  165. p_mac->EMAC_SA2L = (enetaddr[3] << 24) | (enetaddr[2] << 16)
  166. | (enetaddr[1] << 8) | (enetaddr[0]);
  167. p_mac->EMAC_SA2H = (enetaddr[5] << 8) | (enetaddr[4]);
  168. p_mac->EMAC_RBQP = (long) (&rbfdt[0]);
  169. p_mac->EMAC_RSR &= ~(AT91C_EMAC_RSR_OVR | AT91C_EMAC_REC | AT91C_EMAC_BNA);
  170. p_mac->EMAC_CFG = (p_mac->EMAC_CFG | AT91C_EMAC_CAF | AT91C_EMAC_NBC)
  171. & ~AT91C_EMAC_CLK;
  172. #ifdef CONFIG_AT91C_USE_RMII
  173. p_mac->EMAC_CFG |= AT91C_EMAC_RMII;
  174. #endif
  175. #if (AT91C_MASTER_CLOCK > 40000000)
  176. /* MDIO clock must not exceed 2.5 MHz, so enable MCK divider */
  177. p_mac->EMAC_CFG |= AT91C_EMAC_CLK_HCLK_64;
  178. #endif
  179. p_mac->EMAC_CTL |= AT91C_EMAC_TE | AT91C_EMAC_RE;
  180. at91rm9200_GetPhyInterface (& PhyOps);
  181. if (!PhyOps.IsPhyConnected (p_mac))
  182. printf ("PHY not connected!!\n\r");
  183. /* MII management start from here */
  184. if (!(p_mac->EMAC_SR & AT91C_EMAC_LINK)) {
  185. if (!(ret = PhyOps.Init (p_mac))) {
  186. printf ("MAC: error during MII initialization\n");
  187. return 0;
  188. }
  189. } else {
  190. printf ("No link\n\r");
  191. return 0;
  192. }
  193. return 0;
  194. }
  195. int eth_send (volatile void *packet, int length)
  196. {
  197. while (!(p_mac->EMAC_TSR & AT91C_EMAC_BNQ));
  198. p_mac->EMAC_TAR = (long) packet;
  199. p_mac->EMAC_TCR = length;
  200. while (p_mac->EMAC_TCR & 0x7ff);
  201. p_mac->EMAC_TSR |= AT91C_EMAC_COMP;
  202. return 0;
  203. }
  204. int eth_rx (void)
  205. {
  206. int size;
  207. if (!(rbfp->addr & RBF_OWNER))
  208. return 0;
  209. size = rbfp->size & RBF_SIZE;
  210. NetReceive ((volatile uchar *) (rbfp->addr & RBF_ADDR), size);
  211. rbfp->addr &= ~RBF_OWNER;
  212. if (rbfp->addr & RBF_WRAP)
  213. rbfp = &rbfdt[0];
  214. else
  215. rbfp++;
  216. p_mac->EMAC_RSR |= AT91C_EMAC_REC;
  217. return size;
  218. }
  219. void eth_halt (void)
  220. {
  221. };
  222. #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
  223. int at91rm9200_miiphy_read(char *devname, unsigned char addr,
  224. unsigned char reg, unsigned short * value)
  225. {
  226. at91rm9200_EmacEnableMDIO (p_mac);
  227. at91rm9200_EmacReadPhy (p_mac, reg, value);
  228. at91rm9200_EmacDisableMDIO (p_mac);
  229. return 0;
  230. }
  231. int at91rm9200_miiphy_write(char *devname, unsigned char addr,
  232. unsigned char reg, unsigned short value)
  233. {
  234. at91rm9200_EmacEnableMDIO (p_mac);
  235. at91rm9200_EmacWritePhy (p_mac, reg, &value);
  236. at91rm9200_EmacDisableMDIO (p_mac);
  237. return 0;
  238. }
  239. #endif
  240. int at91rm9200_miiphy_initialize(bd_t *bis)
  241. {
  242. #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
  243. miiphy_register("at91rm9200phy", at91rm9200_miiphy_read, at91rm9200_miiphy_write);
  244. #endif
  245. return 0;
  246. }
  247. #endif
  248. #endif /* CONFIG_DRIVER_ETHER */