hcd.c 159 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759
  1. // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
  2. /*
  3. * hcd.c - DesignWare HS OTG Controller host-mode routines
  4. *
  5. * Copyright (C) 2004-2013 Synopsys, Inc.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions, and the following disclaimer,
  12. * without modification.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. The names of the above-listed copyright holders may not be used
  17. * to endorse or promote products derived from this software without
  18. * specific prior written permission.
  19. *
  20. * ALTERNATIVELY, this software may be distributed under the terms of the
  21. * GNU General Public License ("GPL") as published by the Free Software
  22. * Foundation; either version 2 of the License, or (at your option) any
  23. * later version.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  26. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  27. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  28. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  29. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  30. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  31. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  32. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  33. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  34. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. */
  37. /*
  38. * This file contains the core HCD code, and implements the Linux hc_driver
  39. * API
  40. */
  41. #include <linux/kernel.h>
  42. #include <linux/module.h>
  43. #include <linux/spinlock.h>
  44. #include <linux/interrupt.h>
  45. #include <linux/platform_device.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. static void dwc2_port_resume(struct dwc2_hsotg *hsotg);
  56. /*
  57. * =========================================================================
  58. * Host Core Layer Functions
  59. * =========================================================================
  60. */
  61. /**
  62. * dwc2_enable_common_interrupts() - Initializes the commmon interrupts,
  63. * used in both device and host modes
  64. *
  65. * @hsotg: Programming view of the DWC_otg controller
  66. */
  67. static void dwc2_enable_common_interrupts(struct dwc2_hsotg *hsotg)
  68. {
  69. u32 intmsk;
  70. /* Clear any pending OTG Interrupts */
  71. dwc2_writel(hsotg, 0xffffffff, GOTGINT);
  72. /* Clear any pending interrupts */
  73. dwc2_writel(hsotg, 0xffffffff, GINTSTS);
  74. /* Enable the interrupts in the GINTMSK */
  75. intmsk = GINTSTS_MODEMIS | GINTSTS_OTGINT;
  76. if (!hsotg->params.host_dma)
  77. intmsk |= GINTSTS_RXFLVL;
  78. if (!hsotg->params.external_id_pin_ctl)
  79. intmsk |= GINTSTS_CONIDSTSCHNG;
  80. intmsk |= GINTSTS_WKUPINT | GINTSTS_USBSUSP |
  81. GINTSTS_SESSREQINT;
  82. if (dwc2_is_device_mode(hsotg) && hsotg->params.lpm)
  83. intmsk |= GINTSTS_LPMTRANRCVD;
  84. dwc2_writel(hsotg, intmsk, GINTMSK);
  85. }
  86. /*
  87. * Initializes the FSLSPClkSel field of the HCFG register depending on the
  88. * PHY type
  89. */
  90. static void dwc2_init_fs_ls_pclk_sel(struct dwc2_hsotg *hsotg)
  91. {
  92. u32 hcfg, val;
  93. if ((hsotg->hw_params.hs_phy_type == GHWCFG2_HS_PHY_TYPE_ULPI &&
  94. hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED &&
  95. hsotg->params.ulpi_fs_ls) ||
  96. hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS) {
  97. /* Full speed PHY */
  98. val = HCFG_FSLSPCLKSEL_48_MHZ;
  99. } else {
  100. /* High speed PHY running at full speed or high speed */
  101. val = HCFG_FSLSPCLKSEL_30_60_MHZ;
  102. }
  103. dev_dbg(hsotg->dev, "Initializing HCFG.FSLSPClkSel to %08x\n", val);
  104. hcfg = dwc2_readl(hsotg, HCFG);
  105. hcfg &= ~HCFG_FSLSPCLKSEL_MASK;
  106. hcfg |= val << HCFG_FSLSPCLKSEL_SHIFT;
  107. dwc2_writel(hsotg, hcfg, HCFG);
  108. }
  109. static int dwc2_fs_phy_init(struct dwc2_hsotg *hsotg, bool select_phy)
  110. {
  111. u32 usbcfg, ggpio, i2cctl;
  112. int retval = 0;
  113. /*
  114. * core_init() is now called on every switch so only call the
  115. * following for the first time through
  116. */
  117. if (select_phy) {
  118. dev_dbg(hsotg->dev, "FS PHY selected\n");
  119. usbcfg = dwc2_readl(hsotg, GUSBCFG);
  120. if (!(usbcfg & GUSBCFG_PHYSEL)) {
  121. usbcfg |= GUSBCFG_PHYSEL;
  122. dwc2_writel(hsotg, usbcfg, GUSBCFG);
  123. /* Reset after a PHY select */
  124. retval = dwc2_core_reset(hsotg, false);
  125. if (retval) {
  126. dev_err(hsotg->dev,
  127. "%s: Reset failed, aborting", __func__);
  128. return retval;
  129. }
  130. }
  131. if (hsotg->params.activate_stm_fs_transceiver) {
  132. ggpio = dwc2_readl(hsotg, GGPIO);
  133. if (!(ggpio & GGPIO_STM32_OTG_GCCFG_PWRDWN)) {
  134. dev_dbg(hsotg->dev, "Activating transceiver\n");
  135. /*
  136. * STM32F4x9 uses the GGPIO register as general
  137. * core configuration register.
  138. */
  139. ggpio |= GGPIO_STM32_OTG_GCCFG_PWRDWN;
  140. dwc2_writel(hsotg, ggpio, GGPIO);
  141. }
  142. }
  143. }
  144. /*
  145. * Program DCFG.DevSpd or HCFG.FSLSPclkSel to 48Mhz in FS. Also
  146. * do this on HNP Dev/Host mode switches (done in dev_init and
  147. * host_init).
  148. */
  149. if (dwc2_is_host_mode(hsotg))
  150. dwc2_init_fs_ls_pclk_sel(hsotg);
  151. if (hsotg->params.i2c_enable) {
  152. dev_dbg(hsotg->dev, "FS PHY enabling I2C\n");
  153. /* Program GUSBCFG.OtgUtmiFsSel to I2C */
  154. usbcfg = dwc2_readl(hsotg, GUSBCFG);
  155. usbcfg |= GUSBCFG_OTG_UTMI_FS_SEL;
  156. dwc2_writel(hsotg, usbcfg, GUSBCFG);
  157. /* Program GI2CCTL.I2CEn */
  158. i2cctl = dwc2_readl(hsotg, GI2CCTL);
  159. i2cctl &= ~GI2CCTL_I2CDEVADDR_MASK;
  160. i2cctl |= 1 << GI2CCTL_I2CDEVADDR_SHIFT;
  161. i2cctl &= ~GI2CCTL_I2CEN;
  162. dwc2_writel(hsotg, i2cctl, GI2CCTL);
  163. i2cctl |= GI2CCTL_I2CEN;
  164. dwc2_writel(hsotg, i2cctl, GI2CCTL);
  165. }
  166. return retval;
  167. }
  168. static int dwc2_hs_phy_init(struct dwc2_hsotg *hsotg, bool select_phy)
  169. {
  170. u32 usbcfg, usbcfg_old;
  171. int retval = 0;
  172. if (!select_phy)
  173. return 0;
  174. usbcfg = dwc2_readl(hsotg, GUSBCFG);
  175. usbcfg_old = usbcfg;
  176. /*
  177. * HS PHY parameters. These parameters are preserved during soft reset
  178. * so only program the first time. Do a soft reset immediately after
  179. * setting phyif.
  180. */
  181. switch (hsotg->params.phy_type) {
  182. case DWC2_PHY_TYPE_PARAM_ULPI:
  183. /* ULPI interface */
  184. dev_dbg(hsotg->dev, "HS ULPI PHY selected\n");
  185. usbcfg |= GUSBCFG_ULPI_UTMI_SEL;
  186. usbcfg &= ~(GUSBCFG_PHYIF16 | GUSBCFG_DDRSEL);
  187. if (hsotg->params.phy_ulpi_ddr)
  188. usbcfg |= GUSBCFG_DDRSEL;
  189. /* Set external VBUS indicator as needed. */
  190. if (hsotg->params.oc_disable)
  191. usbcfg |= (GUSBCFG_ULPI_INT_VBUS_IND |
  192. GUSBCFG_INDICATORPASSTHROUGH);
  193. break;
  194. case DWC2_PHY_TYPE_PARAM_UTMI:
  195. /* UTMI+ interface */
  196. dev_dbg(hsotg->dev, "HS UTMI+ PHY selected\n");
  197. usbcfg &= ~(GUSBCFG_ULPI_UTMI_SEL | GUSBCFG_PHYIF16);
  198. if (hsotg->params.phy_utmi_width == 16)
  199. usbcfg |= GUSBCFG_PHYIF16;
  200. break;
  201. default:
  202. dev_err(hsotg->dev, "FS PHY selected at HS!\n");
  203. break;
  204. }
  205. if (usbcfg != usbcfg_old) {
  206. dwc2_writel(hsotg, usbcfg, GUSBCFG);
  207. /* Reset after setting the PHY parameters */
  208. retval = dwc2_core_reset(hsotg, false);
  209. if (retval) {
  210. dev_err(hsotg->dev,
  211. "%s: Reset failed, aborting", __func__);
  212. return retval;
  213. }
  214. }
  215. return retval;
  216. }
  217. static int dwc2_phy_init(struct dwc2_hsotg *hsotg, bool select_phy)
  218. {
  219. u32 usbcfg;
  220. int retval = 0;
  221. if ((hsotg->params.speed == DWC2_SPEED_PARAM_FULL ||
  222. hsotg->params.speed == DWC2_SPEED_PARAM_LOW) &&
  223. hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS) {
  224. /* If FS/LS mode with FS/LS PHY */
  225. retval = dwc2_fs_phy_init(hsotg, select_phy);
  226. if (retval)
  227. return retval;
  228. } else {
  229. /* High speed PHY */
  230. retval = dwc2_hs_phy_init(hsotg, select_phy);
  231. if (retval)
  232. return retval;
  233. }
  234. if (hsotg->hw_params.hs_phy_type == GHWCFG2_HS_PHY_TYPE_ULPI &&
  235. hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED &&
  236. hsotg->params.ulpi_fs_ls) {
  237. dev_dbg(hsotg->dev, "Setting ULPI FSLS\n");
  238. usbcfg = dwc2_readl(hsotg, GUSBCFG);
  239. usbcfg |= GUSBCFG_ULPI_FS_LS;
  240. usbcfg |= GUSBCFG_ULPI_CLK_SUSP_M;
  241. dwc2_writel(hsotg, usbcfg, GUSBCFG);
  242. } else {
  243. usbcfg = dwc2_readl(hsotg, GUSBCFG);
  244. usbcfg &= ~GUSBCFG_ULPI_FS_LS;
  245. usbcfg &= ~GUSBCFG_ULPI_CLK_SUSP_M;
  246. dwc2_writel(hsotg, usbcfg, GUSBCFG);
  247. }
  248. return retval;
  249. }
  250. static int dwc2_gahbcfg_init(struct dwc2_hsotg *hsotg)
  251. {
  252. u32 ahbcfg = dwc2_readl(hsotg, GAHBCFG);
  253. switch (hsotg->hw_params.arch) {
  254. case GHWCFG2_EXT_DMA_ARCH:
  255. dev_err(hsotg->dev, "External DMA Mode not supported\n");
  256. return -EINVAL;
  257. case GHWCFG2_INT_DMA_ARCH:
  258. dev_dbg(hsotg->dev, "Internal DMA Mode\n");
  259. if (hsotg->params.ahbcfg != -1) {
  260. ahbcfg &= GAHBCFG_CTRL_MASK;
  261. ahbcfg |= hsotg->params.ahbcfg &
  262. ~GAHBCFG_CTRL_MASK;
  263. }
  264. break;
  265. case GHWCFG2_SLAVE_ONLY_ARCH:
  266. default:
  267. dev_dbg(hsotg->dev, "Slave Only Mode\n");
  268. break;
  269. }
  270. if (hsotg->params.host_dma)
  271. ahbcfg |= GAHBCFG_DMA_EN;
  272. else
  273. hsotg->params.dma_desc_enable = false;
  274. dwc2_writel(hsotg, ahbcfg, GAHBCFG);
  275. return 0;
  276. }
  277. static void dwc2_gusbcfg_init(struct dwc2_hsotg *hsotg)
  278. {
  279. u32 usbcfg;
  280. usbcfg = dwc2_readl(hsotg, GUSBCFG);
  281. usbcfg &= ~(GUSBCFG_HNPCAP | GUSBCFG_SRPCAP);
  282. switch (hsotg->hw_params.op_mode) {
  283. case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
  284. if (hsotg->params.otg_cap ==
  285. DWC2_CAP_PARAM_HNP_SRP_CAPABLE)
  286. usbcfg |= GUSBCFG_HNPCAP;
  287. if (hsotg->params.otg_cap !=
  288. DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE)
  289. usbcfg |= GUSBCFG_SRPCAP;
  290. break;
  291. case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
  292. case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
  293. case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
  294. if (hsotg->params.otg_cap !=
  295. DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE)
  296. usbcfg |= GUSBCFG_SRPCAP;
  297. break;
  298. case GHWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE:
  299. case GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE:
  300. case GHWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST:
  301. default:
  302. break;
  303. }
  304. dwc2_writel(hsotg, usbcfg, GUSBCFG);
  305. }
  306. static int dwc2_vbus_supply_init(struct dwc2_hsotg *hsotg)
  307. {
  308. int ret;
  309. hsotg->vbus_supply = devm_regulator_get_optional(hsotg->dev, "vbus");
  310. if (IS_ERR(hsotg->vbus_supply)) {
  311. ret = PTR_ERR(hsotg->vbus_supply);
  312. hsotg->vbus_supply = NULL;
  313. return ret == -ENODEV ? 0 : ret;
  314. }
  315. return regulator_enable(hsotg->vbus_supply);
  316. }
  317. static int dwc2_vbus_supply_exit(struct dwc2_hsotg *hsotg)
  318. {
  319. if (hsotg->vbus_supply)
  320. return regulator_disable(hsotg->vbus_supply);
  321. return 0;
  322. }
  323. /**
  324. * dwc2_enable_host_interrupts() - Enables the Host mode interrupts
  325. *
  326. * @hsotg: Programming view of DWC_otg controller
  327. */
  328. static void dwc2_enable_host_interrupts(struct dwc2_hsotg *hsotg)
  329. {
  330. u32 intmsk;
  331. dev_dbg(hsotg->dev, "%s()\n", __func__);
  332. /* Disable all interrupts */
  333. dwc2_writel(hsotg, 0, GINTMSK);
  334. dwc2_writel(hsotg, 0, HAINTMSK);
  335. /* Enable the common interrupts */
  336. dwc2_enable_common_interrupts(hsotg);
  337. /* Enable host mode interrupts without disturbing common interrupts */
  338. intmsk = dwc2_readl(hsotg, GINTMSK);
  339. intmsk |= GINTSTS_DISCONNINT | GINTSTS_PRTINT | GINTSTS_HCHINT;
  340. dwc2_writel(hsotg, intmsk, GINTMSK);
  341. }
  342. /**
  343. * dwc2_disable_host_interrupts() - Disables the Host Mode interrupts
  344. *
  345. * @hsotg: Programming view of DWC_otg controller
  346. */
  347. static void dwc2_disable_host_interrupts(struct dwc2_hsotg *hsotg)
  348. {
  349. u32 intmsk = dwc2_readl(hsotg, GINTMSK);
  350. /* Disable host mode interrupts without disturbing common interrupts */
  351. intmsk &= ~(GINTSTS_SOF | GINTSTS_PRTINT | GINTSTS_HCHINT |
  352. GINTSTS_PTXFEMP | GINTSTS_NPTXFEMP | GINTSTS_DISCONNINT);
  353. dwc2_writel(hsotg, intmsk, GINTMSK);
  354. }
  355. /*
  356. * dwc2_calculate_dynamic_fifo() - Calculates the default fifo size
  357. * For system that have a total fifo depth that is smaller than the default
  358. * RX + TX fifo size.
  359. *
  360. * @hsotg: Programming view of DWC_otg controller
  361. */
  362. static void dwc2_calculate_dynamic_fifo(struct dwc2_hsotg *hsotg)
  363. {
  364. struct dwc2_core_params *params = &hsotg->params;
  365. struct dwc2_hw_params *hw = &hsotg->hw_params;
  366. u32 rxfsiz, nptxfsiz, ptxfsiz, total_fifo_size;
  367. total_fifo_size = hw->total_fifo_size;
  368. rxfsiz = params->host_rx_fifo_size;
  369. nptxfsiz = params->host_nperio_tx_fifo_size;
  370. ptxfsiz = params->host_perio_tx_fifo_size;
  371. /*
  372. * Will use Method 2 defined in the DWC2 spec: minimum FIFO depth
  373. * allocation with support for high bandwidth endpoints. Synopsys
  374. * defines MPS(Max Packet size) for a periodic EP=1024, and for
  375. * non-periodic as 512.
  376. */
  377. if (total_fifo_size < (rxfsiz + nptxfsiz + ptxfsiz)) {
  378. /*
  379. * For Buffer DMA mode/Scatter Gather DMA mode
  380. * 2 * ((Largest Packet size / 4) + 1 + 1) + n
  381. * with n = number of host channel.
  382. * 2 * ((1024/4) + 2) = 516
  383. */
  384. rxfsiz = 516 + hw->host_channels;
  385. /*
  386. * min non-periodic tx fifo depth
  387. * 2 * (largest non-periodic USB packet used / 4)
  388. * 2 * (512/4) = 256
  389. */
  390. nptxfsiz = 256;
  391. /*
  392. * min periodic tx fifo depth
  393. * (largest packet size*MC)/4
  394. * (1024 * 3)/4 = 768
  395. */
  396. ptxfsiz = 768;
  397. params->host_rx_fifo_size = rxfsiz;
  398. params->host_nperio_tx_fifo_size = nptxfsiz;
  399. params->host_perio_tx_fifo_size = ptxfsiz;
  400. }
  401. /*
  402. * If the summation of RX, NPTX and PTX fifo sizes is still
  403. * bigger than the total_fifo_size, then we have a problem.
  404. *
  405. * We won't be able to allocate as many endpoints. Right now,
  406. * we're just printing an error message, but ideally this FIFO
  407. * allocation algorithm would be improved in the future.
  408. *
  409. * FIXME improve this FIFO allocation algorithm.
  410. */
  411. if (unlikely(total_fifo_size < (rxfsiz + nptxfsiz + ptxfsiz)))
  412. dev_err(hsotg->dev, "invalid fifo sizes\n");
  413. }
  414. static void dwc2_config_fifos(struct dwc2_hsotg *hsotg)
  415. {
  416. struct dwc2_core_params *params = &hsotg->params;
  417. u32 nptxfsiz, hptxfsiz, dfifocfg, grxfsiz;
  418. if (!params->enable_dynamic_fifo)
  419. return;
  420. dwc2_calculate_dynamic_fifo(hsotg);
  421. /* Rx FIFO */
  422. grxfsiz = dwc2_readl(hsotg, GRXFSIZ);
  423. dev_dbg(hsotg->dev, "initial grxfsiz=%08x\n", grxfsiz);
  424. grxfsiz &= ~GRXFSIZ_DEPTH_MASK;
  425. grxfsiz |= params->host_rx_fifo_size <<
  426. GRXFSIZ_DEPTH_SHIFT & GRXFSIZ_DEPTH_MASK;
  427. dwc2_writel(hsotg, grxfsiz, GRXFSIZ);
  428. dev_dbg(hsotg->dev, "new grxfsiz=%08x\n",
  429. dwc2_readl(hsotg, GRXFSIZ));
  430. /* Non-periodic Tx FIFO */
  431. dev_dbg(hsotg->dev, "initial gnptxfsiz=%08x\n",
  432. dwc2_readl(hsotg, GNPTXFSIZ));
  433. nptxfsiz = params->host_nperio_tx_fifo_size <<
  434. FIFOSIZE_DEPTH_SHIFT & FIFOSIZE_DEPTH_MASK;
  435. nptxfsiz |= params->host_rx_fifo_size <<
  436. FIFOSIZE_STARTADDR_SHIFT & FIFOSIZE_STARTADDR_MASK;
  437. dwc2_writel(hsotg, nptxfsiz, GNPTXFSIZ);
  438. dev_dbg(hsotg->dev, "new gnptxfsiz=%08x\n",
  439. dwc2_readl(hsotg, GNPTXFSIZ));
  440. /* Periodic Tx FIFO */
  441. dev_dbg(hsotg->dev, "initial hptxfsiz=%08x\n",
  442. dwc2_readl(hsotg, HPTXFSIZ));
  443. hptxfsiz = params->host_perio_tx_fifo_size <<
  444. FIFOSIZE_DEPTH_SHIFT & FIFOSIZE_DEPTH_MASK;
  445. hptxfsiz |= (params->host_rx_fifo_size +
  446. params->host_nperio_tx_fifo_size) <<
  447. FIFOSIZE_STARTADDR_SHIFT & FIFOSIZE_STARTADDR_MASK;
  448. dwc2_writel(hsotg, hptxfsiz, HPTXFSIZ);
  449. dev_dbg(hsotg->dev, "new hptxfsiz=%08x\n",
  450. dwc2_readl(hsotg, HPTXFSIZ));
  451. if (hsotg->params.en_multiple_tx_fifo &&
  452. hsotg->hw_params.snpsid >= DWC2_CORE_REV_2_91a) {
  453. /*
  454. * This feature was implemented in 2.91a version
  455. * Global DFIFOCFG calculation for Host mode -
  456. * include RxFIFO, NPTXFIFO and HPTXFIFO
  457. */
  458. dfifocfg = dwc2_readl(hsotg, GDFIFOCFG);
  459. dfifocfg &= ~GDFIFOCFG_EPINFOBASE_MASK;
  460. dfifocfg |= (params->host_rx_fifo_size +
  461. params->host_nperio_tx_fifo_size +
  462. params->host_perio_tx_fifo_size) <<
  463. GDFIFOCFG_EPINFOBASE_SHIFT &
  464. GDFIFOCFG_EPINFOBASE_MASK;
  465. dwc2_writel(hsotg, dfifocfg, GDFIFOCFG);
  466. }
  467. }
  468. /**
  469. * dwc2_calc_frame_interval() - Calculates the correct frame Interval value for
  470. * the HFIR register according to PHY type and speed
  471. *
  472. * @hsotg: Programming view of DWC_otg controller
  473. *
  474. * NOTE: The caller can modify the value of the HFIR register only after the
  475. * Port Enable bit of the Host Port Control and Status register (HPRT.EnaPort)
  476. * has been set
  477. */
  478. u32 dwc2_calc_frame_interval(struct dwc2_hsotg *hsotg)
  479. {
  480. u32 usbcfg;
  481. u32 hprt0;
  482. int clock = 60; /* default value */
  483. usbcfg = dwc2_readl(hsotg, GUSBCFG);
  484. hprt0 = dwc2_readl(hsotg, HPRT0);
  485. if (!(usbcfg & GUSBCFG_PHYSEL) && (usbcfg & GUSBCFG_ULPI_UTMI_SEL) &&
  486. !(usbcfg & GUSBCFG_PHYIF16))
  487. clock = 60;
  488. if ((usbcfg & GUSBCFG_PHYSEL) && hsotg->hw_params.fs_phy_type ==
  489. GHWCFG2_FS_PHY_TYPE_SHARED_ULPI)
  490. clock = 48;
  491. if (!(usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
  492. !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && (usbcfg & GUSBCFG_PHYIF16))
  493. clock = 30;
  494. if (!(usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
  495. !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && !(usbcfg & GUSBCFG_PHYIF16))
  496. clock = 60;
  497. if ((usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
  498. !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && (usbcfg & GUSBCFG_PHYIF16))
  499. clock = 48;
  500. if ((usbcfg & GUSBCFG_PHYSEL) && !(usbcfg & GUSBCFG_PHYIF16) &&
  501. hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_SHARED_UTMI)
  502. clock = 48;
  503. if ((usbcfg & GUSBCFG_PHYSEL) &&
  504. hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED)
  505. clock = 48;
  506. if ((hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT == HPRT0_SPD_HIGH_SPEED)
  507. /* High speed case */
  508. return 125 * clock - 1;
  509. /* FS/LS case */
  510. return 1000 * clock - 1;
  511. }
  512. /**
  513. * dwc2_read_packet() - Reads a packet from the Rx FIFO into the destination
  514. * buffer
  515. *
  516. * @hsotg: Programming view of DWC_otg controller
  517. * @dest: Destination buffer for the packet
  518. * @bytes: Number of bytes to copy to the destination
  519. */
  520. void dwc2_read_packet(struct dwc2_hsotg *hsotg, u8 *dest, u16 bytes)
  521. {
  522. u32 *data_buf = (u32 *)dest;
  523. int word_count = (bytes + 3) / 4;
  524. int i;
  525. /*
  526. * Todo: Account for the case where dest is not dword aligned. This
  527. * requires reading data from the FIFO into a u32 temp buffer, then
  528. * moving it into the data buffer.
  529. */
  530. dev_vdbg(hsotg->dev, "%s(%p,%p,%d)\n", __func__, hsotg, dest, bytes);
  531. for (i = 0; i < word_count; i++, data_buf++)
  532. *data_buf = dwc2_readl(hsotg, HCFIFO(0));
  533. }
  534. /**
  535. * dwc2_dump_channel_info() - Prints the state of a host channel
  536. *
  537. * @hsotg: Programming view of DWC_otg controller
  538. * @chan: Pointer to the channel to dump
  539. *
  540. * Must be called with interrupt disabled and spinlock held
  541. *
  542. * NOTE: This function will be removed once the peripheral controller code
  543. * is integrated and the driver is stable
  544. */
  545. static void dwc2_dump_channel_info(struct dwc2_hsotg *hsotg,
  546. struct dwc2_host_chan *chan)
  547. {
  548. #ifdef VERBOSE_DEBUG
  549. int num_channels = hsotg->params.host_channels;
  550. struct dwc2_qh *qh;
  551. u32 hcchar;
  552. u32 hcsplt;
  553. u32 hctsiz;
  554. u32 hc_dma;
  555. int i;
  556. if (!chan)
  557. return;
  558. hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num));
  559. hcsplt = dwc2_readl(hsotg, HCSPLT(chan->hc_num));
  560. hctsiz = dwc2_readl(hsotg, HCTSIZ(chan->hc_num));
  561. hc_dma = dwc2_readl(hsotg, HCDMA(chan->hc_num));
  562. dev_dbg(hsotg->dev, " Assigned to channel %p:\n", chan);
  563. dev_dbg(hsotg->dev, " hcchar 0x%08x, hcsplt 0x%08x\n",
  564. hcchar, hcsplt);
  565. dev_dbg(hsotg->dev, " hctsiz 0x%08x, hc_dma 0x%08x\n",
  566. hctsiz, hc_dma);
  567. dev_dbg(hsotg->dev, " dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
  568. chan->dev_addr, chan->ep_num, chan->ep_is_in);
  569. dev_dbg(hsotg->dev, " ep_type: %d\n", chan->ep_type);
  570. dev_dbg(hsotg->dev, " max_packet: %d\n", chan->max_packet);
  571. dev_dbg(hsotg->dev, " data_pid_start: %d\n", chan->data_pid_start);
  572. dev_dbg(hsotg->dev, " xfer_started: %d\n", chan->xfer_started);
  573. dev_dbg(hsotg->dev, " halt_status: %d\n", chan->halt_status);
  574. dev_dbg(hsotg->dev, " xfer_buf: %p\n", chan->xfer_buf);
  575. dev_dbg(hsotg->dev, " xfer_dma: %08lx\n",
  576. (unsigned long)chan->xfer_dma);
  577. dev_dbg(hsotg->dev, " xfer_len: %d\n", chan->xfer_len);
  578. dev_dbg(hsotg->dev, " qh: %p\n", chan->qh);
  579. dev_dbg(hsotg->dev, " NP inactive sched:\n");
  580. list_for_each_entry(qh, &hsotg->non_periodic_sched_inactive,
  581. qh_list_entry)
  582. dev_dbg(hsotg->dev, " %p\n", qh);
  583. dev_dbg(hsotg->dev, " NP waiting sched:\n");
  584. list_for_each_entry(qh, &hsotg->non_periodic_sched_waiting,
  585. qh_list_entry)
  586. dev_dbg(hsotg->dev, " %p\n", qh);
  587. dev_dbg(hsotg->dev, " NP active sched:\n");
  588. list_for_each_entry(qh, &hsotg->non_periodic_sched_active,
  589. qh_list_entry)
  590. dev_dbg(hsotg->dev, " %p\n", qh);
  591. dev_dbg(hsotg->dev, " Channels:\n");
  592. for (i = 0; i < num_channels; i++) {
  593. struct dwc2_host_chan *chan = hsotg->hc_ptr_array[i];
  594. dev_dbg(hsotg->dev, " %2d: %p\n", i, chan);
  595. }
  596. #endif /* VERBOSE_DEBUG */
  597. }
  598. static int _dwc2_hcd_start(struct usb_hcd *hcd);
  599. static void dwc2_host_start(struct dwc2_hsotg *hsotg)
  600. {
  601. struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
  602. hcd->self.is_b_host = dwc2_hcd_is_b_host(hsotg);
  603. _dwc2_hcd_start(hcd);
  604. }
  605. static void dwc2_host_disconnect(struct dwc2_hsotg *hsotg)
  606. {
  607. struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
  608. hcd->self.is_b_host = 0;
  609. }
  610. static void dwc2_host_hub_info(struct dwc2_hsotg *hsotg, void *context,
  611. int *hub_addr, int *hub_port)
  612. {
  613. struct urb *urb = context;
  614. if (urb->dev->tt)
  615. *hub_addr = urb->dev->tt->hub->devnum;
  616. else
  617. *hub_addr = 0;
  618. *hub_port = urb->dev->ttport;
  619. }
  620. /*
  621. * =========================================================================
  622. * Low Level Host Channel Access Functions
  623. * =========================================================================
  624. */
  625. static void dwc2_hc_enable_slave_ints(struct dwc2_hsotg *hsotg,
  626. struct dwc2_host_chan *chan)
  627. {
  628. u32 hcintmsk = HCINTMSK_CHHLTD;
  629. switch (chan->ep_type) {
  630. case USB_ENDPOINT_XFER_CONTROL:
  631. case USB_ENDPOINT_XFER_BULK:
  632. dev_vdbg(hsotg->dev, "control/bulk\n");
  633. hcintmsk |= HCINTMSK_XFERCOMPL;
  634. hcintmsk |= HCINTMSK_STALL;
  635. hcintmsk |= HCINTMSK_XACTERR;
  636. hcintmsk |= HCINTMSK_DATATGLERR;
  637. if (chan->ep_is_in) {
  638. hcintmsk |= HCINTMSK_BBLERR;
  639. } else {
  640. hcintmsk |= HCINTMSK_NAK;
  641. hcintmsk |= HCINTMSK_NYET;
  642. if (chan->do_ping)
  643. hcintmsk |= HCINTMSK_ACK;
  644. }
  645. if (chan->do_split) {
  646. hcintmsk |= HCINTMSK_NAK;
  647. if (chan->complete_split)
  648. hcintmsk |= HCINTMSK_NYET;
  649. else
  650. hcintmsk |= HCINTMSK_ACK;
  651. }
  652. if (chan->error_state)
  653. hcintmsk |= HCINTMSK_ACK;
  654. break;
  655. case USB_ENDPOINT_XFER_INT:
  656. if (dbg_perio())
  657. dev_vdbg(hsotg->dev, "intr\n");
  658. hcintmsk |= HCINTMSK_XFERCOMPL;
  659. hcintmsk |= HCINTMSK_NAK;
  660. hcintmsk |= HCINTMSK_STALL;
  661. hcintmsk |= HCINTMSK_XACTERR;
  662. hcintmsk |= HCINTMSK_DATATGLERR;
  663. hcintmsk |= HCINTMSK_FRMOVRUN;
  664. if (chan->ep_is_in)
  665. hcintmsk |= HCINTMSK_BBLERR;
  666. if (chan->error_state)
  667. hcintmsk |= HCINTMSK_ACK;
  668. if (chan->do_split) {
  669. if (chan->complete_split)
  670. hcintmsk |= HCINTMSK_NYET;
  671. else
  672. hcintmsk |= HCINTMSK_ACK;
  673. }
  674. break;
  675. case USB_ENDPOINT_XFER_ISOC:
  676. if (dbg_perio())
  677. dev_vdbg(hsotg->dev, "isoc\n");
  678. hcintmsk |= HCINTMSK_XFERCOMPL;
  679. hcintmsk |= HCINTMSK_FRMOVRUN;
  680. hcintmsk |= HCINTMSK_ACK;
  681. if (chan->ep_is_in) {
  682. hcintmsk |= HCINTMSK_XACTERR;
  683. hcintmsk |= HCINTMSK_BBLERR;
  684. }
  685. break;
  686. default:
  687. dev_err(hsotg->dev, "## Unknown EP type ##\n");
  688. break;
  689. }
  690. dwc2_writel(hsotg, hcintmsk, HCINTMSK(chan->hc_num));
  691. if (dbg_hc(chan))
  692. dev_vdbg(hsotg->dev, "set HCINTMSK to %08x\n", hcintmsk);
  693. }
  694. static void dwc2_hc_enable_dma_ints(struct dwc2_hsotg *hsotg,
  695. struct dwc2_host_chan *chan)
  696. {
  697. u32 hcintmsk = HCINTMSK_CHHLTD;
  698. /*
  699. * For Descriptor DMA mode core halts the channel on AHB error.
  700. * Interrupt is not required.
  701. */
  702. if (!hsotg->params.dma_desc_enable) {
  703. if (dbg_hc(chan))
  704. dev_vdbg(hsotg->dev, "desc DMA disabled\n");
  705. hcintmsk |= HCINTMSK_AHBERR;
  706. } else {
  707. if (dbg_hc(chan))
  708. dev_vdbg(hsotg->dev, "desc DMA enabled\n");
  709. if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
  710. hcintmsk |= HCINTMSK_XFERCOMPL;
  711. }
  712. if (chan->error_state && !chan->do_split &&
  713. chan->ep_type != USB_ENDPOINT_XFER_ISOC) {
  714. if (dbg_hc(chan))
  715. dev_vdbg(hsotg->dev, "setting ACK\n");
  716. hcintmsk |= HCINTMSK_ACK;
  717. if (chan->ep_is_in) {
  718. hcintmsk |= HCINTMSK_DATATGLERR;
  719. if (chan->ep_type != USB_ENDPOINT_XFER_INT)
  720. hcintmsk |= HCINTMSK_NAK;
  721. }
  722. }
  723. dwc2_writel(hsotg, hcintmsk, HCINTMSK(chan->hc_num));
  724. if (dbg_hc(chan))
  725. dev_vdbg(hsotg->dev, "set HCINTMSK to %08x\n", hcintmsk);
  726. }
  727. static void dwc2_hc_enable_ints(struct dwc2_hsotg *hsotg,
  728. struct dwc2_host_chan *chan)
  729. {
  730. u32 intmsk;
  731. if (hsotg->params.host_dma) {
  732. if (dbg_hc(chan))
  733. dev_vdbg(hsotg->dev, "DMA enabled\n");
  734. dwc2_hc_enable_dma_ints(hsotg, chan);
  735. } else {
  736. if (dbg_hc(chan))
  737. dev_vdbg(hsotg->dev, "DMA disabled\n");
  738. dwc2_hc_enable_slave_ints(hsotg, chan);
  739. }
  740. /* Enable the top level host channel interrupt */
  741. intmsk = dwc2_readl(hsotg, HAINTMSK);
  742. intmsk |= 1 << chan->hc_num;
  743. dwc2_writel(hsotg, intmsk, HAINTMSK);
  744. if (dbg_hc(chan))
  745. dev_vdbg(hsotg->dev, "set HAINTMSK to %08x\n", intmsk);
  746. /* Make sure host channel interrupts are enabled */
  747. intmsk = dwc2_readl(hsotg, GINTMSK);
  748. intmsk |= GINTSTS_HCHINT;
  749. dwc2_writel(hsotg, intmsk, GINTMSK);
  750. if (dbg_hc(chan))
  751. dev_vdbg(hsotg->dev, "set GINTMSK to %08x\n", intmsk);
  752. }
  753. /**
  754. * dwc2_hc_init() - Prepares a host channel for transferring packets to/from
  755. * a specific endpoint
  756. *
  757. * @hsotg: Programming view of DWC_otg controller
  758. * @chan: Information needed to initialize the host channel
  759. *
  760. * The HCCHARn register is set up with the characteristics specified in chan.
  761. * Host channel interrupts that may need to be serviced while this transfer is
  762. * in progress are enabled.
  763. */
  764. static void dwc2_hc_init(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan)
  765. {
  766. u8 hc_num = chan->hc_num;
  767. u32 hcintmsk;
  768. u32 hcchar;
  769. u32 hcsplt = 0;
  770. if (dbg_hc(chan))
  771. dev_vdbg(hsotg->dev, "%s()\n", __func__);
  772. /* Clear old interrupt conditions for this host channel */
  773. hcintmsk = 0xffffffff;
  774. hcintmsk &= ~HCINTMSK_RESERVED14_31;
  775. dwc2_writel(hsotg, hcintmsk, HCINT(hc_num));
  776. /* Enable channel interrupts required for this transfer */
  777. dwc2_hc_enable_ints(hsotg, chan);
  778. /*
  779. * Program the HCCHARn register with the endpoint characteristics for
  780. * the current transfer
  781. */
  782. hcchar = chan->dev_addr << HCCHAR_DEVADDR_SHIFT & HCCHAR_DEVADDR_MASK;
  783. hcchar |= chan->ep_num << HCCHAR_EPNUM_SHIFT & HCCHAR_EPNUM_MASK;
  784. if (chan->ep_is_in)
  785. hcchar |= HCCHAR_EPDIR;
  786. if (chan->speed == USB_SPEED_LOW)
  787. hcchar |= HCCHAR_LSPDDEV;
  788. hcchar |= chan->ep_type << HCCHAR_EPTYPE_SHIFT & HCCHAR_EPTYPE_MASK;
  789. hcchar |= chan->max_packet << HCCHAR_MPS_SHIFT & HCCHAR_MPS_MASK;
  790. dwc2_writel(hsotg, hcchar, HCCHAR(hc_num));
  791. if (dbg_hc(chan)) {
  792. dev_vdbg(hsotg->dev, "set HCCHAR(%d) to %08x\n",
  793. hc_num, hcchar);
  794. dev_vdbg(hsotg->dev, "%s: Channel %d\n",
  795. __func__, hc_num);
  796. dev_vdbg(hsotg->dev, " Dev Addr: %d\n",
  797. chan->dev_addr);
  798. dev_vdbg(hsotg->dev, " Ep Num: %d\n",
  799. chan->ep_num);
  800. dev_vdbg(hsotg->dev, " Is In: %d\n",
  801. chan->ep_is_in);
  802. dev_vdbg(hsotg->dev, " Is Low Speed: %d\n",
  803. chan->speed == USB_SPEED_LOW);
  804. dev_vdbg(hsotg->dev, " Ep Type: %d\n",
  805. chan->ep_type);
  806. dev_vdbg(hsotg->dev, " Max Pkt: %d\n",
  807. chan->max_packet);
  808. }
  809. /* Program the HCSPLT register for SPLITs */
  810. if (chan->do_split) {
  811. if (dbg_hc(chan))
  812. dev_vdbg(hsotg->dev,
  813. "Programming HC %d with split --> %s\n",
  814. hc_num,
  815. chan->complete_split ? "CSPLIT" : "SSPLIT");
  816. if (chan->complete_split)
  817. hcsplt |= HCSPLT_COMPSPLT;
  818. hcsplt |= chan->xact_pos << HCSPLT_XACTPOS_SHIFT &
  819. HCSPLT_XACTPOS_MASK;
  820. hcsplt |= chan->hub_addr << HCSPLT_HUBADDR_SHIFT &
  821. HCSPLT_HUBADDR_MASK;
  822. hcsplt |= chan->hub_port << HCSPLT_PRTADDR_SHIFT &
  823. HCSPLT_PRTADDR_MASK;
  824. if (dbg_hc(chan)) {
  825. dev_vdbg(hsotg->dev, " comp split %d\n",
  826. chan->complete_split);
  827. dev_vdbg(hsotg->dev, " xact pos %d\n",
  828. chan->xact_pos);
  829. dev_vdbg(hsotg->dev, " hub addr %d\n",
  830. chan->hub_addr);
  831. dev_vdbg(hsotg->dev, " hub port %d\n",
  832. chan->hub_port);
  833. dev_vdbg(hsotg->dev, " is_in %d\n",
  834. chan->ep_is_in);
  835. dev_vdbg(hsotg->dev, " Max Pkt %d\n",
  836. chan->max_packet);
  837. dev_vdbg(hsotg->dev, " xferlen %d\n",
  838. chan->xfer_len);
  839. }
  840. }
  841. dwc2_writel(hsotg, hcsplt, HCSPLT(hc_num));
  842. }
  843. /**
  844. * dwc2_hc_halt() - Attempts to halt a host channel
  845. *
  846. * @hsotg: Controller register interface
  847. * @chan: Host channel to halt
  848. * @halt_status: Reason for halting the channel
  849. *
  850. * This function should only be called in Slave mode or to abort a transfer in
  851. * either Slave mode or DMA mode. Under normal circumstances in DMA mode, the
  852. * controller halts the channel when the transfer is complete or a condition
  853. * occurs that requires application intervention.
  854. *
  855. * In slave mode, checks for a free request queue entry, then sets the Channel
  856. * Enable and Channel Disable bits of the Host Channel Characteristics
  857. * register of the specified channel to intiate the halt. If there is no free
  858. * request queue entry, sets only the Channel Disable bit of the HCCHARn
  859. * register to flush requests for this channel. In the latter case, sets a
  860. * flag to indicate that the host channel needs to be halted when a request
  861. * queue slot is open.
  862. *
  863. * In DMA mode, always sets the Channel Enable and Channel Disable bits of the
  864. * HCCHARn register. The controller ensures there is space in the request
  865. * queue before submitting the halt request.
  866. *
  867. * Some time may elapse before the core flushes any posted requests for this
  868. * host channel and halts. The Channel Halted interrupt handler completes the
  869. * deactivation of the host channel.
  870. */
  871. void dwc2_hc_halt(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan,
  872. enum dwc2_halt_status halt_status)
  873. {
  874. u32 nptxsts, hptxsts, hcchar;
  875. if (dbg_hc(chan))
  876. dev_vdbg(hsotg->dev, "%s()\n", __func__);
  877. /*
  878. * In buffer DMA or external DMA mode channel can't be halted
  879. * for non-split periodic channels. At the end of the next
  880. * uframe/frame (in the worst case), the core generates a channel
  881. * halted and disables the channel automatically.
  882. */
  883. if ((hsotg->params.g_dma && !hsotg->params.g_dma_desc) ||
  884. hsotg->hw_params.arch == GHWCFG2_EXT_DMA_ARCH) {
  885. if (!chan->do_split &&
  886. (chan->ep_type == USB_ENDPOINT_XFER_ISOC ||
  887. chan->ep_type == USB_ENDPOINT_XFER_INT)) {
  888. dev_err(hsotg->dev, "%s() Channel can't be halted\n",
  889. __func__);
  890. return;
  891. }
  892. }
  893. if (halt_status == DWC2_HC_XFER_NO_HALT_STATUS)
  894. dev_err(hsotg->dev, "!!! halt_status = %d !!!\n", halt_status);
  895. if (halt_status == DWC2_HC_XFER_URB_DEQUEUE ||
  896. halt_status == DWC2_HC_XFER_AHB_ERR) {
  897. /*
  898. * Disable all channel interrupts except Ch Halted. The QTD
  899. * and QH state associated with this transfer has been cleared
  900. * (in the case of URB_DEQUEUE), so the channel needs to be
  901. * shut down carefully to prevent crashes.
  902. */
  903. u32 hcintmsk = HCINTMSK_CHHLTD;
  904. dev_vdbg(hsotg->dev, "dequeue/error\n");
  905. dwc2_writel(hsotg, hcintmsk, HCINTMSK(chan->hc_num));
  906. /*
  907. * Make sure no other interrupts besides halt are currently
  908. * pending. Handling another interrupt could cause a crash due
  909. * to the QTD and QH state.
  910. */
  911. dwc2_writel(hsotg, ~hcintmsk, HCINT(chan->hc_num));
  912. /*
  913. * Make sure the halt status is set to URB_DEQUEUE or AHB_ERR
  914. * even if the channel was already halted for some other
  915. * reason
  916. */
  917. chan->halt_status = halt_status;
  918. hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num));
  919. if (!(hcchar & HCCHAR_CHENA)) {
  920. /*
  921. * The channel is either already halted or it hasn't
  922. * started yet. In DMA mode, the transfer may halt if
  923. * it finishes normally or a condition occurs that
  924. * requires driver intervention. Don't want to halt
  925. * the channel again. In either Slave or DMA mode,
  926. * it's possible that the transfer has been assigned
  927. * to a channel, but not started yet when an URB is
  928. * dequeued. Don't want to halt a channel that hasn't
  929. * started yet.
  930. */
  931. return;
  932. }
  933. }
  934. if (chan->halt_pending) {
  935. /*
  936. * A halt has already been issued for this channel. This might
  937. * happen when a transfer is aborted by a higher level in
  938. * the stack.
  939. */
  940. dev_vdbg(hsotg->dev,
  941. "*** %s: Channel %d, chan->halt_pending already set ***\n",
  942. __func__, chan->hc_num);
  943. return;
  944. }
  945. hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num));
  946. /* No need to set the bit in DDMA for disabling the channel */
  947. /* TODO check it everywhere channel is disabled */
  948. if (!hsotg->params.dma_desc_enable) {
  949. if (dbg_hc(chan))
  950. dev_vdbg(hsotg->dev, "desc DMA disabled\n");
  951. hcchar |= HCCHAR_CHENA;
  952. } else {
  953. if (dbg_hc(chan))
  954. dev_dbg(hsotg->dev, "desc DMA enabled\n");
  955. }
  956. hcchar |= HCCHAR_CHDIS;
  957. if (!hsotg->params.host_dma) {
  958. if (dbg_hc(chan))
  959. dev_vdbg(hsotg->dev, "DMA not enabled\n");
  960. hcchar |= HCCHAR_CHENA;
  961. /* Check for space in the request queue to issue the halt */
  962. if (chan->ep_type == USB_ENDPOINT_XFER_CONTROL ||
  963. chan->ep_type == USB_ENDPOINT_XFER_BULK) {
  964. dev_vdbg(hsotg->dev, "control/bulk\n");
  965. nptxsts = dwc2_readl(hsotg, GNPTXSTS);
  966. if ((nptxsts & TXSTS_QSPCAVAIL_MASK) == 0) {
  967. dev_vdbg(hsotg->dev, "Disabling channel\n");
  968. hcchar &= ~HCCHAR_CHENA;
  969. }
  970. } else {
  971. if (dbg_perio())
  972. dev_vdbg(hsotg->dev, "isoc/intr\n");
  973. hptxsts = dwc2_readl(hsotg, HPTXSTS);
  974. if ((hptxsts & TXSTS_QSPCAVAIL_MASK) == 0 ||
  975. hsotg->queuing_high_bandwidth) {
  976. if (dbg_perio())
  977. dev_vdbg(hsotg->dev, "Disabling channel\n");
  978. hcchar &= ~HCCHAR_CHENA;
  979. }
  980. }
  981. } else {
  982. if (dbg_hc(chan))
  983. dev_vdbg(hsotg->dev, "DMA enabled\n");
  984. }
  985. dwc2_writel(hsotg, hcchar, HCCHAR(chan->hc_num));
  986. chan->halt_status = halt_status;
  987. if (hcchar & HCCHAR_CHENA) {
  988. if (dbg_hc(chan))
  989. dev_vdbg(hsotg->dev, "Channel enabled\n");
  990. chan->halt_pending = 1;
  991. chan->halt_on_queue = 0;
  992. } else {
  993. if (dbg_hc(chan))
  994. dev_vdbg(hsotg->dev, "Channel disabled\n");
  995. chan->halt_on_queue = 1;
  996. }
  997. if (dbg_hc(chan)) {
  998. dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
  999. chan->hc_num);
  1000. dev_vdbg(hsotg->dev, " hcchar: 0x%08x\n",
  1001. hcchar);
  1002. dev_vdbg(hsotg->dev, " halt_pending: %d\n",
  1003. chan->halt_pending);
  1004. dev_vdbg(hsotg->dev, " halt_on_queue: %d\n",
  1005. chan->halt_on_queue);
  1006. dev_vdbg(hsotg->dev, " halt_status: %d\n",
  1007. chan->halt_status);
  1008. }
  1009. }
  1010. /**
  1011. * dwc2_hc_cleanup() - Clears the transfer state for a host channel
  1012. *
  1013. * @hsotg: Programming view of DWC_otg controller
  1014. * @chan: Identifies the host channel to clean up
  1015. *
  1016. * This function is normally called after a transfer is done and the host
  1017. * channel is being released
  1018. */
  1019. void dwc2_hc_cleanup(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan)
  1020. {
  1021. u32 hcintmsk;
  1022. chan->xfer_started = 0;
  1023. list_del_init(&chan->split_order_list_entry);
  1024. /*
  1025. * Clear channel interrupt enables and any unhandled channel interrupt
  1026. * conditions
  1027. */
  1028. dwc2_writel(hsotg, 0, HCINTMSK(chan->hc_num));
  1029. hcintmsk = 0xffffffff;
  1030. hcintmsk &= ~HCINTMSK_RESERVED14_31;
  1031. dwc2_writel(hsotg, hcintmsk, HCINT(chan->hc_num));
  1032. }
  1033. /**
  1034. * dwc2_hc_set_even_odd_frame() - Sets the channel property that indicates in
  1035. * which frame a periodic transfer should occur
  1036. *
  1037. * @hsotg: Programming view of DWC_otg controller
  1038. * @chan: Identifies the host channel to set up and its properties
  1039. * @hcchar: Current value of the HCCHAR register for the specified host channel
  1040. *
  1041. * This function has no effect on non-periodic transfers
  1042. */
  1043. static void dwc2_hc_set_even_odd_frame(struct dwc2_hsotg *hsotg,
  1044. struct dwc2_host_chan *chan, u32 *hcchar)
  1045. {
  1046. if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
  1047. chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
  1048. int host_speed;
  1049. int xfer_ns;
  1050. int xfer_us;
  1051. int bytes_in_fifo;
  1052. u16 fifo_space;
  1053. u16 frame_number;
  1054. u16 wire_frame;
  1055. /*
  1056. * Try to figure out if we're an even or odd frame. If we set
  1057. * even and the current frame number is even the the transfer
  1058. * will happen immediately. Similar if both are odd. If one is
  1059. * even and the other is odd then the transfer will happen when
  1060. * the frame number ticks.
  1061. *
  1062. * There's a bit of a balancing act to get this right.
  1063. * Sometimes we may want to send data in the current frame (AK
  1064. * right away). We might want to do this if the frame number
  1065. * _just_ ticked, but we might also want to do this in order
  1066. * to continue a split transaction that happened late in a
  1067. * microframe (so we didn't know to queue the next transfer
  1068. * until the frame number had ticked). The problem is that we
  1069. * need a lot of knowledge to know if there's actually still
  1070. * time to send things or if it would be better to wait until
  1071. * the next frame.
  1072. *
  1073. * We can look at how much time is left in the current frame
  1074. * and make a guess about whether we'll have time to transfer.
  1075. * We'll do that.
  1076. */
  1077. /* Get speed host is running at */
  1078. host_speed = (chan->speed != USB_SPEED_HIGH &&
  1079. !chan->do_split) ? chan->speed : USB_SPEED_HIGH;
  1080. /* See how many bytes are in the periodic FIFO right now */
  1081. fifo_space = (dwc2_readl(hsotg, HPTXSTS) &
  1082. TXSTS_FSPCAVAIL_MASK) >> TXSTS_FSPCAVAIL_SHIFT;
  1083. bytes_in_fifo = sizeof(u32) *
  1084. (hsotg->params.host_perio_tx_fifo_size -
  1085. fifo_space);
  1086. /*
  1087. * Roughly estimate bus time for everything in the periodic
  1088. * queue + our new transfer. This is "rough" because we're
  1089. * using a function that makes takes into account IN/OUT
  1090. * and INT/ISO and we're just slamming in one value for all
  1091. * transfers. This should be an over-estimate and that should
  1092. * be OK, but we can probably tighten it.
  1093. */
  1094. xfer_ns = usb_calc_bus_time(host_speed, false, false,
  1095. chan->xfer_len + bytes_in_fifo);
  1096. xfer_us = NS_TO_US(xfer_ns);
  1097. /* See what frame number we'll be at by the time we finish */
  1098. frame_number = dwc2_hcd_get_future_frame_number(hsotg, xfer_us);
  1099. /* This is when we were scheduled to be on the wire */
  1100. wire_frame = dwc2_frame_num_inc(chan->qh->next_active_frame, 1);
  1101. /*
  1102. * If we'd finish _after_ the frame we're scheduled in then
  1103. * it's hopeless. Just schedule right away and hope for the
  1104. * best. Note that it _might_ be wise to call back into the
  1105. * scheduler to pick a better frame, but this is better than
  1106. * nothing.
  1107. */
  1108. if (dwc2_frame_num_gt(frame_number, wire_frame)) {
  1109. dwc2_sch_vdbg(hsotg,
  1110. "QH=%p EO MISS fr=%04x=>%04x (%+d)\n",
  1111. chan->qh, wire_frame, frame_number,
  1112. dwc2_frame_num_dec(frame_number,
  1113. wire_frame));
  1114. wire_frame = frame_number;
  1115. /*
  1116. * We picked a different frame number; communicate this
  1117. * back to the scheduler so it doesn't try to schedule
  1118. * another in the same frame.
  1119. *
  1120. * Remember that next_active_frame is 1 before the wire
  1121. * frame.
  1122. */
  1123. chan->qh->next_active_frame =
  1124. dwc2_frame_num_dec(frame_number, 1);
  1125. }
  1126. if (wire_frame & 1)
  1127. *hcchar |= HCCHAR_ODDFRM;
  1128. else
  1129. *hcchar &= ~HCCHAR_ODDFRM;
  1130. }
  1131. }
  1132. static void dwc2_set_pid_isoc(struct dwc2_host_chan *chan)
  1133. {
  1134. /* Set up the initial PID for the transfer */
  1135. if (chan->speed == USB_SPEED_HIGH) {
  1136. if (chan->ep_is_in) {
  1137. if (chan->multi_count == 1)
  1138. chan->data_pid_start = DWC2_HC_PID_DATA0;
  1139. else if (chan->multi_count == 2)
  1140. chan->data_pid_start = DWC2_HC_PID_DATA1;
  1141. else
  1142. chan->data_pid_start = DWC2_HC_PID_DATA2;
  1143. } else {
  1144. if (chan->multi_count == 1)
  1145. chan->data_pid_start = DWC2_HC_PID_DATA0;
  1146. else
  1147. chan->data_pid_start = DWC2_HC_PID_MDATA;
  1148. }
  1149. } else {
  1150. chan->data_pid_start = DWC2_HC_PID_DATA0;
  1151. }
  1152. }
  1153. /**
  1154. * dwc2_hc_write_packet() - Writes a packet into the Tx FIFO associated with
  1155. * the Host Channel
  1156. *
  1157. * @hsotg: Programming view of DWC_otg controller
  1158. * @chan: Information needed to initialize the host channel
  1159. *
  1160. * This function should only be called in Slave mode. For a channel associated
  1161. * with a non-periodic EP, the non-periodic Tx FIFO is written. For a channel
  1162. * associated with a periodic EP, the periodic Tx FIFO is written.
  1163. *
  1164. * Upon return the xfer_buf and xfer_count fields in chan are incremented by
  1165. * the number of bytes written to the Tx FIFO.
  1166. */
  1167. static void dwc2_hc_write_packet(struct dwc2_hsotg *hsotg,
  1168. struct dwc2_host_chan *chan)
  1169. {
  1170. u32 i;
  1171. u32 remaining_count;
  1172. u32 byte_count;
  1173. u32 dword_count;
  1174. u32 __iomem *data_fifo;
  1175. u32 *data_buf = (u32 *)chan->xfer_buf;
  1176. if (dbg_hc(chan))
  1177. dev_vdbg(hsotg->dev, "%s()\n", __func__);
  1178. data_fifo = (u32 __iomem *)(hsotg->regs + HCFIFO(chan->hc_num));
  1179. remaining_count = chan->xfer_len - chan->xfer_count;
  1180. if (remaining_count > chan->max_packet)
  1181. byte_count = chan->max_packet;
  1182. else
  1183. byte_count = remaining_count;
  1184. dword_count = (byte_count + 3) / 4;
  1185. if (((unsigned long)data_buf & 0x3) == 0) {
  1186. /* xfer_buf is DWORD aligned */
  1187. for (i = 0; i < dword_count; i++, data_buf++)
  1188. dwc2_writel(hsotg, *data_buf, HCFIFO(chan->hc_num));
  1189. } else {
  1190. /* xfer_buf is not DWORD aligned */
  1191. for (i = 0; i < dword_count; i++, data_buf++) {
  1192. u32 data = data_buf[0] | data_buf[1] << 8 |
  1193. data_buf[2] << 16 | data_buf[3] << 24;
  1194. dwc2_writel(hsotg, data, HCFIFO(chan->hc_num));
  1195. }
  1196. }
  1197. chan->xfer_count += byte_count;
  1198. chan->xfer_buf += byte_count;
  1199. }
  1200. /**
  1201. * dwc2_hc_do_ping() - Starts a PING transfer
  1202. *
  1203. * @hsotg: Programming view of DWC_otg controller
  1204. * @chan: Information needed to initialize the host channel
  1205. *
  1206. * This function should only be called in Slave mode. The Do Ping bit is set in
  1207. * the HCTSIZ register, then the channel is enabled.
  1208. */
  1209. static void dwc2_hc_do_ping(struct dwc2_hsotg *hsotg,
  1210. struct dwc2_host_chan *chan)
  1211. {
  1212. u32 hcchar;
  1213. u32 hctsiz;
  1214. if (dbg_hc(chan))
  1215. dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
  1216. chan->hc_num);
  1217. hctsiz = TSIZ_DOPNG;
  1218. hctsiz |= 1 << TSIZ_PKTCNT_SHIFT;
  1219. dwc2_writel(hsotg, hctsiz, HCTSIZ(chan->hc_num));
  1220. hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num));
  1221. hcchar |= HCCHAR_CHENA;
  1222. hcchar &= ~HCCHAR_CHDIS;
  1223. dwc2_writel(hsotg, hcchar, HCCHAR(chan->hc_num));
  1224. }
  1225. /**
  1226. * dwc2_hc_start_transfer() - Does the setup for a data transfer for a host
  1227. * channel and starts the transfer
  1228. *
  1229. * @hsotg: Programming view of DWC_otg controller
  1230. * @chan: Information needed to initialize the host channel. The xfer_len value
  1231. * may be reduced to accommodate the max widths of the XferSize and
  1232. * PktCnt fields in the HCTSIZn register. The multi_count value may be
  1233. * changed to reflect the final xfer_len value.
  1234. *
  1235. * This function may be called in either Slave mode or DMA mode. In Slave mode,
  1236. * the caller must ensure that there is sufficient space in the request queue
  1237. * and Tx Data FIFO.
  1238. *
  1239. * For an OUT transfer in Slave mode, it loads a data packet into the
  1240. * appropriate FIFO. If necessary, additional data packets are loaded in the
  1241. * Host ISR.
  1242. *
  1243. * For an IN transfer in Slave mode, a data packet is requested. The data
  1244. * packets are unloaded from the Rx FIFO in the Host ISR. If necessary,
  1245. * additional data packets are requested in the Host ISR.
  1246. *
  1247. * For a PING transfer in Slave mode, the Do Ping bit is set in the HCTSIZ
  1248. * register along with a packet count of 1 and the channel is enabled. This
  1249. * causes a single PING transaction to occur. Other fields in HCTSIZ are
  1250. * simply set to 0 since no data transfer occurs in this case.
  1251. *
  1252. * For a PING transfer in DMA mode, the HCTSIZ register is initialized with
  1253. * all the information required to perform the subsequent data transfer. In
  1254. * addition, the Do Ping bit is set in the HCTSIZ register. In this case, the
  1255. * controller performs the entire PING protocol, then starts the data
  1256. * transfer.
  1257. */
  1258. static void dwc2_hc_start_transfer(struct dwc2_hsotg *hsotg,
  1259. struct dwc2_host_chan *chan)
  1260. {
  1261. u32 max_hc_xfer_size = hsotg->params.max_transfer_size;
  1262. u16 max_hc_pkt_count = hsotg->params.max_packet_count;
  1263. u32 hcchar;
  1264. u32 hctsiz = 0;
  1265. u16 num_packets;
  1266. u32 ec_mc;
  1267. if (dbg_hc(chan))
  1268. dev_vdbg(hsotg->dev, "%s()\n", __func__);
  1269. if (chan->do_ping) {
  1270. if (!hsotg->params.host_dma) {
  1271. if (dbg_hc(chan))
  1272. dev_vdbg(hsotg->dev, "ping, no DMA\n");
  1273. dwc2_hc_do_ping(hsotg, chan);
  1274. chan->xfer_started = 1;
  1275. return;
  1276. }
  1277. if (dbg_hc(chan))
  1278. dev_vdbg(hsotg->dev, "ping, DMA\n");
  1279. hctsiz |= TSIZ_DOPNG;
  1280. }
  1281. if (chan->do_split) {
  1282. if (dbg_hc(chan))
  1283. dev_vdbg(hsotg->dev, "split\n");
  1284. num_packets = 1;
  1285. if (chan->complete_split && !chan->ep_is_in)
  1286. /*
  1287. * For CSPLIT OUT Transfer, set the size to 0 so the
  1288. * core doesn't expect any data written to the FIFO
  1289. */
  1290. chan->xfer_len = 0;
  1291. else if (chan->ep_is_in || chan->xfer_len > chan->max_packet)
  1292. chan->xfer_len = chan->max_packet;
  1293. else if (!chan->ep_is_in && chan->xfer_len > 188)
  1294. chan->xfer_len = 188;
  1295. hctsiz |= chan->xfer_len << TSIZ_XFERSIZE_SHIFT &
  1296. TSIZ_XFERSIZE_MASK;
  1297. /* For split set ec_mc for immediate retries */
  1298. if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
  1299. chan->ep_type == USB_ENDPOINT_XFER_ISOC)
  1300. ec_mc = 3;
  1301. else
  1302. ec_mc = 1;
  1303. } else {
  1304. if (dbg_hc(chan))
  1305. dev_vdbg(hsotg->dev, "no split\n");
  1306. /*
  1307. * Ensure that the transfer length and packet count will fit
  1308. * in the widths allocated for them in the HCTSIZn register
  1309. */
  1310. if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
  1311. chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
  1312. /*
  1313. * Make sure the transfer size is no larger than one
  1314. * (micro)frame's worth of data. (A check was done
  1315. * when the periodic transfer was accepted to ensure
  1316. * that a (micro)frame's worth of data can be
  1317. * programmed into a channel.)
  1318. */
  1319. u32 max_periodic_len =
  1320. chan->multi_count * chan->max_packet;
  1321. if (chan->xfer_len > max_periodic_len)
  1322. chan->xfer_len = max_periodic_len;
  1323. } else if (chan->xfer_len > max_hc_xfer_size) {
  1324. /*
  1325. * Make sure that xfer_len is a multiple of max packet
  1326. * size
  1327. */
  1328. chan->xfer_len =
  1329. max_hc_xfer_size - chan->max_packet + 1;
  1330. }
  1331. if (chan->xfer_len > 0) {
  1332. num_packets = (chan->xfer_len + chan->max_packet - 1) /
  1333. chan->max_packet;
  1334. if (num_packets > max_hc_pkt_count) {
  1335. num_packets = max_hc_pkt_count;
  1336. chan->xfer_len = num_packets * chan->max_packet;
  1337. } else if (chan->ep_is_in) {
  1338. /*
  1339. * Always program an integral # of max packets
  1340. * for IN transfers.
  1341. * Note: This assumes that the input buffer is
  1342. * aligned and sized accordingly.
  1343. */
  1344. chan->xfer_len = num_packets * chan->max_packet;
  1345. }
  1346. } else {
  1347. /* Need 1 packet for transfer length of 0 */
  1348. num_packets = 1;
  1349. }
  1350. if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
  1351. chan->ep_type == USB_ENDPOINT_XFER_ISOC)
  1352. /*
  1353. * Make sure that the multi_count field matches the
  1354. * actual transfer length
  1355. */
  1356. chan->multi_count = num_packets;
  1357. if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
  1358. dwc2_set_pid_isoc(chan);
  1359. hctsiz |= chan->xfer_len << TSIZ_XFERSIZE_SHIFT &
  1360. TSIZ_XFERSIZE_MASK;
  1361. /* The ec_mc gets the multi_count for non-split */
  1362. ec_mc = chan->multi_count;
  1363. }
  1364. chan->start_pkt_count = num_packets;
  1365. hctsiz |= num_packets << TSIZ_PKTCNT_SHIFT & TSIZ_PKTCNT_MASK;
  1366. hctsiz |= chan->data_pid_start << TSIZ_SC_MC_PID_SHIFT &
  1367. TSIZ_SC_MC_PID_MASK;
  1368. dwc2_writel(hsotg, hctsiz, HCTSIZ(chan->hc_num));
  1369. if (dbg_hc(chan)) {
  1370. dev_vdbg(hsotg->dev, "Wrote %08x to HCTSIZ(%d)\n",
  1371. hctsiz, chan->hc_num);
  1372. dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
  1373. chan->hc_num);
  1374. dev_vdbg(hsotg->dev, " Xfer Size: %d\n",
  1375. (hctsiz & TSIZ_XFERSIZE_MASK) >>
  1376. TSIZ_XFERSIZE_SHIFT);
  1377. dev_vdbg(hsotg->dev, " Num Pkts: %d\n",
  1378. (hctsiz & TSIZ_PKTCNT_MASK) >>
  1379. TSIZ_PKTCNT_SHIFT);
  1380. dev_vdbg(hsotg->dev, " Start PID: %d\n",
  1381. (hctsiz & TSIZ_SC_MC_PID_MASK) >>
  1382. TSIZ_SC_MC_PID_SHIFT);
  1383. }
  1384. if (hsotg->params.host_dma) {
  1385. dma_addr_t dma_addr;
  1386. if (chan->align_buf) {
  1387. if (dbg_hc(chan))
  1388. dev_vdbg(hsotg->dev, "align_buf\n");
  1389. dma_addr = chan->align_buf;
  1390. } else {
  1391. dma_addr = chan->xfer_dma;
  1392. }
  1393. dwc2_writel(hsotg, (u32)dma_addr, HCDMA(chan->hc_num));
  1394. if (dbg_hc(chan))
  1395. dev_vdbg(hsotg->dev, "Wrote %08lx to HCDMA(%d)\n",
  1396. (unsigned long)dma_addr, chan->hc_num);
  1397. }
  1398. /* Start the split */
  1399. if (chan->do_split) {
  1400. u32 hcsplt = dwc2_readl(hsotg, HCSPLT(chan->hc_num));
  1401. hcsplt |= HCSPLT_SPLTENA;
  1402. dwc2_writel(hsotg, hcsplt, HCSPLT(chan->hc_num));
  1403. }
  1404. hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num));
  1405. hcchar &= ~HCCHAR_MULTICNT_MASK;
  1406. hcchar |= (ec_mc << HCCHAR_MULTICNT_SHIFT) & HCCHAR_MULTICNT_MASK;
  1407. dwc2_hc_set_even_odd_frame(hsotg, chan, &hcchar);
  1408. if (hcchar & HCCHAR_CHDIS)
  1409. dev_warn(hsotg->dev,
  1410. "%s: chdis set, channel %d, hcchar 0x%08x\n",
  1411. __func__, chan->hc_num, hcchar);
  1412. /* Set host channel enable after all other setup is complete */
  1413. hcchar |= HCCHAR_CHENA;
  1414. hcchar &= ~HCCHAR_CHDIS;
  1415. if (dbg_hc(chan))
  1416. dev_vdbg(hsotg->dev, " Multi Cnt: %d\n",
  1417. (hcchar & HCCHAR_MULTICNT_MASK) >>
  1418. HCCHAR_MULTICNT_SHIFT);
  1419. dwc2_writel(hsotg, hcchar, HCCHAR(chan->hc_num));
  1420. if (dbg_hc(chan))
  1421. dev_vdbg(hsotg->dev, "Wrote %08x to HCCHAR(%d)\n", hcchar,
  1422. chan->hc_num);
  1423. chan->xfer_started = 1;
  1424. chan->requests++;
  1425. if (!hsotg->params.host_dma &&
  1426. !chan->ep_is_in && chan->xfer_len > 0)
  1427. /* Load OUT packet into the appropriate Tx FIFO */
  1428. dwc2_hc_write_packet(hsotg, chan);
  1429. }
  1430. /**
  1431. * dwc2_hc_start_transfer_ddma() - Does the setup for a data transfer for a
  1432. * host channel and starts the transfer in Descriptor DMA mode
  1433. *
  1434. * @hsotg: Programming view of DWC_otg controller
  1435. * @chan: Information needed to initialize the host channel
  1436. *
  1437. * Initializes HCTSIZ register. For a PING transfer the Do Ping bit is set.
  1438. * Sets PID and NTD values. For periodic transfers initializes SCHED_INFO field
  1439. * with micro-frame bitmap.
  1440. *
  1441. * Initializes HCDMA register with descriptor list address and CTD value then
  1442. * starts the transfer via enabling the channel.
  1443. */
  1444. void dwc2_hc_start_transfer_ddma(struct dwc2_hsotg *hsotg,
  1445. struct dwc2_host_chan *chan)
  1446. {
  1447. u32 hcchar;
  1448. u32 hctsiz = 0;
  1449. if (chan->do_ping)
  1450. hctsiz |= TSIZ_DOPNG;
  1451. if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
  1452. dwc2_set_pid_isoc(chan);
  1453. /* Packet Count and Xfer Size are not used in Descriptor DMA mode */
  1454. hctsiz |= chan->data_pid_start << TSIZ_SC_MC_PID_SHIFT &
  1455. TSIZ_SC_MC_PID_MASK;
  1456. /* 0 - 1 descriptor, 1 - 2 descriptors, etc */
  1457. hctsiz |= (chan->ntd - 1) << TSIZ_NTD_SHIFT & TSIZ_NTD_MASK;
  1458. /* Non-zero only for high-speed interrupt endpoints */
  1459. hctsiz |= chan->schinfo << TSIZ_SCHINFO_SHIFT & TSIZ_SCHINFO_MASK;
  1460. if (dbg_hc(chan)) {
  1461. dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
  1462. chan->hc_num);
  1463. dev_vdbg(hsotg->dev, " Start PID: %d\n",
  1464. chan->data_pid_start);
  1465. dev_vdbg(hsotg->dev, " NTD: %d\n", chan->ntd - 1);
  1466. }
  1467. dwc2_writel(hsotg, hctsiz, HCTSIZ(chan->hc_num));
  1468. dma_sync_single_for_device(hsotg->dev, chan->desc_list_addr,
  1469. chan->desc_list_sz, DMA_TO_DEVICE);
  1470. dwc2_writel(hsotg, chan->desc_list_addr, HCDMA(chan->hc_num));
  1471. if (dbg_hc(chan))
  1472. dev_vdbg(hsotg->dev, "Wrote %pad to HCDMA(%d)\n",
  1473. &chan->desc_list_addr, chan->hc_num);
  1474. hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num));
  1475. hcchar &= ~HCCHAR_MULTICNT_MASK;
  1476. hcchar |= chan->multi_count << HCCHAR_MULTICNT_SHIFT &
  1477. HCCHAR_MULTICNT_MASK;
  1478. if (hcchar & HCCHAR_CHDIS)
  1479. dev_warn(hsotg->dev,
  1480. "%s: chdis set, channel %d, hcchar 0x%08x\n",
  1481. __func__, chan->hc_num, hcchar);
  1482. /* Set host channel enable after all other setup is complete */
  1483. hcchar |= HCCHAR_CHENA;
  1484. hcchar &= ~HCCHAR_CHDIS;
  1485. if (dbg_hc(chan))
  1486. dev_vdbg(hsotg->dev, " Multi Cnt: %d\n",
  1487. (hcchar & HCCHAR_MULTICNT_MASK) >>
  1488. HCCHAR_MULTICNT_SHIFT);
  1489. dwc2_writel(hsotg, hcchar, HCCHAR(chan->hc_num));
  1490. if (dbg_hc(chan))
  1491. dev_vdbg(hsotg->dev, "Wrote %08x to HCCHAR(%d)\n", hcchar,
  1492. chan->hc_num);
  1493. chan->xfer_started = 1;
  1494. chan->requests++;
  1495. }
  1496. /**
  1497. * dwc2_hc_continue_transfer() - Continues a data transfer that was started by
  1498. * a previous call to dwc2_hc_start_transfer()
  1499. *
  1500. * @hsotg: Programming view of DWC_otg controller
  1501. * @chan: Information needed to initialize the host channel
  1502. *
  1503. * The caller must ensure there is sufficient space in the request queue and Tx
  1504. * Data FIFO. This function should only be called in Slave mode. In DMA mode,
  1505. * the controller acts autonomously to complete transfers programmed to a host
  1506. * channel.
  1507. *
  1508. * For an OUT transfer, a new data packet is loaded into the appropriate FIFO
  1509. * if there is any data remaining to be queued. For an IN transfer, another
  1510. * data packet is always requested. For the SETUP phase of a control transfer,
  1511. * this function does nothing.
  1512. *
  1513. * Return: 1 if a new request is queued, 0 if no more requests are required
  1514. * for this transfer
  1515. */
  1516. static int dwc2_hc_continue_transfer(struct dwc2_hsotg *hsotg,
  1517. struct dwc2_host_chan *chan)
  1518. {
  1519. if (dbg_hc(chan))
  1520. dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
  1521. chan->hc_num);
  1522. if (chan->do_split)
  1523. /* SPLITs always queue just once per channel */
  1524. return 0;
  1525. if (chan->data_pid_start == DWC2_HC_PID_SETUP)
  1526. /* SETUPs are queued only once since they can't be NAK'd */
  1527. return 0;
  1528. if (chan->ep_is_in) {
  1529. /*
  1530. * Always queue another request for other IN transfers. If
  1531. * back-to-back INs are issued and NAKs are received for both,
  1532. * the driver may still be processing the first NAK when the
  1533. * second NAK is received. When the interrupt handler clears
  1534. * the NAK interrupt for the first NAK, the second NAK will
  1535. * not be seen. So we can't depend on the NAK interrupt
  1536. * handler to requeue a NAK'd request. Instead, IN requests
  1537. * are issued each time this function is called. When the
  1538. * transfer completes, the extra requests for the channel will
  1539. * be flushed.
  1540. */
  1541. u32 hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num));
  1542. dwc2_hc_set_even_odd_frame(hsotg, chan, &hcchar);
  1543. hcchar |= HCCHAR_CHENA;
  1544. hcchar &= ~HCCHAR_CHDIS;
  1545. if (dbg_hc(chan))
  1546. dev_vdbg(hsotg->dev, " IN xfer: hcchar = 0x%08x\n",
  1547. hcchar);
  1548. dwc2_writel(hsotg, hcchar, HCCHAR(chan->hc_num));
  1549. chan->requests++;
  1550. return 1;
  1551. }
  1552. /* OUT transfers */
  1553. if (chan->xfer_count < chan->xfer_len) {
  1554. if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
  1555. chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
  1556. u32 hcchar = dwc2_readl(hsotg,
  1557. HCCHAR(chan->hc_num));
  1558. dwc2_hc_set_even_odd_frame(hsotg, chan,
  1559. &hcchar);
  1560. }
  1561. /* Load OUT packet into the appropriate Tx FIFO */
  1562. dwc2_hc_write_packet(hsotg, chan);
  1563. chan->requests++;
  1564. return 1;
  1565. }
  1566. return 0;
  1567. }
  1568. /*
  1569. * =========================================================================
  1570. * HCD
  1571. * =========================================================================
  1572. */
  1573. /*
  1574. * Processes all the URBs in a single list of QHs. Completes them with
  1575. * -ETIMEDOUT and frees the QTD.
  1576. *
  1577. * Must be called with interrupt disabled and spinlock held
  1578. */
  1579. static void dwc2_kill_urbs_in_qh_list(struct dwc2_hsotg *hsotg,
  1580. struct list_head *qh_list)
  1581. {
  1582. struct dwc2_qh *qh, *qh_tmp;
  1583. struct dwc2_qtd *qtd, *qtd_tmp;
  1584. list_for_each_entry_safe(qh, qh_tmp, qh_list, qh_list_entry) {
  1585. list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list,
  1586. qtd_list_entry) {
  1587. dwc2_host_complete(hsotg, qtd, -ECONNRESET);
  1588. dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
  1589. }
  1590. }
  1591. }
  1592. static void dwc2_qh_list_free(struct dwc2_hsotg *hsotg,
  1593. struct list_head *qh_list)
  1594. {
  1595. struct dwc2_qtd *qtd, *qtd_tmp;
  1596. struct dwc2_qh *qh, *qh_tmp;
  1597. unsigned long flags;
  1598. if (!qh_list->next)
  1599. /* The list hasn't been initialized yet */
  1600. return;
  1601. spin_lock_irqsave(&hsotg->lock, flags);
  1602. /* Ensure there are no QTDs or URBs left */
  1603. dwc2_kill_urbs_in_qh_list(hsotg, qh_list);
  1604. list_for_each_entry_safe(qh, qh_tmp, qh_list, qh_list_entry) {
  1605. dwc2_hcd_qh_unlink(hsotg, qh);
  1606. /* Free each QTD in the QH's QTD list */
  1607. list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list,
  1608. qtd_list_entry)
  1609. dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
  1610. if (qh->channel && qh->channel->qh == qh)
  1611. qh->channel->qh = NULL;
  1612. spin_unlock_irqrestore(&hsotg->lock, flags);
  1613. dwc2_hcd_qh_free(hsotg, qh);
  1614. spin_lock_irqsave(&hsotg->lock, flags);
  1615. }
  1616. spin_unlock_irqrestore(&hsotg->lock, flags);
  1617. }
  1618. /*
  1619. * Responds with an error status of -ETIMEDOUT to all URBs in the non-periodic
  1620. * and periodic schedules. The QTD associated with each URB is removed from
  1621. * the schedule and freed. This function may be called when a disconnect is
  1622. * detected or when the HCD is being stopped.
  1623. *
  1624. * Must be called with interrupt disabled and spinlock held
  1625. */
  1626. static void dwc2_kill_all_urbs(struct dwc2_hsotg *hsotg)
  1627. {
  1628. dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->non_periodic_sched_inactive);
  1629. dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->non_periodic_sched_waiting);
  1630. dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->non_periodic_sched_active);
  1631. dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_inactive);
  1632. dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_ready);
  1633. dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_assigned);
  1634. dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_queued);
  1635. }
  1636. /**
  1637. * dwc2_hcd_start() - Starts the HCD when switching to Host mode
  1638. *
  1639. * @hsotg: Pointer to struct dwc2_hsotg
  1640. */
  1641. void dwc2_hcd_start(struct dwc2_hsotg *hsotg)
  1642. {
  1643. u32 hprt0;
  1644. if (hsotg->op_state == OTG_STATE_B_HOST) {
  1645. /*
  1646. * Reset the port. During a HNP mode switch the reset
  1647. * needs to occur within 1ms and have a duration of at
  1648. * least 50ms.
  1649. */
  1650. hprt0 = dwc2_read_hprt0(hsotg);
  1651. hprt0 |= HPRT0_RST;
  1652. dwc2_writel(hsotg, hprt0, HPRT0);
  1653. }
  1654. queue_delayed_work(hsotg->wq_otg, &hsotg->start_work,
  1655. msecs_to_jiffies(50));
  1656. }
  1657. /* Must be called with interrupt disabled and spinlock held */
  1658. static void dwc2_hcd_cleanup_channels(struct dwc2_hsotg *hsotg)
  1659. {
  1660. int num_channels = hsotg->params.host_channels;
  1661. struct dwc2_host_chan *channel;
  1662. u32 hcchar;
  1663. int i;
  1664. if (!hsotg->params.host_dma) {
  1665. /* Flush out any channel requests in slave mode */
  1666. for (i = 0; i < num_channels; i++) {
  1667. channel = hsotg->hc_ptr_array[i];
  1668. if (!list_empty(&channel->hc_list_entry))
  1669. continue;
  1670. hcchar = dwc2_readl(hsotg, HCCHAR(i));
  1671. if (hcchar & HCCHAR_CHENA) {
  1672. hcchar &= ~(HCCHAR_CHENA | HCCHAR_EPDIR);
  1673. hcchar |= HCCHAR_CHDIS;
  1674. dwc2_writel(hsotg, hcchar, HCCHAR(i));
  1675. }
  1676. }
  1677. }
  1678. for (i = 0; i < num_channels; i++) {
  1679. channel = hsotg->hc_ptr_array[i];
  1680. if (!list_empty(&channel->hc_list_entry))
  1681. continue;
  1682. hcchar = dwc2_readl(hsotg, HCCHAR(i));
  1683. if (hcchar & HCCHAR_CHENA) {
  1684. /* Halt the channel */
  1685. hcchar |= HCCHAR_CHDIS;
  1686. dwc2_writel(hsotg, hcchar, HCCHAR(i));
  1687. }
  1688. dwc2_hc_cleanup(hsotg, channel);
  1689. list_add_tail(&channel->hc_list_entry, &hsotg->free_hc_list);
  1690. /*
  1691. * Added for Descriptor DMA to prevent channel double cleanup in
  1692. * release_channel_ddma(), which is called from ep_disable when
  1693. * device disconnects
  1694. */
  1695. channel->qh = NULL;
  1696. }
  1697. /* All channels have been freed, mark them available */
  1698. if (hsotg->params.uframe_sched) {
  1699. hsotg->available_host_channels =
  1700. hsotg->params.host_channels;
  1701. } else {
  1702. hsotg->non_periodic_channels = 0;
  1703. hsotg->periodic_channels = 0;
  1704. }
  1705. }
  1706. /**
  1707. * dwc2_hcd_connect() - Handles connect of the HCD
  1708. *
  1709. * @hsotg: Pointer to struct dwc2_hsotg
  1710. *
  1711. * Must be called with interrupt disabled and spinlock held
  1712. */
  1713. void dwc2_hcd_connect(struct dwc2_hsotg *hsotg)
  1714. {
  1715. if (hsotg->lx_state != DWC2_L0)
  1716. usb_hcd_resume_root_hub(hsotg->priv);
  1717. hsotg->flags.b.port_connect_status_change = 1;
  1718. hsotg->flags.b.port_connect_status = 1;
  1719. }
  1720. /**
  1721. * dwc2_hcd_disconnect() - Handles disconnect of the HCD
  1722. *
  1723. * @hsotg: Pointer to struct dwc2_hsotg
  1724. * @force: If true, we won't try to reconnect even if we see device connected.
  1725. *
  1726. * Must be called with interrupt disabled and spinlock held
  1727. */
  1728. void dwc2_hcd_disconnect(struct dwc2_hsotg *hsotg, bool force)
  1729. {
  1730. u32 intr;
  1731. u32 hprt0;
  1732. /* Set status flags for the hub driver */
  1733. hsotg->flags.b.port_connect_status_change = 1;
  1734. hsotg->flags.b.port_connect_status = 0;
  1735. /*
  1736. * Shutdown any transfers in process by clearing the Tx FIFO Empty
  1737. * interrupt mask and status bits and disabling subsequent host
  1738. * channel interrupts.
  1739. */
  1740. intr = dwc2_readl(hsotg, GINTMSK);
  1741. intr &= ~(GINTSTS_NPTXFEMP | GINTSTS_PTXFEMP | GINTSTS_HCHINT);
  1742. dwc2_writel(hsotg, intr, GINTMSK);
  1743. intr = GINTSTS_NPTXFEMP | GINTSTS_PTXFEMP | GINTSTS_HCHINT;
  1744. dwc2_writel(hsotg, intr, GINTSTS);
  1745. /*
  1746. * Turn off the vbus power only if the core has transitioned to device
  1747. * mode. If still in host mode, need to keep power on to detect a
  1748. * reconnection.
  1749. */
  1750. if (dwc2_is_device_mode(hsotg)) {
  1751. if (hsotg->op_state != OTG_STATE_A_SUSPEND) {
  1752. dev_dbg(hsotg->dev, "Disconnect: PortPower off\n");
  1753. dwc2_writel(hsotg, 0, HPRT0);
  1754. }
  1755. dwc2_disable_host_interrupts(hsotg);
  1756. }
  1757. /* Respond with an error status to all URBs in the schedule */
  1758. dwc2_kill_all_urbs(hsotg);
  1759. if (dwc2_is_host_mode(hsotg))
  1760. /* Clean up any host channels that were in use */
  1761. dwc2_hcd_cleanup_channels(hsotg);
  1762. dwc2_host_disconnect(hsotg);
  1763. /*
  1764. * Add an extra check here to see if we're actually connected but
  1765. * we don't have a detection interrupt pending. This can happen if:
  1766. * 1. hardware sees connect
  1767. * 2. hardware sees disconnect
  1768. * 3. hardware sees connect
  1769. * 4. dwc2_port_intr() - clears connect interrupt
  1770. * 5. dwc2_handle_common_intr() - calls here
  1771. *
  1772. * Without the extra check here we will end calling disconnect
  1773. * and won't get any future interrupts to handle the connect.
  1774. */
  1775. if (!force) {
  1776. hprt0 = dwc2_readl(hsotg, HPRT0);
  1777. if (!(hprt0 & HPRT0_CONNDET) && (hprt0 & HPRT0_CONNSTS))
  1778. dwc2_hcd_connect(hsotg);
  1779. }
  1780. }
  1781. /**
  1782. * dwc2_hcd_rem_wakeup() - Handles Remote Wakeup
  1783. *
  1784. * @hsotg: Pointer to struct dwc2_hsotg
  1785. */
  1786. static void dwc2_hcd_rem_wakeup(struct dwc2_hsotg *hsotg)
  1787. {
  1788. if (hsotg->bus_suspended) {
  1789. hsotg->flags.b.port_suspend_change = 1;
  1790. usb_hcd_resume_root_hub(hsotg->priv);
  1791. }
  1792. if (hsotg->lx_state == DWC2_L1)
  1793. hsotg->flags.b.port_l1_change = 1;
  1794. }
  1795. /**
  1796. * dwc2_hcd_stop() - Halts the DWC_otg host mode operations in a clean manner
  1797. *
  1798. * @hsotg: Pointer to struct dwc2_hsotg
  1799. *
  1800. * Must be called with interrupt disabled and spinlock held
  1801. */
  1802. void dwc2_hcd_stop(struct dwc2_hsotg *hsotg)
  1803. {
  1804. dev_dbg(hsotg->dev, "DWC OTG HCD STOP\n");
  1805. /*
  1806. * The root hub should be disconnected before this function is called.
  1807. * The disconnect will clear the QTD lists (via ..._hcd_urb_dequeue)
  1808. * and the QH lists (via ..._hcd_endpoint_disable).
  1809. */
  1810. /* Turn off all host-specific interrupts */
  1811. dwc2_disable_host_interrupts(hsotg);
  1812. /* Turn off the vbus power */
  1813. dev_dbg(hsotg->dev, "PortPower off\n");
  1814. dwc2_writel(hsotg, 0, HPRT0);
  1815. }
  1816. /* Caller must hold driver lock */
  1817. static int dwc2_hcd_urb_enqueue(struct dwc2_hsotg *hsotg,
  1818. struct dwc2_hcd_urb *urb, struct dwc2_qh *qh,
  1819. struct dwc2_qtd *qtd)
  1820. {
  1821. u32 intr_mask;
  1822. int retval;
  1823. int dev_speed;
  1824. if (!hsotg->flags.b.port_connect_status) {
  1825. /* No longer connected */
  1826. dev_err(hsotg->dev, "Not connected\n");
  1827. return -ENODEV;
  1828. }
  1829. dev_speed = dwc2_host_get_speed(hsotg, urb->priv);
  1830. /* Some configurations cannot support LS traffic on a FS root port */
  1831. if ((dev_speed == USB_SPEED_LOW) &&
  1832. (hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED) &&
  1833. (hsotg->hw_params.hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI)) {
  1834. u32 hprt0 = dwc2_readl(hsotg, HPRT0);
  1835. u32 prtspd = (hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT;
  1836. if (prtspd == HPRT0_SPD_FULL_SPEED)
  1837. return -ENODEV;
  1838. }
  1839. if (!qtd)
  1840. return -EINVAL;
  1841. dwc2_hcd_qtd_init(qtd, urb);
  1842. retval = dwc2_hcd_qtd_add(hsotg, qtd, qh);
  1843. if (retval) {
  1844. dev_err(hsotg->dev,
  1845. "DWC OTG HCD URB Enqueue failed adding QTD. Error status %d\n",
  1846. retval);
  1847. return retval;
  1848. }
  1849. intr_mask = dwc2_readl(hsotg, GINTMSK);
  1850. if (!(intr_mask & GINTSTS_SOF)) {
  1851. enum dwc2_transaction_type tr_type;
  1852. if (qtd->qh->ep_type == USB_ENDPOINT_XFER_BULK &&
  1853. !(qtd->urb->flags & URB_GIVEBACK_ASAP))
  1854. /*
  1855. * Do not schedule SG transactions until qtd has
  1856. * URB_GIVEBACK_ASAP set
  1857. */
  1858. return 0;
  1859. tr_type = dwc2_hcd_select_transactions(hsotg);
  1860. if (tr_type != DWC2_TRANSACTION_NONE)
  1861. dwc2_hcd_queue_transactions(hsotg, tr_type);
  1862. }
  1863. return 0;
  1864. }
  1865. /* Must be called with interrupt disabled and spinlock held */
  1866. static int dwc2_hcd_urb_dequeue(struct dwc2_hsotg *hsotg,
  1867. struct dwc2_hcd_urb *urb)
  1868. {
  1869. struct dwc2_qh *qh;
  1870. struct dwc2_qtd *urb_qtd;
  1871. urb_qtd = urb->qtd;
  1872. if (!urb_qtd) {
  1873. dev_dbg(hsotg->dev, "## Urb QTD is NULL ##\n");
  1874. return -EINVAL;
  1875. }
  1876. qh = urb_qtd->qh;
  1877. if (!qh) {
  1878. dev_dbg(hsotg->dev, "## Urb QTD QH is NULL ##\n");
  1879. return -EINVAL;
  1880. }
  1881. urb->priv = NULL;
  1882. if (urb_qtd->in_process && qh->channel) {
  1883. dwc2_dump_channel_info(hsotg, qh->channel);
  1884. /* The QTD is in process (it has been assigned to a channel) */
  1885. if (hsotg->flags.b.port_connect_status)
  1886. /*
  1887. * If still connected (i.e. in host mode), halt the
  1888. * channel so it can be used for other transfers. If
  1889. * no longer connected, the host registers can't be
  1890. * written to halt the channel since the core is in
  1891. * device mode.
  1892. */
  1893. dwc2_hc_halt(hsotg, qh->channel,
  1894. DWC2_HC_XFER_URB_DEQUEUE);
  1895. }
  1896. /*
  1897. * Free the QTD and clean up the associated QH. Leave the QH in the
  1898. * schedule if it has any remaining QTDs.
  1899. */
  1900. if (!hsotg->params.dma_desc_enable) {
  1901. u8 in_process = urb_qtd->in_process;
  1902. dwc2_hcd_qtd_unlink_and_free(hsotg, urb_qtd, qh);
  1903. if (in_process) {
  1904. dwc2_hcd_qh_deactivate(hsotg, qh, 0);
  1905. qh->channel = NULL;
  1906. } else if (list_empty(&qh->qtd_list)) {
  1907. dwc2_hcd_qh_unlink(hsotg, qh);
  1908. }
  1909. } else {
  1910. dwc2_hcd_qtd_unlink_and_free(hsotg, urb_qtd, qh);
  1911. }
  1912. return 0;
  1913. }
  1914. /* Must NOT be called with interrupt disabled or spinlock held */
  1915. static int dwc2_hcd_endpoint_disable(struct dwc2_hsotg *hsotg,
  1916. struct usb_host_endpoint *ep, int retry)
  1917. {
  1918. struct dwc2_qtd *qtd, *qtd_tmp;
  1919. struct dwc2_qh *qh;
  1920. unsigned long flags;
  1921. int rc;
  1922. spin_lock_irqsave(&hsotg->lock, flags);
  1923. qh = ep->hcpriv;
  1924. if (!qh) {
  1925. rc = -EINVAL;
  1926. goto err;
  1927. }
  1928. while (!list_empty(&qh->qtd_list) && retry--) {
  1929. if (retry == 0) {
  1930. dev_err(hsotg->dev,
  1931. "## timeout in dwc2_hcd_endpoint_disable() ##\n");
  1932. rc = -EBUSY;
  1933. goto err;
  1934. }
  1935. spin_unlock_irqrestore(&hsotg->lock, flags);
  1936. msleep(20);
  1937. spin_lock_irqsave(&hsotg->lock, flags);
  1938. qh = ep->hcpriv;
  1939. if (!qh) {
  1940. rc = -EINVAL;
  1941. goto err;
  1942. }
  1943. }
  1944. dwc2_hcd_qh_unlink(hsotg, qh);
  1945. /* Free each QTD in the QH's QTD list */
  1946. list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list, qtd_list_entry)
  1947. dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
  1948. ep->hcpriv = NULL;
  1949. if (qh->channel && qh->channel->qh == qh)
  1950. qh->channel->qh = NULL;
  1951. spin_unlock_irqrestore(&hsotg->lock, flags);
  1952. dwc2_hcd_qh_free(hsotg, qh);
  1953. return 0;
  1954. err:
  1955. ep->hcpriv = NULL;
  1956. spin_unlock_irqrestore(&hsotg->lock, flags);
  1957. return rc;
  1958. }
  1959. /* Must be called with interrupt disabled and spinlock held */
  1960. static int dwc2_hcd_endpoint_reset(struct dwc2_hsotg *hsotg,
  1961. struct usb_host_endpoint *ep)
  1962. {
  1963. struct dwc2_qh *qh = ep->hcpriv;
  1964. if (!qh)
  1965. return -EINVAL;
  1966. qh->data_toggle = DWC2_HC_PID_DATA0;
  1967. return 0;
  1968. }
  1969. /**
  1970. * dwc2_core_init() - Initializes the DWC_otg controller registers and
  1971. * prepares the core for device mode or host mode operation
  1972. *
  1973. * @hsotg: Programming view of the DWC_otg controller
  1974. * @initial_setup: If true then this is the first init for this instance.
  1975. */
  1976. int dwc2_core_init(struct dwc2_hsotg *hsotg, bool initial_setup)
  1977. {
  1978. u32 usbcfg, otgctl;
  1979. int retval;
  1980. dev_dbg(hsotg->dev, "%s(%p)\n", __func__, hsotg);
  1981. usbcfg = dwc2_readl(hsotg, GUSBCFG);
  1982. /* Set ULPI External VBUS bit if needed */
  1983. usbcfg &= ~GUSBCFG_ULPI_EXT_VBUS_DRV;
  1984. if (hsotg->params.phy_ulpi_ext_vbus)
  1985. usbcfg |= GUSBCFG_ULPI_EXT_VBUS_DRV;
  1986. /* Set external TS Dline pulsing bit if needed */
  1987. usbcfg &= ~GUSBCFG_TERMSELDLPULSE;
  1988. if (hsotg->params.ts_dline)
  1989. usbcfg |= GUSBCFG_TERMSELDLPULSE;
  1990. dwc2_writel(hsotg, usbcfg, GUSBCFG);
  1991. /*
  1992. * Reset the Controller
  1993. *
  1994. * We only need to reset the controller if this is a re-init.
  1995. * For the first init we know for sure that earlier code reset us (it
  1996. * needed to in order to properly detect various parameters).
  1997. */
  1998. if (!initial_setup) {
  1999. retval = dwc2_core_reset(hsotg, false);
  2000. if (retval) {
  2001. dev_err(hsotg->dev, "%s(): Reset failed, aborting\n",
  2002. __func__);
  2003. return retval;
  2004. }
  2005. }
  2006. /*
  2007. * This needs to happen in FS mode before any other programming occurs
  2008. */
  2009. retval = dwc2_phy_init(hsotg, initial_setup);
  2010. if (retval)
  2011. return retval;
  2012. /* Program the GAHBCFG Register */
  2013. retval = dwc2_gahbcfg_init(hsotg);
  2014. if (retval)
  2015. return retval;
  2016. /* Program the GUSBCFG register */
  2017. dwc2_gusbcfg_init(hsotg);
  2018. /* Program the GOTGCTL register */
  2019. otgctl = dwc2_readl(hsotg, GOTGCTL);
  2020. otgctl &= ~GOTGCTL_OTGVER;
  2021. dwc2_writel(hsotg, otgctl, GOTGCTL);
  2022. /* Clear the SRP success bit for FS-I2c */
  2023. hsotg->srp_success = 0;
  2024. /* Enable common interrupts */
  2025. dwc2_enable_common_interrupts(hsotg);
  2026. /*
  2027. * Do device or host initialization based on mode during PCD and
  2028. * HCD initialization
  2029. */
  2030. if (dwc2_is_host_mode(hsotg)) {
  2031. dev_dbg(hsotg->dev, "Host Mode\n");
  2032. hsotg->op_state = OTG_STATE_A_HOST;
  2033. } else {
  2034. dev_dbg(hsotg->dev, "Device Mode\n");
  2035. hsotg->op_state = OTG_STATE_B_PERIPHERAL;
  2036. }
  2037. return 0;
  2038. }
  2039. /**
  2040. * dwc2_core_host_init() - Initializes the DWC_otg controller registers for
  2041. * Host mode
  2042. *
  2043. * @hsotg: Programming view of DWC_otg controller
  2044. *
  2045. * This function flushes the Tx and Rx FIFOs and flushes any entries in the
  2046. * request queues. Host channels are reset to ensure that they are ready for
  2047. * performing transfers.
  2048. */
  2049. static void dwc2_core_host_init(struct dwc2_hsotg *hsotg)
  2050. {
  2051. u32 hcfg, hfir, otgctl, usbcfg;
  2052. dev_dbg(hsotg->dev, "%s(%p)\n", __func__, hsotg);
  2053. /* Set HS/FS Timeout Calibration to 7 (max available value).
  2054. * The number of PHY clocks that the application programs in
  2055. * this field is added to the high/full speed interpacket timeout
  2056. * duration in the core to account for any additional delays
  2057. * introduced by the PHY. This can be required, because the delay
  2058. * introduced by the PHY in generating the linestate condition
  2059. * can vary from one PHY to another.
  2060. */
  2061. usbcfg = dwc2_readl(hsotg, GUSBCFG);
  2062. usbcfg |= GUSBCFG_TOUTCAL(7);
  2063. dwc2_writel(hsotg, usbcfg, GUSBCFG);
  2064. /* Restart the Phy Clock */
  2065. dwc2_writel(hsotg, 0, PCGCTL);
  2066. /* Initialize Host Configuration Register */
  2067. dwc2_init_fs_ls_pclk_sel(hsotg);
  2068. if (hsotg->params.speed == DWC2_SPEED_PARAM_FULL ||
  2069. hsotg->params.speed == DWC2_SPEED_PARAM_LOW) {
  2070. hcfg = dwc2_readl(hsotg, HCFG);
  2071. hcfg |= HCFG_FSLSSUPP;
  2072. dwc2_writel(hsotg, hcfg, HCFG);
  2073. }
  2074. /*
  2075. * This bit allows dynamic reloading of the HFIR register during
  2076. * runtime. This bit needs to be programmed during initial configuration
  2077. * and its value must not be changed during runtime.
  2078. */
  2079. if (hsotg->params.reload_ctl) {
  2080. hfir = dwc2_readl(hsotg, HFIR);
  2081. hfir |= HFIR_RLDCTRL;
  2082. dwc2_writel(hsotg, hfir, HFIR);
  2083. }
  2084. if (hsotg->params.dma_desc_enable) {
  2085. u32 op_mode = hsotg->hw_params.op_mode;
  2086. if (hsotg->hw_params.snpsid < DWC2_CORE_REV_2_90a ||
  2087. !hsotg->hw_params.dma_desc_enable ||
  2088. op_mode == GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE ||
  2089. op_mode == GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE ||
  2090. op_mode == GHWCFG2_OP_MODE_UNDEFINED) {
  2091. dev_err(hsotg->dev,
  2092. "Hardware does not support descriptor DMA mode -\n");
  2093. dev_err(hsotg->dev,
  2094. "falling back to buffer DMA mode.\n");
  2095. hsotg->params.dma_desc_enable = false;
  2096. } else {
  2097. hcfg = dwc2_readl(hsotg, HCFG);
  2098. hcfg |= HCFG_DESCDMA;
  2099. dwc2_writel(hsotg, hcfg, HCFG);
  2100. }
  2101. }
  2102. /* Configure data FIFO sizes */
  2103. dwc2_config_fifos(hsotg);
  2104. /* TODO - check this */
  2105. /* Clear Host Set HNP Enable in the OTG Control Register */
  2106. otgctl = dwc2_readl(hsotg, GOTGCTL);
  2107. otgctl &= ~GOTGCTL_HSTSETHNPEN;
  2108. dwc2_writel(hsotg, otgctl, GOTGCTL);
  2109. /* Make sure the FIFOs are flushed */
  2110. dwc2_flush_tx_fifo(hsotg, 0x10 /* all TX FIFOs */);
  2111. dwc2_flush_rx_fifo(hsotg);
  2112. /* Clear Host Set HNP Enable in the OTG Control Register */
  2113. otgctl = dwc2_readl(hsotg, GOTGCTL);
  2114. otgctl &= ~GOTGCTL_HSTSETHNPEN;
  2115. dwc2_writel(hsotg, otgctl, GOTGCTL);
  2116. if (!hsotg->params.dma_desc_enable) {
  2117. int num_channels, i;
  2118. u32 hcchar;
  2119. /* Flush out any leftover queued requests */
  2120. num_channels = hsotg->params.host_channels;
  2121. for (i = 0; i < num_channels; i++) {
  2122. hcchar = dwc2_readl(hsotg, HCCHAR(i));
  2123. hcchar &= ~HCCHAR_CHENA;
  2124. hcchar |= HCCHAR_CHDIS;
  2125. hcchar &= ~HCCHAR_EPDIR;
  2126. dwc2_writel(hsotg, hcchar, HCCHAR(i));
  2127. }
  2128. /* Halt all channels to put them into a known state */
  2129. for (i = 0; i < num_channels; i++) {
  2130. hcchar = dwc2_readl(hsotg, HCCHAR(i));
  2131. hcchar |= HCCHAR_CHENA | HCCHAR_CHDIS;
  2132. hcchar &= ~HCCHAR_EPDIR;
  2133. dwc2_writel(hsotg, hcchar, HCCHAR(i));
  2134. dev_dbg(hsotg->dev, "%s: Halt channel %d\n",
  2135. __func__, i);
  2136. if (dwc2_hsotg_wait_bit_clear(hsotg, HCCHAR(i),
  2137. HCCHAR_CHENA, 1000)) {
  2138. dev_warn(hsotg->dev, "Unable to clear enable on channel %d\n",
  2139. i);
  2140. }
  2141. }
  2142. }
  2143. /* Enable ACG feature in host mode, if supported */
  2144. dwc2_enable_acg(hsotg);
  2145. /* Turn on the vbus power */
  2146. dev_dbg(hsotg->dev, "Init: Port Power? op_state=%d\n", hsotg->op_state);
  2147. if (hsotg->op_state == OTG_STATE_A_HOST) {
  2148. u32 hprt0 = dwc2_read_hprt0(hsotg);
  2149. dev_dbg(hsotg->dev, "Init: Power Port (%d)\n",
  2150. !!(hprt0 & HPRT0_PWR));
  2151. if (!(hprt0 & HPRT0_PWR)) {
  2152. hprt0 |= HPRT0_PWR;
  2153. dwc2_writel(hsotg, hprt0, HPRT0);
  2154. }
  2155. }
  2156. dwc2_enable_host_interrupts(hsotg);
  2157. }
  2158. /*
  2159. * Initializes dynamic portions of the DWC_otg HCD state
  2160. *
  2161. * Must be called with interrupt disabled and spinlock held
  2162. */
  2163. static void dwc2_hcd_reinit(struct dwc2_hsotg *hsotg)
  2164. {
  2165. struct dwc2_host_chan *chan, *chan_tmp;
  2166. int num_channels;
  2167. int i;
  2168. hsotg->flags.d32 = 0;
  2169. hsotg->non_periodic_qh_ptr = &hsotg->non_periodic_sched_active;
  2170. if (hsotg->params.uframe_sched) {
  2171. hsotg->available_host_channels =
  2172. hsotg->params.host_channels;
  2173. } else {
  2174. hsotg->non_periodic_channels = 0;
  2175. hsotg->periodic_channels = 0;
  2176. }
  2177. /*
  2178. * Put all channels in the free channel list and clean up channel
  2179. * states
  2180. */
  2181. list_for_each_entry_safe(chan, chan_tmp, &hsotg->free_hc_list,
  2182. hc_list_entry)
  2183. list_del_init(&chan->hc_list_entry);
  2184. num_channels = hsotg->params.host_channels;
  2185. for (i = 0; i < num_channels; i++) {
  2186. chan = hsotg->hc_ptr_array[i];
  2187. list_add_tail(&chan->hc_list_entry, &hsotg->free_hc_list);
  2188. dwc2_hc_cleanup(hsotg, chan);
  2189. }
  2190. /* Initialize the DWC core for host mode operation */
  2191. dwc2_core_host_init(hsotg);
  2192. }
  2193. static void dwc2_hc_init_split(struct dwc2_hsotg *hsotg,
  2194. struct dwc2_host_chan *chan,
  2195. struct dwc2_qtd *qtd, struct dwc2_hcd_urb *urb)
  2196. {
  2197. int hub_addr, hub_port;
  2198. chan->do_split = 1;
  2199. chan->xact_pos = qtd->isoc_split_pos;
  2200. chan->complete_split = qtd->complete_split;
  2201. dwc2_host_hub_info(hsotg, urb->priv, &hub_addr, &hub_port);
  2202. chan->hub_addr = (u8)hub_addr;
  2203. chan->hub_port = (u8)hub_port;
  2204. }
  2205. static void dwc2_hc_init_xfer(struct dwc2_hsotg *hsotg,
  2206. struct dwc2_host_chan *chan,
  2207. struct dwc2_qtd *qtd)
  2208. {
  2209. struct dwc2_hcd_urb *urb = qtd->urb;
  2210. struct dwc2_hcd_iso_packet_desc *frame_desc;
  2211. switch (dwc2_hcd_get_pipe_type(&urb->pipe_info)) {
  2212. case USB_ENDPOINT_XFER_CONTROL:
  2213. chan->ep_type = USB_ENDPOINT_XFER_CONTROL;
  2214. switch (qtd->control_phase) {
  2215. case DWC2_CONTROL_SETUP:
  2216. dev_vdbg(hsotg->dev, " Control setup transaction\n");
  2217. chan->do_ping = 0;
  2218. chan->ep_is_in = 0;
  2219. chan->data_pid_start = DWC2_HC_PID_SETUP;
  2220. if (hsotg->params.host_dma)
  2221. chan->xfer_dma = urb->setup_dma;
  2222. else
  2223. chan->xfer_buf = urb->setup_packet;
  2224. chan->xfer_len = 8;
  2225. break;
  2226. case DWC2_CONTROL_DATA:
  2227. dev_vdbg(hsotg->dev, " Control data transaction\n");
  2228. chan->data_pid_start = qtd->data_toggle;
  2229. break;
  2230. case DWC2_CONTROL_STATUS:
  2231. /*
  2232. * Direction is opposite of data direction or IN if no
  2233. * data
  2234. */
  2235. dev_vdbg(hsotg->dev, " Control status transaction\n");
  2236. if (urb->length == 0)
  2237. chan->ep_is_in = 1;
  2238. else
  2239. chan->ep_is_in =
  2240. dwc2_hcd_is_pipe_out(&urb->pipe_info);
  2241. if (chan->ep_is_in)
  2242. chan->do_ping = 0;
  2243. chan->data_pid_start = DWC2_HC_PID_DATA1;
  2244. chan->xfer_len = 0;
  2245. if (hsotg->params.host_dma)
  2246. chan->xfer_dma = hsotg->status_buf_dma;
  2247. else
  2248. chan->xfer_buf = hsotg->status_buf;
  2249. break;
  2250. }
  2251. break;
  2252. case USB_ENDPOINT_XFER_BULK:
  2253. chan->ep_type = USB_ENDPOINT_XFER_BULK;
  2254. break;
  2255. case USB_ENDPOINT_XFER_INT:
  2256. chan->ep_type = USB_ENDPOINT_XFER_INT;
  2257. break;
  2258. case USB_ENDPOINT_XFER_ISOC:
  2259. chan->ep_type = USB_ENDPOINT_XFER_ISOC;
  2260. if (hsotg->params.dma_desc_enable)
  2261. break;
  2262. frame_desc = &urb->iso_descs[qtd->isoc_frame_index];
  2263. frame_desc->status = 0;
  2264. if (hsotg->params.host_dma) {
  2265. chan->xfer_dma = urb->dma;
  2266. chan->xfer_dma += frame_desc->offset +
  2267. qtd->isoc_split_offset;
  2268. } else {
  2269. chan->xfer_buf = urb->buf;
  2270. chan->xfer_buf += frame_desc->offset +
  2271. qtd->isoc_split_offset;
  2272. }
  2273. chan->xfer_len = frame_desc->length - qtd->isoc_split_offset;
  2274. if (chan->xact_pos == DWC2_HCSPLT_XACTPOS_ALL) {
  2275. if (chan->xfer_len <= 188)
  2276. chan->xact_pos = DWC2_HCSPLT_XACTPOS_ALL;
  2277. else
  2278. chan->xact_pos = DWC2_HCSPLT_XACTPOS_BEGIN;
  2279. }
  2280. break;
  2281. }
  2282. }
  2283. static int dwc2_alloc_split_dma_aligned_buf(struct dwc2_hsotg *hsotg,
  2284. struct dwc2_qh *qh,
  2285. struct dwc2_host_chan *chan)
  2286. {
  2287. if (!hsotg->unaligned_cache ||
  2288. chan->max_packet > DWC2_KMEM_UNALIGNED_BUF_SIZE)
  2289. return -ENOMEM;
  2290. if (!qh->dw_align_buf) {
  2291. qh->dw_align_buf = kmem_cache_alloc(hsotg->unaligned_cache,
  2292. GFP_ATOMIC | GFP_DMA);
  2293. if (!qh->dw_align_buf)
  2294. return -ENOMEM;
  2295. }
  2296. qh->dw_align_buf_dma = dma_map_single(hsotg->dev, qh->dw_align_buf,
  2297. DWC2_KMEM_UNALIGNED_BUF_SIZE,
  2298. DMA_FROM_DEVICE);
  2299. if (dma_mapping_error(hsotg->dev, qh->dw_align_buf_dma)) {
  2300. dev_err(hsotg->dev, "can't map align_buf\n");
  2301. chan->align_buf = 0;
  2302. return -EINVAL;
  2303. }
  2304. chan->align_buf = qh->dw_align_buf_dma;
  2305. return 0;
  2306. }
  2307. #define DWC2_USB_DMA_ALIGN 4
  2308. static void dwc2_free_dma_aligned_buffer(struct urb *urb)
  2309. {
  2310. void *stored_xfer_buffer;
  2311. size_t length;
  2312. if (!(urb->transfer_flags & URB_ALIGNED_TEMP_BUFFER))
  2313. return;
  2314. /* Restore urb->transfer_buffer from the end of the allocated area */
  2315. memcpy(&stored_xfer_buffer,
  2316. PTR_ALIGN(urb->transfer_buffer + urb->transfer_buffer_length,
  2317. dma_get_cache_alignment()),
  2318. sizeof(urb->transfer_buffer));
  2319. if (usb_urb_dir_in(urb)) {
  2320. if (usb_pipeisoc(urb->pipe))
  2321. length = urb->transfer_buffer_length;
  2322. else
  2323. length = urb->actual_length;
  2324. memcpy(stored_xfer_buffer, urb->transfer_buffer, length);
  2325. }
  2326. kfree(urb->transfer_buffer);
  2327. urb->transfer_buffer = stored_xfer_buffer;
  2328. urb->transfer_flags &= ~URB_ALIGNED_TEMP_BUFFER;
  2329. }
  2330. static int dwc2_alloc_dma_aligned_buffer(struct urb *urb, gfp_t mem_flags)
  2331. {
  2332. void *kmalloc_ptr;
  2333. size_t kmalloc_size;
  2334. if (urb->num_sgs || urb->sg ||
  2335. urb->transfer_buffer_length == 0 ||
  2336. !((uintptr_t)urb->transfer_buffer & (DWC2_USB_DMA_ALIGN - 1)))
  2337. return 0;
  2338. /*
  2339. * Allocate a buffer with enough padding for original transfer_buffer
  2340. * pointer. This allocation is guaranteed to be aligned properly for
  2341. * DMA
  2342. */
  2343. kmalloc_size = urb->transfer_buffer_length +
  2344. (dma_get_cache_alignment() - 1) +
  2345. sizeof(urb->transfer_buffer);
  2346. kmalloc_ptr = kmalloc(kmalloc_size, mem_flags);
  2347. if (!kmalloc_ptr)
  2348. return -ENOMEM;
  2349. /*
  2350. * Position value of original urb->transfer_buffer pointer to the end
  2351. * of allocation for later referencing
  2352. */
  2353. memcpy(PTR_ALIGN(kmalloc_ptr + urb->transfer_buffer_length,
  2354. dma_get_cache_alignment()),
  2355. &urb->transfer_buffer, sizeof(urb->transfer_buffer));
  2356. if (usb_urb_dir_out(urb))
  2357. memcpy(kmalloc_ptr, urb->transfer_buffer,
  2358. urb->transfer_buffer_length);
  2359. urb->transfer_buffer = kmalloc_ptr;
  2360. urb->transfer_flags |= URB_ALIGNED_TEMP_BUFFER;
  2361. return 0;
  2362. }
  2363. static int dwc2_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
  2364. gfp_t mem_flags)
  2365. {
  2366. int ret;
  2367. /* We assume setup_dma is always aligned; warn if not */
  2368. WARN_ON_ONCE(urb->setup_dma &&
  2369. (urb->setup_dma & (DWC2_USB_DMA_ALIGN - 1)));
  2370. ret = dwc2_alloc_dma_aligned_buffer(urb, mem_flags);
  2371. if (ret)
  2372. return ret;
  2373. ret = usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
  2374. if (ret)
  2375. dwc2_free_dma_aligned_buffer(urb);
  2376. return ret;
  2377. }
  2378. static void dwc2_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
  2379. {
  2380. usb_hcd_unmap_urb_for_dma(hcd, urb);
  2381. dwc2_free_dma_aligned_buffer(urb);
  2382. }
  2383. /**
  2384. * dwc2_assign_and_init_hc() - Assigns transactions from a QTD to a free host
  2385. * channel and initializes the host channel to perform the transactions. The
  2386. * host channel is removed from the free list.
  2387. *
  2388. * @hsotg: The HCD state structure
  2389. * @qh: Transactions from the first QTD for this QH are selected and assigned
  2390. * to a free host channel
  2391. */
  2392. static int dwc2_assign_and_init_hc(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
  2393. {
  2394. struct dwc2_host_chan *chan;
  2395. struct dwc2_hcd_urb *urb;
  2396. struct dwc2_qtd *qtd;
  2397. if (dbg_qh(qh))
  2398. dev_vdbg(hsotg->dev, "%s(%p,%p)\n", __func__, hsotg, qh);
  2399. if (list_empty(&qh->qtd_list)) {
  2400. dev_dbg(hsotg->dev, "No QTDs in QH list\n");
  2401. return -ENOMEM;
  2402. }
  2403. if (list_empty(&hsotg->free_hc_list)) {
  2404. dev_dbg(hsotg->dev, "No free channel to assign\n");
  2405. return -ENOMEM;
  2406. }
  2407. chan = list_first_entry(&hsotg->free_hc_list, struct dwc2_host_chan,
  2408. hc_list_entry);
  2409. /* Remove host channel from free list */
  2410. list_del_init(&chan->hc_list_entry);
  2411. qtd = list_first_entry(&qh->qtd_list, struct dwc2_qtd, qtd_list_entry);
  2412. urb = qtd->urb;
  2413. qh->channel = chan;
  2414. qtd->in_process = 1;
  2415. /*
  2416. * Use usb_pipedevice to determine device address. This address is
  2417. * 0 before the SET_ADDRESS command and the correct address afterward.
  2418. */
  2419. chan->dev_addr = dwc2_hcd_get_dev_addr(&urb->pipe_info);
  2420. chan->ep_num = dwc2_hcd_get_ep_num(&urb->pipe_info);
  2421. chan->speed = qh->dev_speed;
  2422. chan->max_packet = qh->maxp;
  2423. chan->xfer_started = 0;
  2424. chan->halt_status = DWC2_HC_XFER_NO_HALT_STATUS;
  2425. chan->error_state = (qtd->error_count > 0);
  2426. chan->halt_on_queue = 0;
  2427. chan->halt_pending = 0;
  2428. chan->requests = 0;
  2429. /*
  2430. * The following values may be modified in the transfer type section
  2431. * below. The xfer_len value may be reduced when the transfer is
  2432. * started to accommodate the max widths of the XferSize and PktCnt
  2433. * fields in the HCTSIZn register.
  2434. */
  2435. chan->ep_is_in = (dwc2_hcd_is_pipe_in(&urb->pipe_info) != 0);
  2436. if (chan->ep_is_in)
  2437. chan->do_ping = 0;
  2438. else
  2439. chan->do_ping = qh->ping_state;
  2440. chan->data_pid_start = qh->data_toggle;
  2441. chan->multi_count = 1;
  2442. if (urb->actual_length > urb->length &&
  2443. !dwc2_hcd_is_pipe_in(&urb->pipe_info))
  2444. urb->actual_length = urb->length;
  2445. if (hsotg->params.host_dma)
  2446. chan->xfer_dma = urb->dma + urb->actual_length;
  2447. else
  2448. chan->xfer_buf = (u8 *)urb->buf + urb->actual_length;
  2449. chan->xfer_len = urb->length - urb->actual_length;
  2450. chan->xfer_count = 0;
  2451. /* Set the split attributes if required */
  2452. if (qh->do_split)
  2453. dwc2_hc_init_split(hsotg, chan, qtd, urb);
  2454. else
  2455. chan->do_split = 0;
  2456. /* Set the transfer attributes */
  2457. dwc2_hc_init_xfer(hsotg, chan, qtd);
  2458. /* For non-dword aligned buffers */
  2459. if (hsotg->params.host_dma && qh->do_split &&
  2460. chan->ep_is_in && (chan->xfer_dma & 0x3)) {
  2461. dev_vdbg(hsotg->dev, "Non-aligned buffer\n");
  2462. if (dwc2_alloc_split_dma_aligned_buf(hsotg, qh, chan)) {
  2463. dev_err(hsotg->dev,
  2464. "Failed to allocate memory to handle non-aligned buffer\n");
  2465. /* Add channel back to free list */
  2466. chan->align_buf = 0;
  2467. chan->multi_count = 0;
  2468. list_add_tail(&chan->hc_list_entry,
  2469. &hsotg->free_hc_list);
  2470. qtd->in_process = 0;
  2471. qh->channel = NULL;
  2472. return -ENOMEM;
  2473. }
  2474. } else {
  2475. /*
  2476. * We assume that DMA is always aligned in non-split
  2477. * case or split out case. Warn if not.
  2478. */
  2479. WARN_ON_ONCE(hsotg->params.host_dma &&
  2480. (chan->xfer_dma & 0x3));
  2481. chan->align_buf = 0;
  2482. }
  2483. if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
  2484. chan->ep_type == USB_ENDPOINT_XFER_ISOC)
  2485. /*
  2486. * This value may be modified when the transfer is started
  2487. * to reflect the actual transfer length
  2488. */
  2489. chan->multi_count = qh->maxp_mult;
  2490. if (hsotg->params.dma_desc_enable) {
  2491. chan->desc_list_addr = qh->desc_list_dma;
  2492. chan->desc_list_sz = qh->desc_list_sz;
  2493. }
  2494. dwc2_hc_init(hsotg, chan);
  2495. chan->qh = qh;
  2496. return 0;
  2497. }
  2498. /**
  2499. * dwc2_hcd_select_transactions() - Selects transactions from the HCD transfer
  2500. * schedule and assigns them to available host channels. Called from the HCD
  2501. * interrupt handler functions.
  2502. *
  2503. * @hsotg: The HCD state structure
  2504. *
  2505. * Return: The types of new transactions that were assigned to host channels
  2506. */
  2507. enum dwc2_transaction_type dwc2_hcd_select_transactions(
  2508. struct dwc2_hsotg *hsotg)
  2509. {
  2510. enum dwc2_transaction_type ret_val = DWC2_TRANSACTION_NONE;
  2511. struct list_head *qh_ptr;
  2512. struct dwc2_qh *qh;
  2513. int num_channels;
  2514. #ifdef DWC2_DEBUG_SOF
  2515. dev_vdbg(hsotg->dev, " Select Transactions\n");
  2516. #endif
  2517. /* Process entries in the periodic ready list */
  2518. qh_ptr = hsotg->periodic_sched_ready.next;
  2519. while (qh_ptr != &hsotg->periodic_sched_ready) {
  2520. if (list_empty(&hsotg->free_hc_list))
  2521. break;
  2522. if (hsotg->params.uframe_sched) {
  2523. if (hsotg->available_host_channels <= 1)
  2524. break;
  2525. hsotg->available_host_channels--;
  2526. }
  2527. qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
  2528. if (dwc2_assign_and_init_hc(hsotg, qh))
  2529. break;
  2530. /*
  2531. * Move the QH from the periodic ready schedule to the
  2532. * periodic assigned schedule
  2533. */
  2534. qh_ptr = qh_ptr->next;
  2535. list_move_tail(&qh->qh_list_entry,
  2536. &hsotg->periodic_sched_assigned);
  2537. ret_val = DWC2_TRANSACTION_PERIODIC;
  2538. }
  2539. /*
  2540. * Process entries in the inactive portion of the non-periodic
  2541. * schedule. Some free host channels may not be used if they are
  2542. * reserved for periodic transfers.
  2543. */
  2544. num_channels = hsotg->params.host_channels;
  2545. qh_ptr = hsotg->non_periodic_sched_inactive.next;
  2546. while (qh_ptr != &hsotg->non_periodic_sched_inactive) {
  2547. if (!hsotg->params.uframe_sched &&
  2548. hsotg->non_periodic_channels >= num_channels -
  2549. hsotg->periodic_channels)
  2550. break;
  2551. if (list_empty(&hsotg->free_hc_list))
  2552. break;
  2553. qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
  2554. if (hsotg->params.uframe_sched) {
  2555. if (hsotg->available_host_channels < 1)
  2556. break;
  2557. hsotg->available_host_channels--;
  2558. }
  2559. if (dwc2_assign_and_init_hc(hsotg, qh))
  2560. break;
  2561. /*
  2562. * Move the QH from the non-periodic inactive schedule to the
  2563. * non-periodic active schedule
  2564. */
  2565. qh_ptr = qh_ptr->next;
  2566. list_move_tail(&qh->qh_list_entry,
  2567. &hsotg->non_periodic_sched_active);
  2568. if (ret_val == DWC2_TRANSACTION_NONE)
  2569. ret_val = DWC2_TRANSACTION_NON_PERIODIC;
  2570. else
  2571. ret_val = DWC2_TRANSACTION_ALL;
  2572. if (!hsotg->params.uframe_sched)
  2573. hsotg->non_periodic_channels++;
  2574. }
  2575. return ret_val;
  2576. }
  2577. /**
  2578. * dwc2_queue_transaction() - Attempts to queue a single transaction request for
  2579. * a host channel associated with either a periodic or non-periodic transfer
  2580. *
  2581. * @hsotg: The HCD state structure
  2582. * @chan: Host channel descriptor associated with either a periodic or
  2583. * non-periodic transfer
  2584. * @fifo_dwords_avail: Number of DWORDs available in the periodic Tx FIFO
  2585. * for periodic transfers or the non-periodic Tx FIFO
  2586. * for non-periodic transfers
  2587. *
  2588. * Return: 1 if a request is queued and more requests may be needed to
  2589. * complete the transfer, 0 if no more requests are required for this
  2590. * transfer, -1 if there is insufficient space in the Tx FIFO
  2591. *
  2592. * This function assumes that there is space available in the appropriate
  2593. * request queue. For an OUT transfer or SETUP transaction in Slave mode,
  2594. * it checks whether space is available in the appropriate Tx FIFO.
  2595. *
  2596. * Must be called with interrupt disabled and spinlock held
  2597. */
  2598. static int dwc2_queue_transaction(struct dwc2_hsotg *hsotg,
  2599. struct dwc2_host_chan *chan,
  2600. u16 fifo_dwords_avail)
  2601. {
  2602. int retval = 0;
  2603. if (chan->do_split)
  2604. /* Put ourselves on the list to keep order straight */
  2605. list_move_tail(&chan->split_order_list_entry,
  2606. &hsotg->split_order);
  2607. if (hsotg->params.host_dma) {
  2608. if (hsotg->params.dma_desc_enable) {
  2609. if (!chan->xfer_started ||
  2610. chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
  2611. dwc2_hcd_start_xfer_ddma(hsotg, chan->qh);
  2612. chan->qh->ping_state = 0;
  2613. }
  2614. } else if (!chan->xfer_started) {
  2615. dwc2_hc_start_transfer(hsotg, chan);
  2616. chan->qh->ping_state = 0;
  2617. }
  2618. } else if (chan->halt_pending) {
  2619. /* Don't queue a request if the channel has been halted */
  2620. } else if (chan->halt_on_queue) {
  2621. dwc2_hc_halt(hsotg, chan, chan->halt_status);
  2622. } else if (chan->do_ping) {
  2623. if (!chan->xfer_started)
  2624. dwc2_hc_start_transfer(hsotg, chan);
  2625. } else if (!chan->ep_is_in ||
  2626. chan->data_pid_start == DWC2_HC_PID_SETUP) {
  2627. if ((fifo_dwords_avail * 4) >= chan->max_packet) {
  2628. if (!chan->xfer_started) {
  2629. dwc2_hc_start_transfer(hsotg, chan);
  2630. retval = 1;
  2631. } else {
  2632. retval = dwc2_hc_continue_transfer(hsotg, chan);
  2633. }
  2634. } else {
  2635. retval = -1;
  2636. }
  2637. } else {
  2638. if (!chan->xfer_started) {
  2639. dwc2_hc_start_transfer(hsotg, chan);
  2640. retval = 1;
  2641. } else {
  2642. retval = dwc2_hc_continue_transfer(hsotg, chan);
  2643. }
  2644. }
  2645. return retval;
  2646. }
  2647. /*
  2648. * Processes periodic channels for the next frame and queues transactions for
  2649. * these channels to the DWC_otg controller. After queueing transactions, the
  2650. * Periodic Tx FIFO Empty interrupt is enabled if there are more transactions
  2651. * to queue as Periodic Tx FIFO or request queue space becomes available.
  2652. * Otherwise, the Periodic Tx FIFO Empty interrupt is disabled.
  2653. *
  2654. * Must be called with interrupt disabled and spinlock held
  2655. */
  2656. static void dwc2_process_periodic_channels(struct dwc2_hsotg *hsotg)
  2657. {
  2658. struct list_head *qh_ptr;
  2659. struct dwc2_qh *qh;
  2660. u32 tx_status;
  2661. u32 fspcavail;
  2662. u32 gintmsk;
  2663. int status;
  2664. bool no_queue_space = false;
  2665. bool no_fifo_space = false;
  2666. u32 qspcavail;
  2667. /* If empty list then just adjust interrupt enables */
  2668. if (list_empty(&hsotg->periodic_sched_assigned))
  2669. goto exit;
  2670. if (dbg_perio())
  2671. dev_vdbg(hsotg->dev, "Queue periodic transactions\n");
  2672. tx_status = dwc2_readl(hsotg, HPTXSTS);
  2673. qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
  2674. TXSTS_QSPCAVAIL_SHIFT;
  2675. fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
  2676. TXSTS_FSPCAVAIL_SHIFT;
  2677. if (dbg_perio()) {
  2678. dev_vdbg(hsotg->dev, " P Tx Req Queue Space Avail (before queue): %d\n",
  2679. qspcavail);
  2680. dev_vdbg(hsotg->dev, " P Tx FIFO Space Avail (before queue): %d\n",
  2681. fspcavail);
  2682. }
  2683. qh_ptr = hsotg->periodic_sched_assigned.next;
  2684. while (qh_ptr != &hsotg->periodic_sched_assigned) {
  2685. tx_status = dwc2_readl(hsotg, HPTXSTS);
  2686. qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
  2687. TXSTS_QSPCAVAIL_SHIFT;
  2688. if (qspcavail == 0) {
  2689. no_queue_space = true;
  2690. break;
  2691. }
  2692. qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
  2693. if (!qh->channel) {
  2694. qh_ptr = qh_ptr->next;
  2695. continue;
  2696. }
  2697. /* Make sure EP's TT buffer is clean before queueing qtds */
  2698. if (qh->tt_buffer_dirty) {
  2699. qh_ptr = qh_ptr->next;
  2700. continue;
  2701. }
  2702. /*
  2703. * Set a flag if we're queuing high-bandwidth in slave mode.
  2704. * The flag prevents any halts to get into the request queue in
  2705. * the middle of multiple high-bandwidth packets getting queued.
  2706. */
  2707. if (!hsotg->params.host_dma &&
  2708. qh->channel->multi_count > 1)
  2709. hsotg->queuing_high_bandwidth = 1;
  2710. fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
  2711. TXSTS_FSPCAVAIL_SHIFT;
  2712. status = dwc2_queue_transaction(hsotg, qh->channel, fspcavail);
  2713. if (status < 0) {
  2714. no_fifo_space = true;
  2715. break;
  2716. }
  2717. /*
  2718. * In Slave mode, stay on the current transfer until there is
  2719. * nothing more to do or the high-bandwidth request count is
  2720. * reached. In DMA mode, only need to queue one request. The
  2721. * controller automatically handles multiple packets for
  2722. * high-bandwidth transfers.
  2723. */
  2724. if (hsotg->params.host_dma || status == 0 ||
  2725. qh->channel->requests == qh->channel->multi_count) {
  2726. qh_ptr = qh_ptr->next;
  2727. /*
  2728. * Move the QH from the periodic assigned schedule to
  2729. * the periodic queued schedule
  2730. */
  2731. list_move_tail(&qh->qh_list_entry,
  2732. &hsotg->periodic_sched_queued);
  2733. /* done queuing high bandwidth */
  2734. hsotg->queuing_high_bandwidth = 0;
  2735. }
  2736. }
  2737. exit:
  2738. if (no_queue_space || no_fifo_space ||
  2739. (!hsotg->params.host_dma &&
  2740. !list_empty(&hsotg->periodic_sched_assigned))) {
  2741. /*
  2742. * May need to queue more transactions as the request
  2743. * queue or Tx FIFO empties. Enable the periodic Tx
  2744. * FIFO empty interrupt. (Always use the half-empty
  2745. * level to ensure that new requests are loaded as
  2746. * soon as possible.)
  2747. */
  2748. gintmsk = dwc2_readl(hsotg, GINTMSK);
  2749. if (!(gintmsk & GINTSTS_PTXFEMP)) {
  2750. gintmsk |= GINTSTS_PTXFEMP;
  2751. dwc2_writel(hsotg, gintmsk, GINTMSK);
  2752. }
  2753. } else {
  2754. /*
  2755. * Disable the Tx FIFO empty interrupt since there are
  2756. * no more transactions that need to be queued right
  2757. * now. This function is called from interrupt
  2758. * handlers to queue more transactions as transfer
  2759. * states change.
  2760. */
  2761. gintmsk = dwc2_readl(hsotg, GINTMSK);
  2762. if (gintmsk & GINTSTS_PTXFEMP) {
  2763. gintmsk &= ~GINTSTS_PTXFEMP;
  2764. dwc2_writel(hsotg, gintmsk, GINTMSK);
  2765. }
  2766. }
  2767. }
  2768. /*
  2769. * Processes active non-periodic channels and queues transactions for these
  2770. * channels to the DWC_otg controller. After queueing transactions, the NP Tx
  2771. * FIFO Empty interrupt is enabled if there are more transactions to queue as
  2772. * NP Tx FIFO or request queue space becomes available. Otherwise, the NP Tx
  2773. * FIFO Empty interrupt is disabled.
  2774. *
  2775. * Must be called with interrupt disabled and spinlock held
  2776. */
  2777. static void dwc2_process_non_periodic_channels(struct dwc2_hsotg *hsotg)
  2778. {
  2779. struct list_head *orig_qh_ptr;
  2780. struct dwc2_qh *qh;
  2781. u32 tx_status;
  2782. u32 qspcavail;
  2783. u32 fspcavail;
  2784. u32 gintmsk;
  2785. int status;
  2786. int no_queue_space = 0;
  2787. int no_fifo_space = 0;
  2788. int more_to_do = 0;
  2789. dev_vdbg(hsotg->dev, "Queue non-periodic transactions\n");
  2790. tx_status = dwc2_readl(hsotg, GNPTXSTS);
  2791. qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
  2792. TXSTS_QSPCAVAIL_SHIFT;
  2793. fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
  2794. TXSTS_FSPCAVAIL_SHIFT;
  2795. dev_vdbg(hsotg->dev, " NP Tx Req Queue Space Avail (before queue): %d\n",
  2796. qspcavail);
  2797. dev_vdbg(hsotg->dev, " NP Tx FIFO Space Avail (before queue): %d\n",
  2798. fspcavail);
  2799. /*
  2800. * Keep track of the starting point. Skip over the start-of-list
  2801. * entry.
  2802. */
  2803. if (hsotg->non_periodic_qh_ptr == &hsotg->non_periodic_sched_active)
  2804. hsotg->non_periodic_qh_ptr = hsotg->non_periodic_qh_ptr->next;
  2805. orig_qh_ptr = hsotg->non_periodic_qh_ptr;
  2806. /*
  2807. * Process once through the active list or until no more space is
  2808. * available in the request queue or the Tx FIFO
  2809. */
  2810. do {
  2811. tx_status = dwc2_readl(hsotg, GNPTXSTS);
  2812. qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
  2813. TXSTS_QSPCAVAIL_SHIFT;
  2814. if (!hsotg->params.host_dma && qspcavail == 0) {
  2815. no_queue_space = 1;
  2816. break;
  2817. }
  2818. qh = list_entry(hsotg->non_periodic_qh_ptr, struct dwc2_qh,
  2819. qh_list_entry);
  2820. if (!qh->channel)
  2821. goto next;
  2822. /* Make sure EP's TT buffer is clean before queueing qtds */
  2823. if (qh->tt_buffer_dirty)
  2824. goto next;
  2825. fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
  2826. TXSTS_FSPCAVAIL_SHIFT;
  2827. status = dwc2_queue_transaction(hsotg, qh->channel, fspcavail);
  2828. if (status > 0) {
  2829. more_to_do = 1;
  2830. } else if (status < 0) {
  2831. no_fifo_space = 1;
  2832. break;
  2833. }
  2834. next:
  2835. /* Advance to next QH, skipping start-of-list entry */
  2836. hsotg->non_periodic_qh_ptr = hsotg->non_periodic_qh_ptr->next;
  2837. if (hsotg->non_periodic_qh_ptr ==
  2838. &hsotg->non_periodic_sched_active)
  2839. hsotg->non_periodic_qh_ptr =
  2840. hsotg->non_periodic_qh_ptr->next;
  2841. } while (hsotg->non_periodic_qh_ptr != orig_qh_ptr);
  2842. if (!hsotg->params.host_dma) {
  2843. tx_status = dwc2_readl(hsotg, GNPTXSTS);
  2844. qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
  2845. TXSTS_QSPCAVAIL_SHIFT;
  2846. fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
  2847. TXSTS_FSPCAVAIL_SHIFT;
  2848. dev_vdbg(hsotg->dev,
  2849. " NP Tx Req Queue Space Avail (after queue): %d\n",
  2850. qspcavail);
  2851. dev_vdbg(hsotg->dev,
  2852. " NP Tx FIFO Space Avail (after queue): %d\n",
  2853. fspcavail);
  2854. if (more_to_do || no_queue_space || no_fifo_space) {
  2855. /*
  2856. * May need to queue more transactions as the request
  2857. * queue or Tx FIFO empties. Enable the non-periodic
  2858. * Tx FIFO empty interrupt. (Always use the half-empty
  2859. * level to ensure that new requests are loaded as
  2860. * soon as possible.)
  2861. */
  2862. gintmsk = dwc2_readl(hsotg, GINTMSK);
  2863. gintmsk |= GINTSTS_NPTXFEMP;
  2864. dwc2_writel(hsotg, gintmsk, GINTMSK);
  2865. } else {
  2866. /*
  2867. * Disable the Tx FIFO empty interrupt since there are
  2868. * no more transactions that need to be queued right
  2869. * now. This function is called from interrupt
  2870. * handlers to queue more transactions as transfer
  2871. * states change.
  2872. */
  2873. gintmsk = dwc2_readl(hsotg, GINTMSK);
  2874. gintmsk &= ~GINTSTS_NPTXFEMP;
  2875. dwc2_writel(hsotg, gintmsk, GINTMSK);
  2876. }
  2877. }
  2878. }
  2879. /**
  2880. * dwc2_hcd_queue_transactions() - Processes the currently active host channels
  2881. * and queues transactions for these channels to the DWC_otg controller. Called
  2882. * from the HCD interrupt handler functions.
  2883. *
  2884. * @hsotg: The HCD state structure
  2885. * @tr_type: The type(s) of transactions to queue (non-periodic, periodic,
  2886. * or both)
  2887. *
  2888. * Must be called with interrupt disabled and spinlock held
  2889. */
  2890. void dwc2_hcd_queue_transactions(struct dwc2_hsotg *hsotg,
  2891. enum dwc2_transaction_type tr_type)
  2892. {
  2893. #ifdef DWC2_DEBUG_SOF
  2894. dev_vdbg(hsotg->dev, "Queue Transactions\n");
  2895. #endif
  2896. /* Process host channels associated with periodic transfers */
  2897. if (tr_type == DWC2_TRANSACTION_PERIODIC ||
  2898. tr_type == DWC2_TRANSACTION_ALL)
  2899. dwc2_process_periodic_channels(hsotg);
  2900. /* Process host channels associated with non-periodic transfers */
  2901. if (tr_type == DWC2_TRANSACTION_NON_PERIODIC ||
  2902. tr_type == DWC2_TRANSACTION_ALL) {
  2903. if (!list_empty(&hsotg->non_periodic_sched_active)) {
  2904. dwc2_process_non_periodic_channels(hsotg);
  2905. } else {
  2906. /*
  2907. * Ensure NP Tx FIFO empty interrupt is disabled when
  2908. * there are no non-periodic transfers to process
  2909. */
  2910. u32 gintmsk = dwc2_readl(hsotg, GINTMSK);
  2911. gintmsk &= ~GINTSTS_NPTXFEMP;
  2912. dwc2_writel(hsotg, gintmsk, GINTMSK);
  2913. }
  2914. }
  2915. }
  2916. static void dwc2_conn_id_status_change(struct work_struct *work)
  2917. {
  2918. struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
  2919. wf_otg);
  2920. u32 count = 0;
  2921. u32 gotgctl;
  2922. unsigned long flags;
  2923. dev_dbg(hsotg->dev, "%s()\n", __func__);
  2924. gotgctl = dwc2_readl(hsotg, GOTGCTL);
  2925. dev_dbg(hsotg->dev, "gotgctl=%0x\n", gotgctl);
  2926. dev_dbg(hsotg->dev, "gotgctl.b.conidsts=%d\n",
  2927. !!(gotgctl & GOTGCTL_CONID_B));
  2928. /* B-Device connector (Device Mode) */
  2929. if (gotgctl & GOTGCTL_CONID_B) {
  2930. dwc2_vbus_supply_exit(hsotg);
  2931. /* Wait for switch to device mode */
  2932. dev_dbg(hsotg->dev, "connId B\n");
  2933. if (hsotg->bus_suspended) {
  2934. dev_info(hsotg->dev,
  2935. "Do port resume before switching to device mode\n");
  2936. dwc2_port_resume(hsotg);
  2937. }
  2938. while (!dwc2_is_device_mode(hsotg)) {
  2939. dev_info(hsotg->dev,
  2940. "Waiting for Peripheral Mode, Mode=%s\n",
  2941. dwc2_is_host_mode(hsotg) ? "Host" :
  2942. "Peripheral");
  2943. msleep(20);
  2944. /*
  2945. * Sometimes the initial GOTGCTRL read is wrong, so
  2946. * check it again and jump to host mode if that was
  2947. * the case.
  2948. */
  2949. gotgctl = dwc2_readl(hsotg, GOTGCTL);
  2950. if (!(gotgctl & GOTGCTL_CONID_B))
  2951. goto host;
  2952. if (++count > 250)
  2953. break;
  2954. }
  2955. if (count > 250)
  2956. dev_err(hsotg->dev,
  2957. "Connection id status change timed out\n");
  2958. hsotg->op_state = OTG_STATE_B_PERIPHERAL;
  2959. dwc2_core_init(hsotg, false);
  2960. dwc2_enable_global_interrupts(hsotg);
  2961. spin_lock_irqsave(&hsotg->lock, flags);
  2962. dwc2_hsotg_core_init_disconnected(hsotg, false);
  2963. spin_unlock_irqrestore(&hsotg->lock, flags);
  2964. /* Enable ACG feature in device mode,if supported */
  2965. dwc2_enable_acg(hsotg);
  2966. dwc2_hsotg_core_connect(hsotg);
  2967. } else {
  2968. host:
  2969. /* A-Device connector (Host Mode) */
  2970. dev_dbg(hsotg->dev, "connId A\n");
  2971. while (!dwc2_is_host_mode(hsotg)) {
  2972. dev_info(hsotg->dev, "Waiting for Host Mode, Mode=%s\n",
  2973. dwc2_is_host_mode(hsotg) ?
  2974. "Host" : "Peripheral");
  2975. msleep(20);
  2976. if (++count > 250)
  2977. break;
  2978. }
  2979. if (count > 250)
  2980. dev_err(hsotg->dev,
  2981. "Connection id status change timed out\n");
  2982. spin_lock_irqsave(&hsotg->lock, flags);
  2983. dwc2_hsotg_disconnect(hsotg);
  2984. spin_unlock_irqrestore(&hsotg->lock, flags);
  2985. hsotg->op_state = OTG_STATE_A_HOST;
  2986. /* Initialize the Core for Host mode */
  2987. dwc2_core_init(hsotg, false);
  2988. dwc2_enable_global_interrupts(hsotg);
  2989. dwc2_hcd_start(hsotg);
  2990. }
  2991. }
  2992. static void dwc2_wakeup_detected(struct timer_list *t)
  2993. {
  2994. struct dwc2_hsotg *hsotg = from_timer(hsotg, t, wkp_timer);
  2995. u32 hprt0;
  2996. dev_dbg(hsotg->dev, "%s()\n", __func__);
  2997. /*
  2998. * Clear the Resume after 70ms. (Need 20 ms minimum. Use 70 ms
  2999. * so that OPT tests pass with all PHYs.)
  3000. */
  3001. hprt0 = dwc2_read_hprt0(hsotg);
  3002. dev_dbg(hsotg->dev, "Resume: HPRT0=%0x\n", hprt0);
  3003. hprt0 &= ~HPRT0_RES;
  3004. dwc2_writel(hsotg, hprt0, HPRT0);
  3005. dev_dbg(hsotg->dev, "Clear Resume: HPRT0=%0x\n",
  3006. dwc2_readl(hsotg, HPRT0));
  3007. dwc2_hcd_rem_wakeup(hsotg);
  3008. hsotg->bus_suspended = false;
  3009. /* Change to L0 state */
  3010. hsotg->lx_state = DWC2_L0;
  3011. }
  3012. static int dwc2_host_is_b_hnp_enabled(struct dwc2_hsotg *hsotg)
  3013. {
  3014. struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
  3015. return hcd->self.b_hnp_enable;
  3016. }
  3017. /* Must NOT be called with interrupt disabled or spinlock held */
  3018. static void dwc2_port_suspend(struct dwc2_hsotg *hsotg, u16 windex)
  3019. {
  3020. unsigned long flags;
  3021. u32 hprt0;
  3022. u32 pcgctl;
  3023. u32 gotgctl;
  3024. dev_dbg(hsotg->dev, "%s()\n", __func__);
  3025. spin_lock_irqsave(&hsotg->lock, flags);
  3026. if (windex == hsotg->otg_port && dwc2_host_is_b_hnp_enabled(hsotg)) {
  3027. gotgctl = dwc2_readl(hsotg, GOTGCTL);
  3028. gotgctl |= GOTGCTL_HSTSETHNPEN;
  3029. dwc2_writel(hsotg, gotgctl, GOTGCTL);
  3030. hsotg->op_state = OTG_STATE_A_SUSPEND;
  3031. }
  3032. hprt0 = dwc2_read_hprt0(hsotg);
  3033. hprt0 |= HPRT0_SUSP;
  3034. dwc2_writel(hsotg, hprt0, HPRT0);
  3035. hsotg->bus_suspended = true;
  3036. /*
  3037. * If power_down is supported, Phy clock will be suspended
  3038. * after registers are backuped.
  3039. */
  3040. if (!hsotg->params.power_down) {
  3041. /* Suspend the Phy Clock */
  3042. pcgctl = dwc2_readl(hsotg, PCGCTL);
  3043. pcgctl |= PCGCTL_STOPPCLK;
  3044. dwc2_writel(hsotg, pcgctl, PCGCTL);
  3045. udelay(10);
  3046. }
  3047. /* For HNP the bus must be suspended for at least 200ms */
  3048. if (dwc2_host_is_b_hnp_enabled(hsotg)) {
  3049. pcgctl = dwc2_readl(hsotg, PCGCTL);
  3050. pcgctl &= ~PCGCTL_STOPPCLK;
  3051. dwc2_writel(hsotg, pcgctl, PCGCTL);
  3052. spin_unlock_irqrestore(&hsotg->lock, flags);
  3053. msleep(200);
  3054. } else {
  3055. spin_unlock_irqrestore(&hsotg->lock, flags);
  3056. }
  3057. }
  3058. /* Must NOT be called with interrupt disabled or spinlock held */
  3059. static void dwc2_port_resume(struct dwc2_hsotg *hsotg)
  3060. {
  3061. unsigned long flags;
  3062. u32 hprt0;
  3063. u32 pcgctl;
  3064. spin_lock_irqsave(&hsotg->lock, flags);
  3065. /*
  3066. * If power_down is supported, Phy clock is already resumed
  3067. * after registers restore.
  3068. */
  3069. if (!hsotg->params.power_down) {
  3070. pcgctl = dwc2_readl(hsotg, PCGCTL);
  3071. pcgctl &= ~PCGCTL_STOPPCLK;
  3072. dwc2_writel(hsotg, pcgctl, PCGCTL);
  3073. spin_unlock_irqrestore(&hsotg->lock, flags);
  3074. msleep(20);
  3075. spin_lock_irqsave(&hsotg->lock, flags);
  3076. }
  3077. hprt0 = dwc2_read_hprt0(hsotg);
  3078. hprt0 |= HPRT0_RES;
  3079. hprt0 &= ~HPRT0_SUSP;
  3080. dwc2_writel(hsotg, hprt0, HPRT0);
  3081. spin_unlock_irqrestore(&hsotg->lock, flags);
  3082. msleep(USB_RESUME_TIMEOUT);
  3083. spin_lock_irqsave(&hsotg->lock, flags);
  3084. hprt0 = dwc2_read_hprt0(hsotg);
  3085. hprt0 &= ~(HPRT0_RES | HPRT0_SUSP);
  3086. dwc2_writel(hsotg, hprt0, HPRT0);
  3087. hsotg->bus_suspended = false;
  3088. spin_unlock_irqrestore(&hsotg->lock, flags);
  3089. }
  3090. /* Handles hub class-specific requests */
  3091. static int dwc2_hcd_hub_control(struct dwc2_hsotg *hsotg, u16 typereq,
  3092. u16 wvalue, u16 windex, char *buf, u16 wlength)
  3093. {
  3094. struct usb_hub_descriptor *hub_desc;
  3095. int retval = 0;
  3096. u32 hprt0;
  3097. u32 port_status;
  3098. u32 speed;
  3099. u32 pcgctl;
  3100. u32 pwr;
  3101. switch (typereq) {
  3102. case ClearHubFeature:
  3103. dev_dbg(hsotg->dev, "ClearHubFeature %1xh\n", wvalue);
  3104. switch (wvalue) {
  3105. case C_HUB_LOCAL_POWER:
  3106. case C_HUB_OVER_CURRENT:
  3107. /* Nothing required here */
  3108. break;
  3109. default:
  3110. retval = -EINVAL;
  3111. dev_err(hsotg->dev,
  3112. "ClearHubFeature request %1xh unknown\n",
  3113. wvalue);
  3114. }
  3115. break;
  3116. case ClearPortFeature:
  3117. if (wvalue != USB_PORT_FEAT_L1)
  3118. if (!windex || windex > 1)
  3119. goto error;
  3120. switch (wvalue) {
  3121. case USB_PORT_FEAT_ENABLE:
  3122. dev_dbg(hsotg->dev,
  3123. "ClearPortFeature USB_PORT_FEAT_ENABLE\n");
  3124. hprt0 = dwc2_read_hprt0(hsotg);
  3125. hprt0 |= HPRT0_ENA;
  3126. dwc2_writel(hsotg, hprt0, HPRT0);
  3127. break;
  3128. case USB_PORT_FEAT_SUSPEND:
  3129. dev_dbg(hsotg->dev,
  3130. "ClearPortFeature USB_PORT_FEAT_SUSPEND\n");
  3131. if (hsotg->bus_suspended) {
  3132. if (hsotg->hibernated)
  3133. dwc2_exit_hibernation(hsotg, 0, 0, 1);
  3134. else
  3135. dwc2_port_resume(hsotg);
  3136. }
  3137. break;
  3138. case USB_PORT_FEAT_POWER:
  3139. dev_dbg(hsotg->dev,
  3140. "ClearPortFeature USB_PORT_FEAT_POWER\n");
  3141. hprt0 = dwc2_read_hprt0(hsotg);
  3142. pwr = hprt0 & HPRT0_PWR;
  3143. hprt0 &= ~HPRT0_PWR;
  3144. dwc2_writel(hsotg, hprt0, HPRT0);
  3145. if (pwr)
  3146. dwc2_vbus_supply_exit(hsotg);
  3147. break;
  3148. case USB_PORT_FEAT_INDICATOR:
  3149. dev_dbg(hsotg->dev,
  3150. "ClearPortFeature USB_PORT_FEAT_INDICATOR\n");
  3151. /* Port indicator not supported */
  3152. break;
  3153. case USB_PORT_FEAT_C_CONNECTION:
  3154. /*
  3155. * Clears driver's internal Connect Status Change flag
  3156. */
  3157. dev_dbg(hsotg->dev,
  3158. "ClearPortFeature USB_PORT_FEAT_C_CONNECTION\n");
  3159. hsotg->flags.b.port_connect_status_change = 0;
  3160. break;
  3161. case USB_PORT_FEAT_C_RESET:
  3162. /* Clears driver's internal Port Reset Change flag */
  3163. dev_dbg(hsotg->dev,
  3164. "ClearPortFeature USB_PORT_FEAT_C_RESET\n");
  3165. hsotg->flags.b.port_reset_change = 0;
  3166. break;
  3167. case USB_PORT_FEAT_C_ENABLE:
  3168. /*
  3169. * Clears the driver's internal Port Enable/Disable
  3170. * Change flag
  3171. */
  3172. dev_dbg(hsotg->dev,
  3173. "ClearPortFeature USB_PORT_FEAT_C_ENABLE\n");
  3174. hsotg->flags.b.port_enable_change = 0;
  3175. break;
  3176. case USB_PORT_FEAT_C_SUSPEND:
  3177. /*
  3178. * Clears the driver's internal Port Suspend Change
  3179. * flag, which is set when resume signaling on the host
  3180. * port is complete
  3181. */
  3182. dev_dbg(hsotg->dev,
  3183. "ClearPortFeature USB_PORT_FEAT_C_SUSPEND\n");
  3184. hsotg->flags.b.port_suspend_change = 0;
  3185. break;
  3186. case USB_PORT_FEAT_C_PORT_L1:
  3187. dev_dbg(hsotg->dev,
  3188. "ClearPortFeature USB_PORT_FEAT_C_PORT_L1\n");
  3189. hsotg->flags.b.port_l1_change = 0;
  3190. break;
  3191. case USB_PORT_FEAT_C_OVER_CURRENT:
  3192. dev_dbg(hsotg->dev,
  3193. "ClearPortFeature USB_PORT_FEAT_C_OVER_CURRENT\n");
  3194. hsotg->flags.b.port_over_current_change = 0;
  3195. break;
  3196. default:
  3197. retval = -EINVAL;
  3198. dev_err(hsotg->dev,
  3199. "ClearPortFeature request %1xh unknown or unsupported\n",
  3200. wvalue);
  3201. }
  3202. break;
  3203. case GetHubDescriptor:
  3204. dev_dbg(hsotg->dev, "GetHubDescriptor\n");
  3205. hub_desc = (struct usb_hub_descriptor *)buf;
  3206. hub_desc->bDescLength = 9;
  3207. hub_desc->bDescriptorType = USB_DT_HUB;
  3208. hub_desc->bNbrPorts = 1;
  3209. hub_desc->wHubCharacteristics =
  3210. cpu_to_le16(HUB_CHAR_COMMON_LPSM |
  3211. HUB_CHAR_INDV_PORT_OCPM);
  3212. hub_desc->bPwrOn2PwrGood = 1;
  3213. hub_desc->bHubContrCurrent = 0;
  3214. hub_desc->u.hs.DeviceRemovable[0] = 0;
  3215. hub_desc->u.hs.DeviceRemovable[1] = 0xff;
  3216. break;
  3217. case GetHubStatus:
  3218. dev_dbg(hsotg->dev, "GetHubStatus\n");
  3219. memset(buf, 0, 4);
  3220. break;
  3221. case GetPortStatus:
  3222. dev_vdbg(hsotg->dev,
  3223. "GetPortStatus wIndex=0x%04x flags=0x%08x\n", windex,
  3224. hsotg->flags.d32);
  3225. if (!windex || windex > 1)
  3226. goto error;
  3227. port_status = 0;
  3228. if (hsotg->flags.b.port_connect_status_change)
  3229. port_status |= USB_PORT_STAT_C_CONNECTION << 16;
  3230. if (hsotg->flags.b.port_enable_change)
  3231. port_status |= USB_PORT_STAT_C_ENABLE << 16;
  3232. if (hsotg->flags.b.port_suspend_change)
  3233. port_status |= USB_PORT_STAT_C_SUSPEND << 16;
  3234. if (hsotg->flags.b.port_l1_change)
  3235. port_status |= USB_PORT_STAT_C_L1 << 16;
  3236. if (hsotg->flags.b.port_reset_change)
  3237. port_status |= USB_PORT_STAT_C_RESET << 16;
  3238. if (hsotg->flags.b.port_over_current_change) {
  3239. dev_warn(hsotg->dev, "Overcurrent change detected\n");
  3240. port_status |= USB_PORT_STAT_C_OVERCURRENT << 16;
  3241. }
  3242. if (!hsotg->flags.b.port_connect_status) {
  3243. /*
  3244. * The port is disconnected, which means the core is
  3245. * either in device mode or it soon will be. Just
  3246. * return 0's for the remainder of the port status
  3247. * since the port register can't be read if the core
  3248. * is in device mode.
  3249. */
  3250. *(__le32 *)buf = cpu_to_le32(port_status);
  3251. break;
  3252. }
  3253. hprt0 = dwc2_readl(hsotg, HPRT0);
  3254. dev_vdbg(hsotg->dev, " HPRT0: 0x%08x\n", hprt0);
  3255. if (hprt0 & HPRT0_CONNSTS)
  3256. port_status |= USB_PORT_STAT_CONNECTION;
  3257. if (hprt0 & HPRT0_ENA)
  3258. port_status |= USB_PORT_STAT_ENABLE;
  3259. if (hprt0 & HPRT0_SUSP)
  3260. port_status |= USB_PORT_STAT_SUSPEND;
  3261. if (hprt0 & HPRT0_OVRCURRACT)
  3262. port_status |= USB_PORT_STAT_OVERCURRENT;
  3263. if (hprt0 & HPRT0_RST)
  3264. port_status |= USB_PORT_STAT_RESET;
  3265. if (hprt0 & HPRT0_PWR)
  3266. port_status |= USB_PORT_STAT_POWER;
  3267. speed = (hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT;
  3268. if (speed == HPRT0_SPD_HIGH_SPEED)
  3269. port_status |= USB_PORT_STAT_HIGH_SPEED;
  3270. else if (speed == HPRT0_SPD_LOW_SPEED)
  3271. port_status |= USB_PORT_STAT_LOW_SPEED;
  3272. if (hprt0 & HPRT0_TSTCTL_MASK)
  3273. port_status |= USB_PORT_STAT_TEST;
  3274. /* USB_PORT_FEAT_INDICATOR unsupported always 0 */
  3275. if (hsotg->params.dma_desc_fs_enable) {
  3276. /*
  3277. * Enable descriptor DMA only if a full speed
  3278. * device is connected.
  3279. */
  3280. if (hsotg->new_connection &&
  3281. ((port_status &
  3282. (USB_PORT_STAT_CONNECTION |
  3283. USB_PORT_STAT_HIGH_SPEED |
  3284. USB_PORT_STAT_LOW_SPEED)) ==
  3285. USB_PORT_STAT_CONNECTION)) {
  3286. u32 hcfg;
  3287. dev_info(hsotg->dev, "Enabling descriptor DMA mode\n");
  3288. hsotg->params.dma_desc_enable = true;
  3289. hcfg = dwc2_readl(hsotg, HCFG);
  3290. hcfg |= HCFG_DESCDMA;
  3291. dwc2_writel(hsotg, hcfg, HCFG);
  3292. hsotg->new_connection = false;
  3293. }
  3294. }
  3295. dev_vdbg(hsotg->dev, "port_status=%08x\n", port_status);
  3296. *(__le32 *)buf = cpu_to_le32(port_status);
  3297. break;
  3298. case SetHubFeature:
  3299. dev_dbg(hsotg->dev, "SetHubFeature\n");
  3300. /* No HUB features supported */
  3301. break;
  3302. case SetPortFeature:
  3303. dev_dbg(hsotg->dev, "SetPortFeature\n");
  3304. if (wvalue != USB_PORT_FEAT_TEST && (!windex || windex > 1))
  3305. goto error;
  3306. if (!hsotg->flags.b.port_connect_status) {
  3307. /*
  3308. * The port is disconnected, which means the core is
  3309. * either in device mode or it soon will be. Just
  3310. * return without doing anything since the port
  3311. * register can't be written if the core is in device
  3312. * mode.
  3313. */
  3314. break;
  3315. }
  3316. switch (wvalue) {
  3317. case USB_PORT_FEAT_SUSPEND:
  3318. dev_dbg(hsotg->dev,
  3319. "SetPortFeature - USB_PORT_FEAT_SUSPEND\n");
  3320. if (windex != hsotg->otg_port)
  3321. goto error;
  3322. if (hsotg->params.power_down == 2)
  3323. dwc2_enter_hibernation(hsotg, 1);
  3324. else
  3325. dwc2_port_suspend(hsotg, windex);
  3326. break;
  3327. case USB_PORT_FEAT_POWER:
  3328. dev_dbg(hsotg->dev,
  3329. "SetPortFeature - USB_PORT_FEAT_POWER\n");
  3330. hprt0 = dwc2_read_hprt0(hsotg);
  3331. pwr = hprt0 & HPRT0_PWR;
  3332. hprt0 |= HPRT0_PWR;
  3333. dwc2_writel(hsotg, hprt0, HPRT0);
  3334. if (!pwr)
  3335. dwc2_vbus_supply_init(hsotg);
  3336. break;
  3337. case USB_PORT_FEAT_RESET:
  3338. if (hsotg->params.power_down == 2 &&
  3339. hsotg->hibernated)
  3340. dwc2_exit_hibernation(hsotg, 0, 1, 1);
  3341. hprt0 = dwc2_read_hprt0(hsotg);
  3342. dev_dbg(hsotg->dev,
  3343. "SetPortFeature - USB_PORT_FEAT_RESET\n");
  3344. pcgctl = dwc2_readl(hsotg, PCGCTL);
  3345. pcgctl &= ~(PCGCTL_ENBL_SLEEP_GATING | PCGCTL_STOPPCLK);
  3346. dwc2_writel(hsotg, pcgctl, PCGCTL);
  3347. /* ??? Original driver does this */
  3348. dwc2_writel(hsotg, 0, PCGCTL);
  3349. hprt0 = dwc2_read_hprt0(hsotg);
  3350. pwr = hprt0 & HPRT0_PWR;
  3351. /* Clear suspend bit if resetting from suspend state */
  3352. hprt0 &= ~HPRT0_SUSP;
  3353. /*
  3354. * When B-Host the Port reset bit is set in the Start
  3355. * HCD Callback function, so that the reset is started
  3356. * within 1ms of the HNP success interrupt
  3357. */
  3358. if (!dwc2_hcd_is_b_host(hsotg)) {
  3359. hprt0 |= HPRT0_PWR | HPRT0_RST;
  3360. dev_dbg(hsotg->dev,
  3361. "In host mode, hprt0=%08x\n", hprt0);
  3362. dwc2_writel(hsotg, hprt0, HPRT0);
  3363. if (!pwr)
  3364. dwc2_vbus_supply_init(hsotg);
  3365. }
  3366. /* Clear reset bit in 10ms (FS/LS) or 50ms (HS) */
  3367. msleep(50);
  3368. hprt0 &= ~HPRT0_RST;
  3369. dwc2_writel(hsotg, hprt0, HPRT0);
  3370. hsotg->lx_state = DWC2_L0; /* Now back to On state */
  3371. break;
  3372. case USB_PORT_FEAT_INDICATOR:
  3373. dev_dbg(hsotg->dev,
  3374. "SetPortFeature - USB_PORT_FEAT_INDICATOR\n");
  3375. /* Not supported */
  3376. break;
  3377. case USB_PORT_FEAT_TEST:
  3378. hprt0 = dwc2_read_hprt0(hsotg);
  3379. dev_dbg(hsotg->dev,
  3380. "SetPortFeature - USB_PORT_FEAT_TEST\n");
  3381. hprt0 &= ~HPRT0_TSTCTL_MASK;
  3382. hprt0 |= (windex >> 8) << HPRT0_TSTCTL_SHIFT;
  3383. dwc2_writel(hsotg, hprt0, HPRT0);
  3384. break;
  3385. default:
  3386. retval = -EINVAL;
  3387. dev_err(hsotg->dev,
  3388. "SetPortFeature %1xh unknown or unsupported\n",
  3389. wvalue);
  3390. break;
  3391. }
  3392. break;
  3393. default:
  3394. error:
  3395. retval = -EINVAL;
  3396. dev_dbg(hsotg->dev,
  3397. "Unknown hub control request: %1xh wIndex: %1xh wValue: %1xh\n",
  3398. typereq, windex, wvalue);
  3399. break;
  3400. }
  3401. return retval;
  3402. }
  3403. static int dwc2_hcd_is_status_changed(struct dwc2_hsotg *hsotg, int port)
  3404. {
  3405. int retval;
  3406. if (port != 1)
  3407. return -EINVAL;
  3408. retval = (hsotg->flags.b.port_connect_status_change ||
  3409. hsotg->flags.b.port_reset_change ||
  3410. hsotg->flags.b.port_enable_change ||
  3411. hsotg->flags.b.port_suspend_change ||
  3412. hsotg->flags.b.port_over_current_change);
  3413. if (retval) {
  3414. dev_dbg(hsotg->dev,
  3415. "DWC OTG HCD HUB STATUS DATA: Root port status changed\n");
  3416. dev_dbg(hsotg->dev, " port_connect_status_change: %d\n",
  3417. hsotg->flags.b.port_connect_status_change);
  3418. dev_dbg(hsotg->dev, " port_reset_change: %d\n",
  3419. hsotg->flags.b.port_reset_change);
  3420. dev_dbg(hsotg->dev, " port_enable_change: %d\n",
  3421. hsotg->flags.b.port_enable_change);
  3422. dev_dbg(hsotg->dev, " port_suspend_change: %d\n",
  3423. hsotg->flags.b.port_suspend_change);
  3424. dev_dbg(hsotg->dev, " port_over_current_change: %d\n",
  3425. hsotg->flags.b.port_over_current_change);
  3426. }
  3427. return retval;
  3428. }
  3429. int dwc2_hcd_get_frame_number(struct dwc2_hsotg *hsotg)
  3430. {
  3431. u32 hfnum = dwc2_readl(hsotg, HFNUM);
  3432. #ifdef DWC2_DEBUG_SOF
  3433. dev_vdbg(hsotg->dev, "DWC OTG HCD GET FRAME NUMBER %d\n",
  3434. (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT);
  3435. #endif
  3436. return (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT;
  3437. }
  3438. int dwc2_hcd_get_future_frame_number(struct dwc2_hsotg *hsotg, int us)
  3439. {
  3440. u32 hprt = dwc2_readl(hsotg, HPRT0);
  3441. u32 hfir = dwc2_readl(hsotg, HFIR);
  3442. u32 hfnum = dwc2_readl(hsotg, HFNUM);
  3443. unsigned int us_per_frame;
  3444. unsigned int frame_number;
  3445. unsigned int remaining;
  3446. unsigned int interval;
  3447. unsigned int phy_clks;
  3448. /* High speed has 125 us per (micro) frame; others are 1 ms per */
  3449. us_per_frame = (hprt & HPRT0_SPD_MASK) ? 1000 : 125;
  3450. /* Extract fields */
  3451. frame_number = (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT;
  3452. remaining = (hfnum & HFNUM_FRREM_MASK) >> HFNUM_FRREM_SHIFT;
  3453. interval = (hfir & HFIR_FRINT_MASK) >> HFIR_FRINT_SHIFT;
  3454. /*
  3455. * Number of phy clocks since the last tick of the frame number after
  3456. * "us" has passed.
  3457. */
  3458. phy_clks = (interval - remaining) +
  3459. DIV_ROUND_UP(interval * us, us_per_frame);
  3460. return dwc2_frame_num_inc(frame_number, phy_clks / interval);
  3461. }
  3462. int dwc2_hcd_is_b_host(struct dwc2_hsotg *hsotg)
  3463. {
  3464. return hsotg->op_state == OTG_STATE_B_HOST;
  3465. }
  3466. static struct dwc2_hcd_urb *dwc2_hcd_urb_alloc(struct dwc2_hsotg *hsotg,
  3467. int iso_desc_count,
  3468. gfp_t mem_flags)
  3469. {
  3470. struct dwc2_hcd_urb *urb;
  3471. u32 size = sizeof(*urb) + iso_desc_count *
  3472. sizeof(struct dwc2_hcd_iso_packet_desc);
  3473. urb = kzalloc(size, mem_flags);
  3474. if (urb)
  3475. urb->packet_count = iso_desc_count;
  3476. return urb;
  3477. }
  3478. static void dwc2_hcd_urb_set_pipeinfo(struct dwc2_hsotg *hsotg,
  3479. struct dwc2_hcd_urb *urb, u8 dev_addr,
  3480. u8 ep_num, u8 ep_type, u8 ep_dir,
  3481. u16 maxp, u16 maxp_mult)
  3482. {
  3483. if (dbg_perio() ||
  3484. ep_type == USB_ENDPOINT_XFER_BULK ||
  3485. ep_type == USB_ENDPOINT_XFER_CONTROL)
  3486. dev_vdbg(hsotg->dev,
  3487. "addr=%d, ep_num=%d, ep_dir=%1x, ep_type=%1x, maxp=%d (%d mult)\n",
  3488. dev_addr, ep_num, ep_dir, ep_type, maxp, maxp_mult);
  3489. urb->pipe_info.dev_addr = dev_addr;
  3490. urb->pipe_info.ep_num = ep_num;
  3491. urb->pipe_info.pipe_type = ep_type;
  3492. urb->pipe_info.pipe_dir = ep_dir;
  3493. urb->pipe_info.maxp = maxp;
  3494. urb->pipe_info.maxp_mult = maxp_mult;
  3495. }
  3496. /*
  3497. * NOTE: This function will be removed once the peripheral controller code
  3498. * is integrated and the driver is stable
  3499. */
  3500. void dwc2_hcd_dump_state(struct dwc2_hsotg *hsotg)
  3501. {
  3502. #ifdef DEBUG
  3503. struct dwc2_host_chan *chan;
  3504. struct dwc2_hcd_urb *urb;
  3505. struct dwc2_qtd *qtd;
  3506. int num_channels;
  3507. u32 np_tx_status;
  3508. u32 p_tx_status;
  3509. int i;
  3510. num_channels = hsotg->params.host_channels;
  3511. dev_dbg(hsotg->dev, "\n");
  3512. dev_dbg(hsotg->dev,
  3513. "************************************************************\n");
  3514. dev_dbg(hsotg->dev, "HCD State:\n");
  3515. dev_dbg(hsotg->dev, " Num channels: %d\n", num_channels);
  3516. for (i = 0; i < num_channels; i++) {
  3517. chan = hsotg->hc_ptr_array[i];
  3518. dev_dbg(hsotg->dev, " Channel %d:\n", i);
  3519. dev_dbg(hsotg->dev,
  3520. " dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
  3521. chan->dev_addr, chan->ep_num, chan->ep_is_in);
  3522. dev_dbg(hsotg->dev, " speed: %d\n", chan->speed);
  3523. dev_dbg(hsotg->dev, " ep_type: %d\n", chan->ep_type);
  3524. dev_dbg(hsotg->dev, " max_packet: %d\n", chan->max_packet);
  3525. dev_dbg(hsotg->dev, " data_pid_start: %d\n",
  3526. chan->data_pid_start);
  3527. dev_dbg(hsotg->dev, " multi_count: %d\n", chan->multi_count);
  3528. dev_dbg(hsotg->dev, " xfer_started: %d\n",
  3529. chan->xfer_started);
  3530. dev_dbg(hsotg->dev, " xfer_buf: %p\n", chan->xfer_buf);
  3531. dev_dbg(hsotg->dev, " xfer_dma: %08lx\n",
  3532. (unsigned long)chan->xfer_dma);
  3533. dev_dbg(hsotg->dev, " xfer_len: %d\n", chan->xfer_len);
  3534. dev_dbg(hsotg->dev, " xfer_count: %d\n", chan->xfer_count);
  3535. dev_dbg(hsotg->dev, " halt_on_queue: %d\n",
  3536. chan->halt_on_queue);
  3537. dev_dbg(hsotg->dev, " halt_pending: %d\n",
  3538. chan->halt_pending);
  3539. dev_dbg(hsotg->dev, " halt_status: %d\n", chan->halt_status);
  3540. dev_dbg(hsotg->dev, " do_split: %d\n", chan->do_split);
  3541. dev_dbg(hsotg->dev, " complete_split: %d\n",
  3542. chan->complete_split);
  3543. dev_dbg(hsotg->dev, " hub_addr: %d\n", chan->hub_addr);
  3544. dev_dbg(hsotg->dev, " hub_port: %d\n", chan->hub_port);
  3545. dev_dbg(hsotg->dev, " xact_pos: %d\n", chan->xact_pos);
  3546. dev_dbg(hsotg->dev, " requests: %d\n", chan->requests);
  3547. dev_dbg(hsotg->dev, " qh: %p\n", chan->qh);
  3548. if (chan->xfer_started) {
  3549. u32 hfnum, hcchar, hctsiz, hcint, hcintmsk;
  3550. hfnum = dwc2_readl(hsotg, HFNUM);
  3551. hcchar = dwc2_readl(hsotg, HCCHAR(i));
  3552. hctsiz = dwc2_readl(hsotg, HCTSIZ(i));
  3553. hcint = dwc2_readl(hsotg, HCINT(i));
  3554. hcintmsk = dwc2_readl(hsotg, HCINTMSK(i));
  3555. dev_dbg(hsotg->dev, " hfnum: 0x%08x\n", hfnum);
  3556. dev_dbg(hsotg->dev, " hcchar: 0x%08x\n", hcchar);
  3557. dev_dbg(hsotg->dev, " hctsiz: 0x%08x\n", hctsiz);
  3558. dev_dbg(hsotg->dev, " hcint: 0x%08x\n", hcint);
  3559. dev_dbg(hsotg->dev, " hcintmsk: 0x%08x\n", hcintmsk);
  3560. }
  3561. if (!(chan->xfer_started && chan->qh))
  3562. continue;
  3563. list_for_each_entry(qtd, &chan->qh->qtd_list, qtd_list_entry) {
  3564. if (!qtd->in_process)
  3565. break;
  3566. urb = qtd->urb;
  3567. dev_dbg(hsotg->dev, " URB Info:\n");
  3568. dev_dbg(hsotg->dev, " qtd: %p, urb: %p\n",
  3569. qtd, urb);
  3570. if (urb) {
  3571. dev_dbg(hsotg->dev,
  3572. " Dev: %d, EP: %d %s\n",
  3573. dwc2_hcd_get_dev_addr(&urb->pipe_info),
  3574. dwc2_hcd_get_ep_num(&urb->pipe_info),
  3575. dwc2_hcd_is_pipe_in(&urb->pipe_info) ?
  3576. "IN" : "OUT");
  3577. dev_dbg(hsotg->dev,
  3578. " Max packet size: %d (%d mult)\n",
  3579. dwc2_hcd_get_maxp(&urb->pipe_info),
  3580. dwc2_hcd_get_maxp_mult(&urb->pipe_info));
  3581. dev_dbg(hsotg->dev,
  3582. " transfer_buffer: %p\n",
  3583. urb->buf);
  3584. dev_dbg(hsotg->dev,
  3585. " transfer_dma: %08lx\n",
  3586. (unsigned long)urb->dma);
  3587. dev_dbg(hsotg->dev,
  3588. " transfer_buffer_length: %d\n",
  3589. urb->length);
  3590. dev_dbg(hsotg->dev, " actual_length: %d\n",
  3591. urb->actual_length);
  3592. }
  3593. }
  3594. }
  3595. dev_dbg(hsotg->dev, " non_periodic_channels: %d\n",
  3596. hsotg->non_periodic_channels);
  3597. dev_dbg(hsotg->dev, " periodic_channels: %d\n",
  3598. hsotg->periodic_channels);
  3599. dev_dbg(hsotg->dev, " periodic_usecs: %d\n", hsotg->periodic_usecs);
  3600. np_tx_status = dwc2_readl(hsotg, GNPTXSTS);
  3601. dev_dbg(hsotg->dev, " NP Tx Req Queue Space Avail: %d\n",
  3602. (np_tx_status & TXSTS_QSPCAVAIL_MASK) >> TXSTS_QSPCAVAIL_SHIFT);
  3603. dev_dbg(hsotg->dev, " NP Tx FIFO Space Avail: %d\n",
  3604. (np_tx_status & TXSTS_FSPCAVAIL_MASK) >> TXSTS_FSPCAVAIL_SHIFT);
  3605. p_tx_status = dwc2_readl(hsotg, HPTXSTS);
  3606. dev_dbg(hsotg->dev, " P Tx Req Queue Space Avail: %d\n",
  3607. (p_tx_status & TXSTS_QSPCAVAIL_MASK) >> TXSTS_QSPCAVAIL_SHIFT);
  3608. dev_dbg(hsotg->dev, " P Tx FIFO Space Avail: %d\n",
  3609. (p_tx_status & TXSTS_FSPCAVAIL_MASK) >> TXSTS_FSPCAVAIL_SHIFT);
  3610. dwc2_dump_global_registers(hsotg);
  3611. dwc2_dump_host_registers(hsotg);
  3612. dev_dbg(hsotg->dev,
  3613. "************************************************************\n");
  3614. dev_dbg(hsotg->dev, "\n");
  3615. #endif
  3616. }
  3617. struct wrapper_priv_data {
  3618. struct dwc2_hsotg *hsotg;
  3619. };
  3620. /* Gets the dwc2_hsotg from a usb_hcd */
  3621. static struct dwc2_hsotg *dwc2_hcd_to_hsotg(struct usb_hcd *hcd)
  3622. {
  3623. struct wrapper_priv_data *p;
  3624. p = (struct wrapper_priv_data *)&hcd->hcd_priv;
  3625. return p->hsotg;
  3626. }
  3627. /**
  3628. * dwc2_host_get_tt_info() - Get the dwc2_tt associated with context
  3629. *
  3630. * This will get the dwc2_tt structure (and ttport) associated with the given
  3631. * context (which is really just a struct urb pointer).
  3632. *
  3633. * The first time this is called for a given TT we allocate memory for our
  3634. * structure. When everyone is done and has called dwc2_host_put_tt_info()
  3635. * then the refcount for the structure will go to 0 and we'll free it.
  3636. *
  3637. * @hsotg: The HCD state structure for the DWC OTG controller.
  3638. * @context: The priv pointer from a struct dwc2_hcd_urb.
  3639. * @mem_flags: Flags for allocating memory.
  3640. * @ttport: We'll return this device's port number here. That's used to
  3641. * reference into the bitmap if we're on a multi_tt hub.
  3642. *
  3643. * Return: a pointer to a struct dwc2_tt. Don't forget to call
  3644. * dwc2_host_put_tt_info()! Returns NULL upon memory alloc failure.
  3645. */
  3646. struct dwc2_tt *dwc2_host_get_tt_info(struct dwc2_hsotg *hsotg, void *context,
  3647. gfp_t mem_flags, int *ttport)
  3648. {
  3649. struct urb *urb = context;
  3650. struct dwc2_tt *dwc_tt = NULL;
  3651. if (urb->dev->tt) {
  3652. *ttport = urb->dev->ttport;
  3653. dwc_tt = urb->dev->tt->hcpriv;
  3654. if (!dwc_tt) {
  3655. size_t bitmap_size;
  3656. /*
  3657. * For single_tt we need one schedule. For multi_tt
  3658. * we need one per port.
  3659. */
  3660. bitmap_size = DWC2_ELEMENTS_PER_LS_BITMAP *
  3661. sizeof(dwc_tt->periodic_bitmaps[0]);
  3662. if (urb->dev->tt->multi)
  3663. bitmap_size *= urb->dev->tt->hub->maxchild;
  3664. dwc_tt = kzalloc(sizeof(*dwc_tt) + bitmap_size,
  3665. mem_flags);
  3666. if (!dwc_tt)
  3667. return NULL;
  3668. dwc_tt->usb_tt = urb->dev->tt;
  3669. dwc_tt->usb_tt->hcpriv = dwc_tt;
  3670. }
  3671. dwc_tt->refcount++;
  3672. }
  3673. return dwc_tt;
  3674. }
  3675. /**
  3676. * dwc2_host_put_tt_info() - Put the dwc2_tt from dwc2_host_get_tt_info()
  3677. *
  3678. * Frees resources allocated by dwc2_host_get_tt_info() if all current holders
  3679. * of the structure are done.
  3680. *
  3681. * It's OK to call this with NULL.
  3682. *
  3683. * @hsotg: The HCD state structure for the DWC OTG controller.
  3684. * @dwc_tt: The pointer returned by dwc2_host_get_tt_info.
  3685. */
  3686. void dwc2_host_put_tt_info(struct dwc2_hsotg *hsotg, struct dwc2_tt *dwc_tt)
  3687. {
  3688. /* Model kfree and make put of NULL a no-op */
  3689. if (!dwc_tt)
  3690. return;
  3691. WARN_ON(dwc_tt->refcount < 1);
  3692. dwc_tt->refcount--;
  3693. if (!dwc_tt->refcount) {
  3694. dwc_tt->usb_tt->hcpriv = NULL;
  3695. kfree(dwc_tt);
  3696. }
  3697. }
  3698. int dwc2_host_get_speed(struct dwc2_hsotg *hsotg, void *context)
  3699. {
  3700. struct urb *urb = context;
  3701. return urb->dev->speed;
  3702. }
  3703. static void dwc2_allocate_bus_bandwidth(struct usb_hcd *hcd, u16 bw,
  3704. struct urb *urb)
  3705. {
  3706. struct usb_bus *bus = hcd_to_bus(hcd);
  3707. if (urb->interval)
  3708. bus->bandwidth_allocated += bw / urb->interval;
  3709. if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
  3710. bus->bandwidth_isoc_reqs++;
  3711. else
  3712. bus->bandwidth_int_reqs++;
  3713. }
  3714. static void dwc2_free_bus_bandwidth(struct usb_hcd *hcd, u16 bw,
  3715. struct urb *urb)
  3716. {
  3717. struct usb_bus *bus = hcd_to_bus(hcd);
  3718. if (urb->interval)
  3719. bus->bandwidth_allocated -= bw / urb->interval;
  3720. if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
  3721. bus->bandwidth_isoc_reqs--;
  3722. else
  3723. bus->bandwidth_int_reqs--;
  3724. }
  3725. /*
  3726. * Sets the final status of an URB and returns it to the upper layer. Any
  3727. * required cleanup of the URB is performed.
  3728. *
  3729. * Must be called with interrupt disabled and spinlock held
  3730. */
  3731. void dwc2_host_complete(struct dwc2_hsotg *hsotg, struct dwc2_qtd *qtd,
  3732. int status)
  3733. {
  3734. struct urb *urb;
  3735. int i;
  3736. if (!qtd) {
  3737. dev_dbg(hsotg->dev, "## %s: qtd is NULL ##\n", __func__);
  3738. return;
  3739. }
  3740. if (!qtd->urb) {
  3741. dev_dbg(hsotg->dev, "## %s: qtd->urb is NULL ##\n", __func__);
  3742. return;
  3743. }
  3744. urb = qtd->urb->priv;
  3745. if (!urb) {
  3746. dev_dbg(hsotg->dev, "## %s: urb->priv is NULL ##\n", __func__);
  3747. return;
  3748. }
  3749. urb->actual_length = dwc2_hcd_urb_get_actual_length(qtd->urb);
  3750. if (dbg_urb(urb))
  3751. dev_vdbg(hsotg->dev,
  3752. "%s: urb %p device %d ep %d-%s status %d actual %d\n",
  3753. __func__, urb, usb_pipedevice(urb->pipe),
  3754. usb_pipeendpoint(urb->pipe),
  3755. usb_pipein(urb->pipe) ? "IN" : "OUT", status,
  3756. urb->actual_length);
  3757. if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  3758. urb->error_count = dwc2_hcd_urb_get_error_count(qtd->urb);
  3759. for (i = 0; i < urb->number_of_packets; ++i) {
  3760. urb->iso_frame_desc[i].actual_length =
  3761. dwc2_hcd_urb_get_iso_desc_actual_length(
  3762. qtd->urb, i);
  3763. urb->iso_frame_desc[i].status =
  3764. dwc2_hcd_urb_get_iso_desc_status(qtd->urb, i);
  3765. }
  3766. }
  3767. if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS && dbg_perio()) {
  3768. for (i = 0; i < urb->number_of_packets; i++)
  3769. dev_vdbg(hsotg->dev, " ISO Desc %d status %d\n",
  3770. i, urb->iso_frame_desc[i].status);
  3771. }
  3772. urb->status = status;
  3773. if (!status) {
  3774. if ((urb->transfer_flags & URB_SHORT_NOT_OK) &&
  3775. urb->actual_length < urb->transfer_buffer_length)
  3776. urb->status = -EREMOTEIO;
  3777. }
  3778. if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS ||
  3779. usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
  3780. struct usb_host_endpoint *ep = urb->ep;
  3781. if (ep)
  3782. dwc2_free_bus_bandwidth(dwc2_hsotg_to_hcd(hsotg),
  3783. dwc2_hcd_get_ep_bandwidth(hsotg, ep),
  3784. urb);
  3785. }
  3786. usb_hcd_unlink_urb_from_ep(dwc2_hsotg_to_hcd(hsotg), urb);
  3787. urb->hcpriv = NULL;
  3788. kfree(qtd->urb);
  3789. qtd->urb = NULL;
  3790. usb_hcd_giveback_urb(dwc2_hsotg_to_hcd(hsotg), urb, status);
  3791. }
  3792. /*
  3793. * Work queue function for starting the HCD when A-Cable is connected
  3794. */
  3795. static void dwc2_hcd_start_func(struct work_struct *work)
  3796. {
  3797. struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
  3798. start_work.work);
  3799. dev_dbg(hsotg->dev, "%s() %p\n", __func__, hsotg);
  3800. dwc2_host_start(hsotg);
  3801. }
  3802. /*
  3803. * Reset work queue function
  3804. */
  3805. static void dwc2_hcd_reset_func(struct work_struct *work)
  3806. {
  3807. struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
  3808. reset_work.work);
  3809. unsigned long flags;
  3810. u32 hprt0;
  3811. dev_dbg(hsotg->dev, "USB RESET function called\n");
  3812. spin_lock_irqsave(&hsotg->lock, flags);
  3813. hprt0 = dwc2_read_hprt0(hsotg);
  3814. hprt0 &= ~HPRT0_RST;
  3815. dwc2_writel(hsotg, hprt0, HPRT0);
  3816. hsotg->flags.b.port_reset_change = 1;
  3817. spin_unlock_irqrestore(&hsotg->lock, flags);
  3818. }
  3819. /*
  3820. * =========================================================================
  3821. * Linux HC Driver Functions
  3822. * =========================================================================
  3823. */
  3824. /*
  3825. * Initializes the DWC_otg controller and its root hub and prepares it for host
  3826. * mode operation. Activates the root port. Returns 0 on success and a negative
  3827. * error code on failure.
  3828. */
  3829. static int _dwc2_hcd_start(struct usb_hcd *hcd)
  3830. {
  3831. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  3832. struct usb_bus *bus = hcd_to_bus(hcd);
  3833. unsigned long flags;
  3834. u32 hprt0;
  3835. int ret;
  3836. dev_dbg(hsotg->dev, "DWC OTG HCD START\n");
  3837. spin_lock_irqsave(&hsotg->lock, flags);
  3838. hsotg->lx_state = DWC2_L0;
  3839. hcd->state = HC_STATE_RUNNING;
  3840. set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  3841. if (dwc2_is_device_mode(hsotg)) {
  3842. spin_unlock_irqrestore(&hsotg->lock, flags);
  3843. return 0; /* why 0 ?? */
  3844. }
  3845. dwc2_hcd_reinit(hsotg);
  3846. hprt0 = dwc2_read_hprt0(hsotg);
  3847. /* Has vbus power been turned on in dwc2_core_host_init ? */
  3848. if (hprt0 & HPRT0_PWR) {
  3849. /* Enable external vbus supply before resuming root hub */
  3850. spin_unlock_irqrestore(&hsotg->lock, flags);
  3851. ret = dwc2_vbus_supply_init(hsotg);
  3852. if (ret)
  3853. return ret;
  3854. spin_lock_irqsave(&hsotg->lock, flags);
  3855. }
  3856. /* Initialize and connect root hub if one is not already attached */
  3857. if (bus->root_hub) {
  3858. dev_dbg(hsotg->dev, "DWC OTG HCD Has Root Hub\n");
  3859. /* Inform the HUB driver to resume */
  3860. usb_hcd_resume_root_hub(hcd);
  3861. }
  3862. spin_unlock_irqrestore(&hsotg->lock, flags);
  3863. return 0;
  3864. }
  3865. /*
  3866. * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
  3867. * stopped.
  3868. */
  3869. static void _dwc2_hcd_stop(struct usb_hcd *hcd)
  3870. {
  3871. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  3872. unsigned long flags;
  3873. u32 hprt0;
  3874. /* Turn off all host-specific interrupts */
  3875. dwc2_disable_host_interrupts(hsotg);
  3876. /* Wait for interrupt processing to finish */
  3877. synchronize_irq(hcd->irq);
  3878. spin_lock_irqsave(&hsotg->lock, flags);
  3879. hprt0 = dwc2_read_hprt0(hsotg);
  3880. /* Ensure hcd is disconnected */
  3881. dwc2_hcd_disconnect(hsotg, true);
  3882. dwc2_hcd_stop(hsotg);
  3883. hsotg->lx_state = DWC2_L3;
  3884. hcd->state = HC_STATE_HALT;
  3885. clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  3886. spin_unlock_irqrestore(&hsotg->lock, flags);
  3887. /* keep balanced supply init/exit by checking HPRT0_PWR */
  3888. if (hprt0 & HPRT0_PWR)
  3889. dwc2_vbus_supply_exit(hsotg);
  3890. usleep_range(1000, 3000);
  3891. }
  3892. static int _dwc2_hcd_suspend(struct usb_hcd *hcd)
  3893. {
  3894. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  3895. unsigned long flags;
  3896. int ret = 0;
  3897. u32 hprt0;
  3898. spin_lock_irqsave(&hsotg->lock, flags);
  3899. if (dwc2_is_device_mode(hsotg))
  3900. goto unlock;
  3901. if (hsotg->lx_state != DWC2_L0)
  3902. goto unlock;
  3903. if (!HCD_HW_ACCESSIBLE(hcd))
  3904. goto unlock;
  3905. if (hsotg->op_state == OTG_STATE_B_PERIPHERAL)
  3906. goto unlock;
  3907. if (hsotg->params.power_down != DWC2_POWER_DOWN_PARAM_PARTIAL)
  3908. goto skip_power_saving;
  3909. /*
  3910. * Drive USB suspend and disable port Power
  3911. * if usb bus is not suspended.
  3912. */
  3913. if (!hsotg->bus_suspended) {
  3914. hprt0 = dwc2_read_hprt0(hsotg);
  3915. hprt0 |= HPRT0_SUSP;
  3916. hprt0 &= ~HPRT0_PWR;
  3917. dwc2_writel(hsotg, hprt0, HPRT0);
  3918. spin_unlock_irqrestore(&hsotg->lock, flags);
  3919. dwc2_vbus_supply_exit(hsotg);
  3920. spin_lock_irqsave(&hsotg->lock, flags);
  3921. }
  3922. /* Enter partial_power_down */
  3923. ret = dwc2_enter_partial_power_down(hsotg);
  3924. if (ret) {
  3925. if (ret != -ENOTSUPP)
  3926. dev_err(hsotg->dev,
  3927. "enter partial_power_down failed\n");
  3928. goto skip_power_saving;
  3929. }
  3930. /* Ask phy to be suspended */
  3931. if (!IS_ERR_OR_NULL(hsotg->uphy)) {
  3932. spin_unlock_irqrestore(&hsotg->lock, flags);
  3933. usb_phy_set_suspend(hsotg->uphy, true);
  3934. spin_lock_irqsave(&hsotg->lock, flags);
  3935. }
  3936. /* After entering partial_power_down, hardware is no more accessible */
  3937. clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  3938. skip_power_saving:
  3939. hsotg->lx_state = DWC2_L2;
  3940. unlock:
  3941. spin_unlock_irqrestore(&hsotg->lock, flags);
  3942. return ret;
  3943. }
  3944. static int _dwc2_hcd_resume(struct usb_hcd *hcd)
  3945. {
  3946. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  3947. unsigned long flags;
  3948. int ret = 0;
  3949. spin_lock_irqsave(&hsotg->lock, flags);
  3950. if (dwc2_is_device_mode(hsotg))
  3951. goto unlock;
  3952. if (hsotg->lx_state != DWC2_L2)
  3953. goto unlock;
  3954. if (hsotg->params.power_down != DWC2_POWER_DOWN_PARAM_PARTIAL) {
  3955. hsotg->lx_state = DWC2_L0;
  3956. goto unlock;
  3957. }
  3958. /*
  3959. * Set HW accessible bit before powering on the controller
  3960. * since an interrupt may rise.
  3961. */
  3962. set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  3963. /*
  3964. * Enable power if not already done.
  3965. * This must not be spinlocked since duration
  3966. * of this call is unknown.
  3967. */
  3968. if (!IS_ERR_OR_NULL(hsotg->uphy)) {
  3969. spin_unlock_irqrestore(&hsotg->lock, flags);
  3970. usb_phy_set_suspend(hsotg->uphy, false);
  3971. spin_lock_irqsave(&hsotg->lock, flags);
  3972. }
  3973. /* Exit partial_power_down */
  3974. ret = dwc2_exit_partial_power_down(hsotg, true);
  3975. if (ret && (ret != -ENOTSUPP))
  3976. dev_err(hsotg->dev, "exit partial_power_down failed\n");
  3977. hsotg->lx_state = DWC2_L0;
  3978. spin_unlock_irqrestore(&hsotg->lock, flags);
  3979. if (hsotg->bus_suspended) {
  3980. spin_lock_irqsave(&hsotg->lock, flags);
  3981. hsotg->flags.b.port_suspend_change = 1;
  3982. spin_unlock_irqrestore(&hsotg->lock, flags);
  3983. dwc2_port_resume(hsotg);
  3984. } else {
  3985. dwc2_vbus_supply_init(hsotg);
  3986. /* Wait for controller to correctly update D+/D- level */
  3987. usleep_range(3000, 5000);
  3988. /*
  3989. * Clear Port Enable and Port Status changes.
  3990. * Enable Port Power.
  3991. */
  3992. dwc2_writel(hsotg, HPRT0_PWR | HPRT0_CONNDET |
  3993. HPRT0_ENACHG, HPRT0);
  3994. /* Wait for controller to detect Port Connect */
  3995. usleep_range(5000, 7000);
  3996. }
  3997. return ret;
  3998. unlock:
  3999. spin_unlock_irqrestore(&hsotg->lock, flags);
  4000. return ret;
  4001. }
  4002. /* Returns the current frame number */
  4003. static int _dwc2_hcd_get_frame_number(struct usb_hcd *hcd)
  4004. {
  4005. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  4006. return dwc2_hcd_get_frame_number(hsotg);
  4007. }
  4008. static void dwc2_dump_urb_info(struct usb_hcd *hcd, struct urb *urb,
  4009. char *fn_name)
  4010. {
  4011. #ifdef VERBOSE_DEBUG
  4012. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  4013. char *pipetype = NULL;
  4014. char *speed = NULL;
  4015. dev_vdbg(hsotg->dev, "%s, urb %p\n", fn_name, urb);
  4016. dev_vdbg(hsotg->dev, " Device address: %d\n",
  4017. usb_pipedevice(urb->pipe));
  4018. dev_vdbg(hsotg->dev, " Endpoint: %d, %s\n",
  4019. usb_pipeendpoint(urb->pipe),
  4020. usb_pipein(urb->pipe) ? "IN" : "OUT");
  4021. switch (usb_pipetype(urb->pipe)) {
  4022. case PIPE_CONTROL:
  4023. pipetype = "CONTROL";
  4024. break;
  4025. case PIPE_BULK:
  4026. pipetype = "BULK";
  4027. break;
  4028. case PIPE_INTERRUPT:
  4029. pipetype = "INTERRUPT";
  4030. break;
  4031. case PIPE_ISOCHRONOUS:
  4032. pipetype = "ISOCHRONOUS";
  4033. break;
  4034. }
  4035. dev_vdbg(hsotg->dev, " Endpoint type: %s %s (%s)\n", pipetype,
  4036. usb_urb_dir_in(urb) ? "IN" : "OUT", usb_pipein(urb->pipe) ?
  4037. "IN" : "OUT");
  4038. switch (urb->dev->speed) {
  4039. case USB_SPEED_HIGH:
  4040. speed = "HIGH";
  4041. break;
  4042. case USB_SPEED_FULL:
  4043. speed = "FULL";
  4044. break;
  4045. case USB_SPEED_LOW:
  4046. speed = "LOW";
  4047. break;
  4048. default:
  4049. speed = "UNKNOWN";
  4050. break;
  4051. }
  4052. dev_vdbg(hsotg->dev, " Speed: %s\n", speed);
  4053. dev_vdbg(hsotg->dev, " Max packet size: %d (%d mult)\n",
  4054. usb_endpoint_maxp(&urb->ep->desc),
  4055. usb_endpoint_maxp_mult(&urb->ep->desc));
  4056. dev_vdbg(hsotg->dev, " Data buffer length: %d\n",
  4057. urb->transfer_buffer_length);
  4058. dev_vdbg(hsotg->dev, " Transfer buffer: %p, Transfer DMA: %08lx\n",
  4059. urb->transfer_buffer, (unsigned long)urb->transfer_dma);
  4060. dev_vdbg(hsotg->dev, " Setup buffer: %p, Setup DMA: %08lx\n",
  4061. urb->setup_packet, (unsigned long)urb->setup_dma);
  4062. dev_vdbg(hsotg->dev, " Interval: %d\n", urb->interval);
  4063. if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  4064. int i;
  4065. for (i = 0; i < urb->number_of_packets; i++) {
  4066. dev_vdbg(hsotg->dev, " ISO Desc %d:\n", i);
  4067. dev_vdbg(hsotg->dev, " offset: %d, length %d\n",
  4068. urb->iso_frame_desc[i].offset,
  4069. urb->iso_frame_desc[i].length);
  4070. }
  4071. }
  4072. #endif
  4073. }
  4074. /*
  4075. * Starts processing a USB transfer request specified by a USB Request Block
  4076. * (URB). mem_flags indicates the type of memory allocation to use while
  4077. * processing this URB.
  4078. */
  4079. static int _dwc2_hcd_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
  4080. gfp_t mem_flags)
  4081. {
  4082. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  4083. struct usb_host_endpoint *ep = urb->ep;
  4084. struct dwc2_hcd_urb *dwc2_urb;
  4085. int i;
  4086. int retval;
  4087. int alloc_bandwidth = 0;
  4088. u8 ep_type = 0;
  4089. u32 tflags = 0;
  4090. void *buf;
  4091. unsigned long flags;
  4092. struct dwc2_qh *qh;
  4093. bool qh_allocated = false;
  4094. struct dwc2_qtd *qtd;
  4095. if (dbg_urb(urb)) {
  4096. dev_vdbg(hsotg->dev, "DWC OTG HCD URB Enqueue\n");
  4097. dwc2_dump_urb_info(hcd, urb, "urb_enqueue");
  4098. }
  4099. if (!ep)
  4100. return -EINVAL;
  4101. if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS ||
  4102. usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
  4103. spin_lock_irqsave(&hsotg->lock, flags);
  4104. if (!dwc2_hcd_is_bandwidth_allocated(hsotg, ep))
  4105. alloc_bandwidth = 1;
  4106. spin_unlock_irqrestore(&hsotg->lock, flags);
  4107. }
  4108. switch (usb_pipetype(urb->pipe)) {
  4109. case PIPE_CONTROL:
  4110. ep_type = USB_ENDPOINT_XFER_CONTROL;
  4111. break;
  4112. case PIPE_ISOCHRONOUS:
  4113. ep_type = USB_ENDPOINT_XFER_ISOC;
  4114. break;
  4115. case PIPE_BULK:
  4116. ep_type = USB_ENDPOINT_XFER_BULK;
  4117. break;
  4118. case PIPE_INTERRUPT:
  4119. ep_type = USB_ENDPOINT_XFER_INT;
  4120. break;
  4121. }
  4122. dwc2_urb = dwc2_hcd_urb_alloc(hsotg, urb->number_of_packets,
  4123. mem_flags);
  4124. if (!dwc2_urb)
  4125. return -ENOMEM;
  4126. dwc2_hcd_urb_set_pipeinfo(hsotg, dwc2_urb, usb_pipedevice(urb->pipe),
  4127. usb_pipeendpoint(urb->pipe), ep_type,
  4128. usb_pipein(urb->pipe),
  4129. usb_endpoint_maxp(&ep->desc),
  4130. usb_endpoint_maxp_mult(&ep->desc));
  4131. buf = urb->transfer_buffer;
  4132. if (hcd->self.uses_dma) {
  4133. if (!buf && (urb->transfer_dma & 3)) {
  4134. dev_err(hsotg->dev,
  4135. "%s: unaligned transfer with no transfer_buffer",
  4136. __func__);
  4137. retval = -EINVAL;
  4138. goto fail0;
  4139. }
  4140. }
  4141. if (!(urb->transfer_flags & URB_NO_INTERRUPT))
  4142. tflags |= URB_GIVEBACK_ASAP;
  4143. if (urb->transfer_flags & URB_ZERO_PACKET)
  4144. tflags |= URB_SEND_ZERO_PACKET;
  4145. dwc2_urb->priv = urb;
  4146. dwc2_urb->buf = buf;
  4147. dwc2_urb->dma = urb->transfer_dma;
  4148. dwc2_urb->length = urb->transfer_buffer_length;
  4149. dwc2_urb->setup_packet = urb->setup_packet;
  4150. dwc2_urb->setup_dma = urb->setup_dma;
  4151. dwc2_urb->flags = tflags;
  4152. dwc2_urb->interval = urb->interval;
  4153. dwc2_urb->status = -EINPROGRESS;
  4154. for (i = 0; i < urb->number_of_packets; ++i)
  4155. dwc2_hcd_urb_set_iso_desc_params(dwc2_urb, i,
  4156. urb->iso_frame_desc[i].offset,
  4157. urb->iso_frame_desc[i].length);
  4158. urb->hcpriv = dwc2_urb;
  4159. qh = (struct dwc2_qh *)ep->hcpriv;
  4160. /* Create QH for the endpoint if it doesn't exist */
  4161. if (!qh) {
  4162. qh = dwc2_hcd_qh_create(hsotg, dwc2_urb, mem_flags);
  4163. if (!qh) {
  4164. retval = -ENOMEM;
  4165. goto fail0;
  4166. }
  4167. ep->hcpriv = qh;
  4168. qh_allocated = true;
  4169. }
  4170. qtd = kzalloc(sizeof(*qtd), mem_flags);
  4171. if (!qtd) {
  4172. retval = -ENOMEM;
  4173. goto fail1;
  4174. }
  4175. spin_lock_irqsave(&hsotg->lock, flags);
  4176. retval = usb_hcd_link_urb_to_ep(hcd, urb);
  4177. if (retval)
  4178. goto fail2;
  4179. retval = dwc2_hcd_urb_enqueue(hsotg, dwc2_urb, qh, qtd);
  4180. if (retval)
  4181. goto fail3;
  4182. if (alloc_bandwidth) {
  4183. dwc2_allocate_bus_bandwidth(hcd,
  4184. dwc2_hcd_get_ep_bandwidth(hsotg, ep),
  4185. urb);
  4186. }
  4187. spin_unlock_irqrestore(&hsotg->lock, flags);
  4188. return 0;
  4189. fail3:
  4190. dwc2_urb->priv = NULL;
  4191. usb_hcd_unlink_urb_from_ep(hcd, urb);
  4192. if (qh_allocated && qh->channel && qh->channel->qh == qh)
  4193. qh->channel->qh = NULL;
  4194. fail2:
  4195. spin_unlock_irqrestore(&hsotg->lock, flags);
  4196. urb->hcpriv = NULL;
  4197. kfree(qtd);
  4198. qtd = NULL;
  4199. fail1:
  4200. if (qh_allocated) {
  4201. struct dwc2_qtd *qtd2, *qtd2_tmp;
  4202. ep->hcpriv = NULL;
  4203. dwc2_hcd_qh_unlink(hsotg, qh);
  4204. /* Free each QTD in the QH's QTD list */
  4205. list_for_each_entry_safe(qtd2, qtd2_tmp, &qh->qtd_list,
  4206. qtd_list_entry)
  4207. dwc2_hcd_qtd_unlink_and_free(hsotg, qtd2, qh);
  4208. dwc2_hcd_qh_free(hsotg, qh);
  4209. }
  4210. fail0:
  4211. kfree(dwc2_urb);
  4212. return retval;
  4213. }
  4214. /*
  4215. * Aborts/cancels a USB transfer request. Always returns 0 to indicate success.
  4216. */
  4217. static int _dwc2_hcd_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
  4218. int status)
  4219. {
  4220. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  4221. int rc;
  4222. unsigned long flags;
  4223. dev_dbg(hsotg->dev, "DWC OTG HCD URB Dequeue\n");
  4224. dwc2_dump_urb_info(hcd, urb, "urb_dequeue");
  4225. spin_lock_irqsave(&hsotg->lock, flags);
  4226. rc = usb_hcd_check_unlink_urb(hcd, urb, status);
  4227. if (rc)
  4228. goto out;
  4229. if (!urb->hcpriv) {
  4230. dev_dbg(hsotg->dev, "## urb->hcpriv is NULL ##\n");
  4231. goto out;
  4232. }
  4233. rc = dwc2_hcd_urb_dequeue(hsotg, urb->hcpriv);
  4234. usb_hcd_unlink_urb_from_ep(hcd, urb);
  4235. kfree(urb->hcpriv);
  4236. urb->hcpriv = NULL;
  4237. /* Higher layer software sets URB status */
  4238. spin_unlock(&hsotg->lock);
  4239. usb_hcd_giveback_urb(hcd, urb, status);
  4240. spin_lock(&hsotg->lock);
  4241. dev_dbg(hsotg->dev, "Called usb_hcd_giveback_urb()\n");
  4242. dev_dbg(hsotg->dev, " urb->status = %d\n", urb->status);
  4243. out:
  4244. spin_unlock_irqrestore(&hsotg->lock, flags);
  4245. return rc;
  4246. }
  4247. /*
  4248. * Frees resources in the DWC_otg controller related to a given endpoint. Also
  4249. * clears state in the HCD related to the endpoint. Any URBs for the endpoint
  4250. * must already be dequeued.
  4251. */
  4252. static void _dwc2_hcd_endpoint_disable(struct usb_hcd *hcd,
  4253. struct usb_host_endpoint *ep)
  4254. {
  4255. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  4256. dev_dbg(hsotg->dev,
  4257. "DWC OTG HCD EP DISABLE: bEndpointAddress=0x%02x, ep->hcpriv=%p\n",
  4258. ep->desc.bEndpointAddress, ep->hcpriv);
  4259. dwc2_hcd_endpoint_disable(hsotg, ep, 250);
  4260. }
  4261. /*
  4262. * Resets endpoint specific parameter values, in current version used to reset
  4263. * the data toggle (as a WA). This function can be called from usb_clear_halt
  4264. * routine.
  4265. */
  4266. static void _dwc2_hcd_endpoint_reset(struct usb_hcd *hcd,
  4267. struct usb_host_endpoint *ep)
  4268. {
  4269. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  4270. unsigned long flags;
  4271. dev_dbg(hsotg->dev,
  4272. "DWC OTG HCD EP RESET: bEndpointAddress=0x%02x\n",
  4273. ep->desc.bEndpointAddress);
  4274. spin_lock_irqsave(&hsotg->lock, flags);
  4275. dwc2_hcd_endpoint_reset(hsotg, ep);
  4276. spin_unlock_irqrestore(&hsotg->lock, flags);
  4277. }
  4278. /*
  4279. * Handles host mode interrupts for the DWC_otg controller. Returns IRQ_NONE if
  4280. * there was no interrupt to handle. Returns IRQ_HANDLED if there was a valid
  4281. * interrupt.
  4282. *
  4283. * This function is called by the USB core when an interrupt occurs
  4284. */
  4285. static irqreturn_t _dwc2_hcd_irq(struct usb_hcd *hcd)
  4286. {
  4287. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  4288. return dwc2_handle_hcd_intr(hsotg);
  4289. }
  4290. /*
  4291. * Creates Status Change bitmap for the root hub and root port. The bitmap is
  4292. * returned in buf. Bit 0 is the status change indicator for the root hub. Bit 1
  4293. * is the status change indicator for the single root port. Returns 1 if either
  4294. * change indicator is 1, otherwise returns 0.
  4295. */
  4296. static int _dwc2_hcd_hub_status_data(struct usb_hcd *hcd, char *buf)
  4297. {
  4298. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  4299. buf[0] = dwc2_hcd_is_status_changed(hsotg, 1) << 1;
  4300. return buf[0] != 0;
  4301. }
  4302. /* Handles hub class-specific requests */
  4303. static int _dwc2_hcd_hub_control(struct usb_hcd *hcd, u16 typereq, u16 wvalue,
  4304. u16 windex, char *buf, u16 wlength)
  4305. {
  4306. int retval = dwc2_hcd_hub_control(dwc2_hcd_to_hsotg(hcd), typereq,
  4307. wvalue, windex, buf, wlength);
  4308. return retval;
  4309. }
  4310. /* Handles hub TT buffer clear completions */
  4311. static void _dwc2_hcd_clear_tt_buffer_complete(struct usb_hcd *hcd,
  4312. struct usb_host_endpoint *ep)
  4313. {
  4314. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  4315. struct dwc2_qh *qh;
  4316. unsigned long flags;
  4317. qh = ep->hcpriv;
  4318. if (!qh)
  4319. return;
  4320. spin_lock_irqsave(&hsotg->lock, flags);
  4321. qh->tt_buffer_dirty = 0;
  4322. if (hsotg->flags.b.port_connect_status)
  4323. dwc2_hcd_queue_transactions(hsotg, DWC2_TRANSACTION_ALL);
  4324. spin_unlock_irqrestore(&hsotg->lock, flags);
  4325. }
  4326. /*
  4327. * HPRT0_SPD_HIGH_SPEED: high speed
  4328. * HPRT0_SPD_FULL_SPEED: full speed
  4329. */
  4330. static void dwc2_change_bus_speed(struct usb_hcd *hcd, int speed)
  4331. {
  4332. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  4333. if (hsotg->params.speed == speed)
  4334. return;
  4335. hsotg->params.speed = speed;
  4336. queue_work(hsotg->wq_otg, &hsotg->wf_otg);
  4337. }
  4338. static void dwc2_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
  4339. {
  4340. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  4341. if (!hsotg->params.change_speed_quirk)
  4342. return;
  4343. /*
  4344. * On removal, set speed to default high-speed.
  4345. */
  4346. if (udev->parent && udev->parent->speed > USB_SPEED_UNKNOWN &&
  4347. udev->parent->speed < USB_SPEED_HIGH) {
  4348. dev_info(hsotg->dev, "Set speed to default high-speed\n");
  4349. dwc2_change_bus_speed(hcd, HPRT0_SPD_HIGH_SPEED);
  4350. }
  4351. }
  4352. static int dwc2_reset_device(struct usb_hcd *hcd, struct usb_device *udev)
  4353. {
  4354. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  4355. if (!hsotg->params.change_speed_quirk)
  4356. return 0;
  4357. if (udev->speed == USB_SPEED_HIGH) {
  4358. dev_info(hsotg->dev, "Set speed to high-speed\n");
  4359. dwc2_change_bus_speed(hcd, HPRT0_SPD_HIGH_SPEED);
  4360. } else if ((udev->speed == USB_SPEED_FULL ||
  4361. udev->speed == USB_SPEED_LOW)) {
  4362. /*
  4363. * Change speed setting to full-speed if there's
  4364. * a full-speed or low-speed device plugged in.
  4365. */
  4366. dev_info(hsotg->dev, "Set speed to full-speed\n");
  4367. dwc2_change_bus_speed(hcd, HPRT0_SPD_FULL_SPEED);
  4368. }
  4369. return 0;
  4370. }
  4371. static struct hc_driver dwc2_hc_driver = {
  4372. .description = "dwc2_hsotg",
  4373. .product_desc = "DWC OTG Controller",
  4374. .hcd_priv_size = sizeof(struct wrapper_priv_data),
  4375. .irq = _dwc2_hcd_irq,
  4376. .flags = HCD_MEMORY | HCD_USB2 | HCD_BH,
  4377. .start = _dwc2_hcd_start,
  4378. .stop = _dwc2_hcd_stop,
  4379. .urb_enqueue = _dwc2_hcd_urb_enqueue,
  4380. .urb_dequeue = _dwc2_hcd_urb_dequeue,
  4381. .endpoint_disable = _dwc2_hcd_endpoint_disable,
  4382. .endpoint_reset = _dwc2_hcd_endpoint_reset,
  4383. .get_frame_number = _dwc2_hcd_get_frame_number,
  4384. .hub_status_data = _dwc2_hcd_hub_status_data,
  4385. .hub_control = _dwc2_hcd_hub_control,
  4386. .clear_tt_buffer_complete = _dwc2_hcd_clear_tt_buffer_complete,
  4387. .bus_suspend = _dwc2_hcd_suspend,
  4388. .bus_resume = _dwc2_hcd_resume,
  4389. .map_urb_for_dma = dwc2_map_urb_for_dma,
  4390. .unmap_urb_for_dma = dwc2_unmap_urb_for_dma,
  4391. };
  4392. /*
  4393. * Frees secondary storage associated with the dwc2_hsotg structure contained
  4394. * in the struct usb_hcd field
  4395. */
  4396. static void dwc2_hcd_free(struct dwc2_hsotg *hsotg)
  4397. {
  4398. u32 ahbcfg;
  4399. u32 dctl;
  4400. int i;
  4401. dev_dbg(hsotg->dev, "DWC OTG HCD FREE\n");
  4402. /* Free memory for QH/QTD lists */
  4403. dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_inactive);
  4404. dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_waiting);
  4405. dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_active);
  4406. dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_inactive);
  4407. dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_ready);
  4408. dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_assigned);
  4409. dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_queued);
  4410. /* Free memory for the host channels */
  4411. for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  4412. struct dwc2_host_chan *chan = hsotg->hc_ptr_array[i];
  4413. if (chan) {
  4414. dev_dbg(hsotg->dev, "HCD Free channel #%i, chan=%p\n",
  4415. i, chan);
  4416. hsotg->hc_ptr_array[i] = NULL;
  4417. kfree(chan);
  4418. }
  4419. }
  4420. if (hsotg->params.host_dma) {
  4421. if (hsotg->status_buf) {
  4422. dma_free_coherent(hsotg->dev, DWC2_HCD_STATUS_BUF_SIZE,
  4423. hsotg->status_buf,
  4424. hsotg->status_buf_dma);
  4425. hsotg->status_buf = NULL;
  4426. }
  4427. } else {
  4428. kfree(hsotg->status_buf);
  4429. hsotg->status_buf = NULL;
  4430. }
  4431. ahbcfg = dwc2_readl(hsotg, GAHBCFG);
  4432. /* Disable all interrupts */
  4433. ahbcfg &= ~GAHBCFG_GLBL_INTR_EN;
  4434. dwc2_writel(hsotg, ahbcfg, GAHBCFG);
  4435. dwc2_writel(hsotg, 0, GINTMSK);
  4436. if (hsotg->hw_params.snpsid >= DWC2_CORE_REV_3_00a) {
  4437. dctl = dwc2_readl(hsotg, DCTL);
  4438. dctl |= DCTL_SFTDISCON;
  4439. dwc2_writel(hsotg, dctl, DCTL);
  4440. }
  4441. if (hsotg->wq_otg) {
  4442. if (!cancel_work_sync(&hsotg->wf_otg))
  4443. flush_workqueue(hsotg->wq_otg);
  4444. destroy_workqueue(hsotg->wq_otg);
  4445. }
  4446. del_timer(&hsotg->wkp_timer);
  4447. }
  4448. static void dwc2_hcd_release(struct dwc2_hsotg *hsotg)
  4449. {
  4450. /* Turn off all host-specific interrupts */
  4451. dwc2_disable_host_interrupts(hsotg);
  4452. dwc2_hcd_free(hsotg);
  4453. }
  4454. /*
  4455. * Initializes the HCD. This function allocates memory for and initializes the
  4456. * static parts of the usb_hcd and dwc2_hsotg structures. It also registers the
  4457. * USB bus with the core and calls the hc_driver->start() function. It returns
  4458. * a negative error on failure.
  4459. */
  4460. int dwc2_hcd_init(struct dwc2_hsotg *hsotg)
  4461. {
  4462. struct platform_device *pdev = to_platform_device(hsotg->dev);
  4463. struct resource *res;
  4464. struct usb_hcd *hcd;
  4465. struct dwc2_host_chan *channel;
  4466. u32 hcfg;
  4467. int i, num_channels;
  4468. int retval;
  4469. if (usb_disabled())
  4470. return -ENODEV;
  4471. dev_dbg(hsotg->dev, "DWC OTG HCD INIT\n");
  4472. retval = -ENOMEM;
  4473. hcfg = dwc2_readl(hsotg, HCFG);
  4474. dev_dbg(hsotg->dev, "hcfg=%08x\n", hcfg);
  4475. #ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
  4476. hsotg->frame_num_array = kcalloc(FRAME_NUM_ARRAY_SIZE,
  4477. sizeof(*hsotg->frame_num_array),
  4478. GFP_KERNEL);
  4479. if (!hsotg->frame_num_array)
  4480. goto error1;
  4481. hsotg->last_frame_num_array =
  4482. kcalloc(FRAME_NUM_ARRAY_SIZE,
  4483. sizeof(*hsotg->last_frame_num_array), GFP_KERNEL);
  4484. if (!hsotg->last_frame_num_array)
  4485. goto error1;
  4486. #endif
  4487. hsotg->last_frame_num = HFNUM_MAX_FRNUM;
  4488. /* Check if the bus driver or platform code has setup a dma_mask */
  4489. if (hsotg->params.host_dma &&
  4490. !hsotg->dev->dma_mask) {
  4491. dev_warn(hsotg->dev,
  4492. "dma_mask not set, disabling DMA\n");
  4493. hsotg->params.host_dma = false;
  4494. hsotg->params.dma_desc_enable = false;
  4495. }
  4496. /* Set device flags indicating whether the HCD supports DMA */
  4497. if (hsotg->params.host_dma) {
  4498. if (dma_set_mask(hsotg->dev, DMA_BIT_MASK(32)) < 0)
  4499. dev_warn(hsotg->dev, "can't set DMA mask\n");
  4500. if (dma_set_coherent_mask(hsotg->dev, DMA_BIT_MASK(32)) < 0)
  4501. dev_warn(hsotg->dev, "can't set coherent DMA mask\n");
  4502. }
  4503. if (hsotg->params.change_speed_quirk) {
  4504. dwc2_hc_driver.free_dev = dwc2_free_dev;
  4505. dwc2_hc_driver.reset_device = dwc2_reset_device;
  4506. }
  4507. hcd = usb_create_hcd(&dwc2_hc_driver, hsotg->dev, dev_name(hsotg->dev));
  4508. if (!hcd)
  4509. goto error1;
  4510. if (!hsotg->params.host_dma)
  4511. hcd->self.uses_dma = 0;
  4512. hcd->has_tt = 1;
  4513. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  4514. hcd->rsrc_start = res->start;
  4515. hcd->rsrc_len = resource_size(res);
  4516. ((struct wrapper_priv_data *)&hcd->hcd_priv)->hsotg = hsotg;
  4517. hsotg->priv = hcd;
  4518. /*
  4519. * Disable the global interrupt until all the interrupt handlers are
  4520. * installed
  4521. */
  4522. dwc2_disable_global_interrupts(hsotg);
  4523. /* Initialize the DWC_otg core, and select the Phy type */
  4524. retval = dwc2_core_init(hsotg, true);
  4525. if (retval)
  4526. goto error2;
  4527. /* Create new workqueue and init work */
  4528. retval = -ENOMEM;
  4529. hsotg->wq_otg = alloc_ordered_workqueue("dwc2", 0);
  4530. if (!hsotg->wq_otg) {
  4531. dev_err(hsotg->dev, "Failed to create workqueue\n");
  4532. goto error2;
  4533. }
  4534. INIT_WORK(&hsotg->wf_otg, dwc2_conn_id_status_change);
  4535. timer_setup(&hsotg->wkp_timer, dwc2_wakeup_detected, 0);
  4536. /* Initialize the non-periodic schedule */
  4537. INIT_LIST_HEAD(&hsotg->non_periodic_sched_inactive);
  4538. INIT_LIST_HEAD(&hsotg->non_periodic_sched_waiting);
  4539. INIT_LIST_HEAD(&hsotg->non_periodic_sched_active);
  4540. /* Initialize the periodic schedule */
  4541. INIT_LIST_HEAD(&hsotg->periodic_sched_inactive);
  4542. INIT_LIST_HEAD(&hsotg->periodic_sched_ready);
  4543. INIT_LIST_HEAD(&hsotg->periodic_sched_assigned);
  4544. INIT_LIST_HEAD(&hsotg->periodic_sched_queued);
  4545. INIT_LIST_HEAD(&hsotg->split_order);
  4546. /*
  4547. * Create a host channel descriptor for each host channel implemented
  4548. * in the controller. Initialize the channel descriptor array.
  4549. */
  4550. INIT_LIST_HEAD(&hsotg->free_hc_list);
  4551. num_channels = hsotg->params.host_channels;
  4552. memset(&hsotg->hc_ptr_array[0], 0, sizeof(hsotg->hc_ptr_array));
  4553. for (i = 0; i < num_channels; i++) {
  4554. channel = kzalloc(sizeof(*channel), GFP_KERNEL);
  4555. if (!channel)
  4556. goto error3;
  4557. channel->hc_num = i;
  4558. INIT_LIST_HEAD(&channel->split_order_list_entry);
  4559. hsotg->hc_ptr_array[i] = channel;
  4560. }
  4561. /* Initialize hsotg start work */
  4562. INIT_DELAYED_WORK(&hsotg->start_work, dwc2_hcd_start_func);
  4563. /* Initialize port reset work */
  4564. INIT_DELAYED_WORK(&hsotg->reset_work, dwc2_hcd_reset_func);
  4565. /*
  4566. * Allocate space for storing data on status transactions. Normally no
  4567. * data is sent, but this space acts as a bit bucket. This must be
  4568. * done after usb_add_hcd since that function allocates the DMA buffer
  4569. * pool.
  4570. */
  4571. if (hsotg->params.host_dma)
  4572. hsotg->status_buf = dma_alloc_coherent(hsotg->dev,
  4573. DWC2_HCD_STATUS_BUF_SIZE,
  4574. &hsotg->status_buf_dma, GFP_KERNEL);
  4575. else
  4576. hsotg->status_buf = kzalloc(DWC2_HCD_STATUS_BUF_SIZE,
  4577. GFP_KERNEL);
  4578. if (!hsotg->status_buf)
  4579. goto error3;
  4580. /*
  4581. * Create kmem caches to handle descriptor buffers in descriptor
  4582. * DMA mode.
  4583. * Alignment must be set to 512 bytes.
  4584. */
  4585. if (hsotg->params.dma_desc_enable ||
  4586. hsotg->params.dma_desc_fs_enable) {
  4587. hsotg->desc_gen_cache = kmem_cache_create("dwc2-gen-desc",
  4588. sizeof(struct dwc2_dma_desc) *
  4589. MAX_DMA_DESC_NUM_GENERIC, 512, SLAB_CACHE_DMA,
  4590. NULL);
  4591. if (!hsotg->desc_gen_cache) {
  4592. dev_err(hsotg->dev,
  4593. "unable to create dwc2 generic desc cache\n");
  4594. /*
  4595. * Disable descriptor dma mode since it will not be
  4596. * usable.
  4597. */
  4598. hsotg->params.dma_desc_enable = false;
  4599. hsotg->params.dma_desc_fs_enable = false;
  4600. }
  4601. hsotg->desc_hsisoc_cache = kmem_cache_create("dwc2-hsisoc-desc",
  4602. sizeof(struct dwc2_dma_desc) *
  4603. MAX_DMA_DESC_NUM_HS_ISOC, 512, 0, NULL);
  4604. if (!hsotg->desc_hsisoc_cache) {
  4605. dev_err(hsotg->dev,
  4606. "unable to create dwc2 hs isoc desc cache\n");
  4607. kmem_cache_destroy(hsotg->desc_gen_cache);
  4608. /*
  4609. * Disable descriptor dma mode since it will not be
  4610. * usable.
  4611. */
  4612. hsotg->params.dma_desc_enable = false;
  4613. hsotg->params.dma_desc_fs_enable = false;
  4614. }
  4615. }
  4616. if (hsotg->params.host_dma) {
  4617. /*
  4618. * Create kmem caches to handle non-aligned buffer
  4619. * in Buffer DMA mode.
  4620. */
  4621. hsotg->unaligned_cache = kmem_cache_create("dwc2-unaligned-dma",
  4622. DWC2_KMEM_UNALIGNED_BUF_SIZE, 4,
  4623. SLAB_CACHE_DMA, NULL);
  4624. if (!hsotg->unaligned_cache)
  4625. dev_err(hsotg->dev,
  4626. "unable to create dwc2 unaligned cache\n");
  4627. }
  4628. hsotg->otg_port = 1;
  4629. hsotg->frame_list = NULL;
  4630. hsotg->frame_list_dma = 0;
  4631. hsotg->periodic_qh_count = 0;
  4632. /* Initiate lx_state to L3 disconnected state */
  4633. hsotg->lx_state = DWC2_L3;
  4634. hcd->self.otg_port = hsotg->otg_port;
  4635. /* Don't support SG list at this point */
  4636. hcd->self.sg_tablesize = 0;
  4637. if (!IS_ERR_OR_NULL(hsotg->uphy))
  4638. otg_set_host(hsotg->uphy->otg, &hcd->self);
  4639. /*
  4640. * Finish generic HCD initialization and start the HCD. This function
  4641. * allocates the DMA buffer pool, registers the USB bus, requests the
  4642. * IRQ line, and calls hcd_start method.
  4643. */
  4644. retval = usb_add_hcd(hcd, hsotg->irq, IRQF_SHARED);
  4645. if (retval < 0)
  4646. goto error4;
  4647. device_wakeup_enable(hcd->self.controller);
  4648. dwc2_hcd_dump_state(hsotg);
  4649. dwc2_enable_global_interrupts(hsotg);
  4650. return 0;
  4651. error4:
  4652. kmem_cache_destroy(hsotg->unaligned_cache);
  4653. kmem_cache_destroy(hsotg->desc_hsisoc_cache);
  4654. kmem_cache_destroy(hsotg->desc_gen_cache);
  4655. error3:
  4656. dwc2_hcd_release(hsotg);
  4657. error2:
  4658. usb_put_hcd(hcd);
  4659. error1:
  4660. #ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
  4661. kfree(hsotg->last_frame_num_array);
  4662. kfree(hsotg->frame_num_array);
  4663. #endif
  4664. dev_err(hsotg->dev, "%s() FAILED, returning %d\n", __func__, retval);
  4665. return retval;
  4666. }
  4667. /*
  4668. * Removes the HCD.
  4669. * Frees memory and resources associated with the HCD and deregisters the bus.
  4670. */
  4671. void dwc2_hcd_remove(struct dwc2_hsotg *hsotg)
  4672. {
  4673. struct usb_hcd *hcd;
  4674. dev_dbg(hsotg->dev, "DWC OTG HCD REMOVE\n");
  4675. hcd = dwc2_hsotg_to_hcd(hsotg);
  4676. dev_dbg(hsotg->dev, "hsotg->hcd = %p\n", hcd);
  4677. if (!hcd) {
  4678. dev_dbg(hsotg->dev, "%s: dwc2_hsotg_to_hcd(hsotg) NULL!\n",
  4679. __func__);
  4680. return;
  4681. }
  4682. if (!IS_ERR_OR_NULL(hsotg->uphy))
  4683. otg_set_host(hsotg->uphy->otg, NULL);
  4684. usb_remove_hcd(hcd);
  4685. hsotg->priv = NULL;
  4686. kmem_cache_destroy(hsotg->unaligned_cache);
  4687. kmem_cache_destroy(hsotg->desc_hsisoc_cache);
  4688. kmem_cache_destroy(hsotg->desc_gen_cache);
  4689. dwc2_hcd_release(hsotg);
  4690. usb_put_hcd(hcd);
  4691. #ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
  4692. kfree(hsotg->last_frame_num_array);
  4693. kfree(hsotg->frame_num_array);
  4694. #endif
  4695. }
  4696. /**
  4697. * dwc2_backup_host_registers() - Backup controller host registers.
  4698. * When suspending usb bus, registers needs to be backuped
  4699. * if controller power is disabled once suspended.
  4700. *
  4701. * @hsotg: Programming view of the DWC_otg controller
  4702. */
  4703. int dwc2_backup_host_registers(struct dwc2_hsotg *hsotg)
  4704. {
  4705. struct dwc2_hregs_backup *hr;
  4706. int i;
  4707. dev_dbg(hsotg->dev, "%s\n", __func__);
  4708. /* Backup Host regs */
  4709. hr = &hsotg->hr_backup;
  4710. hr->hcfg = dwc2_readl(hsotg, HCFG);
  4711. hr->haintmsk = dwc2_readl(hsotg, HAINTMSK);
  4712. for (i = 0; i < hsotg->params.host_channels; ++i)
  4713. hr->hcintmsk[i] = dwc2_readl(hsotg, HCINTMSK(i));
  4714. hr->hprt0 = dwc2_read_hprt0(hsotg);
  4715. hr->hfir = dwc2_readl(hsotg, HFIR);
  4716. hr->hptxfsiz = dwc2_readl(hsotg, HPTXFSIZ);
  4717. hr->valid = true;
  4718. return 0;
  4719. }
  4720. /**
  4721. * dwc2_restore_host_registers() - Restore controller host registers.
  4722. * When resuming usb bus, device registers needs to be restored
  4723. * if controller power were disabled.
  4724. *
  4725. * @hsotg: Programming view of the DWC_otg controller
  4726. */
  4727. int dwc2_restore_host_registers(struct dwc2_hsotg *hsotg)
  4728. {
  4729. struct dwc2_hregs_backup *hr;
  4730. int i;
  4731. dev_dbg(hsotg->dev, "%s\n", __func__);
  4732. /* Restore host regs */
  4733. hr = &hsotg->hr_backup;
  4734. if (!hr->valid) {
  4735. dev_err(hsotg->dev, "%s: no host registers to restore\n",
  4736. __func__);
  4737. return -EINVAL;
  4738. }
  4739. hr->valid = false;
  4740. dwc2_writel(hsotg, hr->hcfg, HCFG);
  4741. dwc2_writel(hsotg, hr->haintmsk, HAINTMSK);
  4742. for (i = 0; i < hsotg->params.host_channels; ++i)
  4743. dwc2_writel(hsotg, hr->hcintmsk[i], HCINTMSK(i));
  4744. dwc2_writel(hsotg, hr->hprt0, HPRT0);
  4745. dwc2_writel(hsotg, hr->hfir, HFIR);
  4746. dwc2_writel(hsotg, hr->hptxfsiz, HPTXFSIZ);
  4747. hsotg->frame_number = 0;
  4748. return 0;
  4749. }
  4750. /**
  4751. * dwc2_host_enter_hibernation() - Put controller in Hibernation.
  4752. *
  4753. * @hsotg: Programming view of the DWC_otg controller
  4754. */
  4755. int dwc2_host_enter_hibernation(struct dwc2_hsotg *hsotg)
  4756. {
  4757. unsigned long flags;
  4758. int ret = 0;
  4759. u32 hprt0;
  4760. u32 pcgcctl;
  4761. u32 gusbcfg;
  4762. u32 gpwrdn;
  4763. dev_dbg(hsotg->dev, "Preparing host for hibernation\n");
  4764. ret = dwc2_backup_global_registers(hsotg);
  4765. if (ret) {
  4766. dev_err(hsotg->dev, "%s: failed to backup global registers\n",
  4767. __func__);
  4768. return ret;
  4769. }
  4770. ret = dwc2_backup_host_registers(hsotg);
  4771. if (ret) {
  4772. dev_err(hsotg->dev, "%s: failed to backup host registers\n",
  4773. __func__);
  4774. return ret;
  4775. }
  4776. /* Enter USB Suspend Mode */
  4777. hprt0 = dwc2_readl(hsotg, HPRT0);
  4778. hprt0 |= HPRT0_SUSP;
  4779. hprt0 &= ~HPRT0_ENA;
  4780. dwc2_writel(hsotg, hprt0, HPRT0);
  4781. /* Wait for the HPRT0.PrtSusp register field to be set */
  4782. if (dwc2_hsotg_wait_bit_set(hsotg, HPRT0, HPRT0_SUSP, 5000))
  4783. dev_warn(hsotg->dev, "Suspend wasn't generated\n");
  4784. /*
  4785. * We need to disable interrupts to prevent servicing of any IRQ
  4786. * during going to hibernation
  4787. */
  4788. spin_lock_irqsave(&hsotg->lock, flags);
  4789. hsotg->lx_state = DWC2_L2;
  4790. gusbcfg = dwc2_readl(hsotg, GUSBCFG);
  4791. if (gusbcfg & GUSBCFG_ULPI_UTMI_SEL) {
  4792. /* ULPI interface */
  4793. /* Suspend the Phy Clock */
  4794. pcgcctl = dwc2_readl(hsotg, PCGCTL);
  4795. pcgcctl |= PCGCTL_STOPPCLK;
  4796. dwc2_writel(hsotg, pcgcctl, PCGCTL);
  4797. udelay(10);
  4798. gpwrdn = dwc2_readl(hsotg, GPWRDN);
  4799. gpwrdn |= GPWRDN_PMUACTV;
  4800. dwc2_writel(hsotg, gpwrdn, GPWRDN);
  4801. udelay(10);
  4802. } else {
  4803. /* UTMI+ Interface */
  4804. gpwrdn = dwc2_readl(hsotg, GPWRDN);
  4805. gpwrdn |= GPWRDN_PMUACTV;
  4806. dwc2_writel(hsotg, gpwrdn, GPWRDN);
  4807. udelay(10);
  4808. pcgcctl = dwc2_readl(hsotg, PCGCTL);
  4809. pcgcctl |= PCGCTL_STOPPCLK;
  4810. dwc2_writel(hsotg, pcgcctl, PCGCTL);
  4811. udelay(10);
  4812. }
  4813. /* Enable interrupts from wake up logic */
  4814. gpwrdn = dwc2_readl(hsotg, GPWRDN);
  4815. gpwrdn |= GPWRDN_PMUINTSEL;
  4816. dwc2_writel(hsotg, gpwrdn, GPWRDN);
  4817. udelay(10);
  4818. /* Unmask host mode interrupts in GPWRDN */
  4819. gpwrdn = dwc2_readl(hsotg, GPWRDN);
  4820. gpwrdn |= GPWRDN_DISCONN_DET_MSK;
  4821. gpwrdn |= GPWRDN_LNSTSCHG_MSK;
  4822. gpwrdn |= GPWRDN_STS_CHGINT_MSK;
  4823. dwc2_writel(hsotg, gpwrdn, GPWRDN);
  4824. udelay(10);
  4825. /* Enable Power Down Clamp */
  4826. gpwrdn = dwc2_readl(hsotg, GPWRDN);
  4827. gpwrdn |= GPWRDN_PWRDNCLMP;
  4828. dwc2_writel(hsotg, gpwrdn, GPWRDN);
  4829. udelay(10);
  4830. /* Switch off VDD */
  4831. gpwrdn = dwc2_readl(hsotg, GPWRDN);
  4832. gpwrdn |= GPWRDN_PWRDNSWTCH;
  4833. dwc2_writel(hsotg, gpwrdn, GPWRDN);
  4834. hsotg->hibernated = 1;
  4835. hsotg->bus_suspended = 1;
  4836. dev_dbg(hsotg->dev, "Host hibernation completed\n");
  4837. spin_unlock_irqrestore(&hsotg->lock, flags);
  4838. return ret;
  4839. }
  4840. /*
  4841. * dwc2_host_exit_hibernation()
  4842. *
  4843. * @hsotg: Programming view of the DWC_otg controller
  4844. * @rem_wakeup: indicates whether resume is initiated by Device or Host.
  4845. * @param reset: indicates whether resume is initiated by Reset.
  4846. *
  4847. * Return: non-zero if failed to enter to hibernation.
  4848. *
  4849. * This function is for exiting from Host mode hibernation by
  4850. * Host Initiated Resume/Reset and Device Initiated Remote-Wakeup.
  4851. */
  4852. int dwc2_host_exit_hibernation(struct dwc2_hsotg *hsotg, int rem_wakeup,
  4853. int reset)
  4854. {
  4855. u32 gpwrdn;
  4856. u32 hprt0;
  4857. int ret = 0;
  4858. struct dwc2_gregs_backup *gr;
  4859. struct dwc2_hregs_backup *hr;
  4860. gr = &hsotg->gr_backup;
  4861. hr = &hsotg->hr_backup;
  4862. dev_dbg(hsotg->dev,
  4863. "%s: called with rem_wakeup = %d reset = %d\n",
  4864. __func__, rem_wakeup, reset);
  4865. dwc2_hib_restore_common(hsotg, rem_wakeup, 1);
  4866. hsotg->hibernated = 0;
  4867. /*
  4868. * This step is not described in functional spec but if not wait for
  4869. * this delay, mismatch interrupts occurred because just after restore
  4870. * core is in Device mode(gintsts.curmode == 0)
  4871. */
  4872. mdelay(100);
  4873. /* Clear all pending interupts */
  4874. dwc2_writel(hsotg, 0xffffffff, GINTSTS);
  4875. /* De-assert Restore */
  4876. gpwrdn = dwc2_readl(hsotg, GPWRDN);
  4877. gpwrdn &= ~GPWRDN_RESTORE;
  4878. dwc2_writel(hsotg, gpwrdn, GPWRDN);
  4879. udelay(10);
  4880. /* Restore GUSBCFG, HCFG */
  4881. dwc2_writel(hsotg, gr->gusbcfg, GUSBCFG);
  4882. dwc2_writel(hsotg, hr->hcfg, HCFG);
  4883. /* De-assert Wakeup Logic */
  4884. gpwrdn = dwc2_readl(hsotg, GPWRDN);
  4885. gpwrdn &= ~GPWRDN_PMUACTV;
  4886. dwc2_writel(hsotg, gpwrdn, GPWRDN);
  4887. udelay(10);
  4888. hprt0 = hr->hprt0;
  4889. hprt0 |= HPRT0_PWR;
  4890. hprt0 &= ~HPRT0_ENA;
  4891. hprt0 &= ~HPRT0_SUSP;
  4892. dwc2_writel(hsotg, hprt0, HPRT0);
  4893. hprt0 = hr->hprt0;
  4894. hprt0 |= HPRT0_PWR;
  4895. hprt0 &= ~HPRT0_ENA;
  4896. hprt0 &= ~HPRT0_SUSP;
  4897. if (reset) {
  4898. hprt0 |= HPRT0_RST;
  4899. dwc2_writel(hsotg, hprt0, HPRT0);
  4900. /* Wait for Resume time and then program HPRT again */
  4901. mdelay(60);
  4902. hprt0 &= ~HPRT0_RST;
  4903. dwc2_writel(hsotg, hprt0, HPRT0);
  4904. } else {
  4905. hprt0 |= HPRT0_RES;
  4906. dwc2_writel(hsotg, hprt0, HPRT0);
  4907. /* Wait for Resume time and then program HPRT again */
  4908. mdelay(100);
  4909. hprt0 &= ~HPRT0_RES;
  4910. dwc2_writel(hsotg, hprt0, HPRT0);
  4911. }
  4912. /* Clear all interrupt status */
  4913. hprt0 = dwc2_readl(hsotg, HPRT0);
  4914. hprt0 |= HPRT0_CONNDET;
  4915. hprt0 |= HPRT0_ENACHG;
  4916. hprt0 &= ~HPRT0_ENA;
  4917. dwc2_writel(hsotg, hprt0, HPRT0);
  4918. hprt0 = dwc2_readl(hsotg, HPRT0);
  4919. /* Clear all pending interupts */
  4920. dwc2_writel(hsotg, 0xffffffff, GINTSTS);
  4921. /* Restore global registers */
  4922. ret = dwc2_restore_global_registers(hsotg);
  4923. if (ret) {
  4924. dev_err(hsotg->dev, "%s: failed to restore registers\n",
  4925. __func__);
  4926. return ret;
  4927. }
  4928. /* Restore host registers */
  4929. ret = dwc2_restore_host_registers(hsotg);
  4930. if (ret) {
  4931. dev_err(hsotg->dev, "%s: failed to restore host registers\n",
  4932. __func__);
  4933. return ret;
  4934. }
  4935. if (rem_wakeup) {
  4936. dwc2_hcd_rem_wakeup(hsotg);
  4937. /*
  4938. * Change "port_connect_status_change" flag to re-enumerate,
  4939. * because after exit from hibernation port connection status
  4940. * is not detected.
  4941. */
  4942. hsotg->flags.b.port_connect_status_change = 1;
  4943. }
  4944. hsotg->hibernated = 0;
  4945. hsotg->bus_suspended = 0;
  4946. hsotg->lx_state = DWC2_L0;
  4947. dev_dbg(hsotg->dev, "Host hibernation restore complete\n");
  4948. return ret;
  4949. }