gadget.c 134 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085
  1. #include "usb_os_adapter.h"
  2. #include "trace.h"
  3. #include <asm/dma-mapping.h>
  4. #include <linux/usb/ch9.h>
  5. #include <linux/usb/gadget.h>
  6. #include "usb-compat.h"
  7. #include "cp15/cp15.h"
  8. #include "core.h"
  9. #include "hw.h"
  10. #include "semphr.h"
  11. #include "list.h"
  12. static List_t free_request_list;
  13. static SemaphoreHandle_t free_list_lock;
  14. static struct dwc2_hsotg_req *free_req;
  15. //static void dwc2_hsotg_dump1(struct dwc2_hsotg *hsotg);
  16. void dwc2_dump_global_registers1(struct dwc2_hsotg *hsotg);
  17. static struct dwc2_hsotg_req* get_free_req()
  18. {
  19. struct dwc2_hsotg_req* req = NULL;
  20. if (!xPortIsInInterrupt())
  21. xSemaphoreTake(free_list_lock, portMAX_DELAY);
  22. if (listLIST_IS_EMPTY(&free_request_list)) {
  23. printf("free list req is empty\r\n");
  24. goto exit;
  25. }
  26. req = listGET_OWNER_OF_HEAD_ENTRY(&free_request_list);
  27. uxListRemove(&req->free_list_entry);
  28. exit:
  29. if (!xPortIsInInterrupt())
  30. xSemaphoreGive(free_list_lock);
  31. return req;
  32. }
  33. static void free_used_req(struct dwc2_hsotg_req* req)
  34. {
  35. if (NULL == req)
  36. return;
  37. if (!xPortIsInInterrupt())
  38. xSemaphoreTake(free_list_lock, portMAX_DELAY);
  39. memset(&req->req, 0, sizeof(req->req));
  40. vListInsertEnd(&free_request_list, &req->free_list_entry);
  41. if (!xPortIsInInterrupt())
  42. xSemaphoreGive(free_list_lock);
  43. }
  44. /* conversion functions */
  45. #ifndef NO_GNU
  46. static inline struct dwc2_hsotg_req *our_req(struct usb_request *req)
  47. {
  48. return container_of(req, struct dwc2_hsotg_req, req);
  49. }
  50. static inline struct dwc2_hsotg_ep *our_ep(struct usb_ep *ep)
  51. {
  52. return container_of(ep, struct dwc2_hsotg_ep, ep);
  53. }
  54. static inline struct dwc2_hsotg *to_hsotg(struct usb_gadget *gadget)
  55. {
  56. return container_of(gadget, struct dwc2_hsotg, gadget);
  57. }
  58. static inline void __orr32(void __iomem *ptr, u32 val)
  59. {
  60. dwc2_writel(dwc2_readl(ptr) | val, ptr);
  61. }
  62. static inline void __bic32(void __iomem *ptr, u32 val)
  63. {
  64. dwc2_writel(dwc2_readl(ptr) & ~val, ptr);
  65. }
  66. #else
  67. static inline struct dwc2_hsotg_req *our_req(struct usb_request *req)
  68. {
  69. return (struct dwc2_hsotg_req *)req->powner;
  70. }
  71. static inline struct dwc2_hsotg_ep *our_ep(struct usb_ep *ep)
  72. {
  73. return (struct dwc2_hsotg_ep *)ep->powner;
  74. }
  75. static inline struct dwc2_hsotg *to_hsotg(struct usb_gadget *gadget)
  76. {
  77. return (struct dwc2_hsotg *)gadget->powner;
  78. }
  79. static inline void __orr32(u32 ptr, u32 val)
  80. {
  81. dwc2_writel(dwc2_readl(ptr) | val, ptr);
  82. }
  83. static inline void __bic32(u32 ptr, u32 val)
  84. {
  85. dwc2_writel(dwc2_readl(ptr) & ~val, ptr);
  86. }
  87. #endif
  88. static inline struct dwc2_hsotg_ep *index_to_ep(struct dwc2_hsotg *hsotg,
  89. u32 ep_index, u32 dir_in)
  90. {
  91. if (dir_in)
  92. return hsotg->eps_in[ep_index];
  93. else
  94. return hsotg->eps_out[ep_index];
  95. }
  96. /* forward declaration of functions */
  97. static void dwc2_hsotg_dump(struct dwc2_hsotg *hsotg);
  98. /**
  99. * using_dma - return the DMA status of the driver.
  100. * @hsotg: The driver state.
  101. *
  102. * Return true if we're using DMA.
  103. *
  104. * Currently, we have the DMA support code worked into everywhere
  105. * that needs it, but the AMBA DMA implementation in the hardware can
  106. * only DMA from 32bit aligned addresses. This means that gadgets such
  107. * as the CDC Ethernet cannot work as they often pass packets which are
  108. * not 32bit aligned.
  109. *
  110. * Unfortunately the choice to use DMA or not is global to the controller
  111. * and seems to be only settable when the controller is being put through
  112. * a core reset. This means we either need to fix the gadgets to take
  113. * account of DMA alignment, or add bounce buffers (yuerk).
  114. *
  115. * g_using_dma is set depending on dts flag.
  116. */
  117. static inline bool using_dma(struct dwc2_hsotg *hsotg)
  118. {
  119. return hsotg->params.g_dma;
  120. }
  121. /*
  122. * using_desc_dma - return the descriptor DMA status of the driver.
  123. * @hsotg: The driver state.
  124. *
  125. * Return true if we're using descriptor DMA.
  126. */
  127. static inline bool using_desc_dma(struct dwc2_hsotg *hsotg)
  128. {
  129. return hsotg->params.g_dma_desc;
  130. }
  131. /**
  132. * dwc2_gadget_incr_frame_num - Increments the targeted frame number.
  133. * @hs_ep: The endpoint
  134. * @increment: The value to increment by
  135. *
  136. * This function will also check if the frame number overruns DSTS_SOFFN_LIMIT.
  137. * If an overrun occurs it will wrap the value and set the frame_overrun flag.
  138. */
  139. static inline void dwc2_gadget_incr_frame_num(struct dwc2_hsotg_ep *hs_ep)
  140. {
  141. hs_ep->target_frame += hs_ep->interval;
  142. if (hs_ep->target_frame > DSTS_SOFFN_LIMIT) {
  143. hs_ep->frame_overrun = 1;
  144. hs_ep->target_frame &= DSTS_SOFFN_LIMIT;
  145. } else {
  146. hs_ep->frame_overrun = 0;
  147. }
  148. }
  149. /**
  150. * dwc2_hsotg_en_gsint - enable one or more of the general interrupt
  151. * @hsotg: The device state
  152. * @ints: A bitmask of the interrupts to enable
  153. */
  154. static void dwc2_hsotg_en_gsint(struct dwc2_hsotg *hsotg, u32 ints)
  155. {
  156. u32 gsintmsk = dwc2_readl(hsotg->regs + GINTMSK);
  157. u32 new_gsintmsk;
  158. new_gsintmsk = gsintmsk | ints;
  159. if (new_gsintmsk != gsintmsk) {
  160. dev_dbg(hsotg->dev, "gsintmsk now 0x%08x\n", new_gsintmsk);
  161. dwc2_writel(new_gsintmsk, hsotg->regs + GINTMSK);
  162. }
  163. }
  164. /**
  165. * dwc2_hsotg_disable_gsint - disable one or more of the general interrupt
  166. * @hsotg: The device state
  167. * @ints: A bitmask of the interrupts to enable
  168. */
  169. static void dwc2_hsotg_disable_gsint(struct dwc2_hsotg *hsotg, u32 ints)
  170. {
  171. u32 gsintmsk = dwc2_readl(hsotg->regs + GINTMSK);
  172. u32 new_gsintmsk;
  173. new_gsintmsk = gsintmsk & ~ints;
  174. if (new_gsintmsk != gsintmsk)
  175. dwc2_writel(new_gsintmsk, hsotg->regs + GINTMSK);
  176. }
  177. /**
  178. * dwc2_hsotg_ctrl_epint - enable/disable an endpoint irq
  179. * @hsotg: The device state
  180. * @ep: The endpoint index
  181. * @dir_in: True if direction is in.
  182. * @en: The enable value, true to enable
  183. *
  184. * Set or clear the mask for an individual endpoint's interrupt
  185. * request.
  186. */
  187. static void dwc2_hsotg_ctrl_epint(struct dwc2_hsotg *hsotg,
  188. unsigned int ep, unsigned int dir_in,
  189. unsigned int en)
  190. {
  191. //unsigned long flags;
  192. u32 bit = 1 << ep;
  193. u32 daint;
  194. if (!dir_in)
  195. bit <<= 16;
  196. /*if (!xPortIsInInterrupt())
  197. local_irq_save(flags);
  198. else
  199. spin_lock(&hsotg->lock);*/
  200. daint = dwc2_readl(hsotg->regs + DAINTMSK);
  201. if (en)
  202. daint |= bit;
  203. else
  204. daint &= ~bit;
  205. dwc2_writel(daint, hsotg->regs + DAINTMSK);
  206. /*if (!xPortIsInInterrupt())
  207. local_irq_restore(flags);
  208. else
  209. spin_unlock(&hsotg->lock);*/
  210. }
  211. /**
  212. * dwc2_hsotg_tx_fifo_count - return count of TX FIFOs in device mode
  213. */
  214. int dwc2_hsotg_tx_fifo_count(struct dwc2_hsotg *hsotg)
  215. {
  216. if (hsotg->hw_params.en_multiple_tx_fifo)
  217. /* In dedicated FIFO mode we need count of IN EPs */
  218. return (dwc2_readl(hsotg->regs + GHWCFG4) &
  219. GHWCFG4_NUM_IN_EPS_MASK) >> GHWCFG4_NUM_IN_EPS_SHIFT;
  220. else
  221. /* In shared FIFO mode we need count of Periodic IN EPs */
  222. return hsotg->hw_params.num_dev_perio_in_ep;
  223. }
  224. /**
  225. * dwc2_hsotg_ep_info_size - return Endpoint Info Control block size in DWORDs
  226. */
  227. static int dwc2_hsotg_ep_info_size(struct dwc2_hsotg *hsotg)
  228. {
  229. int val = 0;
  230. int i;
  231. u32 ep_dirs;
  232. /*
  233. * Don't need additional space for ep info control registers in
  234. * slave mode.
  235. */
  236. if (!using_dma(hsotg)) {
  237. dev_dbg(hsotg->dev, "Buffer DMA ep info size 0\n");
  238. return 0;
  239. }
  240. /*
  241. * Buffer DMA mode - 1 location per endpoit
  242. * Descriptor DMA mode - 4 locations per endpoint
  243. */
  244. ep_dirs = hsotg->hw_params.dev_ep_dirs;
  245. for (i = 0; i <= hsotg->hw_params.num_dev_ep; i++) {
  246. val += ep_dirs & 3 ? 1 : 2;
  247. ep_dirs >>= 2;
  248. }
  249. if (using_desc_dma(hsotg))
  250. val = val * 4;
  251. return val;
  252. }
  253. /**
  254. * dwc2_hsotg_tx_fifo_total_depth - return total FIFO depth available for
  255. * device mode TX FIFOs
  256. */
  257. int dwc2_hsotg_tx_fifo_total_depth(struct dwc2_hsotg *hsotg)
  258. {
  259. int ep_info_size;
  260. int addr;
  261. int tx_addr_max;
  262. u32 np_tx_fifo_size;
  263. np_tx_fifo_size = min_t(u32, hsotg->hw_params.dev_nperio_tx_fifo_size,
  264. hsotg->params.g_np_tx_fifo_size);
  265. /* Get Endpoint Info Control block size in DWORDs. */
  266. ep_info_size = dwc2_hsotg_ep_info_size(hsotg);
  267. tx_addr_max = hsotg->hw_params.total_fifo_size - ep_info_size;
  268. addr = hsotg->params.g_rx_fifo_size + np_tx_fifo_size;
  269. if (tx_addr_max <= addr)
  270. return 0;
  271. return tx_addr_max - addr;
  272. }
  273. /**
  274. * dwc2_hsotg_tx_fifo_average_depth - returns average depth of device mode
  275. * TX FIFOs
  276. */
  277. int dwc2_hsotg_tx_fifo_average_depth(struct dwc2_hsotg *hsotg)
  278. {
  279. int tx_fifo_count;
  280. int tx_fifo_depth;
  281. tx_fifo_depth = dwc2_hsotg_tx_fifo_total_depth(hsotg);
  282. tx_fifo_count = dwc2_hsotg_tx_fifo_count(hsotg);
  283. if (!tx_fifo_count)
  284. return tx_fifo_depth;
  285. else
  286. return tx_fifo_depth / tx_fifo_count;
  287. }
  288. /**
  289. * dwc2_hsotg_init_fifo - initialise non-periodic FIFOs
  290. * @hsotg: The device instance.
  291. */
  292. #ifndef WARN_ONCE
  293. #define WARN_ONCE(...)
  294. #endif
  295. static void dwc2_hsotg_init_fifo(struct dwc2_hsotg *hsotg)
  296. {
  297. unsigned int ep;
  298. unsigned int addr;
  299. int timeout;
  300. u32 val;
  301. u32 *txfsz = hsotg->params.g_tx_fifo_size;
  302. /* Reset fifo map if not correctly cleared during previous session */
  303. WARN_ON(hsotg->fifo_map);
  304. hsotg->fifo_map = 0;
  305. /* set RX/NPTX FIFO sizes */
  306. dwc2_writel(hsotg->params.g_rx_fifo_size, hsotg->regs + GRXFSIZ);
  307. dwc2_writel((hsotg->params.g_rx_fifo_size << FIFOSIZE_STARTADDR_SHIFT) |
  308. (hsotg->params.g_np_tx_fifo_size << FIFOSIZE_DEPTH_SHIFT),
  309. hsotg->regs + GNPTXFSIZ);
  310. /*
  311. * arange all the rest of the TX FIFOs, as some versions of this
  312. * block have overlapping default addresses. This also ensures
  313. * that if the settings have been changed, then they are set to
  314. * known values.
  315. */
  316. /* start at the end of the GNPTXFSIZ, rounded up */
  317. addr = hsotg->params.g_rx_fifo_size + hsotg->params.g_np_tx_fifo_size;
  318. /*
  319. * Configure fifos sizes from provided configuration and assign
  320. * them to endpoints dynamically according to maxpacket size value of
  321. * given endpoint.
  322. */
  323. for (ep = 1; ep < MAX_EPS_CHANNELS; ep++) {
  324. if (!txfsz[ep])
  325. continue;
  326. val = addr;
  327. val |= txfsz[ep] << FIFOSIZE_DEPTH_SHIFT;
  328. WARN_ONCE(addr + txfsz[ep] > hsotg->fifo_mem,
  329. "insufficient fifo memory");
  330. addr += txfsz[ep];
  331. dwc2_writel(val, hsotg->regs + DPTXFSIZN(ep));
  332. val = dwc2_readl(hsotg->regs + DPTXFSIZN(ep));
  333. }
  334. dwc2_writel(hsotg->hw_params.total_fifo_size |
  335. addr << GDFIFOCFG_EPINFOBASE_SHIFT,
  336. hsotg->regs + GDFIFOCFG);
  337. /*
  338. * according to p428 of the design guide, we need to ensure that
  339. * all fifos are flushed before continuing
  340. */
  341. dwc2_writel(GRSTCTL_TXFNUM(0x10) | GRSTCTL_TXFFLSH |
  342. GRSTCTL_RXFFLSH, hsotg->regs + GRSTCTL);
  343. /* wait until the fifos are both flushed */
  344. timeout = 100;
  345. while (1) {
  346. val = dwc2_readl(hsotg->regs + GRSTCTL);
  347. if ((val & (GRSTCTL_TXFFLSH | GRSTCTL_RXFFLSH)) == 0)
  348. break;
  349. if (--timeout == 0) {
  350. dev_err(hsotg->dev,
  351. "%s: timeout flushing fifos (GRSTCTL=%08x)\n",
  352. __func__, val);
  353. break;
  354. }
  355. udelay(1);
  356. }
  357. dev_dbg(hsotg->dev, "FIFOs reset, timeout at %d\n", timeout);
  358. }
  359. /**
  360. * @ep: USB endpoint to allocate request for.
  361. * @flags: Allocation flags
  362. *
  363. * Allocate a new USB request structure appropriate for the specified endpoint
  364. */
  365. static struct usb_request *dwc2_hsotg_ep_alloc_request(struct usb_ep *ep,
  366. gfp_t flags)
  367. {
  368. struct dwc2_hsotg_req *req;
  369. req = get_free_req();//kzalloc(sizeof(*req), flags);
  370. if (!req)
  371. return NULL;
  372. #ifndef NO_GNU
  373. INIT_LIST_HEAD(&req->queue);
  374. #else
  375. INIT_LIST_ITEM(&req->queue);
  376. listSET_LIST_ITEM_OWNER(&req->queue, req);
  377. req->req.powner = req;
  378. #endif
  379. return &req->req;
  380. }
  381. /**
  382. * is_ep_periodic - return true if the endpoint is in periodic mode.
  383. * @hs_ep: The endpoint to query.
  384. *
  385. * Returns true if the endpoint is in periodic mode, meaning it is being
  386. * used for an Interrupt or ISO transfer.
  387. */
  388. static inline int is_ep_periodic(struct dwc2_hsotg_ep *hs_ep)
  389. {
  390. return hs_ep->periodic;
  391. }
  392. /**
  393. * dwc2_hsotg_unmap_dma - unmap the DMA memory being used for the request
  394. * @hsotg: The device state.
  395. * @hs_ep: The endpoint for the request
  396. * @hs_req: The request being processed.
  397. *
  398. * This is the reverse of dwc2_hsotg_map_dma(), called for the completion
  399. * of a request to ensure the buffer is ready for access by the caller.
  400. */
  401. static void dwc2_hsotg_unmap_dma(struct dwc2_hsotg *hsotg,
  402. struct dwc2_hsotg_ep *hs_ep,
  403. struct dwc2_hsotg_req *hs_req)
  404. {
  405. struct usb_request *req = &hs_req->req;
  406. usb_gadget_unmap_request(&hsotg->gadget, req, hs_ep->dir_in);
  407. }
  408. /*
  409. * dwc2_gadget_alloc_ctrl_desc_chains - allocate DMA descriptor chains
  410. * for Control endpoint
  411. * @hsotg: The device state.
  412. *
  413. * This function will allocate 4 descriptor chains for EP 0: 2 for
  414. * Setup stage, per one for IN and OUT data/status transactions.
  415. */
  416. static int dwc2_gadget_alloc_ctrl_desc_chains(struct dwc2_hsotg *hsotg)
  417. {
  418. hsotg->setup_desc[0] =
  419. dmam_alloc_coherent(hsotg->dev,
  420. sizeof(struct dwc2_dma_desc),
  421. &hsotg->setup_desc_dma[0],
  422. GFP_KERNEL);
  423. if (!hsotg->setup_desc[0])
  424. goto fail;
  425. hsotg->setup_desc[1] =
  426. dmam_alloc_coherent(hsotg->dev,
  427. sizeof(struct dwc2_dma_desc),
  428. &hsotg->setup_desc_dma[1],
  429. GFP_KERNEL);
  430. if (!hsotg->setup_desc[1])
  431. goto fail;
  432. hsotg->ctrl_in_desc =
  433. dmam_alloc_coherent(hsotg->dev,
  434. sizeof(struct dwc2_dma_desc),
  435. &hsotg->ctrl_in_desc_dma,
  436. GFP_KERNEL);
  437. if (!hsotg->ctrl_in_desc)
  438. goto fail;
  439. hsotg->ctrl_out_desc =
  440. dmam_alloc_coherent(hsotg->dev,
  441. sizeof(struct dwc2_dma_desc),
  442. &hsotg->ctrl_out_desc_dma,
  443. GFP_KERNEL);
  444. if (!hsotg->ctrl_out_desc)
  445. goto fail;
  446. #ifdef NO_GNU
  447. if ((u32)hsotg->setup_desc[0] & (ARCH_DMA_MINALIGN - 1)) {
  448. u32 addr = (u32)(((u32)hsotg->setup_desc[0] + ARCH_DMA_MINALIGN) & (~(ARCH_DMA_MINALIGN - 1)));
  449. hsotg->setup_desc_dma_offset[0] = addr - (u32)hsotg->setup_desc[0];
  450. hsotg->setup_desc[0] = (struct dwc2_dma_desc *)addr;
  451. hsotg->setup_desc_dma[0] += hsotg->setup_desc_dma_offset[0];
  452. } else {
  453. hsotg->setup_desc_dma_offset[0] = 0;
  454. }
  455. if ((u32)hsotg->setup_desc[1] & (ARCH_DMA_MINALIGN - 1)) {
  456. u32 addr = (u32)(((u32)hsotg->setup_desc[1] + ARCH_DMA_MINALIGN) & (~(ARCH_DMA_MINALIGN - 1)));
  457. hsotg->setup_desc_dma_offset[1] = addr - (u32)hsotg->setup_desc[0];
  458. hsotg->setup_desc[1] = (struct dwc2_dma_desc *)addr;
  459. hsotg->setup_desc_dma[1] += hsotg->setup_desc_dma_offset[1];
  460. } else {
  461. hsotg->setup_desc_dma_offset[1] = 0;
  462. }
  463. if ((u32)hsotg->ctrl_in_desc & (ARCH_DMA_MINALIGN - 1)) {
  464. u32 addr = (u32)(((u32)hsotg->ctrl_in_desc + ARCH_DMA_MINALIGN) & (~(ARCH_DMA_MINALIGN - 1)));
  465. hsotg->ctrl_in_desc_offset = addr - (u32)hsotg->ctrl_in_desc;
  466. hsotg->ctrl_in_desc = (struct dwc2_dma_desc *)addr;
  467. hsotg->ctrl_in_desc_dma += hsotg->ctrl_in_desc_offset;
  468. } else {
  469. hsotg->ctrl_in_desc_offset = 0;
  470. }
  471. if ((u32)hsotg->ctrl_out_desc & (ARCH_DMA_MINALIGN - 1)) {
  472. u32 addr = (u32)(((u32)hsotg->ctrl_in_desc + ARCH_DMA_MINALIGN) & (~(ARCH_DMA_MINALIGN - 1)));
  473. hsotg->ctrl_out_desc_offset = addr - (u32)hsotg->ctrl_out_desc;
  474. hsotg->ctrl_out_desc = (struct dwc2_dma_desc *)addr;
  475. hsotg->ctrl_out_desc_dma += hsotg->ctrl_out_desc_offset;
  476. } else {
  477. hsotg->ctrl_out_desc_offset = 0;
  478. }
  479. #endif
  480. return 0;
  481. fail:
  482. return -ENOMEM;
  483. }
  484. /**
  485. * dwc2_hsotg_write_fifo - write packet Data to the TxFIFO
  486. * @hsotg: The controller state.
  487. * @hs_ep: The endpoint we're going to write for.
  488. * @hs_req: The request to write data for.
  489. *
  490. * This is called when the TxFIFO has some space in it to hold a new
  491. * transmission and we have something to give it. The actual setup of
  492. * the data size is done elsewhere, so all we have to do is to actually
  493. * write the data.
  494. *
  495. * The return value is zero if there is more space (or nothing was done)
  496. * otherwise -ENOSPC is returned if the FIFO space was used up.
  497. *
  498. * This routine is only needed for PIO
  499. */
  500. static int dwc2_hsotg_write_fifo(struct dwc2_hsotg *hsotg,
  501. struct dwc2_hsotg_ep *hs_ep,
  502. struct dwc2_hsotg_req *hs_req)
  503. {
  504. bool periodic = is_ep_periodic(hs_ep);
  505. u32 gnptxsts = dwc2_readl(hsotg->regs + GNPTXSTS);
  506. int buf_pos = hs_req->req.actual;
  507. int to_write = hs_ep->size_loaded;
  508. #ifndef NO_GNU
  509. void *data;
  510. #else
  511. u32 data;
  512. #endif
  513. int can_write;
  514. int pkt_round;
  515. int max_transfer;
  516. to_write -= (buf_pos - hs_ep->last_load);
  517. /* if there's nothing to write, get out early */
  518. if (to_write == 0)
  519. return 0;
  520. if (periodic && !hsotg->dedicated_fifos) {
  521. u32 epsize = dwc2_readl(hsotg->regs + DIEPTSIZ(hs_ep->index));
  522. int size_left;
  523. int size_done;
  524. /*
  525. * work out how much data was loaded so we can calculate
  526. * how much data is left in the fifo.
  527. */
  528. size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
  529. /*
  530. * if shared fifo, we cannot write anything until the
  531. * previous data has been completely sent.
  532. */
  533. if (hs_ep->fifo_load != 0) {
  534. dwc2_hsotg_en_gsint(hsotg, GINTSTS_PTXFEMP);
  535. return -ENOSPC;
  536. }
  537. dev_dbg(hsotg->dev, "%s: left=%d, load=%d, fifo=%d, size %d\n",
  538. __func__, size_left,
  539. hs_ep->size_loaded, hs_ep->fifo_load, hs_ep->fifo_size);
  540. /* how much of the data has moved */
  541. size_done = hs_ep->size_loaded - size_left;
  542. /* how much data is left in the fifo */
  543. can_write = hs_ep->fifo_load - size_done;
  544. dev_dbg(hsotg->dev, "%s: => can_write1=%d\n",
  545. __func__, can_write);
  546. can_write = hs_ep->fifo_size - can_write;
  547. dev_dbg(hsotg->dev, "%s: => can_write2=%d\n",
  548. __func__, can_write);
  549. if (can_write <= 0) {
  550. dwc2_hsotg_en_gsint(hsotg, GINTSTS_PTXFEMP);
  551. return -ENOSPC;
  552. }
  553. } else if (hsotg->dedicated_fifos && hs_ep->index != 0) {
  554. can_write = dwc2_readl(hsotg->regs +
  555. DTXFSTS(hs_ep->fifo_index));
  556. can_write &= 0xffff;
  557. can_write *= 4;
  558. } else {
  559. if (GNPTXSTS_NP_TXQ_SPC_AVAIL_GET(gnptxsts) == 0) {
  560. dev_dbg(hsotg->dev,
  561. "%s: no queue slots available (0x%08x)\n",
  562. __func__, gnptxsts);
  563. dwc2_hsotg_en_gsint(hsotg, GINTSTS_NPTXFEMP);
  564. return -ENOSPC;
  565. }
  566. can_write = GNPTXSTS_NP_TXF_SPC_AVAIL_GET(gnptxsts);
  567. can_write *= 4; /* fifo size is in 32bit quantities. */
  568. }
  569. max_transfer = hs_ep->ep.maxpacket * hs_ep->mc;
  570. dev_dbg(hsotg->dev, "%s: GNPTXSTS=%08x, can=%d, to=%d, max_transfer %d\n",
  571. __func__, gnptxsts, can_write, to_write, max_transfer);
  572. /*
  573. * limit to 512 bytes of data, it seems at least on the non-periodic
  574. * FIFO, requests of >512 cause the endpoint to get stuck with a
  575. * fragment of the end of the transfer in it.
  576. */
  577. if (can_write > 512 && !periodic)
  578. can_write = 512;
  579. /*
  580. * limit the write to one max-packet size worth of data, but allow
  581. * the transfer to return that it did not run out of fifo space
  582. * doing it.
  583. */
  584. if (to_write > max_transfer) {
  585. to_write = max_transfer;
  586. /* it's needed only when we do not use dedicated fifos */
  587. if (!hsotg->dedicated_fifos)
  588. dwc2_hsotg_en_gsint(hsotg,
  589. periodic ? GINTSTS_PTXFEMP :
  590. GINTSTS_NPTXFEMP);
  591. }
  592. /* see if we can write data */
  593. if (to_write > can_write) {
  594. to_write = can_write;
  595. pkt_round = to_write % max_transfer;
  596. /*
  597. * Round the write down to an
  598. * exact number of packets.
  599. *
  600. * Note, we do not currently check to see if we can ever
  601. * write a full packet or not to the FIFO.
  602. */
  603. if (pkt_round)
  604. to_write -= pkt_round;
  605. /*
  606. * enable correct FIFO interrupt to alert us when there
  607. * is more room left.
  608. */
  609. /* it's needed only when we do not use dedicated fifos */
  610. if (!hsotg->dedicated_fifos)
  611. dwc2_hsotg_en_gsint(hsotg,
  612. periodic ? GINTSTS_PTXFEMP :
  613. GINTSTS_NPTXFEMP);
  614. }
  615. dev_dbg(hsotg->dev, "write %d/%d, can_write %d, done %d\n",
  616. to_write, hs_req->req.length, can_write, buf_pos);
  617. if (to_write <= 0)
  618. return -ENOSPC;
  619. hs_req->req.actual = buf_pos + to_write;
  620. hs_ep->total_data += to_write;
  621. if (periodic)
  622. hs_ep->fifo_load += to_write;
  623. to_write = DIV_ROUND_UP(to_write, 4);
  624. data = (u32)(hs_req->req.buf) + buf_pos;
  625. iowrite32_rep(hsotg->regs + EPFIFO(hs_ep->index), (const void*)data, to_write);
  626. return (to_write >= can_write) ? -ENOSPC : 0;
  627. }
  628. /**
  629. * get_ep_limit - get the maximum data legnth for this endpoint
  630. * @hs_ep: The endpoint
  631. *
  632. * Return the maximum data that can be queued in one go on a given endpoint
  633. * so that transfers that are too long can be split.
  634. */
  635. static unsigned int get_ep_limit(struct dwc2_hsotg_ep *hs_ep)
  636. {
  637. int index = hs_ep->index;
  638. unsigned int maxsize;
  639. unsigned int maxpkt;
  640. if (index != 0) {
  641. maxsize = DXEPTSIZ_XFERSIZE_LIMIT + 1;
  642. maxpkt = DXEPTSIZ_PKTCNT_LIMIT + 1;
  643. } else {
  644. maxsize = 64 + 64;
  645. if (hs_ep->dir_in)
  646. maxpkt = DIEPTSIZ0_PKTCNT_LIMIT + 1;
  647. else
  648. maxpkt = 2;
  649. }
  650. /* we made the constant loading easier above by using +1 */
  651. maxpkt--;
  652. maxsize--;
  653. /*
  654. * constrain by packet count if maxpkts*pktsize is greater
  655. * than the length register size.
  656. */
  657. if ((maxpkt * hs_ep->ep.maxpacket) < maxsize)
  658. maxsize = maxpkt * hs_ep->ep.maxpacket;
  659. return maxsize;
  660. }
  661. /**
  662. * dwc2_hsotg_read_frameno - read current frame number
  663. * @hsotg: The device instance
  664. *
  665. * Return the current frame number
  666. */
  667. static u32 dwc2_hsotg_read_frameno(struct dwc2_hsotg *hsotg)
  668. {
  669. u32 dsts;
  670. dsts = dwc2_readl(hsotg->regs + DSTS);
  671. dsts &= DSTS_SOFFN_MASK;
  672. dsts >>= DSTS_SOFFN_SHIFT;
  673. return dsts;
  674. }
  675. /**
  676. * dwc2_gadget_get_chain_limit - get the maximum data payload value of the
  677. * DMA descriptor chain prepared for specific endpoint
  678. * @hs_ep: The endpoint
  679. *
  680. * Return the maximum data that can be queued in one go on a given endpoint
  681. * depending on its descriptor chain capacity so that transfers that
  682. * are too long can be split.
  683. */
  684. static unsigned int dwc2_gadget_get_chain_limit(struct dwc2_hsotg_ep *hs_ep)
  685. {
  686. int is_isoc = hs_ep->isochronous;
  687. unsigned int maxsize;
  688. if (is_isoc)
  689. maxsize = hs_ep->dir_in ? DEV_DMA_ISOC_TX_NBYTES_LIMIT :
  690. DEV_DMA_ISOC_RX_NBYTES_LIMIT;
  691. else
  692. maxsize = DEV_DMA_NBYTES_LIMIT;
  693. /* Above size of one descriptor was chosen, multiple it */
  694. maxsize *= MAX_DMA_DESC_NUM_GENERIC;
  695. return maxsize;
  696. }
  697. /*
  698. * dwc2_gadget_get_desc_params - get DMA descriptor parameters.
  699. * @hs_ep: The endpoint
  700. * @mask: RX/TX bytes mask to be defined
  701. *
  702. * Returns maximum data payload for one descriptor after analyzing endpoint
  703. * characteristics.
  704. * DMA descriptor transfer bytes limit depends on EP type:
  705. * Control out - MPS,
  706. * Isochronous - descriptor rx/tx bytes bitfield limit,
  707. * Control In/Bulk/Interrupt - multiple of mps. This will allow to not
  708. * have concatenations from various descriptors within one packet.
  709. *
  710. * Selects corresponding mask for RX/TX bytes as well.
  711. */
  712. static u32 dwc2_gadget_get_desc_params(struct dwc2_hsotg_ep *hs_ep, u32 *mask)
  713. {
  714. u32 mps = hs_ep->ep.maxpacket;
  715. int dir_in = hs_ep->dir_in;
  716. u32 desc_size = 0;
  717. if (!hs_ep->index && !dir_in) {
  718. desc_size = mps;
  719. *mask = DEV_DMA_NBYTES_MASK;
  720. } else if (hs_ep->isochronous) {
  721. if (dir_in) {
  722. desc_size = DEV_DMA_ISOC_TX_NBYTES_LIMIT;
  723. *mask = DEV_DMA_ISOC_TX_NBYTES_MASK;
  724. } else {
  725. desc_size = DEV_DMA_ISOC_RX_NBYTES_LIMIT;
  726. *mask = DEV_DMA_ISOC_RX_NBYTES_MASK;
  727. }
  728. } else {
  729. desc_size = DEV_DMA_NBYTES_LIMIT;
  730. *mask = DEV_DMA_NBYTES_MASK;
  731. /* Round down desc_size to be mps multiple */
  732. desc_size -= desc_size % mps;
  733. }
  734. return desc_size;
  735. }
  736. /*
  737. * dwc2_gadget_config_nonisoc_xfer_ddma - prepare non ISOC DMA desc chain.
  738. * @hs_ep: The endpoint
  739. * @dma_buff: DMA address to use
  740. * @len: Length of the transfer
  741. *
  742. * This function will iterate over descriptor chain and fill its entries
  743. * with corresponding information based on transfer data.
  744. */
  745. static void dwc2_gadget_config_nonisoc_xfer_ddma(struct dwc2_hsotg_ep *hs_ep,
  746. dma_addr_t dma_buff,
  747. unsigned int len)
  748. {
  749. struct dwc2_hsotg *hsotg = hs_ep->parent;
  750. int dir_in = hs_ep->dir_in;
  751. struct dwc2_dma_desc *desc = hs_ep->desc_list;
  752. u32 mps = hs_ep->ep.maxpacket;
  753. u32 maxsize = 0;
  754. u32 offset = 0;
  755. u32 mask = 0;
  756. int i;
  757. USB_UNUSED(hsotg);
  758. maxsize = dwc2_gadget_get_desc_params(hs_ep, &mask);
  759. hs_ep->desc_count = (len / maxsize) +
  760. ((len % maxsize) ? 1 : 0);
  761. if (len == 0)
  762. hs_ep->desc_count = 1;
  763. for (i = 0; i < hs_ep->desc_count; ++i) {
  764. desc->status = 0;
  765. desc->status |= (DEV_DMA_BUFF_STS_HBUSY
  766. << DEV_DMA_BUFF_STS_SHIFT);
  767. if (len > maxsize) {
  768. if (!hs_ep->index && !dir_in)
  769. desc->status |= (DEV_DMA_L | DEV_DMA_IOC);
  770. desc->status |= (maxsize <<
  771. DEV_DMA_NBYTES_SHIFT & mask);
  772. desc->buf = dma_buff + offset;
  773. len -= maxsize;
  774. offset += maxsize;
  775. } else {
  776. desc->status |= (DEV_DMA_L | DEV_DMA_IOC);
  777. if (dir_in)
  778. desc->status |= (len % mps) ? DEV_DMA_SHORT :
  779. ((hs_ep->send_zlp) ? DEV_DMA_SHORT : 0);
  780. if (len > maxsize)
  781. dev_err(hsotg->dev, "wrong len %d\n", len);
  782. desc->status |=
  783. len << DEV_DMA_NBYTES_SHIFT & mask;
  784. desc->buf = dma_buff + offset;
  785. }
  786. desc->status &= ~DEV_DMA_BUFF_STS_MASK;
  787. desc->status |= (DEV_DMA_BUFF_STS_HREADY
  788. << DEV_DMA_BUFF_STS_SHIFT);
  789. desc++;
  790. }
  791. }
  792. /*
  793. * dwc2_gadget_fill_isoc_desc - fills next isochronous descriptor in chain.
  794. * @hs_ep: The isochronous endpoint.
  795. * @dma_buff: usb requests dma buffer.
  796. * @len: usb request transfer length.
  797. *
  798. * Finds out index of first free entry either in the bottom or up half of
  799. * descriptor chain depend on which is under SW control and not processed
  800. * by HW. Then fills that descriptor with the data of the arrived usb request,
  801. * frame info, sets Last and IOC bits increments next_desc. If filled
  802. * descriptor is not the first one, removes L bit from the previous descriptor
  803. * status.
  804. */
  805. static int dwc2_gadget_fill_isoc_desc(struct dwc2_hsotg_ep *hs_ep,
  806. dma_addr_t dma_buff, unsigned int len)
  807. {
  808. struct dwc2_dma_desc *desc;
  809. struct dwc2_hsotg *hsotg = hs_ep->parent;
  810. u32 index;
  811. u32 maxsize = 0;
  812. u32 mask = 0;
  813. u8 pid = 0;
  814. USB_UNUSED(hsotg);
  815. maxsize = dwc2_gadget_get_desc_params(hs_ep, &mask);
  816. if (len > maxsize) {
  817. dev_err(hsotg->dev, "wrong len %d\n", len);
  818. return -EINVAL;
  819. }
  820. /*
  821. * If SW has already filled half of chain, then return and wait for
  822. * the other chain to be processed by HW.
  823. */
  824. if (hs_ep->next_desc == MAX_DMA_DESC_NUM_GENERIC / 2)
  825. return -EBUSY;
  826. /* Increment frame number by interval for IN */
  827. if (hs_ep->dir_in)
  828. dwc2_gadget_incr_frame_num(hs_ep);
  829. index = (MAX_DMA_DESC_NUM_GENERIC / 2) * hs_ep->isoc_chain_num +
  830. hs_ep->next_desc;
  831. /* Sanity check of calculated index */
  832. if ((hs_ep->isoc_chain_num && index > MAX_DMA_DESC_NUM_GENERIC) ||
  833. (!hs_ep->isoc_chain_num && index > MAX_DMA_DESC_NUM_GENERIC / 2)) {
  834. dev_err(hsotg->dev, "wrong index %d for iso chain\n", index);
  835. return -EINVAL;
  836. }
  837. desc = &hs_ep->desc_list[index];
  838. /* Clear L bit of previous desc if more than one entries in the chain */
  839. if (hs_ep->next_desc)
  840. hs_ep->desc_list[index - 1].status &= ~DEV_DMA_L;
  841. dev_dbg(hsotg->dev, "%s: Filling ep %d, dir %s isoc desc # %d\n",
  842. __func__, hs_ep->index, hs_ep->dir_in ? "in" : "out", index);
  843. desc->status = 0;
  844. desc->status |= (DEV_DMA_BUFF_STS_HBUSY << DEV_DMA_BUFF_STS_SHIFT);
  845. desc->buf = dma_buff;
  846. desc->status |= (DEV_DMA_L | DEV_DMA_IOC |
  847. ((len << DEV_DMA_NBYTES_SHIFT) & mask));
  848. if (hs_ep->dir_in) {
  849. if (len)
  850. pid = DIV_ROUND_UP(len, hs_ep->ep.maxpacket);
  851. else
  852. pid = 1;
  853. desc->status |= ((pid << DEV_DMA_ISOC_PID_SHIFT) &
  854. DEV_DMA_ISOC_PID_MASK) |
  855. ((len % hs_ep->ep.maxpacket) ?
  856. DEV_DMA_SHORT : 0) |
  857. ((hs_ep->target_frame <<
  858. DEV_DMA_ISOC_FRNUM_SHIFT) &
  859. DEV_DMA_ISOC_FRNUM_MASK);
  860. }
  861. desc->status &= ~DEV_DMA_BUFF_STS_MASK;
  862. desc->status |= (DEV_DMA_BUFF_STS_HREADY << DEV_DMA_BUFF_STS_SHIFT);
  863. /* Update index of last configured entry in the chain */
  864. hs_ep->next_desc++;
  865. return 0;
  866. }
  867. /*
  868. * dwc2_gadget_start_isoc_ddma - start isochronous transfer in DDMA
  869. * @hs_ep: The isochronous endpoint.
  870. *
  871. * Prepare first descriptor chain for isochronous endpoints. Afterwards
  872. * write DMA address to HW and enable the endpoint.
  873. *
  874. * Switch between descriptor chains via isoc_chain_num to give SW opportunity
  875. * to prepare second descriptor chain while first one is being processed by HW.
  876. */
  877. static void dwc2_gadget_start_isoc_ddma(struct dwc2_hsotg_ep *hs_ep)
  878. {
  879. struct dwc2_hsotg *hsotg = hs_ep->parent;
  880. struct dwc2_hsotg_req *hs_req;//, *treq;
  881. int index = hs_ep->index;
  882. int ret;
  883. u32 dma_reg;
  884. u32 depctl;
  885. u32 ctrl;
  886. if (list_empty(&hs_ep->queue)) {
  887. hs_ep->target_frame = TARGET_FRAME_INITIAL;
  888. dev_dbg(hsotg->dev, "%s: No requests in queue\n", __func__);
  889. return;
  890. }
  891. #ifndef NO_GNU
  892. //list_for_each_entry_safe(hs_req, treq, &hs_ep->queue, queue) {
  893. #else
  894. ListItem_t *pxListItem, *nListItem;
  895. list_for_each_entry_safe(pxListItem, nListItem, hs_req, &hs_ep->queue) {
  896. #endif
  897. ret = dwc2_gadget_fill_isoc_desc(hs_ep, hs_req->req.dma,
  898. hs_req->req.length);
  899. if (ret) {
  900. dev_dbg(hsotg->dev, "%s: desc chain full\n", __func__);
  901. break;
  902. }
  903. }
  904. depctl = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index);
  905. dma_reg = hs_ep->dir_in ? DIEPDMA(index) : DOEPDMA(index);
  906. /* write descriptor chain address to control register */
  907. dwc2_writel(hs_ep->desc_list_dma, hsotg->regs + dma_reg);
  908. ctrl = dwc2_readl(hsotg->regs + depctl);
  909. ctrl |= DXEPCTL_EPENA | DXEPCTL_CNAK;
  910. dwc2_writel(ctrl, hsotg->regs + depctl);
  911. /* Switch ISOC descriptor chain number being processed by SW*/
  912. hs_ep->isoc_chain_num = (hs_ep->isoc_chain_num ^ 1) & 0x1;
  913. hs_ep->next_desc = 0;
  914. }
  915. /**
  916. * dwc2_hsotg_start_req - start a USB request from an endpoint's queue
  917. * @hsotg: The controller state.
  918. * @hs_ep: The endpoint to process a request for
  919. * @hs_req: The request to start.
  920. * @continuing: True if we are doing more for the current request.
  921. *
  922. * Start the given request running by setting the endpoint registers
  923. * appropriately, and writing any data to the FIFOs.
  924. */
  925. static void dwc2_hsotg_start_req(struct dwc2_hsotg *hsotg,
  926. struct dwc2_hsotg_ep *hs_ep,
  927. struct dwc2_hsotg_req *hs_req,
  928. bool continuing)
  929. {
  930. struct usb_request *ureq = &hs_req->req;
  931. int index = hs_ep->index;
  932. int dir_in = hs_ep->dir_in;
  933. u32 epctrl_reg;
  934. u32 epsize_reg;
  935. u32 epsize;
  936. u32 ctrl;
  937. unsigned int length;
  938. unsigned int packets;
  939. unsigned int maxreq;
  940. unsigned int dma_reg;
  941. if (index != 0) {
  942. if (hs_ep->req && !continuing) {
  943. dev_err(hsotg->dev, "%s: active request\n", __func__);
  944. WARN_ON(1);
  945. return;
  946. } else if (hs_ep->req != hs_req && continuing) {
  947. dev_err(hsotg->dev,
  948. "%s: continue different req\n", __func__);
  949. WARN_ON(1);
  950. return;
  951. }
  952. }
  953. dma_reg = dir_in ? DIEPDMA(index) : DOEPDMA(index);
  954. epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
  955. epsize_reg = dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
  956. dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x, ep %d, dir %s\r\n",
  957. __func__, dwc2_readl(hsotg->regs + epctrl_reg), index,
  958. hs_ep->dir_in ? "in" : "out");
  959. /* If endpoint is stalled, we will restart request later */
  960. ctrl = dwc2_readl(hsotg->regs + epctrl_reg);
  961. if (index && ctrl & DXEPCTL_STALL) {
  962. dev_warn(hsotg->dev, "%s: ep%d is stalled\n", __func__, index);
  963. return;
  964. }
  965. length = ureq->length - ureq->actual;
  966. dev_dbg(hsotg->dev, "ureq->length:%d ureq->actual:%d\n",
  967. ureq->length, ureq->actual);
  968. if (!using_desc_dma(hsotg))
  969. maxreq = get_ep_limit(hs_ep);
  970. else
  971. maxreq = dwc2_gadget_get_chain_limit(hs_ep);
  972. if (length > maxreq) {
  973. int round = maxreq % hs_ep->ep.maxpacket;
  974. dev_dbg(hsotg->dev, "%s: length %d, max-req %d, r %d\n",
  975. __func__, length, maxreq, round);
  976. /* round down to multiple of packets */
  977. if (round)
  978. maxreq -= round;
  979. length = maxreq;
  980. }
  981. if (length)
  982. packets = DIV_ROUND_UP(length, hs_ep->ep.maxpacket);
  983. else
  984. packets = 1; /* send one packet if length is zero. */
  985. if (hs_ep->isochronous && length > (hs_ep->mc * hs_ep->ep.maxpacket)) {
  986. dev_err(hsotg->dev, "req length > maxpacket*mc\n");
  987. return;
  988. }
  989. if (dir_in && index != 0)
  990. if (hs_ep->isochronous)
  991. epsize = DXEPTSIZ_MC(packets);
  992. else
  993. epsize = DXEPTSIZ_MC(1);
  994. else
  995. epsize = 0;
  996. /*
  997. * zero length packet should be programmed on its own and should not
  998. * be counted in DIEPTSIZ.PktCnt with other packets.
  999. */
  1000. if (dir_in && ureq->zero && !continuing) {
  1001. /* Test if zlp is actually required. */
  1002. if ((ureq->length >= hs_ep->ep.maxpacket) &&
  1003. !(ureq->length % hs_ep->ep.maxpacket))
  1004. hs_ep->send_zlp = 1;
  1005. }
  1006. epsize |= DXEPTSIZ_PKTCNT(packets);
  1007. epsize |= DXEPTSIZ_XFERSIZE(length);
  1008. dev_dbg(hsotg->dev, "%s: %d@%d/%d, 0x%08x => 0x%08x\r\n",
  1009. __func__, packets, length, ureq->length, epsize, epsize_reg);
  1010. /* store the request as the current one we're doing */
  1011. hs_ep->req = hs_req;
  1012. if (using_desc_dma(hsotg)) {
  1013. u32 offset = 0;
  1014. u32 mps = hs_ep->ep.maxpacket;
  1015. /* Adjust length: EP0 - MPS, other OUT EPs - multiple of MPS */
  1016. if (!dir_in) {
  1017. if (!index)
  1018. length = mps;
  1019. else if (length % mps)
  1020. length += (mps - (length % mps));
  1021. }
  1022. /*
  1023. * If more data to send, adjust DMA for EP0 out data stage.
  1024. * ureq->dma stays unchanged, hence increment it by already
  1025. * passed passed data count before starting new transaction.
  1026. */
  1027. if (!index && hsotg->ep0_state == DWC2_EP0_DATA_OUT &&
  1028. continuing)
  1029. offset = ureq->actual;
  1030. /* Fill DDMA chain entries */
  1031. dwc2_gadget_config_nonisoc_xfer_ddma(hs_ep, ureq->dma + offset,
  1032. length);
  1033. /* write descriptor chain address to control register */
  1034. dwc2_writel(hs_ep->desc_list_dma, hsotg->regs + dma_reg);
  1035. dev_dbg(hsotg->dev, "%s: %08x pad => 0x%08x\n",
  1036. __func__, (u32)hs_ep->desc_list_dma, dma_reg);
  1037. } else {
  1038. /* write size / packets */
  1039. dwc2_writel(epsize, hsotg->regs + epsize_reg);
  1040. if (using_dma(hsotg) && !continuing && (length != 0)) {
  1041. /*
  1042. * write DMA address to control register, buffer
  1043. * already synced by dwc2_hsotg_ep_queue().
  1044. */
  1045. #ifdef NO_GNU
  1046. CP15_flush_dcache_for_dma((uint32_t)ureq->dma, (uint32_t)((uint32_t)ureq->dma + length));
  1047. #endif
  1048. dwc2_writel(ureq->dma, hsotg->regs + dma_reg);
  1049. dev_dbg(hsotg->dev, "%s: %pad => 0x%08x epsize:%d length:%d 0x%08x 0x%08x\r\n",
  1050. __func__, &ureq->dma, dma_reg, epsize, length, ureq->buf, ureq->dma);
  1051. }
  1052. }
  1053. if (hs_ep->isochronous && hs_ep->interval == 1) {
  1054. hs_ep->target_frame = dwc2_hsotg_read_frameno(hsotg);
  1055. dwc2_gadget_incr_frame_num(hs_ep);
  1056. if (hs_ep->target_frame & 0x1)
  1057. ctrl |= DXEPCTL_SETODDFR;
  1058. else
  1059. ctrl |= DXEPCTL_SETEVENFR;
  1060. }
  1061. ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */
  1062. dev_dbg(hsotg->dev, "ep0 state:%d\n", hsotg->ep0_state);
  1063. /* For Setup request do not clear NAK */
  1064. if (!(index == 0 && hsotg->ep0_state == DWC2_EP0_SETUP))
  1065. ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */
  1066. dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
  1067. dwc2_writel(ctrl, hsotg->regs + epctrl_reg);
  1068. /*
  1069. * set these, it seems that DMA support increments past the end
  1070. * of the packet buffer so we need to calculate the length from
  1071. * this information.
  1072. */
  1073. hs_ep->size_loaded = length;
  1074. hs_ep->last_load = ureq->actual;
  1075. if (dir_in && !using_dma(hsotg)) {
  1076. /* set these anyway, we may need them for non-periodic in */
  1077. hs_ep->fifo_load = 0;
  1078. dwc2_hsotg_write_fifo(hsotg, hs_ep, hs_req);
  1079. }
  1080. /*
  1081. * Note, trying to clear the NAK here causes problems with transmit
  1082. * on the S3C6400 ending up with the TXFIFO becoming full.
  1083. */
  1084. /* check ep is enabled */
  1085. if (!(dwc2_readl(hsotg->regs + epctrl_reg) & DXEPCTL_EPENA))
  1086. dev_dbg(hsotg->dev,
  1087. "ep%d: failed to become enabled (DXEPCTL=0x%08x)?\n",
  1088. index, dwc2_readl(hsotg->regs + epctrl_reg));
  1089. dev_dbg(hsotg->dev, "%s: DXEPCTL=0x%08x\n",
  1090. __func__, dwc2_readl(hsotg->regs + epctrl_reg));
  1091. /* enable ep interrupts */
  1092. dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 1);
  1093. }
  1094. /**
  1095. * dwc2_hsotg_map_dma - map the DMA memory being used for the request
  1096. * @hsotg: The device state.
  1097. * @hs_ep: The endpoint the request is on.
  1098. * @req: The request being processed.
  1099. *
  1100. * We've been asked to queue a request, so ensure that the memory buffer
  1101. * is correctly setup for DMA. If we've been passed an extant DMA address
  1102. * then ensure the buffer has been synced to memory. If our buffer has no
  1103. * DMA memory, then we map the memory and mark our request to allow us to
  1104. * cleanup on completion.
  1105. */
  1106. static int dwc2_hsotg_map_dma(struct dwc2_hsotg *hsotg,
  1107. struct dwc2_hsotg_ep *hs_ep,
  1108. struct usb_request *req)
  1109. {
  1110. int ret;
  1111. ret = usb_gadget_map_request(&hsotg->gadget, req, hs_ep->dir_in);
  1112. if (ret)
  1113. goto dma_error;
  1114. return 0;
  1115. dma_error:
  1116. dev_err(hsotg->dev, "%s: failed to map buffer %p, %d bytes\n",
  1117. __func__, req->buf, req->length);
  1118. return -EIO;
  1119. }
  1120. static int dwc2_hsotg_handle_unaligned_buf_start(struct dwc2_hsotg *hsotg,
  1121. struct dwc2_hsotg_ep *hs_ep,
  1122. struct dwc2_hsotg_req *hs_req)
  1123. {
  1124. void *req_buf = hs_req->req.buf;
  1125. /* If dma is not being used or buffer is aligned */
  1126. if (!using_dma(hsotg) || !((long)req_buf & 3))
  1127. return 0;
  1128. WARN_ON(hs_req->saved_req_buf);
  1129. dev_dbg(hsotg->dev, "%s: %s: buf=%p length=%d\n", __func__,
  1130. hs_ep->ep.name, req_buf, hs_req->req.length);
  1131. hs_req->req.buf = kmalloc(hs_req->req.length, GFP_ATOMIC);
  1132. if (!hs_req->req.buf) {
  1133. hs_req->req.buf = req_buf;
  1134. dev_err(hsotg->dev,
  1135. "%s: unable to allocate memory for bounce buffer\n",
  1136. __func__);
  1137. return -ENOMEM;
  1138. }
  1139. /* Save actual buffer */
  1140. hs_req->saved_req_buf = req_buf;
  1141. if (hs_ep->dir_in)
  1142. memcpy(hs_req->req.buf, req_buf, hs_req->req.length);
  1143. return 0;
  1144. }
  1145. static void
  1146. dwc2_hsotg_handle_unaligned_buf_complete(struct dwc2_hsotg *hsotg,
  1147. struct dwc2_hsotg_ep *hs_ep,
  1148. struct dwc2_hsotg_req *hs_req)
  1149. {
  1150. /* If dma is not being used or buffer was aligned */
  1151. if (!using_dma(hsotg) || !hs_req->saved_req_buf)
  1152. return;
  1153. dev_dbg(hsotg->dev, "%s: %s: status=%d actual-length=%d\n", __func__,
  1154. hs_ep->ep.name, hs_req->req.status, hs_req->req.actual);
  1155. /* Copy data from bounce buffer on successful out transfer */
  1156. if (!hs_ep->dir_in && !hs_req->req.status)
  1157. memcpy(hs_req->saved_req_buf, hs_req->req.buf,
  1158. hs_req->req.actual);
  1159. /* Free bounce buffer */
  1160. kfree(hs_req->req.buf);
  1161. hs_req->req.buf = hs_req->saved_req_buf;
  1162. hs_req->saved_req_buf = NULL;
  1163. }
  1164. /**
  1165. * dwc2_gadget_target_frame_elapsed - Checks target frame
  1166. * @hs_ep: The driver endpoint to check
  1167. *
  1168. * Returns 1 if targeted frame elapsed. If returned 1 then we need to drop
  1169. * corresponding transfer.
  1170. */
  1171. static bool dwc2_gadget_target_frame_elapsed(struct dwc2_hsotg_ep *hs_ep)
  1172. {
  1173. struct dwc2_hsotg *hsotg = hs_ep->parent;
  1174. u32 target_frame = hs_ep->target_frame;
  1175. u32 current_frame = dwc2_hsotg_read_frameno(hsotg);
  1176. bool frame_overrun = hs_ep->frame_overrun;
  1177. if (!frame_overrun && current_frame >= target_frame)
  1178. return true;
  1179. if (frame_overrun && current_frame >= target_frame &&
  1180. ((current_frame - target_frame) < DSTS_SOFFN_LIMIT / 2))
  1181. return true;
  1182. return false;
  1183. }
  1184. /*
  1185. * dwc2_gadget_set_ep0_desc_chain - Set EP's desc chain pointers
  1186. * @hsotg: The driver state
  1187. * @hs_ep: the ep descriptor chain is for
  1188. *
  1189. * Called to update EP0 structure's pointers depend on stage of
  1190. * control transfer.
  1191. */
  1192. static int dwc2_gadget_set_ep0_desc_chain(struct dwc2_hsotg *hsotg,
  1193. struct dwc2_hsotg_ep *hs_ep)
  1194. {
  1195. switch (hsotg->ep0_state) {
  1196. case DWC2_EP0_SETUP:
  1197. case DWC2_EP0_STATUS_OUT:
  1198. hs_ep->desc_list = hsotg->setup_desc[0];
  1199. hs_ep->desc_list_dma = hsotg->setup_desc_dma[0];
  1200. break;
  1201. case DWC2_EP0_DATA_IN:
  1202. case DWC2_EP0_STATUS_IN:
  1203. hs_ep->desc_list = hsotg->ctrl_in_desc;
  1204. hs_ep->desc_list_dma = hsotg->ctrl_in_desc_dma;
  1205. break;
  1206. case DWC2_EP0_DATA_OUT:
  1207. hs_ep->desc_list = hsotg->ctrl_out_desc;
  1208. hs_ep->desc_list_dma = hsotg->ctrl_out_desc_dma;
  1209. break;
  1210. default:
  1211. dev_err(hsotg->dev, "invalid EP 0 state in queue %d\n",
  1212. hsotg->ep0_state);
  1213. return -EINVAL;
  1214. }
  1215. return 0;
  1216. }
  1217. static int dwc2_hsotg_ep_queue(struct usb_ep *ep, struct usb_request *req,
  1218. gfp_t gfp_flags)
  1219. {
  1220. struct dwc2_hsotg_req *hs_req = our_req(req);
  1221. struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
  1222. struct dwc2_hsotg *hs = hs_ep->parent;
  1223. bool first;
  1224. int ret;
  1225. dev_dbg(hs->dev, "%s: req %p: %d@%p, noi=%d, zero=%d, snok=%d\n",
  1226. ep->name, req, req->length, req->buf, req->no_interrupt,
  1227. req->zero, req->short_not_ok);
  1228. /* Prevent new request submission when controller is suspended */
  1229. if (hs->lx_state == DWC2_L2) {
  1230. dev_dbg(hs->dev, "%s: don't submit request while suspended\n",
  1231. __func__);
  1232. return -EAGAIN;
  1233. }
  1234. /* initialise status of the request */
  1235. #ifndef NO_GNU
  1236. INIT_LIST_HEAD(&hs_req->queue);
  1237. #else
  1238. INIT_LIST_ITEM(&hs_req->queue);
  1239. hs_req->queue.pvOwner = (void *)hs_req;
  1240. #endif
  1241. req->actual = 0;
  1242. req->status = -EINPROGRESS;
  1243. ret = dwc2_hsotg_handle_unaligned_buf_start(hs, hs_ep, hs_req);
  1244. if (ret)
  1245. return ret;
  1246. /* if we're using DMA, sync the buffers as necessary */
  1247. if (using_dma(hs)) {
  1248. ret = dwc2_hsotg_map_dma(hs, hs_ep, req);
  1249. if (ret)
  1250. return ret;
  1251. }
  1252. /* If using descriptor DMA configure EP0 descriptor chain pointers */
  1253. if (using_desc_dma(hs) && !hs_ep->index) {
  1254. ret = dwc2_gadget_set_ep0_desc_chain(hs, hs_ep);
  1255. if (ret)
  1256. return ret;
  1257. }
  1258. first = list_empty(&hs_ep->queue);
  1259. list_add_tail(&hs_req->queue, &hs_ep->queue);
  1260. /*
  1261. * Handle DDMA isochronous transfers separately - just add new entry
  1262. * to the half of descriptor chain that is not processed by HW.
  1263. * Transfer will be started once SW gets either one of NAK or
  1264. * OutTknEpDis interrupts.
  1265. */
  1266. if (using_desc_dma(hs) && hs_ep->isochronous &&
  1267. hs_ep->target_frame != TARGET_FRAME_INITIAL) {
  1268. ret = dwc2_gadget_fill_isoc_desc(hs_ep, hs_req->req.dma,
  1269. hs_req->req.length);
  1270. if (ret)
  1271. dev_dbg(hs->dev, "%s: ISO desc chain full\n", __func__);
  1272. return 0;
  1273. }
  1274. if (first) {
  1275. if (!hs_ep->isochronous) {
  1276. dwc2_hsotg_start_req(hs, hs_ep, hs_req, false);
  1277. return 0;
  1278. }
  1279. while (dwc2_gadget_target_frame_elapsed(hs_ep))
  1280. dwc2_gadget_incr_frame_num(hs_ep);
  1281. if (hs_ep->target_frame != TARGET_FRAME_INITIAL)
  1282. dwc2_hsotg_start_req(hs, hs_ep, hs_req, false);
  1283. }
  1284. return 0;
  1285. }
  1286. static int dwc2_hsotg_ep_queue_lock(struct usb_ep *ep, struct usb_request *req,
  1287. gfp_t gfp_flags)
  1288. {
  1289. struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
  1290. struct dwc2_hsotg *hs = hs_ep->parent;
  1291. unsigned long flags = 0;
  1292. int ret = 0;
  1293. spin_lock_irqsave(&hs->lock, flags);
  1294. ret = dwc2_hsotg_ep_queue(ep, req, gfp_flags);
  1295. spin_unlock_irqrestore(&hs->lock, flags);
  1296. return ret;
  1297. }
  1298. static void dwc2_hsotg_ep_free_request(struct usb_ep *ep,
  1299. struct usb_request *req)
  1300. {
  1301. struct dwc2_hsotg_req *hs_req = our_req(req);
  1302. free_used_req(hs_req);//kfree(hs_req);
  1303. }
  1304. /**
  1305. * dwc2_hsotg_complete_oursetup - setup completion callback
  1306. * @ep: The endpoint the request was on.
  1307. * @req: The request completed.
  1308. *
  1309. * Called on completion of any requests the driver itself
  1310. * submitted that need cleaning up.
  1311. */
  1312. static void dwc2_hsotg_complete_oursetup(struct usb_ep *ep,
  1313. struct usb_request *req)
  1314. {
  1315. struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
  1316. struct dwc2_hsotg *hsotg = hs_ep->parent;
  1317. USB_UNUSED(hsotg);
  1318. dev_dbg(hsotg->dev, "%s: ep %p, req %p\n", __func__, ep, req);
  1319. dwc2_hsotg_ep_free_request(ep, req);
  1320. }
  1321. /**
  1322. * ep_from_windex - convert control wIndex value to endpoint
  1323. * @hsotg: The driver state.
  1324. * @windex: The control request wIndex field (in host order).
  1325. *
  1326. * Convert the given wIndex into a pointer to an driver endpoint
  1327. * structure, or return NULL if it is not a valid endpoint.
  1328. */
  1329. static struct dwc2_hsotg_ep *ep_from_windex(struct dwc2_hsotg *hsotg,
  1330. u32 windex)
  1331. {
  1332. struct dwc2_hsotg_ep *ep;
  1333. int dir = (windex & USB_DIR_IN) ? 1 : 0;
  1334. int idx = windex & 0x7F;
  1335. if (windex >= 0x100)
  1336. return NULL;
  1337. if (idx > hsotg->num_of_eps)
  1338. return NULL;
  1339. ep = index_to_ep(hsotg, idx, dir);
  1340. if (idx && ep->dir_in != dir)
  1341. return NULL;
  1342. return ep;
  1343. }
  1344. /**
  1345. * dwc2_hsotg_set_test_mode - Enable usb Test Modes
  1346. * @hsotg: The driver state.
  1347. * @testmode: requested usb test mode
  1348. * Enable usb Test Mode requested by the Host.
  1349. */
  1350. int dwc2_hsotg_set_test_mode(struct dwc2_hsotg *hsotg, int testmode)
  1351. {
  1352. int dctl = dwc2_readl(hsotg->regs + DCTL);
  1353. dctl &= ~DCTL_TSTCTL_MASK;
  1354. switch (testmode) {
  1355. case TEST_J:
  1356. case TEST_K:
  1357. case TEST_SE0_NAK:
  1358. case TEST_PACKET:
  1359. case TEST_FORCE_EN:
  1360. dctl |= testmode << DCTL_TSTCTL_SHIFT;
  1361. break;
  1362. default:
  1363. return -EINVAL;
  1364. }
  1365. dwc2_writel(dctl, hsotg->regs + DCTL);
  1366. return 0;
  1367. }
  1368. /**
  1369. * dwc2_hsotg_send_reply - send reply to control request
  1370. * @hsotg: The device state
  1371. * @ep: Endpoint 0
  1372. * @buff: Buffer for request
  1373. * @length: Length of reply.
  1374. *
  1375. * Create a request and queue it on the given endpoint. This is useful as
  1376. * an internal method of sending replies to certain control requests, etc.
  1377. */
  1378. static int dwc2_hsotg_send_reply(struct dwc2_hsotg *hsotg,
  1379. struct dwc2_hsotg_ep *ep,
  1380. void *buff,
  1381. int length)
  1382. {
  1383. struct usb_request *req;
  1384. int ret;
  1385. dev_dbg(hsotg->dev, "%s: buff %p, len %d\n", __func__, buff, length);
  1386. req = dwc2_hsotg_ep_alloc_request(&ep->ep, GFP_ATOMIC);
  1387. hsotg->ep0_reply = req;
  1388. if (!req) {
  1389. dev_warn(hsotg->dev, "%s: cannot alloc req\n", __func__);
  1390. return -ENOMEM;
  1391. }
  1392. req->buf = hsotg->ep0_buff;
  1393. req->length = length;
  1394. /*
  1395. * zero flag is for sending zlp in DATA IN stage. It has no impact on
  1396. * STATUS stage.
  1397. */
  1398. req->zero = 0;
  1399. req->complete = dwc2_hsotg_complete_oursetup;
  1400. if (length)
  1401. memcpy(req->buf, buff, length);
  1402. ret = dwc2_hsotg_ep_queue(&ep->ep, req, GFP_ATOMIC);
  1403. if (ret) {
  1404. dev_warn(hsotg->dev, "%s: cannot queue req\n", __func__);
  1405. return ret;
  1406. }
  1407. return 0;
  1408. }
  1409. /**
  1410. * dwc2_hsotg_process_req_status - process request GET_STATUS
  1411. * @hsotg: The device state
  1412. * @ctrl: USB control request
  1413. */
  1414. static int dwc2_hsotg_process_req_status(struct dwc2_hsotg *hsotg,
  1415. struct usb_ctrlrequest *ctrl)
  1416. {
  1417. struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
  1418. struct dwc2_hsotg_ep *ep;
  1419. __le16 reply;
  1420. int ret;
  1421. dev_dbg(hsotg->dev, "%s: USB_REQ_GET_STATUS\n", __func__);
  1422. if (!ep0->dir_in) {
  1423. dev_warn(hsotg->dev, "%s: direction out?\n", __func__);
  1424. return -EINVAL;
  1425. }
  1426. switch (ctrl->bRequestType & USB_RECIP_MASK) {
  1427. case USB_RECIP_DEVICE:
  1428. /*
  1429. * bit 0 => self powered
  1430. * bit 1 => remote wakeup
  1431. */
  1432. reply = cpu_to_le16(0);
  1433. break;
  1434. case USB_RECIP_INTERFACE:
  1435. /* currently, the data result should be zero */
  1436. reply = cpu_to_le16(0);
  1437. break;
  1438. case USB_RECIP_ENDPOINT:
  1439. ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex));
  1440. if (!ep)
  1441. return -ENOENT;
  1442. reply = cpu_to_le16(ep->halted ? 1 : 0);
  1443. break;
  1444. default:
  1445. return 0;
  1446. }
  1447. if (le16_to_cpu(ctrl->wLength) != 2)
  1448. return -EINVAL;
  1449. ret = dwc2_hsotg_send_reply(hsotg, ep0, &reply, 2);
  1450. if (ret) {
  1451. dev_err(hsotg->dev, "%s: failed to send reply\n", __func__);
  1452. return ret;
  1453. }
  1454. return 1;
  1455. }
  1456. static int dwc2_hsotg_ep_sethalt(struct usb_ep *ep, int value, bool now);
  1457. /**
  1458. * get_ep_head - return the first request on the endpoint
  1459. * @hs_ep: The controller endpoint to get
  1460. *
  1461. * Get the first request on the endpoint.
  1462. */
  1463. /*#ifndef NO_GNU
  1464. #ifndef list_first_entry_or_null(ptr, type, member)
  1465. #define list_first_entry_or_null(ptr, type, member) ({ \
  1466. struct list_head *head__ = (ptr); \
  1467. struct list_head *pos__ = (head__->next); \
  1468. pos__ != head__ ? list_entry(pos__, type, member) : NULL; \
  1469. })
  1470. #endif*/
  1471. static struct dwc2_hsotg_req *get_ep_head(struct dwc2_hsotg_ep *hs_ep)
  1472. {
  1473. #ifndef NO_GNU
  1474. return list_first_entry_or_null(&hs_ep->queue, struct dwc2_hsotg_req,
  1475. queue);
  1476. #else
  1477. return list_first_entry(&hs_ep->queue);
  1478. #endif
  1479. }
  1480. /**
  1481. * dwc2_gadget_start_next_request - Starts next request from ep queue
  1482. * @hs_ep: Endpoint structure
  1483. *
  1484. * If queue is empty and EP is ISOC-OUT - unmasks OUTTKNEPDIS which is masked
  1485. * in its handler. Hence we need to unmask it here to be able to do
  1486. * resynchronization.
  1487. */
  1488. static void dwc2_gadget_start_next_request(struct dwc2_hsotg_ep *hs_ep)
  1489. {
  1490. u32 mask;
  1491. struct dwc2_hsotg *hsotg = hs_ep->parent;
  1492. int dir_in = hs_ep->dir_in;
  1493. struct dwc2_hsotg_req *hs_req;
  1494. u32 epmsk_reg = dir_in ? DIEPMSK : DOEPMSK;
  1495. if (!list_empty(&hs_ep->queue)) {
  1496. hs_req = get_ep_head(hs_ep);
  1497. dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, false);
  1498. return;
  1499. }
  1500. if (!hs_ep->isochronous)
  1501. return;
  1502. if (dir_in) {
  1503. dev_dbg(hsotg->dev, "%s: No more ISOC-IN requests\n",
  1504. __func__);
  1505. } else {
  1506. dev_dbg(hsotg->dev, "%s: No more ISOC-OUT requests\n",
  1507. __func__);
  1508. mask = dwc2_readl(hsotg->regs + epmsk_reg);
  1509. mask |= DOEPMSK_OUTTKNEPDISMSK;
  1510. dwc2_writel(mask, hsotg->regs + epmsk_reg);
  1511. }
  1512. }
  1513. /**
  1514. * dwc2_hsotg_process_req_feature - process request {SET,CLEAR}_FEATURE
  1515. * @hsotg: The device state
  1516. * @ctrl: USB control request
  1517. */
  1518. static int dwc2_hsotg_process_req_feature(struct dwc2_hsotg *hsotg,
  1519. struct usb_ctrlrequest *ctrl)
  1520. {
  1521. struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
  1522. struct dwc2_hsotg_req *hs_req;
  1523. bool set = (ctrl->bRequest == USB_REQ_SET_FEATURE);
  1524. struct dwc2_hsotg_ep *ep;
  1525. int ret;
  1526. bool halted;
  1527. u32 recip;
  1528. u32 wValue;
  1529. u32 wIndex;
  1530. dev_dbg(hsotg->dev, "%s: %s_FEATURE\n",
  1531. __func__, set ? "SET" : "CLEAR");
  1532. wValue = le16_to_cpu(ctrl->wValue);
  1533. wIndex = le16_to_cpu(ctrl->wIndex);
  1534. recip = ctrl->bRequestType & USB_RECIP_MASK;
  1535. switch (recip) {
  1536. case USB_RECIP_DEVICE:
  1537. switch (wValue) {
  1538. case USB_DEVICE_TEST_MODE:
  1539. if ((wIndex & 0xff) != 0)
  1540. return -EINVAL;
  1541. if (!set)
  1542. return -EINVAL;
  1543. hsotg->test_mode = wIndex >> 8;
  1544. ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
  1545. if (ret) {
  1546. dev_err(hsotg->dev,
  1547. "%s: failed to send reply\n", __func__);
  1548. return ret;
  1549. }
  1550. break;
  1551. default:
  1552. return -ENOENT;
  1553. }
  1554. break;
  1555. case USB_RECIP_ENDPOINT:
  1556. ep = ep_from_windex(hsotg, wIndex);
  1557. if (!ep) {
  1558. dev_dbg(hsotg->dev, "%s: no endpoint for 0x%04x\n",
  1559. __func__, wIndex);
  1560. return -ENOENT;
  1561. }
  1562. switch (wValue) {
  1563. case USB_ENDPOINT_HALT:
  1564. halted = ep->halted;
  1565. dwc2_hsotg_ep_sethalt(&ep->ep, set, true);
  1566. ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
  1567. if (ret) {
  1568. dev_err(hsotg->dev,
  1569. "%s: failed to send reply\n", __func__);
  1570. return ret;
  1571. }
  1572. /*
  1573. * we have to complete all requests for ep if it was
  1574. * halted, and the halt was cleared by CLEAR_FEATURE
  1575. */
  1576. if (!set && halted) {
  1577. /*
  1578. * If we have request in progress,
  1579. * then complete it
  1580. */
  1581. if (ep->req) {
  1582. hs_req = ep->req;
  1583. ep->req = NULL;
  1584. list_del_init(&hs_req->queue);
  1585. if (hs_req->req.complete) {
  1586. spin_unlock(&hsotg->lock);
  1587. usb_gadget_giveback_request(
  1588. &ep->ep, &hs_req->req);
  1589. spin_lock(&hsotg->lock);
  1590. }
  1591. }
  1592. /* If we have pending request, then start it */
  1593. if (!ep->req)
  1594. dwc2_gadget_start_next_request(ep);
  1595. }
  1596. break;
  1597. default:
  1598. return -ENOENT;
  1599. }
  1600. break;
  1601. default:
  1602. return -ENOENT;
  1603. }
  1604. return 1;
  1605. }
  1606. static void dwc2_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg);
  1607. /**
  1608. * dwc2_hsotg_stall_ep0 - stall ep0
  1609. * @hsotg: The device state
  1610. *
  1611. * Set stall for ep0 as response for setup request.
  1612. */
  1613. static void dwc2_hsotg_stall_ep0(struct dwc2_hsotg *hsotg)
  1614. {
  1615. struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
  1616. u32 reg;
  1617. u32 ctrl;
  1618. dev_dbg(hsotg->dev, "ep0 stall (dir=%d)\n", ep0->dir_in);
  1619. reg = (ep0->dir_in) ? DIEPCTL0 : DOEPCTL0;
  1620. /*
  1621. * DxEPCTL_Stall will be cleared by EP once it has
  1622. * taken effect, so no need to clear later.
  1623. */
  1624. ctrl = dwc2_readl(hsotg->regs + reg);
  1625. ctrl |= DXEPCTL_STALL;
  1626. ctrl |= DXEPCTL_CNAK;
  1627. dwc2_writel(ctrl, hsotg->regs + reg);
  1628. dev_dbg(hsotg->dev,
  1629. "written DXEPCTL=0x%08x to %08x (DXEPCTL=0x%08x)\n",
  1630. ctrl, reg, dwc2_readl(hsotg->regs + reg));
  1631. /*
  1632. * complete won't be called, so we enqueue
  1633. * setup request here
  1634. */
  1635. dwc2_hsotg_enqueue_setup(hsotg);
  1636. }
  1637. /**
  1638. * dwc2_hsotg_process_control - process a control request
  1639. * @hsotg: The device state
  1640. * @ctrl: The control request received
  1641. *
  1642. * The controller has received the SETUP phase of a control request, and
  1643. * needs to work out what to do next (and whether to pass it on to the
  1644. * gadget driver).
  1645. */
  1646. static void dwc2_hsotg_process_control(struct dwc2_hsotg *hsotg,
  1647. struct usb_ctrlrequest *ctrl)
  1648. {
  1649. struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
  1650. int ret = 0;
  1651. u32 dcfg;
  1652. dev_dbg(hsotg->dev,
  1653. "ctrl Type=%02x, Req=%02x, V=%04x, I=%04x, L=%04x\n",
  1654. ctrl->bRequestType, ctrl->bRequest, ctrl->wValue,
  1655. ctrl->wIndex, ctrl->wLength);
  1656. if (ctrl->wLength == 0) {
  1657. ep0->dir_in = 1;
  1658. hsotg->ep0_state = DWC2_EP0_STATUS_IN;
  1659. } else if (ctrl->bRequestType & USB_DIR_IN) {
  1660. ep0->dir_in = 1;
  1661. hsotg->ep0_state = DWC2_EP0_DATA_IN;
  1662. } else {
  1663. ep0->dir_in = 0;
  1664. hsotg->ep0_state = DWC2_EP0_DATA_OUT;
  1665. }
  1666. if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
  1667. switch (ctrl->bRequest) {
  1668. case USB_REQ_SET_ADDRESS:
  1669. hsotg->connected = 1;
  1670. dcfg = dwc2_readl(hsotg->regs + DCFG);
  1671. dcfg &= ~DCFG_DEVADDR_MASK;
  1672. dcfg |= (le16_to_cpu(ctrl->wValue) <<
  1673. DCFG_DEVADDR_SHIFT) & DCFG_DEVADDR_MASK;
  1674. dwc2_writel(dcfg, hsotg->regs + DCFG);
  1675. dev_info(hsotg->dev, "new address %d\r\n", ctrl->wValue);
  1676. ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
  1677. return;
  1678. case USB_REQ_GET_STATUS:
  1679. ret = dwc2_hsotg_process_req_status(hsotg, ctrl);
  1680. break;
  1681. case USB_REQ_CLEAR_FEATURE:
  1682. case USB_REQ_SET_FEATURE:
  1683. ret = dwc2_hsotg_process_req_feature(hsotg, ctrl);
  1684. break;
  1685. }
  1686. }
  1687. /* as a fallback, try delivering it to the driver to deal with */
  1688. if (ret == 0 && hsotg->driver) {
  1689. spin_unlock(&hsotg->lock);
  1690. ret = hsotg->driver->setup(&hsotg->gadget, ctrl);
  1691. spin_lock(&hsotg->lock);
  1692. if (ret < 0) {
  1693. dev_dbg(hsotg->dev, "driver->setup() ret %d\n", ret);
  1694. } else if (ret == 0 && ctrl->bRequest == USB_REQ_SET_INTERFACE
  1695. && hsotg->enumtimer_start) {
  1696. xTimerStopFromISR(hsotg->enumtimer, 0);
  1697. hsotg->enumtimer_start = 0;
  1698. }
  1699. }
  1700. /*
  1701. * the request is either unhandlable, or is not formatted correctly
  1702. * so respond with a STALL for the status stage to indicate failure.
  1703. */
  1704. if (ret < 0)
  1705. dwc2_hsotg_stall_ep0(hsotg);
  1706. }
  1707. /**
  1708. * dwc2_hsotg_complete_setup - completion of a setup transfer
  1709. * @ep: The endpoint the request was on.
  1710. * @req: The request completed.
  1711. *
  1712. * Called on completion of any requests the driver itself submitted for
  1713. * EP0 setup packets
  1714. */
  1715. static void dwc2_hsotg_complete_setup(struct usb_ep *ep,
  1716. struct usb_request *req)
  1717. {
  1718. struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
  1719. struct dwc2_hsotg *hsotg = hs_ep->parent;
  1720. if (req->status < 0) {
  1721. dev_dbg(hsotg->dev, "%s: failed %d\n", __func__, req->status);
  1722. return;
  1723. }
  1724. spin_lock(&hsotg->lock);
  1725. if (req->actual == 0)
  1726. dwc2_hsotg_enqueue_setup(hsotg);
  1727. else
  1728. dwc2_hsotg_process_control(hsotg, req->buf);
  1729. spin_unlock(&hsotg->lock);
  1730. }
  1731. /**
  1732. * dwc2_hsotg_enqueue_setup - start a request for EP0 packets
  1733. * @hsotg: The device state.
  1734. *
  1735. * Enqueue a request on EP0 if necessary to received any SETUP packets
  1736. * received from the host.
  1737. */
  1738. static void dwc2_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg)
  1739. {
  1740. struct usb_request *req = hsotg->ctrl_req;
  1741. struct dwc2_hsotg_req *hs_req = our_req(req);
  1742. int ret;
  1743. dev_dbg(hsotg->dev, "%s: queueing setup request\n", __func__);
  1744. req->zero = 0;
  1745. req->length = 8;
  1746. req->buf = hsotg->ctrl_buff;
  1747. req->complete = dwc2_hsotg_complete_setup;
  1748. #ifndef NO_GNU
  1749. if (!list_empty(&hs_req->queue)) {
  1750. dev_dbg(hsotg->dev, "%s already queued???\n", __func__);
  1751. return;
  1752. }
  1753. #else
  1754. if (!list_item_empty(&hs_req->queue)) {
  1755. dev_dbg(hsotg->dev, "%s already queued???\n", __func__);
  1756. return;
  1757. }
  1758. #endif
  1759. hsotg->eps_out[0]->dir_in = 0;
  1760. hsotg->eps_out[0]->send_zlp = 0;
  1761. hsotg->ep0_state = DWC2_EP0_SETUP;
  1762. ret = dwc2_hsotg_ep_queue(&hsotg->eps_out[0]->ep, req, GFP_ATOMIC);
  1763. if (ret < 0) {
  1764. dev_err(hsotg->dev, "%s: failed queue (%d)\n", __func__, ret);
  1765. /*
  1766. * Don't think there's much we can do other than watch the
  1767. * driver fail.
  1768. */
  1769. }
  1770. }
  1771. static void dwc2_hsotg_program_zlp(struct dwc2_hsotg *hsotg,
  1772. struct dwc2_hsotg_ep *hs_ep)
  1773. {
  1774. u32 ctrl;
  1775. u8 index = hs_ep->index;
  1776. u32 epctl_reg = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index);
  1777. u32 epsiz_reg = hs_ep->dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
  1778. if (hs_ep->dir_in) {
  1779. dev_dbg(hsotg->dev, "Sending zero-length packet on ep%d\n",
  1780. index);
  1781. } else {
  1782. dev_dbg(hsotg->dev, "Receiving zero-length packet on ep%d\n",
  1783. index);
  1784. }
  1785. if (using_desc_dma(hsotg)) {
  1786. /* Not specific buffer needed for ep0 ZLP */
  1787. dma_addr_t dma = hs_ep->desc_list_dma;
  1788. dwc2_gadget_set_ep0_desc_chain(hsotg, hs_ep);
  1789. dwc2_gadget_config_nonisoc_xfer_ddma(hs_ep, dma, 0);
  1790. } else {
  1791. dwc2_writel(DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
  1792. DXEPTSIZ_XFERSIZE(0), hsotg->regs +
  1793. epsiz_reg);
  1794. }
  1795. ctrl = dwc2_readl(hsotg->regs + epctl_reg);
  1796. ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */
  1797. ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */
  1798. ctrl |= DXEPCTL_USBACTEP;
  1799. dwc2_writel(ctrl, hsotg->regs + epctl_reg);
  1800. }
  1801. /**
  1802. * dwc2_hsotg_complete_request - complete a request given to us
  1803. * @hsotg: The device state.
  1804. * @hs_ep: The endpoint the request was on.
  1805. * @hs_req: The request to complete.
  1806. * @result: The result code (0 => Ok, otherwise errno)
  1807. *
  1808. * The given request has finished, so call the necessary completion
  1809. * if it has one and then look to see if we can start a new request
  1810. * on the endpoint.
  1811. *
  1812. * Note, expects the ep to already be locked as appropriate.
  1813. */
  1814. static void dwc2_hsotg_complete_request(struct dwc2_hsotg *hsotg,
  1815. struct dwc2_hsotg_ep *hs_ep,
  1816. struct dwc2_hsotg_req *hs_req,
  1817. int result)
  1818. {
  1819. if (!hs_req) {
  1820. dev_dbg(hsotg->dev, "%s: nothing to complete?\n", __func__);
  1821. return;
  1822. }
  1823. dev_dbg(hsotg->dev, "complete: ep %p %s, req %p, %d => %p\n",
  1824. hs_ep, hs_ep->ep.name, hs_req, result, hs_req->req.complete);
  1825. /*
  1826. * only replace the status if we've not already set an error
  1827. * from a previous transaction
  1828. */
  1829. if (hs_req->req.status == -EINPROGRESS)
  1830. hs_req->req.status = result;
  1831. if (using_dma(hsotg))
  1832. dwc2_hsotg_unmap_dma(hsotg, hs_ep, hs_req);
  1833. dwc2_hsotg_handle_unaligned_buf_complete(hsotg, hs_ep, hs_req);
  1834. hs_ep->req = NULL;
  1835. list_del_init(&hs_req->queue);
  1836. /*
  1837. * call the complete request with the locks off, just in case the
  1838. * request tries to queue more work for this endpoint.
  1839. */
  1840. if (hs_req->req.complete) {
  1841. spin_unlock(&hsotg->lock);
  1842. usb_gadget_giveback_request(&hs_ep->ep, &hs_req->req);
  1843. spin_lock(&hsotg->lock);
  1844. }
  1845. /* In DDMA don't need to proceed to starting of next ISOC request */
  1846. if (using_desc_dma(hsotg) && hs_ep->isochronous)
  1847. return;
  1848. /*
  1849. * Look to see if there is anything else to do. Note, the completion
  1850. * of the previous request may have caused a new request to be started
  1851. * so be careful when doing this.
  1852. */
  1853. if (!hs_ep->req && result >= 0)
  1854. dwc2_gadget_start_next_request(hs_ep);
  1855. }
  1856. /*
  1857. * dwc2_gadget_complete_isoc_request_ddma - complete an isoc request in DDMA
  1858. * @hs_ep: The endpoint the request was on.
  1859. *
  1860. * Get first request from the ep queue, determine descriptor on which complete
  1861. * happened. SW based on isoc_chain_num discovers which half of the descriptor
  1862. * chain is currently in use by HW, adjusts dma_address and calculates index
  1863. * of completed descriptor based on the value of DEPDMA register. Update actual
  1864. * length of request, giveback to gadget.
  1865. */
  1866. static void dwc2_gadget_complete_isoc_request_ddma(struct dwc2_hsotg_ep *hs_ep)
  1867. {
  1868. struct dwc2_hsotg *hsotg = hs_ep->parent;
  1869. struct dwc2_hsotg_req *hs_req;
  1870. struct usb_request *ureq;
  1871. int index;
  1872. dma_addr_t dma_addr;
  1873. u32 dma_reg;
  1874. u32 depdma;
  1875. u32 desc_sts;
  1876. u32 mask;
  1877. hs_req = get_ep_head(hs_ep);
  1878. if (!hs_req) {
  1879. dev_warn(hsotg->dev, "%s: ISOC EP queue empty\n", __func__);
  1880. return;
  1881. }
  1882. ureq = &hs_req->req;
  1883. dma_addr = hs_ep->desc_list_dma;
  1884. /*
  1885. * If lower half of descriptor chain is currently use by SW,
  1886. * that means higher half is being processed by HW, so shift
  1887. * DMA address to higher half of descriptor chain.
  1888. */
  1889. if (!hs_ep->isoc_chain_num)
  1890. dma_addr += sizeof(struct dwc2_dma_desc) *
  1891. (MAX_DMA_DESC_NUM_GENERIC / 2);
  1892. dma_reg = hs_ep->dir_in ? DIEPDMA(hs_ep->index) : DOEPDMA(hs_ep->index);
  1893. depdma = dwc2_readl(hsotg->regs + dma_reg);
  1894. index = (depdma - dma_addr) / sizeof(struct dwc2_dma_desc) - 1;
  1895. desc_sts = hs_ep->desc_list[index].status;
  1896. mask = hs_ep->dir_in ? DEV_DMA_ISOC_TX_NBYTES_MASK :
  1897. DEV_DMA_ISOC_RX_NBYTES_MASK;
  1898. ureq->actual = ureq->length -
  1899. ((desc_sts & mask) >> DEV_DMA_ISOC_NBYTES_SHIFT);
  1900. /* Adjust actual length for ISOC Out if length is not align of 4 */
  1901. if (!hs_ep->dir_in && ureq->length & 0x3)
  1902. ureq->actual += 4 - (ureq->length & 0x3);
  1903. dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
  1904. }
  1905. /*
  1906. * dwc2_gadget_start_next_isoc_ddma - start next isoc request, if any.
  1907. * @hs_ep: The isochronous endpoint to be re-enabled.
  1908. *
  1909. * If ep has been disabled due to last descriptor servicing (IN endpoint) or
  1910. * BNA (OUT endpoint) check the status of other half of descriptor chain that
  1911. * was under SW control till HW was busy and restart the endpoint if needed.
  1912. */
  1913. static void dwc2_gadget_start_next_isoc_ddma(struct dwc2_hsotg_ep *hs_ep)
  1914. {
  1915. struct dwc2_hsotg *hsotg = hs_ep->parent;
  1916. u32 depctl;
  1917. u32 dma_reg;
  1918. u32 ctrl;
  1919. u32 dma_addr = hs_ep->desc_list_dma;
  1920. unsigned char index = hs_ep->index;
  1921. dma_reg = hs_ep->dir_in ? DIEPDMA(index) : DOEPDMA(index);
  1922. depctl = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index);
  1923. ctrl = dwc2_readl(hsotg->regs + depctl);
  1924. /*
  1925. * EP was disabled if HW has processed last descriptor or BNA was set.
  1926. * So restart ep if SW has prepared new descriptor chain in ep_queue
  1927. * routine while HW was busy.
  1928. */
  1929. if (!(ctrl & DXEPCTL_EPENA)) {
  1930. if (!hs_ep->next_desc) {
  1931. dev_dbg(hsotg->dev, "%s: No more ISOC requests\n",
  1932. __func__);
  1933. return;
  1934. }
  1935. dma_addr += sizeof(struct dwc2_dma_desc) *
  1936. (MAX_DMA_DESC_NUM_GENERIC / 2) *
  1937. hs_ep->isoc_chain_num;
  1938. dwc2_writel(dma_addr, hsotg->regs + dma_reg);
  1939. ctrl |= DXEPCTL_EPENA | DXEPCTL_CNAK;
  1940. dwc2_writel(ctrl, hsotg->regs + depctl);
  1941. /* Switch ISOC descriptor chain number being processed by SW*/
  1942. hs_ep->isoc_chain_num = (hs_ep->isoc_chain_num ^ 1) & 0x1;
  1943. hs_ep->next_desc = 0;
  1944. dev_dbg(hsotg->dev, "%s: Restarted isochronous endpoint\n",
  1945. __func__);
  1946. }
  1947. }
  1948. /**
  1949. * dwc2_hsotg_rx_data - receive data from the FIFO for an endpoint
  1950. * @hsotg: The device state.
  1951. * @ep_idx: The endpoint index for the data
  1952. * @size: The size of data in the fifo, in bytes
  1953. *
  1954. * The FIFO status shows there is data to read from the FIFO for a given
  1955. * endpoint, so sort out whether we need to read the data into a request
  1956. * that has been made for that endpoint.
  1957. */
  1958. static void dwc2_hsotg_rx_data(struct dwc2_hsotg *hsotg, int ep_idx, int size)
  1959. {
  1960. struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[ep_idx];
  1961. struct dwc2_hsotg_req *hs_req = hs_ep->req;
  1962. #ifndef NO_GNU
  1963. void __iomem *fifo = hsotg->regs + EPFIFO(ep_idx);
  1964. #else
  1965. u32 fifo = hsotg->regs + EPFIFO(ep_idx);
  1966. #endif
  1967. int to_read;
  1968. int max_req;
  1969. int read_ptr;
  1970. if (!hs_req) {
  1971. u32 epctl = dwc2_readl(hsotg->regs + DOEPCTL(ep_idx));
  1972. int ptr;
  1973. dev_dbg(hsotg->dev,
  1974. "%s: FIFO %d bytes on ep%d but no req (DXEPCTl=0x%08x)\n",
  1975. __func__, size, ep_idx, epctl);
  1976. /* dump the data from the FIFO, we've nothing we can do */
  1977. for (ptr = 0; ptr < size; ptr += 4)
  1978. (void)dwc2_readl(fifo);
  1979. return;
  1980. }
  1981. to_read = size;
  1982. read_ptr = hs_req->req.actual;
  1983. max_req = hs_req->req.length - read_ptr;
  1984. dev_dbg(hsotg->dev, "%s ep%d: read %d/%d, done %d/%d\r\n",
  1985. __func__, ep_idx, to_read, max_req, read_ptr, hs_req->req.length);
  1986. if (to_read > max_req) {
  1987. /*
  1988. * more data appeared than we where willing
  1989. * to deal with in this request.
  1990. */
  1991. /* currently we don't deal this */
  1992. WARN_ON_ONCE(1);
  1993. }
  1994. hs_ep->total_data += to_read;
  1995. hs_req->req.actual += to_read;
  1996. to_read = DIV_ROUND_UP(to_read, 4);
  1997. /*
  1998. * note, we might over-write the buffer end by 3 bytes depending on
  1999. * alignment of the data.
  2000. */
  2001. ioread32_rep(fifo, (void*)((u32)hs_req->req.buf + read_ptr), to_read);
  2002. }
  2003. /**
  2004. * dwc2_hsotg_ep0_zlp - send/receive zero-length packet on control endpoint
  2005. * @hsotg: The device instance
  2006. * @dir_in: If IN zlp
  2007. *
  2008. * Generate a zero-length IN packet request for terminating a SETUP
  2009. * transaction.
  2010. *
  2011. * Note, since we don't write any data to the TxFIFO, then it is
  2012. * currently believed that we do not need to wait for any space in
  2013. * the TxFIFO.
  2014. */
  2015. static void dwc2_hsotg_ep0_zlp(struct dwc2_hsotg *hsotg, bool dir_in)
  2016. {
  2017. /* eps_out[0] is used in both directions */
  2018. hsotg->eps_out[0]->dir_in = dir_in;
  2019. hsotg->ep0_state = dir_in ? DWC2_EP0_STATUS_IN : DWC2_EP0_STATUS_OUT;
  2020. dwc2_hsotg_program_zlp(hsotg, hsotg->eps_out[0]);
  2021. }
  2022. static void dwc2_hsotg_change_ep_iso_parity(struct dwc2_hsotg *hsotg,
  2023. u32 epctl_reg)
  2024. {
  2025. u32 ctrl;
  2026. ctrl = dwc2_readl(hsotg->regs + epctl_reg);
  2027. if (ctrl & DXEPCTL_EOFRNUM)
  2028. ctrl |= DXEPCTL_SETEVENFR;
  2029. else
  2030. ctrl |= DXEPCTL_SETODDFR;
  2031. dwc2_writel(ctrl, hsotg->regs + epctl_reg);
  2032. }
  2033. /*
  2034. * dwc2_gadget_get_xfersize_ddma - get transferred bytes amount from desc
  2035. * @hs_ep - The endpoint on which transfer went
  2036. *
  2037. * Iterate over endpoints descriptor chain and get info on bytes remained
  2038. * in DMA descriptors after transfer has completed. Used for non isoc EPs.
  2039. */
  2040. static unsigned int dwc2_gadget_get_xfersize_ddma(struct dwc2_hsotg_ep *hs_ep)
  2041. {
  2042. struct dwc2_hsotg *hsotg = hs_ep->parent;
  2043. unsigned int bytes_rem = 0;
  2044. struct dwc2_dma_desc *desc = hs_ep->desc_list;
  2045. int i;
  2046. u32 status;
  2047. USB_UNUSED(hsotg);
  2048. if (!desc)
  2049. return 0;
  2050. for (i = 0; i < hs_ep->desc_count; ++i) {
  2051. status = desc->status;
  2052. bytes_rem += status & DEV_DMA_NBYTES_MASK;
  2053. if (status & DEV_DMA_STS_MASK)
  2054. dev_err(hsotg->dev, "descriptor %d closed with %x\n",
  2055. i, status & DEV_DMA_STS_MASK);
  2056. }
  2057. return bytes_rem;
  2058. }
  2059. /**
  2060. * dwc2_hsotg_handle_outdone - handle receiving OutDone/SetupDone from RXFIFO
  2061. * @hsotg: The device instance
  2062. * @epnum: The endpoint received from
  2063. *
  2064. * The RXFIFO has delivered an OutDone event, which means that the data
  2065. * transfer for an OUT endpoint has been completed, either by a short
  2066. * packet or by the finish of a transfer.
  2067. */
  2068. static void dwc2_hsotg_handle_outdone(struct dwc2_hsotg *hsotg, int epnum)
  2069. {
  2070. u32 epsize = dwc2_readl(hsotg->regs + DOEPTSIZ(epnum));
  2071. struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[epnum];
  2072. struct dwc2_hsotg_req *hs_req = hs_ep->req;
  2073. struct usb_request *req = &hs_req->req;
  2074. unsigned int size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
  2075. int result = 0;
  2076. if (!hs_req) {
  2077. dev_dbg(hsotg->dev, "%s: no request active\n", __func__);
  2078. return;
  2079. }
  2080. if (epnum == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_OUT) {
  2081. dev_dbg(hsotg->dev, "zlp packet received\n");
  2082. dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
  2083. dwc2_hsotg_enqueue_setup(hsotg);
  2084. return;
  2085. }
  2086. if (using_desc_dma(hsotg))
  2087. size_left = dwc2_gadget_get_xfersize_ddma(hs_ep);
  2088. if (using_dma(hsotg)) {
  2089. unsigned int size_done;
  2090. /*
  2091. * Calculate the size of the transfer by checking how much
  2092. * is left in the endpoint size register and then working it
  2093. * out from the amount we loaded for the transfer.
  2094. *
  2095. * We need to do this as DMA pointers are always 32bit aligned
  2096. * so may overshoot/undershoot the transfer.
  2097. */
  2098. size_done = hs_ep->size_loaded - size_left;
  2099. size_done += hs_ep->last_load;
  2100. req->actual = size_done;
  2101. }
  2102. /* if there is more request to do, schedule new transfer */
  2103. if (req->actual < req->length && size_left == 0) {
  2104. dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, true);
  2105. return;
  2106. }
  2107. if (req->actual < req->length && req->short_not_ok) {
  2108. dev_dbg(hsotg->dev, "%s: got %d/%d (short not ok) => error\n",
  2109. __func__, req->actual, req->length);
  2110. /*
  2111. * todo - what should we return here? there's no one else
  2112. * even bothering to check the status.
  2113. */
  2114. }
  2115. /* DDMA IN status phase will start from StsPhseRcvd interrupt */
  2116. if (!using_desc_dma(hsotg) && epnum == 0 &&
  2117. hsotg->ep0_state == DWC2_EP0_DATA_OUT) {
  2118. /* Move to STATUS IN */
  2119. dwc2_hsotg_ep0_zlp(hsotg, true);
  2120. return;
  2121. }
  2122. /*
  2123. * Slave mode OUT transfers do not go through XferComplete so
  2124. * adjust the ISOC parity here.
  2125. */
  2126. if (!using_dma(hsotg)) {
  2127. if (hs_ep->isochronous && hs_ep->interval == 1)
  2128. dwc2_hsotg_change_ep_iso_parity(hsotg, DOEPCTL(epnum));
  2129. else if (hs_ep->isochronous && hs_ep->interval > 1)
  2130. dwc2_gadget_incr_frame_num(hs_ep);
  2131. }
  2132. dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, result);
  2133. }
  2134. /**
  2135. * dwc2_hsotg_handle_rx - RX FIFO has data
  2136. * @hsotg: The device instance
  2137. *
  2138. * The IRQ handler has detected that the RX FIFO has some data in it
  2139. * that requires processing, so find out what is in there and do the
  2140. * appropriate read.
  2141. *
  2142. * The RXFIFO is a true FIFO, the packets coming out are still in packet
  2143. * chunks, so if you have x packets received on an endpoint you'll get x
  2144. * FIFO events delivered, each with a packet's worth of data in it.
  2145. *
  2146. * When using DMA, we should not be processing events from the RXFIFO
  2147. * as the actual data should be sent to the memory directly and we turn
  2148. * on the completion interrupts to get notifications of transfer completion.
  2149. */
  2150. static void dwc2_hsotg_handle_rx(struct dwc2_hsotg *hsotg)
  2151. {
  2152. u32 grxstsr = dwc2_readl(hsotg->regs + GRXSTSP);
  2153. u32 epnum, status, size;
  2154. WARN_ON(using_dma(hsotg));
  2155. epnum = grxstsr & GRXSTS_EPNUM_MASK;
  2156. if (epnum >= hsotg->num_of_eps) {
  2157. dev_err(hsotg->dev, "Error epnum:%d\r\n", epnum);
  2158. return;
  2159. }
  2160. status = grxstsr & GRXSTS_PKTSTS_MASK;
  2161. size = grxstsr & GRXSTS_BYTECNT_MASK;
  2162. size >>= GRXSTS_BYTECNT_SHIFT;
  2163. dev_dbg(hsotg->dev, "%s: GRXSTSP=0x%08x (%d@%d)\r\n",
  2164. __func__, grxstsr, size, epnum);
  2165. switch ((status & GRXSTS_PKTSTS_MASK) >> GRXSTS_PKTSTS_SHIFT) {
  2166. case GRXSTS_PKTSTS_GLOBALOUTNAK:
  2167. dev_dbg(hsotg->dev, "GLOBALOUTNAK\n");
  2168. break;
  2169. case GRXSTS_PKTSTS_OUTDONE:
  2170. dev_dbg(hsotg->dev, "OutDone (Frame=0x%08x)\n",
  2171. dwc2_hsotg_read_frameno(hsotg));
  2172. if (!using_dma(hsotg))
  2173. dwc2_hsotg_handle_outdone(hsotg, epnum);
  2174. break;
  2175. case GRXSTS_PKTSTS_SETUPDONE:
  2176. dev_dbg(hsotg->dev,
  2177. "SetupDone (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
  2178. dwc2_hsotg_read_frameno(hsotg),
  2179. dwc2_readl(hsotg->regs + DOEPCTL(0)));
  2180. /*
  2181. * Call dwc2_hsotg_handle_outdone here if it was not called from
  2182. * GRXSTS_PKTSTS_OUTDONE. That is, if the core didn't
  2183. * generate GRXSTS_PKTSTS_OUTDONE for setup packet.
  2184. */
  2185. if (hsotg->ep0_state == DWC2_EP0_SETUP)
  2186. dwc2_hsotg_handle_outdone(hsotg, epnum);
  2187. break;
  2188. case GRXSTS_PKTSTS_OUTRX:
  2189. dwc2_hsotg_rx_data(hsotg, epnum, size);
  2190. break;
  2191. case GRXSTS_PKTSTS_SETUPRX:
  2192. dev_dbg(hsotg->dev,
  2193. "SetupRX (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
  2194. dwc2_hsotg_read_frameno(hsotg),
  2195. dwc2_readl(hsotg->regs + DOEPCTL(0)));
  2196. WARN_ON(hsotg->ep0_state != DWC2_EP0_SETUP);
  2197. dwc2_hsotg_rx_data(hsotg, epnum, size);
  2198. break;
  2199. default:
  2200. dev_warn(hsotg->dev, "%s: unknown status %08x\n",
  2201. __func__, grxstsr);
  2202. dwc2_hsotg_dump(hsotg);
  2203. break;
  2204. }
  2205. }
  2206. /**
  2207. * dwc2_hsotg_ep0_mps - turn max packet size into register setting
  2208. * @mps: The maximum packet size in bytes.
  2209. */
  2210. static u32 dwc2_hsotg_ep0_mps(unsigned int mps)
  2211. {
  2212. switch (mps) {
  2213. case 64:
  2214. return D0EPCTL_MPS_64;
  2215. case 32:
  2216. return D0EPCTL_MPS_32;
  2217. case 16:
  2218. return D0EPCTL_MPS_16;
  2219. case 8:
  2220. return D0EPCTL_MPS_8;
  2221. }
  2222. /* bad max packet size, warn and return invalid result */
  2223. WARN_ON(1);
  2224. return (u32)-1;
  2225. }
  2226. /**
  2227. * dwc2_hsotg_set_ep_maxpacket - set endpoint's max-packet field
  2228. * @hsotg: The driver state.
  2229. * @ep: The index number of the endpoint
  2230. * @mps: The maximum packet size in bytes
  2231. * @mc: The multicount value
  2232. *
  2233. * Configure the maximum packet size for the given endpoint, updating
  2234. * the hardware control registers to reflect this.
  2235. */
  2236. static void dwc2_hsotg_set_ep_maxpacket(struct dwc2_hsotg *hsotg,
  2237. unsigned int ep, unsigned int mps,
  2238. unsigned int mc, unsigned int dir_in)
  2239. {
  2240. struct dwc2_hsotg_ep *hs_ep;
  2241. #ifndef NO_GNU
  2242. void __iomem *regs = hsotg->regs;
  2243. #else
  2244. u32 regs = hsotg->regs;
  2245. #endif
  2246. u32 reg;
  2247. hs_ep = index_to_ep(hsotg, ep, dir_in);
  2248. if (!hs_ep)
  2249. return;
  2250. if (ep == 0) {
  2251. u32 mps_bytes = mps;
  2252. /* EP0 is a special case */
  2253. mps = dwc2_hsotg_ep0_mps(mps_bytes);
  2254. if (mps > 3)
  2255. goto bad_mps;
  2256. hs_ep->ep.maxpacket = mps_bytes;
  2257. hs_ep->mc = 1;
  2258. } else {
  2259. if (mps > 1024)
  2260. goto bad_mps;
  2261. hs_ep->mc = mc;
  2262. if (mc > 3)
  2263. goto bad_mps;
  2264. hs_ep->ep.maxpacket = mps;
  2265. }
  2266. if (dir_in) {
  2267. reg = dwc2_readl(regs + DIEPCTL(ep));
  2268. reg &= ~DXEPCTL_MPS_MASK;
  2269. reg |= mps;
  2270. dwc2_writel(reg, regs + DIEPCTL(ep));
  2271. } else {
  2272. reg = dwc2_readl(regs + DOEPCTL(ep));
  2273. reg &= ~DXEPCTL_MPS_MASK;
  2274. reg |= mps;
  2275. dwc2_writel(reg, regs + DOEPCTL(ep));
  2276. }
  2277. return;
  2278. bad_mps:
  2279. dev_err(hsotg->dev, "ep%d: bad mps of %d\n", ep, mps);
  2280. }
  2281. /**
  2282. * dwc2_hsotg_txfifo_flush - flush Tx FIFO
  2283. * @hsotg: The driver state
  2284. * @idx: The index for the endpoint (0..15)
  2285. */
  2286. static void dwc2_hsotg_txfifo_flush(struct dwc2_hsotg *hsotg, unsigned int idx)
  2287. {
  2288. int timeout;
  2289. int val;
  2290. dwc2_writel(GRSTCTL_TXFNUM(idx) | GRSTCTL_TXFFLSH,
  2291. hsotg->regs + GRSTCTL);
  2292. /* wait until the fifo is flushed */
  2293. timeout = 100;
  2294. while (1) {
  2295. val = dwc2_readl(hsotg->regs + GRSTCTL);
  2296. if ((val & (GRSTCTL_TXFFLSH)) == 0)
  2297. break;
  2298. if (--timeout == 0) {
  2299. dev_err(hsotg->dev,
  2300. "%s: timeout flushing fifo (GRSTCTL=%08x)\n",
  2301. __func__, val);
  2302. break;
  2303. }
  2304. udelay(1);
  2305. }
  2306. }
  2307. /**
  2308. * dwc2_hsotg_trytx - check to see if anything needs transmitting
  2309. * @hsotg: The driver state
  2310. * @hs_ep: The driver endpoint to check.
  2311. *
  2312. * Check to see if there is a request that has data to send, and if so
  2313. * make an attempt to write data into the FIFO.
  2314. */
  2315. static int dwc2_hsotg_trytx(struct dwc2_hsotg *hsotg,
  2316. struct dwc2_hsotg_ep *hs_ep)
  2317. {
  2318. struct dwc2_hsotg_req *hs_req = hs_ep->req;
  2319. if (!hs_ep->dir_in || !hs_req) {
  2320. /**
  2321. * if request is not enqueued, we disable interrupts
  2322. * for endpoints, excepting ep0
  2323. */
  2324. if (hs_ep->index != 0)
  2325. dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index,
  2326. hs_ep->dir_in, 0);
  2327. return 0;
  2328. }
  2329. if (hs_req->req.actual < hs_req->req.length) {
  2330. dev_dbg(hsotg->dev, "trying to write more for ep%d\n",
  2331. hs_ep->index);
  2332. return dwc2_hsotg_write_fifo(hsotg, hs_ep, hs_req);
  2333. }
  2334. return 0;
  2335. }
  2336. /**
  2337. * dwc2_hsotg_complete_in - complete IN transfer
  2338. * @hsotg: The device state.
  2339. * @hs_ep: The endpoint that has just completed.
  2340. *
  2341. * An IN transfer has been completed, update the transfer's state and then
  2342. * call the relevant completion routines.
  2343. */
  2344. static void dwc2_hsotg_complete_in(struct dwc2_hsotg *hsotg,
  2345. struct dwc2_hsotg_ep *hs_ep)
  2346. {
  2347. struct dwc2_hsotg_req *hs_req = hs_ep->req;
  2348. u32 epsize = dwc2_readl(hsotg->regs + DIEPTSIZ(hs_ep->index));
  2349. int size_left, size_done;
  2350. if (!hs_req) {
  2351. dev_dbg(hsotg->dev, "XferCompl but no req\n");
  2352. return;
  2353. }
  2354. /* Finish ZLP handling for IN EP0 transactions */
  2355. if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_IN) {
  2356. dev_dbg(hsotg->dev, "zlp packet sent\n");
  2357. /*
  2358. * While send zlp for DWC2_EP0_STATUS_IN EP direction was
  2359. * changed to IN. Change back to complete OUT transfer request
  2360. */
  2361. hs_ep->dir_in = 0;
  2362. dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
  2363. if (hsotg->test_mode) {
  2364. int ret;
  2365. ret = dwc2_hsotg_set_test_mode(hsotg, hsotg->test_mode);
  2366. if (ret < 0) {
  2367. dev_dbg(hsotg->dev, "Invalid Test #%d\n",
  2368. hsotg->test_mode);
  2369. dwc2_hsotg_stall_ep0(hsotg);
  2370. return;
  2371. }
  2372. }
  2373. dwc2_hsotg_enqueue_setup(hsotg);
  2374. return;
  2375. }
  2376. /*
  2377. * Calculate the size of the transfer by checking how much is left
  2378. * in the endpoint size register and then working it out from
  2379. * the amount we loaded for the transfer.
  2380. *
  2381. * We do this even for DMA, as the transfer may have incremented
  2382. * past the end of the buffer (DMA transfers are always 32bit
  2383. * aligned).
  2384. */
  2385. if (using_desc_dma(hsotg)) {
  2386. size_left = dwc2_gadget_get_xfersize_ddma(hs_ep);
  2387. if (size_left < 0)
  2388. dev_err(hsotg->dev, "error parsing DDMA results %d\n",
  2389. size_left);
  2390. } else {
  2391. size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
  2392. }
  2393. size_done = hs_ep->size_loaded - size_left;
  2394. size_done += hs_ep->last_load;
  2395. if (hs_req->req.actual != size_done)
  2396. dev_dbg(hsotg->dev, "%s: adjusting size done %d => %d\n",
  2397. __func__, hs_req->req.actual, size_done);
  2398. hs_req->req.actual = size_done;
  2399. dev_dbg(hsotg->dev, "req->length:%d req->actual:%d req->zero:%d\n",
  2400. hs_req->req.length, hs_req->req.actual, hs_req->req.zero);
  2401. if (!size_left && hs_req->req.actual < hs_req->req.length) {
  2402. dev_dbg(hsotg->dev, "%s trying more for req...\n", __func__);
  2403. dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, true);
  2404. return;
  2405. }
  2406. /* Zlp for all endpoints, for ep0 only in DATA IN stage */
  2407. if (hs_ep->send_zlp) {
  2408. dwc2_hsotg_program_zlp(hsotg, hs_ep);
  2409. hs_ep->send_zlp = 0;
  2410. /* transfer will be completed on next complete interrupt */
  2411. return;
  2412. }
  2413. if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_DATA_IN) {
  2414. /* Move to STATUS OUT */
  2415. dwc2_hsotg_ep0_zlp(hsotg, false);
  2416. return;
  2417. }
  2418. dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
  2419. }
  2420. /**
  2421. * dwc2_gadget_read_ep_interrupts - reads interrupts for given ep
  2422. * @hsotg: The device state.
  2423. * @idx: Index of ep.
  2424. * @dir_in: Endpoint direction 1-in 0-out.
  2425. *
  2426. * Reads for endpoint with given index and direction, by masking
  2427. * epint_reg with coresponding mask.
  2428. */
  2429. static u32 dwc2_gadget_read_ep_interrupts(struct dwc2_hsotg *hsotg,
  2430. unsigned int idx, int dir_in)
  2431. {
  2432. u32 epmsk_reg = dir_in ? DIEPMSK : DOEPMSK;
  2433. u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx);
  2434. u32 ints;
  2435. u32 mask;
  2436. u32 diepempmsk;
  2437. mask = dwc2_readl(hsotg->regs + epmsk_reg);
  2438. diepempmsk = dwc2_readl(hsotg->regs + DIEPEMPMSK);
  2439. mask |= ((diepempmsk >> idx) & 0x1) ? DIEPMSK_TXFIFOEMPTY : 0;
  2440. mask |= DXEPINT_SETUP_RCVD;
  2441. ints = dwc2_readl(hsotg->regs + epint_reg);
  2442. ints &= mask;
  2443. return ints;
  2444. }
  2445. /**
  2446. * dwc2_gadget_handle_ep_disabled - handle DXEPINT_EPDISBLD
  2447. * @hs_ep: The endpoint on which interrupt is asserted.
  2448. *
  2449. * This interrupt indicates that the endpoint has been disabled per the
  2450. * application's request.
  2451. *
  2452. * For IN endpoints flushes txfifo, in case of BULK clears DCTL_CGNPINNAK,
  2453. * in case of ISOC completes current request.
  2454. *
  2455. * For ISOC-OUT endpoints completes expired requests. If there is remaining
  2456. * request starts it.
  2457. */
  2458. static void dwc2_gadget_handle_ep_disabled(struct dwc2_hsotg_ep *hs_ep)
  2459. {
  2460. struct dwc2_hsotg *hsotg = hs_ep->parent;
  2461. struct dwc2_hsotg_req *hs_req;
  2462. unsigned char idx = hs_ep->index;
  2463. int dir_in = hs_ep->dir_in;
  2464. u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx);
  2465. int dctl = dwc2_readl(hsotg->regs + DCTL);
  2466. dev_dbg(hsotg->dev, "%s: EPDisbld\n", __func__);
  2467. if (dir_in) {
  2468. int epctl = dwc2_readl(hsotg->regs + epctl_reg);
  2469. dwc2_hsotg_txfifo_flush(hsotg, hs_ep->fifo_index);
  2470. if (hs_ep->isochronous) {
  2471. dwc2_hsotg_complete_in(hsotg, hs_ep);
  2472. return;
  2473. }
  2474. if ((epctl & DXEPCTL_STALL) && (epctl & DXEPCTL_EPTYPE_BULK)) {
  2475. int dctl = dwc2_readl(hsotg->regs + DCTL);
  2476. dctl |= DCTL_CGNPINNAK;
  2477. dwc2_writel(dctl, hsotg->regs + DCTL);
  2478. }
  2479. return;
  2480. }
  2481. if (dctl & DCTL_GOUTNAKSTS) {
  2482. dctl |= DCTL_CGOUTNAK;
  2483. dwc2_writel(dctl, hsotg->regs + DCTL);
  2484. }
  2485. if (!hs_ep->isochronous)
  2486. return;
  2487. if (list_empty(&hs_ep->queue)) {
  2488. dev_dbg(hsotg->dev, "%s: complete_ep 0x%p, ep->queue empty!\n",
  2489. __func__, hs_ep);
  2490. return;
  2491. }
  2492. do {
  2493. hs_req = get_ep_head(hs_ep);
  2494. if (hs_req)
  2495. dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req,
  2496. -ENODATA);
  2497. dwc2_gadget_incr_frame_num(hs_ep);
  2498. } while (dwc2_gadget_target_frame_elapsed(hs_ep));
  2499. dwc2_gadget_start_next_request(hs_ep);
  2500. }
  2501. /**
  2502. * dwc2_gadget_handle_out_token_ep_disabled - handle DXEPINT_OUTTKNEPDIS
  2503. * @hs_ep: The endpoint on which interrupt is asserted.
  2504. *
  2505. * This is starting point for ISOC-OUT transfer, synchronization done with
  2506. * first out token received from host while corresponding EP is disabled.
  2507. *
  2508. * Device does not know initial frame in which out token will come. For this
  2509. * HW generates OUTTKNEPDIS - out token is received while EP is disabled. Upon
  2510. * getting this interrupt SW starts calculation for next transfer frame.
  2511. */
  2512. static void dwc2_gadget_handle_out_token_ep_disabled(struct dwc2_hsotg_ep *ep)
  2513. {
  2514. struct dwc2_hsotg *hsotg = ep->parent;
  2515. int dir_in = ep->dir_in;
  2516. u32 doepmsk;
  2517. u32 tmp;
  2518. if (dir_in || !ep->isochronous)
  2519. return;
  2520. /*
  2521. * Store frame in which irq was asserted here, as
  2522. * it can change while completing request below.
  2523. */
  2524. tmp = dwc2_hsotg_read_frameno(hsotg);
  2525. dwc2_hsotg_complete_request(hsotg, ep, get_ep_head(ep), -ENODATA);
  2526. if (using_desc_dma(hsotg)) {
  2527. if (ep->target_frame == TARGET_FRAME_INITIAL) {
  2528. /* Start first ISO Out */
  2529. ep->target_frame = tmp;
  2530. dwc2_gadget_start_isoc_ddma(ep);
  2531. }
  2532. return;
  2533. }
  2534. if (ep->interval > 1 &&
  2535. ep->target_frame == TARGET_FRAME_INITIAL) {
  2536. u32 dsts;
  2537. u32 ctrl;
  2538. dsts = dwc2_readl(hsotg->regs + DSTS);
  2539. USB_UNUSED(dsts);
  2540. ep->target_frame = dwc2_hsotg_read_frameno(hsotg);
  2541. dwc2_gadget_incr_frame_num(ep);
  2542. ctrl = dwc2_readl(hsotg->regs + DOEPCTL(ep->index));
  2543. if (ep->target_frame & 0x1)
  2544. ctrl |= DXEPCTL_SETODDFR;
  2545. else
  2546. ctrl |= DXEPCTL_SETEVENFR;
  2547. dwc2_writel(ctrl, hsotg->regs + DOEPCTL(ep->index));
  2548. }
  2549. dwc2_gadget_start_next_request(ep);
  2550. doepmsk = dwc2_readl(hsotg->regs + DOEPMSK);
  2551. doepmsk &= ~DOEPMSK_OUTTKNEPDISMSK;
  2552. dwc2_writel(doepmsk, hsotg->regs + DOEPMSK);
  2553. }
  2554. /**
  2555. * dwc2_gadget_handle_nak - handle NAK interrupt
  2556. * @hs_ep: The endpoint on which interrupt is asserted.
  2557. *
  2558. * This is starting point for ISOC-IN transfer, synchronization done with
  2559. * first IN token received from host while corresponding EP is disabled.
  2560. *
  2561. * Device does not know when first one token will arrive from host. On first
  2562. * token arrival HW generates 2 interrupts: 'in token received while FIFO empty'
  2563. * and 'NAK'. NAK interrupt for ISOC-IN means that token has arrived and ZLP was
  2564. * sent in response to that as there was no data in FIFO. SW is basing on this
  2565. * interrupt to obtain frame in which token has come and then based on the
  2566. * interval calculates next frame for transfer.
  2567. */
  2568. static void dwc2_gadget_handle_nak(struct dwc2_hsotg_ep *hs_ep)
  2569. {
  2570. struct dwc2_hsotg *hsotg = hs_ep->parent;
  2571. int dir_in = hs_ep->dir_in;
  2572. if (!dir_in || !hs_ep->isochronous)
  2573. return;
  2574. if (hs_ep->target_frame == TARGET_FRAME_INITIAL) {
  2575. hs_ep->target_frame = dwc2_hsotg_read_frameno(hsotg);
  2576. if (using_desc_dma(hsotg)) {
  2577. dwc2_gadget_start_isoc_ddma(hs_ep);
  2578. return;
  2579. }
  2580. if (hs_ep->interval > 1) {
  2581. u32 ctrl = dwc2_readl(hsotg->regs +
  2582. DIEPCTL(hs_ep->index));
  2583. if (hs_ep->target_frame & 0x1)
  2584. ctrl |= DXEPCTL_SETODDFR;
  2585. else
  2586. ctrl |= DXEPCTL_SETEVENFR;
  2587. dwc2_writel(ctrl, hsotg->regs + DIEPCTL(hs_ep->index));
  2588. }
  2589. dwc2_hsotg_complete_request(hsotg, hs_ep,
  2590. get_ep_head(hs_ep), 0);
  2591. }
  2592. dwc2_gadget_incr_frame_num(hs_ep);
  2593. }
  2594. /**
  2595. * dwc2_hsotg_epint - handle an in/out endpoint interrupt
  2596. * @hsotg: The driver state
  2597. * @idx: The index for the endpoint (0..15)
  2598. * @dir_in: Set if this is an IN endpoint
  2599. *
  2600. * Process and clear any interrupt pending for an individual endpoint
  2601. */
  2602. static void dwc2_hsotg_epint(struct dwc2_hsotg *hsotg, unsigned int idx,
  2603. int dir_in)
  2604. {
  2605. struct dwc2_hsotg_ep *hs_ep = index_to_ep(hsotg, idx, dir_in);
  2606. u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx);
  2607. u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx);
  2608. u32 epsiz_reg = dir_in ? DIEPTSIZ(idx) : DOEPTSIZ(idx);
  2609. u32 ints;
  2610. u32 ctrl;
  2611. USB_UNUSED(epsiz_reg);
  2612. ints = dwc2_gadget_read_ep_interrupts(hsotg, idx, dir_in);
  2613. ctrl = dwc2_readl(hsotg->regs + epctl_reg);
  2614. USB_UNUSED(ctrl);
  2615. /* Clear endpoint interrupts */
  2616. dwc2_writel(ints, hsotg->regs + epint_reg);
  2617. if (!hs_ep) {
  2618. dev_err(hsotg->dev, "%s:Interrupt for unconfigured ep%d(%s)\n",
  2619. __func__, idx, dir_in ? "in" : "out");
  2620. return;
  2621. }
  2622. dev_dbg(hsotg->dev, "%s: ep%d(%s) DxEPINT=0x%08x\r\n",
  2623. __func__, idx, dir_in ? "in" : "out", ints);
  2624. /* Don't process XferCompl interrupt if it is a setup packet */
  2625. if (idx == 0 && (ints & (DXEPINT_SETUP | DXEPINT_SETUP_RCVD)))
  2626. ints &= ~DXEPINT_XFERCOMPL;
  2627. /*
  2628. * Don't process XferCompl interrupt in DDMA if EP0 is still in SETUP
  2629. * stage and xfercomplete was generated without SETUP phase done
  2630. * interrupt. SW should parse received setup packet only after host's
  2631. * exit from setup phase of control transfer.
  2632. */
  2633. if (using_desc_dma(hsotg) && idx == 0 && !hs_ep->dir_in &&
  2634. hsotg->ep0_state == DWC2_EP0_SETUP && !(ints & DXEPINT_SETUP))
  2635. ints &= ~DXEPINT_XFERCOMPL;
  2636. if (ints & DXEPINT_XFERCOMPL) {
  2637. dev_dbg(hsotg->dev,
  2638. "%s: XferCompl: DxEPCTL=0x%08x, DXEPTSIZ=%08x\r\n",
  2639. __func__, dwc2_readl(hsotg->regs + epctl_reg),
  2640. dwc2_readl(hsotg->regs + epsiz_reg));
  2641. /* In DDMA handle isochronous requests separately */
  2642. if (using_desc_dma(hsotg) && hs_ep->isochronous) {
  2643. dwc2_gadget_complete_isoc_request_ddma(hs_ep);
  2644. /* Try to start next isoc request */
  2645. dwc2_gadget_start_next_isoc_ddma(hs_ep);
  2646. } else if (dir_in) {
  2647. /*
  2648. * We get OutDone from the FIFO, so we only
  2649. * need to look at completing IN requests here
  2650. * if operating slave mode
  2651. */
  2652. if (hs_ep->isochronous && hs_ep->interval > 1)
  2653. dwc2_gadget_incr_frame_num(hs_ep);
  2654. dwc2_hsotg_complete_in(hsotg, hs_ep);
  2655. if (ints & DXEPINT_NAKINTRPT)
  2656. ints &= ~DXEPINT_NAKINTRPT;
  2657. if (idx == 0 && !hs_ep->req)
  2658. dwc2_hsotg_enqueue_setup(hsotg);
  2659. } else if (using_dma(hsotg)) {
  2660. /*
  2661. * We're using DMA, we need to fire an OutDone here
  2662. * as we ignore the RXFIFO.
  2663. */
  2664. if (hs_ep->isochronous && hs_ep->interval > 1)
  2665. dwc2_gadget_incr_frame_num(hs_ep);
  2666. dwc2_hsotg_handle_outdone(hsotg, idx);
  2667. }
  2668. }
  2669. if (ints & DXEPINT_EPDISBLD)
  2670. dwc2_gadget_handle_ep_disabled(hs_ep);
  2671. if (ints & DXEPINT_OUTTKNEPDIS)
  2672. dwc2_gadget_handle_out_token_ep_disabled(hs_ep);
  2673. if (ints & DXEPINT_NAKINTRPT)
  2674. dwc2_gadget_handle_nak(hs_ep);
  2675. if (ints & DXEPINT_AHBERR)
  2676. dev_dbg(hsotg->dev, "%s: AHBErr\n", __func__);
  2677. if (ints & DXEPINT_SETUP) { /* Setup or Timeout */
  2678. dev_dbg(hsotg->dev, "%s: Setup/Timeout\n", __func__);
  2679. if (using_dma(hsotg) && idx == 0) {
  2680. /*
  2681. * this is the notification we've received a
  2682. * setup packet. In non-DMA mode we'd get this
  2683. * from the RXFIFO, instead we need to process
  2684. * the setup here.
  2685. */
  2686. if (dir_in) {
  2687. WARN_ON_ONCE(1);
  2688. } else
  2689. dwc2_hsotg_handle_outdone(hsotg, 0);
  2690. }
  2691. }
  2692. if (ints & DXEPINT_STSPHSERCVD) {
  2693. dev_dbg(hsotg->dev, "%s: StsPhseRcvd\n", __func__);
  2694. /* Move to STATUS IN for DDMA */
  2695. if (using_desc_dma(hsotg))
  2696. dwc2_hsotg_ep0_zlp(hsotg, true);
  2697. }
  2698. if (ints & DXEPINT_BACK2BACKSETUP)
  2699. dev_dbg(hsotg->dev, "%s: B2BSetup/INEPNakEff\n", __func__);
  2700. if (ints & DXEPINT_BNAINTR) {
  2701. dev_dbg(hsotg->dev, "%s: BNA interrupt\n", __func__);
  2702. /*
  2703. * Try to start next isoc request, if any.
  2704. * Sometimes the endpoint remains enabled after BNA interrupt
  2705. * assertion, which is not expected, hence we can enter here
  2706. * couple of times.
  2707. */
  2708. if (hs_ep->isochronous)
  2709. dwc2_gadget_start_next_isoc_ddma(hs_ep);
  2710. }
  2711. if (dir_in && !hs_ep->isochronous) {
  2712. /* not sure if this is important, but we'll clear it anyway */
  2713. if (ints & DXEPINT_INTKNTXFEMP) {
  2714. dev_dbg(hsotg->dev, "%s: ep%d: INTknTXFEmpMsk\n",
  2715. __func__, idx);
  2716. }
  2717. /* this probably means something bad is happening */
  2718. if (ints & DXEPINT_INTKNEPMIS) {
  2719. dev_warn(hsotg->dev, "%s: ep%d: INTknEP\n",
  2720. __func__, idx);
  2721. }
  2722. /* FIFO has space or is empty (see GAHBCFG) */
  2723. if (hsotg->dedicated_fifos &&
  2724. ints & DXEPINT_TXFEMP) {
  2725. dev_dbg(hsotg->dev, "%s: ep%d: TxFIFOEmpty\n",
  2726. __func__, idx);
  2727. if (!using_dma(hsotg))
  2728. dwc2_hsotg_trytx(hsotg, hs_ep);
  2729. }
  2730. }
  2731. }
  2732. static const char *const speed_names[] = {
  2733. [USB_SPEED_UNKNOWN] = "UNKNOWN",
  2734. [USB_SPEED_LOW] = "low-speed",
  2735. [USB_SPEED_FULL] = "full-speed",
  2736. [USB_SPEED_HIGH] = "high-speed",
  2737. [USB_SPEED_WIRELESS] = "wireless",
  2738. [USB_SPEED_SUPER] = "super-speed",
  2739. //[USB_SPEED_SUPER_PLUS] = "super-speed-plus",
  2740. };
  2741. const char *usb_speed_string(enum usb_device_speed speed)
  2742. {
  2743. if (/* speed < 0 || */speed >= ARRAY_SIZE(speed_names))
  2744. speed = USB_SPEED_UNKNOWN;
  2745. return speed_names[speed];
  2746. }
  2747. /**
  2748. * dwc2_hsotg_irq_enumdone - Handle EnumDone interrupt (enumeration done)
  2749. * @hsotg: The device state.
  2750. *
  2751. * Handle updating the device settings after the enumeration phase has
  2752. * been completed.
  2753. */
  2754. #include "sysctl.h"
  2755. static void prvEnumTimerCallback( TimerHandle_t xTimerHandle )
  2756. {
  2757. struct dwc2_hsotg *hsotg = (struct dwc2_hsotg *)pvTimerGetTimerID(xTimerHandle);
  2758. TRACE_INFO("prvEnumTimerCallback\r\n");
  2759. portENTER_CRITICAL();
  2760. hsotg->enumtimer_start = 0;
  2761. hsotg->params.phy_type = DWC2_PHY_TYPE_PARAM_FS;
  2762. hsotg->params.speed = DWC2_SPEED_PARAM_FULL;
  2763. portEXIT_CRITICAL();
  2764. vSysctlConfigure(SYS_ANA_CFG, 4, 3, 0); //set usb host(bit[5]]=0)
  2765. vSysctlConfigure(SYS_SOFTRESET_CTL1, 22, 1, 0); //usb phy softreset
  2766. vSysctlConfigure(SYS_SOFTRESET_CTL, 3, 1, 0); //usb softreset.
  2767. vSysctlConfigure(SYS_SOFTRESET_CTL1, 5, 1, 0); //usb utmi softreset(usb phy interface).
  2768. struct wq_msg *pmsg = &hsotg->xmsg;
  2769. pmsg->id = OTG_WQ_MSG_DEV;
  2770. pmsg->delay = 0;
  2771. xQueueSend(hsotg->wq_otg, (void*)pmsg, 0);
  2772. }
  2773. static void dwc2_hsotg_irq_enumdone(struct dwc2_hsotg *hsotg)
  2774. {
  2775. u32 dsts = dwc2_readl(hsotg->regs + DSTS);
  2776. int ep0_mps = 0, ep_mps = 8;
  2777. /*
  2778. * This should signal the finish of the enumeration phase
  2779. * of the USB handshaking, so we should now know what rate
  2780. * we connected at.
  2781. */
  2782. dev_dbg(hsotg->dev, "EnumDone (DSTS=0x%08x)\n", dsts);
  2783. /*
  2784. * note, since we're limited by the size of transfer on EP0, and
  2785. * it seems IN transfers must be a even number of packets we do
  2786. * not advertise a 64byte MPS on EP0.
  2787. */
  2788. /* catch both EnumSpd_FS and EnumSpd_FS48 */
  2789. switch ((dsts & DSTS_ENUMSPD_MASK) >> DSTS_ENUMSPD_SHIFT) {
  2790. case DSTS_ENUMSPD_FS:
  2791. case DSTS_ENUMSPD_FS48:
  2792. hsotg->gadget.speed = USB_SPEED_FULL;
  2793. ep0_mps = EP0_MPS_LIMIT;
  2794. ep_mps = 1023;
  2795. if (!hsotg->enumtimer_start) {
  2796. xTimerStartFromISR(hsotg->enumtimer, 0);
  2797. hsotg->enumtimer_start = 1;
  2798. }
  2799. break;
  2800. case DSTS_ENUMSPD_HS:
  2801. hsotg->gadget.speed = USB_SPEED_HIGH;
  2802. ep0_mps = EP0_MPS_LIMIT;
  2803. ep_mps = 1024;
  2804. break;
  2805. case DSTS_ENUMSPD_LS:
  2806. hsotg->gadget.speed = USB_SPEED_LOW;
  2807. ep0_mps = 8;
  2808. ep_mps = 8;
  2809. /*
  2810. * note, we don't actually support LS in this driver at the
  2811. * moment, and the documentation seems to imply that it isn't
  2812. * supported by the PHYs on some of the devices.
  2813. */
  2814. break;
  2815. }
  2816. dev_info(hsotg->dev, "new device is %s\r\n",
  2817. usb_speed_string(hsotg->gadget.speed));
  2818. /*
  2819. * we should now know the maximum packet size for an
  2820. * endpoint, so set the endpoints to a default value.
  2821. */
  2822. if (ep0_mps) {
  2823. int i;
  2824. /* Initialize ep0 for both in and out directions */
  2825. dwc2_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 0, 1);
  2826. dwc2_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 0, 0);
  2827. for (i = 1; i < hsotg->num_of_eps; i++) {
  2828. if (hsotg->eps_in[i])
  2829. dwc2_hsotg_set_ep_maxpacket(hsotg, i, ep_mps,
  2830. 0, 1);
  2831. if (hsotg->eps_out[i])
  2832. dwc2_hsotg_set_ep_maxpacket(hsotg, i, ep_mps,
  2833. 0, 0);
  2834. }
  2835. }
  2836. /* ensure after enumeration our EP0 is active */
  2837. dwc2_hsotg_enqueue_setup(hsotg);
  2838. dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
  2839. dwc2_readl(hsotg->regs + DIEPCTL0),
  2840. dwc2_readl(hsotg->regs + DOEPCTL0));
  2841. }
  2842. /**
  2843. * kill_all_requests - remove all requests from the endpoint's queue
  2844. * @hsotg: The device state.
  2845. * @ep: The endpoint the requests may be on.
  2846. * @result: The result code to use.
  2847. *
  2848. * Go through the requests on the given endpoint and mark them
  2849. * completed with the given result code.
  2850. */
  2851. static void kill_all_requests(struct dwc2_hsotg *hsotg,
  2852. struct dwc2_hsotg_ep *ep,
  2853. int result)
  2854. {
  2855. struct dwc2_hsotg_req *req;//, *treq;
  2856. unsigned int size;
  2857. ep->req = NULL;
  2858. #ifndef NO_GNU
  2859. list_for_each_entry_safe(req, treq, &ep->queue, queue)
  2860. dwc2_hsotg_complete_request(hsotg, ep, req,
  2861. result);
  2862. #else
  2863. ListItem_t *pxListItem, *nListItem;
  2864. list_for_each_entry_safe(pxListItem, nListItem, req, &ep->queue) {
  2865. dwc2_hsotg_complete_request(hsotg, ep, req, result);
  2866. }
  2867. #endif
  2868. if (!hsotg->dedicated_fifos)
  2869. return;
  2870. size = (dwc2_readl(hsotg->regs + DTXFSTS(ep->fifo_index)) & 0xffff) * 4;
  2871. if (size < ep->fifo_size)
  2872. dwc2_hsotg_txfifo_flush(hsotg, ep->fifo_index);
  2873. }
  2874. /**
  2875. * dwc2_hsotg_disconnect - disconnect service
  2876. * @hsotg: The device state.
  2877. *
  2878. * The device has been disconnected. Remove all current
  2879. * transactions and signal the gadget driver that this
  2880. * has happened.
  2881. */
  2882. void dwc2_hsotg_disconnect(struct dwc2_hsotg *hsotg)
  2883. {
  2884. unsigned int ep;
  2885. if (!hsotg->connected)
  2886. return;
  2887. hsotg->connected = 0;
  2888. hsotg->test_mode = 0;
  2889. for (ep = 0; ep < hsotg->num_of_eps; ep++) {
  2890. if (hsotg->eps_in[ep])
  2891. kill_all_requests(hsotg, hsotg->eps_in[ep],
  2892. -ESHUTDOWN);
  2893. if (hsotg->eps_out[ep])
  2894. kill_all_requests(hsotg, hsotg->eps_out[ep],
  2895. -ESHUTDOWN);
  2896. }
  2897. call_gadget(hsotg, disconnect);
  2898. hsotg->lx_state = DWC2_L3;
  2899. }
  2900. /**
  2901. * dwc2_hsotg_irq_fifoempty - TX FIFO empty interrupt handler
  2902. * @hsotg: The device state:
  2903. * @periodic: True if this is a periodic FIFO interrupt
  2904. */
  2905. static void dwc2_hsotg_irq_fifoempty(struct dwc2_hsotg *hsotg, bool periodic)
  2906. {
  2907. struct dwc2_hsotg_ep *ep;
  2908. int epno, ret;
  2909. /* look through for any more data to transmit */
  2910. for (epno = 0; epno < hsotg->num_of_eps; epno++) {
  2911. ep = index_to_ep(hsotg, epno, 1);
  2912. if (!ep)
  2913. continue;
  2914. if (!ep->dir_in)
  2915. continue;
  2916. if ((periodic && !ep->periodic) ||
  2917. (!periodic && ep->periodic))
  2918. continue;
  2919. ret = dwc2_hsotg_trytx(hsotg, ep);
  2920. if (ret < 0)
  2921. break;
  2922. }
  2923. }
  2924. /* IRQ flags which will trigger a retry around the IRQ loop */
  2925. #define IRQ_RETRY_MASK (GINTSTS_NPTXFEMP | \
  2926. GINTSTS_PTXFEMP | \
  2927. GINTSTS_RXFLVL)
  2928. /**
  2929. * dwc2_hsotg_core_init - issue softreset to the core
  2930. * @hsotg: The device state
  2931. *
  2932. * Issue a soft reset to the core, and await the core finishing it.
  2933. */
  2934. void dwc2_hsotg_core_init_disconnected(struct dwc2_hsotg *hsotg,
  2935. bool is_usb_reset)
  2936. {
  2937. u32 intmsk;
  2938. u32 val;
  2939. u32 usbcfg;
  2940. u32 dcfg = 0;
  2941. /* Kill any ep0 requests as controller will be reinitialized */
  2942. kill_all_requests(hsotg, hsotg->eps_out[0], -ECONNRESET);
  2943. if (!is_usb_reset)
  2944. if (dwc2_core_reset(hsotg, true))
  2945. return;
  2946. /*
  2947. * we must now enable ep0 ready for host detection and then
  2948. * set configuration.
  2949. */
  2950. /* keep other bits untouched (so e.g. forced modes are not lost) */
  2951. usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
  2952. usbcfg &= ~(GUSBCFG_TOUTCAL_MASK | GUSBCFG_PHYIF16 | GUSBCFG_SRPCAP |
  2953. GUSBCFG_HNPCAP | GUSBCFG_USBTRDTIM_MASK);
  2954. if (hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS &&
  2955. (hsotg->params.speed == DWC2_SPEED_PARAM_FULL ||
  2956. hsotg->params.speed == DWC2_SPEED_PARAM_LOW)) {
  2957. /* FS/LS Dedicated Transceiver Interface */
  2958. usbcfg |= GUSBCFG_PHYSEL;
  2959. } else {
  2960. /* set the PLL on, remove the HNP/SRP and set the PHY */
  2961. val = (hsotg->phyif == GUSBCFG_PHYIF8) ? 9 : 5;
  2962. usbcfg |= hsotg->phyif | GUSBCFG_TOUTCAL(7) |
  2963. (val << GUSBCFG_USBTRDTIM_SHIFT);
  2964. }
  2965. dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
  2966. dwc2_hsotg_init_fifo(hsotg);
  2967. if (!is_usb_reset)
  2968. __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON);
  2969. dcfg |= DCFG_EPMISCNT(1);
  2970. switch (hsotg->params.speed) {
  2971. case DWC2_SPEED_PARAM_LOW:
  2972. dcfg |= DCFG_DEVSPD_LS;
  2973. break;
  2974. case DWC2_SPEED_PARAM_FULL:
  2975. if (hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS)
  2976. dcfg |= DCFG_DEVSPD_FS48;
  2977. else
  2978. dcfg |= DCFG_DEVSPD_FS;
  2979. break;
  2980. default:
  2981. dcfg |= DCFG_DEVSPD_HS;
  2982. }
  2983. dwc2_writel(dcfg, hsotg->regs + DCFG);
  2984. /* Clear any pending OTG interrupts */
  2985. dwc2_writel(0xffffffff, hsotg->regs + GOTGINT);
  2986. /* Clear any pending interrupts */
  2987. dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
  2988. intmsk = GINTSTS_ERLYSUSP | GINTSTS_SESSREQINT |
  2989. GINTSTS_GOUTNAKEFF | GINTSTS_GINNAKEFF |
  2990. GINTSTS_USBRST | GINTSTS_RESETDET |
  2991. GINTSTS_ENUMDONE | GINTSTS_OTGINT |
  2992. GINTSTS_USBSUSP | GINTSTS_WKUPINT;
  2993. if (!using_desc_dma(hsotg))
  2994. intmsk |= GINTSTS_INCOMPL_SOIN | GINTSTS_INCOMPL_SOOUT;
  2995. if (!hsotg->params.external_id_pin_ctl)
  2996. intmsk |= GINTSTS_CONIDSTSCHNG;
  2997. dwc2_writel(intmsk, hsotg->regs + GINTMSK);
  2998. if (using_dma(hsotg)) {
  2999. dwc2_writel(GAHBCFG_GLBL_INTR_EN | GAHBCFG_DMA_EN |
  3000. (GAHBCFG_HBSTLEN_INCR4 << GAHBCFG_HBSTLEN_SHIFT),
  3001. hsotg->regs + GAHBCFG);
  3002. /* Set DDMA mode support in the core if needed */
  3003. if (using_desc_dma(hsotg))
  3004. __orr32(hsotg->regs + DCFG, DCFG_DESCDMA_EN);
  3005. } else {
  3006. dwc2_writel(((hsotg->dedicated_fifos) ?
  3007. (GAHBCFG_NP_TXF_EMP_LVL |
  3008. GAHBCFG_P_TXF_EMP_LVL) : 0) |
  3009. GAHBCFG_GLBL_INTR_EN, hsotg->regs + GAHBCFG);
  3010. }
  3011. /*
  3012. * If INTknTXFEmpMsk is enabled, it's important to disable ep interrupts
  3013. * when we have no data to transfer. Otherwise we get being flooded by
  3014. * interrupts.
  3015. */
  3016. dwc2_writel(((hsotg->dedicated_fifos && !using_dma(hsotg)) ?
  3017. DIEPMSK_TXFIFOEMPTY | DIEPMSK_INTKNTXFEMPMSK : 0) |
  3018. DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK |
  3019. DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK,
  3020. hsotg->regs + DIEPMSK);
  3021. /*
  3022. * don't need XferCompl, we get that from RXFIFO in slave mode. In
  3023. * DMA mode we may need this and StsPhseRcvd.
  3024. */
  3025. dwc2_writel((using_dma(hsotg) ? (DIEPMSK_XFERCOMPLMSK |
  3026. DOEPMSK_STSPHSERCVDMSK) : 0) |
  3027. DOEPMSK_EPDISBLDMSK | DOEPMSK_AHBERRMSK |
  3028. DOEPMSK_SETUPMSK,
  3029. hsotg->regs + DOEPMSK);
  3030. /* Enable BNA interrupt for DDMA */
  3031. if (using_desc_dma(hsotg))
  3032. __orr32(hsotg->regs + DOEPMSK, DOEPMSK_BNAMSK);
  3033. dwc2_writel(0, hsotg->regs + DAINTMSK);
  3034. dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
  3035. dwc2_readl(hsotg->regs + DIEPCTL0),
  3036. dwc2_readl(hsotg->regs + DOEPCTL0));
  3037. /* enable in and out endpoint interrupts */
  3038. dwc2_hsotg_en_gsint(hsotg, GINTSTS_OEPINT | GINTSTS_IEPINT);
  3039. /*
  3040. * Enable the RXFIFO when in slave mode, as this is how we collect
  3041. * the data. In DMA mode, we get events from the FIFO but also
  3042. * things we cannot process, so do not use it.
  3043. */
  3044. if (!using_dma(hsotg))
  3045. dwc2_hsotg_en_gsint(hsotg, GINTSTS_RXFLVL);
  3046. /* Enable interrupts for EP0 in and out */
  3047. dwc2_hsotg_ctrl_epint(hsotg, 0, 0, 1);
  3048. dwc2_hsotg_ctrl_epint(hsotg, 0, 1, 1);
  3049. if (!is_usb_reset) {
  3050. __orr32(hsotg->regs + DCTL, DCTL_PWRONPRGDONE);
  3051. udelay(10); /* see openiboot */
  3052. __bic32(hsotg->regs + DCTL, DCTL_PWRONPRGDONE);
  3053. }
  3054. dev_dbg(hsotg->dev, "DCTL=0x%08x\n", dwc2_readl(hsotg->regs + DCTL));
  3055. /*
  3056. * DxEPCTL_USBActEp says RO in manual, but seems to be set by
  3057. * writing to the EPCTL register..
  3058. */
  3059. /* set to read 1 8byte packet */
  3060. dwc2_writel(DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
  3061. DXEPTSIZ_XFERSIZE(8), hsotg->regs + DOEPTSIZ0);
  3062. dwc2_writel(dwc2_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) |
  3063. DXEPCTL_CNAK | DXEPCTL_EPENA |
  3064. DXEPCTL_USBACTEP,
  3065. hsotg->regs + DOEPCTL0);
  3066. /* enable, but don't activate EP0in */
  3067. dwc2_writel(dwc2_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) |
  3068. DXEPCTL_USBACTEP, hsotg->regs + DIEPCTL0);
  3069. /* clear global NAKs */
  3070. val = DCTL_CGOUTNAK | DCTL_CGNPINNAK;
  3071. if (!is_usb_reset)
  3072. val |= DCTL_SFTDISCON;
  3073. __orr32(hsotg->regs + DCTL, val);
  3074. /* must be at-least 3ms to allow bus to see disconnect */
  3075. mdelay(3);
  3076. hsotg->lx_state = DWC2_L0;
  3077. dwc2_hsotg_enqueue_setup(hsotg);
  3078. dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
  3079. dwc2_readl(hsotg->regs + DIEPCTL0),
  3080. dwc2_readl(hsotg->regs + DOEPCTL0));
  3081. }
  3082. static void dwc2_hsotg_core_disconnect(struct dwc2_hsotg *hsotg)
  3083. {
  3084. /* set the soft-disconnect bit */
  3085. __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON);
  3086. }
  3087. void dwc2_hsotg_core_connect(struct dwc2_hsotg *hsotg)
  3088. {
  3089. /* remove the soft-disconnect and let's go */
  3090. __bic32(hsotg->regs + DCTL, DCTL_SFTDISCON);
  3091. }
  3092. /**
  3093. * dwc2_gadget_handle_incomplete_isoc_in - handle incomplete ISO IN Interrupt.
  3094. * @hsotg: The device state:
  3095. *
  3096. * This interrupt indicates one of the following conditions occurred while
  3097. * transmitting an ISOC transaction.
  3098. * - Corrupted IN Token for ISOC EP.
  3099. * - Packet not complete in FIFO.
  3100. *
  3101. * The following actions will be taken:
  3102. * - Determine the EP
  3103. * - Disable EP; when 'Endpoint Disabled' interrupt is received Flush FIFO
  3104. */
  3105. static void dwc2_gadget_handle_incomplete_isoc_in(struct dwc2_hsotg *hsotg)
  3106. {
  3107. struct dwc2_hsotg_ep *hs_ep;
  3108. u32 epctrl;
  3109. u32 idx;
  3110. dev_dbg(hsotg->dev, "Incomplete isoc in interrupt received:\n");
  3111. for (idx = 1; idx <= hsotg->num_of_eps; idx++) {
  3112. hs_ep = hsotg->eps_in[idx];
  3113. epctrl = dwc2_readl(hsotg->regs + DIEPCTL(idx));
  3114. if ((epctrl & DXEPCTL_EPENA) && hs_ep->isochronous &&
  3115. dwc2_gadget_target_frame_elapsed(hs_ep)) {
  3116. epctrl |= DXEPCTL_SNAK;
  3117. epctrl |= DXEPCTL_EPDIS;
  3118. dwc2_writel(epctrl, hsotg->regs + DIEPCTL(idx));
  3119. }
  3120. }
  3121. /* Clear interrupt */
  3122. dwc2_writel(GINTSTS_INCOMPL_SOIN, hsotg->regs + GINTSTS);
  3123. }
  3124. /**
  3125. * dwc2_gadget_handle_incomplete_isoc_out - handle incomplete ISO OUT Interrupt
  3126. * @hsotg: The device state:
  3127. *
  3128. * This interrupt indicates one of the following conditions occurred while
  3129. * transmitting an ISOC transaction.
  3130. * - Corrupted OUT Token for ISOC EP.
  3131. * - Packet not complete in FIFO.
  3132. *
  3133. * The following actions will be taken:
  3134. * - Determine the EP
  3135. * - Set DCTL_SGOUTNAK and unmask GOUTNAKEFF if target frame elapsed.
  3136. */
  3137. static void dwc2_gadget_handle_incomplete_isoc_out(struct dwc2_hsotg *hsotg)
  3138. {
  3139. u32 gintsts;
  3140. u32 gintmsk;
  3141. u32 epctrl;
  3142. struct dwc2_hsotg_ep *hs_ep;
  3143. int idx;
  3144. dev_dbg(hsotg->dev, "%s: GINTSTS_INCOMPL_SOOUT\n", __func__);
  3145. for (idx = 1; idx <= hsotg->num_of_eps; idx++) {
  3146. hs_ep = hsotg->eps_out[idx];
  3147. epctrl = dwc2_readl(hsotg->regs + DOEPCTL(idx));
  3148. if ((epctrl & DXEPCTL_EPENA) && hs_ep->isochronous &&
  3149. dwc2_gadget_target_frame_elapsed(hs_ep)) {
  3150. /* Unmask GOUTNAKEFF interrupt */
  3151. gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
  3152. gintmsk |= GINTSTS_GOUTNAKEFF;
  3153. dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
  3154. gintsts = dwc2_readl(hsotg->regs + GINTSTS);
  3155. if (!(gintsts & GINTSTS_GOUTNAKEFF))
  3156. __orr32(hsotg->regs + DCTL, DCTL_SGOUTNAK);
  3157. }
  3158. }
  3159. /* Clear interrupt */
  3160. dwc2_writel(GINTSTS_INCOMPL_SOOUT, hsotg->regs + GINTSTS);
  3161. }
  3162. /**
  3163. * dwc2_hsotg_irq - handle device interrupt
  3164. * @irq: The IRQ number triggered
  3165. * @pw: The pw value when registered the handler.
  3166. */
  3167. irqreturn_t dwc2_hsotg_irq(int irq, void *pw)
  3168. {
  3169. struct dwc2_hsotg *hsotg = pw;
  3170. int retry_count = 8;
  3171. u32 gintsts;
  3172. u32 gintmsk;
  3173. if (!dwc2_is_device_mode(hsotg))
  3174. return IRQ_NONE;
  3175. spin_lock(&hsotg->lock);
  3176. irq_retry:
  3177. gintsts = dwc2_readl(hsotg->regs + GINTSTS);
  3178. gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
  3179. dev_dbg(hsotg->dev, "%s: %08x %08x (%08x) retry %d\r\n",
  3180. __func__, gintsts, gintsts & gintmsk, gintmsk, retry_count);
  3181. gintsts &= gintmsk;
  3182. if (gintsts & GINTSTS_RESETDET) {
  3183. dev_dbg(hsotg->dev, "%s: USBRstDet\n", __func__);
  3184. dwc2_writel(GINTSTS_RESETDET, hsotg->regs + GINTSTS);
  3185. /* This event must be used only if controller is suspended */
  3186. if (hsotg->lx_state == DWC2_L2) {
  3187. dwc2_exit_hibernation(hsotg, true);
  3188. hsotg->lx_state = DWC2_L0;
  3189. }
  3190. }
  3191. if (gintsts & (GINTSTS_USBRST | GINTSTS_RESETDET)) {
  3192. u32 usb_status = dwc2_readl(hsotg->regs + GOTGCTL);
  3193. u32 connected = hsotg->connected;
  3194. dev_dbg(hsotg->dev, "%s: USBRst\n", __func__);
  3195. dev_dbg(hsotg->dev, "GNPTXSTS=%08x\n",
  3196. dwc2_readl(hsotg->regs + GNPTXSTS));
  3197. dwc2_writel(GINTSTS_USBRST, hsotg->regs + GINTSTS);
  3198. /* Report disconnection if it is not already done. */
  3199. dwc2_hsotg_disconnect(hsotg);
  3200. /* Reset device address to zero */
  3201. __bic32(hsotg->regs + DCFG, DCFG_DEVADDR_MASK);
  3202. if (usb_status & GOTGCTL_BSESVLD && connected)
  3203. dwc2_hsotg_core_init_disconnected(hsotg, true);
  3204. }
  3205. if (gintsts & GINTSTS_ENUMDONE) {
  3206. dwc2_writel(GINTSTS_ENUMDONE, hsotg->regs + GINTSTS);
  3207. dwc2_hsotg_irq_enumdone(hsotg);
  3208. }
  3209. if (gintsts & (GINTSTS_OEPINT | GINTSTS_IEPINT)) {
  3210. u32 daint = dwc2_readl(hsotg->regs + DAINT);
  3211. u32 daintmsk = dwc2_readl(hsotg->regs + DAINTMSK);
  3212. u32 daint_out, daint_in;
  3213. int ep;
  3214. daint &= daintmsk;
  3215. daint_out = daint >> DAINT_OUTEP_SHIFT;
  3216. daint_in = daint & ~(daint_out << DAINT_OUTEP_SHIFT);
  3217. dev_dbg(hsotg->dev, "#%s: daint=%08x daintmsk=%08x\r\n", __func__, daint, daintmsk);
  3218. for (ep = 0; ep < hsotg->num_of_eps && daint_out;
  3219. ep++, daint_out >>= 1) {
  3220. if (daint_out & 1)
  3221. dwc2_hsotg_epint(hsotg, ep, 0);
  3222. }
  3223. for (ep = 0; ep < hsotg->num_of_eps && daint_in;
  3224. ep++, daint_in >>= 1) {
  3225. if (daint_in & 1)
  3226. dwc2_hsotg_epint(hsotg, ep, 1);
  3227. }
  3228. }
  3229. /* check both FIFOs */
  3230. if (gintsts & GINTSTS_NPTXFEMP) {
  3231. dev_dbg(hsotg->dev, "NPTxFEmp\n");
  3232. /*
  3233. * Disable the interrupt to stop it happening again
  3234. * unless one of these endpoint routines decides that
  3235. * it needs re-enabling
  3236. */
  3237. dwc2_hsotg_disable_gsint(hsotg, GINTSTS_NPTXFEMP);
  3238. dwc2_hsotg_irq_fifoempty(hsotg, false);
  3239. }
  3240. if (gintsts & GINTSTS_PTXFEMP) {
  3241. dev_dbg(hsotg->dev, "PTxFEmp\n");
  3242. /* See note in GINTSTS_NPTxFEmp */
  3243. dwc2_hsotg_disable_gsint(hsotg, GINTSTS_PTXFEMP);
  3244. dwc2_hsotg_irq_fifoempty(hsotg, true);
  3245. }
  3246. if (gintsts & GINTSTS_RXFLVL) {
  3247. /*
  3248. * note, since GINTSTS_RxFLvl doubles as FIFO-not-empty,
  3249. * we need to retry dwc2_hsotg_handle_rx if this is still
  3250. * set.
  3251. */
  3252. dwc2_hsotg_handle_rx(hsotg);
  3253. }
  3254. if (gintsts & GINTSTS_ERLYSUSP) {
  3255. dev_dbg(hsotg->dev, "GINTSTS_ErlySusp\r\n");
  3256. dwc2_writel(GINTSTS_ERLYSUSP, hsotg->regs + GINTSTS);
  3257. hsotg->driver->disconnect(&hsotg->gadget);
  3258. }
  3259. /*
  3260. * these next two seem to crop-up occasionally causing the core
  3261. * to shutdown the USB transfer, so try clearing them and logging
  3262. * the occurrence.
  3263. */
  3264. if (gintsts & GINTSTS_GOUTNAKEFF) {
  3265. u8 idx;
  3266. u32 epctrl;
  3267. u32 gintmsk;
  3268. struct dwc2_hsotg_ep *hs_ep;
  3269. /* Mask this interrupt */
  3270. gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
  3271. gintmsk &= ~GINTSTS_GOUTNAKEFF;
  3272. dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
  3273. dev_dbg(hsotg->dev, "GOUTNakEff triggered\n");
  3274. for (idx = 1; idx <= hsotg->num_of_eps; idx++) {
  3275. hs_ep = hsotg->eps_out[idx];
  3276. epctrl = dwc2_readl(hsotg->regs + DOEPCTL(idx));
  3277. if ((epctrl & DXEPCTL_EPENA) && hs_ep->isochronous) {
  3278. epctrl |= DXEPCTL_SNAK;
  3279. epctrl |= DXEPCTL_EPDIS;
  3280. dwc2_writel(epctrl, hsotg->regs + DOEPCTL(idx));
  3281. }
  3282. }
  3283. /* This interrupt bit is cleared in DXEPINT_EPDISBLD handler */
  3284. }
  3285. if (gintsts & GINTSTS_GINNAKEFF) {
  3286. dev_info(hsotg->dev, "GINNakEff triggered\r\n");
  3287. __orr32(hsotg->regs + DCTL, DCTL_CGNPINNAK);
  3288. dwc2_hsotg_dump(hsotg);
  3289. do {
  3290. struct wq_msg *pmsg = &hsotg->xmsg;
  3291. pmsg->id = OTG_WQ_MSG_ID_DEV_RESET;
  3292. pmsg->delay = 50;
  3293. xQueueSendFromISR(hsotg->wq_otg, (void*)pmsg, 0);
  3294. } while(0);
  3295. }
  3296. if (gintsts & GINTSTS_INCOMPL_SOIN)
  3297. dwc2_gadget_handle_incomplete_isoc_in(hsotg);
  3298. if (gintsts & GINTSTS_INCOMPL_SOOUT)
  3299. dwc2_gadget_handle_incomplete_isoc_out(hsotg);
  3300. /*
  3301. * if we've had fifo events, we should try and go around the
  3302. * loop again to see if there's any point in returning yet.
  3303. */
  3304. if (gintsts & IRQ_RETRY_MASK && --retry_count > 0)
  3305. goto irq_retry;
  3306. spin_unlock(&hsotg->lock);
  3307. return IRQ_HANDLED;
  3308. }
  3309. static int dwc2_hsotg_wait_bit_set(struct dwc2_hsotg *hs_otg, u32 reg,
  3310. u32 bit, u32 timeout)
  3311. {
  3312. u32 i;
  3313. for (i = 0; i < timeout; i++) {
  3314. if (dwc2_readl(hs_otg->regs + reg) & bit)
  3315. return 0;
  3316. udelay(1);
  3317. }
  3318. return -ETIMEDOUT;
  3319. }
  3320. static void dwc2_hsotg_ep_stop_xfr(struct dwc2_hsotg *hsotg,
  3321. struct dwc2_hsotg_ep *hs_ep)
  3322. {
  3323. u32 epctrl_reg;
  3324. u32 epint_reg;
  3325. epctrl_reg = hs_ep->dir_in ? DIEPCTL(hs_ep->index) :
  3326. DOEPCTL(hs_ep->index);
  3327. epint_reg = hs_ep->dir_in ? DIEPINT(hs_ep->index) :
  3328. DOEPINT(hs_ep->index);
  3329. dev_dbg(hsotg->dev, "%s: stopping transfer on %s\n", __func__,
  3330. hs_ep->name);
  3331. if (hs_ep->dir_in) {
  3332. if (hsotg->dedicated_fifos || hs_ep->periodic) {
  3333. __orr32(hsotg->regs + epctrl_reg, DXEPCTL_SNAK);
  3334. /* Wait for Nak effect */
  3335. if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg,
  3336. DXEPINT_INEPNAKEFF, 100))
  3337. dev_warn(hsotg->dev,
  3338. "%s: timeout DIEPINT.NAKEFF\n",
  3339. __func__);
  3340. } else {
  3341. __orr32(hsotg->regs + DCTL, DCTL_SGNPINNAK);
  3342. /* Wait for Nak effect */
  3343. if (dwc2_hsotg_wait_bit_set(hsotg, GINTSTS,
  3344. GINTSTS_GINNAKEFF, 100))
  3345. dev_warn(hsotg->dev,
  3346. "%s: timeout GINTSTS.GINNAKEFF\n",
  3347. __func__);
  3348. }
  3349. } else {
  3350. if (!(dwc2_readl(hsotg->regs + GINTSTS) & GINTSTS_GOUTNAKEFF))
  3351. __orr32(hsotg->regs + DCTL, DCTL_SGOUTNAK);
  3352. /* Wait for global nak to take effect */
  3353. if (dwc2_hsotg_wait_bit_set(hsotg, GINTSTS,
  3354. GINTSTS_GOUTNAKEFF, 100))
  3355. dev_warn(hsotg->dev, "%s: timeout GINTSTS.GOUTNAKEFF\n",
  3356. __func__);
  3357. }
  3358. /* Disable ep */
  3359. __orr32(hsotg->regs + epctrl_reg, DXEPCTL_EPDIS | DXEPCTL_SNAK);
  3360. /* Wait for ep to be disabled */
  3361. if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg, DXEPINT_EPDISBLD, 100))
  3362. dev_warn(hsotg->dev,
  3363. "%s: timeout DOEPCTL.EPDisable\n", __func__);
  3364. /* Clear EPDISBLD interrupt */
  3365. __orr32(hsotg->regs + epint_reg, DXEPINT_EPDISBLD);
  3366. if (hs_ep->dir_in) {
  3367. unsigned short fifo_index;
  3368. if (hsotg->dedicated_fifos || hs_ep->periodic)
  3369. fifo_index = hs_ep->fifo_index;
  3370. else
  3371. fifo_index = 0;
  3372. /* Flush TX FIFO */
  3373. dwc2_flush_tx_fifo(hsotg, fifo_index);
  3374. /* Clear Global In NP NAK in Shared FIFO for non periodic ep */
  3375. if (!hsotg->dedicated_fifos && !hs_ep->periodic)
  3376. __orr32(hsotg->regs + DCTL, DCTL_CGNPINNAK);
  3377. } else {
  3378. /* Remove global NAKs */
  3379. __orr32(hsotg->regs + DCTL, DCTL_CGOUTNAK);
  3380. }
  3381. }
  3382. /**
  3383. * dwc2_hsotg_ep_enable - enable the given endpoint
  3384. * @ep: The USB endpint to configure
  3385. * @desc: The USB endpoint descriptor to configure with.
  3386. *
  3387. * This is called from the USB gadget code's usb_ep_enable().
  3388. */
  3389. static int dwc2_hsotg_ep_enable(struct usb_ep *ep,
  3390. const struct usb_endpoint_descriptor *desc)
  3391. {
  3392. struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
  3393. struct dwc2_hsotg *hsotg = hs_ep->parent;
  3394. unsigned long flags;
  3395. unsigned int index = hs_ep->index;
  3396. u32 epctrl_reg;
  3397. u32 epctrl;
  3398. u32 mps;
  3399. u32 mc;
  3400. u32 mask;
  3401. unsigned int dir_in;
  3402. unsigned int i, val, size;
  3403. int ret = 0;
  3404. dev_dbg(hsotg->dev,
  3405. "%s: ep %s: a 0x%02x, attr 0x%02x, mps 0x%04x, intr %d\r\n",
  3406. __func__, ep->name, desc->bEndpointAddress, desc->bmAttributes,
  3407. desc->wMaxPacketSize, desc->bInterval);
  3408. /* not to be called for EP0 */
  3409. if (index == 0) {
  3410. dev_err(hsotg->dev, "%s: called for EP 0\n", __func__);
  3411. return -EINVAL;
  3412. }
  3413. dir_in = (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? 1 : 0;
  3414. if (dir_in != hs_ep->dir_in) {
  3415. dev_err(hsotg->dev, "%s: direction mismatch!\n", __func__);
  3416. return -EINVAL;
  3417. }
  3418. mps = usb_endpoint_maxp(desc);
  3419. mc = usb_endpoint_maxp_mult(desc);
  3420. /* note, we handle this here instead of dwc2_hsotg_set_ep_maxpacket */
  3421. epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
  3422. epctrl = dwc2_readl(hsotg->regs + epctrl_reg);
  3423. dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x from 0x%08x\n",
  3424. __func__, epctrl, epctrl_reg);
  3425. /* Allocate DMA descriptor chain for non-ctrl endpoints */
  3426. if (using_desc_dma(hsotg) && !hs_ep->desc_list) {
  3427. hs_ep->desc_list = dmam_alloc_coherent(hsotg->dev,
  3428. MAX_DMA_DESC_NUM_GENERIC *
  3429. sizeof(struct dwc2_dma_desc),
  3430. &hs_ep->desc_list_dma, GFP_ATOMIC);
  3431. if (!hs_ep->desc_list) {
  3432. ret = -ENOMEM;
  3433. goto error2;
  3434. }
  3435. #ifdef NO_GNU
  3436. if ((u32)hs_ep->desc_list & (ARCH_DMA_MINALIGN - 1)) {
  3437. u32 addr = (u32)(((u32)hs_ep->desc_list + ARCH_DMA_MINALIGN) & (~(ARCH_DMA_MINALIGN - 1)));
  3438. hs_ep->desc_list_offset = addr - (u32)hs_ep->desc_list;
  3439. hs_ep->desc_list = (struct dwc2_dma_desc *)addr;
  3440. hs_ep->desc_list_dma += hs_ep->desc_list_offset;
  3441. } else {
  3442. hs_ep->desc_list_offset = 0;
  3443. }
  3444. #endif
  3445. }
  3446. spin_lock_irqsave(&hsotg->lock, flags);
  3447. epctrl &= ~(DXEPCTL_EPTYPE_MASK | DXEPCTL_MPS_MASK);
  3448. epctrl |= DXEPCTL_MPS(mps);
  3449. /*
  3450. * mark the endpoint as active, otherwise the core may ignore
  3451. * transactions entirely for this endpoint
  3452. */
  3453. epctrl |= DXEPCTL_USBACTEP;
  3454. /* update the endpoint state */
  3455. dwc2_hsotg_set_ep_maxpacket(hsotg, hs_ep->index, mps, mc, dir_in);
  3456. /* default, set to non-periodic */
  3457. hs_ep->isochronous = 0;
  3458. hs_ep->periodic = 0;
  3459. hs_ep->halted = 0;
  3460. hs_ep->interval = desc->bInterval;
  3461. switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
  3462. case USB_ENDPOINT_XFER_ISOC:
  3463. epctrl |= DXEPCTL_EPTYPE_ISO;
  3464. epctrl |= DXEPCTL_SETEVENFR;
  3465. hs_ep->isochronous = 1;
  3466. hs_ep->interval = 1 << (desc->bInterval - 1);
  3467. hs_ep->target_frame = TARGET_FRAME_INITIAL;
  3468. hs_ep->isoc_chain_num = 0;
  3469. hs_ep->next_desc = 0;
  3470. if (dir_in) {
  3471. hs_ep->periodic = 1;
  3472. mask = dwc2_readl(hsotg->regs + DIEPMSK);
  3473. mask |= DIEPMSK_NAKMSK;
  3474. dwc2_writel(mask, hsotg->regs + DIEPMSK);
  3475. } else {
  3476. mask = dwc2_readl(hsotg->regs + DOEPMSK);
  3477. mask |= DOEPMSK_OUTTKNEPDISMSK;
  3478. dwc2_writel(mask, hsotg->regs + DOEPMSK);
  3479. }
  3480. break;
  3481. case USB_ENDPOINT_XFER_BULK:
  3482. epctrl |= DXEPCTL_EPTYPE_BULK;
  3483. break;
  3484. case USB_ENDPOINT_XFER_INT:
  3485. if (dir_in)
  3486. hs_ep->periodic = 1;
  3487. if (hsotg->gadget.speed == USB_SPEED_HIGH)
  3488. hs_ep->interval = 1 << (desc->bInterval - 1);
  3489. epctrl |= DXEPCTL_EPTYPE_INTERRUPT;
  3490. break;
  3491. case USB_ENDPOINT_XFER_CONTROL:
  3492. epctrl |= DXEPCTL_EPTYPE_CONTROL;
  3493. break;
  3494. }
  3495. /*
  3496. * if the hardware has dedicated fifos, we must give each IN EP
  3497. * a unique tx-fifo even if it is non-periodic.
  3498. */
  3499. if (dir_in && (hsotg->dedicated_fifos || (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)) {
  3500. u32 fifo_index = 0;
  3501. u32 fifo_size = UINT_MAX;
  3502. size = hs_ep->ep.maxpacket * hs_ep->mc;
  3503. for (i = 1; i < hsotg->num_of_eps; ++i) {
  3504. if (hsotg->fifo_map & (1 << i))
  3505. continue;
  3506. val = dwc2_readl(hsotg->regs + DPTXFSIZN(i));
  3507. val = (val >> FIFOSIZE_DEPTH_SHIFT) * 4;
  3508. if (val < size)
  3509. continue;
  3510. /* Search for smallest acceptable fifo */
  3511. if (val < fifo_size) {
  3512. fifo_size = val;
  3513. fifo_index = i;
  3514. }
  3515. }
  3516. if (!fifo_index) {
  3517. dev_err(hsotg->dev,
  3518. "%s: No suitable fifo found\n", __func__);
  3519. ret = -ENOMEM;
  3520. goto error1;
  3521. }
  3522. hsotg->fifo_map |= 1 << fifo_index;
  3523. epctrl |= DXEPCTL_TXFNUM(fifo_index);
  3524. hs_ep->fifo_index = fifo_index;
  3525. hs_ep->fifo_size = fifo_size;
  3526. }
  3527. /* for non control endpoints, set PID to D0 */
  3528. if (index && !hs_ep->isochronous)
  3529. epctrl |= DXEPCTL_SETD0PID;
  3530. dev_dbg(hsotg->dev, "%s: write DxEPCTL=0x%08x\n",
  3531. __func__, epctrl);
  3532. dwc2_writel(epctrl, hsotg->regs + epctrl_reg);
  3533. dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x\n",
  3534. __func__, dwc2_readl(hsotg->regs + epctrl_reg));
  3535. /* enable the endpoint interrupt */
  3536. dwc2_hsotg_ctrl_epint(hsotg, index, dir_in, 1);
  3537. error1:
  3538. spin_unlock_irqrestore(&hsotg->lock, flags);
  3539. error2:
  3540. if (ret && using_desc_dma(hsotg) && hs_ep->desc_list) {
  3541. #ifdef NO_GNU
  3542. u32 addr = (u32)(hs_ep->desc_list) - hs_ep->desc_list_offset;
  3543. hs_ep->desc_list = (struct dwc2_dma_desc *)addr;
  3544. hs_ep->desc_list_dma -= hs_ep->desc_list_offset;
  3545. #endif
  3546. dmam_free_coherent(hsotg->dev, MAX_DMA_DESC_NUM_GENERIC *
  3547. sizeof(struct dwc2_dma_desc),
  3548. (void *)hs_ep->desc_list, hs_ep->desc_list_dma);
  3549. hs_ep->desc_list = NULL;
  3550. }
  3551. return ret;
  3552. }
  3553. /**
  3554. * dwc2_hsotg_ep_disable - disable given endpoint
  3555. * @ep: The endpoint to disable.
  3556. */
  3557. static int dwc2_hsotg_ep_disable(struct usb_ep *ep)
  3558. {
  3559. struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
  3560. struct dwc2_hsotg *hsotg = hs_ep->parent;
  3561. int dir_in = hs_ep->dir_in;
  3562. int index = hs_ep->index;
  3563. unsigned long flags;
  3564. u32 epctrl_reg;
  3565. u32 ctrl;
  3566. dev_dbg(hsotg->dev, "%s(ep %p)\n", __func__, ep);
  3567. if (ep == &hsotg->eps_out[0]->ep) {
  3568. dev_err(hsotg->dev, "%s: called for ep0\n", __func__);
  3569. return -EINVAL;
  3570. }
  3571. epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
  3572. spin_lock_irqsave(&hsotg->lock, flags);
  3573. ctrl = dwc2_readl(hsotg->regs + epctrl_reg);
  3574. if (ctrl & DXEPCTL_EPENA)
  3575. dwc2_hsotg_ep_stop_xfr(hsotg, hs_ep);
  3576. ctrl &= ~DXEPCTL_EPENA;
  3577. ctrl &= ~DXEPCTL_USBACTEP;
  3578. ctrl |= DXEPCTL_SNAK;
  3579. dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
  3580. dwc2_writel(ctrl, hsotg->regs + epctrl_reg);
  3581. /* disable endpoint interrupts */
  3582. dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 0);
  3583. /* terminate all requests with shutdown */
  3584. kill_all_requests(hsotg, hs_ep, -ESHUTDOWN);
  3585. hsotg->fifo_map &= ~(1 << hs_ep->fifo_index);
  3586. hs_ep->fifo_index = 0;
  3587. hs_ep->fifo_size = 0;
  3588. spin_unlock_irqrestore(&hsotg->lock, flags);
  3589. return 0;
  3590. }
  3591. /**
  3592. * on_list - check request is on the given endpoint
  3593. * @ep: The endpoint to check.
  3594. * @test: The request to test if it is on the endpoint.
  3595. */
  3596. static bool on_list(struct dwc2_hsotg_ep *ep, struct dwc2_hsotg_req *test)
  3597. {
  3598. struct dwc2_hsotg_req *req;//, *treq;
  3599. #ifndef NO_GNU
  3600. list_for_each_entry_safe(req, treq, &ep->queue, queue) {
  3601. if (req == test)
  3602. return true;
  3603. }
  3604. #else
  3605. ListItem_t *pxListItem, *nListItem;
  3606. list_for_each_entry_safe(pxListItem, nListItem, req, &ep->queue) {
  3607. if (req == test)
  3608. return true;
  3609. }
  3610. #endif
  3611. return false;
  3612. }
  3613. /**
  3614. * dwc2_hsotg_ep_dequeue - dequeue given endpoint
  3615. * @ep: The endpoint to dequeue.
  3616. * @req: The request to be removed from a queue.
  3617. */
  3618. static int dwc2_hsotg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
  3619. {
  3620. struct dwc2_hsotg_req *hs_req = our_req(req);
  3621. struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
  3622. struct dwc2_hsotg *hs = hs_ep->parent;
  3623. unsigned long flags;
  3624. dev_dbg(hs->dev, "ep_dequeue(%p,%p)\n", ep, req);
  3625. spin_lock_irqsave(&hs->lock, flags);
  3626. if (!on_list(hs_ep, hs_req)) {
  3627. spin_unlock_irqrestore(&hs->lock, flags);
  3628. return -EINVAL;
  3629. }
  3630. /* Dequeue already started request */
  3631. if (req == &hs_ep->req->req)
  3632. dwc2_hsotg_ep_stop_xfr(hs, hs_ep);
  3633. dwc2_hsotg_complete_request(hs, hs_ep, hs_req, -ECONNRESET);
  3634. spin_unlock_irqrestore(&hs->lock, flags);
  3635. return 0;
  3636. }
  3637. /**
  3638. * dwc2_hsotg_ep_sethalt - set halt on a given endpoint
  3639. * @ep: The endpoint to set halt.
  3640. * @value: Set or unset the halt.
  3641. * @now: If true, stall the endpoint now. Otherwise return -EAGAIN if
  3642. * the endpoint is busy processing requests.
  3643. *
  3644. * We need to stall the endpoint immediately if request comes from set_feature
  3645. * protocol command handler.
  3646. */
  3647. static int dwc2_hsotg_ep_sethalt(struct usb_ep *ep, int value, bool now)
  3648. {
  3649. struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
  3650. struct dwc2_hsotg *hs = hs_ep->parent;
  3651. int index = hs_ep->index;
  3652. u32 epreg;
  3653. u32 epctl;
  3654. u32 xfertype;
  3655. dev_dbg(hs->dev, "%s(ep %p %s, %d)\r\n", __func__, ep, ep->name, value);
  3656. if (index == 0) {
  3657. if (value)
  3658. dwc2_hsotg_stall_ep0(hs);
  3659. else
  3660. dev_warn(hs->dev,
  3661. "%s: can't clear halt on ep0\n", __func__);
  3662. return 0;
  3663. }
  3664. if (hs_ep->isochronous) {
  3665. dev_err(hs->dev, "%s is Isochronous Endpoint\n", ep->name);
  3666. return -EINVAL;
  3667. }
  3668. if (!now && value && !list_empty(&hs_ep->queue)) {
  3669. dev_dbg(hs->dev, "%s request is pending, cannot halt\n",
  3670. ep->name);
  3671. return -EAGAIN;
  3672. }
  3673. if (hs_ep->dir_in) {
  3674. epreg = DIEPCTL(index);
  3675. epctl = dwc2_readl(hs->regs + epreg);
  3676. if (value) {
  3677. epctl |= DXEPCTL_STALL | DXEPCTL_SNAK;
  3678. if (epctl & DXEPCTL_EPENA)
  3679. epctl |= DXEPCTL_EPDIS;
  3680. } else {
  3681. epctl &= ~DXEPCTL_STALL;
  3682. xfertype = epctl & DXEPCTL_EPTYPE_MASK;
  3683. if (xfertype == DXEPCTL_EPTYPE_BULK ||
  3684. xfertype == DXEPCTL_EPTYPE_INTERRUPT)
  3685. epctl |= DXEPCTL_SETD0PID;
  3686. }
  3687. dwc2_writel(epctl, hs->regs + epreg);
  3688. } else {
  3689. epreg = DOEPCTL(index);
  3690. epctl = dwc2_readl(hs->regs + epreg);
  3691. if (value) {
  3692. epctl |= DXEPCTL_STALL;
  3693. } else {
  3694. epctl &= ~DXEPCTL_STALL;
  3695. xfertype = epctl & DXEPCTL_EPTYPE_MASK;
  3696. if (xfertype == DXEPCTL_EPTYPE_BULK ||
  3697. xfertype == DXEPCTL_EPTYPE_INTERRUPT)
  3698. epctl |= DXEPCTL_SETD0PID;
  3699. }
  3700. dwc2_writel(epctl, hs->regs + epreg);
  3701. }
  3702. hs_ep->halted = value;
  3703. return 0;
  3704. }
  3705. /**
  3706. * dwc2_hsotg_ep_sethalt_lock - set halt on a given endpoint with lock held
  3707. * @ep: The endpoint to set halt.
  3708. * @value: Set or unset the halt.
  3709. */
  3710. static int dwc2_hsotg_ep_sethalt_lock(struct usb_ep *ep, int value)
  3711. {
  3712. struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
  3713. struct dwc2_hsotg *hs = hs_ep->parent;
  3714. unsigned long flags = 0;
  3715. int ret = 0;
  3716. spin_lock_irqsave(&hs->lock, flags);
  3717. ret = dwc2_hsotg_ep_sethalt(ep, value, false);
  3718. spin_unlock_irqrestore(&hs->lock, flags);
  3719. return ret;
  3720. }
  3721. static const struct usb_ep_ops dwc2_hsotg_ep_ops = {
  3722. .enable = dwc2_hsotg_ep_enable,
  3723. .disable = dwc2_hsotg_ep_disable,
  3724. .alloc_request = dwc2_hsotg_ep_alloc_request,
  3725. .free_request = dwc2_hsotg_ep_free_request,
  3726. .queue = dwc2_hsotg_ep_queue_lock,
  3727. .dequeue = dwc2_hsotg_ep_dequeue,
  3728. .set_halt = dwc2_hsotg_ep_sethalt_lock,
  3729. /* note, don't believe we have any call for the fifo routines */
  3730. };
  3731. /**
  3732. * dwc2_hsotg_init - initialize the usb core
  3733. * @hsotg: The driver state
  3734. */
  3735. static void dwc2_hsotg_init(struct dwc2_hsotg *hsotg)
  3736. {
  3737. u32 trdtim;
  3738. u32 usbcfg;
  3739. /* unmask subset of endpoint interrupts */
  3740. dwc2_writel(DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK |
  3741. DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK,
  3742. hsotg->regs + DIEPMSK);
  3743. dwc2_writel(DOEPMSK_SETUPMSK | DOEPMSK_AHBERRMSK |
  3744. DOEPMSK_EPDISBLDMSK | DOEPMSK_XFERCOMPLMSK,
  3745. hsotg->regs + DOEPMSK);
  3746. dwc2_writel(0, hsotg->regs + DAINTMSK);
  3747. /* Be in disconnected state until gadget is registered */
  3748. __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON);
  3749. /* setup fifos */
  3750. dev_dbg(hsotg->dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
  3751. dwc2_readl(hsotg->regs + GRXFSIZ),
  3752. dwc2_readl(hsotg->regs + GNPTXFSIZ));
  3753. dwc2_hsotg_init_fifo(hsotg);
  3754. /* keep other bits untouched (so e.g. forced modes are not lost) */
  3755. usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
  3756. usbcfg &= ~(GUSBCFG_TOUTCAL_MASK | GUSBCFG_PHYIF16 | GUSBCFG_SRPCAP |
  3757. GUSBCFG_HNPCAP | GUSBCFG_USBTRDTIM_MASK);
  3758. /* set the PLL on, remove the HNP/SRP and set the PHY */
  3759. trdtim = (hsotg->phyif == GUSBCFG_PHYIF8) ? 9 : 5;
  3760. usbcfg |= hsotg->phyif | GUSBCFG_TOUTCAL(7) |
  3761. (trdtim << GUSBCFG_USBTRDTIM_SHIFT);
  3762. dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
  3763. if (using_dma(hsotg))
  3764. __orr32(hsotg->regs + GAHBCFG, GAHBCFG_DMA_EN);
  3765. }
  3766. /**
  3767. * dwc2_hsotg_udc_start - prepare the udc for work
  3768. * @gadget: The usb gadget state
  3769. * @driver: The usb gadget driver
  3770. *
  3771. * Perform initialization to prepare udc device and driver
  3772. * to work.
  3773. */
  3774. static int dwc2_hsotg_udc_start(struct usb_gadget *gadget,
  3775. struct usb_gadget_driver *driver)
  3776. {
  3777. struct dwc2_hsotg *hsotg = to_hsotg(gadget);
  3778. unsigned long flags;
  3779. int ret;
  3780. if (!hsotg) {
  3781. pr_err("%s: called with no device\n", __func__);
  3782. return -ENODEV;
  3783. }
  3784. if (!driver) {
  3785. dev_err(hsotg->dev, "%s: no driver\n", __func__);
  3786. return -EINVAL;
  3787. }
  3788. if (!driver->setup) {
  3789. dev_err(hsotg->dev, "%s: missing entry points\n", __func__);
  3790. return -EINVAL;
  3791. }
  3792. WARN_ON(hsotg->driver);
  3793. hsotg->driver = driver;
  3794. hsotg->gadget.speed = USB_SPEED_UNKNOWN;
  3795. if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) {
  3796. ret = dwc2_lowlevel_hw_enable(hsotg);
  3797. if (ret)
  3798. goto err;
  3799. }
  3800. spin_lock_irqsave(&hsotg->lock, flags);
  3801. if (dwc2_hw_is_device(hsotg)) {
  3802. dwc2_hsotg_init(hsotg);
  3803. dwc2_hsotg_core_init_disconnected(hsotg, false);
  3804. }
  3805. hsotg->enabled = 0;
  3806. spin_unlock_irqrestore(&hsotg->lock, flags);
  3807. return 0;
  3808. err:
  3809. hsotg->driver = NULL;
  3810. return ret;
  3811. }
  3812. /**
  3813. * dwc2_hsotg_udc_stop - stop the udc
  3814. * @gadget: The usb gadget state
  3815. * @driver: The usb gadget driver
  3816. *
  3817. * Stop udc hw block and stay tunned for future transmissions
  3818. */
  3819. static int dwc2_hsotg_udc_stop(struct usb_gadget *gadget)
  3820. {
  3821. struct dwc2_hsotg *hsotg = to_hsotg(gadget);
  3822. unsigned long flags = 0;
  3823. int ep;
  3824. if (!hsotg)
  3825. return -ENODEV;
  3826. /* all endpoints should be shutdown */
  3827. for (ep = 1; ep < hsotg->num_of_eps; ep++) {
  3828. if (hsotg->eps_in[ep])
  3829. dwc2_hsotg_ep_disable(&hsotg->eps_in[ep]->ep);
  3830. if (hsotg->eps_out[ep])
  3831. dwc2_hsotg_ep_disable(&hsotg->eps_out[ep]->ep);
  3832. }
  3833. spin_lock_irqsave(&hsotg->lock, flags);
  3834. hsotg->driver = NULL;
  3835. hsotg->gadget.speed = USB_SPEED_UNKNOWN;
  3836. hsotg->enabled = 0;
  3837. spin_unlock_irqrestore(&hsotg->lock, flags);
  3838. if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
  3839. dwc2_lowlevel_hw_disable(hsotg);
  3840. return 0;
  3841. }
  3842. /**
  3843. * dwc2_hsotg_gadget_getframe - read the frame number
  3844. * @gadget: The usb gadget state
  3845. *
  3846. * Read the {micro} frame number
  3847. */
  3848. static int dwc2_hsotg_gadget_getframe(struct usb_gadget *gadget)
  3849. {
  3850. return dwc2_hsotg_read_frameno(to_hsotg(gadget));
  3851. }
  3852. /**
  3853. * dwc2_hsotg_pullup - connect/disconnect the USB PHY
  3854. * @gadget: The usb gadget state
  3855. * @is_on: Current state of the USB PHY
  3856. *
  3857. * Connect/Disconnect the USB PHY pullup
  3858. */
  3859. static int dwc2_hsotg_pullup(struct usb_gadget *gadget, int is_on)
  3860. {
  3861. struct dwc2_hsotg *hsotg = to_hsotg(gadget);
  3862. unsigned long flags = 0;
  3863. dev_dbg(hsotg->dev, "%s: is_on: %d op_state: %d\n", __func__, is_on,
  3864. hsotg->op_state);
  3865. /* Don't modify pullup state while in host mode */
  3866. if (hsotg->op_state != OTG_STATE_B_PERIPHERAL) {
  3867. hsotg->enabled = is_on;
  3868. return 0;
  3869. }
  3870. spin_lock_irqsave(&hsotg->lock, flags);
  3871. if (is_on) {
  3872. hsotg->enabled = 1;
  3873. dwc2_hsotg_core_init_disconnected(hsotg, false);
  3874. dwc2_hsotg_core_connect(hsotg);
  3875. } else {
  3876. dwc2_hsotg_core_disconnect(hsotg);
  3877. dwc2_hsotg_disconnect(hsotg);
  3878. hsotg->enabled = 0;
  3879. }
  3880. hsotg->gadget.speed = USB_SPEED_UNKNOWN;
  3881. spin_unlock_irqrestore(&hsotg->lock, flags);
  3882. return 0;
  3883. }
  3884. static int dwc2_hsotg_vbus_session(struct usb_gadget *gadget, int is_active)
  3885. {
  3886. struct dwc2_hsotg *hsotg = to_hsotg(gadget);
  3887. unsigned long flags;
  3888. dev_dbg(hsotg->dev, "%s: is_active: %d\n", __func__, is_active);
  3889. spin_lock_irqsave(&hsotg->lock, flags);
  3890. /*
  3891. * If controller is hibernated, it must exit from hibernation
  3892. * before being initialized / de-initialized
  3893. */
  3894. if (hsotg->lx_state == DWC2_L2)
  3895. dwc2_exit_hibernation(hsotg, false);
  3896. if (is_active) {
  3897. hsotg->op_state = OTG_STATE_B_PERIPHERAL;
  3898. dwc2_hsotg_core_init_disconnected(hsotg, false);
  3899. if (hsotg->enabled)
  3900. dwc2_hsotg_core_connect(hsotg);
  3901. } else {
  3902. dwc2_hsotg_core_disconnect(hsotg);
  3903. dwc2_hsotg_disconnect(hsotg);
  3904. }
  3905. spin_unlock_irqrestore(&hsotg->lock, flags);
  3906. return 0;
  3907. }
  3908. /**
  3909. * dwc2_hsotg_vbus_draw - report bMaxPower field
  3910. * @gadget: The usb gadget state
  3911. * @mA: Amount of current
  3912. *
  3913. * Report how much power the device may consume to the phy.
  3914. */
  3915. static int dwc2_hsotg_vbus_draw(struct usb_gadget *gadget, unsigned int mA)
  3916. {
  3917. struct dwc2_hsotg *hsotg = to_hsotg(gadget);
  3918. return 0;
  3919. }
  3920. static const struct usb_gadget_ops dwc2_hsotg_gadget_ops = {
  3921. .get_frame = dwc2_hsotg_gadget_getframe,
  3922. .udc_start = dwc2_hsotg_udc_start,
  3923. .udc_stop = dwc2_hsotg_udc_stop,
  3924. .pullup = dwc2_hsotg_pullup,
  3925. .vbus_session = dwc2_hsotg_vbus_session,
  3926. .vbus_draw = dwc2_hsotg_vbus_draw,
  3927. };
  3928. /**
  3929. * dwc2_hsotg_initep - initialise a single endpoint
  3930. * @hsotg: The device state.
  3931. * @hs_ep: The endpoint to be initialised.
  3932. * @epnum: The endpoint number
  3933. *
  3934. * Initialise the given endpoint (as part of the probe and device state
  3935. * creation) to give to the gadget driver. Setup the endpoint name, any
  3936. * direction information and other state that may be required.
  3937. */
  3938. static void dwc2_hsotg_initep(struct dwc2_hsotg *hsotg,
  3939. struct dwc2_hsotg_ep *hs_ep,
  3940. int epnum,
  3941. bool dir_in)
  3942. {
  3943. char *dir;
  3944. if (epnum == 0)
  3945. dir = "";
  3946. else if (dir_in)
  3947. dir = "in";
  3948. else
  3949. dir = "out";
  3950. hs_ep->dir_in = dir_in;
  3951. hs_ep->index = epnum;
  3952. snprintf(hs_ep->name, sizeof(hs_ep->name), "ep%d%s", epnum, dir);
  3953. INIT_LIST_HEAD(&hs_ep->queue);
  3954. #ifndef NO_GNU
  3955. INIT_LIST_HEAD(&hs_ep->ep.ep_list);
  3956. #else
  3957. INIT_LIST_ITEM(&hs_ep->ep.ep_list);
  3958. listSET_LIST_ITEM_OWNER(&hs_ep->ep.ep_list, &(hs_ep->ep));
  3959. #endif
  3960. /* add to the list of endpoints known by the gadget driver */
  3961. if (epnum)
  3962. list_add_tail(&hs_ep->ep.ep_list, &hsotg->gadget.ep_list);
  3963. hs_ep->parent = hsotg;
  3964. hs_ep->ep.name = hs_ep->name;
  3965. if (hsotg->params.speed == DWC2_SPEED_PARAM_LOW)
  3966. usb_ep_set_maxpacket_limit(&hs_ep->ep, 8);
  3967. else
  3968. usb_ep_set_maxpacket_limit(&hs_ep->ep,
  3969. epnum ? 1024 : EP0_MPS_LIMIT);
  3970. hs_ep->ep.ops = &dwc2_hsotg_ep_ops;
  3971. /*
  3972. * if we're using dma, we need to set the next-endpoint pointer
  3973. * to be something valid.
  3974. */
  3975. if (using_dma(hsotg)) {
  3976. u32 next = DXEPCTL_NEXTEP((epnum + 1) % 15);
  3977. if (dir_in)
  3978. dwc2_writel(next, hsotg->regs + DIEPCTL(epnum));
  3979. else
  3980. dwc2_writel(next, hsotg->regs + DOEPCTL(epnum));
  3981. }
  3982. }
  3983. /**
  3984. * dwc2_hsotg_hw_cfg - read HW configuration registers
  3985. * @param: The device state
  3986. *
  3987. * Read the USB core HW configuration registers
  3988. */
  3989. static int dwc2_hsotg_hw_cfg(struct dwc2_hsotg *hsotg)
  3990. {
  3991. u32 cfg;
  3992. u32 ep_type;
  3993. u32 i;
  3994. /* check hardware configuration */
  3995. hsotg->num_of_eps = hsotg->hw_params.num_dev_ep;
  3996. /* Add ep0 */
  3997. hsotg->num_of_eps++;
  3998. hsotg->eps_in[0] = (struct dwc2_hsotg_ep *)devm_kzalloc(hsotg->dev,
  3999. sizeof(struct dwc2_hsotg_ep),
  4000. GFP_KERNEL);
  4001. if (!hsotg->eps_in[0])
  4002. return -ENOMEM;
  4003. /* Same dwc2_hsotg_ep is used in both directions for ep0 */
  4004. hsotg->eps_out[0] = hsotg->eps_in[0];
  4005. hsotg->eps_in[0]->ep.powner = (void*)(hsotg->eps_in[0]);
  4006. hsotg->eps_out[0]->ep.powner = (void*)(hsotg->eps_out[0]);
  4007. listSET_LIST_ITEM_OWNER(&hsotg->eps_out[0]->ep.ep_list, &hsotg->eps_out[0]->ep);
  4008. vListInitialise(&(hsotg->eps_out[0]->queue));
  4009. listSET_LIST_ITEM_OWNER(&hsotg->eps_in[0]->ep.ep_list, &hsotg->eps_in[0]->ep);
  4010. vListInitialise(&(hsotg->eps_in[0]->queue));
  4011. cfg = hsotg->hw_params.dev_ep_dirs;
  4012. for (i = 1, cfg >>= 2; i < hsotg->num_of_eps; i++, cfg >>= 2) {
  4013. ep_type = cfg & 3;
  4014. /* Direction in or both */
  4015. if (!(ep_type & 2)) {
  4016. hsotg->eps_in[i] = (struct dwc2_hsotg_ep *)devm_kzalloc(hsotg->dev,
  4017. sizeof(struct dwc2_hsotg_ep), GFP_KERNEL);
  4018. if (!hsotg->eps_in[i])
  4019. return -ENOMEM;
  4020. #ifdef NO_GNU
  4021. hsotg->eps_in[i]->ep.powner = (void*)(hsotg->eps_in[i]);
  4022. listSET_LIST_ITEM_OWNER(&hsotg->eps_in[i]->ep.ep_list, &hsotg->eps_in[i]->ep);
  4023. vListInitialise(&(hsotg->eps_in[i]->queue));
  4024. #endif
  4025. }
  4026. /* Direction out or both */
  4027. if (!(ep_type & 1)) {
  4028. hsotg->eps_out[i] = (struct dwc2_hsotg_ep *)devm_kzalloc(hsotg->dev,
  4029. sizeof(struct dwc2_hsotg_ep), GFP_KERNEL);
  4030. if (!hsotg->eps_out[i])
  4031. return -ENOMEM;
  4032. #ifdef NO_GNU
  4033. hsotg->eps_out[i]->ep.powner = (void*)(hsotg->eps_out[i]);
  4034. listSET_LIST_ITEM_OWNER(&hsotg->eps_out[i]->ep.ep_list, &hsotg->eps_out[i]->ep);
  4035. vListInitialise(&(hsotg->eps_out[i]->queue));
  4036. #endif
  4037. }
  4038. }
  4039. hsotg->fifo_mem = hsotg->hw_params.total_fifo_size;
  4040. hsotg->dedicated_fifos = hsotg->hw_params.en_multiple_tx_fifo;
  4041. dev_info(hsotg->dev, "EPs: %d, %s fifos, %d entries in SPRAM\r\n",
  4042. hsotg->num_of_eps,
  4043. hsotg->dedicated_fifos ? "dedicated" : "shared",
  4044. hsotg->fifo_mem);
  4045. return 0;
  4046. }
  4047. /**
  4048. * dwc2_hsotg_dump - dump state of the udc
  4049. * @param: The device state
  4050. */
  4051. static void dwc2_hsotg_dump(struct dwc2_hsotg *hsotg)
  4052. {
  4053. #ifdef DEBUG
  4054. struct device *dev = hsotg->dev;
  4055. u32 regs = hsotg->regs;
  4056. u32 val;
  4057. int idx;
  4058. dev_info(dev, "DCFG=0x%08x, DCTL=0x%08x, DIEPMSK=%08x\n",
  4059. dwc2_readl(regs + DCFG), dwc2_readl(regs + DCTL),
  4060. dwc2_readl(regs + DIEPMSK));
  4061. dev_info(dev, "GAHBCFG=0x%08x, GHWCFG1=0x%08x\n",
  4062. dwc2_readl(regs + GAHBCFG), dwc2_readl(regs + GHWCFG1));
  4063. dev_info(dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
  4064. dwc2_readl(regs + GRXFSIZ), dwc2_readl(regs + GNPTXFSIZ));
  4065. /* show periodic fifo settings */
  4066. for (idx = 1; idx < hsotg->num_of_eps; idx++) {
  4067. val = dwc2_readl(regs + DPTXFSIZN(idx));
  4068. dev_info(dev, "DPTx[%d] FSize=%d, StAddr=0x%08x\n", idx,
  4069. val >> FIFOSIZE_DEPTH_SHIFT,
  4070. val & FIFOSIZE_STARTADDR_MASK);
  4071. }
  4072. for (idx = 0; idx < hsotg->num_of_eps; idx++) {
  4073. dev_info(dev,
  4074. "ep%d-in: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n", idx,
  4075. dwc2_readl(regs + DIEPCTL(idx)),
  4076. dwc2_readl(regs + DIEPTSIZ(idx)),
  4077. dwc2_readl(regs + DIEPDMA(idx)));
  4078. val = dwc2_readl(regs + DOEPCTL(idx));
  4079. dev_info(dev,
  4080. "ep%d-out: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n",
  4081. idx, dwc2_readl(regs + DOEPCTL(idx)),
  4082. dwc2_readl(regs + DOEPTSIZ(idx)),
  4083. dwc2_readl(regs + DOEPDMA(idx)));
  4084. }
  4085. dev_info(dev, "DVBUSDIS=0x%08x, DVBUSPULSE=%08x\n",
  4086. dwc2_readl(regs + DVBUSDIS), dwc2_readl(regs + DVBUSPULSE));
  4087. #endif
  4088. }
  4089. /**
  4090. * dwc2_gadget_init - init function for gadget
  4091. * @dwc2: The data structure for the DWC2 driver.
  4092. * @irq: The IRQ number for the controller.
  4093. */
  4094. void dwc2_hsotg_init_wq_msg(struct dwc2_hsotg *hsotg);
  4095. int dwc2_gadget_init(struct dwc2_hsotg *hsotg, int irq)
  4096. {
  4097. struct device *dev = hsotg->dev;
  4098. int epnum;
  4099. int ret, i;
  4100. USB_UNUSED(dev);
  4101. /* Dump fifo information */
  4102. dev_dbg(dev, "NonPeriodic TXFIFO size: %d\n",
  4103. hsotg->params.g_np_tx_fifo_size);
  4104. dev_info(dev, "RXFIFO size: %d\n", hsotg->params.g_rx_fifo_size);
  4105. #ifdef NO_GNU
  4106. hsotg->gadget.powner = (void*)hsotg;
  4107. #endif
  4108. hsotg->gadget.max_speed = USB_SPEED_HIGH;
  4109. hsotg->gadget.ops = &dwc2_hsotg_gadget_ops;
  4110. #ifndef NO_GNU
  4111. hsotg->gadget.name = dev_name(dev);
  4112. #else
  4113. hsotg->gadget.name = "gadget";
  4114. #endif
  4115. if (hsotg->dr_mode == USB_DR_MODE_OTG)
  4116. hsotg->gadget.is_otg = 1;
  4117. else if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
  4118. hsotg->op_state = OTG_STATE_B_PERIPHERAL;
  4119. dwc2_hsotg_init_wq_msg(hsotg);
  4120. ret = dwc2_hsotg_hw_cfg(hsotg);
  4121. if (ret) {
  4122. dev_err(hsotg->dev, "Hardware configuration failed: %d\n", ret);
  4123. return ret;
  4124. }
  4125. hsotg->ctrl_buff = (void*)devm_kzalloc(hsotg->dev,
  4126. DWC2_CTRL_BUFF_SIZE, GFP_KERNEL);
  4127. if (!hsotg->ctrl_buff)
  4128. return -ENOMEM;
  4129. hsotg->ep0_buff = (void*)devm_kzalloc(hsotg->dev,
  4130. DWC2_CTRL_BUFF_SIZE, GFP_KERNEL);
  4131. if (!hsotg->ep0_buff)
  4132. return -ENOMEM;
  4133. if (using_desc_dma(hsotg)) {
  4134. ret = dwc2_gadget_alloc_ctrl_desc_chains(hsotg);
  4135. if (ret < 0)
  4136. return ret;
  4137. }
  4138. /* hsotg->num_of_eps holds number of EPs other than ep0 */
  4139. if (hsotg->num_of_eps == 0) {
  4140. dev_err(dev, "wrong number of EPs (zero)\n");
  4141. return -EINVAL;
  4142. }
  4143. /* setup endpoint information */
  4144. INIT_LIST_HEAD(&hsotg->gadget.ep_list);
  4145. hsotg->gadget.ep0 = &hsotg->eps_out[0]->ep;
  4146. vListInitialise(&free_request_list);
  4147. free_list_lock = xSemaphoreCreateMutex();
  4148. free_req = (struct dwc2_hsotg_req*)kzalloc(sizeof(struct dwc2_hsotg_req) * 64, 0);
  4149. xSemaphoreTake(free_list_lock, portMAX_DELAY);
  4150. for (i = 0; i < 64; i++) {
  4151. vListInitialiseItem(&free_req[i].free_list_entry);
  4152. vListInsertEnd(&free_request_list, &free_req[i].free_list_entry);
  4153. listSET_LIST_ITEM_OWNER(&free_req[i].free_list_entry, &free_req[i]);
  4154. }
  4155. xSemaphoreGive(free_list_lock);
  4156. /* allocate EP0 request */
  4157. hsotg->ctrl_req = dwc2_hsotg_ep_alloc_request(&hsotg->eps_out[0]->ep,
  4158. GFP_KERNEL);
  4159. if (!hsotg->ctrl_req) {
  4160. dev_err(dev, "failed to allocate ctrl req\n");
  4161. return -ENOMEM;
  4162. }
  4163. /* initialise the endpoints now the core has been initialised */
  4164. for (epnum = 0; epnum < hsotg->num_of_eps; epnum++) {//5
  4165. if (hsotg->eps_in[epnum] && (epnum % 2))
  4166. dwc2_hsotg_initep(hsotg, hsotg->eps_in[epnum],
  4167. epnum, 1);
  4168. if (hsotg->eps_out[epnum] && (!(epnum % 2)))
  4169. dwc2_hsotg_initep(hsotg, hsotg->eps_out[epnum],
  4170. epnum, 0);
  4171. }
  4172. dwc2_hsotg_dump(hsotg);
  4173. hsotg->enumtimer = xTimerCreate( "EnumTimer",
  4174. pdMS_TO_TICKS(1500), /* The period of the software timer in ticks. */
  4175. pdFALSE, /* xAutoReload is set to pdFALSE, so this is a one-shot timer. */
  4176. (void*)hsotg, /* The timer's ID is not used. */
  4177. prvEnumTimerCallback );
  4178. return 0;
  4179. }
  4180. /**
  4181. * dwc2_hsotg_remove - remove function for hsotg driver
  4182. * @pdev: The platform information for the driver
  4183. */
  4184. int dwc2_hsotg_remove(struct dwc2_hsotg *hsotg)
  4185. {
  4186. dwc2_hsotg_ep_free_request(&hsotg->eps_out[0]->ep, hsotg->ctrl_req);
  4187. return 0;
  4188. }
  4189. /**
  4190. * dwc2_backup_device_registers() - Backup controller device registers.
  4191. * When suspending usb bus, registers needs to be backuped
  4192. * if controller power is disabled once suspended.
  4193. *
  4194. * @hsotg: Programming view of the DWC_otg controller
  4195. */
  4196. int dwc2_backup_device_registers(struct dwc2_hsotg *hsotg)
  4197. {
  4198. struct dwc2_dregs_backup *dr;
  4199. int i;
  4200. dev_dbg(hsotg->dev, "%s\n", __func__);
  4201. /* Backup dev regs */
  4202. dr = &hsotg->dr_backup;
  4203. dr->dcfg = dwc2_readl(hsotg->regs + DCFG);
  4204. dr->dctl = dwc2_readl(hsotg->regs + DCTL);
  4205. dr->daintmsk = dwc2_readl(hsotg->regs + DAINTMSK);
  4206. dr->diepmsk = dwc2_readl(hsotg->regs + DIEPMSK);
  4207. dr->doepmsk = dwc2_readl(hsotg->regs + DOEPMSK);
  4208. for (i = 0; i < hsotg->num_of_eps; i++) {
  4209. /* Backup IN EPs */
  4210. dr->diepctl[i] = dwc2_readl(hsotg->regs + DIEPCTL(i));
  4211. /* Ensure DATA PID is correctly configured */
  4212. if (dr->diepctl[i] & DXEPCTL_DPID)
  4213. dr->diepctl[i] |= DXEPCTL_SETD1PID;
  4214. else
  4215. dr->diepctl[i] |= DXEPCTL_SETD0PID;
  4216. dr->dieptsiz[i] = dwc2_readl(hsotg->regs + DIEPTSIZ(i));
  4217. dr->diepdma[i] = dwc2_readl(hsotg->regs + DIEPDMA(i));
  4218. /* Backup OUT EPs */
  4219. dr->doepctl[i] = dwc2_readl(hsotg->regs + DOEPCTL(i));
  4220. /* Ensure DATA PID is correctly configured */
  4221. if (dr->doepctl[i] & DXEPCTL_DPID)
  4222. dr->doepctl[i] |= DXEPCTL_SETD1PID;
  4223. else
  4224. dr->doepctl[i] |= DXEPCTL_SETD0PID;
  4225. dr->doeptsiz[i] = dwc2_readl(hsotg->regs + DOEPTSIZ(i));
  4226. dr->doepdma[i] = dwc2_readl(hsotg->regs + DOEPDMA(i));
  4227. }
  4228. dr->valid = true;
  4229. return 0;
  4230. }
  4231. /**
  4232. * dwc2_restore_device_registers() - Restore controller device registers.
  4233. * When resuming usb bus, device registers needs to be restored
  4234. * if controller power were disabled.
  4235. *
  4236. * @hsotg: Programming view of the DWC_otg controller
  4237. */
  4238. int dwc2_restore_device_registers(struct dwc2_hsotg *hsotg)
  4239. {
  4240. struct dwc2_dregs_backup *dr;
  4241. u32 dctl;
  4242. int i;
  4243. dev_dbg(hsotg->dev, "%s\n", __func__);
  4244. /* Restore dev regs */
  4245. dr = &hsotg->dr_backup;
  4246. if (!dr->valid) {
  4247. dev_err(hsotg->dev, "%s: no device registers to restore\n",
  4248. __func__);
  4249. return -EINVAL;
  4250. }
  4251. dr->valid = false;
  4252. dwc2_writel(dr->dcfg, hsotg->regs + DCFG);
  4253. dwc2_writel(dr->dctl, hsotg->regs + DCTL);
  4254. dwc2_writel(dr->daintmsk, hsotg->regs + DAINTMSK);
  4255. dwc2_writel(dr->diepmsk, hsotg->regs + DIEPMSK);
  4256. dwc2_writel(dr->doepmsk, hsotg->regs + DOEPMSK);
  4257. for (i = 0; i < hsotg->num_of_eps; i++) {
  4258. /* Restore IN EPs */
  4259. dwc2_writel(dr->diepctl[i], hsotg->regs + DIEPCTL(i));
  4260. dwc2_writel(dr->dieptsiz[i], hsotg->regs + DIEPTSIZ(i));
  4261. dwc2_writel(dr->diepdma[i], hsotg->regs + DIEPDMA(i));
  4262. /* Restore OUT EPs */
  4263. dwc2_writel(dr->doepctl[i], hsotg->regs + DOEPCTL(i));
  4264. dwc2_writel(dr->doeptsiz[i], hsotg->regs + DOEPTSIZ(i));
  4265. dwc2_writel(dr->doepdma[i], hsotg->regs + DOEPDMA(i));
  4266. }
  4267. /* Set the Power-On Programming done bit */
  4268. dctl = dwc2_readl(hsotg->regs + DCTL);
  4269. dctl |= DCTL_PWRONPRGDONE;
  4270. dwc2_writel(dctl, hsotg->regs + DCTL);
  4271. return 0;
  4272. }