core.c 94 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280
  1. /*
  2. * core.c - DesignWare HS OTG Controller common 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. * The Core code provides basic services for accessing and managing the
  38. * DWC_otg hardware. These services are used by both the Host Controller
  39. * Driver and the Peripheral Controller Driver.
  40. */
  41. #include <linux/kernel.h>
  42. #include <linux/module.h>
  43. #include <linux/moduleparam.h>
  44. #include <linux/spinlock.h>
  45. #include <linux/interrupt.h>
  46. #include <linux/dma-mapping.h>
  47. #include <linux/delay.h>
  48. #include <linux/io.h>
  49. #include <linux/slab.h>
  50. #include <linux/usb.h>
  51. #include <linux/usb/hcd.h>
  52. #include <linux/usb/ch11.h>
  53. #include "core.h"
  54. #include "hcd.h"
  55. #if IS_ENABLED(CONFIG_USB_DWC2_HOST) || IS_ENABLED(CONFIG_USB_DWC2_DUAL_ROLE)
  56. /**
  57. * dwc2_backup_host_registers() - Backup controller host registers.
  58. * When suspending usb bus, registers needs to be backuped
  59. * if controller power is disabled once suspended.
  60. *
  61. * @hsotg: Programming view of the DWC_otg controller
  62. */
  63. static int dwc2_backup_host_registers(struct dwc2_hsotg *hsotg)
  64. {
  65. struct dwc2_hregs_backup *hr;
  66. int i;
  67. dev_dbg(hsotg->dev, "%s\n", __func__);
  68. /* Backup Host regs */
  69. hr = &hsotg->hr_backup;
  70. hr->hcfg = dwc2_readl(hsotg->regs + HCFG);
  71. hr->haintmsk = dwc2_readl(hsotg->regs + HAINTMSK);
  72. for (i = 0; i < hsotg->core_params->host_channels; ++i)
  73. hr->hcintmsk[i] = dwc2_readl(hsotg->regs + HCINTMSK(i));
  74. hr->hprt0 = dwc2_read_hprt0(hsotg);
  75. hr->hfir = dwc2_readl(hsotg->regs + HFIR);
  76. hr->valid = true;
  77. return 0;
  78. }
  79. /**
  80. * dwc2_restore_host_registers() - Restore controller host registers.
  81. * When resuming usb bus, device registers needs to be restored
  82. * if controller power were disabled.
  83. *
  84. * @hsotg: Programming view of the DWC_otg controller
  85. */
  86. static int dwc2_restore_host_registers(struct dwc2_hsotg *hsotg)
  87. {
  88. struct dwc2_hregs_backup *hr;
  89. int i;
  90. dev_dbg(hsotg->dev, "%s\n", __func__);
  91. /* Restore host regs */
  92. hr = &hsotg->hr_backup;
  93. if (!hr->valid) {
  94. dev_err(hsotg->dev, "%s: no host registers to restore\n",
  95. __func__);
  96. return -EINVAL;
  97. }
  98. hr->valid = false;
  99. dwc2_writel(hr->hcfg, hsotg->regs + HCFG);
  100. dwc2_writel(hr->haintmsk, hsotg->regs + HAINTMSK);
  101. for (i = 0; i < hsotg->core_params->host_channels; ++i)
  102. dwc2_writel(hr->hcintmsk[i], hsotg->regs + HCINTMSK(i));
  103. dwc2_writel(hr->hprt0, hsotg->regs + HPRT0);
  104. dwc2_writel(hr->hfir, hsotg->regs + HFIR);
  105. hsotg->frame_number = 0;
  106. return 0;
  107. }
  108. #else
  109. static inline int dwc2_backup_host_registers(struct dwc2_hsotg *hsotg)
  110. { return 0; }
  111. static inline int dwc2_restore_host_registers(struct dwc2_hsotg *hsotg)
  112. { return 0; }
  113. #endif
  114. #if IS_ENABLED(CONFIG_USB_DWC2_PERIPHERAL) || \
  115. IS_ENABLED(CONFIG_USB_DWC2_DUAL_ROLE)
  116. /**
  117. * dwc2_backup_device_registers() - Backup controller device registers.
  118. * When suspending usb bus, registers needs to be backuped
  119. * if controller power is disabled once suspended.
  120. *
  121. * @hsotg: Programming view of the DWC_otg controller
  122. */
  123. static int dwc2_backup_device_registers(struct dwc2_hsotg *hsotg)
  124. {
  125. struct dwc2_dregs_backup *dr;
  126. int i;
  127. dev_dbg(hsotg->dev, "%s\n", __func__);
  128. /* Backup dev regs */
  129. dr = &hsotg->dr_backup;
  130. dr->dcfg = dwc2_readl(hsotg->regs + DCFG);
  131. dr->dctl = dwc2_readl(hsotg->regs + DCTL);
  132. dr->daintmsk = dwc2_readl(hsotg->regs + DAINTMSK);
  133. dr->diepmsk = dwc2_readl(hsotg->regs + DIEPMSK);
  134. dr->doepmsk = dwc2_readl(hsotg->regs + DOEPMSK);
  135. for (i = 0; i < hsotg->num_of_eps; i++) {
  136. /* Backup IN EPs */
  137. dr->diepctl[i] = dwc2_readl(hsotg->regs + DIEPCTL(i));
  138. /* Ensure DATA PID is correctly configured */
  139. if (dr->diepctl[i] & DXEPCTL_DPID)
  140. dr->diepctl[i] |= DXEPCTL_SETD1PID;
  141. else
  142. dr->diepctl[i] |= DXEPCTL_SETD0PID;
  143. dr->dieptsiz[i] = dwc2_readl(hsotg->regs + DIEPTSIZ(i));
  144. dr->diepdma[i] = dwc2_readl(hsotg->regs + DIEPDMA(i));
  145. /* Backup OUT EPs */
  146. dr->doepctl[i] = dwc2_readl(hsotg->regs + DOEPCTL(i));
  147. /* Ensure DATA PID is correctly configured */
  148. if (dr->doepctl[i] & DXEPCTL_DPID)
  149. dr->doepctl[i] |= DXEPCTL_SETD1PID;
  150. else
  151. dr->doepctl[i] |= DXEPCTL_SETD0PID;
  152. dr->doeptsiz[i] = dwc2_readl(hsotg->regs + DOEPTSIZ(i));
  153. dr->doepdma[i] = dwc2_readl(hsotg->regs + DOEPDMA(i));
  154. }
  155. dr->valid = true;
  156. return 0;
  157. }
  158. /**
  159. * dwc2_restore_device_registers() - Restore controller device registers.
  160. * When resuming usb bus, device registers needs to be restored
  161. * if controller power were disabled.
  162. *
  163. * @hsotg: Programming view of the DWC_otg controller
  164. */
  165. static int dwc2_restore_device_registers(struct dwc2_hsotg *hsotg)
  166. {
  167. struct dwc2_dregs_backup *dr;
  168. u32 dctl;
  169. int i;
  170. dev_dbg(hsotg->dev, "%s\n", __func__);
  171. /* Restore dev regs */
  172. dr = &hsotg->dr_backup;
  173. if (!dr->valid) {
  174. dev_err(hsotg->dev, "%s: no device registers to restore\n",
  175. __func__);
  176. return -EINVAL;
  177. }
  178. dr->valid = false;
  179. dwc2_writel(dr->dcfg, hsotg->regs + DCFG);
  180. dwc2_writel(dr->dctl, hsotg->regs + DCTL);
  181. dwc2_writel(dr->daintmsk, hsotg->regs + DAINTMSK);
  182. dwc2_writel(dr->diepmsk, hsotg->regs + DIEPMSK);
  183. dwc2_writel(dr->doepmsk, hsotg->regs + DOEPMSK);
  184. for (i = 0; i < hsotg->num_of_eps; i++) {
  185. /* Restore IN EPs */
  186. dwc2_writel(dr->diepctl[i], hsotg->regs + DIEPCTL(i));
  187. dwc2_writel(dr->dieptsiz[i], hsotg->regs + DIEPTSIZ(i));
  188. dwc2_writel(dr->diepdma[i], hsotg->regs + DIEPDMA(i));
  189. /* Restore OUT EPs */
  190. dwc2_writel(dr->doepctl[i], hsotg->regs + DOEPCTL(i));
  191. dwc2_writel(dr->doeptsiz[i], hsotg->regs + DOEPTSIZ(i));
  192. dwc2_writel(dr->doepdma[i], hsotg->regs + DOEPDMA(i));
  193. }
  194. /* Set the Power-On Programming done bit */
  195. dctl = dwc2_readl(hsotg->regs + DCTL);
  196. dctl |= DCTL_PWRONPRGDONE;
  197. dwc2_writel(dctl, hsotg->regs + DCTL);
  198. return 0;
  199. }
  200. #else
  201. static inline int dwc2_backup_device_registers(struct dwc2_hsotg *hsotg)
  202. { return 0; }
  203. static inline int dwc2_restore_device_registers(struct dwc2_hsotg *hsotg)
  204. { return 0; }
  205. #endif
  206. /**
  207. * dwc2_backup_global_registers() - Backup global controller registers.
  208. * When suspending usb bus, registers needs to be backuped
  209. * if controller power is disabled once suspended.
  210. *
  211. * @hsotg: Programming view of the DWC_otg controller
  212. */
  213. static int dwc2_backup_global_registers(struct dwc2_hsotg *hsotg)
  214. {
  215. struct dwc2_gregs_backup *gr;
  216. int i;
  217. /* Backup global regs */
  218. gr = &hsotg->gr_backup;
  219. gr->gotgctl = dwc2_readl(hsotg->regs + GOTGCTL);
  220. gr->gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
  221. gr->gahbcfg = dwc2_readl(hsotg->regs + GAHBCFG);
  222. gr->gusbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
  223. gr->grxfsiz = dwc2_readl(hsotg->regs + GRXFSIZ);
  224. gr->gnptxfsiz = dwc2_readl(hsotg->regs + GNPTXFSIZ);
  225. gr->hptxfsiz = dwc2_readl(hsotg->regs + HPTXFSIZ);
  226. gr->gdfifocfg = dwc2_readl(hsotg->regs + GDFIFOCFG);
  227. for (i = 0; i < MAX_EPS_CHANNELS; i++)
  228. gr->dtxfsiz[i] = dwc2_readl(hsotg->regs + DPTXFSIZN(i));
  229. gr->valid = true;
  230. return 0;
  231. }
  232. /**
  233. * dwc2_restore_global_registers() - Restore controller global registers.
  234. * When resuming usb bus, device registers needs to be restored
  235. * if controller power were disabled.
  236. *
  237. * @hsotg: Programming view of the DWC_otg controller
  238. */
  239. static int dwc2_restore_global_registers(struct dwc2_hsotg *hsotg)
  240. {
  241. struct dwc2_gregs_backup *gr;
  242. int i;
  243. dev_dbg(hsotg->dev, "%s\n", __func__);
  244. /* Restore global regs */
  245. gr = &hsotg->gr_backup;
  246. if (!gr->valid) {
  247. dev_err(hsotg->dev, "%s: no global registers to restore\n",
  248. __func__);
  249. return -EINVAL;
  250. }
  251. gr->valid = false;
  252. dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
  253. dwc2_writel(gr->gotgctl, hsotg->regs + GOTGCTL);
  254. dwc2_writel(gr->gintmsk, hsotg->regs + GINTMSK);
  255. dwc2_writel(gr->gusbcfg, hsotg->regs + GUSBCFG);
  256. dwc2_writel(gr->gahbcfg, hsotg->regs + GAHBCFG);
  257. dwc2_writel(gr->grxfsiz, hsotg->regs + GRXFSIZ);
  258. dwc2_writel(gr->gnptxfsiz, hsotg->regs + GNPTXFSIZ);
  259. dwc2_writel(gr->hptxfsiz, hsotg->regs + HPTXFSIZ);
  260. dwc2_writel(gr->gdfifocfg, hsotg->regs + GDFIFOCFG);
  261. for (i = 0; i < MAX_EPS_CHANNELS; i++)
  262. dwc2_writel(gr->dtxfsiz[i], hsotg->regs + DPTXFSIZN(i));
  263. return 0;
  264. }
  265. /**
  266. * dwc2_exit_hibernation() - Exit controller from Partial Power Down.
  267. *
  268. * @hsotg: Programming view of the DWC_otg controller
  269. * @restore: Controller registers need to be restored
  270. */
  271. int dwc2_exit_hibernation(struct dwc2_hsotg *hsotg, bool restore)
  272. {
  273. u32 pcgcctl;
  274. int ret = 0;
  275. if (!hsotg->core_params->hibernation)
  276. return -ENOTSUPP;
  277. pcgcctl = dwc2_readl(hsotg->regs + PCGCTL);
  278. pcgcctl &= ~PCGCTL_STOPPCLK;
  279. dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
  280. pcgcctl = dwc2_readl(hsotg->regs + PCGCTL);
  281. pcgcctl &= ~PCGCTL_PWRCLMP;
  282. dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
  283. pcgcctl = dwc2_readl(hsotg->regs + PCGCTL);
  284. pcgcctl &= ~PCGCTL_RSTPDWNMODULE;
  285. dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
  286. udelay(100);
  287. if (restore) {
  288. ret = dwc2_restore_global_registers(hsotg);
  289. if (ret) {
  290. dev_err(hsotg->dev, "%s: failed to restore registers\n",
  291. __func__);
  292. return ret;
  293. }
  294. if (dwc2_is_host_mode(hsotg)) {
  295. ret = dwc2_restore_host_registers(hsotg);
  296. if (ret) {
  297. dev_err(hsotg->dev, "%s: failed to restore host registers\n",
  298. __func__);
  299. return ret;
  300. }
  301. } else {
  302. ret = dwc2_restore_device_registers(hsotg);
  303. if (ret) {
  304. dev_err(hsotg->dev, "%s: failed to restore device registers\n",
  305. __func__);
  306. return ret;
  307. }
  308. }
  309. }
  310. return ret;
  311. }
  312. /**
  313. * dwc2_enter_hibernation() - Put controller in Partial Power Down.
  314. *
  315. * @hsotg: Programming view of the DWC_otg controller
  316. */
  317. int dwc2_enter_hibernation(struct dwc2_hsotg *hsotg)
  318. {
  319. u32 pcgcctl;
  320. int ret = 0;
  321. if (!hsotg->core_params->hibernation)
  322. return -ENOTSUPP;
  323. /* Backup all registers */
  324. ret = dwc2_backup_global_registers(hsotg);
  325. if (ret) {
  326. dev_err(hsotg->dev, "%s: failed to backup global registers\n",
  327. __func__);
  328. return ret;
  329. }
  330. if (dwc2_is_host_mode(hsotg)) {
  331. ret = dwc2_backup_host_registers(hsotg);
  332. if (ret) {
  333. dev_err(hsotg->dev, "%s: failed to backup host registers\n",
  334. __func__);
  335. return ret;
  336. }
  337. } else {
  338. ret = dwc2_backup_device_registers(hsotg);
  339. if (ret) {
  340. dev_err(hsotg->dev, "%s: failed to backup device registers\n",
  341. __func__);
  342. return ret;
  343. }
  344. }
  345. /*
  346. * Clear any pending interrupts since dwc2 will not be able to
  347. * clear them after entering hibernation.
  348. */
  349. dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
  350. /* Put the controller in low power state */
  351. pcgcctl = dwc2_readl(hsotg->regs + PCGCTL);
  352. pcgcctl |= PCGCTL_PWRCLMP;
  353. dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
  354. ndelay(20);
  355. pcgcctl |= PCGCTL_RSTPDWNMODULE;
  356. dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
  357. ndelay(20);
  358. pcgcctl |= PCGCTL_STOPPCLK;
  359. dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
  360. return ret;
  361. }
  362. /**
  363. * dwc2_enable_common_interrupts() - Initializes the commmon interrupts,
  364. * used in both device and host modes
  365. *
  366. * @hsotg: Programming view of the DWC_otg controller
  367. */
  368. static void dwc2_enable_common_interrupts(struct dwc2_hsotg *hsotg)
  369. {
  370. u32 intmsk;
  371. /* Clear any pending OTG Interrupts */
  372. dwc2_writel(0xffffffff, hsotg->regs + GOTGINT);
  373. /* Clear any pending interrupts */
  374. dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
  375. /* Enable the interrupts in the GINTMSK */
  376. intmsk = GINTSTS_MODEMIS | GINTSTS_OTGINT;
  377. if (hsotg->core_params->dma_enable <= 0)
  378. intmsk |= GINTSTS_RXFLVL;
  379. if (hsotg->core_params->external_id_pin_ctl <= 0)
  380. intmsk |= GINTSTS_CONIDSTSCHNG;
  381. intmsk |= GINTSTS_WKUPINT | GINTSTS_USBSUSP |
  382. GINTSTS_SESSREQINT;
  383. dwc2_writel(intmsk, hsotg->regs + GINTMSK);
  384. }
  385. /*
  386. * Initializes the FSLSPClkSel field of the HCFG register depending on the
  387. * PHY type
  388. */
  389. static void dwc2_init_fs_ls_pclk_sel(struct dwc2_hsotg *hsotg)
  390. {
  391. u32 hcfg, val;
  392. if ((hsotg->hw_params.hs_phy_type == GHWCFG2_HS_PHY_TYPE_ULPI &&
  393. hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED &&
  394. hsotg->core_params->ulpi_fs_ls > 0) ||
  395. hsotg->core_params->phy_type == DWC2_PHY_TYPE_PARAM_FS) {
  396. /* Full speed PHY */
  397. val = HCFG_FSLSPCLKSEL_48_MHZ;
  398. } else {
  399. /* High speed PHY running at full speed or high speed */
  400. val = HCFG_FSLSPCLKSEL_30_60_MHZ;
  401. }
  402. dev_dbg(hsotg->dev, "Initializing HCFG.FSLSPClkSel to %08x\n", val);
  403. hcfg = dwc2_readl(hsotg->regs + HCFG);
  404. hcfg &= ~HCFG_FSLSPCLKSEL_MASK;
  405. hcfg |= val << HCFG_FSLSPCLKSEL_SHIFT;
  406. dwc2_writel(hcfg, hsotg->regs + HCFG);
  407. }
  408. /*
  409. * Do core a soft reset of the core. Be careful with this because it
  410. * resets all the internal state machines of the core.
  411. */
  412. static int dwc2_core_reset(struct dwc2_hsotg *hsotg)
  413. {
  414. u32 greset;
  415. int count = 0;
  416. u32 gusbcfg;
  417. dev_vdbg(hsotg->dev, "%s()\n", __func__);
  418. /* Wait for AHB master IDLE state */
  419. do {
  420. usleep_range(20000, 40000);
  421. greset = dwc2_readl(hsotg->regs + GRSTCTL);
  422. if (++count > 50) {
  423. dev_warn(hsotg->dev,
  424. "%s() HANG! AHB Idle GRSTCTL=%0x\n",
  425. __func__, greset);
  426. return -EBUSY;
  427. }
  428. } while (!(greset & GRSTCTL_AHBIDLE));
  429. /* Core Soft Reset */
  430. count = 0;
  431. greset |= GRSTCTL_CSFTRST;
  432. dwc2_writel(greset, hsotg->regs + GRSTCTL);
  433. do {
  434. usleep_range(20000, 40000);
  435. greset = dwc2_readl(hsotg->regs + GRSTCTL);
  436. if (++count > 50) {
  437. dev_warn(hsotg->dev,
  438. "%s() HANG! Soft Reset GRSTCTL=%0x\n",
  439. __func__, greset);
  440. return -EBUSY;
  441. }
  442. } while (greset & GRSTCTL_CSFTRST);
  443. if (hsotg->dr_mode == USB_DR_MODE_HOST) {
  444. gusbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
  445. gusbcfg &= ~GUSBCFG_FORCEDEVMODE;
  446. gusbcfg |= GUSBCFG_FORCEHOSTMODE;
  447. dwc2_writel(gusbcfg, hsotg->regs + GUSBCFG);
  448. } else if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) {
  449. gusbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
  450. gusbcfg &= ~GUSBCFG_FORCEHOSTMODE;
  451. gusbcfg |= GUSBCFG_FORCEDEVMODE;
  452. dwc2_writel(gusbcfg, hsotg->regs + GUSBCFG);
  453. } else if (hsotg->dr_mode == USB_DR_MODE_OTG) {
  454. gusbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
  455. gusbcfg &= ~GUSBCFG_FORCEHOSTMODE;
  456. gusbcfg &= ~GUSBCFG_FORCEDEVMODE;
  457. dwc2_writel(gusbcfg, hsotg->regs + GUSBCFG);
  458. }
  459. /*
  460. * NOTE: This long sleep is _very_ important, otherwise the core will
  461. * not stay in host mode after a connector ID change!
  462. */
  463. usleep_range(150000, 200000);
  464. return 0;
  465. }
  466. static int dwc2_fs_phy_init(struct dwc2_hsotg *hsotg, bool select_phy)
  467. {
  468. u32 usbcfg, i2cctl;
  469. int retval = 0;
  470. /*
  471. * core_init() is now called on every switch so only call the
  472. * following for the first time through
  473. */
  474. if (select_phy) {
  475. dev_dbg(hsotg->dev, "FS PHY selected\n");
  476. usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
  477. usbcfg |= GUSBCFG_PHYSEL;
  478. dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
  479. /* Reset after a PHY select */
  480. retval = dwc2_core_reset(hsotg);
  481. if (retval) {
  482. dev_err(hsotg->dev, "%s() Reset failed, aborting",
  483. __func__);
  484. return retval;
  485. }
  486. }
  487. /*
  488. * Program DCFG.DevSpd or HCFG.FSLSPclkSel to 48Mhz in FS. Also
  489. * do this on HNP Dev/Host mode switches (done in dev_init and
  490. * host_init).
  491. */
  492. if (dwc2_is_host_mode(hsotg))
  493. dwc2_init_fs_ls_pclk_sel(hsotg);
  494. if (hsotg->core_params->i2c_enable > 0) {
  495. dev_dbg(hsotg->dev, "FS PHY enabling I2C\n");
  496. /* Program GUSBCFG.OtgUtmiFsSel to I2C */
  497. usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
  498. usbcfg |= GUSBCFG_OTG_UTMI_FS_SEL;
  499. dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
  500. /* Program GI2CCTL.I2CEn */
  501. i2cctl = dwc2_readl(hsotg->regs + GI2CCTL);
  502. i2cctl &= ~GI2CCTL_I2CDEVADDR_MASK;
  503. i2cctl |= 1 << GI2CCTL_I2CDEVADDR_SHIFT;
  504. i2cctl &= ~GI2CCTL_I2CEN;
  505. dwc2_writel(i2cctl, hsotg->regs + GI2CCTL);
  506. i2cctl |= GI2CCTL_I2CEN;
  507. dwc2_writel(i2cctl, hsotg->regs + GI2CCTL);
  508. }
  509. return retval;
  510. }
  511. static int dwc2_hs_phy_init(struct dwc2_hsotg *hsotg, bool select_phy)
  512. {
  513. u32 usbcfg;
  514. int retval = 0;
  515. if (!select_phy)
  516. return 0;
  517. usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
  518. /*
  519. * HS PHY parameters. These parameters are preserved during soft reset
  520. * so only program the first time. Do a soft reset immediately after
  521. * setting phyif.
  522. */
  523. switch (hsotg->core_params->phy_type) {
  524. case DWC2_PHY_TYPE_PARAM_ULPI:
  525. /* ULPI interface */
  526. dev_dbg(hsotg->dev, "HS ULPI PHY selected\n");
  527. usbcfg |= GUSBCFG_ULPI_UTMI_SEL;
  528. usbcfg &= ~(GUSBCFG_PHYIF16 | GUSBCFG_DDRSEL);
  529. if (hsotg->core_params->phy_ulpi_ddr > 0)
  530. usbcfg |= GUSBCFG_DDRSEL;
  531. break;
  532. case DWC2_PHY_TYPE_PARAM_UTMI:
  533. /* UTMI+ interface */
  534. dev_dbg(hsotg->dev, "HS UTMI+ PHY selected\n");
  535. usbcfg &= ~(GUSBCFG_ULPI_UTMI_SEL | GUSBCFG_PHYIF16);
  536. if (hsotg->core_params->phy_utmi_width == 16)
  537. usbcfg |= GUSBCFG_PHYIF16;
  538. break;
  539. default:
  540. dev_err(hsotg->dev, "FS PHY selected at HS!\n");
  541. break;
  542. }
  543. dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
  544. /* Reset after setting the PHY parameters */
  545. retval = dwc2_core_reset(hsotg);
  546. if (retval) {
  547. dev_err(hsotg->dev, "%s() Reset failed, aborting",
  548. __func__);
  549. return retval;
  550. }
  551. return retval;
  552. }
  553. static int dwc2_phy_init(struct dwc2_hsotg *hsotg, bool select_phy)
  554. {
  555. u32 usbcfg;
  556. int retval = 0;
  557. if (hsotg->core_params->speed == DWC2_SPEED_PARAM_FULL &&
  558. hsotg->core_params->phy_type == DWC2_PHY_TYPE_PARAM_FS) {
  559. /* If FS mode with FS PHY */
  560. retval = dwc2_fs_phy_init(hsotg, select_phy);
  561. if (retval)
  562. return retval;
  563. } else {
  564. /* High speed PHY */
  565. retval = dwc2_hs_phy_init(hsotg, select_phy);
  566. if (retval)
  567. return retval;
  568. }
  569. if (hsotg->hw_params.hs_phy_type == GHWCFG2_HS_PHY_TYPE_ULPI &&
  570. hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED &&
  571. hsotg->core_params->ulpi_fs_ls > 0) {
  572. dev_dbg(hsotg->dev, "Setting ULPI FSLS\n");
  573. usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
  574. usbcfg |= GUSBCFG_ULPI_FS_LS;
  575. usbcfg |= GUSBCFG_ULPI_CLK_SUSP_M;
  576. dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
  577. } else {
  578. usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
  579. usbcfg &= ~GUSBCFG_ULPI_FS_LS;
  580. usbcfg &= ~GUSBCFG_ULPI_CLK_SUSP_M;
  581. dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
  582. }
  583. return retval;
  584. }
  585. static int dwc2_gahbcfg_init(struct dwc2_hsotg *hsotg)
  586. {
  587. u32 ahbcfg = dwc2_readl(hsotg->regs + GAHBCFG);
  588. switch (hsotg->hw_params.arch) {
  589. case GHWCFG2_EXT_DMA_ARCH:
  590. dev_err(hsotg->dev, "External DMA Mode not supported\n");
  591. return -EINVAL;
  592. case GHWCFG2_INT_DMA_ARCH:
  593. dev_dbg(hsotg->dev, "Internal DMA Mode\n");
  594. if (hsotg->core_params->ahbcfg != -1) {
  595. ahbcfg &= GAHBCFG_CTRL_MASK;
  596. ahbcfg |= hsotg->core_params->ahbcfg &
  597. ~GAHBCFG_CTRL_MASK;
  598. }
  599. break;
  600. case GHWCFG2_SLAVE_ONLY_ARCH:
  601. default:
  602. dev_dbg(hsotg->dev, "Slave Only Mode\n");
  603. break;
  604. }
  605. dev_dbg(hsotg->dev, "dma_enable:%d dma_desc_enable:%d\n",
  606. hsotg->core_params->dma_enable,
  607. hsotg->core_params->dma_desc_enable);
  608. if (hsotg->core_params->dma_enable > 0) {
  609. if (hsotg->core_params->dma_desc_enable > 0)
  610. dev_dbg(hsotg->dev, "Using Descriptor DMA mode\n");
  611. else
  612. dev_dbg(hsotg->dev, "Using Buffer DMA mode\n");
  613. } else {
  614. dev_dbg(hsotg->dev, "Using Slave mode\n");
  615. hsotg->core_params->dma_desc_enable = 0;
  616. }
  617. if (hsotg->core_params->dma_enable > 0)
  618. ahbcfg |= GAHBCFG_DMA_EN;
  619. dwc2_writel(ahbcfg, hsotg->regs + GAHBCFG);
  620. return 0;
  621. }
  622. static void dwc2_gusbcfg_init(struct dwc2_hsotg *hsotg)
  623. {
  624. u32 usbcfg;
  625. usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
  626. usbcfg &= ~(GUSBCFG_HNPCAP | GUSBCFG_SRPCAP);
  627. switch (hsotg->hw_params.op_mode) {
  628. case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
  629. if (hsotg->core_params->otg_cap ==
  630. DWC2_CAP_PARAM_HNP_SRP_CAPABLE)
  631. usbcfg |= GUSBCFG_HNPCAP;
  632. if (hsotg->core_params->otg_cap !=
  633. DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE)
  634. usbcfg |= GUSBCFG_SRPCAP;
  635. break;
  636. case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
  637. case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
  638. case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
  639. if (hsotg->core_params->otg_cap !=
  640. DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE)
  641. usbcfg |= GUSBCFG_SRPCAP;
  642. break;
  643. case GHWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE:
  644. case GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE:
  645. case GHWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST:
  646. default:
  647. break;
  648. }
  649. dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
  650. }
  651. /**
  652. * dwc2_core_init() - Initializes the DWC_otg controller registers and
  653. * prepares the core for device mode or host mode operation
  654. *
  655. * @hsotg: Programming view of the DWC_otg controller
  656. * @select_phy: If true then also set the Phy type
  657. * @irq: If >= 0, the irq to register
  658. */
  659. int dwc2_core_init(struct dwc2_hsotg *hsotg, bool select_phy, int irq)
  660. {
  661. u32 usbcfg, otgctl;
  662. int retval;
  663. dev_dbg(hsotg->dev, "%s(%p)\n", __func__, hsotg);
  664. usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
  665. /* Set ULPI External VBUS bit if needed */
  666. usbcfg &= ~GUSBCFG_ULPI_EXT_VBUS_DRV;
  667. if (hsotg->core_params->phy_ulpi_ext_vbus ==
  668. DWC2_PHY_ULPI_EXTERNAL_VBUS)
  669. usbcfg |= GUSBCFG_ULPI_EXT_VBUS_DRV;
  670. /* Set external TS Dline pulsing bit if needed */
  671. usbcfg &= ~GUSBCFG_TERMSELDLPULSE;
  672. if (hsotg->core_params->ts_dline > 0)
  673. usbcfg |= GUSBCFG_TERMSELDLPULSE;
  674. dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
  675. /* Reset the Controller */
  676. retval = dwc2_core_reset(hsotg);
  677. if (retval) {
  678. dev_err(hsotg->dev, "%s(): Reset failed, aborting\n",
  679. __func__);
  680. return retval;
  681. }
  682. /*
  683. * This needs to happen in FS mode before any other programming occurs
  684. */
  685. retval = dwc2_phy_init(hsotg, select_phy);
  686. if (retval)
  687. return retval;
  688. /* Program the GAHBCFG Register */
  689. retval = dwc2_gahbcfg_init(hsotg);
  690. if (retval)
  691. return retval;
  692. /* Program the GUSBCFG register */
  693. dwc2_gusbcfg_init(hsotg);
  694. /* Program the GOTGCTL register */
  695. otgctl = dwc2_readl(hsotg->regs + GOTGCTL);
  696. otgctl &= ~GOTGCTL_OTGVER;
  697. if (hsotg->core_params->otg_ver > 0)
  698. otgctl |= GOTGCTL_OTGVER;
  699. dwc2_writel(otgctl, hsotg->regs + GOTGCTL);
  700. dev_dbg(hsotg->dev, "OTG VER PARAM: %d\n", hsotg->core_params->otg_ver);
  701. /* Clear the SRP success bit for FS-I2c */
  702. hsotg->srp_success = 0;
  703. /* Enable common interrupts */
  704. dwc2_enable_common_interrupts(hsotg);
  705. /*
  706. * Do device or host initialization based on mode during PCD and
  707. * HCD initialization
  708. */
  709. if (dwc2_is_host_mode(hsotg)) {
  710. dev_dbg(hsotg->dev, "Host Mode\n");
  711. hsotg->op_state = OTG_STATE_A_HOST;
  712. } else {
  713. dev_dbg(hsotg->dev, "Device Mode\n");
  714. hsotg->op_state = OTG_STATE_B_PERIPHERAL;
  715. }
  716. return 0;
  717. }
  718. /**
  719. * dwc2_enable_host_interrupts() - Enables the Host mode interrupts
  720. *
  721. * @hsotg: Programming view of DWC_otg controller
  722. */
  723. void dwc2_enable_host_interrupts(struct dwc2_hsotg *hsotg)
  724. {
  725. u32 intmsk;
  726. dev_dbg(hsotg->dev, "%s()\n", __func__);
  727. /* Disable all interrupts */
  728. dwc2_writel(0, hsotg->regs + GINTMSK);
  729. dwc2_writel(0, hsotg->regs + HAINTMSK);
  730. /* Enable the common interrupts */
  731. dwc2_enable_common_interrupts(hsotg);
  732. /* Enable host mode interrupts without disturbing common interrupts */
  733. intmsk = dwc2_readl(hsotg->regs + GINTMSK);
  734. intmsk |= GINTSTS_DISCONNINT | GINTSTS_PRTINT | GINTSTS_HCHINT;
  735. dwc2_writel(intmsk, hsotg->regs + GINTMSK);
  736. }
  737. /**
  738. * dwc2_disable_host_interrupts() - Disables the Host Mode interrupts
  739. *
  740. * @hsotg: Programming view of DWC_otg controller
  741. */
  742. void dwc2_disable_host_interrupts(struct dwc2_hsotg *hsotg)
  743. {
  744. u32 intmsk = dwc2_readl(hsotg->regs + GINTMSK);
  745. /* Disable host mode interrupts without disturbing common interrupts */
  746. intmsk &= ~(GINTSTS_SOF | GINTSTS_PRTINT | GINTSTS_HCHINT |
  747. GINTSTS_PTXFEMP | GINTSTS_NPTXFEMP | GINTSTS_DISCONNINT);
  748. dwc2_writel(intmsk, hsotg->regs + GINTMSK);
  749. }
  750. /*
  751. * dwc2_calculate_dynamic_fifo() - Calculates the default fifo size
  752. * For system that have a total fifo depth that is smaller than the default
  753. * RX + TX fifo size.
  754. *
  755. * @hsotg: Programming view of DWC_otg controller
  756. */
  757. static void dwc2_calculate_dynamic_fifo(struct dwc2_hsotg *hsotg)
  758. {
  759. struct dwc2_core_params *params = hsotg->core_params;
  760. struct dwc2_hw_params *hw = &hsotg->hw_params;
  761. u32 rxfsiz, nptxfsiz, ptxfsiz, total_fifo_size;
  762. total_fifo_size = hw->total_fifo_size;
  763. rxfsiz = params->host_rx_fifo_size;
  764. nptxfsiz = params->host_nperio_tx_fifo_size;
  765. ptxfsiz = params->host_perio_tx_fifo_size;
  766. /*
  767. * Will use Method 2 defined in the DWC2 spec: minimum FIFO depth
  768. * allocation with support for high bandwidth endpoints. Synopsys
  769. * defines MPS(Max Packet size) for a periodic EP=1024, and for
  770. * non-periodic as 512.
  771. */
  772. if (total_fifo_size < (rxfsiz + nptxfsiz + ptxfsiz)) {
  773. /*
  774. * For Buffer DMA mode/Scatter Gather DMA mode
  775. * 2 * ((Largest Packet size / 4) + 1 + 1) + n
  776. * with n = number of host channel.
  777. * 2 * ((1024/4) + 2) = 516
  778. */
  779. rxfsiz = 516 + hw->host_channels;
  780. /*
  781. * min non-periodic tx fifo depth
  782. * 2 * (largest non-periodic USB packet used / 4)
  783. * 2 * (512/4) = 256
  784. */
  785. nptxfsiz = 256;
  786. /*
  787. * min periodic tx fifo depth
  788. * (largest packet size*MC)/4
  789. * (1024 * 3)/4 = 768
  790. */
  791. ptxfsiz = 768;
  792. params->host_rx_fifo_size = rxfsiz;
  793. params->host_nperio_tx_fifo_size = nptxfsiz;
  794. params->host_perio_tx_fifo_size = ptxfsiz;
  795. }
  796. /*
  797. * If the summation of RX, NPTX and PTX fifo sizes is still
  798. * bigger than the total_fifo_size, then we have a problem.
  799. *
  800. * We won't be able to allocate as many endpoints. Right now,
  801. * we're just printing an error message, but ideally this FIFO
  802. * allocation algorithm would be improved in the future.
  803. *
  804. * FIXME improve this FIFO allocation algorithm.
  805. */
  806. if (unlikely(total_fifo_size < (rxfsiz + nptxfsiz + ptxfsiz)))
  807. dev_err(hsotg->dev, "invalid fifo sizes\n");
  808. }
  809. static void dwc2_config_fifos(struct dwc2_hsotg *hsotg)
  810. {
  811. struct dwc2_core_params *params = hsotg->core_params;
  812. u32 nptxfsiz, hptxfsiz, dfifocfg, grxfsiz;
  813. if (!params->enable_dynamic_fifo)
  814. return;
  815. dwc2_calculate_dynamic_fifo(hsotg);
  816. /* Rx FIFO */
  817. grxfsiz = dwc2_readl(hsotg->regs + GRXFSIZ);
  818. dev_dbg(hsotg->dev, "initial grxfsiz=%08x\n", grxfsiz);
  819. grxfsiz &= ~GRXFSIZ_DEPTH_MASK;
  820. grxfsiz |= params->host_rx_fifo_size <<
  821. GRXFSIZ_DEPTH_SHIFT & GRXFSIZ_DEPTH_MASK;
  822. dwc2_writel(grxfsiz, hsotg->regs + GRXFSIZ);
  823. dev_dbg(hsotg->dev, "new grxfsiz=%08x\n",
  824. dwc2_readl(hsotg->regs + GRXFSIZ));
  825. /* Non-periodic Tx FIFO */
  826. dev_dbg(hsotg->dev, "initial gnptxfsiz=%08x\n",
  827. dwc2_readl(hsotg->regs + GNPTXFSIZ));
  828. nptxfsiz = params->host_nperio_tx_fifo_size <<
  829. FIFOSIZE_DEPTH_SHIFT & FIFOSIZE_DEPTH_MASK;
  830. nptxfsiz |= params->host_rx_fifo_size <<
  831. FIFOSIZE_STARTADDR_SHIFT & FIFOSIZE_STARTADDR_MASK;
  832. dwc2_writel(nptxfsiz, hsotg->regs + GNPTXFSIZ);
  833. dev_dbg(hsotg->dev, "new gnptxfsiz=%08x\n",
  834. dwc2_readl(hsotg->regs + GNPTXFSIZ));
  835. /* Periodic Tx FIFO */
  836. dev_dbg(hsotg->dev, "initial hptxfsiz=%08x\n",
  837. dwc2_readl(hsotg->regs + HPTXFSIZ));
  838. hptxfsiz = params->host_perio_tx_fifo_size <<
  839. FIFOSIZE_DEPTH_SHIFT & FIFOSIZE_DEPTH_MASK;
  840. hptxfsiz |= (params->host_rx_fifo_size +
  841. params->host_nperio_tx_fifo_size) <<
  842. FIFOSIZE_STARTADDR_SHIFT & FIFOSIZE_STARTADDR_MASK;
  843. dwc2_writel(hptxfsiz, hsotg->regs + HPTXFSIZ);
  844. dev_dbg(hsotg->dev, "new hptxfsiz=%08x\n",
  845. dwc2_readl(hsotg->regs + HPTXFSIZ));
  846. if (hsotg->core_params->en_multiple_tx_fifo > 0 &&
  847. hsotg->hw_params.snpsid <= DWC2_CORE_REV_2_94a) {
  848. /*
  849. * Global DFIFOCFG calculation for Host mode -
  850. * include RxFIFO, NPTXFIFO and HPTXFIFO
  851. */
  852. dfifocfg = dwc2_readl(hsotg->regs + GDFIFOCFG);
  853. dfifocfg &= ~GDFIFOCFG_EPINFOBASE_MASK;
  854. dfifocfg |= (params->host_rx_fifo_size +
  855. params->host_nperio_tx_fifo_size +
  856. params->host_perio_tx_fifo_size) <<
  857. GDFIFOCFG_EPINFOBASE_SHIFT &
  858. GDFIFOCFG_EPINFOBASE_MASK;
  859. dwc2_writel(dfifocfg, hsotg->regs + GDFIFOCFG);
  860. }
  861. }
  862. /**
  863. * dwc2_core_host_init() - Initializes the DWC_otg controller registers for
  864. * Host mode
  865. *
  866. * @hsotg: Programming view of DWC_otg controller
  867. *
  868. * This function flushes the Tx and Rx FIFOs and flushes any entries in the
  869. * request queues. Host channels are reset to ensure that they are ready for
  870. * performing transfers.
  871. */
  872. void dwc2_core_host_init(struct dwc2_hsotg *hsotg)
  873. {
  874. u32 hcfg, hfir, otgctl;
  875. dev_dbg(hsotg->dev, "%s(%p)\n", __func__, hsotg);
  876. /* Restart the Phy Clock */
  877. dwc2_writel(0, hsotg->regs + PCGCTL);
  878. /* Initialize Host Configuration Register */
  879. dwc2_init_fs_ls_pclk_sel(hsotg);
  880. if (hsotg->core_params->speed == DWC2_SPEED_PARAM_FULL) {
  881. hcfg = dwc2_readl(hsotg->regs + HCFG);
  882. hcfg |= HCFG_FSLSSUPP;
  883. dwc2_writel(hcfg, hsotg->regs + HCFG);
  884. }
  885. /*
  886. * This bit allows dynamic reloading of the HFIR register during
  887. * runtime. This bit needs to be programmed during initial configuration
  888. * and its value must not be changed during runtime.
  889. */
  890. if (hsotg->core_params->reload_ctl > 0) {
  891. hfir = dwc2_readl(hsotg->regs + HFIR);
  892. hfir |= HFIR_RLDCTRL;
  893. dwc2_writel(hfir, hsotg->regs + HFIR);
  894. }
  895. if (hsotg->core_params->dma_desc_enable > 0) {
  896. u32 op_mode = hsotg->hw_params.op_mode;
  897. if (hsotg->hw_params.snpsid < DWC2_CORE_REV_2_90a ||
  898. !hsotg->hw_params.dma_desc_enable ||
  899. op_mode == GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE ||
  900. op_mode == GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE ||
  901. op_mode == GHWCFG2_OP_MODE_UNDEFINED) {
  902. dev_err(hsotg->dev,
  903. "Hardware does not support descriptor DMA mode -\n");
  904. dev_err(hsotg->dev,
  905. "falling back to buffer DMA mode.\n");
  906. hsotg->core_params->dma_desc_enable = 0;
  907. } else {
  908. hcfg = dwc2_readl(hsotg->regs + HCFG);
  909. hcfg |= HCFG_DESCDMA;
  910. dwc2_writel(hcfg, hsotg->regs + HCFG);
  911. }
  912. }
  913. /* Configure data FIFO sizes */
  914. dwc2_config_fifos(hsotg);
  915. /* TODO - check this */
  916. /* Clear Host Set HNP Enable in the OTG Control Register */
  917. otgctl = dwc2_readl(hsotg->regs + GOTGCTL);
  918. otgctl &= ~GOTGCTL_HSTSETHNPEN;
  919. dwc2_writel(otgctl, hsotg->regs + GOTGCTL);
  920. /* Make sure the FIFOs are flushed */
  921. dwc2_flush_tx_fifo(hsotg, 0x10 /* all TX FIFOs */);
  922. dwc2_flush_rx_fifo(hsotg);
  923. /* Clear Host Set HNP Enable in the OTG Control Register */
  924. otgctl = dwc2_readl(hsotg->regs + GOTGCTL);
  925. otgctl &= ~GOTGCTL_HSTSETHNPEN;
  926. dwc2_writel(otgctl, hsotg->regs + GOTGCTL);
  927. if (hsotg->core_params->dma_desc_enable <= 0) {
  928. int num_channels, i;
  929. u32 hcchar;
  930. /* Flush out any leftover queued requests */
  931. num_channels = hsotg->core_params->host_channels;
  932. for (i = 0; i < num_channels; i++) {
  933. hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
  934. hcchar &= ~HCCHAR_CHENA;
  935. hcchar |= HCCHAR_CHDIS;
  936. hcchar &= ~HCCHAR_EPDIR;
  937. dwc2_writel(hcchar, hsotg->regs + HCCHAR(i));
  938. }
  939. /* Halt all channels to put them into a known state */
  940. for (i = 0; i < num_channels; i++) {
  941. int count = 0;
  942. hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
  943. hcchar |= HCCHAR_CHENA | HCCHAR_CHDIS;
  944. hcchar &= ~HCCHAR_EPDIR;
  945. dwc2_writel(hcchar, hsotg->regs + HCCHAR(i));
  946. dev_dbg(hsotg->dev, "%s: Halt channel %d\n",
  947. __func__, i);
  948. do {
  949. hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
  950. if (++count > 1000) {
  951. dev_err(hsotg->dev,
  952. "Unable to clear enable on channel %d\n",
  953. i);
  954. break;
  955. }
  956. udelay(1);
  957. } while (hcchar & HCCHAR_CHENA);
  958. }
  959. }
  960. /* Turn on the vbus power */
  961. dev_dbg(hsotg->dev, "Init: Port Power? op_state=%d\n", hsotg->op_state);
  962. if (hsotg->op_state == OTG_STATE_A_HOST) {
  963. u32 hprt0 = dwc2_read_hprt0(hsotg);
  964. dev_dbg(hsotg->dev, "Init: Power Port (%d)\n",
  965. !!(hprt0 & HPRT0_PWR));
  966. if (!(hprt0 & HPRT0_PWR)) {
  967. hprt0 |= HPRT0_PWR;
  968. dwc2_writel(hprt0, hsotg->regs + HPRT0);
  969. }
  970. }
  971. dwc2_enable_host_interrupts(hsotg);
  972. }
  973. static void dwc2_hc_enable_slave_ints(struct dwc2_hsotg *hsotg,
  974. struct dwc2_host_chan *chan)
  975. {
  976. u32 hcintmsk = HCINTMSK_CHHLTD;
  977. switch (chan->ep_type) {
  978. case USB_ENDPOINT_XFER_CONTROL:
  979. case USB_ENDPOINT_XFER_BULK:
  980. dev_vdbg(hsotg->dev, "control/bulk\n");
  981. hcintmsk |= HCINTMSK_XFERCOMPL;
  982. hcintmsk |= HCINTMSK_STALL;
  983. hcintmsk |= HCINTMSK_XACTERR;
  984. hcintmsk |= HCINTMSK_DATATGLERR;
  985. if (chan->ep_is_in) {
  986. hcintmsk |= HCINTMSK_BBLERR;
  987. } else {
  988. hcintmsk |= HCINTMSK_NAK;
  989. hcintmsk |= HCINTMSK_NYET;
  990. if (chan->do_ping)
  991. hcintmsk |= HCINTMSK_ACK;
  992. }
  993. if (chan->do_split) {
  994. hcintmsk |= HCINTMSK_NAK;
  995. if (chan->complete_split)
  996. hcintmsk |= HCINTMSK_NYET;
  997. else
  998. hcintmsk |= HCINTMSK_ACK;
  999. }
  1000. if (chan->error_state)
  1001. hcintmsk |= HCINTMSK_ACK;
  1002. break;
  1003. case USB_ENDPOINT_XFER_INT:
  1004. if (dbg_perio())
  1005. dev_vdbg(hsotg->dev, "intr\n");
  1006. hcintmsk |= HCINTMSK_XFERCOMPL;
  1007. hcintmsk |= HCINTMSK_NAK;
  1008. hcintmsk |= HCINTMSK_STALL;
  1009. hcintmsk |= HCINTMSK_XACTERR;
  1010. hcintmsk |= HCINTMSK_DATATGLERR;
  1011. hcintmsk |= HCINTMSK_FRMOVRUN;
  1012. if (chan->ep_is_in)
  1013. hcintmsk |= HCINTMSK_BBLERR;
  1014. if (chan->error_state)
  1015. hcintmsk |= HCINTMSK_ACK;
  1016. if (chan->do_split) {
  1017. if (chan->complete_split)
  1018. hcintmsk |= HCINTMSK_NYET;
  1019. else
  1020. hcintmsk |= HCINTMSK_ACK;
  1021. }
  1022. break;
  1023. case USB_ENDPOINT_XFER_ISOC:
  1024. if (dbg_perio())
  1025. dev_vdbg(hsotg->dev, "isoc\n");
  1026. hcintmsk |= HCINTMSK_XFERCOMPL;
  1027. hcintmsk |= HCINTMSK_FRMOVRUN;
  1028. hcintmsk |= HCINTMSK_ACK;
  1029. if (chan->ep_is_in) {
  1030. hcintmsk |= HCINTMSK_XACTERR;
  1031. hcintmsk |= HCINTMSK_BBLERR;
  1032. }
  1033. break;
  1034. default:
  1035. dev_err(hsotg->dev, "## Unknown EP type ##\n");
  1036. break;
  1037. }
  1038. dwc2_writel(hcintmsk, hsotg->regs + HCINTMSK(chan->hc_num));
  1039. if (dbg_hc(chan))
  1040. dev_vdbg(hsotg->dev, "set HCINTMSK to %08x\n", hcintmsk);
  1041. }
  1042. static void dwc2_hc_enable_dma_ints(struct dwc2_hsotg *hsotg,
  1043. struct dwc2_host_chan *chan)
  1044. {
  1045. u32 hcintmsk = HCINTMSK_CHHLTD;
  1046. /*
  1047. * For Descriptor DMA mode core halts the channel on AHB error.
  1048. * Interrupt is not required.
  1049. */
  1050. if (hsotg->core_params->dma_desc_enable <= 0) {
  1051. if (dbg_hc(chan))
  1052. dev_vdbg(hsotg->dev, "desc DMA disabled\n");
  1053. hcintmsk |= HCINTMSK_AHBERR;
  1054. } else {
  1055. if (dbg_hc(chan))
  1056. dev_vdbg(hsotg->dev, "desc DMA enabled\n");
  1057. if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
  1058. hcintmsk |= HCINTMSK_XFERCOMPL;
  1059. }
  1060. if (chan->error_state && !chan->do_split &&
  1061. chan->ep_type != USB_ENDPOINT_XFER_ISOC) {
  1062. if (dbg_hc(chan))
  1063. dev_vdbg(hsotg->dev, "setting ACK\n");
  1064. hcintmsk |= HCINTMSK_ACK;
  1065. if (chan->ep_is_in) {
  1066. hcintmsk |= HCINTMSK_DATATGLERR;
  1067. if (chan->ep_type != USB_ENDPOINT_XFER_INT)
  1068. hcintmsk |= HCINTMSK_NAK;
  1069. }
  1070. }
  1071. dwc2_writel(hcintmsk, hsotg->regs + HCINTMSK(chan->hc_num));
  1072. if (dbg_hc(chan))
  1073. dev_vdbg(hsotg->dev, "set HCINTMSK to %08x\n", hcintmsk);
  1074. }
  1075. static void dwc2_hc_enable_ints(struct dwc2_hsotg *hsotg,
  1076. struct dwc2_host_chan *chan)
  1077. {
  1078. u32 intmsk;
  1079. if (hsotg->core_params->dma_enable > 0) {
  1080. if (dbg_hc(chan))
  1081. dev_vdbg(hsotg->dev, "DMA enabled\n");
  1082. dwc2_hc_enable_dma_ints(hsotg, chan);
  1083. } else {
  1084. if (dbg_hc(chan))
  1085. dev_vdbg(hsotg->dev, "DMA disabled\n");
  1086. dwc2_hc_enable_slave_ints(hsotg, chan);
  1087. }
  1088. /* Enable the top level host channel interrupt */
  1089. intmsk = dwc2_readl(hsotg->regs + HAINTMSK);
  1090. intmsk |= 1 << chan->hc_num;
  1091. dwc2_writel(intmsk, hsotg->regs + HAINTMSK);
  1092. if (dbg_hc(chan))
  1093. dev_vdbg(hsotg->dev, "set HAINTMSK to %08x\n", intmsk);
  1094. /* Make sure host channel interrupts are enabled */
  1095. intmsk = dwc2_readl(hsotg->regs + GINTMSK);
  1096. intmsk |= GINTSTS_HCHINT;
  1097. dwc2_writel(intmsk, hsotg->regs + GINTMSK);
  1098. if (dbg_hc(chan))
  1099. dev_vdbg(hsotg->dev, "set GINTMSK to %08x\n", intmsk);
  1100. }
  1101. /**
  1102. * dwc2_hc_init() - Prepares a host channel for transferring packets to/from
  1103. * a specific endpoint
  1104. *
  1105. * @hsotg: Programming view of DWC_otg controller
  1106. * @chan: Information needed to initialize the host channel
  1107. *
  1108. * The HCCHARn register is set up with the characteristics specified in chan.
  1109. * Host channel interrupts that may need to be serviced while this transfer is
  1110. * in progress are enabled.
  1111. */
  1112. void dwc2_hc_init(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan)
  1113. {
  1114. u8 hc_num = chan->hc_num;
  1115. u32 hcintmsk;
  1116. u32 hcchar;
  1117. u32 hcsplt = 0;
  1118. if (dbg_hc(chan))
  1119. dev_vdbg(hsotg->dev, "%s()\n", __func__);
  1120. /* Clear old interrupt conditions for this host channel */
  1121. hcintmsk = 0xffffffff;
  1122. hcintmsk &= ~HCINTMSK_RESERVED14_31;
  1123. dwc2_writel(hcintmsk, hsotg->regs + HCINT(hc_num));
  1124. /* Enable channel interrupts required for this transfer */
  1125. dwc2_hc_enable_ints(hsotg, chan);
  1126. /*
  1127. * Program the HCCHARn register with the endpoint characteristics for
  1128. * the current transfer
  1129. */
  1130. hcchar = chan->dev_addr << HCCHAR_DEVADDR_SHIFT & HCCHAR_DEVADDR_MASK;
  1131. hcchar |= chan->ep_num << HCCHAR_EPNUM_SHIFT & HCCHAR_EPNUM_MASK;
  1132. if (chan->ep_is_in)
  1133. hcchar |= HCCHAR_EPDIR;
  1134. if (chan->speed == USB_SPEED_LOW)
  1135. hcchar |= HCCHAR_LSPDDEV;
  1136. hcchar |= chan->ep_type << HCCHAR_EPTYPE_SHIFT & HCCHAR_EPTYPE_MASK;
  1137. hcchar |= chan->max_packet << HCCHAR_MPS_SHIFT & HCCHAR_MPS_MASK;
  1138. dwc2_writel(hcchar, hsotg->regs + HCCHAR(hc_num));
  1139. if (dbg_hc(chan)) {
  1140. dev_vdbg(hsotg->dev, "set HCCHAR(%d) to %08x\n",
  1141. hc_num, hcchar);
  1142. dev_vdbg(hsotg->dev, "%s: Channel %d\n",
  1143. __func__, hc_num);
  1144. dev_vdbg(hsotg->dev, " Dev Addr: %d\n",
  1145. chan->dev_addr);
  1146. dev_vdbg(hsotg->dev, " Ep Num: %d\n",
  1147. chan->ep_num);
  1148. dev_vdbg(hsotg->dev, " Is In: %d\n",
  1149. chan->ep_is_in);
  1150. dev_vdbg(hsotg->dev, " Is Low Speed: %d\n",
  1151. chan->speed == USB_SPEED_LOW);
  1152. dev_vdbg(hsotg->dev, " Ep Type: %d\n",
  1153. chan->ep_type);
  1154. dev_vdbg(hsotg->dev, " Max Pkt: %d\n",
  1155. chan->max_packet);
  1156. }
  1157. /* Program the HCSPLT register for SPLITs */
  1158. if (chan->do_split) {
  1159. if (dbg_hc(chan))
  1160. dev_vdbg(hsotg->dev,
  1161. "Programming HC %d with split --> %s\n",
  1162. hc_num,
  1163. chan->complete_split ? "CSPLIT" : "SSPLIT");
  1164. if (chan->complete_split)
  1165. hcsplt |= HCSPLT_COMPSPLT;
  1166. hcsplt |= chan->xact_pos << HCSPLT_XACTPOS_SHIFT &
  1167. HCSPLT_XACTPOS_MASK;
  1168. hcsplt |= chan->hub_addr << HCSPLT_HUBADDR_SHIFT &
  1169. HCSPLT_HUBADDR_MASK;
  1170. hcsplt |= chan->hub_port << HCSPLT_PRTADDR_SHIFT &
  1171. HCSPLT_PRTADDR_MASK;
  1172. if (dbg_hc(chan)) {
  1173. dev_vdbg(hsotg->dev, " comp split %d\n",
  1174. chan->complete_split);
  1175. dev_vdbg(hsotg->dev, " xact pos %d\n",
  1176. chan->xact_pos);
  1177. dev_vdbg(hsotg->dev, " hub addr %d\n",
  1178. chan->hub_addr);
  1179. dev_vdbg(hsotg->dev, " hub port %d\n",
  1180. chan->hub_port);
  1181. dev_vdbg(hsotg->dev, " is_in %d\n",
  1182. chan->ep_is_in);
  1183. dev_vdbg(hsotg->dev, " Max Pkt %d\n",
  1184. chan->max_packet);
  1185. dev_vdbg(hsotg->dev, " xferlen %d\n",
  1186. chan->xfer_len);
  1187. }
  1188. }
  1189. dwc2_writel(hcsplt, hsotg->regs + HCSPLT(hc_num));
  1190. }
  1191. /**
  1192. * dwc2_hc_halt() - Attempts to halt a host channel
  1193. *
  1194. * @hsotg: Controller register interface
  1195. * @chan: Host channel to halt
  1196. * @halt_status: Reason for halting the channel
  1197. *
  1198. * This function should only be called in Slave mode or to abort a transfer in
  1199. * either Slave mode or DMA mode. Under normal circumstances in DMA mode, the
  1200. * controller halts the channel when the transfer is complete or a condition
  1201. * occurs that requires application intervention.
  1202. *
  1203. * In slave mode, checks for a free request queue entry, then sets the Channel
  1204. * Enable and Channel Disable bits of the Host Channel Characteristics
  1205. * register of the specified channel to intiate the halt. If there is no free
  1206. * request queue entry, sets only the Channel Disable bit of the HCCHARn
  1207. * register to flush requests for this channel. In the latter case, sets a
  1208. * flag to indicate that the host channel needs to be halted when a request
  1209. * queue slot is open.
  1210. *
  1211. * In DMA mode, always sets the Channel Enable and Channel Disable bits of the
  1212. * HCCHARn register. The controller ensures there is space in the request
  1213. * queue before submitting the halt request.
  1214. *
  1215. * Some time may elapse before the core flushes any posted requests for this
  1216. * host channel and halts. The Channel Halted interrupt handler completes the
  1217. * deactivation of the host channel.
  1218. */
  1219. void dwc2_hc_halt(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan,
  1220. enum dwc2_halt_status halt_status)
  1221. {
  1222. u32 nptxsts, hptxsts, hcchar;
  1223. if (dbg_hc(chan))
  1224. dev_vdbg(hsotg->dev, "%s()\n", __func__);
  1225. if (halt_status == DWC2_HC_XFER_NO_HALT_STATUS)
  1226. dev_err(hsotg->dev, "!!! halt_status = %d !!!\n", halt_status);
  1227. if (halt_status == DWC2_HC_XFER_URB_DEQUEUE ||
  1228. halt_status == DWC2_HC_XFER_AHB_ERR) {
  1229. /*
  1230. * Disable all channel interrupts except Ch Halted. The QTD
  1231. * and QH state associated with this transfer has been cleared
  1232. * (in the case of URB_DEQUEUE), so the channel needs to be
  1233. * shut down carefully to prevent crashes.
  1234. */
  1235. u32 hcintmsk = HCINTMSK_CHHLTD;
  1236. dev_vdbg(hsotg->dev, "dequeue/error\n");
  1237. dwc2_writel(hcintmsk, hsotg->regs + HCINTMSK(chan->hc_num));
  1238. /*
  1239. * Make sure no other interrupts besides halt are currently
  1240. * pending. Handling another interrupt could cause a crash due
  1241. * to the QTD and QH state.
  1242. */
  1243. dwc2_writel(~hcintmsk, hsotg->regs + HCINT(chan->hc_num));
  1244. /*
  1245. * Make sure the halt status is set to URB_DEQUEUE or AHB_ERR
  1246. * even if the channel was already halted for some other
  1247. * reason
  1248. */
  1249. chan->halt_status = halt_status;
  1250. hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
  1251. if (!(hcchar & HCCHAR_CHENA)) {
  1252. /*
  1253. * The channel is either already halted or it hasn't
  1254. * started yet. In DMA mode, the transfer may halt if
  1255. * it finishes normally or a condition occurs that
  1256. * requires driver intervention. Don't want to halt
  1257. * the channel again. In either Slave or DMA mode,
  1258. * it's possible that the transfer has been assigned
  1259. * to a channel, but not started yet when an URB is
  1260. * dequeued. Don't want to halt a channel that hasn't
  1261. * started yet.
  1262. */
  1263. return;
  1264. }
  1265. }
  1266. if (chan->halt_pending) {
  1267. /*
  1268. * A halt has already been issued for this channel. This might
  1269. * happen when a transfer is aborted by a higher level in
  1270. * the stack.
  1271. */
  1272. dev_vdbg(hsotg->dev,
  1273. "*** %s: Channel %d, chan->halt_pending already set ***\n",
  1274. __func__, chan->hc_num);
  1275. return;
  1276. }
  1277. hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
  1278. /* No need to set the bit in DDMA for disabling the channel */
  1279. /* TODO check it everywhere channel is disabled */
  1280. if (hsotg->core_params->dma_desc_enable <= 0) {
  1281. if (dbg_hc(chan))
  1282. dev_vdbg(hsotg->dev, "desc DMA disabled\n");
  1283. hcchar |= HCCHAR_CHENA;
  1284. } else {
  1285. if (dbg_hc(chan))
  1286. dev_dbg(hsotg->dev, "desc DMA enabled\n");
  1287. }
  1288. hcchar |= HCCHAR_CHDIS;
  1289. if (hsotg->core_params->dma_enable <= 0) {
  1290. if (dbg_hc(chan))
  1291. dev_vdbg(hsotg->dev, "DMA not enabled\n");
  1292. hcchar |= HCCHAR_CHENA;
  1293. /* Check for space in the request queue to issue the halt */
  1294. if (chan->ep_type == USB_ENDPOINT_XFER_CONTROL ||
  1295. chan->ep_type == USB_ENDPOINT_XFER_BULK) {
  1296. dev_vdbg(hsotg->dev, "control/bulk\n");
  1297. nptxsts = dwc2_readl(hsotg->regs + GNPTXSTS);
  1298. if ((nptxsts & TXSTS_QSPCAVAIL_MASK) == 0) {
  1299. dev_vdbg(hsotg->dev, "Disabling channel\n");
  1300. hcchar &= ~HCCHAR_CHENA;
  1301. }
  1302. } else {
  1303. if (dbg_perio())
  1304. dev_vdbg(hsotg->dev, "isoc/intr\n");
  1305. hptxsts = dwc2_readl(hsotg->regs + HPTXSTS);
  1306. if ((hptxsts & TXSTS_QSPCAVAIL_MASK) == 0 ||
  1307. hsotg->queuing_high_bandwidth) {
  1308. if (dbg_perio())
  1309. dev_vdbg(hsotg->dev, "Disabling channel\n");
  1310. hcchar &= ~HCCHAR_CHENA;
  1311. }
  1312. }
  1313. } else {
  1314. if (dbg_hc(chan))
  1315. dev_vdbg(hsotg->dev, "DMA enabled\n");
  1316. }
  1317. dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
  1318. chan->halt_status = halt_status;
  1319. if (hcchar & HCCHAR_CHENA) {
  1320. if (dbg_hc(chan))
  1321. dev_vdbg(hsotg->dev, "Channel enabled\n");
  1322. chan->halt_pending = 1;
  1323. chan->halt_on_queue = 0;
  1324. } else {
  1325. if (dbg_hc(chan))
  1326. dev_vdbg(hsotg->dev, "Channel disabled\n");
  1327. chan->halt_on_queue = 1;
  1328. }
  1329. if (dbg_hc(chan)) {
  1330. dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
  1331. chan->hc_num);
  1332. dev_vdbg(hsotg->dev, " hcchar: 0x%08x\n",
  1333. hcchar);
  1334. dev_vdbg(hsotg->dev, " halt_pending: %d\n",
  1335. chan->halt_pending);
  1336. dev_vdbg(hsotg->dev, " halt_on_queue: %d\n",
  1337. chan->halt_on_queue);
  1338. dev_vdbg(hsotg->dev, " halt_status: %d\n",
  1339. chan->halt_status);
  1340. }
  1341. }
  1342. /**
  1343. * dwc2_hc_cleanup() - Clears the transfer state for a host channel
  1344. *
  1345. * @hsotg: Programming view of DWC_otg controller
  1346. * @chan: Identifies the host channel to clean up
  1347. *
  1348. * This function is normally called after a transfer is done and the host
  1349. * channel is being released
  1350. */
  1351. void dwc2_hc_cleanup(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan)
  1352. {
  1353. u32 hcintmsk;
  1354. chan->xfer_started = 0;
  1355. /*
  1356. * Clear channel interrupt enables and any unhandled channel interrupt
  1357. * conditions
  1358. */
  1359. dwc2_writel(0, hsotg->regs + HCINTMSK(chan->hc_num));
  1360. hcintmsk = 0xffffffff;
  1361. hcintmsk &= ~HCINTMSK_RESERVED14_31;
  1362. dwc2_writel(hcintmsk, hsotg->regs + HCINT(chan->hc_num));
  1363. }
  1364. /**
  1365. * dwc2_hc_set_even_odd_frame() - Sets the channel property that indicates in
  1366. * which frame a periodic transfer should occur
  1367. *
  1368. * @hsotg: Programming view of DWC_otg controller
  1369. * @chan: Identifies the host channel to set up and its properties
  1370. * @hcchar: Current value of the HCCHAR register for the specified host channel
  1371. *
  1372. * This function has no effect on non-periodic transfers
  1373. */
  1374. static void dwc2_hc_set_even_odd_frame(struct dwc2_hsotg *hsotg,
  1375. struct dwc2_host_chan *chan, u32 *hcchar)
  1376. {
  1377. if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
  1378. chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
  1379. /* 1 if _next_ frame is odd, 0 if it's even */
  1380. if (!(dwc2_hcd_get_frame_number(hsotg) & 0x1))
  1381. *hcchar |= HCCHAR_ODDFRM;
  1382. }
  1383. }
  1384. static void dwc2_set_pid_isoc(struct dwc2_host_chan *chan)
  1385. {
  1386. /* Set up the initial PID for the transfer */
  1387. if (chan->speed == USB_SPEED_HIGH) {
  1388. if (chan->ep_is_in) {
  1389. if (chan->multi_count == 1)
  1390. chan->data_pid_start = DWC2_HC_PID_DATA0;
  1391. else if (chan->multi_count == 2)
  1392. chan->data_pid_start = DWC2_HC_PID_DATA1;
  1393. else
  1394. chan->data_pid_start = DWC2_HC_PID_DATA2;
  1395. } else {
  1396. if (chan->multi_count == 1)
  1397. chan->data_pid_start = DWC2_HC_PID_DATA0;
  1398. else
  1399. chan->data_pid_start = DWC2_HC_PID_MDATA;
  1400. }
  1401. } else {
  1402. chan->data_pid_start = DWC2_HC_PID_DATA0;
  1403. }
  1404. }
  1405. /**
  1406. * dwc2_hc_write_packet() - Writes a packet into the Tx FIFO associated with
  1407. * the Host Channel
  1408. *
  1409. * @hsotg: Programming view of DWC_otg controller
  1410. * @chan: Information needed to initialize the host channel
  1411. *
  1412. * This function should only be called in Slave mode. For a channel associated
  1413. * with a non-periodic EP, the non-periodic Tx FIFO is written. For a channel
  1414. * associated with a periodic EP, the periodic Tx FIFO is written.
  1415. *
  1416. * Upon return the xfer_buf and xfer_count fields in chan are incremented by
  1417. * the number of bytes written to the Tx FIFO.
  1418. */
  1419. static void dwc2_hc_write_packet(struct dwc2_hsotg *hsotg,
  1420. struct dwc2_host_chan *chan)
  1421. {
  1422. u32 i;
  1423. u32 remaining_count;
  1424. u32 byte_count;
  1425. u32 dword_count;
  1426. u32 __iomem *data_fifo;
  1427. u32 *data_buf = (u32 *)chan->xfer_buf;
  1428. if (dbg_hc(chan))
  1429. dev_vdbg(hsotg->dev, "%s()\n", __func__);
  1430. data_fifo = (u32 __iomem *)(hsotg->regs + HCFIFO(chan->hc_num));
  1431. remaining_count = chan->xfer_len - chan->xfer_count;
  1432. if (remaining_count > chan->max_packet)
  1433. byte_count = chan->max_packet;
  1434. else
  1435. byte_count = remaining_count;
  1436. dword_count = (byte_count + 3) / 4;
  1437. if (((unsigned long)data_buf & 0x3) == 0) {
  1438. /* xfer_buf is DWORD aligned */
  1439. for (i = 0; i < dword_count; i++, data_buf++)
  1440. dwc2_writel(*data_buf, data_fifo);
  1441. } else {
  1442. /* xfer_buf is not DWORD aligned */
  1443. for (i = 0; i < dword_count; i++, data_buf++) {
  1444. u32 data = data_buf[0] | data_buf[1] << 8 |
  1445. data_buf[2] << 16 | data_buf[3] << 24;
  1446. dwc2_writel(data, data_fifo);
  1447. }
  1448. }
  1449. chan->xfer_count += byte_count;
  1450. chan->xfer_buf += byte_count;
  1451. }
  1452. /**
  1453. * dwc2_hc_start_transfer() - Does the setup for a data transfer for a host
  1454. * channel and starts the transfer
  1455. *
  1456. * @hsotg: Programming view of DWC_otg controller
  1457. * @chan: Information needed to initialize the host channel. The xfer_len value
  1458. * may be reduced to accommodate the max widths of the XferSize and
  1459. * PktCnt fields in the HCTSIZn register. The multi_count value may be
  1460. * changed to reflect the final xfer_len value.
  1461. *
  1462. * This function may be called in either Slave mode or DMA mode. In Slave mode,
  1463. * the caller must ensure that there is sufficient space in the request queue
  1464. * and Tx Data FIFO.
  1465. *
  1466. * For an OUT transfer in Slave mode, it loads a data packet into the
  1467. * appropriate FIFO. If necessary, additional data packets are loaded in the
  1468. * Host ISR.
  1469. *
  1470. * For an IN transfer in Slave mode, a data packet is requested. The data
  1471. * packets are unloaded from the Rx FIFO in the Host ISR. If necessary,
  1472. * additional data packets are requested in the Host ISR.
  1473. *
  1474. * For a PING transfer in Slave mode, the Do Ping bit is set in the HCTSIZ
  1475. * register along with a packet count of 1 and the channel is enabled. This
  1476. * causes a single PING transaction to occur. Other fields in HCTSIZ are
  1477. * simply set to 0 since no data transfer occurs in this case.
  1478. *
  1479. * For a PING transfer in DMA mode, the HCTSIZ register is initialized with
  1480. * all the information required to perform the subsequent data transfer. In
  1481. * addition, the Do Ping bit is set in the HCTSIZ register. In this case, the
  1482. * controller performs the entire PING protocol, then starts the data
  1483. * transfer.
  1484. */
  1485. void dwc2_hc_start_transfer(struct dwc2_hsotg *hsotg,
  1486. struct dwc2_host_chan *chan)
  1487. {
  1488. u32 max_hc_xfer_size = hsotg->core_params->max_transfer_size;
  1489. u16 max_hc_pkt_count = hsotg->core_params->max_packet_count;
  1490. u32 hcchar;
  1491. u32 hctsiz = 0;
  1492. u16 num_packets;
  1493. if (dbg_hc(chan))
  1494. dev_vdbg(hsotg->dev, "%s()\n", __func__);
  1495. if (chan->do_ping) {
  1496. if (hsotg->core_params->dma_enable <= 0) {
  1497. if (dbg_hc(chan))
  1498. dev_vdbg(hsotg->dev, "ping, no DMA\n");
  1499. dwc2_hc_do_ping(hsotg, chan);
  1500. chan->xfer_started = 1;
  1501. return;
  1502. } else {
  1503. if (dbg_hc(chan))
  1504. dev_vdbg(hsotg->dev, "ping, DMA\n");
  1505. hctsiz |= TSIZ_DOPNG;
  1506. }
  1507. }
  1508. if (chan->do_split) {
  1509. if (dbg_hc(chan))
  1510. dev_vdbg(hsotg->dev, "split\n");
  1511. num_packets = 1;
  1512. if (chan->complete_split && !chan->ep_is_in)
  1513. /*
  1514. * For CSPLIT OUT Transfer, set the size to 0 so the
  1515. * core doesn't expect any data written to the FIFO
  1516. */
  1517. chan->xfer_len = 0;
  1518. else if (chan->ep_is_in || chan->xfer_len > chan->max_packet)
  1519. chan->xfer_len = chan->max_packet;
  1520. else if (!chan->ep_is_in && chan->xfer_len > 188)
  1521. chan->xfer_len = 188;
  1522. hctsiz |= chan->xfer_len << TSIZ_XFERSIZE_SHIFT &
  1523. TSIZ_XFERSIZE_MASK;
  1524. } else {
  1525. if (dbg_hc(chan))
  1526. dev_vdbg(hsotg->dev, "no split\n");
  1527. /*
  1528. * Ensure that the transfer length and packet count will fit
  1529. * in the widths allocated for them in the HCTSIZn register
  1530. */
  1531. if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
  1532. chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
  1533. /*
  1534. * Make sure the transfer size is no larger than one
  1535. * (micro)frame's worth of data. (A check was done
  1536. * when the periodic transfer was accepted to ensure
  1537. * that a (micro)frame's worth of data can be
  1538. * programmed into a channel.)
  1539. */
  1540. u32 max_periodic_len =
  1541. chan->multi_count * chan->max_packet;
  1542. if (chan->xfer_len > max_periodic_len)
  1543. chan->xfer_len = max_periodic_len;
  1544. } else if (chan->xfer_len > max_hc_xfer_size) {
  1545. /*
  1546. * Make sure that xfer_len is a multiple of max packet
  1547. * size
  1548. */
  1549. chan->xfer_len =
  1550. max_hc_xfer_size - chan->max_packet + 1;
  1551. }
  1552. if (chan->xfer_len > 0) {
  1553. num_packets = (chan->xfer_len + chan->max_packet - 1) /
  1554. chan->max_packet;
  1555. if (num_packets > max_hc_pkt_count) {
  1556. num_packets = max_hc_pkt_count;
  1557. chan->xfer_len = num_packets * chan->max_packet;
  1558. }
  1559. } else {
  1560. /* Need 1 packet for transfer length of 0 */
  1561. num_packets = 1;
  1562. }
  1563. if (chan->ep_is_in)
  1564. /*
  1565. * Always program an integral # of max packets for IN
  1566. * transfers
  1567. */
  1568. chan->xfer_len = num_packets * chan->max_packet;
  1569. if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
  1570. chan->ep_type == USB_ENDPOINT_XFER_ISOC)
  1571. /*
  1572. * Make sure that the multi_count field matches the
  1573. * actual transfer length
  1574. */
  1575. chan->multi_count = num_packets;
  1576. if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
  1577. dwc2_set_pid_isoc(chan);
  1578. hctsiz |= chan->xfer_len << TSIZ_XFERSIZE_SHIFT &
  1579. TSIZ_XFERSIZE_MASK;
  1580. }
  1581. chan->start_pkt_count = num_packets;
  1582. hctsiz |= num_packets << TSIZ_PKTCNT_SHIFT & TSIZ_PKTCNT_MASK;
  1583. hctsiz |= chan->data_pid_start << TSIZ_SC_MC_PID_SHIFT &
  1584. TSIZ_SC_MC_PID_MASK;
  1585. dwc2_writel(hctsiz, hsotg->regs + HCTSIZ(chan->hc_num));
  1586. if (dbg_hc(chan)) {
  1587. dev_vdbg(hsotg->dev, "Wrote %08x to HCTSIZ(%d)\n",
  1588. hctsiz, chan->hc_num);
  1589. dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
  1590. chan->hc_num);
  1591. dev_vdbg(hsotg->dev, " Xfer Size: %d\n",
  1592. (hctsiz & TSIZ_XFERSIZE_MASK) >>
  1593. TSIZ_XFERSIZE_SHIFT);
  1594. dev_vdbg(hsotg->dev, " Num Pkts: %d\n",
  1595. (hctsiz & TSIZ_PKTCNT_MASK) >>
  1596. TSIZ_PKTCNT_SHIFT);
  1597. dev_vdbg(hsotg->dev, " Start PID: %d\n",
  1598. (hctsiz & TSIZ_SC_MC_PID_MASK) >>
  1599. TSIZ_SC_MC_PID_SHIFT);
  1600. }
  1601. if (hsotg->core_params->dma_enable > 0) {
  1602. dma_addr_t dma_addr;
  1603. if (chan->align_buf) {
  1604. if (dbg_hc(chan))
  1605. dev_vdbg(hsotg->dev, "align_buf\n");
  1606. dma_addr = chan->align_buf;
  1607. } else {
  1608. dma_addr = chan->xfer_dma;
  1609. }
  1610. dwc2_writel((u32)dma_addr, hsotg->regs + HCDMA(chan->hc_num));
  1611. if (dbg_hc(chan))
  1612. dev_vdbg(hsotg->dev, "Wrote %08lx to HCDMA(%d)\n",
  1613. (unsigned long)dma_addr, chan->hc_num);
  1614. }
  1615. /* Start the split */
  1616. if (chan->do_split) {
  1617. u32 hcsplt = dwc2_readl(hsotg->regs + HCSPLT(chan->hc_num));
  1618. hcsplt |= HCSPLT_SPLTENA;
  1619. dwc2_writel(hcsplt, hsotg->regs + HCSPLT(chan->hc_num));
  1620. }
  1621. hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
  1622. hcchar &= ~HCCHAR_MULTICNT_MASK;
  1623. hcchar |= chan->multi_count << HCCHAR_MULTICNT_SHIFT &
  1624. HCCHAR_MULTICNT_MASK;
  1625. dwc2_hc_set_even_odd_frame(hsotg, chan, &hcchar);
  1626. if (hcchar & HCCHAR_CHDIS)
  1627. dev_warn(hsotg->dev,
  1628. "%s: chdis set, channel %d, hcchar 0x%08x\n",
  1629. __func__, chan->hc_num, hcchar);
  1630. /* Set host channel enable after all other setup is complete */
  1631. hcchar |= HCCHAR_CHENA;
  1632. hcchar &= ~HCCHAR_CHDIS;
  1633. if (dbg_hc(chan))
  1634. dev_vdbg(hsotg->dev, " Multi Cnt: %d\n",
  1635. (hcchar & HCCHAR_MULTICNT_MASK) >>
  1636. HCCHAR_MULTICNT_SHIFT);
  1637. dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
  1638. if (dbg_hc(chan))
  1639. dev_vdbg(hsotg->dev, "Wrote %08x to HCCHAR(%d)\n", hcchar,
  1640. chan->hc_num);
  1641. chan->xfer_started = 1;
  1642. chan->requests++;
  1643. if (hsotg->core_params->dma_enable <= 0 &&
  1644. !chan->ep_is_in && chan->xfer_len > 0)
  1645. /* Load OUT packet into the appropriate Tx FIFO */
  1646. dwc2_hc_write_packet(hsotg, chan);
  1647. }
  1648. /**
  1649. * dwc2_hc_start_transfer_ddma() - Does the setup for a data transfer for a
  1650. * host channel and starts the transfer in Descriptor DMA mode
  1651. *
  1652. * @hsotg: Programming view of DWC_otg controller
  1653. * @chan: Information needed to initialize the host channel
  1654. *
  1655. * Initializes HCTSIZ register. For a PING transfer the Do Ping bit is set.
  1656. * Sets PID and NTD values. For periodic transfers initializes SCHED_INFO field
  1657. * with micro-frame bitmap.
  1658. *
  1659. * Initializes HCDMA register with descriptor list address and CTD value then
  1660. * starts the transfer via enabling the channel.
  1661. */
  1662. void dwc2_hc_start_transfer_ddma(struct dwc2_hsotg *hsotg,
  1663. struct dwc2_host_chan *chan)
  1664. {
  1665. u32 hcchar;
  1666. u32 hc_dma;
  1667. u32 hctsiz = 0;
  1668. if (chan->do_ping)
  1669. hctsiz |= TSIZ_DOPNG;
  1670. if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
  1671. dwc2_set_pid_isoc(chan);
  1672. /* Packet Count and Xfer Size are not used in Descriptor DMA mode */
  1673. hctsiz |= chan->data_pid_start << TSIZ_SC_MC_PID_SHIFT &
  1674. TSIZ_SC_MC_PID_MASK;
  1675. /* 0 - 1 descriptor, 1 - 2 descriptors, etc */
  1676. hctsiz |= (chan->ntd - 1) << TSIZ_NTD_SHIFT & TSIZ_NTD_MASK;
  1677. /* Non-zero only for high-speed interrupt endpoints */
  1678. hctsiz |= chan->schinfo << TSIZ_SCHINFO_SHIFT & TSIZ_SCHINFO_MASK;
  1679. if (dbg_hc(chan)) {
  1680. dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
  1681. chan->hc_num);
  1682. dev_vdbg(hsotg->dev, " Start PID: %d\n",
  1683. chan->data_pid_start);
  1684. dev_vdbg(hsotg->dev, " NTD: %d\n", chan->ntd - 1);
  1685. }
  1686. dwc2_writel(hctsiz, hsotg->regs + HCTSIZ(chan->hc_num));
  1687. hc_dma = (u32)chan->desc_list_addr & HCDMA_DMA_ADDR_MASK;
  1688. /* Always start from first descriptor */
  1689. hc_dma &= ~HCDMA_CTD_MASK;
  1690. dwc2_writel(hc_dma, hsotg->regs + HCDMA(chan->hc_num));
  1691. if (dbg_hc(chan))
  1692. dev_vdbg(hsotg->dev, "Wrote %08x to HCDMA(%d)\n",
  1693. hc_dma, chan->hc_num);
  1694. hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
  1695. hcchar &= ~HCCHAR_MULTICNT_MASK;
  1696. hcchar |= chan->multi_count << HCCHAR_MULTICNT_SHIFT &
  1697. HCCHAR_MULTICNT_MASK;
  1698. if (hcchar & HCCHAR_CHDIS)
  1699. dev_warn(hsotg->dev,
  1700. "%s: chdis set, channel %d, hcchar 0x%08x\n",
  1701. __func__, chan->hc_num, hcchar);
  1702. /* Set host channel enable after all other setup is complete */
  1703. hcchar |= HCCHAR_CHENA;
  1704. hcchar &= ~HCCHAR_CHDIS;
  1705. if (dbg_hc(chan))
  1706. dev_vdbg(hsotg->dev, " Multi Cnt: %d\n",
  1707. (hcchar & HCCHAR_MULTICNT_MASK) >>
  1708. HCCHAR_MULTICNT_SHIFT);
  1709. dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
  1710. if (dbg_hc(chan))
  1711. dev_vdbg(hsotg->dev, "Wrote %08x to HCCHAR(%d)\n", hcchar,
  1712. chan->hc_num);
  1713. chan->xfer_started = 1;
  1714. chan->requests++;
  1715. }
  1716. /**
  1717. * dwc2_hc_continue_transfer() - Continues a data transfer that was started by
  1718. * a previous call to dwc2_hc_start_transfer()
  1719. *
  1720. * @hsotg: Programming view of DWC_otg controller
  1721. * @chan: Information needed to initialize the host channel
  1722. *
  1723. * The caller must ensure there is sufficient space in the request queue and Tx
  1724. * Data FIFO. This function should only be called in Slave mode. In DMA mode,
  1725. * the controller acts autonomously to complete transfers programmed to a host
  1726. * channel.
  1727. *
  1728. * For an OUT transfer, a new data packet is loaded into the appropriate FIFO
  1729. * if there is any data remaining to be queued. For an IN transfer, another
  1730. * data packet is always requested. For the SETUP phase of a control transfer,
  1731. * this function does nothing.
  1732. *
  1733. * Return: 1 if a new request is queued, 0 if no more requests are required
  1734. * for this transfer
  1735. */
  1736. int dwc2_hc_continue_transfer(struct dwc2_hsotg *hsotg,
  1737. struct dwc2_host_chan *chan)
  1738. {
  1739. if (dbg_hc(chan))
  1740. dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
  1741. chan->hc_num);
  1742. if (chan->do_split)
  1743. /* SPLITs always queue just once per channel */
  1744. return 0;
  1745. if (chan->data_pid_start == DWC2_HC_PID_SETUP)
  1746. /* SETUPs are queued only once since they can't be NAK'd */
  1747. return 0;
  1748. if (chan->ep_is_in) {
  1749. /*
  1750. * Always queue another request for other IN transfers. If
  1751. * back-to-back INs are issued and NAKs are received for both,
  1752. * the driver may still be processing the first NAK when the
  1753. * second NAK is received. When the interrupt handler clears
  1754. * the NAK interrupt for the first NAK, the second NAK will
  1755. * not be seen. So we can't depend on the NAK interrupt
  1756. * handler to requeue a NAK'd request. Instead, IN requests
  1757. * are issued each time this function is called. When the
  1758. * transfer completes, the extra requests for the channel will
  1759. * be flushed.
  1760. */
  1761. u32 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
  1762. dwc2_hc_set_even_odd_frame(hsotg, chan, &hcchar);
  1763. hcchar |= HCCHAR_CHENA;
  1764. hcchar &= ~HCCHAR_CHDIS;
  1765. if (dbg_hc(chan))
  1766. dev_vdbg(hsotg->dev, " IN xfer: hcchar = 0x%08x\n",
  1767. hcchar);
  1768. dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
  1769. chan->requests++;
  1770. return 1;
  1771. }
  1772. /* OUT transfers */
  1773. if (chan->xfer_count < chan->xfer_len) {
  1774. if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
  1775. chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
  1776. u32 hcchar = dwc2_readl(hsotg->regs +
  1777. HCCHAR(chan->hc_num));
  1778. dwc2_hc_set_even_odd_frame(hsotg, chan,
  1779. &hcchar);
  1780. }
  1781. /* Load OUT packet into the appropriate Tx FIFO */
  1782. dwc2_hc_write_packet(hsotg, chan);
  1783. chan->requests++;
  1784. return 1;
  1785. }
  1786. return 0;
  1787. }
  1788. /**
  1789. * dwc2_hc_do_ping() - Starts a PING transfer
  1790. *
  1791. * @hsotg: Programming view of DWC_otg controller
  1792. * @chan: Information needed to initialize the host channel
  1793. *
  1794. * This function should only be called in Slave mode. The Do Ping bit is set in
  1795. * the HCTSIZ register, then the channel is enabled.
  1796. */
  1797. void dwc2_hc_do_ping(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan)
  1798. {
  1799. u32 hcchar;
  1800. u32 hctsiz;
  1801. if (dbg_hc(chan))
  1802. dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
  1803. chan->hc_num);
  1804. hctsiz = TSIZ_DOPNG;
  1805. hctsiz |= 1 << TSIZ_PKTCNT_SHIFT;
  1806. dwc2_writel(hctsiz, hsotg->regs + HCTSIZ(chan->hc_num));
  1807. hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
  1808. hcchar |= HCCHAR_CHENA;
  1809. hcchar &= ~HCCHAR_CHDIS;
  1810. dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
  1811. }
  1812. /**
  1813. * dwc2_calc_frame_interval() - Calculates the correct frame Interval value for
  1814. * the HFIR register according to PHY type and speed
  1815. *
  1816. * @hsotg: Programming view of DWC_otg controller
  1817. *
  1818. * NOTE: The caller can modify the value of the HFIR register only after the
  1819. * Port Enable bit of the Host Port Control and Status register (HPRT.EnaPort)
  1820. * has been set
  1821. */
  1822. u32 dwc2_calc_frame_interval(struct dwc2_hsotg *hsotg)
  1823. {
  1824. u32 usbcfg;
  1825. u32 hprt0;
  1826. int clock = 60; /* default value */
  1827. usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
  1828. hprt0 = dwc2_readl(hsotg->regs + HPRT0);
  1829. if (!(usbcfg & GUSBCFG_PHYSEL) && (usbcfg & GUSBCFG_ULPI_UTMI_SEL) &&
  1830. !(usbcfg & GUSBCFG_PHYIF16))
  1831. clock = 60;
  1832. if ((usbcfg & GUSBCFG_PHYSEL) && hsotg->hw_params.fs_phy_type ==
  1833. GHWCFG2_FS_PHY_TYPE_SHARED_ULPI)
  1834. clock = 48;
  1835. if (!(usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
  1836. !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && (usbcfg & GUSBCFG_PHYIF16))
  1837. clock = 30;
  1838. if (!(usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
  1839. !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && !(usbcfg & GUSBCFG_PHYIF16))
  1840. clock = 60;
  1841. if ((usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
  1842. !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && (usbcfg & GUSBCFG_PHYIF16))
  1843. clock = 48;
  1844. if ((usbcfg & GUSBCFG_PHYSEL) && !(usbcfg & GUSBCFG_PHYIF16) &&
  1845. hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_SHARED_UTMI)
  1846. clock = 48;
  1847. if ((usbcfg & GUSBCFG_PHYSEL) &&
  1848. hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED)
  1849. clock = 48;
  1850. if ((hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT == HPRT0_SPD_HIGH_SPEED)
  1851. /* High speed case */
  1852. return 125 * clock;
  1853. else
  1854. /* FS/LS case */
  1855. return 1000 * clock;
  1856. }
  1857. /**
  1858. * dwc2_read_packet() - Reads a packet from the Rx FIFO into the destination
  1859. * buffer
  1860. *
  1861. * @core_if: Programming view of DWC_otg controller
  1862. * @dest: Destination buffer for the packet
  1863. * @bytes: Number of bytes to copy to the destination
  1864. */
  1865. void dwc2_read_packet(struct dwc2_hsotg *hsotg, u8 *dest, u16 bytes)
  1866. {
  1867. u32 __iomem *fifo = hsotg->regs + HCFIFO(0);
  1868. u32 *data_buf = (u32 *)dest;
  1869. int word_count = (bytes + 3) / 4;
  1870. int i;
  1871. /*
  1872. * Todo: Account for the case where dest is not dword aligned. This
  1873. * requires reading data from the FIFO into a u32 temp buffer, then
  1874. * moving it into the data buffer.
  1875. */
  1876. dev_vdbg(hsotg->dev, "%s(%p,%p,%d)\n", __func__, hsotg, dest, bytes);
  1877. for (i = 0; i < word_count; i++, data_buf++)
  1878. *data_buf = dwc2_readl(fifo);
  1879. }
  1880. /**
  1881. * dwc2_dump_host_registers() - Prints the host registers
  1882. *
  1883. * @hsotg: Programming view of DWC_otg controller
  1884. *
  1885. * NOTE: This function will be removed once the peripheral controller code
  1886. * is integrated and the driver is stable
  1887. */
  1888. void dwc2_dump_host_registers(struct dwc2_hsotg *hsotg)
  1889. {
  1890. #ifdef DEBUG
  1891. u32 __iomem *addr;
  1892. int i;
  1893. dev_dbg(hsotg->dev, "Host Global Registers\n");
  1894. addr = hsotg->regs + HCFG;
  1895. dev_dbg(hsotg->dev, "HCFG @0x%08lX : 0x%08X\n",
  1896. (unsigned long)addr, dwc2_readl(addr));
  1897. addr = hsotg->regs + HFIR;
  1898. dev_dbg(hsotg->dev, "HFIR @0x%08lX : 0x%08X\n",
  1899. (unsigned long)addr, dwc2_readl(addr));
  1900. addr = hsotg->regs + HFNUM;
  1901. dev_dbg(hsotg->dev, "HFNUM @0x%08lX : 0x%08X\n",
  1902. (unsigned long)addr, dwc2_readl(addr));
  1903. addr = hsotg->regs + HPTXSTS;
  1904. dev_dbg(hsotg->dev, "HPTXSTS @0x%08lX : 0x%08X\n",
  1905. (unsigned long)addr, dwc2_readl(addr));
  1906. addr = hsotg->regs + HAINT;
  1907. dev_dbg(hsotg->dev, "HAINT @0x%08lX : 0x%08X\n",
  1908. (unsigned long)addr, dwc2_readl(addr));
  1909. addr = hsotg->regs + HAINTMSK;
  1910. dev_dbg(hsotg->dev, "HAINTMSK @0x%08lX : 0x%08X\n",
  1911. (unsigned long)addr, dwc2_readl(addr));
  1912. if (hsotg->core_params->dma_desc_enable > 0) {
  1913. addr = hsotg->regs + HFLBADDR;
  1914. dev_dbg(hsotg->dev, "HFLBADDR @0x%08lX : 0x%08X\n",
  1915. (unsigned long)addr, dwc2_readl(addr));
  1916. }
  1917. addr = hsotg->regs + HPRT0;
  1918. dev_dbg(hsotg->dev, "HPRT0 @0x%08lX : 0x%08X\n",
  1919. (unsigned long)addr, dwc2_readl(addr));
  1920. for (i = 0; i < hsotg->core_params->host_channels; i++) {
  1921. dev_dbg(hsotg->dev, "Host Channel %d Specific Registers\n", i);
  1922. addr = hsotg->regs + HCCHAR(i);
  1923. dev_dbg(hsotg->dev, "HCCHAR @0x%08lX : 0x%08X\n",
  1924. (unsigned long)addr, dwc2_readl(addr));
  1925. addr = hsotg->regs + HCSPLT(i);
  1926. dev_dbg(hsotg->dev, "HCSPLT @0x%08lX : 0x%08X\n",
  1927. (unsigned long)addr, dwc2_readl(addr));
  1928. addr = hsotg->regs + HCINT(i);
  1929. dev_dbg(hsotg->dev, "HCINT @0x%08lX : 0x%08X\n",
  1930. (unsigned long)addr, dwc2_readl(addr));
  1931. addr = hsotg->regs + HCINTMSK(i);
  1932. dev_dbg(hsotg->dev, "HCINTMSK @0x%08lX : 0x%08X\n",
  1933. (unsigned long)addr, dwc2_readl(addr));
  1934. addr = hsotg->regs + HCTSIZ(i);
  1935. dev_dbg(hsotg->dev, "HCTSIZ @0x%08lX : 0x%08X\n",
  1936. (unsigned long)addr, dwc2_readl(addr));
  1937. addr = hsotg->regs + HCDMA(i);
  1938. dev_dbg(hsotg->dev, "HCDMA @0x%08lX : 0x%08X\n",
  1939. (unsigned long)addr, dwc2_readl(addr));
  1940. if (hsotg->core_params->dma_desc_enable > 0) {
  1941. addr = hsotg->regs + HCDMAB(i);
  1942. dev_dbg(hsotg->dev, "HCDMAB @0x%08lX : 0x%08X\n",
  1943. (unsigned long)addr, dwc2_readl(addr));
  1944. }
  1945. }
  1946. #endif
  1947. }
  1948. /**
  1949. * dwc2_dump_global_registers() - Prints the core global registers
  1950. *
  1951. * @hsotg: Programming view of DWC_otg controller
  1952. *
  1953. * NOTE: This function will be removed once the peripheral controller code
  1954. * is integrated and the driver is stable
  1955. */
  1956. void dwc2_dump_global_registers(struct dwc2_hsotg *hsotg)
  1957. {
  1958. #ifdef DEBUG
  1959. u32 __iomem *addr;
  1960. dev_dbg(hsotg->dev, "Core Global Registers\n");
  1961. addr = hsotg->regs + GOTGCTL;
  1962. dev_dbg(hsotg->dev, "GOTGCTL @0x%08lX : 0x%08X\n",
  1963. (unsigned long)addr, dwc2_readl(addr));
  1964. addr = hsotg->regs + GOTGINT;
  1965. dev_dbg(hsotg->dev, "GOTGINT @0x%08lX : 0x%08X\n",
  1966. (unsigned long)addr, dwc2_readl(addr));
  1967. addr = hsotg->regs + GAHBCFG;
  1968. dev_dbg(hsotg->dev, "GAHBCFG @0x%08lX : 0x%08X\n",
  1969. (unsigned long)addr, dwc2_readl(addr));
  1970. addr = hsotg->regs + GUSBCFG;
  1971. dev_dbg(hsotg->dev, "GUSBCFG @0x%08lX : 0x%08X\n",
  1972. (unsigned long)addr, dwc2_readl(addr));
  1973. addr = hsotg->regs + GRSTCTL;
  1974. dev_dbg(hsotg->dev, "GRSTCTL @0x%08lX : 0x%08X\n",
  1975. (unsigned long)addr, dwc2_readl(addr));
  1976. addr = hsotg->regs + GINTSTS;
  1977. dev_dbg(hsotg->dev, "GINTSTS @0x%08lX : 0x%08X\n",
  1978. (unsigned long)addr, dwc2_readl(addr));
  1979. addr = hsotg->regs + GINTMSK;
  1980. dev_dbg(hsotg->dev, "GINTMSK @0x%08lX : 0x%08X\n",
  1981. (unsigned long)addr, dwc2_readl(addr));
  1982. addr = hsotg->regs + GRXSTSR;
  1983. dev_dbg(hsotg->dev, "GRXSTSR @0x%08lX : 0x%08X\n",
  1984. (unsigned long)addr, dwc2_readl(addr));
  1985. addr = hsotg->regs + GRXFSIZ;
  1986. dev_dbg(hsotg->dev, "GRXFSIZ @0x%08lX : 0x%08X\n",
  1987. (unsigned long)addr, dwc2_readl(addr));
  1988. addr = hsotg->regs + GNPTXFSIZ;
  1989. dev_dbg(hsotg->dev, "GNPTXFSIZ @0x%08lX : 0x%08X\n",
  1990. (unsigned long)addr, dwc2_readl(addr));
  1991. addr = hsotg->regs + GNPTXSTS;
  1992. dev_dbg(hsotg->dev, "GNPTXSTS @0x%08lX : 0x%08X\n",
  1993. (unsigned long)addr, dwc2_readl(addr));
  1994. addr = hsotg->regs + GI2CCTL;
  1995. dev_dbg(hsotg->dev, "GI2CCTL @0x%08lX : 0x%08X\n",
  1996. (unsigned long)addr, dwc2_readl(addr));
  1997. addr = hsotg->regs + GPVNDCTL;
  1998. dev_dbg(hsotg->dev, "GPVNDCTL @0x%08lX : 0x%08X\n",
  1999. (unsigned long)addr, dwc2_readl(addr));
  2000. addr = hsotg->regs + GGPIO;
  2001. dev_dbg(hsotg->dev, "GGPIO @0x%08lX : 0x%08X\n",
  2002. (unsigned long)addr, dwc2_readl(addr));
  2003. addr = hsotg->regs + GUID;
  2004. dev_dbg(hsotg->dev, "GUID @0x%08lX : 0x%08X\n",
  2005. (unsigned long)addr, dwc2_readl(addr));
  2006. addr = hsotg->regs + GSNPSID;
  2007. dev_dbg(hsotg->dev, "GSNPSID @0x%08lX : 0x%08X\n",
  2008. (unsigned long)addr, dwc2_readl(addr));
  2009. addr = hsotg->regs + GHWCFG1;
  2010. dev_dbg(hsotg->dev, "GHWCFG1 @0x%08lX : 0x%08X\n",
  2011. (unsigned long)addr, dwc2_readl(addr));
  2012. addr = hsotg->regs + GHWCFG2;
  2013. dev_dbg(hsotg->dev, "GHWCFG2 @0x%08lX : 0x%08X\n",
  2014. (unsigned long)addr, dwc2_readl(addr));
  2015. addr = hsotg->regs + GHWCFG3;
  2016. dev_dbg(hsotg->dev, "GHWCFG3 @0x%08lX : 0x%08X\n",
  2017. (unsigned long)addr, dwc2_readl(addr));
  2018. addr = hsotg->regs + GHWCFG4;
  2019. dev_dbg(hsotg->dev, "GHWCFG4 @0x%08lX : 0x%08X\n",
  2020. (unsigned long)addr, dwc2_readl(addr));
  2021. addr = hsotg->regs + GLPMCFG;
  2022. dev_dbg(hsotg->dev, "GLPMCFG @0x%08lX : 0x%08X\n",
  2023. (unsigned long)addr, dwc2_readl(addr));
  2024. addr = hsotg->regs + GPWRDN;
  2025. dev_dbg(hsotg->dev, "GPWRDN @0x%08lX : 0x%08X\n",
  2026. (unsigned long)addr, dwc2_readl(addr));
  2027. addr = hsotg->regs + GDFIFOCFG;
  2028. dev_dbg(hsotg->dev, "GDFIFOCFG @0x%08lX : 0x%08X\n",
  2029. (unsigned long)addr, dwc2_readl(addr));
  2030. addr = hsotg->regs + HPTXFSIZ;
  2031. dev_dbg(hsotg->dev, "HPTXFSIZ @0x%08lX : 0x%08X\n",
  2032. (unsigned long)addr, dwc2_readl(addr));
  2033. addr = hsotg->regs + PCGCTL;
  2034. dev_dbg(hsotg->dev, "PCGCTL @0x%08lX : 0x%08X\n",
  2035. (unsigned long)addr, dwc2_readl(addr));
  2036. #endif
  2037. }
  2038. /**
  2039. * dwc2_flush_tx_fifo() - Flushes a Tx FIFO
  2040. *
  2041. * @hsotg: Programming view of DWC_otg controller
  2042. * @num: Tx FIFO to flush
  2043. */
  2044. void dwc2_flush_tx_fifo(struct dwc2_hsotg *hsotg, const int num)
  2045. {
  2046. u32 greset;
  2047. int count = 0;
  2048. dev_vdbg(hsotg->dev, "Flush Tx FIFO %d\n", num);
  2049. greset = GRSTCTL_TXFFLSH;
  2050. greset |= num << GRSTCTL_TXFNUM_SHIFT & GRSTCTL_TXFNUM_MASK;
  2051. dwc2_writel(greset, hsotg->regs + GRSTCTL);
  2052. do {
  2053. greset = dwc2_readl(hsotg->regs + GRSTCTL);
  2054. if (++count > 10000) {
  2055. dev_warn(hsotg->dev,
  2056. "%s() HANG! GRSTCTL=%0x GNPTXSTS=0x%08x\n",
  2057. __func__, greset,
  2058. dwc2_readl(hsotg->regs + GNPTXSTS));
  2059. break;
  2060. }
  2061. udelay(1);
  2062. } while (greset & GRSTCTL_TXFFLSH);
  2063. /* Wait for at least 3 PHY Clocks */
  2064. udelay(1);
  2065. }
  2066. /**
  2067. * dwc2_flush_rx_fifo() - Flushes the Rx FIFO
  2068. *
  2069. * @hsotg: Programming view of DWC_otg controller
  2070. */
  2071. void dwc2_flush_rx_fifo(struct dwc2_hsotg *hsotg)
  2072. {
  2073. u32 greset;
  2074. int count = 0;
  2075. dev_vdbg(hsotg->dev, "%s()\n", __func__);
  2076. greset = GRSTCTL_RXFFLSH;
  2077. dwc2_writel(greset, hsotg->regs + GRSTCTL);
  2078. do {
  2079. greset = dwc2_readl(hsotg->regs + GRSTCTL);
  2080. if (++count > 10000) {
  2081. dev_warn(hsotg->dev, "%s() HANG! GRSTCTL=%0x\n",
  2082. __func__, greset);
  2083. break;
  2084. }
  2085. udelay(1);
  2086. } while (greset & GRSTCTL_RXFFLSH);
  2087. /* Wait for at least 3 PHY Clocks */
  2088. udelay(1);
  2089. }
  2090. #define DWC2_OUT_OF_BOUNDS(a, b, c) ((a) < (b) || (a) > (c))
  2091. /* Parameter access functions */
  2092. void dwc2_set_param_otg_cap(struct dwc2_hsotg *hsotg, int val)
  2093. {
  2094. int valid = 1;
  2095. switch (val) {
  2096. case DWC2_CAP_PARAM_HNP_SRP_CAPABLE:
  2097. if (hsotg->hw_params.op_mode != GHWCFG2_OP_MODE_HNP_SRP_CAPABLE)
  2098. valid = 0;
  2099. break;
  2100. case DWC2_CAP_PARAM_SRP_ONLY_CAPABLE:
  2101. switch (hsotg->hw_params.op_mode) {
  2102. case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
  2103. case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
  2104. case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
  2105. case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
  2106. break;
  2107. default:
  2108. valid = 0;
  2109. break;
  2110. }
  2111. break;
  2112. case DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE:
  2113. /* always valid */
  2114. break;
  2115. default:
  2116. valid = 0;
  2117. break;
  2118. }
  2119. if (!valid) {
  2120. if (val >= 0)
  2121. dev_err(hsotg->dev,
  2122. "%d invalid for otg_cap parameter. Check HW configuration.\n",
  2123. val);
  2124. switch (hsotg->hw_params.op_mode) {
  2125. case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
  2126. val = DWC2_CAP_PARAM_HNP_SRP_CAPABLE;
  2127. break;
  2128. case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
  2129. case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
  2130. case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
  2131. val = DWC2_CAP_PARAM_SRP_ONLY_CAPABLE;
  2132. break;
  2133. default:
  2134. val = DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE;
  2135. break;
  2136. }
  2137. dev_dbg(hsotg->dev, "Setting otg_cap to %d\n", val);
  2138. }
  2139. hsotg->core_params->otg_cap = val;
  2140. }
  2141. void dwc2_set_param_dma_enable(struct dwc2_hsotg *hsotg, int val)
  2142. {
  2143. int valid = 1;
  2144. if (val > 0 && hsotg->hw_params.arch == GHWCFG2_SLAVE_ONLY_ARCH)
  2145. valid = 0;
  2146. if (val < 0)
  2147. valid = 0;
  2148. if (!valid) {
  2149. if (val >= 0)
  2150. dev_err(hsotg->dev,
  2151. "%d invalid for dma_enable parameter. Check HW configuration.\n",
  2152. val);
  2153. val = hsotg->hw_params.arch != GHWCFG2_SLAVE_ONLY_ARCH;
  2154. dev_dbg(hsotg->dev, "Setting dma_enable to %d\n", val);
  2155. }
  2156. hsotg->core_params->dma_enable = val;
  2157. }
  2158. void dwc2_set_param_dma_desc_enable(struct dwc2_hsotg *hsotg, int val)
  2159. {
  2160. int valid = 1;
  2161. if (val > 0 && (hsotg->core_params->dma_enable <= 0 ||
  2162. !hsotg->hw_params.dma_desc_enable))
  2163. valid = 0;
  2164. if (val < 0)
  2165. valid = 0;
  2166. if (!valid) {
  2167. if (val >= 0)
  2168. dev_err(hsotg->dev,
  2169. "%d invalid for dma_desc_enable parameter. Check HW configuration.\n",
  2170. val);
  2171. val = (hsotg->core_params->dma_enable > 0 &&
  2172. hsotg->hw_params.dma_desc_enable);
  2173. dev_dbg(hsotg->dev, "Setting dma_desc_enable to %d\n", val);
  2174. }
  2175. hsotg->core_params->dma_desc_enable = val;
  2176. }
  2177. void dwc2_set_param_host_support_fs_ls_low_power(struct dwc2_hsotg *hsotg,
  2178. int val)
  2179. {
  2180. if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
  2181. if (val >= 0) {
  2182. dev_err(hsotg->dev,
  2183. "Wrong value for host_support_fs_low_power\n");
  2184. dev_err(hsotg->dev,
  2185. "host_support_fs_low_power must be 0 or 1\n");
  2186. }
  2187. val = 0;
  2188. dev_dbg(hsotg->dev,
  2189. "Setting host_support_fs_low_power to %d\n", val);
  2190. }
  2191. hsotg->core_params->host_support_fs_ls_low_power = val;
  2192. }
  2193. void dwc2_set_param_enable_dynamic_fifo(struct dwc2_hsotg *hsotg, int val)
  2194. {
  2195. int valid = 1;
  2196. if (val > 0 && !hsotg->hw_params.enable_dynamic_fifo)
  2197. valid = 0;
  2198. if (val < 0)
  2199. valid = 0;
  2200. if (!valid) {
  2201. if (val >= 0)
  2202. dev_err(hsotg->dev,
  2203. "%d invalid for enable_dynamic_fifo parameter. Check HW configuration.\n",
  2204. val);
  2205. val = hsotg->hw_params.enable_dynamic_fifo;
  2206. dev_dbg(hsotg->dev, "Setting enable_dynamic_fifo to %d\n", val);
  2207. }
  2208. hsotg->core_params->enable_dynamic_fifo = val;
  2209. }
  2210. void dwc2_set_param_host_rx_fifo_size(struct dwc2_hsotg *hsotg, int val)
  2211. {
  2212. int valid = 1;
  2213. if (val < 16 || val > hsotg->hw_params.host_rx_fifo_size)
  2214. valid = 0;
  2215. if (!valid) {
  2216. if (val >= 0)
  2217. dev_err(hsotg->dev,
  2218. "%d invalid for host_rx_fifo_size. Check HW configuration.\n",
  2219. val);
  2220. val = hsotg->hw_params.host_rx_fifo_size;
  2221. dev_dbg(hsotg->dev, "Setting host_rx_fifo_size to %d\n", val);
  2222. }
  2223. hsotg->core_params->host_rx_fifo_size = val;
  2224. }
  2225. void dwc2_set_param_host_nperio_tx_fifo_size(struct dwc2_hsotg *hsotg, int val)
  2226. {
  2227. int valid = 1;
  2228. if (val < 16 || val > hsotg->hw_params.host_nperio_tx_fifo_size)
  2229. valid = 0;
  2230. if (!valid) {
  2231. if (val >= 0)
  2232. dev_err(hsotg->dev,
  2233. "%d invalid for host_nperio_tx_fifo_size. Check HW configuration.\n",
  2234. val);
  2235. val = hsotg->hw_params.host_nperio_tx_fifo_size;
  2236. dev_dbg(hsotg->dev, "Setting host_nperio_tx_fifo_size to %d\n",
  2237. val);
  2238. }
  2239. hsotg->core_params->host_nperio_tx_fifo_size = val;
  2240. }
  2241. void dwc2_set_param_host_perio_tx_fifo_size(struct dwc2_hsotg *hsotg, int val)
  2242. {
  2243. int valid = 1;
  2244. if (val < 16 || val > hsotg->hw_params.host_perio_tx_fifo_size)
  2245. valid = 0;
  2246. if (!valid) {
  2247. if (val >= 0)
  2248. dev_err(hsotg->dev,
  2249. "%d invalid for host_perio_tx_fifo_size. Check HW configuration.\n",
  2250. val);
  2251. val = hsotg->hw_params.host_perio_tx_fifo_size;
  2252. dev_dbg(hsotg->dev, "Setting host_perio_tx_fifo_size to %d\n",
  2253. val);
  2254. }
  2255. hsotg->core_params->host_perio_tx_fifo_size = val;
  2256. }
  2257. void dwc2_set_param_max_transfer_size(struct dwc2_hsotg *hsotg, int val)
  2258. {
  2259. int valid = 1;
  2260. if (val < 2047 || val > hsotg->hw_params.max_transfer_size)
  2261. valid = 0;
  2262. if (!valid) {
  2263. if (val >= 0)
  2264. dev_err(hsotg->dev,
  2265. "%d invalid for max_transfer_size. Check HW configuration.\n",
  2266. val);
  2267. val = hsotg->hw_params.max_transfer_size;
  2268. dev_dbg(hsotg->dev, "Setting max_transfer_size to %d\n", val);
  2269. }
  2270. hsotg->core_params->max_transfer_size = val;
  2271. }
  2272. void dwc2_set_param_max_packet_count(struct dwc2_hsotg *hsotg, int val)
  2273. {
  2274. int valid = 1;
  2275. if (val < 15 || val > hsotg->hw_params.max_packet_count)
  2276. valid = 0;
  2277. if (!valid) {
  2278. if (val >= 0)
  2279. dev_err(hsotg->dev,
  2280. "%d invalid for max_packet_count. Check HW configuration.\n",
  2281. val);
  2282. val = hsotg->hw_params.max_packet_count;
  2283. dev_dbg(hsotg->dev, "Setting max_packet_count to %d\n", val);
  2284. }
  2285. hsotg->core_params->max_packet_count = val;
  2286. }
  2287. void dwc2_set_param_host_channels(struct dwc2_hsotg *hsotg, int val)
  2288. {
  2289. int valid = 1;
  2290. if (val < 1 || val > hsotg->hw_params.host_channels)
  2291. valid = 0;
  2292. if (!valid) {
  2293. if (val >= 0)
  2294. dev_err(hsotg->dev,
  2295. "%d invalid for host_channels. Check HW configuration.\n",
  2296. val);
  2297. val = hsotg->hw_params.host_channels;
  2298. dev_dbg(hsotg->dev, "Setting host_channels to %d\n", val);
  2299. }
  2300. hsotg->core_params->host_channels = val;
  2301. }
  2302. void dwc2_set_param_phy_type(struct dwc2_hsotg *hsotg, int val)
  2303. {
  2304. int valid = 0;
  2305. u32 hs_phy_type, fs_phy_type;
  2306. if (DWC2_OUT_OF_BOUNDS(val, DWC2_PHY_TYPE_PARAM_FS,
  2307. DWC2_PHY_TYPE_PARAM_ULPI)) {
  2308. if (val >= 0) {
  2309. dev_err(hsotg->dev, "Wrong value for phy_type\n");
  2310. dev_err(hsotg->dev, "phy_type must be 0, 1 or 2\n");
  2311. }
  2312. valid = 0;
  2313. }
  2314. hs_phy_type = hsotg->hw_params.hs_phy_type;
  2315. fs_phy_type = hsotg->hw_params.fs_phy_type;
  2316. if (val == DWC2_PHY_TYPE_PARAM_UTMI &&
  2317. (hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI ||
  2318. hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI))
  2319. valid = 1;
  2320. else if (val == DWC2_PHY_TYPE_PARAM_ULPI &&
  2321. (hs_phy_type == GHWCFG2_HS_PHY_TYPE_ULPI ||
  2322. hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI))
  2323. valid = 1;
  2324. else if (val == DWC2_PHY_TYPE_PARAM_FS &&
  2325. fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED)
  2326. valid = 1;
  2327. if (!valid) {
  2328. if (val >= 0)
  2329. dev_err(hsotg->dev,
  2330. "%d invalid for phy_type. Check HW configuration.\n",
  2331. val);
  2332. val = DWC2_PHY_TYPE_PARAM_FS;
  2333. if (hs_phy_type != GHWCFG2_HS_PHY_TYPE_NOT_SUPPORTED) {
  2334. if (hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI ||
  2335. hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI)
  2336. val = DWC2_PHY_TYPE_PARAM_UTMI;
  2337. else
  2338. val = DWC2_PHY_TYPE_PARAM_ULPI;
  2339. }
  2340. dev_dbg(hsotg->dev, "Setting phy_type to %d\n", val);
  2341. }
  2342. hsotg->core_params->phy_type = val;
  2343. }
  2344. static int dwc2_get_param_phy_type(struct dwc2_hsotg *hsotg)
  2345. {
  2346. return hsotg->core_params->phy_type;
  2347. }
  2348. void dwc2_set_param_speed(struct dwc2_hsotg *hsotg, int val)
  2349. {
  2350. int valid = 1;
  2351. if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
  2352. if (val >= 0) {
  2353. dev_err(hsotg->dev, "Wrong value for speed parameter\n");
  2354. dev_err(hsotg->dev, "max_speed parameter must be 0 or 1\n");
  2355. }
  2356. valid = 0;
  2357. }
  2358. if (val == DWC2_SPEED_PARAM_HIGH &&
  2359. dwc2_get_param_phy_type(hsotg) == DWC2_PHY_TYPE_PARAM_FS)
  2360. valid = 0;
  2361. if (!valid) {
  2362. if (val >= 0)
  2363. dev_err(hsotg->dev,
  2364. "%d invalid for speed parameter. Check HW configuration.\n",
  2365. val);
  2366. val = dwc2_get_param_phy_type(hsotg) == DWC2_PHY_TYPE_PARAM_FS ?
  2367. DWC2_SPEED_PARAM_FULL : DWC2_SPEED_PARAM_HIGH;
  2368. dev_dbg(hsotg->dev, "Setting speed to %d\n", val);
  2369. }
  2370. hsotg->core_params->speed = val;
  2371. }
  2372. void dwc2_set_param_host_ls_low_power_phy_clk(struct dwc2_hsotg *hsotg, int val)
  2373. {
  2374. int valid = 1;
  2375. if (DWC2_OUT_OF_BOUNDS(val, DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ,
  2376. DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ)) {
  2377. if (val >= 0) {
  2378. dev_err(hsotg->dev,
  2379. "Wrong value for host_ls_low_power_phy_clk parameter\n");
  2380. dev_err(hsotg->dev,
  2381. "host_ls_low_power_phy_clk must be 0 or 1\n");
  2382. }
  2383. valid = 0;
  2384. }
  2385. if (val == DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ &&
  2386. dwc2_get_param_phy_type(hsotg) == DWC2_PHY_TYPE_PARAM_FS)
  2387. valid = 0;
  2388. if (!valid) {
  2389. if (val >= 0)
  2390. dev_err(hsotg->dev,
  2391. "%d invalid for host_ls_low_power_phy_clk. Check HW configuration.\n",
  2392. val);
  2393. val = dwc2_get_param_phy_type(hsotg) == DWC2_PHY_TYPE_PARAM_FS
  2394. ? DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ
  2395. : DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ;
  2396. dev_dbg(hsotg->dev, "Setting host_ls_low_power_phy_clk to %d\n",
  2397. val);
  2398. }
  2399. hsotg->core_params->host_ls_low_power_phy_clk = val;
  2400. }
  2401. void dwc2_set_param_phy_ulpi_ddr(struct dwc2_hsotg *hsotg, int val)
  2402. {
  2403. if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
  2404. if (val >= 0) {
  2405. dev_err(hsotg->dev, "Wrong value for phy_ulpi_ddr\n");
  2406. dev_err(hsotg->dev, "phy_upli_ddr must be 0 or 1\n");
  2407. }
  2408. val = 0;
  2409. dev_dbg(hsotg->dev, "Setting phy_upli_ddr to %d\n", val);
  2410. }
  2411. hsotg->core_params->phy_ulpi_ddr = val;
  2412. }
  2413. void dwc2_set_param_phy_ulpi_ext_vbus(struct dwc2_hsotg *hsotg, int val)
  2414. {
  2415. if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
  2416. if (val >= 0) {
  2417. dev_err(hsotg->dev,
  2418. "Wrong value for phy_ulpi_ext_vbus\n");
  2419. dev_err(hsotg->dev,
  2420. "phy_ulpi_ext_vbus must be 0 or 1\n");
  2421. }
  2422. val = 0;
  2423. dev_dbg(hsotg->dev, "Setting phy_ulpi_ext_vbus to %d\n", val);
  2424. }
  2425. hsotg->core_params->phy_ulpi_ext_vbus = val;
  2426. }
  2427. void dwc2_set_param_phy_utmi_width(struct dwc2_hsotg *hsotg, int val)
  2428. {
  2429. int valid = 0;
  2430. switch (hsotg->hw_params.utmi_phy_data_width) {
  2431. case GHWCFG4_UTMI_PHY_DATA_WIDTH_8:
  2432. valid = (val == 8);
  2433. break;
  2434. case GHWCFG4_UTMI_PHY_DATA_WIDTH_16:
  2435. valid = (val == 16);
  2436. break;
  2437. case GHWCFG4_UTMI_PHY_DATA_WIDTH_8_OR_16:
  2438. valid = (val == 8 || val == 16);
  2439. break;
  2440. }
  2441. if (!valid) {
  2442. if (val >= 0) {
  2443. dev_err(hsotg->dev,
  2444. "%d invalid for phy_utmi_width. Check HW configuration.\n",
  2445. val);
  2446. }
  2447. val = (hsotg->hw_params.utmi_phy_data_width ==
  2448. GHWCFG4_UTMI_PHY_DATA_WIDTH_8) ? 8 : 16;
  2449. dev_dbg(hsotg->dev, "Setting phy_utmi_width to %d\n", val);
  2450. }
  2451. hsotg->core_params->phy_utmi_width = val;
  2452. }
  2453. void dwc2_set_param_ulpi_fs_ls(struct dwc2_hsotg *hsotg, int val)
  2454. {
  2455. if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
  2456. if (val >= 0) {
  2457. dev_err(hsotg->dev, "Wrong value for ulpi_fs_ls\n");
  2458. dev_err(hsotg->dev, "ulpi_fs_ls must be 0 or 1\n");
  2459. }
  2460. val = 0;
  2461. dev_dbg(hsotg->dev, "Setting ulpi_fs_ls to %d\n", val);
  2462. }
  2463. hsotg->core_params->ulpi_fs_ls = val;
  2464. }
  2465. void dwc2_set_param_ts_dline(struct dwc2_hsotg *hsotg, int val)
  2466. {
  2467. if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
  2468. if (val >= 0) {
  2469. dev_err(hsotg->dev, "Wrong value for ts_dline\n");
  2470. dev_err(hsotg->dev, "ts_dline must be 0 or 1\n");
  2471. }
  2472. val = 0;
  2473. dev_dbg(hsotg->dev, "Setting ts_dline to %d\n", val);
  2474. }
  2475. hsotg->core_params->ts_dline = val;
  2476. }
  2477. void dwc2_set_param_i2c_enable(struct dwc2_hsotg *hsotg, int val)
  2478. {
  2479. int valid = 1;
  2480. if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
  2481. if (val >= 0) {
  2482. dev_err(hsotg->dev, "Wrong value for i2c_enable\n");
  2483. dev_err(hsotg->dev, "i2c_enable must be 0 or 1\n");
  2484. }
  2485. valid = 0;
  2486. }
  2487. if (val == 1 && !(hsotg->hw_params.i2c_enable))
  2488. valid = 0;
  2489. if (!valid) {
  2490. if (val >= 0)
  2491. dev_err(hsotg->dev,
  2492. "%d invalid for i2c_enable. Check HW configuration.\n",
  2493. val);
  2494. val = hsotg->hw_params.i2c_enable;
  2495. dev_dbg(hsotg->dev, "Setting i2c_enable to %d\n", val);
  2496. }
  2497. hsotg->core_params->i2c_enable = val;
  2498. }
  2499. void dwc2_set_param_en_multiple_tx_fifo(struct dwc2_hsotg *hsotg, int val)
  2500. {
  2501. int valid = 1;
  2502. if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
  2503. if (val >= 0) {
  2504. dev_err(hsotg->dev,
  2505. "Wrong value for en_multiple_tx_fifo,\n");
  2506. dev_err(hsotg->dev,
  2507. "en_multiple_tx_fifo must be 0 or 1\n");
  2508. }
  2509. valid = 0;
  2510. }
  2511. if (val == 1 && !hsotg->hw_params.en_multiple_tx_fifo)
  2512. valid = 0;
  2513. if (!valid) {
  2514. if (val >= 0)
  2515. dev_err(hsotg->dev,
  2516. "%d invalid for parameter en_multiple_tx_fifo. Check HW configuration.\n",
  2517. val);
  2518. val = hsotg->hw_params.en_multiple_tx_fifo;
  2519. dev_dbg(hsotg->dev, "Setting en_multiple_tx_fifo to %d\n", val);
  2520. }
  2521. hsotg->core_params->en_multiple_tx_fifo = val;
  2522. }
  2523. void dwc2_set_param_reload_ctl(struct dwc2_hsotg *hsotg, int val)
  2524. {
  2525. int valid = 1;
  2526. if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
  2527. if (val >= 0) {
  2528. dev_err(hsotg->dev,
  2529. "'%d' invalid for parameter reload_ctl\n", val);
  2530. dev_err(hsotg->dev, "reload_ctl must be 0 or 1\n");
  2531. }
  2532. valid = 0;
  2533. }
  2534. if (val == 1 && hsotg->hw_params.snpsid < DWC2_CORE_REV_2_92a)
  2535. valid = 0;
  2536. if (!valid) {
  2537. if (val >= 0)
  2538. dev_err(hsotg->dev,
  2539. "%d invalid for parameter reload_ctl. Check HW configuration.\n",
  2540. val);
  2541. val = hsotg->hw_params.snpsid >= DWC2_CORE_REV_2_92a;
  2542. dev_dbg(hsotg->dev, "Setting reload_ctl to %d\n", val);
  2543. }
  2544. hsotg->core_params->reload_ctl = val;
  2545. }
  2546. void dwc2_set_param_ahbcfg(struct dwc2_hsotg *hsotg, int val)
  2547. {
  2548. if (val != -1)
  2549. hsotg->core_params->ahbcfg = val;
  2550. else
  2551. hsotg->core_params->ahbcfg = GAHBCFG_HBSTLEN_INCR4 <<
  2552. GAHBCFG_HBSTLEN_SHIFT;
  2553. }
  2554. void dwc2_set_param_otg_ver(struct dwc2_hsotg *hsotg, int val)
  2555. {
  2556. if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
  2557. if (val >= 0) {
  2558. dev_err(hsotg->dev,
  2559. "'%d' invalid for parameter otg_ver\n", val);
  2560. dev_err(hsotg->dev,
  2561. "otg_ver must be 0 (for OTG 1.3 support) or 1 (for OTG 2.0 support)\n");
  2562. }
  2563. val = 0;
  2564. dev_dbg(hsotg->dev, "Setting otg_ver to %d\n", val);
  2565. }
  2566. hsotg->core_params->otg_ver = val;
  2567. }
  2568. static void dwc2_set_param_uframe_sched(struct dwc2_hsotg *hsotg, int val)
  2569. {
  2570. if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
  2571. if (val >= 0) {
  2572. dev_err(hsotg->dev,
  2573. "'%d' invalid for parameter uframe_sched\n",
  2574. val);
  2575. dev_err(hsotg->dev, "uframe_sched must be 0 or 1\n");
  2576. }
  2577. val = 1;
  2578. dev_dbg(hsotg->dev, "Setting uframe_sched to %d\n", val);
  2579. }
  2580. hsotg->core_params->uframe_sched = val;
  2581. }
  2582. static void dwc2_set_param_external_id_pin_ctl(struct dwc2_hsotg *hsotg,
  2583. int val)
  2584. {
  2585. if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
  2586. if (val >= 0) {
  2587. dev_err(hsotg->dev,
  2588. "'%d' invalid for parameter external_id_pin_ctl\n",
  2589. val);
  2590. dev_err(hsotg->dev, "external_id_pin_ctl must be 0 or 1\n");
  2591. }
  2592. val = 0;
  2593. dev_dbg(hsotg->dev, "Setting external_id_pin_ctl to %d\n", val);
  2594. }
  2595. hsotg->core_params->external_id_pin_ctl = val;
  2596. }
  2597. static void dwc2_set_param_hibernation(struct dwc2_hsotg *hsotg,
  2598. int val)
  2599. {
  2600. if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
  2601. if (val >= 0) {
  2602. dev_err(hsotg->dev,
  2603. "'%d' invalid for parameter hibernation\n",
  2604. val);
  2605. dev_err(hsotg->dev, "hibernation must be 0 or 1\n");
  2606. }
  2607. val = 0;
  2608. dev_dbg(hsotg->dev, "Setting hibernation to %d\n", val);
  2609. }
  2610. hsotg->core_params->hibernation = val;
  2611. }
  2612. /*
  2613. * This function is called during module intialization to pass module parameters
  2614. * for the DWC_otg core.
  2615. */
  2616. void dwc2_set_parameters(struct dwc2_hsotg *hsotg,
  2617. const struct dwc2_core_params *params)
  2618. {
  2619. dev_dbg(hsotg->dev, "%s()\n", __func__);
  2620. dwc2_set_param_otg_cap(hsotg, params->otg_cap);
  2621. dwc2_set_param_dma_enable(hsotg, params->dma_enable);
  2622. dwc2_set_param_dma_desc_enable(hsotg, params->dma_desc_enable);
  2623. dwc2_set_param_host_support_fs_ls_low_power(hsotg,
  2624. params->host_support_fs_ls_low_power);
  2625. dwc2_set_param_enable_dynamic_fifo(hsotg,
  2626. params->enable_dynamic_fifo);
  2627. dwc2_set_param_host_rx_fifo_size(hsotg,
  2628. params->host_rx_fifo_size);
  2629. dwc2_set_param_host_nperio_tx_fifo_size(hsotg,
  2630. params->host_nperio_tx_fifo_size);
  2631. dwc2_set_param_host_perio_tx_fifo_size(hsotg,
  2632. params->host_perio_tx_fifo_size);
  2633. dwc2_set_param_max_transfer_size(hsotg,
  2634. params->max_transfer_size);
  2635. dwc2_set_param_max_packet_count(hsotg,
  2636. params->max_packet_count);
  2637. dwc2_set_param_host_channels(hsotg, params->host_channels);
  2638. dwc2_set_param_phy_type(hsotg, params->phy_type);
  2639. dwc2_set_param_speed(hsotg, params->speed);
  2640. dwc2_set_param_host_ls_low_power_phy_clk(hsotg,
  2641. params->host_ls_low_power_phy_clk);
  2642. dwc2_set_param_phy_ulpi_ddr(hsotg, params->phy_ulpi_ddr);
  2643. dwc2_set_param_phy_ulpi_ext_vbus(hsotg,
  2644. params->phy_ulpi_ext_vbus);
  2645. dwc2_set_param_phy_utmi_width(hsotg, params->phy_utmi_width);
  2646. dwc2_set_param_ulpi_fs_ls(hsotg, params->ulpi_fs_ls);
  2647. dwc2_set_param_ts_dline(hsotg, params->ts_dline);
  2648. dwc2_set_param_i2c_enable(hsotg, params->i2c_enable);
  2649. dwc2_set_param_en_multiple_tx_fifo(hsotg,
  2650. params->en_multiple_tx_fifo);
  2651. dwc2_set_param_reload_ctl(hsotg, params->reload_ctl);
  2652. dwc2_set_param_ahbcfg(hsotg, params->ahbcfg);
  2653. dwc2_set_param_otg_ver(hsotg, params->otg_ver);
  2654. dwc2_set_param_uframe_sched(hsotg, params->uframe_sched);
  2655. dwc2_set_param_external_id_pin_ctl(hsotg, params->external_id_pin_ctl);
  2656. dwc2_set_param_hibernation(hsotg, params->hibernation);
  2657. }
  2658. /**
  2659. * During device initialization, read various hardware configuration
  2660. * registers and interpret the contents.
  2661. */
  2662. int dwc2_get_hwparams(struct dwc2_hsotg *hsotg)
  2663. {
  2664. struct dwc2_hw_params *hw = &hsotg->hw_params;
  2665. unsigned width;
  2666. u32 hwcfg1, hwcfg2, hwcfg3, hwcfg4;
  2667. u32 hptxfsiz, grxfsiz, gnptxfsiz;
  2668. u32 gusbcfg;
  2669. /*
  2670. * Attempt to ensure this device is really a DWC_otg Controller.
  2671. * Read and verify the GSNPSID register contents. The value should be
  2672. * 0x45f42xxx or 0x45f43xxx, which corresponds to either "OT2" or "OT3",
  2673. * as in "OTG version 2.xx" or "OTG version 3.xx".
  2674. */
  2675. hw->snpsid = dwc2_readl(hsotg->regs + GSNPSID);
  2676. if ((hw->snpsid & 0xfffff000) != 0x4f542000 &&
  2677. (hw->snpsid & 0xfffff000) != 0x4f543000) {
  2678. dev_err(hsotg->dev, "Bad value for GSNPSID: 0x%08x\n",
  2679. hw->snpsid);
  2680. return -ENODEV;
  2681. }
  2682. dev_dbg(hsotg->dev, "Core Release: %1x.%1x%1x%1x (snpsid=%x)\n",
  2683. hw->snpsid >> 12 & 0xf, hw->snpsid >> 8 & 0xf,
  2684. hw->snpsid >> 4 & 0xf, hw->snpsid & 0xf, hw->snpsid);
  2685. hwcfg1 = dwc2_readl(hsotg->regs + GHWCFG1);
  2686. hwcfg2 = dwc2_readl(hsotg->regs + GHWCFG2);
  2687. hwcfg3 = dwc2_readl(hsotg->regs + GHWCFG3);
  2688. hwcfg4 = dwc2_readl(hsotg->regs + GHWCFG4);
  2689. grxfsiz = dwc2_readl(hsotg->regs + GRXFSIZ);
  2690. dev_dbg(hsotg->dev, "hwcfg1=%08x\n", hwcfg1);
  2691. dev_dbg(hsotg->dev, "hwcfg2=%08x\n", hwcfg2);
  2692. dev_dbg(hsotg->dev, "hwcfg3=%08x\n", hwcfg3);
  2693. dev_dbg(hsotg->dev, "hwcfg4=%08x\n", hwcfg4);
  2694. dev_dbg(hsotg->dev, "grxfsiz=%08x\n", grxfsiz);
  2695. /* Force host mode to get HPTXFSIZ / GNPTXFSIZ exact power on value */
  2696. gusbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
  2697. gusbcfg |= GUSBCFG_FORCEHOSTMODE;
  2698. dwc2_writel(gusbcfg, hsotg->regs + GUSBCFG);
  2699. usleep_range(100000, 150000);
  2700. gnptxfsiz = dwc2_readl(hsotg->regs + GNPTXFSIZ);
  2701. hptxfsiz = dwc2_readl(hsotg->regs + HPTXFSIZ);
  2702. dev_dbg(hsotg->dev, "gnptxfsiz=%08x\n", gnptxfsiz);
  2703. dev_dbg(hsotg->dev, "hptxfsiz=%08x\n", hptxfsiz);
  2704. gusbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
  2705. gusbcfg &= ~GUSBCFG_FORCEHOSTMODE;
  2706. dwc2_writel(gusbcfg, hsotg->regs + GUSBCFG);
  2707. usleep_range(100000, 150000);
  2708. /* hwcfg2 */
  2709. hw->op_mode = (hwcfg2 & GHWCFG2_OP_MODE_MASK) >>
  2710. GHWCFG2_OP_MODE_SHIFT;
  2711. hw->arch = (hwcfg2 & GHWCFG2_ARCHITECTURE_MASK) >>
  2712. GHWCFG2_ARCHITECTURE_SHIFT;
  2713. hw->enable_dynamic_fifo = !!(hwcfg2 & GHWCFG2_DYNAMIC_FIFO);
  2714. hw->host_channels = 1 + ((hwcfg2 & GHWCFG2_NUM_HOST_CHAN_MASK) >>
  2715. GHWCFG2_NUM_HOST_CHAN_SHIFT);
  2716. hw->hs_phy_type = (hwcfg2 & GHWCFG2_HS_PHY_TYPE_MASK) >>
  2717. GHWCFG2_HS_PHY_TYPE_SHIFT;
  2718. hw->fs_phy_type = (hwcfg2 & GHWCFG2_FS_PHY_TYPE_MASK) >>
  2719. GHWCFG2_FS_PHY_TYPE_SHIFT;
  2720. hw->num_dev_ep = (hwcfg2 & GHWCFG2_NUM_DEV_EP_MASK) >>
  2721. GHWCFG2_NUM_DEV_EP_SHIFT;
  2722. hw->nperio_tx_q_depth =
  2723. (hwcfg2 & GHWCFG2_NONPERIO_TX_Q_DEPTH_MASK) >>
  2724. GHWCFG2_NONPERIO_TX_Q_DEPTH_SHIFT << 1;
  2725. hw->host_perio_tx_q_depth =
  2726. (hwcfg2 & GHWCFG2_HOST_PERIO_TX_Q_DEPTH_MASK) >>
  2727. GHWCFG2_HOST_PERIO_TX_Q_DEPTH_SHIFT << 1;
  2728. hw->dev_token_q_depth =
  2729. (hwcfg2 & GHWCFG2_DEV_TOKEN_Q_DEPTH_MASK) >>
  2730. GHWCFG2_DEV_TOKEN_Q_DEPTH_SHIFT;
  2731. /* hwcfg3 */
  2732. width = (hwcfg3 & GHWCFG3_XFER_SIZE_CNTR_WIDTH_MASK) >>
  2733. GHWCFG3_XFER_SIZE_CNTR_WIDTH_SHIFT;
  2734. hw->max_transfer_size = (1 << (width + 11)) - 1;
  2735. /*
  2736. * Clip max_transfer_size to 65535. dwc2_hc_setup_align_buf() allocates
  2737. * coherent buffers with this size, and if it's too large we can
  2738. * exhaust the coherent DMA pool.
  2739. */
  2740. if (hw->max_transfer_size > 65535)
  2741. hw->max_transfer_size = 65535;
  2742. width = (hwcfg3 & GHWCFG3_PACKET_SIZE_CNTR_WIDTH_MASK) >>
  2743. GHWCFG3_PACKET_SIZE_CNTR_WIDTH_SHIFT;
  2744. hw->max_packet_count = (1 << (width + 4)) - 1;
  2745. hw->i2c_enable = !!(hwcfg3 & GHWCFG3_I2C);
  2746. hw->total_fifo_size = (hwcfg3 & GHWCFG3_DFIFO_DEPTH_MASK) >>
  2747. GHWCFG3_DFIFO_DEPTH_SHIFT;
  2748. /* hwcfg4 */
  2749. hw->en_multiple_tx_fifo = !!(hwcfg4 & GHWCFG4_DED_FIFO_EN);
  2750. hw->num_dev_perio_in_ep = (hwcfg4 & GHWCFG4_NUM_DEV_PERIO_IN_EP_MASK) >>
  2751. GHWCFG4_NUM_DEV_PERIO_IN_EP_SHIFT;
  2752. hw->dma_desc_enable = !!(hwcfg4 & GHWCFG4_DESC_DMA);
  2753. hw->power_optimized = !!(hwcfg4 & GHWCFG4_POWER_OPTIMIZ);
  2754. hw->utmi_phy_data_width = (hwcfg4 & GHWCFG4_UTMI_PHY_DATA_WIDTH_MASK) >>
  2755. GHWCFG4_UTMI_PHY_DATA_WIDTH_SHIFT;
  2756. /* fifo sizes */
  2757. hw->host_rx_fifo_size = (grxfsiz & GRXFSIZ_DEPTH_MASK) >>
  2758. GRXFSIZ_DEPTH_SHIFT;
  2759. hw->host_nperio_tx_fifo_size = (gnptxfsiz & FIFOSIZE_DEPTH_MASK) >>
  2760. FIFOSIZE_DEPTH_SHIFT;
  2761. hw->host_perio_tx_fifo_size = (hptxfsiz & FIFOSIZE_DEPTH_MASK) >>
  2762. FIFOSIZE_DEPTH_SHIFT;
  2763. dev_dbg(hsotg->dev, "Detected values from hardware:\n");
  2764. dev_dbg(hsotg->dev, " op_mode=%d\n",
  2765. hw->op_mode);
  2766. dev_dbg(hsotg->dev, " arch=%d\n",
  2767. hw->arch);
  2768. dev_dbg(hsotg->dev, " dma_desc_enable=%d\n",
  2769. hw->dma_desc_enable);
  2770. dev_dbg(hsotg->dev, " power_optimized=%d\n",
  2771. hw->power_optimized);
  2772. dev_dbg(hsotg->dev, " i2c_enable=%d\n",
  2773. hw->i2c_enable);
  2774. dev_dbg(hsotg->dev, " hs_phy_type=%d\n",
  2775. hw->hs_phy_type);
  2776. dev_dbg(hsotg->dev, " fs_phy_type=%d\n",
  2777. hw->fs_phy_type);
  2778. dev_dbg(hsotg->dev, " utmi_phy_data_width=%d\n",
  2779. hw->utmi_phy_data_width);
  2780. dev_dbg(hsotg->dev, " num_dev_ep=%d\n",
  2781. hw->num_dev_ep);
  2782. dev_dbg(hsotg->dev, " num_dev_perio_in_ep=%d\n",
  2783. hw->num_dev_perio_in_ep);
  2784. dev_dbg(hsotg->dev, " host_channels=%d\n",
  2785. hw->host_channels);
  2786. dev_dbg(hsotg->dev, " max_transfer_size=%d\n",
  2787. hw->max_transfer_size);
  2788. dev_dbg(hsotg->dev, " max_packet_count=%d\n",
  2789. hw->max_packet_count);
  2790. dev_dbg(hsotg->dev, " nperio_tx_q_depth=0x%0x\n",
  2791. hw->nperio_tx_q_depth);
  2792. dev_dbg(hsotg->dev, " host_perio_tx_q_depth=0x%0x\n",
  2793. hw->host_perio_tx_q_depth);
  2794. dev_dbg(hsotg->dev, " dev_token_q_depth=0x%0x\n",
  2795. hw->dev_token_q_depth);
  2796. dev_dbg(hsotg->dev, " enable_dynamic_fifo=%d\n",
  2797. hw->enable_dynamic_fifo);
  2798. dev_dbg(hsotg->dev, " en_multiple_tx_fifo=%d\n",
  2799. hw->en_multiple_tx_fifo);
  2800. dev_dbg(hsotg->dev, " total_fifo_size=%d\n",
  2801. hw->total_fifo_size);
  2802. dev_dbg(hsotg->dev, " host_rx_fifo_size=%d\n",
  2803. hw->host_rx_fifo_size);
  2804. dev_dbg(hsotg->dev, " host_nperio_tx_fifo_size=%d\n",
  2805. hw->host_nperio_tx_fifo_size);
  2806. dev_dbg(hsotg->dev, " host_perio_tx_fifo_size=%d\n",
  2807. hw->host_perio_tx_fifo_size);
  2808. dev_dbg(hsotg->dev, "\n");
  2809. return 0;
  2810. }
  2811. /*
  2812. * Sets all parameters to the given value.
  2813. *
  2814. * Assumes that the dwc2_core_params struct contains only integers.
  2815. */
  2816. void dwc2_set_all_params(struct dwc2_core_params *params, int value)
  2817. {
  2818. int *p = (int *)params;
  2819. size_t size = sizeof(*params) / sizeof(*p);
  2820. int i;
  2821. for (i = 0; i < size; i++)
  2822. p[i] = value;
  2823. }
  2824. u16 dwc2_get_otg_version(struct dwc2_hsotg *hsotg)
  2825. {
  2826. return hsotg->core_params->otg_ver == 1 ? 0x0200 : 0x0103;
  2827. }
  2828. bool dwc2_is_controller_alive(struct dwc2_hsotg *hsotg)
  2829. {
  2830. if (dwc2_readl(hsotg->regs + GSNPSID) == 0xffffffff)
  2831. return false;
  2832. else
  2833. return true;
  2834. }
  2835. /**
  2836. * dwc2_enable_global_interrupts() - Enables the controller's Global
  2837. * Interrupt in the AHB Config register
  2838. *
  2839. * @hsotg: Programming view of DWC_otg controller
  2840. */
  2841. void dwc2_enable_global_interrupts(struct dwc2_hsotg *hsotg)
  2842. {
  2843. u32 ahbcfg = dwc2_readl(hsotg->regs + GAHBCFG);
  2844. ahbcfg |= GAHBCFG_GLBL_INTR_EN;
  2845. dwc2_writel(ahbcfg, hsotg->regs + GAHBCFG);
  2846. }
  2847. /**
  2848. * dwc2_disable_global_interrupts() - Disables the controller's Global
  2849. * Interrupt in the AHB Config register
  2850. *
  2851. * @hsotg: Programming view of DWC_otg controller
  2852. */
  2853. void dwc2_disable_global_interrupts(struct dwc2_hsotg *hsotg)
  2854. {
  2855. u32 ahbcfg = dwc2_readl(hsotg->regs + GAHBCFG);
  2856. ahbcfg &= ~GAHBCFG_GLBL_INTR_EN;
  2857. dwc2_writel(ahbcfg, hsotg->regs + GAHBCFG);
  2858. }
  2859. MODULE_DESCRIPTION("DESIGNWARE HS OTG Core");
  2860. MODULE_AUTHOR("Synopsys, Inc.");
  2861. MODULE_LICENSE("Dual BSD/GPL");