dwc_otg_pcd_intr.c 131 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828
  1. /* ==========================================================================
  2. * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd_intr.c $
  3. * $Revision: #113 $
  4. * $Date: 2011/10/24 $
  5. * $Change: 1871160 $
  6. *
  7. * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  8. * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  9. * otherwise expressly agreed to in writing between Synopsys and you.
  10. *
  11. * The Software IS NOT an item of Licensed Software or Licensed Product under
  12. * any End User Software License Agreement or Agreement for Licensed Product
  13. * with Synopsys or any supplement thereto. You are permitted to use and
  14. * redistribute this Software in source and binary forms, with or without
  15. * modification, provided that redistributions of source code must retain this
  16. * notice. You may not view, use, disclose, copy or distribute this file or
  17. * any information contained herein except pursuant to this license grant from
  18. * Synopsys. If you do not agree with this notice, including the disclaimer
  19. * below, then you are not authorized to use the Software.
  20. *
  21. * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  22. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  25. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  26. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  27. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  31. * DAMAGE.
  32. * ========================================================================== */
  33. #ifndef DWC_HOST_ONLY
  34. #include "dwc_otg_pcd.h"
  35. #ifdef DWC_UTE_CFI
  36. #include "dwc_otg_cfi.h"
  37. #endif
  38. #ifdef DWC_UTE_PER_IO
  39. extern void complete_xiso_ep(dwc_otg_pcd_ep_t * ep);
  40. #endif
  41. //#define PRINT_CFI_DMA_DESCS
  42. #define DEBUG_EP0
  43. /**
  44. * This function updates OTG.
  45. */
  46. static void dwc_otg_pcd_update_otg(dwc_otg_pcd_t * pcd, const unsigned reset)
  47. {
  48. if (reset) {
  49. pcd->b_hnp_enable = 0;
  50. pcd->a_hnp_support = 0;
  51. pcd->a_alt_hnp_support = 0;
  52. }
  53. if (pcd->fops->hnp_changed) {
  54. pcd->fops->hnp_changed(pcd);
  55. }
  56. }
  57. /** @file
  58. * This file contains the implementation of the PCD Interrupt handlers.
  59. *
  60. * The PCD handles the device interrupts. Many conditions can cause a
  61. * device interrupt. When an interrupt occurs, the device interrupt
  62. * service routine determines the cause of the interrupt and
  63. * dispatches handling to the appropriate function. These interrupt
  64. * handling functions are described below.
  65. * All interrupt registers are processed from LSB to MSB.
  66. */
  67. /**
  68. * This function prints the ep0 state for debug purposes.
  69. */
  70. static inline void print_ep0_state(dwc_otg_pcd_t * pcd)
  71. {
  72. #ifdef DEBUG
  73. char str[40];
  74. switch (pcd->ep0state) {
  75. case EP0_DISCONNECT:
  76. dwc_strcpy(str, "EP0_DISCONNECT");
  77. break;
  78. case EP0_IDLE:
  79. dwc_strcpy(str, "EP0_IDLE");
  80. break;
  81. case EP0_IN_DATA_PHASE:
  82. dwc_strcpy(str, "EP0_IN_DATA_PHASE");
  83. break;
  84. case EP0_OUT_DATA_PHASE:
  85. dwc_strcpy(str, "EP0_OUT_DATA_PHASE");
  86. break;
  87. case EP0_IN_STATUS_PHASE:
  88. dwc_strcpy(str, "EP0_IN_STATUS_PHASE");
  89. break;
  90. case EP0_OUT_STATUS_PHASE:
  91. dwc_strcpy(str, "EP0_OUT_STATUS_PHASE");
  92. break;
  93. case EP0_STALL:
  94. dwc_strcpy(str, "EP0_STALL");
  95. break;
  96. default:
  97. dwc_strcpy(str, "EP0_INVALID");
  98. }
  99. DWC_DEBUGPL(DBG_ANY, "%s(%d)\n", str, pcd->ep0state);
  100. #endif
  101. }
  102. /**
  103. * This function calculate the size of the payload in the memory
  104. * for out endpoints and prints size for debug purposes(used in
  105. * 2.93a DevOutNak feature).
  106. */
  107. static inline void print_memory_payload(dwc_otg_pcd_t * pcd, dwc_ep_t * ep)
  108. {
  109. #ifdef DEBUG
  110. deptsiz_data_t deptsiz_init = {.d32 = 0 };
  111. deptsiz_data_t deptsiz_updt = {.d32 = 0 };
  112. int pack_num;
  113. unsigned payload;
  114. deptsiz_init.d32 = pcd->core_if->start_doeptsiz_val[ep->num];
  115. deptsiz_updt.d32 =
  116. DWC_READ_REG32(&pcd->core_if->dev_if->
  117. out_ep_regs[ep->num]->doeptsiz);
  118. /* Payload will be */
  119. payload = deptsiz_init.b.xfersize - deptsiz_updt.b.xfersize;
  120. /* Packet count is decremented every time a packet
  121. * is written to the RxFIFO not in to the external memory
  122. * So, if payload == 0, then it means no packet was sent to ext memory*/
  123. pack_num = (!payload) ? 0 : (deptsiz_init.b.pktcnt - deptsiz_updt.b.pktcnt);
  124. DWC_DEBUGPL(DBG_PCDV,
  125. "Payload for EP%d-%s\n",
  126. ep->num, (ep->is_in ? "IN" : "OUT"));
  127. DWC_DEBUGPL(DBG_PCDV,
  128. "Number of transfered bytes = 0x%08x\n", payload);
  129. DWC_DEBUGPL(DBG_PCDV,
  130. "Number of transfered packets = %d\n", pack_num);
  131. #endif
  132. }
  133. #ifdef DWC_UTE_CFI
  134. static inline void print_desc(struct dwc_otg_dma_desc *ddesc,
  135. const uint8_t * epname, int descnum)
  136. {
  137. CFI_INFO
  138. ("%s DMA_DESC(%d) buf=0x%08x bytes=0x%04x; sp=0x%x; l=0x%x; sts=0x%02x; bs=0x%02x\n",
  139. epname, descnum, ddesc->buf, ddesc->status.b.bytes,
  140. ddesc->status.b.sp, ddesc->status.b.l, ddesc->status.b.sts,
  141. ddesc->status.b.bs);
  142. }
  143. #endif
  144. /**
  145. * This function returns pointer to in ep struct with number ep_num
  146. */
  147. static inline dwc_otg_pcd_ep_t *get_in_ep(dwc_otg_pcd_t * pcd, uint32_t ep_num)
  148. {
  149. int i;
  150. int num_in_eps = GET_CORE_IF(pcd)->dev_if->num_in_eps;
  151. if (ep_num == 0) {
  152. return &pcd->ep0;
  153. } else {
  154. for (i = 0; i < num_in_eps; ++i) {
  155. if (pcd->in_ep[i].dwc_ep.num == ep_num)
  156. return &pcd->in_ep[i];
  157. }
  158. return 0;
  159. }
  160. }
  161. /**
  162. * This function returns pointer to out ep struct with number ep_num
  163. */
  164. static inline dwc_otg_pcd_ep_t *get_out_ep(dwc_otg_pcd_t * pcd, uint32_t ep_num)
  165. {
  166. int i;
  167. int num_out_eps = GET_CORE_IF(pcd)->dev_if->num_out_eps;
  168. if (ep_num == 0) {
  169. return &pcd->ep0;
  170. } else {
  171. for (i = 0; i < num_out_eps; ++i) {
  172. if (pcd->out_ep[i].dwc_ep.num == ep_num)
  173. return &pcd->out_ep[i];
  174. }
  175. return 0;
  176. }
  177. }
  178. /**
  179. * This functions gets a pointer to an EP from the wIndex address
  180. * value of the control request.
  181. */
  182. dwc_otg_pcd_ep_t *get_ep_by_addr(dwc_otg_pcd_t * pcd, u16 wIndex)
  183. {
  184. dwc_otg_pcd_ep_t *ep;
  185. uint32_t ep_num = UE_GET_ADDR(wIndex);
  186. if (ep_num == 0) {
  187. ep = &pcd->ep0;
  188. } else if (UE_GET_DIR(wIndex) == UE_DIR_IN) { /* in ep */
  189. ep = &pcd->in_ep[ep_num - 1];
  190. } else {
  191. ep = &pcd->out_ep[ep_num - 1];
  192. }
  193. return ep;
  194. }
  195. /**
  196. * This function checks the EP request queue, if the queue is not
  197. * empty the next request is started.
  198. */
  199. void start_next_request(dwc_otg_pcd_ep_t * ep)
  200. {
  201. dwc_otg_pcd_request_t *req = 0;
  202. uint32_t max_transfer =
  203. GET_CORE_IF(ep->pcd)->core_params->max_transfer_size;
  204. #ifdef DWC_UTE_CFI
  205. struct dwc_otg_pcd *pcd;
  206. pcd = ep->pcd;
  207. #endif
  208. if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  209. req = DWC_CIRCLEQ_FIRST(&ep->queue);
  210. #ifdef DWC_UTE_CFI
  211. if (ep->dwc_ep.buff_mode != BM_STANDARD) {
  212. ep->dwc_ep.cfi_req_len = req->length;
  213. pcd->cfi->ops.build_descriptors(pcd->cfi, pcd, ep, req);
  214. } else {
  215. #endif
  216. /* Setup and start the Transfer */
  217. if (req->dw_align_buf) {
  218. ep->dwc_ep.dma_addr = req->dw_align_buf_dma;
  219. ep->dwc_ep.start_xfer_buff = req->dw_align_buf;
  220. ep->dwc_ep.xfer_buff = req->dw_align_buf;
  221. } else {
  222. ep->dwc_ep.dma_addr = req->dma;
  223. ep->dwc_ep.start_xfer_buff = req->buf;
  224. ep->dwc_ep.xfer_buff = req->buf;
  225. }
  226. ep->dwc_ep.sent_zlp = 0;
  227. ep->dwc_ep.total_len = req->length;
  228. ep->dwc_ep.xfer_len = 0;
  229. ep->dwc_ep.xfer_count = 0;
  230. ep->dwc_ep.maxxfer = max_transfer;
  231. if (GET_CORE_IF(ep->pcd)->dma_desc_enable) {
  232. uint32_t out_max_xfer = DDMA_MAX_TRANSFER_SIZE
  233. - (DDMA_MAX_TRANSFER_SIZE % 4);
  234. if (ep->dwc_ep.is_in) {
  235. if (ep->dwc_ep.maxxfer >
  236. DDMA_MAX_TRANSFER_SIZE) {
  237. ep->dwc_ep.maxxfer =
  238. DDMA_MAX_TRANSFER_SIZE;
  239. }
  240. } else {
  241. if (ep->dwc_ep.maxxfer > out_max_xfer) {
  242. ep->dwc_ep.maxxfer =
  243. out_max_xfer;
  244. }
  245. }
  246. }
  247. if (ep->dwc_ep.maxxfer < ep->dwc_ep.total_len) {
  248. ep->dwc_ep.maxxfer -=
  249. (ep->dwc_ep.maxxfer % ep->dwc_ep.maxpacket);
  250. }
  251. if (req->sent_zlp) {
  252. if ((ep->dwc_ep.total_len %
  253. ep->dwc_ep.maxpacket == 0)
  254. && (ep->dwc_ep.total_len != 0)) {
  255. ep->dwc_ep.sent_zlp = 1;
  256. }
  257. }
  258. #ifdef DWC_UTE_CFI
  259. }
  260. #endif
  261. dwc_otg_ep_start_transfer(GET_CORE_IF(ep->pcd), &ep->dwc_ep);
  262. } else if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  263. DWC_PRINTF("There are no more ISOC requests \n");
  264. ep->dwc_ep.frame_num = 0xFFFFFFFF;
  265. }
  266. }
  267. /**
  268. * This function handles the SOF Interrupts. At this time the SOF
  269. * Interrupt is disabled.
  270. */
  271. int32_t dwc_otg_pcd_handle_sof_intr(dwc_otg_pcd_t * pcd)
  272. {
  273. dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  274. gintsts_data_t gintsts;
  275. DWC_DEBUGPL(DBG_PCD, "SOF\n");
  276. /* Clear interrupt */
  277. gintsts.d32 = 0;
  278. gintsts.b.sofintr = 1;
  279. DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  280. return 1;
  281. }
  282. /**
  283. * This function handles the Rx Status Queue Level Interrupt, which
  284. * indicates that there is a least one packet in the Rx FIFO. The
  285. * packets are moved from the FIFO to memory, where they will be
  286. * processed when the Endpoint Interrupt Register indicates Transfer
  287. * Complete or SETUP Phase Done.
  288. *
  289. * Repeat the following until the Rx Status Queue is empty:
  290. * -# Read the Receive Status Pop Register (GRXSTSP) to get Packet
  291. * info
  292. * -# If Receive FIFO is empty then skip to step Clear the interrupt
  293. * and exit
  294. * -# If SETUP Packet call dwc_otg_read_setup_packet to copy the
  295. * SETUP data to the buffer
  296. * -# If OUT Data Packet call dwc_otg_read_packet to copy the data
  297. * to the destination buffer
  298. */
  299. int32_t dwc_otg_pcd_handle_rx_status_q_level_intr(dwc_otg_pcd_t * pcd)
  300. {
  301. dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  302. dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  303. gintmsk_data_t gintmask = {.d32 = 0 };
  304. device_grxsts_data_t status;
  305. dwc_otg_pcd_ep_t *ep;
  306. gintsts_data_t gintsts;
  307. #ifdef DEBUG
  308. static char *dpid_str[] = { "D0", "D2", "D1", "MDATA" };
  309. #endif
  310. //DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, _pcd);
  311. /* Disable the Rx Status Queue Level interrupt */
  312. gintmask.b.rxstsqlvl = 1;
  313. DWC_MODIFY_REG32(&global_regs->gintmsk, gintmask.d32, 0);
  314. /* Get the Status from the top of the FIFO */
  315. status.d32 = DWC_READ_REG32(&global_regs->grxstsp);
  316. DWC_DEBUGPL(DBG_PCD, "EP:%d BCnt:%d DPID:%s "
  317. "pktsts:%x Frame:%d(0x%0x)\n",
  318. status.b.epnum, status.b.bcnt,
  319. dpid_str[status.b.dpid],
  320. status.b.pktsts, status.b.fn, status.b.fn);
  321. /* Get pointer to EP structure */
  322. ep = get_out_ep(pcd, status.b.epnum);
  323. switch (status.b.pktsts) {
  324. case DWC_DSTS_GOUT_NAK:
  325. DWC_DEBUGPL(DBG_PCDV, "Global OUT NAK\n");
  326. break;
  327. case DWC_STS_DATA_UPDT:
  328. DWC_DEBUGPL(DBG_PCDV, "OUT Data Packet\n");
  329. if (status.b.bcnt && ep->dwc_ep.xfer_buff) {
  330. /** @todo NGS Check for buffer overflow? */
  331. dwc_otg_read_packet(core_if,
  332. ep->dwc_ep.xfer_buff,
  333. status.b.bcnt);
  334. ep->dwc_ep.xfer_count += status.b.bcnt;
  335. ep->dwc_ep.xfer_buff += status.b.bcnt;
  336. }
  337. break;
  338. case DWC_STS_XFER_COMP:
  339. DWC_DEBUGPL(DBG_PCDV, "OUT Complete\n");
  340. break;
  341. case DWC_DSTS_SETUP_COMP:
  342. #ifdef DEBUG_EP0
  343. DWC_DEBUGPL(DBG_PCDV, "Setup Complete\n");
  344. #endif
  345. break;
  346. case DWC_DSTS_SETUP_UPDT:
  347. dwc_otg_read_setup_packet(core_if, pcd->setup_pkt->d32);
  348. #ifdef DEBUG_EP0
  349. DWC_DEBUGPL(DBG_PCD,
  350. "SETUP PKT: %02x.%02x v%04x i%04x l%04x\n",
  351. pcd->setup_pkt->req.bmRequestType,
  352. pcd->setup_pkt->req.bRequest,
  353. UGETW(pcd->setup_pkt->req.wValue),
  354. UGETW(pcd->setup_pkt->req.wIndex),
  355. UGETW(pcd->setup_pkt->req.wLength));
  356. #endif
  357. ep->dwc_ep.xfer_count += status.b.bcnt;
  358. break;
  359. default:
  360. DWC_DEBUGPL(DBG_PCDV, "Invalid Packet Status (0x%0x)\n",
  361. status.b.pktsts);
  362. break;
  363. }
  364. /* Enable the Rx Status Queue Level interrupt */
  365. DWC_MODIFY_REG32(&global_regs->gintmsk, 0, gintmask.d32);
  366. /* Clear interrupt */
  367. gintsts.d32 = 0;
  368. gintsts.b.rxstsqlvl = 1;
  369. DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  370. //DWC_DEBUGPL(DBG_PCDV, "EXIT: %s\n", __func__);
  371. return 1;
  372. }
  373. /**
  374. * This function examines the Device IN Token Learning Queue to
  375. * determine the EP number of the last IN token received. This
  376. * implementation is for the Mass Storage device where there are only
  377. * 2 IN EPs (Control-IN and BULK-IN).
  378. *
  379. * The EP numbers for the first six IN Tokens are in DTKNQR1 and there
  380. * are 8 EP Numbers in each of the other possible DTKNQ Registers.
  381. *
  382. * @param core_if Programming view of DWC_otg controller.
  383. *
  384. */
  385. static inline int get_ep_of_last_in_token(dwc_otg_core_if_t * core_if)
  386. {
  387. dwc_otg_device_global_regs_t *dev_global_regs =
  388. core_if->dev_if->dev_global_regs;
  389. const uint32_t TOKEN_Q_DEPTH = core_if->hwcfg2.b.dev_token_q_depth;
  390. /* Number of Token Queue Registers */
  391. const int DTKNQ_REG_CNT = (TOKEN_Q_DEPTH + 7) / 8;
  392. dtknq1_data_t dtknqr1;
  393. uint32_t in_tkn_epnums[4];
  394. int ndx = 0;
  395. int i = 0;
  396. volatile uint32_t *addr = &dev_global_regs->dtknqr1;
  397. int epnum = 0;
  398. //DWC_DEBUGPL(DBG_PCD,"dev_token_q_depth=%d\n",TOKEN_Q_DEPTH);
  399. /* Read the DTKNQ Registers */
  400. for (i = 0; i < DTKNQ_REG_CNT; i++) {
  401. in_tkn_epnums[i] = DWC_READ_REG32(addr);
  402. DWC_DEBUGPL(DBG_PCDV, "DTKNQR%d=0x%08x\n", i + 1,
  403. in_tkn_epnums[i]);
  404. if (addr == &dev_global_regs->dvbusdis) {
  405. addr = &dev_global_regs->dtknqr3_dthrctl;
  406. } else {
  407. ++addr;
  408. }
  409. }
  410. /* Copy the DTKNQR1 data to the bit field. */
  411. dtknqr1.d32 = in_tkn_epnums[0];
  412. /* Get the EP numbers */
  413. in_tkn_epnums[0] = dtknqr1.b.epnums0_5;
  414. ndx = dtknqr1.b.intknwptr - 1;
  415. //DWC_DEBUGPL(DBG_PCDV,"ndx=%d\n",ndx);
  416. if (ndx == -1) {
  417. /** @todo Find a simpler way to calculate the max
  418. * queue position.*/
  419. int cnt = TOKEN_Q_DEPTH;
  420. if (TOKEN_Q_DEPTH <= 6) {
  421. cnt = TOKEN_Q_DEPTH - 1;
  422. } else if (TOKEN_Q_DEPTH <= 14) {
  423. cnt = TOKEN_Q_DEPTH - 7;
  424. } else if (TOKEN_Q_DEPTH <= 22) {
  425. cnt = TOKEN_Q_DEPTH - 15;
  426. } else {
  427. cnt = TOKEN_Q_DEPTH - 23;
  428. }
  429. epnum = (in_tkn_epnums[DTKNQ_REG_CNT - 1] >> (cnt * 4)) & 0xF;
  430. } else {
  431. if (ndx <= 5) {
  432. epnum = (in_tkn_epnums[0] >> (ndx * 4)) & 0xF;
  433. } else if (ndx <= 13) {
  434. ndx -= 6;
  435. epnum = (in_tkn_epnums[1] >> (ndx * 4)) & 0xF;
  436. } else if (ndx <= 21) {
  437. ndx -= 14;
  438. epnum = (in_tkn_epnums[2] >> (ndx * 4)) & 0xF;
  439. } else if (ndx <= 29) {
  440. ndx -= 22;
  441. epnum = (in_tkn_epnums[3] >> (ndx * 4)) & 0xF;
  442. }
  443. }
  444. //DWC_DEBUGPL(DBG_PCD,"epnum=%d\n",epnum);
  445. return epnum;
  446. }
  447. /**
  448. * This interrupt occurs when the non-periodic Tx FIFO is half-empty.
  449. * The active request is checked for the next packet to be loaded into
  450. * the non-periodic Tx FIFO.
  451. */
  452. int32_t dwc_otg_pcd_handle_np_tx_fifo_empty_intr(dwc_otg_pcd_t * pcd)
  453. {
  454. dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  455. dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  456. dwc_otg_dev_in_ep_regs_t *ep_regs;
  457. gnptxsts_data_t txstatus = {.d32 = 0 };
  458. gintsts_data_t gintsts;
  459. int epnum = 0;
  460. dwc_otg_pcd_ep_t *ep = 0;
  461. uint32_t len = 0;
  462. int dwords;
  463. /* Get the epnum from the IN Token Learning Queue. */
  464. epnum = get_ep_of_last_in_token(core_if);
  465. ep = get_in_ep(pcd, epnum);
  466. DWC_DEBUGPL(DBG_PCD, "NP TxFifo Empty: %d \n", epnum);
  467. ep_regs = core_if->dev_if->in_ep_regs[epnum];
  468. len = ep->dwc_ep.xfer_len - ep->dwc_ep.xfer_count;
  469. if (len > ep->dwc_ep.maxpacket) {
  470. len = ep->dwc_ep.maxpacket;
  471. }
  472. dwords = (len + 3) / 4;
  473. /* While there is space in the queue and space in the FIFO and
  474. * More data to tranfer, Write packets to the Tx FIFO */
  475. txstatus.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
  476. DWC_DEBUGPL(DBG_PCDV, "b4 GNPTXSTS=0x%08x\n", txstatus.d32);
  477. while (txstatus.b.nptxqspcavail > 0 &&
  478. txstatus.b.nptxfspcavail > dwords &&
  479. ep->dwc_ep.xfer_count < ep->dwc_ep.xfer_len) {
  480. /* Write the FIFO */
  481. dwc_otg_ep_write_packet(core_if, &ep->dwc_ep, 0);
  482. len = ep->dwc_ep.xfer_len - ep->dwc_ep.xfer_count;
  483. if (len > ep->dwc_ep.maxpacket) {
  484. len = ep->dwc_ep.maxpacket;
  485. }
  486. dwords = (len + 3) / 4;
  487. txstatus.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
  488. DWC_DEBUGPL(DBG_PCDV, "GNPTXSTS=0x%08x\n", txstatus.d32);
  489. }
  490. DWC_DEBUGPL(DBG_PCDV, "GNPTXSTS=0x%08x\n",
  491. DWC_READ_REG32(&global_regs->gnptxsts));
  492. /* Clear interrupt */
  493. gintsts.d32 = 0;
  494. gintsts.b.nptxfempty = 1;
  495. DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  496. return 1;
  497. }
  498. /**
  499. * This function is called when dedicated Tx FIFO Empty interrupt occurs.
  500. * The active request is checked for the next packet to be loaded into
  501. * apropriate Tx FIFO.
  502. */
  503. static int32_t write_empty_tx_fifo(dwc_otg_pcd_t * pcd, uint32_t epnum)
  504. {
  505. dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  506. dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  507. dwc_otg_dev_in_ep_regs_t *ep_regs;
  508. dtxfsts_data_t txstatus = {.d32 = 0 };
  509. dwc_otg_pcd_ep_t *ep = 0;
  510. uint32_t len = 0;
  511. int dwords;
  512. ep = get_in_ep(pcd, epnum);
  513. DWC_DEBUGPL(DBG_PCD, "Dedicated TxFifo Empty: %d \n", epnum);
  514. ep_regs = core_if->dev_if->in_ep_regs[epnum];
  515. len = ep->dwc_ep.xfer_len - ep->dwc_ep.xfer_count;
  516. if (len > ep->dwc_ep.maxpacket) {
  517. len = ep->dwc_ep.maxpacket;
  518. }
  519. dwords = (len + 3) / 4;
  520. /* While there is space in the queue and space in the FIFO and
  521. * More data to tranfer, Write packets to the Tx FIFO */
  522. txstatus.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts);
  523. DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", epnum, txstatus.d32);
  524. while (txstatus.b.txfspcavail > dwords &&
  525. ep->dwc_ep.xfer_count < ep->dwc_ep.xfer_len &&
  526. ep->dwc_ep.xfer_len != 0) {
  527. /* Write the FIFO */
  528. dwc_otg_ep_write_packet(core_if, &ep->dwc_ep, 0);
  529. len = ep->dwc_ep.xfer_len - ep->dwc_ep.xfer_count;
  530. if (len > ep->dwc_ep.maxpacket) {
  531. len = ep->dwc_ep.maxpacket;
  532. }
  533. dwords = (len + 3) / 4;
  534. txstatus.d32 =
  535. DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts);
  536. DWC_DEBUGPL(DBG_PCDV, "dtxfsts[%d]=0x%08x\n", epnum,
  537. txstatus.d32);
  538. }
  539. DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", epnum,
  540. DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts));
  541. return 1;
  542. }
  543. /**
  544. * This function is called when the Device is disconnected. It stops
  545. * any active requests and informs the Gadget driver of the
  546. * disconnect.
  547. */
  548. void dwc_otg_pcd_stop(dwc_otg_pcd_t * pcd)
  549. {
  550. int i, num_in_eps, num_out_eps;
  551. dwc_otg_pcd_ep_t *ep;
  552. gintmsk_data_t intr_mask = {.d32 = 0 };
  553. DWC_SPINLOCK(pcd->lock);
  554. num_in_eps = GET_CORE_IF(pcd)->dev_if->num_in_eps;
  555. num_out_eps = GET_CORE_IF(pcd)->dev_if->num_out_eps;
  556. DWC_DEBUGPL(DBG_PCDV, "%s() \n", __func__);
  557. /* don't disconnect drivers more than once */
  558. if (pcd->ep0state == EP0_DISCONNECT) {
  559. DWC_DEBUGPL(DBG_ANY, "%s() Already Disconnected\n", __func__);
  560. DWC_SPINUNLOCK(pcd->lock);
  561. return;
  562. }
  563. pcd->ep0state = EP0_DISCONNECT;
  564. /* Reset the OTG state. */
  565. dwc_otg_pcd_update_otg(pcd, 1);
  566. /* Disable the NP Tx Fifo Empty Interrupt. */
  567. intr_mask.b.nptxfempty = 1;
  568. DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  569. intr_mask.d32, 0);
  570. /* Flush the FIFOs */
  571. /**@todo NGS Flush Periodic FIFOs */
  572. dwc_otg_flush_tx_fifo(GET_CORE_IF(pcd), 0x10);
  573. dwc_otg_flush_rx_fifo(GET_CORE_IF(pcd));
  574. /* prevent new request submissions, kill any outstanding requests */
  575. ep = &pcd->ep0;
  576. dwc_otg_request_nuke(ep);
  577. /* prevent new request submissions, kill any outstanding requests */
  578. for (i = 0; i < num_in_eps; i++) {
  579. dwc_otg_pcd_ep_t *ep = &pcd->in_ep[i];
  580. dwc_otg_request_nuke(ep);
  581. }
  582. /* prevent new request submissions, kill any outstanding requests */
  583. for (i = 0; i < num_out_eps; i++) {
  584. dwc_otg_pcd_ep_t *ep = &pcd->out_ep[i];
  585. dwc_otg_request_nuke(ep);
  586. }
  587. /* report disconnect; the driver is already quiesced */
  588. if (pcd->fops->disconnect) {
  589. DWC_SPINUNLOCK(pcd->lock);
  590. pcd->fops->disconnect(pcd);
  591. DWC_SPINLOCK(pcd->lock);
  592. }
  593. DWC_SPINUNLOCK(pcd->lock);
  594. }
  595. /**
  596. * This interrupt indicates that ...
  597. */
  598. int32_t dwc_otg_pcd_handle_i2c_intr(dwc_otg_pcd_t * pcd)
  599. {
  600. gintmsk_data_t intr_mask = {.d32 = 0 };
  601. gintsts_data_t gintsts;
  602. DWC_PRINTF("INTERRUPT Handler not implemented for %s\n", "i2cintr");
  603. intr_mask.b.i2cintr = 1;
  604. DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  605. intr_mask.d32, 0);
  606. /* Clear interrupt */
  607. gintsts.d32 = 0;
  608. gintsts.b.i2cintr = 1;
  609. DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  610. gintsts.d32);
  611. return 1;
  612. }
  613. /**
  614. * This interrupt indicates that ...
  615. */
  616. int32_t dwc_otg_pcd_handle_early_suspend_intr(dwc_otg_pcd_t * pcd)
  617. {
  618. gintsts_data_t gintsts;
  619. #if defined(VERBOSE)
  620. DWC_PRINTF("Early Suspend Detected\n");
  621. #endif
  622. /* Clear interrupt */
  623. gintsts.d32 = 0;
  624. gintsts.b.erlysuspend = 1;
  625. DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  626. gintsts.d32);
  627. return 1;
  628. }
  629. /**
  630. * This function configures EPO to receive SETUP packets.
  631. *
  632. * @todo NGS: Update the comments from the HW FS.
  633. *
  634. * -# Program the following fields in the endpoint specific registers
  635. * for Control OUT EP 0, in order to receive a setup packet
  636. * - DOEPTSIZ0.Packet Count = 3 (To receive up to 3 back to back
  637. * setup packets)
  638. * - DOEPTSIZE0.Transfer Size = 24 Bytes (To receive up to 3 back
  639. * to back setup packets)
  640. * - In DMA mode, DOEPDMA0 Register with a memory address to
  641. * store any setup packets received
  642. *
  643. * @param core_if Programming view of DWC_otg controller.
  644. * @param pcd Programming view of the PCD.
  645. */
  646. static inline void ep0_out_start(dwc_otg_core_if_t * core_if,
  647. dwc_otg_pcd_t * pcd)
  648. {
  649. dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  650. deptsiz0_data_t doeptsize0 = {.d32 = 0 };
  651. dwc_otg_dev_dma_desc_t *dma_desc;
  652. depctl_data_t doepctl = {.d32 = 0 };
  653. #ifdef VERBOSE
  654. DWC_DEBUGPL(DBG_PCDV, "%s() doepctl0=%0x\n", __func__,
  655. DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl));
  656. #endif
  657. doeptsize0.b.supcnt = 3;
  658. doeptsize0.b.pktcnt = 1;
  659. doeptsize0.b.xfersize = 8 * 3;
  660. if (core_if->dma_enable) {
  661. if (!core_if->dma_desc_enable) {
  662. /** put here as for Hermes mode deptisz register should not be written */
  663. DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doeptsiz,
  664. doeptsize0.d32);
  665. /** @todo dma needs to handle multiple setup packets (up to 3) */
  666. DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doepdma,
  667. pcd->setup_pkt_dma_handle);
  668. } else {
  669. dev_if->setup_desc_index =
  670. (dev_if->setup_desc_index + 1) & 1;
  671. dma_desc =
  672. dev_if->setup_desc_addr[dev_if->setup_desc_index];
  673. /** DMA Descriptor Setup */
  674. dma_desc->status.b.bs = BS_HOST_BUSY;
  675. dma_desc->status.b.l = 1;
  676. dma_desc->status.b.ioc = 1;
  677. dma_desc->status.b.bytes = pcd->ep0.dwc_ep.maxpacket;
  678. dma_desc->buf = pcd->setup_pkt_dma_handle;
  679. dma_desc->status.b.sts = 0;
  680. dma_desc->status.b.bs = BS_HOST_READY;
  681. /** DOEPDMA0 Register write */
  682. DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doepdma,
  683. dev_if->
  684. dma_setup_desc_addr
  685. [dev_if->setup_desc_index]);
  686. }
  687. } else {
  688. /** put here as for Hermes mode deptisz register should not be written */
  689. DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doeptsiz,
  690. doeptsize0.d32);
  691. }
  692. /** DOEPCTL0 Register write */
  693. doepctl.b.epena = 1;
  694. doepctl.b.cnak = 1;
  695. DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doepctl, doepctl.d32);
  696. #ifdef VERBOSE
  697. DWC_DEBUGPL(DBG_PCDV, "doepctl0=%0x\n",
  698. DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl));
  699. DWC_DEBUGPL(DBG_PCDV, "diepctl0=%0x\n",
  700. DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl));
  701. #endif
  702. }
  703. /**
  704. * This interrupt occurs when a USB Reset is detected. When the USB
  705. * Reset Interrupt occurs the device state is set to DEFAULT and the
  706. * EP0 state is set to IDLE.
  707. * -# Set the NAK bit for all OUT endpoints (DOEPCTLn.SNAK = 1)
  708. * -# Unmask the following interrupt bits
  709. * - DAINTMSK.INEP0 = 1 (Control 0 IN endpoint)
  710. * - DAINTMSK.OUTEP0 = 1 (Control 0 OUT endpoint)
  711. * - DOEPMSK.SETUP = 1
  712. * - DOEPMSK.XferCompl = 1
  713. * - DIEPMSK.XferCompl = 1
  714. * - DIEPMSK.TimeOut = 1
  715. * -# Program the following fields in the endpoint specific registers
  716. * for Control OUT EP 0, in order to receive a setup packet
  717. * - DOEPTSIZ0.Packet Count = 3 (To receive up to 3 back to back
  718. * setup packets)
  719. * - DOEPTSIZE0.Transfer Size = 24 Bytes (To receive up to 3 back
  720. * to back setup packets)
  721. * - In DMA mode, DOEPDMA0 Register with a memory address to
  722. * store any setup packets received
  723. * At this point, all the required initialization, except for enabling
  724. * the control 0 OUT endpoint is done, for receiving SETUP packets.
  725. */
  726. int32_t dwc_otg_pcd_handle_usb_reset_intr(dwc_otg_pcd_t * pcd)
  727. {
  728. dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  729. dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  730. depctl_data_t doepctl = {.d32 = 0 };
  731. depctl_data_t diepctl = {.d32 = 0 };
  732. depctl_data_t diepctl_rd = {.d32 = 0};
  733. daint_data_t daintmsk = {.d32 = 0 };
  734. doepmsk_data_t doepmsk = {.d32 = 0 };
  735. diepmsk_data_t diepmsk = {.d32 = 0 };
  736. dcfg_data_t dcfg = {.d32 = 0 };
  737. grstctl_t resetctl = {.d32 = 0 };
  738. dctl_data_t dctl = {.d32 = 0 };
  739. int i = 0;
  740. gintsts_data_t gintsts;
  741. pcgcctl_data_t power = {.d32 = 0 };
  742. power.d32 = DWC_READ_REG32(core_if->pcgcctl);
  743. if (power.b.stoppclk) {
  744. power.d32 = 0;
  745. power.b.stoppclk = 1;
  746. DWC_MODIFY_REG32(core_if->pcgcctl, power.d32, 0);
  747. power.b.pwrclmp = 1;
  748. DWC_MODIFY_REG32(core_if->pcgcctl, power.d32, 0);
  749. power.b.rstpdwnmodule = 1;
  750. DWC_MODIFY_REG32(core_if->pcgcctl, power.d32, 0);
  751. }
  752. core_if->lx_state = DWC_OTG_L0;
  753. DWC_PRINTF("USB RESET\n");
  754. #ifdef DWC_EN_ISOC
  755. for (i = 1; i < 16; ++i) {
  756. dwc_otg_pcd_ep_t *ep;
  757. dwc_ep_t *dwc_ep;
  758. ep = get_in_ep(pcd, i);
  759. if (ep != 0) {
  760. dwc_ep = &ep->dwc_ep;
  761. dwc_ep->next_frame = 0xffffffff;
  762. }
  763. }
  764. #endif /* DWC_EN_ISOC */
  765. /* reset the HNP settings */
  766. dwc_otg_pcd_update_otg(pcd, 1);
  767. /* Clear the Remote Wakeup Signalling */
  768. dctl.b.rmtwkupsig = 1;
  769. DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32, 0);
  770. /* Disable all active IN EPs */
  771. diepctl.b.epdis = 1;
  772. diepctl.b.snak = 1;
  773. for (i=0; i < dev_if->num_in_eps; i++) {
  774. diepctl_rd.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  775. if (diepctl_rd.b.epena) {
  776. DWC_WRITE_REG32( &dev_if->in_ep_regs[i]->diepctl,
  777. diepctl.d32 );
  778. }
  779. }
  780. /* Set NAK for all OUT EPs */
  781. doepctl.b.snak = 1;
  782. for (i = 0; i <= dev_if->num_out_eps; i++) {
  783. DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepctl, doepctl.d32);
  784. }
  785. /* Flush the NP Tx FIFO */
  786. dwc_otg_flush_tx_fifo(core_if, 0x10);
  787. /* Flush the Learning Queue */
  788. resetctl.b.intknqflsh = 1;
  789. DWC_WRITE_REG32(&core_if->core_global_regs->grstctl, resetctl.d32);
  790. if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable) {
  791. core_if->start_predict = 0;
  792. for (i = 0; i<= core_if->dev_if->num_in_eps; ++i) {
  793. core_if->nextep_seq[i] = 0xff; // 0xff - EP not active
  794. }
  795. core_if->nextep_seq[0] = 0;
  796. core_if->first_in_nextep_seq = 0;
  797. diepctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl);
  798. diepctl.b.nextep = 0;
  799. DWC_WRITE_REG32(&dev_if->in_ep_regs[0]->diepctl, diepctl.d32);
  800. /* Update IN Endpoint Mismatch Count by active IN NP EP count + 1 */
  801. dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
  802. dcfg.b.epmscnt = 2;
  803. DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
  804. DWC_DEBUGPL(DBG_PCDV,"%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
  805. __func__, core_if->first_in_nextep_seq);
  806. for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
  807. DWC_DEBUGPL(DBG_PCDV, "%2d\n", core_if->nextep_seq[i]);
  808. }
  809. }
  810. if (core_if->multiproc_int_enable) {
  811. daintmsk.b.inep0 = 1;
  812. daintmsk.b.outep0 = 1;
  813. DWC_WRITE_REG32(&dev_if->dev_global_regs->deachintmsk,
  814. daintmsk.d32);
  815. doepmsk.b.setup = 1;
  816. doepmsk.b.xfercompl = 1;
  817. doepmsk.b.ahberr = 1;
  818. doepmsk.b.epdisabled = 1;
  819. if (core_if->dma_desc_enable) {
  820. doepmsk.b.stsphsercvd = 1;
  821. doepmsk.b.bna = 1;
  822. }
  823. /*
  824. doepmsk.b.babble = 1;
  825. doepmsk.b.nyet = 1;
  826. if (core_if->dma_enable) {
  827. doepmsk.b.nak = 1;
  828. }
  829. */
  830. DWC_WRITE_REG32(&dev_if->dev_global_regs->doepeachintmsk[0],
  831. doepmsk.d32);
  832. diepmsk.b.xfercompl = 1;
  833. diepmsk.b.timeout = 1;
  834. diepmsk.b.epdisabled = 1;
  835. diepmsk.b.ahberr = 1;
  836. diepmsk.b.intknepmis = 1;
  837. if (!core_if->en_multiple_tx_fifo && core_if->dma_enable)
  838. diepmsk.b.intknepmis = 0;
  839. /* if (core_if->dma_desc_enable) {
  840. diepmsk.b.bna = 1;
  841. }
  842. */
  843. /*
  844. if (core_if->dma_enable) {
  845. diepmsk.b.nak = 1;
  846. }
  847. */
  848. DWC_WRITE_REG32(&dev_if->dev_global_regs->diepeachintmsk[0],
  849. diepmsk.d32);
  850. } else {
  851. daintmsk.b.inep0 = 1;
  852. daintmsk.b.outep0 = 1;
  853. DWC_WRITE_REG32(&dev_if->dev_global_regs->daintmsk,
  854. daintmsk.d32);
  855. doepmsk.b.setup = 1;
  856. doepmsk.b.xfercompl = 1;
  857. doepmsk.b.ahberr = 1;
  858. doepmsk.b.epdisabled = 1;
  859. if (core_if->dma_desc_enable) {
  860. doepmsk.b.stsphsercvd = 1;
  861. doepmsk.b.bna = 1;
  862. }
  863. DWC_WRITE_REG32(&dev_if->dev_global_regs->doepmsk, doepmsk.d32);
  864. diepmsk.b.xfercompl = 1;
  865. diepmsk.b.timeout = 1;
  866. diepmsk.b.epdisabled = 1;
  867. diepmsk.b.ahberr = 1;
  868. if (!core_if->en_multiple_tx_fifo && core_if->dma_enable)
  869. diepmsk.b.intknepmis = 0;
  870. /*
  871. if (core_if->dma_desc_enable) {
  872. diepmsk.b.bna = 1;
  873. }
  874. */
  875. DWC_WRITE_REG32(&dev_if->dev_global_regs->diepmsk, diepmsk.d32);
  876. }
  877. /* Reset Device Address */
  878. dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
  879. dcfg.b.devaddr = 0;
  880. DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
  881. /* setup EP0 to receive SETUP packets */
  882. ep0_out_start(core_if, pcd);
  883. /* Clear interrupt */
  884. gintsts.d32 = 0;
  885. gintsts.b.usbreset = 1;
  886. DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  887. return 1;
  888. }
  889. /**
  890. * Get the device speed from the device status register and convert it
  891. * to USB speed constant.
  892. *
  893. * @param core_if Programming view of DWC_otg controller.
  894. */
  895. static int get_device_speed(dwc_otg_core_if_t * core_if)
  896. {
  897. dsts_data_t dsts;
  898. int speed = 0;
  899. dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  900. switch (dsts.b.enumspd) {
  901. case DWC_DSTS_ENUMSPD_HS_PHY_30MHZ_OR_60MHZ:
  902. speed = USB_SPEED_HIGH;
  903. break;
  904. case DWC_DSTS_ENUMSPD_FS_PHY_30MHZ_OR_60MHZ:
  905. case DWC_DSTS_ENUMSPD_FS_PHY_48MHZ:
  906. speed = USB_SPEED_FULL;
  907. break;
  908. case DWC_DSTS_ENUMSPD_LS_PHY_6MHZ:
  909. speed = USB_SPEED_LOW;
  910. break;
  911. }
  912. return speed;
  913. }
  914. /**
  915. * Read the device status register and set the device speed in the
  916. * data structure.
  917. * Set up EP0 to receive SETUP packets by calling dwc_ep0_activate.
  918. */
  919. int32_t dwc_otg_pcd_handle_enum_done_intr(dwc_otg_pcd_t * pcd)
  920. {
  921. dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  922. gintsts_data_t gintsts;
  923. gusbcfg_data_t gusbcfg;
  924. dwc_otg_core_global_regs_t *global_regs =
  925. GET_CORE_IF(pcd)->core_global_regs;
  926. uint8_t utmi16b, utmi8b;
  927. int speed;
  928. DWC_DEBUGPL(DBG_PCD, "SPEED ENUM\n");
  929. if (GET_CORE_IF(pcd)->snpsid >= OTG_CORE_REV_2_60a) {
  930. utmi16b = 6; //vahrama old value was 6;
  931. utmi8b = 9;
  932. } else {
  933. utmi16b = 4;
  934. utmi8b = 8;
  935. }
  936. dwc_otg_ep0_activate(GET_CORE_IF(pcd), &ep0->dwc_ep);
  937. #ifdef DEBUG_EP0
  938. print_ep0_state(pcd);
  939. #endif
  940. if (pcd->ep0state == EP0_DISCONNECT) {
  941. pcd->ep0state = EP0_IDLE;
  942. } else if (pcd->ep0state == EP0_STALL) {
  943. pcd->ep0state = EP0_IDLE;
  944. }
  945. pcd->ep0state = EP0_IDLE;
  946. ep0->stopped = 0;
  947. speed = get_device_speed(GET_CORE_IF(pcd));
  948. pcd->fops->connect(pcd, speed);
  949. /* Set USB turnaround time based on device speed and PHY interface. */
  950. gusbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  951. if (speed == USB_SPEED_HIGH) {
  952. if (GET_CORE_IF(pcd)->hwcfg2.b.hs_phy_type ==
  953. DWC_HWCFG2_HS_PHY_TYPE_ULPI) {
  954. /* ULPI interface */
  955. gusbcfg.b.usbtrdtim = 9;
  956. }
  957. if (GET_CORE_IF(pcd)->hwcfg2.b.hs_phy_type ==
  958. DWC_HWCFG2_HS_PHY_TYPE_UTMI) {
  959. /* UTMI+ interface */
  960. if (GET_CORE_IF(pcd)->hwcfg4.b.utmi_phy_data_width == 0) {
  961. gusbcfg.b.usbtrdtim = utmi8b;
  962. } else if (GET_CORE_IF(pcd)->hwcfg4.
  963. b.utmi_phy_data_width == 1) {
  964. gusbcfg.b.usbtrdtim = utmi16b;
  965. } else if (GET_CORE_IF(pcd)->
  966. core_params->phy_utmi_width == 8) {
  967. gusbcfg.b.usbtrdtim = utmi8b;
  968. } else {
  969. gusbcfg.b.usbtrdtim = utmi16b;
  970. }
  971. }
  972. if (GET_CORE_IF(pcd)->hwcfg2.b.hs_phy_type ==
  973. DWC_HWCFG2_HS_PHY_TYPE_UTMI_ULPI) {
  974. /* UTMI+ OR ULPI interface */
  975. if (gusbcfg.b.ulpi_utmi_sel == 1) {
  976. /* ULPI interface */
  977. gusbcfg.b.usbtrdtim = 9;
  978. } else {
  979. /* UTMI+ interface */
  980. if (GET_CORE_IF(pcd)->
  981. core_params->phy_utmi_width == 16) {
  982. gusbcfg.b.usbtrdtim = utmi16b;
  983. } else {
  984. gusbcfg.b.usbtrdtim = utmi8b;
  985. }
  986. }
  987. }
  988. } else {
  989. /* Full or low speed */
  990. gusbcfg.b.usbtrdtim = 9;
  991. }
  992. DWC_WRITE_REG32(&global_regs->gusbcfg, gusbcfg.d32);
  993. /* Clear interrupt */
  994. gintsts.d32 = 0;
  995. gintsts.b.enumdone = 1;
  996. DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  997. gintsts.d32);
  998. return 1;
  999. }
  1000. /**
  1001. * This interrupt indicates that the ISO OUT Packet was dropped due to
  1002. * Rx FIFO full or Rx Status Queue Full. If this interrupt occurs
  1003. * read all the data from the Rx FIFO.
  1004. */
  1005. int32_t dwc_otg_pcd_handle_isoc_out_packet_dropped_intr(dwc_otg_pcd_t * pcd)
  1006. {
  1007. gintmsk_data_t intr_mask = {.d32 = 0 };
  1008. gintsts_data_t gintsts;
  1009. DWC_WARN("INTERRUPT Handler not implemented for %s\n",
  1010. "ISOC Out Dropped");
  1011. intr_mask.b.isooutdrop = 1;
  1012. DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  1013. intr_mask.d32, 0);
  1014. /* Clear interrupt */
  1015. gintsts.d32 = 0;
  1016. gintsts.b.isooutdrop = 1;
  1017. DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  1018. gintsts.d32);
  1019. return 1;
  1020. }
  1021. /**
  1022. * This interrupt indicates the end of the portion of the micro-frame
  1023. * for periodic transactions. If there is a periodic transaction for
  1024. * the next frame, load the packets into the EP periodic Tx FIFO.
  1025. */
  1026. int32_t dwc_otg_pcd_handle_end_periodic_frame_intr(dwc_otg_pcd_t * pcd)
  1027. {
  1028. gintmsk_data_t intr_mask = {.d32 = 0 };
  1029. gintsts_data_t gintsts;
  1030. DWC_PRINTF("INTERRUPT Handler not implemented for %s\n", "EOP");
  1031. intr_mask.b.eopframe = 1;
  1032. DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  1033. intr_mask.d32, 0);
  1034. /* Clear interrupt */
  1035. gintsts.d32 = 0;
  1036. gintsts.b.eopframe = 1;
  1037. DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  1038. gintsts.d32);
  1039. return 1;
  1040. }
  1041. /**
  1042. * This interrupt indicates that EP of the packet on the top of the
  1043. * non-periodic Tx FIFO does not match EP of the IN Token received.
  1044. *
  1045. * The "Device IN Token Queue" Registers are read to determine the
  1046. * order the IN Tokens have been received. The non-periodic Tx FIFO
  1047. * is flushed, so it can be reloaded in the order seen in the IN Token
  1048. * Queue.
  1049. */
  1050. int32_t dwc_otg_pcd_handle_ep_mismatch_intr(dwc_otg_pcd_t * pcd)
  1051. {
  1052. gintsts_data_t gintsts;
  1053. dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  1054. dctl_data_t dctl;
  1055. gintmsk_data_t intr_mask = {.d32 = 0 };
  1056. if (!core_if->en_multiple_tx_fifo && core_if->dma_enable) {
  1057. core_if->start_predict = 1;
  1058. DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, core_if);
  1059. gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  1060. if (!gintsts.b.ginnakeff) {
  1061. /* Disable EP Mismatch interrupt */
  1062. intr_mask.d32 = 0;
  1063. intr_mask.b.epmismatch = 1;
  1064. DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, intr_mask.d32, 0);
  1065. /* Enable the Global IN NAK Effective Interrupt */
  1066. intr_mask.d32 = 0;
  1067. intr_mask.b.ginnakeff = 1;
  1068. DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0, intr_mask.d32);
  1069. /* Set the global non-periodic IN NAK handshake */
  1070. dctl.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
  1071. dctl.b.sgnpinnak = 1;
  1072. DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32);
  1073. } else {
  1074. DWC_PRINTF("gintsts.b.ginnakeff = 1! dctl.b.sgnpinnak not set\n");
  1075. }
  1076. /* Disabling of all EP's will be done in dwc_otg_pcd_handle_in_nak_effective()
  1077. * handler after Global IN NAK Effective interrupt will be asserted */
  1078. }
  1079. /* Clear interrupt */
  1080. gintsts.d32 = 0;
  1081. gintsts.b.epmismatch = 1;
  1082. DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  1083. return 1;
  1084. }
  1085. /**
  1086. * This interrupt is valid only in DMA mode. This interrupt indicates that the
  1087. * core has stopped fetching data for IN endpoints due to the unavailability of
  1088. * TxFIFO space or Request Queue space. This interrupt is used by the
  1089. * application for an endpoint mismatch algorithm.
  1090. *
  1091. * @param pcd The PCD
  1092. */
  1093. int32_t dwc_otg_pcd_handle_ep_fetsusp_intr(dwc_otg_pcd_t * pcd)
  1094. {
  1095. gintsts_data_t gintsts;
  1096. gintmsk_data_t gintmsk_data;
  1097. dctl_data_t dctl;
  1098. dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  1099. DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, core_if);
  1100. /* Clear the global non-periodic IN NAK handshake */
  1101. dctl.d32 = 0;
  1102. dctl.b.cgnpinnak = 1;
  1103. DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
  1104. /* Mask GINTSTS.FETSUSP interrupt */
  1105. gintmsk_data.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
  1106. gintmsk_data.b.fetsusp = 0;
  1107. DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gintmsk_data.d32);
  1108. /* Clear interrupt */
  1109. gintsts.d32 = 0;
  1110. gintsts.b.fetsusp = 1;
  1111. DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  1112. return 1;
  1113. }
  1114. /**
  1115. * This funcion stalls EP0.
  1116. */
  1117. static inline void ep0_do_stall(dwc_otg_pcd_t * pcd, const int err_val)
  1118. {
  1119. dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  1120. usb_device_request_t *ctrl = &pcd->setup_pkt->req;
  1121. DWC_WARN("req %02x.%02x protocol STALL; err %d\n",
  1122. ctrl->bmRequestType, ctrl->bRequest, err_val);
  1123. ep0->dwc_ep.is_in = 1;
  1124. dwc_otg_ep_set_stall(GET_CORE_IF(pcd), &ep0->dwc_ep);
  1125. pcd->ep0.stopped = 1;
  1126. pcd->ep0state = EP0_IDLE;
  1127. ep0_out_start(GET_CORE_IF(pcd), pcd);
  1128. }
  1129. /**
  1130. * This functions delegates the setup command to the gadget driver.
  1131. */
  1132. static inline void do_gadget_setup(dwc_otg_pcd_t * pcd,
  1133. usb_device_request_t * ctrl)
  1134. {
  1135. int ret = 0;
  1136. DWC_SPINUNLOCK(pcd->lock);
  1137. ret = pcd->fops->setup(pcd, (uint8_t *) ctrl);
  1138. DWC_SPINLOCK(pcd->lock);
  1139. if (ret < 0) {
  1140. ep0_do_stall(pcd, ret);
  1141. }
  1142. /** @todo This is a g_file_storage gadget driver specific
  1143. * workaround: a DELAYED_STATUS result from the fsg_setup
  1144. * routine will result in the gadget queueing a EP0 IN status
  1145. * phase for a two-stage control transfer. Exactly the same as
  1146. * a SET_CONFIGURATION/SET_INTERFACE except that this is a class
  1147. * specific request. Need a generic way to know when the gadget
  1148. * driver will queue the status phase. Can we assume when we
  1149. * call the gadget driver setup() function that it will always
  1150. * queue and require the following flag? Need to look into
  1151. * this.
  1152. */
  1153. if (ret == 256 + 999) {
  1154. pcd->request_config = 1;
  1155. }
  1156. }
  1157. #ifdef DWC_UTE_CFI
  1158. /**
  1159. * This functions delegates the CFI setup commands to the gadget driver.
  1160. * This function will return a negative value to indicate a failure.
  1161. */
  1162. static inline int cfi_gadget_setup(dwc_otg_pcd_t * pcd,
  1163. struct cfi_usb_ctrlrequest *ctrl_req)
  1164. {
  1165. int ret = 0;
  1166. if (pcd->fops && pcd->fops->cfi_setup) {
  1167. DWC_SPINUNLOCK(pcd->lock);
  1168. ret = pcd->fops->cfi_setup(pcd, ctrl_req);
  1169. DWC_SPINLOCK(pcd->lock);
  1170. if (ret < 0) {
  1171. ep0_do_stall(pcd, ret);
  1172. return ret;
  1173. }
  1174. }
  1175. return ret;
  1176. }
  1177. #endif
  1178. /**
  1179. * This function starts the Zero-Length Packet for the IN status phase
  1180. * of a 2 stage control transfer.
  1181. */
  1182. static inline void do_setup_in_status_phase(dwc_otg_pcd_t * pcd)
  1183. {
  1184. dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  1185. if (pcd->ep0state == EP0_STALL) {
  1186. return;
  1187. }
  1188. pcd->ep0state = EP0_IN_STATUS_PHASE;
  1189. /* Prepare for more SETUP Packets */
  1190. DWC_DEBUGPL(DBG_PCD, "EP0 IN ZLP\n");
  1191. ep0->dwc_ep.xfer_len = 0;
  1192. ep0->dwc_ep.xfer_count = 0;
  1193. ep0->dwc_ep.is_in = 1;
  1194. ep0->dwc_ep.dma_addr = pcd->setup_pkt_dma_handle;
  1195. dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd), &ep0->dwc_ep);
  1196. /* Prepare for more SETUP Packets */
  1197. //ep0_out_start(GET_CORE_IF(pcd), pcd);
  1198. }
  1199. /**
  1200. * This function starts the Zero-Length Packet for the OUT status phase
  1201. * of a 2 stage control transfer.
  1202. */
  1203. static inline void do_setup_out_status_phase(dwc_otg_pcd_t * pcd)
  1204. {
  1205. dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  1206. if (pcd->ep0state == EP0_STALL) {
  1207. DWC_DEBUGPL(DBG_PCD, "EP0 STALLED\n");
  1208. return;
  1209. }
  1210. pcd->ep0state = EP0_OUT_STATUS_PHASE;
  1211. DWC_DEBUGPL(DBG_PCD, "EP0 OUT ZLP\n");
  1212. ep0->dwc_ep.xfer_len = 0;
  1213. ep0->dwc_ep.xfer_count = 0;
  1214. ep0->dwc_ep.is_in = 0;
  1215. ep0->dwc_ep.dma_addr = pcd->setup_pkt_dma_handle;
  1216. dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd), &ep0->dwc_ep);
  1217. /* Prepare for more SETUP Packets */
  1218. if (GET_CORE_IF(pcd)->dma_enable == 0) {
  1219. ep0_out_start(GET_CORE_IF(pcd), pcd);
  1220. }
  1221. }
  1222. /**
  1223. * Clear the EP halt (STALL) and if pending requests start the
  1224. * transfer.
  1225. */
  1226. static inline void pcd_clear_halt(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * ep)
  1227. {
  1228. if (ep->dwc_ep.stall_clear_flag == 0)
  1229. dwc_otg_ep_clear_stall(GET_CORE_IF(pcd), &ep->dwc_ep);
  1230. /* Reactive the EP */
  1231. dwc_otg_ep_activate(GET_CORE_IF(pcd), &ep->dwc_ep);
  1232. if (ep->stopped) {
  1233. ep->stopped = 0;
  1234. /* If there is a request in the EP queue start it */
  1235. /** @todo FIXME: this causes an EP mismatch in DMA mode.
  1236. * epmismatch not yet implemented. */
  1237. /*
  1238. * Above fixme is solved by implmenting a tasklet to call the
  1239. * start_next_request(), outside of interrupt context at some
  1240. * time after the current time, after a clear-halt setup packet.
  1241. * Still need to implement ep mismatch in the future if a gadget
  1242. * ever uses more than one endpoint at once
  1243. */
  1244. ep->queue_sof = 1;
  1245. DWC_TASK_SCHEDULE(pcd->start_xfer_tasklet);
  1246. }
  1247. /* Start Control Status Phase */
  1248. do_setup_in_status_phase(pcd);
  1249. }
  1250. /**
  1251. * This function is called when the SET_FEATURE TEST_MODE Setup packet
  1252. * is sent from the host. The Device Control register is written with
  1253. * the Test Mode bits set to the specified Test Mode. This is done as
  1254. * a tasklet so that the "Status" phase of the control transfer
  1255. * completes before transmitting the TEST packets.
  1256. *
  1257. * @todo This has not been tested since the tasklet struct was put
  1258. * into the PCD struct!
  1259. *
  1260. */
  1261. void do_test_mode(void *data)
  1262. {
  1263. dctl_data_t dctl;
  1264. dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) data;
  1265. dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  1266. int test_mode = pcd->test_mode;
  1267. // DWC_WARN("%s() has not been tested since being rewritten!\n", __func__);
  1268. dctl.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
  1269. switch (test_mode) {
  1270. case 1: // TEST_J
  1271. dctl.b.tstctl = 1;
  1272. break;
  1273. case 2: // TEST_K
  1274. dctl.b.tstctl = 2;
  1275. break;
  1276. case 3: // TEST_SE0_NAK
  1277. dctl.b.tstctl = 3;
  1278. break;
  1279. case 4: // TEST_PACKET
  1280. dctl.b.tstctl = 4;
  1281. break;
  1282. case 5: // TEST_FORCE_ENABLE
  1283. dctl.b.tstctl = 5;
  1284. break;
  1285. }
  1286. DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32);
  1287. }
  1288. /**
  1289. * This function process the GET_STATUS Setup Commands.
  1290. */
  1291. static inline void do_get_status(dwc_otg_pcd_t * pcd)
  1292. {
  1293. usb_device_request_t ctrl = pcd->setup_pkt->req;
  1294. dwc_otg_pcd_ep_t *ep;
  1295. dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  1296. uint16_t *status = pcd->status_buf;
  1297. dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  1298. #ifdef DEBUG_EP0
  1299. DWC_DEBUGPL(DBG_PCD,
  1300. "GET_STATUS %02x.%02x v%04x i%04x l%04x\n",
  1301. ctrl.bmRequestType, ctrl.bRequest,
  1302. UGETW(ctrl.wValue), UGETW(ctrl.wIndex),
  1303. UGETW(ctrl.wLength));
  1304. #endif
  1305. switch (UT_GET_RECIPIENT(ctrl.bmRequestType)) {
  1306. case UT_DEVICE:
  1307. if(UGETW(ctrl.wIndex) == 0xF000) { /* OTG Status selector */
  1308. DWC_PRINTF("wIndex - %d\n", UGETW(ctrl.wIndex));
  1309. DWC_PRINTF("OTG VERSION - %d\n", core_if->otg_ver);
  1310. DWC_PRINTF("OTG CAP - %d, %d\n", core_if->core_params->otg_cap,
  1311. DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE);
  1312. if(core_if->otg_ver == 1 &&
  1313. core_if->core_params->otg_cap == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE) {
  1314. uint8_t *otgsts = (uint8_t*)pcd->status_buf;
  1315. *otgsts = (core_if->otg_sts & 0x1);
  1316. pcd->ep0_pending = 1;
  1317. ep0->dwc_ep.start_xfer_buff = (uint8_t *) otgsts;
  1318. ep0->dwc_ep.xfer_buff = (uint8_t *) otgsts;
  1319. ep0->dwc_ep.dma_addr = pcd->status_buf_dma_handle;
  1320. ep0->dwc_ep.xfer_len = 1;
  1321. ep0->dwc_ep.xfer_count = 0;
  1322. ep0->dwc_ep.total_len = ep0->dwc_ep.xfer_len;
  1323. dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd), &ep0->dwc_ep);
  1324. return;
  1325. } else {
  1326. ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  1327. return;
  1328. }
  1329. break;
  1330. } else {
  1331. *status = 0x1; /* Self powered */
  1332. *status |= pcd->remote_wakeup_enable << 1;
  1333. break;
  1334. }
  1335. case UT_INTERFACE:
  1336. *status = 0;
  1337. break;
  1338. case UT_ENDPOINT:
  1339. ep = get_ep_by_addr(pcd, UGETW(ctrl.wIndex));
  1340. if (ep == 0 || UGETW(ctrl.wLength) > 2) {
  1341. ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  1342. return;
  1343. }
  1344. /** @todo check for EP stall */
  1345. *status = ep->stopped;
  1346. break;
  1347. }
  1348. pcd->ep0_pending = 1;
  1349. ep0->dwc_ep.start_xfer_buff = (uint8_t *) status;
  1350. ep0->dwc_ep.xfer_buff = (uint8_t *) status;
  1351. ep0->dwc_ep.dma_addr = pcd->status_buf_dma_handle;
  1352. ep0->dwc_ep.xfer_len = 2;
  1353. ep0->dwc_ep.xfer_count = 0;
  1354. ep0->dwc_ep.total_len = ep0->dwc_ep.xfer_len;
  1355. dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd), &ep0->dwc_ep);
  1356. }
  1357. /**
  1358. * This function process the SET_FEATURE Setup Commands.
  1359. */
  1360. static inline void do_set_feature(dwc_otg_pcd_t * pcd)
  1361. {
  1362. dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  1363. dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  1364. usb_device_request_t ctrl = pcd->setup_pkt->req;
  1365. dwc_otg_pcd_ep_t *ep = 0;
  1366. int32_t otg_cap_param = core_if->core_params->otg_cap;
  1367. gotgctl_data_t gotgctl = {.d32 = 0 };
  1368. DWC_DEBUGPL(DBG_PCD, "SET_FEATURE:%02x.%02x v%04x i%04x l%04x\n",
  1369. ctrl.bmRequestType, ctrl.bRequest,
  1370. UGETW(ctrl.wValue), UGETW(ctrl.wIndex),
  1371. UGETW(ctrl.wLength));
  1372. DWC_DEBUGPL(DBG_PCD, "otg_cap=%d\n", otg_cap_param);
  1373. switch (UT_GET_RECIPIENT(ctrl.bmRequestType)) {
  1374. case UT_DEVICE:
  1375. switch (UGETW(ctrl.wValue)) {
  1376. case UF_DEVICE_REMOTE_WAKEUP:
  1377. pcd->remote_wakeup_enable = 1;
  1378. break;
  1379. case UF_TEST_MODE:
  1380. /* Setup the Test Mode tasklet to do the Test
  1381. * Packet generation after the SETUP Status
  1382. * phase has completed. */
  1383. /** @todo This has not been tested since the
  1384. * tasklet struct was put into the PCD
  1385. * struct! */
  1386. pcd->test_mode = UGETW(ctrl.wIndex) >> 8;
  1387. DWC_TASK_SCHEDULE(pcd->test_mode_tasklet);
  1388. break;
  1389. case UF_DEVICE_B_HNP_ENABLE:
  1390. DWC_DEBUGPL(DBG_PCDV,
  1391. "SET_FEATURE: USB_DEVICE_B_HNP_ENABLE\n");
  1392. /* dev may initiate HNP */
  1393. if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE) {
  1394. pcd->b_hnp_enable = 1;
  1395. dwc_otg_pcd_update_otg(pcd, 0);
  1396. DWC_DEBUGPL(DBG_PCD, "Request B HNP\n");
  1397. /**@todo Is the gotgctl.devhnpen cleared
  1398. * by a USB Reset? */
  1399. gotgctl.b.devhnpen = 1;
  1400. gotgctl.b.hnpreq = 1;
  1401. DWC_WRITE_REG32(&global_regs->gotgctl,
  1402. gotgctl.d32);
  1403. } else {
  1404. ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  1405. return;
  1406. }
  1407. break;
  1408. case UF_DEVICE_A_HNP_SUPPORT:
  1409. /* RH port supports HNP */
  1410. DWC_DEBUGPL(DBG_PCDV,
  1411. "SET_FEATURE: USB_DEVICE_A_HNP_SUPPORT\n");
  1412. if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE) {
  1413. pcd->a_hnp_support = 1;
  1414. dwc_otg_pcd_update_otg(pcd, 0);
  1415. } else {
  1416. ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  1417. return;
  1418. }
  1419. break;
  1420. case UF_DEVICE_A_ALT_HNP_SUPPORT:
  1421. /* other RH port does */
  1422. DWC_DEBUGPL(DBG_PCDV,
  1423. "SET_FEATURE: USB_DEVICE_A_ALT_HNP_SUPPORT\n");
  1424. if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE) {
  1425. pcd->a_alt_hnp_support = 1;
  1426. dwc_otg_pcd_update_otg(pcd, 0);
  1427. } else {
  1428. ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  1429. return;
  1430. }
  1431. break;
  1432. default:
  1433. ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  1434. return;
  1435. }
  1436. do_setup_in_status_phase(pcd);
  1437. break;
  1438. case UT_INTERFACE:
  1439. do_gadget_setup(pcd, &ctrl);
  1440. break;
  1441. case UT_ENDPOINT:
  1442. if (UGETW(ctrl.wValue) == UF_ENDPOINT_HALT) {
  1443. ep = get_ep_by_addr(pcd, UGETW(ctrl.wIndex));
  1444. if (ep == 0) {
  1445. ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  1446. return;
  1447. }
  1448. ep->stopped = 1;
  1449. dwc_otg_ep_set_stall(core_if, &ep->dwc_ep);
  1450. }
  1451. do_setup_in_status_phase(pcd);
  1452. break;
  1453. }
  1454. }
  1455. /**
  1456. * This function process the CLEAR_FEATURE Setup Commands.
  1457. */
  1458. static inline void do_clear_feature(dwc_otg_pcd_t * pcd)
  1459. {
  1460. usb_device_request_t ctrl = pcd->setup_pkt->req;
  1461. dwc_otg_pcd_ep_t *ep = 0;
  1462. DWC_DEBUGPL(DBG_PCD,
  1463. "CLEAR_FEATURE:%02x.%02x v%04x i%04x l%04x\n",
  1464. ctrl.bmRequestType, ctrl.bRequest,
  1465. UGETW(ctrl.wValue), UGETW(ctrl.wIndex),
  1466. UGETW(ctrl.wLength));
  1467. switch (UT_GET_RECIPIENT(ctrl.bmRequestType)) {
  1468. case UT_DEVICE:
  1469. switch (UGETW(ctrl.wValue)) {
  1470. case UF_DEVICE_REMOTE_WAKEUP:
  1471. pcd->remote_wakeup_enable = 0;
  1472. break;
  1473. case UF_TEST_MODE:
  1474. /** @todo Add CLEAR_FEATURE for TEST modes. */
  1475. break;
  1476. default:
  1477. ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  1478. return;
  1479. }
  1480. do_setup_in_status_phase(pcd);
  1481. break;
  1482. case UT_ENDPOINT:
  1483. ep = get_ep_by_addr(pcd, UGETW(ctrl.wIndex));
  1484. if (ep == 0) {
  1485. ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  1486. return;
  1487. }
  1488. pcd_clear_halt(pcd, ep);
  1489. break;
  1490. }
  1491. }
  1492. /**
  1493. * This function process the SET_ADDRESS Setup Commands.
  1494. */
  1495. static inline void do_set_address(dwc_otg_pcd_t * pcd)
  1496. {
  1497. dwc_otg_dev_if_t *dev_if = GET_CORE_IF(pcd)->dev_if;
  1498. usb_device_request_t ctrl = pcd->setup_pkt->req;
  1499. if (ctrl.bmRequestType == UT_DEVICE) {
  1500. dcfg_data_t dcfg = {.d32 = 0 };
  1501. #ifdef DEBUG_EP0
  1502. // DWC_DEBUGPL(DBG_PCDV, "SET_ADDRESS:%d\n", ctrl.wValue);
  1503. #endif
  1504. dcfg.b.devaddr = UGETW(ctrl.wValue);
  1505. DWC_MODIFY_REG32(&dev_if->dev_global_regs->dcfg, 0, dcfg.d32);
  1506. do_setup_in_status_phase(pcd);
  1507. }
  1508. }
  1509. /**
  1510. * This function processes SETUP commands. In Linux, the USB Command
  1511. * processing is done in two places - the first being the PCD and the
  1512. * second in the Gadget Driver (for example, the File-Backed Storage
  1513. * Gadget Driver).
  1514. *
  1515. * <table>
  1516. * <tr><td>Command </td><td>Driver </td><td>Description</td></tr>
  1517. *
  1518. * <tr><td>GET_STATUS </td><td>PCD </td><td>Command is processed as
  1519. * defined in chapter 9 of the USB 2.0 Specification chapter 9
  1520. * </td></tr>
  1521. *
  1522. * <tr><td>CLEAR_FEATURE </td><td>PCD </td><td>The Device and Endpoint
  1523. * requests are the ENDPOINT_HALT feature is procesed, all others the
  1524. * interface requests are ignored.</td></tr>
  1525. *
  1526. * <tr><td>SET_FEATURE </td><td>PCD </td><td>The Device and Endpoint
  1527. * requests are processed by the PCD. Interface requests are passed
  1528. * to the Gadget Driver.</td></tr>
  1529. *
  1530. * <tr><td>SET_ADDRESS </td><td>PCD </td><td>Program the DCFG reg,
  1531. * with device address received </td></tr>
  1532. *
  1533. * <tr><td>GET_DESCRIPTOR </td><td>Gadget Driver </td><td>Return the
  1534. * requested descriptor</td></tr>
  1535. *
  1536. * <tr><td>SET_DESCRIPTOR </td><td>Gadget Driver </td><td>Optional -
  1537. * not implemented by any of the existing Gadget Drivers.</td></tr>
  1538. *
  1539. * <tr><td>SET_CONFIGURATION </td><td>Gadget Driver </td><td>Disable
  1540. * all EPs and enable EPs for new configuration.</td></tr>
  1541. *
  1542. * <tr><td>GET_CONFIGURATION </td><td>Gadget Driver </td><td>Return
  1543. * the current configuration</td></tr>
  1544. *
  1545. * <tr><td>SET_INTERFACE </td><td>Gadget Driver </td><td>Disable all
  1546. * EPs and enable EPs for new configuration.</td></tr>
  1547. *
  1548. * <tr><td>GET_INTERFACE </td><td>Gadget Driver </td><td>Return the
  1549. * current interface.</td></tr>
  1550. *
  1551. * <tr><td>SYNC_FRAME </td><td>PCD </td><td>Display debug
  1552. * message.</td></tr>
  1553. * </table>
  1554. *
  1555. * When the SETUP Phase Done interrupt occurs, the PCD SETUP commands are
  1556. * processed by pcd_setup. Calling the Function Driver's setup function from
  1557. * pcd_setup processes the gadget SETUP commands.
  1558. */
  1559. static inline void pcd_setup(dwc_otg_pcd_t * pcd)
  1560. {
  1561. dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  1562. dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  1563. usb_device_request_t ctrl = pcd->setup_pkt->req;
  1564. dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  1565. deptsiz0_data_t doeptsize0 = {.d32 = 0 };
  1566. #ifdef DWC_UTE_CFI
  1567. int retval = 0;
  1568. struct cfi_usb_ctrlrequest cfi_req;
  1569. #endif
  1570. #ifdef DEBUG_EP0
  1571. DWC_DEBUGPL(DBG_PCD, "SETUP %02x.%02x v%04x i%04x l%04x\n",
  1572. ctrl.bmRequestType, ctrl.bRequest,
  1573. UGETW(ctrl.wValue), UGETW(ctrl.wIndex),
  1574. UGETW(ctrl.wLength));
  1575. #endif
  1576. doeptsize0.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[0]->doeptsiz);
  1577. /** @todo handle > 1 setup packet , assert error for now */
  1578. if (core_if->dma_enable && core_if->dma_desc_enable == 0
  1579. && (doeptsize0.b.supcnt < 2)) {
  1580. DWC_ERROR
  1581. ("\n\n----------- CANNOT handle > 1 setup packet in DMA mode\n\n");
  1582. }
  1583. /* Clean up the request queue */
  1584. dwc_otg_request_nuke(ep0);
  1585. ep0->stopped = 0;
  1586. if (ctrl.bmRequestType & UE_DIR_IN) {
  1587. ep0->dwc_ep.is_in = 1;
  1588. pcd->ep0state = EP0_IN_DATA_PHASE;
  1589. } else {
  1590. ep0->dwc_ep.is_in = 0;
  1591. pcd->ep0state = EP0_OUT_DATA_PHASE;
  1592. }
  1593. if (UGETW(ctrl.wLength) == 0) {
  1594. ep0->dwc_ep.is_in = 1;
  1595. pcd->ep0state = EP0_IN_STATUS_PHASE;
  1596. }
  1597. if (UT_GET_TYPE(ctrl.bmRequestType) != UT_STANDARD) {
  1598. #ifdef DWC_UTE_CFI
  1599. DWC_MEMCPY(&cfi_req, &ctrl, sizeof(usb_device_request_t));
  1600. //printk(KERN_ALERT "CFI: req_type=0x%02x; req=0x%02x\n",
  1601. ctrl.bRequestType, ctrl.bRequest);
  1602. if (UT_GET_TYPE(cfi_req.bRequestType) == UT_VENDOR) {
  1603. if (cfi_req.bRequest > 0xB0 && cfi_req.bRequest < 0xBF) {
  1604. retval = cfi_setup(pcd, &cfi_req);
  1605. if (retval < 0) {
  1606. ep0_do_stall(pcd, retval);
  1607. pcd->ep0_pending = 0;
  1608. return;
  1609. }
  1610. /* if need gadget setup then call it and check the retval */
  1611. if (pcd->cfi->need_gadget_att) {
  1612. retval =
  1613. cfi_gadget_setup(pcd,
  1614. &pcd->
  1615. cfi->ctrl_req);
  1616. if (retval < 0) {
  1617. pcd->ep0_pending = 0;
  1618. return;
  1619. }
  1620. }
  1621. if (pcd->cfi->need_status_in_complete) {
  1622. do_setup_in_status_phase(pcd);
  1623. }
  1624. return;
  1625. }
  1626. }
  1627. #endif
  1628. /* handle non-standard (class/vendor) requests in the gadget driver */
  1629. do_gadget_setup(pcd, &ctrl);
  1630. return;
  1631. }
  1632. /** @todo NGS: Handle bad setup packet? */
  1633. ///////////////////////////////////////////
  1634. //// --- Standard Request handling --- ////
  1635. switch (ctrl.bRequest) {
  1636. case UR_GET_STATUS:
  1637. do_get_status(pcd);
  1638. break;
  1639. case UR_CLEAR_FEATURE:
  1640. do_clear_feature(pcd);
  1641. break;
  1642. case UR_SET_FEATURE:
  1643. do_set_feature(pcd);
  1644. break;
  1645. case UR_SET_ADDRESS:
  1646. do_set_address(pcd);
  1647. break;
  1648. case UR_SET_INTERFACE:
  1649. case UR_SET_CONFIG:
  1650. // _pcd->request_config = 1; /* Configuration changed */
  1651. do_gadget_setup(pcd, &ctrl);
  1652. break;
  1653. case UR_SYNCH_FRAME:
  1654. do_gadget_setup(pcd, &ctrl);
  1655. break;
  1656. default:
  1657. /* Call the Gadget Driver's setup functions */
  1658. do_gadget_setup(pcd, &ctrl);
  1659. break;
  1660. }
  1661. }
  1662. /**
  1663. * This function completes the ep0 control transfer.
  1664. */
  1665. static int32_t ep0_complete_request(dwc_otg_pcd_ep_t * ep)
  1666. {
  1667. dwc_otg_core_if_t *core_if = GET_CORE_IF(ep->pcd);
  1668. dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  1669. dwc_otg_dev_in_ep_regs_t *in_ep_regs =
  1670. dev_if->in_ep_regs[ep->dwc_ep.num];
  1671. #ifdef DEBUG_EP0
  1672. dwc_otg_dev_out_ep_regs_t *out_ep_regs =
  1673. dev_if->out_ep_regs[ep->dwc_ep.num];
  1674. #endif
  1675. deptsiz0_data_t deptsiz;
  1676. dev_dma_desc_sts_t desc_sts;
  1677. dwc_otg_pcd_request_t *req;
  1678. int is_last = 0;
  1679. dwc_otg_pcd_t *pcd = ep->pcd;
  1680. #ifdef DWC_UTE_CFI
  1681. struct cfi_usb_ctrlrequest *ctrlreq;
  1682. int retval = -DWC_E_NOT_SUPPORTED;
  1683. #endif
  1684. if (pcd->ep0_pending && DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  1685. if (ep->dwc_ep.is_in) {
  1686. #ifdef DEBUG_EP0
  1687. DWC_DEBUGPL(DBG_PCDV, "Do setup OUT status phase\n");
  1688. #endif
  1689. do_setup_out_status_phase(pcd);
  1690. } else {
  1691. #ifdef DEBUG_EP0
  1692. DWC_DEBUGPL(DBG_PCDV, "Do setup IN status phase\n");
  1693. #endif
  1694. #ifdef DWC_UTE_CFI
  1695. ctrlreq = &pcd->cfi->ctrl_req;
  1696. if (UT_GET_TYPE(ctrlreq->bRequestType) == UT_VENDOR) {
  1697. if (ctrlreq->bRequest > 0xB0
  1698. && ctrlreq->bRequest < 0xBF) {
  1699. /* Return if the PCD failed to handle the request */
  1700. if ((retval =
  1701. pcd->cfi->ops.
  1702. ctrl_write_complete(pcd->cfi,
  1703. pcd)) < 0) {
  1704. CFI_INFO
  1705. ("ERROR setting a new value in the PCD(%d)\n",
  1706. retval);
  1707. ep0_do_stall(pcd, retval);
  1708. pcd->ep0_pending = 0;
  1709. return 0;
  1710. }
  1711. /* If the gadget needs to be notified on the request */
  1712. if (pcd->cfi->need_gadget_att == 1) {
  1713. //retval = do_gadget_setup(pcd, &pcd->cfi->ctrl_req);
  1714. retval =
  1715. cfi_gadget_setup(pcd,
  1716. &pcd->cfi->
  1717. ctrl_req);
  1718. /* Return from the function if the gadget failed to process
  1719. * the request properly - this should never happen !!!
  1720. */
  1721. if (retval < 0) {
  1722. CFI_INFO
  1723. ("ERROR setting a new value in the gadget(%d)\n",
  1724. retval);
  1725. pcd->ep0_pending = 0;
  1726. return 0;
  1727. }
  1728. }
  1729. CFI_INFO("%s: RETVAL=%d\n", __func__,
  1730. retval);
  1731. /* If we hit here then the PCD and the gadget has properly
  1732. * handled the request - so send the ZLP IN to the host.
  1733. */
  1734. /* @todo: MAS - decide whether we need to start the setup
  1735. * stage based on the need_setup value of the cfi object
  1736. */
  1737. do_setup_in_status_phase(pcd);
  1738. pcd->ep0_pending = 0;
  1739. return 1;
  1740. }
  1741. }
  1742. #endif
  1743. do_setup_in_status_phase(pcd);
  1744. }
  1745. pcd->ep0_pending = 0;
  1746. return 1;
  1747. }
  1748. if (DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  1749. return 0;
  1750. }
  1751. req = DWC_CIRCLEQ_FIRST(&ep->queue);
  1752. if (pcd->ep0state == EP0_OUT_STATUS_PHASE
  1753. || pcd->ep0state == EP0_IN_STATUS_PHASE) {
  1754. is_last = 1;
  1755. } else if (ep->dwc_ep.is_in) {
  1756. deptsiz.d32 = DWC_READ_REG32(&in_ep_regs->dieptsiz);
  1757. if (core_if->dma_desc_enable != 0)
  1758. desc_sts = dev_if->in_desc_addr->status;
  1759. #ifdef DEBUG_EP0
  1760. DWC_DEBUGPL(DBG_PCDV, "%d len=%d xfersize=%d pktcnt=%d\n",
  1761. ep->dwc_ep.num, ep->dwc_ep.xfer_len,
  1762. deptsiz.b.xfersize, deptsiz.b.pktcnt);
  1763. #endif
  1764. if (((core_if->dma_desc_enable == 0)
  1765. && (deptsiz.b.xfersize == 0))
  1766. || ((core_if->dma_desc_enable != 0)
  1767. && (desc_sts.b.bytes == 0))) {
  1768. req->actual = ep->dwc_ep.xfer_count;
  1769. /* Is a Zero Len Packet needed? */
  1770. if (req->sent_zlp) {
  1771. #ifdef DEBUG_EP0
  1772. DWC_DEBUGPL(DBG_PCD, "Setup Rx ZLP\n");
  1773. #endif
  1774. req->sent_zlp = 0;
  1775. }
  1776. do_setup_out_status_phase(pcd);
  1777. }
  1778. } else {
  1779. /* ep0-OUT */
  1780. #ifdef DEBUG_EP0
  1781. deptsiz.d32 = DWC_READ_REG32(&out_ep_regs->doeptsiz);
  1782. DWC_DEBUGPL(DBG_PCDV, "%d len=%d xsize=%d pktcnt=%d\n",
  1783. ep->dwc_ep.num, ep->dwc_ep.xfer_len,
  1784. deptsiz.b.xfersize, deptsiz.b.pktcnt);
  1785. #endif
  1786. req->actual = ep->dwc_ep.xfer_count;
  1787. /* Is a Zero Len Packet needed? */
  1788. if (req->sent_zlp) {
  1789. #ifdef DEBUG_EP0
  1790. DWC_DEBUGPL(DBG_PCDV, "Setup Tx ZLP\n");
  1791. #endif
  1792. req->sent_zlp = 0;
  1793. }
  1794. if (core_if->dma_desc_enable == 0)
  1795. do_setup_in_status_phase(pcd);
  1796. }
  1797. /* Complete the request */
  1798. if (is_last) {
  1799. dwc_otg_request_done(ep, req, 0);
  1800. ep->dwc_ep.start_xfer_buff = 0;
  1801. ep->dwc_ep.xfer_buff = 0;
  1802. ep->dwc_ep.xfer_len = 0;
  1803. return 1;
  1804. }
  1805. return 0;
  1806. }
  1807. #ifdef DWC_UTE_CFI
  1808. /**
  1809. * This function calculates traverses all the CFI DMA descriptors and
  1810. * and accumulates the bytes that are left to be transfered.
  1811. *
  1812. * @return The total bytes left to transfered, or a negative value as failure
  1813. */
  1814. static inline int cfi_calc_desc_residue(dwc_otg_pcd_ep_t * ep)
  1815. {
  1816. int32_t ret = 0;
  1817. int i;
  1818. struct dwc_otg_dma_desc *ddesc = NULL;
  1819. struct cfi_ep *cfiep;
  1820. /* See if the pcd_ep has its respective cfi_ep mapped */
  1821. cfiep = get_cfi_ep_by_pcd_ep(ep->pcd->cfi, ep);
  1822. if (!cfiep) {
  1823. CFI_INFO("%s: Failed to find ep\n", __func__);
  1824. return -1;
  1825. }
  1826. ddesc = ep->dwc_ep.descs;
  1827. for (i = 0; (i < cfiep->desc_count) && (i < MAX_DMA_DESCS_PER_EP); i++) {
  1828. #if defined(PRINT_CFI_DMA_DESCS)
  1829. print_desc(ddesc, ep->ep.name, i);
  1830. #endif
  1831. ret += ddesc->status.b.bytes;
  1832. ddesc++;
  1833. }
  1834. if (ret)
  1835. CFI_INFO("!!!!!!!!!! WARNING (%s) - residue=%d\n", __func__,
  1836. ret);
  1837. return ret;
  1838. }
  1839. #endif
  1840. /**
  1841. * This function completes the request for the EP. If there are
  1842. * additional requests for the EP in the queue they will be started.
  1843. */
  1844. static void complete_ep(dwc_otg_pcd_ep_t * ep)
  1845. {
  1846. dwc_otg_core_if_t *core_if = GET_CORE_IF(ep->pcd);
  1847. dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  1848. dwc_otg_dev_in_ep_regs_t *in_ep_regs =
  1849. dev_if->in_ep_regs[ep->dwc_ep.num];
  1850. deptsiz_data_t deptsiz;
  1851. dev_dma_desc_sts_t desc_sts;
  1852. dwc_otg_pcd_request_t *req = 0;
  1853. dwc_otg_dev_dma_desc_t *dma_desc;
  1854. uint32_t byte_count = 0;
  1855. int is_last = 0;
  1856. int i;
  1857. DWC_DEBUGPL(DBG_PCDV, "%s() %d-%s\n", __func__, ep->dwc_ep.num,
  1858. (ep->dwc_ep.is_in ? "IN" : "OUT"));
  1859. /* Get any pending requests */
  1860. if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  1861. req = DWC_CIRCLEQ_FIRST(&ep->queue);
  1862. if (!req) {
  1863. DWC_PRINTF("complete_ep 0x%p, req = NULL!\n", ep);
  1864. return;
  1865. }
  1866. } else {
  1867. DWC_PRINTF("complete_ep 0x%p, ep->queue empty!\n", ep);
  1868. return;
  1869. }
  1870. DWC_DEBUGPL(DBG_PCD, "Requests %d\n", ep->pcd->request_pending);
  1871. if (ep->dwc_ep.is_in) {
  1872. deptsiz.d32 = DWC_READ_REG32(&in_ep_regs->dieptsiz);
  1873. if (core_if->dma_enable) {
  1874. if (core_if->dma_desc_enable == 0) {
  1875. if (deptsiz.b.xfersize == 0
  1876. && deptsiz.b.pktcnt == 0) {
  1877. byte_count =
  1878. ep->dwc_ep.xfer_len -
  1879. ep->dwc_ep.xfer_count;
  1880. ep->dwc_ep.xfer_buff += byte_count;
  1881. ep->dwc_ep.dma_addr += byte_count;
  1882. ep->dwc_ep.xfer_count += byte_count;
  1883. DWC_DEBUGPL(DBG_PCDV,
  1884. "%d-%s len=%d xfersize=%d pktcnt=%d\n",
  1885. ep->dwc_ep.num,
  1886. (ep->dwc_ep.
  1887. is_in ? "IN" : "OUT"),
  1888. ep->dwc_ep.xfer_len,
  1889. deptsiz.b.xfersize,
  1890. deptsiz.b.pktcnt);
  1891. if (ep->dwc_ep.xfer_len <
  1892. ep->dwc_ep.total_len) {
  1893. dwc_otg_ep_start_transfer
  1894. (core_if, &ep->dwc_ep);
  1895. } else if (ep->dwc_ep.sent_zlp) {
  1896. /*
  1897. * This fragment of code should initiate 0
  1898. * length transfer in case if it is queued
  1899. * a transfer with size divisible to EPs max
  1900. * packet size and with usb_request zero field
  1901. * is set, which means that after data is transfered,
  1902. * it is also should be transfered
  1903. * a 0 length packet at the end. For Slave and
  1904. * Buffer DMA modes in this case SW has
  1905. * to initiate 2 transfers one with transfer size,
  1906. * and the second with 0 size. For Descriptor
  1907. * DMA mode SW is able to initiate a transfer,
  1908. * which will handle all the packets including
  1909. * the last 0 length.
  1910. */
  1911. ep->dwc_ep.sent_zlp = 0;
  1912. dwc_otg_ep_start_zl_transfer
  1913. (core_if, &ep->dwc_ep);
  1914. } else {
  1915. is_last = 1;
  1916. }
  1917. } else {
  1918. if(ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC)
  1919. {
  1920. req->actual = 0;
  1921. dwc_otg_request_done(ep, req, 0);
  1922. ep->dwc_ep.start_xfer_buff = 0;
  1923. ep->dwc_ep.xfer_buff = 0;
  1924. ep->dwc_ep.xfer_len = 0;
  1925. /* If there is a request in the queue start it. */
  1926. start_next_request(ep);
  1927. } else
  1928. DWC_WARN
  1929. ("Incomplete transfer (%d - %s [siz=%d pkt=%d])\n",
  1930. ep->dwc_ep.num,
  1931. (ep->dwc_ep.is_in ? "IN" : "OUT"),
  1932. deptsiz.b.xfersize,
  1933. deptsiz.b.pktcnt);
  1934. }
  1935. } else {
  1936. dma_desc = ep->dwc_ep.desc_addr;
  1937. byte_count = 0;
  1938. ep->dwc_ep.sent_zlp = 0;
  1939. #ifdef DWC_UTE_CFI
  1940. CFI_INFO("%s: BUFFER_MODE=%d\n", __func__,
  1941. ep->dwc_ep.buff_mode);
  1942. if (ep->dwc_ep.buff_mode != BM_STANDARD) {
  1943. int residue;
  1944. residue = cfi_calc_desc_residue(ep);
  1945. if (residue < 0)
  1946. return;
  1947. byte_count = residue;
  1948. } else {
  1949. #endif
  1950. for (i = 0; i < ep->dwc_ep.desc_cnt;
  1951. ++i) {
  1952. desc_sts = dma_desc->status;
  1953. byte_count += desc_sts.b.bytes;
  1954. dma_desc++;
  1955. }
  1956. #ifdef DWC_UTE_CFI
  1957. }
  1958. #endif
  1959. if (byte_count == 0) {
  1960. ep->dwc_ep.xfer_count =
  1961. ep->dwc_ep.total_len;
  1962. is_last = 1;
  1963. } else {
  1964. DWC_WARN("Incomplete transfer\n");
  1965. }
  1966. }
  1967. } else {
  1968. if (deptsiz.b.xfersize == 0 && deptsiz.b.pktcnt == 0) {
  1969. DWC_DEBUGPL(DBG_PCDV,
  1970. "%d-%s len=%d xfersize=%d pktcnt=%d\n",
  1971. ep->dwc_ep.num,
  1972. ep->dwc_ep.is_in ? "IN" : "OUT",
  1973. ep->dwc_ep.xfer_len,
  1974. deptsiz.b.xfersize,
  1975. deptsiz.b.pktcnt);
  1976. /* Check if the whole transfer was completed,
  1977. * if no, setup transfer for next portion of data
  1978. */
  1979. if (ep->dwc_ep.xfer_len < ep->dwc_ep.total_len) {
  1980. dwc_otg_ep_start_transfer(core_if,
  1981. &ep->dwc_ep);
  1982. } else if (ep->dwc_ep.sent_zlp) {
  1983. /*
  1984. * This fragment of code should initiate 0
  1985. * length trasfer in case if it is queued
  1986. * a trasfer with size divisible to EPs max
  1987. * packet size and with usb_request zero field
  1988. * is set, which means that after data is transfered,
  1989. * it is also should be transfered
  1990. * a 0 length packet at the end. For Slave and
  1991. * Buffer DMA modes in this case SW has
  1992. * to initiate 2 transfers one with transfer size,
  1993. * and the second with 0 size. For Desriptor
  1994. * DMA mode SW is able to initiate a transfer,
  1995. * which will handle all the packets including
  1996. * the last 0 legth.
  1997. */
  1998. ep->dwc_ep.sent_zlp = 0;
  1999. dwc_otg_ep_start_zl_transfer(core_if,
  2000. &ep->dwc_ep);
  2001. } else {
  2002. is_last = 1;
  2003. }
  2004. } else {
  2005. DWC_WARN
  2006. ("Incomplete transfer (%d-%s [siz=%d pkt=%d])\n",
  2007. ep->dwc_ep.num,
  2008. (ep->dwc_ep.is_in ? "IN" : "OUT"),
  2009. deptsiz.b.xfersize, deptsiz.b.pktcnt);
  2010. }
  2011. }
  2012. } else {
  2013. dwc_otg_dev_out_ep_regs_t *out_ep_regs =
  2014. dev_if->out_ep_regs[ep->dwc_ep.num];
  2015. desc_sts.d32 = 0;
  2016. if (core_if->dma_enable) {
  2017. if (core_if->dma_desc_enable) {
  2018. dma_desc = ep->dwc_ep.desc_addr;
  2019. byte_count = 0;
  2020. ep->dwc_ep.sent_zlp = 0;
  2021. #ifdef DWC_UTE_CFI
  2022. CFI_INFO("%s: BUFFER_MODE=%d\n", __func__,
  2023. ep->dwc_ep.buff_mode);
  2024. if (ep->dwc_ep.buff_mode != BM_STANDARD) {
  2025. int residue;
  2026. residue = cfi_calc_desc_residue(ep);
  2027. if (residue < 0)
  2028. return;
  2029. byte_count = residue;
  2030. } else {
  2031. #endif
  2032. for (i = 0; i < ep->dwc_ep.desc_cnt;
  2033. ++i) {
  2034. desc_sts = dma_desc->status;
  2035. byte_count += desc_sts.b.bytes;
  2036. dma_desc++;
  2037. }
  2038. #ifdef DWC_UTE_CFI
  2039. }
  2040. #endif
  2041. /* Checking for interrupt Out transfers with not
  2042. * dword aligned mps sizes
  2043. */
  2044. if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_INTR &&
  2045. (ep->dwc_ep.maxpacket%4)) {
  2046. ep->dwc_ep.xfer_count = ep->dwc_ep.total_len - byte_count;
  2047. if ((ep->dwc_ep.xfer_len % ep->dwc_ep.maxpacket) &&
  2048. (ep->dwc_ep.xfer_len/ep->dwc_ep.maxpacket < MAX_DMA_DESC_CNT))
  2049. ep->dwc_ep.xfer_len -=
  2050. (ep->dwc_ep.desc_cnt - 1) * ep->dwc_ep.maxpacket +
  2051. ep->dwc_ep.xfer_len % ep->dwc_ep.maxpacket;
  2052. else
  2053. ep->dwc_ep.xfer_len -=
  2054. ep->dwc_ep.desc_cnt * ep->dwc_ep.maxpacket;
  2055. if (ep->dwc_ep.xfer_len > 0) {
  2056. dwc_otg_ep_start_transfer(core_if,
  2057. &ep->dwc_ep);
  2058. } else {
  2059. is_last = 1;
  2060. }
  2061. } else {
  2062. ep->dwc_ep.xfer_count = ep->dwc_ep.total_len
  2063. - byte_count +
  2064. ((4 - (ep->dwc_ep.total_len & 0x3)) & 0x3);
  2065. is_last = 1;
  2066. }
  2067. } else {
  2068. deptsiz.d32 = 0;
  2069. deptsiz.d32 =
  2070. DWC_READ_REG32(&out_ep_regs->doeptsiz);
  2071. byte_count = (ep->dwc_ep.xfer_len -
  2072. ep->dwc_ep.xfer_count -
  2073. deptsiz.b.xfersize);
  2074. ep->dwc_ep.xfer_buff += byte_count;
  2075. ep->dwc_ep.dma_addr += byte_count;
  2076. ep->dwc_ep.xfer_count += byte_count;
  2077. /* Check if the whole transfer was completed,
  2078. * if no, setup transfer for next portion of data
  2079. */
  2080. if (ep->dwc_ep.xfer_len < ep->dwc_ep.total_len) {
  2081. dwc_otg_ep_start_transfer(core_if,
  2082. &ep->dwc_ep);
  2083. } else if (ep->dwc_ep.sent_zlp) {
  2084. /*
  2085. * This fragment of code should initiate 0
  2086. * length trasfer in case if it is queued
  2087. * a trasfer with size divisible to EPs max
  2088. * packet size and with usb_request zero field
  2089. * is set, which means that after data is transfered,
  2090. * it is also should be transfered
  2091. * a 0 length packet at the end. For Slave and
  2092. * Buffer DMA modes in this case SW has
  2093. * to initiate 2 transfers one with transfer size,
  2094. * and the second with 0 size. For Desriptor
  2095. * DMA mode SW is able to initiate a transfer,
  2096. * which will handle all the packets including
  2097. * the last 0 legth.
  2098. */
  2099. ep->dwc_ep.sent_zlp = 0;
  2100. dwc_otg_ep_start_zl_transfer(core_if,
  2101. &ep->dwc_ep);
  2102. } else {
  2103. is_last = 1;
  2104. }
  2105. }
  2106. } else {
  2107. /* Check if the whole transfer was completed,
  2108. * if no, setup transfer for next portion of data
  2109. */
  2110. if (ep->dwc_ep.xfer_len < ep->dwc_ep.total_len) {
  2111. dwc_otg_ep_start_transfer(core_if, &ep->dwc_ep);
  2112. } else if (ep->dwc_ep.sent_zlp) {
  2113. /*
  2114. * This fragment of code should initiate 0
  2115. * length transfer in case if it is queued
  2116. * a transfer with size divisible to EPs max
  2117. * packet size and with usb_request zero field
  2118. * is set, which means that after data is transfered,
  2119. * it is also should be transfered
  2120. * a 0 length packet at the end. For Slave and
  2121. * Buffer DMA modes in this case SW has
  2122. * to initiate 2 transfers one with transfer size,
  2123. * and the second with 0 size. For Descriptor
  2124. * DMA mode SW is able to initiate a transfer,
  2125. * which will handle all the packets including
  2126. * the last 0 length.
  2127. */
  2128. ep->dwc_ep.sent_zlp = 0;
  2129. dwc_otg_ep_start_zl_transfer(core_if,
  2130. &ep->dwc_ep);
  2131. } else {
  2132. is_last = 1;
  2133. }
  2134. }
  2135. DWC_DEBUGPL(DBG_PCDV,
  2136. "addr %p, %d-%s len=%d cnt=%d xsize=%d pktcnt=%d\n",
  2137. &out_ep_regs->doeptsiz, ep->dwc_ep.num,
  2138. ep->dwc_ep.is_in ? "IN" : "OUT",
  2139. ep->dwc_ep.xfer_len, ep->dwc_ep.xfer_count,
  2140. deptsiz.b.xfersize, deptsiz.b.pktcnt);
  2141. }
  2142. /* Complete the request */
  2143. if (is_last) {
  2144. #ifdef DWC_UTE_CFI
  2145. if (ep->dwc_ep.buff_mode != BM_STANDARD) {
  2146. req->actual = ep->dwc_ep.cfi_req_len - byte_count;
  2147. } else {
  2148. #endif
  2149. req->actual = ep->dwc_ep.xfer_count;
  2150. #ifdef DWC_UTE_CFI
  2151. }
  2152. #endif
  2153. if (req->dw_align_buf) {
  2154. if (!ep->dwc_ep.is_in) {
  2155. dwc_memcpy(req->buf, req->dw_align_buf, req->length);
  2156. }
  2157. DWC_DMA_FREE(req->length, req->dw_align_buf,
  2158. req->dw_align_buf_dma);
  2159. }
  2160. dwc_otg_request_done(ep, req, 0);
  2161. ep->dwc_ep.start_xfer_buff = 0;
  2162. ep->dwc_ep.xfer_buff = 0;
  2163. ep->dwc_ep.xfer_len = 0;
  2164. /* If there is a request in the queue start it. */
  2165. start_next_request(ep);
  2166. }
  2167. }
  2168. #ifdef DWC_EN_ISOC
  2169. /**
  2170. * This function BNA interrupt for Isochronous EPs
  2171. *
  2172. */
  2173. static void dwc_otg_pcd_handle_iso_bna(dwc_otg_pcd_ep_t * ep)
  2174. {
  2175. dwc_ep_t *dwc_ep = &ep->dwc_ep;
  2176. volatile uint32_t *addr;
  2177. depctl_data_t depctl = {.d32 = 0 };
  2178. dwc_otg_pcd_t *pcd = ep->pcd;
  2179. dwc_otg_dev_dma_desc_t *dma_desc;
  2180. int i;
  2181. dma_desc =
  2182. dwc_ep->iso_desc_addr + dwc_ep->desc_cnt * (dwc_ep->proc_buf_num);
  2183. if (dwc_ep->is_in) {
  2184. dev_dma_desc_sts_t sts = {.d32 = 0 };
  2185. for (i = 0; i < dwc_ep->desc_cnt; ++i, ++dma_desc) {
  2186. sts.d32 = dma_desc->status.d32;
  2187. sts.b_iso_in.bs = BS_HOST_READY;
  2188. dma_desc->status.d32 = sts.d32;
  2189. }
  2190. } else {
  2191. dev_dma_desc_sts_t sts = {.d32 = 0 };
  2192. for (i = 0; i < dwc_ep->desc_cnt; ++i, ++dma_desc) {
  2193. sts.d32 = dma_desc->status.d32;
  2194. sts.b_iso_out.bs = BS_HOST_READY;
  2195. dma_desc->status.d32 = sts.d32;
  2196. }
  2197. }
  2198. if (dwc_ep->is_in == 0) {
  2199. addr =
  2200. &GET_CORE_IF(pcd)->dev_if->out_ep_regs[dwc_ep->
  2201. num]->doepctl;
  2202. } else {
  2203. addr =
  2204. &GET_CORE_IF(pcd)->dev_if->in_ep_regs[dwc_ep->num]->diepctl;
  2205. }
  2206. depctl.b.epena = 1;
  2207. DWC_MODIFY_REG32(addr, depctl.d32, depctl.d32);
  2208. }
  2209. /**
  2210. * This function sets latest iso packet information(non-PTI mode)
  2211. *
  2212. * @param core_if Programming view of DWC_otg controller.
  2213. * @param ep The EP to start the transfer on.
  2214. *
  2215. */
  2216. void set_current_pkt_info(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  2217. {
  2218. deptsiz_data_t deptsiz = {.d32 = 0 };
  2219. dma_addr_t dma_addr;
  2220. uint32_t offset;
  2221. if (ep->proc_buf_num)
  2222. dma_addr = ep->dma_addr1;
  2223. else
  2224. dma_addr = ep->dma_addr0;
  2225. if (ep->is_in) {
  2226. deptsiz.d32 =
  2227. DWC_READ_REG32(&core_if->dev_if->
  2228. in_ep_regs[ep->num]->dieptsiz);
  2229. offset = ep->data_per_frame;
  2230. } else {
  2231. deptsiz.d32 =
  2232. DWC_READ_REG32(&core_if->dev_if->
  2233. out_ep_regs[ep->num]->doeptsiz);
  2234. offset =
  2235. ep->data_per_frame +
  2236. (0x4 & (0x4 - (ep->data_per_frame & 0x3)));
  2237. }
  2238. if (!deptsiz.b.xfersize) {
  2239. ep->pkt_info[ep->cur_pkt].length = ep->data_per_frame;
  2240. ep->pkt_info[ep->cur_pkt].offset =
  2241. ep->cur_pkt_dma_addr - dma_addr;
  2242. ep->pkt_info[ep->cur_pkt].status = 0;
  2243. } else {
  2244. ep->pkt_info[ep->cur_pkt].length = ep->data_per_frame;
  2245. ep->pkt_info[ep->cur_pkt].offset =
  2246. ep->cur_pkt_dma_addr - dma_addr;
  2247. ep->pkt_info[ep->cur_pkt].status = -DWC_E_NO_DATA;
  2248. }
  2249. ep->cur_pkt_addr += offset;
  2250. ep->cur_pkt_dma_addr += offset;
  2251. ep->cur_pkt++;
  2252. }
  2253. /**
  2254. * This function sets latest iso packet information(DDMA mode)
  2255. *
  2256. * @param core_if Programming view of DWC_otg controller.
  2257. * @param dwc_ep The EP to start the transfer on.
  2258. *
  2259. */
  2260. static void set_ddma_iso_pkts_info(dwc_otg_core_if_t * core_if,
  2261. dwc_ep_t * dwc_ep)
  2262. {
  2263. dwc_otg_dev_dma_desc_t *dma_desc;
  2264. dev_dma_desc_sts_t sts = {.d32 = 0 };
  2265. iso_pkt_info_t *iso_packet;
  2266. uint32_t data_per_desc;
  2267. uint32_t offset;
  2268. int i, j;
  2269. iso_packet = dwc_ep->pkt_info;
  2270. /** Reinit closed DMA Descriptors*/
  2271. /** ISO OUT EP */
  2272. if (dwc_ep->is_in == 0) {
  2273. dma_desc =
  2274. dwc_ep->iso_desc_addr +
  2275. dwc_ep->desc_cnt * dwc_ep->proc_buf_num;
  2276. offset = 0;
  2277. for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
  2278. i += dwc_ep->pkt_per_frm) {
  2279. for (j = 0; j < dwc_ep->pkt_per_frm; ++j) {
  2280. data_per_desc =
  2281. ((j + 1) * dwc_ep->maxpacket >
  2282. dwc_ep->
  2283. data_per_frame) ? dwc_ep->data_per_frame -
  2284. j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  2285. data_per_desc +=
  2286. (data_per_desc % 4) ? (4 -
  2287. data_per_desc %
  2288. 4) : 0;
  2289. sts.d32 = dma_desc->status.d32;
  2290. /* Write status in iso_packet_decsriptor */
  2291. iso_packet->status =
  2292. sts.b_iso_out.rxsts +
  2293. (sts.b_iso_out.bs ^ BS_DMA_DONE);
  2294. if (iso_packet->status) {
  2295. iso_packet->status = -DWC_E_NO_DATA;
  2296. }
  2297. /* Received data length */
  2298. if (!sts.b_iso_out.rxbytes) {
  2299. iso_packet->length =
  2300. data_per_desc -
  2301. sts.b_iso_out.rxbytes;
  2302. } else {
  2303. iso_packet->length =
  2304. data_per_desc -
  2305. sts.b_iso_out.rxbytes + (4 -
  2306. dwc_ep->data_per_frame
  2307. % 4);
  2308. }
  2309. iso_packet->offset = offset;
  2310. offset += data_per_desc;
  2311. dma_desc++;
  2312. iso_packet++;
  2313. }
  2314. }
  2315. for (j = 0; j < dwc_ep->pkt_per_frm - 1; ++j) {
  2316. data_per_desc =
  2317. ((j + 1) * dwc_ep->maxpacket >
  2318. dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
  2319. j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  2320. data_per_desc +=
  2321. (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
  2322. sts.d32 = dma_desc->status.d32;
  2323. /* Write status in iso_packet_decsriptor */
  2324. iso_packet->status =
  2325. sts.b_iso_out.rxsts +
  2326. (sts.b_iso_out.bs ^ BS_DMA_DONE);
  2327. if (iso_packet->status) {
  2328. iso_packet->status = -DWC_E_NO_DATA;
  2329. }
  2330. /* Received data length */
  2331. iso_packet->length =
  2332. dwc_ep->data_per_frame - sts.b_iso_out.rxbytes;
  2333. iso_packet->offset = offset;
  2334. offset += data_per_desc;
  2335. iso_packet++;
  2336. dma_desc++;
  2337. }
  2338. sts.d32 = dma_desc->status.d32;
  2339. /* Write status in iso_packet_decsriptor */
  2340. iso_packet->status =
  2341. sts.b_iso_out.rxsts + (sts.b_iso_out.bs ^ BS_DMA_DONE);
  2342. if (iso_packet->status) {
  2343. iso_packet->status = -DWC_E_NO_DATA;
  2344. }
  2345. /* Received data length */
  2346. if (!sts.b_iso_out.rxbytes) {
  2347. iso_packet->length =
  2348. dwc_ep->data_per_frame - sts.b_iso_out.rxbytes;
  2349. } else {
  2350. iso_packet->length =
  2351. dwc_ep->data_per_frame - sts.b_iso_out.rxbytes +
  2352. (4 - dwc_ep->data_per_frame % 4);
  2353. }
  2354. iso_packet->offset = offset;
  2355. } else {
  2356. /** ISO IN EP */
  2357. dma_desc =
  2358. dwc_ep->iso_desc_addr +
  2359. dwc_ep->desc_cnt * dwc_ep->proc_buf_num;
  2360. for (i = 0; i < dwc_ep->desc_cnt - 1; i++) {
  2361. sts.d32 = dma_desc->status.d32;
  2362. /* Write status in iso packet descriptor */
  2363. iso_packet->status =
  2364. sts.b_iso_in.txsts +
  2365. (sts.b_iso_in.bs ^ BS_DMA_DONE);
  2366. if (iso_packet->status != 0) {
  2367. iso_packet->status = -DWC_E_NO_DATA;
  2368. }
  2369. /* Bytes has been transfered */
  2370. iso_packet->length =
  2371. dwc_ep->data_per_frame - sts.b_iso_in.txbytes;
  2372. dma_desc++;
  2373. iso_packet++;
  2374. }
  2375. sts.d32 = dma_desc->status.d32;
  2376. while (sts.b_iso_in.bs == BS_DMA_BUSY) {
  2377. sts.d32 = dma_desc->status.d32;
  2378. }
  2379. /* Write status in iso packet descriptor ??? do be done with ERROR codes */
  2380. iso_packet->status =
  2381. sts.b_iso_in.txsts + (sts.b_iso_in.bs ^ BS_DMA_DONE);
  2382. if (iso_packet->status != 0) {
  2383. iso_packet->status = -DWC_E_NO_DATA;
  2384. }
  2385. /* Bytes has been transfered */
  2386. iso_packet->length =
  2387. dwc_ep->data_per_frame - sts.b_iso_in.txbytes;
  2388. }
  2389. }
  2390. /**
  2391. * This function reinitialize DMA Descriptors for Isochronous transfer
  2392. *
  2393. * @param core_if Programming view of DWC_otg controller.
  2394. * @param dwc_ep The EP to start the transfer on.
  2395. *
  2396. */
  2397. static void reinit_ddma_iso_xfer(dwc_otg_core_if_t * core_if, dwc_ep_t * dwc_ep)
  2398. {
  2399. int i, j;
  2400. dwc_otg_dev_dma_desc_t *dma_desc;
  2401. dma_addr_t dma_ad;
  2402. volatile uint32_t *addr;
  2403. dev_dma_desc_sts_t sts = {.d32 = 0 };
  2404. uint32_t data_per_desc;
  2405. if (dwc_ep->is_in == 0) {
  2406. addr = &core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl;
  2407. } else {
  2408. addr = &core_if->dev_if->in_ep_regs[dwc_ep->num]->diepctl;
  2409. }
  2410. if (dwc_ep->proc_buf_num == 0) {
  2411. /** Buffer 0 descriptors setup */
  2412. dma_ad = dwc_ep->dma_addr0;
  2413. } else {
  2414. /** Buffer 1 descriptors setup */
  2415. dma_ad = dwc_ep->dma_addr1;
  2416. }
  2417. /** Reinit closed DMA Descriptors*/
  2418. /** ISO OUT EP */
  2419. if (dwc_ep->is_in == 0) {
  2420. dma_desc =
  2421. dwc_ep->iso_desc_addr +
  2422. dwc_ep->desc_cnt * dwc_ep->proc_buf_num;
  2423. sts.b_iso_out.bs = BS_HOST_READY;
  2424. sts.b_iso_out.rxsts = 0;
  2425. sts.b_iso_out.l = 0;
  2426. sts.b_iso_out.sp = 0;
  2427. sts.b_iso_out.ioc = 0;
  2428. sts.b_iso_out.pid = 0;
  2429. sts.b_iso_out.framenum = 0;
  2430. for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
  2431. i += dwc_ep->pkt_per_frm) {
  2432. for (j = 0; j < dwc_ep->pkt_per_frm; ++j) {
  2433. data_per_desc =
  2434. ((j + 1) * dwc_ep->maxpacket >
  2435. dwc_ep->
  2436. data_per_frame) ? dwc_ep->data_per_frame -
  2437. j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  2438. data_per_desc +=
  2439. (data_per_desc % 4) ? (4 -
  2440. data_per_desc %
  2441. 4) : 0;
  2442. sts.b_iso_out.rxbytes = data_per_desc;
  2443. dma_desc->buf = dma_ad;
  2444. dma_desc->status.d32 = sts.d32;
  2445. dma_ad += data_per_desc;
  2446. dma_desc++;
  2447. }
  2448. }
  2449. for (j = 0; j < dwc_ep->pkt_per_frm - 1; ++j) {
  2450. data_per_desc =
  2451. ((j + 1) * dwc_ep->maxpacket >
  2452. dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
  2453. j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  2454. data_per_desc +=
  2455. (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
  2456. sts.b_iso_out.rxbytes = data_per_desc;
  2457. dma_desc->buf = dma_ad;
  2458. dma_desc->status.d32 = sts.d32;
  2459. dma_desc++;
  2460. dma_ad += data_per_desc;
  2461. }
  2462. sts.b_iso_out.ioc = 1;
  2463. sts.b_iso_out.l = dwc_ep->proc_buf_num;
  2464. data_per_desc =
  2465. ((j + 1) * dwc_ep->maxpacket >
  2466. dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
  2467. j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  2468. data_per_desc +=
  2469. (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
  2470. sts.b_iso_out.rxbytes = data_per_desc;
  2471. dma_desc->buf = dma_ad;
  2472. dma_desc->status.d32 = sts.d32;
  2473. } else {
  2474. /** ISO IN EP */
  2475. dma_desc =
  2476. dwc_ep->iso_desc_addr +
  2477. dwc_ep->desc_cnt * dwc_ep->proc_buf_num;
  2478. sts.b_iso_in.bs = BS_HOST_READY;
  2479. sts.b_iso_in.txsts = 0;
  2480. sts.b_iso_in.sp = 0;
  2481. sts.b_iso_in.ioc = 0;
  2482. sts.b_iso_in.pid = dwc_ep->pkt_per_frm;
  2483. sts.b_iso_in.framenum = dwc_ep->next_frame;
  2484. sts.b_iso_in.txbytes = dwc_ep->data_per_frame;
  2485. sts.b_iso_in.l = 0;
  2486. for (i = 0; i < dwc_ep->desc_cnt - 1; i++) {
  2487. dma_desc->buf = dma_ad;
  2488. dma_desc->status.d32 = sts.d32;
  2489. sts.b_iso_in.framenum += dwc_ep->bInterval;
  2490. dma_ad += dwc_ep->data_per_frame;
  2491. dma_desc++;
  2492. }
  2493. sts.b_iso_in.ioc = 1;
  2494. sts.b_iso_in.l = dwc_ep->proc_buf_num;
  2495. dma_desc->buf = dma_ad;
  2496. dma_desc->status.d32 = sts.d32;
  2497. dwc_ep->next_frame =
  2498. sts.b_iso_in.framenum + dwc_ep->bInterval * 1;
  2499. }
  2500. dwc_ep->proc_buf_num = (dwc_ep->proc_buf_num ^ 1) & 0x1;
  2501. }
  2502. /**
  2503. * This function is to handle Iso EP transfer complete interrupt
  2504. * in case Iso out packet was dropped
  2505. *
  2506. * @param core_if Programming view of DWC_otg controller.
  2507. * @param dwc_ep The EP for wihich transfer complete was asserted
  2508. *
  2509. */
  2510. static uint32_t handle_iso_out_pkt_dropped(dwc_otg_core_if_t * core_if,
  2511. dwc_ep_t * dwc_ep)
  2512. {
  2513. uint32_t dma_addr;
  2514. uint32_t drp_pkt;
  2515. uint32_t drp_pkt_cnt;
  2516. deptsiz_data_t deptsiz = {.d32 = 0 };
  2517. depctl_data_t depctl = {.d32 = 0 };
  2518. int i;
  2519. deptsiz.d32 =
  2520. DWC_READ_REG32(&core_if->dev_if->
  2521. out_ep_regs[dwc_ep->num]->doeptsiz);
  2522. drp_pkt = dwc_ep->pkt_cnt - deptsiz.b.pktcnt;
  2523. drp_pkt_cnt = dwc_ep->pkt_per_frm - (drp_pkt % dwc_ep->pkt_per_frm);
  2524. /* Setting dropped packets status */
  2525. for (i = 0; i < drp_pkt_cnt; ++i) {
  2526. dwc_ep->pkt_info[drp_pkt].status = -DWC_E_NO_DATA;
  2527. drp_pkt++;
  2528. deptsiz.b.pktcnt--;
  2529. }
  2530. if (deptsiz.b.pktcnt > 0) {
  2531. deptsiz.b.xfersize =
  2532. dwc_ep->xfer_len - (dwc_ep->pkt_cnt -
  2533. deptsiz.b.pktcnt) * dwc_ep->maxpacket;
  2534. } else {
  2535. deptsiz.b.xfersize = 0;
  2536. deptsiz.b.pktcnt = 0;
  2537. }
  2538. DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doeptsiz,
  2539. deptsiz.d32);
  2540. if (deptsiz.b.pktcnt > 0) {
  2541. if (dwc_ep->proc_buf_num) {
  2542. dma_addr =
  2543. dwc_ep->dma_addr1 + dwc_ep->xfer_len -
  2544. deptsiz.b.xfersize;
  2545. } else {
  2546. dma_addr =
  2547. dwc_ep->dma_addr0 + dwc_ep->xfer_len -
  2548. deptsiz.b.xfersize;;
  2549. }
  2550. DWC_WRITE_REG32(&core_if->dev_if->
  2551. out_ep_regs[dwc_ep->num]->doepdma, dma_addr);
  2552. /** Re-enable endpoint, clear nak */
  2553. depctl.d32 = 0;
  2554. depctl.b.epena = 1;
  2555. depctl.b.cnak = 1;
  2556. DWC_MODIFY_REG32(&core_if->dev_if->
  2557. out_ep_regs[dwc_ep->num]->doepctl, depctl.d32,
  2558. depctl.d32);
  2559. return 0;
  2560. } else {
  2561. return 1;
  2562. }
  2563. }
  2564. /**
  2565. * This function sets iso packets information(PTI mode)
  2566. *
  2567. * @param core_if Programming view of DWC_otg controller.
  2568. * @param ep The EP to start the transfer on.
  2569. *
  2570. */
  2571. static uint32_t set_iso_pkts_info(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  2572. {
  2573. int i, j;
  2574. dma_addr_t dma_ad;
  2575. iso_pkt_info_t *packet_info = ep->pkt_info;
  2576. uint32_t offset;
  2577. uint32_t frame_data;
  2578. deptsiz_data_t deptsiz;
  2579. if (ep->proc_buf_num == 0) {
  2580. /** Buffer 0 descriptors setup */
  2581. dma_ad = ep->dma_addr0;
  2582. } else {
  2583. /** Buffer 1 descriptors setup */
  2584. dma_ad = ep->dma_addr1;
  2585. }
  2586. if (ep->is_in) {
  2587. deptsiz.d32 =
  2588. DWC_READ_REG32(&core_if->dev_if->
  2589. in_ep_regs[ep->num]->dieptsiz);
  2590. } else {
  2591. deptsiz.d32 =
  2592. DWC_READ_REG32(&core_if->dev_if->
  2593. out_ep_regs[ep->num]->doeptsiz);
  2594. }
  2595. if (!deptsiz.b.xfersize) {
  2596. offset = 0;
  2597. for (i = 0; i < ep->pkt_cnt; i += ep->pkt_per_frm) {
  2598. frame_data = ep->data_per_frame;
  2599. for (j = 0; j < ep->pkt_per_frm; ++j) {
  2600. /* Packet status - is not set as initially
  2601. * it is set to 0 and if packet was sent
  2602. successfully, status field will remain 0*/
  2603. /* Bytes has been transfered */
  2604. packet_info->length =
  2605. (ep->maxpacket <
  2606. frame_data) ? ep->maxpacket : frame_data;
  2607. /* Received packet offset */
  2608. packet_info->offset = offset;
  2609. offset += packet_info->length;
  2610. frame_data -= packet_info->length;
  2611. packet_info++;
  2612. }
  2613. }
  2614. return 1;
  2615. } else {
  2616. /* This is a workaround for in case of Transfer Complete with
  2617. * PktDrpSts interrupts merging - in this case Transfer complete
  2618. * interrupt for Isoc Out Endpoint is asserted without PktDrpSts
  2619. * set and with DOEPTSIZ register non zero. Investigations showed,
  2620. * that this happens when Out packet is dropped, but because of
  2621. * interrupts merging during first interrupt handling PktDrpSts
  2622. * bit is cleared and for next merged interrupts it is not reset.
  2623. * In this case SW hadles the interrupt as if PktDrpSts bit is set.
  2624. */
  2625. if (ep->is_in) {
  2626. return 1;
  2627. } else {
  2628. return handle_iso_out_pkt_dropped(core_if, ep);
  2629. }
  2630. }
  2631. }
  2632. /**
  2633. * This function is to handle Iso EP transfer complete interrupt
  2634. *
  2635. * @param pcd The PCD
  2636. * @param ep The EP for which transfer complete was asserted
  2637. *
  2638. */
  2639. static void complete_iso_ep(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * ep)
  2640. {
  2641. dwc_otg_core_if_t *core_if = GET_CORE_IF(ep->pcd);
  2642. dwc_ep_t *dwc_ep = &ep->dwc_ep;
  2643. uint8_t is_last = 0;
  2644. if (ep->dwc_ep.next_frame == 0xffffffff) {
  2645. DWC_WARN("Next frame is not set!\n");
  2646. return;
  2647. }
  2648. if (core_if->dma_enable) {
  2649. if (core_if->dma_desc_enable) {
  2650. set_ddma_iso_pkts_info(core_if, dwc_ep);
  2651. reinit_ddma_iso_xfer(core_if, dwc_ep);
  2652. is_last = 1;
  2653. } else {
  2654. if (core_if->pti_enh_enable) {
  2655. if (set_iso_pkts_info(core_if, dwc_ep)) {
  2656. dwc_ep->proc_buf_num =
  2657. (dwc_ep->proc_buf_num ^ 1) & 0x1;
  2658. dwc_otg_iso_ep_start_buf_transfer
  2659. (core_if, dwc_ep);
  2660. is_last = 1;
  2661. }
  2662. } else {
  2663. set_current_pkt_info(core_if, dwc_ep);
  2664. if (dwc_ep->cur_pkt >= dwc_ep->pkt_cnt) {
  2665. is_last = 1;
  2666. dwc_ep->cur_pkt = 0;
  2667. dwc_ep->proc_buf_num =
  2668. (dwc_ep->proc_buf_num ^ 1) & 0x1;
  2669. if (dwc_ep->proc_buf_num) {
  2670. dwc_ep->cur_pkt_addr =
  2671. dwc_ep->xfer_buff1;
  2672. dwc_ep->cur_pkt_dma_addr =
  2673. dwc_ep->dma_addr1;
  2674. } else {
  2675. dwc_ep->cur_pkt_addr =
  2676. dwc_ep->xfer_buff0;
  2677. dwc_ep->cur_pkt_dma_addr =
  2678. dwc_ep->dma_addr0;
  2679. }
  2680. }
  2681. dwc_otg_iso_ep_start_frm_transfer(core_if,
  2682. dwc_ep);
  2683. }
  2684. }
  2685. } else {
  2686. set_current_pkt_info(core_if, dwc_ep);
  2687. if (dwc_ep->cur_pkt >= dwc_ep->pkt_cnt) {
  2688. is_last = 1;
  2689. dwc_ep->cur_pkt = 0;
  2690. dwc_ep->proc_buf_num = (dwc_ep->proc_buf_num ^ 1) & 0x1;
  2691. if (dwc_ep->proc_buf_num) {
  2692. dwc_ep->cur_pkt_addr = dwc_ep->xfer_buff1;
  2693. dwc_ep->cur_pkt_dma_addr = dwc_ep->dma_addr1;
  2694. } else {
  2695. dwc_ep->cur_pkt_addr = dwc_ep->xfer_buff0;
  2696. dwc_ep->cur_pkt_dma_addr = dwc_ep->dma_addr0;
  2697. }
  2698. }
  2699. dwc_otg_iso_ep_start_frm_transfer(core_if, dwc_ep);
  2700. }
  2701. if (is_last)
  2702. dwc_otg_iso_buffer_done(pcd, ep, ep->iso_req_handle);
  2703. }
  2704. #endif /* DWC_EN_ISOC */
  2705. /**
  2706. * This function handle BNA interrupt for Non Isochronous EPs
  2707. *
  2708. */
  2709. static void dwc_otg_pcd_handle_noniso_bna(dwc_otg_pcd_ep_t * ep)
  2710. {
  2711. dwc_ep_t *dwc_ep = &ep->dwc_ep;
  2712. volatile uint32_t *addr;
  2713. depctl_data_t depctl = {.d32 = 0 };
  2714. dwc_otg_pcd_t *pcd = ep->pcd;
  2715. dwc_otg_dev_dma_desc_t *dma_desc;
  2716. dev_dma_desc_sts_t sts = {.d32 = 0 };
  2717. dwc_otg_core_if_t *core_if = ep->pcd->core_if;
  2718. int i, start;
  2719. if (!dwc_ep->desc_cnt)
  2720. DWC_WARN("Descriptor count = %d\n", dwc_ep->desc_cnt);
  2721. if (core_if->core_params->cont_on_bna && !dwc_ep->is_in
  2722. && dwc_ep->type != DWC_OTG_EP_TYPE_CONTROL) {
  2723. uint32_t doepdma;
  2724. dwc_otg_dev_out_ep_regs_t *out_regs =
  2725. core_if->dev_if->out_ep_regs[dwc_ep->num];
  2726. doepdma = DWC_READ_REG32(&(out_regs->doepdma));
  2727. start = (doepdma - dwc_ep->dma_desc_addr)/sizeof(dwc_otg_dev_dma_desc_t);
  2728. dma_desc = &(dwc_ep->desc_addr[start]);
  2729. } else {
  2730. start = 0;
  2731. dma_desc = dwc_ep->desc_addr;
  2732. }
  2733. for (i = start; i < dwc_ep->desc_cnt; ++i, ++dma_desc) {
  2734. sts.d32 = dma_desc->status.d32;
  2735. sts.b.bs = BS_HOST_READY;
  2736. dma_desc->status.d32 = sts.d32;
  2737. }
  2738. if (dwc_ep->is_in == 0) {
  2739. addr =
  2740. &GET_CORE_IF(pcd)->dev_if->out_ep_regs[dwc_ep->
  2741. num]->doepctl;
  2742. } else {
  2743. addr =
  2744. &GET_CORE_IF(pcd)->dev_if->in_ep_regs[dwc_ep->num]->diepctl;
  2745. }
  2746. depctl.b.epena = 1;
  2747. depctl.b.cnak = 1;
  2748. DWC_MODIFY_REG32(addr, 0, depctl.d32);
  2749. }
  2750. /**
  2751. * This function handles EP0 Control transfers.
  2752. *
  2753. * The state of the control tranfers are tracked in
  2754. * <code>ep0state</code>.
  2755. */
  2756. static void handle_ep0(dwc_otg_pcd_t * pcd)
  2757. {
  2758. dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  2759. dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  2760. dev_dma_desc_sts_t desc_sts;
  2761. deptsiz0_data_t deptsiz;
  2762. uint32_t byte_count;
  2763. #ifdef DEBUG_EP0
  2764. DWC_DEBUGPL(DBG_PCDV, "%s()\n", __func__);
  2765. print_ep0_state(pcd);
  2766. #endif
  2767. // DWC_PRINTF("HANDLE EP0\n");
  2768. switch (pcd->ep0state) {
  2769. case EP0_DISCONNECT:
  2770. break;
  2771. case EP0_IDLE:
  2772. pcd->request_config = 0;
  2773. pcd_setup(pcd);
  2774. break;
  2775. case EP0_IN_DATA_PHASE:
  2776. #ifdef DEBUG_EP0
  2777. DWC_DEBUGPL(DBG_PCD, "DATA_IN EP%d-%s: type=%d, mps=%d\n",
  2778. ep0->dwc_ep.num, (ep0->dwc_ep.is_in ? "IN" : "OUT"),
  2779. ep0->dwc_ep.type, ep0->dwc_ep.maxpacket);
  2780. #endif
  2781. if (core_if->dma_enable != 0) {
  2782. /*
  2783. * For EP0 we can only program 1 packet at a time so we
  2784. * need to do the make calculations after each complete.
  2785. * Call write_packet to make the calculations, as in
  2786. * slave mode, and use those values to determine if we
  2787. * can complete.
  2788. */
  2789. if (core_if->dma_desc_enable == 0) {
  2790. deptsiz.d32 =
  2791. DWC_READ_REG32(&core_if->
  2792. dev_if->in_ep_regs[0]->
  2793. dieptsiz);
  2794. byte_count =
  2795. ep0->dwc_ep.xfer_len - deptsiz.b.xfersize;
  2796. } else {
  2797. desc_sts =
  2798. core_if->dev_if->in_desc_addr->status;
  2799. byte_count =
  2800. ep0->dwc_ep.xfer_len - desc_sts.b.bytes;
  2801. }
  2802. ep0->dwc_ep.xfer_count += byte_count;
  2803. ep0->dwc_ep.xfer_buff += byte_count;
  2804. ep0->dwc_ep.dma_addr += byte_count;
  2805. }
  2806. if (ep0->dwc_ep.xfer_count < ep0->dwc_ep.total_len) {
  2807. dwc_otg_ep0_continue_transfer(GET_CORE_IF(pcd),
  2808. &ep0->dwc_ep);
  2809. DWC_DEBUGPL(DBG_PCD, "CONTINUE TRANSFER\n");
  2810. } else if (ep0->dwc_ep.sent_zlp) {
  2811. dwc_otg_ep0_continue_transfer(GET_CORE_IF(pcd),
  2812. &ep0->dwc_ep);
  2813. ep0->dwc_ep.sent_zlp = 0;
  2814. DWC_DEBUGPL(DBG_PCD, "CONTINUE TRANSFER\n");
  2815. } else {
  2816. ep0_complete_request(ep0);
  2817. DWC_DEBUGPL(DBG_PCD, "COMPLETE TRANSFER\n");
  2818. }
  2819. break;
  2820. case EP0_OUT_DATA_PHASE:
  2821. #ifdef DEBUG_EP0
  2822. DWC_DEBUGPL(DBG_PCD, "DATA_OUT EP%d-%s: type=%d, mps=%d\n",
  2823. ep0->dwc_ep.num, (ep0->dwc_ep.is_in ? "IN" : "OUT"),
  2824. ep0->dwc_ep.type, ep0->dwc_ep.maxpacket);
  2825. #endif
  2826. if (core_if->dma_enable != 0) {
  2827. if (core_if->dma_desc_enable == 0) {
  2828. deptsiz.d32 =
  2829. DWC_READ_REG32(&core_if->
  2830. dev_if->out_ep_regs[0]->
  2831. doeptsiz);
  2832. byte_count =
  2833. ep0->dwc_ep.maxpacket - deptsiz.b.xfersize;
  2834. } else {
  2835. desc_sts =
  2836. core_if->dev_if->out_desc_addr->status;
  2837. byte_count =
  2838. ep0->dwc_ep.maxpacket - desc_sts.b.bytes;
  2839. }
  2840. ep0->dwc_ep.xfer_count += byte_count;
  2841. ep0->dwc_ep.xfer_buff += byte_count;
  2842. ep0->dwc_ep.dma_addr += byte_count;
  2843. }
  2844. if (ep0->dwc_ep.xfer_count < ep0->dwc_ep.total_len) {
  2845. dwc_otg_ep0_continue_transfer(GET_CORE_IF(pcd),
  2846. &ep0->dwc_ep);
  2847. DWC_DEBUGPL(DBG_PCD, "CONTINUE TRANSFER\n");
  2848. } else if (ep0->dwc_ep.sent_zlp) {
  2849. dwc_otg_ep0_continue_transfer(GET_CORE_IF(pcd),
  2850. &ep0->dwc_ep);
  2851. ep0->dwc_ep.sent_zlp = 0;
  2852. DWC_DEBUGPL(DBG_PCD, "CONTINUE TRANSFER\n");
  2853. } else {
  2854. ep0_complete_request(ep0);
  2855. DWC_DEBUGPL(DBG_PCD, "COMPLETE TRANSFER\n");
  2856. }
  2857. break;
  2858. case EP0_IN_STATUS_PHASE:
  2859. case EP0_OUT_STATUS_PHASE:
  2860. DWC_DEBUGPL(DBG_PCD, "CASE: EP0_STATUS\n");
  2861. ep0_complete_request(ep0);
  2862. pcd->ep0state = EP0_IDLE;
  2863. ep0->stopped = 1;
  2864. ep0->dwc_ep.is_in = 0; /* OUT for next SETUP */
  2865. /* Prepare for more SETUP Packets */
  2866. if (core_if->dma_enable) {
  2867. ep0_out_start(core_if, pcd);
  2868. }
  2869. break;
  2870. case EP0_STALL:
  2871. DWC_ERROR("EP0 STALLed, should not get here pcd_setup()\n");
  2872. break;
  2873. }
  2874. #ifdef DEBUG_EP0
  2875. print_ep0_state(pcd);
  2876. #endif
  2877. }
  2878. /**
  2879. * Restart transfer
  2880. */
  2881. static void restart_transfer(dwc_otg_pcd_t * pcd, const uint32_t epnum)
  2882. {
  2883. dwc_otg_core_if_t *core_if;
  2884. dwc_otg_dev_if_t *dev_if;
  2885. deptsiz_data_t dieptsiz = {.d32 = 0 };
  2886. dwc_otg_pcd_ep_t *ep;
  2887. ep = get_in_ep(pcd, epnum);
  2888. #ifdef DWC_EN_ISOC
  2889. if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  2890. return;
  2891. }
  2892. #endif /* DWC_EN_ISOC */
  2893. core_if = GET_CORE_IF(pcd);
  2894. dev_if = core_if->dev_if;
  2895. dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dieptsiz);
  2896. DWC_DEBUGPL(DBG_PCD, "xfer_buff=%p xfer_count=%0x xfer_len=%0x"
  2897. " stopped=%d\n", ep->dwc_ep.xfer_buff,
  2898. ep->dwc_ep.xfer_count, ep->dwc_ep.xfer_len, ep->stopped);
  2899. /*
  2900. * If xfersize is 0 and pktcnt in not 0, resend the last packet.
  2901. */
  2902. if (dieptsiz.b.pktcnt && dieptsiz.b.xfersize == 0 &&
  2903. ep->dwc_ep.start_xfer_buff != 0) {
  2904. if (ep->dwc_ep.total_len <= ep->dwc_ep.maxpacket) {
  2905. ep->dwc_ep.xfer_count = 0;
  2906. ep->dwc_ep.xfer_buff = ep->dwc_ep.start_xfer_buff;
  2907. ep->dwc_ep.xfer_len = ep->dwc_ep.xfer_count;
  2908. } else {
  2909. ep->dwc_ep.xfer_count -= ep->dwc_ep.maxpacket;
  2910. /* convert packet size to dwords. */
  2911. ep->dwc_ep.xfer_buff -= ep->dwc_ep.maxpacket;
  2912. ep->dwc_ep.xfer_len = ep->dwc_ep.xfer_count;
  2913. }
  2914. ep->stopped = 0;
  2915. DWC_DEBUGPL(DBG_PCD, "xfer_buff=%p xfer_count=%0x "
  2916. "xfer_len=%0x stopped=%d\n",
  2917. ep->dwc_ep.xfer_buff,
  2918. ep->dwc_ep.xfer_count, ep->dwc_ep.xfer_len,
  2919. ep->stopped);
  2920. if (epnum == 0) {
  2921. //dwc_otg_ep0_start_transfer(core_if, &ep->dwc_ep);
  2922. } else {
  2923. //dwc_otg_ep_start_transfer(core_if, &ep->dwc_ep);
  2924. }
  2925. }
  2926. }
  2927. /*
  2928. * This function create new nextep sequnce based on Learn Queue.
  2929. *
  2930. * @param core_if Programming view of DWC_otg controller
  2931. */
  2932. void predict_nextep_seq( dwc_otg_core_if_t * core_if)
  2933. {
  2934. dwc_otg_device_global_regs_t *dev_global_regs =
  2935. core_if->dev_if->dev_global_regs;
  2936. const uint32_t TOKEN_Q_DEPTH = core_if->hwcfg2.b.dev_token_q_depth;
  2937. /* Number of Token Queue Registers */
  2938. const int DTKNQ_REG_CNT = (TOKEN_Q_DEPTH + 7) / 8;
  2939. dtknq1_data_t dtknqr1;
  2940. uint32_t in_tkn_epnums[4];
  2941. uint8_t seqnum[MAX_EPS_CHANNELS];
  2942. uint8_t intkn_seq[TOKEN_Q_DEPTH];
  2943. grstctl_t resetctl = {.d32 = 0 };
  2944. uint8_t temp;
  2945. int ndx = 0;
  2946. int start = 0;
  2947. int end = 0;
  2948. int sort_done = 0;
  2949. int i = 0;
  2950. volatile uint32_t *addr = &dev_global_regs->dtknqr1;
  2951. DWC_DEBUGPL(DBG_PCD,"dev_token_q_depth=%d\n",TOKEN_Q_DEPTH);
  2952. /* Read the DTKNQ Registers */
  2953. for (i = 0; i < DTKNQ_REG_CNT; i++) {
  2954. in_tkn_epnums[i] = DWC_READ_REG32(addr);
  2955. DWC_DEBUGPL(DBG_PCDV, "DTKNQR%d=0x%08x\n", i + 1,
  2956. in_tkn_epnums[i]);
  2957. if (addr == &dev_global_regs->dvbusdis) {
  2958. addr = &dev_global_regs->dtknqr3_dthrctl;
  2959. } else {
  2960. ++addr;
  2961. }
  2962. }
  2963. /* Copy the DTKNQR1 data to the bit field. */
  2964. dtknqr1.d32 = in_tkn_epnums[0];
  2965. if (dtknqr1.b.wrap_bit) {
  2966. ndx = dtknqr1.b.intknwptr;
  2967. end = ndx -1;
  2968. if (end < 0)
  2969. end = TOKEN_Q_DEPTH -1;
  2970. } else {
  2971. ndx = 0;
  2972. end = dtknqr1.b.intknwptr -1;
  2973. if (end < 0)
  2974. end = 0;
  2975. }
  2976. start = ndx;
  2977. /* Fill seqnum[] by initial values: EP number + 31 */
  2978. for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
  2979. seqnum[i] = i +31;
  2980. }
  2981. /* Fill intkn_seq[] from in_tkn_epnums[0] */
  2982. for (i=0; i < 6; i++)
  2983. intkn_seq[i] = (in_tkn_epnums[0] >> ((7-i) * 4)) & 0xf;
  2984. if (TOKEN_Q_DEPTH > 6) {
  2985. /* Fill intkn_seq[] from in_tkn_epnums[1] */
  2986. for (i=6; i < 14; i++)
  2987. intkn_seq[i] = (in_tkn_epnums[1] >> ((7-(i-6)) * 4)) & 0xf;
  2988. }
  2989. if (TOKEN_Q_DEPTH > 14) {
  2990. /* Fill intkn_seq[] from in_tkn_epnums[1] */
  2991. for (i=14; i < 22; i++)
  2992. intkn_seq[i] = (in_tkn_epnums[2] >> ((7-(i-14)) * 4)) & 0xf;
  2993. }
  2994. if (TOKEN_Q_DEPTH > 22) {
  2995. /* Fill intkn_seq[] from in_tkn_epnums[1] */
  2996. for (i=22; i < 30; i++)
  2997. intkn_seq[i] = (in_tkn_epnums[3] >> ((7-(i-22)) * 4)) & 0xf;
  2998. }
  2999. DWC_DEBUGPL(DBG_PCDV,"%s start=%d end=%d intkn_seq[]:\n", __func__, start, end);
  3000. for (i=0; i<TOKEN_Q_DEPTH; i++)
  3001. DWC_DEBUGPL(DBG_PCDV,"%d\n", intkn_seq[i]);
  3002. /* Update seqnum based on intkn_seq[] */
  3003. i = 0;
  3004. do {
  3005. seqnum[intkn_seq[ndx]] = i;
  3006. ndx++;
  3007. i++;
  3008. if (ndx == TOKEN_Q_DEPTH)
  3009. ndx = 0;
  3010. } while ( i < TOKEN_Q_DEPTH );
  3011. /* Mark non active EP's in seqnum[] by 0xff */
  3012. for (i=0; i<=core_if->dev_if->num_in_eps; i++) {
  3013. if (core_if->nextep_seq[i] == 0xff )
  3014. seqnum[i] = 0xff;
  3015. }
  3016. /* Sort seqnum[] */
  3017. sort_done = 0;
  3018. while (!sort_done) {
  3019. sort_done = 1;
  3020. for (i=0; i<core_if->dev_if->num_in_eps; i++) {
  3021. if (seqnum[i] > seqnum[i+1]) {
  3022. temp = seqnum[i];
  3023. seqnum[i] = seqnum[i+1];
  3024. seqnum[i+1] = temp;
  3025. sort_done = 0;
  3026. }
  3027. }
  3028. }
  3029. ndx = start + seqnum[0];
  3030. if (ndx >= TOKEN_Q_DEPTH)
  3031. ndx = ndx % TOKEN_Q_DEPTH;
  3032. core_if->first_in_nextep_seq = intkn_seq[ndx];
  3033. /* Update seqnum[] by EP numbers */
  3034. for (i=0; i<=core_if->dev_if->num_in_eps; i++) {
  3035. ndx = start + i;
  3036. if (seqnum[i] < 31) {
  3037. ndx = start + seqnum[i];
  3038. if (ndx >= TOKEN_Q_DEPTH)
  3039. ndx = ndx % TOKEN_Q_DEPTH;
  3040. seqnum[i] = intkn_seq[ndx];
  3041. } else {
  3042. if (seqnum[i] < 0xff) {
  3043. seqnum[i] = seqnum[i] - 31;
  3044. } else {
  3045. break;
  3046. }
  3047. }
  3048. }
  3049. /* Update nextep_seq[] based on seqnum[] */
  3050. for (i=0; i<core_if->dev_if->num_in_eps; i++) {
  3051. if (seqnum[i] != 0xff) {
  3052. if (seqnum[i+1] != 0xff) {
  3053. core_if->nextep_seq[seqnum[i]] = seqnum[i+1];
  3054. } else {
  3055. core_if->nextep_seq[seqnum[i]] = core_if->first_in_nextep_seq;
  3056. break;
  3057. }
  3058. } else {
  3059. break;
  3060. }
  3061. }
  3062. DWC_DEBUGPL(DBG_PCDV, "%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
  3063. __func__, core_if->first_in_nextep_seq);
  3064. for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
  3065. DWC_DEBUGPL(DBG_PCDV,"%2d\n", core_if->nextep_seq[i]);
  3066. }
  3067. /* Flush the Learning Queue */
  3068. resetctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->grstctl);
  3069. resetctl.b.intknqflsh = 1;
  3070. DWC_WRITE_REG32(&core_if->core_global_regs->grstctl, resetctl.d32);
  3071. }
  3072. /**
  3073. * handle the IN EP disable interrupt.
  3074. */
  3075. static inline void handle_in_ep_disable_intr(dwc_otg_pcd_t * pcd,
  3076. const uint32_t epnum)
  3077. {
  3078. dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  3079. dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  3080. deptsiz_data_t dieptsiz = {.d32 = 0 };
  3081. dctl_data_t dctl = {.d32 = 0 };
  3082. dwc_otg_pcd_ep_t *ep;
  3083. dwc_ep_t *dwc_ep;
  3084. gintmsk_data_t gintmsk_data;
  3085. depctl_data_t depctl;
  3086. uint32_t diepdma;
  3087. uint32_t remain_to_transfer = 0;
  3088. uint8_t i;
  3089. uint32_t xfer_size;
  3090. ep = get_in_ep(pcd, epnum);
  3091. dwc_ep = &ep->dwc_ep;
  3092. if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  3093. dwc_otg_flush_tx_fifo(core_if, dwc_ep->tx_fifo_num);
  3094. complete_ep(ep);
  3095. return;
  3096. }
  3097. DWC_DEBUGPL(DBG_PCD, "diepctl%d=%0x\n", epnum,
  3098. DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->diepctl));
  3099. dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dieptsiz);
  3100. depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->diepctl);
  3101. DWC_DEBUGPL(DBG_ANY, "pktcnt=%d size=%d\n",
  3102. dieptsiz.b.pktcnt, dieptsiz.b.xfersize);
  3103. if ((core_if->start_predict == 0) || (depctl.b.eptype & 1)) {
  3104. if (ep->stopped) {
  3105. if (core_if->en_multiple_tx_fifo)
  3106. /* Flush the Tx FIFO */
  3107. dwc_otg_flush_tx_fifo(core_if, dwc_ep->tx_fifo_num);
  3108. /* Clear the Global IN NP NAK */
  3109. dctl.d32 = 0;
  3110. dctl.b.cgnpinnak = 1;
  3111. DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
  3112. /* Restart the transaction */
  3113. if (dieptsiz.b.pktcnt != 0 || dieptsiz.b.xfersize != 0) {
  3114. restart_transfer(pcd, epnum);
  3115. }
  3116. } else {
  3117. /* Restart the transaction */
  3118. if (dieptsiz.b.pktcnt != 0 || dieptsiz.b.xfersize != 0) {
  3119. restart_transfer(pcd, epnum);
  3120. }
  3121. DWC_DEBUGPL(DBG_ANY, "STOPPED!!!\n");
  3122. }
  3123. return;
  3124. }
  3125. if (core_if->start_predict > 2) { // NP IN EP
  3126. core_if->start_predict--;
  3127. return;
  3128. }
  3129. core_if->start_predict--;
  3130. if (core_if->start_predict == 1) { // All NP IN Ep's disabled now
  3131. predict_nextep_seq(core_if);
  3132. /* Update all active IN EP's NextEP field based of nextep_seq[] */
  3133. for ( i = 0; i <= core_if->dev_if->num_in_eps; i++) {
  3134. depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  3135. if (core_if->nextep_seq[i] != 0xff) { // Active NP IN EP
  3136. depctl.b.nextep = core_if->nextep_seq[i];
  3137. DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepctl, depctl.d32);
  3138. }
  3139. }
  3140. /* Flush Shared NP TxFIFO */
  3141. dwc_otg_flush_tx_fifo(core_if, 0);
  3142. /* Rewind buffers */
  3143. if (!core_if->dma_desc_enable) {
  3144. i = core_if->first_in_nextep_seq;
  3145. do {
  3146. ep = get_in_ep(pcd, i);
  3147. dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->dieptsiz);
  3148. xfer_size = ep->dwc_ep.total_len - ep->dwc_ep.xfer_count;
  3149. if (xfer_size > ep->dwc_ep.maxxfer)
  3150. xfer_size = ep->dwc_ep.maxxfer;
  3151. depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  3152. if (dieptsiz.b.pktcnt != 0) {
  3153. if (xfer_size == 0) {
  3154. remain_to_transfer = 0;
  3155. } else {
  3156. if ((xfer_size % ep->dwc_ep.maxpacket) == 0) {
  3157. remain_to_transfer =
  3158. dieptsiz.b.pktcnt * ep->dwc_ep.maxpacket;
  3159. } else {
  3160. remain_to_transfer = ((dieptsiz.b.pktcnt -1) * ep->dwc_ep.maxpacket)
  3161. + (xfer_size % ep->dwc_ep.maxpacket);
  3162. }
  3163. }
  3164. diepdma = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepdma);
  3165. dieptsiz.b.xfersize = remain_to_transfer;
  3166. DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->dieptsiz, dieptsiz.d32);
  3167. diepdma = ep->dwc_ep.dma_addr + (xfer_size - remain_to_transfer);
  3168. DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepdma, diepdma);
  3169. }
  3170. i = core_if->nextep_seq[i];
  3171. } while (i != core_if->first_in_nextep_seq);
  3172. } else { // dma_desc_enable
  3173. DWC_PRINTF("%s Learning Queue not supported in DDMA\n", __func__);
  3174. }
  3175. /* Restart transfers in predicted sequences */
  3176. i = core_if->first_in_nextep_seq;
  3177. do {
  3178. dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->dieptsiz);
  3179. depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  3180. if (dieptsiz.b.pktcnt != 0) {
  3181. depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  3182. depctl.b.epena = 1;
  3183. depctl.b.cnak = 1;
  3184. DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepctl, depctl.d32);
  3185. }
  3186. i = core_if->nextep_seq[i];
  3187. } while (i != core_if->first_in_nextep_seq);
  3188. /* Clear the global non-periodic IN NAK handshake */
  3189. dctl.d32 = 0;
  3190. dctl.b.cgnpinnak = 1;
  3191. DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
  3192. /* Unmask EP Mismatch interrupt */
  3193. gintmsk_data.d32 = 0;
  3194. gintmsk_data.b.epmismatch = 1;
  3195. DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0, gintmsk_data.d32);
  3196. core_if->start_predict = 0;
  3197. }
  3198. }
  3199. /**
  3200. * Handler for the IN EP timeout handshake interrupt.
  3201. */
  3202. static inline void handle_in_ep_timeout_intr(dwc_otg_pcd_t * pcd,
  3203. const uint32_t epnum)
  3204. {
  3205. dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  3206. dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  3207. #ifdef DEBUG
  3208. deptsiz_data_t dieptsiz = {.d32 = 0 };
  3209. uint32_t num = 0;
  3210. #endif
  3211. dctl_data_t dctl = {.d32 = 0 };
  3212. dwc_otg_pcd_ep_t *ep;
  3213. gintmsk_data_t intr_mask = {.d32 = 0 };
  3214. ep = get_in_ep(pcd, epnum);
  3215. /* Disable the NP Tx Fifo Empty Interrrupt */
  3216. if (!core_if->dma_enable) {
  3217. intr_mask.b.nptxfempty = 1;
  3218. DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk,
  3219. intr_mask.d32, 0);
  3220. }
  3221. /** @todo NGS Check EP type.
  3222. * Implement for Periodic EPs */
  3223. /*
  3224. * Non-periodic EP
  3225. */
  3226. /* Enable the Global IN NAK Effective Interrupt */
  3227. intr_mask.b.ginnakeff = 1;
  3228. DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0, intr_mask.d32);
  3229. /* Set Global IN NAK */
  3230. dctl.b.sgnpinnak = 1;
  3231. DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
  3232. ep->stopped = 1;
  3233. #ifdef DEBUG
  3234. dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[num]->dieptsiz);
  3235. DWC_DEBUGPL(DBG_ANY, "pktcnt=%d size=%d\n",
  3236. dieptsiz.b.pktcnt, dieptsiz.b.xfersize);
  3237. #endif
  3238. #ifdef DISABLE_PERIODIC_EP
  3239. /*
  3240. * Set the NAK bit for this EP to
  3241. * start the disable process.
  3242. */
  3243. diepctl.d32 = 0;
  3244. diepctl.b.snak = 1;
  3245. DWC_MODIFY_REG32(&dev_if->in_ep_regs[num]->diepctl, diepctl.d32,
  3246. diepctl.d32);
  3247. ep->disabling = 1;
  3248. ep->stopped = 1;
  3249. #endif
  3250. }
  3251. /**
  3252. * Handler for the IN EP NAK interrupt.
  3253. */
  3254. static inline int32_t handle_in_ep_nak_intr(dwc_otg_pcd_t * pcd,
  3255. const uint32_t epnum)
  3256. {
  3257. /** @todo implement ISR */
  3258. dwc_otg_core_if_t *core_if;
  3259. diepmsk_data_t intr_mask = {.d32 = 0 };
  3260. DWC_PRINTF("INTERRUPT Handler not implemented for %s\n", "IN EP NAK");
  3261. core_if = GET_CORE_IF(pcd);
  3262. intr_mask.b.nak = 1;
  3263. if (core_if->multiproc_int_enable) {
  3264. DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  3265. diepeachintmsk[epnum], intr_mask.d32, 0);
  3266. } else {
  3267. DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->diepmsk,
  3268. intr_mask.d32, 0);
  3269. }
  3270. return 1;
  3271. }
  3272. /**
  3273. * Handler for the OUT EP Babble interrupt.
  3274. */
  3275. static inline int32_t handle_out_ep_babble_intr(dwc_otg_pcd_t * pcd,
  3276. const uint32_t epnum)
  3277. {
  3278. /** @todo implement ISR */
  3279. dwc_otg_core_if_t *core_if;
  3280. doepmsk_data_t intr_mask = {.d32 = 0 };
  3281. DWC_PRINTF("INTERRUPT Handler not implemented for %s\n",
  3282. "OUT EP Babble");
  3283. core_if = GET_CORE_IF(pcd);
  3284. intr_mask.b.babble = 1;
  3285. if (core_if->multiproc_int_enable) {
  3286. DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  3287. doepeachintmsk[epnum], intr_mask.d32, 0);
  3288. } else {
  3289. DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->doepmsk,
  3290. intr_mask.d32, 0);
  3291. }
  3292. return 1;
  3293. }
  3294. /**
  3295. * Handler for the OUT EP NAK interrupt.
  3296. */
  3297. static inline int32_t handle_out_ep_nak_intr(dwc_otg_pcd_t * pcd,
  3298. const uint32_t epnum)
  3299. {
  3300. /** @todo implement ISR */
  3301. dwc_otg_core_if_t *core_if;
  3302. doepmsk_data_t intr_mask = {.d32 = 0 };
  3303. DWC_PRINTF("INTERRUPT Handler not implemented for %s\n", "OUT EP NAK");
  3304. core_if = GET_CORE_IF(pcd);
  3305. intr_mask.b.nak = 1;
  3306. if (core_if->multiproc_int_enable) {
  3307. DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  3308. doepeachintmsk[epnum], intr_mask.d32, 0);
  3309. } else {
  3310. DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->doepmsk,
  3311. intr_mask.d32, 0);
  3312. }
  3313. return 1;
  3314. }
  3315. /**
  3316. * Handler for the OUT EP NYET interrupt.
  3317. */
  3318. static inline int32_t handle_out_ep_nyet_intr(dwc_otg_pcd_t * pcd,
  3319. const uint32_t epnum)
  3320. {
  3321. /** @todo implement ISR */
  3322. dwc_otg_core_if_t *core_if;
  3323. doepmsk_data_t intr_mask = {.d32 = 0 };
  3324. DWC_PRINTF("INTERRUPT Handler not implemented for %s\n", "OUT EP NYET");
  3325. core_if = GET_CORE_IF(pcd);
  3326. intr_mask.b.nyet = 1;
  3327. if (core_if->multiproc_int_enable) {
  3328. DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  3329. doepeachintmsk[epnum], intr_mask.d32, 0);
  3330. } else {
  3331. DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->doepmsk,
  3332. intr_mask.d32, 0);
  3333. }
  3334. return 1;
  3335. }
  3336. /**
  3337. * This interrupt indicates that an IN EP has a pending Interrupt.
  3338. * The sequence for handling the IN EP interrupt is shown below:
  3339. * -# Read the Device All Endpoint Interrupt register
  3340. * -# Repeat the following for each IN EP interrupt bit set (from
  3341. * LSB to MSB).
  3342. * -# Read the Device Endpoint Interrupt (DIEPINTn) register
  3343. * -# If "Transfer Complete" call the request complete function
  3344. * -# If "Endpoint Disabled" complete the EP disable procedure.
  3345. * -# If "AHB Error Interrupt" log error
  3346. * -# If "Time-out Handshake" log error
  3347. * -# If "IN Token Received when TxFIFO Empty" write packet to Tx
  3348. * FIFO.
  3349. * -# If "IN Token EP Mismatch" (disable, this is handled by EP
  3350. * Mismatch Interrupt)
  3351. */
  3352. static int32_t dwc_otg_pcd_handle_in_ep_intr(dwc_otg_pcd_t * pcd)
  3353. {
  3354. #define CLEAR_IN_EP_INTR(__core_if,__epnum,__intr) \
  3355. do { \
  3356. diepint_data_t diepint = {.d32=0}; \
  3357. diepint.b.__intr = 1; \
  3358. DWC_WRITE_REG32(&__core_if->dev_if->in_ep_regs[__epnum]->diepint, \
  3359. diepint.d32); \
  3360. } while (0)
  3361. dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  3362. dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  3363. diepint_data_t diepint = {.d32 = 0 };
  3364. depctl_data_t depctl = {.d32 = 0 };
  3365. uint32_t ep_intr;
  3366. uint32_t epnum = 0;
  3367. dwc_otg_pcd_ep_t *ep;
  3368. dwc_ep_t *dwc_ep;
  3369. gintmsk_data_t intr_mask = {.d32 = 0 };
  3370. DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, pcd);
  3371. /* Read in the device interrupt bits */
  3372. ep_intr = dwc_otg_read_dev_all_in_ep_intr(core_if);
  3373. /* Service the Device IN interrupts for each endpoint */
  3374. while (ep_intr) {
  3375. if (ep_intr & 0x1) {
  3376. uint32_t empty_msk;
  3377. /* Get EP pointer */
  3378. ep = get_in_ep(pcd, epnum);
  3379. dwc_ep = &ep->dwc_ep;
  3380. depctl.d32 =
  3381. DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->diepctl);
  3382. empty_msk =
  3383. DWC_READ_REG32(&dev_if->
  3384. dev_global_regs->dtknqr4_fifoemptymsk);
  3385. DWC_DEBUGPL(DBG_PCDV,
  3386. "IN EP INTERRUPT - %d\nepmty_msk - %8x diepctl - %8x\n",
  3387. epnum, empty_msk, depctl.d32);
  3388. DWC_DEBUGPL(DBG_PCD,
  3389. "EP%d-%s: type=%d, mps=%d\n",
  3390. dwc_ep->num, (dwc_ep->is_in ? "IN" : "OUT"),
  3391. dwc_ep->type, dwc_ep->maxpacket);
  3392. diepint.d32 =
  3393. dwc_otg_read_dev_in_ep_intr(core_if, dwc_ep);
  3394. DWC_DEBUGPL(DBG_PCDV,
  3395. "EP %d Interrupt Register - 0x%x\n", epnum,
  3396. diepint.d32);
  3397. /* Transfer complete */
  3398. if (diepint.b.xfercompl) {
  3399. /* Disable the NP Tx FIFO Empty
  3400. * Interrrupt */
  3401. if (core_if->en_multiple_tx_fifo == 0) {
  3402. intr_mask.b.nptxfempty = 1;
  3403. DWC_MODIFY_REG32
  3404. (&core_if->core_global_regs->gintmsk,
  3405. intr_mask.d32, 0);
  3406. } else {
  3407. /* Disable the Tx FIFO Empty Interrupt for this EP */
  3408. uint32_t fifoemptymsk =
  3409. 0x1 << dwc_ep->num;
  3410. DWC_MODIFY_REG32(&core_if->
  3411. dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
  3412. fifoemptymsk, 0);
  3413. }
  3414. /* Clear the bit in DIEPINTn for this interrupt */
  3415. CLEAR_IN_EP_INTR(core_if, epnum, xfercompl);
  3416. /* Complete the transfer */
  3417. if (epnum == 0) {
  3418. handle_ep0(pcd);
  3419. }
  3420. #ifdef DWC_EN_ISOC
  3421. else if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  3422. if (!ep->stopped)
  3423. complete_iso_ep(pcd, ep);
  3424. }
  3425. #endif /* DWC_EN_ISOC */
  3426. #ifdef DWC_UTE_PER_IO
  3427. else if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  3428. if (!ep->stopped)
  3429. complete_xiso_ep(ep);
  3430. }
  3431. #endif /* DWC_UTE_PER_IO */
  3432. else {
  3433. if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC &&
  3434. dwc_ep->bInterval > 1) {
  3435. dwc_ep->frame_num += dwc_ep->bInterval;
  3436. if (dwc_ep->frame_num > 0x3FFF)
  3437. {
  3438. dwc_ep->frm_overrun = 1;
  3439. dwc_ep->frame_num &= 0x3FFF;
  3440. } else
  3441. dwc_ep->frm_overrun = 0;
  3442. }
  3443. complete_ep(ep);
  3444. if(diepint.b.nak)
  3445. CLEAR_IN_EP_INTR(core_if, epnum, nak);
  3446. }
  3447. }
  3448. /* Endpoint disable */
  3449. if (diepint.b.epdisabled) {
  3450. DWC_DEBUGPL(DBG_ANY, "EP%d IN disabled\n",
  3451. epnum);
  3452. handle_in_ep_disable_intr(pcd, epnum);
  3453. /* Clear the bit in DIEPINTn for this interrupt */
  3454. CLEAR_IN_EP_INTR(core_if, epnum, epdisabled);
  3455. }
  3456. /* AHB Error */
  3457. if (diepint.b.ahberr) {
  3458. DWC_ERROR("EP%d IN AHB Error\n", epnum);
  3459. /* Clear the bit in DIEPINTn for this interrupt */
  3460. CLEAR_IN_EP_INTR(core_if, epnum, ahberr);
  3461. }
  3462. /* TimeOUT Handshake (non-ISOC IN EPs) */
  3463. if (diepint.b.timeout) {
  3464. DWC_ERROR("EP%d IN Time-out\n", epnum);
  3465. handle_in_ep_timeout_intr(pcd, epnum);
  3466. CLEAR_IN_EP_INTR(core_if, epnum, timeout);
  3467. }
  3468. /** IN Token received with TxF Empty */
  3469. if (diepint.b.intktxfemp) {
  3470. DWC_DEBUGPL(DBG_ANY,
  3471. "EP%d IN TKN TxFifo Empty\n",
  3472. epnum);
  3473. if (!ep->stopped && epnum != 0) {
  3474. diepmsk_data_t diepmsk = {.d32 = 0 };
  3475. diepmsk.b.intktxfemp = 1;
  3476. if (core_if->multiproc_int_enable) {
  3477. DWC_MODIFY_REG32
  3478. (&dev_if->dev_global_regs->diepeachintmsk
  3479. [epnum], diepmsk.d32, 0);
  3480. } else {
  3481. DWC_MODIFY_REG32
  3482. (&dev_if->dev_global_regs->diepmsk,
  3483. diepmsk.d32, 0);
  3484. }
  3485. } else if (core_if->dma_desc_enable
  3486. && epnum == 0
  3487. && pcd->ep0state ==
  3488. EP0_OUT_STATUS_PHASE) {
  3489. // EP0 IN set STALL
  3490. depctl.d32 =
  3491. DWC_READ_REG32(&dev_if->in_ep_regs
  3492. [epnum]->diepctl);
  3493. /* set the disable and stall bits */
  3494. if (depctl.b.epena) {
  3495. depctl.b.epdis = 1;
  3496. }
  3497. depctl.b.stall = 1;
  3498. DWC_WRITE_REG32(&dev_if->in_ep_regs
  3499. [epnum]->diepctl,
  3500. depctl.d32);
  3501. }
  3502. CLEAR_IN_EP_INTR(core_if, epnum, intktxfemp);
  3503. }
  3504. /** IN Token Received with EP mismatch */
  3505. if (diepint.b.intknepmis) {
  3506. DWC_DEBUGPL(DBG_ANY,
  3507. "EP%d IN TKN EP Mismatch\n", epnum);
  3508. CLEAR_IN_EP_INTR(core_if, epnum, intknepmis);
  3509. }
  3510. /** IN Endpoint NAK Effective */
  3511. if (diepint.b.inepnakeff) {
  3512. DWC_DEBUGPL(DBG_ANY,
  3513. "EP%d IN EP NAK Effective\n",
  3514. epnum);
  3515. /* Periodic EP */
  3516. if (ep->disabling) {
  3517. depctl.d32 = 0;
  3518. depctl.b.snak = 1;
  3519. depctl.b.epdis = 1;
  3520. DWC_MODIFY_REG32(&dev_if->in_ep_regs
  3521. [epnum]->diepctl,
  3522. depctl.d32,
  3523. depctl.d32);
  3524. }
  3525. CLEAR_IN_EP_INTR(core_if, epnum, inepnakeff);
  3526. }
  3527. /** IN EP Tx FIFO Empty Intr */
  3528. if (diepint.b.emptyintr) {
  3529. DWC_DEBUGPL(DBG_ANY,
  3530. "EP%d Tx FIFO Empty Intr \n",
  3531. epnum);
  3532. write_empty_tx_fifo(pcd, epnum);
  3533. CLEAR_IN_EP_INTR(core_if, epnum, emptyintr);
  3534. }
  3535. /** IN EP BNA Intr */
  3536. if (diepint.b.bna) {
  3537. CLEAR_IN_EP_INTR(core_if, epnum, bna);
  3538. if (core_if->dma_desc_enable) {
  3539. #ifdef DWC_EN_ISOC
  3540. if (dwc_ep->type ==
  3541. DWC_OTG_EP_TYPE_ISOC) {
  3542. /*
  3543. * This checking is performed to prevent first "false" BNA
  3544. * handling occuring right after reconnect
  3545. */
  3546. if (dwc_ep->next_frame !=
  3547. 0xffffffff)
  3548. dwc_otg_pcd_handle_iso_bna(ep);
  3549. } else
  3550. #endif /* DWC_EN_ISOC */
  3551. {
  3552. dwc_otg_pcd_handle_noniso_bna(ep);
  3553. }
  3554. }
  3555. }
  3556. /* NAK Interrutp */
  3557. if (diepint.b.nak) {
  3558. DWC_DEBUGPL(DBG_ANY, "EP%d IN NAK Interrupt\n",
  3559. epnum);
  3560. if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC)
  3561. {
  3562. depctl_data_t depctl;
  3563. if (ep->dwc_ep.frame_num == 0xFFFFFFFF)
  3564. {
  3565. ep->dwc_ep.frame_num = core_if->frame_num;
  3566. if (ep->dwc_ep.bInterval > 1)
  3567. {
  3568. depctl.d32 = 0;
  3569. depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->diepctl);
  3570. if (ep->dwc_ep.frame_num & 0x1) {
  3571. depctl.b.setd1pid = 1;
  3572. depctl.b.setd0pid = 0;
  3573. } else {
  3574. depctl.b.setd0pid = 1;
  3575. depctl.b.setd1pid = 0;
  3576. }
  3577. DWC_WRITE_REG32(&dev_if->in_ep_regs[epnum]->diepctl, depctl.d32);
  3578. }
  3579. start_next_request(ep);
  3580. }
  3581. ep->dwc_ep.frame_num += ep->dwc_ep.bInterval;
  3582. if (dwc_ep->frame_num > 0x3FFF)
  3583. {
  3584. dwc_ep->frm_overrun = 1;
  3585. dwc_ep->frame_num &= 0x3FFF;
  3586. } else
  3587. dwc_ep->frm_overrun = 0;
  3588. }
  3589. CLEAR_IN_EP_INTR(core_if, epnum, nak);
  3590. }
  3591. }
  3592. epnum++;
  3593. ep_intr >>= 1;
  3594. }
  3595. return 1;
  3596. #undef CLEAR_IN_EP_INTR
  3597. }
  3598. /**
  3599. * This interrupt indicates that an OUT EP has a pending Interrupt.
  3600. * The sequence for handling the OUT EP interrupt is shown below:
  3601. * -# Read the Device All Endpoint Interrupt register
  3602. * -# Repeat the following for each OUT EP interrupt bit set (from
  3603. * LSB to MSB).
  3604. * -# Read the Device Endpoint Interrupt (DOEPINTn) register
  3605. * -# If "Transfer Complete" call the request complete function
  3606. * -# If "Endpoint Disabled" complete the EP disable procedure.
  3607. * -# If "AHB Error Interrupt" log error
  3608. * -# If "Setup Phase Done" process Setup Packet (See Standard USB
  3609. * Command Processing)
  3610. */
  3611. static int32_t dwc_otg_pcd_handle_out_ep_intr(dwc_otg_pcd_t * pcd)
  3612. {
  3613. #define CLEAR_OUT_EP_INTR(__core_if,__epnum,__intr) \
  3614. do { \
  3615. doepint_data_t doepint = {.d32=0}; \
  3616. doepint.b.__intr = 1; \
  3617. DWC_WRITE_REG32(&__core_if->dev_if->out_ep_regs[__epnum]->doepint, \
  3618. doepint.d32); \
  3619. } while (0)
  3620. dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  3621. uint32_t ep_intr;
  3622. doepint_data_t doepint = {.d32 = 0 };
  3623. uint32_t epnum = 0;
  3624. dwc_otg_pcd_ep_t *ep;
  3625. dwc_ep_t *dwc_ep;
  3626. dctl_data_t dctl = {.d32 = 0 };
  3627. gintmsk_data_t gintmsk = {.d32 = 0 };
  3628. DWC_DEBUGPL(DBG_PCDV, "%s()\n", __func__);
  3629. /* Read in the device interrupt bits */
  3630. ep_intr = dwc_otg_read_dev_all_out_ep_intr(core_if);
  3631. while (ep_intr) {
  3632. if (ep_intr & 0x1) {
  3633. /* Get EP pointer */
  3634. ep = get_out_ep(pcd, epnum);
  3635. dwc_ep = &ep->dwc_ep;
  3636. #ifdef VERBOSE
  3637. DWC_DEBUGPL(DBG_PCDV,
  3638. "EP%d-%s: type=%d, mps=%d\n",
  3639. dwc_ep->num, (dwc_ep->is_in ? "IN" : "OUT"),
  3640. dwc_ep->type, dwc_ep->maxpacket);
  3641. #endif
  3642. doepint.d32 =
  3643. dwc_otg_read_dev_out_ep_intr(core_if, dwc_ep);
  3644. /* Transfer complete */
  3645. if (doepint.b.xfercompl) {
  3646. if (epnum == 0) {
  3647. /* Clear the bit in DOEPINTn for this interrupt */
  3648. CLEAR_OUT_EP_INTR(core_if, epnum,
  3649. xfercompl);
  3650. if (core_if->dma_desc_enable == 0
  3651. || pcd->ep0state != EP0_IDLE)
  3652. handle_ep0(pcd);
  3653. #ifdef DWC_EN_ISOC
  3654. } else if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  3655. if (doepint.b.pktdrpsts == 0) {
  3656. /* Clear the bit in DOEPINTn for this interrupt */
  3657. CLEAR_OUT_EP_INTR(core_if,
  3658. epnum,
  3659. xfercompl);
  3660. complete_iso_ep(pcd, ep);
  3661. } else {
  3662. doepint_data_t doepint = {.d32 = 0 };
  3663. doepint.b.xfercompl = 1;
  3664. doepint.b.pktdrpsts = 1;
  3665. DWC_WRITE_REG32
  3666. (&core_if->dev_if->out_ep_regs
  3667. [epnum]->doepint,
  3668. doepint.d32);
  3669. if (handle_iso_out_pkt_dropped
  3670. (core_if, dwc_ep)) {
  3671. complete_iso_ep(pcd,
  3672. ep);
  3673. }
  3674. }
  3675. #endif /* DWC_EN_ISOC */
  3676. #ifdef DWC_UTE_PER_IO
  3677. } else if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  3678. CLEAR_OUT_EP_INTR(core_if, epnum, xfercompl);
  3679. if (!ep->stopped)
  3680. complete_xiso_ep(ep);
  3681. #endif /* DWC_UTE_PER_IO */
  3682. } else {
  3683. /* Clear the bit in DOEPINTn for this interrupt */
  3684. CLEAR_OUT_EP_INTR(core_if, epnum,
  3685. xfercompl);
  3686. if (core_if->core_params->dev_out_nak) {
  3687. DWC_TIMER_CANCEL(pcd->core_if->ep_xfer_timer[epnum]);
  3688. pcd->core_if->ep_xfer_info[epnum].state = 0;
  3689. #ifdef DEBUG
  3690. print_memory_payload(pcd, dwc_ep);
  3691. #endif
  3692. }
  3693. complete_ep(ep);
  3694. }
  3695. }
  3696. /* Endpoint disable */
  3697. if (doepint.b.epdisabled) {
  3698. /* Clear the bit in DOEPINTn for this interrupt */
  3699. CLEAR_OUT_EP_INTR(core_if, epnum, epdisabled);
  3700. if (core_if->core_params->dev_out_nak) {
  3701. #ifdef DEBUG
  3702. print_memory_payload(pcd, dwc_ep);
  3703. #endif
  3704. /* In case of timeout condition */
  3705. if (core_if->ep_xfer_info[epnum].state == 2) {
  3706. dctl.d32 = DWC_READ_REG32(&core_if->dev_if->
  3707. dev_global_regs->dctl);
  3708. dctl.b.cgoutnak = 1;
  3709. DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl,
  3710. dctl.d32);
  3711. /* Unmask goutnakeff interrupt which was masked
  3712. * during handle nak out interrupt */
  3713. gintmsk.b.goutnakeff = 1;
  3714. DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk,
  3715. 0, gintmsk.d32);
  3716. complete_ep(ep);
  3717. }
  3718. }
  3719. if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC)
  3720. {
  3721. dctl_data_t dctl;
  3722. gintmsk_data_t intr_mask = {.d32 = 0};
  3723. dwc_otg_pcd_request_t *req = 0;
  3724. dctl.d32 = DWC_READ_REG32(&core_if->dev_if->
  3725. dev_global_regs->dctl);
  3726. dctl.b.cgoutnak = 1;
  3727. DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl,
  3728. dctl.d32);
  3729. intr_mask.d32 = 0;
  3730. intr_mask.b.incomplisoout = 1;
  3731. /* Get any pending requests */
  3732. if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  3733. req = DWC_CIRCLEQ_FIRST(&ep->queue);
  3734. if (!req) {
  3735. DWC_PRINTF("complete_ep 0x%p, req = NULL!\n", ep);
  3736. } else {
  3737. dwc_otg_request_done(ep, req, 0);
  3738. start_next_request(ep);
  3739. }
  3740. } else {
  3741. DWC_PRINTF("complete_ep 0x%p, ep->queue empty!\n", ep);
  3742. }
  3743. }
  3744. }
  3745. /* AHB Error */
  3746. if (doepint.b.ahberr) {
  3747. DWC_ERROR("EP%d OUT AHB Error\n", epnum);
  3748. DWC_ERROR("EP%d DEPDMA=0x%08x \n",
  3749. epnum, core_if->dev_if->out_ep_regs[epnum]->doepdma);
  3750. CLEAR_OUT_EP_INTR(core_if, epnum, ahberr);
  3751. }
  3752. /* Setup Phase Done (contorl EPs) */
  3753. if (doepint.b.setup) {
  3754. #ifdef DEBUG_EP0
  3755. DWC_DEBUGPL(DBG_PCD, "EP%d SETUP Done\n",
  3756. epnum);
  3757. #endif
  3758. CLEAR_OUT_EP_INTR(core_if, epnum, setup);
  3759. handle_ep0(pcd);
  3760. }
  3761. /** OUT EP BNA Intr */
  3762. if (doepint.b.bna) {
  3763. CLEAR_OUT_EP_INTR(core_if, epnum, bna);
  3764. if (core_if->dma_desc_enable) {
  3765. #ifdef DWC_EN_ISOC
  3766. if (dwc_ep->type ==
  3767. DWC_OTG_EP_TYPE_ISOC) {
  3768. /*
  3769. * This checking is performed to prevent first "false" BNA
  3770. * handling occuring right after reconnect
  3771. */
  3772. if (dwc_ep->next_frame !=
  3773. 0xffffffff)
  3774. dwc_otg_pcd_handle_iso_bna(ep);
  3775. } else
  3776. #endif /* DWC_EN_ISOC */
  3777. {
  3778. dwc_otg_pcd_handle_noniso_bna(ep);
  3779. }
  3780. }
  3781. }
  3782. if (doepint.b.stsphsercvd) {
  3783. CLEAR_OUT_EP_INTR(core_if, epnum, stsphsercvd);
  3784. if (core_if->dma_desc_enable) {
  3785. do_setup_in_status_phase(pcd);
  3786. }
  3787. }
  3788. /* Babble Interrutp */
  3789. if (doepint.b.babble) {
  3790. DWC_DEBUGPL(DBG_ANY, "EP%d OUT Babble\n",
  3791. epnum);
  3792. handle_out_ep_babble_intr(pcd, epnum);
  3793. CLEAR_OUT_EP_INTR(core_if, epnum, babble);
  3794. }
  3795. if (doepint.b.outtknepdis)
  3796. {
  3797. DWC_DEBUGPL(DBG_ANY, "EP%d OUT Token received when EP is \
  3798. disabled\n",epnum);
  3799. if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC)
  3800. {
  3801. doepmsk_data_t doepmsk = {.d32 = 0};
  3802. ep->dwc_ep.frame_num = core_if->frame_num;
  3803. if (ep->dwc_ep.bInterval > 1)
  3804. {
  3805. depctl_data_t depctl;
  3806. depctl.d32 = DWC_READ_REG32(&core_if->dev_if->
  3807. out_ep_regs[epnum]->doepctl);
  3808. if (ep->dwc_ep.frame_num & 0x1) {
  3809. depctl.b.setd1pid = 1;
  3810. depctl.b.setd0pid = 0;
  3811. } else {
  3812. depctl.b.setd0pid = 1;
  3813. depctl.b.setd1pid = 0;
  3814. }
  3815. DWC_WRITE_REG32(&core_if->dev_if->
  3816. out_ep_regs[epnum]->doepctl, depctl.d32);
  3817. }
  3818. start_next_request(ep);
  3819. doepmsk.b.outtknepdis = 1;
  3820. DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->doepmsk,
  3821. doepmsk.d32, 0);
  3822. }
  3823. CLEAR_OUT_EP_INTR(core_if, epnum, outtknepdis);
  3824. }
  3825. /* NAK Interrutp */
  3826. if (doepint.b.nak) {
  3827. DWC_DEBUGPL(DBG_ANY, "EP%d OUT NAK\n", epnum);
  3828. handle_out_ep_nak_intr(pcd, epnum);
  3829. CLEAR_OUT_EP_INTR(core_if, epnum, nak);
  3830. }
  3831. /* NYET Interrutp */
  3832. if (doepint.b.nyet) {
  3833. DWC_DEBUGPL(DBG_ANY, "EP%d OUT NYET\n", epnum);
  3834. handle_out_ep_nyet_intr(pcd, epnum);
  3835. CLEAR_OUT_EP_INTR(core_if, epnum, nyet);
  3836. }
  3837. }
  3838. epnum++;
  3839. ep_intr >>= 1;
  3840. }
  3841. return 1;
  3842. #undef CLEAR_OUT_EP_INTR
  3843. }
  3844. static int drop_transfer(uint32_t trgt_fr, uint32_t curr_fr, uint8_t frm_overrun)
  3845. {
  3846. int retval = 0;
  3847. if(!frm_overrun && curr_fr >= trgt_fr)
  3848. retval = 1;
  3849. else if (frm_overrun && (curr_fr >= trgt_fr && ((curr_fr - trgt_fr) < 0x3FFF/2)))
  3850. retval = 1;
  3851. return retval;
  3852. }
  3853. /**
  3854. * Incomplete ISO IN Transfer Interrupt.
  3855. * This interrupt indicates one of the following conditions occurred
  3856. * while transmitting an ISOC transaction.
  3857. * - Corrupted IN Token for ISOC EP.
  3858. * - Packet not complete in FIFO.
  3859. * The follow actions will be taken:
  3860. * -# Determine the EP
  3861. * -# Set incomplete flag in dwc_ep structure
  3862. * -# Disable EP; when "Endpoint Disabled" interrupt is received
  3863. * Flush FIFO
  3864. */
  3865. int32_t dwc_otg_pcd_handle_incomplete_isoc_in_intr(dwc_otg_pcd_t * pcd)
  3866. {
  3867. gintsts_data_t gintsts;
  3868. #ifdef DWC_EN_ISOC
  3869. dwc_otg_dev_if_t *dev_if;
  3870. deptsiz_data_t deptsiz = {.d32 = 0 };
  3871. depctl_data_t depctl = {.d32 = 0 };
  3872. dsts_data_t dsts = {.d32 = 0 };
  3873. dwc_ep_t *dwc_ep;
  3874. int i;
  3875. dev_if = GET_CORE_IF(pcd)->dev_if;
  3876. for (i = 1; i <= dev_if->num_in_eps; ++i) {
  3877. dwc_ep = &pcd->in_ep[i].dwc_ep;
  3878. if (dwc_ep->active && dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  3879. deptsiz.d32 =
  3880. DWC_READ_REG32(&dev_if->in_ep_regs[i]->dieptsiz);
  3881. depctl.d32 =
  3882. DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  3883. if (depctl.b.epdis && deptsiz.d32) {
  3884. set_current_pkt_info(GET_CORE_IF(pcd), dwc_ep);
  3885. if (dwc_ep->cur_pkt >= dwc_ep->pkt_cnt) {
  3886. dwc_ep->cur_pkt = 0;
  3887. dwc_ep->proc_buf_num =
  3888. (dwc_ep->proc_buf_num ^ 1) & 0x1;
  3889. if (dwc_ep->proc_buf_num) {
  3890. dwc_ep->cur_pkt_addr =
  3891. dwc_ep->xfer_buff1;
  3892. dwc_ep->cur_pkt_dma_addr =
  3893. dwc_ep->dma_addr1;
  3894. } else {
  3895. dwc_ep->cur_pkt_addr =
  3896. dwc_ep->xfer_buff0;
  3897. dwc_ep->cur_pkt_dma_addr =
  3898. dwc_ep->dma_addr0;
  3899. }
  3900. }
  3901. dsts.d32 =
  3902. DWC_READ_REG32(&GET_CORE_IF(pcd)->dev_if->
  3903. dev_global_regs->dsts);
  3904. dwc_ep->next_frame = dsts.b.soffn;
  3905. dwc_otg_iso_ep_start_frm_transfer(GET_CORE_IF
  3906. (pcd),
  3907. dwc_ep);
  3908. }
  3909. }
  3910. }
  3911. #else
  3912. depctl_data_t depctl = {.d32 = 0 };
  3913. dwc_ep_t *dwc_ep;
  3914. dwc_otg_dev_if_t *dev_if;
  3915. int i;
  3916. dev_if = GET_CORE_IF(pcd)->dev_if;
  3917. DWC_DEBUGPL(DBG_PCD,"Incomplete ISO IN \n");
  3918. for (i = 1; i <= dev_if->num_in_eps; ++i) {
  3919. dwc_ep = &pcd->in_ep[i-1].dwc_ep;
  3920. depctl.d32 =
  3921. DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  3922. if (depctl.b.epena && dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  3923. if (drop_transfer(dwc_ep->frame_num, GET_CORE_IF(pcd)->frame_num,
  3924. dwc_ep->frm_overrun))
  3925. {
  3926. depctl.d32 =
  3927. DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  3928. depctl.b.snak = 1;
  3929. depctl.b.epdis = 1;
  3930. DWC_MODIFY_REG32(&dev_if->in_ep_regs[i]->diepctl, depctl.d32, depctl.d32);
  3931. }
  3932. }
  3933. }
  3934. /*intr_mask.b.incomplisoin = 1;
  3935. DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  3936. intr_mask.d32, 0); */
  3937. #endif //DWC_EN_ISOC
  3938. /* Clear interrupt */
  3939. gintsts.d32 = 0;
  3940. gintsts.b.incomplisoin = 1;
  3941. DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  3942. gintsts.d32);
  3943. return 1;
  3944. }
  3945. /**
  3946. * Incomplete ISO OUT Transfer Interrupt.
  3947. *
  3948. * This interrupt indicates that the core has dropped an ISO OUT
  3949. * packet. The following conditions can be the cause:
  3950. * - FIFO Full, the entire packet would not fit in the FIFO.
  3951. * - CRC Error
  3952. * - Corrupted Token
  3953. * The follow actions will be taken:
  3954. * -# Determine the EP
  3955. * -# Set incomplete flag in dwc_ep structure
  3956. * -# Read any data from the FIFO
  3957. * -# Disable EP. When "Endpoint Disabled" interrupt is received
  3958. * re-enable EP.
  3959. */
  3960. int32_t dwc_otg_pcd_handle_incomplete_isoc_out_intr(dwc_otg_pcd_t * pcd)
  3961. {
  3962. gintsts_data_t gintsts;
  3963. #ifdef DWC_EN_ISOC
  3964. dwc_otg_dev_if_t *dev_if;
  3965. deptsiz_data_t deptsiz = {.d32 = 0 };
  3966. depctl_data_t depctl = {.d32 = 0 };
  3967. dsts_data_t dsts = {.d32 = 0 };
  3968. dwc_ep_t *dwc_ep;
  3969. int i;
  3970. dev_if = GET_CORE_IF(pcd)->dev_if;
  3971. for (i = 1; i <= dev_if->num_out_eps; ++i) {
  3972. dwc_ep = &pcd->in_ep[i].dwc_ep;
  3973. if (pcd->out_ep[i].dwc_ep.active &&
  3974. pcd->out_ep[i].dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  3975. deptsiz.d32 =
  3976. DWC_READ_REG32(&dev_if->out_ep_regs[i]->doeptsiz);
  3977. depctl.d32 =
  3978. DWC_READ_REG32(&dev_if->out_ep_regs[i]->doepctl);
  3979. if (depctl.b.epdis && deptsiz.d32) {
  3980. set_current_pkt_info(GET_CORE_IF(pcd),
  3981. &pcd->out_ep[i].dwc_ep);
  3982. if (dwc_ep->cur_pkt >= dwc_ep->pkt_cnt) {
  3983. dwc_ep->cur_pkt = 0;
  3984. dwc_ep->proc_buf_num =
  3985. (dwc_ep->proc_buf_num ^ 1) & 0x1;
  3986. if (dwc_ep->proc_buf_num) {
  3987. dwc_ep->cur_pkt_addr =
  3988. dwc_ep->xfer_buff1;
  3989. dwc_ep->cur_pkt_dma_addr =
  3990. dwc_ep->dma_addr1;
  3991. } else {
  3992. dwc_ep->cur_pkt_addr =
  3993. dwc_ep->xfer_buff0;
  3994. dwc_ep->cur_pkt_dma_addr =
  3995. dwc_ep->dma_addr0;
  3996. }
  3997. }
  3998. dsts.d32 =
  3999. DWC_READ_REG32(&GET_CORE_IF(pcd)->dev_if->
  4000. dev_global_regs->dsts);
  4001. dwc_ep->next_frame = dsts.b.soffn;
  4002. dwc_otg_iso_ep_start_frm_transfer(GET_CORE_IF
  4003. (pcd),
  4004. dwc_ep);
  4005. }
  4006. }
  4007. }
  4008. #else
  4009. /** @todo implement ISR */
  4010. gintmsk_data_t intr_mask = {.d32 = 0 };
  4011. dwc_otg_core_if_t *core_if;
  4012. deptsiz_data_t deptsiz = {.d32 = 0 };
  4013. depctl_data_t depctl = {.d32 = 0 };
  4014. dctl_data_t dctl = {.d32 = 0 };
  4015. dwc_ep_t *dwc_ep = NULL;
  4016. int i;
  4017. core_if = GET_CORE_IF(pcd);
  4018. for (i = 0; i < core_if->dev_if->num_out_eps; ++i) {
  4019. dwc_ep = &pcd->out_ep[i].dwc_ep;
  4020. depctl.d32 =
  4021. DWC_READ_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl);
  4022. if (depctl.b.epena && depctl.b.dpid == (core_if->frame_num & 0x1)) {
  4023. core_if->dev_if->isoc_ep = dwc_ep;
  4024. deptsiz.d32 =
  4025. DWC_READ_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doeptsiz);
  4026. break;
  4027. }
  4028. }
  4029. dctl.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
  4030. gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  4031. intr_mask.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
  4032. if (!intr_mask.b.goutnakeff) {
  4033. /* Unmask it */
  4034. intr_mask.b.goutnakeff = 1;
  4035. DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, intr_mask.d32);
  4036. }
  4037. if (!gintsts.b.goutnakeff) {
  4038. dctl.b.sgoutnak = 1;
  4039. }
  4040. DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32);
  4041. depctl.d32 = DWC_READ_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl);
  4042. if (depctl.b.epena) {
  4043. depctl.b.epdis = 1;
  4044. depctl.b.snak = 1;
  4045. }
  4046. DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl, depctl.d32);
  4047. intr_mask.d32 = 0;
  4048. intr_mask.b.incomplisoout = 1;
  4049. #endif /* DWC_EN_ISOC */
  4050. /* Clear interrupt */
  4051. gintsts.d32 = 0;
  4052. gintsts.b.incomplisoout = 1;
  4053. DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  4054. gintsts.d32);
  4055. return 1;
  4056. }
  4057. /**
  4058. * This function handles the Global IN NAK Effective interrupt.
  4059. *
  4060. */
  4061. int32_t dwc_otg_pcd_handle_in_nak_effective(dwc_otg_pcd_t * pcd)
  4062. {
  4063. dwc_otg_dev_if_t *dev_if = GET_CORE_IF(pcd)->dev_if;
  4064. depctl_data_t diepctl = {.d32 = 0 };
  4065. gintmsk_data_t intr_mask = {.d32 = 0 };
  4066. gintsts_data_t gintsts;
  4067. dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  4068. int i;
  4069. DWC_DEBUGPL(DBG_PCD, "Global IN NAK Effective\n");
  4070. /* Disable all active IN EPs */
  4071. for (i = 0; i <= dev_if->num_in_eps; i++) {
  4072. diepctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  4073. if (!(diepctl.b.eptype & 1) && diepctl.b.epena) {
  4074. if (core_if->start_predict > 0)
  4075. core_if->start_predict++;
  4076. diepctl.b.epdis = 1;
  4077. diepctl.b.snak = 1;
  4078. DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepctl, diepctl.d32);
  4079. }
  4080. }
  4081. /* Disable the Global IN NAK Effective Interrupt */
  4082. intr_mask.b.ginnakeff = 1;
  4083. DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  4084. intr_mask.d32, 0);
  4085. /* Clear interrupt */
  4086. gintsts.d32 = 0;
  4087. gintsts.b.ginnakeff = 1;
  4088. DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  4089. gintsts.d32);
  4090. return 1;
  4091. }
  4092. /**
  4093. * OUT NAK Effective.
  4094. *
  4095. */
  4096. int32_t dwc_otg_pcd_handle_out_nak_effective(dwc_otg_pcd_t * pcd)
  4097. {
  4098. dwc_otg_dev_if_t *dev_if = GET_CORE_IF(pcd)->dev_if;
  4099. gintmsk_data_t intr_mask = {.d32 = 0 };
  4100. gintsts_data_t gintsts;
  4101. depctl_data_t doepctl;
  4102. int i;
  4103. /* Disable the Global OUT NAK Effective Interrupt */
  4104. intr_mask.b.goutnakeff = 1;
  4105. DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  4106. intr_mask.d32, 0);
  4107. /* If DEV OUT NAK enabled*/
  4108. if (pcd->core_if->core_params->dev_out_nak) {
  4109. /* Run over all out endpoints to determine the ep number on
  4110. * which the timeout has happened
  4111. */
  4112. for (i = 0; i <= dev_if->num_out_eps; i++) {
  4113. if ( pcd->core_if->ep_xfer_info[i].state == 2 )
  4114. break;
  4115. }
  4116. if (i > dev_if->num_out_eps) {
  4117. dctl_data_t dctl;
  4118. dctl.d32 = DWC_READ_REG32(&dev_if->
  4119. dev_global_regs->dctl);
  4120. dctl.b.cgoutnak = 1;
  4121. DWC_WRITE_REG32(&dev_if->dev_global_regs->dctl,
  4122. dctl.d32);
  4123. goto out;
  4124. }
  4125. /* Disable the endpoint */
  4126. doepctl.d32 = DWC_READ_REG32(&dev_if->
  4127. out_ep_regs[i]->doepctl);
  4128. if (doepctl.b.epena) {
  4129. doepctl.b.epdis = 1;
  4130. doepctl.b.snak = 1;
  4131. }
  4132. DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepctl, doepctl.d32);
  4133. return 1;
  4134. }
  4135. /* We come here from Incomplete ISO OUT handler */
  4136. if(dev_if->isoc_ep)
  4137. {
  4138. dwc_ep_t *dwc_ep = (dwc_ep_t *)dev_if->isoc_ep;
  4139. uint32_t epnum = dwc_ep->num;
  4140. doepint_data_t doepint;
  4141. doepint.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[dwc_ep->num]->doepint);
  4142. dev_if->isoc_ep = NULL;
  4143. doepctl.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[epnum]->doepctl);
  4144. DWC_PRINTF("Before disable DOEPCTL = %08x\n", doepctl.d32);
  4145. if (doepctl.b.epena) {
  4146. doepctl.b.epdis = 1;
  4147. doepctl.b.snak = 1;
  4148. }
  4149. DWC_WRITE_REG32(&dev_if->out_ep_regs[epnum]->doepctl, doepctl.d32);
  4150. return 1;
  4151. } else
  4152. DWC_PRINTF("INTERRUPT Handler not implemented for %s\n",
  4153. "Global OUT NAK Effective\n");
  4154. out:
  4155. /* Clear interrupt */
  4156. gintsts.d32 = 0;
  4157. gintsts.b.goutnakeff = 1;
  4158. DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  4159. gintsts.d32);
  4160. return 1;
  4161. }
  4162. /**
  4163. * PCD interrupt handler.
  4164. *
  4165. * The PCD handles the device interrupts. Many conditions can cause a
  4166. * device interrupt. When an interrupt occurs, the device interrupt
  4167. * service routine determines the cause of the interrupt and
  4168. * dispatches handling to the appropriate function. These interrupt
  4169. * handling functions are described below.
  4170. *
  4171. * All interrupt registers are processed from LSB to MSB.
  4172. *
  4173. */
  4174. int32_t dwc_otg_pcd_handle_intr(dwc_otg_pcd_t * pcd)
  4175. {
  4176. dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  4177. #ifdef VERBOSE
  4178. dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  4179. #endif
  4180. gintsts_data_t gintr_status;
  4181. int32_t retval = 0;
  4182. /* Exit from ISR if core is hibernated */
  4183. if (core_if->hibernation_suspend == 1) {
  4184. return retval;
  4185. }
  4186. #ifdef VERBOSE
  4187. DWC_DEBUGPL(DBG_ANY, "%s() gintsts=%08x gintmsk=%08x\n",
  4188. __func__,
  4189. DWC_READ_REG32(&global_regs->gintsts),
  4190. DWC_READ_REG32(&global_regs->gintmsk));
  4191. #endif
  4192. if (dwc_otg_is_device_mode(core_if)) {
  4193. DWC_SPINLOCK(pcd->lock);
  4194. #ifdef VERBOSE
  4195. DWC_DEBUGPL(DBG_PCDV, "%s() gintsts=%08x gintmsk=%08x\n",
  4196. __func__,
  4197. DWC_READ_REG32(&global_regs->gintsts),
  4198. DWC_READ_REG32(&global_regs->gintmsk));
  4199. #endif
  4200. gintr_status.d32 = dwc_otg_read_core_intr(core_if);
  4201. DWC_DEBUGPL(DBG_PCDV, "%s: gintsts&gintmsk=%08x\n",
  4202. __func__, gintr_status.d32);
  4203. if (gintr_status.b.sofintr) {
  4204. retval |= dwc_otg_pcd_handle_sof_intr(pcd);
  4205. }
  4206. if (gintr_status.b.rxstsqlvl) {
  4207. retval |=
  4208. dwc_otg_pcd_handle_rx_status_q_level_intr(pcd);
  4209. }
  4210. if (gintr_status.b.nptxfempty) {
  4211. retval |= dwc_otg_pcd_handle_np_tx_fifo_empty_intr(pcd);
  4212. }
  4213. if (gintr_status.b.goutnakeff) {
  4214. retval |= dwc_otg_pcd_handle_out_nak_effective(pcd);
  4215. }
  4216. if (gintr_status.b.i2cintr) {
  4217. retval |= dwc_otg_pcd_handle_i2c_intr(pcd);
  4218. }
  4219. if (gintr_status.b.erlysuspend) {
  4220. retval |= dwc_otg_pcd_handle_early_suspend_intr(pcd);
  4221. }
  4222. if (gintr_status.b.usbreset) {
  4223. retval |= dwc_otg_pcd_handle_usb_reset_intr(pcd);
  4224. }
  4225. if (gintr_status.b.enumdone) {
  4226. retval |= dwc_otg_pcd_handle_enum_done_intr(pcd);
  4227. }
  4228. if (gintr_status.b.isooutdrop) {
  4229. retval |=
  4230. dwc_otg_pcd_handle_isoc_out_packet_dropped_intr
  4231. (pcd);
  4232. }
  4233. if (gintr_status.b.eopframe) {
  4234. retval |=
  4235. dwc_otg_pcd_handle_end_periodic_frame_intr(pcd);
  4236. }
  4237. if (gintr_status.b.inepint) {
  4238. if (!core_if->multiproc_int_enable) {
  4239. retval |= dwc_otg_pcd_handle_in_ep_intr(pcd);
  4240. }
  4241. }
  4242. if (gintr_status.b.outepintr) {
  4243. if (!core_if->multiproc_int_enable) {
  4244. retval |= dwc_otg_pcd_handle_out_ep_intr(pcd);
  4245. }
  4246. }
  4247. if (gintr_status.b.epmismatch) {
  4248. retval |= dwc_otg_pcd_handle_ep_mismatch_intr(pcd);
  4249. }
  4250. if (gintr_status.b.fetsusp) {
  4251. retval |= dwc_otg_pcd_handle_ep_fetsusp_intr(pcd);
  4252. }
  4253. if (gintr_status.b.ginnakeff) {
  4254. retval |= dwc_otg_pcd_handle_in_nak_effective(pcd);
  4255. }
  4256. if (gintr_status.b.incomplisoin) {
  4257. retval |=
  4258. dwc_otg_pcd_handle_incomplete_isoc_in_intr(pcd);
  4259. }
  4260. if (gintr_status.b.incomplisoout) {
  4261. retval |=
  4262. dwc_otg_pcd_handle_incomplete_isoc_out_intr(pcd);
  4263. }
  4264. /* In MPI mode Device Endpoints interrupts are asserted
  4265. * without setting outepintr and inepint bits set, so these
  4266. * Interrupt handlers are called without checking these bit-fields
  4267. */
  4268. if (core_if->multiproc_int_enable) {
  4269. retval |= dwc_otg_pcd_handle_in_ep_intr(pcd);
  4270. retval |= dwc_otg_pcd_handle_out_ep_intr(pcd);
  4271. }
  4272. #ifdef VERBOSE
  4273. DWC_DEBUGPL(DBG_PCDV, "%s() gintsts=%0x\n", __func__,
  4274. DWC_READ_REG32(&global_regs->gintsts));
  4275. #endif
  4276. DWC_SPINUNLOCK(pcd->lock);
  4277. }
  4278. return retval;
  4279. }
  4280. #endif /* DWC_HOST_ONLY */