gadget.c 93 KB

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