hcd.c 152 KB

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