hcd.c 149 KB

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