dma.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  1. /*****************************************************************************
  2. * Copyright 2004 - 2008 Broadcom Corporation. All rights reserved.
  3. *
  4. * Unless you and Broadcom execute a separate written software license
  5. * agreement governing use of this software, this software is licensed to you
  6. * under the terms of the GNU General Public License version 2, available at
  7. * http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  8. *
  9. * Notwithstanding the above, under no circumstances may you combine this
  10. * software in any way with any other Broadcom software provided under a
  11. * license other than the GPL, without Broadcom's express prior written
  12. * consent.
  13. *****************************************************************************/
  14. /****************************************************************************/
  15. /**
  16. * @file dma.h
  17. *
  18. * @brief API definitions for the linux DMA interface.
  19. */
  20. /****************************************************************************/
  21. #if !defined(ASM_ARM_ARCH_BCMRING_DMA_H)
  22. #define ASM_ARM_ARCH_BCMRING_DMA_H
  23. /* ---- Include Files ---------------------------------------------------- */
  24. #include <linux/kernel.h>
  25. #include <linux/wait.h>
  26. #include <linux/semaphore.h>
  27. #include <csp/dmacHw.h>
  28. #include <mach/timer.h>
  29. #include <linux/scatterlist.h>
  30. #include <linux/dma-mapping.h>
  31. #include <linux/mm.h>
  32. #include <linux/vmalloc.h>
  33. #include <linux/pagemap.h>
  34. /* ---- Constants and Types ---------------------------------------------- */
  35. /* If DMA_DEBUG_TRACK_RESERVATION is set to a non-zero value, then the filename */
  36. /* and line number of the reservation request will be recorded in the channel table */
  37. #define DMA_DEBUG_TRACK_RESERVATION 1
  38. #define DMA_NUM_CONTROLLERS 2
  39. #define DMA_NUM_CHANNELS 8 /* per controller */
  40. typedef enum {
  41. DMA_DEVICE_MEM_TO_MEM, /* For memory to memory transfers */
  42. DMA_DEVICE_I2S0_DEV_TO_MEM,
  43. DMA_DEVICE_I2S0_MEM_TO_DEV,
  44. DMA_DEVICE_I2S1_DEV_TO_MEM,
  45. DMA_DEVICE_I2S1_MEM_TO_DEV,
  46. DMA_DEVICE_APM_CODEC_A_DEV_TO_MEM,
  47. DMA_DEVICE_APM_CODEC_A_MEM_TO_DEV,
  48. DMA_DEVICE_APM_CODEC_B_DEV_TO_MEM,
  49. DMA_DEVICE_APM_CODEC_B_MEM_TO_DEV,
  50. DMA_DEVICE_APM_CODEC_C_DEV_TO_MEM, /* Additional mic input for beam-forming */
  51. DMA_DEVICE_APM_PCM0_DEV_TO_MEM,
  52. DMA_DEVICE_APM_PCM0_MEM_TO_DEV,
  53. DMA_DEVICE_APM_PCM1_DEV_TO_MEM,
  54. DMA_DEVICE_APM_PCM1_MEM_TO_DEV,
  55. DMA_DEVICE_SPUM_DEV_TO_MEM,
  56. DMA_DEVICE_SPUM_MEM_TO_DEV,
  57. DMA_DEVICE_SPIH_DEV_TO_MEM,
  58. DMA_DEVICE_SPIH_MEM_TO_DEV,
  59. DMA_DEVICE_UART_A_DEV_TO_MEM,
  60. DMA_DEVICE_UART_A_MEM_TO_DEV,
  61. DMA_DEVICE_UART_B_DEV_TO_MEM,
  62. DMA_DEVICE_UART_B_MEM_TO_DEV,
  63. DMA_DEVICE_PIF_MEM_TO_DEV,
  64. DMA_DEVICE_PIF_DEV_TO_MEM,
  65. DMA_DEVICE_ESW_DEV_TO_MEM,
  66. DMA_DEVICE_ESW_MEM_TO_DEV,
  67. DMA_DEVICE_VPM_MEM_TO_MEM,
  68. DMA_DEVICE_CLCD_MEM_TO_MEM,
  69. DMA_DEVICE_NAND_MEM_TO_MEM,
  70. DMA_DEVICE_MEM_TO_VRAM,
  71. DMA_DEVICE_VRAM_TO_MEM,
  72. /* Add new entries before this line. */
  73. DMA_NUM_DEVICE_ENTRIES,
  74. DMA_DEVICE_NONE = 0xff, /* Special value to indicate that no device is currently assigned. */
  75. } DMA_Device_t;
  76. /****************************************************************************
  77. *
  78. * The DMA_Handle_t is the primary object used by callers of the API.
  79. *
  80. *****************************************************************************/
  81. #define DMA_INVALID_HANDLE ((DMA_Handle_t) -1)
  82. typedef int DMA_Handle_t;
  83. /****************************************************************************
  84. *
  85. * The DMA_DescriptorRing_t contains a ring of descriptors which is used
  86. * to point to regions of memory.
  87. *
  88. *****************************************************************************/
  89. typedef struct {
  90. void *virtAddr; /* Virtual Address of the descriptor ring */
  91. dma_addr_t physAddr; /* Physical address of the descriptor ring */
  92. int descriptorsAllocated; /* Number of descriptors allocated in the descriptor ring */
  93. size_t bytesAllocated; /* Number of bytes allocated in the descriptor ring */
  94. } DMA_DescriptorRing_t;
  95. /****************************************************************************
  96. *
  97. * The DMA_MemType_t and DMA_MemMap_t are helper structures used to setup
  98. * DMA chains from a variety of memory sources.
  99. *
  100. *****************************************************************************/
  101. #define DMA_MEM_MAP_MIN_SIZE 4096 /* Pages less than this size are better */
  102. /* off not being DMA'd. */
  103. typedef enum {
  104. DMA_MEM_TYPE_NONE, /* Not a valid setting */
  105. DMA_MEM_TYPE_VMALLOC, /* Memory came from vmalloc call */
  106. DMA_MEM_TYPE_KMALLOC, /* Memory came from kmalloc call */
  107. DMA_MEM_TYPE_DMA, /* Memory came from dma_alloc_xxx call */
  108. DMA_MEM_TYPE_USER, /* Memory came from user space. */
  109. } DMA_MemType_t;
  110. /* A segment represents a physically and virtually contiguous chunk of memory. */
  111. /* i.e. each segment can be DMA'd */
  112. /* A user of the DMA code will add memory regions. Each region may need to be */
  113. /* represented by one or more segments. */
  114. typedef struct {
  115. void *virtAddr; /* Virtual address used for this segment */
  116. dma_addr_t physAddr; /* Physical address this segment maps to */
  117. size_t numBytes; /* Size of the segment, in bytes */
  118. } DMA_Segment_t;
  119. /* A region represents a virtually contiguous chunk of memory, which may be */
  120. /* made up of multiple segments. */
  121. typedef struct {
  122. DMA_MemType_t memType;
  123. void *virtAddr;
  124. size_t numBytes;
  125. /* Each region (virtually contiguous) consists of one or more segments. Each */
  126. /* segment is virtually and physically contiguous. */
  127. int numSegmentsUsed;
  128. int numSegmentsAllocated;
  129. DMA_Segment_t *segment;
  130. /* When a region corresponds to user memory, we need to lock all of the pages */
  131. /* down before we can figure out the physical addresses. The lockedPage array contains */
  132. /* the pages that were locked, and which subsequently need to be unlocked once the */
  133. /* memory is unmapped. */
  134. unsigned numLockedPages;
  135. struct page **lockedPages;
  136. } DMA_Region_t;
  137. typedef struct {
  138. int inUse; /* Is this mapping currently being used? */
  139. struct semaphore lock; /* Acquired when using this structure */
  140. enum dma_data_direction dir; /* Direction this transfer is intended for */
  141. /* In the event that we're mapping user memory, we need to know which task */
  142. /* the memory is for, so that we can obtain the correct mm locks. */
  143. struct task_struct *userTask;
  144. int numRegionsUsed;
  145. int numRegionsAllocated;
  146. DMA_Region_t *region;
  147. } DMA_MemMap_t;
  148. /****************************************************************************
  149. *
  150. * The DMA_DeviceAttribute_t contains information which describes a
  151. * particular DMA device (or peripheral).
  152. *
  153. * It is anticipated that the arrary of DMA_DeviceAttribute_t's will be
  154. * statically initialized.
  155. *
  156. *****************************************************************************/
  157. /* The device handler is called whenever a DMA operation completes. The reaon */
  158. /* for it to be called will be a bitmask with one or more of the following bits */
  159. /* set. */
  160. #define DMA_HANDLER_REASON_BLOCK_COMPLETE dmacHw_INTERRUPT_STATUS_BLOCK
  161. #define DMA_HANDLER_REASON_TRANSFER_COMPLETE dmacHw_INTERRUPT_STATUS_TRANS
  162. #define DMA_HANDLER_REASON_ERROR dmacHw_INTERRUPT_STATUS_ERROR
  163. typedef void (*DMA_DeviceHandler_t) (DMA_Device_t dev, int reason,
  164. void *userData);
  165. #define DMA_DEVICE_FLAG_ON_DMA0 0x00000001
  166. #define DMA_DEVICE_FLAG_ON_DMA1 0x00000002
  167. #define DMA_DEVICE_FLAG_PORT_PER_DMAC 0x00000004 /* If set, it means that the port used on DMAC0 is different from the port used on DMAC1 */
  168. #define DMA_DEVICE_FLAG_ALLOC_DMA1_FIRST 0x00000008 /* If set, allocate from DMA1 before allocating from DMA0 */
  169. #define DMA_DEVICE_FLAG_IS_DEDICATED 0x00000100
  170. #define DMA_DEVICE_FLAG_NO_ISR 0x00000200
  171. #define DMA_DEVICE_FLAG_ALLOW_LARGE_FIFO 0x00000400
  172. #define DMA_DEVICE_FLAG_IN_USE 0x00000800 /* If set, device is in use on a channel */
  173. /* Note: Some DMA devices can be used from multiple DMA Controllers. The bitmask is used to */
  174. /* determine which DMA controllers a given device can be used from, and the interface */
  175. /* array determeines the actual interface number to use for a given controller. */
  176. typedef struct {
  177. uint32_t flags; /* Bitmask of DMA_DEVICE_FLAG_xxx constants */
  178. uint8_t dedicatedController; /* Controller number to use if DMA_DEVICE_FLAG_IS_DEDICATED is set. */
  179. uint8_t dedicatedChannel; /* Channel number to use if DMA_DEVICE_FLAG_IS_DEDICATED is set. */
  180. const char *name; /* Will show up in the /proc entry */
  181. uint32_t dmacPort[DMA_NUM_CONTROLLERS]; /* Specifies the port number when DMA_DEVICE_FLAG_PORT_PER_DMAC flag is set */
  182. dmacHw_CONFIG_t config; /* Configuration to use when DMA'ing using this device */
  183. void *userData; /* Passed to the devHandler */
  184. DMA_DeviceHandler_t devHandler; /* Called when DMA operations finish. */
  185. timer_tick_count_t transferStartTime; /* Time the current transfer was started */
  186. /* The following statistical information will be collected and presented in a proc entry. */
  187. /* Note: With a contiuous bandwidth of 1 Gb/sec, it would take 584 years to overflow */
  188. /* a 64 bit counter. */
  189. uint64_t numTransfers; /* Number of DMA transfers performed */
  190. uint64_t transferTicks; /* Total time spent doing DMA transfers (measured in timer_tick_count_t's) */
  191. uint64_t transferBytes; /* Total bytes transferred */
  192. uint32_t timesBlocked; /* Number of times a channel was unavailable */
  193. uint32_t numBytes; /* Last transfer size */
  194. /* It's not possible to free memory which is allocated for the descriptors from within */
  195. /* the ISR. So make the presumption that a given device will tend to use the */
  196. /* same sized buffers over and over again, and we keep them around. */
  197. DMA_DescriptorRing_t ring; /* Ring of descriptors allocated for this device */
  198. /* We stash away some of the information from the previous transfer. If back-to-back */
  199. /* transfers are performed from the same buffer, then we don't have to keep re-initializing */
  200. /* the descriptor buffers. */
  201. uint32_t prevNumBytes;
  202. dma_addr_t prevSrcData;
  203. dma_addr_t prevDstData;
  204. } DMA_DeviceAttribute_t;
  205. /****************************************************************************
  206. *
  207. * DMA_Channel_t, DMA_Controller_t, and DMA_State_t are really internal
  208. * data structures and don't belong in this header file, but are included
  209. * merely for discussion.
  210. *
  211. * By the time this is implemented, these structures will be moved out into
  212. * the appropriate C source file instead.
  213. *
  214. *****************************************************************************/
  215. /****************************************************************************
  216. *
  217. * The DMA_Channel_t contains state information about each DMA channel. Some
  218. * of the channels are dedicated. Non-dedicated channels are shared
  219. * amongst the other devices.
  220. *
  221. *****************************************************************************/
  222. #define DMA_CHANNEL_FLAG_IN_USE 0x00000001
  223. #define DMA_CHANNEL_FLAG_IS_DEDICATED 0x00000002
  224. #define DMA_CHANNEL_FLAG_NO_ISR 0x00000004
  225. #define DMA_CHANNEL_FLAG_LARGE_FIFO 0x00000008
  226. typedef struct {
  227. uint32_t flags; /* bitmask of DMA_CHANNEL_FLAG_xxx constants */
  228. DMA_Device_t devType; /* Device this channel is currently reserved for */
  229. DMA_Device_t lastDevType; /* Device type that used this previously */
  230. char name[20]; /* Name passed onto request_irq */
  231. #if (DMA_DEBUG_TRACK_RESERVATION)
  232. const char *fileName; /* Place where channel reservation took place */
  233. int lineNum; /* Place where channel reservation took place */
  234. #endif
  235. dmacHw_HANDLE_t dmacHwHandle; /* low level channel handle. */
  236. } DMA_Channel_t;
  237. /****************************************************************************
  238. *
  239. * The DMA_Controller_t contains state information about each DMA controller.
  240. *
  241. * The freeChannelQ is stored in the controller data structure rather than
  242. * the channel data structure since several of the devices are accessible
  243. * from multiple controllers, and there is no way to know which controller
  244. * will become available first.
  245. *
  246. *****************************************************************************/
  247. typedef struct {
  248. DMA_Channel_t channel[DMA_NUM_CHANNELS];
  249. } DMA_Controller_t;
  250. /****************************************************************************
  251. *
  252. * The DMA_Global_t contains all of the global state information used by
  253. * the DMA code.
  254. *
  255. * Callers which need to allocate a shared channel will be queued up
  256. * on the freeChannelQ until a channel becomes available.
  257. *
  258. *****************************************************************************/
  259. typedef struct {
  260. struct semaphore lock; /* acquired when manipulating table entries */
  261. wait_queue_head_t freeChannelQ;
  262. DMA_Controller_t controller[DMA_NUM_CONTROLLERS];
  263. } DMA_Global_t;
  264. /* ---- Variable Externs ------------------------------------------------- */
  265. extern DMA_DeviceAttribute_t DMA_gDeviceAttribute[DMA_NUM_DEVICE_ENTRIES];
  266. /* ---- Function Prototypes ---------------------------------------------- */
  267. #if defined(__KERNEL__)
  268. /****************************************************************************/
  269. /**
  270. * Initializes the DMA module.
  271. *
  272. * @return
  273. * 0 - Success
  274. * < 0 - Error
  275. */
  276. /****************************************************************************/
  277. int dma_init(void);
  278. #if (DMA_DEBUG_TRACK_RESERVATION)
  279. DMA_Handle_t dma_request_channel_dbg(DMA_Device_t dev, const char *fileName,
  280. int lineNum);
  281. #define dma_request_channel(dev) dma_request_channel_dbg(dev, __FILE__, __LINE__)
  282. #else
  283. /****************************************************************************/
  284. /**
  285. * Reserves a channel for use with @a dev. If the device is setup to use
  286. * a shared channel, then this function will block until a free channel
  287. * becomes available.
  288. *
  289. * @return
  290. * >= 0 - A valid DMA Handle.
  291. * -EBUSY - Device is currently being used.
  292. * -ENODEV - Device handed in is invalid.
  293. */
  294. /****************************************************************************/
  295. DMA_Handle_t dma_request_channel(DMA_Device_t dev /* Device to use with the allocated channel. */
  296. );
  297. #endif
  298. /****************************************************************************/
  299. /**
  300. * Frees a previously allocated DMA Handle.
  301. *
  302. * @return
  303. * 0 - DMA Handle was released successfully.
  304. * -EINVAL - Invalid DMA handle
  305. */
  306. /****************************************************************************/
  307. int dma_free_channel(DMA_Handle_t channel /* DMA handle. */
  308. );
  309. /****************************************************************************/
  310. /**
  311. * Determines if a given device has been configured as using a shared
  312. * channel.
  313. *
  314. * @return boolean
  315. * 0 Device uses a dedicated channel
  316. * non-zero Device uses a shared channel
  317. */
  318. /****************************************************************************/
  319. int dma_device_is_channel_shared(DMA_Device_t dev /* Device to check. */
  320. );
  321. /****************************************************************************/
  322. /**
  323. * Allocates memory to hold a descriptor ring. The descriptor ring then
  324. * needs to be populated by making one or more calls to
  325. * dna_add_descriptors.
  326. *
  327. * The returned descriptor ring will be automatically initialized.
  328. *
  329. * @return
  330. * 0 Descriptor ring was allocated successfully
  331. * -ENOMEM Unable to allocate memory for the desired number of descriptors.
  332. */
  333. /****************************************************************************/
  334. int dma_alloc_descriptor_ring(DMA_DescriptorRing_t *ring, /* Descriptor ring to populate */
  335. int numDescriptors /* Number of descriptors that need to be allocated. */
  336. );
  337. /****************************************************************************/
  338. /**
  339. * Releases the memory which was previously allocated for a descriptor ring.
  340. */
  341. /****************************************************************************/
  342. void dma_free_descriptor_ring(DMA_DescriptorRing_t *ring /* Descriptor to release */
  343. );
  344. /****************************************************************************/
  345. /**
  346. * Initializes a descriptor ring, so that descriptors can be added to it.
  347. * Once a descriptor ring has been allocated, it may be reinitialized for
  348. * use with additional/different regions of memory.
  349. *
  350. * Note that if 7 descriptors are allocated, it's perfectly acceptable to
  351. * initialize the ring with a smaller number of descriptors. The amount
  352. * of memory allocated for the descriptor ring will not be reduced, and
  353. * the descriptor ring may be reinitialized later
  354. *
  355. * @return
  356. * 0 Descriptor ring was initialized successfully
  357. * -ENOMEM The descriptor which was passed in has insufficient space
  358. * to hold the desired number of descriptors.
  359. */
  360. /****************************************************************************/
  361. int dma_init_descriptor_ring(DMA_DescriptorRing_t *ring, /* Descriptor ring to initialize */
  362. int numDescriptors /* Number of descriptors to initialize. */
  363. );
  364. /****************************************************************************/
  365. /**
  366. * Determines the number of descriptors which would be required for a
  367. * transfer of the indicated memory region.
  368. *
  369. * This function also needs to know which DMA device this transfer will
  370. * be destined for, so that the appropriate DMA configuration can be retrieved.
  371. * DMA parameters such as transfer width, and whether this is a memory-to-memory
  372. * or memory-to-peripheral, etc can all affect the actual number of descriptors
  373. * required.
  374. *
  375. * @return
  376. * > 0 Returns the number of descriptors required for the indicated transfer
  377. * -EINVAL Invalid device type for this kind of transfer
  378. * (i.e. the device is _MEM_TO_DEV and not _DEV_TO_MEM)
  379. * -ENOMEM Memory exhausted
  380. */
  381. /****************************************************************************/
  382. int dma_calculate_descriptor_count(DMA_Device_t device, /* DMA Device that this will be associated with */
  383. dma_addr_t srcData, /* Place to get data to write to device */
  384. dma_addr_t dstData, /* Pointer to device data address */
  385. size_t numBytes /* Number of bytes to transfer to the device */
  386. );
  387. /****************************************************************************/
  388. /**
  389. * Adds a region of memory to the descriptor ring. Note that it may take
  390. * multiple descriptors for each region of memory. It is the callers
  391. * responsibility to allocate a sufficiently large descriptor ring.
  392. *
  393. * @return
  394. * 0 Descriptors were added successfully
  395. * -EINVAL Invalid device type for this kind of transfer
  396. * (i.e. the device is _MEM_TO_DEV and not _DEV_TO_MEM)
  397. * -ENOMEM Memory exhausted
  398. */
  399. /****************************************************************************/
  400. int dma_add_descriptors(DMA_DescriptorRing_t *ring, /* Descriptor ring to add descriptors to */
  401. DMA_Device_t device, /* DMA Device that descriptors are for */
  402. dma_addr_t srcData, /* Place to get data (memory or device) */
  403. dma_addr_t dstData, /* Place to put data (memory or device) */
  404. size_t numBytes /* Number of bytes to transfer to the device */
  405. );
  406. /****************************************************************************/
  407. /**
  408. * Sets the descriptor ring associated with a device.
  409. *
  410. * Once set, the descriptor ring will be associated with the device, even
  411. * across channel request/free calls. Passing in a NULL descriptor ring
  412. * will release any descriptor ring currently associated with the device.
  413. *
  414. * Note: If you call dma_transfer, or one of the other dma_alloc_ functions
  415. * the descriptor ring may be released and reallocated.
  416. *
  417. * Note: This function will release the descriptor memory for any current
  418. * descriptor ring associated with this device.
  419. */
  420. /****************************************************************************/
  421. int dma_set_device_descriptor_ring(DMA_Device_t device, /* Device to update the descriptor ring for. */
  422. DMA_DescriptorRing_t *ring /* Descriptor ring to add descriptors to */
  423. );
  424. /****************************************************************************/
  425. /**
  426. * Retrieves the descriptor ring associated with a device.
  427. */
  428. /****************************************************************************/
  429. int dma_get_device_descriptor_ring(DMA_Device_t device, /* Device to retrieve the descriptor ring for. */
  430. DMA_DescriptorRing_t *ring /* Place to store retrieved ring */
  431. );
  432. /****************************************************************************/
  433. /**
  434. * Allocates buffers for the descriptors. This is normally done automatically
  435. * but needs to be done explicitly when initiating a dma from interrupt
  436. * context.
  437. *
  438. * @return
  439. * 0 Descriptors were allocated successfully
  440. * -EINVAL Invalid device type for this kind of transfer
  441. * (i.e. the device is _MEM_TO_DEV and not _DEV_TO_MEM)
  442. * -ENOMEM Memory exhausted
  443. */
  444. /****************************************************************************/
  445. int dma_alloc_descriptors(DMA_Handle_t handle, /* DMA Handle */
  446. dmacHw_TRANSFER_TYPE_e transferType, /* Type of transfer being performed */
  447. dma_addr_t srcData, /* Place to get data to write to device */
  448. dma_addr_t dstData, /* Pointer to device data address */
  449. size_t numBytes /* Number of bytes to transfer to the device */
  450. );
  451. /****************************************************************************/
  452. /**
  453. * Allocates and sets up descriptors for a double buffered circular buffer.
  454. *
  455. * This is primarily intended to be used for things like the ingress samples
  456. * from a microphone.
  457. *
  458. * @return
  459. * > 0 Number of descriptors actually allocated.
  460. * -EINVAL Invalid device type for this kind of transfer
  461. * (i.e. the device is _MEM_TO_DEV and not _DEV_TO_MEM)
  462. * -ENOMEM Memory exhausted
  463. */
  464. /****************************************************************************/
  465. int dma_alloc_double_dst_descriptors(DMA_Handle_t handle, /* DMA Handle */
  466. dma_addr_t srcData, /* Physical address of source data */
  467. dma_addr_t dstData1, /* Physical address of first destination buffer */
  468. dma_addr_t dstData2, /* Physical address of second destination buffer */
  469. size_t numBytes /* Number of bytes in each destination buffer */
  470. );
  471. /****************************************************************************/
  472. /**
  473. * Initializes a DMA_MemMap_t data structure
  474. */
  475. /****************************************************************************/
  476. int dma_init_mem_map(DMA_MemMap_t *memMap /* Stores state information about the map */
  477. );
  478. /****************************************************************************/
  479. /**
  480. * Releases any memory currently being held by a memory mapping structure.
  481. */
  482. /****************************************************************************/
  483. int dma_term_mem_map(DMA_MemMap_t *memMap /* Stores state information about the map */
  484. );
  485. /****************************************************************************/
  486. /**
  487. * Looks at a memory address and categorizes it.
  488. *
  489. * @return One of the values from the DMA_MemType_t enumeration.
  490. */
  491. /****************************************************************************/
  492. DMA_MemType_t dma_mem_type(void *addr);
  493. /****************************************************************************/
  494. /**
  495. * Sets the process (aka userTask) associated with a mem map. This is
  496. * required if user-mode segments will be added to the mapping.
  497. */
  498. /****************************************************************************/
  499. static inline void dma_mem_map_set_user_task(DMA_MemMap_t *memMap,
  500. struct task_struct *task)
  501. {
  502. memMap->userTask = task;
  503. }
  504. /****************************************************************************/
  505. /**
  506. * Looks at a memory address and determines if we support DMA'ing to/from
  507. * that type of memory.
  508. *
  509. * @return boolean -
  510. * return value != 0 means dma supported
  511. * return value == 0 means dma not supported
  512. */
  513. /****************************************************************************/
  514. int dma_mem_supports_dma(void *addr);
  515. /****************************************************************************/
  516. /**
  517. * Initializes a memory map for use. Since this function acquires a
  518. * sempaphore within the memory map, it is VERY important that dma_unmap
  519. * be called when you're finished using the map.
  520. */
  521. /****************************************************************************/
  522. int dma_map_start(DMA_MemMap_t *memMap, /* Stores state information about the map */
  523. enum dma_data_direction dir /* Direction that the mapping will be going */
  524. );
  525. /****************************************************************************/
  526. /**
  527. * Adds a segment of memory to a memory map.
  528. *
  529. * @return 0 on success, error code otherwise.
  530. */
  531. /****************************************************************************/
  532. int dma_map_add_region(DMA_MemMap_t *memMap, /* Stores state information about the map */
  533. void *mem, /* Virtual address that we want to get a map of */
  534. size_t numBytes /* Number of bytes being mapped */
  535. );
  536. /****************************************************************************/
  537. /**
  538. * Creates a descriptor ring from a memory mapping.
  539. *
  540. * @return 0 on success, error code otherwise.
  541. */
  542. /****************************************************************************/
  543. int dma_map_create_descriptor_ring(DMA_Device_t dev, /* DMA device (where the ring is stored) */
  544. DMA_MemMap_t *memMap, /* Memory map that will be used */
  545. dma_addr_t devPhysAddr /* Physical address of device */
  546. );
  547. /****************************************************************************/
  548. /**
  549. * Maps in a memory region such that it can be used for performing a DMA.
  550. *
  551. * @return
  552. */
  553. /****************************************************************************/
  554. int dma_map_mem(DMA_MemMap_t *memMap, /* Stores state information about the map */
  555. void *addr, /* Virtual address that we want to get a map of */
  556. size_t count, /* Number of bytes being mapped */
  557. enum dma_data_direction dir /* Direction that the mapping will be going */
  558. );
  559. /****************************************************************************/
  560. /**
  561. * Maps in a memory region such that it can be used for performing a DMA.
  562. *
  563. * @return
  564. */
  565. /****************************************************************************/
  566. int dma_unmap(DMA_MemMap_t *memMap, /* Stores state information about the map */
  567. int dirtied /* non-zero if any of the pages were modified */
  568. );
  569. /****************************************************************************/
  570. /**
  571. * Initiates a transfer when the descriptors have already been setup.
  572. *
  573. * This is a special case, and normally, the dma_transfer_xxx functions should
  574. * be used.
  575. *
  576. * @return
  577. * 0 Transfer was started successfully
  578. * -ENODEV Invalid handle
  579. */
  580. /****************************************************************************/
  581. int dma_start_transfer(DMA_Handle_t handle);
  582. /****************************************************************************/
  583. /**
  584. * Stops a previously started DMA transfer.
  585. *
  586. * @return
  587. * 0 Transfer was stopped successfully
  588. * -ENODEV Invalid handle
  589. */
  590. /****************************************************************************/
  591. int dma_stop_transfer(DMA_Handle_t handle);
  592. /****************************************************************************/
  593. /**
  594. * Waits for a DMA to complete by polling. This function is only intended
  595. * to be used for testing. Interrupts should be used for most DMA operations.
  596. */
  597. /****************************************************************************/
  598. int dma_wait_transfer_done(DMA_Handle_t handle);
  599. /****************************************************************************/
  600. /**
  601. * Initiates a DMA transfer
  602. *
  603. * @return
  604. * 0 Transfer was started successfully
  605. * -EINVAL Invalid device type for this kind of transfer
  606. * (i.e. the device is _MEM_TO_DEV and not _DEV_TO_MEM)
  607. */
  608. /****************************************************************************/
  609. int dma_transfer(DMA_Handle_t handle, /* DMA Handle */
  610. dmacHw_TRANSFER_TYPE_e transferType, /* Type of transfer being performed */
  611. dma_addr_t srcData, /* Place to get data to write to device */
  612. dma_addr_t dstData, /* Pointer to device data address */
  613. size_t numBytes /* Number of bytes to transfer to the device */
  614. );
  615. /****************************************************************************/
  616. /**
  617. * Initiates a transfer from memory to a device.
  618. *
  619. * @return
  620. * 0 Transfer was started successfully
  621. * -EINVAL Invalid device type for this kind of transfer
  622. * (i.e. the device is _DEV_TO_MEM and not _MEM_TO_DEV)
  623. */
  624. /****************************************************************************/
  625. static inline int dma_transfer_to_device(DMA_Handle_t handle, /* DMA Handle */
  626. dma_addr_t srcData, /* Place to get data to write to device (physical address) */
  627. dma_addr_t dstData, /* Pointer to device data address (physical address) */
  628. size_t numBytes /* Number of bytes to transfer to the device */
  629. ) {
  630. return dma_transfer(handle,
  631. dmacHw_TRANSFER_TYPE_MEM_TO_PERIPHERAL,
  632. srcData, dstData, numBytes);
  633. }
  634. /****************************************************************************/
  635. /**
  636. * Initiates a transfer from a device to memory.
  637. *
  638. * @return
  639. * 0 Transfer was started successfully
  640. * -EINVAL Invalid device type for this kind of transfer
  641. * (i.e. the device is _MEM_TO_DEV and not _DEV_TO_MEM)
  642. */
  643. /****************************************************************************/
  644. static inline int dma_transfer_from_device(DMA_Handle_t handle, /* DMA Handle */
  645. dma_addr_t srcData, /* Pointer to the device data address (physical address) */
  646. dma_addr_t dstData, /* Place to store data retrieved from the device (physical address) */
  647. size_t numBytes /* Number of bytes to retrieve from the device */
  648. ) {
  649. return dma_transfer(handle,
  650. dmacHw_TRANSFER_TYPE_PERIPHERAL_TO_MEM,
  651. srcData, dstData, numBytes);
  652. }
  653. /****************************************************************************/
  654. /**
  655. * Initiates a memory to memory transfer.
  656. *
  657. * @return
  658. * 0 Transfer was started successfully
  659. * -EINVAL Invalid device type for this kind of transfer
  660. * (i.e. the device wasn't DMA_DEVICE_MEM_TO_MEM)
  661. */
  662. /****************************************************************************/
  663. static inline int dma_transfer_mem_to_mem(DMA_Handle_t handle, /* DMA Handle */
  664. dma_addr_t srcData, /* Place to transfer data from (physical address) */
  665. dma_addr_t dstData, /* Place to transfer data to (physical address) */
  666. size_t numBytes /* Number of bytes to transfer */
  667. ) {
  668. return dma_transfer(handle,
  669. dmacHw_TRANSFER_TYPE_MEM_TO_MEM,
  670. srcData, dstData, numBytes);
  671. }
  672. /****************************************************************************/
  673. /**
  674. * Set the callback function which will be called when a transfer completes.
  675. * If a NULL callback function is set, then no callback will occur.
  676. *
  677. * @note @a devHandler will be called from IRQ context.
  678. *
  679. * @return
  680. * 0 - Success
  681. * -ENODEV - Device handed in is invalid.
  682. */
  683. /****************************************************************************/
  684. int dma_set_device_handler(DMA_Device_t dev, /* Device to set the callback for. */
  685. DMA_DeviceHandler_t devHandler, /* Function to call when the DMA completes */
  686. void *userData /* Pointer which will be passed to devHandler. */
  687. );
  688. #endif
  689. #endif /* ASM_ARM_ARCH_BCMRING_DMA_H */