bf60x: sec: Clean up interrupt initialization code for SEC.
[linux-2.6/libata-dev.git] / arch / arm / mach-bcmring / dma.c
blobe5fd241fccdc4b5c93e982ed503c6e43f45bac9b
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 <mach/dma.h>
40 /* ---- Public Variables ------------------------------------------------- */
42 /* ---- Private Constants and Types -------------------------------------- */
44 #define MAKE_HANDLE(controllerIdx, channelIdx) (((controllerIdx) << 4) | (channelIdx))
46 #define CONTROLLER_FROM_HANDLE(handle) (((handle) >> 4) & 0x0f)
47 #define CHANNEL_FROM_HANDLE(handle) ((handle) & 0x0f)
50 /* ---- Private Variables ------------------------------------------------ */
52 static DMA_Global_t gDMA;
53 static struct proc_dir_entry *gDmaDir;
55 #include "dma_device.c"
57 /* ---- Private Function Prototypes -------------------------------------- */
59 /* ---- Functions ------------------------------------------------------- */
61 /****************************************************************************/
62 /**
63 * Displays information for /proc/dma/channels
65 /****************************************************************************/
67 static int dma_proc_read_channels(char *buf, char **start, off_t offset,
68 int count, int *eof, void *data)
70 int controllerIdx;
71 int channelIdx;
72 int limit = count - 200;
73 int len = 0;
74 DMA_Channel_t *channel;
76 if (down_interruptible(&gDMA.lock) < 0) {
77 return -ERESTARTSYS;
80 for (controllerIdx = 0; controllerIdx < DMA_NUM_CONTROLLERS;
81 controllerIdx++) {
82 for (channelIdx = 0; channelIdx < DMA_NUM_CHANNELS;
83 channelIdx++) {
84 if (len >= limit) {
85 break;
88 channel =
89 &gDMA.controller[controllerIdx].channel[channelIdx];
91 len +=
92 sprintf(buf + len, "%d:%d ", controllerIdx,
93 channelIdx);
95 if ((channel->flags & DMA_CHANNEL_FLAG_IS_DEDICATED) !=
96 0) {
97 len +=
98 sprintf(buf + len, "Dedicated for %s ",
99 DMA_gDeviceAttribute[channel->
100 devType].name);
101 } else {
102 len += sprintf(buf + len, "Shared ");
105 if ((channel->flags & DMA_CHANNEL_FLAG_NO_ISR) != 0) {
106 len += sprintf(buf + len, "No ISR ");
109 if ((channel->flags & DMA_CHANNEL_FLAG_LARGE_FIFO) != 0) {
110 len += sprintf(buf + len, "Fifo: 128 ");
111 } else {
112 len += sprintf(buf + len, "Fifo: 64 ");
115 if ((channel->flags & DMA_CHANNEL_FLAG_IN_USE) != 0) {
116 len +=
117 sprintf(buf + len, "InUse by %s",
118 DMA_gDeviceAttribute[channel->
119 devType].name);
120 #if (DMA_DEBUG_TRACK_RESERVATION)
121 len +=
122 sprintf(buf + len, " (%s:%d)",
123 channel->fileName,
124 channel->lineNum);
125 #endif
126 } else {
127 len += sprintf(buf + len, "Avail ");
130 if (channel->lastDevType != DMA_DEVICE_NONE) {
131 len +=
132 sprintf(buf + len, "Last use: %s ",
133 DMA_gDeviceAttribute[channel->
134 lastDevType].
135 name);
138 len += sprintf(buf + len, "\n");
141 up(&gDMA.lock);
142 *eof = 1;
144 return len;
147 /****************************************************************************/
149 * Displays information for /proc/dma/devices
151 /****************************************************************************/
153 static int dma_proc_read_devices(char *buf, char **start, off_t offset,
154 int count, int *eof, void *data)
156 int limit = count - 200;
157 int len = 0;
158 int devIdx;
160 if (down_interruptible(&gDMA.lock) < 0) {
161 return -ERESTARTSYS;
164 for (devIdx = 0; devIdx < DMA_NUM_DEVICE_ENTRIES; devIdx++) {
165 DMA_DeviceAttribute_t *devAttr = &DMA_gDeviceAttribute[devIdx];
167 if (devAttr->name == NULL) {
168 continue;
171 if (len >= limit) {
172 break;
175 len += sprintf(buf + len, "%-12s ", devAttr->name);
177 if ((devAttr->flags & DMA_DEVICE_FLAG_IS_DEDICATED) != 0) {
178 len +=
179 sprintf(buf + len, "Dedicated %d:%d ",
180 devAttr->dedicatedController,
181 devAttr->dedicatedChannel);
182 } else {
183 len += sprintf(buf + len, "Shared DMA:");
184 if ((devAttr->flags & DMA_DEVICE_FLAG_ON_DMA0) != 0) {
185 len += sprintf(buf + len, "0");
187 if ((devAttr->flags & DMA_DEVICE_FLAG_ON_DMA1) != 0) {
188 len += sprintf(buf + len, "1");
190 len += sprintf(buf + len, " ");
192 if ((devAttr->flags & DMA_DEVICE_FLAG_NO_ISR) != 0) {
193 len += sprintf(buf + len, "NoISR ");
195 if ((devAttr->flags & DMA_DEVICE_FLAG_ALLOW_LARGE_FIFO) != 0) {
196 len += sprintf(buf + len, "Allow-128 ");
199 len +=
200 sprintf(buf + len,
201 "Xfer #: %Lu Ticks: %Lu Bytes: %Lu DescLen: %u\n",
202 devAttr->numTransfers, devAttr->transferTicks,
203 devAttr->transferBytes,
204 devAttr->ring.bytesAllocated);
208 up(&gDMA.lock);
209 *eof = 1;
211 return len;
214 /****************************************************************************/
216 * Determines if a DMA_Device_t is "valid".
218 * @return
219 * TRUE - dma device is valid
220 * FALSE - dma device isn't valid
222 /****************************************************************************/
224 static inline int IsDeviceValid(DMA_Device_t device)
226 return (device >= 0) && (device < DMA_NUM_DEVICE_ENTRIES);
229 /****************************************************************************/
231 * Translates a DMA handle into a pointer to a channel.
233 * @return
234 * non-NULL - pointer to DMA_Channel_t
235 * NULL - DMA Handle was invalid
237 /****************************************************************************/
239 static inline DMA_Channel_t *HandleToChannel(DMA_Handle_t handle)
241 int controllerIdx;
242 int channelIdx;
244 controllerIdx = CONTROLLER_FROM_HANDLE(handle);
245 channelIdx = CHANNEL_FROM_HANDLE(handle);
247 if ((controllerIdx > DMA_NUM_CONTROLLERS)
248 || (channelIdx > DMA_NUM_CHANNELS)) {
249 return NULL;
251 return &gDMA.controller[controllerIdx].channel[channelIdx];
254 /****************************************************************************/
256 * Interrupt handler which is called to process DMA interrupts.
258 /****************************************************************************/
260 static irqreturn_t dma_interrupt_handler(int irq, void *dev_id)
262 DMA_Channel_t *channel;
263 DMA_DeviceAttribute_t *devAttr;
264 int irqStatus;
266 channel = (DMA_Channel_t *) dev_id;
268 /* Figure out why we were called, and knock down the interrupt */
270 irqStatus = dmacHw_getInterruptStatus(channel->dmacHwHandle);
271 dmacHw_clearInterrupt(channel->dmacHwHandle);
273 if ((channel->devType < 0)
274 || (channel->devType > DMA_NUM_DEVICE_ENTRIES)) {
275 printk(KERN_ERR "dma_interrupt_handler: Invalid devType: %d\n",
276 channel->devType);
277 return IRQ_NONE;
279 devAttr = &DMA_gDeviceAttribute[channel->devType];
281 /* Update stats */
283 if ((irqStatus & dmacHw_INTERRUPT_STATUS_TRANS) != 0) {
284 devAttr->transferTicks +=
285 (timer_get_tick_count() - devAttr->transferStartTime);
288 if ((irqStatus & dmacHw_INTERRUPT_STATUS_ERROR) != 0) {
289 printk(KERN_ERR
290 "dma_interrupt_handler: devType :%d DMA error (%s)\n",
291 channel->devType, devAttr->name);
292 } else {
293 devAttr->numTransfers++;
294 devAttr->transferBytes += devAttr->numBytes;
297 /* Call any installed handler */
299 if (devAttr->devHandler != NULL) {
300 devAttr->devHandler(channel->devType, irqStatus,
301 devAttr->userData);
304 return IRQ_HANDLED;
307 /****************************************************************************/
309 * Allocates memory to hold a descriptor ring. The descriptor ring then
310 * needs to be populated by making one or more calls to
311 * dna_add_descriptors.
313 * The returned descriptor ring will be automatically initialized.
315 * @return
316 * 0 Descriptor ring was allocated successfully
317 * -EINVAL Invalid parameters passed in
318 * -ENOMEM Unable to allocate memory for the desired number of descriptors.
320 /****************************************************************************/
322 int dma_alloc_descriptor_ring(DMA_DescriptorRing_t *ring, /* Descriptor ring to populate */
323 int numDescriptors /* Number of descriptors that need to be allocated. */
325 size_t bytesToAlloc = dmacHw_descriptorLen(numDescriptors);
327 if ((ring == NULL) || (numDescriptors <= 0)) {
328 return -EINVAL;
331 ring->physAddr = 0;
332 ring->descriptorsAllocated = 0;
333 ring->bytesAllocated = 0;
335 ring->virtAddr = dma_alloc_writecombine(NULL,
336 bytesToAlloc,
337 &ring->physAddr,
338 GFP_KERNEL);
339 if (ring->virtAddr == NULL) {
340 return -ENOMEM;
343 ring->bytesAllocated = bytesToAlloc;
344 ring->descriptorsAllocated = numDescriptors;
346 return dma_init_descriptor_ring(ring, numDescriptors);
349 EXPORT_SYMBOL(dma_alloc_descriptor_ring);
351 /****************************************************************************/
353 * Releases the memory which was previously allocated for a descriptor ring.
355 /****************************************************************************/
357 void dma_free_descriptor_ring(DMA_DescriptorRing_t *ring /* Descriptor to release */
359 if (ring->virtAddr != NULL) {
360 dma_free_writecombine(NULL,
361 ring->bytesAllocated,
362 ring->virtAddr, ring->physAddr);
365 ring->bytesAllocated = 0;
366 ring->descriptorsAllocated = 0;
367 ring->virtAddr = NULL;
368 ring->physAddr = 0;
371 EXPORT_SYMBOL(dma_free_descriptor_ring);
373 /****************************************************************************/
375 * Initializes a descriptor ring, so that descriptors can be added to it.
376 * Once a descriptor ring has been allocated, it may be reinitialized for
377 * use with additional/different regions of memory.
379 * Note that if 7 descriptors are allocated, it's perfectly acceptable to
380 * initialize the ring with a smaller number of descriptors. The amount
381 * of memory allocated for the descriptor ring will not be reduced, and
382 * the descriptor ring may be reinitialized later
384 * @return
385 * 0 Descriptor ring was initialized successfully
386 * -ENOMEM The descriptor which was passed in has insufficient space
387 * to hold the desired number of descriptors.
389 /****************************************************************************/
391 int dma_init_descriptor_ring(DMA_DescriptorRing_t *ring, /* Descriptor ring to initialize */
392 int numDescriptors /* Number of descriptors to initialize. */
394 if (ring->virtAddr == NULL) {
395 return -EINVAL;
397 if (dmacHw_initDescriptor(ring->virtAddr,
398 ring->physAddr,
399 ring->bytesAllocated, numDescriptors) < 0) {
400 printk(KERN_ERR
401 "dma_init_descriptor_ring: dmacHw_initDescriptor failed\n");
402 return -ENOMEM;
405 return 0;
408 EXPORT_SYMBOL(dma_init_descriptor_ring);
410 /****************************************************************************/
412 * Determines the number of descriptors which would be required for a
413 * transfer of the indicated memory region.
415 * This function also needs to know which DMA device this transfer will
416 * be destined for, so that the appropriate DMA configuration can be retrieved.
417 * DMA parameters such as transfer width, and whether this is a memory-to-memory
418 * or memory-to-peripheral, etc can all affect the actual number of descriptors
419 * required.
421 * @return
422 * > 0 Returns the number of descriptors required for the indicated transfer
423 * -ENODEV - Device handed in is invalid.
424 * -EINVAL Invalid parameters
425 * -ENOMEM Memory exhausted
427 /****************************************************************************/
429 int dma_calculate_descriptor_count(DMA_Device_t device, /* DMA Device that this will be associated with */
430 dma_addr_t srcData, /* Place to get data to write to device */
431 dma_addr_t dstData, /* Pointer to device data address */
432 size_t numBytes /* Number of bytes to transfer to the device */
434 int numDescriptors;
435 DMA_DeviceAttribute_t *devAttr;
437 if (!IsDeviceValid(device)) {
438 return -ENODEV;
440 devAttr = &DMA_gDeviceAttribute[device];
442 numDescriptors = dmacHw_calculateDescriptorCount(&devAttr->config,
443 (void *)srcData,
444 (void *)dstData,
445 numBytes);
446 if (numDescriptors < 0) {
447 printk(KERN_ERR
448 "dma_calculate_descriptor_count: dmacHw_calculateDescriptorCount failed\n");
449 return -EINVAL;
452 return numDescriptors;
455 EXPORT_SYMBOL(dma_calculate_descriptor_count);
457 /****************************************************************************/
459 * Adds a region of memory to the descriptor ring. Note that it may take
460 * multiple descriptors for each region of memory. It is the callers
461 * responsibility to allocate a sufficiently large descriptor ring.
463 * @return
464 * 0 Descriptors were added successfully
465 * -ENODEV Device handed in is invalid.
466 * -EINVAL Invalid parameters
467 * -ENOMEM Memory exhausted
469 /****************************************************************************/
471 int dma_add_descriptors(DMA_DescriptorRing_t *ring, /* Descriptor ring to add descriptors to */
472 DMA_Device_t device, /* DMA Device that descriptors are for */
473 dma_addr_t srcData, /* Place to get data (memory or device) */
474 dma_addr_t dstData, /* Place to put data (memory or device) */
475 size_t numBytes /* Number of bytes to transfer to the device */
477 int rc;
478 DMA_DeviceAttribute_t *devAttr;
480 if (!IsDeviceValid(device)) {
481 return -ENODEV;
483 devAttr = &DMA_gDeviceAttribute[device];
485 rc = dmacHw_setDataDescriptor(&devAttr->config,
486 ring->virtAddr,
487 (void *)srcData,
488 (void *)dstData, numBytes);
489 if (rc < 0) {
490 printk(KERN_ERR
491 "dma_add_descriptors: dmacHw_setDataDescriptor failed with code: %d\n",
492 rc);
493 return -ENOMEM;
496 return 0;
499 EXPORT_SYMBOL(dma_add_descriptors);
501 /****************************************************************************/
503 * Sets the descriptor ring associated with a device.
505 * Once set, the descriptor ring will be associated with the device, even
506 * across channel request/free calls. Passing in a NULL descriptor ring
507 * will release any descriptor ring currently associated with the device.
509 * Note: If you call dma_transfer, or one of the other dma_alloc_ functions
510 * the descriptor ring may be released and reallocated.
512 * Note: This function will release the descriptor memory for any current
513 * descriptor ring associated with this device.
515 * @return
516 * 0 Descriptors were added successfully
517 * -ENODEV Device handed in is invalid.
519 /****************************************************************************/
521 int dma_set_device_descriptor_ring(DMA_Device_t device, /* Device to update the descriptor ring for. */
522 DMA_DescriptorRing_t *ring /* Descriptor ring to add descriptors to */
524 DMA_DeviceAttribute_t *devAttr;
526 if (!IsDeviceValid(device)) {
527 return -ENODEV;
529 devAttr = &DMA_gDeviceAttribute[device];
531 /* Free the previously allocated descriptor ring */
533 dma_free_descriptor_ring(&devAttr->ring);
535 if (ring != NULL) {
536 /* Copy in the new one */
538 devAttr->ring = *ring;
541 /* Set things up so that if dma_transfer is called then this descriptor */
542 /* ring will get freed. */
544 devAttr->prevSrcData = 0;
545 devAttr->prevDstData = 0;
546 devAttr->prevNumBytes = 0;
548 return 0;
551 EXPORT_SYMBOL(dma_set_device_descriptor_ring);
553 /****************************************************************************/
555 * Retrieves the descriptor ring associated with a device.
557 * @return
558 * 0 Descriptors were added successfully
559 * -ENODEV Device handed in is invalid.
561 /****************************************************************************/
563 int dma_get_device_descriptor_ring(DMA_Device_t device, /* Device to retrieve the descriptor ring for. */
564 DMA_DescriptorRing_t *ring /* Place to store retrieved ring */
566 DMA_DeviceAttribute_t *devAttr;
568 memset(ring, 0, sizeof(*ring));
570 if (!IsDeviceValid(device)) {
571 return -ENODEV;
573 devAttr = &DMA_gDeviceAttribute[device];
575 *ring = devAttr->ring;
577 return 0;
580 EXPORT_SYMBOL(dma_get_device_descriptor_ring);
582 /****************************************************************************/
584 * Configures a DMA channel.
586 * @return
587 * >= 0 - Initialization was successful.
589 * -EBUSY - Device is currently being used.
590 * -ENODEV - Device handed in is invalid.
592 /****************************************************************************/
594 static int ConfigChannel(DMA_Handle_t handle)
596 DMA_Channel_t *channel;
597 DMA_DeviceAttribute_t *devAttr;
598 int controllerIdx;
600 channel = HandleToChannel(handle);
601 if (channel == NULL) {
602 return -ENODEV;
604 devAttr = &DMA_gDeviceAttribute[channel->devType];
605 controllerIdx = CONTROLLER_FROM_HANDLE(handle);
607 if ((devAttr->flags & DMA_DEVICE_FLAG_PORT_PER_DMAC) != 0) {
608 if (devAttr->config.transferType ==
609 dmacHw_TRANSFER_TYPE_MEM_TO_PERIPHERAL) {
610 devAttr->config.dstPeripheralPort =
611 devAttr->dmacPort[controllerIdx];
612 } else if (devAttr->config.transferType ==
613 dmacHw_TRANSFER_TYPE_PERIPHERAL_TO_MEM) {
614 devAttr->config.srcPeripheralPort =
615 devAttr->dmacPort[controllerIdx];
619 if (dmacHw_configChannel(channel->dmacHwHandle, &devAttr->config) != 0) {
620 printk(KERN_ERR "ConfigChannel: dmacHw_configChannel failed\n");
621 return -EIO;
624 return 0;
627 /****************************************************************************/
629 * Initializes all of the data structures associated with the DMA.
630 * @return
631 * >= 0 - Initialization was successful.
633 * -EBUSY - Device is currently being used.
634 * -ENODEV - Device handed in is invalid.
636 /****************************************************************************/
638 int dma_init(void)
640 int rc = 0;
641 int controllerIdx;
642 int channelIdx;
643 DMA_Device_t devIdx;
644 DMA_Channel_t *channel;
645 DMA_Handle_t dedicatedHandle;
647 memset(&gDMA, 0, sizeof(gDMA));
649 sema_init(&gDMA.lock, 0);
650 init_waitqueue_head(&gDMA.freeChannelQ);
652 /* Initialize the Hardware */
654 dmacHw_initDma();
656 /* Start off by marking all of the DMA channels as shared. */
658 for (controllerIdx = 0; controllerIdx < DMA_NUM_CONTROLLERS;
659 controllerIdx++) {
660 for (channelIdx = 0; channelIdx < DMA_NUM_CHANNELS;
661 channelIdx++) {
662 channel =
663 &gDMA.controller[controllerIdx].channel[channelIdx];
665 channel->flags = 0;
666 channel->devType = DMA_DEVICE_NONE;
667 channel->lastDevType = DMA_DEVICE_NONE;
669 #if (DMA_DEBUG_TRACK_RESERVATION)
670 channel->fileName = "";
671 channel->lineNum = 0;
672 #endif
674 channel->dmacHwHandle =
675 dmacHw_getChannelHandle(dmacHw_MAKE_CHANNEL_ID
676 (controllerIdx,
677 channelIdx));
678 dmacHw_initChannel(channel->dmacHwHandle);
682 /* Record any special attributes that channels may have */
684 gDMA.controller[0].channel[0].flags |= DMA_CHANNEL_FLAG_LARGE_FIFO;
685 gDMA.controller[0].channel[1].flags |= DMA_CHANNEL_FLAG_LARGE_FIFO;
686 gDMA.controller[1].channel[0].flags |= DMA_CHANNEL_FLAG_LARGE_FIFO;
687 gDMA.controller[1].channel[1].flags |= DMA_CHANNEL_FLAG_LARGE_FIFO;
689 /* Now walk through and record the dedicated channels. */
691 for (devIdx = 0; devIdx < DMA_NUM_DEVICE_ENTRIES; devIdx++) {
692 DMA_DeviceAttribute_t *devAttr = &DMA_gDeviceAttribute[devIdx];
694 if (((devAttr->flags & DMA_DEVICE_FLAG_NO_ISR) != 0)
695 && ((devAttr->flags & DMA_DEVICE_FLAG_IS_DEDICATED) == 0)) {
696 printk(KERN_ERR
697 "DMA Device: %s Can only request NO_ISR for dedicated devices\n",
698 devAttr->name);
699 rc = -EINVAL;
700 goto out;
703 if ((devAttr->flags & DMA_DEVICE_FLAG_IS_DEDICATED) != 0) {
704 /* This is a dedicated device. Mark the channel as being reserved. */
706 if (devAttr->dedicatedController >= DMA_NUM_CONTROLLERS) {
707 printk(KERN_ERR
708 "DMA Device: %s DMA Controller %d is out of range\n",
709 devAttr->name,
710 devAttr->dedicatedController);
711 rc = -EINVAL;
712 goto out;
715 if (devAttr->dedicatedChannel >= DMA_NUM_CHANNELS) {
716 printk(KERN_ERR
717 "DMA Device: %s DMA Channel %d is out of range\n",
718 devAttr->name,
719 devAttr->dedicatedChannel);
720 rc = -EINVAL;
721 goto out;
724 dedicatedHandle =
725 MAKE_HANDLE(devAttr->dedicatedController,
726 devAttr->dedicatedChannel);
727 channel = HandleToChannel(dedicatedHandle);
729 if ((channel->flags & DMA_CHANNEL_FLAG_IS_DEDICATED) !=
730 0) {
731 printk
732 ("DMA Device: %s attempting to use same DMA Controller:Channel (%d:%d) as %s\n",
733 devAttr->name,
734 devAttr->dedicatedController,
735 devAttr->dedicatedChannel,
736 DMA_gDeviceAttribute[channel->devType].
737 name);
738 rc = -EBUSY;
739 goto out;
742 channel->flags |= DMA_CHANNEL_FLAG_IS_DEDICATED;
743 channel->devType = devIdx;
745 if (devAttr->flags & DMA_DEVICE_FLAG_NO_ISR) {
746 channel->flags |= DMA_CHANNEL_FLAG_NO_ISR;
749 /* For dedicated channels, we can go ahead and configure the DMA channel now */
750 /* as well. */
752 ConfigChannel(dedicatedHandle);
756 /* Go through and register the interrupt handlers */
758 for (controllerIdx = 0; controllerIdx < DMA_NUM_CONTROLLERS;
759 controllerIdx++) {
760 for (channelIdx = 0; channelIdx < DMA_NUM_CHANNELS;
761 channelIdx++) {
762 channel =
763 &gDMA.controller[controllerIdx].channel[channelIdx];
765 if ((channel->flags & DMA_CHANNEL_FLAG_NO_ISR) == 0) {
766 snprintf(channel->name, sizeof(channel->name),
767 "dma %d:%d %s", controllerIdx,
768 channelIdx,
769 channel->devType ==
770 DMA_DEVICE_NONE ? "" :
771 DMA_gDeviceAttribute[channel->devType].
772 name);
774 rc =
775 request_irq(IRQ_DMA0C0 +
776 (controllerIdx *
777 DMA_NUM_CHANNELS) +
778 channelIdx,
779 dma_interrupt_handler,
780 IRQF_DISABLED, channel->name,
781 channel);
782 if (rc != 0) {
783 printk(KERN_ERR
784 "request_irq for IRQ_DMA%dC%d failed\n",
785 controllerIdx, channelIdx);
791 /* Create /proc/dma/channels and /proc/dma/devices */
793 gDmaDir = proc_mkdir("dma", NULL);
795 if (gDmaDir == NULL) {
796 printk(KERN_ERR "Unable to create /proc/dma\n");
797 } else {
798 create_proc_read_entry("channels", 0, gDmaDir,
799 dma_proc_read_channels, NULL);
800 create_proc_read_entry("devices", 0, gDmaDir,
801 dma_proc_read_devices, NULL);
804 out:
806 up(&gDMA.lock);
808 return rc;
811 /****************************************************************************/
813 * Reserves a channel for use with @a dev. If the device is setup to use
814 * a shared channel, then this function will block until a free channel
815 * becomes available.
817 * @return
818 * >= 0 - A valid DMA Handle.
819 * -EBUSY - Device is currently being used.
820 * -ENODEV - Device handed in is invalid.
822 /****************************************************************************/
824 #if (DMA_DEBUG_TRACK_RESERVATION)
825 DMA_Handle_t dma_request_channel_dbg
826 (DMA_Device_t dev, const char *fileName, int lineNum)
827 #else
828 DMA_Handle_t dma_request_channel(DMA_Device_t dev)
829 #endif
831 DMA_Handle_t handle;
832 DMA_DeviceAttribute_t *devAttr;
833 DMA_Channel_t *channel;
834 int controllerIdx;
835 int controllerIdx2;
836 int channelIdx;
838 if (down_interruptible(&gDMA.lock) < 0) {
839 return -ERESTARTSYS;
842 if ((dev < 0) || (dev >= DMA_NUM_DEVICE_ENTRIES)) {
843 handle = -ENODEV;
844 goto out;
846 devAttr = &DMA_gDeviceAttribute[dev];
848 #if (DMA_DEBUG_TRACK_RESERVATION)
850 char *s;
852 s = strrchr(fileName, '/');
853 if (s != NULL) {
854 fileName = s + 1;
857 #endif
858 if ((devAttr->flags & DMA_DEVICE_FLAG_IN_USE) != 0) {
859 /* This device has already been requested and not been freed */
861 printk(KERN_ERR "%s: device %s is already requested\n",
862 __func__, devAttr->name);
863 handle = -EBUSY;
864 goto out;
867 if ((devAttr->flags & DMA_DEVICE_FLAG_IS_DEDICATED) != 0) {
868 /* This device has a dedicated channel. */
870 channel =
871 &gDMA.controller[devAttr->dedicatedController].
872 channel[devAttr->dedicatedChannel];
873 if ((channel->flags & DMA_CHANNEL_FLAG_IN_USE) != 0) {
874 handle = -EBUSY;
875 goto out;
878 channel->flags |= DMA_CHANNEL_FLAG_IN_USE;
879 devAttr->flags |= DMA_DEVICE_FLAG_IN_USE;
881 #if (DMA_DEBUG_TRACK_RESERVATION)
882 channel->fileName = fileName;
883 channel->lineNum = lineNum;
884 #endif
885 handle =
886 MAKE_HANDLE(devAttr->dedicatedController,
887 devAttr->dedicatedChannel);
888 goto out;
891 /* This device needs to use one of the shared channels. */
893 handle = DMA_INVALID_HANDLE;
894 while (handle == DMA_INVALID_HANDLE) {
895 /* Scan through the shared channels and see if one is available */
897 for (controllerIdx2 = 0; controllerIdx2 < DMA_NUM_CONTROLLERS;
898 controllerIdx2++) {
899 /* Check to see if we should try on controller 1 first. */
901 controllerIdx = controllerIdx2;
902 if ((devAttr->
903 flags & DMA_DEVICE_FLAG_ALLOC_DMA1_FIRST) != 0) {
904 controllerIdx = 1 - controllerIdx;
907 /* See if the device is available on the controller being tested */
909 if ((devAttr->
910 flags & (DMA_DEVICE_FLAG_ON_DMA0 << controllerIdx))
911 != 0) {
912 for (channelIdx = 0;
913 channelIdx < DMA_NUM_CHANNELS;
914 channelIdx++) {
915 channel =
916 &gDMA.controller[controllerIdx].
917 channel[channelIdx];
919 if (((channel->
920 flags &
921 DMA_CHANNEL_FLAG_IS_DEDICATED) ==
924 ((channel->
925 flags & DMA_CHANNEL_FLAG_IN_USE)
926 == 0)) {
927 if (((channel->
928 flags &
929 DMA_CHANNEL_FLAG_LARGE_FIFO)
930 != 0)
932 ((devAttr->
933 flags &
934 DMA_DEVICE_FLAG_ALLOW_LARGE_FIFO)
935 == 0)) {
936 /* This channel is a large fifo - don't tie it up */
937 /* with devices that we don't want using it. */
939 continue;
942 channel->flags |=
943 DMA_CHANNEL_FLAG_IN_USE;
944 channel->devType = dev;
945 devAttr->flags |=
946 DMA_DEVICE_FLAG_IN_USE;
948 #if (DMA_DEBUG_TRACK_RESERVATION)
949 channel->fileName = fileName;
950 channel->lineNum = lineNum;
951 #endif
952 handle =
953 MAKE_HANDLE(controllerIdx,
954 channelIdx);
956 /* Now that we've reserved the channel - we can go ahead and configure it */
958 if (ConfigChannel(handle) != 0) {
959 handle = -EIO;
960 printk(KERN_ERR
961 "dma_request_channel: ConfigChannel failed\n");
963 goto out;
969 /* No channels are currently available. Let's wait for one to free up. */
972 DEFINE_WAIT(wait);
974 prepare_to_wait(&gDMA.freeChannelQ, &wait,
975 TASK_INTERRUPTIBLE);
976 up(&gDMA.lock);
977 schedule();
978 finish_wait(&gDMA.freeChannelQ, &wait);
980 if (signal_pending(current)) {
981 /* We don't currently hold gDMA.lock, so we return directly */
983 return -ERESTARTSYS;
987 if (down_interruptible(&gDMA.lock)) {
988 return -ERESTARTSYS;
992 out:
993 up(&gDMA.lock);
995 return handle;
998 /* Create both _dbg and non _dbg functions for modules. */
1000 #if (DMA_DEBUG_TRACK_RESERVATION)
1001 #undef dma_request_channel
1002 DMA_Handle_t dma_request_channel(DMA_Device_t dev)
1004 return dma_request_channel_dbg(dev, __FILE__, __LINE__);
1007 EXPORT_SYMBOL(dma_request_channel_dbg);
1008 #endif
1009 EXPORT_SYMBOL(dma_request_channel);
1011 /****************************************************************************/
1013 * Frees a previously allocated DMA Handle.
1015 /****************************************************************************/
1017 int dma_free_channel(DMA_Handle_t handle /* DMA handle. */
1019 int rc = 0;
1020 DMA_Channel_t *channel;
1021 DMA_DeviceAttribute_t *devAttr;
1023 if (down_interruptible(&gDMA.lock) < 0) {
1024 return -ERESTARTSYS;
1027 channel = HandleToChannel(handle);
1028 if (channel == NULL) {
1029 rc = -EINVAL;
1030 goto out;
1033 devAttr = &DMA_gDeviceAttribute[channel->devType];
1035 if ((channel->flags & DMA_CHANNEL_FLAG_IS_DEDICATED) == 0) {
1036 channel->lastDevType = channel->devType;
1037 channel->devType = DMA_DEVICE_NONE;
1039 channel->flags &= ~DMA_CHANNEL_FLAG_IN_USE;
1040 devAttr->flags &= ~DMA_DEVICE_FLAG_IN_USE;
1042 out:
1043 up(&gDMA.lock);
1045 wake_up_interruptible(&gDMA.freeChannelQ);
1047 return rc;
1050 EXPORT_SYMBOL(dma_free_channel);
1052 /****************************************************************************/
1054 * Determines if a given device has been configured as using a shared
1055 * channel.
1057 * @return
1058 * 0 Device uses a dedicated channel
1059 * > zero Device uses a shared channel
1060 * < zero Error code
1062 /****************************************************************************/
1064 int dma_device_is_channel_shared(DMA_Device_t device /* Device to check. */
1066 DMA_DeviceAttribute_t *devAttr;
1068 if (!IsDeviceValid(device)) {
1069 return -ENODEV;
1071 devAttr = &DMA_gDeviceAttribute[device];
1073 return ((devAttr->flags & DMA_DEVICE_FLAG_IS_DEDICATED) == 0);
1076 EXPORT_SYMBOL(dma_device_is_channel_shared);
1078 /****************************************************************************/
1080 * Allocates buffers for the descriptors. This is normally done automatically
1081 * but needs to be done explicitly when initiating a dma from interrupt
1082 * context.
1084 * @return
1085 * 0 Descriptors were allocated successfully
1086 * -EINVAL Invalid device type for this kind of transfer
1087 * (i.e. the device is _MEM_TO_DEV and not _DEV_TO_MEM)
1088 * -ENOMEM Memory exhausted
1090 /****************************************************************************/
1092 int dma_alloc_descriptors(DMA_Handle_t handle, /* DMA Handle */
1093 dmacHw_TRANSFER_TYPE_e transferType, /* Type of transfer being performed */
1094 dma_addr_t srcData, /* Place to get data to write to device */
1095 dma_addr_t dstData, /* Pointer to device data address */
1096 size_t numBytes /* Number of bytes to transfer to the device */
1098 DMA_Channel_t *channel;
1099 DMA_DeviceAttribute_t *devAttr;
1100 int numDescriptors;
1101 size_t ringBytesRequired;
1102 int rc = 0;
1104 channel = HandleToChannel(handle);
1105 if (channel == NULL) {
1106 return -ENODEV;
1109 devAttr = &DMA_gDeviceAttribute[channel->devType];
1111 if (devAttr->config.transferType != transferType) {
1112 return -EINVAL;
1115 /* Figure out how many descriptors we need. */
1117 /* printk("srcData: 0x%08x dstData: 0x%08x, numBytes: %d\n", */
1118 /* srcData, dstData, numBytes); */
1120 numDescriptors = dmacHw_calculateDescriptorCount(&devAttr->config,
1121 (void *)srcData,
1122 (void *)dstData,
1123 numBytes);
1124 if (numDescriptors < 0) {
1125 printk(KERN_ERR "%s: dmacHw_calculateDescriptorCount failed\n",
1126 __func__);
1127 return -EINVAL;
1130 /* Check to see if we can reuse the existing descriptor ring, or if we need to allocate */
1131 /* a new one. */
1133 ringBytesRequired = dmacHw_descriptorLen(numDescriptors);
1135 /* printk("ringBytesRequired: %d\n", ringBytesRequired); */
1137 if (ringBytesRequired > devAttr->ring.bytesAllocated) {
1138 /* Make sure that this code path is never taken from interrupt context. */
1139 /* It's OK for an interrupt to initiate a DMA transfer, but the descriptor */
1140 /* allocation needs to have already been done. */
1142 might_sleep();
1144 /* Free the old descriptor ring and allocate a new one. */
1146 dma_free_descriptor_ring(&devAttr->ring);
1148 /* And allocate a new one. */
1150 rc =
1151 dma_alloc_descriptor_ring(&devAttr->ring,
1152 numDescriptors);
1153 if (rc < 0) {
1154 printk(KERN_ERR
1155 "%s: dma_alloc_descriptor_ring(%d) failed\n",
1156 __func__, numDescriptors);
1157 return rc;
1159 /* Setup the descriptor for this transfer */
1161 if (dmacHw_initDescriptor(devAttr->ring.virtAddr,
1162 devAttr->ring.physAddr,
1163 devAttr->ring.bytesAllocated,
1164 numDescriptors) < 0) {
1165 printk(KERN_ERR "%s: dmacHw_initDescriptor failed\n",
1166 __func__);
1167 return -EINVAL;
1169 } else {
1170 /* We've already got enough ring buffer allocated. All we need to do is reset */
1171 /* any control information, just in case the previous DMA was stopped. */
1173 dmacHw_resetDescriptorControl(devAttr->ring.virtAddr);
1176 /* dma_alloc/free both set the prevSrc/DstData to 0. If they happen to be the same */
1177 /* as last time, then we don't need to call setDataDescriptor again. */
1179 if (dmacHw_setDataDescriptor(&devAttr->config,
1180 devAttr->ring.virtAddr,
1181 (void *)srcData,
1182 (void *)dstData, numBytes) < 0) {
1183 printk(KERN_ERR "%s: dmacHw_setDataDescriptor failed\n",
1184 __func__);
1185 return -EINVAL;
1188 /* Remember the critical information for this transfer so that we can eliminate */
1189 /* another call to dma_alloc_descriptors if the caller reuses the same buffers */
1191 devAttr->prevSrcData = srcData;
1192 devAttr->prevDstData = dstData;
1193 devAttr->prevNumBytes = numBytes;
1195 return 0;
1198 EXPORT_SYMBOL(dma_alloc_descriptors);
1200 /****************************************************************************/
1202 * Allocates and sets up descriptors for a double buffered circular buffer.
1204 * This is primarily intended to be used for things like the ingress samples
1205 * from a microphone.
1207 * @return
1208 * > 0 Number of descriptors actually allocated.
1209 * -EINVAL Invalid device type for this kind of transfer
1210 * (i.e. the device is _MEM_TO_DEV and not _DEV_TO_MEM)
1211 * -ENOMEM Memory exhausted
1213 /****************************************************************************/
1215 int dma_alloc_double_dst_descriptors(DMA_Handle_t handle, /* DMA Handle */
1216 dma_addr_t srcData, /* Physical address of source data */
1217 dma_addr_t dstData1, /* Physical address of first destination buffer */
1218 dma_addr_t dstData2, /* Physical address of second destination buffer */
1219 size_t numBytes /* Number of bytes in each destination buffer */
1221 DMA_Channel_t *channel;
1222 DMA_DeviceAttribute_t *devAttr;
1223 int numDst1Descriptors;
1224 int numDst2Descriptors;
1225 int numDescriptors;
1226 size_t ringBytesRequired;
1227 int rc = 0;
1229 channel = HandleToChannel(handle);
1230 if (channel == NULL) {
1231 return -ENODEV;
1234 devAttr = &DMA_gDeviceAttribute[channel->devType];
1236 /* Figure out how many descriptors we need. */
1238 /* printk("srcData: 0x%08x dstData: 0x%08x, numBytes: %d\n", */
1239 /* srcData, dstData, numBytes); */
1241 numDst1Descriptors =
1242 dmacHw_calculateDescriptorCount(&devAttr->config, (void *)srcData,
1243 (void *)dstData1, numBytes);
1244 if (numDst1Descriptors < 0) {
1245 return -EINVAL;
1247 numDst2Descriptors =
1248 dmacHw_calculateDescriptorCount(&devAttr->config, (void *)srcData,
1249 (void *)dstData2, numBytes);
1250 if (numDst2Descriptors < 0) {
1251 return -EINVAL;
1253 numDescriptors = numDst1Descriptors + numDst2Descriptors;
1254 /* printk("numDescriptors: %d\n", numDescriptors); */
1256 /* Check to see if we can reuse the existing descriptor ring, or if we need to allocate */
1257 /* a new one. */
1259 ringBytesRequired = dmacHw_descriptorLen(numDescriptors);
1261 /* printk("ringBytesRequired: %d\n", ringBytesRequired); */
1263 if (ringBytesRequired > devAttr->ring.bytesAllocated) {
1264 /* Make sure that this code path is never taken from interrupt context. */
1265 /* It's OK for an interrupt to initiate a DMA transfer, but the descriptor */
1266 /* allocation needs to have already been done. */
1268 might_sleep();
1270 /* Free the old descriptor ring and allocate a new one. */
1272 dma_free_descriptor_ring(&devAttr->ring);
1274 /* And allocate a new one. */
1276 rc =
1277 dma_alloc_descriptor_ring(&devAttr->ring,
1278 numDescriptors);
1279 if (rc < 0) {
1280 printk(KERN_ERR
1281 "%s: dma_alloc_descriptor_ring(%d) failed\n",
1282 __func__, ringBytesRequired);
1283 return rc;
1287 /* Setup the descriptor for this transfer. Since this function is used with */
1288 /* CONTINUOUS DMA operations, we need to reinitialize every time, otherwise */
1289 /* setDataDescriptor will keep trying to append onto the end. */
1291 if (dmacHw_initDescriptor(devAttr->ring.virtAddr,
1292 devAttr->ring.physAddr,
1293 devAttr->ring.bytesAllocated,
1294 numDescriptors) < 0) {
1295 printk(KERN_ERR "%s: dmacHw_initDescriptor failed\n", __func__);
1296 return -EINVAL;
1299 /* dma_alloc/free both set the prevSrc/DstData to 0. If they happen to be the same */
1300 /* as last time, then we don't need to call setDataDescriptor again. */
1302 if (dmacHw_setDataDescriptor(&devAttr->config,
1303 devAttr->ring.virtAddr,
1304 (void *)srcData,
1305 (void *)dstData1, numBytes) < 0) {
1306 printk(KERN_ERR "%s: dmacHw_setDataDescriptor 1 failed\n",
1307 __func__);
1308 return -EINVAL;
1310 if (dmacHw_setDataDescriptor(&devAttr->config,
1311 devAttr->ring.virtAddr,
1312 (void *)srcData,
1313 (void *)dstData2, numBytes) < 0) {
1314 printk(KERN_ERR "%s: dmacHw_setDataDescriptor 2 failed\n",
1315 __func__);
1316 return -EINVAL;
1319 /* You should use dma_start_transfer rather than dma_transfer_xxx so we don't */
1320 /* try to make the 'prev' variables right. */
1322 devAttr->prevSrcData = 0;
1323 devAttr->prevDstData = 0;
1324 devAttr->prevNumBytes = 0;
1326 return numDescriptors;
1329 EXPORT_SYMBOL(dma_alloc_double_dst_descriptors);
1331 /****************************************************************************/
1333 * Initiates a transfer when the descriptors have already been setup.
1335 * This is a special case, and normally, the dma_transfer_xxx functions should
1336 * be used.
1338 * @return
1339 * 0 Transfer was started successfully
1340 * -ENODEV Invalid handle
1342 /****************************************************************************/
1344 int dma_start_transfer(DMA_Handle_t handle)
1346 DMA_Channel_t *channel;
1347 DMA_DeviceAttribute_t *devAttr;
1349 channel = HandleToChannel(handle);
1350 if (channel == NULL) {
1351 return -ENODEV;
1353 devAttr = &DMA_gDeviceAttribute[channel->devType];
1355 dmacHw_initiateTransfer(channel->dmacHwHandle, &devAttr->config,
1356 devAttr->ring.virtAddr);
1358 /* Since we got this far, everything went successfully */
1360 return 0;
1363 EXPORT_SYMBOL(dma_start_transfer);
1365 /****************************************************************************/
1367 * Stops a previously started DMA transfer.
1369 * @return
1370 * 0 Transfer was stopped successfully
1371 * -ENODEV Invalid handle
1373 /****************************************************************************/
1375 int dma_stop_transfer(DMA_Handle_t handle)
1377 DMA_Channel_t *channel;
1379 channel = HandleToChannel(handle);
1380 if (channel == NULL) {
1381 return -ENODEV;
1384 dmacHw_stopTransfer(channel->dmacHwHandle);
1386 return 0;
1389 EXPORT_SYMBOL(dma_stop_transfer);
1391 /****************************************************************************/
1393 * Waits for a DMA to complete by polling. This function is only intended
1394 * to be used for testing. Interrupts should be used for most DMA operations.
1396 /****************************************************************************/
1398 int dma_wait_transfer_done(DMA_Handle_t handle)
1400 DMA_Channel_t *channel;
1401 dmacHw_TRANSFER_STATUS_e status;
1403 channel = HandleToChannel(handle);
1404 if (channel == NULL) {
1405 return -ENODEV;
1408 while ((status =
1409 dmacHw_transferCompleted(channel->dmacHwHandle)) ==
1410 dmacHw_TRANSFER_STATUS_BUSY) {
1414 if (status == dmacHw_TRANSFER_STATUS_ERROR) {
1415 printk(KERN_ERR "%s: DMA transfer failed\n", __func__);
1416 return -EIO;
1418 return 0;
1421 EXPORT_SYMBOL(dma_wait_transfer_done);
1423 /****************************************************************************/
1425 * Initiates a DMA, allocating the descriptors as required.
1427 * @return
1428 * 0 Transfer was started successfully
1429 * -EINVAL Invalid device type for this kind of transfer
1430 * (i.e. the device is _DEV_TO_MEM and not _MEM_TO_DEV)
1432 /****************************************************************************/
1434 int dma_transfer(DMA_Handle_t handle, /* DMA Handle */
1435 dmacHw_TRANSFER_TYPE_e transferType, /* Type of transfer being performed */
1436 dma_addr_t srcData, /* Place to get data to write to device */
1437 dma_addr_t dstData, /* Pointer to device data address */
1438 size_t numBytes /* Number of bytes to transfer to the device */
1440 DMA_Channel_t *channel;
1441 DMA_DeviceAttribute_t *devAttr;
1442 int rc = 0;
1444 channel = HandleToChannel(handle);
1445 if (channel == NULL) {
1446 return -ENODEV;
1449 devAttr = &DMA_gDeviceAttribute[channel->devType];
1451 if (devAttr->config.transferType != transferType) {
1452 return -EINVAL;
1455 /* We keep track of the information about the previous request for this */
1456 /* device, and if the attributes match, then we can use the descriptors we setup */
1457 /* the last time, and not have to reinitialize everything. */
1460 rc =
1461 dma_alloc_descriptors(handle, transferType, srcData,
1462 dstData, numBytes);
1463 if (rc != 0) {
1464 return rc;
1468 /* And kick off the transfer */
1470 devAttr->numBytes = numBytes;
1471 devAttr->transferStartTime = timer_get_tick_count();
1473 dmacHw_initiateTransfer(channel->dmacHwHandle, &devAttr->config,
1474 devAttr->ring.virtAddr);
1476 /* Since we got this far, everything went successfully */
1478 return 0;
1481 EXPORT_SYMBOL(dma_transfer);
1483 /****************************************************************************/
1485 * Set the callback function which will be called when a transfer completes.
1486 * If a NULL callback function is set, then no callback will occur.
1488 * @note @a devHandler will be called from IRQ context.
1490 * @return
1491 * 0 - Success
1492 * -ENODEV - Device handed in is invalid.
1494 /****************************************************************************/
1496 int dma_set_device_handler(DMA_Device_t dev, /* Device to set the callback for. */
1497 DMA_DeviceHandler_t devHandler, /* Function to call when the DMA completes */
1498 void *userData /* Pointer which will be passed to devHandler. */
1500 DMA_DeviceAttribute_t *devAttr;
1501 unsigned long flags;
1503 if (!IsDeviceValid(dev)) {
1504 return -ENODEV;
1506 devAttr = &DMA_gDeviceAttribute[dev];
1508 local_irq_save(flags);
1510 devAttr->userData = userData;
1511 devAttr->devHandler = devHandler;
1513 local_irq_restore(flags);
1515 return 0;
1518 EXPORT_SYMBOL(dma_set_device_handler);