gadget.c 90 KB

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