gadget.c 96 KB

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