xemac_options.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. /*****************************************************************************/
  40. /**
  41. *
  42. * @file xemac_options.c
  43. *
  44. * Functions in this file handle configuration of the XEmac driver.
  45. *
  46. * <pre>
  47. * MODIFICATION HISTORY:
  48. *
  49. * Ver Who Date Changes
  50. * ----- ---- -------- -----------------------------------------------
  51. * 1.00a rpm 07/31/01 First release
  52. * 1.00b rpm 02/20/02 Repartitioned files and functions
  53. * 1.00c rpm 12/05/02 New version includes support for simple DMA
  54. * </pre>
  55. *
  56. ******************************************************************************/
  57. /***************************** Include Files *********************************/
  58. #include "xbasic_types.h"
  59. #include "xemac_i.h"
  60. #include "xio.h"
  61. /************************** Constant Definitions *****************************/
  62. #define XEM_MAX_IFG 32 /* Maximum Interframe gap value */
  63. /**************************** Type Definitions *******************************/
  64. /***************** Macros (Inline Functions) Definitions *********************/
  65. /************************** Function Prototypes ******************************/
  66. /************************** Variable Definitions *****************************/
  67. /*
  68. * A table of options and masks. This table maps the user-visible options with
  69. * the control register masks. It is used in Set/GetOptions as an alternative
  70. * to a series of if/else pairs. Note that the polled options does not have a
  71. * corresponding entry in the control register, so it does not exist in the
  72. * table.
  73. */
  74. typedef struct {
  75. u32 Option;
  76. u32 Mask;
  77. } OptionMap;
  78. static OptionMap OptionsTable[] = {
  79. {XEM_UNICAST_OPTION, XEM_ECR_UNICAST_ENABLE_MASK},
  80. {XEM_BROADCAST_OPTION, XEM_ECR_BROAD_ENABLE_MASK},
  81. {XEM_PROMISC_OPTION, XEM_ECR_PROMISC_ENABLE_MASK},
  82. {XEM_FDUPLEX_OPTION, XEM_ECR_FULL_DUPLEX_MASK},
  83. {XEM_LOOPBACK_OPTION, XEM_ECR_LOOPBACK_MASK},
  84. {XEM_MULTICAST_OPTION, XEM_ECR_MULTI_ENABLE_MASK},
  85. {XEM_FLOW_CONTROL_OPTION, XEM_ECR_PAUSE_FRAME_MASK},
  86. {XEM_INSERT_PAD_OPTION, XEM_ECR_XMIT_PAD_ENABLE_MASK},
  87. {XEM_INSERT_FCS_OPTION, XEM_ECR_XMIT_FCS_ENABLE_MASK},
  88. {XEM_INSERT_ADDR_OPTION, XEM_ECR_XMIT_ADDR_INSERT_MASK},
  89. {XEM_OVWRT_ADDR_OPTION, XEM_ECR_XMIT_ADDR_OVWRT_MASK},
  90. {XEM_STRIP_PAD_FCS_OPTION, XEM_ECR_RECV_STRIP_ENABLE_MASK}
  91. };
  92. #define XEM_NUM_OPTIONS (sizeof(OptionsTable) / sizeof(OptionMap))
  93. /*****************************************************************************/
  94. /**
  95. *
  96. * Set Ethernet driver/device options. The device must be stopped before
  97. * calling this function. The options are contained within a bit-mask with each
  98. * bit representing an option (i.e., you can OR the options together). A one (1)
  99. * in the bit-mask turns an option on, and a zero (0) turns the option off.
  100. *
  101. * @param InstancePtr is a pointer to the XEmac instance to be worked on.
  102. * @param OptionsFlag is a bit-mask representing the Ethernet options to turn on
  103. * or off. See xemac.h for a description of the available options.
  104. *
  105. * @return
  106. *
  107. * - XST_SUCCESS if the options were set successfully
  108. * - XST_DEVICE_IS_STARTED if the device has not yet been stopped
  109. *
  110. * @note
  111. *
  112. * This function is not thread-safe and makes use of internal resources that are
  113. * shared between the Start, Stop, and SetOptions functions, so if one task
  114. * might be setting device options while another is trying to start the device,
  115. * protection of this shared data (typically using a semaphore) is required.
  116. *
  117. ******************************************************************************/
  118. XStatus
  119. XEmac_SetOptions(XEmac * InstancePtr, u32 OptionsFlag)
  120. {
  121. u32 ControlReg;
  122. int Index;
  123. XASSERT_NONVOID(InstancePtr != NULL);
  124. XASSERT_NONVOID(InstancePtr->IsReady == XCOMPONENT_IS_READY);
  125. if (InstancePtr->IsStarted == XCOMPONENT_IS_STARTED) {
  126. return XST_DEVICE_IS_STARTED;
  127. }
  128. ControlReg = XIo_In32(InstancePtr->BaseAddress + XEM_ECR_OFFSET);
  129. /*
  130. * Loop through the options table, turning the option on or off
  131. * depending on whether the bit is set in the incoming options flag.
  132. */
  133. for (Index = 0; Index < XEM_NUM_OPTIONS; Index++) {
  134. if (OptionsFlag & OptionsTable[Index].Option) {
  135. ControlReg |= OptionsTable[Index].Mask; /* turn it on */
  136. } else {
  137. ControlReg &= ~OptionsTable[Index].Mask; /* turn it off */
  138. }
  139. }
  140. /*
  141. * TODO: need to validate addr-overwrite only if addr-insert?
  142. */
  143. /*
  144. * Now write the control register. Leave it to the upper layers
  145. * to restart the device.
  146. */
  147. XIo_Out32(InstancePtr->BaseAddress + XEM_ECR_OFFSET, ControlReg);
  148. /*
  149. * Check the polled option
  150. */
  151. if (OptionsFlag & XEM_POLLED_OPTION) {
  152. InstancePtr->IsPolled = TRUE;
  153. } else {
  154. InstancePtr->IsPolled = FALSE;
  155. }
  156. return XST_SUCCESS;
  157. }
  158. /*****************************************************************************/
  159. /**
  160. *
  161. * Get Ethernet driver/device options. The 32-bit value returned is a bit-mask
  162. * representing the options. A one (1) in the bit-mask means the option is on,
  163. * and a zero (0) means the option is off.
  164. *
  165. * @param InstancePtr is a pointer to the XEmac instance to be worked on.
  166. *
  167. * @return
  168. *
  169. * The 32-bit value of the Ethernet options. The value is a bit-mask
  170. * representing all options that are currently enabled. See xemac.h for a
  171. * description of the available options.
  172. *
  173. * @note
  174. *
  175. * None.
  176. *
  177. ******************************************************************************/
  178. u32
  179. XEmac_GetOptions(XEmac * InstancePtr)
  180. {
  181. u32 OptionsFlag = 0;
  182. u32 ControlReg;
  183. int Index;
  184. XASSERT_NONVOID(InstancePtr != NULL);
  185. XASSERT_NONVOID(InstancePtr->IsReady == XCOMPONENT_IS_READY);
  186. /*
  187. * Get the control register to determine which options are currently set.
  188. */
  189. ControlReg = XIo_In32(InstancePtr->BaseAddress + XEM_ECR_OFFSET);
  190. /*
  191. * Loop through the options table to determine which options are set
  192. */
  193. for (Index = 0; Index < XEM_NUM_OPTIONS; Index++) {
  194. if (ControlReg & OptionsTable[Index].Mask) {
  195. OptionsFlag |= OptionsTable[Index].Option;
  196. }
  197. }
  198. if (InstancePtr->IsPolled) {
  199. OptionsFlag |= XEM_POLLED_OPTION;
  200. }
  201. return OptionsFlag;
  202. }
  203. /*****************************************************************************/
  204. /**
  205. *
  206. * Set the Interframe Gap (IFG), which is the time the MAC delays between
  207. * transmitting frames. There are two parts required. The total interframe gap
  208. * is the total of the two parts. The values provided for the Part1 and Part2
  209. * parameters are multiplied by 4 to obtain the bit-time interval. The first
  210. * part should be the first 2/3 of the total interframe gap. The MAC will reset
  211. * the interframe gap timer if carrier sense becomes true during the period
  212. * defined by interframe gap Part1. Part1 may be shorter than 2/3 the total and
  213. * can be as small as zero. The second part should be the last 1/3 of the total
  214. * interframe gap, but can be as large as the total interframe gap. The MAC
  215. * will not reset the interframe gap timer if carrier sense becomes true during
  216. * the period defined by interframe gap Part2.
  217. *
  218. * The device must be stopped before setting the interframe gap.
  219. *
  220. * @param InstancePtr is a pointer to the XEmac instance to be worked on.
  221. * @param Part1 is the interframe gap part 1 (which will be multiplied by 4 to
  222. * get the bit-time interval).
  223. * @param Part2 is the interframe gap part 2 (which will be multiplied by 4 to
  224. * get the bit-time interval).
  225. *
  226. * @return
  227. *
  228. * - XST_SUCCESS if the interframe gap was set successfully
  229. * - XST_DEVICE_IS_STARTED if the device has not been stopped
  230. *
  231. * @note
  232. *
  233. * None.
  234. *
  235. ******************************************************************************/
  236. XStatus
  237. XEmac_SetInterframeGap(XEmac * InstancePtr, u8 Part1, u8 Part2)
  238. {
  239. u32 Ifg;
  240. XASSERT_NONVOID(InstancePtr != NULL);
  241. XASSERT_NONVOID(Part1 < XEM_MAX_IFG);
  242. XASSERT_NONVOID(Part2 < XEM_MAX_IFG);
  243. XASSERT_NONVOID(InstancePtr->IsReady == XCOMPONENT_IS_READY);
  244. /*
  245. * Be sure device has been stopped
  246. */
  247. if (InstancePtr->IsStarted == XCOMPONENT_IS_STARTED) {
  248. return XST_DEVICE_IS_STARTED;
  249. }
  250. Ifg = Part1 << XEM_IFGP_PART1_SHIFT;
  251. Ifg |= (Part2 << XEM_IFGP_PART2_SHIFT);
  252. XIo_Out32(InstancePtr->BaseAddress + XEM_IFGP_OFFSET, Ifg);
  253. return XST_SUCCESS;
  254. }
  255. /*****************************************************************************/
  256. /**
  257. *
  258. * Get the interframe gap, parts 1 and 2. See the description of interframe gap
  259. * above in XEmac_SetInterframeGap().
  260. *
  261. * @param InstancePtr is a pointer to the XEmac instance to be worked on.
  262. * @param Part1Ptr is a pointer to an 8-bit buffer into which the interframe gap
  263. * part 1 value will be copied.
  264. * @param Part2Ptr is a pointer to an 8-bit buffer into which the interframe gap
  265. * part 2 value will be copied.
  266. *
  267. * @return
  268. *
  269. * None. The values of the interframe gap parts are copied into the
  270. * output parameters.
  271. *
  272. ******************************************************************************/
  273. void
  274. XEmac_GetInterframeGap(XEmac * InstancePtr, u8 * Part1Ptr, u8 * Part2Ptr)
  275. {
  276. u32 Ifg;
  277. XASSERT_VOID(InstancePtr != NULL);
  278. XASSERT_VOID(Part1Ptr != NULL);
  279. XASSERT_VOID(Part2Ptr != NULL);
  280. XASSERT_VOID(InstancePtr->IsReady == XCOMPONENT_IS_READY);
  281. Ifg = XIo_In32(InstancePtr->BaseAddress + XEM_IFGP_OFFSET);
  282. *Part1Ptr = (Ifg & XEM_IFGP_PART1_MASK) >> XEM_IFGP_PART1_SHIFT;
  283. *Part2Ptr = (Ifg & XEM_IFGP_PART2_MASK) >> XEM_IFGP_PART2_SHIFT;
  284. }