dma.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519
  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.c
  17. *
  18. * @brief Implements the DMA interface.
  19. */
  20. /****************************************************************************/
  21. /* ---- Include Files ---------------------------------------------------- */
  22. #include <linux/module.h>
  23. #include <linux/device.h>
  24. #include <linux/dma-mapping.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/sched.h>
  27. #include <linux/irqreturn.h>
  28. #include <linux/proc_fs.h>
  29. #include <linux/slab.h>
  30. #include <mach/timer.h>
  31. #include <linux/pfn.h>
  32. #include <linux/atomic.h>
  33. #include <mach/dma.h>
  34. /* ---- Public Variables ------------------------------------------------- */
  35. /* ---- Private Constants and Types -------------------------------------- */
  36. #define MAKE_HANDLE(controllerIdx, channelIdx) (((controllerIdx) << 4) | (channelIdx))
  37. #define CONTROLLER_FROM_HANDLE(handle) (((handle) >> 4) & 0x0f)
  38. #define CHANNEL_FROM_HANDLE(handle) ((handle) & 0x0f)
  39. /* ---- Private Variables ------------------------------------------------ */
  40. static DMA_Global_t gDMA;
  41. static struct proc_dir_entry *gDmaDir;
  42. #include "dma_device.c"
  43. /* ---- Private Function Prototypes -------------------------------------- */
  44. /* ---- Functions ------------------------------------------------------- */
  45. /****************************************************************************/
  46. /**
  47. * Displays information for /proc/dma/channels
  48. */
  49. /****************************************************************************/
  50. static int dma_proc_read_channels(char *buf, char **start, off_t offset,
  51. int count, int *eof, void *data)
  52. {
  53. int controllerIdx;
  54. int channelIdx;
  55. int limit = count - 200;
  56. int len = 0;
  57. DMA_Channel_t *channel;
  58. if (down_interruptible(&gDMA.lock) < 0) {
  59. return -ERESTARTSYS;
  60. }
  61. for (controllerIdx = 0; controllerIdx < DMA_NUM_CONTROLLERS;
  62. controllerIdx++) {
  63. for (channelIdx = 0; channelIdx < DMA_NUM_CHANNELS;
  64. channelIdx++) {
  65. if (len >= limit) {
  66. break;
  67. }
  68. channel =
  69. &gDMA.controller[controllerIdx].channel[channelIdx];
  70. len +=
  71. sprintf(buf + len, "%d:%d ", controllerIdx,
  72. channelIdx);
  73. if ((channel->flags & DMA_CHANNEL_FLAG_IS_DEDICATED) !=
  74. 0) {
  75. len +=
  76. sprintf(buf + len, "Dedicated for %s ",
  77. DMA_gDeviceAttribute[channel->
  78. devType].name);
  79. } else {
  80. len += sprintf(buf + len, "Shared ");
  81. }
  82. if ((channel->flags & DMA_CHANNEL_FLAG_NO_ISR) != 0) {
  83. len += sprintf(buf + len, "No ISR ");
  84. }
  85. if ((channel->flags & DMA_CHANNEL_FLAG_LARGE_FIFO) != 0) {
  86. len += sprintf(buf + len, "Fifo: 128 ");
  87. } else {
  88. len += sprintf(buf + len, "Fifo: 64 ");
  89. }
  90. if ((channel->flags & DMA_CHANNEL_FLAG_IN_USE) != 0) {
  91. len +=
  92. sprintf(buf + len, "InUse by %s",
  93. DMA_gDeviceAttribute[channel->
  94. devType].name);
  95. #if (DMA_DEBUG_TRACK_RESERVATION)
  96. len +=
  97. sprintf(buf + len, " (%s:%d)",
  98. channel->fileName,
  99. channel->lineNum);
  100. #endif
  101. } else {
  102. len += sprintf(buf + len, "Avail ");
  103. }
  104. if (channel->lastDevType != DMA_DEVICE_NONE) {
  105. len +=
  106. sprintf(buf + len, "Last use: %s ",
  107. DMA_gDeviceAttribute[channel->
  108. lastDevType].
  109. name);
  110. }
  111. len += sprintf(buf + len, "\n");
  112. }
  113. }
  114. up(&gDMA.lock);
  115. *eof = 1;
  116. return len;
  117. }
  118. /****************************************************************************/
  119. /**
  120. * Displays information for /proc/dma/devices
  121. */
  122. /****************************************************************************/
  123. static int dma_proc_read_devices(char *buf, char **start, off_t offset,
  124. int count, int *eof, void *data)
  125. {
  126. int limit = count - 200;
  127. int len = 0;
  128. int devIdx;
  129. if (down_interruptible(&gDMA.lock) < 0) {
  130. return -ERESTARTSYS;
  131. }
  132. for (devIdx = 0; devIdx < DMA_NUM_DEVICE_ENTRIES; devIdx++) {
  133. DMA_DeviceAttribute_t *devAttr = &DMA_gDeviceAttribute[devIdx];
  134. if (devAttr->name == NULL) {
  135. continue;
  136. }
  137. if (len >= limit) {
  138. break;
  139. }
  140. len += sprintf(buf + len, "%-12s ", devAttr->name);
  141. if ((devAttr->flags & DMA_DEVICE_FLAG_IS_DEDICATED) != 0) {
  142. len +=
  143. sprintf(buf + len, "Dedicated %d:%d ",
  144. devAttr->dedicatedController,
  145. devAttr->dedicatedChannel);
  146. } else {
  147. len += sprintf(buf + len, "Shared DMA:");
  148. if ((devAttr->flags & DMA_DEVICE_FLAG_ON_DMA0) != 0) {
  149. len += sprintf(buf + len, "0");
  150. }
  151. if ((devAttr->flags & DMA_DEVICE_FLAG_ON_DMA1) != 0) {
  152. len += sprintf(buf + len, "1");
  153. }
  154. len += sprintf(buf + len, " ");
  155. }
  156. if ((devAttr->flags & DMA_DEVICE_FLAG_NO_ISR) != 0) {
  157. len += sprintf(buf + len, "NoISR ");
  158. }
  159. if ((devAttr->flags & DMA_DEVICE_FLAG_ALLOW_LARGE_FIFO) != 0) {
  160. len += sprintf(buf + len, "Allow-128 ");
  161. }
  162. len +=
  163. sprintf(buf + len,
  164. "Xfer #: %Lu Ticks: %Lu Bytes: %Lu DescLen: %u\n",
  165. devAttr->numTransfers, devAttr->transferTicks,
  166. devAttr->transferBytes,
  167. devAttr->ring.bytesAllocated);
  168. }
  169. up(&gDMA.lock);
  170. *eof = 1;
  171. return len;
  172. }
  173. /****************************************************************************/
  174. /**
  175. * Determines if a DMA_Device_t is "valid".
  176. *
  177. * @return
  178. * TRUE - dma device is valid
  179. * FALSE - dma device isn't valid
  180. */
  181. /****************************************************************************/
  182. static inline int IsDeviceValid(DMA_Device_t device)
  183. {
  184. return (device >= 0) && (device < DMA_NUM_DEVICE_ENTRIES);
  185. }
  186. /****************************************************************************/
  187. /**
  188. * Translates a DMA handle into a pointer to a channel.
  189. *
  190. * @return
  191. * non-NULL - pointer to DMA_Channel_t
  192. * NULL - DMA Handle was invalid
  193. */
  194. /****************************************************************************/
  195. static inline DMA_Channel_t *HandleToChannel(DMA_Handle_t handle)
  196. {
  197. int controllerIdx;
  198. int channelIdx;
  199. controllerIdx = CONTROLLER_FROM_HANDLE(handle);
  200. channelIdx = CHANNEL_FROM_HANDLE(handle);
  201. if ((controllerIdx > DMA_NUM_CONTROLLERS)
  202. || (channelIdx > DMA_NUM_CHANNELS)) {
  203. return NULL;
  204. }
  205. return &gDMA.controller[controllerIdx].channel[channelIdx];
  206. }
  207. /****************************************************************************/
  208. /**
  209. * Interrupt handler which is called to process DMA interrupts.
  210. */
  211. /****************************************************************************/
  212. static irqreturn_t dma_interrupt_handler(int irq, void *dev_id)
  213. {
  214. DMA_Channel_t *channel;
  215. DMA_DeviceAttribute_t *devAttr;
  216. int irqStatus;
  217. channel = (DMA_Channel_t *) dev_id;
  218. /* Figure out why we were called, and knock down the interrupt */
  219. irqStatus = dmacHw_getInterruptStatus(channel->dmacHwHandle);
  220. dmacHw_clearInterrupt(channel->dmacHwHandle);
  221. if ((channel->devType < 0)
  222. || (channel->devType > DMA_NUM_DEVICE_ENTRIES)) {
  223. printk(KERN_ERR "dma_interrupt_handler: Invalid devType: %d\n",
  224. channel->devType);
  225. return IRQ_NONE;
  226. }
  227. devAttr = &DMA_gDeviceAttribute[channel->devType];
  228. /* Update stats */
  229. if ((irqStatus & dmacHw_INTERRUPT_STATUS_TRANS) != 0) {
  230. devAttr->transferTicks +=
  231. (timer_get_tick_count() - devAttr->transferStartTime);
  232. }
  233. if ((irqStatus & dmacHw_INTERRUPT_STATUS_ERROR) != 0) {
  234. printk(KERN_ERR
  235. "dma_interrupt_handler: devType :%d DMA error (%s)\n",
  236. channel->devType, devAttr->name);
  237. } else {
  238. devAttr->numTransfers++;
  239. devAttr->transferBytes += devAttr->numBytes;
  240. }
  241. /* Call any installed handler */
  242. if (devAttr->devHandler != NULL) {
  243. devAttr->devHandler(channel->devType, irqStatus,
  244. devAttr->userData);
  245. }
  246. return IRQ_HANDLED;
  247. }
  248. /****************************************************************************/
  249. /**
  250. * Allocates memory to hold a descriptor ring. The descriptor ring then
  251. * needs to be populated by making one or more calls to
  252. * dna_add_descriptors.
  253. *
  254. * The returned descriptor ring will be automatically initialized.
  255. *
  256. * @return
  257. * 0 Descriptor ring was allocated successfully
  258. * -EINVAL Invalid parameters passed in
  259. * -ENOMEM Unable to allocate memory for the desired number of descriptors.
  260. */
  261. /****************************************************************************/
  262. int dma_alloc_descriptor_ring(DMA_DescriptorRing_t *ring, /* Descriptor ring to populate */
  263. int numDescriptors /* Number of descriptors that need to be allocated. */
  264. ) {
  265. size_t bytesToAlloc = dmacHw_descriptorLen(numDescriptors);
  266. if ((ring == NULL) || (numDescriptors <= 0)) {
  267. return -EINVAL;
  268. }
  269. ring->physAddr = 0;
  270. ring->descriptorsAllocated = 0;
  271. ring->bytesAllocated = 0;
  272. ring->virtAddr = dma_alloc_writecombine(NULL,
  273. bytesToAlloc,
  274. &ring->physAddr,
  275. GFP_KERNEL);
  276. if (ring->virtAddr == NULL) {
  277. return -ENOMEM;
  278. }
  279. ring->bytesAllocated = bytesToAlloc;
  280. ring->descriptorsAllocated = numDescriptors;
  281. return dma_init_descriptor_ring(ring, numDescriptors);
  282. }
  283. EXPORT_SYMBOL(dma_alloc_descriptor_ring);
  284. /****************************************************************************/
  285. /**
  286. * Releases the memory which was previously allocated for a descriptor ring.
  287. */
  288. /****************************************************************************/
  289. void dma_free_descriptor_ring(DMA_DescriptorRing_t *ring /* Descriptor to release */
  290. ) {
  291. if (ring->virtAddr != NULL) {
  292. dma_free_writecombine(NULL,
  293. ring->bytesAllocated,
  294. ring->virtAddr, ring->physAddr);
  295. }
  296. ring->bytesAllocated = 0;
  297. ring->descriptorsAllocated = 0;
  298. ring->virtAddr = NULL;
  299. ring->physAddr = 0;
  300. }
  301. EXPORT_SYMBOL(dma_free_descriptor_ring);
  302. /****************************************************************************/
  303. /**
  304. * Initializes a descriptor ring, so that descriptors can be added to it.
  305. * Once a descriptor ring has been allocated, it may be reinitialized for
  306. * use with additional/different regions of memory.
  307. *
  308. * Note that if 7 descriptors are allocated, it's perfectly acceptable to
  309. * initialize the ring with a smaller number of descriptors. The amount
  310. * of memory allocated for the descriptor ring will not be reduced, and
  311. * the descriptor ring may be reinitialized later
  312. *
  313. * @return
  314. * 0 Descriptor ring was initialized successfully
  315. * -ENOMEM The descriptor which was passed in has insufficient space
  316. * to hold the desired number of descriptors.
  317. */
  318. /****************************************************************************/
  319. int dma_init_descriptor_ring(DMA_DescriptorRing_t *ring, /* Descriptor ring to initialize */
  320. int numDescriptors /* Number of descriptors to initialize. */
  321. ) {
  322. if (ring->virtAddr == NULL) {
  323. return -EINVAL;
  324. }
  325. if (dmacHw_initDescriptor(ring->virtAddr,
  326. ring->physAddr,
  327. ring->bytesAllocated, numDescriptors) < 0) {
  328. printk(KERN_ERR
  329. "dma_init_descriptor_ring: dmacHw_initDescriptor failed\n");
  330. return -ENOMEM;
  331. }
  332. return 0;
  333. }
  334. EXPORT_SYMBOL(dma_init_descriptor_ring);
  335. /****************************************************************************/
  336. /**
  337. * Determines the number of descriptors which would be required for a
  338. * transfer of the indicated memory region.
  339. *
  340. * This function also needs to know which DMA device this transfer will
  341. * be destined for, so that the appropriate DMA configuration can be retrieved.
  342. * DMA parameters such as transfer width, and whether this is a memory-to-memory
  343. * or memory-to-peripheral, etc can all affect the actual number of descriptors
  344. * required.
  345. *
  346. * @return
  347. * > 0 Returns the number of descriptors required for the indicated transfer
  348. * -ENODEV - Device handed in is invalid.
  349. * -EINVAL Invalid parameters
  350. * -ENOMEM Memory exhausted
  351. */
  352. /****************************************************************************/
  353. int dma_calculate_descriptor_count(DMA_Device_t device, /* DMA Device that this will be associated with */
  354. dma_addr_t srcData, /* Place to get data to write to device */
  355. dma_addr_t dstData, /* Pointer to device data address */
  356. size_t numBytes /* Number of bytes to transfer to the device */
  357. ) {
  358. int numDescriptors;
  359. DMA_DeviceAttribute_t *devAttr;
  360. if (!IsDeviceValid(device)) {
  361. return -ENODEV;
  362. }
  363. devAttr = &DMA_gDeviceAttribute[device];
  364. numDescriptors = dmacHw_calculateDescriptorCount(&devAttr->config,
  365. (void *)srcData,
  366. (void *)dstData,
  367. numBytes);
  368. if (numDescriptors < 0) {
  369. printk(KERN_ERR
  370. "dma_calculate_descriptor_count: dmacHw_calculateDescriptorCount failed\n");
  371. return -EINVAL;
  372. }
  373. return numDescriptors;
  374. }
  375. EXPORT_SYMBOL(dma_calculate_descriptor_count);
  376. /****************************************************************************/
  377. /**
  378. * Adds a region of memory to the descriptor ring. Note that it may take
  379. * multiple descriptors for each region of memory. It is the callers
  380. * responsibility to allocate a sufficiently large descriptor ring.
  381. *
  382. * @return
  383. * 0 Descriptors were added successfully
  384. * -ENODEV Device handed in is invalid.
  385. * -EINVAL Invalid parameters
  386. * -ENOMEM Memory exhausted
  387. */
  388. /****************************************************************************/
  389. int dma_add_descriptors(DMA_DescriptorRing_t *ring, /* Descriptor ring to add descriptors to */
  390. DMA_Device_t device, /* DMA Device that descriptors are for */
  391. dma_addr_t srcData, /* Place to get data (memory or device) */
  392. dma_addr_t dstData, /* Place to put data (memory or device) */
  393. size_t numBytes /* Number of bytes to transfer to the device */
  394. ) {
  395. int rc;
  396. DMA_DeviceAttribute_t *devAttr;
  397. if (!IsDeviceValid(device)) {
  398. return -ENODEV;
  399. }
  400. devAttr = &DMA_gDeviceAttribute[device];
  401. rc = dmacHw_setDataDescriptor(&devAttr->config,
  402. ring->virtAddr,
  403. (void *)srcData,
  404. (void *)dstData, numBytes);
  405. if (rc < 0) {
  406. printk(KERN_ERR
  407. "dma_add_descriptors: dmacHw_setDataDescriptor failed with code: %d\n",
  408. rc);
  409. return -ENOMEM;
  410. }
  411. return 0;
  412. }
  413. EXPORT_SYMBOL(dma_add_descriptors);
  414. /****************************************************************************/
  415. /**
  416. * Sets the descriptor ring associated with a device.
  417. *
  418. * Once set, the descriptor ring will be associated with the device, even
  419. * across channel request/free calls. Passing in a NULL descriptor ring
  420. * will release any descriptor ring currently associated with the device.
  421. *
  422. * Note: If you call dma_transfer, or one of the other dma_alloc_ functions
  423. * the descriptor ring may be released and reallocated.
  424. *
  425. * Note: This function will release the descriptor memory for any current
  426. * descriptor ring associated with this device.
  427. *
  428. * @return
  429. * 0 Descriptors were added successfully
  430. * -ENODEV Device handed in is invalid.
  431. */
  432. /****************************************************************************/
  433. int dma_set_device_descriptor_ring(DMA_Device_t device, /* Device to update the descriptor ring for. */
  434. DMA_DescriptorRing_t *ring /* Descriptor ring to add descriptors to */
  435. ) {
  436. DMA_DeviceAttribute_t *devAttr;
  437. if (!IsDeviceValid(device)) {
  438. return -ENODEV;
  439. }
  440. devAttr = &DMA_gDeviceAttribute[device];
  441. /* Free the previously allocated descriptor ring */
  442. dma_free_descriptor_ring(&devAttr->ring);
  443. if (ring != NULL) {
  444. /* Copy in the new one */
  445. devAttr->ring = *ring;
  446. }
  447. /* Set things up so that if dma_transfer is called then this descriptor */
  448. /* ring will get freed. */
  449. devAttr->prevSrcData = 0;
  450. devAttr->prevDstData = 0;
  451. devAttr->prevNumBytes = 0;
  452. return 0;
  453. }
  454. EXPORT_SYMBOL(dma_set_device_descriptor_ring);
  455. /****************************************************************************/
  456. /**
  457. * Retrieves the descriptor ring associated with a device.
  458. *
  459. * @return
  460. * 0 Descriptors were added successfully
  461. * -ENODEV Device handed in is invalid.
  462. */
  463. /****************************************************************************/
  464. int dma_get_device_descriptor_ring(DMA_Device_t device, /* Device to retrieve the descriptor ring for. */
  465. DMA_DescriptorRing_t *ring /* Place to store retrieved ring */
  466. ) {
  467. DMA_DeviceAttribute_t *devAttr;
  468. memset(ring, 0, sizeof(*ring));
  469. if (!IsDeviceValid(device)) {
  470. return -ENODEV;
  471. }
  472. devAttr = &DMA_gDeviceAttribute[device];
  473. *ring = devAttr->ring;
  474. return 0;
  475. }
  476. EXPORT_SYMBOL(dma_get_device_descriptor_ring);
  477. /****************************************************************************/
  478. /**
  479. * Configures a DMA channel.
  480. *
  481. * @return
  482. * >= 0 - Initialization was successful.
  483. *
  484. * -EBUSY - Device is currently being used.
  485. * -ENODEV - Device handed in is invalid.
  486. */
  487. /****************************************************************************/
  488. static int ConfigChannel(DMA_Handle_t handle)
  489. {
  490. DMA_Channel_t *channel;
  491. DMA_DeviceAttribute_t *devAttr;
  492. int controllerIdx;
  493. channel = HandleToChannel(handle);
  494. if (channel == NULL) {
  495. return -ENODEV;
  496. }
  497. devAttr = &DMA_gDeviceAttribute[channel->devType];
  498. controllerIdx = CONTROLLER_FROM_HANDLE(handle);
  499. if ((devAttr->flags & DMA_DEVICE_FLAG_PORT_PER_DMAC) != 0) {
  500. if (devAttr->config.transferType ==
  501. dmacHw_TRANSFER_TYPE_MEM_TO_PERIPHERAL) {
  502. devAttr->config.dstPeripheralPort =
  503. devAttr->dmacPort[controllerIdx];
  504. } else if (devAttr->config.transferType ==
  505. dmacHw_TRANSFER_TYPE_PERIPHERAL_TO_MEM) {
  506. devAttr->config.srcPeripheralPort =
  507. devAttr->dmacPort[controllerIdx];
  508. }
  509. }
  510. if (dmacHw_configChannel(channel->dmacHwHandle, &devAttr->config) != 0) {
  511. printk(KERN_ERR "ConfigChannel: dmacHw_configChannel failed\n");
  512. return -EIO;
  513. }
  514. return 0;
  515. }
  516. /****************************************************************************/
  517. /**
  518. * Initializes all of the data structures associated with the DMA.
  519. * @return
  520. * >= 0 - Initialization was successful.
  521. *
  522. * -EBUSY - Device is currently being used.
  523. * -ENODEV - Device handed in is invalid.
  524. */
  525. /****************************************************************************/
  526. int dma_init(void)
  527. {
  528. int rc = 0;
  529. int controllerIdx;
  530. int channelIdx;
  531. DMA_Device_t devIdx;
  532. DMA_Channel_t *channel;
  533. DMA_Handle_t dedicatedHandle;
  534. memset(&gDMA, 0, sizeof(gDMA));
  535. sema_init(&gDMA.lock, 0);
  536. init_waitqueue_head(&gDMA.freeChannelQ);
  537. /* Initialize the Hardware */
  538. dmacHw_initDma();
  539. /* Start off by marking all of the DMA channels as shared. */
  540. for (controllerIdx = 0; controllerIdx < DMA_NUM_CONTROLLERS;
  541. controllerIdx++) {
  542. for (channelIdx = 0; channelIdx < DMA_NUM_CHANNELS;
  543. channelIdx++) {
  544. channel =
  545. &gDMA.controller[controllerIdx].channel[channelIdx];
  546. channel->flags = 0;
  547. channel->devType = DMA_DEVICE_NONE;
  548. channel->lastDevType = DMA_DEVICE_NONE;
  549. #if (DMA_DEBUG_TRACK_RESERVATION)
  550. channel->fileName = "";
  551. channel->lineNum = 0;
  552. #endif
  553. channel->dmacHwHandle =
  554. dmacHw_getChannelHandle(dmacHw_MAKE_CHANNEL_ID
  555. (controllerIdx,
  556. channelIdx));
  557. dmacHw_initChannel(channel->dmacHwHandle);
  558. }
  559. }
  560. /* Record any special attributes that channels may have */
  561. gDMA.controller[0].channel[0].flags |= DMA_CHANNEL_FLAG_LARGE_FIFO;
  562. gDMA.controller[0].channel[1].flags |= DMA_CHANNEL_FLAG_LARGE_FIFO;
  563. gDMA.controller[1].channel[0].flags |= DMA_CHANNEL_FLAG_LARGE_FIFO;
  564. gDMA.controller[1].channel[1].flags |= DMA_CHANNEL_FLAG_LARGE_FIFO;
  565. /* Now walk through and record the dedicated channels. */
  566. for (devIdx = 0; devIdx < DMA_NUM_DEVICE_ENTRIES; devIdx++) {
  567. DMA_DeviceAttribute_t *devAttr = &DMA_gDeviceAttribute[devIdx];
  568. if (((devAttr->flags & DMA_DEVICE_FLAG_NO_ISR) != 0)
  569. && ((devAttr->flags & DMA_DEVICE_FLAG_IS_DEDICATED) == 0)) {
  570. printk(KERN_ERR
  571. "DMA Device: %s Can only request NO_ISR for dedicated devices\n",
  572. devAttr->name);
  573. rc = -EINVAL;
  574. goto out;
  575. }
  576. if ((devAttr->flags & DMA_DEVICE_FLAG_IS_DEDICATED) != 0) {
  577. /* This is a dedicated device. Mark the channel as being reserved. */
  578. if (devAttr->dedicatedController >= DMA_NUM_CONTROLLERS) {
  579. printk(KERN_ERR
  580. "DMA Device: %s DMA Controller %d is out of range\n",
  581. devAttr->name,
  582. devAttr->dedicatedController);
  583. rc = -EINVAL;
  584. goto out;
  585. }
  586. if (devAttr->dedicatedChannel >= DMA_NUM_CHANNELS) {
  587. printk(KERN_ERR
  588. "DMA Device: %s DMA Channel %d is out of range\n",
  589. devAttr->name,
  590. devAttr->dedicatedChannel);
  591. rc = -EINVAL;
  592. goto out;
  593. }
  594. dedicatedHandle =
  595. MAKE_HANDLE(devAttr->dedicatedController,
  596. devAttr->dedicatedChannel);
  597. channel = HandleToChannel(dedicatedHandle);
  598. if ((channel->flags & DMA_CHANNEL_FLAG_IS_DEDICATED) !=
  599. 0) {
  600. printk
  601. ("DMA Device: %s attempting to use same DMA Controller:Channel (%d:%d) as %s\n",
  602. devAttr->name,
  603. devAttr->dedicatedController,
  604. devAttr->dedicatedChannel,
  605. DMA_gDeviceAttribute[channel->devType].
  606. name);
  607. rc = -EBUSY;
  608. goto out;
  609. }
  610. channel->flags |= DMA_CHANNEL_FLAG_IS_DEDICATED;
  611. channel->devType = devIdx;
  612. if (devAttr->flags & DMA_DEVICE_FLAG_NO_ISR) {
  613. channel->flags |= DMA_CHANNEL_FLAG_NO_ISR;
  614. }
  615. /* For dedicated channels, we can go ahead and configure the DMA channel now */
  616. /* as well. */
  617. ConfigChannel(dedicatedHandle);
  618. }
  619. }
  620. /* Go through and register the interrupt handlers */
  621. for (controllerIdx = 0; controllerIdx < DMA_NUM_CONTROLLERS;
  622. controllerIdx++) {
  623. for (channelIdx = 0; channelIdx < DMA_NUM_CHANNELS;
  624. channelIdx++) {
  625. channel =
  626. &gDMA.controller[controllerIdx].channel[channelIdx];
  627. if ((channel->flags & DMA_CHANNEL_FLAG_NO_ISR) == 0) {
  628. snprintf(channel->name, sizeof(channel->name),
  629. "dma %d:%d %s", controllerIdx,
  630. channelIdx,
  631. channel->devType ==
  632. DMA_DEVICE_NONE ? "" :
  633. DMA_gDeviceAttribute[channel->devType].
  634. name);
  635. rc =
  636. request_irq(IRQ_DMA0C0 +
  637. (controllerIdx *
  638. DMA_NUM_CHANNELS) +
  639. channelIdx,
  640. dma_interrupt_handler,
  641. IRQF_DISABLED, channel->name,
  642. channel);
  643. if (rc != 0) {
  644. printk(KERN_ERR
  645. "request_irq for IRQ_DMA%dC%d failed\n",
  646. controllerIdx, channelIdx);
  647. }
  648. }
  649. }
  650. }
  651. /* Create /proc/dma/channels and /proc/dma/devices */
  652. gDmaDir = proc_mkdir("dma", NULL);
  653. if (gDmaDir == NULL) {
  654. printk(KERN_ERR "Unable to create /proc/dma\n");
  655. } else {
  656. create_proc_read_entry("channels", 0, gDmaDir,
  657. dma_proc_read_channels, NULL);
  658. create_proc_read_entry("devices", 0, gDmaDir,
  659. dma_proc_read_devices, NULL);
  660. }
  661. out:
  662. up(&gDMA.lock);
  663. return rc;
  664. }
  665. /****************************************************************************/
  666. /**
  667. * Reserves a channel for use with @a dev. If the device is setup to use
  668. * a shared channel, then this function will block until a free channel
  669. * becomes available.
  670. *
  671. * @return
  672. * >= 0 - A valid DMA Handle.
  673. * -EBUSY - Device is currently being used.
  674. * -ENODEV - Device handed in is invalid.
  675. */
  676. /****************************************************************************/
  677. #if (DMA_DEBUG_TRACK_RESERVATION)
  678. DMA_Handle_t dma_request_channel_dbg
  679. (DMA_Device_t dev, const char *fileName, int lineNum)
  680. #else
  681. DMA_Handle_t dma_request_channel(DMA_Device_t dev)
  682. #endif
  683. {
  684. DMA_Handle_t handle;
  685. DMA_DeviceAttribute_t *devAttr;
  686. DMA_Channel_t *channel;
  687. int controllerIdx;
  688. int controllerIdx2;
  689. int channelIdx;
  690. if (down_interruptible(&gDMA.lock) < 0) {
  691. return -ERESTARTSYS;
  692. }
  693. if ((dev < 0) || (dev >= DMA_NUM_DEVICE_ENTRIES)) {
  694. handle = -ENODEV;
  695. goto out;
  696. }
  697. devAttr = &DMA_gDeviceAttribute[dev];
  698. #if (DMA_DEBUG_TRACK_RESERVATION)
  699. {
  700. char *s;
  701. s = strrchr(fileName, '/');
  702. if (s != NULL) {
  703. fileName = s + 1;
  704. }
  705. }
  706. #endif
  707. if ((devAttr->flags & DMA_DEVICE_FLAG_IN_USE) != 0) {
  708. /* This device has already been requested and not been freed */
  709. printk(KERN_ERR "%s: device %s is already requested\n",
  710. __func__, devAttr->name);
  711. handle = -EBUSY;
  712. goto out;
  713. }
  714. if ((devAttr->flags & DMA_DEVICE_FLAG_IS_DEDICATED) != 0) {
  715. /* This device has a dedicated channel. */
  716. channel =
  717. &gDMA.controller[devAttr->dedicatedController].
  718. channel[devAttr->dedicatedChannel];
  719. if ((channel->flags & DMA_CHANNEL_FLAG_IN_USE) != 0) {
  720. handle = -EBUSY;
  721. goto out;
  722. }
  723. channel->flags |= DMA_CHANNEL_FLAG_IN_USE;
  724. devAttr->flags |= DMA_DEVICE_FLAG_IN_USE;
  725. #if (DMA_DEBUG_TRACK_RESERVATION)
  726. channel->fileName = fileName;
  727. channel->lineNum = lineNum;
  728. #endif
  729. handle =
  730. MAKE_HANDLE(devAttr->dedicatedController,
  731. devAttr->dedicatedChannel);
  732. goto out;
  733. }
  734. /* This device needs to use one of the shared channels. */
  735. handle = DMA_INVALID_HANDLE;
  736. while (handle == DMA_INVALID_HANDLE) {
  737. /* Scan through the shared channels and see if one is available */
  738. for (controllerIdx2 = 0; controllerIdx2 < DMA_NUM_CONTROLLERS;
  739. controllerIdx2++) {
  740. /* Check to see if we should try on controller 1 first. */
  741. controllerIdx = controllerIdx2;
  742. if ((devAttr->
  743. flags & DMA_DEVICE_FLAG_ALLOC_DMA1_FIRST) != 0) {
  744. controllerIdx = 1 - controllerIdx;
  745. }
  746. /* See if the device is available on the controller being tested */
  747. if ((devAttr->
  748. flags & (DMA_DEVICE_FLAG_ON_DMA0 << controllerIdx))
  749. != 0) {
  750. for (channelIdx = 0;
  751. channelIdx < DMA_NUM_CHANNELS;
  752. channelIdx++) {
  753. channel =
  754. &gDMA.controller[controllerIdx].
  755. channel[channelIdx];
  756. if (((channel->
  757. flags &
  758. DMA_CHANNEL_FLAG_IS_DEDICATED) ==
  759. 0)
  760. &&
  761. ((channel->
  762. flags & DMA_CHANNEL_FLAG_IN_USE)
  763. == 0)) {
  764. if (((channel->
  765. flags &
  766. DMA_CHANNEL_FLAG_LARGE_FIFO)
  767. != 0)
  768. &&
  769. ((devAttr->
  770. flags &
  771. DMA_DEVICE_FLAG_ALLOW_LARGE_FIFO)
  772. == 0)) {
  773. /* This channel is a large fifo - don't tie it up */
  774. /* with devices that we don't want using it. */
  775. continue;
  776. }
  777. channel->flags |=
  778. DMA_CHANNEL_FLAG_IN_USE;
  779. channel->devType = dev;
  780. devAttr->flags |=
  781. DMA_DEVICE_FLAG_IN_USE;
  782. #if (DMA_DEBUG_TRACK_RESERVATION)
  783. channel->fileName = fileName;
  784. channel->lineNum = lineNum;
  785. #endif
  786. handle =
  787. MAKE_HANDLE(controllerIdx,
  788. channelIdx);
  789. /* Now that we've reserved the channel - we can go ahead and configure it */
  790. if (ConfigChannel(handle) != 0) {
  791. handle = -EIO;
  792. printk(KERN_ERR
  793. "dma_request_channel: ConfigChannel failed\n");
  794. }
  795. goto out;
  796. }
  797. }
  798. }
  799. }
  800. /* No channels are currently available. Let's wait for one to free up. */
  801. {
  802. DEFINE_WAIT(wait);
  803. prepare_to_wait(&gDMA.freeChannelQ, &wait,
  804. TASK_INTERRUPTIBLE);
  805. up(&gDMA.lock);
  806. schedule();
  807. finish_wait(&gDMA.freeChannelQ, &wait);
  808. if (signal_pending(current)) {
  809. /* We don't currently hold gDMA.lock, so we return directly */
  810. return -ERESTARTSYS;
  811. }
  812. }
  813. if (down_interruptible(&gDMA.lock)) {
  814. return -ERESTARTSYS;
  815. }
  816. }
  817. out:
  818. up(&gDMA.lock);
  819. return handle;
  820. }
  821. /* Create both _dbg and non _dbg functions for modules. */
  822. #if (DMA_DEBUG_TRACK_RESERVATION)
  823. #undef dma_request_channel
  824. DMA_Handle_t dma_request_channel(DMA_Device_t dev)
  825. {
  826. return dma_request_channel_dbg(dev, __FILE__, __LINE__);
  827. }
  828. EXPORT_SYMBOL(dma_request_channel_dbg);
  829. #endif
  830. EXPORT_SYMBOL(dma_request_channel);
  831. /****************************************************************************/
  832. /**
  833. * Frees a previously allocated DMA Handle.
  834. */
  835. /****************************************************************************/
  836. int dma_free_channel(DMA_Handle_t handle /* DMA handle. */
  837. ) {
  838. int rc = 0;
  839. DMA_Channel_t *channel;
  840. DMA_DeviceAttribute_t *devAttr;
  841. if (down_interruptible(&gDMA.lock) < 0) {
  842. return -ERESTARTSYS;
  843. }
  844. channel = HandleToChannel(handle);
  845. if (channel == NULL) {
  846. rc = -EINVAL;
  847. goto out;
  848. }
  849. devAttr = &DMA_gDeviceAttribute[channel->devType];
  850. if ((channel->flags & DMA_CHANNEL_FLAG_IS_DEDICATED) == 0) {
  851. channel->lastDevType = channel->devType;
  852. channel->devType = DMA_DEVICE_NONE;
  853. }
  854. channel->flags &= ~DMA_CHANNEL_FLAG_IN_USE;
  855. devAttr->flags &= ~DMA_DEVICE_FLAG_IN_USE;
  856. out:
  857. up(&gDMA.lock);
  858. wake_up_interruptible(&gDMA.freeChannelQ);
  859. return rc;
  860. }
  861. EXPORT_SYMBOL(dma_free_channel);
  862. /****************************************************************************/
  863. /**
  864. * Determines if a given device has been configured as using a shared
  865. * channel.
  866. *
  867. * @return
  868. * 0 Device uses a dedicated channel
  869. * > zero Device uses a shared channel
  870. * < zero Error code
  871. */
  872. /****************************************************************************/
  873. int dma_device_is_channel_shared(DMA_Device_t device /* Device to check. */
  874. ) {
  875. DMA_DeviceAttribute_t *devAttr;
  876. if (!IsDeviceValid(device)) {
  877. return -ENODEV;
  878. }
  879. devAttr = &DMA_gDeviceAttribute[device];
  880. return ((devAttr->flags & DMA_DEVICE_FLAG_IS_DEDICATED) == 0);
  881. }
  882. EXPORT_SYMBOL(dma_device_is_channel_shared);
  883. /****************************************************************************/
  884. /**
  885. * Allocates buffers for the descriptors. This is normally done automatically
  886. * but needs to be done explicitly when initiating a dma from interrupt
  887. * context.
  888. *
  889. * @return
  890. * 0 Descriptors were allocated successfully
  891. * -EINVAL Invalid device type for this kind of transfer
  892. * (i.e. the device is _MEM_TO_DEV and not _DEV_TO_MEM)
  893. * -ENOMEM Memory exhausted
  894. */
  895. /****************************************************************************/
  896. int dma_alloc_descriptors(DMA_Handle_t handle, /* DMA Handle */
  897. dmacHw_TRANSFER_TYPE_e transferType, /* Type of transfer being performed */
  898. dma_addr_t srcData, /* Place to get data to write to device */
  899. dma_addr_t dstData, /* Pointer to device data address */
  900. size_t numBytes /* Number of bytes to transfer to the device */
  901. ) {
  902. DMA_Channel_t *channel;
  903. DMA_DeviceAttribute_t *devAttr;
  904. int numDescriptors;
  905. size_t ringBytesRequired;
  906. int rc = 0;
  907. channel = HandleToChannel(handle);
  908. if (channel == NULL) {
  909. return -ENODEV;
  910. }
  911. devAttr = &DMA_gDeviceAttribute[channel->devType];
  912. if (devAttr->config.transferType != transferType) {
  913. return -EINVAL;
  914. }
  915. /* Figure out how many descriptors we need. */
  916. /* printk("srcData: 0x%08x dstData: 0x%08x, numBytes: %d\n", */
  917. /* srcData, dstData, numBytes); */
  918. numDescriptors = dmacHw_calculateDescriptorCount(&devAttr->config,
  919. (void *)srcData,
  920. (void *)dstData,
  921. numBytes);
  922. if (numDescriptors < 0) {
  923. printk(KERN_ERR "%s: dmacHw_calculateDescriptorCount failed\n",
  924. __func__);
  925. return -EINVAL;
  926. }
  927. /* Check to see if we can reuse the existing descriptor ring, or if we need to allocate */
  928. /* a new one. */
  929. ringBytesRequired = dmacHw_descriptorLen(numDescriptors);
  930. /* printk("ringBytesRequired: %d\n", ringBytesRequired); */
  931. if (ringBytesRequired > devAttr->ring.bytesAllocated) {
  932. /* Make sure that this code path is never taken from interrupt context. */
  933. /* It's OK for an interrupt to initiate a DMA transfer, but the descriptor */
  934. /* allocation needs to have already been done. */
  935. might_sleep();
  936. /* Free the old descriptor ring and allocate a new one. */
  937. dma_free_descriptor_ring(&devAttr->ring);
  938. /* And allocate a new one. */
  939. rc =
  940. dma_alloc_descriptor_ring(&devAttr->ring,
  941. numDescriptors);
  942. if (rc < 0) {
  943. printk(KERN_ERR
  944. "%s: dma_alloc_descriptor_ring(%d) failed\n",
  945. __func__, numDescriptors);
  946. return rc;
  947. }
  948. /* Setup the descriptor for this transfer */
  949. if (dmacHw_initDescriptor(devAttr->ring.virtAddr,
  950. devAttr->ring.physAddr,
  951. devAttr->ring.bytesAllocated,
  952. numDescriptors) < 0) {
  953. printk(KERN_ERR "%s: dmacHw_initDescriptor failed\n",
  954. __func__);
  955. return -EINVAL;
  956. }
  957. } else {
  958. /* We've already got enough ring buffer allocated. All we need to do is reset */
  959. /* any control information, just in case the previous DMA was stopped. */
  960. dmacHw_resetDescriptorControl(devAttr->ring.virtAddr);
  961. }
  962. /* dma_alloc/free both set the prevSrc/DstData to 0. If they happen to be the same */
  963. /* as last time, then we don't need to call setDataDescriptor again. */
  964. if (dmacHw_setDataDescriptor(&devAttr->config,
  965. devAttr->ring.virtAddr,
  966. (void *)srcData,
  967. (void *)dstData, numBytes) < 0) {
  968. printk(KERN_ERR "%s: dmacHw_setDataDescriptor failed\n",
  969. __func__);
  970. return -EINVAL;
  971. }
  972. /* Remember the critical information for this transfer so that we can eliminate */
  973. /* another call to dma_alloc_descriptors if the caller reuses the same buffers */
  974. devAttr->prevSrcData = srcData;
  975. devAttr->prevDstData = dstData;
  976. devAttr->prevNumBytes = numBytes;
  977. return 0;
  978. }
  979. EXPORT_SYMBOL(dma_alloc_descriptors);
  980. /****************************************************************************/
  981. /**
  982. * Allocates and sets up descriptors for a double buffered circular buffer.
  983. *
  984. * This is primarily intended to be used for things like the ingress samples
  985. * from a microphone.
  986. *
  987. * @return
  988. * > 0 Number of descriptors actually allocated.
  989. * -EINVAL Invalid device type for this kind of transfer
  990. * (i.e. the device is _MEM_TO_DEV and not _DEV_TO_MEM)
  991. * -ENOMEM Memory exhausted
  992. */
  993. /****************************************************************************/
  994. int dma_alloc_double_dst_descriptors(DMA_Handle_t handle, /* DMA Handle */
  995. dma_addr_t srcData, /* Physical address of source data */
  996. dma_addr_t dstData1, /* Physical address of first destination buffer */
  997. dma_addr_t dstData2, /* Physical address of second destination buffer */
  998. size_t numBytes /* Number of bytes in each destination buffer */
  999. ) {
  1000. DMA_Channel_t *channel;
  1001. DMA_DeviceAttribute_t *devAttr;
  1002. int numDst1Descriptors;
  1003. int numDst2Descriptors;
  1004. int numDescriptors;
  1005. size_t ringBytesRequired;
  1006. int rc = 0;
  1007. channel = HandleToChannel(handle);
  1008. if (channel == NULL) {
  1009. return -ENODEV;
  1010. }
  1011. devAttr = &DMA_gDeviceAttribute[channel->devType];
  1012. /* Figure out how many descriptors we need. */
  1013. /* printk("srcData: 0x%08x dstData: 0x%08x, numBytes: %d\n", */
  1014. /* srcData, dstData, numBytes); */
  1015. numDst1Descriptors =
  1016. dmacHw_calculateDescriptorCount(&devAttr->config, (void *)srcData,
  1017. (void *)dstData1, numBytes);
  1018. if (numDst1Descriptors < 0) {
  1019. return -EINVAL;
  1020. }
  1021. numDst2Descriptors =
  1022. dmacHw_calculateDescriptorCount(&devAttr->config, (void *)srcData,
  1023. (void *)dstData2, numBytes);
  1024. if (numDst2Descriptors < 0) {
  1025. return -EINVAL;
  1026. }
  1027. numDescriptors = numDst1Descriptors + numDst2Descriptors;
  1028. /* printk("numDescriptors: %d\n", numDescriptors); */
  1029. /* Check to see if we can reuse the existing descriptor ring, or if we need to allocate */
  1030. /* a new one. */
  1031. ringBytesRequired = dmacHw_descriptorLen(numDescriptors);
  1032. /* printk("ringBytesRequired: %d\n", ringBytesRequired); */
  1033. if (ringBytesRequired > devAttr->ring.bytesAllocated) {
  1034. /* Make sure that this code path is never taken from interrupt context. */
  1035. /* It's OK for an interrupt to initiate a DMA transfer, but the descriptor */
  1036. /* allocation needs to have already been done. */
  1037. might_sleep();
  1038. /* Free the old descriptor ring and allocate a new one. */
  1039. dma_free_descriptor_ring(&devAttr->ring);
  1040. /* And allocate a new one. */
  1041. rc =
  1042. dma_alloc_descriptor_ring(&devAttr->ring,
  1043. numDescriptors);
  1044. if (rc < 0) {
  1045. printk(KERN_ERR
  1046. "%s: dma_alloc_descriptor_ring(%d) failed\n",
  1047. __func__, ringBytesRequired);
  1048. return rc;
  1049. }
  1050. }
  1051. /* Setup the descriptor for this transfer. Since this function is used with */
  1052. /* CONTINUOUS DMA operations, we need to reinitialize every time, otherwise */
  1053. /* setDataDescriptor will keep trying to append onto the end. */
  1054. if (dmacHw_initDescriptor(devAttr->ring.virtAddr,
  1055. devAttr->ring.physAddr,
  1056. devAttr->ring.bytesAllocated,
  1057. numDescriptors) < 0) {
  1058. printk(KERN_ERR "%s: dmacHw_initDescriptor failed\n", __func__);
  1059. return -EINVAL;
  1060. }
  1061. /* dma_alloc/free both set the prevSrc/DstData to 0. If they happen to be the same */
  1062. /* as last time, then we don't need to call setDataDescriptor again. */
  1063. if (dmacHw_setDataDescriptor(&devAttr->config,
  1064. devAttr->ring.virtAddr,
  1065. (void *)srcData,
  1066. (void *)dstData1, numBytes) < 0) {
  1067. printk(KERN_ERR "%s: dmacHw_setDataDescriptor 1 failed\n",
  1068. __func__);
  1069. return -EINVAL;
  1070. }
  1071. if (dmacHw_setDataDescriptor(&devAttr->config,
  1072. devAttr->ring.virtAddr,
  1073. (void *)srcData,
  1074. (void *)dstData2, numBytes) < 0) {
  1075. printk(KERN_ERR "%s: dmacHw_setDataDescriptor 2 failed\n",
  1076. __func__);
  1077. return -EINVAL;
  1078. }
  1079. /* You should use dma_start_transfer rather than dma_transfer_xxx so we don't */
  1080. /* try to make the 'prev' variables right. */
  1081. devAttr->prevSrcData = 0;
  1082. devAttr->prevDstData = 0;
  1083. devAttr->prevNumBytes = 0;
  1084. return numDescriptors;
  1085. }
  1086. EXPORT_SYMBOL(dma_alloc_double_dst_descriptors);
  1087. /****************************************************************************/
  1088. /**
  1089. * Initiates a transfer when the descriptors have already been setup.
  1090. *
  1091. * This is a special case, and normally, the dma_transfer_xxx functions should
  1092. * be used.
  1093. *
  1094. * @return
  1095. * 0 Transfer was started successfully
  1096. * -ENODEV Invalid handle
  1097. */
  1098. /****************************************************************************/
  1099. int dma_start_transfer(DMA_Handle_t handle)
  1100. {
  1101. DMA_Channel_t *channel;
  1102. DMA_DeviceAttribute_t *devAttr;
  1103. channel = HandleToChannel(handle);
  1104. if (channel == NULL) {
  1105. return -ENODEV;
  1106. }
  1107. devAttr = &DMA_gDeviceAttribute[channel->devType];
  1108. dmacHw_initiateTransfer(channel->dmacHwHandle, &devAttr->config,
  1109. devAttr->ring.virtAddr);
  1110. /* Since we got this far, everything went successfully */
  1111. return 0;
  1112. }
  1113. EXPORT_SYMBOL(dma_start_transfer);
  1114. /****************************************************************************/
  1115. /**
  1116. * Stops a previously started DMA transfer.
  1117. *
  1118. * @return
  1119. * 0 Transfer was stopped successfully
  1120. * -ENODEV Invalid handle
  1121. */
  1122. /****************************************************************************/
  1123. int dma_stop_transfer(DMA_Handle_t handle)
  1124. {
  1125. DMA_Channel_t *channel;
  1126. channel = HandleToChannel(handle);
  1127. if (channel == NULL) {
  1128. return -ENODEV;
  1129. }
  1130. dmacHw_stopTransfer(channel->dmacHwHandle);
  1131. return 0;
  1132. }
  1133. EXPORT_SYMBOL(dma_stop_transfer);
  1134. /****************************************************************************/
  1135. /**
  1136. * Waits for a DMA to complete by polling. This function is only intended
  1137. * to be used for testing. Interrupts should be used for most DMA operations.
  1138. */
  1139. /****************************************************************************/
  1140. int dma_wait_transfer_done(DMA_Handle_t handle)
  1141. {
  1142. DMA_Channel_t *channel;
  1143. dmacHw_TRANSFER_STATUS_e status;
  1144. channel = HandleToChannel(handle);
  1145. if (channel == NULL) {
  1146. return -ENODEV;
  1147. }
  1148. while ((status =
  1149. dmacHw_transferCompleted(channel->dmacHwHandle)) ==
  1150. dmacHw_TRANSFER_STATUS_BUSY) {
  1151. ;
  1152. }
  1153. if (status == dmacHw_TRANSFER_STATUS_ERROR) {
  1154. printk(KERN_ERR "%s: DMA transfer failed\n", __func__);
  1155. return -EIO;
  1156. }
  1157. return 0;
  1158. }
  1159. EXPORT_SYMBOL(dma_wait_transfer_done);
  1160. /****************************************************************************/
  1161. /**
  1162. * Initiates a DMA, allocating the descriptors as required.
  1163. *
  1164. * @return
  1165. * 0 Transfer was started successfully
  1166. * -EINVAL Invalid device type for this kind of transfer
  1167. * (i.e. the device is _DEV_TO_MEM and not _MEM_TO_DEV)
  1168. */
  1169. /****************************************************************************/
  1170. int dma_transfer(DMA_Handle_t handle, /* DMA Handle */
  1171. dmacHw_TRANSFER_TYPE_e transferType, /* Type of transfer being performed */
  1172. dma_addr_t srcData, /* Place to get data to write to device */
  1173. dma_addr_t dstData, /* Pointer to device data address */
  1174. size_t numBytes /* Number of bytes to transfer to the device */
  1175. ) {
  1176. DMA_Channel_t *channel;
  1177. DMA_DeviceAttribute_t *devAttr;
  1178. int rc = 0;
  1179. channel = HandleToChannel(handle);
  1180. if (channel == NULL) {
  1181. return -ENODEV;
  1182. }
  1183. devAttr = &DMA_gDeviceAttribute[channel->devType];
  1184. if (devAttr->config.transferType != transferType) {
  1185. return -EINVAL;
  1186. }
  1187. /* We keep track of the information about the previous request for this */
  1188. /* device, and if the attributes match, then we can use the descriptors we setup */
  1189. /* the last time, and not have to reinitialize everything. */
  1190. {
  1191. rc =
  1192. dma_alloc_descriptors(handle, transferType, srcData,
  1193. dstData, numBytes);
  1194. if (rc != 0) {
  1195. return rc;
  1196. }
  1197. }
  1198. /* And kick off the transfer */
  1199. devAttr->numBytes = numBytes;
  1200. devAttr->transferStartTime = timer_get_tick_count();
  1201. dmacHw_initiateTransfer(channel->dmacHwHandle, &devAttr->config,
  1202. devAttr->ring.virtAddr);
  1203. /* Since we got this far, everything went successfully */
  1204. return 0;
  1205. }
  1206. EXPORT_SYMBOL(dma_transfer);
  1207. /****************************************************************************/
  1208. /**
  1209. * Set the callback function which will be called when a transfer completes.
  1210. * If a NULL callback function is set, then no callback will occur.
  1211. *
  1212. * @note @a devHandler will be called from IRQ context.
  1213. *
  1214. * @return
  1215. * 0 - Success
  1216. * -ENODEV - Device handed in is invalid.
  1217. */
  1218. /****************************************************************************/
  1219. int dma_set_device_handler(DMA_Device_t dev, /* Device to set the callback for. */
  1220. DMA_DeviceHandler_t devHandler, /* Function to call when the DMA completes */
  1221. void *userData /* Pointer which will be passed to devHandler. */
  1222. ) {
  1223. DMA_DeviceAttribute_t *devAttr;
  1224. unsigned long flags;
  1225. if (!IsDeviceValid(dev)) {
  1226. return -ENODEV;
  1227. }
  1228. devAttr = &DMA_gDeviceAttribute[dev];
  1229. local_irq_save(flags);
  1230. devAttr->userData = userData;
  1231. devAttr->devHandler = devHandler;
  1232. local_irq_restore(flags);
  1233. return 0;
  1234. }
  1235. EXPORT_SYMBOL(dma_set_device_handler);