gadget.c 133 KB

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