gadget.c 135 KB

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