gadget.c 132 KB

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