hcd.c 157 KB

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