hcd.c 84 KB

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