hcd.c 84 KB

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