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>
38 #include <linux/sched.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 /****************************************************************************/
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
)
73 int limit
= count
- 200;
75 DMA_Channel_t
*channel
;
77 if (down_interruptible(&gDMA
.lock
) < 0) {
81 for (controllerIdx
= 0; controllerIdx
< DMA_NUM_CONTROLLERS
;
83 for (channelIdx
= 0; channelIdx
< DMA_NUM_CHANNELS
;
90 &gDMA
.controller
[controllerIdx
].channel
[channelIdx
];
93 sprintf(buf
+ len
, "%d:%d ", controllerIdx
,
96 if ((channel
->flags
& DMA_CHANNEL_FLAG_IS_DEDICATED
) !=
99 sprintf(buf
+ len
, "Dedicated for %s ",
100 DMA_gDeviceAttribute
[channel
->
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 ");
113 len
+= sprintf(buf
+ len
, "Fifo: 64 ");
116 if ((channel
->flags
& DMA_CHANNEL_FLAG_IN_USE
) != 0) {
118 sprintf(buf
+ len
, "InUse by %s",
119 DMA_gDeviceAttribute
[channel
->
121 #if (DMA_DEBUG_TRACK_RESERVATION)
123 sprintf(buf
+ len
, " (%s:%d)",
128 len
+= sprintf(buf
+ len
, "Avail ");
131 if (channel
->lastDevType
!= DMA_DEVICE_NONE
) {
133 sprintf(buf
+ len
, "Last use: %s ",
134 DMA_gDeviceAttribute
[channel
->
139 len
+= sprintf(buf
+ len
, "\n");
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;
161 if (down_interruptible(&gDMA
.lock
) < 0) {
165 for (devIdx
= 0; devIdx
< DMA_NUM_DEVICE_ENTRIES
; devIdx
++) {
166 DMA_DeviceAttribute_t
*devAttr
= &DMA_gDeviceAttribute
[devIdx
];
168 if (devAttr
->name
== NULL
) {
176 len
+= sprintf(buf
+ len
, "%-12s ", devAttr
->name
);
178 if ((devAttr
->flags
& DMA_DEVICE_FLAG_IS_DEDICATED
) != 0) {
180 sprintf(buf
+ len
, "Dedicated %d:%d ",
181 devAttr
->dedicatedController
,
182 devAttr
->dedicatedChannel
);
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 ");
202 "Xfer #: %Lu Ticks: %Lu Bytes: %Lu DescLen: %u\n",
203 devAttr
->numTransfers
, devAttr
->transferTicks
,
204 devAttr
->transferBytes
,
205 devAttr
->ring
.bytesAllocated
);
215 /****************************************************************************/
217 * Determines if a DMA_Device_t is "valid".
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.
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
)
245 controllerIdx
= CONTROLLER_FROM_HANDLE(handle
);
246 channelIdx
= CHANNEL_FROM_HANDLE(handle
);
248 if ((controllerIdx
> DMA_NUM_CONTROLLERS
)
249 || (channelIdx
> DMA_NUM_CHANNELS
)) {
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
;
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",
280 devAttr
= &DMA_gDeviceAttribute
[channel
->devType
];
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) {
291 "dma_interrupt_handler: devType :%d DMA error (%s)\n",
292 channel
->devType
, devAttr
->name
);
294 devAttr
->numTransfers
++;
295 devAttr
->transferBytes
+= devAttr
->numBytes
;
298 /* Call any installed handler */
300 if (devAttr
->devHandler
!= NULL
) {
301 devAttr
->devHandler(channel
->devType
, irqStatus
,
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.
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)) {
333 ring
->descriptorsAllocated
= 0;
334 ring
->bytesAllocated
= 0;
336 ring
->virtAddr
= dma_alloc_writecombine(NULL
,
340 if (ring
->virtAddr
== NULL
) {
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
;
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
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
) {
398 if (dmacHw_initDescriptor(ring
->virtAddr
,
400 ring
->bytesAllocated
, numDescriptors
) < 0) {
402 "dma_init_descriptor_ring: dmacHw_initDescriptor failed\n");
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
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 */
436 DMA_DeviceAttribute_t
*devAttr
;
438 if (!IsDeviceValid(device
)) {
441 devAttr
= &DMA_gDeviceAttribute
[device
];
443 numDescriptors
= dmacHw_calculateDescriptorCount(&devAttr
->config
,
447 if (numDescriptors
< 0) {
449 "dma_calculate_descriptor_count: dmacHw_calculateDescriptorCount failed\n");
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.
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 */
479 DMA_DeviceAttribute_t
*devAttr
;
481 if (!IsDeviceValid(device
)) {
484 devAttr
= &DMA_gDeviceAttribute
[device
];
486 rc
= dmacHw_setDataDescriptor(&devAttr
->config
,
489 (void *)dstData
, numBytes
);
492 "dma_add_descriptors: dmacHw_setDataDescriptor failed with code: %d\n",
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.
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
)) {
530 devAttr
= &DMA_gDeviceAttribute
[device
];
532 /* Free the previously allocated descriptor ring */
534 dma_free_descriptor_ring(&devAttr
->ring
);
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;
552 EXPORT_SYMBOL(dma_set_device_descriptor_ring
);
554 /****************************************************************************/
556 * Retrieves the descriptor ring associated with a device.
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
)) {
574 devAttr
= &DMA_gDeviceAttribute
[device
];
576 *ring
= devAttr
->ring
;
581 EXPORT_SYMBOL(dma_get_device_descriptor_ring
);
583 /****************************************************************************/
585 * Configures a DMA channel.
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
;
601 channel
= HandleToChannel(handle
);
602 if (channel
== NULL
) {
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");
628 /****************************************************************************/
630 * Initializes all of the data structures associated with the DMA.
632 * >= 0 - Initialization was successful.
634 * -EBUSY - Device is currently being used.
635 * -ENODEV - Device handed in is invalid.
637 /****************************************************************************/
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 */
657 /* Start off by marking all of the DMA channels as shared. */
659 for (controllerIdx
= 0; controllerIdx
< DMA_NUM_CONTROLLERS
;
661 for (channelIdx
= 0; channelIdx
< DMA_NUM_CHANNELS
;
664 &gDMA
.controller
[controllerIdx
].channel
[channelIdx
];
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;
675 channel
->dmacHwHandle
=
676 dmacHw_getChannelHandle(dmacHw_MAKE_CHANNEL_ID
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)) {
698 "DMA Device: %s Can only request NO_ISR for dedicated devices\n",
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
) {
709 "DMA Device: %s DMA Controller %d is out of range\n",
711 devAttr
->dedicatedController
);
716 if (devAttr
->dedicatedChannel
>= DMA_NUM_CHANNELS
) {
718 "DMA Device: %s DMA Channel %d is out of range\n",
720 devAttr
->dedicatedChannel
);
726 MAKE_HANDLE(devAttr
->dedicatedController
,
727 devAttr
->dedicatedChannel
);
728 channel
= HandleToChannel(dedicatedHandle
);
730 if ((channel
->flags
& DMA_CHANNEL_FLAG_IS_DEDICATED
) !=
733 ("DMA Device: %s attempting to use same DMA Controller:Channel (%d:%d) as %s\n",
735 devAttr
->dedicatedController
,
736 devAttr
->dedicatedChannel
,
737 DMA_gDeviceAttribute
[channel
->devType
].
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 */
753 ConfigChannel(dedicatedHandle
);
757 /* Go through and register the interrupt handlers */
759 for (controllerIdx
= 0; controllerIdx
< DMA_NUM_CONTROLLERS
;
761 for (channelIdx
= 0; channelIdx
< DMA_NUM_CHANNELS
;
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
,
771 DMA_DEVICE_NONE
? "" :
772 DMA_gDeviceAttribute
[channel
->devType
].
776 request_irq(IRQ_DMA0C0
+
780 dma_interrupt_handler
,
781 IRQF_DISABLED
, channel
->name
,
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");
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
);
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
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
)
829 DMA_Handle_t
dma_request_channel(DMA_Device_t dev
)
833 DMA_DeviceAttribute_t
*devAttr
;
834 DMA_Channel_t
*channel
;
839 if (down_interruptible(&gDMA
.lock
) < 0) {
843 if ((dev
< 0) || (dev
>= DMA_NUM_DEVICE_ENTRIES
)) {
847 devAttr
= &DMA_gDeviceAttribute
[dev
];
849 #if (DMA_DEBUG_TRACK_RESERVATION)
853 s
= strrchr(fileName
, '/');
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
);
868 if ((devAttr
->flags
& DMA_DEVICE_FLAG_IS_DEDICATED
) != 0) {
869 /* This device has a dedicated channel. */
872 &gDMA
.controller
[devAttr
->dedicatedController
].
873 channel
[devAttr
->dedicatedChannel
];
874 if ((channel
->flags
& DMA_CHANNEL_FLAG_IN_USE
) != 0) {
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
;
887 MAKE_HANDLE(devAttr
->dedicatedController
,
888 devAttr
->dedicatedChannel
);
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
;
900 /* Check to see if we should try on controller 1 first. */
902 controllerIdx
= controllerIdx2
;
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 */
911 flags
& (DMA_DEVICE_FLAG_ON_DMA0
<< controllerIdx
))
914 channelIdx
< DMA_NUM_CHANNELS
;
917 &gDMA
.controller
[controllerIdx
].
922 DMA_CHANNEL_FLAG_IS_DEDICATED
) ==
926 flags
& DMA_CHANNEL_FLAG_IN_USE
)
930 DMA_CHANNEL_FLAG_LARGE_FIFO
)
935 DMA_DEVICE_FLAG_ALLOW_LARGE_FIFO
)
937 /* This channel is a large fifo - don't tie it up */
938 /* with devices that we don't want using it. */
944 DMA_CHANNEL_FLAG_IN_USE
;
945 channel
->devType
= dev
;
947 DMA_DEVICE_FLAG_IN_USE
;
949 #if (DMA_DEBUG_TRACK_RESERVATION)
950 channel
->fileName
= fileName
;
951 channel
->lineNum
= lineNum
;
954 MAKE_HANDLE(controllerIdx
,
957 /* Now that we've reserved the channel - we can go ahead and configure it */
959 if (ConfigChannel(handle
) != 0) {
962 "dma_request_channel: ConfigChannel failed\n");
970 /* No channels are currently available. Let's wait for one to free up. */
975 prepare_to_wait(&gDMA
.freeChannelQ
, &wait
,
979 finish_wait(&gDMA
.freeChannelQ
, &wait
);
981 if (signal_pending(current
)) {
982 /* We don't currently hold gDMA.lock, so we return directly */
988 if (down_interruptible(&gDMA
.lock
)) {
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
);
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. */
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
) {
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
;
1046 wake_up_interruptible(&gDMA
.freeChannelQ
);
1051 EXPORT_SYMBOL(dma_free_channel
);
1053 /****************************************************************************/
1055 * Determines if a given device has been configured as using a shared
1059 * 0 Device uses a dedicated channel
1060 * > zero Device uses a shared channel
1063 /****************************************************************************/
1065 int dma_device_is_channel_shared(DMA_Device_t device
/* Device to check. */
1067 DMA_DeviceAttribute_t
*devAttr
;
1069 if (!IsDeviceValid(device
)) {
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
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
;
1102 size_t ringBytesRequired
;
1105 channel
= HandleToChannel(handle
);
1106 if (channel
== NULL
) {
1110 devAttr
= &DMA_gDeviceAttribute
[channel
->devType
];
1112 if (devAttr
->config
.transferType
!= transferType
) {
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
,
1125 if (numDescriptors
< 0) {
1126 printk(KERN_ERR
"%s: dmacHw_calculateDescriptorCount failed\n",
1131 /* Check to see if we can reuse the existing descriptor ring, or if we need to allocate */
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. */
1145 /* Free the old descriptor ring and allocate a new one. */
1147 dma_free_descriptor_ring(&devAttr
->ring
);
1149 /* And allocate a new one. */
1152 dma_alloc_descriptor_ring(&devAttr
->ring
,
1156 "%s: dma_alloc_descriptor_ring(%d) failed\n",
1157 __func__
, numDescriptors
);
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",
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
,
1183 (void *)dstData
, numBytes
) < 0) {
1184 printk(KERN_ERR
"%s: dmacHw_setDataDescriptor failed\n",
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
;
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.
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
;
1227 size_t ringBytesRequired
;
1230 channel
= HandleToChannel(handle
);
1231 if (channel
== NULL
) {
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) {
1248 numDst2Descriptors
=
1249 dmacHw_calculateDescriptorCount(&devAttr
->config
, (void *)srcData
,
1250 (void *)dstData2
, numBytes
);
1251 if (numDst2Descriptors
< 0) {
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 */
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. */
1271 /* Free the old descriptor ring and allocate a new one. */
1273 dma_free_descriptor_ring(&devAttr
->ring
);
1275 /* And allocate a new one. */
1278 dma_alloc_descriptor_ring(&devAttr
->ring
,
1282 "%s: dma_alloc_descriptor_ring(%d) failed\n",
1283 __func__
, ringBytesRequired
);
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__
);
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
,
1306 (void *)dstData1
, numBytes
) < 0) {
1307 printk(KERN_ERR
"%s: dmacHw_setDataDescriptor 1 failed\n",
1311 if (dmacHw_setDataDescriptor(&devAttr
->config
,
1312 devAttr
->ring
.virtAddr
,
1314 (void *)dstData2
, numBytes
) < 0) {
1315 printk(KERN_ERR
"%s: dmacHw_setDataDescriptor 2 failed\n",
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
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
) {
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 */
1364 EXPORT_SYMBOL(dma_start_transfer
);
1366 /****************************************************************************/
1368 * Stops a previously started DMA transfer.
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
) {
1385 dmacHw_stopTransfer(channel
->dmacHwHandle
);
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
) {
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__
);
1422 EXPORT_SYMBOL(dma_wait_transfer_done
);
1424 /****************************************************************************/
1426 * Initiates a DMA, allocating the descriptors as required.
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
;
1445 channel
= HandleToChannel(handle
);
1446 if (channel
== NULL
) {
1450 devAttr
= &DMA_gDeviceAttribute
[channel
->devType
];
1452 if (devAttr
->config
.transferType
!= transferType
) {
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. */
1462 dma_alloc_descriptors(handle
, transferType
, srcData
,
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 */
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.
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
)) {
1507 devAttr
= &DMA_gDeviceAttribute
[dev
];
1509 local_irq_save(flags
);
1511 devAttr
->userData
= userData
;
1512 devAttr
->devHandler
= devHandler
;
1514 local_irq_restore(flags
);
1519 EXPORT_SYMBOL(dma_set_device_handler
);