hcd.c 163 KB

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