mt_musb.c 55 KB

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