Use dentry_path() to create full path to inode object
[pohmelfs.git] / arch / arm / mach-bcmring / dma.c
blob1024396797e16f6c839383da409731bfaef494d3
1 /*****************************************************************************
2 * Copyright 2004 - 2008 Broadcom Corporation. All rights reserved.
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").
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 *****************************************************************************/
15 /****************************************************************************/
16 /**
17 * @file dma.c
19 * @brief Implements the DMA interface.
21 /****************************************************************************/
23 /* ---- Include Files ---------------------------------------------------- */
25 #include <linux/module.h>
26 #include <linux/device.h>
27 #include <linux/dma-mapping.h>
28 #include <linux/interrupt.h>
29 #include <linux/sched.h>
30 #include <linux/irqreturn.h>
31 #include <linux/proc_fs.h>
32 #include <linux/slab.h>
34 #include <mach/timer.h>
36 #include <linux/pfn.h>
37 #include <linux/atomic.h>
38 #include <linux/sched.h>
39 #include <mach/dma.h>
41 /* ---- Public Variables ------------------------------------------------- */
43 /* ---- Private Constants and Types -------------------------------------- */
45 #define MAKE_HANDLE(controllerIdx, channelIdx) (((controllerIdx) << 4) | (channelIdx))
47 #define CONTROLLER_FROM_HANDLE(handle) (((handle) >> 4) & 0x0f)
48 #define CHANNEL_FROM_HANDLE(handle) ((handle) & 0x0f)
51 /* ---- Private Variables ------------------------------------------------ */
53 static DMA_Global_t gDMA;
54 static struct proc_dir_entry *gDmaDir;
56 #include "dma_device.c"
58 /* ---- Private Function Prototypes -------------------------------------- */
60 /* ---- Functions ------------------------------------------------------- */
62 /****************************************************************************/
63 /**
64 * Displays information for /proc/dma/channels
66 /****************************************************************************/
68 static int dma_proc_read_channels(char *buf, char **start, off_t offset,
69 int count, int *eof, void *data)
71 int controllerIdx;
72 int channelIdx;
73 int limit = count - 200;
74 int len = 0;
75 DMA_Channel_t *channel;
77 if (down_interruptible(&gDMA.lock) < 0) {
78 return -ERESTARTSYS;
81 for (controllerIdx = 0; controllerIdx < DMA_NUM_CONTROLLERS;
82 controllerIdx++) {
83 for (channelIdx = 0; channelIdx < DMA_NUM_CHANNELS;
84 channelIdx++) {
85 if (len >= limit) {
86 break;
89 channel =
90 &gDMA.controller[controllerIdx].channel[channelIdx];
92 len +=
93 sprintf(buf + len, "%d:%d ", controllerIdx,
94 channelIdx);
96 if ((channel->flags & DMA_CHANNEL_FLAG_IS_DEDICATED) !=
97 0) {
98 len +=
99 sprintf(buf + len, "Dedicated for %s ",
100 DMA_gDeviceAttribute[channel->
101 devType].name);
102 } else {
103 len += sprintf(buf + len, "Shared ");
106 if ((channel->flags & DMA_CHANNEL_FLAG_NO_ISR) != 0) {
107 len += sprintf(buf + len, "No ISR ");
110 if ((channel->flags & DMA_CHANNEL_FLAG_LARGE_FIFO) != 0) {
111 len += sprintf(buf + len, "Fifo: 128 ");
112 } else {
113 len += sprintf(buf + len, "Fifo: 64 ");
116 if ((channel->flags & DMA_CHANNEL_FLAG_IN_USE) != 0) {
117 len +=
118 sprintf(buf + len, "InUse by %s",
119 DMA_gDeviceAttribute[channel->
120 devType].name);
121 #if (DMA_DEBUG_TRACK_RESERVATION)
122 len +=
123 sprintf(buf + len, " (%s:%d)",
124 channel->fileName,
125 channel->lineNum);
126 #endif
127 } else {
128 len += sprintf(buf + len, "Avail ");
131 if (channel->lastDevType != DMA_DEVICE_NONE) {
132 len +=
133 sprintf(buf + len, "Last use: %s ",
134 DMA_gDeviceAttribute[channel->
135 lastDevType].
136 name);
139 len += sprintf(buf + len, "\n");
142 up(&gDMA.lock);
143 *eof = 1;
145 return len;
148 /****************************************************************************/
150 * Displays information for /proc/dma/devices
152 /****************************************************************************/
154 static int dma_proc_read_devices(char *buf, char **start, off_t offset,
155 int count, int *eof, void *data)
157 int limit = count - 200;
158 int len = 0;
159 int devIdx;
161 if (down_interruptible(&gDMA.lock) < 0) {
162 return -ERESTARTSYS;
165 for (devIdx = 0; devIdx < DMA_NUM_DEVICE_ENTRIES; devIdx++) {
166 DMA_DeviceAttribute_t *devAttr = &DMA_gDeviceAttribute[devIdx];
168 if (devAttr->name == NULL) {
169 continue;
172 if (len >= limit) {
173 break;
176 len += sprintf(buf + len, "%-12s ", devAttr->name);
178 if ((devAttr->flags & DMA_DEVICE_FLAG_IS_DEDICATED) != 0) {
179 len +=
180 sprintf(buf + len, "Dedicated %d:%d ",
181 devAttr->dedicatedController,
182 devAttr->dedicatedChannel);
183 } else {
184 len += sprintf(buf + len, "Shared DMA:");
185 if ((devAttr->flags & DMA_DEVICE_FLAG_ON_DMA0) != 0) {
186 len += sprintf(buf + len, "0");
188 if ((devAttr->flags & DMA_DEVICE_FLAG_ON_DMA1) != 0) {
189 len += sprintf(buf + len, "1");
191 len += sprintf(buf + len, " ");
193 if ((devAttr->flags & DMA_DEVICE_FLAG_NO_ISR) != 0) {
194 len += sprintf(buf + len, "NoISR ");
196 if ((devAttr->flags & DMA_DEVICE_FLAG_ALLOW_LARGE_FIFO) != 0) {
197 len += sprintf(buf + len, "Allow-128 ");
200 len +=
201 sprintf(buf + len,
202 "Xfer #: %Lu Ticks: %Lu Bytes: %Lu DescLen: %u\n",
203 devAttr->numTransfers, devAttr->transferTicks,
204 devAttr->transferBytes,
205 devAttr->ring.bytesAllocated);
209 up(&gDMA.lock);
210 *eof = 1;
212 return len;
215 /****************************************************************************/
217 * Determines if a DMA_Device_t is "valid".
219 * @return
220 * TRUE - dma device is valid
221 * FALSE - dma device isn't valid
223 /****************************************************************************/
225 static inline int IsDeviceValid(DMA_Device_t device)
227 return (device >= 0) && (device < DMA_NUM_DEVICE_ENTRIES);
230 /****************************************************************************/
232 * Translates a DMA handle into a pointer to a channel.
234 * @return
235 * non-NULL - pointer to DMA_Channel_t
236 * NULL - DMA Handle was invalid
238 /****************************************************************************/
240 static inline DMA_Channel_t *HandleToChannel(DMA_Handle_t handle)
242 int controllerIdx;
243 int channelIdx;
245 controllerIdx = CONTROLLER_FROM_HANDLE(handle);
246 channelIdx = CHANNEL_FROM_HANDLE(handle);
248 if ((controllerIdx > DMA_NUM_CONTROLLERS)
249 || (channelIdx > DMA_NUM_CHANNELS)) {
250 return NULL;
252 return &gDMA.controller[controllerIdx].channel[channelIdx];
255 /****************************************************************************/
257 * Interrupt handler which is called to process DMA interrupts.
259 /****************************************************************************/
261 static irqreturn_t dma_interrupt_handler(int irq, void *dev_id)
263 DMA_Channel_t *channel;
264 DMA_DeviceAttribute_t *devAttr;
265 int irqStatus;
267 channel = (DMA_Channel_t *) dev_id;
269 /* Figure out why we were called, and knock down the interrupt */
271 irqStatus = dmacHw_getInterruptStatus(channel->dmacHwHandle);
272 dmacHw_clearInterrupt(channel->dmacHwHandle);
274 if ((channel->devType < 0)
275 || (channel->devType > DMA_NUM_DEVICE_ENTRIES)) {
276 printk(KERN_ERR "dma_interrupt_handler: Invalid devType: %d\n",
277 channel->devType);
278 return IRQ_NONE;
280 devAttr = &DMA_gDeviceAttribute[channel->devType];
282 /* Update stats */
284 if ((irqStatus & dmacHw_INTERRUPT_STATUS_TRANS) != 0) {
285 devAttr->transferTicks +=
286 (timer_get_tick_count() - devAttr->transferStartTime);
289 if ((irqStatus & dmacHw_INTERRUPT_STATUS_ERROR) != 0) {
290 printk(KERN_ERR
291 "dma_interrupt_handler: devType :%d DMA error (%s)\n",
292 channel->devType, devAttr->name);
293 } else {
294 devAttr->numTransfers++;
295 devAttr->transferBytes += devAttr->numBytes;
298 /* Call any installed handler */
300 if (devAttr->devHandler != NULL) {
301 devAttr->devHandler(channel->devType, irqStatus,
302 devAttr->userData);
305 return IRQ_HANDLED;
308 /****************************************************************************/
310 * Allocates memory to hold a descriptor ring. The descriptor ring then
311 * needs to be populated by making one or more calls to
312 * dna_add_descriptors.
314 * The returned descriptor ring will be automatically initialized.
316 * @return
317 * 0 Descriptor ring was allocated successfully
318 * -EINVAL Invalid parameters passed in
319 * -ENOMEM Unable to allocate memory for the desired number of descriptors.
321 /****************************************************************************/
323 int dma_alloc_descriptor_ring(DMA_DescriptorRing_t *ring, /* Descriptor ring to populate */
324 int numDescriptors /* Number of descriptors that need to be allocated. */
326 size_t bytesToAlloc = dmacHw_descriptorLen(numDescriptors);
328 if ((ring == NULL) || (numDescriptors <= 0)) {
329 return -EINVAL;
332 ring->physAddr = 0;
333 ring->descriptorsAllocated = 0;
334 ring->bytesAllocated = 0;
336 ring->virtAddr = dma_alloc_writecombine(NULL,
337 bytesToAlloc,
338 &ring->physAddr,
339 GFP_KERNEL);
340 if (ring->virtAddr == NULL) {
341 return -ENOMEM;
344 ring->bytesAllocated = bytesToAlloc;
345 ring->descriptorsAllocated = numDescriptors;
347 return dma_init_descriptor_ring(ring, numDescriptors);
350 EXPORT_SYMBOL(dma_alloc_descriptor_ring);
352 /****************************************************************************/
354 * Releases the memory which was previously allocated for a descriptor ring.
356 /****************************************************************************/
358 void dma_free_descriptor_ring(DMA_DescriptorRing_t *ring /* Descriptor to release */
360 if (ring->virtAddr != NULL) {
361 dma_free_writecombine(NULL,
362 ring->bytesAllocated,
363 ring->virtAddr, ring->physAddr);
366 ring->bytesAllocated = 0;
367 ring->descriptorsAllocated = 0;
368 ring->virtAddr = NULL;
369 ring->physAddr = 0;
372 EXPORT_SYMBOL(dma_free_descriptor_ring);
374 /****************************************************************************/
376 * Initializes a descriptor ring, so that descriptors can be added to it.
377 * Once a descriptor ring has been allocated, it may be reinitialized for
378 * use with additional/different regions of memory.
380 * Note that if 7 descriptors are allocated, it's perfectly acceptable to
381 * initialize the ring with a smaller number of descriptors. The amount
382 * of memory allocated for the descriptor ring will not be reduced, and
383 * the descriptor ring may be reinitialized later
385 * @return
386 * 0 Descriptor ring was initialized successfully
387 * -ENOMEM The descriptor which was passed in has insufficient space
388 * to hold the desired number of descriptors.
390 /****************************************************************************/
392 int dma_init_descriptor_ring(DMA_DescriptorRing_t *ring, /* Descriptor ring to initialize */
393 int numDescriptors /* Number of descriptors to initialize. */
395 if (ring->virtAddr == NULL) {
396 return -EINVAL;
398 if (dmacHw_initDescriptor(ring->virtAddr,
399 ring->physAddr,
400 ring->bytesAllocated, numDescriptors) < 0) {
401 printk(KERN_ERR
402 "dma_init_descriptor_ring: dmacHw_initDescriptor failed\n");
403 return -ENOMEM;
406 return 0;
409 EXPORT_SYMBOL(dma_init_descriptor_ring);
411 /****************************************************************************/
413 * Determines the number of descriptors which would be required for a
414 * transfer of the indicated memory region.
416 * This function also needs to know which DMA device this transfer will
417 * be destined for, so that the appropriate DMA configuration can be retrieved.
418 * DMA parameters such as transfer width, and whether this is a memory-to-memory
419 * or memory-to-peripheral, etc can all affect the actual number of descriptors
420 * required.
422 * @return
423 * > 0 Returns the number of descriptors required for the indicated transfer
424 * -ENODEV - Device handed in is invalid.
425 * -EINVAL Invalid parameters
426 * -ENOMEM Memory exhausted
428 /****************************************************************************/
430 int dma_calculate_descriptor_count(DMA_Device_t device, /* DMA Device that this will be associated with */
431 dma_addr_t srcData, /* Place to get data to write to device */
432 dma_addr_t dstData, /* Pointer to device data address */
433 size_t numBytes /* Number of bytes to transfer to the device */
435 int numDescriptors;
436 DMA_DeviceAttribute_t *devAttr;
438 if (!IsDeviceValid(device)) {
439 return -ENODEV;
441 devAttr = &DMA_gDeviceAttribute[device];
443 numDescriptors = dmacHw_calculateDescriptorCount(&devAttr->config,
444 (void *)srcData,
445 (void *)dstData,
446 numBytes);
447 if (numDescriptors < 0) {
448 printk(KERN_ERR
449 "dma_calculate_descriptor_count: dmacHw_calculateDescriptorCount failed\n");
450 return -EINVAL;
453 return numDescriptors;
456 EXPORT_SYMBOL(dma_calculate_descriptor_count);
458 /****************************************************************************/
460 * Adds a region of memory to the descriptor ring. Note that it may take
461 * multiple descriptors for each region of memory. It is the callers
462 * responsibility to allocate a sufficiently large descriptor ring.
464 * @return
465 * 0 Descriptors were added successfully
466 * -ENODEV Device handed in is invalid.
467 * -EINVAL Invalid parameters
468 * -ENOMEM Memory exhausted
470 /****************************************************************************/
472 int dma_add_descriptors(DMA_DescriptorRing_t *ring, /* Descriptor ring to add descriptors to */
473 DMA_Device_t device, /* DMA Device that descriptors are for */
474 dma_addr_t srcData, /* Place to get data (memory or device) */
475 dma_addr_t dstData, /* Place to put data (memory or device) */
476 size_t numBytes /* Number of bytes to transfer to the device */
478 int rc;
479 DMA_DeviceAttribute_t *devAttr;
481 if (!IsDeviceValid(device)) {
482 return -ENODEV;
484 devAttr = &DMA_gDeviceAttribute[device];
486 rc = dmacHw_setDataDescriptor(&devAttr->config,
487 ring->virtAddr,
488 (void *)srcData,
489 (void *)dstData, numBytes);
490 if (rc < 0) {
491 printk(KERN_ERR
492 "dma_add_descriptors: dmacHw_setDataDescriptor failed with code: %d\n",
493 rc);
494 return -ENOMEM;
497 return 0;
500 EXPORT_SYMBOL(dma_add_descriptors);
502 /****************************************************************************/
504 * Sets the descriptor ring associated with a device.
506 * Once set, the descriptor ring will be associated with the device, even
507 * across channel request/free calls. Passing in a NULL descriptor ring
508 * will release any descriptor ring currently associated with the device.
510 * Note: If you call dma_transfer, or one of the other dma_alloc_ functions
511 * the descriptor ring may be released and reallocated.
513 * Note: This function will release the descriptor memory for any current
514 * descriptor ring associated with this device.
516 * @return
517 * 0 Descriptors were added successfully
518 * -ENODEV Device handed in is invalid.
520 /****************************************************************************/
522 int dma_set_device_descriptor_ring(DMA_Device_t device, /* Device to update the descriptor ring for. */
523 DMA_DescriptorRing_t *ring /* Descriptor ring to add descriptors to */
525 DMA_DeviceAttribute_t *devAttr;
527 if (!IsDeviceValid(device)) {
528 return -ENODEV;
530 devAttr = &DMA_gDeviceAttribute[device];
532 /* Free the previously allocated descriptor ring */
534 dma_free_descriptor_ring(&devAttr->ring);
536 if (ring != NULL) {
537 /* Copy in the new one */
539 devAttr->ring = *ring;
542 /* Set things up so that if dma_transfer is called then this descriptor */
543 /* ring will get freed. */
545 devAttr->prevSrcData = 0;
546 devAttr->prevDstData = 0;
547 devAttr->prevNumBytes = 0;
549 return 0;
552 EXPORT_SYMBOL(dma_set_device_descriptor_ring);
554 /****************************************************************************/
556 * Retrieves the descriptor ring associated with a device.
558 * @return
559 * 0 Descriptors were added successfully
560 * -ENODEV Device handed in is invalid.
562 /****************************************************************************/
564 int dma_get_device_descriptor_ring(DMA_Device_t device, /* Device to retrieve the descriptor ring for. */
565 DMA_DescriptorRing_t *ring /* Place to store retrieved ring */
567 DMA_DeviceAttribute_t *devAttr;
569 memset(ring, 0, sizeof(*ring));
571 if (!IsDeviceValid(device)) {
572 return -ENODEV;
574 devAttr = &DMA_gDeviceAttribute[device];
576 *ring = devAttr->ring;
578 return 0;
581 EXPORT_SYMBOL(dma_get_device_descriptor_ring);
583 /****************************************************************************/
585 * Configures a DMA channel.
587 * @return
588 * >= 0 - Initialization was successful.
590 * -EBUSY - Device is currently being used.
591 * -ENODEV - Device handed in is invalid.
593 /****************************************************************************/
595 static int ConfigChannel(DMA_Handle_t handle)
597 DMA_Channel_t *channel;
598 DMA_DeviceAttribute_t *devAttr;
599 int controllerIdx;
601 channel = HandleToChannel(handle);
602 if (channel == NULL) {
603 return -ENODEV;
605 devAttr = &DMA_gDeviceAttribute[channel->devType];
606 controllerIdx = CONTROLLER_FROM_HANDLE(handle);
608 if ((devAttr->flags & DMA_DEVICE_FLAG_PORT_PER_DMAC) != 0) {
609 if (devAttr->config.transferType ==
610 dmacHw_TRANSFER_TYPE_MEM_TO_PERIPHERAL) {
611 devAttr->config.dstPeripheralPort =
612 devAttr->dmacPort[controllerIdx];
613 } else if (devAttr->config.transferType ==
614 dmacHw_TRANSFER_TYPE_PERIPHERAL_TO_MEM) {
615 devAttr->config.srcPeripheralPort =
616 devAttr->dmacPort[controllerIdx];
620 if (dmacHw_configChannel(channel->dmacHwHandle, &devAttr->config) != 0) {
621 printk(KERN_ERR "ConfigChannel: dmacHw_configChannel failed\n");
622 return -EIO;
625 return 0;
628 /****************************************************************************/
630 * Initializes all of the data structures associated with the DMA.
631 * @return
632 * >= 0 - Initialization was successful.
634 * -EBUSY - Device is currently being used.
635 * -ENODEV - Device handed in is invalid.
637 /****************************************************************************/
639 int dma_init(void)
641 int rc = 0;
642 int controllerIdx;
643 int channelIdx;
644 DMA_Device_t devIdx;
645 DMA_Channel_t *channel;
646 DMA_Handle_t dedicatedHandle;
648 memset(&gDMA, 0, sizeof(gDMA));
650 sema_init(&gDMA.lock, 0);
651 init_waitqueue_head(&gDMA.freeChannelQ);
653 /* Initialize the Hardware */
655 dmacHw_initDma();
657 /* Start off by marking all of the DMA channels as shared. */
659 for (controllerIdx = 0; controllerIdx < DMA_NUM_CONTROLLERS;
660 controllerIdx++) {
661 for (channelIdx = 0; channelIdx < DMA_NUM_CHANNELS;
662 channelIdx++) {
663 channel =
664 &gDMA.controller[controllerIdx].channel[channelIdx];
666 channel->flags = 0;
667 channel->devType = DMA_DEVICE_NONE;
668 channel->lastDevType = DMA_DEVICE_NONE;
670 #if (DMA_DEBUG_TRACK_RESERVATION)
671 channel->fileName = "";
672 channel->lineNum = 0;
673 #endif
675 channel->dmacHwHandle =
676 dmacHw_getChannelHandle(dmacHw_MAKE_CHANNEL_ID
677 (controllerIdx,
678 channelIdx));
679 dmacHw_initChannel(channel->dmacHwHandle);
683 /* Record any special attributes that channels may have */
685 gDMA.controller[0].channel[0].flags |= DMA_CHANNEL_FLAG_LARGE_FIFO;
686 gDMA.controller[0].channel[1].flags |= DMA_CHANNEL_FLAG_LARGE_FIFO;
687 gDMA.controller[1].channel[0].flags |= DMA_CHANNEL_FLAG_LARGE_FIFO;
688 gDMA.controller[1].channel[1].flags |= DMA_CHANNEL_FLAG_LARGE_FIFO;
690 /* Now walk through and record the dedicated channels. */
692 for (devIdx = 0; devIdx < DMA_NUM_DEVICE_ENTRIES; devIdx++) {
693 DMA_DeviceAttribute_t *devAttr = &DMA_gDeviceAttribute[devIdx];
695 if (((devAttr->flags & DMA_DEVICE_FLAG_NO_ISR) != 0)
696 && ((devAttr->flags & DMA_DEVICE_FLAG_IS_DEDICATED) == 0)) {
697 printk(KERN_ERR
698 "DMA Device: %s Can only request NO_ISR for dedicated devices\n",
699 devAttr->name);
700 rc = -EINVAL;
701 goto out;
704 if ((devAttr->flags & DMA_DEVICE_FLAG_IS_DEDICATED) != 0) {
705 /* This is a dedicated device. Mark the channel as being reserved. */
707 if (devAttr->dedicatedController >= DMA_NUM_CONTROLLERS) {
708 printk(KERN_ERR
709 "DMA Device: %s DMA Controller %d is out of range\n",
710 devAttr->name,
711 devAttr->dedicatedController);
712 rc = -EINVAL;
713 goto out;
716 if (devAttr->dedicatedChannel >= DMA_NUM_CHANNELS) {
717 printk(KERN_ERR
718 "DMA Device: %s DMA Channel %d is out of range\n",
719 devAttr->name,
720 devAttr->dedicatedChannel);
721 rc = -EINVAL;
722 goto out;
725 dedicatedHandle =
726 MAKE_HANDLE(devAttr->dedicatedController,
727 devAttr->dedicatedChannel);
728 channel = HandleToChannel(dedicatedHandle);
730 if ((channel->flags & DMA_CHANNEL_FLAG_IS_DEDICATED) !=
731 0) {
732 printk
733 ("DMA Device: %s attempting to use same DMA Controller:Channel (%d:%d) as %s\n",
734 devAttr->name,
735 devAttr->dedicatedController,
736 devAttr->dedicatedChannel,
737 DMA_gDeviceAttribute[channel->devType].
738 name);
739 rc = -EBUSY;
740 goto out;
743 channel->flags |= DMA_CHANNEL_FLAG_IS_DEDICATED;
744 channel->devType = devIdx;
746 if (devAttr->flags & DMA_DEVICE_FLAG_NO_ISR) {
747 channel->flags |= DMA_CHANNEL_FLAG_NO_ISR;
750 /* For dedicated channels, we can go ahead and configure the DMA channel now */
751 /* as well. */
753 ConfigChannel(dedicatedHandle);
757 /* Go through and register the interrupt handlers */
759 for (controllerIdx = 0; controllerIdx < DMA_NUM_CONTROLLERS;
760 controllerIdx++) {
761 for (channelIdx = 0; channelIdx < DMA_NUM_CHANNELS;
762 channelIdx++) {
763 channel =
764 &gDMA.controller[controllerIdx].channel[channelIdx];
766 if ((channel->flags & DMA_CHANNEL_FLAG_NO_ISR) == 0) {
767 snprintf(channel->name, sizeof(channel->name),
768 "dma %d:%d %s", controllerIdx,
769 channelIdx,
770 channel->devType ==
771 DMA_DEVICE_NONE ? "" :
772 DMA_gDeviceAttribute[channel->devType].
773 name);
775 rc =
776 request_irq(IRQ_DMA0C0 +
777 (controllerIdx *
778 DMA_NUM_CHANNELS) +
779 channelIdx,
780 dma_interrupt_handler,
781 IRQF_DISABLED, channel->name,
782 channel);
783 if (rc != 0) {
784 printk(KERN_ERR
785 "request_irq for IRQ_DMA%dC%d failed\n",
786 controllerIdx, channelIdx);
792 /* Create /proc/dma/channels and /proc/dma/devices */
794 gDmaDir = proc_mkdir("dma", NULL);
796 if (gDmaDir == NULL) {
797 printk(KERN_ERR "Unable to create /proc/dma\n");
798 } else {
799 create_proc_read_entry("channels", 0, gDmaDir,
800 dma_proc_read_channels, NULL);
801 create_proc_read_entry("devices", 0, gDmaDir,
802 dma_proc_read_devices, NULL);
805 out:
807 up(&gDMA.lock);
809 return rc;
812 /****************************************************************************/
814 * Reserves a channel for use with @a dev. If the device is setup to use
815 * a shared channel, then this function will block until a free channel
816 * becomes available.
818 * @return
819 * >= 0 - A valid DMA Handle.
820 * -EBUSY - Device is currently being used.
821 * -ENODEV - Device handed in is invalid.
823 /****************************************************************************/
825 #if (DMA_DEBUG_TRACK_RESERVATION)
826 DMA_Handle_t dma_request_channel_dbg
827 (DMA_Device_t dev, const char *fileName, int lineNum)
828 #else
829 DMA_Handle_t dma_request_channel(DMA_Device_t dev)
830 #endif
832 DMA_Handle_t handle;
833 DMA_DeviceAttribute_t *devAttr;
834 DMA_Channel_t *channel;
835 int controllerIdx;
836 int controllerIdx2;
837 int channelIdx;
839 if (down_interruptible(&gDMA.lock) < 0) {
840 return -ERESTARTSYS;
843 if ((dev < 0) || (dev >= DMA_NUM_DEVICE_ENTRIES)) {
844 handle = -ENODEV;
845 goto out;
847 devAttr = &DMA_gDeviceAttribute[dev];
849 #if (DMA_DEBUG_TRACK_RESERVATION)
851 char *s;
853 s = strrchr(fileName, '/');
854 if (s != NULL) {
855 fileName = s + 1;
858 #endif
859 if ((devAttr->flags & DMA_DEVICE_FLAG_IN_USE) != 0) {
860 /* This device has already been requested and not been freed */
862 printk(KERN_ERR "%s: device %s is already requested\n",
863 __func__, devAttr->name);
864 handle = -EBUSY;
865 goto out;
868 if ((devAttr->flags & DMA_DEVICE_FLAG_IS_DEDICATED) != 0) {
869 /* This device has a dedicated channel. */
871 channel =
872 &gDMA.controller[devAttr->dedicatedController].
873 channel[devAttr->dedicatedChannel];
874 if ((channel->flags & DMA_CHANNEL_FLAG_IN_USE) != 0) {
875 handle = -EBUSY;
876 goto out;
879 channel->flags |= DMA_CHANNEL_FLAG_IN_USE;
880 devAttr->flags |= DMA_DEVICE_FLAG_IN_USE;
882 #if (DMA_DEBUG_TRACK_RESERVATION)
883 channel->fileName = fileName;
884 channel->lineNum = lineNum;
885 #endif
886 handle =
887 MAKE_HANDLE(devAttr->dedicatedController,
888 devAttr->dedicatedChannel);
889 goto out;
892 /* This device needs to use one of the shared channels. */
894 handle = DMA_INVALID_HANDLE;
895 while (handle == DMA_INVALID_HANDLE) {
896 /* Scan through the shared channels and see if one is available */
898 for (controllerIdx2 = 0; controllerIdx2 < DMA_NUM_CONTROLLERS;
899 controllerIdx2++) {
900 /* Check to see if we should try on controller 1 first. */
902 controllerIdx = controllerIdx2;
903 if ((devAttr->
904 flags & DMA_DEVICE_FLAG_ALLOC_DMA1_FIRST) != 0) {
905 controllerIdx = 1 - controllerIdx;
908 /* See if the device is available on the controller being tested */
910 if ((devAttr->
911 flags & (DMA_DEVICE_FLAG_ON_DMA0 << controllerIdx))
912 != 0) {
913 for (channelIdx = 0;
914 channelIdx < DMA_NUM_CHANNELS;
915 channelIdx++) {
916 channel =
917 &gDMA.controller[controllerIdx].
918 channel[channelIdx];
920 if (((channel->
921 flags &
922 DMA_CHANNEL_FLAG_IS_DEDICATED) ==
925 ((channel->
926 flags & DMA_CHANNEL_FLAG_IN_USE)
927 == 0)) {
928 if (((channel->
929 flags &
930 DMA_CHANNEL_FLAG_LARGE_FIFO)
931 != 0)
933 ((devAttr->
934 flags &
935 DMA_DEVICE_FLAG_ALLOW_LARGE_FIFO)
936 == 0)) {
937 /* This channel is a large fifo - don't tie it up */
938 /* with devices that we don't want using it. */
940 continue;
943 channel->flags |=
944 DMA_CHANNEL_FLAG_IN_USE;
945 channel->devType = dev;
946 devAttr->flags |=
947 DMA_DEVICE_FLAG_IN_USE;
949 #if (DMA_DEBUG_TRACK_RESERVATION)
950 channel->fileName = fileName;
951 channel->lineNum = lineNum;
952 #endif
953 handle =
954 MAKE_HANDLE(controllerIdx,
955 channelIdx);
957 /* Now that we've reserved the channel - we can go ahead and configure it */
959 if (ConfigChannel(handle) != 0) {
960 handle = -EIO;
961 printk(KERN_ERR
962 "dma_request_channel: ConfigChannel failed\n");
964 goto out;
970 /* No channels are currently available. Let's wait for one to free up. */
973 DEFINE_WAIT(wait);
975 prepare_to_wait(&gDMA.freeChannelQ, &wait,
976 TASK_INTERRUPTIBLE);
977 up(&gDMA.lock);
978 schedule();
979 finish_wait(&gDMA.freeChannelQ, &wait);
981 if (signal_pending(current)) {
982 /* We don't currently hold gDMA.lock, so we return directly */
984 return -ERESTARTSYS;
988 if (down_interruptible(&gDMA.lock)) {
989 return -ERESTARTSYS;
993 out:
994 up(&gDMA.lock);
996 return handle;
999 /* Create both _dbg and non _dbg functions for modules. */
1001 #if (DMA_DEBUG_TRACK_RESERVATION)
1002 #undef dma_request_channel
1003 DMA_Handle_t dma_request_channel(DMA_Device_t dev)
1005 return dma_request_channel_dbg(dev, __FILE__, __LINE__);
1008 EXPORT_SYMBOL(dma_request_channel_dbg);
1009 #endif
1010 EXPORT_SYMBOL(dma_request_channel);
1012 /****************************************************************************/
1014 * Frees a previously allocated DMA Handle.
1016 /****************************************************************************/
1018 int dma_free_channel(DMA_Handle_t handle /* DMA handle. */
1020 int rc = 0;
1021 DMA_Channel_t *channel;
1022 DMA_DeviceAttribute_t *devAttr;
1024 if (down_interruptible(&gDMA.lock) < 0) {
1025 return -ERESTARTSYS;
1028 channel = HandleToChannel(handle);
1029 if (channel == NULL) {
1030 rc = -EINVAL;
1031 goto out;
1034 devAttr = &DMA_gDeviceAttribute[channel->devType];
1036 if ((channel->flags & DMA_CHANNEL_FLAG_IS_DEDICATED) == 0) {
1037 channel->lastDevType = channel->devType;
1038 channel->devType = DMA_DEVICE_NONE;
1040 channel->flags &= ~DMA_CHANNEL_FLAG_IN_USE;
1041 devAttr->flags &= ~DMA_DEVICE_FLAG_IN_USE;
1043 out:
1044 up(&gDMA.lock);
1046 wake_up_interruptible(&gDMA.freeChannelQ);
1048 return rc;
1051 EXPORT_SYMBOL(dma_free_channel);
1053 /****************************************************************************/
1055 * Determines if a given device has been configured as using a shared
1056 * channel.
1058 * @return
1059 * 0 Device uses a dedicated channel
1060 * > zero Device uses a shared channel
1061 * < zero Error code
1063 /****************************************************************************/
1065 int dma_device_is_channel_shared(DMA_Device_t device /* Device to check. */
1067 DMA_DeviceAttribute_t *devAttr;
1069 if (!IsDeviceValid(device)) {
1070 return -ENODEV;
1072 devAttr = &DMA_gDeviceAttribute[device];
1074 return ((devAttr->flags & DMA_DEVICE_FLAG_IS_DEDICATED) == 0);
1077 EXPORT_SYMBOL(dma_device_is_channel_shared);
1079 /****************************************************************************/
1081 * Allocates buffers for the descriptors. This is normally done automatically
1082 * but needs to be done explicitly when initiating a dma from interrupt
1083 * context.
1085 * @return
1086 * 0 Descriptors were allocated successfully
1087 * -EINVAL Invalid device type for this kind of transfer
1088 * (i.e. the device is _MEM_TO_DEV and not _DEV_TO_MEM)
1089 * -ENOMEM Memory exhausted
1091 /****************************************************************************/
1093 int dma_alloc_descriptors(DMA_Handle_t handle, /* DMA Handle */
1094 dmacHw_TRANSFER_TYPE_e transferType, /* Type of transfer being performed */
1095 dma_addr_t srcData, /* Place to get data to write to device */
1096 dma_addr_t dstData, /* Pointer to device data address */
1097 size_t numBytes /* Number of bytes to transfer to the device */
1099 DMA_Channel_t *channel;
1100 DMA_DeviceAttribute_t *devAttr;
1101 int numDescriptors;
1102 size_t ringBytesRequired;
1103 int rc = 0;
1105 channel = HandleToChannel(handle);
1106 if (channel == NULL) {
1107 return -ENODEV;
1110 devAttr = &DMA_gDeviceAttribute[channel->devType];
1112 if (devAttr->config.transferType != transferType) {
1113 return -EINVAL;
1116 /* Figure out how many descriptors we need. */
1118 /* printk("srcData: 0x%08x dstData: 0x%08x, numBytes: %d\n", */
1119 /* srcData, dstData, numBytes); */
1121 numDescriptors = dmacHw_calculateDescriptorCount(&devAttr->config,
1122 (void *)srcData,
1123 (void *)dstData,
1124 numBytes);
1125 if (numDescriptors < 0) {
1126 printk(KERN_ERR "%s: dmacHw_calculateDescriptorCount failed\n",
1127 __func__);
1128 return -EINVAL;
1131 /* Check to see if we can reuse the existing descriptor ring, or if we need to allocate */
1132 /* a new one. */
1134 ringBytesRequired = dmacHw_descriptorLen(numDescriptors);
1136 /* printk("ringBytesRequired: %d\n", ringBytesRequired); */
1138 if (ringBytesRequired > devAttr->ring.bytesAllocated) {
1139 /* Make sure that this code path is never taken from interrupt context. */
1140 /* It's OK for an interrupt to initiate a DMA transfer, but the descriptor */
1141 /* allocation needs to have already been done. */
1143 might_sleep();
1145 /* Free the old descriptor ring and allocate a new one. */
1147 dma_free_descriptor_ring(&devAttr->ring);
1149 /* And allocate a new one. */
1151 rc =
1152 dma_alloc_descriptor_ring(&devAttr->ring,
1153 numDescriptors);
1154 if (rc < 0) {
1155 printk(KERN_ERR
1156 "%s: dma_alloc_descriptor_ring(%d) failed\n",
1157 __func__, numDescriptors);
1158 return rc;
1160 /* Setup the descriptor for this transfer */
1162 if (dmacHw_initDescriptor(devAttr->ring.virtAddr,
1163 devAttr->ring.physAddr,
1164 devAttr->ring.bytesAllocated,
1165 numDescriptors) < 0) {
1166 printk(KERN_ERR "%s: dmacHw_initDescriptor failed\n",
1167 __func__);
1168 return -EINVAL;
1170 } else {
1171 /* We've already got enough ring buffer allocated. All we need to do is reset */
1172 /* any control information, just in case the previous DMA was stopped. */
1174 dmacHw_resetDescriptorControl(devAttr->ring.virtAddr);
1177 /* dma_alloc/free both set the prevSrc/DstData to 0. If they happen to be the same */
1178 /* as last time, then we don't need to call setDataDescriptor again. */
1180 if (dmacHw_setDataDescriptor(&devAttr->config,
1181 devAttr->ring.virtAddr,
1182 (void *)srcData,
1183 (void *)dstData, numBytes) < 0) {
1184 printk(KERN_ERR "%s: dmacHw_setDataDescriptor failed\n",
1185 __func__);
1186 return -EINVAL;
1189 /* Remember the critical information for this transfer so that we can eliminate */
1190 /* another call to dma_alloc_descriptors if the caller reuses the same buffers */
1192 devAttr->prevSrcData = srcData;
1193 devAttr->prevDstData = dstData;
1194 devAttr->prevNumBytes = numBytes;
1196 return 0;
1199 EXPORT_SYMBOL(dma_alloc_descriptors);
1201 /****************************************************************************/
1203 * Allocates and sets up descriptors for a double buffered circular buffer.
1205 * This is primarily intended to be used for things like the ingress samples
1206 * from a microphone.
1208 * @return
1209 * > 0 Number of descriptors actually allocated.
1210 * -EINVAL Invalid device type for this kind of transfer
1211 * (i.e. the device is _MEM_TO_DEV and not _DEV_TO_MEM)
1212 * -ENOMEM Memory exhausted
1214 /****************************************************************************/
1216 int dma_alloc_double_dst_descriptors(DMA_Handle_t handle, /* DMA Handle */
1217 dma_addr_t srcData, /* Physical address of source data */
1218 dma_addr_t dstData1, /* Physical address of first destination buffer */
1219 dma_addr_t dstData2, /* Physical address of second destination buffer */
1220 size_t numBytes /* Number of bytes in each destination buffer */
1222 DMA_Channel_t *channel;
1223 DMA_DeviceAttribute_t *devAttr;
1224 int numDst1Descriptors;
1225 int numDst2Descriptors;
1226 int numDescriptors;
1227 size_t ringBytesRequired;
1228 int rc = 0;
1230 channel = HandleToChannel(handle);
1231 if (channel == NULL) {
1232 return -ENODEV;
1235 devAttr = &DMA_gDeviceAttribute[channel->devType];
1237 /* Figure out how many descriptors we need. */
1239 /* printk("srcData: 0x%08x dstData: 0x%08x, numBytes: %d\n", */
1240 /* srcData, dstData, numBytes); */
1242 numDst1Descriptors =
1243 dmacHw_calculateDescriptorCount(&devAttr->config, (void *)srcData,
1244 (void *)dstData1, numBytes);
1245 if (numDst1Descriptors < 0) {
1246 return -EINVAL;
1248 numDst2Descriptors =
1249 dmacHw_calculateDescriptorCount(&devAttr->config, (void *)srcData,
1250 (void *)dstData2, numBytes);
1251 if (numDst2Descriptors < 0) {
1252 return -EINVAL;
1254 numDescriptors = numDst1Descriptors + numDst2Descriptors;
1255 /* printk("numDescriptors: %d\n", numDescriptors); */
1257 /* Check to see if we can reuse the existing descriptor ring, or if we need to allocate */
1258 /* a new one. */
1260 ringBytesRequired = dmacHw_descriptorLen(numDescriptors);
1262 /* printk("ringBytesRequired: %d\n", ringBytesRequired); */
1264 if (ringBytesRequired > devAttr->ring.bytesAllocated) {
1265 /* Make sure that this code path is never taken from interrupt context. */
1266 /* It's OK for an interrupt to initiate a DMA transfer, but the descriptor */
1267 /* allocation needs to have already been done. */
1269 might_sleep();
1271 /* Free the old descriptor ring and allocate a new one. */
1273 dma_free_descriptor_ring(&devAttr->ring);
1275 /* And allocate a new one. */
1277 rc =
1278 dma_alloc_descriptor_ring(&devAttr->ring,
1279 numDescriptors);
1280 if (rc < 0) {
1281 printk(KERN_ERR
1282 "%s: dma_alloc_descriptor_ring(%d) failed\n",
1283 __func__, ringBytesRequired);
1284 return rc;
1288 /* Setup the descriptor for this transfer. Since this function is used with */
1289 /* CONTINUOUS DMA operations, we need to reinitialize every time, otherwise */
1290 /* setDataDescriptor will keep trying to append onto the end. */
1292 if (dmacHw_initDescriptor(devAttr->ring.virtAddr,
1293 devAttr->ring.physAddr,
1294 devAttr->ring.bytesAllocated,
1295 numDescriptors) < 0) {
1296 printk(KERN_ERR "%s: dmacHw_initDescriptor failed\n", __func__);
1297 return -EINVAL;
1300 /* dma_alloc/free both set the prevSrc/DstData to 0. If they happen to be the same */
1301 /* as last time, then we don't need to call setDataDescriptor again. */
1303 if (dmacHw_setDataDescriptor(&devAttr->config,
1304 devAttr->ring.virtAddr,
1305 (void *)srcData,
1306 (void *)dstData1, numBytes) < 0) {
1307 printk(KERN_ERR "%s: dmacHw_setDataDescriptor 1 failed\n",
1308 __func__);
1309 return -EINVAL;
1311 if (dmacHw_setDataDescriptor(&devAttr->config,
1312 devAttr->ring.virtAddr,
1313 (void *)srcData,
1314 (void *)dstData2, numBytes) < 0) {
1315 printk(KERN_ERR "%s: dmacHw_setDataDescriptor 2 failed\n",
1316 __func__);
1317 return -EINVAL;
1320 /* You should use dma_start_transfer rather than dma_transfer_xxx so we don't */
1321 /* try to make the 'prev' variables right. */
1323 devAttr->prevSrcData = 0;
1324 devAttr->prevDstData = 0;
1325 devAttr->prevNumBytes = 0;
1327 return numDescriptors;
1330 EXPORT_SYMBOL(dma_alloc_double_dst_descriptors);
1332 /****************************************************************************/
1334 * Initiates a transfer when the descriptors have already been setup.
1336 * This is a special case, and normally, the dma_transfer_xxx functions should
1337 * be used.
1339 * @return
1340 * 0 Transfer was started successfully
1341 * -ENODEV Invalid handle
1343 /****************************************************************************/
1345 int dma_start_transfer(DMA_Handle_t handle)
1347 DMA_Channel_t *channel;
1348 DMA_DeviceAttribute_t *devAttr;
1350 channel = HandleToChannel(handle);
1351 if (channel == NULL) {
1352 return -ENODEV;
1354 devAttr = &DMA_gDeviceAttribute[channel->devType];
1356 dmacHw_initiateTransfer(channel->dmacHwHandle, &devAttr->config,
1357 devAttr->ring.virtAddr);
1359 /* Since we got this far, everything went successfully */
1361 return 0;
1364 EXPORT_SYMBOL(dma_start_transfer);
1366 /****************************************************************************/
1368 * Stops a previously started DMA transfer.
1370 * @return
1371 * 0 Transfer was stopped successfully
1372 * -ENODEV Invalid handle
1374 /****************************************************************************/
1376 int dma_stop_transfer(DMA_Handle_t handle)
1378 DMA_Channel_t *channel;
1380 channel = HandleToChannel(handle);
1381 if (channel == NULL) {
1382 return -ENODEV;
1385 dmacHw_stopTransfer(channel->dmacHwHandle);
1387 return 0;
1390 EXPORT_SYMBOL(dma_stop_transfer);
1392 /****************************************************************************/
1394 * Waits for a DMA to complete by polling. This function is only intended
1395 * to be used for testing. Interrupts should be used for most DMA operations.
1397 /****************************************************************************/
1399 int dma_wait_transfer_done(DMA_Handle_t handle)
1401 DMA_Channel_t *channel;
1402 dmacHw_TRANSFER_STATUS_e status;
1404 channel = HandleToChannel(handle);
1405 if (channel == NULL) {
1406 return -ENODEV;
1409 while ((status =
1410 dmacHw_transferCompleted(channel->dmacHwHandle)) ==
1411 dmacHw_TRANSFER_STATUS_BUSY) {
1415 if (status == dmacHw_TRANSFER_STATUS_ERROR) {
1416 printk(KERN_ERR "%s: DMA transfer failed\n", __func__);
1417 return -EIO;
1419 return 0;
1422 EXPORT_SYMBOL(dma_wait_transfer_done);
1424 /****************************************************************************/
1426 * Initiates a DMA, allocating the descriptors as required.
1428 * @return
1429 * 0 Transfer was started successfully
1430 * -EINVAL Invalid device type for this kind of transfer
1431 * (i.e. the device is _DEV_TO_MEM and not _MEM_TO_DEV)
1433 /****************************************************************************/
1435 int dma_transfer(DMA_Handle_t handle, /* DMA Handle */
1436 dmacHw_TRANSFER_TYPE_e transferType, /* Type of transfer being performed */
1437 dma_addr_t srcData, /* Place to get data to write to device */
1438 dma_addr_t dstData, /* Pointer to device data address */
1439 size_t numBytes /* Number of bytes to transfer to the device */
1441 DMA_Channel_t *channel;
1442 DMA_DeviceAttribute_t *devAttr;
1443 int rc = 0;
1445 channel = HandleToChannel(handle);
1446 if (channel == NULL) {
1447 return -ENODEV;
1450 devAttr = &DMA_gDeviceAttribute[channel->devType];
1452 if (devAttr->config.transferType != transferType) {
1453 return -EINVAL;
1456 /* We keep track of the information about the previous request for this */
1457 /* device, and if the attributes match, then we can use the descriptors we setup */
1458 /* the last time, and not have to reinitialize everything. */
1461 rc =
1462 dma_alloc_descriptors(handle, transferType, srcData,
1463 dstData, numBytes);
1464 if (rc != 0) {
1465 return rc;
1469 /* And kick off the transfer */
1471 devAttr->numBytes = numBytes;
1472 devAttr->transferStartTime = timer_get_tick_count();
1474 dmacHw_initiateTransfer(channel->dmacHwHandle, &devAttr->config,
1475 devAttr->ring.virtAddr);
1477 /* Since we got this far, everything went successfully */
1479 return 0;
1482 EXPORT_SYMBOL(dma_transfer);
1484 /****************************************************************************/
1486 * Set the callback function which will be called when a transfer completes.
1487 * If a NULL callback function is set, then no callback will occur.
1489 * @note @a devHandler will be called from IRQ context.
1491 * @return
1492 * 0 - Success
1493 * -ENODEV - Device handed in is invalid.
1495 /****************************************************************************/
1497 int dma_set_device_handler(DMA_Device_t dev, /* Device to set the callback for. */
1498 DMA_DeviceHandler_t devHandler, /* Function to call when the DMA completes */
1499 void *userData /* Pointer which will be passed to devHandler. */
1501 DMA_DeviceAttribute_t *devAttr;
1502 unsigned long flags;
1504 if (!IsDeviceValid(dev)) {
1505 return -ENODEV;
1507 devAttr = &DMA_gDeviceAttribute[dev];
1509 local_irq_save(flags);
1511 devAttr->userData = userData;
1512 devAttr->devHandler = devHandler;
1514 local_irq_restore(flags);
1516 return 0;
1519 EXPORT_SYMBOL(dma_set_device_handler);