gadget.c 102 KB

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