gadget.c 106 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133
  1. /**
  2. * Copyright (c) 2011 Samsung Electronics Co., Ltd.
  3. * http://www.samsung.com
  4. *
  5. * Copyright 2008 Openmoko, Inc.
  6. * Copyright 2008 Simtec Electronics
  7. * Ben Dooks <ben@simtec.co.uk>
  8. * http://armlinux.simtec.co.uk/
  9. *
  10. * S3C USB2.0 High-speed / OtG driver
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2 as
  14. * published by the Free Software Foundation.
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/dma-mapping.h>
  22. #include <linux/mutex.h>
  23. #include <linux/seq_file.h>
  24. #include <linux/delay.h>
  25. #include <linux/io.h>
  26. #include <linux/slab.h>
  27. #include <linux/of_platform.h>
  28. #include <linux/usb/ch9.h>
  29. #include <linux/usb/gadget.h>
  30. #include <linux/usb/phy.h>
  31. #include "core.h"
  32. #include "hw.h"
  33. /* conversion functions */
  34. static inline struct dwc2_hsotg_req *our_req(struct usb_request *req)
  35. {
  36. return container_of(req, struct dwc2_hsotg_req, req);
  37. }
  38. static inline struct dwc2_hsotg_ep *our_ep(struct usb_ep *ep)
  39. {
  40. return container_of(ep, struct dwc2_hsotg_ep, ep);
  41. }
  42. static inline struct dwc2_hsotg *to_hsotg(struct usb_gadget *gadget)
  43. {
  44. return container_of(gadget, struct dwc2_hsotg, gadget);
  45. }
  46. static inline void __orr32(void __iomem *ptr, u32 val)
  47. {
  48. dwc2_writel(dwc2_readl(ptr) | val, ptr);
  49. }
  50. static inline void __bic32(void __iomem *ptr, u32 val)
  51. {
  52. dwc2_writel(dwc2_readl(ptr) & ~val, ptr);
  53. }
  54. static inline struct dwc2_hsotg_ep *index_to_ep(struct dwc2_hsotg *hsotg,
  55. u32 ep_index, u32 dir_in)
  56. {
  57. if (dir_in)
  58. return hsotg->eps_in[ep_index];
  59. else
  60. return hsotg->eps_out[ep_index];
  61. }
  62. /* forward declaration of functions */
  63. static void dwc2_hsotg_dump(struct dwc2_hsotg *hsotg);
  64. /**
  65. * using_dma - return the DMA status of the driver.
  66. * @hsotg: The driver state.
  67. *
  68. * Return true if we're using DMA.
  69. *
  70. * Currently, we have the DMA support code worked into everywhere
  71. * that needs it, but the AMBA DMA implementation in the hardware can
  72. * only DMA from 32bit aligned addresses. This means that gadgets such
  73. * as the CDC Ethernet cannot work as they often pass packets which are
  74. * not 32bit aligned.
  75. *
  76. * Unfortunately the choice to use DMA or not is global to the controller
  77. * and seems to be only settable when the controller is being put through
  78. * a core reset. This means we either need to fix the gadgets to take
  79. * account of DMA alignment, or add bounce buffers (yuerk).
  80. *
  81. * g_using_dma is set depending on dts flag.
  82. */
  83. static inline bool using_dma(struct dwc2_hsotg *hsotg)
  84. {
  85. return hsotg->g_using_dma;
  86. }
  87. /**
  88. * dwc2_gadget_incr_frame_num - Increments the targeted frame number.
  89. * @hs_ep: The endpoint
  90. * @increment: The value to increment by
  91. *
  92. * This function will also check if the frame number overruns DSTS_SOFFN_LIMIT.
  93. * If an overrun occurs it will wrap the value and set the frame_overrun flag.
  94. */
  95. static inline void dwc2_gadget_incr_frame_num(struct dwc2_hsotg_ep *hs_ep)
  96. {
  97. hs_ep->target_frame += hs_ep->interval;
  98. if (hs_ep->target_frame > DSTS_SOFFN_LIMIT) {
  99. hs_ep->frame_overrun = 1;
  100. hs_ep->target_frame &= DSTS_SOFFN_LIMIT;
  101. } else {
  102. hs_ep->frame_overrun = 0;
  103. }
  104. }
  105. /**
  106. * dwc2_hsotg_en_gsint - enable one or more of the general interrupt
  107. * @hsotg: The device state
  108. * @ints: A bitmask of the interrupts to enable
  109. */
  110. static void dwc2_hsotg_en_gsint(struct dwc2_hsotg *hsotg, u32 ints)
  111. {
  112. u32 gsintmsk = dwc2_readl(hsotg->regs + GINTMSK);
  113. u32 new_gsintmsk;
  114. new_gsintmsk = gsintmsk | ints;
  115. if (new_gsintmsk != gsintmsk) {
  116. dev_dbg(hsotg->dev, "gsintmsk now 0x%08x\n", new_gsintmsk);
  117. dwc2_writel(new_gsintmsk, hsotg->regs + GINTMSK);
  118. }
  119. }
  120. /**
  121. * dwc2_hsotg_disable_gsint - disable one or more of the general interrupt
  122. * @hsotg: The device state
  123. * @ints: A bitmask of the interrupts to enable
  124. */
  125. static void dwc2_hsotg_disable_gsint(struct dwc2_hsotg *hsotg, u32 ints)
  126. {
  127. u32 gsintmsk = dwc2_readl(hsotg->regs + GINTMSK);
  128. u32 new_gsintmsk;
  129. new_gsintmsk = gsintmsk & ~ints;
  130. if (new_gsintmsk != gsintmsk)
  131. dwc2_writel(new_gsintmsk, hsotg->regs + GINTMSK);
  132. }
  133. /**
  134. * dwc2_hsotg_ctrl_epint - enable/disable an endpoint irq
  135. * @hsotg: The device state
  136. * @ep: The endpoint index
  137. * @dir_in: True if direction is in.
  138. * @en: The enable value, true to enable
  139. *
  140. * Set or clear the mask for an individual endpoint's interrupt
  141. * request.
  142. */
  143. static void dwc2_hsotg_ctrl_epint(struct dwc2_hsotg *hsotg,
  144. unsigned int ep, unsigned int dir_in,
  145. unsigned int en)
  146. {
  147. unsigned long flags;
  148. u32 bit = 1 << ep;
  149. u32 daint;
  150. if (!dir_in)
  151. bit <<= 16;
  152. local_irq_save(flags);
  153. daint = dwc2_readl(hsotg->regs + DAINTMSK);
  154. if (en)
  155. daint |= bit;
  156. else
  157. daint &= ~bit;
  158. dwc2_writel(daint, hsotg->regs + DAINTMSK);
  159. local_irq_restore(flags);
  160. }
  161. /**
  162. * dwc2_hsotg_init_fifo - initialise non-periodic FIFOs
  163. * @hsotg: The device instance.
  164. */
  165. static void dwc2_hsotg_init_fifo(struct dwc2_hsotg *hsotg)
  166. {
  167. unsigned int ep;
  168. unsigned int addr;
  169. int timeout;
  170. u32 val;
  171. /* Reset fifo map if not correctly cleared during previous session */
  172. WARN_ON(hsotg->fifo_map);
  173. hsotg->fifo_map = 0;
  174. /* set RX/NPTX FIFO sizes */
  175. dwc2_writel(hsotg->g_rx_fifo_sz, hsotg->regs + GRXFSIZ);
  176. dwc2_writel((hsotg->g_rx_fifo_sz << FIFOSIZE_STARTADDR_SHIFT) |
  177. (hsotg->g_np_g_tx_fifo_sz << FIFOSIZE_DEPTH_SHIFT),
  178. hsotg->regs + GNPTXFSIZ);
  179. /*
  180. * arange all the rest of the TX FIFOs, as some versions of this
  181. * block have overlapping default addresses. This also ensures
  182. * that if the settings have been changed, then they are set to
  183. * known values.
  184. */
  185. /* start at the end of the GNPTXFSIZ, rounded up */
  186. addr = hsotg->g_rx_fifo_sz + hsotg->g_np_g_tx_fifo_sz;
  187. /*
  188. * Configure fifos sizes from provided configuration and assign
  189. * them to endpoints dynamically according to maxpacket size value of
  190. * given endpoint.
  191. */
  192. for (ep = 1; ep < MAX_EPS_CHANNELS; ep++) {
  193. if (!hsotg->g_tx_fifo_sz[ep])
  194. continue;
  195. val = addr;
  196. val |= hsotg->g_tx_fifo_sz[ep] << FIFOSIZE_DEPTH_SHIFT;
  197. WARN_ONCE(addr + hsotg->g_tx_fifo_sz[ep] > hsotg->fifo_mem,
  198. "insufficient fifo memory");
  199. addr += hsotg->g_tx_fifo_sz[ep];
  200. dwc2_writel(val, hsotg->regs + DPTXFSIZN(ep));
  201. }
  202. /*
  203. * according to p428 of the design guide, we need to ensure that
  204. * all fifos are flushed before continuing
  205. */
  206. dwc2_writel(GRSTCTL_TXFNUM(0x10) | GRSTCTL_TXFFLSH |
  207. GRSTCTL_RXFFLSH, hsotg->regs + GRSTCTL);
  208. /* wait until the fifos are both flushed */
  209. timeout = 100;
  210. while (1) {
  211. val = dwc2_readl(hsotg->regs + GRSTCTL);
  212. if ((val & (GRSTCTL_TXFFLSH | GRSTCTL_RXFFLSH)) == 0)
  213. break;
  214. if (--timeout == 0) {
  215. dev_err(hsotg->dev,
  216. "%s: timeout flushing fifos (GRSTCTL=%08x)\n",
  217. __func__, val);
  218. break;
  219. }
  220. udelay(1);
  221. }
  222. dev_dbg(hsotg->dev, "FIFOs reset, timeout at %d\n", timeout);
  223. }
  224. /**
  225. * @ep: USB endpoint to allocate request for.
  226. * @flags: Allocation flags
  227. *
  228. * Allocate a new USB request structure appropriate for the specified endpoint
  229. */
  230. static struct usb_request *dwc2_hsotg_ep_alloc_request(struct usb_ep *ep,
  231. gfp_t flags)
  232. {
  233. struct dwc2_hsotg_req *req;
  234. req = kzalloc(sizeof(struct dwc2_hsotg_req), flags);
  235. if (!req)
  236. return NULL;
  237. INIT_LIST_HEAD(&req->queue);
  238. return &req->req;
  239. }
  240. /**
  241. * is_ep_periodic - return true if the endpoint is in periodic mode.
  242. * @hs_ep: The endpoint to query.
  243. *
  244. * Returns true if the endpoint is in periodic mode, meaning it is being
  245. * used for an Interrupt or ISO transfer.
  246. */
  247. static inline int is_ep_periodic(struct dwc2_hsotg_ep *hs_ep)
  248. {
  249. return hs_ep->periodic;
  250. }
  251. /**
  252. * dwc2_hsotg_unmap_dma - unmap the DMA memory being used for the request
  253. * @hsotg: The device state.
  254. * @hs_ep: The endpoint for the request
  255. * @hs_req: The request being processed.
  256. *
  257. * This is the reverse of dwc2_hsotg_map_dma(), called for the completion
  258. * of a request to ensure the buffer is ready for access by the caller.
  259. */
  260. static void dwc2_hsotg_unmap_dma(struct dwc2_hsotg *hsotg,
  261. struct dwc2_hsotg_ep *hs_ep,
  262. struct dwc2_hsotg_req *hs_req)
  263. {
  264. struct usb_request *req = &hs_req->req;
  265. /* ignore this if we're not moving any data */
  266. if (hs_req->req.length == 0)
  267. return;
  268. usb_gadget_unmap_request(&hsotg->gadget, req, hs_ep->dir_in);
  269. }
  270. /**
  271. * dwc2_hsotg_write_fifo - write packet Data to the TxFIFO
  272. * @hsotg: The controller state.
  273. * @hs_ep: The endpoint we're going to write for.
  274. * @hs_req: The request to write data for.
  275. *
  276. * This is called when the TxFIFO has some space in it to hold a new
  277. * transmission and we have something to give it. The actual setup of
  278. * the data size is done elsewhere, so all we have to do is to actually
  279. * write the data.
  280. *
  281. * The return value is zero if there is more space (or nothing was done)
  282. * otherwise -ENOSPC is returned if the FIFO space was used up.
  283. *
  284. * This routine is only needed for PIO
  285. */
  286. static int dwc2_hsotg_write_fifo(struct dwc2_hsotg *hsotg,
  287. struct dwc2_hsotg_ep *hs_ep,
  288. struct dwc2_hsotg_req *hs_req)
  289. {
  290. bool periodic = is_ep_periodic(hs_ep);
  291. u32 gnptxsts = dwc2_readl(hsotg->regs + GNPTXSTS);
  292. int buf_pos = hs_req->req.actual;
  293. int to_write = hs_ep->size_loaded;
  294. void *data;
  295. int can_write;
  296. int pkt_round;
  297. int max_transfer;
  298. to_write -= (buf_pos - hs_ep->last_load);
  299. /* if there's nothing to write, get out early */
  300. if (to_write == 0)
  301. return 0;
  302. if (periodic && !hsotg->dedicated_fifos) {
  303. u32 epsize = dwc2_readl(hsotg->regs + DIEPTSIZ(hs_ep->index));
  304. int size_left;
  305. int size_done;
  306. /*
  307. * work out how much data was loaded so we can calculate
  308. * how much data is left in the fifo.
  309. */
  310. size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
  311. /*
  312. * if shared fifo, we cannot write anything until the
  313. * previous data has been completely sent.
  314. */
  315. if (hs_ep->fifo_load != 0) {
  316. dwc2_hsotg_en_gsint(hsotg, GINTSTS_PTXFEMP);
  317. return -ENOSPC;
  318. }
  319. dev_dbg(hsotg->dev, "%s: left=%d, load=%d, fifo=%d, size %d\n",
  320. __func__, size_left,
  321. hs_ep->size_loaded, hs_ep->fifo_load, hs_ep->fifo_size);
  322. /* how much of the data has moved */
  323. size_done = hs_ep->size_loaded - size_left;
  324. /* how much data is left in the fifo */
  325. can_write = hs_ep->fifo_load - size_done;
  326. dev_dbg(hsotg->dev, "%s: => can_write1=%d\n",
  327. __func__, can_write);
  328. can_write = hs_ep->fifo_size - can_write;
  329. dev_dbg(hsotg->dev, "%s: => can_write2=%d\n",
  330. __func__, can_write);
  331. if (can_write <= 0) {
  332. dwc2_hsotg_en_gsint(hsotg, GINTSTS_PTXFEMP);
  333. return -ENOSPC;
  334. }
  335. } else if (hsotg->dedicated_fifos && hs_ep->index != 0) {
  336. can_write = dwc2_readl(hsotg->regs + DTXFSTS(hs_ep->index));
  337. can_write &= 0xffff;
  338. can_write *= 4;
  339. } else {
  340. if (GNPTXSTS_NP_TXQ_SPC_AVAIL_GET(gnptxsts) == 0) {
  341. dev_dbg(hsotg->dev,
  342. "%s: no queue slots available (0x%08x)\n",
  343. __func__, gnptxsts);
  344. dwc2_hsotg_en_gsint(hsotg, GINTSTS_NPTXFEMP);
  345. return -ENOSPC;
  346. }
  347. can_write = GNPTXSTS_NP_TXF_SPC_AVAIL_GET(gnptxsts);
  348. can_write *= 4; /* fifo size is in 32bit quantities. */
  349. }
  350. max_transfer = hs_ep->ep.maxpacket * hs_ep->mc;
  351. dev_dbg(hsotg->dev, "%s: GNPTXSTS=%08x, can=%d, to=%d, max_transfer %d\n",
  352. __func__, gnptxsts, can_write, to_write, max_transfer);
  353. /*
  354. * limit to 512 bytes of data, it seems at least on the non-periodic
  355. * FIFO, requests of >512 cause the endpoint to get stuck with a
  356. * fragment of the end of the transfer in it.
  357. */
  358. if (can_write > 512 && !periodic)
  359. can_write = 512;
  360. /*
  361. * limit the write to one max-packet size worth of data, but allow
  362. * the transfer to return that it did not run out of fifo space
  363. * doing it.
  364. */
  365. if (to_write > max_transfer) {
  366. to_write = max_transfer;
  367. /* it's needed only when we do not use dedicated fifos */
  368. if (!hsotg->dedicated_fifos)
  369. dwc2_hsotg_en_gsint(hsotg,
  370. periodic ? GINTSTS_PTXFEMP :
  371. GINTSTS_NPTXFEMP);
  372. }
  373. /* see if we can write data */
  374. if (to_write > can_write) {
  375. to_write = can_write;
  376. pkt_round = to_write % max_transfer;
  377. /*
  378. * Round the write down to an
  379. * exact number of packets.
  380. *
  381. * Note, we do not currently check to see if we can ever
  382. * write a full packet or not to the FIFO.
  383. */
  384. if (pkt_round)
  385. to_write -= pkt_round;
  386. /*
  387. * enable correct FIFO interrupt to alert us when there
  388. * is more room left.
  389. */
  390. /* it's needed only when we do not use dedicated fifos */
  391. if (!hsotg->dedicated_fifos)
  392. dwc2_hsotg_en_gsint(hsotg,
  393. periodic ? GINTSTS_PTXFEMP :
  394. GINTSTS_NPTXFEMP);
  395. }
  396. dev_dbg(hsotg->dev, "write %d/%d, can_write %d, done %d\n",
  397. to_write, hs_req->req.length, can_write, buf_pos);
  398. if (to_write <= 0)
  399. return -ENOSPC;
  400. hs_req->req.actual = buf_pos + to_write;
  401. hs_ep->total_data += to_write;
  402. if (periodic)
  403. hs_ep->fifo_load += to_write;
  404. to_write = DIV_ROUND_UP(to_write, 4);
  405. data = hs_req->req.buf + buf_pos;
  406. iowrite32_rep(hsotg->regs + EPFIFO(hs_ep->index), data, to_write);
  407. return (to_write >= can_write) ? -ENOSPC : 0;
  408. }
  409. /**
  410. * get_ep_limit - get the maximum data legnth for this endpoint
  411. * @hs_ep: The endpoint
  412. *
  413. * Return the maximum data that can be queued in one go on a given endpoint
  414. * so that transfers that are too long can be split.
  415. */
  416. static unsigned get_ep_limit(struct dwc2_hsotg_ep *hs_ep)
  417. {
  418. int index = hs_ep->index;
  419. unsigned maxsize;
  420. unsigned maxpkt;
  421. if (index != 0) {
  422. maxsize = DXEPTSIZ_XFERSIZE_LIMIT + 1;
  423. maxpkt = DXEPTSIZ_PKTCNT_LIMIT + 1;
  424. } else {
  425. maxsize = 64+64;
  426. if (hs_ep->dir_in)
  427. maxpkt = DIEPTSIZ0_PKTCNT_LIMIT + 1;
  428. else
  429. maxpkt = 2;
  430. }
  431. /* we made the constant loading easier above by using +1 */
  432. maxpkt--;
  433. maxsize--;
  434. /*
  435. * constrain by packet count if maxpkts*pktsize is greater
  436. * than the length register size.
  437. */
  438. if ((maxpkt * hs_ep->ep.maxpacket) < maxsize)
  439. maxsize = maxpkt * hs_ep->ep.maxpacket;
  440. return maxsize;
  441. }
  442. /**
  443. * dwc2_hsotg_read_frameno - read current frame number
  444. * @hsotg: The device instance
  445. *
  446. * Return the current frame number
  447. */
  448. static u32 dwc2_hsotg_read_frameno(struct dwc2_hsotg *hsotg)
  449. {
  450. u32 dsts;
  451. dsts = dwc2_readl(hsotg->regs + DSTS);
  452. dsts &= DSTS_SOFFN_MASK;
  453. dsts >>= DSTS_SOFFN_SHIFT;
  454. return dsts;
  455. }
  456. /**
  457. * dwc2_hsotg_start_req - start a USB request from an endpoint's queue
  458. * @hsotg: The controller state.
  459. * @hs_ep: The endpoint to process a request for
  460. * @hs_req: The request to start.
  461. * @continuing: True if we are doing more for the current request.
  462. *
  463. * Start the given request running by setting the endpoint registers
  464. * appropriately, and writing any data to the FIFOs.
  465. */
  466. static void dwc2_hsotg_start_req(struct dwc2_hsotg *hsotg,
  467. struct dwc2_hsotg_ep *hs_ep,
  468. struct dwc2_hsotg_req *hs_req,
  469. bool continuing)
  470. {
  471. struct usb_request *ureq = &hs_req->req;
  472. int index = hs_ep->index;
  473. int dir_in = hs_ep->dir_in;
  474. u32 epctrl_reg;
  475. u32 epsize_reg;
  476. u32 epsize;
  477. u32 ctrl;
  478. unsigned length;
  479. unsigned packets;
  480. unsigned maxreq;
  481. if (index != 0) {
  482. if (hs_ep->req && !continuing) {
  483. dev_err(hsotg->dev, "%s: active request\n", __func__);
  484. WARN_ON(1);
  485. return;
  486. } else if (hs_ep->req != hs_req && continuing) {
  487. dev_err(hsotg->dev,
  488. "%s: continue different req\n", __func__);
  489. WARN_ON(1);
  490. return;
  491. }
  492. }
  493. epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
  494. epsize_reg = dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
  495. dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x, ep %d, dir %s\n",
  496. __func__, dwc2_readl(hsotg->regs + epctrl_reg), index,
  497. hs_ep->dir_in ? "in" : "out");
  498. /* If endpoint is stalled, we will restart request later */
  499. ctrl = dwc2_readl(hsotg->regs + epctrl_reg);
  500. if (index && ctrl & DXEPCTL_STALL) {
  501. dev_warn(hsotg->dev, "%s: ep%d is stalled\n", __func__, index);
  502. return;
  503. }
  504. length = ureq->length - ureq->actual;
  505. dev_dbg(hsotg->dev, "ureq->length:%d ureq->actual:%d\n",
  506. ureq->length, ureq->actual);
  507. maxreq = get_ep_limit(hs_ep);
  508. if (length > maxreq) {
  509. int round = maxreq % hs_ep->ep.maxpacket;
  510. dev_dbg(hsotg->dev, "%s: length %d, max-req %d, r %d\n",
  511. __func__, length, maxreq, round);
  512. /* round down to multiple of packets */
  513. if (round)
  514. maxreq -= round;
  515. length = maxreq;
  516. }
  517. if (length)
  518. packets = DIV_ROUND_UP(length, hs_ep->ep.maxpacket);
  519. else
  520. packets = 1; /* send one packet if length is zero. */
  521. if (hs_ep->isochronous && length > (hs_ep->mc * hs_ep->ep.maxpacket)) {
  522. dev_err(hsotg->dev, "req length > maxpacket*mc\n");
  523. return;
  524. }
  525. if (dir_in && index != 0)
  526. if (hs_ep->isochronous)
  527. epsize = DXEPTSIZ_MC(packets);
  528. else
  529. epsize = DXEPTSIZ_MC(1);
  530. else
  531. epsize = 0;
  532. /*
  533. * zero length packet should be programmed on its own and should not
  534. * be counted in DIEPTSIZ.PktCnt with other packets.
  535. */
  536. if (dir_in && ureq->zero && !continuing) {
  537. /* Test if zlp is actually required. */
  538. if ((ureq->length >= hs_ep->ep.maxpacket) &&
  539. !(ureq->length % hs_ep->ep.maxpacket))
  540. hs_ep->send_zlp = 1;
  541. }
  542. epsize |= DXEPTSIZ_PKTCNT(packets);
  543. epsize |= DXEPTSIZ_XFERSIZE(length);
  544. dev_dbg(hsotg->dev, "%s: %d@%d/%d, 0x%08x => 0x%08x\n",
  545. __func__, packets, length, ureq->length, epsize, epsize_reg);
  546. /* store the request as the current one we're doing */
  547. hs_ep->req = hs_req;
  548. /* write size / packets */
  549. dwc2_writel(epsize, hsotg->regs + epsize_reg);
  550. if (using_dma(hsotg) && !continuing) {
  551. unsigned int dma_reg;
  552. /*
  553. * write DMA address to control register, buffer already
  554. * synced by dwc2_hsotg_ep_queue().
  555. */
  556. dma_reg = dir_in ? DIEPDMA(index) : DOEPDMA(index);
  557. dwc2_writel(ureq->dma, hsotg->regs + dma_reg);
  558. dev_dbg(hsotg->dev, "%s: %pad => 0x%08x\n",
  559. __func__, &ureq->dma, dma_reg);
  560. }
  561. if (hs_ep->isochronous && hs_ep->interval == 1) {
  562. hs_ep->target_frame = dwc2_hsotg_read_frameno(hsotg);
  563. dwc2_gadget_incr_frame_num(hs_ep);
  564. if (hs_ep->target_frame & 0x1)
  565. ctrl |= DXEPCTL_SETODDFR;
  566. else
  567. ctrl |= DXEPCTL_SETEVENFR;
  568. }
  569. ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */
  570. dev_dbg(hsotg->dev, "ep0 state:%d\n", hsotg->ep0_state);
  571. /* For Setup request do not clear NAK */
  572. if (!(index == 0 && hsotg->ep0_state == DWC2_EP0_SETUP))
  573. ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */
  574. dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
  575. dwc2_writel(ctrl, hsotg->regs + epctrl_reg);
  576. /*
  577. * set these, it seems that DMA support increments past the end
  578. * of the packet buffer so we need to calculate the length from
  579. * this information.
  580. */
  581. hs_ep->size_loaded = length;
  582. hs_ep->last_load = ureq->actual;
  583. if (dir_in && !using_dma(hsotg)) {
  584. /* set these anyway, we may need them for non-periodic in */
  585. hs_ep->fifo_load = 0;
  586. dwc2_hsotg_write_fifo(hsotg, hs_ep, hs_req);
  587. }
  588. /*
  589. * Note, trying to clear the NAK here causes problems with transmit
  590. * on the S3C6400 ending up with the TXFIFO becoming full.
  591. */
  592. /* check ep is enabled */
  593. if (!(dwc2_readl(hsotg->regs + epctrl_reg) & DXEPCTL_EPENA))
  594. dev_dbg(hsotg->dev,
  595. "ep%d: failed to become enabled (DXEPCTL=0x%08x)?\n",
  596. index, dwc2_readl(hsotg->regs + epctrl_reg));
  597. dev_dbg(hsotg->dev, "%s: DXEPCTL=0x%08x\n",
  598. __func__, dwc2_readl(hsotg->regs + epctrl_reg));
  599. /* enable ep interrupts */
  600. dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 1);
  601. }
  602. /**
  603. * dwc2_hsotg_map_dma - map the DMA memory being used for the request
  604. * @hsotg: The device state.
  605. * @hs_ep: The endpoint the request is on.
  606. * @req: The request being processed.
  607. *
  608. * We've been asked to queue a request, so ensure that the memory buffer
  609. * is correctly setup for DMA. If we've been passed an extant DMA address
  610. * then ensure the buffer has been synced to memory. If our buffer has no
  611. * DMA memory, then we map the memory and mark our request to allow us to
  612. * cleanup on completion.
  613. */
  614. static int dwc2_hsotg_map_dma(struct dwc2_hsotg *hsotg,
  615. struct dwc2_hsotg_ep *hs_ep,
  616. struct usb_request *req)
  617. {
  618. struct dwc2_hsotg_req *hs_req = our_req(req);
  619. int ret;
  620. /* if the length is zero, ignore the DMA data */
  621. if (hs_req->req.length == 0)
  622. return 0;
  623. ret = usb_gadget_map_request(&hsotg->gadget, req, hs_ep->dir_in);
  624. if (ret)
  625. goto dma_error;
  626. return 0;
  627. dma_error:
  628. dev_err(hsotg->dev, "%s: failed to map buffer %p, %d bytes\n",
  629. __func__, req->buf, req->length);
  630. return -EIO;
  631. }
  632. static int dwc2_hsotg_handle_unaligned_buf_start(struct dwc2_hsotg *hsotg,
  633. struct dwc2_hsotg_ep *hs_ep, struct dwc2_hsotg_req *hs_req)
  634. {
  635. void *req_buf = hs_req->req.buf;
  636. /* If dma is not being used or buffer is aligned */
  637. if (!using_dma(hsotg) || !((long)req_buf & 3))
  638. return 0;
  639. WARN_ON(hs_req->saved_req_buf);
  640. dev_dbg(hsotg->dev, "%s: %s: buf=%p length=%d\n", __func__,
  641. hs_ep->ep.name, req_buf, hs_req->req.length);
  642. hs_req->req.buf = kmalloc(hs_req->req.length, GFP_ATOMIC);
  643. if (!hs_req->req.buf) {
  644. hs_req->req.buf = req_buf;
  645. dev_err(hsotg->dev,
  646. "%s: unable to allocate memory for bounce buffer\n",
  647. __func__);
  648. return -ENOMEM;
  649. }
  650. /* Save actual buffer */
  651. hs_req->saved_req_buf = req_buf;
  652. if (hs_ep->dir_in)
  653. memcpy(hs_req->req.buf, req_buf, hs_req->req.length);
  654. return 0;
  655. }
  656. static void dwc2_hsotg_handle_unaligned_buf_complete(struct dwc2_hsotg *hsotg,
  657. struct dwc2_hsotg_ep *hs_ep, struct dwc2_hsotg_req *hs_req)
  658. {
  659. /* If dma is not being used or buffer was aligned */
  660. if (!using_dma(hsotg) || !hs_req->saved_req_buf)
  661. return;
  662. dev_dbg(hsotg->dev, "%s: %s: status=%d actual-length=%d\n", __func__,
  663. hs_ep->ep.name, hs_req->req.status, hs_req->req.actual);
  664. /* Copy data from bounce buffer on successful out transfer */
  665. if (!hs_ep->dir_in && !hs_req->req.status)
  666. memcpy(hs_req->saved_req_buf, hs_req->req.buf,
  667. hs_req->req.actual);
  668. /* Free bounce buffer */
  669. kfree(hs_req->req.buf);
  670. hs_req->req.buf = hs_req->saved_req_buf;
  671. hs_req->saved_req_buf = NULL;
  672. }
  673. /**
  674. * dwc2_gadget_target_frame_elapsed - Checks target frame
  675. * @hs_ep: The driver endpoint to check
  676. *
  677. * Returns 1 if targeted frame elapsed. If returned 1 then we need to drop
  678. * corresponding transfer.
  679. */
  680. static bool dwc2_gadget_target_frame_elapsed(struct dwc2_hsotg_ep *hs_ep)
  681. {
  682. struct dwc2_hsotg *hsotg = hs_ep->parent;
  683. u32 target_frame = hs_ep->target_frame;
  684. u32 current_frame = dwc2_hsotg_read_frameno(hsotg);
  685. bool frame_overrun = hs_ep->frame_overrun;
  686. if (!frame_overrun && current_frame >= target_frame)
  687. return true;
  688. if (frame_overrun && current_frame >= target_frame &&
  689. ((current_frame - target_frame) < DSTS_SOFFN_LIMIT / 2))
  690. return true;
  691. return false;
  692. }
  693. static int dwc2_hsotg_ep_queue(struct usb_ep *ep, struct usb_request *req,
  694. gfp_t gfp_flags)
  695. {
  696. struct dwc2_hsotg_req *hs_req = our_req(req);
  697. struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
  698. struct dwc2_hsotg *hs = hs_ep->parent;
  699. bool first;
  700. int ret;
  701. dev_dbg(hs->dev, "%s: req %p: %d@%p, noi=%d, zero=%d, snok=%d\n",
  702. ep->name, req, req->length, req->buf, req->no_interrupt,
  703. req->zero, req->short_not_ok);
  704. /* Prevent new request submission when controller is suspended */
  705. if (hs->lx_state == DWC2_L2) {
  706. dev_dbg(hs->dev, "%s: don't submit request while suspended\n",
  707. __func__);
  708. return -EAGAIN;
  709. }
  710. /* initialise status of the request */
  711. INIT_LIST_HEAD(&hs_req->queue);
  712. req->actual = 0;
  713. req->status = -EINPROGRESS;
  714. ret = dwc2_hsotg_handle_unaligned_buf_start(hs, hs_ep, hs_req);
  715. if (ret)
  716. return ret;
  717. /* if we're using DMA, sync the buffers as necessary */
  718. if (using_dma(hs)) {
  719. ret = dwc2_hsotg_map_dma(hs, hs_ep, req);
  720. if (ret)
  721. return ret;
  722. }
  723. first = list_empty(&hs_ep->queue);
  724. list_add_tail(&hs_req->queue, &hs_ep->queue);
  725. if (first) {
  726. if (!hs_ep->isochronous) {
  727. dwc2_hsotg_start_req(hs, hs_ep, hs_req, false);
  728. return 0;
  729. }
  730. while (dwc2_gadget_target_frame_elapsed(hs_ep))
  731. dwc2_gadget_incr_frame_num(hs_ep);
  732. if (hs_ep->target_frame != TARGET_FRAME_INITIAL)
  733. dwc2_hsotg_start_req(hs, hs_ep, hs_req, false);
  734. }
  735. return 0;
  736. }
  737. static int dwc2_hsotg_ep_queue_lock(struct usb_ep *ep, struct usb_request *req,
  738. gfp_t gfp_flags)
  739. {
  740. struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
  741. struct dwc2_hsotg *hs = hs_ep->parent;
  742. unsigned long flags = 0;
  743. int ret = 0;
  744. spin_lock_irqsave(&hs->lock, flags);
  745. ret = dwc2_hsotg_ep_queue(ep, req, gfp_flags);
  746. spin_unlock_irqrestore(&hs->lock, flags);
  747. return ret;
  748. }
  749. static void dwc2_hsotg_ep_free_request(struct usb_ep *ep,
  750. struct usb_request *req)
  751. {
  752. struct dwc2_hsotg_req *hs_req = our_req(req);
  753. kfree(hs_req);
  754. }
  755. /**
  756. * dwc2_hsotg_complete_oursetup - setup completion callback
  757. * @ep: The endpoint the request was on.
  758. * @req: The request completed.
  759. *
  760. * Called on completion of any requests the driver itself
  761. * submitted that need cleaning up.
  762. */
  763. static void dwc2_hsotg_complete_oursetup(struct usb_ep *ep,
  764. struct usb_request *req)
  765. {
  766. struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
  767. struct dwc2_hsotg *hsotg = hs_ep->parent;
  768. dev_dbg(hsotg->dev, "%s: ep %p, req %p\n", __func__, ep, req);
  769. dwc2_hsotg_ep_free_request(ep, req);
  770. }
  771. /**
  772. * ep_from_windex - convert control wIndex value to endpoint
  773. * @hsotg: The driver state.
  774. * @windex: The control request wIndex field (in host order).
  775. *
  776. * Convert the given wIndex into a pointer to an driver endpoint
  777. * structure, or return NULL if it is not a valid endpoint.
  778. */
  779. static struct dwc2_hsotg_ep *ep_from_windex(struct dwc2_hsotg *hsotg,
  780. u32 windex)
  781. {
  782. struct dwc2_hsotg_ep *ep;
  783. int dir = (windex & USB_DIR_IN) ? 1 : 0;
  784. int idx = windex & 0x7F;
  785. if (windex >= 0x100)
  786. return NULL;
  787. if (idx > hsotg->num_of_eps)
  788. return NULL;
  789. ep = index_to_ep(hsotg, idx, dir);
  790. if (idx && ep->dir_in != dir)
  791. return NULL;
  792. return ep;
  793. }
  794. /**
  795. * dwc2_hsotg_set_test_mode - Enable usb Test Modes
  796. * @hsotg: The driver state.
  797. * @testmode: requested usb test mode
  798. * Enable usb Test Mode requested by the Host.
  799. */
  800. int dwc2_hsotg_set_test_mode(struct dwc2_hsotg *hsotg, int testmode)
  801. {
  802. int dctl = dwc2_readl(hsotg->regs + DCTL);
  803. dctl &= ~DCTL_TSTCTL_MASK;
  804. switch (testmode) {
  805. case TEST_J:
  806. case TEST_K:
  807. case TEST_SE0_NAK:
  808. case TEST_PACKET:
  809. case TEST_FORCE_EN:
  810. dctl |= testmode << DCTL_TSTCTL_SHIFT;
  811. break;
  812. default:
  813. return -EINVAL;
  814. }
  815. dwc2_writel(dctl, hsotg->regs + DCTL);
  816. return 0;
  817. }
  818. /**
  819. * dwc2_hsotg_send_reply - send reply to control request
  820. * @hsotg: The device state
  821. * @ep: Endpoint 0
  822. * @buff: Buffer for request
  823. * @length: Length of reply.
  824. *
  825. * Create a request and queue it on the given endpoint. This is useful as
  826. * an internal method of sending replies to certain control requests, etc.
  827. */
  828. static int dwc2_hsotg_send_reply(struct dwc2_hsotg *hsotg,
  829. struct dwc2_hsotg_ep *ep,
  830. void *buff,
  831. int length)
  832. {
  833. struct usb_request *req;
  834. int ret;
  835. dev_dbg(hsotg->dev, "%s: buff %p, len %d\n", __func__, buff, length);
  836. req = dwc2_hsotg_ep_alloc_request(&ep->ep, GFP_ATOMIC);
  837. hsotg->ep0_reply = req;
  838. if (!req) {
  839. dev_warn(hsotg->dev, "%s: cannot alloc req\n", __func__);
  840. return -ENOMEM;
  841. }
  842. req->buf = hsotg->ep0_buff;
  843. req->length = length;
  844. /*
  845. * zero flag is for sending zlp in DATA IN stage. It has no impact on
  846. * STATUS stage.
  847. */
  848. req->zero = 0;
  849. req->complete = dwc2_hsotg_complete_oursetup;
  850. if (length)
  851. memcpy(req->buf, buff, length);
  852. ret = dwc2_hsotg_ep_queue(&ep->ep, req, GFP_ATOMIC);
  853. if (ret) {
  854. dev_warn(hsotg->dev, "%s: cannot queue req\n", __func__);
  855. return ret;
  856. }
  857. return 0;
  858. }
  859. /**
  860. * dwc2_hsotg_process_req_status - process request GET_STATUS
  861. * @hsotg: The device state
  862. * @ctrl: USB control request
  863. */
  864. static int dwc2_hsotg_process_req_status(struct dwc2_hsotg *hsotg,
  865. struct usb_ctrlrequest *ctrl)
  866. {
  867. struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
  868. struct dwc2_hsotg_ep *ep;
  869. __le16 reply;
  870. int ret;
  871. dev_dbg(hsotg->dev, "%s: USB_REQ_GET_STATUS\n", __func__);
  872. if (!ep0->dir_in) {
  873. dev_warn(hsotg->dev, "%s: direction out?\n", __func__);
  874. return -EINVAL;
  875. }
  876. switch (ctrl->bRequestType & USB_RECIP_MASK) {
  877. case USB_RECIP_DEVICE:
  878. reply = cpu_to_le16(0); /* bit 0 => self powered,
  879. * bit 1 => remote wakeup */
  880. break;
  881. case USB_RECIP_INTERFACE:
  882. /* currently, the data result should be zero */
  883. reply = cpu_to_le16(0);
  884. break;
  885. case USB_RECIP_ENDPOINT:
  886. ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex));
  887. if (!ep)
  888. return -ENOENT;
  889. reply = cpu_to_le16(ep->halted ? 1 : 0);
  890. break;
  891. default:
  892. return 0;
  893. }
  894. if (le16_to_cpu(ctrl->wLength) != 2)
  895. return -EINVAL;
  896. ret = dwc2_hsotg_send_reply(hsotg, ep0, &reply, 2);
  897. if (ret) {
  898. dev_err(hsotg->dev, "%s: failed to send reply\n", __func__);
  899. return ret;
  900. }
  901. return 1;
  902. }
  903. static int dwc2_hsotg_ep_sethalt(struct usb_ep *ep, int value, bool now);
  904. /**
  905. * get_ep_head - return the first request on the endpoint
  906. * @hs_ep: The controller endpoint to get
  907. *
  908. * Get the first request on the endpoint.
  909. */
  910. static struct dwc2_hsotg_req *get_ep_head(struct dwc2_hsotg_ep *hs_ep)
  911. {
  912. if (list_empty(&hs_ep->queue))
  913. return NULL;
  914. return list_first_entry(&hs_ep->queue, struct dwc2_hsotg_req, queue);
  915. }
  916. /**
  917. * dwc2_gadget_start_next_request - Starts next request from ep queue
  918. * @hs_ep: Endpoint structure
  919. *
  920. * If queue is empty and EP is ISOC-OUT - unmasks OUTTKNEPDIS which is masked
  921. * in its handler. Hence we need to unmask it here to be able to do
  922. * resynchronization.
  923. */
  924. static void dwc2_gadget_start_next_request(struct dwc2_hsotg_ep *hs_ep)
  925. {
  926. u32 mask;
  927. struct dwc2_hsotg *hsotg = hs_ep->parent;
  928. int dir_in = hs_ep->dir_in;
  929. struct dwc2_hsotg_req *hs_req;
  930. u32 epmsk_reg = dir_in ? DIEPMSK : DOEPMSK;
  931. if (!list_empty(&hs_ep->queue)) {
  932. hs_req = get_ep_head(hs_ep);
  933. dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, false);
  934. return;
  935. }
  936. if (!hs_ep->isochronous)
  937. return;
  938. if (dir_in) {
  939. dev_dbg(hsotg->dev, "%s: No more ISOC-IN requests\n",
  940. __func__);
  941. } else {
  942. dev_dbg(hsotg->dev, "%s: No more ISOC-OUT requests\n",
  943. __func__);
  944. mask = dwc2_readl(hsotg->regs + epmsk_reg);
  945. mask |= DOEPMSK_OUTTKNEPDISMSK;
  946. dwc2_writel(mask, hsotg->regs + epmsk_reg);
  947. }
  948. }
  949. /**
  950. * dwc2_hsotg_process_req_feature - process request {SET,CLEAR}_FEATURE
  951. * @hsotg: The device state
  952. * @ctrl: USB control request
  953. */
  954. static int dwc2_hsotg_process_req_feature(struct dwc2_hsotg *hsotg,
  955. struct usb_ctrlrequest *ctrl)
  956. {
  957. struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
  958. struct dwc2_hsotg_req *hs_req;
  959. bool set = (ctrl->bRequest == USB_REQ_SET_FEATURE);
  960. struct dwc2_hsotg_ep *ep;
  961. int ret;
  962. bool halted;
  963. u32 recip;
  964. u32 wValue;
  965. u32 wIndex;
  966. dev_dbg(hsotg->dev, "%s: %s_FEATURE\n",
  967. __func__, set ? "SET" : "CLEAR");
  968. wValue = le16_to_cpu(ctrl->wValue);
  969. wIndex = le16_to_cpu(ctrl->wIndex);
  970. recip = ctrl->bRequestType & USB_RECIP_MASK;
  971. switch (recip) {
  972. case USB_RECIP_DEVICE:
  973. switch (wValue) {
  974. case USB_DEVICE_TEST_MODE:
  975. if ((wIndex & 0xff) != 0)
  976. return -EINVAL;
  977. if (!set)
  978. return -EINVAL;
  979. hsotg->test_mode = wIndex >> 8;
  980. ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
  981. if (ret) {
  982. dev_err(hsotg->dev,
  983. "%s: failed to send reply\n", __func__);
  984. return ret;
  985. }
  986. break;
  987. default:
  988. return -ENOENT;
  989. }
  990. break;
  991. case USB_RECIP_ENDPOINT:
  992. ep = ep_from_windex(hsotg, wIndex);
  993. if (!ep) {
  994. dev_dbg(hsotg->dev, "%s: no endpoint for 0x%04x\n",
  995. __func__, wIndex);
  996. return -ENOENT;
  997. }
  998. switch (wValue) {
  999. case USB_ENDPOINT_HALT:
  1000. halted = ep->halted;
  1001. dwc2_hsotg_ep_sethalt(&ep->ep, set, true);
  1002. ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
  1003. if (ret) {
  1004. dev_err(hsotg->dev,
  1005. "%s: failed to send reply\n", __func__);
  1006. return ret;
  1007. }
  1008. /*
  1009. * we have to complete all requests for ep if it was
  1010. * halted, and the halt was cleared by CLEAR_FEATURE
  1011. */
  1012. if (!set && halted) {
  1013. /*
  1014. * If we have request in progress,
  1015. * then complete it
  1016. */
  1017. if (ep->req) {
  1018. hs_req = ep->req;
  1019. ep->req = NULL;
  1020. list_del_init(&hs_req->queue);
  1021. if (hs_req->req.complete) {
  1022. spin_unlock(&hsotg->lock);
  1023. usb_gadget_giveback_request(
  1024. &ep->ep, &hs_req->req);
  1025. spin_lock(&hsotg->lock);
  1026. }
  1027. }
  1028. /* If we have pending request, then start it */
  1029. if (!ep->req) {
  1030. dwc2_gadget_start_next_request(ep);
  1031. }
  1032. }
  1033. break;
  1034. default:
  1035. return -ENOENT;
  1036. }
  1037. break;
  1038. default:
  1039. return -ENOENT;
  1040. }
  1041. return 1;
  1042. }
  1043. static void dwc2_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg);
  1044. /**
  1045. * dwc2_hsotg_stall_ep0 - stall ep0
  1046. * @hsotg: The device state
  1047. *
  1048. * Set stall for ep0 as response for setup request.
  1049. */
  1050. static void dwc2_hsotg_stall_ep0(struct dwc2_hsotg *hsotg)
  1051. {
  1052. struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
  1053. u32 reg;
  1054. u32 ctrl;
  1055. dev_dbg(hsotg->dev, "ep0 stall (dir=%d)\n", ep0->dir_in);
  1056. reg = (ep0->dir_in) ? DIEPCTL0 : DOEPCTL0;
  1057. /*
  1058. * DxEPCTL_Stall will be cleared by EP once it has
  1059. * taken effect, so no need to clear later.
  1060. */
  1061. ctrl = dwc2_readl(hsotg->regs + reg);
  1062. ctrl |= DXEPCTL_STALL;
  1063. ctrl |= DXEPCTL_CNAK;
  1064. dwc2_writel(ctrl, hsotg->regs + reg);
  1065. dev_dbg(hsotg->dev,
  1066. "written DXEPCTL=0x%08x to %08x (DXEPCTL=0x%08x)\n",
  1067. ctrl, reg, dwc2_readl(hsotg->regs + reg));
  1068. /*
  1069. * complete won't be called, so we enqueue
  1070. * setup request here
  1071. */
  1072. dwc2_hsotg_enqueue_setup(hsotg);
  1073. }
  1074. /**
  1075. * dwc2_hsotg_process_control - process a control request
  1076. * @hsotg: The device state
  1077. * @ctrl: The control request received
  1078. *
  1079. * The controller has received the SETUP phase of a control request, and
  1080. * needs to work out what to do next (and whether to pass it on to the
  1081. * gadget driver).
  1082. */
  1083. static void dwc2_hsotg_process_control(struct dwc2_hsotg *hsotg,
  1084. struct usb_ctrlrequest *ctrl)
  1085. {
  1086. struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
  1087. int ret = 0;
  1088. u32 dcfg;
  1089. dev_dbg(hsotg->dev,
  1090. "ctrl Type=%02x, Req=%02x, V=%04x, I=%04x, L=%04x\n",
  1091. ctrl->bRequestType, ctrl->bRequest, ctrl->wValue,
  1092. ctrl->wIndex, ctrl->wLength);
  1093. if (ctrl->wLength == 0) {
  1094. ep0->dir_in = 1;
  1095. hsotg->ep0_state = DWC2_EP0_STATUS_IN;
  1096. } else if (ctrl->bRequestType & USB_DIR_IN) {
  1097. ep0->dir_in = 1;
  1098. hsotg->ep0_state = DWC2_EP0_DATA_IN;
  1099. } else {
  1100. ep0->dir_in = 0;
  1101. hsotg->ep0_state = DWC2_EP0_DATA_OUT;
  1102. }
  1103. if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
  1104. switch (ctrl->bRequest) {
  1105. case USB_REQ_SET_ADDRESS:
  1106. hsotg->connected = 1;
  1107. dcfg = dwc2_readl(hsotg->regs + DCFG);
  1108. dcfg &= ~DCFG_DEVADDR_MASK;
  1109. dcfg |= (le16_to_cpu(ctrl->wValue) <<
  1110. DCFG_DEVADDR_SHIFT) & DCFG_DEVADDR_MASK;
  1111. dwc2_writel(dcfg, hsotg->regs + DCFG);
  1112. dev_info(hsotg->dev, "new address %d\n", ctrl->wValue);
  1113. ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
  1114. return;
  1115. case USB_REQ_GET_STATUS:
  1116. ret = dwc2_hsotg_process_req_status(hsotg, ctrl);
  1117. break;
  1118. case USB_REQ_CLEAR_FEATURE:
  1119. case USB_REQ_SET_FEATURE:
  1120. ret = dwc2_hsotg_process_req_feature(hsotg, ctrl);
  1121. break;
  1122. }
  1123. }
  1124. /* as a fallback, try delivering it to the driver to deal with */
  1125. if (ret == 0 && hsotg->driver) {
  1126. spin_unlock(&hsotg->lock);
  1127. ret = hsotg->driver->setup(&hsotg->gadget, ctrl);
  1128. spin_lock(&hsotg->lock);
  1129. if (ret < 0)
  1130. dev_dbg(hsotg->dev, "driver->setup() ret %d\n", ret);
  1131. }
  1132. /*
  1133. * the request is either unhandlable, or is not formatted correctly
  1134. * so respond with a STALL for the status stage to indicate failure.
  1135. */
  1136. if (ret < 0)
  1137. dwc2_hsotg_stall_ep0(hsotg);
  1138. }
  1139. /**
  1140. * dwc2_hsotg_complete_setup - completion of a setup transfer
  1141. * @ep: The endpoint the request was on.
  1142. * @req: The request completed.
  1143. *
  1144. * Called on completion of any requests the driver itself submitted for
  1145. * EP0 setup packets
  1146. */
  1147. static void dwc2_hsotg_complete_setup(struct usb_ep *ep,
  1148. struct usb_request *req)
  1149. {
  1150. struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
  1151. struct dwc2_hsotg *hsotg = hs_ep->parent;
  1152. if (req->status < 0) {
  1153. dev_dbg(hsotg->dev, "%s: failed %d\n", __func__, req->status);
  1154. return;
  1155. }
  1156. spin_lock(&hsotg->lock);
  1157. if (req->actual == 0)
  1158. dwc2_hsotg_enqueue_setup(hsotg);
  1159. else
  1160. dwc2_hsotg_process_control(hsotg, req->buf);
  1161. spin_unlock(&hsotg->lock);
  1162. }
  1163. /**
  1164. * dwc2_hsotg_enqueue_setup - start a request for EP0 packets
  1165. * @hsotg: The device state.
  1166. *
  1167. * Enqueue a request on EP0 if necessary to received any SETUP packets
  1168. * received from the host.
  1169. */
  1170. static void dwc2_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg)
  1171. {
  1172. struct usb_request *req = hsotg->ctrl_req;
  1173. struct dwc2_hsotg_req *hs_req = our_req(req);
  1174. int ret;
  1175. dev_dbg(hsotg->dev, "%s: queueing setup request\n", __func__);
  1176. req->zero = 0;
  1177. req->length = 8;
  1178. req->buf = hsotg->ctrl_buff;
  1179. req->complete = dwc2_hsotg_complete_setup;
  1180. if (!list_empty(&hs_req->queue)) {
  1181. dev_dbg(hsotg->dev, "%s already queued???\n", __func__);
  1182. return;
  1183. }
  1184. hsotg->eps_out[0]->dir_in = 0;
  1185. hsotg->eps_out[0]->send_zlp = 0;
  1186. hsotg->ep0_state = DWC2_EP0_SETUP;
  1187. ret = dwc2_hsotg_ep_queue(&hsotg->eps_out[0]->ep, req, GFP_ATOMIC);
  1188. if (ret < 0) {
  1189. dev_err(hsotg->dev, "%s: failed queue (%d)\n", __func__, ret);
  1190. /*
  1191. * Don't think there's much we can do other than watch the
  1192. * driver fail.
  1193. */
  1194. }
  1195. }
  1196. static void dwc2_hsotg_program_zlp(struct dwc2_hsotg *hsotg,
  1197. struct dwc2_hsotg_ep *hs_ep)
  1198. {
  1199. u32 ctrl;
  1200. u8 index = hs_ep->index;
  1201. u32 epctl_reg = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index);
  1202. u32 epsiz_reg = hs_ep->dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
  1203. if (hs_ep->dir_in)
  1204. dev_dbg(hsotg->dev, "Sending zero-length packet on ep%d\n",
  1205. index);
  1206. else
  1207. dev_dbg(hsotg->dev, "Receiving zero-length packet on ep%d\n",
  1208. index);
  1209. dwc2_writel(DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
  1210. DXEPTSIZ_XFERSIZE(0), hsotg->regs +
  1211. epsiz_reg);
  1212. ctrl = dwc2_readl(hsotg->regs + epctl_reg);
  1213. ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */
  1214. ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */
  1215. ctrl |= DXEPCTL_USBACTEP;
  1216. dwc2_writel(ctrl, hsotg->regs + epctl_reg);
  1217. }
  1218. /**
  1219. * dwc2_hsotg_complete_request - complete a request given to us
  1220. * @hsotg: The device state.
  1221. * @hs_ep: The endpoint the request was on.
  1222. * @hs_req: The request to complete.
  1223. * @result: The result code (0 => Ok, otherwise errno)
  1224. *
  1225. * The given request has finished, so call the necessary completion
  1226. * if it has one and then look to see if we can start a new request
  1227. * on the endpoint.
  1228. *
  1229. * Note, expects the ep to already be locked as appropriate.
  1230. */
  1231. static void dwc2_hsotg_complete_request(struct dwc2_hsotg *hsotg,
  1232. struct dwc2_hsotg_ep *hs_ep,
  1233. struct dwc2_hsotg_req *hs_req,
  1234. int result)
  1235. {
  1236. if (!hs_req) {
  1237. dev_dbg(hsotg->dev, "%s: nothing to complete?\n", __func__);
  1238. return;
  1239. }
  1240. dev_dbg(hsotg->dev, "complete: ep %p %s, req %p, %d => %p\n",
  1241. hs_ep, hs_ep->ep.name, hs_req, result, hs_req->req.complete);
  1242. /*
  1243. * only replace the status if we've not already set an error
  1244. * from a previous transaction
  1245. */
  1246. if (hs_req->req.status == -EINPROGRESS)
  1247. hs_req->req.status = result;
  1248. if (using_dma(hsotg))
  1249. dwc2_hsotg_unmap_dma(hsotg, hs_ep, hs_req);
  1250. dwc2_hsotg_handle_unaligned_buf_complete(hsotg, hs_ep, hs_req);
  1251. hs_ep->req = NULL;
  1252. list_del_init(&hs_req->queue);
  1253. /*
  1254. * call the complete request with the locks off, just in case the
  1255. * request tries to queue more work for this endpoint.
  1256. */
  1257. if (hs_req->req.complete) {
  1258. spin_unlock(&hsotg->lock);
  1259. usb_gadget_giveback_request(&hs_ep->ep, &hs_req->req);
  1260. spin_lock(&hsotg->lock);
  1261. }
  1262. /*
  1263. * Look to see if there is anything else to do. Note, the completion
  1264. * of the previous request may have caused a new request to be started
  1265. * so be careful when doing this.
  1266. */
  1267. if (!hs_ep->req && result >= 0) {
  1268. dwc2_gadget_start_next_request(hs_ep);
  1269. }
  1270. }
  1271. /**
  1272. * dwc2_hsotg_rx_data - receive data from the FIFO for an endpoint
  1273. * @hsotg: The device state.
  1274. * @ep_idx: The endpoint index for the data
  1275. * @size: The size of data in the fifo, in bytes
  1276. *
  1277. * The FIFO status shows there is data to read from the FIFO for a given
  1278. * endpoint, so sort out whether we need to read the data into a request
  1279. * that has been made for that endpoint.
  1280. */
  1281. static void dwc2_hsotg_rx_data(struct dwc2_hsotg *hsotg, int ep_idx, int size)
  1282. {
  1283. struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[ep_idx];
  1284. struct dwc2_hsotg_req *hs_req = hs_ep->req;
  1285. void __iomem *fifo = hsotg->regs + EPFIFO(ep_idx);
  1286. int to_read;
  1287. int max_req;
  1288. int read_ptr;
  1289. if (!hs_req) {
  1290. u32 epctl = dwc2_readl(hsotg->regs + DOEPCTL(ep_idx));
  1291. int ptr;
  1292. dev_dbg(hsotg->dev,
  1293. "%s: FIFO %d bytes on ep%d but no req (DXEPCTl=0x%08x)\n",
  1294. __func__, size, ep_idx, epctl);
  1295. /* dump the data from the FIFO, we've nothing we can do */
  1296. for (ptr = 0; ptr < size; ptr += 4)
  1297. (void)dwc2_readl(fifo);
  1298. return;
  1299. }
  1300. to_read = size;
  1301. read_ptr = hs_req->req.actual;
  1302. max_req = hs_req->req.length - read_ptr;
  1303. dev_dbg(hsotg->dev, "%s: read %d/%d, done %d/%d\n",
  1304. __func__, to_read, max_req, read_ptr, hs_req->req.length);
  1305. if (to_read > max_req) {
  1306. /*
  1307. * more data appeared than we where willing
  1308. * to deal with in this request.
  1309. */
  1310. /* currently we don't deal this */
  1311. WARN_ON_ONCE(1);
  1312. }
  1313. hs_ep->total_data += to_read;
  1314. hs_req->req.actual += to_read;
  1315. to_read = DIV_ROUND_UP(to_read, 4);
  1316. /*
  1317. * note, we might over-write the buffer end by 3 bytes depending on
  1318. * alignment of the data.
  1319. */
  1320. ioread32_rep(fifo, hs_req->req.buf + read_ptr, to_read);
  1321. }
  1322. /**
  1323. * dwc2_hsotg_ep0_zlp - send/receive zero-length packet on control endpoint
  1324. * @hsotg: The device instance
  1325. * @dir_in: If IN zlp
  1326. *
  1327. * Generate a zero-length IN packet request for terminating a SETUP
  1328. * transaction.
  1329. *
  1330. * Note, since we don't write any data to the TxFIFO, then it is
  1331. * currently believed that we do not need to wait for any space in
  1332. * the TxFIFO.
  1333. */
  1334. static void dwc2_hsotg_ep0_zlp(struct dwc2_hsotg *hsotg, bool dir_in)
  1335. {
  1336. /* eps_out[0] is used in both directions */
  1337. hsotg->eps_out[0]->dir_in = dir_in;
  1338. hsotg->ep0_state = dir_in ? DWC2_EP0_STATUS_IN : DWC2_EP0_STATUS_OUT;
  1339. dwc2_hsotg_program_zlp(hsotg, hsotg->eps_out[0]);
  1340. }
  1341. static void dwc2_hsotg_change_ep_iso_parity(struct dwc2_hsotg *hsotg,
  1342. u32 epctl_reg)
  1343. {
  1344. u32 ctrl;
  1345. ctrl = dwc2_readl(hsotg->regs + epctl_reg);
  1346. if (ctrl & DXEPCTL_EOFRNUM)
  1347. ctrl |= DXEPCTL_SETEVENFR;
  1348. else
  1349. ctrl |= DXEPCTL_SETODDFR;
  1350. dwc2_writel(ctrl, hsotg->regs + epctl_reg);
  1351. }
  1352. /**
  1353. * dwc2_hsotg_handle_outdone - handle receiving OutDone/SetupDone from RXFIFO
  1354. * @hsotg: The device instance
  1355. * @epnum: The endpoint received from
  1356. *
  1357. * The RXFIFO has delivered an OutDone event, which means that the data
  1358. * transfer for an OUT endpoint has been completed, either by a short
  1359. * packet or by the finish of a transfer.
  1360. */
  1361. static void dwc2_hsotg_handle_outdone(struct dwc2_hsotg *hsotg, int epnum)
  1362. {
  1363. u32 epsize = dwc2_readl(hsotg->regs + DOEPTSIZ(epnum));
  1364. struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[epnum];
  1365. struct dwc2_hsotg_req *hs_req = hs_ep->req;
  1366. struct usb_request *req = &hs_req->req;
  1367. unsigned size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
  1368. int result = 0;
  1369. if (!hs_req) {
  1370. dev_dbg(hsotg->dev, "%s: no request active\n", __func__);
  1371. return;
  1372. }
  1373. if (epnum == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_OUT) {
  1374. dev_dbg(hsotg->dev, "zlp packet received\n");
  1375. dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
  1376. dwc2_hsotg_enqueue_setup(hsotg);
  1377. return;
  1378. }
  1379. if (using_dma(hsotg)) {
  1380. unsigned size_done;
  1381. /*
  1382. * Calculate the size of the transfer by checking how much
  1383. * is left in the endpoint size register and then working it
  1384. * out from the amount we loaded for the transfer.
  1385. *
  1386. * We need to do this as DMA pointers are always 32bit aligned
  1387. * so may overshoot/undershoot the transfer.
  1388. */
  1389. size_done = hs_ep->size_loaded - size_left;
  1390. size_done += hs_ep->last_load;
  1391. req->actual = size_done;
  1392. }
  1393. /* if there is more request to do, schedule new transfer */
  1394. if (req->actual < req->length && size_left == 0) {
  1395. dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, true);
  1396. return;
  1397. }
  1398. if (req->actual < req->length && req->short_not_ok) {
  1399. dev_dbg(hsotg->dev, "%s: got %d/%d (short not ok) => error\n",
  1400. __func__, req->actual, req->length);
  1401. /*
  1402. * todo - what should we return here? there's no one else
  1403. * even bothering to check the status.
  1404. */
  1405. }
  1406. if (epnum == 0 && hsotg->ep0_state == DWC2_EP0_DATA_OUT) {
  1407. /* Move to STATUS IN */
  1408. dwc2_hsotg_ep0_zlp(hsotg, true);
  1409. return;
  1410. }
  1411. /*
  1412. * Slave mode OUT transfers do not go through XferComplete so
  1413. * adjust the ISOC parity here.
  1414. */
  1415. if (!using_dma(hsotg)) {
  1416. if (hs_ep->isochronous && hs_ep->interval == 1)
  1417. dwc2_hsotg_change_ep_iso_parity(hsotg, DOEPCTL(epnum));
  1418. else if (hs_ep->isochronous && hs_ep->interval > 1)
  1419. dwc2_gadget_incr_frame_num(hs_ep);
  1420. }
  1421. dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, result);
  1422. }
  1423. /**
  1424. * dwc2_hsotg_handle_rx - RX FIFO has data
  1425. * @hsotg: The device instance
  1426. *
  1427. * The IRQ handler has detected that the RX FIFO has some data in it
  1428. * that requires processing, so find out what is in there and do the
  1429. * appropriate read.
  1430. *
  1431. * The RXFIFO is a true FIFO, the packets coming out are still in packet
  1432. * chunks, so if you have x packets received on an endpoint you'll get x
  1433. * FIFO events delivered, each with a packet's worth of data in it.
  1434. *
  1435. * When using DMA, we should not be processing events from the RXFIFO
  1436. * as the actual data should be sent to the memory directly and we turn
  1437. * on the completion interrupts to get notifications of transfer completion.
  1438. */
  1439. static void dwc2_hsotg_handle_rx(struct dwc2_hsotg *hsotg)
  1440. {
  1441. u32 grxstsr = dwc2_readl(hsotg->regs + GRXSTSP);
  1442. u32 epnum, status, size;
  1443. WARN_ON(using_dma(hsotg));
  1444. epnum = grxstsr & GRXSTS_EPNUM_MASK;
  1445. status = grxstsr & GRXSTS_PKTSTS_MASK;
  1446. size = grxstsr & GRXSTS_BYTECNT_MASK;
  1447. size >>= GRXSTS_BYTECNT_SHIFT;
  1448. dev_dbg(hsotg->dev, "%s: GRXSTSP=0x%08x (%d@%d)\n",
  1449. __func__, grxstsr, size, epnum);
  1450. switch ((status & GRXSTS_PKTSTS_MASK) >> GRXSTS_PKTSTS_SHIFT) {
  1451. case GRXSTS_PKTSTS_GLOBALOUTNAK:
  1452. dev_dbg(hsotg->dev, "GLOBALOUTNAK\n");
  1453. break;
  1454. case GRXSTS_PKTSTS_OUTDONE:
  1455. dev_dbg(hsotg->dev, "OutDone (Frame=0x%08x)\n",
  1456. dwc2_hsotg_read_frameno(hsotg));
  1457. if (!using_dma(hsotg))
  1458. dwc2_hsotg_handle_outdone(hsotg, epnum);
  1459. break;
  1460. case GRXSTS_PKTSTS_SETUPDONE:
  1461. dev_dbg(hsotg->dev,
  1462. "SetupDone (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
  1463. dwc2_hsotg_read_frameno(hsotg),
  1464. dwc2_readl(hsotg->regs + DOEPCTL(0)));
  1465. /*
  1466. * Call dwc2_hsotg_handle_outdone here if it was not called from
  1467. * GRXSTS_PKTSTS_OUTDONE. That is, if the core didn't
  1468. * generate GRXSTS_PKTSTS_OUTDONE for setup packet.
  1469. */
  1470. if (hsotg->ep0_state == DWC2_EP0_SETUP)
  1471. dwc2_hsotg_handle_outdone(hsotg, epnum);
  1472. break;
  1473. case GRXSTS_PKTSTS_OUTRX:
  1474. dwc2_hsotg_rx_data(hsotg, epnum, size);
  1475. break;
  1476. case GRXSTS_PKTSTS_SETUPRX:
  1477. dev_dbg(hsotg->dev,
  1478. "SetupRX (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
  1479. dwc2_hsotg_read_frameno(hsotg),
  1480. dwc2_readl(hsotg->regs + DOEPCTL(0)));
  1481. WARN_ON(hsotg->ep0_state != DWC2_EP0_SETUP);
  1482. dwc2_hsotg_rx_data(hsotg, epnum, size);
  1483. break;
  1484. default:
  1485. dev_warn(hsotg->dev, "%s: unknown status %08x\n",
  1486. __func__, grxstsr);
  1487. dwc2_hsotg_dump(hsotg);
  1488. break;
  1489. }
  1490. }
  1491. /**
  1492. * dwc2_hsotg_ep0_mps - turn max packet size into register setting
  1493. * @mps: The maximum packet size in bytes.
  1494. */
  1495. static u32 dwc2_hsotg_ep0_mps(unsigned int mps)
  1496. {
  1497. switch (mps) {
  1498. case 64:
  1499. return D0EPCTL_MPS_64;
  1500. case 32:
  1501. return D0EPCTL_MPS_32;
  1502. case 16:
  1503. return D0EPCTL_MPS_16;
  1504. case 8:
  1505. return D0EPCTL_MPS_8;
  1506. }
  1507. /* bad max packet size, warn and return invalid result */
  1508. WARN_ON(1);
  1509. return (u32)-1;
  1510. }
  1511. /**
  1512. * dwc2_hsotg_set_ep_maxpacket - set endpoint's max-packet field
  1513. * @hsotg: The driver state.
  1514. * @ep: The index number of the endpoint
  1515. * @mps: The maximum packet size in bytes
  1516. *
  1517. * Configure the maximum packet size for the given endpoint, updating
  1518. * the hardware control registers to reflect this.
  1519. */
  1520. static void dwc2_hsotg_set_ep_maxpacket(struct dwc2_hsotg *hsotg,
  1521. unsigned int ep, unsigned int mps, unsigned int dir_in)
  1522. {
  1523. struct dwc2_hsotg_ep *hs_ep;
  1524. void __iomem *regs = hsotg->regs;
  1525. u32 mpsval;
  1526. u32 mcval;
  1527. u32 reg;
  1528. hs_ep = index_to_ep(hsotg, ep, dir_in);
  1529. if (!hs_ep)
  1530. return;
  1531. if (ep == 0) {
  1532. /* EP0 is a special case */
  1533. mpsval = dwc2_hsotg_ep0_mps(mps);
  1534. if (mpsval > 3)
  1535. goto bad_mps;
  1536. hs_ep->ep.maxpacket = mps;
  1537. hs_ep->mc = 1;
  1538. } else {
  1539. mpsval = mps & DXEPCTL_MPS_MASK;
  1540. if (mpsval > 1024)
  1541. goto bad_mps;
  1542. mcval = ((mps >> 11) & 0x3) + 1;
  1543. hs_ep->mc = mcval;
  1544. if (mcval > 3)
  1545. goto bad_mps;
  1546. hs_ep->ep.maxpacket = mpsval;
  1547. }
  1548. if (dir_in) {
  1549. reg = dwc2_readl(regs + DIEPCTL(ep));
  1550. reg &= ~DXEPCTL_MPS_MASK;
  1551. reg |= mpsval;
  1552. dwc2_writel(reg, regs + DIEPCTL(ep));
  1553. } else {
  1554. reg = dwc2_readl(regs + DOEPCTL(ep));
  1555. reg &= ~DXEPCTL_MPS_MASK;
  1556. reg |= mpsval;
  1557. dwc2_writel(reg, regs + DOEPCTL(ep));
  1558. }
  1559. return;
  1560. bad_mps:
  1561. dev_err(hsotg->dev, "ep%d: bad mps of %d\n", ep, mps);
  1562. }
  1563. /**
  1564. * dwc2_hsotg_txfifo_flush - flush Tx FIFO
  1565. * @hsotg: The driver state
  1566. * @idx: The index for the endpoint (0..15)
  1567. */
  1568. static void dwc2_hsotg_txfifo_flush(struct dwc2_hsotg *hsotg, unsigned int idx)
  1569. {
  1570. int timeout;
  1571. int val;
  1572. dwc2_writel(GRSTCTL_TXFNUM(idx) | GRSTCTL_TXFFLSH,
  1573. hsotg->regs + GRSTCTL);
  1574. /* wait until the fifo is flushed */
  1575. timeout = 100;
  1576. while (1) {
  1577. val = dwc2_readl(hsotg->regs + GRSTCTL);
  1578. if ((val & (GRSTCTL_TXFFLSH)) == 0)
  1579. break;
  1580. if (--timeout == 0) {
  1581. dev_err(hsotg->dev,
  1582. "%s: timeout flushing fifo (GRSTCTL=%08x)\n",
  1583. __func__, val);
  1584. break;
  1585. }
  1586. udelay(1);
  1587. }
  1588. }
  1589. /**
  1590. * dwc2_hsotg_trytx - check to see if anything needs transmitting
  1591. * @hsotg: The driver state
  1592. * @hs_ep: The driver endpoint to check.
  1593. *
  1594. * Check to see if there is a request that has data to send, and if so
  1595. * make an attempt to write data into the FIFO.
  1596. */
  1597. static int dwc2_hsotg_trytx(struct dwc2_hsotg *hsotg,
  1598. struct dwc2_hsotg_ep *hs_ep)
  1599. {
  1600. struct dwc2_hsotg_req *hs_req = hs_ep->req;
  1601. if (!hs_ep->dir_in || !hs_req) {
  1602. /**
  1603. * if request is not enqueued, we disable interrupts
  1604. * for endpoints, excepting ep0
  1605. */
  1606. if (hs_ep->index != 0)
  1607. dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index,
  1608. hs_ep->dir_in, 0);
  1609. return 0;
  1610. }
  1611. if (hs_req->req.actual < hs_req->req.length) {
  1612. dev_dbg(hsotg->dev, "trying to write more for ep%d\n",
  1613. hs_ep->index);
  1614. return dwc2_hsotg_write_fifo(hsotg, hs_ep, hs_req);
  1615. }
  1616. return 0;
  1617. }
  1618. /**
  1619. * dwc2_hsotg_complete_in - complete IN transfer
  1620. * @hsotg: The device state.
  1621. * @hs_ep: The endpoint that has just completed.
  1622. *
  1623. * An IN transfer has been completed, update the transfer's state and then
  1624. * call the relevant completion routines.
  1625. */
  1626. static void dwc2_hsotg_complete_in(struct dwc2_hsotg *hsotg,
  1627. struct dwc2_hsotg_ep *hs_ep)
  1628. {
  1629. struct dwc2_hsotg_req *hs_req = hs_ep->req;
  1630. u32 epsize = dwc2_readl(hsotg->regs + DIEPTSIZ(hs_ep->index));
  1631. int size_left, size_done;
  1632. if (!hs_req) {
  1633. dev_dbg(hsotg->dev, "XferCompl but no req\n");
  1634. return;
  1635. }
  1636. /* Finish ZLP handling for IN EP0 transactions */
  1637. if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_IN) {
  1638. dev_dbg(hsotg->dev, "zlp packet sent\n");
  1639. dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
  1640. if (hsotg->test_mode) {
  1641. int ret;
  1642. ret = dwc2_hsotg_set_test_mode(hsotg, hsotg->test_mode);
  1643. if (ret < 0) {
  1644. dev_dbg(hsotg->dev, "Invalid Test #%d\n",
  1645. hsotg->test_mode);
  1646. dwc2_hsotg_stall_ep0(hsotg);
  1647. return;
  1648. }
  1649. }
  1650. dwc2_hsotg_enqueue_setup(hsotg);
  1651. return;
  1652. }
  1653. /*
  1654. * Calculate the size of the transfer by checking how much is left
  1655. * in the endpoint size register and then working it out from
  1656. * the amount we loaded for the transfer.
  1657. *
  1658. * We do this even for DMA, as the transfer may have incremented
  1659. * past the end of the buffer (DMA transfers are always 32bit
  1660. * aligned).
  1661. */
  1662. size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
  1663. size_done = hs_ep->size_loaded - size_left;
  1664. size_done += hs_ep->last_load;
  1665. if (hs_req->req.actual != size_done)
  1666. dev_dbg(hsotg->dev, "%s: adjusting size done %d => %d\n",
  1667. __func__, hs_req->req.actual, size_done);
  1668. hs_req->req.actual = size_done;
  1669. dev_dbg(hsotg->dev, "req->length:%d req->actual:%d req->zero:%d\n",
  1670. hs_req->req.length, hs_req->req.actual, hs_req->req.zero);
  1671. if (!size_left && hs_req->req.actual < hs_req->req.length) {
  1672. dev_dbg(hsotg->dev, "%s trying more for req...\n", __func__);
  1673. dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, true);
  1674. return;
  1675. }
  1676. /* Zlp for all endpoints, for ep0 only in DATA IN stage */
  1677. if (hs_ep->send_zlp) {
  1678. dwc2_hsotg_program_zlp(hsotg, hs_ep);
  1679. hs_ep->send_zlp = 0;
  1680. /* transfer will be completed on next complete interrupt */
  1681. return;
  1682. }
  1683. if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_DATA_IN) {
  1684. /* Move to STATUS OUT */
  1685. dwc2_hsotg_ep0_zlp(hsotg, false);
  1686. return;
  1687. }
  1688. dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
  1689. }
  1690. /**
  1691. * dwc2_gadget_read_ep_interrupts - reads interrupts for given ep
  1692. * @hsotg: The device state.
  1693. * @idx: Index of ep.
  1694. * @dir_in: Endpoint direction 1-in 0-out.
  1695. *
  1696. * Reads for endpoint with given index and direction, by masking
  1697. * epint_reg with coresponding mask.
  1698. */
  1699. static u32 dwc2_gadget_read_ep_interrupts(struct dwc2_hsotg *hsotg,
  1700. unsigned int idx, int dir_in)
  1701. {
  1702. u32 epmsk_reg = dir_in ? DIEPMSK : DOEPMSK;
  1703. u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx);
  1704. u32 ints;
  1705. u32 mask;
  1706. u32 diepempmsk;
  1707. mask = dwc2_readl(hsotg->regs + epmsk_reg);
  1708. diepempmsk = dwc2_readl(hsotg->regs + DIEPEMPMSK);
  1709. mask |= ((diepempmsk >> idx) & 0x1) ? DIEPMSK_TXFIFOEMPTY : 0;
  1710. mask |= DXEPINT_SETUP_RCVD;
  1711. ints = dwc2_readl(hsotg->regs + epint_reg);
  1712. ints &= mask;
  1713. return ints;
  1714. }
  1715. /**
  1716. * dwc2_gadget_handle_ep_disabled - handle DXEPINT_EPDISBLD
  1717. * @hs_ep: The endpoint on which interrupt is asserted.
  1718. *
  1719. * This interrupt indicates that the endpoint has been disabled per the
  1720. * application's request.
  1721. *
  1722. * For IN endpoints flushes txfifo, in case of BULK clears DCTL_CGNPINNAK,
  1723. * in case of ISOC completes current request.
  1724. *
  1725. * For ISOC-OUT endpoints completes expired requests. If there is remaining
  1726. * request starts it.
  1727. */
  1728. static void dwc2_gadget_handle_ep_disabled(struct dwc2_hsotg_ep *hs_ep)
  1729. {
  1730. struct dwc2_hsotg *hsotg = hs_ep->parent;
  1731. struct dwc2_hsotg_req *hs_req;
  1732. unsigned char idx = hs_ep->index;
  1733. int dir_in = hs_ep->dir_in;
  1734. u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx);
  1735. int dctl = dwc2_readl(hsotg->regs + DCTL);
  1736. dev_dbg(hsotg->dev, "%s: EPDisbld\n", __func__);
  1737. if (dir_in) {
  1738. int epctl = dwc2_readl(hsotg->regs + epctl_reg);
  1739. dwc2_hsotg_txfifo_flush(hsotg, hs_ep->fifo_index);
  1740. if (hs_ep->isochronous) {
  1741. dwc2_hsotg_complete_in(hsotg, hs_ep);
  1742. return;
  1743. }
  1744. if ((epctl & DXEPCTL_STALL) && (epctl & DXEPCTL_EPTYPE_BULK)) {
  1745. int dctl = dwc2_readl(hsotg->regs + DCTL);
  1746. dctl |= DCTL_CGNPINNAK;
  1747. dwc2_writel(dctl, hsotg->regs + DCTL);
  1748. }
  1749. return;
  1750. }
  1751. if (dctl & DCTL_GOUTNAKSTS) {
  1752. dctl |= DCTL_CGOUTNAK;
  1753. dwc2_writel(dctl, hsotg->regs + DCTL);
  1754. }
  1755. if (!hs_ep->isochronous)
  1756. return;
  1757. if (list_empty(&hs_ep->queue)) {
  1758. dev_dbg(hsotg->dev, "%s: complete_ep 0x%p, ep->queue empty!\n",
  1759. __func__, hs_ep);
  1760. return;
  1761. }
  1762. do {
  1763. hs_req = get_ep_head(hs_ep);
  1764. if (hs_req)
  1765. dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req,
  1766. -ENODATA);
  1767. dwc2_gadget_incr_frame_num(hs_ep);
  1768. } while (dwc2_gadget_target_frame_elapsed(hs_ep));
  1769. dwc2_gadget_start_next_request(hs_ep);
  1770. }
  1771. /**
  1772. * dwc2_gadget_handle_out_token_ep_disabled - handle DXEPINT_OUTTKNEPDIS
  1773. * @hs_ep: The endpoint on which interrupt is asserted.
  1774. *
  1775. * This is starting point for ISOC-OUT transfer, synchronization done with
  1776. * first out token received from host while corresponding EP is disabled.
  1777. *
  1778. * Device does not know initial frame in which out token will come. For this
  1779. * HW generates OUTTKNEPDIS - out token is received while EP is disabled. Upon
  1780. * getting this interrupt SW starts calculation for next transfer frame.
  1781. */
  1782. static void dwc2_gadget_handle_out_token_ep_disabled(struct dwc2_hsotg_ep *ep)
  1783. {
  1784. struct dwc2_hsotg *hsotg = ep->parent;
  1785. int dir_in = ep->dir_in;
  1786. u32 doepmsk;
  1787. if (dir_in || !ep->isochronous)
  1788. return;
  1789. dwc2_hsotg_complete_request(hsotg, ep, get_ep_head(ep), -ENODATA);
  1790. if (ep->interval > 1 &&
  1791. ep->target_frame == TARGET_FRAME_INITIAL) {
  1792. u32 dsts;
  1793. u32 ctrl;
  1794. dsts = dwc2_readl(hsotg->regs + DSTS);
  1795. ep->target_frame = dwc2_hsotg_read_frameno(hsotg);
  1796. dwc2_gadget_incr_frame_num(ep);
  1797. ctrl = dwc2_readl(hsotg->regs + DOEPCTL(ep->index));
  1798. if (ep->target_frame & 0x1)
  1799. ctrl |= DXEPCTL_SETODDFR;
  1800. else
  1801. ctrl |= DXEPCTL_SETEVENFR;
  1802. dwc2_writel(ctrl, hsotg->regs + DOEPCTL(ep->index));
  1803. }
  1804. dwc2_gadget_start_next_request(ep);
  1805. doepmsk = dwc2_readl(hsotg->regs + DOEPMSK);
  1806. doepmsk &= ~DOEPMSK_OUTTKNEPDISMSK;
  1807. dwc2_writel(doepmsk, hsotg->regs + DOEPMSK);
  1808. }
  1809. /**
  1810. * dwc2_gadget_handle_nak - handle NAK interrupt
  1811. * @hs_ep: The endpoint on which interrupt is asserted.
  1812. *
  1813. * This is starting point for ISOC-IN transfer, synchronization done with
  1814. * first IN token received from host while corresponding EP is disabled.
  1815. *
  1816. * Device does not know when first one token will arrive from host. On first
  1817. * token arrival HW generates 2 interrupts: 'in token received while FIFO empty'
  1818. * and 'NAK'. NAK interrupt for ISOC-IN means that token has arrived and ZLP was
  1819. * sent in response to that as there was no data in FIFO. SW is basing on this
  1820. * interrupt to obtain frame in which token has come and then based on the
  1821. * interval calculates next frame for transfer.
  1822. */
  1823. static void dwc2_gadget_handle_nak(struct dwc2_hsotg_ep *hs_ep)
  1824. {
  1825. struct dwc2_hsotg *hsotg = hs_ep->parent;
  1826. int dir_in = hs_ep->dir_in;
  1827. if (!dir_in || !hs_ep->isochronous)
  1828. return;
  1829. if (hs_ep->target_frame == TARGET_FRAME_INITIAL) {
  1830. hs_ep->target_frame = dwc2_hsotg_read_frameno(hsotg);
  1831. if (hs_ep->interval > 1) {
  1832. u32 ctrl = dwc2_readl(hsotg->regs +
  1833. DIEPCTL(hs_ep->index));
  1834. if (hs_ep->target_frame & 0x1)
  1835. ctrl |= DXEPCTL_SETODDFR;
  1836. else
  1837. ctrl |= DXEPCTL_SETEVENFR;
  1838. dwc2_writel(ctrl, hsotg->regs + DIEPCTL(hs_ep->index));
  1839. }
  1840. dwc2_hsotg_complete_request(hsotg, hs_ep,
  1841. get_ep_head(hs_ep), 0);
  1842. }
  1843. dwc2_gadget_incr_frame_num(hs_ep);
  1844. }
  1845. /**
  1846. * dwc2_hsotg_epint - handle an in/out endpoint interrupt
  1847. * @hsotg: The driver state
  1848. * @idx: The index for the endpoint (0..15)
  1849. * @dir_in: Set if this is an IN endpoint
  1850. *
  1851. * Process and clear any interrupt pending for an individual endpoint
  1852. */
  1853. static void dwc2_hsotg_epint(struct dwc2_hsotg *hsotg, unsigned int idx,
  1854. int dir_in)
  1855. {
  1856. struct dwc2_hsotg_ep *hs_ep = index_to_ep(hsotg, idx, dir_in);
  1857. u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx);
  1858. u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx);
  1859. u32 epsiz_reg = dir_in ? DIEPTSIZ(idx) : DOEPTSIZ(idx);
  1860. u32 ints;
  1861. u32 ctrl;
  1862. ints = dwc2_gadget_read_ep_interrupts(hsotg, idx, dir_in);
  1863. ctrl = dwc2_readl(hsotg->regs + epctl_reg);
  1864. /* Clear endpoint interrupts */
  1865. dwc2_writel(ints, hsotg->regs + epint_reg);
  1866. if (!hs_ep) {
  1867. dev_err(hsotg->dev, "%s:Interrupt for unconfigured ep%d(%s)\n",
  1868. __func__, idx, dir_in ? "in" : "out");
  1869. return;
  1870. }
  1871. dev_dbg(hsotg->dev, "%s: ep%d(%s) DxEPINT=0x%08x\n",
  1872. __func__, idx, dir_in ? "in" : "out", ints);
  1873. /* Don't process XferCompl interrupt if it is a setup packet */
  1874. if (idx == 0 && (ints & (DXEPINT_SETUP | DXEPINT_SETUP_RCVD)))
  1875. ints &= ~DXEPINT_XFERCOMPL;
  1876. if (ints & DXEPINT_STSPHSERCVD)
  1877. dev_dbg(hsotg->dev, "%s: StsPhseRcvd asserted\n", __func__);
  1878. if (ints & DXEPINT_XFERCOMPL) {
  1879. dev_dbg(hsotg->dev,
  1880. "%s: XferCompl: DxEPCTL=0x%08x, DXEPTSIZ=%08x\n",
  1881. __func__, dwc2_readl(hsotg->regs + epctl_reg),
  1882. dwc2_readl(hsotg->regs + epsiz_reg));
  1883. /*
  1884. * we get OutDone from the FIFO, so we only need to look
  1885. * at completing IN requests here
  1886. */
  1887. if (dir_in) {
  1888. if (hs_ep->isochronous && hs_ep->interval > 1)
  1889. dwc2_gadget_incr_frame_num(hs_ep);
  1890. dwc2_hsotg_complete_in(hsotg, hs_ep);
  1891. if (ints & DXEPINT_NAKINTRPT)
  1892. ints &= ~DXEPINT_NAKINTRPT;
  1893. if (idx == 0 && !hs_ep->req)
  1894. dwc2_hsotg_enqueue_setup(hsotg);
  1895. } else if (using_dma(hsotg)) {
  1896. /*
  1897. * We're using DMA, we need to fire an OutDone here
  1898. * as we ignore the RXFIFO.
  1899. */
  1900. if (hs_ep->isochronous && hs_ep->interval > 1)
  1901. dwc2_gadget_incr_frame_num(hs_ep);
  1902. dwc2_hsotg_handle_outdone(hsotg, idx);
  1903. }
  1904. }
  1905. if (ints & DXEPINT_EPDISBLD)
  1906. dwc2_gadget_handle_ep_disabled(hs_ep);
  1907. if (ints & DXEPINT_OUTTKNEPDIS)
  1908. dwc2_gadget_handle_out_token_ep_disabled(hs_ep);
  1909. if (ints & DXEPINT_NAKINTRPT)
  1910. dwc2_gadget_handle_nak(hs_ep);
  1911. if (ints & DXEPINT_AHBERR)
  1912. dev_dbg(hsotg->dev, "%s: AHBErr\n", __func__);
  1913. if (ints & DXEPINT_SETUP) { /* Setup or Timeout */
  1914. dev_dbg(hsotg->dev, "%s: Setup/Timeout\n", __func__);
  1915. if (using_dma(hsotg) && idx == 0) {
  1916. /*
  1917. * this is the notification we've received a
  1918. * setup packet. In non-DMA mode we'd get this
  1919. * from the RXFIFO, instead we need to process
  1920. * the setup here.
  1921. */
  1922. if (dir_in)
  1923. WARN_ON_ONCE(1);
  1924. else
  1925. dwc2_hsotg_handle_outdone(hsotg, 0);
  1926. }
  1927. }
  1928. if (ints & DXEPINT_BACK2BACKSETUP)
  1929. dev_dbg(hsotg->dev, "%s: B2BSetup/INEPNakEff\n", __func__);
  1930. if (dir_in && !hs_ep->isochronous) {
  1931. /* not sure if this is important, but we'll clear it anyway */
  1932. if (ints & DXEPINT_INTKNTXFEMP) {
  1933. dev_dbg(hsotg->dev, "%s: ep%d: INTknTXFEmpMsk\n",
  1934. __func__, idx);
  1935. }
  1936. /* this probably means something bad is happening */
  1937. if (ints & DXEPINT_INTKNEPMIS) {
  1938. dev_warn(hsotg->dev, "%s: ep%d: INTknEP\n",
  1939. __func__, idx);
  1940. }
  1941. /* FIFO has space or is empty (see GAHBCFG) */
  1942. if (hsotg->dedicated_fifos &&
  1943. ints & DXEPINT_TXFEMP) {
  1944. dev_dbg(hsotg->dev, "%s: ep%d: TxFIFOEmpty\n",
  1945. __func__, idx);
  1946. if (!using_dma(hsotg))
  1947. dwc2_hsotg_trytx(hsotg, hs_ep);
  1948. }
  1949. }
  1950. }
  1951. /**
  1952. * dwc2_hsotg_irq_enumdone - Handle EnumDone interrupt (enumeration done)
  1953. * @hsotg: The device state.
  1954. *
  1955. * Handle updating the device settings after the enumeration phase has
  1956. * been completed.
  1957. */
  1958. static void dwc2_hsotg_irq_enumdone(struct dwc2_hsotg *hsotg)
  1959. {
  1960. u32 dsts = dwc2_readl(hsotg->regs + DSTS);
  1961. int ep0_mps = 0, ep_mps = 8;
  1962. /*
  1963. * This should signal the finish of the enumeration phase
  1964. * of the USB handshaking, so we should now know what rate
  1965. * we connected at.
  1966. */
  1967. dev_dbg(hsotg->dev, "EnumDone (DSTS=0x%08x)\n", dsts);
  1968. /*
  1969. * note, since we're limited by the size of transfer on EP0, and
  1970. * it seems IN transfers must be a even number of packets we do
  1971. * not advertise a 64byte MPS on EP0.
  1972. */
  1973. /* catch both EnumSpd_FS and EnumSpd_FS48 */
  1974. switch ((dsts & DSTS_ENUMSPD_MASK) >> DSTS_ENUMSPD_SHIFT) {
  1975. case DSTS_ENUMSPD_FS:
  1976. case DSTS_ENUMSPD_FS48:
  1977. hsotg->gadget.speed = USB_SPEED_FULL;
  1978. ep0_mps = EP0_MPS_LIMIT;
  1979. ep_mps = 1023;
  1980. break;
  1981. case DSTS_ENUMSPD_HS:
  1982. hsotg->gadget.speed = USB_SPEED_HIGH;
  1983. ep0_mps = EP0_MPS_LIMIT;
  1984. ep_mps = 1024;
  1985. break;
  1986. case DSTS_ENUMSPD_LS:
  1987. hsotg->gadget.speed = USB_SPEED_LOW;
  1988. /*
  1989. * note, we don't actually support LS in this driver at the
  1990. * moment, and the documentation seems to imply that it isn't
  1991. * supported by the PHYs on some of the devices.
  1992. */
  1993. break;
  1994. }
  1995. dev_info(hsotg->dev, "new device is %s\n",
  1996. usb_speed_string(hsotg->gadget.speed));
  1997. /*
  1998. * we should now know the maximum packet size for an
  1999. * endpoint, so set the endpoints to a default value.
  2000. */
  2001. if (ep0_mps) {
  2002. int i;
  2003. /* Initialize ep0 for both in and out directions */
  2004. dwc2_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 1);
  2005. dwc2_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 0);
  2006. for (i = 1; i < hsotg->num_of_eps; i++) {
  2007. if (hsotg->eps_in[i])
  2008. dwc2_hsotg_set_ep_maxpacket(hsotg, i, ep_mps, 1);
  2009. if (hsotg->eps_out[i])
  2010. dwc2_hsotg_set_ep_maxpacket(hsotg, i, ep_mps, 0);
  2011. }
  2012. }
  2013. /* ensure after enumeration our EP0 is active */
  2014. dwc2_hsotg_enqueue_setup(hsotg);
  2015. dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
  2016. dwc2_readl(hsotg->regs + DIEPCTL0),
  2017. dwc2_readl(hsotg->regs + DOEPCTL0));
  2018. }
  2019. /**
  2020. * kill_all_requests - remove all requests from the endpoint's queue
  2021. * @hsotg: The device state.
  2022. * @ep: The endpoint the requests may be on.
  2023. * @result: The result code to use.
  2024. *
  2025. * Go through the requests on the given endpoint and mark them
  2026. * completed with the given result code.
  2027. */
  2028. static void kill_all_requests(struct dwc2_hsotg *hsotg,
  2029. struct dwc2_hsotg_ep *ep,
  2030. int result)
  2031. {
  2032. struct dwc2_hsotg_req *req, *treq;
  2033. unsigned size;
  2034. ep->req = NULL;
  2035. list_for_each_entry_safe(req, treq, &ep->queue, queue)
  2036. dwc2_hsotg_complete_request(hsotg, ep, req,
  2037. result);
  2038. if (!hsotg->dedicated_fifos)
  2039. return;
  2040. size = (dwc2_readl(hsotg->regs + DTXFSTS(ep->index)) & 0xffff) * 4;
  2041. if (size < ep->fifo_size)
  2042. dwc2_hsotg_txfifo_flush(hsotg, ep->fifo_index);
  2043. }
  2044. /**
  2045. * dwc2_hsotg_disconnect - disconnect service
  2046. * @hsotg: The device state.
  2047. *
  2048. * The device has been disconnected. Remove all current
  2049. * transactions and signal the gadget driver that this
  2050. * has happened.
  2051. */
  2052. void dwc2_hsotg_disconnect(struct dwc2_hsotg *hsotg)
  2053. {
  2054. unsigned ep;
  2055. if (!hsotg->connected)
  2056. return;
  2057. hsotg->connected = 0;
  2058. hsotg->test_mode = 0;
  2059. for (ep = 0; ep < hsotg->num_of_eps; ep++) {
  2060. if (hsotg->eps_in[ep])
  2061. kill_all_requests(hsotg, hsotg->eps_in[ep],
  2062. -ESHUTDOWN);
  2063. if (hsotg->eps_out[ep])
  2064. kill_all_requests(hsotg, hsotg->eps_out[ep],
  2065. -ESHUTDOWN);
  2066. }
  2067. call_gadget(hsotg, disconnect);
  2068. hsotg->lx_state = DWC2_L3;
  2069. }
  2070. /**
  2071. * dwc2_hsotg_irq_fifoempty - TX FIFO empty interrupt handler
  2072. * @hsotg: The device state:
  2073. * @periodic: True if this is a periodic FIFO interrupt
  2074. */
  2075. static void dwc2_hsotg_irq_fifoempty(struct dwc2_hsotg *hsotg, bool periodic)
  2076. {
  2077. struct dwc2_hsotg_ep *ep;
  2078. int epno, ret;
  2079. /* look through for any more data to transmit */
  2080. for (epno = 0; epno < hsotg->num_of_eps; epno++) {
  2081. ep = index_to_ep(hsotg, epno, 1);
  2082. if (!ep)
  2083. continue;
  2084. if (!ep->dir_in)
  2085. continue;
  2086. if ((periodic && !ep->periodic) ||
  2087. (!periodic && ep->periodic))
  2088. continue;
  2089. ret = dwc2_hsotg_trytx(hsotg, ep);
  2090. if (ret < 0)
  2091. break;
  2092. }
  2093. }
  2094. /* IRQ flags which will trigger a retry around the IRQ loop */
  2095. #define IRQ_RETRY_MASK (GINTSTS_NPTXFEMP | \
  2096. GINTSTS_PTXFEMP | \
  2097. GINTSTS_RXFLVL)
  2098. /**
  2099. * dwc2_hsotg_core_init - issue softreset to the core
  2100. * @hsotg: The device state
  2101. *
  2102. * Issue a soft reset to the core, and await the core finishing it.
  2103. */
  2104. void dwc2_hsotg_core_init_disconnected(struct dwc2_hsotg *hsotg,
  2105. bool is_usb_reset)
  2106. {
  2107. u32 intmsk;
  2108. u32 val;
  2109. u32 usbcfg;
  2110. /* Kill any ep0 requests as controller will be reinitialized */
  2111. kill_all_requests(hsotg, hsotg->eps_out[0], -ECONNRESET);
  2112. if (!is_usb_reset)
  2113. if (dwc2_core_reset(hsotg))
  2114. return;
  2115. /*
  2116. * we must now enable ep0 ready for host detection and then
  2117. * set configuration.
  2118. */
  2119. /* keep other bits untouched (so e.g. forced modes are not lost) */
  2120. usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
  2121. usbcfg &= ~(GUSBCFG_TOUTCAL_MASK | GUSBCFG_PHYIF16 | GUSBCFG_SRPCAP |
  2122. GUSBCFG_HNPCAP);
  2123. /* set the PLL on, remove the HNP/SRP and set the PHY */
  2124. val = (hsotg->phyif == GUSBCFG_PHYIF8) ? 9 : 5;
  2125. usbcfg |= hsotg->phyif | GUSBCFG_TOUTCAL(7) |
  2126. (val << GUSBCFG_USBTRDTIM_SHIFT);
  2127. dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
  2128. dwc2_hsotg_init_fifo(hsotg);
  2129. if (!is_usb_reset)
  2130. __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON);
  2131. dwc2_writel(DCFG_EPMISCNT(1) | DCFG_DEVSPD_HS, hsotg->regs + DCFG);
  2132. /* Clear any pending OTG interrupts */
  2133. dwc2_writel(0xffffffff, hsotg->regs + GOTGINT);
  2134. /* Clear any pending interrupts */
  2135. dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
  2136. intmsk = GINTSTS_ERLYSUSP | GINTSTS_SESSREQINT |
  2137. GINTSTS_GOUTNAKEFF | GINTSTS_GINNAKEFF |
  2138. GINTSTS_USBRST | GINTSTS_RESETDET |
  2139. GINTSTS_ENUMDONE | GINTSTS_OTGINT |
  2140. GINTSTS_USBSUSP | GINTSTS_WKUPINT |
  2141. GINTSTS_INCOMPL_SOIN | GINTSTS_INCOMPL_SOOUT;
  2142. if (hsotg->core_params->external_id_pin_ctl <= 0)
  2143. intmsk |= GINTSTS_CONIDSTSCHNG;
  2144. dwc2_writel(intmsk, hsotg->regs + GINTMSK);
  2145. if (using_dma(hsotg))
  2146. dwc2_writel(GAHBCFG_GLBL_INTR_EN | GAHBCFG_DMA_EN |
  2147. (GAHBCFG_HBSTLEN_INCR4 << GAHBCFG_HBSTLEN_SHIFT),
  2148. hsotg->regs + GAHBCFG);
  2149. else
  2150. dwc2_writel(((hsotg->dedicated_fifos) ?
  2151. (GAHBCFG_NP_TXF_EMP_LVL |
  2152. GAHBCFG_P_TXF_EMP_LVL) : 0) |
  2153. GAHBCFG_GLBL_INTR_EN, hsotg->regs + GAHBCFG);
  2154. /*
  2155. * If INTknTXFEmpMsk is enabled, it's important to disable ep interrupts
  2156. * when we have no data to transfer. Otherwise we get being flooded by
  2157. * interrupts.
  2158. */
  2159. dwc2_writel(((hsotg->dedicated_fifos && !using_dma(hsotg)) ?
  2160. DIEPMSK_TXFIFOEMPTY | DIEPMSK_INTKNTXFEMPMSK : 0) |
  2161. DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK |
  2162. DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK,
  2163. hsotg->regs + DIEPMSK);
  2164. /*
  2165. * don't need XferCompl, we get that from RXFIFO in slave mode. In
  2166. * DMA mode we may need this.
  2167. */
  2168. dwc2_writel((using_dma(hsotg) ? (DIEPMSK_XFERCOMPLMSK) : 0) |
  2169. DOEPMSK_EPDISBLDMSK | DOEPMSK_AHBERRMSK |
  2170. DOEPMSK_SETUPMSK | DOEPMSK_STSPHSERCVDMSK,
  2171. hsotg->regs + DOEPMSK);
  2172. dwc2_writel(0, hsotg->regs + DAINTMSK);
  2173. dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
  2174. dwc2_readl(hsotg->regs + DIEPCTL0),
  2175. dwc2_readl(hsotg->regs + DOEPCTL0));
  2176. /* enable in and out endpoint interrupts */
  2177. dwc2_hsotg_en_gsint(hsotg, GINTSTS_OEPINT | GINTSTS_IEPINT);
  2178. /*
  2179. * Enable the RXFIFO when in slave mode, as this is how we collect
  2180. * the data. In DMA mode, we get events from the FIFO but also
  2181. * things we cannot process, so do not use it.
  2182. */
  2183. if (!using_dma(hsotg))
  2184. dwc2_hsotg_en_gsint(hsotg, GINTSTS_RXFLVL);
  2185. /* Enable interrupts for EP0 in and out */
  2186. dwc2_hsotg_ctrl_epint(hsotg, 0, 0, 1);
  2187. dwc2_hsotg_ctrl_epint(hsotg, 0, 1, 1);
  2188. if (!is_usb_reset) {
  2189. __orr32(hsotg->regs + DCTL, DCTL_PWRONPRGDONE);
  2190. udelay(10); /* see openiboot */
  2191. __bic32(hsotg->regs + DCTL, DCTL_PWRONPRGDONE);
  2192. }
  2193. dev_dbg(hsotg->dev, "DCTL=0x%08x\n", dwc2_readl(hsotg->regs + DCTL));
  2194. /*
  2195. * DxEPCTL_USBActEp says RO in manual, but seems to be set by
  2196. * writing to the EPCTL register..
  2197. */
  2198. /* set to read 1 8byte packet */
  2199. dwc2_writel(DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
  2200. DXEPTSIZ_XFERSIZE(8), hsotg->regs + DOEPTSIZ0);
  2201. dwc2_writel(dwc2_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) |
  2202. DXEPCTL_CNAK | DXEPCTL_EPENA |
  2203. DXEPCTL_USBACTEP,
  2204. hsotg->regs + DOEPCTL0);
  2205. /* enable, but don't activate EP0in */
  2206. dwc2_writel(dwc2_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) |
  2207. DXEPCTL_USBACTEP, hsotg->regs + DIEPCTL0);
  2208. dwc2_hsotg_enqueue_setup(hsotg);
  2209. dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
  2210. dwc2_readl(hsotg->regs + DIEPCTL0),
  2211. dwc2_readl(hsotg->regs + DOEPCTL0));
  2212. /* clear global NAKs */
  2213. val = DCTL_CGOUTNAK | DCTL_CGNPINNAK;
  2214. if (!is_usb_reset)
  2215. val |= DCTL_SFTDISCON;
  2216. __orr32(hsotg->regs + DCTL, val);
  2217. /* must be at-least 3ms to allow bus to see disconnect */
  2218. mdelay(3);
  2219. hsotg->lx_state = DWC2_L0;
  2220. }
  2221. static void dwc2_hsotg_core_disconnect(struct dwc2_hsotg *hsotg)
  2222. {
  2223. /* set the soft-disconnect bit */
  2224. __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON);
  2225. }
  2226. void dwc2_hsotg_core_connect(struct dwc2_hsotg *hsotg)
  2227. {
  2228. /* remove the soft-disconnect and let's go */
  2229. __bic32(hsotg->regs + DCTL, DCTL_SFTDISCON);
  2230. }
  2231. /**
  2232. * dwc2_gadget_handle_incomplete_isoc_in - handle incomplete ISO IN Interrupt.
  2233. * @hsotg: The device state:
  2234. *
  2235. * This interrupt indicates one of the following conditions occurred while
  2236. * transmitting an ISOC transaction.
  2237. * - Corrupted IN Token for ISOC EP.
  2238. * - Packet not complete in FIFO.
  2239. *
  2240. * The following actions will be taken:
  2241. * - Determine the EP
  2242. * - Disable EP; when 'Endpoint Disabled' interrupt is received Flush FIFO
  2243. */
  2244. static void dwc2_gadget_handle_incomplete_isoc_in(struct dwc2_hsotg *hsotg)
  2245. {
  2246. struct dwc2_hsotg_ep *hs_ep;
  2247. u32 epctrl;
  2248. u32 idx;
  2249. dev_dbg(hsotg->dev, "Incomplete isoc in interrupt received:\n");
  2250. for (idx = 1; idx <= hsotg->num_of_eps; idx++) {
  2251. hs_ep = hsotg->eps_in[idx];
  2252. epctrl = dwc2_readl(hsotg->regs + DIEPCTL(idx));
  2253. if ((epctrl & DXEPCTL_EPENA) && hs_ep->isochronous &&
  2254. dwc2_gadget_target_frame_elapsed(hs_ep)) {
  2255. epctrl |= DXEPCTL_SNAK;
  2256. epctrl |= DXEPCTL_EPDIS;
  2257. dwc2_writel(epctrl, hsotg->regs + DIEPCTL(idx));
  2258. }
  2259. }
  2260. /* Clear interrupt */
  2261. dwc2_writel(GINTSTS_INCOMPL_SOIN, hsotg->regs + GINTSTS);
  2262. }
  2263. /**
  2264. * dwc2_gadget_handle_incomplete_isoc_out - handle incomplete ISO OUT Interrupt
  2265. * @hsotg: The device state:
  2266. *
  2267. * This interrupt indicates one of the following conditions occurred while
  2268. * transmitting an ISOC transaction.
  2269. * - Corrupted OUT Token for ISOC EP.
  2270. * - Packet not complete in FIFO.
  2271. *
  2272. * The following actions will be taken:
  2273. * - Determine the EP
  2274. * - Set DCTL_SGOUTNAK and unmask GOUTNAKEFF if target frame elapsed.
  2275. */
  2276. static void dwc2_gadget_handle_incomplete_isoc_out(struct dwc2_hsotg *hsotg)
  2277. {
  2278. u32 gintsts;
  2279. u32 gintmsk;
  2280. u32 epctrl;
  2281. struct dwc2_hsotg_ep *hs_ep;
  2282. int idx;
  2283. dev_dbg(hsotg->dev, "%s: GINTSTS_INCOMPL_SOOUT\n", __func__);
  2284. for (idx = 1; idx <= hsotg->num_of_eps; idx++) {
  2285. hs_ep = hsotg->eps_out[idx];
  2286. epctrl = dwc2_readl(hsotg->regs + DOEPCTL(idx));
  2287. if ((epctrl & DXEPCTL_EPENA) && hs_ep->isochronous &&
  2288. dwc2_gadget_target_frame_elapsed(hs_ep)) {
  2289. /* Unmask GOUTNAKEFF interrupt */
  2290. gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
  2291. gintmsk |= GINTSTS_GOUTNAKEFF;
  2292. dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
  2293. gintsts = dwc2_readl(hsotg->regs + GINTSTS);
  2294. if (!(gintsts & GINTSTS_GOUTNAKEFF))
  2295. __orr32(hsotg->regs + DCTL, DCTL_SGOUTNAK);
  2296. }
  2297. }
  2298. /* Clear interrupt */
  2299. dwc2_writel(GINTSTS_INCOMPL_SOOUT, hsotg->regs + GINTSTS);
  2300. }
  2301. /**
  2302. * dwc2_hsotg_irq - handle device interrupt
  2303. * @irq: The IRQ number triggered
  2304. * @pw: The pw value when registered the handler.
  2305. */
  2306. static irqreturn_t dwc2_hsotg_irq(int irq, void *pw)
  2307. {
  2308. struct dwc2_hsotg *hsotg = pw;
  2309. int retry_count = 8;
  2310. u32 gintsts;
  2311. u32 gintmsk;
  2312. if (!dwc2_is_device_mode(hsotg))
  2313. return IRQ_NONE;
  2314. spin_lock(&hsotg->lock);
  2315. irq_retry:
  2316. gintsts = dwc2_readl(hsotg->regs + GINTSTS);
  2317. gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
  2318. dev_dbg(hsotg->dev, "%s: %08x %08x (%08x) retry %d\n",
  2319. __func__, gintsts, gintsts & gintmsk, gintmsk, retry_count);
  2320. gintsts &= gintmsk;
  2321. if (gintsts & GINTSTS_RESETDET) {
  2322. dev_dbg(hsotg->dev, "%s: USBRstDet\n", __func__);
  2323. dwc2_writel(GINTSTS_RESETDET, hsotg->regs + GINTSTS);
  2324. /* This event must be used only if controller is suspended */
  2325. if (hsotg->lx_state == DWC2_L2) {
  2326. dwc2_exit_hibernation(hsotg, true);
  2327. hsotg->lx_state = DWC2_L0;
  2328. }
  2329. }
  2330. if (gintsts & (GINTSTS_USBRST | GINTSTS_RESETDET)) {
  2331. u32 usb_status = dwc2_readl(hsotg->regs + GOTGCTL);
  2332. u32 connected = hsotg->connected;
  2333. dev_dbg(hsotg->dev, "%s: USBRst\n", __func__);
  2334. dev_dbg(hsotg->dev, "GNPTXSTS=%08x\n",
  2335. dwc2_readl(hsotg->regs + GNPTXSTS));
  2336. dwc2_writel(GINTSTS_USBRST, hsotg->regs + GINTSTS);
  2337. /* Report disconnection if it is not already done. */
  2338. dwc2_hsotg_disconnect(hsotg);
  2339. if (usb_status & GOTGCTL_BSESVLD && connected)
  2340. dwc2_hsotg_core_init_disconnected(hsotg, true);
  2341. }
  2342. if (gintsts & GINTSTS_ENUMDONE) {
  2343. dwc2_writel(GINTSTS_ENUMDONE, hsotg->regs + GINTSTS);
  2344. dwc2_hsotg_irq_enumdone(hsotg);
  2345. }
  2346. if (gintsts & (GINTSTS_OEPINT | GINTSTS_IEPINT)) {
  2347. u32 daint = dwc2_readl(hsotg->regs + DAINT);
  2348. u32 daintmsk = dwc2_readl(hsotg->regs + DAINTMSK);
  2349. u32 daint_out, daint_in;
  2350. int ep;
  2351. daint &= daintmsk;
  2352. daint_out = daint >> DAINT_OUTEP_SHIFT;
  2353. daint_in = daint & ~(daint_out << DAINT_OUTEP_SHIFT);
  2354. dev_dbg(hsotg->dev, "%s: daint=%08x\n", __func__, daint);
  2355. for (ep = 0; ep < hsotg->num_of_eps && daint_out;
  2356. ep++, daint_out >>= 1) {
  2357. if (daint_out & 1)
  2358. dwc2_hsotg_epint(hsotg, ep, 0);
  2359. }
  2360. for (ep = 0; ep < hsotg->num_of_eps && daint_in;
  2361. ep++, daint_in >>= 1) {
  2362. if (daint_in & 1)
  2363. dwc2_hsotg_epint(hsotg, ep, 1);
  2364. }
  2365. }
  2366. /* check both FIFOs */
  2367. if (gintsts & GINTSTS_NPTXFEMP) {
  2368. dev_dbg(hsotg->dev, "NPTxFEmp\n");
  2369. /*
  2370. * Disable the interrupt to stop it happening again
  2371. * unless one of these endpoint routines decides that
  2372. * it needs re-enabling
  2373. */
  2374. dwc2_hsotg_disable_gsint(hsotg, GINTSTS_NPTXFEMP);
  2375. dwc2_hsotg_irq_fifoempty(hsotg, false);
  2376. }
  2377. if (gintsts & GINTSTS_PTXFEMP) {
  2378. dev_dbg(hsotg->dev, "PTxFEmp\n");
  2379. /* See note in GINTSTS_NPTxFEmp */
  2380. dwc2_hsotg_disable_gsint(hsotg, GINTSTS_PTXFEMP);
  2381. dwc2_hsotg_irq_fifoempty(hsotg, true);
  2382. }
  2383. if (gintsts & GINTSTS_RXFLVL) {
  2384. /*
  2385. * note, since GINTSTS_RxFLvl doubles as FIFO-not-empty,
  2386. * we need to retry dwc2_hsotg_handle_rx if this is still
  2387. * set.
  2388. */
  2389. dwc2_hsotg_handle_rx(hsotg);
  2390. }
  2391. if (gintsts & GINTSTS_ERLYSUSP) {
  2392. dev_dbg(hsotg->dev, "GINTSTS_ErlySusp\n");
  2393. dwc2_writel(GINTSTS_ERLYSUSP, hsotg->regs + GINTSTS);
  2394. }
  2395. /*
  2396. * these next two seem to crop-up occasionally causing the core
  2397. * to shutdown the USB transfer, so try clearing them and logging
  2398. * the occurrence.
  2399. */
  2400. if (gintsts & GINTSTS_GOUTNAKEFF) {
  2401. u8 idx;
  2402. u32 epctrl;
  2403. u32 gintmsk;
  2404. struct dwc2_hsotg_ep *hs_ep;
  2405. /* Mask this interrupt */
  2406. gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
  2407. gintmsk &= ~GINTSTS_GOUTNAKEFF;
  2408. dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
  2409. dev_dbg(hsotg->dev, "GOUTNakEff triggered\n");
  2410. for (idx = 1; idx <= hsotg->num_of_eps; idx++) {
  2411. hs_ep = hsotg->eps_out[idx];
  2412. epctrl = dwc2_readl(hsotg->regs + DOEPCTL(idx));
  2413. if ((epctrl & DXEPCTL_EPENA) && hs_ep->isochronous) {
  2414. epctrl |= DXEPCTL_SNAK;
  2415. epctrl |= DXEPCTL_EPDIS;
  2416. dwc2_writel(epctrl, hsotg->regs + DOEPCTL(idx));
  2417. }
  2418. }
  2419. /* This interrupt bit is cleared in DXEPINT_EPDISBLD handler */
  2420. }
  2421. if (gintsts & GINTSTS_GINNAKEFF) {
  2422. dev_info(hsotg->dev, "GINNakEff triggered\n");
  2423. __orr32(hsotg->regs + DCTL, DCTL_CGNPINNAK);
  2424. dwc2_hsotg_dump(hsotg);
  2425. }
  2426. if (gintsts & GINTSTS_INCOMPL_SOIN)
  2427. dwc2_gadget_handle_incomplete_isoc_in(hsotg);
  2428. if (gintsts & GINTSTS_INCOMPL_SOOUT)
  2429. dwc2_gadget_handle_incomplete_isoc_out(hsotg);
  2430. /*
  2431. * if we've had fifo events, we should try and go around the
  2432. * loop again to see if there's any point in returning yet.
  2433. */
  2434. if (gintsts & IRQ_RETRY_MASK && --retry_count > 0)
  2435. goto irq_retry;
  2436. spin_unlock(&hsotg->lock);
  2437. return IRQ_HANDLED;
  2438. }
  2439. /**
  2440. * dwc2_hsotg_ep_enable - enable the given endpoint
  2441. * @ep: The USB endpint to configure
  2442. * @desc: The USB endpoint descriptor to configure with.
  2443. *
  2444. * This is called from the USB gadget code's usb_ep_enable().
  2445. */
  2446. static int dwc2_hsotg_ep_enable(struct usb_ep *ep,
  2447. const struct usb_endpoint_descriptor *desc)
  2448. {
  2449. struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
  2450. struct dwc2_hsotg *hsotg = hs_ep->parent;
  2451. unsigned long flags;
  2452. unsigned int index = hs_ep->index;
  2453. u32 epctrl_reg;
  2454. u32 epctrl;
  2455. u32 mps;
  2456. u32 mask;
  2457. unsigned int dir_in;
  2458. unsigned int i, val, size;
  2459. int ret = 0;
  2460. dev_dbg(hsotg->dev,
  2461. "%s: ep %s: a 0x%02x, attr 0x%02x, mps 0x%04x, intr %d\n",
  2462. __func__, ep->name, desc->bEndpointAddress, desc->bmAttributes,
  2463. desc->wMaxPacketSize, desc->bInterval);
  2464. /* not to be called for EP0 */
  2465. if (index == 0) {
  2466. dev_err(hsotg->dev, "%s: called for EP 0\n", __func__);
  2467. return -EINVAL;
  2468. }
  2469. dir_in = (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? 1 : 0;
  2470. if (dir_in != hs_ep->dir_in) {
  2471. dev_err(hsotg->dev, "%s: direction mismatch!\n", __func__);
  2472. return -EINVAL;
  2473. }
  2474. mps = usb_endpoint_maxp(desc);
  2475. /* note, we handle this here instead of dwc2_hsotg_set_ep_maxpacket */
  2476. epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
  2477. epctrl = dwc2_readl(hsotg->regs + epctrl_reg);
  2478. dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x from 0x%08x\n",
  2479. __func__, epctrl, epctrl_reg);
  2480. spin_lock_irqsave(&hsotg->lock, flags);
  2481. epctrl &= ~(DXEPCTL_EPTYPE_MASK | DXEPCTL_MPS_MASK);
  2482. epctrl |= DXEPCTL_MPS(mps);
  2483. /*
  2484. * mark the endpoint as active, otherwise the core may ignore
  2485. * transactions entirely for this endpoint
  2486. */
  2487. epctrl |= DXEPCTL_USBACTEP;
  2488. /* update the endpoint state */
  2489. dwc2_hsotg_set_ep_maxpacket(hsotg, hs_ep->index, mps, dir_in);
  2490. /* default, set to non-periodic */
  2491. hs_ep->isochronous = 0;
  2492. hs_ep->periodic = 0;
  2493. hs_ep->halted = 0;
  2494. hs_ep->interval = desc->bInterval;
  2495. switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
  2496. case USB_ENDPOINT_XFER_ISOC:
  2497. epctrl |= DXEPCTL_EPTYPE_ISO;
  2498. epctrl |= DXEPCTL_SETEVENFR;
  2499. hs_ep->isochronous = 1;
  2500. hs_ep->interval = 1 << (desc->bInterval - 1);
  2501. hs_ep->target_frame = TARGET_FRAME_INITIAL;
  2502. if (dir_in) {
  2503. hs_ep->periodic = 1;
  2504. mask = dwc2_readl(hsotg->regs + DIEPMSK);
  2505. mask |= DIEPMSK_NAKMSK;
  2506. dwc2_writel(mask, hsotg->regs + DIEPMSK);
  2507. } else {
  2508. mask = dwc2_readl(hsotg->regs + DOEPMSK);
  2509. mask |= DOEPMSK_OUTTKNEPDISMSK;
  2510. dwc2_writel(mask, hsotg->regs + DOEPMSK);
  2511. }
  2512. break;
  2513. case USB_ENDPOINT_XFER_BULK:
  2514. epctrl |= DXEPCTL_EPTYPE_BULK;
  2515. break;
  2516. case USB_ENDPOINT_XFER_INT:
  2517. if (dir_in)
  2518. hs_ep->periodic = 1;
  2519. if (hsotg->gadget.speed == USB_SPEED_HIGH)
  2520. hs_ep->interval = 1 << (desc->bInterval - 1);
  2521. epctrl |= DXEPCTL_EPTYPE_INTERRUPT;
  2522. break;
  2523. case USB_ENDPOINT_XFER_CONTROL:
  2524. epctrl |= DXEPCTL_EPTYPE_CONTROL;
  2525. break;
  2526. }
  2527. /* If fifo is already allocated for this ep */
  2528. if (hs_ep->fifo_index) {
  2529. size = hs_ep->ep.maxpacket * hs_ep->mc;
  2530. /* If bigger fifo is required deallocate current one */
  2531. if (size > hs_ep->fifo_size) {
  2532. hsotg->fifo_map &= ~(1 << hs_ep->fifo_index);
  2533. hs_ep->fifo_index = 0;
  2534. hs_ep->fifo_size = 0;
  2535. }
  2536. }
  2537. /*
  2538. * if the hardware has dedicated fifos, we must give each IN EP
  2539. * a unique tx-fifo even if it is non-periodic.
  2540. */
  2541. if (dir_in && hsotg->dedicated_fifos && !hs_ep->fifo_index) {
  2542. u32 fifo_index = 0;
  2543. u32 fifo_size = UINT_MAX;
  2544. size = hs_ep->ep.maxpacket*hs_ep->mc;
  2545. for (i = 1; i < hsotg->num_of_eps; ++i) {
  2546. if (hsotg->fifo_map & (1<<i))
  2547. continue;
  2548. val = dwc2_readl(hsotg->regs + DPTXFSIZN(i));
  2549. val = (val >> FIFOSIZE_DEPTH_SHIFT)*4;
  2550. if (val < size)
  2551. continue;
  2552. /* Search for smallest acceptable fifo */
  2553. if (val < fifo_size) {
  2554. fifo_size = val;
  2555. fifo_index = i;
  2556. }
  2557. }
  2558. if (!fifo_index) {
  2559. dev_err(hsotg->dev,
  2560. "%s: No suitable fifo found\n", __func__);
  2561. ret = -ENOMEM;
  2562. goto error;
  2563. }
  2564. hsotg->fifo_map |= 1 << fifo_index;
  2565. epctrl |= DXEPCTL_TXFNUM(fifo_index);
  2566. hs_ep->fifo_index = fifo_index;
  2567. hs_ep->fifo_size = fifo_size;
  2568. }
  2569. /* for non control endpoints, set PID to D0 */
  2570. if (index && !hs_ep->isochronous)
  2571. epctrl |= DXEPCTL_SETD0PID;
  2572. dev_dbg(hsotg->dev, "%s: write DxEPCTL=0x%08x\n",
  2573. __func__, epctrl);
  2574. dwc2_writel(epctrl, hsotg->regs + epctrl_reg);
  2575. dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x\n",
  2576. __func__, dwc2_readl(hsotg->regs + epctrl_reg));
  2577. /* enable the endpoint interrupt */
  2578. dwc2_hsotg_ctrl_epint(hsotg, index, dir_in, 1);
  2579. error:
  2580. spin_unlock_irqrestore(&hsotg->lock, flags);
  2581. return ret;
  2582. }
  2583. /**
  2584. * dwc2_hsotg_ep_disable - disable given endpoint
  2585. * @ep: The endpoint to disable.
  2586. */
  2587. static int dwc2_hsotg_ep_disable(struct usb_ep *ep)
  2588. {
  2589. struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
  2590. struct dwc2_hsotg *hsotg = hs_ep->parent;
  2591. int dir_in = hs_ep->dir_in;
  2592. int index = hs_ep->index;
  2593. unsigned long flags;
  2594. u32 epctrl_reg;
  2595. u32 ctrl;
  2596. dev_dbg(hsotg->dev, "%s(ep %p)\n", __func__, ep);
  2597. if (ep == &hsotg->eps_out[0]->ep) {
  2598. dev_err(hsotg->dev, "%s: called for ep0\n", __func__);
  2599. return -EINVAL;
  2600. }
  2601. epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
  2602. spin_lock_irqsave(&hsotg->lock, flags);
  2603. hsotg->fifo_map &= ~(1<<hs_ep->fifo_index);
  2604. hs_ep->fifo_index = 0;
  2605. hs_ep->fifo_size = 0;
  2606. ctrl = dwc2_readl(hsotg->regs + epctrl_reg);
  2607. ctrl &= ~DXEPCTL_EPENA;
  2608. ctrl &= ~DXEPCTL_USBACTEP;
  2609. ctrl |= DXEPCTL_SNAK;
  2610. dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
  2611. dwc2_writel(ctrl, hsotg->regs + epctrl_reg);
  2612. /* disable endpoint interrupts */
  2613. dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 0);
  2614. /* terminate all requests with shutdown */
  2615. kill_all_requests(hsotg, hs_ep, -ESHUTDOWN);
  2616. spin_unlock_irqrestore(&hsotg->lock, flags);
  2617. return 0;
  2618. }
  2619. /**
  2620. * on_list - check request is on the given endpoint
  2621. * @ep: The endpoint to check.
  2622. * @test: The request to test if it is on the endpoint.
  2623. */
  2624. static bool on_list(struct dwc2_hsotg_ep *ep, struct dwc2_hsotg_req *test)
  2625. {
  2626. struct dwc2_hsotg_req *req, *treq;
  2627. list_for_each_entry_safe(req, treq, &ep->queue, queue) {
  2628. if (req == test)
  2629. return true;
  2630. }
  2631. return false;
  2632. }
  2633. static int dwc2_hsotg_wait_bit_set(struct dwc2_hsotg *hs_otg, u32 reg,
  2634. u32 bit, u32 timeout)
  2635. {
  2636. u32 i;
  2637. for (i = 0; i < timeout; i++) {
  2638. if (dwc2_readl(hs_otg->regs + reg) & bit)
  2639. return 0;
  2640. udelay(1);
  2641. }
  2642. return -ETIMEDOUT;
  2643. }
  2644. static void dwc2_hsotg_ep_stop_xfr(struct dwc2_hsotg *hsotg,
  2645. struct dwc2_hsotg_ep *hs_ep)
  2646. {
  2647. u32 epctrl_reg;
  2648. u32 epint_reg;
  2649. epctrl_reg = hs_ep->dir_in ? DIEPCTL(hs_ep->index) :
  2650. DOEPCTL(hs_ep->index);
  2651. epint_reg = hs_ep->dir_in ? DIEPINT(hs_ep->index) :
  2652. DOEPINT(hs_ep->index);
  2653. dev_dbg(hsotg->dev, "%s: stopping transfer on %s\n", __func__,
  2654. hs_ep->name);
  2655. if (hs_ep->dir_in) {
  2656. __orr32(hsotg->regs + epctrl_reg, DXEPCTL_SNAK);
  2657. /* Wait for Nak effect */
  2658. if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg,
  2659. DXEPINT_INEPNAKEFF, 100))
  2660. dev_warn(hsotg->dev,
  2661. "%s: timeout DIEPINT.NAKEFF\n", __func__);
  2662. } else {
  2663. if (!(dwc2_readl(hsotg->regs + GINTSTS) & GINTSTS_GOUTNAKEFF))
  2664. __orr32(hsotg->regs + DCTL, DCTL_SGOUTNAK);
  2665. /* Wait for global nak to take effect */
  2666. if (dwc2_hsotg_wait_bit_set(hsotg, GINTSTS,
  2667. GINTSTS_GOUTNAKEFF, 100))
  2668. dev_warn(hsotg->dev,
  2669. "%s: timeout GINTSTS.GOUTNAKEFF\n", __func__);
  2670. }
  2671. /* Disable ep */
  2672. __orr32(hsotg->regs + epctrl_reg, DXEPCTL_EPDIS | DXEPCTL_SNAK);
  2673. /* Wait for ep to be disabled */
  2674. if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg, DXEPINT_EPDISBLD, 100))
  2675. dev_warn(hsotg->dev,
  2676. "%s: timeout DOEPCTL.EPDisable\n", __func__);
  2677. if (hs_ep->dir_in) {
  2678. if (hsotg->dedicated_fifos) {
  2679. dwc2_writel(GRSTCTL_TXFNUM(hs_ep->fifo_index) |
  2680. GRSTCTL_TXFFLSH, hsotg->regs + GRSTCTL);
  2681. /* Wait for fifo flush */
  2682. if (dwc2_hsotg_wait_bit_set(hsotg, GRSTCTL,
  2683. GRSTCTL_TXFFLSH, 100))
  2684. dev_warn(hsotg->dev,
  2685. "%s: timeout flushing fifos\n",
  2686. __func__);
  2687. }
  2688. /* TODO: Flush shared tx fifo */
  2689. } else {
  2690. /* Remove global NAKs */
  2691. __bic32(hsotg->regs + DCTL, DCTL_SGOUTNAK);
  2692. }
  2693. }
  2694. /**
  2695. * dwc2_hsotg_ep_dequeue - dequeue given endpoint
  2696. * @ep: The endpoint to dequeue.
  2697. * @req: The request to be removed from a queue.
  2698. */
  2699. static int dwc2_hsotg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
  2700. {
  2701. struct dwc2_hsotg_req *hs_req = our_req(req);
  2702. struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
  2703. struct dwc2_hsotg *hs = hs_ep->parent;
  2704. unsigned long flags;
  2705. dev_dbg(hs->dev, "ep_dequeue(%p,%p)\n", ep, req);
  2706. spin_lock_irqsave(&hs->lock, flags);
  2707. if (!on_list(hs_ep, hs_req)) {
  2708. spin_unlock_irqrestore(&hs->lock, flags);
  2709. return -EINVAL;
  2710. }
  2711. /* Dequeue already started request */
  2712. if (req == &hs_ep->req->req)
  2713. dwc2_hsotg_ep_stop_xfr(hs, hs_ep);
  2714. dwc2_hsotg_complete_request(hs, hs_ep, hs_req, -ECONNRESET);
  2715. spin_unlock_irqrestore(&hs->lock, flags);
  2716. return 0;
  2717. }
  2718. /**
  2719. * dwc2_hsotg_ep_sethalt - set halt on a given endpoint
  2720. * @ep: The endpoint to set halt.
  2721. * @value: Set or unset the halt.
  2722. * @now: If true, stall the endpoint now. Otherwise return -EAGAIN if
  2723. * the endpoint is busy processing requests.
  2724. *
  2725. * We need to stall the endpoint immediately if request comes from set_feature
  2726. * protocol command handler.
  2727. */
  2728. static int dwc2_hsotg_ep_sethalt(struct usb_ep *ep, int value, bool now)
  2729. {
  2730. struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
  2731. struct dwc2_hsotg *hs = hs_ep->parent;
  2732. int index = hs_ep->index;
  2733. u32 epreg;
  2734. u32 epctl;
  2735. u32 xfertype;
  2736. dev_info(hs->dev, "%s(ep %p %s, %d)\n", __func__, ep, ep->name, value);
  2737. if (index == 0) {
  2738. if (value)
  2739. dwc2_hsotg_stall_ep0(hs);
  2740. else
  2741. dev_warn(hs->dev,
  2742. "%s: can't clear halt on ep0\n", __func__);
  2743. return 0;
  2744. }
  2745. if (hs_ep->isochronous) {
  2746. dev_err(hs->dev, "%s is Isochronous Endpoint\n", ep->name);
  2747. return -EINVAL;
  2748. }
  2749. if (!now && value && !list_empty(&hs_ep->queue)) {
  2750. dev_dbg(hs->dev, "%s request is pending, cannot halt\n",
  2751. ep->name);
  2752. return -EAGAIN;
  2753. }
  2754. if (hs_ep->dir_in) {
  2755. epreg = DIEPCTL(index);
  2756. epctl = dwc2_readl(hs->regs + epreg);
  2757. if (value) {
  2758. epctl |= DXEPCTL_STALL | DXEPCTL_SNAK;
  2759. if (epctl & DXEPCTL_EPENA)
  2760. epctl |= DXEPCTL_EPDIS;
  2761. } else {
  2762. epctl &= ~DXEPCTL_STALL;
  2763. xfertype = epctl & DXEPCTL_EPTYPE_MASK;
  2764. if (xfertype == DXEPCTL_EPTYPE_BULK ||
  2765. xfertype == DXEPCTL_EPTYPE_INTERRUPT)
  2766. epctl |= DXEPCTL_SETD0PID;
  2767. }
  2768. dwc2_writel(epctl, hs->regs + epreg);
  2769. } else {
  2770. epreg = DOEPCTL(index);
  2771. epctl = dwc2_readl(hs->regs + epreg);
  2772. if (value)
  2773. epctl |= DXEPCTL_STALL;
  2774. else {
  2775. epctl &= ~DXEPCTL_STALL;
  2776. xfertype = epctl & DXEPCTL_EPTYPE_MASK;
  2777. if (xfertype == DXEPCTL_EPTYPE_BULK ||
  2778. xfertype == DXEPCTL_EPTYPE_INTERRUPT)
  2779. epctl |= DXEPCTL_SETD0PID;
  2780. }
  2781. dwc2_writel(epctl, hs->regs + epreg);
  2782. }
  2783. hs_ep->halted = value;
  2784. return 0;
  2785. }
  2786. /**
  2787. * dwc2_hsotg_ep_sethalt_lock - set halt on a given endpoint with lock held
  2788. * @ep: The endpoint to set halt.
  2789. * @value: Set or unset the halt.
  2790. */
  2791. static int dwc2_hsotg_ep_sethalt_lock(struct usb_ep *ep, int value)
  2792. {
  2793. struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
  2794. struct dwc2_hsotg *hs = hs_ep->parent;
  2795. unsigned long flags = 0;
  2796. int ret = 0;
  2797. spin_lock_irqsave(&hs->lock, flags);
  2798. ret = dwc2_hsotg_ep_sethalt(ep, value, false);
  2799. spin_unlock_irqrestore(&hs->lock, flags);
  2800. return ret;
  2801. }
  2802. static struct usb_ep_ops dwc2_hsotg_ep_ops = {
  2803. .enable = dwc2_hsotg_ep_enable,
  2804. .disable = dwc2_hsotg_ep_disable,
  2805. .alloc_request = dwc2_hsotg_ep_alloc_request,
  2806. .free_request = dwc2_hsotg_ep_free_request,
  2807. .queue = dwc2_hsotg_ep_queue_lock,
  2808. .dequeue = dwc2_hsotg_ep_dequeue,
  2809. .set_halt = dwc2_hsotg_ep_sethalt_lock,
  2810. /* note, don't believe we have any call for the fifo routines */
  2811. };
  2812. /**
  2813. * dwc2_hsotg_init - initalize the usb core
  2814. * @hsotg: The driver state
  2815. */
  2816. static void dwc2_hsotg_init(struct dwc2_hsotg *hsotg)
  2817. {
  2818. u32 trdtim;
  2819. u32 usbcfg;
  2820. /* unmask subset of endpoint interrupts */
  2821. dwc2_writel(DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK |
  2822. DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK,
  2823. hsotg->regs + DIEPMSK);
  2824. dwc2_writel(DOEPMSK_SETUPMSK | DOEPMSK_AHBERRMSK |
  2825. DOEPMSK_EPDISBLDMSK | DOEPMSK_XFERCOMPLMSK,
  2826. hsotg->regs + DOEPMSK);
  2827. dwc2_writel(0, hsotg->regs + DAINTMSK);
  2828. /* Be in disconnected state until gadget is registered */
  2829. __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON);
  2830. /* setup fifos */
  2831. dev_dbg(hsotg->dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
  2832. dwc2_readl(hsotg->regs + GRXFSIZ),
  2833. dwc2_readl(hsotg->regs + GNPTXFSIZ));
  2834. dwc2_hsotg_init_fifo(hsotg);
  2835. /* keep other bits untouched (so e.g. forced modes are not lost) */
  2836. usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
  2837. usbcfg &= ~(GUSBCFG_TOUTCAL_MASK | GUSBCFG_PHYIF16 | GUSBCFG_SRPCAP |
  2838. GUSBCFG_HNPCAP);
  2839. /* set the PLL on, remove the HNP/SRP and set the PHY */
  2840. trdtim = (hsotg->phyif == GUSBCFG_PHYIF8) ? 9 : 5;
  2841. usbcfg |= hsotg->phyif | GUSBCFG_TOUTCAL(7) |
  2842. (trdtim << GUSBCFG_USBTRDTIM_SHIFT);
  2843. dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
  2844. if (using_dma(hsotg))
  2845. __orr32(hsotg->regs + GAHBCFG, GAHBCFG_DMA_EN);
  2846. }
  2847. /**
  2848. * dwc2_hsotg_udc_start - prepare the udc for work
  2849. * @gadget: The usb gadget state
  2850. * @driver: The usb gadget driver
  2851. *
  2852. * Perform initialization to prepare udc device and driver
  2853. * to work.
  2854. */
  2855. static int dwc2_hsotg_udc_start(struct usb_gadget *gadget,
  2856. struct usb_gadget_driver *driver)
  2857. {
  2858. struct dwc2_hsotg *hsotg = to_hsotg(gadget);
  2859. unsigned long flags;
  2860. int ret;
  2861. if (!hsotg) {
  2862. pr_err("%s: called with no device\n", __func__);
  2863. return -ENODEV;
  2864. }
  2865. if (!driver) {
  2866. dev_err(hsotg->dev, "%s: no driver\n", __func__);
  2867. return -EINVAL;
  2868. }
  2869. if (driver->max_speed < USB_SPEED_FULL)
  2870. dev_err(hsotg->dev, "%s: bad speed\n", __func__);
  2871. if (!driver->setup) {
  2872. dev_err(hsotg->dev, "%s: missing entry points\n", __func__);
  2873. return -EINVAL;
  2874. }
  2875. WARN_ON(hsotg->driver);
  2876. driver->driver.bus = NULL;
  2877. hsotg->driver = driver;
  2878. hsotg->gadget.dev.of_node = hsotg->dev->of_node;
  2879. hsotg->gadget.speed = USB_SPEED_UNKNOWN;
  2880. if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) {
  2881. ret = dwc2_lowlevel_hw_enable(hsotg);
  2882. if (ret)
  2883. goto err;
  2884. }
  2885. if (!IS_ERR_OR_NULL(hsotg->uphy))
  2886. otg_set_peripheral(hsotg->uphy->otg, &hsotg->gadget);
  2887. spin_lock_irqsave(&hsotg->lock, flags);
  2888. dwc2_hsotg_init(hsotg);
  2889. dwc2_hsotg_core_init_disconnected(hsotg, false);
  2890. hsotg->enabled = 0;
  2891. spin_unlock_irqrestore(&hsotg->lock, flags);
  2892. dev_info(hsotg->dev, "bound driver %s\n", driver->driver.name);
  2893. return 0;
  2894. err:
  2895. hsotg->driver = NULL;
  2896. return ret;
  2897. }
  2898. /**
  2899. * dwc2_hsotg_udc_stop - stop the udc
  2900. * @gadget: The usb gadget state
  2901. * @driver: The usb gadget driver
  2902. *
  2903. * Stop udc hw block and stay tunned for future transmissions
  2904. */
  2905. static int dwc2_hsotg_udc_stop(struct usb_gadget *gadget)
  2906. {
  2907. struct dwc2_hsotg *hsotg = to_hsotg(gadget);
  2908. unsigned long flags = 0;
  2909. int ep;
  2910. if (!hsotg)
  2911. return -ENODEV;
  2912. /* all endpoints should be shutdown */
  2913. for (ep = 1; ep < hsotg->num_of_eps; ep++) {
  2914. if (hsotg->eps_in[ep])
  2915. dwc2_hsotg_ep_disable(&hsotg->eps_in[ep]->ep);
  2916. if (hsotg->eps_out[ep])
  2917. dwc2_hsotg_ep_disable(&hsotg->eps_out[ep]->ep);
  2918. }
  2919. spin_lock_irqsave(&hsotg->lock, flags);
  2920. hsotg->driver = NULL;
  2921. hsotg->gadget.speed = USB_SPEED_UNKNOWN;
  2922. hsotg->enabled = 0;
  2923. spin_unlock_irqrestore(&hsotg->lock, flags);
  2924. if (!IS_ERR_OR_NULL(hsotg->uphy))
  2925. otg_set_peripheral(hsotg->uphy->otg, NULL);
  2926. if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
  2927. dwc2_lowlevel_hw_disable(hsotg);
  2928. return 0;
  2929. }
  2930. /**
  2931. * dwc2_hsotg_gadget_getframe - read the frame number
  2932. * @gadget: The usb gadget state
  2933. *
  2934. * Read the {micro} frame number
  2935. */
  2936. static int dwc2_hsotg_gadget_getframe(struct usb_gadget *gadget)
  2937. {
  2938. return dwc2_hsotg_read_frameno(to_hsotg(gadget));
  2939. }
  2940. /**
  2941. * dwc2_hsotg_pullup - connect/disconnect the USB PHY
  2942. * @gadget: The usb gadget state
  2943. * @is_on: Current state of the USB PHY
  2944. *
  2945. * Connect/Disconnect the USB PHY pullup
  2946. */
  2947. static int dwc2_hsotg_pullup(struct usb_gadget *gadget, int is_on)
  2948. {
  2949. struct dwc2_hsotg *hsotg = to_hsotg(gadget);
  2950. unsigned long flags = 0;
  2951. dev_dbg(hsotg->dev, "%s: is_on: %d op_state: %d\n", __func__, is_on,
  2952. hsotg->op_state);
  2953. /* Don't modify pullup state while in host mode */
  2954. if (hsotg->op_state != OTG_STATE_B_PERIPHERAL) {
  2955. hsotg->enabled = is_on;
  2956. return 0;
  2957. }
  2958. spin_lock_irqsave(&hsotg->lock, flags);
  2959. if (is_on) {
  2960. hsotg->enabled = 1;
  2961. dwc2_hsotg_core_init_disconnected(hsotg, false);
  2962. dwc2_hsotg_core_connect(hsotg);
  2963. } else {
  2964. dwc2_hsotg_core_disconnect(hsotg);
  2965. dwc2_hsotg_disconnect(hsotg);
  2966. hsotg->enabled = 0;
  2967. }
  2968. hsotg->gadget.speed = USB_SPEED_UNKNOWN;
  2969. spin_unlock_irqrestore(&hsotg->lock, flags);
  2970. return 0;
  2971. }
  2972. static int dwc2_hsotg_vbus_session(struct usb_gadget *gadget, int is_active)
  2973. {
  2974. struct dwc2_hsotg *hsotg = to_hsotg(gadget);
  2975. unsigned long flags;
  2976. dev_dbg(hsotg->dev, "%s: is_active: %d\n", __func__, is_active);
  2977. spin_lock_irqsave(&hsotg->lock, flags);
  2978. /*
  2979. * If controller is hibernated, it must exit from hibernation
  2980. * before being initialized / de-initialized
  2981. */
  2982. if (hsotg->lx_state == DWC2_L2)
  2983. dwc2_exit_hibernation(hsotg, false);
  2984. if (is_active) {
  2985. hsotg->op_state = OTG_STATE_B_PERIPHERAL;
  2986. dwc2_hsotg_core_init_disconnected(hsotg, false);
  2987. if (hsotg->enabled)
  2988. dwc2_hsotg_core_connect(hsotg);
  2989. } else {
  2990. dwc2_hsotg_core_disconnect(hsotg);
  2991. dwc2_hsotg_disconnect(hsotg);
  2992. }
  2993. spin_unlock_irqrestore(&hsotg->lock, flags);
  2994. return 0;
  2995. }
  2996. /**
  2997. * dwc2_hsotg_vbus_draw - report bMaxPower field
  2998. * @gadget: The usb gadget state
  2999. * @mA: Amount of current
  3000. *
  3001. * Report how much power the device may consume to the phy.
  3002. */
  3003. static int dwc2_hsotg_vbus_draw(struct usb_gadget *gadget, unsigned mA)
  3004. {
  3005. struct dwc2_hsotg *hsotg = to_hsotg(gadget);
  3006. if (IS_ERR_OR_NULL(hsotg->uphy))
  3007. return -ENOTSUPP;
  3008. return usb_phy_set_power(hsotg->uphy, mA);
  3009. }
  3010. static const struct usb_gadget_ops dwc2_hsotg_gadget_ops = {
  3011. .get_frame = dwc2_hsotg_gadget_getframe,
  3012. .udc_start = dwc2_hsotg_udc_start,
  3013. .udc_stop = dwc2_hsotg_udc_stop,
  3014. .pullup = dwc2_hsotg_pullup,
  3015. .vbus_session = dwc2_hsotg_vbus_session,
  3016. .vbus_draw = dwc2_hsotg_vbus_draw,
  3017. };
  3018. /**
  3019. * dwc2_hsotg_initep - initialise a single endpoint
  3020. * @hsotg: The device state.
  3021. * @hs_ep: The endpoint to be initialised.
  3022. * @epnum: The endpoint number
  3023. *
  3024. * Initialise the given endpoint (as part of the probe and device state
  3025. * creation) to give to the gadget driver. Setup the endpoint name, any
  3026. * direction information and other state that may be required.
  3027. */
  3028. static void dwc2_hsotg_initep(struct dwc2_hsotg *hsotg,
  3029. struct dwc2_hsotg_ep *hs_ep,
  3030. int epnum,
  3031. bool dir_in)
  3032. {
  3033. char *dir;
  3034. if (epnum == 0)
  3035. dir = "";
  3036. else if (dir_in)
  3037. dir = "in";
  3038. else
  3039. dir = "out";
  3040. hs_ep->dir_in = dir_in;
  3041. hs_ep->index = epnum;
  3042. snprintf(hs_ep->name, sizeof(hs_ep->name), "ep%d%s", epnum, dir);
  3043. INIT_LIST_HEAD(&hs_ep->queue);
  3044. INIT_LIST_HEAD(&hs_ep->ep.ep_list);
  3045. /* add to the list of endpoints known by the gadget driver */
  3046. if (epnum)
  3047. list_add_tail(&hs_ep->ep.ep_list, &hsotg->gadget.ep_list);
  3048. hs_ep->parent = hsotg;
  3049. hs_ep->ep.name = hs_ep->name;
  3050. usb_ep_set_maxpacket_limit(&hs_ep->ep, epnum ? 1024 : EP0_MPS_LIMIT);
  3051. hs_ep->ep.ops = &dwc2_hsotg_ep_ops;
  3052. if (epnum == 0) {
  3053. hs_ep->ep.caps.type_control = true;
  3054. } else {
  3055. hs_ep->ep.caps.type_iso = true;
  3056. hs_ep->ep.caps.type_bulk = true;
  3057. hs_ep->ep.caps.type_int = true;
  3058. }
  3059. if (dir_in)
  3060. hs_ep->ep.caps.dir_in = true;
  3061. else
  3062. hs_ep->ep.caps.dir_out = true;
  3063. /*
  3064. * if we're using dma, we need to set the next-endpoint pointer
  3065. * to be something valid.
  3066. */
  3067. if (using_dma(hsotg)) {
  3068. u32 next = DXEPCTL_NEXTEP((epnum + 1) % 15);
  3069. if (dir_in)
  3070. dwc2_writel(next, hsotg->regs + DIEPCTL(epnum));
  3071. else
  3072. dwc2_writel(next, hsotg->regs + DOEPCTL(epnum));
  3073. }
  3074. }
  3075. /**
  3076. * dwc2_hsotg_hw_cfg - read HW configuration registers
  3077. * @param: The device state
  3078. *
  3079. * Read the USB core HW configuration registers
  3080. */
  3081. static int dwc2_hsotg_hw_cfg(struct dwc2_hsotg *hsotg)
  3082. {
  3083. u32 cfg;
  3084. u32 ep_type;
  3085. u32 i;
  3086. /* check hardware configuration */
  3087. hsotg->num_of_eps = hsotg->hw_params.num_dev_ep;
  3088. /* Add ep0 */
  3089. hsotg->num_of_eps++;
  3090. hsotg->eps_in[0] = devm_kzalloc(hsotg->dev, sizeof(struct dwc2_hsotg_ep),
  3091. GFP_KERNEL);
  3092. if (!hsotg->eps_in[0])
  3093. return -ENOMEM;
  3094. /* Same dwc2_hsotg_ep is used in both directions for ep0 */
  3095. hsotg->eps_out[0] = hsotg->eps_in[0];
  3096. cfg = hsotg->hw_params.dev_ep_dirs;
  3097. for (i = 1, cfg >>= 2; i < hsotg->num_of_eps; i++, cfg >>= 2) {
  3098. ep_type = cfg & 3;
  3099. /* Direction in or both */
  3100. if (!(ep_type & 2)) {
  3101. hsotg->eps_in[i] = devm_kzalloc(hsotg->dev,
  3102. sizeof(struct dwc2_hsotg_ep), GFP_KERNEL);
  3103. if (!hsotg->eps_in[i])
  3104. return -ENOMEM;
  3105. }
  3106. /* Direction out or both */
  3107. if (!(ep_type & 1)) {
  3108. hsotg->eps_out[i] = devm_kzalloc(hsotg->dev,
  3109. sizeof(struct dwc2_hsotg_ep), GFP_KERNEL);
  3110. if (!hsotg->eps_out[i])
  3111. return -ENOMEM;
  3112. }
  3113. }
  3114. hsotg->fifo_mem = hsotg->hw_params.total_fifo_size;
  3115. hsotg->dedicated_fifos = hsotg->hw_params.en_multiple_tx_fifo;
  3116. dev_info(hsotg->dev, "EPs: %d, %s fifos, %d entries in SPRAM\n",
  3117. hsotg->num_of_eps,
  3118. hsotg->dedicated_fifos ? "dedicated" : "shared",
  3119. hsotg->fifo_mem);
  3120. return 0;
  3121. }
  3122. /**
  3123. * dwc2_hsotg_dump - dump state of the udc
  3124. * @param: The device state
  3125. */
  3126. static void dwc2_hsotg_dump(struct dwc2_hsotg *hsotg)
  3127. {
  3128. #ifdef DEBUG
  3129. struct device *dev = hsotg->dev;
  3130. void __iomem *regs = hsotg->regs;
  3131. u32 val;
  3132. int idx;
  3133. dev_info(dev, "DCFG=0x%08x, DCTL=0x%08x, DIEPMSK=%08x\n",
  3134. dwc2_readl(regs + DCFG), dwc2_readl(regs + DCTL),
  3135. dwc2_readl(regs + DIEPMSK));
  3136. dev_info(dev, "GAHBCFG=0x%08x, GHWCFG1=0x%08x\n",
  3137. dwc2_readl(regs + GAHBCFG), dwc2_readl(regs + GHWCFG1));
  3138. dev_info(dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
  3139. dwc2_readl(regs + GRXFSIZ), dwc2_readl(regs + GNPTXFSIZ));
  3140. /* show periodic fifo settings */
  3141. for (idx = 1; idx < hsotg->num_of_eps; idx++) {
  3142. val = dwc2_readl(regs + DPTXFSIZN(idx));
  3143. dev_info(dev, "DPTx[%d] FSize=%d, StAddr=0x%08x\n", idx,
  3144. val >> FIFOSIZE_DEPTH_SHIFT,
  3145. val & FIFOSIZE_STARTADDR_MASK);
  3146. }
  3147. for (idx = 0; idx < hsotg->num_of_eps; idx++) {
  3148. dev_info(dev,
  3149. "ep%d-in: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n", idx,
  3150. dwc2_readl(regs + DIEPCTL(idx)),
  3151. dwc2_readl(regs + DIEPTSIZ(idx)),
  3152. dwc2_readl(regs + DIEPDMA(idx)));
  3153. val = dwc2_readl(regs + DOEPCTL(idx));
  3154. dev_info(dev,
  3155. "ep%d-out: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n",
  3156. idx, dwc2_readl(regs + DOEPCTL(idx)),
  3157. dwc2_readl(regs + DOEPTSIZ(idx)),
  3158. dwc2_readl(regs + DOEPDMA(idx)));
  3159. }
  3160. dev_info(dev, "DVBUSDIS=0x%08x, DVBUSPULSE=%08x\n",
  3161. dwc2_readl(regs + DVBUSDIS), dwc2_readl(regs + DVBUSPULSE));
  3162. #endif
  3163. }
  3164. #ifdef CONFIG_OF
  3165. static void dwc2_hsotg_of_probe(struct dwc2_hsotg *hsotg)
  3166. {
  3167. struct device_node *np = hsotg->dev->of_node;
  3168. u32 len = 0;
  3169. u32 i = 0;
  3170. /* Enable dma if requested in device tree */
  3171. hsotg->g_using_dma = of_property_read_bool(np, "g-use-dma");
  3172. /*
  3173. * Register TX periodic fifo size per endpoint.
  3174. * EP0 is excluded since it has no fifo configuration.
  3175. */
  3176. if (!of_find_property(np, "g-tx-fifo-size", &len))
  3177. goto rx_fifo;
  3178. len /= sizeof(u32);
  3179. /* Read tx fifo sizes other than ep0 */
  3180. if (of_property_read_u32_array(np, "g-tx-fifo-size",
  3181. &hsotg->g_tx_fifo_sz[1], len))
  3182. goto rx_fifo;
  3183. /* Add ep0 */
  3184. len++;
  3185. /* Make remaining TX fifos unavailable */
  3186. if (len < MAX_EPS_CHANNELS) {
  3187. for (i = len; i < MAX_EPS_CHANNELS; i++)
  3188. hsotg->g_tx_fifo_sz[i] = 0;
  3189. }
  3190. rx_fifo:
  3191. /* Register RX fifo size */
  3192. of_property_read_u32(np, "g-rx-fifo-size", &hsotg->g_rx_fifo_sz);
  3193. /* Register NPTX fifo size */
  3194. of_property_read_u32(np, "g-np-tx-fifo-size",
  3195. &hsotg->g_np_g_tx_fifo_sz);
  3196. }
  3197. #else
  3198. static inline void dwc2_hsotg_of_probe(struct dwc2_hsotg *hsotg) { }
  3199. #endif
  3200. /**
  3201. * dwc2_gadget_init - init function for gadget
  3202. * @dwc2: The data structure for the DWC2 driver.
  3203. * @irq: The IRQ number for the controller.
  3204. */
  3205. int dwc2_gadget_init(struct dwc2_hsotg *hsotg, int irq)
  3206. {
  3207. struct device *dev = hsotg->dev;
  3208. int epnum;
  3209. int ret;
  3210. int i;
  3211. u32 p_tx_fifo[] = DWC2_G_P_LEGACY_TX_FIFO_SIZE;
  3212. /* Initialize to legacy fifo configuration values */
  3213. hsotg->g_rx_fifo_sz = 2048;
  3214. hsotg->g_np_g_tx_fifo_sz = 1024;
  3215. memcpy(&hsotg->g_tx_fifo_sz[1], p_tx_fifo, sizeof(p_tx_fifo));
  3216. /* Device tree specific probe */
  3217. dwc2_hsotg_of_probe(hsotg);
  3218. /* Check against largest possible value. */
  3219. if (hsotg->g_np_g_tx_fifo_sz >
  3220. hsotg->hw_params.dev_nperio_tx_fifo_size) {
  3221. dev_warn(dev, "Specified GNPTXFDEP=%d > %d\n",
  3222. hsotg->g_np_g_tx_fifo_sz,
  3223. hsotg->hw_params.dev_nperio_tx_fifo_size);
  3224. hsotg->g_np_g_tx_fifo_sz =
  3225. hsotg->hw_params.dev_nperio_tx_fifo_size;
  3226. }
  3227. /* Dump fifo information */
  3228. dev_dbg(dev, "NonPeriodic TXFIFO size: %d\n",
  3229. hsotg->g_np_g_tx_fifo_sz);
  3230. dev_dbg(dev, "RXFIFO size: %d\n", hsotg->g_rx_fifo_sz);
  3231. for (i = 0; i < MAX_EPS_CHANNELS; i++)
  3232. dev_dbg(dev, "Periodic TXFIFO%2d size: %d\n", i,
  3233. hsotg->g_tx_fifo_sz[i]);
  3234. hsotg->gadget.max_speed = USB_SPEED_HIGH;
  3235. hsotg->gadget.ops = &dwc2_hsotg_gadget_ops;
  3236. hsotg->gadget.name = dev_name(dev);
  3237. if (hsotg->dr_mode == USB_DR_MODE_OTG)
  3238. hsotg->gadget.is_otg = 1;
  3239. else if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
  3240. hsotg->op_state = OTG_STATE_B_PERIPHERAL;
  3241. ret = dwc2_hsotg_hw_cfg(hsotg);
  3242. if (ret) {
  3243. dev_err(hsotg->dev, "Hardware configuration failed: %d\n", ret);
  3244. return ret;
  3245. }
  3246. hsotg->ctrl_buff = devm_kzalloc(hsotg->dev,
  3247. DWC2_CTRL_BUFF_SIZE, GFP_KERNEL);
  3248. if (!hsotg->ctrl_buff) {
  3249. dev_err(dev, "failed to allocate ctrl request buff\n");
  3250. return -ENOMEM;
  3251. }
  3252. hsotg->ep0_buff = devm_kzalloc(hsotg->dev,
  3253. DWC2_CTRL_BUFF_SIZE, GFP_KERNEL);
  3254. if (!hsotg->ep0_buff) {
  3255. dev_err(dev, "failed to allocate ctrl reply buff\n");
  3256. return -ENOMEM;
  3257. }
  3258. ret = devm_request_irq(hsotg->dev, irq, dwc2_hsotg_irq, IRQF_SHARED,
  3259. dev_name(hsotg->dev), hsotg);
  3260. if (ret < 0) {
  3261. dev_err(dev, "cannot claim IRQ for gadget\n");
  3262. return ret;
  3263. }
  3264. /* hsotg->num_of_eps holds number of EPs other than ep0 */
  3265. if (hsotg->num_of_eps == 0) {
  3266. dev_err(dev, "wrong number of EPs (zero)\n");
  3267. return -EINVAL;
  3268. }
  3269. /* setup endpoint information */
  3270. INIT_LIST_HEAD(&hsotg->gadget.ep_list);
  3271. hsotg->gadget.ep0 = &hsotg->eps_out[0]->ep;
  3272. /* allocate EP0 request */
  3273. hsotg->ctrl_req = dwc2_hsotg_ep_alloc_request(&hsotg->eps_out[0]->ep,
  3274. GFP_KERNEL);
  3275. if (!hsotg->ctrl_req) {
  3276. dev_err(dev, "failed to allocate ctrl req\n");
  3277. return -ENOMEM;
  3278. }
  3279. /* initialise the endpoints now the core has been initialised */
  3280. for (epnum = 0; epnum < hsotg->num_of_eps; epnum++) {
  3281. if (hsotg->eps_in[epnum])
  3282. dwc2_hsotg_initep(hsotg, hsotg->eps_in[epnum],
  3283. epnum, 1);
  3284. if (hsotg->eps_out[epnum])
  3285. dwc2_hsotg_initep(hsotg, hsotg->eps_out[epnum],
  3286. epnum, 0);
  3287. }
  3288. ret = usb_add_gadget_udc(dev, &hsotg->gadget);
  3289. if (ret)
  3290. return ret;
  3291. dwc2_hsotg_dump(hsotg);
  3292. return 0;
  3293. }
  3294. /**
  3295. * dwc2_hsotg_remove - remove function for hsotg driver
  3296. * @pdev: The platform information for the driver
  3297. */
  3298. int dwc2_hsotg_remove(struct dwc2_hsotg *hsotg)
  3299. {
  3300. usb_del_gadget_udc(&hsotg->gadget);
  3301. return 0;
  3302. }
  3303. int dwc2_hsotg_suspend(struct dwc2_hsotg *hsotg)
  3304. {
  3305. unsigned long flags;
  3306. if (hsotg->lx_state != DWC2_L0)
  3307. return 0;
  3308. if (hsotg->driver) {
  3309. int ep;
  3310. dev_info(hsotg->dev, "suspending usb gadget %s\n",
  3311. hsotg->driver->driver.name);
  3312. spin_lock_irqsave(&hsotg->lock, flags);
  3313. if (hsotg->enabled)
  3314. dwc2_hsotg_core_disconnect(hsotg);
  3315. dwc2_hsotg_disconnect(hsotg);
  3316. hsotg->gadget.speed = USB_SPEED_UNKNOWN;
  3317. spin_unlock_irqrestore(&hsotg->lock, flags);
  3318. for (ep = 0; ep < hsotg->num_of_eps; ep++) {
  3319. if (hsotg->eps_in[ep])
  3320. dwc2_hsotg_ep_disable(&hsotg->eps_in[ep]->ep);
  3321. if (hsotg->eps_out[ep])
  3322. dwc2_hsotg_ep_disable(&hsotg->eps_out[ep]->ep);
  3323. }
  3324. }
  3325. return 0;
  3326. }
  3327. int dwc2_hsotg_resume(struct dwc2_hsotg *hsotg)
  3328. {
  3329. unsigned long flags;
  3330. if (hsotg->lx_state == DWC2_L2)
  3331. return 0;
  3332. if (hsotg->driver) {
  3333. dev_info(hsotg->dev, "resuming usb gadget %s\n",
  3334. hsotg->driver->driver.name);
  3335. spin_lock_irqsave(&hsotg->lock, flags);
  3336. dwc2_hsotg_core_init_disconnected(hsotg, false);
  3337. if (hsotg->enabled)
  3338. dwc2_hsotg_core_connect(hsotg);
  3339. spin_unlock_irqrestore(&hsotg->lock, flags);
  3340. }
  3341. return 0;
  3342. }
  3343. /**
  3344. * dwc2_backup_device_registers() - Backup controller device registers.
  3345. * When suspending usb bus, registers needs to be backuped
  3346. * if controller power is disabled once suspended.
  3347. *
  3348. * @hsotg: Programming view of the DWC_otg controller
  3349. */
  3350. int dwc2_backup_device_registers(struct dwc2_hsotg *hsotg)
  3351. {
  3352. struct dwc2_dregs_backup *dr;
  3353. int i;
  3354. dev_dbg(hsotg->dev, "%s\n", __func__);
  3355. /* Backup dev regs */
  3356. dr = &hsotg->dr_backup;
  3357. dr->dcfg = dwc2_readl(hsotg->regs + DCFG);
  3358. dr->dctl = dwc2_readl(hsotg->regs + DCTL);
  3359. dr->daintmsk = dwc2_readl(hsotg->regs + DAINTMSK);
  3360. dr->diepmsk = dwc2_readl(hsotg->regs + DIEPMSK);
  3361. dr->doepmsk = dwc2_readl(hsotg->regs + DOEPMSK);
  3362. for (i = 0; i < hsotg->num_of_eps; i++) {
  3363. /* Backup IN EPs */
  3364. dr->diepctl[i] = dwc2_readl(hsotg->regs + DIEPCTL(i));
  3365. /* Ensure DATA PID is correctly configured */
  3366. if (dr->diepctl[i] & DXEPCTL_DPID)
  3367. dr->diepctl[i] |= DXEPCTL_SETD1PID;
  3368. else
  3369. dr->diepctl[i] |= DXEPCTL_SETD0PID;
  3370. dr->dieptsiz[i] = dwc2_readl(hsotg->regs + DIEPTSIZ(i));
  3371. dr->diepdma[i] = dwc2_readl(hsotg->regs + DIEPDMA(i));
  3372. /* Backup OUT EPs */
  3373. dr->doepctl[i] = dwc2_readl(hsotg->regs + DOEPCTL(i));
  3374. /* Ensure DATA PID is correctly configured */
  3375. if (dr->doepctl[i] & DXEPCTL_DPID)
  3376. dr->doepctl[i] |= DXEPCTL_SETD1PID;
  3377. else
  3378. dr->doepctl[i] |= DXEPCTL_SETD0PID;
  3379. dr->doeptsiz[i] = dwc2_readl(hsotg->regs + DOEPTSIZ(i));
  3380. dr->doepdma[i] = dwc2_readl(hsotg->regs + DOEPDMA(i));
  3381. }
  3382. dr->valid = true;
  3383. return 0;
  3384. }
  3385. /**
  3386. * dwc2_restore_device_registers() - Restore controller device registers.
  3387. * When resuming usb bus, device registers needs to be restored
  3388. * if controller power were disabled.
  3389. *
  3390. * @hsotg: Programming view of the DWC_otg controller
  3391. */
  3392. int dwc2_restore_device_registers(struct dwc2_hsotg *hsotg)
  3393. {
  3394. struct dwc2_dregs_backup *dr;
  3395. u32 dctl;
  3396. int i;
  3397. dev_dbg(hsotg->dev, "%s\n", __func__);
  3398. /* Restore dev regs */
  3399. dr = &hsotg->dr_backup;
  3400. if (!dr->valid) {
  3401. dev_err(hsotg->dev, "%s: no device registers to restore\n",
  3402. __func__);
  3403. return -EINVAL;
  3404. }
  3405. dr->valid = false;
  3406. dwc2_writel(dr->dcfg, hsotg->regs + DCFG);
  3407. dwc2_writel(dr->dctl, hsotg->regs + DCTL);
  3408. dwc2_writel(dr->daintmsk, hsotg->regs + DAINTMSK);
  3409. dwc2_writel(dr->diepmsk, hsotg->regs + DIEPMSK);
  3410. dwc2_writel(dr->doepmsk, hsotg->regs + DOEPMSK);
  3411. for (i = 0; i < hsotg->num_of_eps; i++) {
  3412. /* Restore IN EPs */
  3413. dwc2_writel(dr->diepctl[i], hsotg->regs + DIEPCTL(i));
  3414. dwc2_writel(dr->dieptsiz[i], hsotg->regs + DIEPTSIZ(i));
  3415. dwc2_writel(dr->diepdma[i], hsotg->regs + DIEPDMA(i));
  3416. /* Restore OUT EPs */
  3417. dwc2_writel(dr->doepctl[i], hsotg->regs + DOEPCTL(i));
  3418. dwc2_writel(dr->doeptsiz[i], hsotg->regs + DOEPTSIZ(i));
  3419. dwc2_writel(dr->doepdma[i], hsotg->regs + DOEPDMA(i));
  3420. }
  3421. /* Set the Power-On Programming done bit */
  3422. dctl = dwc2_readl(hsotg->regs + DCTL);
  3423. dctl |= DCTL_PWRONPRGDONE;
  3424. dwc2_writel(dctl, hsotg->regs + DCTL);
  3425. return 0;
  3426. }