hcd.c 84 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997
  1. /*
  2. * hcd.c - DesignWare HS OTG Controller host-mode routines
  3. *
  4. * Copyright (C) 2004-2013 Synopsys, Inc.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions, and the following disclaimer,
  11. * without modification.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. The names of the above-listed copyright holders may not be used
  16. * to endorse or promote products derived from this software without
  17. * specific prior written permission.
  18. *
  19. * ALTERNATIVELY, this software may be distributed under the terms of the
  20. * GNU General Public License ("GPL") as published by the Free Software
  21. * Foundation; either version 2 of the License, or (at your option) any
  22. * later version.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  25. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  26. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  27. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  28. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  29. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  30. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  31. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  32. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  33. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  34. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. /*
  37. * This file contains the core HCD code, and implements the Linux hc_driver
  38. * API
  39. */
  40. #include <linux/kernel.h>
  41. #include <linux/module.h>
  42. #include <linux/spinlock.h>
  43. #include <linux/interrupt.h>
  44. #include <linux/dma-mapping.h>
  45. #include <linux/delay.h>
  46. #include <linux/io.h>
  47. #include <linux/slab.h>
  48. #include <linux/usb.h>
  49. #include <linux/usb/hcd.h>
  50. #include <linux/usb/ch11.h>
  51. #include "core.h"
  52. #include "hcd.h"
  53. /**
  54. * dwc2_dump_channel_info() - Prints the state of a host channel
  55. *
  56. * @hsotg: Programming view of DWC_otg controller
  57. * @chan: Pointer to the channel to dump
  58. *
  59. * Must be called with interrupt disabled and spinlock held
  60. *
  61. * NOTE: This function will be removed once the peripheral controller code
  62. * is integrated and the driver is stable
  63. */
  64. static void dwc2_dump_channel_info(struct dwc2_hsotg *hsotg,
  65. struct dwc2_host_chan *chan)
  66. {
  67. #ifdef VERBOSE_DEBUG
  68. int num_channels = hsotg->core_params->host_channels;
  69. struct dwc2_qh *qh;
  70. u32 hcchar;
  71. u32 hcsplt;
  72. u32 hctsiz;
  73. u32 hc_dma;
  74. int i;
  75. if (chan == NULL)
  76. return;
  77. hcchar = readl(hsotg->regs + HCCHAR(chan->hc_num));
  78. hcsplt = readl(hsotg->regs + HCSPLT(chan->hc_num));
  79. hctsiz = readl(hsotg->regs + HCTSIZ(chan->hc_num));
  80. hc_dma = readl(hsotg->regs + HCDMA(chan->hc_num));
  81. dev_dbg(hsotg->dev, " Assigned to channel %p:\n", chan);
  82. dev_dbg(hsotg->dev, " hcchar 0x%08x, hcsplt 0x%08x\n",
  83. hcchar, hcsplt);
  84. dev_dbg(hsotg->dev, " hctsiz 0x%08x, hc_dma 0x%08x\n",
  85. hctsiz, hc_dma);
  86. dev_dbg(hsotg->dev, " dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
  87. chan->dev_addr, chan->ep_num, chan->ep_is_in);
  88. dev_dbg(hsotg->dev, " ep_type: %d\n", chan->ep_type);
  89. dev_dbg(hsotg->dev, " max_packet: %d\n", chan->max_packet);
  90. dev_dbg(hsotg->dev, " data_pid_start: %d\n", chan->data_pid_start);
  91. dev_dbg(hsotg->dev, " xfer_started: %d\n", chan->xfer_started);
  92. dev_dbg(hsotg->dev, " halt_status: %d\n", chan->halt_status);
  93. dev_dbg(hsotg->dev, " xfer_buf: %p\n", chan->xfer_buf);
  94. dev_dbg(hsotg->dev, " xfer_dma: %08lx\n",
  95. (unsigned long)chan->xfer_dma);
  96. dev_dbg(hsotg->dev, " xfer_len: %d\n", chan->xfer_len);
  97. dev_dbg(hsotg->dev, " qh: %p\n", chan->qh);
  98. dev_dbg(hsotg->dev, " NP inactive sched:\n");
  99. list_for_each_entry(qh, &hsotg->non_periodic_sched_inactive,
  100. qh_list_entry)
  101. dev_dbg(hsotg->dev, " %p\n", qh);
  102. dev_dbg(hsotg->dev, " NP active sched:\n");
  103. list_for_each_entry(qh, &hsotg->non_periodic_sched_active,
  104. qh_list_entry)
  105. dev_dbg(hsotg->dev, " %p\n", qh);
  106. dev_dbg(hsotg->dev, " Channels:\n");
  107. for (i = 0; i < num_channels; i++) {
  108. struct dwc2_host_chan *chan = hsotg->hc_ptr_array[i];
  109. dev_dbg(hsotg->dev, " %2d: %p\n", i, chan);
  110. }
  111. #endif /* VERBOSE_DEBUG */
  112. }
  113. /*
  114. * Processes all the URBs in a single list of QHs. Completes them with
  115. * -ETIMEDOUT and frees the QTD.
  116. *
  117. * Must be called with interrupt disabled and spinlock held
  118. */
  119. static void dwc2_kill_urbs_in_qh_list(struct dwc2_hsotg *hsotg,
  120. struct list_head *qh_list)
  121. {
  122. struct dwc2_qh *qh, *qh_tmp;
  123. struct dwc2_qtd *qtd, *qtd_tmp;
  124. list_for_each_entry_safe(qh, qh_tmp, qh_list, qh_list_entry) {
  125. list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list,
  126. qtd_list_entry) {
  127. dwc2_host_complete(hsotg, qtd, -ETIMEDOUT);
  128. dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
  129. }
  130. }
  131. }
  132. static void dwc2_qh_list_free(struct dwc2_hsotg *hsotg,
  133. struct list_head *qh_list)
  134. {
  135. struct dwc2_qtd *qtd, *qtd_tmp;
  136. struct dwc2_qh *qh, *qh_tmp;
  137. unsigned long flags;
  138. if (!qh_list->next)
  139. /* The list hasn't been initialized yet */
  140. return;
  141. spin_lock_irqsave(&hsotg->lock, flags);
  142. /* Ensure there are no QTDs or URBs left */
  143. dwc2_kill_urbs_in_qh_list(hsotg, qh_list);
  144. list_for_each_entry_safe(qh, qh_tmp, qh_list, qh_list_entry) {
  145. dwc2_hcd_qh_unlink(hsotg, qh);
  146. /* Free each QTD in the QH's QTD list */
  147. list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list,
  148. qtd_list_entry)
  149. dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
  150. spin_unlock_irqrestore(&hsotg->lock, flags);
  151. dwc2_hcd_qh_free(hsotg, qh);
  152. spin_lock_irqsave(&hsotg->lock, flags);
  153. }
  154. spin_unlock_irqrestore(&hsotg->lock, flags);
  155. }
  156. /*
  157. * Responds with an error status of -ETIMEDOUT to all URBs in the non-periodic
  158. * and periodic schedules. The QTD associated with each URB is removed from
  159. * the schedule and freed. This function may be called when a disconnect is
  160. * detected or when the HCD is being stopped.
  161. *
  162. * Must be called with interrupt disabled and spinlock held
  163. */
  164. static void dwc2_kill_all_urbs(struct dwc2_hsotg *hsotg)
  165. {
  166. dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->non_periodic_sched_inactive);
  167. dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->non_periodic_sched_active);
  168. dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_inactive);
  169. dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_ready);
  170. dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_assigned);
  171. dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_queued);
  172. }
  173. /**
  174. * dwc2_hcd_start() - Starts the HCD when switching to Host mode
  175. *
  176. * @hsotg: Pointer to struct dwc2_hsotg
  177. */
  178. void dwc2_hcd_start(struct dwc2_hsotg *hsotg)
  179. {
  180. u32 hprt0;
  181. if (hsotg->op_state == OTG_STATE_B_HOST) {
  182. /*
  183. * Reset the port. During a HNP mode switch the reset
  184. * needs to occur within 1ms and have a duration of at
  185. * least 50ms.
  186. */
  187. hprt0 = dwc2_read_hprt0(hsotg);
  188. hprt0 |= HPRT0_RST;
  189. writel(hprt0, hsotg->regs + HPRT0);
  190. }
  191. queue_delayed_work(hsotg->wq_otg, &hsotg->start_work,
  192. msecs_to_jiffies(50));
  193. }
  194. /* Must be called with interrupt disabled and spinlock held */
  195. static void dwc2_hcd_cleanup_channels(struct dwc2_hsotg *hsotg)
  196. {
  197. int num_channels = hsotg->core_params->host_channels;
  198. struct dwc2_host_chan *channel;
  199. u32 hcchar;
  200. int i;
  201. if (hsotg->core_params->dma_enable <= 0) {
  202. /* Flush out any channel requests in slave mode */
  203. for (i = 0; i < num_channels; i++) {
  204. channel = hsotg->hc_ptr_array[i];
  205. if (!list_empty(&channel->hc_list_entry))
  206. continue;
  207. hcchar = readl(hsotg->regs + HCCHAR(i));
  208. if (hcchar & HCCHAR_CHENA) {
  209. hcchar &= ~(HCCHAR_CHENA | HCCHAR_EPDIR);
  210. hcchar |= HCCHAR_CHDIS;
  211. writel(hcchar, hsotg->regs + HCCHAR(i));
  212. }
  213. }
  214. }
  215. for (i = 0; i < num_channels; i++) {
  216. channel = hsotg->hc_ptr_array[i];
  217. if (!list_empty(&channel->hc_list_entry))
  218. continue;
  219. hcchar = readl(hsotg->regs + HCCHAR(i));
  220. if (hcchar & HCCHAR_CHENA) {
  221. /* Halt the channel */
  222. hcchar |= HCCHAR_CHDIS;
  223. writel(hcchar, hsotg->regs + HCCHAR(i));
  224. }
  225. dwc2_hc_cleanup(hsotg, channel);
  226. list_add_tail(&channel->hc_list_entry, &hsotg->free_hc_list);
  227. /*
  228. * Added for Descriptor DMA to prevent channel double cleanup in
  229. * release_channel_ddma(), which is called from ep_disable when
  230. * device disconnects
  231. */
  232. channel->qh = NULL;
  233. }
  234. }
  235. /**
  236. * dwc2_hcd_disconnect() - Handles disconnect of the HCD
  237. *
  238. * @hsotg: Pointer to struct dwc2_hsotg
  239. *
  240. * Must be called with interrupt disabled and spinlock held
  241. */
  242. void dwc2_hcd_disconnect(struct dwc2_hsotg *hsotg)
  243. {
  244. u32 intr;
  245. /* Set status flags for the hub driver */
  246. hsotg->flags.b.port_connect_status_change = 1;
  247. hsotg->flags.b.port_connect_status = 0;
  248. /*
  249. * Shutdown any transfers in process by clearing the Tx FIFO Empty
  250. * interrupt mask and status bits and disabling subsequent host
  251. * channel interrupts.
  252. */
  253. intr = readl(hsotg->regs + GINTMSK);
  254. intr &= ~(GINTSTS_NPTXFEMP | GINTSTS_PTXFEMP | GINTSTS_HCHINT);
  255. writel(intr, hsotg->regs + GINTMSK);
  256. intr = GINTSTS_NPTXFEMP | GINTSTS_PTXFEMP | GINTSTS_HCHINT;
  257. writel(intr, hsotg->regs + GINTSTS);
  258. /*
  259. * Turn off the vbus power only if the core has transitioned to device
  260. * mode. If still in host mode, need to keep power on to detect a
  261. * reconnection.
  262. */
  263. if (dwc2_is_device_mode(hsotg)) {
  264. if (hsotg->op_state != OTG_STATE_A_SUSPEND) {
  265. dev_dbg(hsotg->dev, "Disconnect: PortPower off\n");
  266. writel(0, hsotg->regs + HPRT0);
  267. }
  268. dwc2_disable_host_interrupts(hsotg);
  269. }
  270. /* Respond with an error status to all URBs in the schedule */
  271. dwc2_kill_all_urbs(hsotg);
  272. if (dwc2_is_host_mode(hsotg))
  273. /* Clean up any host channels that were in use */
  274. dwc2_hcd_cleanup_channels(hsotg);
  275. dwc2_host_disconnect(hsotg);
  276. }
  277. /**
  278. * dwc2_hcd_rem_wakeup() - Handles Remote Wakeup
  279. *
  280. * @hsotg: Pointer to struct dwc2_hsotg
  281. */
  282. static void dwc2_hcd_rem_wakeup(struct dwc2_hsotg *hsotg)
  283. {
  284. if (hsotg->lx_state == DWC2_L2)
  285. hsotg->flags.b.port_suspend_change = 1;
  286. else
  287. hsotg->flags.b.port_l1_change = 1;
  288. }
  289. /**
  290. * dwc2_hcd_stop() - Halts the DWC_otg host mode operations in a clean manner
  291. *
  292. * @hsotg: Pointer to struct dwc2_hsotg
  293. *
  294. * Must be called with interrupt disabled and spinlock held
  295. */
  296. void dwc2_hcd_stop(struct dwc2_hsotg *hsotg)
  297. {
  298. dev_dbg(hsotg->dev, "DWC OTG HCD STOP\n");
  299. /*
  300. * The root hub should be disconnected before this function is called.
  301. * The disconnect will clear the QTD lists (via ..._hcd_urb_dequeue)
  302. * and the QH lists (via ..._hcd_endpoint_disable).
  303. */
  304. /* Turn off all host-specific interrupts */
  305. dwc2_disable_host_interrupts(hsotg);
  306. /* Turn off the vbus power */
  307. dev_dbg(hsotg->dev, "PortPower off\n");
  308. writel(0, hsotg->regs + HPRT0);
  309. }
  310. static int dwc2_hcd_urb_enqueue(struct dwc2_hsotg *hsotg,
  311. struct dwc2_hcd_urb *urb, void **ep_handle,
  312. gfp_t mem_flags)
  313. {
  314. struct dwc2_qtd *qtd;
  315. unsigned long flags;
  316. u32 intr_mask;
  317. int retval;
  318. int dev_speed;
  319. if (!hsotg->flags.b.port_connect_status) {
  320. /* No longer connected */
  321. dev_err(hsotg->dev, "Not connected\n");
  322. return -ENODEV;
  323. }
  324. dev_speed = dwc2_host_get_speed(hsotg, urb->priv);
  325. /* Some configurations cannot support LS traffic on a FS root port */
  326. if ((dev_speed == USB_SPEED_LOW) &&
  327. (hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED) &&
  328. (hsotg->hw_params.hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI)) {
  329. u32 hprt0 = readl(hsotg->regs + HPRT0);
  330. u32 prtspd = (hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT;
  331. if (prtspd == HPRT0_SPD_FULL_SPEED)
  332. return -ENODEV;
  333. }
  334. qtd = kzalloc(sizeof(*qtd), mem_flags);
  335. if (!qtd)
  336. return -ENOMEM;
  337. dwc2_hcd_qtd_init(qtd, urb);
  338. retval = dwc2_hcd_qtd_add(hsotg, qtd, (struct dwc2_qh **)ep_handle,
  339. mem_flags);
  340. if (retval) {
  341. dev_err(hsotg->dev,
  342. "DWC OTG HCD URB Enqueue failed adding QTD. Error status %d\n",
  343. retval);
  344. kfree(qtd);
  345. return retval;
  346. }
  347. intr_mask = readl(hsotg->regs + GINTMSK);
  348. if (!(intr_mask & GINTSTS_SOF)) {
  349. enum dwc2_transaction_type tr_type;
  350. if (qtd->qh->ep_type == USB_ENDPOINT_XFER_BULK &&
  351. !(qtd->urb->flags & URB_GIVEBACK_ASAP))
  352. /*
  353. * Do not schedule SG transactions until qtd has
  354. * URB_GIVEBACK_ASAP set
  355. */
  356. return 0;
  357. spin_lock_irqsave(&hsotg->lock, flags);
  358. tr_type = dwc2_hcd_select_transactions(hsotg);
  359. if (tr_type != DWC2_TRANSACTION_NONE)
  360. dwc2_hcd_queue_transactions(hsotg, tr_type);
  361. spin_unlock_irqrestore(&hsotg->lock, flags);
  362. }
  363. return 0;
  364. }
  365. /* Must be called with interrupt disabled and spinlock held */
  366. static int dwc2_hcd_urb_dequeue(struct dwc2_hsotg *hsotg,
  367. struct dwc2_hcd_urb *urb)
  368. {
  369. struct dwc2_qh *qh;
  370. struct dwc2_qtd *urb_qtd;
  371. urb_qtd = urb->qtd;
  372. if (!urb_qtd) {
  373. dev_dbg(hsotg->dev, "## Urb QTD is NULL ##\n");
  374. return -EINVAL;
  375. }
  376. qh = urb_qtd->qh;
  377. if (!qh) {
  378. dev_dbg(hsotg->dev, "## Urb QTD QH is NULL ##\n");
  379. return -EINVAL;
  380. }
  381. urb->priv = NULL;
  382. if (urb_qtd->in_process && qh->channel) {
  383. dwc2_dump_channel_info(hsotg, qh->channel);
  384. /* The QTD is in process (it has been assigned to a channel) */
  385. if (hsotg->flags.b.port_connect_status)
  386. /*
  387. * If still connected (i.e. in host mode), halt the
  388. * channel so it can be used for other transfers. If
  389. * no longer connected, the host registers can't be
  390. * written to halt the channel since the core is in
  391. * device mode.
  392. */
  393. dwc2_hc_halt(hsotg, qh->channel,
  394. DWC2_HC_XFER_URB_DEQUEUE);
  395. }
  396. /*
  397. * Free the QTD and clean up the associated QH. Leave the QH in the
  398. * schedule if it has any remaining QTDs.
  399. */
  400. if (hsotg->core_params->dma_desc_enable <= 0) {
  401. u8 in_process = urb_qtd->in_process;
  402. dwc2_hcd_qtd_unlink_and_free(hsotg, urb_qtd, qh);
  403. if (in_process) {
  404. dwc2_hcd_qh_deactivate(hsotg, qh, 0);
  405. qh->channel = NULL;
  406. } else if (list_empty(&qh->qtd_list)) {
  407. dwc2_hcd_qh_unlink(hsotg, qh);
  408. }
  409. } else {
  410. dwc2_hcd_qtd_unlink_and_free(hsotg, urb_qtd, qh);
  411. }
  412. return 0;
  413. }
  414. /* Must NOT be called with interrupt disabled or spinlock held */
  415. static int dwc2_hcd_endpoint_disable(struct dwc2_hsotg *hsotg,
  416. struct usb_host_endpoint *ep, int retry)
  417. {
  418. struct dwc2_qtd *qtd, *qtd_tmp;
  419. struct dwc2_qh *qh;
  420. unsigned long flags;
  421. int rc;
  422. spin_lock_irqsave(&hsotg->lock, flags);
  423. qh = ep->hcpriv;
  424. if (!qh) {
  425. rc = -EINVAL;
  426. goto err;
  427. }
  428. while (!list_empty(&qh->qtd_list) && retry--) {
  429. if (retry == 0) {
  430. dev_err(hsotg->dev,
  431. "## timeout in dwc2_hcd_endpoint_disable() ##\n");
  432. rc = -EBUSY;
  433. goto err;
  434. }
  435. spin_unlock_irqrestore(&hsotg->lock, flags);
  436. usleep_range(20000, 40000);
  437. spin_lock_irqsave(&hsotg->lock, flags);
  438. qh = ep->hcpriv;
  439. if (!qh) {
  440. rc = -EINVAL;
  441. goto err;
  442. }
  443. }
  444. dwc2_hcd_qh_unlink(hsotg, qh);
  445. /* Free each QTD in the QH's QTD list */
  446. list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list, qtd_list_entry)
  447. dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
  448. ep->hcpriv = NULL;
  449. spin_unlock_irqrestore(&hsotg->lock, flags);
  450. dwc2_hcd_qh_free(hsotg, qh);
  451. return 0;
  452. err:
  453. ep->hcpriv = NULL;
  454. spin_unlock_irqrestore(&hsotg->lock, flags);
  455. return rc;
  456. }
  457. /* Must be called with interrupt disabled and spinlock held */
  458. static int dwc2_hcd_endpoint_reset(struct dwc2_hsotg *hsotg,
  459. struct usb_host_endpoint *ep)
  460. {
  461. struct dwc2_qh *qh = ep->hcpriv;
  462. if (!qh)
  463. return -EINVAL;
  464. qh->data_toggle = DWC2_HC_PID_DATA0;
  465. return 0;
  466. }
  467. /*
  468. * Initializes dynamic portions of the DWC_otg HCD state
  469. *
  470. * Must be called with interrupt disabled and spinlock held
  471. */
  472. static void dwc2_hcd_reinit(struct dwc2_hsotg *hsotg)
  473. {
  474. struct dwc2_host_chan *chan, *chan_tmp;
  475. int num_channels;
  476. int i;
  477. hsotg->flags.d32 = 0;
  478. hsotg->non_periodic_qh_ptr = &hsotg->non_periodic_sched_active;
  479. if (hsotg->core_params->uframe_sched > 0) {
  480. hsotg->available_host_channels =
  481. hsotg->core_params->host_channels;
  482. } else {
  483. hsotg->non_periodic_channels = 0;
  484. hsotg->periodic_channels = 0;
  485. }
  486. /*
  487. * Put all channels in the free channel list and clean up channel
  488. * states
  489. */
  490. list_for_each_entry_safe(chan, chan_tmp, &hsotg->free_hc_list,
  491. hc_list_entry)
  492. list_del_init(&chan->hc_list_entry);
  493. num_channels = hsotg->core_params->host_channels;
  494. for (i = 0; i < num_channels; i++) {
  495. chan = hsotg->hc_ptr_array[i];
  496. list_add_tail(&chan->hc_list_entry, &hsotg->free_hc_list);
  497. dwc2_hc_cleanup(hsotg, chan);
  498. }
  499. /* Initialize the DWC core for host mode operation */
  500. dwc2_core_host_init(hsotg);
  501. }
  502. static void dwc2_hc_init_split(struct dwc2_hsotg *hsotg,
  503. struct dwc2_host_chan *chan,
  504. struct dwc2_qtd *qtd, struct dwc2_hcd_urb *urb)
  505. {
  506. int hub_addr, hub_port;
  507. chan->do_split = 1;
  508. chan->xact_pos = qtd->isoc_split_pos;
  509. chan->complete_split = qtd->complete_split;
  510. dwc2_host_hub_info(hsotg, urb->priv, &hub_addr, &hub_port);
  511. chan->hub_addr = (u8)hub_addr;
  512. chan->hub_port = (u8)hub_port;
  513. }
  514. static void *dwc2_hc_init_xfer(struct dwc2_hsotg *hsotg,
  515. struct dwc2_host_chan *chan,
  516. struct dwc2_qtd *qtd, void *bufptr)
  517. {
  518. struct dwc2_hcd_urb *urb = qtd->urb;
  519. struct dwc2_hcd_iso_packet_desc *frame_desc;
  520. switch (dwc2_hcd_get_pipe_type(&urb->pipe_info)) {
  521. case USB_ENDPOINT_XFER_CONTROL:
  522. chan->ep_type = USB_ENDPOINT_XFER_CONTROL;
  523. switch (qtd->control_phase) {
  524. case DWC2_CONTROL_SETUP:
  525. dev_vdbg(hsotg->dev, " Control setup transaction\n");
  526. chan->do_ping = 0;
  527. chan->ep_is_in = 0;
  528. chan->data_pid_start = DWC2_HC_PID_SETUP;
  529. if (hsotg->core_params->dma_enable > 0)
  530. chan->xfer_dma = urb->setup_dma;
  531. else
  532. chan->xfer_buf = urb->setup_packet;
  533. chan->xfer_len = 8;
  534. bufptr = NULL;
  535. break;
  536. case DWC2_CONTROL_DATA:
  537. dev_vdbg(hsotg->dev, " Control data transaction\n");
  538. chan->data_pid_start = qtd->data_toggle;
  539. break;
  540. case DWC2_CONTROL_STATUS:
  541. /*
  542. * Direction is opposite of data direction or IN if no
  543. * data
  544. */
  545. dev_vdbg(hsotg->dev, " Control status transaction\n");
  546. if (urb->length == 0)
  547. chan->ep_is_in = 1;
  548. else
  549. chan->ep_is_in =
  550. dwc2_hcd_is_pipe_out(&urb->pipe_info);
  551. if (chan->ep_is_in)
  552. chan->do_ping = 0;
  553. chan->data_pid_start = DWC2_HC_PID_DATA1;
  554. chan->xfer_len = 0;
  555. if (hsotg->core_params->dma_enable > 0)
  556. chan->xfer_dma = hsotg->status_buf_dma;
  557. else
  558. chan->xfer_buf = hsotg->status_buf;
  559. bufptr = NULL;
  560. break;
  561. }
  562. break;
  563. case USB_ENDPOINT_XFER_BULK:
  564. chan->ep_type = USB_ENDPOINT_XFER_BULK;
  565. break;
  566. case USB_ENDPOINT_XFER_INT:
  567. chan->ep_type = USB_ENDPOINT_XFER_INT;
  568. break;
  569. case USB_ENDPOINT_XFER_ISOC:
  570. chan->ep_type = USB_ENDPOINT_XFER_ISOC;
  571. if (hsotg->core_params->dma_desc_enable > 0)
  572. break;
  573. frame_desc = &urb->iso_descs[qtd->isoc_frame_index];
  574. frame_desc->status = 0;
  575. if (hsotg->core_params->dma_enable > 0) {
  576. chan->xfer_dma = urb->dma;
  577. chan->xfer_dma += frame_desc->offset +
  578. qtd->isoc_split_offset;
  579. } else {
  580. chan->xfer_buf = urb->buf;
  581. chan->xfer_buf += frame_desc->offset +
  582. qtd->isoc_split_offset;
  583. }
  584. chan->xfer_len = frame_desc->length - qtd->isoc_split_offset;
  585. /* For non-dword aligned buffers */
  586. if (hsotg->core_params->dma_enable > 0 &&
  587. (chan->xfer_dma & 0x3))
  588. bufptr = (u8 *)urb->buf + frame_desc->offset +
  589. qtd->isoc_split_offset;
  590. else
  591. bufptr = NULL;
  592. if (chan->xact_pos == DWC2_HCSPLT_XACTPOS_ALL) {
  593. if (chan->xfer_len <= 188)
  594. chan->xact_pos = DWC2_HCSPLT_XACTPOS_ALL;
  595. else
  596. chan->xact_pos = DWC2_HCSPLT_XACTPOS_BEGIN;
  597. }
  598. break;
  599. }
  600. return bufptr;
  601. }
  602. static int dwc2_hc_setup_align_buf(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
  603. struct dwc2_host_chan *chan,
  604. struct dwc2_hcd_urb *urb, void *bufptr)
  605. {
  606. u32 buf_size;
  607. struct urb *usb_urb;
  608. struct usb_hcd *hcd;
  609. if (!qh->dw_align_buf) {
  610. if (chan->ep_type != USB_ENDPOINT_XFER_ISOC)
  611. buf_size = hsotg->core_params->max_transfer_size;
  612. else
  613. /* 3072 = 3 max-size Isoc packets */
  614. buf_size = 3072;
  615. qh->dw_align_buf = dma_alloc_coherent(hsotg->dev, buf_size,
  616. &qh->dw_align_buf_dma,
  617. GFP_ATOMIC);
  618. if (!qh->dw_align_buf)
  619. return -ENOMEM;
  620. qh->dw_align_buf_size = buf_size;
  621. }
  622. if (chan->xfer_len) {
  623. dev_vdbg(hsotg->dev, "%s(): non-aligned buffer\n", __func__);
  624. usb_urb = urb->priv;
  625. if (usb_urb) {
  626. if (usb_urb->transfer_flags &
  627. (URB_SETUP_MAP_SINGLE | URB_DMA_MAP_SG |
  628. URB_DMA_MAP_PAGE | URB_DMA_MAP_SINGLE)) {
  629. hcd = dwc2_hsotg_to_hcd(hsotg);
  630. usb_hcd_unmap_urb_for_dma(hcd, usb_urb);
  631. }
  632. if (!chan->ep_is_in)
  633. memcpy(qh->dw_align_buf, bufptr,
  634. chan->xfer_len);
  635. } else {
  636. dev_warn(hsotg->dev, "no URB in dwc2_urb\n");
  637. }
  638. }
  639. chan->align_buf = qh->dw_align_buf_dma;
  640. return 0;
  641. }
  642. /**
  643. * dwc2_assign_and_init_hc() - Assigns transactions from a QTD to a free host
  644. * channel and initializes the host channel to perform the transactions. The
  645. * host channel is removed from the free list.
  646. *
  647. * @hsotg: The HCD state structure
  648. * @qh: Transactions from the first QTD for this QH are selected and assigned
  649. * to a free host channel
  650. */
  651. static int dwc2_assign_and_init_hc(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
  652. {
  653. struct dwc2_host_chan *chan;
  654. struct dwc2_hcd_urb *urb;
  655. struct dwc2_qtd *qtd;
  656. void *bufptr = NULL;
  657. if (dbg_qh(qh))
  658. dev_vdbg(hsotg->dev, "%s(%p,%p)\n", __func__, hsotg, qh);
  659. if (list_empty(&qh->qtd_list)) {
  660. dev_dbg(hsotg->dev, "No QTDs in QH list\n");
  661. return -ENOMEM;
  662. }
  663. if (list_empty(&hsotg->free_hc_list)) {
  664. dev_dbg(hsotg->dev, "No free channel to assign\n");
  665. return -ENOMEM;
  666. }
  667. chan = list_first_entry(&hsotg->free_hc_list, struct dwc2_host_chan,
  668. hc_list_entry);
  669. /* Remove host channel from free list */
  670. list_del_init(&chan->hc_list_entry);
  671. qtd = list_first_entry(&qh->qtd_list, struct dwc2_qtd, qtd_list_entry);
  672. urb = qtd->urb;
  673. qh->channel = chan;
  674. qtd->in_process = 1;
  675. /*
  676. * Use usb_pipedevice to determine device address. This address is
  677. * 0 before the SET_ADDRESS command and the correct address afterward.
  678. */
  679. chan->dev_addr = dwc2_hcd_get_dev_addr(&urb->pipe_info);
  680. chan->ep_num = dwc2_hcd_get_ep_num(&urb->pipe_info);
  681. chan->speed = qh->dev_speed;
  682. chan->max_packet = dwc2_max_packet(qh->maxp);
  683. chan->xfer_started = 0;
  684. chan->halt_status = DWC2_HC_XFER_NO_HALT_STATUS;
  685. chan->error_state = (qtd->error_count > 0);
  686. chan->halt_on_queue = 0;
  687. chan->halt_pending = 0;
  688. chan->requests = 0;
  689. /*
  690. * The following values may be modified in the transfer type section
  691. * below. The xfer_len value may be reduced when the transfer is
  692. * started to accommodate the max widths of the XferSize and PktCnt
  693. * fields in the HCTSIZn register.
  694. */
  695. chan->ep_is_in = (dwc2_hcd_is_pipe_in(&urb->pipe_info) != 0);
  696. if (chan->ep_is_in)
  697. chan->do_ping = 0;
  698. else
  699. chan->do_ping = qh->ping_state;
  700. chan->data_pid_start = qh->data_toggle;
  701. chan->multi_count = 1;
  702. if (urb->actual_length > urb->length &&
  703. !dwc2_hcd_is_pipe_in(&urb->pipe_info))
  704. urb->actual_length = urb->length;
  705. if (hsotg->core_params->dma_enable > 0) {
  706. chan->xfer_dma = urb->dma + urb->actual_length;
  707. /* For non-dword aligned case */
  708. if (hsotg->core_params->dma_desc_enable <= 0 &&
  709. (chan->xfer_dma & 0x3))
  710. bufptr = (u8 *)urb->buf + urb->actual_length;
  711. } else {
  712. chan->xfer_buf = (u8 *)urb->buf + urb->actual_length;
  713. }
  714. chan->xfer_len = urb->length - urb->actual_length;
  715. chan->xfer_count = 0;
  716. /* Set the split attributes if required */
  717. if (qh->do_split)
  718. dwc2_hc_init_split(hsotg, chan, qtd, urb);
  719. else
  720. chan->do_split = 0;
  721. /* Set the transfer attributes */
  722. bufptr = dwc2_hc_init_xfer(hsotg, chan, qtd, bufptr);
  723. /* Non DWORD-aligned buffer case */
  724. if (bufptr) {
  725. dev_vdbg(hsotg->dev, "Non-aligned buffer\n");
  726. if (dwc2_hc_setup_align_buf(hsotg, qh, chan, urb, bufptr)) {
  727. dev_err(hsotg->dev,
  728. "%s: Failed to allocate memory to handle non-dword aligned buffer\n",
  729. __func__);
  730. /* Add channel back to free list */
  731. chan->align_buf = 0;
  732. chan->multi_count = 0;
  733. list_add_tail(&chan->hc_list_entry,
  734. &hsotg->free_hc_list);
  735. qtd->in_process = 0;
  736. qh->channel = NULL;
  737. return -ENOMEM;
  738. }
  739. } else {
  740. chan->align_buf = 0;
  741. }
  742. if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
  743. chan->ep_type == USB_ENDPOINT_XFER_ISOC)
  744. /*
  745. * This value may be modified when the transfer is started
  746. * to reflect the actual transfer length
  747. */
  748. chan->multi_count = dwc2_hb_mult(qh->maxp);
  749. if (hsotg->core_params->dma_desc_enable > 0)
  750. chan->desc_list_addr = qh->desc_list_dma;
  751. dwc2_hc_init(hsotg, chan);
  752. chan->qh = qh;
  753. return 0;
  754. }
  755. /**
  756. * dwc2_hcd_select_transactions() - Selects transactions from the HCD transfer
  757. * schedule and assigns them to available host channels. Called from the HCD
  758. * interrupt handler functions.
  759. *
  760. * @hsotg: The HCD state structure
  761. *
  762. * Return: The types of new transactions that were assigned to host channels
  763. */
  764. enum dwc2_transaction_type dwc2_hcd_select_transactions(
  765. struct dwc2_hsotg *hsotg)
  766. {
  767. enum dwc2_transaction_type ret_val = DWC2_TRANSACTION_NONE;
  768. struct list_head *qh_ptr;
  769. struct dwc2_qh *qh;
  770. int num_channels;
  771. #ifdef DWC2_DEBUG_SOF
  772. dev_vdbg(hsotg->dev, " Select Transactions\n");
  773. #endif
  774. /* Process entries in the periodic ready list */
  775. qh_ptr = hsotg->periodic_sched_ready.next;
  776. while (qh_ptr != &hsotg->periodic_sched_ready) {
  777. if (list_empty(&hsotg->free_hc_list))
  778. break;
  779. if (hsotg->core_params->uframe_sched > 0) {
  780. if (hsotg->available_host_channels <= 1)
  781. break;
  782. hsotg->available_host_channels--;
  783. }
  784. qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
  785. if (dwc2_assign_and_init_hc(hsotg, qh))
  786. break;
  787. /*
  788. * Move the QH from the periodic ready schedule to the
  789. * periodic assigned schedule
  790. */
  791. qh_ptr = qh_ptr->next;
  792. list_move(&qh->qh_list_entry, &hsotg->periodic_sched_assigned);
  793. ret_val = DWC2_TRANSACTION_PERIODIC;
  794. }
  795. /*
  796. * Process entries in the inactive portion of the non-periodic
  797. * schedule. Some free host channels may not be used if they are
  798. * reserved for periodic transfers.
  799. */
  800. num_channels = hsotg->core_params->host_channels;
  801. qh_ptr = hsotg->non_periodic_sched_inactive.next;
  802. while (qh_ptr != &hsotg->non_periodic_sched_inactive) {
  803. if (hsotg->core_params->uframe_sched <= 0 &&
  804. hsotg->non_periodic_channels >= num_channels -
  805. hsotg->periodic_channels)
  806. break;
  807. if (list_empty(&hsotg->free_hc_list))
  808. break;
  809. qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
  810. if (hsotg->core_params->uframe_sched > 0) {
  811. if (hsotg->available_host_channels < 1)
  812. break;
  813. hsotg->available_host_channels--;
  814. }
  815. if (dwc2_assign_and_init_hc(hsotg, qh))
  816. break;
  817. /*
  818. * Move the QH from the non-periodic inactive schedule to the
  819. * non-periodic active schedule
  820. */
  821. qh_ptr = qh_ptr->next;
  822. list_move(&qh->qh_list_entry,
  823. &hsotg->non_periodic_sched_active);
  824. if (ret_val == DWC2_TRANSACTION_NONE)
  825. ret_val = DWC2_TRANSACTION_NON_PERIODIC;
  826. else
  827. ret_val = DWC2_TRANSACTION_ALL;
  828. if (hsotg->core_params->uframe_sched <= 0)
  829. hsotg->non_periodic_channels++;
  830. }
  831. return ret_val;
  832. }
  833. /**
  834. * dwc2_queue_transaction() - Attempts to queue a single transaction request for
  835. * a host channel associated with either a periodic or non-periodic transfer
  836. *
  837. * @hsotg: The HCD state structure
  838. * @chan: Host channel descriptor associated with either a periodic or
  839. * non-periodic transfer
  840. * @fifo_dwords_avail: Number of DWORDs available in the periodic Tx FIFO
  841. * for periodic transfers or the non-periodic Tx FIFO
  842. * for non-periodic transfers
  843. *
  844. * Return: 1 if a request is queued and more requests may be needed to
  845. * complete the transfer, 0 if no more requests are required for this
  846. * transfer, -1 if there is insufficient space in the Tx FIFO
  847. *
  848. * This function assumes that there is space available in the appropriate
  849. * request queue. For an OUT transfer or SETUP transaction in Slave mode,
  850. * it checks whether space is available in the appropriate Tx FIFO.
  851. *
  852. * Must be called with interrupt disabled and spinlock held
  853. */
  854. static int dwc2_queue_transaction(struct dwc2_hsotg *hsotg,
  855. struct dwc2_host_chan *chan,
  856. u16 fifo_dwords_avail)
  857. {
  858. int retval = 0;
  859. if (hsotg->core_params->dma_enable > 0) {
  860. if (hsotg->core_params->dma_desc_enable > 0) {
  861. if (!chan->xfer_started ||
  862. chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
  863. dwc2_hcd_start_xfer_ddma(hsotg, chan->qh);
  864. chan->qh->ping_state = 0;
  865. }
  866. } else if (!chan->xfer_started) {
  867. dwc2_hc_start_transfer(hsotg, chan);
  868. chan->qh->ping_state = 0;
  869. }
  870. } else if (chan->halt_pending) {
  871. /* Don't queue a request if the channel has been halted */
  872. } else if (chan->halt_on_queue) {
  873. dwc2_hc_halt(hsotg, chan, chan->halt_status);
  874. } else if (chan->do_ping) {
  875. if (!chan->xfer_started)
  876. dwc2_hc_start_transfer(hsotg, chan);
  877. } else if (!chan->ep_is_in ||
  878. chan->data_pid_start == DWC2_HC_PID_SETUP) {
  879. if ((fifo_dwords_avail * 4) >= chan->max_packet) {
  880. if (!chan->xfer_started) {
  881. dwc2_hc_start_transfer(hsotg, chan);
  882. retval = 1;
  883. } else {
  884. retval = dwc2_hc_continue_transfer(hsotg, chan);
  885. }
  886. } else {
  887. retval = -1;
  888. }
  889. } else {
  890. if (!chan->xfer_started) {
  891. dwc2_hc_start_transfer(hsotg, chan);
  892. retval = 1;
  893. } else {
  894. retval = dwc2_hc_continue_transfer(hsotg, chan);
  895. }
  896. }
  897. return retval;
  898. }
  899. /*
  900. * Processes periodic channels for the next frame and queues transactions for
  901. * these channels to the DWC_otg controller. After queueing transactions, the
  902. * Periodic Tx FIFO Empty interrupt is enabled if there are more transactions
  903. * to queue as Periodic Tx FIFO or request queue space becomes available.
  904. * Otherwise, the Periodic Tx FIFO Empty interrupt is disabled.
  905. *
  906. * Must be called with interrupt disabled and spinlock held
  907. */
  908. static void dwc2_process_periodic_channels(struct dwc2_hsotg *hsotg)
  909. {
  910. struct list_head *qh_ptr;
  911. struct dwc2_qh *qh;
  912. u32 tx_status;
  913. u32 fspcavail;
  914. u32 gintmsk;
  915. int status;
  916. int no_queue_space = 0;
  917. int no_fifo_space = 0;
  918. u32 qspcavail;
  919. if (dbg_perio())
  920. dev_vdbg(hsotg->dev, "Queue periodic transactions\n");
  921. tx_status = readl(hsotg->regs + HPTXSTS);
  922. qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
  923. TXSTS_QSPCAVAIL_SHIFT;
  924. fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
  925. TXSTS_FSPCAVAIL_SHIFT;
  926. if (dbg_perio()) {
  927. dev_vdbg(hsotg->dev, " P Tx Req Queue Space Avail (before queue): %d\n",
  928. qspcavail);
  929. dev_vdbg(hsotg->dev, " P Tx FIFO Space Avail (before queue): %d\n",
  930. fspcavail);
  931. }
  932. qh_ptr = hsotg->periodic_sched_assigned.next;
  933. while (qh_ptr != &hsotg->periodic_sched_assigned) {
  934. tx_status = readl(hsotg->regs + HPTXSTS);
  935. qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
  936. TXSTS_QSPCAVAIL_SHIFT;
  937. if (qspcavail == 0) {
  938. no_queue_space = 1;
  939. break;
  940. }
  941. qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
  942. if (!qh->channel) {
  943. qh_ptr = qh_ptr->next;
  944. continue;
  945. }
  946. /* Make sure EP's TT buffer is clean before queueing qtds */
  947. if (qh->tt_buffer_dirty) {
  948. qh_ptr = qh_ptr->next;
  949. continue;
  950. }
  951. /*
  952. * Set a flag if we're queuing high-bandwidth in slave mode.
  953. * The flag prevents any halts to get into the request queue in
  954. * the middle of multiple high-bandwidth packets getting queued.
  955. */
  956. if (hsotg->core_params->dma_enable <= 0 &&
  957. qh->channel->multi_count > 1)
  958. hsotg->queuing_high_bandwidth = 1;
  959. fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
  960. TXSTS_FSPCAVAIL_SHIFT;
  961. status = dwc2_queue_transaction(hsotg, qh->channel, fspcavail);
  962. if (status < 0) {
  963. no_fifo_space = 1;
  964. break;
  965. }
  966. /*
  967. * In Slave mode, stay on the current transfer until there is
  968. * nothing more to do or the high-bandwidth request count is
  969. * reached. In DMA mode, only need to queue one request. The
  970. * controller automatically handles multiple packets for
  971. * high-bandwidth transfers.
  972. */
  973. if (hsotg->core_params->dma_enable > 0 || status == 0 ||
  974. qh->channel->requests == qh->channel->multi_count) {
  975. qh_ptr = qh_ptr->next;
  976. /*
  977. * Move the QH from the periodic assigned schedule to
  978. * the periodic queued schedule
  979. */
  980. list_move(&qh->qh_list_entry,
  981. &hsotg->periodic_sched_queued);
  982. /* done queuing high bandwidth */
  983. hsotg->queuing_high_bandwidth = 0;
  984. }
  985. }
  986. if (hsotg->core_params->dma_enable <= 0) {
  987. tx_status = readl(hsotg->regs + HPTXSTS);
  988. qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
  989. TXSTS_QSPCAVAIL_SHIFT;
  990. fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
  991. TXSTS_FSPCAVAIL_SHIFT;
  992. if (dbg_perio()) {
  993. dev_vdbg(hsotg->dev,
  994. " P Tx Req Queue Space Avail (after queue): %d\n",
  995. qspcavail);
  996. dev_vdbg(hsotg->dev,
  997. " P Tx FIFO Space Avail (after queue): %d\n",
  998. fspcavail);
  999. }
  1000. if (!list_empty(&hsotg->periodic_sched_assigned) ||
  1001. no_queue_space || no_fifo_space) {
  1002. /*
  1003. * May need to queue more transactions as the request
  1004. * queue or Tx FIFO empties. Enable the periodic Tx
  1005. * FIFO empty interrupt. (Always use the half-empty
  1006. * level to ensure that new requests are loaded as
  1007. * soon as possible.)
  1008. */
  1009. gintmsk = readl(hsotg->regs + GINTMSK);
  1010. gintmsk |= GINTSTS_PTXFEMP;
  1011. writel(gintmsk, hsotg->regs + GINTMSK);
  1012. } else {
  1013. /*
  1014. * Disable the Tx FIFO empty interrupt since there are
  1015. * no more transactions that need to be queued right
  1016. * now. This function is called from interrupt
  1017. * handlers to queue more transactions as transfer
  1018. * states change.
  1019. */
  1020. gintmsk = readl(hsotg->regs + GINTMSK);
  1021. gintmsk &= ~GINTSTS_PTXFEMP;
  1022. writel(gintmsk, hsotg->regs + GINTMSK);
  1023. }
  1024. }
  1025. }
  1026. /*
  1027. * Processes active non-periodic channels and queues transactions for these
  1028. * channels to the DWC_otg controller. After queueing transactions, the NP Tx
  1029. * FIFO Empty interrupt is enabled if there are more transactions to queue as
  1030. * NP Tx FIFO or request queue space becomes available. Otherwise, the NP Tx
  1031. * FIFO Empty interrupt is disabled.
  1032. *
  1033. * Must be called with interrupt disabled and spinlock held
  1034. */
  1035. static void dwc2_process_non_periodic_channels(struct dwc2_hsotg *hsotg)
  1036. {
  1037. struct list_head *orig_qh_ptr;
  1038. struct dwc2_qh *qh;
  1039. u32 tx_status;
  1040. u32 qspcavail;
  1041. u32 fspcavail;
  1042. u32 gintmsk;
  1043. int status;
  1044. int no_queue_space = 0;
  1045. int no_fifo_space = 0;
  1046. int more_to_do = 0;
  1047. dev_vdbg(hsotg->dev, "Queue non-periodic transactions\n");
  1048. tx_status = readl(hsotg->regs + GNPTXSTS);
  1049. qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
  1050. TXSTS_QSPCAVAIL_SHIFT;
  1051. fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
  1052. TXSTS_FSPCAVAIL_SHIFT;
  1053. dev_vdbg(hsotg->dev, " NP Tx Req Queue Space Avail (before queue): %d\n",
  1054. qspcavail);
  1055. dev_vdbg(hsotg->dev, " NP Tx FIFO Space Avail (before queue): %d\n",
  1056. fspcavail);
  1057. /*
  1058. * Keep track of the starting point. Skip over the start-of-list
  1059. * entry.
  1060. */
  1061. if (hsotg->non_periodic_qh_ptr == &hsotg->non_periodic_sched_active)
  1062. hsotg->non_periodic_qh_ptr = hsotg->non_periodic_qh_ptr->next;
  1063. orig_qh_ptr = hsotg->non_periodic_qh_ptr;
  1064. /*
  1065. * Process once through the active list or until no more space is
  1066. * available in the request queue or the Tx FIFO
  1067. */
  1068. do {
  1069. tx_status = readl(hsotg->regs + GNPTXSTS);
  1070. qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
  1071. TXSTS_QSPCAVAIL_SHIFT;
  1072. if (hsotg->core_params->dma_enable <= 0 && qspcavail == 0) {
  1073. no_queue_space = 1;
  1074. break;
  1075. }
  1076. qh = list_entry(hsotg->non_periodic_qh_ptr, struct dwc2_qh,
  1077. qh_list_entry);
  1078. if (!qh->channel)
  1079. goto next;
  1080. /* Make sure EP's TT buffer is clean before queueing qtds */
  1081. if (qh->tt_buffer_dirty)
  1082. goto next;
  1083. fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
  1084. TXSTS_FSPCAVAIL_SHIFT;
  1085. status = dwc2_queue_transaction(hsotg, qh->channel, fspcavail);
  1086. if (status > 0) {
  1087. more_to_do = 1;
  1088. } else if (status < 0) {
  1089. no_fifo_space = 1;
  1090. break;
  1091. }
  1092. next:
  1093. /* Advance to next QH, skipping start-of-list entry */
  1094. hsotg->non_periodic_qh_ptr = hsotg->non_periodic_qh_ptr->next;
  1095. if (hsotg->non_periodic_qh_ptr ==
  1096. &hsotg->non_periodic_sched_active)
  1097. hsotg->non_periodic_qh_ptr =
  1098. hsotg->non_periodic_qh_ptr->next;
  1099. } while (hsotg->non_periodic_qh_ptr != orig_qh_ptr);
  1100. if (hsotg->core_params->dma_enable <= 0) {
  1101. tx_status = readl(hsotg->regs + GNPTXSTS);
  1102. qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
  1103. TXSTS_QSPCAVAIL_SHIFT;
  1104. fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
  1105. TXSTS_FSPCAVAIL_SHIFT;
  1106. dev_vdbg(hsotg->dev,
  1107. " NP Tx Req Queue Space Avail (after queue): %d\n",
  1108. qspcavail);
  1109. dev_vdbg(hsotg->dev,
  1110. " NP Tx FIFO Space Avail (after queue): %d\n",
  1111. fspcavail);
  1112. if (more_to_do || no_queue_space || no_fifo_space) {
  1113. /*
  1114. * May need to queue more transactions as the request
  1115. * queue or Tx FIFO empties. Enable the non-periodic
  1116. * Tx FIFO empty interrupt. (Always use the half-empty
  1117. * level to ensure that new requests are loaded as
  1118. * soon as possible.)
  1119. */
  1120. gintmsk = readl(hsotg->regs + GINTMSK);
  1121. gintmsk |= GINTSTS_NPTXFEMP;
  1122. writel(gintmsk, hsotg->regs + GINTMSK);
  1123. } else {
  1124. /*
  1125. * Disable the Tx FIFO empty interrupt since there are
  1126. * no more transactions that need to be queued right
  1127. * now. This function is called from interrupt
  1128. * handlers to queue more transactions as transfer
  1129. * states change.
  1130. */
  1131. gintmsk = readl(hsotg->regs + GINTMSK);
  1132. gintmsk &= ~GINTSTS_NPTXFEMP;
  1133. writel(gintmsk, hsotg->regs + GINTMSK);
  1134. }
  1135. }
  1136. }
  1137. /**
  1138. * dwc2_hcd_queue_transactions() - Processes the currently active host channels
  1139. * and queues transactions for these channels to the DWC_otg controller. Called
  1140. * from the HCD interrupt handler functions.
  1141. *
  1142. * @hsotg: The HCD state structure
  1143. * @tr_type: The type(s) of transactions to queue (non-periodic, periodic,
  1144. * or both)
  1145. *
  1146. * Must be called with interrupt disabled and spinlock held
  1147. */
  1148. void dwc2_hcd_queue_transactions(struct dwc2_hsotg *hsotg,
  1149. enum dwc2_transaction_type tr_type)
  1150. {
  1151. #ifdef DWC2_DEBUG_SOF
  1152. dev_vdbg(hsotg->dev, "Queue Transactions\n");
  1153. #endif
  1154. /* Process host channels associated with periodic transfers */
  1155. if ((tr_type == DWC2_TRANSACTION_PERIODIC ||
  1156. tr_type == DWC2_TRANSACTION_ALL) &&
  1157. !list_empty(&hsotg->periodic_sched_assigned))
  1158. dwc2_process_periodic_channels(hsotg);
  1159. /* Process host channels associated with non-periodic transfers */
  1160. if (tr_type == DWC2_TRANSACTION_NON_PERIODIC ||
  1161. tr_type == DWC2_TRANSACTION_ALL) {
  1162. if (!list_empty(&hsotg->non_periodic_sched_active)) {
  1163. dwc2_process_non_periodic_channels(hsotg);
  1164. } else {
  1165. /*
  1166. * Ensure NP Tx FIFO empty interrupt is disabled when
  1167. * there are no non-periodic transfers to process
  1168. */
  1169. u32 gintmsk = readl(hsotg->regs + GINTMSK);
  1170. gintmsk &= ~GINTSTS_NPTXFEMP;
  1171. writel(gintmsk, hsotg->regs + GINTMSK);
  1172. }
  1173. }
  1174. }
  1175. static void dwc2_conn_id_status_change(struct work_struct *work)
  1176. {
  1177. struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
  1178. wf_otg);
  1179. u32 count = 0;
  1180. u32 gotgctl;
  1181. dev_dbg(hsotg->dev, "%s()\n", __func__);
  1182. gotgctl = readl(hsotg->regs + GOTGCTL);
  1183. dev_dbg(hsotg->dev, "gotgctl=%0x\n", gotgctl);
  1184. dev_dbg(hsotg->dev, "gotgctl.b.conidsts=%d\n",
  1185. !!(gotgctl & GOTGCTL_CONID_B));
  1186. /* B-Device connector (Device Mode) */
  1187. if (gotgctl & GOTGCTL_CONID_B) {
  1188. /* Wait for switch to device mode */
  1189. dev_dbg(hsotg->dev, "connId B\n");
  1190. while (!dwc2_is_device_mode(hsotg)) {
  1191. dev_info(hsotg->dev,
  1192. "Waiting for Peripheral Mode, Mode=%s\n",
  1193. dwc2_is_host_mode(hsotg) ? "Host" :
  1194. "Peripheral");
  1195. usleep_range(20000, 40000);
  1196. if (++count > 250)
  1197. break;
  1198. }
  1199. if (count > 250)
  1200. dev_err(hsotg->dev,
  1201. "Connection id status change timed out\n");
  1202. hsotg->op_state = OTG_STATE_B_PERIPHERAL;
  1203. dwc2_core_init(hsotg, false, -1);
  1204. dwc2_enable_global_interrupts(hsotg);
  1205. } else {
  1206. /* A-Device connector (Host Mode) */
  1207. dev_dbg(hsotg->dev, "connId A\n");
  1208. while (!dwc2_is_host_mode(hsotg)) {
  1209. dev_info(hsotg->dev, "Waiting for Host Mode, Mode=%s\n",
  1210. dwc2_is_host_mode(hsotg) ?
  1211. "Host" : "Peripheral");
  1212. usleep_range(20000, 40000);
  1213. if (++count > 250)
  1214. break;
  1215. }
  1216. if (count > 250)
  1217. dev_err(hsotg->dev,
  1218. "Connection id status change timed out\n");
  1219. hsotg->op_state = OTG_STATE_A_HOST;
  1220. /* Initialize the Core for Host mode */
  1221. dwc2_core_init(hsotg, false, -1);
  1222. dwc2_enable_global_interrupts(hsotg);
  1223. dwc2_hcd_start(hsotg);
  1224. }
  1225. }
  1226. static void dwc2_wakeup_detected(unsigned long data)
  1227. {
  1228. struct dwc2_hsotg *hsotg = (struct dwc2_hsotg *)data;
  1229. u32 hprt0;
  1230. dev_dbg(hsotg->dev, "%s()\n", __func__);
  1231. /*
  1232. * Clear the Resume after 70ms. (Need 20 ms minimum. Use 70 ms
  1233. * so that OPT tests pass with all PHYs.)
  1234. */
  1235. hprt0 = dwc2_read_hprt0(hsotg);
  1236. dev_dbg(hsotg->dev, "Resume: HPRT0=%0x\n", hprt0);
  1237. hprt0 &= ~HPRT0_RES;
  1238. writel(hprt0, hsotg->regs + HPRT0);
  1239. dev_dbg(hsotg->dev, "Clear Resume: HPRT0=%0x\n",
  1240. readl(hsotg->regs + HPRT0));
  1241. dwc2_hcd_rem_wakeup(hsotg);
  1242. /* Change to L0 state */
  1243. hsotg->lx_state = DWC2_L0;
  1244. }
  1245. static int dwc2_host_is_b_hnp_enabled(struct dwc2_hsotg *hsotg)
  1246. {
  1247. struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
  1248. return hcd->self.b_hnp_enable;
  1249. }
  1250. /* Must NOT be called with interrupt disabled or spinlock held */
  1251. static void dwc2_port_suspend(struct dwc2_hsotg *hsotg, u16 windex)
  1252. {
  1253. unsigned long flags;
  1254. u32 hprt0;
  1255. u32 pcgctl;
  1256. u32 gotgctl;
  1257. dev_dbg(hsotg->dev, "%s()\n", __func__);
  1258. spin_lock_irqsave(&hsotg->lock, flags);
  1259. if (windex == hsotg->otg_port && dwc2_host_is_b_hnp_enabled(hsotg)) {
  1260. gotgctl = readl(hsotg->regs + GOTGCTL);
  1261. gotgctl |= GOTGCTL_HSTSETHNPEN;
  1262. writel(gotgctl, hsotg->regs + GOTGCTL);
  1263. hsotg->op_state = OTG_STATE_A_SUSPEND;
  1264. }
  1265. hprt0 = dwc2_read_hprt0(hsotg);
  1266. hprt0 |= HPRT0_SUSP;
  1267. writel(hprt0, hsotg->regs + HPRT0);
  1268. /* Update lx_state */
  1269. hsotg->lx_state = DWC2_L2;
  1270. /* Suspend the Phy Clock */
  1271. pcgctl = readl(hsotg->regs + PCGCTL);
  1272. pcgctl |= PCGCTL_STOPPCLK;
  1273. writel(pcgctl, hsotg->regs + PCGCTL);
  1274. udelay(10);
  1275. /* For HNP the bus must be suspended for at least 200ms */
  1276. if (dwc2_host_is_b_hnp_enabled(hsotg)) {
  1277. pcgctl = readl(hsotg->regs + PCGCTL);
  1278. pcgctl &= ~PCGCTL_STOPPCLK;
  1279. writel(pcgctl, hsotg->regs + PCGCTL);
  1280. spin_unlock_irqrestore(&hsotg->lock, flags);
  1281. usleep_range(200000, 250000);
  1282. } else {
  1283. spin_unlock_irqrestore(&hsotg->lock, flags);
  1284. }
  1285. }
  1286. /* Handles hub class-specific requests */
  1287. static int dwc2_hcd_hub_control(struct dwc2_hsotg *hsotg, u16 typereq,
  1288. u16 wvalue, u16 windex, char *buf, u16 wlength)
  1289. {
  1290. struct usb_hub_descriptor *hub_desc;
  1291. int retval = 0;
  1292. u32 hprt0;
  1293. u32 port_status;
  1294. u32 speed;
  1295. u32 pcgctl;
  1296. switch (typereq) {
  1297. case ClearHubFeature:
  1298. dev_dbg(hsotg->dev, "ClearHubFeature %1xh\n", wvalue);
  1299. switch (wvalue) {
  1300. case C_HUB_LOCAL_POWER:
  1301. case C_HUB_OVER_CURRENT:
  1302. /* Nothing required here */
  1303. break;
  1304. default:
  1305. retval = -EINVAL;
  1306. dev_err(hsotg->dev,
  1307. "ClearHubFeature request %1xh unknown\n",
  1308. wvalue);
  1309. }
  1310. break;
  1311. case ClearPortFeature:
  1312. if (wvalue != USB_PORT_FEAT_L1)
  1313. if (!windex || windex > 1)
  1314. goto error;
  1315. switch (wvalue) {
  1316. case USB_PORT_FEAT_ENABLE:
  1317. dev_dbg(hsotg->dev,
  1318. "ClearPortFeature USB_PORT_FEAT_ENABLE\n");
  1319. hprt0 = dwc2_read_hprt0(hsotg);
  1320. hprt0 |= HPRT0_ENA;
  1321. writel(hprt0, hsotg->regs + HPRT0);
  1322. break;
  1323. case USB_PORT_FEAT_SUSPEND:
  1324. dev_dbg(hsotg->dev,
  1325. "ClearPortFeature USB_PORT_FEAT_SUSPEND\n");
  1326. writel(0, hsotg->regs + PCGCTL);
  1327. usleep_range(20000, 40000);
  1328. hprt0 = dwc2_read_hprt0(hsotg);
  1329. hprt0 |= HPRT0_RES;
  1330. writel(hprt0, hsotg->regs + HPRT0);
  1331. hprt0 &= ~HPRT0_SUSP;
  1332. usleep_range(100000, 150000);
  1333. hprt0 &= ~HPRT0_RES;
  1334. writel(hprt0, hsotg->regs + HPRT0);
  1335. break;
  1336. case USB_PORT_FEAT_POWER:
  1337. dev_dbg(hsotg->dev,
  1338. "ClearPortFeature USB_PORT_FEAT_POWER\n");
  1339. hprt0 = dwc2_read_hprt0(hsotg);
  1340. hprt0 &= ~HPRT0_PWR;
  1341. writel(hprt0, hsotg->regs + HPRT0);
  1342. break;
  1343. case USB_PORT_FEAT_INDICATOR:
  1344. dev_dbg(hsotg->dev,
  1345. "ClearPortFeature USB_PORT_FEAT_INDICATOR\n");
  1346. /* Port indicator not supported */
  1347. break;
  1348. case USB_PORT_FEAT_C_CONNECTION:
  1349. /*
  1350. * Clears driver's internal Connect Status Change flag
  1351. */
  1352. dev_dbg(hsotg->dev,
  1353. "ClearPortFeature USB_PORT_FEAT_C_CONNECTION\n");
  1354. hsotg->flags.b.port_connect_status_change = 0;
  1355. break;
  1356. case USB_PORT_FEAT_C_RESET:
  1357. /* Clears driver's internal Port Reset Change flag */
  1358. dev_dbg(hsotg->dev,
  1359. "ClearPortFeature USB_PORT_FEAT_C_RESET\n");
  1360. hsotg->flags.b.port_reset_change = 0;
  1361. break;
  1362. case USB_PORT_FEAT_C_ENABLE:
  1363. /*
  1364. * Clears the driver's internal Port Enable/Disable
  1365. * Change flag
  1366. */
  1367. dev_dbg(hsotg->dev,
  1368. "ClearPortFeature USB_PORT_FEAT_C_ENABLE\n");
  1369. hsotg->flags.b.port_enable_change = 0;
  1370. break;
  1371. case USB_PORT_FEAT_C_SUSPEND:
  1372. /*
  1373. * Clears the driver's internal Port Suspend Change
  1374. * flag, which is set when resume signaling on the host
  1375. * port is complete
  1376. */
  1377. dev_dbg(hsotg->dev,
  1378. "ClearPortFeature USB_PORT_FEAT_C_SUSPEND\n");
  1379. hsotg->flags.b.port_suspend_change = 0;
  1380. break;
  1381. case USB_PORT_FEAT_C_PORT_L1:
  1382. dev_dbg(hsotg->dev,
  1383. "ClearPortFeature USB_PORT_FEAT_C_PORT_L1\n");
  1384. hsotg->flags.b.port_l1_change = 0;
  1385. break;
  1386. case USB_PORT_FEAT_C_OVER_CURRENT:
  1387. dev_dbg(hsotg->dev,
  1388. "ClearPortFeature USB_PORT_FEAT_C_OVER_CURRENT\n");
  1389. hsotg->flags.b.port_over_current_change = 0;
  1390. break;
  1391. default:
  1392. retval = -EINVAL;
  1393. dev_err(hsotg->dev,
  1394. "ClearPortFeature request %1xh unknown or unsupported\n",
  1395. wvalue);
  1396. }
  1397. break;
  1398. case GetHubDescriptor:
  1399. dev_dbg(hsotg->dev, "GetHubDescriptor\n");
  1400. hub_desc = (struct usb_hub_descriptor *)buf;
  1401. hub_desc->bDescLength = 9;
  1402. hub_desc->bDescriptorType = 0x29;
  1403. hub_desc->bNbrPorts = 1;
  1404. hub_desc->wHubCharacteristics = cpu_to_le16(0x08);
  1405. hub_desc->bPwrOn2PwrGood = 1;
  1406. hub_desc->bHubContrCurrent = 0;
  1407. hub_desc->u.hs.DeviceRemovable[0] = 0;
  1408. hub_desc->u.hs.DeviceRemovable[1] = 0xff;
  1409. break;
  1410. case GetHubStatus:
  1411. dev_dbg(hsotg->dev, "GetHubStatus\n");
  1412. memset(buf, 0, 4);
  1413. break;
  1414. case GetPortStatus:
  1415. dev_vdbg(hsotg->dev,
  1416. "GetPortStatus wIndex=0x%04x flags=0x%08x\n", windex,
  1417. hsotg->flags.d32);
  1418. if (!windex || windex > 1)
  1419. goto error;
  1420. port_status = 0;
  1421. if (hsotg->flags.b.port_connect_status_change)
  1422. port_status |= USB_PORT_STAT_C_CONNECTION << 16;
  1423. if (hsotg->flags.b.port_enable_change)
  1424. port_status |= USB_PORT_STAT_C_ENABLE << 16;
  1425. if (hsotg->flags.b.port_suspend_change)
  1426. port_status |= USB_PORT_STAT_C_SUSPEND << 16;
  1427. if (hsotg->flags.b.port_l1_change)
  1428. port_status |= USB_PORT_STAT_C_L1 << 16;
  1429. if (hsotg->flags.b.port_reset_change)
  1430. port_status |= USB_PORT_STAT_C_RESET << 16;
  1431. if (hsotg->flags.b.port_over_current_change) {
  1432. dev_warn(hsotg->dev, "Overcurrent change detected\n");
  1433. port_status |= USB_PORT_STAT_C_OVERCURRENT << 16;
  1434. }
  1435. if (!hsotg->flags.b.port_connect_status) {
  1436. /*
  1437. * The port is disconnected, which means the core is
  1438. * either in device mode or it soon will be. Just
  1439. * return 0's for the remainder of the port status
  1440. * since the port register can't be read if the core
  1441. * is in device mode.
  1442. */
  1443. *(__le32 *)buf = cpu_to_le32(port_status);
  1444. break;
  1445. }
  1446. hprt0 = readl(hsotg->regs + HPRT0);
  1447. dev_vdbg(hsotg->dev, " HPRT0: 0x%08x\n", hprt0);
  1448. if (hprt0 & HPRT0_CONNSTS)
  1449. port_status |= USB_PORT_STAT_CONNECTION;
  1450. if (hprt0 & HPRT0_ENA)
  1451. port_status |= USB_PORT_STAT_ENABLE;
  1452. if (hprt0 & HPRT0_SUSP)
  1453. port_status |= USB_PORT_STAT_SUSPEND;
  1454. if (hprt0 & HPRT0_OVRCURRACT)
  1455. port_status |= USB_PORT_STAT_OVERCURRENT;
  1456. if (hprt0 & HPRT0_RST)
  1457. port_status |= USB_PORT_STAT_RESET;
  1458. if (hprt0 & HPRT0_PWR)
  1459. port_status |= USB_PORT_STAT_POWER;
  1460. speed = (hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT;
  1461. if (speed == HPRT0_SPD_HIGH_SPEED)
  1462. port_status |= USB_PORT_STAT_HIGH_SPEED;
  1463. else if (speed == HPRT0_SPD_LOW_SPEED)
  1464. port_status |= USB_PORT_STAT_LOW_SPEED;
  1465. if (hprt0 & HPRT0_TSTCTL_MASK)
  1466. port_status |= USB_PORT_STAT_TEST;
  1467. /* USB_PORT_FEAT_INDICATOR unsupported always 0 */
  1468. dev_vdbg(hsotg->dev, "port_status=%08x\n", port_status);
  1469. *(__le32 *)buf = cpu_to_le32(port_status);
  1470. break;
  1471. case SetHubFeature:
  1472. dev_dbg(hsotg->dev, "SetHubFeature\n");
  1473. /* No HUB features supported */
  1474. break;
  1475. case SetPortFeature:
  1476. dev_dbg(hsotg->dev, "SetPortFeature\n");
  1477. if (wvalue != USB_PORT_FEAT_TEST && (!windex || windex > 1))
  1478. goto error;
  1479. if (!hsotg->flags.b.port_connect_status) {
  1480. /*
  1481. * The port is disconnected, which means the core is
  1482. * either in device mode or it soon will be. Just
  1483. * return without doing anything since the port
  1484. * register can't be written if the core is in device
  1485. * mode.
  1486. */
  1487. break;
  1488. }
  1489. switch (wvalue) {
  1490. case USB_PORT_FEAT_SUSPEND:
  1491. dev_dbg(hsotg->dev,
  1492. "SetPortFeature - USB_PORT_FEAT_SUSPEND\n");
  1493. if (windex != hsotg->otg_port)
  1494. goto error;
  1495. dwc2_port_suspend(hsotg, windex);
  1496. break;
  1497. case USB_PORT_FEAT_POWER:
  1498. dev_dbg(hsotg->dev,
  1499. "SetPortFeature - USB_PORT_FEAT_POWER\n");
  1500. hprt0 = dwc2_read_hprt0(hsotg);
  1501. hprt0 |= HPRT0_PWR;
  1502. writel(hprt0, hsotg->regs + HPRT0);
  1503. break;
  1504. case USB_PORT_FEAT_RESET:
  1505. hprt0 = dwc2_read_hprt0(hsotg);
  1506. dev_dbg(hsotg->dev,
  1507. "SetPortFeature - USB_PORT_FEAT_RESET\n");
  1508. pcgctl = readl(hsotg->regs + PCGCTL);
  1509. pcgctl &= ~(PCGCTL_ENBL_SLEEP_GATING | PCGCTL_STOPPCLK);
  1510. writel(pcgctl, hsotg->regs + PCGCTL);
  1511. /* ??? Original driver does this */
  1512. writel(0, hsotg->regs + PCGCTL);
  1513. hprt0 = dwc2_read_hprt0(hsotg);
  1514. /* Clear suspend bit if resetting from suspend state */
  1515. hprt0 &= ~HPRT0_SUSP;
  1516. /*
  1517. * When B-Host the Port reset bit is set in the Start
  1518. * HCD Callback function, so that the reset is started
  1519. * within 1ms of the HNP success interrupt
  1520. */
  1521. if (!dwc2_hcd_is_b_host(hsotg)) {
  1522. hprt0 |= HPRT0_PWR | HPRT0_RST;
  1523. dev_dbg(hsotg->dev,
  1524. "In host mode, hprt0=%08x\n", hprt0);
  1525. writel(hprt0, hsotg->regs + HPRT0);
  1526. }
  1527. /* Clear reset bit in 10ms (FS/LS) or 50ms (HS) */
  1528. usleep_range(50000, 70000);
  1529. hprt0 &= ~HPRT0_RST;
  1530. writel(hprt0, hsotg->regs + HPRT0);
  1531. hsotg->lx_state = DWC2_L0; /* Now back to On state */
  1532. break;
  1533. case USB_PORT_FEAT_INDICATOR:
  1534. dev_dbg(hsotg->dev,
  1535. "SetPortFeature - USB_PORT_FEAT_INDICATOR\n");
  1536. /* Not supported */
  1537. break;
  1538. default:
  1539. retval = -EINVAL;
  1540. dev_err(hsotg->dev,
  1541. "SetPortFeature %1xh unknown or unsupported\n",
  1542. wvalue);
  1543. break;
  1544. }
  1545. break;
  1546. default:
  1547. error:
  1548. retval = -EINVAL;
  1549. dev_dbg(hsotg->dev,
  1550. "Unknown hub control request: %1xh wIndex: %1xh wValue: %1xh\n",
  1551. typereq, windex, wvalue);
  1552. break;
  1553. }
  1554. return retval;
  1555. }
  1556. static int dwc2_hcd_is_status_changed(struct dwc2_hsotg *hsotg, int port)
  1557. {
  1558. int retval;
  1559. if (port != 1)
  1560. return -EINVAL;
  1561. retval = (hsotg->flags.b.port_connect_status_change ||
  1562. hsotg->flags.b.port_reset_change ||
  1563. hsotg->flags.b.port_enable_change ||
  1564. hsotg->flags.b.port_suspend_change ||
  1565. hsotg->flags.b.port_over_current_change);
  1566. if (retval) {
  1567. dev_dbg(hsotg->dev,
  1568. "DWC OTG HCD HUB STATUS DATA: Root port status changed\n");
  1569. dev_dbg(hsotg->dev, " port_connect_status_change: %d\n",
  1570. hsotg->flags.b.port_connect_status_change);
  1571. dev_dbg(hsotg->dev, " port_reset_change: %d\n",
  1572. hsotg->flags.b.port_reset_change);
  1573. dev_dbg(hsotg->dev, " port_enable_change: %d\n",
  1574. hsotg->flags.b.port_enable_change);
  1575. dev_dbg(hsotg->dev, " port_suspend_change: %d\n",
  1576. hsotg->flags.b.port_suspend_change);
  1577. dev_dbg(hsotg->dev, " port_over_current_change: %d\n",
  1578. hsotg->flags.b.port_over_current_change);
  1579. }
  1580. return retval;
  1581. }
  1582. int dwc2_hcd_get_frame_number(struct dwc2_hsotg *hsotg)
  1583. {
  1584. u32 hfnum = readl(hsotg->regs + HFNUM);
  1585. #ifdef DWC2_DEBUG_SOF
  1586. dev_vdbg(hsotg->dev, "DWC OTG HCD GET FRAME NUMBER %d\n",
  1587. (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT);
  1588. #endif
  1589. return (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT;
  1590. }
  1591. int dwc2_hcd_is_b_host(struct dwc2_hsotg *hsotg)
  1592. {
  1593. return hsotg->op_state == OTG_STATE_B_HOST;
  1594. }
  1595. static struct dwc2_hcd_urb *dwc2_hcd_urb_alloc(struct dwc2_hsotg *hsotg,
  1596. int iso_desc_count,
  1597. gfp_t mem_flags)
  1598. {
  1599. struct dwc2_hcd_urb *urb;
  1600. u32 size = sizeof(*urb) + iso_desc_count *
  1601. sizeof(struct dwc2_hcd_iso_packet_desc);
  1602. urb = kzalloc(size, mem_flags);
  1603. if (urb)
  1604. urb->packet_count = iso_desc_count;
  1605. return urb;
  1606. }
  1607. static void dwc2_hcd_urb_set_pipeinfo(struct dwc2_hsotg *hsotg,
  1608. struct dwc2_hcd_urb *urb, u8 dev_addr,
  1609. u8 ep_num, u8 ep_type, u8 ep_dir, u16 mps)
  1610. {
  1611. if (dbg_perio() ||
  1612. ep_type == USB_ENDPOINT_XFER_BULK ||
  1613. ep_type == USB_ENDPOINT_XFER_CONTROL)
  1614. dev_vdbg(hsotg->dev,
  1615. "addr=%d, ep_num=%d, ep_dir=%1x, ep_type=%1x, mps=%d\n",
  1616. dev_addr, ep_num, ep_dir, ep_type, mps);
  1617. urb->pipe_info.dev_addr = dev_addr;
  1618. urb->pipe_info.ep_num = ep_num;
  1619. urb->pipe_info.pipe_type = ep_type;
  1620. urb->pipe_info.pipe_dir = ep_dir;
  1621. urb->pipe_info.mps = mps;
  1622. }
  1623. /*
  1624. * NOTE: This function will be removed once the peripheral controller code
  1625. * is integrated and the driver is stable
  1626. */
  1627. void dwc2_hcd_dump_state(struct dwc2_hsotg *hsotg)
  1628. {
  1629. #ifdef DEBUG
  1630. struct dwc2_host_chan *chan;
  1631. struct dwc2_hcd_urb *urb;
  1632. struct dwc2_qtd *qtd;
  1633. int num_channels;
  1634. u32 np_tx_status;
  1635. u32 p_tx_status;
  1636. int i;
  1637. num_channels = hsotg->core_params->host_channels;
  1638. dev_dbg(hsotg->dev, "\n");
  1639. dev_dbg(hsotg->dev,
  1640. "************************************************************\n");
  1641. dev_dbg(hsotg->dev, "HCD State:\n");
  1642. dev_dbg(hsotg->dev, " Num channels: %d\n", num_channels);
  1643. for (i = 0; i < num_channels; i++) {
  1644. chan = hsotg->hc_ptr_array[i];
  1645. dev_dbg(hsotg->dev, " Channel %d:\n", i);
  1646. dev_dbg(hsotg->dev,
  1647. " dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
  1648. chan->dev_addr, chan->ep_num, chan->ep_is_in);
  1649. dev_dbg(hsotg->dev, " speed: %d\n", chan->speed);
  1650. dev_dbg(hsotg->dev, " ep_type: %d\n", chan->ep_type);
  1651. dev_dbg(hsotg->dev, " max_packet: %d\n", chan->max_packet);
  1652. dev_dbg(hsotg->dev, " data_pid_start: %d\n",
  1653. chan->data_pid_start);
  1654. dev_dbg(hsotg->dev, " multi_count: %d\n", chan->multi_count);
  1655. dev_dbg(hsotg->dev, " xfer_started: %d\n",
  1656. chan->xfer_started);
  1657. dev_dbg(hsotg->dev, " xfer_buf: %p\n", chan->xfer_buf);
  1658. dev_dbg(hsotg->dev, " xfer_dma: %08lx\n",
  1659. (unsigned long)chan->xfer_dma);
  1660. dev_dbg(hsotg->dev, " xfer_len: %d\n", chan->xfer_len);
  1661. dev_dbg(hsotg->dev, " xfer_count: %d\n", chan->xfer_count);
  1662. dev_dbg(hsotg->dev, " halt_on_queue: %d\n",
  1663. chan->halt_on_queue);
  1664. dev_dbg(hsotg->dev, " halt_pending: %d\n",
  1665. chan->halt_pending);
  1666. dev_dbg(hsotg->dev, " halt_status: %d\n", chan->halt_status);
  1667. dev_dbg(hsotg->dev, " do_split: %d\n", chan->do_split);
  1668. dev_dbg(hsotg->dev, " complete_split: %d\n",
  1669. chan->complete_split);
  1670. dev_dbg(hsotg->dev, " hub_addr: %d\n", chan->hub_addr);
  1671. dev_dbg(hsotg->dev, " hub_port: %d\n", chan->hub_port);
  1672. dev_dbg(hsotg->dev, " xact_pos: %d\n", chan->xact_pos);
  1673. dev_dbg(hsotg->dev, " requests: %d\n", chan->requests);
  1674. dev_dbg(hsotg->dev, " qh: %p\n", chan->qh);
  1675. if (chan->xfer_started) {
  1676. u32 hfnum, hcchar, hctsiz, hcint, hcintmsk;
  1677. hfnum = readl(hsotg->regs + HFNUM);
  1678. hcchar = readl(hsotg->regs + HCCHAR(i));
  1679. hctsiz = readl(hsotg->regs + HCTSIZ(i));
  1680. hcint = readl(hsotg->regs + HCINT(i));
  1681. hcintmsk = readl(hsotg->regs + HCINTMSK(i));
  1682. dev_dbg(hsotg->dev, " hfnum: 0x%08x\n", hfnum);
  1683. dev_dbg(hsotg->dev, " hcchar: 0x%08x\n", hcchar);
  1684. dev_dbg(hsotg->dev, " hctsiz: 0x%08x\n", hctsiz);
  1685. dev_dbg(hsotg->dev, " hcint: 0x%08x\n", hcint);
  1686. dev_dbg(hsotg->dev, " hcintmsk: 0x%08x\n", hcintmsk);
  1687. }
  1688. if (!(chan->xfer_started && chan->qh))
  1689. continue;
  1690. list_for_each_entry(qtd, &chan->qh->qtd_list, qtd_list_entry) {
  1691. if (!qtd->in_process)
  1692. break;
  1693. urb = qtd->urb;
  1694. dev_dbg(hsotg->dev, " URB Info:\n");
  1695. dev_dbg(hsotg->dev, " qtd: %p, urb: %p\n",
  1696. qtd, urb);
  1697. if (urb) {
  1698. dev_dbg(hsotg->dev,
  1699. " Dev: %d, EP: %d %s\n",
  1700. dwc2_hcd_get_dev_addr(&urb->pipe_info),
  1701. dwc2_hcd_get_ep_num(&urb->pipe_info),
  1702. dwc2_hcd_is_pipe_in(&urb->pipe_info) ?
  1703. "IN" : "OUT");
  1704. dev_dbg(hsotg->dev,
  1705. " Max packet size: %d\n",
  1706. dwc2_hcd_get_mps(&urb->pipe_info));
  1707. dev_dbg(hsotg->dev,
  1708. " transfer_buffer: %p\n",
  1709. urb->buf);
  1710. dev_dbg(hsotg->dev,
  1711. " transfer_dma: %08lx\n",
  1712. (unsigned long)urb->dma);
  1713. dev_dbg(hsotg->dev,
  1714. " transfer_buffer_length: %d\n",
  1715. urb->length);
  1716. dev_dbg(hsotg->dev, " actual_length: %d\n",
  1717. urb->actual_length);
  1718. }
  1719. }
  1720. }
  1721. dev_dbg(hsotg->dev, " non_periodic_channels: %d\n",
  1722. hsotg->non_periodic_channels);
  1723. dev_dbg(hsotg->dev, " periodic_channels: %d\n",
  1724. hsotg->periodic_channels);
  1725. dev_dbg(hsotg->dev, " periodic_usecs: %d\n", hsotg->periodic_usecs);
  1726. np_tx_status = readl(hsotg->regs + GNPTXSTS);
  1727. dev_dbg(hsotg->dev, " NP Tx Req Queue Space Avail: %d\n",
  1728. (np_tx_status & TXSTS_QSPCAVAIL_MASK) >> TXSTS_QSPCAVAIL_SHIFT);
  1729. dev_dbg(hsotg->dev, " NP Tx FIFO Space Avail: %d\n",
  1730. (np_tx_status & TXSTS_FSPCAVAIL_MASK) >> TXSTS_FSPCAVAIL_SHIFT);
  1731. p_tx_status = readl(hsotg->regs + HPTXSTS);
  1732. dev_dbg(hsotg->dev, " P Tx Req Queue Space Avail: %d\n",
  1733. (p_tx_status & TXSTS_QSPCAVAIL_MASK) >> TXSTS_QSPCAVAIL_SHIFT);
  1734. dev_dbg(hsotg->dev, " P Tx FIFO Space Avail: %d\n",
  1735. (p_tx_status & TXSTS_FSPCAVAIL_MASK) >> TXSTS_FSPCAVAIL_SHIFT);
  1736. dwc2_hcd_dump_frrem(hsotg);
  1737. dwc2_dump_global_registers(hsotg);
  1738. dwc2_dump_host_registers(hsotg);
  1739. dev_dbg(hsotg->dev,
  1740. "************************************************************\n");
  1741. dev_dbg(hsotg->dev, "\n");
  1742. #endif
  1743. }
  1744. /*
  1745. * NOTE: This function will be removed once the peripheral controller code
  1746. * is integrated and the driver is stable
  1747. */
  1748. void dwc2_hcd_dump_frrem(struct dwc2_hsotg *hsotg)
  1749. {
  1750. #ifdef DWC2_DUMP_FRREM
  1751. dev_dbg(hsotg->dev, "Frame remaining at SOF:\n");
  1752. dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
  1753. hsotg->frrem_samples, hsotg->frrem_accum,
  1754. hsotg->frrem_samples > 0 ?
  1755. hsotg->frrem_accum / hsotg->frrem_samples : 0);
  1756. dev_dbg(hsotg->dev, "\n");
  1757. dev_dbg(hsotg->dev, "Frame remaining at start_transfer (uframe 7):\n");
  1758. dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
  1759. hsotg->hfnum_7_samples,
  1760. hsotg->hfnum_7_frrem_accum,
  1761. hsotg->hfnum_7_samples > 0 ?
  1762. hsotg->hfnum_7_frrem_accum / hsotg->hfnum_7_samples : 0);
  1763. dev_dbg(hsotg->dev, "Frame remaining at start_transfer (uframe 0):\n");
  1764. dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
  1765. hsotg->hfnum_0_samples,
  1766. hsotg->hfnum_0_frrem_accum,
  1767. hsotg->hfnum_0_samples > 0 ?
  1768. hsotg->hfnum_0_frrem_accum / hsotg->hfnum_0_samples : 0);
  1769. dev_dbg(hsotg->dev, "Frame remaining at start_transfer (uframe 1-6):\n");
  1770. dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
  1771. hsotg->hfnum_other_samples,
  1772. hsotg->hfnum_other_frrem_accum,
  1773. hsotg->hfnum_other_samples > 0 ?
  1774. hsotg->hfnum_other_frrem_accum / hsotg->hfnum_other_samples :
  1775. 0);
  1776. dev_dbg(hsotg->dev, "\n");
  1777. dev_dbg(hsotg->dev, "Frame remaining at sample point A (uframe 7):\n");
  1778. dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
  1779. hsotg->hfnum_7_samples_a, hsotg->hfnum_7_frrem_accum_a,
  1780. hsotg->hfnum_7_samples_a > 0 ?
  1781. hsotg->hfnum_7_frrem_accum_a / hsotg->hfnum_7_samples_a : 0);
  1782. dev_dbg(hsotg->dev, "Frame remaining at sample point A (uframe 0):\n");
  1783. dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
  1784. hsotg->hfnum_0_samples_a, hsotg->hfnum_0_frrem_accum_a,
  1785. hsotg->hfnum_0_samples_a > 0 ?
  1786. hsotg->hfnum_0_frrem_accum_a / hsotg->hfnum_0_samples_a : 0);
  1787. dev_dbg(hsotg->dev, "Frame remaining at sample point A (uframe 1-6):\n");
  1788. dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
  1789. hsotg->hfnum_other_samples_a, hsotg->hfnum_other_frrem_accum_a,
  1790. hsotg->hfnum_other_samples_a > 0 ?
  1791. hsotg->hfnum_other_frrem_accum_a / hsotg->hfnum_other_samples_a
  1792. : 0);
  1793. dev_dbg(hsotg->dev, "\n");
  1794. dev_dbg(hsotg->dev, "Frame remaining at sample point B (uframe 7):\n");
  1795. dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
  1796. hsotg->hfnum_7_samples_b, hsotg->hfnum_7_frrem_accum_b,
  1797. hsotg->hfnum_7_samples_b > 0 ?
  1798. hsotg->hfnum_7_frrem_accum_b / hsotg->hfnum_7_samples_b : 0);
  1799. dev_dbg(hsotg->dev, "Frame remaining at sample point B (uframe 0):\n");
  1800. dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
  1801. hsotg->hfnum_0_samples_b, hsotg->hfnum_0_frrem_accum_b,
  1802. (hsotg->hfnum_0_samples_b > 0) ?
  1803. hsotg->hfnum_0_frrem_accum_b / hsotg->hfnum_0_samples_b : 0);
  1804. dev_dbg(hsotg->dev, "Frame remaining at sample point B (uframe 1-6):\n");
  1805. dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
  1806. hsotg->hfnum_other_samples_b, hsotg->hfnum_other_frrem_accum_b,
  1807. (hsotg->hfnum_other_samples_b > 0) ?
  1808. hsotg->hfnum_other_frrem_accum_b / hsotg->hfnum_other_samples_b
  1809. : 0);
  1810. #endif
  1811. }
  1812. struct wrapper_priv_data {
  1813. struct dwc2_hsotg *hsotg;
  1814. };
  1815. /* Gets the dwc2_hsotg from a usb_hcd */
  1816. static struct dwc2_hsotg *dwc2_hcd_to_hsotg(struct usb_hcd *hcd)
  1817. {
  1818. struct wrapper_priv_data *p;
  1819. p = (struct wrapper_priv_data *) &hcd->hcd_priv;
  1820. return p->hsotg;
  1821. }
  1822. static int _dwc2_hcd_start(struct usb_hcd *hcd);
  1823. void dwc2_host_start(struct dwc2_hsotg *hsotg)
  1824. {
  1825. struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
  1826. hcd->self.is_b_host = dwc2_hcd_is_b_host(hsotg);
  1827. _dwc2_hcd_start(hcd);
  1828. }
  1829. void dwc2_host_disconnect(struct dwc2_hsotg *hsotg)
  1830. {
  1831. struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
  1832. hcd->self.is_b_host = 0;
  1833. }
  1834. void dwc2_host_hub_info(struct dwc2_hsotg *hsotg, void *context, int *hub_addr,
  1835. int *hub_port)
  1836. {
  1837. struct urb *urb = context;
  1838. if (urb->dev->tt)
  1839. *hub_addr = urb->dev->tt->hub->devnum;
  1840. else
  1841. *hub_addr = 0;
  1842. *hub_port = urb->dev->ttport;
  1843. }
  1844. int dwc2_host_get_speed(struct dwc2_hsotg *hsotg, void *context)
  1845. {
  1846. struct urb *urb = context;
  1847. return urb->dev->speed;
  1848. }
  1849. static void dwc2_allocate_bus_bandwidth(struct usb_hcd *hcd, u16 bw,
  1850. struct urb *urb)
  1851. {
  1852. struct usb_bus *bus = hcd_to_bus(hcd);
  1853. if (urb->interval)
  1854. bus->bandwidth_allocated += bw / urb->interval;
  1855. if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
  1856. bus->bandwidth_isoc_reqs++;
  1857. else
  1858. bus->bandwidth_int_reqs++;
  1859. }
  1860. static void dwc2_free_bus_bandwidth(struct usb_hcd *hcd, u16 bw,
  1861. struct urb *urb)
  1862. {
  1863. struct usb_bus *bus = hcd_to_bus(hcd);
  1864. if (urb->interval)
  1865. bus->bandwidth_allocated -= bw / urb->interval;
  1866. if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
  1867. bus->bandwidth_isoc_reqs--;
  1868. else
  1869. bus->bandwidth_int_reqs--;
  1870. }
  1871. /*
  1872. * Sets the final status of an URB and returns it to the upper layer. Any
  1873. * required cleanup of the URB is performed.
  1874. *
  1875. * Must be called with interrupt disabled and spinlock held
  1876. */
  1877. void dwc2_host_complete(struct dwc2_hsotg *hsotg, struct dwc2_qtd *qtd,
  1878. int status)
  1879. {
  1880. struct urb *urb;
  1881. int i;
  1882. if (!qtd) {
  1883. dev_dbg(hsotg->dev, "## %s: qtd is NULL ##\n", __func__);
  1884. return;
  1885. }
  1886. if (!qtd->urb) {
  1887. dev_dbg(hsotg->dev, "## %s: qtd->urb is NULL ##\n", __func__);
  1888. return;
  1889. }
  1890. urb = qtd->urb->priv;
  1891. if (!urb) {
  1892. dev_dbg(hsotg->dev, "## %s: urb->priv is NULL ##\n", __func__);
  1893. return;
  1894. }
  1895. urb->actual_length = dwc2_hcd_urb_get_actual_length(qtd->urb);
  1896. if (dbg_urb(urb))
  1897. dev_vdbg(hsotg->dev,
  1898. "%s: urb %p device %d ep %d-%s status %d actual %d\n",
  1899. __func__, urb, usb_pipedevice(urb->pipe),
  1900. usb_pipeendpoint(urb->pipe),
  1901. usb_pipein(urb->pipe) ? "IN" : "OUT", status,
  1902. urb->actual_length);
  1903. if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS && dbg_perio()) {
  1904. for (i = 0; i < urb->number_of_packets; i++)
  1905. dev_vdbg(hsotg->dev, " ISO Desc %d status %d\n",
  1906. i, urb->iso_frame_desc[i].status);
  1907. }
  1908. if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  1909. urb->error_count = dwc2_hcd_urb_get_error_count(qtd->urb);
  1910. for (i = 0; i < urb->number_of_packets; ++i) {
  1911. urb->iso_frame_desc[i].actual_length =
  1912. dwc2_hcd_urb_get_iso_desc_actual_length(
  1913. qtd->urb, i);
  1914. urb->iso_frame_desc[i].status =
  1915. dwc2_hcd_urb_get_iso_desc_status(qtd->urb, i);
  1916. }
  1917. }
  1918. urb->status = status;
  1919. if (!status) {
  1920. if ((urb->transfer_flags & URB_SHORT_NOT_OK) &&
  1921. urb->actual_length < urb->transfer_buffer_length)
  1922. urb->status = -EREMOTEIO;
  1923. }
  1924. if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS ||
  1925. usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
  1926. struct usb_host_endpoint *ep = urb->ep;
  1927. if (ep)
  1928. dwc2_free_bus_bandwidth(dwc2_hsotg_to_hcd(hsotg),
  1929. dwc2_hcd_get_ep_bandwidth(hsotg, ep),
  1930. urb);
  1931. }
  1932. usb_hcd_unlink_urb_from_ep(dwc2_hsotg_to_hcd(hsotg), urb);
  1933. urb->hcpriv = NULL;
  1934. kfree(qtd->urb);
  1935. qtd->urb = NULL;
  1936. spin_unlock(&hsotg->lock);
  1937. usb_hcd_giveback_urb(dwc2_hsotg_to_hcd(hsotg), urb, status);
  1938. spin_lock(&hsotg->lock);
  1939. }
  1940. /*
  1941. * Work queue function for starting the HCD when A-Cable is connected
  1942. */
  1943. static void dwc2_hcd_start_func(struct work_struct *work)
  1944. {
  1945. struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
  1946. start_work.work);
  1947. dev_dbg(hsotg->dev, "%s() %p\n", __func__, hsotg);
  1948. dwc2_host_start(hsotg);
  1949. }
  1950. /*
  1951. * Reset work queue function
  1952. */
  1953. static void dwc2_hcd_reset_func(struct work_struct *work)
  1954. {
  1955. struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
  1956. reset_work.work);
  1957. u32 hprt0;
  1958. dev_dbg(hsotg->dev, "USB RESET function called\n");
  1959. hprt0 = dwc2_read_hprt0(hsotg);
  1960. hprt0 &= ~HPRT0_RST;
  1961. writel(hprt0, hsotg->regs + HPRT0);
  1962. hsotg->flags.b.port_reset_change = 1;
  1963. }
  1964. /*
  1965. * =========================================================================
  1966. * Linux HC Driver Functions
  1967. * =========================================================================
  1968. */
  1969. /*
  1970. * Initializes the DWC_otg controller and its root hub and prepares it for host
  1971. * mode operation. Activates the root port. Returns 0 on success and a negative
  1972. * error code on failure.
  1973. */
  1974. static int _dwc2_hcd_start(struct usb_hcd *hcd)
  1975. {
  1976. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  1977. struct usb_bus *bus = hcd_to_bus(hcd);
  1978. unsigned long flags;
  1979. dev_dbg(hsotg->dev, "DWC OTG HCD START\n");
  1980. spin_lock_irqsave(&hsotg->lock, flags);
  1981. hcd->state = HC_STATE_RUNNING;
  1982. if (dwc2_is_device_mode(hsotg)) {
  1983. spin_unlock_irqrestore(&hsotg->lock, flags);
  1984. return 0; /* why 0 ?? */
  1985. }
  1986. dwc2_hcd_reinit(hsotg);
  1987. /* Initialize and connect root hub if one is not already attached */
  1988. if (bus->root_hub) {
  1989. dev_dbg(hsotg->dev, "DWC OTG HCD Has Root Hub\n");
  1990. /* Inform the HUB driver to resume */
  1991. usb_hcd_resume_root_hub(hcd);
  1992. }
  1993. spin_unlock_irqrestore(&hsotg->lock, flags);
  1994. return 0;
  1995. }
  1996. /*
  1997. * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
  1998. * stopped.
  1999. */
  2000. static void _dwc2_hcd_stop(struct usb_hcd *hcd)
  2001. {
  2002. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  2003. unsigned long flags;
  2004. spin_lock_irqsave(&hsotg->lock, flags);
  2005. dwc2_hcd_stop(hsotg);
  2006. spin_unlock_irqrestore(&hsotg->lock, flags);
  2007. usleep_range(1000, 3000);
  2008. }
  2009. /* Returns the current frame number */
  2010. static int _dwc2_hcd_get_frame_number(struct usb_hcd *hcd)
  2011. {
  2012. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  2013. return dwc2_hcd_get_frame_number(hsotg);
  2014. }
  2015. static void dwc2_dump_urb_info(struct usb_hcd *hcd, struct urb *urb,
  2016. char *fn_name)
  2017. {
  2018. #ifdef VERBOSE_DEBUG
  2019. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  2020. char *pipetype;
  2021. char *speed;
  2022. dev_vdbg(hsotg->dev, "%s, urb %p\n", fn_name, urb);
  2023. dev_vdbg(hsotg->dev, " Device address: %d\n",
  2024. usb_pipedevice(urb->pipe));
  2025. dev_vdbg(hsotg->dev, " Endpoint: %d, %s\n",
  2026. usb_pipeendpoint(urb->pipe),
  2027. usb_pipein(urb->pipe) ? "IN" : "OUT");
  2028. switch (usb_pipetype(urb->pipe)) {
  2029. case PIPE_CONTROL:
  2030. pipetype = "CONTROL";
  2031. break;
  2032. case PIPE_BULK:
  2033. pipetype = "BULK";
  2034. break;
  2035. case PIPE_INTERRUPT:
  2036. pipetype = "INTERRUPT";
  2037. break;
  2038. case PIPE_ISOCHRONOUS:
  2039. pipetype = "ISOCHRONOUS";
  2040. break;
  2041. default:
  2042. pipetype = "UNKNOWN";
  2043. break;
  2044. }
  2045. dev_vdbg(hsotg->dev, " Endpoint type: %s %s (%s)\n", pipetype,
  2046. usb_urb_dir_in(urb) ? "IN" : "OUT", usb_pipein(urb->pipe) ?
  2047. "IN" : "OUT");
  2048. switch (urb->dev->speed) {
  2049. case USB_SPEED_HIGH:
  2050. speed = "HIGH";
  2051. break;
  2052. case USB_SPEED_FULL:
  2053. speed = "FULL";
  2054. break;
  2055. case USB_SPEED_LOW:
  2056. speed = "LOW";
  2057. break;
  2058. default:
  2059. speed = "UNKNOWN";
  2060. break;
  2061. }
  2062. dev_vdbg(hsotg->dev, " Speed: %s\n", speed);
  2063. dev_vdbg(hsotg->dev, " Max packet size: %d\n",
  2064. usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe)));
  2065. dev_vdbg(hsotg->dev, " Data buffer length: %d\n",
  2066. urb->transfer_buffer_length);
  2067. dev_vdbg(hsotg->dev, " Transfer buffer: %p, Transfer DMA: %08lx\n",
  2068. urb->transfer_buffer, (unsigned long)urb->transfer_dma);
  2069. dev_vdbg(hsotg->dev, " Setup buffer: %p, Setup DMA: %08lx\n",
  2070. urb->setup_packet, (unsigned long)urb->setup_dma);
  2071. dev_vdbg(hsotg->dev, " Interval: %d\n", urb->interval);
  2072. if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  2073. int i;
  2074. for (i = 0; i < urb->number_of_packets; i++) {
  2075. dev_vdbg(hsotg->dev, " ISO Desc %d:\n", i);
  2076. dev_vdbg(hsotg->dev, " offset: %d, length %d\n",
  2077. urb->iso_frame_desc[i].offset,
  2078. urb->iso_frame_desc[i].length);
  2079. }
  2080. }
  2081. #endif
  2082. }
  2083. /*
  2084. * Starts processing a USB transfer request specified by a USB Request Block
  2085. * (URB). mem_flags indicates the type of memory allocation to use while
  2086. * processing this URB.
  2087. */
  2088. static int _dwc2_hcd_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
  2089. gfp_t mem_flags)
  2090. {
  2091. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  2092. struct usb_host_endpoint *ep = urb->ep;
  2093. struct dwc2_hcd_urb *dwc2_urb;
  2094. int i;
  2095. int retval;
  2096. int alloc_bandwidth = 0;
  2097. u8 ep_type = 0;
  2098. u32 tflags = 0;
  2099. void *buf;
  2100. unsigned long flags;
  2101. if (dbg_urb(urb)) {
  2102. dev_vdbg(hsotg->dev, "DWC OTG HCD URB Enqueue\n");
  2103. dwc2_dump_urb_info(hcd, urb, "urb_enqueue");
  2104. }
  2105. if (ep == NULL)
  2106. return -EINVAL;
  2107. if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS ||
  2108. usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
  2109. spin_lock_irqsave(&hsotg->lock, flags);
  2110. if (!dwc2_hcd_is_bandwidth_allocated(hsotg, ep))
  2111. alloc_bandwidth = 1;
  2112. spin_unlock_irqrestore(&hsotg->lock, flags);
  2113. }
  2114. switch (usb_pipetype(urb->pipe)) {
  2115. case PIPE_CONTROL:
  2116. ep_type = USB_ENDPOINT_XFER_CONTROL;
  2117. break;
  2118. case PIPE_ISOCHRONOUS:
  2119. ep_type = USB_ENDPOINT_XFER_ISOC;
  2120. break;
  2121. case PIPE_BULK:
  2122. ep_type = USB_ENDPOINT_XFER_BULK;
  2123. break;
  2124. case PIPE_INTERRUPT:
  2125. ep_type = USB_ENDPOINT_XFER_INT;
  2126. break;
  2127. default:
  2128. dev_warn(hsotg->dev, "Wrong ep type\n");
  2129. }
  2130. dwc2_urb = dwc2_hcd_urb_alloc(hsotg, urb->number_of_packets,
  2131. mem_flags);
  2132. if (!dwc2_urb)
  2133. return -ENOMEM;
  2134. dwc2_hcd_urb_set_pipeinfo(hsotg, dwc2_urb, usb_pipedevice(urb->pipe),
  2135. usb_pipeendpoint(urb->pipe), ep_type,
  2136. usb_pipein(urb->pipe),
  2137. usb_maxpacket(urb->dev, urb->pipe,
  2138. !(usb_pipein(urb->pipe))));
  2139. buf = urb->transfer_buffer;
  2140. if (hcd->self.uses_dma) {
  2141. if (!buf && (urb->transfer_dma & 3)) {
  2142. dev_err(hsotg->dev,
  2143. "%s: unaligned transfer with no transfer_buffer",
  2144. __func__);
  2145. retval = -EINVAL;
  2146. goto fail1;
  2147. }
  2148. }
  2149. if (!(urb->transfer_flags & URB_NO_INTERRUPT))
  2150. tflags |= URB_GIVEBACK_ASAP;
  2151. if (urb->transfer_flags & URB_ZERO_PACKET)
  2152. tflags |= URB_SEND_ZERO_PACKET;
  2153. dwc2_urb->priv = urb;
  2154. dwc2_urb->buf = buf;
  2155. dwc2_urb->dma = urb->transfer_dma;
  2156. dwc2_urb->length = urb->transfer_buffer_length;
  2157. dwc2_urb->setup_packet = urb->setup_packet;
  2158. dwc2_urb->setup_dma = urb->setup_dma;
  2159. dwc2_urb->flags = tflags;
  2160. dwc2_urb->interval = urb->interval;
  2161. dwc2_urb->status = -EINPROGRESS;
  2162. for (i = 0; i < urb->number_of_packets; ++i)
  2163. dwc2_hcd_urb_set_iso_desc_params(dwc2_urb, i,
  2164. urb->iso_frame_desc[i].offset,
  2165. urb->iso_frame_desc[i].length);
  2166. urb->hcpriv = dwc2_urb;
  2167. spin_lock_irqsave(&hsotg->lock, flags);
  2168. retval = usb_hcd_link_urb_to_ep(hcd, urb);
  2169. spin_unlock_irqrestore(&hsotg->lock, flags);
  2170. if (retval)
  2171. goto fail1;
  2172. retval = dwc2_hcd_urb_enqueue(hsotg, dwc2_urb, &ep->hcpriv, mem_flags);
  2173. if (retval)
  2174. goto fail2;
  2175. if (alloc_bandwidth) {
  2176. spin_lock_irqsave(&hsotg->lock, flags);
  2177. dwc2_allocate_bus_bandwidth(hcd,
  2178. dwc2_hcd_get_ep_bandwidth(hsotg, ep),
  2179. urb);
  2180. spin_unlock_irqrestore(&hsotg->lock, flags);
  2181. }
  2182. return 0;
  2183. fail2:
  2184. spin_lock_irqsave(&hsotg->lock, flags);
  2185. dwc2_urb->priv = NULL;
  2186. usb_hcd_unlink_urb_from_ep(hcd, urb);
  2187. spin_unlock_irqrestore(&hsotg->lock, flags);
  2188. fail1:
  2189. urb->hcpriv = NULL;
  2190. kfree(dwc2_urb);
  2191. return retval;
  2192. }
  2193. /*
  2194. * Aborts/cancels a USB transfer request. Always returns 0 to indicate success.
  2195. */
  2196. static int _dwc2_hcd_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
  2197. int status)
  2198. {
  2199. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  2200. int rc;
  2201. unsigned long flags;
  2202. dev_dbg(hsotg->dev, "DWC OTG HCD URB Dequeue\n");
  2203. dwc2_dump_urb_info(hcd, urb, "urb_dequeue");
  2204. spin_lock_irqsave(&hsotg->lock, flags);
  2205. rc = usb_hcd_check_unlink_urb(hcd, urb, status);
  2206. if (rc)
  2207. goto out;
  2208. if (!urb->hcpriv) {
  2209. dev_dbg(hsotg->dev, "## urb->hcpriv is NULL ##\n");
  2210. goto out;
  2211. }
  2212. rc = dwc2_hcd_urb_dequeue(hsotg, urb->hcpriv);
  2213. usb_hcd_unlink_urb_from_ep(hcd, urb);
  2214. kfree(urb->hcpriv);
  2215. urb->hcpriv = NULL;
  2216. /* Higher layer software sets URB status */
  2217. spin_unlock(&hsotg->lock);
  2218. usb_hcd_giveback_urb(hcd, urb, status);
  2219. spin_lock(&hsotg->lock);
  2220. dev_dbg(hsotg->dev, "Called usb_hcd_giveback_urb()\n");
  2221. dev_dbg(hsotg->dev, " urb->status = %d\n", urb->status);
  2222. out:
  2223. spin_unlock_irqrestore(&hsotg->lock, flags);
  2224. return rc;
  2225. }
  2226. /*
  2227. * Frees resources in the DWC_otg controller related to a given endpoint. Also
  2228. * clears state in the HCD related to the endpoint. Any URBs for the endpoint
  2229. * must already be dequeued.
  2230. */
  2231. static void _dwc2_hcd_endpoint_disable(struct usb_hcd *hcd,
  2232. struct usb_host_endpoint *ep)
  2233. {
  2234. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  2235. dev_dbg(hsotg->dev,
  2236. "DWC OTG HCD EP DISABLE: bEndpointAddress=0x%02x, ep->hcpriv=%p\n",
  2237. ep->desc.bEndpointAddress, ep->hcpriv);
  2238. dwc2_hcd_endpoint_disable(hsotg, ep, 250);
  2239. }
  2240. /*
  2241. * Resets endpoint specific parameter values, in current version used to reset
  2242. * the data toggle (as a WA). This function can be called from usb_clear_halt
  2243. * routine.
  2244. */
  2245. static void _dwc2_hcd_endpoint_reset(struct usb_hcd *hcd,
  2246. struct usb_host_endpoint *ep)
  2247. {
  2248. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  2249. unsigned long flags;
  2250. dev_dbg(hsotg->dev,
  2251. "DWC OTG HCD EP RESET: bEndpointAddress=0x%02x\n",
  2252. ep->desc.bEndpointAddress);
  2253. spin_lock_irqsave(&hsotg->lock, flags);
  2254. dwc2_hcd_endpoint_reset(hsotg, ep);
  2255. spin_unlock_irqrestore(&hsotg->lock, flags);
  2256. }
  2257. /*
  2258. * Handles host mode interrupts for the DWC_otg controller. Returns IRQ_NONE if
  2259. * there was no interrupt to handle. Returns IRQ_HANDLED if there was a valid
  2260. * interrupt.
  2261. *
  2262. * This function is called by the USB core when an interrupt occurs
  2263. */
  2264. static irqreturn_t _dwc2_hcd_irq(struct usb_hcd *hcd)
  2265. {
  2266. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  2267. return dwc2_handle_hcd_intr(hsotg);
  2268. }
  2269. /*
  2270. * Creates Status Change bitmap for the root hub and root port. The bitmap is
  2271. * returned in buf. Bit 0 is the status change indicator for the root hub. Bit 1
  2272. * is the status change indicator for the single root port. Returns 1 if either
  2273. * change indicator is 1, otherwise returns 0.
  2274. */
  2275. static int _dwc2_hcd_hub_status_data(struct usb_hcd *hcd, char *buf)
  2276. {
  2277. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  2278. buf[0] = dwc2_hcd_is_status_changed(hsotg, 1) << 1;
  2279. return buf[0] != 0;
  2280. }
  2281. /* Handles hub class-specific requests */
  2282. static int _dwc2_hcd_hub_control(struct usb_hcd *hcd, u16 typereq, u16 wvalue,
  2283. u16 windex, char *buf, u16 wlength)
  2284. {
  2285. int retval = dwc2_hcd_hub_control(dwc2_hcd_to_hsotg(hcd), typereq,
  2286. wvalue, windex, buf, wlength);
  2287. return retval;
  2288. }
  2289. /* Handles hub TT buffer clear completions */
  2290. static void _dwc2_hcd_clear_tt_buffer_complete(struct usb_hcd *hcd,
  2291. struct usb_host_endpoint *ep)
  2292. {
  2293. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  2294. struct dwc2_qh *qh;
  2295. unsigned long flags;
  2296. qh = ep->hcpriv;
  2297. if (!qh)
  2298. return;
  2299. spin_lock_irqsave(&hsotg->lock, flags);
  2300. qh->tt_buffer_dirty = 0;
  2301. if (hsotg->flags.b.port_connect_status)
  2302. dwc2_hcd_queue_transactions(hsotg, DWC2_TRANSACTION_ALL);
  2303. spin_unlock_irqrestore(&hsotg->lock, flags);
  2304. }
  2305. static struct hc_driver dwc2_hc_driver = {
  2306. .description = "dwc2_hsotg",
  2307. .product_desc = "DWC OTG Controller",
  2308. .hcd_priv_size = sizeof(struct wrapper_priv_data),
  2309. .irq = _dwc2_hcd_irq,
  2310. .flags = HCD_MEMORY | HCD_USB2,
  2311. .start = _dwc2_hcd_start,
  2312. .stop = _dwc2_hcd_stop,
  2313. .urb_enqueue = _dwc2_hcd_urb_enqueue,
  2314. .urb_dequeue = _dwc2_hcd_urb_dequeue,
  2315. .endpoint_disable = _dwc2_hcd_endpoint_disable,
  2316. .endpoint_reset = _dwc2_hcd_endpoint_reset,
  2317. .get_frame_number = _dwc2_hcd_get_frame_number,
  2318. .hub_status_data = _dwc2_hcd_hub_status_data,
  2319. .hub_control = _dwc2_hcd_hub_control,
  2320. .clear_tt_buffer_complete = _dwc2_hcd_clear_tt_buffer_complete,
  2321. };
  2322. /*
  2323. * Frees secondary storage associated with the dwc2_hsotg structure contained
  2324. * in the struct usb_hcd field
  2325. */
  2326. static void dwc2_hcd_free(struct dwc2_hsotg *hsotg)
  2327. {
  2328. u32 ahbcfg;
  2329. u32 dctl;
  2330. int i;
  2331. dev_dbg(hsotg->dev, "DWC OTG HCD FREE\n");
  2332. /* Free memory for QH/QTD lists */
  2333. dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_inactive);
  2334. dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_active);
  2335. dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_inactive);
  2336. dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_ready);
  2337. dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_assigned);
  2338. dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_queued);
  2339. /* Free memory for the host channels */
  2340. for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  2341. struct dwc2_host_chan *chan = hsotg->hc_ptr_array[i];
  2342. if (chan != NULL) {
  2343. dev_dbg(hsotg->dev, "HCD Free channel #%i, chan=%p\n",
  2344. i, chan);
  2345. hsotg->hc_ptr_array[i] = NULL;
  2346. kfree(chan);
  2347. }
  2348. }
  2349. if (hsotg->core_params->dma_enable > 0) {
  2350. if (hsotg->status_buf) {
  2351. dma_free_coherent(hsotg->dev, DWC2_HCD_STATUS_BUF_SIZE,
  2352. hsotg->status_buf,
  2353. hsotg->status_buf_dma);
  2354. hsotg->status_buf = NULL;
  2355. }
  2356. } else {
  2357. kfree(hsotg->status_buf);
  2358. hsotg->status_buf = NULL;
  2359. }
  2360. ahbcfg = readl(hsotg->regs + GAHBCFG);
  2361. /* Disable all interrupts */
  2362. ahbcfg &= ~GAHBCFG_GLBL_INTR_EN;
  2363. writel(ahbcfg, hsotg->regs + GAHBCFG);
  2364. writel(0, hsotg->regs + GINTMSK);
  2365. if (hsotg->hw_params.snpsid >= DWC2_CORE_REV_3_00a) {
  2366. dctl = readl(hsotg->regs + DCTL);
  2367. dctl |= DCTL_SFTDISCON;
  2368. writel(dctl, hsotg->regs + DCTL);
  2369. }
  2370. if (hsotg->wq_otg) {
  2371. if (!cancel_work_sync(&hsotg->wf_otg))
  2372. flush_workqueue(hsotg->wq_otg);
  2373. destroy_workqueue(hsotg->wq_otg);
  2374. }
  2375. kfree(hsotg->core_params);
  2376. hsotg->core_params = NULL;
  2377. del_timer(&hsotg->wkp_timer);
  2378. }
  2379. static void dwc2_hcd_release(struct dwc2_hsotg *hsotg)
  2380. {
  2381. /* Turn off all host-specific interrupts */
  2382. dwc2_disable_host_interrupts(hsotg);
  2383. dwc2_hcd_free(hsotg);
  2384. }
  2385. /*
  2386. * Sets all parameters to the given value.
  2387. *
  2388. * Assumes that the dwc2_core_params struct contains only integers.
  2389. */
  2390. void dwc2_set_all_params(struct dwc2_core_params *params, int value)
  2391. {
  2392. int *p = (int *)params;
  2393. size_t size = sizeof(*params) / sizeof(*p);
  2394. int i;
  2395. for (i = 0; i < size; i++)
  2396. p[i] = value;
  2397. }
  2398. EXPORT_SYMBOL_GPL(dwc2_set_all_params);
  2399. /*
  2400. * Initializes the HCD. This function allocates memory for and initializes the
  2401. * static parts of the usb_hcd and dwc2_hsotg structures. It also registers the
  2402. * USB bus with the core and calls the hc_driver->start() function. It returns
  2403. * a negative error on failure.
  2404. */
  2405. int dwc2_hcd_init(struct dwc2_hsotg *hsotg, int irq,
  2406. const struct dwc2_core_params *params)
  2407. {
  2408. struct usb_hcd *hcd;
  2409. struct dwc2_host_chan *channel;
  2410. u32 hcfg;
  2411. int i, num_channels;
  2412. int retval;
  2413. dev_dbg(hsotg->dev, "DWC OTG HCD INIT\n");
  2414. /* Detect config values from hardware */
  2415. retval = dwc2_get_hwparams(hsotg);
  2416. if (retval)
  2417. return retval;
  2418. retval = -ENOMEM;
  2419. hcfg = readl(hsotg->regs + HCFG);
  2420. dev_dbg(hsotg->dev, "hcfg=%08x\n", hcfg);
  2421. #ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
  2422. hsotg->frame_num_array = kzalloc(sizeof(*hsotg->frame_num_array) *
  2423. FRAME_NUM_ARRAY_SIZE, GFP_KERNEL);
  2424. if (!hsotg->frame_num_array)
  2425. goto error1;
  2426. hsotg->last_frame_num_array = kzalloc(
  2427. sizeof(*hsotg->last_frame_num_array) *
  2428. FRAME_NUM_ARRAY_SIZE, GFP_KERNEL);
  2429. if (!hsotg->last_frame_num_array)
  2430. goto error1;
  2431. hsotg->last_frame_num = HFNUM_MAX_FRNUM;
  2432. #endif
  2433. hsotg->core_params = kzalloc(sizeof(*hsotg->core_params), GFP_KERNEL);
  2434. if (!hsotg->core_params)
  2435. goto error1;
  2436. dwc2_set_all_params(hsotg->core_params, -1);
  2437. /* Validate parameter values */
  2438. dwc2_set_parameters(hsotg, params);
  2439. /* Check if the bus driver or platform code has setup a dma_mask */
  2440. if (hsotg->core_params->dma_enable > 0 &&
  2441. hsotg->dev->dma_mask == NULL) {
  2442. dev_warn(hsotg->dev,
  2443. "dma_mask not set, disabling DMA\n");
  2444. hsotg->core_params->dma_enable = 0;
  2445. hsotg->core_params->dma_desc_enable = 0;
  2446. }
  2447. /* Set device flags indicating whether the HCD supports DMA */
  2448. if (hsotg->core_params->dma_enable > 0) {
  2449. if (dma_set_mask(hsotg->dev, DMA_BIT_MASK(32)) < 0)
  2450. dev_warn(hsotg->dev, "can't set DMA mask\n");
  2451. if (dma_set_coherent_mask(hsotg->dev, DMA_BIT_MASK(32)) < 0)
  2452. dev_warn(hsotg->dev, "can't set coherent DMA mask\n");
  2453. }
  2454. hcd = usb_create_hcd(&dwc2_hc_driver, hsotg->dev, dev_name(hsotg->dev));
  2455. if (!hcd)
  2456. goto error1;
  2457. if (hsotg->core_params->dma_enable <= 0)
  2458. hcd->self.uses_dma = 0;
  2459. hcd->has_tt = 1;
  2460. spin_lock_init(&hsotg->lock);
  2461. ((struct wrapper_priv_data *) &hcd->hcd_priv)->hsotg = hsotg;
  2462. hsotg->priv = hcd;
  2463. /*
  2464. * Disable the global interrupt until all the interrupt handlers are
  2465. * installed
  2466. */
  2467. dwc2_disable_global_interrupts(hsotg);
  2468. /* Initialize the DWC_otg core, and select the Phy type */
  2469. retval = dwc2_core_init(hsotg, true, irq);
  2470. if (retval)
  2471. goto error2;
  2472. /* Create new workqueue and init work */
  2473. retval = -ENOMEM;
  2474. hsotg->wq_otg = create_singlethread_workqueue("dwc2");
  2475. if (!hsotg->wq_otg) {
  2476. dev_err(hsotg->dev, "Failed to create workqueue\n");
  2477. goto error2;
  2478. }
  2479. INIT_WORK(&hsotg->wf_otg, dwc2_conn_id_status_change);
  2480. setup_timer(&hsotg->wkp_timer, dwc2_wakeup_detected,
  2481. (unsigned long)hsotg);
  2482. /* Initialize the non-periodic schedule */
  2483. INIT_LIST_HEAD(&hsotg->non_periodic_sched_inactive);
  2484. INIT_LIST_HEAD(&hsotg->non_periodic_sched_active);
  2485. /* Initialize the periodic schedule */
  2486. INIT_LIST_HEAD(&hsotg->periodic_sched_inactive);
  2487. INIT_LIST_HEAD(&hsotg->periodic_sched_ready);
  2488. INIT_LIST_HEAD(&hsotg->periodic_sched_assigned);
  2489. INIT_LIST_HEAD(&hsotg->periodic_sched_queued);
  2490. /*
  2491. * Create a host channel descriptor for each host channel implemented
  2492. * in the controller. Initialize the channel descriptor array.
  2493. */
  2494. INIT_LIST_HEAD(&hsotg->free_hc_list);
  2495. num_channels = hsotg->core_params->host_channels;
  2496. memset(&hsotg->hc_ptr_array[0], 0, sizeof(hsotg->hc_ptr_array));
  2497. for (i = 0; i < num_channels; i++) {
  2498. channel = kzalloc(sizeof(*channel), GFP_KERNEL);
  2499. if (channel == NULL)
  2500. goto error3;
  2501. channel->hc_num = i;
  2502. hsotg->hc_ptr_array[i] = channel;
  2503. }
  2504. if (hsotg->core_params->uframe_sched > 0)
  2505. dwc2_hcd_init_usecs(hsotg);
  2506. /* Initialize hsotg start work */
  2507. INIT_DELAYED_WORK(&hsotg->start_work, dwc2_hcd_start_func);
  2508. /* Initialize port reset work */
  2509. INIT_DELAYED_WORK(&hsotg->reset_work, dwc2_hcd_reset_func);
  2510. /*
  2511. * Allocate space for storing data on status transactions. Normally no
  2512. * data is sent, but this space acts as a bit bucket. This must be
  2513. * done after usb_add_hcd since that function allocates the DMA buffer
  2514. * pool.
  2515. */
  2516. if (hsotg->core_params->dma_enable > 0)
  2517. hsotg->status_buf = dma_alloc_coherent(hsotg->dev,
  2518. DWC2_HCD_STATUS_BUF_SIZE,
  2519. &hsotg->status_buf_dma, GFP_KERNEL);
  2520. else
  2521. hsotg->status_buf = kzalloc(DWC2_HCD_STATUS_BUF_SIZE,
  2522. GFP_KERNEL);
  2523. if (!hsotg->status_buf)
  2524. goto error3;
  2525. hsotg->otg_port = 1;
  2526. hsotg->frame_list = NULL;
  2527. hsotg->frame_list_dma = 0;
  2528. hsotg->periodic_qh_count = 0;
  2529. /* Initiate lx_state to L3 disconnected state */
  2530. hsotg->lx_state = DWC2_L3;
  2531. hcd->self.otg_port = hsotg->otg_port;
  2532. /* Don't support SG list at this point */
  2533. hcd->self.sg_tablesize = 0;
  2534. /*
  2535. * Finish generic HCD initialization and start the HCD. This function
  2536. * allocates the DMA buffer pool, registers the USB bus, requests the
  2537. * IRQ line, and calls hcd_start method.
  2538. */
  2539. retval = usb_add_hcd(hcd, irq, IRQF_SHARED);
  2540. if (retval < 0)
  2541. goto error3;
  2542. device_wakeup_enable(hcd->self.controller);
  2543. dwc2_hcd_dump_state(hsotg);
  2544. dwc2_enable_global_interrupts(hsotg);
  2545. return 0;
  2546. error3:
  2547. dwc2_hcd_release(hsotg);
  2548. error2:
  2549. usb_put_hcd(hcd);
  2550. error1:
  2551. kfree(hsotg->core_params);
  2552. #ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
  2553. kfree(hsotg->last_frame_num_array);
  2554. kfree(hsotg->frame_num_array);
  2555. #endif
  2556. dev_err(hsotg->dev, "%s() FAILED, returning %d\n", __func__, retval);
  2557. return retval;
  2558. }
  2559. EXPORT_SYMBOL_GPL(dwc2_hcd_init);
  2560. /*
  2561. * Removes the HCD.
  2562. * Frees memory and resources associated with the HCD and deregisters the bus.
  2563. */
  2564. void dwc2_hcd_remove(struct dwc2_hsotg *hsotg)
  2565. {
  2566. struct usb_hcd *hcd;
  2567. dev_dbg(hsotg->dev, "DWC OTG HCD REMOVE\n");
  2568. hcd = dwc2_hsotg_to_hcd(hsotg);
  2569. dev_dbg(hsotg->dev, "hsotg->hcd = %p\n", hcd);
  2570. if (!hcd) {
  2571. dev_dbg(hsotg->dev, "%s: dwc2_hsotg_to_hcd(hsotg) NULL!\n",
  2572. __func__);
  2573. return;
  2574. }
  2575. usb_remove_hcd(hcd);
  2576. hsotg->priv = NULL;
  2577. dwc2_hcd_release(hsotg);
  2578. usb_put_hcd(hcd);
  2579. #ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
  2580. kfree(hsotg->last_frame_num_array);
  2581. kfree(hsotg->frame_num_array);
  2582. #endif
  2583. }
  2584. EXPORT_SYMBOL_GPL(dwc2_hcd_remove);