mt_ssusb.c 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900
  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/mt_irq.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 <mt_gic.h>
  35. #include <dev/udc.h>
  36. #include <mt_ssusb.h>
  37. #include <mt_ssusb_sifslv_ippc.h>
  38. #include <mt_ssusb_usb3_mac_csr.h>
  39. #ifdef SUPPORT_QMU
  40. #include <mt_ssusb_qmu.h>
  41. #include <mt_mu3d_hal_qmu_drv.h>
  42. #endif
  43. #define USB_GINTR
  44. /* DEBUG INFO Sections */
  45. #ifdef USB_DEBUG
  46. #define DBG_USB_DUMP_DESC 0
  47. #define DBG_USB_DUMP_DATA 0
  48. #define DBG_USB_DUMP_SETUP 1
  49. #define DBG_USB_FIFO 0
  50. #define DBG_USB_GENERAL 1
  51. #define DBG_USB_IRQ 0
  52. #define DBG_PHY_CALIBRATION 0
  53. #endif
  54. #define DBG_C(x...) dprintf(CRITICAL, "[USB] " x)
  55. #define DBG_I(x...) dprintf(INFO, "[USB] " x)
  56. #define DBG_S(x...) dprintf(SPEW, "[USB] " x)
  57. #if DBG_USB_GENERAL
  58. #define DBG_IRQ(x...) dprintf(INFO, x)
  59. #else
  60. #define DBG_IRQ(x...) do{} while(0)
  61. #endif
  62. /* bits used in all the endpoint status registers */
  63. #define EPT_TX(n) (1 << ((n) + 16))
  64. #define EPT_RX(n) (1 << (n))
  65. /* udc.h wrapper for usbdcore */
  66. static unsigned char usb_config_value = 0;
  67. EP0_STATE ep0_state = EP0_IDLE;
  68. int set_address = 0;
  69. u8 g_qmu_init = 0;
  70. #define EP0 0
  71. /* USB transfer directions */
  72. #define USB_DIR_IN DEVICE_WRITE /* val: 0x80 */
  73. #define USB_DIR_OUT DEVICE_READ /* val: 0x00 */
  74. /* Request types */
  75. #define USB_TYPE_STANDARD (0x00 << 5)
  76. #define USB_TYPE_CLASS (0x01 << 5)
  77. #define USB_TYPE_VENDOR (0x02 << 5)
  78. #define USB_TYPE_RESERVED (0x03 << 5)
  79. /* values used in GET_STATUS requests */
  80. #define USB_STAT_SELFPOWERED 0x01
  81. /* USB recipients */
  82. #define USB_RECIP_DEVICE 0x00
  83. #define USB_RECIP_INTERFACE 0x01
  84. #define USB_RECIP_ENDPOINT 0x02
  85. #define USB_RECIP_OTHER 0x03
  86. /* Endpoints */
  87. #define USB_EP_NUM_MASK 0x0f /* in bEndpointAddress */
  88. #define USB_EP_DIR_MASK 0x80
  89. #define USB_TYPE_MASK 0x60
  90. #define USB_RECIP_MASK 0x1f
  91. #if CFG_FPGA_PLATFORM
  92. /* for usb phy */
  93. #include <platform/mt_i2c.h>
  94. #endif
  95. #define URB_BUF_SIZE 512
  96. /* function prototype */
  97. /* usbphy.c */
  98. extern void mt_usb_phy_recover(void);
  99. extern void mt_usb_phy_savecurrent(void);
  100. extern void mt_usb_phy_poweron(void);
  101. static struct udc_endpoint *ep0in, *ep0out;
  102. static struct udc_request *ep0req;
  103. static unsigned char ep0_buf[4096] __attribute__((aligned(32)));
  104. struct urb mt_ep0_urb;
  105. struct urb mt_tx_urb;
  106. struct urb mt_rx_urb;
  107. struct urb *ep0_urb = &mt_ep0_urb;
  108. struct urb *tx_urb = &mt_tx_urb;
  109. struct urb *rx_urb = &mt_rx_urb;
  110. /* mt_usb related function declaration */
  111. void mt_setup_ep(unsigned int ep, struct udc_endpoint *endpoint);
  112. /* from mt_usbtty.h */
  113. #define NUM_ENDPOINTS 3
  114. /* origin endpoint_array */
  115. struct udc_endpoint ep_list[NUM_ENDPOINTS + 1]; /* one extra for control endpoint */
  116. static int usb_online = 0;
  117. u32 g_speed = 0;
  118. static u8 dev_address = 0;
  119. u32 g_tx_fifo_addr = USB_TX_FIFO_START_ADDRESS;
  120. u32 g_rx_fifo_addr = USB_RX_FIFO_START_ADDRESS;
  121. int g_enable_u3 = 0;
  122. static struct udc_device *the_device;
  123. static struct udc_gadget *the_gadget;
  124. /* end from hsusb.c */
  125. /* declare ept_complete handle */
  126. static void handle_ept_complete(struct udc_endpoint *ept);
  127. extern void mt_usb_phy_poweron(void);
  128. extern void mt_usb_phy_savecurrent(void);
  129. extern void mt_usb_phy_recover(void);
  130. /* get_max_packet_size */
  131. int get_max_packet_size(int enable_u3)
  132. {
  133. if (enable_u3 != 0) {
  134. return EP0_MAX_PACKET_SIZE;
  135. } else {
  136. return EP0_MAX_PACKET_SIZE_U3;
  137. }
  138. }
  139. void board_usb_init(void)
  140. {
  141. mt_usb_phy_poweron();
  142. mt_usb_phy_savecurrent();
  143. }
  144. struct udc_descriptor {
  145. struct udc_descriptor *next;
  146. unsigned short tag; /* ((TYPE << 8) | NUM) */
  147. unsigned short len; /* total length */
  148. unsigned char data[0];
  149. };
  150. #if DBG_USB_DUMP_SETUP
  151. static void dump_setup_packet(char *str, struct setup_packet *sp)
  152. {
  153. DBG_I("\n");
  154. DBG_I("%s", str);
  155. DBG_I(" bmRequestType = %x\n", sp->type);
  156. DBG_I(" bRequest = %x\n", sp->request);
  157. DBG_I(" wValue = %x\n", sp->value);
  158. DBG_I(" wIndex = %x\n", sp->index);
  159. DBG_I(" wLength = %x\n", sp->length);
  160. }
  161. #else
  162. static void dump_setup_packet(char *str, struct setup_packet *sp) {}
  163. #endif
  164. #if defined(DBG_USB_EP0CSR)
  165. void explain_csr0(u32 csr0)
  166. {
  167. if (csr0 & EP0_EP_RESET) {
  168. DBG_I("EP0_EP_RESET is set");
  169. }
  170. if (csr0 & EP0_AUTOCLEAR) {
  171. DBG_I("EP0_AUTOCLEAR is set");
  172. }
  173. if (csr0 & EP0_AUTOSET) {
  174. DBG_I("EP0_AUTOSET is set");
  175. }
  176. if (csr0 & EP0_DMAREQEN) {
  177. DBG_I("EP0_DMAREQEN is set");
  178. }
  179. if (csr0 & EP0_SENDSTALL) {
  180. DBG_I("EP0_SENDSTALL is set");
  181. }
  182. if (csr0 & EP0_FIFOFULL) {
  183. DBG_I("EP0_FIFOFULL is set");
  184. }
  185. if (csr0 & EP0_SENTSTALL) {
  186. DBG_I("EP0_SENTSTALL is set");
  187. }
  188. if (csr0 & EP0_DPHTX) {
  189. DBG_I("EP0_DPHTX is set");
  190. }
  191. if (csr0 & EP0_DATAEND) {
  192. DBG_I("EP0_DATAEND is set");
  193. }
  194. if (csr0 & EP0_TXPKTRDY) {
  195. DBG_I("EP0_TXPKTRDY is set");
  196. }
  197. if (csr0 & EP0_SETUPPKTRDY) {
  198. DBG_I("EP0_SETUPPKTRDY is set");
  199. }
  200. if (csr0 & EP0_RXPKTRDY) {
  201. DBG_I("EP0_RXPKTRDY is set");
  202. }
  203. }
  204. #endif
  205. static void copy_desc(struct urb *urb, void *data, int length)
  206. {
  207. #if DBG_USB_FIFO
  208. DBG_I("%s: urb: %p, data %p, length: %d, actual_length: %d\n",
  209. __func__, urb->buf, data, length, urb->actual_length);
  210. #endif
  211. //memcpy(urb->buf + urb->actual_length, data, length);
  212. memcpy(urb->buf, data, length);
  213. //urb->actual_length += length;
  214. urb->actual_length = length;
  215. #if DBG_USB_FIFO
  216. DBG_I("%s: urb: %p, data %p, length: %d, actual_length: %d\n",
  217. __func__, urb->buf, data, length, urb->actual_length);
  218. #endif
  219. }
  220. struct udc_descriptor *udc_descriptor_alloc(unsigned type, unsigned num,
  221. unsigned len)
  222. {
  223. struct udc_descriptor *desc;
  224. if ((len > 255) || (len < 2) || (num > 255) || (type > 255))
  225. return 0;
  226. if (!(desc = malloc(sizeof(struct udc_descriptor) + len)))
  227. return 0;
  228. desc->next = 0;
  229. desc->tag = (type << 8) | num;
  230. desc->len = len;
  231. desc->data[0] = len;
  232. desc->data[1] = type;
  233. return desc;
  234. }
  235. static struct udc_descriptor *desc_list = 0;
  236. static unsigned next_string_id = 1;
  237. #ifdef SUPPORT_U3
  238. static struct udc_descriptor *desc_list_u3 = 0;
  239. static unsigned next_string_id_u3 = 1;
  240. #endif
  241. void udc_descriptor_register(struct udc_descriptor *desc)
  242. {
  243. desc->next = desc_list;
  244. desc_list = desc;
  245. }
  246. #ifdef SUPPORT_U3
  247. void udc_descriptor_register_u3(struct udc_descriptor *desc)
  248. {
  249. desc->next = desc_list_u3;
  250. desc_list_u3 = desc;
  251. }
  252. #endif
  253. unsigned udc_string_desc_alloc(const char *str)
  254. {
  255. unsigned len;
  256. struct udc_descriptor *desc;
  257. unsigned char *data;
  258. if (next_string_id > 255)
  259. return 0;
  260. if (!str)
  261. return 0;
  262. len = strlen(str);
  263. desc = udc_descriptor_alloc(TYPE_STRING, next_string_id, len * 2 + 2);
  264. if (!desc)
  265. return 0;
  266. next_string_id++;
  267. /* expand ascii string to utf16 */
  268. data = desc->data + 2;
  269. while (len-- > 0) {
  270. *data++ = *str++;
  271. *data++ = 0;
  272. }
  273. udc_descriptor_register(desc);
  274. return desc->tag & 0xff;
  275. }
  276. #ifdef SUPPORT_U3
  277. unsigned udc_string_desc_alloc_u3(const char *str)
  278. {
  279. unsigned len;
  280. struct udc_descriptor *desc;
  281. unsigned char *data;
  282. if (next_string_id_u3 > 255)
  283. return 0;
  284. if (!str)
  285. return 0;
  286. len = strlen(str);
  287. desc = udc_descriptor_alloc(TYPE_STRING, next_string_id_u3, len * 2 + 2);
  288. if (!desc)
  289. return 0;
  290. next_string_id_u3++;
  291. /* expand ascii string to utf16 */
  292. data = desc->data + 2;
  293. while (len-- > 0) {
  294. *data++ = *str++;
  295. *data++ = 0;
  296. }
  297. udc_descriptor_register_u3(desc);
  298. return desc->tag & 0xff;
  299. }
  300. #endif
  301. /* mt_udc_state related functions */
  302. /*
  303. * If abnormal DATA transfer happened, like USB unplugged,
  304. * we cannot fix this after mt_udc_reset().
  305. * Because sometimes there will come reset twice.
  306. */
  307. static void mt_udc_suspend(void)
  308. {
  309. /* handle abnormal DATA transfer if we had any */
  310. struct udc_endpoint *endpoint;
  311. int i;
  312. /* initialize flags */
  313. usb_online = 0;
  314. usb_config_value = 0;
  315. the_gadget->notify(the_gadget, UDC_EVENT_OFFLINE);
  316. /* error out any pending reqs */
  317. for (i = 1; i < MT_EP_NUM; i++) {
  318. /* ensure that ept_complete considers
  319. * this to be an error state
  320. */
  321. /* End operation when encounter uninitialized ept */
  322. if (ep_list[i].num == 0)
  323. break;
  324. #if DBG_USB_GENERAL
  325. DBG_I("%s: ep: %i, in: %s, req: %p\n",
  326. __func__, ep_list[i].num, ep_list[i].in ? "IN" : "OUT", ep_list[i].req);
  327. #endif
  328. /* do nothing if usb_online == 0 */
  329. if ((ep_list[i].req && (ep_list[i].in == 0)) || /* USB_DIR_OUT */
  330. (ep_list[i].req && (ep_list[i].in == 1))) { /* USB_DIR_IN */
  331. ep_list[i].status = -1; /* HALT */
  332. endpoint = &ep_list[i];
  333. handle_ept_complete(endpoint);
  334. }
  335. }
  336. /* this is required for error handling during data transfer */
  337. txn_status = -1;
  338. #if defined(SUPPORT_QMU)
  339. /* stop qmu engine */
  340. mu3d_hal_stop_qmu(1, USB_DIR_IN);
  341. mu3d_hal_stop_qmu(1, USB_DIR_OUT);
  342. /* disable qmu interrupts */
  343. writel(0x0, U3D_QGCSR);
  344. writel(0x0, U3D_QIESR0);
  345. writel(0x0, U3D_QIESR1);
  346. /* do qmu flush */
  347. mu3d_hal_flush_qmu(1, USB_DIR_IN);
  348. mu3d_hal_flush_qmu(1, USB_DIR_OUT);
  349. /* mu3d_hal_reset_ep: we do reset here only, do not start qmu here */
  350. mu3d_hal_reset_qmu_ep(1, USB_DIR_IN);
  351. mu3d_hal_reset_qmu_ep(1, USB_DIR_OUT);
  352. #endif
  353. }
  354. /* mu3d hal related functions */
  355. /* functinos used by mu3d hal functions */
  356. int wait_for_value(int addr, unsigned int msk, unsigned int value, int ms_intvl, int count)
  357. {
  358. int i;
  359. for (i = 0; i < count; i++) {
  360. if ((readl(addr) & msk) == value)
  361. return RET_SUCCESS;
  362. mdelay(ms_intvl);
  363. }
  364. return RET_FAIL;
  365. }
  366. /*
  367. * mu3d_hal_pio_read_fifo - pio read one packet
  368. * @args - arg1: ep number, arg2: data buffer
  369. */
  370. int mu3d_hal_pio_read_fifo(int ep_num, u8 *p_buf)
  371. {
  372. u32 count = 0, residue = 0;
  373. u32 temp = 0;
  374. u8 *p_tmpbuf = p_buf;
  375. if (ep_num == 0) {
  376. residue = count = readl(U3D_RXCOUNT0);
  377. } else {
  378. residue = count = USB_READCSR32(U3D_RX1CSR3, ep_num) >> 16;
  379. }
  380. while (residue > 0) {
  381. temp = readl(USB_FIFO(ep_num));
  382. *p_tmpbuf = temp&0xFF;
  383. if (residue > 1)
  384. *(p_tmpbuf + 1) = (temp >> 8)&0xFF;
  385. if (residue > 2)
  386. *(p_tmpbuf + 2) = (temp >> 16)&0xFF;
  387. if (residue > 3)
  388. *(p_tmpbuf + 3) = (temp >> 24)&0xFF;
  389. if (residue > 4) {
  390. p_tmpbuf = p_tmpbuf + 4;
  391. residue -= 4;
  392. } else {
  393. residue = 0;
  394. }
  395. }
  396. return count;
  397. }
  398. /*
  399. * mu3d_hal_pio_write_fifo - pio write one packet
  400. * @args - arg1: ep number, arg2: data buffer
  401. */
  402. int mu3d_hal_pio_write_fifo(int ep_num, int length, u8 *p_buf, int maxp)
  403. {
  404. u32 residue = length;
  405. u32 temp = 0;
  406. #if DBG_USB_FIFO
  407. DBG_I("%s process p_num: %d, length: %d, buf: %p, maxp: %d\n",
  408. __func__, ep_num, length, p_buf, maxp);
  409. #endif
  410. while (residue > 0) {
  411. switch (residue) {
  412. case 1:
  413. temp = ((*p_buf) & 0xFF);
  414. writeb(temp, USB_FIFO(ep_num));
  415. p_buf += 1;
  416. residue -= 1;
  417. break;
  418. case 2:
  419. temp = ((*p_buf) & 0xFF)
  420. + (((*(p_buf + 1)) << 8)&0xFF00);
  421. writew(temp, USB_FIFO(ep_num));
  422. p_buf += 2;
  423. residue -= 2;
  424. udelay(50);
  425. break;
  426. case 3:
  427. temp = ((*p_buf) & 0xFF)
  428. + (((*(p_buf+1)) << 8)&0xFF00);
  429. writew(temp, USB_FIFO(ep_num));
  430. p_buf += 2;
  431. temp = ((*p_buf)&0xFF);
  432. writeb(temp, USB_FIFO(ep_num));
  433. p_buf += 1;
  434. residue -= 3;
  435. break;
  436. default:
  437. temp = ((*p_buf) & 0xFF)
  438. + (((*(p_buf + 1)) << 8)&0xFF00)
  439. + (((*(p_buf + 2)) << 16)&0xFF0000)
  440. + (((*(p_buf + 3)) << 24)&0xFF000000);
  441. writel(temp, USB_FIFO(ep_num));
  442. p_buf += 4;
  443. residue -= 4;
  444. break;
  445. };
  446. }
  447. return length;
  448. }
  449. /*
  450. * mu3d_hal_check_clk_sts - check sys125,u3 mac,u2 mac clock status
  451. */
  452. int mu3d_hal_check_clk_sts(void)
  453. {
  454. int ret;
  455. ret = wait_for_value(U3D_SSUSB_IP_PW_STS1, SSUSB_SYS125_RST_B_STS, SSUSB_SYS125_RST_B_STS, 1, 10);
  456. if (ret == RET_FAIL) {
  457. DBG_I("SSUSB_SYS125_RST_B_STS NG\n");
  458. goto CHECK_ERROR;
  459. } else {
  460. DBG_I("clk sys125:OK\n");
  461. }
  462. #ifdef SUPPORT_U3
  463. ret = wait_for_value(U3D_SSUSB_IP_PW_STS1, SSUSB_U3_MAC_RST_B_STS, SSUSB_U3_MAC_RST_B_STS, 1, 10);
  464. if (ret == RET_FAIL) {
  465. DBG_I("SSUSB_U3_MAC_RST_B_STS NG\n");
  466. goto CHECK_ERROR;
  467. } else {
  468. DBG_I("clk mac3:OK\n");
  469. }
  470. #endif
  471. ret = wait_for_value(U3D_SSUSB_IP_PW_STS2, SSUSB_U2_MAC_SYS_RST_B_STS, SSUSB_U2_MAC_SYS_RST_B_STS, 1, 10);
  472. if (ret == RET_FAIL) {
  473. DBG_I("SSUSB_U2_MAC_SYS_RST_B_STS NG\n");
  474. goto CHECK_ERROR;
  475. } else {
  476. DBG_I("clk mac2:OK\n");
  477. }
  478. return RET_SUCCESS;
  479. CHECK_ERROR:
  480. DBG_I("Reference clock stability check failed!\n");
  481. return RET_FAIL;
  482. }
  483. /*
  484. * mu3d_hal_ssusb_en - disable ssusb power down & enable u2/u3 ports
  485. */
  486. void mu3d_hal_ssusb_en(void)
  487. {
  488. clrbits(U3D_SSUSB_IP_PW_CTRL0, SSUSB_IP_SW_RST);
  489. clrbits(U3D_SSUSB_IP_PW_CTRL2, SSUSB_IP_DEV_PDN);
  490. #if defined(SUPPORT_U3) || defined(CFG_FPGA_PLATFORM)
  491. clrbits(U3D_SSUSB_U3_CTRL_0P, (SSUSB_U3_PORT_DIS | SSUSB_U3_PORT_PDN | SSUSB_U3_PORT_HOST_SEL));
  492. #endif
  493. clrbits(U3D_SSUSB_U2_CTRL_0P, (SSUSB_U2_PORT_DIS | SSUSB_U2_PORT_PDN | SSUSB_U2_PORT_HOST_SEL));
  494. setbits(U3D_SSUSB_REF_CK_CTRL, (SSUSB_REF_MAC_CK_GATE_EN | SSUSB_REF_PHY_CK_GATE_EN | SSUSB_REF_CK_GATE_EN | SSUSB_REF_MAC3_CK_GATE_EN));
  495. /* check U3D sys125,u3 mac,u2 mac clock status. */
  496. mu3d_hal_check_clk_sts();
  497. }
  498. /*
  499. * mu3d_hal_u2dev_connect - u2 device softconnect
  500. */
  501. void mu3d_hal_u2dev_connect(void)
  502. {
  503. setbits(U3D_POWER_MANAGEMENT, SOFT_CONN);
  504. }
  505. void mu3d_hal_u2dev_disconnect(void)
  506. {
  507. clrbits(U3D_POWER_MANAGEMENT, SOFT_CONN);
  508. }
  509. void mu3d_hal_u3dev_connect(void)
  510. {
  511. #ifdef SUPPORT_U3
  512. setbits(U3D_USB3_CONFIG, USB3_EN);
  513. mdelay(40);
  514. #endif
  515. }
  516. void mu3d_hal_u3dev_disconnect(void)
  517. {
  518. #ifdef SUPPORT_U3
  519. clrbits(U3D_USB3_CONFIG, USB3_EN);
  520. #endif
  521. }
  522. /*
  523. * mu3d_hal_set_speed - enable ss or connect to hs/fs
  524. * @args - arg1: speed
  525. */
  526. void mu3d_hal_set_speed(USB_SPEED speed)
  527. {
  528. /* clear ltssm state */
  529. #ifdef DBG_USB_GENERAL
  530. DBG_I("%s\n", __func__);
  531. #endif
  532. switch (speed) {
  533. case SSUSB_SPEED_FULL:
  534. clrbits(U3D_POWER_MANAGEMENT, HS_ENABLE);
  535. break;
  536. case SSUSB_SPEED_HIGH:
  537. enable_highspeed(); /* usbtty.c */
  538. setbits(U3D_POWER_MANAGEMENT, HS_ENABLE);
  539. DBG_C("USB: speed HS!!\n");
  540. break;
  541. #if defined(SUPPORT_U3)
  542. case SSUSB_SPEED_SUPER:
  543. enable_superspeed(); /* usbtty.c */
  544. setbits(U3D_POWER_MANAGEMENT, HS_ENABLE);
  545. g_enable_u3 = true;
  546. DBG_C("USB: speed SS!!\n");
  547. break;
  548. #endif
  549. default:
  550. DBG_C("Unsupported speed %d!!\n", speed);
  551. /*
  552. * work around: Disconnect U3 will stop here,
  553. * we need to call mt_udc_suspend since there
  554. * is no SUSPEND interrupt when U3 was connected before.
  555. */
  556. mt_udc_suspend();
  557. break;
  558. };
  559. }
  560. void mu3d_hal_rst_dev(void)
  561. {
  562. int ret;
  563. writel(SSUSB_DEV_SW_RST, U3D_SSUSB_DEV_RST_CTRL);
  564. writel(0, U3D_SSUSB_DEV_RST_CTRL);
  565. /* do not check when SSUSB_U2_PORT_DIS = 1, because U2 port stays in reset state */
  566. if (!(readl(U3D_SSUSB_U2_CTRL_0P) & SSUSB_U2_PORT_DIS)) {
  567. ret = wait_for_value(U3D_SSUSB_IP_PW_STS2, SSUSB_U2_MAC_SYS_RST_B_STS, SSUSB_U2_MAC_SYS_RST_B_STS, 1, 10);
  568. if (ret == RET_FAIL)
  569. DBG_C("[ERR]: SSUSB_U2_MAC_SYS_RST_B_STS NG\n");
  570. }
  571. #ifdef SUPPORT_U3
  572. /* do not check when SSUSB_U3_PORT_PDN = 1, because U3 port stays in reset state */
  573. if (!(readl(U3D_SSUSB_U3_CTRL_0P) & SSUSB_U3_PORT_PDN)) {
  574. ret = wait_for_value(U3D_SSUSB_IP_PW_STS1, SSUSB_U3_MAC_RST_B_STS, SSUSB_U3_MAC_RST_B_STS, 1, 10);
  575. if (ret == RET_FAIL)
  576. DBG_C("[ERR]: SSUSB_U3_MAC_RST_B_STS NG\n");
  577. }
  578. #endif
  579. ret = wait_for_value(U3D_SSUSB_IP_PW_STS1, SSUSB_DEV_QMU_RST_B_STS, SSUSB_DEV_QMU_RST_B_STS, 1, 10);
  580. if (ret == RET_FAIL)
  581. DBG_C("[ERR]: %d SSUSB_DEV_QMU_RST_B_STS NG\n", __LINE__);
  582. ret = wait_for_value(U3D_SSUSB_IP_PW_STS1, SSUSB_DEV_BMU_RST_B_STS, SSUSB_DEV_BMU_RST_B_STS, 1, 10);
  583. if (ret == RET_FAIL)
  584. DBG_C("[ERR]: %d SSUSB_DEV_BMU_RST_B_STS NG\n", __LINE__);
  585. ret = wait_for_value(U3D_SSUSB_IP_PW_STS1, SSUSB_DEV_RST_B_STS, SSUSB_DEV_RST_B_STS, 1, 10);
  586. if (ret == RET_FAIL)
  587. DBG_C("[ERR]: %d SSUSB_DEV_RST_B_STS NG\n", __LINE__);
  588. mdelay(50);
  589. }
  590. /*
  591. * mu3d_hal_system_intr_en - enable system global interrupt
  592. */
  593. void mu3d_hal_system_intr_en(void)
  594. {
  595. u32 int_en;
  596. #ifdef SUPPORT_U3
  597. u32 ltssm_int_en;
  598. #endif
  599. writel(readl(U3D_EPIER), U3D_EPIECR);
  600. writel(readl(U3D_DMAIER), U3D_DMAIECR);
  601. /* clear and enable common USB interrupts */
  602. writel(0, U3D_COMMON_USB_INTR_ENABLE);
  603. writel(readl(U3D_COMMON_USB_INTR), U3D_COMMON_USB_INTR);
  604. int_en = SUSPEND_INTR_EN | RESUME_INTR_EN | RESET_INTR_EN | CONN_INTR_EN |
  605. DISCONN_INTR_EN | VBUSERR_INTR_EN | LPM_INTR_EN | LPM_RESUME_INTR_EN;
  606. writel(int_en, U3D_COMMON_USB_INTR_ENABLE);
  607. #ifdef SUPPORT_U3
  608. /* clear and enable LTSSM interrupts */
  609. writel(0, U3D_LTSSM_INTR_ENABLE);
  610. writel(readl(U3D_LTSSM_INTR), U3D_LTSSM_INTR);
  611. ltssm_int_en = SS_INACTIVE_INTR_EN | SS_DISABLE_INTR_EN | COMPLIANCE_INTR_EN |
  612. LOOPBACK_INTR_EN | HOT_RST_INTR_EN | WARM_RST_INTR_EN | RECOVERY_INTR_EN |
  613. ENTER_U0_INTR_EN | ENTER_U1_INTR_EN | ENTER_U2_INTR_EN | ENTER_U3_INTR_EN |
  614. EXIT_U1_INTR_EN | EXIT_U2_INTR_EN | EXIT_U3_INTR_EN | RXDET_SUCCESS_INTR_EN |
  615. VBUS_RISE_INTR_EN | VBUS_FALL_INTR_EN | U3_LFPS_TMOUT_INTR_EN |
  616. U3_RESUME_INTR_EN;
  617. writel(ltssm_int_en, U3D_LTSSM_INTR_ENABLE);
  618. #endif
  619. writel(SSUSB_DEV_SPEED_CHG_INTR_EN, U3D_DEV_LINK_INTR_ENABLE);
  620. }
  621. void mu3d_hal_disable_intr(void)
  622. {
  623. writel(0xffffffff, U3D_EPISR);
  624. writel(0xffffffff, U3D_DMAISR);
  625. writel(0xffffffff, U3D_QISAR0);
  626. writel(0xffffffff, U3D_QISAR1);
  627. writel(0xffffffff, U3D_QEMIR);
  628. writel(0xffffffff, U3D_TQERRIR0);
  629. writel(0xffffffff, U3D_RQERRIR0);
  630. writel(0xffffffff, U3D_RQERRIR1);
  631. writel(0xffffffff, U3D_LV1IECR);
  632. writel(0xffffffff, U3D_EPIECR);
  633. writel(0xffffffff, U3D_DMAIECR);
  634. /* clear registers */
  635. writel(0xffffffff, U3D_QIECR0);
  636. writel(0xffffffff, U3D_QIECR1);
  637. writel(0xffffffff, U3D_QEMIECR);
  638. writel(0xffffffff, U3D_TQERRIECR0);
  639. writel(0xffffffff, U3D_RQERRIECR0);
  640. writel(0xffffffff, U3D_RQERRIECR1);
  641. }
  642. /* usb generic functions */
  643. static int mt_read_fifo(struct udc_endpoint *endpoint)
  644. {
  645. struct urb *urb = endpoint->rcv_urb;
  646. int len = 0, count = 0;
  647. int ep_num = endpoint->num;
  648. unsigned char *cp;
  649. if (ep_num == EP0)
  650. urb = ep0_urb;
  651. if (urb) {
  652. cp = (u8 *) (urb->buf + urb->actual_length);
  653. #if DBG_USB_FIFO
  654. DBG_I("%s: ep_num: %d, urb: %p, urb->buf: %p, urb->actual_length = %d\n",
  655. __func__, ep_num, urb, urb->buf, urb->actual_length);
  656. #endif
  657. count = len = mu3d_hal_pio_read_fifo(ep_num, cp);
  658. if (ep_num != 0) {
  659. #if DBG_USB_FIFO
  660. DBG_I("%s: ep_num: %d count = %d\n",
  661. __func__, ep_num, count);
  662. #endif
  663. }
  664. #if DBG_USB_DUMP_DATA
  665. if (ep_num != 0) {
  666. DBG_I("%s: &urb->buf: %p\n", __func__, urb->buf);
  667. DBG_I("dump data:\n");
  668. hexdump8(urb->buf, count);
  669. }
  670. #endif
  671. urb->actual_length += count;
  672. }
  673. return count;
  674. }
  675. /* Linux txstate() ?? */
  676. static int mt_write_fifo(struct udc_endpoint *ept)
  677. {
  678. int ep_num = ept->num;
  679. struct urb *urb = ept->tx_urb;
  680. int last = 0, count = 0;
  681. unsigned char *buf = NULL;
  682. u32 wrote = 0;
  683. if (ep_num == EP0)
  684. urb = ep0_urb;
  685. if (urb) {
  686. #if DBG_USB_DUMP_DESC
  687. DBG_I("%s: dump desc\n", __func__);
  688. hexdump8(urb->buf, urb->actual_length);
  689. #endif
  690. #if DBG_USB_FIFO
  691. DBG_I("%s: ep_num: %d urb: %p, actual_length: %d\n",
  692. __func__, ep_num, urb, urb->actual_length);
  693. DBG_I("%s: sent: %d, tx_pkt_size: %d\n", __func__, ept->sent, ept->maxpkt);
  694. #endif
  695. count = last = MIN (urb->actual_length - ept->sent, ept->maxpkt);
  696. #if DBG_USB_FIFO
  697. DBG_I ("[%s] urb->actual_length = %d, ept->sent = %d\n",
  698. __func__, urb->actual_length, ept->sent);
  699. #endif
  700. do {
  701. buf = urb->buf + ept->sent;
  702. /* do not use "urb->ept->tx_pktsz */
  703. wrote = mu3d_hal_pio_write_fifo(ep_num, count, buf, ept->maxpkt);
  704. count -= wrote;
  705. } while (count > 0);
  706. ept->last = last;
  707. ept->sent += last;
  708. }
  709. return last;
  710. }
  711. struct udc_endpoint *mt_find_ep(int ep_num, u8 dir)
  712. {
  713. int i;
  714. u8 in;
  715. /* convert dir to in */
  716. if (dir == USB_DIR_IN) /* dir == USB_DIR_IN */
  717. in = 1;
  718. else
  719. in = 0;
  720. for (i = 0; i < MT_EP_NUM; i++) {
  721. if ((ep_list[i].num == ep_num) && (ep_list[i].in == in)) {
  722. #if DBG_USB_GENERAL
  723. DBG_I("%s: find ep!\n", __func__);
  724. #endif
  725. return &ep_list[i];
  726. }
  727. }
  728. return NULL;
  729. }
  730. static void mt_udc_flush_fifo(u8 ep_num, u8 dir)
  731. {
  732. struct udc_endpoint *endpoint;
  733. if (ep_num == 0) {
  734. setbits(U3D_EP_RST, EP0_RST);
  735. clrbits(U3D_EP_RST, EP0_RST);
  736. } else {
  737. endpoint = mt_find_ep(ep_num, dir);
  738. if (endpoint->in == 0) { /* USB_DIR_OUT: val is 0x0; in == 0 here */
  739. setbits(U3D_EP_RST, (1 << ep_num));//reset RX EP
  740. clrbits(U3D_EP_RST, (1 << ep_num));//reset reset RX EP
  741. } else {
  742. setbits(U3D_EP_RST, (BIT16 << ep_num));//reset TX EP
  743. clrbits(U3D_EP_RST, (BIT16 << ep_num));//reset reset TX EP
  744. }
  745. }
  746. }
  747. static void mt_udc_flush_ep0_fifo(void)
  748. {
  749. mt_udc_flush_fifo(0, 0);
  750. }
  751. /* is_tx_ep_busy: used by usbtty.c */
  752. int mt_ep_busy(struct udc_endpoint *endpoint)
  753. {
  754. int ep_num = endpoint->num;
  755. u32 csr = 0;
  756. if (endpoint->in == 0) { /* USB_DIR_OUT: val is 0x0; in == 0 here */
  757. DBG_I("mt_ep_busy: ep%d is RX endpoint\n", ep_num);
  758. } else {
  759. csr = USB_READCSR32(U3D_TX1CSR0, ep_num);
  760. }
  761. return (csr & TX_TXPKTRDY);
  762. }
  763. static void udc_clean_sentstall(unsigned int ep_num, u8 dir)
  764. {
  765. if (ep_num == 0) {
  766. ep0csr_setbits(EP0_SENDSTALL);
  767. } else {
  768. if (dir == USB_DIR_OUT) {
  769. USB_WRITECSR32(U3D_RX1CSR0, ep_num, USB_READCSR32(U3D_RX1CSR0, ep_num) | RX_SENTSTALL);
  770. USB_WRITECSR32(U3D_RX1CSR0, ep_num, USB_READCSR32(U3D_RX1CSR0, ep_num) &~ RX_SENDSTALL);
  771. } else {
  772. USB_WRITECSR32(U3D_TX1CSR0, ep_num, USB_READCSR32(U3D_TX1CSR0, ep_num) | TX_SENTSTALL);
  773. USB_WRITECSR32(U3D_TX1CSR0, ep_num, USB_READCSR32(U3D_TX1CSR0, ep_num) &~ TX_SENDSTALL);
  774. }
  775. }
  776. }
  777. /* the endpoint does not support the received command, stall it!! */
  778. static void udc_stall_ep(unsigned int ep_num, u8 dir)
  779. {
  780. struct udc_endpoint *endpoint = mt_find_ep(ep_num, dir);
  781. u32 csr;
  782. DBG_C("%s\n", __func__);
  783. if (ep_num == 0) {
  784. csr = 0;
  785. mt_udc_flush_ep0_fifo();
  786. ep0csr_setbits(EP0_SENDSTALL);
  787. //TODO: check whether we have to wait for SENTSTALL here
  788. while (!(readl(U3D_EP0CSR) & EP0_SENTSTALL));
  789. udc_clean_sentstall(0, USB_DIR_OUT);
  790. } else {
  791. if (endpoint->in == 0) { /* USB_DIR_OUT: val is 0x0; in == 0 here */
  792. csr = USB_READCSR32(U3D_RX1CSR0, ep_num);
  793. csr &= RX_W1C_BITS;
  794. csr |= RX_SENDSTALL;
  795. USB_WRITECSR32(U3D_RX1CSR0, ep_num, csr);
  796. //TODO: do we have to wait for SENTSTALL here?
  797. while (!(USB_READCSR32(U3D_RX1CSR0, ep_num) & RX_SENTSTALL));
  798. udc_clean_sentstall(ep_num, USB_DIR_OUT);
  799. mt_udc_flush_fifo(ep_num, USB_DIR_OUT);
  800. } else {
  801. csr = USB_READCSR32(U3D_TX1CSR0, ep_num);
  802. csr &= TX_W1C_BITS;
  803. csr |= TX_SENDSTALL;
  804. USB_WRITECSR32(U3D_TX1CSR0, ep_num, csr);
  805. //TODO: do we have to wait for SENTSTALL here?
  806. while (!(USB_READCSR32(U3D_TX1CSR0, ep_num) & TX_SENTSTALL));
  807. udc_clean_sentstall(ep_num, USB_DIR_IN);
  808. mt_udc_flush_fifo(ep_num, USB_DIR_IN);
  809. }
  810. }
  811. ep0_state = EP0_IDLE;
  812. return;
  813. }
  814. /*
  815. * mu3d_hal_dft_reg() - apply default register settings
  816. * no use? deleted
  817. */
  818. /* from mt_usbtty.c - start */
  819. void enable_highspeed(void)
  820. {
  821. int i;
  822. g_enable_u3 = 0;
  823. g_speed = SSUSB_SPEED_HIGH;
  824. for (i = 1; i < MT_EP_NUM; i++) {
  825. if (ep_list[i].num != 0) { /* allocated */
  826. ep_list[i].maxpkt = 512;
  827. mt_setup_ep(ep_list[i].num, &ep_list[i]);
  828. }
  829. }
  830. }
  831. void enable_superspeed(void)
  832. {
  833. int i;
  834. g_enable_u3 = true;
  835. g_speed = SSUSB_SPEED_SUPER;
  836. for (i = 1; i < MT_EP_NUM; i++) {
  837. if (ep_list[i].num != 0) { /* allocated */
  838. ep_list[i].maxpkt = 1024;
  839. mt_setup_ep(ep_list[i].num, &ep_list[i]);
  840. }
  841. }
  842. }
  843. /* from mt_usbtty.c - end */
  844. USB_SPEED mt_udc_get_speed(void)
  845. {
  846. u32 speed = readl(U3D_DEVICE_CONF) & SSUSB_DEV_SPEED;
  847. switch (speed) {
  848. case 1:
  849. DBG_I("FS is detected\n");
  850. return SSUSB_SPEED_FULL;
  851. break;
  852. case 3:
  853. DBG_I("HS is detected\n");
  854. return SSUSB_SPEED_HIGH;
  855. break;
  856. case 4:
  857. DBG_I("SS is detected\n");
  858. return SSUSB_SPEED_SUPER;
  859. break;
  860. default:
  861. DBG_I("Unrecognized Speed %d\n", speed);
  862. break;
  863. };
  864. return SSUSB_SPEED_UNKNOWN;
  865. }
  866. /*
  867. * u3d_ep0en - enable ep0 function
  868. */
  869. void u3d_ep0en(void)
  870. {
  871. u32 temp = 0;
  872. struct udc_endpoint *ep0 = &ep_list[EP0];
  873. if (mt_udc_get_speed() == SSUSB_SPEED_SUPER) {
  874. ep0->maxpkt = EP0_MAX_PACKET_SIZE_U3;
  875. } else {
  876. ep0->maxpkt = EP0_MAX_PACKET_SIZE;
  877. }
  878. /* EP0CSR */
  879. temp = readl(U3D_EP0CSR);
  880. temp = ((temp & ~0x3ff) | (ep0->maxpkt & 0x3ff));
  881. //No DMA
  882. //temp |= EP0_DMAREQEN;
  883. writel(temp, U3D_EP0CSR);
  884. /* enable EP0 interrupts */
  885. setbits(U3D_EPIESR, (EP0ISR | SETUPENDISR));
  886. }
  887. void u3d_irq_en()
  888. {
  889. writel(0xFFFFFFFF, U3D_LV1IESR);
  890. }
  891. void mu3d_initialize_drv(void)
  892. {
  893. /* enable LV1 ISR */
  894. u3d_irq_en();
  895. writel(readl(U3D_POWER_MANAGEMENT) & ~LPM_MODE, U3D_POWER_MANAGEMENT);
  896. writel(readl(U3D_POWER_MANAGEMENT) | (LPM_MODE&0x1), U3D_POWER_MANAGEMENT);
  897. #ifdef EXT_VBUS_DET
  898. /* force VBUS on */
  899. writel(0x3, U3D_MISC_CTRL);
  900. #else
  901. writel(0x0, U3D_MISC_CTRL);
  902. #endif
  903. /* enable common USB ISR and L3 LTSSM ISR */
  904. mu3d_hal_system_intr_en();
  905. u3d_ep0en();
  906. }
  907. /* reset USB hardware */
  908. void mt_udc_reset(USB_SPEED speed)
  909. {
  910. u32 dwtmp1 = 0, dwtmp2 = 0, dwtmp3 = 0;
  911. dwtmp1 = readl(U3D_SSUSB_PRB_CTRL1);
  912. dwtmp2 = readl(U3D_SSUSB_PRB_CTRL2);
  913. dwtmp3 = readl(U3D_SSUSB_PRB_CTRL3);
  914. mu3d_hal_rst_dev();
  915. writel(dwtmp1, U3D_SSUSB_PRB_CTRL1);
  916. writel(dwtmp2, U3D_SSUSB_PRB_CTRL2);
  917. writel(dwtmp3, U3D_SSUSB_PRB_CTRL3);
  918. mdelay(50);
  919. mu3d_hal_ssusb_en();
  920. mu3d_hal_set_speed(speed);
  921. //TODO: Any more global variable needs to be reset here?
  922. dev_address = 0;
  923. g_tx_fifo_addr = USB_TX_FIFO_START_ADDRESS;
  924. g_rx_fifo_addr = USB_RX_FIFO_START_ADDRESS;
  925. txn_status = 0;
  926. mu3d_initialize_drv();
  927. }
  928. static void mt_udc_ep0_write(void)
  929. {
  930. struct udc_endpoint *ept = &ep_list[EP0];
  931. unsigned int count = 0;
  932. u32 csr0;
  933. csr0 = readl(U3D_EP0CSR);
  934. if (csr0 & EP0_TXPKTRDY) {
  935. DBG_I("mt_udc_ep0_write: ep0 is not ready to be written\n");
  936. return;
  937. }
  938. count = mt_write_fifo(ept);
  939. #if DBG_USB_GENERAL
  940. DBG_I("%s: count = %d\n", __func__, count);
  941. #endif
  942. /* hardware limitiation: can't set (EP0_TXPKTRDY | EP0_DATAEND) at same time */
  943. csr0 |= (EP0_TXPKTRDY);
  944. writel(csr0, U3D_EP0CSR);
  945. if (count <= ept->maxpkt) {
  946. /* last packet */
  947. ep0_urb->actual_length = 0;
  948. ept->sent = 0;
  949. } else {
  950. DBG_C(" %s wrote %d bytes n there's more, maxp is %d",
  951. __func__, count, ept->maxpkt);
  952. }
  953. }
  954. static void mt_udc_ep0_read(void)
  955. {
  956. struct udc_endpoint *ept = &ep_list[EP0];
  957. unsigned int count = 0;
  958. u32 csr0 = 0;
  959. csr0 = readl(U3D_EP0CSR);
  960. /* erroneous ep0 interrupt */
  961. if (!(csr0 & EP0_RXPKTRDY)) {
  962. return;
  963. }
  964. count = mt_read_fifo(ept);
  965. /* work around: cannot set (EP0_RXPKTRDY | EP0_DATAEND) at same time */
  966. csr0 |= (EP0_RXPKTRDY);
  967. writel(csr0, U3D_EP0CSR);
  968. if (count <= ept->maxpkt) {
  969. /* last packet */
  970. csr0 |= EP0_DATAEND;
  971. ep0_state = EP0_IDLE;
  972. } else {
  973. /* more packets are waiting to be transferred */
  974. DBG_C(" %s wrote %d bytes n there's more, maxp is %d",
  975. __func__, count, ept->maxpkt);
  976. csr0 |= EP0_RXPKTRDY;
  977. }
  978. udelay(100);
  979. writel(csr0, U3D_EP0CSR);
  980. }
  981. static void u3d_set_address(int addr)
  982. {
  983. writel((addr << DEV_ADDR_OFST), U3D_DEVICE_CONF);
  984. }
  985. static int ep0_standard_setup(struct urb *urb)
  986. {
  987. struct setup_packet *request;
  988. struct udc_descriptor *desc;
  989. u8 *cp = urb->buf;
  990. /* for CLEAR FEATURE */
  991. u8 ep_num; /* ep number */
  992. u8 dir; /* DIR */
  993. u32 csr;
  994. request = &urb->device_request;
  995. dump_setup_packet("Device Request\n", request);
  996. if ((request->type & USB_TYPE_MASK) != 0) {
  997. return false; /* Class-specific requests are handled elsewhere */
  998. }
  999. /* handle all requests that return data (direction bit set on bm RequestType) */
  1000. if ((request->type & USB_EP_DIR_MASK)) {
  1001. /* send the descriptor */
  1002. ep0_state = EP0_TX;
  1003. switch (request->request) {
  1004. /* data stage: from device to host */
  1005. case GET_STATUS:
  1006. #if DBG_USB_GENERAL
  1007. DBG_I("GET_STATUS\n");
  1008. #endif
  1009. urb->actual_length = 2;
  1010. cp[0] = cp[1] = 0;
  1011. switch (request->type & USB_RECIP_MASK) {
  1012. case USB_RECIP_DEVICE:
  1013. cp[0] = USB_STAT_SELFPOWERED;
  1014. break;
  1015. case USB_RECIP_OTHER:
  1016. urb->actual_length = 0;
  1017. break;
  1018. default:
  1019. break;
  1020. }
  1021. return 0;
  1022. case GET_DESCRIPTOR:
  1023. #if DBG_USB_GENERAL
  1024. DBG_I("GET_DESCRIPTOR\n");
  1025. #endif
  1026. /* usb_highspeed? */
  1027. #ifdef SUPPORT_U3
  1028. if (g_enable_u3 == true) {
  1029. desc = desc_list_u3;
  1030. #ifdef DBG_USB_GENERAL
  1031. DBG_I("g_enable_u3 == true, g_speed: %d\n", g_speed);
  1032. #endif
  1033. } else {
  1034. desc = desc_list;
  1035. #ifdef DBG_USB_GENERAL
  1036. DBG_I("g_enable_u3 != true, g_speed: %d\n", g_speed);
  1037. #endif
  1038. }
  1039. /* fix build warning here: using "desc = desc" */
  1040. for (desc = desc; desc; desc = desc->next) {
  1041. #else
  1042. for (desc = desc_list; desc; desc = desc->next) {
  1043. #endif
  1044. #if DBG_USB_DUMP_DESC
  1045. DBG_I("desc->tag: %x: request->value: %x\n", desc->tag, request->value);
  1046. #endif
  1047. if (desc->tag == request->value) {
  1048. #if DBG_USB_DUMP_DESC
  1049. DBG_I("Find packet!\n");
  1050. #endif
  1051. unsigned len = desc->len;
  1052. if (len > request->length)
  1053. len = request->length;
  1054. #if DBG_USB_GENERAL
  1055. DBG_I("%s: urb: %p, cp: %p\n", __func__, urb, cp);
  1056. #endif
  1057. copy_desc(urb, desc->data, len);
  1058. return 0;
  1059. }
  1060. }
  1061. /* descriptor lookup failed */
  1062. return false;
  1063. case GET_CONFIGURATION:
  1064. #if DBG_USB_GENERAL
  1065. DBG_I("GET_CONFIGURATION\n");
  1066. #endif
  1067. urb->actual_length = 1;
  1068. cp[0] = 1;
  1069. return 0;
  1070. case GET_INTERFACE:
  1071. #if DBG_USB_GENERAL
  1072. DBG_I("GET_INTERFACE\n");
  1073. #endif
  1074. default:
  1075. DBG_C("Unsupported command with TX data stage\n");
  1076. break;
  1077. }
  1078. }
  1079. else {
  1080. switch (request->request) {
  1081. case SET_ADDRESS:
  1082. #if DBG_USB_GENERAL
  1083. DBG_I("SET_ADDRESS\n");
  1084. #endif
  1085. dev_address = (request->value);
  1086. set_address = 1;
  1087. u3d_set_address(dev_address);
  1088. return 0;
  1089. case SET_CONFIGURATION:
  1090. #if DBG_USB_GENERAL
  1091. DBG_I("SET_CONFIGURATION\n");
  1092. #endif
  1093. if (request->value == 1) {
  1094. usb_config_value = 1;
  1095. /* work around: restore epx status after BUS RESET?? */
  1096. mu3d_hal_set_speed(g_speed);
  1097. udelay(50);
  1098. /* restore QMU */
  1099. #ifdef SUPPORT_QMU
  1100. usb_initialize_qmu();
  1101. mu3d_hal_start_qmu(1, USB_DIR_OUT);
  1102. mu3d_hal_start_qmu(1, USB_DIR_IN);
  1103. udelay(50);
  1104. #endif
  1105. the_gadget->notify(the_gadget, UDC_EVENT_ONLINE);
  1106. } else {
  1107. usb_config_value = 0;
  1108. the_gadget->notify(the_gadget, UDC_EVENT_OFFLINE);
  1109. }
  1110. usb_online = request->value ? 1 : 0;
  1111. #ifdef DBG_USB_GENERAL
  1112. DBG_I("usb_online: %d\n", usb_online);
  1113. #endif
  1114. return 0;
  1115. #ifdef SUPPORT_U3
  1116. case SET_SEL:
  1117. ep0_state = EP0_RX;
  1118. return 0;
  1119. case SET_FEATURE:
  1120. if (request->value == USB3_U1_ENABLE)
  1121. #if DBG_USB_GENERAL
  1122. DBG_I("USB3_U1_ENABLE\n");
  1123. #endif
  1124. if (request->value == USB3_U2_ENABLE)
  1125. #if DBG_USB_GENERAL
  1126. DBG_I("USB3_U2_ENABLE\n");
  1127. #endif
  1128. return 0;
  1129. #endif
  1130. case CLEAR_FEATURE:
  1131. #if DBG_USB_GENERAL
  1132. DBG_I("CLEAR_FEATURE\n");
  1133. #endif
  1134. ep_num = request->index & 0xf;
  1135. dir = request->index & 0x80;
  1136. if ((request->value == 0) && (request->length == 0)) {
  1137. #if DBG_USB_GENERAL
  1138. DBG_I("Clear Feature: ep: %d dir: %d\n", ep_num, dir);
  1139. #endif
  1140. switch (dir) {
  1141. case USB_DIR_OUT:
  1142. csr = USB_READCSR32(U3D_TX1CSR0, ep_num) & TX_W1C_BITS;
  1143. csr = (csr & (~TX_SENDSTALL)) | TX_SENTSTALL;
  1144. USB_WRITECSR32(U3D_TX1CSR0, ep_num, csr);
  1145. #if DBG_USB_GENERAL
  1146. DBG_I("clear tx stall ep: %d dir: %d\n", ep_num, dir);
  1147. #endif
  1148. setbits(U3D_EP_RST, (BIT16 << ep_num));//reset TX EP
  1149. clrbits(U3D_EP_RST, (BIT16 << ep_num));//reset reset TX EP
  1150. break;
  1151. case USB_DIR_IN:
  1152. csr = USB_READCSR32(U3D_RX1CSR0, ep_num) & RX_W1C_BITS;
  1153. csr = (csr & (~RX_SENDSTALL)) | RX_SENTSTALL;
  1154. USB_WRITECSR32(U3D_RX1CSR0, ep_num, csr);
  1155. #if DBG_USB_GENERAL
  1156. DBG_I("clear rx stall ep: %d dir: %d\n", ep_num, dir);
  1157. #endif
  1158. setbits(U3D_EP_RST, (1 << ep_num));//reset RX EP
  1159. clrbits(U3D_EP_RST, (1 << ep_num));//reset reset RX EP
  1160. break;
  1161. default:
  1162. break;
  1163. }
  1164. return 0;
  1165. }
  1166. default:
  1167. #ifdef DBG_USB_DUMP_SETUP
  1168. DBG_I("desc->tag: %x: request->request: %x, request->value: %x\n", desc->tag, request->request, request->value);
  1169. DBG_C("Unsupported command with RX data stage\n");
  1170. #endif
  1171. break;
  1172. } /* switch request */
  1173. }
  1174. return FALSE;
  1175. }
  1176. static void mt_udc_ep0_setup(void) {
  1177. struct udc_endpoint *endpoint = &ep_list[0];
  1178. u8 stall = 0;
  1179. u32 csr0;
  1180. struct setup_packet *request;
  1181. #ifdef USB_DEBUG
  1182. u16 count;
  1183. #endif
  1184. /* Read control status register for endpiont 0 */
  1185. csr0 = readl(U3D_EP0CSR);
  1186. /* check whether RxPktRdy is set? */
  1187. if (!(csr0 & EP0_SETUPPKTRDY))
  1188. return;
  1189. /* unload fifo */
  1190. ep0_urb->actual_length = 0;
  1191. #ifndef USB_DEBUG
  1192. mt_read_fifo(endpoint);
  1193. #else
  1194. count = mt_read_fifo(endpoint);
  1195. #if DBG_USB_FIFO
  1196. DBG_I("%s: mt_read_fifo count = %d\n", __func__, count);
  1197. #endif
  1198. #endif
  1199. /* decode command */
  1200. request = &ep0_urb->device_request;
  1201. memcpy(request, ep0_urb->buf, sizeof(struct setup_packet));
  1202. if (((request->type) & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
  1203. #if DBG_USB_GENERAL
  1204. DBG_I("Standard Request\n");
  1205. #endif
  1206. stall = ep0_standard_setup(ep0_urb);
  1207. if (stall) {
  1208. dump_setup_packet("STANDARD REQUEST NOT SUPPORTED\n", request);
  1209. }
  1210. } else if (((request->type) & USB_TYPE_MASK) == USB_TYPE_CLASS) {
  1211. #if DBG_USB_GENERAL
  1212. DBG_I("Class-Specific Request\n");
  1213. #endif
  1214. if (stall) {
  1215. dump_setup_packet("CLASS REQUEST NOT SUPPORTED\n", request);
  1216. }
  1217. } else if (((request->type) & USB_TYPE_MASK) == USB_TYPE_VENDOR) {
  1218. #if DBG_USB_GENERAL
  1219. DBG_I("Vendor-Specific Request\n");
  1220. /* do nothing now */
  1221. DBG_I("ALL VENDOR-SPECIFIC REQUESTS ARE NOT SUPPORTED!!\n");
  1222. #endif
  1223. }
  1224. if (stall) {
  1225. /* the received command is not supported */
  1226. udc_stall_ep(0, USB_DIR_OUT);
  1227. return;
  1228. }
  1229. /* handle EP0 state */
  1230. csr0 = readl(U3D_EP0CSR);
  1231. switch (ep0_state) {
  1232. case EP0_TX:
  1233. /* data stage: from device to host */
  1234. #if DBG_USB_GENERAL
  1235. DBG_I("%s: EP0_TX\n", __func__);
  1236. #endif
  1237. /* move out from udc_chg_ep0_state() */
  1238. csr0 = readl(U3D_EP0CSR);
  1239. csr0 |= (EP0_SETUPPKTRDY | EP0_DPHTX);
  1240. writel(csr0, U3D_EP0CSR);
  1241. mt_udc_ep0_write();
  1242. break;
  1243. case EP0_RX:
  1244. /* data stage: from host to device */
  1245. #if DBG_USB_GENERAL
  1246. DBG_I("%s: EP0_RX\n", __func__);
  1247. #endif
  1248. /* move out from udc_chg_ep0_state(): no need? */
  1249. csr0 = readl(U3D_EP0CSR);
  1250. csr0 |= (EP0_SETUPPKTRDY);
  1251. writel(csr0, U3D_EP0CSR);
  1252. break;
  1253. case EP0_IDLE:
  1254. /* no data stage */
  1255. #if DBG_USB_GENERAL
  1256. DBG_I("%s: EP0_IDLE\n", __func__);
  1257. #endif
  1258. csr0 = readl(U3D_EP0CSR);
  1259. csr0 |= (EP0_RXPKTRDY | EP0_DATAEND);
  1260. writel(csr0, U3D_EP0CSR);
  1261. break;
  1262. default:
  1263. break;
  1264. }
  1265. }
  1266. static void mt_udc_ep0_handler(void) {
  1267. u32 csr0;
  1268. /*
  1269. * EP0 interrupt is generated when
  1270. * - EP0CSR.RxPktRdy bit is set after data packet has been received and stored into FIFO0
  1271. * - data packet in FIFO0 has been sent to host successfully
  1272. * - EP0CSR.SentStall bit is set after host receives STALL
  1273. * - EP0CSR.SetupEnd bit is set after receiving SETUP transaction in DATA/STATUS phase
  1274. */
  1275. csr0 = readl(U3D_EP0CSR);
  1276. if (csr0 & EP0_SENTSTALL) {
  1277. #if DBG_USB_GENERAL
  1278. DBG_I("USB: [EP0] SENTSTALL\n");
  1279. #endif
  1280. /* needs implementation for exception handling here */
  1281. //ep0_state = EP0_IDLE;
  1282. // writel((readl(U3D_EP0CSR) & EP0_W1C_BITS) | EP0_SENTSTALL, U3D_EP0CSR);
  1283. //udc_chg_ep0_state(EP0_IDLE);
  1284. ep0_state = EP0_IDLE;
  1285. ep0csr_setbits(EP0_SENTSTALL);
  1286. }
  1287. switch (ep0_state) {
  1288. case EP0_IDLE:
  1289. #if DBG_USB_GENERAL
  1290. DBG_I("%s: EP0_IDLE\n", __func__);
  1291. #endif
  1292. if (set_address) {
  1293. u3d_set_address(dev_address);
  1294. set_address = 0;
  1295. }
  1296. mt_udc_ep0_setup();
  1297. break;
  1298. case EP0_TX:
  1299. #if DBG_USB_GENERAL
  1300. DBG_I("%s: EP0_TX\n", __func__);
  1301. #endif
  1302. /* complete and sendout buffer */
  1303. csr0 |= EP0_DATAEND;
  1304. writel(csr0, U3D_EP0CSR);
  1305. ep0_state = EP0_IDLE;
  1306. break;
  1307. case EP0_RX:
  1308. #if DBG_USB_GENERAL
  1309. DBG_I("%s: EP0_RX\n", __func__);
  1310. #endif
  1311. mt_udc_ep0_read();
  1312. ep0_state = EP0_IDLE;
  1313. break;
  1314. default:
  1315. DBG_I("[ERR]: Unrecognized ep0 state%d", ep0_state);
  1316. break;
  1317. }
  1318. return;
  1319. }
  1320. static void mt_udc_epx_handler(u8 ep_num, u8 dir) {
  1321. u32 csr;
  1322. u32 count;
  1323. struct udc_endpoint *ept;
  1324. struct urb *urb;
  1325. struct udc_request *req; /* for event signaling */
  1326. ept = mt_find_ep(ep_num, dir);
  1327. #if DBG_USB_GENERAL
  1328. DBG_I("EP%d Interrupt\n", ep_num);
  1329. DBG_I("dir: %x\n", dir);
  1330. #endif
  1331. switch (dir) {
  1332. case USB_DIR_OUT:
  1333. /* transfer direction is from host to device */
  1334. /* from the view of usb device, it's RX */
  1335. csr = USB_READCSR32(U3D_RX1CSR0, ep_num);
  1336. if (csr & RX_SENTSTALL) {
  1337. DBG_C("EP %d(RX): STALL\n", ep_num);
  1338. /* exception handling: implement this!! */
  1339. return;
  1340. }
  1341. #ifdef SUPPORT_QMU /* SUPPORT_QMU */
  1342. count = ept->rcv_urb->actual_length;
  1343. #ifdef DBG_USB_QMU
  1344. DBG_I("%s: QMU: count: %d\n", __func__, count);
  1345. #endif
  1346. #else /* PIO MODE */
  1347. if (!(csr & RX_RXPKTRDY)) {
  1348. #if DBG_USB_GENERAL
  1349. DBG_I("EP %d: ERRONEOUS INTERRUPT\n", ep_num); // normal
  1350. #endif
  1351. return;
  1352. }
  1353. count = mt_read_fifo(ept);
  1354. #if DBG_USB_GENERAL
  1355. DBG_I("EP%d(RX), count = %d\n", ep_num, count);
  1356. #endif
  1357. /* write 1 to clear RXPKTRDY */
  1358. csr |= RX_RXPKTRDY;
  1359. USB_WRITECSR32(U3D_RX1CSR0, ep_num, csr);
  1360. if (USB_READCSR32(U3D_RX1CSR0, ep_num) & RX_RXPKTRDY) {
  1361. #if DBG_USB_GENERAL
  1362. DBG_I("%s: rxpktrdy clear failed\n", __func__);
  1363. #endif
  1364. }
  1365. #endif /* ifndef SUPPORT_QMU */
  1366. /* do signaling */
  1367. req = ept->req;
  1368. /* workaround: if req->lenth == 64 bytes (not huge data transmission)
  1369. * do normal return */
  1370. #if DBG_USB_GENERAL
  1371. DBG_I("%s: req->length: %x, ept->rcv_urb->actual_length: %x\n",
  1372. __func__, req->length, ept->rcv_urb->actual_length);
  1373. #endif
  1374. /* Deal with FASTBOOT command */
  1375. if ((req->length >= ept->rcv_urb->actual_length) && req->length == 64) {
  1376. req->length = count;
  1377. /* mask EPx INTRRXE */
  1378. /* The buffer is passed from the AP caller.
  1379. * It happens that AP is dealing with the buffer filled data by driver,
  1380. * but the driver is still receiving the next data packets onto the buffer.
  1381. * Data corrupted happens if the every request use the same buffer.
  1382. * Mask the EPx to ensure that AP and driver are not accessing the buffer parallely.
  1383. */
  1384. /* set 1 to disable (clear) enable mask */
  1385. writel((BIT16 << ep_num), U3D_EPIECR);
  1386. }
  1387. /* Deal with DATA transfer */
  1388. if ((req->length == ept->rcv_urb->actual_length) ||
  1389. ((req->length >= ept->rcv_urb->actual_length) && req->length == 64)) {
  1390. handle_ept_complete(ept);
  1391. /* mask EPx INTRRXE */
  1392. /* The buffer is passed from the AP caller.
  1393. * It happens that AP is dealing with the buffer filled data by driver,
  1394. * but the driver is still receiving the next data packets onto the buffer.
  1395. * Data corrupted happens if the every request use the same buffer.
  1396. * Mask the EPx to ensure that AP and driver are not accessing the buffer parallely.
  1397. */
  1398. /* set 1 to disable (clear) enable mask */
  1399. writel((BIT16 << ep_num), U3D_EPIECR);
  1400. }
  1401. break;
  1402. case USB_DIR_IN:
  1403. /* transfer direction is from device to host */
  1404. /* from the view of usb device, it's tx */
  1405. csr = USB_READCSR32(U3D_TX1CSR0, ep_num);
  1406. if (csr & TX_SENTSTALL) {
  1407. DBG_C("EP %d(TX): STALL\n", ep_num);
  1408. ept->status = -1;
  1409. handle_ept_complete(ept);
  1410. /* exception handling: implement this!! */
  1411. return;
  1412. }
  1413. #ifndef SUPPORT_QMU
  1414. if (csr & TX_TXPKTRDY) {
  1415. DBG_C
  1416. ("%s: ep%d is not ready to be written\n",
  1417. __func__, ep_num);
  1418. return;
  1419. }
  1420. #endif
  1421. urb = ept->tx_urb;
  1422. if (ept->sent == urb->actual_length) {
  1423. /* do signaling */
  1424. handle_ept_complete(ept);
  1425. break;
  1426. }
  1427. /* send next packet of the same urb */
  1428. #ifndef SUPPORT_QMU
  1429. count = mt_write_fifo(ept);
  1430. #if DBG_USB_GENERAL
  1431. DBG_I("EP%d(TX), count = %d\n", ep_num, ept->sent);
  1432. #endif
  1433. if (count != 0) {
  1434. /* not the interrupt generated by the last tx packet of the transfer */
  1435. csr |= TX_TXPKTRDY;
  1436. USB_WRITECSR32(TX_TXPKTRDY, ep_num, csr);
  1437. }
  1438. #endif
  1439. break;
  1440. default:
  1441. break;
  1442. }
  1443. }
  1444. #if defined(DBG_USB_LTSSM)
  1445. void report_ltssm_type(u32 ltssm) {
  1446. if (ltssm & RXDET_SUCCESS_INTR) {
  1447. DBG_I("RXDET_SUCCESS_INTR\n");
  1448. }
  1449. if (ltssm & HOT_RST_INTR) {
  1450. DBG_I("HOT_RST_INTR\n");
  1451. }
  1452. if (ltssm & WARM_RST_INTR) {
  1453. DBG_I("WARM_RST_INTR\n");
  1454. }
  1455. if (ltssm & ENTER_U0_INTR) {
  1456. DBG_I("ENTER_U0_INTR\n");
  1457. }
  1458. if (ltssm & VBUS_RISE_INTR) {
  1459. DBG_I("VBUS_RISE_INTR\n");
  1460. }
  1461. if (ltssm & VBUS_FALL_INTR) {
  1462. DBG_I("VBUS_FALL_INTR\n");
  1463. }
  1464. if (ltssm & ENTER_U1_INTR) {
  1465. DBG_I("ENTER_U1_INTR\n");
  1466. }
  1467. if (ltssm & ENTER_U2_INTR) {
  1468. DBG_I("ENTER_U2_INTR\n");
  1469. }
  1470. if (ltssm & ENTER_U3_INTR) {
  1471. DBG_I("ENTER_U3_INTR\n");
  1472. }
  1473. if (ltssm & EXIT_U1_INTR) {
  1474. DBG_I("EXIT_U1_INTR\n");
  1475. }
  1476. if (ltssm & EXIT_U2_INTR) {
  1477. DBG_I("EXIT_U2_INTR\n");
  1478. }
  1479. if (ltssm & EXIT_U3_INTR) {
  1480. DBG_I("EXIT_U3_INTR\n");
  1481. }
  1482. }
  1483. #endif
  1484. /*
  1485. * get_seg_size()
  1486. *
  1487. * Return value indicates the TxFIFO size of 2^n bytes, (ex: value 10 means 2^10 =
  1488. * 1024 bytes.) TXFIFOSEGSIZE should be equal or bigger than 4. The TxFIFO size of
  1489. * 2^n bytes also should be equal or bigger than TXMAXPKTSZ. This EndPoint occupy
  1490. * total memory size (TX_SLOT + 1 )*2^TXFIFOSEGSIZE bytes.
  1491. */
  1492. u8 get_seg_size(u32 max_packet_size) {
  1493. /* Set fifo size(double buffering is currently not enabled) */
  1494. switch (max_packet_size) {
  1495. case 8:
  1496. case 16:
  1497. return USB_FIFOSZ_SIZE_16;
  1498. case 32:
  1499. return USB_FIFOSZ_SIZE_32;
  1500. case 64:
  1501. return USB_FIFOSZ_SIZE_64;
  1502. case 128:
  1503. return USB_FIFOSZ_SIZE_128;
  1504. case 256:
  1505. return USB_FIFOSZ_SIZE_256;
  1506. case 512:
  1507. return USB_FIFOSZ_SIZE_512;
  1508. case 1023:
  1509. case 1024:
  1510. case 2048:
  1511. case 3072:
  1512. case 4096:
  1513. return USB_FIFOSZ_SIZE_1024;
  1514. default:
  1515. DBG_I("The max_packet_size %d is not supported\n", max_packet_size);
  1516. return USB_FIFOSZ_SIZE_512;
  1517. }
  1518. }
  1519. /*
  1520. * udc_setup_ep - setup endpoint
  1521. *
  1522. * Associate a physical endpoint with endpoint_instance and initialize FIFO
  1523. */
  1524. void mt_setup_ep(unsigned int ep_num, struct udc_endpoint *endpoint) {
  1525. u32 csr0, csr1, csr2;
  1526. u32 max_packet_size;
  1527. u8 seg_size;
  1528. u8 max_pkt;
  1529. u8 burst = endpoint->burst;
  1530. u8 mult = endpoint->mult;
  1531. u8 type = endpoint->type;
  1532. u8 slot = endpoint->slot;
  1533. /* EP table records in bits hence bit 1 is ep0 */
  1534. /* Nothing needs to be done for ep0 */
  1535. if (ep_num == 0) { // or (endpoint->type == USB_EP_XFER_CTRL)
  1536. return;
  1537. }
  1538. /* Configure endpoint fifo */
  1539. /* Set fifo address, fifo size, and fifo max packet size */
  1540. #if DBG_USB_GENERAL
  1541. DBG_I("%s: endpoint->in: %d, maxpkt: %d\n",
  1542. __func__, endpoint->in, endpoint->maxpkt);
  1543. #endif
  1544. if (endpoint->in == 0) { /* USB_DIR_OUT: val is 0x0; in == 0 here */
  1545. /* RX case */
  1546. mt_udc_flush_fifo(ep_num, USB_DIR_OUT);
  1547. max_packet_size = endpoint->maxpkt;
  1548. /* Set fifo size(double buffering is currently not enabled) */
  1549. seg_size = get_seg_size(max_packet_size);
  1550. /* CSR0 */
  1551. csr0 = USB_READCSR32(U3D_RX1CSR0, ep_num) &~ RX_RXMAXPKTSZ;
  1552. csr0 |= (max_packet_size & RX_RXMAXPKTSZ);
  1553. #ifndef SUPPORT_QMU /* PIO_MODE */
  1554. csr0 &= ~RX_DMAREQEN;
  1555. #endif
  1556. /* CSR1 */
  1557. max_pkt = (burst + 1) * (mult + 1) - 1;
  1558. csr1 = (burst & SS_RX_BURST);
  1559. csr1 |= (slot << RX_SLOT_OFST) & RX_SLOT;
  1560. csr1 |= (max_pkt << RX_MAX_PKT_OFST) & RX_MAX_PKT;
  1561. csr1 |= (mult << RX_MULT_OFST) & RX_MULT;
  1562. /* CSR2 */
  1563. csr2 = (g_rx_fifo_addr >> 4) & RXFIFOADDR;
  1564. csr2 |= (seg_size << RXFIFOSEGSIZE_OFST) & RXFIFOSEGSIZE;
  1565. /* In LK (FASTBOOT) will use BULK transfer only */
  1566. if (type == USB_EP_XFER_BULK) {
  1567. csr1 |= TYPE_BULK;
  1568. } else if (type == USB_EP_XFER_INT) {
  1569. csr1 |= TYPE_INT;
  1570. csr2 |= (endpoint->binterval << RXBINTERVAL_OFST) & RXBINTERVAL;
  1571. }
  1572. /* Write 1 to clear EPIER */
  1573. setbits(U3D_EPIECR, (BIT16 << ep_num));
  1574. USB_WRITECSR32(U3D_RX1CSR0, ep_num, csr0);
  1575. USB_WRITECSR32(U3D_RX1CSR1, ep_num, csr1);
  1576. USB_WRITECSR32(U3D_RX1CSR2, ep_num, csr2);
  1577. /* Write 1 to set EPIER */
  1578. #ifndef SUPPORT_QMU /* For QMU, we don't enable EP interrupts. */
  1579. writel(readl(U3D_EPIER) | (BIT16 << ep_num), U3D_EPIESR);
  1580. #endif
  1581. if (max_packet_size == 1023) {
  1582. g_rx_fifo_addr += (1024 * (slot + 1));
  1583. } else {
  1584. g_rx_fifo_addr += (max_packet_size * (slot + 1));
  1585. }
  1586. if (g_rx_fifo_addr > readl(U3D_CAP_EPNRXFFSZ)) {
  1587. DBG_I("[ERR]g_rx_fifo_addr is %x and U3D_CAP_EPNTXFFSZ is %x for ep%d\n",
  1588. g_rx_fifo_addr, readl(U3D_CAP_EPNRXFFSZ), ep_num);
  1589. DBG_I("max_packet_size = %d\n", max_packet_size);
  1590. DBG_I("slot = %d\n", slot);
  1591. }
  1592. } else { /* USB_DIR_IN: val is 0x80; in == 1 here */
  1593. /* TX case */
  1594. mt_udc_flush_fifo(ep_num, USB_DIR_IN);
  1595. max_packet_size = endpoint->maxpkt;
  1596. /* Set fifo size(double buffering is currently not enabled) */
  1597. seg_size = get_seg_size(max_packet_size);
  1598. /* CSR0 */
  1599. csr0 = USB_READCSR32(U3D_TX1CSR0, ep_num) &~ TX_TXMAXPKTSZ;
  1600. csr0 |= (max_packet_size & TX_TXMAXPKTSZ);
  1601. #ifndef SUPPORT_QMU /* PIO_MODE */
  1602. csr0 &= ~TX_DMAREQEN;
  1603. #endif
  1604. /* CSR1 */
  1605. max_pkt = (burst + 1)*(mult + 1) - 1;
  1606. csr1 = (burst & SS_TX_BURST);
  1607. csr1 |= (slot << TX_SLOT_OFST) & TX_SLOT;
  1608. csr1 |= (max_pkt << TX_MAX_PKT_OFST) & TX_MAX_PKT;
  1609. csr1 |= (mult << TX_MULT_OFST) & TX_MULT;
  1610. /* CSR2 */
  1611. csr2 = (g_tx_fifo_addr >> 4) & TXFIFOADDR;
  1612. csr2 |= (seg_size << TXFIFOSEGSIZE_OFST) & TXFIFOSEGSIZE;
  1613. /* In LK (FASTBOOT) will use BULK transfer only */
  1614. if (type == USB_EP_XFER_BULK) {
  1615. csr1 |= TYPE_BULK;
  1616. } else if (type == USB_EP_XFER_INT) {
  1617. csr1 |= TYPE_INT;
  1618. csr2 |= (endpoint->binterval << TXBINTERVAL_OFST) & TXBINTERVAL;
  1619. }
  1620. /* Write 1 to clear EPIER */
  1621. setbits(U3D_EPIECR, (BIT0 << ep_num));
  1622. USB_WRITECSR32(U3D_TX1CSR0, ep_num, csr0);
  1623. USB_WRITECSR32(U3D_TX1CSR1, ep_num, csr1);
  1624. USB_WRITECSR32(U3D_TX1CSR2, ep_num, csr2);
  1625. /* Write 1 to set EPIER */
  1626. #ifndef SUPPORT_QMU /* For QMU, we don't enable EP interrupts. */
  1627. writel(readl(U3D_EPIER) | (BIT0 << ep_num), U3D_EPIESR);
  1628. #endif
  1629. if (max_packet_size == 1023) {
  1630. g_tx_fifo_addr += (1024 * (slot + 1));
  1631. } else {
  1632. g_tx_fifo_addr += (max_packet_size * (slot + 1));
  1633. }
  1634. if (g_tx_fifo_addr > readl(U3D_CAP_EPNTXFFSZ)) {
  1635. DBG_I("[ERR]g_tx_fifo_addr is %x and U3D_CAP_EPNTXFFSZ is %x for ep%d",
  1636. g_tx_fifo_addr, readl(U3D_CAP_EPNTXFFSZ), ep_num);
  1637. DBG_I("max_packet_size = %d\n", max_packet_size);
  1638. DBG_I("slot = %d]n", slot);
  1639. }
  1640. }
  1641. }
  1642. struct udc_endpoint *_udc_endpoint_alloc(unsigned char num, unsigned char in,
  1643. unsigned short max_pkt) {
  1644. int i;
  1645. /*
  1646. * find an unused slot in ep_list from EP1 to MAX_EP
  1647. * for example, EP1 will use 2 slot one for IN and the other for OUT
  1648. */
  1649. if (num != EP0) {
  1650. for (i = 1; i < MT_EP_NUM; i++) {
  1651. if (ep_list[i].num == 0) /* usable */
  1652. break;
  1653. }
  1654. if (i == MT_EP_NUM) /* ep has been exhausted. */
  1655. return NULL;
  1656. /* EPx Type */
  1657. ep_list[i].type = USB_EP_XFER_BULK;
  1658. /* set urb */
  1659. if (in) { /* usb EP1 tx */
  1660. ep_list[i].tx_urb = tx_urb;
  1661. tx_urb->ept = &ep_list[i];
  1662. } else { /* usb EP1 rx */
  1663. ep_list[i].rcv_urb = rx_urb;
  1664. rx_urb->ept = &ep_list[i];
  1665. }
  1666. } else {
  1667. i = EP0; /* EP0 */
  1668. }
  1669. ep_list[i].maxpkt = max_pkt;
  1670. ep_list[i].num = num;
  1671. ep_list[i].in = in;
  1672. ep_list[i].req = NULL;
  1673. ep_list[i].slot = 1; /* USB burst mode: 1: enable, 0: disable */
  1674. /* store EPT_TX/RX info */
  1675. if (ep_list[i].in) {
  1676. ep_list[i].bit = EPT_TX(num);
  1677. } else {
  1678. ep_list[i].bit = EPT_RX(num);
  1679. }
  1680. /* write parameters to this ep (write to hardware) */
  1681. mt_setup_ep(num, &ep_list[i]);
  1682. DBG_I("ept%d %s @%p/%p max=%d bit=%x\n",
  1683. num, in ? "in" : "out", &ep_list[i], &ep_list, max_pkt, ep_list[i].bit);
  1684. return &ep_list[i];
  1685. }
  1686. #define SETUP(type,request) (((type) << 8) | (request))
  1687. static unsigned long ept_alloc_table = EPT_TX(0) | EPT_RX(0);
  1688. struct udc_endpoint *udc_endpoint_alloc(unsigned type, unsigned maxpkt) {
  1689. struct udc_endpoint *ept;
  1690. unsigned n;
  1691. unsigned in;
  1692. if (type == UDC_TYPE_BULK_IN) {
  1693. in = 1;
  1694. } else if (type == UDC_TYPE_BULK_OUT) {
  1695. in = 0;
  1696. } else {
  1697. return 0;
  1698. }
  1699. /* udc_endpoint_alloc is used for EPx except EP0 */
  1700. for (n = 1; n < 16; n++) {
  1701. unsigned long bit = in ? EPT_TX(n) : EPT_RX(n);
  1702. if (ept_alloc_table & bit)
  1703. continue;
  1704. ept = _udc_endpoint_alloc(n, in, maxpkt);
  1705. if (ept)
  1706. ept_alloc_table |= bit;
  1707. return ept;
  1708. }
  1709. return 0;
  1710. }
  1711. /* must use with request. If req == NULL should be error handling */
  1712. static void handle_ept_complete(struct udc_endpoint *ept) {
  1713. unsigned int actual;
  1714. int status;
  1715. struct udc_request *req;
  1716. req = ept->req;
  1717. if (req) {
  1718. #if DBG_USB_GENERAL
  1719. DBG_I("%s: req: %p: req->length: %d: status: %d\n", __func__, req, req->length, ept->status);
  1720. #endif
  1721. /*
  1722. * This request releasement will cause udc_request_queue abnormal when INTR comes later.
  1723. * but will only affect QMU mode because signal didn't return correctly.
  1724. */
  1725. ept->req = NULL;
  1726. if (ept->status == -1) {
  1727. actual = 0;
  1728. status = -1; /* txn_status */
  1729. DBG_C("%s: EP%d/%s FAIL status: %x\n",
  1730. __func__, ept->num, ept->in ? "in" : "out", status);
  1731. } else {
  1732. actual = req->length;
  1733. status = 0;
  1734. }
  1735. /* clean chache because CPU may prefetch request buffer to cache */
  1736. arch_clean_invalidate_cache_range((addr_t) req->buf, req->length);
  1737. if (req->complete)
  1738. req->complete(req, actual, status);
  1739. }
  1740. }
  1741. void mt_udc_irq(u32 ltssm, u32 intrusb, u32 dmaintr, u16 intrtx, u16 intrrx,
  1742. u32 intrqmu, u32 intrqmudone, u32 linkint) {
  1743. u32 temp = 0;
  1744. u32 ep_num = 0;
  1745. #if DBG_USB_IRQ
  1746. DBG_I("mt_udc_irq\n");
  1747. DBG_I("ltssm : %x\n", ltssm);
  1748. DBG_I("intrusb : %x\n", intrusb);
  1749. DBG_I("dmaintr : %x\n", dmaintr);
  1750. DBG_I("intrtx : %x\n", intrtx);
  1751. DBG_I("intrrx : %x\n", intrrx);
  1752. DBG_I("intrqmu : %x\n", intrqmu);
  1753. DBG_I("intrqmudone : %x\n", intrqmudone);
  1754. DBG_I("linkint : %x\n", linkint);
  1755. #ifdef DBG_EP0ISR
  1756. DBG_I("U3D_EP0CSR : %x\n", readl(U3D_EP0CSR));
  1757. DBG_I("USB_EPIER : %x\n", readl(U3D_EPIER));
  1758. DBG_I("USB_EPISR :%x\n", readl(U3D_EPISR));
  1759. DBG_I("U3D_DEVICE_CONF: %x\n", readl(U3D_DEVICE_CONF));
  1760. DBG_I("U3D_DEVICE_MONITOR: %x\n", readl(U3D_DEVICE_MONITOR));
  1761. #endif
  1762. #ifdef SBG_EP1ISR
  1763. DBG_I("[CSR] U3D_RX1CSR0: %p: val: %x\n", (void *)U3D_RX1CSR0, USB_READCSR32(U3D_RX1CSR0, 1));
  1764. DBG_I("[CSR] U3D_RX1CSR1: %p: val: %x\n", (void *)U3D_RX1CSR1, USB_READCSR32(U3D_RX1CSR1, 1));
  1765. DBG_I("[CSR] U3D_RX1CSR2: %p: val: %x\n", (void *)U3D_RX1CSR2, USB_READCSR32(U3D_RX1CSR2, 1));
  1766. DBG_I("[CSR] U3D_TX1CSR0: %p: val: %x\n", (void *)U3D_TX1CSR0, USB_READCSR32(U3D_TX1CSR0, 1));
  1767. DBG_I("[CSR] U3D_TX1CSR1: %p: val: %x\n", (void *)U3D_TX1CSR1, USB_READCSR32(U3D_TX1CSR1, 1));
  1768. DBG_I("[CSR] U3D_TX1CSR2: %p: val: %x\n", (void *)U3D_TX1CSR2, USB_READCSR32(U3D_TX1CSR2, 1));
  1769. #endif
  1770. #ifdef DBG_USB_QMU
  1771. DBG_I("U3D_RXQCSR1: %p, val: %x\n", (void *)U3D_RXQCSR(1), readl(U3D_RXQCSR(1)));
  1772. DBG_I("U3D_RXQSAR1: %p, val: %x\n", (void *)U3D_RXQSAR(1), readl(U3D_RXQSAR(1)));
  1773. DBG_I("U3D_RXQCPR1: %p, val: %x\n", (void *)U3D_RXQCPR(1), readl(U3D_RXQCPR(1)));
  1774. DBG_I("U3D_RXQLDPR1: %p, val: %x\n", (void *)U3D_RXQLDPR(1), readl(U3D_RXQLDPR(1)));
  1775. DBG_I("U3D_TXQCSR1: %p, val: %x\n", (void *)U3D_TXQCSR(1), readl(U3D_TXQCSR(1)));
  1776. DBG_I("U3D_TXQSAR1: %p, val: %x\n", (void *)U3D_TXQSAR(1), readl(U3D_TXQSAR(1)));
  1777. DBG_I("U3D_TXQCPR1: %p, val: %x\n", (void *)U3D_TXQCPR(1), readl(U3D_TXQCPR(1)));
  1778. #endif /* ifdef DBG_USB_QMU */
  1779. #endif /* ifdef DBG_USB_IRQ */
  1780. #ifdef SUPPORT_QMU
  1781. if (intrqmudone) {
  1782. qmu_done_interrupt(intrqmudone);
  1783. if (intrqmudone & QMU_RX_DONE(1)) {
  1784. mt_udc_epx_handler(1, USB_DIR_OUT);
  1785. }
  1786. if (intrqmudone & QMU_TX_DONE(1)) {
  1787. mt_udc_epx_handler(1, USB_DIR_IN);
  1788. }
  1789. }
  1790. if (intrqmu) {
  1791. qmu_handler(intrqmu);
  1792. }
  1793. #endif
  1794. if (linkint & SSUSB_DEV_SPEED_CHG_INTR) {
  1795. DBG_I("[INTR] Speed Change\n");
  1796. g_speed = mt_udc_get_speed();
  1797. mu3d_hal_set_speed(g_speed);
  1798. }
  1799. /* Check for reset interrupt */
  1800. if (intrusb & RESET_INTR) {
  1801. DBG_I("[INTR] Reset\n");
  1802. udelay(20);
  1803. /* set device address to 0 after reset */
  1804. set_address = 0;
  1805. intrtx = 0;
  1806. intrrx = 0;
  1807. }
  1808. #ifdef SUPPORT_U3
  1809. if (ltssm) {
  1810. if (ltssm & SS_DISABLE_INTR) {
  1811. DBG_I("[INTR] SS is Disabled, %d\n",
  1812. (readl(U3D_LTSSM_INFO) & DISABLE_CNT) >> DISABLE_CNT_OFST);
  1813. /* Set soft_conn to enable U2 termination */
  1814. mu3d_hal_u2dev_connect();
  1815. u3d_ep0en(); /* u3d_ep0en will update g_speed. */
  1816. mu3d_hal_set_speed(g_speed); /* do we really need set speed here ?? */
  1817. ltssm = 0;
  1818. }
  1819. #if defined(DBG_USB_LTSSM)
  1820. report_ltssm_type(ltssm);
  1821. #endif
  1822. if (ltssm & ENTER_U0_INTR) {
  1823. #if DBG_USB_IRQ
  1824. DBG_I("ENTER_U0_INTR");
  1825. #endif
  1826. mu3d_initialize_drv();
  1827. g_tx_fifo_addr = USB_TX_FIFO_START_ADDRESS;
  1828. g_rx_fifo_addr = USB_RX_FIFO_START_ADDRESS;
  1829. set_address = 0;
  1830. intrtx = 0;
  1831. intrrx = 0;
  1832. }
  1833. #ifndef POWER_SAVING_MODE
  1834. if (ltssm & U3_RESUME_INTR) {
  1835. DBG_I("[INTR] Resume \n");
  1836. clrbits(U3D_SSUSB_U3_CTRL_0P, SSUSB_U3_PORT_PDN);
  1837. clrbits(U3D_SSUSB_IP_PW_CTRL2, SSUSB_IP_DEV_PDN);
  1838. while (!(readl(U3D_SSUSB_IP_PW_STS1) & SSUSB_U3_MAC_RST_B_STS));
  1839. setbits(U3D_LINK_POWER_CONTROL, UX_EXIT);
  1840. }
  1841. #endif
  1842. } // ltssm
  1843. #endif // #if SUPPORT_U3
  1844. if (intrusb & DISCONN_INTR) {
  1845. DBG_I("[INTR] DISCONN_INTR\n");
  1846. }
  1847. if (intrusb & CONN_INTR) {
  1848. DBG_I("[INTR] CONN_INTR\n");
  1849. }
  1850. if (intrusb & SUSPEND_INTR) {
  1851. DBG_I("[INTR] SUSPEND_INTR\n");
  1852. /* mt_udc_suspend will also call stop qmu */
  1853. mt_udc_suspend();
  1854. /*
  1855. * work around: Only disconnect U2 will stop here,
  1856. * Deal with 1. U2->U2 or U2->U3
  1857. * we need to "reset" or to "clean" SSUSB fail counnter
  1858. * to enable U3 for next enumeration.
  1859. *
  1860. * Otherwise U3 cannot detect speed.
  1861. */
  1862. #if defined(USB_RESET_AFTER_SUSPEND)
  1863. mt_udc_reset(U3D_DFT_SPEED);
  1864. mt_usb_disconnect_internal();
  1865. /* soft connect U2 or U3 by g_speed */
  1866. mt_usb_connect_internal();
  1867. #else
  1868. mt_usb_disconnect_internal();
  1869. mu3d_hal_u3dev_connect();
  1870. #endif
  1871. }
  1872. //TODO: Possibly don't need to handle this interrupt
  1873. if (intrusb & LPM_INTR) {
  1874. DBG_I("[INTR] LPM Interrupt\n");
  1875. temp = readl(U3D_USB20_LPM_PARAM);
  1876. DBG_I("[INTR] %x, BESL: %x, x <= %x <= %x\n",
  1877. temp&0xf, (temp >> 8) & 0xf, (temp >> 12) & 0xf, (temp >> 4) & 0xf);
  1878. temp = readl(U3D_POWER_MANAGEMENT);
  1879. DBG_I("[RWP]: %x\n", (temp & LPM_RWP) >> 11);
  1880. //if (g_sw_rw)
  1881. {
  1882. // s/w LPM only
  1883. setbits(U3D_USB20_MISC_CONTROL, LPM_U3_ACK_EN);
  1884. //wait a while before remote wakeup, so xHCI PLS status is not affected
  1885. mdelay(20);
  1886. setbits(U3D_POWER_MANAGEMENT, RESUME);
  1887. DBG_I("RESUME: %d\n", readl(U3D_POWER_MANAGEMENT) & RESUME);
  1888. }
  1889. }
  1890. if (intrusb & LPM_RESUME_INTR) {
  1891. DBG_I("[INTR] LPM Resume\n");
  1892. if (!(readl(U3D_POWER_MANAGEMENT) & LPM_HRWE)) {
  1893. setbits(U3D_USB20_MISC_CONTROL, LPM_U3_ACK_EN);
  1894. }
  1895. }
  1896. /* Check for resume from suspend mode */
  1897. if (intrusb & RESUME_INTR) {
  1898. DBG_I("[INTR] Resume Interrupt\n");
  1899. }
  1900. #ifdef SUPPORT_DMA
  1901. if (dmaintr) {
  1902. u3d_dma_handler(dmaintr);
  1903. }
  1904. #endif
  1905. /* For EP0 */
  1906. if ((intrtx & 0x1) || (intrrx & 0x1)) {
  1907. if (intrrx & 0x1) {
  1908. DBG_I("Service SETUPEND");
  1909. intrrx = intrrx & ~0x1; //SETUPENDISR of EP0
  1910. }
  1911. mt_udc_ep0_handler();
  1912. intrtx = intrtx & ~0x1; //EPISR
  1913. //intrrx = intrrx & ~0x1; //SETUPENDISR of EP0
  1914. //ep0_state = EP0_IDLE;
  1915. }
  1916. /* For EPx (TX) */
  1917. if (intrtx) {
  1918. for (ep_num = 1; ep_num < MT_EP_NUM; ep_num++) {
  1919. if (intrtx & EPMASK(ep_num)) {
  1920. mt_udc_epx_handler(ep_num, USB_DIR_IN);
  1921. }
  1922. }
  1923. }
  1924. /* For EPx (RX) */
  1925. if (intrrx) {
  1926. for (ep_num = 1; ep_num < MT_EP_NUM; ep_num++) {
  1927. if (intrrx & EPMASK(ep_num)) {
  1928. mt_udc_epx_handler(ep_num, USB_DIR_OUT);
  1929. }
  1930. }
  1931. }
  1932. }
  1933. /* from usbtty.c */
  1934. void service_interrupts(void) {
  1935. u32 ltssm;
  1936. u32 intrusb;
  1937. u32 dmaintr;
  1938. u16 intrtx;
  1939. u16 intrrx;
  1940. u32 intrqmu;
  1941. u32 intrqmudone;
  1942. u32 linkint;
  1943. u32 intrep;
  1944. u32 lv1_isr;
  1945. /* give ltssm and intrusb initial value */
  1946. ltssm = 0;
  1947. intrusb = 0;
  1948. intrqmu = 0;
  1949. intrqmudone = 0;
  1950. /* read */
  1951. lv1_isr = readl(U3D_LV1ISR);
  1952. if (lv1_isr & MAC2_INTR) {
  1953. intrusb = readl(U3D_COMMON_USB_INTR) & readl(U3D_COMMON_USB_INTR_ENABLE);
  1954. }
  1955. #ifdef SUPPORT_U3
  1956. if (lv1_isr & MAC3_INTR) {
  1957. ltssm = readl(U3D_LTSSM_INTR) & readl(U3D_LTSSM_INTR_ENABLE);
  1958. }
  1959. #endif
  1960. dmaintr = readl(U3D_DMAISR) & readl(U3D_DMAIER);
  1961. intrep = readl(U3D_EPISR) & readl(U3D_EPIER);
  1962. #ifdef SUPPORT_QMU
  1963. intrqmu = readl(U3D_QISAR1);
  1964. intrqmudone = readl(U3D_QISAR0) & readl(U3D_QIER0);
  1965. #endif
  1966. intrtx = intrep & 0xffff;
  1967. intrrx = (intrep >> 16);
  1968. linkint = readl(U3D_DEV_LINK_INTR) & readl(U3D_DEV_LINK_INTR_ENABLE);
  1969. #if DBG_USB_IRQ
  1970. DBG_I("intrqmudone: %x\n",intrqmudone);
  1971. DBG_I("intrep: %x\n",intrep);
  1972. DBG_I("interrupt: intrusb [%x] intrtx[%x] intrrx [%x] intrdma[%x] intrqmu [%x] intrltssm [%x]\r\n"
  1973. , intrusb, intrtx, intrrx, dmaintr, intrqmu, ltssm);
  1974. #endif
  1975. if (!(intrusb || intrep || dmaintr || intrqmu || ltssm || intrqmudone)) {
  1976. DBG_I("[NULL INTR] REG_INTRL1 = 0x%08X\n",
  1977. (u32)lv1_isr);
  1978. }
  1979. #ifdef SUPPORT_QMU
  1980. writel(intrqmudone, U3D_QISAR0);
  1981. #endif
  1982. if (lv1_isr & MAC2_INTR) {
  1983. writel(intrusb, U3D_COMMON_USB_INTR);
  1984. }
  1985. #ifdef SUPPORT_U3
  1986. if (lv1_isr & MAC3_INTR) {
  1987. writel(ltssm, U3D_LTSSM_INTR);
  1988. }
  1989. #endif
  1990. writel(intrep, U3D_EPISR);
  1991. writel(linkint, U3D_DEV_LINK_INTR);
  1992. if (ltssm | intrusb | dmaintr | intrtx
  1993. | intrrx | intrqmu | intrqmudone | linkint) {
  1994. mt_udc_irq(ltssm, intrusb, dmaintr, intrtx, intrrx,
  1995. intrqmu, intrqmudone, linkint);
  1996. }
  1997. }
  1998. void lk_usb_scheduler(void) {
  1999. mt_irq_ack(MT_USB0_IRQ_ID);
  2000. service_interrupts();
  2001. return;
  2002. }
  2003. int mt_usb_irq_init(void) {
  2004. /* disable all endpoint interrupts */
  2005. mu3d_hal_disable_intr();
  2006. /* 2. Ack all gpt irq if needed */
  2007. //writel(0x3F, GPT_IRQ_ACK);
  2008. /* 3. Register usb irq */
  2009. mt_irq_set_sens(MT_USB0_IRQ_ID, MT65xx_LEVEL_SENSITIVE);
  2010. mt_irq_set_polarity(MT_USB0_IRQ_ID, MT65xx_POLARITY_LOW);
  2011. return 0;
  2012. }
  2013. /* Turn on the USB connection by enabling the pullup resistor */
  2014. void mt_usb_connect_internal(void) {
  2015. if (g_speed != SSUSB_SPEED_SUPER) {
  2016. mu3d_hal_u2dev_connect();
  2017. } else {
  2018. mu3d_hal_u3dev_connect();
  2019. }
  2020. }
  2021. /* Turn off the USB connection by disabling the pullup resistor */
  2022. void mt_usb_disconnect_internal(void) {
  2023. mu3d_hal_u2dev_disconnect();
  2024. mu3d_hal_u3dev_disconnect();
  2025. }
  2026. int udc_init(struct udc_device *dev) {
  2027. struct udc_descriptor *desc = NULL;
  2028. #ifdef USB_GINTR
  2029. #ifdef USB_HSDMA_ISR
  2030. u32 usb_dmaintr;
  2031. #endif
  2032. #endif
  2033. DBG_I("%s:\n", __func__);
  2034. DBG_I("ep0_urb: %p\n", ep0_urb);
  2035. /* RESET */
  2036. /*
  2037. * mt_usb_disconnect_internal can be removed:
  2038. * if we call udc_stop() in cmd_reboot and cmd_reboot_bootloader.
  2039. */
  2040. //mt_usb_disconnect_internal();
  2041. thread_sleep(20);
  2042. /* usb phy init */
  2043. board_usb_init();
  2044. thread_sleep(20);
  2045. /* allocate ep0 */
  2046. ep0out = _udc_endpoint_alloc(EP0, 0, EP0_MAX_PACKET_SIZE);
  2047. ep0in = _udc_endpoint_alloc(EP0, 1, EP0_MAX_PACKET_SIZE);
  2048. ep0req = udc_request_alloc();
  2049. ep0req->buf = malloc(4096);
  2050. ep0_urb->buf = ep0_buf;
  2051. /* create and register a language table descriptor */
  2052. /* language 0x0409 is US English */
  2053. desc = udc_descriptor_alloc(TYPE_STRING, EP0, 4);
  2054. desc->data[2] = 0x09;
  2055. desc->data[3] = 0x04;
  2056. udc_descriptor_register(desc);
  2057. #ifdef SUPPORT_U3
  2058. /* allocate this twice otherwise desc_list and desc_list_u3 will have 2 same head */
  2059. desc = udc_descriptor_alloc(TYPE_STRING, EP0, 4);
  2060. desc->data[2] = 0x09;
  2061. desc->data[3] = 0x04;
  2062. udc_descriptor_register_u3(desc);
  2063. #endif
  2064. #ifdef SUPPORT_QMU
  2065. mu3d_hal_alloc_qmu_mem();
  2066. #endif
  2067. #ifdef USB_HSDMA_ISR
  2068. /* setting HSDMA interrupt register */
  2069. usb_dmaintr = (0xff | 0xff << USB_DMA_INTR_UNMASK_SET_OFFSET);
  2070. writel(usb_dmaintr, USB_DMA_INTR);
  2071. #endif
  2072. the_device = dev;
  2073. return 0;
  2074. }
  2075. void udc_endpoint_free(struct udc_endpoint *ept) {
  2076. /* todo */
  2077. }
  2078. struct udc_request *udc_request_alloc(void) {
  2079. struct udc_request *req;
  2080. req = malloc(sizeof(*req));
  2081. if (!req)
  2082. panic("%s cannot allocate memory!\n", __func__);
  2083. req->buf = NULL;
  2084. req->length = 0;
  2085. return req;
  2086. }
  2087. void udc_request_free(struct udc_request *req) {
  2088. free(req);
  2089. }
  2090. /* Called to start packet transmission. */
  2091. /*
  2092. * It must be applied in udc_request_queue when polling mode is used.
  2093. * (When USB_GINTR is undefined).
  2094. * If interrupt mode is used, you can use
  2095. * mt_udc_epx_handler(ept->num, USB_DIR_IN); to replace mt_ep_write make ISR
  2096. * do it for you.
  2097. */
  2098. static int mt_ep_write(struct udc_endpoint *ept) {
  2099. int ep_num = ept->num;
  2100. int count;
  2101. u32 csr;
  2102. /* udc_endpoint_write: cannot write ep0 */
  2103. if (ep_num == 0)
  2104. return FALSE;
  2105. /* udc_endpoint_write: cannot write USB_DIR_OUT */
  2106. if (ept->in == 0) /* USB_DIR_OUT: val is 0x0; in == 0 here */
  2107. return FALSE;
  2108. csr = USB_READCSR32(U3D_TX1CSR0, ep_num);
  2109. if (csr & TX_TXPKTRDY) {
  2110. #if DBG_USB_GENERAL
  2111. DBG_I("udc_endpoint_write: ep%d is not ready to be written\n",
  2112. ep_num);
  2113. #endif
  2114. return FALSE;
  2115. }
  2116. count = mt_write_fifo(ept);
  2117. csr |= TX_TXPKTRDY;
  2118. USB_WRITECSR32(U3D_TX1CSR0, ep_num, csr);
  2119. return count;
  2120. }
  2121. int udc_request_queue(struct udc_endpoint *ept, struct udc_request *req) {
  2122. #ifdef SUPPORT_QMU
  2123. u8 *pbuf;
  2124. #endif
  2125. #ifdef SUPPORT_QMU
  2126. /* don't dump debug message here, will cause ISR fail */
  2127. /* work around for abnormal disconnect line during qmu transfer */
  2128. if (!usb_online)
  2129. return 0;
  2130. #endif
  2131. #if DBG_USB_GENERAL
  2132. DBG_I("%s: ept %d %s queue req=%p, req->length=%x\n",
  2133. __func__, ept->num, ept->in ? "in" : "out", req, req->length);
  2134. DBG_I("%s: ept %d: %p, ept->in: %s, ept->rcv_urb->buf: %p, ept->tx_urb->buf: %p, req->buf: %p\n",
  2135. __func__, ept->num, ept, ept->in ? "IN" : "OUT" , ept->rcv_urb->buf, ept->tx_urb->buf, req->buf);
  2136. #endif
  2137. enter_critical_section();
  2138. ept->req = req;
  2139. ept->status = 0; /* ACTIVE */
  2140. ept->sent = 0;
  2141. ept->last = 0;
  2142. /* read */
  2143. if (!ept->in) {
  2144. /* update dst buffer with request's address */
  2145. ept->rcv_urb->buf = req->buf;
  2146. ept->rcv_urb->actual_length = 0;
  2147. /* unmask EPx INTRRXE */
  2148. /*
  2149. * To avoid the parallely access the buffer,
  2150. * it is umasked here and umask at complete.
  2151. */
  2152. #ifdef SUPPORT_QMU /* For QMU, don't enable EP interrupts. */
  2153. pbuf = ept->rcv_urb->buf;
  2154. /* FASTBOOT COMMAND */
  2155. if (req->length <= GPD_BUF_SIZE_ALIGN) {
  2156. 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);
  2157. } else { /* FASTBOOT DATA */
  2158. DBG_C("udc_request exceeded the maximum QMU buffer size GPD_BUF_SIZE_ALIGN\n");
  2159. }
  2160. /* start transfer */
  2161. arch_clean_invalidate_cache_range((addr_t) ept->rcv_urb->buf, req->length);
  2162. mu3d_hal_resume_qmu(ept->num, USB_DIR_OUT);
  2163. #else /* For PIO, enable EP interrupts */
  2164. writel(readl(U3D_EPIER) | (BIT16 << ept->num), U3D_EPIESR);
  2165. #endif
  2166. }
  2167. /* write */
  2168. if (ept->in) {
  2169. ept->tx_urb->buf = req->buf;
  2170. ept->tx_urb->actual_length = req->length;
  2171. #ifdef SUPPORT_QMU /* For QMU, we don't call mt_ep_write() */
  2172. //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);
  2173. /* According to usb_read implementation on PC side, ZLP flag should not be set */
  2174. mu3d_hal_insert_transfer_gpd(ept->num, USB_DIR_IN, ept->tx_urb->buf, req->length, true, true, false, 0, ept->maxpkt);
  2175. arch_clean_invalidate_cache_range((addr_t) ept->tx_urb->buf, req->length);
  2176. mu3d_hal_resume_qmu(ept->num, USB_DIR_IN);
  2177. #else /* For PIO, call mt_ep_write() */
  2178. mt_ep_write(ept);
  2179. #endif
  2180. }
  2181. exit_critical_section();
  2182. return 0;
  2183. }
  2184. int udc_register_gadget(struct udc_gadget *gadget) {
  2185. if (the_gadget) {
  2186. DBG_C("only one gadget supported\n");
  2187. return FALSE;
  2188. }
  2189. the_gadget = gadget;
  2190. return 0;
  2191. }
  2192. static void udc_ept_desc_fill(struct udc_endpoint *ept, unsigned char *data) {
  2193. data[0] = 7;
  2194. data[1] = TYPE_ENDPOINT;
  2195. data[2] = ept->num | (ept->in ? USB_DIR_IN : USB_DIR_OUT);
  2196. data[3] = 0x02; /* bulk -- the only kind we support */
  2197. data[4] = 0x00; // ept->maxpkt; 512bytes
  2198. data[5] = 0x02; // ept->maxpkt >> 8; 512bytes
  2199. data[6] = ept->in ? 0x00 : 0x01;
  2200. }
  2201. #ifdef SUPPORT_U3
  2202. static void udc_ept_desc_fill_u3(struct udc_endpoint *ept, unsigned char *data) {
  2203. data[0] = 7;
  2204. data[1] = TYPE_ENDPOINT;
  2205. data[2] = ept->num | (ept->in ? USB_DIR_IN : USB_DIR_OUT);
  2206. data[3] = 0x02; /* bulk -- the only kind we support */
  2207. data[4] = 0x00; /* ept->maxpkt; 1024bytes */
  2208. data[5] = 0x04; /* ept->maxpkt >> 8; 1024bytes */
  2209. data[6] = ept->in ? 0x00 : 0x01;
  2210. }
  2211. #endif
  2212. static unsigned udc_ifc_desc_size(struct udc_gadget *g) {
  2213. return 9 + g->ifc_endpoints * 7;
  2214. }
  2215. static void udc_ifc_desc_fill(struct udc_gadget *g, unsigned char *data) {
  2216. unsigned n;
  2217. data[0] = 0x09;
  2218. data[1] = TYPE_INTERFACE;
  2219. data[2] = 0x00; /* ifc number */
  2220. data[3] = 0x00; /* alt number */
  2221. data[4] = g->ifc_endpoints; /* 0x02 */
  2222. data[5] = g->ifc_class; /* 0xff */
  2223. data[6] = g->ifc_subclass; /* 0x42 */
  2224. data[7] = g->ifc_protocol; /* 0x03 */
  2225. data[8] = udc_string_desc_alloc(g->ifc_string);
  2226. data += 9;
  2227. for (n = 0; n < g->ifc_endpoints; n++) {
  2228. udc_ept_desc_fill(g->ept[n], data);
  2229. data += 7;
  2230. }
  2231. }
  2232. #ifdef SUPPORT_U3
  2233. static void udc_companion_desc_fill(unsigned char *data) {
  2234. data[0] = 6;
  2235. data[1] = TYPE_SS_EP_COMP;
  2236. data[2] = 0x0f; /* max burst: 0x0~0xf */
  2237. data[3] = 0x00;
  2238. data[4] = 0x00;
  2239. data[5] = 0x00;
  2240. }
  2241. static unsigned udc_ifc_desc_size_u3(struct udc_gadget *g) {
  2242. return 9 + g->ifc_endpoints * (7 + 6); /* Endpoint + Companion desc */
  2243. }
  2244. static void udc_ifc_desc_fill_u3(struct udc_gadget *g, unsigned char *data) {
  2245. unsigned n;
  2246. data[0] = 0x09;
  2247. data[1] = TYPE_INTERFACE;
  2248. data[2] = 0x00; /* ifc number */
  2249. data[3] = 0x00; /* alt number */
  2250. data[4] = g->ifc_endpoints; /* 0x02 */
  2251. data[5] = g->ifc_class; /* 0xFF */
  2252. data[6] = g->ifc_subclass; /* 0x42 */
  2253. data[7] = g->ifc_protocol; /* 0x03 <-> ref: 0x01 */
  2254. data[8] = udc_string_desc_alloc_u3(g->ifc_string);
  2255. data += 9;
  2256. for (n = 0; n < g->ifc_endpoints; n++) {
  2257. udc_ept_desc_fill_u3(g->ept[n], data);
  2258. data += 7;
  2259. udc_companion_desc_fill(data);
  2260. data += 6;
  2261. }
  2262. }
  2263. static void udc_devcapa_desc_fill_u2(unsigned char *data) {
  2264. /* data 0-6 (BOS 5-11) */
  2265. data[0] = 0x07; /* bLength: 7 */
  2266. data[1] = 0x10; /* bDescriptorType: DEVICE CAPABILITY */
  2267. data[2] = 0x02; /* bDevCapabilityType: USB 2.0 Ext Descriptor */
  2268. data[3] = 0x02; /* bmAttributes[4]: LPM (SuperSpeed) */
  2269. data[4] = 0x00;
  2270. data[5] = 0x00;
  2271. data[6] = 0x00;
  2272. }
  2273. static void udc_devcapa_desc_fill_u3(unsigned char *data) {
  2274. /* data 0-9 (BOS 12-21) */
  2275. data[0] = 0x0A; /* bLength: 10 */
  2276. data[1] = 0x10; /* bDescriptorType: DEVICE CAPABILITY */
  2277. data[2] = 0x03; /* bDevCapabilityType: SuperSpeed */
  2278. data[3] = 0x00; /* bmAttributes: Don't support LTM */
  2279. data[4] = 0x0E; /* wSpeedsSupported[0]: b'1110 */
  2280. data[5] = 0x00; /* wSpeedsSupported[1] */
  2281. data[6] = 0x01; /* bFunctionalitySupport */
  2282. data[7] = 0x0A; /* bU1DevExitLat: Less than 10us */
  2283. data[8] = 0x20; /* wU2DevExitLat[0]: 32us */
  2284. data[9] = 0x00; /* wU2DevExitLat[1] */
  2285. }
  2286. #endif
  2287. int udc_start(void) {
  2288. struct udc_descriptor *desc;
  2289. unsigned char *data;
  2290. unsigned size;
  2291. if (!the_device) {
  2292. DBG_C("udc cannot start before init\n");
  2293. return FALSE;
  2294. }
  2295. if (!the_gadget) {
  2296. DBG_C("udc has no gadget registered\n");
  2297. return FALSE;
  2298. }
  2299. /* create our device descriptor - USB2 */
  2300. desc = udc_descriptor_alloc(TYPE_DEVICE, EP0, 18);
  2301. data = desc->data;
  2302. data[2] = 0x00; /* usb spec minor rev */
  2303. data[3] = 0x02; /* usb spec major rev */
  2304. data[4] = 0x00; /* class */
  2305. data[5] = 0x00; /* subclass */
  2306. data[6] = 0x00; /* protocol */
  2307. data[7] = 0x40; /* max packet size on ept 0 */
  2308. memcpy(data + 8, &the_device->vendor_id, sizeof(short));
  2309. memcpy(data + 10, &the_device->product_id, sizeof(short));
  2310. memcpy(data + 12, &the_device->version_id, sizeof(short));
  2311. data[14] = udc_string_desc_alloc(the_device->manufacturer);
  2312. data[15] = udc_string_desc_alloc(the_device->product);
  2313. data[16] = udc_string_desc_alloc(the_device->serialno);
  2314. data[17] = 1; /* number of configurations */
  2315. udc_descriptor_register(desc);
  2316. #ifdef SUPPORT_U3
  2317. /* create our device descriptor - USB3 */
  2318. desc = udc_descriptor_alloc(TYPE_DEVICE, EP0, 18);
  2319. data = desc->data;
  2320. data[2] = 0x00; /* usb spec minor rev */
  2321. data[3] = 0x03; /* usb spec major rev */
  2322. data[4] = 0x00; /* class */
  2323. data[5] = 0x00; /* subclass */
  2324. data[6] = 0x00; /* protocol */
  2325. data[7] = 0x09; /* max packet size on ept 0 (USB3) */
  2326. memcpy(data + 8, &the_device->vendor_id, sizeof(short));
  2327. memcpy(data + 10, &the_device->product_id, sizeof(short));
  2328. memcpy(data + 12, &the_device->version_id, sizeof(short));
  2329. data[14] = udc_string_desc_alloc_u3(the_device->manufacturer);
  2330. data[15] = udc_string_desc_alloc_u3(the_device->product);
  2331. data[16] = udc_string_desc_alloc_u3(the_device->serialno);
  2332. data[17] = 1; /* number of configurations */
  2333. udc_descriptor_register_u3(desc);
  2334. /* create our BOS Binary Device Object descriptor - USB3 */
  2335. desc = udc_descriptor_alloc(TYPE_BOS, EP0, 5);
  2336. data = desc->data;
  2337. /* wTotalLength: Length of this descriptor and all of its sub descriptors */
  2338. data[2] = 0x16; /* wTotalLength[0] */
  2339. data[3] = 0x00; /* wTotalLength[1] */
  2340. data[4] = 0x02; /* bNumDeviceCaps: number of separate device capability descriptors in BOS */
  2341. udc_descriptor_register_u3(desc);
  2342. /* create our BOS Binary Device Object descriptor - USB3 (FULL) */
  2343. desc = udc_descriptor_alloc(TYPE_BOS, EP0, 22);
  2344. data = desc->data;
  2345. data[0] = 0x05; /* bLength of BOS Header */
  2346. data[2] = 0x16; /* wTotalLength[0] */
  2347. data[3] = 0x00; /* wTotalLength[1] */
  2348. data[4] = 0x02; /* bNumDeviceCaps: number of separate device capability descriptors in BOS */
  2349. /* BOS 1 */
  2350. udc_devcapa_desc_fill_u2(data + 5);
  2351. /* BOS 2 */
  2352. udc_devcapa_desc_fill_u3(data + 5 + 7);
  2353. udc_descriptor_register_u3(desc);
  2354. #endif
  2355. /* create our configuration descriptor */
  2356. size = 9 + udc_ifc_desc_size(the_gadget);
  2357. desc = udc_descriptor_alloc(TYPE_CONFIGURATION, EP0, size);
  2358. data = desc->data;
  2359. data[0] = 0x09;
  2360. data[2] = size;
  2361. data[3] = size >> 8;
  2362. data[4] = 0x01; /* number of interfaces */
  2363. data[5] = 0x01; /* configuration value */
  2364. data[6] = 0x00; /* configuration string */
  2365. data[7] = 0x80; /* attributes */
  2366. data[8] = 0x80; /* max power (250ma) -- todo fix this */
  2367. udc_ifc_desc_fill(the_gadget, data + 9);
  2368. udc_descriptor_register(desc);
  2369. #ifdef SUPPORT_U3
  2370. /* create our configuration descriptor - USB3 */
  2371. size = 9 + udc_ifc_desc_size_u3(the_gadget);
  2372. desc = udc_descriptor_alloc(TYPE_CONFIGURATION, EP0, size);
  2373. data = desc->data;
  2374. data[0] = 0x09;
  2375. data[2] = size;
  2376. data[3] = size >> 8;
  2377. data[4] = 0x01; /* number of interfaces */
  2378. data[5] = 0x01; /* configuration value */
  2379. data[6] = 0x00; /* configuration string */
  2380. data[7] = 0x80; /* attrib-power: bus: 0x80, self: 0xC0 */
  2381. data[8] = 0x32; /* max power: bus: 0x32, self: 0x18 */
  2382. udc_ifc_desc_fill_u3(the_gadget, data + 9);
  2383. udc_descriptor_register_u3(desc);
  2384. #endif
  2385. #if DBG_USB_DUMP_DESC
  2386. DBG_I("%s: dump desc_list\n", __func__);
  2387. for (desc = desc_list; desc; desc = desc->next) {
  2388. DBG_I("tag: %04x\n", desc->tag);
  2389. DBG_I("len: %d\n", desc->len);
  2390. DBG_I("data:");
  2391. hexdump8(desc->data, desc->len);
  2392. }
  2393. #ifdef SUPPORT_U3
  2394. DBG_I("%s: dump desc_list_u3\n", __func__);
  2395. for (desc = desc_list_u3; desc; desc = desc->next) {
  2396. DBG_I("tag: %04x\n", desc->tag);
  2397. DBG_I("len: %d\n", desc->len);
  2398. DBG_I("data:");
  2399. hexdump8(desc->data, desc->len);
  2400. }
  2401. #endif /* ifdef SUPPORT_U3 */
  2402. #endif
  2403. /* register interrupt handler */
  2404. mt_usb_irq_init();
  2405. /* go to RUN mode */
  2406. mt_usb_phy_recover();
  2407. /* unmask usb irq */
  2408. #ifdef USB_GINTR
  2409. mt_irq_unmask(MT_USB0_IRQ_ID);
  2410. #endif
  2411. /* udc_enable end */
  2412. mt_udc_reset(U3D_DFT_SPEED);
  2413. udelay(100);
  2414. /* usb_disconnect */
  2415. mt_usb_disconnect_internal();
  2416. /* usb_connect */
  2417. mt_usb_connect_internal();
  2418. #ifndef USB_GINTR
  2419. while (1) {
  2420. service_interrupts();
  2421. }
  2422. #endif
  2423. return 0;
  2424. }
  2425. int udc_stop(void) {
  2426. thread_sleep(10);
  2427. mt_usb_disconnect_internal();
  2428. mt_usb_phy_savecurrent();
  2429. return 0;
  2430. }