MCD_dmaApi.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  1. /*
  2. * Copyright (C) 2004-2007 Freescale Semiconductor, Inc.
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. /*Main C file for multi-channel DMA API. */
  7. #include <common.h>
  8. #include <MCD_dma.h>
  9. #include <MCD_tasksInit.h>
  10. #include <MCD_progCheck.h>
  11. /********************************************************************/
  12. /* This is an API-internal pointer to the DMA's registers */
  13. dmaRegs *MCD_dmaBar;
  14. /*
  15. * These are the real and model task tables as generated by the
  16. * build process
  17. */
  18. extern TaskTableEntry MCD_realTaskTableSrc[NCHANNELS];
  19. extern TaskTableEntry MCD_modelTaskTableSrc[NUMOFVARIANTS];
  20. /*
  21. * However, this (usually) gets relocated to on-chip SRAM, at which
  22. * point we access them as these tables
  23. */
  24. volatile TaskTableEntry *MCD_taskTable;
  25. TaskTableEntry *MCD_modelTaskTable;
  26. /*
  27. * MCD_chStatus[] is an array of status indicators for remembering
  28. * whether a DMA has ever been attempted on each channel, pausing
  29. * status, etc.
  30. */
  31. static int MCD_chStatus[NCHANNELS] = {
  32. MCD_NO_DMA, MCD_NO_DMA, MCD_NO_DMA, MCD_NO_DMA,
  33. MCD_NO_DMA, MCD_NO_DMA, MCD_NO_DMA, MCD_NO_DMA,
  34. MCD_NO_DMA, MCD_NO_DMA, MCD_NO_DMA, MCD_NO_DMA,
  35. MCD_NO_DMA, MCD_NO_DMA, MCD_NO_DMA, MCD_NO_DMA
  36. };
  37. /* Prototypes for local functions */
  38. static void MCD_memcpy(int *dest, int *src, u32 size);
  39. static void MCD_resmActions(int channel);
  40. /*
  41. * Buffer descriptors used for storage of progress info for single Dmas
  42. * Also used as storage for the DMA for CRCs for single DMAs
  43. * Otherwise, the DMA does not parse these buffer descriptors
  44. */
  45. #ifdef MCD_INCLUDE_EU
  46. extern MCD_bufDesc MCD_singleBufDescs[NCHANNELS];
  47. #else
  48. MCD_bufDesc MCD_singleBufDescs[NCHANNELS];
  49. #endif
  50. MCD_bufDesc *MCD_relocBuffDesc;
  51. /* Defines for the debug control register's functions */
  52. #define DBG_CTL_COMP1_TASK (0x00002000)
  53. #define DBG_CTL_ENABLE (DBG_CTL_AUTO_ARM | \
  54. DBG_CTL_BREAK | \
  55. DBG_CTL_INT_BREAK | \
  56. DBG_CTL_COMP1_TASK)
  57. #define DBG_CTL_DISABLE (DBG_CTL_AUTO_ARM | \
  58. DBG_CTL_INT_BREAK | \
  59. DBG_CTL_COMP1_TASK)
  60. #define DBG_KILL_ALL_STAT (0xFFFFFFFF)
  61. /* Offset to context save area where progress info is stored */
  62. #define CSAVE_OFFSET 10
  63. /* Defines for Byte Swapping */
  64. #define MCD_BYTE_SWAP_KILLER 0xFFF8888F
  65. #define MCD_NO_BYTE_SWAP_ATALL 0x00040000
  66. /* Execution Unit Identifiers */
  67. #define MAC 0 /* legacy - not used */
  68. #define LUAC 1 /* legacy - not used */
  69. #define CRC 2 /* legacy - not used */
  70. #define LURC 3 /* Logic Unit with CRC */
  71. /* Task Identifiers */
  72. #define TASK_CHAINNOEU 0
  73. #define TASK_SINGLENOEU 1
  74. #ifdef MCD_INCLUDE_EU
  75. #define TASK_CHAINEU 2
  76. #define TASK_SINGLEEU 3
  77. #define TASK_FECRX 4
  78. #define TASK_FECTX 5
  79. #else
  80. #define TASK_CHAINEU 0
  81. #define TASK_SINGLEEU 1
  82. #define TASK_FECRX 2
  83. #define TASK_FECTX 3
  84. #endif
  85. /*
  86. * Structure to remember which variant is on which channel
  87. * TBD- need this?
  88. */
  89. typedef struct MCD_remVariants_struct MCD_remVariant;
  90. struct MCD_remVariants_struct {
  91. int remDestRsdIncr[NCHANNELS]; /* -1,0,1 */
  92. int remSrcRsdIncr[NCHANNELS]; /* -1,0,1 */
  93. s16 remDestIncr[NCHANNELS]; /* DestIncr */
  94. s16 remSrcIncr[NCHANNELS]; /* srcIncr */
  95. u32 remXferSize[NCHANNELS]; /* xferSize */
  96. };
  97. /* Structure to remember the startDma parameters for each channel */
  98. MCD_remVariant MCD_remVariants;
  99. /********************************************************************/
  100. /* Function: MCD_initDma
  101. * Purpose: Initializes the DMA API by setting up a pointer to the DMA
  102. * registers, relocating and creating the appropriate task
  103. * structures, and setting up some global settings
  104. * Arguments:
  105. * dmaBarAddr - pointer to the multichannel DMA registers
  106. * taskTableDest - location to move DMA task code and structs to
  107. * flags - operational parameters
  108. * Return Value:
  109. * MCD_TABLE_UNALIGNED if taskTableDest is not 512-byte aligned
  110. * MCD_OK otherwise
  111. */
  112. extern u32 MCD_funcDescTab0[];
  113. int MCD_initDma(dmaRegs * dmaBarAddr, void *taskTableDest, u32 flags)
  114. {
  115. int i;
  116. TaskTableEntry *entryPtr;
  117. /* setup the local pointer to register set */
  118. MCD_dmaBar = dmaBarAddr;
  119. /* do we need to move/create a task table */
  120. if ((flags & MCD_RELOC_TASKS) != 0) {
  121. int fixedSize;
  122. u32 *fixedPtr;
  123. /*int *tablePtr = taskTableDest;TBD */
  124. int varTabsOffset, funcDescTabsOffset, contextSavesOffset;
  125. int taskDescTabsOffset;
  126. int taskTableSize, varTabsSize, funcDescTabsSize,
  127. contextSavesSize;
  128. int taskDescTabSize;
  129. int i;
  130. /* check if physical address is aligned on 512 byte boundary */
  131. if (((u32) taskTableDest & 0x000001ff) != 0)
  132. return (MCD_TABLE_UNALIGNED);
  133. /* set up local pointer to task Table */
  134. MCD_taskTable = taskTableDest;
  135. /*
  136. * Create a task table:
  137. * - compute aligned base offsets for variable tables and
  138. * function descriptor tables, then
  139. * - loop through the task table and setup the pointers
  140. * - copy over model task table with the the actual task
  141. * descriptor tables
  142. */
  143. taskTableSize = NCHANNELS * sizeof(TaskTableEntry);
  144. /* align variable tables to size */
  145. varTabsOffset = taskTableSize + (u32) taskTableDest;
  146. if ((varTabsOffset & (VAR_TAB_SIZE - 1)) != 0)
  147. varTabsOffset =
  148. (varTabsOffset + VAR_TAB_SIZE) & (~VAR_TAB_SIZE);
  149. /* align function descriptor tables */
  150. varTabsSize = NCHANNELS * VAR_TAB_SIZE;
  151. funcDescTabsOffset = varTabsOffset + varTabsSize;
  152. if ((funcDescTabsOffset & (FUNCDESC_TAB_SIZE - 1)) != 0)
  153. funcDescTabsOffset =
  154. (funcDescTabsOffset +
  155. FUNCDESC_TAB_SIZE) & (~FUNCDESC_TAB_SIZE);
  156. funcDescTabsSize = FUNCDESC_TAB_NUM * FUNCDESC_TAB_SIZE;
  157. contextSavesOffset = funcDescTabsOffset + funcDescTabsSize;
  158. contextSavesSize = (NCHANNELS * CONTEXT_SAVE_SIZE);
  159. fixedSize =
  160. taskTableSize + varTabsSize + funcDescTabsSize +
  161. contextSavesSize;
  162. /* zero the thing out */
  163. fixedPtr = (u32 *) taskTableDest;
  164. for (i = 0; i < (fixedSize / 4); i++)
  165. fixedPtr[i] = 0;
  166. entryPtr = (TaskTableEntry *) MCD_taskTable;
  167. /* set up fixed pointers */
  168. for (i = 0; i < NCHANNELS; i++) {
  169. /* update ptr to local value */
  170. entryPtr[i].varTab = (u32) varTabsOffset;
  171. entryPtr[i].FDTandFlags =
  172. (u32) funcDescTabsOffset | MCD_TT_FLAGS_DEF;
  173. entryPtr[i].contextSaveSpace = (u32) contextSavesOffset;
  174. varTabsOffset += VAR_TAB_SIZE;
  175. #ifdef MCD_INCLUDE_EU
  176. /* if not there is only one, just point to the
  177. same one */
  178. funcDescTabsOffset += FUNCDESC_TAB_SIZE;
  179. #endif
  180. contextSavesOffset += CONTEXT_SAVE_SIZE;
  181. }
  182. /* copy over the function descriptor table */
  183. for (i = 0; i < FUNCDESC_TAB_NUM; i++) {
  184. MCD_memcpy((void *)(entryPtr[i].
  185. FDTandFlags & ~MCD_TT_FLAGS_MASK),
  186. (void *)MCD_funcDescTab0, FUNCDESC_TAB_SIZE);
  187. }
  188. /* copy model task table to where the context saves stuff
  189. leaves off */
  190. MCD_modelTaskTable = (TaskTableEntry *) contextSavesOffset;
  191. MCD_memcpy((void *)MCD_modelTaskTable,
  192. (void *)MCD_modelTaskTableSrc,
  193. NUMOFVARIANTS * sizeof(TaskTableEntry));
  194. /* point to local version of model task table */
  195. entryPtr = MCD_modelTaskTable;
  196. taskDescTabsOffset = (u32) MCD_modelTaskTable +
  197. (NUMOFVARIANTS * sizeof(TaskTableEntry));
  198. /* copy actual task code and update TDT ptrs in local
  199. model task table */
  200. for (i = 0; i < NUMOFVARIANTS; i++) {
  201. taskDescTabSize =
  202. entryPtr[i].TDTend - entryPtr[i].TDTstart + 4;
  203. MCD_memcpy((void *)taskDescTabsOffset,
  204. (void *)entryPtr[i].TDTstart,
  205. taskDescTabSize);
  206. entryPtr[i].TDTstart = (u32) taskDescTabsOffset;
  207. taskDescTabsOffset += taskDescTabSize;
  208. entryPtr[i].TDTend = (u32) taskDescTabsOffset - 4;
  209. }
  210. #ifdef MCD_INCLUDE_EU
  211. /* Tack single DMA BDs onto end of code so API controls
  212. where they are since DMA might write to them */
  213. MCD_relocBuffDesc =
  214. (MCD_bufDesc *) (entryPtr[NUMOFVARIANTS - 1].TDTend + 4);
  215. #else
  216. /* DMA does not touch them so they can be wherever and we
  217. don't need to waste SRAM on them */
  218. MCD_relocBuffDesc = MCD_singleBufDescs;
  219. #endif
  220. } else {
  221. /* point the would-be relocated task tables and the
  222. buffer descriptors to the ones the linker generated */
  223. if (((u32) MCD_realTaskTableSrc & 0x000001ff) != 0)
  224. return (MCD_TABLE_UNALIGNED);
  225. /* need to add code to make sure that every thing else is
  226. aligned properly TBD. this is problematic if we init
  227. more than once or after running tasks, need to add
  228. variable to see if we have aleady init'd */
  229. entryPtr = MCD_realTaskTableSrc;
  230. for (i = 0; i < NCHANNELS; i++) {
  231. if (((entryPtr[i].varTab & (VAR_TAB_SIZE - 1)) != 0) ||
  232. ((entryPtr[i].
  233. FDTandFlags & (FUNCDESC_TAB_SIZE - 1)) != 0))
  234. return (MCD_TABLE_UNALIGNED);
  235. }
  236. MCD_taskTable = MCD_realTaskTableSrc;
  237. MCD_modelTaskTable = MCD_modelTaskTableSrc;
  238. MCD_relocBuffDesc = MCD_singleBufDescs;
  239. }
  240. /* Make all channels as totally inactive, and remember them as such: */
  241. MCD_dmaBar->taskbar = (u32) MCD_taskTable;
  242. for (i = 0; i < NCHANNELS; i++) {
  243. MCD_dmaBar->taskControl[i] = 0x0;
  244. MCD_chStatus[i] = MCD_NO_DMA;
  245. }
  246. /* Set up pausing mechanism to inactive state: */
  247. /* no particular values yet for either comparator registers */
  248. MCD_dmaBar->debugComp1 = 0;
  249. MCD_dmaBar->debugComp2 = 0;
  250. MCD_dmaBar->debugControl = DBG_CTL_DISABLE;
  251. MCD_dmaBar->debugStatus = DBG_KILL_ALL_STAT;
  252. /* enable or disable commbus prefetch, really need an ifdef or
  253. something to keep from trying to set this in the 8220 */
  254. if ((flags & MCD_COMM_PREFETCH_EN) != 0)
  255. MCD_dmaBar->ptdControl &= ~PTD_CTL_COMM_PREFETCH;
  256. else
  257. MCD_dmaBar->ptdControl |= PTD_CTL_COMM_PREFETCH;
  258. return (MCD_OK);
  259. }
  260. /*********************** End of MCD_initDma() ***********************/
  261. /********************************************************************/
  262. /* Function: MCD_dmaStatus
  263. * Purpose: Returns the status of the DMA on the requested channel
  264. * Arguments: channel - channel number
  265. * Returns: Predefined status indicators
  266. */
  267. int MCD_dmaStatus(int channel)
  268. {
  269. u16 tcrValue;
  270. if ((channel < 0) || (channel >= NCHANNELS))
  271. return (MCD_CHANNEL_INVALID);
  272. tcrValue = MCD_dmaBar->taskControl[channel];
  273. if ((tcrValue & TASK_CTL_EN) == 0) { /* nothing running */
  274. /* if last reported with task enabled */
  275. if (MCD_chStatus[channel] == MCD_RUNNING
  276. || MCD_chStatus[channel] == MCD_IDLE)
  277. MCD_chStatus[channel] = MCD_DONE;
  278. } else { /* something is running */
  279. /* There are three possibilities: paused, running or idle. */
  280. if (MCD_chStatus[channel] == MCD_RUNNING
  281. || MCD_chStatus[channel] == MCD_IDLE) {
  282. MCD_dmaBar->ptdDebug = PTD_DBG_TSK_VLD_INIT;
  283. /* This register is selected to know which initiator is
  284. actually asserted. */
  285. if ((MCD_dmaBar->ptdDebug >> channel) & 0x1)
  286. MCD_chStatus[channel] = MCD_RUNNING;
  287. else
  288. MCD_chStatus[channel] = MCD_IDLE;
  289. /* do not change the status if it is already paused. */
  290. }
  291. }
  292. return MCD_chStatus[channel];
  293. }
  294. /******************** End of MCD_dmaStatus() ************************/
  295. /********************************************************************/
  296. /* Function: MCD_startDma
  297. * Ppurpose: Starts a particular kind of DMA
  298. * Arguments:
  299. * srcAddr - the channel on which to run the DMA
  300. * srcIncr - the address to move data from, or buffer-descriptor address
  301. * destAddr - the amount to increment the source address per transfer
  302. * destIncr - the address to move data to
  303. * dmaSize - the amount to increment the destination address per transfer
  304. * xferSize - the number bytes in of each data movement (1, 2, or 4)
  305. * initiator - what device initiates the DMA
  306. * priority - priority of the DMA
  307. * flags - flags describing the DMA
  308. * funcDesc - description of byte swapping, bit swapping, and CRC actions
  309. * srcAddrVirt - virtual buffer descriptor address TBD
  310. * Returns: MCD_CHANNEL_INVALID if channel is invalid, else MCD_OK
  311. */
  312. int MCD_startDma(int channel, s8 * srcAddr, s16 srcIncr, s8 * destAddr,
  313. s16 destIncr, u32 dmaSize, u32 xferSize, u32 initiator,
  314. int priority, u32 flags, u32 funcDesc
  315. #ifdef MCD_NEED_ADDR_TRANS
  316. s8 * srcAddrVirt
  317. #endif
  318. )
  319. {
  320. int srcRsdIncr, destRsdIncr;
  321. int *cSave;
  322. short xferSizeIncr;
  323. int tcrCount = 0;
  324. #ifdef MCD_INCLUDE_EU
  325. u32 *realFuncArray;
  326. #endif
  327. if ((channel < 0) || (channel >= NCHANNELS))
  328. return (MCD_CHANNEL_INVALID);
  329. /* tbd - need to determine the proper response to a bad funcDesc when
  330. not including EU functions, for now, assign a benign funcDesc, but
  331. maybe should return an error */
  332. #ifndef MCD_INCLUDE_EU
  333. funcDesc = MCD_FUNC_NOEU1;
  334. #endif
  335. #ifdef MCD_DEBUG
  336. printf("startDma:Setting up params\n");
  337. #endif
  338. /* Set us up for task-wise priority. We don't technically need to do
  339. this on every start, but since the register involved is in the same
  340. longword as other registers that users are in control of, setting
  341. it more than once is probably preferable. That since the
  342. documentation doesn't seem to be completely consistent about the
  343. nature of the PTD control register. */
  344. MCD_dmaBar->ptdControl |= (u16) 0x8000;
  345. /* Not sure what we need to keep here rtm TBD */
  346. #if 1
  347. /* Calculate additional parameters to the regular DMA calls. */
  348. srcRsdIncr = srcIncr < 0 ? -1 : (srcIncr > 0 ? 1 : 0);
  349. destRsdIncr = destIncr < 0 ? -1 : (destIncr > 0 ? 1 : 0);
  350. xferSizeIncr = (xferSize & 0xffff) | 0x20000000;
  351. /* Remember for each channel which variant is running. */
  352. MCD_remVariants.remSrcRsdIncr[channel] = srcRsdIncr;
  353. MCD_remVariants.remDestRsdIncr[channel] = destRsdIncr;
  354. MCD_remVariants.remDestIncr[channel] = destIncr;
  355. MCD_remVariants.remSrcIncr[channel] = srcIncr;
  356. MCD_remVariants.remXferSize[channel] = xferSize;
  357. #endif
  358. cSave =
  359. (int *)(MCD_taskTable[channel].contextSaveSpace) + CSAVE_OFFSET +
  360. CURRBD;
  361. #ifdef MCD_INCLUDE_EU
  362. /* may move this to EU specific calls */
  363. realFuncArray =
  364. (u32 *) (MCD_taskTable[channel].FDTandFlags & 0xffffff00);
  365. /* Modify the LURC's normal and byte-residue-loop functions according
  366. to parameter. */
  367. realFuncArray[(LURC * 16)] = xferSize == 4 ?
  368. funcDesc : xferSize == 2 ?
  369. funcDesc & 0xfffff00f : funcDesc & 0xffff000f;
  370. realFuncArray[(LURC * 16 + 1)] =
  371. (funcDesc & MCD_BYTE_SWAP_KILLER) | MCD_NO_BYTE_SWAP_ATALL;
  372. #endif
  373. /* Write the initiator field in the TCR, and also set the
  374. initiator-hold bit. Note that,due to a hardware quirk, this could
  375. collide with an MDE access to the initiator-register file, so we
  376. have to verify that the write reads back correctly. */
  377. MCD_dmaBar->taskControl[channel] =
  378. (initiator << 8) | TASK_CTL_HIPRITSKEN | TASK_CTL_HLDINITNUM;
  379. while (((MCD_dmaBar->taskControl[channel] & 0x1fff) !=
  380. ((initiator << 8) | TASK_CTL_HIPRITSKEN | TASK_CTL_HLDINITNUM))
  381. && (tcrCount < 1000)) {
  382. tcrCount++;
  383. /*MCD_dmaBar->ptd_tcr[channel] = (initiator << 8) | 0x0020; */
  384. MCD_dmaBar->taskControl[channel] =
  385. (initiator << 8) | TASK_CTL_HIPRITSKEN |
  386. TASK_CTL_HLDINITNUM;
  387. }
  388. MCD_dmaBar->priority[channel] = (u8) priority & PRIORITY_PRI_MASK;
  389. /* should be albe to handle this stuff with only one write to ts reg
  390. - tbd */
  391. if (channel < 8 && channel >= 0) {
  392. MCD_dmaBar->taskSize0 &= ~(0xf << (7 - channel) * 4);
  393. MCD_dmaBar->taskSize0 |=
  394. (xferSize & 3) << (((7 - channel) * 4) + 2);
  395. MCD_dmaBar->taskSize0 |= (xferSize & 3) << ((7 - channel) * 4);
  396. } else {
  397. MCD_dmaBar->taskSize1 &= ~(0xf << (15 - channel) * 4);
  398. MCD_dmaBar->taskSize1 |=
  399. (xferSize & 3) << (((15 - channel) * 4) + 2);
  400. MCD_dmaBar->taskSize1 |= (xferSize & 3) << ((15 - channel) * 4);
  401. }
  402. /* setup task table flags/options which mostly control the line
  403. buffers */
  404. MCD_taskTable[channel].FDTandFlags &= ~MCD_TT_FLAGS_MASK;
  405. MCD_taskTable[channel].FDTandFlags |= (MCD_TT_FLAGS_MASK & flags);
  406. if (flags & MCD_FECTX_DMA) {
  407. /* TDTStart and TDTEnd */
  408. MCD_taskTable[channel].TDTstart =
  409. MCD_modelTaskTable[TASK_FECTX].TDTstart;
  410. MCD_taskTable[channel].TDTend =
  411. MCD_modelTaskTable[TASK_FECTX].TDTend;
  412. MCD_startDmaENetXmit((char *)srcAddr, (char *)srcAddr,
  413. (char *)destAddr, MCD_taskTable,
  414. channel);
  415. } else if (flags & MCD_FECRX_DMA) {
  416. /* TDTStart and TDTEnd */
  417. MCD_taskTable[channel].TDTstart =
  418. MCD_modelTaskTable[TASK_FECRX].TDTstart;
  419. MCD_taskTable[channel].TDTend =
  420. MCD_modelTaskTable[TASK_FECRX].TDTend;
  421. MCD_startDmaENetRcv((char *)srcAddr, (char *)srcAddr,
  422. (char *)destAddr, MCD_taskTable,
  423. channel);
  424. } else if (flags & MCD_SINGLE_DMA) {
  425. /* this buffer descriptor is used for storing off initial
  426. parameters for later progress query calculation and for the
  427. DMA to write the resulting checksum. The DMA does not use
  428. this to determine how to operate, that info is passed with
  429. the init routine */
  430. MCD_relocBuffDesc[channel].srcAddr = srcAddr;
  431. MCD_relocBuffDesc[channel].destAddr = destAddr;
  432. /* definitely not its final value */
  433. MCD_relocBuffDesc[channel].lastDestAddr = destAddr;
  434. MCD_relocBuffDesc[channel].dmaSize = dmaSize;
  435. MCD_relocBuffDesc[channel].flags = 0; /* not used */
  436. MCD_relocBuffDesc[channel].csumResult = 0; /* not used */
  437. MCD_relocBuffDesc[channel].next = 0; /* not used */
  438. /* Initialize the progress-querying stuff to show no
  439. progress: */
  440. ((volatile int *)MCD_taskTable[channel].
  441. contextSaveSpace)[SRCPTR + CSAVE_OFFSET] = (int)srcAddr;
  442. ((volatile int *)MCD_taskTable[channel].
  443. contextSaveSpace)[DESTPTR + CSAVE_OFFSET] = (int)destAddr;
  444. ((volatile int *)MCD_taskTable[channel].
  445. contextSaveSpace)[DCOUNT + CSAVE_OFFSET] = 0;
  446. ((volatile int *)MCD_taskTable[channel].
  447. contextSaveSpace)[CURRBD + CSAVE_OFFSET] =
  448. (u32) & (MCD_relocBuffDesc[channel]);
  449. /* tbd - need to keep the user from trying to call the EU
  450. routine when MCD_INCLUDE_EU is not defined */
  451. if (funcDesc == MCD_FUNC_NOEU1 || funcDesc == MCD_FUNC_NOEU2) {
  452. /* TDTStart and TDTEnd */
  453. MCD_taskTable[channel].TDTstart =
  454. MCD_modelTaskTable[TASK_SINGLENOEU].TDTstart;
  455. MCD_taskTable[channel].TDTend =
  456. MCD_modelTaskTable[TASK_SINGLENOEU].TDTend;
  457. MCD_startDmaSingleNoEu((char *)srcAddr, srcIncr,
  458. (char *)destAddr, destIncr,
  459. (int)dmaSize, xferSizeIncr,
  460. flags, (int *)
  461. &(MCD_relocBuffDesc[channel]),
  462. cSave, MCD_taskTable, channel);
  463. } else {
  464. /* TDTStart and TDTEnd */
  465. MCD_taskTable[channel].TDTstart =
  466. MCD_modelTaskTable[TASK_SINGLEEU].TDTstart;
  467. MCD_taskTable[channel].TDTend =
  468. MCD_modelTaskTable[TASK_SINGLEEU].TDTend;
  469. MCD_startDmaSingleEu((char *)srcAddr, srcIncr,
  470. (char *)destAddr, destIncr,
  471. (int)dmaSize, xferSizeIncr,
  472. flags, (int *)
  473. &(MCD_relocBuffDesc[channel]),
  474. cSave, MCD_taskTable, channel);
  475. }
  476. } else { /* chained DMAS */
  477. /* Initialize the progress-querying stuff to show no
  478. progress: */
  479. #if 1
  480. /* (!defined(MCD_NEED_ADDR_TRANS)) */
  481. ((volatile int *)MCD_taskTable[channel].
  482. contextSaveSpace)[SRCPTR + CSAVE_OFFSET]
  483. = (int)((MCD_bufDesc *) srcAddr)->srcAddr;
  484. ((volatile int *)MCD_taskTable[channel].
  485. contextSaveSpace)[DESTPTR + CSAVE_OFFSET]
  486. = (int)((MCD_bufDesc *) srcAddr)->destAddr;
  487. #else
  488. /* if using address translation, need the virtual addr of the
  489. first buffdesc */
  490. ((volatile int *)MCD_taskTable[channel].
  491. contextSaveSpace)[SRCPTR + CSAVE_OFFSET]
  492. = (int)((MCD_bufDesc *) srcAddrVirt)->srcAddr;
  493. ((volatile int *)MCD_taskTable[channel].
  494. contextSaveSpace)[DESTPTR + CSAVE_OFFSET]
  495. = (int)((MCD_bufDesc *) srcAddrVirt)->destAddr;
  496. #endif
  497. ((volatile int *)MCD_taskTable[channel].
  498. contextSaveSpace)[DCOUNT + CSAVE_OFFSET] = 0;
  499. ((volatile int *)MCD_taskTable[channel].
  500. contextSaveSpace)[CURRBD + CSAVE_OFFSET] = (u32) srcAddr;
  501. if (funcDesc == MCD_FUNC_NOEU1 || funcDesc == MCD_FUNC_NOEU2) {
  502. /*TDTStart and TDTEnd */
  503. MCD_taskTable[channel].TDTstart =
  504. MCD_modelTaskTable[TASK_CHAINNOEU].TDTstart;
  505. MCD_taskTable[channel].TDTend =
  506. MCD_modelTaskTable[TASK_CHAINNOEU].TDTend;
  507. MCD_startDmaChainNoEu((int *)srcAddr, srcIncr,
  508. destIncr, xferSize,
  509. xferSizeIncr, cSave,
  510. MCD_taskTable, channel);
  511. } else {
  512. /*TDTStart and TDTEnd */
  513. MCD_taskTable[channel].TDTstart =
  514. MCD_modelTaskTable[TASK_CHAINEU].TDTstart;
  515. MCD_taskTable[channel].TDTend =
  516. MCD_modelTaskTable[TASK_CHAINEU].TDTend;
  517. MCD_startDmaChainEu((int *)srcAddr, srcIncr, destIncr,
  518. xferSize, xferSizeIncr, cSave,
  519. MCD_taskTable, channel);
  520. }
  521. }
  522. MCD_chStatus[channel] = MCD_IDLE;
  523. return (MCD_OK);
  524. }
  525. /************************ End of MCD_startDma() *********************/
  526. /********************************************************************/
  527. /* Function: MCD_XferProgrQuery
  528. * Purpose: Returns progress of DMA on requested channel
  529. * Arguments: channel - channel to retrieve progress for
  530. * progRep - pointer to user supplied MCD_XferProg struct
  531. * Returns: MCD_CHANNEL_INVALID if channel is invalid, else MCD_OK
  532. *
  533. * Notes:
  534. * MCD_XferProgrQuery() upon completing or after aborting a DMA, or
  535. * while the DMA is in progress, this function returns the first
  536. * DMA-destination address not (or not yet) used in the DMA. When
  537. * encountering a non-ready buffer descriptor, the information for
  538. * the last completed descriptor is returned.
  539. *
  540. * MCD_XferProgQuery() has to avoid the possibility of getting
  541. * partially-updated information in the event that we should happen
  542. * to query DMA progress just as the DMA is updating it. It does that
  543. * by taking advantage of the fact context is not saved frequently for
  544. * the most part. We therefore read it at least twice until we get the
  545. * same information twice in a row.
  546. *
  547. * Because a small, but not insignificant, amount of time is required
  548. * to write out the progress-query information, especially upon
  549. * completion of the DMA, it would be wise to guarantee some time lag
  550. * between successive readings of the progress-query information.
  551. */
  552. /* How many iterations of the loop below to execute to stabilize values */
  553. #define STABTIME 0
  554. int MCD_XferProgrQuery(int channel, MCD_XferProg * progRep)
  555. {
  556. MCD_XferProg prevRep;
  557. int again; /* true if we are to try again to ge
  558. consistent results */
  559. int i; /* used as a time-waste counter */
  560. int destDiffBytes; /* Total no of bytes that we think actually
  561. got xfered. */
  562. int numIterations; /* number of iterations */
  563. int bytesNotXfered; /* bytes that did not get xfered. */
  564. s8 *LWAlignedInitDestAddr, *LWAlignedCurrDestAddr;
  565. int subModVal, addModVal; /* Mode values to added and subtracted
  566. from the final destAddr */
  567. if ((channel < 0) || (channel >= NCHANNELS))
  568. return (MCD_CHANNEL_INVALID);
  569. /* Read a trial value for the progress-reporting values */
  570. prevRep.lastSrcAddr =
  571. (s8 *) ((volatile int *)MCD_taskTable[channel].
  572. contextSaveSpace)[SRCPTR + CSAVE_OFFSET];
  573. prevRep.lastDestAddr =
  574. (s8 *) ((volatile int *)MCD_taskTable[channel].
  575. contextSaveSpace)[DESTPTR + CSAVE_OFFSET];
  576. prevRep.dmaSize =
  577. ((volatile int *)MCD_taskTable[channel].contextSaveSpace)[DCOUNT +
  578. CSAVE_OFFSET];
  579. prevRep.currBufDesc =
  580. (MCD_bufDesc *) ((volatile int *)MCD_taskTable[channel].
  581. contextSaveSpace)[CURRBD + CSAVE_OFFSET];
  582. /* Repeatedly reread those values until they match previous values: */
  583. do {
  584. /* Waste a little bit of time to ensure stability: */
  585. for (i = 0; i < STABTIME; i++) {
  586. /* make sure this loop does something so that it
  587. doesn't get optimized out */
  588. i += i >> 2;
  589. }
  590. /* Check them again: */
  591. progRep->lastSrcAddr =
  592. (s8 *) ((volatile int *)MCD_taskTable[channel].
  593. contextSaveSpace)[SRCPTR + CSAVE_OFFSET];
  594. progRep->lastDestAddr =
  595. (s8 *) ((volatile int *)MCD_taskTable[channel].
  596. contextSaveSpace)[DESTPTR + CSAVE_OFFSET];
  597. progRep->dmaSize =
  598. ((volatile int *)MCD_taskTable[channel].
  599. contextSaveSpace)[DCOUNT + CSAVE_OFFSET];
  600. progRep->currBufDesc =
  601. (MCD_bufDesc *) ((volatile int *)MCD_taskTable[channel].
  602. contextSaveSpace)[CURRBD + CSAVE_OFFSET];
  603. /* See if they match: */
  604. if (prevRep.lastSrcAddr != progRep->lastSrcAddr
  605. || prevRep.lastDestAddr != progRep->lastDestAddr
  606. || prevRep.dmaSize != progRep->dmaSize
  607. || prevRep.currBufDesc != progRep->currBufDesc) {
  608. /* If they don't match, remember previous values and
  609. try again: */
  610. prevRep.lastSrcAddr = progRep->lastSrcAddr;
  611. prevRep.lastDestAddr = progRep->lastDestAddr;
  612. prevRep.dmaSize = progRep->dmaSize;
  613. prevRep.currBufDesc = progRep->currBufDesc;
  614. again = MCD_TRUE;
  615. } else
  616. again = MCD_FALSE;
  617. } while (again == MCD_TRUE);
  618. /* Update the dCount, srcAddr and destAddr */
  619. /* To calculate dmaCount, we consider destination address. C
  620. overs M1,P1,Z for destination */
  621. switch (MCD_remVariants.remDestRsdIncr[channel]) {
  622. case MINUS1:
  623. subModVal =
  624. ((int)progRep->
  625. lastDestAddr) & ((MCD_remVariants.remXferSize[channel]) -
  626. 1);
  627. addModVal =
  628. ((int)progRep->currBufDesc->
  629. destAddr) & ((MCD_remVariants.remXferSize[channel]) - 1);
  630. LWAlignedInitDestAddr =
  631. (progRep->currBufDesc->destAddr) - addModVal;
  632. LWAlignedCurrDestAddr = (progRep->lastDestAddr) - subModVal;
  633. destDiffBytes = LWAlignedInitDestAddr - LWAlignedCurrDestAddr;
  634. bytesNotXfered =
  635. (destDiffBytes / MCD_remVariants.remDestIncr[channel]) *
  636. (MCD_remVariants.remDestIncr[channel]
  637. + MCD_remVariants.remXferSize[channel]);
  638. progRep->dmaSize =
  639. destDiffBytes - bytesNotXfered + addModVal - subModVal;
  640. break;
  641. case ZERO:
  642. progRep->lastDestAddr = progRep->currBufDesc->destAddr;
  643. break;
  644. case PLUS1:
  645. /* This value has to be subtracted from the final
  646. calculated dCount. */
  647. subModVal =
  648. ((int)progRep->currBufDesc->
  649. destAddr) & ((MCD_remVariants.remXferSize[channel]) - 1);
  650. /* These bytes are already in lastDestAddr. */
  651. addModVal =
  652. ((int)progRep->
  653. lastDestAddr) & ((MCD_remVariants.remXferSize[channel]) -
  654. 1);
  655. LWAlignedInitDestAddr =
  656. (progRep->currBufDesc->destAddr) - subModVal;
  657. LWAlignedCurrDestAddr = (progRep->lastDestAddr) - addModVal;
  658. destDiffBytes = (progRep->lastDestAddr - LWAlignedInitDestAddr);
  659. numIterations =
  660. (LWAlignedCurrDestAddr -
  661. LWAlignedInitDestAddr) /
  662. MCD_remVariants.remDestIncr[channel];
  663. bytesNotXfered =
  664. numIterations * (MCD_remVariants.remDestIncr[channel]
  665. - MCD_remVariants.remXferSize[channel]);
  666. progRep->dmaSize = destDiffBytes - bytesNotXfered - subModVal;
  667. break;
  668. default:
  669. break;
  670. }
  671. /* This covers M1,P1,Z for source */
  672. switch (MCD_remVariants.remSrcRsdIncr[channel]) {
  673. case MINUS1:
  674. progRep->lastSrcAddr =
  675. progRep->currBufDesc->srcAddr +
  676. (MCD_remVariants.remSrcIncr[channel] *
  677. (progRep->dmaSize / MCD_remVariants.remXferSize[channel]));
  678. break;
  679. case ZERO:
  680. progRep->lastSrcAddr = progRep->currBufDesc->srcAddr;
  681. break;
  682. case PLUS1:
  683. progRep->lastSrcAddr =
  684. progRep->currBufDesc->srcAddr +
  685. (MCD_remVariants.remSrcIncr[channel] *
  686. (progRep->dmaSize / MCD_remVariants.remXferSize[channel]));
  687. break;
  688. default:
  689. break;
  690. }
  691. return (MCD_OK);
  692. }
  693. /******************* End of MCD_XferProgrQuery() ********************/
  694. /********************************************************************/
  695. /* MCD_resmActions() does the majority of the actions of a DMA resume.
  696. * It is called from MCD_killDma() and MCD_resumeDma(). It has to be
  697. * a separate function because the kill function has to negate the task
  698. * enable before resuming it, but the resume function has to do nothing
  699. * if there is no DMA on that channel (i.e., if the enable bit is 0).
  700. */
  701. static void MCD_resmActions(int channel)
  702. {
  703. MCD_dmaBar->debugControl = DBG_CTL_DISABLE;
  704. MCD_dmaBar->debugStatus = MCD_dmaBar->debugStatus;
  705. /* This register is selected to know which initiator is
  706. actually asserted. */
  707. MCD_dmaBar->ptdDebug = PTD_DBG_TSK_VLD_INIT;
  708. if ((MCD_dmaBar->ptdDebug >> channel) & 0x1)
  709. MCD_chStatus[channel] = MCD_RUNNING;
  710. else
  711. MCD_chStatus[channel] = MCD_IDLE;
  712. }
  713. /********************* End of MCD_resmActions() *********************/
  714. /********************************************************************/
  715. /* Function: MCD_killDma
  716. * Purpose: Halt the DMA on the requested channel, without any
  717. * intention of resuming the DMA.
  718. * Arguments: channel - requested channel
  719. * Returns: MCD_CHANNEL_INVALID if channel is invalid, else MCD_OK
  720. *
  721. * Notes:
  722. * A DMA may be killed from any state, including paused state, and it
  723. * always goes to the MCD_HALTED state even if it is killed while in
  724. * the MCD_NO_DMA or MCD_IDLE states.
  725. */
  726. int MCD_killDma(int channel)
  727. {
  728. /* MCD_XferProg progRep; */
  729. if ((channel < 0) || (channel >= NCHANNELS))
  730. return (MCD_CHANNEL_INVALID);
  731. MCD_dmaBar->taskControl[channel] = 0x0;
  732. MCD_resumeDma(channel);
  733. /*
  734. * This must be after the write to the TCR so that the task doesn't
  735. * start up again momentarily, and before the status assignment so
  736. * as to override whatever MCD_resumeDma() may do to the channel
  737. * status.
  738. */
  739. MCD_chStatus[channel] = MCD_HALTED;
  740. /*
  741. * Update the current buffer descriptor's lastDestAddr field
  742. *
  743. * MCD_XferProgrQuery (channel, &progRep);
  744. * progRep.currBufDesc->lastDestAddr = progRep.lastDestAddr;
  745. */
  746. return (MCD_OK);
  747. }
  748. /************************ End of MCD_killDma() **********************/
  749. /********************************************************************/
  750. /* Function: MCD_continDma
  751. * Purpose: Continue a DMA which as stopped due to encountering an
  752. * unready buffer descriptor.
  753. * Arguments: channel - channel to continue the DMA on
  754. * Returns: MCD_CHANNEL_INVALID if channel is invalid, else MCD_OK
  755. *
  756. * Notes:
  757. * This routine does not check to see if there is a task which can
  758. * be continued. Also this routine should not be used with single DMAs.
  759. */
  760. int MCD_continDma(int channel)
  761. {
  762. if ((channel < 0) || (channel >= NCHANNELS))
  763. return (MCD_CHANNEL_INVALID);
  764. MCD_dmaBar->taskControl[channel] |= TASK_CTL_EN;
  765. MCD_chStatus[channel] = MCD_RUNNING;
  766. return (MCD_OK);
  767. }
  768. /********************** End of MCD_continDma() **********************/
  769. /*********************************************************************
  770. * MCD_pauseDma() and MCD_resumeDma() below use the DMA's debug unit
  771. * to freeze a task and resume it. We freeze a task by breakpointing
  772. * on the stated task. That is, not any specific place in the task,
  773. * but any time that task executes. In particular, when that task
  774. * executes, we want to freeze that task and only that task.
  775. *
  776. * The bits of the debug control register influence interrupts vs.
  777. * breakpoints as follows:
  778. * - Bits 14 and 0 enable or disable debug functions. If enabled, you
  779. * will get the interrupt but you may or may not get a breakpoint.
  780. * - Bits 2 and 1 decide whether you also get a breakpoint in addition
  781. * to an interrupt.
  782. *
  783. * The debug unit can do these actions in response to either internally
  784. * detected breakpoint conditions from the comparators, or in response
  785. * to the external breakpoint pin, or both.
  786. * - Bits 14 and 1 perform the above-described functions for
  787. * internally-generated conditions, i.e., the debug comparators.
  788. * - Bits 0 and 2 perform the above-described functions for external
  789. * conditions, i.e., the breakpoint external pin.
  790. *
  791. * Note that, although you "always" get the interrupt when you turn
  792. * the debug functions, the interrupt can nevertheless, if desired, be
  793. * masked by the corresponding bit in the PTD's IMR. Note also that
  794. * this means that bits 14 and 0 must enable debug functions before
  795. * bits 1 and 2, respectively, have any effect.
  796. *
  797. * NOTE: It's extremely important to not pause more than one DMA channel
  798. * at a time.
  799. ********************************************************************/
  800. /********************************************************************/
  801. /* Function: MCD_pauseDma
  802. * Purpose: Pauses the DMA on a given channel (if any DMA is running
  803. * on that channel).
  804. * Arguments: channel
  805. * Returns: MCD_CHANNEL_INVALID if channel is invalid, else MCD_OK
  806. */
  807. int MCD_pauseDma(int channel)
  808. {
  809. /* MCD_XferProg progRep; */
  810. if ((channel < 0) || (channel >= NCHANNELS))
  811. return (MCD_CHANNEL_INVALID);
  812. if (MCD_dmaBar->taskControl[channel] & TASK_CTL_EN) {
  813. MCD_dmaBar->debugComp1 = channel;
  814. MCD_dmaBar->debugControl =
  815. DBG_CTL_ENABLE | (1 << (channel + 16));
  816. MCD_chStatus[channel] = MCD_PAUSED;
  817. /*
  818. * Update the current buffer descriptor's lastDestAddr field
  819. *
  820. * MCD_XferProgrQuery (channel, &progRep);
  821. * progRep.currBufDesc->lastDestAddr = progRep.lastDestAddr;
  822. */
  823. }
  824. return (MCD_OK);
  825. }
  826. /************************* End of MCD_pauseDma() ********************/
  827. /********************************************************************/
  828. /* Function: MCD_resumeDma
  829. * Purpose: Resumes the DMA on a given channel (if any DMA is
  830. * running on that channel).
  831. * Arguments: channel - channel on which to resume DMA
  832. * Returns: MCD_CHANNEL_INVALID if channel is invalid, else MCD_OK
  833. */
  834. int MCD_resumeDma(int channel)
  835. {
  836. if ((channel < 0) || (channel >= NCHANNELS))
  837. return (MCD_CHANNEL_INVALID);
  838. if (MCD_dmaBar->taskControl[channel] & TASK_CTL_EN)
  839. MCD_resmActions(channel);
  840. return (MCD_OK);
  841. }
  842. /************************ End of MCD_resumeDma() ********************/
  843. /********************************************************************/
  844. /* Function: MCD_csumQuery
  845. * Purpose: Provide the checksum after performing a non-chained DMA
  846. * Arguments: channel - channel to report on
  847. * csum - pointer to where to write the checksum/CRC
  848. * Returns: MCD_ERROR if the channel is invalid, else MCD_OK
  849. *
  850. * Notes:
  851. *
  852. */
  853. int MCD_csumQuery(int channel, u32 * csum)
  854. {
  855. #ifdef MCD_INCLUDE_EU
  856. if ((channel < 0) || (channel >= NCHANNELS))
  857. return (MCD_CHANNEL_INVALID);
  858. *csum = MCD_relocBuffDesc[channel].csumResult;
  859. return (MCD_OK);
  860. #else
  861. return (MCD_ERROR);
  862. #endif
  863. }
  864. /*********************** End of MCD_resumeDma() *********************/
  865. /********************************************************************/
  866. /* Function: MCD_getCodeSize
  867. * Purpose: Provide the size requirements of the microcoded tasks
  868. * Returns: Size in bytes
  869. */
  870. int MCD_getCodeSize(void)
  871. {
  872. #ifdef MCD_INCLUDE_EU
  873. return (0x2b5c);
  874. #else
  875. return (0x173c);
  876. #endif
  877. }
  878. /********************** End of MCD_getCodeSize() ********************/
  879. /********************************************************************/
  880. /* Function: MCD_getVersion
  881. * Purpose: Provide the version string and number
  882. * Arguments: longVersion - user supplied pointer to a pointer to a char
  883. * which points to the version string
  884. * Returns: Version number and version string (by reference)
  885. */
  886. char MCD_versionString[] = "Multi-channel DMA API Alpha v0.3 (2004-04-26)";
  887. #define MCD_REV_MAJOR 0x00
  888. #define MCD_REV_MINOR 0x03
  889. int MCD_getVersion(char **longVersion)
  890. {
  891. *longVersion = MCD_versionString;
  892. return ((MCD_REV_MAJOR << 8) | MCD_REV_MINOR);
  893. }
  894. /********************** End of MCD_getVersion() *********************/
  895. /********************************************************************/
  896. /* Private version of memcpy()
  897. * Note that everything this is used for is longword-aligned.
  898. */
  899. static void MCD_memcpy(int *dest, int *src, u32 size)
  900. {
  901. u32 i;
  902. for (i = 0; i < size; i += sizeof(int), dest++, src++)
  903. *dest = *src;
  904. }