hcd.c 157 KB

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