xdma_channel_sg.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317
  1. /* $Id: xdma_channel_sg.c,v 1.6 2003/02/03 19:50:33 moleres Exp $ */
  2. /******************************************************************************
  3. *
  4. * Author: Xilinx, Inc.
  5. *
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2 of the License, or (at your
  10. * option) any later version.
  11. *
  12. *
  13. * XILINX IS PROVIDING THIS DESIGN, CODE, OR INFORMATION "AS IS" AS A
  14. * COURTESY TO YOU. BY PROVIDING THIS DESIGN, CODE, OR INFORMATION AS
  15. * ONE POSSIBLE IMPLEMENTATION OF THIS FEATURE, APPLICATION OR STANDARD,
  16. * XILINX IS MAKING NO REPRESENTATION THAT THIS IMPLEMENTATION IS FREE
  17. * FROM ANY CLAIMS OF INFRINGEMENT, AND YOU ARE RESPONSIBLE FOR OBTAINING
  18. * ANY THIRD PARTY RIGHTS YOU MAY REQUIRE FOR YOUR IMPLEMENTATION.
  19. * XILINX EXPRESSLY DISCLAIMS ANY WARRANTY WHATSOEVER WITH RESPECT TO
  20. * THE ADEQUACY OF THE IMPLEMENTATION, INCLUDING BUT NOT LIMITED TO ANY
  21. * WARRANTIES OR REPRESENTATIONS THAT THIS IMPLEMENTATION IS FREE FROM
  22. * CLAIMS OF INFRINGEMENT, IMPLIED WARRANTIES OF MERCHANTABILITY AND
  23. * FITNESS FOR A PARTICULAR PURPOSE.
  24. *
  25. *
  26. * Xilinx hardware products are not intended for use in life support
  27. * appliances, devices, or systems. Use in such applications is
  28. * expressly prohibited.
  29. *
  30. *
  31. * (c) Copyright 2002-2004 Xilinx Inc.
  32. * All rights reserved.
  33. *
  34. *
  35. * You should have received a copy of the GNU General Public License along
  36. * with this program; if not, write to the Free Software Foundation, Inc.,
  37. * 675 Mass Ave, Cambridge, MA 02139, USA.
  38. *
  39. * FILENAME:
  40. *
  41. * xdma_channel_sg.c
  42. *
  43. * DESCRIPTION:
  44. *
  45. * This file contains the implementation of the XDmaChannel component which is
  46. * related to scatter gather operations.
  47. *
  48. * Scatter Gather Operations
  49. *
  50. * The DMA channel may support scatter gather operations. A scatter gather
  51. * operation automates the DMA channel such that multiple buffers can be
  52. * sent or received with minimal software interaction with the hardware. Buffer
  53. * descriptors, contained in the XBufDescriptor component, are used by the
  54. * scatter gather operations of the DMA channel to describe the buffers to be
  55. * processed.
  56. *
  57. * Scatter Gather List Operations
  58. *
  59. * A scatter gather list may be supported by each DMA channel. The scatter
  60. * gather list allows buffer descriptors to be put into the list by a device
  61. * driver which requires scatter gather. The hardware processes the buffer
  62. * descriptors which are contained in the list and modifies the buffer
  63. * descriptors to reflect the status of the DMA operations. The device driver
  64. * is notified by interrupt that specific DMA events occur including scatter
  65. * gather events. The device driver removes the completed buffer descriptors
  66. * from the scatter gather list to evaluate the status of each DMA operation.
  67. *
  68. * The scatter gather list is created and buffer descriptors are inserted into
  69. * the list. Buffer descriptors are never removed from the list after it's
  70. * creation such that a put operation copies from a temporary buffer descriptor
  71. * to a buffer descriptor in the list. Get operations don't copy from the list
  72. * to a temporary, but return a pointer to the buffer descriptor in the list.
  73. * A buffer descriptor in the list may be locked to prevent it from being
  74. * overwritten by a put operation. This allows the device driver to get a
  75. * descriptor from a scatter gather list and prevent it from being overwritten
  76. * until the buffer associated with the buffer descriptor has been processed.
  77. *
  78. * The get and put functions only operate on the list and are asynchronous from
  79. * the hardware which may be using the list of descriptors. This is important
  80. * because there are no checks in the get and put functions to ensure that the
  81. * hardware has processed the descriptors. This must be handled by the driver
  82. * using the DMA scatter gather channel through the use of the other functions.
  83. * When a scatter gather operation is started, the start function does ensure
  84. * that the descriptor to start has not already been processed by the hardware
  85. * and is not the first of a series of descriptors that have not been committed
  86. * yet.
  87. *
  88. * Descriptors are put into the list but not marked as ready to use by the
  89. * hardware until a commit operation is done. This allows multiple descriptors
  90. * which may contain a single packet of information for a protocol to be
  91. * guaranteed not to cause any underflow conditions during transmission. The
  92. * hardware design only allows descriptors to cause it to stop after a descriptor
  93. * has been processed rather than before it is processed. A series of
  94. * descriptors are put into the list followed by a commit operation, or each
  95. * descriptor may be commited. A commit operation is performed by changing a
  96. * single descriptor, the first of the series of puts, to indicate that the
  97. * hardware may now use all descriptors after it. The last descriptor in the
  98. * list is always set to cause the hardware to stop after it is processed.
  99. *
  100. * Typical Scatter Gather Processing
  101. *
  102. * The following steps illustrate the typical processing to use the
  103. * scatter gather features of a DMA channel.
  104. *
  105. * 1. Create a scatter gather list for the DMA channel which puts empty buffer
  106. * descriptors into the list.
  107. * 2. Create buffer descriptors which describe the buffers to be filled with
  108. * receive data or the buffers which contain data to be sent.
  109. * 3. Put buffer descriptors into the DMA channel scatter list such that scatter
  110. * gather operations are requested.
  111. * 4. Commit the buffer descriptors in the list such that they are ready to be
  112. * used by the DMA channel hardware.
  113. * 5. Start the scatter gather operations of the DMA channel.
  114. * 6. Process any interrupts which occur as a result of the scatter gather
  115. * operations or poll the DMA channel to determine the status. This may
  116. * be accomplished by getting the packet count for the channel and then
  117. * getting the appropriate number of descriptors from the list for that
  118. * number of packets.
  119. *
  120. * Minimizing Interrupts
  121. *
  122. * The Scatter Gather operating mode is designed to reduce the amount of CPU
  123. * throughput necessary to manage the hardware for devices. A key to the CPU
  124. * throughput is the number and rate of interrupts that the CPU must service.
  125. * Devices with higher data rates can cause larger numbers of interrupts and
  126. * higher frequency interrupts. Ideally the number of interrupts can be reduced
  127. * by only generating an interrupt when a specific amount of data has been
  128. * received from the interface. This design suffers from a lack of interrupts
  129. * when the amount of data received is less than the specified amount of data
  130. * to generate an interrupt. In order to help minimize the number of interrupts
  131. * which the CPU must service, an algorithm referred to as "interrupt coalescing"
  132. * is utilized.
  133. *
  134. * Interrupt Coalescing
  135. *
  136. * The principle of interrupt coalescing is to wait before generating an
  137. * interrupt until a certain number of packets have been received or sent. An
  138. * interrupt is also generated if a smaller number of packets have been received
  139. * followed by a certain period of time with no packet reception. This is a
  140. * trade-off of latency for bandwidth and is accomplished using several
  141. * mechanisms of the hardware including a counter for packets received or
  142. * transmitted and a packet timer. These two hardware mechanisms work in
  143. * combination to allow a reduction in the number of interrupts processed by the
  144. * CPU for packet reception.
  145. *
  146. * Unserviced Packet Count
  147. *
  148. * The purpose of the packet counter is to count the number of packets received
  149. * or transmitted and provide an interrupt when a specific number of packets
  150. * have been processed by the hardware. An interrupt is generated whenever the
  151. * counter is greater than or equal to the Packet Count Threshold. This counter
  152. * contains an accurate count of the number of packets that the hardware has
  153. * processed, either received or transmitted, and the software has not serviced.
  154. *
  155. * The packet counter allows the number of interrupts to be reduced by waiting
  156. * to generate an interrupt until enough packets are received. For packet
  157. * reception, packet counts of less than the number to generate an interrupt
  158. * would not be serviced without the addition of a packet timer. This counter is
  159. * continuously updated by the hardware, not latched to the value at the time
  160. * the interrupt occurred.
  161. *
  162. * The packet counter can be used within the interrupt service routine for the
  163. * device to reduce the number of interrupts. The interrupt service routine
  164. * loops while performing processing for each packet which has been received or
  165. * transmitted and decrements the counter by a specified value. At the same time,
  166. * the hardware is possibly continuing to receive or transmit more packets such
  167. * that the software may choose, based upon the value in the packet counter, to
  168. * remain in the interrupt service routine rather than exiting and immediately
  169. * returning. This feature should be used with caution as reducing the number of
  170. * interrupts is beneficial, but unbounded interrupt processing is not desirable.
  171. *
  172. * Since the hardware may be incrementing the packet counter simultaneously
  173. * with the software decrementing the counter, there is a need for atomic
  174. * operations. The hardware ensures that the operation is atomic such that
  175. * simultaneous accesses are properly handled.
  176. *
  177. * Packet Wait Bound
  178. *
  179. * The purpose of the packet wait bound is to augment the unserviced packet
  180. * count. Whenever there is no pending interrupt for the channel and the
  181. * unserviced packet count is non-zero, a timer starts counting timeout at the
  182. * value contained the the packet wait bound register. If the timeout is
  183. * reached, an interrupt is generated such that the software may service the
  184. * data which was buffered.
  185. *
  186. * NOTES:
  187. *
  188. * Special Test Conditions:
  189. *
  190. * The scatter gather list processing must be thoroughly tested if changes are
  191. * made. Testing should include putting and committing single descriptors and
  192. * putting multiple descriptors followed by a single commit. There are some
  193. * conditions in the code which handle the exception conditions.
  194. *
  195. * The Put Pointer points to the next location in the descriptor list to copy
  196. * in a new descriptor. The Get Pointer points to the next location in the
  197. * list to get a descriptor from. The Get Pointer only allows software to
  198. * have a traverse the list after the hardware has finished processing some
  199. * number of descriptors. The Commit Pointer points to the descriptor in the
  200. * list which is to be committed. It is also used to determine that no
  201. * descriptor is waiting to be commited (NULL). The Last Pointer points to
  202. * the last descriptor that was put into the list. It typically points
  203. * to the previous descriptor to the one pointed to by the Put Pointer.
  204. * Comparisons are done between these pointers to determine when the following
  205. * special conditions exist.
  206. * Single Put And Commit
  207. *
  208. * The buffer descriptor is ready to be used by the hardware so it is important
  209. * for the descriptor to not appear to be waiting to be committed. The commit
  210. * pointer is reset when a commit is done indicating there are no descriptors
  211. * waiting to be committed. In all cases but this one, the descriptor is
  212. * changed to cause the hardware to go to the next descriptor after processing
  213. * this one. But in this case, this is the last descriptor in the list such
  214. * that it must not be changed.
  215. *
  216. * 3 Or More Puts And Commit
  217. *
  218. * A series of 3 or more puts followed by a single commit is different in that
  219. * only the 1st descriptor put into the list is changed when the commit is done.
  220. * This requires each put starting on the 3rd to change the previous descriptor
  221. * so that it allows the hardware to continue to the next descriptor in the list.
  222. *
  223. * The 1st Put Following A Commit
  224. *
  225. * The commit caused the commit pointer to be NULL indicating that there are no
  226. * descriptors waiting to be committed. It is necessary for the next put to set
  227. * the commit pointer so that a commit must follow the put for the hardware to
  228. * use the descriptor.
  229. *
  230. * <pre>
  231. * MODIFICATION HISTORY:
  232. *
  233. * Ver Who Date Changes
  234. * ----- ---- -------- ------------------------------------------------------
  235. * 1.00a rpm 02/03/03 Removed the XST_DMA_SG_COUNT_EXCEEDED return code
  236. * from SetPktThreshold.
  237. * </pre>
  238. *
  239. ******************************************************************************/
  240. /***************************** Include Files *********************************/
  241. #include "xdma_channel.h"
  242. #include "xbasic_types.h"
  243. #include "xio.h"
  244. #include "xbuf_descriptor.h"
  245. #include "xstatus.h"
  246. /************************** Constant Definitions *****************************/
  247. #define XDC_SWCR_SG_ENABLE_MASK 0x80000000UL /* scatter gather enable */
  248. /**************************** Type Definitions *******************************/
  249. /***************** Macros (Inline Functions) Definitions *********************/
  250. /* the following macro copies selected fields of a buffer descriptor to another
  251. * buffer descriptor, this was provided by the buffer descriptor component but
  252. * was moved here since it is only used internally to this component and since
  253. * it does not copy all fields
  254. */
  255. #define CopyBufferDescriptor(InstancePtr, DestinationPtr) \
  256. { \
  257. *((u32 *)DestinationPtr + XBD_CONTROL_OFFSET) = \
  258. *((u32 *)InstancePtr + XBD_CONTROL_OFFSET); \
  259. *((u32 *)DestinationPtr + XBD_SOURCE_OFFSET) = \
  260. *((u32 *)InstancePtr + XBD_SOURCE_OFFSET); \
  261. *((u32 *)DestinationPtr + XBD_DESTINATION_OFFSET) = \
  262. *((u32 *)InstancePtr + XBD_DESTINATION_OFFSET); \
  263. *((u32 *)DestinationPtr + XBD_LENGTH_OFFSET) = \
  264. *((u32 *)InstancePtr + XBD_LENGTH_OFFSET); \
  265. *((u32 *)DestinationPtr + XBD_STATUS_OFFSET) = \
  266. *((u32 *)InstancePtr + XBD_STATUS_OFFSET); \
  267. *((u32 *)DestinationPtr + XBD_DEVICE_STATUS_OFFSET) = \
  268. *((u32 *)InstancePtr + XBD_DEVICE_STATUS_OFFSET); \
  269. *((u32 *)DestinationPtr + XBD_ID_OFFSET) = \
  270. *((u32 *)InstancePtr + XBD_ID_OFFSET); \
  271. *((u32 *)DestinationPtr + XBD_FLAGS_OFFSET) = \
  272. *((u32 *)InstancePtr + XBD_FLAGS_OFFSET); \
  273. *((u32 *)DestinationPtr + XBD_RQSTED_LENGTH_OFFSET) = \
  274. *((u32 *)InstancePtr + XBD_RQSTED_LENGTH_OFFSET); \
  275. }
  276. /************************** Variable Definitions *****************************/
  277. /************************** Function Prototypes ******************************/
  278. /******************************************************************************
  279. *
  280. * FUNCTION:
  281. *
  282. * XDmaChannel_SgStart
  283. *
  284. * DESCRIPTION:
  285. *
  286. * This function starts a scatter gather operation for a scatter gather
  287. * DMA channel. The first buffer descriptor in the buffer descriptor list
  288. * will be started with the scatter gather operation. A scatter gather list
  289. * should have previously been created for the DMA channel and buffer
  290. * descriptors put into the scatter gather list such that there are scatter
  291. * operations ready to be performed.
  292. *
  293. * ARGUMENTS:
  294. *
  295. * InstancePtr contains a pointer to the DMA channel to operate on. The DMA
  296. * channel should be configured to use scatter gather in order for this function
  297. * to be called.
  298. *
  299. * RETURN VALUE:
  300. *
  301. * A status containing XST_SUCCESS if scatter gather was started successfully
  302. * for the DMA channel.
  303. *
  304. * A value of XST_DMA_SG_NO_LIST indicates the scatter gather list has not
  305. * been created.
  306. *
  307. * A value of XST_DMA_SG_LIST_EMPTY indicates scatter gather was not started
  308. * because the scatter gather list of the DMA channel does not contain any
  309. * buffer descriptors that are ready to be processed by the hardware.
  310. *
  311. * A value of XST_DMA_SG_IS_STARTED indicates scatter gather was not started
  312. * because the scatter gather was not stopped, but was already started.
  313. *
  314. * A value of XST_DMA_SG_BD_NOT_COMMITTED indicates the buffer descriptor of
  315. * scatter gather list which was to be started is not committed to the list.
  316. * This status is more likely if this function is being called from an ISR
  317. * and non-ISR processing is putting descriptors into the list.
  318. *
  319. * A value of XST_DMA_SG_NO_DATA indicates that the buffer descriptor of the
  320. * scatter gather list which was to be started had already been used by the
  321. * hardware for a DMA transfer that has been completed.
  322. *
  323. * NOTES:
  324. *
  325. * It is the responsibility of the caller to get all the buffer descriptors
  326. * after performing a stop operation and before performing a start operation.
  327. * If buffer descriptors are not retrieved between stop and start operations,
  328. * buffer descriptors may be processed by the hardware more than once.
  329. *
  330. ******************************************************************************/
  331. XStatus
  332. XDmaChannel_SgStart(XDmaChannel * InstancePtr)
  333. {
  334. u32 Register;
  335. XBufDescriptor *LastDescriptorPtr;
  336. /* assert to verify input arguments */
  337. XASSERT_NONVOID(InstancePtr != NULL);
  338. XASSERT_NONVOID(InstancePtr->IsReady == XCOMPONENT_IS_READY);
  339. /* if a scatter gather list has not been created yet, return a status */
  340. if (InstancePtr->TotalDescriptorCount == 0) {
  341. return XST_DMA_SG_NO_LIST;
  342. }
  343. /* if the scatter gather list exists but is empty then return a status */
  344. if (XDmaChannel_IsSgListEmpty(InstancePtr)) {
  345. return XST_DMA_SG_LIST_EMPTY;
  346. }
  347. /* if scatter gather is busy for the DMA channel, return a status because
  348. * restarting it could lose data
  349. */
  350. Register = XIo_In32(InstancePtr->RegBaseAddress + XDC_DMAS_REG_OFFSET);
  351. if (Register & XDC_DMASR_SG_BUSY_MASK) {
  352. return XST_DMA_SG_IS_STARTED;
  353. }
  354. /* get the address of the last buffer descriptor which the DMA hardware
  355. * finished processing
  356. */
  357. LastDescriptorPtr =
  358. (XBufDescriptor *) XIo_In32(InstancePtr->RegBaseAddress +
  359. XDC_BDA_REG_OFFSET);
  360. /* setup the first buffer descriptor that will be sent when the scatter
  361. * gather channel is enabled, this is only necessary one time since
  362. * the BDA register of the channel maintains the last buffer descriptor
  363. * processed
  364. */
  365. if (LastDescriptorPtr == NULL) {
  366. XIo_Out32(InstancePtr->RegBaseAddress + XDC_BDA_REG_OFFSET,
  367. (u32) InstancePtr->GetPtr);
  368. } else {
  369. XBufDescriptor *NextDescriptorPtr;
  370. /* get the next descriptor to be started, if the status indicates it
  371. * hasn't already been used by the h/w, then it's OK to start it,
  372. * s/w sets the status of each descriptor to busy and then h/w clears
  373. * the busy when it is complete
  374. */
  375. NextDescriptorPtr =
  376. XBufDescriptor_GetNextPtr(LastDescriptorPtr);
  377. if ((XBufDescriptor_GetStatus(NextDescriptorPtr) &
  378. XDC_DMASR_BUSY_MASK) == 0) {
  379. return XST_DMA_SG_NO_DATA;
  380. }
  381. /* don't start the DMA SG channel if the descriptor to be processed
  382. * by h/w is to be committed by the s/w, this function can be called
  383. * such that it interrupts a thread that was putting into the list
  384. */
  385. if (NextDescriptorPtr == InstancePtr->CommitPtr) {
  386. return XST_DMA_SG_BD_NOT_COMMITTED;
  387. }
  388. }
  389. /* start the scatter gather operation by clearing the stop bit in the
  390. * control register and setting the enable bit in the s/w control register,
  391. * both of these are necessary to cause it to start, right now the order of
  392. * these statements is important, the software control register should be
  393. * set 1st. The other order can cause the CPU to have a loss of sync
  394. * because it cannot read/write the register while the DMA operation is
  395. * running
  396. */
  397. Register = XIo_In32(InstancePtr->RegBaseAddress + XDC_SWCR_REG_OFFSET);
  398. XIo_Out32(InstancePtr->RegBaseAddress + XDC_SWCR_REG_OFFSET,
  399. Register | XDC_SWCR_SG_ENABLE_MASK);
  400. Register = XIo_In32(InstancePtr->RegBaseAddress + XDC_DMAC_REG_OFFSET);
  401. XIo_Out32(InstancePtr->RegBaseAddress + XDC_DMAC_REG_OFFSET,
  402. Register & ~XDC_DMACR_SG_DISABLE_MASK);
  403. /* indicate the DMA channel scatter gather operation was started
  404. * successfully
  405. */
  406. return XST_SUCCESS;
  407. }
  408. /******************************************************************************
  409. *
  410. * FUNCTION:
  411. *
  412. * XDmaChannel_SgStop
  413. *
  414. * DESCRIPTION:
  415. *
  416. * This function stops a scatter gather operation for a scatter gather
  417. * DMA channel. This function starts the process of stopping a scatter
  418. * gather operation that is in progress and waits for the stop to be completed.
  419. * Since it waits for the operation to stopped before returning, this function
  420. * could take an amount of time relative to the size of the DMA scatter gather
  421. * operation which is in progress. The scatter gather list of the DMA channel
  422. * is not modified by this function such that starting the scatter gather
  423. * channel after stopping it will cause it to resume. This operation is
  424. * considered to be a graceful stop in that the scatter gather operation
  425. * completes the current buffer descriptor before stopping.
  426. *
  427. * If the interrupt is enabled, an interrupt will be generated when the
  428. * operation is stopped and the caller is responsible for handling the
  429. * interrupt.
  430. *
  431. * ARGUMENTS:
  432. *
  433. * InstancePtr contains a pointer to the DMA channel to operate on. The DMA
  434. * channel should be configured to use scatter gather in order for this function
  435. * to be called.
  436. *
  437. * BufDescriptorPtr is also a return value which contains a pointer to the
  438. * buffer descriptor which the scatter gather operation completed when it
  439. * was stopped.
  440. *
  441. * RETURN VALUE:
  442. *
  443. * A status containing XST_SUCCESS if scatter gather was stopped successfully
  444. * for the DMA channel.
  445. *
  446. * A value of XST_DMA_SG_IS_STOPPED indicates scatter gather was not stoppped
  447. * because the scatter gather is not started, but was already stopped.
  448. *
  449. * BufDescriptorPtr contains a pointer to the buffer descriptor which was
  450. * completed when the operation was stopped.
  451. *
  452. * NOTES:
  453. *
  454. * This function implements a loop which polls the hardware for an infinite
  455. * amount of time. If the hardware is not operating correctly, this function
  456. * may never return.
  457. *
  458. ******************************************************************************/
  459. XStatus
  460. XDmaChannel_SgStop(XDmaChannel * InstancePtr,
  461. XBufDescriptor ** BufDescriptorPtr)
  462. {
  463. u32 Register;
  464. /* assert to verify input arguments */
  465. XASSERT_NONVOID(InstancePtr != NULL);
  466. XASSERT_NONVOID(BufDescriptorPtr != NULL);
  467. XASSERT_NONVOID(InstancePtr->IsReady == XCOMPONENT_IS_READY);
  468. /* get the contents of the software control register, if scatter gather is not
  469. * enabled (started), then return a status because the disable acknowledge
  470. * would not be generated
  471. */
  472. Register = XIo_In32(InstancePtr->RegBaseAddress + XDC_SWCR_REG_OFFSET);
  473. if ((Register & XDC_SWCR_SG_ENABLE_MASK) == 0) {
  474. return XST_DMA_SG_IS_STOPPED;
  475. }
  476. /* Ensure the interrupt status for the scatter gather is cleared such
  477. * that this function will wait til the disable has occurred, writing
  478. * a 1 to only that bit in the register will clear only it
  479. */
  480. XIo_Out32(InstancePtr->RegBaseAddress + XDC_IS_REG_OFFSET,
  481. XDC_IXR_SG_DISABLE_ACK_MASK);
  482. /* disable scatter gather by writing to the software control register
  483. * without modifying any other bits of the register
  484. */
  485. XIo_Out32(InstancePtr->RegBaseAddress + XDC_SWCR_REG_OFFSET,
  486. Register & ~XDC_SWCR_SG_ENABLE_MASK);
  487. /* scatter gather does not disable immediately, but after the current
  488. * buffer descriptor is complete, so wait for the DMA channel to indicate
  489. * the disable is complete
  490. */
  491. do {
  492. Register =
  493. XIo_In32(InstancePtr->RegBaseAddress + XDC_IS_REG_OFFSET);
  494. } while ((Register & XDC_IXR_SG_DISABLE_ACK_MASK) == 0);
  495. /* Ensure the interrupt status for the scatter gather disable is cleared,
  496. * writing a 1 to only that bit in the register will clear only it
  497. */
  498. XIo_Out32(InstancePtr->RegBaseAddress + XDC_IS_REG_OFFSET,
  499. XDC_IXR_SG_DISABLE_ACK_MASK);
  500. /* set the specified buffer descriptor pointer to point to the buffer
  501. * descriptor that the scatter gather DMA channel was processing
  502. */
  503. *BufDescriptorPtr =
  504. (XBufDescriptor *) XIo_In32(InstancePtr->RegBaseAddress +
  505. XDC_BDA_REG_OFFSET);
  506. return XST_SUCCESS;
  507. }
  508. /******************************************************************************
  509. *
  510. * FUNCTION:
  511. *
  512. * XDmaChannel_CreateSgList
  513. *
  514. * DESCRIPTION:
  515. *
  516. * This function creates a scatter gather list in the DMA channel. A scatter
  517. * gather list consists of a list of buffer descriptors that are available to
  518. * be used for scatter gather operations. Buffer descriptors are put into the
  519. * list to request a scatter gather operation to be performed.
  520. *
  521. * A number of buffer descriptors are created from the specified memory and put
  522. * into a buffer descriptor list as empty buffer descriptors. This function must
  523. * be called before non-empty buffer descriptors may be put into the DMA channel
  524. * to request scatter gather operations.
  525. *
  526. * ARGUMENTS:
  527. *
  528. * InstancePtr contains a pointer to the DMA channel to operate on. The DMA
  529. * channel should be configured to use scatter gather in order for this function
  530. * to be called.
  531. *
  532. * MemoryPtr contains a pointer to the memory which is to be used for buffer
  533. * descriptors and must not be cached.
  534. *
  535. * ByteCount contains the number of bytes for the specified memory to be used
  536. * for buffer descriptors.
  537. *
  538. * RETURN VALUE:
  539. *
  540. * A status contains XST_SUCCESS if the scatter gather list was successfully
  541. * created.
  542. *
  543. * A value of XST_DMA_SG_LIST_EXISTS indicates that the scatter gather list
  544. * was not created because the list has already been created.
  545. *
  546. * NOTES:
  547. *
  548. * None.
  549. *
  550. ******************************************************************************/
  551. XStatus
  552. XDmaChannel_CreateSgList(XDmaChannel * InstancePtr,
  553. u32 * MemoryPtr, u32 ByteCount)
  554. {
  555. XBufDescriptor *BufferDescriptorPtr = (XBufDescriptor *) MemoryPtr;
  556. XBufDescriptor *PreviousDescriptorPtr = NULL;
  557. XBufDescriptor *StartOfListPtr = BufferDescriptorPtr;
  558. u32 UsedByteCount;
  559. /* assert to verify valid input arguments, alignment for those
  560. * arguments that have alignment restrictions, and at least enough
  561. * memory for one buffer descriptor
  562. */
  563. XASSERT_NONVOID(InstancePtr != NULL);
  564. XASSERT_NONVOID(MemoryPtr != NULL);
  565. XASSERT_NONVOID(((u32) MemoryPtr & 3) == 0);
  566. XASSERT_NONVOID(ByteCount != 0);
  567. XASSERT_NONVOID(ByteCount >= sizeof (XBufDescriptor));
  568. XASSERT_NONVOID(InstancePtr->IsReady == XCOMPONENT_IS_READY);
  569. /* if the scatter gather list has already been created, then return
  570. * with a status
  571. */
  572. if (InstancePtr->TotalDescriptorCount != 0) {
  573. return XST_DMA_SG_LIST_EXISTS;
  574. }
  575. /* loop thru the specified memory block and create as many buffer
  576. * descriptors as possible putting each into the list which is
  577. * implemented as a ring buffer, make sure not to use any memory which
  578. * is not large enough for a complete buffer descriptor
  579. */
  580. UsedByteCount = 0;
  581. while ((UsedByteCount + sizeof (XBufDescriptor)) <= ByteCount) {
  582. /* setup a pointer to the next buffer descriptor in the memory and
  583. * update # of used bytes to know when all of memory is used
  584. */
  585. BufferDescriptorPtr = (XBufDescriptor *) ((u32) MemoryPtr +
  586. UsedByteCount);
  587. /* initialize the new buffer descriptor such that it doesn't contain
  588. * garbage which could be used by the DMA hardware
  589. */
  590. XBufDescriptor_Initialize(BufferDescriptorPtr);
  591. /* if this is not the first buffer descriptor to be created,
  592. * then link it to the last created buffer descriptor
  593. */
  594. if (PreviousDescriptorPtr != NULL) {
  595. XBufDescriptor_SetNextPtr(PreviousDescriptorPtr,
  596. BufferDescriptorPtr);
  597. }
  598. /* always keep a pointer to the last created buffer descriptor such
  599. * that they can be linked together in the ring buffer
  600. */
  601. PreviousDescriptorPtr = BufferDescriptorPtr;
  602. /* keep a count of the number of descriptors in the list to allow
  603. * error processing to be performed
  604. */
  605. InstancePtr->TotalDescriptorCount++;
  606. UsedByteCount += sizeof (XBufDescriptor);
  607. }
  608. /* connect the last buffer descriptor created and inserted in the list
  609. * to the first such that a ring buffer is created
  610. */
  611. XBufDescriptor_SetNextPtr(BufferDescriptorPtr, StartOfListPtr);
  612. /* initialize the ring buffer to indicate that there are no
  613. * buffer descriptors in the list which point to valid data buffers
  614. */
  615. InstancePtr->PutPtr = BufferDescriptorPtr;
  616. InstancePtr->GetPtr = BufferDescriptorPtr;
  617. InstancePtr->CommitPtr = NULL;
  618. InstancePtr->LastPtr = BufferDescriptorPtr;
  619. InstancePtr->ActiveDescriptorCount = 0;
  620. /* indicate the scatter gather list was successfully created */
  621. return XST_SUCCESS;
  622. }
  623. /******************************************************************************
  624. *
  625. * FUNCTION:
  626. *
  627. * XDmaChannel_IsSgListEmpty
  628. *
  629. * DESCRIPTION:
  630. *
  631. * This function determines if the scatter gather list of a DMA channel is
  632. * empty with regard to buffer descriptors which are pointing to buffers to be
  633. * used for scatter gather operations.
  634. *
  635. * ARGUMENTS:
  636. *
  637. * InstancePtr contains a pointer to the DMA channel to operate on. The DMA
  638. * channel should be configured to use scatter gather in order for this function
  639. * to be called.
  640. *
  641. * RETURN VALUE:
  642. *
  643. * A value of TRUE if the scatter gather list is empty, otherwise a value of
  644. * FALSE.
  645. *
  646. * NOTES:
  647. *
  648. * None.
  649. *
  650. ******************************************************************************/
  651. u32
  652. XDmaChannel_IsSgListEmpty(XDmaChannel * InstancePtr)
  653. {
  654. /* assert to verify valid input arguments */
  655. XASSERT_NONVOID(InstancePtr != NULL);
  656. XASSERT_NONVOID(InstancePtr->IsReady == XCOMPONENT_IS_READY);
  657. /* if the number of descriptors which are being used in the list is zero
  658. * then the list is empty
  659. */
  660. return (InstancePtr->ActiveDescriptorCount == 0);
  661. }
  662. /******************************************************************************
  663. *
  664. * FUNCTION:
  665. *
  666. * XDmaChannel_PutDescriptor
  667. *
  668. * DESCRIPTION:
  669. *
  670. * This function puts a buffer descriptor into the DMA channel scatter
  671. * gather list. A DMA channel maintains a list of buffer descriptors which are
  672. * to be processed. This function puts the specified buffer descriptor
  673. * at the next location in the list. Note that since the list is already intact,
  674. * the information in the parameter is copied into the list (rather than modify
  675. * list pointers on the fly).
  676. *
  677. * After buffer descriptors are put into the list, they must also be committed
  678. * by calling another function. This allows multiple buffer descriptors which
  679. * span a single packet to be put into the list while preventing the hardware
  680. * from starting the first buffer descriptor of the packet.
  681. *
  682. * ARGUMENTS:
  683. *
  684. * InstancePtr contains a pointer to the DMA channel to operate on. The DMA
  685. * channel should be configured to use scatter gather in order for this function
  686. * to be called.
  687. *
  688. * BufferDescriptorPtr is a pointer to the buffer descriptor to be put into
  689. * the next available location of the scatter gather list.
  690. *
  691. * RETURN VALUE:
  692. *
  693. * A status which indicates XST_SUCCESS if the buffer descriptor was
  694. * successfully put into the scatter gather list.
  695. *
  696. * A value of XST_DMA_SG_NO_LIST indicates the scatter gather list has not
  697. * been created.
  698. *
  699. * A value of XST_DMA_SG_LIST_FULL indicates the buffer descriptor was not
  700. * put into the list because the list was full.
  701. *
  702. * A value of XST_DMA_SG_BD_LOCKED indicates the buffer descriptor was not
  703. * put into the list because the buffer descriptor in the list which is to
  704. * be overwritten was locked. A locked buffer descriptor indicates the higher
  705. * layered software is still using the buffer descriptor.
  706. *
  707. * NOTES:
  708. *
  709. * It is necessary to create a scatter gather list for a DMA channel before
  710. * putting buffer descriptors into it.
  711. *
  712. ******************************************************************************/
  713. XStatus
  714. XDmaChannel_PutDescriptor(XDmaChannel * InstancePtr,
  715. XBufDescriptor * BufferDescriptorPtr)
  716. {
  717. u32 Control;
  718. /* assert to verify valid input arguments and alignment for those
  719. * arguments that have alignment restrictions
  720. */
  721. XASSERT_NONVOID(InstancePtr != NULL);
  722. XASSERT_NONVOID(BufferDescriptorPtr != NULL);
  723. XASSERT_NONVOID(((u32) BufferDescriptorPtr & 3) == 0);
  724. XASSERT_NONVOID(InstancePtr->IsReady == XCOMPONENT_IS_READY);
  725. /* if a scatter gather list has not been created yet, return a status */
  726. if (InstancePtr->TotalDescriptorCount == 0) {
  727. return XST_DMA_SG_NO_LIST;
  728. }
  729. /* if the list is full because all descriptors are pointing to valid
  730. * buffers, then indicate an error, this code assumes no list or an
  731. * empty list is detected above
  732. */
  733. if (InstancePtr->ActiveDescriptorCount ==
  734. InstancePtr->TotalDescriptorCount) {
  735. return XST_DMA_SG_LIST_FULL;
  736. }
  737. /* if the buffer descriptor in the list which is to be overwritten is
  738. * locked, then don't overwrite it and return a status
  739. */
  740. if (XBufDescriptor_IsLocked(InstancePtr->PutPtr)) {
  741. return XST_DMA_SG_BD_LOCKED;
  742. }
  743. /* set the scatter gather stop bit in the control word of the descriptor
  744. * to cause the h/w to stop after it processes this descriptor since it
  745. * will be the last in the list
  746. */
  747. Control = XBufDescriptor_GetControl(BufferDescriptorPtr);
  748. XBufDescriptor_SetControl(BufferDescriptorPtr,
  749. Control | XDC_DMACR_SG_DISABLE_MASK);
  750. /* set both statuses in the descriptor so we tell if they are updated with
  751. * the status of the transfer, the hardware should change the busy in the
  752. * DMA status to be false when it completes
  753. */
  754. XBufDescriptor_SetStatus(BufferDescriptorPtr, XDC_DMASR_BUSY_MASK);
  755. XBufDescriptor_SetDeviceStatus(BufferDescriptorPtr, 0);
  756. /* copy the descriptor into the next position in the list so it's ready to
  757. * be used by the h/w, this assumes the descriptor in the list prior to this
  758. * one still has the stop bit in the control word set such that the h/w
  759. * use this one yet
  760. */
  761. CopyBufferDescriptor(BufferDescriptorPtr, InstancePtr->PutPtr);
  762. /* only the last in the list and the one to be committed have scatter gather
  763. * disabled in the control word, a commit requires only one descriptor
  764. * to be changed, when # of descriptors to commit > 2 all others except the
  765. * 1st and last have scatter gather enabled
  766. */
  767. if ((InstancePtr->CommitPtr != InstancePtr->LastPtr) &&
  768. (InstancePtr->CommitPtr != NULL)) {
  769. Control = XBufDescriptor_GetControl(InstancePtr->LastPtr);
  770. XBufDescriptor_SetControl(InstancePtr->LastPtr,
  771. Control & ~XDC_DMACR_SG_DISABLE_MASK);
  772. }
  773. /* update the list data based upon putting a descriptor into the list,
  774. * these operations must be last
  775. */
  776. InstancePtr->ActiveDescriptorCount++;
  777. /* only update the commit pointer if it is not already active, this allows
  778. * it to be deactivated after every commit such that a single descriptor
  779. * which is committed does not appear to be waiting to be committed
  780. */
  781. if (InstancePtr->CommitPtr == NULL) {
  782. InstancePtr->CommitPtr = InstancePtr->LastPtr;
  783. }
  784. /* these updates MUST BE LAST after the commit pointer update in order for
  785. * the commit pointer to track the correct descriptor to be committed
  786. */
  787. InstancePtr->LastPtr = InstancePtr->PutPtr;
  788. InstancePtr->PutPtr = XBufDescriptor_GetNextPtr(InstancePtr->PutPtr);
  789. return XST_SUCCESS;
  790. }
  791. /******************************************************************************
  792. *
  793. * FUNCTION:
  794. *
  795. * XDmaChannel_CommitPuts
  796. *
  797. * DESCRIPTION:
  798. *
  799. * This function commits the buffer descriptors which have been put into the
  800. * scatter list for the DMA channel since the last commit operation was
  801. * performed. This enables the calling functions to put several buffer
  802. * descriptors into the list (e.g.,a packet's worth) before allowing the scatter
  803. * gather operations to start. This prevents the DMA channel hardware from
  804. * starting to use the buffer descriptors in the list before they are ready
  805. * to be used (multiple buffer descriptors for a single packet).
  806. *
  807. * ARGUMENTS:
  808. *
  809. * InstancePtr contains a pointer to the DMA channel to operate on. The DMA
  810. * channel should be configured to use scatter gather in order for this function
  811. * to be called.
  812. *
  813. * RETURN VALUE:
  814. *
  815. * A status indicating XST_SUCCESS if the buffer descriptors of the list were
  816. * successfully committed.
  817. *
  818. * A value of XST_DMA_SG_NOTHING_TO_COMMIT indicates that the buffer descriptors
  819. * were not committed because there was nothing to commit in the list. All the
  820. * buffer descriptors which are in the list are commited.
  821. *
  822. * NOTES:
  823. *
  824. * None.
  825. *
  826. ******************************************************************************/
  827. XStatus
  828. XDmaChannel_CommitPuts(XDmaChannel * InstancePtr)
  829. {
  830. /* assert to verify input arguments */
  831. XASSERT_NONVOID(InstancePtr != NULL);
  832. XASSERT_NONVOID(InstancePtr->IsReady == XCOMPONENT_IS_READY);
  833. /* if the buffer descriptor to be committed is already committed or
  834. * the list is empty (none have been put in), then indicate an error
  835. */
  836. if ((InstancePtr->CommitPtr == NULL) ||
  837. XDmaChannel_IsSgListEmpty(InstancePtr)) {
  838. return XST_DMA_SG_NOTHING_TO_COMMIT;
  839. }
  840. /* last descriptor in the list must have scatter gather disabled so the end
  841. * of the list is hit by h/w, if descriptor to commit is not last in list,
  842. * commit descriptors by enabling scatter gather in the descriptor
  843. */
  844. if (InstancePtr->CommitPtr != InstancePtr->LastPtr) {
  845. u32 Control;
  846. Control = XBufDescriptor_GetControl(InstancePtr->CommitPtr);
  847. XBufDescriptor_SetControl(InstancePtr->CommitPtr, Control &
  848. ~XDC_DMACR_SG_DISABLE_MASK);
  849. }
  850. /* Update the commit pointer to indicate that there is nothing to be
  851. * committed, this state is used by start processing to know that the
  852. * buffer descriptor to start is not waiting to be committed
  853. */
  854. InstancePtr->CommitPtr = NULL;
  855. return XST_SUCCESS;
  856. }
  857. /******************************************************************************
  858. *
  859. * FUNCTION:
  860. *
  861. * XDmaChannel_GetDescriptor
  862. *
  863. * DESCRIPTION:
  864. *
  865. * This function gets a buffer descriptor from the scatter gather list of the
  866. * DMA channel. The buffer descriptor is retrieved from the scatter gather list
  867. * and the scatter gather list is updated to not include the retrieved buffer
  868. * descriptor. This is typically done after a scatter gather operation
  869. * completes indicating that a data buffer has been successfully sent or data
  870. * has been received into the data buffer. The purpose of this function is to
  871. * allow the device using the scatter gather operation to get the results of the
  872. * operation.
  873. *
  874. * ARGUMENTS:
  875. *
  876. * InstancePtr contains a pointer to the DMA channel to operate on. The DMA
  877. * channel should be configured to use scatter gather in order for this function
  878. * to be called.
  879. *
  880. * BufDescriptorPtr is a pointer to a pointer to the buffer descriptor which
  881. * was retrieved from the list. The buffer descriptor is not really removed
  882. * from the list, but it is changed to a state such that the hardware will not
  883. * use it again until it is put into the scatter gather list of the DMA channel.
  884. *
  885. * RETURN VALUE:
  886. *
  887. * A status indicating XST_SUCCESS if a buffer descriptor was retrieved from
  888. * the scatter gather list of the DMA channel.
  889. *
  890. * A value of XST_DMA_SG_NO_LIST indicates the scatter gather list has not
  891. * been created.
  892. *
  893. * A value of XST_DMA_SG_LIST_EMPTY indicates no buffer descriptor was
  894. * retrieved from the list because there are no buffer descriptors to be
  895. * processed in the list.
  896. *
  897. * BufDescriptorPtr is updated to point to the buffer descriptor which was
  898. * retrieved from the list if the status indicates success.
  899. *
  900. * NOTES:
  901. *
  902. * None.
  903. *
  904. ******************************************************************************/
  905. XStatus
  906. XDmaChannel_GetDescriptor(XDmaChannel * InstancePtr,
  907. XBufDescriptor ** BufDescriptorPtr)
  908. {
  909. u32 Control;
  910. /* assert to verify input arguments */
  911. XASSERT_NONVOID(InstancePtr != NULL);
  912. XASSERT_NONVOID(BufDescriptorPtr != NULL);
  913. XASSERT_NONVOID(InstancePtr->IsReady == XCOMPONENT_IS_READY);
  914. /* if a scatter gather list has not been created yet, return a status */
  915. if (InstancePtr->TotalDescriptorCount == 0) {
  916. return XST_DMA_SG_NO_LIST;
  917. }
  918. /* if the buffer descriptor list is empty, then indicate an error */
  919. if (XDmaChannel_IsSgListEmpty(InstancePtr)) {
  920. return XST_DMA_SG_LIST_EMPTY;
  921. }
  922. /* retrieve the next buffer descriptor which is ready to be processed from
  923. * the buffer descriptor list for the DMA channel, set the control word
  924. * such that hardware will stop after the descriptor has been processed
  925. */
  926. Control = XBufDescriptor_GetControl(InstancePtr->GetPtr);
  927. XBufDescriptor_SetControl(InstancePtr->GetPtr,
  928. Control | XDC_DMACR_SG_DISABLE_MASK);
  929. /* set the input argument, which is also an output, to point to the
  930. * buffer descriptor which is to be retrieved from the list
  931. */
  932. *BufDescriptorPtr = InstancePtr->GetPtr;
  933. /* update the pointer of the DMA channel to reflect the buffer descriptor
  934. * was retrieved from the list by setting it to the next buffer descriptor
  935. * in the list and indicate one less descriptor in the list now
  936. */
  937. InstancePtr->GetPtr = XBufDescriptor_GetNextPtr(InstancePtr->GetPtr);
  938. InstancePtr->ActiveDescriptorCount--;
  939. return XST_SUCCESS;
  940. }
  941. /*********************** Interrupt Collescing Functions **********************/
  942. /******************************************************************************
  943. *
  944. * FUNCTION:
  945. *
  946. * XDmaChannel_GetPktCount
  947. *
  948. * DESCRIPTION:
  949. *
  950. * This function returns the value of the unserviced packet count register of
  951. * the DMA channel. This count represents the number of packets that have been
  952. * sent or received by the hardware, but not processed by software.
  953. *
  954. * ARGUMENTS:
  955. *
  956. * InstancePtr contains a pointer to the DMA channel to operate on. The DMA
  957. * channel should be configured to use scatter gather in order for this function
  958. * to be called.
  959. *
  960. * RETURN VALUE:
  961. *
  962. * The unserviced packet counter register contents for the DMA channel.
  963. *
  964. * NOTES:
  965. *
  966. * None.
  967. *
  968. ******************************************************************************/
  969. u32
  970. XDmaChannel_GetPktCount(XDmaChannel * InstancePtr)
  971. {
  972. /* assert to verify input arguments */
  973. XASSERT_NONVOID(InstancePtr != NULL);
  974. XASSERT_NONVOID(InstancePtr->IsReady == XCOMPONENT_IS_READY);
  975. /* get the unserviced packet count from the register and return it */
  976. return XIo_In32(InstancePtr->RegBaseAddress + XDC_UPC_REG_OFFSET);
  977. }
  978. /******************************************************************************
  979. *
  980. * FUNCTION:
  981. *
  982. * XDmaChannel_DecrementPktCount
  983. *
  984. * DESCRIPTION:
  985. *
  986. * This function decrements the value of the unserviced packet count register.
  987. * This informs the hardware that the software has processed a packet. The
  988. * unserviced packet count register may only be decremented by one in the
  989. * hardware.
  990. *
  991. * ARGUMENTS:
  992. *
  993. * InstancePtr contains a pointer to the DMA channel to operate on. The DMA
  994. * channel should be configured to use scatter gather in order for this function
  995. * to be called.
  996. *
  997. * RETURN VALUE:
  998. *
  999. * None.
  1000. *
  1001. * NOTES:
  1002. *
  1003. * None.
  1004. *
  1005. ******************************************************************************/
  1006. void
  1007. XDmaChannel_DecrementPktCount(XDmaChannel * InstancePtr)
  1008. {
  1009. u32 Register;
  1010. /* assert to verify input arguments */
  1011. XASSERT_VOID(InstancePtr != NULL);
  1012. XASSERT_VOID(InstancePtr->IsReady == XCOMPONENT_IS_READY);
  1013. /* if the unserviced packet count register can be decremented (rather
  1014. * than rolling over) decrement it by writing a 1 to the register,
  1015. * this is the only valid write to the register as it serves as an
  1016. * acknowledge that a packet was handled by the software
  1017. */
  1018. Register = XIo_In32(InstancePtr->RegBaseAddress + XDC_UPC_REG_OFFSET);
  1019. if (Register > 0) {
  1020. XIo_Out32(InstancePtr->RegBaseAddress + XDC_UPC_REG_OFFSET,
  1021. 1UL);
  1022. }
  1023. }
  1024. /******************************************************************************
  1025. *
  1026. * FUNCTION:
  1027. *
  1028. * XDmaChannel_SetPktThreshold
  1029. *
  1030. * DESCRIPTION:
  1031. *
  1032. * This function sets the value of the packet count threshold register of the
  1033. * DMA channel. It reflects the number of packets that must be sent or
  1034. * received before generating an interrupt. This value helps implement
  1035. * a concept called "interrupt coalescing", which is used to reduce the number
  1036. * of interrupts from devices with high data rates.
  1037. *
  1038. * ARGUMENTS:
  1039. *
  1040. * InstancePtr contains a pointer to the DMA channel to operate on. The DMA
  1041. * channel should be configured to use scatter gather in order for this function
  1042. * to be called.
  1043. *
  1044. * Threshold is the value that is written to the threshold register of the
  1045. * DMA channel.
  1046. *
  1047. * RETURN VALUE:
  1048. *
  1049. * A status containing XST_SUCCESS if the packet count threshold was
  1050. * successfully set.
  1051. *
  1052. * NOTES:
  1053. *
  1054. * The packet threshold could be set to larger than the number of descriptors
  1055. * allocated to the DMA channel. In this case, the wait bound will take over
  1056. * and always indicate data arrival. There was a check in this function that
  1057. * returned an error if the treshold was larger than the number of descriptors,
  1058. * but that was removed because users would then have to set the threshold
  1059. * only after they set descriptor space, which is an order dependency that
  1060. * caused confustion.
  1061. *
  1062. ******************************************************************************/
  1063. XStatus
  1064. XDmaChannel_SetPktThreshold(XDmaChannel * InstancePtr, u8 Threshold)
  1065. {
  1066. /* assert to verify input arguments, don't assert the threshold since
  1067. * it's range is unknown
  1068. */
  1069. XASSERT_NONVOID(InstancePtr != NULL);
  1070. XASSERT_NONVOID(InstancePtr->IsReady == XCOMPONENT_IS_READY);
  1071. /* set the packet count threshold in the register such that an interrupt
  1072. * may be generated, if enabled, when the packet count threshold is
  1073. * reached or exceeded
  1074. */
  1075. XIo_Out32(InstancePtr->RegBaseAddress + XDC_PCT_REG_OFFSET,
  1076. (u32) Threshold);
  1077. /* indicate the packet count threshold was successfully set */
  1078. return XST_SUCCESS;
  1079. }
  1080. /******************************************************************************
  1081. *
  1082. * FUNCTION:
  1083. *
  1084. * XDmaChannel_GetPktThreshold
  1085. *
  1086. * DESCRIPTION:
  1087. *
  1088. * This function gets the value of the packet count threshold register of the
  1089. * DMA channel. This value reflects the number of packets that must be sent or
  1090. * received before generating an interrupt. This value helps implement a concept
  1091. * called "interrupt coalescing", which is used to reduce the number of
  1092. * interrupts from devices with high data rates.
  1093. *
  1094. * ARGUMENTS:
  1095. *
  1096. * InstancePtr contains a pointer to the DMA channel to operate on. The DMA
  1097. * channel should be configured to use scatter gather in order for this function
  1098. * to be called.
  1099. *
  1100. * RETURN VALUE:
  1101. *
  1102. * The packet threshold register contents for the DMA channel and is a value in
  1103. * the range 0 - 1023. A value of 0 indicates the packet wait bound timer is
  1104. * disabled.
  1105. *
  1106. * NOTES:
  1107. *
  1108. * None.
  1109. *
  1110. ******************************************************************************/
  1111. u8
  1112. XDmaChannel_GetPktThreshold(XDmaChannel * InstancePtr)
  1113. {
  1114. /* assert to verify input arguments */
  1115. XASSERT_NONVOID(InstancePtr != NULL);
  1116. XASSERT_NONVOID(InstancePtr->IsReady == XCOMPONENT_IS_READY);
  1117. /* get the packet count threshold from the register and return it,
  1118. * since only 8 bits are used, cast it to return only those bits */
  1119. return (u8) XIo_In32(InstancePtr->RegBaseAddress + XDC_PCT_REG_OFFSET);
  1120. }
  1121. /******************************************************************************
  1122. *
  1123. * FUNCTION:
  1124. *
  1125. * XDmaChannel_SetPktWaitBound
  1126. *
  1127. * DESCRIPTION:
  1128. *
  1129. * This function sets the value of the packet wait bound register of the
  1130. * DMA channel. This value reflects the timer value used to trigger an
  1131. * interrupt when not enough packets have been received to reach the packet
  1132. * count threshold.
  1133. *
  1134. * The timer is in millisecond units with +/- 33% accuracy.
  1135. *
  1136. * ARGUMENTS:
  1137. *
  1138. * InstancePtr contains a pointer to the DMA channel to operate on. The DMA
  1139. * channel should be configured to use scatter gather in order for this function
  1140. * to be called.
  1141. *
  1142. * WaitBound is the value, in milliseconds, to be stored in the wait bound
  1143. * register of the DMA channel and is a value in the range 0 - 1023. A value
  1144. * of 0 disables the packet wait bound timer.
  1145. *
  1146. * RETURN VALUE:
  1147. *
  1148. * None.
  1149. *
  1150. * NOTES:
  1151. *
  1152. * None.
  1153. *
  1154. ******************************************************************************/
  1155. void
  1156. XDmaChannel_SetPktWaitBound(XDmaChannel * InstancePtr, u32 WaitBound)
  1157. {
  1158. /* assert to verify input arguments */
  1159. XASSERT_VOID(InstancePtr != NULL);
  1160. XASSERT_VOID(WaitBound < 1024);
  1161. XASSERT_VOID(InstancePtr->IsReady == XCOMPONENT_IS_READY);
  1162. /* set the packet wait bound in the register such that interrupt may be
  1163. * generated, if enabled, when packets have not been handled for a specific
  1164. * amount of time
  1165. */
  1166. XIo_Out32(InstancePtr->RegBaseAddress + XDC_PWB_REG_OFFSET, WaitBound);
  1167. }
  1168. /******************************************************************************
  1169. *
  1170. * FUNCTION:
  1171. *
  1172. * XDmaChannel_GetPktWaitBound
  1173. *
  1174. * DESCRIPTION:
  1175. *
  1176. * This function gets the value of the packet wait bound register of the
  1177. * DMA channel. This value contains the timer value used to trigger an
  1178. * interrupt when not enough packets have been received to reach the packet
  1179. * count threshold.
  1180. *
  1181. * The timer is in millisecond units with +/- 33% accuracy.
  1182. *
  1183. * ARGUMENTS:
  1184. *
  1185. * InstancePtr contains a pointer to the DMA channel to operate on. The DMA
  1186. * channel should be configured to use scatter gather in order for this function
  1187. * to be called.
  1188. *
  1189. * RETURN VALUE:
  1190. *
  1191. * The packet wait bound register contents for the DMA channel.
  1192. *
  1193. * NOTES:
  1194. *
  1195. * None.
  1196. *
  1197. ******************************************************************************/
  1198. u32
  1199. XDmaChannel_GetPktWaitBound(XDmaChannel * InstancePtr)
  1200. {
  1201. /* assert to verify input arguments */
  1202. XASSERT_NONVOID(InstancePtr != NULL);
  1203. XASSERT_NONVOID(InstancePtr->IsReady == XCOMPONENT_IS_READY);
  1204. /* get the packet wait bound from the register and return it */
  1205. return XIo_In32(InstancePtr->RegBaseAddress + XDC_PWB_REG_OFFSET);
  1206. }