gadget.c 91 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701
  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/seq_file.h>
  24. #include <linux/delay.h>
  25. #include <linux/io.h>
  26. #include <linux/slab.h>
  27. #include <linux/clk.h>
  28. #include <linux/regulator/consumer.h>
  29. #include <linux/of_platform.h>
  30. #include <linux/phy/phy.h>
  31. #include <linux/usb/ch9.h>
  32. #include <linux/usb/gadget.h>
  33. #include <linux/usb/phy.h>
  34. #include <linux/platform_data/s3c-hsotg.h>
  35. #include "core.h"
  36. /* conversion functions */
  37. static inline struct s3c_hsotg_req *our_req(struct usb_request *req)
  38. {
  39. return container_of(req, struct s3c_hsotg_req, req);
  40. }
  41. static inline struct s3c_hsotg_ep *our_ep(struct usb_ep *ep)
  42. {
  43. return container_of(ep, struct s3c_hsotg_ep, ep);
  44. }
  45. static inline struct s3c_hsotg *to_hsotg(struct usb_gadget *gadget)
  46. {
  47. return container_of(gadget, struct s3c_hsotg, gadget);
  48. }
  49. static inline void __orr32(void __iomem *ptr, u32 val)
  50. {
  51. writel(readl(ptr) | val, ptr);
  52. }
  53. static inline void __bic32(void __iomem *ptr, u32 val)
  54. {
  55. writel(readl(ptr) & ~val, ptr);
  56. }
  57. /* forward decleration of functions */
  58. static void s3c_hsotg_dump(struct s3c_hsotg *hsotg);
  59. /**
  60. * using_dma - return the DMA status of the driver.
  61. * @hsotg: The driver state.
  62. *
  63. * Return true if we're using DMA.
  64. *
  65. * Currently, we have the DMA support code worked into everywhere
  66. * that needs it, but the AMBA DMA implementation in the hardware can
  67. * only DMA from 32bit aligned addresses. This means that gadgets such
  68. * as the CDC Ethernet cannot work as they often pass packets which are
  69. * not 32bit aligned.
  70. *
  71. * Unfortunately the choice to use DMA or not is global to the controller
  72. * and seems to be only settable when the controller is being put through
  73. * a core reset. This means we either need to fix the gadgets to take
  74. * account of DMA alignment, or add bounce buffers (yuerk).
  75. *
  76. * Until this issue is sorted out, we always return 'false'.
  77. */
  78. static inline bool using_dma(struct s3c_hsotg *hsotg)
  79. {
  80. return false; /* support is not complete */
  81. }
  82. /**
  83. * s3c_hsotg_en_gsint - enable one or more of the general interrupt
  84. * @hsotg: The device state
  85. * @ints: A bitmask of the interrupts to enable
  86. */
  87. static void s3c_hsotg_en_gsint(struct s3c_hsotg *hsotg, u32 ints)
  88. {
  89. u32 gsintmsk = readl(hsotg->regs + GINTMSK);
  90. u32 new_gsintmsk;
  91. new_gsintmsk = gsintmsk | ints;
  92. if (new_gsintmsk != gsintmsk) {
  93. dev_dbg(hsotg->dev, "gsintmsk now 0x%08x\n", new_gsintmsk);
  94. writel(new_gsintmsk, hsotg->regs + GINTMSK);
  95. }
  96. }
  97. /**
  98. * s3c_hsotg_disable_gsint - disable one or more of the general interrupt
  99. * @hsotg: The device state
  100. * @ints: A bitmask of the interrupts to enable
  101. */
  102. static void s3c_hsotg_disable_gsint(struct s3c_hsotg *hsotg, u32 ints)
  103. {
  104. u32 gsintmsk = readl(hsotg->regs + GINTMSK);
  105. u32 new_gsintmsk;
  106. new_gsintmsk = gsintmsk & ~ints;
  107. if (new_gsintmsk != gsintmsk)
  108. writel(new_gsintmsk, hsotg->regs + GINTMSK);
  109. }
  110. /**
  111. * s3c_hsotg_ctrl_epint - enable/disable an endpoint irq
  112. * @hsotg: The device state
  113. * @ep: The endpoint index
  114. * @dir_in: True if direction is in.
  115. * @en: The enable value, true to enable
  116. *
  117. * Set or clear the mask for an individual endpoint's interrupt
  118. * request.
  119. */
  120. static void s3c_hsotg_ctrl_epint(struct s3c_hsotg *hsotg,
  121. unsigned int ep, unsigned int dir_in,
  122. unsigned int en)
  123. {
  124. unsigned long flags;
  125. u32 bit = 1 << ep;
  126. u32 daint;
  127. if (!dir_in)
  128. bit <<= 16;
  129. local_irq_save(flags);
  130. daint = readl(hsotg->regs + DAINTMSK);
  131. if (en)
  132. daint |= bit;
  133. else
  134. daint &= ~bit;
  135. writel(daint, hsotg->regs + DAINTMSK);
  136. local_irq_restore(flags);
  137. }
  138. /**
  139. * s3c_hsotg_init_fifo - initialise non-periodic FIFOs
  140. * @hsotg: The device instance.
  141. */
  142. static void s3c_hsotg_init_fifo(struct s3c_hsotg *hsotg)
  143. {
  144. unsigned int ep;
  145. unsigned int addr;
  146. unsigned int size;
  147. int timeout;
  148. u32 val;
  149. /* set FIFO sizes to 2048/1024 */
  150. writel(2048, hsotg->regs + GRXFSIZ);
  151. writel((2048 << FIFOSIZE_STARTADDR_SHIFT) |
  152. (1024 << FIFOSIZE_DEPTH_SHIFT), hsotg->regs + GNPTXFSIZ);
  153. /*
  154. * arange all the rest of the TX FIFOs, as some versions of this
  155. * block have overlapping default addresses. This also ensures
  156. * that if the settings have been changed, then they are set to
  157. * known values.
  158. */
  159. /* start at the end of the GNPTXFSIZ, rounded up */
  160. addr = 2048 + 1024;
  161. /*
  162. * Because we have not enough memory to have each TX FIFO of size at
  163. * least 3072 bytes (the maximum single packet size), we create four
  164. * FIFOs of lenght 1024, and four of length 3072 bytes, and assing
  165. * them to endpoints dynamically according to maxpacket size value of
  166. * given endpoint.
  167. */
  168. /* 256*4=1024 bytes FIFO length */
  169. size = 256;
  170. for (ep = 1; ep <= 4; ep++) {
  171. val = addr;
  172. val |= size << FIFOSIZE_DEPTH_SHIFT;
  173. WARN_ONCE(addr + size > hsotg->fifo_mem,
  174. "insufficient fifo memory");
  175. addr += size;
  176. writel(val, hsotg->regs + DPTXFSIZN(ep));
  177. }
  178. /* 768*4=3072 bytes FIFO length */
  179. size = 768;
  180. for (ep = 5; ep <= 8; ep++) {
  181. val = addr;
  182. val |= size << FIFOSIZE_DEPTH_SHIFT;
  183. WARN_ONCE(addr + size > hsotg->fifo_mem,
  184. "insufficient fifo memory");
  185. addr += size;
  186. writel(val, hsotg->regs + DPTXFSIZN(ep));
  187. }
  188. /*
  189. * according to p428 of the design guide, we need to ensure that
  190. * all fifos are flushed before continuing
  191. */
  192. writel(GRSTCTL_TXFNUM(0x10) | GRSTCTL_TXFFLSH |
  193. GRSTCTL_RXFFLSH, hsotg->regs + GRSTCTL);
  194. /* wait until the fifos are both flushed */
  195. timeout = 100;
  196. while (1) {
  197. val = readl(hsotg->regs + GRSTCTL);
  198. if ((val & (GRSTCTL_TXFFLSH | GRSTCTL_RXFFLSH)) == 0)
  199. break;
  200. if (--timeout == 0) {
  201. dev_err(hsotg->dev,
  202. "%s: timeout flushing fifos (GRSTCTL=%08x)\n",
  203. __func__, val);
  204. }
  205. udelay(1);
  206. }
  207. dev_dbg(hsotg->dev, "FIFOs reset, timeout at %d\n", timeout);
  208. }
  209. /**
  210. * @ep: USB endpoint to allocate request for.
  211. * @flags: Allocation flags
  212. *
  213. * Allocate a new USB request structure appropriate for the specified endpoint
  214. */
  215. static struct usb_request *s3c_hsotg_ep_alloc_request(struct usb_ep *ep,
  216. gfp_t flags)
  217. {
  218. struct s3c_hsotg_req *req;
  219. req = kzalloc(sizeof(struct s3c_hsotg_req), flags);
  220. if (!req)
  221. return NULL;
  222. INIT_LIST_HEAD(&req->queue);
  223. return &req->req;
  224. }
  225. /**
  226. * is_ep_periodic - return true if the endpoint is in periodic mode.
  227. * @hs_ep: The endpoint to query.
  228. *
  229. * Returns true if the endpoint is in periodic mode, meaning it is being
  230. * used for an Interrupt or ISO transfer.
  231. */
  232. static inline int is_ep_periodic(struct s3c_hsotg_ep *hs_ep)
  233. {
  234. return hs_ep->periodic;
  235. }
  236. /**
  237. * s3c_hsotg_unmap_dma - unmap the DMA memory being used for the request
  238. * @hsotg: The device state.
  239. * @hs_ep: The endpoint for the request
  240. * @hs_req: The request being processed.
  241. *
  242. * This is the reverse of s3c_hsotg_map_dma(), called for the completion
  243. * of a request to ensure the buffer is ready for access by the caller.
  244. */
  245. static void s3c_hsotg_unmap_dma(struct s3c_hsotg *hsotg,
  246. struct s3c_hsotg_ep *hs_ep,
  247. struct s3c_hsotg_req *hs_req)
  248. {
  249. struct usb_request *req = &hs_req->req;
  250. /* ignore this if we're not moving any data */
  251. if (hs_req->req.length == 0)
  252. return;
  253. usb_gadget_unmap_request(&hsotg->gadget, req, hs_ep->dir_in);
  254. }
  255. /**
  256. * s3c_hsotg_write_fifo - write packet Data to the TxFIFO
  257. * @hsotg: The controller state.
  258. * @hs_ep: The endpoint we're going to write for.
  259. * @hs_req: The request to write data for.
  260. *
  261. * This is called when the TxFIFO has some space in it to hold a new
  262. * transmission and we have something to give it. The actual setup of
  263. * the data size is done elsewhere, so all we have to do is to actually
  264. * write the data.
  265. *
  266. * The return value is zero if there is more space (or nothing was done)
  267. * otherwise -ENOSPC is returned if the FIFO space was used up.
  268. *
  269. * This routine is only needed for PIO
  270. */
  271. static int s3c_hsotg_write_fifo(struct s3c_hsotg *hsotg,
  272. struct s3c_hsotg_ep *hs_ep,
  273. struct s3c_hsotg_req *hs_req)
  274. {
  275. bool periodic = is_ep_periodic(hs_ep);
  276. u32 gnptxsts = readl(hsotg->regs + GNPTXSTS);
  277. int buf_pos = hs_req->req.actual;
  278. int to_write = hs_ep->size_loaded;
  279. void *data;
  280. int can_write;
  281. int pkt_round;
  282. int max_transfer;
  283. to_write -= (buf_pos - hs_ep->last_load);
  284. /* if there's nothing to write, get out early */
  285. if (to_write == 0)
  286. return 0;
  287. if (periodic && !hsotg->dedicated_fifos) {
  288. u32 epsize = readl(hsotg->regs + DIEPTSIZ(hs_ep->index));
  289. int size_left;
  290. int size_done;
  291. /*
  292. * work out how much data was loaded so we can calculate
  293. * how much data is left in the fifo.
  294. */
  295. size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
  296. /*
  297. * if shared fifo, we cannot write anything until the
  298. * previous data has been completely sent.
  299. */
  300. if (hs_ep->fifo_load != 0) {
  301. s3c_hsotg_en_gsint(hsotg, GINTSTS_PTXFEMP);
  302. return -ENOSPC;
  303. }
  304. dev_dbg(hsotg->dev, "%s: left=%d, load=%d, fifo=%d, size %d\n",
  305. __func__, size_left,
  306. hs_ep->size_loaded, hs_ep->fifo_load, hs_ep->fifo_size);
  307. /* how much of the data has moved */
  308. size_done = hs_ep->size_loaded - size_left;
  309. /* how much data is left in the fifo */
  310. can_write = hs_ep->fifo_load - size_done;
  311. dev_dbg(hsotg->dev, "%s: => can_write1=%d\n",
  312. __func__, can_write);
  313. can_write = hs_ep->fifo_size - can_write;
  314. dev_dbg(hsotg->dev, "%s: => can_write2=%d\n",
  315. __func__, can_write);
  316. if (can_write <= 0) {
  317. s3c_hsotg_en_gsint(hsotg, GINTSTS_PTXFEMP);
  318. return -ENOSPC;
  319. }
  320. } else if (hsotg->dedicated_fifos && hs_ep->index != 0) {
  321. can_write = readl(hsotg->regs + DTXFSTS(hs_ep->index));
  322. can_write &= 0xffff;
  323. can_write *= 4;
  324. } else {
  325. if (GNPTXSTS_NP_TXQ_SPC_AVAIL_GET(gnptxsts) == 0) {
  326. dev_dbg(hsotg->dev,
  327. "%s: no queue slots available (0x%08x)\n",
  328. __func__, gnptxsts);
  329. s3c_hsotg_en_gsint(hsotg, GINTSTS_NPTXFEMP);
  330. return -ENOSPC;
  331. }
  332. can_write = GNPTXSTS_NP_TXF_SPC_AVAIL_GET(gnptxsts);
  333. can_write *= 4; /* fifo size is in 32bit quantities. */
  334. }
  335. max_transfer = hs_ep->ep.maxpacket * hs_ep->mc;
  336. dev_dbg(hsotg->dev, "%s: GNPTXSTS=%08x, can=%d, to=%d, max_transfer %d\n",
  337. __func__, gnptxsts, can_write, to_write, max_transfer);
  338. /*
  339. * limit to 512 bytes of data, it seems at least on the non-periodic
  340. * FIFO, requests of >512 cause the endpoint to get stuck with a
  341. * fragment of the end of the transfer in it.
  342. */
  343. if (can_write > 512 && !periodic)
  344. can_write = 512;
  345. /*
  346. * limit the write to one max-packet size worth of data, but allow
  347. * the transfer to return that it did not run out of fifo space
  348. * doing it.
  349. */
  350. if (to_write > max_transfer) {
  351. to_write = max_transfer;
  352. /* it's needed only when we do not use dedicated fifos */
  353. if (!hsotg->dedicated_fifos)
  354. s3c_hsotg_en_gsint(hsotg,
  355. periodic ? GINTSTS_PTXFEMP :
  356. GINTSTS_NPTXFEMP);
  357. }
  358. /* see if we can write data */
  359. if (to_write > can_write) {
  360. to_write = can_write;
  361. pkt_round = to_write % max_transfer;
  362. /*
  363. * Round the write down to an
  364. * exact number of packets.
  365. *
  366. * Note, we do not currently check to see if we can ever
  367. * write a full packet or not to the FIFO.
  368. */
  369. if (pkt_round)
  370. to_write -= pkt_round;
  371. /*
  372. * enable correct FIFO interrupt to alert us when there
  373. * is more room left.
  374. */
  375. /* it's needed only when we do not use dedicated fifos */
  376. if (!hsotg->dedicated_fifos)
  377. s3c_hsotg_en_gsint(hsotg,
  378. periodic ? GINTSTS_PTXFEMP :
  379. GINTSTS_NPTXFEMP);
  380. }
  381. dev_dbg(hsotg->dev, "write %d/%d, can_write %d, done %d\n",
  382. to_write, hs_req->req.length, can_write, buf_pos);
  383. if (to_write <= 0)
  384. return -ENOSPC;
  385. hs_req->req.actual = buf_pos + to_write;
  386. hs_ep->total_data += to_write;
  387. if (periodic)
  388. hs_ep->fifo_load += to_write;
  389. to_write = DIV_ROUND_UP(to_write, 4);
  390. data = hs_req->req.buf + buf_pos;
  391. iowrite32_rep(hsotg->regs + EPFIFO(hs_ep->index), data, to_write);
  392. return (to_write >= can_write) ? -ENOSPC : 0;
  393. }
  394. /**
  395. * get_ep_limit - get the maximum data legnth for this endpoint
  396. * @hs_ep: The endpoint
  397. *
  398. * Return the maximum data that can be queued in one go on a given endpoint
  399. * so that transfers that are too long can be split.
  400. */
  401. static unsigned get_ep_limit(struct s3c_hsotg_ep *hs_ep)
  402. {
  403. int index = hs_ep->index;
  404. unsigned maxsize;
  405. unsigned maxpkt;
  406. if (index != 0) {
  407. maxsize = DXEPTSIZ_XFERSIZE_LIMIT + 1;
  408. maxpkt = DXEPTSIZ_PKTCNT_LIMIT + 1;
  409. } else {
  410. maxsize = 64+64;
  411. if (hs_ep->dir_in)
  412. maxpkt = DIEPTSIZ0_PKTCNT_LIMIT + 1;
  413. else
  414. maxpkt = 2;
  415. }
  416. /* we made the constant loading easier above by using +1 */
  417. maxpkt--;
  418. maxsize--;
  419. /*
  420. * constrain by packet count if maxpkts*pktsize is greater
  421. * than the length register size.
  422. */
  423. if ((maxpkt * hs_ep->ep.maxpacket) < maxsize)
  424. maxsize = maxpkt * hs_ep->ep.maxpacket;
  425. return maxsize;
  426. }
  427. /**
  428. * s3c_hsotg_start_req - start a USB request from an endpoint's queue
  429. * @hsotg: The controller state.
  430. * @hs_ep: The endpoint to process a request for
  431. * @hs_req: The request to start.
  432. * @continuing: True if we are doing more for the current request.
  433. *
  434. * Start the given request running by setting the endpoint registers
  435. * appropriately, and writing any data to the FIFOs.
  436. */
  437. static void s3c_hsotg_start_req(struct s3c_hsotg *hsotg,
  438. struct s3c_hsotg_ep *hs_ep,
  439. struct s3c_hsotg_req *hs_req,
  440. bool continuing)
  441. {
  442. struct usb_request *ureq = &hs_req->req;
  443. int index = hs_ep->index;
  444. int dir_in = hs_ep->dir_in;
  445. u32 epctrl_reg;
  446. u32 epsize_reg;
  447. u32 epsize;
  448. u32 ctrl;
  449. unsigned length;
  450. unsigned packets;
  451. unsigned maxreq;
  452. if (index != 0) {
  453. if (hs_ep->req && !continuing) {
  454. dev_err(hsotg->dev, "%s: active request\n", __func__);
  455. WARN_ON(1);
  456. return;
  457. } else if (hs_ep->req != hs_req && continuing) {
  458. dev_err(hsotg->dev,
  459. "%s: continue different req\n", __func__);
  460. WARN_ON(1);
  461. return;
  462. }
  463. }
  464. epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
  465. epsize_reg = dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
  466. dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x, ep %d, dir %s\n",
  467. __func__, readl(hsotg->regs + epctrl_reg), index,
  468. hs_ep->dir_in ? "in" : "out");
  469. /* If endpoint is stalled, we will restart request later */
  470. ctrl = readl(hsotg->regs + epctrl_reg);
  471. if (ctrl & DXEPCTL_STALL) {
  472. dev_warn(hsotg->dev, "%s: ep%d is stalled\n", __func__, index);
  473. return;
  474. }
  475. length = ureq->length - ureq->actual;
  476. dev_dbg(hsotg->dev, "ureq->length:%d ureq->actual:%d\n",
  477. ureq->length, ureq->actual);
  478. if (0)
  479. dev_dbg(hsotg->dev,
  480. "REQ buf %p len %d dma %pad noi=%d zp=%d snok=%d\n",
  481. ureq->buf, length, &ureq->dma,
  482. ureq->no_interrupt, ureq->zero, ureq->short_not_ok);
  483. maxreq = get_ep_limit(hs_ep);
  484. if (length > maxreq) {
  485. int round = maxreq % hs_ep->ep.maxpacket;
  486. dev_dbg(hsotg->dev, "%s: length %d, max-req %d, r %d\n",
  487. __func__, length, maxreq, round);
  488. /* round down to multiple of packets */
  489. if (round)
  490. maxreq -= round;
  491. length = maxreq;
  492. }
  493. if (length)
  494. packets = DIV_ROUND_UP(length, hs_ep->ep.maxpacket);
  495. else
  496. packets = 1; /* send one packet if length is zero. */
  497. if (hs_ep->isochronous && length > (hs_ep->mc * hs_ep->ep.maxpacket)) {
  498. dev_err(hsotg->dev, "req length > maxpacket*mc\n");
  499. return;
  500. }
  501. if (dir_in && index != 0)
  502. if (hs_ep->isochronous)
  503. epsize = DXEPTSIZ_MC(packets);
  504. else
  505. epsize = DXEPTSIZ_MC(1);
  506. else
  507. epsize = 0;
  508. if (index != 0 && ureq->zero) {
  509. /*
  510. * test for the packets being exactly right for the
  511. * transfer
  512. */
  513. if (length == (packets * hs_ep->ep.maxpacket))
  514. packets++;
  515. }
  516. epsize |= DXEPTSIZ_PKTCNT(packets);
  517. epsize |= DXEPTSIZ_XFERSIZE(length);
  518. dev_dbg(hsotg->dev, "%s: %d@%d/%d, 0x%08x => 0x%08x\n",
  519. __func__, packets, length, ureq->length, epsize, epsize_reg);
  520. /* store the request as the current one we're doing */
  521. hs_ep->req = hs_req;
  522. /* write size / packets */
  523. writel(epsize, hsotg->regs + epsize_reg);
  524. if (using_dma(hsotg) && !continuing) {
  525. unsigned int dma_reg;
  526. /*
  527. * write DMA address to control register, buffer already
  528. * synced by s3c_hsotg_ep_queue().
  529. */
  530. dma_reg = dir_in ? DIEPDMA(index) : DOEPDMA(index);
  531. writel(ureq->dma, hsotg->regs + dma_reg);
  532. dev_dbg(hsotg->dev, "%s: %pad => 0x%08x\n",
  533. __func__, &ureq->dma, dma_reg);
  534. }
  535. ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */
  536. ctrl |= DXEPCTL_USBACTEP;
  537. dev_dbg(hsotg->dev, "setup req:%d\n", hsotg->setup);
  538. /* For Setup request do not clear NAK */
  539. if (hsotg->setup && index == 0)
  540. hsotg->setup = 0;
  541. else
  542. ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */
  543. dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
  544. writel(ctrl, hsotg->regs + epctrl_reg);
  545. /*
  546. * set these, it seems that DMA support increments past the end
  547. * of the packet buffer so we need to calculate the length from
  548. * this information.
  549. */
  550. hs_ep->size_loaded = length;
  551. hs_ep->last_load = ureq->actual;
  552. if (dir_in && !using_dma(hsotg)) {
  553. /* set these anyway, we may need them for non-periodic in */
  554. hs_ep->fifo_load = 0;
  555. s3c_hsotg_write_fifo(hsotg, hs_ep, hs_req);
  556. }
  557. /*
  558. * clear the INTknTXFEmpMsk when we start request, more as a aide
  559. * to debugging to see what is going on.
  560. */
  561. if (dir_in)
  562. writel(DIEPMSK_INTKNTXFEMPMSK,
  563. hsotg->regs + DIEPINT(index));
  564. /*
  565. * Note, trying to clear the NAK here causes problems with transmit
  566. * on the S3C6400 ending up with the TXFIFO becoming full.
  567. */
  568. /* check ep is enabled */
  569. if (!(readl(hsotg->regs + epctrl_reg) & DXEPCTL_EPENA))
  570. dev_warn(hsotg->dev,
  571. "ep%d: failed to become enabled (DXEPCTL=0x%08x)?\n",
  572. index, readl(hsotg->regs + epctrl_reg));
  573. dev_dbg(hsotg->dev, "%s: DXEPCTL=0x%08x\n",
  574. __func__, readl(hsotg->regs + epctrl_reg));
  575. /* enable ep interrupts */
  576. s3c_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 1);
  577. }
  578. /**
  579. * s3c_hsotg_map_dma - map the DMA memory being used for the request
  580. * @hsotg: The device state.
  581. * @hs_ep: The endpoint the request is on.
  582. * @req: The request being processed.
  583. *
  584. * We've been asked to queue a request, so ensure that the memory buffer
  585. * is correctly setup for DMA. If we've been passed an extant DMA address
  586. * then ensure the buffer has been synced to memory. If our buffer has no
  587. * DMA memory, then we map the memory and mark our request to allow us to
  588. * cleanup on completion.
  589. */
  590. static int s3c_hsotg_map_dma(struct s3c_hsotg *hsotg,
  591. struct s3c_hsotg_ep *hs_ep,
  592. struct usb_request *req)
  593. {
  594. struct s3c_hsotg_req *hs_req = our_req(req);
  595. int ret;
  596. /* if the length is zero, ignore the DMA data */
  597. if (hs_req->req.length == 0)
  598. return 0;
  599. ret = usb_gadget_map_request(&hsotg->gadget, req, hs_ep->dir_in);
  600. if (ret)
  601. goto dma_error;
  602. return 0;
  603. dma_error:
  604. dev_err(hsotg->dev, "%s: failed to map buffer %p, %d bytes\n",
  605. __func__, req->buf, req->length);
  606. return -EIO;
  607. }
  608. static int s3c_hsotg_ep_queue(struct usb_ep *ep, struct usb_request *req,
  609. gfp_t gfp_flags)
  610. {
  611. struct s3c_hsotg_req *hs_req = our_req(req);
  612. struct s3c_hsotg_ep *hs_ep = our_ep(ep);
  613. struct s3c_hsotg *hs = hs_ep->parent;
  614. bool first;
  615. dev_dbg(hs->dev, "%s: req %p: %d@%p, noi=%d, zero=%d, snok=%d\n",
  616. ep->name, req, req->length, req->buf, req->no_interrupt,
  617. req->zero, req->short_not_ok);
  618. /* initialise status of the request */
  619. INIT_LIST_HEAD(&hs_req->queue);
  620. req->actual = 0;
  621. req->status = -EINPROGRESS;
  622. /* if we're using DMA, sync the buffers as necessary */
  623. if (using_dma(hs)) {
  624. int ret = s3c_hsotg_map_dma(hs, hs_ep, req);
  625. if (ret)
  626. return ret;
  627. }
  628. first = list_empty(&hs_ep->queue);
  629. list_add_tail(&hs_req->queue, &hs_ep->queue);
  630. if (first)
  631. s3c_hsotg_start_req(hs, hs_ep, hs_req, false);
  632. return 0;
  633. }
  634. static int s3c_hsotg_ep_queue_lock(struct usb_ep *ep, struct usb_request *req,
  635. gfp_t gfp_flags)
  636. {
  637. struct s3c_hsotg_ep *hs_ep = our_ep(ep);
  638. struct s3c_hsotg *hs = hs_ep->parent;
  639. unsigned long flags = 0;
  640. int ret = 0;
  641. spin_lock_irqsave(&hs->lock, flags);
  642. ret = s3c_hsotg_ep_queue(ep, req, gfp_flags);
  643. spin_unlock_irqrestore(&hs->lock, flags);
  644. return ret;
  645. }
  646. static void s3c_hsotg_ep_free_request(struct usb_ep *ep,
  647. struct usb_request *req)
  648. {
  649. struct s3c_hsotg_req *hs_req = our_req(req);
  650. kfree(hs_req);
  651. }
  652. /**
  653. * s3c_hsotg_complete_oursetup - setup completion callback
  654. * @ep: The endpoint the request was on.
  655. * @req: The request completed.
  656. *
  657. * Called on completion of any requests the driver itself
  658. * submitted that need cleaning up.
  659. */
  660. static void s3c_hsotg_complete_oursetup(struct usb_ep *ep,
  661. struct usb_request *req)
  662. {
  663. struct s3c_hsotg_ep *hs_ep = our_ep(ep);
  664. struct s3c_hsotg *hsotg = hs_ep->parent;
  665. dev_dbg(hsotg->dev, "%s: ep %p, req %p\n", __func__, ep, req);
  666. s3c_hsotg_ep_free_request(ep, req);
  667. }
  668. /**
  669. * ep_from_windex - convert control wIndex value to endpoint
  670. * @hsotg: The driver state.
  671. * @windex: The control request wIndex field (in host order).
  672. *
  673. * Convert the given wIndex into a pointer to an driver endpoint
  674. * structure, or return NULL if it is not a valid endpoint.
  675. */
  676. static struct s3c_hsotg_ep *ep_from_windex(struct s3c_hsotg *hsotg,
  677. u32 windex)
  678. {
  679. struct s3c_hsotg_ep *ep = &hsotg->eps[windex & 0x7F];
  680. int dir = (windex & USB_DIR_IN) ? 1 : 0;
  681. int idx = windex & 0x7F;
  682. if (windex >= 0x100)
  683. return NULL;
  684. if (idx > hsotg->num_of_eps)
  685. return NULL;
  686. if (idx && ep->dir_in != dir)
  687. return NULL;
  688. return ep;
  689. }
  690. /**
  691. * s3c_hsotg_send_reply - send reply to control request
  692. * @hsotg: The device state
  693. * @ep: Endpoint 0
  694. * @buff: Buffer for request
  695. * @length: Length of reply.
  696. *
  697. * Create a request and queue it on the given endpoint. This is useful as
  698. * an internal method of sending replies to certain control requests, etc.
  699. */
  700. static int s3c_hsotg_send_reply(struct s3c_hsotg *hsotg,
  701. struct s3c_hsotg_ep *ep,
  702. void *buff,
  703. int length)
  704. {
  705. struct usb_request *req;
  706. int ret;
  707. dev_dbg(hsotg->dev, "%s: buff %p, len %d\n", __func__, buff, length);
  708. req = s3c_hsotg_ep_alloc_request(&ep->ep, GFP_ATOMIC);
  709. hsotg->ep0_reply = req;
  710. if (!req) {
  711. dev_warn(hsotg->dev, "%s: cannot alloc req\n", __func__);
  712. return -ENOMEM;
  713. }
  714. req->buf = hsotg->ep0_buff;
  715. req->length = length;
  716. req->zero = 1; /* always do zero-length final transfer */
  717. req->complete = s3c_hsotg_complete_oursetup;
  718. if (length)
  719. memcpy(req->buf, buff, length);
  720. else
  721. ep->sent_zlp = 1;
  722. ret = s3c_hsotg_ep_queue(&ep->ep, req, GFP_ATOMIC);
  723. if (ret) {
  724. dev_warn(hsotg->dev, "%s: cannot queue req\n", __func__);
  725. return ret;
  726. }
  727. return 0;
  728. }
  729. /**
  730. * s3c_hsotg_process_req_status - process request GET_STATUS
  731. * @hsotg: The device state
  732. * @ctrl: USB control request
  733. */
  734. static int s3c_hsotg_process_req_status(struct s3c_hsotg *hsotg,
  735. struct usb_ctrlrequest *ctrl)
  736. {
  737. struct s3c_hsotg_ep *ep0 = &hsotg->eps[0];
  738. struct s3c_hsotg_ep *ep;
  739. __le16 reply;
  740. int ret;
  741. dev_dbg(hsotg->dev, "%s: USB_REQ_GET_STATUS\n", __func__);
  742. if (!ep0->dir_in) {
  743. dev_warn(hsotg->dev, "%s: direction out?\n", __func__);
  744. return -EINVAL;
  745. }
  746. switch (ctrl->bRequestType & USB_RECIP_MASK) {
  747. case USB_RECIP_DEVICE:
  748. reply = cpu_to_le16(0); /* bit 0 => self powered,
  749. * bit 1 => remote wakeup */
  750. break;
  751. case USB_RECIP_INTERFACE:
  752. /* currently, the data result should be zero */
  753. reply = cpu_to_le16(0);
  754. break;
  755. case USB_RECIP_ENDPOINT:
  756. ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex));
  757. if (!ep)
  758. return -ENOENT;
  759. reply = cpu_to_le16(ep->halted ? 1 : 0);
  760. break;
  761. default:
  762. return 0;
  763. }
  764. if (le16_to_cpu(ctrl->wLength) != 2)
  765. return -EINVAL;
  766. ret = s3c_hsotg_send_reply(hsotg, ep0, &reply, 2);
  767. if (ret) {
  768. dev_err(hsotg->dev, "%s: failed to send reply\n", __func__);
  769. return ret;
  770. }
  771. return 1;
  772. }
  773. static int s3c_hsotg_ep_sethalt(struct usb_ep *ep, int value);
  774. /**
  775. * get_ep_head - return the first request on the endpoint
  776. * @hs_ep: The controller endpoint to get
  777. *
  778. * Get the first request on the endpoint.
  779. */
  780. static struct s3c_hsotg_req *get_ep_head(struct s3c_hsotg_ep *hs_ep)
  781. {
  782. if (list_empty(&hs_ep->queue))
  783. return NULL;
  784. return list_first_entry(&hs_ep->queue, struct s3c_hsotg_req, queue);
  785. }
  786. /**
  787. * s3c_hsotg_process_req_featire - process request {SET,CLEAR}_FEATURE
  788. * @hsotg: The device state
  789. * @ctrl: USB control request
  790. */
  791. static int s3c_hsotg_process_req_feature(struct s3c_hsotg *hsotg,
  792. struct usb_ctrlrequest *ctrl)
  793. {
  794. struct s3c_hsotg_ep *ep0 = &hsotg->eps[0];
  795. struct s3c_hsotg_req *hs_req;
  796. bool restart;
  797. bool set = (ctrl->bRequest == USB_REQ_SET_FEATURE);
  798. struct s3c_hsotg_ep *ep;
  799. int ret;
  800. bool halted;
  801. dev_dbg(hsotg->dev, "%s: %s_FEATURE\n",
  802. __func__, set ? "SET" : "CLEAR");
  803. if (ctrl->bRequestType == USB_RECIP_ENDPOINT) {
  804. ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex));
  805. if (!ep) {
  806. dev_dbg(hsotg->dev, "%s: no endpoint for 0x%04x\n",
  807. __func__, le16_to_cpu(ctrl->wIndex));
  808. return -ENOENT;
  809. }
  810. switch (le16_to_cpu(ctrl->wValue)) {
  811. case USB_ENDPOINT_HALT:
  812. halted = ep->halted;
  813. s3c_hsotg_ep_sethalt(&ep->ep, set);
  814. ret = s3c_hsotg_send_reply(hsotg, ep0, NULL, 0);
  815. if (ret) {
  816. dev_err(hsotg->dev,
  817. "%s: failed to send reply\n", __func__);
  818. return ret;
  819. }
  820. /*
  821. * we have to complete all requests for ep if it was
  822. * halted, and the halt was cleared by CLEAR_FEATURE
  823. */
  824. if (!set && halted) {
  825. /*
  826. * If we have request in progress,
  827. * then complete it
  828. */
  829. if (ep->req) {
  830. hs_req = ep->req;
  831. ep->req = NULL;
  832. list_del_init(&hs_req->queue);
  833. usb_gadget_giveback_request(&ep->ep,
  834. &hs_req->req);
  835. }
  836. /* If we have pending request, then start it */
  837. restart = !list_empty(&ep->queue);
  838. if (restart) {
  839. hs_req = get_ep_head(ep);
  840. s3c_hsotg_start_req(hsotg, ep,
  841. hs_req, false);
  842. }
  843. }
  844. break;
  845. default:
  846. return -ENOENT;
  847. }
  848. } else
  849. return -ENOENT; /* currently only deal with endpoint */
  850. return 1;
  851. }
  852. static void s3c_hsotg_enqueue_setup(struct s3c_hsotg *hsotg);
  853. static void s3c_hsotg_disconnect(struct s3c_hsotg *hsotg);
  854. /**
  855. * s3c_hsotg_stall_ep0 - stall ep0
  856. * @hsotg: The device state
  857. *
  858. * Set stall for ep0 as response for setup request.
  859. */
  860. static void s3c_hsotg_stall_ep0(struct s3c_hsotg *hsotg)
  861. {
  862. struct s3c_hsotg_ep *ep0 = &hsotg->eps[0];
  863. u32 reg;
  864. u32 ctrl;
  865. dev_dbg(hsotg->dev, "ep0 stall (dir=%d)\n", ep0->dir_in);
  866. reg = (ep0->dir_in) ? DIEPCTL0 : DOEPCTL0;
  867. /*
  868. * DxEPCTL_Stall will be cleared by EP once it has
  869. * taken effect, so no need to clear later.
  870. */
  871. ctrl = readl(hsotg->regs + reg);
  872. ctrl |= DXEPCTL_STALL;
  873. ctrl |= DXEPCTL_CNAK;
  874. writel(ctrl, hsotg->regs + reg);
  875. dev_dbg(hsotg->dev,
  876. "written DXEPCTL=0x%08x to %08x (DXEPCTL=0x%08x)\n",
  877. ctrl, reg, readl(hsotg->regs + reg));
  878. /*
  879. * complete won't be called, so we enqueue
  880. * setup request here
  881. */
  882. s3c_hsotg_enqueue_setup(hsotg);
  883. }
  884. /**
  885. * s3c_hsotg_process_control - process a control request
  886. * @hsotg: The device state
  887. * @ctrl: The control request received
  888. *
  889. * The controller has received the SETUP phase of a control request, and
  890. * needs to work out what to do next (and whether to pass it on to the
  891. * gadget driver).
  892. */
  893. static void s3c_hsotg_process_control(struct s3c_hsotg *hsotg,
  894. struct usb_ctrlrequest *ctrl)
  895. {
  896. struct s3c_hsotg_ep *ep0 = &hsotg->eps[0];
  897. int ret = 0;
  898. u32 dcfg;
  899. ep0->sent_zlp = 0;
  900. dev_dbg(hsotg->dev, "ctrl Req=%02x, Type=%02x, V=%04x, L=%04x\n",
  901. ctrl->bRequest, ctrl->bRequestType,
  902. ctrl->wValue, ctrl->wLength);
  903. /*
  904. * record the direction of the request, for later use when enquing
  905. * packets onto EP0.
  906. */
  907. ep0->dir_in = (ctrl->bRequestType & USB_DIR_IN) ? 1 : 0;
  908. dev_dbg(hsotg->dev, "ctrl: dir_in=%d\n", ep0->dir_in);
  909. /*
  910. * if we've no data with this request, then the last part of the
  911. * transaction is going to implicitly be IN.
  912. */
  913. if (ctrl->wLength == 0)
  914. ep0->dir_in = 1;
  915. if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
  916. switch (ctrl->bRequest) {
  917. case USB_REQ_SET_ADDRESS:
  918. s3c_hsotg_disconnect(hsotg);
  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 s3c_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 s3c_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 s3c_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 s3c_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 s3c_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 s3c_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 s3c_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 s3c_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 s3c_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 s3c_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 s3c_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 s3c_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 s3c_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 s3c_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 s3c_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. static void s3c_hsotg_disconnect(struct s3c_hsotg *hsotg)
  1692. {
  1693. unsigned ep;
  1694. for (ep = 0; ep < hsotg->num_of_eps; ep++)
  1695. kill_all_requests(hsotg, &hsotg->eps[ep], -ESHUTDOWN, true);
  1696. call_gadget(hsotg, disconnect);
  1697. }
  1698. /**
  1699. * s3c_hsotg_irq_fifoempty - TX FIFO empty interrupt handler
  1700. * @hsotg: The device state:
  1701. * @periodic: True if this is a periodic FIFO interrupt
  1702. */
  1703. static void s3c_hsotg_irq_fifoempty(struct s3c_hsotg *hsotg, bool periodic)
  1704. {
  1705. struct s3c_hsotg_ep *ep;
  1706. int epno, ret;
  1707. /* look through for any more data to transmit */
  1708. for (epno = 0; epno < hsotg->num_of_eps; epno++) {
  1709. ep = &hsotg->eps[epno];
  1710. if (!ep->dir_in)
  1711. continue;
  1712. if ((periodic && !ep->periodic) ||
  1713. (!periodic && ep->periodic))
  1714. continue;
  1715. ret = s3c_hsotg_trytx(hsotg, ep);
  1716. if (ret < 0)
  1717. break;
  1718. }
  1719. }
  1720. /* IRQ flags which will trigger a retry around the IRQ loop */
  1721. #define IRQ_RETRY_MASK (GINTSTS_NPTXFEMP | \
  1722. GINTSTS_PTXFEMP | \
  1723. GINTSTS_RXFLVL)
  1724. /**
  1725. * s3c_hsotg_corereset - issue softreset to the core
  1726. * @hsotg: The device state
  1727. *
  1728. * Issue a soft reset to the core, and await the core finishing it.
  1729. */
  1730. static int s3c_hsotg_corereset(struct s3c_hsotg *hsotg)
  1731. {
  1732. int timeout;
  1733. u32 grstctl;
  1734. dev_dbg(hsotg->dev, "resetting core\n");
  1735. /* issue soft reset */
  1736. writel(GRSTCTL_CSFTRST, hsotg->regs + GRSTCTL);
  1737. timeout = 10000;
  1738. do {
  1739. grstctl = readl(hsotg->regs + GRSTCTL);
  1740. } while ((grstctl & GRSTCTL_CSFTRST) && timeout-- > 0);
  1741. if (grstctl & GRSTCTL_CSFTRST) {
  1742. dev_err(hsotg->dev, "Failed to get CSftRst asserted\n");
  1743. return -EINVAL;
  1744. }
  1745. timeout = 10000;
  1746. while (1) {
  1747. u32 grstctl = readl(hsotg->regs + GRSTCTL);
  1748. if (timeout-- < 0) {
  1749. dev_info(hsotg->dev,
  1750. "%s: reset failed, GRSTCTL=%08x\n",
  1751. __func__, grstctl);
  1752. return -ETIMEDOUT;
  1753. }
  1754. if (!(grstctl & GRSTCTL_AHBIDLE))
  1755. continue;
  1756. break; /* reset done */
  1757. }
  1758. dev_dbg(hsotg->dev, "reset successful\n");
  1759. return 0;
  1760. }
  1761. /**
  1762. * s3c_hsotg_core_init - issue softreset to the core
  1763. * @hsotg: The device state
  1764. *
  1765. * Issue a soft reset to the core, and await the core finishing it.
  1766. */
  1767. static void s3c_hsotg_core_init(struct s3c_hsotg *hsotg)
  1768. {
  1769. s3c_hsotg_corereset(hsotg);
  1770. /*
  1771. * we must now enable ep0 ready for host detection and then
  1772. * set configuration.
  1773. */
  1774. /* set the PLL on, remove the HNP/SRP and set the PHY */
  1775. writel(hsotg->phyif | GUSBCFG_TOUTCAL(7) |
  1776. (0x5 << 10), hsotg->regs + GUSBCFG);
  1777. s3c_hsotg_init_fifo(hsotg);
  1778. __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON);
  1779. writel(1 << 18 | DCFG_DEVSPD_HS, hsotg->regs + DCFG);
  1780. /* Clear any pending OTG interrupts */
  1781. writel(0xffffffff, hsotg->regs + GOTGINT);
  1782. /* Clear any pending interrupts */
  1783. writel(0xffffffff, hsotg->regs + GINTSTS);
  1784. writel(GINTSTS_ERLYSUSP | GINTSTS_SESSREQINT |
  1785. GINTSTS_GOUTNAKEFF | GINTSTS_GINNAKEFF |
  1786. GINTSTS_CONIDSTSCHNG | GINTSTS_USBRST |
  1787. GINTSTS_ENUMDONE | GINTSTS_OTGINT |
  1788. GINTSTS_USBSUSP | GINTSTS_WKUPINT,
  1789. hsotg->regs + GINTMSK);
  1790. if (using_dma(hsotg))
  1791. writel(GAHBCFG_GLBL_INTR_EN | GAHBCFG_DMA_EN |
  1792. GAHBCFG_HBSTLEN_INCR4,
  1793. hsotg->regs + GAHBCFG);
  1794. else
  1795. writel(((hsotg->dedicated_fifos) ? (GAHBCFG_NP_TXF_EMP_LVL |
  1796. GAHBCFG_P_TXF_EMP_LVL) : 0) |
  1797. GAHBCFG_GLBL_INTR_EN,
  1798. hsotg->regs + GAHBCFG);
  1799. /*
  1800. * If INTknTXFEmpMsk is enabled, it's important to disable ep interrupts
  1801. * when we have no data to transfer. Otherwise we get being flooded by
  1802. * interrupts.
  1803. */
  1804. writel(((hsotg->dedicated_fifos) ? DIEPMSK_TXFIFOEMPTY |
  1805. DIEPMSK_INTKNTXFEMPMSK : 0) |
  1806. DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK |
  1807. DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK |
  1808. DIEPMSK_INTKNEPMISMSK,
  1809. hsotg->regs + DIEPMSK);
  1810. /*
  1811. * don't need XferCompl, we get that from RXFIFO in slave mode. In
  1812. * DMA mode we may need this.
  1813. */
  1814. writel((using_dma(hsotg) ? (DIEPMSK_XFERCOMPLMSK |
  1815. DIEPMSK_TIMEOUTMSK) : 0) |
  1816. DOEPMSK_EPDISBLDMSK | DOEPMSK_AHBERRMSK |
  1817. DOEPMSK_SETUPMSK,
  1818. hsotg->regs + DOEPMSK);
  1819. writel(0, hsotg->regs + DAINTMSK);
  1820. dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
  1821. readl(hsotg->regs + DIEPCTL0),
  1822. readl(hsotg->regs + DOEPCTL0));
  1823. /* enable in and out endpoint interrupts */
  1824. s3c_hsotg_en_gsint(hsotg, GINTSTS_OEPINT | GINTSTS_IEPINT);
  1825. /*
  1826. * Enable the RXFIFO when in slave mode, as this is how we collect
  1827. * the data. In DMA mode, we get events from the FIFO but also
  1828. * things we cannot process, so do not use it.
  1829. */
  1830. if (!using_dma(hsotg))
  1831. s3c_hsotg_en_gsint(hsotg, GINTSTS_RXFLVL);
  1832. /* Enable interrupts for EP0 in and out */
  1833. s3c_hsotg_ctrl_epint(hsotg, 0, 0, 1);
  1834. s3c_hsotg_ctrl_epint(hsotg, 0, 1, 1);
  1835. __orr32(hsotg->regs + DCTL, DCTL_PWRONPRGDONE);
  1836. udelay(10); /* see openiboot */
  1837. __bic32(hsotg->regs + DCTL, DCTL_PWRONPRGDONE);
  1838. dev_dbg(hsotg->dev, "DCTL=0x%08x\n", readl(hsotg->regs + DCTL));
  1839. /*
  1840. * DxEPCTL_USBActEp says RO in manual, but seems to be set by
  1841. * writing to the EPCTL register..
  1842. */
  1843. /* set to read 1 8byte packet */
  1844. writel(DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
  1845. DXEPTSIZ_XFERSIZE(8), hsotg->regs + DOEPTSIZ0);
  1846. writel(s3c_hsotg_ep0_mps(hsotg->eps[0].ep.maxpacket) |
  1847. DXEPCTL_CNAK | DXEPCTL_EPENA |
  1848. DXEPCTL_USBACTEP,
  1849. hsotg->regs + DOEPCTL0);
  1850. /* enable, but don't activate EP0in */
  1851. writel(s3c_hsotg_ep0_mps(hsotg->eps[0].ep.maxpacket) |
  1852. DXEPCTL_USBACTEP, hsotg->regs + DIEPCTL0);
  1853. s3c_hsotg_enqueue_setup(hsotg);
  1854. dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
  1855. readl(hsotg->regs + DIEPCTL0),
  1856. readl(hsotg->regs + DOEPCTL0));
  1857. /* clear global NAKs */
  1858. writel(DCTL_CGOUTNAK | DCTL_CGNPINNAK,
  1859. hsotg->regs + DCTL);
  1860. /* must be at-least 3ms to allow bus to see disconnect */
  1861. mdelay(3);
  1862. /* remove the soft-disconnect and let's go */
  1863. __bic32(hsotg->regs + DCTL, DCTL_SFTDISCON);
  1864. }
  1865. /**
  1866. * s3c_hsotg_irq - handle device interrupt
  1867. * @irq: The IRQ number triggered
  1868. * @pw: The pw value when registered the handler.
  1869. */
  1870. static irqreturn_t s3c_hsotg_irq(int irq, void *pw)
  1871. {
  1872. struct s3c_hsotg *hsotg = pw;
  1873. int retry_count = 8;
  1874. u32 gintsts;
  1875. u32 gintmsk;
  1876. spin_lock(&hsotg->lock);
  1877. irq_retry:
  1878. gintsts = readl(hsotg->regs + GINTSTS);
  1879. gintmsk = readl(hsotg->regs + GINTMSK);
  1880. dev_dbg(hsotg->dev, "%s: %08x %08x (%08x) retry %d\n",
  1881. __func__, gintsts, gintsts & gintmsk, gintmsk, retry_count);
  1882. gintsts &= gintmsk;
  1883. if (gintsts & GINTSTS_OTGINT) {
  1884. u32 otgint = readl(hsotg->regs + GOTGINT);
  1885. dev_info(hsotg->dev, "OTGInt: %08x\n", otgint);
  1886. writel(otgint, hsotg->regs + GOTGINT);
  1887. }
  1888. if (gintsts & GINTSTS_SESSREQINT) {
  1889. dev_dbg(hsotg->dev, "%s: SessReqInt\n", __func__);
  1890. writel(GINTSTS_SESSREQINT, hsotg->regs + GINTSTS);
  1891. }
  1892. if (gintsts & GINTSTS_ENUMDONE) {
  1893. writel(GINTSTS_ENUMDONE, hsotg->regs + GINTSTS);
  1894. s3c_hsotg_irq_enumdone(hsotg);
  1895. }
  1896. if (gintsts & GINTSTS_CONIDSTSCHNG) {
  1897. dev_dbg(hsotg->dev, "ConIDStsChg (DSTS=0x%08x, GOTCTL=%08x)\n",
  1898. readl(hsotg->regs + DSTS),
  1899. readl(hsotg->regs + GOTGCTL));
  1900. writel(GINTSTS_CONIDSTSCHNG, hsotg->regs + GINTSTS);
  1901. }
  1902. if (gintsts & (GINTSTS_OEPINT | GINTSTS_IEPINT)) {
  1903. u32 daint = readl(hsotg->regs + DAINT);
  1904. u32 daintmsk = readl(hsotg->regs + DAINTMSK);
  1905. u32 daint_out, daint_in;
  1906. int ep;
  1907. daint &= daintmsk;
  1908. daint_out = daint >> DAINT_OUTEP_SHIFT;
  1909. daint_in = daint & ~(daint_out << DAINT_OUTEP_SHIFT);
  1910. dev_dbg(hsotg->dev, "%s: daint=%08x\n", __func__, daint);
  1911. for (ep = 0; ep < 15 && daint_out; ep++, daint_out >>= 1) {
  1912. if (daint_out & 1)
  1913. s3c_hsotg_epint(hsotg, ep, 0);
  1914. }
  1915. for (ep = 0; ep < 15 && daint_in; ep++, daint_in >>= 1) {
  1916. if (daint_in & 1)
  1917. s3c_hsotg_epint(hsotg, ep, 1);
  1918. }
  1919. }
  1920. if (gintsts & GINTSTS_USBRST) {
  1921. u32 usb_status = readl(hsotg->regs + GOTGCTL);
  1922. dev_dbg(hsotg->dev, "%s: USBRst\n", __func__);
  1923. dev_dbg(hsotg->dev, "GNPTXSTS=%08x\n",
  1924. readl(hsotg->regs + GNPTXSTS));
  1925. writel(GINTSTS_USBRST, hsotg->regs + GINTSTS);
  1926. if (usb_status & GOTGCTL_BSESVLD) {
  1927. if (time_after(jiffies, hsotg->last_rst +
  1928. msecs_to_jiffies(200))) {
  1929. kill_all_requests(hsotg, &hsotg->eps[0],
  1930. -ECONNRESET, true);
  1931. s3c_hsotg_core_init(hsotg);
  1932. hsotg->last_rst = jiffies;
  1933. }
  1934. }
  1935. }
  1936. /* check both FIFOs */
  1937. if (gintsts & GINTSTS_NPTXFEMP) {
  1938. dev_dbg(hsotg->dev, "NPTxFEmp\n");
  1939. /*
  1940. * Disable the interrupt to stop it happening again
  1941. * unless one of these endpoint routines decides that
  1942. * it needs re-enabling
  1943. */
  1944. s3c_hsotg_disable_gsint(hsotg, GINTSTS_NPTXFEMP);
  1945. s3c_hsotg_irq_fifoempty(hsotg, false);
  1946. }
  1947. if (gintsts & GINTSTS_PTXFEMP) {
  1948. dev_dbg(hsotg->dev, "PTxFEmp\n");
  1949. /* See note in GINTSTS_NPTxFEmp */
  1950. s3c_hsotg_disable_gsint(hsotg, GINTSTS_PTXFEMP);
  1951. s3c_hsotg_irq_fifoempty(hsotg, true);
  1952. }
  1953. if (gintsts & GINTSTS_RXFLVL) {
  1954. /*
  1955. * note, since GINTSTS_RxFLvl doubles as FIFO-not-empty,
  1956. * we need to retry s3c_hsotg_handle_rx if this is still
  1957. * set.
  1958. */
  1959. s3c_hsotg_handle_rx(hsotg);
  1960. }
  1961. if (gintsts & GINTSTS_MODEMIS) {
  1962. dev_warn(hsotg->dev, "warning, mode mismatch triggered\n");
  1963. writel(GINTSTS_MODEMIS, hsotg->regs + GINTSTS);
  1964. }
  1965. if (gintsts & GINTSTS_USBSUSP) {
  1966. dev_info(hsotg->dev, "GINTSTS_USBSusp\n");
  1967. writel(GINTSTS_USBSUSP, hsotg->regs + GINTSTS);
  1968. call_gadget(hsotg, suspend);
  1969. }
  1970. if (gintsts & GINTSTS_WKUPINT) {
  1971. dev_info(hsotg->dev, "GINTSTS_WkUpIn\n");
  1972. writel(GINTSTS_WKUPINT, hsotg->regs + GINTSTS);
  1973. call_gadget(hsotg, resume);
  1974. }
  1975. if (gintsts & GINTSTS_ERLYSUSP) {
  1976. dev_dbg(hsotg->dev, "GINTSTS_ErlySusp\n");
  1977. writel(GINTSTS_ERLYSUSP, hsotg->regs + GINTSTS);
  1978. }
  1979. /*
  1980. * these next two seem to crop-up occasionally causing the core
  1981. * to shutdown the USB transfer, so try clearing them and logging
  1982. * the occurrence.
  1983. */
  1984. if (gintsts & GINTSTS_GOUTNAKEFF) {
  1985. dev_info(hsotg->dev, "GOUTNakEff triggered\n");
  1986. writel(DCTL_CGOUTNAK, hsotg->regs + DCTL);
  1987. s3c_hsotg_dump(hsotg);
  1988. }
  1989. if (gintsts & GINTSTS_GINNAKEFF) {
  1990. dev_info(hsotg->dev, "GINNakEff triggered\n");
  1991. writel(DCTL_CGNPINNAK, hsotg->regs + DCTL);
  1992. s3c_hsotg_dump(hsotg);
  1993. }
  1994. /*
  1995. * if we've had fifo events, we should try and go around the
  1996. * loop again to see if there's any point in returning yet.
  1997. */
  1998. if (gintsts & IRQ_RETRY_MASK && --retry_count > 0)
  1999. goto irq_retry;
  2000. spin_unlock(&hsotg->lock);
  2001. return IRQ_HANDLED;
  2002. }
  2003. /**
  2004. * s3c_hsotg_ep_enable - enable the given endpoint
  2005. * @ep: The USB endpint to configure
  2006. * @desc: The USB endpoint descriptor to configure with.
  2007. *
  2008. * This is called from the USB gadget code's usb_ep_enable().
  2009. */
  2010. static int s3c_hsotg_ep_enable(struct usb_ep *ep,
  2011. const struct usb_endpoint_descriptor *desc)
  2012. {
  2013. struct s3c_hsotg_ep *hs_ep = our_ep(ep);
  2014. struct s3c_hsotg *hsotg = hs_ep->parent;
  2015. unsigned long flags;
  2016. int index = hs_ep->index;
  2017. u32 epctrl_reg;
  2018. u32 epctrl;
  2019. u32 mps;
  2020. int dir_in;
  2021. int i, val, size;
  2022. int ret = 0;
  2023. dev_dbg(hsotg->dev,
  2024. "%s: ep %s: a 0x%02x, attr 0x%02x, mps 0x%04x, intr %d\n",
  2025. __func__, ep->name, desc->bEndpointAddress, desc->bmAttributes,
  2026. desc->wMaxPacketSize, desc->bInterval);
  2027. /* not to be called for EP0 */
  2028. WARN_ON(index == 0);
  2029. dir_in = (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? 1 : 0;
  2030. if (dir_in != hs_ep->dir_in) {
  2031. dev_err(hsotg->dev, "%s: direction mismatch!\n", __func__);
  2032. return -EINVAL;
  2033. }
  2034. mps = usb_endpoint_maxp(desc);
  2035. /* note, we handle this here instead of s3c_hsotg_set_ep_maxpacket */
  2036. epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
  2037. epctrl = readl(hsotg->regs + epctrl_reg);
  2038. dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x from 0x%08x\n",
  2039. __func__, epctrl, epctrl_reg);
  2040. spin_lock_irqsave(&hsotg->lock, flags);
  2041. epctrl &= ~(DXEPCTL_EPTYPE_MASK | DXEPCTL_MPS_MASK);
  2042. epctrl |= DXEPCTL_MPS(mps);
  2043. /*
  2044. * mark the endpoint as active, otherwise the core may ignore
  2045. * transactions entirely for this endpoint
  2046. */
  2047. epctrl |= DXEPCTL_USBACTEP;
  2048. /*
  2049. * set the NAK status on the endpoint, otherwise we might try and
  2050. * do something with data that we've yet got a request to process
  2051. * since the RXFIFO will take data for an endpoint even if the
  2052. * size register hasn't been set.
  2053. */
  2054. epctrl |= DXEPCTL_SNAK;
  2055. /* update the endpoint state */
  2056. s3c_hsotg_set_ep_maxpacket(hsotg, hs_ep->index, mps);
  2057. /* default, set to non-periodic */
  2058. hs_ep->isochronous = 0;
  2059. hs_ep->periodic = 0;
  2060. hs_ep->halted = 0;
  2061. hs_ep->interval = desc->bInterval;
  2062. if (hs_ep->interval > 1 && hs_ep->mc > 1)
  2063. dev_err(hsotg->dev, "MC > 1 when interval is not 1\n");
  2064. switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
  2065. case USB_ENDPOINT_XFER_ISOC:
  2066. epctrl |= DXEPCTL_EPTYPE_ISO;
  2067. epctrl |= DXEPCTL_SETEVENFR;
  2068. hs_ep->isochronous = 1;
  2069. if (dir_in)
  2070. hs_ep->periodic = 1;
  2071. break;
  2072. case USB_ENDPOINT_XFER_BULK:
  2073. epctrl |= DXEPCTL_EPTYPE_BULK;
  2074. break;
  2075. case USB_ENDPOINT_XFER_INT:
  2076. if (dir_in)
  2077. hs_ep->periodic = 1;
  2078. epctrl |= DXEPCTL_EPTYPE_INTERRUPT;
  2079. break;
  2080. case USB_ENDPOINT_XFER_CONTROL:
  2081. epctrl |= DXEPCTL_EPTYPE_CONTROL;
  2082. break;
  2083. }
  2084. /*
  2085. * if the hardware has dedicated fifos, we must give each IN EP
  2086. * a unique tx-fifo even if it is non-periodic.
  2087. */
  2088. if (dir_in && hsotg->dedicated_fifos) {
  2089. size = hs_ep->ep.maxpacket*hs_ep->mc;
  2090. for (i = 1; i <= 8; ++i) {
  2091. if (hsotg->fifo_map & (1<<i))
  2092. continue;
  2093. val = readl(hsotg->regs + DPTXFSIZN(i));
  2094. val = (val >> FIFOSIZE_DEPTH_SHIFT)*4;
  2095. if (val < size)
  2096. continue;
  2097. hsotg->fifo_map |= 1<<i;
  2098. epctrl |= DXEPCTL_TXFNUM(i);
  2099. hs_ep->fifo_index = i;
  2100. hs_ep->fifo_size = val;
  2101. break;
  2102. }
  2103. if (i == 8) {
  2104. ret = -ENOMEM;
  2105. goto error;
  2106. }
  2107. }
  2108. /* for non control endpoints, set PID to D0 */
  2109. if (index)
  2110. epctrl |= DXEPCTL_SETD0PID;
  2111. dev_dbg(hsotg->dev, "%s: write DxEPCTL=0x%08x\n",
  2112. __func__, epctrl);
  2113. writel(epctrl, hsotg->regs + epctrl_reg);
  2114. dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x\n",
  2115. __func__, readl(hsotg->regs + epctrl_reg));
  2116. /* enable the endpoint interrupt */
  2117. s3c_hsotg_ctrl_epint(hsotg, index, dir_in, 1);
  2118. error:
  2119. spin_unlock_irqrestore(&hsotg->lock, flags);
  2120. return ret;
  2121. }
  2122. /**
  2123. * s3c_hsotg_ep_disable - disable given endpoint
  2124. * @ep: The endpoint to disable.
  2125. */
  2126. static int s3c_hsotg_ep_disable(struct usb_ep *ep)
  2127. {
  2128. struct s3c_hsotg_ep *hs_ep = our_ep(ep);
  2129. struct s3c_hsotg *hsotg = hs_ep->parent;
  2130. int dir_in = hs_ep->dir_in;
  2131. int index = hs_ep->index;
  2132. unsigned long flags;
  2133. u32 epctrl_reg;
  2134. u32 ctrl;
  2135. dev_dbg(hsotg->dev, "%s(ep %p)\n", __func__, ep);
  2136. if (ep == &hsotg->eps[0].ep) {
  2137. dev_err(hsotg->dev, "%s: called for ep0\n", __func__);
  2138. return -EINVAL;
  2139. }
  2140. epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
  2141. spin_lock_irqsave(&hsotg->lock, flags);
  2142. /* terminate all requests with shutdown */
  2143. kill_all_requests(hsotg, hs_ep, -ESHUTDOWN, false);
  2144. hsotg->fifo_map &= ~(1<<hs_ep->fifo_index);
  2145. hs_ep->fifo_index = 0;
  2146. hs_ep->fifo_size = 0;
  2147. ctrl = readl(hsotg->regs + epctrl_reg);
  2148. ctrl &= ~DXEPCTL_EPENA;
  2149. ctrl &= ~DXEPCTL_USBACTEP;
  2150. ctrl |= DXEPCTL_SNAK;
  2151. dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
  2152. writel(ctrl, hsotg->regs + epctrl_reg);
  2153. /* disable endpoint interrupts */
  2154. s3c_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 0);
  2155. spin_unlock_irqrestore(&hsotg->lock, flags);
  2156. return 0;
  2157. }
  2158. /**
  2159. * on_list - check request is on the given endpoint
  2160. * @ep: The endpoint to check.
  2161. * @test: The request to test if it is on the endpoint.
  2162. */
  2163. static bool on_list(struct s3c_hsotg_ep *ep, struct s3c_hsotg_req *test)
  2164. {
  2165. struct s3c_hsotg_req *req, *treq;
  2166. list_for_each_entry_safe(req, treq, &ep->queue, queue) {
  2167. if (req == test)
  2168. return true;
  2169. }
  2170. return false;
  2171. }
  2172. /**
  2173. * s3c_hsotg_ep_dequeue - dequeue given endpoint
  2174. * @ep: The endpoint to dequeue.
  2175. * @req: The request to be removed from a queue.
  2176. */
  2177. static int s3c_hsotg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
  2178. {
  2179. struct s3c_hsotg_req *hs_req = our_req(req);
  2180. struct s3c_hsotg_ep *hs_ep = our_ep(ep);
  2181. struct s3c_hsotg *hs = hs_ep->parent;
  2182. unsigned long flags;
  2183. dev_dbg(hs->dev, "ep_dequeue(%p,%p)\n", ep, req);
  2184. spin_lock_irqsave(&hs->lock, flags);
  2185. if (!on_list(hs_ep, hs_req)) {
  2186. spin_unlock_irqrestore(&hs->lock, flags);
  2187. return -EINVAL;
  2188. }
  2189. s3c_hsotg_complete_request(hs, hs_ep, hs_req, -ECONNRESET);
  2190. spin_unlock_irqrestore(&hs->lock, flags);
  2191. return 0;
  2192. }
  2193. /**
  2194. * s3c_hsotg_ep_sethalt - set halt on a given endpoint
  2195. * @ep: The endpoint to set halt.
  2196. * @value: Set or unset the halt.
  2197. */
  2198. static int s3c_hsotg_ep_sethalt(struct usb_ep *ep, int value)
  2199. {
  2200. struct s3c_hsotg_ep *hs_ep = our_ep(ep);
  2201. struct s3c_hsotg *hs = hs_ep->parent;
  2202. int index = hs_ep->index;
  2203. u32 epreg;
  2204. u32 epctl;
  2205. u32 xfertype;
  2206. dev_info(hs->dev, "%s(ep %p %s, %d)\n", __func__, ep, ep->name, value);
  2207. if (index == 0) {
  2208. if (value)
  2209. s3c_hsotg_stall_ep0(hs);
  2210. else
  2211. dev_warn(hs->dev,
  2212. "%s: can't clear halt on ep0\n", __func__);
  2213. return 0;
  2214. }
  2215. /* write both IN and OUT control registers */
  2216. epreg = DIEPCTL(index);
  2217. epctl = readl(hs->regs + epreg);
  2218. if (value) {
  2219. epctl |= DXEPCTL_STALL + DXEPCTL_SNAK;
  2220. if (epctl & DXEPCTL_EPENA)
  2221. epctl |= DXEPCTL_EPDIS;
  2222. } else {
  2223. epctl &= ~DXEPCTL_STALL;
  2224. xfertype = epctl & DXEPCTL_EPTYPE_MASK;
  2225. if (xfertype == DXEPCTL_EPTYPE_BULK ||
  2226. xfertype == DXEPCTL_EPTYPE_INTERRUPT)
  2227. epctl |= DXEPCTL_SETD0PID;
  2228. }
  2229. writel(epctl, hs->regs + epreg);
  2230. epreg = DOEPCTL(index);
  2231. epctl = readl(hs->regs + epreg);
  2232. if (value)
  2233. epctl |= DXEPCTL_STALL;
  2234. else {
  2235. epctl &= ~DXEPCTL_STALL;
  2236. xfertype = epctl & DXEPCTL_EPTYPE_MASK;
  2237. if (xfertype == DXEPCTL_EPTYPE_BULK ||
  2238. xfertype == DXEPCTL_EPTYPE_INTERRUPT)
  2239. epctl |= DXEPCTL_SETD0PID;
  2240. }
  2241. writel(epctl, hs->regs + epreg);
  2242. hs_ep->halted = value;
  2243. return 0;
  2244. }
  2245. /**
  2246. * s3c_hsotg_ep_sethalt_lock - set halt on a given endpoint with lock held
  2247. * @ep: The endpoint to set halt.
  2248. * @value: Set or unset the halt.
  2249. */
  2250. static int s3c_hsotg_ep_sethalt_lock(struct usb_ep *ep, int value)
  2251. {
  2252. struct s3c_hsotg_ep *hs_ep = our_ep(ep);
  2253. struct s3c_hsotg *hs = hs_ep->parent;
  2254. unsigned long flags = 0;
  2255. int ret = 0;
  2256. spin_lock_irqsave(&hs->lock, flags);
  2257. ret = s3c_hsotg_ep_sethalt(ep, value);
  2258. spin_unlock_irqrestore(&hs->lock, flags);
  2259. return ret;
  2260. }
  2261. static struct usb_ep_ops s3c_hsotg_ep_ops = {
  2262. .enable = s3c_hsotg_ep_enable,
  2263. .disable = s3c_hsotg_ep_disable,
  2264. .alloc_request = s3c_hsotg_ep_alloc_request,
  2265. .free_request = s3c_hsotg_ep_free_request,
  2266. .queue = s3c_hsotg_ep_queue_lock,
  2267. .dequeue = s3c_hsotg_ep_dequeue,
  2268. .set_halt = s3c_hsotg_ep_sethalt_lock,
  2269. /* note, don't believe we have any call for the fifo routines */
  2270. };
  2271. /**
  2272. * s3c_hsotg_phy_enable - enable platform phy dev
  2273. * @hsotg: The driver state
  2274. *
  2275. * A wrapper for platform code responsible for controlling
  2276. * low-level USB code
  2277. */
  2278. static void s3c_hsotg_phy_enable(struct s3c_hsotg *hsotg)
  2279. {
  2280. struct platform_device *pdev = to_platform_device(hsotg->dev);
  2281. dev_dbg(hsotg->dev, "pdev 0x%p\n", pdev);
  2282. if (hsotg->uphy)
  2283. usb_phy_init(hsotg->uphy);
  2284. else if (hsotg->plat && hsotg->plat->phy_init)
  2285. hsotg->plat->phy_init(pdev, hsotg->plat->phy_type);
  2286. else {
  2287. phy_init(hsotg->phy);
  2288. phy_power_on(hsotg->phy);
  2289. }
  2290. }
  2291. /**
  2292. * s3c_hsotg_phy_disable - disable platform phy dev
  2293. * @hsotg: The driver state
  2294. *
  2295. * A wrapper for platform code responsible for controlling
  2296. * low-level USB code
  2297. */
  2298. static void s3c_hsotg_phy_disable(struct s3c_hsotg *hsotg)
  2299. {
  2300. struct platform_device *pdev = to_platform_device(hsotg->dev);
  2301. if (hsotg->uphy)
  2302. usb_phy_shutdown(hsotg->uphy);
  2303. else if (hsotg->plat && hsotg->plat->phy_exit)
  2304. hsotg->plat->phy_exit(pdev, hsotg->plat->phy_type);
  2305. else {
  2306. phy_power_off(hsotg->phy);
  2307. phy_exit(hsotg->phy);
  2308. }
  2309. }
  2310. /**
  2311. * s3c_hsotg_init - initalize the usb core
  2312. * @hsotg: The driver state
  2313. */
  2314. static void s3c_hsotg_init(struct s3c_hsotg *hsotg)
  2315. {
  2316. /* unmask subset of endpoint interrupts */
  2317. writel(DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK |
  2318. DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK,
  2319. hsotg->regs + DIEPMSK);
  2320. writel(DOEPMSK_SETUPMSK | DOEPMSK_AHBERRMSK |
  2321. DOEPMSK_EPDISBLDMSK | DOEPMSK_XFERCOMPLMSK,
  2322. hsotg->regs + DOEPMSK);
  2323. writel(0, hsotg->regs + DAINTMSK);
  2324. /* Be in disconnected state until gadget is registered */
  2325. __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON);
  2326. if (0) {
  2327. /* post global nak until we're ready */
  2328. writel(DCTL_SGNPINNAK | DCTL_SGOUTNAK,
  2329. hsotg->regs + DCTL);
  2330. }
  2331. /* setup fifos */
  2332. dev_dbg(hsotg->dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
  2333. readl(hsotg->regs + GRXFSIZ),
  2334. readl(hsotg->regs + GNPTXFSIZ));
  2335. s3c_hsotg_init_fifo(hsotg);
  2336. /* set the PLL on, remove the HNP/SRP and set the PHY */
  2337. writel(GUSBCFG_PHYIF16 | GUSBCFG_TOUTCAL(7) | (0x5 << 10),
  2338. hsotg->regs + GUSBCFG);
  2339. writel(using_dma(hsotg) ? GAHBCFG_DMA_EN : 0x0,
  2340. hsotg->regs + GAHBCFG);
  2341. }
  2342. /**
  2343. * s3c_hsotg_udc_start - prepare the udc for work
  2344. * @gadget: The usb gadget state
  2345. * @driver: The usb gadget driver
  2346. *
  2347. * Perform initialization to prepare udc device and driver
  2348. * to work.
  2349. */
  2350. static int s3c_hsotg_udc_start(struct usb_gadget *gadget,
  2351. struct usb_gadget_driver *driver)
  2352. {
  2353. struct s3c_hsotg *hsotg = to_hsotg(gadget);
  2354. int ret;
  2355. if (!hsotg) {
  2356. pr_err("%s: called with no device\n", __func__);
  2357. return -ENODEV;
  2358. }
  2359. if (!driver) {
  2360. dev_err(hsotg->dev, "%s: no driver\n", __func__);
  2361. return -EINVAL;
  2362. }
  2363. if (driver->max_speed < USB_SPEED_FULL)
  2364. dev_err(hsotg->dev, "%s: bad speed\n", __func__);
  2365. if (!driver->setup) {
  2366. dev_err(hsotg->dev, "%s: missing entry points\n", __func__);
  2367. return -EINVAL;
  2368. }
  2369. WARN_ON(hsotg->driver);
  2370. driver->driver.bus = NULL;
  2371. hsotg->driver = driver;
  2372. hsotg->gadget.dev.of_node = hsotg->dev->of_node;
  2373. hsotg->gadget.speed = USB_SPEED_UNKNOWN;
  2374. clk_enable(hsotg->clk);
  2375. ret = regulator_bulk_enable(ARRAY_SIZE(hsotg->supplies),
  2376. hsotg->supplies);
  2377. if (ret) {
  2378. dev_err(hsotg->dev, "failed to enable supplies: %d\n", ret);
  2379. goto err;
  2380. }
  2381. hsotg->last_rst = jiffies;
  2382. dev_info(hsotg->dev, "bound driver %s\n", driver->driver.name);
  2383. return 0;
  2384. err:
  2385. hsotg->driver = NULL;
  2386. return ret;
  2387. }
  2388. /**
  2389. * s3c_hsotg_udc_stop - stop the udc
  2390. * @gadget: The usb gadget state
  2391. * @driver: The usb gadget driver
  2392. *
  2393. * Stop udc hw block and stay tunned for future transmissions
  2394. */
  2395. static int s3c_hsotg_udc_stop(struct usb_gadget *gadget,
  2396. struct usb_gadget_driver *driver)
  2397. {
  2398. struct s3c_hsotg *hsotg = to_hsotg(gadget);
  2399. unsigned long flags = 0;
  2400. int ep;
  2401. if (!hsotg)
  2402. return -ENODEV;
  2403. /* all endpoints should be shutdown */
  2404. for (ep = 1; ep < hsotg->num_of_eps; ep++)
  2405. s3c_hsotg_ep_disable(&hsotg->eps[ep].ep);
  2406. spin_lock_irqsave(&hsotg->lock, flags);
  2407. hsotg->driver = NULL;
  2408. hsotg->gadget.speed = USB_SPEED_UNKNOWN;
  2409. spin_unlock_irqrestore(&hsotg->lock, flags);
  2410. regulator_bulk_disable(ARRAY_SIZE(hsotg->supplies), hsotg->supplies);
  2411. clk_disable(hsotg->clk);
  2412. return 0;
  2413. }
  2414. /**
  2415. * s3c_hsotg_gadget_getframe - read the frame number
  2416. * @gadget: The usb gadget state
  2417. *
  2418. * Read the {micro} frame number
  2419. */
  2420. static int s3c_hsotg_gadget_getframe(struct usb_gadget *gadget)
  2421. {
  2422. return s3c_hsotg_read_frameno(to_hsotg(gadget));
  2423. }
  2424. /**
  2425. * s3c_hsotg_pullup - connect/disconnect the USB PHY
  2426. * @gadget: The usb gadget state
  2427. * @is_on: Current state of the USB PHY
  2428. *
  2429. * Connect/Disconnect the USB PHY pullup
  2430. */
  2431. static int s3c_hsotg_pullup(struct usb_gadget *gadget, int is_on)
  2432. {
  2433. struct s3c_hsotg *hsotg = to_hsotg(gadget);
  2434. unsigned long flags = 0;
  2435. dev_dbg(hsotg->dev, "%s: is_on: %d\n", __func__, is_on);
  2436. spin_lock_irqsave(&hsotg->lock, flags);
  2437. if (is_on) {
  2438. s3c_hsotg_phy_enable(hsotg);
  2439. clk_enable(hsotg->clk);
  2440. s3c_hsotg_core_init(hsotg);
  2441. } else {
  2442. clk_disable(hsotg->clk);
  2443. s3c_hsotg_phy_disable(hsotg);
  2444. }
  2445. hsotg->gadget.speed = USB_SPEED_UNKNOWN;
  2446. spin_unlock_irqrestore(&hsotg->lock, flags);
  2447. return 0;
  2448. }
  2449. static const struct usb_gadget_ops s3c_hsotg_gadget_ops = {
  2450. .get_frame = s3c_hsotg_gadget_getframe,
  2451. .udc_start = s3c_hsotg_udc_start,
  2452. .udc_stop = s3c_hsotg_udc_stop,
  2453. .pullup = s3c_hsotg_pullup,
  2454. };
  2455. /**
  2456. * s3c_hsotg_initep - initialise a single endpoint
  2457. * @hsotg: The device state.
  2458. * @hs_ep: The endpoint to be initialised.
  2459. * @epnum: The endpoint number
  2460. *
  2461. * Initialise the given endpoint (as part of the probe and device state
  2462. * creation) to give to the gadget driver. Setup the endpoint name, any
  2463. * direction information and other state that may be required.
  2464. */
  2465. static void s3c_hsotg_initep(struct s3c_hsotg *hsotg,
  2466. struct s3c_hsotg_ep *hs_ep,
  2467. int epnum)
  2468. {
  2469. char *dir;
  2470. if (epnum == 0)
  2471. dir = "";
  2472. else if ((epnum % 2) == 0) {
  2473. dir = "out";
  2474. } else {
  2475. dir = "in";
  2476. hs_ep->dir_in = 1;
  2477. }
  2478. hs_ep->index = epnum;
  2479. snprintf(hs_ep->name, sizeof(hs_ep->name), "ep%d%s", epnum, dir);
  2480. INIT_LIST_HEAD(&hs_ep->queue);
  2481. INIT_LIST_HEAD(&hs_ep->ep.ep_list);
  2482. /* add to the list of endpoints known by the gadget driver */
  2483. if (epnum)
  2484. list_add_tail(&hs_ep->ep.ep_list, &hsotg->gadget.ep_list);
  2485. hs_ep->parent = hsotg;
  2486. hs_ep->ep.name = hs_ep->name;
  2487. usb_ep_set_maxpacket_limit(&hs_ep->ep, epnum ? 1024 : EP0_MPS_LIMIT);
  2488. hs_ep->ep.ops = &s3c_hsotg_ep_ops;
  2489. /*
  2490. * if we're using dma, we need to set the next-endpoint pointer
  2491. * to be something valid.
  2492. */
  2493. if (using_dma(hsotg)) {
  2494. u32 next = DXEPCTL_NEXTEP((epnum + 1) % 15);
  2495. writel(next, hsotg->regs + DIEPCTL(epnum));
  2496. writel(next, hsotg->regs + DOEPCTL(epnum));
  2497. }
  2498. }
  2499. /**
  2500. * s3c_hsotg_hw_cfg - read HW configuration registers
  2501. * @param: The device state
  2502. *
  2503. * Read the USB core HW configuration registers
  2504. */
  2505. static void s3c_hsotg_hw_cfg(struct s3c_hsotg *hsotg)
  2506. {
  2507. u32 cfg2, cfg3, cfg4;
  2508. /* check hardware configuration */
  2509. cfg2 = readl(hsotg->regs + 0x48);
  2510. hsotg->num_of_eps = (cfg2 >> 10) & 0xF;
  2511. cfg3 = readl(hsotg->regs + 0x4C);
  2512. hsotg->fifo_mem = (cfg3 >> 16);
  2513. cfg4 = readl(hsotg->regs + 0x50);
  2514. hsotg->dedicated_fifos = (cfg4 >> 25) & 1;
  2515. dev_info(hsotg->dev, "EPs: %d, %s fifos, %d entries in SPRAM\n",
  2516. hsotg->num_of_eps,
  2517. hsotg->dedicated_fifos ? "dedicated" : "shared",
  2518. hsotg->fifo_mem);
  2519. }
  2520. /**
  2521. * s3c_hsotg_dump - dump state of the udc
  2522. * @param: The device state
  2523. */
  2524. static void s3c_hsotg_dump(struct s3c_hsotg *hsotg)
  2525. {
  2526. #ifdef DEBUG
  2527. struct device *dev = hsotg->dev;
  2528. void __iomem *regs = hsotg->regs;
  2529. u32 val;
  2530. int idx;
  2531. dev_info(dev, "DCFG=0x%08x, DCTL=0x%08x, DIEPMSK=%08x\n",
  2532. readl(regs + DCFG), readl(regs + DCTL),
  2533. readl(regs + DIEPMSK));
  2534. dev_info(dev, "GAHBCFG=0x%08x, 0x44=0x%08x\n",
  2535. readl(regs + GAHBCFG), readl(regs + 0x44));
  2536. dev_info(dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
  2537. readl(regs + GRXFSIZ), readl(regs + GNPTXFSIZ));
  2538. /* show periodic fifo settings */
  2539. for (idx = 1; idx <= 15; idx++) {
  2540. val = readl(regs + DPTXFSIZN(idx));
  2541. dev_info(dev, "DPTx[%d] FSize=%d, StAddr=0x%08x\n", idx,
  2542. val >> FIFOSIZE_DEPTH_SHIFT,
  2543. val & FIFOSIZE_STARTADDR_MASK);
  2544. }
  2545. for (idx = 0; idx < 15; idx++) {
  2546. dev_info(dev,
  2547. "ep%d-in: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n", idx,
  2548. readl(regs + DIEPCTL(idx)),
  2549. readl(regs + DIEPTSIZ(idx)),
  2550. readl(regs + DIEPDMA(idx)));
  2551. val = readl(regs + DOEPCTL(idx));
  2552. dev_info(dev,
  2553. "ep%d-out: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n",
  2554. idx, readl(regs + DOEPCTL(idx)),
  2555. readl(regs + DOEPTSIZ(idx)),
  2556. readl(regs + DOEPDMA(idx)));
  2557. }
  2558. dev_info(dev, "DVBUSDIS=0x%08x, DVBUSPULSE=%08x\n",
  2559. readl(regs + DVBUSDIS), readl(regs + DVBUSPULSE));
  2560. #endif
  2561. }
  2562. /**
  2563. * state_show - debugfs: show overall driver and device state.
  2564. * @seq: The seq file to write to.
  2565. * @v: Unused parameter.
  2566. *
  2567. * This debugfs entry shows the overall state of the hardware and
  2568. * some general information about each of the endpoints available
  2569. * to the system.
  2570. */
  2571. static int state_show(struct seq_file *seq, void *v)
  2572. {
  2573. struct s3c_hsotg *hsotg = seq->private;
  2574. void __iomem *regs = hsotg->regs;
  2575. int idx;
  2576. seq_printf(seq, "DCFG=0x%08x, DCTL=0x%08x, DSTS=0x%08x\n",
  2577. readl(regs + DCFG),
  2578. readl(regs + DCTL),
  2579. readl(regs + DSTS));
  2580. seq_printf(seq, "DIEPMSK=0x%08x, DOEPMASK=0x%08x\n",
  2581. readl(regs + DIEPMSK), readl(regs + DOEPMSK));
  2582. seq_printf(seq, "GINTMSK=0x%08x, GINTSTS=0x%08x\n",
  2583. readl(regs + GINTMSK),
  2584. readl(regs + GINTSTS));
  2585. seq_printf(seq, "DAINTMSK=0x%08x, DAINT=0x%08x\n",
  2586. readl(regs + DAINTMSK),
  2587. readl(regs + DAINT));
  2588. seq_printf(seq, "GNPTXSTS=0x%08x, GRXSTSR=%08x\n",
  2589. readl(regs + GNPTXSTS),
  2590. readl(regs + GRXSTSR));
  2591. seq_puts(seq, "\nEndpoint status:\n");
  2592. for (idx = 0; idx < 15; idx++) {
  2593. u32 in, out;
  2594. in = readl(regs + DIEPCTL(idx));
  2595. out = readl(regs + DOEPCTL(idx));
  2596. seq_printf(seq, "ep%d: DIEPCTL=0x%08x, DOEPCTL=0x%08x",
  2597. idx, in, out);
  2598. in = readl(regs + DIEPTSIZ(idx));
  2599. out = readl(regs + DOEPTSIZ(idx));
  2600. seq_printf(seq, ", DIEPTSIZ=0x%08x, DOEPTSIZ=0x%08x",
  2601. in, out);
  2602. seq_puts(seq, "\n");
  2603. }
  2604. return 0;
  2605. }
  2606. static int state_open(struct inode *inode, struct file *file)
  2607. {
  2608. return single_open(file, state_show, inode->i_private);
  2609. }
  2610. static const struct file_operations state_fops = {
  2611. .owner = THIS_MODULE,
  2612. .open = state_open,
  2613. .read = seq_read,
  2614. .llseek = seq_lseek,
  2615. .release = single_release,
  2616. };
  2617. /**
  2618. * fifo_show - debugfs: show the fifo information
  2619. * @seq: The seq_file to write data to.
  2620. * @v: Unused parameter.
  2621. *
  2622. * Show the FIFO information for the overall fifo and all the
  2623. * periodic transmission FIFOs.
  2624. */
  2625. static int fifo_show(struct seq_file *seq, void *v)
  2626. {
  2627. struct s3c_hsotg *hsotg = seq->private;
  2628. void __iomem *regs = hsotg->regs;
  2629. u32 val;
  2630. int idx;
  2631. seq_puts(seq, "Non-periodic FIFOs:\n");
  2632. seq_printf(seq, "RXFIFO: Size %d\n", readl(regs + GRXFSIZ));
  2633. val = readl(regs + GNPTXFSIZ);
  2634. seq_printf(seq, "NPTXFIFO: Size %d, Start 0x%08x\n",
  2635. val >> FIFOSIZE_DEPTH_SHIFT,
  2636. val & FIFOSIZE_DEPTH_MASK);
  2637. seq_puts(seq, "\nPeriodic TXFIFOs:\n");
  2638. for (idx = 1; idx <= 15; idx++) {
  2639. val = readl(regs + DPTXFSIZN(idx));
  2640. seq_printf(seq, "\tDPTXFIFO%2d: Size %d, Start 0x%08x\n", idx,
  2641. val >> FIFOSIZE_DEPTH_SHIFT,
  2642. val & FIFOSIZE_STARTADDR_MASK);
  2643. }
  2644. return 0;
  2645. }
  2646. static int fifo_open(struct inode *inode, struct file *file)
  2647. {
  2648. return single_open(file, fifo_show, inode->i_private);
  2649. }
  2650. static const struct file_operations fifo_fops = {
  2651. .owner = THIS_MODULE,
  2652. .open = fifo_open,
  2653. .read = seq_read,
  2654. .llseek = seq_lseek,
  2655. .release = single_release,
  2656. };
  2657. static const char *decode_direction(int is_in)
  2658. {
  2659. return is_in ? "in" : "out";
  2660. }
  2661. /**
  2662. * ep_show - debugfs: show the state of an endpoint.
  2663. * @seq: The seq_file to write data to.
  2664. * @v: Unused parameter.
  2665. *
  2666. * This debugfs entry shows the state of the given endpoint (one is
  2667. * registered for each available).
  2668. */
  2669. static int ep_show(struct seq_file *seq, void *v)
  2670. {
  2671. struct s3c_hsotg_ep *ep = seq->private;
  2672. struct s3c_hsotg *hsotg = ep->parent;
  2673. struct s3c_hsotg_req *req;
  2674. void __iomem *regs = hsotg->regs;
  2675. int index = ep->index;
  2676. int show_limit = 15;
  2677. unsigned long flags;
  2678. seq_printf(seq, "Endpoint index %d, named %s, dir %s:\n",
  2679. ep->index, ep->ep.name, decode_direction(ep->dir_in));
  2680. /* first show the register state */
  2681. seq_printf(seq, "\tDIEPCTL=0x%08x, DOEPCTL=0x%08x\n",
  2682. readl(regs + DIEPCTL(index)),
  2683. readl(regs + DOEPCTL(index)));
  2684. seq_printf(seq, "\tDIEPDMA=0x%08x, DOEPDMA=0x%08x\n",
  2685. readl(regs + DIEPDMA(index)),
  2686. readl(regs + DOEPDMA(index)));
  2687. seq_printf(seq, "\tDIEPINT=0x%08x, DOEPINT=0x%08x\n",
  2688. readl(regs + DIEPINT(index)),
  2689. readl(regs + DOEPINT(index)));
  2690. seq_printf(seq, "\tDIEPTSIZ=0x%08x, DOEPTSIZ=0x%08x\n",
  2691. readl(regs + DIEPTSIZ(index)),
  2692. readl(regs + DOEPTSIZ(index)));
  2693. seq_puts(seq, "\n");
  2694. seq_printf(seq, "mps %d\n", ep->ep.maxpacket);
  2695. seq_printf(seq, "total_data=%ld\n", ep->total_data);
  2696. seq_printf(seq, "request list (%p,%p):\n",
  2697. ep->queue.next, ep->queue.prev);
  2698. spin_lock_irqsave(&hsotg->lock, flags);
  2699. list_for_each_entry(req, &ep->queue, queue) {
  2700. if (--show_limit < 0) {
  2701. seq_puts(seq, "not showing more requests...\n");
  2702. break;
  2703. }
  2704. seq_printf(seq, "%c req %p: %d bytes @%p, ",
  2705. req == ep->req ? '*' : ' ',
  2706. req, req->req.length, req->req.buf);
  2707. seq_printf(seq, "%d done, res %d\n",
  2708. req->req.actual, req->req.status);
  2709. }
  2710. spin_unlock_irqrestore(&hsotg->lock, flags);
  2711. return 0;
  2712. }
  2713. static int ep_open(struct inode *inode, struct file *file)
  2714. {
  2715. return single_open(file, ep_show, inode->i_private);
  2716. }
  2717. static const struct file_operations ep_fops = {
  2718. .owner = THIS_MODULE,
  2719. .open = ep_open,
  2720. .read = seq_read,
  2721. .llseek = seq_lseek,
  2722. .release = single_release,
  2723. };
  2724. /**
  2725. * s3c_hsotg_create_debug - create debugfs directory and files
  2726. * @hsotg: The driver state
  2727. *
  2728. * Create the debugfs files to allow the user to get information
  2729. * about the state of the system. The directory name is created
  2730. * with the same name as the device itself, in case we end up
  2731. * with multiple blocks in future systems.
  2732. */
  2733. static void s3c_hsotg_create_debug(struct s3c_hsotg *hsotg)
  2734. {
  2735. struct dentry *root;
  2736. unsigned epidx;
  2737. root = debugfs_create_dir(dev_name(hsotg->dev), NULL);
  2738. hsotg->debug_root = root;
  2739. if (IS_ERR(root)) {
  2740. dev_err(hsotg->dev, "cannot create debug root\n");
  2741. return;
  2742. }
  2743. /* create general state file */
  2744. hsotg->debug_file = debugfs_create_file("state", 0444, root,
  2745. hsotg, &state_fops);
  2746. if (IS_ERR(hsotg->debug_file))
  2747. dev_err(hsotg->dev, "%s: failed to create state\n", __func__);
  2748. hsotg->debug_fifo = debugfs_create_file("fifo", 0444, root,
  2749. hsotg, &fifo_fops);
  2750. if (IS_ERR(hsotg->debug_fifo))
  2751. dev_err(hsotg->dev, "%s: failed to create fifo\n", __func__);
  2752. /* create one file for each endpoint */
  2753. for (epidx = 0; epidx < hsotg->num_of_eps; epidx++) {
  2754. struct s3c_hsotg_ep *ep = &hsotg->eps[epidx];
  2755. ep->debugfs = debugfs_create_file(ep->name, 0444,
  2756. root, ep, &ep_fops);
  2757. if (IS_ERR(ep->debugfs))
  2758. dev_err(hsotg->dev, "failed to create %s debug file\n",
  2759. ep->name);
  2760. }
  2761. }
  2762. /**
  2763. * s3c_hsotg_delete_debug - cleanup debugfs entries
  2764. * @hsotg: The driver state
  2765. *
  2766. * Cleanup (remove) the debugfs files for use on module exit.
  2767. */
  2768. static void s3c_hsotg_delete_debug(struct s3c_hsotg *hsotg)
  2769. {
  2770. unsigned epidx;
  2771. for (epidx = 0; epidx < hsotg->num_of_eps; epidx++) {
  2772. struct s3c_hsotg_ep *ep = &hsotg->eps[epidx];
  2773. debugfs_remove(ep->debugfs);
  2774. }
  2775. debugfs_remove(hsotg->debug_file);
  2776. debugfs_remove(hsotg->debug_fifo);
  2777. debugfs_remove(hsotg->debug_root);
  2778. }
  2779. /**
  2780. * s3c_hsotg_probe - probe function for hsotg driver
  2781. * @pdev: The platform information for the driver
  2782. */
  2783. static int s3c_hsotg_probe(struct platform_device *pdev)
  2784. {
  2785. struct s3c_hsotg_plat *plat = dev_get_platdata(&pdev->dev);
  2786. struct phy *phy;
  2787. struct usb_phy *uphy;
  2788. struct device *dev = &pdev->dev;
  2789. struct s3c_hsotg_ep *eps;
  2790. struct s3c_hsotg *hsotg;
  2791. struct resource *res;
  2792. int epnum;
  2793. int ret;
  2794. int i;
  2795. hsotg = devm_kzalloc(&pdev->dev, sizeof(struct s3c_hsotg), GFP_KERNEL);
  2796. if (!hsotg)
  2797. return -ENOMEM;
  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(&pdev->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(&pdev->dev);
  2810. if (!plat) {
  2811. dev_err(&pdev->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->dev = dev;
  2828. hsotg->clk = devm_clk_get(&pdev->dev, "otg");
  2829. if (IS_ERR(hsotg->clk)) {
  2830. dev_err(dev, "cannot get otg clock\n");
  2831. return PTR_ERR(hsotg->clk);
  2832. }
  2833. platform_set_drvdata(pdev, hsotg);
  2834. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2835. hsotg->regs = devm_ioremap_resource(&pdev->dev, res);
  2836. if (IS_ERR(hsotg->regs)) {
  2837. ret = PTR_ERR(hsotg->regs);
  2838. goto err_clk;
  2839. }
  2840. ret = platform_get_irq(pdev, 0);
  2841. if (ret < 0) {
  2842. dev_err(dev, "cannot find IRQ\n");
  2843. goto err_clk;
  2844. }
  2845. spin_lock_init(&hsotg->lock);
  2846. hsotg->irq = ret;
  2847. dev_info(dev, "regs %p, irq %d\n", hsotg->regs, hsotg->irq);
  2848. hsotg->gadget.max_speed = USB_SPEED_HIGH;
  2849. hsotg->gadget.ops = &s3c_hsotg_gadget_ops;
  2850. hsotg->gadget.name = dev_name(dev);
  2851. /* reset the system */
  2852. clk_prepare_enable(hsotg->clk);
  2853. /* regulators */
  2854. for (i = 0; i < ARRAY_SIZE(hsotg->supplies); i++)
  2855. hsotg->supplies[i].supply = s3c_hsotg_supply_names[i];
  2856. ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(hsotg->supplies),
  2857. hsotg->supplies);
  2858. if (ret) {
  2859. dev_err(dev, "failed to request supplies: %d\n", ret);
  2860. goto err_clk;
  2861. }
  2862. ret = regulator_bulk_enable(ARRAY_SIZE(hsotg->supplies),
  2863. hsotg->supplies);
  2864. if (ret) {
  2865. dev_err(hsotg->dev, "failed to enable supplies: %d\n", ret);
  2866. goto err_supplies;
  2867. }
  2868. /* usb phy enable */
  2869. s3c_hsotg_phy_enable(hsotg);
  2870. s3c_hsotg_corereset(hsotg);
  2871. s3c_hsotg_hw_cfg(hsotg);
  2872. s3c_hsotg_init(hsotg);
  2873. ret = devm_request_irq(&pdev->dev, hsotg->irq, s3c_hsotg_irq, 0,
  2874. dev_name(dev), hsotg);
  2875. if (ret < 0) {
  2876. s3c_hsotg_phy_disable(hsotg);
  2877. clk_disable_unprepare(hsotg->clk);
  2878. regulator_bulk_disable(ARRAY_SIZE(hsotg->supplies),
  2879. hsotg->supplies);
  2880. dev_err(dev, "cannot claim IRQ\n");
  2881. goto err_clk;
  2882. }
  2883. /* hsotg->num_of_eps holds number of EPs other than ep0 */
  2884. if (hsotg->num_of_eps == 0) {
  2885. dev_err(dev, "wrong number of EPs (zero)\n");
  2886. ret = -EINVAL;
  2887. goto err_supplies;
  2888. }
  2889. eps = kcalloc(hsotg->num_of_eps + 1, sizeof(struct s3c_hsotg_ep),
  2890. GFP_KERNEL);
  2891. if (!eps) {
  2892. ret = -ENOMEM;
  2893. goto err_supplies;
  2894. }
  2895. hsotg->eps = eps;
  2896. /* setup endpoint information */
  2897. INIT_LIST_HEAD(&hsotg->gadget.ep_list);
  2898. hsotg->gadget.ep0 = &hsotg->eps[0].ep;
  2899. /* allocate EP0 request */
  2900. hsotg->ctrl_req = s3c_hsotg_ep_alloc_request(&hsotg->eps[0].ep,
  2901. GFP_KERNEL);
  2902. if (!hsotg->ctrl_req) {
  2903. dev_err(dev, "failed to allocate ctrl req\n");
  2904. ret = -ENOMEM;
  2905. goto err_ep_mem;
  2906. }
  2907. /* initialise the endpoints now the core has been initialised */
  2908. for (epnum = 0; epnum < hsotg->num_of_eps; epnum++)
  2909. s3c_hsotg_initep(hsotg, &hsotg->eps[epnum], epnum);
  2910. /* disable power and clock */
  2911. s3c_hsotg_phy_disable(hsotg);
  2912. ret = regulator_bulk_disable(ARRAY_SIZE(hsotg->supplies),
  2913. hsotg->supplies);
  2914. if (ret) {
  2915. dev_err(hsotg->dev, "failed to disable supplies: %d\n", ret);
  2916. goto err_ep_mem;
  2917. }
  2918. ret = usb_add_gadget_udc(&pdev->dev, &hsotg->gadget);
  2919. if (ret)
  2920. goto err_ep_mem;
  2921. s3c_hsotg_create_debug(hsotg);
  2922. s3c_hsotg_dump(hsotg);
  2923. return 0;
  2924. err_ep_mem:
  2925. kfree(eps);
  2926. err_supplies:
  2927. s3c_hsotg_phy_disable(hsotg);
  2928. err_clk:
  2929. clk_disable_unprepare(hsotg->clk);
  2930. return ret;
  2931. }
  2932. /**
  2933. * s3c_hsotg_remove - remove function for hsotg driver
  2934. * @pdev: The platform information for the driver
  2935. */
  2936. static int s3c_hsotg_remove(struct platform_device *pdev)
  2937. {
  2938. struct s3c_hsotg *hsotg = platform_get_drvdata(pdev);
  2939. usb_del_gadget_udc(&hsotg->gadget);
  2940. s3c_hsotg_delete_debug(hsotg);
  2941. if (hsotg->driver) {
  2942. /* should have been done already by driver model core */
  2943. usb_gadget_unregister_driver(hsotg->driver);
  2944. }
  2945. clk_disable_unprepare(hsotg->clk);
  2946. return 0;
  2947. }
  2948. static int s3c_hsotg_suspend(struct platform_device *pdev, pm_message_t state)
  2949. {
  2950. struct s3c_hsotg *hsotg = platform_get_drvdata(pdev);
  2951. unsigned long flags;
  2952. int ret = 0;
  2953. if (hsotg->driver)
  2954. dev_info(hsotg->dev, "suspending usb gadget %s\n",
  2955. hsotg->driver->driver.name);
  2956. spin_lock_irqsave(&hsotg->lock, flags);
  2957. s3c_hsotg_disconnect(hsotg);
  2958. s3c_hsotg_phy_disable(hsotg);
  2959. hsotg->gadget.speed = USB_SPEED_UNKNOWN;
  2960. spin_unlock_irqrestore(&hsotg->lock, flags);
  2961. if (hsotg->driver) {
  2962. int ep;
  2963. for (ep = 0; ep < hsotg->num_of_eps; ep++)
  2964. s3c_hsotg_ep_disable(&hsotg->eps[ep].ep);
  2965. ret = regulator_bulk_disable(ARRAY_SIZE(hsotg->supplies),
  2966. hsotg->supplies);
  2967. clk_disable(hsotg->clk);
  2968. }
  2969. return ret;
  2970. }
  2971. static int s3c_hsotg_resume(struct platform_device *pdev)
  2972. {
  2973. struct s3c_hsotg *hsotg = platform_get_drvdata(pdev);
  2974. unsigned long flags;
  2975. int ret = 0;
  2976. if (hsotg->driver) {
  2977. dev_info(hsotg->dev, "resuming usb gadget %s\n",
  2978. hsotg->driver->driver.name);
  2979. clk_enable(hsotg->clk);
  2980. ret = regulator_bulk_enable(ARRAY_SIZE(hsotg->supplies),
  2981. hsotg->supplies);
  2982. }
  2983. spin_lock_irqsave(&hsotg->lock, flags);
  2984. hsotg->last_rst = jiffies;
  2985. s3c_hsotg_phy_enable(hsotg);
  2986. s3c_hsotg_core_init(hsotg);
  2987. spin_unlock_irqrestore(&hsotg->lock, flags);
  2988. return ret;
  2989. }
  2990. #ifdef CONFIG_OF
  2991. static const struct of_device_id s3c_hsotg_of_ids[] = {
  2992. { .compatible = "samsung,s3c6400-hsotg", },
  2993. { .compatible = "snps,dwc2", },
  2994. { /* sentinel */ }
  2995. };
  2996. MODULE_DEVICE_TABLE(of, s3c_hsotg_of_ids);
  2997. #endif
  2998. static struct platform_driver s3c_hsotg_driver = {
  2999. .driver = {
  3000. .name = "s3c-hsotg",
  3001. .owner = THIS_MODULE,
  3002. .of_match_table = of_match_ptr(s3c_hsotg_of_ids),
  3003. },
  3004. .probe = s3c_hsotg_probe,
  3005. .remove = s3c_hsotg_remove,
  3006. .suspend = s3c_hsotg_suspend,
  3007. .resume = s3c_hsotg_resume,
  3008. };
  3009. module_platform_driver(s3c_hsotg_driver);
  3010. MODULE_DESCRIPTION("Samsung S3C USB High-speed/OtG device");
  3011. MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
  3012. MODULE_LICENSE("GPL");
  3013. MODULE_ALIAS("platform:s3c-hsotg");