hcd.c 152 KB

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