mt_musb.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456
  1. /*
  2. * Copyright (c) 2012 MediaTek Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files
  6. * (the "Software"), to deal in the Software without restriction,
  7. * including without limitation the rights to use, copy, modify, merge,
  8. * publish, distribute, sublicense, and/or sell copies of the Software,
  9. * and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <sys/types.h>
  24. #include <string.h>
  25. #include <stdlib.h>
  26. #include <debug.h>
  27. #include <reg.h>
  28. #include <platform/bitops.h>
  29. #include <platform/errno.h>
  30. #include <platform/mt_reg_base.h>
  31. #include <platform/mt_typedefs.h>
  32. #include <platform/timer.h>
  33. #include <kernel/thread.h>
  34. #include <platform/sec_devinfo.h>
  35. #include <dev/udc.h>
  36. #include <arch/ops.h>
  37. #include <mt_musb.h>
  38. #ifdef SUPPORT_QMU
  39. #include <mt_musb_qmu.h>
  40. #endif
  41. #if defined(mt6735) || defined(mt6580) || defined(mt6755) || \
  42. defined(mt8163) || defined(mt8167) || defined(mt8173)
  43. /* Fix duplicated defines of IRQ_MASK_HEADER and related APIs */
  44. #include <platform/mt_irq.h>
  45. #else
  46. #include <mt_gic.h>
  47. #endif
  48. #ifdef MACH_FPGA
  49. #define CFG_FPGA_PLATFORM (1)
  50. #else
  51. #define DBG_PHY_CALIBRATION 1
  52. #endif
  53. #define USB_DOUBLE_BUF
  54. #define USB_GINTR
  55. #ifdef USB_DEBUG
  56. /* DEBUG INFO Sections */
  57. #define DBG_USB_DUMP_DESC 0
  58. #define DBG_USB_DUMP_DATA 0
  59. #define DBG_USB_DUMP_SETUP 1
  60. #define DBG_USB_FIFO 0
  61. #define DBG_USB_GENERAL 1
  62. #define DBG_PHY_CALIBRATION 0
  63. #endif
  64. #define DBG_C(x...) dprintf(CRITICAL, x)
  65. #define DBG_I(x...) dprintf(INFO, x)
  66. #define DBG_S(x...) dprintf(SPEW, x)
  67. #if DBG_USB_GENERAL
  68. #define DBG_IRQ(x...) dprintf(INFO, x)
  69. #else
  70. #define DBG_IRQ(x...) do{} while(0)
  71. #endif
  72. /* bits used in all the endpoint status registers */
  73. #define EPT_TX(n) (1 << ((n) + 16))
  74. #define EPT_RX(n) (1 << (n))
  75. /* udc.h wrapper for usbdcore */
  76. static unsigned char usb_config_value = 0;
  77. EP0_STATE ep0_state = EP0_IDLE;
  78. int set_address = 0;
  79. u32 fifo_addr = FIFO_ADDR_START;
  80. #define EP0 0
  81. #define EP0_MAX_PACKET_SIZE 64
  82. /* Request types */
  83. #define USB_TYPE_STANDARD (0x00 << 5)
  84. #define USB_TYPE_CLASS (0x01 << 5)
  85. #define USB_TYPE_VENDOR (0x02 << 5)
  86. #define USB_TYPE_RESERVED (0x03 << 5)
  87. /* values used in GET_STATUS requests */
  88. #define USB_STAT_SELFPOWERED 0x01
  89. /* USB recipients */
  90. #define USB_RECIP_DEVICE 0x00
  91. #define USB_RECIP_INTERFACE 0x01
  92. #define USB_RECIP_ENDPOINT 0x02
  93. #define USB_RECIP_OTHER 0x03
  94. /* Endpoints */
  95. #define USB_EP_NUM_MASK 0x0f /* in bEndpointAddress */
  96. #define USB_EP_DIR_MASK 0x80
  97. #define USB_TYPE_MASK 0x60
  98. #define USB_RECIP_MASK 0x1f
  99. #define URB_BUF_SIZE 512
  100. #define EP0_BUF_SZ 4096
  101. static struct udc_endpoint *ep0in, *ep0out;
  102. static struct udc_request *ep0req;
  103. struct urb mt_ep0_urb;
  104. struct urb mt_tx_urb;
  105. struct urb mt_rx_urb;
  106. struct urb *ep0_urb = &mt_ep0_urb;
  107. struct urb *tx_urb = &mt_tx_urb;
  108. struct urb *rx_urb = &mt_rx_urb;
  109. static int usb_online = 0;
  110. static u8 dev_address = 0;
  111. static struct udc_device *the_device;
  112. static struct udc_gadget *the_gadget;
  113. /* end from hsusb.c */
  114. /* declare ept_complete handle */
  115. static void handle_ept_complete(struct udc_endpoint *ept);
  116. void board_usb_init(void)
  117. {
  118. mt_usb_phy_poweron();
  119. }
  120. struct udc_descriptor {
  121. struct udc_descriptor *next;
  122. unsigned short tag; /* ((TYPE << 8) | NUM) */
  123. unsigned short len; /* total length */
  124. unsigned char data[0];
  125. };
  126. #if DBG_USB_DUMP_SETUP
  127. static void dump_setup_packet(char *str, struct setup_packet *sp)
  128. {
  129. DBG_I("\n");
  130. DBG_I(str);
  131. DBG_I(" bmRequestType = %x\n", sp->type);
  132. DBG_I(" bRequest = %x\n", sp->request);
  133. DBG_I(" wValue = %x\n", sp->value);
  134. DBG_I(" wIndex = %x\n", sp->index);
  135. DBG_I(" wLength = %x\n", sp->length);
  136. }
  137. #else
  138. static void dump_setup_packet(char *str, struct setup_packet *sp) {}
  139. #endif
  140. static void copy_desc(struct urb *urb, void *data, int length)
  141. {
  142. #if DBG_USB_FIFO
  143. DBG_I("%s: urb: %x, data %x, length: %d, actual_length: %d\n",
  144. __func__, urb->buf, data, length, urb->actual_length);
  145. #endif
  146. //memcpy(urb->buf + urb->actual_length, data, length);
  147. memcpy(urb->buf, data, length);
  148. //urb->actual_length += length;
  149. urb->actual_length = length;
  150. #if DBG_USB_FIFO
  151. DBG_I("%s: urb: %x, data %x, length: %d, actual_length: %d\n",
  152. __func__, urb, data, length, urb->actual_length);
  153. #endif
  154. }
  155. struct udc_descriptor *udc_descriptor_alloc(unsigned type, unsigned num,
  156. unsigned len)
  157. {
  158. struct udc_descriptor *desc;
  159. if ((len > 255) || (len < 2) || (num > 255) || (type > 255))
  160. return 0;
  161. if (!(desc = malloc(sizeof(struct udc_descriptor) + len)))
  162. return 0;
  163. desc->next = 0;
  164. desc->tag = (type << 8) | num;
  165. desc->len = len;
  166. desc->data[0] = len;
  167. desc->data[1] = type;
  168. return desc;
  169. }
  170. static struct udc_descriptor *desc_list = 0;
  171. static unsigned next_string_id = 1;
  172. void udc_descriptor_register(struct udc_descriptor *desc)
  173. {
  174. desc->next = desc_list;
  175. desc_list = desc;
  176. }
  177. unsigned udc_string_desc_alloc(const char *str)
  178. {
  179. unsigned len;
  180. struct udc_descriptor *desc;
  181. unsigned char *data;
  182. if (next_string_id > 255)
  183. return 0;
  184. if (!str)
  185. return 0;
  186. len = strlen(str);
  187. desc = udc_descriptor_alloc(TYPE_STRING, next_string_id, len * 2 + 2);
  188. if (!desc)
  189. return 0;
  190. next_string_id++;
  191. /* expand ascii string to utf16 */
  192. data = desc->data + 2;
  193. while (len-- > 0) {
  194. *data++ = *str++;
  195. *data++ = 0;
  196. }
  197. udc_descriptor_register(desc);
  198. return desc->tag & 0xff;
  199. }
  200. static int mt_read_fifo(struct udc_endpoint *endpoint)
  201. {
  202. struct urb *urb = endpoint->rcv_urb;
  203. int len = 0, count = 0;
  204. int ep_num = endpoint->num;
  205. int index;
  206. unsigned char *cp;
  207. u32 *wp;
  208. #if !CFG_FPGA_PLATFORM
  209. u16 dma_cntl = 0;
  210. #endif
  211. if (ep_num == EP0)
  212. urb = ep0_urb;
  213. if (urb) {
  214. index = readb(INDEX);
  215. writeb(ep_num, INDEX);
  216. cp = (u8 *) (urb->buf + urb->actual_length);
  217. wp = (u32 *) cp;
  218. #if DBG_USB_FIFO
  219. DBG_I("%s: ep_num: %d, urb: %x, urb->buf: %x, urb->actual_length = %d\n",
  220. __func__, ep_num, urb, urb->buf, urb->actual_length);
  221. #endif
  222. count = len = readw(IECSR + RXCOUNT);
  223. if (ep_num != 0) {
  224. #if DBG_USB_FIFO
  225. DBG_I("%s: ep_num: %d count = %d\n",
  226. __func__, ep_num, count);
  227. #endif
  228. }
  229. /* FIX: DMA has problem write now */
  230. #if !CFG_FPGA_PLATFORM
  231. arch_clean_invalidate_cache_range((addr_t) cp, count);
  232. if (ep_num != 0) {
  233. writel((u32)wp, USB_DMA_ADDR (ep_num));
  234. writel(count, USB_DMA_COUNT (ep_num));
  235. dma_cntl =
  236. USB_DMA_BURST_MODE_3 | (ep_num << USB_DMA_ENDPNT_OFFSET) |
  237. USB_DMA_EN;
  238. writew(dma_cntl, USB_DMA_CNTL (ep_num));
  239. while (readw(USB_DMA_CNTL (ep_num)) & USB_DMA_EN);
  240. } else
  241. #endif
  242. {
  243. while (len > 0) {
  244. if (len >= 4) {
  245. *wp++ = readl(FIFO(ep_num));
  246. cp = (unsigned char *) wp;
  247. //DBG_I("USB READ FIFO: wp = %lu, cp = %lu\n", wp, cp);
  248. len -= 4;
  249. } else {
  250. *cp++ = readb(FIFO(ep_num));
  251. //DBG_I("USB READ FIFO: wp = %lu, cp = %lu\n", wp, cp);
  252. len--;
  253. }
  254. }
  255. }
  256. #if DBG_USB_DUMP_DATA
  257. if (ep_num != 0) {
  258. DBG_I("%s: &urb->buf: %x\n", __func__, urb->buf);
  259. DBG_I("[USB] dump data:\n");
  260. hexdump8(urb->buf, count);
  261. }
  262. #endif
  263. urb->actual_length += count;
  264. writeb(index, INDEX);
  265. }
  266. return count;
  267. }
  268. static int mt_write_fifo(struct udc_endpoint *endpoint)
  269. {
  270. struct urb *urb = endpoint->tx_urb;
  271. int last = 0, count = 0;
  272. int ep_num = endpoint->num;
  273. int index;
  274. unsigned char *cp = NULL;
  275. #ifdef USB_TX_DMA_MODE_0
  276. u32 *wp;
  277. u16 dma_cntl = 0;
  278. #endif
  279. if (ep_num == EP0)
  280. urb = ep0_urb;
  281. if (urb) {
  282. index = readb(INDEX);
  283. writeb(ep_num, INDEX);
  284. #if DBG_USB_DUMP_DESC
  285. DBG_I("%s: dump desc\n", __func__);
  286. hexdump8(urb->buf, urb->actual_length);
  287. #endif
  288. #if DBG_USB_FIFO
  289. DBG_I("%s: ep_num: %d urb: %x, actual_length: %d\n",
  290. __func__, ep_num, urb, urb->actual_length);
  291. DBG_I("%s: sent: %d, tx_pkt_size: %d\n", __func__, endpoint->sent, endpoint->maxpkt);
  292. #endif
  293. count = last = MIN (urb->actual_length - endpoint->sent, endpoint->maxpkt);
  294. //count = last = urb->actual_length;
  295. #if DBG_USB_FIFO
  296. DBG_I("%s: count: %d\n", __func__, count);
  297. DBG_I("%s: urb->actual_length = %d\n", __func__, urb->actual_length);
  298. DBG_I("%s: endpoint->sent = %d\n", __func__, endpoint->sent);
  299. #endif
  300. if (count < 0) {
  301. DBG_C("%s: something is wrong, count < 0", __func__);
  302. }
  303. if (count) {
  304. cp = urb->buf + endpoint->sent;
  305. #ifdef USB_TX_DMA_MODE_0
  306. wp = (u32 *)cp;
  307. arch_clean_invalidate_cache_range((addr_t) cp, count);
  308. if (ep_num != 0) {
  309. writel(wp, USB_DMA_ADDR(ep_num));
  310. writel(count, USB_DMA_COUNT(ep_num));
  311. dma_cntl =
  312. USB_DMA_BURST_MODE_3 | (ep_num << USB_DMA_ENDPNT_OFFSET) |
  313. USB_DMA_EN | USB_DMA_DIR;
  314. writew(dma_cntl, USB_DMA_CNTL(ep_num));
  315. while (readw(USB_DMA_CNTL (ep_num)) & USB_DMA_EN);
  316. } else
  317. #endif
  318. {
  319. //DBG("---------write USB fifo---------\n");
  320. while (count > 0) {
  321. //hexdump8(cp, 1);
  322. writeb(*cp, FIFO (ep_num));
  323. cp++;
  324. count--;
  325. }
  326. }
  327. }
  328. endpoint->last = last;
  329. endpoint->sent += last;
  330. writeb(index, INDEX);
  331. }
  332. return last;
  333. }
  334. struct udc_endpoint * mt_find_ep(int ep_num, u8 dir)
  335. {
  336. int i;
  337. u8 in = 0;
  338. /* convert dir to in */
  339. if (dir == USB_DIR_IN) /* dir == USB_DIR_IN */
  340. in = 1;
  341. /* for (i = 0; i < udc_device->max_endpoints; i++) */
  342. /* for (i = 0; i < the_gadget->ifc_endpoints; i++) */
  343. for (i = 0; i < MT_EP_NUM; i++) {
  344. if ((ep_list[i].num == ep_num) && (ep_list[i].in == in)) {
  345. #if DBG_USB_GENERAL
  346. DBG_I("%s: find ep!\n", __func__);
  347. #endif
  348. return &ep_list[i];
  349. }
  350. }
  351. return NULL;
  352. }
  353. static void mt_udc_flush_fifo(u8 ep_num, u8 dir)
  354. {
  355. u16 tmpReg16;
  356. u8 index;
  357. struct udc_endpoint *endpoint;
  358. index = readb(INDEX);
  359. writeb(ep_num, INDEX);
  360. if (ep_num == 0) {
  361. tmpReg16 = readw(IECSR + CSR0);
  362. tmpReg16 |= EP0_FLUSH_FIFO;
  363. writew(tmpReg16, IECSR + CSR0);
  364. writew(tmpReg16, IECSR + CSR0);
  365. } else {
  366. endpoint = mt_find_ep(ep_num, dir);
  367. if (endpoint == NULL) {
  368. DBG_C("endpoint is NULL!\n");
  369. return;
  370. }
  371. if (endpoint->in == 0) { /* USB_DIR_OUT */
  372. tmpReg16 = readw(IECSR + RXCSR);
  373. tmpReg16 |= EPX_RX_FLUSHFIFO;
  374. writew(tmpReg16, IECSR + RXCSR);
  375. writew(tmpReg16, IECSR + RXCSR);
  376. } else {
  377. tmpReg16 = readw(IECSR + TXCSR);
  378. tmpReg16 |= EPX_TX_FLUSHFIFO;
  379. writew(tmpReg16, IECSR + TXCSR);
  380. writew(tmpReg16, IECSR + TXCSR);
  381. }
  382. }
  383. /* recover index register */
  384. writeb(index, INDEX);
  385. }
  386. /* the endpoint does not support the received command, stall it!! */
  387. static void udc_stall_ep(unsigned int ep_num, u8 dir)
  388. {
  389. struct udc_endpoint *endpoint = mt_find_ep(ep_num, dir);
  390. u8 index;
  391. u16 csr;
  392. DBG_C("[USB] %s\n", __func__);
  393. if (endpoint == NULL) {
  394. DBG_C("endpoint is NULL!\n");
  395. return;
  396. }
  397. index = readb(INDEX);
  398. writeb(ep_num, INDEX);
  399. if (ep_num == 0) {
  400. csr = readw(IECSR + CSR0);
  401. csr |= EP0_SENDSTALL;
  402. writew(csr, IECSR + CSR0);
  403. mt_udc_flush_fifo(ep_num, USB_DIR_OUT);
  404. } else {
  405. if (endpoint->in == 0) { /* USB_DIR_OUT */
  406. csr = readb(IECSR + RXCSR);
  407. csr |= EPX_RX_SENDSTALL;
  408. writew(csr, IECSR + RXCSR);
  409. mt_udc_flush_fifo(ep_num, USB_DIR_OUT);
  410. } else {
  411. csr = readb(IECSR + TXCSR);
  412. csr |= EPX_TX_SENDSTALL;
  413. writew(csr, IECSR + TXCSR);
  414. mt_udc_flush_fifo(ep_num, USB_DIR_IN);
  415. }
  416. }
  417. //mt_udc_flush_fifo (ep_num, USB_DIR_OUT);
  418. //mt_udc_flush_fifo (ep_num, USB_DIR_IN);
  419. ep0_state = EP0_IDLE;
  420. writeb(index, INDEX);
  421. return;
  422. }
  423. /*
  424. * If abnormal DATA transfer happened, like USB unplugged,
  425. * we cannot fix this after mt_udc_reset().
  426. * Because sometimes there will come reset twice.
  427. */
  428. static void mt_udc_suspend(void)
  429. {
  430. /* handle abnormal DATA transfer if we had any */
  431. struct udc_endpoint *endpoint;
  432. int i;
  433. /* deal with flags */
  434. usb_online = 0;
  435. usb_config_value = 0;
  436. the_gadget->notify(the_gadget, UDC_EVENT_OFFLINE);
  437. /* error out any pending reqs */
  438. for (i = 1; i < MT_EP_NUM; i++) {
  439. /* ensure that ept_complete considers
  440. * this to be an error state
  441. */
  442. #if DBG_USB_GENERAL
  443. DBG_I("%s: ep: %i, in: %s, req: %x\n",
  444. __func__, ep_list[i].num, ep_list[i].in ? "IN" : "OUT", ep_list[i].req);
  445. #endif
  446. if ((ep_list[i].req && (ep_list[i].in == 0)) || /* USB_DIR_OUT */
  447. (ep_list[i].req && (ep_list[i].in == 1))) { /* USB_DIR_IN */
  448. ep_list[i].status = -1; /* HALT */
  449. endpoint = &ep_list[i];
  450. handle_ept_complete(endpoint);
  451. }
  452. }
  453. /* this is required for error handling during data transfer */
  454. txn_status = -1;
  455. #if defined(SUPPORT_QMU)
  456. /* stop qmu engine */
  457. mu3d_hal_stop_qmu(1, USB_DIR_IN);
  458. mu3d_hal_stop_qmu(1, USB_DIR_OUT);
  459. /* Disable QMU Tx/Rx. */
  460. /* do qmu flush */
  461. mu3d_hal_flush_qmu(1, USB_DIR_IN);
  462. mu3d_hal_flush_qmu(1, USB_DIR_OUT);
  463. /* mu3d_hal_reset_ep: we do reset here only, do not start qmu here */
  464. #endif
  465. }
  466. static void mt_udc_rxtxmap_recover(void)
  467. {
  468. int i;
  469. for (i = 1; i < MT_EP_NUM; i++) {
  470. if (ep_list[i].num != 0) { /* allocated */
  471. writeb(ep_list[i].num, INDEX);
  472. if (ep_list[i].in == 0) /* USB_DIR_OUT */
  473. writel(ep_list[i].maxpkt, (IECSR + RXMAP));
  474. else
  475. writel(ep_list[i].maxpkt, (IECSR + TXMAP));
  476. }
  477. }
  478. }
  479. static void mt_udc_reset(void)
  480. {
  481. /* MUSBHDRC automatically does the following when reset signal is detected */
  482. /* 1. Sets FAddr to 0
  483. * 2. Sets Index to 0
  484. * 3. Flush all endpoint FIFOs
  485. * 4. Clears all control/status registers
  486. * 5. Enables all endpoint interrupts
  487. * 6. Generates a Rest interrupt
  488. */
  489. DBG_I("[USB] %s\n", __func__);
  490. /* disable all endpoint interrupts */
  491. writeb(0, INTRTXE);
  492. writeb(0, INTRRXE);
  493. writeb(0, INTRUSBE);
  494. writew(SWRST_SWRST | SWRST_DISUSBRESET, SWRST);
  495. dev_address = 0;
  496. /* flush FIFO */
  497. mt_udc_flush_fifo(0, USB_DIR_OUT);
  498. mt_udc_flush_fifo(1, USB_DIR_OUT);
  499. mt_udc_flush_fifo(1, USB_DIR_IN);
  500. //mt_udc_flush_fifo (2, USB_DIR_IN);
  501. /* detect USB speed */
  502. if (readb(POWER) & PWR_HS_MODE) {
  503. DBG_I("[USB] USB High Speed\n");
  504. // enable_highspeed();
  505. } else {
  506. DBG_I("[USB] USB Full Speed\n");
  507. }
  508. /* restore RXMAP and TXMAP if the endpoint has been configured */
  509. mt_udc_rxtxmap_recover();
  510. #ifdef SUPPORT_QMU
  511. {
  512. unsigned int wCsr = 0;
  513. unsigned int intr_e = 0;
  514. wCsr |= EPX_TX_DMAREQEN;
  515. writew(wCsr, IECSR + TXCSR);
  516. //turn off intrTx
  517. intr_e = readb(INTRTXE);
  518. intr_e = intr_e & (~(1<<(1)));
  519. writeb(intr_e, INTRTXE);
  520. wCsr = 0;
  521. intr_e = 0;
  522. wCsr |= EPX_RX_DMAREQEN;
  523. writew(wCsr, IECSR + RXCSR);
  524. //turn off intrRx
  525. intr_e = readb(INTRRXE);
  526. intr_e = intr_e & (~(1<<(1)));
  527. writeb(intr_e, INTRRXE);
  528. }
  529. #endif
  530. /* enable suspend */
  531. writeb((INTRUSB_SUSPEND | INTRUSB_RESUME | INTRUSB_RESET |INTRUSB_DISCON), INTRUSBE);
  532. txn_status = 0;
  533. }
  534. static void mt_udc_ep0_write(void)
  535. {
  536. struct udc_endpoint *endpoint = &ep_list[EP0];
  537. int count = 0;
  538. u16 csr0 = 0;
  539. u8 index = 0;
  540. index = readb(INDEX);
  541. writeb(0, INDEX);
  542. csr0 = readw(IECSR + CSR0);
  543. if (csr0 & EP0_TXPKTRDY) {
  544. DBG_I("mt_udc_ep0_write: ep0 is not ready to be written\n");
  545. return;
  546. }
  547. count = mt_write_fifo(endpoint);
  548. #if DBG_USB_GENERAL
  549. DBG_I("%s: count = %d\n", __func__, count);
  550. #endif
  551. if (count < EP0_MAX_PACKET_SIZE) {
  552. /* last packet */
  553. csr0 |= (EP0_TXPKTRDY | EP0_DATAEND);
  554. ep0_urb->actual_length = 0;
  555. endpoint->sent = 0;
  556. ep0_state = EP0_IDLE;
  557. } else {
  558. /* more packets are waiting to be transferred */
  559. csr0 |= EP0_TXPKTRDY;
  560. }
  561. writew(csr0, IECSR + CSR0);
  562. writeb(index, INDEX);
  563. return;
  564. }
  565. static void mt_udc_ep0_read(void)
  566. {
  567. struct udc_endpoint *endpoint = &ep_list[EP0];
  568. int count = 0;
  569. u16 csr0 = 0;
  570. u8 index = 0;
  571. index = readb(INDEX);
  572. writeb(EP0, INDEX);
  573. csr0 = readw(IECSR + CSR0);
  574. /* erroneous ep0 interrupt */
  575. if (!(csr0 & EP0_RXPKTRDY)) {
  576. return;
  577. }
  578. count = mt_read_fifo(endpoint);
  579. if (count <= EP0_MAX_PACKET_SIZE) {
  580. /* last packet */
  581. csr0 |= (EP0_SERVICED_RXPKTRDY | EP0_DATAEND);
  582. ep0_state = EP0_IDLE;
  583. } else {
  584. /* more packets are waiting to be transferred */
  585. csr0 |= EP0_SERVICED_RXPKTRDY;
  586. }
  587. writew(csr0, IECSR + CSR0);
  588. writeb(index, INDEX);
  589. return;
  590. }
  591. static int ep0_standard_setup(struct urb *urb)
  592. {
  593. struct setup_packet *request;
  594. struct udc_descriptor *desc;
  595. /* for CLEAR_FEATURE */
  596. u8 ep_num; /* ep number */
  597. u8 dir; /* DIR */
  598. u8 index;
  599. u16 rxtoggle, txtoggle;
  600. //struct udc_device *device;
  601. u8 *cp = urb->buf;
  602. #if 0
  603. if (!urb || !urb->device) {
  604. DBG ("\n!urb || !urb->device\n");
  605. return FALSE;
  606. }
  607. #endif
  608. request = &urb->device_request;
  609. //device = urb->device;
  610. dump_setup_packet("[USB] Device Request\n", request);
  611. if ((request->type & USB_TYPE_MASK) != 0) {
  612. return FALSE; /* Class-specific requests are handled elsewhere */
  613. }
  614. /* handle all requests that return data (direction bit set on bm RequestType) */
  615. if ((request->type & USB_EP_DIR_MASK)) {
  616. /* send the descriptor */
  617. ep0_state = EP0_TX;
  618. switch (request->request) {
  619. /* data stage: from device to host */
  620. case GET_STATUS:
  621. #if DBG_USB_GENERAL
  622. DBG_I("GET_STATUS\n");
  623. #endif
  624. urb->actual_length = 2;
  625. cp[0] = cp[1] = 0;
  626. switch (request->type & USB_RECIP_MASK) {
  627. case USB_RECIP_DEVICE:
  628. cp[0] = USB_STAT_SELFPOWERED;
  629. break;
  630. case USB_RECIP_OTHER:
  631. urb->actual_length = 0;
  632. break;
  633. default:
  634. break;
  635. }
  636. return 0;
  637. case GET_DESCRIPTOR:
  638. #if DBG_USB_GENERAL
  639. DBG_I("GET_DESCRIPTOR\n");
  640. #endif
  641. /* usb_highspeed? */
  642. for (desc = desc_list; desc; desc = desc->next) {
  643. #if DBG_USB_DUMP_DESC
  644. DBG_I("desc->tag: %x: request->value: %x\n", desc->tag, request->value);
  645. #endif
  646. if (desc->tag == request->value) {
  647. #if DBG_USB_DUMP_DESC
  648. DBG_I("Find packet!\n");
  649. #endif
  650. unsigned len = desc->len;
  651. if (len > request->length)
  652. len = request->length;
  653. #if DBG_USB_GENERAL
  654. DBG_I("%s: urb: %x, cp: %p\n", __func__, urb, cp);
  655. #endif
  656. copy_desc(urb, desc->data, len);
  657. return 0;
  658. }
  659. }
  660. /* descriptor lookup failed */
  661. return FALSE;
  662. case GET_CONFIGURATION:
  663. #if DBG_USB_GENERAL
  664. DBG_I("GET_CONFIGURATION\n");
  665. DBG_I("USB_EP_DIR_MASK\n");
  666. #endif
  667. #if 0
  668. urb->actual_length = 1;
  669. ((char *) urb->buf)[0] = device->configuration;
  670. #endif
  671. // return 0;
  672. break;
  673. case GET_INTERFACE:
  674. #if DBG_USB_GENERAL
  675. DBG_I("GET_INTERFACE\n");
  676. #endif
  677. #if 0
  678. urb->actual_length = 1;
  679. ((char *) urb->buf)[0] = device->alternate;
  680. return 0;
  681. #endif
  682. break;
  683. default:
  684. DBG_C("Unsupported command with TX data stage\n");
  685. break;
  686. }
  687. } else {
  688. switch (request->request) {
  689. case SET_ADDRESS:
  690. #if DBG_USB_GENERAL
  691. DBG_I("SET_ADDRESS\n");
  692. #endif
  693. dev_address = (request->value);
  694. set_address = 1;
  695. return 0;
  696. case SET_CONFIGURATION:
  697. #if DBG_USB_GENERAL
  698. DBG_I("SET_CONFIGURATION\n");
  699. #endif
  700. #if 0
  701. device->configuration = (request->value) & 0x7f;
  702. device->interface = device->alternate = 0;
  703. #endif
  704. if (request->value == 1) {
  705. usb_config_value = 1;
  706. the_gadget->notify(the_gadget, UDC_EVENT_ONLINE);
  707. } else {
  708. usb_config_value = 0;
  709. the_gadget->notify(the_gadget, UDC_EVENT_OFFLINE);
  710. }
  711. usb_online = request->value ? 1 : 0;
  712. //usb_status(request->value ? 1 : 0, usb_highspeed);
  713. return 0;
  714. case CLEAR_FEATURE:
  715. #if DBG_USB_GENERAL
  716. DBG_I("CLEAR_FEATURE\n");
  717. #endif
  718. ep_num = request->index & 0xf;
  719. dir = request->index & 0x80;
  720. /* wvalue == 0 (USB_ENDPOINT_HALT) */
  721. if ((request->value == 0) && (request->length == 0)) {
  722. #if DBG_USB_GENERAL
  723. DBG_I("Clear Feature: ep: %d dir: %d\n", ep_num, dir);
  724. #endif
  725. index = readb(INDEX);
  726. writeb(ep_num, index);
  727. switch (dir) {
  728. case USB_DIR_IN:
  729. DBG_I("Clear tx toggle\n");
  730. txtoggle = readw(TXTOG);
  731. DBG_I("TXTOG: 0x%x\n", txtoggle);
  732. if (txtoggle) {
  733. /* TX TOGGLE ENABLE */
  734. writew(txtoggle, TXTOGEN);
  735. txtoggle &= ~(txtoggle);
  736. writew(txtoggle, TXTOG);
  737. txtoggle = readw(TXTOG);
  738. DBG_I("after clear TXTOG: 0x%x\n", txtoggle);
  739. }
  740. break;
  741. case USB_DIR_OUT:
  742. DBG_I("Clear rx toggle\n");
  743. rxtoggle = readw(RXTOG);
  744. DBG_I("RXTOG: 0x%x\n", rxtoggle);
  745. if (rxtoggle) {
  746. /* RX TOGGLE ENABLE */
  747. writew(rxtoggle, RXTOGEN);
  748. rxtoggle &= ~(rxtoggle);
  749. writew(rxtoggle, RXTOG);
  750. rxtoggle = readw(RXTOG);
  751. DBG_I("after clear RXTOG: 0x%x\n", rxtoggle);
  752. }
  753. break;
  754. default:
  755. break;
  756. }
  757. /* write back index */
  758. writeb(index, INDEX);
  759. return 0;
  760. }
  761. break;
  762. default:
  763. DBG_C("Unsupported command with RX data stage\n");
  764. break;
  765. }
  766. }
  767. return FALSE;
  768. }
  769. static void mt_udc_ep0_setup(void)
  770. {
  771. struct udc_endpoint *endpoint = &ep_list[0];
  772. u8 index;
  773. u8 stall = 0;
  774. u16 csr0;
  775. struct setup_packet *request;
  776. #ifdef USB_DEBUG
  777. u16 count;
  778. #endif
  779. index = readb(INDEX);
  780. writeb(0, INDEX);
  781. /* Read control status register for endpiont 0 */
  782. csr0 = readw(IECSR + CSR0);
  783. /* check whether RxPktRdy is set? */
  784. if (!(csr0 & EP0_RXPKTRDY))
  785. return;
  786. /* unload fifo */
  787. ep0_urb->actual_length = 0;
  788. #ifndef USB_DEBUG
  789. mt_read_fifo(endpoint);
  790. #else
  791. count = mt_read_fifo(endpoint);
  792. #if DBG_USB_FIFO
  793. DBG_I("%s: mt_read_fifo count = %d\n", __func__, count);
  794. #endif
  795. #endif
  796. /* decode command */
  797. request = &ep0_urb->device_request;
  798. memcpy(request, ep0_urb->buf, sizeof(struct setup_packet));
  799. if (((request->type) & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
  800. #if DBG_USB_GENERAL
  801. DBG_I("[USB] Standard Request\n");
  802. #endif
  803. stall = ep0_standard_setup(ep0_urb);
  804. if (stall) {
  805. dump_setup_packet("[USB] STANDARD REQUEST NOT SUPPORTED\n", request);
  806. }
  807. } else if (((request->type) & USB_TYPE_MASK) == USB_TYPE_CLASS) {
  808. #if DBG_USB_GENERAL
  809. DBG_I("[USB] Class-Specific Request\n");
  810. #endif
  811. // stall = ep0_class_setup(ep0_urb);
  812. /* Mark dead code, reported by Coverity. */
  813. //if (stall) {
  814. // dump_setup_packet("[USB] CLASS REQUEST NOT SUPPORTED\n", request);
  815. //}
  816. } else if (((request->type) & USB_TYPE_MASK) == USB_TYPE_VENDOR) {
  817. #if DBG_USB_GENERAL
  818. DBG_I("[USB] Vendor-Specific Request\n");
  819. /* do nothing now */
  820. DBG_I("[USB] ALL VENDOR-SPECIFIC REQUESTS ARE NOT SUPPORTED!!\n");
  821. #endif
  822. }
  823. if (stall) {
  824. /* the received command is not supported */
  825. udc_stall_ep(0, USB_DIR_OUT);
  826. return;
  827. }
  828. switch (ep0_state) {
  829. case EP0_TX:
  830. /* data stage: from device to host */
  831. #if DBG_USB_GENERAL
  832. DBG_I("%s: EP0_TX\n", __func__);
  833. #endif
  834. csr0 = readw(IECSR + CSR0);
  835. csr0 |= (EP0_SERVICED_RXPKTRDY);
  836. writew(csr0, IECSR + CSR0);
  837. mt_udc_ep0_write();
  838. break;
  839. case EP0_RX:
  840. /* data stage: from host to device */
  841. #if DBG_USB_GENERAL
  842. DBG_I("%s: EP0_RX\n", __func__);
  843. #endif
  844. csr0 = readw(IECSR + CSR0);
  845. csr0 |= (EP0_SERVICED_RXPKTRDY);
  846. writew(csr0, IECSR + CSR0);
  847. break;
  848. case EP0_IDLE:
  849. /* no data stage */
  850. #if DBG_USB_GENERAL
  851. DBG_I("%s: EP0_IDLE\n", __func__);
  852. #endif
  853. csr0 = readw(IECSR + CSR0);
  854. csr0 |= (EP0_SERVICED_RXPKTRDY | EP0_DATAEND);
  855. writew(csr0, IECSR + CSR0);
  856. writew(csr0, IECSR + CSR0);
  857. break;
  858. default:
  859. break;
  860. }
  861. writeb(index, INDEX);
  862. return;
  863. }
  864. static void mt_udc_ep0_handler(void)
  865. {
  866. u16 csr0;
  867. u8 index = 0;
  868. index = readb(INDEX);
  869. writeb(0, INDEX);
  870. csr0 = readw(IECSR + CSR0);
  871. if (csr0 & EP0_SENTSTALL) {
  872. #if DBG_USB_GENERAL
  873. DBG_I("USB: [EP0] SENTSTALL\n");
  874. #endif
  875. /* needs implementation for exception handling here */
  876. ep0_state = EP0_IDLE;
  877. }
  878. if (csr0 & EP0_SETUPEND) {
  879. #if DBG_USB_GENERAL
  880. DBG_I("USB: [EP0] SETUPEND\n");
  881. #endif
  882. csr0 |= EP0_SERVICE_SETUP_END;
  883. writew(csr0, IECSR + CSR0);
  884. ep0_state = EP0_IDLE;
  885. }
  886. switch (ep0_state) {
  887. case EP0_IDLE:
  888. #if DBG_USB_GENERAL
  889. DBG_I("%s: EP0_IDLE\n", __func__);
  890. #endif
  891. if (set_address) {
  892. writeb(dev_address, FADDR);
  893. set_address = 0;
  894. }
  895. mt_udc_ep0_setup();
  896. break;
  897. case EP0_TX:
  898. #if DBG_USB_GENERAL
  899. DBG_I("%s: EP0_TX\n", __func__);
  900. #endif
  901. mt_udc_ep0_write();
  902. break;
  903. case EP0_RX:
  904. #if DBG_USB_GENERAL
  905. DBG_I("%s: EP0_RX\n", __func__);
  906. #endif
  907. mt_udc_ep0_read();
  908. break;
  909. default:
  910. break;
  911. }
  912. writeb(index, INDEX);
  913. return;
  914. }
  915. /*
  916. * udc_setup_ep - setup endpoint
  917. *
  918. * Associate a physical endpoint with endpoint_instance and initialize FIFO
  919. */
  920. void mt_setup_ep(unsigned int ep, struct udc_endpoint *endpoint)
  921. {
  922. u8 index;
  923. u16 csr;
  924. u16 csr0;
  925. u16 max_packet_size;
  926. u8 fifosz = 0;
  927. /* EP table records in bits hence bit 1 is ep0 */
  928. index = readb(INDEX);
  929. writeb(ep, INDEX);
  930. if (ep == EP0) {
  931. /* Read control status register for endpiont 0 */
  932. csr0 = readw(IECSR + CSR0);
  933. /* check whether RxPktRdy is set? */
  934. if (!(csr0 & EP0_RXPKTRDY))
  935. return;
  936. }
  937. /* Configure endpoint fifo */
  938. /* Set fifo address, fifo size, and fifo max packet size */
  939. #if DBG_USB_GENERAL
  940. DBG_I("%s: endpoint->in: %d, maxpkt: %d\n",
  941. __func__, endpoint->in, endpoint->maxpkt);
  942. #endif
  943. if (endpoint->in == 0) { /* USB_DIR_OUT */
  944. /* Clear data toggle to 0 */
  945. csr = readw(IECSR + RXCSR);
  946. /* pangyen 20090911 */
  947. csr |= EPX_RX_CLRDATATOG | EPX_RX_FLUSHFIFO;
  948. writew(csr, IECSR + RXCSR);
  949. /* Set fifo address */
  950. writew(fifo_addr >> 3, RXFIFOADD);
  951. /* Set fifo max packet size */
  952. max_packet_size = endpoint->maxpkt;
  953. writew(max_packet_size, IECSR + RXMAP);
  954. /* Set fifo size (double buffering is currently not enabled) */
  955. switch (max_packet_size) {
  956. case 8:
  957. case 16:
  958. case 32:
  959. case 64:
  960. case 128:
  961. case 256:
  962. case 512:
  963. case 1024:
  964. case 2048:
  965. if (endpoint->mode == DOUBLE_BUF)
  966. fifosz |= FIFOSZ_DPB;
  967. fifosz |= uffs(max_packet_size >> 4);
  968. writeb(fifosz, RXFIFOSZ);
  969. break;
  970. case 4096:
  971. fifosz |= uffs(max_packet_size >> 4);
  972. writeb(fifosz, RXFIFOSZ);
  973. break;
  974. case 3072:
  975. fifosz = uffs(4096 >> 4);
  976. writeb(fifosz, RXFIFOSZ);
  977. break;
  978. default:
  979. DBG_C("The max_packet_size for ep %d is not supported\n", ep);
  980. }
  981. } else {
  982. /* Clear data toggle to 0 */
  983. csr = readw(IECSR + TXCSR);
  984. /* pangyen 20090911 */
  985. csr |= EPX_TX_CLRDATATOG | EPX_TX_FLUSHFIFO;
  986. writew(csr, IECSR + TXCSR);
  987. /* Set fifo address */
  988. writew(fifo_addr >> 3, TXFIFOADD);
  989. /* Set fifo max packet size */
  990. max_packet_size = endpoint->maxpkt;
  991. writew(max_packet_size, IECSR + TXMAP);
  992. /* Set fifo size(double buffering is currently not enabled) */
  993. switch (max_packet_size) {
  994. case 8:
  995. case 16:
  996. case 32:
  997. case 64:
  998. case 128:
  999. case 256:
  1000. case 512:
  1001. case 1024:
  1002. case 2048:
  1003. if (endpoint->mode == DOUBLE_BUF)
  1004. fifosz |= FIFOSZ_DPB;
  1005. /* Add for resolve issue reported by Coverity */
  1006. fifosz |= uffs(max_packet_size >> 4);
  1007. writeb(fifosz, TXFIFOSZ);
  1008. break;
  1009. case 4096:
  1010. fifosz |= uffs(max_packet_size >> 4);
  1011. writeb(fifosz, TXFIFOSZ);
  1012. break;
  1013. case 3072:
  1014. fifosz = uffs(4096 >> 4);
  1015. writeb(fifosz, TXFIFOSZ);
  1016. break;
  1017. default:
  1018. DBG_C("The max_packet_size for ep %d is not supported\n", ep);
  1019. }
  1020. }
  1021. if (endpoint->mode == DOUBLE_BUF)
  1022. fifo_addr += (max_packet_size << 1);
  1023. else
  1024. fifo_addr += max_packet_size;
  1025. /* recover INDEX register */
  1026. writeb(index, INDEX);
  1027. }
  1028. struct udc_endpoint *_udc_endpoint_alloc(unsigned char num, unsigned char in,
  1029. unsigned short max_pkt)
  1030. {
  1031. int i;
  1032. /*
  1033. * find an unused slot in ep_list from EP1 to MAX_EP
  1034. * for example, EP1 will use 2 slot one for IN and the other for OUT
  1035. */
  1036. if (num != EP0) {
  1037. for (i = 1; i < MT_EP_NUM; i++) {
  1038. if (ep_list[i].num == 0) /* usable */
  1039. break;
  1040. }
  1041. if (i == MT_EP_NUM) /* ep has been exhausted. */
  1042. return NULL;
  1043. if (in) { /* usb EP1 tx */
  1044. ep_list[i].tx_urb = tx_urb;
  1045. #ifdef USB_DOUBLE_BUF
  1046. ep_list[i].mode = DOUBLE_BUF;
  1047. #endif
  1048. } else { /* usb EP1 rx */
  1049. ep_list[i].rcv_urb = rx_urb;
  1050. #ifdef USB_DOUBLE_BUF
  1051. ep_list[i].mode = DOUBLE_BUF;
  1052. #endif
  1053. }
  1054. } else {
  1055. i = EP0; /* EP0 */
  1056. }
  1057. ep_list[i].maxpkt = max_pkt;
  1058. ep_list[i].num = num;
  1059. ep_list[i].in = in;
  1060. ep_list[i].req = NULL;
  1061. /* store EPT_TX/RX info */
  1062. if (ep_list[i].in) {
  1063. ep_list[i].bit = EPT_TX(num);
  1064. } else {
  1065. ep_list[i].bit = EPT_RX(num);
  1066. }
  1067. /* write parameters to this ep (write to hardware) */
  1068. mt_setup_ep(num, &ep_list[i]);
  1069. DBG_I("[USB] ept%d %s @%p/%p max=%d bit=%x\n",
  1070. num, in ? "in" : "out", &ep_list[i], &ep_list, max_pkt, ep_list[i].bit);
  1071. return &ep_list[i];
  1072. }
  1073. #define SETUP(type,request) (((type) << 8) | (request))
  1074. static unsigned long ept_alloc_table = EPT_TX(0) | EPT_RX(0);
  1075. struct udc_endpoint *udc_endpoint_alloc(unsigned type, unsigned maxpkt)
  1076. {
  1077. struct udc_endpoint *ept;
  1078. unsigned n;
  1079. unsigned in;
  1080. if (type == UDC_TYPE_BULK_IN) {
  1081. in = 1;
  1082. } else if (type == UDC_TYPE_BULK_OUT) {
  1083. in = 0;
  1084. } else {
  1085. return 0;
  1086. }
  1087. /* udc_endpoint_alloc is used for EPx except EP0 */
  1088. for (n = 1; n < 16; n++) {
  1089. unsigned long bit = in ? EPT_TX(n) : EPT_RX(n);
  1090. if (ept_alloc_table & bit)
  1091. continue;
  1092. ept = _udc_endpoint_alloc(n, in, maxpkt);
  1093. if (ept)
  1094. ept_alloc_table |= bit;
  1095. return ept;
  1096. }
  1097. return 0;
  1098. }
  1099. static void handle_ept_complete(struct udc_endpoint *ept)
  1100. {
  1101. unsigned int actual;
  1102. int status;
  1103. struct udc_request *req;
  1104. req = ept->req;
  1105. if (req) {
  1106. #if DBG_USB_GENERAL
  1107. DBG_I("%s: req: %x: req->length: %d: status: %d\n", __func__, req, req->length, ept->status);
  1108. #endif
  1109. /* release this request for processing next */
  1110. ept->req = NULL;
  1111. if (ept->status == -1) {
  1112. actual = 0;
  1113. status = -1;
  1114. DBG_C("%s: EP%d/%s FAIL status: %x\n",
  1115. __func__, ept->num, ept->in ? "in" : "out", status);
  1116. } else {
  1117. actual = req->length;
  1118. status = 0;
  1119. }
  1120. /* clean chache because CPU may prefetch request buffer to cache */
  1121. arch_clean_invalidate_cache_range((addr_t) req->buf, req->length);
  1122. if (req->complete)
  1123. req->complete(req, actual, status);
  1124. }
  1125. }
  1126. static void mt_udc_epx_handler(u8 ep_num, u8 dir)
  1127. {
  1128. u8 index;
  1129. u16 csr;
  1130. u32 count;
  1131. struct udc_endpoint *endpoint;
  1132. struct urb *urb;
  1133. struct udc_request *req; /* for event signaling */
  1134. u8 intrrxe;
  1135. endpoint = mt_find_ep(ep_num, dir);
  1136. if (endpoint == NULL) {
  1137. DBG_C("endpoint is NULL!\n");
  1138. return;
  1139. }
  1140. index = readb(INDEX);
  1141. writeb(ep_num, INDEX);
  1142. #if DBG_USB_GENERAL
  1143. DBG_I("EP%d Interrupt\n", ep_num);
  1144. DBG_I("dir: %x\n", dir);
  1145. #endif
  1146. switch (dir) {
  1147. case USB_DIR_OUT:
  1148. /* transfer direction is from host to device */
  1149. /* from the view of usb device, it's RX */
  1150. csr = readw(IECSR + RXCSR);
  1151. if (csr & EPX_RX_SENTSTALL) {
  1152. DBG_C("EP %d(RX): STALL\n", ep_num);
  1153. /* exception handling: implement this!! */
  1154. return;
  1155. }
  1156. #ifdef SUPPORT_QMU /* SUPPORT_QMU */
  1157. count = endpoint->rcv_urb->actual_length;
  1158. #ifdef DBG_USB_QMU
  1159. DBG_I("%s: QMU: count: %d\n", __func__, count);
  1160. #endif
  1161. #else /* PIO MODE */
  1162. if (!(csr & EPX_RX_RXPKTRDY)) {
  1163. #if DBG_USB_GENERAL
  1164. DBG_I("EP %d: ERRONEOUS INTERRUPT\n", ep_num); // normal
  1165. #endif
  1166. return;
  1167. }
  1168. //DBG_C("mt_read_fifo, start\n");
  1169. count = mt_read_fifo(endpoint);
  1170. //DBG_C("mt_read_fifo, end\n");
  1171. #if DBG_USB_GENERAL
  1172. DBG_I("EP%d(RX), count = %d\n", ep_num, count);
  1173. #endif
  1174. csr &= ~EPX_RX_RXPKTRDY;
  1175. writew(csr, IECSR + RXCSR);
  1176. if (readw(IECSR + RXCSR) & EPX_RX_RXPKTRDY) {
  1177. #if DBG_USB_GENERAL
  1178. DBG_I("%s: rxpktrdy clear failed\n", __func__);
  1179. #endif
  1180. }
  1181. #endif /* ifndef SUPPORT_QMU */
  1182. /* do signaling */
  1183. req = endpoint->req;
  1184. /* workaround: if req->lenth == 64 bytes (not huge data transmission)
  1185. * do normal return */
  1186. #if DBG_USB_GENERAL
  1187. DBG_I("%s: req->length: %x, endpoint->rcv_urb->actual_length: %x\n",
  1188. __func__, req->length, endpoint->rcv_urb->actual_length);
  1189. #endif
  1190. /* Deal with FASTBOOT command */
  1191. if ((req->length >= endpoint->rcv_urb->actual_length) && req->length == 64) {
  1192. req->length = count;
  1193. /* mask EPx INTRRXE */
  1194. /* The buffer is passed from the AP caller.
  1195. * It happens that AP is dealing with the buffer filled data by driver,
  1196. * but the driver is still receiving the next data packets onto the buffer.
  1197. * Data corrupted happens if the every request use the same buffer.
  1198. * Mask the EPx to ensure that AP and driver are not accessing the buffer parallely.
  1199. */
  1200. intrrxe = readb(INTRRXE);
  1201. writeb((intrrxe &= ~(1 << ep_num)), INTRRXE);
  1202. }
  1203. /* Deal with DATA transfer */
  1204. if ((req->length == endpoint->rcv_urb->actual_length) ||
  1205. ((req->length >= endpoint->rcv_urb->actual_length) && req->length == 64)) {
  1206. handle_ept_complete(endpoint);
  1207. /* mask EPx INTRRXE */
  1208. /* The buffer is passed from the AP caller.
  1209. * It happens that AP is dealing with the buffer filled data by driver,
  1210. * but the driver is still receiving the next data packets onto the buffer.
  1211. * Data corrupted happens if the every request use the same buffer.
  1212. * Mask the EPx to ensure that AP and driver are not accessing the buffer parallely.
  1213. */
  1214. intrrxe = readb(INTRRXE);
  1215. writeb((intrrxe &= ~(1 << ep_num)), INTRRXE);
  1216. }
  1217. break;
  1218. case USB_DIR_IN:
  1219. /* transfer direction is from device to host */
  1220. /* from the view of usb device, it's tx */
  1221. csr = readw(IECSR + TXCSR);
  1222. if (csr & EPX_TX_SENTSTALL) {
  1223. DBG_C("EP %d(TX): STALL\n", ep_num);
  1224. endpoint->status = -1;
  1225. handle_ept_complete(endpoint);
  1226. /* exception handling: implement this!! */
  1227. return;
  1228. }
  1229. #ifndef SUPPORT_QMU
  1230. if (csr & EPX_TX_TXPKTRDY) {
  1231. DBG_C
  1232. ("mt_udc_epx_handler: ep%d is not ready to be written\n",
  1233. ep_num);
  1234. return;
  1235. }
  1236. #endif
  1237. urb = endpoint->tx_urb;
  1238. if (endpoint->sent == urb->actual_length) {
  1239. /* do signaling */
  1240. handle_ept_complete(endpoint);
  1241. break;
  1242. }
  1243. /* send next packet of the same urb */
  1244. #ifndef SUPPORT_QMU
  1245. count = mt_write_fifo(endpoint);
  1246. #if DBG_USB_GENERAL
  1247. DBG_I("EP%d(TX), count = %d\n", ep_num, endpoint->sent);
  1248. #endif
  1249. if (count != 0) {
  1250. /* not the interrupt generated by the last tx packet of the transfer */
  1251. csr |= EPX_TX_TXPKTRDY;
  1252. writew(csr, IECSR + TXCSR);
  1253. }
  1254. #endif
  1255. break;
  1256. default:
  1257. break;
  1258. }
  1259. writeb(index, INDEX);
  1260. return;
  1261. }
  1262. void mt_udc_irq(u8 intrtx, u8 intrrx, u8 intrusb, u32 wQmuVal)
  1263. {
  1264. int i;
  1265. DBG_IRQ("[USB] INTERRUPT\n");
  1266. if (intrusb) {
  1267. if (intrusb & INTRUSB_RESUME) {
  1268. DBG_IRQ("[USB] INTRUSB: RESUME\n");
  1269. }
  1270. if (intrusb & INTRUSB_SESS_REQ) {
  1271. DBG_IRQ("[USB] INTRUSB: SESSION REQUEST\n");
  1272. }
  1273. if (intrusb & INTRUSB_VBUS_ERROR) {
  1274. DBG_IRQ("[USB] INTRUSB: VBUS ERROR\n");
  1275. }
  1276. if (intrusb & INTRUSB_SUSPEND) {
  1277. DBG_IRQ("[USB] INTRUSB: SUSPEND\n");
  1278. mt_udc_suspend();
  1279. }
  1280. if (intrusb & INTRUSB_CONN) {
  1281. DBG_IRQ("[USB] INTRUSB: CONNECT\n");
  1282. }
  1283. if (intrusb & INTRUSB_DISCON) {
  1284. DBG_IRQ("[USB] INTRUSB: DISCONNECT\n");
  1285. }
  1286. if (intrusb & INTRUSB_RESET) {
  1287. DBG_IRQ("[USB] INTRUSB: RESET\n");
  1288. mt_udc_reset();
  1289. }
  1290. if (intrusb & INTRUSB_SOF) {
  1291. DBG_IRQ("[USB] INTRUSB: SOF\n");
  1292. }
  1293. }
  1294. #ifdef SUPPORT_QMU
  1295. wQmuVal &= ~(DRV_Reg32(USB_QIMR));
  1296. if ((wQmuVal & DQMU_M_RX_DONE(1))||(wQmuVal & DQMU_M_TX_DONE(1))) {
  1297. qmu_done_interrupt(wQmuVal);
  1298. if (wQmuVal & DQMU_M_RX_DONE(1)) {
  1299. DBG_IRQ("[USB] DQMU_M_RX_DONE\n");
  1300. mt_udc_epx_handler(1, USB_DIR_OUT);
  1301. }
  1302. if (wQmuVal & DQMU_M_TX_DONE(1)) {
  1303. DBG_IRQ("[USB] DQMU_M_TX_DONE\n");
  1304. mt_udc_epx_handler(1, USB_DIR_IN);
  1305. }
  1306. }
  1307. if (wQmuVal) {
  1308. qmu_handler(wQmuVal);
  1309. }
  1310. #endif
  1311. /* endpoint 0 interrupt? */
  1312. if (intrtx & EPMASK (0)) {
  1313. mt_udc_ep0_handler();
  1314. intrtx &= ~0x1;
  1315. }
  1316. if (intrtx) {
  1317. for (i = 1; i < MT_EP_NUM; i++) {
  1318. if (intrtx & EPMASK (i)) {
  1319. mt_udc_epx_handler(i, USB_DIR_IN);
  1320. }
  1321. }
  1322. }
  1323. if (intrrx) {
  1324. for (i = 1; i < MT_EP_NUM; i++) {
  1325. if (intrrx & EPMASK (i)) {
  1326. mt_udc_epx_handler(i, USB_DIR_OUT);
  1327. }
  1328. }
  1329. }
  1330. }
  1331. void service_interrupts(void)
  1332. {
  1333. volatile u8 intrtx, intrrx, intrusb;
  1334. volatile u32 wQmuVal;
  1335. /* polling interrupt status for incoming interrupts and service it */
  1336. intrtx = readb(INTRTX) & readb(INTRTXE);
  1337. intrrx = readb(INTRRX) & readb(INTRRXE);
  1338. intrusb = readb(INTRUSB) & readb(INTRUSBE);
  1339. writeb(intrtx, INTRTX);
  1340. writeb(intrrx, INTRRX);
  1341. writeb(intrusb, INTRUSB);
  1342. intrusb &= ~INTRUSB_SOF;
  1343. wQmuVal = 0;
  1344. #ifdef SUPPORT_QMU
  1345. wQmuVal = DRV_Reg32(USB_QISAR);
  1346. if (wQmuVal) {
  1347. DRV_WriteReg32(USB_QISAR, wQmuVal);
  1348. }
  1349. #endif
  1350. if (intrtx | intrrx | intrusb | wQmuVal) {
  1351. mt_udc_irq(intrtx, intrrx, intrusb, wQmuVal);
  1352. }
  1353. }
  1354. void lk_usb_scheduler(void)
  1355. {
  1356. mt_irq_ack(MT_USB0_IRQ_ID);
  1357. service_interrupts();
  1358. #if 0
  1359. static enum handler_return ret;
  1360. ret = INT_RESCHEDULE;
  1361. if (ret == INT_RESCHEDULE) {
  1362. thread_preempt();
  1363. }
  1364. #endif
  1365. return;
  1366. }
  1367. int mt_usb_irq_init(void)
  1368. {
  1369. /* disable all endpoint interrupts */
  1370. writeb(0, INTRTXE);
  1371. writeb(0, INTRRXE);
  1372. writeb(0, INTRUSBE);
  1373. /* 2. Ack all gpt irq if needed */
  1374. //writel(0x3F, GPT_IRQ_ACK);
  1375. /* 3. Register usb irq */
  1376. mt_irq_set_sens(MT_USB0_IRQ_ID, MT65xx_LEVEL_SENSITIVE);
  1377. mt_irq_set_polarity(MT_USB0_IRQ_ID, MT65xx_POLARITY_LOW);
  1378. return 0;
  1379. }
  1380. /* Turn on the USB connection by enabling the pullup resistor */
  1381. void mt_usb_connect_internal(void)
  1382. {
  1383. u8 tmpReg8;
  1384. /* connect */
  1385. tmpReg8 = readb(POWER);
  1386. tmpReg8 |= PWR_SOFT_CONN;
  1387. tmpReg8 |= PWR_ENABLE_SUSPENDM;
  1388. #ifdef USB_FORCE_FULL_SPEED
  1389. tmpReg8 &= ~PWR_HS_ENAB;
  1390. #else
  1391. tmpReg8 |= PWR_HS_ENAB;
  1392. #endif
  1393. writeb(tmpReg8, POWER);
  1394. }
  1395. /* Turn off the USB connection by disabling the pullup resistor */
  1396. void mt_usb_disconnect_internal(void)
  1397. {
  1398. u8 tmpReg8;
  1399. /* connect */
  1400. tmpReg8 = readb(POWER);
  1401. tmpReg8 &= ~PWR_SOFT_CONN;
  1402. writeb(tmpReg8, POWER);
  1403. }
  1404. int udc_init(struct udc_device *dev)
  1405. {
  1406. struct udc_descriptor *desc = NULL;
  1407. #ifdef USB_GINTR
  1408. #ifdef USB_HSDMA_ISR
  1409. u32 usb_dmaintr;
  1410. #endif
  1411. u32 usb_l1intm;
  1412. #endif
  1413. DBG_I("[USB] %s:\n", __func__);
  1414. DBG_I("[USB] ep0_urb: %p\n", ep0_urb);
  1415. /* RESET */
  1416. /*
  1417. * mt_usb_disconnect_internal can be removed:
  1418. * if we call udc_stop() in cmd_reboot and cmd_reboot_bootloader.
  1419. */
  1420. //mt_usb_disconnect_internal();
  1421. thread_sleep(20);
  1422. #if 0 /* Wait for HW designer's instruction (depends on tapeout process) */
  1423. //#if !CFG_FPGA_PLATFORM
  1424. //ALPS00427972, implement the analog register formula
  1425. //Set the calibration after power on
  1426. //Add here for eFuse, chip version checking -> analog register calibration
  1427. int input_reg = INREG16(M_HW_RES3);
  1428. #if DBG_PHY_CALIBRATION
  1429. DBG_I("%s: input_reg = 0x%x \n", __func__, input_reg);
  1430. #endif
  1431. int term_vref = (input_reg & RG_USB20_TERM_VREF_SEL_MASK) >> 13; //0xE000 //0b 1110,0000,0000,0000 15~13
  1432. int clkref = (input_reg & RG_USB20_CLKREF_REF_MASK) >> 10; //0x1C00 //0b 0001,1100,0000,0000 12~10
  1433. int vrt_vref = (input_reg & RG_USB20_VRT_VREF_SEL_MASK) >> 7; //0x0380 //0b 0000,0011,1000,0000 9~7
  1434. #if DBG_PHY_CALIBRATION
  1435. DBG_I("%s: term_vref = 0x%x, clkref = 0x%x, vrt_vref = 0x%x,\n", __func__, term_vref, clkref, vrt_vref);
  1436. #endif
  1437. if (term_vref)
  1438. mt_usb_phy_calibraion(1, term_vref);
  1439. if (clkref)
  1440. mt_usb_phy_calibraion(2, clkref);
  1441. if (vrt_vref)
  1442. mt_usb_phy_calibraion(3, vrt_vref);
  1443. //ALPS00427972, implement the analog register formula
  1444. //#endif
  1445. #endif
  1446. #if 0
  1447. if (get_devinfo_with_index(14) & (0x01<<22)) {
  1448. USBPHY_CLR8(0x00, 0x20);
  1449. }
  1450. if (get_devinfo_with_index(14) & (0x07<<19)) {
  1451. //RG_USB20_VRT_VREF_SEL[2:0]=5 (ori:4) (0x11110804[14:12])
  1452. USBPHY_CLR8(0x05, 0x70);
  1453. USBPHY_SET8(0x05, ((get_devinfo_with_index(7)>>19)<<4) & (0x70));
  1454. }
  1455. #endif
  1456. /* usb phy init */
  1457. board_usb_init();
  1458. mt_usb_phy_recover();
  1459. thread_sleep(20);
  1460. /* allocate ep0 */
  1461. ep0out = _udc_endpoint_alloc(EP0, 0, EP0_MAX_PACKET_SIZE);
  1462. ep0in = _udc_endpoint_alloc(EP0, 1, EP0_MAX_PACKET_SIZE);
  1463. ep0req = udc_request_alloc();
  1464. ep0req->buf = malloc(EP0_BUF_SZ);
  1465. if (!ep0req->buf)
  1466. return -ENOMEM;
  1467. ep0_urb->buf = malloc(EP0_BUF_SZ);
  1468. if (!ep0_urb->buf)
  1469. return -ENOMEM;
  1470. {
  1471. /* create and register a language table descriptor */
  1472. /* language 0x0409 is US English */
  1473. desc = udc_descriptor_alloc(TYPE_STRING, EP0, 4);
  1474. if (desc == NULL) {
  1475. DBG_I("[USB] desc is NULL!");
  1476. return -1;
  1477. }
  1478. desc->data[2] = 0x09;
  1479. desc->data[3] = 0x04;
  1480. udc_descriptor_register(desc);
  1481. }
  1482. #ifdef USB_HSDMA_ISR
  1483. /* setting HSDMA interrupt register */
  1484. usb_dmaintr = (0xff | 0xff << USB_DMA_INTR_UNMASK_SET_OFFSET);
  1485. writel(usb_dmaintr, USB_DMA_INTR);
  1486. #endif
  1487. #ifdef USB_GINTR
  1488. #ifdef SUPPORT_QMU
  1489. usb_l1intm = (TX_INT_STATUS | RX_INT_STATUS | USBCOM_INT_STATUS | DMA_INT_STATUS | QINT_STATUS);
  1490. writel(usb_l1intm, USB_L1INTM);
  1491. #else
  1492. usb_l1intm = (TX_INT_STATUS | RX_INT_STATUS | USBCOM_INT_STATUS | DMA_INT_STATUS);
  1493. writel(usb_l1intm, USB_L1INTM);
  1494. #endif
  1495. #endif
  1496. the_device = dev;
  1497. return 0;
  1498. }
  1499. void udc_endpoint_free(struct udc_endpoint *ept)
  1500. {
  1501. /* todo */
  1502. }
  1503. struct udc_request *udc_request_alloc(void)
  1504. {
  1505. struct udc_request *req;
  1506. req = malloc(sizeof(*req));
  1507. if (!req)
  1508. panic("%s cannot allocate memory!\n", __func__);
  1509. req->buf = NULL;
  1510. req->length = 0;
  1511. return req;
  1512. }
  1513. void udc_request_free(struct udc_request *req)
  1514. {
  1515. free(req);
  1516. }
  1517. /* Called to start packet transmission. */
  1518. /* It must be applied in udc_request_queue when polling mode is used.
  1519. * (When USB_GINTR is undefined).
  1520. * If interrupt mode is used, you can use
  1521. * mt_udc_epx_handler(ept->num, USB_DIR_IN); to replace mt_ep_write make ISR
  1522. * do it for you.
  1523. */
  1524. static int mt_ep_write(struct udc_endpoint *endpoint)
  1525. {
  1526. int ep_num = endpoint->num;
  1527. int count;
  1528. u8 index;
  1529. u16 csr;
  1530. index = readb(INDEX);
  1531. writeb(ep_num, INDEX);
  1532. /* udc_endpoint_write: cannot write ep0 */
  1533. if (ep_num == 0)
  1534. return FALSE;
  1535. /* udc_endpoint_write: cannot write USB_DIR_OUT */
  1536. if (endpoint->in == 0)
  1537. return FALSE;
  1538. csr = readw(IECSR + TXCSR);
  1539. if (csr & EPX_TX_TXPKTRDY) {
  1540. #if DBG_USB_GENERAL
  1541. DBG_I("[USB]: udc_endpoint_write: ep%d is not ready to be written\n",
  1542. ep_num);
  1543. #endif
  1544. return FALSE;
  1545. }
  1546. count = mt_write_fifo(endpoint);
  1547. csr |= EPX_TX_TXPKTRDY;
  1548. writew(csr, IECSR + TXCSR);
  1549. writeb(index, INDEX);
  1550. return count;
  1551. }
  1552. int udc_request_queue(struct udc_endpoint *ept, struct udc_request *req)
  1553. {
  1554. #ifdef SUPPORT_QMU
  1555. u8 *pbuf;
  1556. #else
  1557. u8 intrrxe;
  1558. #endif
  1559. #ifdef SUPPORT_QMU
  1560. /* don't dump debug message here, will cause ISR fail */
  1561. /* work around for abnormal disconnect line during qmu transfer */
  1562. if (!usb_online)
  1563. return 0;
  1564. #endif
  1565. #if DBG_USB_GENERAL
  1566. DBG_I("[USB] %s: ept%d %s queue req=%p, req->length=%x\n",
  1567. __func__, ept->num, ept->in ? "in" : "out", req, req->length);
  1568. DBG_I("[USB] %s: ept%d: %x, ept->in: %s, ept->rcv_urb->buf: %x, req->buf: %x\n",
  1569. __func__, ept->num, ept, ept->in ? "IN" : "OUT" , ept->rcv_urb->buf, req->buf);
  1570. #endif
  1571. enter_critical_section();
  1572. ept->req = req;
  1573. ept->status = 0; /* ACTIVE */
  1574. ept->sent = 0;
  1575. ept->last = 0;
  1576. /* read */
  1577. if (!ept->in) {
  1578. ept->rcv_urb->buf = req->buf;
  1579. ept->rcv_urb->actual_length = 0;
  1580. /* unmask EPx INTRRXE */
  1581. /*
  1582. * To avoid the parallely access the buffer,
  1583. * it is umasked here and umask at complete.
  1584. */
  1585. #ifdef SUPPORT_QMU /* For QMU, don't enable EP interrupts. */
  1586. pbuf = ept->rcv_urb->buf;
  1587. /* FASTBOOT COMMAND */
  1588. if (req->length <= GPD_BUF_SIZE_ALIGN) {
  1589. mu3d_hal_insert_transfer_gpd(ept->num, USB_DIR_OUT, pbuf, req->length, true, true, false, (ept->type == USB_EP_XFER_ISO ? 0 : 1), ept->maxpkt);
  1590. } else { /* FASTBOOT DATA */
  1591. DBG_C("udc_request exceeded the maximum QMU buffer size GPD_BUF_SIZE_ALIGN\n");
  1592. }
  1593. /* start transfer */
  1594. arch_clean_invalidate_cache_range((addr_t) ept->rcv_urb->buf, req->length);
  1595. mu3d_hal_resume_qmu(ept->num, USB_DIR_OUT);
  1596. #else /* For PIO, enable EP interrupts */
  1597. intrrxe = readb(INTRRXE);
  1598. intrrxe |= (1 << ept->num);
  1599. writeb(intrrxe, INTRRXE);
  1600. #endif
  1601. }
  1602. /* write */
  1603. if (ept->in) {
  1604. ept->tx_urb->buf = req->buf;
  1605. ept->tx_urb->actual_length = req->length;
  1606. #ifdef SUPPORT_QMU /* For QMU, we don't call mt_ep_write() */
  1607. mu3d_hal_insert_transfer_gpd(ept->num, USB_DIR_IN, ept->tx_urb->buf, req->length, true, true, false, (ept->type == USB_EP_XFER_ISO ? 0 : 1), ept->maxpkt);
  1608. arch_clean_invalidate_cache_range((addr_t) ept->tx_urb->buf, req->length);
  1609. mu3d_hal_resume_qmu(ept->num, USB_DIR_IN);
  1610. #else /* For PIO, call mt_ep_write() */
  1611. mt_ep_write(ept);
  1612. #endif
  1613. }
  1614. exit_critical_section();
  1615. return 0;
  1616. }
  1617. #if 0
  1618. enum handler_return udc_interrupt(void *arg)
  1619. {
  1620. struct udc_endpoint *ept;
  1621. unsigned ret;
  1622. return ret;
  1623. }
  1624. #endif
  1625. int udc_register_gadget(struct udc_gadget *gadget)
  1626. {
  1627. if (the_gadget) {
  1628. DBG_C("only one gadget supported\n");
  1629. return FALSE;
  1630. }
  1631. the_gadget = gadget;
  1632. return 0;
  1633. }
  1634. static void udc_ept_desc_fill(struct udc_endpoint *ept, unsigned char *data)
  1635. {
  1636. data[0] = 7;
  1637. data[1] = TYPE_ENDPOINT;
  1638. data[2] = ept->num | (ept->in ? USB_DIR_IN : USB_DIR_OUT);
  1639. data[3] = 0x02; /* bulk -- the only kind we support */
  1640. data[4] = ept->maxpkt;
  1641. data[5] = ept->maxpkt >> 8;
  1642. data[6] = ept->in ? 0x00 : 0x01;
  1643. }
  1644. static unsigned udc_ifc_desc_size(struct udc_gadget *g)
  1645. {
  1646. return 9 + g->ifc_endpoints * 7;
  1647. }
  1648. static void udc_ifc_desc_fill(struct udc_gadget *g, unsigned char *data)
  1649. {
  1650. unsigned n;
  1651. data[0] = 0x09;
  1652. data[1] = TYPE_INTERFACE;
  1653. data[2] = 0x00; /* ifc number */
  1654. data[3] = 0x00; /* alt number */
  1655. data[4] = g->ifc_endpoints;
  1656. data[5] = g->ifc_class;
  1657. data[6] = g->ifc_subclass;
  1658. data[7] = g->ifc_protocol;
  1659. data[8] = udc_string_desc_alloc(g->ifc_string);
  1660. data += 9;
  1661. for (n = 0; n < g->ifc_endpoints; n++) {
  1662. udc_ept_desc_fill(g->ept[n], data);
  1663. data += 7;
  1664. }
  1665. }
  1666. int udc_start(void)
  1667. {
  1668. struct udc_descriptor *desc;
  1669. unsigned char *data;
  1670. unsigned size;
  1671. DBG_C("[USB] %s\n", __func__);
  1672. if (!the_device) {
  1673. DBG_C("udc cannot start before init\n");
  1674. return FALSE;
  1675. }
  1676. if (!the_gadget) {
  1677. DBG_C("udc has no gadget registered\n");
  1678. return FALSE;
  1679. }
  1680. /* create our device descriptor */
  1681. desc = udc_descriptor_alloc(TYPE_DEVICE, EP0, 18);
  1682. if (desc == NULL) {
  1683. DBG_C("desc is NULL!\n");
  1684. return FALSE;
  1685. }
  1686. data = desc->data;
  1687. data[2] = 0x00; /* usb spec minor rev */
  1688. data[3] = 0x02; /* usb spec major rev */
  1689. data[4] = 0x00; /* class */
  1690. data[5] = 0x00; /* subclass */
  1691. data[6] = 0x00; /* protocol */
  1692. data[7] = 0x40; /* max packet size on ept 0 */
  1693. memcpy(data + 8, &the_device->vendor_id, sizeof(short));
  1694. memcpy(data + 10, &the_device->product_id, sizeof(short));
  1695. memcpy(data + 12, &the_device->version_id, sizeof(short));
  1696. data[14] = udc_string_desc_alloc(the_device->manufacturer);
  1697. data[15] = udc_string_desc_alloc(the_device->product);
  1698. data[16] = udc_string_desc_alloc(the_device->serialno);
  1699. data[17] = 1; /* number of configurations */
  1700. udc_descriptor_register(desc);
  1701. /* create our configuration descriptor */
  1702. size = 9 + udc_ifc_desc_size(the_gadget);
  1703. desc = udc_descriptor_alloc(TYPE_CONFIGURATION, EP0, size);
  1704. if (desc == NULL) {
  1705. DBG_C("desc is NULL!\n");
  1706. return FALSE;
  1707. }
  1708. data = desc->data;
  1709. data[0] = 0x09;
  1710. data[2] = size;
  1711. data[3] = size >> 8;
  1712. data[4] = 0x01; /* number of interfaces */
  1713. data[5] = 0x01; /* configuration value */
  1714. data[6] = 0x00; /* configuration string */
  1715. data[7] = 0x80; /* attributes */
  1716. data[8] = 0x80; /* max power (250ma) -- todo fix this */
  1717. udc_ifc_desc_fill(the_gadget, data + 9);
  1718. udc_descriptor_register(desc);
  1719. #if DBG_USB_DUMP_DESC
  1720. DBG_I("%s: dump desc_list\n", __func__);
  1721. for (desc = desc_list; desc; desc = desc->next) {
  1722. DBG_I("tag: %04x\n", desc->tag);
  1723. DBG_I("len: %d\n", desc->len);
  1724. DBG_I("data:");
  1725. hexdump8(desc->data, desc->len);
  1726. }
  1727. #endif
  1728. /* register interrupt handler */
  1729. mt_usb_irq_init();
  1730. /* go to RUN mode */
  1731. mt_usb_phy_recover();
  1732. /* clear INTRTX, INTRRX and INTRUSB */
  1733. writew(0xffff, INTRTX); /* writew */
  1734. writew(0xffff, INTRRX); /* writew */
  1735. writeb(0xff, INTRUSB); /* writeb */
  1736. #ifdef SUPPORT_QMU
  1737. usb_qmu_init_and_start();
  1738. /* set DMA channel 0 burst mode to boost QMU speed */
  1739. writew((readw(USB_DMA_CNTL(1)) | 0x600), USB_DMA_CNTL(1));
  1740. #endif
  1741. /* unmask usb irq */
  1742. #ifdef USB_GINTR
  1743. mt_irq_unmask(MT_USB0_IRQ_ID);
  1744. #endif
  1745. writeb((INTRUSB_SUSPEND | INTRUSB_RESUME | INTRUSB_RESET |INTRUSB_DISCON), INTRUSBE);
  1746. /* enable the pullup resistor */
  1747. mt_usb_connect_internal();
  1748. while (1) {
  1749. #ifdef USB_GINTR
  1750. thread_sleep(1);
  1751. #else
  1752. service_interrupts();
  1753. #endif
  1754. }
  1755. return 0;
  1756. }
  1757. int udc_stop(void)
  1758. {
  1759. thread_sleep(10);
  1760. mt_usb_disconnect_internal();
  1761. mt_usb_phy_savecurrent();
  1762. return 0;
  1763. }
  1764. #ifdef SUPPORT_QMU
  1765. void usb_ep_disable_interrupt(u8 ep_num, u8 dir)
  1766. {
  1767. unsigned int wCsr = 0;
  1768. unsigned int intr_e = 0;
  1769. writeb(ep_num, INDEX);
  1770. if(dir == USB_DIR_OUT){
  1771. wCsr |= EPX_RX_DMAREQEN;
  1772. writew(wCsr, IECSR + RXCSR);
  1773. //turn off intrRx
  1774. intr_e = readb(INTRRXE);
  1775. intr_e = intr_e & (~(1<<(1)));
  1776. writeb(intr_e, INTRRXE);
  1777. } else {
  1778. wCsr |= EPX_TX_DMAREQEN;
  1779. writew(wCsr, IECSR + TXCSR);
  1780. //turn off intrTx
  1781. intr_e = readb(INTRTXE);
  1782. intr_e = intr_e & (~(1<<(1)));
  1783. writeb(intr_e, INTRTXE);
  1784. }
  1785. }
  1786. void usb_qmu_init(void)
  1787. {
  1788. mu3d_hal_alloc_qmu_mem();
  1789. mu3d_hal_init_qmu();
  1790. gpd_buf_size = GPD_BUF_SIZE_ALIGN; //max allowable data buffer length. Don't care when linking with BD.
  1791. g_dma_buffer_size = STRESS_DATA_LENGTH * MAX_GPD_NUM;
  1792. }
  1793. void usb_qmu_init_and_start(void)
  1794. {
  1795. u32 i;
  1796. usb_qmu_init();
  1797. for (i = 1; i <= MAX_QMU_EP; i++) {
  1798. usb_start_qmu(1, USB_DIR_OUT);
  1799. usb_start_qmu(1, USB_DIR_IN);
  1800. }
  1801. for (i = 1; i <= MAX_QMU_EP; i++) {
  1802. usb_ep_disable_interrupt(i, USB_DIR_OUT);
  1803. usb_ep_disable_interrupt(i, USB_DIR_IN);
  1804. }
  1805. }
  1806. /*
  1807. * qmu_handler - handle qmu error events
  1808. * @args - arg1: ep number
  1809. */
  1810. void qmu_handler(u32 qmu_val)
  1811. {
  1812. u32 wRetVal;
  1813. int i;
  1814. wRetVal= 0;
  1815. //RXQ ERROR
  1816. if (qmu_val & DQMU_M_RXQ_ERR)
  1817. {
  1818. wRetVal = MGC_ReadQIRQ32(MGC_O_QIRQ_RQEIR) & (~(MGC_ReadQIRQ32(MGC_O_QIRQ_RQEIMR)));
  1819. DBG_I("Rx Queue error in QMU mode![0x%x]\n", wRetVal);
  1820. for (i=1; i<=RXQ_NUM; i++)
  1821. {
  1822. if (wRetVal & DQMU_M_RX_GPDCS_ERR(i))
  1823. {
  1824. DBG_I("Rx %d GPD checksum error!\n", i);
  1825. }
  1826. if (wRetVal & DQMU_M_RX_LEN_ERR(i))
  1827. {
  1828. DBG_I("Rx %d recieve length error!\n", i);
  1829. }
  1830. if (wRetVal & DQMU_M_RX_ZLP_ERR(i))
  1831. {
  1832. DBG_I("Rx %d recieve an zlp packet!\n", i);
  1833. }
  1834. }
  1835. MGC_WriteQIRQ32(MGC_O_QIRQ_RQEIR, wRetVal);
  1836. }
  1837. //TXQ ERROR
  1838. if (qmu_val & DQMU_M_TXQ_ERR)
  1839. {
  1840. wRetVal = MGC_ReadQIRQ32(MGC_O_QIRQ_TQEIR) & (~(MGC_ReadQIRQ32(MGC_O_QIRQ_TQEIMR)));
  1841. DBG_I("Tx Queue error in QMU mode![0x%x]\n", wRetVal);
  1842. for (i=1; i<=TXQ_NUM; i++)
  1843. {
  1844. if (wRetVal & DQMU_M_TX_BDCS_ERR(i))
  1845. {
  1846. DBG_I("Tx %d BD checksum error!\n", i);
  1847. }
  1848. if (wRetVal & DQMU_M_TX_GPDCS_ERR(i))
  1849. {
  1850. DBG_I("Tx %d GPD checksum error!\n", i);
  1851. }
  1852. if (wRetVal & DQMU_M_TX_LEN_ERR(i))
  1853. {
  1854. DBG_I("Tx %d buffer length error!\n", i);
  1855. }
  1856. }
  1857. MGC_WriteQIRQ32(MGC_O_QIRQ_TQEIR, wRetVal);
  1858. }
  1859. //RX EP ERROR
  1860. if (qmu_val & DQMU_M_RXEP_ERR)
  1861. {
  1862. wRetVal = MGC_ReadQIRQ32(MGC_O_QIRQ_REPEIR) & (~(MGC_ReadQIRQ32(MGC_O_QIRQ_REPEIMR)));
  1863. DBG_I("Rx endpoint error in QMU mode![0x%x]\n", wRetVal);
  1864. for (i=1; i<=RXQ_NUM; i++)
  1865. {
  1866. if (wRetVal & DQMU_M_RX_EP_ERR(i))
  1867. {
  1868. DBG_I("Rx %d Ep Error!\n", i);
  1869. }
  1870. }
  1871. MGC_WriteQIRQ32(MGC_O_QIRQ_REPEIR, wRetVal);
  1872. }
  1873. //TX EP ERROR
  1874. if(qmu_val & DQMU_M_TXEP_ERR)
  1875. {
  1876. wRetVal = MGC_ReadQIRQ32(MGC_O_QIRQ_TEPEIR)& (~(MGC_ReadQIRQ32(MGC_O_QIRQ_TEPEIMR)));
  1877. DBG_I("Tx endpoint error in QMU mode![0x%x]\n", wRetVal);
  1878. for (i=1; i<=TXQ_NUM; i++){
  1879. if (wRetVal &DQMU_M_TX_EP_ERR(i))
  1880. {
  1881. DBG_I("Tx %d Ep Error!\n", i);
  1882. }
  1883. }
  1884. MGC_WriteQIRQ32(MGC_O_QIRQ_TEPEIR, wRetVal);
  1885. }
  1886. //RXQ EMPTY
  1887. if (qmu_val & DQMU_M_RQ_EMPTY)
  1888. {
  1889. wRetVal = MGC_ReadQIRQ32(MGC_O_QIRQ_REPEMPR) & (~(MGC_ReadQIRQ32(MGC_O_QIRQ_REPEMPMR)));
  1890. DBG_I("Rx Empty in QMU mode![0x%x]\n", wRetVal);
  1891. for (i=1; i<=RXQ_NUM; i++)
  1892. {
  1893. if (wRetVal & DQMU_M_RX_EMPTY(i))
  1894. {
  1895. DBG_I("Rx %d Empty!\n", i);
  1896. }
  1897. }
  1898. MGC_WriteQIRQ32(MGC_O_QIRQ_REPEMPR, wRetVal);
  1899. }
  1900. //TXQ EMPTY
  1901. if (qmu_val & DQMU_M_TQ_EMPTY)
  1902. {
  1903. wRetVal = MGC_ReadQIRQ32(MGC_O_QIRQ_TEPEMPR) & (~(MGC_ReadQIRQ32(MGC_O_QIRQ_TEPEMPMR)));
  1904. DBG_I("Tx Empty in QMU mode![0x%x]\n", wRetVal);
  1905. for (i=1; i<=TXQ_NUM; i++)
  1906. {
  1907. if (wRetVal & DQMU_M_TX_EMPTY(i))
  1908. {
  1909. DBG_I("Tx %d Empty!\n", i);
  1910. }
  1911. }
  1912. MGC_WriteQIRQ32(MGC_O_QIRQ_TEPEMPR, wRetVal);
  1913. }
  1914. }
  1915. /*
  1916. * 1. Find the last gpd HW has executed and update tx_gpd_last[]
  1917. * 2. Set the flag for txstate to know that TX has been completed
  1918. * ported from proc_qmu_tx() from test driver.
  1919. * caller:qmu_interrupt after getting QMU done interrupt and TX is raised
  1920. */
  1921. void qmu_tx_interrupt(u8 ep_num)
  1922. {
  1923. struct tgpd *gpd = tx_gpd_last[ep_num];
  1924. struct tgpd *gpd_current = (struct tgpd*)MGC_ReadQMU32(MGC_O_QMU_TQCPR(ep_num));
  1925. struct udc_endpoint *ept;
  1926. ept = mt_find_ep(ep_num, USB_DIR_IN);
  1927. if (ept == NULL) {
  1928. DBG_I("ept is NULL!\n");
  1929. return;
  1930. }
  1931. #ifdef DBG_USB_QMU
  1932. DBG_I("tx_gpd_last 0x%x, gpd_current 0x%x, gpd_end 0x%x, \n", (u32)gpd, (u32)gpd_current, (u32)tx_gpd_end[ep_num]);
  1933. #endif
  1934. if (gpd == gpd_current) {//gpd_current should at least point to the next GPD to the previous last one.
  1935. DBG_I("should not happen: %s %d\n", __func__, __LINE__);
  1936. return;
  1937. }
  1938. arch_clean_invalidate_cache_range((addr_t) gpd, sizeof(struct tgpd));
  1939. while (gpd != gpd_current && !TGPD_IS_FLAGS_HWO(gpd)) {
  1940. arch_clean_invalidate_cache_range((addr_t) gpd, sizeof(struct tgpd));
  1941. #ifdef DBG_USB_QMU
  1942. DBG_I("Tx gpd %x info { HWO %d, BPD %d, Next_GPD %x , DataBuffer %x, BufferLen %d, Endpoint %d}\n",
  1943. (u32)gpd, (u32)TGPD_GET_FLAG(gpd), (u32)TGPD_GET_FORMAT(gpd), (u32)TGPD_GET_NEXT(gpd),
  1944. (u32)TGPD_GET_DATA(gpd), (u32)TGPD_GET_BUF_LEN(gpd), (u32)TGPD_GET_EPaddr(gpd));
  1945. #endif
  1946. /* required for mt_udc_epx_handler */
  1947. ept->sent = (u32)TGPD_GET_BUF_LEN(gpd);
  1948. gpd = TGPD_GET_NEXT(gpd);
  1949. tx_gpd_last[ep_num] = gpd;
  1950. }
  1951. #ifdef DBG_USB_QMU
  1952. DBG_I("tx_gpd_last[%d]: 0x%x\n", ep_num, (u32)tx_gpd_last[ep_num]);
  1953. DBG_I("tx_gpd_end[%d]: 0x%x\n", ep_num, (u32)tx_gpd_end[ep_num]);
  1954. DBG_I("Tx %d complete\n", ep_num);
  1955. #endif
  1956. return;
  1957. }
  1958. /*
  1959. * When receiving RXQ done interrupt, qmu_interrupt calls this function.
  1960. *
  1961. * 1. Traverse GPD/BD data structures to count actual transferred length.
  1962. * 2. Set the done flag to notify rxstate_qmu() to report status to upper gadget driver.
  1963. *
  1964. * ported from proc_qmu_rx() from test driver.
  1965. * caller:qmu_interrupt after getting QMU done interrupt and TX is raised
  1966. */
  1967. void qmu_rx_interrupt(u8 ep_num)
  1968. {
  1969. u32 recivedlength = 0;
  1970. struct tgpd *gpd = (struct tgpd*) rx_gpd_last[ep_num];
  1971. struct tgpd *gpd_current = (struct tgpd*)MGC_ReadQMU32(MGC_O_QMU_RQCPR(ep_num));
  1972. struct tbd *bd;
  1973. struct udc_endpoint *ept;
  1974. struct urb *current_urb;
  1975. ept = mt_find_ep(ep_num, USB_DIR_OUT);
  1976. if (ept == NULL) {
  1977. DBG_I("ept is NULL!\n");
  1978. return;
  1979. }
  1980. current_urb = ept->rcv_urb;
  1981. #ifdef DBG_USB_QMU
  1982. DBG_I("ep_num: %d ,Rx_gpd_last: 0x%x, gpd_current: 0x%x, gpd_end: 0x%x \n",ep_num,(u32)gpd, (u32)gpd_current, (u32)rx_gpd_end[ep_num]);
  1983. #endif
  1984. if (gpd == gpd_current) {
  1985. DBG_I("should not happen: %s %d\n", __func__, __LINE__);
  1986. return;
  1987. }
  1988. arch_clean_invalidate_cache_range((addr_t) gpd, sizeof(struct tgpd));
  1989. while (gpd != gpd_current && !TGPD_IS_FLAGS_HWO(gpd)) {
  1990. arch_clean_invalidate_cache_range((addr_t) gpd, sizeof(struct tgpd));
  1991. if (TGPD_IS_FORMAT_BDP(gpd)) {
  1992. bd = (struct tbd *)TGPD_GET_DATA(gpd);
  1993. while (1) {
  1994. #ifdef DBG_USB_QMU
  1995. DBG_I("BD: 0x%x\n", (u32)bd);
  1996. DBG_I("Buf Len: 0x%x\n", (u32)TBD_GET_BUF_LEN(bd));
  1997. #endif
  1998. //req->transferCount += TBD_GET_BUF_LEN(bd);
  1999. //ept->qmu_done_length += TBD_GET_BUF_LEN(bd);
  2000. current_urb->actual_length += TBD_GET_BUF_LEN(bd);
  2001. //DBG_I("Total Len : 0x%x\n",ept->qmu_done_length);
  2002. if (TBD_IS_FLAGS_EOL(bd)) {
  2003. break;
  2004. }
  2005. bd = TBD_GET_NEXT(bd);
  2006. }
  2007. } else {
  2008. recivedlength = (u32)TGPD_GET_BUF_LEN(gpd);
  2009. /* required for mt_udc_epx_handler */
  2010. current_urb->actual_length += recivedlength;
  2011. }
  2012. #ifdef DBG_USB_QMU
  2013. DBG_I("Rx gpd info { HWO %d, Next_GPD %x ,DataBufferLength %d, DataBuffer %x, Recived Len %d, Endpoint %d, TGL %d, ZLP %d}\n",
  2014. (u32)TGPD_GET_FLAG(gpd), (u32)TGPD_GET_NEXT(gpd),
  2015. (u32)TGPD_GET_DATABUF_LEN(gpd), (u32)TGPD_GET_DATA(gpd),
  2016. (u32)TGPD_GET_BUF_LEN(gpd), (u32)TGPD_GET_EPaddr(gpd),
  2017. (u32)TGPD_GET_TGL(gpd), (u32)TGPD_GET_ZLP(gpd));
  2018. #endif
  2019. gpd = TGPD_GET_NEXT(gpd);
  2020. rx_gpd_last[ep_num] = gpd;
  2021. }
  2022. #ifdef DBG_USB_QMU
  2023. DBG_I("rx_gpd_last[%d]: 0x%x\n", ep_num, (u32)rx_gpd_last[ep_num]);
  2024. DBG_I("rx_gpd_end[%d]: 0x%x\n", ep_num, (u32)rx_gpd_end[ep_num]);
  2025. #endif
  2026. }
  2027. void qmu_done_interrupt(u32 qmu_val)
  2028. {
  2029. int i;
  2030. #ifdef DBG_USB_QMU
  2031. DBG_I("[USB][QMU] qmu_interrupt\n");
  2032. #endif
  2033. for (i = 1; i <= MAX_QMU_EP; i++) {
  2034. if (qmu_val & DQMU_M_RX_DONE(i)) {
  2035. qmu_rx_interrupt(i);
  2036. }
  2037. if (qmu_val & DQMU_M_TX_DONE(i)) {
  2038. qmu_tx_interrupt(i);
  2039. }
  2040. }
  2041. }
  2042. #endif