hcd.c 156 KB

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