ether.c 7.3 KB

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