hcd.c 158 KB

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