pch_udc.c 89 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273
  1. /*
  2. * Copyright (C) 2011 LAPIS Semiconductor Co., Ltd.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; version 2 of the License.
  7. */
  8. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/pci.h>
  12. #include <linux/delay.h>
  13. #include <linux/errno.h>
  14. #include <linux/list.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/usb/ch9.h>
  17. #include <linux/usb/gadget.h>
  18. #include <linux/gpio.h>
  19. #include <linux/irq.h>
  20. /* GPIO port for VBUS detecting */
  21. static int vbus_gpio_port = -1; /* GPIO port number (-1:Not used) */
  22. #define PCH_VBUS_PERIOD 3000 /* VBUS polling period (msec) */
  23. #define PCH_VBUS_INTERVAL 10 /* VBUS polling interval (msec) */
  24. /* Address offset of Registers */
  25. #define UDC_EP_REG_SHIFT 0x20 /* Offset to next EP */
  26. #define UDC_EPCTL_ADDR 0x00 /* Endpoint control */
  27. #define UDC_EPSTS_ADDR 0x04 /* Endpoint status */
  28. #define UDC_BUFIN_FRAMENUM_ADDR 0x08 /* buffer size in / frame number out */
  29. #define UDC_BUFOUT_MAXPKT_ADDR 0x0C /* buffer size out / maxpkt in */
  30. #define UDC_SUBPTR_ADDR 0x10 /* setup buffer pointer */
  31. #define UDC_DESPTR_ADDR 0x14 /* Data descriptor pointer */
  32. #define UDC_CONFIRM_ADDR 0x18 /* Write/Read confirmation */
  33. #define UDC_DEVCFG_ADDR 0x400 /* Device configuration */
  34. #define UDC_DEVCTL_ADDR 0x404 /* Device control */
  35. #define UDC_DEVSTS_ADDR 0x408 /* Device status */
  36. #define UDC_DEVIRQSTS_ADDR 0x40C /* Device irq status */
  37. #define UDC_DEVIRQMSK_ADDR 0x410 /* Device irq mask */
  38. #define UDC_EPIRQSTS_ADDR 0x414 /* Endpoint irq status */
  39. #define UDC_EPIRQMSK_ADDR 0x418 /* Endpoint irq mask */
  40. #define UDC_DEVLPM_ADDR 0x41C /* LPM control / status */
  41. #define UDC_CSR_BUSY_ADDR 0x4f0 /* UDC_CSR_BUSY Status register */
  42. #define UDC_SRST_ADDR 0x4fc /* SOFT RESET register */
  43. #define UDC_CSR_ADDR 0x500 /* USB_DEVICE endpoint register */
  44. /* Endpoint control register */
  45. /* Bit position */
  46. #define UDC_EPCTL_MRXFLUSH (1 << 12)
  47. #define UDC_EPCTL_RRDY (1 << 9)
  48. #define UDC_EPCTL_CNAK (1 << 8)
  49. #define UDC_EPCTL_SNAK (1 << 7)
  50. #define UDC_EPCTL_NAK (1 << 6)
  51. #define UDC_EPCTL_P (1 << 3)
  52. #define UDC_EPCTL_F (1 << 1)
  53. #define UDC_EPCTL_S (1 << 0)
  54. #define UDC_EPCTL_ET_SHIFT 4
  55. /* Mask patern */
  56. #define UDC_EPCTL_ET_MASK 0x00000030
  57. /* Value for ET field */
  58. #define UDC_EPCTL_ET_CONTROL 0
  59. #define UDC_EPCTL_ET_ISO 1
  60. #define UDC_EPCTL_ET_BULK 2
  61. #define UDC_EPCTL_ET_INTERRUPT 3
  62. /* Endpoint status register */
  63. /* Bit position */
  64. #define UDC_EPSTS_XFERDONE (1 << 27)
  65. #define UDC_EPSTS_RSS (1 << 26)
  66. #define UDC_EPSTS_RCS (1 << 25)
  67. #define UDC_EPSTS_TXEMPTY (1 << 24)
  68. #define UDC_EPSTS_TDC (1 << 10)
  69. #define UDC_EPSTS_HE (1 << 9)
  70. #define UDC_EPSTS_MRXFIFO_EMP (1 << 8)
  71. #define UDC_EPSTS_BNA (1 << 7)
  72. #define UDC_EPSTS_IN (1 << 6)
  73. #define UDC_EPSTS_OUT_SHIFT 4
  74. /* Mask patern */
  75. #define UDC_EPSTS_OUT_MASK 0x00000030
  76. #define UDC_EPSTS_ALL_CLR_MASK 0x1F0006F0
  77. /* Value for OUT field */
  78. #define UDC_EPSTS_OUT_SETUP 2
  79. #define UDC_EPSTS_OUT_DATA 1
  80. /* Device configuration register */
  81. /* Bit position */
  82. #define UDC_DEVCFG_CSR_PRG (1 << 17)
  83. #define UDC_DEVCFG_SP (1 << 3)
  84. /* SPD Valee */
  85. #define UDC_DEVCFG_SPD_HS 0x0
  86. #define UDC_DEVCFG_SPD_FS 0x1
  87. #define UDC_DEVCFG_SPD_LS 0x2
  88. /* Device control register */
  89. /* Bit position */
  90. #define UDC_DEVCTL_THLEN_SHIFT 24
  91. #define UDC_DEVCTL_BRLEN_SHIFT 16
  92. #define UDC_DEVCTL_CSR_DONE (1 << 13)
  93. #define UDC_DEVCTL_SD (1 << 10)
  94. #define UDC_DEVCTL_MODE (1 << 9)
  95. #define UDC_DEVCTL_BREN (1 << 8)
  96. #define UDC_DEVCTL_THE (1 << 7)
  97. #define UDC_DEVCTL_DU (1 << 4)
  98. #define UDC_DEVCTL_TDE (1 << 3)
  99. #define UDC_DEVCTL_RDE (1 << 2)
  100. #define UDC_DEVCTL_RES (1 << 0)
  101. /* Device status register */
  102. /* Bit position */
  103. #define UDC_DEVSTS_TS_SHIFT 18
  104. #define UDC_DEVSTS_ENUM_SPEED_SHIFT 13
  105. #define UDC_DEVSTS_ALT_SHIFT 8
  106. #define UDC_DEVSTS_INTF_SHIFT 4
  107. #define UDC_DEVSTS_CFG_SHIFT 0
  108. /* Mask patern */
  109. #define UDC_DEVSTS_TS_MASK 0xfffc0000
  110. #define UDC_DEVSTS_ENUM_SPEED_MASK 0x00006000
  111. #define UDC_DEVSTS_ALT_MASK 0x00000f00
  112. #define UDC_DEVSTS_INTF_MASK 0x000000f0
  113. #define UDC_DEVSTS_CFG_MASK 0x0000000f
  114. /* value for maximum speed for SPEED field */
  115. #define UDC_DEVSTS_ENUM_SPEED_FULL 1
  116. #define UDC_DEVSTS_ENUM_SPEED_HIGH 0
  117. #define UDC_DEVSTS_ENUM_SPEED_LOW 2
  118. #define UDC_DEVSTS_ENUM_SPEED_FULLX 3
  119. /* Device irq register */
  120. /* Bit position */
  121. #define UDC_DEVINT_RWKP (1 << 7)
  122. #define UDC_DEVINT_ENUM (1 << 6)
  123. #define UDC_DEVINT_SOF (1 << 5)
  124. #define UDC_DEVINT_US (1 << 4)
  125. #define UDC_DEVINT_UR (1 << 3)
  126. #define UDC_DEVINT_ES (1 << 2)
  127. #define UDC_DEVINT_SI (1 << 1)
  128. #define UDC_DEVINT_SC (1 << 0)
  129. /* Mask patern */
  130. #define UDC_DEVINT_MSK 0x7f
  131. /* Endpoint irq register */
  132. /* Bit position */
  133. #define UDC_EPINT_IN_SHIFT 0
  134. #define UDC_EPINT_OUT_SHIFT 16
  135. #define UDC_EPINT_IN_EP0 (1 << 0)
  136. #define UDC_EPINT_OUT_EP0 (1 << 16)
  137. /* Mask patern */
  138. #define UDC_EPINT_MSK_DISABLE_ALL 0xffffffff
  139. /* UDC_CSR_BUSY Status register */
  140. /* Bit position */
  141. #define UDC_CSR_BUSY (1 << 0)
  142. /* SOFT RESET register */
  143. /* Bit position */
  144. #define UDC_PSRST (1 << 1)
  145. #define UDC_SRST (1 << 0)
  146. /* USB_DEVICE endpoint register */
  147. /* Bit position */
  148. #define UDC_CSR_NE_NUM_SHIFT 0
  149. #define UDC_CSR_NE_DIR_SHIFT 4
  150. #define UDC_CSR_NE_TYPE_SHIFT 5
  151. #define UDC_CSR_NE_CFG_SHIFT 7
  152. #define UDC_CSR_NE_INTF_SHIFT 11
  153. #define UDC_CSR_NE_ALT_SHIFT 15
  154. #define UDC_CSR_NE_MAX_PKT_SHIFT 19
  155. /* Mask patern */
  156. #define UDC_CSR_NE_NUM_MASK 0x0000000f
  157. #define UDC_CSR_NE_DIR_MASK 0x00000010
  158. #define UDC_CSR_NE_TYPE_MASK 0x00000060
  159. #define UDC_CSR_NE_CFG_MASK 0x00000780
  160. #define UDC_CSR_NE_INTF_MASK 0x00007800
  161. #define UDC_CSR_NE_ALT_MASK 0x00078000
  162. #define UDC_CSR_NE_MAX_PKT_MASK 0x3ff80000
  163. #define PCH_UDC_CSR(ep) (UDC_CSR_ADDR + ep*4)
  164. #define PCH_UDC_EPINT(in, num)\
  165. (1 << (num + (in ? UDC_EPINT_IN_SHIFT : UDC_EPINT_OUT_SHIFT)))
  166. /* Index of endpoint */
  167. #define UDC_EP0IN_IDX 0
  168. #define UDC_EP0OUT_IDX 1
  169. #define UDC_EPIN_IDX(ep) (ep * 2)
  170. #define UDC_EPOUT_IDX(ep) (ep * 2 + 1)
  171. #define PCH_UDC_EP0 0
  172. #define PCH_UDC_EP1 1
  173. #define PCH_UDC_EP2 2
  174. #define PCH_UDC_EP3 3
  175. /* Number of endpoint */
  176. #define PCH_UDC_EP_NUM 32 /* Total number of EPs (16 IN,16 OUT) */
  177. #define PCH_UDC_USED_EP_NUM 4 /* EP number of EP's really used */
  178. /* Length Value */
  179. #define PCH_UDC_BRLEN 0x0F /* Burst length */
  180. #define PCH_UDC_THLEN 0x1F /* Threshold length */
  181. /* Value of EP Buffer Size */
  182. #define UDC_EP0IN_BUFF_SIZE 16
  183. #define UDC_EPIN_BUFF_SIZE 256
  184. #define UDC_EP0OUT_BUFF_SIZE 16
  185. #define UDC_EPOUT_BUFF_SIZE 256
  186. /* Value of EP maximum packet size */
  187. #define UDC_EP0IN_MAX_PKT_SIZE 64
  188. #define UDC_EP0OUT_MAX_PKT_SIZE 64
  189. #define UDC_BULK_MAX_PKT_SIZE 512
  190. /* DMA */
  191. #define DMA_DIR_RX 1 /* DMA for data receive */
  192. #define DMA_DIR_TX 2 /* DMA for data transmit */
  193. #define DMA_ADDR_INVALID (~(dma_addr_t)0)
  194. #define UDC_DMA_MAXPACKET 65536 /* maximum packet size for DMA */
  195. /**
  196. * struct pch_udc_data_dma_desc - Structure to hold DMA descriptor information
  197. * for data
  198. * @status: Status quadlet
  199. * @reserved: Reserved
  200. * @dataptr: Buffer descriptor
  201. * @next: Next descriptor
  202. */
  203. struct pch_udc_data_dma_desc {
  204. u32 status;
  205. u32 reserved;
  206. u32 dataptr;
  207. u32 next;
  208. };
  209. /**
  210. * struct pch_udc_stp_dma_desc - Structure to hold DMA descriptor information
  211. * for control data
  212. * @status: Status
  213. * @reserved: Reserved
  214. * @data12: First setup word
  215. * @data34: Second setup word
  216. */
  217. struct pch_udc_stp_dma_desc {
  218. u32 status;
  219. u32 reserved;
  220. struct usb_ctrlrequest request;
  221. } __attribute((packed));
  222. /* DMA status definitions */
  223. /* Buffer status */
  224. #define PCH_UDC_BUFF_STS 0xC0000000
  225. #define PCH_UDC_BS_HST_RDY 0x00000000
  226. #define PCH_UDC_BS_DMA_BSY 0x40000000
  227. #define PCH_UDC_BS_DMA_DONE 0x80000000
  228. #define PCH_UDC_BS_HST_BSY 0xC0000000
  229. /* Rx/Tx Status */
  230. #define PCH_UDC_RXTX_STS 0x30000000
  231. #define PCH_UDC_RTS_SUCC 0x00000000
  232. #define PCH_UDC_RTS_DESERR 0x10000000
  233. #define PCH_UDC_RTS_BUFERR 0x30000000
  234. /* Last Descriptor Indication */
  235. #define PCH_UDC_DMA_LAST 0x08000000
  236. /* Number of Rx/Tx Bytes Mask */
  237. #define PCH_UDC_RXTX_BYTES 0x0000ffff
  238. /**
  239. * struct pch_udc_cfg_data - Structure to hold current configuration
  240. * and interface information
  241. * @cur_cfg: current configuration in use
  242. * @cur_intf: current interface in use
  243. * @cur_alt: current alt interface in use
  244. */
  245. struct pch_udc_cfg_data {
  246. u16 cur_cfg;
  247. u16 cur_intf;
  248. u16 cur_alt;
  249. };
  250. /**
  251. * struct pch_udc_ep - Structure holding a PCH USB device Endpoint information
  252. * @ep: embedded ep request
  253. * @td_stp_phys: for setup request
  254. * @td_data_phys: for data request
  255. * @td_stp: for setup request
  256. * @td_data: for data request
  257. * @dev: reference to device struct
  258. * @offset_addr: offset address of ep register
  259. * @desc: for this ep
  260. * @queue: queue for requests
  261. * @num: endpoint number
  262. * @in: endpoint is IN
  263. * @halted: endpoint halted?
  264. * @epsts: Endpoint status
  265. */
  266. struct pch_udc_ep {
  267. struct usb_ep ep;
  268. dma_addr_t td_stp_phys;
  269. dma_addr_t td_data_phys;
  270. struct pch_udc_stp_dma_desc *td_stp;
  271. struct pch_udc_data_dma_desc *td_data;
  272. struct pch_udc_dev *dev;
  273. unsigned long offset_addr;
  274. struct list_head queue;
  275. unsigned num:5,
  276. in:1,
  277. halted:1;
  278. unsigned long epsts;
  279. };
  280. /**
  281. * struct pch_vbus_gpio_data - Structure holding GPIO informaton
  282. * for detecting VBUS
  283. * @port: gpio port number
  284. * @intr: gpio interrupt number
  285. * @irq_work_fall Structure for WorkQueue
  286. * @irq_work_rise Structure for WorkQueue
  287. */
  288. struct pch_vbus_gpio_data {
  289. int port;
  290. int intr;
  291. struct work_struct irq_work_fall;
  292. struct work_struct irq_work_rise;
  293. };
  294. /**
  295. * struct pch_udc_dev - Structure holding complete information
  296. * of the PCH USB device
  297. * @gadget: gadget driver data
  298. * @driver: reference to gadget driver bound
  299. * @pdev: reference to the PCI device
  300. * @ep: array of endpoints
  301. * @lock: protects all state
  302. * @active: enabled the PCI device
  303. * @stall: stall requested
  304. * @prot_stall: protcol stall requested
  305. * @irq_registered: irq registered with system
  306. * @mem_region: device memory mapped
  307. * @registered: driver registered with system
  308. * @suspended: driver in suspended state
  309. * @connected: gadget driver associated
  310. * @vbus_session: required vbus_session state
  311. * @set_cfg_not_acked: pending acknowledgement 4 setup
  312. * @waiting_zlp_ack: pending acknowledgement 4 ZLP
  313. * @data_requests: DMA pool for data requests
  314. * @stp_requests: DMA pool for setup requests
  315. * @dma_addr: DMA pool for received
  316. * @ep0out_buf: Buffer for DMA
  317. * @setup_data: Received setup data
  318. * @phys_addr: of device memory
  319. * @base_addr: for mapped device memory
  320. * @bar: Indicates which PCI BAR for USB regs
  321. * @irq: IRQ line for the device
  322. * @cfg_data: current cfg, intf, and alt in use
  323. * @vbus_gpio: GPIO informaton for detecting VBUS
  324. */
  325. struct pch_udc_dev {
  326. struct usb_gadget gadget;
  327. struct usb_gadget_driver *driver;
  328. struct pci_dev *pdev;
  329. struct pch_udc_ep ep[PCH_UDC_EP_NUM];
  330. spinlock_t lock; /* protects all state */
  331. unsigned active:1,
  332. stall:1,
  333. prot_stall:1,
  334. irq_registered:1,
  335. mem_region:1,
  336. suspended:1,
  337. connected:1,
  338. vbus_session:1,
  339. set_cfg_not_acked:1,
  340. waiting_zlp_ack:1;
  341. struct pci_pool *data_requests;
  342. struct pci_pool *stp_requests;
  343. dma_addr_t dma_addr;
  344. void *ep0out_buf;
  345. struct usb_ctrlrequest setup_data;
  346. unsigned long phys_addr;
  347. void __iomem *base_addr;
  348. unsigned bar;
  349. unsigned irq;
  350. struct pch_udc_cfg_data cfg_data;
  351. struct pch_vbus_gpio_data vbus_gpio;
  352. };
  353. #define to_pch_udc(g) (container_of((g), struct pch_udc_dev, gadget))
  354. #define PCH_UDC_PCI_BAR_QUARK_X1000 0
  355. #define PCH_UDC_PCI_BAR 1
  356. #define PCI_DEVICE_ID_INTEL_EG20T_UDC 0x8808
  357. #define PCI_DEVICE_ID_INTEL_QUARK_X1000_UDC 0x0939
  358. #define PCI_VENDOR_ID_ROHM 0x10DB
  359. #define PCI_DEVICE_ID_ML7213_IOH_UDC 0x801D
  360. #define PCI_DEVICE_ID_ML7831_IOH_UDC 0x8808
  361. static const char ep0_string[] = "ep0in";
  362. static DEFINE_SPINLOCK(udc_stall_spinlock); /* stall spin lock */
  363. static bool speed_fs;
  364. module_param_named(speed_fs, speed_fs, bool, S_IRUGO);
  365. MODULE_PARM_DESC(speed_fs, "true for Full speed operation");
  366. /**
  367. * struct pch_udc_request - Structure holding a PCH USB device request packet
  368. * @req: embedded ep request
  369. * @td_data_phys: phys. address
  370. * @td_data: first dma desc. of chain
  371. * @td_data_last: last dma desc. of chain
  372. * @queue: associated queue
  373. * @dma_going: DMA in progress for request
  374. * @dma_mapped: DMA memory mapped for request
  375. * @dma_done: DMA completed for request
  376. * @chain_len: chain length
  377. * @buf: Buffer memory for align adjustment
  378. * @dma: DMA memory for align adjustment
  379. */
  380. struct pch_udc_request {
  381. struct usb_request req;
  382. dma_addr_t td_data_phys;
  383. struct pch_udc_data_dma_desc *td_data;
  384. struct pch_udc_data_dma_desc *td_data_last;
  385. struct list_head queue;
  386. unsigned dma_going:1,
  387. dma_mapped:1,
  388. dma_done:1;
  389. unsigned chain_len;
  390. void *buf;
  391. dma_addr_t dma;
  392. };
  393. static inline u32 pch_udc_readl(struct pch_udc_dev *dev, unsigned long reg)
  394. {
  395. return ioread32(dev->base_addr + reg);
  396. }
  397. static inline void pch_udc_writel(struct pch_udc_dev *dev,
  398. unsigned long val, unsigned long reg)
  399. {
  400. iowrite32(val, dev->base_addr + reg);
  401. }
  402. static inline void pch_udc_bit_set(struct pch_udc_dev *dev,
  403. unsigned long reg,
  404. unsigned long bitmask)
  405. {
  406. pch_udc_writel(dev, pch_udc_readl(dev, reg) | bitmask, reg);
  407. }
  408. static inline void pch_udc_bit_clr(struct pch_udc_dev *dev,
  409. unsigned long reg,
  410. unsigned long bitmask)
  411. {
  412. pch_udc_writel(dev, pch_udc_readl(dev, reg) & ~(bitmask), reg);
  413. }
  414. static inline u32 pch_udc_ep_readl(struct pch_udc_ep *ep, unsigned long reg)
  415. {
  416. return ioread32(ep->dev->base_addr + ep->offset_addr + reg);
  417. }
  418. static inline void pch_udc_ep_writel(struct pch_udc_ep *ep,
  419. unsigned long val, unsigned long reg)
  420. {
  421. iowrite32(val, ep->dev->base_addr + ep->offset_addr + reg);
  422. }
  423. static inline void pch_udc_ep_bit_set(struct pch_udc_ep *ep,
  424. unsigned long reg,
  425. unsigned long bitmask)
  426. {
  427. pch_udc_ep_writel(ep, pch_udc_ep_readl(ep, reg) | bitmask, reg);
  428. }
  429. static inline void pch_udc_ep_bit_clr(struct pch_udc_ep *ep,
  430. unsigned long reg,
  431. unsigned long bitmask)
  432. {
  433. pch_udc_ep_writel(ep, pch_udc_ep_readl(ep, reg) & ~(bitmask), reg);
  434. }
  435. /**
  436. * pch_udc_csr_busy() - Wait till idle.
  437. * @dev: Reference to pch_udc_dev structure
  438. */
  439. static void pch_udc_csr_busy(struct pch_udc_dev *dev)
  440. {
  441. unsigned int count = 200;
  442. /* Wait till idle */
  443. while ((pch_udc_readl(dev, UDC_CSR_BUSY_ADDR) & UDC_CSR_BUSY)
  444. && --count)
  445. cpu_relax();
  446. if (!count)
  447. dev_err(&dev->pdev->dev, "%s: wait error\n", __func__);
  448. }
  449. /**
  450. * pch_udc_write_csr() - Write the command and status registers.
  451. * @dev: Reference to pch_udc_dev structure
  452. * @val: value to be written to CSR register
  453. * @addr: address of CSR register
  454. */
  455. static void pch_udc_write_csr(struct pch_udc_dev *dev, unsigned long val,
  456. unsigned int ep)
  457. {
  458. unsigned long reg = PCH_UDC_CSR(ep);
  459. pch_udc_csr_busy(dev); /* Wait till idle */
  460. pch_udc_writel(dev, val, reg);
  461. pch_udc_csr_busy(dev); /* Wait till idle */
  462. }
  463. /**
  464. * pch_udc_read_csr() - Read the command and status registers.
  465. * @dev: Reference to pch_udc_dev structure
  466. * @addr: address of CSR register
  467. *
  468. * Return codes: content of CSR register
  469. */
  470. static u32 pch_udc_read_csr(struct pch_udc_dev *dev, unsigned int ep)
  471. {
  472. unsigned long reg = PCH_UDC_CSR(ep);
  473. pch_udc_csr_busy(dev); /* Wait till idle */
  474. pch_udc_readl(dev, reg); /* Dummy read */
  475. pch_udc_csr_busy(dev); /* Wait till idle */
  476. return pch_udc_readl(dev, reg);
  477. }
  478. /**
  479. * pch_udc_rmt_wakeup() - Initiate for remote wakeup
  480. * @dev: Reference to pch_udc_dev structure
  481. */
  482. static inline void pch_udc_rmt_wakeup(struct pch_udc_dev *dev)
  483. {
  484. pch_udc_bit_set(dev, UDC_DEVCTL_ADDR, UDC_DEVCTL_RES);
  485. mdelay(1);
  486. pch_udc_bit_clr(dev, UDC_DEVCTL_ADDR, UDC_DEVCTL_RES);
  487. }
  488. /**
  489. * pch_udc_get_frame() - Get the current frame from device status register
  490. * @dev: Reference to pch_udc_dev structure
  491. * Retern current frame
  492. */
  493. static inline int pch_udc_get_frame(struct pch_udc_dev *dev)
  494. {
  495. u32 frame = pch_udc_readl(dev, UDC_DEVSTS_ADDR);
  496. return (frame & UDC_DEVSTS_TS_MASK) >> UDC_DEVSTS_TS_SHIFT;
  497. }
  498. /**
  499. * pch_udc_clear_selfpowered() - Clear the self power control
  500. * @dev: Reference to pch_udc_regs structure
  501. */
  502. static inline void pch_udc_clear_selfpowered(struct pch_udc_dev *dev)
  503. {
  504. pch_udc_bit_clr(dev, UDC_DEVCFG_ADDR, UDC_DEVCFG_SP);
  505. }
  506. /**
  507. * pch_udc_set_selfpowered() - Set the self power control
  508. * @dev: Reference to pch_udc_regs structure
  509. */
  510. static inline void pch_udc_set_selfpowered(struct pch_udc_dev *dev)
  511. {
  512. pch_udc_bit_set(dev, UDC_DEVCFG_ADDR, UDC_DEVCFG_SP);
  513. }
  514. /**
  515. * pch_udc_set_disconnect() - Set the disconnect status.
  516. * @dev: Reference to pch_udc_regs structure
  517. */
  518. static inline void pch_udc_set_disconnect(struct pch_udc_dev *dev)
  519. {
  520. pch_udc_bit_set(dev, UDC_DEVCTL_ADDR, UDC_DEVCTL_SD);
  521. }
  522. /**
  523. * pch_udc_clear_disconnect() - Clear the disconnect status.
  524. * @dev: Reference to pch_udc_regs structure
  525. */
  526. static void pch_udc_clear_disconnect(struct pch_udc_dev *dev)
  527. {
  528. /* Clear the disconnect */
  529. pch_udc_bit_set(dev, UDC_DEVCTL_ADDR, UDC_DEVCTL_RES);
  530. pch_udc_bit_clr(dev, UDC_DEVCTL_ADDR, UDC_DEVCTL_SD);
  531. mdelay(1);
  532. /* Resume USB signalling */
  533. pch_udc_bit_clr(dev, UDC_DEVCTL_ADDR, UDC_DEVCTL_RES);
  534. }
  535. /**
  536. * pch_udc_reconnect() - This API initializes usb device controller,
  537. * and clear the disconnect status.
  538. * @dev: Reference to pch_udc_regs structure
  539. */
  540. static void pch_udc_init(struct pch_udc_dev *dev);
  541. static void pch_udc_reconnect(struct pch_udc_dev *dev)
  542. {
  543. pch_udc_init(dev);
  544. /* enable device interrupts */
  545. /* pch_udc_enable_interrupts() */
  546. pch_udc_bit_clr(dev, UDC_DEVIRQMSK_ADDR,
  547. UDC_DEVINT_UR | UDC_DEVINT_ENUM);
  548. /* Clear the disconnect */
  549. pch_udc_bit_set(dev, UDC_DEVCTL_ADDR, UDC_DEVCTL_RES);
  550. pch_udc_bit_clr(dev, UDC_DEVCTL_ADDR, UDC_DEVCTL_SD);
  551. mdelay(1);
  552. /* Resume USB signalling */
  553. pch_udc_bit_clr(dev, UDC_DEVCTL_ADDR, UDC_DEVCTL_RES);
  554. }
  555. /**
  556. * pch_udc_vbus_session() - set or clearr the disconnect status.
  557. * @dev: Reference to pch_udc_regs structure
  558. * @is_active: Parameter specifying the action
  559. * 0: indicating VBUS power is ending
  560. * !0: indicating VBUS power is starting
  561. */
  562. static inline void pch_udc_vbus_session(struct pch_udc_dev *dev,
  563. int is_active)
  564. {
  565. if (is_active) {
  566. pch_udc_reconnect(dev);
  567. dev->vbus_session = 1;
  568. } else {
  569. if (dev->driver && dev->driver->disconnect) {
  570. spin_lock(&dev->lock);
  571. dev->driver->disconnect(&dev->gadget);
  572. spin_unlock(&dev->lock);
  573. }
  574. pch_udc_set_disconnect(dev);
  575. dev->vbus_session = 0;
  576. }
  577. }
  578. /**
  579. * pch_udc_ep_set_stall() - Set the stall of endpoint
  580. * @ep: Reference to structure of type pch_udc_ep_regs
  581. */
  582. static void pch_udc_ep_set_stall(struct pch_udc_ep *ep)
  583. {
  584. if (ep->in) {
  585. pch_udc_ep_bit_set(ep, UDC_EPCTL_ADDR, UDC_EPCTL_F);
  586. pch_udc_ep_bit_set(ep, UDC_EPCTL_ADDR, UDC_EPCTL_S);
  587. } else {
  588. pch_udc_ep_bit_set(ep, UDC_EPCTL_ADDR, UDC_EPCTL_S);
  589. }
  590. }
  591. /**
  592. * pch_udc_ep_clear_stall() - Clear the stall of endpoint
  593. * @ep: Reference to structure of type pch_udc_ep_regs
  594. */
  595. static inline void pch_udc_ep_clear_stall(struct pch_udc_ep *ep)
  596. {
  597. /* Clear the stall */
  598. pch_udc_ep_bit_clr(ep, UDC_EPCTL_ADDR, UDC_EPCTL_S);
  599. /* Clear NAK by writing CNAK */
  600. pch_udc_ep_bit_set(ep, UDC_EPCTL_ADDR, UDC_EPCTL_CNAK);
  601. }
  602. /**
  603. * pch_udc_ep_set_trfr_type() - Set the transfer type of endpoint
  604. * @ep: Reference to structure of type pch_udc_ep_regs
  605. * @type: Type of endpoint
  606. */
  607. static inline void pch_udc_ep_set_trfr_type(struct pch_udc_ep *ep,
  608. u8 type)
  609. {
  610. pch_udc_ep_writel(ep, ((type << UDC_EPCTL_ET_SHIFT) &
  611. UDC_EPCTL_ET_MASK), UDC_EPCTL_ADDR);
  612. }
  613. /**
  614. * pch_udc_ep_set_bufsz() - Set the maximum packet size for the endpoint
  615. * @ep: Reference to structure of type pch_udc_ep_regs
  616. * @buf_size: The buffer word size
  617. */
  618. static void pch_udc_ep_set_bufsz(struct pch_udc_ep *ep,
  619. u32 buf_size, u32 ep_in)
  620. {
  621. u32 data;
  622. if (ep_in) {
  623. data = pch_udc_ep_readl(ep, UDC_BUFIN_FRAMENUM_ADDR);
  624. data = (data & 0xffff0000) | (buf_size & 0xffff);
  625. pch_udc_ep_writel(ep, data, UDC_BUFIN_FRAMENUM_ADDR);
  626. } else {
  627. data = pch_udc_ep_readl(ep, UDC_BUFOUT_MAXPKT_ADDR);
  628. data = (buf_size << 16) | (data & 0xffff);
  629. pch_udc_ep_writel(ep, data, UDC_BUFOUT_MAXPKT_ADDR);
  630. }
  631. }
  632. /**
  633. * pch_udc_ep_set_maxpkt() - Set the Max packet size for the endpoint
  634. * @ep: Reference to structure of type pch_udc_ep_regs
  635. * @pkt_size: The packet byte size
  636. */
  637. static void pch_udc_ep_set_maxpkt(struct pch_udc_ep *ep, u32 pkt_size)
  638. {
  639. u32 data = pch_udc_ep_readl(ep, UDC_BUFOUT_MAXPKT_ADDR);
  640. data = (data & 0xffff0000) | (pkt_size & 0xffff);
  641. pch_udc_ep_writel(ep, data, UDC_BUFOUT_MAXPKT_ADDR);
  642. }
  643. /**
  644. * pch_udc_ep_set_subptr() - Set the Setup buffer pointer for the endpoint
  645. * @ep: Reference to structure of type pch_udc_ep_regs
  646. * @addr: Address of the register
  647. */
  648. static inline void pch_udc_ep_set_subptr(struct pch_udc_ep *ep, u32 addr)
  649. {
  650. pch_udc_ep_writel(ep, addr, UDC_SUBPTR_ADDR);
  651. }
  652. /**
  653. * pch_udc_ep_set_ddptr() - Set the Data descriptor pointer for the endpoint
  654. * @ep: Reference to structure of type pch_udc_ep_regs
  655. * @addr: Address of the register
  656. */
  657. static inline void pch_udc_ep_set_ddptr(struct pch_udc_ep *ep, u32 addr)
  658. {
  659. pch_udc_ep_writel(ep, addr, UDC_DESPTR_ADDR);
  660. }
  661. /**
  662. * pch_udc_ep_set_pd() - Set the poll demand bit for the endpoint
  663. * @ep: Reference to structure of type pch_udc_ep_regs
  664. */
  665. static inline void pch_udc_ep_set_pd(struct pch_udc_ep *ep)
  666. {
  667. pch_udc_ep_bit_set(ep, UDC_EPCTL_ADDR, UDC_EPCTL_P);
  668. }
  669. /**
  670. * pch_udc_ep_set_rrdy() - Set the receive ready bit for the endpoint
  671. * @ep: Reference to structure of type pch_udc_ep_regs
  672. */
  673. static inline void pch_udc_ep_set_rrdy(struct pch_udc_ep *ep)
  674. {
  675. pch_udc_ep_bit_set(ep, UDC_EPCTL_ADDR, UDC_EPCTL_RRDY);
  676. }
  677. /**
  678. * pch_udc_ep_clear_rrdy() - Clear the receive ready bit for the endpoint
  679. * @ep: Reference to structure of type pch_udc_ep_regs
  680. */
  681. static inline void pch_udc_ep_clear_rrdy(struct pch_udc_ep *ep)
  682. {
  683. pch_udc_ep_bit_clr(ep, UDC_EPCTL_ADDR, UDC_EPCTL_RRDY);
  684. }
  685. /**
  686. * pch_udc_set_dma() - Set the 'TDE' or RDE bit of device control
  687. * register depending on the direction specified
  688. * @dev: Reference to structure of type pch_udc_regs
  689. * @dir: whether Tx or Rx
  690. * DMA_DIR_RX: Receive
  691. * DMA_DIR_TX: Transmit
  692. */
  693. static inline void pch_udc_set_dma(struct pch_udc_dev *dev, int dir)
  694. {
  695. if (dir == DMA_DIR_RX)
  696. pch_udc_bit_set(dev, UDC_DEVCTL_ADDR, UDC_DEVCTL_RDE);
  697. else if (dir == DMA_DIR_TX)
  698. pch_udc_bit_set(dev, UDC_DEVCTL_ADDR, UDC_DEVCTL_TDE);
  699. }
  700. /**
  701. * pch_udc_clear_dma() - Clear the 'TDE' or RDE bit of device control
  702. * register depending on the direction specified
  703. * @dev: Reference to structure of type pch_udc_regs
  704. * @dir: Whether Tx or Rx
  705. * DMA_DIR_RX: Receive
  706. * DMA_DIR_TX: Transmit
  707. */
  708. static inline void pch_udc_clear_dma(struct pch_udc_dev *dev, int dir)
  709. {
  710. if (dir == DMA_DIR_RX)
  711. pch_udc_bit_clr(dev, UDC_DEVCTL_ADDR, UDC_DEVCTL_RDE);
  712. else if (dir == DMA_DIR_TX)
  713. pch_udc_bit_clr(dev, UDC_DEVCTL_ADDR, UDC_DEVCTL_TDE);
  714. }
  715. /**
  716. * pch_udc_set_csr_done() - Set the device control register
  717. * CSR done field (bit 13)
  718. * @dev: reference to structure of type pch_udc_regs
  719. */
  720. static inline void pch_udc_set_csr_done(struct pch_udc_dev *dev)
  721. {
  722. pch_udc_bit_set(dev, UDC_DEVCTL_ADDR, UDC_DEVCTL_CSR_DONE);
  723. }
  724. /**
  725. * pch_udc_disable_interrupts() - Disables the specified interrupts
  726. * @dev: Reference to structure of type pch_udc_regs
  727. * @mask: Mask to disable interrupts
  728. */
  729. static inline void pch_udc_disable_interrupts(struct pch_udc_dev *dev,
  730. u32 mask)
  731. {
  732. pch_udc_bit_set(dev, UDC_DEVIRQMSK_ADDR, mask);
  733. }
  734. /**
  735. * pch_udc_enable_interrupts() - Enable the specified interrupts
  736. * @dev: Reference to structure of type pch_udc_regs
  737. * @mask: Mask to enable interrupts
  738. */
  739. static inline void pch_udc_enable_interrupts(struct pch_udc_dev *dev,
  740. u32 mask)
  741. {
  742. pch_udc_bit_clr(dev, UDC_DEVIRQMSK_ADDR, mask);
  743. }
  744. /**
  745. * pch_udc_disable_ep_interrupts() - Disable endpoint interrupts
  746. * @dev: Reference to structure of type pch_udc_regs
  747. * @mask: Mask to disable interrupts
  748. */
  749. static inline void pch_udc_disable_ep_interrupts(struct pch_udc_dev *dev,
  750. u32 mask)
  751. {
  752. pch_udc_bit_set(dev, UDC_EPIRQMSK_ADDR, mask);
  753. }
  754. /**
  755. * pch_udc_enable_ep_interrupts() - Enable endpoint interrupts
  756. * @dev: Reference to structure of type pch_udc_regs
  757. * @mask: Mask to enable interrupts
  758. */
  759. static inline void pch_udc_enable_ep_interrupts(struct pch_udc_dev *dev,
  760. u32 mask)
  761. {
  762. pch_udc_bit_clr(dev, UDC_EPIRQMSK_ADDR, mask);
  763. }
  764. /**
  765. * pch_udc_read_device_interrupts() - Read the device interrupts
  766. * @dev: Reference to structure of type pch_udc_regs
  767. * Retern The device interrupts
  768. */
  769. static inline u32 pch_udc_read_device_interrupts(struct pch_udc_dev *dev)
  770. {
  771. return pch_udc_readl(dev, UDC_DEVIRQSTS_ADDR);
  772. }
  773. /**
  774. * pch_udc_write_device_interrupts() - Write device interrupts
  775. * @dev: Reference to structure of type pch_udc_regs
  776. * @val: The value to be written to interrupt register
  777. */
  778. static inline void pch_udc_write_device_interrupts(struct pch_udc_dev *dev,
  779. u32 val)
  780. {
  781. pch_udc_writel(dev, val, UDC_DEVIRQSTS_ADDR);
  782. }
  783. /**
  784. * pch_udc_read_ep_interrupts() - Read the endpoint interrupts
  785. * @dev: Reference to structure of type pch_udc_regs
  786. * Retern The endpoint interrupt
  787. */
  788. static inline u32 pch_udc_read_ep_interrupts(struct pch_udc_dev *dev)
  789. {
  790. return pch_udc_readl(dev, UDC_EPIRQSTS_ADDR);
  791. }
  792. /**
  793. * pch_udc_write_ep_interrupts() - Clear endpoint interupts
  794. * @dev: Reference to structure of type pch_udc_regs
  795. * @val: The value to be written to interrupt register
  796. */
  797. static inline void pch_udc_write_ep_interrupts(struct pch_udc_dev *dev,
  798. u32 val)
  799. {
  800. pch_udc_writel(dev, val, UDC_EPIRQSTS_ADDR);
  801. }
  802. /**
  803. * pch_udc_read_device_status() - Read the device status
  804. * @dev: Reference to structure of type pch_udc_regs
  805. * Retern The device status
  806. */
  807. static inline u32 pch_udc_read_device_status(struct pch_udc_dev *dev)
  808. {
  809. return pch_udc_readl(dev, UDC_DEVSTS_ADDR);
  810. }
  811. /**
  812. * pch_udc_read_ep_control() - Read the endpoint control
  813. * @ep: Reference to structure of type pch_udc_ep_regs
  814. * Retern The endpoint control register value
  815. */
  816. static inline u32 pch_udc_read_ep_control(struct pch_udc_ep *ep)
  817. {
  818. return pch_udc_ep_readl(ep, UDC_EPCTL_ADDR);
  819. }
  820. /**
  821. * pch_udc_clear_ep_control() - Clear the endpoint control register
  822. * @ep: Reference to structure of type pch_udc_ep_regs
  823. * Retern The endpoint control register value
  824. */
  825. static inline void pch_udc_clear_ep_control(struct pch_udc_ep *ep)
  826. {
  827. return pch_udc_ep_writel(ep, 0, UDC_EPCTL_ADDR);
  828. }
  829. /**
  830. * pch_udc_read_ep_status() - Read the endpoint status
  831. * @ep: Reference to structure of type pch_udc_ep_regs
  832. * Retern The endpoint status
  833. */
  834. static inline u32 pch_udc_read_ep_status(struct pch_udc_ep *ep)
  835. {
  836. return pch_udc_ep_readl(ep, UDC_EPSTS_ADDR);
  837. }
  838. /**
  839. * pch_udc_clear_ep_status() - Clear the endpoint status
  840. * @ep: Reference to structure of type pch_udc_ep_regs
  841. * @stat: Endpoint status
  842. */
  843. static inline void pch_udc_clear_ep_status(struct pch_udc_ep *ep,
  844. u32 stat)
  845. {
  846. return pch_udc_ep_writel(ep, stat, UDC_EPSTS_ADDR);
  847. }
  848. /**
  849. * pch_udc_ep_set_nak() - Set the bit 7 (SNAK field)
  850. * of the endpoint control register
  851. * @ep: Reference to structure of type pch_udc_ep_regs
  852. */
  853. static inline void pch_udc_ep_set_nak(struct pch_udc_ep *ep)
  854. {
  855. pch_udc_ep_bit_set(ep, UDC_EPCTL_ADDR, UDC_EPCTL_SNAK);
  856. }
  857. /**
  858. * pch_udc_ep_clear_nak() - Set the bit 8 (CNAK field)
  859. * of the endpoint control register
  860. * @ep: reference to structure of type pch_udc_ep_regs
  861. */
  862. static void pch_udc_ep_clear_nak(struct pch_udc_ep *ep)
  863. {
  864. unsigned int loopcnt = 0;
  865. struct pch_udc_dev *dev = ep->dev;
  866. if (!(pch_udc_ep_readl(ep, UDC_EPCTL_ADDR) & UDC_EPCTL_NAK))
  867. return;
  868. if (!ep->in) {
  869. loopcnt = 10000;
  870. while (!(pch_udc_read_ep_status(ep) & UDC_EPSTS_MRXFIFO_EMP) &&
  871. --loopcnt)
  872. udelay(5);
  873. if (!loopcnt)
  874. dev_err(&dev->pdev->dev, "%s: RxFIFO not Empty\n",
  875. __func__);
  876. }
  877. loopcnt = 10000;
  878. while ((pch_udc_read_ep_control(ep) & UDC_EPCTL_NAK) && --loopcnt) {
  879. pch_udc_ep_bit_set(ep, UDC_EPCTL_ADDR, UDC_EPCTL_CNAK);
  880. udelay(5);
  881. }
  882. if (!loopcnt)
  883. dev_err(&dev->pdev->dev, "%s: Clear NAK not set for ep%d%s\n",
  884. __func__, ep->num, (ep->in ? "in" : "out"));
  885. }
  886. /**
  887. * pch_udc_ep_fifo_flush() - Flush the endpoint fifo
  888. * @ep: reference to structure of type pch_udc_ep_regs
  889. * @dir: direction of endpoint
  890. * 0: endpoint is OUT
  891. * !0: endpoint is IN
  892. */
  893. static void pch_udc_ep_fifo_flush(struct pch_udc_ep *ep, int dir)
  894. {
  895. if (dir) { /* IN ep */
  896. pch_udc_ep_bit_set(ep, UDC_EPCTL_ADDR, UDC_EPCTL_F);
  897. return;
  898. }
  899. }
  900. /**
  901. * pch_udc_ep_enable() - This api enables endpoint
  902. * @regs: Reference to structure pch_udc_ep_regs
  903. * @desc: endpoint descriptor
  904. */
  905. static void pch_udc_ep_enable(struct pch_udc_ep *ep,
  906. struct pch_udc_cfg_data *cfg,
  907. const struct usb_endpoint_descriptor *desc)
  908. {
  909. u32 val = 0;
  910. u32 buff_size = 0;
  911. pch_udc_ep_set_trfr_type(ep, desc->bmAttributes);
  912. if (ep->in)
  913. buff_size = UDC_EPIN_BUFF_SIZE;
  914. else
  915. buff_size = UDC_EPOUT_BUFF_SIZE;
  916. pch_udc_ep_set_bufsz(ep, buff_size, ep->in);
  917. pch_udc_ep_set_maxpkt(ep, usb_endpoint_maxp(desc));
  918. pch_udc_ep_set_nak(ep);
  919. pch_udc_ep_fifo_flush(ep, ep->in);
  920. /* Configure the endpoint */
  921. val = ep->num << UDC_CSR_NE_NUM_SHIFT | ep->in << UDC_CSR_NE_DIR_SHIFT |
  922. ((desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) <<
  923. UDC_CSR_NE_TYPE_SHIFT) |
  924. (cfg->cur_cfg << UDC_CSR_NE_CFG_SHIFT) |
  925. (cfg->cur_intf << UDC_CSR_NE_INTF_SHIFT) |
  926. (cfg->cur_alt << UDC_CSR_NE_ALT_SHIFT) |
  927. usb_endpoint_maxp(desc) << UDC_CSR_NE_MAX_PKT_SHIFT;
  928. if (ep->in)
  929. pch_udc_write_csr(ep->dev, val, UDC_EPIN_IDX(ep->num));
  930. else
  931. pch_udc_write_csr(ep->dev, val, UDC_EPOUT_IDX(ep->num));
  932. }
  933. /**
  934. * pch_udc_ep_disable() - This api disables endpoint
  935. * @regs: Reference to structure pch_udc_ep_regs
  936. */
  937. static void pch_udc_ep_disable(struct pch_udc_ep *ep)
  938. {
  939. if (ep->in) {
  940. /* flush the fifo */
  941. pch_udc_ep_writel(ep, UDC_EPCTL_F, UDC_EPCTL_ADDR);
  942. /* set NAK */
  943. pch_udc_ep_writel(ep, UDC_EPCTL_SNAK, UDC_EPCTL_ADDR);
  944. pch_udc_ep_bit_set(ep, UDC_EPSTS_ADDR, UDC_EPSTS_IN);
  945. } else {
  946. /* set NAK */
  947. pch_udc_ep_writel(ep, UDC_EPCTL_SNAK, UDC_EPCTL_ADDR);
  948. }
  949. /* reset desc pointer */
  950. pch_udc_ep_writel(ep, 0, UDC_DESPTR_ADDR);
  951. }
  952. /**
  953. * pch_udc_wait_ep_stall() - Wait EP stall.
  954. * @dev: Reference to pch_udc_dev structure
  955. */
  956. static void pch_udc_wait_ep_stall(struct pch_udc_ep *ep)
  957. {
  958. unsigned int count = 10000;
  959. /* Wait till idle */
  960. while ((pch_udc_read_ep_control(ep) & UDC_EPCTL_S) && --count)
  961. udelay(5);
  962. if (!count)
  963. dev_err(&ep->dev->pdev->dev, "%s: wait error\n", __func__);
  964. }
  965. /**
  966. * pch_udc_init() - This API initializes usb device controller
  967. * @dev: Rreference to pch_udc_regs structure
  968. */
  969. static void pch_udc_init(struct pch_udc_dev *dev)
  970. {
  971. if (NULL == dev) {
  972. pr_err("%s: Invalid address\n", __func__);
  973. return;
  974. }
  975. /* Soft Reset and Reset PHY */
  976. pch_udc_writel(dev, UDC_SRST, UDC_SRST_ADDR);
  977. pch_udc_writel(dev, UDC_SRST | UDC_PSRST, UDC_SRST_ADDR);
  978. mdelay(1);
  979. pch_udc_writel(dev, UDC_SRST, UDC_SRST_ADDR);
  980. pch_udc_writel(dev, 0x00, UDC_SRST_ADDR);
  981. mdelay(1);
  982. /* mask and clear all device interrupts */
  983. pch_udc_bit_set(dev, UDC_DEVIRQMSK_ADDR, UDC_DEVINT_MSK);
  984. pch_udc_bit_set(dev, UDC_DEVIRQSTS_ADDR, UDC_DEVINT_MSK);
  985. /* mask and clear all ep interrupts */
  986. pch_udc_bit_set(dev, UDC_EPIRQMSK_ADDR, UDC_EPINT_MSK_DISABLE_ALL);
  987. pch_udc_bit_set(dev, UDC_EPIRQSTS_ADDR, UDC_EPINT_MSK_DISABLE_ALL);
  988. /* enable dynamic CSR programmingi, self powered and device speed */
  989. if (speed_fs)
  990. pch_udc_bit_set(dev, UDC_DEVCFG_ADDR, UDC_DEVCFG_CSR_PRG |
  991. UDC_DEVCFG_SP | UDC_DEVCFG_SPD_FS);
  992. else /* defaul high speed */
  993. pch_udc_bit_set(dev, UDC_DEVCFG_ADDR, UDC_DEVCFG_CSR_PRG |
  994. UDC_DEVCFG_SP | UDC_DEVCFG_SPD_HS);
  995. pch_udc_bit_set(dev, UDC_DEVCTL_ADDR,
  996. (PCH_UDC_THLEN << UDC_DEVCTL_THLEN_SHIFT) |
  997. (PCH_UDC_BRLEN << UDC_DEVCTL_BRLEN_SHIFT) |
  998. UDC_DEVCTL_MODE | UDC_DEVCTL_BREN |
  999. UDC_DEVCTL_THE);
  1000. }
  1001. /**
  1002. * pch_udc_exit() - This API exit usb device controller
  1003. * @dev: Reference to pch_udc_regs structure
  1004. */
  1005. static void pch_udc_exit(struct pch_udc_dev *dev)
  1006. {
  1007. /* mask all device interrupts */
  1008. pch_udc_bit_set(dev, UDC_DEVIRQMSK_ADDR, UDC_DEVINT_MSK);
  1009. /* mask all ep interrupts */
  1010. pch_udc_bit_set(dev, UDC_EPIRQMSK_ADDR, UDC_EPINT_MSK_DISABLE_ALL);
  1011. /* put device in disconnected state */
  1012. pch_udc_set_disconnect(dev);
  1013. }
  1014. /**
  1015. * pch_udc_pcd_get_frame() - This API is invoked to get the current frame number
  1016. * @gadget: Reference to the gadget driver
  1017. *
  1018. * Return codes:
  1019. * 0: Success
  1020. * -EINVAL: If the gadget passed is NULL
  1021. */
  1022. static int pch_udc_pcd_get_frame(struct usb_gadget *gadget)
  1023. {
  1024. struct pch_udc_dev *dev;
  1025. if (!gadget)
  1026. return -EINVAL;
  1027. dev = container_of(gadget, struct pch_udc_dev, gadget);
  1028. return pch_udc_get_frame(dev);
  1029. }
  1030. /**
  1031. * pch_udc_pcd_wakeup() - This API is invoked to initiate a remote wakeup
  1032. * @gadget: Reference to the gadget driver
  1033. *
  1034. * Return codes:
  1035. * 0: Success
  1036. * -EINVAL: If the gadget passed is NULL
  1037. */
  1038. static int pch_udc_pcd_wakeup(struct usb_gadget *gadget)
  1039. {
  1040. struct pch_udc_dev *dev;
  1041. unsigned long flags;
  1042. if (!gadget)
  1043. return -EINVAL;
  1044. dev = container_of(gadget, struct pch_udc_dev, gadget);
  1045. spin_lock_irqsave(&dev->lock, flags);
  1046. pch_udc_rmt_wakeup(dev);
  1047. spin_unlock_irqrestore(&dev->lock, flags);
  1048. return 0;
  1049. }
  1050. /**
  1051. * pch_udc_pcd_selfpowered() - This API is invoked to specify whether the device
  1052. * is self powered or not
  1053. * @gadget: Reference to the gadget driver
  1054. * @value: Specifies self powered or not
  1055. *
  1056. * Return codes:
  1057. * 0: Success
  1058. * -EINVAL: If the gadget passed is NULL
  1059. */
  1060. static int pch_udc_pcd_selfpowered(struct usb_gadget *gadget, int value)
  1061. {
  1062. struct pch_udc_dev *dev;
  1063. if (!gadget)
  1064. return -EINVAL;
  1065. gadget->is_selfpowered = (value != 0);
  1066. dev = container_of(gadget, struct pch_udc_dev, gadget);
  1067. if (value)
  1068. pch_udc_set_selfpowered(dev);
  1069. else
  1070. pch_udc_clear_selfpowered(dev);
  1071. return 0;
  1072. }
  1073. /**
  1074. * pch_udc_pcd_pullup() - This API is invoked to make the device
  1075. * visible/invisible to the host
  1076. * @gadget: Reference to the gadget driver
  1077. * @is_on: Specifies whether the pull up is made active or inactive
  1078. *
  1079. * Return codes:
  1080. * 0: Success
  1081. * -EINVAL: If the gadget passed is NULL
  1082. */
  1083. static int pch_udc_pcd_pullup(struct usb_gadget *gadget, int is_on)
  1084. {
  1085. struct pch_udc_dev *dev;
  1086. if (!gadget)
  1087. return -EINVAL;
  1088. dev = container_of(gadget, struct pch_udc_dev, gadget);
  1089. if (is_on) {
  1090. pch_udc_reconnect(dev);
  1091. } else {
  1092. if (dev->driver && dev->driver->disconnect) {
  1093. spin_lock(&dev->lock);
  1094. dev->driver->disconnect(&dev->gadget);
  1095. spin_unlock(&dev->lock);
  1096. }
  1097. pch_udc_set_disconnect(dev);
  1098. }
  1099. return 0;
  1100. }
  1101. /**
  1102. * pch_udc_pcd_vbus_session() - This API is used by a driver for an external
  1103. * transceiver (or GPIO) that
  1104. * detects a VBUS power session starting/ending
  1105. * @gadget: Reference to the gadget driver
  1106. * @is_active: specifies whether the session is starting or ending
  1107. *
  1108. * Return codes:
  1109. * 0: Success
  1110. * -EINVAL: If the gadget passed is NULL
  1111. */
  1112. static int pch_udc_pcd_vbus_session(struct usb_gadget *gadget, int is_active)
  1113. {
  1114. struct pch_udc_dev *dev;
  1115. if (!gadget)
  1116. return -EINVAL;
  1117. dev = container_of(gadget, struct pch_udc_dev, gadget);
  1118. pch_udc_vbus_session(dev, is_active);
  1119. return 0;
  1120. }
  1121. /**
  1122. * pch_udc_pcd_vbus_draw() - This API is used by gadget drivers during
  1123. * SET_CONFIGURATION calls to
  1124. * specify how much power the device can consume
  1125. * @gadget: Reference to the gadget driver
  1126. * @mA: specifies the current limit in 2mA unit
  1127. *
  1128. * Return codes:
  1129. * -EINVAL: If the gadget passed is NULL
  1130. * -EOPNOTSUPP:
  1131. */
  1132. static int pch_udc_pcd_vbus_draw(struct usb_gadget *gadget, unsigned int mA)
  1133. {
  1134. return -EOPNOTSUPP;
  1135. }
  1136. static int pch_udc_start(struct usb_gadget *g,
  1137. struct usb_gadget_driver *driver);
  1138. static int pch_udc_stop(struct usb_gadget *g);
  1139. static const struct usb_gadget_ops pch_udc_ops = {
  1140. .get_frame = pch_udc_pcd_get_frame,
  1141. .wakeup = pch_udc_pcd_wakeup,
  1142. .set_selfpowered = pch_udc_pcd_selfpowered,
  1143. .pullup = pch_udc_pcd_pullup,
  1144. .vbus_session = pch_udc_pcd_vbus_session,
  1145. .vbus_draw = pch_udc_pcd_vbus_draw,
  1146. .udc_start = pch_udc_start,
  1147. .udc_stop = pch_udc_stop,
  1148. };
  1149. /**
  1150. * pch_vbus_gpio_get_value() - This API gets value of GPIO port as VBUS status.
  1151. * @dev: Reference to the driver structure
  1152. *
  1153. * Return value:
  1154. * 1: VBUS is high
  1155. * 0: VBUS is low
  1156. * -1: It is not enable to detect VBUS using GPIO
  1157. */
  1158. static int pch_vbus_gpio_get_value(struct pch_udc_dev *dev)
  1159. {
  1160. int vbus = 0;
  1161. if (dev->vbus_gpio.port)
  1162. vbus = gpio_get_value(dev->vbus_gpio.port) ? 1 : 0;
  1163. else
  1164. vbus = -1;
  1165. return vbus;
  1166. }
  1167. /**
  1168. * pch_vbus_gpio_work_fall() - This API keeps watch on VBUS becoming Low.
  1169. * If VBUS is Low, disconnect is processed
  1170. * @irq_work: Structure for WorkQueue
  1171. *
  1172. */
  1173. static void pch_vbus_gpio_work_fall(struct work_struct *irq_work)
  1174. {
  1175. struct pch_vbus_gpio_data *vbus_gpio = container_of(irq_work,
  1176. struct pch_vbus_gpio_data, irq_work_fall);
  1177. struct pch_udc_dev *dev =
  1178. container_of(vbus_gpio, struct pch_udc_dev, vbus_gpio);
  1179. int vbus_saved = -1;
  1180. int vbus;
  1181. int count;
  1182. if (!dev->vbus_gpio.port)
  1183. return;
  1184. for (count = 0; count < (PCH_VBUS_PERIOD / PCH_VBUS_INTERVAL);
  1185. count++) {
  1186. vbus = pch_vbus_gpio_get_value(dev);
  1187. if ((vbus_saved == vbus) && (vbus == 0)) {
  1188. dev_dbg(&dev->pdev->dev, "VBUS fell");
  1189. if (dev->driver
  1190. && dev->driver->disconnect) {
  1191. dev->driver->disconnect(
  1192. &dev->gadget);
  1193. }
  1194. if (dev->vbus_gpio.intr)
  1195. pch_udc_init(dev);
  1196. else
  1197. pch_udc_reconnect(dev);
  1198. return;
  1199. }
  1200. vbus_saved = vbus;
  1201. mdelay(PCH_VBUS_INTERVAL);
  1202. }
  1203. }
  1204. /**
  1205. * pch_vbus_gpio_work_rise() - This API checks VBUS is High.
  1206. * If VBUS is High, connect is processed
  1207. * @irq_work: Structure for WorkQueue
  1208. *
  1209. */
  1210. static void pch_vbus_gpio_work_rise(struct work_struct *irq_work)
  1211. {
  1212. struct pch_vbus_gpio_data *vbus_gpio = container_of(irq_work,
  1213. struct pch_vbus_gpio_data, irq_work_rise);
  1214. struct pch_udc_dev *dev =
  1215. container_of(vbus_gpio, struct pch_udc_dev, vbus_gpio);
  1216. int vbus;
  1217. if (!dev->vbus_gpio.port)
  1218. return;
  1219. mdelay(PCH_VBUS_INTERVAL);
  1220. vbus = pch_vbus_gpio_get_value(dev);
  1221. if (vbus == 1) {
  1222. dev_dbg(&dev->pdev->dev, "VBUS rose");
  1223. pch_udc_reconnect(dev);
  1224. return;
  1225. }
  1226. }
  1227. /**
  1228. * pch_vbus_gpio_irq() - IRQ handler for GPIO intrerrupt for changing VBUS
  1229. * @irq: Interrupt request number
  1230. * @dev: Reference to the device structure
  1231. *
  1232. * Return codes:
  1233. * 0: Success
  1234. * -EINVAL: GPIO port is invalid or can't be initialized.
  1235. */
  1236. static irqreturn_t pch_vbus_gpio_irq(int irq, void *data)
  1237. {
  1238. struct pch_udc_dev *dev = (struct pch_udc_dev *)data;
  1239. if (!dev->vbus_gpio.port || !dev->vbus_gpio.intr)
  1240. return IRQ_NONE;
  1241. if (pch_vbus_gpio_get_value(dev))
  1242. schedule_work(&dev->vbus_gpio.irq_work_rise);
  1243. else
  1244. schedule_work(&dev->vbus_gpio.irq_work_fall);
  1245. return IRQ_HANDLED;
  1246. }
  1247. /**
  1248. * pch_vbus_gpio_init() - This API initializes GPIO port detecting VBUS.
  1249. * @dev: Reference to the driver structure
  1250. * @vbus_gpio Number of GPIO port to detect gpio
  1251. *
  1252. * Return codes:
  1253. * 0: Success
  1254. * -EINVAL: GPIO port is invalid or can't be initialized.
  1255. */
  1256. static int pch_vbus_gpio_init(struct pch_udc_dev *dev, int vbus_gpio_port)
  1257. {
  1258. int err;
  1259. int irq_num = 0;
  1260. dev->vbus_gpio.port = 0;
  1261. dev->vbus_gpio.intr = 0;
  1262. if (vbus_gpio_port <= -1)
  1263. return -EINVAL;
  1264. err = gpio_is_valid(vbus_gpio_port);
  1265. if (!err) {
  1266. pr_err("%s: gpio port %d is invalid\n",
  1267. __func__, vbus_gpio_port);
  1268. return -EINVAL;
  1269. }
  1270. err = gpio_request(vbus_gpio_port, "pch_vbus");
  1271. if (err) {
  1272. pr_err("%s: can't request gpio port %d, err: %d\n",
  1273. __func__, vbus_gpio_port, err);
  1274. return -EINVAL;
  1275. }
  1276. dev->vbus_gpio.port = vbus_gpio_port;
  1277. gpio_direction_input(vbus_gpio_port);
  1278. INIT_WORK(&dev->vbus_gpio.irq_work_fall, pch_vbus_gpio_work_fall);
  1279. irq_num = gpio_to_irq(vbus_gpio_port);
  1280. if (irq_num > 0) {
  1281. irq_set_irq_type(irq_num, IRQ_TYPE_EDGE_BOTH);
  1282. err = request_irq(irq_num, pch_vbus_gpio_irq, 0,
  1283. "vbus_detect", dev);
  1284. if (!err) {
  1285. dev->vbus_gpio.intr = irq_num;
  1286. INIT_WORK(&dev->vbus_gpio.irq_work_rise,
  1287. pch_vbus_gpio_work_rise);
  1288. } else {
  1289. pr_err("%s: can't request irq %d, err: %d\n",
  1290. __func__, irq_num, err);
  1291. }
  1292. }
  1293. return 0;
  1294. }
  1295. /**
  1296. * pch_vbus_gpio_free() - This API frees resources of GPIO port
  1297. * @dev: Reference to the driver structure
  1298. */
  1299. static void pch_vbus_gpio_free(struct pch_udc_dev *dev)
  1300. {
  1301. if (dev->vbus_gpio.intr)
  1302. free_irq(dev->vbus_gpio.intr, dev);
  1303. if (dev->vbus_gpio.port)
  1304. gpio_free(dev->vbus_gpio.port);
  1305. }
  1306. /**
  1307. * complete_req() - This API is invoked from the driver when processing
  1308. * of a request is complete
  1309. * @ep: Reference to the endpoint structure
  1310. * @req: Reference to the request structure
  1311. * @status: Indicates the success/failure of completion
  1312. */
  1313. static void complete_req(struct pch_udc_ep *ep, struct pch_udc_request *req,
  1314. int status)
  1315. __releases(&dev->lock)
  1316. __acquires(&dev->lock)
  1317. {
  1318. struct pch_udc_dev *dev;
  1319. unsigned halted = ep->halted;
  1320. list_del_init(&req->queue);
  1321. /* set new status if pending */
  1322. if (req->req.status == -EINPROGRESS)
  1323. req->req.status = status;
  1324. else
  1325. status = req->req.status;
  1326. dev = ep->dev;
  1327. if (req->dma_mapped) {
  1328. if (req->dma == DMA_ADDR_INVALID) {
  1329. if (ep->in)
  1330. dma_unmap_single(&dev->pdev->dev, req->req.dma,
  1331. req->req.length,
  1332. DMA_TO_DEVICE);
  1333. else
  1334. dma_unmap_single(&dev->pdev->dev, req->req.dma,
  1335. req->req.length,
  1336. DMA_FROM_DEVICE);
  1337. req->req.dma = DMA_ADDR_INVALID;
  1338. } else {
  1339. if (ep->in)
  1340. dma_unmap_single(&dev->pdev->dev, req->dma,
  1341. req->req.length,
  1342. DMA_TO_DEVICE);
  1343. else {
  1344. dma_unmap_single(&dev->pdev->dev, req->dma,
  1345. req->req.length,
  1346. DMA_FROM_DEVICE);
  1347. memcpy(req->req.buf, req->buf, req->req.length);
  1348. }
  1349. kfree(req->buf);
  1350. req->dma = DMA_ADDR_INVALID;
  1351. }
  1352. req->dma_mapped = 0;
  1353. }
  1354. ep->halted = 1;
  1355. spin_lock(&dev->lock);
  1356. if (!ep->in)
  1357. pch_udc_ep_clear_rrdy(ep);
  1358. usb_gadget_giveback_request(&ep->ep, &req->req);
  1359. spin_unlock(&dev->lock);
  1360. ep->halted = halted;
  1361. }
  1362. /**
  1363. * empty_req_queue() - This API empties the request queue of an endpoint
  1364. * @ep: Reference to the endpoint structure
  1365. */
  1366. static void empty_req_queue(struct pch_udc_ep *ep)
  1367. {
  1368. struct pch_udc_request *req;
  1369. ep->halted = 1;
  1370. while (!list_empty(&ep->queue)) {
  1371. req = list_entry(ep->queue.next, struct pch_udc_request, queue);
  1372. complete_req(ep, req, -ESHUTDOWN); /* Remove from list */
  1373. }
  1374. }
  1375. /**
  1376. * pch_udc_free_dma_chain() - This function frees the DMA chain created
  1377. * for the request
  1378. * @dev Reference to the driver structure
  1379. * @req Reference to the request to be freed
  1380. *
  1381. * Return codes:
  1382. * 0: Success
  1383. */
  1384. static void pch_udc_free_dma_chain(struct pch_udc_dev *dev,
  1385. struct pch_udc_request *req)
  1386. {
  1387. struct pch_udc_data_dma_desc *td = req->td_data;
  1388. unsigned i = req->chain_len;
  1389. dma_addr_t addr2;
  1390. dma_addr_t addr = (dma_addr_t)td->next;
  1391. td->next = 0x00;
  1392. for (; i > 1; --i) {
  1393. /* do not free first desc., will be done by free for request */
  1394. td = phys_to_virt(addr);
  1395. addr2 = (dma_addr_t)td->next;
  1396. pci_pool_free(dev->data_requests, td, addr);
  1397. addr = addr2;
  1398. }
  1399. req->chain_len = 1;
  1400. }
  1401. /**
  1402. * pch_udc_create_dma_chain() - This function creates or reinitializes
  1403. * a DMA chain
  1404. * @ep: Reference to the endpoint structure
  1405. * @req: Reference to the request
  1406. * @buf_len: The buffer length
  1407. * @gfp_flags: Flags to be used while mapping the data buffer
  1408. *
  1409. * Return codes:
  1410. * 0: success,
  1411. * -ENOMEM: pci_pool_alloc invocation fails
  1412. */
  1413. static int pch_udc_create_dma_chain(struct pch_udc_ep *ep,
  1414. struct pch_udc_request *req,
  1415. unsigned long buf_len,
  1416. gfp_t gfp_flags)
  1417. {
  1418. struct pch_udc_data_dma_desc *td = req->td_data, *last;
  1419. unsigned long bytes = req->req.length, i = 0;
  1420. dma_addr_t dma_addr;
  1421. unsigned len = 1;
  1422. if (req->chain_len > 1)
  1423. pch_udc_free_dma_chain(ep->dev, req);
  1424. if (req->dma == DMA_ADDR_INVALID)
  1425. td->dataptr = req->req.dma;
  1426. else
  1427. td->dataptr = req->dma;
  1428. td->status = PCH_UDC_BS_HST_BSY;
  1429. for (; ; bytes -= buf_len, ++len) {
  1430. td->status = PCH_UDC_BS_HST_BSY | min(buf_len, bytes);
  1431. if (bytes <= buf_len)
  1432. break;
  1433. last = td;
  1434. td = pci_pool_alloc(ep->dev->data_requests, gfp_flags,
  1435. &dma_addr);
  1436. if (!td)
  1437. goto nomem;
  1438. i += buf_len;
  1439. td->dataptr = req->td_data->dataptr + i;
  1440. last->next = dma_addr;
  1441. }
  1442. req->td_data_last = td;
  1443. td->status |= PCH_UDC_DMA_LAST;
  1444. td->next = req->td_data_phys;
  1445. req->chain_len = len;
  1446. return 0;
  1447. nomem:
  1448. if (len > 1) {
  1449. req->chain_len = len;
  1450. pch_udc_free_dma_chain(ep->dev, req);
  1451. }
  1452. req->chain_len = 1;
  1453. return -ENOMEM;
  1454. }
  1455. /**
  1456. * prepare_dma() - This function creates and initializes the DMA chain
  1457. * for the request
  1458. * @ep: Reference to the endpoint structure
  1459. * @req: Reference to the request
  1460. * @gfp: Flag to be used while mapping the data buffer
  1461. *
  1462. * Return codes:
  1463. * 0: Success
  1464. * Other 0: linux error number on failure
  1465. */
  1466. static int prepare_dma(struct pch_udc_ep *ep, struct pch_udc_request *req,
  1467. gfp_t gfp)
  1468. {
  1469. int retval;
  1470. /* Allocate and create a DMA chain */
  1471. retval = pch_udc_create_dma_chain(ep, req, ep->ep.maxpacket, gfp);
  1472. if (retval) {
  1473. pr_err("%s: could not create DMA chain:%d\n", __func__, retval);
  1474. return retval;
  1475. }
  1476. if (ep->in)
  1477. req->td_data->status = (req->td_data->status &
  1478. ~PCH_UDC_BUFF_STS) | PCH_UDC_BS_HST_RDY;
  1479. return 0;
  1480. }
  1481. /**
  1482. * process_zlp() - This function process zero length packets
  1483. * from the gadget driver
  1484. * @ep: Reference to the endpoint structure
  1485. * @req: Reference to the request
  1486. */
  1487. static void process_zlp(struct pch_udc_ep *ep, struct pch_udc_request *req)
  1488. {
  1489. struct pch_udc_dev *dev = ep->dev;
  1490. /* IN zlp's are handled by hardware */
  1491. complete_req(ep, req, 0);
  1492. /* if set_config or set_intf is waiting for ack by zlp
  1493. * then set CSR_DONE
  1494. */
  1495. if (dev->set_cfg_not_acked) {
  1496. pch_udc_set_csr_done(dev);
  1497. dev->set_cfg_not_acked = 0;
  1498. }
  1499. /* setup command is ACK'ed now by zlp */
  1500. if (!dev->stall && dev->waiting_zlp_ack) {
  1501. pch_udc_ep_clear_nak(&(dev->ep[UDC_EP0IN_IDX]));
  1502. dev->waiting_zlp_ack = 0;
  1503. }
  1504. }
  1505. /**
  1506. * pch_udc_start_rxrequest() - This function starts the receive requirement.
  1507. * @ep: Reference to the endpoint structure
  1508. * @req: Reference to the request structure
  1509. */
  1510. static void pch_udc_start_rxrequest(struct pch_udc_ep *ep,
  1511. struct pch_udc_request *req)
  1512. {
  1513. struct pch_udc_data_dma_desc *td_data;
  1514. pch_udc_clear_dma(ep->dev, DMA_DIR_RX);
  1515. td_data = req->td_data;
  1516. /* Set the status bits for all descriptors */
  1517. while (1) {
  1518. td_data->status = (td_data->status & ~PCH_UDC_BUFF_STS) |
  1519. PCH_UDC_BS_HST_RDY;
  1520. if ((td_data->status & PCH_UDC_DMA_LAST) == PCH_UDC_DMA_LAST)
  1521. break;
  1522. td_data = phys_to_virt(td_data->next);
  1523. }
  1524. /* Write the descriptor pointer */
  1525. pch_udc_ep_set_ddptr(ep, req->td_data_phys);
  1526. req->dma_going = 1;
  1527. pch_udc_enable_ep_interrupts(ep->dev, UDC_EPINT_OUT_EP0 << ep->num);
  1528. pch_udc_set_dma(ep->dev, DMA_DIR_RX);
  1529. pch_udc_ep_clear_nak(ep);
  1530. pch_udc_ep_set_rrdy(ep);
  1531. }
  1532. /**
  1533. * pch_udc_pcd_ep_enable() - This API enables the endpoint. It is called
  1534. * from gadget driver
  1535. * @usbep: Reference to the USB endpoint structure
  1536. * @desc: Reference to the USB endpoint descriptor structure
  1537. *
  1538. * Return codes:
  1539. * 0: Success
  1540. * -EINVAL:
  1541. * -ESHUTDOWN:
  1542. */
  1543. static int pch_udc_pcd_ep_enable(struct usb_ep *usbep,
  1544. const struct usb_endpoint_descriptor *desc)
  1545. {
  1546. struct pch_udc_ep *ep;
  1547. struct pch_udc_dev *dev;
  1548. unsigned long iflags;
  1549. if (!usbep || (usbep->name == ep0_string) || !desc ||
  1550. (desc->bDescriptorType != USB_DT_ENDPOINT) || !desc->wMaxPacketSize)
  1551. return -EINVAL;
  1552. ep = container_of(usbep, struct pch_udc_ep, ep);
  1553. dev = ep->dev;
  1554. if (!dev->driver || (dev->gadget.speed == USB_SPEED_UNKNOWN))
  1555. return -ESHUTDOWN;
  1556. spin_lock_irqsave(&dev->lock, iflags);
  1557. ep->ep.desc = desc;
  1558. ep->halted = 0;
  1559. pch_udc_ep_enable(ep, &ep->dev->cfg_data, desc);
  1560. ep->ep.maxpacket = usb_endpoint_maxp(desc);
  1561. pch_udc_enable_ep_interrupts(ep->dev, PCH_UDC_EPINT(ep->in, ep->num));
  1562. spin_unlock_irqrestore(&dev->lock, iflags);
  1563. return 0;
  1564. }
  1565. /**
  1566. * pch_udc_pcd_ep_disable() - This API disables endpoint and is called
  1567. * from gadget driver
  1568. * @usbep Reference to the USB endpoint structure
  1569. *
  1570. * Return codes:
  1571. * 0: Success
  1572. * -EINVAL:
  1573. */
  1574. static int pch_udc_pcd_ep_disable(struct usb_ep *usbep)
  1575. {
  1576. struct pch_udc_ep *ep;
  1577. struct pch_udc_dev *dev;
  1578. unsigned long iflags;
  1579. if (!usbep)
  1580. return -EINVAL;
  1581. ep = container_of(usbep, struct pch_udc_ep, ep);
  1582. dev = ep->dev;
  1583. if ((usbep->name == ep0_string) || !ep->ep.desc)
  1584. return -EINVAL;
  1585. spin_lock_irqsave(&ep->dev->lock, iflags);
  1586. empty_req_queue(ep);
  1587. ep->halted = 1;
  1588. pch_udc_ep_disable(ep);
  1589. pch_udc_disable_ep_interrupts(ep->dev, PCH_UDC_EPINT(ep->in, ep->num));
  1590. ep->ep.desc = NULL;
  1591. INIT_LIST_HEAD(&ep->queue);
  1592. spin_unlock_irqrestore(&ep->dev->lock, iflags);
  1593. return 0;
  1594. }
  1595. /**
  1596. * pch_udc_alloc_request() - This function allocates request structure.
  1597. * It is called by gadget driver
  1598. * @usbep: Reference to the USB endpoint structure
  1599. * @gfp: Flag to be used while allocating memory
  1600. *
  1601. * Return codes:
  1602. * NULL: Failure
  1603. * Allocated address: Success
  1604. */
  1605. static struct usb_request *pch_udc_alloc_request(struct usb_ep *usbep,
  1606. gfp_t gfp)
  1607. {
  1608. struct pch_udc_request *req;
  1609. struct pch_udc_ep *ep;
  1610. struct pch_udc_data_dma_desc *dma_desc;
  1611. struct pch_udc_dev *dev;
  1612. if (!usbep)
  1613. return NULL;
  1614. ep = container_of(usbep, struct pch_udc_ep, ep);
  1615. dev = ep->dev;
  1616. req = kzalloc(sizeof *req, gfp);
  1617. if (!req)
  1618. return NULL;
  1619. req->req.dma = DMA_ADDR_INVALID;
  1620. req->dma = DMA_ADDR_INVALID;
  1621. INIT_LIST_HEAD(&req->queue);
  1622. if (!ep->dev->dma_addr)
  1623. return &req->req;
  1624. /* ep0 in requests are allocated from data pool here */
  1625. dma_desc = pci_pool_alloc(ep->dev->data_requests, gfp,
  1626. &req->td_data_phys);
  1627. if (NULL == dma_desc) {
  1628. kfree(req);
  1629. return NULL;
  1630. }
  1631. /* prevent from using desc. - set HOST BUSY */
  1632. dma_desc->status |= PCH_UDC_BS_HST_BSY;
  1633. dma_desc->dataptr = cpu_to_le32(DMA_ADDR_INVALID);
  1634. req->td_data = dma_desc;
  1635. req->td_data_last = dma_desc;
  1636. req->chain_len = 1;
  1637. return &req->req;
  1638. }
  1639. /**
  1640. * pch_udc_free_request() - This function frees request structure.
  1641. * It is called by gadget driver
  1642. * @usbep: Reference to the USB endpoint structure
  1643. * @usbreq: Reference to the USB request
  1644. */
  1645. static void pch_udc_free_request(struct usb_ep *usbep,
  1646. struct usb_request *usbreq)
  1647. {
  1648. struct pch_udc_ep *ep;
  1649. struct pch_udc_request *req;
  1650. struct pch_udc_dev *dev;
  1651. if (!usbep || !usbreq)
  1652. return;
  1653. ep = container_of(usbep, struct pch_udc_ep, ep);
  1654. req = container_of(usbreq, struct pch_udc_request, req);
  1655. dev = ep->dev;
  1656. if (!list_empty(&req->queue))
  1657. dev_err(&dev->pdev->dev, "%s: %s req=0x%p queue not empty\n",
  1658. __func__, usbep->name, req);
  1659. if (req->td_data != NULL) {
  1660. if (req->chain_len > 1)
  1661. pch_udc_free_dma_chain(ep->dev, req);
  1662. pci_pool_free(ep->dev->data_requests, req->td_data,
  1663. req->td_data_phys);
  1664. }
  1665. kfree(req);
  1666. }
  1667. /**
  1668. * pch_udc_pcd_queue() - This function queues a request packet. It is called
  1669. * by gadget driver
  1670. * @usbep: Reference to the USB endpoint structure
  1671. * @usbreq: Reference to the USB request
  1672. * @gfp: Flag to be used while mapping the data buffer
  1673. *
  1674. * Return codes:
  1675. * 0: Success
  1676. * linux error number: Failure
  1677. */
  1678. static int pch_udc_pcd_queue(struct usb_ep *usbep, struct usb_request *usbreq,
  1679. gfp_t gfp)
  1680. {
  1681. int retval = 0;
  1682. struct pch_udc_ep *ep;
  1683. struct pch_udc_dev *dev;
  1684. struct pch_udc_request *req;
  1685. unsigned long iflags;
  1686. if (!usbep || !usbreq || !usbreq->complete || !usbreq->buf)
  1687. return -EINVAL;
  1688. ep = container_of(usbep, struct pch_udc_ep, ep);
  1689. dev = ep->dev;
  1690. if (!ep->ep.desc && ep->num)
  1691. return -EINVAL;
  1692. req = container_of(usbreq, struct pch_udc_request, req);
  1693. if (!list_empty(&req->queue))
  1694. return -EINVAL;
  1695. if (!dev->driver || (dev->gadget.speed == USB_SPEED_UNKNOWN))
  1696. return -ESHUTDOWN;
  1697. spin_lock_irqsave(&dev->lock, iflags);
  1698. /* map the buffer for dma */
  1699. if (usbreq->length &&
  1700. ((usbreq->dma == DMA_ADDR_INVALID) || !usbreq->dma)) {
  1701. if (!((unsigned long)(usbreq->buf) & 0x03)) {
  1702. if (ep->in)
  1703. usbreq->dma = dma_map_single(&dev->pdev->dev,
  1704. usbreq->buf,
  1705. usbreq->length,
  1706. DMA_TO_DEVICE);
  1707. else
  1708. usbreq->dma = dma_map_single(&dev->pdev->dev,
  1709. usbreq->buf,
  1710. usbreq->length,
  1711. DMA_FROM_DEVICE);
  1712. } else {
  1713. req->buf = kzalloc(usbreq->length, GFP_ATOMIC);
  1714. if (!req->buf) {
  1715. retval = -ENOMEM;
  1716. goto probe_end;
  1717. }
  1718. if (ep->in) {
  1719. memcpy(req->buf, usbreq->buf, usbreq->length);
  1720. req->dma = dma_map_single(&dev->pdev->dev,
  1721. req->buf,
  1722. usbreq->length,
  1723. DMA_TO_DEVICE);
  1724. } else
  1725. req->dma = dma_map_single(&dev->pdev->dev,
  1726. req->buf,
  1727. usbreq->length,
  1728. DMA_FROM_DEVICE);
  1729. }
  1730. req->dma_mapped = 1;
  1731. }
  1732. if (usbreq->length > 0) {
  1733. retval = prepare_dma(ep, req, GFP_ATOMIC);
  1734. if (retval)
  1735. goto probe_end;
  1736. }
  1737. usbreq->actual = 0;
  1738. usbreq->status = -EINPROGRESS;
  1739. req->dma_done = 0;
  1740. if (list_empty(&ep->queue) && !ep->halted) {
  1741. /* no pending transfer, so start this req */
  1742. if (!usbreq->length) {
  1743. process_zlp(ep, req);
  1744. retval = 0;
  1745. goto probe_end;
  1746. }
  1747. if (!ep->in) {
  1748. pch_udc_start_rxrequest(ep, req);
  1749. } else {
  1750. /*
  1751. * For IN trfr the descriptors will be programmed and
  1752. * P bit will be set when
  1753. * we get an IN token
  1754. */
  1755. pch_udc_wait_ep_stall(ep);
  1756. pch_udc_ep_clear_nak(ep);
  1757. pch_udc_enable_ep_interrupts(ep->dev, (1 << ep->num));
  1758. }
  1759. }
  1760. /* Now add this request to the ep's pending requests */
  1761. if (req != NULL)
  1762. list_add_tail(&req->queue, &ep->queue);
  1763. probe_end:
  1764. spin_unlock_irqrestore(&dev->lock, iflags);
  1765. return retval;
  1766. }
  1767. /**
  1768. * pch_udc_pcd_dequeue() - This function de-queues a request packet.
  1769. * It is called by gadget driver
  1770. * @usbep: Reference to the USB endpoint structure
  1771. * @usbreq: Reference to the USB request
  1772. *
  1773. * Return codes:
  1774. * 0: Success
  1775. * linux error number: Failure
  1776. */
  1777. static int pch_udc_pcd_dequeue(struct usb_ep *usbep,
  1778. struct usb_request *usbreq)
  1779. {
  1780. struct pch_udc_ep *ep;
  1781. struct pch_udc_request *req;
  1782. struct pch_udc_dev *dev;
  1783. unsigned long flags;
  1784. int ret = -EINVAL;
  1785. ep = container_of(usbep, struct pch_udc_ep, ep);
  1786. dev = ep->dev;
  1787. if (!usbep || !usbreq || (!ep->ep.desc && ep->num))
  1788. return ret;
  1789. req = container_of(usbreq, struct pch_udc_request, req);
  1790. spin_lock_irqsave(&ep->dev->lock, flags);
  1791. /* make sure it's still queued on this endpoint */
  1792. list_for_each_entry(req, &ep->queue, queue) {
  1793. if (&req->req == usbreq) {
  1794. pch_udc_ep_set_nak(ep);
  1795. if (!list_empty(&req->queue))
  1796. complete_req(ep, req, -ECONNRESET);
  1797. ret = 0;
  1798. break;
  1799. }
  1800. }
  1801. spin_unlock_irqrestore(&ep->dev->lock, flags);
  1802. return ret;
  1803. }
  1804. /**
  1805. * pch_udc_pcd_set_halt() - This function Sets or clear the endpoint halt
  1806. * feature
  1807. * @usbep: Reference to the USB endpoint structure
  1808. * @halt: Specifies whether to set or clear the feature
  1809. *
  1810. * Return codes:
  1811. * 0: Success
  1812. * linux error number: Failure
  1813. */
  1814. static int pch_udc_pcd_set_halt(struct usb_ep *usbep, int halt)
  1815. {
  1816. struct pch_udc_ep *ep;
  1817. struct pch_udc_dev *dev;
  1818. unsigned long iflags;
  1819. int ret;
  1820. if (!usbep)
  1821. return -EINVAL;
  1822. ep = container_of(usbep, struct pch_udc_ep, ep);
  1823. dev = ep->dev;
  1824. if (!ep->ep.desc && !ep->num)
  1825. return -EINVAL;
  1826. if (!ep->dev->driver || (ep->dev->gadget.speed == USB_SPEED_UNKNOWN))
  1827. return -ESHUTDOWN;
  1828. spin_lock_irqsave(&udc_stall_spinlock, iflags);
  1829. if (list_empty(&ep->queue)) {
  1830. if (halt) {
  1831. if (ep->num == PCH_UDC_EP0)
  1832. ep->dev->stall = 1;
  1833. pch_udc_ep_set_stall(ep);
  1834. pch_udc_enable_ep_interrupts(ep->dev,
  1835. PCH_UDC_EPINT(ep->in,
  1836. ep->num));
  1837. } else {
  1838. pch_udc_ep_clear_stall(ep);
  1839. }
  1840. ret = 0;
  1841. } else {
  1842. ret = -EAGAIN;
  1843. }
  1844. spin_unlock_irqrestore(&udc_stall_spinlock, iflags);
  1845. return ret;
  1846. }
  1847. /**
  1848. * pch_udc_pcd_set_wedge() - This function Sets or clear the endpoint
  1849. * halt feature
  1850. * @usbep: Reference to the USB endpoint structure
  1851. * @halt: Specifies whether to set or clear the feature
  1852. *
  1853. * Return codes:
  1854. * 0: Success
  1855. * linux error number: Failure
  1856. */
  1857. static int pch_udc_pcd_set_wedge(struct usb_ep *usbep)
  1858. {
  1859. struct pch_udc_ep *ep;
  1860. struct pch_udc_dev *dev;
  1861. unsigned long iflags;
  1862. int ret;
  1863. if (!usbep)
  1864. return -EINVAL;
  1865. ep = container_of(usbep, struct pch_udc_ep, ep);
  1866. dev = ep->dev;
  1867. if (!ep->ep.desc && !ep->num)
  1868. return -EINVAL;
  1869. if (!ep->dev->driver || (ep->dev->gadget.speed == USB_SPEED_UNKNOWN))
  1870. return -ESHUTDOWN;
  1871. spin_lock_irqsave(&udc_stall_spinlock, iflags);
  1872. if (!list_empty(&ep->queue)) {
  1873. ret = -EAGAIN;
  1874. } else {
  1875. if (ep->num == PCH_UDC_EP0)
  1876. ep->dev->stall = 1;
  1877. pch_udc_ep_set_stall(ep);
  1878. pch_udc_enable_ep_interrupts(ep->dev,
  1879. PCH_UDC_EPINT(ep->in, ep->num));
  1880. ep->dev->prot_stall = 1;
  1881. ret = 0;
  1882. }
  1883. spin_unlock_irqrestore(&udc_stall_spinlock, iflags);
  1884. return ret;
  1885. }
  1886. /**
  1887. * pch_udc_pcd_fifo_flush() - This function Flush the FIFO of specified endpoint
  1888. * @usbep: Reference to the USB endpoint structure
  1889. */
  1890. static void pch_udc_pcd_fifo_flush(struct usb_ep *usbep)
  1891. {
  1892. struct pch_udc_ep *ep;
  1893. if (!usbep)
  1894. return;
  1895. ep = container_of(usbep, struct pch_udc_ep, ep);
  1896. if (ep->ep.desc || !ep->num)
  1897. pch_udc_ep_fifo_flush(ep, ep->in);
  1898. }
  1899. static const struct usb_ep_ops pch_udc_ep_ops = {
  1900. .enable = pch_udc_pcd_ep_enable,
  1901. .disable = pch_udc_pcd_ep_disable,
  1902. .alloc_request = pch_udc_alloc_request,
  1903. .free_request = pch_udc_free_request,
  1904. .queue = pch_udc_pcd_queue,
  1905. .dequeue = pch_udc_pcd_dequeue,
  1906. .set_halt = pch_udc_pcd_set_halt,
  1907. .set_wedge = pch_udc_pcd_set_wedge,
  1908. .fifo_status = NULL,
  1909. .fifo_flush = pch_udc_pcd_fifo_flush,
  1910. };
  1911. /**
  1912. * pch_udc_init_setup_buff() - This function initializes the SETUP buffer
  1913. * @td_stp: Reference to the SETP buffer structure
  1914. */
  1915. static void pch_udc_init_setup_buff(struct pch_udc_stp_dma_desc *td_stp)
  1916. {
  1917. static u32 pky_marker;
  1918. if (!td_stp)
  1919. return;
  1920. td_stp->reserved = ++pky_marker;
  1921. memset(&td_stp->request, 0xFF, sizeof td_stp->request);
  1922. td_stp->status = PCH_UDC_BS_HST_RDY;
  1923. }
  1924. /**
  1925. * pch_udc_start_next_txrequest() - This function starts
  1926. * the next transmission requirement
  1927. * @ep: Reference to the endpoint structure
  1928. */
  1929. static void pch_udc_start_next_txrequest(struct pch_udc_ep *ep)
  1930. {
  1931. struct pch_udc_request *req;
  1932. struct pch_udc_data_dma_desc *td_data;
  1933. if (pch_udc_read_ep_control(ep) & UDC_EPCTL_P)
  1934. return;
  1935. if (list_empty(&ep->queue))
  1936. return;
  1937. /* next request */
  1938. req = list_entry(ep->queue.next, struct pch_udc_request, queue);
  1939. if (req->dma_going)
  1940. return;
  1941. if (!req->td_data)
  1942. return;
  1943. pch_udc_wait_ep_stall(ep);
  1944. req->dma_going = 1;
  1945. pch_udc_ep_set_ddptr(ep, 0);
  1946. td_data = req->td_data;
  1947. while (1) {
  1948. td_data->status = (td_data->status & ~PCH_UDC_BUFF_STS) |
  1949. PCH_UDC_BS_HST_RDY;
  1950. if ((td_data->status & PCH_UDC_DMA_LAST) == PCH_UDC_DMA_LAST)
  1951. break;
  1952. td_data = phys_to_virt(td_data->next);
  1953. }
  1954. pch_udc_ep_set_ddptr(ep, req->td_data_phys);
  1955. pch_udc_set_dma(ep->dev, DMA_DIR_TX);
  1956. pch_udc_ep_set_pd(ep);
  1957. pch_udc_enable_ep_interrupts(ep->dev, PCH_UDC_EPINT(ep->in, ep->num));
  1958. pch_udc_ep_clear_nak(ep);
  1959. }
  1960. /**
  1961. * pch_udc_complete_transfer() - This function completes a transfer
  1962. * @ep: Reference to the endpoint structure
  1963. */
  1964. static void pch_udc_complete_transfer(struct pch_udc_ep *ep)
  1965. {
  1966. struct pch_udc_request *req;
  1967. struct pch_udc_dev *dev = ep->dev;
  1968. if (list_empty(&ep->queue))
  1969. return;
  1970. req = list_entry(ep->queue.next, struct pch_udc_request, queue);
  1971. if ((req->td_data_last->status & PCH_UDC_BUFF_STS) !=
  1972. PCH_UDC_BS_DMA_DONE)
  1973. return;
  1974. if ((req->td_data_last->status & PCH_UDC_RXTX_STS) !=
  1975. PCH_UDC_RTS_SUCC) {
  1976. dev_err(&dev->pdev->dev, "Invalid RXTX status (0x%08x) "
  1977. "epstatus=0x%08x\n",
  1978. (req->td_data_last->status & PCH_UDC_RXTX_STS),
  1979. (int)(ep->epsts));
  1980. return;
  1981. }
  1982. req->req.actual = req->req.length;
  1983. req->td_data_last->status = PCH_UDC_BS_HST_BSY | PCH_UDC_DMA_LAST;
  1984. req->td_data->status = PCH_UDC_BS_HST_BSY | PCH_UDC_DMA_LAST;
  1985. complete_req(ep, req, 0);
  1986. req->dma_going = 0;
  1987. if (!list_empty(&ep->queue)) {
  1988. pch_udc_wait_ep_stall(ep);
  1989. pch_udc_ep_clear_nak(ep);
  1990. pch_udc_enable_ep_interrupts(ep->dev,
  1991. PCH_UDC_EPINT(ep->in, ep->num));
  1992. } else {
  1993. pch_udc_disable_ep_interrupts(ep->dev,
  1994. PCH_UDC_EPINT(ep->in, ep->num));
  1995. }
  1996. }
  1997. /**
  1998. * pch_udc_complete_receiver() - This function completes a receiver
  1999. * @ep: Reference to the endpoint structure
  2000. */
  2001. static void pch_udc_complete_receiver(struct pch_udc_ep *ep)
  2002. {
  2003. struct pch_udc_request *req;
  2004. struct pch_udc_dev *dev = ep->dev;
  2005. unsigned int count;
  2006. struct pch_udc_data_dma_desc *td;
  2007. dma_addr_t addr;
  2008. if (list_empty(&ep->queue))
  2009. return;
  2010. /* next request */
  2011. req = list_entry(ep->queue.next, struct pch_udc_request, queue);
  2012. pch_udc_clear_dma(ep->dev, DMA_DIR_RX);
  2013. pch_udc_ep_set_ddptr(ep, 0);
  2014. if ((req->td_data_last->status & PCH_UDC_BUFF_STS) ==
  2015. PCH_UDC_BS_DMA_DONE)
  2016. td = req->td_data_last;
  2017. else
  2018. td = req->td_data;
  2019. while (1) {
  2020. if ((td->status & PCH_UDC_RXTX_STS) != PCH_UDC_RTS_SUCC) {
  2021. dev_err(&dev->pdev->dev, "Invalid RXTX status=0x%08x "
  2022. "epstatus=0x%08x\n",
  2023. (req->td_data->status & PCH_UDC_RXTX_STS),
  2024. (int)(ep->epsts));
  2025. return;
  2026. }
  2027. if ((td->status & PCH_UDC_BUFF_STS) == PCH_UDC_BS_DMA_DONE)
  2028. if (td->status & PCH_UDC_DMA_LAST) {
  2029. count = td->status & PCH_UDC_RXTX_BYTES;
  2030. break;
  2031. }
  2032. if (td == req->td_data_last) {
  2033. dev_err(&dev->pdev->dev, "Not complete RX descriptor");
  2034. return;
  2035. }
  2036. addr = (dma_addr_t)td->next;
  2037. td = phys_to_virt(addr);
  2038. }
  2039. /* on 64k packets the RXBYTES field is zero */
  2040. if (!count && (req->req.length == UDC_DMA_MAXPACKET))
  2041. count = UDC_DMA_MAXPACKET;
  2042. req->td_data->status |= PCH_UDC_DMA_LAST;
  2043. td->status |= PCH_UDC_BS_HST_BSY;
  2044. req->dma_going = 0;
  2045. req->req.actual = count;
  2046. complete_req(ep, req, 0);
  2047. /* If there is a new/failed requests try that now */
  2048. if (!list_empty(&ep->queue)) {
  2049. req = list_entry(ep->queue.next, struct pch_udc_request, queue);
  2050. pch_udc_start_rxrequest(ep, req);
  2051. }
  2052. }
  2053. /**
  2054. * pch_udc_svc_data_in() - This function process endpoint interrupts
  2055. * for IN endpoints
  2056. * @dev: Reference to the device structure
  2057. * @ep_num: Endpoint that generated the interrupt
  2058. */
  2059. static void pch_udc_svc_data_in(struct pch_udc_dev *dev, int ep_num)
  2060. {
  2061. u32 epsts;
  2062. struct pch_udc_ep *ep;
  2063. ep = &dev->ep[UDC_EPIN_IDX(ep_num)];
  2064. epsts = ep->epsts;
  2065. ep->epsts = 0;
  2066. if (!(epsts & (UDC_EPSTS_IN | UDC_EPSTS_BNA | UDC_EPSTS_HE |
  2067. UDC_EPSTS_TDC | UDC_EPSTS_RCS | UDC_EPSTS_TXEMPTY |
  2068. UDC_EPSTS_RSS | UDC_EPSTS_XFERDONE)))
  2069. return;
  2070. if ((epsts & UDC_EPSTS_BNA))
  2071. return;
  2072. if (epsts & UDC_EPSTS_HE)
  2073. return;
  2074. if (epsts & UDC_EPSTS_RSS) {
  2075. pch_udc_ep_set_stall(ep);
  2076. pch_udc_enable_ep_interrupts(ep->dev,
  2077. PCH_UDC_EPINT(ep->in, ep->num));
  2078. }
  2079. if (epsts & UDC_EPSTS_RCS) {
  2080. if (!dev->prot_stall) {
  2081. pch_udc_ep_clear_stall(ep);
  2082. } else {
  2083. pch_udc_ep_set_stall(ep);
  2084. pch_udc_enable_ep_interrupts(ep->dev,
  2085. PCH_UDC_EPINT(ep->in, ep->num));
  2086. }
  2087. }
  2088. if (epsts & UDC_EPSTS_TDC)
  2089. pch_udc_complete_transfer(ep);
  2090. /* On IN interrupt, provide data if we have any */
  2091. if ((epsts & UDC_EPSTS_IN) && !(epsts & UDC_EPSTS_RSS) &&
  2092. !(epsts & UDC_EPSTS_TDC) && !(epsts & UDC_EPSTS_TXEMPTY))
  2093. pch_udc_start_next_txrequest(ep);
  2094. }
  2095. /**
  2096. * pch_udc_svc_data_out() - Handles interrupts from OUT endpoint
  2097. * @dev: Reference to the device structure
  2098. * @ep_num: Endpoint that generated the interrupt
  2099. */
  2100. static void pch_udc_svc_data_out(struct pch_udc_dev *dev, int ep_num)
  2101. {
  2102. u32 epsts;
  2103. struct pch_udc_ep *ep;
  2104. struct pch_udc_request *req = NULL;
  2105. ep = &dev->ep[UDC_EPOUT_IDX(ep_num)];
  2106. epsts = ep->epsts;
  2107. ep->epsts = 0;
  2108. if ((epsts & UDC_EPSTS_BNA) && (!list_empty(&ep->queue))) {
  2109. /* next request */
  2110. req = list_entry(ep->queue.next, struct pch_udc_request,
  2111. queue);
  2112. if ((req->td_data_last->status & PCH_UDC_BUFF_STS) !=
  2113. PCH_UDC_BS_DMA_DONE) {
  2114. if (!req->dma_going)
  2115. pch_udc_start_rxrequest(ep, req);
  2116. return;
  2117. }
  2118. }
  2119. if (epsts & UDC_EPSTS_HE)
  2120. return;
  2121. if (epsts & UDC_EPSTS_RSS) {
  2122. pch_udc_ep_set_stall(ep);
  2123. pch_udc_enable_ep_interrupts(ep->dev,
  2124. PCH_UDC_EPINT(ep->in, ep->num));
  2125. }
  2126. if (epsts & UDC_EPSTS_RCS) {
  2127. if (!dev->prot_stall) {
  2128. pch_udc_ep_clear_stall(ep);
  2129. } else {
  2130. pch_udc_ep_set_stall(ep);
  2131. pch_udc_enable_ep_interrupts(ep->dev,
  2132. PCH_UDC_EPINT(ep->in, ep->num));
  2133. }
  2134. }
  2135. if (((epsts & UDC_EPSTS_OUT_MASK) >> UDC_EPSTS_OUT_SHIFT) ==
  2136. UDC_EPSTS_OUT_DATA) {
  2137. if (ep->dev->prot_stall == 1) {
  2138. pch_udc_ep_set_stall(ep);
  2139. pch_udc_enable_ep_interrupts(ep->dev,
  2140. PCH_UDC_EPINT(ep->in, ep->num));
  2141. } else {
  2142. pch_udc_complete_receiver(ep);
  2143. }
  2144. }
  2145. if (list_empty(&ep->queue))
  2146. pch_udc_set_dma(dev, DMA_DIR_RX);
  2147. }
  2148. /**
  2149. * pch_udc_svc_control_in() - Handle Control IN endpoint interrupts
  2150. * @dev: Reference to the device structure
  2151. */
  2152. static void pch_udc_svc_control_in(struct pch_udc_dev *dev)
  2153. {
  2154. u32 epsts;
  2155. struct pch_udc_ep *ep;
  2156. struct pch_udc_ep *ep_out;
  2157. ep = &dev->ep[UDC_EP0IN_IDX];
  2158. ep_out = &dev->ep[UDC_EP0OUT_IDX];
  2159. epsts = ep->epsts;
  2160. ep->epsts = 0;
  2161. if (!(epsts & (UDC_EPSTS_IN | UDC_EPSTS_BNA | UDC_EPSTS_HE |
  2162. UDC_EPSTS_TDC | UDC_EPSTS_RCS | UDC_EPSTS_TXEMPTY |
  2163. UDC_EPSTS_XFERDONE)))
  2164. return;
  2165. if ((epsts & UDC_EPSTS_BNA))
  2166. return;
  2167. if (epsts & UDC_EPSTS_HE)
  2168. return;
  2169. if ((epsts & UDC_EPSTS_TDC) && (!dev->stall)) {
  2170. pch_udc_complete_transfer(ep);
  2171. pch_udc_clear_dma(dev, DMA_DIR_RX);
  2172. ep_out->td_data->status = (ep_out->td_data->status &
  2173. ~PCH_UDC_BUFF_STS) |
  2174. PCH_UDC_BS_HST_RDY;
  2175. pch_udc_ep_clear_nak(ep_out);
  2176. pch_udc_set_dma(dev, DMA_DIR_RX);
  2177. pch_udc_ep_set_rrdy(ep_out);
  2178. }
  2179. /* On IN interrupt, provide data if we have any */
  2180. if ((epsts & UDC_EPSTS_IN) && !(epsts & UDC_EPSTS_TDC) &&
  2181. !(epsts & UDC_EPSTS_TXEMPTY))
  2182. pch_udc_start_next_txrequest(ep);
  2183. }
  2184. /**
  2185. * pch_udc_svc_control_out() - Routine that handle Control
  2186. * OUT endpoint interrupts
  2187. * @dev: Reference to the device structure
  2188. */
  2189. static void pch_udc_svc_control_out(struct pch_udc_dev *dev)
  2190. __releases(&dev->lock)
  2191. __acquires(&dev->lock)
  2192. {
  2193. u32 stat;
  2194. int setup_supported;
  2195. struct pch_udc_ep *ep;
  2196. ep = &dev->ep[UDC_EP0OUT_IDX];
  2197. stat = ep->epsts;
  2198. ep->epsts = 0;
  2199. /* If setup data */
  2200. if (((stat & UDC_EPSTS_OUT_MASK) >> UDC_EPSTS_OUT_SHIFT) ==
  2201. UDC_EPSTS_OUT_SETUP) {
  2202. dev->stall = 0;
  2203. dev->ep[UDC_EP0IN_IDX].halted = 0;
  2204. dev->ep[UDC_EP0OUT_IDX].halted = 0;
  2205. dev->setup_data = ep->td_stp->request;
  2206. pch_udc_init_setup_buff(ep->td_stp);
  2207. pch_udc_clear_dma(dev, DMA_DIR_RX);
  2208. pch_udc_ep_fifo_flush(&(dev->ep[UDC_EP0IN_IDX]),
  2209. dev->ep[UDC_EP0IN_IDX].in);
  2210. if ((dev->setup_data.bRequestType & USB_DIR_IN))
  2211. dev->gadget.ep0 = &dev->ep[UDC_EP0IN_IDX].ep;
  2212. else /* OUT */
  2213. dev->gadget.ep0 = &ep->ep;
  2214. spin_lock(&dev->lock);
  2215. /* If Mass storage Reset */
  2216. if ((dev->setup_data.bRequestType == 0x21) &&
  2217. (dev->setup_data.bRequest == 0xFF))
  2218. dev->prot_stall = 0;
  2219. /* call gadget with setup data received */
  2220. setup_supported = dev->driver->setup(&dev->gadget,
  2221. &dev->setup_data);
  2222. spin_unlock(&dev->lock);
  2223. if (dev->setup_data.bRequestType & USB_DIR_IN) {
  2224. ep->td_data->status = (ep->td_data->status &
  2225. ~PCH_UDC_BUFF_STS) |
  2226. PCH_UDC_BS_HST_RDY;
  2227. pch_udc_ep_set_ddptr(ep, ep->td_data_phys);
  2228. }
  2229. /* ep0 in returns data on IN phase */
  2230. if (setup_supported >= 0 && setup_supported <
  2231. UDC_EP0IN_MAX_PKT_SIZE) {
  2232. pch_udc_ep_clear_nak(&(dev->ep[UDC_EP0IN_IDX]));
  2233. /* Gadget would have queued a request when
  2234. * we called the setup */
  2235. if (!(dev->setup_data.bRequestType & USB_DIR_IN)) {
  2236. pch_udc_set_dma(dev, DMA_DIR_RX);
  2237. pch_udc_ep_clear_nak(ep);
  2238. }
  2239. } else if (setup_supported < 0) {
  2240. /* if unsupported request, then stall */
  2241. pch_udc_ep_set_stall(&(dev->ep[UDC_EP0IN_IDX]));
  2242. pch_udc_enable_ep_interrupts(ep->dev,
  2243. PCH_UDC_EPINT(ep->in, ep->num));
  2244. dev->stall = 0;
  2245. pch_udc_set_dma(dev, DMA_DIR_RX);
  2246. } else {
  2247. dev->waiting_zlp_ack = 1;
  2248. }
  2249. } else if ((((stat & UDC_EPSTS_OUT_MASK) >> UDC_EPSTS_OUT_SHIFT) ==
  2250. UDC_EPSTS_OUT_DATA) && !dev->stall) {
  2251. pch_udc_clear_dma(dev, DMA_DIR_RX);
  2252. pch_udc_ep_set_ddptr(ep, 0);
  2253. if (!list_empty(&ep->queue)) {
  2254. ep->epsts = stat;
  2255. pch_udc_svc_data_out(dev, PCH_UDC_EP0);
  2256. }
  2257. pch_udc_set_dma(dev, DMA_DIR_RX);
  2258. }
  2259. pch_udc_ep_set_rrdy(ep);
  2260. }
  2261. /**
  2262. * pch_udc_postsvc_epinters() - This function enables end point interrupts
  2263. * and clears NAK status
  2264. * @dev: Reference to the device structure
  2265. * @ep_num: End point number
  2266. */
  2267. static void pch_udc_postsvc_epinters(struct pch_udc_dev *dev, int ep_num)
  2268. {
  2269. struct pch_udc_ep *ep;
  2270. struct pch_udc_request *req;
  2271. ep = &dev->ep[UDC_EPIN_IDX(ep_num)];
  2272. if (!list_empty(&ep->queue)) {
  2273. req = list_entry(ep->queue.next, struct pch_udc_request, queue);
  2274. pch_udc_enable_ep_interrupts(ep->dev,
  2275. PCH_UDC_EPINT(ep->in, ep->num));
  2276. pch_udc_ep_clear_nak(ep);
  2277. }
  2278. }
  2279. /**
  2280. * pch_udc_read_all_epstatus() - This function read all endpoint status
  2281. * @dev: Reference to the device structure
  2282. * @ep_intr: Status of endpoint interrupt
  2283. */
  2284. static void pch_udc_read_all_epstatus(struct pch_udc_dev *dev, u32 ep_intr)
  2285. {
  2286. int i;
  2287. struct pch_udc_ep *ep;
  2288. for (i = 0; i < PCH_UDC_USED_EP_NUM; i++) {
  2289. /* IN */
  2290. if (ep_intr & (0x1 << i)) {
  2291. ep = &dev->ep[UDC_EPIN_IDX(i)];
  2292. ep->epsts = pch_udc_read_ep_status(ep);
  2293. pch_udc_clear_ep_status(ep, ep->epsts);
  2294. }
  2295. /* OUT */
  2296. if (ep_intr & (0x10000 << i)) {
  2297. ep = &dev->ep[UDC_EPOUT_IDX(i)];
  2298. ep->epsts = pch_udc_read_ep_status(ep);
  2299. pch_udc_clear_ep_status(ep, ep->epsts);
  2300. }
  2301. }
  2302. }
  2303. /**
  2304. * pch_udc_activate_control_ep() - This function enables the control endpoints
  2305. * for traffic after a reset
  2306. * @dev: Reference to the device structure
  2307. */
  2308. static void pch_udc_activate_control_ep(struct pch_udc_dev *dev)
  2309. {
  2310. struct pch_udc_ep *ep;
  2311. u32 val;
  2312. /* Setup the IN endpoint */
  2313. ep = &dev->ep[UDC_EP0IN_IDX];
  2314. pch_udc_clear_ep_control(ep);
  2315. pch_udc_ep_fifo_flush(ep, ep->in);
  2316. pch_udc_ep_set_bufsz(ep, UDC_EP0IN_BUFF_SIZE, ep->in);
  2317. pch_udc_ep_set_maxpkt(ep, UDC_EP0IN_MAX_PKT_SIZE);
  2318. /* Initialize the IN EP Descriptor */
  2319. ep->td_data = NULL;
  2320. ep->td_stp = NULL;
  2321. ep->td_data_phys = 0;
  2322. ep->td_stp_phys = 0;
  2323. /* Setup the OUT endpoint */
  2324. ep = &dev->ep[UDC_EP0OUT_IDX];
  2325. pch_udc_clear_ep_control(ep);
  2326. pch_udc_ep_fifo_flush(ep, ep->in);
  2327. pch_udc_ep_set_bufsz(ep, UDC_EP0OUT_BUFF_SIZE, ep->in);
  2328. pch_udc_ep_set_maxpkt(ep, UDC_EP0OUT_MAX_PKT_SIZE);
  2329. val = UDC_EP0OUT_MAX_PKT_SIZE << UDC_CSR_NE_MAX_PKT_SHIFT;
  2330. pch_udc_write_csr(ep->dev, val, UDC_EP0OUT_IDX);
  2331. /* Initialize the SETUP buffer */
  2332. pch_udc_init_setup_buff(ep->td_stp);
  2333. /* Write the pointer address of dma descriptor */
  2334. pch_udc_ep_set_subptr(ep, ep->td_stp_phys);
  2335. /* Write the pointer address of Setup descriptor */
  2336. pch_udc_ep_set_ddptr(ep, ep->td_data_phys);
  2337. /* Initialize the dma descriptor */
  2338. ep->td_data->status = PCH_UDC_DMA_LAST;
  2339. ep->td_data->dataptr = dev->dma_addr;
  2340. ep->td_data->next = ep->td_data_phys;
  2341. pch_udc_ep_clear_nak(ep);
  2342. }
  2343. /**
  2344. * pch_udc_svc_ur_interrupt() - This function handles a USB reset interrupt
  2345. * @dev: Reference to driver structure
  2346. */
  2347. static void pch_udc_svc_ur_interrupt(struct pch_udc_dev *dev)
  2348. {
  2349. struct pch_udc_ep *ep;
  2350. int i;
  2351. pch_udc_clear_dma(dev, DMA_DIR_TX);
  2352. pch_udc_clear_dma(dev, DMA_DIR_RX);
  2353. /* Mask all endpoint interrupts */
  2354. pch_udc_disable_ep_interrupts(dev, UDC_EPINT_MSK_DISABLE_ALL);
  2355. /* clear all endpoint interrupts */
  2356. pch_udc_write_ep_interrupts(dev, UDC_EPINT_MSK_DISABLE_ALL);
  2357. for (i = 0; i < PCH_UDC_EP_NUM; i++) {
  2358. ep = &dev->ep[i];
  2359. pch_udc_clear_ep_status(ep, UDC_EPSTS_ALL_CLR_MASK);
  2360. pch_udc_clear_ep_control(ep);
  2361. pch_udc_ep_set_ddptr(ep, 0);
  2362. pch_udc_write_csr(ep->dev, 0x00, i);
  2363. }
  2364. dev->stall = 0;
  2365. dev->prot_stall = 0;
  2366. dev->waiting_zlp_ack = 0;
  2367. dev->set_cfg_not_acked = 0;
  2368. /* disable ep to empty req queue. Skip the control EP's */
  2369. for (i = 0; i < (PCH_UDC_USED_EP_NUM*2); i++) {
  2370. ep = &dev->ep[i];
  2371. pch_udc_ep_set_nak(ep);
  2372. pch_udc_ep_fifo_flush(ep, ep->in);
  2373. /* Complete request queue */
  2374. empty_req_queue(ep);
  2375. }
  2376. if (dev->driver) {
  2377. spin_lock(&dev->lock);
  2378. usb_gadget_udc_reset(&dev->gadget, dev->driver);
  2379. spin_unlock(&dev->lock);
  2380. }
  2381. }
  2382. /**
  2383. * pch_udc_svc_enum_interrupt() - This function handles a USB speed enumeration
  2384. * done interrupt
  2385. * @dev: Reference to driver structure
  2386. */
  2387. static void pch_udc_svc_enum_interrupt(struct pch_udc_dev *dev)
  2388. {
  2389. u32 dev_stat, dev_speed;
  2390. u32 speed = USB_SPEED_FULL;
  2391. dev_stat = pch_udc_read_device_status(dev);
  2392. dev_speed = (dev_stat & UDC_DEVSTS_ENUM_SPEED_MASK) >>
  2393. UDC_DEVSTS_ENUM_SPEED_SHIFT;
  2394. switch (dev_speed) {
  2395. case UDC_DEVSTS_ENUM_SPEED_HIGH:
  2396. speed = USB_SPEED_HIGH;
  2397. break;
  2398. case UDC_DEVSTS_ENUM_SPEED_FULL:
  2399. speed = USB_SPEED_FULL;
  2400. break;
  2401. case UDC_DEVSTS_ENUM_SPEED_LOW:
  2402. speed = USB_SPEED_LOW;
  2403. break;
  2404. default:
  2405. BUG();
  2406. }
  2407. dev->gadget.speed = speed;
  2408. pch_udc_activate_control_ep(dev);
  2409. pch_udc_enable_ep_interrupts(dev, UDC_EPINT_IN_EP0 | UDC_EPINT_OUT_EP0);
  2410. pch_udc_set_dma(dev, DMA_DIR_TX);
  2411. pch_udc_set_dma(dev, DMA_DIR_RX);
  2412. pch_udc_ep_set_rrdy(&(dev->ep[UDC_EP0OUT_IDX]));
  2413. /* enable device interrupts */
  2414. pch_udc_enable_interrupts(dev, UDC_DEVINT_UR | UDC_DEVINT_US |
  2415. UDC_DEVINT_ES | UDC_DEVINT_ENUM |
  2416. UDC_DEVINT_SI | UDC_DEVINT_SC);
  2417. }
  2418. /**
  2419. * pch_udc_svc_intf_interrupt() - This function handles a set interface
  2420. * interrupt
  2421. * @dev: Reference to driver structure
  2422. */
  2423. static void pch_udc_svc_intf_interrupt(struct pch_udc_dev *dev)
  2424. {
  2425. u32 reg, dev_stat = 0;
  2426. int i, ret;
  2427. dev_stat = pch_udc_read_device_status(dev);
  2428. dev->cfg_data.cur_intf = (dev_stat & UDC_DEVSTS_INTF_MASK) >>
  2429. UDC_DEVSTS_INTF_SHIFT;
  2430. dev->cfg_data.cur_alt = (dev_stat & UDC_DEVSTS_ALT_MASK) >>
  2431. UDC_DEVSTS_ALT_SHIFT;
  2432. dev->set_cfg_not_acked = 1;
  2433. /* Construct the usb request for gadget driver and inform it */
  2434. memset(&dev->setup_data, 0 , sizeof dev->setup_data);
  2435. dev->setup_data.bRequest = USB_REQ_SET_INTERFACE;
  2436. dev->setup_data.bRequestType = USB_RECIP_INTERFACE;
  2437. dev->setup_data.wValue = cpu_to_le16(dev->cfg_data.cur_alt);
  2438. dev->setup_data.wIndex = cpu_to_le16(dev->cfg_data.cur_intf);
  2439. /* programm the Endpoint Cfg registers */
  2440. /* Only one end point cfg register */
  2441. reg = pch_udc_read_csr(dev, UDC_EP0OUT_IDX);
  2442. reg = (reg & ~UDC_CSR_NE_INTF_MASK) |
  2443. (dev->cfg_data.cur_intf << UDC_CSR_NE_INTF_SHIFT);
  2444. reg = (reg & ~UDC_CSR_NE_ALT_MASK) |
  2445. (dev->cfg_data.cur_alt << UDC_CSR_NE_ALT_SHIFT);
  2446. pch_udc_write_csr(dev, reg, UDC_EP0OUT_IDX);
  2447. for (i = 0; i < PCH_UDC_USED_EP_NUM * 2; i++) {
  2448. /* clear stall bits */
  2449. pch_udc_ep_clear_stall(&(dev->ep[i]));
  2450. dev->ep[i].halted = 0;
  2451. }
  2452. dev->stall = 0;
  2453. spin_lock(&dev->lock);
  2454. ret = dev->driver->setup(&dev->gadget, &dev->setup_data);
  2455. spin_unlock(&dev->lock);
  2456. }
  2457. /**
  2458. * pch_udc_svc_cfg_interrupt() - This function handles a set configuration
  2459. * interrupt
  2460. * @dev: Reference to driver structure
  2461. */
  2462. static void pch_udc_svc_cfg_interrupt(struct pch_udc_dev *dev)
  2463. {
  2464. int i, ret;
  2465. u32 reg, dev_stat = 0;
  2466. dev_stat = pch_udc_read_device_status(dev);
  2467. dev->set_cfg_not_acked = 1;
  2468. dev->cfg_data.cur_cfg = (dev_stat & UDC_DEVSTS_CFG_MASK) >>
  2469. UDC_DEVSTS_CFG_SHIFT;
  2470. /* make usb request for gadget driver */
  2471. memset(&dev->setup_data, 0 , sizeof dev->setup_data);
  2472. dev->setup_data.bRequest = USB_REQ_SET_CONFIGURATION;
  2473. dev->setup_data.wValue = cpu_to_le16(dev->cfg_data.cur_cfg);
  2474. /* program the NE registers */
  2475. /* Only one end point cfg register */
  2476. reg = pch_udc_read_csr(dev, UDC_EP0OUT_IDX);
  2477. reg = (reg & ~UDC_CSR_NE_CFG_MASK) |
  2478. (dev->cfg_data.cur_cfg << UDC_CSR_NE_CFG_SHIFT);
  2479. pch_udc_write_csr(dev, reg, UDC_EP0OUT_IDX);
  2480. for (i = 0; i < PCH_UDC_USED_EP_NUM * 2; i++) {
  2481. /* clear stall bits */
  2482. pch_udc_ep_clear_stall(&(dev->ep[i]));
  2483. dev->ep[i].halted = 0;
  2484. }
  2485. dev->stall = 0;
  2486. /* call gadget zero with setup data received */
  2487. spin_lock(&dev->lock);
  2488. ret = dev->driver->setup(&dev->gadget, &dev->setup_data);
  2489. spin_unlock(&dev->lock);
  2490. }
  2491. /**
  2492. * pch_udc_dev_isr() - This function services device interrupts
  2493. * by invoking appropriate routines.
  2494. * @dev: Reference to the device structure
  2495. * @dev_intr: The Device interrupt status.
  2496. */
  2497. static void pch_udc_dev_isr(struct pch_udc_dev *dev, u32 dev_intr)
  2498. {
  2499. int vbus;
  2500. /* USB Reset Interrupt */
  2501. if (dev_intr & UDC_DEVINT_UR) {
  2502. pch_udc_svc_ur_interrupt(dev);
  2503. dev_dbg(&dev->pdev->dev, "USB_RESET\n");
  2504. }
  2505. /* Enumeration Done Interrupt */
  2506. if (dev_intr & UDC_DEVINT_ENUM) {
  2507. pch_udc_svc_enum_interrupt(dev);
  2508. dev_dbg(&dev->pdev->dev, "USB_ENUM\n");
  2509. }
  2510. /* Set Interface Interrupt */
  2511. if (dev_intr & UDC_DEVINT_SI)
  2512. pch_udc_svc_intf_interrupt(dev);
  2513. /* Set Config Interrupt */
  2514. if (dev_intr & UDC_DEVINT_SC)
  2515. pch_udc_svc_cfg_interrupt(dev);
  2516. /* USB Suspend interrupt */
  2517. if (dev_intr & UDC_DEVINT_US) {
  2518. if (dev->driver
  2519. && dev->driver->suspend) {
  2520. spin_unlock(&dev->lock);
  2521. dev->driver->suspend(&dev->gadget);
  2522. spin_lock(&dev->lock);
  2523. }
  2524. vbus = pch_vbus_gpio_get_value(dev);
  2525. if ((dev->vbus_session == 0)
  2526. && (vbus != 1)) {
  2527. if (dev->driver && dev->driver->disconnect) {
  2528. spin_unlock(&dev->lock);
  2529. dev->driver->disconnect(&dev->gadget);
  2530. spin_lock(&dev->lock);
  2531. }
  2532. pch_udc_reconnect(dev);
  2533. } else if ((dev->vbus_session == 0)
  2534. && (vbus == 1)
  2535. && !dev->vbus_gpio.intr)
  2536. schedule_work(&dev->vbus_gpio.irq_work_fall);
  2537. dev_dbg(&dev->pdev->dev, "USB_SUSPEND\n");
  2538. }
  2539. /* Clear the SOF interrupt, if enabled */
  2540. if (dev_intr & UDC_DEVINT_SOF)
  2541. dev_dbg(&dev->pdev->dev, "SOF\n");
  2542. /* ES interrupt, IDLE > 3ms on the USB */
  2543. if (dev_intr & UDC_DEVINT_ES)
  2544. dev_dbg(&dev->pdev->dev, "ES\n");
  2545. /* RWKP interrupt */
  2546. if (dev_intr & UDC_DEVINT_RWKP)
  2547. dev_dbg(&dev->pdev->dev, "RWKP\n");
  2548. }
  2549. /**
  2550. * pch_udc_isr() - This function handles interrupts from the PCH USB Device
  2551. * @irq: Interrupt request number
  2552. * @dev: Reference to the device structure
  2553. */
  2554. static irqreturn_t pch_udc_isr(int irq, void *pdev)
  2555. {
  2556. struct pch_udc_dev *dev = (struct pch_udc_dev *) pdev;
  2557. u32 dev_intr, ep_intr;
  2558. int i;
  2559. dev_intr = pch_udc_read_device_interrupts(dev);
  2560. ep_intr = pch_udc_read_ep_interrupts(dev);
  2561. /* For a hot plug, this find that the controller is hung up. */
  2562. if (dev_intr == ep_intr)
  2563. if (dev_intr == pch_udc_readl(dev, UDC_DEVCFG_ADDR)) {
  2564. dev_dbg(&dev->pdev->dev, "UDC: Hung up\n");
  2565. /* The controller is reset */
  2566. pch_udc_writel(dev, UDC_SRST, UDC_SRST_ADDR);
  2567. return IRQ_HANDLED;
  2568. }
  2569. if (dev_intr)
  2570. /* Clear device interrupts */
  2571. pch_udc_write_device_interrupts(dev, dev_intr);
  2572. if (ep_intr)
  2573. /* Clear ep interrupts */
  2574. pch_udc_write_ep_interrupts(dev, ep_intr);
  2575. if (!dev_intr && !ep_intr)
  2576. return IRQ_NONE;
  2577. spin_lock(&dev->lock);
  2578. if (dev_intr)
  2579. pch_udc_dev_isr(dev, dev_intr);
  2580. if (ep_intr) {
  2581. pch_udc_read_all_epstatus(dev, ep_intr);
  2582. /* Process Control In interrupts, if present */
  2583. if (ep_intr & UDC_EPINT_IN_EP0) {
  2584. pch_udc_svc_control_in(dev);
  2585. pch_udc_postsvc_epinters(dev, 0);
  2586. }
  2587. /* Process Control Out interrupts, if present */
  2588. if (ep_intr & UDC_EPINT_OUT_EP0)
  2589. pch_udc_svc_control_out(dev);
  2590. /* Process data in end point interrupts */
  2591. for (i = 1; i < PCH_UDC_USED_EP_NUM; i++) {
  2592. if (ep_intr & (1 << i)) {
  2593. pch_udc_svc_data_in(dev, i);
  2594. pch_udc_postsvc_epinters(dev, i);
  2595. }
  2596. }
  2597. /* Process data out end point interrupts */
  2598. for (i = UDC_EPINT_OUT_SHIFT + 1; i < (UDC_EPINT_OUT_SHIFT +
  2599. PCH_UDC_USED_EP_NUM); i++)
  2600. if (ep_intr & (1 << i))
  2601. pch_udc_svc_data_out(dev, i -
  2602. UDC_EPINT_OUT_SHIFT);
  2603. }
  2604. spin_unlock(&dev->lock);
  2605. return IRQ_HANDLED;
  2606. }
  2607. /**
  2608. * pch_udc_setup_ep0() - This function enables control endpoint for traffic
  2609. * @dev: Reference to the device structure
  2610. */
  2611. static void pch_udc_setup_ep0(struct pch_udc_dev *dev)
  2612. {
  2613. /* enable ep0 interrupts */
  2614. pch_udc_enable_ep_interrupts(dev, UDC_EPINT_IN_EP0 |
  2615. UDC_EPINT_OUT_EP0);
  2616. /* enable device interrupts */
  2617. pch_udc_enable_interrupts(dev, UDC_DEVINT_UR | UDC_DEVINT_US |
  2618. UDC_DEVINT_ES | UDC_DEVINT_ENUM |
  2619. UDC_DEVINT_SI | UDC_DEVINT_SC);
  2620. }
  2621. /**
  2622. * gadget_release() - Free the gadget driver private data
  2623. * @pdev reference to struct pci_dev
  2624. */
  2625. static void gadget_release(struct device *pdev)
  2626. {
  2627. struct pch_udc_dev *dev = dev_get_drvdata(pdev);
  2628. kfree(dev);
  2629. }
  2630. /**
  2631. * pch_udc_pcd_reinit() - This API initializes the endpoint structures
  2632. * @dev: Reference to the driver structure
  2633. */
  2634. static void pch_udc_pcd_reinit(struct pch_udc_dev *dev)
  2635. {
  2636. const char *const ep_string[] = {
  2637. ep0_string, "ep0out", "ep1in", "ep1out", "ep2in", "ep2out",
  2638. "ep3in", "ep3out", "ep4in", "ep4out", "ep5in", "ep5out",
  2639. "ep6in", "ep6out", "ep7in", "ep7out", "ep8in", "ep8out",
  2640. "ep9in", "ep9out", "ep10in", "ep10out", "ep11in", "ep11out",
  2641. "ep12in", "ep12out", "ep13in", "ep13out", "ep14in", "ep14out",
  2642. "ep15in", "ep15out",
  2643. };
  2644. int i;
  2645. dev->gadget.speed = USB_SPEED_UNKNOWN;
  2646. INIT_LIST_HEAD(&dev->gadget.ep_list);
  2647. /* Initialize the endpoints structures */
  2648. memset(dev->ep, 0, sizeof dev->ep);
  2649. for (i = 0; i < PCH_UDC_EP_NUM; i++) {
  2650. struct pch_udc_ep *ep = &dev->ep[i];
  2651. ep->dev = dev;
  2652. ep->halted = 1;
  2653. ep->num = i / 2;
  2654. ep->in = ~i & 1;
  2655. ep->ep.name = ep_string[i];
  2656. ep->ep.ops = &pch_udc_ep_ops;
  2657. if (ep->in) {
  2658. ep->offset_addr = ep->num * UDC_EP_REG_SHIFT;
  2659. ep->ep.caps.dir_in = true;
  2660. } else {
  2661. ep->offset_addr = (UDC_EPINT_OUT_SHIFT + ep->num) *
  2662. UDC_EP_REG_SHIFT;
  2663. ep->ep.caps.dir_out = true;
  2664. }
  2665. if (i == UDC_EP0IN_IDX || i == UDC_EP0OUT_IDX) {
  2666. ep->ep.caps.type_control = true;
  2667. } else {
  2668. ep->ep.caps.type_iso = true;
  2669. ep->ep.caps.type_bulk = true;
  2670. ep->ep.caps.type_int = true;
  2671. }
  2672. /* need to set ep->ep.maxpacket and set Default Configuration?*/
  2673. usb_ep_set_maxpacket_limit(&ep->ep, UDC_BULK_MAX_PKT_SIZE);
  2674. list_add_tail(&ep->ep.ep_list, &dev->gadget.ep_list);
  2675. INIT_LIST_HEAD(&ep->queue);
  2676. }
  2677. usb_ep_set_maxpacket_limit(&dev->ep[UDC_EP0IN_IDX].ep, UDC_EP0IN_MAX_PKT_SIZE);
  2678. usb_ep_set_maxpacket_limit(&dev->ep[UDC_EP0OUT_IDX].ep, UDC_EP0OUT_MAX_PKT_SIZE);
  2679. /* remove ep0 in and out from the list. They have own pointer */
  2680. list_del_init(&dev->ep[UDC_EP0IN_IDX].ep.ep_list);
  2681. list_del_init(&dev->ep[UDC_EP0OUT_IDX].ep.ep_list);
  2682. dev->gadget.ep0 = &dev->ep[UDC_EP0IN_IDX].ep;
  2683. INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
  2684. }
  2685. /**
  2686. * pch_udc_pcd_init() - This API initializes the driver structure
  2687. * @dev: Reference to the driver structure
  2688. *
  2689. * Return codes:
  2690. * 0: Success
  2691. */
  2692. static int pch_udc_pcd_init(struct pch_udc_dev *dev)
  2693. {
  2694. pch_udc_init(dev);
  2695. pch_udc_pcd_reinit(dev);
  2696. pch_vbus_gpio_init(dev, vbus_gpio_port);
  2697. return 0;
  2698. }
  2699. /**
  2700. * init_dma_pools() - create dma pools during initialization
  2701. * @pdev: reference to struct pci_dev
  2702. */
  2703. static int init_dma_pools(struct pch_udc_dev *dev)
  2704. {
  2705. struct pch_udc_stp_dma_desc *td_stp;
  2706. struct pch_udc_data_dma_desc *td_data;
  2707. /* DMA setup */
  2708. dev->data_requests = pci_pool_create("data_requests", dev->pdev,
  2709. sizeof(struct pch_udc_data_dma_desc), 0, 0);
  2710. if (!dev->data_requests) {
  2711. dev_err(&dev->pdev->dev, "%s: can't get request data pool\n",
  2712. __func__);
  2713. return -ENOMEM;
  2714. }
  2715. /* dma desc for setup data */
  2716. dev->stp_requests = pci_pool_create("setup requests", dev->pdev,
  2717. sizeof(struct pch_udc_stp_dma_desc), 0, 0);
  2718. if (!dev->stp_requests) {
  2719. dev_err(&dev->pdev->dev, "%s: can't get setup request pool\n",
  2720. __func__);
  2721. return -ENOMEM;
  2722. }
  2723. /* setup */
  2724. td_stp = pci_pool_alloc(dev->stp_requests, GFP_KERNEL,
  2725. &dev->ep[UDC_EP0OUT_IDX].td_stp_phys);
  2726. if (!td_stp) {
  2727. dev_err(&dev->pdev->dev,
  2728. "%s: can't allocate setup dma descriptor\n", __func__);
  2729. return -ENOMEM;
  2730. }
  2731. dev->ep[UDC_EP0OUT_IDX].td_stp = td_stp;
  2732. /* data: 0 packets !? */
  2733. td_data = pci_pool_alloc(dev->data_requests, GFP_KERNEL,
  2734. &dev->ep[UDC_EP0OUT_IDX].td_data_phys);
  2735. if (!td_data) {
  2736. dev_err(&dev->pdev->dev,
  2737. "%s: can't allocate data dma descriptor\n", __func__);
  2738. return -ENOMEM;
  2739. }
  2740. dev->ep[UDC_EP0OUT_IDX].td_data = td_data;
  2741. dev->ep[UDC_EP0IN_IDX].td_stp = NULL;
  2742. dev->ep[UDC_EP0IN_IDX].td_stp_phys = 0;
  2743. dev->ep[UDC_EP0IN_IDX].td_data = NULL;
  2744. dev->ep[UDC_EP0IN_IDX].td_data_phys = 0;
  2745. dev->ep0out_buf = kzalloc(UDC_EP0OUT_BUFF_SIZE * 4, GFP_KERNEL);
  2746. if (!dev->ep0out_buf)
  2747. return -ENOMEM;
  2748. dev->dma_addr = dma_map_single(&dev->pdev->dev, dev->ep0out_buf,
  2749. UDC_EP0OUT_BUFF_SIZE * 4,
  2750. DMA_FROM_DEVICE);
  2751. return 0;
  2752. }
  2753. static int pch_udc_start(struct usb_gadget *g,
  2754. struct usb_gadget_driver *driver)
  2755. {
  2756. struct pch_udc_dev *dev = to_pch_udc(g);
  2757. driver->driver.bus = NULL;
  2758. dev->driver = driver;
  2759. /* get ready for ep0 traffic */
  2760. pch_udc_setup_ep0(dev);
  2761. /* clear SD */
  2762. if ((pch_vbus_gpio_get_value(dev) != 0) || !dev->vbus_gpio.intr)
  2763. pch_udc_clear_disconnect(dev);
  2764. dev->connected = 1;
  2765. return 0;
  2766. }
  2767. static int pch_udc_stop(struct usb_gadget *g)
  2768. {
  2769. struct pch_udc_dev *dev = to_pch_udc(g);
  2770. pch_udc_disable_interrupts(dev, UDC_DEVINT_MSK);
  2771. /* Assures that there are no pending requests with this driver */
  2772. dev->driver = NULL;
  2773. dev->connected = 0;
  2774. /* set SD */
  2775. pch_udc_set_disconnect(dev);
  2776. return 0;
  2777. }
  2778. static void pch_udc_shutdown(struct pci_dev *pdev)
  2779. {
  2780. struct pch_udc_dev *dev = pci_get_drvdata(pdev);
  2781. pch_udc_disable_interrupts(dev, UDC_DEVINT_MSK);
  2782. pch_udc_disable_ep_interrupts(dev, UDC_EPINT_MSK_DISABLE_ALL);
  2783. /* disable the pullup so the host will think we're gone */
  2784. pch_udc_set_disconnect(dev);
  2785. }
  2786. static void pch_udc_remove(struct pci_dev *pdev)
  2787. {
  2788. struct pch_udc_dev *dev = pci_get_drvdata(pdev);
  2789. usb_del_gadget_udc(&dev->gadget);
  2790. /* gadget driver must not be registered */
  2791. if (dev->driver)
  2792. dev_err(&pdev->dev,
  2793. "%s: gadget driver still bound!!!\n", __func__);
  2794. /* dma pool cleanup */
  2795. if (dev->data_requests)
  2796. pci_pool_destroy(dev->data_requests);
  2797. if (dev->stp_requests) {
  2798. /* cleanup DMA desc's for ep0in */
  2799. if (dev->ep[UDC_EP0OUT_IDX].td_stp) {
  2800. pci_pool_free(dev->stp_requests,
  2801. dev->ep[UDC_EP0OUT_IDX].td_stp,
  2802. dev->ep[UDC_EP0OUT_IDX].td_stp_phys);
  2803. }
  2804. if (dev->ep[UDC_EP0OUT_IDX].td_data) {
  2805. pci_pool_free(dev->stp_requests,
  2806. dev->ep[UDC_EP0OUT_IDX].td_data,
  2807. dev->ep[UDC_EP0OUT_IDX].td_data_phys);
  2808. }
  2809. pci_pool_destroy(dev->stp_requests);
  2810. }
  2811. if (dev->dma_addr)
  2812. dma_unmap_single(&dev->pdev->dev, dev->dma_addr,
  2813. UDC_EP0OUT_BUFF_SIZE * 4, DMA_FROM_DEVICE);
  2814. kfree(dev->ep0out_buf);
  2815. pch_vbus_gpio_free(dev);
  2816. pch_udc_exit(dev);
  2817. if (dev->irq_registered)
  2818. free_irq(pdev->irq, dev);
  2819. if (dev->base_addr)
  2820. iounmap(dev->base_addr);
  2821. if (dev->mem_region)
  2822. release_mem_region(dev->phys_addr,
  2823. pci_resource_len(pdev, dev->bar));
  2824. if (dev->active)
  2825. pci_disable_device(pdev);
  2826. kfree(dev);
  2827. }
  2828. #ifdef CONFIG_PM
  2829. static int pch_udc_suspend(struct pci_dev *pdev, pm_message_t state)
  2830. {
  2831. struct pch_udc_dev *dev = pci_get_drvdata(pdev);
  2832. pch_udc_disable_interrupts(dev, UDC_DEVINT_MSK);
  2833. pch_udc_disable_ep_interrupts(dev, UDC_EPINT_MSK_DISABLE_ALL);
  2834. pci_disable_device(pdev);
  2835. pci_enable_wake(pdev, PCI_D3hot, 0);
  2836. if (pci_save_state(pdev)) {
  2837. dev_err(&pdev->dev,
  2838. "%s: could not save PCI config state\n", __func__);
  2839. return -ENOMEM;
  2840. }
  2841. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  2842. return 0;
  2843. }
  2844. static int pch_udc_resume(struct pci_dev *pdev)
  2845. {
  2846. int ret;
  2847. pci_set_power_state(pdev, PCI_D0);
  2848. pci_restore_state(pdev);
  2849. ret = pci_enable_device(pdev);
  2850. if (ret) {
  2851. dev_err(&pdev->dev, "%s: pci_enable_device failed\n", __func__);
  2852. return ret;
  2853. }
  2854. pci_enable_wake(pdev, PCI_D3hot, 0);
  2855. return 0;
  2856. }
  2857. #else
  2858. #define pch_udc_suspend NULL
  2859. #define pch_udc_resume NULL
  2860. #endif /* CONFIG_PM */
  2861. static int pch_udc_probe(struct pci_dev *pdev,
  2862. const struct pci_device_id *id)
  2863. {
  2864. unsigned long resource;
  2865. unsigned long len;
  2866. int retval;
  2867. struct pch_udc_dev *dev;
  2868. /* init */
  2869. dev = kzalloc(sizeof *dev, GFP_KERNEL);
  2870. if (!dev) {
  2871. pr_err("%s: no memory for device structure\n", __func__);
  2872. return -ENOMEM;
  2873. }
  2874. /* pci setup */
  2875. if (pci_enable_device(pdev) < 0) {
  2876. kfree(dev);
  2877. pr_err("%s: pci_enable_device failed\n", __func__);
  2878. return -ENODEV;
  2879. }
  2880. dev->active = 1;
  2881. pci_set_drvdata(pdev, dev);
  2882. /* Determine BAR based on PCI ID */
  2883. if (id->device == PCI_DEVICE_ID_INTEL_QUARK_X1000_UDC)
  2884. dev->bar = PCH_UDC_PCI_BAR_QUARK_X1000;
  2885. else
  2886. dev->bar = PCH_UDC_PCI_BAR;
  2887. /* PCI resource allocation */
  2888. resource = pci_resource_start(pdev, dev->bar);
  2889. len = pci_resource_len(pdev, dev->bar);
  2890. if (!request_mem_region(resource, len, KBUILD_MODNAME)) {
  2891. dev_err(&pdev->dev, "%s: pci device used already\n", __func__);
  2892. retval = -EBUSY;
  2893. goto finished;
  2894. }
  2895. dev->phys_addr = resource;
  2896. dev->mem_region = 1;
  2897. dev->base_addr = ioremap_nocache(resource, len);
  2898. if (!dev->base_addr) {
  2899. pr_err("%s: device memory cannot be mapped\n", __func__);
  2900. retval = -ENOMEM;
  2901. goto finished;
  2902. }
  2903. if (!pdev->irq) {
  2904. dev_err(&pdev->dev, "%s: irq not set\n", __func__);
  2905. retval = -ENODEV;
  2906. goto finished;
  2907. }
  2908. /* initialize the hardware */
  2909. if (pch_udc_pcd_init(dev)) {
  2910. retval = -ENODEV;
  2911. goto finished;
  2912. }
  2913. if (request_irq(pdev->irq, pch_udc_isr, IRQF_SHARED, KBUILD_MODNAME,
  2914. dev)) {
  2915. dev_err(&pdev->dev, "%s: request_irq(%d) fail\n", __func__,
  2916. pdev->irq);
  2917. retval = -ENODEV;
  2918. goto finished;
  2919. }
  2920. dev->irq = pdev->irq;
  2921. dev->irq_registered = 1;
  2922. pci_set_master(pdev);
  2923. pci_try_set_mwi(pdev);
  2924. /* device struct setup */
  2925. spin_lock_init(&dev->lock);
  2926. dev->pdev = pdev;
  2927. dev->gadget.ops = &pch_udc_ops;
  2928. retval = init_dma_pools(dev);
  2929. if (retval)
  2930. goto finished;
  2931. dev->gadget.name = KBUILD_MODNAME;
  2932. dev->gadget.max_speed = USB_SPEED_HIGH;
  2933. /* Put the device in disconnected state till a driver is bound */
  2934. pch_udc_set_disconnect(dev);
  2935. retval = usb_add_gadget_udc_release(&pdev->dev, &dev->gadget,
  2936. gadget_release);
  2937. if (retval)
  2938. goto finished;
  2939. return 0;
  2940. finished:
  2941. pch_udc_remove(pdev);
  2942. return retval;
  2943. }
  2944. static const struct pci_device_id pch_udc_pcidev_id[] = {
  2945. {
  2946. PCI_DEVICE(PCI_VENDOR_ID_INTEL,
  2947. PCI_DEVICE_ID_INTEL_QUARK_X1000_UDC),
  2948. .class = (PCI_CLASS_SERIAL_USB << 8) | 0xfe,
  2949. .class_mask = 0xffffffff,
  2950. },
  2951. {
  2952. PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_EG20T_UDC),
  2953. .class = (PCI_CLASS_SERIAL_USB << 8) | 0xfe,
  2954. .class_mask = 0xffffffff,
  2955. },
  2956. {
  2957. PCI_DEVICE(PCI_VENDOR_ID_ROHM, PCI_DEVICE_ID_ML7213_IOH_UDC),
  2958. .class = (PCI_CLASS_SERIAL_USB << 8) | 0xfe,
  2959. .class_mask = 0xffffffff,
  2960. },
  2961. {
  2962. PCI_DEVICE(PCI_VENDOR_ID_ROHM, PCI_DEVICE_ID_ML7831_IOH_UDC),
  2963. .class = (PCI_CLASS_SERIAL_USB << 8) | 0xfe,
  2964. .class_mask = 0xffffffff,
  2965. },
  2966. { 0 },
  2967. };
  2968. MODULE_DEVICE_TABLE(pci, pch_udc_pcidev_id);
  2969. static struct pci_driver pch_udc_driver = {
  2970. .name = KBUILD_MODNAME,
  2971. .id_table = pch_udc_pcidev_id,
  2972. .probe = pch_udc_probe,
  2973. .remove = pch_udc_remove,
  2974. .suspend = pch_udc_suspend,
  2975. .resume = pch_udc_resume,
  2976. .shutdown = pch_udc_shutdown,
  2977. };
  2978. module_pci_driver(pch_udc_driver);
  2979. MODULE_DESCRIPTION("Intel EG20T USB Device Controller");
  2980. MODULE_AUTHOR("LAPIS Semiconductor, <tomoya-linux@dsn.lapis-semi.com>");
  2981. MODULE_LICENSE("GPL");