hcd.c 85 KB

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