hcd.c 157 KB

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