gadget.c 148 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2011 Samsung Electronics Co., Ltd.
  4. * http://www.samsung.com
  5. *
  6. * Copyright 2008 Openmoko, Inc.
  7. * Copyright 2008 Simtec Electronics
  8. * Ben Dooks <ben@simtec.co.uk>
  9. * http://armlinux.simtec.co.uk/
  10. *
  11. * S3C USB2.0 High-speed / OtG driver
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/dma-mapping.h>
  19. #include <linux/mutex.h>
  20. #include <linux/seq_file.h>
  21. #include <linux/delay.h>
  22. #include <linux/io.h>
  23. #include <linux/slab.h>
  24. #include <linux/usb/ch9.h>
  25. #include <linux/usb/gadget.h>
  26. #include <linux/usb/phy.h>
  27. #include <linux/usb/composite.h>
  28. #include "core.h"
  29. #include "hw.h"
  30. /* conversion functions */
  31. static inline struct dwc2_hsotg_req *our_req(struct usb_request *req)
  32. {
  33. return container_of(req, struct dwc2_hsotg_req, req);
  34. }
  35. static inline struct dwc2_hsotg_ep *our_ep(struct usb_ep *ep)
  36. {
  37. return container_of(ep, struct dwc2_hsotg_ep, ep);
  38. }
  39. static inline struct dwc2_hsotg *to_hsotg(struct usb_gadget *gadget)
  40. {
  41. return container_of(gadget, struct dwc2_hsotg, gadget);
  42. }
  43. static inline void dwc2_set_bit(struct dwc2_hsotg *hsotg, u32 offset, u32 val)
  44. {
  45. dwc2_writel(hsotg, dwc2_readl(hsotg, offset) | val, offset);
  46. }
  47. static inline void dwc2_clear_bit(struct dwc2_hsotg *hsotg, u32 offset, u32 val)
  48. {
  49. dwc2_writel(hsotg, dwc2_readl(hsotg, offset) & ~val, offset);
  50. }
  51. static inline struct dwc2_hsotg_ep *index_to_ep(struct dwc2_hsotg *hsotg,
  52. u32 ep_index, u32 dir_in)
  53. {
  54. if (dir_in)
  55. return hsotg->eps_in[ep_index];
  56. else
  57. return hsotg->eps_out[ep_index];
  58. }
  59. /* forward declaration of functions */
  60. static void dwc2_hsotg_dump(struct dwc2_hsotg *hsotg);
  61. /**
  62. * using_dma - return the DMA status of the driver.
  63. * @hsotg: The driver state.
  64. *
  65. * Return true if we're using DMA.
  66. *
  67. * Currently, we have the DMA support code worked into everywhere
  68. * that needs it, but the AMBA DMA implementation in the hardware can
  69. * only DMA from 32bit aligned addresses. This means that gadgets such
  70. * as the CDC Ethernet cannot work as they often pass packets which are
  71. * not 32bit aligned.
  72. *
  73. * Unfortunately the choice to use DMA or not is global to the controller
  74. * and seems to be only settable when the controller is being put through
  75. * a core reset. This means we either need to fix the gadgets to take
  76. * account of DMA alignment, or add bounce buffers (yuerk).
  77. *
  78. * g_using_dma is set depending on dts flag.
  79. */
  80. static inline bool using_dma(struct dwc2_hsotg *hsotg)
  81. {
  82. return hsotg->params.g_dma;
  83. }
  84. /*
  85. * using_desc_dma - return the descriptor DMA status of the driver.
  86. * @hsotg: The driver state.
  87. *
  88. * Return true if we're using descriptor DMA.
  89. */
  90. static inline bool using_desc_dma(struct dwc2_hsotg *hsotg)
  91. {
  92. return hsotg->params.g_dma_desc;
  93. }
  94. /**
  95. * dwc2_gadget_incr_frame_num - Increments the targeted frame number.
  96. * @hs_ep: The endpoint
  97. *
  98. * This function will also check if the frame number overruns DSTS_SOFFN_LIMIT.
  99. * If an overrun occurs it will wrap the value and set the frame_overrun flag.
  100. */
  101. static inline void dwc2_gadget_incr_frame_num(struct dwc2_hsotg_ep *hs_ep)
  102. {
  103. struct dwc2_hsotg *hsotg = hs_ep->parent;
  104. u16 limit = DSTS_SOFFN_LIMIT;
  105. if (hsotg->gadget.speed != USB_SPEED_HIGH)
  106. limit >>= 3;
  107. hs_ep->target_frame += hs_ep->interval;
  108. if (hs_ep->target_frame > limit) {
  109. hs_ep->frame_overrun = true;
  110. hs_ep->target_frame &= limit;
  111. } else {
  112. hs_ep->frame_overrun = false;
  113. }
  114. }
  115. /**
  116. * dwc2_gadget_dec_frame_num_by_one - Decrements the targeted frame number
  117. * by one.
  118. * @hs_ep: The endpoint.
  119. *
  120. * This function used in service interval based scheduling flow to calculate
  121. * descriptor frame number filed value. For service interval mode frame
  122. * number in descriptor should point to last (u)frame in the interval.
  123. *
  124. */
  125. static inline void dwc2_gadget_dec_frame_num_by_one(struct dwc2_hsotg_ep *hs_ep)
  126. {
  127. struct dwc2_hsotg *hsotg = hs_ep->parent;
  128. u16 limit = DSTS_SOFFN_LIMIT;
  129. if (hsotg->gadget.speed != USB_SPEED_HIGH)
  130. limit >>= 3;
  131. if (hs_ep->target_frame)
  132. hs_ep->target_frame -= 1;
  133. else
  134. hs_ep->target_frame = limit;
  135. }
  136. /**
  137. * dwc2_hsotg_en_gsint - enable one or more of the general interrupt
  138. * @hsotg: The device state
  139. * @ints: A bitmask of the interrupts to enable
  140. */
  141. static void dwc2_hsotg_en_gsint(struct dwc2_hsotg *hsotg, u32 ints)
  142. {
  143. u32 gsintmsk = dwc2_readl(hsotg, GINTMSK);
  144. u32 new_gsintmsk;
  145. new_gsintmsk = gsintmsk | ints;
  146. if (new_gsintmsk != gsintmsk) {
  147. dev_dbg(hsotg->dev, "gsintmsk now 0x%08x\n", new_gsintmsk);
  148. dwc2_writel(hsotg, new_gsintmsk, GINTMSK);
  149. }
  150. }
  151. /**
  152. * dwc2_hsotg_disable_gsint - disable one or more of the general interrupt
  153. * @hsotg: The device state
  154. * @ints: A bitmask of the interrupts to enable
  155. */
  156. static void dwc2_hsotg_disable_gsint(struct dwc2_hsotg *hsotg, u32 ints)
  157. {
  158. u32 gsintmsk = dwc2_readl(hsotg, GINTMSK);
  159. u32 new_gsintmsk;
  160. new_gsintmsk = gsintmsk & ~ints;
  161. if (new_gsintmsk != gsintmsk)
  162. dwc2_writel(hsotg, new_gsintmsk, GINTMSK);
  163. }
  164. /**
  165. * dwc2_hsotg_ctrl_epint - enable/disable an endpoint irq
  166. * @hsotg: The device state
  167. * @ep: The endpoint index
  168. * @dir_in: True if direction is in.
  169. * @en: The enable value, true to enable
  170. *
  171. * Set or clear the mask for an individual endpoint's interrupt
  172. * request.
  173. */
  174. static void dwc2_hsotg_ctrl_epint(struct dwc2_hsotg *hsotg,
  175. unsigned int ep, unsigned int dir_in,
  176. unsigned int en)
  177. {
  178. unsigned long flags;
  179. u32 bit = 1 << ep;
  180. u32 daint;
  181. if (!dir_in)
  182. bit <<= 16;
  183. local_irq_save(flags);
  184. daint = dwc2_readl(hsotg, DAINTMSK);
  185. if (en)
  186. daint |= bit;
  187. else
  188. daint &= ~bit;
  189. dwc2_writel(hsotg, daint, DAINTMSK);
  190. local_irq_restore(flags);
  191. }
  192. /**
  193. * dwc2_hsotg_tx_fifo_count - return count of TX FIFOs in device mode
  194. *
  195. * @hsotg: Programming view of the DWC_otg controller
  196. */
  197. int dwc2_hsotg_tx_fifo_count(struct dwc2_hsotg *hsotg)
  198. {
  199. if (hsotg->hw_params.en_multiple_tx_fifo)
  200. /* In dedicated FIFO mode we need count of IN EPs */
  201. return hsotg->hw_params.num_dev_in_eps;
  202. else
  203. /* In shared FIFO mode we need count of Periodic IN EPs */
  204. return hsotg->hw_params.num_dev_perio_in_ep;
  205. }
  206. /**
  207. * dwc2_hsotg_tx_fifo_total_depth - return total FIFO depth available for
  208. * device mode TX FIFOs
  209. *
  210. * @hsotg: Programming view of the DWC_otg controller
  211. */
  212. int dwc2_hsotg_tx_fifo_total_depth(struct dwc2_hsotg *hsotg)
  213. {
  214. int addr;
  215. int tx_addr_max;
  216. u32 np_tx_fifo_size;
  217. np_tx_fifo_size = min_t(u32, hsotg->hw_params.dev_nperio_tx_fifo_size,
  218. hsotg->params.g_np_tx_fifo_size);
  219. /* Get Endpoint Info Control block size in DWORDs. */
  220. tx_addr_max = hsotg->hw_params.total_fifo_size;
  221. addr = hsotg->params.g_rx_fifo_size + np_tx_fifo_size;
  222. if (tx_addr_max <= addr)
  223. return 0;
  224. return tx_addr_max - addr;
  225. }
  226. /**
  227. * dwc2_gadget_wkup_alert_handler - Handler for WKUP_ALERT interrupt
  228. *
  229. * @hsotg: Programming view of the DWC_otg controller
  230. *
  231. */
  232. static void dwc2_gadget_wkup_alert_handler(struct dwc2_hsotg *hsotg)
  233. {
  234. u32 gintsts2;
  235. u32 gintmsk2;
  236. gintsts2 = dwc2_readl(hsotg, GINTSTS2);
  237. gintmsk2 = dwc2_readl(hsotg, GINTMSK2);
  238. gintsts2 &= gintmsk2;
  239. if (gintsts2 & GINTSTS2_WKUP_ALERT_INT) {
  240. dev_dbg(hsotg->dev, "%s: Wkup_Alert_Int\n", __func__);
  241. dwc2_set_bit(hsotg, GINTSTS2, GINTSTS2_WKUP_ALERT_INT);
  242. dwc2_set_bit(hsotg, DCTL, DCTL_RMTWKUPSIG);
  243. }
  244. }
  245. /**
  246. * dwc2_hsotg_tx_fifo_average_depth - returns average depth of device mode
  247. * TX FIFOs
  248. *
  249. * @hsotg: Programming view of the DWC_otg controller
  250. */
  251. int dwc2_hsotg_tx_fifo_average_depth(struct dwc2_hsotg *hsotg)
  252. {
  253. int tx_fifo_count;
  254. int tx_fifo_depth;
  255. tx_fifo_depth = dwc2_hsotg_tx_fifo_total_depth(hsotg);
  256. tx_fifo_count = dwc2_hsotg_tx_fifo_count(hsotg);
  257. if (!tx_fifo_count)
  258. return tx_fifo_depth;
  259. else
  260. return tx_fifo_depth / tx_fifo_count;
  261. }
  262. /**
  263. * dwc2_hsotg_init_fifo - initialise non-periodic FIFOs
  264. * @hsotg: The device instance.
  265. */
  266. static void dwc2_hsotg_init_fifo(struct dwc2_hsotg *hsotg)
  267. {
  268. unsigned int ep;
  269. unsigned int addr;
  270. int timeout;
  271. u32 val;
  272. u32 *txfsz = hsotg->params.g_tx_fifo_size;
  273. /* Reset fifo map if not correctly cleared during previous session */
  274. WARN_ON(hsotg->fifo_map);
  275. hsotg->fifo_map = 0;
  276. /* set RX/NPTX FIFO sizes */
  277. dwc2_writel(hsotg, hsotg->params.g_rx_fifo_size, GRXFSIZ);
  278. dwc2_writel(hsotg, (hsotg->params.g_rx_fifo_size <<
  279. FIFOSIZE_STARTADDR_SHIFT) |
  280. (hsotg->params.g_np_tx_fifo_size << FIFOSIZE_DEPTH_SHIFT),
  281. GNPTXFSIZ);
  282. /*
  283. * arange all the rest of the TX FIFOs, as some versions of this
  284. * block have overlapping default addresses. This also ensures
  285. * that if the settings have been changed, then they are set to
  286. * known values.
  287. */
  288. /* start at the end of the GNPTXFSIZ, rounded up */
  289. addr = hsotg->params.g_rx_fifo_size + hsotg->params.g_np_tx_fifo_size;
  290. /*
  291. * Configure fifos sizes from provided configuration and assign
  292. * them to endpoints dynamically according to maxpacket size value of
  293. * given endpoint.
  294. */
  295. for (ep = 1; ep < MAX_EPS_CHANNELS; ep++) {
  296. if (!txfsz[ep])
  297. continue;
  298. val = addr;
  299. val |= txfsz[ep] << FIFOSIZE_DEPTH_SHIFT;
  300. WARN_ONCE(addr + txfsz[ep] > hsotg->fifo_mem,
  301. "insufficient fifo memory");
  302. addr += txfsz[ep];
  303. dwc2_writel(hsotg, val, DPTXFSIZN(ep));
  304. val = dwc2_readl(hsotg, DPTXFSIZN(ep));
  305. }
  306. dwc2_writel(hsotg, hsotg->hw_params.total_fifo_size |
  307. addr << GDFIFOCFG_EPINFOBASE_SHIFT,
  308. GDFIFOCFG);
  309. /*
  310. * according to p428 of the design guide, we need to ensure that
  311. * all fifos are flushed before continuing
  312. */
  313. dwc2_writel(hsotg, GRSTCTL_TXFNUM(0x10) | GRSTCTL_TXFFLSH |
  314. GRSTCTL_RXFFLSH, GRSTCTL);
  315. /* wait until the fifos are both flushed */
  316. timeout = 100;
  317. while (1) {
  318. val = dwc2_readl(hsotg, GRSTCTL);
  319. if ((val & (GRSTCTL_TXFFLSH | GRSTCTL_RXFFLSH)) == 0)
  320. break;
  321. if (--timeout == 0) {
  322. dev_err(hsotg->dev,
  323. "%s: timeout flushing fifos (GRSTCTL=%08x)\n",
  324. __func__, val);
  325. break;
  326. }
  327. udelay(1);
  328. }
  329. dev_dbg(hsotg->dev, "FIFOs reset, timeout at %d\n", timeout);
  330. }
  331. /**
  332. * dwc2_hsotg_ep_alloc_request - allocate USB rerequest structure
  333. * @ep: USB endpoint to allocate request for.
  334. * @flags: Allocation flags
  335. *
  336. * Allocate a new USB request structure appropriate for the specified endpoint
  337. */
  338. static struct usb_request *dwc2_hsotg_ep_alloc_request(struct usb_ep *ep,
  339. gfp_t flags)
  340. {
  341. struct dwc2_hsotg_req *req;
  342. req = kzalloc(sizeof(*req), flags);
  343. if (!req)
  344. return NULL;
  345. INIT_LIST_HEAD(&req->queue);
  346. return &req->req;
  347. }
  348. /**
  349. * is_ep_periodic - return true if the endpoint is in periodic mode.
  350. * @hs_ep: The endpoint to query.
  351. *
  352. * Returns true if the endpoint is in periodic mode, meaning it is being
  353. * used for an Interrupt or ISO transfer.
  354. */
  355. static inline int is_ep_periodic(struct dwc2_hsotg_ep *hs_ep)
  356. {
  357. return hs_ep->periodic;
  358. }
  359. /**
  360. * dwc2_hsotg_unmap_dma - unmap the DMA memory being used for the request
  361. * @hsotg: The device state.
  362. * @hs_ep: The endpoint for the request
  363. * @hs_req: The request being processed.
  364. *
  365. * This is the reverse of dwc2_hsotg_map_dma(), called for the completion
  366. * of a request to ensure the buffer is ready for access by the caller.
  367. */
  368. static void dwc2_hsotg_unmap_dma(struct dwc2_hsotg *hsotg,
  369. struct dwc2_hsotg_ep *hs_ep,
  370. struct dwc2_hsotg_req *hs_req)
  371. {
  372. struct usb_request *req = &hs_req->req;
  373. usb_gadget_unmap_request(&hsotg->gadget, req, hs_ep->map_dir);
  374. }
  375. /*
  376. * dwc2_gadget_alloc_ctrl_desc_chains - allocate DMA descriptor chains
  377. * for Control endpoint
  378. * @hsotg: The device state.
  379. *
  380. * This function will allocate 4 descriptor chains for EP 0: 2 for
  381. * Setup stage, per one for IN and OUT data/status transactions.
  382. */
  383. static int dwc2_gadget_alloc_ctrl_desc_chains(struct dwc2_hsotg *hsotg)
  384. {
  385. hsotg->setup_desc[0] =
  386. dmam_alloc_coherent(hsotg->dev,
  387. sizeof(struct dwc2_dma_desc),
  388. &hsotg->setup_desc_dma[0],
  389. GFP_KERNEL);
  390. if (!hsotg->setup_desc[0])
  391. goto fail;
  392. hsotg->setup_desc[1] =
  393. dmam_alloc_coherent(hsotg->dev,
  394. sizeof(struct dwc2_dma_desc),
  395. &hsotg->setup_desc_dma[1],
  396. GFP_KERNEL);
  397. if (!hsotg->setup_desc[1])
  398. goto fail;
  399. hsotg->ctrl_in_desc =
  400. dmam_alloc_coherent(hsotg->dev,
  401. sizeof(struct dwc2_dma_desc),
  402. &hsotg->ctrl_in_desc_dma,
  403. GFP_KERNEL);
  404. if (!hsotg->ctrl_in_desc)
  405. goto fail;
  406. hsotg->ctrl_out_desc =
  407. dmam_alloc_coherent(hsotg->dev,
  408. sizeof(struct dwc2_dma_desc),
  409. &hsotg->ctrl_out_desc_dma,
  410. GFP_KERNEL);
  411. if (!hsotg->ctrl_out_desc)
  412. goto fail;
  413. return 0;
  414. fail:
  415. return -ENOMEM;
  416. }
  417. /**
  418. * dwc2_hsotg_write_fifo - write packet Data to the TxFIFO
  419. * @hsotg: The controller state.
  420. * @hs_ep: The endpoint we're going to write for.
  421. * @hs_req: The request to write data for.
  422. *
  423. * This is called when the TxFIFO has some space in it to hold a new
  424. * transmission and we have something to give it. The actual setup of
  425. * the data size is done elsewhere, so all we have to do is to actually
  426. * write the data.
  427. *
  428. * The return value is zero if there is more space (or nothing was done)
  429. * otherwise -ENOSPC is returned if the FIFO space was used up.
  430. *
  431. * This routine is only needed for PIO
  432. */
  433. static int dwc2_hsotg_write_fifo(struct dwc2_hsotg *hsotg,
  434. struct dwc2_hsotg_ep *hs_ep,
  435. struct dwc2_hsotg_req *hs_req)
  436. {
  437. bool periodic = is_ep_periodic(hs_ep);
  438. u32 gnptxsts = dwc2_readl(hsotg, GNPTXSTS);
  439. int buf_pos = hs_req->req.actual;
  440. int to_write = hs_ep->size_loaded;
  441. void *data;
  442. int can_write;
  443. int pkt_round;
  444. int max_transfer;
  445. to_write -= (buf_pos - hs_ep->last_load);
  446. /* if there's nothing to write, get out early */
  447. if (to_write == 0)
  448. return 0;
  449. if (periodic && !hsotg->dedicated_fifos) {
  450. u32 epsize = dwc2_readl(hsotg, DIEPTSIZ(hs_ep->index));
  451. int size_left;
  452. int size_done;
  453. /*
  454. * work out how much data was loaded so we can calculate
  455. * how much data is left in the fifo.
  456. */
  457. size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
  458. /*
  459. * if shared fifo, we cannot write anything until the
  460. * previous data has been completely sent.
  461. */
  462. if (hs_ep->fifo_load != 0) {
  463. dwc2_hsotg_en_gsint(hsotg, GINTSTS_PTXFEMP);
  464. return -ENOSPC;
  465. }
  466. dev_dbg(hsotg->dev, "%s: left=%d, load=%d, fifo=%d, size %d\n",
  467. __func__, size_left,
  468. hs_ep->size_loaded, hs_ep->fifo_load, hs_ep->fifo_size);
  469. /* how much of the data has moved */
  470. size_done = hs_ep->size_loaded - size_left;
  471. /* how much data is left in the fifo */
  472. can_write = hs_ep->fifo_load - size_done;
  473. dev_dbg(hsotg->dev, "%s: => can_write1=%d\n",
  474. __func__, can_write);
  475. can_write = hs_ep->fifo_size - can_write;
  476. dev_dbg(hsotg->dev, "%s: => can_write2=%d\n",
  477. __func__, can_write);
  478. if (can_write <= 0) {
  479. dwc2_hsotg_en_gsint(hsotg, GINTSTS_PTXFEMP);
  480. return -ENOSPC;
  481. }
  482. } else if (hsotg->dedicated_fifos && hs_ep->index != 0) {
  483. can_write = dwc2_readl(hsotg,
  484. DTXFSTS(hs_ep->fifo_index));
  485. can_write &= 0xffff;
  486. can_write *= 4;
  487. } else {
  488. if (GNPTXSTS_NP_TXQ_SPC_AVAIL_GET(gnptxsts) == 0) {
  489. dev_dbg(hsotg->dev,
  490. "%s: no queue slots available (0x%08x)\n",
  491. __func__, gnptxsts);
  492. dwc2_hsotg_en_gsint(hsotg, GINTSTS_NPTXFEMP);
  493. return -ENOSPC;
  494. }
  495. can_write = GNPTXSTS_NP_TXF_SPC_AVAIL_GET(gnptxsts);
  496. can_write *= 4; /* fifo size is in 32bit quantities. */
  497. }
  498. max_transfer = hs_ep->ep.maxpacket * hs_ep->mc;
  499. dev_dbg(hsotg->dev, "%s: GNPTXSTS=%08x, can=%d, to=%d, max_transfer %d\n",
  500. __func__, gnptxsts, can_write, to_write, max_transfer);
  501. /*
  502. * limit to 512 bytes of data, it seems at least on the non-periodic
  503. * FIFO, requests of >512 cause the endpoint to get stuck with a
  504. * fragment of the end of the transfer in it.
  505. */
  506. if (can_write > 512 && !periodic)
  507. can_write = 512;
  508. /*
  509. * limit the write to one max-packet size worth of data, but allow
  510. * the transfer to return that it did not run out of fifo space
  511. * doing it.
  512. */
  513. if (to_write > max_transfer) {
  514. to_write = max_transfer;
  515. /* it's needed only when we do not use dedicated fifos */
  516. if (!hsotg->dedicated_fifos)
  517. dwc2_hsotg_en_gsint(hsotg,
  518. periodic ? GINTSTS_PTXFEMP :
  519. GINTSTS_NPTXFEMP);
  520. }
  521. /* see if we can write data */
  522. if (to_write > can_write) {
  523. to_write = can_write;
  524. pkt_round = to_write % max_transfer;
  525. /*
  526. * Round the write down to an
  527. * exact number of packets.
  528. *
  529. * Note, we do not currently check to see if we can ever
  530. * write a full packet or not to the FIFO.
  531. */
  532. if (pkt_round)
  533. to_write -= pkt_round;
  534. /*
  535. * enable correct FIFO interrupt to alert us when there
  536. * is more room left.
  537. */
  538. /* it's needed only when we do not use dedicated fifos */
  539. if (!hsotg->dedicated_fifos)
  540. dwc2_hsotg_en_gsint(hsotg,
  541. periodic ? GINTSTS_PTXFEMP :
  542. GINTSTS_NPTXFEMP);
  543. }
  544. dev_dbg(hsotg->dev, "write %d/%d, can_write %d, done %d\n",
  545. to_write, hs_req->req.length, can_write, buf_pos);
  546. if (to_write <= 0)
  547. return -ENOSPC;
  548. hs_req->req.actual = buf_pos + to_write;
  549. hs_ep->total_data += to_write;
  550. if (periodic)
  551. hs_ep->fifo_load += to_write;
  552. to_write = DIV_ROUND_UP(to_write, 4);
  553. data = hs_req->req.buf + buf_pos;
  554. dwc2_writel_rep(hsotg, EPFIFO(hs_ep->index), data, to_write);
  555. return (to_write >= can_write) ? -ENOSPC : 0;
  556. }
  557. /**
  558. * get_ep_limit - get the maximum data legnth for this endpoint
  559. * @hs_ep: The endpoint
  560. *
  561. * Return the maximum data that can be queued in one go on a given endpoint
  562. * so that transfers that are too long can be split.
  563. */
  564. static unsigned int get_ep_limit(struct dwc2_hsotg_ep *hs_ep)
  565. {
  566. int index = hs_ep->index;
  567. unsigned int maxsize;
  568. unsigned int maxpkt;
  569. if (index != 0) {
  570. maxsize = DXEPTSIZ_XFERSIZE_LIMIT + 1;
  571. maxpkt = DXEPTSIZ_PKTCNT_LIMIT + 1;
  572. } else {
  573. maxsize = 64 + 64;
  574. if (hs_ep->dir_in)
  575. maxpkt = DIEPTSIZ0_PKTCNT_LIMIT + 1;
  576. else
  577. maxpkt = 2;
  578. }
  579. /* we made the constant loading easier above by using +1 */
  580. maxpkt--;
  581. maxsize--;
  582. /*
  583. * constrain by packet count if maxpkts*pktsize is greater
  584. * than the length register size.
  585. */
  586. if ((maxpkt * hs_ep->ep.maxpacket) < maxsize)
  587. maxsize = maxpkt * hs_ep->ep.maxpacket;
  588. return maxsize;
  589. }
  590. /**
  591. * dwc2_hsotg_read_frameno - read current frame number
  592. * @hsotg: The device instance
  593. *
  594. * Return the current frame number
  595. */
  596. static u32 dwc2_hsotg_read_frameno(struct dwc2_hsotg *hsotg)
  597. {
  598. u32 dsts;
  599. dsts = dwc2_readl(hsotg, DSTS);
  600. dsts &= DSTS_SOFFN_MASK;
  601. dsts >>= DSTS_SOFFN_SHIFT;
  602. return dsts;
  603. }
  604. /**
  605. * dwc2_gadget_get_chain_limit - get the maximum data payload value of the
  606. * DMA descriptor chain prepared for specific endpoint
  607. * @hs_ep: The endpoint
  608. *
  609. * Return the maximum data that can be queued in one go on a given endpoint
  610. * depending on its descriptor chain capacity so that transfers that
  611. * are too long can be split.
  612. */
  613. static unsigned int dwc2_gadget_get_chain_limit(struct dwc2_hsotg_ep *hs_ep)
  614. {
  615. const struct usb_endpoint_descriptor *ep_desc = hs_ep->ep.desc;
  616. int is_isoc = hs_ep->isochronous;
  617. unsigned int maxsize;
  618. u32 mps = hs_ep->ep.maxpacket;
  619. int dir_in = hs_ep->dir_in;
  620. if (is_isoc)
  621. maxsize = (hs_ep->dir_in ? DEV_DMA_ISOC_TX_NBYTES_LIMIT :
  622. DEV_DMA_ISOC_RX_NBYTES_LIMIT) *
  623. MAX_DMA_DESC_NUM_HS_ISOC;
  624. else
  625. maxsize = DEV_DMA_NBYTES_LIMIT * MAX_DMA_DESC_NUM_GENERIC;
  626. /* Interrupt OUT EP with mps not multiple of 4 */
  627. if (hs_ep->index)
  628. if (usb_endpoint_xfer_int(ep_desc) && !dir_in && (mps % 4))
  629. maxsize = mps * MAX_DMA_DESC_NUM_GENERIC;
  630. return maxsize;
  631. }
  632. /*
  633. * dwc2_gadget_get_desc_params - get DMA descriptor parameters.
  634. * @hs_ep: The endpoint
  635. * @mask: RX/TX bytes mask to be defined
  636. *
  637. * Returns maximum data payload for one descriptor after analyzing endpoint
  638. * characteristics.
  639. * DMA descriptor transfer bytes limit depends on EP type:
  640. * Control out - MPS,
  641. * Isochronous - descriptor rx/tx bytes bitfield limit,
  642. * Control In/Bulk/Interrupt - multiple of mps. This will allow to not
  643. * have concatenations from various descriptors within one packet.
  644. * Interrupt OUT - if mps not multiple of 4 then a single packet corresponds
  645. * to a single descriptor.
  646. *
  647. * Selects corresponding mask for RX/TX bytes as well.
  648. */
  649. static u32 dwc2_gadget_get_desc_params(struct dwc2_hsotg_ep *hs_ep, u32 *mask)
  650. {
  651. const struct usb_endpoint_descriptor *ep_desc = hs_ep->ep.desc;
  652. u32 mps = hs_ep->ep.maxpacket;
  653. int dir_in = hs_ep->dir_in;
  654. u32 desc_size = 0;
  655. if (!hs_ep->index && !dir_in) {
  656. desc_size = mps;
  657. *mask = DEV_DMA_NBYTES_MASK;
  658. } else if (hs_ep->isochronous) {
  659. if (dir_in) {
  660. desc_size = DEV_DMA_ISOC_TX_NBYTES_LIMIT;
  661. *mask = DEV_DMA_ISOC_TX_NBYTES_MASK;
  662. } else {
  663. desc_size = DEV_DMA_ISOC_RX_NBYTES_LIMIT;
  664. *mask = DEV_DMA_ISOC_RX_NBYTES_MASK;
  665. }
  666. } else {
  667. desc_size = DEV_DMA_NBYTES_LIMIT;
  668. *mask = DEV_DMA_NBYTES_MASK;
  669. /* Round down desc_size to be mps multiple */
  670. desc_size -= desc_size % mps;
  671. }
  672. /* Interrupt OUT EP with mps not multiple of 4 */
  673. if (hs_ep->index)
  674. if (usb_endpoint_xfer_int(ep_desc) && !dir_in && (mps % 4)) {
  675. desc_size = mps;
  676. *mask = DEV_DMA_NBYTES_MASK;
  677. }
  678. return desc_size;
  679. }
  680. static void dwc2_gadget_fill_nonisoc_xfer_ddma_one(struct dwc2_hsotg_ep *hs_ep,
  681. struct dwc2_dma_desc **desc,
  682. dma_addr_t dma_buff,
  683. unsigned int len,
  684. bool true_last)
  685. {
  686. int dir_in = hs_ep->dir_in;
  687. u32 mps = hs_ep->ep.maxpacket;
  688. u32 maxsize = 0;
  689. u32 offset = 0;
  690. u32 mask = 0;
  691. int i;
  692. maxsize = dwc2_gadget_get_desc_params(hs_ep, &mask);
  693. hs_ep->desc_count = (len / maxsize) +
  694. ((len % maxsize) ? 1 : 0);
  695. if (len == 0)
  696. hs_ep->desc_count = 1;
  697. for (i = 0; i < hs_ep->desc_count; ++i) {
  698. (*desc)->status = 0;
  699. (*desc)->status |= (DEV_DMA_BUFF_STS_HBUSY
  700. << DEV_DMA_BUFF_STS_SHIFT);
  701. if (len > maxsize) {
  702. if (!hs_ep->index && !dir_in)
  703. (*desc)->status |= (DEV_DMA_L | DEV_DMA_IOC);
  704. (*desc)->status |=
  705. maxsize << DEV_DMA_NBYTES_SHIFT & mask;
  706. (*desc)->buf = dma_buff + offset;
  707. len -= maxsize;
  708. offset += maxsize;
  709. } else {
  710. if (true_last)
  711. (*desc)->status |= (DEV_DMA_L | DEV_DMA_IOC);
  712. if (dir_in)
  713. (*desc)->status |= (len % mps) ? DEV_DMA_SHORT :
  714. ((hs_ep->send_zlp && true_last) ?
  715. DEV_DMA_SHORT : 0);
  716. (*desc)->status |=
  717. len << DEV_DMA_NBYTES_SHIFT & mask;
  718. (*desc)->buf = dma_buff + offset;
  719. }
  720. (*desc)->status &= ~DEV_DMA_BUFF_STS_MASK;
  721. (*desc)->status |= (DEV_DMA_BUFF_STS_HREADY
  722. << DEV_DMA_BUFF_STS_SHIFT);
  723. (*desc)++;
  724. }
  725. }
  726. /*
  727. * dwc2_gadget_config_nonisoc_xfer_ddma - prepare non ISOC DMA desc chain.
  728. * @hs_ep: The endpoint
  729. * @ureq: Request to transfer
  730. * @offset: offset in bytes
  731. * @len: Length of the transfer
  732. *
  733. * This function will iterate over descriptor chain and fill its entries
  734. * with corresponding information based on transfer data.
  735. */
  736. static void dwc2_gadget_config_nonisoc_xfer_ddma(struct dwc2_hsotg_ep *hs_ep,
  737. dma_addr_t dma_buff,
  738. unsigned int len)
  739. {
  740. struct usb_request *ureq = NULL;
  741. struct dwc2_dma_desc *desc = hs_ep->desc_list;
  742. struct scatterlist *sg;
  743. int i;
  744. u8 desc_count = 0;
  745. if (hs_ep->req)
  746. ureq = &hs_ep->req->req;
  747. /* non-DMA sg buffer */
  748. if (!ureq || !ureq->num_sgs) {
  749. dwc2_gadget_fill_nonisoc_xfer_ddma_one(hs_ep, &desc,
  750. dma_buff, len, true);
  751. return;
  752. }
  753. /* DMA sg buffer */
  754. for_each_sg(ureq->sg, sg, ureq->num_mapped_sgs, i) {
  755. dwc2_gadget_fill_nonisoc_xfer_ddma_one(hs_ep, &desc,
  756. sg_dma_address(sg) + sg->offset, sg_dma_len(sg),
  757. (i == (ureq->num_mapped_sgs - 1)));
  758. desc_count += hs_ep->desc_count;
  759. }
  760. hs_ep->desc_count = desc_count;
  761. }
  762. /*
  763. * dwc2_gadget_fill_isoc_desc - fills next isochronous descriptor in chain.
  764. * @hs_ep: The isochronous endpoint.
  765. * @dma_buff: usb requests dma buffer.
  766. * @len: usb request transfer length.
  767. *
  768. * Fills next free descriptor with the data of the arrived usb request,
  769. * frame info, sets Last and IOC bits increments next_desc. If filled
  770. * descriptor is not the first one, removes L bit from the previous descriptor
  771. * status.
  772. */
  773. static int dwc2_gadget_fill_isoc_desc(struct dwc2_hsotg_ep *hs_ep,
  774. dma_addr_t dma_buff, unsigned int len)
  775. {
  776. struct dwc2_dma_desc *desc;
  777. struct dwc2_hsotg *hsotg = hs_ep->parent;
  778. u32 index;
  779. u32 mask = 0;
  780. u8 pid = 0;
  781. dwc2_gadget_get_desc_params(hs_ep, &mask);
  782. index = hs_ep->next_desc;
  783. desc = &hs_ep->desc_list[index];
  784. /* Check if descriptor chain full */
  785. if ((desc->status >> DEV_DMA_BUFF_STS_SHIFT) ==
  786. DEV_DMA_BUFF_STS_HREADY) {
  787. dev_dbg(hsotg->dev, "%s: desc chain full\n", __func__);
  788. return 1;
  789. }
  790. /* Clear L bit of previous desc if more than one entries in the chain */
  791. if (hs_ep->next_desc)
  792. hs_ep->desc_list[index - 1].status &= ~DEV_DMA_L;
  793. dev_dbg(hsotg->dev, "%s: Filling ep %d, dir %s isoc desc # %d\n",
  794. __func__, hs_ep->index, hs_ep->dir_in ? "in" : "out", index);
  795. desc->status = 0;
  796. desc->status |= (DEV_DMA_BUFF_STS_HBUSY << DEV_DMA_BUFF_STS_SHIFT);
  797. desc->buf = dma_buff;
  798. desc->status |= (DEV_DMA_L | DEV_DMA_IOC |
  799. ((len << DEV_DMA_NBYTES_SHIFT) & mask));
  800. if (hs_ep->dir_in) {
  801. if (len)
  802. pid = DIV_ROUND_UP(len, hs_ep->ep.maxpacket);
  803. else
  804. pid = 1;
  805. desc->status |= ((pid << DEV_DMA_ISOC_PID_SHIFT) &
  806. DEV_DMA_ISOC_PID_MASK) |
  807. ((len % hs_ep->ep.maxpacket) ?
  808. DEV_DMA_SHORT : 0) |
  809. ((hs_ep->target_frame <<
  810. DEV_DMA_ISOC_FRNUM_SHIFT) &
  811. DEV_DMA_ISOC_FRNUM_MASK);
  812. }
  813. desc->status &= ~DEV_DMA_BUFF_STS_MASK;
  814. desc->status |= (DEV_DMA_BUFF_STS_HREADY << DEV_DMA_BUFF_STS_SHIFT);
  815. /* Increment frame number by interval for IN */
  816. if (hs_ep->dir_in)
  817. dwc2_gadget_incr_frame_num(hs_ep);
  818. /* Update index of last configured entry in the chain */
  819. hs_ep->next_desc++;
  820. if (hs_ep->next_desc >= MAX_DMA_DESC_NUM_HS_ISOC)
  821. hs_ep->next_desc = 0;
  822. return 0;
  823. }
  824. /*
  825. * dwc2_gadget_start_isoc_ddma - start isochronous transfer in DDMA
  826. * @hs_ep: The isochronous endpoint.
  827. *
  828. * Prepare descriptor chain for isochronous endpoints. Afterwards
  829. * write DMA address to HW and enable the endpoint.
  830. */
  831. static void dwc2_gadget_start_isoc_ddma(struct dwc2_hsotg_ep *hs_ep)
  832. {
  833. struct dwc2_hsotg *hsotg = hs_ep->parent;
  834. struct dwc2_hsotg_req *hs_req, *treq;
  835. int index = hs_ep->index;
  836. int ret;
  837. int i;
  838. u32 dma_reg;
  839. u32 depctl;
  840. u32 ctrl;
  841. struct dwc2_dma_desc *desc;
  842. if (list_empty(&hs_ep->queue)) {
  843. hs_ep->target_frame = TARGET_FRAME_INITIAL;
  844. dev_dbg(hsotg->dev, "%s: No requests in queue\n", __func__);
  845. return;
  846. }
  847. /* Initialize descriptor chain by Host Busy status */
  848. for (i = 0; i < MAX_DMA_DESC_NUM_HS_ISOC; i++) {
  849. desc = &hs_ep->desc_list[i];
  850. desc->status = 0;
  851. desc->status |= (DEV_DMA_BUFF_STS_HBUSY
  852. << DEV_DMA_BUFF_STS_SHIFT);
  853. }
  854. hs_ep->next_desc = 0;
  855. list_for_each_entry_safe(hs_req, treq, &hs_ep->queue, queue) {
  856. dma_addr_t dma_addr = hs_req->req.dma;
  857. if (hs_req->req.num_sgs) {
  858. WARN_ON(hs_req->req.num_sgs > 1);
  859. dma_addr = sg_dma_address(hs_req->req.sg);
  860. }
  861. ret = dwc2_gadget_fill_isoc_desc(hs_ep, dma_addr,
  862. hs_req->req.length);
  863. if (ret)
  864. break;
  865. }
  866. hs_ep->compl_desc = 0;
  867. depctl = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index);
  868. dma_reg = hs_ep->dir_in ? DIEPDMA(index) : DOEPDMA(index);
  869. /* write descriptor chain address to control register */
  870. dwc2_writel(hsotg, hs_ep->desc_list_dma, dma_reg);
  871. ctrl = dwc2_readl(hsotg, depctl);
  872. ctrl |= DXEPCTL_EPENA | DXEPCTL_CNAK;
  873. dwc2_writel(hsotg, ctrl, depctl);
  874. }
  875. static bool dwc2_gadget_target_frame_elapsed(struct dwc2_hsotg_ep *hs_ep);
  876. static void dwc2_hsotg_complete_request(struct dwc2_hsotg *hsotg,
  877. struct dwc2_hsotg_ep *hs_ep,
  878. struct dwc2_hsotg_req *hs_req,
  879. int result);
  880. /**
  881. * dwc2_hsotg_start_req - start a USB request from an endpoint's queue
  882. * @hsotg: The controller state.
  883. * @hs_ep: The endpoint to process a request for
  884. * @hs_req: The request to start.
  885. * @continuing: True if we are doing more for the current request.
  886. *
  887. * Start the given request running by setting the endpoint registers
  888. * appropriately, and writing any data to the FIFOs.
  889. */
  890. static void dwc2_hsotg_start_req(struct dwc2_hsotg *hsotg,
  891. struct dwc2_hsotg_ep *hs_ep,
  892. struct dwc2_hsotg_req *hs_req,
  893. bool continuing)
  894. {
  895. struct usb_request *ureq = &hs_req->req;
  896. int index = hs_ep->index;
  897. int dir_in = hs_ep->dir_in;
  898. u32 epctrl_reg;
  899. u32 epsize_reg;
  900. u32 epsize;
  901. u32 ctrl;
  902. unsigned int length;
  903. unsigned int packets;
  904. unsigned int maxreq;
  905. unsigned int dma_reg;
  906. if (index != 0) {
  907. if (hs_ep->req && !continuing) {
  908. dev_err(hsotg->dev, "%s: active request\n", __func__);
  909. WARN_ON(1);
  910. return;
  911. } else if (hs_ep->req != hs_req && continuing) {
  912. dev_err(hsotg->dev,
  913. "%s: continue different req\n", __func__);
  914. WARN_ON(1);
  915. return;
  916. }
  917. }
  918. dma_reg = dir_in ? DIEPDMA(index) : DOEPDMA(index);
  919. epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
  920. epsize_reg = dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
  921. dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x, ep %d, dir %s\n",
  922. __func__, dwc2_readl(hsotg, epctrl_reg), index,
  923. hs_ep->dir_in ? "in" : "out");
  924. /* If endpoint is stalled, we will restart request later */
  925. ctrl = dwc2_readl(hsotg, epctrl_reg);
  926. if (index && ctrl & DXEPCTL_STALL) {
  927. dev_warn(hsotg->dev, "%s: ep%d is stalled\n", __func__, index);
  928. return;
  929. }
  930. length = ureq->length - ureq->actual;
  931. dev_dbg(hsotg->dev, "ureq->length:%d ureq->actual:%d\n",
  932. ureq->length, ureq->actual);
  933. if (!using_desc_dma(hsotg))
  934. maxreq = get_ep_limit(hs_ep);
  935. else
  936. maxreq = dwc2_gadget_get_chain_limit(hs_ep);
  937. if (length > maxreq) {
  938. int round = maxreq % hs_ep->ep.maxpacket;
  939. dev_dbg(hsotg->dev, "%s: length %d, max-req %d, r %d\n",
  940. __func__, length, maxreq, round);
  941. /* round down to multiple of packets */
  942. if (round)
  943. maxreq -= round;
  944. length = maxreq;
  945. }
  946. if (length)
  947. packets = DIV_ROUND_UP(length, hs_ep->ep.maxpacket);
  948. else
  949. packets = 1; /* send one packet if length is zero. */
  950. if (dir_in && index != 0)
  951. if (hs_ep->isochronous)
  952. epsize = DXEPTSIZ_MC(packets);
  953. else
  954. epsize = DXEPTSIZ_MC(1);
  955. else
  956. epsize = 0;
  957. /*
  958. * zero length packet should be programmed on its own and should not
  959. * be counted in DIEPTSIZ.PktCnt with other packets.
  960. */
  961. if (dir_in && ureq->zero && !continuing) {
  962. /* Test if zlp is actually required. */
  963. if ((ureq->length >= hs_ep->ep.maxpacket) &&
  964. !(ureq->length % hs_ep->ep.maxpacket))
  965. hs_ep->send_zlp = 1;
  966. }
  967. epsize |= DXEPTSIZ_PKTCNT(packets);
  968. epsize |= DXEPTSIZ_XFERSIZE(length);
  969. dev_dbg(hsotg->dev, "%s: %d@%d/%d, 0x%08x => 0x%08x\n",
  970. __func__, packets, length, ureq->length, epsize, epsize_reg);
  971. /* store the request as the current one we're doing */
  972. hs_ep->req = hs_req;
  973. if (using_desc_dma(hsotg)) {
  974. u32 offset = 0;
  975. u32 mps = hs_ep->ep.maxpacket;
  976. /* Adjust length: EP0 - MPS, other OUT EPs - multiple of MPS */
  977. if (!dir_in) {
  978. if (!index)
  979. length = mps;
  980. else if (length % mps)
  981. length += (mps - (length % mps));
  982. }
  983. if (continuing)
  984. offset = ureq->actual;
  985. /* Fill DDMA chain entries */
  986. dwc2_gadget_config_nonisoc_xfer_ddma(hs_ep, ureq->dma + offset,
  987. length);
  988. /* write descriptor chain address to control register */
  989. dwc2_writel(hsotg, hs_ep->desc_list_dma, dma_reg);
  990. dev_dbg(hsotg->dev, "%s: %08x pad => 0x%08x\n",
  991. __func__, (u32)hs_ep->desc_list_dma, dma_reg);
  992. } else {
  993. /* write size / packets */
  994. dwc2_writel(hsotg, epsize, epsize_reg);
  995. if (using_dma(hsotg) && !continuing && (length != 0)) {
  996. /*
  997. * write DMA address to control register, buffer
  998. * already synced by dwc2_hsotg_ep_queue().
  999. */
  1000. dwc2_writel(hsotg, ureq->dma, dma_reg);
  1001. dev_dbg(hsotg->dev, "%s: %pad => 0x%08x\n",
  1002. __func__, &ureq->dma, dma_reg);
  1003. }
  1004. }
  1005. if (hs_ep->isochronous) {
  1006. if (!dwc2_gadget_target_frame_elapsed(hs_ep)) {
  1007. if (hs_ep->interval == 1) {
  1008. if (hs_ep->target_frame & 0x1)
  1009. ctrl |= DXEPCTL_SETODDFR;
  1010. else
  1011. ctrl |= DXEPCTL_SETEVENFR;
  1012. }
  1013. ctrl |= DXEPCTL_CNAK;
  1014. } else {
  1015. hs_req->req.frame_number = hs_ep->target_frame;
  1016. hs_req->req.actual = 0;
  1017. dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, -ENODATA);
  1018. return;
  1019. }
  1020. }
  1021. ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */
  1022. dev_dbg(hsotg->dev, "ep0 state:%d\n", hsotg->ep0_state);
  1023. /* For Setup request do not clear NAK */
  1024. if (!(index == 0 && hsotg->ep0_state == DWC2_EP0_SETUP))
  1025. ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */
  1026. dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
  1027. dwc2_writel(hsotg, ctrl, epctrl_reg);
  1028. /*
  1029. * set these, it seems that DMA support increments past the end
  1030. * of the packet buffer so we need to calculate the length from
  1031. * this information.
  1032. */
  1033. hs_ep->size_loaded = length;
  1034. hs_ep->last_load = ureq->actual;
  1035. if (dir_in && !using_dma(hsotg)) {
  1036. /* set these anyway, we may need them for non-periodic in */
  1037. hs_ep->fifo_load = 0;
  1038. dwc2_hsotg_write_fifo(hsotg, hs_ep, hs_req);
  1039. }
  1040. /*
  1041. * Note, trying to clear the NAK here causes problems with transmit
  1042. * on the S3C6400 ending up with the TXFIFO becoming full.
  1043. */
  1044. /* check ep is enabled */
  1045. if (!(dwc2_readl(hsotg, epctrl_reg) & DXEPCTL_EPENA))
  1046. dev_dbg(hsotg->dev,
  1047. "ep%d: failed to become enabled (DXEPCTL=0x%08x)?\n",
  1048. index, dwc2_readl(hsotg, epctrl_reg));
  1049. dev_dbg(hsotg->dev, "%s: DXEPCTL=0x%08x\n",
  1050. __func__, dwc2_readl(hsotg, epctrl_reg));
  1051. /* enable ep interrupts */
  1052. dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 1);
  1053. }
  1054. /**
  1055. * dwc2_hsotg_map_dma - map the DMA memory being used for the request
  1056. * @hsotg: The device state.
  1057. * @hs_ep: The endpoint the request is on.
  1058. * @req: The request being processed.
  1059. *
  1060. * We've been asked to queue a request, so ensure that the memory buffer
  1061. * is correctly setup for DMA. If we've been passed an extant DMA address
  1062. * then ensure the buffer has been synced to memory. If our buffer has no
  1063. * DMA memory, then we map the memory and mark our request to allow us to
  1064. * cleanup on completion.
  1065. */
  1066. static int dwc2_hsotg_map_dma(struct dwc2_hsotg *hsotg,
  1067. struct dwc2_hsotg_ep *hs_ep,
  1068. struct usb_request *req)
  1069. {
  1070. int ret;
  1071. hs_ep->map_dir = hs_ep->dir_in;
  1072. ret = usb_gadget_map_request(&hsotg->gadget, req, hs_ep->dir_in);
  1073. if (ret)
  1074. goto dma_error;
  1075. return 0;
  1076. dma_error:
  1077. dev_err(hsotg->dev, "%s: failed to map buffer %p, %d bytes\n",
  1078. __func__, req->buf, req->length);
  1079. return -EIO;
  1080. }
  1081. static int dwc2_hsotg_handle_unaligned_buf_start(struct dwc2_hsotg *hsotg,
  1082. struct dwc2_hsotg_ep *hs_ep,
  1083. struct dwc2_hsotg_req *hs_req)
  1084. {
  1085. void *req_buf = hs_req->req.buf;
  1086. /* If dma is not being used or buffer is aligned */
  1087. if (!using_dma(hsotg) || !((long)req_buf & 3))
  1088. return 0;
  1089. WARN_ON(hs_req->saved_req_buf);
  1090. dev_dbg(hsotg->dev, "%s: %s: buf=%p length=%d\n", __func__,
  1091. hs_ep->ep.name, req_buf, hs_req->req.length);
  1092. hs_req->req.buf = kmalloc(hs_req->req.length, GFP_ATOMIC);
  1093. if (!hs_req->req.buf) {
  1094. hs_req->req.buf = req_buf;
  1095. dev_err(hsotg->dev,
  1096. "%s: unable to allocate memory for bounce buffer\n",
  1097. __func__);
  1098. return -ENOMEM;
  1099. }
  1100. /* Save actual buffer */
  1101. hs_req->saved_req_buf = req_buf;
  1102. if (hs_ep->dir_in)
  1103. memcpy(hs_req->req.buf, req_buf, hs_req->req.length);
  1104. return 0;
  1105. }
  1106. static void
  1107. dwc2_hsotg_handle_unaligned_buf_complete(struct dwc2_hsotg *hsotg,
  1108. struct dwc2_hsotg_ep *hs_ep,
  1109. struct dwc2_hsotg_req *hs_req)
  1110. {
  1111. /* If dma is not being used or buffer was aligned */
  1112. if (!using_dma(hsotg) || !hs_req->saved_req_buf)
  1113. return;
  1114. dev_dbg(hsotg->dev, "%s: %s: status=%d actual-length=%d\n", __func__,
  1115. hs_ep->ep.name, hs_req->req.status, hs_req->req.actual);
  1116. /* Copy data from bounce buffer on successful out transfer */
  1117. if (!hs_ep->dir_in && !hs_req->req.status)
  1118. memcpy(hs_req->saved_req_buf, hs_req->req.buf,
  1119. hs_req->req.actual);
  1120. /* Free bounce buffer */
  1121. kfree(hs_req->req.buf);
  1122. hs_req->req.buf = hs_req->saved_req_buf;
  1123. hs_req->saved_req_buf = NULL;
  1124. }
  1125. /**
  1126. * dwc2_gadget_target_frame_elapsed - Checks target frame
  1127. * @hs_ep: The driver endpoint to check
  1128. *
  1129. * Returns 1 if targeted frame elapsed. If returned 1 then we need to drop
  1130. * corresponding transfer.
  1131. */
  1132. static bool dwc2_gadget_target_frame_elapsed(struct dwc2_hsotg_ep *hs_ep)
  1133. {
  1134. struct dwc2_hsotg *hsotg = hs_ep->parent;
  1135. u32 target_frame = hs_ep->target_frame;
  1136. u32 current_frame = hsotg->frame_number;
  1137. bool frame_overrun = hs_ep->frame_overrun;
  1138. u16 limit = DSTS_SOFFN_LIMIT;
  1139. if (hsotg->gadget.speed != USB_SPEED_HIGH)
  1140. limit >>= 3;
  1141. if (!frame_overrun && current_frame >= target_frame)
  1142. return true;
  1143. if (frame_overrun && current_frame >= target_frame &&
  1144. ((current_frame - target_frame) < limit / 2))
  1145. return true;
  1146. return false;
  1147. }
  1148. /*
  1149. * dwc2_gadget_set_ep0_desc_chain - Set EP's desc chain pointers
  1150. * @hsotg: The driver state
  1151. * @hs_ep: the ep descriptor chain is for
  1152. *
  1153. * Called to update EP0 structure's pointers depend on stage of
  1154. * control transfer.
  1155. */
  1156. static int dwc2_gadget_set_ep0_desc_chain(struct dwc2_hsotg *hsotg,
  1157. struct dwc2_hsotg_ep *hs_ep)
  1158. {
  1159. switch (hsotg->ep0_state) {
  1160. case DWC2_EP0_SETUP:
  1161. case DWC2_EP0_STATUS_OUT:
  1162. hs_ep->desc_list = hsotg->setup_desc[0];
  1163. hs_ep->desc_list_dma = hsotg->setup_desc_dma[0];
  1164. break;
  1165. case DWC2_EP0_DATA_IN:
  1166. case DWC2_EP0_STATUS_IN:
  1167. hs_ep->desc_list = hsotg->ctrl_in_desc;
  1168. hs_ep->desc_list_dma = hsotg->ctrl_in_desc_dma;
  1169. break;
  1170. case DWC2_EP0_DATA_OUT:
  1171. hs_ep->desc_list = hsotg->ctrl_out_desc;
  1172. hs_ep->desc_list_dma = hsotg->ctrl_out_desc_dma;
  1173. break;
  1174. default:
  1175. dev_err(hsotg->dev, "invalid EP 0 state in queue %d\n",
  1176. hsotg->ep0_state);
  1177. return -EINVAL;
  1178. }
  1179. return 0;
  1180. }
  1181. static int dwc2_hsotg_ep_queue(struct usb_ep *ep, struct usb_request *req,
  1182. gfp_t gfp_flags)
  1183. {
  1184. struct dwc2_hsotg_req *hs_req = our_req(req);
  1185. struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
  1186. struct dwc2_hsotg *hs = hs_ep->parent;
  1187. bool first;
  1188. int ret;
  1189. u32 maxsize = 0;
  1190. u32 mask = 0;
  1191. dev_dbg(hs->dev, "%s: req %p: %d@%p, noi=%d, zero=%d, snok=%d\n",
  1192. ep->name, req, req->length, req->buf, req->no_interrupt,
  1193. req->zero, req->short_not_ok);
  1194. if (hs->lx_state == DWC2_L1) {
  1195. dwc2_wakeup_from_lpm_l1(hs, true);
  1196. }
  1197. /* Prevent new request submission when controller is suspended */
  1198. if (hs->lx_state != DWC2_L0) {
  1199. dev_dbg(hs->dev, "%s: submit request only in active state\n",
  1200. __func__);
  1201. return -EAGAIN;
  1202. }
  1203. /* initialise status of the request */
  1204. INIT_LIST_HEAD(&hs_req->queue);
  1205. req->actual = 0;
  1206. req->status = -EINPROGRESS;
  1207. /* Don't queue ISOC request if length greater than mps*mc */
  1208. if (hs_ep->isochronous &&
  1209. req->length > (hs_ep->mc * hs_ep->ep.maxpacket)) {
  1210. dev_err(hs->dev, "req length > maxpacket*mc\n");
  1211. return -EINVAL;
  1212. }
  1213. /* In DDMA mode for ISOC's don't queue request if length greater
  1214. * than descriptor limits.
  1215. */
  1216. if (using_desc_dma(hs) && hs_ep->isochronous) {
  1217. maxsize = dwc2_gadget_get_desc_params(hs_ep, &mask);
  1218. if (hs_ep->dir_in && req->length > maxsize) {
  1219. dev_err(hs->dev, "wrong length %d (maxsize=%d)\n",
  1220. req->length, maxsize);
  1221. return -EINVAL;
  1222. }
  1223. if (!hs_ep->dir_in && req->length > hs_ep->ep.maxpacket) {
  1224. dev_err(hs->dev, "ISOC OUT: wrong length %d (mps=%d)\n",
  1225. req->length, hs_ep->ep.maxpacket);
  1226. return -EINVAL;
  1227. }
  1228. }
  1229. ret = dwc2_hsotg_handle_unaligned_buf_start(hs, hs_ep, hs_req);
  1230. if (ret)
  1231. return ret;
  1232. /* if we're using DMA, sync the buffers as necessary */
  1233. if (using_dma(hs)) {
  1234. ret = dwc2_hsotg_map_dma(hs, hs_ep, req);
  1235. if (ret)
  1236. return ret;
  1237. }
  1238. /* If using descriptor DMA configure EP0 descriptor chain pointers */
  1239. if (using_desc_dma(hs) && !hs_ep->index) {
  1240. ret = dwc2_gadget_set_ep0_desc_chain(hs, hs_ep);
  1241. if (ret)
  1242. return ret;
  1243. }
  1244. first = list_empty(&hs_ep->queue);
  1245. list_add_tail(&hs_req->queue, &hs_ep->queue);
  1246. /*
  1247. * Handle DDMA isochronous transfers separately - just add new entry
  1248. * to the descriptor chain.
  1249. * Transfer will be started once SW gets either one of NAK or
  1250. * OutTknEpDis interrupts.
  1251. */
  1252. if (using_desc_dma(hs) && hs_ep->isochronous) {
  1253. if (hs_ep->target_frame != TARGET_FRAME_INITIAL) {
  1254. dma_addr_t dma_addr = hs_req->req.dma;
  1255. if (hs_req->req.num_sgs) {
  1256. WARN_ON(hs_req->req.num_sgs > 1);
  1257. dma_addr = sg_dma_address(hs_req->req.sg);
  1258. }
  1259. dwc2_gadget_fill_isoc_desc(hs_ep, dma_addr,
  1260. hs_req->req.length);
  1261. }
  1262. return 0;
  1263. }
  1264. /* Change EP direction if status phase request is after data out */
  1265. if (!hs_ep->index && !req->length && !hs_ep->dir_in &&
  1266. hs->ep0_state == DWC2_EP0_DATA_OUT)
  1267. hs_ep->dir_in = 1;
  1268. if (first) {
  1269. if (!hs_ep->isochronous) {
  1270. dwc2_hsotg_start_req(hs, hs_ep, hs_req, false);
  1271. return 0;
  1272. }
  1273. /* Update current frame number value. */
  1274. hs->frame_number = dwc2_hsotg_read_frameno(hs);
  1275. while (dwc2_gadget_target_frame_elapsed(hs_ep)) {
  1276. dwc2_gadget_incr_frame_num(hs_ep);
  1277. /* Update current frame number value once more as it
  1278. * changes here.
  1279. */
  1280. hs->frame_number = dwc2_hsotg_read_frameno(hs);
  1281. }
  1282. if (hs_ep->target_frame != TARGET_FRAME_INITIAL)
  1283. dwc2_hsotg_start_req(hs, hs_ep, hs_req, false);
  1284. }
  1285. return 0;
  1286. }
  1287. static int dwc2_hsotg_ep_queue_lock(struct usb_ep *ep, struct usb_request *req,
  1288. gfp_t gfp_flags)
  1289. {
  1290. struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
  1291. struct dwc2_hsotg *hs = hs_ep->parent;
  1292. unsigned long flags;
  1293. int ret;
  1294. spin_lock_irqsave(&hs->lock, flags);
  1295. ret = dwc2_hsotg_ep_queue(ep, req, gfp_flags);
  1296. spin_unlock_irqrestore(&hs->lock, flags);
  1297. return ret;
  1298. }
  1299. static void dwc2_hsotg_ep_free_request(struct usb_ep *ep,
  1300. struct usb_request *req)
  1301. {
  1302. struct dwc2_hsotg_req *hs_req = our_req(req);
  1303. kfree(hs_req);
  1304. }
  1305. /**
  1306. * dwc2_hsotg_complete_oursetup - setup completion callback
  1307. * @ep: The endpoint the request was on.
  1308. * @req: The request completed.
  1309. *
  1310. * Called on completion of any requests the driver itself
  1311. * submitted that need cleaning up.
  1312. */
  1313. static void dwc2_hsotg_complete_oursetup(struct usb_ep *ep,
  1314. struct usb_request *req)
  1315. {
  1316. struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
  1317. struct dwc2_hsotg *hsotg = hs_ep->parent;
  1318. dev_dbg(hsotg->dev, "%s: ep %p, req %p\n", __func__, ep, req);
  1319. dwc2_hsotg_ep_free_request(ep, req);
  1320. }
  1321. /**
  1322. * ep_from_windex - convert control wIndex value to endpoint
  1323. * @hsotg: The driver state.
  1324. * @windex: The control request wIndex field (in host order).
  1325. *
  1326. * Convert the given wIndex into a pointer to an driver endpoint
  1327. * structure, or return NULL if it is not a valid endpoint.
  1328. */
  1329. static struct dwc2_hsotg_ep *ep_from_windex(struct dwc2_hsotg *hsotg,
  1330. u32 windex)
  1331. {
  1332. int dir = (windex & USB_DIR_IN) ? 1 : 0;
  1333. int idx = windex & 0x7F;
  1334. if (windex >= 0x100)
  1335. return NULL;
  1336. if (idx > hsotg->num_of_eps)
  1337. return NULL;
  1338. return index_to_ep(hsotg, idx, dir);
  1339. }
  1340. /**
  1341. * dwc2_hsotg_set_test_mode - Enable usb Test Modes
  1342. * @hsotg: The driver state.
  1343. * @testmode: requested usb test mode
  1344. * Enable usb Test Mode requested by the Host.
  1345. */
  1346. int dwc2_hsotg_set_test_mode(struct dwc2_hsotg *hsotg, int testmode)
  1347. {
  1348. int dctl = dwc2_readl(hsotg, DCTL);
  1349. dctl &= ~DCTL_TSTCTL_MASK;
  1350. switch (testmode) {
  1351. case USB_TEST_J:
  1352. case USB_TEST_K:
  1353. case USB_TEST_SE0_NAK:
  1354. case USB_TEST_PACKET:
  1355. case USB_TEST_FORCE_ENABLE:
  1356. dctl |= testmode << DCTL_TSTCTL_SHIFT;
  1357. break;
  1358. default:
  1359. return -EINVAL;
  1360. }
  1361. dwc2_writel(hsotg, dctl, DCTL);
  1362. return 0;
  1363. }
  1364. /**
  1365. * dwc2_hsotg_send_reply - send reply to control request
  1366. * @hsotg: The device state
  1367. * @ep: Endpoint 0
  1368. * @buff: Buffer for request
  1369. * @length: Length of reply.
  1370. *
  1371. * Create a request and queue it on the given endpoint. This is useful as
  1372. * an internal method of sending replies to certain control requests, etc.
  1373. */
  1374. static int dwc2_hsotg_send_reply(struct dwc2_hsotg *hsotg,
  1375. struct dwc2_hsotg_ep *ep,
  1376. void *buff,
  1377. int length)
  1378. {
  1379. struct usb_request *req;
  1380. int ret;
  1381. dev_dbg(hsotg->dev, "%s: buff %p, len %d\n", __func__, buff, length);
  1382. req = dwc2_hsotg_ep_alloc_request(&ep->ep, GFP_ATOMIC);
  1383. hsotg->ep0_reply = req;
  1384. if (!req) {
  1385. dev_warn(hsotg->dev, "%s: cannot alloc req\n", __func__);
  1386. return -ENOMEM;
  1387. }
  1388. req->buf = hsotg->ep0_buff;
  1389. req->length = length;
  1390. /*
  1391. * zero flag is for sending zlp in DATA IN stage. It has no impact on
  1392. * STATUS stage.
  1393. */
  1394. req->zero = 0;
  1395. req->complete = dwc2_hsotg_complete_oursetup;
  1396. if (length)
  1397. memcpy(req->buf, buff, length);
  1398. ret = dwc2_hsotg_ep_queue(&ep->ep, req, GFP_ATOMIC);
  1399. if (ret) {
  1400. dev_warn(hsotg->dev, "%s: cannot queue req\n", __func__);
  1401. return ret;
  1402. }
  1403. return 0;
  1404. }
  1405. /**
  1406. * dwc2_hsotg_process_req_status - process request GET_STATUS
  1407. * @hsotg: The device state
  1408. * @ctrl: USB control request
  1409. */
  1410. static int dwc2_hsotg_process_req_status(struct dwc2_hsotg *hsotg,
  1411. struct usb_ctrlrequest *ctrl)
  1412. {
  1413. struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
  1414. struct dwc2_hsotg_ep *ep;
  1415. __le16 reply;
  1416. u16 status;
  1417. int ret;
  1418. dev_dbg(hsotg->dev, "%s: USB_REQ_GET_STATUS\n", __func__);
  1419. if (!ep0->dir_in) {
  1420. dev_warn(hsotg->dev, "%s: direction out?\n", __func__);
  1421. return -EINVAL;
  1422. }
  1423. switch (ctrl->bRequestType & USB_RECIP_MASK) {
  1424. case USB_RECIP_DEVICE:
  1425. status = hsotg->gadget.is_selfpowered <<
  1426. USB_DEVICE_SELF_POWERED;
  1427. status |= hsotg->remote_wakeup_allowed <<
  1428. USB_DEVICE_REMOTE_WAKEUP;
  1429. reply = cpu_to_le16(status);
  1430. break;
  1431. case USB_RECIP_INTERFACE:
  1432. /* currently, the data result should be zero */
  1433. reply = cpu_to_le16(0);
  1434. break;
  1435. case USB_RECIP_ENDPOINT:
  1436. ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex));
  1437. if (!ep)
  1438. return -ENOENT;
  1439. reply = cpu_to_le16(ep->halted ? 1 : 0);
  1440. break;
  1441. default:
  1442. return 0;
  1443. }
  1444. if (le16_to_cpu(ctrl->wLength) != 2)
  1445. return -EINVAL;
  1446. ret = dwc2_hsotg_send_reply(hsotg, ep0, &reply, 2);
  1447. if (ret) {
  1448. dev_err(hsotg->dev, "%s: failed to send reply\n", __func__);
  1449. return ret;
  1450. }
  1451. return 1;
  1452. }
  1453. static int dwc2_hsotg_ep_sethalt(struct usb_ep *ep, int value, bool now);
  1454. /**
  1455. * get_ep_head - return the first request on the endpoint
  1456. * @hs_ep: The controller endpoint to get
  1457. *
  1458. * Get the first request on the endpoint.
  1459. */
  1460. static struct dwc2_hsotg_req *get_ep_head(struct dwc2_hsotg_ep *hs_ep)
  1461. {
  1462. return list_first_entry_or_null(&hs_ep->queue, struct dwc2_hsotg_req,
  1463. queue);
  1464. }
  1465. /**
  1466. * dwc2_gadget_start_next_request - Starts next request from ep queue
  1467. * @hs_ep: Endpoint structure
  1468. *
  1469. * If queue is empty and EP is ISOC-OUT - unmasks OUTTKNEPDIS which is masked
  1470. * in its handler. Hence we need to unmask it here to be able to do
  1471. * resynchronization.
  1472. */
  1473. static void dwc2_gadget_start_next_request(struct dwc2_hsotg_ep *hs_ep)
  1474. {
  1475. struct dwc2_hsotg *hsotg = hs_ep->parent;
  1476. int dir_in = hs_ep->dir_in;
  1477. struct dwc2_hsotg_req *hs_req;
  1478. if (!list_empty(&hs_ep->queue)) {
  1479. hs_req = get_ep_head(hs_ep);
  1480. dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, false);
  1481. return;
  1482. }
  1483. if (!hs_ep->isochronous)
  1484. return;
  1485. if (dir_in) {
  1486. dev_dbg(hsotg->dev, "%s: No more ISOC-IN requests\n",
  1487. __func__);
  1488. } else {
  1489. dev_dbg(hsotg->dev, "%s: No more ISOC-OUT requests\n",
  1490. __func__);
  1491. }
  1492. }
  1493. /**
  1494. * dwc2_hsotg_process_req_feature - process request {SET,CLEAR}_FEATURE
  1495. * @hsotg: The device state
  1496. * @ctrl: USB control request
  1497. */
  1498. static int dwc2_hsotg_process_req_feature(struct dwc2_hsotg *hsotg,
  1499. struct usb_ctrlrequest *ctrl)
  1500. {
  1501. struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
  1502. struct dwc2_hsotg_req *hs_req;
  1503. bool set = (ctrl->bRequest == USB_REQ_SET_FEATURE);
  1504. struct dwc2_hsotg_ep *ep;
  1505. int ret;
  1506. bool halted;
  1507. u32 recip;
  1508. u32 wValue;
  1509. u32 wIndex;
  1510. dev_dbg(hsotg->dev, "%s: %s_FEATURE\n",
  1511. __func__, set ? "SET" : "CLEAR");
  1512. wValue = le16_to_cpu(ctrl->wValue);
  1513. wIndex = le16_to_cpu(ctrl->wIndex);
  1514. recip = ctrl->bRequestType & USB_RECIP_MASK;
  1515. switch (recip) {
  1516. case USB_RECIP_DEVICE:
  1517. switch (wValue) {
  1518. case USB_DEVICE_REMOTE_WAKEUP:
  1519. if (set)
  1520. hsotg->remote_wakeup_allowed = 1;
  1521. else
  1522. hsotg->remote_wakeup_allowed = 0;
  1523. break;
  1524. case USB_DEVICE_TEST_MODE:
  1525. if ((wIndex & 0xff) != 0)
  1526. return -EINVAL;
  1527. if (!set)
  1528. return -EINVAL;
  1529. hsotg->test_mode = wIndex >> 8;
  1530. break;
  1531. default:
  1532. return -ENOENT;
  1533. }
  1534. ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
  1535. if (ret) {
  1536. dev_err(hsotg->dev,
  1537. "%s: failed to send reply\n", __func__);
  1538. return ret;
  1539. }
  1540. break;
  1541. case USB_RECIP_ENDPOINT:
  1542. ep = ep_from_windex(hsotg, wIndex);
  1543. if (!ep) {
  1544. dev_dbg(hsotg->dev, "%s: no endpoint for 0x%04x\n",
  1545. __func__, wIndex);
  1546. return -ENOENT;
  1547. }
  1548. switch (wValue) {
  1549. case USB_ENDPOINT_HALT:
  1550. halted = ep->halted;
  1551. if (!ep->wedged)
  1552. dwc2_hsotg_ep_sethalt(&ep->ep, set, true);
  1553. ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
  1554. if (ret) {
  1555. dev_err(hsotg->dev,
  1556. "%s: failed to send reply\n", __func__);
  1557. return ret;
  1558. }
  1559. /*
  1560. * we have to complete all requests for ep if it was
  1561. * halted, and the halt was cleared by CLEAR_FEATURE
  1562. */
  1563. if (!set && halted) {
  1564. /*
  1565. * If we have request in progress,
  1566. * then complete it
  1567. */
  1568. if (ep->req) {
  1569. hs_req = ep->req;
  1570. ep->req = NULL;
  1571. list_del_init(&hs_req->queue);
  1572. if (hs_req->req.complete) {
  1573. spin_unlock(&hsotg->lock);
  1574. usb_gadget_giveback_request(
  1575. &ep->ep, &hs_req->req);
  1576. spin_lock(&hsotg->lock);
  1577. }
  1578. }
  1579. /* If we have pending request, then start it */
  1580. if (!ep->req)
  1581. dwc2_gadget_start_next_request(ep);
  1582. }
  1583. break;
  1584. default:
  1585. return -ENOENT;
  1586. }
  1587. break;
  1588. default:
  1589. return -ENOENT;
  1590. }
  1591. return 1;
  1592. }
  1593. static void dwc2_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg);
  1594. /**
  1595. * dwc2_hsotg_stall_ep0 - stall ep0
  1596. * @hsotg: The device state
  1597. *
  1598. * Set stall for ep0 as response for setup request.
  1599. */
  1600. static void dwc2_hsotg_stall_ep0(struct dwc2_hsotg *hsotg)
  1601. {
  1602. struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
  1603. u32 reg;
  1604. u32 ctrl;
  1605. dev_dbg(hsotg->dev, "ep0 stall (dir=%d)\n", ep0->dir_in);
  1606. reg = (ep0->dir_in) ? DIEPCTL0 : DOEPCTL0;
  1607. /*
  1608. * DxEPCTL_Stall will be cleared by EP once it has
  1609. * taken effect, so no need to clear later.
  1610. */
  1611. ctrl = dwc2_readl(hsotg, reg);
  1612. ctrl |= DXEPCTL_STALL;
  1613. ctrl |= DXEPCTL_CNAK;
  1614. dwc2_writel(hsotg, ctrl, reg);
  1615. dev_dbg(hsotg->dev,
  1616. "written DXEPCTL=0x%08x to %08x (DXEPCTL=0x%08x)\n",
  1617. ctrl, reg, dwc2_readl(hsotg, reg));
  1618. /*
  1619. * complete won't be called, so we enqueue
  1620. * setup request here
  1621. */
  1622. dwc2_hsotg_enqueue_setup(hsotg);
  1623. }
  1624. /**
  1625. * dwc2_hsotg_process_control - process a control request
  1626. * @hsotg: The device state
  1627. * @ctrl: The control request received
  1628. *
  1629. * The controller has received the SETUP phase of a control request, and
  1630. * needs to work out what to do next (and whether to pass it on to the
  1631. * gadget driver).
  1632. */
  1633. static void dwc2_hsotg_process_control(struct dwc2_hsotg *hsotg,
  1634. struct usb_ctrlrequest *ctrl)
  1635. {
  1636. struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
  1637. int ret = 0;
  1638. u32 dcfg;
  1639. dev_dbg(hsotg->dev,
  1640. "ctrl Type=%02x, Req=%02x, V=%04x, I=%04x, L=%04x\n",
  1641. ctrl->bRequestType, ctrl->bRequest, ctrl->wValue,
  1642. ctrl->wIndex, ctrl->wLength);
  1643. if (ctrl->wLength == 0) {
  1644. ep0->dir_in = 1;
  1645. hsotg->ep0_state = DWC2_EP0_STATUS_IN;
  1646. } else if (ctrl->bRequestType & USB_DIR_IN) {
  1647. ep0->dir_in = 1;
  1648. hsotg->ep0_state = DWC2_EP0_DATA_IN;
  1649. } else {
  1650. ep0->dir_in = 0;
  1651. hsotg->ep0_state = DWC2_EP0_DATA_OUT;
  1652. }
  1653. if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
  1654. switch (ctrl->bRequest) {
  1655. case USB_REQ_SET_ADDRESS:
  1656. hsotg->connected = 1;
  1657. dcfg = dwc2_readl(hsotg, DCFG);
  1658. dcfg &= ~DCFG_DEVADDR_MASK;
  1659. dcfg |= (le16_to_cpu(ctrl->wValue) <<
  1660. DCFG_DEVADDR_SHIFT) & DCFG_DEVADDR_MASK;
  1661. dwc2_writel(hsotg, dcfg, DCFG);
  1662. dev_info(hsotg->dev, "new address %d\n", ctrl->wValue);
  1663. ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
  1664. return;
  1665. case USB_REQ_GET_STATUS:
  1666. ret = dwc2_hsotg_process_req_status(hsotg, ctrl);
  1667. break;
  1668. case USB_REQ_CLEAR_FEATURE:
  1669. case USB_REQ_SET_FEATURE:
  1670. ret = dwc2_hsotg_process_req_feature(hsotg, ctrl);
  1671. break;
  1672. }
  1673. }
  1674. /* as a fallback, try delivering it to the driver to deal with */
  1675. if (ret == 0 && hsotg->driver) {
  1676. spin_unlock(&hsotg->lock);
  1677. ret = hsotg->driver->setup(&hsotg->gadget, ctrl);
  1678. spin_lock(&hsotg->lock);
  1679. if (ret < 0)
  1680. dev_dbg(hsotg->dev, "driver->setup() ret %d\n", ret);
  1681. }
  1682. hsotg->delayed_status = false;
  1683. if (ret == USB_GADGET_DELAYED_STATUS)
  1684. hsotg->delayed_status = true;
  1685. /*
  1686. * the request is either unhandlable, or is not formatted correctly
  1687. * so respond with a STALL for the status stage to indicate failure.
  1688. */
  1689. if (ret < 0)
  1690. dwc2_hsotg_stall_ep0(hsotg);
  1691. }
  1692. /**
  1693. * dwc2_hsotg_complete_setup - completion of a setup transfer
  1694. * @ep: The endpoint the request was on.
  1695. * @req: The request completed.
  1696. *
  1697. * Called on completion of any requests the driver itself submitted for
  1698. * EP0 setup packets
  1699. */
  1700. static void dwc2_hsotg_complete_setup(struct usb_ep *ep,
  1701. struct usb_request *req)
  1702. {
  1703. struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
  1704. struct dwc2_hsotg *hsotg = hs_ep->parent;
  1705. if (req->status < 0) {
  1706. dev_dbg(hsotg->dev, "%s: failed %d\n", __func__, req->status);
  1707. return;
  1708. }
  1709. spin_lock(&hsotg->lock);
  1710. if (req->actual == 0)
  1711. dwc2_hsotg_enqueue_setup(hsotg);
  1712. else
  1713. dwc2_hsotg_process_control(hsotg, req->buf);
  1714. spin_unlock(&hsotg->lock);
  1715. }
  1716. /**
  1717. * dwc2_hsotg_enqueue_setup - start a request for EP0 packets
  1718. * @hsotg: The device state.
  1719. *
  1720. * Enqueue a request on EP0 if necessary to received any SETUP packets
  1721. * received from the host.
  1722. */
  1723. static void dwc2_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg)
  1724. {
  1725. struct usb_request *req = hsotg->ctrl_req;
  1726. struct dwc2_hsotg_req *hs_req = our_req(req);
  1727. int ret;
  1728. dev_dbg(hsotg->dev, "%s: queueing setup request\n", __func__);
  1729. req->zero = 0;
  1730. req->length = 8;
  1731. req->buf = hsotg->ctrl_buff;
  1732. req->complete = dwc2_hsotg_complete_setup;
  1733. if (!list_empty(&hs_req->queue)) {
  1734. dev_dbg(hsotg->dev, "%s already queued???\n", __func__);
  1735. return;
  1736. }
  1737. hsotg->eps_out[0]->dir_in = 0;
  1738. hsotg->eps_out[0]->send_zlp = 0;
  1739. hsotg->ep0_state = DWC2_EP0_SETUP;
  1740. ret = dwc2_hsotg_ep_queue(&hsotg->eps_out[0]->ep, req, GFP_ATOMIC);
  1741. if (ret < 0) {
  1742. dev_err(hsotg->dev, "%s: failed queue (%d)\n", __func__, ret);
  1743. /*
  1744. * Don't think there's much we can do other than watch the
  1745. * driver fail.
  1746. */
  1747. }
  1748. }
  1749. static void dwc2_hsotg_program_zlp(struct dwc2_hsotg *hsotg,
  1750. struct dwc2_hsotg_ep *hs_ep)
  1751. {
  1752. u32 ctrl;
  1753. u8 index = hs_ep->index;
  1754. u32 epctl_reg = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index);
  1755. u32 epsiz_reg = hs_ep->dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
  1756. if (hs_ep->dir_in)
  1757. dev_dbg(hsotg->dev, "Sending zero-length packet on ep%d\n",
  1758. index);
  1759. else
  1760. dev_dbg(hsotg->dev, "Receiving zero-length packet on ep%d\n",
  1761. index);
  1762. if (using_desc_dma(hsotg)) {
  1763. /* Not specific buffer needed for ep0 ZLP */
  1764. dma_addr_t dma = hs_ep->desc_list_dma;
  1765. if (!index)
  1766. dwc2_gadget_set_ep0_desc_chain(hsotg, hs_ep);
  1767. dwc2_gadget_config_nonisoc_xfer_ddma(hs_ep, dma, 0);
  1768. } else {
  1769. dwc2_writel(hsotg, DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
  1770. DXEPTSIZ_XFERSIZE(0),
  1771. epsiz_reg);
  1772. }
  1773. ctrl = dwc2_readl(hsotg, epctl_reg);
  1774. ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */
  1775. ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */
  1776. ctrl |= DXEPCTL_USBACTEP;
  1777. dwc2_writel(hsotg, ctrl, epctl_reg);
  1778. }
  1779. /**
  1780. * dwc2_hsotg_complete_request - complete a request given to us
  1781. * @hsotg: The device state.
  1782. * @hs_ep: The endpoint the request was on.
  1783. * @hs_req: The request to complete.
  1784. * @result: The result code (0 => Ok, otherwise errno)
  1785. *
  1786. * The given request has finished, so call the necessary completion
  1787. * if it has one and then look to see if we can start a new request
  1788. * on the endpoint.
  1789. *
  1790. * Note, expects the ep to already be locked as appropriate.
  1791. */
  1792. static void dwc2_hsotg_complete_request(struct dwc2_hsotg *hsotg,
  1793. struct dwc2_hsotg_ep *hs_ep,
  1794. struct dwc2_hsotg_req *hs_req,
  1795. int result)
  1796. {
  1797. if (!hs_req) {
  1798. dev_dbg(hsotg->dev, "%s: nothing to complete?\n", __func__);
  1799. return;
  1800. }
  1801. dev_dbg(hsotg->dev, "complete: ep %p %s, req %p, %d => %p\n",
  1802. hs_ep, hs_ep->ep.name, hs_req, result, hs_req->req.complete);
  1803. /*
  1804. * only replace the status if we've not already set an error
  1805. * from a previous transaction
  1806. */
  1807. if (hs_req->req.status == -EINPROGRESS)
  1808. hs_req->req.status = result;
  1809. if (using_dma(hsotg))
  1810. dwc2_hsotg_unmap_dma(hsotg, hs_ep, hs_req);
  1811. dwc2_hsotg_handle_unaligned_buf_complete(hsotg, hs_ep, hs_req);
  1812. hs_ep->req = NULL;
  1813. list_del_init(&hs_req->queue);
  1814. /*
  1815. * call the complete request with the locks off, just in case the
  1816. * request tries to queue more work for this endpoint.
  1817. */
  1818. if (hs_req->req.complete) {
  1819. spin_unlock(&hsotg->lock);
  1820. usb_gadget_giveback_request(&hs_ep->ep, &hs_req->req);
  1821. spin_lock(&hsotg->lock);
  1822. }
  1823. /* In DDMA don't need to proceed to starting of next ISOC request */
  1824. if (using_desc_dma(hsotg) && hs_ep->isochronous)
  1825. return;
  1826. /*
  1827. * Look to see if there is anything else to do. Note, the completion
  1828. * of the previous request may have caused a new request to be started
  1829. * so be careful when doing this.
  1830. */
  1831. if (!hs_ep->req && result >= 0)
  1832. dwc2_gadget_start_next_request(hs_ep);
  1833. }
  1834. /*
  1835. * dwc2_gadget_complete_isoc_request_ddma - complete an isoc request in DDMA
  1836. * @hs_ep: The endpoint the request was on.
  1837. *
  1838. * Get first request from the ep queue, determine descriptor on which complete
  1839. * happened. SW discovers which descriptor currently in use by HW, adjusts
  1840. * dma_address and calculates index of completed descriptor based on the value
  1841. * of DEPDMA register. Update actual length of request, giveback to gadget.
  1842. */
  1843. static void dwc2_gadget_complete_isoc_request_ddma(struct dwc2_hsotg_ep *hs_ep)
  1844. {
  1845. struct dwc2_hsotg *hsotg = hs_ep->parent;
  1846. struct dwc2_hsotg_req *hs_req;
  1847. struct usb_request *ureq;
  1848. u32 desc_sts;
  1849. u32 mask;
  1850. desc_sts = hs_ep->desc_list[hs_ep->compl_desc].status;
  1851. /* Process only descriptors with buffer status set to DMA done */
  1852. while ((desc_sts & DEV_DMA_BUFF_STS_MASK) >>
  1853. DEV_DMA_BUFF_STS_SHIFT == DEV_DMA_BUFF_STS_DMADONE) {
  1854. hs_req = get_ep_head(hs_ep);
  1855. if (!hs_req) {
  1856. dev_warn(hsotg->dev, "%s: ISOC EP queue empty\n", __func__);
  1857. return;
  1858. }
  1859. ureq = &hs_req->req;
  1860. /* Check completion status */
  1861. if ((desc_sts & DEV_DMA_STS_MASK) >> DEV_DMA_STS_SHIFT ==
  1862. DEV_DMA_STS_SUCC) {
  1863. mask = hs_ep->dir_in ? DEV_DMA_ISOC_TX_NBYTES_MASK :
  1864. DEV_DMA_ISOC_RX_NBYTES_MASK;
  1865. ureq->actual = ureq->length - ((desc_sts & mask) >>
  1866. DEV_DMA_ISOC_NBYTES_SHIFT);
  1867. /* Adjust actual len for ISOC Out if len is
  1868. * not align of 4
  1869. */
  1870. if (!hs_ep->dir_in && ureq->length & 0x3)
  1871. ureq->actual += 4 - (ureq->length & 0x3);
  1872. /* Set actual frame number for completed transfers */
  1873. ureq->frame_number =
  1874. (desc_sts & DEV_DMA_ISOC_FRNUM_MASK) >>
  1875. DEV_DMA_ISOC_FRNUM_SHIFT;
  1876. }
  1877. dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
  1878. hs_ep->compl_desc++;
  1879. if (hs_ep->compl_desc > (MAX_DMA_DESC_NUM_HS_ISOC - 1))
  1880. hs_ep->compl_desc = 0;
  1881. desc_sts = hs_ep->desc_list[hs_ep->compl_desc].status;
  1882. }
  1883. }
  1884. /*
  1885. * dwc2_gadget_handle_isoc_bna - handle BNA interrupt for ISOC.
  1886. * @hs_ep: The isochronous endpoint.
  1887. *
  1888. * If EP ISOC OUT then need to flush RX FIFO to remove source of BNA
  1889. * interrupt. Reset target frame and next_desc to allow to start
  1890. * ISOC's on NAK interrupt for IN direction or on OUTTKNEPDIS
  1891. * interrupt for OUT direction.
  1892. */
  1893. static void dwc2_gadget_handle_isoc_bna(struct dwc2_hsotg_ep *hs_ep)
  1894. {
  1895. struct dwc2_hsotg *hsotg = hs_ep->parent;
  1896. if (!hs_ep->dir_in)
  1897. dwc2_flush_rx_fifo(hsotg);
  1898. dwc2_hsotg_complete_request(hsotg, hs_ep, get_ep_head(hs_ep), 0);
  1899. hs_ep->target_frame = TARGET_FRAME_INITIAL;
  1900. hs_ep->next_desc = 0;
  1901. hs_ep->compl_desc = 0;
  1902. }
  1903. /**
  1904. * dwc2_hsotg_rx_data - receive data from the FIFO for an endpoint
  1905. * @hsotg: The device state.
  1906. * @ep_idx: The endpoint index for the data
  1907. * @size: The size of data in the fifo, in bytes
  1908. *
  1909. * The FIFO status shows there is data to read from the FIFO for a given
  1910. * endpoint, so sort out whether we need to read the data into a request
  1911. * that has been made for that endpoint.
  1912. */
  1913. static void dwc2_hsotg_rx_data(struct dwc2_hsotg *hsotg, int ep_idx, int size)
  1914. {
  1915. struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[ep_idx];
  1916. struct dwc2_hsotg_req *hs_req = NULL;//hs_ep->req;
  1917. int to_read;
  1918. int max_req;
  1919. int read_ptr;
  1920. if (hs_ep == NULL) {
  1921. printk("hs_ep is NULL at %s:%d\n", __func__, __LINE__);
  1922. return;
  1923. }
  1924. hs_req = hs_ep->req;
  1925. if (!hs_req) {
  1926. u32 epctl = dwc2_readl(hsotg, DOEPCTL(ep_idx));
  1927. int ptr;
  1928. dev_dbg(hsotg->dev,
  1929. "%s: FIFO %d bytes on ep%d but no req (DXEPCTl=0x%08x)\n",
  1930. __func__, size, ep_idx, epctl);
  1931. /* dump the data from the FIFO, we've nothing we can do */
  1932. for (ptr = 0; ptr < size; ptr += 4)
  1933. (void)dwc2_readl(hsotg, EPFIFO(ep_idx));
  1934. return;
  1935. }
  1936. to_read = size;
  1937. read_ptr = hs_req->req.actual;
  1938. max_req = hs_req->req.length - read_ptr;
  1939. dev_dbg(hsotg->dev, "%s: read %d/%d, done %d/%d\n",
  1940. __func__, to_read, max_req, read_ptr, hs_req->req.length);
  1941. if (to_read > max_req) {
  1942. /*
  1943. * more data appeared than we where willing
  1944. * to deal with in this request.
  1945. */
  1946. /* currently we don't deal this */
  1947. //WARN_ON_ONCE(1);
  1948. printk("##WARN_ON_ONCE(1);\n");
  1949. //return;
  1950. }
  1951. hs_ep->total_data += to_read;
  1952. hs_req->req.actual += to_read;
  1953. to_read = DIV_ROUND_UP(to_read, 4);
  1954. /*
  1955. * note, we might over-write the buffer end by 3 bytes depending on
  1956. * alignment of the data.
  1957. */
  1958. dwc2_readl_rep(hsotg, EPFIFO(ep_idx),
  1959. hs_req->req.buf + read_ptr, to_read);
  1960. }
  1961. /**
  1962. * dwc2_hsotg_ep0_zlp - send/receive zero-length packet on control endpoint
  1963. * @hsotg: The device instance
  1964. * @dir_in: If IN zlp
  1965. *
  1966. * Generate a zero-length IN packet request for terminating a SETUP
  1967. * transaction.
  1968. *
  1969. * Note, since we don't write any data to the TxFIFO, then it is
  1970. * currently believed that we do not need to wait for any space in
  1971. * the TxFIFO.
  1972. */
  1973. static void dwc2_hsotg_ep0_zlp(struct dwc2_hsotg *hsotg, bool dir_in)
  1974. {
  1975. /* eps_out[0] is used in both directions */
  1976. hsotg->eps_out[0]->dir_in = dir_in;
  1977. hsotg->ep0_state = dir_in ? DWC2_EP0_STATUS_IN : DWC2_EP0_STATUS_OUT;
  1978. dwc2_hsotg_program_zlp(hsotg, hsotg->eps_out[0]);
  1979. }
  1980. /*
  1981. * dwc2_gadget_get_xfersize_ddma - get transferred bytes amount from desc
  1982. * @hs_ep - The endpoint on which transfer went
  1983. *
  1984. * Iterate over endpoints descriptor chain and get info on bytes remained
  1985. * in DMA descriptors after transfer has completed. Used for non isoc EPs.
  1986. */
  1987. static unsigned int dwc2_gadget_get_xfersize_ddma(struct dwc2_hsotg_ep *hs_ep)
  1988. {
  1989. const struct usb_endpoint_descriptor *ep_desc = hs_ep->ep.desc;
  1990. struct dwc2_hsotg *hsotg = hs_ep->parent;
  1991. unsigned int bytes_rem = 0;
  1992. unsigned int bytes_rem_correction = 0;
  1993. struct dwc2_dma_desc *desc = hs_ep->desc_list;
  1994. int i;
  1995. u32 status;
  1996. u32 mps = hs_ep->ep.maxpacket;
  1997. int dir_in = hs_ep->dir_in;
  1998. if (!desc)
  1999. return -EINVAL;
  2000. /* Interrupt OUT EP with mps not multiple of 4 */
  2001. if (hs_ep->index)
  2002. if (usb_endpoint_xfer_int(ep_desc) && !dir_in && (mps % 4))
  2003. bytes_rem_correction = 4 - (mps % 4);
  2004. for (i = 0; i < hs_ep->desc_count; ++i) {
  2005. status = desc->status;
  2006. bytes_rem += status & DEV_DMA_NBYTES_MASK;
  2007. bytes_rem -= bytes_rem_correction;
  2008. if (status & DEV_DMA_STS_MASK)
  2009. dev_err(hsotg->dev, "descriptor %d closed with %x\n",
  2010. i, status & DEV_DMA_STS_MASK);
  2011. if (status & DEV_DMA_L)
  2012. break;
  2013. desc++;
  2014. }
  2015. return bytes_rem;
  2016. }
  2017. /**
  2018. * dwc2_hsotg_handle_outdone - handle receiving OutDone/SetupDone from RXFIFO
  2019. * @hsotg: The device instance
  2020. * @epnum: The endpoint received from
  2021. *
  2022. * The RXFIFO has delivered an OutDone event, which means that the data
  2023. * transfer for an OUT endpoint has been completed, either by a short
  2024. * packet or by the finish of a transfer.
  2025. */
  2026. static void dwc2_hsotg_handle_outdone(struct dwc2_hsotg *hsotg, int epnum)
  2027. {
  2028. u32 epsize = dwc2_readl(hsotg, DOEPTSIZ(epnum));
  2029. struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[epnum];
  2030. struct dwc2_hsotg_req *hs_req = NULL;
  2031. struct usb_request *req = NULL;
  2032. unsigned int size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
  2033. int result = 0;
  2034. if (hs_ep == NULL) {
  2035. printk("hs_ep is NULL at %s:%d\n", __func__, __LINE__);
  2036. return;
  2037. }
  2038. hs_req = hs_ep->req;
  2039. if (!hs_req) {
  2040. dev_dbg(hsotg->dev, "%s: no request active\n", __func__);
  2041. return;
  2042. }
  2043. req = &hs_req->req;
  2044. if (epnum == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_OUT) {
  2045. dev_dbg(hsotg->dev, "zlp packet received\n");
  2046. dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
  2047. dwc2_hsotg_enqueue_setup(hsotg);
  2048. return;
  2049. }
  2050. if (using_desc_dma(hsotg))
  2051. size_left = dwc2_gadget_get_xfersize_ddma(hs_ep);
  2052. if (using_dma(hsotg)) {
  2053. unsigned int size_done;
  2054. /*
  2055. * Calculate the size of the transfer by checking how much
  2056. * is left in the endpoint size register and then working it
  2057. * out from the amount we loaded for the transfer.
  2058. *
  2059. * We need to do this as DMA pointers are always 32bit aligned
  2060. * so may overshoot/undershoot the transfer.
  2061. */
  2062. size_done = hs_ep->size_loaded - size_left;
  2063. size_done += hs_ep->last_load;
  2064. req->actual = size_done;
  2065. }
  2066. /* if there is more request to do, schedule new transfer */
  2067. if (req->actual < req->length && size_left == 0) {
  2068. dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, true);
  2069. return;
  2070. }
  2071. if (req->actual < req->length && req->short_not_ok) {
  2072. dev_dbg(hsotg->dev, "%s: got %d/%d (short not ok) => error\n",
  2073. __func__, req->actual, req->length);
  2074. /*
  2075. * todo - what should we return here? there's no one else
  2076. * even bothering to check the status.
  2077. */
  2078. }
  2079. /* DDMA IN status phase will start from StsPhseRcvd interrupt */
  2080. if (!using_desc_dma(hsotg) && epnum == 0 &&
  2081. hsotg->ep0_state == DWC2_EP0_DATA_OUT) {
  2082. /* Move to STATUS IN */
  2083. if (!hsotg->delayed_status)
  2084. dwc2_hsotg_ep0_zlp(hsotg, true);
  2085. }
  2086. /* Set actual frame number for completed transfers */
  2087. if (!using_desc_dma(hsotg) && hs_ep->isochronous) {
  2088. req->frame_number = hs_ep->target_frame;
  2089. dwc2_gadget_incr_frame_num(hs_ep);
  2090. }
  2091. dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, result);
  2092. }
  2093. /**
  2094. * dwc2_hsotg_handle_rx - RX FIFO has data
  2095. * @hsotg: The device instance
  2096. *
  2097. * The IRQ handler has detected that the RX FIFO has some data in it
  2098. * that requires processing, so find out what is in there and do the
  2099. * appropriate read.
  2100. *
  2101. * The RXFIFO is a true FIFO, the packets coming out are still in packet
  2102. * chunks, so if you have x packets received on an endpoint you'll get x
  2103. * FIFO events delivered, each with a packet's worth of data in it.
  2104. *
  2105. * When using DMA, we should not be processing events from the RXFIFO
  2106. * as the actual data should be sent to the memory directly and we turn
  2107. * on the completion interrupts to get notifications of transfer completion.
  2108. */
  2109. static void dwc2_hsotg_handle_rx(struct dwc2_hsotg *hsotg)
  2110. {
  2111. u32 grxstsr = dwc2_readl(hsotg, GRXSTSP);
  2112. u32 epnum, status, size;
  2113. WARN_ON(using_dma(hsotg));
  2114. epnum = grxstsr & GRXSTS_EPNUM_MASK;
  2115. status = grxstsr & GRXSTS_PKTSTS_MASK;
  2116. size = grxstsr & GRXSTS_BYTECNT_MASK;
  2117. size >>= GRXSTS_BYTECNT_SHIFT;
  2118. dev_dbg(hsotg->dev, "%s: GRXSTSP=0x%08x (%d@%d)\n",
  2119. __func__, grxstsr, size, epnum);
  2120. switch ((status & GRXSTS_PKTSTS_MASK) >> GRXSTS_PKTSTS_SHIFT) {
  2121. case GRXSTS_PKTSTS_GLOBALOUTNAK:
  2122. dev_dbg(hsotg->dev, "GLOBALOUTNAK\n");
  2123. break;
  2124. case GRXSTS_PKTSTS_OUTDONE:
  2125. dev_dbg(hsotg->dev, "OutDone (Frame=0x%08x)\n",
  2126. dwc2_hsotg_read_frameno(hsotg));
  2127. if (!using_dma(hsotg))
  2128. dwc2_hsotg_handle_outdone(hsotg, epnum);
  2129. break;
  2130. case GRXSTS_PKTSTS_SETUPDONE:
  2131. dev_dbg(hsotg->dev,
  2132. "SetupDone (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
  2133. dwc2_hsotg_read_frameno(hsotg),
  2134. dwc2_readl(hsotg, DOEPCTL(0)));
  2135. /*
  2136. * Call dwc2_hsotg_handle_outdone here if it was not called from
  2137. * GRXSTS_PKTSTS_OUTDONE. That is, if the core didn't
  2138. * generate GRXSTS_PKTSTS_OUTDONE for setup packet.
  2139. */
  2140. if (hsotg->ep0_state == DWC2_EP0_SETUP)
  2141. dwc2_hsotg_handle_outdone(hsotg, epnum);
  2142. break;
  2143. case GRXSTS_PKTSTS_OUTRX:
  2144. dwc2_hsotg_rx_data(hsotg, epnum, size);
  2145. break;
  2146. case GRXSTS_PKTSTS_SETUPRX:
  2147. //printk("GRXSTS_PKTSTS_SETUPRX ep0_state:%d\n", hsotg->ep0_state);
  2148. dev_dbg(hsotg->dev,
  2149. "SetupRX (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
  2150. dwc2_hsotg_read_frameno(hsotg),
  2151. dwc2_readl(hsotg, DOEPCTL(0)));
  2152. WARN_ON(hsotg->ep0_state != DWC2_EP0_SETUP);
  2153. dwc2_hsotg_rx_data(hsotg, epnum, size);
  2154. break;
  2155. default:
  2156. dev_warn(hsotg->dev, "%s: unknown status %08x\n",
  2157. __func__, grxstsr);
  2158. dwc2_hsotg_dump(hsotg);
  2159. break;
  2160. }
  2161. }
  2162. /**
  2163. * dwc2_hsotg_ep0_mps - turn max packet size into register setting
  2164. * @mps: The maximum packet size in bytes.
  2165. */
  2166. static u32 dwc2_hsotg_ep0_mps(unsigned int mps)
  2167. {
  2168. switch (mps) {
  2169. case 64:
  2170. return D0EPCTL_MPS_64;
  2171. case 32:
  2172. return D0EPCTL_MPS_32;
  2173. case 16:
  2174. return D0EPCTL_MPS_16;
  2175. case 8:
  2176. return D0EPCTL_MPS_8;
  2177. }
  2178. /* bad max packet size, warn and return invalid result */
  2179. WARN_ON(1);
  2180. return (u32)-1;
  2181. }
  2182. /**
  2183. * dwc2_hsotg_set_ep_maxpacket - set endpoint's max-packet field
  2184. * @hsotg: The driver state.
  2185. * @ep: The index number of the endpoint
  2186. * @mps: The maximum packet size in bytes
  2187. * @mc: The multicount value
  2188. * @dir_in: True if direction is in.
  2189. *
  2190. * Configure the maximum packet size for the given endpoint, updating
  2191. * the hardware control registers to reflect this.
  2192. */
  2193. static void dwc2_hsotg_set_ep_maxpacket(struct dwc2_hsotg *hsotg,
  2194. unsigned int ep, unsigned int mps,
  2195. unsigned int mc, unsigned int dir_in)
  2196. {
  2197. struct dwc2_hsotg_ep *hs_ep;
  2198. u32 reg;
  2199. hs_ep = index_to_ep(hsotg, ep, dir_in);
  2200. if (!hs_ep)
  2201. return;
  2202. if (ep == 0) {
  2203. u32 mps_bytes = mps;
  2204. /* EP0 is a special case */
  2205. mps = dwc2_hsotg_ep0_mps(mps_bytes);
  2206. if (mps > 3)
  2207. goto bad_mps;
  2208. hs_ep->ep.maxpacket = mps_bytes;
  2209. hs_ep->mc = 1;
  2210. } else {
  2211. if (mps > 1024)
  2212. goto bad_mps;
  2213. hs_ep->mc = mc;
  2214. if (mc > 3)
  2215. goto bad_mps;
  2216. hs_ep->ep.maxpacket = mps;
  2217. }
  2218. if (dir_in) {
  2219. reg = dwc2_readl(hsotg, DIEPCTL(ep));
  2220. reg &= ~DXEPCTL_MPS_MASK;
  2221. reg |= mps;
  2222. dwc2_writel(hsotg, reg, DIEPCTL(ep));
  2223. } else {
  2224. reg = dwc2_readl(hsotg, DOEPCTL(ep));
  2225. reg &= ~DXEPCTL_MPS_MASK;
  2226. reg |= mps;
  2227. dwc2_writel(hsotg, reg, DOEPCTL(ep));
  2228. }
  2229. return;
  2230. bad_mps:
  2231. dev_err(hsotg->dev, "ep%d: bad mps of %d\n", ep, mps);
  2232. }
  2233. /**
  2234. * dwc2_hsotg_txfifo_flush - flush Tx FIFO
  2235. * @hsotg: The driver state
  2236. * @idx: The index for the endpoint (0..15)
  2237. */
  2238. static void dwc2_hsotg_txfifo_flush(struct dwc2_hsotg *hsotg, unsigned int idx)
  2239. {
  2240. dwc2_writel(hsotg, GRSTCTL_TXFNUM(idx) | GRSTCTL_TXFFLSH,
  2241. GRSTCTL);
  2242. /* wait until the fifo is flushed */
  2243. if (dwc2_hsotg_wait_bit_clear(hsotg, GRSTCTL, GRSTCTL_TXFFLSH, 100))
  2244. dev_warn(hsotg->dev, "%s: timeout flushing fifo GRSTCTL_TXFFLSH\n",
  2245. __func__);
  2246. }
  2247. /**
  2248. * dwc2_hsotg_trytx - check to see if anything needs transmitting
  2249. * @hsotg: The driver state
  2250. * @hs_ep: The driver endpoint to check.
  2251. *
  2252. * Check to see if there is a request that has data to send, and if so
  2253. * make an attempt to write data into the FIFO.
  2254. */
  2255. static int dwc2_hsotg_trytx(struct dwc2_hsotg *hsotg,
  2256. struct dwc2_hsotg_ep *hs_ep)
  2257. {
  2258. struct dwc2_hsotg_req *hs_req = hs_ep->req;
  2259. if (!hs_ep->dir_in || !hs_req) {
  2260. /**
  2261. * if request is not enqueued, we disable interrupts
  2262. * for endpoints, excepting ep0
  2263. */
  2264. if (hs_ep->index != 0)
  2265. dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index,
  2266. hs_ep->dir_in, 0);
  2267. return 0;
  2268. }
  2269. if (hs_req->req.actual < hs_req->req.length) {
  2270. dev_dbg(hsotg->dev, "trying to write more for ep%d\n",
  2271. hs_ep->index);
  2272. return dwc2_hsotg_write_fifo(hsotg, hs_ep, hs_req);
  2273. }
  2274. return 0;
  2275. }
  2276. /**
  2277. * dwc2_hsotg_complete_in - complete IN transfer
  2278. * @hsotg: The device state.
  2279. * @hs_ep: The endpoint that has just completed.
  2280. *
  2281. * An IN transfer has been completed, update the transfer's state and then
  2282. * call the relevant completion routines.
  2283. */
  2284. static void dwc2_hsotg_complete_in(struct dwc2_hsotg *hsotg,
  2285. struct dwc2_hsotg_ep *hs_ep)
  2286. {
  2287. struct dwc2_hsotg_req *hs_req = hs_ep->req;
  2288. u32 epsize = dwc2_readl(hsotg, DIEPTSIZ(hs_ep->index));
  2289. int size_left, size_done;
  2290. if (!hs_req) {
  2291. dev_dbg(hsotg->dev, "XferCompl but no req\n");
  2292. return;
  2293. }
  2294. /* Finish ZLP handling for IN EP0 transactions */
  2295. if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_IN) {
  2296. dev_dbg(hsotg->dev, "zlp packet sent\n");
  2297. /*
  2298. * While send zlp for DWC2_EP0_STATUS_IN EP direction was
  2299. * changed to IN. Change back to complete OUT transfer request
  2300. */
  2301. hs_ep->dir_in = 0;
  2302. dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
  2303. if (hsotg->test_mode) {
  2304. int ret;
  2305. ret = dwc2_hsotg_set_test_mode(hsotg, hsotg->test_mode);
  2306. if (ret < 0) {
  2307. dev_dbg(hsotg->dev, "Invalid Test #%d\n",
  2308. hsotg->test_mode);
  2309. dwc2_hsotg_stall_ep0(hsotg);
  2310. return;
  2311. }
  2312. }
  2313. dwc2_hsotg_enqueue_setup(hsotg);
  2314. return;
  2315. }
  2316. /*
  2317. * Calculate the size of the transfer by checking how much is left
  2318. * in the endpoint size register and then working it out from
  2319. * the amount we loaded for the transfer.
  2320. *
  2321. * We do this even for DMA, as the transfer may have incremented
  2322. * past the end of the buffer (DMA transfers are always 32bit
  2323. * aligned).
  2324. */
  2325. if (using_desc_dma(hsotg)) {
  2326. size_left = dwc2_gadget_get_xfersize_ddma(hs_ep);
  2327. if (size_left < 0)
  2328. dev_err(hsotg->dev, "error parsing DDMA results %d\n",
  2329. size_left);
  2330. } else {
  2331. size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
  2332. }
  2333. size_done = hs_ep->size_loaded - size_left;
  2334. size_done += hs_ep->last_load;
  2335. if (hs_req->req.actual != size_done)
  2336. dev_dbg(hsotg->dev, "%s: adjusting size done %d => %d\n",
  2337. __func__, hs_req->req.actual, size_done);
  2338. hs_req->req.actual = size_done;
  2339. dev_dbg(hsotg->dev, "req->length:%d req->actual:%d req->zero:%d\n",
  2340. hs_req->req.length, hs_req->req.actual, hs_req->req.zero);
  2341. if (!size_left && hs_req->req.actual < hs_req->req.length) {
  2342. dev_dbg(hsotg->dev, "%s trying more for req...\n", __func__);
  2343. dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, true);
  2344. return;
  2345. }
  2346. /* Zlp for all endpoints in non DDMA, for ep0 only in DATA IN stage */
  2347. if (hs_ep->send_zlp) {
  2348. hs_ep->send_zlp = 0;
  2349. if (!using_desc_dma(hsotg)) {
  2350. dwc2_hsotg_program_zlp(hsotg, hs_ep);
  2351. /* transfer will be completed on next complete interrupt */
  2352. return;
  2353. }
  2354. }
  2355. if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_DATA_IN) {
  2356. /* Move to STATUS OUT */
  2357. dwc2_hsotg_ep0_zlp(hsotg, false);
  2358. return;
  2359. }
  2360. /* Set actual frame number for completed transfers */
  2361. if (!using_desc_dma(hsotg) && hs_ep->isochronous) {
  2362. hs_req->req.frame_number = hs_ep->target_frame;
  2363. dwc2_gadget_incr_frame_num(hs_ep);
  2364. }
  2365. dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
  2366. }
  2367. /**
  2368. * dwc2_gadget_read_ep_interrupts - reads interrupts for given ep
  2369. * @hsotg: The device state.
  2370. * @idx: Index of ep.
  2371. * @dir_in: Endpoint direction 1-in 0-out.
  2372. *
  2373. * Reads for endpoint with given index and direction, by masking
  2374. * epint_reg with coresponding mask.
  2375. */
  2376. static u32 dwc2_gadget_read_ep_interrupts(struct dwc2_hsotg *hsotg,
  2377. unsigned int idx, int dir_in)
  2378. {
  2379. u32 epmsk_reg = dir_in ? DIEPMSK : DOEPMSK;
  2380. u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx);
  2381. u32 ints;
  2382. u32 mask;
  2383. u32 diepempmsk;
  2384. mask = dwc2_readl(hsotg, epmsk_reg);
  2385. diepempmsk = dwc2_readl(hsotg, DIEPEMPMSK);
  2386. mask |= ((diepempmsk >> idx) & 0x1) ? DIEPMSK_TXFIFOEMPTY : 0;
  2387. mask |= DXEPINT_SETUP_RCVD;
  2388. ints = dwc2_readl(hsotg, epint_reg);
  2389. ints &= mask;
  2390. return ints;
  2391. }
  2392. /**
  2393. * dwc2_gadget_handle_ep_disabled - handle DXEPINT_EPDISBLD
  2394. * @hs_ep: The endpoint on which interrupt is asserted.
  2395. *
  2396. * This interrupt indicates that the endpoint has been disabled per the
  2397. * application's request.
  2398. *
  2399. * For IN endpoints flushes txfifo, in case of BULK clears DCTL_CGNPINNAK,
  2400. * in case of ISOC completes current request.
  2401. *
  2402. * For ISOC-OUT endpoints completes expired requests. If there is remaining
  2403. * request starts it.
  2404. */
  2405. static void dwc2_gadget_handle_ep_disabled(struct dwc2_hsotg_ep *hs_ep)
  2406. {
  2407. struct dwc2_hsotg *hsotg = hs_ep->parent;
  2408. struct dwc2_hsotg_req *hs_req;
  2409. unsigned char idx = hs_ep->index;
  2410. int dir_in = hs_ep->dir_in;
  2411. u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx);
  2412. int dctl = dwc2_readl(hsotg, DCTL);
  2413. dev_dbg(hsotg->dev, "%s: EPDisbld\n", __func__);
  2414. if (dir_in) {
  2415. int epctl = dwc2_readl(hsotg, epctl_reg);
  2416. dwc2_hsotg_txfifo_flush(hsotg, hs_ep->fifo_index);
  2417. if ((epctl & DXEPCTL_STALL) && (epctl & DXEPCTL_EPTYPE_BULK)) {
  2418. int dctl = dwc2_readl(hsotg, DCTL);
  2419. dctl |= DCTL_CGNPINNAK;
  2420. dwc2_writel(hsotg, dctl, DCTL);
  2421. }
  2422. } else {
  2423. if (dctl & DCTL_GOUTNAKSTS) {
  2424. dctl |= DCTL_CGOUTNAK;
  2425. dwc2_writel(hsotg, dctl, DCTL);
  2426. }
  2427. }
  2428. if (!hs_ep->isochronous)
  2429. return;
  2430. if (list_empty(&hs_ep->queue)) {
  2431. dev_dbg(hsotg->dev, "%s: complete_ep 0x%p, ep->queue empty!\n",
  2432. __func__, hs_ep);
  2433. return;
  2434. }
  2435. do {
  2436. hs_req = get_ep_head(hs_ep);
  2437. if (hs_req) {
  2438. hs_req->req.frame_number = hs_ep->target_frame;
  2439. hs_req->req.actual = 0;
  2440. dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req,
  2441. -ENODATA);
  2442. }
  2443. dwc2_gadget_incr_frame_num(hs_ep);
  2444. /* Update current frame number value. */
  2445. hsotg->frame_number = dwc2_hsotg_read_frameno(hsotg);
  2446. } while (dwc2_gadget_target_frame_elapsed(hs_ep));
  2447. }
  2448. /**
  2449. * dwc2_gadget_handle_out_token_ep_disabled - handle DXEPINT_OUTTKNEPDIS
  2450. * @ep: The endpoint on which interrupt is asserted.
  2451. *
  2452. * This is starting point for ISOC-OUT transfer, synchronization done with
  2453. * first out token received from host while corresponding EP is disabled.
  2454. *
  2455. * Device does not know initial frame in which out token will come. For this
  2456. * HW generates OUTTKNEPDIS - out token is received while EP is disabled. Upon
  2457. * getting this interrupt SW starts calculation for next transfer frame.
  2458. */
  2459. static void dwc2_gadget_handle_out_token_ep_disabled(struct dwc2_hsotg_ep *ep)
  2460. {
  2461. struct dwc2_hsotg *hsotg = ep->parent;
  2462. struct dwc2_hsotg_req *hs_req;
  2463. int dir_in = ep->dir_in;
  2464. if (dir_in || !ep->isochronous)
  2465. return;
  2466. if (using_desc_dma(hsotg)) {
  2467. if (ep->target_frame == TARGET_FRAME_INITIAL) {
  2468. /* Start first ISO Out */
  2469. ep->target_frame = hsotg->frame_number;
  2470. dwc2_gadget_start_isoc_ddma(ep);
  2471. }
  2472. return;
  2473. }
  2474. if (ep->target_frame == TARGET_FRAME_INITIAL) {
  2475. u32 ctrl;
  2476. ep->target_frame = hsotg->frame_number;
  2477. if (ep->interval > 1) {
  2478. ctrl = dwc2_readl(hsotg, DOEPCTL(ep->index));
  2479. if (ep->target_frame & 0x1)
  2480. ctrl |= DXEPCTL_SETODDFR;
  2481. else
  2482. ctrl |= DXEPCTL_SETEVENFR;
  2483. dwc2_writel(hsotg, ctrl, DOEPCTL(ep->index));
  2484. }
  2485. }
  2486. while (dwc2_gadget_target_frame_elapsed(ep)) {
  2487. hs_req = get_ep_head(ep);
  2488. if (hs_req) {
  2489. hs_req->req.frame_number = ep->target_frame;
  2490. hs_req->req.actual = 0;
  2491. dwc2_hsotg_complete_request(hsotg, ep, hs_req, -ENODATA);
  2492. }
  2493. dwc2_gadget_incr_frame_num(ep);
  2494. /* Update current frame number value. */
  2495. hsotg->frame_number = dwc2_hsotg_read_frameno(hsotg);
  2496. }
  2497. if (!ep->req)
  2498. dwc2_gadget_start_next_request(ep);
  2499. }
  2500. static void dwc2_hsotg_ep_stop_xfr(struct dwc2_hsotg *hsotg,
  2501. struct dwc2_hsotg_ep *hs_ep);
  2502. /**
  2503. * dwc2_gadget_handle_nak - handle NAK interrupt
  2504. * @hs_ep: The endpoint on which interrupt is asserted.
  2505. *
  2506. * This is starting point for ISOC-IN transfer, synchronization done with
  2507. * first IN token received from host while corresponding EP is disabled.
  2508. *
  2509. * Device does not know when first one token will arrive from host. On first
  2510. * token arrival HW generates 2 interrupts: 'in token received while FIFO empty'
  2511. * and 'NAK'. NAK interrupt for ISOC-IN means that token has arrived and ZLP was
  2512. * sent in response to that as there was no data in FIFO. SW is basing on this
  2513. * interrupt to obtain frame in which token has come and then based on the
  2514. * interval calculates next frame for transfer.
  2515. */
  2516. static void dwc2_gadget_handle_nak(struct dwc2_hsotg_ep *hs_ep)
  2517. {
  2518. struct dwc2_hsotg *hsotg = hs_ep->parent;
  2519. struct dwc2_hsotg_req *hs_req;
  2520. int dir_in = hs_ep->dir_in;
  2521. u32 ctrl;
  2522. if (!dir_in || !hs_ep->isochronous)
  2523. return;
  2524. if (hs_ep->target_frame == TARGET_FRAME_INITIAL) {
  2525. if (using_desc_dma(hsotg)) {
  2526. hs_ep->target_frame = hsotg->frame_number;
  2527. dwc2_gadget_incr_frame_num(hs_ep);
  2528. /* In service interval mode target_frame must
  2529. * be set to last (u)frame of the service interval.
  2530. */
  2531. if (hsotg->params.service_interval) {
  2532. /* Set target_frame to the first (u)frame of
  2533. * the service interval
  2534. */
  2535. hs_ep->target_frame &= ~hs_ep->interval + 1;
  2536. /* Set target_frame to the last (u)frame of
  2537. * the service interval
  2538. */
  2539. dwc2_gadget_incr_frame_num(hs_ep);
  2540. dwc2_gadget_dec_frame_num_by_one(hs_ep);
  2541. }
  2542. dwc2_gadget_start_isoc_ddma(hs_ep);
  2543. return;
  2544. }
  2545. hs_ep->target_frame = hsotg->frame_number;
  2546. if (hs_ep->interval > 1) {
  2547. u32 ctrl = dwc2_readl(hsotg,
  2548. DIEPCTL(hs_ep->index));
  2549. if (hs_ep->target_frame & 0x1)
  2550. ctrl |= DXEPCTL_SETODDFR;
  2551. else
  2552. ctrl |= DXEPCTL_SETEVENFR;
  2553. dwc2_writel(hsotg, ctrl, DIEPCTL(hs_ep->index));
  2554. }
  2555. }
  2556. if (using_desc_dma(hsotg))
  2557. return;
  2558. ctrl = dwc2_readl(hsotg, DIEPCTL(hs_ep->index));
  2559. if (ctrl & DXEPCTL_EPENA)
  2560. dwc2_hsotg_ep_stop_xfr(hsotg, hs_ep);
  2561. else
  2562. dwc2_hsotg_txfifo_flush(hsotg, hs_ep->fifo_index);
  2563. while (dwc2_gadget_target_frame_elapsed(hs_ep)) {
  2564. hs_req = get_ep_head(hs_ep);
  2565. if (hs_req) {
  2566. hs_req->req.frame_number = hs_ep->target_frame;
  2567. hs_req->req.actual = 0;
  2568. dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, -ENODATA);
  2569. }
  2570. dwc2_gadget_incr_frame_num(hs_ep);
  2571. /* Update current frame number value. */
  2572. hsotg->frame_number = dwc2_hsotg_read_frameno(hsotg);
  2573. }
  2574. if (!hs_ep->req)
  2575. dwc2_gadget_start_next_request(hs_ep);
  2576. }
  2577. /**
  2578. * dwc2_hsotg_epint - handle an in/out endpoint interrupt
  2579. * @hsotg: The driver state
  2580. * @idx: The index for the endpoint (0..15)
  2581. * @dir_in: Set if this is an IN endpoint
  2582. *
  2583. * Process and clear any interrupt pending for an individual endpoint
  2584. */
  2585. static void dwc2_hsotg_epint(struct dwc2_hsotg *hsotg, unsigned int idx,
  2586. int dir_in)
  2587. {
  2588. struct dwc2_hsotg_ep *hs_ep = index_to_ep(hsotg, idx, dir_in);
  2589. u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx);
  2590. u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx);
  2591. u32 epsiz_reg = dir_in ? DIEPTSIZ(idx) : DOEPTSIZ(idx);
  2592. u32 ints;
  2593. ints = dwc2_gadget_read_ep_interrupts(hsotg, idx, dir_in);
  2594. /* Clear endpoint interrupts */
  2595. dwc2_writel(hsotg, ints, epint_reg);
  2596. if (!hs_ep) {
  2597. dev_err(hsotg->dev, "%s:Interrupt for unconfigured ep%d(%s)\n",
  2598. __func__, idx, dir_in ? "in" : "out");
  2599. return;
  2600. }
  2601. dev_dbg(hsotg->dev, "%s: ep%d(%s) DxEPINT=0x%08x\n",
  2602. __func__, idx, dir_in ? "in" : "out", ints);
  2603. /* Don't process XferCompl interrupt if it is a setup packet */
  2604. if (idx == 0 && (ints & (DXEPINT_SETUP | DXEPINT_SETUP_RCVD)))
  2605. ints &= ~DXEPINT_XFERCOMPL;
  2606. /*
  2607. * Don't process XferCompl interrupt in DDMA if EP0 is still in SETUP
  2608. * stage and xfercomplete was generated without SETUP phase done
  2609. * interrupt. SW should parse received setup packet only after host's
  2610. * exit from setup phase of control transfer.
  2611. */
  2612. if (using_desc_dma(hsotg) && idx == 0 && !hs_ep->dir_in &&
  2613. hsotg->ep0_state == DWC2_EP0_SETUP && !(ints & DXEPINT_SETUP))
  2614. ints &= ~DXEPINT_XFERCOMPL;
  2615. if (ints & DXEPINT_XFERCOMPL) {
  2616. dev_dbg(hsotg->dev,
  2617. "%s: XferCompl: DxEPCTL=0x%08x, DXEPTSIZ=%08x\n",
  2618. __func__, dwc2_readl(hsotg, epctl_reg),
  2619. dwc2_readl(hsotg, epsiz_reg));
  2620. /* In DDMA handle isochronous requests separately */
  2621. if (using_desc_dma(hsotg) && hs_ep->isochronous) {
  2622. dwc2_gadget_complete_isoc_request_ddma(hs_ep);
  2623. } else if (dir_in) {
  2624. /*
  2625. * We get OutDone from the FIFO, so we only
  2626. * need to look at completing IN requests here
  2627. * if operating slave mode
  2628. */
  2629. if (!hs_ep->isochronous || !(ints & DXEPINT_NAKINTRPT))
  2630. dwc2_hsotg_complete_in(hsotg, hs_ep);
  2631. if (idx == 0 && !hs_ep->req)
  2632. dwc2_hsotg_enqueue_setup(hsotg);
  2633. } else if (using_dma(hsotg)) {
  2634. /*
  2635. * We're using DMA, we need to fire an OutDone here
  2636. * as we ignore the RXFIFO.
  2637. */
  2638. if (!hs_ep->isochronous || !(ints & DXEPINT_OUTTKNEPDIS))
  2639. dwc2_hsotg_handle_outdone(hsotg, idx);
  2640. }
  2641. }
  2642. if (ints & DXEPINT_EPDISBLD)
  2643. dwc2_gadget_handle_ep_disabled(hs_ep);
  2644. if (ints & DXEPINT_OUTTKNEPDIS)
  2645. dwc2_gadget_handle_out_token_ep_disabled(hs_ep);
  2646. if (ints & DXEPINT_NAKINTRPT)
  2647. dwc2_gadget_handle_nak(hs_ep);
  2648. if (ints & DXEPINT_AHBERR)
  2649. dev_dbg(hsotg->dev, "%s: AHBErr\n", __func__);
  2650. if (ints & DXEPINT_SETUP) { /* Setup or Timeout */
  2651. dev_dbg(hsotg->dev, "%s: Setup/Timeout\n", __func__);
  2652. if (using_dma(hsotg) && idx == 0) {
  2653. /*
  2654. * this is the notification we've received a
  2655. * setup packet. In non-DMA mode we'd get this
  2656. * from the RXFIFO, instead we need to process
  2657. * the setup here.
  2658. */
  2659. if (dir_in)
  2660. WARN_ON_ONCE(1);
  2661. else
  2662. dwc2_hsotg_handle_outdone(hsotg, 0);
  2663. }
  2664. }
  2665. if (ints & DXEPINT_STSPHSERCVD) {
  2666. dev_dbg(hsotg->dev, "%s: StsPhseRcvd\n", __func__);
  2667. /* Safety check EP0 state when STSPHSERCVD asserted */
  2668. if (hsotg->ep0_state == DWC2_EP0_DATA_OUT) {
  2669. /* Move to STATUS IN for DDMA */
  2670. if (using_desc_dma(hsotg)) {
  2671. if (!hsotg->delayed_status)
  2672. dwc2_hsotg_ep0_zlp(hsotg, true);
  2673. else
  2674. /* In case of 3 stage Control Write with delayed
  2675. * status, when Status IN transfer started
  2676. * before STSPHSERCVD asserted, NAKSTS bit not
  2677. * cleared by CNAK in dwc2_hsotg_start_req()
  2678. * function. Clear now NAKSTS to allow complete
  2679. * transfer.
  2680. */
  2681. dwc2_set_bit(hsotg, DIEPCTL(0),
  2682. DXEPCTL_CNAK);
  2683. }
  2684. }
  2685. }
  2686. if (ints & DXEPINT_BACK2BACKSETUP)
  2687. dev_dbg(hsotg->dev, "%s: B2BSetup/INEPNakEff\n", __func__);
  2688. if (ints & DXEPINT_BNAINTR) {
  2689. dev_dbg(hsotg->dev, "%s: BNA interrupt\n", __func__);
  2690. if (hs_ep->isochronous)
  2691. dwc2_gadget_handle_isoc_bna(hs_ep);
  2692. }
  2693. if (dir_in && !hs_ep->isochronous) {
  2694. /* not sure if this is important, but we'll clear it anyway */
  2695. if (ints & DXEPINT_INTKNTXFEMP) {
  2696. dev_dbg(hsotg->dev, "%s: ep%d: INTknTXFEmpMsk\n",
  2697. __func__, idx);
  2698. }
  2699. /* this probably means something bad is happening */
  2700. if (ints & DXEPINT_INTKNEPMIS) {
  2701. dev_warn(hsotg->dev, "%s: ep%d: INTknEP\n",
  2702. __func__, idx);
  2703. }
  2704. /* FIFO has space or is empty (see GAHBCFG) */
  2705. if (hsotg->dedicated_fifos &&
  2706. ints & DXEPINT_TXFEMP) {
  2707. dev_dbg(hsotg->dev, "%s: ep%d: TxFIFOEmpty\n",
  2708. __func__, idx);
  2709. if (!using_dma(hsotg))
  2710. dwc2_hsotg_trytx(hsotg, hs_ep);
  2711. }
  2712. }
  2713. }
  2714. /**
  2715. * dwc2_hsotg_irq_enumdone - Handle EnumDone interrupt (enumeration done)
  2716. * @hsotg: The device state.
  2717. *
  2718. * Handle updating the device settings after the enumeration phase has
  2719. * been completed.
  2720. */
  2721. static void dwc2_hsotg_irq_enumdone(struct dwc2_hsotg *hsotg)
  2722. {
  2723. u32 dsts = dwc2_readl(hsotg, DSTS);
  2724. int ep0_mps = 0, ep_mps = 8;
  2725. /*
  2726. * This should signal the finish of the enumeration phase
  2727. * of the USB handshaking, so we should now know what rate
  2728. * we connected at.
  2729. */
  2730. dev_dbg(hsotg->dev, "EnumDone (DSTS=0x%08x)\n", dsts);
  2731. /*
  2732. * note, since we're limited by the size of transfer on EP0, and
  2733. * it seems IN transfers must be a even number of packets we do
  2734. * not advertise a 64byte MPS on EP0.
  2735. */
  2736. /* catch both EnumSpd_FS and EnumSpd_FS48 */
  2737. switch ((dsts & DSTS_ENUMSPD_MASK) >> DSTS_ENUMSPD_SHIFT) {
  2738. case DSTS_ENUMSPD_FS:
  2739. case DSTS_ENUMSPD_FS48:
  2740. hsotg->gadget.speed = USB_SPEED_FULL;
  2741. ep0_mps = EP0_MPS_LIMIT;
  2742. ep_mps = 1023;
  2743. break;
  2744. case DSTS_ENUMSPD_HS:
  2745. hsotg->gadget.speed = USB_SPEED_HIGH;
  2746. ep0_mps = EP0_MPS_LIMIT;
  2747. ep_mps = 1024;
  2748. break;
  2749. case DSTS_ENUMSPD_LS:
  2750. hsotg->gadget.speed = USB_SPEED_LOW;
  2751. ep0_mps = 8;
  2752. ep_mps = 8;
  2753. /*
  2754. * note, we don't actually support LS in this driver at the
  2755. * moment, and the documentation seems to imply that it isn't
  2756. * supported by the PHYs on some of the devices.
  2757. */
  2758. break;
  2759. }
  2760. dev_info(hsotg->dev, "new device is %s\n",
  2761. usb_speed_string(hsotg->gadget.speed));
  2762. /*
  2763. * we should now know the maximum packet size for an
  2764. * endpoint, so set the endpoints to a default value.
  2765. */
  2766. if (ep0_mps) {
  2767. int i;
  2768. /* Initialize ep0 for both in and out directions */
  2769. dwc2_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 0, 1);
  2770. dwc2_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 0, 0);
  2771. for (i = 1; i < hsotg->num_of_eps; i++) {
  2772. if (hsotg->eps_in[i])
  2773. dwc2_hsotg_set_ep_maxpacket(hsotg, i, ep_mps,
  2774. 0, 1);
  2775. if (hsotg->eps_out[i])
  2776. dwc2_hsotg_set_ep_maxpacket(hsotg, i, ep_mps,
  2777. 0, 0);
  2778. }
  2779. }
  2780. /* ensure after enumeration our EP0 is active */
  2781. dwc2_hsotg_enqueue_setup(hsotg);
  2782. dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
  2783. dwc2_readl(hsotg, DIEPCTL0),
  2784. dwc2_readl(hsotg, DOEPCTL0));
  2785. }
  2786. /**
  2787. * kill_all_requests - remove all requests from the endpoint's queue
  2788. * @hsotg: The device state.
  2789. * @ep: The endpoint the requests may be on.
  2790. * @result: The result code to use.
  2791. *
  2792. * Go through the requests on the given endpoint and mark them
  2793. * completed with the given result code.
  2794. */
  2795. static void kill_all_requests(struct dwc2_hsotg *hsotg,
  2796. struct dwc2_hsotg_ep *ep,
  2797. int result)
  2798. {
  2799. unsigned int size;
  2800. ep->req = NULL;
  2801. while (!list_empty(&ep->queue)) {
  2802. struct dwc2_hsotg_req *req = get_ep_head(ep);
  2803. dwc2_hsotg_complete_request(hsotg, ep, req, result);
  2804. }
  2805. if (!hsotg->dedicated_fifos)
  2806. return;
  2807. size = (dwc2_readl(hsotg, DTXFSTS(ep->fifo_index)) & 0xffff) * 4;
  2808. if (size < ep->fifo_size)
  2809. dwc2_hsotg_txfifo_flush(hsotg, ep->fifo_index);
  2810. }
  2811. /**
  2812. * dwc2_hsotg_disconnect - disconnect service
  2813. * @hsotg: The device state.
  2814. *
  2815. * The device has been disconnected. Remove all current
  2816. * transactions and signal the gadget driver that this
  2817. * has happened.
  2818. */
  2819. void dwc2_hsotg_disconnect(struct dwc2_hsotg *hsotg)
  2820. {
  2821. unsigned int ep;
  2822. if (!hsotg->connected)
  2823. return;
  2824. hsotg->connected = 0;
  2825. hsotg->test_mode = 0;
  2826. /* all endpoints should be shutdown */
  2827. for (ep = 0; ep < hsotg->num_of_eps; ep++) {
  2828. if (hsotg->eps_in[ep])
  2829. kill_all_requests(hsotg, hsotg->eps_in[ep],
  2830. -ESHUTDOWN);
  2831. if (hsotg->eps_out[ep])
  2832. kill_all_requests(hsotg, hsotg->eps_out[ep],
  2833. -ESHUTDOWN);
  2834. }
  2835. call_gadget(hsotg, disconnect);
  2836. hsotg->lx_state = DWC2_L3;
  2837. usb_gadget_set_state(&hsotg->gadget, USB_STATE_NOTATTACHED);
  2838. }
  2839. /**
  2840. * dwc2_hsotg_irq_fifoempty - TX FIFO empty interrupt handler
  2841. * @hsotg: The device state:
  2842. * @periodic: True if this is a periodic FIFO interrupt
  2843. */
  2844. static void dwc2_hsotg_irq_fifoempty(struct dwc2_hsotg *hsotg, bool periodic)
  2845. {
  2846. struct dwc2_hsotg_ep *ep;
  2847. int epno, ret;
  2848. /* look through for any more data to transmit */
  2849. for (epno = 0; epno < hsotg->num_of_eps; epno++) {
  2850. ep = index_to_ep(hsotg, epno, 1);
  2851. if (!ep)
  2852. continue;
  2853. if (!ep->dir_in)
  2854. continue;
  2855. if ((periodic && !ep->periodic) ||
  2856. (!periodic && ep->periodic))
  2857. continue;
  2858. ret = dwc2_hsotg_trytx(hsotg, ep);
  2859. if (ret < 0)
  2860. break;
  2861. }
  2862. }
  2863. /* IRQ flags which will trigger a retry around the IRQ loop */
  2864. #define IRQ_RETRY_MASK (GINTSTS_NPTXFEMP | \
  2865. GINTSTS_PTXFEMP | \
  2866. GINTSTS_RXFLVL)
  2867. static int dwc2_hsotg_ep_disable(struct usb_ep *ep);
  2868. /**
  2869. * dwc2_hsotg_core_init_disconnected - issue softreset to the core
  2870. * @hsotg: The device state
  2871. * @is_usb_reset: Usb resetting flag
  2872. *
  2873. * Issue a soft reset to the core, and await the core finishing it.
  2874. */
  2875. void dwc2_hsotg_core_init_disconnected(struct dwc2_hsotg *hsotg,
  2876. bool is_usb_reset)
  2877. {
  2878. u32 intmsk;
  2879. u32 val;
  2880. u32 usbcfg;
  2881. u32 dcfg = 0;
  2882. int ep;
  2883. /* Kill any ep0 requests as controller will be reinitialized */
  2884. kill_all_requests(hsotg, hsotg->eps_out[0], -ECONNRESET);
  2885. if (!is_usb_reset) {
  2886. if (dwc2_core_reset(hsotg, true))
  2887. return;
  2888. } else {
  2889. /* all endpoints should be shutdown */
  2890. for (ep = 1; ep < hsotg->num_of_eps; ep++) {
  2891. if (hsotg->eps_in[ep])
  2892. dwc2_hsotg_ep_disable(&hsotg->eps_in[ep]->ep);
  2893. if (hsotg->eps_out[ep])
  2894. dwc2_hsotg_ep_disable(&hsotg->eps_out[ep]->ep);
  2895. }
  2896. }
  2897. /*
  2898. * we must now enable ep0 ready for host detection and then
  2899. * set configuration.
  2900. */
  2901. /* keep other bits untouched (so e.g. forced modes are not lost) */
  2902. usbcfg = dwc2_readl(hsotg, GUSBCFG);
  2903. usbcfg &= ~GUSBCFG_TOUTCAL_MASK;
  2904. usbcfg |= GUSBCFG_TOUTCAL(7);
  2905. /* remove the HNP/SRP and set the PHY */
  2906. usbcfg &= ~(GUSBCFG_SRPCAP | GUSBCFG_HNPCAP);
  2907. dwc2_writel(hsotg, usbcfg, GUSBCFG);
  2908. dwc2_phy_init(hsotg, true);
  2909. dwc2_hsotg_init_fifo(hsotg);
  2910. if (!is_usb_reset) {
  2911. dwc2_set_bit(hsotg, DCTL, DCTL_SFTDISCON);
  2912. if (hsotg->params.eusb2_disc)
  2913. dwc2_set_bit(hsotg, GOTGCTL, GOTGCTL_EUSB2_DISC_SUPP);
  2914. }
  2915. dcfg |= DCFG_EPMISCNT(1);
  2916. switch (hsotg->params.speed) {
  2917. case DWC2_SPEED_PARAM_LOW:
  2918. dcfg |= DCFG_DEVSPD_LS;
  2919. break;
  2920. case DWC2_SPEED_PARAM_FULL:
  2921. if (hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS)
  2922. dcfg |= DCFG_DEVSPD_FS48;
  2923. else
  2924. dcfg |= DCFG_DEVSPD_FS;
  2925. break;
  2926. default:
  2927. dcfg |= DCFG_DEVSPD_HS;
  2928. }
  2929. if (hsotg->params.ipg_isoc_en)
  2930. dcfg |= DCFG_IPG_ISOC_SUPPORDED;
  2931. dwc2_writel(hsotg, dcfg, DCFG);
  2932. /* Clear any pending OTG interrupts */
  2933. dwc2_writel(hsotg, 0xffffffff, GOTGINT);
  2934. /* Clear any pending interrupts */
  2935. dwc2_writel(hsotg, 0xffffffff, GINTSTS);
  2936. intmsk = GINTSTS_ERLYSUSP | GINTSTS_SESSREQINT |
  2937. GINTSTS_GOUTNAKEFF | GINTSTS_GINNAKEFF |
  2938. GINTSTS_USBRST | GINTSTS_RESETDET |
  2939. GINTSTS_ENUMDONE | GINTSTS_OTGINT |
  2940. GINTSTS_USBSUSP | GINTSTS_WKUPINT |
  2941. GINTSTS_LPMTRANRCVD;
  2942. if (!using_desc_dma(hsotg))
  2943. intmsk |= GINTSTS_INCOMPL_SOIN | GINTSTS_INCOMPL_SOOUT;
  2944. if (!hsotg->params.external_id_pin_ctl)
  2945. intmsk |= GINTSTS_CONIDSTSCHNG;
  2946. dwc2_writel(hsotg, intmsk, GINTMSK);
  2947. if (using_dma(hsotg)) {
  2948. dwc2_writel(hsotg, GAHBCFG_GLBL_INTR_EN | GAHBCFG_DMA_EN |
  2949. hsotg->params.ahbcfg,
  2950. GAHBCFG);
  2951. /* Set DDMA mode support in the core if needed */
  2952. if (using_desc_dma(hsotg))
  2953. dwc2_set_bit(hsotg, DCFG, DCFG_DESCDMA_EN);
  2954. } else {
  2955. dwc2_writel(hsotg, ((hsotg->dedicated_fifos) ?
  2956. (GAHBCFG_NP_TXF_EMP_LVL |
  2957. GAHBCFG_P_TXF_EMP_LVL) : 0) |
  2958. GAHBCFG_GLBL_INTR_EN, GAHBCFG);
  2959. }
  2960. /*
  2961. * If INTknTXFEmpMsk is enabled, it's important to disable ep interrupts
  2962. * when we have no data to transfer. Otherwise we get being flooded by
  2963. * interrupts.
  2964. */
  2965. dwc2_writel(hsotg, ((hsotg->dedicated_fifos && !using_dma(hsotg)) ?
  2966. DIEPMSK_TXFIFOEMPTY | DIEPMSK_INTKNTXFEMPMSK : 0) |
  2967. DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK |
  2968. DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK,
  2969. DIEPMSK);
  2970. /*
  2971. * don't need XferCompl, we get that from RXFIFO in slave mode. In
  2972. * DMA mode we may need this and StsPhseRcvd.
  2973. */
  2974. dwc2_writel(hsotg, (using_dma(hsotg) ? (DIEPMSK_XFERCOMPLMSK |
  2975. DOEPMSK_STSPHSERCVDMSK) : 0) |
  2976. DOEPMSK_EPDISBLDMSK | DOEPMSK_AHBERRMSK |
  2977. DOEPMSK_SETUPMSK,
  2978. DOEPMSK);
  2979. /* Enable BNA interrupt for DDMA */
  2980. if (using_desc_dma(hsotg)) {
  2981. dwc2_set_bit(hsotg, DOEPMSK, DOEPMSK_BNAMSK);
  2982. dwc2_set_bit(hsotg, DIEPMSK, DIEPMSK_BNAININTRMSK);
  2983. }
  2984. /* Enable Service Interval mode if supported */
  2985. if (using_desc_dma(hsotg) && hsotg->params.service_interval)
  2986. dwc2_set_bit(hsotg, DCTL, DCTL_SERVICE_INTERVAL_SUPPORTED);
  2987. dwc2_writel(hsotg, 0, DAINTMSK);
  2988. dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
  2989. dwc2_readl(hsotg, DIEPCTL0),
  2990. dwc2_readl(hsotg, DOEPCTL0));
  2991. /* enable in and out endpoint interrupts */
  2992. dwc2_hsotg_en_gsint(hsotg, GINTSTS_OEPINT | GINTSTS_IEPINT);
  2993. /*
  2994. * Enable the RXFIFO when in slave mode, as this is how we collect
  2995. * the data. In DMA mode, we get events from the FIFO but also
  2996. * things we cannot process, so do not use it.
  2997. */
  2998. if (!using_dma(hsotg))
  2999. dwc2_hsotg_en_gsint(hsotg, GINTSTS_RXFLVL);
  3000. /* Enable interrupts for EP0 in and out */
  3001. dwc2_hsotg_ctrl_epint(hsotg, 0, 0, 1);
  3002. dwc2_hsotg_ctrl_epint(hsotg, 0, 1, 1);
  3003. if (!is_usb_reset) {
  3004. dwc2_set_bit(hsotg, DCTL, DCTL_PWRONPRGDONE);
  3005. udelay(10); /* see openiboot */
  3006. dwc2_clear_bit(hsotg, DCTL, DCTL_PWRONPRGDONE);
  3007. }
  3008. dev_dbg(hsotg->dev, "DCTL=0x%08x\n", dwc2_readl(hsotg, DCTL));
  3009. /*
  3010. * DxEPCTL_USBActEp says RO in manual, but seems to be set by
  3011. * writing to the EPCTL register..
  3012. */
  3013. /* set to read 1 8byte packet */
  3014. dwc2_writel(hsotg, DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
  3015. DXEPTSIZ_XFERSIZE(8), DOEPTSIZ0);
  3016. dwc2_writel(hsotg, dwc2_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) |
  3017. DXEPCTL_CNAK | DXEPCTL_EPENA |
  3018. DXEPCTL_USBACTEP,
  3019. DOEPCTL0);
  3020. /* enable, but don't activate EP0in */
  3021. dwc2_writel(hsotg, dwc2_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) |
  3022. DXEPCTL_USBACTEP, DIEPCTL0);
  3023. /* clear global NAKs */
  3024. val = DCTL_CGOUTNAK | DCTL_CGNPINNAK;
  3025. if (!is_usb_reset)
  3026. val |= DCTL_SFTDISCON;
  3027. dwc2_set_bit(hsotg, DCTL, val);
  3028. /* configure the core to support LPM */
  3029. dwc2_gadget_init_lpm(hsotg);
  3030. /* program GREFCLK register if needed */
  3031. if (using_desc_dma(hsotg) && hsotg->params.service_interval)
  3032. dwc2_gadget_program_ref_clk(hsotg);
  3033. /* must be at-least 3ms to allow bus to see disconnect */
  3034. mdelay(3);
  3035. hsotg->lx_state = DWC2_L0;
  3036. dwc2_hsotg_enqueue_setup(hsotg);
  3037. dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
  3038. dwc2_readl(hsotg, DIEPCTL0),
  3039. dwc2_readl(hsotg, DOEPCTL0));
  3040. }
  3041. void dwc2_hsotg_core_disconnect(struct dwc2_hsotg *hsotg)
  3042. {
  3043. /* set the soft-disconnect bit */
  3044. dwc2_set_bit(hsotg, DCTL, DCTL_SFTDISCON);
  3045. }
  3046. void dwc2_hsotg_core_connect(struct dwc2_hsotg *hsotg)
  3047. {
  3048. /* remove the soft-disconnect and let's go */
  3049. if (!hsotg->role_sw || (dwc2_readl(hsotg, GOTGCTL) & GOTGCTL_BSESVLD))
  3050. dwc2_clear_bit(hsotg, DCTL, DCTL_SFTDISCON);
  3051. }
  3052. /**
  3053. * dwc2_gadget_handle_incomplete_isoc_in - handle incomplete ISO IN Interrupt.
  3054. * @hsotg: The device state:
  3055. *
  3056. * This interrupt indicates one of the following conditions occurred while
  3057. * transmitting an ISOC transaction.
  3058. * - Corrupted IN Token for ISOC EP.
  3059. * - Packet not complete in FIFO.
  3060. *
  3061. * The following actions will be taken:
  3062. * - Determine the EP
  3063. * - Disable EP; when 'Endpoint Disabled' interrupt is received Flush FIFO
  3064. */
  3065. static void dwc2_gadget_handle_incomplete_isoc_in(struct dwc2_hsotg *hsotg)
  3066. {
  3067. struct dwc2_hsotg_ep *hs_ep;
  3068. u32 epctrl;
  3069. u32 daintmsk;
  3070. u32 idx;
  3071. dev_dbg(hsotg->dev, "Incomplete isoc in interrupt received:\n");
  3072. daintmsk = dwc2_readl(hsotg, DAINTMSK);
  3073. for (idx = 1; idx < hsotg->num_of_eps; idx++) {
  3074. hs_ep = hsotg->eps_in[idx];
  3075. /* Proceed only unmasked ISOC EPs */
  3076. if ((BIT(idx) & ~daintmsk) || !hs_ep->isochronous)
  3077. continue;
  3078. epctrl = dwc2_readl(hsotg, DIEPCTL(idx));
  3079. if ((epctrl & DXEPCTL_EPENA) &&
  3080. dwc2_gadget_target_frame_elapsed(hs_ep)) {
  3081. epctrl |= DXEPCTL_SNAK;
  3082. epctrl |= DXEPCTL_EPDIS;
  3083. dwc2_writel(hsotg, epctrl, DIEPCTL(idx));
  3084. }
  3085. }
  3086. /* Clear interrupt */
  3087. dwc2_writel(hsotg, GINTSTS_INCOMPL_SOIN, GINTSTS);
  3088. }
  3089. /**
  3090. * dwc2_gadget_handle_incomplete_isoc_out - handle incomplete ISO OUT Interrupt
  3091. * @hsotg: The device state:
  3092. *
  3093. * This interrupt indicates one of the following conditions occurred while
  3094. * transmitting an ISOC transaction.
  3095. * - Corrupted OUT Token for ISOC EP.
  3096. * - Packet not complete in FIFO.
  3097. *
  3098. * The following actions will be taken:
  3099. * - Determine the EP
  3100. * - Set DCTL_SGOUTNAK and unmask GOUTNAKEFF if target frame elapsed.
  3101. */
  3102. static void dwc2_gadget_handle_incomplete_isoc_out(struct dwc2_hsotg *hsotg)
  3103. {
  3104. u32 gintsts;
  3105. u32 gintmsk;
  3106. u32 daintmsk;
  3107. u32 epctrl;
  3108. struct dwc2_hsotg_ep *hs_ep;
  3109. int idx;
  3110. dev_dbg(hsotg->dev, "%s: GINTSTS_INCOMPL_SOOUT\n", __func__);
  3111. daintmsk = dwc2_readl(hsotg, DAINTMSK);
  3112. daintmsk >>= DAINT_OUTEP_SHIFT;
  3113. for (idx = 1; idx < hsotg->num_of_eps; idx++) {
  3114. hs_ep = hsotg->eps_out[idx];
  3115. /* Proceed only unmasked ISOC EPs */
  3116. if ((BIT(idx) & ~daintmsk) || !hs_ep->isochronous)
  3117. continue;
  3118. epctrl = dwc2_readl(hsotg, DOEPCTL(idx));
  3119. if ((epctrl & DXEPCTL_EPENA) &&
  3120. dwc2_gadget_target_frame_elapsed(hs_ep)) {
  3121. /* Unmask GOUTNAKEFF interrupt */
  3122. gintmsk = dwc2_readl(hsotg, GINTMSK);
  3123. gintmsk |= GINTSTS_GOUTNAKEFF;
  3124. dwc2_writel(hsotg, gintmsk, GINTMSK);
  3125. gintsts = dwc2_readl(hsotg, GINTSTS);
  3126. if (!(gintsts & GINTSTS_GOUTNAKEFF)) {
  3127. dwc2_set_bit(hsotg, DCTL, DCTL_SGOUTNAK);
  3128. break;
  3129. }
  3130. }
  3131. }
  3132. /* Clear interrupt */
  3133. dwc2_writel(hsotg, GINTSTS_INCOMPL_SOOUT, GINTSTS);
  3134. }
  3135. /**
  3136. * dwc2_hsotg_irq - handle device interrupt
  3137. * @irq: The IRQ number triggered
  3138. * @pw: The pw value when registered the handler.
  3139. */
  3140. static irqreturn_t dwc2_hsotg_irq(int irq, void *pw)
  3141. {
  3142. struct dwc2_hsotg *hsotg = pw;
  3143. int retry_count = 8;
  3144. u32 gintsts;
  3145. u32 gintmsk;
  3146. if (!dwc2_is_device_mode(hsotg))
  3147. return IRQ_NONE;
  3148. spin_lock(&hsotg->lock);
  3149. irq_retry:
  3150. gintsts = dwc2_readl(hsotg, GINTSTS);
  3151. gintmsk = dwc2_readl(hsotg, GINTMSK);
  3152. dev_dbg(hsotg->dev, "%s: %08x %08x (%08x) retry %d\n",
  3153. __func__, gintsts, gintsts & gintmsk, gintmsk, retry_count);
  3154. gintsts &= gintmsk;
  3155. if (gintsts & GINTSTS_RESETDET) {
  3156. dev_dbg(hsotg->dev, "%s: USBRstDet\n", __func__);
  3157. dwc2_writel(hsotg, GINTSTS_RESETDET, GINTSTS);
  3158. /* This event must be used only if controller is suspended */
  3159. if (hsotg->in_ppd && hsotg->lx_state == DWC2_L2)
  3160. dwc2_exit_partial_power_down(hsotg, 0, true);
  3161. /* Exit gadget mode clock gating. */
  3162. if (hsotg->params.power_down ==
  3163. DWC2_POWER_DOWN_PARAM_NONE && hsotg->bus_suspended &&
  3164. !hsotg->params.no_clock_gating)
  3165. dwc2_gadget_exit_clock_gating(hsotg, 0);
  3166. hsotg->lx_state = DWC2_L0;
  3167. }
  3168. if (gintsts & (GINTSTS_USBRST | GINTSTS_RESETDET)) {
  3169. u32 usb_status = dwc2_readl(hsotg, GOTGCTL);
  3170. u32 connected = hsotg->connected;
  3171. dev_dbg(hsotg->dev, "%s: USBRst\n", __func__);
  3172. dev_dbg(hsotg->dev, "GNPTXSTS=%08x\n",
  3173. dwc2_readl(hsotg, GNPTXSTS));
  3174. dwc2_writel(hsotg, GINTSTS_USBRST, GINTSTS);
  3175. /* Report disconnection if it is not already done. */
  3176. dwc2_hsotg_disconnect(hsotg);
  3177. /* Reset device address to zero */
  3178. dwc2_clear_bit(hsotg, DCFG, DCFG_DEVADDR_MASK);
  3179. if (usb_status & GOTGCTL_BSESVLD && connected)
  3180. dwc2_hsotg_core_init_disconnected(hsotg, true);
  3181. }
  3182. if (gintsts & GINTSTS_ENUMDONE) {
  3183. dwc2_writel(hsotg, GINTSTS_ENUMDONE, GINTSTS);
  3184. dwc2_hsotg_irq_enumdone(hsotg);
  3185. }
  3186. if (gintsts & (GINTSTS_OEPINT | GINTSTS_IEPINT)) {
  3187. u32 daint = dwc2_readl(hsotg, DAINT);
  3188. u32 daintmsk = dwc2_readl(hsotg, DAINTMSK);
  3189. u32 daint_out, daint_in;
  3190. int ep;
  3191. daint &= daintmsk;
  3192. daint_out = daint >> DAINT_OUTEP_SHIFT;
  3193. daint_in = daint & ~(daint_out << DAINT_OUTEP_SHIFT);
  3194. dev_dbg(hsotg->dev, "%s: daint=%08x\n", __func__, daint);
  3195. for (ep = 0; ep < hsotg->num_of_eps && daint_out;
  3196. ep++, daint_out >>= 1) {
  3197. if (daint_out & 1)
  3198. dwc2_hsotg_epint(hsotg, ep, 0);
  3199. }
  3200. for (ep = 0; ep < hsotg->num_of_eps && daint_in;
  3201. ep++, daint_in >>= 1) {
  3202. if (daint_in & 1)
  3203. dwc2_hsotg_epint(hsotg, ep, 1);
  3204. }
  3205. }
  3206. /* check both FIFOs */
  3207. if (gintsts & GINTSTS_NPTXFEMP) {
  3208. dev_dbg(hsotg->dev, "NPTxFEmp\n");
  3209. /*
  3210. * Disable the interrupt to stop it happening again
  3211. * unless one of these endpoint routines decides that
  3212. * it needs re-enabling
  3213. */
  3214. dwc2_hsotg_disable_gsint(hsotg, GINTSTS_NPTXFEMP);
  3215. dwc2_hsotg_irq_fifoempty(hsotg, false);
  3216. }
  3217. if (gintsts & GINTSTS_PTXFEMP) {
  3218. dev_dbg(hsotg->dev, "PTxFEmp\n");
  3219. /* See note in GINTSTS_NPTxFEmp */
  3220. dwc2_hsotg_disable_gsint(hsotg, GINTSTS_PTXFEMP);
  3221. dwc2_hsotg_irq_fifoempty(hsotg, true);
  3222. }
  3223. if (gintsts & GINTSTS_RXFLVL) {
  3224. /*
  3225. * note, since GINTSTS_RxFLvl doubles as FIFO-not-empty,
  3226. * we need to retry dwc2_hsotg_handle_rx if this is still
  3227. * set.
  3228. */
  3229. dwc2_hsotg_handle_rx(hsotg);
  3230. }
  3231. if (gintsts & GINTSTS_ERLYSUSP) {
  3232. dev_dbg(hsotg->dev, "GINTSTS_ErlySusp\n");
  3233. dwc2_writel(hsotg, GINTSTS_ERLYSUSP, GINTSTS);
  3234. }
  3235. /*
  3236. * these next two seem to crop-up occasionally causing the core
  3237. * to shutdown the USB transfer, so try clearing them and logging
  3238. * the occurrence.
  3239. */
  3240. if (gintsts & GINTSTS_GOUTNAKEFF) {
  3241. u8 idx;
  3242. u32 epctrl;
  3243. u32 gintmsk;
  3244. u32 daintmsk;
  3245. struct dwc2_hsotg_ep *hs_ep;
  3246. daintmsk = dwc2_readl(hsotg, DAINTMSK);
  3247. daintmsk >>= DAINT_OUTEP_SHIFT;
  3248. /* Mask this interrupt */
  3249. gintmsk = dwc2_readl(hsotg, GINTMSK);
  3250. gintmsk &= ~GINTSTS_GOUTNAKEFF;
  3251. dwc2_writel(hsotg, gintmsk, GINTMSK);
  3252. dev_dbg(hsotg->dev, "GOUTNakEff triggered\n");
  3253. for (idx = 1; idx < hsotg->num_of_eps; idx++) {
  3254. hs_ep = hsotg->eps_out[idx];
  3255. /* Proceed only unmasked ISOC EPs */
  3256. if (BIT(idx) & ~daintmsk)
  3257. continue;
  3258. epctrl = dwc2_readl(hsotg, DOEPCTL(idx));
  3259. //ISOC Ep's only
  3260. if ((epctrl & DXEPCTL_EPENA) && hs_ep->isochronous) {
  3261. epctrl |= DXEPCTL_SNAK;
  3262. epctrl |= DXEPCTL_EPDIS;
  3263. dwc2_writel(hsotg, epctrl, DOEPCTL(idx));
  3264. continue;
  3265. }
  3266. //Non-ISOC EP's
  3267. if (hs_ep->halted) {
  3268. if (!(epctrl & DXEPCTL_EPENA))
  3269. epctrl |= DXEPCTL_EPENA;
  3270. epctrl |= DXEPCTL_EPDIS;
  3271. epctrl |= DXEPCTL_STALL;
  3272. dwc2_writel(hsotg, epctrl, DOEPCTL(idx));
  3273. }
  3274. }
  3275. /* This interrupt bit is cleared in DXEPINT_EPDISBLD handler */
  3276. }
  3277. if (gintsts & GINTSTS_GINNAKEFF) {
  3278. dev_info(hsotg->dev, "GINNakEff triggered\n");
  3279. dwc2_set_bit(hsotg, DCTL, DCTL_CGNPINNAK);
  3280. dwc2_hsotg_dump(hsotg);
  3281. }
  3282. if (gintsts & GINTSTS_INCOMPL_SOIN)
  3283. dwc2_gadget_handle_incomplete_isoc_in(hsotg);
  3284. if (gintsts & GINTSTS_INCOMPL_SOOUT)
  3285. dwc2_gadget_handle_incomplete_isoc_out(hsotg);
  3286. /*
  3287. * if we've had fifo events, we should try and go around the
  3288. * loop again to see if there's any point in returning yet.
  3289. */
  3290. if (gintsts & IRQ_RETRY_MASK && --retry_count > 0)
  3291. goto irq_retry;
  3292. /* Check WKUP_ALERT interrupt*/
  3293. if (hsotg->params.service_interval)
  3294. dwc2_gadget_wkup_alert_handler(hsotg);
  3295. spin_unlock(&hsotg->lock);
  3296. return IRQ_HANDLED;
  3297. }
  3298. static void dwc2_hsotg_ep_stop_xfr(struct dwc2_hsotg *hsotg,
  3299. struct dwc2_hsotg_ep *hs_ep)
  3300. {
  3301. u32 epctrl_reg;
  3302. u32 epint_reg;
  3303. epctrl_reg = hs_ep->dir_in ? DIEPCTL(hs_ep->index) :
  3304. DOEPCTL(hs_ep->index);
  3305. epint_reg = hs_ep->dir_in ? DIEPINT(hs_ep->index) :
  3306. DOEPINT(hs_ep->index);
  3307. dev_dbg(hsotg->dev, "%s: stopping transfer on %s\n", __func__,
  3308. hs_ep->name);
  3309. if (hs_ep->dir_in) {
  3310. if (hsotg->dedicated_fifos || hs_ep->periodic) {
  3311. dwc2_set_bit(hsotg, epctrl_reg, DXEPCTL_SNAK);
  3312. /* Wait for Nak effect */
  3313. if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg,
  3314. DXEPINT_INEPNAKEFF, 100))
  3315. dev_warn(hsotg->dev,
  3316. "%s: timeout DIEPINT.NAKEFF\n",
  3317. __func__);
  3318. } else {
  3319. dwc2_set_bit(hsotg, DCTL, DCTL_SGNPINNAK);
  3320. /* Wait for Nak effect */
  3321. if (dwc2_hsotg_wait_bit_set(hsotg, GINTSTS,
  3322. GINTSTS_GINNAKEFF, 100))
  3323. dev_warn(hsotg->dev,
  3324. "%s: timeout GINTSTS.GINNAKEFF\n",
  3325. __func__);
  3326. }
  3327. } else {
  3328. /* Mask GINTSTS_GOUTNAKEFF interrupt */
  3329. dwc2_hsotg_disable_gsint(hsotg, GINTSTS_GOUTNAKEFF);
  3330. if (!(dwc2_readl(hsotg, GINTSTS) & GINTSTS_GOUTNAKEFF))
  3331. dwc2_set_bit(hsotg, DCTL, DCTL_SGOUTNAK);
  3332. if (!using_dma(hsotg)) {
  3333. /* Wait for GINTSTS_RXFLVL interrupt */
  3334. if (dwc2_hsotg_wait_bit_set(hsotg, GINTSTS,
  3335. GINTSTS_RXFLVL, 100)) {
  3336. dev_warn(hsotg->dev, "%s: timeout GINTSTS.RXFLVL\n",
  3337. __func__);
  3338. } else {
  3339. /*
  3340. * Pop GLOBAL OUT NAK status packet from RxFIFO
  3341. * to assert GOUTNAKEFF interrupt
  3342. */
  3343. dwc2_readl(hsotg, GRXSTSP);
  3344. }
  3345. }
  3346. /* Wait for global nak to take effect */
  3347. if (dwc2_hsotg_wait_bit_set(hsotg, GINTSTS,
  3348. GINTSTS_GOUTNAKEFF, 100))
  3349. dev_warn(hsotg->dev, "%s: timeout GINTSTS.GOUTNAKEFF\n",
  3350. __func__);
  3351. }
  3352. /* Disable ep */
  3353. dwc2_set_bit(hsotg, epctrl_reg, DXEPCTL_EPDIS | DXEPCTL_SNAK);
  3354. /* Wait for ep to be disabled */
  3355. if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg, DXEPINT_EPDISBLD, 100))
  3356. dev_warn(hsotg->dev,
  3357. "%s: timeout DOEPCTL.EPDisable\n", __func__);
  3358. /* Clear EPDISBLD interrupt */
  3359. dwc2_set_bit(hsotg, epint_reg, DXEPINT_EPDISBLD);
  3360. if (hs_ep->dir_in) {
  3361. unsigned short fifo_index;
  3362. if (hsotg->dedicated_fifos || hs_ep->periodic)
  3363. fifo_index = hs_ep->fifo_index;
  3364. else
  3365. fifo_index = 0;
  3366. /* Flush TX FIFO */
  3367. dwc2_flush_tx_fifo(hsotg, fifo_index);
  3368. /* Clear Global In NP NAK in Shared FIFO for non periodic ep */
  3369. if (!hsotg->dedicated_fifos && !hs_ep->periodic)
  3370. dwc2_set_bit(hsotg, DCTL, DCTL_CGNPINNAK);
  3371. } else {
  3372. /* Remove global NAKs */
  3373. dwc2_set_bit(hsotg, DCTL, DCTL_CGOUTNAK);
  3374. }
  3375. }
  3376. /**
  3377. * dwc2_hsotg_ep_enable - enable the given endpoint
  3378. * @ep: The USB endpint to configure
  3379. * @desc: The USB endpoint descriptor to configure with.
  3380. *
  3381. * This is called from the USB gadget code's usb_ep_enable().
  3382. */
  3383. static int dwc2_hsotg_ep_enable(struct usb_ep *ep,
  3384. const struct usb_endpoint_descriptor *desc)
  3385. {
  3386. struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
  3387. struct dwc2_hsotg *hsotg = hs_ep->parent;
  3388. unsigned long flags;
  3389. unsigned int index = hs_ep->index;
  3390. u32 epctrl_reg;
  3391. u32 epctrl;
  3392. u32 mps;
  3393. u32 mc;
  3394. u32 mask;
  3395. unsigned int dir_in;
  3396. unsigned int i, val, size;
  3397. int ret = 0;
  3398. unsigned char ep_type;
  3399. int desc_num;
  3400. dev_dbg(hsotg->dev,
  3401. "%s: ep %s: a 0x%02x, attr 0x%02x, mps 0x%04x, intr %d\n",
  3402. __func__, ep->name, desc->bEndpointAddress, desc->bmAttributes,
  3403. desc->wMaxPacketSize, desc->bInterval);
  3404. /* not to be called for EP0 */
  3405. if (index == 0) {
  3406. dev_err(hsotg->dev, "%s: called for EP 0\n", __func__);
  3407. return -EINVAL;
  3408. }
  3409. dir_in = (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? 1 : 0;
  3410. if (dir_in != hs_ep->dir_in) {
  3411. dev_err(hsotg->dev, "%s: direction mismatch!\n", __func__);
  3412. return -EINVAL;
  3413. }
  3414. ep_type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
  3415. mps = usb_endpoint_maxp(desc);
  3416. mc = usb_endpoint_maxp_mult(desc);
  3417. /* ISOC IN in DDMA supported bInterval up to 10 */
  3418. if (using_desc_dma(hsotg) && ep_type == USB_ENDPOINT_XFER_ISOC &&
  3419. dir_in && desc->bInterval > 10) {
  3420. dev_err(hsotg->dev,
  3421. "%s: ISOC IN, DDMA: bInterval>10 not supported!\n", __func__);
  3422. return -EINVAL;
  3423. }
  3424. /* High bandwidth ISOC OUT in DDMA not supported */
  3425. if (using_desc_dma(hsotg) && ep_type == USB_ENDPOINT_XFER_ISOC &&
  3426. !dir_in && mc > 1) {
  3427. dev_err(hsotg->dev,
  3428. "%s: ISOC OUT, DDMA: HB not supported!\n", __func__);
  3429. return -EINVAL;
  3430. }
  3431. /* note, we handle this here instead of dwc2_hsotg_set_ep_maxpacket */
  3432. epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
  3433. epctrl = dwc2_readl(hsotg, epctrl_reg);
  3434. dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x from 0x%08x\n",
  3435. __func__, epctrl, epctrl_reg);
  3436. if (using_desc_dma(hsotg) && ep_type == USB_ENDPOINT_XFER_ISOC)
  3437. desc_num = MAX_DMA_DESC_NUM_HS_ISOC;
  3438. else
  3439. desc_num = MAX_DMA_DESC_NUM_GENERIC;
  3440. /* Allocate DMA descriptor chain for non-ctrl endpoints */
  3441. if (using_desc_dma(hsotg) && !hs_ep->desc_list) {
  3442. hs_ep->desc_list = dmam_alloc_coherent(hsotg->dev,
  3443. desc_num * sizeof(struct dwc2_dma_desc),
  3444. &hs_ep->desc_list_dma, GFP_ATOMIC);
  3445. if (!hs_ep->desc_list) {
  3446. ret = -ENOMEM;
  3447. goto error2;
  3448. }
  3449. }
  3450. spin_lock_irqsave(&hsotg->lock, flags);
  3451. epctrl &= ~(DXEPCTL_EPTYPE_MASK | DXEPCTL_MPS_MASK);
  3452. epctrl |= DXEPCTL_MPS(mps);
  3453. /*
  3454. * mark the endpoint as active, otherwise the core may ignore
  3455. * transactions entirely for this endpoint
  3456. */
  3457. epctrl |= DXEPCTL_USBACTEP;
  3458. /* update the endpoint state */
  3459. dwc2_hsotg_set_ep_maxpacket(hsotg, hs_ep->index, mps, mc, dir_in);
  3460. /* default, set to non-periodic */
  3461. hs_ep->isochronous = 0;
  3462. hs_ep->periodic = 0;
  3463. hs_ep->halted = 0;
  3464. hs_ep->wedged = 0;
  3465. hs_ep->interval = desc->bInterval;
  3466. switch (ep_type) {
  3467. case USB_ENDPOINT_XFER_ISOC:
  3468. epctrl |= DXEPCTL_EPTYPE_ISO;
  3469. epctrl |= DXEPCTL_SETEVENFR;
  3470. hs_ep->isochronous = 1;
  3471. hs_ep->interval = 1 << (desc->bInterval - 1);
  3472. hs_ep->target_frame = TARGET_FRAME_INITIAL;
  3473. hs_ep->next_desc = 0;
  3474. hs_ep->compl_desc = 0;
  3475. if (dir_in) {
  3476. hs_ep->periodic = 1;
  3477. mask = dwc2_readl(hsotg, DIEPMSK);
  3478. mask |= DIEPMSK_NAKMSK;
  3479. dwc2_writel(hsotg, mask, DIEPMSK);
  3480. } else {
  3481. epctrl |= DXEPCTL_SNAK;
  3482. mask = dwc2_readl(hsotg, DOEPMSK);
  3483. mask |= DOEPMSK_OUTTKNEPDISMSK;
  3484. dwc2_writel(hsotg, mask, DOEPMSK);
  3485. }
  3486. break;
  3487. case USB_ENDPOINT_XFER_BULK:
  3488. epctrl |= DXEPCTL_EPTYPE_BULK;
  3489. break;
  3490. case USB_ENDPOINT_XFER_INT:
  3491. if (dir_in)
  3492. hs_ep->periodic = 1;
  3493. if (hsotg->gadget.speed == USB_SPEED_HIGH)
  3494. hs_ep->interval = 1 << (desc->bInterval - 1);
  3495. epctrl |= DXEPCTL_EPTYPE_INTERRUPT;
  3496. break;
  3497. case USB_ENDPOINT_XFER_CONTROL:
  3498. epctrl |= DXEPCTL_EPTYPE_CONTROL;
  3499. break;
  3500. }
  3501. /*
  3502. * if the hardware has dedicated fifos, we must give each IN EP
  3503. * a unique tx-fifo even if it is non-periodic.
  3504. */
  3505. if (dir_in && (hsotg->dedicated_fifos || ((desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT))) {
  3506. unsigned fifo_count = dwc2_hsotg_tx_fifo_count(hsotg);
  3507. u32 fifo_index = 0;
  3508. u32 fifo_size = UINT_MAX;
  3509. size = hs_ep->ep.maxpacket * hs_ep->mc;
  3510. for (i = 1; i <= fifo_count; ++i) {
  3511. if (hsotg->fifo_map & (1 << i))
  3512. continue;
  3513. val = dwc2_readl(hsotg, DPTXFSIZN(i));
  3514. val = (val >> FIFOSIZE_DEPTH_SHIFT) * 4;
  3515. if (val < size)
  3516. continue;
  3517. /* Search for smallest acceptable fifo */
  3518. if (val < fifo_size) {
  3519. fifo_size = val;
  3520. fifo_index = i;
  3521. }
  3522. }
  3523. if (!fifo_index) {
  3524. dev_err(hsotg->dev,
  3525. "%s: No suitable fifo found\n", __func__);
  3526. ret = -ENOMEM;
  3527. goto error1;
  3528. }
  3529. epctrl &= ~(DXEPCTL_TXFNUM_LIMIT << DXEPCTL_TXFNUM_SHIFT);
  3530. hsotg->fifo_map |= 1 << fifo_index;
  3531. epctrl |= DXEPCTL_TXFNUM(fifo_index);
  3532. hs_ep->fifo_index = fifo_index;
  3533. hs_ep->fifo_size = fifo_size;
  3534. }
  3535. /* for non control endpoints, set PID to D0 */
  3536. if (index && !hs_ep->isochronous)
  3537. epctrl |= DXEPCTL_SETD0PID;
  3538. /* WA for Full speed ISOC IN in DDMA mode.
  3539. * By Clear NAK status of EP, core will send ZLP
  3540. * to IN token and assert NAK interrupt relying
  3541. * on TxFIFO status only
  3542. */
  3543. if (hsotg->gadget.speed == USB_SPEED_FULL &&
  3544. hs_ep->isochronous && dir_in) {
  3545. /* The WA applies only to core versions from 2.72a
  3546. * to 4.00a (including both). Also for FS_IOT_1.00a
  3547. * and HS_IOT_1.00a.
  3548. */
  3549. u32 gsnpsid = dwc2_readl(hsotg, GSNPSID);
  3550. if ((gsnpsid >= DWC2_CORE_REV_2_72a &&
  3551. gsnpsid <= DWC2_CORE_REV_4_00a) ||
  3552. gsnpsid == DWC2_FS_IOT_REV_1_00a ||
  3553. gsnpsid == DWC2_HS_IOT_REV_1_00a)
  3554. epctrl |= DXEPCTL_CNAK;
  3555. }
  3556. dev_dbg(hsotg->dev, "%s: write DxEPCTL=0x%08x\n",
  3557. __func__, epctrl);
  3558. dwc2_writel(hsotg, epctrl, epctrl_reg);
  3559. dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x\n",
  3560. __func__, dwc2_readl(hsotg, epctrl_reg));
  3561. /* enable the endpoint interrupt */
  3562. dwc2_hsotg_ctrl_epint(hsotg, index, dir_in, 1);
  3563. error1:
  3564. spin_unlock_irqrestore(&hsotg->lock, flags);
  3565. error2:
  3566. if (ret && using_desc_dma(hsotg) && hs_ep->desc_list) {
  3567. dmam_free_coherent(hsotg->dev, desc_num *
  3568. sizeof(struct dwc2_dma_desc),
  3569. hs_ep->desc_list, hs_ep->desc_list_dma);
  3570. hs_ep->desc_list = NULL;
  3571. }
  3572. return ret;
  3573. }
  3574. /**
  3575. * dwc2_hsotg_ep_disable - disable given endpoint
  3576. * @ep: The endpoint to disable.
  3577. */
  3578. static int dwc2_hsotg_ep_disable(struct usb_ep *ep)
  3579. {
  3580. struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
  3581. struct dwc2_hsotg *hsotg = hs_ep->parent;
  3582. int dir_in = hs_ep->dir_in;
  3583. int index = hs_ep->index;
  3584. u32 epctrl_reg;
  3585. u32 ctrl;
  3586. dev_dbg(hsotg->dev, "%s(ep %p)\n", __func__, ep);
  3587. if (ep == &hsotg->eps_out[0]->ep) {
  3588. dev_err(hsotg->dev, "%s: called for ep0\n", __func__);
  3589. return -EINVAL;
  3590. }
  3591. if (hsotg->op_state != OTG_STATE_B_PERIPHERAL) {
  3592. dev_err(hsotg->dev, "%s: called in host mode?\n", __func__);
  3593. return -EINVAL;
  3594. }
  3595. epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
  3596. ctrl = dwc2_readl(hsotg, epctrl_reg);
  3597. if (ctrl & DXEPCTL_EPENA)
  3598. dwc2_hsotg_ep_stop_xfr(hsotg, hs_ep);
  3599. ctrl &= ~DXEPCTL_EPENA;
  3600. ctrl &= ~DXEPCTL_USBACTEP;
  3601. ctrl |= DXEPCTL_SNAK;
  3602. dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
  3603. dwc2_writel(hsotg, ctrl, epctrl_reg);
  3604. /* disable endpoint interrupts */
  3605. dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 0);
  3606. /* terminate all requests with shutdown */
  3607. kill_all_requests(hsotg, hs_ep, -ESHUTDOWN);
  3608. hsotg->fifo_map &= ~(1 << hs_ep->fifo_index);
  3609. hs_ep->fifo_index = 0;
  3610. hs_ep->fifo_size = 0;
  3611. return 0;
  3612. }
  3613. static int dwc2_hsotg_ep_disable_lock(struct usb_ep *ep)
  3614. {
  3615. struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
  3616. struct dwc2_hsotg *hsotg = hs_ep->parent;
  3617. unsigned long flags;
  3618. int ret;
  3619. spin_lock_irqsave(&hsotg->lock, flags);
  3620. ret = dwc2_hsotg_ep_disable(ep);
  3621. spin_unlock_irqrestore(&hsotg->lock, flags);
  3622. return ret;
  3623. }
  3624. /**
  3625. * on_list - check request is on the given endpoint
  3626. * @ep: The endpoint to check.
  3627. * @test: The request to test if it is on the endpoint.
  3628. */
  3629. static bool on_list(struct dwc2_hsotg_ep *ep, struct dwc2_hsotg_req *test)
  3630. {
  3631. struct dwc2_hsotg_req *req, *treq;
  3632. list_for_each_entry_safe(req, treq, &ep->queue, queue) {
  3633. if (req == test)
  3634. return true;
  3635. }
  3636. return false;
  3637. }
  3638. /**
  3639. * dwc2_hsotg_ep_dequeue - dequeue given endpoint
  3640. * @ep: The endpoint to dequeue.
  3641. * @req: The request to be removed from a queue.
  3642. */
  3643. static int dwc2_hsotg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
  3644. {
  3645. struct dwc2_hsotg_req *hs_req = our_req(req);
  3646. struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
  3647. struct dwc2_hsotg *hs = hs_ep->parent;
  3648. unsigned long flags;
  3649. dev_dbg(hs->dev, "ep_dequeue(%p,%p)\n", ep, req);
  3650. spin_lock_irqsave(&hs->lock, flags);
  3651. if (!on_list(hs_ep, hs_req)) {
  3652. spin_unlock_irqrestore(&hs->lock, flags);
  3653. return -EINVAL;
  3654. }
  3655. /* Dequeue already started request */
  3656. if (req == &hs_ep->req->req)
  3657. dwc2_hsotg_ep_stop_xfr(hs, hs_ep);
  3658. dwc2_hsotg_complete_request(hs, hs_ep, hs_req, -ECONNRESET);
  3659. spin_unlock_irqrestore(&hs->lock, flags);
  3660. return 0;
  3661. }
  3662. /**
  3663. * dwc2_gadget_ep_set_wedge - set wedge on a given endpoint
  3664. * @ep: The endpoint to be wedged.
  3665. *
  3666. */
  3667. static int dwc2_gadget_ep_set_wedge(struct usb_ep *ep)
  3668. {
  3669. struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
  3670. struct dwc2_hsotg *hs = hs_ep->parent;
  3671. unsigned long flags;
  3672. int ret;
  3673. spin_lock_irqsave(&hs->lock, flags);
  3674. hs_ep->wedged = 1;
  3675. ret = dwc2_hsotg_ep_sethalt(ep, 1, false);
  3676. spin_unlock_irqrestore(&hs->lock, flags);
  3677. return ret;
  3678. }
  3679. /**
  3680. * dwc2_hsotg_ep_sethalt - set halt on a given endpoint
  3681. * @ep: The endpoint to set halt.
  3682. * @value: Set or unset the halt.
  3683. * @now: If true, stall the endpoint now. Otherwise return -EAGAIN if
  3684. * the endpoint is busy processing requests.
  3685. *
  3686. * We need to stall the endpoint immediately if request comes from set_feature
  3687. * protocol command handler.
  3688. */
  3689. static int dwc2_hsotg_ep_sethalt(struct usb_ep *ep, int value, bool now)
  3690. {
  3691. struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
  3692. struct dwc2_hsotg *hs = hs_ep->parent;
  3693. int index = hs_ep->index;
  3694. u32 epreg;
  3695. u32 epctl;
  3696. u32 xfertype;
  3697. return 0;
  3698. dev_info(hs->dev, "%s(ep %p %s, %d)\n", __func__, ep, ep->name, value);
  3699. if (index == 0) {
  3700. if (value)
  3701. dwc2_hsotg_stall_ep0(hs);
  3702. else
  3703. dev_warn(hs->dev,
  3704. "%s: can't clear halt on ep0\n", __func__);
  3705. return 0;
  3706. }
  3707. if (hs_ep->isochronous) {
  3708. dev_err(hs->dev, "%s is Isochronous Endpoint\n", ep->name);
  3709. return -EINVAL;
  3710. }
  3711. if (!now && value && !list_empty(&hs_ep->queue)) {
  3712. dev_dbg(hs->dev, "%s request is pending, cannot halt\n",
  3713. ep->name);
  3714. return -EAGAIN;
  3715. }
  3716. if (hs_ep->dir_in) {
  3717. epreg = DIEPCTL(index);
  3718. epctl = dwc2_readl(hs, epreg);
  3719. if (value) {
  3720. epctl |= DXEPCTL_STALL | DXEPCTL_SNAK;
  3721. if (epctl & DXEPCTL_EPENA)
  3722. epctl |= DXEPCTL_EPDIS;
  3723. } else {
  3724. epctl &= ~DXEPCTL_STALL;
  3725. hs_ep->wedged = 0;
  3726. xfertype = epctl & DXEPCTL_EPTYPE_MASK;
  3727. if (xfertype == DXEPCTL_EPTYPE_BULK ||
  3728. xfertype == DXEPCTL_EPTYPE_INTERRUPT)
  3729. epctl |= DXEPCTL_SETD0PID;
  3730. }
  3731. dwc2_writel(hs, epctl, epreg);
  3732. } else {
  3733. epreg = DOEPCTL(index);
  3734. epctl = dwc2_readl(hs, epreg);
  3735. if (value) {
  3736. /* Unmask GOUTNAKEFF interrupt */
  3737. dwc2_hsotg_en_gsint(hs, GINTSTS_GOUTNAKEFF);
  3738. if (!(dwc2_readl(hs, GINTSTS) & GINTSTS_GOUTNAKEFF))
  3739. dwc2_set_bit(hs, DCTL, DCTL_SGOUTNAK);
  3740. // STALL bit will be set in GOUTNAKEFF interrupt handler
  3741. } else {
  3742. epctl &= ~DXEPCTL_STALL;
  3743. hs_ep->wedged = 0;
  3744. xfertype = epctl & DXEPCTL_EPTYPE_MASK;
  3745. if (xfertype == DXEPCTL_EPTYPE_BULK ||
  3746. xfertype == DXEPCTL_EPTYPE_INTERRUPT)
  3747. epctl |= DXEPCTL_SETD0PID;
  3748. dwc2_writel(hs, epctl, epreg);
  3749. }
  3750. }
  3751. hs_ep->halted = value;
  3752. return 0;
  3753. }
  3754. /**
  3755. * dwc2_hsotg_ep_sethalt_lock - set halt on a given endpoint with lock held
  3756. * @ep: The endpoint to set halt.
  3757. * @value: Set or unset the halt.
  3758. */
  3759. static int dwc2_hsotg_ep_sethalt_lock(struct usb_ep *ep, int value)
  3760. {
  3761. struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
  3762. struct dwc2_hsotg *hs = hs_ep->parent;
  3763. unsigned long flags;
  3764. int ret;
  3765. spin_lock_irqsave(&hs->lock, flags);
  3766. ret = dwc2_hsotg_ep_sethalt(ep, value, false);
  3767. spin_unlock_irqrestore(&hs->lock, flags);
  3768. return ret;
  3769. }
  3770. static const struct usb_ep_ops dwc2_hsotg_ep_ops = {
  3771. .enable = dwc2_hsotg_ep_enable,
  3772. .disable = dwc2_hsotg_ep_disable_lock,
  3773. .alloc_request = dwc2_hsotg_ep_alloc_request,
  3774. .free_request = dwc2_hsotg_ep_free_request,
  3775. .queue = dwc2_hsotg_ep_queue_lock,
  3776. .dequeue = dwc2_hsotg_ep_dequeue,
  3777. .set_halt = dwc2_hsotg_ep_sethalt_lock,
  3778. .set_wedge = dwc2_gadget_ep_set_wedge,
  3779. /* note, don't believe we have any call for the fifo routines */
  3780. };
  3781. /**
  3782. * dwc2_hsotg_init - initialize the usb core
  3783. * @hsotg: The driver state
  3784. */
  3785. static void dwc2_hsotg_init(struct dwc2_hsotg *hsotg)
  3786. {
  3787. /* unmask subset of endpoint interrupts */
  3788. dwc2_writel(hsotg, DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK |
  3789. DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK,
  3790. DIEPMSK);
  3791. dwc2_writel(hsotg, DOEPMSK_SETUPMSK | DOEPMSK_AHBERRMSK |
  3792. DOEPMSK_EPDISBLDMSK | DOEPMSK_XFERCOMPLMSK,
  3793. DOEPMSK);
  3794. dwc2_writel(hsotg, 0, DAINTMSK);
  3795. /* Be in disconnected state until gadget is registered */
  3796. dwc2_set_bit(hsotg, DCTL, DCTL_SFTDISCON);
  3797. /* setup fifos */
  3798. dev_dbg(hsotg->dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
  3799. dwc2_readl(hsotg, GRXFSIZ),
  3800. dwc2_readl(hsotg, GNPTXFSIZ));
  3801. dwc2_hsotg_init_fifo(hsotg);
  3802. if (using_dma(hsotg))
  3803. dwc2_set_bit(hsotg, GAHBCFG, GAHBCFG_DMA_EN);
  3804. }
  3805. /**
  3806. * dwc2_hsotg_udc_start - prepare the udc for work
  3807. * @gadget: The usb gadget state
  3808. * @driver: The usb gadget driver
  3809. *
  3810. * Perform initialization to prepare udc device and driver
  3811. * to work.
  3812. */
  3813. static int dwc2_hsotg_udc_start(struct usb_gadget *gadget,
  3814. struct usb_gadget_driver *driver)
  3815. {
  3816. struct dwc2_hsotg *hsotg = to_hsotg(gadget);
  3817. unsigned long flags;
  3818. int ret;
  3819. if (!hsotg) {
  3820. pr_err("%s: called with no device\n", __func__);
  3821. return -ENODEV;
  3822. }
  3823. if (!driver) {
  3824. dev_err(hsotg->dev, "%s: no driver\n", __func__);
  3825. return -EINVAL;
  3826. }
  3827. if (driver->max_speed < USB_SPEED_FULL)
  3828. dev_err(hsotg->dev, "%s: bad speed\n", __func__);
  3829. if (!driver->setup) {
  3830. dev_err(hsotg->dev, "%s: missing entry points\n", __func__);
  3831. return -EINVAL;
  3832. }
  3833. WARN_ON(hsotg->driver);
  3834. hsotg->driver = driver;
  3835. hsotg->gadget.dev.of_node = hsotg->dev->of_node;
  3836. hsotg->gadget.speed = USB_SPEED_UNKNOWN;
  3837. if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) {
  3838. ret = dwc2_lowlevel_hw_enable(hsotg);
  3839. if (ret)
  3840. goto err;
  3841. }
  3842. if (!IS_ERR_OR_NULL(hsotg->uphy))
  3843. otg_set_peripheral(hsotg->uphy->otg, &hsotg->gadget);
  3844. spin_lock_irqsave(&hsotg->lock, flags);
  3845. if (dwc2_hw_is_device(hsotg)) {
  3846. dwc2_hsotg_init(hsotg);
  3847. dwc2_hsotg_core_init_disconnected(hsotg, false);
  3848. }
  3849. hsotg->enabled = 0;
  3850. spin_unlock_irqrestore(&hsotg->lock, flags);
  3851. gadget->sg_supported = using_desc_dma(hsotg);
  3852. dev_info(hsotg->dev, "bound driver %s\n", driver->driver.name);
  3853. return 0;
  3854. err:
  3855. hsotg->driver = NULL;
  3856. return ret;
  3857. }
  3858. /**
  3859. * dwc2_hsotg_udc_stop - stop the udc
  3860. * @gadget: The usb gadget state
  3861. *
  3862. * Stop udc hw block and stay tunned for future transmissions
  3863. */
  3864. static int dwc2_hsotg_udc_stop(struct usb_gadget *gadget)
  3865. {
  3866. struct dwc2_hsotg *hsotg = to_hsotg(gadget);
  3867. unsigned long flags;
  3868. int ep;
  3869. if (!hsotg)
  3870. return -ENODEV;
  3871. /* all endpoints should be shutdown */
  3872. for (ep = 1; ep < hsotg->num_of_eps; ep++) {
  3873. if (hsotg->eps_in[ep])
  3874. dwc2_hsotg_ep_disable_lock(&hsotg->eps_in[ep]->ep);
  3875. if (hsotg->eps_out[ep])
  3876. dwc2_hsotg_ep_disable_lock(&hsotg->eps_out[ep]->ep);
  3877. }
  3878. spin_lock_irqsave(&hsotg->lock, flags);
  3879. hsotg->driver = NULL;
  3880. hsotg->gadget.dev.of_node = NULL;
  3881. hsotg->gadget.speed = USB_SPEED_UNKNOWN;
  3882. hsotg->enabled = 0;
  3883. spin_unlock_irqrestore(&hsotg->lock, flags);
  3884. if (!IS_ERR_OR_NULL(hsotg->uphy))
  3885. otg_set_peripheral(hsotg->uphy->otg, NULL);
  3886. if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
  3887. dwc2_lowlevel_hw_disable(hsotg);
  3888. return 0;
  3889. }
  3890. /**
  3891. * dwc2_hsotg_gadget_getframe - read the frame number
  3892. * @gadget: The usb gadget state
  3893. *
  3894. * Read the {micro} frame number
  3895. */
  3896. static int dwc2_hsotg_gadget_getframe(struct usb_gadget *gadget)
  3897. {
  3898. return dwc2_hsotg_read_frameno(to_hsotg(gadget));
  3899. }
  3900. /**
  3901. * dwc2_hsotg_set_selfpowered - set if device is self/bus powered
  3902. * @gadget: The usb gadget state
  3903. * @is_selfpowered: Whether the device is self-powered
  3904. *
  3905. * Set if the device is self or bus powered.
  3906. */
  3907. static int dwc2_hsotg_set_selfpowered(struct usb_gadget *gadget,
  3908. int is_selfpowered)
  3909. {
  3910. struct dwc2_hsotg *hsotg = to_hsotg(gadget);
  3911. unsigned long flags;
  3912. spin_lock_irqsave(&hsotg->lock, flags);
  3913. gadget->is_selfpowered = !!is_selfpowered;
  3914. spin_unlock_irqrestore(&hsotg->lock, flags);
  3915. return 0;
  3916. }
  3917. /**
  3918. * dwc2_hsotg_pullup - connect/disconnect the USB PHY
  3919. * @gadget: The usb gadget state
  3920. * @is_on: Current state of the USB PHY
  3921. *
  3922. * Connect/Disconnect the USB PHY pullup
  3923. */
  3924. static int dwc2_hsotg_pullup(struct usb_gadget *gadget, int is_on)
  3925. {
  3926. struct dwc2_hsotg *hsotg = to_hsotg(gadget);
  3927. unsigned long flags;
  3928. dev_dbg(hsotg->dev, "%s: is_on: %d op_state: %d\n", __func__, is_on,
  3929. hsotg->op_state);
  3930. /* Don't modify pullup state while in host mode */
  3931. if (hsotg->op_state != OTG_STATE_B_PERIPHERAL) {
  3932. hsotg->enabled = is_on;
  3933. return 0;
  3934. }
  3935. spin_lock_irqsave(&hsotg->lock, flags);
  3936. if (is_on) {
  3937. hsotg->enabled = 1;
  3938. dwc2_hsotg_core_init_disconnected(hsotg, false);
  3939. /* Enable ACG feature in device mode,if supported */
  3940. dwc2_enable_acg(hsotg);
  3941. dwc2_hsotg_core_connect(hsotg);
  3942. } else {
  3943. dwc2_hsotg_core_disconnect(hsotg);
  3944. dwc2_hsotg_disconnect(hsotg);
  3945. hsotg->enabled = 0;
  3946. }
  3947. hsotg->gadget.speed = USB_SPEED_UNKNOWN;
  3948. spin_unlock_irqrestore(&hsotg->lock, flags);
  3949. return 0;
  3950. }
  3951. static int dwc2_hsotg_vbus_session(struct usb_gadget *gadget, int is_active)
  3952. {
  3953. struct dwc2_hsotg *hsotg = to_hsotg(gadget);
  3954. unsigned long flags;
  3955. dev_dbg(hsotg->dev, "%s: is_active: %d\n", __func__, is_active);
  3956. spin_lock_irqsave(&hsotg->lock, flags);
  3957. /*
  3958. * If controller is in partial power down state, it must exit from
  3959. * that state before being initialized / de-initialized
  3960. */
  3961. if (hsotg->lx_state == DWC2_L2 && hsotg->in_ppd)
  3962. /*
  3963. * No need to check the return value as
  3964. * registers are not being restored.
  3965. */
  3966. dwc2_exit_partial_power_down(hsotg, 0, false);
  3967. if (is_active) {
  3968. hsotg->op_state = OTG_STATE_B_PERIPHERAL;
  3969. dwc2_hsotg_core_init_disconnected(hsotg, false);
  3970. if (hsotg->enabled) {
  3971. /* Enable ACG feature in device mode,if supported */
  3972. dwc2_enable_acg(hsotg);
  3973. dwc2_hsotg_core_connect(hsotg);
  3974. }
  3975. } else {
  3976. dwc2_hsotg_core_disconnect(hsotg);
  3977. dwc2_hsotg_disconnect(hsotg);
  3978. }
  3979. spin_unlock_irqrestore(&hsotg->lock, flags);
  3980. return 0;
  3981. }
  3982. /**
  3983. * dwc2_hsotg_vbus_draw - report bMaxPower field
  3984. * @gadget: The usb gadget state
  3985. * @mA: Amount of current
  3986. *
  3987. * Report how much power the device may consume to the phy.
  3988. */
  3989. static int dwc2_hsotg_vbus_draw(struct usb_gadget *gadget, unsigned int mA)
  3990. {
  3991. struct dwc2_hsotg *hsotg = to_hsotg(gadget);
  3992. if (IS_ERR_OR_NULL(hsotg->uphy))
  3993. return -ENOTSUPP;
  3994. return usb_phy_set_power(hsotg->uphy, mA);
  3995. }
  3996. static void dwc2_gadget_set_speed(struct usb_gadget *g, enum usb_device_speed speed)
  3997. {
  3998. struct dwc2_hsotg *hsotg = to_hsotg(g);
  3999. unsigned long flags;
  4000. spin_lock_irqsave(&hsotg->lock, flags);
  4001. switch (speed) {
  4002. case USB_SPEED_HIGH:
  4003. hsotg->params.speed = DWC2_SPEED_PARAM_HIGH;
  4004. break;
  4005. case USB_SPEED_FULL:
  4006. hsotg->params.speed = DWC2_SPEED_PARAM_FULL;
  4007. break;
  4008. case USB_SPEED_LOW:
  4009. hsotg->params.speed = DWC2_SPEED_PARAM_LOW;
  4010. break;
  4011. default:
  4012. dev_err(hsotg->dev, "invalid speed (%d)\n", speed);
  4013. }
  4014. spin_unlock_irqrestore(&hsotg->lock, flags);
  4015. }
  4016. static const struct usb_gadget_ops dwc2_hsotg_gadget_ops = {
  4017. .get_frame = dwc2_hsotg_gadget_getframe,
  4018. .set_selfpowered = dwc2_hsotg_set_selfpowered,
  4019. .udc_start = dwc2_hsotg_udc_start,
  4020. .udc_stop = dwc2_hsotg_udc_stop,
  4021. .pullup = dwc2_hsotg_pullup,
  4022. .udc_set_speed = dwc2_gadget_set_speed,
  4023. .vbus_session = dwc2_hsotg_vbus_session,
  4024. .vbus_draw = dwc2_hsotg_vbus_draw,
  4025. };
  4026. /**
  4027. * dwc2_hsotg_initep - initialise a single endpoint
  4028. * @hsotg: The device state.
  4029. * @hs_ep: The endpoint to be initialised.
  4030. * @epnum: The endpoint number
  4031. * @dir_in: True if direction is in.
  4032. *
  4033. * Initialise the given endpoint (as part of the probe and device state
  4034. * creation) to give to the gadget driver. Setup the endpoint name, any
  4035. * direction information and other state that may be required.
  4036. */
  4037. static void dwc2_hsotg_initep(struct dwc2_hsotg *hsotg,
  4038. struct dwc2_hsotg_ep *hs_ep,
  4039. int epnum,
  4040. bool dir_in)
  4041. {
  4042. char *dir;
  4043. if (epnum == 0)
  4044. dir = "";
  4045. else if (dir_in)
  4046. dir = "in";
  4047. else
  4048. dir = "out";
  4049. hs_ep->dir_in = dir_in;
  4050. hs_ep->index = epnum;
  4051. snprintf(hs_ep->name, sizeof(hs_ep->name), "ep%d%s", epnum, dir);
  4052. printk("%s:%d name:%s dir:%d\n", __func__, __LINE__, hs_ep->name, dir_in);
  4053. INIT_LIST_HEAD(&hs_ep->queue);
  4054. INIT_LIST_HEAD(&hs_ep->ep.ep_list);
  4055. /* add to the list of endpoints known by the gadget driver */
  4056. if (epnum)
  4057. list_add_tail(&hs_ep->ep.ep_list, &hsotg->gadget.ep_list);
  4058. hs_ep->parent = hsotg;
  4059. hs_ep->ep.name = hs_ep->name;
  4060. if (hsotg->params.speed == DWC2_SPEED_PARAM_LOW)
  4061. usb_ep_set_maxpacket_limit(&hs_ep->ep, 8);
  4062. else
  4063. usb_ep_set_maxpacket_limit(&hs_ep->ep,
  4064. epnum ? 1024 : EP0_MPS_LIMIT);
  4065. hs_ep->ep.ops = &dwc2_hsotg_ep_ops;
  4066. if (epnum == 0) {
  4067. hs_ep->ep.caps.type_control = true;
  4068. } else {
  4069. if (hsotg->params.speed != DWC2_SPEED_PARAM_LOW) {
  4070. hs_ep->ep.caps.type_iso = true;
  4071. hs_ep->ep.caps.type_bulk = true;
  4072. }
  4073. hs_ep->ep.caps.type_int = true;
  4074. }
  4075. if (dir_in)
  4076. hs_ep->ep.caps.dir_in = true;
  4077. else
  4078. hs_ep->ep.caps.dir_out = true;
  4079. /*
  4080. * if we're using dma, we need to set the next-endpoint pointer
  4081. * to be something valid.
  4082. */
  4083. if (using_dma(hsotg)) {
  4084. u32 next = DXEPCTL_NEXTEP((epnum + 1) % 15);
  4085. if (dir_in)
  4086. dwc2_writel(hsotg, next, DIEPCTL(epnum));
  4087. else
  4088. dwc2_writel(hsotg, next, DOEPCTL(epnum));
  4089. }
  4090. }
  4091. /**
  4092. * dwc2_hsotg_hw_cfg - read HW configuration registers
  4093. * @hsotg: Programming view of the DWC_otg controller
  4094. *
  4095. * Read the USB core HW configuration registers
  4096. */
  4097. static int dwc2_hsotg_hw_cfg(struct dwc2_hsotg *hsotg)
  4098. {
  4099. u32 cfg;
  4100. u32 ep_type;
  4101. u32 i;
  4102. /* check hardware configuration */
  4103. hsotg->num_of_eps = hsotg->hw_params.num_dev_ep;
  4104. /* Add ep0 */
  4105. hsotg->num_of_eps++;
  4106. hsotg->eps_in[0] = devm_kzalloc(hsotg->dev,
  4107. sizeof(struct dwc2_hsotg_ep),
  4108. GFP_KERNEL);
  4109. if (!hsotg->eps_in[0])
  4110. return -ENOMEM;
  4111. /* Same dwc2_hsotg_ep is used in both directions for ep0 */
  4112. hsotg->eps_out[0] = hsotg->eps_in[0];
  4113. cfg = hsotg->hw_params.dev_ep_dirs;
  4114. for (i = 1, cfg >>= 2; i < hsotg->num_of_eps; i++, cfg >>= 2) {
  4115. ep_type = cfg & 3;
  4116. /* Direction in or both */
  4117. if (!(ep_type & 2)) {
  4118. hsotg->eps_in[i] = devm_kzalloc(hsotg->dev,
  4119. sizeof(struct dwc2_hsotg_ep), GFP_KERNEL);
  4120. if (!hsotg->eps_in[i])
  4121. return -ENOMEM;
  4122. }
  4123. /* Direction out or both */
  4124. if (!(ep_type & 1)) {
  4125. hsotg->eps_out[i] = devm_kzalloc(hsotg->dev,
  4126. sizeof(struct dwc2_hsotg_ep), GFP_KERNEL);
  4127. if (!hsotg->eps_out[i])
  4128. return -ENOMEM;
  4129. }
  4130. }
  4131. hsotg->fifo_mem = hsotg->hw_params.total_fifo_size;
  4132. hsotg->dedicated_fifos = hsotg->hw_params.en_multiple_tx_fifo;
  4133. dev_info(hsotg->dev, "EPs: %d, %s fifos, %d entries in SPRAM\n",
  4134. hsotg->num_of_eps,
  4135. hsotg->dedicated_fifos ? "dedicated" : "shared",
  4136. hsotg->fifo_mem);
  4137. return 0;
  4138. }
  4139. /**
  4140. * dwc2_hsotg_dump - dump state of the udc
  4141. * @hsotg: Programming view of the DWC_otg controller
  4142. *
  4143. */
  4144. static void dwc2_hsotg_dump(struct dwc2_hsotg *hsotg)
  4145. {
  4146. #ifdef DEBUG
  4147. struct device *dev = hsotg->dev;
  4148. u32 val;
  4149. int idx;
  4150. dev_info(dev, "DCFG=0x%08x, DCTL=0x%08x, DIEPMSK=%08x\n",
  4151. dwc2_readl(hsotg, DCFG), dwc2_readl(hsotg, DCTL),
  4152. dwc2_readl(hsotg, DIEPMSK));
  4153. dev_info(dev, "GAHBCFG=0x%08x, GHWCFG1=0x%08x\n",
  4154. dwc2_readl(hsotg, GAHBCFG), dwc2_readl(hsotg, GHWCFG1));
  4155. dev_info(dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
  4156. dwc2_readl(hsotg, GRXFSIZ), dwc2_readl(hsotg, GNPTXFSIZ));
  4157. /* show periodic fifo settings */
  4158. for (idx = 1; idx < hsotg->num_of_eps; idx++) {
  4159. val = dwc2_readl(hsotg, DPTXFSIZN(idx));
  4160. dev_info(dev, "DPTx[%d] FSize=%d, StAddr=0x%08x\n", idx,
  4161. val >> FIFOSIZE_DEPTH_SHIFT,
  4162. val & FIFOSIZE_STARTADDR_MASK);
  4163. }
  4164. for (idx = 0; idx < hsotg->num_of_eps; idx++) {
  4165. dev_info(dev,
  4166. "ep%d-in: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n", idx,
  4167. dwc2_readl(hsotg, DIEPCTL(idx)),
  4168. dwc2_readl(hsotg, DIEPTSIZ(idx)),
  4169. dwc2_readl(hsotg, DIEPDMA(idx)));
  4170. val = dwc2_readl(hsotg, DOEPCTL(idx));
  4171. dev_info(dev,
  4172. "ep%d-out: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n",
  4173. idx, dwc2_readl(hsotg, DOEPCTL(idx)),
  4174. dwc2_readl(hsotg, DOEPTSIZ(idx)),
  4175. dwc2_readl(hsotg, DOEPDMA(idx)));
  4176. }
  4177. dev_info(dev, "DVBUSDIS=0x%08x, DVBUSPULSE=%08x\n",
  4178. dwc2_readl(hsotg, DVBUSDIS), dwc2_readl(hsotg, DVBUSPULSE));
  4179. #endif
  4180. }
  4181. /**
  4182. * dwc2_gadget_init - init function for gadget
  4183. * @hsotg: Programming view of the DWC_otg controller
  4184. *
  4185. */
  4186. int dwc2_gadget_init(struct dwc2_hsotg *hsotg)
  4187. {
  4188. struct device *dev = hsotg->dev;
  4189. int epnum;
  4190. int ret;
  4191. /* Dump fifo information */
  4192. dev_dbg(dev, "NonPeriodic TXFIFO size: %d\n",
  4193. hsotg->params.g_np_tx_fifo_size);
  4194. dev_dbg(dev, "RXFIFO size: %d\n", hsotg->params.g_rx_fifo_size);
  4195. switch (hsotg->params.speed) {
  4196. case DWC2_SPEED_PARAM_LOW:
  4197. hsotg->gadget.max_speed = USB_SPEED_LOW;
  4198. break;
  4199. case DWC2_SPEED_PARAM_FULL:
  4200. hsotg->gadget.max_speed = USB_SPEED_FULL;
  4201. break;
  4202. default:
  4203. hsotg->gadget.max_speed = USB_SPEED_HIGH;
  4204. break;
  4205. }
  4206. hsotg->gadget.ops = &dwc2_hsotg_gadget_ops;
  4207. hsotg->gadget.name = dev_name(dev);
  4208. hsotg->gadget.otg_caps = &hsotg->params.otg_caps;
  4209. hsotg->remote_wakeup_allowed = 0;
  4210. if (hsotg->params.lpm)
  4211. hsotg->gadget.lpm_capable = true;
  4212. if (hsotg->dr_mode == USB_DR_MODE_OTG)
  4213. hsotg->gadget.is_otg = 1;
  4214. else if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
  4215. hsotg->op_state = OTG_STATE_B_PERIPHERAL;
  4216. ret = dwc2_hsotg_hw_cfg(hsotg);
  4217. if (ret) {
  4218. dev_err(hsotg->dev, "Hardware configuration failed: %d\n", ret);
  4219. return ret;
  4220. }
  4221. hsotg->ctrl_buff = devm_kzalloc(hsotg->dev,
  4222. DWC2_CTRL_BUFF_SIZE, GFP_KERNEL);
  4223. if (!hsotg->ctrl_buff)
  4224. return -ENOMEM;
  4225. hsotg->ep0_buff = devm_kzalloc(hsotg->dev,
  4226. DWC2_CTRL_BUFF_SIZE, GFP_KERNEL);
  4227. if (!hsotg->ep0_buff)
  4228. return -ENOMEM;
  4229. if (using_desc_dma(hsotg)) {
  4230. ret = dwc2_gadget_alloc_ctrl_desc_chains(hsotg);
  4231. if (ret < 0)
  4232. return ret;
  4233. }
  4234. ret = devm_request_irq(hsotg->dev, hsotg->irq, dwc2_hsotg_irq,
  4235. IRQF_SHARED, dev_name(hsotg->dev), hsotg);
  4236. if (ret < 0) {
  4237. dev_err(dev, "cannot claim IRQ for gadget\n");
  4238. return ret;
  4239. }
  4240. /* hsotg->num_of_eps holds number of EPs other than ep0 */
  4241. if (hsotg->num_of_eps == 0) {
  4242. dev_err(dev, "wrong number of EPs (zero)\n");
  4243. return -EINVAL;
  4244. }
  4245. /* setup endpoint information */
  4246. INIT_LIST_HEAD(&hsotg->gadget.ep_list);
  4247. hsotg->gadget.ep0 = &hsotg->eps_out[0]->ep;
  4248. /* allocate EP0 request */
  4249. hsotg->ctrl_req = dwc2_hsotg_ep_alloc_request(&hsotg->eps_out[0]->ep,
  4250. GFP_KERNEL);
  4251. if (!hsotg->ctrl_req) {
  4252. dev_err(dev, "failed to allocate ctrl req\n");
  4253. return -ENOMEM;
  4254. }
  4255. /* initialise the endpoints now the core has been initialised */
  4256. for (epnum = 0; epnum < hsotg->num_of_eps; epnum++) {
  4257. if (hsotg->eps_in[epnum] && (epnum % 2))
  4258. dwc2_hsotg_initep(hsotg, hsotg->eps_in[epnum],
  4259. epnum, 1);
  4260. else if (epnum)
  4261. hsotg->eps_in[epnum] = NULL;
  4262. if (hsotg->eps_out[epnum] && (!(epnum % 2)))
  4263. dwc2_hsotg_initep(hsotg, hsotg->eps_out[epnum],
  4264. epnum, 0);
  4265. else if (epnum)
  4266. hsotg->eps_out[epnum] = NULL;
  4267. }
  4268. dwc2_hsotg_dump(hsotg);
  4269. return 0;
  4270. }
  4271. /**
  4272. * dwc2_hsotg_remove - remove function for hsotg driver
  4273. * @hsotg: Programming view of the DWC_otg controller
  4274. *
  4275. */
  4276. int dwc2_hsotg_remove(struct dwc2_hsotg *hsotg)
  4277. {
  4278. usb_del_gadget_udc(&hsotg->gadget);
  4279. dwc2_hsotg_ep_free_request(&hsotg->eps_out[0]->ep, hsotg->ctrl_req);
  4280. return 0;
  4281. }
  4282. int dwc2_hsotg_suspend(struct dwc2_hsotg *hsotg)
  4283. {
  4284. unsigned long flags;
  4285. if (hsotg->lx_state != DWC2_L0)
  4286. return 0;
  4287. if (hsotg->driver) {
  4288. int ep;
  4289. dev_info(hsotg->dev, "suspending usb gadget %s\n",
  4290. hsotg->driver->driver.name);
  4291. spin_lock_irqsave(&hsotg->lock, flags);
  4292. if (hsotg->enabled)
  4293. dwc2_hsotg_core_disconnect(hsotg);
  4294. dwc2_hsotg_disconnect(hsotg);
  4295. hsotg->gadget.speed = USB_SPEED_UNKNOWN;
  4296. spin_unlock_irqrestore(&hsotg->lock, flags);
  4297. for (ep = 1; ep < hsotg->num_of_eps; ep++) {
  4298. if (hsotg->eps_in[ep])
  4299. dwc2_hsotg_ep_disable_lock(&hsotg->eps_in[ep]->ep);
  4300. if (hsotg->eps_out[ep])
  4301. dwc2_hsotg_ep_disable_lock(&hsotg->eps_out[ep]->ep);
  4302. }
  4303. }
  4304. return 0;
  4305. }
  4306. int dwc2_hsotg_resume(struct dwc2_hsotg *hsotg)
  4307. {
  4308. unsigned long flags;
  4309. if (hsotg->lx_state == DWC2_L2)
  4310. return 0;
  4311. if (hsotg->driver) {
  4312. dev_info(hsotg->dev, "resuming usb gadget %s\n",
  4313. hsotg->driver->driver.name);
  4314. spin_lock_irqsave(&hsotg->lock, flags);
  4315. dwc2_hsotg_core_init_disconnected(hsotg, false);
  4316. if (hsotg->enabled) {
  4317. /* Enable ACG feature in device mode,if supported */
  4318. dwc2_enable_acg(hsotg);
  4319. dwc2_hsotg_core_connect(hsotg);
  4320. }
  4321. spin_unlock_irqrestore(&hsotg->lock, flags);
  4322. }
  4323. return 0;
  4324. }
  4325. /**
  4326. * dwc2_backup_device_registers() - Backup controller device registers.
  4327. * When suspending usb bus, registers needs to be backuped
  4328. * if controller power is disabled once suspended.
  4329. *
  4330. * @hsotg: Programming view of the DWC_otg controller
  4331. */
  4332. int dwc2_backup_device_registers(struct dwc2_hsotg *hsotg)
  4333. {
  4334. struct dwc2_dregs_backup *dr;
  4335. int i;
  4336. dev_dbg(hsotg->dev, "%s\n", __func__);
  4337. /* Backup dev regs */
  4338. dr = &hsotg->dr_backup;
  4339. dr->dcfg = dwc2_readl(hsotg, DCFG);
  4340. dr->dctl = dwc2_readl(hsotg, DCTL);
  4341. dr->daintmsk = dwc2_readl(hsotg, DAINTMSK);
  4342. dr->diepmsk = dwc2_readl(hsotg, DIEPMSK);
  4343. dr->doepmsk = dwc2_readl(hsotg, DOEPMSK);
  4344. for (i = 0; i < hsotg->num_of_eps; i++) {
  4345. /* Backup IN EPs */
  4346. dr->diepctl[i] = dwc2_readl(hsotg, DIEPCTL(i));
  4347. /* Ensure DATA PID is correctly configured */
  4348. if (dr->diepctl[i] & DXEPCTL_DPID)
  4349. dr->diepctl[i] |= DXEPCTL_SETD1PID;
  4350. else
  4351. dr->diepctl[i] |= DXEPCTL_SETD0PID;
  4352. dr->dieptsiz[i] = dwc2_readl(hsotg, DIEPTSIZ(i));
  4353. dr->diepdma[i] = dwc2_readl(hsotg, DIEPDMA(i));
  4354. /* Backup OUT EPs */
  4355. dr->doepctl[i] = dwc2_readl(hsotg, DOEPCTL(i));
  4356. /* Ensure DATA PID is correctly configured */
  4357. if (dr->doepctl[i] & DXEPCTL_DPID)
  4358. dr->doepctl[i] |= DXEPCTL_SETD1PID;
  4359. else
  4360. dr->doepctl[i] |= DXEPCTL_SETD0PID;
  4361. dr->doeptsiz[i] = dwc2_readl(hsotg, DOEPTSIZ(i));
  4362. dr->doepdma[i] = dwc2_readl(hsotg, DOEPDMA(i));
  4363. dr->dtxfsiz[i] = dwc2_readl(hsotg, DPTXFSIZN(i));
  4364. }
  4365. dr->valid = true;
  4366. return 0;
  4367. }
  4368. /**
  4369. * dwc2_restore_device_registers() - Restore controller device registers.
  4370. * When resuming usb bus, device registers needs to be restored
  4371. * if controller power were disabled.
  4372. *
  4373. * @hsotg: Programming view of the DWC_otg controller
  4374. * @remote_wakeup: Indicates whether resume is initiated by Device or Host.
  4375. *
  4376. * Return: 0 if successful, negative error code otherwise
  4377. */
  4378. int dwc2_restore_device_registers(struct dwc2_hsotg *hsotg, int remote_wakeup)
  4379. {
  4380. struct dwc2_dregs_backup *dr;
  4381. int i;
  4382. dev_dbg(hsotg->dev, "%s\n", __func__);
  4383. /* Restore dev regs */
  4384. dr = &hsotg->dr_backup;
  4385. if (!dr->valid) {
  4386. dev_err(hsotg->dev, "%s: no device registers to restore\n",
  4387. __func__);
  4388. return -EINVAL;
  4389. }
  4390. dr->valid = false;
  4391. if (!remote_wakeup)
  4392. dwc2_writel(hsotg, dr->dctl, DCTL);
  4393. dwc2_writel(hsotg, dr->daintmsk, DAINTMSK);
  4394. dwc2_writel(hsotg, dr->diepmsk, DIEPMSK);
  4395. dwc2_writel(hsotg, dr->doepmsk, DOEPMSK);
  4396. for (i = 0; i < hsotg->num_of_eps; i++) {
  4397. /* Restore IN EPs */
  4398. dwc2_writel(hsotg, dr->dieptsiz[i], DIEPTSIZ(i));
  4399. dwc2_writel(hsotg, dr->diepdma[i], DIEPDMA(i));
  4400. dwc2_writel(hsotg, dr->doeptsiz[i], DOEPTSIZ(i));
  4401. /** WA for enabled EPx's IN in DDMA mode. On entering to
  4402. * hibernation wrong value read and saved from DIEPDMAx,
  4403. * as result BNA interrupt asserted on hibernation exit
  4404. * by restoring from saved area.
  4405. */
  4406. if (using_desc_dma(hsotg) &&
  4407. (dr->diepctl[i] & DXEPCTL_EPENA))
  4408. dr->diepdma[i] = hsotg->eps_in[i]->desc_list_dma;
  4409. dwc2_writel(hsotg, dr->dtxfsiz[i], DPTXFSIZN(i));
  4410. dwc2_writel(hsotg, dr->diepctl[i], DIEPCTL(i));
  4411. /* Restore OUT EPs */
  4412. dwc2_writel(hsotg, dr->doeptsiz[i], DOEPTSIZ(i));
  4413. /* WA for enabled EPx's OUT in DDMA mode. On entering to
  4414. * hibernation wrong value read and saved from DOEPDMAx,
  4415. * as result BNA interrupt asserted on hibernation exit
  4416. * by restoring from saved area.
  4417. */
  4418. if (using_desc_dma(hsotg) &&
  4419. (dr->doepctl[i] & DXEPCTL_EPENA))
  4420. dr->doepdma[i] = hsotg->eps_out[i]->desc_list_dma;
  4421. dwc2_writel(hsotg, dr->doepdma[i], DOEPDMA(i));
  4422. dwc2_writel(hsotg, dr->doepctl[i], DOEPCTL(i));
  4423. }
  4424. return 0;
  4425. }
  4426. /**
  4427. * dwc2_gadget_init_lpm - Configure the core to support LPM in device mode
  4428. *
  4429. * @hsotg: Programming view of DWC_otg controller
  4430. *
  4431. */
  4432. void dwc2_gadget_init_lpm(struct dwc2_hsotg *hsotg)
  4433. {
  4434. u32 val;
  4435. if (!hsotg->params.lpm)
  4436. return;
  4437. val = GLPMCFG_LPMCAP | GLPMCFG_APPL1RES;
  4438. val |= hsotg->params.hird_threshold_en ? GLPMCFG_HIRD_THRES_EN : 0;
  4439. val |= hsotg->params.lpm_clock_gating ? GLPMCFG_ENBLSLPM : 0;
  4440. val |= hsotg->params.hird_threshold << GLPMCFG_HIRD_THRES_SHIFT;
  4441. val |= hsotg->params.besl ? GLPMCFG_ENBESL : 0;
  4442. val |= GLPMCFG_LPM_REJECT_CTRL_CONTROL;
  4443. val |= GLPMCFG_LPM_ACCEPT_CTRL_ISOC;
  4444. dwc2_writel(hsotg, val, GLPMCFG);
  4445. dev_dbg(hsotg->dev, "GLPMCFG=0x%08x\n", dwc2_readl(hsotg, GLPMCFG));
  4446. /* Unmask WKUP_ALERT Interrupt */
  4447. if (hsotg->params.service_interval)
  4448. dwc2_set_bit(hsotg, GINTMSK2, GINTMSK2_WKUP_ALERT_INT_MSK);
  4449. }
  4450. /**
  4451. * dwc2_gadget_program_ref_clk - Program GREFCLK register in device mode
  4452. *
  4453. * @hsotg: Programming view of DWC_otg controller
  4454. *
  4455. */
  4456. void dwc2_gadget_program_ref_clk(struct dwc2_hsotg *hsotg)
  4457. {
  4458. u32 val = 0;
  4459. val |= GREFCLK_REF_CLK_MODE;
  4460. val |= hsotg->params.ref_clk_per << GREFCLK_REFCLKPER_SHIFT;
  4461. val |= hsotg->params.sof_cnt_wkup_alert <<
  4462. GREFCLK_SOF_CNT_WKUP_ALERT_SHIFT;
  4463. dwc2_writel(hsotg, val, GREFCLK);
  4464. dev_dbg(hsotg->dev, "GREFCLK=0x%08x\n", dwc2_readl(hsotg, GREFCLK));
  4465. }
  4466. /**
  4467. * dwc2_gadget_enter_hibernation() - Put controller in Hibernation.
  4468. *
  4469. * @hsotg: Programming view of the DWC_otg controller
  4470. *
  4471. * Return non-zero if failed to enter to hibernation.
  4472. */
  4473. int dwc2_gadget_enter_hibernation(struct dwc2_hsotg *hsotg)
  4474. {
  4475. u32 gpwrdn;
  4476. u32 gusbcfg;
  4477. u32 pcgcctl;
  4478. int ret = 0;
  4479. /* Change to L2(suspend) state */
  4480. hsotg->lx_state = DWC2_L2;
  4481. dev_dbg(hsotg->dev, "Start of hibernation completed\n");
  4482. ret = dwc2_backup_global_registers(hsotg);
  4483. if (ret) {
  4484. dev_err(hsotg->dev, "%s: failed to backup global registers\n",
  4485. __func__);
  4486. return ret;
  4487. }
  4488. ret = dwc2_backup_device_registers(hsotg);
  4489. if (ret) {
  4490. dev_err(hsotg->dev, "%s: failed to backup device registers\n",
  4491. __func__);
  4492. return ret;
  4493. }
  4494. gpwrdn = GPWRDN_PWRDNRSTN;
  4495. udelay(10);
  4496. gusbcfg = dwc2_readl(hsotg, GUSBCFG);
  4497. if (gusbcfg & GUSBCFG_ULPI_UTMI_SEL) {
  4498. /* ULPI interface */
  4499. gpwrdn |= GPWRDN_ULPI_LATCH_EN_DURING_HIB_ENTRY;
  4500. }
  4501. dwc2_writel(hsotg, gpwrdn, GPWRDN);
  4502. udelay(10);
  4503. /* Suspend the Phy Clock */
  4504. pcgcctl = dwc2_readl(hsotg, PCGCTL);
  4505. pcgcctl |= PCGCTL_STOPPCLK;
  4506. dwc2_writel(hsotg, pcgcctl, PCGCTL);
  4507. udelay(10);
  4508. gpwrdn = dwc2_readl(hsotg, GPWRDN);
  4509. gpwrdn |= GPWRDN_PMUACTV;
  4510. dwc2_writel(hsotg, gpwrdn, GPWRDN);
  4511. udelay(10);
  4512. /* Set flag to indicate that we are in hibernation */
  4513. hsotg->hibernated = 1;
  4514. /* Enable interrupts from wake up logic */
  4515. gpwrdn = dwc2_readl(hsotg, GPWRDN);
  4516. gpwrdn |= GPWRDN_PMUINTSEL;
  4517. dwc2_writel(hsotg, gpwrdn, GPWRDN);
  4518. udelay(10);
  4519. /* Unmask device mode interrupts in GPWRDN */
  4520. gpwrdn = dwc2_readl(hsotg, GPWRDN);
  4521. gpwrdn |= GPWRDN_RST_DET_MSK;
  4522. gpwrdn |= GPWRDN_LNSTSCHG_MSK;
  4523. gpwrdn |= GPWRDN_STS_CHGINT_MSK;
  4524. dwc2_writel(hsotg, gpwrdn, GPWRDN);
  4525. udelay(10);
  4526. /* Enable Power Down Clamp */
  4527. gpwrdn = dwc2_readl(hsotg, GPWRDN);
  4528. gpwrdn |= GPWRDN_PWRDNCLMP;
  4529. dwc2_writel(hsotg, gpwrdn, GPWRDN);
  4530. udelay(10);
  4531. /* Switch off VDD */
  4532. gpwrdn = dwc2_readl(hsotg, GPWRDN);
  4533. gpwrdn |= GPWRDN_PWRDNSWTCH;
  4534. dwc2_writel(hsotg, gpwrdn, GPWRDN);
  4535. udelay(10);
  4536. /* Save gpwrdn register for further usage if stschng interrupt */
  4537. hsotg->gr_backup.gpwrdn = dwc2_readl(hsotg, GPWRDN);
  4538. dev_dbg(hsotg->dev, "Hibernation completed\n");
  4539. return ret;
  4540. }
  4541. /**
  4542. * dwc2_gadget_exit_hibernation()
  4543. * This function is for exiting from Device mode hibernation by host initiated
  4544. * resume/reset and device initiated remote-wakeup.
  4545. *
  4546. * @hsotg: Programming view of the DWC_otg controller
  4547. * @rem_wakeup: indicates whether resume is initiated by Device or Host.
  4548. * @reset: indicates whether resume is initiated by Reset.
  4549. *
  4550. * Return non-zero if failed to exit from hibernation.
  4551. */
  4552. int dwc2_gadget_exit_hibernation(struct dwc2_hsotg *hsotg,
  4553. int rem_wakeup, int reset)
  4554. {
  4555. u32 pcgcctl;
  4556. u32 gpwrdn;
  4557. u32 dctl;
  4558. int ret = 0;
  4559. struct dwc2_gregs_backup *gr;
  4560. struct dwc2_dregs_backup *dr;
  4561. gr = &hsotg->gr_backup;
  4562. dr = &hsotg->dr_backup;
  4563. if (!hsotg->hibernated) {
  4564. dev_dbg(hsotg->dev, "Already exited from Hibernation\n");
  4565. return 1;
  4566. }
  4567. dev_dbg(hsotg->dev,
  4568. "%s: called with rem_wakeup = %d reset = %d\n",
  4569. __func__, rem_wakeup, reset);
  4570. dwc2_hib_restore_common(hsotg, rem_wakeup, 0);
  4571. if (!reset) {
  4572. /* Clear all pending interupts */
  4573. dwc2_writel(hsotg, 0xffffffff, GINTSTS);
  4574. }
  4575. /* De-assert Restore */
  4576. gpwrdn = dwc2_readl(hsotg, GPWRDN);
  4577. gpwrdn &= ~GPWRDN_RESTORE;
  4578. dwc2_writel(hsotg, gpwrdn, GPWRDN);
  4579. udelay(10);
  4580. if (!rem_wakeup) {
  4581. pcgcctl = dwc2_readl(hsotg, PCGCTL);
  4582. pcgcctl &= ~PCGCTL_RSTPDWNMODULE;
  4583. dwc2_writel(hsotg, pcgcctl, PCGCTL);
  4584. }
  4585. /* Restore GUSBCFG, DCFG and DCTL */
  4586. dwc2_writel(hsotg, gr->gusbcfg, GUSBCFG);
  4587. dwc2_writel(hsotg, dr->dcfg, DCFG);
  4588. dwc2_writel(hsotg, dr->dctl, DCTL);
  4589. /* On USB Reset, reset device address to zero */
  4590. if (reset)
  4591. dwc2_clear_bit(hsotg, DCFG, DCFG_DEVADDR_MASK);
  4592. /* Reset ULPI latch */
  4593. gpwrdn = dwc2_readl(hsotg, GPWRDN);
  4594. gpwrdn &= ~GPWRDN_ULPI_LATCH_EN_DURING_HIB_ENTRY;
  4595. dwc2_writel(hsotg, gpwrdn, GPWRDN);
  4596. /* De-assert Wakeup Logic */
  4597. gpwrdn = dwc2_readl(hsotg, GPWRDN);
  4598. gpwrdn &= ~GPWRDN_PMUACTV;
  4599. dwc2_writel(hsotg, gpwrdn, GPWRDN);
  4600. if (rem_wakeup) {
  4601. udelay(10);
  4602. /* Start Remote Wakeup Signaling */
  4603. dwc2_writel(hsotg, dr->dctl | DCTL_RMTWKUPSIG, DCTL);
  4604. } else {
  4605. udelay(50);
  4606. /* Set Device programming done bit */
  4607. dctl = dwc2_readl(hsotg, DCTL);
  4608. dctl |= DCTL_PWRONPRGDONE;
  4609. dwc2_writel(hsotg, dctl, DCTL);
  4610. }
  4611. /* Wait for interrupts which must be cleared */
  4612. mdelay(2);
  4613. /* Clear all pending interupts */
  4614. dwc2_writel(hsotg, 0xffffffff, GINTSTS);
  4615. /* Restore global registers */
  4616. ret = dwc2_restore_global_registers(hsotg);
  4617. if (ret) {
  4618. dev_err(hsotg->dev, "%s: failed to restore registers\n",
  4619. __func__);
  4620. return ret;
  4621. }
  4622. /* Restore device registers */
  4623. ret = dwc2_restore_device_registers(hsotg, rem_wakeup);
  4624. if (ret) {
  4625. dev_err(hsotg->dev, "%s: failed to restore device registers\n",
  4626. __func__);
  4627. return ret;
  4628. }
  4629. if (rem_wakeup) {
  4630. mdelay(10);
  4631. dctl = dwc2_readl(hsotg, DCTL);
  4632. dctl &= ~DCTL_RMTWKUPSIG;
  4633. dwc2_writel(hsotg, dctl, DCTL);
  4634. }
  4635. hsotg->hibernated = 0;
  4636. hsotg->lx_state = DWC2_L0;
  4637. dev_dbg(hsotg->dev, "Hibernation recovery completes here\n");
  4638. return ret;
  4639. }
  4640. /**
  4641. * dwc2_gadget_enter_partial_power_down() - Put controller in partial
  4642. * power down.
  4643. *
  4644. * @hsotg: Programming view of the DWC_otg controller
  4645. *
  4646. * Return: non-zero if failed to enter device partial power down.
  4647. *
  4648. * This function is for entering device mode partial power down.
  4649. */
  4650. int dwc2_gadget_enter_partial_power_down(struct dwc2_hsotg *hsotg)
  4651. {
  4652. u32 pcgcctl;
  4653. int ret = 0;
  4654. dev_dbg(hsotg->dev, "Entering device partial power down started.\n");
  4655. /* Backup all registers */
  4656. ret = dwc2_backup_global_registers(hsotg);
  4657. if (ret) {
  4658. dev_err(hsotg->dev, "%s: failed to backup global registers\n",
  4659. __func__);
  4660. return ret;
  4661. }
  4662. ret = dwc2_backup_device_registers(hsotg);
  4663. if (ret) {
  4664. dev_err(hsotg->dev, "%s: failed to backup device registers\n",
  4665. __func__);
  4666. return ret;
  4667. }
  4668. /*
  4669. * Clear any pending interrupts since dwc2 will not be able to
  4670. * clear them after entering partial_power_down.
  4671. */
  4672. dwc2_writel(hsotg, 0xffffffff, GINTSTS);
  4673. /* Put the controller in low power state */
  4674. pcgcctl = dwc2_readl(hsotg, PCGCTL);
  4675. pcgcctl |= PCGCTL_PWRCLMP;
  4676. dwc2_writel(hsotg, pcgcctl, PCGCTL);
  4677. udelay(5);
  4678. pcgcctl |= PCGCTL_RSTPDWNMODULE;
  4679. dwc2_writel(hsotg, pcgcctl, PCGCTL);
  4680. udelay(5);
  4681. pcgcctl |= PCGCTL_STOPPCLK;
  4682. dwc2_writel(hsotg, pcgcctl, PCGCTL);
  4683. /* Set in_ppd flag to 1 as here core enters suspend. */
  4684. hsotg->in_ppd = 1;
  4685. hsotg->lx_state = DWC2_L2;
  4686. dev_dbg(hsotg->dev, "Entering device partial power down completed.\n");
  4687. return ret;
  4688. }
  4689. /*
  4690. * dwc2_gadget_exit_partial_power_down() - Exit controller from device partial
  4691. * power down.
  4692. *
  4693. * @hsotg: Programming view of the DWC_otg controller
  4694. * @restore: indicates whether need to restore the registers or not.
  4695. *
  4696. * Return: non-zero if failed to exit device partial power down.
  4697. *
  4698. * This function is for exiting from device mode partial power down.
  4699. */
  4700. int dwc2_gadget_exit_partial_power_down(struct dwc2_hsotg *hsotg,
  4701. bool restore)
  4702. {
  4703. u32 pcgcctl;
  4704. u32 dctl;
  4705. struct dwc2_dregs_backup *dr;
  4706. int ret = 0;
  4707. dr = &hsotg->dr_backup;
  4708. dev_dbg(hsotg->dev, "Exiting device partial Power Down started.\n");
  4709. pcgcctl = dwc2_readl(hsotg, PCGCTL);
  4710. pcgcctl &= ~PCGCTL_STOPPCLK;
  4711. dwc2_writel(hsotg, pcgcctl, PCGCTL);
  4712. pcgcctl = dwc2_readl(hsotg, PCGCTL);
  4713. pcgcctl &= ~PCGCTL_PWRCLMP;
  4714. dwc2_writel(hsotg, pcgcctl, PCGCTL);
  4715. pcgcctl = dwc2_readl(hsotg, PCGCTL);
  4716. pcgcctl &= ~PCGCTL_RSTPDWNMODULE;
  4717. dwc2_writel(hsotg, pcgcctl, PCGCTL);
  4718. udelay(100);
  4719. if (restore) {
  4720. ret = dwc2_restore_global_registers(hsotg);
  4721. if (ret) {
  4722. dev_err(hsotg->dev, "%s: failed to restore registers\n",
  4723. __func__);
  4724. return ret;
  4725. }
  4726. /* Restore DCFG */
  4727. dwc2_writel(hsotg, dr->dcfg, DCFG);
  4728. ret = dwc2_restore_device_registers(hsotg, 0);
  4729. if (ret) {
  4730. dev_err(hsotg->dev, "%s: failed to restore device registers\n",
  4731. __func__);
  4732. return ret;
  4733. }
  4734. }
  4735. /* Set the Power-On Programming done bit */
  4736. dctl = dwc2_readl(hsotg, DCTL);
  4737. dctl |= DCTL_PWRONPRGDONE;
  4738. dwc2_writel(hsotg, dctl, DCTL);
  4739. /* Set in_ppd flag to 0 as here core exits from suspend. */
  4740. hsotg->in_ppd = 0;
  4741. hsotg->lx_state = DWC2_L0;
  4742. dev_dbg(hsotg->dev, "Exiting device partial Power Down completed.\n");
  4743. return ret;
  4744. }
  4745. /**
  4746. * dwc2_gadget_enter_clock_gating() - Put controller in clock gating.
  4747. *
  4748. * @hsotg: Programming view of the DWC_otg controller
  4749. *
  4750. * Return: non-zero if failed to enter device partial power down.
  4751. *
  4752. * This function is for entering device mode clock gating.
  4753. */
  4754. void dwc2_gadget_enter_clock_gating(struct dwc2_hsotg *hsotg)
  4755. {
  4756. u32 pcgctl;
  4757. dev_dbg(hsotg->dev, "Entering device clock gating.\n");
  4758. /* Set the Phy Clock bit as suspend is received. */
  4759. pcgctl = dwc2_readl(hsotg, PCGCTL);
  4760. pcgctl |= PCGCTL_STOPPCLK;
  4761. dwc2_writel(hsotg, pcgctl, PCGCTL);
  4762. udelay(5);
  4763. /* Set the Gate hclk as suspend is received. */
  4764. pcgctl = dwc2_readl(hsotg, PCGCTL);
  4765. pcgctl |= PCGCTL_GATEHCLK;
  4766. dwc2_writel(hsotg, pcgctl, PCGCTL);
  4767. udelay(5);
  4768. hsotg->lx_state = DWC2_L2;
  4769. hsotg->bus_suspended = true;
  4770. }
  4771. /*
  4772. * dwc2_gadget_exit_clock_gating() - Exit controller from device clock gating.
  4773. *
  4774. * @hsotg: Programming view of the DWC_otg controller
  4775. * @rem_wakeup: indicates whether remote wake up is enabled.
  4776. *
  4777. * This function is for exiting from device mode clock gating.
  4778. */
  4779. void dwc2_gadget_exit_clock_gating(struct dwc2_hsotg *hsotg, int rem_wakeup)
  4780. {
  4781. u32 pcgctl;
  4782. u32 dctl;
  4783. dev_dbg(hsotg->dev, "Exiting device clock gating.\n");
  4784. /* Clear the Gate hclk. */
  4785. pcgctl = dwc2_readl(hsotg, PCGCTL);
  4786. pcgctl &= ~PCGCTL_GATEHCLK;
  4787. dwc2_writel(hsotg, pcgctl, PCGCTL);
  4788. udelay(5);
  4789. /* Phy Clock bit. */
  4790. pcgctl = dwc2_readl(hsotg, PCGCTL);
  4791. pcgctl &= ~PCGCTL_STOPPCLK;
  4792. dwc2_writel(hsotg, pcgctl, PCGCTL);
  4793. udelay(5);
  4794. if (rem_wakeup) {
  4795. /* Set Remote Wakeup Signaling */
  4796. dctl = dwc2_readl(hsotg, DCTL);
  4797. dctl |= DCTL_RMTWKUPSIG;
  4798. dwc2_writel(hsotg, dctl, DCTL);
  4799. }
  4800. /* Change to L0 state */
  4801. call_gadget(hsotg, resume);
  4802. hsotg->lx_state = DWC2_L0;
  4803. hsotg->bus_suspended = false;
  4804. }