MCD_dmaApi.c 35 KB

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