1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2004-2007 Freescale Semiconductor, Inc.
6 /*Main C file for multi-channel DMA API. */
11 #include <MCD_tasksInit.h>
12 #include <MCD_progCheck.h>
14 /********************************************************************/
15 /* This is an API-internal pointer to the DMA's registers */
19 * These are the real and model task tables as generated by the
22 extern TaskTableEntry MCD_realTaskTableSrc[NCHANNELS];
23 extern TaskTableEntry MCD_modelTaskTableSrc[NUMOFVARIANTS];
26 * However, this (usually) gets relocated to on-chip SRAM, at which
27 * point we access them as these tables
29 volatile TaskTableEntry *MCD_taskTable;
30 TaskTableEntry *MCD_modelTaskTable;
33 * MCD_chStatus[] is an array of status indicators for remembering
34 * whether a DMA has ever been attempted on each channel, pausing
37 static int MCD_chStatus[NCHANNELS] = {
38 MCD_NO_DMA, MCD_NO_DMA, MCD_NO_DMA, MCD_NO_DMA,
39 MCD_NO_DMA, MCD_NO_DMA, MCD_NO_DMA, MCD_NO_DMA,
40 MCD_NO_DMA, MCD_NO_DMA, MCD_NO_DMA, MCD_NO_DMA,
41 MCD_NO_DMA, MCD_NO_DMA, MCD_NO_DMA, MCD_NO_DMA
44 /* Prototypes for local functions */
45 static void MCD_memcpy(int *dest, int *src, u32 size);
46 static void MCD_resmActions(int channel);
49 * Buffer descriptors used for storage of progress info for single Dmas
50 * Also used as storage for the DMA for CRCs for single DMAs
51 * Otherwise, the DMA does not parse these buffer descriptors
54 extern MCD_bufDesc MCD_singleBufDescs[NCHANNELS];
56 MCD_bufDesc MCD_singleBufDescs[NCHANNELS];
58 MCD_bufDesc *MCD_relocBuffDesc;
60 /* Defines for the debug control register's functions */
61 #define DBG_CTL_COMP1_TASK (0x00002000)
62 #define DBG_CTL_ENABLE (DBG_CTL_AUTO_ARM | \
66 #define DBG_CTL_DISABLE (DBG_CTL_AUTO_ARM | \
69 #define DBG_KILL_ALL_STAT (0xFFFFFFFF)
71 /* Offset to context save area where progress info is stored */
72 #define CSAVE_OFFSET 10
74 /* Defines for Byte Swapping */
75 #define MCD_BYTE_SWAP_KILLER 0xFFF8888F
76 #define MCD_NO_BYTE_SWAP_ATALL 0x00040000
78 /* Execution Unit Identifiers */
79 #define MAC 0 /* legacy - not used */
80 #define LUAC 1 /* legacy - not used */
81 #define CRC 2 /* legacy - not used */
82 #define LURC 3 /* Logic Unit with CRC */
84 /* Task Identifiers */
85 #define TASK_CHAINNOEU 0
86 #define TASK_SINGLENOEU 1
88 #define TASK_CHAINEU 2
89 #define TASK_SINGLEEU 3
93 #define TASK_CHAINEU 0
94 #define TASK_SINGLEEU 1
100 * Structure to remember which variant is on which channel
103 typedef struct MCD_remVariants_struct MCD_remVariant;
104 struct MCD_remVariants_struct {
105 int remDestRsdIncr[NCHANNELS]; /* -1,0,1 */
106 int remSrcRsdIncr[NCHANNELS]; /* -1,0,1 */
107 s16 remDestIncr[NCHANNELS]; /* DestIncr */
108 s16 remSrcIncr[NCHANNELS]; /* srcIncr */
109 u32 remXferSize[NCHANNELS]; /* xferSize */
112 /* Structure to remember the startDma parameters for each channel */
113 MCD_remVariant MCD_remVariants;
114 /********************************************************************/
115 /* Function: MCD_initDma
116 * Purpose: Initializes the DMA API by setting up a pointer to the DMA
117 * registers, relocating and creating the appropriate task
118 * structures, and setting up some global settings
120 * dmaBarAddr - pointer to the multichannel DMA registers
121 * taskTableDest - location to move DMA task code and structs to
122 * flags - operational parameters
124 * MCD_TABLE_UNALIGNED if taskTableDest is not 512-byte aligned
127 extern u32 MCD_funcDescTab0[];
129 int MCD_initDma(dmaRegs * dmaBarAddr, void *taskTableDest, u32 flags)
132 TaskTableEntry *entryPtr;
134 /* setup the local pointer to register set */
135 MCD_dmaBar = dmaBarAddr;
137 /* do we need to move/create a task table */
138 if ((flags & MCD_RELOC_TASKS) != 0) {
141 /*int *tablePtr = taskTableDest;TBD */
142 int varTabsOffset, funcDescTabsOffset, contextSavesOffset;
143 int taskDescTabsOffset;
144 int taskTableSize, varTabsSize, funcDescTabsSize,
150 /* check if physical address is aligned on 512 byte boundary */
151 if (((u32) taskTableDest & 0x000001ff) != 0)
152 return (MCD_TABLE_UNALIGNED);
154 /* set up local pointer to task Table */
155 MCD_taskTable = taskTableDest;
158 * Create a task table:
159 * - compute aligned base offsets for variable tables and
160 * function descriptor tables, then
161 * - loop through the task table and setup the pointers
162 * - copy over model task table with the the actual task
166 taskTableSize = NCHANNELS * sizeof(TaskTableEntry);
167 /* align variable tables to size */
168 varTabsOffset = taskTableSize + (u32) taskTableDest;
169 if ((varTabsOffset & (VAR_TAB_SIZE - 1)) != 0)
171 (varTabsOffset + VAR_TAB_SIZE) & (~VAR_TAB_SIZE);
172 /* align function descriptor tables */
173 varTabsSize = NCHANNELS * VAR_TAB_SIZE;
174 funcDescTabsOffset = varTabsOffset + varTabsSize;
176 if ((funcDescTabsOffset & (FUNCDESC_TAB_SIZE - 1)) != 0)
178 (funcDescTabsOffset +
179 FUNCDESC_TAB_SIZE) & (~FUNCDESC_TAB_SIZE);
181 funcDescTabsSize = FUNCDESC_TAB_NUM * FUNCDESC_TAB_SIZE;
182 contextSavesOffset = funcDescTabsOffset + funcDescTabsSize;
183 contextSavesSize = (NCHANNELS * CONTEXT_SAVE_SIZE);
185 taskTableSize + varTabsSize + funcDescTabsSize +
188 /* zero the thing out */
189 fixedPtr = (u32 *) taskTableDest;
190 for (i = 0; i < (fixedSize / 4); i++)
193 entryPtr = (TaskTableEntry *) MCD_taskTable;
194 /* set up fixed pointers */
195 for (i = 0; i < NCHANNELS; i++) {
196 /* update ptr to local value */
197 entryPtr[i].varTab = (u32) varTabsOffset;
198 entryPtr[i].FDTandFlags =
199 (u32) funcDescTabsOffset | MCD_TT_FLAGS_DEF;
200 entryPtr[i].contextSaveSpace = (u32) contextSavesOffset;
201 varTabsOffset += VAR_TAB_SIZE;
202 #ifdef MCD_INCLUDE_EU
203 /* if not there is only one, just point to the
205 funcDescTabsOffset += FUNCDESC_TAB_SIZE;
207 contextSavesOffset += CONTEXT_SAVE_SIZE;
209 /* copy over the function descriptor table */
210 for (i = 0; i < FUNCDESC_TAB_NUM; i++) {
211 MCD_memcpy((void *)(entryPtr[i].
212 FDTandFlags & ~MCD_TT_FLAGS_MASK),
213 (void *)MCD_funcDescTab0, FUNCDESC_TAB_SIZE);
216 /* copy model task table to where the context saves stuff
218 MCD_modelTaskTable = (TaskTableEntry *) contextSavesOffset;
220 MCD_memcpy((void *)MCD_modelTaskTable,
221 (void *)MCD_modelTaskTableSrc,
222 NUMOFVARIANTS * sizeof(TaskTableEntry));
224 /* point to local version of model task table */
225 entryPtr = MCD_modelTaskTable;
226 taskDescTabsOffset = (u32) MCD_modelTaskTable +
227 (NUMOFVARIANTS * sizeof(TaskTableEntry));
229 /* copy actual task code and update TDT ptrs in local
231 for (i = 0; i < NUMOFVARIANTS; i++) {
233 entryPtr[i].TDTend - entryPtr[i].TDTstart + 4;
234 MCD_memcpy((void *)taskDescTabsOffset,
235 (void *)entryPtr[i].TDTstart,
237 entryPtr[i].TDTstart = (u32) taskDescTabsOffset;
238 taskDescTabsOffset += taskDescTabSize;
239 entryPtr[i].TDTend = (u32) taskDescTabsOffset - 4;
241 #ifdef MCD_INCLUDE_EU
242 /* Tack single DMA BDs onto end of code so API controls
243 where they are since DMA might write to them */
245 (MCD_bufDesc *) (entryPtr[NUMOFVARIANTS - 1].TDTend + 4);
247 /* DMA does not touch them so they can be wherever and we
248 don't need to waste SRAM on them */
249 MCD_relocBuffDesc = MCD_singleBufDescs;
252 /* point the would-be relocated task tables and the
253 buffer descriptors to the ones the linker generated */
255 if (((u32) MCD_realTaskTableSrc & 0x000001ff) != 0)
256 return (MCD_TABLE_UNALIGNED);
258 /* need to add code to make sure that every thing else is
259 aligned properly TBD. this is problematic if we init
260 more than once or after running tasks, need to add
261 variable to see if we have aleady init'd */
262 entryPtr = MCD_realTaskTableSrc;
263 for (i = 0; i < NCHANNELS; i++) {
264 if (((entryPtr[i].varTab & (VAR_TAB_SIZE - 1)) != 0) ||
266 FDTandFlags & (FUNCDESC_TAB_SIZE - 1)) != 0))
267 return (MCD_TABLE_UNALIGNED);
270 MCD_taskTable = MCD_realTaskTableSrc;
271 MCD_modelTaskTable = MCD_modelTaskTableSrc;
272 MCD_relocBuffDesc = MCD_singleBufDescs;
275 /* Make all channels as totally inactive, and remember them as such: */
277 MCD_dmaBar->taskbar = (u32) MCD_taskTable;
278 for (i = 0; i < NCHANNELS; i++) {
279 MCD_dmaBar->taskControl[i] = 0x0;
280 MCD_chStatus[i] = MCD_NO_DMA;
283 /* Set up pausing mechanism to inactive state: */
284 /* no particular values yet for either comparator registers */
285 MCD_dmaBar->debugComp1 = 0;
286 MCD_dmaBar->debugComp2 = 0;
287 MCD_dmaBar->debugControl = DBG_CTL_DISABLE;
288 MCD_dmaBar->debugStatus = DBG_KILL_ALL_STAT;
290 /* enable or disable commbus prefetch, really need an ifdef or
291 something to keep from trying to set this in the 8220 */
292 if ((flags & MCD_COMM_PREFETCH_EN) != 0)
293 MCD_dmaBar->ptdControl &= ~PTD_CTL_COMM_PREFETCH;
295 MCD_dmaBar->ptdControl |= PTD_CTL_COMM_PREFETCH;
300 /*********************** End of MCD_initDma() ***********************/
302 /********************************************************************/
303 /* Function: MCD_dmaStatus
304 * Purpose: Returns the status of the DMA on the requested channel
305 * Arguments: channel - channel number
306 * Returns: Predefined status indicators
308 int MCD_dmaStatus(int channel)
312 if ((channel < 0) || (channel >= NCHANNELS))
313 return (MCD_CHANNEL_INVALID);
315 tcrValue = MCD_dmaBar->taskControl[channel];
316 if ((tcrValue & TASK_CTL_EN) == 0) { /* nothing running */
317 /* if last reported with task enabled */
318 if (MCD_chStatus[channel] == MCD_RUNNING
319 || MCD_chStatus[channel] == MCD_IDLE)
320 MCD_chStatus[channel] = MCD_DONE;
321 } else { /* something is running */
323 /* There are three possibilities: paused, running or idle. */
324 if (MCD_chStatus[channel] == MCD_RUNNING
325 || MCD_chStatus[channel] == MCD_IDLE) {
326 MCD_dmaBar->ptdDebug = PTD_DBG_TSK_VLD_INIT;
327 /* This register is selected to know which initiator is
328 actually asserted. */
329 if ((MCD_dmaBar->ptdDebug >> channel) & 0x1)
330 MCD_chStatus[channel] = MCD_RUNNING;
332 MCD_chStatus[channel] = MCD_IDLE;
333 /* do not change the status if it is already paused. */
336 return MCD_chStatus[channel];
339 /******************** End of MCD_dmaStatus() ************************/
341 /********************************************************************/
342 /* Function: MCD_startDma
343 * Ppurpose: Starts a particular kind of DMA
345 * srcAddr - the channel on which to run the DMA
346 * srcIncr - the address to move data from, or buffer-descriptor address
347 * destAddr - the amount to increment the source address per transfer
348 * destIncr - the address to move data to
349 * dmaSize - the amount to increment the destination address per transfer
350 * xferSize - the number bytes in of each data movement (1, 2, or 4)
351 * initiator - what device initiates the DMA
352 * priority - priority of the DMA
353 * flags - flags describing the DMA
354 * funcDesc - description of byte swapping, bit swapping, and CRC actions
355 * srcAddrVirt - virtual buffer descriptor address TBD
356 * Returns: MCD_CHANNEL_INVALID if channel is invalid, else MCD_OK
359 int MCD_startDma(int channel, s8 * srcAddr, s16 srcIncr, s8 * destAddr,
360 s16 destIncr, u32 dmaSize, u32 xferSize, u32 initiator,
361 int priority, u32 flags, u32 funcDesc
362 #ifdef MCD_NEED_ADDR_TRANS
367 int srcRsdIncr, destRsdIncr;
371 #ifdef MCD_INCLUDE_EU
375 if ((channel < 0) || (channel >= NCHANNELS))
376 return (MCD_CHANNEL_INVALID);
378 /* tbd - need to determine the proper response to a bad funcDesc when
379 not including EU functions, for now, assign a benign funcDesc, but
380 maybe should return an error */
381 #ifndef MCD_INCLUDE_EU
382 funcDesc = MCD_FUNC_NOEU1;
386 printf("startDma:Setting up params\n");
388 /* Set us up for task-wise priority. We don't technically need to do
389 this on every start, but since the register involved is in the same
390 longword as other registers that users are in control of, setting
391 it more than once is probably preferable. That since the
392 documentation doesn't seem to be completely consistent about the
393 nature of the PTD control register. */
394 MCD_dmaBar->ptdControl |= (u16) 0x8000;
396 /* Not sure what we need to keep here rtm TBD */
398 /* Calculate additional parameters to the regular DMA calls. */
399 srcRsdIncr = srcIncr < 0 ? -1 : (srcIncr > 0 ? 1 : 0);
400 destRsdIncr = destIncr < 0 ? -1 : (destIncr > 0 ? 1 : 0);
402 xferSizeIncr = (xferSize & 0xffff) | 0x20000000;
404 /* Remember for each channel which variant is running. */
405 MCD_remVariants.remSrcRsdIncr[channel] = srcRsdIncr;
406 MCD_remVariants.remDestRsdIncr[channel] = destRsdIncr;
407 MCD_remVariants.remDestIncr[channel] = destIncr;
408 MCD_remVariants.remSrcIncr[channel] = srcIncr;
409 MCD_remVariants.remXferSize[channel] = xferSize;
413 (int *)(MCD_taskTable[channel].contextSaveSpace) + CSAVE_OFFSET +
416 #ifdef MCD_INCLUDE_EU
417 /* may move this to EU specific calls */
419 (u32 *) (MCD_taskTable[channel].FDTandFlags & 0xffffff00);
420 /* Modify the LURC's normal and byte-residue-loop functions according
422 realFuncArray[(LURC * 16)] = xferSize == 4 ?
423 funcDesc : xferSize == 2 ?
424 funcDesc & 0xfffff00f : funcDesc & 0xffff000f;
425 realFuncArray[(LURC * 16 + 1)] =
426 (funcDesc & MCD_BYTE_SWAP_KILLER) | MCD_NO_BYTE_SWAP_ATALL;
428 /* Write the initiator field in the TCR, and also set the
429 initiator-hold bit. Note that,due to a hardware quirk, this could
430 collide with an MDE access to the initiator-register file, so we
431 have to verify that the write reads back correctly. */
433 MCD_dmaBar->taskControl[channel] =
434 (initiator << 8) | TASK_CTL_HIPRITSKEN | TASK_CTL_HLDINITNUM;
436 while (((MCD_dmaBar->taskControl[channel] & 0x1fff) !=
437 ((initiator << 8) | TASK_CTL_HIPRITSKEN | TASK_CTL_HLDINITNUM))
438 && (tcrCount < 1000)) {
440 /*MCD_dmaBar->ptd_tcr[channel] = (initiator << 8) | 0x0020; */
441 MCD_dmaBar->taskControl[channel] =
442 (initiator << 8) | TASK_CTL_HIPRITSKEN |
446 MCD_dmaBar->priority[channel] = (u8) priority & PRIORITY_PRI_MASK;
447 /* should be albe to handle this stuff with only one write to ts reg
449 if (channel < 8 && channel >= 0) {
450 MCD_dmaBar->taskSize0 &= ~(0xf << (7 - channel) * 4);
451 MCD_dmaBar->taskSize0 |=
452 (xferSize & 3) << (((7 - channel) * 4) + 2);
453 MCD_dmaBar->taskSize0 |= (xferSize & 3) << ((7 - channel) * 4);
455 MCD_dmaBar->taskSize1 &= ~(0xf << (15 - channel) * 4);
456 MCD_dmaBar->taskSize1 |=
457 (xferSize & 3) << (((15 - channel) * 4) + 2);
458 MCD_dmaBar->taskSize1 |= (xferSize & 3) << ((15 - channel) * 4);
461 /* setup task table flags/options which mostly control the line
463 MCD_taskTable[channel].FDTandFlags &= ~MCD_TT_FLAGS_MASK;
464 MCD_taskTable[channel].FDTandFlags |= (MCD_TT_FLAGS_MASK & flags);
466 if (flags & MCD_FECTX_DMA) {
467 /* TDTStart and TDTEnd */
468 MCD_taskTable[channel].TDTstart =
469 MCD_modelTaskTable[TASK_FECTX].TDTstart;
470 MCD_taskTable[channel].TDTend =
471 MCD_modelTaskTable[TASK_FECTX].TDTend;
472 MCD_startDmaENetXmit((char *)srcAddr, (char *)srcAddr,
473 (char *)destAddr, MCD_taskTable,
475 } else if (flags & MCD_FECRX_DMA) {
476 /* TDTStart and TDTEnd */
477 MCD_taskTable[channel].TDTstart =
478 MCD_modelTaskTable[TASK_FECRX].TDTstart;
479 MCD_taskTable[channel].TDTend =
480 MCD_modelTaskTable[TASK_FECRX].TDTend;
481 MCD_startDmaENetRcv((char *)srcAddr, (char *)srcAddr,
482 (char *)destAddr, MCD_taskTable,
484 } else if (flags & MCD_SINGLE_DMA) {
485 /* this buffer descriptor is used for storing off initial
486 parameters for later progress query calculation and for the
487 DMA to write the resulting checksum. The DMA does not use
488 this to determine how to operate, that info is passed with
490 MCD_relocBuffDesc[channel].srcAddr = srcAddr;
491 MCD_relocBuffDesc[channel].destAddr = destAddr;
493 /* definitely not its final value */
494 MCD_relocBuffDesc[channel].lastDestAddr = destAddr;
496 MCD_relocBuffDesc[channel].dmaSize = dmaSize;
497 MCD_relocBuffDesc[channel].flags = 0; /* not used */
498 MCD_relocBuffDesc[channel].csumResult = 0; /* not used */
499 MCD_relocBuffDesc[channel].next = 0; /* not used */
501 /* Initialize the progress-querying stuff to show no
503 ((volatile int *)MCD_taskTable[channel].
504 contextSaveSpace)[SRCPTR + CSAVE_OFFSET] = (int)srcAddr;
505 ((volatile int *)MCD_taskTable[channel].
506 contextSaveSpace)[DESTPTR + CSAVE_OFFSET] = (int)destAddr;
507 ((volatile int *)MCD_taskTable[channel].
508 contextSaveSpace)[DCOUNT + CSAVE_OFFSET] = 0;
509 ((volatile int *)MCD_taskTable[channel].
510 contextSaveSpace)[CURRBD + CSAVE_OFFSET] =
511 (u32) & (MCD_relocBuffDesc[channel]);
512 /* tbd - need to keep the user from trying to call the EU
513 routine when MCD_INCLUDE_EU is not defined */
514 if (funcDesc == MCD_FUNC_NOEU1 || funcDesc == MCD_FUNC_NOEU2) {
515 /* TDTStart and TDTEnd */
516 MCD_taskTable[channel].TDTstart =
517 MCD_modelTaskTable[TASK_SINGLENOEU].TDTstart;
518 MCD_taskTable[channel].TDTend =
519 MCD_modelTaskTable[TASK_SINGLENOEU].TDTend;
520 MCD_startDmaSingleNoEu((char *)srcAddr, srcIncr,
521 (char *)destAddr, destIncr,
522 (int)dmaSize, xferSizeIncr,
524 &(MCD_relocBuffDesc[channel]),
525 cSave, MCD_taskTable, channel);
527 /* TDTStart and TDTEnd */
528 MCD_taskTable[channel].TDTstart =
529 MCD_modelTaskTable[TASK_SINGLEEU].TDTstart;
530 MCD_taskTable[channel].TDTend =
531 MCD_modelTaskTable[TASK_SINGLEEU].TDTend;
532 MCD_startDmaSingleEu((char *)srcAddr, srcIncr,
533 (char *)destAddr, destIncr,
534 (int)dmaSize, xferSizeIncr,
536 &(MCD_relocBuffDesc[channel]),
537 cSave, MCD_taskTable, channel);
539 } else { /* chained DMAS */
540 /* Initialize the progress-querying stuff to show no
543 /* (!defined(MCD_NEED_ADDR_TRANS)) */
544 ((volatile int *)MCD_taskTable[channel].
545 contextSaveSpace)[SRCPTR + CSAVE_OFFSET]
546 = (int)((MCD_bufDesc *) srcAddr)->srcAddr;
547 ((volatile int *)MCD_taskTable[channel].
548 contextSaveSpace)[DESTPTR + CSAVE_OFFSET]
549 = (int)((MCD_bufDesc *) srcAddr)->destAddr;
551 /* if using address translation, need the virtual addr of the
553 ((volatile int *)MCD_taskTable[channel].
554 contextSaveSpace)[SRCPTR + CSAVE_OFFSET]
555 = (int)((MCD_bufDesc *) srcAddrVirt)->srcAddr;
556 ((volatile int *)MCD_taskTable[channel].
557 contextSaveSpace)[DESTPTR + CSAVE_OFFSET]
558 = (int)((MCD_bufDesc *) srcAddrVirt)->destAddr;
560 ((volatile int *)MCD_taskTable[channel].
561 contextSaveSpace)[DCOUNT + CSAVE_OFFSET] = 0;
562 ((volatile int *)MCD_taskTable[channel].
563 contextSaveSpace)[CURRBD + CSAVE_OFFSET] = (u32) srcAddr;
565 if (funcDesc == MCD_FUNC_NOEU1 || funcDesc == MCD_FUNC_NOEU2) {
566 /*TDTStart and TDTEnd */
567 MCD_taskTable[channel].TDTstart =
568 MCD_modelTaskTable[TASK_CHAINNOEU].TDTstart;
569 MCD_taskTable[channel].TDTend =
570 MCD_modelTaskTable[TASK_CHAINNOEU].TDTend;
571 MCD_startDmaChainNoEu((int *)srcAddr, srcIncr,
574 MCD_taskTable, channel);
576 /*TDTStart and TDTEnd */
577 MCD_taskTable[channel].TDTstart =
578 MCD_modelTaskTable[TASK_CHAINEU].TDTstart;
579 MCD_taskTable[channel].TDTend =
580 MCD_modelTaskTable[TASK_CHAINEU].TDTend;
581 MCD_startDmaChainEu((int *)srcAddr, srcIncr, destIncr,
582 xferSize, xferSizeIncr, cSave,
583 MCD_taskTable, channel);
586 MCD_chStatus[channel] = MCD_IDLE;
590 /************************ End of MCD_startDma() *********************/
592 /********************************************************************/
593 /* Function: MCD_XferProgrQuery
594 * Purpose: Returns progress of DMA on requested channel
595 * Arguments: channel - channel to retrieve progress for
596 * progRep - pointer to user supplied MCD_XferProg struct
597 * Returns: MCD_CHANNEL_INVALID if channel is invalid, else MCD_OK
600 * MCD_XferProgrQuery() upon completing or after aborting a DMA, or
601 * while the DMA is in progress, this function returns the first
602 * DMA-destination address not (or not yet) used in the DMA. When
603 * encountering a non-ready buffer descriptor, the information for
604 * the last completed descriptor is returned.
606 * MCD_XferProgQuery() has to avoid the possibility of getting
607 * partially-updated information in the event that we should happen
608 * to query DMA progress just as the DMA is updating it. It does that
609 * by taking advantage of the fact context is not saved frequently for
610 * the most part. We therefore read it at least twice until we get the
611 * same information twice in a row.
613 * Because a small, but not insignificant, amount of time is required
614 * to write out the progress-query information, especially upon
615 * completion of the DMA, it would be wise to guarantee some time lag
616 * between successive readings of the progress-query information.
619 /* How many iterations of the loop below to execute to stabilize values */
622 int MCD_XferProgrQuery(int channel, MCD_XferProg * progRep)
624 MCD_XferProg prevRep;
625 int again; /* true if we are to try again to ge
626 consistent results */
627 int i; /* used as a time-waste counter */
628 int destDiffBytes; /* Total no of bytes that we think actually
630 int numIterations; /* number of iterations */
631 int bytesNotXfered; /* bytes that did not get xfered. */
632 s8 *LWAlignedInitDestAddr, *LWAlignedCurrDestAddr;
633 int subModVal, addModVal; /* Mode values to added and subtracted
634 from the final destAddr */
636 if ((channel < 0) || (channel >= NCHANNELS))
637 return (MCD_CHANNEL_INVALID);
639 /* Read a trial value for the progress-reporting values */
640 prevRep.lastSrcAddr =
641 (s8 *) ((volatile int *)MCD_taskTable[channel].
642 contextSaveSpace)[SRCPTR + CSAVE_OFFSET];
643 prevRep.lastDestAddr =
644 (s8 *) ((volatile int *)MCD_taskTable[channel].
645 contextSaveSpace)[DESTPTR + CSAVE_OFFSET];
647 ((volatile int *)MCD_taskTable[channel].contextSaveSpace)[DCOUNT +
649 prevRep.currBufDesc =
650 (MCD_bufDesc *) ((volatile int *)MCD_taskTable[channel].
651 contextSaveSpace)[CURRBD + CSAVE_OFFSET];
652 /* Repeatedly reread those values until they match previous values: */
654 /* Waste a little bit of time to ensure stability: */
655 for (i = 0; i < STABTIME; i++) {
656 /* make sure this loop does something so that it
657 doesn't get optimized out */
660 /* Check them again: */
661 progRep->lastSrcAddr =
662 (s8 *) ((volatile int *)MCD_taskTable[channel].
663 contextSaveSpace)[SRCPTR + CSAVE_OFFSET];
664 progRep->lastDestAddr =
665 (s8 *) ((volatile int *)MCD_taskTable[channel].
666 contextSaveSpace)[DESTPTR + CSAVE_OFFSET];
668 ((volatile int *)MCD_taskTable[channel].
669 contextSaveSpace)[DCOUNT + CSAVE_OFFSET];
670 progRep->currBufDesc =
671 (MCD_bufDesc *) ((volatile int *)MCD_taskTable[channel].
672 contextSaveSpace)[CURRBD + CSAVE_OFFSET];
673 /* See if they match: */
674 if (prevRep.lastSrcAddr != progRep->lastSrcAddr
675 || prevRep.lastDestAddr != progRep->lastDestAddr
676 || prevRep.dmaSize != progRep->dmaSize
677 || prevRep.currBufDesc != progRep->currBufDesc) {
678 /* If they don't match, remember previous values and
680 prevRep.lastSrcAddr = progRep->lastSrcAddr;
681 prevRep.lastDestAddr = progRep->lastDestAddr;
682 prevRep.dmaSize = progRep->dmaSize;
683 prevRep.currBufDesc = progRep->currBufDesc;
687 } while (again == MCD_TRUE);
689 /* Update the dCount, srcAddr and destAddr */
690 /* To calculate dmaCount, we consider destination address. C
691 overs M1,P1,Z for destination */
692 switch (MCD_remVariants.remDestRsdIncr[channel]) {
696 lastDestAddr) & ((MCD_remVariants.remXferSize[channel]) -
699 ((int)progRep->currBufDesc->
700 destAddr) & ((MCD_remVariants.remXferSize[channel]) - 1);
701 LWAlignedInitDestAddr =
702 (progRep->currBufDesc->destAddr) - addModVal;
703 LWAlignedCurrDestAddr = (progRep->lastDestAddr) - subModVal;
704 destDiffBytes = LWAlignedInitDestAddr - LWAlignedCurrDestAddr;
706 (destDiffBytes / MCD_remVariants.remDestIncr[channel]) *
707 (MCD_remVariants.remDestIncr[channel]
708 + MCD_remVariants.remXferSize[channel]);
710 destDiffBytes - bytesNotXfered + addModVal - subModVal;
713 progRep->lastDestAddr = progRep->currBufDesc->destAddr;
716 /* This value has to be subtracted from the final
717 calculated dCount. */
719 ((int)progRep->currBufDesc->
720 destAddr) & ((MCD_remVariants.remXferSize[channel]) - 1);
721 /* These bytes are already in lastDestAddr. */
724 lastDestAddr) & ((MCD_remVariants.remXferSize[channel]) -
726 LWAlignedInitDestAddr =
727 (progRep->currBufDesc->destAddr) - subModVal;
728 LWAlignedCurrDestAddr = (progRep->lastDestAddr) - addModVal;
729 destDiffBytes = (progRep->lastDestAddr - LWAlignedInitDestAddr);
731 (LWAlignedCurrDestAddr -
732 LWAlignedInitDestAddr) /
733 MCD_remVariants.remDestIncr[channel];
735 numIterations * (MCD_remVariants.remDestIncr[channel]
736 - MCD_remVariants.remXferSize[channel]);
737 progRep->dmaSize = destDiffBytes - bytesNotXfered - subModVal;
743 /* This covers M1,P1,Z for source */
744 switch (MCD_remVariants.remSrcRsdIncr[channel]) {
746 progRep->lastSrcAddr =
747 progRep->currBufDesc->srcAddr +
748 (MCD_remVariants.remSrcIncr[channel] *
749 (progRep->dmaSize / MCD_remVariants.remXferSize[channel]));
752 progRep->lastSrcAddr = progRep->currBufDesc->srcAddr;
755 progRep->lastSrcAddr =
756 progRep->currBufDesc->srcAddr +
757 (MCD_remVariants.remSrcIncr[channel] *
758 (progRep->dmaSize / MCD_remVariants.remXferSize[channel]));
767 /******************* End of MCD_XferProgrQuery() ********************/
769 /********************************************************************/
770 /* MCD_resmActions() does the majority of the actions of a DMA resume.
771 * It is called from MCD_killDma() and MCD_resumeDma(). It has to be
772 * a separate function because the kill function has to negate the task
773 * enable before resuming it, but the resume function has to do nothing
774 * if there is no DMA on that channel (i.e., if the enable bit is 0).
776 static void MCD_resmActions(int channel)
778 MCD_dmaBar->debugControl = DBG_CTL_DISABLE;
779 MCD_dmaBar->debugStatus = MCD_dmaBar->debugStatus;
780 /* This register is selected to know which initiator is
781 actually asserted. */
782 MCD_dmaBar->ptdDebug = PTD_DBG_TSK_VLD_INIT;
784 if ((MCD_dmaBar->ptdDebug >> channel) & 0x1)
785 MCD_chStatus[channel] = MCD_RUNNING;
787 MCD_chStatus[channel] = MCD_IDLE;
790 /********************* End of MCD_resmActions() *********************/
792 /********************************************************************/
793 /* Function: MCD_killDma
794 * Purpose: Halt the DMA on the requested channel, without any
795 * intention of resuming the DMA.
796 * Arguments: channel - requested channel
797 * Returns: MCD_CHANNEL_INVALID if channel is invalid, else MCD_OK
800 * A DMA may be killed from any state, including paused state, and it
801 * always goes to the MCD_HALTED state even if it is killed while in
802 * the MCD_NO_DMA or MCD_IDLE states.
804 int MCD_killDma(int channel)
806 /* MCD_XferProg progRep; */
808 if ((channel < 0) || (channel >= NCHANNELS))
809 return (MCD_CHANNEL_INVALID);
811 MCD_dmaBar->taskControl[channel] = 0x0;
812 MCD_resumeDma(channel);
814 * This must be after the write to the TCR so that the task doesn't
815 * start up again momentarily, and before the status assignment so
816 * as to override whatever MCD_resumeDma() may do to the channel
819 MCD_chStatus[channel] = MCD_HALTED;
822 * Update the current buffer descriptor's lastDestAddr field
824 * MCD_XferProgrQuery (channel, &progRep);
825 * progRep.currBufDesc->lastDestAddr = progRep.lastDestAddr;
830 /************************ End of MCD_killDma() **********************/
832 /********************************************************************/
833 /* Function: MCD_continDma
834 * Purpose: Continue a DMA which as stopped due to encountering an
835 * unready buffer descriptor.
836 * Arguments: channel - channel to continue the DMA on
837 * Returns: MCD_CHANNEL_INVALID if channel is invalid, else MCD_OK
840 * This routine does not check to see if there is a task which can
841 * be continued. Also this routine should not be used with single DMAs.
843 int MCD_continDma(int channel)
845 if ((channel < 0) || (channel >= NCHANNELS))
846 return (MCD_CHANNEL_INVALID);
848 MCD_dmaBar->taskControl[channel] |= TASK_CTL_EN;
849 MCD_chStatus[channel] = MCD_RUNNING;
854 /********************** End of MCD_continDma() **********************/
856 /*********************************************************************
857 * MCD_pauseDma() and MCD_resumeDma() below use the DMA's debug unit
858 * to freeze a task and resume it. We freeze a task by breakpointing
859 * on the stated task. That is, not any specific place in the task,
860 * but any time that task executes. In particular, when that task
861 * executes, we want to freeze that task and only that task.
863 * The bits of the debug control register influence interrupts vs.
864 * breakpoints as follows:
865 * - Bits 14 and 0 enable or disable debug functions. If enabled, you
866 * will get the interrupt but you may or may not get a breakpoint.
867 * - Bits 2 and 1 decide whether you also get a breakpoint in addition
870 * The debug unit can do these actions in response to either internally
871 * detected breakpoint conditions from the comparators, or in response
872 * to the external breakpoint pin, or both.
873 * - Bits 14 and 1 perform the above-described functions for
874 * internally-generated conditions, i.e., the debug comparators.
875 * - Bits 0 and 2 perform the above-described functions for external
876 * conditions, i.e., the breakpoint external pin.
878 * Note that, although you "always" get the interrupt when you turn
879 * the debug functions, the interrupt can nevertheless, if desired, be
880 * masked by the corresponding bit in the PTD's IMR. Note also that
881 * this means that bits 14 and 0 must enable debug functions before
882 * bits 1 and 2, respectively, have any effect.
884 * NOTE: It's extremely important to not pause more than one DMA channel
886 ********************************************************************/
888 /********************************************************************/
889 /* Function: MCD_pauseDma
890 * Purpose: Pauses the DMA on a given channel (if any DMA is running
893 * Returns: MCD_CHANNEL_INVALID if channel is invalid, else MCD_OK
895 int MCD_pauseDma(int channel)
897 /* MCD_XferProg progRep; */
899 if ((channel < 0) || (channel >= NCHANNELS))
900 return (MCD_CHANNEL_INVALID);
902 if (MCD_dmaBar->taskControl[channel] & TASK_CTL_EN) {
903 MCD_dmaBar->debugComp1 = channel;
904 MCD_dmaBar->debugControl =
905 DBG_CTL_ENABLE | (1 << (channel + 16));
906 MCD_chStatus[channel] = MCD_PAUSED;
909 * Update the current buffer descriptor's lastDestAddr field
911 * MCD_XferProgrQuery (channel, &progRep);
912 * progRep.currBufDesc->lastDestAddr = progRep.lastDestAddr;
918 /************************* End of MCD_pauseDma() ********************/
920 /********************************************************************/
921 /* Function: MCD_resumeDma
922 * Purpose: Resumes the DMA on a given channel (if any DMA is
923 * running on that channel).
924 * Arguments: channel - channel on which to resume DMA
925 * Returns: MCD_CHANNEL_INVALID if channel is invalid, else MCD_OK
927 int MCD_resumeDma(int channel)
929 if ((channel < 0) || (channel >= NCHANNELS))
930 return (MCD_CHANNEL_INVALID);
932 if (MCD_dmaBar->taskControl[channel] & TASK_CTL_EN)
933 MCD_resmActions(channel);
938 /************************ End of MCD_resumeDma() ********************/
940 /********************************************************************/
941 /* Function: MCD_csumQuery
942 * Purpose: Provide the checksum after performing a non-chained DMA
943 * Arguments: channel - channel to report on
944 * csum - pointer to where to write the checksum/CRC
945 * Returns: MCD_ERROR if the channel is invalid, else MCD_OK
950 int MCD_csumQuery(int channel, u32 * csum)
952 #ifdef MCD_INCLUDE_EU
953 if ((channel < 0) || (channel >= NCHANNELS))
954 return (MCD_CHANNEL_INVALID);
956 *csum = MCD_relocBuffDesc[channel].csumResult;
963 /*********************** End of MCD_resumeDma() *********************/
965 /********************************************************************/
966 /* Function: MCD_getCodeSize
967 * Purpose: Provide the size requirements of the microcoded tasks
968 * Returns: Size in bytes
970 int MCD_getCodeSize(void)
972 #ifdef MCD_INCLUDE_EU
979 /********************** End of MCD_getCodeSize() ********************/
981 /********************************************************************/
982 /* Function: MCD_getVersion
983 * Purpose: Provide the version string and number
984 * Arguments: longVersion - user supplied pointer to a pointer to a char
985 * which points to the version string
986 * Returns: Version number and version string (by reference)
988 char MCD_versionString[] = "Multi-channel DMA API Alpha v0.3 (2004-04-26)";
989 #define MCD_REV_MAJOR 0x00
990 #define MCD_REV_MINOR 0x03
992 int MCD_getVersion(char **longVersion)
994 *longVersion = MCD_versionString;
995 return ((MCD_REV_MAJOR << 8) | MCD_REV_MINOR);
998 /********************** End of MCD_getVersion() *********************/
1000 /********************************************************************/
1001 /* Private version of memcpy()
1002 * Note that everything this is used for is longword-aligned.
1004 static void MCD_memcpy(int *dest, int *src, u32 size)
1008 for (i = 0; i < size; i += sizeof(int), dest++, src++)