hcd.c 85 KB

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