MCD_dmaApi.c 35 KB

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