hcd_intr.c 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295
  1. // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
  2. /*
  3. * hcd_intr.c - DesignWare HS OTG Controller host-mode interrupt handling
  4. *
  5. * Copyright (C) 2004-2013 Synopsys, Inc.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions, and the following disclaimer,
  12. * without modification.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. The names of the above-listed copyright holders may not be used
  17. * to endorse or promote products derived from this software without
  18. * specific prior written permission.
  19. *
  20. * ALTERNATIVELY, this software may be distributed under the terms of the
  21. * GNU General Public License ("GPL") as published by the Free Software
  22. * Foundation; either version 2 of the License, or (at your option) any
  23. * later version.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  26. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  27. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  28. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  29. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  30. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  31. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  32. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  33. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  34. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. */
  37. /*
  38. * This file contains the interrupt handlers for Host mode
  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/io.h>
  46. #include <linux/slab.h>
  47. #include <linux/usb.h>
  48. #include <linux/usb/hcd.h>
  49. #include <linux/usb/ch11.h>
  50. #include "core.h"
  51. #include "hcd.h"
  52. /*
  53. * If we get this many NAKs on a split transaction we'll slow down
  54. * retransmission. A 1 here means delay after the first NAK.
  55. */
  56. #define DWC2_NAKS_BEFORE_DELAY 3
  57. /* This function is for debug only */
  58. static void dwc2_track_missed_sofs(struct dwc2_hsotg *hsotg)
  59. {
  60. u16 curr_frame_number = hsotg->frame_number;
  61. u16 expected = dwc2_frame_num_inc(hsotg->last_frame_num, 1);
  62. if (expected != curr_frame_number)
  63. dwc2_sch_vdbg(hsotg, "MISSED SOF %04x != %04x\n",
  64. expected, curr_frame_number);
  65. #ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
  66. if (hsotg->frame_num_idx < FRAME_NUM_ARRAY_SIZE) {
  67. if (expected != curr_frame_number) {
  68. hsotg->frame_num_array[hsotg->frame_num_idx] =
  69. curr_frame_number;
  70. hsotg->last_frame_num_array[hsotg->frame_num_idx] =
  71. hsotg->last_frame_num;
  72. hsotg->frame_num_idx++;
  73. }
  74. } else if (!hsotg->dumped_frame_num_array) {
  75. int i;
  76. dev_info(hsotg->dev, "Frame Last Frame\n");
  77. dev_info(hsotg->dev, "----- ----------\n");
  78. for (i = 0; i < FRAME_NUM_ARRAY_SIZE; i++) {
  79. dev_info(hsotg->dev, "0x%04x 0x%04x\n",
  80. hsotg->frame_num_array[i],
  81. hsotg->last_frame_num_array[i]);
  82. }
  83. hsotg->dumped_frame_num_array = 1;
  84. }
  85. #endif
  86. hsotg->last_frame_num = curr_frame_number;
  87. }
  88. static void dwc2_hc_handle_tt_clear(struct dwc2_hsotg *hsotg,
  89. struct dwc2_host_chan *chan,
  90. struct dwc2_qtd *qtd)
  91. {
  92. struct usb_device *root_hub = dwc2_hsotg_to_hcd(hsotg)->self.root_hub;
  93. struct urb *usb_urb;
  94. if (!chan->qh)
  95. return;
  96. if (chan->qh->dev_speed == USB_SPEED_HIGH)
  97. return;
  98. if (!qtd->urb)
  99. return;
  100. usb_urb = qtd->urb->priv;
  101. if (!usb_urb || !usb_urb->dev || !usb_urb->dev->tt)
  102. return;
  103. /*
  104. * The root hub doesn't really have a TT, but Linux thinks it
  105. * does because how could you have a "high speed hub" that
  106. * directly talks directly to low speed devices without a TT?
  107. * It's all lies. Lies, I tell you.
  108. */
  109. if (usb_urb->dev->tt->hub == root_hub)
  110. return;
  111. if (qtd->urb->status != -EPIPE && qtd->urb->status != -EREMOTEIO) {
  112. chan->qh->tt_buffer_dirty = 1;
  113. if (usb_hub_clear_tt_buffer(usb_urb))
  114. /* Clear failed; let's hope things work anyway */
  115. chan->qh->tt_buffer_dirty = 0;
  116. }
  117. }
  118. /*
  119. * Handles the start-of-frame interrupt in host mode. Non-periodic
  120. * transactions may be queued to the DWC_otg controller for the current
  121. * (micro)frame. Periodic transactions may be queued to the controller
  122. * for the next (micro)frame.
  123. */
  124. static void dwc2_sof_intr(struct dwc2_hsotg *hsotg)
  125. {
  126. struct list_head *qh_entry;
  127. struct dwc2_qh *qh;
  128. enum dwc2_transaction_type tr_type;
  129. /* Clear interrupt */
  130. dwc2_writel(hsotg, GINTSTS_SOF, GINTSTS);
  131. #ifdef DEBUG_SOF
  132. dev_vdbg(hsotg->dev, "--Start of Frame Interrupt--\n");
  133. #endif
  134. hsotg->frame_number = dwc2_hcd_get_frame_number(hsotg);
  135. dwc2_track_missed_sofs(hsotg);
  136. /* Determine whether any periodic QHs should be executed */
  137. qh_entry = hsotg->periodic_sched_inactive.next;
  138. while (qh_entry != &hsotg->periodic_sched_inactive) {
  139. qh = list_entry(qh_entry, struct dwc2_qh, qh_list_entry);
  140. qh_entry = qh_entry->next;
  141. if (dwc2_frame_num_le(qh->next_active_frame,
  142. hsotg->frame_number)) {
  143. dwc2_sch_vdbg(hsotg, "QH=%p ready fn=%04x, nxt=%04x\n",
  144. qh, hsotg->frame_number,
  145. qh->next_active_frame);
  146. /*
  147. * Move QH to the ready list to be executed next
  148. * (micro)frame
  149. */
  150. list_move_tail(&qh->qh_list_entry,
  151. &hsotg->periodic_sched_ready);
  152. }
  153. }
  154. tr_type = dwc2_hcd_select_transactions(hsotg);
  155. if (tr_type != DWC2_TRANSACTION_NONE)
  156. dwc2_hcd_queue_transactions(hsotg, tr_type);
  157. }
  158. /*
  159. * Handles the Rx FIFO Level Interrupt, which indicates that there is
  160. * at least one packet in the Rx FIFO. The packets are moved from the FIFO to
  161. * memory if the DWC_otg controller is operating in Slave mode.
  162. */
  163. static void dwc2_rx_fifo_level_intr(struct dwc2_hsotg *hsotg)
  164. {
  165. u32 grxsts, chnum, bcnt, dpid, pktsts;
  166. struct dwc2_host_chan *chan;
  167. if (dbg_perio())
  168. dev_vdbg(hsotg->dev, "--RxFIFO Level Interrupt--\n");
  169. grxsts = dwc2_readl(hsotg, GRXSTSP);
  170. chnum = (grxsts & GRXSTS_HCHNUM_MASK) >> GRXSTS_HCHNUM_SHIFT;
  171. chan = hsotg->hc_ptr_array[chnum];
  172. if (!chan) {
  173. dev_err(hsotg->dev, "Unable to get corresponding channel\n");
  174. return;
  175. }
  176. bcnt = (grxsts & GRXSTS_BYTECNT_MASK) >> GRXSTS_BYTECNT_SHIFT;
  177. dpid = (grxsts & GRXSTS_DPID_MASK) >> GRXSTS_DPID_SHIFT;
  178. pktsts = (grxsts & GRXSTS_PKTSTS_MASK) >> GRXSTS_PKTSTS_SHIFT;
  179. /* Packet Status */
  180. if (dbg_perio()) {
  181. dev_vdbg(hsotg->dev, " Ch num = %d\n", chnum);
  182. dev_vdbg(hsotg->dev, " Count = %d\n", bcnt);
  183. dev_vdbg(hsotg->dev, " DPID = %d, chan.dpid = %d\n", dpid,
  184. chan->data_pid_start);
  185. dev_vdbg(hsotg->dev, " PStatus = %d\n", pktsts);
  186. }
  187. switch (pktsts) {
  188. case GRXSTS_PKTSTS_HCHIN:
  189. /* Read the data into the host buffer */
  190. if (bcnt > 0) {
  191. dwc2_read_packet(hsotg, chan->xfer_buf, bcnt);
  192. /* Update the HC fields for the next packet received */
  193. chan->xfer_count += bcnt;
  194. chan->xfer_buf += bcnt;
  195. }
  196. break;
  197. case GRXSTS_PKTSTS_HCHIN_XFER_COMP:
  198. case GRXSTS_PKTSTS_DATATOGGLEERR:
  199. case GRXSTS_PKTSTS_HCHHALTED:
  200. /* Handled in interrupt, just ignore data */
  201. break;
  202. default:
  203. dev_err(hsotg->dev,
  204. "RxFIFO Level Interrupt: Unknown status %d\n", pktsts);
  205. break;
  206. }
  207. }
  208. /*
  209. * This interrupt occurs when the non-periodic Tx FIFO is half-empty. More
  210. * data packets may be written to the FIFO for OUT transfers. More requests
  211. * may be written to the non-periodic request queue for IN transfers. This
  212. * interrupt is enabled only in Slave mode.
  213. */
  214. static void dwc2_np_tx_fifo_empty_intr(struct dwc2_hsotg *hsotg)
  215. {
  216. dev_vdbg(hsotg->dev, "--Non-Periodic TxFIFO Empty Interrupt--\n");
  217. dwc2_hcd_queue_transactions(hsotg, DWC2_TRANSACTION_NON_PERIODIC);
  218. }
  219. /*
  220. * This interrupt occurs when the periodic Tx FIFO is half-empty. More data
  221. * packets may be written to the FIFO for OUT transfers. More requests may be
  222. * written to the periodic request queue for IN transfers. This interrupt is
  223. * enabled only in Slave mode.
  224. */
  225. static void dwc2_perio_tx_fifo_empty_intr(struct dwc2_hsotg *hsotg)
  226. {
  227. if (dbg_perio())
  228. dev_vdbg(hsotg->dev, "--Periodic TxFIFO Empty Interrupt--\n");
  229. dwc2_hcd_queue_transactions(hsotg, DWC2_TRANSACTION_PERIODIC);
  230. }
  231. static void dwc2_hprt0_enable(struct dwc2_hsotg *hsotg, u32 hprt0,
  232. u32 *hprt0_modify)
  233. {
  234. struct dwc2_core_params *params = &hsotg->params;
  235. int do_reset = 0;
  236. u32 usbcfg;
  237. u32 prtspd;
  238. u32 hcfg;
  239. u32 fslspclksel;
  240. u32 hfir;
  241. dev_vdbg(hsotg->dev, "%s(%p)\n", __func__, hsotg);
  242. /* Every time when port enables calculate HFIR.FrInterval */
  243. hfir = dwc2_readl(hsotg, HFIR);
  244. hfir &= ~HFIR_FRINT_MASK;
  245. hfir |= dwc2_calc_frame_interval(hsotg) << HFIR_FRINT_SHIFT &
  246. HFIR_FRINT_MASK;
  247. dwc2_writel(hsotg, hfir, HFIR);
  248. /* Check if we need to adjust the PHY clock speed for low power */
  249. if (!params->host_support_fs_ls_low_power) {
  250. /* Port has been enabled, set the reset change flag */
  251. hsotg->flags.b.port_reset_change = 1;
  252. return;
  253. }
  254. usbcfg = dwc2_readl(hsotg, GUSBCFG);
  255. prtspd = (hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT;
  256. if (prtspd == HPRT0_SPD_LOW_SPEED || prtspd == HPRT0_SPD_FULL_SPEED) {
  257. /* Low power */
  258. if (!(usbcfg & GUSBCFG_PHY_LP_CLK_SEL)) {
  259. /* Set PHY low power clock select for FS/LS devices */
  260. usbcfg |= GUSBCFG_PHY_LP_CLK_SEL;
  261. dwc2_writel(hsotg, usbcfg, GUSBCFG);
  262. do_reset = 1;
  263. }
  264. hcfg = dwc2_readl(hsotg, HCFG);
  265. fslspclksel = (hcfg & HCFG_FSLSPCLKSEL_MASK) >>
  266. HCFG_FSLSPCLKSEL_SHIFT;
  267. if (prtspd == HPRT0_SPD_LOW_SPEED &&
  268. params->host_ls_low_power_phy_clk) {
  269. /* 6 MHZ */
  270. dev_vdbg(hsotg->dev,
  271. "FS_PHY programming HCFG to 6 MHz\n");
  272. if (fslspclksel != HCFG_FSLSPCLKSEL_6_MHZ) {
  273. fslspclksel = HCFG_FSLSPCLKSEL_6_MHZ;
  274. hcfg &= ~HCFG_FSLSPCLKSEL_MASK;
  275. hcfg |= fslspclksel << HCFG_FSLSPCLKSEL_SHIFT;
  276. dwc2_writel(hsotg, hcfg, HCFG);
  277. do_reset = 1;
  278. }
  279. } else {
  280. /* 48 MHZ */
  281. dev_vdbg(hsotg->dev,
  282. "FS_PHY programming HCFG to 48 MHz\n");
  283. if (fslspclksel != HCFG_FSLSPCLKSEL_48_MHZ) {
  284. fslspclksel = HCFG_FSLSPCLKSEL_48_MHZ;
  285. hcfg &= ~HCFG_FSLSPCLKSEL_MASK;
  286. hcfg |= fslspclksel << HCFG_FSLSPCLKSEL_SHIFT;
  287. dwc2_writel(hsotg, hcfg, HCFG);
  288. do_reset = 1;
  289. }
  290. }
  291. } else {
  292. /* Not low power */
  293. if (usbcfg & GUSBCFG_PHY_LP_CLK_SEL) {
  294. usbcfg &= ~GUSBCFG_PHY_LP_CLK_SEL;
  295. dwc2_writel(hsotg, usbcfg, GUSBCFG);
  296. do_reset = 1;
  297. }
  298. }
  299. if (do_reset) {
  300. *hprt0_modify |= HPRT0_RST;
  301. dwc2_writel(hsotg, *hprt0_modify, HPRT0);
  302. queue_delayed_work(hsotg->wq_otg, &hsotg->reset_work,
  303. msecs_to_jiffies(60));
  304. } else {
  305. /* Port has been enabled, set the reset change flag */
  306. hsotg->flags.b.port_reset_change = 1;
  307. }
  308. }
  309. /*
  310. * There are multiple conditions that can cause a port interrupt. This function
  311. * determines which interrupt conditions have occurred and handles them
  312. * appropriately.
  313. */
  314. static void dwc2_port_intr(struct dwc2_hsotg *hsotg)
  315. {
  316. u32 hprt0;
  317. u32 hprt0_modify;
  318. dev_vdbg(hsotg->dev, "--Port Interrupt--\n");
  319. hprt0 = dwc2_readl(hsotg, HPRT0);
  320. hprt0_modify = hprt0;
  321. /*
  322. * Clear appropriate bits in HPRT0 to clear the interrupt bit in
  323. * GINTSTS
  324. */
  325. hprt0_modify &= ~(HPRT0_ENA | HPRT0_CONNDET | HPRT0_ENACHG |
  326. HPRT0_OVRCURRCHG);
  327. /*
  328. * Port Connect Detected
  329. * Set flag and clear if detected
  330. */
  331. if (hprt0 & HPRT0_CONNDET) {
  332. dwc2_writel(hsotg, hprt0_modify | HPRT0_CONNDET, HPRT0);
  333. dev_vdbg(hsotg->dev,
  334. "--Port Interrupt HPRT0=0x%08x Port Connect Detected--\n",
  335. hprt0);
  336. dwc2_hcd_connect(hsotg);
  337. /*
  338. * The Hub driver asserts a reset when it sees port connect
  339. * status change flag
  340. */
  341. }
  342. /*
  343. * Port Enable Changed
  344. * Clear if detected - Set internal flag if disabled
  345. */
  346. if (hprt0 & HPRT0_ENACHG) {
  347. dwc2_writel(hsotg, hprt0_modify | HPRT0_ENACHG, HPRT0);
  348. dev_vdbg(hsotg->dev,
  349. " --Port Interrupt HPRT0=0x%08x Port Enable Changed (now %d)--\n",
  350. hprt0, !!(hprt0 & HPRT0_ENA));
  351. if (hprt0 & HPRT0_ENA) {
  352. hsotg->new_connection = true;
  353. dwc2_hprt0_enable(hsotg, hprt0, &hprt0_modify);
  354. } else {
  355. hsotg->flags.b.port_enable_change = 1;
  356. if (hsotg->params.dma_desc_fs_enable) {
  357. u32 hcfg;
  358. hsotg->params.dma_desc_enable = false;
  359. hsotg->new_connection = false;
  360. hcfg = dwc2_readl(hsotg, HCFG);
  361. hcfg &= ~HCFG_DESCDMA;
  362. dwc2_writel(hsotg, hcfg, HCFG);
  363. }
  364. }
  365. }
  366. /* Overcurrent Change Interrupt */
  367. if (hprt0 & HPRT0_OVRCURRCHG) {
  368. dwc2_writel(hsotg, hprt0_modify | HPRT0_OVRCURRCHG,
  369. HPRT0);
  370. dev_vdbg(hsotg->dev,
  371. " --Port Interrupt HPRT0=0x%08x Port Overcurrent Changed--\n",
  372. hprt0);
  373. hsotg->flags.b.port_over_current_change = 1;
  374. }
  375. }
  376. /*
  377. * Gets the actual length of a transfer after the transfer halts. halt_status
  378. * holds the reason for the halt.
  379. *
  380. * For IN transfers where halt_status is DWC2_HC_XFER_COMPLETE, *short_read
  381. * is set to 1 upon return if less than the requested number of bytes were
  382. * transferred. short_read may also be NULL on entry, in which case it remains
  383. * unchanged.
  384. */
  385. static u32 dwc2_get_actual_xfer_length(struct dwc2_hsotg *hsotg,
  386. struct dwc2_host_chan *chan, int chnum,
  387. struct dwc2_qtd *qtd,
  388. enum dwc2_halt_status halt_status,
  389. int *short_read)
  390. {
  391. u32 hctsiz, count, length;
  392. hctsiz = dwc2_readl(hsotg, HCTSIZ(chnum));
  393. if (halt_status == DWC2_HC_XFER_COMPLETE) {
  394. if (chan->ep_is_in) {
  395. count = (hctsiz & TSIZ_XFERSIZE_MASK) >>
  396. TSIZ_XFERSIZE_SHIFT;
  397. length = chan->xfer_len - count;
  398. if (short_read)
  399. *short_read = (count != 0);
  400. } else if (chan->qh->do_split) {
  401. length = qtd->ssplit_out_xfer_count;
  402. } else {
  403. length = chan->xfer_len;
  404. }
  405. } else {
  406. /*
  407. * Must use the hctsiz.pktcnt field to determine how much data
  408. * has been transferred. This field reflects the number of
  409. * packets that have been transferred via the USB. This is
  410. * always an integral number of packets if the transfer was
  411. * halted before its normal completion. (Can't use the
  412. * hctsiz.xfersize field because that reflects the number of
  413. * bytes transferred via the AHB, not the USB).
  414. */
  415. count = (hctsiz & TSIZ_PKTCNT_MASK) >> TSIZ_PKTCNT_SHIFT;
  416. length = (chan->start_pkt_count - count) * chan->max_packet;
  417. }
  418. return length;
  419. }
  420. /**
  421. * dwc2_update_urb_state() - Updates the state of the URB after a Transfer
  422. * Complete interrupt on the host channel. Updates the actual_length field
  423. * of the URB based on the number of bytes transferred via the host channel.
  424. * Sets the URB status if the data transfer is finished.
  425. *
  426. * @hsotg: Programming view of the DWC_otg controller
  427. * @chan: Programming view of host channel
  428. * @chnum: Channel number
  429. * @urb: Processing URB
  430. * @qtd: Queue transfer descriptor
  431. *
  432. * Return: 1 if the data transfer specified by the URB is completely finished,
  433. * 0 otherwise
  434. */
  435. static int dwc2_update_urb_state(struct dwc2_hsotg *hsotg,
  436. struct dwc2_host_chan *chan, int chnum,
  437. struct dwc2_hcd_urb *urb,
  438. struct dwc2_qtd *qtd)
  439. {
  440. u32 hctsiz;
  441. int xfer_done = 0;
  442. int short_read = 0;
  443. int xfer_length = dwc2_get_actual_xfer_length(hsotg, chan, chnum, qtd,
  444. DWC2_HC_XFER_COMPLETE,
  445. &short_read);
  446. if (urb->actual_length + xfer_length > urb->length) {
  447. dev_dbg(hsotg->dev, "%s(): trimming xfer length\n", __func__);
  448. xfer_length = urb->length - urb->actual_length;
  449. }
  450. dev_vdbg(hsotg->dev, "urb->actual_length=%d xfer_length=%d\n",
  451. urb->actual_length, xfer_length);
  452. urb->actual_length += xfer_length;
  453. if (xfer_length && chan->ep_type == USB_ENDPOINT_XFER_BULK &&
  454. (urb->flags & URB_SEND_ZERO_PACKET) &&
  455. urb->actual_length >= urb->length &&
  456. !(urb->length % chan->max_packet)) {
  457. xfer_done = 0;
  458. } else if (short_read || urb->actual_length >= urb->length) {
  459. xfer_done = 1;
  460. urb->status = 0;
  461. }
  462. hctsiz = dwc2_readl(hsotg, HCTSIZ(chnum));
  463. dev_vdbg(hsotg->dev, "DWC_otg: %s: %s, channel %d\n",
  464. __func__, (chan->ep_is_in ? "IN" : "OUT"), chnum);
  465. dev_vdbg(hsotg->dev, " chan->xfer_len %d\n", chan->xfer_len);
  466. dev_vdbg(hsotg->dev, " hctsiz.xfersize %d\n",
  467. (hctsiz & TSIZ_XFERSIZE_MASK) >> TSIZ_XFERSIZE_SHIFT);
  468. dev_vdbg(hsotg->dev, " urb->transfer_buffer_length %d\n", urb->length);
  469. dev_vdbg(hsotg->dev, " urb->actual_length %d\n", urb->actual_length);
  470. dev_vdbg(hsotg->dev, " short_read %d, xfer_done %d\n", short_read,
  471. xfer_done);
  472. return xfer_done;
  473. }
  474. /*
  475. * Save the starting data toggle for the next transfer. The data toggle is
  476. * saved in the QH for non-control transfers and it's saved in the QTD for
  477. * control transfers.
  478. */
  479. void dwc2_hcd_save_data_toggle(struct dwc2_hsotg *hsotg,
  480. struct dwc2_host_chan *chan, int chnum,
  481. struct dwc2_qtd *qtd)
  482. {
  483. u32 hctsiz = dwc2_readl(hsotg, HCTSIZ(chnum));
  484. u32 pid = (hctsiz & TSIZ_SC_MC_PID_MASK) >> TSIZ_SC_MC_PID_SHIFT;
  485. if (chan->ep_type != USB_ENDPOINT_XFER_CONTROL) {
  486. if (WARN(!chan || !chan->qh,
  487. "chan->qh must be specified for non-control eps\n"))
  488. return;
  489. if (pid == TSIZ_SC_MC_PID_DATA0)
  490. chan->qh->data_toggle = DWC2_HC_PID_DATA0;
  491. else
  492. chan->qh->data_toggle = DWC2_HC_PID_DATA1;
  493. } else {
  494. if (WARN(!qtd,
  495. "qtd must be specified for control eps\n"))
  496. return;
  497. if (pid == TSIZ_SC_MC_PID_DATA0)
  498. qtd->data_toggle = DWC2_HC_PID_DATA0;
  499. else
  500. qtd->data_toggle = DWC2_HC_PID_DATA1;
  501. }
  502. }
  503. /**
  504. * dwc2_update_isoc_urb_state() - Updates the state of an Isochronous URB when
  505. * the transfer is stopped for any reason. The fields of the current entry in
  506. * the frame descriptor array are set based on the transfer state and the input
  507. * halt_status. Completes the Isochronous URB if all the URB frames have been
  508. * completed.
  509. *
  510. * @hsotg: Programming view of the DWC_otg controller
  511. * @chan: Programming view of host channel
  512. * @chnum: Channel number
  513. * @halt_status: Reason for halting a host channel
  514. * @qtd: Queue transfer descriptor
  515. *
  516. * Return: DWC2_HC_XFER_COMPLETE if there are more frames remaining to be
  517. * transferred in the URB. Otherwise return DWC2_HC_XFER_URB_COMPLETE.
  518. */
  519. static enum dwc2_halt_status dwc2_update_isoc_urb_state(
  520. struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan,
  521. int chnum, struct dwc2_qtd *qtd,
  522. enum dwc2_halt_status halt_status)
  523. {
  524. struct dwc2_hcd_iso_packet_desc *frame_desc;
  525. struct dwc2_hcd_urb *urb = qtd->urb;
  526. if (!urb)
  527. return DWC2_HC_XFER_NO_HALT_STATUS;
  528. frame_desc = &urb->iso_descs[qtd->isoc_frame_index];
  529. switch (halt_status) {
  530. case DWC2_HC_XFER_COMPLETE:
  531. frame_desc->status = 0;
  532. frame_desc->actual_length = dwc2_get_actual_xfer_length(hsotg,
  533. chan, chnum, qtd, halt_status, NULL);
  534. break;
  535. case DWC2_HC_XFER_FRAME_OVERRUN:
  536. urb->error_count++;
  537. if (chan->ep_is_in)
  538. frame_desc->status = -ENOSR;
  539. else
  540. frame_desc->status = -ECOMM;
  541. frame_desc->actual_length = 0;
  542. break;
  543. case DWC2_HC_XFER_BABBLE_ERR:
  544. urb->error_count++;
  545. frame_desc->status = -EOVERFLOW;
  546. /* Don't need to update actual_length in this case */
  547. break;
  548. case DWC2_HC_XFER_XACT_ERR:
  549. urb->error_count++;
  550. frame_desc->status = -EPROTO;
  551. frame_desc->actual_length = dwc2_get_actual_xfer_length(hsotg,
  552. chan, chnum, qtd, halt_status, NULL);
  553. /* Skip whole frame */
  554. if (chan->qh->do_split &&
  555. chan->ep_type == USB_ENDPOINT_XFER_ISOC && chan->ep_is_in &&
  556. hsotg->params.host_dma) {
  557. qtd->complete_split = 0;
  558. qtd->isoc_split_offset = 0;
  559. }
  560. break;
  561. default:
  562. dev_err(hsotg->dev, "Unhandled halt_status (%d)\n",
  563. halt_status);
  564. break;
  565. }
  566. if (++qtd->isoc_frame_index == urb->packet_count) {
  567. /*
  568. * urb->status is not used for isoc transfers. The individual
  569. * frame_desc statuses are used instead.
  570. */
  571. dwc2_host_complete(hsotg, qtd, 0);
  572. halt_status = DWC2_HC_XFER_URB_COMPLETE;
  573. } else {
  574. halt_status = DWC2_HC_XFER_COMPLETE;
  575. }
  576. return halt_status;
  577. }
  578. /*
  579. * Frees the first QTD in the QH's list if free_qtd is 1. For non-periodic
  580. * QHs, removes the QH from the active non-periodic schedule. If any QTDs are
  581. * still linked to the QH, the QH is added to the end of the inactive
  582. * non-periodic schedule. For periodic QHs, removes the QH from the periodic
  583. * schedule if no more QTDs are linked to the QH.
  584. */
  585. static void dwc2_deactivate_qh(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
  586. int free_qtd)
  587. {
  588. int continue_split = 0;
  589. struct dwc2_qtd *qtd;
  590. if (dbg_qh(qh))
  591. dev_vdbg(hsotg->dev, " %s(%p,%p,%d)\n", __func__,
  592. hsotg, qh, free_qtd);
  593. if (list_empty(&qh->qtd_list)) {
  594. dev_dbg(hsotg->dev, "## QTD list empty ##\n");
  595. goto no_qtd;
  596. }
  597. qtd = list_first_entry(&qh->qtd_list, struct dwc2_qtd, qtd_list_entry);
  598. if (qtd->complete_split)
  599. continue_split = 1;
  600. else if (qtd->isoc_split_pos == DWC2_HCSPLT_XACTPOS_MID ||
  601. qtd->isoc_split_pos == DWC2_HCSPLT_XACTPOS_END)
  602. continue_split = 1;
  603. if (free_qtd) {
  604. dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
  605. continue_split = 0;
  606. }
  607. no_qtd:
  608. qh->channel = NULL;
  609. dwc2_hcd_qh_deactivate(hsotg, qh, continue_split);
  610. }
  611. /**
  612. * dwc2_release_channel() - Releases a host channel for use by other transfers
  613. *
  614. * @hsotg: The HCD state structure
  615. * @chan: The host channel to release
  616. * @qtd: The QTD associated with the host channel. This QTD may be
  617. * freed if the transfer is complete or an error has occurred.
  618. * @halt_status: Reason the channel is being released. This status
  619. * determines the actions taken by this function.
  620. *
  621. * Also attempts to select and queue more transactions since at least one host
  622. * channel is available.
  623. */
  624. static void dwc2_release_channel(struct dwc2_hsotg *hsotg,
  625. struct dwc2_host_chan *chan,
  626. struct dwc2_qtd *qtd,
  627. enum dwc2_halt_status halt_status)
  628. {
  629. enum dwc2_transaction_type tr_type;
  630. u32 haintmsk;
  631. int free_qtd = 0;
  632. if (dbg_hc(chan))
  633. dev_vdbg(hsotg->dev, " %s: channel %d, halt_status %d\n",
  634. __func__, chan->hc_num, halt_status);
  635. switch (halt_status) {
  636. case DWC2_HC_XFER_URB_COMPLETE:
  637. free_qtd = 1;
  638. break;
  639. case DWC2_HC_XFER_AHB_ERR:
  640. case DWC2_HC_XFER_STALL:
  641. case DWC2_HC_XFER_BABBLE_ERR:
  642. free_qtd = 1;
  643. break;
  644. case DWC2_HC_XFER_XACT_ERR:
  645. if (qtd && qtd->error_count >= 3) {
  646. dev_vdbg(hsotg->dev,
  647. " Complete URB with transaction error\n");
  648. free_qtd = 1;
  649. dwc2_host_complete(hsotg, qtd, -EPROTO);
  650. }
  651. break;
  652. case DWC2_HC_XFER_URB_DEQUEUE:
  653. /*
  654. * The QTD has already been removed and the QH has been
  655. * deactivated. Don't want to do anything except release the
  656. * host channel and try to queue more transfers.
  657. */
  658. goto cleanup;
  659. case DWC2_HC_XFER_PERIODIC_INCOMPLETE:
  660. dev_vdbg(hsotg->dev, " Complete URB with I/O error\n");
  661. free_qtd = 1;
  662. dwc2_host_complete(hsotg, qtd, -EIO);
  663. break;
  664. case DWC2_HC_XFER_NO_HALT_STATUS:
  665. default:
  666. break;
  667. }
  668. dwc2_deactivate_qh(hsotg, chan->qh, free_qtd);
  669. cleanup:
  670. /*
  671. * Release the host channel for use by other transfers. The cleanup
  672. * function clears the channel interrupt enables and conditions, so
  673. * there's no need to clear the Channel Halted interrupt separately.
  674. */
  675. if (!list_empty(&chan->hc_list_entry))
  676. list_del(&chan->hc_list_entry);
  677. dwc2_hc_cleanup(hsotg, chan);
  678. list_add_tail(&chan->hc_list_entry, &hsotg->free_hc_list);
  679. if (hsotg->params.uframe_sched) {
  680. hsotg->available_host_channels++;
  681. } else {
  682. switch (chan->ep_type) {
  683. case USB_ENDPOINT_XFER_CONTROL:
  684. case USB_ENDPOINT_XFER_BULK:
  685. hsotg->non_periodic_channels--;
  686. break;
  687. default:
  688. /*
  689. * Don't release reservations for periodic channels
  690. * here. That's done when a periodic transfer is
  691. * descheduled (i.e. when the QH is removed from the
  692. * periodic schedule).
  693. */
  694. break;
  695. }
  696. }
  697. haintmsk = dwc2_readl(hsotg, HAINTMSK);
  698. haintmsk &= ~(1 << chan->hc_num);
  699. dwc2_writel(hsotg, haintmsk, HAINTMSK);
  700. /* Try to queue more transfers now that there's a free channel */
  701. tr_type = dwc2_hcd_select_transactions(hsotg);
  702. if (tr_type != DWC2_TRANSACTION_NONE)
  703. dwc2_hcd_queue_transactions(hsotg, tr_type);
  704. }
  705. /*
  706. * Halts a host channel. If the channel cannot be halted immediately because
  707. * the request queue is full, this function ensures that the FIFO empty
  708. * interrupt for the appropriate queue is enabled so that the halt request can
  709. * be queued when there is space in the request queue.
  710. *
  711. * This function may also be called in DMA mode. In that case, the channel is
  712. * simply released since the core always halts the channel automatically in
  713. * DMA mode.
  714. */
  715. static void dwc2_halt_channel(struct dwc2_hsotg *hsotg,
  716. struct dwc2_host_chan *chan, struct dwc2_qtd *qtd,
  717. enum dwc2_halt_status halt_status)
  718. {
  719. if (dbg_hc(chan))
  720. dev_vdbg(hsotg->dev, "%s()\n", __func__);
  721. if (hsotg->params.host_dma) {
  722. if (dbg_hc(chan))
  723. dev_vdbg(hsotg->dev, "DMA enabled\n");
  724. dwc2_release_channel(hsotg, chan, qtd, halt_status);
  725. return;
  726. }
  727. /* Slave mode processing */
  728. dwc2_hc_halt(hsotg, chan, halt_status);
  729. if (chan->halt_on_queue) {
  730. u32 gintmsk;
  731. dev_vdbg(hsotg->dev, "Halt on queue\n");
  732. if (chan->ep_type == USB_ENDPOINT_XFER_CONTROL ||
  733. chan->ep_type == USB_ENDPOINT_XFER_BULK) {
  734. dev_vdbg(hsotg->dev, "control/bulk\n");
  735. /*
  736. * Make sure the Non-periodic Tx FIFO empty interrupt
  737. * is enabled so that the non-periodic schedule will
  738. * be processed
  739. */
  740. gintmsk = dwc2_readl(hsotg, GINTMSK);
  741. gintmsk |= GINTSTS_NPTXFEMP;
  742. dwc2_writel(hsotg, gintmsk, GINTMSK);
  743. } else {
  744. dev_vdbg(hsotg->dev, "isoc/intr\n");
  745. /*
  746. * Move the QH from the periodic queued schedule to
  747. * the periodic assigned schedule. This allows the
  748. * halt to be queued when the periodic schedule is
  749. * processed.
  750. */
  751. list_move_tail(&chan->qh->qh_list_entry,
  752. &hsotg->periodic_sched_assigned);
  753. /*
  754. * Make sure the Periodic Tx FIFO Empty interrupt is
  755. * enabled so that the periodic schedule will be
  756. * processed
  757. */
  758. gintmsk = dwc2_readl(hsotg, GINTMSK);
  759. gintmsk |= GINTSTS_PTXFEMP;
  760. dwc2_writel(hsotg, gintmsk, GINTMSK);
  761. }
  762. }
  763. }
  764. /*
  765. * Performs common cleanup for non-periodic transfers after a Transfer
  766. * Complete interrupt. This function should be called after any endpoint type
  767. * specific handling is finished to release the host channel.
  768. */
  769. static void dwc2_complete_non_periodic_xfer(struct dwc2_hsotg *hsotg,
  770. struct dwc2_host_chan *chan,
  771. int chnum, struct dwc2_qtd *qtd,
  772. enum dwc2_halt_status halt_status)
  773. {
  774. dev_vdbg(hsotg->dev, "%s()\n", __func__);
  775. qtd->error_count = 0;
  776. if (chan->hcint & HCINTMSK_NYET) {
  777. /*
  778. * Got a NYET on the last transaction of the transfer. This
  779. * means that the endpoint should be in the PING state at the
  780. * beginning of the next transfer.
  781. */
  782. dev_vdbg(hsotg->dev, "got NYET\n");
  783. chan->qh->ping_state = 1;
  784. }
  785. /*
  786. * Always halt and release the host channel to make it available for
  787. * more transfers. There may still be more phases for a control
  788. * transfer or more data packets for a bulk transfer at this point,
  789. * but the host channel is still halted. A channel will be reassigned
  790. * to the transfer when the non-periodic schedule is processed after
  791. * the channel is released. This allows transactions to be queued
  792. * properly via dwc2_hcd_queue_transactions, which also enables the
  793. * Tx FIFO Empty interrupt if necessary.
  794. */
  795. if (chan->ep_is_in) {
  796. /*
  797. * IN transfers in Slave mode require an explicit disable to
  798. * halt the channel. (In DMA mode, this call simply releases
  799. * the channel.)
  800. */
  801. dwc2_halt_channel(hsotg, chan, qtd, halt_status);
  802. } else {
  803. /*
  804. * The channel is automatically disabled by the core for OUT
  805. * transfers in Slave mode
  806. */
  807. dwc2_release_channel(hsotg, chan, qtd, halt_status);
  808. }
  809. }
  810. /*
  811. * Performs common cleanup for periodic transfers after a Transfer Complete
  812. * interrupt. This function should be called after any endpoint type specific
  813. * handling is finished to release the host channel.
  814. */
  815. static void dwc2_complete_periodic_xfer(struct dwc2_hsotg *hsotg,
  816. struct dwc2_host_chan *chan, int chnum,
  817. struct dwc2_qtd *qtd,
  818. enum dwc2_halt_status halt_status)
  819. {
  820. u32 hctsiz = dwc2_readl(hsotg, HCTSIZ(chnum));
  821. qtd->error_count = 0;
  822. if (!chan->ep_is_in || (hctsiz & TSIZ_PKTCNT_MASK) == 0)
  823. /* Core halts channel in these cases */
  824. dwc2_release_channel(hsotg, chan, qtd, halt_status);
  825. else
  826. /* Flush any outstanding requests from the Tx queue */
  827. dwc2_halt_channel(hsotg, chan, qtd, halt_status);
  828. }
  829. static int dwc2_xfercomp_isoc_split_in(struct dwc2_hsotg *hsotg,
  830. struct dwc2_host_chan *chan, int chnum,
  831. struct dwc2_qtd *qtd)
  832. {
  833. struct dwc2_hcd_iso_packet_desc *frame_desc;
  834. u32 len;
  835. u32 hctsiz;
  836. u32 pid;
  837. if (!qtd->urb)
  838. return 0;
  839. frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
  840. len = dwc2_get_actual_xfer_length(hsotg, chan, chnum, qtd,
  841. DWC2_HC_XFER_COMPLETE, NULL);
  842. if (!len && !qtd->isoc_split_offset) {
  843. qtd->complete_split = 0;
  844. return 0;
  845. }
  846. frame_desc->actual_length += len;
  847. if (chan->align_buf) {
  848. dev_vdbg(hsotg->dev, "non-aligned buffer\n");
  849. dma_unmap_single(hsotg->dev, chan->qh->dw_align_buf_dma,
  850. DWC2_KMEM_UNALIGNED_BUF_SIZE, DMA_FROM_DEVICE);
  851. memcpy(qtd->urb->buf + (chan->xfer_dma - qtd->urb->dma),
  852. chan->qh->dw_align_buf, len);
  853. }
  854. qtd->isoc_split_offset += len;
  855. hctsiz = dwc2_readl(hsotg, HCTSIZ(chnum));
  856. pid = (hctsiz & TSIZ_SC_MC_PID_MASK) >> TSIZ_SC_MC_PID_SHIFT;
  857. if (frame_desc->actual_length >= frame_desc->length || pid == 0) {
  858. frame_desc->status = 0;
  859. qtd->isoc_frame_index++;
  860. qtd->complete_split = 0;
  861. qtd->isoc_split_offset = 0;
  862. }
  863. if (qtd->isoc_frame_index == qtd->urb->packet_count) {
  864. dwc2_host_complete(hsotg, qtd, 0);
  865. dwc2_release_channel(hsotg, chan, qtd,
  866. DWC2_HC_XFER_URB_COMPLETE);
  867. } else {
  868. dwc2_release_channel(hsotg, chan, qtd,
  869. DWC2_HC_XFER_NO_HALT_STATUS);
  870. }
  871. return 1; /* Indicates that channel released */
  872. }
  873. /*
  874. * Handles a host channel Transfer Complete interrupt. This handler may be
  875. * called in either DMA mode or Slave mode.
  876. */
  877. static void dwc2_hc_xfercomp_intr(struct dwc2_hsotg *hsotg,
  878. struct dwc2_host_chan *chan, int chnum,
  879. struct dwc2_qtd *qtd)
  880. {
  881. struct dwc2_hcd_urb *urb = qtd->urb;
  882. enum dwc2_halt_status halt_status = DWC2_HC_XFER_COMPLETE;
  883. int pipe_type;
  884. int urb_xfer_done;
  885. if (dbg_hc(chan))
  886. dev_vdbg(hsotg->dev,
  887. "--Host Channel %d Interrupt: Transfer Complete--\n",
  888. chnum);
  889. if (!urb)
  890. goto handle_xfercomp_done;
  891. pipe_type = dwc2_hcd_get_pipe_type(&urb->pipe_info);
  892. if (hsotg->params.dma_desc_enable) {
  893. dwc2_hcd_complete_xfer_ddma(hsotg, chan, chnum, halt_status);
  894. if (pipe_type == USB_ENDPOINT_XFER_ISOC)
  895. /* Do not disable the interrupt, just clear it */
  896. return;
  897. goto handle_xfercomp_done;
  898. }
  899. /* Handle xfer complete on CSPLIT */
  900. if (chan->qh->do_split) {
  901. if (chan->ep_type == USB_ENDPOINT_XFER_ISOC && chan->ep_is_in &&
  902. hsotg->params.host_dma) {
  903. if (qtd->complete_split &&
  904. dwc2_xfercomp_isoc_split_in(hsotg, chan, chnum,
  905. qtd))
  906. goto handle_xfercomp_done;
  907. } else {
  908. qtd->complete_split = 0;
  909. }
  910. }
  911. /* Update the QTD and URB states */
  912. switch (pipe_type) {
  913. case USB_ENDPOINT_XFER_CONTROL:
  914. switch (qtd->control_phase) {
  915. case DWC2_CONTROL_SETUP:
  916. if (urb->length > 0)
  917. qtd->control_phase = DWC2_CONTROL_DATA;
  918. else
  919. qtd->control_phase = DWC2_CONTROL_STATUS;
  920. dev_vdbg(hsotg->dev,
  921. " Control setup transaction done\n");
  922. halt_status = DWC2_HC_XFER_COMPLETE;
  923. break;
  924. case DWC2_CONTROL_DATA:
  925. urb_xfer_done = dwc2_update_urb_state(hsotg, chan,
  926. chnum, urb, qtd);
  927. if (urb_xfer_done) {
  928. qtd->control_phase = DWC2_CONTROL_STATUS;
  929. dev_vdbg(hsotg->dev,
  930. " Control data transfer done\n");
  931. } else {
  932. dwc2_hcd_save_data_toggle(hsotg, chan, chnum,
  933. qtd);
  934. }
  935. halt_status = DWC2_HC_XFER_COMPLETE;
  936. break;
  937. case DWC2_CONTROL_STATUS:
  938. dev_vdbg(hsotg->dev, " Control transfer complete\n");
  939. if (urb->status == -EINPROGRESS)
  940. urb->status = 0;
  941. dwc2_host_complete(hsotg, qtd, urb->status);
  942. halt_status = DWC2_HC_XFER_URB_COMPLETE;
  943. break;
  944. }
  945. dwc2_complete_non_periodic_xfer(hsotg, chan, chnum, qtd,
  946. halt_status);
  947. break;
  948. case USB_ENDPOINT_XFER_BULK:
  949. dev_vdbg(hsotg->dev, " Bulk transfer complete\n");
  950. urb_xfer_done = dwc2_update_urb_state(hsotg, chan, chnum, urb,
  951. qtd);
  952. if (urb_xfer_done) {
  953. dwc2_host_complete(hsotg, qtd, urb->status);
  954. halt_status = DWC2_HC_XFER_URB_COMPLETE;
  955. } else {
  956. halt_status = DWC2_HC_XFER_COMPLETE;
  957. }
  958. dwc2_hcd_save_data_toggle(hsotg, chan, chnum, qtd);
  959. dwc2_complete_non_periodic_xfer(hsotg, chan, chnum, qtd,
  960. halt_status);
  961. break;
  962. case USB_ENDPOINT_XFER_INT:
  963. dev_vdbg(hsotg->dev, " Interrupt transfer complete\n");
  964. urb_xfer_done = dwc2_update_urb_state(hsotg, chan, chnum, urb,
  965. qtd);
  966. /*
  967. * Interrupt URB is done on the first transfer complete
  968. * interrupt
  969. */
  970. if (urb_xfer_done) {
  971. dwc2_host_complete(hsotg, qtd, urb->status);
  972. halt_status = DWC2_HC_XFER_URB_COMPLETE;
  973. } else {
  974. halt_status = DWC2_HC_XFER_COMPLETE;
  975. }
  976. dwc2_hcd_save_data_toggle(hsotg, chan, chnum, qtd);
  977. dwc2_complete_periodic_xfer(hsotg, chan, chnum, qtd,
  978. halt_status);
  979. break;
  980. case USB_ENDPOINT_XFER_ISOC:
  981. if (dbg_perio())
  982. dev_vdbg(hsotg->dev, " Isochronous transfer complete\n");
  983. if (qtd->isoc_split_pos == DWC2_HCSPLT_XACTPOS_ALL)
  984. halt_status = dwc2_update_isoc_urb_state(hsotg, chan,
  985. chnum, qtd,
  986. DWC2_HC_XFER_COMPLETE);
  987. dwc2_complete_periodic_xfer(hsotg, chan, chnum, qtd,
  988. halt_status);
  989. break;
  990. }
  991. handle_xfercomp_done:
  992. disable_hc_int(hsotg, chnum, HCINTMSK_XFERCOMPL);
  993. }
  994. /*
  995. * Handles a host channel STALL interrupt. This handler may be called in
  996. * either DMA mode or Slave mode.
  997. */
  998. static void dwc2_hc_stall_intr(struct dwc2_hsotg *hsotg,
  999. struct dwc2_host_chan *chan, int chnum,
  1000. struct dwc2_qtd *qtd)
  1001. {
  1002. struct dwc2_hcd_urb *urb = qtd->urb;
  1003. int pipe_type;
  1004. dev_dbg(hsotg->dev, "--Host Channel %d Interrupt: STALL Received--\n",
  1005. chnum);
  1006. if (hsotg->params.dma_desc_enable) {
  1007. dwc2_hcd_complete_xfer_ddma(hsotg, chan, chnum,
  1008. DWC2_HC_XFER_STALL);
  1009. goto handle_stall_done;
  1010. }
  1011. if (!urb)
  1012. goto handle_stall_halt;
  1013. pipe_type = dwc2_hcd_get_pipe_type(&urb->pipe_info);
  1014. if (pipe_type == USB_ENDPOINT_XFER_CONTROL)
  1015. dwc2_host_complete(hsotg, qtd, -EPIPE);
  1016. if (pipe_type == USB_ENDPOINT_XFER_BULK ||
  1017. pipe_type == USB_ENDPOINT_XFER_INT) {
  1018. dwc2_host_complete(hsotg, qtd, -EPIPE);
  1019. /*
  1020. * USB protocol requires resetting the data toggle for bulk
  1021. * and interrupt endpoints when a CLEAR_FEATURE(ENDPOINT_HALT)
  1022. * setup command is issued to the endpoint. Anticipate the
  1023. * CLEAR_FEATURE command since a STALL has occurred and reset
  1024. * the data toggle now.
  1025. */
  1026. chan->qh->data_toggle = 0;
  1027. }
  1028. handle_stall_halt:
  1029. dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_STALL);
  1030. handle_stall_done:
  1031. disable_hc_int(hsotg, chnum, HCINTMSK_STALL);
  1032. }
  1033. /*
  1034. * Updates the state of the URB when a transfer has been stopped due to an
  1035. * abnormal condition before the transfer completes. Modifies the
  1036. * actual_length field of the URB to reflect the number of bytes that have
  1037. * actually been transferred via the host channel.
  1038. */
  1039. static void dwc2_update_urb_state_abn(struct dwc2_hsotg *hsotg,
  1040. struct dwc2_host_chan *chan, int chnum,
  1041. struct dwc2_hcd_urb *urb,
  1042. struct dwc2_qtd *qtd,
  1043. enum dwc2_halt_status halt_status)
  1044. {
  1045. u32 xfer_length = dwc2_get_actual_xfer_length(hsotg, chan, chnum,
  1046. qtd, halt_status, NULL);
  1047. u32 hctsiz;
  1048. if (urb->actual_length + xfer_length > urb->length) {
  1049. dev_warn(hsotg->dev, "%s(): trimming xfer length\n", __func__);
  1050. xfer_length = urb->length - urb->actual_length;
  1051. }
  1052. urb->actual_length += xfer_length;
  1053. hctsiz = dwc2_readl(hsotg, HCTSIZ(chnum));
  1054. dev_vdbg(hsotg->dev, "DWC_otg: %s: %s, channel %d\n",
  1055. __func__, (chan->ep_is_in ? "IN" : "OUT"), chnum);
  1056. dev_vdbg(hsotg->dev, " chan->start_pkt_count %d\n",
  1057. chan->start_pkt_count);
  1058. dev_vdbg(hsotg->dev, " hctsiz.pktcnt %d\n",
  1059. (hctsiz & TSIZ_PKTCNT_MASK) >> TSIZ_PKTCNT_SHIFT);
  1060. dev_vdbg(hsotg->dev, " chan->max_packet %d\n", chan->max_packet);
  1061. dev_vdbg(hsotg->dev, " bytes_transferred %d\n",
  1062. xfer_length);
  1063. dev_vdbg(hsotg->dev, " urb->actual_length %d\n",
  1064. urb->actual_length);
  1065. dev_vdbg(hsotg->dev, " urb->transfer_buffer_length %d\n",
  1066. urb->length);
  1067. }
  1068. /*
  1069. * Handles a host channel NAK interrupt. This handler may be called in either
  1070. * DMA mode or Slave mode.
  1071. */
  1072. static void dwc2_hc_nak_intr(struct dwc2_hsotg *hsotg,
  1073. struct dwc2_host_chan *chan, int chnum,
  1074. struct dwc2_qtd *qtd)
  1075. {
  1076. if (!qtd) {
  1077. dev_dbg(hsotg->dev, "%s: qtd is NULL\n", __func__);
  1078. return;
  1079. }
  1080. if (!qtd->urb) {
  1081. dev_dbg(hsotg->dev, "%s: qtd->urb is NULL\n", __func__);
  1082. return;
  1083. }
  1084. if (dbg_hc(chan))
  1085. dev_vdbg(hsotg->dev, "--Host Channel %d Interrupt: NAK Received--\n",
  1086. chnum);
  1087. /*
  1088. * Handle NAK for IN/OUT SSPLIT/CSPLIT transfers, bulk, control, and
  1089. * interrupt. Re-start the SSPLIT transfer.
  1090. *
  1091. * Normally for non-periodic transfers we'll retry right away, but to
  1092. * avoid interrupt storms we'll wait before retrying if we've got
  1093. * several NAKs. If we didn't do this we'd retry directly from the
  1094. * interrupt handler and could end up quickly getting another
  1095. * interrupt (another NAK), which we'd retry. Note that we do not
  1096. * delay retries for IN parts of control requests, as those are expected
  1097. * to complete fairly quickly, and if we delay them we risk confusing
  1098. * the device and cause it issue STALL.
  1099. *
  1100. * Note that in DMA mode software only gets involved to re-send NAKed
  1101. * transfers for split transactions, so we only need to apply this
  1102. * delaying logic when handling splits. In non-DMA mode presumably we
  1103. * might want a similar delay if someone can demonstrate this problem
  1104. * affects that code path too.
  1105. */
  1106. if (chan->do_split) {
  1107. if (chan->complete_split)
  1108. qtd->error_count = 0;
  1109. qtd->complete_split = 0;
  1110. qtd->num_naks++;
  1111. qtd->qh->want_wait = qtd->num_naks >= DWC2_NAKS_BEFORE_DELAY &&
  1112. !(chan->ep_type == USB_ENDPOINT_XFER_CONTROL &&
  1113. chan->ep_is_in);
  1114. dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_NAK);
  1115. goto handle_nak_done;
  1116. }
  1117. switch (dwc2_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
  1118. case USB_ENDPOINT_XFER_CONTROL:
  1119. case USB_ENDPOINT_XFER_BULK:
  1120. if (hsotg->params.host_dma && chan->ep_is_in) {
  1121. /*
  1122. * NAK interrupts are enabled on bulk/control IN
  1123. * transfers in DMA mode for the sole purpose of
  1124. * resetting the error count after a transaction error
  1125. * occurs. The core will continue transferring data.
  1126. */
  1127. qtd->error_count = 0;
  1128. break;
  1129. }
  1130. /*
  1131. * NAK interrupts normally occur during OUT transfers in DMA
  1132. * or Slave mode. For IN transfers, more requests will be
  1133. * queued as request queue space is available.
  1134. */
  1135. qtd->error_count = 0;
  1136. if (!chan->qh->ping_state) {
  1137. dwc2_update_urb_state_abn(hsotg, chan, chnum, qtd->urb,
  1138. qtd, DWC2_HC_XFER_NAK);
  1139. dwc2_hcd_save_data_toggle(hsotg, chan, chnum, qtd);
  1140. if (chan->speed == USB_SPEED_HIGH)
  1141. chan->qh->ping_state = 1;
  1142. }
  1143. /*
  1144. * Halt the channel so the transfer can be re-started from
  1145. * the appropriate point or the PING protocol will
  1146. * start/continue
  1147. */
  1148. dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_NAK);
  1149. break;
  1150. case USB_ENDPOINT_XFER_INT:
  1151. qtd->error_count = 0;
  1152. dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_NAK);
  1153. break;
  1154. case USB_ENDPOINT_XFER_ISOC:
  1155. /* Should never get called for isochronous transfers */
  1156. dev_err(hsotg->dev, "NACK interrupt for ISOC transfer\n");
  1157. break;
  1158. }
  1159. handle_nak_done:
  1160. disable_hc_int(hsotg, chnum, HCINTMSK_NAK);
  1161. }
  1162. /*
  1163. * Handles a host channel ACK interrupt. This interrupt is enabled when
  1164. * performing the PING protocol in Slave mode, when errors occur during
  1165. * either Slave mode or DMA mode, and during Start Split transactions.
  1166. */
  1167. static void dwc2_hc_ack_intr(struct dwc2_hsotg *hsotg,
  1168. struct dwc2_host_chan *chan, int chnum,
  1169. struct dwc2_qtd *qtd)
  1170. {
  1171. struct dwc2_hcd_iso_packet_desc *frame_desc;
  1172. if (dbg_hc(chan))
  1173. dev_vdbg(hsotg->dev, "--Host Channel %d Interrupt: ACK Received--\n",
  1174. chnum);
  1175. if (chan->do_split) {
  1176. /* Handle ACK on SSPLIT. ACK should not occur in CSPLIT. */
  1177. if (!chan->ep_is_in &&
  1178. chan->data_pid_start != DWC2_HC_PID_SETUP)
  1179. qtd->ssplit_out_xfer_count = chan->xfer_len;
  1180. if (chan->ep_type != USB_ENDPOINT_XFER_ISOC || chan->ep_is_in) {
  1181. qtd->complete_split = 1;
  1182. dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_ACK);
  1183. } else {
  1184. /* ISOC OUT */
  1185. switch (chan->xact_pos) {
  1186. case DWC2_HCSPLT_XACTPOS_ALL:
  1187. break;
  1188. case DWC2_HCSPLT_XACTPOS_END:
  1189. qtd->isoc_split_pos = DWC2_HCSPLT_XACTPOS_ALL;
  1190. qtd->isoc_split_offset = 0;
  1191. break;
  1192. case DWC2_HCSPLT_XACTPOS_BEGIN:
  1193. case DWC2_HCSPLT_XACTPOS_MID:
  1194. /*
  1195. * For BEGIN or MID, calculate the length for
  1196. * the next microframe to determine the correct
  1197. * SSPLIT token, either MID or END
  1198. */
  1199. frame_desc = &qtd->urb->iso_descs[
  1200. qtd->isoc_frame_index];
  1201. qtd->isoc_split_offset += 188;
  1202. if (frame_desc->length - qtd->isoc_split_offset
  1203. <= 188)
  1204. qtd->isoc_split_pos =
  1205. DWC2_HCSPLT_XACTPOS_END;
  1206. else
  1207. qtd->isoc_split_pos =
  1208. DWC2_HCSPLT_XACTPOS_MID;
  1209. break;
  1210. }
  1211. }
  1212. } else {
  1213. qtd->error_count = 0;
  1214. if (chan->qh->ping_state) {
  1215. chan->qh->ping_state = 0;
  1216. /*
  1217. * Halt the channel so the transfer can be re-started
  1218. * from the appropriate point. This only happens in
  1219. * Slave mode. In DMA mode, the ping_state is cleared
  1220. * when the transfer is started because the core
  1221. * automatically executes the PING, then the transfer.
  1222. */
  1223. dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_ACK);
  1224. }
  1225. }
  1226. /*
  1227. * If the ACK occurred when _not_ in the PING state, let the channel
  1228. * continue transferring data after clearing the error count
  1229. */
  1230. disable_hc_int(hsotg, chnum, HCINTMSK_ACK);
  1231. }
  1232. /*
  1233. * Handles a host channel NYET interrupt. This interrupt should only occur on
  1234. * Bulk and Control OUT endpoints and for complete split transactions. If a
  1235. * NYET occurs at the same time as a Transfer Complete interrupt, it is
  1236. * handled in the xfercomp interrupt handler, not here. This handler may be
  1237. * called in either DMA mode or Slave mode.
  1238. */
  1239. static void dwc2_hc_nyet_intr(struct dwc2_hsotg *hsotg,
  1240. struct dwc2_host_chan *chan, int chnum,
  1241. struct dwc2_qtd *qtd)
  1242. {
  1243. if (dbg_hc(chan))
  1244. dev_vdbg(hsotg->dev, "--Host Channel %d Interrupt: NYET Received--\n",
  1245. chnum);
  1246. /*
  1247. * NYET on CSPLIT
  1248. * re-do the CSPLIT immediately on non-periodic
  1249. */
  1250. if (chan->do_split && chan->complete_split) {
  1251. if (chan->ep_is_in && chan->ep_type == USB_ENDPOINT_XFER_ISOC &&
  1252. hsotg->params.host_dma) {
  1253. qtd->complete_split = 0;
  1254. qtd->isoc_split_offset = 0;
  1255. qtd->isoc_frame_index++;
  1256. if (qtd->urb &&
  1257. qtd->isoc_frame_index == qtd->urb->packet_count) {
  1258. dwc2_host_complete(hsotg, qtd, 0);
  1259. dwc2_release_channel(hsotg, chan, qtd,
  1260. DWC2_HC_XFER_URB_COMPLETE);
  1261. } else {
  1262. dwc2_release_channel(hsotg, chan, qtd,
  1263. DWC2_HC_XFER_NO_HALT_STATUS);
  1264. }
  1265. goto handle_nyet_done;
  1266. }
  1267. if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
  1268. chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
  1269. struct dwc2_qh *qh = chan->qh;
  1270. bool past_end;
  1271. if (!hsotg->params.uframe_sched) {
  1272. int frnum = dwc2_hcd_get_frame_number(hsotg);
  1273. /* Don't have num_hs_transfers; simple logic */
  1274. past_end = dwc2_full_frame_num(frnum) !=
  1275. dwc2_full_frame_num(qh->next_active_frame);
  1276. } else {
  1277. int end_frnum;
  1278. /*
  1279. * Figure out the end frame based on
  1280. * schedule.
  1281. *
  1282. * We don't want to go on trying again
  1283. * and again forever. Let's stop when
  1284. * we've done all the transfers that
  1285. * were scheduled.
  1286. *
  1287. * We're going to be comparing
  1288. * start_active_frame and
  1289. * next_active_frame, both of which
  1290. * are 1 before the time the packet
  1291. * goes on the wire, so that cancels
  1292. * out. Basically if had 1 transfer
  1293. * and we saw 1 NYET then we're done.
  1294. * We're getting a NYET here so if
  1295. * next >= (start + num_transfers)
  1296. * we're done. The complexity is that
  1297. * for all but ISOC_OUT we skip one
  1298. * slot.
  1299. */
  1300. end_frnum = dwc2_frame_num_inc(
  1301. qh->start_active_frame,
  1302. qh->num_hs_transfers);
  1303. if (qh->ep_type != USB_ENDPOINT_XFER_ISOC ||
  1304. qh->ep_is_in)
  1305. end_frnum =
  1306. dwc2_frame_num_inc(end_frnum, 1);
  1307. past_end = dwc2_frame_num_le(
  1308. end_frnum, qh->next_active_frame);
  1309. }
  1310. if (past_end) {
  1311. /* Treat this as a transaction error. */
  1312. #if 0
  1313. /*
  1314. * Todo: Fix system performance so this can
  1315. * be treated as an error. Right now complete
  1316. * splits cannot be scheduled precisely enough
  1317. * due to other system activity, so this error
  1318. * occurs regularly in Slave mode.
  1319. */
  1320. qtd->error_count++;
  1321. #endif
  1322. qtd->complete_split = 0;
  1323. dwc2_halt_channel(hsotg, chan, qtd,
  1324. DWC2_HC_XFER_XACT_ERR);
  1325. /* Todo: add support for isoc release */
  1326. goto handle_nyet_done;
  1327. }
  1328. }
  1329. dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_NYET);
  1330. goto handle_nyet_done;
  1331. }
  1332. chan->qh->ping_state = 1;
  1333. qtd->error_count = 0;
  1334. dwc2_update_urb_state_abn(hsotg, chan, chnum, qtd->urb, qtd,
  1335. DWC2_HC_XFER_NYET);
  1336. dwc2_hcd_save_data_toggle(hsotg, chan, chnum, qtd);
  1337. /*
  1338. * Halt the channel and re-start the transfer so the PING protocol
  1339. * will start
  1340. */
  1341. dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_NYET);
  1342. handle_nyet_done:
  1343. disable_hc_int(hsotg, chnum, HCINTMSK_NYET);
  1344. }
  1345. /*
  1346. * Handles a host channel babble interrupt. This handler may be called in
  1347. * either DMA mode or Slave mode.
  1348. */
  1349. static void dwc2_hc_babble_intr(struct dwc2_hsotg *hsotg,
  1350. struct dwc2_host_chan *chan, int chnum,
  1351. struct dwc2_qtd *qtd)
  1352. {
  1353. dev_dbg(hsotg->dev, "--Host Channel %d Interrupt: Babble Error--\n",
  1354. chnum);
  1355. dwc2_hc_handle_tt_clear(hsotg, chan, qtd);
  1356. if (hsotg->params.dma_desc_enable) {
  1357. dwc2_hcd_complete_xfer_ddma(hsotg, chan, chnum,
  1358. DWC2_HC_XFER_BABBLE_ERR);
  1359. goto disable_int;
  1360. }
  1361. if (chan->ep_type != USB_ENDPOINT_XFER_ISOC) {
  1362. dwc2_host_complete(hsotg, qtd, -EOVERFLOW);
  1363. dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_BABBLE_ERR);
  1364. } else {
  1365. enum dwc2_halt_status halt_status;
  1366. halt_status = dwc2_update_isoc_urb_state(hsotg, chan, chnum,
  1367. qtd, DWC2_HC_XFER_BABBLE_ERR);
  1368. dwc2_halt_channel(hsotg, chan, qtd, halt_status);
  1369. }
  1370. disable_int:
  1371. disable_hc_int(hsotg, chnum, HCINTMSK_BBLERR);
  1372. }
  1373. /*
  1374. * Handles a host channel AHB error interrupt. This handler is only called in
  1375. * DMA mode.
  1376. */
  1377. static void dwc2_hc_ahberr_intr(struct dwc2_hsotg *hsotg,
  1378. struct dwc2_host_chan *chan, int chnum,
  1379. struct dwc2_qtd *qtd)
  1380. {
  1381. struct dwc2_hcd_urb *urb = qtd->urb;
  1382. char *pipetype, *speed;
  1383. u32 hcchar;
  1384. u32 hcsplt;
  1385. u32 hctsiz;
  1386. u32 hc_dma;
  1387. dev_dbg(hsotg->dev, "--Host Channel %d Interrupt: AHB Error--\n",
  1388. chnum);
  1389. if (!urb)
  1390. goto handle_ahberr_halt;
  1391. dwc2_hc_handle_tt_clear(hsotg, chan, qtd);
  1392. hcchar = dwc2_readl(hsotg, HCCHAR(chnum));
  1393. hcsplt = dwc2_readl(hsotg, HCSPLT(chnum));
  1394. hctsiz = dwc2_readl(hsotg, HCTSIZ(chnum));
  1395. hc_dma = dwc2_readl(hsotg, HCDMA(chnum));
  1396. dev_err(hsotg->dev, "AHB ERROR, Channel %d\n", chnum);
  1397. dev_err(hsotg->dev, " hcchar 0x%08x, hcsplt 0x%08x\n", hcchar, hcsplt);
  1398. dev_err(hsotg->dev, " hctsiz 0x%08x, hc_dma 0x%08x\n", hctsiz, hc_dma);
  1399. dev_err(hsotg->dev, " Device address: %d\n",
  1400. dwc2_hcd_get_dev_addr(&urb->pipe_info));
  1401. dev_err(hsotg->dev, " Endpoint: %d, %s\n",
  1402. dwc2_hcd_get_ep_num(&urb->pipe_info),
  1403. dwc2_hcd_is_pipe_in(&urb->pipe_info) ? "IN" : "OUT");
  1404. switch (dwc2_hcd_get_pipe_type(&urb->pipe_info)) {
  1405. case USB_ENDPOINT_XFER_CONTROL:
  1406. pipetype = "CONTROL";
  1407. break;
  1408. case USB_ENDPOINT_XFER_BULK:
  1409. pipetype = "BULK";
  1410. break;
  1411. case USB_ENDPOINT_XFER_INT:
  1412. pipetype = "INTERRUPT";
  1413. break;
  1414. case USB_ENDPOINT_XFER_ISOC:
  1415. pipetype = "ISOCHRONOUS";
  1416. break;
  1417. default:
  1418. pipetype = "UNKNOWN";
  1419. break;
  1420. }
  1421. dev_err(hsotg->dev, " Endpoint type: %s\n", pipetype);
  1422. switch (chan->speed) {
  1423. case USB_SPEED_HIGH:
  1424. speed = "HIGH";
  1425. break;
  1426. case USB_SPEED_FULL:
  1427. speed = "FULL";
  1428. break;
  1429. case USB_SPEED_LOW:
  1430. speed = "LOW";
  1431. break;
  1432. default:
  1433. speed = "UNKNOWN";
  1434. break;
  1435. }
  1436. dev_err(hsotg->dev, " Speed: %s\n", speed);
  1437. dev_err(hsotg->dev, " Max packet size: %d (mult %d)\n",
  1438. dwc2_hcd_get_maxp(&urb->pipe_info),
  1439. dwc2_hcd_get_maxp_mult(&urb->pipe_info));
  1440. dev_err(hsotg->dev, " Data buffer length: %d\n", urb->length);
  1441. dev_err(hsotg->dev, " Transfer buffer: %p, Transfer DMA: %08lx\n",
  1442. urb->buf, (unsigned long)urb->dma);
  1443. dev_err(hsotg->dev, " Setup buffer: %p, Setup DMA: %08lx\n",
  1444. urb->setup_packet, (unsigned long)urb->setup_dma);
  1445. dev_err(hsotg->dev, " Interval: %d\n", urb->interval);
  1446. /* Core halts the channel for Descriptor DMA mode */
  1447. if (hsotg->params.dma_desc_enable) {
  1448. dwc2_hcd_complete_xfer_ddma(hsotg, chan, chnum,
  1449. DWC2_HC_XFER_AHB_ERR);
  1450. goto handle_ahberr_done;
  1451. }
  1452. dwc2_host_complete(hsotg, qtd, -EIO);
  1453. handle_ahberr_halt:
  1454. /*
  1455. * Force a channel halt. Don't call dwc2_halt_channel because that won't
  1456. * write to the HCCHARn register in DMA mode to force the halt.
  1457. */
  1458. dwc2_hc_halt(hsotg, chan, DWC2_HC_XFER_AHB_ERR);
  1459. handle_ahberr_done:
  1460. disable_hc_int(hsotg, chnum, HCINTMSK_AHBERR);
  1461. }
  1462. /*
  1463. * Handles a host channel transaction error interrupt. This handler may be
  1464. * called in either DMA mode or Slave mode.
  1465. */
  1466. static void dwc2_hc_xacterr_intr(struct dwc2_hsotg *hsotg,
  1467. struct dwc2_host_chan *chan, int chnum,
  1468. struct dwc2_qtd *qtd)
  1469. {
  1470. dev_dbg(hsotg->dev,
  1471. "--Host Channel %d Interrupt: Transaction Error--\n", chnum);
  1472. dwc2_hc_handle_tt_clear(hsotg, chan, qtd);
  1473. if (hsotg->params.dma_desc_enable) {
  1474. dwc2_hcd_complete_xfer_ddma(hsotg, chan, chnum,
  1475. DWC2_HC_XFER_XACT_ERR);
  1476. goto handle_xacterr_done;
  1477. }
  1478. switch (dwc2_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
  1479. case USB_ENDPOINT_XFER_CONTROL:
  1480. case USB_ENDPOINT_XFER_BULK:
  1481. qtd->error_count++;
  1482. if (!chan->qh->ping_state) {
  1483. dwc2_update_urb_state_abn(hsotg, chan, chnum, qtd->urb,
  1484. qtd, DWC2_HC_XFER_XACT_ERR);
  1485. dwc2_hcd_save_data_toggle(hsotg, chan, chnum, qtd);
  1486. if (!chan->ep_is_in && chan->speed == USB_SPEED_HIGH)
  1487. chan->qh->ping_state = 1;
  1488. }
  1489. /*
  1490. * Halt the channel so the transfer can be re-started from
  1491. * the appropriate point or the PING protocol will start
  1492. */
  1493. dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_XACT_ERR);
  1494. break;
  1495. case USB_ENDPOINT_XFER_INT:
  1496. qtd->error_count++;
  1497. if (chan->do_split && chan->complete_split)
  1498. qtd->complete_split = 0;
  1499. dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_XACT_ERR);
  1500. break;
  1501. case USB_ENDPOINT_XFER_ISOC:
  1502. {
  1503. enum dwc2_halt_status halt_status;
  1504. halt_status = dwc2_update_isoc_urb_state(hsotg, chan,
  1505. chnum, qtd, DWC2_HC_XFER_XACT_ERR);
  1506. dwc2_halt_channel(hsotg, chan, qtd, halt_status);
  1507. }
  1508. break;
  1509. }
  1510. handle_xacterr_done:
  1511. disable_hc_int(hsotg, chnum, HCINTMSK_XACTERR);
  1512. }
  1513. /*
  1514. * Handles a host channel frame overrun interrupt. This handler may be called
  1515. * in either DMA mode or Slave mode.
  1516. */
  1517. static void dwc2_hc_frmovrun_intr(struct dwc2_hsotg *hsotg,
  1518. struct dwc2_host_chan *chan, int chnum,
  1519. struct dwc2_qtd *qtd)
  1520. {
  1521. enum dwc2_halt_status halt_status;
  1522. if (dbg_hc(chan))
  1523. dev_dbg(hsotg->dev, "--Host Channel %d Interrupt: Frame Overrun--\n",
  1524. chnum);
  1525. dwc2_hc_handle_tt_clear(hsotg, chan, qtd);
  1526. switch (dwc2_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
  1527. case USB_ENDPOINT_XFER_CONTROL:
  1528. case USB_ENDPOINT_XFER_BULK:
  1529. break;
  1530. case USB_ENDPOINT_XFER_INT:
  1531. dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_FRAME_OVERRUN);
  1532. break;
  1533. case USB_ENDPOINT_XFER_ISOC:
  1534. halt_status = dwc2_update_isoc_urb_state(hsotg, chan, chnum,
  1535. qtd, DWC2_HC_XFER_FRAME_OVERRUN);
  1536. dwc2_halt_channel(hsotg, chan, qtd, halt_status);
  1537. break;
  1538. }
  1539. disable_hc_int(hsotg, chnum, HCINTMSK_FRMOVRUN);
  1540. }
  1541. /*
  1542. * Handles a host channel data toggle error interrupt. This handler may be
  1543. * called in either DMA mode or Slave mode.
  1544. */
  1545. static void dwc2_hc_datatglerr_intr(struct dwc2_hsotg *hsotg,
  1546. struct dwc2_host_chan *chan, int chnum,
  1547. struct dwc2_qtd *qtd)
  1548. {
  1549. dev_dbg(hsotg->dev,
  1550. "--Host Channel %d Interrupt: Data Toggle Error--\n", chnum);
  1551. if (chan->ep_is_in)
  1552. qtd->error_count = 0;
  1553. else
  1554. dev_err(hsotg->dev,
  1555. "Data Toggle Error on OUT transfer, channel %d\n",
  1556. chnum);
  1557. dwc2_hc_handle_tt_clear(hsotg, chan, qtd);
  1558. disable_hc_int(hsotg, chnum, HCINTMSK_DATATGLERR);
  1559. }
  1560. /*
  1561. * For debug only. It checks that a valid halt status is set and that
  1562. * HCCHARn.chdis is clear. If there's a problem, corrective action is
  1563. * taken and a warning is issued.
  1564. *
  1565. * Return: true if halt status is ok, false otherwise
  1566. */
  1567. static bool dwc2_halt_status_ok(struct dwc2_hsotg *hsotg,
  1568. struct dwc2_host_chan *chan, int chnum,
  1569. struct dwc2_qtd *qtd)
  1570. {
  1571. #ifdef DEBUG
  1572. u32 hcchar;
  1573. u32 hctsiz;
  1574. u32 hcintmsk;
  1575. u32 hcsplt;
  1576. if (chan->halt_status == DWC2_HC_XFER_NO_HALT_STATUS) {
  1577. /*
  1578. * This code is here only as a check. This condition should
  1579. * never happen. Ignore the halt if it does occur.
  1580. */
  1581. hcchar = dwc2_readl(hsotg, HCCHAR(chnum));
  1582. hctsiz = dwc2_readl(hsotg, HCTSIZ(chnum));
  1583. hcintmsk = dwc2_readl(hsotg, HCINTMSK(chnum));
  1584. hcsplt = dwc2_readl(hsotg, HCSPLT(chnum));
  1585. dev_dbg(hsotg->dev,
  1586. "%s: chan->halt_status DWC2_HC_XFER_NO_HALT_STATUS,\n",
  1587. __func__);
  1588. dev_dbg(hsotg->dev,
  1589. "channel %d, hcchar 0x%08x, hctsiz 0x%08x,\n",
  1590. chnum, hcchar, hctsiz);
  1591. dev_dbg(hsotg->dev,
  1592. "hcint 0x%08x, hcintmsk 0x%08x, hcsplt 0x%08x,\n",
  1593. chan->hcint, hcintmsk, hcsplt);
  1594. if (qtd)
  1595. dev_dbg(hsotg->dev, "qtd->complete_split %d\n",
  1596. qtd->complete_split);
  1597. dev_warn(hsotg->dev,
  1598. "%s: no halt status, channel %d, ignoring interrupt\n",
  1599. __func__, chnum);
  1600. return false;
  1601. }
  1602. /*
  1603. * This code is here only as a check. hcchar.chdis should never be set
  1604. * when the halt interrupt occurs. Halt the channel again if it does
  1605. * occur.
  1606. */
  1607. hcchar = dwc2_readl(hsotg, HCCHAR(chnum));
  1608. if (hcchar & HCCHAR_CHDIS) {
  1609. dev_warn(hsotg->dev,
  1610. "%s: hcchar.chdis set unexpectedly, hcchar 0x%08x, trying to halt again\n",
  1611. __func__, hcchar);
  1612. chan->halt_pending = 0;
  1613. dwc2_halt_channel(hsotg, chan, qtd, chan->halt_status);
  1614. return false;
  1615. }
  1616. #endif
  1617. return true;
  1618. }
  1619. /*
  1620. * Handles a host Channel Halted interrupt in DMA mode. This handler
  1621. * determines the reason the channel halted and proceeds accordingly.
  1622. */
  1623. static void dwc2_hc_chhltd_intr_dma(struct dwc2_hsotg *hsotg,
  1624. struct dwc2_host_chan *chan, int chnum,
  1625. struct dwc2_qtd *qtd)
  1626. {
  1627. u32 hcintmsk;
  1628. int out_nak_enh = 0;
  1629. if (dbg_hc(chan))
  1630. dev_vdbg(hsotg->dev,
  1631. "--Host Channel %d Interrupt: DMA Channel Halted--\n",
  1632. chnum);
  1633. /*
  1634. * For core with OUT NAK enhancement, the flow for high-speed
  1635. * CONTROL/BULK OUT is handled a little differently
  1636. */
  1637. if (hsotg->hw_params.snpsid >= DWC2_CORE_REV_2_71a) {
  1638. if (chan->speed == USB_SPEED_HIGH && !chan->ep_is_in &&
  1639. (chan->ep_type == USB_ENDPOINT_XFER_CONTROL ||
  1640. chan->ep_type == USB_ENDPOINT_XFER_BULK)) {
  1641. out_nak_enh = 1;
  1642. }
  1643. }
  1644. if (chan->halt_status == DWC2_HC_XFER_URB_DEQUEUE ||
  1645. (chan->halt_status == DWC2_HC_XFER_AHB_ERR &&
  1646. !hsotg->params.dma_desc_enable)) {
  1647. if (hsotg->params.dma_desc_enable)
  1648. dwc2_hcd_complete_xfer_ddma(hsotg, chan, chnum,
  1649. chan->halt_status);
  1650. else
  1651. /*
  1652. * Just release the channel. A dequeue can happen on a
  1653. * transfer timeout. In the case of an AHB Error, the
  1654. * channel was forced to halt because there's no way to
  1655. * gracefully recover.
  1656. */
  1657. dwc2_release_channel(hsotg, chan, qtd,
  1658. chan->halt_status);
  1659. return;
  1660. }
  1661. hcintmsk = dwc2_readl(hsotg, HCINTMSK(chnum));
  1662. if (chan->hcint & HCINTMSK_XFERCOMPL) {
  1663. /*
  1664. * Todo: This is here because of a possible hardware bug. Spec
  1665. * says that on SPLIT-ISOC OUT transfers in DMA mode that a HALT
  1666. * interrupt w/ACK bit set should occur, but I only see the
  1667. * XFERCOMP bit, even with it masked out. This is a workaround
  1668. * for that behavior. Should fix this when hardware is fixed.
  1669. */
  1670. if (chan->ep_type == USB_ENDPOINT_XFER_ISOC && !chan->ep_is_in)
  1671. dwc2_hc_ack_intr(hsotg, chan, chnum, qtd);
  1672. dwc2_hc_xfercomp_intr(hsotg, chan, chnum, qtd);
  1673. } else if (chan->hcint & HCINTMSK_STALL) {
  1674. dwc2_hc_stall_intr(hsotg, chan, chnum, qtd);
  1675. } else if ((chan->hcint & HCINTMSK_XACTERR) &&
  1676. !hsotg->params.dma_desc_enable) {
  1677. if (out_nak_enh) {
  1678. if (chan->hcint &
  1679. (HCINTMSK_NYET | HCINTMSK_NAK | HCINTMSK_ACK)) {
  1680. dev_vdbg(hsotg->dev,
  1681. "XactErr with NYET/NAK/ACK\n");
  1682. qtd->error_count = 0;
  1683. } else {
  1684. dev_vdbg(hsotg->dev,
  1685. "XactErr without NYET/NAK/ACK\n");
  1686. }
  1687. }
  1688. /*
  1689. * Must handle xacterr before nak or ack. Could get a xacterr
  1690. * at the same time as either of these on a BULK/CONTROL OUT
  1691. * that started with a PING. The xacterr takes precedence.
  1692. */
  1693. dwc2_hc_xacterr_intr(hsotg, chan, chnum, qtd);
  1694. } else if ((chan->hcint & HCINTMSK_XCS_XACT) &&
  1695. hsotg->params.dma_desc_enable) {
  1696. dwc2_hc_xacterr_intr(hsotg, chan, chnum, qtd);
  1697. } else if ((chan->hcint & HCINTMSK_AHBERR) &&
  1698. hsotg->params.dma_desc_enable) {
  1699. dwc2_hc_ahberr_intr(hsotg, chan, chnum, qtd);
  1700. } else if (chan->hcint & HCINTMSK_BBLERR) {
  1701. dwc2_hc_babble_intr(hsotg, chan, chnum, qtd);
  1702. } else if (chan->hcint & HCINTMSK_FRMOVRUN) {
  1703. dwc2_hc_frmovrun_intr(hsotg, chan, chnum, qtd);
  1704. } else if (!out_nak_enh) {
  1705. if (chan->hcint & HCINTMSK_NYET) {
  1706. /*
  1707. * Must handle nyet before nak or ack. Could get a nyet
  1708. * at the same time as either of those on a BULK/CONTROL
  1709. * OUT that started with a PING. The nyet takes
  1710. * precedence.
  1711. */
  1712. dwc2_hc_nyet_intr(hsotg, chan, chnum, qtd);
  1713. } else if ((chan->hcint & HCINTMSK_NAK) &&
  1714. !(hcintmsk & HCINTMSK_NAK)) {
  1715. /*
  1716. * If nak is not masked, it's because a non-split IN
  1717. * transfer is in an error state. In that case, the nak
  1718. * is handled by the nak interrupt handler, not here.
  1719. * Handle nak here for BULK/CONTROL OUT transfers, which
  1720. * halt on a NAK to allow rewinding the buffer pointer.
  1721. */
  1722. dwc2_hc_nak_intr(hsotg, chan, chnum, qtd);
  1723. } else if ((chan->hcint & HCINTMSK_ACK) &&
  1724. !(hcintmsk & HCINTMSK_ACK)) {
  1725. /*
  1726. * If ack is not masked, it's because a non-split IN
  1727. * transfer is in an error state. In that case, the ack
  1728. * is handled by the ack interrupt handler, not here.
  1729. * Handle ack here for split transfers. Start splits
  1730. * halt on ACK.
  1731. */
  1732. dwc2_hc_ack_intr(hsotg, chan, chnum, qtd);
  1733. } else {
  1734. if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
  1735. chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
  1736. /*
  1737. * A periodic transfer halted with no other
  1738. * channel interrupts set. Assume it was halted
  1739. * by the core because it could not be completed
  1740. * in its scheduled (micro)frame.
  1741. */
  1742. dev_dbg(hsotg->dev,
  1743. "%s: Halt channel %d (assume incomplete periodic transfer)\n",
  1744. __func__, chnum);
  1745. dwc2_halt_channel(hsotg, chan, qtd,
  1746. DWC2_HC_XFER_PERIODIC_INCOMPLETE);
  1747. } else {
  1748. dev_err(hsotg->dev,
  1749. "%s: Channel %d - ChHltd set, but reason is unknown\n",
  1750. __func__, chnum);
  1751. dev_err(hsotg->dev,
  1752. "hcint 0x%08x, intsts 0x%08x\n",
  1753. chan->hcint,
  1754. dwc2_readl(hsotg, GINTSTS));
  1755. goto error;
  1756. }
  1757. }
  1758. } else {
  1759. dev_info(hsotg->dev,
  1760. "NYET/NAK/ACK/other in non-error case, 0x%08x\n",
  1761. chan->hcint);
  1762. error:
  1763. /* Failthrough: use 3-strikes rule */
  1764. qtd->error_count++;
  1765. dwc2_update_urb_state_abn(hsotg, chan, chnum, qtd->urb,
  1766. qtd, DWC2_HC_XFER_XACT_ERR);
  1767. /*
  1768. * We can get here after a completed transaction
  1769. * (urb->actual_length >= urb->length) which was not reported
  1770. * as completed. If that is the case, and we do not abort
  1771. * the transfer, a transfer of size 0 will be enqueued
  1772. * subsequently. If urb->actual_length is not DMA-aligned,
  1773. * the buffer will then point to an unaligned address, and
  1774. * the resulting behavior is undefined. Bail out in that
  1775. * situation.
  1776. */
  1777. if (qtd->urb->actual_length >= qtd->urb->length)
  1778. qtd->error_count = 3;
  1779. dwc2_hcd_save_data_toggle(hsotg, chan, chnum, qtd);
  1780. dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_XACT_ERR);
  1781. }
  1782. }
  1783. /*
  1784. * Handles a host channel Channel Halted interrupt
  1785. *
  1786. * In slave mode, this handler is called only when the driver specifically
  1787. * requests a halt. This occurs during handling other host channel interrupts
  1788. * (e.g. nak, xacterr, stall, nyet, etc.).
  1789. *
  1790. * In DMA mode, this is the interrupt that occurs when the core has finished
  1791. * processing a transfer on a channel. Other host channel interrupts (except
  1792. * ahberr) are disabled in DMA mode.
  1793. */
  1794. static void dwc2_hc_chhltd_intr(struct dwc2_hsotg *hsotg,
  1795. struct dwc2_host_chan *chan, int chnum,
  1796. struct dwc2_qtd *qtd)
  1797. {
  1798. if (dbg_hc(chan))
  1799. dev_vdbg(hsotg->dev, "--Host Channel %d Interrupt: Channel Halted--\n",
  1800. chnum);
  1801. if (hsotg->params.host_dma) {
  1802. dwc2_hc_chhltd_intr_dma(hsotg, chan, chnum, qtd);
  1803. } else {
  1804. if (!dwc2_halt_status_ok(hsotg, chan, chnum, qtd))
  1805. return;
  1806. dwc2_release_channel(hsotg, chan, qtd, chan->halt_status);
  1807. }
  1808. }
  1809. /*
  1810. * Check if the given qtd is still the top of the list (and thus valid).
  1811. *
  1812. * If dwc2_hcd_qtd_unlink_and_free() has been called since we grabbed
  1813. * the qtd from the top of the list, this will return false (otherwise true).
  1814. */
  1815. static bool dwc2_check_qtd_still_ok(struct dwc2_qtd *qtd, struct dwc2_qh *qh)
  1816. {
  1817. struct dwc2_qtd *cur_head;
  1818. if (!qh)
  1819. return false;
  1820. cur_head = list_first_entry(&qh->qtd_list, struct dwc2_qtd,
  1821. qtd_list_entry);
  1822. return (cur_head == qtd);
  1823. }
  1824. /* Handles interrupt for a specific Host Channel */
  1825. static void dwc2_hc_n_intr(struct dwc2_hsotg *hsotg, int chnum)
  1826. {
  1827. struct dwc2_qtd *qtd;
  1828. struct dwc2_host_chan *chan;
  1829. u32 hcint, hcintmsk;
  1830. chan = hsotg->hc_ptr_array[chnum];
  1831. hcint = dwc2_readl(hsotg, HCINT(chnum));
  1832. hcintmsk = dwc2_readl(hsotg, HCINTMSK(chnum));
  1833. if (!chan) {
  1834. dev_err(hsotg->dev, "## hc_ptr_array for channel is NULL ##\n");
  1835. dwc2_writel(hsotg, hcint, HCINT(chnum));
  1836. return;
  1837. }
  1838. if (dbg_hc(chan)) {
  1839. dev_vdbg(hsotg->dev, "--Host Channel Interrupt--, Channel %d\n",
  1840. chnum);
  1841. dev_vdbg(hsotg->dev,
  1842. " hcint 0x%08x, hcintmsk 0x%08x, hcint&hcintmsk 0x%08x\n",
  1843. hcint, hcintmsk, hcint & hcintmsk);
  1844. }
  1845. dwc2_writel(hsotg, hcint, HCINT(chnum));
  1846. /*
  1847. * If we got an interrupt after someone called
  1848. * dwc2_hcd_endpoint_disable() we don't want to crash below
  1849. */
  1850. if (!chan->qh) {
  1851. dev_warn(hsotg->dev, "Interrupt on disabled channel\n");
  1852. return;
  1853. }
  1854. chan->hcint = hcint;
  1855. hcint &= hcintmsk;
  1856. /*
  1857. * If the channel was halted due to a dequeue, the qtd list might
  1858. * be empty or at least the first entry will not be the active qtd.
  1859. * In this case, take a shortcut and just release the channel.
  1860. */
  1861. if (chan->halt_status == DWC2_HC_XFER_URB_DEQUEUE) {
  1862. /*
  1863. * If the channel was halted, this should be the only
  1864. * interrupt unmasked
  1865. */
  1866. WARN_ON(hcint != HCINTMSK_CHHLTD);
  1867. if (hsotg->params.dma_desc_enable)
  1868. dwc2_hcd_complete_xfer_ddma(hsotg, chan, chnum,
  1869. chan->halt_status);
  1870. else
  1871. dwc2_release_channel(hsotg, chan, NULL,
  1872. chan->halt_status);
  1873. return;
  1874. }
  1875. if (list_empty(&chan->qh->qtd_list)) {
  1876. /*
  1877. * TODO: Will this ever happen with the
  1878. * DWC2_HC_XFER_URB_DEQUEUE handling above?
  1879. */
  1880. dev_dbg(hsotg->dev, "## no QTD queued for channel %d ##\n",
  1881. chnum);
  1882. dev_dbg(hsotg->dev,
  1883. " hcint 0x%08x, hcintmsk 0x%08x, hcint&hcintmsk 0x%08x\n",
  1884. chan->hcint, hcintmsk, hcint);
  1885. chan->halt_status = DWC2_HC_XFER_NO_HALT_STATUS;
  1886. disable_hc_int(hsotg, chnum, HCINTMSK_CHHLTD);
  1887. chan->hcint = 0;
  1888. return;
  1889. }
  1890. qtd = list_first_entry(&chan->qh->qtd_list, struct dwc2_qtd,
  1891. qtd_list_entry);
  1892. if (!hsotg->params.host_dma) {
  1893. if ((hcint & HCINTMSK_CHHLTD) && hcint != HCINTMSK_CHHLTD)
  1894. hcint &= ~HCINTMSK_CHHLTD;
  1895. }
  1896. if (hcint & HCINTMSK_XFERCOMPL) {
  1897. dwc2_hc_xfercomp_intr(hsotg, chan, chnum, qtd);
  1898. /*
  1899. * If NYET occurred at same time as Xfer Complete, the NYET is
  1900. * handled by the Xfer Complete interrupt handler. Don't want
  1901. * to call the NYET interrupt handler in this case.
  1902. */
  1903. hcint &= ~HCINTMSK_NYET;
  1904. }
  1905. if (hcint & HCINTMSK_CHHLTD) {
  1906. dwc2_hc_chhltd_intr(hsotg, chan, chnum, qtd);
  1907. if (!dwc2_check_qtd_still_ok(qtd, chan->qh))
  1908. goto exit;
  1909. }
  1910. if (hcint & HCINTMSK_AHBERR) {
  1911. dwc2_hc_ahberr_intr(hsotg, chan, chnum, qtd);
  1912. if (!dwc2_check_qtd_still_ok(qtd, chan->qh))
  1913. goto exit;
  1914. }
  1915. if (hcint & HCINTMSK_STALL) {
  1916. dwc2_hc_stall_intr(hsotg, chan, chnum, qtd);
  1917. if (!dwc2_check_qtd_still_ok(qtd, chan->qh))
  1918. goto exit;
  1919. }
  1920. if (hcint & HCINTMSK_NAK) {
  1921. dwc2_hc_nak_intr(hsotg, chan, chnum, qtd);
  1922. if (!dwc2_check_qtd_still_ok(qtd, chan->qh))
  1923. goto exit;
  1924. }
  1925. if (hcint & HCINTMSK_ACK) {
  1926. dwc2_hc_ack_intr(hsotg, chan, chnum, qtd);
  1927. if (!dwc2_check_qtd_still_ok(qtd, chan->qh))
  1928. goto exit;
  1929. }
  1930. if (hcint & HCINTMSK_NYET) {
  1931. dwc2_hc_nyet_intr(hsotg, chan, chnum, qtd);
  1932. if (!dwc2_check_qtd_still_ok(qtd, chan->qh))
  1933. goto exit;
  1934. }
  1935. if (hcint & HCINTMSK_XACTERR) {
  1936. dwc2_hc_xacterr_intr(hsotg, chan, chnum, qtd);
  1937. if (!dwc2_check_qtd_still_ok(qtd, chan->qh))
  1938. goto exit;
  1939. }
  1940. if (hcint & HCINTMSK_BBLERR) {
  1941. dwc2_hc_babble_intr(hsotg, chan, chnum, qtd);
  1942. if (!dwc2_check_qtd_still_ok(qtd, chan->qh))
  1943. goto exit;
  1944. }
  1945. if (hcint & HCINTMSK_FRMOVRUN) {
  1946. dwc2_hc_frmovrun_intr(hsotg, chan, chnum, qtd);
  1947. if (!dwc2_check_qtd_still_ok(qtd, chan->qh))
  1948. goto exit;
  1949. }
  1950. if (hcint & HCINTMSK_DATATGLERR) {
  1951. dwc2_hc_datatglerr_intr(hsotg, chan, chnum, qtd);
  1952. if (!dwc2_check_qtd_still_ok(qtd, chan->qh))
  1953. goto exit;
  1954. }
  1955. exit:
  1956. chan->hcint = 0;
  1957. }
  1958. /*
  1959. * This interrupt indicates that one or more host channels has a pending
  1960. * interrupt. There are multiple conditions that can cause each host channel
  1961. * interrupt. This function determines which conditions have occurred for each
  1962. * host channel interrupt and handles them appropriately.
  1963. */
  1964. static void dwc2_hc_intr(struct dwc2_hsotg *hsotg)
  1965. {
  1966. u32 haint;
  1967. int i;
  1968. struct dwc2_host_chan *chan, *chan_tmp;
  1969. haint = dwc2_readl(hsotg, HAINT);
  1970. if (dbg_perio()) {
  1971. dev_vdbg(hsotg->dev, "%s()\n", __func__);
  1972. dev_vdbg(hsotg->dev, "HAINT=%08x\n", haint);
  1973. }
  1974. /*
  1975. * According to USB 2.0 spec section 11.18.8, a host must
  1976. * issue complete-split transactions in a microframe for a
  1977. * set of full-/low-speed endpoints in the same relative
  1978. * order as the start-splits were issued in a microframe for.
  1979. */
  1980. list_for_each_entry_safe(chan, chan_tmp, &hsotg->split_order,
  1981. split_order_list_entry) {
  1982. int hc_num = chan->hc_num;
  1983. if (haint & (1 << hc_num)) {
  1984. dwc2_hc_n_intr(hsotg, hc_num);
  1985. haint &= ~(1 << hc_num);
  1986. }
  1987. }
  1988. for (i = 0; i < hsotg->params.host_channels; i++) {
  1989. if (haint & (1 << i))
  1990. dwc2_hc_n_intr(hsotg, i);
  1991. }
  1992. }
  1993. /* This function handles interrupts for the HCD */
  1994. irqreturn_t dwc2_handle_hcd_intr(struct dwc2_hsotg *hsotg)
  1995. {
  1996. u32 gintsts, dbg_gintsts;
  1997. irqreturn_t retval = IRQ_NONE;
  1998. if (!dwc2_is_controller_alive(hsotg)) {
  1999. dev_warn(hsotg->dev, "Controller is dead\n");
  2000. return retval;
  2001. }
  2002. spin_lock(&hsotg->lock);
  2003. /* Check if HOST Mode */
  2004. if (dwc2_is_host_mode(hsotg)) {
  2005. gintsts = dwc2_read_core_intr(hsotg);
  2006. if (!gintsts) {
  2007. spin_unlock(&hsotg->lock);
  2008. return retval;
  2009. }
  2010. retval = IRQ_HANDLED;
  2011. dbg_gintsts = gintsts;
  2012. #ifndef DEBUG_SOF
  2013. dbg_gintsts &= ~GINTSTS_SOF;
  2014. #endif
  2015. if (!dbg_perio())
  2016. dbg_gintsts &= ~(GINTSTS_HCHINT | GINTSTS_RXFLVL |
  2017. GINTSTS_PTXFEMP);
  2018. /* Only print if there are any non-suppressed interrupts left */
  2019. if (dbg_gintsts)
  2020. dev_vdbg(hsotg->dev,
  2021. "DWC OTG HCD Interrupt Detected gintsts&gintmsk=0x%08x\n",
  2022. gintsts);
  2023. if (gintsts & GINTSTS_SOF)
  2024. dwc2_sof_intr(hsotg);
  2025. if (gintsts & GINTSTS_RXFLVL)
  2026. dwc2_rx_fifo_level_intr(hsotg);
  2027. if (gintsts & GINTSTS_NPTXFEMP)
  2028. dwc2_np_tx_fifo_empty_intr(hsotg);
  2029. if (gintsts & GINTSTS_PRTINT)
  2030. dwc2_port_intr(hsotg);
  2031. if (gintsts & GINTSTS_HCHINT)
  2032. dwc2_hc_intr(hsotg);
  2033. if (gintsts & GINTSTS_PTXFEMP)
  2034. dwc2_perio_tx_fifo_empty_intr(hsotg);
  2035. if (dbg_gintsts) {
  2036. dev_vdbg(hsotg->dev,
  2037. "DWC OTG HCD Finished Servicing Interrupts\n");
  2038. dev_vdbg(hsotg->dev,
  2039. "DWC OTG HCD gintsts=0x%08x gintmsk=0x%08x\n",
  2040. dwc2_readl(hsotg, GINTSTS),
  2041. dwc2_readl(hsotg, GINTMSK));
  2042. }
  2043. }
  2044. spin_unlock(&hsotg->lock);
  2045. return retval;
  2046. }