x1000_dwc.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027
  1. #include <rthw.h>
  2. #include <rtthread.h>
  3. #include <rtdevice.h>
  4. #include <drivers/usb_device.h>
  5. #include <stdio.h>
  6. #include <stdint.h>
  7. #include <string.h>
  8. #include <x1000.h>
  9. #include <mips_regs.h>
  10. #include <cache.h>
  11. #include "x1000_dwc.h"
  12. //#define DWC_DEBUG
  13. #ifdef DWC_DEBUG
  14. #define DWC_DBG(fmt, args...) rt_kprintf(fmt ,##args)
  15. #else
  16. #define DWC_DBG(fmt, args...)
  17. #endif
  18. #define UdcID (('U' << 24) | ('D' << 16) | ('C' << 16) | (':' << 16))
  19. #define IS_SLAVE_MODE 0
  20. #define IS_INTERN_DMA 2
  21. #define IS_EXTERN_DMA 1
  22. const char *ep0_state_string[] =
  23. {
  24. "EP_SETUP",
  25. "EP_DATA",
  26. "EP_STATUS",
  27. "EP_SETUP_PHASEDONE",
  28. };
  29. #if DWC_FORCE_SPEED_FULL
  30. #define DEP_EP_MAXPKT(n) \
  31. ({ \
  32. int v = 0; \
  33. if (n) \
  34. v = 64; \
  35. else \
  36. v = 64; \
  37. v; \
  38. })
  39. #else
  40. #define DEP_EP_MAXPKT(n) \
  41. ({ \
  42. int v = 0; \
  43. if (n) \
  44. v = 512; \
  45. else \
  46. v = 64; \
  47. v; \
  48. })
  49. #endif
  50. #define MAX_PKT_CNT 1023
  51. ALIGN(32)
  52. //static uint32_t setup_packet[64] = {0, 0, 0, 0, 0};
  53. static int sleep_flag = 0;
  54. /*
  55. * static functions
  56. */
  57. static void dwc_otg_device_init(dwc_handle *dwc);
  58. static void dwc_otg_core_reset(dwc_handle *dwc);
  59. static void dwc_otg_core_init(dwc_handle *dwc,uint8_t dma_enable);
  60. static void dwc_otg_phy_suspend(int suspend);
  61. static void udelay(uint32_t x)
  62. {
  63. volatile uint32_t n = 1000;
  64. while(x--)
  65. {
  66. for (n = 0; n < 1000; ++n);
  67. }
  68. }
  69. static void mdelay(uint32_t x)
  70. {
  71. while(x--)
  72. udelay(1000);
  73. }
  74. static int dwc_get_utmi_width(dwc_handle *dwc)
  75. {
  76. return (REG_GHW_CFG4 >> 14) & 0x3;
  77. }
  78. static void dwc_otg_select_phy_width(dwc_handle *dwc)
  79. {
  80. REG_GUSB_CFG &= ~USBCFG_TRDTIME_MASK;
  81. REG_GUSB_CFG |= (1 << 3);
  82. REG_GUSB_CFG |= USBCFG_TRDTIME_6;
  83. REG_CPM_USBPCR1 |= (3 << 18);
  84. }
  85. static void dwc_otg_write_packet(dwc_handle *dwc, uint8_t epnum)
  86. {
  87. int i;
  88. uint32_t dwords;
  89. uint32_t byte_count;
  90. dwc_ep *pep;
  91. epnum &= DWC_EPNO_MASK;
  92. pep = dwc->dep[DWC_EP_IN_OFS + epnum];
  93. byte_count = pep->xfer_len - pep->xfer_count;
  94. if (byte_count > DEP_EP_MAXPKT(epnum))
  95. byte_count = DEP_EP_MAXPKT(epnum);
  96. dwords = (byte_count + 3) / 4;
  97. for (i = 0; i < dwords; i++)
  98. {
  99. REG_EP_FIFO(epnum) = REG32((uint32_t * )(pep->xfer_buff) + i);
  100. }
  101. pep->xfer_count += byte_count;
  102. pep->xfer_buff += byte_count;
  103. }
  104. void dwc_read_ep_packet(dwc_handle *dwc, uint8_t epnum, uint32_t count)
  105. {
  106. int i;
  107. int dwords = (count + 3) / 4;
  108. dwc_ep *pep;
  109. epnum &= DWC_EPNO_MASK;
  110. pep = dwc->dep[DWC_EP_OUT_OFS + epnum];
  111. for (i = 0; i < dwords; i++)
  112. REG32((uint32_t *)(pep->xfer_buff + pep->xfer_count / 4) + i) = REG_EP_FIFO(epnum);
  113. pep->xfer_count += count;
  114. }
  115. void dwc_write_ep_packet(dwc_handle *dwc,uint8_t epnum)
  116. {
  117. uint32_t xfersize, finish, insize;
  118. uint32_t dwords;
  119. uint32_t txstatus = REG_DIEP_TXFSTS(epnum & 0x0F);
  120. dwc_ep *pep;
  121. epnum &= DWC_EPNO_MASK;
  122. pep = dwc->dep[DWC_EP_IN_OFS + epnum];
  123. insize = pep->xfer_len;
  124. if (pep->xfer_len > DEP_EP_MAXPKT(epnum))
  125. xfersize = DEP_EP_MAXPKT(epnum);
  126. else
  127. xfersize = pep->xfer_len;
  128. dwords = (xfersize + 3) / 4;
  129. DWC_DBG("txstatus (%x) dwords (%x) length (%x) xfer_count (%x) \n", txstatus, dwords, pep->xfer_len, pep->xfer_count);
  130. while ((txstatus > dwords) && (pep->xfer_len > 0) && (pep->xfer_count < pep->xfer_len) )
  131. {
  132. dwc_otg_write_packet(dwc, epnum);
  133. xfersize = pep->xfer_len - pep->xfer_count;
  134. if (xfersize > DEP_EP_MAXPKT(epnum))
  135. xfersize = DEP_EP_MAXPKT(epnum);
  136. dwords = (xfersize + 3) / 4;
  137. txstatus = REG_DIEP_TXFSTS(epnum);
  138. }
  139. finish = pep->xfer_count;
  140. if (insize > finish)
  141. {
  142. uint32_t intr = REG_DIEP_INT(epnum);
  143. while (!(intr & DEP_TXFIFO_EMPTY))
  144. {
  145. intr = REG_DIEP_INT(epnum);
  146. }
  147. HW_SendPKT(dwc,epnum, pep->xfer_buff, insize - finish);
  148. }
  149. return;
  150. }
  151. void dwc_handle_ep_data_in_phase(dwc_handle *dwc, uint8_t epnum)
  152. {
  153. uint32_t pktcnt, xfersize;
  154. uint32_t dma_addr, dma_len;
  155. dwc_ep *pep;
  156. DWC_DBG("%s %d\n",__func__,__LINE__);
  157. DWC_DBG("epnum = %d\n",epnum);
  158. epnum &= DWC_EPNO_MASK;
  159. pep = dwc->dep[DWC_EP_IN_OFS + epnum];
  160. xfersize = pep->xfer_len;
  161. pktcnt = (xfersize + DEP_EP_MAXPKT(epnum) - 1) / DEP_EP_MAXPKT(epnum);
  162. if (pktcnt > 1023)
  163. {
  164. DWC_DBG("WARNING...\n");
  165. while (1) ;
  166. }
  167. if (epnum == 0)
  168. {
  169. REG_DIEP_SIZE(epnum) &= ~(0x1fffff);
  170. REG_DIEP_SIZE(epnum) |= (pktcnt << 19) | xfersize;
  171. }
  172. else
  173. {
  174. REG_DIEP_SIZE(epnum) &= ~(0x1fffffff);
  175. REG_DIEP_SIZE(epnum) |= (pktcnt << 19) | xfersize;
  176. }
  177. if (dwc->is_dma != 0)
  178. {
  179. dma_addr = (uint32_t)(pep->xfer_buff);
  180. dma_len = (((pep->xfer_len + 7) >> 3) << 3);
  181. //dump data...
  182. DWC_DBG("IN:\n");
  183. {
  184. int i;
  185. for (i = 0; i < dma_len; ++i)
  186. {
  187. DWC_DBG("%02x ", *(unsigned char *)(dma_addr+i));
  188. if ((i + 1) % 16 == 0)
  189. DWC_DBG("\n");
  190. }
  191. }
  192. DWC_DBG("\n");
  193. REG_DIEP_DMA(epnum) = PHYS(pep->xfer_buff);
  194. REG_DIEP_CTL(epnum) |= (DEP_ENA_BIT | DEP_CLEAR_NAK);
  195. }
  196. else
  197. {
  198. REG_DIEP_CTL(epnum) |= (DEP_ENA_BIT | DEP_CLEAR_NAK);
  199. REG_DIEP_EMPMSK |= (1 << epnum);
  200. }
  201. return ;
  202. }
  203. void dwc_handle_ep_status_in_phase(dwc_handle *dwc, uint8_t epnum)
  204. {
  205. dwc_ep *pep;
  206. DWC_DBG("%s %d\n",__func__,__LINE__);
  207. DWC_DBG("epnum = %d\n",epnum);
  208. epnum &= DWC_EPNO_MASK;
  209. pep = dwc->dep[DWC_EP_IN_OFS + epnum];
  210. pep->xfer_len = 0;
  211. pep->xfer_count = 0;
  212. if (epnum == 0)
  213. {
  214. REG_DIEP_SIZE(epnum) &= ~(0x1fffff);
  215. REG_DIEP_SIZE(epnum) |= DOEPSIZE0_PKTCNT_BIT | (pep->xfer_len); // pktcnt->1 xfersize->0
  216. }
  217. else
  218. {
  219. REG_DIEP_SIZE(epnum) &= ~(0x1FFFFFFF);
  220. REG_DIEP_SIZE(epnum) |= DOEPSIZE0_PKTCNT_BIT | (pep->xfer_len); // pktcnt->1 xfersize->0
  221. }
  222. if (dwc->is_dma == IS_INTERN_DMA)
  223. {
  224. // pep->xfer_buff = (void *)0xFFFFFFFF;
  225. // REG_DIEP_DMA(epnum) = PHYS(pep->xfer_buff);
  226. REG_DIEP_DMA(epnum) = PHYS(0xFFFFFFFF);
  227. REG_DIEP_CTL(epnum) |= DEP_ENA_BIT | DEP_CLEAR_NAK;
  228. }
  229. else
  230. {
  231. REG_DIEP_CTL(epnum) |= DEP_ENA_BIT | DEP_CLEAR_NAK;
  232. }
  233. return ;
  234. }
  235. void dwc_handle_ep_data_out_phase(dwc_handle *dwc,uint8_t epnum)
  236. {
  237. uint32_t dma_addr, dma_len;
  238. uint32_t pktcnt;
  239. dwc_ep *pep;
  240. DWC_DBG("%s %d\n",__func__,__LINE__);
  241. DWC_DBG("epnum = %d\n",epnum);
  242. epnum &= DWC_EPNO_MASK;
  243. pep = dwc->dep[DWC_EP_OUT_OFS + epnum];
  244. if (epnum == 0)
  245. {
  246. if (dwc->is_dma == IS_INTERN_DMA)
  247. {
  248. dma_len = pep->maxpacket;
  249. dma_addr = (uint32_t) (pep->xfer_buff);
  250. rt_hw_dcache_flush_range(dma_addr,dma_len);
  251. REG_DOEP_DMA(epnum) = PHYS(pep->xfer_buff);
  252. }
  253. REG_DOEP_SIZE(epnum) = DOEPSIZE0_SUPCNT_3 | DOEPSIZE0_PKTCNT_BIT | (pep->maxpacket);
  254. REG_DOEP_CTL(epnum) |= DEP_ENA_BIT | DEP_CLEAR_NAK;
  255. }
  256. else
  257. {
  258. if (pep->xfer_len > 0)
  259. {
  260. if (pep->xfer_len > MAX_PKT_CNT * DEP_EP_MAXPKT(epnum))
  261. pep->xfer_len = MAX_PKT_CNT * DEP_EP_MAXPKT(epnum);
  262. pktcnt = (pep->xfer_len + DEP_EP_MAXPKT(epnum) - 1) / DEP_EP_MAXPKT(epnum);
  263. if (pktcnt > 1023)
  264. {
  265. DWC_DBG("WARNING...\n");
  266. while (1) ;
  267. }
  268. REG_DOEP_SIZE(epnum) &= ~(0x1fffffff);
  269. REG_DOEP_SIZE(epnum) |= (pktcnt << 19) | (pep->xfer_len);
  270. }
  271. if (dwc->is_dma == IS_INTERN_DMA)
  272. {
  273. dma_len = (((pep->xfer_len + 7) >> 3) << 3);
  274. dma_addr = (uint32_t)(pep->xfer_buff);
  275. rt_hw_dcache_flush_range(dma_addr, dma_len);
  276. REG_DOEP_DMA(epnum) = PHYS(pep->xfer_buff);
  277. }
  278. /* Program the DOEPCTLn Register with endpoint charateristics,
  279. * and set the Endpoint Enable and Clear NAK bit */
  280. REG_DOEP_CTL(epnum) |= DEP_ENA_BIT | DEP_CLEAR_NAK;
  281. }
  282. }
  283. int HW_SendPKT(dwc_handle *dwc, uint8_t epnum, const uint8_t *buf, int size)
  284. {
  285. uint32_t dma_addr, dma_len;
  286. dwc_ep *pep;
  287. rt_base_t level;
  288. DWC_DBG("HW_SendPKT addr = %02x,size = %d\n",epnum,size);
  289. epnum &= DWC_EPNO_MASK;
  290. pep = dwc->dep[DWC_EP_IN_OFS + epnum];
  291. pep->xfer_len = size; /* number of bytes to transfer */
  292. pep->xfer_count = 0; /* number of bytes transfered */
  293. // pep->xfer_buff = (uint8_t *)buf; /* pointer to transfer buffer */
  294. if(size > 0)
  295. {
  296. memcpy(pep->xfer_buff,buf,size);
  297. rt_hw_dcache_flush_range((rt_uint32_t)pep->xfer_buff,(rt_uint32_t)size);
  298. }
  299. if (pep->xfer_len > MAX_PKT_CNT * DEP_EP_MAXPKT(epnum))
  300. pep->xfer_len = MAX_PKT_CNT * DEP_EP_MAXPKT(epnum);
  301. pep->xfer_count = 0;
  302. switch (pep->type)
  303. {
  304. case DWC_OTG_EP_TYPE_CONTROL:
  305. if (pep->xfer_len > 0)
  306. pep->ep_state = EP_DATA;
  307. else
  308. pep->ep_state = EP_STATUS;
  309. /* 2 Stage */
  310. if (pep->ep_state == EP_STATUS && pep->xfer_len == 0) /*EP_SETUP 0 EP_DATA 1 EP_STATUS 2*/
  311. {
  312. DWC_DBG("%s %d ep_state = %s\n", __func__, __LINE__, ep0_state_string[pep->ep_state]);
  313. dwc_handle_ep_status_in_phase(dwc, 0);
  314. return 0;
  315. }
  316. /* 3 Stage */
  317. if (pep->ep_state == EP_DATA)
  318. {
  319. /* enable in data phase */
  320. dwc_handle_ep_data_in_phase(dwc, epnum);
  321. }
  322. break;
  323. case DWC_OTG_EP_TYPE_BULK:
  324. if (pep->ep_state == EP_IDLE || pep->ep_state == EP_TRANSFERED)
  325. {
  326. pep->ep_state = EP_TRANSFERING;
  327. if (pep->xfer_len == 0)
  328. {
  329. dwc_handle_ep_status_in_phase(dwc, epnum);
  330. return 0;
  331. }
  332. dwc_handle_ep_data_in_phase(dwc, epnum);
  333. }
  334. break;
  335. }
  336. return pep->xfer_len;
  337. }
  338. int HW_GetPKT(dwc_handle *dwc, uint8_t epnum, uint8_t *buf,int size)
  339. {
  340. int i;
  341. dwc_ep *pep;
  342. DWC_DBG("HW_GetPKT:%d %d\n", epnum, dwc->is_dma);
  343. epnum &= DWC_EPNO_MASK;
  344. pep = dwc->dep[DWC_EP_OUT_OFS + epnum];
  345. if ((size == 0) || (size > pep->xfer_count))
  346. size = pep->xfer_count;
  347. if (dwc->is_dma == IS_INTERN_DMA)
  348. {
  349. DWC_DBG("HW_GetPKT:%x %x \n", pep->ctrl_req_addr, UNCACHED(pep->xfer_buff));
  350. memcpy((uint8_t*) buf, (uint8_t *) UNCACHED(pep->xfer_buff), size);
  351. }
  352. else
  353. {
  354. memcpy((uint8_t*) buf, (uint8_t *) (pep->xfer_buff), size);
  355. }
  356. return size;
  357. }
  358. static void dwc_otg_flush_rx_fifo(dwc_handle *dwc)
  359. {
  360. ;
  361. }
  362. static void dwc_otg_flush_tx_fifo(dwc_handle *dwc,uint8_t epnum)
  363. {
  364. uint32_t gintsts;
  365. uint32_t grstctl;
  366. uint32_t cnt;
  367. gintsts = REG_GINT_STS;
  368. /* Step1: Check that GINTSTS.GinNakEff=0 if this
  369. * bit is cleared then set Dctl.SGNPInNak = 1.
  370. * Nak effective interrupt = H indicating the core
  371. * is not reading from fifo*/
  372. if ((gintsts & GINTSTS_GINNAK_EFF))
  373. {
  374. REG_OTG_DCTL |= DCTL_SGNPINNAK;
  375. /* Step2: wait for GINTSTS.GINNakEff=1,which indicates
  376. * the NAK setting has taken effect to all IN endpoints */
  377. while (!(REG_GINT_STS & GINTSTS_GINNAK_EFF))
  378. udelay(1);
  379. }
  380. /* Step3: wait for ahb master idle state */
  381. while (!(REG_GRST_CTL & RSTCTL_AHB_IDLE))
  382. udelay(1);
  383. /* Step4: Check that GrstCtl.TxFFlsh=0, if it is 0, then write
  384. * the TxFIFO number you want to flush to GrstCTL.TxFNum*/
  385. grstctl = REG_GRST_CTL;
  386. if (!(grstctl & RSTCTL_TXFIFO_FLUSH))
  387. {
  388. REG_GRST_CTL |= ((epnum & 0x0F) << 6);
  389. }
  390. /* Step5: Set GRSTCTL.TxFFlsh=1 and wait for it to clear */
  391. REG_GRST_CTL |= RSTCTL_TXFIFO_FLUSH;
  392. while (REG_GRST_CTL & RSTCTL_TXFIFO_FLUSH)
  393. {
  394. udelay(1);
  395. }
  396. /* Step6: Set the DCTL.GCNPinNak bit */
  397. REG_OTG_DCTL |= DCTL_CLR_GNPINNAK;
  398. }
  399. static void dwc_set_in_nak(dwc_handle *dwc, int epnum)
  400. {
  401. int timeout = 5000;
  402. epnum &= DWC_EPNO_MASK;
  403. REG_DIEP_CTL(epnum) |= DEP_SET_NAK;
  404. do
  405. {
  406. udelay(1);
  407. if (timeout < 2)
  408. {
  409. DWC_DBG("dwc set in nak timeout epnum %d\n", epnum);
  410. }
  411. } while ((!(REG_DIEP_INT(epnum) & DEP_INEP_NAKEFF)) && (--timeout > 0));
  412. }
  413. static void dwc_set_out_nak(dwc_handle *dwc,int epnum)
  414. {
  415. epnum &= DWC_EPNO_MASK;
  416. REG_DOEP_CTL(epnum) |= DEP_SET_NAK;
  417. }
  418. static void dwc_disable_in_ep(dwc_handle *dwc,int epnum)
  419. {
  420. int timeout = 100000;
  421. epnum &= DWC_EPNO_MASK;
  422. if (!(REG_DIEP_CTL(epnum) & DEP_ENA_BIT))
  423. return ;
  424. /*step 1 : set nak*/
  425. dwc_set_in_nak(dwc,epnum);
  426. /*step 2: disable endpoint*/
  427. REG_DIEP_CTL(epnum) |= DEP_DISENA_BIT;
  428. do
  429. {
  430. udelay(1);
  431. if (timeout < 2)
  432. {
  433. DWC_DBG("dwc disable in ep timeout epnum : %d\n", epnum);
  434. }
  435. } while ( (!(REG_DIEP_INT(epnum) & DEP_EPDIS_INT)) && (--timeout > 0));
  436. REG_DIEP_INT(epnum) = DEP_EPDIS_INT;
  437. /*step 3: flush tx fifo*/
  438. dwc_otg_flush_tx_fifo(dwc, epnum);
  439. REG_DIEP_SIZE(epnum) = 0x0;
  440. /*step 4: clear nak*/
  441. if (epnum == 1)
  442. REG_DIEP_CTL(1) |= DEP_CLEAR_NAK;
  443. }
  444. int dwc_enable_in_ep(dwc_handle *dwc,uint8_t epnum)
  445. {
  446. dwc_ep *pep = RT_NULL;
  447. epnum &= DWC_EPNO_MASK;
  448. pep = dwc->dep[DWC_EP_IN_OFS + epnum];
  449. /* Program the endpoint register to configure them with the characteristics of valid endpoints */
  450. REG_DIEP_CTL(epnum) &= ~DEP_PKTSIZE_MASK;
  451. REG_DIEP_CTL(epnum) &= ~DEP_TYPE_MASK;
  452. switch (dwc->speed)
  453. {
  454. case USB_SPEED_FULL:
  455. case USB_SPEED_LOW:
  456. REG_DIEP_CTL(epnum) |= DEP_FS_PKTSIZE;
  457. break;
  458. case USB_SPEED_HIGH:
  459. REG_DIEP_CTL(epnum) |= DEP_HS_PKTSIZE;
  460. break;
  461. }
  462. //tx fifo number
  463. REG_DIEP_CTL(epnum) |= (epnum << 22);
  464. //ep type
  465. switch (pep->type)
  466. {
  467. case DWC_OTG_EP_TYPE_CONTROL:
  468. REG_DIEP_CTL(epnum) |= USB_ACTIVE_EP | DEP_TYPE_CNTL;
  469. break;
  470. case DWC_OTG_EP_TYPE_ISOC:
  471. REG_DIEP_CTL(epnum) |= USB_ACTIVE_EP | DEP_TYPE_ISO;
  472. break;
  473. case DWC_OTG_EP_TYPE_BULK:
  474. REG_DIEP_CTL(epnum) |= USB_ACTIVE_EP | DEP_TYPE_BULK;
  475. break;
  476. case DWC_OTG_EP_TYPE_INTR:
  477. REG_DIEP_CTL(epnum) |= USB_ACTIVE_EP | DEP_TYPE_INTR;
  478. break;
  479. }
  480. /* DATA0 */
  481. REG_DIEP_CTL(epnum) |= (1 << 28);
  482. /* Enable EP INT */
  483. REG_DAINT_MASK |= (0x01 << (DWC_EP_IN_OFS + epnum));
  484. return 0;
  485. }
  486. int dwc_enable_out_ep(dwc_handle *dwc,uint8_t epnum)
  487. {
  488. uint32_t xfersize;
  489. uint32_t dma_addr, dma_len, pktcnt;
  490. dwc_ep *pep = RT_NULL;
  491. epnum &= DWC_EPNO_MASK;
  492. pep = dwc->dep[DWC_EP_OUT_OFS + epnum];
  493. /* Program the endpoint register to configure them with the characteristics of valid endpoints */
  494. REG_DOEP_CTL(epnum) &= ~DEP_PKTSIZE_MASK;
  495. REG_DOEP_CTL(epnum) &= ~DEP_TYPE_MASK;
  496. switch (dwc->speed)
  497. {
  498. case USB_SPEED_FULL:
  499. case USB_SPEED_LOW:
  500. REG_DOEP_CTL(epnum) |= DEP_FS_PKTSIZE;
  501. break;
  502. case USB_SPEED_HIGH:
  503. REG_DOEP_CTL(epnum) |= DEP_HS_PKTSIZE;
  504. break;
  505. }
  506. //ep type
  507. switch (pep->type)
  508. {
  509. case DWC_OTG_EP_TYPE_CONTROL:
  510. REG_DOEP_CTL(epnum) |= USB_ACTIVE_EP | DEP_TYPE_CNTL;
  511. break;
  512. case DWC_OTG_EP_TYPE_ISOC:
  513. REG_DOEP_CTL(epnum) |= USB_ACTIVE_EP | DEP_TYPE_ISO;
  514. break;
  515. case DWC_OTG_EP_TYPE_BULK:
  516. REG_DOEP_CTL(epnum) |= USB_ACTIVE_EP | DEP_TYPE_BULK;
  517. break;
  518. case DWC_OTG_EP_TYPE_INTR:
  519. REG_DOEP_CTL(epnum) |= USB_ACTIVE_EP | DEP_TYPE_INTR;
  520. break;
  521. }
  522. /* DATA0 */
  523. REG_DOEP_CTL(epnum) |= (1 << 28);
  524. /* Enable EP INT */
  525. REG_DAINT_MASK |= (0x01 << (DWC_EP_OUT_OFS + epnum));
  526. /* OUT-EP must init xfer buffer */
  527. xfersize = pep->maxpacket * 2;
  528. pktcnt = xfersize / DEP_EP_MAXPKT(epnum);
  529. pep->xfer_len = xfersize;
  530. pep->xfer_count = 0;
  531. /* xfer_buffer has been initialized by up-layer */
  532. // pep->xfer_buff = pep->xfer_buff;
  533. DWC_DBG("%s %d xfer_buff: %x %x\n", __FUNCTION__, __LINE__, pep->xfer_buff, PHYS(pep->xfer_buff));
  534. /* Program the DOEPSIZn register for the transfer size and corresponding packet count */
  535. REG_DOEP_SIZE(epnum) &= ~(0x1fffffff);
  536. REG_DOEP_SIZE(epnum) = (pktcnt << 19) | xfersize;
  537. if (dwc->is_dma == IS_INTERN_DMA)
  538. {
  539. dma_addr = (uint32_t) (pep->xfer_buff);
  540. dma_len = (((xfersize + 7) >> 3) << 3); //pep->xfer_len;
  541. rt_hw_dcache_flush_range(dma_addr, dma_len);
  542. /* Additionally, in DMA mode, program the DOEPDMAn register */
  543. REG_DOEP_DMA(epnum) = PHYS(pep->xfer_buff);
  544. }
  545. /* Program the DOEPCTLn Register with endpoint charateristics,
  546. * and set the Endpoint Enable and Clear NAK bit */
  547. REG_DOEP_CTL(epnum) |= DEP_ENA_BIT | DEP_CLEAR_NAK;
  548. return 0;
  549. }
  550. void dwc_set_address(dwc_handle *dwc,uint8_t address)
  551. {
  552. sleep_flag = 1;
  553. REG_OTG_DCFG &= ~DCFG_DEV_ADDR_MASK;
  554. REG_OTG_DCFG |= address << DCFG_DEV_ADDR_BIT;
  555. }
  556. void dwc_otg_ep0_out_start(dwc_handle *dwc)
  557. {
  558. dwc_ep *pep = dwc->dep[DWC_EP_OUT_OFS + 0];
  559. DWC_DBG("%s %d\n",__func__,__LINE__);
  560. pep->xfer_len = 64;
  561. pep->xfer_count = 0;
  562. pep->maxpacket = 64;
  563. // pep->ctrl_req_addr = (uint32_t)(&setup_packet[0]);
  564. pep->xfer_buff = pep->xfer_buff;
  565. if (dwc->is_dma == IS_INTERN_DMA)
  566. {
  567. REG_DOEP_SIZE(0) = DOEPSIZE0_SUPCNT_3 | DOEPSIZE0_PKTCNT_BIT | (pep->maxpacket);
  568. // REG_DOEP_DMA(0) = PHYS(pep->ctrl_req_addr);
  569. REG_DOEP_DMA(0) = PHYS(pep->xfer_buff);
  570. }
  571. else
  572. {
  573. REG_DOEP_SIZE(0) = DOEPSIZE0_SUPCNT_3 | DOEPSIZE0_PKTCNT_BIT | (pep->maxpacket);
  574. }
  575. }
  576. static void dwc_calculate_fifo_size(dwc_handle *dwc)
  577. {
  578. /*
  579. * TODO: we are use "Dedicated FIFO Mode with No Thresholding"
  580. * if need thresholding, the calculation algorithm may need change
  581. */
  582. /**
  583. * 3.2.1.1 FIFO SPRAM(Single-Port RAM) mapping:
  584. *
  585. * 1. One common RxFIFO, used in Host and Device modes
  586. * 2. One common Periodic TxFIFO, used in Host mode
  587. * 3. Separate IN endpoint transmit FIFO for each Device mode IN endpoints in Dedicated Transmit FIFO
  588. * operation (OTG_EN_DED_TX_FIFO = 1)
  589. * 4. The FIFO SPRAM is also used for storing some register values to save gates. In Scatter/Gather DMA
  590. * mode, four SPRAM locations (four 35-bit words) are reserved for this. In DMA and Slave modes
  591. * (non-Scatter/Gather mode), one SPRAM location (one 35-bit word) is used for storing the DMA epnum.
  592. *
  593. * NOTE: when the device is operating in Scatter/Gather mode, then the last
  594. * locations of the SPRAM store the Base Descriptor epnum, Current
  595. * Descriptor epnum, Current Buffer epnum and status quadlet
  596. * information for each endpoint direction (4 locations per Endpoint).
  597. * If an endpoint is bidirectional, then 4 locations will be used for IN,
  598. * and another 4 for OUT
  599. * 3.2.4.4 Endpoint Information Controller
  600. * The last locations in the SPRAM are used to hold register values.
  601. * Device Buffer DMA Mode:
  602. * one location per endpoint direction is used in SPRAM to store the
  603. * DIEPDMA and DOEPDMA value. The application writes data and then reads
  604. * it from the same location
  605. * For example, if there are ten bidirectional endpoints, then the last
  606. * 20 SPRAM locations are reserved for storing the DMA epnum for IN
  607. * and OUT endpoints
  608. * Scatter/Gather DMA Mode:
  609. * Four locations per endpoint direction are used in SPRAM to store the
  610. * Base Descriptor epnum, Current Descriptor epnum, Current Buffer
  611. * Pointer and the Status Quadlet.
  612. * The application writes data to the base descriptor epnum.
  613. * When the application reads the location where it wrote the base
  614. * descriptor epnum, it receives the current descriptor epnum.
  615. * For example, if there are ten bidirectional endpoints, then the last 80
  616. * locations are reserved for storing these values.
  617. *
  618. * Figure 3-13
  619. * ________________________
  620. * | |
  621. * | DI/OEPDMAn Register | Depends on the value of OTG_NUM_EPS
  622. * | and Descriptor Status | and OTG_EP_DIRn, see not above
  623. * | values |
  624. * -------------------------
  625. * | TxFIFO #n Packets | DIEPTXFn
  626. * -------------------------
  627. * | |
  628. * | ................ |
  629. * | |
  630. * -------------------------
  631. * | TxFIFO #1 Packets | DIEPTXF1
  632. * -------------------------
  633. * | TxFIFO #0 Packets |
  634. * |( up to3 SETUP Packets)| GNPTXFSIZ
  635. * ------------------------
  636. * | |
  637. * | Rx Packets | GRXFSIZ
  638. * | |
  639. * ------------------------- epnum = 0, Rx starting epnum fixed to 0
  640. *
  641. */
  642. /**
  643. * Rx FIFO Allocation (rx_fifo_size)
  644. *
  645. * RAM for SETUP Packets: 4 * n + 6 locations must be Reserved in the receive FIFO to receive up to
  646. * n SETUP packets on control endpoints, where n is the number of control endpoints the device
  647. * core supports.
  648. *
  649. * One location for Global OUT NAK
  650. *
  651. * Status information is written to the FIFO along with each received packet. Therefore, a minimum
  652. * space of (Largest Packet Size / 4) + 1 must be allotted to receive packets. If a high-bandwidth
  653. * endpoint is enabled, or multiple isochronous endpoints are enabled, then at least two (Largest
  654. * Packet Size / 4) + 1 spaces must be allotted to receive back-to-back packets. Typically, two
  655. * (Largest Packet Size / 4) + 1 spaces are recommended so that when the previous packet is being
  656. * transferred to AHB, the USB can receive the subsequent packet. If AHB latency is high, you must
  657. * allocate enough space to receive multiple packets. This is critical to prevent dropping of any
  658. * isochronous packets.
  659. *
  660. * Typically, one location for each OUT endpoint is recommended.
  661. *
  662. * one location for eatch endpoint for EPDisable is required
  663. */
  664. /**
  665. * Tx FIFO Allocation (tx_fifo_size[n])
  666. *
  667. * The minimum RAM space required for each IN Endpoint Transmit FIFO is the maximum packet size
  668. * for that particular IN endpoint.
  669. *
  670. * More space allocated in the transmit IN Endpoint FIFO results in a better performance on the USB
  671. *and can hide latencies on the AHB.
  672. */
  673. uint32_t rx_fifo_size, i;
  674. uint32_t np_txfifo_size = 0;
  675. uint32_t tx_fifo_size;
  676. uint16_t startaddr;
  677. uint16_t fifocfg;
  678. const int x = 1;
  679. /* Step1: Recevice FIFO Size Register (GRXFSIZ) */
  680. rx_fifo_size = (4 * 1 + 6) + (2) * (1024 / 4 + 1) + (2 * dwc->hwcfg2.b.num_dev_ep) + 1;
  681. REG_GRXFIFO_SIZE = rx_fifo_size;
  682. /* Step2: Program device in ep transmit fifo0 size register (GNPTXFSIZ) */
  683. np_txfifo_size |= ((1 + 1) * (64 / 4) << 16); //depth
  684. np_txfifo_size |= rx_fifo_size; //startaddr
  685. REG_GNPTXFIFO_SIZE = np_txfifo_size;
  686. #define DWC_TX_FIFO_SIZE ((1 + 1) * (512 / 4))
  687. startaddr = ((1 + 1) * (64 / 4) << 16) + rx_fifo_size;
  688. for (i=1; i<dwc->hwcfg4.b.num_in_eps; i++)
  689. {
  690. tx_fifo_size |= (DWC_TX_FIFO_SIZE << 16) | startaddr;
  691. REG_GDIEP_TXF(i) = tx_fifo_size;
  692. startaddr += DWC_TX_FIFO_SIZE;
  693. }
  694. /* Configure fifo start addr and depth for endpoint information controller */
  695. REG_GDFIFO_CFG |= startaddr << 16;
  696. fifocfg = REG_GHW_CFG3;
  697. fifocfg = (fifocfg >> 16);
  698. REG_GDFIFO_CFG |= fifocfg;
  699. /* flush tx and rx fifo */
  700. dwc_otg_flush_rx_fifo(dwc);
  701. dwc_otg_flush_tx_fifo(dwc,0x10);
  702. }
  703. static void dwc_handle_enum_done_intr(dwc_handle *dwc)
  704. {
  705. dwc_ep *pep = dwc->dep[0];
  706. /* Step1: Read the DSTS register to determine the enumeration speed */
  707. uint32_t dsts = REG_OTG_DSTS;
  708. uint32_t diep0ctl = REG_DIEP_CTL(0);
  709. diep0ctl &= ~(0x3);
  710. switch (dsts & DSTS_ENUM_SPEED_MASK)
  711. {
  712. case DSTS_ENUM_SPEED_HIGH:
  713. DWC_DBG("High speed.\n");
  714. dwc->speed = USB_SPEED_HIGH;
  715. pep->maxpacket = 64;
  716. diep0ctl |= DEP_EP0_MPS_64;
  717. REG_OTG_DCFG &= ~1;
  718. break;
  719. case DSTS_ENUM_SPEED_FULL_30OR60:
  720. case DSTS_ENUM_SPEED_FULL_48:
  721. DWC_DBG("Full speed.\n");
  722. dwc->speed = USB_SPEED_FULL;
  723. pep->maxpacket = 64;
  724. diep0ctl |= DEP_EP0_MPS_64;
  725. REG_OTG_DCFG |= 1;
  726. break;
  727. case DSTS_ENUM_SPEED_LOW:
  728. DWC_DBG("Low speed.\n");
  729. dwc->speed = USB_SPEED_LOW;
  730. pep->maxpacket = 8;
  731. diep0ctl |= DEP_EP0_MPS_8;
  732. break;
  733. default:
  734. DWC_DBG("Fault speed enumration\n");
  735. break;
  736. }
  737. REG_OTG_DCTL |= DCTL_CLR_GNPINNAK;
  738. /* Step2: Program the DIEPCTL0.MPS to set the maximum packet size */
  739. REG_DIEP_CTL(0) = diep0ctl;
  740. /* Step3: In Dma mode program the DOEPCTL0 register
  741. * to enable control ouctrl_req_addrt endpoint0 to receive setup
  742. * packet .*/
  743. // dwc_otg_ep0_out_start(dwc);
  744. if (dwc->is_dma == IS_INTERN_DMA)
  745. {
  746. rt_hw_dcache_flush_all();
  747. DWC_DBG("0 doepsize %x ctl %x\n", REG_DOEP_SIZE(0), REG_DOEP_CTL(0));
  748. REG_DOEP_CTL(0) |= DEP_ENA_BIT | DEP_CLEAR_NAK;
  749. }
  750. else
  751. {
  752. REG_DOEP_CTL(0) |= DEP_ENA_BIT | DEP_CLEAR_NAK;
  753. }
  754. /* Step4: unmask the SOF interrupt */
  755. REG_GINT_MASK |= GINTMSK_START_FRAM;
  756. REG_GINT_STS = GINTSTS_ENUM_DONE;
  757. // dump_global_dwcreg();
  758. return;
  759. }
  760. static void dwc_handle_early_suspend_intr(dwc_handle *dwc)
  761. {
  762. DWC_DBG("Handle early suspend intr.\n");
  763. REG_GINT_STS = GINTSTS_USB_EARLYSUSPEND;
  764. if (REG_OTG_DSTS & DSTS_ERRATIC_ERROR)
  765. {
  766. REG_OTG_DCTL |= DCTL_SOFT_DISCONN;
  767. mdelay(100);
  768. dwc_otg_core_reset(dwc);
  769. dwc_otg_core_init(dwc,1);
  770. dwc_otg_device_init(dwc);
  771. dwc_calculate_fifo_size(dwc);
  772. }
  773. }
  774. static void dwc_handle_suspend_intr(dwc_handle *dwc)
  775. {
  776. DWC_DBG("Handle suspend intr.\n");
  777. REG_GINT_STS = GINTSTS_USB_SUSPEND;
  778. DWC_DBG("==>%s,sleep_flag = %d\n",__func__,sleep_flag);
  779. #if 0
  780. if(sleep_flag)
  781. {
  782. while(!(REG_OTG_DSTS & 1))
  783. {
  784. printf("REG_OTG_DSTS is 0x%x\n",REG_OTG_DSTS);
  785. }
  786. sleep_flag = 0;
  787. enable_irq(IRQ_OTG);
  788. jz_pm_sleep();
  789. }
  790. #endif
  791. }
  792. static void dwc_handle_start_frame_intr(dwc_handle *dwc)
  793. {
  794. REG_GINT_STS = GINTSTS_START_FRAM;
  795. }
  796. static void dwc_handle_reset_intr(dwc_handle *dwc)
  797. {
  798. int i;
  799. /* Step1: NAK OUT ep */
  800. for (i=0; i<dwc->hwcfg2.b.num_dev_ep; i++)
  801. {
  802. REG_DOEP_CTL(i) |= DEP_SET_NAK;
  803. }
  804. /* Step2: unmask the following interrupt bits */
  805. REG_DAINT_MASK = 0;
  806. REG_DOEP_MASK = 0;
  807. REG_DIEP_MASK = 0;
  808. REG_DAINT_MASK |= (1 << 0) | (1 << 16); //inep0 outep0
  809. REG_DOEP_MASK |= DEP_XFER_COMP | DEP_SETUP_PHASE_DONE | DEP_AHB_ERR; // xfercompl setupdone
  810. REG_DIEP_MASK |= DEP_XFER_COMP | DEP_TIME_OUT | DEP_AHB_ERR; // xfercompl ahberr timeout
  811. dwc->dep[0]->ep_state = EP_SETUP;
  812. /* Step3: Device initalization */
  813. dwc_otg_device_init(dwc);
  814. /* Step4: Set up the data fifo ram for each of the fifo */
  815. //dwc_calculate_fifo_size();
  816. /* Step5: Reset Device Address */
  817. REG_OTG_DCFG &= (~DCFG_DEV_ADDR_MASK);
  818. /* Step6: setup EP0 to receive SETUP packets */
  819. dwc_otg_ep0_out_start(dwc);
  820. if (dwc->is_dma == IS_INTERN_DMA)
  821. {
  822. rt_hw_dcache_flush_all();
  823. REG_DOEP_CTL(0) |= DEP_ENA_BIT | DEP_CLEAR_NAK;
  824. }
  825. else
  826. {
  827. REG_DOEP_CTL(0) |= DEP_ENA_BIT | DEP_CLEAR_NAK;
  828. }
  829. dwc_disable_in_ep(dwc,0);
  830. REG_GINT_STS = GINTSTS_USB_RESET;
  831. return;
  832. }
  833. void dwc_handle_rxfifo_nempty(dwc_handle *dwc)
  834. {
  835. dwc_ep *pep;
  836. uint32_t *setup_buf;
  837. uint32_t count;
  838. uint32_t rxsts_pop = REG_GRXSTS_POP;
  839. uint8_t epnum = (rxsts_pop & 0xf);
  840. switch (rxsts_pop & GRXSTSP_PKSTS_MASK)
  841. {
  842. case GRXSTSP_PKSTS_GOUT_NAK:
  843. DWC_DBG("GRXSTSP_PKSTS_GOUT_NAK.\n");
  844. break;
  845. case GRXSTSP_PKSTS_GOUT_RECV:
  846. DWC_DBG("GRXSTSP_PKSTS_GOUT_RECV. - ");
  847. count = (rxsts_pop & GRXSTSP_BYTE_CNT_MASK) >> GRXSTSP_BYTE_CNT_BIT;
  848. if (count)
  849. {
  850. DWC_DBG("count:%d\n", count);
  851. dwc_read_ep_packet(dwc,epnum, count);
  852. }
  853. break;
  854. case GRXSTSP_PKSTS_TX_COMP:
  855. DWC_DBG("GRXSTSP_PKSTS_TX_COMP.\n");
  856. break;
  857. case GRXSTSP_PKSTS_SETUP_COMP:
  858. DWC_DBG("GRXSTSP_PKSTS_SETUP_COMP.\n");
  859. break;
  860. case GRXSTSP_PKSTS_SETUP_RECV:
  861. DWC_DBG("GRXSTSP_PKSTS_SETUP_RECV. - ");
  862. // setup_packet[0] = REG_EP_FIFO(0);
  863. // setup_packet[1] = REG_EP_FIFO(0);
  864. // DWC_DBG("%x %x\n", setup_packet[0], setup_packet[1]);
  865. ((uint8_t *)dwc->dep[0]->xfer_buff)[0] = REG_EP_FIFO(0);
  866. ((uint8_t *)dwc->dep[0]->xfer_buff)[1] = REG_EP_FIFO(1);
  867. DWC_DBG("%x %x\n", ((uint8_t *)dwc->dep[0]->xfer_buff)[0], ((uint8_t *)dwc->dep[0]->xfer_buff)[1]);
  868. break;
  869. default:
  870. break;
  871. }
  872. REG_GINT_STS = GINTSTS_RXFIFO_NEMPTY;
  873. }
  874. void dwc_ep0_in_intr(dwc_handle *dwc, uint8_t epnum)
  875. {
  876. uint32_t updated_size;
  877. uint32_t dma_addr, dma_len;
  878. uint8_t *ptr;
  879. uint32_t intr = REG_DIEP_INT(epnum & 0x0F);
  880. dwc_ep *pep ;
  881. DWC_DBG("ep0 in intr:%x\n", intr);
  882. epnum &= DWC_EPNO_MASK;
  883. pep = dwc->dep[DWC_EP_IN_OFS + epnum];
  884. udelay(1);
  885. /* When the transfer size if 0 and the packet count is 0,
  886. * the transfer complete interrupt for the endpoint is generated
  887. * and the endpoint enable is cleared */
  888. if (intr & DEP_XFER_COMP)
  889. {
  890. DWC_DBG("XFER_COMP\n");
  891. REG_DIEP_INT(epnum) = DEP_XFER_COMP; // clear int
  892. if (dwc->is_dma == IS_SLAVE_MODE)
  893. REG_DIEP_EMPMSK &= ~(1 << epnum);
  894. updated_size = (REG_DIEP_SIZE(epnum) & 0x7f);
  895. pep->xfer_count = pep->xfer_len - updated_size; // number of bytes transfered
  896. DWC_DBG("in xfer_count:%d xfer_len:%d updated_size:%d\n", pep->xfer_count, pep->xfer_len, updated_size);
  897. if (pep->xfer_count != pep->xfer_len)
  898. {
  899. DWC_DBG("in xfer_count:%d xfer_len:%d updated_size:%d\n", pep->xfer_count, pep->xfer_len, updated_size);
  900. pep->xfer_len -= pep->xfer_count;
  901. ptr = (uint8_t *)pep->xfer_buff + pep->xfer_count;
  902. HW_SendPKT(dwc, 0, ptr, pep->xfer_len);
  903. return;
  904. }
  905. DWC_DBG("pep->ep_state = %s\n",ep0_state_string[pep->ep_state]);
  906. switch(pep->ep_state)
  907. {
  908. case EP_DATA:
  909. #if 1
  910. /* 3 Stage */
  911. if(pep->xfer_len == pep->maxpacket)
  912. {
  913. x1000_usbd_event_cb(0, USB_EVT_IN, RT_NULL);
  914. }
  915. else
  916. #endif
  917. {
  918. pep->ep_state = EP_STATUS;
  919. dwc_handle_ep_data_out_phase(dwc,0);
  920. }
  921. break;
  922. case EP_STATUS:
  923. pep->ep_state = EP_SETUP;
  924. dwc_handle_ep_data_out_phase(dwc,0);
  925. break;
  926. }
  927. }
  928. if (dwc->is_dma == IS_SLAVE_MODE)
  929. {
  930. if ((intr & DEP_TXFIFO_EMPTY) && (REG_DIEP_EMPMSK & (1 << epnum)))
  931. {
  932. if (pep->xfer_len)
  933. {
  934. dwc_write_ep_packet(dwc,epnum);
  935. }
  936. REG_DIEP_INT(epnum) = DEP_TXFIFO_EMPTY;
  937. }
  938. }
  939. if (intr & DEP_AHB_ERR)
  940. {
  941. DWC_DBG("1 AHB ERR\n");
  942. REG_DIEP_INT(epnum) = DEP_AHB_ERR;
  943. }
  944. if (intr & DEP_TIME_OUT)
  945. {
  946. DWC_DBG("IN TIME_OUT.\n");
  947. REG_DIEP_INT(epnum) = DEP_TIME_OUT;
  948. }
  949. }
  950. void dwc_epn_in_intr(dwc_handle *dwc, uint8_t epnum)
  951. {
  952. uint32_t intr = REG_DIEP_INT(epnum & 0x0F);
  953. uint32_t updated_size;
  954. /* When the transfer size if 0 and the packet count is 0,
  955. * the transfer complete interrupt for the endpoint is generated
  956. * and the endpoint enable is cleared */
  957. dwc_ep *pep;
  958. epnum &= DWC_EPNO_MASK;
  959. pep = dwc->dep[DWC_EP_IN_OFS + epnum];
  960. if (intr & DEP_XFER_COMP)
  961. {
  962. DWC_DBG("1 IN XFER_COMP. %x\n", REG_DIEP_SIZE(epnum));
  963. REG_DIEP_INT(epnum) = DEP_XFER_COMP;
  964. REG_DIEP_CTL(epnum) |= DEP_SET_NAK;
  965. if (pep->ep_state == EP_TRANSFERING)
  966. {
  967. if (dwc->is_dma == IS_SLAVE_MODE)
  968. REG_DIEP_EMPMSK &= ~(1 << epnum);
  969. updated_size = (REG_DIEP_SIZE(epnum) & 0x7ffff);
  970. pep->xfer_count = pep->xfer_len - updated_size;
  971. pep->ep_state = EP_TRANSFERED;
  972. // rt_kprintf("updated_size = %d,xfer_len = %d,xfer_count = %d\n",updated_size,pep->xfer_len, pep->xfer_count);
  973. // BusNotify(arg, UDC_PROTAL_SEND_FINISH, NULL, 0);
  974. x1000_usbd_event_cb(epnum,USB_EVT_IN,0);
  975. }
  976. }
  977. if (dwc->is_dma == IS_SLAVE_MODE)
  978. {
  979. if ((intr & DEP_TXFIFO_EMPTY) && (REG_DIEP_EMPMSK & (1 << epnum)))
  980. {
  981. REG_DIEP_EMPMSK &= ~(1 << epnum);
  982. // DWC_DBG("TX FIFO EMPTY intr.\n");
  983. if (pep->xfer_len)
  984. {
  985. dwc_write_ep_packet(dwc,epnum);
  986. }
  987. REG_DIEP_INT(epnum) = DEP_TXFIFO_EMPTY;
  988. }
  989. }
  990. if (intr & DEP_AHB_ERR)
  991. {
  992. DWC_DBG("1 AHB ERR\n");
  993. REG_DIEP_INT(epnum) = DEP_AHB_ERR;
  994. }
  995. if (intr & DEP_TIME_OUT)
  996. {
  997. DWC_DBG("IN TIME_OUT.\n");
  998. REG_DIEP_INT(epnum) = DEP_TIME_OUT;
  999. }
  1000. }
  1001. /*
  1002. * ep0 control transfer:
  1003. * 3 Stage:
  1004. * SetupPhase-------->IN DataPhase ---------> OUT StatusPhase
  1005. * Or 2 Stage:
  1006. * SetupPhase-------->IN StatusPhase
  1007. * */
  1008. typedef struct {
  1009. u8 bmRequestType;
  1010. u8 bRequest;
  1011. u16 wValue;
  1012. u16 wIndex;
  1013. u16 wLength;
  1014. } __attribute__ ((packed)) dwc_DeviceRequest;
  1015. int dwc_ep0_out_intr(dwc_handle *dwc, uint8_t epnum)
  1016. {
  1017. uint32_t intr, doep0size, dma_addr, dma_len;
  1018. uint8_t rem_supcnt, xfersize;
  1019. dwc_ep *pep = RT_NULL;
  1020. epnum &= DWC_EPNO_MASK;
  1021. pep = dwc->dep[DWC_EP_OUT_OFS + epnum];
  1022. intr = REG_DOEP_INT(epnum);
  1023. // printf("ep0 out intr:%x\n", intr);
  1024. udelay(1);
  1025. /* comp intrerrupt indeicates completion of the status out phase */
  1026. if (intr & DEP_XFER_COMP)
  1027. {
  1028. REG_DOEP_INT(epnum) = DEP_XFER_COMP;
  1029. DWC_DBG("pep->ep_state = %s\n",ep0_state_string[pep->ep_state]);
  1030. if (pep->ep_state == EP_STATUS)
  1031. {
  1032. pep->ep_state = EP_SETUP;
  1033. dwc_handle_ep_data_out_phase(dwc,0);
  1034. }
  1035. else if (pep->ep_state == EP_DATA)
  1036. {
  1037. DWC_DBG("*** EP0 DATA ***\n");
  1038. pep->xfer_count = pep->maxpacket - (REG_DOEP_SIZE(epnum) & 0x7ffff);
  1039. DWC_DBG("pep->xfer_count = %d\n",pep->xfer_count);
  1040. x1000_usbd_event_cb(0,USB_EVT_OUT,0);
  1041. }
  1042. else if (!(intr & (DEP_SETUP_PHASE_DONE | (1 << 15))))
  1043. {
  1044. DWC_DBG("error\n");
  1045. pep->ep_state = EP_SETUP;
  1046. dwc_handle_ep_data_out_phase(dwc, epnum);
  1047. }
  1048. else if (pep->ep_state != EP_SETUP)
  1049. {
  1050. DWC_DBG("ep0 state mismatch\n");
  1051. }
  1052. }
  1053. //IN Token
  1054. if (intr & DEP_INTOKEN_EPMISATCH)
  1055. {
  1056. if (pep->ep_state == EP_DATA)
  1057. {
  1058. pep->ep_state = EP_STATUS;
  1059. dwc_handle_ep_status_in_phase(dwc, epnum);
  1060. }
  1061. REG_DOEP_INT(epnum) = DEP_INTOKEN_EPMISATCH;
  1062. }
  1063. if (intr & DEP_AHB_ERR)
  1064. {
  1065. DWC_DBG("AHB ERR\n");
  1066. REG_DOEP_INT(0) = DEP_AHB_ERR;
  1067. }
  1068. if (intr & DEP_NAK_INT)
  1069. {
  1070. REG_DOEP_INT(0) = DEP_NAK_INT;
  1071. }
  1072. if (intr & (DEP_SETUP_PHASE_DONE | (1 << 15)))
  1073. {
  1074. DWC_DBG("SETUP_PHASE_DONE.\n");
  1075. /* read the DOEPTSIZn to determine the number of setup packets
  1076. * recevied and process the last recevied setup packet */
  1077. REG_DOEP_INT(epnum) = DEP_SETUP_PHASE_DONE | (1 << 15);
  1078. doep0size = REG_DOEP_SIZE(epnum);
  1079. xfersize = doep0size & 0x7ffff;
  1080. rem_supcnt = (doep0size & (0x3 << 29)) >> 29;
  1081. DWC_DBG("xfersize = %d,rem_supcnt = %d\n",xfersize,rem_supcnt);
  1082. if (intr & DEP_B2B_SETUP_RECV)
  1083. {
  1084. DWC_DBG("back to back setup recevie\n");
  1085. }
  1086. else
  1087. {
  1088. /* Read out the last packet from the rxfifo */
  1089. // rt_hw_dcache_invalidate_range((uint32_t)(pep->ctrl_req_addr), sizeof(dwc_DeviceRequest));
  1090. rt_hw_dcache_invalidate_range((uint32_t)(pep->xfer_buff), sizeof(dwc_DeviceRequest));
  1091. #if 0
  1092. {
  1093. dwc_DeviceRequest* device_req = (dwc_DeviceRequest *)(pep->ctrl_req_addr);
  1094. rt_kprintf("\n-------------\n");
  1095. rt_kprintf("bRequest: %x\n", device_req->bRequest);
  1096. rt_kprintf("bRequestType: %x\n", device_req->bmRequestType);
  1097. rt_kprintf("wIndex: %x\n", device_req->wIndex);
  1098. rt_kprintf("wLength: %x\n", device_req->wLength);
  1099. rt_kprintf("wValue: %x\n", device_req->wValue);
  1100. rt_kprintf("-------------\n");
  1101. }
  1102. #endif
  1103. /* At the end of the Setup stage, the appliaction must reporgram the
  1104. * DOEPTSIZn.SUPCnt field to 3 receive the next SETUP packet */
  1105. if (pep->ep_state == EP_SETUP)
  1106. {
  1107. if (dwc->is_dma == 2)
  1108. {
  1109. //printf("1 doepsize %x ctl %x\n", REG_DOEP_SIZE(0), REG_DOEP_CTL(0));
  1110. REG_DOEP_SIZE(epnum) = DOEPSIZE0_SUPCNT_3 | DOEPSIZE0_PKTCNT_BIT | (pep->maxpacket);
  1111. // REG_DOEP_DMA(epnum) = PHYS(pep->ctrl_req_addr);
  1112. REG_DOEP_DMA(epnum) = PHYS(pep->xfer_buff);
  1113. }
  1114. else
  1115. {
  1116. REG_DOEP_SIZE(epnum) = DOEPSIZE0_SUPCNT_3 | DOEPSIZE0_PKTCNT_BIT | (pep->maxpacket);
  1117. }
  1118. }
  1119. /* Setup Finish */
  1120. pep->xfer_count = sizeof(dwc_DeviceRequest);
  1121. // pep->xfer_buff = (void *) (pep->ctrl_req_addr);
  1122. // x1000_usbd_event_cb(0, USB_EVT_SETUP, (void *) (pep->ctrl_req_addr));
  1123. x1000_usbd_event_cb(0, USB_EVT_SETUP, pep->xfer_buff);
  1124. REG_DOEP_CTL(epnum) |= DEP_DISENA_BIT;
  1125. // REG_DOEP_CTL(epnum) |= DEP_SET_NAK;
  1126. }
  1127. }
  1128. return 0;
  1129. }
  1130. int dwc_epn_out_intr(dwc_handle *dwc, uint8_t epnum)
  1131. {
  1132. uint32_t intr, updated_size;
  1133. dwc_ep *pep ;
  1134. epnum &= DWC_EPNO_MASK;
  1135. pep = dwc->dep[DWC_EP_OUT_OFS + epnum];
  1136. udelay(1);
  1137. DWC_DBG("ep%d out_intr\n",epnum);
  1138. intr = REG_DOEP_INT(epnum);
  1139. if (intr & DEP_XFER_COMP)
  1140. {
  1141. REG_DOEP_INT(epnum) = DEP_XFER_COMP;
  1142. updated_size = REG_DOEP_SIZE(epnum) & 0x7ffff;
  1143. pep->xfer_count = pep->xfer_len - updated_size;
  1144. DWC_DBG("xfer_count = %d\n",pep->xfer_count);
  1145. // BusNotify((uint32_t)arg,UDC_PROTAL_RECEIVE_FINISH, (uint8_t *)pep->xfer_buff, pep->xfer_count);
  1146. x1000_usbd_event_cb(epnum,USB_EVT_OUT,0);
  1147. #if 0
  1148. pep->xfer_len = pep->maxpacket; /* number of bytes to transfer */
  1149. pep->xfer_count = 0; /* number of bytes transfered */
  1150. // pep->xfer_buff = pep->xfer_buff; /* pointer to transfer buffer */
  1151. dwc_handle_ep_data_out_phase(dwc, epnum);
  1152. DWC_DBG("REG_DOEP_SIZE: %x \n", REG_DOEP_SIZE(epnum));
  1153. #endif
  1154. }
  1155. if (intr & DEP_AHB_ERR)
  1156. {
  1157. DWC_DBG("1 AHB ERR\n");
  1158. REG_DOEP_INT(epnum) = DEP_AHB_ERR;
  1159. }
  1160. return 0;
  1161. }
  1162. static void dwc_handle_inep_intr(dwc_handle *dwc)
  1163. {
  1164. uint32_t ep_intr;
  1165. uint8_t epnum = 0;
  1166. ep_intr = (REG_OTG_DAINT & 0xffff);
  1167. DWC_DBG("\n\nEp IN %x - \n", ep_intr);
  1168. while (ep_intr)
  1169. {
  1170. if (ep_intr & 0x01)
  1171. {
  1172. if (epnum == 0)
  1173. {
  1174. dwc_ep0_in_intr(dwc, epnum);
  1175. }
  1176. else
  1177. {
  1178. dwc_epn_in_intr(dwc, epnum);
  1179. }
  1180. }
  1181. epnum++;
  1182. ep_intr >>= 1;
  1183. }
  1184. REG_GINT_STS = GINTSTS_IEP_INTR;
  1185. return ;
  1186. }
  1187. static void dwc_handle_outep_intr(dwc_handle *dwc)
  1188. {
  1189. uint32_t ep_intr, epnum = 0;
  1190. ep_intr = (REG_OTG_DAINT & 0xffff0000) >> 16;
  1191. DWC_DBG("\n\nEp OUT %x - \n", ep_intr);
  1192. while (ep_intr)
  1193. {
  1194. if (ep_intr & 0x01)
  1195. {
  1196. if (epnum == 0)
  1197. {
  1198. dwc_ep0_out_intr(dwc, 0);
  1199. }
  1200. else
  1201. {
  1202. dwc_epn_out_intr(dwc, epnum);
  1203. }
  1204. }
  1205. epnum ++;
  1206. ep_intr >>= 1;
  1207. }
  1208. REG_GINT_STS = GINTSTS_OEP_INTR;
  1209. }
  1210. static void dwc_otg_intr(dwc_handle *dwc)
  1211. {
  1212. REG_GINT_STS = GINTSTS_OTG_INTR;
  1213. }
  1214. void dwc_common_intr(dwc_handle *dwc,uint32_t intsts)
  1215. {
  1216. if (intsts & GINTSTS_USB_EARLYSUSPEND)
  1217. {
  1218. dwc_handle_early_suspend_intr(dwc);
  1219. }
  1220. if (intsts & GINTSTS_USB_SUSPEND)
  1221. {
  1222. dwc_handle_suspend_intr(dwc);
  1223. }
  1224. if (intsts & GINTSTS_USB_RESET)
  1225. {
  1226. dwc_handle_reset_intr(dwc);
  1227. }
  1228. if (intsts & GINTSTS_ENUM_DONE)
  1229. {
  1230. dwc_handle_enum_done_intr(dwc);
  1231. }
  1232. if (intsts & GINTSTS_START_FRAM)
  1233. {
  1234. dwc_handle_start_frame_intr(dwc);
  1235. }
  1236. }
  1237. void dwc_handle_resume_intr(dwc_handle *dwc)
  1238. {
  1239. DWC_DBG("Handle resume intr.\n");
  1240. REG_GINT_STS = GINTSTS_RSUME_DETE;
  1241. // dwc_otg_phy_suspend(0);
  1242. }
  1243. static void dwc_irq_handler(int vector,void *arg)
  1244. {
  1245. dwc_handle *dwc = (dwc_handle *)arg;
  1246. RT_ASSERT(dwc != RT_NULL);
  1247. rt_hw_interrupt_mask(IRQ_OTG);
  1248. rt_sem_release(dwc->isr_sem);
  1249. }
  1250. static void dwc_otg_core_reset(dwc_handle *dwc)
  1251. {
  1252. uint32_t greset = 0;
  1253. uint32_t cnt = 0;
  1254. REG_GRST_CTL |= RSTCTL_CORE_RST;
  1255. do
  1256. {
  1257. greset = REG_GRST_CTL;
  1258. if (cnt++ > 100000)
  1259. {
  1260. DWC_DBG("GRESET wait reset timeout.\n");
  1261. return;
  1262. }
  1263. udelay(10);
  1264. } while (greset & RSTCTL_CORE_RST);
  1265. cnt = 0;
  1266. do
  1267. {
  1268. udelay(10);
  1269. greset = REG_GRST_CTL;
  1270. if (cnt++ > 100000)
  1271. {
  1272. DWC_DBG("GRESET wait IDLE timeout.\n");
  1273. return;
  1274. }
  1275. } while ((greset & RSTCTL_AHB_IDLE) == 0);
  1276. /* wait for 3 phy clocks */
  1277. udelay(100);
  1278. }
  1279. static int dwc_otg_phy_is_suspend(void)
  1280. {
  1281. return (!(cpm_test_bit(7, CPM_OPCR)));
  1282. }
  1283. static void dwc_otg_phy_suspend(int suspend)
  1284. {
  1285. if (!suspend && dwc_otg_phy_is_suspend())
  1286. {
  1287. DWC_DBG("EN PHY\n");
  1288. cpm_set_bit(7, CPM_OPCR);
  1289. udelay(45);
  1290. }
  1291. else if (suspend && !dwc_otg_phy_is_suspend())
  1292. {
  1293. DWC_DBG("DIS PHY\n");
  1294. cpm_clear_bit(7, CPM_OPCR);
  1295. udelay(5);
  1296. }
  1297. }
  1298. static void dwc_otg_device_init(dwc_handle *dwc)
  1299. {
  1300. uint32_t dcfg = 0;
  1301. uint32_t pcgcctl;
  1302. uint32_t rx_fifo_size;
  1303. /* Restart the phy clock */
  1304. if (REG_PCGC_CTL & 0x1)
  1305. {
  1306. DWC_DBG("<<<<<< pcgcctl %x >>>>>\n", REG_PCGC_CTL);
  1307. REG_PCGC_CTL &= ~(0x1 | (1 << 2) | (1 << 3));
  1308. }
  1309. /* In dma mode GINTMSK_NPTXFIFO_EMPTY , GINTMSK_RXFIFO_NEMPTY must be masked*/
  1310. if (dwc->is_dma == IS_INTERN_DMA)
  1311. {
  1312. if (REG_GINT_MASK & (GINTMSK_NPTXFIFO_EMPTY | GINTMSK_RXFIFO_NEMPTY))
  1313. {
  1314. REG_GINT_MASK &= ~(GINTMSK_NPTXFIFO_EMPTY | GINTMSK_RXFIFO_NEMPTY);
  1315. }
  1316. }
  1317. else
  1318. {
  1319. REG_GINT_MASK |= (GINTMSK_NPTXFIFO_EMPTY | GINTMSK_RXFIFO_NEMPTY);
  1320. }
  1321. /* Program the DCFG register */
  1322. if (dwc->hwcfg4.b.desc_dma)
  1323. {
  1324. dcfg |= DCFG_DEV_DESC_DMA;
  1325. }
  1326. #if DWC_FORCE_SPEED_FULL
  1327. REG_OTG_DCFG |= 1; //dma buffer mode full speed
  1328. #else
  1329. REG_OTG_DCFG &= ~3; //dma buffer mode HIGH speed
  1330. #endif
  1331. /* Clear the DCTL.SftDiscon bit the core issues aconnect after ths bit is cleared */
  1332. REG_OTG_DCTL &= ~DCTL_SOFT_DISCONN;
  1333. REG_GINT_STS = 0xffffffff;
  1334. /* Program the GINTMSK */
  1335. REG_GINT_MASK |= GINTMSK_IEP_INTR | GINTMSK_OEP_INTR |GINTMSK_USB_RESET | GINTMSK_ENUM_DONE |GINTMSK_USB_EARLYSUSPEND | GINTMSK_USB_SUSPEND | (1 << 31);
  1336. }
  1337. static void dwc_otg_core_init(dwc_handle *dwc,uint8_t dma_enable)
  1338. {
  1339. uint32_t ahbcfg = 0, gusbcfg = 0, curmod = 0, tmp;
  1340. uint8_t arch;
  1341. DWC_DBG("Core Init...\n");
  1342. /* Step1: Read the GHWCFG1,2,3,4 to find the configuration parameters selected for DWC_otg core */
  1343. dwc->hwcfg1.d32 = REG_GHW_CFG1;
  1344. dwc->hwcfg2.d32 = REG_GHW_CFG2;
  1345. dwc->hwcfg3.d32 = REG_GHW_CFG3;
  1346. dwc->hwcfg4.d32 = REG_GHW_CFG4;
  1347. DWC_DBG("cfg1:%x 2:%x 3:%x 4:%x\n", dwc->hwcfg1, dwc->hwcfg2, dwc->hwcfg3, dwc->hwcfg4);
  1348. DWC_DBG("cfg2->arch %x\n", dwc->hwcfg2.b.architecture);
  1349. arch = dwc->hwcfg2.b.architecture;
  1350. switch (arch)
  1351. {
  1352. case IS_SLAVE_MODE:
  1353. dwc->is_dma = IS_SLAVE_MODE;
  1354. break;
  1355. case IS_EXTERN_DMA:
  1356. dwc->is_dma = IS_EXTERN_DMA;
  1357. break;
  1358. case IS_INTERN_DMA:
  1359. dwc->is_dma = IS_INTERN_DMA;
  1360. break;
  1361. }
  1362. /* Step2: Program the GAHBCFG register */
  1363. /* DMA Mode bit and Burst Length */
  1364. if (dwc->is_dma == IS_EXTERN_DMA)
  1365. {
  1366. DWC_DBG("DWC IS_EXTERN_DMA\n");
  1367. ahbcfg |= AHBCFG_DMA_ENA;
  1368. }
  1369. else if (dwc->is_dma == IS_INTERN_DMA)
  1370. {
  1371. if (dma_enable)
  1372. {
  1373. DWC_DBG("DWC IS_INTERN_DMA\n");
  1374. ahbcfg |= AHBCFG_DMA_ENA | (DWC_GAHBCFG_INT_DMA_BURST_INCR16 << 1);
  1375. }
  1376. else
  1377. {
  1378. ahbcfg |= AHBCFG_TXFE_LVL;
  1379. dwc->is_dma = 0;
  1380. }
  1381. }
  1382. else
  1383. {
  1384. DWC_DBG("DWC IS_SLAVE_MODE\n");
  1385. }
  1386. /* Step3: Program the GINTMSK register */
  1387. REG_GINT_MASK = 0;
  1388. /* Step4: Program the GUSBCFG register */
  1389. gusbcfg = REG_GUSB_CFG;
  1390. gusbcfg &= ~((1 << 4) | (1 << 6) | (1 << 8) | (1 << 9));
  1391. REG_GUSB_CFG = gusbcfg; // HNP SRP not support and select UTMI+
  1392. dwc_otg_select_phy_width(dwc);
  1393. dwc_otg_core_reset(dwc);
  1394. /* Global Interrupt Mask bit = 1 */
  1395. ahbcfg |= AHBCFG_GLOBLE_INTRMASK;
  1396. REG_GAHB_CFG = ahbcfg;
  1397. /* Step5: The software must unmask OTG Interrupt Mask bit ,
  1398. * MOde mismatch interrupt Mask bit in the GINTMSK */
  1399. REG_GINT_MASK |= (GINTMSK_MODE_MISMATCH | GINTMSK_OTG_INTR);
  1400. }
  1401. int dwc_set_config(dwc_handle *dwc)
  1402. {
  1403. return 0;
  1404. }
  1405. int dwc_set_ep_stall(dwc_handle *dwc,uint8_t epnum)
  1406. {
  1407. depctl_data_t depctl;
  1408. daint_data_t daintmsk;
  1409. dwc_ep *pep = RT_NULL;
  1410. if(epnum & USB_DIR_IN)
  1411. {
  1412. pep = dwc->dep[epnum & 0x0F + DWC_EP_IN_OFS];
  1413. }
  1414. else
  1415. {
  1416. pep = dwc->dep[epnum & 0x0F + DWC_EP_OUT_OFS];
  1417. }
  1418. epnum &= DWC_EPNO_MASK;
  1419. if (pep->is_in)
  1420. {
  1421. depctl.d32 = REG_DIEP_CTL(epnum);
  1422. depctl.b.stall = 1;
  1423. REG_DIEP_CTL(epnum) = depctl.d32;
  1424. }
  1425. else
  1426. {
  1427. depctl.d32 = REG_DOEP_CTL(epnum);
  1428. depctl.b.stall = 1;
  1429. REG_DOEP_CTL(epnum) = depctl.d32;
  1430. }
  1431. return 0;
  1432. }
  1433. int dwc_clr_ep_stall(dwc_handle *dwc,uint8_t epnum)
  1434. {
  1435. depctl_data_t depctl;
  1436. daint_data_t daintmsk;
  1437. dwc_ep *pep = RT_NULL;
  1438. if(epnum & USB_DIR_IN)
  1439. {
  1440. pep = dwc->dep[epnum & 0x0F + DWC_EP_IN_OFS];
  1441. }
  1442. else
  1443. {
  1444. pep = dwc->dep[epnum & 0x0F + DWC_EP_OUT_OFS];
  1445. }
  1446. epnum &= DWC_EPNO_MASK;
  1447. if (pep->is_in)
  1448. {
  1449. depctl.d32 = REG_DIEP_CTL(epnum);
  1450. depctl.b.stall = 0;
  1451. REG_DIEP_CTL(epnum) = depctl.d32;
  1452. }
  1453. else
  1454. {
  1455. depctl.d32 = REG_DOEP_CTL(epnum);
  1456. depctl.b.stall = 0;
  1457. REG_DOEP_CTL(epnum) = depctl.d32;
  1458. }
  1459. return 0;
  1460. }
  1461. int dwc_ep_disable(dwc_handle *dwc,uint8_t epnum)
  1462. {
  1463. depctl_data_t depctl;
  1464. daint_data_t daintmsk;
  1465. dwc_ep *pep = RT_NULL;
  1466. DWC_DBG("%s epnum = %02x \n",epnum);
  1467. if(epnum & USB_DIR_IN)
  1468. {
  1469. pep = dwc->dep[epnum & 0x0F + DWC_EP_IN_OFS];
  1470. }
  1471. else
  1472. {
  1473. pep = dwc->dep[epnum & 0x0F + DWC_EP_OUT_OFS];
  1474. }
  1475. epnum &= DWC_EPNO_MASK;
  1476. /* EP0 can not deactivate! */
  1477. if (epnum == 0)
  1478. return -1;
  1479. daintmsk.d32 = REG_DAINT_MASK;
  1480. if (pep->is_in)
  1481. {
  1482. depctl.d32 = REG_DIEP_CTL(epnum);
  1483. daintmsk.ep.in &= ~(1 << epnum);
  1484. }
  1485. else
  1486. {
  1487. depctl.d32 = REG_DOEP_CTL(epnum);
  1488. daintmsk.ep.out &= ~(1 << epnum);
  1489. }
  1490. if (!depctl.b.usbactep)
  1491. {
  1492. DWC_DBG("EP %d already deactivated\n", pep->num);
  1493. return 0;
  1494. }
  1495. depctl.b.usbactep = 0;
  1496. if (pep->is_in)
  1497. {
  1498. REG_DIEP_CTL(epnum) = depctl.d32;
  1499. }
  1500. else
  1501. {
  1502. REG_DOEP_CTL(epnum) = depctl.d32;
  1503. }
  1504. /* mask EP interrupts */
  1505. REG_DAINT_MASK = daintmsk.d32;
  1506. if(pep->is_in)
  1507. {/* Disable IN-EP */
  1508. }
  1509. else
  1510. {
  1511. /* Disable IN-EP */
  1512. }
  1513. DWC_DBG("EP %d deactivated\n", pep->num);
  1514. return 0;
  1515. }
  1516. static void x1000_usb_phy_switch(dwc_handle *dwc,uint8_t is_on)
  1517. {
  1518. uint32_t value;
  1519. if (is_on)
  1520. {
  1521. value = REG_CPM_OPCR;
  1522. REG_CPM_OPCR |= OPCR_OTGPHY0_ENABLE;
  1523. mdelay(1);
  1524. }
  1525. else
  1526. {
  1527. value = REG_CPM_OPCR;
  1528. REG_CPM_OPCR &= ~OPCR_OTGPHY0_ENABLE;
  1529. mdelay(1);
  1530. }
  1531. }
  1532. static void x1000_usb_set_device_only_mode(dwc_handle *dwc)
  1533. {
  1534. REG_CPM_USBPCR &= ~USBPCR_USB_MODE;
  1535. REG_CPM_USBPCR &= ~USBPCR_OTG_DISABLE;
  1536. }
  1537. static void x1000_usb_phy_init(dwc_handle *dwc)
  1538. {
  1539. REG_CPM_USBPCR |= USBPCR_POR;
  1540. mdelay(1);
  1541. REG_CPM_USBPCR &= ~USBPCR_POR;
  1542. mdelay(1);
  1543. }
  1544. /* usb device init */
  1545. static void dwc_gadget_init(dwc_handle *dwc)
  1546. {
  1547. uint32_t curmod;
  1548. int err;
  1549. // REG_CPM_CLKGR0 &= ~(1 << 2);
  1550. rt_hw_interrupt_mask(IRQ_OTG);
  1551. /* usb_cpm_init(); */
  1552. {
  1553. uint32_t ref_clk_div = 24 / 24; //24 / 24;
  1554. uint32_t usbpcr1;
  1555. /* select dwc otg */
  1556. REG_CPM_USBPCR1 |= USBPCR1_USB_SEL;
  1557. /* select utmi data bus width of port0 to 16bit/30M */
  1558. REG_CPM_USBPCR1 |= USBPCR1_WORD_IF0;
  1559. usbpcr1 = REG_CPM_USBPCR1;
  1560. usbpcr1 &= ~(0x3 << 24);
  1561. usbpcr1 |= (ref_clk_div << 24);
  1562. REG_CPM_USBPCR1 = usbpcr1;
  1563. /* fil */
  1564. REG_CPM_USBVBFIL = 0;
  1565. /* rdt */
  1566. REG_CPM_USBRDT = 0x96;
  1567. /* rdt - filload_en */
  1568. REG_CPM_USBRDT |= USBRDT_VBFIL_LD_EN;
  1569. /* TXRISETUNE & TXVREFTUNE. */
  1570. REG_CPM_USBPCR = 0x3f;
  1571. REG_CPM_USBPCR = 0x35;
  1572. #if 1
  1573. REG_CPM_USBPCR &= ~(1 << 31);
  1574. REG_CPM_USBPCR |= (1 << 23) | (1 << 24);
  1575. #endif
  1576. /* enable tx pre-emphasis */
  1577. REG_CPM_USBPCR |= USBPCR_TXPREEMPHTUNE;
  1578. /* OTGTUNE adjust */
  1579. REG_CPM_USBPCR = (7 << 14);
  1580. }
  1581. REG_CPM_USBPCR |= 1 << 20;
  1582. /* force usb device mode */
  1583. x1000_usb_set_device_only_mode(dwc);
  1584. x1000_usb_phy_init(dwc);
  1585. x1000_usb_phy_switch(dwc,1);
  1586. /* soft disconnect and soft reset */
  1587. REG_OTG_DCTL |= DCTL_SOFT_DISCONN;
  1588. udelay(3000);
  1589. /* reset dwc register */
  1590. dwc_otg_core_reset(dwc);
  1591. /* DWC OTG Core init */
  1592. dwc_otg_core_init(dwc,1);
  1593. /* Read Gintsts confirm the device or host mode */
  1594. curmod = REG_GINT_STS;
  1595. if (curmod & 0x1)
  1596. {
  1597. DWC_DBG("Curmod: Host Mode\n");
  1598. }
  1599. else
  1600. {
  1601. DWC_DBG("Curmod: Device Mode\n");
  1602. /* DWC OTG Device init */
  1603. dwc_otg_device_init(dwc);
  1604. /* DWC OTG Fifo init */
  1605. dwc_calculate_fifo_size(dwc);
  1606. }
  1607. /* End-point has been inited */
  1608. // dwc_init_endpoint(dwc);
  1609. }
  1610. static void x1000_usbd_isr_service(void *param)
  1611. {
  1612. dwc_handle *dwc = (dwc_handle *)param;
  1613. uint8_t err;
  1614. uint32_t intsts;
  1615. RT_ASSERT(dwc != RT_NULL);
  1616. while (1)
  1617. {
  1618. rt_sem_take(dwc->isr_sem, RT_WAITING_FOREVER);
  1619. intsts = REG_GINT_STS;
  1620. if (intsts & GINTSTS_OTG_INTR)
  1621. {
  1622. DWC_DBG("OTG_INTR\n");
  1623. dwc_otg_intr(dwc);
  1624. }
  1625. if ((intsts & GINTSTS_USB_EARLYSUSPEND)
  1626. || (intsts & GINTSTS_USB_SUSPEND)
  1627. || (intsts & GINTSTS_START_FRAM)
  1628. || (intsts & GINTSTS_USB_RESET)
  1629. || (intsts & GINTSTS_ENUM_DONE))
  1630. {
  1631. dwc_common_intr(dwc, intsts);
  1632. }
  1633. /* dwc in pio mode not dma mode */
  1634. if (intsts & GINTSTS_RXFIFO_NEMPTY)
  1635. {
  1636. DWC_DBG("GINTSTS_RXFIFO_NEMPTY!!\n");
  1637. if (dwc->is_dma == IS_SLAVE_MODE) dwc_handle_rxfifo_nempty(dwc);
  1638. REG_GINT_STS = GINTSTS_RXFIFO_NEMPTY;
  1639. }
  1640. if (intsts & GINTSTS_IEP_INTR)
  1641. {
  1642. DWC_DBG("IEP_INTR!!!\n");
  1643. dwc_handle_inep_intr(dwc);
  1644. }
  1645. if (intsts & GINTSTS_OEP_INTR)
  1646. {
  1647. DWC_DBG("OEP_INTR!!!\n");
  1648. dwc_handle_outep_intr(dwc);
  1649. }
  1650. if (intsts & GINTSTS_RSUME_DETE)
  1651. {
  1652. DWC_DBG("RESUME_INTR\n");
  1653. dwc_handle_resume_intr(dwc);
  1654. }
  1655. if (intsts & (1 << 31))
  1656. {
  1657. REG_GINT_STS = 1 << 31;
  1658. }
  1659. rt_hw_interrupt_umask(IRQ_OTG);
  1660. }
  1661. }
  1662. void x1000_usbd_init(dwc_handle *dwc)
  1663. {
  1664. uint32_t curmod = 0;
  1665. DWC_DBG("Init UDC %s %s\n",__DATE__,__TIME__);
  1666. if(dwc->isr_sem == RT_NULL)
  1667. {
  1668. dwc->isr_sem = rt_sem_create("dwcSem",0,RT_IPC_FLAG_FIFO);
  1669. if (!dwc->isr_sem)
  1670. {
  1671. DWC_DBG("%s %d sem create err\n", __func__, __LINE__);
  1672. while (1) ;
  1673. }
  1674. dwc->status.b.state = USB_CABLE_DISCONNECT;
  1675. dwc->status.b.event = 0;
  1676. }
  1677. dwc_gadget_init(dwc);
  1678. /* create a ISR service task */
  1679. {
  1680. rt_thread_t tid;
  1681. tid = rt_thread_create("dwcIntSv",
  1682. x1000_usbd_isr_service, (void *) dwc,
  1683. 2048,
  1684. RT_THREAD_PRIORITY_MAX/5,
  1685. 20);
  1686. if (tid != RT_NULL) rt_thread_startup(tid);
  1687. rt_kprintf("dwc interrupt service init done...\n");
  1688. }
  1689. /* request irq */
  1690. rt_hw_interrupt_install(IRQ_OTG,dwc_irq_handler,(void *)dwc,"otgISR");
  1691. rt_hw_interrupt_umask(IRQ_OTG);
  1692. DWC_DBG("[DWC] DWC request IRQ success %x\n", REG_GINT_MASK);
  1693. }