mt_ssusb.c 72 KB

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