Skip to content

Commit

Permalink
Refactor similar functions into EXI_DmaEx
Browse files Browse the repository at this point in the history
  • Loading branch information
Extrems committed Sep 12, 2023
1 parent c4a963d commit 63e2aeb
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 115 deletions.
14 changes: 13 additions & 1 deletion gc/ogc/exi.h
Original file line number Diff line number Diff line change
Expand Up @@ -257,14 +257,26 @@ s32 EXI_ImmEx(s32 nChn,void *pData,u32 nLen,u32 nMode);
\param[in] nChn EXI channel to select
\param[in,out] pData pointer to a buffer to read/copy from/to data.
\param[in] nLen lenght of data to transfer.
\param[in] nMode direction of transferoperation(EXI_READ,EXI_WRITE,EXI_READWRITE)
\param[in] nMode direction of transferoperation(EXI_READ,EXI_WRITE)
\param[in] tc_cb pointer to a callback to call when transfer has completed. May be NULL.
\return 1 on success, <=0 on error
*/
s32 EXI_Dma(s32 nChn,void *pData,u32 nLen,u32 nMode,EXICallback tc_cb);


/*! \fn s32 EXI_DmaEx(s32 nChn,void *pData,u32 nLen,u32 nMode)
\brief Initializes an extended DMA mode EXI transfer.
\param[in] nChn EXI channel to select
\param[in,out] pData pointer to a buffer to read/copy from/to data.
\param[in] nLen lenght of data to transfer.
\param[in] nMode direction of transferoperation(EXI_READ,EXI_WRITE)
\return 1 on success, <=0 on error
*/
s32 EXI_DmaEx(s32 nChn,void *pData,u32 nLen,u32 nMode);


/*! \fn s32 EXI_GetState(s32 nChn)
\brief Get the EXI state
\param[in] nChn EXI channel to select
Expand Down
42 changes: 35 additions & 7 deletions libogc/exi.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ distribution.
#include "irq.h"
#include "processor.h"
#include "spinlock.h"
#include "cache.h"
#include "exi.h"

//#define _EXI_DEBUG
Expand Down Expand Up @@ -498,23 +499,19 @@ s32 EXI_ImmEx(s32 nChn,void *pData,u32 nLen,u32 nMode)
{
u8 *buf = pData;
u32 tc;
s32 ret = 0;
#ifdef _EXI_DEBUG
printf("EXI_ImmEx(%d,%p,%d,%d)\n",nChn,pData,nLen,nMode);
#endif
while(nLen) {
ret = 0;
tc = nLen;
if(tc>4) tc = 4;

if(!EXI_Imm(nChn,buf,tc,nMode,NULL)) break;
if(!EXI_Sync(nChn)) break;
if(!EXI_Imm(nChn,buf,tc,nMode,NULL)) return 0;
if(!EXI_Sync(nChn)) return 0;
nLen -= tc;
buf += tc;

ret = 1;
}
return ret;
return 1;
}

s32 EXI_Dma(s32 nChn,void *pData,u32 nLen,u32 nMode,EXICallback tc_cb)
Expand Down Expand Up @@ -554,6 +551,37 @@ s32 EXI_Dma(s32 nChn,void *pData,u32 nLen,u32 nMode,EXICallback tc_cb)
return 1;
}

s32 EXI_DmaEx(s32 nChn,void *pData,u32 nLen,u32 nMode)
{
u32 roundlen;
s32 missalign;
s32 len = nLen;
u8 *ptr = pData;

if(!ptr || len<=0) return 0;

missalign = -((u32)ptr)&0x1f;
if((len-missalign)<32) return EXI_ImmEx(nChn,ptr,len,nMode);

if(missalign>0) {
if(!EXI_ImmEx(nChn,ptr,missalign,nMode)) return 0;
len -= missalign;
ptr += missalign;
}

roundlen = (len&~0x1f);
if(nMode==EXI_READ) DCInvalidateRange(ptr,roundlen);
else DCStoreRange(ptr,roundlen);
if(!EXI_Dma(nChn,ptr,roundlen,nMode,NULL)) return 0;
if(!EXI_Sync(nChn)) return 0;

len -= roundlen;
ptr += roundlen;
if(len>0) return EXI_ImmEx(nChn,ptr,len,nMode);

return 1;
}

s32 EXI_GetState(s32 nChn)
{
exibus_priv *exi = &eximap[nChn];
Expand Down
33 changes: 2 additions & 31 deletions libogc/sdgecko_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -240,35 +240,6 @@ static u16 __make_crc16(void *buffer,u32 len)
}
*/

static s32 __card_writedata_fast(s32 drv_no,void *buf,s32 len)
{
u32 roundlen;
s32 missalign;
u8 *ptr = buf;

if(!ptr || len<=0) return 0;

missalign = -((u32)ptr)&0x1f;
if((len-missalign)<32) return EXI_ImmEx(drv_no,ptr,len,EXI_WRITE);

if(missalign>0) {
if(EXI_ImmEx(drv_no,ptr,missalign,EXI_WRITE)==0) return 0;
len -= missalign;
ptr += missalign;
}

roundlen = (len&~0x1f);
DCStoreRange(ptr,roundlen);
if(EXI_Dma(drv_no,ptr,roundlen,EXI_WRITE,NULL)==0) return 0;
if(EXI_Sync(drv_no)==0) return 0;

len -= roundlen;
ptr += roundlen;
if(len>0) return EXI_ImmEx(drv_no,ptr,len,EXI_WRITE);

return 1;
}

static u32 __card_checktimeout(s32 drv_no,u32 startT,u32 timeout)
{
u32 endT,diff;
Expand Down Expand Up @@ -788,7 +759,7 @@ static s32 __card_datawrite(s32 drv_no,void *buf,u32 len)
return CARDIO_ERROR_IOERROR;
}

