hcd.c 151 KB

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