gadget.c 85 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323
  1. /**
  2. * gadget.c - DesignWare USB3 DRD Controller Gadget Framework Link
  3. *
  4. * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
  5. *
  6. * Authors: Felipe Balbi <balbi@ti.com>,
  7. * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions, and the following disclaimer,
  14. * without modification.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * 3. The names of the above-listed copyright holders may not be used
  19. * to endorse or promote products derived from this software without
  20. * specific prior written permission.
  21. *
  22. * ALTERNATIVELY, this software may be distributed under the terms of the
  23. * GNU General Public License ("GPL") version 2, as published by the Free
  24. * Software Foundation.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  27. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  28. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  29. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  30. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  31. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  32. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  33. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  34. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  35. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  36. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  37. */
  38. #if defined(CONFIG_SEC_H_PROJECT) || defined(CONFIG_SEC_F_PROJECT) || defined(CONFIG_SEC_K_PROJECT)
  39. #include <linux/module.h>
  40. #endif
  41. #include <linux/kernel.h>
  42. #include <linux/delay.h>
  43. #include <linux/slab.h>
  44. #include <linux/spinlock.h>
  45. #include <linux/platform_device.h>
  46. #include <linux/pm_runtime.h>
  47. #include <linux/ratelimit.h>
  48. #include <linux/interrupt.h>
  49. #include <linux/io.h>
  50. #include <linux/list.h>
  51. #include <linux/dma-mapping.h>
  52. #include <linux/usb/ch9.h>
  53. #include <linux/usb/gadget.h>
  54. #include <linux/usb/otg.h>
  55. #include "core.h"
  56. #include "gadget.h"
  57. #include "debug.h"
  58. #include "io.h"
  59. #if defined(CONFIG_SEC_H_PROJECT) || defined(CONFIG_SEC_F_PROJECT) || defined(CONFIG_SEC_K_PROJECT)
  60. #define EP0_HS_MPS 64
  61. #define EP0_SS_MPS 512
  62. #define WORK_CANCEL(udc) \
  63. cancel_work_sync(&udc->reconnect_work);
  64. #define WORK_SCHEDULE(udc) \
  65. schedule_work(&udc->reconnect_work);
  66. #else
  67. #define WORK_CANCEL(udc)
  68. #define WORK_SCHEDULE(udc)
  69. #endif
  70. static void dwc3_gadget_usb2_phy_suspend(struct dwc3 *dwc, int suspend);
  71. static void dwc3_gadget_usb3_phy_suspend(struct dwc3 *dwc, int suspend);
  72. /**
  73. * dwc3_gadget_set_test_mode - Enables USB2 Test Modes
  74. * @dwc: pointer to our context structure
  75. * @mode: the mode to set (J, K SE0 NAK, Force Enable)
  76. *
  77. * Caller should take care of locking. This function will
  78. * return 0 on success or -EINVAL if wrong Test Selector
  79. * is passed
  80. */
  81. int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode)
  82. {
  83. u32 reg;
  84. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  85. reg &= ~DWC3_DCTL_TSTCTRL_MASK;
  86. switch (mode) {
  87. case TEST_J:
  88. case TEST_K:
  89. case TEST_SE0_NAK:
  90. case TEST_PACKET:
  91. case TEST_FORCE_EN:
  92. reg |= mode << 1;
  93. break;
  94. default:
  95. return -EINVAL;
  96. }
  97. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  98. return 0;
  99. }
  100. /**
  101. * dwc3_gadget_set_link_state - Sets USB Link to a particular State
  102. * @dwc: pointer to our context structure
  103. * @state: the state to put link into
  104. *
  105. * Caller should take care of locking. This function will
  106. * return 0 on success or -ETIMEDOUT.
  107. */
  108. int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state)
  109. {
  110. int retries = 10000;
  111. u32 reg;
  112. /*
  113. * Wait until device controller is ready. Only applies to 1.94a and
  114. * later RTL.
  115. */
  116. if (dwc->revision >= DWC3_REVISION_194A) {
  117. while (--retries) {
  118. reg = dwc3_readl(dwc->regs, DWC3_DSTS);
  119. if (reg & DWC3_DSTS_DCNRD)
  120. udelay(5);
  121. else
  122. break;
  123. }
  124. if (retries <= 0)
  125. return -ETIMEDOUT;
  126. }
  127. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  128. reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
  129. /* set requested state */
  130. reg |= DWC3_DCTL_ULSTCHNGREQ(state);
  131. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  132. /*
  133. * The following code is racy when called from dwc3_gadget_wakeup,
  134. * and is not needed, at least on newer versions
  135. */
  136. if (dwc->revision >= DWC3_REVISION_194A)
  137. return 0;
  138. /* wait for a change in DSTS */
  139. retries = 10000;
  140. while (--retries) {
  141. reg = dwc3_readl(dwc->regs, DWC3_DSTS);
  142. if (DWC3_DSTS_USBLNKST(reg) == state)
  143. return 0;
  144. udelay(5);
  145. }
  146. dev_vdbg(dwc->dev, "link state change request timed out\n");
  147. return -ETIMEDOUT;
  148. }
  149. /**
  150. * dwc3_gadget_resize_tx_fifos - reallocate fifo spaces for current use-case
  151. * @dwc: pointer to our context structure
  152. *
  153. * This function will a best effort FIFO allocation in order
  154. * to improve FIFO usage and throughput, while still allowing
  155. * us to enable as many endpoints as possible.
  156. *
  157. * Keep in mind that this operation will be highly dependent
  158. * on the configured size for RAM1 - which contains TxFifo -,
  159. * the amount of endpoints enabled on coreConsultant tool, and
  160. * the width of the Master Bus.
  161. *
  162. * In the ideal world, we would always be able to satisfy the
  163. * following equation:
  164. *
  165. * ((512 + 2 * MDWIDTH-Bytes) + (Number of IN Endpoints - 1) * \
  166. * (3 * (1024 + MDWIDTH-Bytes) + MDWIDTH-Bytes)) / MDWIDTH-Bytes
  167. *
  168. * Unfortunately, due to many variables that's not always the case.
  169. */
  170. int dwc3_gadget_resize_tx_fifos(struct dwc3 *dwc)
  171. {
  172. int last_fifo_depth = 0;
  173. int ram1_depth;
  174. int fifo_size;
  175. int mdwidth;
  176. int num;
  177. if (!dwc->needs_fifo_resize)
  178. return 0;
  179. ram1_depth = DWC3_RAM1_DEPTH(dwc->hwparams.hwparams7);
  180. mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
  181. /* MDWIDTH is represented in bits, we need it in bytes */
  182. mdwidth >>= 3;
  183. /*
  184. * FIXME For now we will only allocate 1 wMaxPacketSize space
  185. * for each enabled endpoint, later patches will come to
  186. * improve this algorithm so that we better use the internal
  187. * FIFO space. Also consider the case where TxFIFO RAM space
  188. * may change dynamically based on the USB configuration.
  189. */
  190. for (num = 0; num < dwc->num_in_eps; num++) {
  191. struct dwc3_ep *dep = dwc->eps[(num << 1) | 1];
  192. int mult = 1;
  193. int tmp;
  194. if (!(dep->flags & DWC3_EP_ENABLED))
  195. continue;
  196. if (((dep->endpoint.maxburst > 1) &&
  197. usb_endpoint_xfer_bulk(dep->endpoint.desc))
  198. || usb_endpoint_xfer_isoc(dep->endpoint.desc))
  199. mult = 3;
  200. /*
  201. * REVISIT: the following assumes we will always have enough
  202. * space available on the FIFO RAM for all possible use cases.
  203. * Make sure that's true somehow and change FIFO allocation
  204. * accordingly.
  205. *
  206. * If we have Bulk (burst only) or Isochronous endpoints, we
  207. * want them to be able to be very, very fast. So we're giving
  208. * those endpoints a fifo_size which is enough for 3 full
  209. * packets
  210. */
  211. tmp = mult * (dep->endpoint.maxpacket + mdwidth);
  212. if (dwc->tx_fifo_size &&
  213. (usb_endpoint_xfer_bulk(dep->endpoint.desc)
  214. || usb_endpoint_xfer_isoc(dep->endpoint.desc))) {
  215. /*
  216. * Allocate 3KB fifo size for bulk and isochronous TX
  217. * endpoints irrespective of speed if tx_fifo is not
  218. * reduced. Otherwise allocate 1KB for endpoints in HS
  219. * mode and for non burst endpoints in SS mode. For
  220. * interrupt ep, allocate fifo size of ep maxpacket.
  221. */
  222. if (!dwc->tx_fifo_reduced)
  223. tmp = 3 * (1024 + mdwidth);
  224. else
  225. tmp = mult * (1024 + mdwidth);
  226. }
  227. tmp += mdwidth;
  228. fifo_size = DIV_ROUND_UP(tmp, mdwidth);
  229. fifo_size |= (last_fifo_depth << 16);
  230. dev_vdbg(dwc->dev, "%s: Fifo Addr %04x Size %d\n",
  231. dep->name, last_fifo_depth, fifo_size & 0xffff);
  232. last_fifo_depth += (fifo_size & 0xffff);
  233. if (dwc->tx_fifo_size &&
  234. (last_fifo_depth >= dwc->tx_fifo_size)) {
  235. /*
  236. * Fifo size allocated exceeded available RAM size.
  237. * Hence return error.
  238. */
  239. dev_err(dwc->dev, "Fifosize(%d) > available RAM(%d)\n",
  240. last_fifo_depth, dwc->tx_fifo_size);
  241. return -ENOMEM;
  242. }
  243. dwc3_writel(dwc->regs, DWC3_GTXFIFOSIZ(num), fifo_size);
  244. }
  245. return 0;
  246. }
  247. void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
  248. int status)
  249. {
  250. struct dwc3 *dwc = dep->dwc;
  251. if (req->queued) {
  252. req->queued = false;
  253. if (req->request.num_mapped_sgs)
  254. dep->busy_slot += req->request.num_mapped_sgs;
  255. else
  256. dep->busy_slot++;
  257. /*
  258. * Skip LINK TRB. We can't use req->trb and check for
  259. * DWC3_TRBCTL_LINK_TRB because it points the TRB we just
  260. * completed (not the LINK TRB).
  261. */
  262. if (((dep->busy_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
  263. usb_endpoint_xfer_isoc(dep->endpoint.desc))
  264. dep->busy_slot++;
  265. if (req->request.zero && req->ztrb) {
  266. dep->busy_slot++;
  267. req->ztrb = NULL;
  268. if (((dep->busy_slot & DWC3_TRB_MASK) ==
  269. DWC3_TRB_NUM - 1) &&
  270. usb_endpoint_xfer_isoc(dep->endpoint.desc))
  271. dep->busy_slot++;
  272. }
  273. }
  274. list_del(&req->list);
  275. req->trb = NULL;
  276. if (req->request.status == -EINPROGRESS)
  277. req->request.status = status;
  278. if (dwc->ep0_bounced && dep->number == 0)
  279. dwc->ep0_bounced = false;
  280. else
  281. usb_gadget_unmap_request(&dwc->gadget, &req->request,
  282. req->direction);
  283. dev_dbg(dwc->dev, "request %pK from %s completed %d/%d ===> %d\n",
  284. req, dep->name, req->request.actual,
  285. req->request.length, status);
  286. dbg_done(dep->number, req->request.actual, req->request.status);
  287. if (req->request.complete) {
  288. spin_unlock(&dwc->lock);
  289. req->request.complete(&dep->endpoint, &req->request);
  290. spin_lock(&dwc->lock);
  291. }
  292. }
  293. static const char *dwc3_gadget_ep_cmd_string(u8 cmd)
  294. {
  295. switch (cmd) {
  296. case DWC3_DEPCMD_DEPSTARTCFG:
  297. return "Start New Configuration";
  298. case DWC3_DEPCMD_ENDTRANSFER:
  299. return "End Transfer";
  300. case DWC3_DEPCMD_UPDATETRANSFER:
  301. return "Update Transfer";
  302. case DWC3_DEPCMD_STARTTRANSFER:
  303. return "Start Transfer";
  304. case DWC3_DEPCMD_CLEARSTALL:
  305. return "Clear Stall";
  306. case DWC3_DEPCMD_SETSTALL:
  307. return "Set Stall";
  308. case DWC3_DEPCMD_GETEPSTATE:
  309. return "Get Endpoint State";
  310. case DWC3_DEPCMD_SETTRANSFRESOURCE:
  311. return "Set Endpoint Transfer Resource";
  312. case DWC3_DEPCMD_SETEPCONFIG:
  313. return "Set Endpoint Configuration";
  314. default:
  315. return "UNKNOWN command";
  316. }
  317. }
  318. int dwc3_send_gadget_generic_command(struct dwc3 *dwc, int cmd, u32 param)
  319. {
  320. u32 timeout = 500;
  321. u32 reg;
  322. bool hsphy_suspend_enabled;
  323. int ret;
  324. /* Commands to controller will work only if PHY is not suspended */
  325. hsphy_suspend_enabled = (dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0)) &
  326. DWC3_GUSB2PHYCFG_SUSPHY);
  327. /* Disable suspend of the USB2 PHY */
  328. if (hsphy_suspend_enabled)
  329. dwc3_gadget_usb2_phy_suspend(dwc, false);
  330. dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
  331. dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
  332. do {
  333. reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
  334. if (!(reg & DWC3_DGCMD_CMDACT)) {
  335. dev_vdbg(dwc->dev, "Command Complete --> %d\n",
  336. DWC3_DGCMD_STATUS(reg));
  337. ret = 0;
  338. break;
  339. }
  340. /*
  341. * We can't sleep here, because it's also called from
  342. * interrupt context.
  343. */
  344. timeout--;
  345. if (!timeout) {
  346. ret = -ETIMEDOUT;
  347. break;
  348. }
  349. udelay(1);
  350. } while (1);
  351. /* Enable suspend of the USB2 PHY */
  352. if (hsphy_suspend_enabled)
  353. dwc3_gadget_usb2_phy_suspend(dwc, true);
  354. return ret;
  355. }
  356. int dwc3_send_gadget_ep_cmd(struct dwc3 *dwc, unsigned ep,
  357. unsigned cmd, struct dwc3_gadget_ep_cmd_params *params)
  358. {
  359. struct dwc3_ep *dep = dwc->eps[ep];
  360. u32 timeout = 500;
  361. u32 reg;
  362. bool hsphy_suspend_enabled;
  363. int ret;
  364. dev_vdbg(dwc->dev, "%s: cmd '%s' params %08x %08x %08x\n",
  365. dep->name,
  366. dwc3_gadget_ep_cmd_string(cmd), params->param0,
  367. params->param1, params->param2);
  368. /* Commands to controller will work only if PHY is not suspended */
  369. hsphy_suspend_enabled = (dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0)) &
  370. DWC3_GUSB2PHYCFG_SUSPHY);
  371. /* Disable suspend of the USB2 PHY */
  372. if (hsphy_suspend_enabled)
  373. dwc3_gadget_usb2_phy_suspend(dwc, false);
  374. dwc3_writel(dwc->regs, DWC3_DEPCMDPAR0(ep), params->param0);
  375. dwc3_writel(dwc->regs, DWC3_DEPCMDPAR1(ep), params->param1);
  376. dwc3_writel(dwc->regs, DWC3_DEPCMDPAR2(ep), params->param2);
  377. dwc3_writel(dwc->regs, DWC3_DEPCMD(ep), cmd | DWC3_DEPCMD_CMDACT);
  378. do {
  379. reg = dwc3_readl(dwc->regs, DWC3_DEPCMD(ep));
  380. if (!(reg & DWC3_DEPCMD_CMDACT)) {
  381. dev_vdbg(dwc->dev, "Command Complete --> %d\n",
  382. DWC3_DEPCMD_STATUS(reg));
  383. /* SW issues START TRANSFER command to isochronous ep
  384. * with future frame interval. If future interval time
  385. * has already passed when core recieves command, core
  386. * will respond with an error(bit13 in Command complete
  387. * event. Hence return error in this case.
  388. */
  389. if (reg & 0x2000)
  390. ret = -EAGAIN;
  391. else if (DWC3_DEPCMD_STATUS(reg))
  392. ret = -EINVAL;
  393. else
  394. ret = 0;
  395. break;
  396. }
  397. /*
  398. * We can't sleep here, because it is also called from
  399. * interrupt context.
  400. */
  401. timeout--;
  402. if (!timeout) {
  403. dev_err(dwc->dev, "%s command timeout for %s\n",
  404. dwc3_gadget_ep_cmd_string(cmd),
  405. dep->name);
  406. ret = -ETIMEDOUT;
  407. break;
  408. }
  409. udelay(1);
  410. } while (1);
  411. /* Enable suspend of the USB2 PHY */
  412. if (hsphy_suspend_enabled)
  413. dwc3_gadget_usb2_phy_suspend(dwc, true);
  414. return ret;
  415. }
  416. dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
  417. struct dwc3_trb *trb)
  418. {
  419. u32 offset = (char *) trb - (char *) dep->trb_pool;
  420. return dep->trb_pool_dma + offset;
  421. }
  422. static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
  423. {
  424. struct dwc3 *dwc = dep->dwc;
  425. if (dep->trb_pool)
  426. return 0;
  427. if (dep->number == 0 || dep->number == 1)
  428. return 0;
  429. dep->trb_pool = dma_alloc_coherent(dwc->dev,
  430. sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
  431. &dep->trb_pool_dma, GFP_KERNEL);
  432. if (!dep->trb_pool) {
  433. dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
  434. dep->name);
  435. return -ENOMEM;
  436. }
  437. return 0;
  438. }
  439. static void dwc3_free_trb_pool(struct dwc3_ep *dep)
  440. {
  441. struct dwc3 *dwc = dep->dwc;
  442. dma_free_coherent(dwc->dev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
  443. dep->trb_pool, dep->trb_pool_dma);
  444. dep->trb_pool = NULL;
  445. dep->trb_pool_dma = 0;
  446. }
  447. static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep);
  448. /**
  449. * dwc3_gadget_start_config - Configure EP resources
  450. * @dwc: pointer to our controller context structure
  451. * @dep: endpoint that is being enabled
  452. *
  453. * The assignment of transfer resources cannot perfectly follow the
  454. * data book due to the fact that the controller driver does not have
  455. * all knowledge of the configuration in advance. It is given this
  456. * information piecemeal by the composite gadget framework after every
  457. * SET_CONFIGURATION and SET_INTERFACE. Trying to follow the databook
  458. * programming model in this scenario can cause errors. For two
  459. * reasons:
  460. *
  461. * 1) The databook says to do DEPSTARTCFG for every SET_CONFIGURATION
  462. * and SET_INTERFACE (8.1.5). This is incorrect in the scenario of
  463. * multiple interfaces.
  464. *
  465. * 2) The databook does not mention doing more DEPXFERCFG for new
  466. * endpoint on alt setting (8.1.6).
  467. *
  468. * The following simplified method is used instead:
  469. *
  470. * All hardware endpoints can be assigned a transfer resource and this
  471. * setting will stay persistent until either a core reset or
  472. * hibernation. So whenever we do a DEPSTARTCFG(0) we can go ahead and
  473. * do DEPXFERCFG for every hardware endpoint as well. We are
  474. * guaranteed that there are as many transfer resources as endpoints.
  475. *
  476. * This function is called for each endpoint when it is being enabled
  477. * but is triggered only when called for EP0-out, which always happens
  478. * first, and which should only happen in one of the above conditions.
  479. */
  480. static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
  481. {
  482. struct dwc3_gadget_ep_cmd_params params;
  483. u32 cmd;
  484. int i;
  485. int ret;
  486. if (dep->number)
  487. return 0;
  488. memset(&params, 0x00, sizeof(params));
  489. cmd = DWC3_DEPCMD_DEPSTARTCFG;
  490. ret = dwc3_send_gadget_ep_cmd(dwc, 0, cmd, &params);
  491. if (ret)
  492. return ret;
  493. for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
  494. struct dwc3_ep *dep = dwc->eps[i];
  495. if (!dep)
  496. continue;
  497. ret = dwc3_gadget_set_xfer_resource(dwc, dep);
  498. if (ret)
  499. return ret;
  500. }
  501. return 0;
  502. }
  503. static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
  504. const struct usb_endpoint_descriptor *desc,
  505. const struct usb_ss_ep_comp_descriptor *comp_desc,
  506. bool ignore)
  507. {
  508. struct dwc3_gadget_ep_cmd_params params;
  509. memset(&params, 0x00, sizeof(params));
  510. params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
  511. | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
  512. /* Burst size is only needed in SuperSpeed mode */
  513. if (dwc->gadget.speed == USB_SPEED_SUPER) {
  514. u32 burst = dep->endpoint.maxburst - 1;
  515. params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst);
  516. }
  517. if (ignore)
  518. params.param0 |= DWC3_DEPCFG_IGN_SEQ_NUM;
  519. params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN
  520. | DWC3_DEPCFG_XFER_NOT_READY_EN;
  521. if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
  522. params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
  523. | DWC3_DEPCFG_STREAM_EVENT_EN;
  524. dep->stream_capable = true;
  525. }
  526. if (!usb_endpoint_xfer_control(desc))
  527. params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
  528. /*
  529. * We are doing 1:1 mapping for endpoints, meaning
  530. * Physical Endpoints 2 maps to Logical Endpoint 2 and
  531. * so on. We consider the direction bit as part of the physical
  532. * endpoint number. So USB endpoint 0x81 is 0x03.
  533. */
  534. params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
  535. /*
  536. * We must use the lower 16 TX FIFOs even though
  537. * HW might have more
  538. */
  539. if (dep->direction)
  540. params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
  541. if (desc->bInterval) {
  542. params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
  543. dep->interval = 1 << (desc->bInterval - 1);
  544. }
  545. return dwc3_send_gadget_ep_cmd(dwc, dep->number,
  546. DWC3_DEPCMD_SETEPCONFIG, &params);
  547. }
  548. static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
  549. {
  550. struct dwc3_gadget_ep_cmd_params params;
  551. memset(&params, 0x00, sizeof(params));
  552. params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
  553. return dwc3_send_gadget_ep_cmd(dwc, dep->number,
  554. DWC3_DEPCMD_SETTRANSFRESOURCE, &params);
  555. }
  556. /**
  557. * __dwc3_gadget_ep_enable - Initializes a HW endpoint
  558. * @dep: endpoint to be initialized
  559. * @desc: USB Endpoint Descriptor
  560. *
  561. * Caller should take care of locking
  562. */
  563. static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
  564. const struct usb_endpoint_descriptor *desc,
  565. const struct usb_ss_ep_comp_descriptor *comp_desc,
  566. bool ignore)
  567. {
  568. struct dwc3 *dwc = dep->dwc;
  569. u32 reg;
  570. int ret = -ENOMEM;
  571. if (!(dep->flags & DWC3_EP_ENABLED)) {
  572. ret = dwc3_gadget_start_config(dwc, dep);
  573. if (ret)
  574. return ret;
  575. }
  576. ret = dwc3_gadget_set_ep_config(dwc, dep, desc, comp_desc, ignore);
  577. if (ret)
  578. return ret;
  579. if (!(dep->flags & DWC3_EP_ENABLED)) {
  580. struct dwc3_trb *trb_st_hw;
  581. struct dwc3_trb *trb_link;
  582. dep->endpoint.desc = desc;
  583. dep->comp_desc = comp_desc;
  584. dep->type = usb_endpoint_type(desc);
  585. dep->flags |= DWC3_EP_ENABLED;
  586. reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
  587. reg |= DWC3_DALEPENA_EP(dep->number);
  588. dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
  589. if (!usb_endpoint_xfer_isoc(desc))
  590. return 0;
  591. /* Link TRB for ISOC. The HWO bit is never reset */
  592. trb_st_hw = &dep->trb_pool[0];
  593. trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
  594. memset(trb_link, 0, sizeof(*trb_link));
  595. trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
  596. trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
  597. trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
  598. trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
  599. }
  600. return 0;
  601. }
  602. static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum);
  603. static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
  604. {
  605. struct dwc3_request *req;
  606. if (!list_empty(&dep->req_queued)) {
  607. dwc3_stop_active_transfer(dwc, dep->number);
  608. /* - giveback all requests to gadget driver */
  609. while (!list_empty(&dep->req_queued)) {
  610. req = next_request(&dep->req_queued);
  611. dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
  612. }
  613. }
  614. while (!list_empty(&dep->request_list)) {
  615. req = next_request(&dep->request_list);
  616. dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
  617. }
  618. }
  619. /**
  620. * __dwc3_gadget_ep_disable - Disables a HW endpoint
  621. * @dep: the endpoint to disable
  622. *
  623. * This function also removes requests which are currently processed ny the
  624. * hardware and those which are not yet scheduled.
  625. * Caller should take care of locking.
  626. */
  627. static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
  628. {
  629. struct dwc3 *dwc = dep->dwc;
  630. u32 reg;
  631. dwc3_remove_requests(dwc, dep);
  632. /* make sure HW endpoint isn't stalled */
  633. if (dep->flags & DWC3_EP_STALL)
  634. __dwc3_gadget_ep_set_halt(dep, 0, false);
  635. reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
  636. reg &= ~DWC3_DALEPENA_EP(dep->number);
  637. dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
  638. dep->stream_capable = false;
  639. dep->endpoint.desc = NULL;
  640. dep->comp_desc = NULL;
  641. dep->type = 0;
  642. dep->flags = 0;
  643. return 0;
  644. }
  645. /* -------------------------------------------------------------------------- */
  646. static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
  647. const struct usb_endpoint_descriptor *desc)
  648. {
  649. return -EINVAL;
  650. }
  651. static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
  652. {
  653. return -EINVAL;
  654. }
  655. /* -------------------------------------------------------------------------- */
  656. static int dwc3_gadget_ep_enable(struct usb_ep *ep,
  657. const struct usb_endpoint_descriptor *desc)
  658. {
  659. struct dwc3_ep *dep;
  660. struct dwc3 *dwc;
  661. unsigned long flags;
  662. int ret;
  663. if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
  664. pr_debug("dwc3: invalid parameters\n");
  665. return -EINVAL;
  666. }
  667. if (!desc->wMaxPacketSize) {
  668. pr_debug("dwc3: missing wMaxPacketSize\n");
  669. return -EINVAL;
  670. }
  671. dep = to_dwc3_ep(ep);
  672. dwc = dep->dwc;
  673. if (dep->flags & DWC3_EP_ENABLED) {
  674. dev_WARN_ONCE(dwc->dev, true, "%s is already enabled\n",
  675. dep->name);
  676. return 0;
  677. }
  678. switch (usb_endpoint_type(desc)) {
  679. case USB_ENDPOINT_XFER_CONTROL:
  680. strlcat(dep->name, "-control", sizeof(dep->name));
  681. break;
  682. case USB_ENDPOINT_XFER_ISOC:
  683. strlcat(dep->name, "-isoc", sizeof(dep->name));
  684. break;
  685. case USB_ENDPOINT_XFER_BULK:
  686. strlcat(dep->name, "-bulk", sizeof(dep->name));
  687. break;
  688. case USB_ENDPOINT_XFER_INT:
  689. strlcat(dep->name, "-int", sizeof(dep->name));
  690. break;
  691. default:
  692. dev_err(dwc->dev, "invalid endpoint transfer type\n");
  693. }
  694. dev_vdbg(dwc->dev, "Enabling %s\n", dep->name);
  695. spin_lock_irqsave(&dwc->lock, flags);
  696. ret = __dwc3_gadget_ep_enable(dep, desc, ep->comp_desc, false);
  697. dbg_event(dep->number, "ENABLE", ret);
  698. spin_unlock_irqrestore(&dwc->lock, flags);
  699. return ret;
  700. }
  701. static int dwc3_gadget_ep_disable(struct usb_ep *ep)
  702. {
  703. struct dwc3_ep *dep;
  704. struct dwc3 *dwc;
  705. unsigned long flags;
  706. int ret;
  707. if (!ep) {
  708. pr_debug("dwc3: invalid parameters\n");
  709. return -EINVAL;
  710. }
  711. dep = to_dwc3_ep(ep);
  712. dwc = dep->dwc;
  713. if (!(dep->flags & DWC3_EP_ENABLED)) {
  714. dev_WARN_ONCE(dwc->dev, true, "%s is already disabled\n",
  715. dep->name);
  716. return 0;
  717. }
  718. snprintf(dep->name, sizeof(dep->name), "ep%d%s",
  719. dep->number >> 1,
  720. (dep->number & 1) ? "in" : "out");
  721. spin_lock_irqsave(&dwc->lock, flags);
  722. ret = __dwc3_gadget_ep_disable(dep);
  723. dbg_event(dep->number, "DISABLE", ret);
  724. spin_unlock_irqrestore(&dwc->lock, flags);
  725. return ret;
  726. }
  727. static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
  728. gfp_t gfp_flags)
  729. {
  730. struct dwc3_request *req;
  731. struct dwc3_ep *dep = to_dwc3_ep(ep);
  732. struct dwc3 *dwc = dep->dwc;
  733. req = kzalloc(sizeof(*req), gfp_flags);
  734. if (!req) {
  735. dev_err(dwc->dev, "not enough memory\n");
  736. return NULL;
  737. }
  738. req->epnum = dep->number;
  739. req->dep = dep;
  740. return &req->request;
  741. }
  742. static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
  743. struct usb_request *request)
  744. {
  745. struct dwc3_request *req = to_dwc3_request(request);
  746. kfree(req);
  747. }
  748. /**
  749. * dwc3_prepare_one_trb - setup one TRB from one request
  750. * @dep: endpoint for which this request is prepared
  751. * @req: dwc3_request pointer
  752. */
  753. static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
  754. struct dwc3_request *req, dma_addr_t dma,
  755. unsigned length, unsigned last, unsigned chain)
  756. {
  757. struct dwc3 *dwc = dep->dwc;
  758. struct dwc3_trb *trb;
  759. unsigned int cur_slot;
  760. dev_vdbg(dwc->dev, "%s: req %pK dma %08llx length %d%s%s\n",
  761. dep->name, req, (unsigned long long) dma,
  762. length, last ? " last" : "",
  763. chain ? " chain" : "");
  764. trb = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK];
  765. cur_slot = dep->free_slot;
  766. dep->free_slot++;
  767. /* Skip the LINK-TRB on ISOC */
  768. if (((dep->free_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
  769. usb_endpoint_xfer_isoc(dep->endpoint.desc))
  770. dep->free_slot++;
  771. if (!req->trb) {
  772. dwc3_gadget_move_request_queued(req);
  773. req->trb = trb;
  774. req->trb_dma = dwc3_trb_dma_offset(dep, trb);
  775. }
  776. update_trb:
  777. trb->size = DWC3_TRB_SIZE_LENGTH(length);
  778. trb->bpl = lower_32_bits(dma);
  779. trb->bph = upper_32_bits(dma);
  780. switch (usb_endpoint_type(dep->endpoint.desc)) {
  781. case USB_ENDPOINT_XFER_CONTROL:
  782. trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
  783. break;
  784. case USB_ENDPOINT_XFER_ISOC:
  785. trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
  786. if (!req->request.no_interrupt)
  787. trb->ctrl |= DWC3_TRB_CTRL_IOC;
  788. break;
  789. case USB_ENDPOINT_XFER_BULK:
  790. case USB_ENDPOINT_XFER_INT:
  791. trb->ctrl = DWC3_TRBCTL_NORMAL;
  792. break;
  793. default:
  794. /*
  795. * This is only possible with faulty memory because we
  796. * checked it already :)
  797. */
  798. BUG();
  799. }
  800. if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
  801. trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
  802. trb->ctrl |= DWC3_TRB_CTRL_CSP;
  803. } else {
  804. if (chain)
  805. trb->ctrl |= DWC3_TRB_CTRL_CHN;
  806. }
  807. if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
  808. trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(req->request.stream_id);
  809. trb->ctrl |= DWC3_TRB_CTRL_HWO;
  810. if (req->request.zero && length &&
  811. (length % usb_endpoint_maxp(dep->endpoint.desc) == 0)) {
  812. trb = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK];
  813. dep->free_slot++;
  814. /* Skip the LINK-TRB on ISOC */
  815. if (((dep->free_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
  816. usb_endpoint_xfer_isoc(dep->endpoint.desc))
  817. dep->free_slot++;
  818. req->ztrb = trb;
  819. length = 0;
  820. goto update_trb;
  821. }
  822. if (!usb_endpoint_xfer_isoc(dep->endpoint.desc) && last)
  823. trb->ctrl |= DWC3_TRB_CTRL_LST;
  824. }
  825. /*
  826. * dwc3_prepare_trbs - setup TRBs from requests
  827. * @dep: endpoint for which requests are being prepared
  828. * @starting: true if the endpoint is idle and no requests are queued.
  829. *
  830. * The function goes through the requests list and sets up TRBs for the
  831. * transfers. The function returns once there are no more TRBs available or
  832. * it runs out of requests.
  833. */
  834. static void dwc3_prepare_trbs(struct dwc3_ep *dep, bool starting)
  835. {
  836. struct dwc3_request *req, *n;
  837. u32 trbs_left;
  838. u32 max;
  839. unsigned int last_one = 0;
  840. BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
  841. /* the first request must not be queued */
  842. trbs_left = (dep->busy_slot - dep->free_slot) & DWC3_TRB_MASK;
  843. /* Can't wrap around on a non-isoc EP since there's no link TRB */
  844. if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
  845. max = DWC3_TRB_NUM - (dep->free_slot & DWC3_TRB_MASK);
  846. if (trbs_left > max)
  847. trbs_left = max;
  848. }
  849. /*
  850. * If busy & slot are equal than it is either full or empty. If we are
  851. * starting to process requests then we are empty. Otherwise we are
  852. * full and don't do anything
  853. */
  854. if (!trbs_left) {
  855. if (!starting)
  856. return;
  857. trbs_left = DWC3_TRB_NUM;
  858. /*
  859. * In case we start from scratch, we queue the ISOC requests
  860. * starting from slot 1. This is done because we use ring
  861. * buffer and have no LST bit to stop us. Instead, we place
  862. * IOC bit every TRB_NUM/4. We try to avoid having an interrupt
  863. * after the first request so we start at slot 1 and have
  864. * 7 requests proceed before we hit the first IOC.
  865. * Other transfer types don't use the ring buffer and are
  866. * processed from the first TRB until the last one. Since we
  867. * don't wrap around we have to start at the beginning.
  868. */
  869. if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
  870. dep->busy_slot = 1;
  871. dep->free_slot = 1;
  872. } else {
  873. dep->busy_slot = 0;
  874. dep->free_slot = 0;
  875. }
  876. }
  877. /* The last TRB is a link TRB, not used for xfer */
  878. if ((trbs_left <= 1) && usb_endpoint_xfer_isoc(dep->endpoint.desc))
  879. return;
  880. list_for_each_entry_safe(req, n, &dep->request_list, list) {
  881. unsigned length;
  882. dma_addr_t dma;
  883. if (req->request.num_mapped_sgs > 0) {
  884. struct usb_request *request = &req->request;
  885. struct scatterlist *sg = request->sg;
  886. struct scatterlist *s;
  887. int i;
  888. for_each_sg(sg, s, request->num_mapped_sgs, i) {
  889. unsigned chain = true;
  890. length = sg_dma_len(s);
  891. dma = sg_dma_address(s);
  892. if (i == (request->num_mapped_sgs - 1) ||
  893. sg_is_last(s)) {
  894. last_one = true;
  895. chain = false;
  896. }
  897. trbs_left--;
  898. if (!trbs_left)
  899. last_one = true;
  900. if (last_one)
  901. chain = false;
  902. dwc3_prepare_one_trb(dep, req, dma, length,
  903. last_one, chain);
  904. if (last_one)
  905. break;
  906. }
  907. dbg_queue(dep->number, &req->request, 0);
  908. if (last_one)
  909. break;
  910. } else {
  911. struct dwc3_request *req1;
  912. int maxpkt_size = usb_endpoint_maxp(dep->endpoint.desc);
  913. dma = req->request.dma;
  914. length = req->request.length;
  915. trbs_left--;
  916. if (req->request.zero && length &&
  917. (length % maxpkt_size == 0))
  918. trbs_left--;
  919. if (!trbs_left) {
  920. last_one = 1;
  921. } else if (dep->direction && (trbs_left <= 1)) {
  922. req1 = next_request(&req->list);
  923. if (req1->request.zero && req1->request.length
  924. && (req1->request.length % maxpkt_size == 0))
  925. last_one = 1;
  926. }
  927. /* Is this the last request? */
  928. if (list_is_last(&req->list, &dep->request_list))
  929. last_one = 1;
  930. if(usb_endpoint_dir_in(dep->endpoint.desc))
  931. dwc3_prepare_one_trb(dep, req, dma, length,
  932. last_one, false);
  933. else
  934. dwc3_prepare_one_trb(dep, req, dma, length,
  935. 1, false);
  936. dbg_queue(dep->number, &req->request, 0);
  937. if (last_one)
  938. break;
  939. }
  940. }
  941. }
  942. static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param,
  943. int start_new)
  944. {
  945. struct dwc3_gadget_ep_cmd_params params;
  946. struct dwc3_request *req, *req1, *n;
  947. struct dwc3 *dwc = dep->dwc;
  948. int ret;
  949. u32 cmd;
  950. if (start_new && (dep->flags & DWC3_EP_BUSY)) {
  951. dev_vdbg(dwc->dev, "%s: endpoint busy\n", dep->name);
  952. return -EBUSY;
  953. }
  954. dep->flags &= ~DWC3_EP_PENDING_REQUEST;
  955. /*
  956. * If we are getting here after a short-out-packet we don't enqueue any
  957. * new requests as we try to set the IOC bit only on the last request.
  958. */
  959. if (start_new) {
  960. if (list_empty(&dep->req_queued))
  961. dwc3_prepare_trbs(dep, start_new);
  962. /* req points to the first request which will be sent */
  963. req = next_request(&dep->req_queued);
  964. } else {
  965. dwc3_prepare_trbs(dep, start_new);
  966. /*
  967. * req points to the first request where HWO changed from 0 to 1
  968. */
  969. req = next_request(&dep->req_queued);
  970. }
  971. if (!req) {
  972. dep->flags |= DWC3_EP_PENDING_REQUEST;
  973. dbg_event(dep->number, "NO REQ", 0);
  974. return 0;
  975. }
  976. memset(&params, 0, sizeof(params));
  977. params.param0 = upper_32_bits(req->trb_dma);
  978. params.param1 = lower_32_bits(req->trb_dma);
  979. if (start_new)
  980. cmd = DWC3_DEPCMD_STARTTRANSFER;
  981. else
  982. cmd = DWC3_DEPCMD_UPDATETRANSFER;
  983. cmd |= DWC3_DEPCMD_PARAM(cmd_param);
  984. ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
  985. if (ret < 0) {
  986. dev_dbg(dwc->dev, "failed to send STARTTRANSFER command\n");
  987. if ((ret == -EAGAIN) && start_new &&
  988. usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
  989. /* If bit13 in Command complete event is set, software
  990. * must issue ENDTRANDFER command and wait for
  991. * Xfernotready event to queue the requests again.
  992. */
  993. if (!dep->resource_index) {
  994. dep->resource_index =
  995. dwc3_gadget_ep_get_transfer_index(dwc,
  996. dep->number);
  997. WARN_ON_ONCE(!dep->resource_index);
  998. }
  999. dwc3_stop_active_transfer(dwc, dep->number);
  1000. list_for_each_entry_safe_reverse(req1, n,
  1001. &dep->req_queued, list) {
  1002. req1->trb = NULL;
  1003. dwc3_gadget_move_request_list_front(req1);
  1004. if (req->request.num_mapped_sgs)
  1005. dep->busy_slot +=
  1006. req->request.num_mapped_sgs;
  1007. else
  1008. dep->busy_slot++;
  1009. if ((dep->busy_slot & DWC3_TRB_MASK) ==
  1010. DWC3_TRB_NUM - 1)
  1011. dep->busy_slot++;
  1012. }
  1013. return ret;
  1014. } else {
  1015. /*
  1016. * FIXME we need to iterate over the list of requests
  1017. * here and stop, unmap, free and del each of the linked
  1018. * requests instead of what we do now.
  1019. */
  1020. usb_gadget_unmap_request(&dwc->gadget, &req->request,
  1021. req->direction);
  1022. list_del(&req->list);
  1023. return ret;
  1024. }
  1025. }
  1026. dep->flags |= DWC3_EP_BUSY;
  1027. if (start_new) {
  1028. dep->resource_index = dwc3_gadget_ep_get_transfer_index(dwc,
  1029. dep->number);
  1030. WARN_ON_ONCE(!dep->resource_index);
  1031. }
  1032. return 0;
  1033. }
  1034. static void __dwc3_gadget_start_isoc(struct dwc3 *dwc,
  1035. struct dwc3_ep *dep, u32 cur_uf)
  1036. {
  1037. u32 uf;
  1038. int ret;
  1039. dep->current_uf = cur_uf;
  1040. if (list_empty(&dep->request_list)) {
  1041. dev_vdbg(dwc->dev, "ISOC ep %s run out for requests.\n",
  1042. dep->name);
  1043. dep->flags |= DWC3_EP_PENDING_REQUEST;
  1044. return;
  1045. }
  1046. /* 4 micro frames in the future */
  1047. uf = cur_uf + dep->interval * 4;
  1048. ret = __dwc3_gadget_kick_transfer(dep, uf, 1);
  1049. if (ret < 0)
  1050. dbg_event(dep->number, "QUEUE", ret);
  1051. }
  1052. static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
  1053. struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
  1054. {
  1055. u32 cur_uf, mask;
  1056. mask = ~(dep->interval - 1);
  1057. cur_uf = event->parameters & mask;
  1058. __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
  1059. }
  1060. static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
  1061. {
  1062. struct dwc3 *dwc = dep->dwc;
  1063. int ret;
  1064. if (req->request.status == -EINPROGRESS) {
  1065. ret = -EBUSY;
  1066. dev_err(dwc->dev, "%s: %pK request already in queue",
  1067. dep->name, req);
  1068. return ret;
  1069. }
  1070. req->request.actual = 0;
  1071. req->request.status = -EINPROGRESS;
  1072. req->direction = dep->direction;
  1073. req->epnum = dep->number;
  1074. /*
  1075. * We only add to our list of requests now and
  1076. * start consuming the list once we get XferNotReady
  1077. * IRQ.
  1078. *
  1079. * That way, we avoid doing anything that we don't need
  1080. * to do now and defer it until the point we receive a
  1081. * particular token from the Host side.
  1082. *
  1083. * This will also avoid Host cancelling URBs due to too
  1084. * many NAKs.
  1085. */
  1086. ret = usb_gadget_map_request(&dwc->gadget, &req->request,
  1087. dep->direction);
  1088. if (ret)
  1089. return ret;
  1090. list_add_tail(&req->list, &dep->request_list);
  1091. /*
  1092. * There are a few special cases:
  1093. *
  1094. * 1. XferNotReady with empty list of requests. We need to kick the
  1095. * transfer here in that situation, otherwise we will be NAKing
  1096. * forever. If we get XferNotReady before gadget driver has a
  1097. * chance to queue a request, we will ACK the IRQ but won't be
  1098. * able to receive the data until the next request is queued.
  1099. * The following code is handling exactly that.
  1100. *
  1101. */
  1102. if (dep->flags & DWC3_EP_PENDING_REQUEST) {
  1103. int ret;
  1104. /*
  1105. * If xfernotready is already elapsed and it is a case
  1106. * of isoc transfer, then issue END TRANSFER, so that
  1107. * you can receive xfernotready again and can have
  1108. * notion of current microframe.
  1109. */
  1110. if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
  1111. /* If xfernotready event is recieved before issuing
  1112. * START TRANSFER command, don't issue END TRANSFER.
  1113. * Rather start queueing the requests by issuing START
  1114. * TRANSFER command.
  1115. */
  1116. if (list_empty(&dep->req_queued) && dep->resource_index)
  1117. dwc3_stop_active_transfer(dwc, dep->number);
  1118. else
  1119. __dwc3_gadget_start_isoc(dwc, dep,
  1120. dep->current_uf);
  1121. dep->flags &= ~DWC3_EP_PENDING_REQUEST;
  1122. return 0;
  1123. }
  1124. ret = __dwc3_gadget_kick_transfer(dep, 0, true);
  1125. if (ret && ret != -EBUSY) {
  1126. dbg_event(dep->number, "QUEUE", ret);
  1127. dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
  1128. dep->name);
  1129. }
  1130. }
  1131. /*
  1132. * 2. XferInProgress on Isoc EP with an active transfer. We need to
  1133. * kick the transfer here after queuing a request, otherwise the
  1134. * core may not see the modified TRB(s).
  1135. */
  1136. if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
  1137. (dep->flags & DWC3_EP_BUSY) &&
  1138. !(dep->flags & DWC3_EP_MISSED_ISOC)) {
  1139. WARN_ON_ONCE(!dep->resource_index);
  1140. ret = __dwc3_gadget_kick_transfer(dep, dep->resource_index,
  1141. false);
  1142. if (ret && ret != -EBUSY) {
  1143. dbg_event(dep->number, "QUEUE", ret);
  1144. dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
  1145. dep->name);
  1146. }
  1147. }
  1148. return 0;
  1149. }
  1150. static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
  1151. gfp_t gfp_flags)
  1152. {
  1153. struct dwc3_request *req = to_dwc3_request(request);
  1154. struct dwc3_ep *dep = to_dwc3_ep(ep);
  1155. struct dwc3 *dwc = dep->dwc;
  1156. unsigned long flags;
  1157. int ret;
  1158. spin_lock_irqsave(&dwc->lock, flags);
  1159. if (!dep->endpoint.desc) {
  1160. spin_unlock_irqrestore(&dwc->lock, flags);
  1161. dev_dbg(dwc->dev, "trying to queue request %pK to disabled %s\n",
  1162. request, ep->name);
  1163. return -ESHUTDOWN;
  1164. }
  1165. dev_vdbg(dwc->dev, "queing request %pK to %s length %d\n",
  1166. request, ep->name, request->length);
  1167. WARN(!dep->direction && (request->length % ep->desc->wMaxPacketSize),
  1168. "trying to queue unaligned request (%d)\n", request->length);
  1169. ret = __dwc3_gadget_ep_queue(dep, req);
  1170. spin_unlock_irqrestore(&dwc->lock, flags);
  1171. return ret;
  1172. }
  1173. static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
  1174. struct usb_request *request)
  1175. {
  1176. struct dwc3_request *req = to_dwc3_request(request);
  1177. struct dwc3_request *r = NULL;
  1178. struct dwc3_ep *dep = to_dwc3_ep(ep);
  1179. struct dwc3 *dwc = dep->dwc;
  1180. unsigned long flags;
  1181. int ret = 0;
  1182. spin_lock_irqsave(&dwc->lock, flags);
  1183. list_for_each_entry(r, &dep->request_list, list) {
  1184. if (r == req)
  1185. break;
  1186. }
  1187. if (r != req) {
  1188. list_for_each_entry(r, &dep->req_queued, list) {
  1189. if (r == req)
  1190. break;
  1191. }
  1192. if (r == req) {
  1193. /* wait until it is processed */
  1194. dwc3_stop_active_transfer(dwc, dep->number);
  1195. goto out1;
  1196. }
  1197. dev_err(dwc->dev, "request %pK was not queued to %s\n",
  1198. request, ep->name);
  1199. ret = -EINVAL;
  1200. goto out0;
  1201. }
  1202. out1:
  1203. dbg_event(dep->number, "DEQUEUE", 0);
  1204. /* giveback the request */
  1205. dwc3_gadget_giveback(dep, req, -ECONNRESET);
  1206. out0:
  1207. spin_unlock_irqrestore(&dwc->lock, flags);
  1208. return ret;
  1209. }
  1210. int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
  1211. {
  1212. struct dwc3_gadget_ep_cmd_params params;
  1213. struct dwc3 *dwc = dep->dwc;
  1214. int ret;
  1215. memset(&params, 0x00, sizeof(params));
  1216. if (value) {
  1217. if (!protocol && ((dep->direction && dep->flags & DWC3_EP_BUSY) ||
  1218. (!list_empty(&dep->req_queued) ||
  1219. !list_empty(&dep->request_list)))) {
  1220. dev_dbg(dwc->dev, "%s: pending request, cannot halt\n",
  1221. dep->name);
  1222. return -EAGAIN;
  1223. }
  1224. ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
  1225. DWC3_DEPCMD_SETSTALL, &params);
  1226. if (ret) {
  1227. dbg_event(dep->number, "SETSTAL", ret);
  1228. dev_dbg(dwc->dev, "failed to %s STALL on %s\n",
  1229. value ? "set" : "clear",
  1230. dep->name);
  1231. } else {
  1232. dep->flags |= DWC3_EP_STALL;
  1233. }
  1234. } else {
  1235. ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
  1236. DWC3_DEPCMD_CLEARSTALL, &params);
  1237. if (ret) {
  1238. dbg_event(dep->number, "CLRSTAL", ret);
  1239. dev_dbg(dwc->dev, "failed to %s STALL on %s\n",
  1240. value ? "set" : "clear",
  1241. dep->name);
  1242. } else {
  1243. dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
  1244. }
  1245. }
  1246. return ret;
  1247. }
  1248. static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
  1249. {
  1250. struct dwc3_ep *dep = to_dwc3_ep(ep);
  1251. struct dwc3 *dwc = dep->dwc;
  1252. unsigned long flags;
  1253. int ret;
  1254. spin_lock_irqsave(&dwc->lock, flags);
  1255. if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
  1256. dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
  1257. ret = -EINVAL;
  1258. goto out;
  1259. }
  1260. dbg_event(dep->number, "HALT", value);
  1261. ret = __dwc3_gadget_ep_set_halt(dep, value, false);
  1262. out:
  1263. spin_unlock_irqrestore(&dwc->lock, flags);
  1264. return ret;
  1265. }
  1266. static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
  1267. {
  1268. struct dwc3_ep *dep = to_dwc3_ep(ep);
  1269. struct dwc3 *dwc = dep->dwc;
  1270. unsigned long flags;
  1271. spin_lock_irqsave(&dwc->lock, flags);
  1272. dbg_event(dep->number, "WEDGE", 0);
  1273. dep->flags |= DWC3_EP_WEDGE;
  1274. spin_unlock_irqrestore(&dwc->lock, flags);
  1275. if (dep->number == 0 || dep->number == 1)
  1276. return dwc3_gadget_ep0_set_halt(ep, 1);
  1277. else
  1278. return dwc3_gadget_ep_set_halt(ep, 1);
  1279. }
  1280. /* -------------------------------------------------------------------------- */
  1281. static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
  1282. .bLength = USB_DT_ENDPOINT_SIZE,
  1283. .bDescriptorType = USB_DT_ENDPOINT,
  1284. .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
  1285. };
  1286. static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
  1287. .enable = dwc3_gadget_ep0_enable,
  1288. .disable = dwc3_gadget_ep0_disable,
  1289. .alloc_request = dwc3_gadget_ep_alloc_request,
  1290. .free_request = dwc3_gadget_ep_free_request,
  1291. .queue = dwc3_gadget_ep0_queue,
  1292. .dequeue = dwc3_gadget_ep_dequeue,
  1293. .set_halt = dwc3_gadget_ep0_set_halt,
  1294. .set_wedge = dwc3_gadget_ep_set_wedge,
  1295. };
  1296. static const struct usb_ep_ops dwc3_gadget_ep_ops = {
  1297. .enable = dwc3_gadget_ep_enable,
  1298. .disable = dwc3_gadget_ep_disable,
  1299. .alloc_request = dwc3_gadget_ep_alloc_request,
  1300. .free_request = dwc3_gadget_ep_free_request,
  1301. .queue = dwc3_gadget_ep_queue,
  1302. .dequeue = dwc3_gadget_ep_dequeue,
  1303. .set_halt = dwc3_gadget_ep_set_halt,
  1304. .set_wedge = dwc3_gadget_ep_set_wedge,
  1305. };
  1306. /* -------------------------------------------------------------------------- */
  1307. static int dwc3_gadget_get_frame(struct usb_gadget *g)
  1308. {
  1309. struct dwc3 *dwc = gadget_to_dwc(g);
  1310. u32 reg;
  1311. reg = dwc3_readl(dwc->regs, DWC3_DSTS);
  1312. return DWC3_DSTS_SOFFN(reg);
  1313. }
  1314. static int dwc3_gadget_wakeup(struct usb_gadget *g)
  1315. {
  1316. struct dwc3 *dwc = gadget_to_dwc(g);
  1317. unsigned long timeout;
  1318. unsigned long flags;
  1319. u32 reg;
  1320. int ret = 0;
  1321. u8 link_state;
  1322. u8 speed;
  1323. spin_lock_irqsave(&dwc->lock, flags);
  1324. /*
  1325. * According to the Databook Remote wakeup request should
  1326. * be issued only when the device is in early suspend state.
  1327. *
  1328. * We can check that via USB Link State bits in DSTS register.
  1329. */
  1330. reg = dwc3_readl(dwc->regs, DWC3_DSTS);
  1331. speed = reg & DWC3_DSTS_CONNECTSPD;
  1332. if (speed == DWC3_DSTS_SUPERSPEED) {
  1333. dev_dbg(dwc->dev, "no wakeup on SuperSpeed\n");
  1334. ret = -EINVAL;
  1335. goto out;
  1336. }
  1337. link_state = DWC3_DSTS_USBLNKST(reg);
  1338. switch (link_state) {
  1339. case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
  1340. case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
  1341. break;
  1342. default:
  1343. dev_dbg(dwc->dev, "can't wakeup from link state %d\n",
  1344. link_state);
  1345. ret = -EINVAL;
  1346. goto out;
  1347. }
  1348. ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
  1349. if (ret < 0) {
  1350. dev_err(dwc->dev, "failed to put link in Recovery\n");
  1351. goto out;
  1352. }
  1353. /* Recent versions do this automatically */
  1354. if (dwc->revision < DWC3_REVISION_194A) {
  1355. /* write zeroes to Link Change Request */
  1356. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  1357. reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
  1358. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  1359. }
  1360. /* poll until Link State changes to ON */
  1361. timeout = jiffies + msecs_to_jiffies(100);
  1362. while (!time_after(jiffies, timeout)) {
  1363. reg = dwc3_readl(dwc->regs, DWC3_DSTS);
  1364. /* in HS, means ON */
  1365. if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
  1366. break;
  1367. }
  1368. if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
  1369. dev_err(dwc->dev, "failed to send remote wakeup\n");
  1370. ret = -EINVAL;
  1371. }
  1372. out:
  1373. spin_unlock_irqrestore(&dwc->lock, flags);
  1374. return ret;
  1375. }
  1376. static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
  1377. int is_selfpowered)
  1378. {
  1379. struct dwc3 *dwc = gadget_to_dwc(g);
  1380. unsigned long flags;
  1381. spin_lock_irqsave(&dwc->lock, flags);
  1382. dwc->is_selfpowered = !!is_selfpowered;
  1383. spin_unlock_irqrestore(&dwc->lock, flags);
  1384. return 0;
  1385. }
  1386. #define DWC3_SOFT_RESET_TIMEOUT 10 /* 10 msec */
  1387. static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on)
  1388. {
  1389. u32 reg;
  1390. u32 timeout = 500;
  1391. ktime_t start, diff;
  1392. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  1393. if (is_on) {
  1394. if (dwc->revision <= DWC3_REVISION_187A) {
  1395. reg &= ~DWC3_DCTL_TRGTULST_MASK;
  1396. reg |= DWC3_DCTL_TRGTULST_RX_DET;
  1397. }
  1398. if (dwc->revision >= DWC3_REVISION_194A)
  1399. reg &= ~DWC3_DCTL_KEEP_CONNECT;
  1400. start = ktime_get();
  1401. /* issue device SoftReset */
  1402. dwc3_writel(dwc->regs, DWC3_DCTL, reg | DWC3_DCTL_CSFTRST);
  1403. do {
  1404. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  1405. if (!(reg & DWC3_DCTL_CSFTRST))
  1406. break;
  1407. diff = ktime_sub(ktime_get(), start);
  1408. /* poll for max. 10ms */
  1409. if (ktime_to_ms(diff) > DWC3_SOFT_RESET_TIMEOUT) {
  1410. printk_ratelimited(KERN_ERR
  1411. "%s:core Reset Timed Out\n", __func__);
  1412. break;
  1413. }
  1414. cpu_relax();
  1415. } while (true);
  1416. dwc3_event_buffers_setup(dwc);
  1417. dwc3_gadget_restart(dwc);
  1418. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  1419. reg |= DWC3_DCTL_RUN_STOP;
  1420. } else {
  1421. reg &= ~DWC3_DCTL_RUN_STOP;
  1422. }
  1423. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  1424. do {
  1425. reg = dwc3_readl(dwc->regs, DWC3_DSTS);
  1426. if (is_on) {
  1427. if (!(reg & DWC3_DSTS_DEVCTRLHLT))
  1428. break;
  1429. } else {
  1430. if (reg & DWC3_DSTS_DEVCTRLHLT)
  1431. break;
  1432. }
  1433. timeout--;
  1434. if (!timeout)
  1435. return -ETIMEDOUT;
  1436. udelay(1);
  1437. } while (1);
  1438. dev_info(dwc->dev, "gadget %s data soft-%s\n",
  1439. dwc->gadget_driver
  1440. ? dwc->gadget_driver->function : "no-function",
  1441. is_on ? "connect" : "disconnect");
  1442. return 0;
  1443. }
  1444. static int dwc3_gadget_vbus_draw(struct usb_gadget *g, unsigned mA)
  1445. {
  1446. struct dwc3 *dwc = gadget_to_dwc(g);
  1447. struct dwc3_otg *dotg = dwc->dotg;
  1448. if (dotg && dotg->otg.phy)
  1449. return usb_phy_set_power(dotg->otg.phy, mA);
  1450. return -ENOTSUPP;
  1451. }
  1452. static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
  1453. {
  1454. struct dwc3 *dwc = gadget_to_dwc(g);
  1455. unsigned long flags;
  1456. int ret;
  1457. is_on = !!is_on;
  1458. spin_lock_irqsave(&dwc->lock, flags);
  1459. dwc->softconnect = is_on;
  1460. if ((dwc->dotg && !dwc->vbus_active) ||
  1461. !dwc->gadget_driver) {
  1462. spin_unlock_irqrestore(&dwc->lock, flags);
  1463. /*
  1464. * Need to wait for vbus_session(on) from otg driver or to
  1465. * the udc_start.
  1466. */
  1467. return 0;
  1468. }
  1469. ret = dwc3_gadget_run_stop(dwc, is_on);
  1470. spin_unlock_irqrestore(&dwc->lock, flags);
  1471. return ret;
  1472. }
  1473. static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc);
  1474. static int dwc3_gadget_vbus_session(struct usb_gadget *_gadget, int is_active)
  1475. {
  1476. struct dwc3 *dwc = gadget_to_dwc(_gadget);
  1477. unsigned long flags;
  1478. int ret = 0;
  1479. #if defined(CONFIG_SEC_H_PROJECT) || defined(CONFIG_SEC_F_PROJECT) || defined(CONFIG_SEC_K_PROJECT)
  1480. int cancel_work = 0;
  1481. #endif
  1482. if (!dwc->dotg)
  1483. return -EPERM;
  1484. is_active = !!is_active;
  1485. spin_lock_irqsave(&dwc->lock, flags);
  1486. #if defined(CONFIG_SEC_H_PROJECT) || defined(CONFIG_SEC_F_PROJECT) || defined(CONFIG_SEC_K_PROJECT)
  1487. if (!is_active) {
  1488. dwc->ss_host_avail = -1;
  1489. dwc->speed_limit = dwc->gadget.max_speed;
  1490. cancel_work = 1;
  1491. }
  1492. #endif
  1493. if (dwc->vbus_active == is_active) {
  1494. printk(KERN_ERR "dwc3 state is same\n");
  1495. spin_unlock_irqrestore(&dwc->lock, flags);
  1496. return 0;
  1497. }
  1498. /* Mark that the vbus was powered */
  1499. dwc->vbus_active = is_active;
  1500. /*
  1501. * Check if upper level usb_gadget_driver was already registerd with
  1502. * this udc controller driver (if dwc3_gadget_start was called)
  1503. */
  1504. if (dwc->gadget_driver && dwc->softconnect) {
  1505. if (dwc->vbus_active) {
  1506. /*
  1507. * Both vbus was activated by otg and pullup was
  1508. * signaled by the gadget driver.
  1509. */
  1510. ret = dwc3_gadget_run_stop(dwc, 1);
  1511. } else {
  1512. ret = dwc3_gadget_run_stop(dwc, 0);
  1513. }
  1514. }
  1515. /*
  1516. * Clearing run/stop bit might occur before disconnect event is seen.
  1517. * Make sure to let gadget driver know in that case.
  1518. */
  1519. if (!dwc->vbus_active) {
  1520. dev_dbg(dwc->dev, "calling disconnect from %s\n", __func__);
  1521. dwc3_gadget_disconnect_interrupt(dwc);
  1522. }
  1523. spin_unlock_irqrestore(&dwc->lock, flags);
  1524. #if defined(CONFIG_SEC_H_PROJECT) || defined(CONFIG_SEC_F_PROJECT) || defined(CONFIG_SEC_K_PROJECT)
  1525. if (cancel_work) WORK_CANCEL(dwc);
  1526. #endif
  1527. return ret;
  1528. }
  1529. /* Required gadget re-initialization before switching to gadget in OTG mode */
  1530. void dwc3_gadget_restart(struct dwc3 *dwc)
  1531. {
  1532. struct dwc3_ep *dep;
  1533. int ret = 0;
  1534. u32 reg;
  1535. #if defined(CONFIG_SEC_H_PROJECT) || defined(CONFIG_SEC_F_PROJECT) || defined(CONFIG_SEC_K_PROJECT)
  1536. int ep0_mps;
  1537. #endif
  1538. /* Enable all but Start and End of Frame IRQs */
  1539. reg = (DWC3_DEVTEN_EVNTOVERFLOWEN |
  1540. DWC3_DEVTEN_CMDCMPLTEN |
  1541. DWC3_DEVTEN_ERRTICERREN |
  1542. DWC3_DEVTEN_WKUPEVTEN |
  1543. DWC3_DEVTEN_ULSTCNGEN |
  1544. DWC3_DEVTEN_CONNECTDONEEN |
  1545. DWC3_DEVTEN_USBRSTEN |
  1546. DWC3_DEVTEN_DISCONNEVTEN);
  1547. dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
  1548. /* Enable USB2 LPM and automatic phy suspend only on recent versions */
  1549. if (dwc->revision >= DWC3_REVISION_194A) {
  1550. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  1551. reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
  1552. /* TODO: This should be configurable */
  1553. reg |= DWC3_DCTL_HIRD_THRES(28);
  1554. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  1555. dwc3_gadget_usb2_phy_suspend(dwc, true);
  1556. dwc3_gadget_usb3_phy_suspend(dwc, true);
  1557. }
  1558. reg = dwc3_readl(dwc->regs, DWC3_DCFG);
  1559. reg &= ~(DWC3_DCFG_SPEED_MASK);
  1560. /**
  1561. * WORKAROUND: DWC3 revision < 2.20a have an issue
  1562. * which would cause metastability state on Run/Stop
  1563. * bit if we try to force the IP to USB2-only mode.
  1564. *
  1565. * Because of that, we cannot configure the IP to any
  1566. * speed other than the SuperSpeed
  1567. *
  1568. * Refers to:
  1569. *
  1570. * STAR#9000525659: Clock Domain Crossing on DCTL in
  1571. * USB 2.0 Mode
  1572. */
  1573. if (dwc->revision < DWC3_REVISION_220A)
  1574. reg |= DWC3_DCFG_SUPERSPEED;
  1575. else
  1576. #if !defined(CONFIG_SEC_H_PROJECT) && !defined(CONFIG_SEC_F_PROJECT) && !defined(CONFIG_SEC_K_PROJECT)
  1577. reg |= dwc->maximum_speed;
  1578. #else
  1579. switch(dwc->speed_limit) {
  1580. case USB_SPEED_SUPER:
  1581. reg |= DWC3_DCFG_SUPERSPEED;
  1582. break;
  1583. case USB_SPEED_HIGH:
  1584. reg |= DWC3_DCFG_HIGHSPEED;
  1585. break;
  1586. case USB_SPEED_FULL:
  1587. reg |= DWC3_DCFG_FULLSPEED1;
  1588. break;
  1589. default:
  1590. reg |= DWC3_DCFG_SUPERSPEED;
  1591. }
  1592. #endif
  1593. dwc3_writel(dwc->regs, DWC3_DCFG, reg);
  1594. #if defined(CONFIG_SEC_H_PROJECT) || defined(CONFIG_SEC_F_PROJECT) || defined(CONFIG_SEC_K_PROJECT)
  1595. switch (dwc->speed_limit) {
  1596. case USB_SPEED_FULL:
  1597. case USB_SPEED_HIGH:
  1598. ep0_mps = EP0_HS_MPS;
  1599. break;
  1600. case USB_SPEED_SUPER:
  1601. ep0_mps = EP0_SS_MPS;
  1602. break;
  1603. default:
  1604. dev_warn(dwc->dev, "%s: unsupported device speed\n", __func__);
  1605. ep0_mps = (dwc->gadget.max_speed == USB_SPEED_SUPER) ?
  1606. EP0_SS_MPS : EP0_HS_MPS;
  1607. }
  1608. dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(ep0_mps);
  1609. #else
  1610. /* Start with SuperSpeed Default */
  1611. dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
  1612. #endif
  1613. dwc->delayed_status = false;
  1614. /* reinitialize physical ep0-1 */
  1615. dep = dwc->eps[0];
  1616. dep->flags = 0;
  1617. ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false);
  1618. if (ret) {
  1619. dev_err(dwc->dev, "failed to enable %s\n", dep->name);
  1620. return;
  1621. }
  1622. dep = dwc->eps[1];
  1623. dep->flags = 0;
  1624. ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false);
  1625. if (ret) {
  1626. dev_err(dwc->dev, "failed to enable %s\n", dep->name);
  1627. return;
  1628. }
  1629. /* begin to receive SETUP packets */
  1630. dwc->ep0state = EP0_SETUP_PHASE;
  1631. dwc3_ep0_out_start(dwc);
  1632. }
  1633. static int dwc3_gadget_start(struct usb_gadget *g,
  1634. struct usb_gadget_driver *driver)
  1635. {
  1636. struct dwc3 *dwc = gadget_to_dwc(g);
  1637. struct dwc3_ep *dep;
  1638. unsigned long flags;
  1639. int ret = 0;
  1640. u32 reg;
  1641. pm_runtime_get_sync(dwc->dev);
  1642. spin_lock_irqsave(&dwc->lock, flags);
  1643. if (dwc->gadget_driver) {
  1644. dev_err(dwc->dev, "%s is already bound to %s\n",
  1645. dwc->gadget.name,
  1646. dwc->gadget_driver->driver.name);
  1647. ret = -EBUSY;
  1648. goto err0;
  1649. }
  1650. dwc->gadget_driver = driver;
  1651. dwc->gadget.dev.driver = &driver->driver;
  1652. reg = dwc3_readl(dwc->regs, DWC3_DCFG);
  1653. reg &= ~(DWC3_DCFG_SPEED_MASK);
  1654. /**
  1655. * WORKAROUND: DWC3 revision < 2.20a have an issue
  1656. * which would cause metastability state on Run/Stop
  1657. * bit if we try to force the IP to USB2-only mode.
  1658. *
  1659. * Because of that, we cannot configure the IP to any
  1660. * speed other than the SuperSpeed
  1661. *
  1662. * Refers to:
  1663. *
  1664. * STAR#9000525659: Clock Domain Crossing on DCTL in
  1665. * USB 2.0 Mode
  1666. */
  1667. if (dwc->revision < DWC3_REVISION_220A)
  1668. reg |= DWC3_DCFG_SUPERSPEED;
  1669. else
  1670. reg |= dwc->maximum_speed;
  1671. dwc3_writel(dwc->regs, DWC3_DCFG, reg);
  1672. /* Start with SuperSpeed Default */
  1673. dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
  1674. dep = dwc->eps[0];
  1675. dep->endpoint.maxburst = 1;
  1676. ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false);
  1677. if (ret) {
  1678. dev_err(dwc->dev, "failed to enable %s\n", dep->name);
  1679. goto err0;
  1680. }
  1681. dep = dwc->eps[1];
  1682. dep->endpoint.maxburst = 1;
  1683. ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false);
  1684. if (ret) {
  1685. dev_err(dwc->dev, "failed to enable %s\n", dep->name);
  1686. goto err1;
  1687. }
  1688. /* begin to receive SETUP packets */
  1689. dwc->ep0state = EP0_SETUP_PHASE;
  1690. dwc3_ep0_out_start(dwc);
  1691. spin_unlock_irqrestore(&dwc->lock, flags);
  1692. pm_runtime_put(dwc->dev);
  1693. return 0;
  1694. err1:
  1695. __dwc3_gadget_ep_disable(dwc->eps[0]);
  1696. err0:
  1697. dwc->gadget_driver = NULL;
  1698. spin_unlock_irqrestore(&dwc->lock, flags);
  1699. pm_runtime_put(dwc->dev);
  1700. return ret;
  1701. }
  1702. static int dwc3_gadget_stop(struct usb_gadget *g,
  1703. struct usb_gadget_driver *driver)
  1704. {
  1705. struct dwc3 *dwc = gadget_to_dwc(g);
  1706. unsigned long flags;
  1707. spin_lock_irqsave(&dwc->lock, flags);
  1708. __dwc3_gadget_ep_disable(dwc->eps[0]);
  1709. __dwc3_gadget_ep_disable(dwc->eps[1]);
  1710. dwc->gadget_driver = NULL;
  1711. dwc->gadget.dev.driver = NULL;
  1712. spin_unlock_irqrestore(&dwc->lock, flags);
  1713. return 0;
  1714. }
  1715. static const struct usb_gadget_ops dwc3_gadget_ops = {
  1716. .get_frame = dwc3_gadget_get_frame,
  1717. .wakeup = dwc3_gadget_wakeup,
  1718. .set_selfpowered = dwc3_gadget_set_selfpowered,
  1719. .vbus_session = dwc3_gadget_vbus_session,
  1720. .vbus_draw = dwc3_gadget_vbus_draw,
  1721. .pullup = dwc3_gadget_pullup,
  1722. .udc_start = dwc3_gadget_start,
  1723. .udc_stop = dwc3_gadget_stop,
  1724. };
  1725. /* -------------------------------------------------------------------------- */
  1726. static int __devinit dwc3_gadget_init_hw_endpoints(struct dwc3 *dwc,
  1727. u8 num, u32 direction)
  1728. {
  1729. struct dwc3_ep *dep;
  1730. u8 i;
  1731. for (i = 0; i < num; i++) {
  1732. u8 epnum = (i << 1) | (!!direction);
  1733. dep = kzalloc(sizeof(*dep), GFP_KERNEL);
  1734. if (!dep) {
  1735. dev_err(dwc->dev, "can't allocate endpoint %d\n",
  1736. epnum);
  1737. return -ENOMEM;
  1738. }
  1739. dep->dwc = dwc;
  1740. dep->number = epnum;
  1741. dwc->eps[epnum] = dep;
  1742. snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
  1743. (epnum & 1) ? "in" : "out");
  1744. dep->endpoint.name = dep->name;
  1745. dep->direction = (epnum & 1);
  1746. if (epnum == 0 || epnum == 1) {
  1747. dep->endpoint.maxpacket = 512;
  1748. dep->endpoint.maxburst = 1;
  1749. dep->endpoint.ops = &dwc3_gadget_ep0_ops;
  1750. if (!epnum)
  1751. dwc->gadget.ep0 = &dep->endpoint;
  1752. } else {
  1753. int ret;
  1754. dep->endpoint.maxpacket = 1024;
  1755. dep->endpoint.max_streams = 15;
  1756. dep->endpoint.ops = &dwc3_gadget_ep_ops;
  1757. list_add_tail(&dep->endpoint.ep_list,
  1758. &dwc->gadget.ep_list);
  1759. ret = dwc3_alloc_trb_pool(dep);
  1760. if (ret)
  1761. return ret;
  1762. }
  1763. INIT_LIST_HEAD(&dep->request_list);
  1764. INIT_LIST_HEAD(&dep->req_queued);
  1765. }
  1766. return 0;
  1767. }
  1768. static int dwc3_gadget_init_endpoints(struct dwc3 *dwc)
  1769. {
  1770. int ret;
  1771. INIT_LIST_HEAD(&dwc->gadget.ep_list);
  1772. ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_out_eps, 0);
  1773. if (ret < 0) {
  1774. dev_vdbg(dwc->dev, "failed to allocate OUT endpoints\n");
  1775. return ret;
  1776. }
  1777. ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_in_eps, 1);
  1778. if (ret < 0) {
  1779. dev_vdbg(dwc->dev, "failed to allocate IN endpoints\n");
  1780. return ret;
  1781. }
  1782. return 0;
  1783. }
  1784. static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
  1785. {
  1786. struct dwc3_ep *dep;
  1787. u8 epnum;
  1788. for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
  1789. dep = dwc->eps[epnum];
  1790. if (!dep)
  1791. continue;
  1792. /*
  1793. * Physical endpoints 0 and 1 are special; they form the
  1794. * bi-directional USB endpoint 0.
  1795. *
  1796. * For those two physical endpoints, we don't allocate a TRB
  1797. * pool nor do we add them the endpoints list. Due to that, we
  1798. * shouldn't do these two operations otherwise we would end up
  1799. * with all sorts of bugs when removing dwc3.ko.
  1800. */
  1801. if (epnum != 0 && epnum != 1) {
  1802. dwc3_free_trb_pool(dep);
  1803. list_del(&dep->endpoint.ep_list);
  1804. }
  1805. kfree(dep);
  1806. }
  1807. }
  1808. static void dwc3_gadget_release(struct device *dev)
  1809. {
  1810. dev_dbg(dev, "%s\n", __func__);
  1811. }
  1812. /* -------------------------------------------------------------------------- */
  1813. static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
  1814. const struct dwc3_event_depevt *event, int status)
  1815. {
  1816. struct dwc3_request *req;
  1817. struct dwc3_trb *trb;
  1818. unsigned int count;
  1819. unsigned int s_pkt = 0;
  1820. unsigned int trb_status;
  1821. if (dep->endpoint.desc == NULL)
  1822. return 1;
  1823. do {
  1824. req = next_request(&dep->req_queued);
  1825. if (!req) {
  1826. if (event->status)
  1827. dev_err(dwc->dev,
  1828. "%s: evt sts %x for no req queued",
  1829. dep->name, event->status);
  1830. return 1;
  1831. }
  1832. trb = req->trb;
  1833. if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
  1834. /*
  1835. * We continue despite the error. There is not much we
  1836. * can do. If we don't clean it up we loop forever. If
  1837. * we skip the TRB then it gets overwritten after a
  1838. * while since we use them in a ring buffer. A BUG()
  1839. * would help. Lets hope that if this occurs, someone
  1840. * fixes the root cause instead of looking away :)
  1841. */
  1842. dev_err(dwc->dev, "%s's TRB (%pK) still owned by HW\n",
  1843. dep->name, req->trb);
  1844. count = trb->size & DWC3_TRB_SIZE_MASK;
  1845. if (dep->direction) {
  1846. if (count) {
  1847. trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size);
  1848. if (trb_status == DWC3_TRBSTS_MISSED_ISOC) {
  1849. dev_dbg(dwc->dev, "incomplete IN transfer %s\n",
  1850. dep->name);
  1851. /*
  1852. * If missed isoc occurred and there is
  1853. * no request queued then issue END
  1854. * TRANSFER, so that core generates
  1855. * next xfernotready and we will issue
  1856. * a fresh START TRANSFER.
  1857. * If there are still queued request
  1858. * then wait, do not issue either END
  1859. * or UPDATE TRANSFER, just attach next
  1860. * request in request_list during
  1861. * giveback.If any future queued request
  1862. * is successfully transferred then we
  1863. * will issue UPDATE TRANSFER for all
  1864. * request in the request_list.
  1865. */
  1866. dep->flags |= DWC3_EP_MISSED_ISOC;
  1867. dbg_event(dep->number, "MISSED ISOC",
  1868. status);
  1869. } else {
  1870. dev_err(dwc->dev, "incomplete IN transfer %s\n",
  1871. dep->name);
  1872. status = -ECONNRESET;
  1873. }
  1874. } else {
  1875. dep->flags &= ~DWC3_EP_MISSED_ISOC;
  1876. }
  1877. } else {
  1878. if (count && (event->status & DEPEVT_STATUS_SHORT))
  1879. s_pkt = 1;
  1880. }
  1881. if (req->ztrb)
  1882. trb = req->ztrb;
  1883. /*
  1884. * We assume here we will always receive the entire data block
  1885. * which we should receive. Meaning, if we program RX to
  1886. * receive 4K but we receive only 2K, we assume that's all we
  1887. * should receive and we simply bounce the request back to the
  1888. * gadget driver for further processing.
  1889. */
  1890. req->request.actual += req->request.length - count;
  1891. dwc3_gadget_giveback(dep, req, status);
  1892. if (s_pkt)
  1893. break;
  1894. if ((event->status & DEPEVT_STATUS_LST) &&
  1895. (trb->ctrl & (DWC3_TRB_CTRL_LST |
  1896. DWC3_TRB_CTRL_HWO)))
  1897. break;
  1898. if ((event->status & DEPEVT_STATUS_IOC) &&
  1899. (trb->ctrl & DWC3_TRB_CTRL_IOC))
  1900. break;
  1901. } while (1);
  1902. dwc->gadget.xfer_isr_count++;
  1903. if (dep->endpoint.desc == NULL)
  1904. return 1;
  1905. if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
  1906. list_empty(&dep->req_queued)) {
  1907. if (list_empty(&dep->request_list))
  1908. /*
  1909. * If there is no entry in request list then do
  1910. * not issue END TRANSFER now. Just set PENDING
  1911. * flag, so that END TRANSFER is issued when an
  1912. * entry is added into request list.
  1913. */
  1914. dep->flags |= DWC3_EP_PENDING_REQUEST;
  1915. else
  1916. dwc3_stop_active_transfer(dwc, dep->number);
  1917. dep->flags &= ~DWC3_EP_MISSED_ISOC;
  1918. return 1;
  1919. }
  1920. if ((event->status & DEPEVT_STATUS_IOC) &&
  1921. (trb->ctrl & DWC3_TRB_CTRL_IOC))
  1922. return 0;
  1923. return 1;
  1924. }
  1925. static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
  1926. struct dwc3_ep *dep, const struct dwc3_event_depevt *event,
  1927. int start_new)
  1928. {
  1929. unsigned status = 0;
  1930. int clean_busy;
  1931. if (event->status & DEPEVT_STATUS_BUSERR)
  1932. status = -ECONNRESET;
  1933. clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
  1934. if (clean_busy)
  1935. dep->flags &= ~DWC3_EP_BUSY;
  1936. /*
  1937. * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
  1938. * See dwc3_gadget_linksts_change_interrupt() for 1st half.
  1939. */
  1940. if (dwc->revision < DWC3_REVISION_183A) {
  1941. u32 reg;
  1942. int i;
  1943. for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
  1944. dep = dwc->eps[i];
  1945. if (!(dep->flags & DWC3_EP_ENABLED))
  1946. continue;
  1947. if (!list_empty(&dep->req_queued))
  1948. return;
  1949. }
  1950. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  1951. reg |= dwc->u1u2;
  1952. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  1953. dwc->u1u2 = 0;
  1954. }
  1955. }
  1956. static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
  1957. const struct dwc3_event_depevt *event)
  1958. {
  1959. struct dwc3_ep *dep;
  1960. u8 epnum = event->endpoint_number;
  1961. dep = dwc->eps[epnum];
  1962. if (!(dep->flags & DWC3_EP_ENABLED))
  1963. return;
  1964. dev_vdbg(dwc->dev, "%s: %s\n", dep->name,
  1965. dwc3_ep_event_string(event->endpoint_event));
  1966. if (epnum == 0 || epnum == 1) {
  1967. dwc3_ep0_interrupt(dwc, event);
  1968. return;
  1969. }
  1970. switch (event->endpoint_event) {
  1971. case DWC3_DEPEVT_XFERCOMPLETE:
  1972. dep->resource_index = 0;
  1973. if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
  1974. dev_dbg(dwc->dev, "%s is an Isochronous endpoint\n",
  1975. dep->name);
  1976. return;
  1977. }
  1978. dbg_event(dep->number, "XFRCOMP", 0);
  1979. dwc3_endpoint_transfer_complete(dwc, dep, event, 1);
  1980. break;
  1981. case DWC3_DEPEVT_XFERINPROGRESS:
  1982. if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
  1983. dev_dbg(dwc->dev, "%s is not an Isochronous endpoint\n",
  1984. dep->name);
  1985. }
  1986. dbg_event(dep->number, "XFRPROG", 0);
  1987. dwc3_endpoint_transfer_complete(dwc, dep, event, 0);
  1988. break;
  1989. case DWC3_DEPEVT_XFERNOTREADY:
  1990. dbg_event(dep->number, "XFRNRDY", 0);
  1991. if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
  1992. dwc3_gadget_start_isoc(dwc, dep, event);
  1993. } else {
  1994. int ret;
  1995. dev_vdbg(dwc->dev, "%s: reason %s\n",
  1996. dep->name, event->status &
  1997. DEPEVT_STATUS_TRANSFER_ACTIVE
  1998. ? "Transfer Active"
  1999. : "Transfer Not Active");
  2000. ret = __dwc3_gadget_kick_transfer(dep, 0, 1);
  2001. if (!ret || ret == -EBUSY)
  2002. return;
  2003. else
  2004. dbg_event(dep->number, "QUEUE", ret);
  2005. dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
  2006. dep->name);
  2007. }
  2008. break;
  2009. case DWC3_DEPEVT_STREAMEVT:
  2010. if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) {
  2011. dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
  2012. dep->name);
  2013. return;
  2014. }
  2015. switch (event->status) {
  2016. case DEPEVT_STREAMEVT_FOUND:
  2017. dev_vdbg(dwc->dev, "Stream %d found and started\n",
  2018. event->parameters);
  2019. break;
  2020. case DEPEVT_STREAMEVT_NOTFOUND:
  2021. /* FALLTHROUGH */
  2022. default:
  2023. dev_dbg(dwc->dev, "Couldn't find suitable stream\n");
  2024. }
  2025. break;
  2026. case DWC3_DEPEVT_RXTXFIFOEVT:
  2027. dev_dbg(dwc->dev, "%s FIFO Overrun\n", dep->name);
  2028. break;
  2029. case DWC3_DEPEVT_EPCMDCMPLT:
  2030. dev_vdbg(dwc->dev, "Endpoint Command Complete\n");
  2031. break;
  2032. }
  2033. }
  2034. static void dwc3_disconnect_gadget(struct dwc3 *dwc)
  2035. {
  2036. if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
  2037. spin_unlock(&dwc->lock);
  2038. dwc->gadget_driver->disconnect(&dwc->gadget);
  2039. spin_lock(&dwc->lock);
  2040. }
  2041. dwc->gadget.xfer_isr_count = 0;
  2042. }
  2043. static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum)
  2044. {
  2045. struct dwc3_ep *dep;
  2046. struct dwc3_gadget_ep_cmd_params params;
  2047. u32 cmd;
  2048. int ret;
  2049. dep = dwc->eps[epnum];
  2050. if (!dep->resource_index)
  2051. return;
  2052. /*
  2053. * NOTICE: We are violating what the Databook says about the
  2054. * EndTransfer command. Ideally we would _always_ wait for the
  2055. * EndTransfer Command Completion IRQ, but that's causing too
  2056. * much trouble synchronizing between us and gadget driver.
  2057. *
  2058. * We have discussed this with the IP Provider and it was
  2059. * suggested to giveback all requests here, but give HW some
  2060. * extra time to synchronize with the interconnect. We're using
  2061. * an arbitraty 100us delay for that.
  2062. *
  2063. * Note also that a similar handling was tested by Synopsys
  2064. * (thanks a lot Paul) and nothing bad has come out of it.
  2065. * In short, what we're doing is:
  2066. *
  2067. * - Issue EndTransfer WITH CMDIOC bit set
  2068. * - Wait 100us
  2069. */
  2070. cmd = DWC3_DEPCMD_ENDTRANSFER;
  2071. cmd |= DWC3_DEPCMD_HIPRI_FORCERM | DWC3_DEPCMD_CMDIOC;
  2072. cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
  2073. memset(&params, 0, sizeof(params));
  2074. ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
  2075. WARN_ON_ONCE(ret);
  2076. dep->resource_index = 0;
  2077. dep->flags &= ~DWC3_EP_BUSY;
  2078. udelay(100);
  2079. }
  2080. static void dwc3_stop_active_transfers(struct dwc3 *dwc)
  2081. {
  2082. u32 epnum;
  2083. for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
  2084. struct dwc3_ep *dep;
  2085. dep = dwc->eps[epnum];
  2086. if (!dep)
  2087. continue;
  2088. if (!(dep->flags & DWC3_EP_ENABLED))
  2089. continue;
  2090. dwc3_remove_requests(dwc, dep);
  2091. }
  2092. }
  2093. static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
  2094. {
  2095. u32 epnum;
  2096. for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
  2097. struct dwc3_ep *dep;
  2098. struct dwc3_gadget_ep_cmd_params params;
  2099. int ret;
  2100. dep = dwc->eps[epnum];
  2101. if (!dep)
  2102. continue;
  2103. if (!(dep->flags & DWC3_EP_STALL))
  2104. continue;
  2105. dep->flags &= ~DWC3_EP_STALL;
  2106. memset(&params, 0, sizeof(params));
  2107. ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
  2108. DWC3_DEPCMD_CLEARSTALL, &params);
  2109. if (ret) {
  2110. dev_dbg(dwc->dev, "%s; send ep cmd CLEARSTALL failed",
  2111. dep->name);
  2112. dbg_event(dep->number, "ECLRSTALL", ret);
  2113. }
  2114. }
  2115. }
  2116. static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
  2117. {
  2118. int reg;
  2119. pr_info("usb:: %s\n", __func__);
  2120. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  2121. reg &= ~DWC3_DCTL_INITU1ENA;
  2122. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  2123. reg &= ~DWC3_DCTL_INITU2ENA;
  2124. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  2125. dbg_event(0xFF, "DISCONNECT", 0);
  2126. dwc3_disconnect_gadget(dwc);
  2127. dwc->gadget.speed = USB_SPEED_UNKNOWN;
  2128. dwc->setup_packet_pending = false;
  2129. }
  2130. static void dwc3_gadget_usb3_phy_suspend(struct dwc3 *dwc, int suspend)
  2131. {
  2132. u32 reg;
  2133. reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
  2134. if (suspend)
  2135. reg |= DWC3_GUSB3PIPECTL_SUSPHY;
  2136. else
  2137. reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
  2138. dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
  2139. }
  2140. static void dwc3_gadget_usb2_phy_suspend(struct dwc3 *dwc, int suspend)
  2141. {
  2142. u32 reg;
  2143. reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
  2144. if (suspend)
  2145. reg |= DWC3_GUSB2PHYCFG_SUSPHY;
  2146. else
  2147. reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
  2148. dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
  2149. }
  2150. static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
  2151. {
  2152. u32 reg;
  2153. struct dwc3_otg *dotg = dwc->dotg;
  2154. pr_info("usb:: %s\n", __func__);
  2155. /*
  2156. * WORKAROUND: DWC3 revisions <1.88a have an issue which
  2157. * would cause a missing Disconnect Event if there's a
  2158. * pending Setup Packet in the FIFO.
  2159. *
  2160. * There's no suggested workaround on the official Bug
  2161. * report, which states that "unless the driver/application
  2162. * is doing any special handling of a disconnect event,
  2163. * there is no functional issue".
  2164. *
  2165. * Unfortunately, it turns out that we _do_ some special
  2166. * handling of a disconnect event, namely complete all
  2167. * pending transfers, notify gadget driver of the
  2168. * disconnection, and so on.
  2169. *
  2170. * Our suggested workaround is to follow the Disconnect
  2171. * Event steps here, instead, based on a setup_packet_pending
  2172. * flag. Such flag gets set whenever we have a XferNotReady
  2173. * event on EP0 and gets cleared on XferComplete for the
  2174. * same endpoint.
  2175. *
  2176. * Refers to:
  2177. *
  2178. * STAR#9000466709: RTL: Device : Disconnect event not
  2179. * generated if setup packet pending in FIFO
  2180. */
  2181. if (dwc->revision < DWC3_REVISION_188A) {
  2182. if (dwc->setup_packet_pending)
  2183. dwc3_gadget_disconnect_interrupt(dwc);
  2184. }
  2185. dbg_event(0xFF, "BUS RST", 0);
  2186. /* after reset -> Default State */
  2187. dwc->dev_state = DWC3_DEFAULT_STATE;
  2188. /* Recent versions support automatic phy suspend and don't need this */
  2189. if (dwc->revision < DWC3_REVISION_194A) {
  2190. /* Resume PHYs */
  2191. dwc3_gadget_usb2_phy_suspend(dwc, false);
  2192. dwc3_gadget_usb3_phy_suspend(dwc, false);
  2193. }
  2194. if (dotg && dotg->otg.phy)
  2195. usb_phy_set_power(dotg->otg.phy, 0);
  2196. if (dwc->gadget.speed != USB_SPEED_UNKNOWN)
  2197. dwc3_disconnect_gadget(dwc);
  2198. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  2199. reg &= ~DWC3_DCTL_TSTCTRL_MASK;
  2200. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  2201. dwc->test_mode = false;
  2202. dwc3_stop_active_transfers(dwc);
  2203. dwc3_clear_stall_all_ep(dwc);
  2204. /* Reset device address to zero */
  2205. reg = dwc3_readl(dwc->regs, DWC3_DCFG);
  2206. reg &= ~(DWC3_DCFG_DEVADDR_MASK);
  2207. dwc3_writel(dwc->regs, DWC3_DCFG, reg);
  2208. }
  2209. static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
  2210. {
  2211. u32 reg;
  2212. u32 usb30_clock = DWC3_GCTL_CLK_BUS;
  2213. /*
  2214. * We change the clock only at SS but I dunno why I would want to do
  2215. * this. Maybe it becomes part of the power saving plan.
  2216. */
  2217. if (speed != DWC3_DSTS_SUPERSPEED)
  2218. return;
  2219. /*
  2220. * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
  2221. * each time on Connect Done.
  2222. */
  2223. if (!usb30_clock)
  2224. return;
  2225. reg = dwc3_readl(dwc->regs, DWC3_GCTL);
  2226. reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
  2227. dwc3_writel(dwc->regs, DWC3_GCTL, reg);
  2228. }
  2229. static void dwc3_gadget_phy_suspend(struct dwc3 *dwc, u8 speed)
  2230. {
  2231. switch (speed) {
  2232. case USB_SPEED_SUPER:
  2233. dwc3_gadget_usb2_phy_suspend(dwc, true);
  2234. break;
  2235. case USB_SPEED_HIGH:
  2236. case USB_SPEED_FULL:
  2237. case USB_SPEED_LOW:
  2238. dwc3_gadget_usb3_phy_suspend(dwc, true);
  2239. break;
  2240. }
  2241. }
  2242. static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
  2243. {
  2244. struct dwc3_gadget_ep_cmd_params params;
  2245. struct dwc3_ep *dep;
  2246. int ret;
  2247. u32 reg;
  2248. u8 speed;
  2249. dev_vdbg(dwc->dev, "%s\n", __func__);
  2250. memset(&params, 0x00, sizeof(params));
  2251. reg = dwc3_readl(dwc->regs, DWC3_DSTS);
  2252. speed = reg & DWC3_DSTS_CONNECTSPD;
  2253. dwc->speed = speed;
  2254. dwc3_update_ram_clk_sel(dwc, speed);
  2255. switch (speed) {
  2256. case DWC3_DCFG_SUPERSPEED:
  2257. /*
  2258. * WORKAROUND: DWC3 revisions <1.90a have an issue which
  2259. * would cause a missing USB3 Reset event.
  2260. *
  2261. * In such situations, we should force a USB3 Reset
  2262. * event by calling our dwc3_gadget_reset_interrupt()
  2263. * routine.
  2264. *
  2265. * Refers to:
  2266. *
  2267. * STAR#9000483510: RTL: SS : USB3 reset event may
  2268. * not be generated always when the link enters poll
  2269. */
  2270. if (dwc->revision < DWC3_REVISION_190A)
  2271. dwc3_gadget_reset_interrupt(dwc);
  2272. dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
  2273. dwc->gadget.ep0->maxpacket = 512;
  2274. dwc->gadget.speed = USB_SPEED_SUPER;
  2275. pr_info("usb:: %s (SS)\n", __func__);
  2276. break;
  2277. case DWC3_DCFG_HIGHSPEED:
  2278. dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
  2279. dwc->gadget.ep0->maxpacket = 64;
  2280. dwc->gadget.speed = USB_SPEED_HIGH;
  2281. pr_info("usb:: %s (HS)\n", __func__);
  2282. break;
  2283. case DWC3_DCFG_FULLSPEED2:
  2284. case DWC3_DCFG_FULLSPEED1:
  2285. dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
  2286. dwc->gadget.ep0->maxpacket = 64;
  2287. dwc->gadget.speed = USB_SPEED_FULL;
  2288. pr_info("usb:: %s (FS)\n", __func__);
  2289. break;
  2290. case DWC3_DCFG_LOWSPEED:
  2291. dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
  2292. dwc->gadget.ep0->maxpacket = 8;
  2293. dwc->gadget.speed = USB_SPEED_LOW;
  2294. pr_info("usb:: %s (LS)\n", __func__);
  2295. break;
  2296. }
  2297. /* Recent versions support automatic phy suspend and don't need this */
  2298. if (dwc->revision < DWC3_REVISION_194A) {
  2299. /* Suspend unneeded PHY */
  2300. dwc3_gadget_phy_suspend(dwc, dwc->gadget.speed);
  2301. }
  2302. dep = dwc->eps[0];
  2303. ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true);
  2304. if (ret) {
  2305. dev_err(dwc->dev, "failed to enable %s\n", dep->name);
  2306. return;
  2307. }
  2308. dep = dwc->eps[1];
  2309. ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true);
  2310. if (ret) {
  2311. dev_err(dwc->dev, "failed to enable %s\n", dep->name);
  2312. return;
  2313. }
  2314. /*
  2315. * Configure PHY via GUSB3PIPECTLn if required.
  2316. *
  2317. * Update GTXFIFOSIZn
  2318. *
  2319. * In both cases reset values should be sufficient.
  2320. */
  2321. #if defined(CONFIG_SEC_H_PROJECT) || defined(CONFIG_SEC_F_PROJECT) || defined(CONFIG_SEC_K_PROJECT)
  2322. /*
  2323. * Incase of H-Prj we want to Probe whether Host for super speed
  2324. * We Start the UDC with speed_limit always USB_SPEED_SUPER.
  2325. * if we connect at USB_SPEED_SUPER then we set that the ss_host_avail = 1
  2326. * We then change the speed_limit to USB_HIGH_SPEED and schedule
  2327. * a work item to reconnect at HIGH SPEED.
  2328. * In factory mode we do not need this logic. we should always connect USB3.0
  2329. */
  2330. #ifndef CONFIG_SEC_FACTORY
  2331. if (dwc->ss_host_avail == -1) {
  2332. if (dwc->gadget.speed == USB_SPEED_SUPER) {
  2333. dwc->ss_host_avail = 1;
  2334. dwc->speed_limit = USB_SPEED_HIGH;
  2335. dwc->reconnect = true;
  2336. } else {
  2337. printk(KERN_ERR"usb: Super speed host not available \n");
  2338. dwc->ss_host_avail = 0;
  2339. }
  2340. }
  2341. #endif
  2342. #endif
  2343. }
  2344. static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
  2345. {
  2346. pr_info("usb:: %s\n", __func__);
  2347. /* Only perform resume from L2 or Early suspend states */
  2348. if (dwc->link_state == DWC3_LINK_STATE_U3) {
  2349. dbg_event(0xFF, "WAKEUP", 0);
  2350. /*
  2351. * gadget_driver resume function might require some dwc3-gadget
  2352. * operations, such as ep_enable. Hence, dwc->lock must be released.
  2353. */
  2354. spin_unlock(&dwc->lock);
  2355. dwc->gadget_driver->resume(&dwc->gadget);
  2356. spin_lock(&dwc->lock);
  2357. }
  2358. dwc->link_state = DWC3_LINK_STATE_U0;
  2359. }
  2360. static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
  2361. unsigned int evtinfo)
  2362. {
  2363. enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
  2364. /*
  2365. * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
  2366. * on the link partner, the USB session might do multiple entry/exit
  2367. * of low power states before a transfer takes place.
  2368. *
  2369. * Due to this problem, we might experience lower throughput. The
  2370. * suggested workaround is to disable DCTL[12:9] bits if we're
  2371. * transitioning from U1/U2 to U0 and enable those bits again
  2372. * after a transfer completes and there are no pending transfers
  2373. * on any of the enabled endpoints.
  2374. *
  2375. * This is the first half of that workaround.
  2376. *
  2377. * Refers to:
  2378. *
  2379. * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
  2380. * core send LGO_Ux entering U0
  2381. */
  2382. if (dwc->revision < DWC3_REVISION_183A) {
  2383. if (next == DWC3_LINK_STATE_U0) {
  2384. u32 u1u2;
  2385. u32 reg;
  2386. switch (dwc->link_state) {
  2387. case DWC3_LINK_STATE_U1:
  2388. case DWC3_LINK_STATE_U2:
  2389. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  2390. u1u2 = reg & (DWC3_DCTL_INITU2ENA
  2391. | DWC3_DCTL_ACCEPTU2ENA
  2392. | DWC3_DCTL_INITU1ENA
  2393. | DWC3_DCTL_ACCEPTU1ENA);
  2394. if (!dwc->u1u2)
  2395. dwc->u1u2 = reg & u1u2;
  2396. reg &= ~u1u2;
  2397. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  2398. break;
  2399. default:
  2400. /* do nothing */
  2401. break;
  2402. }
  2403. }
  2404. }
  2405. if (next == DWC3_LINK_STATE_U0) {
  2406. if (dwc->link_state == DWC3_LINK_STATE_U3) {
  2407. dbg_event(0xFF, "RESUME", 0);
  2408. dwc->gadget_driver->resume(&dwc->gadget);
  2409. }
  2410. } else if (next == DWC3_LINK_STATE_U3) {
  2411. dbg_event(0xFF, "SUSPEND", 0);
  2412. /*
  2413. * gadget_driver suspend function might require some dwc3-gadget
  2414. * operations, such as ep_disable. Hence, dwc->lock must be
  2415. * released.
  2416. */
  2417. spin_unlock(&dwc->lock);
  2418. dwc->gadget_driver->suspend(&dwc->gadget);
  2419. spin_lock(&dwc->lock);
  2420. }
  2421. dwc->link_state = next;
  2422. dev_vdbg(dwc->dev, "%s link %d\n", __func__, dwc->link_state);
  2423. }
  2424. #if 0
  2425. static void dwc3_dump_reg_info(struct dwc3 *dwc)
  2426. {
  2427. dbg_event(0xFF, "REGDUMP", 0);
  2428. dbg_print_reg("GUSB3PIPCTL", dwc3_readl(dwc->regs,
  2429. DWC3_GUSB3PIPECTL(0)));
  2430. dbg_print_reg("GUSB2PHYCONFIG", dwc3_readl(dwc->regs,
  2431. DWC3_GUSB2PHYCFG(0)));
  2432. dbg_print_reg("GCTL", dwc3_readl(dwc->regs, DWC3_GCTL));
  2433. dbg_print_reg("GUCTL", dwc3_readl(dwc->regs, DWC3_GUCTL));
  2434. dbg_print_reg("GDBGLTSSM", dwc3_readl(dwc->regs, DWC3_GDBGLTSSM));
  2435. dbg_print_reg("DCFG", dwc3_readl(dwc->regs, DWC3_DCFG));
  2436. dbg_print_reg("DCTL", dwc3_readl(dwc->regs, DWC3_DCTL));
  2437. dbg_print_reg("DEVTEN", dwc3_readl(dwc->regs, DWC3_DEVTEN));
  2438. dbg_print_reg("DSTS", dwc3_readl(dwc->regs, DWC3_DSTS));
  2439. dbg_print_reg("DALPENA", dwc3_readl(dwc->regs, DWC3_DALEPENA));
  2440. dbg_print_reg("DGCMD", dwc3_readl(dwc->regs, DWC3_DGCMD));
  2441. dbg_print_reg("OCFG", dwc3_readl(dwc->regs, DWC3_OCFG));
  2442. dbg_print_reg("OCTL", dwc3_readl(dwc->regs, DWC3_OCTL));
  2443. dbg_print_reg("OEVT", dwc3_readl(dwc->regs, DWC3_OEVT));
  2444. dbg_print_reg("OSTS", dwc3_readl(dwc->regs, DWC3_OSTS));
  2445. dwc3_notify_event(dwc, DWC3_CONTROLLER_ERROR_EVENT);
  2446. }
  2447. #endif
  2448. static void dwc3_gadget_interrupt(struct dwc3 *dwc,
  2449. const struct dwc3_event_devt *event)
  2450. {
  2451. switch (event->type) {
  2452. case DWC3_DEVICE_EVENT_DISCONNECT:
  2453. dwc3_gadget_disconnect_interrupt(dwc);
  2454. break;
  2455. case DWC3_DEVICE_EVENT_RESET:
  2456. dwc3_gadget_reset_interrupt(dwc);
  2457. break;
  2458. case DWC3_DEVICE_EVENT_CONNECT_DONE:
  2459. dwc3_gadget_conndone_interrupt(dwc);
  2460. break;
  2461. case DWC3_DEVICE_EVENT_WAKEUP:
  2462. dwc3_gadget_wakeup_interrupt(dwc);
  2463. break;
  2464. case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
  2465. dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
  2466. break;
  2467. case DWC3_DEVICE_EVENT_EOPF:
  2468. dev_vdbg(dwc->dev, "End of Periodic Frame\n");
  2469. break;
  2470. case DWC3_DEVICE_EVENT_SOF:
  2471. dev_vdbg(dwc->dev, "Start of Periodic Frame\n");
  2472. break;
  2473. case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
  2474. dbg_event(0xFF, "ERROR", 0);
  2475. dev_vdbg(dwc->dev, "Erratic Error\n");
  2476. break;
  2477. case DWC3_DEVICE_EVENT_CMD_CMPL:
  2478. dev_vdbg(dwc->dev, "Command Complete\n");
  2479. break;
  2480. case DWC3_DEVICE_EVENT_OVERFLOW:
  2481. dbg_event(0xFF, "OVERFL", 0);
  2482. dev_vdbg(dwc->dev, "Overflow\n");
  2483. /*
  2484. * Controllers prior to 2.30a revision has a bug where
  2485. * Overflow Event may overwrite an unacknowledged event
  2486. * in the event buffer. The severity of the issue depends
  2487. * on the overwritten event type. Add a warning message
  2488. * saying that an event is overwritten.
  2489. *
  2490. * TODO: In future we may need to see if we can re-enumerate
  2491. * with host.
  2492. */
  2493. if (dwc->revision < DWC3_REVISION_230A)
  2494. dev_warn(dwc->dev, "Unacknowledged event overwritten\n");
  2495. break;
  2496. case DWC3_DEVICE_EVENT_VENDOR_DEV_TEST_LMP:
  2497. /*
  2498. * Controllers prior to 2.30a revision has a bug, due to which
  2499. * a vendor device test LMP event can not be filtered. But
  2500. * this event is not handled in the current code. This is a
  2501. * special event and 8 bytes of data will follow the event.
  2502. * Handling this event is tricky when event buffer is almost
  2503. * full. Moreover this event will not occur in normal scenario
  2504. * and can only happen with special hosts in testing scenarios.
  2505. * Add a warning message to indicate that this event is received
  2506. * which means that event buffer might have corrupted.
  2507. */
  2508. dbg_event(0xFF, "TSTLMP", 0);
  2509. if (dwc->revision < DWC3_REVISION_230A)
  2510. dev_warn(dwc->dev, "Vendor Device Test LMP Received\n");
  2511. break;
  2512. default:
  2513. pr_info("usb:: [%s] UNKNOWN IRQ %d\n", __func__, event->type);
  2514. }
  2515. }
  2516. static void dwc3_process_event_entry(struct dwc3 *dwc,
  2517. const union dwc3_event *event)
  2518. {
  2519. /* Endpoint IRQ, handle it and return early */
  2520. if (event->type.is_devspec == 0) {
  2521. /* depevt */
  2522. return dwc3_endpoint_interrupt(dwc, &event->depevt);
  2523. }
  2524. switch (event->type.type) {
  2525. case DWC3_EVENT_TYPE_DEV:
  2526. dwc3_gadget_interrupt(dwc, &event->devt);
  2527. break;
  2528. /* REVISIT what to do with Carkit and I2C events ? */
  2529. default:
  2530. dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
  2531. }
  2532. }
  2533. static irqreturn_t dwc3_process_event_buf(struct dwc3 *dwc, u32 buf)
  2534. {
  2535. struct dwc3_event_buffer *evt;
  2536. int left;
  2537. u32 count;
  2538. count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(buf));
  2539. count &= DWC3_GEVNTCOUNT_MASK;
  2540. if (!count)
  2541. return IRQ_NONE;
  2542. evt = dwc->ev_buffs[buf];
  2543. left = count;
  2544. while (left > 0) {
  2545. union dwc3_event event;
  2546. event.raw = *(u32 *) (evt->buf + evt->lpos);
  2547. dwc3_process_event_entry(dwc, &event);
  2548. /*
  2549. * XXX we wrap around correctly to the next entry as almost all
  2550. * entries are 4 bytes in size. There is one entry which has 12
  2551. * bytes which is a regular entry followed by 8 bytes data. ATM
  2552. * I don't know how things are organized if were get next to the
  2553. * a boundary so I worry about that once we try to handle that.
  2554. */
  2555. evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
  2556. left -= 4;
  2557. dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(buf), 4);
  2558. }
  2559. #if defined(CONFIG_SEC_H_PROJECT) || defined(CONFIG_SEC_F_PROJECT) || defined(CONFIG_SEC_K_PROJECT)
  2560. /* Schedule the reconnect work event */
  2561. if (dwc->reconnect) {
  2562. dwc->reconnect = false;
  2563. WORK_SCHEDULE(dwc);
  2564. }
  2565. #endif
  2566. return IRQ_HANDLED;
  2567. }
  2568. static irqreturn_t dwc3_interrupt(int irq, void *_dwc)
  2569. {
  2570. struct dwc3 *dwc = _dwc;
  2571. int i;
  2572. irqreturn_t ret = IRQ_NONE;
  2573. spin_lock(&dwc->lock);
  2574. for (i = 0; i < dwc->num_event_buffers; i++) {
  2575. irqreturn_t status;
  2576. status = dwc3_process_event_buf(dwc, i);
  2577. if (status == IRQ_HANDLED)
  2578. ret = status;
  2579. }
  2580. spin_unlock(&dwc->lock);
  2581. return ret;
  2582. }
  2583. /**
  2584. * dwc3_gadget_init - Initializes gadget related registers
  2585. * @dwc: pointer to our controller context structure
  2586. *
  2587. * Returns 0 on success otherwise negative errno.
  2588. */
  2589. int __devinit dwc3_gadget_init(struct dwc3 *dwc)
  2590. {
  2591. u32 reg;
  2592. int ret;
  2593. int irq;
  2594. dwc->ctrl_req = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
  2595. &dwc->ctrl_req_addr, GFP_KERNEL);
  2596. if (!dwc->ctrl_req) {
  2597. dev_err(dwc->dev, "failed to allocate ctrl request\n");
  2598. ret = -ENOMEM;
  2599. goto err0;
  2600. }
  2601. dwc->ep0_trb = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
  2602. &dwc->ep0_trb_addr, GFP_KERNEL);
  2603. if (!dwc->ep0_trb) {
  2604. dev_err(dwc->dev, "failed to allocate ep0 trb\n");
  2605. ret = -ENOMEM;
  2606. goto err1;
  2607. }
  2608. dwc->setup_buf = kzalloc(DWC3_EP0_BOUNCE_SIZE, GFP_KERNEL);
  2609. if (!dwc->setup_buf) {
  2610. dev_err(dwc->dev, "failed to allocate setup buffer\n");
  2611. ret = -ENOMEM;
  2612. goto err2;
  2613. }
  2614. dwc->ep0_bounce = dma_alloc_coherent(dwc->dev,
  2615. DWC3_EP0_BOUNCE_SIZE, &dwc->ep0_bounce_addr,
  2616. GFP_KERNEL);
  2617. if (!dwc->ep0_bounce) {
  2618. dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
  2619. ret = -ENOMEM;
  2620. goto err3;
  2621. }
  2622. dev_set_name(&dwc->gadget.dev, "gadget");
  2623. dwc->gadget.ops = &dwc3_gadget_ops;
  2624. #if defined(CONFIG_SEC_LT03_PROJECT) || defined(CONFIG_SEC_MONDRIAN_PROJECT)\
  2625. || defined(CONFIG_SEC_KS01_PROJECT) || defined(CONFIG_SEC_PICASSO_PROJECT)\
  2626. || defined(CONFIG_SEC_KACTIVE_PROJECT) || defined(CONFIG_SEC_FRESCO_PROJECT)\
  2627. || defined(CONFIG_SEC_KSPORTS_PROJECT) || defined(CONFIG_SEC_JACTIVE_PROJECT)\
  2628. || defined(CONFIG_SEC_S_PROJECT) || defined(CONFIG_SEC_PATEK_PROJECT)\
  2629. || defined(CONFIG_SEC_CHAGALL_PROJECT) || defined(CONFIG_SEC_KLIMT_PROJECT)\
  2630. || defined(CONFIG_MACH_JS01LTEDCM) || defined(CONFIG_MACH_JSGLTE_CHN_CMCC)
  2631. dwc->gadget.max_speed = USB_SPEED_HIGH;
  2632. #else
  2633. dwc->gadget.max_speed = USB_SPEED_SUPER;
  2634. #endif
  2635. dwc->gadget.speed = USB_SPEED_UNKNOWN;
  2636. dwc->gadget.dev.parent = dwc->dev;
  2637. dwc->gadget.sg_supported = true;
  2638. dma_set_coherent_mask(&dwc->gadget.dev, dwc->dev->coherent_dma_mask);
  2639. dwc->gadget.dev.dma_parms = dwc->dev->dma_parms;
  2640. dwc->gadget.dev.dma_mask = dwc->dev->dma_mask;
  2641. dwc->gadget.dev.release = dwc3_gadget_release;
  2642. dwc->gadget.name = "dwc3-gadget";
  2643. /*
  2644. * REVISIT: Here we should clear all pending IRQs to be
  2645. * sure we're starting from a well known location.
  2646. */
  2647. ret = dwc3_gadget_init_endpoints(dwc);
  2648. if (ret)
  2649. goto err4;
  2650. irq = platform_get_irq(to_platform_device(dwc->dev), 0);
  2651. ret = request_irq(irq, dwc3_interrupt, IRQF_SHARED,
  2652. "dwc3", dwc);
  2653. if (ret) {
  2654. dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
  2655. irq, ret);
  2656. goto err5;
  2657. }
  2658. /* Enable all but Start and End of Frame IRQs */
  2659. reg = (DWC3_DEVTEN_EVNTOVERFLOWEN |
  2660. DWC3_DEVTEN_CMDCMPLTEN |
  2661. DWC3_DEVTEN_ERRTICERREN |
  2662. DWC3_DEVTEN_WKUPEVTEN |
  2663. DWC3_DEVTEN_ULSTCNGEN |
  2664. DWC3_DEVTEN_CONNECTDONEEN |
  2665. DWC3_DEVTEN_USBRSTEN |
  2666. DWC3_DEVTEN_DISCONNEVTEN);
  2667. dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
  2668. /* Enable USB2 LPM and automatic phy suspend only on recent versions */
  2669. if (dwc->revision >= DWC3_REVISION_194A) {
  2670. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  2671. reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
  2672. /* TODO: This should be configurable */
  2673. reg |= DWC3_DCTL_HIRD_THRES(28);
  2674. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  2675. /*
  2676. * Clear autosuspend bit in dwc3 register for USB2. It will be
  2677. * enabled before setting run/stop bit.
  2678. */
  2679. dwc3_gadget_usb2_phy_suspend(dwc, false);
  2680. dwc3_gadget_usb3_phy_suspend(dwc, true);
  2681. }
  2682. ret = device_register(&dwc->gadget.dev);
  2683. if (ret) {
  2684. dev_err(dwc->dev, "failed to register gadget device\n");
  2685. put_device(&dwc->gadget.dev);
  2686. goto err6;
  2687. }
  2688. ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
  2689. if (ret) {
  2690. dev_err(dwc->dev, "failed to register udc\n");
  2691. goto err7;
  2692. }
  2693. if (dwc->dotg) {
  2694. /* dwc3 otg driver is active (DRD mode + SRPSupport=1) */
  2695. ret = otg_set_peripheral(&dwc->dotg->otg, &dwc->gadget);
  2696. if (ret) {
  2697. dev_err(dwc->dev, "failed to set peripheral to otg\n");
  2698. goto err7;
  2699. }
  2700. } else {
  2701. pm_runtime_no_callbacks(&dwc->gadget.dev);
  2702. pm_runtime_set_active(&dwc->gadget.dev);
  2703. pm_runtime_enable(&dwc->gadget.dev);
  2704. pm_runtime_get(&dwc->gadget.dev);
  2705. }
  2706. return 0;
  2707. err7:
  2708. device_unregister(&dwc->gadget.dev);
  2709. err6:
  2710. dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
  2711. free_irq(irq, dwc);
  2712. err5:
  2713. dwc3_gadget_free_endpoints(dwc);
  2714. err4:
  2715. dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
  2716. dwc->ep0_bounce, dwc->ep0_bounce_addr);
  2717. err3:
  2718. kfree(dwc->setup_buf);
  2719. err2:
  2720. dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
  2721. dwc->ep0_trb, dwc->ep0_trb_addr);
  2722. err1:
  2723. dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
  2724. dwc->ctrl_req, dwc->ctrl_req_addr);
  2725. err0:
  2726. return ret;
  2727. }
  2728. void dwc3_gadget_exit(struct dwc3 *dwc)
  2729. {
  2730. int irq;
  2731. if (dwc->dotg) {
  2732. pm_runtime_put(&dwc->gadget.dev);
  2733. pm_runtime_disable(&dwc->gadget.dev);
  2734. }
  2735. usb_del_gadget_udc(&dwc->gadget);
  2736. irq = platform_get_irq(to_platform_device(dwc->dev), 0);
  2737. dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
  2738. free_irq(irq, dwc);
  2739. dwc3_gadget_free_endpoints(dwc);
  2740. dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
  2741. dwc->ep0_bounce, dwc->ep0_bounce_addr);
  2742. kfree(dwc->setup_buf);
  2743. dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
  2744. dwc->ep0_trb, dwc->ep0_trb_addr);
  2745. dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
  2746. dwc->ctrl_req, dwc->ctrl_req_addr);
  2747. device_unregister(&dwc->gadget.dev);
  2748. }
  2749. #if defined(CONFIG_SEC_H_PROJECT) || defined(CONFIG_SEC_F_PROJECT) || defined(CONFIG_SEC_K_PROJECT)
  2750. int sec_set_speedlimit(struct usb_gadget *gadget,
  2751. enum usb_device_speed speed)
  2752. {
  2753. struct dwc3 *dwc;
  2754. unsigned long flags;
  2755. if (!gadget)
  2756. return -1;
  2757. if (speed > gadget->max_speed || speed < USB_SPEED_FULL)
  2758. return -1;
  2759. dwc = container_of(gadget, struct dwc3, gadget);
  2760. spin_lock_irqsave(&dwc->lock, flags);
  2761. dwc->speed_limit = speed;
  2762. spin_unlock_irqrestore(&dwc->lock, flags);
  2763. return 0;
  2764. }
  2765. EXPORT_SYMBOL_GPL(sec_set_speedlimit);
  2766. int sec_get_ss_host_available(struct usb_gadget *gadget)
  2767. {
  2768. struct dwc3 *dwc;
  2769. int ss_host_avail;
  2770. unsigned long flags;
  2771. if (!gadget)
  2772. return -1;
  2773. dwc = container_of(gadget, struct dwc3, gadget);
  2774. spin_lock_irqsave(&dwc->lock, flags);
  2775. ss_host_avail = dwc->ss_host_avail;
  2776. spin_unlock_irqrestore(&dwc->lock, flags);
  2777. dev_dbg(dwc->dev,"Superspeed Host avail(%d) \n",ss_host_avail);
  2778. return ss_host_avail;
  2779. }
  2780. EXPORT_SYMBOL_GPL(sec_get_ss_host_available);
  2781. #endif