if(__card_writedata_fast(drv_no,buf,len)==0) {
if(EXI_DmaEx(drv_no,buf,len,EXI_WRITE)==0) {
EXI_Deselect(drv_no);
EXI_Unlock(drv_no);
return CARDIO_ERROR_IOERROR;
Expand Down Expand Up @@ -832,7 +803,7 @@ static s32 __card_multidatawrite(s32 drv_no,void *buf,u32 len)
return CARDIO_ERROR_IOERROR;
}

if(__card_writedata_fast(drv_no,buf,len)==0) {
if(EXI_DmaEx(drv_no,buf,len,EXI_WRITE)==0) {
EXI_Deselect(drv_no);
EXI_Unlock(drv_no);
return CARDIO_ERROR_IOERROR;
Expand Down
78 changes: 4 additions & 74 deletions lwip/arch/gc/netif/gcif.c
Original file line number Diff line number Diff line change
Expand Up @@ -272,42 +272,7 @@ static __inline__ void bba_insregister(u32 reg)

static __inline__ void bba_insdata(void *val,u32 len)
{
EXI_ImmEx(EXI_CHANNEL_0,val,len,EXI_READ);
}

static __inline__ void bba_insdmadata(void *val,u32 len,s32 (*dmasubrcv)(s32 chn,s32 dev))
{
EXI_Dma(EXI_CHANNEL_0,val,len,EXI_READ,dmasubrcv);
}

static void bba_insdata_fast(void *val,s32 len)
{
u32 roundlen;
s32 missalign;
u8 *ptr = val;

if(!val || len<=0) return;

missalign = -((u32)val)&0x1f;
if((s32)(len-missalign)<32) {
bba_insdata(val,len);
return;
}

if(missalign>0) {
bba_insdata(ptr,missalign);
len -= missalign;
ptr += missalign;
}

roundlen = (len&~0x1f);
DCInvalidateRange(ptr,roundlen);
bba_insdmadata(ptr,roundlen,NULL);
bba_sync();

len -= roundlen;
ptr += roundlen;
if(len>0) bba_insdata(ptr,len);
EXI_DmaEx(EXI_CHANNEL_0,val,len,EXI_READ);
}

static __inline__ void bba_outsregister(u32 reg)
Expand All @@ -320,42 +285,7 @@ static __inline__ void bba_outsregister(u32 reg)

static __inline__ void bba_outsdata(void *val,u32 len)
{
EXI_ImmEx(EXI_CHANNEL_0,val,len,EXI_WRITE);
}

static __inline__ void bba_outsdmadata(void *val,u32 len,s32 (*dmasubsnd)(s32 chn,s32 dev))
{
EXI_Dma(EXI_CHANNEL_0,val,len,EXI_WRITE,dmasubsnd);
}

static void bba_outsdata_fast(void *val,s32 len)
{
u32 roundlen;
s32 missalign;
u8 *ptr = val;

if(!val || len<=0) return;

missalign = -((u32)val)&0x1f;
if((s32)(len-missalign)<32) {
bba_outsdata(val,len);
return;
}

if(missalign>0) {
bba_outsdata(ptr,missalign);
len -= missalign;
ptr += missalign;
}

roundlen = (len&~0x1f);
DCStoreRange(ptr,roundlen);
bba_outsdmadata(ptr,roundlen,NULL);
bba_sync();

len -= roundlen;
ptr += roundlen;
if(len>0) bba_outsdata(ptr,len);
EXI_DmaEx(EXI_CHANNEL_0,val,len,EXI_WRITE);
}

static inline void bba_cmd_ins(u32 reg,void *val,u32 len)
Expand Down Expand Up @@ -651,7 +581,7 @@ static err_t __bba_link_tx(struct netif *dev,struct pbuf *p)
bba_select();
bba_outsregister(BBA_WRTXFIFOD);
for(tmp=p;tmp!=NULL;tmp=tmp->next) {
bba_outsdata_fast(tmp->payload,tmp->len);
bba_outsdata(tmp->payload,tmp->len);
}
if(p->tot_len<BBA_MINPKTSIZE) bba_outsdata(tmpbuf,(BBA_MINPKTSIZE-p->tot_len));
bba_deselect();
Expand Down Expand Up @@ -710,7 +640,7 @@ static err_t bba_start_rx(struct netif *dev,u32 budget)

bba_select();
bba_insregister(pos);
bba_insdata_fast(tmp->payload,size);
bba_insdata(tmp->payload,size);
bba_deselect();
pos += size;
}
Expand Down
4 changes: 2 additions & 2 deletions lwip/netif/enc28j60if.c
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ static bool ENC28J60_ReadCmd(s32 chan, u32 cmd, void *buf, u32 len)

err |= !EXI_Imm(chan, &cmd, 1 + ENC28J60_EXI_DUMMY(cmd), EXI_WRITE, NULL);
err |= !EXI_Sync(chan);
err |= !EXI_ImmEx(chan, buf, len, EXI_READ);
err |= !EXI_DmaEx(chan, buf, len, EXI_READ);
err |= !EXI_Deselect(chan);
return !err;
}
Expand All @@ -330,7 +330,7 @@ static bool ENC28J60_WriteCmd(s32 chan, u32 cmd, const void *buf, u32 len)

err |= !EXI_Imm(chan, &cmd, 1, EXI_WRITE, NULL);
err |= !EXI_Sync(chan);
err |= !EXI_ImmEx(chan, (void *)buf, len, EXI_WRITE);
err |= !EXI_DmaEx(chan, (void *)buf, len, EXI_WRITE);
err |= !EXI_Deselect(chan);
return !err;
}
Expand Down

0 comments on commit 63e2aeb

Please sign in to comment.