gadget.c 134 KB

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