#include "dma.h" /** DMA Control Register */ #define DMA_REG_CTRL ((volatile uint32_t *)0x1000E000) /** Interrupt Status Register */ #define DMA_REG_STAT ((volatile uint32_t *)0x1000E010) /** Priority Control Register */ #define DMA_REG_PCR ((volatile uint32_t *)0x1000E020) /** Interleave Size Register */ #define DMA_REG_SQWC ((volatile uint32_t *)0x1000E030) /** Ring Buffer Size Register */ #define DMA_REG_RBSR ((volatile uint32_t *)0x1000E040) /** Ring Buffer Address Register */ #define DMA_REG_RBOR ((volatile uint32_t *)0x1000E050) /** Stall Address Register */ #define DMA_REG_STADR ((volatile uint32_t *)0x1000E060) /** DMA Hold State Register */ #define DMA_REG_ENABLER ((volatile uint32_t *)0x1000F520) /** DMA Hold Control Register */ #define DMA_REG_ENABLEW ((volatile uint32_t *)0x1000F590) /** Enable DMA Controller */ #define DMAE_DISABLE 0 #define DMAE_ENABLE 1 /** Enable Cycle Stealing */ #define RELE_OFF 0 #define RELE_ON 1 /** fifo drain? */ #define MFD_OFF 0 #define MFD_RES 1 #define MFD_VIF 2 #define MFD_GIF 3 /** stall source? */ #define STS_UNSPEC 0 #define STS_SIF 1 #define STS_SPR 2 #define STS_IPU 3 /** stall drain? */ #define STD_OFF 0 #define STD_VIF 1 #define STD_GIF 2 #define STD_SIF 3 /** Cycles to release control */ #define RCYC_8 0 #define RCYC_16 1 #define RCYC_32 2 #define RCYC_64 3 #define RCYC_128 4 #define RCYC_256 5 #define DMA_SET_CTRL(DMAE,RELE,MFD,STS,STD,RCYC) \ (uint32_t)(DMAE & 0x00000001) << 0 | (uint32_t)(RELE & 0x00000001) << 1 | \ (uint32_t)(MFD & 0x00000003) << 2 | (uint32_t)(STS & 0x00000003) << 4 | \ (uint32_t)(STD & 0x00000003) << 6 | (uint32_t)(RCYC & 0x00000007) << 8 #define DMA_SET_STAT(CIS,SIS,MEIS,BEIS,CIM,SIM,MEIM) \ (uint32_t)((CIS) & 0x000003FF) << 0 | (uint32_t)((SIS) & 0x00000001) << 13 | \ (uint32_t)((MEIS) & 0x00000001) << 14 | (uint32_t)((BEIS) & 0x00000001) << 15 | \ (uint32_t)((CIM) & 0x000003FF) << 16 | (uint32_t)((SIM) & 0x00000001) << 29 | \ (uint32_t)((MEIM) & 0x00000001) << 30 #define DMA_SET_PCR(CPCOND,CDE,PCE) \ (uint32_t)((CPCOND) & 0x000003FF) << 0 | (uint32_t)((CDE) & 0x000003FF) << 16 | \ (uint32_t)((PCE) & 0x00000001) << 31 #define DMA_SET_SQWC(SQWC,TQWC) \ (uint32_t)((SQWC) & 0x000000FF) << 0 | (uint32_t)((TQWC) & 0x000000FF) << 16 #define DMA_SET_RBOR(ADDR) (uint32_t)((ADDR) & 0x00007FFF) #define DMA_SET_RBSR(RMSK) (uint32_t)((RMSK) & 0x00007FFF) #define DMA_SET_STADR(ADDR) (uint32_t)((ADDR) & 0x00007FFF) #define DMA_SET_ENABLEW(A) (uint32_t)((A) & 0x00000001) << 16 #define DMA_SET_ENABLER(A) (uint32_t)((A) & 0x00000001) << 16 /** Per-dma channel registers */ #define DMA_SET_CHCR(DIR,MODE,ASP,TTE,TIE,STR,TAG) \ (uint32_t)((DIR) & 0x00000001) << 0 | (uint32_t)((MODE) & 0x00000003) << 2 | \ (uint32_t)((ASP) & 0x00000003) << 4 | (uint32_t)((TTE ) & 0x00000001) << 6 | \ (uint32_t)((TIE) & 0x00000001) << 7 | (uint32_t)((STR ) & 0x00000001) << 8 | \ (uint32_t)((TAG) & 0x0000FFFF) << 16 #define DMA_SET_MADR(ADDR,SPR) \ (uint32_t)(((uint32_t)ADDR) & 0x7FFFFFFF) << 0 | (uint32_t)((SPR) & 0x00000001) << 31 #define DMA_SET_TADR(ADDR,SPR) \ (uint32_t)(((uint32_t)ADDR) & 0x7FFFFFFF) << 0 | (uint32_t)((SPR) & 0x00000001) << 31 #define DMA_SET_ASR0(ADDR,SPR) \ (uint32_t)(((uint32_t)ADDR) & 0x7FFFFFFF) << 0 | (uint32_t)((SPR) & 0x00000001) << 31 #define DMA_SET_ASR1(ADDR,SPR) \ (uint32_t)(((uint32_t)ADDR) & 0x7FFFFFFF) << 0 | (uint32_t)((SPR) & 0x00000001) << 31 #define DMA_SET_SADR(ADDR) (uint32_t)(((uint32_t)ADDR) & 0x00003FFF) #define DMA_SET_QWC(QWC) (uint32_t)((QWC) & 0x0000FFFF) #define DMA_SET_SIZE(A) \ (uint32_t)(A & 0x0000FFFF) << 0 /** * T = location of qwords * D = next dmatag to be read * QWC = quadword count for dmatag */ /** T=QWC D=QWC+1 MADR => STADR */ #define DMA_TAG_CNTS 0x00 /** T=ADDR then END */ #define DMA_TAG_REFE 0x00 /** T=QWC D=QWC+1 */ #define DMA_TAG_CNT 0x01 /** T=QWC D=ADDR */ #define DMA_TAG_NEXT 0x02 /** D=D+1 T=ADDR */ #define DMA_TAG_REF 0x03 /** .. + stall ctrl */ #define DMA_TAG_REFS 0x04 /** T=QWC D=ADDR QWC+1 => ASR0 */ #define DMA_TAG_CALL 0x05 /** T=QWC (ASR0 => D) if !ASR0 then END */ #define DMA_TAG_RET 0x06 /** T=QWC then END */ #define DMA_TAG_END 0x07 #define DMATAG(QWC,PCE,ID,IRQ,ADDR,SPR) \ (u64)((QWC) & 0x0000FFFF) << 0 | (u64)((PCE) & 0x00000003) << 26 | \ (u64)((ID) & 0x00000007) << 28 | (u64)((IRQ) & 0x00000001) << 31 | \ (u64)((ADDR) & 0x7FFFFFFF) << 32 | (u64)((SPR) & 0x00000001) << 63 #define PACK_DMATAG(Q,D0,W2,W3) \ Q->dw[0] = (u64)(D0), \ Q->sw[2] = (u32)(W2), \ Q->sw[3] = (u32)(W3) /** Insert before qword block, and add dmatag after qword block to continue */ #define DMATAG_CNT(Q,QWC,SPR,W2,W3) \ Q->dw[0] = DMATAG(QWC,0,DMA_TAG_CNT,0,0,SPR), \ Q->sw[2] = (u32)(W2), \ Q->sw[3] = (u32)(W3) /** Insert before qword block, and add dmatag after qword block to continue */ #define DMATAG_CNTS(Q,QWC,SPR,W2,W3) \ Q->dw[0] = DMATAG(QWC,0,DMA_TAG_CNTS,0,0,SPR), \ Q->sw[2] = (u32)(W2), \ Q->sw[3] = (u32)(W3) /** Insert before qword block, and add dmatag at addr to continue */ #define DMATAG_NEXT(Q,QWC,ADDR,SPR,W2,W3), \ Q->dw[0] = DMATAG(QWC,0,DMA_TAG_NEXT,0,ADDR,SPR), \ Q->sw[2] = (u32)(W2), \ Q->sw[3] = (u32)(W3) /** Insert before final qword block, or by itself to end transfer */ #define DMATAG_END(Q,QWC,SPR,W2,W3) \ Q->dw[0] = DMATAG(QWC,0,DMA_TAG_END,0,0,SPR), \ Q->sw[2] = (u32)(W2), \ Q->sw[3] = (u32)(W3) /** Insert before qword block, and add dmatag at qword after qwc to be saved (up to 2) */ #define DMATAG_CALL(Q,QWC,ADDR,SPR,W2,W3) \ Q->dw[0] = DMATAG(QWC,0,DMA_TAG_CALL,0,ADDR,SPR), \ Q->sw[2] = (u32)(W2), \ Q->sw[3] = (u32)(W3) /** Insert before qword block, and continues with saved dmatag */ #define DMATAG_RET(Q,QWC,SPR,W2,W3) \ Q->dw[0] = DMATAG(QWC,0,DMA_TAG_RET,0,0,SPR), \ Q->sw[2] = (u32)(W2), \ Q->sw[3] = (u32)(W3) /** Insert anywhere, qwc at addr is sent, reads next qword to continue */ #define DMATAG_REF(Q,QWC,ADDR,SPR,W2,W3) \ Q->dw[0] = DMATAG(QWC,0,DMA_TAG_REF,0,ADDR,SPR), \ Q->sw[2] = (u32)(W2), \ Q->sw[3] = (u32)(W3) /** Insert anywhere, qwc at addr is sent, reads next qword to continue */ #define DMATAG_REFS(Q,QWC,ADDR,SPR,W2,W3) \ Q->dw[0] = DMATAG(QWC,0,DMA_TAG_REFS,0,ADDR,SPR), \ Q->sw[2] = (u32)(W2), \ Q->sw[3] = (u32)(W3) /** Insert anywhere, qwc at addr is sent, then ends transfer */ #define DMATAG_REFE(Q,QWC,ADDR,SPR,W2,W3) \ Q->dw[0] = DMATAG(qwc, 0, DMA_TAG_REFE, 0, addr, spr), \ Q->sw[2] = (u32)(W2), \ Q->sw[3] = (u32)(W3) // Channel Control static volatile uint32_t * dma_chcr[10] = { (volatile uint32_t *)0x10008000, (volatile uint32_t *)0x10009000, (volatile uint32_t *)0x1000A000, (volatile uint32_t *)0x1000B000, (volatile uint32_t *)0x1000B400, (volatile uint32_t *)0x1000C000, (volatile uint32_t *)0x1000C400, (volatile uint32_t *)0x1000C800, (volatile uint32_t *)0x1000D000, (volatile uint32_t *)0x1000D400 }; // Quadword Count static volatile uint32_t * dma_qwc[10] = { (volatile uint32_t *)0x10008020, (volatile uint32_t *)0x10009020, (volatile uint32_t *)0x1000A020, (volatile uint32_t *)0x1000B020, (volatile uint32_t *)0x1000B420, (volatile uint32_t *)0x1000C020, (volatile uint32_t *)0x1000C420, (volatile uint32_t *)0x1000C820, (volatile uint32_t *)0x1000D020, (volatile uint32_t *)0x1000D420 }; // Memory Address static volatile uint32_t * dma_madr[10] = { (volatile uint32_t *)0x10008010, (volatile uint32_t *)0x10009010, (volatile uint32_t *)0x1000A010, (volatile uint32_t *)0x1000B010, (volatile uint32_t *)0x1000B410, (volatile uint32_t *)0x1000C010, (volatile uint32_t *)0x1000C410, (volatile uint32_t *)0x1000C810, (volatile uint32_t *)0x1000D010, (volatile uint32_t *)0x1000D410 }; // Tag Address static volatile uint32_t * dma_tadr[10] = { (volatile uint32_t *)0x10008030, (volatile uint32_t *)0x10009030, (volatile uint32_t *)0x1000A030, (volatile uint32_t *)0x1000B030, (volatile uint32_t *)0x1000B430, (volatile uint32_t *)0x1000C030, (volatile uint32_t *)0x1000C430, (volatile uint32_t *)0x1000C830, (volatile uint32_t *)0x1000D030, (volatile uint32_t *)0x1000D430 }; // Tag Address Save 0 static volatile uint32_t * dma_asr0[10] = { (volatile uint32_t *)0x10008040, (volatile uint32_t *)0x10009040, (volatile uint32_t *)0x1000A040, (volatile uint32_t *)0x00000000, (volatile uint32_t *)0x00000000, (volatile uint32_t *)0x00000000, (volatile uint32_t *)0x00000000, (volatile uint32_t *)0x00000000, (volatile uint32_t *)0x00000000, (volatile uint32_t *)0x00000000 }; // Tag Address Save 1 static volatile uint32_t * dma_asr1[10] = { (volatile uint32_t *)0x10008050, (volatile uint32_t *)0x10009050, (volatile uint32_t *)0x1000A050, (volatile uint32_t *)0x00000000, (volatile uint32_t *)0x00000000, (volatile uint32_t *)0x00000000, (volatile uint32_t *)0x00000000, (volatile uint32_t *)0x00000000, (volatile uint32_t *)0x00000000, (volatile uint32_t *)0x00000000 }; // SPR Transfer Address static volatile uint32_t * dma_sadr[10] = { (volatile uint32_t *)0x10008080, (volatile uint32_t *)0x10009080, (volatile uint32_t *)0x1000A080, (volatile uint32_t *)0x00000000, (volatile uint32_t *)0x00000000, (volatile uint32_t *)0x00000000, (volatile uint32_t *)0x00000000, (volatile uint32_t *)0x00000000, (volatile uint32_t *)0x1000D080, (volatile uint32_t *)0x1000D480 }; int _dma_init(){ // Reset the control registers. *DMA_REG_CTRL = 0x00000000; *DMA_REG_STAT = 0x00000000; *DMA_REG_PCR = 0x00000000; *DMA_REG_SQWC = 0x00000000; *DMA_REG_RBSR = 0x00000000; *DMA_REG_RBOR = 0x00000000; // Enable all channels, enable cycle stealing. *DMA_REG_CTRL = DMA_SET_CTRL( 1, 1, 0, 0, 0, 0 ); // End function. return 0; }; static uint32_t dma_handler_id[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; static uint8_t dma_channel_initialized[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; int dma_reset(void){ uint8_t idx = 0; for(; idx < 10; idx++){ dma_channel_shutdown( idx, 0 ); } ResetEE( 0x01 ); return 0; }; int dma_channel_initialize( uint8_t channel, void *handler, uint32_t flags ){ // Shut down the channel before making changes. if( dma_channel_shutdown(channel, flags ) < 0 ) return -1; // Clear any saved dmatags if( *dma_asr0[channel] ){ *dma_asr0[channel] = 0; *dma_asr1[channel] = 0; }; // Clear saved spr address if( *dma_sadr[channel] ){ *dma_sadr[channel] = 0; }; // If a handler is provided... if( handler != NULL ){ // Add the handler, storing the handler id. dma_handler_id[channel] = AddDmacHandler( channel, handler, 0 ); // Enable the channel interrupt. if( flags & DMA_FLAG_INTERRUPTSAFE ) { iEnableDmac(channel); } else { EnableDmac(channel); }; }; // Tell everyone we are initialized. dma_channel_initialized[channel] = 1; // End function. return 0; }; // allows for cpcond0 checking //void dma_channel_fast_waits( uint8_t channel ){ // *DMA_REG_PCR |= 1 << channel; //}; void dma_wait_fast(void){ __asm__ ( "sync.l; sync.p;" \ "0:" \ "bc0t 0f; nop;" \ "bc0t 0f; nop;" \ "bc0t 0f; nop;" \ "bc0f 0b; nop;" \ "0:" ); }; int dma_channel_wait( uint8_t channel, uint32_t timeout ){ // While the channel is not ready... while( *dma_chcr[channel] & 0x00000100 ){ // Decrement the timeout counter, exiting if it expires. if( !( timeout-- ) ) return -1; }; // End function. return 0; }; void dma_channel_send_packet2( packet2_t *packet2, uint8_t channel, uint8_t flush_cache ){ if( packet2 == NULL ) return; // dma_channel_send_chain does NOT flush all data that is "source chained" if( packet2->mode == P2_MODE_CHAIN ){ // "dma_channel_send_normal always flushes the data cache" if( flush_cache ) FlushCache(0); dma_channel_send_chain( channel, (void *)((uint32_t)packet2->base & 0x0FFFFFFF ), 0, packet2->tte ? DMA_FLAG_TRANSFERTAG : 0, 0 ); } else { dma_channel_send_normal( channel, (void *)((uint32_t)packet2->base & 0x0FFFFFFF), // make ptr normal ((uint32_t)packet2->next - (uint32_t)packet2->base) >> 4, // length in qwords 0, 0 ); }; }; int dma_channel_send_chain( uint8_t channel, void *data, uint32_t data_size, uint32_t flags, uint32_t spr ){ if( data == NULL ) return -1; // clear channel status *DMA_REG_STAT = DMA_SET_STAT(1 << channel,0,0,0,0,0,0); if( flags & DMA_FLAG_INTERRUPTSAFE ){ iSyncDCache( data, data + (data_size<<4) ); } else { SyncDCache( data, data + (data_size<<4) ); }; // Set the size of the data, in quadwords. *dma_qwc[channel] = DMA_SET_QWC(0); // Set the address of the data. *dma_madr[channel] = DMA_SET_MADR(0, 0); // Set the address of the data tag. *dma_tadr[channel] = DMA_SET_TADR( data, spr ); // Start the transfer. *dma_chcr[channel] = DMA_SET_CHCR( 1, 1, 0, flags & DMA_FLAG_TRANSFERTAG, 1, 1, 0 ); return 0; }; int dma_channel_send_chain_ucab( uint8_t channel, void *data, uint32_t qwc, uint32_t flags){ if( data == NULL ) return -1; // clear channel status *DMA_REG_STAT = 1 << channel; // Set the size of the data, in quadwords. *dma_qwc[channel] = DMA_SET_QWC(0); // Set the address of the data. *dma_madr[channel] = DMA_SET_MADR(0, 0); // Set the address of the data tag. *dma_tadr[channel] = DMA_SET_TADR( data - 0x30000000, 0 ); // Start the transfer. *dma_chcr[channel] = DMA_SET_CHCR( 1, 1, 0, flags & DMA_FLAG_TRANSFERTAG, 1, 1, 0 ); // End function. return 0; }; int dma_channel_send_normal( uint8_t channel, void *data, uint32_t qwc, uint32_t flags, uint32_t spr){ if( data == NULL ) return -1; // clear channel status *DMA_REG_STAT = DMA_SET_STAT(1 << channel,0,0,0,0,0,0); // Not sure if this should be here. if( flags & DMA_FLAG_INTERRUPTSAFE ){ iSyncDCache( data, data + (qwc<<4) ); } else { SyncDCache( data, data + (qwc<<4) ); }; // Set the size of the data, in quadwords. *dma_qwc[channel] = DMA_SET_QWC( qwc ); // Set the address of the data. *dma_madr[channel] = DMA_SET_MADR( data, spr ); // Start the transfer. *dma_chcr[channel] = DMA_SET_CHCR( 1, 0, 0, flags & DMA_FLAG_TRANSFERTAG, 1, 1, 0 ); return 0; }; int dma_channel_send_normal_ucab( uint8_t channel, void *data, uint32_t qwc, uint32_t flags ){ if( data == NULL ) return -1; // clear channel status *DMA_REG_STAT = DMA_SET_STAT(1 << channel,0,0,0,0,0,0); // Not sure if this should be here. if( flags & DMA_FLAG_INTERRUPTSAFE ){ iSyncDCache(data, data + (qwc<<4)); } else { SyncDCache(data, data + (qwc<<4)); }; // Set the size of the data, in quadwords. *dma_qwc[channel] = DMA_SET_QWC(qwc); // Set the address of the data. *dma_madr[channel] = DMA_SET_MADR( data - 0x30000000, 0); // Start the transfer. *dma_chcr[channel] = DMA_SET_CHCR(1, 0, 0, flags & DMA_FLAG_TRANSFERTAG, 1, 1, 0); return 0; } int dma_channel_receive_chain( uint8_t channel, void *data, uint32_t data_size, uint32_t flags, uint32_t spr ){ if( data == NULL ) return -1; // If we are not initialized... if( !dma_channel_initialized[channel] ){ // Initialize the channel. if( dma_channel_initialize( channel, NULL, flags ) < 0 ) return -1; }; // Set the size of the data, in quadwords. *dma_qwc[channel] = DMA_SET_QWC((data_size + 15) >> 4); // Set the address of the data. *dma_madr[channel] = DMA_SET_MADR( data, spr ); // Start the transfer. *dma_chcr[channel] = DMA_SET_CHCR( 0, 1, 0, 0, 0, 1, 0 ); // End function. return 0; }; int dma_channel_receive_normal( uint8_t channel, void *data, uint32_t data_size, uint32_t flags, uint32_t spr){ if( data == NULL ) return -1; // If we are not initialized... if( !dma_channel_initialized[channel] ) { // Initialize the channel. if( dma_channel_initialize( channel, NULL, flags ) < 0 ) return -1; }; // Set the size of the data, in quadwords. *dma_qwc[channel] = DMA_SET_QWC( (data_size + 15) >> 4 ); // Set the address of the data. *dma_madr[channel] = DMA_SET_MADR( data, spr ); // Start the transfer. *dma_chcr[channel] = DMA_SET_CHCR( 0, 0, 0, 0, 0, 1, 0 ); // End function. return 0; }; int dma_channel_shutdown( uint8_t channel, uint32_t flags ){ // If we are not initialized, no need to shut down. if( !dma_channel_initialized[channel] ) return 0; // If a handler was provided... if( dma_handler_id[channel] != 0 ){ // Disable the channel. if( flags & DMA_FLAG_INTERRUPTSAFE ){ iDisableDmac( channel ); } else { DisableDmac( channel ); }; // Remove the handler. RemoveDmacHandler( channel, dma_handler_id[channel] ); // Clear the handler id. dma_handler_id[channel] = 0; }; // Tell everyone we are not initialized. dma_channel_initialized[channel] = 0; // End function. return 0; };