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
13 *****************************************************************************/
15 /****************************************************************************/
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>
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 /****************************************************************************/
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
)
72 int limit
= count
- 200;
74 DMA_Channel_t
*channel
;
76 if (down_interruptible(&gDMA
.lock
) < 0) {
80 for (controllerIdx
= 0; controllerIdx
< DMA_NUM_CONTROLLERS
;
82 for (channelIdx
= 0; channelIdx
< DMA_NUM_CHANNELS
;
89 &gDMA
.controller
[controllerIdx
].channel
[channelIdx
];
92 sprintf(buf
+ len
, "%d:%d ", controllerIdx
,
95 if ((channel
->flags
& DMA_CHANNEL_FLAG_IS_DEDICATED
) !=
98 sprintf(buf
+ len
, "Dedicated for %s ",
99 DMA_gDeviceAttribute
[channel
->
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 ");
112 len
+= sprintf(buf
+ len
, "Fifo: 64 ");
115 if ((channel
->flags
& DMA_CHANNEL_FLAG_IN_USE
) != 0) {
117 sprintf(buf
+ len
, "InUse by %s",
118 DMA_gDeviceAttribute
[channel
->
120 #if (DMA_DEBUG_TRACK_RESERVATION)
122 sprintf(buf
+ len
, " (%s:%d)",
127 len
+= sprintf(buf
+ len
, "Avail ");
130 if (channel
->lastDevType
!= DMA_DEVICE_NONE
) {
132 sprintf(buf
+ len
, "Last use: %s ",
133 DMA_gDeviceAttribute
[channel
->
138 len
+= sprintf(buf
+ len
, "\n");
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;
160 if (down_interruptible(&gDMA
.lock
) < 0) {
164 for (devIdx
= 0; devIdx
< DMA_NUM_DEVICE_ENTRIES
; devIdx
++) {
165 DMA_DeviceAttribute_t
*devAttr
= &DMA_gDeviceAttribute
[devIdx
];
167 if (devAttr
->name
== NULL
) {
175 len
+= sprintf(buf
+ len
, "%-12s ", devAttr
->name
);
177 if ((devAttr
->flags
& DMA_DEVICE_FLAG_IS_DEDICATED
) != 0) {
179 sprintf(buf
+ len
, "Dedicated %d:%d ",
180 devAttr
->dedicatedController
,
181 devAttr
->dedicatedChannel
);
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 ");
201 "Xfer #: %Lu Ticks: %Lu Bytes: %Lu DescLen: %u\n",
202 devAttr
->numTransfers
, devAttr
->transferTicks
,
203 devAttr
->transferBytes
,
204 devAttr
->ring
.bytesAllocated
);
214 /****************************************************************************/
216 * Determines if a DMA_Device_t is "valid".
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.
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
)
244 controllerIdx
= CONTROLLER_FROM_HANDLE(handle
);
245 channelIdx
= CHANNEL_FROM_HANDLE(handle
);
247 if ((controllerIdx
> DMA_NUM_CONTROLLERS
)
248 || (channelIdx
> DMA_NUM_CHANNELS
)) {
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
;
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",
279 devAttr
= &DMA_gDeviceAttribute
[channel
->devType
];
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) {
290 "dma_interrupt_handler: devType :%d DMA error (%s)\n",
291 channel
->devType
, devAttr
->name
);
293 devAttr
->numTransfers
++;
294 devAttr
->transferBytes
+= devAttr
->numBytes
;
297 /* Call any installed handler */
299 if (devAttr
->devHandler
!= NULL
) {
300 devAttr
->devHandler(channel
->devType
, irqStatus
,
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.
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)) {
332 ring
->descriptorsAllocated
= 0;
333 ring
->bytesAllocated
= 0;
335 ring
->virtAddr
= dma_alloc_writecombine(NULL
,
339 if (ring
->virtAddr
== NULL
) {
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
;
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
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
) {
397 if (dmacHw_initDescriptor(ring
->virtAddr
,
399 ring
->bytesAllocated
, numDescriptors
) < 0) {
401 "dma_init_descriptor_ring: dmacHw_initDescriptor failed\n");
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
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 */
435 DMA_DeviceAttribute_t
*devAttr
;
437 if (!IsDeviceValid(device
)) {
440 devAttr
= &DMA_gDeviceAttribute
[device
];
442 numDescriptors
= dmacHw_calculateDescriptorCount(&devAttr
->config
,
446 if (numDescriptors
< 0) {
448 "dma_calculate_descriptor_count: dmacHw_calculateDescriptorCount failed\n");
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.
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 */
478 DMA_DeviceAttribute_t
*devAttr
;
480 if (!IsDeviceValid(device
)) {
483 devAttr
= &DMA_gDeviceAttribute
[device
];
485 rc
= dmacHw_setDataDescriptor(&devAttr
->config
,
488 (void *)dstData
, numBytes
);
491 "dma_add_descriptors: dmacHw_setDataDescriptor failed with code: %d\n",
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.
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
)) {
529 devAttr
= &DMA_gDeviceAttribute
[device
];
531 /* Free the previously allocated descriptor ring */
533 dma_free_descriptor_ring(&devAttr
->ring
);
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;
551 EXPORT_SYMBOL(dma_set_device_descriptor_ring
);
553 /****************************************************************************/
555 * Retrieves the descriptor ring associated with a device.
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
)) {
573 devAttr
= &DMA_gDeviceAttribute
[device
];
575 *ring
= devAttr
->ring
;
580 EXPORT_SYMBOL(dma_get_device_descriptor_ring
);
582 /****************************************************************************/
584 * Configures a DMA channel.
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
;
600 channel
= HandleToChannel(handle
);
601 if (channel
== NULL
) {
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");
627 /****************************************************************************/
629 * Initializes all of the data structures associated with the DMA.
631 * >= 0 - Initialization was successful.
633 * -EBUSY - Device is currently being used.
634 * -ENODEV - Device handed in is invalid.
636 /****************************************************************************/
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 */
656 /* Start off by marking all of the DMA channels as shared. */
658 for (controllerIdx
= 0; controllerIdx
< DMA_NUM_CONTROLLERS
;
660 for (channelIdx
= 0; channelIdx
< DMA_NUM_CHANNELS
;
663 &gDMA
.controller
[controllerIdx
].channel
[channelIdx
];
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;
674 channel
->dmacHwHandle
=
675 dmacHw_getChannelHandle(dmacHw_MAKE_CHANNEL_ID
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)) {
697 "DMA Device: %s Can only request NO_ISR for dedicated devices\n",
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
) {
708 "DMA Device: %s DMA Controller %d is out of range\n",
710 devAttr
->dedicatedController
);
715 if (devAttr
->dedicatedChannel
>= DMA_NUM_CHANNELS
) {
717 "DMA Device: %s DMA Channel %d is out of range\n",
719 devAttr
->dedicatedChannel
);
725 MAKE_HANDLE(devAttr
->dedicatedController
,
726 devAttr
->dedicatedChannel
);
727 channel
= HandleToChannel(dedicatedHandle
);
729 if ((channel
->flags
& DMA_CHANNEL_FLAG_IS_DEDICATED
) !=
732 ("DMA Device: %s attempting to use same DMA Controller:Channel (%d:%d) as %s\n",
734 devAttr
->dedicatedController
,
735 devAttr
->dedicatedChannel
,
736 DMA_gDeviceAttribute
[channel
->devType
].
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 */
752 ConfigChannel(dedicatedHandle
);
756 /* Go through and register the interrupt handlers */
758 for (controllerIdx
= 0; controllerIdx
< DMA_NUM_CONTROLLERS
;
760 for (channelIdx
= 0; channelIdx
< DMA_NUM_CHANNELS
;
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
,
770 DMA_DEVICE_NONE
? "" :
771 DMA_gDeviceAttribute
[channel
->devType
].
775 request_irq(IRQ_DMA0C0
+
779 dma_interrupt_handler
,
780 IRQF_DISABLED
, channel
->name
,
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");
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
);
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
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
)
828 DMA_Handle_t
dma_request_channel(DMA_Device_t dev
)
832 DMA_DeviceAttribute_t
*devAttr
;
833 DMA_Channel_t
*channel
;
838 if (down_interruptible(&gDMA
.lock
) < 0) {
842 if ((dev
< 0) || (dev
>= DMA_NUM_DEVICE_ENTRIES
)) {
846 devAttr
= &DMA_gDeviceAttribute
[dev
];
848 #if (DMA_DEBUG_TRACK_RESERVATION)
852 s
= strrchr(fileName
, '/');
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
);
867 if ((devAttr
->flags
& DMA_DEVICE_FLAG_IS_DEDICATED
) != 0) {
868 /* This device has a dedicated channel. */
871 &gDMA
.controller
[devAttr
->dedicatedController
].
872 channel
[devAttr
->dedicatedChannel
];
873 if ((channel
->flags
& DMA_CHANNEL_FLAG_IN_USE
) != 0) {
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
;
886 MAKE_HANDLE(devAttr
->dedicatedController
,
887 devAttr
->dedicatedChannel
);
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
;
899 /* Check to see if we should try on controller 1 first. */
901 controllerIdx
= controllerIdx2
;
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 */
910 flags
& (DMA_DEVICE_FLAG_ON_DMA0
<< controllerIdx
))
913 channelIdx
< DMA_NUM_CHANNELS
;
916 &gDMA
.controller
[controllerIdx
].
921 DMA_CHANNEL_FLAG_IS_DEDICATED
) ==
925 flags
& DMA_CHANNEL_FLAG_IN_USE
)
929 DMA_CHANNEL_FLAG_LARGE_FIFO
)
934 DMA_DEVICE_FLAG_ALLOW_LARGE_FIFO
)
936 /* This channel is a large fifo - don't tie it up */
937 /* with devices that we don't want using it. */
943 DMA_CHANNEL_FLAG_IN_USE
;
944 channel
->devType
= dev
;
946 DMA_DEVICE_FLAG_IN_USE
;
948 #if (DMA_DEBUG_TRACK_RESERVATION)
949 channel
->fileName
= fileName
;
950 channel
->lineNum
= lineNum
;
953 MAKE_HANDLE(controllerIdx
,
956 /* Now that we've reserved the channel - we can go ahead and configure it */
958 if (ConfigChannel(handle
) != 0) {
961 "dma_request_channel: ConfigChannel failed\n");
969 /* No channels are currently available. Let's wait for one to free up. */
974 prepare_to_wait(&gDMA
.freeChannelQ
, &wait
,
978 finish_wait(&gDMA
.freeChannelQ
, &wait
);
980 if (signal_pending(current
)) {
981 /* We don't currently hold gDMA.lock, so we return directly */
987 if (down_interruptible(&gDMA
.lock
)) {
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
);
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. */
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
) {
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
;
1045 wake_up_interruptible(&gDMA
.freeChannelQ
);
1050 EXPORT_SYMBOL(dma_free_channel
);
1052 /****************************************************************************/
1054 * Determines if a given device has been configured as using a shared
1058 * 0 Device uses a dedicated channel
1059 * > zero Device uses a shared channel
1062 /****************************************************************************/
1064 int dma_device_is_channel_shared(DMA_Device_t device
/* Device to check. */
1066 DMA_DeviceAttribute_t
*devAttr
;
1068 if (!IsDeviceValid(device
)) {
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
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
;
1101 size_t ringBytesRequired
;
1104 channel
= HandleToChannel(handle
);
1105 if (channel
== NULL
) {
1109 devAttr
= &DMA_gDeviceAttribute
[channel
->devType
];
1111 if (devAttr
->config
.transferType
!= transferType
) {
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
,
1124 if (numDescriptors
< 0) {
1125 printk(KERN_ERR
"%s: dmacHw_calculateDescriptorCount failed\n",
1130 /* Check to see if we can reuse the existing descriptor ring, or if we need to allocate */
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. */
1144 /* Free the old descriptor ring and allocate a new one. */
1146 dma_free_descriptor_ring(&devAttr
->ring
);
1148 /* And allocate a new one. */
1151 dma_alloc_descriptor_ring(&devAttr
->ring
,
1155 "%s: dma_alloc_descriptor_ring(%d) failed\n",
1156 __func__
, numDescriptors
);
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",
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
,
1182 (void *)dstData
, numBytes
) < 0) {
1183 printk(KERN_ERR
"%s: dmacHw_setDataDescriptor failed\n",
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
;
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.
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
;
1226 size_t ringBytesRequired
;
1229 channel
= HandleToChannel(handle
);
1230 if (channel
== NULL
) {
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) {
1247 numDst2Descriptors
=
1248 dmacHw_calculateDescriptorCount(&devAttr
->config
, (void *)srcData
,
1249 (void *)dstData2
, numBytes
);
1250 if (numDst2Descriptors
< 0) {
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 */
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. */
1270 /* Free the old descriptor ring and allocate a new one. */
1272 dma_free_descriptor_ring(&devAttr
->ring
);
1274 /* And allocate a new one. */
1277 dma_alloc_descriptor_ring(&devAttr
->ring
,
1281 "%s: dma_alloc_descriptor_ring(%d) failed\n",
1282 __func__
, ringBytesRequired
);
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__
);
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
,
1305 (void *)dstData1
, numBytes
) < 0) {
1306 printk(KERN_ERR
"%s: dmacHw_setDataDescriptor 1 failed\n",
1310 if (dmacHw_setDataDescriptor(&devAttr
->config
,
1311 devAttr
->ring
.virtAddr
,
1313 (void *)dstData2
, numBytes
) < 0) {
1314 printk(KERN_ERR
"%s: dmacHw_setDataDescriptor 2 failed\n",
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
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
) {
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 */
1363 EXPORT_SYMBOL(dma_start_transfer
);
1365 /****************************************************************************/
1367 * Stops a previously started DMA transfer.
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
) {
1384 dmacHw_stopTransfer(channel
->dmacHwHandle
);
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
) {
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__
);
1421 EXPORT_SYMBOL(dma_wait_transfer_done
);
1423 /****************************************************************************/
1425 * Initiates a DMA, allocating the descriptors as required.
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
;
1444 channel
= HandleToChannel(handle
);
1445 if (channel
== NULL
) {
1449 devAttr
= &DMA_gDeviceAttribute
[channel
->devType
];
1451 if (devAttr
->config
.transferType
!= transferType
) {
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. */
1461 dma_alloc_descriptors(handle
, transferType
, srcData
,
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 */
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.
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
)) {
1506 devAttr
= &DMA_gDeviceAttribute
[dev
];
1508 local_irq_save(flags
);
1510 devAttr
->userData
= userData
;
1511 devAttr
->devHandler
= devHandler
;
1513 local_irq_restore(flags
);
1518 EXPORT_SYMBOL(dma_set_device_handler
);