hcd_intr.c 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284
  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_warn(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. dwc2_hcd_save_data_toggle(hsotg, chan, chnum, qtd);
  1768. dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_XACT_ERR);
  1769. }
  1770. }
  1771. /*
  1772. * Handles a host channel Channel Halted interrupt
  1773. *
  1774. * In slave mode, this handler is called only when the driver specifically
  1775. * requests a halt. This occurs during handling other host channel interrupts
  1776. * (e.g. nak, xacterr, stall, nyet, etc.).
  1777. *
  1778. * In DMA mode, this is the interrupt that occurs when the core has finished
  1779. * processing a transfer on a channel. Other host channel interrupts (except
  1780. * ahberr) are disabled in DMA mode.
  1781. */
  1782. static void dwc2_hc_chhltd_intr(struct dwc2_hsotg *hsotg,
  1783. struct dwc2_host_chan *chan, int chnum,
  1784. struct dwc2_qtd *qtd)
  1785. {
  1786. if (dbg_hc(chan))
  1787. dev_vdbg(hsotg->dev, "--Host Channel %d Interrupt: Channel Halted--\n",
  1788. chnum);
  1789. if (hsotg->params.host_dma) {
  1790. dwc2_hc_chhltd_intr_dma(hsotg, chan, chnum, qtd);
  1791. } else {
  1792. if (!dwc2_halt_status_ok(hsotg, chan, chnum, qtd))
  1793. return;
  1794. dwc2_release_channel(hsotg, chan, qtd, chan->halt_status);
  1795. }
  1796. }
  1797. /*
  1798. * Check if the given qtd is still the top of the list (and thus valid).
  1799. *
  1800. * If dwc2_hcd_qtd_unlink_and_free() has been called since we grabbed
  1801. * the qtd from the top of the list, this will return false (otherwise true).
  1802. */
  1803. static bool dwc2_check_qtd_still_ok(struct dwc2_qtd *qtd, struct dwc2_qh *qh)
  1804. {
  1805. struct dwc2_qtd *cur_head;
  1806. if (!qh)
  1807. return false;
  1808. cur_head = list_first_entry(&qh->qtd_list, struct dwc2_qtd,
  1809. qtd_list_entry);
  1810. return (cur_head == qtd);
  1811. }
  1812. /* Handles interrupt for a specific Host Channel */
  1813. static void dwc2_hc_n_intr(struct dwc2_hsotg *hsotg, int chnum)
  1814. {
  1815. struct dwc2_qtd *qtd;
  1816. struct dwc2_host_chan *chan;
  1817. u32 hcint, hcintmsk;
  1818. chan = hsotg->hc_ptr_array[chnum];
  1819. hcint = dwc2_readl(hsotg, HCINT(chnum));
  1820. hcintmsk = dwc2_readl(hsotg, HCINTMSK(chnum));
  1821. if (!chan) {
  1822. dev_err(hsotg->dev, "## hc_ptr_array for channel is NULL ##\n");
  1823. dwc2_writel(hsotg, hcint, HCINT(chnum));
  1824. return;
  1825. }
  1826. if (dbg_hc(chan)) {
  1827. dev_vdbg(hsotg->dev, "--Host Channel Interrupt--, Channel %d\n",
  1828. chnum);
  1829. dev_vdbg(hsotg->dev,
  1830. " hcint 0x%08x, hcintmsk 0x%08x, hcint&hcintmsk 0x%08x\n",
  1831. hcint, hcintmsk, hcint & hcintmsk);
  1832. }
  1833. dwc2_writel(hsotg, hcint, HCINT(chnum));
  1834. /*
  1835. * If we got an interrupt after someone called
  1836. * dwc2_hcd_endpoint_disable() we don't want to crash below
  1837. */
  1838. if (!chan->qh) {
  1839. dev_warn(hsotg->dev, "Interrupt on disabled channel\n");
  1840. return;
  1841. }
  1842. chan->hcint = hcint;
  1843. hcint &= hcintmsk;
  1844. /*
  1845. * If the channel was halted due to a dequeue, the qtd list might
  1846. * be empty or at least the first entry will not be the active qtd.
  1847. * In this case, take a shortcut and just release the channel.
  1848. */
  1849. if (chan->halt_status == DWC2_HC_XFER_URB_DEQUEUE) {
  1850. /*
  1851. * If the channel was halted, this should be the only
  1852. * interrupt unmasked
  1853. */
  1854. WARN_ON(hcint != HCINTMSK_CHHLTD);
  1855. if (hsotg->params.dma_desc_enable)
  1856. dwc2_hcd_complete_xfer_ddma(hsotg, chan, chnum,
  1857. chan->halt_status);
  1858. else
  1859. dwc2_release_channel(hsotg, chan, NULL,
  1860. chan->halt_status);
  1861. return;
  1862. }
  1863. if (list_empty(&chan->qh->qtd_list)) {
  1864. /*
  1865. * TODO: Will this ever happen with the
  1866. * DWC2_HC_XFER_URB_DEQUEUE handling above?
  1867. */
  1868. dev_dbg(hsotg->dev, "## no QTD queued for channel %d ##\n",
  1869. chnum);
  1870. dev_dbg(hsotg->dev,
  1871. " hcint 0x%08x, hcintmsk 0x%08x, hcint&hcintmsk 0x%08x\n",
  1872. chan->hcint, hcintmsk, hcint);
  1873. chan->halt_status = DWC2_HC_XFER_NO_HALT_STATUS;
  1874. disable_hc_int(hsotg, chnum, HCINTMSK_CHHLTD);
  1875. chan->hcint = 0;
  1876. return;
  1877. }
  1878. qtd = list_first_entry(&chan->qh->qtd_list, struct dwc2_qtd,
  1879. qtd_list_entry);
  1880. if (!hsotg->params.host_dma) {
  1881. if ((hcint & HCINTMSK_CHHLTD) && hcint != HCINTMSK_CHHLTD)
  1882. hcint &= ~HCINTMSK_CHHLTD;
  1883. }
  1884. if (hcint & HCINTMSK_XFERCOMPL) {
  1885. dwc2_hc_xfercomp_intr(hsotg, chan, chnum, qtd);
  1886. /*
  1887. * If NYET occurred at same time as Xfer Complete, the NYET is
  1888. * handled by the Xfer Complete interrupt handler. Don't want
  1889. * to call the NYET interrupt handler in this case.
  1890. */
  1891. hcint &= ~HCINTMSK_NYET;
  1892. }
  1893. if (hcint & HCINTMSK_CHHLTD) {
  1894. dwc2_hc_chhltd_intr(hsotg, chan, chnum, qtd);
  1895. if (!dwc2_check_qtd_still_ok(qtd, chan->qh))
  1896. goto exit;
  1897. }
  1898. if (hcint & HCINTMSK_AHBERR) {
  1899. dwc2_hc_ahberr_intr(hsotg, chan, chnum, qtd);
  1900. if (!dwc2_check_qtd_still_ok(qtd, chan->qh))
  1901. goto exit;
  1902. }
  1903. if (hcint & HCINTMSK_STALL) {
  1904. dwc2_hc_stall_intr(hsotg, chan, chnum, qtd);
  1905. if (!dwc2_check_qtd_still_ok(qtd, chan->qh))
  1906. goto exit;
  1907. }
  1908. if (hcint & HCINTMSK_NAK) {
  1909. dwc2_hc_nak_intr(hsotg, chan, chnum, qtd);
  1910. if (!dwc2_check_qtd_still_ok(qtd, chan->qh))
  1911. goto exit;
  1912. }
  1913. if (hcint & HCINTMSK_ACK) {
  1914. dwc2_hc_ack_intr(hsotg, chan, chnum, qtd);
  1915. if (!dwc2_check_qtd_still_ok(qtd, chan->qh))
  1916. goto exit;
  1917. }
  1918. if (hcint & HCINTMSK_NYET) {
  1919. dwc2_hc_nyet_intr(hsotg, chan, chnum, qtd);
  1920. if (!dwc2_check_qtd_still_ok(qtd, chan->qh))
  1921. goto exit;
  1922. }
  1923. if (hcint & HCINTMSK_XACTERR) {
  1924. dwc2_hc_xacterr_intr(hsotg, chan, chnum, qtd);
  1925. if (!dwc2_check_qtd_still_ok(qtd, chan->qh))
  1926. goto exit;
  1927. }
  1928. if (hcint & HCINTMSK_BBLERR) {
  1929. dwc2_hc_babble_intr(hsotg, chan, chnum, qtd);
  1930. if (!dwc2_check_qtd_still_ok(qtd, chan->qh))
  1931. goto exit;
  1932. }
  1933. if (hcint & HCINTMSK_FRMOVRUN) {
  1934. dwc2_hc_frmovrun_intr(hsotg, chan, chnum, qtd);
  1935. if (!dwc2_check_qtd_still_ok(qtd, chan->qh))
  1936. goto exit;
  1937. }
  1938. if (hcint & HCINTMSK_DATATGLERR) {
  1939. dwc2_hc_datatglerr_intr(hsotg, chan, chnum, qtd);
  1940. if (!dwc2_check_qtd_still_ok(qtd, chan->qh))
  1941. goto exit;
  1942. }
  1943. exit:
  1944. chan->hcint = 0;
  1945. }
  1946. /*
  1947. * This interrupt indicates that one or more host channels has a pending
  1948. * interrupt. There are multiple conditions that can cause each host channel
  1949. * interrupt. This function determines which conditions have occurred for each
  1950. * host channel interrupt and handles them appropriately.
  1951. */
  1952. static void dwc2_hc_intr(struct dwc2_hsotg *hsotg)
  1953. {
  1954. u32 haint;
  1955. int i;
  1956. struct dwc2_host_chan *chan, *chan_tmp;
  1957. haint = dwc2_readl(hsotg, HAINT);
  1958. if (dbg_perio()) {
  1959. dev_vdbg(hsotg->dev, "%s()\n", __func__);
  1960. dev_vdbg(hsotg->dev, "HAINT=%08x\n", haint);
  1961. }
  1962. /*
  1963. * According to USB 2.0 spec section 11.18.8, a host must
  1964. * issue complete-split transactions in a microframe for a
  1965. * set of full-/low-speed endpoints in the same relative
  1966. * order as the start-splits were issued in a microframe for.
  1967. */
  1968. list_for_each_entry_safe(chan, chan_tmp, &hsotg->split_order,
  1969. split_order_list_entry) {
  1970. int hc_num = chan->hc_num;
  1971. if (haint & (1 << hc_num)) {
  1972. dwc2_hc_n_intr(hsotg, hc_num);
  1973. haint &= ~(1 << hc_num);
  1974. }
  1975. }
  1976. for (i = 0; i < hsotg->params.host_channels; i++) {
  1977. if (haint & (1 << i))
  1978. dwc2_hc_n_intr(hsotg, i);
  1979. }
  1980. }
  1981. /* This function handles interrupts for the HCD */
  1982. irqreturn_t dwc2_handle_hcd_intr(struct dwc2_hsotg *hsotg)
  1983. {
  1984. u32 gintsts, dbg_gintsts;
  1985. irqreturn_t retval = IRQ_NONE;
  1986. if (!dwc2_is_controller_alive(hsotg)) {
  1987. dev_warn(hsotg->dev, "Controller is dead\n");
  1988. return retval;
  1989. }
  1990. spin_lock(&hsotg->lock);
  1991. /* Check if HOST Mode */
  1992. if (dwc2_is_host_mode(hsotg)) {
  1993. gintsts = dwc2_read_core_intr(hsotg);
  1994. if (!gintsts) {
  1995. spin_unlock(&hsotg->lock);
  1996. return retval;
  1997. }
  1998. retval = IRQ_HANDLED;
  1999. dbg_gintsts = gintsts;
  2000. #ifndef DEBUG_SOF
  2001. dbg_gintsts &= ~GINTSTS_SOF;
  2002. #endif
  2003. if (!dbg_perio())
  2004. dbg_gintsts &= ~(GINTSTS_HCHINT | GINTSTS_RXFLVL |
  2005. GINTSTS_PTXFEMP);
  2006. /* Only print if there are any non-suppressed interrupts left */
  2007. if (dbg_gintsts)
  2008. dev_vdbg(hsotg->dev,
  2009. "DWC OTG HCD Interrupt Detected gintsts&gintmsk=0x%08x\n",
  2010. gintsts);
  2011. if (gintsts & GINTSTS_SOF)
  2012. dwc2_sof_intr(hsotg);
  2013. if (gintsts & GINTSTS_RXFLVL)
  2014. dwc2_rx_fifo_level_intr(hsotg);
  2015. if (gintsts & GINTSTS_NPTXFEMP)
  2016. dwc2_np_tx_fifo_empty_intr(hsotg);
  2017. if (gintsts & GINTSTS_PRTINT)
  2018. dwc2_port_intr(hsotg);
  2019. if (gintsts & GINTSTS_HCHINT)
  2020. dwc2_hc_intr(hsotg);
  2021. if (gintsts & GINTSTS_PTXFEMP)
  2022. dwc2_perio_tx_fifo_empty_intr(hsotg);
  2023. if (dbg_gintsts) {
  2024. dev_vdbg(hsotg->dev,
  2025. "DWC OTG HCD Finished Servicing Interrupts\n");
  2026. dev_vdbg(hsotg->dev,
  2027. "DWC OTG HCD gintsts=0x%08x gintmsk=0x%08x\n",
  2028. dwc2_readl(hsotg, GINTSTS),
  2029. dwc2_readl(hsotg, GINTMSK));
  2030. }
  2031. }
  2032. spin_unlock(&hsotg->lock);
  2033. return retval;
  2034. }