hcd.c 158 KB

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