emac_adapter.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 CFG_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. #ifdef DEBUG
  70. printf("EMAC Initialization Started\n\r");
  71. #endif
  72. Result = XEmac_Initialize(&Emac, XPAR_EMAC_0_DEVICE_ID);
  73. if (Result != XST_SUCCESS) {
  74. return 0;
  75. }
  76. /* make sure the Emac is stopped before it is started */
  77. (void) XEmac_Stop(&Emac);
  78. #ifdef CFG_ENV_IS_NOWHERE
  79. memcpy(bis->bi_enetaddr, EMACAddr, 6);
  80. #endif
  81. Result = XEmac_SetMacAddress(&Emac, bis->bi_enetaddr);
  82. if (Result != XST_SUCCESS) {
  83. return 0;
  84. }
  85. Options =
  86. (XEM_POLLED_OPTION | XEM_UNICAST_OPTION | XEM_BROADCAST_OPTION |
  87. XEM_FDUPLEX_OPTION | XEM_INSERT_FCS_OPTION |
  88. XEM_INSERT_PAD_OPTION);
  89. Result = XEmac_SetOptions(&Emac, Options);
  90. if (Result != XST_SUCCESS) {
  91. return 0;
  92. }
  93. Result = XEmac_Start(&Emac);
  94. if (Result != XST_SUCCESS) {
  95. return 0;
  96. }
  97. #ifdef DEBUG
  98. printf("EMAC Initialization complete\n\r");
  99. #endif
  100. initialized = 1;
  101. return (0);
  102. }
  103. /*-----------------------------------------------------------------------------+
  104. +-----------------------------------------------------------------------------*/
  105. int
  106. eth_send(volatile void *ptr, int len)
  107. {
  108. XStatus Result;
  109. if (len > ENET_MAX_MTU)
  110. len = ENET_MAX_MTU;
  111. Result = XEmac_PollSend(&Emac, (u8 *) ptr, len);
  112. if (Result == XST_SUCCESS) {
  113. return (1);
  114. } else {
  115. printf("Error while sending frame\n\r");
  116. return (0);
  117. }
  118. }
  119. int
  120. eth_rx(void)
  121. {
  122. u32 RecvFrameLength;
  123. XStatus Result;
  124. RecvFrameLength = PKTSIZE;
  125. Result = XEmac_PollRecv(&Emac, (u8 *) etherrxbuff, &RecvFrameLength);
  126. if (Result == XST_SUCCESS) {
  127. #ifndef CONFIG_MICROBLAZE
  128. NetReceive((uchar *)etherrxbuff, RecvFrameLength);
  129. #else
  130. NetReceive(etherrxbuff, RecvFrameLength);
  131. #endif
  132. return (1);
  133. } else {
  134. return (0);
  135. }
  136. }
  137. #endif