hcd_intr.c 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270
  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(GINTSTS_SOF, hsotg->regs + 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->regs + 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->regs + HFIR);
  244. hfir &= ~HFIR_FRINT_MASK;
  245. hfir |= dwc2_calc_frame_interval(hsotg) << HFIR_FRINT_SHIFT &
  246. HFIR_FRINT_MASK;
  247. dwc2_writel(hfir, hsotg->regs + 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->regs + 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(usbcfg, hsotg->regs + GUSBCFG);
  262. do_reset = 1;
  263. }
  264. hcfg = dwc2_readl(hsotg->regs + 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(hcfg, hsotg->regs + 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(hcfg, hsotg->regs + 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(usbcfg, hsotg->regs + GUSBCFG);
  296. do_reset = 1;
  297. }
  298. }
  299. if (do_reset) {
  300. *hprt0_modify |= HPRT0_RST;
  301. dwc2_writel(*hprt0_modify, hsotg->regs + 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->regs + 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(hprt0_modify | HPRT0_CONNDET, hsotg->regs + 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(hprt0_modify | HPRT0_ENACHG, hsotg->regs + 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->regs + HCFG);
  361. hcfg &= ~HCFG_DESCDMA;
  362. dwc2_writel(hcfg, hsotg->regs + HCFG);
  363. }
  364. }
  365. }
  366. /* Overcurrent Change Interrupt */
  367. if (hprt0 & HPRT0_OVRCURRCHG) {
  368. dwc2_writel(hprt0_modify | HPRT0_OVRCURRCHG,
  369. hsotg->regs + 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->regs + 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->regs + 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->regs + 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->regs + HAINTMSK);
  698. haintmsk &= ~(1 << chan->hc_num);
  699. dwc2_writel(haintmsk, hsotg->regs + 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->regs + GINTMSK);
  741. gintmsk |= GINTSTS_NPTXFEMP;
  742. dwc2_writel(gintmsk, hsotg->regs + 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->regs + GINTMSK);
  759. gintmsk |= GINTSTS_PTXFEMP;
  760. dwc2_writel(gintmsk, hsotg->regs + 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->regs + 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) {
  843. qtd->complete_split = 0;
  844. qtd->isoc_split_offset = 0;
  845. return 0;
  846. }
  847. frame_desc->actual_length += len;
  848. qtd->isoc_split_offset += len;
  849. hctsiz = dwc2_readl(hsotg->regs + HCTSIZ(chnum));
  850. pid = (hctsiz & TSIZ_SC_MC_PID_MASK) >> TSIZ_SC_MC_PID_SHIFT;
  851. if (frame_desc->actual_length >= frame_desc->length || pid == 0) {
  852. frame_desc->status = 0;
  853. qtd->isoc_frame_index++;
  854. qtd->complete_split = 0;
  855. qtd->isoc_split_offset = 0;
  856. }
  857. if (qtd->isoc_frame_index == qtd->urb->packet_count) {
  858. dwc2_host_complete(hsotg, qtd, 0);
  859. dwc2_release_channel(hsotg, chan, qtd,
  860. DWC2_HC_XFER_URB_COMPLETE);
  861. } else {
  862. dwc2_release_channel(hsotg, chan, qtd,
  863. DWC2_HC_XFER_NO_HALT_STATUS);
  864. }
  865. return 1; /* Indicates that channel released */
  866. }
  867. /*
  868. * Handles a host channel Transfer Complete interrupt. This handler may be
  869. * called in either DMA mode or Slave mode.
  870. */
  871. static void dwc2_hc_xfercomp_intr(struct dwc2_hsotg *hsotg,
  872. struct dwc2_host_chan *chan, int chnum,
  873. struct dwc2_qtd *qtd)
  874. {
  875. struct dwc2_hcd_urb *urb = qtd->urb;
  876. enum dwc2_halt_status halt_status = DWC2_HC_XFER_COMPLETE;
  877. int pipe_type;
  878. int urb_xfer_done;
  879. if (dbg_hc(chan))
  880. dev_vdbg(hsotg->dev,
  881. "--Host Channel %d Interrupt: Transfer Complete--\n",
  882. chnum);
  883. if (!urb)
  884. goto handle_xfercomp_done;
  885. pipe_type = dwc2_hcd_get_pipe_type(&urb->pipe_info);
  886. if (hsotg->params.dma_desc_enable) {
  887. dwc2_hcd_complete_xfer_ddma(hsotg, chan, chnum, halt_status);
  888. if (pipe_type == USB_ENDPOINT_XFER_ISOC)
  889. /* Do not disable the interrupt, just clear it */
  890. return;
  891. goto handle_xfercomp_done;
  892. }
  893. /* Handle xfer complete on CSPLIT */
  894. if (chan->qh->do_split) {
  895. if (chan->ep_type == USB_ENDPOINT_XFER_ISOC && chan->ep_is_in &&
  896. hsotg->params.host_dma) {
  897. if (qtd->complete_split &&
  898. dwc2_xfercomp_isoc_split_in(hsotg, chan, chnum,
  899. qtd))
  900. goto handle_xfercomp_done;
  901. } else {
  902. qtd->complete_split = 0;
  903. }
  904. }
  905. /* Update the QTD and URB states */
  906. switch (pipe_type) {
  907. case USB_ENDPOINT_XFER_CONTROL:
  908. switch (qtd->control_phase) {
  909. case DWC2_CONTROL_SETUP:
  910. if (urb->length > 0)
  911. qtd->control_phase = DWC2_CONTROL_DATA;
  912. else
  913. qtd->control_phase = DWC2_CONTROL_STATUS;
  914. dev_vdbg(hsotg->dev,
  915. " Control setup transaction done\n");
  916. halt_status = DWC2_HC_XFER_COMPLETE;
  917. break;
  918. case DWC2_CONTROL_DATA:
  919. urb_xfer_done = dwc2_update_urb_state(hsotg, chan,
  920. chnum, urb, qtd);
  921. if (urb_xfer_done) {
  922. qtd->control_phase = DWC2_CONTROL_STATUS;
  923. dev_vdbg(hsotg->dev,
  924. " Control data transfer done\n");
  925. } else {
  926. dwc2_hcd_save_data_toggle(hsotg, chan, chnum,
  927. qtd);
  928. }
  929. halt_status = DWC2_HC_XFER_COMPLETE;
  930. break;
  931. case DWC2_CONTROL_STATUS:
  932. dev_vdbg(hsotg->dev, " Control transfer complete\n");
  933. if (urb->status == -EINPROGRESS)
  934. urb->status = 0;
  935. dwc2_host_complete(hsotg, qtd, urb->status);
  936. halt_status = DWC2_HC_XFER_URB_COMPLETE;
  937. break;
  938. }
  939. dwc2_complete_non_periodic_xfer(hsotg, chan, chnum, qtd,
  940. halt_status);
  941. break;
  942. case USB_ENDPOINT_XFER_BULK:
  943. dev_vdbg(hsotg->dev, " Bulk transfer complete\n");
  944. urb_xfer_done = dwc2_update_urb_state(hsotg, chan, chnum, urb,
  945. qtd);
  946. if (urb_xfer_done) {
  947. dwc2_host_complete(hsotg, qtd, urb->status);
  948. halt_status = DWC2_HC_XFER_URB_COMPLETE;
  949. } else {
  950. halt_status = DWC2_HC_XFER_COMPLETE;
  951. }
  952. dwc2_hcd_save_data_toggle(hsotg, chan, chnum, qtd);
  953. dwc2_complete_non_periodic_xfer(hsotg, chan, chnum, qtd,
  954. halt_status);
  955. break;
  956. case USB_ENDPOINT_XFER_INT:
  957. dev_vdbg(hsotg->dev, " Interrupt transfer complete\n");
  958. urb_xfer_done = dwc2_update_urb_state(hsotg, chan, chnum, urb,
  959. qtd);
  960. /*
  961. * Interrupt URB is done on the first transfer complete
  962. * interrupt
  963. */
  964. if (urb_xfer_done) {
  965. dwc2_host_complete(hsotg, qtd, urb->status);
  966. halt_status = DWC2_HC_XFER_URB_COMPLETE;
  967. } else {
  968. halt_status = DWC2_HC_XFER_COMPLETE;
  969. }
  970. dwc2_hcd_save_data_toggle(hsotg, chan, chnum, qtd);
  971. dwc2_complete_periodic_xfer(hsotg, chan, chnum, qtd,
  972. halt_status);
  973. break;
  974. case USB_ENDPOINT_XFER_ISOC:
  975. if (dbg_perio())
  976. dev_vdbg(hsotg->dev, " Isochronous transfer complete\n");
  977. if (qtd->isoc_split_pos == DWC2_HCSPLT_XACTPOS_ALL)
  978. halt_status = dwc2_update_isoc_urb_state(hsotg, chan,
  979. chnum, qtd,
  980. DWC2_HC_XFER_COMPLETE);
  981. dwc2_complete_periodic_xfer(hsotg, chan, chnum, qtd,
  982. halt_status);
  983. break;
  984. }
  985. handle_xfercomp_done:
  986. disable_hc_int(hsotg, chnum, HCINTMSK_XFERCOMPL);
  987. }
  988. /*
  989. * Handles a host channel STALL interrupt. This handler may be called in
  990. * either DMA mode or Slave mode.
  991. */
  992. static void dwc2_hc_stall_intr(struct dwc2_hsotg *hsotg,
  993. struct dwc2_host_chan *chan, int chnum,
  994. struct dwc2_qtd *qtd)
  995. {
  996. struct dwc2_hcd_urb *urb = qtd->urb;
  997. int pipe_type;
  998. dev_dbg(hsotg->dev, "--Host Channel %d Interrupt: STALL Received--\n",
  999. chnum);
  1000. if (hsotg->params.dma_desc_enable) {
  1001. dwc2_hcd_complete_xfer_ddma(hsotg, chan, chnum,
  1002. DWC2_HC_XFER_STALL);
  1003. goto handle_stall_done;
  1004. }
  1005. if (!urb)
  1006. goto handle_stall_halt;
  1007. pipe_type = dwc2_hcd_get_pipe_type(&urb->pipe_info);
  1008. if (pipe_type == USB_ENDPOINT_XFER_CONTROL)
  1009. dwc2_host_complete(hsotg, qtd, -EPIPE);
  1010. if (pipe_type == USB_ENDPOINT_XFER_BULK ||
  1011. pipe_type == USB_ENDPOINT_XFER_INT) {
  1012. dwc2_host_complete(hsotg, qtd, -EPIPE);
  1013. /*
  1014. * USB protocol requires resetting the data toggle for bulk
  1015. * and interrupt endpoints when a CLEAR_FEATURE(ENDPOINT_HALT)
  1016. * setup command is issued to the endpoint. Anticipate the
  1017. * CLEAR_FEATURE command since a STALL has occurred and reset
  1018. * the data toggle now.
  1019. */
  1020. chan->qh->data_toggle = 0;
  1021. }
  1022. handle_stall_halt:
  1023. dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_STALL);
  1024. handle_stall_done:
  1025. disable_hc_int(hsotg, chnum, HCINTMSK_STALL);
  1026. }
  1027. /*
  1028. * Updates the state of the URB when a transfer has been stopped due to an
  1029. * abnormal condition before the transfer completes. Modifies the
  1030. * actual_length field of the URB to reflect the number of bytes that have
  1031. * actually been transferred via the host channel.
  1032. */
  1033. static void dwc2_update_urb_state_abn(struct dwc2_hsotg *hsotg,
  1034. struct dwc2_host_chan *chan, int chnum,
  1035. struct dwc2_hcd_urb *urb,
  1036. struct dwc2_qtd *qtd,
  1037. enum dwc2_halt_status halt_status)
  1038. {
  1039. u32 xfer_length = dwc2_get_actual_xfer_length(hsotg, chan, chnum,
  1040. qtd, halt_status, NULL);
  1041. u32 hctsiz;
  1042. if (urb->actual_length + xfer_length > urb->length) {
  1043. dev_warn(hsotg->dev, "%s(): trimming xfer length\n", __func__);
  1044. xfer_length = urb->length - urb->actual_length;
  1045. }
  1046. urb->actual_length += xfer_length;
  1047. hctsiz = dwc2_readl(hsotg->regs + HCTSIZ(chnum));
  1048. dev_vdbg(hsotg->dev, "DWC_otg: %s: %s, channel %d\n",
  1049. __func__, (chan->ep_is_in ? "IN" : "OUT"), chnum);
  1050. dev_vdbg(hsotg->dev, " chan->start_pkt_count %d\n",
  1051. chan->start_pkt_count);
  1052. dev_vdbg(hsotg->dev, " hctsiz.pktcnt %d\n",
  1053. (hctsiz & TSIZ_PKTCNT_MASK) >> TSIZ_PKTCNT_SHIFT);
  1054. dev_vdbg(hsotg->dev, " chan->max_packet %d\n", chan->max_packet);
  1055. dev_vdbg(hsotg->dev, " bytes_transferred %d\n",
  1056. xfer_length);
  1057. dev_vdbg(hsotg->dev, " urb->actual_length %d\n",
  1058. urb->actual_length);
  1059. dev_vdbg(hsotg->dev, " urb->transfer_buffer_length %d\n",
  1060. urb->length);
  1061. }
  1062. /*
  1063. * Handles a host channel NAK interrupt. This handler may be called in either
  1064. * DMA mode or Slave mode.
  1065. */
  1066. static void dwc2_hc_nak_intr(struct dwc2_hsotg *hsotg,
  1067. struct dwc2_host_chan *chan, int chnum,
  1068. struct dwc2_qtd *qtd)
  1069. {
  1070. if (!qtd) {
  1071. dev_dbg(hsotg->dev, "%s: qtd is NULL\n", __func__);
  1072. return;
  1073. }
  1074. if (!qtd->urb) {
  1075. dev_dbg(hsotg->dev, "%s: qtd->urb is NULL\n", __func__);
  1076. return;
  1077. }
  1078. if (dbg_hc(chan))
  1079. dev_vdbg(hsotg->dev, "--Host Channel %d Interrupt: NAK Received--\n",
  1080. chnum);
  1081. /*
  1082. * Handle NAK for IN/OUT SSPLIT/CSPLIT transfers, bulk, control, and
  1083. * interrupt. Re-start the SSPLIT transfer.
  1084. *
  1085. * Normally for non-periodic transfers we'll retry right away, but to
  1086. * avoid interrupt storms we'll wait before retrying if we've got
  1087. * several NAKs. If we didn't do this we'd retry directly from the
  1088. * interrupt handler and could end up quickly getting another
  1089. * interrupt (another NAK), which we'd retry.
  1090. *
  1091. * Note that in DMA mode software only gets involved to re-send NAKed
  1092. * transfers for split transactions, so we only need to apply this
  1093. * delaying logic when handling splits. In non-DMA mode presumably we
  1094. * might want a similar delay if someone can demonstrate this problem
  1095. * affects that code path too.
  1096. */
  1097. if (chan->do_split) {
  1098. if (chan->complete_split)
  1099. qtd->error_count = 0;
  1100. qtd->complete_split = 0;
  1101. qtd->num_naks++;
  1102. qtd->qh->want_wait = qtd->num_naks >= DWC2_NAKS_BEFORE_DELAY;
  1103. dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_NAK);
  1104. goto handle_nak_done;
  1105. }
  1106. switch (dwc2_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
  1107. case USB_ENDPOINT_XFER_CONTROL:
  1108. case USB_ENDPOINT_XFER_BULK:
  1109. if (hsotg->params.host_dma && chan->ep_is_in) {
  1110. /*
  1111. * NAK interrupts are enabled on bulk/control IN
  1112. * transfers in DMA mode for the sole purpose of
  1113. * resetting the error count after a transaction error
  1114. * occurs. The core will continue transferring data.
  1115. */
  1116. qtd->error_count = 0;
  1117. break;
  1118. }
  1119. /*
  1120. * NAK interrupts normally occur during OUT transfers in DMA
  1121. * or Slave mode. For IN transfers, more requests will be
  1122. * queued as request queue space is available.
  1123. */
  1124. qtd->error_count = 0;
  1125. if (!chan->qh->ping_state) {
  1126. dwc2_update_urb_state_abn(hsotg, chan, chnum, qtd->urb,
  1127. qtd, DWC2_HC_XFER_NAK);
  1128. dwc2_hcd_save_data_toggle(hsotg, chan, chnum, qtd);
  1129. if (chan->speed == USB_SPEED_HIGH)
  1130. chan->qh->ping_state = 1;
  1131. }
  1132. /*
  1133. * Halt the channel so the transfer can be re-started from
  1134. * the appropriate point or the PING protocol will
  1135. * start/continue
  1136. */
  1137. dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_NAK);
  1138. break;
  1139. case USB_ENDPOINT_XFER_INT:
  1140. qtd->error_count = 0;
  1141. dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_NAK);
  1142. break;
  1143. case USB_ENDPOINT_XFER_ISOC:
  1144. /* Should never get called for isochronous transfers */
  1145. dev_err(hsotg->dev, "NACK interrupt for ISOC transfer\n");
  1146. break;
  1147. }
  1148. handle_nak_done:
  1149. disable_hc_int(hsotg, chnum, HCINTMSK_NAK);
  1150. }
  1151. /*
  1152. * Handles a host channel ACK interrupt. This interrupt is enabled when
  1153. * performing the PING protocol in Slave mode, when errors occur during
  1154. * either Slave mode or DMA mode, and during Start Split transactions.
  1155. */
  1156. static void dwc2_hc_ack_intr(struct dwc2_hsotg *hsotg,
  1157. struct dwc2_host_chan *chan, int chnum,
  1158. struct dwc2_qtd *qtd)
  1159. {
  1160. struct dwc2_hcd_iso_packet_desc *frame_desc;
  1161. if (dbg_hc(chan))
  1162. dev_vdbg(hsotg->dev, "--Host Channel %d Interrupt: ACK Received--\n",
  1163. chnum);
  1164. if (chan->do_split) {
  1165. /* Handle ACK on SSPLIT. ACK should not occur in CSPLIT. */
  1166. if (!chan->ep_is_in &&
  1167. chan->data_pid_start != DWC2_HC_PID_SETUP)
  1168. qtd->ssplit_out_xfer_count = chan->xfer_len;
  1169. if (chan->ep_type != USB_ENDPOINT_XFER_ISOC || chan->ep_is_in) {
  1170. qtd->complete_split = 1;
  1171. dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_ACK);
  1172. } else {
  1173. /* ISOC OUT */
  1174. switch (chan->xact_pos) {
  1175. case DWC2_HCSPLT_XACTPOS_ALL:
  1176. break;
  1177. case DWC2_HCSPLT_XACTPOS_END:
  1178. qtd->isoc_split_pos = DWC2_HCSPLT_XACTPOS_ALL;
  1179. qtd->isoc_split_offset = 0;
  1180. break;
  1181. case DWC2_HCSPLT_XACTPOS_BEGIN:
  1182. case DWC2_HCSPLT_XACTPOS_MID:
  1183. /*
  1184. * For BEGIN or MID, calculate the length for
  1185. * the next microframe to determine the correct
  1186. * SSPLIT token, either MID or END
  1187. */
  1188. frame_desc = &qtd->urb->iso_descs[
  1189. qtd->isoc_frame_index];
  1190. qtd->isoc_split_offset += 188;
  1191. if (frame_desc->length - qtd->isoc_split_offset
  1192. <= 188)
  1193. qtd->isoc_split_pos =
  1194. DWC2_HCSPLT_XACTPOS_END;
  1195. else
  1196. qtd->isoc_split_pos =
  1197. DWC2_HCSPLT_XACTPOS_MID;
  1198. break;
  1199. }
  1200. }
  1201. } else {
  1202. qtd->error_count = 0;
  1203. if (chan->qh->ping_state) {
  1204. chan->qh->ping_state = 0;
  1205. /*
  1206. * Halt the channel so the transfer can be re-started
  1207. * from the appropriate point. This only happens in
  1208. * Slave mode. In DMA mode, the ping_state is cleared
  1209. * when the transfer is started because the core
  1210. * automatically executes the PING, then the transfer.
  1211. */
  1212. dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_ACK);
  1213. }
  1214. }
  1215. /*
  1216. * If the ACK occurred when _not_ in the PING state, let the channel
  1217. * continue transferring data after clearing the error count
  1218. */
  1219. disable_hc_int(hsotg, chnum, HCINTMSK_ACK);
  1220. }
  1221. /*
  1222. * Handles a host channel NYET interrupt. This interrupt should only occur on
  1223. * Bulk and Control OUT endpoints and for complete split transactions. If a
  1224. * NYET occurs at the same time as a Transfer Complete interrupt, it is
  1225. * handled in the xfercomp interrupt handler, not here. This handler may be
  1226. * called in either DMA mode or Slave mode.
  1227. */
  1228. static void dwc2_hc_nyet_intr(struct dwc2_hsotg *hsotg,
  1229. struct dwc2_host_chan *chan, int chnum,
  1230. struct dwc2_qtd *qtd)
  1231. {
  1232. if (dbg_hc(chan))
  1233. dev_vdbg(hsotg->dev, "--Host Channel %d Interrupt: NYET Received--\n",
  1234. chnum);
  1235. /*
  1236. * NYET on CSPLIT
  1237. * re-do the CSPLIT immediately on non-periodic
  1238. */
  1239. if (chan->do_split && chan->complete_split) {
  1240. if (chan->ep_is_in && chan->ep_type == USB_ENDPOINT_XFER_ISOC &&
  1241. hsotg->params.host_dma) {
  1242. qtd->complete_split = 0;
  1243. qtd->isoc_split_offset = 0;
  1244. qtd->isoc_frame_index++;
  1245. if (qtd->urb &&
  1246. qtd->isoc_frame_index == qtd->urb->packet_count) {
  1247. dwc2_host_complete(hsotg, qtd, 0);
  1248. dwc2_release_channel(hsotg, chan, qtd,
  1249. DWC2_HC_XFER_URB_COMPLETE);
  1250. } else {
  1251. dwc2_release_channel(hsotg, chan, qtd,
  1252. DWC2_HC_XFER_NO_HALT_STATUS);
  1253. }
  1254. goto handle_nyet_done;
  1255. }
  1256. if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
  1257. chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
  1258. struct dwc2_qh *qh = chan->qh;
  1259. bool past_end;
  1260. if (!hsotg->params.uframe_sched) {
  1261. int frnum = dwc2_hcd_get_frame_number(hsotg);
  1262. /* Don't have num_hs_transfers; simple logic */
  1263. past_end = dwc2_full_frame_num(frnum) !=
  1264. dwc2_full_frame_num(qh->next_active_frame);
  1265. } else {
  1266. int end_frnum;
  1267. /*
  1268. * Figure out the end frame based on
  1269. * schedule.
  1270. *
  1271. * We don't want to go on trying again
  1272. * and again forever. Let's stop when
  1273. * we've done all the transfers that
  1274. * were scheduled.
  1275. *
  1276. * We're going to be comparing
  1277. * start_active_frame and
  1278. * next_active_frame, both of which
  1279. * are 1 before the time the packet
  1280. * goes on the wire, so that cancels
  1281. * out. Basically if had 1 transfer
  1282. * and we saw 1 NYET then we're done.
  1283. * We're getting a NYET here so if
  1284. * next >= (start + num_transfers)
  1285. * we're done. The complexity is that
  1286. * for all but ISOC_OUT we skip one
  1287. * slot.
  1288. */
  1289. end_frnum = dwc2_frame_num_inc(
  1290. qh->start_active_frame,
  1291. qh->num_hs_transfers);
  1292. if (qh->ep_type != USB_ENDPOINT_XFER_ISOC ||
  1293. qh->ep_is_in)
  1294. end_frnum =
  1295. dwc2_frame_num_inc(end_frnum, 1);
  1296. past_end = dwc2_frame_num_le(
  1297. end_frnum, qh->next_active_frame);
  1298. }
  1299. if (past_end) {
  1300. /* Treat this as a transaction error. */
  1301. #if 0
  1302. /*
  1303. * Todo: Fix system performance so this can
  1304. * be treated as an error. Right now complete
  1305. * splits cannot be scheduled precisely enough
  1306. * due to other system activity, so this error
  1307. * occurs regularly in Slave mode.
  1308. */
  1309. qtd->error_count++;
  1310. #endif
  1311. qtd->complete_split = 0;
  1312. dwc2_halt_channel(hsotg, chan, qtd,
  1313. DWC2_HC_XFER_XACT_ERR);
  1314. /* Todo: add support for isoc release */
  1315. goto handle_nyet_done;
  1316. }
  1317. }
  1318. dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_NYET);
  1319. goto handle_nyet_done;
  1320. }
  1321. chan->qh->ping_state = 1;
  1322. qtd->error_count = 0;
  1323. dwc2_update_urb_state_abn(hsotg, chan, chnum, qtd->urb, qtd,
  1324. DWC2_HC_XFER_NYET);
  1325. dwc2_hcd_save_data_toggle(hsotg, chan, chnum, qtd);
  1326. /*
  1327. * Halt the channel and re-start the transfer so the PING protocol
  1328. * will start
  1329. */
  1330. dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_NYET);
  1331. handle_nyet_done:
  1332. disable_hc_int(hsotg, chnum, HCINTMSK_NYET);
  1333. }
  1334. /*
  1335. * Handles a host channel babble interrupt. This handler may be called in
  1336. * either DMA mode or Slave mode.
  1337. */
  1338. static void dwc2_hc_babble_intr(struct dwc2_hsotg *hsotg,
  1339. struct dwc2_host_chan *chan, int chnum,
  1340. struct dwc2_qtd *qtd)
  1341. {
  1342. dev_dbg(hsotg->dev, "--Host Channel %d Interrupt: Babble Error--\n",
  1343. chnum);
  1344. dwc2_hc_handle_tt_clear(hsotg, chan, qtd);
  1345. if (hsotg->params.dma_desc_enable) {
  1346. dwc2_hcd_complete_xfer_ddma(hsotg, chan, chnum,
  1347. DWC2_HC_XFER_BABBLE_ERR);
  1348. goto disable_int;
  1349. }
  1350. if (chan->ep_type != USB_ENDPOINT_XFER_ISOC) {
  1351. dwc2_host_complete(hsotg, qtd, -EOVERFLOW);
  1352. dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_BABBLE_ERR);
  1353. } else {
  1354. enum dwc2_halt_status halt_status;
  1355. halt_status = dwc2_update_isoc_urb_state(hsotg, chan, chnum,
  1356. qtd, DWC2_HC_XFER_BABBLE_ERR);
  1357. dwc2_halt_channel(hsotg, chan, qtd, halt_status);
  1358. }
  1359. disable_int:
  1360. disable_hc_int(hsotg, chnum, HCINTMSK_BBLERR);
  1361. }
  1362. /*
  1363. * Handles a host channel AHB error interrupt. This handler is only called in
  1364. * DMA mode.
  1365. */
  1366. static void dwc2_hc_ahberr_intr(struct dwc2_hsotg *hsotg,
  1367. struct dwc2_host_chan *chan, int chnum,
  1368. struct dwc2_qtd *qtd)
  1369. {
  1370. struct dwc2_hcd_urb *urb = qtd->urb;
  1371. char *pipetype, *speed;
  1372. u32 hcchar;
  1373. u32 hcsplt;
  1374. u32 hctsiz;
  1375. u32 hc_dma;
  1376. dev_dbg(hsotg->dev, "--Host Channel %d Interrupt: AHB Error--\n",
  1377. chnum);
  1378. if (!urb)
  1379. goto handle_ahberr_halt;
  1380. dwc2_hc_handle_tt_clear(hsotg, chan, qtd);
  1381. hcchar = dwc2_readl(hsotg->regs + HCCHAR(chnum));
  1382. hcsplt = dwc2_readl(hsotg->regs + HCSPLT(chnum));
  1383. hctsiz = dwc2_readl(hsotg->regs + HCTSIZ(chnum));
  1384. hc_dma = dwc2_readl(hsotg->regs + HCDMA(chnum));
  1385. dev_err(hsotg->dev, "AHB ERROR, Channel %d\n", chnum);
  1386. dev_err(hsotg->dev, " hcchar 0x%08x, hcsplt 0x%08x\n", hcchar, hcsplt);
  1387. dev_err(hsotg->dev, " hctsiz 0x%08x, hc_dma 0x%08x\n", hctsiz, hc_dma);
  1388. dev_err(hsotg->dev, " Device address: %d\n",
  1389. dwc2_hcd_get_dev_addr(&urb->pipe_info));
  1390. dev_err(hsotg->dev, " Endpoint: %d, %s\n",
  1391. dwc2_hcd_get_ep_num(&urb->pipe_info),
  1392. dwc2_hcd_is_pipe_in(&urb->pipe_info) ? "IN" : "OUT");
  1393. switch (dwc2_hcd_get_pipe_type(&urb->pipe_info)) {
  1394. case USB_ENDPOINT_XFER_CONTROL:
  1395. pipetype = "CONTROL";
  1396. break;
  1397. case USB_ENDPOINT_XFER_BULK:
  1398. pipetype = "BULK";
  1399. break;
  1400. case USB_ENDPOINT_XFER_INT:
  1401. pipetype = "INTERRUPT";
  1402. break;
  1403. case USB_ENDPOINT_XFER_ISOC:
  1404. pipetype = "ISOCHRONOUS";
  1405. break;
  1406. default:
  1407. pipetype = "UNKNOWN";
  1408. break;
  1409. }
  1410. dev_err(hsotg->dev, " Endpoint type: %s\n", pipetype);
  1411. switch (chan->speed) {
  1412. case USB_SPEED_HIGH:
  1413. speed = "HIGH";
  1414. break;
  1415. case USB_SPEED_FULL:
  1416. speed = "FULL";
  1417. break;
  1418. case USB_SPEED_LOW:
  1419. speed = "LOW";
  1420. break;
  1421. default:
  1422. speed = "UNKNOWN";
  1423. break;
  1424. }
  1425. dev_err(hsotg->dev, " Speed: %s\n", speed);
  1426. dev_err(hsotg->dev, " Max packet size: %d\n",
  1427. dwc2_hcd_get_mps(&urb->pipe_info));
  1428. dev_err(hsotg->dev, " Data buffer length: %d\n", urb->length);
  1429. dev_err(hsotg->dev, " Transfer buffer: %p, Transfer DMA: %08lx\n",
  1430. urb->buf, (unsigned long)urb->dma);
  1431. dev_err(hsotg->dev, " Setup buffer: %p, Setup DMA: %08lx\n",
  1432. urb->setup_packet, (unsigned long)urb->setup_dma);
  1433. dev_err(hsotg->dev, " Interval: %d\n", urb->interval);
  1434. /* Core halts the channel for Descriptor DMA mode */
  1435. if (hsotg->params.dma_desc_enable) {
  1436. dwc2_hcd_complete_xfer_ddma(hsotg, chan, chnum,
  1437. DWC2_HC_XFER_AHB_ERR);
  1438. goto handle_ahberr_done;
  1439. }
  1440. dwc2_host_complete(hsotg, qtd, -EIO);
  1441. handle_ahberr_halt:
  1442. /*
  1443. * Force a channel halt. Don't call dwc2_halt_channel because that won't
  1444. * write to the HCCHARn register in DMA mode to force the halt.
  1445. */
  1446. dwc2_hc_halt(hsotg, chan, DWC2_HC_XFER_AHB_ERR);
  1447. handle_ahberr_done:
  1448. disable_hc_int(hsotg, chnum, HCINTMSK_AHBERR);
  1449. }
  1450. /*
  1451. * Handles a host channel transaction error interrupt. This handler may be
  1452. * called in either DMA mode or Slave mode.
  1453. */
  1454. static void dwc2_hc_xacterr_intr(struct dwc2_hsotg *hsotg,
  1455. struct dwc2_host_chan *chan, int chnum,
  1456. struct dwc2_qtd *qtd)
  1457. {
  1458. dev_dbg(hsotg->dev,
  1459. "--Host Channel %d Interrupt: Transaction Error--\n", chnum);
  1460. dwc2_hc_handle_tt_clear(hsotg, chan, qtd);
  1461. if (hsotg->params.dma_desc_enable) {
  1462. dwc2_hcd_complete_xfer_ddma(hsotg, chan, chnum,
  1463. DWC2_HC_XFER_XACT_ERR);
  1464. goto handle_xacterr_done;
  1465. }
  1466. switch (dwc2_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
  1467. case USB_ENDPOINT_XFER_CONTROL:
  1468. case USB_ENDPOINT_XFER_BULK:
  1469. qtd->error_count++;
  1470. if (!chan->qh->ping_state) {
  1471. dwc2_update_urb_state_abn(hsotg, chan, chnum, qtd->urb,
  1472. qtd, DWC2_HC_XFER_XACT_ERR);
  1473. dwc2_hcd_save_data_toggle(hsotg, chan, chnum, qtd);
  1474. if (!chan->ep_is_in && chan->speed == USB_SPEED_HIGH)
  1475. chan->qh->ping_state = 1;
  1476. }
  1477. /*
  1478. * Halt the channel so the transfer can be re-started from
  1479. * the appropriate point or the PING protocol will start
  1480. */
  1481. dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_XACT_ERR);
  1482. break;
  1483. case USB_ENDPOINT_XFER_INT:
  1484. qtd->error_count++;
  1485. if (chan->do_split && chan->complete_split)
  1486. qtd->complete_split = 0;
  1487. dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_XACT_ERR);
  1488. break;
  1489. case USB_ENDPOINT_XFER_ISOC:
  1490. {
  1491. enum dwc2_halt_status halt_status;
  1492. halt_status = dwc2_update_isoc_urb_state(hsotg, chan,
  1493. chnum, qtd, DWC2_HC_XFER_XACT_ERR);
  1494. dwc2_halt_channel(hsotg, chan, qtd, halt_status);
  1495. }
  1496. break;
  1497. }
  1498. handle_xacterr_done:
  1499. disable_hc_int(hsotg, chnum, HCINTMSK_XACTERR);
  1500. }
  1501. /*
  1502. * Handles a host channel frame overrun interrupt. This handler may be called
  1503. * in either DMA mode or Slave mode.
  1504. */
  1505. static void dwc2_hc_frmovrun_intr(struct dwc2_hsotg *hsotg,
  1506. struct dwc2_host_chan *chan, int chnum,
  1507. struct dwc2_qtd *qtd)
  1508. {
  1509. enum dwc2_halt_status halt_status;
  1510. if (dbg_hc(chan))
  1511. dev_dbg(hsotg->dev, "--Host Channel %d Interrupt: Frame Overrun--\n",
  1512. chnum);
  1513. dwc2_hc_handle_tt_clear(hsotg, chan, qtd);
  1514. switch (dwc2_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
  1515. case USB_ENDPOINT_XFER_CONTROL:
  1516. case USB_ENDPOINT_XFER_BULK:
  1517. break;
  1518. case USB_ENDPOINT_XFER_INT:
  1519. dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_FRAME_OVERRUN);
  1520. break;
  1521. case USB_ENDPOINT_XFER_ISOC:
  1522. halt_status = dwc2_update_isoc_urb_state(hsotg, chan, chnum,
  1523. qtd, DWC2_HC_XFER_FRAME_OVERRUN);
  1524. dwc2_halt_channel(hsotg, chan, qtd, halt_status);
  1525. break;
  1526. }
  1527. disable_hc_int(hsotg, chnum, HCINTMSK_FRMOVRUN);
  1528. }
  1529. /*
  1530. * Handles a host channel data toggle error interrupt. This handler may be
  1531. * called in either DMA mode or Slave mode.
  1532. */
  1533. static void dwc2_hc_datatglerr_intr(struct dwc2_hsotg *hsotg,
  1534. struct dwc2_host_chan *chan, int chnum,
  1535. struct dwc2_qtd *qtd)
  1536. {
  1537. dev_dbg(hsotg->dev,
  1538. "--Host Channel %d Interrupt: Data Toggle Error--\n", chnum);
  1539. if (chan->ep_is_in)
  1540. qtd->error_count = 0;
  1541. else
  1542. dev_err(hsotg->dev,
  1543. "Data Toggle Error on OUT transfer, channel %d\n",
  1544. chnum);
  1545. dwc2_hc_handle_tt_clear(hsotg, chan, qtd);
  1546. disable_hc_int(hsotg, chnum, HCINTMSK_DATATGLERR);
  1547. }
  1548. /*
  1549. * For debug only. It checks that a valid halt status is set and that
  1550. * HCCHARn.chdis is clear. If there's a problem, corrective action is
  1551. * taken and a warning is issued.
  1552. *
  1553. * Return: true if halt status is ok, false otherwise
  1554. */
  1555. static bool dwc2_halt_status_ok(struct dwc2_hsotg *hsotg,
  1556. struct dwc2_host_chan *chan, int chnum,
  1557. struct dwc2_qtd *qtd)
  1558. {
  1559. #ifdef DEBUG
  1560. u32 hcchar;
  1561. u32 hctsiz;
  1562. u32 hcintmsk;
  1563. u32 hcsplt;
  1564. if (chan->halt_status == DWC2_HC_XFER_NO_HALT_STATUS) {
  1565. /*
  1566. * This code is here only as a check. This condition should
  1567. * never happen. Ignore the halt if it does occur.
  1568. */
  1569. hcchar = dwc2_readl(hsotg->regs + HCCHAR(chnum));
  1570. hctsiz = dwc2_readl(hsotg->regs + HCTSIZ(chnum));
  1571. hcintmsk = dwc2_readl(hsotg->regs + HCINTMSK(chnum));
  1572. hcsplt = dwc2_readl(hsotg->regs + HCSPLT(chnum));
  1573. dev_dbg(hsotg->dev,
  1574. "%s: chan->halt_status DWC2_HC_XFER_NO_HALT_STATUS,\n",
  1575. __func__);
  1576. dev_dbg(hsotg->dev,
  1577. "channel %d, hcchar 0x%08x, hctsiz 0x%08x,\n",
  1578. chnum, hcchar, hctsiz);
  1579. dev_dbg(hsotg->dev,
  1580. "hcint 0x%08x, hcintmsk 0x%08x, hcsplt 0x%08x,\n",
  1581. chan->hcint, hcintmsk, hcsplt);
  1582. if (qtd)
  1583. dev_dbg(hsotg->dev, "qtd->complete_split %d\n",
  1584. qtd->complete_split);
  1585. dev_warn(hsotg->dev,
  1586. "%s: no halt status, channel %d, ignoring interrupt\n",
  1587. __func__, chnum);
  1588. return false;
  1589. }
  1590. /*
  1591. * This code is here only as a check. hcchar.chdis should never be set
  1592. * when the halt interrupt occurs. Halt the channel again if it does
  1593. * occur.
  1594. */
  1595. hcchar = dwc2_readl(hsotg->regs + HCCHAR(chnum));
  1596. if (hcchar & HCCHAR_CHDIS) {
  1597. dev_warn(hsotg->dev,
  1598. "%s: hcchar.chdis set unexpectedly, hcchar 0x%08x, trying to halt again\n",
  1599. __func__, hcchar);
  1600. chan->halt_pending = 0;
  1601. dwc2_halt_channel(hsotg, chan, qtd, chan->halt_status);
  1602. return false;
  1603. }
  1604. #endif
  1605. return true;
  1606. }
  1607. /*
  1608. * Handles a host Channel Halted interrupt in DMA mode. This handler
  1609. * determines the reason the channel halted and proceeds accordingly.
  1610. */
  1611. static void dwc2_hc_chhltd_intr_dma(struct dwc2_hsotg *hsotg,
  1612. struct dwc2_host_chan *chan, int chnum,
  1613. struct dwc2_qtd *qtd)
  1614. {
  1615. u32 hcintmsk;
  1616. int out_nak_enh = 0;
  1617. if (dbg_hc(chan))
  1618. dev_vdbg(hsotg->dev,
  1619. "--Host Channel %d Interrupt: DMA Channel Halted--\n",
  1620. chnum);
  1621. /*
  1622. * For core with OUT NAK enhancement, the flow for high-speed
  1623. * CONTROL/BULK OUT is handled a little differently
  1624. */
  1625. if (hsotg->hw_params.snpsid >= DWC2_CORE_REV_2_71a) {
  1626. if (chan->speed == USB_SPEED_HIGH && !chan->ep_is_in &&
  1627. (chan->ep_type == USB_ENDPOINT_XFER_CONTROL ||
  1628. chan->ep_type == USB_ENDPOINT_XFER_BULK)) {
  1629. out_nak_enh = 1;
  1630. }
  1631. }
  1632. if (chan->halt_status == DWC2_HC_XFER_URB_DEQUEUE ||
  1633. (chan->halt_status == DWC2_HC_XFER_AHB_ERR &&
  1634. !hsotg->params.dma_desc_enable)) {
  1635. if (hsotg->params.dma_desc_enable)
  1636. dwc2_hcd_complete_xfer_ddma(hsotg, chan, chnum,
  1637. chan->halt_status);
  1638. else
  1639. /*
  1640. * Just release the channel. A dequeue can happen on a
  1641. * transfer timeout. In the case of an AHB Error, the
  1642. * channel was forced to halt because there's no way to
  1643. * gracefully recover.
  1644. */
  1645. dwc2_release_channel(hsotg, chan, qtd,
  1646. chan->halt_status);
  1647. return;
  1648. }
  1649. hcintmsk = dwc2_readl(hsotg->regs + HCINTMSK(chnum));
  1650. if (chan->hcint & HCINTMSK_XFERCOMPL) {
  1651. /*
  1652. * Todo: This is here because of a possible hardware bug. Spec
  1653. * says that on SPLIT-ISOC OUT transfers in DMA mode that a HALT
  1654. * interrupt w/ACK bit set should occur, but I only see the
  1655. * XFERCOMP bit, even with it masked out. This is a workaround
  1656. * for that behavior. Should fix this when hardware is fixed.
  1657. */
  1658. if (chan->ep_type == USB_ENDPOINT_XFER_ISOC && !chan->ep_is_in)
  1659. dwc2_hc_ack_intr(hsotg, chan, chnum, qtd);
  1660. dwc2_hc_xfercomp_intr(hsotg, chan, chnum, qtd);
  1661. } else if (chan->hcint & HCINTMSK_STALL) {
  1662. dwc2_hc_stall_intr(hsotg, chan, chnum, qtd);
  1663. } else if ((chan->hcint & HCINTMSK_XACTERR) &&
  1664. !hsotg->params.dma_desc_enable) {
  1665. if (out_nak_enh) {
  1666. if (chan->hcint &
  1667. (HCINTMSK_NYET | HCINTMSK_NAK | HCINTMSK_ACK)) {
  1668. dev_vdbg(hsotg->dev,
  1669. "XactErr with NYET/NAK/ACK\n");
  1670. qtd->error_count = 0;
  1671. } else {
  1672. dev_vdbg(hsotg->dev,
  1673. "XactErr without NYET/NAK/ACK\n");
  1674. }
  1675. }
  1676. /*
  1677. * Must handle xacterr before nak or ack. Could get a xacterr
  1678. * at the same time as either of these on a BULK/CONTROL OUT
  1679. * that started with a PING. The xacterr takes precedence.
  1680. */
  1681. dwc2_hc_xacterr_intr(hsotg, chan, chnum, qtd);
  1682. } else if ((chan->hcint & HCINTMSK_XCS_XACT) &&
  1683. hsotg->params.dma_desc_enable) {
  1684. dwc2_hc_xacterr_intr(hsotg, chan, chnum, qtd);
  1685. } else if ((chan->hcint & HCINTMSK_AHBERR) &&
  1686. hsotg->params.dma_desc_enable) {
  1687. dwc2_hc_ahberr_intr(hsotg, chan, chnum, qtd);
  1688. } else if (chan->hcint & HCINTMSK_BBLERR) {
  1689. dwc2_hc_babble_intr(hsotg, chan, chnum, qtd);
  1690. } else if (chan->hcint & HCINTMSK_FRMOVRUN) {
  1691. dwc2_hc_frmovrun_intr(hsotg, chan, chnum, qtd);
  1692. } else if (!out_nak_enh) {
  1693. if (chan->hcint & HCINTMSK_NYET) {
  1694. /*
  1695. * Must handle nyet before nak or ack. Could get a nyet
  1696. * at the same time as either of those on a BULK/CONTROL
  1697. * OUT that started with a PING. The nyet takes
  1698. * precedence.
  1699. */
  1700. dwc2_hc_nyet_intr(hsotg, chan, chnum, qtd);
  1701. } else if ((chan->hcint & HCINTMSK_NAK) &&
  1702. !(hcintmsk & HCINTMSK_NAK)) {
  1703. /*
  1704. * If nak is not masked, it's because a non-split IN
  1705. * transfer is in an error state. In that case, the nak
  1706. * is handled by the nak interrupt handler, not here.
  1707. * Handle nak here for BULK/CONTROL OUT transfers, which
  1708. * halt on a NAK to allow rewinding the buffer pointer.
  1709. */
  1710. dwc2_hc_nak_intr(hsotg, chan, chnum, qtd);
  1711. } else if ((chan->hcint & HCINTMSK_ACK) &&
  1712. !(hcintmsk & HCINTMSK_ACK)) {
  1713. /*
  1714. * If ack is not masked, it's because a non-split IN
  1715. * transfer is in an error state. In that case, the ack
  1716. * is handled by the ack interrupt handler, not here.
  1717. * Handle ack here for split transfers. Start splits
  1718. * halt on ACK.
  1719. */
  1720. dwc2_hc_ack_intr(hsotg, chan, chnum, qtd);
  1721. } else {
  1722. if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
  1723. chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
  1724. /*
  1725. * A periodic transfer halted with no other
  1726. * channel interrupts set. Assume it was halted
  1727. * by the core because it could not be completed
  1728. * in its scheduled (micro)frame.
  1729. */
  1730. dev_dbg(hsotg->dev,
  1731. "%s: Halt channel %d (assume incomplete periodic transfer)\n",
  1732. __func__, chnum);
  1733. dwc2_halt_channel(hsotg, chan, qtd,
  1734. DWC2_HC_XFER_PERIODIC_INCOMPLETE);
  1735. } else {
  1736. dev_err(hsotg->dev,
  1737. "%s: Channel %d - ChHltd set, but reason is unknown\n",
  1738. __func__, chnum);
  1739. dev_err(hsotg->dev,
  1740. "hcint 0x%08x, intsts 0x%08x\n",
  1741. chan->hcint,
  1742. dwc2_readl(hsotg->regs + GINTSTS));
  1743. goto error;
  1744. }
  1745. }
  1746. } else {
  1747. dev_info(hsotg->dev,
  1748. "NYET/NAK/ACK/other in non-error case, 0x%08x\n",
  1749. chan->hcint);
  1750. error:
  1751. /* Failthrough: use 3-strikes rule */
  1752. qtd->error_count++;
  1753. dwc2_update_urb_state_abn(hsotg, chan, chnum, qtd->urb,
  1754. qtd, DWC2_HC_XFER_XACT_ERR);
  1755. dwc2_hcd_save_data_toggle(hsotg, chan, chnum, qtd);
  1756. dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_XACT_ERR);
  1757. }
  1758. }
  1759. /*
  1760. * Handles a host channel Channel Halted interrupt
  1761. *
  1762. * In slave mode, this handler is called only when the driver specifically
  1763. * requests a halt. This occurs during handling other host channel interrupts
  1764. * (e.g. nak, xacterr, stall, nyet, etc.).
  1765. *
  1766. * In DMA mode, this is the interrupt that occurs when the core has finished
  1767. * processing a transfer on a channel. Other host channel interrupts (except
  1768. * ahberr) are disabled in DMA mode.
  1769. */
  1770. static void dwc2_hc_chhltd_intr(struct dwc2_hsotg *hsotg,
  1771. struct dwc2_host_chan *chan, int chnum,
  1772. struct dwc2_qtd *qtd)
  1773. {
  1774. if (dbg_hc(chan))
  1775. dev_vdbg(hsotg->dev, "--Host Channel %d Interrupt: Channel Halted--\n",
  1776. chnum);
  1777. if (hsotg->params.host_dma) {
  1778. dwc2_hc_chhltd_intr_dma(hsotg, chan, chnum, qtd);
  1779. } else {
  1780. if (!dwc2_halt_status_ok(hsotg, chan, chnum, qtd))
  1781. return;
  1782. dwc2_release_channel(hsotg, chan, qtd, chan->halt_status);
  1783. }
  1784. }
  1785. /*
  1786. * Check if the given qtd is still the top of the list (and thus valid).
  1787. *
  1788. * If dwc2_hcd_qtd_unlink_and_free() has been called since we grabbed
  1789. * the qtd from the top of the list, this will return false (otherwise true).
  1790. */
  1791. static bool dwc2_check_qtd_still_ok(struct dwc2_qtd *qtd, struct dwc2_qh *qh)
  1792. {
  1793. struct dwc2_qtd *cur_head;
  1794. if (!qh)
  1795. return false;
  1796. cur_head = list_first_entry(&qh->qtd_list, struct dwc2_qtd,
  1797. qtd_list_entry);
  1798. return (cur_head == qtd);
  1799. }
  1800. /* Handles interrupt for a specific Host Channel */
  1801. static void dwc2_hc_n_intr(struct dwc2_hsotg *hsotg, int chnum)
  1802. {
  1803. struct dwc2_qtd *qtd;
  1804. struct dwc2_host_chan *chan;
  1805. u32 hcint, hcintmsk;
  1806. chan = hsotg->hc_ptr_array[chnum];
  1807. hcint = dwc2_readl(hsotg->regs + HCINT(chnum));
  1808. hcintmsk = dwc2_readl(hsotg->regs + HCINTMSK(chnum));
  1809. if (!chan) {
  1810. dev_err(hsotg->dev, "## hc_ptr_array for channel is NULL ##\n");
  1811. dwc2_writel(hcint, hsotg->regs + HCINT(chnum));
  1812. return;
  1813. }
  1814. if (dbg_hc(chan)) {
  1815. dev_vdbg(hsotg->dev, "--Host Channel Interrupt--, Channel %d\n",
  1816. chnum);
  1817. dev_vdbg(hsotg->dev,
  1818. " hcint 0x%08x, hcintmsk 0x%08x, hcint&hcintmsk 0x%08x\n",
  1819. hcint, hcintmsk, hcint & hcintmsk);
  1820. }
  1821. dwc2_writel(hcint, hsotg->regs + HCINT(chnum));
  1822. /*
  1823. * If we got an interrupt after someone called
  1824. * dwc2_hcd_endpoint_disable() we don't want to crash below
  1825. */
  1826. if (!chan->qh) {
  1827. dev_warn(hsotg->dev, "Interrupt on disabled channel\n");
  1828. return;
  1829. }
  1830. chan->hcint = hcint;
  1831. hcint &= hcintmsk;
  1832. /*
  1833. * If the channel was halted due to a dequeue, the qtd list might
  1834. * be empty or at least the first entry will not be the active qtd.
  1835. * In this case, take a shortcut and just release the channel.
  1836. */
  1837. if (chan->halt_status == DWC2_HC_XFER_URB_DEQUEUE) {
  1838. /*
  1839. * If the channel was halted, this should be the only
  1840. * interrupt unmasked
  1841. */
  1842. WARN_ON(hcint != HCINTMSK_CHHLTD);
  1843. if (hsotg->params.dma_desc_enable)
  1844. dwc2_hcd_complete_xfer_ddma(hsotg, chan, chnum,
  1845. chan->halt_status);
  1846. else
  1847. dwc2_release_channel(hsotg, chan, NULL,
  1848. chan->halt_status);
  1849. return;
  1850. }
  1851. if (list_empty(&chan->qh->qtd_list)) {
  1852. /*
  1853. * TODO: Will this ever happen with the
  1854. * DWC2_HC_XFER_URB_DEQUEUE handling above?
  1855. */
  1856. dev_dbg(hsotg->dev, "## no QTD queued for channel %d ##\n",
  1857. chnum);
  1858. dev_dbg(hsotg->dev,
  1859. " hcint 0x%08x, hcintmsk 0x%08x, hcint&hcintmsk 0x%08x\n",
  1860. chan->hcint, hcintmsk, hcint);
  1861. chan->halt_status = DWC2_HC_XFER_NO_HALT_STATUS;
  1862. disable_hc_int(hsotg, chnum, HCINTMSK_CHHLTD);
  1863. chan->hcint = 0;
  1864. return;
  1865. }
  1866. qtd = list_first_entry(&chan->qh->qtd_list, struct dwc2_qtd,
  1867. qtd_list_entry);
  1868. if (!hsotg->params.host_dma) {
  1869. if ((hcint & HCINTMSK_CHHLTD) && hcint != HCINTMSK_CHHLTD)
  1870. hcint &= ~HCINTMSK_CHHLTD;
  1871. }
  1872. if (hcint & HCINTMSK_XFERCOMPL) {
  1873. dwc2_hc_xfercomp_intr(hsotg, chan, chnum, qtd);
  1874. /*
  1875. * If NYET occurred at same time as Xfer Complete, the NYET is
  1876. * handled by the Xfer Complete interrupt handler. Don't want
  1877. * to call the NYET interrupt handler in this case.
  1878. */
  1879. hcint &= ~HCINTMSK_NYET;
  1880. }
  1881. if (hcint & HCINTMSK_CHHLTD) {
  1882. dwc2_hc_chhltd_intr(hsotg, chan, chnum, qtd);
  1883. if (!dwc2_check_qtd_still_ok(qtd, chan->qh))
  1884. goto exit;
  1885. }
  1886. if (hcint & HCINTMSK_AHBERR) {
  1887. dwc2_hc_ahberr_intr(hsotg, chan, chnum, qtd);
  1888. if (!dwc2_check_qtd_still_ok(qtd, chan->qh))
  1889. goto exit;
  1890. }
  1891. if (hcint & HCINTMSK_STALL) {
  1892. dwc2_hc_stall_intr(hsotg, chan, chnum, qtd);
  1893. if (!dwc2_check_qtd_still_ok(qtd, chan->qh))
  1894. goto exit;
  1895. }
  1896. if (hcint & HCINTMSK_NAK) {
  1897. dwc2_hc_nak_intr(hsotg, chan, chnum, qtd);
  1898. if (!dwc2_check_qtd_still_ok(qtd, chan->qh))
  1899. goto exit;
  1900. }
  1901. if (hcint & HCINTMSK_ACK) {
  1902. dwc2_hc_ack_intr(hsotg, chan, chnum, qtd);
  1903. if (!dwc2_check_qtd_still_ok(qtd, chan->qh))
  1904. goto exit;
  1905. }
  1906. if (hcint & HCINTMSK_NYET) {
  1907. dwc2_hc_nyet_intr(hsotg, chan, chnum, qtd);
  1908. if (!dwc2_check_qtd_still_ok(qtd, chan->qh))
  1909. goto exit;
  1910. }
  1911. if (hcint & HCINTMSK_XACTERR) {
  1912. dwc2_hc_xacterr_intr(hsotg, chan, chnum, qtd);
  1913. if (!dwc2_check_qtd_still_ok(qtd, chan->qh))
  1914. goto exit;
  1915. }
  1916. if (hcint & HCINTMSK_BBLERR) {
  1917. dwc2_hc_babble_intr(hsotg, chan, chnum, qtd);
  1918. if (!dwc2_check_qtd_still_ok(qtd, chan->qh))
  1919. goto exit;
  1920. }
  1921. if (hcint & HCINTMSK_FRMOVRUN) {
  1922. dwc2_hc_frmovrun_intr(hsotg, chan, chnum, qtd);
  1923. if (!dwc2_check_qtd_still_ok(qtd, chan->qh))
  1924. goto exit;
  1925. }
  1926. if (hcint & HCINTMSK_DATATGLERR) {
  1927. dwc2_hc_datatglerr_intr(hsotg, chan, chnum, qtd);
  1928. if (!dwc2_check_qtd_still_ok(qtd, chan->qh))
  1929. goto exit;
  1930. }
  1931. exit:
  1932. chan->hcint = 0;
  1933. }
  1934. /*
  1935. * This interrupt indicates that one or more host channels has a pending
  1936. * interrupt. There are multiple conditions that can cause each host channel
  1937. * interrupt. This function determines which conditions have occurred for each
  1938. * host channel interrupt and handles them appropriately.
  1939. */
  1940. static void dwc2_hc_intr(struct dwc2_hsotg *hsotg)
  1941. {
  1942. u32 haint;
  1943. int i;
  1944. struct dwc2_host_chan *chan, *chan_tmp;
  1945. haint = dwc2_readl(hsotg->regs + HAINT);
  1946. if (dbg_perio()) {
  1947. dev_vdbg(hsotg->dev, "%s()\n", __func__);
  1948. dev_vdbg(hsotg->dev, "HAINT=%08x\n", haint);
  1949. }
  1950. /*
  1951. * According to USB 2.0 spec section 11.18.8, a host must
  1952. * issue complete-split transactions in a microframe for a
  1953. * set of full-/low-speed endpoints in the same relative
  1954. * order as the start-splits were issued in a microframe for.
  1955. */
  1956. list_for_each_entry_safe(chan, chan_tmp, &hsotg->split_order,
  1957. split_order_list_entry) {
  1958. int hc_num = chan->hc_num;
  1959. if (haint & (1 << hc_num)) {
  1960. dwc2_hc_n_intr(hsotg, hc_num);
  1961. haint &= ~(1 << hc_num);
  1962. }
  1963. }
  1964. for (i = 0; i < hsotg->params.host_channels; i++) {
  1965. if (haint & (1 << i))
  1966. dwc2_hc_n_intr(hsotg, i);
  1967. }
  1968. }
  1969. /* This function handles interrupts for the HCD */
  1970. irqreturn_t dwc2_handle_hcd_intr(struct dwc2_hsotg *hsotg)
  1971. {
  1972. u32 gintsts, dbg_gintsts;
  1973. irqreturn_t retval = IRQ_NONE;
  1974. if (!dwc2_is_controller_alive(hsotg)) {
  1975. dev_warn(hsotg->dev, "Controller is dead\n");
  1976. return retval;
  1977. }
  1978. spin_lock(&hsotg->lock);
  1979. /* Check if HOST Mode */
  1980. if (dwc2_is_host_mode(hsotg)) {
  1981. gintsts = dwc2_read_core_intr(hsotg);
  1982. if (!gintsts) {
  1983. spin_unlock(&hsotg->lock);
  1984. return retval;
  1985. }
  1986. retval = IRQ_HANDLED;
  1987. dbg_gintsts = gintsts;
  1988. #ifndef DEBUG_SOF
  1989. dbg_gintsts &= ~GINTSTS_SOF;
  1990. #endif
  1991. if (!dbg_perio())
  1992. dbg_gintsts &= ~(GINTSTS_HCHINT | GINTSTS_RXFLVL |
  1993. GINTSTS_PTXFEMP);
  1994. /* Only print if there are any non-suppressed interrupts left */
  1995. if (dbg_gintsts)
  1996. dev_vdbg(hsotg->dev,
  1997. "DWC OTG HCD Interrupt Detected gintsts&gintmsk=0x%08x\n",
  1998. gintsts);
  1999. if (gintsts & GINTSTS_SOF)
  2000. dwc2_sof_intr(hsotg);
  2001. if (gintsts & GINTSTS_RXFLVL)
  2002. dwc2_rx_fifo_level_intr(hsotg);
  2003. if (gintsts & GINTSTS_NPTXFEMP)
  2004. dwc2_np_tx_fifo_empty_intr(hsotg);
  2005. if (gintsts & GINTSTS_PRTINT)
  2006. dwc2_port_intr(hsotg);
  2007. if (gintsts & GINTSTS_HCHINT)
  2008. dwc2_hc_intr(hsotg);
  2009. if (gintsts & GINTSTS_PTXFEMP)
  2010. dwc2_perio_tx_fifo_empty_intr(hsotg);
  2011. if (dbg_gintsts) {
  2012. dev_vdbg(hsotg->dev,
  2013. "DWC OTG HCD Finished Servicing Interrupts\n");
  2014. dev_vdbg(hsotg->dev,
  2015. "DWC OTG HCD gintsts=0x%08x gintmsk=0x%08x\n",
  2016. dwc2_readl(hsotg->regs + GINTSTS),
  2017. dwc2_readl(hsotg->regs + GINTMSK));
  2018. }
  2019. }
  2020. spin_unlock(&hsotg->lock);
  2021. return retval;
  2022. }