gadget.c 146 KB

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