hcd.c 163 KB

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