ether.c 7.9 KB

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