7 #include <machine/processor.h>
11 #define _SHIFTL(v, s, w) \
12 ((u32) (((u32)(v) & ((0x01 << (w)) - 1)) << (s)))
13 #define _SHIFTR(v, s, w) \
14 ((u32)(((u32)(v) >> (s)) & ((0x01 << (w)) - 1)))
17 static u32 usbgecko_inited
= 0;
18 static lwpq_t wait_exi_queue
[2];
20 static s32
__usbgecko_exi_unlock(s32 chan
,s32 dev
)
22 LWP_ThreadBroadcast(wait_exi_queue
[chan
]);
26 static void __usbgecko_init()
30 for(i
=0;i
<EXI_CHANNEL_2
;i
++) {
31 LWP_InitQueue(&wait_exi_queue
[i
]);
36 static __inline__
void __usbgecko_exi_wait(s32 chn
)
40 _CPU_ISR_Disable(level
);
41 if(!usbgecko_inited
) __usbgecko_init();
42 while(EXI_Lock(chn
,EXI_DEVICE_0
,__usbgecko_exi_unlock
)==0) {
43 LWP_ThreadSleep(wait_exi_queue
[chn
]);
45 _CPU_ISR_Restore(level
);
48 static __inline__
int __send_command(s32 chn
,u16
*cmd
)
52 if(!EXI_Select(chn
,EXI_DEVICE_0
,EXI_SPEED32MHZ
)) ret
|= 0x01;
53 if(!EXI_Imm(chn
,cmd
,sizeof(u16
),EXI_READWRITE
,NULL
)) ret
|= 0x02;
54 if(!EXI_Sync(chn
)) ret
|= 0x04;
55 if(!EXI_Deselect(chn
)) ret
|= 0x08;
61 static int __usb_sendbyte(s32 chn
,char ch
)
66 val
= (0xB000|_SHIFTL(ch
,4,8));
67 ret
= __send_command(chn
,&val
);
68 if(ret
==1 && !(val
&0x0400)) ret
= 0;
73 static int __usb_recvbyte(s32 chn
,char *ch
)
80 ret
= __send_command(chn
,&val
);
81 if(ret
==1 && !(val
&0x0800)) ret
= 0;
82 else if(ret
==1) *ch
= (val
&0xff);
87 int __usb_checksend(s32 chn
)
93 ret
= __send_command(chn
,&val
);
94 if(ret
==1 && !(val
&0x0400)) ret
= 0;
99 int __usb_checkrecv(s32 chn
)
105 ret
= __send_command(chn
,&val
);
106 if(ret
==1 && !(val
&0x0400)) ret
= 0;
111 void usb_flush(s32 chn
)
115 __usbgecko_exi_wait(chn
);
116 while(__usb_recvbyte(chn
,&tmp
));
120 int usb_isgeckoalive(s32 chn
)
126 if (EXI_GetID(chn
, EXI_DEVICE_0
, &id
) == 0)
132 __usbgecko_exi_wait(chn
);
135 ret
= __send_command(chn
,&val
);
136 if(ret
==1 && !(val
&0x0470)) ret
= 0;
143 int usb_recvbuffer_ex(s32 chn
,void *buffer
,int size
, int retries
)
147 char *ptr
= (char*)buffer
;
149 __usbgecko_exi_wait(chn
);
151 ret
= __usb_recvbyte(chn
,ptr
);
165 return (size
- left
);
168 int usb_recvbuffer(s32 chn
,void *buffer
,int size
) {
169 return usb_recvbuffer_ex(chn
, buffer
, size
, -1);
172 int usb_sendbuffer_ex(s32 chn
,const void *buffer
,int size
, int retries
)
176 char *ptr
= (char*)buffer
;
178 __usbgecko_exi_wait(chn
);
180 ret
= __usb_sendbyte(chn
,*ptr
);
194 return (size
- left
);
197 int usb_sendbuffer(s32 chn
,const void *buffer
,int size
) {
198 return usb_sendbuffer_ex(chn
, buffer
, size
, -1);
201 int usb_recvbuffer_safe_ex(s32 chn
,void *buffer
,int size
, int retries
)
205 char *ptr
= (char*)buffer
;
207 __usbgecko_exi_wait(chn
);
209 if(__usb_checkrecv(chn
)) {
210 ret
= __usb_recvbyte(chn
,ptr
);
225 return (size
- left
);
228 int usb_recvbuffer_safe(s32 chn
,void *buffer
,int size
) {
229 return usb_recvbuffer_safe_ex(chn
, buffer
, size
, -1);
232 int usb_sendbuffer_safe_ex(s32 chn
,const void *buffer
,int size
, int retries
)
236 char *ptr
= (char*)buffer
;
238 __usbgecko_exi_wait(chn
);
240 if(__usb_checksend(chn
)) {
241 ret
= __usb_sendbyte(chn
,*ptr
);
256 return (size
- left
);
259 int usb_sendbuffer_safe(s32 chn
,const void *buffer
,int size
) {
260 return usb_sendbuffer_safe_ex(chn
, buffer
, size
, -1);