Sony PlayStation2 Study
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

541 lines
17 KiB

4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "dma.h"
  2. /** DMA Control Register */
  3. #define DMA_REG_CTRL ((volatile uint32_t *)0x1000E000)
  4. /** Interrupt Status Register */
  5. #define DMA_REG_STAT ((volatile uint32_t *)0x1000E010)
  6. /** Priority Control Register */
  7. #define DMA_REG_PCR ((volatile uint32_t *)0x1000E020)
  8. /** Interleave Size Register */
  9. #define DMA_REG_SQWC ((volatile uint32_t *)0x1000E030)
  10. /** Ring Buffer Size Register */
  11. #define DMA_REG_RBSR ((volatile uint32_t *)0x1000E040)
  12. /** Ring Buffer Address Register */
  13. #define DMA_REG_RBOR ((volatile uint32_t *)0x1000E050)
  14. /** Stall Address Register */
  15. #define DMA_REG_STADR ((volatile uint32_t *)0x1000E060)
  16. /** DMA Hold State Register */
  17. #define DMA_REG_ENABLER ((volatile uint32_t *)0x1000F520)
  18. /** DMA Hold Control Register */
  19. #define DMA_REG_ENABLEW ((volatile uint32_t *)0x1000F590)
  20. /** Enable DMA Controller */
  21. #define DMAE_DISABLE 0
  22. #define DMAE_ENABLE 1
  23. /** Enable Cycle Stealing */
  24. #define RELE_OFF 0
  25. #define RELE_ON 1
  26. /** fifo drain? */
  27. #define MFD_OFF 0
  28. #define MFD_RES 1
  29. #define MFD_VIF 2
  30. #define MFD_GIF 3
  31. /** stall source? */
  32. #define STS_UNSPEC 0
  33. #define STS_SIF 1
  34. #define STS_SPR 2
  35. #define STS_IPU 3
  36. /** stall drain? */
  37. #define STD_OFF 0
  38. #define STD_VIF 1
  39. #define STD_GIF 2
  40. #define STD_SIF 3
  41. /** Cycles to release control */
  42. #define RCYC_8 0
  43. #define RCYC_16 1
  44. #define RCYC_32 2
  45. #define RCYC_64 3
  46. #define RCYC_128 4
  47. #define RCYC_256 5
  48. #define DMA_SET_CTRL(DMAE,RELE,MFD,STS,STD,RCYC) \
  49. (uint32_t)(DMAE & 0x00000001) << 0 | (uint32_t)(RELE & 0x00000001) << 1 | \
  50. (uint32_t)(MFD & 0x00000003) << 2 | (uint32_t)(STS & 0x00000003) << 4 | \
  51. (uint32_t)(STD & 0x00000003) << 6 | (uint32_t)(RCYC & 0x00000007) << 8
  52. #define DMA_SET_STAT(CIS,SIS,MEIS,BEIS,CIM,SIM,MEIM) \
  53. (uint32_t)((CIS) & 0x000003FF) << 0 | (uint32_t)((SIS) & 0x00000001) << 13 | \
  54. (uint32_t)((MEIS) & 0x00000001) << 14 | (uint32_t)((BEIS) & 0x00000001) << 15 | \
  55. (uint32_t)((CIM) & 0x000003FF) << 16 | (uint32_t)((SIM) & 0x00000001) << 29 | \
  56. (uint32_t)((MEIM) & 0x00000001) << 30
  57. #define DMA_SET_PCR(CPCOND,CDE,PCE) \
  58. (uint32_t)((CPCOND) & 0x000003FF) << 0 | (uint32_t)((CDE) & 0x000003FF) << 16 | \
  59. (uint32_t)((PCE) & 0x00000001) << 31
  60. #define DMA_SET_SQWC(SQWC,TQWC) \
  61. (uint32_t)((SQWC) & 0x000000FF) << 0 | (uint32_t)((TQWC) & 0x000000FF) << 16
  62. #define DMA_SET_RBOR(ADDR) (uint32_t)((ADDR) & 0x00007FFF)
  63. #define DMA_SET_RBSR(RMSK) (uint32_t)((RMSK) & 0x00007FFF)
  64. #define DMA_SET_STADR(ADDR) (uint32_t)((ADDR) & 0x00007FFF)
  65. #define DMA_SET_ENABLEW(A) (uint32_t)((A) & 0x00000001) << 16
  66. #define DMA_SET_ENABLER(A) (uint32_t)((A) & 0x00000001) << 16
  67. /** Per-dma channel registers */
  68. #define DMA_SET_CHCR(DIR,MODE,ASP,TTE,TIE,STR,TAG) \
  69. (uint32_t)((DIR) & 0x00000001) << 0 | (uint32_t)((MODE) & 0x00000003) << 2 | \
  70. (uint32_t)((ASP) & 0x00000003) << 4 | (uint32_t)((TTE ) & 0x00000001) << 6 | \
  71. (uint32_t)((TIE) & 0x00000001) << 7 | (uint32_t)((STR ) & 0x00000001) << 8 | \
  72. (uint32_t)((TAG) & 0x0000FFFF) << 16
  73. #define DMA_SET_MADR(ADDR,SPR) \
  74. (uint32_t)(((uint32_t)ADDR) & 0x7FFFFFFF) << 0 | (uint32_t)((SPR) & 0x00000001) << 31
  75. #define DMA_SET_TADR(ADDR,SPR) \
  76. (uint32_t)(((uint32_t)ADDR) & 0x7FFFFFFF) << 0 | (uint32_t)((SPR) & 0x00000001) << 31
  77. #define DMA_SET_ASR0(ADDR,SPR) \
  78. (uint32_t)(((uint32_t)ADDR) & 0x7FFFFFFF) << 0 | (uint32_t)((SPR) & 0x00000001) << 31
  79. #define DMA_SET_ASR1(ADDR,SPR) \
  80. (uint32_t)(((uint32_t)ADDR) & 0x7FFFFFFF) << 0 | (uint32_t)((SPR) & 0x00000001) << 31
  81. #define DMA_SET_SADR(ADDR) (uint32_t)(((uint32_t)ADDR) & 0x00003FFF)
  82. #define DMA_SET_QWC(QWC) (uint32_t)((QWC) & 0x0000FFFF)
  83. #define DMA_SET_SIZE(A) \
  84. (uint32_t)(A & 0x0000FFFF) << 0
  85. /**
  86. * T = location of qwords
  87. * D = next dmatag to be read
  88. * QWC = quadword count for dmatag
  89. */
  90. /** T=QWC D=QWC+1 MADR => STADR */
  91. #define DMA_TAG_CNTS 0x00
  92. /** T=ADDR then END */
  93. #define DMA_TAG_REFE 0x00
  94. /** T=QWC D=QWC+1 */
  95. #define DMA_TAG_CNT 0x01
  96. /** T=QWC D=ADDR */
  97. #define DMA_TAG_NEXT 0x02
  98. /** D=D+1 T=ADDR */
  99. #define DMA_TAG_REF 0x03
  100. /** .. + stall ctrl */
  101. #define DMA_TAG_REFS 0x04
  102. /** T=QWC D=ADDR QWC+1 => ASR0 */
  103. #define DMA_TAG_CALL 0x05
  104. /** T=QWC (ASR0 => D) if !ASR0 then END */
  105. #define DMA_TAG_RET 0x06
  106. /** T=QWC then END */
  107. #define DMA_TAG_END 0x07
  108. #define DMATAG(QWC,PCE,ID,IRQ,ADDR,SPR) \
  109. (u64)((QWC) & 0x0000FFFF) << 0 | (u64)((PCE) & 0x00000003) << 26 | \
  110. (u64)((ID) & 0x00000007) << 28 | (u64)((IRQ) & 0x00000001) << 31 | \
  111. (u64)((ADDR) & 0x7FFFFFFF) << 32 | (u64)((SPR) & 0x00000001) << 63
  112. #define PACK_DMATAG(Q,D0,W2,W3) \
  113. Q->dw[0] = (u64)(D0), \
  114. Q->sw[2] = (u32)(W2), \
  115. Q->sw[3] = (u32)(W3)
  116. /** Insert before qword block, and add dmatag after qword block to continue */
  117. #define DMATAG_CNT(Q,QWC,SPR,W2,W3) \
  118. Q->dw[0] = DMATAG(QWC,0,DMA_TAG_CNT,0,0,SPR), \
  119. Q->sw[2] = (u32)(W2), \
  120. Q->sw[3] = (u32)(W3)
  121. /** Insert before qword block, and add dmatag after qword block to continue */
  122. #define DMATAG_CNTS(Q,QWC,SPR,W2,W3) \
  123. Q->dw[0] = DMATAG(QWC,0,DMA_TAG_CNTS,0,0,SPR), \
  124. Q->sw[2] = (u32)(W2), \
  125. Q->sw[3] = (u32)(W3)
  126. /** Insert before qword block, and add dmatag at addr to continue */
  127. #define DMATAG_NEXT(Q,QWC,ADDR,SPR,W2,W3), \
  128. Q->dw[0] = DMATAG(QWC,0,DMA_TAG_NEXT,0,ADDR,SPR), \
  129. Q->sw[2] = (u32)(W2), \
  130. Q->sw[3] = (u32)(W3)
  131. /** Insert before final qword block, or by itself to end transfer */
  132. #define DMATAG_END(Q,QWC,SPR,W2,W3) \
  133. Q->dw[0] = DMATAG(QWC,0,DMA_TAG_END,0,0,SPR), \
  134. Q->sw[2] = (u32)(W2), \
  135. Q->sw[3] = (u32)(W3)
  136. /** Insert before qword block, and add dmatag at qword after qwc to be saved (up to 2) */
  137. #define DMATAG_CALL(Q,QWC,ADDR,SPR,W2,W3) \
  138. Q->dw[0] = DMATAG(QWC,0,DMA_TAG_CALL,0,ADDR,SPR), \
  139. Q->sw[2] = (u32)(W2), \
  140. Q->sw[3] = (u32)(W3)
  141. /** Insert before qword block, and continues with saved dmatag */
  142. #define DMATAG_RET(Q,QWC,SPR,W2,W3) \
  143. Q->dw[0] = DMATAG(QWC,0,DMA_TAG_RET,0,0,SPR), \
  144. Q->sw[2] = (u32)(W2), \
  145. Q->sw[3] = (u32)(W3)
  146. /** Insert anywhere, qwc at addr is sent, reads next qword to continue */
  147. #define DMATAG_REF(Q,QWC,ADDR,SPR,W2,W3) \
  148. Q->dw[0] = DMATAG(QWC,0,DMA_TAG_REF,0,ADDR,SPR), \
  149. Q->sw[2] = (u32)(W2), \
  150. Q->sw[3] = (u32)(W3)
  151. /** Insert anywhere, qwc at addr is sent, reads next qword to continue */
  152. #define DMATAG_REFS(Q,QWC,ADDR,SPR,W2,W3) \
  153. Q->dw[0] = DMATAG(QWC,0,DMA_TAG_REFS,0,ADDR,SPR), \
  154. Q->sw[2] = (u32)(W2), \
  155. Q->sw[3] = (u32)(W3)
  156. /** Insert anywhere, qwc at addr is sent, then ends transfer */
  157. #define DMATAG_REFE(Q,QWC,ADDR,SPR,W2,W3) \
  158. Q->dw[0] = DMATAG(qwc, 0, DMA_TAG_REFE, 0, addr, spr), \
  159. Q->sw[2] = (u32)(W2), \
  160. Q->sw[3] = (u32)(W3)
  161. // Channel Control
  162. static volatile uint32_t * dma_chcr[10] = {
  163. (volatile uint32_t *)0x10008000,
  164. (volatile uint32_t *)0x10009000,
  165. (volatile uint32_t *)0x1000A000,
  166. (volatile uint32_t *)0x1000B000,
  167. (volatile uint32_t *)0x1000B400,
  168. (volatile uint32_t *)0x1000C000,
  169. (volatile uint32_t *)0x1000C400,
  170. (volatile uint32_t *)0x1000C800,
  171. (volatile uint32_t *)0x1000D000,
  172. (volatile uint32_t *)0x1000D400
  173. };
  174. // Quadword Count
  175. static volatile uint32_t * dma_qwc[10] = {
  176. (volatile uint32_t *)0x10008020,
  177. (volatile uint32_t *)0x10009020,
  178. (volatile uint32_t *)0x1000A020,
  179. (volatile uint32_t *)0x1000B020,
  180. (volatile uint32_t *)0x1000B420,
  181. (volatile uint32_t *)0x1000C020,
  182. (volatile uint32_t *)0x1000C420,
  183. (volatile uint32_t *)0x1000C820,
  184. (volatile uint32_t *)0x1000D020,
  185. (volatile uint32_t *)0x1000D420
  186. };
  187. // Memory Address
  188. static volatile uint32_t * dma_madr[10] = {
  189. (volatile uint32_t *)0x10008010,
  190. (volatile uint32_t *)0x10009010,
  191. (volatile uint32_t *)0x1000A010,
  192. (volatile uint32_t *)0x1000B010,
  193. (volatile uint32_t *)0x1000B410,
  194. (volatile uint32_t *)0x1000C010,
  195. (volatile uint32_t *)0x1000C410,
  196. (volatile uint32_t *)0x1000C810,
  197. (volatile uint32_t *)0x1000D010,
  198. (volatile uint32_t *)0x1000D410
  199. };
  200. // Tag Address
  201. static volatile uint32_t * dma_tadr[10] = {
  202. (volatile uint32_t *)0x10008030,
  203. (volatile uint32_t *)0x10009030,
  204. (volatile uint32_t *)0x1000A030,
  205. (volatile uint32_t *)0x1000B030,
  206. (volatile uint32_t *)0x1000B430,
  207. (volatile uint32_t *)0x1000C030,
  208. (volatile uint32_t *)0x1000C430,
  209. (volatile uint32_t *)0x1000C830,
  210. (volatile uint32_t *)0x1000D030,
  211. (volatile uint32_t *)0x1000D430
  212. };
  213. // Tag Address Save 0
  214. static volatile uint32_t * dma_asr0[10] = {
  215. (volatile uint32_t *)0x10008040,
  216. (volatile uint32_t *)0x10009040,
  217. (volatile uint32_t *)0x1000A040,
  218. (volatile uint32_t *)0x00000000,
  219. (volatile uint32_t *)0x00000000,
  220. (volatile uint32_t *)0x00000000,
  221. (volatile uint32_t *)0x00000000,
  222. (volatile uint32_t *)0x00000000,
  223. (volatile uint32_t *)0x00000000,
  224. (volatile uint32_t *)0x00000000
  225. };
  226. // Tag Address Save 1
  227. static volatile uint32_t * dma_asr1[10] = {
  228. (volatile uint32_t *)0x10008050,
  229. (volatile uint32_t *)0x10009050,
  230. (volatile uint32_t *)0x1000A050,
  231. (volatile uint32_t *)0x00000000,
  232. (volatile uint32_t *)0x00000000,
  233. (volatile uint32_t *)0x00000000,
  234. (volatile uint32_t *)0x00000000,
  235. (volatile uint32_t *)0x00000000,
  236. (volatile uint32_t *)0x00000000,
  237. (volatile uint32_t *)0x00000000
  238. };
  239. // SPR Transfer Address
  240. static volatile uint32_t * dma_sadr[10] = {
  241. (volatile uint32_t *)0x10008080,
  242. (volatile uint32_t *)0x10009080,
  243. (volatile uint32_t *)0x1000A080,
  244. (volatile uint32_t *)0x00000000,
  245. (volatile uint32_t *)0x00000000,
  246. (volatile uint32_t *)0x00000000,
  247. (volatile uint32_t *)0x00000000,
  248. (volatile uint32_t *)0x00000000,
  249. (volatile uint32_t *)0x1000D080,
  250. (volatile uint32_t *)0x1000D480
  251. };
  252. int _dma_init(){
  253. // Reset the control registers.
  254. *DMA_REG_CTRL = 0x00000000;
  255. *DMA_REG_STAT = 0x00000000;
  256. *DMA_REG_PCR = 0x00000000;
  257. *DMA_REG_SQWC = 0x00000000;
  258. *DMA_REG_RBSR = 0x00000000;
  259. *DMA_REG_RBOR = 0x00000000;
  260. // Enable all channels, enable cycle stealing.
  261. *DMA_REG_CTRL = DMA_SET_CTRL( 1, 1, 0, 0, 0, 0 );
  262. // End function.
  263. return 0;
  264. };
  265. static uint32_t dma_handler_id[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  266. static uint8_t dma_channel_initialized[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  267. int dma_reset(void){
  268. uint8_t idx = 0;
  269. for(; idx < 10; idx++){
  270. dma_channel_shutdown( idx, 0 );
  271. }
  272. ResetEE( 0x01 );
  273. return 0;
  274. };
  275. int dma_channel_initialize( uint8_t channel, void *handler, uint32_t flags ){
  276. // Shut down the channel before making changes.
  277. if( dma_channel_shutdown(channel, flags ) < 0 ) return -1;
  278. // Clear any saved dmatags
  279. if( *dma_asr0[channel] ){
  280. *dma_asr0[channel] = 0;
  281. *dma_asr1[channel] = 0;
  282. };
  283. // Clear saved spr address
  284. if( *dma_sadr[channel] ){
  285. *dma_sadr[channel] = 0;
  286. };
  287. // If a handler is provided...
  288. if( handler != NULL ){
  289. // Add the handler, storing the handler id.
  290. dma_handler_id[channel] = AddDmacHandler( channel, handler, 0 );
  291. // Enable the channel interrupt.
  292. if( flags & DMA_FLAG_INTERRUPTSAFE ) {
  293. iEnableDmac(channel);
  294. } else {
  295. EnableDmac(channel);
  296. };
  297. };
  298. // Tell everyone we are initialized.
  299. dma_channel_initialized[channel] = 1;
  300. // End function.
  301. return 0;
  302. };
  303. // allows for cpcond0 checking
  304. //void dma_channel_fast_waits( uint8_t channel ){
  305. // *DMA_REG_PCR |= 1 << channel;
  306. //};
  307. void dma_wait_fast(void){
  308. __asm__ (
  309. "sync.l; sync.p;" \
  310. "0:" \
  311. "bc0t 0f; nop;" \
  312. "bc0t 0f; nop;" \
  313. "bc0t 0f; nop;" \
  314. "bc0f 0b; nop;" \
  315. "0:"
  316. );
  317. };
  318. int dma_channel_wait( uint8_t channel, uint32_t timeout ){
  319. // While the channel is not ready...
  320. while( *dma_chcr[channel] & 0x00000100 ){
  321. // Decrement the timeout counter, exiting if it expires.
  322. if( !( timeout-- ) ) return -1;
  323. };
  324. // End function.
  325. return 0;
  326. };
  327. void dma_channel_send_packet2( packet2_t *packet2, uint8_t channel, uint8_t flush_cache ){
  328. if( packet2 == NULL ) return;
  329. // dma_channel_send_chain does NOT flush all data that is "source chained"
  330. if( packet2->mode == P2_MODE_CHAIN ){
  331. // "dma_channel_send_normal always flushes the data cache"
  332. if( flush_cache ) FlushCache(0);
  333. dma_channel_send_chain( channel,
  334. (void *)((uint32_t)packet2->base & 0x0FFFFFFF ),
  335. 0,
  336. packet2->tte ? DMA_FLAG_TRANSFERTAG : 0,
  337. 0
  338. );
  339. } else {
  340. dma_channel_send_normal(
  341. channel,
  342. (void *)((uint32_t)packet2->base & 0x0FFFFFFF), // make ptr normal
  343. ((uint32_t)packet2->next - (uint32_t)packet2->base) >> 4, // length in qwords
  344. 0,
  345. 0
  346. );
  347. };
  348. };
  349. int dma_channel_send_chain( uint8_t channel, void *data, uint32_t data_size, uint32_t flags, uint32_t spr ){
  350. if( data == NULL ) return -1;
  351. // clear channel status
  352. *DMA_REG_STAT = DMA_SET_STAT(1 << channel,0,0,0,0,0,0);
  353. if( flags & DMA_FLAG_INTERRUPTSAFE ){
  354. iSyncDCache( data, data + (data_size<<4) );
  355. } else {
  356. SyncDCache( data, data + (data_size<<4) );
  357. };
  358. // Set the size of the data, in quadwords.
  359. *dma_qwc[channel] = DMA_SET_QWC(0);
  360. // Set the address of the data.
  361. *dma_madr[channel] = DMA_SET_MADR(0, 0);
  362. // Set the address of the data tag.
  363. *dma_tadr[channel] = DMA_SET_TADR( data, spr );
  364. // Start the transfer.
  365. *dma_chcr[channel] = DMA_SET_CHCR( 1, 1, 0, flags & DMA_FLAG_TRANSFERTAG, 1, 1, 0 );
  366. return 0;
  367. };
  368. int dma_channel_send_chain_ucab( uint8_t channel, void *data, uint32_t qwc, uint32_t flags){
  369. if( data == NULL ) return -1;
  370. // clear channel status
  371. *DMA_REG_STAT = 1 << channel;
  372. // Set the size of the data, in quadwords.
  373. *dma_qwc[channel] = DMA_SET_QWC(0);
  374. // Set the address of the data.
  375. *dma_madr[channel] = DMA_SET_MADR(0, 0);
  376. // Set the address of the data tag.
  377. *dma_tadr[channel] = DMA_SET_TADR( data - 0x30000000, 0 );
  378. // Start the transfer.
  379. *dma_chcr[channel] = DMA_SET_CHCR( 1, 1, 0, flags & DMA_FLAG_TRANSFERTAG, 1, 1, 0 );
  380. // End function.
  381. return 0;
  382. };
  383. int dma_channel_send_normal( uint8_t channel, void *data, uint32_t qwc, uint32_t flags, uint32_t spr){
  384. if( data == NULL ) return -1;
  385. // clear channel status
  386. *DMA_REG_STAT = DMA_SET_STAT(1 << channel,0,0,0,0,0,0);
  387. // Not sure if this should be here.
  388. if( flags & DMA_FLAG_INTERRUPTSAFE ){
  389. iSyncDCache( data, data + (qwc<<4) );
  390. } else {
  391. SyncDCache( data, data + (qwc<<4) );
  392. };
  393. // Set the size of the data, in quadwords.
  394. *dma_qwc[channel] = DMA_SET_QWC( qwc );
  395. // Set the address of the data.
  396. *dma_madr[channel] = DMA_SET_MADR( data, spr );
  397. // Start the transfer.
  398. *dma_chcr[channel] = DMA_SET_CHCR( 1, 0, 0, flags & DMA_FLAG_TRANSFERTAG, 1, 1, 0 );
  399. return 0;
  400. };
  401. int dma_channel_send_normal_ucab( uint8_t channel, void *data, uint32_t qwc, uint32_t flags ){
  402. if( data == NULL ) return -1;
  403. // clear channel status
  404. *DMA_REG_STAT = DMA_SET_STAT(1 << channel,0,0,0,0,0,0);
  405. // Not sure if this should be here.
  406. if( flags & DMA_FLAG_INTERRUPTSAFE ){
  407. iSyncDCache(data, data + (qwc<<4));
  408. } else {
  409. SyncDCache(data, data + (qwc<<4));
  410. };
  411. // Set the size of the data, in quadwords.
  412. *dma_qwc[channel] = DMA_SET_QWC(qwc);
  413. // Set the address of the data.
  414. *dma_madr[channel] = DMA_SET_MADR( data - 0x30000000, 0);
  415. // Start the transfer.
  416. *dma_chcr[channel] = DMA_SET_CHCR(1, 0, 0, flags & DMA_FLAG_TRANSFERTAG, 1, 1, 0);
  417. return 0;
  418. }
  419. int dma_channel_receive_chain( uint8_t channel, void *data, uint32_t data_size, uint32_t flags, uint32_t spr ){
  420. if( data == NULL ) return -1;
  421. // If we are not initialized...
  422. if( !dma_channel_initialized[channel] ){
  423. // Initialize the channel.
  424. if( dma_channel_initialize( channel, NULL, flags ) < 0 ) return -1;
  425. };
  426. // Set the size of the data, in quadwords.
  427. *dma_qwc[channel] = DMA_SET_QWC((data_size + 15) >> 4);
  428. // Set the address of the data.
  429. *dma_madr[channel] = DMA_SET_MADR( data, spr );
  430. // Start the transfer.
  431. *dma_chcr[channel] = DMA_SET_CHCR( 0, 1, 0, 0, 0, 1, 0 );
  432. // End function.
  433. return 0;
  434. };
  435. int dma_channel_receive_normal( uint8_t channel, void *data, uint32_t data_size, uint32_t flags, uint32_t spr){
  436. if( data == NULL ) return -1;
  437. // If we are not initialized...
  438. if( !dma_channel_initialized[channel] ) {
  439. // Initialize the channel.
  440. if( dma_channel_initialize( channel, NULL, flags ) < 0 ) return -1;
  441. };
  442. // Set the size of the data, in quadwords.
  443. *dma_qwc[channel] = DMA_SET_QWC( (data_size + 15) >> 4 );
  444. // Set the address of the data.
  445. *dma_madr[channel] = DMA_SET_MADR( data, spr );
  446. // Start the transfer.
  447. *dma_chcr[channel] = DMA_SET_CHCR( 0, 0, 0, 0, 0, 1, 0 );
  448. // End function.
  449. return 0;
  450. };
  451. int dma_channel_shutdown( uint8_t channel, uint32_t flags ){
  452. // If we are not initialized, no need to shut down.
  453. if( !dma_channel_initialized[channel] ) return 0;
  454. // If a handler was provided...
  455. if( dma_handler_id[channel] != 0 ){
  456. // Disable the channel.
  457. if( flags & DMA_FLAG_INTERRUPTSAFE ){
  458. iDisableDmac( channel );
  459. } else {
  460. DisableDmac( channel );
  461. };
  462. // Remove the handler.
  463. RemoveDmacHandler( channel, dma_handler_id[channel] );
  464. // Clear the handler id.
  465. dma_handler_id[channel] = 0;
  466. };
  467. // Tell everyone we are not initialized.
  468. dma_channel_initialized[channel] = 0;
  469. // End function.
  470. return 0;
  471. };