hcd.c 83 KB

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