xemac.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  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.c
  43. *
  44. * The XEmac driver. Functions in this file are the minimum required functions
  45. * for this driver. See xemac.h for a detailed description of the driver.
  46. *
  47. * <pre>
  48. * MODIFICATION HISTORY:
  49. *
  50. * Ver Who Date Changes
  51. * ----- ---- -------- -------------------------------------------------------
  52. * 1.00a rpm 07/31/01 First release
  53. * 1.00b rpm 02/20/02 Repartitioned files and functions
  54. * 1.00b rpm 07/23/02 Removed the PHY reset from Initialize()
  55. * 1.00b rmm 09/23/02 Removed commented code in Initialize(). Recycled as
  56. * XEmac_mPhyReset macro in xemac_l.h.
  57. * 1.00c rpm 12/05/02 New version includes support for simple DMA
  58. * 1.00c rpm 12/12/02 Changed location of IsStarted assignment in XEmac_Start
  59. * to be sure the flag is set before the device and
  60. * interrupts are enabled.
  61. * 1.00c rpm 02/03/03 SelfTest was not clearing polled mode. Take driver out
  62. * of polled mode in XEmac_Reset() to fix this problem.
  63. * 1.00c rmm 05/13/03 Fixed diab compiler warnings relating to asserts.
  64. * </pre>
  65. ******************************************************************************/
  66. /***************************** Include Files *********************************/
  67. #include "xbasic_types.h"
  68. #include "xemac_i.h"
  69. #include "xio.h"
  70. #include "xipif_v1_23_b.h" /* Uses v1.23b of the IPIF */
  71. /************************** Constant Definitions *****************************/
  72. /**************************** Type Definitions *******************************/
  73. /***************** Macros (Inline Functions) Definitions *********************/
  74. /************************** Function Prototypes ******************************/
  75. static XStatus ConfigureDma(XEmac * InstancePtr);
  76. static XStatus ConfigureFifo(XEmac * InstancePtr);
  77. static void StubFifoHandler(void *CallBackRef);
  78. static void StubErrorHandler(void *CallBackRef, XStatus ErrorCode);
  79. static void StubSgHandler(void *CallBackRef, XBufDescriptor * BdPtr,
  80. u32 NumBds);
  81. /************************** Variable Definitions *****************************/
  82. /*****************************************************************************/
  83. /**
  84. *
  85. * Initialize a specific XEmac instance/driver. The initialization entails:
  86. * - Initialize fields of the XEmac structure
  87. * - Clear the Ethernet statistics for this device
  88. * - Initialize the IPIF component with its register base address
  89. * - Configure the FIFO components with their register base addresses.
  90. * - If the device is configured with DMA, configure the DMA channel components
  91. * with their register base addresses. At some later time, memory pools for
  92. * the scatter-gather descriptor lists may be passed to the driver.
  93. * - Reset the Ethernet MAC
  94. *
  95. * @param InstancePtr is a pointer to the XEmac instance to be worked on.
  96. * @param DeviceId is the unique id of the device controlled by this XEmac
  97. * instance. Passing in a device id associates the generic XEmac
  98. * instance to a specific device, as chosen by the caller or application
  99. * developer.
  100. *
  101. * @return
  102. *
  103. * - XST_SUCCESS if initialization was successful
  104. * - XST_DEVICE_IS_STARTED if the device has already been started
  105. * - XST_DEVICE_NOT_FOUND if device configuration information was not found for
  106. * a device with the supplied device ID.
  107. *
  108. * @note
  109. *
  110. * None.
  111. *
  112. ******************************************************************************/
  113. XStatus
  114. XEmac_Initialize(XEmac * InstancePtr, u16 DeviceId)
  115. {
  116. XStatus Result;
  117. XEmac_Config *ConfigPtr; /* configuration information */
  118. XASSERT_NONVOID(InstancePtr != NULL);
  119. /*
  120. * If the device is started, disallow the initialize and return a status
  121. * indicating it is started. This allows the user to stop the device
  122. * and reinitialize, but prevents a user from inadvertently initializing
  123. */
  124. if (InstancePtr->IsStarted == XCOMPONENT_IS_STARTED) {
  125. return XST_DEVICE_IS_STARTED;
  126. }
  127. /*
  128. * Lookup the device configuration in the temporary CROM table. Use this
  129. * configuration info down below when initializing this component.
  130. */
  131. ConfigPtr = XEmac_LookupConfig(DeviceId);
  132. if (ConfigPtr == NULL) {
  133. return XST_DEVICE_NOT_FOUND;
  134. }
  135. /*
  136. * Set some default values
  137. */
  138. InstancePtr->IsReady = 0;
  139. InstancePtr->IsStarted = 0;
  140. InstancePtr->IpIfDmaConfig = ConfigPtr->IpIfDmaConfig;
  141. InstancePtr->HasMii = ConfigPtr->HasMii;
  142. InstancePtr->HasMulticastHash = FALSE;
  143. /* Always default polled to false, let user configure this mode */
  144. InstancePtr->IsPolled = FALSE;
  145. InstancePtr->FifoRecvHandler = StubFifoHandler;
  146. InstancePtr->FifoSendHandler = StubFifoHandler;
  147. InstancePtr->ErrorHandler = StubErrorHandler;
  148. InstancePtr->SgRecvHandler = StubSgHandler;
  149. InstancePtr->SgSendHandler = StubSgHandler;
  150. /*
  151. * Clear the statistics for this driver
  152. */
  153. XEmac_mClearStruct((u8 *) & InstancePtr->Stats, sizeof (XEmac_Stats));
  154. /*
  155. * Initialize the device register base addresses
  156. */
  157. InstancePtr->BaseAddress = ConfigPtr->BaseAddress;
  158. /*
  159. * Configure the send and receive FIFOs in the MAC
  160. */
  161. Result = ConfigureFifo(InstancePtr);
  162. if (Result != XST_SUCCESS) {
  163. return Result;
  164. }
  165. /*
  166. * If the device is configured for DMA, configure the send and receive DMA
  167. * channels in the MAC.
  168. */
  169. if (XEmac_mIsDma(InstancePtr)) {
  170. Result = ConfigureDma(InstancePtr);
  171. if (Result != XST_SUCCESS) {
  172. return Result;
  173. }
  174. }
  175. /*
  176. * Indicate the component is now ready to use. Note that this is done before
  177. * we reset the device and the PHY below, which may seem a bit odd. The
  178. * choice was made to move it here rather than remove the asserts in various
  179. * functions (e.g., Reset() and all functions that it calls). Applications
  180. * that use multiple threads, one to initialize the XEmac driver and one
  181. * waiting on the IsReady condition could have a problem with this sequence.
  182. */
  183. InstancePtr->IsReady = XCOMPONENT_IS_READY;
  184. /*
  185. * Reset the MAC to get it into its initial state. It is expected that
  186. * device configuration by the user will take place after this
  187. * initialization is done, but before the device is started.
  188. */
  189. XEmac_Reset(InstancePtr);
  190. return XST_SUCCESS;
  191. }
  192. /*****************************************************************************/
  193. /**
  194. *
  195. * Start the Ethernet controller as follows:
  196. * - If not in polled mode
  197. * - Set the internal interrupt enable registers appropriately
  198. * - Enable interrupts within the device itself. Note that connection of
  199. * the driver's interrupt handler to the interrupt source (typically
  200. * done using the interrupt controller component) is done by the higher
  201. * layer software.
  202. * - If the device is configured with scatter-gather DMA, start the DMA
  203. * channels if the descriptor lists are not empty
  204. * - Enable the transmitter
  205. * - Enable the receiver
  206. *
  207. * The PHY is enabled after driver initialization. We assume the upper layer
  208. * software has configured it and the EMAC appropriately before this function
  209. * is called.
  210. *
  211. * @param InstancePtr is a pointer to the XEmac instance to be worked on.
  212. *
  213. * @return
  214. *
  215. * - XST_SUCCESS if the device was started successfully
  216. * - XST_NO_CALLBACK if a callback function has not yet been registered using
  217. * the SetxxxHandler function. This is required if in interrupt mode.
  218. * - XST_DEVICE_IS_STARTED if the device is already started
  219. * - XST_DMA_SG_NO_LIST if configured for scatter-gather DMA and a descriptor
  220. * list has not yet been created for the send or receive channel.
  221. *
  222. * @note
  223. *
  224. * The driver tries to match the hardware configuration. So if the hardware
  225. * is configured with scatter-gather DMA, the driver expects to start the
  226. * scatter-gather channels and expects that the user has set up the buffer
  227. * descriptor lists already. If the user expects to use the driver in a mode
  228. * different than how the hardware is configured, the user should modify the
  229. * configuration table to reflect the mode to be used. Modifying the config
  230. * table is a workaround for now until we get some experience with how users
  231. * are intending to use the hardware in its different configurations. For
  232. * example, if the hardware is built with scatter-gather DMA but the user is
  233. * intending to use only simple DMA, the user either needs to modify the config
  234. * table as a workaround or rebuild the hardware with only simple DMA.
  235. *
  236. * This function makes use of internal resources that are shared between the
  237. * Start, Stop, and SetOptions functions. So if one task might be setting device
  238. * options while another is trying to start the device, the user is required to
  239. * provide protection of this shared data (typically using a semaphore).
  240. *
  241. ******************************************************************************/
  242. XStatus
  243. XEmac_Start(XEmac * InstancePtr)
  244. {
  245. u32 ControlReg;
  246. XStatus Result;
  247. XASSERT_NONVOID(InstancePtr != NULL);
  248. XASSERT_NONVOID(InstancePtr->IsReady == XCOMPONENT_IS_READY);
  249. /*
  250. * If it is already started, return a status indicating so
  251. */
  252. if (InstancePtr->IsStarted == XCOMPONENT_IS_STARTED) {
  253. return XST_DEVICE_IS_STARTED;
  254. }
  255. /*
  256. * If not polled, enable interrupts
  257. */
  258. if (!InstancePtr->IsPolled) {
  259. /*
  260. * Verify that the callbacks have been registered, then enable
  261. * interrupts
  262. */
  263. if (XEmac_mIsSgDma(InstancePtr)) {
  264. if ((InstancePtr->SgRecvHandler == StubSgHandler) ||
  265. (InstancePtr->SgSendHandler == StubSgHandler)) {
  266. return XST_NO_CALLBACK;
  267. }
  268. /* Enable IPIF interrupts */
  269. XIIF_V123B_WRITE_DIER(InstancePtr->BaseAddress,
  270. XEM_IPIF_DMA_DFT_MASK |
  271. XIIF_V123B_ERROR_MASK);
  272. XIIF_V123B_WRITE_IIER(InstancePtr->BaseAddress,
  273. XEM_EIR_DFT_SG_MASK);
  274. /* Enable scatter-gather DMA interrupts */
  275. XDmaChannel_SetIntrEnable(&InstancePtr->RecvChannel,
  276. XEM_DMA_SG_INTR_MASK);
  277. XDmaChannel_SetIntrEnable(&InstancePtr->SendChannel,
  278. XEM_DMA_SG_INTR_MASK);
  279. } else {
  280. if ((InstancePtr->FifoRecvHandler == StubFifoHandler) ||
  281. (InstancePtr->FifoSendHandler == StubFifoHandler)) {
  282. return XST_NO_CALLBACK;
  283. }
  284. /* Enable IPIF interrupts (used by simple DMA also) */
  285. XIIF_V123B_WRITE_DIER(InstancePtr->BaseAddress,
  286. XEM_IPIF_FIFO_DFT_MASK |
  287. XIIF_V123B_ERROR_MASK);
  288. XIIF_V123B_WRITE_IIER(InstancePtr->BaseAddress,
  289. XEM_EIR_DFT_FIFO_MASK);
  290. }
  291. /* Enable the global IPIF interrupt output */
  292. XIIF_V123B_GINTR_ENABLE(InstancePtr->BaseAddress);
  293. }
  294. /*
  295. * Indicate that the device is started before we enable the transmitter
  296. * or receiver. This needs to be done before because as soon as the
  297. * receiver is enabled we may get an interrupt, and there are functions
  298. * in the interrupt handling path that rely on the IsStarted flag.
  299. */
  300. InstancePtr->IsStarted = XCOMPONENT_IS_STARTED;
  301. /*
  302. * Enable the transmitter, and receiver (do a read/modify/write to preserve
  303. * current settings). There is no critical section here since this register
  304. * is not modified during interrupt context.
  305. */
  306. ControlReg = XIo_In32(InstancePtr->BaseAddress + XEM_ECR_OFFSET);
  307. ControlReg &= ~(XEM_ECR_XMIT_RESET_MASK | XEM_ECR_RECV_RESET_MASK);
  308. ControlReg |= (XEM_ECR_XMIT_ENABLE_MASK | XEM_ECR_RECV_ENABLE_MASK);
  309. XIo_Out32(InstancePtr->BaseAddress + XEM_ECR_OFFSET, ControlReg);
  310. /*
  311. * If configured with scatter-gather DMA and not polled, restart the
  312. * DMA channels in case there are buffers ready to be sent or received into.
  313. * The DMA SgStart function uses data that can be modified during interrupt
  314. * context, so a critical section is required here.
  315. */
  316. if ((XEmac_mIsSgDma(InstancePtr)) && (!InstancePtr->IsPolled)) {
  317. XIIF_V123B_GINTR_DISABLE(InstancePtr->BaseAddress);
  318. /*
  319. * The only error we care about is if the list has not yet been
  320. * created, or on receive, if no buffer descriptors have been
  321. * added yet (the list is empty). Other errors are benign at this point.
  322. */
  323. Result = XDmaChannel_SgStart(&InstancePtr->RecvChannel);
  324. if ((Result == XST_DMA_SG_NO_LIST)
  325. || (Result == XST_DMA_SG_LIST_EMPTY)) {
  326. XIIF_V123B_GINTR_ENABLE(InstancePtr->BaseAddress);
  327. return Result;
  328. }
  329. Result = XDmaChannel_SgStart(&InstancePtr->SendChannel);
  330. if (Result == XST_DMA_SG_NO_LIST) {
  331. XIIF_V123B_GINTR_ENABLE(InstancePtr->BaseAddress);
  332. return Result;
  333. }
  334. XIIF_V123B_GINTR_ENABLE(InstancePtr->BaseAddress);
  335. }
  336. return XST_SUCCESS;
  337. }
  338. /*****************************************************************************/
  339. /**
  340. *
  341. * Stop the Ethernet MAC as follows:
  342. * - If the device is configured with scatter-gather DMA, stop the DMA
  343. * channels (wait for acknowledgment of stop)
  344. * - Disable the transmitter and receiver
  345. * - Disable interrupts if not in polled mode (the higher layer software is
  346. * responsible for disabling interrupts at the interrupt controller)
  347. *
  348. * The PHY is left enabled after a Stop is called.
  349. *
  350. * If the device is configured for scatter-gather DMA, the DMA engine stops at
  351. * the next buffer descriptor in its list. The remaining descriptors in the list
  352. * are not removed, so anything in the list will be transmitted or received when
  353. * the device is restarted. The side effect of doing this is that the last
  354. * buffer descriptor processed by the DMA engine before stopping may not be the
  355. * last descriptor in the Ethernet frame. So when the device is restarted, a
  356. * partial frame (i.e., a bad frame) may be transmitted/received. This is only a
  357. * concern if a frame can span multiple buffer descriptors, which is dependent
  358. * on the size of the network buffers.
  359. *
  360. * @param InstancePtr is a pointer to the XEmac instance to be worked on.
  361. *
  362. * @return
  363. *
  364. * - XST_SUCCESS if the device was stopped successfully
  365. * - XST_DEVICE_IS_STOPPED if the device is already stopped
  366. *
  367. * @note
  368. *
  369. * This function makes use of internal resources that are shared between the
  370. * Start, Stop, and SetOptions functions. So if one task might be setting device
  371. * options while another is trying to start the device, the user is required to
  372. * provide protection of this shared data (typically using a semaphore).
  373. *
  374. ******************************************************************************/
  375. XStatus
  376. XEmac_Stop(XEmac * InstancePtr)
  377. {
  378. u32 ControlReg;
  379. XASSERT_NONVOID(InstancePtr != NULL);
  380. XASSERT_NONVOID(InstancePtr->IsReady == XCOMPONENT_IS_READY);
  381. /*
  382. * If the device is already stopped, do nothing but return a status
  383. * indicating so
  384. */
  385. if (InstancePtr->IsStarted != XCOMPONENT_IS_STARTED) {
  386. return XST_DEVICE_IS_STOPPED;
  387. }
  388. /*
  389. * If configured for scatter-gather DMA, stop the DMA channels. Ignore
  390. * the XST_DMA_SG_IS_STOPPED return code. There is a critical section
  391. * here between SgStart and SgStop, and SgStart can be called in interrupt
  392. * context, so disable interrupts while calling SgStop.
  393. */
  394. if (XEmac_mIsSgDma(InstancePtr)) {
  395. XBufDescriptor *BdTemp; /* temporary descriptor pointer */
  396. XIIF_V123B_GINTR_DISABLE(InstancePtr->BaseAddress);
  397. (void) XDmaChannel_SgStop(&InstancePtr->SendChannel, &BdTemp);
  398. (void) XDmaChannel_SgStop(&InstancePtr->RecvChannel, &BdTemp);
  399. XIIF_V123B_GINTR_ENABLE(InstancePtr->BaseAddress);
  400. }
  401. /*
  402. * Disable the transmitter and receiver. There is no critical section
  403. * here since this register is not modified during interrupt context.
  404. */
  405. ControlReg = XIo_In32(InstancePtr->BaseAddress + XEM_ECR_OFFSET);
  406. ControlReg &= ~(XEM_ECR_XMIT_ENABLE_MASK | XEM_ECR_RECV_ENABLE_MASK);
  407. XIo_Out32(InstancePtr->BaseAddress + XEM_ECR_OFFSET, ControlReg);
  408. /*
  409. * If not in polled mode, disable interrupts for IPIF (includes MAC and
  410. * DMAs)
  411. */
  412. if (!InstancePtr->IsPolled) {
  413. XIIF_V123B_GINTR_DISABLE(InstancePtr->BaseAddress);
  414. }
  415. InstancePtr->IsStarted = 0;
  416. return XST_SUCCESS;
  417. }
  418. /*****************************************************************************/
  419. /**
  420. *
  421. * Reset the Ethernet MAC. This is a graceful reset in that the device is stopped
  422. * first. Resets the DMA channels, the FIFOs, the transmitter, and the receiver.
  423. * The PHY is not reset. Any frames in the scatter-gather descriptor lists will
  424. * remain in the lists. The side effect of doing this is that after a reset and
  425. * following a restart of the device, frames that were in the list before the
  426. * reset may be transmitted or received. Reset must only be called after the
  427. * driver has been initialized.
  428. *
  429. * The driver is also taken out of polled mode if polled mode was set. The user
  430. * is responsbile for re-configuring the driver into polled mode after the
  431. * reset if desired.
  432. *
  433. * The configuration after this reset is as follows:
  434. * - Half duplex
  435. * - Disabled transmitter and receiver
  436. * - Enabled PHY (the PHY is not reset)
  437. * - MAC transmitter does pad insertion, FCS insertion, and source address
  438. * overwrite.
  439. * - MAC receiver does not strip padding or FCS
  440. * - Interframe Gap as recommended by IEEE Std. 802.3 (96 bit times)
  441. * - Unicast addressing enabled
  442. * - Broadcast addressing enabled
  443. * - Multicast addressing disabled (addresses are preserved)
  444. * - Promiscuous addressing disabled
  445. * - Default packet threshold and packet wait bound register values for
  446. * scatter-gather DMA operation
  447. * - MAC address of all zeros
  448. * - Non-polled mode
  449. *
  450. * The upper layer software is responsible for re-configuring (if necessary)
  451. * and restarting the MAC after the reset. Note that the PHY is not reset. PHY
  452. * control is left to the upper layer software. Note also that driver statistics
  453. * are not cleared on reset. It is up to the upper layer software to clear the
  454. * statistics if needed.
  455. *
  456. * When a reset is required due to an internal error, the driver notifies the
  457. * upper layer software of this need through the ErrorHandler callback and
  458. * specific status codes. The upper layer software is responsible for calling
  459. * this Reset function and then re-configuring the device.
  460. *
  461. * @param InstancePtr is a pointer to the XEmac instance to be worked on.
  462. *
  463. * @return
  464. *
  465. * None.
  466. *
  467. * @note
  468. *
  469. * None.
  470. *
  471. * @internal
  472. *
  473. * The reset is accomplished by setting the IPIF reset register. This takes
  474. * care of resetting all hardware blocks, including the MAC.
  475. *
  476. ******************************************************************************/
  477. void
  478. XEmac_Reset(XEmac * InstancePtr)
  479. {
  480. XASSERT_VOID(InstancePtr != NULL);
  481. XASSERT_VOID(InstancePtr->IsReady == XCOMPONENT_IS_READY);
  482. /*
  483. * Stop the device first
  484. */
  485. (void) XEmac_Stop(InstancePtr);
  486. /*
  487. * Take the driver out of polled mode
  488. */
  489. InstancePtr->IsPolled = FALSE;
  490. /*
  491. * Reset the entire IPIF at once. If we choose someday to reset each
  492. * hardware block separately, the reset should occur in the direction of
  493. * data flow. For example, for the send direction the reset order is DMA
  494. * first, then FIFO, then the MAC transmitter.
  495. */
  496. XIIF_V123B_RESET(InstancePtr->BaseAddress);
  497. if (XEmac_mIsSgDma(InstancePtr)) {
  498. /*
  499. * After reset, configure the scatter-gather DMA packet threshold and
  500. * packet wait bound registers to default values. Ignore the return
  501. * values of these functions since they only return error if the device
  502. * is not stopped.
  503. */
  504. (void) XEmac_SetPktThreshold(InstancePtr, XEM_SEND,
  505. XEM_SGDMA_DFT_THRESHOLD);
  506. (void) XEmac_SetPktThreshold(InstancePtr, XEM_RECV,
  507. XEM_SGDMA_DFT_THRESHOLD);
  508. (void) XEmac_SetPktWaitBound(InstancePtr, XEM_SEND,
  509. XEM_SGDMA_DFT_WAITBOUND);
  510. (void) XEmac_SetPktWaitBound(InstancePtr, XEM_RECV,
  511. XEM_SGDMA_DFT_WAITBOUND);
  512. }
  513. }
  514. /*****************************************************************************/
  515. /**
  516. *
  517. * Set the MAC address for this driver/device. The address is a 48-bit value.
  518. * The device must be stopped before calling this function.
  519. *
  520. * @param InstancePtr is a pointer to the XEmac instance to be worked on.
  521. * @param AddressPtr is a pointer to a 6-byte MAC address.
  522. *
  523. * @return
  524. *
  525. * - XST_SUCCESS if the MAC address was set successfully
  526. * - XST_DEVICE_IS_STARTED if the device has not yet been stopped
  527. *
  528. * @note
  529. *
  530. * None.
  531. *
  532. ******************************************************************************/
  533. XStatus
  534. XEmac_SetMacAddress(XEmac * InstancePtr, u8 * AddressPtr)
  535. {
  536. u32 MacAddr = 0;
  537. XASSERT_NONVOID(InstancePtr != NULL);
  538. XASSERT_NONVOID(AddressPtr != NULL);
  539. XASSERT_NONVOID(InstancePtr->IsReady == XCOMPONENT_IS_READY);
  540. /*
  541. * The device must be stopped before setting the MAC address
  542. */
  543. if (InstancePtr->IsStarted == XCOMPONENT_IS_STARTED) {
  544. return XST_DEVICE_IS_STARTED;
  545. }
  546. /*
  547. * Set the device station address high and low registers
  548. */
  549. MacAddr = (AddressPtr[0] << 8) | AddressPtr[1];
  550. XIo_Out32(InstancePtr->BaseAddress + XEM_SAH_OFFSET, MacAddr);
  551. MacAddr = (AddressPtr[2] << 24) | (AddressPtr[3] << 16) |
  552. (AddressPtr[4] << 8) | AddressPtr[5];
  553. XIo_Out32(InstancePtr->BaseAddress + XEM_SAL_OFFSET, MacAddr);
  554. return XST_SUCCESS;
  555. }
  556. /*****************************************************************************/
  557. /**
  558. *
  559. * Get the MAC address for this driver/device.
  560. *
  561. * @param InstancePtr is a pointer to the XEmac instance to be worked on.
  562. * @param BufferPtr is an output parameter, and is a pointer to a buffer into
  563. * which the current MAC address will be copied. The buffer must be at
  564. * least 6 bytes.
  565. *
  566. * @return
  567. *
  568. * None.
  569. *
  570. * @note
  571. *
  572. * None.
  573. *
  574. ******************************************************************************/
  575. void
  576. XEmac_GetMacAddress(XEmac * InstancePtr, u8 * BufferPtr)
  577. {
  578. u32 MacAddrHi;
  579. u32 MacAddrLo;
  580. XASSERT_VOID(InstancePtr != NULL);
  581. XASSERT_VOID(BufferPtr != NULL);
  582. XASSERT_VOID(InstancePtr->IsReady == XCOMPONENT_IS_READY);
  583. MacAddrHi = XIo_In32(InstancePtr->BaseAddress + XEM_SAH_OFFSET);
  584. MacAddrLo = XIo_In32(InstancePtr->BaseAddress + XEM_SAL_OFFSET);
  585. BufferPtr[0] = (u8) (MacAddrHi >> 8);
  586. BufferPtr[1] = (u8) MacAddrHi;
  587. BufferPtr[2] = (u8) (MacAddrLo >> 24);
  588. BufferPtr[3] = (u8) (MacAddrLo >> 16);
  589. BufferPtr[4] = (u8) (MacAddrLo >> 8);
  590. BufferPtr[5] = (u8) MacAddrLo;
  591. }
  592. /******************************************************************************/
  593. /**
  594. *
  595. * Configure DMA capabilities.
  596. *
  597. * @param InstancePtr is a pointer to the XEmac instance to be worked on.
  598. *
  599. * @return
  600. *
  601. * - XST_SUCCESS if successful initialization of DMA
  602. *
  603. * @note
  604. *
  605. * None.
  606. *
  607. ******************************************************************************/
  608. static XStatus
  609. ConfigureDma(XEmac * InstancePtr)
  610. {
  611. XStatus Result;
  612. /*
  613. * Initialize the DMA channels with their base addresses. We assume
  614. * scatter-gather DMA is the only possible configuration. Descriptor space
  615. * will need to be set later by the upper layer.
  616. */
  617. Result = XDmaChannel_Initialize(&InstancePtr->RecvChannel,
  618. InstancePtr->BaseAddress +
  619. XEM_DMA_RECV_OFFSET);
  620. if (Result != XST_SUCCESS) {
  621. return Result;
  622. }
  623. Result = XDmaChannel_Initialize(&InstancePtr->SendChannel,
  624. InstancePtr->BaseAddress +
  625. XEM_DMA_SEND_OFFSET);
  626. return Result;
  627. }
  628. /******************************************************************************/
  629. /**
  630. *
  631. * Configure the send and receive FIFO components with their base addresses
  632. * and interrupt masks. Currently the base addresses are defined constants.
  633. *
  634. * @param InstancePtr is a pointer to the XEmac instance to be worked on.
  635. *
  636. * @return
  637. *
  638. * XST_SUCCESS if successful initialization of the packet FIFOs
  639. *
  640. * @note
  641. *
  642. * None.
  643. *
  644. ******************************************************************************/
  645. static XStatus
  646. ConfigureFifo(XEmac * InstancePtr)
  647. {
  648. XStatus Result;
  649. /*
  650. * Return status from the packet FIFOs initialization is ignored since
  651. * they always return success.
  652. */
  653. Result = XPacketFifoV100b_Initialize(&InstancePtr->RecvFifo,
  654. InstancePtr->BaseAddress +
  655. XEM_PFIFO_RXREG_OFFSET,
  656. InstancePtr->BaseAddress +
  657. XEM_PFIFO_RXDATA_OFFSET);
  658. if (Result != XST_SUCCESS) {
  659. return Result;
  660. }
  661. Result = XPacketFifoV100b_Initialize(&InstancePtr->SendFifo,
  662. InstancePtr->BaseAddress +
  663. XEM_PFIFO_TXREG_OFFSET,
  664. InstancePtr->BaseAddress +
  665. XEM_PFIFO_TXDATA_OFFSET);
  666. return Result;
  667. }
  668. /******************************************************************************/
  669. /**
  670. *
  671. * This is a stub for the scatter-gather send and recv callbacks. The stub
  672. * is here in case the upper layers forget to set the handlers.
  673. *
  674. * @param CallBackRef is a pointer to the upper layer callback reference
  675. * @param BdPtr is a pointer to the first buffer descriptor in a list
  676. * @param NumBds is the number of descriptors in the list.
  677. *
  678. * @return
  679. *
  680. * None.
  681. *
  682. * @note
  683. *
  684. * None.
  685. *
  686. ******************************************************************************/
  687. static void
  688. StubSgHandler(void *CallBackRef, XBufDescriptor * BdPtr, u32 NumBds)
  689. {
  690. XASSERT_VOID_ALWAYS();
  691. }
  692. /******************************************************************************/
  693. /**
  694. *
  695. * This is a stub for the non-DMA send and recv callbacks. The stub is here in
  696. * case the upper layers forget to set the handlers.
  697. *
  698. * @param CallBackRef is a pointer to the upper layer callback reference
  699. *
  700. * @return
  701. *
  702. * None.
  703. *
  704. * @note
  705. *
  706. * None.
  707. *
  708. ******************************************************************************/
  709. static void
  710. StubFifoHandler(void *CallBackRef)
  711. {
  712. XASSERT_VOID_ALWAYS();
  713. }
  714. /******************************************************************************/
  715. /**
  716. *
  717. * This is a stub for the asynchronous error callback. The stub is here in
  718. * case the upper layers forget to set the handler.
  719. *
  720. * @param CallBackRef is a pointer to the upper layer callback reference
  721. * @param ErrorCode is the Xilinx error code, indicating the cause of the error
  722. *
  723. * @return
  724. *
  725. * None.
  726. *
  727. * @note
  728. *
  729. * None.
  730. *
  731. ******************************************************************************/
  732. static void
  733. StubErrorHandler(void *CallBackRef, XStatus ErrorCode)
  734. {
  735. XASSERT_VOID_ALWAYS();
  736. }
  737. /*****************************************************************************/
  738. /**
  739. *
  740. * Lookup the device configuration based on the unique device ID. The table
  741. * EmacConfigTable contains the configuration info for each device in the system.
  742. *
  743. * @param DeviceId is the unique device ID of the device being looked up.
  744. *
  745. * @return
  746. *
  747. * A pointer to the configuration table entry corresponding to the given
  748. * device ID, or NULL if no match is found.
  749. *
  750. * @note
  751. *
  752. * None.
  753. *
  754. ******************************************************************************/
  755. XEmac_Config *
  756. XEmac_LookupConfig(u16 DeviceId)
  757. {
  758. XEmac_Config *CfgPtr = NULL;
  759. int i;
  760. for (i = 0; i < XPAR_XEMAC_NUM_INSTANCES; i++) {
  761. if (XEmac_ConfigTable[i].DeviceId == DeviceId) {
  762. CfgPtr = &XEmac_ConfigTable[i];
  763. break;
  764. }
  765. }
  766. return CfgPtr;
  767. }