renesas_usbf.c 87 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Renesas USBF USB Function driver
  4. *
  5. * Copyright 2022 Schneider Electric
  6. * Author: Herve Codina <herve.codina@bootlin.com>
  7. */
  8. #include <linux/delay.h>
  9. #include <linux/dma-mapping.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/iopoll.h>
  12. #include <linux/kernel.h>
  13. #include <linux/kfifo.h>
  14. #include <linux/mod_devicetable.h>
  15. #include <linux/module.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/pm_runtime.h>
  18. #include <linux/types.h>
  19. #include <linux/usb/composite.h>
  20. #include <linux/usb/gadget.h>
  21. #include <linux/usb/role.h>
  22. #define USBF_NUM_ENDPOINTS 16
  23. #define USBF_EP0_MAX_PCKT_SIZE 64
  24. /* EPC registers */
  25. #define USBF_REG_USB_CONTROL 0x000
  26. #define USBF_USB_PUE2 BIT(2)
  27. #define USBF_USB_CONNECTB BIT(3)
  28. #define USBF_USB_DEFAULT BIT(4)
  29. #define USBF_USB_CONF BIT(5)
  30. #define USBF_USB_SUSPEND BIT(6)
  31. #define USBF_USB_RSUM_IN BIT(7)
  32. #define USBF_USB_SOF_RCV BIT(8)
  33. #define USBF_USB_FORCEFS BIT(9)
  34. #define USBF_USB_INT_SEL BIT(10)
  35. #define USBF_USB_SOF_CLK_MODE BIT(11)
  36. #define USBF_REG_USB_STATUS 0x004
  37. #define USBF_USB_RSUM_OUT BIT(1)
  38. #define USBF_USB_SPND_OUT BIT(2)
  39. #define USBF_USB_USB_RST BIT(3)
  40. #define USBF_USB_DEFAULT_ST BIT(4)
  41. #define USBF_USB_CONF_ST BIT(5)
  42. #define USBF_USB_SPEED_MODE BIT(6)
  43. #define USBF_USB_SOF_DELAY_STATUS BIT(31)
  44. #define USBF_REG_USB_ADDRESS 0x008
  45. #define USBF_USB_SOF_STATUS BIT(15)
  46. #define USBF_USB_SET_USB_ADDR(_a) ((_a) << 16)
  47. #define USBF_USB_GET_FRAME(_r) ((_r) & 0x7FF)
  48. #define USBF_REG_SETUP_DATA0 0x018
  49. #define USBF_REG_SETUP_DATA1 0x01C
  50. #define USBF_REG_USB_INT_STA 0x020
  51. #define USBF_USB_RSUM_INT BIT(1)
  52. #define USBF_USB_SPND_INT BIT(2)
  53. #define USBF_USB_USB_RST_INT BIT(3)
  54. #define USBF_USB_SOF_INT BIT(4)
  55. #define USBF_USB_SOF_ERROR_INT BIT(5)
  56. #define USBF_USB_SPEED_MODE_INT BIT(6)
  57. #define USBF_USB_EPN_INT(_n) (BIT(8) << (_n)) /* n=0..15 */
  58. #define USBF_REG_USB_INT_ENA 0x024
  59. #define USBF_USB_RSUM_EN BIT(1)
  60. #define USBF_USB_SPND_EN BIT(2)
  61. #define USBF_USB_USB_RST_EN BIT(3)
  62. #define USBF_USB_SOF_EN BIT(4)
  63. #define USBF_USB_SOF_ERROR_EN BIT(5)
  64. #define USBF_USB_SPEED_MODE_EN BIT(6)
  65. #define USBF_USB_EPN_EN(_n) (BIT(8) << (_n)) /* n=0..15 */
  66. #define USBF_BASE_EP0 0x028
  67. /* EP0 registers offsets from Base + USBF_BASE_EP0 (EP0 regs area) */
  68. #define USBF_REG_EP0_CONTROL 0x00
  69. #define USBF_EP0_ONAK BIT(0)
  70. #define USBF_EP0_INAK BIT(1)
  71. #define USBF_EP0_STL BIT(2)
  72. #define USBF_EP0_PERR_NAK_CLR BIT(3)
  73. #define USBF_EP0_INAK_EN BIT(4)
  74. #define USBF_EP0_DW_MASK (0x3 << 5)
  75. #define USBF_EP0_DW(_s) ((_s) << 5)
  76. #define USBF_EP0_DEND BIT(7)
  77. #define USBF_EP0_BCLR BIT(8)
  78. #define USBF_EP0_PIDCLR BIT(9)
  79. #define USBF_EP0_AUTO BIT(16)
  80. #define USBF_EP0_OVERSEL BIT(17)
  81. #define USBF_EP0_STGSEL BIT(18)
  82. #define USBF_REG_EP0_STATUS 0x04
  83. #define USBF_EP0_SETUP_INT BIT(0)
  84. #define USBF_EP0_STG_START_INT BIT(1)
  85. #define USBF_EP0_STG_END_INT BIT(2)
  86. #define USBF_EP0_STALL_INT BIT(3)
  87. #define USBF_EP0_IN_INT BIT(4)
  88. #define USBF_EP0_OUT_INT BIT(5)
  89. #define USBF_EP0_OUT_OR_INT BIT(6)
  90. #define USBF_EP0_OUT_NULL_INT BIT(7)
  91. #define USBF_EP0_IN_EMPTY BIT(8)
  92. #define USBF_EP0_IN_FULL BIT(9)
  93. #define USBF_EP0_IN_DATA BIT(10)
  94. #define USBF_EP0_IN_NAK_INT BIT(11)
  95. #define USBF_EP0_OUT_EMPTY BIT(12)
  96. #define USBF_EP0_OUT_FULL BIT(13)
  97. #define USBF_EP0_OUT_NULL BIT(14)
  98. #define USBF_EP0_OUT_NAK_INT BIT(15)
  99. #define USBF_EP0_PERR_NAK_INT BIT(16)
  100. #define USBF_EP0_PERR_NAK BIT(17)
  101. #define USBF_EP0_PID BIT(18)
  102. #define USBF_REG_EP0_INT_ENA 0x08
  103. #define USBF_EP0_SETUP_EN BIT(0)
  104. #define USBF_EP0_STG_START_EN BIT(1)
  105. #define USBF_EP0_STG_END_EN BIT(2)
  106. #define USBF_EP0_STALL_EN BIT(3)
  107. #define USBF_EP0_IN_EN BIT(4)
  108. #define USBF_EP0_OUT_EN BIT(5)
  109. #define USBF_EP0_OUT_OR_EN BIT(6)
  110. #define USBF_EP0_OUT_NULL_EN BIT(7)
  111. #define USBF_EP0_IN_NAK_EN BIT(11)
  112. #define USBF_EP0_OUT_NAK_EN BIT(15)
  113. #define USBF_EP0_PERR_NAK_EN BIT(16)
  114. #define USBF_REG_EP0_LENGTH 0x0C
  115. #define USBF_EP0_LDATA (0x7FF << 0)
  116. #define USBF_REG_EP0_READ 0x10
  117. #define USBF_REG_EP0_WRITE 0x14
  118. #define USBF_BASE_EPN(_n) (0x040 + (_n) * 0x020)
  119. /* EPn registers offsets from Base + USBF_BASE_EPN(n-1). n=1..15 */
  120. #define USBF_REG_EPN_CONTROL 0x000
  121. #define USBF_EPN_ONAK BIT(0)
  122. #define USBF_EPN_OSTL BIT(2)
  123. #define USBF_EPN_ISTL BIT(3)
  124. #define USBF_EPN_OSTL_EN BIT(4)
  125. #define USBF_EPN_DW_MASK (0x3 << 5)
  126. #define USBF_EPN_DW(_s) ((_s) << 5)
  127. #define USBF_EPN_DEND BIT(7)
  128. #define USBF_EPN_CBCLR BIT(8)
  129. #define USBF_EPN_BCLR BIT(9)
  130. #define USBF_EPN_OPIDCLR BIT(10)
  131. #define USBF_EPN_IPIDCLR BIT(11)
  132. #define USBF_EPN_AUTO BIT(16)
  133. #define USBF_EPN_OVERSEL BIT(17)
  134. #define USBF_EPN_MODE_MASK (0x3 << 24)
  135. #define USBF_EPN_MODE_BULK (0x0 << 24)
  136. #define USBF_EPN_MODE_INTR (0x1 << 24)
  137. #define USBF_EPN_MODE_ISO (0x2 << 24)
  138. #define USBF_EPN_DIR0 BIT(26)
  139. #define USBF_EPN_BUF_TYPE_DOUBLE BIT(30)
  140. #define USBF_EPN_EN BIT(31)
  141. #define USBF_REG_EPN_STATUS 0x004
  142. #define USBF_EPN_IN_EMPTY BIT(0)
  143. #define USBF_EPN_IN_FULL BIT(1)
  144. #define USBF_EPN_IN_DATA BIT(2)
  145. #define USBF_EPN_IN_INT BIT(3)
  146. #define USBF_EPN_IN_STALL_INT BIT(4)
  147. #define USBF_EPN_IN_NAK_ERR_INT BIT(5)
  148. #define USBF_EPN_IN_END_INT BIT(7)
  149. #define USBF_EPN_IPID BIT(10)
  150. #define USBF_EPN_OUT_EMPTY BIT(16)
  151. #define USBF_EPN_OUT_FULL BIT(17)
  152. #define USBF_EPN_OUT_NULL_INT BIT(18)
  153. #define USBF_EPN_OUT_INT BIT(19)
  154. #define USBF_EPN_OUT_STALL_INT BIT(20)
  155. #define USBF_EPN_OUT_NAK_ERR_INT BIT(21)
  156. #define USBF_EPN_OUT_OR_INT BIT(22)
  157. #define USBF_EPN_OUT_END_INT BIT(23)
  158. #define USBF_EPN_ISO_CRC BIT(24)
  159. #define USBF_EPN_ISO_OR BIT(26)
  160. #define USBF_EPN_OUT_NOTKN BIT(27)
  161. #define USBF_EPN_ISO_OPID BIT(28)
  162. #define USBF_EPN_ISO_PIDERR BIT(29)
  163. #define USBF_REG_EPN_INT_ENA 0x008
  164. #define USBF_EPN_IN_EN BIT(3)
  165. #define USBF_EPN_IN_STALL_EN BIT(4)
  166. #define USBF_EPN_IN_NAK_ERR_EN BIT(5)
  167. #define USBF_EPN_IN_END_EN BIT(7)
  168. #define USBF_EPN_OUT_NULL_EN BIT(18)
  169. #define USBF_EPN_OUT_EN BIT(19)
  170. #define USBF_EPN_OUT_STALL_EN BIT(20)
  171. #define USBF_EPN_OUT_NAK_ERR_EN BIT(21)
  172. #define USBF_EPN_OUT_OR_EN BIT(22)
  173. #define USBF_EPN_OUT_END_EN BIT(23)
  174. #define USBF_REG_EPN_DMA_CTRL 0x00C
  175. #define USBF_EPN_DMAMODE0 BIT(0)
  176. #define USBF_EPN_DMA_EN BIT(4)
  177. #define USBF_EPN_STOP_SET BIT(8)
  178. #define USBF_EPN_BURST_SET BIT(9)
  179. #define USBF_EPN_DEND_SET BIT(10)
  180. #define USBF_EPN_STOP_MODE BIT(11)
  181. #define USBF_REG_EPN_PCKT_ADRS 0x010
  182. #define USBF_EPN_MPKT(_l) ((_l) << 0)
  183. #define USBF_EPN_BASEAD(_a) ((_a) << 16)
  184. #define USBF_REG_EPN_LEN_DCNT 0x014
  185. #define USBF_EPN_GET_LDATA(_r) ((_r) & 0x7FF)
  186. #define USBF_EPN_SET_DMACNT(_c) ((_c) << 16)
  187. #define USBF_EPN_GET_DMACNT(_r) (((_r) >> 16) & 0x1ff)
  188. #define USBF_REG_EPN_READ 0x018
  189. #define USBF_REG_EPN_WRITE 0x01C
  190. /* AHB-EPC Bridge registers */
  191. #define USBF_REG_AHBSCTR 0x1000
  192. #define USBF_REG_AHBMCTR 0x1004
  193. #define USBF_SYS_WBURST_TYPE BIT(2)
  194. #define USBF_SYS_ARBITER_CTR BIT(31)
  195. #define USBF_REG_AHBBINT 0x1008
  196. #define USBF_SYS_ERR_MASTER (0x0F << 0)
  197. #define USBF_SYS_SBUS_ERRINT0 BIT(4)
  198. #define USBF_SYS_SBUS_ERRINT1 BIT(5)
  199. #define USBF_SYS_MBUS_ERRINT BIT(6)
  200. #define USBF_SYS_VBUS_INT BIT(13)
  201. #define USBF_SYS_DMA_ENDINT_EPN(_n) (BIT(16) << (_n)) /* _n=1..15 */
  202. #define USBF_REG_AHBBINTEN 0x100C
  203. #define USBF_SYS_SBUS_ERRINT0EN BIT(4)
  204. #define USBF_SYS_SBUS_ERRINT1EN BIT(5)
  205. #define USBF_SYS_MBUS_ERRINTEN BIT(6)
  206. #define USBF_SYS_VBUS_INTEN BIT(13)
  207. #define USBF_SYS_DMA_ENDINTEN_EPN(_n) (BIT(16) << (_n)) /* _n=1..15 */
  208. #define USBF_REG_EPCTR 0x1010
  209. #define USBF_SYS_EPC_RST BIT(0)
  210. #define USBF_SYS_PLL_RST BIT(2)
  211. #define USBF_SYS_PLL_LOCK BIT(4)
  212. #define USBF_SYS_PLL_RESUME BIT(5)
  213. #define USBF_SYS_VBUS_LEVEL BIT(8)
  214. #define USBF_SYS_DIRPD BIT(12)
  215. #define USBF_REG_USBSSVER 0x1020
  216. #define USBF_REG_USBSSCONF 0x1024
  217. #define USBF_SYS_DMA_AVAILABLE(_n) (BIT(0) << (_n)) /* _n=0..15 */
  218. #define USBF_SYS_EP_AVAILABLE(_n) (BIT(16) << (_n)) /* _n=0..15 */
  219. #define USBF_BASE_DMA_EPN(_n) (0x1110 + (_n) * 0x010)
  220. /* EPn DMA registers offsets from Base USBF_BASE_DMA_EPN(n-1). n=1..15*/
  221. #define USBF_REG_DMA_EPN_DCR1 0x00
  222. #define USBF_SYS_EPN_REQEN BIT(0)
  223. #define USBF_SYS_EPN_DIR0 BIT(1)
  224. #define USBF_SYS_EPN_SET_DMACNT(_c) ((_c) << 16)
  225. #define USBF_SYS_EPN_GET_DMACNT(_r) (((_r) >> 16) & 0x0FF)
  226. #define USBF_REG_DMA_EPN_DCR2 0x04
  227. #define USBF_SYS_EPN_MPKT(_s) ((_s) << 0)
  228. #define USBF_SYS_EPN_LMPKT(_l) ((_l) << 16)
  229. #define USBF_REG_DMA_EPN_TADR 0x08
  230. /* USB request */
  231. struct usbf_req {
  232. struct usb_request req;
  233. struct list_head queue;
  234. unsigned int is_zero_sent : 1;
  235. unsigned int is_mapped : 1;
  236. enum {
  237. USBF_XFER_START,
  238. USBF_XFER_WAIT_DMA,
  239. USBF_XFER_SEND_NULL,
  240. USBF_XFER_WAIT_END,
  241. USBF_XFER_WAIT_DMA_SHORT,
  242. USBF_XFER_WAIT_BRIDGE,
  243. } xfer_step;
  244. size_t dma_size;
  245. };
  246. /* USB Endpoint */
  247. struct usbf_ep {
  248. struct usb_ep ep;
  249. char name[32];
  250. struct list_head queue;
  251. unsigned int is_processing : 1;
  252. unsigned int is_in : 1;
  253. struct usbf_udc *udc;
  254. void __iomem *regs;
  255. void __iomem *dma_regs;
  256. unsigned int id : 8;
  257. unsigned int disabled : 1;
  258. unsigned int is_wedged : 1;
  259. unsigned int delayed_status : 1;
  260. u32 status;
  261. void (*bridge_on_dma_end)(struct usbf_ep *ep);
  262. };
  263. enum usbf_ep0state {
  264. EP0_IDLE,
  265. EP0_IN_DATA_PHASE,
  266. EP0_OUT_DATA_PHASE,
  267. EP0_OUT_STATUS_START_PHASE,
  268. EP0_OUT_STATUS_PHASE,
  269. EP0_OUT_STATUS_END_PHASE,
  270. EP0_IN_STATUS_START_PHASE,
  271. EP0_IN_STATUS_PHASE,
  272. EP0_IN_STATUS_END_PHASE,
  273. };
  274. struct usbf_udc {
  275. struct usb_gadget gadget;
  276. struct usb_gadget_driver *driver;
  277. struct device *dev;
  278. void __iomem *regs;
  279. spinlock_t lock;
  280. bool is_remote_wakeup;
  281. bool is_usb_suspended;
  282. struct usbf_ep ep[USBF_NUM_ENDPOINTS];
  283. /* for EP0 control messages */
  284. enum usbf_ep0state ep0state;
  285. struct usbf_req setup_reply;
  286. u8 ep0_buf[USBF_EP0_MAX_PCKT_SIZE];
  287. };
  288. struct usbf_ep_info {
  289. const char *name;
  290. struct usb_ep_caps caps;
  291. u16 base_addr;
  292. unsigned int is_double : 1;
  293. u16 maxpacket_limit;
  294. };
  295. #define USBF_SINGLE_BUFFER 0
  296. #define USBF_DOUBLE_BUFFER 1
  297. #define USBF_EP_INFO(_name, _caps, _base_addr, _is_double, _maxpacket_limit) \
  298. { \
  299. .name = _name, \
  300. .caps = _caps, \
  301. .base_addr = _base_addr, \
  302. .is_double = _is_double, \
  303. .maxpacket_limit = _maxpacket_limit, \
  304. }
  305. /* This table is computed from the recommended values provided in the SOC
  306. * datasheet. The buffer type (single/double) and the endpoint type cannot
  307. * be changed. The mapping in internal RAM (base_addr and number of words)
  308. * for each endpoints depends on the max packet size and the buffer type.
  309. */
  310. static const struct usbf_ep_info usbf_ep_info[USBF_NUM_ENDPOINTS] = {
  311. /* ep0: buf @0x0000 64 bytes, fixed 32 words */
  312. [0] = USBF_EP_INFO("ep0-ctrl",
  313. USB_EP_CAPS(USB_EP_CAPS_TYPE_CONTROL,
  314. USB_EP_CAPS_DIR_ALL),
  315. 0x0000, USBF_SINGLE_BUFFER, USBF_EP0_MAX_PCKT_SIZE),
  316. /* ep1: buf @0x0020, 2 buffers 512 bytes -> (512 * 2 / 4) words */
  317. [1] = USBF_EP_INFO("ep1-bulk",
  318. USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK,
  319. USB_EP_CAPS_DIR_ALL),
  320. 0x0020, USBF_DOUBLE_BUFFER, 512),
  321. /* ep2: buf @0x0120, 2 buffers 512 bytes -> (512 * 2 / 4) words */
  322. [2] = USBF_EP_INFO("ep2-bulk",
  323. USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK,
  324. USB_EP_CAPS_DIR_ALL),
  325. 0x0120, USBF_DOUBLE_BUFFER, 512),
  326. /* ep3: buf @0x0220, 1 buffer 512 bytes -> (512 * 2 / 4) words */
  327. [3] = USBF_EP_INFO("ep3-bulk",
  328. USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK,
  329. USB_EP_CAPS_DIR_ALL),
  330. 0x0220, USBF_SINGLE_BUFFER, 512),
  331. /* ep4: buf @0x02A0, 1 buffer 512 bytes -> (512 * 1 / 4) words */
  332. [4] = USBF_EP_INFO("ep4-bulk",
  333. USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK,
  334. USB_EP_CAPS_DIR_ALL),
  335. 0x02A0, USBF_SINGLE_BUFFER, 512),
  336. /* ep5: buf @0x0320, 1 buffer 512 bytes -> (512 * 2 / 4) words */
  337. [5] = USBF_EP_INFO("ep5-bulk",
  338. USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK,
  339. USB_EP_CAPS_DIR_ALL),
  340. 0x0320, USBF_SINGLE_BUFFER, 512),
  341. /* ep6: buf @0x03A0, 1 buffer 1024 bytes -> (1024 * 1 / 4) words */
  342. [6] = USBF_EP_INFO("ep6-int",
  343. USB_EP_CAPS(USB_EP_CAPS_TYPE_INT,
  344. USB_EP_CAPS_DIR_ALL),
  345. 0x03A0, USBF_SINGLE_BUFFER, 1024),
  346. /* ep7: buf @0x04A0, 1 buffer 1024 bytes -> (1024 * 1 / 4) words */
  347. [7] = USBF_EP_INFO("ep7-int",
  348. USB_EP_CAPS(USB_EP_CAPS_TYPE_INT,
  349. USB_EP_CAPS_DIR_ALL),
  350. 0x04A0, USBF_SINGLE_BUFFER, 1024),
  351. /* ep8: buf @0x0520, 1 buffer 1024 bytes -> (1024 * 1 / 4) words */
  352. [8] = USBF_EP_INFO("ep8-int",
  353. USB_EP_CAPS(USB_EP_CAPS_TYPE_INT,
  354. USB_EP_CAPS_DIR_ALL),
  355. 0x0520, USBF_SINGLE_BUFFER, 1024),
  356. /* ep9: buf @0x0620, 1 buffer 1024 bytes -> (1024 * 1 / 4) words */
  357. [9] = USBF_EP_INFO("ep9-int",
  358. USB_EP_CAPS(USB_EP_CAPS_TYPE_INT,
  359. USB_EP_CAPS_DIR_ALL),
  360. 0x0620, USBF_SINGLE_BUFFER, 1024),
  361. /* ep10: buf @0x0720, 2 buffers 1024 bytes -> (1024 * 2 / 4) words */
  362. [10] = USBF_EP_INFO("ep10-iso",
  363. USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO,
  364. USB_EP_CAPS_DIR_ALL),
  365. 0x0720, USBF_DOUBLE_BUFFER, 1024),
  366. /* ep11: buf @0x0920, 2 buffers 1024 bytes -> (1024 * 2 / 4) words */
  367. [11] = USBF_EP_INFO("ep11-iso",
  368. USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO,
  369. USB_EP_CAPS_DIR_ALL),
  370. 0x0920, USBF_DOUBLE_BUFFER, 1024),
  371. /* ep12: buf @0x0B20, 2 buffers 1024 bytes -> (1024 * 2 / 4) words */
  372. [12] = USBF_EP_INFO("ep12-iso",
  373. USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO,
  374. USB_EP_CAPS_DIR_ALL),
  375. 0x0B20, USBF_DOUBLE_BUFFER, 1024),
  376. /* ep13: buf @0x0D20, 2 buffers 1024 bytes -> (1024 * 2 / 4) words */
  377. [13] = USBF_EP_INFO("ep13-iso",
  378. USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO,
  379. USB_EP_CAPS_DIR_ALL),
  380. 0x0D20, USBF_DOUBLE_BUFFER, 1024),
  381. /* ep14: buf @0x0F20, 2 buffers 1024 bytes -> (1024 * 2 / 4) words */
  382. [14] = USBF_EP_INFO("ep14-iso",
  383. USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO,
  384. USB_EP_CAPS_DIR_ALL),
  385. 0x0F20, USBF_DOUBLE_BUFFER, 1024),
  386. /* ep15: buf @0x1120, 2 buffers 1024 bytes -> (1024 * 2 / 4) words */
  387. [15] = USBF_EP_INFO("ep15-iso",
  388. USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO,
  389. USB_EP_CAPS_DIR_ALL),
  390. 0x1120, USBF_DOUBLE_BUFFER, 1024),
  391. };
  392. static inline u32 usbf_reg_readl(struct usbf_udc *udc, uint offset)
  393. {
  394. return readl(udc->regs + offset);
  395. }
  396. static inline void usbf_reg_writel(struct usbf_udc *udc, uint offset, u32 val)
  397. {
  398. writel(val, udc->regs + offset);
  399. }
  400. static inline void usbf_reg_bitset(struct usbf_udc *udc, uint offset, u32 set)
  401. {
  402. u32 tmp;
  403. tmp = usbf_reg_readl(udc, offset);
  404. tmp |= set;
  405. usbf_reg_writel(udc, offset, tmp);
  406. }
  407. static inline void usbf_reg_bitclr(struct usbf_udc *udc, uint offset, u32 clr)
  408. {
  409. u32 tmp;
  410. tmp = usbf_reg_readl(udc, offset);
  411. tmp &= ~clr;
  412. usbf_reg_writel(udc, offset, tmp);
  413. }
  414. static inline void usbf_reg_clrset(struct usbf_udc *udc, uint offset,
  415. u32 clr, u32 set)
  416. {
  417. u32 tmp;
  418. tmp = usbf_reg_readl(udc, offset);
  419. tmp &= ~clr;
  420. tmp |= set;
  421. usbf_reg_writel(udc, offset, tmp);
  422. }
  423. static inline u32 usbf_ep_reg_readl(struct usbf_ep *ep, uint offset)
  424. {
  425. return readl(ep->regs + offset);
  426. }
  427. static inline void usbf_ep_reg_read_rep(struct usbf_ep *ep, uint offset,
  428. void *dst, uint count)
  429. {
  430. readsl(ep->regs + offset, dst, count);
  431. }
  432. static inline void usbf_ep_reg_writel(struct usbf_ep *ep, uint offset, u32 val)
  433. {
  434. writel(val, ep->regs + offset);
  435. }
  436. static inline void usbf_ep_reg_write_rep(struct usbf_ep *ep, uint offset,
  437. const void *src, uint count)
  438. {
  439. writesl(ep->regs + offset, src, count);
  440. }
  441. static inline void usbf_ep_reg_bitset(struct usbf_ep *ep, uint offset, u32 set)
  442. {
  443. u32 tmp;
  444. tmp = usbf_ep_reg_readl(ep, offset);
  445. tmp |= set;
  446. usbf_ep_reg_writel(ep, offset, tmp);
  447. }
  448. static inline void usbf_ep_reg_bitclr(struct usbf_ep *ep, uint offset, u32 clr)
  449. {
  450. u32 tmp;
  451. tmp = usbf_ep_reg_readl(ep, offset);
  452. tmp &= ~clr;
  453. usbf_ep_reg_writel(ep, offset, tmp);
  454. }
  455. static inline void usbf_ep_reg_clrset(struct usbf_ep *ep, uint offset,
  456. u32 clr, u32 set)
  457. {
  458. u32 tmp;
  459. tmp = usbf_ep_reg_readl(ep, offset);
  460. tmp &= ~clr;
  461. tmp |= set;
  462. usbf_ep_reg_writel(ep, offset, tmp);
  463. }
  464. static inline u32 usbf_ep_dma_reg_readl(struct usbf_ep *ep, uint offset)
  465. {
  466. return readl(ep->dma_regs + offset);
  467. }
  468. static inline void usbf_ep_dma_reg_writel(struct usbf_ep *ep, uint offset,
  469. u32 val)
  470. {
  471. writel(val, ep->dma_regs + offset);
  472. }
  473. static inline void usbf_ep_dma_reg_bitset(struct usbf_ep *ep, uint offset,
  474. u32 set)
  475. {
  476. u32 tmp;
  477. tmp = usbf_ep_dma_reg_readl(ep, offset);
  478. tmp |= set;
  479. usbf_ep_dma_reg_writel(ep, offset, tmp);
  480. }
  481. static inline void usbf_ep_dma_reg_bitclr(struct usbf_ep *ep, uint offset,
  482. u32 clr)
  483. {
  484. u32 tmp;
  485. tmp = usbf_ep_dma_reg_readl(ep, offset);
  486. tmp &= ~clr;
  487. usbf_ep_dma_reg_writel(ep, offset, tmp);
  488. }
  489. static void usbf_ep0_send_null(struct usbf_ep *ep0, bool is_data1)
  490. {
  491. u32 set;
  492. set = USBF_EP0_DEND;
  493. if (is_data1)
  494. set |= USBF_EP0_PIDCLR;
  495. usbf_ep_reg_bitset(ep0, USBF_REG_EP0_CONTROL, set);
  496. }
  497. static int usbf_ep0_pio_in(struct usbf_ep *ep0, struct usbf_req *req)
  498. {
  499. unsigned int left;
  500. unsigned int nb;
  501. const void *buf;
  502. u32 ctrl;
  503. u32 last;
  504. left = req->req.length - req->req.actual;
  505. if (left == 0) {
  506. if (!req->is_zero_sent) {
  507. if (req->req.length == 0) {
  508. dev_dbg(ep0->udc->dev, "ep0 send null\n");
  509. usbf_ep0_send_null(ep0, false);
  510. req->is_zero_sent = 1;
  511. return -EINPROGRESS;
  512. }
  513. if ((req->req.actual % ep0->ep.maxpacket) == 0) {
  514. if (req->req.zero) {
  515. dev_dbg(ep0->udc->dev, "ep0 send null\n");
  516. usbf_ep0_send_null(ep0, false);
  517. req->is_zero_sent = 1;
  518. return -EINPROGRESS;
  519. }
  520. }
  521. }
  522. return 0;
  523. }
  524. if (left > ep0->ep.maxpacket)
  525. left = ep0->ep.maxpacket;
  526. buf = req->req.buf;
  527. buf += req->req.actual;
  528. nb = left / sizeof(u32);
  529. if (nb) {
  530. usbf_ep_reg_write_rep(ep0, USBF_REG_EP0_WRITE, buf, nb);
  531. buf += (nb * sizeof(u32));
  532. req->req.actual += (nb * sizeof(u32));
  533. left -= (nb * sizeof(u32));
  534. }
  535. ctrl = usbf_ep_reg_readl(ep0, USBF_REG_EP0_CONTROL);
  536. ctrl &= ~USBF_EP0_DW_MASK;
  537. if (left) {
  538. memcpy(&last, buf, left);
  539. usbf_ep_reg_writel(ep0, USBF_REG_EP0_WRITE, last);
  540. ctrl |= USBF_EP0_DW(left);
  541. req->req.actual += left;
  542. }
  543. usbf_ep_reg_writel(ep0, USBF_REG_EP0_CONTROL, ctrl | USBF_EP0_DEND);
  544. dev_dbg(ep0->udc->dev, "ep0 send %u/%u\n",
  545. req->req.actual, req->req.length);
  546. return -EINPROGRESS;
  547. }
  548. static int usbf_ep0_pio_out(struct usbf_ep *ep0, struct usbf_req *req)
  549. {
  550. int req_status = 0;
  551. unsigned int count;
  552. unsigned int recv;
  553. unsigned int left;
  554. unsigned int nb;
  555. void *buf;
  556. u32 last;
  557. if (ep0->status & USBF_EP0_OUT_INT) {
  558. recv = usbf_ep_reg_readl(ep0, USBF_REG_EP0_LENGTH) & USBF_EP0_LDATA;
  559. count = recv;
  560. buf = req->req.buf;
  561. buf += req->req.actual;
  562. left = req->req.length - req->req.actual;
  563. dev_dbg(ep0->udc->dev, "ep0 recv %u, left %u\n", count, left);
  564. if (left > ep0->ep.maxpacket)
  565. left = ep0->ep.maxpacket;
  566. if (count > left) {
  567. req_status = -EOVERFLOW;
  568. count = left;
  569. }
  570. if (count) {
  571. nb = count / sizeof(u32);
  572. if (nb) {
  573. usbf_ep_reg_read_rep(ep0, USBF_REG_EP0_READ,
  574. buf, nb);
  575. buf += (nb * sizeof(u32));
  576. req->req.actual += (nb * sizeof(u32));
  577. count -= (nb * sizeof(u32));
  578. }
  579. if (count) {
  580. last = usbf_ep_reg_readl(ep0, USBF_REG_EP0_READ);
  581. memcpy(buf, &last, count);
  582. req->req.actual += count;
  583. }
  584. }
  585. dev_dbg(ep0->udc->dev, "ep0 recv %u/%u\n",
  586. req->req.actual, req->req.length);
  587. if (req_status) {
  588. dev_dbg(ep0->udc->dev, "ep0 req.status=%d\n", req_status);
  589. req->req.status = req_status;
  590. return 0;
  591. }
  592. if (recv < ep0->ep.maxpacket) {
  593. dev_dbg(ep0->udc->dev, "ep0 short packet\n");
  594. /* This is a short packet -> It is the end */
  595. req->req.status = 0;
  596. return 0;
  597. }
  598. /* The Data stage of a control transfer from an endpoint to the
  599. * host is complete when the endpoint does one of the following:
  600. * - Has transferred exactly the expected amount of data
  601. * - Transfers a packet with a payload size less than
  602. * wMaxPacketSize or transfers a zero-length packet
  603. */
  604. if (req->req.actual == req->req.length) {
  605. req->req.status = 0;
  606. return 0;
  607. }
  608. }
  609. if (ep0->status & USBF_EP0_OUT_NULL_INT) {
  610. /* NULL packet received */
  611. dev_dbg(ep0->udc->dev, "ep0 null packet\n");
  612. if (req->req.actual != req->req.length) {
  613. req->req.status = req->req.short_not_ok ?
  614. -EREMOTEIO : 0;
  615. } else {
  616. req->req.status = 0;
  617. }
  618. return 0;
  619. }
  620. return -EINPROGRESS;
  621. }
  622. static void usbf_ep0_fifo_flush(struct usbf_ep *ep0)
  623. {
  624. u32 sts;
  625. int ret;
  626. usbf_ep_reg_bitset(ep0, USBF_REG_EP0_CONTROL, USBF_EP0_BCLR);
  627. ret = readl_poll_timeout_atomic(ep0->regs + USBF_REG_EP0_STATUS, sts,
  628. (sts & (USBF_EP0_IN_DATA | USBF_EP0_IN_EMPTY)) == USBF_EP0_IN_EMPTY,
  629. 0, 10000);
  630. if (ret)
  631. dev_err(ep0->udc->dev, "ep0 flush fifo timed out\n");
  632. }
  633. static void usbf_epn_send_null(struct usbf_ep *epn)
  634. {
  635. usbf_ep_reg_bitset(epn, USBF_REG_EPN_CONTROL, USBF_EPN_DEND);
  636. }
  637. static void usbf_epn_send_residue(struct usbf_ep *epn, const void *buf,
  638. unsigned int size)
  639. {
  640. u32 tmp;
  641. memcpy(&tmp, buf, size);
  642. usbf_ep_reg_writel(epn, USBF_REG_EPN_WRITE, tmp);
  643. usbf_ep_reg_clrset(epn, USBF_REG_EPN_CONTROL,
  644. USBF_EPN_DW_MASK,
  645. USBF_EPN_DW(size) | USBF_EPN_DEND);
  646. }
  647. static int usbf_epn_pio_in(struct usbf_ep *epn, struct usbf_req *req)
  648. {
  649. unsigned int left;
  650. unsigned int nb;
  651. const void *buf;
  652. left = req->req.length - req->req.actual;
  653. if (left == 0) {
  654. if (!req->is_zero_sent) {
  655. if (req->req.length == 0) {
  656. dev_dbg(epn->udc->dev, "ep%u send_null\n", epn->id);
  657. usbf_epn_send_null(epn);
  658. req->is_zero_sent = 1;
  659. return -EINPROGRESS;
  660. }
  661. if ((req->req.actual % epn->ep.maxpacket) == 0) {
  662. if (req->req.zero) {
  663. dev_dbg(epn->udc->dev, "ep%u send_null\n",
  664. epn->id);
  665. usbf_epn_send_null(epn);
  666. req->is_zero_sent = 1;
  667. return -EINPROGRESS;
  668. }
  669. }
  670. }
  671. return 0;
  672. }
  673. if (left > epn->ep.maxpacket)
  674. left = epn->ep.maxpacket;
  675. buf = req->req.buf;
  676. buf += req->req.actual;
  677. nb = left / sizeof(u32);
  678. if (nb) {
  679. usbf_ep_reg_write_rep(epn, USBF_REG_EPN_WRITE, buf, nb);
  680. buf += (nb * sizeof(u32));
  681. req->req.actual += (nb * sizeof(u32));
  682. left -= (nb * sizeof(u32));
  683. }
  684. if (left) {
  685. usbf_epn_send_residue(epn, buf, left);
  686. req->req.actual += left;
  687. } else {
  688. usbf_ep_reg_clrset(epn, USBF_REG_EPN_CONTROL,
  689. USBF_EPN_DW_MASK,
  690. USBF_EPN_DEND);
  691. }
  692. dev_dbg(epn->udc->dev, "ep%u send %u/%u\n", epn->id, req->req.actual,
  693. req->req.length);
  694. return -EINPROGRESS;
  695. }
  696. static void usbf_epn_enable_in_end_int(struct usbf_ep *epn)
  697. {
  698. usbf_ep_reg_bitset(epn, USBF_REG_EPN_INT_ENA, USBF_EPN_IN_END_EN);
  699. }
  700. static int usbf_epn_dma_in(struct usbf_ep *epn, struct usbf_req *req)
  701. {
  702. unsigned int left;
  703. u32 npkt;
  704. u32 lastpkt;
  705. int ret;
  706. if (!IS_ALIGNED((uintptr_t)req->req.buf, 4)) {
  707. dev_dbg(epn->udc->dev, "ep%u buf unaligned -> fallback pio\n",
  708. epn->id);
  709. return usbf_epn_pio_in(epn, req);
  710. }
  711. left = req->req.length - req->req.actual;
  712. switch (req->xfer_step) {
  713. default:
  714. case USBF_XFER_START:
  715. if (left == 0) {
  716. dev_dbg(epn->udc->dev, "ep%u send null\n", epn->id);
  717. usbf_epn_send_null(epn);
  718. req->xfer_step = USBF_XFER_WAIT_END;
  719. break;
  720. }
  721. if (left < 4) {
  722. dev_dbg(epn->udc->dev, "ep%u send residue %u\n", epn->id,
  723. left);
  724. usbf_epn_send_residue(epn,
  725. req->req.buf + req->req.actual, left);
  726. req->req.actual += left;
  727. req->xfer_step = USBF_XFER_WAIT_END;
  728. break;
  729. }
  730. ret = usb_gadget_map_request(&epn->udc->gadget, &req->req, 1);
  731. if (ret < 0) {
  732. dev_err(epn->udc->dev, "usb_gadget_map_request failed (%d)\n",
  733. ret);
  734. return ret;
  735. }
  736. req->is_mapped = 1;
  737. npkt = DIV_ROUND_UP(left, epn->ep.maxpacket);
  738. lastpkt = (left % epn->ep.maxpacket);
  739. if (lastpkt == 0)
  740. lastpkt = epn->ep.maxpacket;
  741. lastpkt &= ~0x3; /* DMA is done on 32bit units */
  742. usbf_ep_dma_reg_writel(epn, USBF_REG_DMA_EPN_DCR2,
  743. USBF_SYS_EPN_MPKT(epn->ep.maxpacket) | USBF_SYS_EPN_LMPKT(lastpkt));
  744. usbf_ep_dma_reg_writel(epn, USBF_REG_DMA_EPN_TADR,
  745. req->req.dma);
  746. usbf_ep_dma_reg_writel(epn, USBF_REG_DMA_EPN_DCR1,
  747. USBF_SYS_EPN_SET_DMACNT(npkt));
  748. usbf_ep_dma_reg_bitset(epn, USBF_REG_DMA_EPN_DCR1,
  749. USBF_SYS_EPN_REQEN);
  750. usbf_ep_reg_writel(epn, USBF_REG_EPN_LEN_DCNT, USBF_EPN_SET_DMACNT(npkt));
  751. usbf_ep_reg_bitset(epn, USBF_REG_EPN_CONTROL, USBF_EPN_AUTO);
  752. /* The end of DMA transfer at the USBF level needs to be handle
  753. * after the detection of the end of DMA transfer at the brige
  754. * level.
  755. * To force this sequence, EPN_IN_END_EN will be set by the
  756. * detection of the end of transfer at bridge level (ie. bridge
  757. * interrupt).
  758. */
  759. usbf_ep_reg_bitclr(epn, USBF_REG_EPN_INT_ENA,
  760. USBF_EPN_IN_EN | USBF_EPN_IN_END_EN);
  761. epn->bridge_on_dma_end = usbf_epn_enable_in_end_int;
  762. /* Clear any pending IN_END interrupt */
  763. usbf_ep_reg_writel(epn, USBF_REG_EPN_STATUS, ~(u32)USBF_EPN_IN_END_INT);
  764. usbf_ep_reg_writel(epn, USBF_REG_EPN_DMA_CTRL,
  765. USBF_EPN_BURST_SET | USBF_EPN_DMAMODE0);
  766. usbf_ep_reg_bitset(epn, USBF_REG_EPN_DMA_CTRL,
  767. USBF_EPN_DMA_EN);
  768. req->dma_size = (npkt - 1) * epn->ep.maxpacket + lastpkt;
  769. dev_dbg(epn->udc->dev, "ep%u dma xfer %zu\n", epn->id,
  770. req->dma_size);
  771. req->xfer_step = USBF_XFER_WAIT_DMA;
  772. break;
  773. case USBF_XFER_WAIT_DMA:
  774. if (!(epn->status & USBF_EPN_IN_END_INT)) {
  775. dev_dbg(epn->udc->dev, "ep%u dma not done\n", epn->id);
  776. break;
  777. }
  778. dev_dbg(epn->udc->dev, "ep%u dma done\n", epn->id);
  779. usb_gadget_unmap_request(&epn->udc->gadget, &req->req, 1);
  780. req->is_mapped = 0;
  781. usbf_ep_reg_bitclr(epn, USBF_REG_EPN_CONTROL, USBF_EPN_AUTO);
  782. usbf_ep_reg_clrset(epn, USBF_REG_EPN_INT_ENA,
  783. USBF_EPN_IN_END_EN,
  784. USBF_EPN_IN_EN);
  785. req->req.actual += req->dma_size;
  786. left = req->req.length - req->req.actual;
  787. if (left) {
  788. usbf_ep_reg_writel(epn, USBF_REG_EPN_STATUS, ~(u32)USBF_EPN_IN_INT);
  789. dev_dbg(epn->udc->dev, "ep%u send residue %u\n", epn->id,
  790. left);
  791. usbf_epn_send_residue(epn,
  792. req->req.buf + req->req.actual, left);
  793. req->req.actual += left;
  794. req->xfer_step = USBF_XFER_WAIT_END;
  795. break;
  796. }
  797. if (req->req.actual % epn->ep.maxpacket) {
  798. /* last packet was a short packet. Tell the hardware to
  799. * send it right now.
  800. */
  801. dev_dbg(epn->udc->dev, "ep%u send short\n", epn->id);
  802. usbf_ep_reg_writel(epn, USBF_REG_EPN_STATUS,
  803. ~(u32)USBF_EPN_IN_INT);
  804. usbf_ep_reg_bitset(epn, USBF_REG_EPN_CONTROL,
  805. USBF_EPN_DEND);
  806. req->xfer_step = USBF_XFER_WAIT_END;
  807. break;
  808. }
  809. /* Last packet size was a maxpacket size
  810. * Send null packet if needed
  811. */
  812. if (req->req.zero) {
  813. req->xfer_step = USBF_XFER_SEND_NULL;
  814. break;
  815. }
  816. /* No more action to do. Wait for the end of the USB transfer */
  817. req->xfer_step = USBF_XFER_WAIT_END;
  818. break;
  819. case USBF_XFER_SEND_NULL:
  820. dev_dbg(epn->udc->dev, "ep%u send null\n", epn->id);
  821. usbf_epn_send_null(epn);
  822. req->xfer_step = USBF_XFER_WAIT_END;
  823. break;
  824. case USBF_XFER_WAIT_END:
  825. if (!(epn->status & USBF_EPN_IN_INT)) {
  826. dev_dbg(epn->udc->dev, "ep%u end not done\n", epn->id);
  827. break;
  828. }
  829. dev_dbg(epn->udc->dev, "ep%u send done %u/%u\n", epn->id,
  830. req->req.actual, req->req.length);
  831. req->xfer_step = USBF_XFER_START;
  832. return 0;
  833. }
  834. return -EINPROGRESS;
  835. }
  836. static void usbf_epn_recv_residue(struct usbf_ep *epn, void *buf,
  837. unsigned int size)
  838. {
  839. u32 last;
  840. last = usbf_ep_reg_readl(epn, USBF_REG_EPN_READ);
  841. memcpy(buf, &last, size);
  842. }
  843. static int usbf_epn_pio_out(struct usbf_ep *epn, struct usbf_req *req)
  844. {
  845. int req_status = 0;
  846. unsigned int count;
  847. unsigned int recv;
  848. unsigned int left;
  849. unsigned int nb;
  850. void *buf;
  851. if (epn->status & USBF_EPN_OUT_INT) {
  852. recv = USBF_EPN_GET_LDATA(
  853. usbf_ep_reg_readl(epn, USBF_REG_EPN_LEN_DCNT));
  854. count = recv;
  855. buf = req->req.buf;
  856. buf += req->req.actual;
  857. left = req->req.length - req->req.actual;
  858. dev_dbg(epn->udc->dev, "ep%u recv %u, left %u, mpkt %u\n", epn->id,
  859. recv, left, epn->ep.maxpacket);
  860. if (left > epn->ep.maxpacket)
  861. left = epn->ep.maxpacket;
  862. if (count > left) {
  863. req_status = -EOVERFLOW;
  864. count = left;
  865. }
  866. if (count) {
  867. nb = count / sizeof(u32);
  868. if (nb) {
  869. usbf_ep_reg_read_rep(epn, USBF_REG_EPN_READ,
  870. buf, nb);
  871. buf += (nb * sizeof(u32));
  872. req->req.actual += (nb * sizeof(u32));
  873. count -= (nb * sizeof(u32));
  874. }
  875. if (count) {
  876. usbf_epn_recv_residue(epn, buf, count);
  877. req->req.actual += count;
  878. }
  879. }
  880. dev_dbg(epn->udc->dev, "ep%u recv %u/%u\n", epn->id,
  881. req->req.actual, req->req.length);
  882. if (req_status) {
  883. dev_dbg(epn->udc->dev, "ep%u req.status=%d\n", epn->id,
  884. req_status);
  885. req->req.status = req_status;
  886. return 0;
  887. }
  888. if (recv < epn->ep.maxpacket) {
  889. dev_dbg(epn->udc->dev, "ep%u short packet\n", epn->id);
  890. /* This is a short packet -> It is the end */
  891. req->req.status = 0;
  892. return 0;
  893. }
  894. /* Request full -> complete */
  895. if (req->req.actual == req->req.length) {
  896. req->req.status = 0;
  897. return 0;
  898. }
  899. }
  900. if (epn->status & USBF_EPN_OUT_NULL_INT) {
  901. /* NULL packet received */
  902. dev_dbg(epn->udc->dev, "ep%u null packet\n", epn->id);
  903. if (req->req.actual != req->req.length) {
  904. req->req.status = req->req.short_not_ok ?
  905. -EREMOTEIO : 0;
  906. } else {
  907. req->req.status = 0;
  908. }
  909. return 0;
  910. }
  911. return -EINPROGRESS;
  912. }
  913. static void usbf_epn_enable_out_end_int(struct usbf_ep *epn)
  914. {
  915. usbf_ep_reg_bitset(epn, USBF_REG_EPN_INT_ENA, USBF_EPN_OUT_END_EN);
  916. }
  917. static void usbf_epn_process_queue(struct usbf_ep *epn);
  918. static void usbf_epn_dma_out_send_dma(struct usbf_ep *epn, dma_addr_t addr, u32 npkt, bool is_short)
  919. {
  920. usbf_ep_dma_reg_writel(epn, USBF_REG_DMA_EPN_DCR2, USBF_SYS_EPN_MPKT(epn->ep.maxpacket));
  921. usbf_ep_dma_reg_writel(epn, USBF_REG_DMA_EPN_TADR, addr);
  922. if (is_short) {
  923. usbf_ep_dma_reg_writel(epn, USBF_REG_DMA_EPN_DCR1,
  924. USBF_SYS_EPN_SET_DMACNT(1) | USBF_SYS_EPN_DIR0);
  925. usbf_ep_dma_reg_bitset(epn, USBF_REG_DMA_EPN_DCR1,
  926. USBF_SYS_EPN_REQEN);
  927. usbf_ep_reg_writel(epn, USBF_REG_EPN_LEN_DCNT,
  928. USBF_EPN_SET_DMACNT(0));
  929. /* The end of DMA transfer at the USBF level needs to be handled
  930. * after the detection of the end of DMA transfer at the brige
  931. * level.
  932. * To force this sequence, enabling the OUT_END interrupt will
  933. * be donee by the detection of the end of transfer at bridge
  934. * level (ie. bridge interrupt).
  935. */
  936. usbf_ep_reg_bitclr(epn, USBF_REG_EPN_INT_ENA,
  937. USBF_EPN_OUT_EN | USBF_EPN_OUT_NULL_EN | USBF_EPN_OUT_END_EN);
  938. epn->bridge_on_dma_end = usbf_epn_enable_out_end_int;
  939. /* Clear any pending OUT_END interrupt */
  940. usbf_ep_reg_writel(epn, USBF_REG_EPN_STATUS,
  941. ~(u32)USBF_EPN_OUT_END_INT);
  942. usbf_ep_reg_writel(epn, USBF_REG_EPN_DMA_CTRL,
  943. USBF_EPN_STOP_MODE | USBF_EPN_STOP_SET | USBF_EPN_DMAMODE0);
  944. usbf_ep_reg_bitset(epn, USBF_REG_EPN_DMA_CTRL,
  945. USBF_EPN_DMA_EN);
  946. return;
  947. }
  948. usbf_ep_dma_reg_writel(epn, USBF_REG_DMA_EPN_DCR1,
  949. USBF_SYS_EPN_SET_DMACNT(npkt) | USBF_SYS_EPN_DIR0);
  950. usbf_ep_dma_reg_bitset(epn, USBF_REG_DMA_EPN_DCR1,
  951. USBF_SYS_EPN_REQEN);
  952. usbf_ep_reg_writel(epn, USBF_REG_EPN_LEN_DCNT,
  953. USBF_EPN_SET_DMACNT(npkt));
  954. /* Here, the bridge may or may not generate an interrupt to signal the
  955. * end of DMA transfer.
  956. * Keep only OUT_END interrupt and let handle the bridge later during
  957. * the OUT_END processing.
  958. */
  959. usbf_ep_reg_clrset(epn, USBF_REG_EPN_INT_ENA,
  960. USBF_EPN_OUT_EN | USBF_EPN_OUT_NULL_EN,
  961. USBF_EPN_OUT_END_EN);
  962. /* Disable bridge interrupt. It will be renabled later */
  963. usbf_reg_bitclr(epn->udc, USBF_REG_AHBBINTEN,
  964. USBF_SYS_DMA_ENDINTEN_EPN(epn->id));
  965. /* Clear any pending DMA_END interrupt at bridge level */
  966. usbf_reg_writel(epn->udc, USBF_REG_AHBBINT,
  967. USBF_SYS_DMA_ENDINT_EPN(epn->id));
  968. /* Clear any pending OUT_END interrupt */
  969. usbf_ep_reg_writel(epn, USBF_REG_EPN_STATUS,
  970. ~(u32)USBF_EPN_OUT_END_INT);
  971. usbf_ep_reg_writel(epn, USBF_REG_EPN_DMA_CTRL,
  972. USBF_EPN_STOP_MODE | USBF_EPN_STOP_SET | USBF_EPN_DMAMODE0 | USBF_EPN_BURST_SET);
  973. usbf_ep_reg_bitset(epn, USBF_REG_EPN_DMA_CTRL,
  974. USBF_EPN_DMA_EN);
  975. }
  976. static size_t usbf_epn_dma_out_complete_dma(struct usbf_ep *epn, bool is_short)
  977. {
  978. u32 dmacnt;
  979. u32 tmp;
  980. int ret;
  981. /* Restore interrupt mask */
  982. usbf_ep_reg_clrset(epn, USBF_REG_EPN_INT_ENA,
  983. USBF_EPN_OUT_END_EN,
  984. USBF_EPN_OUT_EN | USBF_EPN_OUT_NULL_EN);
  985. if (is_short) {
  986. /* Nothing more to do when the DMA was for a short packet */
  987. return 0;
  988. }
  989. /* Enable the bridge interrupt */
  990. usbf_reg_bitset(epn->udc, USBF_REG_AHBBINTEN,
  991. USBF_SYS_DMA_ENDINTEN_EPN(epn->id));
  992. tmp = usbf_ep_reg_readl(epn, USBF_REG_EPN_LEN_DCNT);
  993. dmacnt = USBF_EPN_GET_DMACNT(tmp);
  994. if (dmacnt) {
  995. /* Some packet were not received (halted by a short or a null
  996. * packet.
  997. * The bridge never raises an interrupt in this case.
  998. * Wait for the end of transfer at bridge level
  999. */
  1000. ret = readl_poll_timeout_atomic(
  1001. epn->dma_regs + USBF_REG_DMA_EPN_DCR1,
  1002. tmp, (USBF_SYS_EPN_GET_DMACNT(tmp) == dmacnt),
  1003. 0, 10000);
  1004. if (ret) {
  1005. dev_err(epn->udc->dev, "ep%u wait bridge timed out\n",
  1006. epn->id);
  1007. }
  1008. usbf_ep_dma_reg_bitclr(epn, USBF_REG_DMA_EPN_DCR1,
  1009. USBF_SYS_EPN_REQEN);
  1010. /* The dmacnt value tells how many packet were not transferred
  1011. * from the maximum number of packet we set for the DMA transfer.
  1012. * Compute the left DMA size based on this value.
  1013. */
  1014. return dmacnt * epn->ep.maxpacket;
  1015. }
  1016. return 0;
  1017. }
  1018. static int usbf_epn_dma_out(struct usbf_ep *epn, struct usbf_req *req)
  1019. {
  1020. unsigned int dma_left;
  1021. unsigned int count;
  1022. unsigned int recv;
  1023. unsigned int left;
  1024. u32 npkt;
  1025. int ret;
  1026. if (!IS_ALIGNED((uintptr_t)req->req.buf, 4)) {
  1027. dev_dbg(epn->udc->dev, "ep%u buf unaligned -> fallback pio\n",
  1028. epn->id);
  1029. return usbf_epn_pio_out(epn, req);
  1030. }
  1031. switch (req->xfer_step) {
  1032. default:
  1033. case USBF_XFER_START:
  1034. if (epn->status & USBF_EPN_OUT_NULL_INT) {
  1035. dev_dbg(epn->udc->dev, "ep%u null packet\n", epn->id);
  1036. if (req->req.actual != req->req.length) {
  1037. req->req.status = req->req.short_not_ok ?
  1038. -EREMOTEIO : 0;
  1039. } else {
  1040. req->req.status = 0;
  1041. }
  1042. return 0;
  1043. }
  1044. if (!(epn->status & USBF_EPN_OUT_INT)) {
  1045. dev_dbg(epn->udc->dev, "ep%u OUT_INT not set -> spurious\n",
  1046. epn->id);
  1047. break;
  1048. }
  1049. recv = USBF_EPN_GET_LDATA(
  1050. usbf_ep_reg_readl(epn, USBF_REG_EPN_LEN_DCNT));
  1051. if (!recv) {
  1052. dev_dbg(epn->udc->dev, "ep%u recv = 0 -> spurious\n",
  1053. epn->id);
  1054. break;
  1055. }
  1056. left = req->req.length - req->req.actual;
  1057. dev_dbg(epn->udc->dev, "ep%u recv %u, left %u, mpkt %u\n", epn->id,
  1058. recv, left, epn->ep.maxpacket);
  1059. if (recv > left) {
  1060. dev_err(epn->udc->dev, "ep%u overflow (%u/%u)\n",
  1061. epn->id, recv, left);
  1062. req->req.status = -EOVERFLOW;
  1063. return -EOVERFLOW;
  1064. }
  1065. if (recv < epn->ep.maxpacket) {
  1066. /* Short packet received */
  1067. dev_dbg(epn->udc->dev, "ep%u short packet\n", epn->id);
  1068. if (recv <= 3) {
  1069. usbf_epn_recv_residue(epn,
  1070. req->req.buf + req->req.actual, recv);
  1071. req->req.actual += recv;
  1072. dev_dbg(epn->udc->dev, "ep%u recv done %u/%u\n",
  1073. epn->id, req->req.actual, req->req.length);
  1074. req->xfer_step = USBF_XFER_START;
  1075. return 0;
  1076. }
  1077. ret = usb_gadget_map_request(&epn->udc->gadget, &req->req, 0);
  1078. if (ret < 0) {
  1079. dev_err(epn->udc->dev, "map request failed (%d)\n",
  1080. ret);
  1081. return ret;
  1082. }
  1083. req->is_mapped = 1;
  1084. usbf_epn_dma_out_send_dma(epn,
  1085. req->req.dma + req->req.actual,
  1086. 1, true);
  1087. req->dma_size = recv & ~0x3;
  1088. dev_dbg(epn->udc->dev, "ep%u dma short xfer %zu\n", epn->id,
  1089. req->dma_size);
  1090. req->xfer_step = USBF_XFER_WAIT_DMA_SHORT;
  1091. break;
  1092. }
  1093. ret = usb_gadget_map_request(&epn->udc->gadget, &req->req, 0);
  1094. if (ret < 0) {
  1095. dev_err(epn->udc->dev, "map request failed (%d)\n",
  1096. ret);
  1097. return ret;
  1098. }
  1099. req->is_mapped = 1;
  1100. /* Use the maximum DMA size according to the request buffer.
  1101. * We will adjust the received size later at the end of the DMA
  1102. * transfer with the left size computed from
  1103. * usbf_epn_dma_out_complete_dma().
  1104. */
  1105. npkt = left / epn->ep.maxpacket;
  1106. usbf_epn_dma_out_send_dma(epn,
  1107. req->req.dma + req->req.actual,
  1108. npkt, false);
  1109. req->dma_size = npkt * epn->ep.maxpacket;
  1110. dev_dbg(epn->udc->dev, "ep%u dma xfer %zu (%u)\n", epn->id,
  1111. req->dma_size, npkt);
  1112. req->xfer_step = USBF_XFER_WAIT_DMA;
  1113. break;
  1114. case USBF_XFER_WAIT_DMA_SHORT:
  1115. if (!(epn->status & USBF_EPN_OUT_END_INT)) {
  1116. dev_dbg(epn->udc->dev, "ep%u dma short not done\n", epn->id);
  1117. break;
  1118. }
  1119. dev_dbg(epn->udc->dev, "ep%u dma short done\n", epn->id);
  1120. usbf_epn_dma_out_complete_dma(epn, true);
  1121. usb_gadget_unmap_request(&epn->udc->gadget, &req->req, 0);
  1122. req->is_mapped = 0;
  1123. req->req.actual += req->dma_size;
  1124. recv = USBF_EPN_GET_LDATA(
  1125. usbf_ep_reg_readl(epn, USBF_REG_EPN_LEN_DCNT));
  1126. count = recv & 0x3;
  1127. if (count) {
  1128. dev_dbg(epn->udc->dev, "ep%u recv residue %u\n", epn->id,
  1129. count);
  1130. usbf_epn_recv_residue(epn,
  1131. req->req.buf + req->req.actual, count);
  1132. req->req.actual += count;
  1133. }
  1134. dev_dbg(epn->udc->dev, "ep%u recv done %u/%u\n", epn->id,
  1135. req->req.actual, req->req.length);
  1136. req->xfer_step = USBF_XFER_START;
  1137. return 0;
  1138. case USBF_XFER_WAIT_DMA:
  1139. if (!(epn->status & USBF_EPN_OUT_END_INT)) {
  1140. dev_dbg(epn->udc->dev, "ep%u dma not done\n", epn->id);
  1141. break;
  1142. }
  1143. dev_dbg(epn->udc->dev, "ep%u dma done\n", epn->id);
  1144. dma_left = usbf_epn_dma_out_complete_dma(epn, false);
  1145. if (dma_left) {
  1146. /* Adjust the final DMA size with */
  1147. count = req->dma_size - dma_left;
  1148. dev_dbg(epn->udc->dev, "ep%u dma xfer done %u\n", epn->id,
  1149. count);
  1150. req->req.actual += count;
  1151. if (epn->status & USBF_EPN_OUT_NULL_INT) {
  1152. /* DMA was stopped by a null packet reception */
  1153. dev_dbg(epn->udc->dev, "ep%u dma stopped by null pckt\n",
  1154. epn->id);
  1155. usb_gadget_unmap_request(&epn->udc->gadget,
  1156. &req->req, 0);
  1157. req->is_mapped = 0;
  1158. usbf_ep_reg_writel(epn, USBF_REG_EPN_STATUS,
  1159. ~(u32)USBF_EPN_OUT_NULL_INT);
  1160. if (req->req.actual != req->req.length) {
  1161. req->req.status = req->req.short_not_ok ?
  1162. -EREMOTEIO : 0;
  1163. } else {
  1164. req->req.status = 0;
  1165. }
  1166. dev_dbg(epn->udc->dev, "ep%u recv done %u/%u\n",
  1167. epn->id, req->req.actual, req->req.length);
  1168. req->xfer_step = USBF_XFER_START;
  1169. return 0;
  1170. }
  1171. recv = USBF_EPN_GET_LDATA(
  1172. usbf_ep_reg_readl(epn, USBF_REG_EPN_LEN_DCNT));
  1173. left = req->req.length - req->req.actual;
  1174. if (recv > left) {
  1175. dev_err(epn->udc->dev,
  1176. "ep%u overflow (%u/%u)\n", epn->id,
  1177. recv, left);
  1178. req->req.status = -EOVERFLOW;
  1179. usb_gadget_unmap_request(&epn->udc->gadget,
  1180. &req->req, 0);
  1181. req->is_mapped = 0;
  1182. req->xfer_step = USBF_XFER_START;
  1183. return -EOVERFLOW;
  1184. }
  1185. if (recv > 3) {
  1186. usbf_epn_dma_out_send_dma(epn,
  1187. req->req.dma + req->req.actual,
  1188. 1, true);
  1189. req->dma_size = recv & ~0x3;
  1190. dev_dbg(epn->udc->dev, "ep%u dma short xfer %zu\n",
  1191. epn->id, req->dma_size);
  1192. req->xfer_step = USBF_XFER_WAIT_DMA_SHORT;
  1193. break;
  1194. }
  1195. usb_gadget_unmap_request(&epn->udc->gadget, &req->req, 0);
  1196. req->is_mapped = 0;
  1197. count = recv & 0x3;
  1198. if (count) {
  1199. dev_dbg(epn->udc->dev, "ep%u recv residue %u\n",
  1200. epn->id, count);
  1201. usbf_epn_recv_residue(epn,
  1202. req->req.buf + req->req.actual, count);
  1203. req->req.actual += count;
  1204. }
  1205. dev_dbg(epn->udc->dev, "ep%u recv done %u/%u\n", epn->id,
  1206. req->req.actual, req->req.length);
  1207. req->xfer_step = USBF_XFER_START;
  1208. return 0;
  1209. }
  1210. /* Process queue at bridge interrupt only */
  1211. usbf_ep_reg_bitclr(epn, USBF_REG_EPN_INT_ENA,
  1212. USBF_EPN_OUT_END_EN | USBF_EPN_OUT_EN | USBF_EPN_OUT_NULL_EN);
  1213. epn->status = 0;
  1214. epn->bridge_on_dma_end = usbf_epn_process_queue;
  1215. req->xfer_step = USBF_XFER_WAIT_BRIDGE;
  1216. break;
  1217. case USBF_XFER_WAIT_BRIDGE:
  1218. dev_dbg(epn->udc->dev, "ep%u bridge transfers done\n", epn->id);
  1219. /* Restore interrupt mask */
  1220. usbf_ep_reg_clrset(epn, USBF_REG_EPN_INT_ENA,
  1221. USBF_EPN_OUT_END_EN,
  1222. USBF_EPN_OUT_EN | USBF_EPN_OUT_NULL_EN);
  1223. usb_gadget_unmap_request(&epn->udc->gadget, &req->req, 0);
  1224. req->is_mapped = 0;
  1225. req->req.actual += req->dma_size;
  1226. req->xfer_step = USBF_XFER_START;
  1227. left = req->req.length - req->req.actual;
  1228. if (!left) {
  1229. /* No more data can be added to the buffer */
  1230. dev_dbg(epn->udc->dev, "ep%u recv done %u/%u\n", epn->id,
  1231. req->req.actual, req->req.length);
  1232. return 0;
  1233. }
  1234. dev_dbg(epn->udc->dev, "ep%u recv done %u/%u, wait more data\n",
  1235. epn->id, req->req.actual, req->req.length);
  1236. break;
  1237. }
  1238. return -EINPROGRESS;
  1239. }
  1240. static void usbf_epn_dma_stop(struct usbf_ep *epn)
  1241. {
  1242. usbf_ep_dma_reg_bitclr(epn, USBF_REG_DMA_EPN_DCR1, USBF_SYS_EPN_REQEN);
  1243. /* In the datasheet:
  1244. * If EP[m]_REQEN = 0b is set during DMA transfer, AHB-EPC stops DMA
  1245. * after 1 packet transfer completed.
  1246. * Therefore, wait sufficient time for ensuring DMA transfer
  1247. * completion. The WAIT time depends on the system, especially AHB
  1248. * bus activity
  1249. * So arbitrary 10ms would be sufficient.
  1250. */
  1251. mdelay(10);
  1252. usbf_ep_reg_bitclr(epn, USBF_REG_EPN_DMA_CTRL, USBF_EPN_DMA_EN);
  1253. }
  1254. static void usbf_epn_dma_abort(struct usbf_ep *epn, struct usbf_req *req)
  1255. {
  1256. dev_dbg(epn->udc->dev, "ep%u %s dma abort\n", epn->id,
  1257. epn->is_in ? "in" : "out");
  1258. epn->bridge_on_dma_end = NULL;
  1259. usbf_epn_dma_stop(epn);
  1260. usb_gadget_unmap_request(&epn->udc->gadget, &req->req,
  1261. epn->is_in ? 1 : 0);
  1262. req->is_mapped = 0;
  1263. usbf_ep_reg_bitclr(epn, USBF_REG_EPN_CONTROL, USBF_EPN_AUTO);
  1264. if (epn->is_in) {
  1265. usbf_ep_reg_clrset(epn, USBF_REG_EPN_INT_ENA,
  1266. USBF_EPN_IN_END_EN,
  1267. USBF_EPN_IN_EN);
  1268. } else {
  1269. usbf_ep_reg_clrset(epn, USBF_REG_EPN_INT_ENA,
  1270. USBF_EPN_OUT_END_EN,
  1271. USBF_EPN_OUT_EN | USBF_EPN_OUT_NULL_EN);
  1272. }
  1273. /* As dma is stopped, be sure that no DMA interrupt are pending */
  1274. usbf_ep_reg_writel(epn, USBF_REG_EPN_STATUS,
  1275. USBF_EPN_IN_END_INT | USBF_EPN_OUT_END_INT);
  1276. usbf_reg_writel(epn->udc, USBF_REG_AHBBINT, USBF_SYS_DMA_ENDINT_EPN(epn->id));
  1277. /* Enable DMA interrupt the bridge level */
  1278. usbf_reg_bitset(epn->udc, USBF_REG_AHBBINTEN,
  1279. USBF_SYS_DMA_ENDINTEN_EPN(epn->id));
  1280. /* Reset transfer step */
  1281. req->xfer_step = USBF_XFER_START;
  1282. }
  1283. static void usbf_epn_fifo_flush(struct usbf_ep *epn)
  1284. {
  1285. u32 ctrl;
  1286. u32 sts;
  1287. int ret;
  1288. dev_dbg(epn->udc->dev, "ep%u %s fifo flush\n", epn->id,
  1289. epn->is_in ? "in" : "out");
  1290. ctrl = usbf_ep_reg_readl(epn, USBF_REG_EPN_CONTROL);
  1291. usbf_ep_reg_writel(epn, USBF_REG_EPN_CONTROL, ctrl | USBF_EPN_BCLR);
  1292. if (ctrl & USBF_EPN_DIR0)
  1293. return;
  1294. ret = readl_poll_timeout_atomic(epn->regs + USBF_REG_EPN_STATUS, sts,
  1295. (sts & (USBF_EPN_IN_DATA | USBF_EPN_IN_EMPTY)) == USBF_EPN_IN_EMPTY,
  1296. 0, 10000);
  1297. if (ret)
  1298. dev_err(epn->udc->dev, "ep%u flush fifo timed out\n", epn->id);
  1299. }
  1300. static void usbf_ep_req_done(struct usbf_ep *ep, struct usbf_req *req,
  1301. int status)
  1302. {
  1303. list_del_init(&req->queue);
  1304. if (status) {
  1305. req->req.status = status;
  1306. } else {
  1307. if (req->req.status == -EINPROGRESS)
  1308. req->req.status = status;
  1309. }
  1310. dev_dbg(ep->udc->dev, "ep%u %s req done length %u/%u, status=%d\n", ep->id,
  1311. ep->is_in ? "in" : "out",
  1312. req->req.actual, req->req.length, req->req.status);
  1313. if (req->is_mapped)
  1314. usbf_epn_dma_abort(ep, req);
  1315. spin_unlock(&ep->udc->lock);
  1316. usb_gadget_giveback_request(&ep->ep, &req->req);
  1317. spin_lock(&ep->udc->lock);
  1318. }
  1319. static void usbf_ep_nuke(struct usbf_ep *ep, int status)
  1320. {
  1321. struct usbf_req *req;
  1322. dev_dbg(ep->udc->dev, "ep%u %s nuke status %d\n", ep->id,
  1323. ep->is_in ? "in" : "out",
  1324. status);
  1325. while (!list_empty(&ep->queue)) {
  1326. req = list_first_entry(&ep->queue, struct usbf_req, queue);
  1327. usbf_ep_req_done(ep, req, status);
  1328. }
  1329. if (ep->id == 0)
  1330. usbf_ep0_fifo_flush(ep);
  1331. else
  1332. usbf_epn_fifo_flush(ep);
  1333. }
  1334. static bool usbf_ep_is_stalled(struct usbf_ep *ep)
  1335. {
  1336. u32 ctrl;
  1337. if (ep->id == 0) {
  1338. ctrl = usbf_ep_reg_readl(ep, USBF_REG_EP0_CONTROL);
  1339. return (ctrl & USBF_EP0_STL) ? true : false;
  1340. }
  1341. ctrl = usbf_ep_reg_readl(ep, USBF_REG_EPN_CONTROL);
  1342. if (ep->is_in)
  1343. return (ctrl & USBF_EPN_ISTL) ? true : false;
  1344. return (ctrl & USBF_EPN_OSTL) ? true : false;
  1345. }
  1346. static int usbf_epn_start_queue(struct usbf_ep *epn)
  1347. {
  1348. struct usbf_req *req;
  1349. int ret;
  1350. if (usbf_ep_is_stalled(epn))
  1351. return 0;
  1352. req = list_first_entry_or_null(&epn->queue, struct usbf_req, queue);
  1353. if (epn->is_in) {
  1354. if (req && !epn->is_processing) {
  1355. ret = epn->dma_regs ?
  1356. usbf_epn_dma_in(epn, req) :
  1357. usbf_epn_pio_in(epn, req);
  1358. if (ret != -EINPROGRESS) {
  1359. dev_err(epn->udc->dev,
  1360. "queued next request not in progress\n");
  1361. /* The request cannot be completed (ie
  1362. * ret == 0) on the first call.
  1363. * stall and nuke the endpoint
  1364. */
  1365. return ret ? ret : -EIO;
  1366. }
  1367. }
  1368. } else {
  1369. if (req) {
  1370. /* Clear ONAK to accept OUT tokens */
  1371. usbf_ep_reg_bitclr(epn, USBF_REG_EPN_CONTROL,
  1372. USBF_EPN_ONAK);
  1373. /* Enable interrupts */
  1374. usbf_ep_reg_bitset(epn, USBF_REG_EPN_INT_ENA,
  1375. USBF_EPN_OUT_INT | USBF_EPN_OUT_NULL_INT);
  1376. } else {
  1377. /* Disable incoming data and interrupt.
  1378. * They will be enable on next usb_eb_queue call
  1379. */
  1380. usbf_ep_reg_bitset(epn, USBF_REG_EPN_CONTROL,
  1381. USBF_EPN_ONAK);
  1382. usbf_ep_reg_bitclr(epn, USBF_REG_EPN_INT_ENA,
  1383. USBF_EPN_OUT_INT | USBF_EPN_OUT_NULL_INT);
  1384. }
  1385. }
  1386. return 0;
  1387. }
  1388. static int usbf_ep_process_queue(struct usbf_ep *ep)
  1389. {
  1390. int (*usbf_ep_xfer)(struct usbf_ep *ep, struct usbf_req *req);
  1391. struct usbf_req *req;
  1392. int is_processing;
  1393. int ret;
  1394. if (ep->is_in) {
  1395. usbf_ep_xfer = usbf_ep0_pio_in;
  1396. if (ep->id) {
  1397. usbf_ep_xfer = ep->dma_regs ?
  1398. usbf_epn_dma_in : usbf_epn_pio_in;
  1399. }
  1400. } else {
  1401. usbf_ep_xfer = usbf_ep0_pio_out;
  1402. if (ep->id) {
  1403. usbf_ep_xfer = ep->dma_regs ?
  1404. usbf_epn_dma_out : usbf_epn_pio_out;
  1405. }
  1406. }
  1407. req = list_first_entry_or_null(&ep->queue, struct usbf_req, queue);
  1408. if (!req) {
  1409. dev_err(ep->udc->dev,
  1410. "no request available for ep%u %s process\n", ep->id,
  1411. ep->is_in ? "in" : "out");
  1412. return -ENOENT;
  1413. }
  1414. do {
  1415. /* Were going to read the FIFO for this current request.
  1416. * NAK any other incoming data to avoid a race condition if no
  1417. * more request are available.
  1418. */
  1419. if (!ep->is_in && ep->id != 0) {
  1420. usbf_ep_reg_bitset(ep, USBF_REG_EPN_CONTROL,
  1421. USBF_EPN_ONAK);
  1422. }
  1423. ret = usbf_ep_xfer(ep, req);
  1424. if (ret == -EINPROGRESS) {
  1425. if (!ep->is_in && ep->id != 0) {
  1426. /* The current request needs more data.
  1427. * Allow incoming data
  1428. */
  1429. usbf_ep_reg_bitclr(ep, USBF_REG_EPN_CONTROL,
  1430. USBF_EPN_ONAK);
  1431. }
  1432. return ret;
  1433. }
  1434. is_processing = ep->is_processing;
  1435. ep->is_processing = 1;
  1436. usbf_ep_req_done(ep, req, ret);
  1437. ep->is_processing = is_processing;
  1438. if (ret) {
  1439. /* An error was detected during the request transfer.
  1440. * Any pending DMA transfers were aborted by the
  1441. * usbf_ep_req_done() call.
  1442. * It's time to flush the fifo
  1443. */
  1444. if (ep->id == 0)
  1445. usbf_ep0_fifo_flush(ep);
  1446. else
  1447. usbf_epn_fifo_flush(ep);
  1448. }
  1449. req = list_first_entry_or_null(&ep->queue, struct usbf_req,
  1450. queue);
  1451. if (ep->is_in)
  1452. continue;
  1453. if (ep->id != 0) {
  1454. if (req) {
  1455. /* An other request is available.
  1456. * Allow incoming data
  1457. */
  1458. usbf_ep_reg_bitclr(ep, USBF_REG_EPN_CONTROL,
  1459. USBF_EPN_ONAK);
  1460. } else {
  1461. /* No request queued. Disable interrupts.
  1462. * They will be enabled on usb_ep_queue
  1463. */
  1464. usbf_ep_reg_bitclr(ep, USBF_REG_EPN_INT_ENA,
  1465. USBF_EPN_OUT_INT | USBF_EPN_OUT_NULL_INT);
  1466. }
  1467. }
  1468. /* Do not recall usbf_ep_xfer() */
  1469. return req ? -EINPROGRESS : 0;
  1470. } while (req);
  1471. return 0;
  1472. }
  1473. static void usbf_ep_stall(struct usbf_ep *ep, bool stall)
  1474. {
  1475. struct usbf_req *first;
  1476. dev_dbg(ep->udc->dev, "ep%u %s %s\n", ep->id,
  1477. ep->is_in ? "in" : "out",
  1478. stall ? "stall" : "unstall");
  1479. if (ep->id == 0) {
  1480. if (stall)
  1481. usbf_ep_reg_bitset(ep, USBF_REG_EP0_CONTROL, USBF_EP0_STL);
  1482. else
  1483. usbf_ep_reg_bitclr(ep, USBF_REG_EP0_CONTROL, USBF_EP0_STL);
  1484. return;
  1485. }
  1486. if (stall) {
  1487. if (ep->is_in)
  1488. usbf_ep_reg_bitset(ep, USBF_REG_EPN_CONTROL,
  1489. USBF_EPN_ISTL);
  1490. else
  1491. usbf_ep_reg_bitset(ep, USBF_REG_EPN_CONTROL,
  1492. USBF_EPN_OSTL | USBF_EPN_OSTL_EN);
  1493. } else {
  1494. first = list_first_entry_or_null(&ep->queue, struct usbf_req, queue);
  1495. if (first && first->is_mapped) {
  1496. /* This can appear if the host halts an endpoint using
  1497. * SET_FEATURE and then un-halts the endpoint
  1498. */
  1499. usbf_epn_dma_abort(ep, first);
  1500. }
  1501. usbf_epn_fifo_flush(ep);
  1502. if (ep->is_in) {
  1503. usbf_ep_reg_clrset(ep, USBF_REG_EPN_CONTROL,
  1504. USBF_EPN_ISTL,
  1505. USBF_EPN_IPIDCLR);
  1506. } else {
  1507. usbf_ep_reg_clrset(ep, USBF_REG_EPN_CONTROL,
  1508. USBF_EPN_OSTL,
  1509. USBF_EPN_OSTL_EN | USBF_EPN_OPIDCLR);
  1510. }
  1511. usbf_epn_start_queue(ep);
  1512. }
  1513. }
  1514. static void usbf_ep0_enable(struct usbf_ep *ep0)
  1515. {
  1516. usbf_ep_reg_writel(ep0, USBF_REG_EP0_CONTROL, USBF_EP0_INAK_EN | USBF_EP0_BCLR);
  1517. usbf_ep_reg_writel(ep0, USBF_REG_EP0_INT_ENA,
  1518. USBF_EP0_SETUP_EN | USBF_EP0_STG_START_EN | USBF_EP0_STG_END_EN |
  1519. USBF_EP0_OUT_EN | USBF_EP0_OUT_NULL_EN | USBF_EP0_IN_EN);
  1520. ep0->udc->ep0state = EP0_IDLE;
  1521. ep0->disabled = 0;
  1522. /* enable interrupts for the ep0 */
  1523. usbf_reg_bitset(ep0->udc, USBF_REG_USB_INT_ENA, USBF_USB_EPN_EN(0));
  1524. }
  1525. static int usbf_epn_enable(struct usbf_ep *epn)
  1526. {
  1527. u32 base_addr;
  1528. u32 ctrl;
  1529. base_addr = usbf_ep_info[epn->id].base_addr;
  1530. usbf_ep_reg_writel(epn, USBF_REG_EPN_PCKT_ADRS,
  1531. USBF_EPN_BASEAD(base_addr) | USBF_EPN_MPKT(epn->ep.maxpacket));
  1532. /* OUT transfer interrupt are enabled during usb_ep_queue */
  1533. if (epn->is_in) {
  1534. /* Will be changed in DMA processing */
  1535. usbf_ep_reg_writel(epn, USBF_REG_EPN_INT_ENA, USBF_EPN_IN_EN);
  1536. }
  1537. /* Clear, set endpoint direction, set IN/OUT STL, and enable
  1538. * Send NAK for Data out as request are not queued yet
  1539. */
  1540. ctrl = USBF_EPN_EN | USBF_EPN_BCLR;
  1541. if (epn->is_in)
  1542. ctrl |= USBF_EPN_OSTL | USBF_EPN_OSTL_EN;
  1543. else
  1544. ctrl |= USBF_EPN_DIR0 | USBF_EPN_ISTL | USBF_EPN_OSTL_EN | USBF_EPN_ONAK;
  1545. usbf_ep_reg_writel(epn, USBF_REG_EPN_CONTROL, ctrl);
  1546. return 0;
  1547. }
  1548. static int usbf_ep_enable(struct usb_ep *_ep,
  1549. const struct usb_endpoint_descriptor *desc)
  1550. {
  1551. struct usbf_ep *ep = container_of(_ep, struct usbf_ep, ep);
  1552. struct usbf_udc *udc = ep->udc;
  1553. unsigned long flags;
  1554. int ret;
  1555. if (ep->id == 0)
  1556. return -EINVAL;
  1557. if (!desc || desc->bDescriptorType != USB_DT_ENDPOINT)
  1558. return -EINVAL;
  1559. dev_dbg(ep->udc->dev, "ep%u %s mpkts %d\n", ep->id,
  1560. usb_endpoint_dir_in(desc) ? "in" : "out",
  1561. usb_endpoint_maxp(desc));
  1562. spin_lock_irqsave(&ep->udc->lock, flags);
  1563. ep->is_in = usb_endpoint_dir_in(desc);
  1564. ep->ep.maxpacket = usb_endpoint_maxp(desc);
  1565. ret = usbf_epn_enable(ep);
  1566. if (ret)
  1567. goto end;
  1568. ep->disabled = 0;
  1569. /* enable interrupts for this endpoint */
  1570. usbf_reg_bitset(udc, USBF_REG_USB_INT_ENA, USBF_USB_EPN_EN(ep->id));
  1571. /* enable DMA interrupt at bridge level if DMA is used */
  1572. if (ep->dma_regs) {
  1573. ep->bridge_on_dma_end = NULL;
  1574. usbf_reg_bitset(udc, USBF_REG_AHBBINTEN,
  1575. USBF_SYS_DMA_ENDINTEN_EPN(ep->id));
  1576. }
  1577. ret = 0;
  1578. end:
  1579. spin_unlock_irqrestore(&ep->udc->lock, flags);
  1580. return ret;
  1581. }
  1582. static int usbf_epn_disable(struct usbf_ep *epn)
  1583. {
  1584. /* Disable interrupts */
  1585. usbf_ep_reg_writel(epn, USBF_REG_EPN_INT_ENA, 0);
  1586. /* Disable endpoint */
  1587. usbf_ep_reg_bitclr(epn, USBF_REG_EPN_CONTROL, USBF_EPN_EN);
  1588. /* remove anything that was pending */
  1589. usbf_ep_nuke(epn, -ESHUTDOWN);
  1590. return 0;
  1591. }
  1592. static int usbf_ep_disable(struct usb_ep *_ep)
  1593. {
  1594. struct usbf_ep *ep = container_of(_ep, struct usbf_ep, ep);
  1595. struct usbf_udc *udc = ep->udc;
  1596. unsigned long flags;
  1597. int ret;
  1598. if (ep->id == 0)
  1599. return -EINVAL;
  1600. dev_dbg(ep->udc->dev, "ep%u %s mpkts %d\n", ep->id,
  1601. ep->is_in ? "in" : "out", ep->ep.maxpacket);
  1602. spin_lock_irqsave(&ep->udc->lock, flags);
  1603. ep->disabled = 1;
  1604. /* Disable DMA interrupt */
  1605. if (ep->dma_regs) {
  1606. usbf_reg_bitclr(udc, USBF_REG_AHBBINTEN,
  1607. USBF_SYS_DMA_ENDINTEN_EPN(ep->id));
  1608. ep->bridge_on_dma_end = NULL;
  1609. }
  1610. /* disable interrupts for this endpoint */
  1611. usbf_reg_bitclr(udc, USBF_REG_USB_INT_ENA, USBF_USB_EPN_EN(ep->id));
  1612. /* and the endpoint itself */
  1613. ret = usbf_epn_disable(ep);
  1614. spin_unlock_irqrestore(&ep->udc->lock, flags);
  1615. return ret;
  1616. }
  1617. static int usbf_ep0_queue(struct usbf_ep *ep0, struct usbf_req *req,
  1618. gfp_t gfp_flags)
  1619. {
  1620. int ret;
  1621. req->req.actual = 0;
  1622. req->req.status = -EINPROGRESS;
  1623. req->is_zero_sent = 0;
  1624. list_add_tail(&req->queue, &ep0->queue);
  1625. if (ep0->udc->ep0state == EP0_IN_STATUS_START_PHASE)
  1626. return 0;
  1627. if (!ep0->is_in)
  1628. return 0;
  1629. if (ep0->udc->ep0state == EP0_IN_STATUS_PHASE) {
  1630. if (req->req.length) {
  1631. dev_err(ep0->udc->dev,
  1632. "request lng %u for ep0 in status phase\n",
  1633. req->req.length);
  1634. return -EINVAL;
  1635. }
  1636. ep0->delayed_status = 0;
  1637. }
  1638. if (!ep0->is_processing) {
  1639. ret = usbf_ep0_pio_in(ep0, req);
  1640. if (ret != -EINPROGRESS) {
  1641. dev_err(ep0->udc->dev,
  1642. "queued request not in progress\n");
  1643. /* The request cannot be completed (ie
  1644. * ret == 0) on the first call
  1645. */
  1646. return ret ? ret : -EIO;
  1647. }
  1648. }
  1649. return 0;
  1650. }
  1651. static int usbf_epn_queue(struct usbf_ep *ep, struct usbf_req *req,
  1652. gfp_t gfp_flags)
  1653. {
  1654. int was_empty;
  1655. int ret;
  1656. if (ep->disabled) {
  1657. dev_err(ep->udc->dev, "ep%u request queue while disable\n",
  1658. ep->id);
  1659. return -ESHUTDOWN;
  1660. }
  1661. req->req.actual = 0;
  1662. req->req.status = -EINPROGRESS;
  1663. req->is_zero_sent = 0;
  1664. req->xfer_step = USBF_XFER_START;
  1665. was_empty = list_empty(&ep->queue);
  1666. list_add_tail(&req->queue, &ep->queue);
  1667. if (was_empty) {
  1668. ret = usbf_epn_start_queue(ep);
  1669. if (ret)
  1670. return ret;
  1671. }
  1672. return 0;
  1673. }
  1674. static int usbf_ep_queue(struct usb_ep *_ep, struct usb_request *_req,
  1675. gfp_t gfp_flags)
  1676. {
  1677. struct usbf_req *req = container_of(_req, struct usbf_req, req);
  1678. struct usbf_ep *ep = container_of(_ep, struct usbf_ep, ep);
  1679. struct usbf_udc *udc = ep->udc;
  1680. unsigned long flags;
  1681. int ret;
  1682. if (!_req || !_req->buf)
  1683. return -EINVAL;
  1684. if (!udc || !udc->driver)
  1685. return -EINVAL;
  1686. dev_dbg(ep->udc->dev, "ep%u %s req queue length %u, zero %u, short_not_ok %u\n",
  1687. ep->id, ep->is_in ? "in" : "out",
  1688. req->req.length, req->req.zero, req->req.short_not_ok);
  1689. spin_lock_irqsave(&ep->udc->lock, flags);
  1690. if (ep->id == 0)
  1691. ret = usbf_ep0_queue(ep, req, gfp_flags);
  1692. else
  1693. ret = usbf_epn_queue(ep, req, gfp_flags);
  1694. spin_unlock_irqrestore(&ep->udc->lock, flags);
  1695. return ret;
  1696. }
  1697. static int usbf_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
  1698. {
  1699. struct usbf_req *req = container_of(_req, struct usbf_req, req);
  1700. struct usbf_ep *ep = container_of(_ep, struct usbf_ep, ep);
  1701. unsigned long flags;
  1702. int is_processing;
  1703. int first;
  1704. int ret;
  1705. spin_lock_irqsave(&ep->udc->lock, flags);
  1706. dev_dbg(ep->udc->dev, "ep%u %s req dequeue length %u/%u\n",
  1707. ep->id, ep->is_in ? "in" : "out",
  1708. req->req.actual, req->req.length);
  1709. first = list_is_first(&req->queue, &ep->queue);
  1710. /* Complete the request but avoid any operation that could be done
  1711. * if a new request is queued during the request completion
  1712. */
  1713. is_processing = ep->is_processing;
  1714. ep->is_processing = 1;
  1715. usbf_ep_req_done(ep, req, -ECONNRESET);
  1716. ep->is_processing = is_processing;
  1717. if (first) {
  1718. /* The first item in the list was dequeued.
  1719. * This item could already be submitted to the hardware.
  1720. * So, flush the fifo
  1721. */
  1722. if (ep->id)
  1723. usbf_epn_fifo_flush(ep);
  1724. else
  1725. usbf_ep0_fifo_flush(ep);
  1726. }
  1727. if (ep->id == 0) {
  1728. /* We dequeue a request on ep0. On this endpoint, we can have
  1729. * 1 request related to the data stage and/or 1 request
  1730. * related to the status stage.
  1731. * We dequeue one of them and so the USB control transaction
  1732. * is no more coherent. The simple way to be consistent after
  1733. * dequeuing is to stall and nuke the endpoint and wait the
  1734. * next SETUP packet.
  1735. */
  1736. usbf_ep_stall(ep, true);
  1737. usbf_ep_nuke(ep, -ECONNRESET);
  1738. ep->udc->ep0state = EP0_IDLE;
  1739. goto end;
  1740. }
  1741. if (!first)
  1742. goto end;
  1743. ret = usbf_epn_start_queue(ep);
  1744. if (ret) {
  1745. usbf_ep_stall(ep, true);
  1746. usbf_ep_nuke(ep, -EIO);
  1747. }
  1748. end:
  1749. spin_unlock_irqrestore(&ep->udc->lock, flags);
  1750. return 0;
  1751. }
  1752. static struct usb_request *usbf_ep_alloc_request(struct usb_ep *_ep,
  1753. gfp_t gfp_flags)
  1754. {
  1755. struct usbf_req *req;
  1756. if (!_ep)
  1757. return NULL;
  1758. req = kzalloc_obj(*req, gfp_flags);
  1759. if (!req)
  1760. return NULL;
  1761. INIT_LIST_HEAD(&req->queue);
  1762. return &req->req;
  1763. }
  1764. static void usbf_ep_free_request(struct usb_ep *_ep, struct usb_request *_req)
  1765. {
  1766. struct usbf_req *req;
  1767. unsigned long flags;
  1768. struct usbf_ep *ep;
  1769. if (!_ep || !_req)
  1770. return;
  1771. req = container_of(_req, struct usbf_req, req);
  1772. ep = container_of(_ep, struct usbf_ep, ep);
  1773. spin_lock_irqsave(&ep->udc->lock, flags);
  1774. list_del_init(&req->queue);
  1775. spin_unlock_irqrestore(&ep->udc->lock, flags);
  1776. kfree(req);
  1777. }
  1778. static int usbf_ep_set_halt(struct usb_ep *_ep, int halt)
  1779. {
  1780. struct usbf_ep *ep = container_of(_ep, struct usbf_ep, ep);
  1781. unsigned long flags;
  1782. int ret;
  1783. if (ep->id == 0)
  1784. return -EINVAL;
  1785. spin_lock_irqsave(&ep->udc->lock, flags);
  1786. if (!list_empty(&ep->queue)) {
  1787. ret = -EAGAIN;
  1788. goto end;
  1789. }
  1790. usbf_ep_stall(ep, halt);
  1791. if (!halt)
  1792. ep->is_wedged = 0;
  1793. ret = 0;
  1794. end:
  1795. spin_unlock_irqrestore(&ep->udc->lock, flags);
  1796. return ret;
  1797. }
  1798. static int usbf_ep_set_wedge(struct usb_ep *_ep)
  1799. {
  1800. struct usbf_ep *ep = container_of(_ep, struct usbf_ep, ep);
  1801. unsigned long flags;
  1802. int ret;
  1803. if (ep->id == 0)
  1804. return -EINVAL;
  1805. spin_lock_irqsave(&ep->udc->lock, flags);
  1806. if (!list_empty(&ep->queue)) {
  1807. ret = -EAGAIN;
  1808. goto end;
  1809. }
  1810. usbf_ep_stall(ep, 1);
  1811. ep->is_wedged = 1;
  1812. ret = 0;
  1813. end:
  1814. spin_unlock_irqrestore(&ep->udc->lock, flags);
  1815. return ret;
  1816. }
  1817. static struct usb_ep_ops usbf_ep_ops = {
  1818. .enable = usbf_ep_enable,
  1819. .disable = usbf_ep_disable,
  1820. .queue = usbf_ep_queue,
  1821. .dequeue = usbf_ep_dequeue,
  1822. .set_halt = usbf_ep_set_halt,
  1823. .set_wedge = usbf_ep_set_wedge,
  1824. .alloc_request = usbf_ep_alloc_request,
  1825. .free_request = usbf_ep_free_request,
  1826. };
  1827. static void usbf_ep0_req_complete(struct usb_ep *_ep, struct usb_request *_req)
  1828. {
  1829. }
  1830. static void usbf_ep0_fill_req(struct usbf_ep *ep0, struct usbf_req *req,
  1831. void *buf, unsigned int length,
  1832. void (*complete)(struct usb_ep *_ep,
  1833. struct usb_request *_req))
  1834. {
  1835. if (buf && length)
  1836. memcpy(ep0->udc->ep0_buf, buf, length);
  1837. req->req.buf = ep0->udc->ep0_buf;
  1838. req->req.length = length;
  1839. req->req.dma = 0;
  1840. req->req.zero = true;
  1841. req->req.complete = complete ? complete : usbf_ep0_req_complete;
  1842. req->req.status = -EINPROGRESS;
  1843. req->req.context = NULL;
  1844. req->req.actual = 0;
  1845. }
  1846. static struct usbf_ep *usbf_get_ep_by_addr(struct usbf_udc *udc, u8 address)
  1847. {
  1848. struct usbf_ep *ep;
  1849. unsigned int i;
  1850. if ((address & USB_ENDPOINT_NUMBER_MASK) == 0)
  1851. return &udc->ep[0];
  1852. for (i = 1; i < ARRAY_SIZE(udc->ep); i++) {
  1853. ep = &udc->ep[i];
  1854. if (!ep->ep.desc)
  1855. continue;
  1856. if (ep->ep.desc->bEndpointAddress == address)
  1857. return ep;
  1858. }
  1859. return NULL;
  1860. }
  1861. static int usbf_req_delegate(struct usbf_udc *udc,
  1862. const struct usb_ctrlrequest *ctrlrequest)
  1863. {
  1864. int ret;
  1865. spin_unlock(&udc->lock);
  1866. ret = udc->driver->setup(&udc->gadget, ctrlrequest);
  1867. spin_lock(&udc->lock);
  1868. if (ret < 0) {
  1869. dev_dbg(udc->dev, "udc driver setup failed %d\n", ret);
  1870. return ret;
  1871. }
  1872. if (ret == USB_GADGET_DELAYED_STATUS) {
  1873. dev_dbg(udc->dev, "delayed status set\n");
  1874. udc->ep[0].delayed_status = 1;
  1875. return 0;
  1876. }
  1877. return ret;
  1878. }
  1879. static int usbf_req_get_status(struct usbf_udc *udc,
  1880. const struct usb_ctrlrequest *ctrlrequest)
  1881. {
  1882. struct usbf_ep *ep;
  1883. u16 status_data;
  1884. u16 wLength;
  1885. u16 wValue;
  1886. u16 wIndex;
  1887. wValue = le16_to_cpu(ctrlrequest->wValue);
  1888. wLength = le16_to_cpu(ctrlrequest->wLength);
  1889. wIndex = le16_to_cpu(ctrlrequest->wIndex);
  1890. switch (ctrlrequest->bRequestType) {
  1891. case USB_DIR_IN | USB_RECIP_DEVICE | USB_TYPE_STANDARD:
  1892. if ((wValue != 0) || (wIndex != 0) || (wLength != 2))
  1893. goto delegate;
  1894. status_data = 0;
  1895. if (udc->gadget.is_selfpowered)
  1896. status_data |= BIT(USB_DEVICE_SELF_POWERED);
  1897. if (udc->is_remote_wakeup)
  1898. status_data |= BIT(USB_DEVICE_REMOTE_WAKEUP);
  1899. break;
  1900. case USB_DIR_IN | USB_RECIP_ENDPOINT | USB_TYPE_STANDARD:
  1901. if ((wValue != 0) || (wLength != 2))
  1902. goto delegate;
  1903. ep = usbf_get_ep_by_addr(udc, wIndex);
  1904. if (!ep)
  1905. return -EINVAL;
  1906. status_data = 0;
  1907. if (usbf_ep_is_stalled(ep))
  1908. status_data |= cpu_to_le16(1);
  1909. break;
  1910. case USB_DIR_IN | USB_RECIP_INTERFACE | USB_TYPE_STANDARD:
  1911. if ((wValue != 0) || (wLength != 2))
  1912. goto delegate;
  1913. status_data = 0;
  1914. break;
  1915. default:
  1916. goto delegate;
  1917. }
  1918. usbf_ep0_fill_req(&udc->ep[0], &udc->setup_reply, &status_data,
  1919. sizeof(status_data), NULL);
  1920. usbf_ep0_queue(&udc->ep[0], &udc->setup_reply, GFP_ATOMIC);
  1921. return 0;
  1922. delegate:
  1923. return usbf_req_delegate(udc, ctrlrequest);
  1924. }
  1925. static int usbf_req_clear_set_feature(struct usbf_udc *udc,
  1926. const struct usb_ctrlrequest *ctrlrequest,
  1927. bool is_set)
  1928. {
  1929. struct usbf_ep *ep;
  1930. u16 wLength;
  1931. u16 wValue;
  1932. u16 wIndex;
  1933. wValue = le16_to_cpu(ctrlrequest->wValue);
  1934. wLength = le16_to_cpu(ctrlrequest->wLength);
  1935. wIndex = le16_to_cpu(ctrlrequest->wIndex);
  1936. switch (ctrlrequest->bRequestType) {
  1937. case USB_DIR_OUT | USB_RECIP_DEVICE:
  1938. if ((wIndex != 0) || (wLength != 0))
  1939. goto delegate;
  1940. if (wValue != cpu_to_le16(USB_DEVICE_REMOTE_WAKEUP))
  1941. goto delegate;
  1942. udc->is_remote_wakeup = is_set;
  1943. break;
  1944. case USB_DIR_OUT | USB_RECIP_ENDPOINT:
  1945. if (wLength != 0)
  1946. goto delegate;
  1947. ep = usbf_get_ep_by_addr(udc, wIndex);
  1948. if (!ep)
  1949. return -EINVAL;
  1950. if ((ep->id == 0) && is_set) {
  1951. /* Endpoint 0 cannot be halted (stalled)
  1952. * Returning an error code leads to a STALL on this ep0
  1953. * but keep the automate in a consistent state.
  1954. */
  1955. return -EINVAL;
  1956. }
  1957. if (ep->is_wedged && !is_set) {
  1958. /* Ignore CLEAR_FEATURE(HALT ENDPOINT) when the
  1959. * endpoint is wedged
  1960. */
  1961. break;
  1962. }
  1963. usbf_ep_stall(ep, is_set);
  1964. break;
  1965. default:
  1966. goto delegate;
  1967. }
  1968. return 0;
  1969. delegate:
  1970. return usbf_req_delegate(udc, ctrlrequest);
  1971. }
  1972. static void usbf_ep0_req_set_address_complete(struct usb_ep *_ep,
  1973. struct usb_request *_req)
  1974. {
  1975. struct usbf_ep *ep = container_of(_ep, struct usbf_ep, ep);
  1976. /* The status phase of the SET_ADDRESS request is completed ... */
  1977. if (_req->status == 0) {
  1978. /* ... without any errors -> Signaled the state to the core. */
  1979. usb_gadget_set_state(&ep->udc->gadget, USB_STATE_ADDRESS);
  1980. }
  1981. /* In case of request failure, there is no need to revert the address
  1982. * value set to the hardware as the hardware will take care of the
  1983. * value only if the status stage is completed normally.
  1984. */
  1985. }
  1986. static int usbf_req_set_address(struct usbf_udc *udc,
  1987. const struct usb_ctrlrequest *ctrlrequest)
  1988. {
  1989. u16 wLength;
  1990. u16 wValue;
  1991. u16 wIndex;
  1992. u32 addr;
  1993. wValue = le16_to_cpu(ctrlrequest->wValue);
  1994. wLength = le16_to_cpu(ctrlrequest->wLength);
  1995. wIndex = le16_to_cpu(ctrlrequest->wIndex);
  1996. if (ctrlrequest->bRequestType != (USB_DIR_OUT | USB_RECIP_DEVICE))
  1997. goto delegate;
  1998. if ((wIndex != 0) || (wLength != 0) || (wValue > 127))
  1999. return -EINVAL;
  2000. addr = wValue;
  2001. /* The hardware will take care of this USB address after the status
  2002. * stage of the SET_ADDRESS request is completed normally.
  2003. * It is safe to write it now
  2004. */
  2005. usbf_reg_writel(udc, USBF_REG_USB_ADDRESS, USBF_USB_SET_USB_ADDR(addr));
  2006. /* Queued the status request */
  2007. usbf_ep0_fill_req(&udc->ep[0], &udc->setup_reply, NULL, 0,
  2008. usbf_ep0_req_set_address_complete);
  2009. usbf_ep0_queue(&udc->ep[0], &udc->setup_reply, GFP_ATOMIC);
  2010. return 0;
  2011. delegate:
  2012. return usbf_req_delegate(udc, ctrlrequest);
  2013. }
  2014. static int usbf_req_set_configuration(struct usbf_udc *udc,
  2015. const struct usb_ctrlrequest *ctrlrequest)
  2016. {
  2017. u16 wLength;
  2018. u16 wValue;
  2019. u16 wIndex;
  2020. int ret;
  2021. ret = usbf_req_delegate(udc, ctrlrequest);
  2022. if (ret)
  2023. return ret;
  2024. wValue = le16_to_cpu(ctrlrequest->wValue);
  2025. wLength = le16_to_cpu(ctrlrequest->wLength);
  2026. wIndex = le16_to_cpu(ctrlrequest->wIndex);
  2027. if ((ctrlrequest->bRequestType != (USB_DIR_OUT | USB_RECIP_DEVICE)) ||
  2028. (wIndex != 0) || (wLength != 0)) {
  2029. /* No error detected by driver->setup() but it is not an USB2.0
  2030. * Ch9 SET_CONFIGURATION.
  2031. * Nothing more to do
  2032. */
  2033. return 0;
  2034. }
  2035. if (wValue & 0x00FF) {
  2036. usbf_reg_bitset(udc, USBF_REG_USB_CONTROL, USBF_USB_CONF);
  2037. } else {
  2038. usbf_reg_bitclr(udc, USBF_REG_USB_CONTROL, USBF_USB_CONF);
  2039. /* Go back to Address State */
  2040. spin_unlock(&udc->lock);
  2041. usb_gadget_set_state(&udc->gadget, USB_STATE_ADDRESS);
  2042. spin_lock(&udc->lock);
  2043. }
  2044. return 0;
  2045. }
  2046. static int usbf_handle_ep0_setup(struct usbf_ep *ep0)
  2047. {
  2048. union {
  2049. struct usb_ctrlrequest ctrlreq;
  2050. u32 raw[2];
  2051. } crq;
  2052. struct usbf_udc *udc = ep0->udc;
  2053. int ret;
  2054. /* Read setup data (ie the USB control request) */
  2055. crq.raw[0] = usbf_reg_readl(udc, USBF_REG_SETUP_DATA0);
  2056. crq.raw[1] = usbf_reg_readl(udc, USBF_REG_SETUP_DATA1);
  2057. dev_dbg(ep0->udc->dev,
  2058. "ep0 req%02x.%02x, wValue 0x%04x, wIndex 0x%04x, wLength 0x%04x\n",
  2059. crq.ctrlreq.bRequestType, crq.ctrlreq.bRequest,
  2060. crq.ctrlreq.wValue, crq.ctrlreq.wIndex, crq.ctrlreq.wLength);
  2061. /* Set current EP0 state according to the received request */
  2062. if (crq.ctrlreq.wLength) {
  2063. if (crq.ctrlreq.bRequestType & USB_DIR_IN) {
  2064. udc->ep0state = EP0_IN_DATA_PHASE;
  2065. usbf_ep_reg_clrset(ep0, USBF_REG_EP0_CONTROL,
  2066. USBF_EP0_INAK,
  2067. USBF_EP0_INAK_EN);
  2068. ep0->is_in = 1;
  2069. } else {
  2070. udc->ep0state = EP0_OUT_DATA_PHASE;
  2071. usbf_ep_reg_bitclr(ep0, USBF_REG_EP0_CONTROL,
  2072. USBF_EP0_ONAK);
  2073. ep0->is_in = 0;
  2074. }
  2075. } else {
  2076. udc->ep0state = EP0_IN_STATUS_START_PHASE;
  2077. ep0->is_in = 1;
  2078. }
  2079. /* We starts a new control transfer -> Clear the delayed status flag */
  2080. ep0->delayed_status = 0;
  2081. if ((crq.ctrlreq.bRequestType & USB_TYPE_MASK) != USB_TYPE_STANDARD) {
  2082. /* This is not a USB standard request -> delegate */
  2083. goto delegate;
  2084. }
  2085. switch (crq.ctrlreq.bRequest) {
  2086. case USB_REQ_GET_STATUS:
  2087. ret = usbf_req_get_status(udc, &crq.ctrlreq);
  2088. break;
  2089. case USB_REQ_CLEAR_FEATURE:
  2090. ret = usbf_req_clear_set_feature(udc, &crq.ctrlreq, false);
  2091. break;
  2092. case USB_REQ_SET_FEATURE:
  2093. ret = usbf_req_clear_set_feature(udc, &crq.ctrlreq, true);
  2094. break;
  2095. case USB_REQ_SET_ADDRESS:
  2096. ret = usbf_req_set_address(udc, &crq.ctrlreq);
  2097. break;
  2098. case USB_REQ_SET_CONFIGURATION:
  2099. ret = usbf_req_set_configuration(udc, &crq.ctrlreq);
  2100. break;
  2101. default:
  2102. goto delegate;
  2103. }
  2104. return ret;
  2105. delegate:
  2106. return usbf_req_delegate(udc, &crq.ctrlreq);
  2107. }
  2108. static int usbf_handle_ep0_data_status(struct usbf_ep *ep0,
  2109. const char *ep0state_name,
  2110. enum usbf_ep0state next_ep0state)
  2111. {
  2112. struct usbf_udc *udc = ep0->udc;
  2113. int ret;
  2114. ret = usbf_ep_process_queue(ep0);
  2115. switch (ret) {
  2116. case -ENOENT:
  2117. dev_err(udc->dev,
  2118. "no request available for ep0 %s phase\n",
  2119. ep0state_name);
  2120. break;
  2121. case -EINPROGRESS:
  2122. /* More data needs to be processed */
  2123. ret = 0;
  2124. break;
  2125. case 0:
  2126. /* All requests in the queue are processed */
  2127. udc->ep0state = next_ep0state;
  2128. break;
  2129. default:
  2130. dev_err(udc->dev,
  2131. "process queue failed for ep0 %s phase (%d)\n",
  2132. ep0state_name, ret);
  2133. break;
  2134. }
  2135. return ret;
  2136. }
  2137. static int usbf_handle_ep0_out_status_start(struct usbf_ep *ep0)
  2138. {
  2139. struct usbf_udc *udc = ep0->udc;
  2140. struct usbf_req *req;
  2141. usbf_ep_reg_clrset(ep0, USBF_REG_EP0_CONTROL,
  2142. USBF_EP0_ONAK,
  2143. USBF_EP0_PIDCLR);
  2144. ep0->is_in = 0;
  2145. req = list_first_entry_or_null(&ep0->queue, struct usbf_req, queue);
  2146. if (!req) {
  2147. usbf_ep0_fill_req(ep0, &udc->setup_reply, NULL, 0, NULL);
  2148. usbf_ep0_queue(ep0, &udc->setup_reply, GFP_ATOMIC);
  2149. } else {
  2150. if (req->req.length) {
  2151. dev_err(udc->dev,
  2152. "queued request length %u for ep0 out status phase\n",
  2153. req->req.length);
  2154. }
  2155. }
  2156. udc->ep0state = EP0_OUT_STATUS_PHASE;
  2157. return 0;
  2158. }
  2159. static int usbf_handle_ep0_in_status_start(struct usbf_ep *ep0)
  2160. {
  2161. struct usbf_udc *udc = ep0->udc;
  2162. struct usbf_req *req;
  2163. int ret;
  2164. usbf_ep_reg_clrset(ep0, USBF_REG_EP0_CONTROL,
  2165. USBF_EP0_INAK,
  2166. USBF_EP0_INAK_EN | USBF_EP0_PIDCLR);
  2167. ep0->is_in = 1;
  2168. /* Queue request for status if needed */
  2169. req = list_first_entry_or_null(&ep0->queue, struct usbf_req, queue);
  2170. if (!req) {
  2171. if (ep0->delayed_status) {
  2172. dev_dbg(ep0->udc->dev,
  2173. "EP0_IN_STATUS_START_PHASE ep0->delayed_status set\n");
  2174. udc->ep0state = EP0_IN_STATUS_PHASE;
  2175. return 0;
  2176. }
  2177. usbf_ep0_fill_req(ep0, &udc->setup_reply, NULL,
  2178. 0, NULL);
  2179. usbf_ep0_queue(ep0, &udc->setup_reply,
  2180. GFP_ATOMIC);
  2181. req = list_first_entry_or_null(&ep0->queue, struct usbf_req, queue);
  2182. } else {
  2183. if (req->req.length) {
  2184. dev_err(udc->dev,
  2185. "queued request length %u for ep0 in status phase\n",
  2186. req->req.length);
  2187. }
  2188. }
  2189. ret = usbf_ep0_pio_in(ep0, req);
  2190. if (ret != -EINPROGRESS) {
  2191. usbf_ep_req_done(ep0, req, ret);
  2192. udc->ep0state = EP0_IN_STATUS_END_PHASE;
  2193. return 0;
  2194. }
  2195. udc->ep0state = EP0_IN_STATUS_PHASE;
  2196. return 0;
  2197. }
  2198. static void usbf_ep0_interrupt(struct usbf_ep *ep0)
  2199. {
  2200. struct usbf_udc *udc = ep0->udc;
  2201. u32 sts, prev_sts;
  2202. int prev_ep0state;
  2203. int ret;
  2204. ep0->status = usbf_ep_reg_readl(ep0, USBF_REG_EP0_STATUS);
  2205. usbf_ep_reg_writel(ep0, USBF_REG_EP0_STATUS, ~ep0->status);
  2206. dev_dbg(ep0->udc->dev, "ep0 status=0x%08x, enable=%08x\n, ctrl=0x%08x\n",
  2207. ep0->status,
  2208. usbf_ep_reg_readl(ep0, USBF_REG_EP0_INT_ENA),
  2209. usbf_ep_reg_readl(ep0, USBF_REG_EP0_CONTROL));
  2210. sts = ep0->status & (USBF_EP0_SETUP_INT | USBF_EP0_IN_INT | USBF_EP0_OUT_INT |
  2211. USBF_EP0_OUT_NULL_INT | USBF_EP0_STG_START_INT |
  2212. USBF_EP0_STG_END_INT);
  2213. ret = 0;
  2214. do {
  2215. dev_dbg(ep0->udc->dev, "udc->ep0state=%d\n", udc->ep0state);
  2216. prev_sts = sts;
  2217. prev_ep0state = udc->ep0state;
  2218. switch (udc->ep0state) {
  2219. case EP0_IDLE:
  2220. if (!(sts & USBF_EP0_SETUP_INT))
  2221. break;
  2222. sts &= ~USBF_EP0_SETUP_INT;
  2223. dev_dbg(ep0->udc->dev, "ep0 handle setup\n");
  2224. ret = usbf_handle_ep0_setup(ep0);
  2225. break;
  2226. case EP0_IN_DATA_PHASE:
  2227. if (!(sts & USBF_EP0_IN_INT))
  2228. break;
  2229. sts &= ~USBF_EP0_IN_INT;
  2230. dev_dbg(ep0->udc->dev, "ep0 handle in data phase\n");
  2231. ret = usbf_handle_ep0_data_status(ep0,
  2232. "in data", EP0_OUT_STATUS_START_PHASE);
  2233. break;
  2234. case EP0_OUT_STATUS_START_PHASE:
  2235. if (!(sts & USBF_EP0_STG_START_INT))
  2236. break;
  2237. sts &= ~USBF_EP0_STG_START_INT;
  2238. dev_dbg(ep0->udc->dev, "ep0 handle out status start phase\n");
  2239. ret = usbf_handle_ep0_out_status_start(ep0);
  2240. break;
  2241. case EP0_OUT_STATUS_PHASE:
  2242. if (!(sts & (USBF_EP0_OUT_INT | USBF_EP0_OUT_NULL_INT)))
  2243. break;
  2244. sts &= ~(USBF_EP0_OUT_INT | USBF_EP0_OUT_NULL_INT);
  2245. dev_dbg(ep0->udc->dev, "ep0 handle out status phase\n");
  2246. ret = usbf_handle_ep0_data_status(ep0,
  2247. "out status",
  2248. EP0_OUT_STATUS_END_PHASE);
  2249. break;
  2250. case EP0_OUT_STATUS_END_PHASE:
  2251. if (!(sts & (USBF_EP0_STG_END_INT | USBF_EP0_SETUP_INT)))
  2252. break;
  2253. sts &= ~USBF_EP0_STG_END_INT;
  2254. dev_dbg(ep0->udc->dev, "ep0 handle out status end phase\n");
  2255. udc->ep0state = EP0_IDLE;
  2256. break;
  2257. case EP0_OUT_DATA_PHASE:
  2258. if (!(sts & (USBF_EP0_OUT_INT | USBF_EP0_OUT_NULL_INT)))
  2259. break;
  2260. sts &= ~(USBF_EP0_OUT_INT | USBF_EP0_OUT_NULL_INT);
  2261. dev_dbg(ep0->udc->dev, "ep0 handle out data phase\n");
  2262. ret = usbf_handle_ep0_data_status(ep0,
  2263. "out data", EP0_IN_STATUS_START_PHASE);
  2264. break;
  2265. case EP0_IN_STATUS_START_PHASE:
  2266. if (!(sts & USBF_EP0_STG_START_INT))
  2267. break;
  2268. sts &= ~USBF_EP0_STG_START_INT;
  2269. dev_dbg(ep0->udc->dev, "ep0 handle in status start phase\n");
  2270. ret = usbf_handle_ep0_in_status_start(ep0);
  2271. break;
  2272. case EP0_IN_STATUS_PHASE:
  2273. if (!(sts & USBF_EP0_IN_INT))
  2274. break;
  2275. sts &= ~USBF_EP0_IN_INT;
  2276. dev_dbg(ep0->udc->dev, "ep0 handle in status phase\n");
  2277. ret = usbf_handle_ep0_data_status(ep0,
  2278. "in status", EP0_IN_STATUS_END_PHASE);
  2279. break;
  2280. case EP0_IN_STATUS_END_PHASE:
  2281. if (!(sts & (USBF_EP0_STG_END_INT | USBF_EP0_SETUP_INT)))
  2282. break;
  2283. sts &= ~USBF_EP0_STG_END_INT;
  2284. dev_dbg(ep0->udc->dev, "ep0 handle in status end\n");
  2285. udc->ep0state = EP0_IDLE;
  2286. break;
  2287. default:
  2288. udc->ep0state = EP0_IDLE;
  2289. break;
  2290. }
  2291. if (ret) {
  2292. dev_dbg(ep0->udc->dev, "ep0 failed (%d)\n", ret);
  2293. /* Failure -> stall.
  2294. * This stall state will be automatically cleared when
  2295. * the IP receives the next SETUP packet
  2296. */
  2297. usbf_ep_stall(ep0, true);
  2298. /* Remove anything that was pending */
  2299. usbf_ep_nuke(ep0, -EPROTO);
  2300. udc->ep0state = EP0_IDLE;
  2301. break;
  2302. }
  2303. } while ((prev_ep0state != udc->ep0state) || (prev_sts != sts));
  2304. dev_dbg(ep0->udc->dev, "ep0 done udc->ep0state=%d, status=0x%08x. next=0x%08x\n",
  2305. udc->ep0state, sts,
  2306. usbf_ep_reg_readl(ep0, USBF_REG_EP0_STATUS));
  2307. }
  2308. static void usbf_epn_process_queue(struct usbf_ep *epn)
  2309. {
  2310. int ret;
  2311. ret = usbf_ep_process_queue(epn);
  2312. switch (ret) {
  2313. case -ENOENT:
  2314. dev_warn(epn->udc->dev, "ep%u %s, no request available\n",
  2315. epn->id, epn->is_in ? "in" : "out");
  2316. break;
  2317. case -EINPROGRESS:
  2318. /* More data needs to be processed */
  2319. ret = 0;
  2320. break;
  2321. case 0:
  2322. /* All requests in the queue are processed */
  2323. break;
  2324. default:
  2325. dev_err(epn->udc->dev, "ep%u %s, process queue failed (%d)\n",
  2326. epn->id, epn->is_in ? "in" : "out", ret);
  2327. break;
  2328. }
  2329. if (ret) {
  2330. dev_dbg(epn->udc->dev, "ep%u %s failed (%d)\n", epn->id,
  2331. epn->is_in ? "in" : "out", ret);
  2332. usbf_ep_stall(epn, true);
  2333. usbf_ep_nuke(epn, ret);
  2334. }
  2335. }
  2336. static void usbf_epn_interrupt(struct usbf_ep *epn)
  2337. {
  2338. u32 sts;
  2339. u32 ena;
  2340. epn->status = usbf_ep_reg_readl(epn, USBF_REG_EPN_STATUS);
  2341. ena = usbf_ep_reg_readl(epn, USBF_REG_EPN_INT_ENA);
  2342. usbf_ep_reg_writel(epn, USBF_REG_EPN_STATUS, ~(epn->status & ena));
  2343. dev_dbg(epn->udc->dev, "ep%u %s status=0x%08x, enable=%08x\n, ctrl=0x%08x\n",
  2344. epn->id, epn->is_in ? "in" : "out", epn->status, ena,
  2345. usbf_ep_reg_readl(epn, USBF_REG_EPN_CONTROL));
  2346. if (epn->disabled) {
  2347. dev_warn(epn->udc->dev, "ep%u %s, interrupt while disabled\n",
  2348. epn->id, epn->is_in ? "in" : "out");
  2349. return;
  2350. }
  2351. sts = epn->status & ena;
  2352. if (sts & (USBF_EPN_IN_END_INT | USBF_EPN_IN_INT)) {
  2353. sts &= ~(USBF_EPN_IN_END_INT | USBF_EPN_IN_INT);
  2354. dev_dbg(epn->udc->dev, "ep%u %s process queue (in interrupts)\n",
  2355. epn->id, epn->is_in ? "in" : "out");
  2356. usbf_epn_process_queue(epn);
  2357. }
  2358. if (sts & (USBF_EPN_OUT_END_INT | USBF_EPN_OUT_INT | USBF_EPN_OUT_NULL_INT)) {
  2359. sts &= ~(USBF_EPN_OUT_END_INT | USBF_EPN_OUT_INT | USBF_EPN_OUT_NULL_INT);
  2360. dev_dbg(epn->udc->dev, "ep%u %s process queue (out interrupts)\n",
  2361. epn->id, epn->is_in ? "in" : "out");
  2362. usbf_epn_process_queue(epn);
  2363. }
  2364. dev_dbg(epn->udc->dev, "ep%u %s done status=0x%08x. next=0x%08x\n",
  2365. epn->id, epn->is_in ? "in" : "out",
  2366. sts, usbf_ep_reg_readl(epn, USBF_REG_EPN_STATUS));
  2367. }
  2368. static void usbf_ep_reset(struct usbf_ep *ep)
  2369. {
  2370. ep->status = 0;
  2371. /* Remove anything that was pending */
  2372. usbf_ep_nuke(ep, -ESHUTDOWN);
  2373. }
  2374. static void usbf_reset(struct usbf_udc *udc)
  2375. {
  2376. int i;
  2377. for (i = 0; i < ARRAY_SIZE(udc->ep); i++) {
  2378. if (udc->ep[i].disabled)
  2379. continue;
  2380. usbf_ep_reset(&udc->ep[i]);
  2381. }
  2382. if (usbf_reg_readl(udc, USBF_REG_USB_STATUS) & USBF_USB_SPEED_MODE)
  2383. udc->gadget.speed = USB_SPEED_HIGH;
  2384. else
  2385. udc->gadget.speed = USB_SPEED_FULL;
  2386. /* Remote wakeup feature must be disabled on USB bus reset */
  2387. udc->is_remote_wakeup = false;
  2388. /* Enable endpoint zero */
  2389. usbf_ep0_enable(&udc->ep[0]);
  2390. if (udc->driver) {
  2391. /* Signal the reset */
  2392. spin_unlock(&udc->lock);
  2393. usb_gadget_udc_reset(&udc->gadget, udc->driver);
  2394. spin_lock(&udc->lock);
  2395. }
  2396. }
  2397. static void usbf_driver_suspend(struct usbf_udc *udc)
  2398. {
  2399. if (udc->is_usb_suspended) {
  2400. dev_dbg(udc->dev, "already suspended\n");
  2401. return;
  2402. }
  2403. dev_dbg(udc->dev, "do usb suspend\n");
  2404. udc->is_usb_suspended = true;
  2405. if (udc->driver && udc->driver->suspend) {
  2406. spin_unlock(&udc->lock);
  2407. udc->driver->suspend(&udc->gadget);
  2408. spin_lock(&udc->lock);
  2409. /* The datasheet tells to set the USB_CONTROL register SUSPEND
  2410. * bit when the USB bus suspend is detected.
  2411. * This bit stops the clocks (clocks for EPC, SIE, USBPHY) but
  2412. * these clocks seems not used only by the USB device. Some
  2413. * UARTs can be lost ...
  2414. * So, do not set the USB_CONTROL register SUSPEND bit.
  2415. */
  2416. }
  2417. }
  2418. static void usbf_driver_resume(struct usbf_udc *udc)
  2419. {
  2420. if (!udc->is_usb_suspended)
  2421. return;
  2422. dev_dbg(udc->dev, "do usb resume\n");
  2423. udc->is_usb_suspended = false;
  2424. if (udc->driver && udc->driver->resume) {
  2425. spin_unlock(&udc->lock);
  2426. udc->driver->resume(&udc->gadget);
  2427. spin_lock(&udc->lock);
  2428. }
  2429. }
  2430. static irqreturn_t usbf_epc_irq(int irq, void *_udc)
  2431. {
  2432. struct usbf_udc *udc = (struct usbf_udc *)_udc;
  2433. unsigned long flags;
  2434. struct usbf_ep *ep;
  2435. u32 int_sts;
  2436. u32 int_en;
  2437. int i;
  2438. spin_lock_irqsave(&udc->lock, flags);
  2439. int_en = usbf_reg_readl(udc, USBF_REG_USB_INT_ENA);
  2440. int_sts = usbf_reg_readl(udc, USBF_REG_USB_INT_STA) & int_en;
  2441. usbf_reg_writel(udc, USBF_REG_USB_INT_STA, ~int_sts);
  2442. dev_dbg(udc->dev, "int_sts=0x%08x\n", int_sts);
  2443. if (int_sts & USBF_USB_RSUM_INT) {
  2444. dev_dbg(udc->dev, "handle resume\n");
  2445. usbf_driver_resume(udc);
  2446. }
  2447. if (int_sts & USBF_USB_USB_RST_INT) {
  2448. dev_dbg(udc->dev, "handle bus reset\n");
  2449. usbf_driver_resume(udc);
  2450. usbf_reset(udc);
  2451. }
  2452. if (int_sts & USBF_USB_SPEED_MODE_INT) {
  2453. if (usbf_reg_readl(udc, USBF_REG_USB_STATUS) & USBF_USB_SPEED_MODE)
  2454. udc->gadget.speed = USB_SPEED_HIGH;
  2455. else
  2456. udc->gadget.speed = USB_SPEED_FULL;
  2457. dev_dbg(udc->dev, "handle speed change (%s)\n",
  2458. udc->gadget.speed == USB_SPEED_HIGH ? "High" : "Full");
  2459. }
  2460. if (int_sts & USBF_USB_EPN_INT(0)) {
  2461. usbf_driver_resume(udc);
  2462. usbf_ep0_interrupt(&udc->ep[0]);
  2463. }
  2464. for (i = 1; i < ARRAY_SIZE(udc->ep); i++) {
  2465. ep = &udc->ep[i];
  2466. if (int_sts & USBF_USB_EPN_INT(i)) {
  2467. usbf_driver_resume(udc);
  2468. usbf_epn_interrupt(ep);
  2469. }
  2470. }
  2471. if (int_sts & USBF_USB_SPND_INT) {
  2472. dev_dbg(udc->dev, "handle suspend\n");
  2473. usbf_driver_suspend(udc);
  2474. }
  2475. spin_unlock_irqrestore(&udc->lock, flags);
  2476. return IRQ_HANDLED;
  2477. }
  2478. static irqreturn_t usbf_ahb_epc_irq(int irq, void *_udc)
  2479. {
  2480. struct usbf_udc *udc = (struct usbf_udc *)_udc;
  2481. unsigned long flags;
  2482. struct usbf_ep *epn;
  2483. u32 sysbint;
  2484. void (*ep_action)(struct usbf_ep *epn);
  2485. int i;
  2486. spin_lock_irqsave(&udc->lock, flags);
  2487. /* Read and ack interrupts */
  2488. sysbint = usbf_reg_readl(udc, USBF_REG_AHBBINT);
  2489. usbf_reg_writel(udc, USBF_REG_AHBBINT, sysbint);
  2490. if ((sysbint & USBF_SYS_VBUS_INT) == USBF_SYS_VBUS_INT) {
  2491. if (usbf_reg_readl(udc, USBF_REG_EPCTR) & USBF_SYS_VBUS_LEVEL) {
  2492. dev_dbg(udc->dev, "handle vbus (1)\n");
  2493. spin_unlock(&udc->lock);
  2494. usb_udc_vbus_handler(&udc->gadget, true);
  2495. usb_gadget_set_state(&udc->gadget, USB_STATE_POWERED);
  2496. spin_lock(&udc->lock);
  2497. } else {
  2498. dev_dbg(udc->dev, "handle vbus (0)\n");
  2499. udc->is_usb_suspended = false;
  2500. spin_unlock(&udc->lock);
  2501. usb_udc_vbus_handler(&udc->gadget, false);
  2502. usb_gadget_set_state(&udc->gadget,
  2503. USB_STATE_NOTATTACHED);
  2504. spin_lock(&udc->lock);
  2505. }
  2506. }
  2507. for (i = 1; i < ARRAY_SIZE(udc->ep); i++) {
  2508. if (sysbint & USBF_SYS_DMA_ENDINT_EPN(i)) {
  2509. epn = &udc->ep[i];
  2510. dev_dbg(epn->udc->dev,
  2511. "ep%u handle DMA complete. action=%ps\n",
  2512. epn->id, epn->bridge_on_dma_end);
  2513. ep_action = epn->bridge_on_dma_end;
  2514. if (ep_action) {
  2515. epn->bridge_on_dma_end = NULL;
  2516. ep_action(epn);
  2517. }
  2518. }
  2519. }
  2520. spin_unlock_irqrestore(&udc->lock, flags);
  2521. return IRQ_HANDLED;
  2522. }
  2523. static int usbf_udc_start(struct usb_gadget *gadget,
  2524. struct usb_gadget_driver *driver)
  2525. {
  2526. struct usbf_udc *udc = container_of(gadget, struct usbf_udc, gadget);
  2527. unsigned long flags;
  2528. dev_info(udc->dev, "start (driver '%s')\n", driver->driver.name);
  2529. spin_lock_irqsave(&udc->lock, flags);
  2530. /* hook up the driver */
  2531. udc->driver = driver;
  2532. /* Enable VBUS interrupt */
  2533. usbf_reg_writel(udc, USBF_REG_AHBBINTEN, USBF_SYS_VBUS_INTEN);
  2534. spin_unlock_irqrestore(&udc->lock, flags);
  2535. return 0;
  2536. }
  2537. static int usbf_udc_stop(struct usb_gadget *gadget)
  2538. {
  2539. struct usbf_udc *udc = container_of(gadget, struct usbf_udc, gadget);
  2540. unsigned long flags;
  2541. spin_lock_irqsave(&udc->lock, flags);
  2542. /* Disable VBUS interrupt */
  2543. usbf_reg_writel(udc, USBF_REG_AHBBINTEN, 0);
  2544. udc->driver = NULL;
  2545. spin_unlock_irqrestore(&udc->lock, flags);
  2546. dev_info(udc->dev, "stopped\n");
  2547. return 0;
  2548. }
  2549. static int usbf_get_frame(struct usb_gadget *gadget)
  2550. {
  2551. struct usbf_udc *udc = container_of(gadget, struct usbf_udc, gadget);
  2552. return USBF_USB_GET_FRAME(usbf_reg_readl(udc, USBF_REG_USB_ADDRESS));
  2553. }
  2554. static void usbf_attach(struct usbf_udc *udc)
  2555. {
  2556. /* Enable USB signal to Function PHY
  2557. * D+ signal Pull-up
  2558. * Disable endpoint 0, it will be automatically enable when a USB reset
  2559. * is received.
  2560. * Disable the other endpoints
  2561. */
  2562. usbf_reg_clrset(udc, USBF_REG_USB_CONTROL,
  2563. USBF_USB_CONNECTB | USBF_USB_DEFAULT | USBF_USB_CONF,
  2564. USBF_USB_PUE2);
  2565. /* Enable reset and mode change interrupts */
  2566. usbf_reg_bitset(udc, USBF_REG_USB_INT_ENA,
  2567. USBF_USB_USB_RST_EN | USBF_USB_SPEED_MODE_EN | USBF_USB_RSUM_EN | USBF_USB_SPND_EN);
  2568. }
  2569. static void usbf_detach(struct usbf_udc *udc)
  2570. {
  2571. int i;
  2572. /* Disable interrupts */
  2573. usbf_reg_writel(udc, USBF_REG_USB_INT_ENA, 0);
  2574. for (i = 0; i < ARRAY_SIZE(udc->ep); i++) {
  2575. if (udc->ep[i].disabled)
  2576. continue;
  2577. usbf_ep_reset(&udc->ep[i]);
  2578. }
  2579. /* Disable USB signal to Function PHY
  2580. * Do not Pull-up D+ signal
  2581. * Disable endpoint 0
  2582. * Disable the other endpoints
  2583. */
  2584. usbf_reg_clrset(udc, USBF_REG_USB_CONTROL,
  2585. USBF_USB_PUE2 | USBF_USB_DEFAULT | USBF_USB_CONF,
  2586. USBF_USB_CONNECTB);
  2587. }
  2588. static int usbf_pullup(struct usb_gadget *gadget, int is_on)
  2589. {
  2590. struct usbf_udc *udc = container_of(gadget, struct usbf_udc, gadget);
  2591. unsigned long flags;
  2592. dev_dbg(udc->dev, "pullup %d\n", is_on);
  2593. spin_lock_irqsave(&udc->lock, flags);
  2594. if (is_on)
  2595. usbf_attach(udc);
  2596. else
  2597. usbf_detach(udc);
  2598. spin_unlock_irqrestore(&udc->lock, flags);
  2599. return 0;
  2600. }
  2601. static int usbf_udc_set_selfpowered(struct usb_gadget *gadget,
  2602. int is_selfpowered)
  2603. {
  2604. struct usbf_udc *udc = container_of(gadget, struct usbf_udc, gadget);
  2605. unsigned long flags;
  2606. spin_lock_irqsave(&udc->lock, flags);
  2607. gadget->is_selfpowered = (is_selfpowered != 0);
  2608. spin_unlock_irqrestore(&udc->lock, flags);
  2609. return 0;
  2610. }
  2611. static int usbf_udc_wakeup(struct usb_gadget *gadget)
  2612. {
  2613. struct usbf_udc *udc = container_of(gadget, struct usbf_udc, gadget);
  2614. unsigned long flags;
  2615. int ret;
  2616. spin_lock_irqsave(&udc->lock, flags);
  2617. if (!udc->is_remote_wakeup) {
  2618. dev_dbg(udc->dev, "remote wakeup not allowed\n");
  2619. ret = -EINVAL;
  2620. goto end;
  2621. }
  2622. dev_dbg(udc->dev, "do wakeup\n");
  2623. /* Send the resume signal */
  2624. usbf_reg_bitset(udc, USBF_REG_USB_CONTROL, USBF_USB_RSUM_IN);
  2625. usbf_reg_bitclr(udc, USBF_REG_USB_CONTROL, USBF_USB_RSUM_IN);
  2626. ret = 0;
  2627. end:
  2628. spin_unlock_irqrestore(&udc->lock, flags);
  2629. return ret;
  2630. }
  2631. static struct usb_gadget_ops usbf_gadget_ops = {
  2632. .get_frame = usbf_get_frame,
  2633. .pullup = usbf_pullup,
  2634. .udc_start = usbf_udc_start,
  2635. .udc_stop = usbf_udc_stop,
  2636. .set_selfpowered = usbf_udc_set_selfpowered,
  2637. .wakeup = usbf_udc_wakeup,
  2638. };
  2639. static int usbf_epn_check(struct usbf_ep *epn)
  2640. {
  2641. const char *type_txt;
  2642. const char *buf_txt;
  2643. int ret = 0;
  2644. u32 ctrl;
  2645. ctrl = usbf_ep_reg_readl(epn, USBF_REG_EPN_CONTROL);
  2646. switch (ctrl & USBF_EPN_MODE_MASK) {
  2647. case USBF_EPN_MODE_BULK:
  2648. type_txt = "bulk";
  2649. if (epn->ep.caps.type_control || epn->ep.caps.type_iso ||
  2650. !epn->ep.caps.type_bulk || epn->ep.caps.type_int) {
  2651. dev_err(epn->udc->dev,
  2652. "ep%u caps mismatch, bulk expected\n", epn->id);
  2653. ret = -EINVAL;
  2654. }
  2655. break;
  2656. case USBF_EPN_MODE_INTR:
  2657. type_txt = "intr";
  2658. if (epn->ep.caps.type_control || epn->ep.caps.type_iso ||
  2659. epn->ep.caps.type_bulk || !epn->ep.caps.type_int) {
  2660. dev_err(epn->udc->dev,
  2661. "ep%u caps mismatch, int expected\n", epn->id);
  2662. ret = -EINVAL;
  2663. }
  2664. break;
  2665. case USBF_EPN_MODE_ISO:
  2666. type_txt = "iso";
  2667. if (epn->ep.caps.type_control || !epn->ep.caps.type_iso ||
  2668. epn->ep.caps.type_bulk || epn->ep.caps.type_int) {
  2669. dev_err(epn->udc->dev,
  2670. "ep%u caps mismatch, iso expected\n", epn->id);
  2671. ret = -EINVAL;
  2672. }
  2673. break;
  2674. default:
  2675. type_txt = "unknown";
  2676. dev_err(epn->udc->dev, "ep%u unknown type\n", epn->id);
  2677. ret = -EINVAL;
  2678. break;
  2679. }
  2680. if (ctrl & USBF_EPN_BUF_TYPE_DOUBLE) {
  2681. buf_txt = "double";
  2682. if (!usbf_ep_info[epn->id].is_double) {
  2683. dev_err(epn->udc->dev,
  2684. "ep%u buffer mismatch, double expected\n",
  2685. epn->id);
  2686. ret = -EINVAL;
  2687. }
  2688. } else {
  2689. buf_txt = "single";
  2690. if (usbf_ep_info[epn->id].is_double) {
  2691. dev_err(epn->udc->dev,
  2692. "ep%u buffer mismatch, single expected\n",
  2693. epn->id);
  2694. ret = -EINVAL;
  2695. }
  2696. }
  2697. dev_dbg(epn->udc->dev, "ep%u (%s) %s, %s buffer %u, checked %s\n",
  2698. epn->id, epn->ep.name, type_txt, buf_txt,
  2699. epn->ep.maxpacket_limit, ret ? "failed" : "ok");
  2700. return ret;
  2701. }
  2702. static int usbf_probe(struct platform_device *pdev)
  2703. {
  2704. struct device *dev = &pdev->dev;
  2705. struct usbf_udc *udc;
  2706. struct usbf_ep *ep;
  2707. unsigned int i;
  2708. int irq;
  2709. int ret;
  2710. udc = devm_kzalloc(dev, sizeof(*udc), GFP_KERNEL);
  2711. if (!udc)
  2712. return -ENOMEM;
  2713. platform_set_drvdata(pdev, udc);
  2714. udc->dev = dev;
  2715. spin_lock_init(&udc->lock);
  2716. udc->regs = devm_platform_ioremap_resource(pdev, 0);
  2717. if (IS_ERR(udc->regs))
  2718. return PTR_ERR(udc->regs);
  2719. ret = devm_pm_runtime_enable(&pdev->dev);
  2720. if (ret)
  2721. return ret;
  2722. ret = pm_runtime_resume_and_get(&pdev->dev);
  2723. if (ret < 0)
  2724. return ret;
  2725. dev_info(dev, "USBF version: %08x\n",
  2726. usbf_reg_readl(udc, USBF_REG_USBSSVER));
  2727. /* Resetting the PLL is handled via the clock driver as it has common
  2728. * registers with USB Host
  2729. */
  2730. usbf_reg_bitclr(udc, USBF_REG_EPCTR, USBF_SYS_EPC_RST);
  2731. /* modify in register gadget process */
  2732. udc->gadget.speed = USB_SPEED_FULL;
  2733. udc->gadget.max_speed = USB_SPEED_HIGH;
  2734. udc->gadget.ops = &usbf_gadget_ops;
  2735. udc->gadget.name = dev->driver->name;
  2736. udc->gadget.dev.parent = dev;
  2737. udc->gadget.ep0 = &udc->ep[0].ep;
  2738. /* The hardware DMA controller needs dma addresses aligned on 32bit.
  2739. * A fallback to pio is done if DMA addresses are not aligned.
  2740. */
  2741. udc->gadget.quirk_avoids_skb_reserve = 1;
  2742. INIT_LIST_HEAD(&udc->gadget.ep_list);
  2743. /* we have a canned request structure to allow sending packets as reply
  2744. * to get_status requests
  2745. */
  2746. INIT_LIST_HEAD(&udc->setup_reply.queue);
  2747. for (i = 0; i < ARRAY_SIZE(udc->ep); i++) {
  2748. ep = &udc->ep[i];
  2749. if (!(usbf_reg_readl(udc, USBF_REG_USBSSCONF) &
  2750. USBF_SYS_EP_AVAILABLE(i))) {
  2751. continue;
  2752. }
  2753. INIT_LIST_HEAD(&ep->queue);
  2754. ep->id = i;
  2755. ep->disabled = 1;
  2756. ep->udc = udc;
  2757. ep->ep.ops = &usbf_ep_ops;
  2758. ep->ep.name = usbf_ep_info[i].name;
  2759. ep->ep.caps = usbf_ep_info[i].caps;
  2760. usb_ep_set_maxpacket_limit(&ep->ep,
  2761. usbf_ep_info[i].maxpacket_limit);
  2762. if (ep->id == 0) {
  2763. ep->regs = ep->udc->regs + USBF_BASE_EP0;
  2764. } else {
  2765. ep->regs = ep->udc->regs + USBF_BASE_EPN(ep->id - 1);
  2766. ret = usbf_epn_check(ep);
  2767. if (ret)
  2768. return ret;
  2769. if (usbf_reg_readl(udc, USBF_REG_USBSSCONF) &
  2770. USBF_SYS_DMA_AVAILABLE(i)) {
  2771. ep->dma_regs = ep->udc->regs +
  2772. USBF_BASE_DMA_EPN(ep->id - 1);
  2773. }
  2774. list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
  2775. }
  2776. }
  2777. irq = platform_get_irq(pdev, 0);
  2778. if (irq < 0)
  2779. return irq;
  2780. ret = devm_request_irq(dev, irq, usbf_epc_irq, 0, "usbf-epc", udc);
  2781. if (ret) {
  2782. dev_err(dev, "cannot request irq %d err %d\n", irq, ret);
  2783. return ret;
  2784. }
  2785. irq = platform_get_irq(pdev, 1);
  2786. if (irq < 0)
  2787. return irq;
  2788. ret = devm_request_irq(dev, irq, usbf_ahb_epc_irq, 0, "usbf-ahb-epc", udc);
  2789. if (ret) {
  2790. dev_err(dev, "cannot request irq %d err %d\n", irq, ret);
  2791. return ret;
  2792. }
  2793. usbf_reg_bitset(udc, USBF_REG_AHBMCTR, USBF_SYS_WBURST_TYPE);
  2794. usbf_reg_bitset(udc, USBF_REG_USB_CONTROL,
  2795. USBF_USB_INT_SEL | USBF_USB_SOF_RCV | USBF_USB_SOF_CLK_MODE);
  2796. ret = usb_add_gadget_udc(dev, &udc->gadget);
  2797. if (ret)
  2798. return ret;
  2799. return 0;
  2800. }
  2801. static void usbf_remove(struct platform_device *pdev)
  2802. {
  2803. struct usbf_udc *udc = platform_get_drvdata(pdev);
  2804. usb_del_gadget_udc(&udc->gadget);
  2805. pm_runtime_put(&pdev->dev);
  2806. }
  2807. static const struct of_device_id usbf_match[] = {
  2808. { .compatible = "renesas,rzn1-usbf" },
  2809. {} /* sentinel */
  2810. };
  2811. MODULE_DEVICE_TABLE(of, usbf_match);
  2812. static struct platform_driver udc_driver = {
  2813. .driver = {
  2814. .name = "usbf_renesas",
  2815. .of_match_table = usbf_match,
  2816. },
  2817. .probe = usbf_probe,
  2818. .remove = usbf_remove,
  2819. };
  2820. module_platform_driver(udc_driver);
  2821. MODULE_AUTHOR("Herve Codina <herve.codina@bootlin.com>");
  2822. MODULE_DESCRIPTION("Renesas R-Car Gen3 & RZ/N1 USB Function driver");
  2823. MODULE_LICENSE("GPL");