emac_adapter.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /******************************************************************************
  2. *
  3. * Author: Xilinx, Inc.
  4. *
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2 of the License, or (at your
  9. * option) any later version.
  10. *
  11. *
  12. * XILINX IS PROVIDING THIS DESIGN, CODE, OR INFORMATION "AS IS" AS A
  13. * COURTESY TO YOU. BY PROVIDING THIS DESIGN, CODE, OR INFORMATION AS
  14. * ONE POSSIBLE IMPLEMENTATION OF THIS FEATURE, APPLICATION OR STANDARD,
  15. * XILINX IS MAKING NO REPRESENTATION THAT THIS IMPLEMENTATION IS FREE
  16. * FROM ANY CLAIMS OF INFRINGEMENT, AND YOU ARE RESPONSIBLE FOR OBTAINING
  17. * ANY THIRD PARTY RIGHTS YOU MAY REQUIRE FOR YOUR IMPLEMENTATION.
  18. * XILINX EXPRESSLY DISCLAIMS ANY WARRANTY WHATSOEVER WITH RESPECT TO
  19. * THE ADEQUACY OF THE IMPLEMENTATION, INCLUDING BUT NOT LIMITED TO ANY
  20. * WARRANTIES OR REPRESENTATIONS THAT THIS IMPLEMENTATION IS FREE FROM
  21. * CLAIMS OF INFRINGEMENT, IMPLIED WARRANTIES OF MERCHANTABILITY AND
  22. * FITNESS FOR A PARTICULAR PURPOSE.
  23. *
  24. *
  25. * Xilinx hardware products are not intended for use in life support
  26. * appliances, devices, or systems. Use in such applications is
  27. * expressly prohibited.
  28. *
  29. *
  30. * (c) Copyright 2002-2004 Xilinx Inc.
  31. * All rights reserved.
  32. *
  33. *
  34. * You should have received a copy of the GNU General Public License along
  35. * with this program; if not, write to the Free Software Foundation, Inc.,
  36. * 675 Mass Ave, Cambridge, MA 02139, USA.
  37. *
  38. ******************************************************************************/
  39. #include <config.h>
  40. #include <common.h>
  41. #include <net.h>
  42. #include "xemac.h"
  43. #if defined(XPAR_EMAC_0_DEVICE_ID)
  44. /*
  45. * ENET_MAX_MTU and ENET_MAX_MTU_ALIGNED are set from
  46. * PKTSIZE and PKTSIZE_ALIGN (include/net.h)
  47. */
  48. #define ENET_MAX_MTU PKTSIZE
  49. #define ENET_MAX_MTU_ALIGNED PKTSIZE_ALIGN
  50. #define ENET_ADDR_LENGTH 6
  51. static XEmac Emac;
  52. static char etherrxbuff[PKTSIZE_ALIGN]; /* Receive buffer */
  53. /* hardcoded MAC address for the Xilinx EMAC Core when env is nowhere*/
  54. #ifdef CONFIG_ENV_IS_NOWHERE
  55. static u8 EMACAddr[ENET_ADDR_LENGTH] = { 0x00, 0x0a, 0x35, 0x00, 0x22, 0x01 };
  56. #endif
  57. static int initialized = 0;
  58. void
  59. eth_halt(void)
  60. {
  61. if (initialized)
  62. (void) XEmac_Stop(&Emac);
  63. }
  64. int
  65. eth_init(bd_t * bis)
  66. {
  67. u32 Options;
  68. XStatus Result;
  69. uchar enetaddr[6];
  70. #ifdef DEBUG
  71. printf("EMAC Initialization Started\n\r");
  72. #endif
  73. Result = XEmac_Initialize(&Emac, XPAR_EMAC_0_DEVICE_ID);
  74. if (Result != XST_SUCCESS) {
  75. return 0;
  76. }
  77. /* make sure the Emac is stopped before it is started */
  78. (void) XEmac_Stop(&Emac);
  79. if (!eth_getenv_enetaddr("ethaddr", enetaddr)) {
  80. #ifdef CONFIG_ENV_IS_NOWHERE
  81. memcpy(enetaddr, EMACAddr, 6);
  82. eth_setenv_enetaddr("ethaddr", enetaddr);
  83. #endif
  84. }
  85. Result = XEmac_SetMacAddress(&Emac, enetaddr);
  86. if (Result != XST_SUCCESS) {
  87. return 0;
  88. }
  89. Options =
  90. (XEM_POLLED_OPTION | XEM_UNICAST_OPTION | XEM_BROADCAST_OPTION |
  91. XEM_FDUPLEX_OPTION | XEM_INSERT_FCS_OPTION |
  92. XEM_INSERT_PAD_OPTION);
  93. Result = XEmac_SetOptions(&Emac, Options);
  94. if (Result != XST_SUCCESS) {
  95. return 0;
  96. }
  97. Result = XEmac_Start(&Emac);
  98. if (Result != XST_SUCCESS) {
  99. return 0;
  100. }
  101. #ifdef DEBUG
  102. printf("EMAC Initialization complete\n\r");
  103. #endif
  104. initialized = 1;
  105. return (0);
  106. }
  107. /*-----------------------------------------------------------------------------+
  108. +-----------------------------------------------------------------------------*/
  109. int
  110. eth_send(volatile void *ptr, int len)
  111. {
  112. XStatus Result;
  113. if (len > ENET_MAX_MTU)
  114. len = ENET_MAX_MTU;
  115. Result = XEmac_PollSend(&Emac, (u8 *) ptr, len);
  116. if (Result == XST_SUCCESS) {
  117. return (1);
  118. } else {
  119. printf("Error while sending frame\n\r");
  120. return (0);
  121. }
  122. }
  123. int
  124. eth_rx(void)
  125. {
  126. u32 RecvFrameLength;
  127. XStatus Result;
  128. RecvFrameLength = PKTSIZE;
  129. Result = XEmac_PollRecv(&Emac, (u8 *) etherrxbuff, &RecvFrameLength);
  130. if (Result == XST_SUCCESS) {
  131. #ifndef CONFIG_EMACLITE
  132. NetReceive((uchar *)etherrxbuff, RecvFrameLength);
  133. #else
  134. NetReceive(etherrxbuff, RecvFrameLength);
  135. #endif
  136. return (1);
  137. } else {
  138. return (0);
  139. }
  140. }
  141. #endif