core.c 93 KB

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