hcd.c 151 KB

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