pxa25x_udc.c 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Intel PXA25x and IXP4xx on-chip full speed USB device controllers
  4. *
  5. * Copyright (C) 2002 Intrinsyc, Inc. (Frank Becker)
  6. * Copyright (C) 2003 Robert Schwebel, Pengutronix
  7. * Copyright (C) 2003 Benedikt Spranger, Pengutronix
  8. * Copyright (C) 2003 David Brownell
  9. * Copyright (C) 2003 Joshua Wise
  10. */
  11. /* #define VERBOSE_DEBUG */
  12. #include <linux/device.h>
  13. #include <linux/gpio.h>
  14. #include <linux/module.h>
  15. #include <linux/kernel.h>
  16. #include <linux/ioport.h>
  17. #include <linux/types.h>
  18. #include <linux/errno.h>
  19. #include <linux/err.h>
  20. #include <linux/delay.h>
  21. #include <linux/slab.h>
  22. #include <linux/timer.h>
  23. #include <linux/list.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/mm.h>
  26. #include <linux/platform_data/pxa2xx_udc.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/dma-mapping.h>
  29. #include <linux/irq.h>
  30. #include <linux/clk.h>
  31. #include <linux/seq_file.h>
  32. #include <linux/debugfs.h>
  33. #include <linux/io.h>
  34. #include <linux/prefetch.h>
  35. #include <asm/byteorder.h>
  36. #include <asm/dma.h>
  37. #include <asm/mach-types.h>
  38. #include <asm/unaligned.h>
  39. #include <linux/usb/ch9.h>
  40. #include <linux/usb/gadget.h>
  41. #include <linux/usb/otg.h>
  42. #define UDCCR 0x0000 /* UDC Control Register */
  43. #define UDC_RES1 0x0004 /* UDC Undocumented - Reserved1 */
  44. #define UDC_RES2 0x0008 /* UDC Undocumented - Reserved2 */
  45. #define UDC_RES3 0x000C /* UDC Undocumented - Reserved3 */
  46. #define UDCCS0 0x0010 /* UDC Endpoint 0 Control/Status Register */
  47. #define UDCCS1 0x0014 /* UDC Endpoint 1 (IN) Control/Status Register */
  48. #define UDCCS2 0x0018 /* UDC Endpoint 2 (OUT) Control/Status Register */
  49. #define UDCCS3 0x001C /* UDC Endpoint 3 (IN) Control/Status Register */
  50. #define UDCCS4 0x0020 /* UDC Endpoint 4 (OUT) Control/Status Register */
  51. #define UDCCS5 0x0024 /* UDC Endpoint 5 (Interrupt) Control/Status Register */
  52. #define UDCCS6 0x0028 /* UDC Endpoint 6 (IN) Control/Status Register */
  53. #define UDCCS7 0x002C /* UDC Endpoint 7 (OUT) Control/Status Register */
  54. #define UDCCS8 0x0030 /* UDC Endpoint 8 (IN) Control/Status Register */
  55. #define UDCCS9 0x0034 /* UDC Endpoint 9 (OUT) Control/Status Register */
  56. #define UDCCS10 0x0038 /* UDC Endpoint 10 (Interrupt) Control/Status Register */
  57. #define UDCCS11 0x003C /* UDC Endpoint 11 (IN) Control/Status Register */
  58. #define UDCCS12 0x0040 /* UDC Endpoint 12 (OUT) Control/Status Register */
  59. #define UDCCS13 0x0044 /* UDC Endpoint 13 (IN) Control/Status Register */
  60. #define UDCCS14 0x0048 /* UDC Endpoint 14 (OUT) Control/Status Register */
  61. #define UDCCS15 0x004C /* UDC Endpoint 15 (Interrupt) Control/Status Register */
  62. #define UFNRH 0x0060 /* UDC Frame Number Register High */
  63. #define UFNRL 0x0064 /* UDC Frame Number Register Low */
  64. #define UBCR2 0x0068 /* UDC Byte Count Reg 2 */
  65. #define UBCR4 0x006c /* UDC Byte Count Reg 4 */
  66. #define UBCR7 0x0070 /* UDC Byte Count Reg 7 */
  67. #define UBCR9 0x0074 /* UDC Byte Count Reg 9 */
  68. #define UBCR12 0x0078 /* UDC Byte Count Reg 12 */
  69. #define UBCR14 0x007c /* UDC Byte Count Reg 14 */
  70. #define UDDR0 0x0080 /* UDC Endpoint 0 Data Register */
  71. #define UDDR1 0x0100 /* UDC Endpoint 1 Data Register */
  72. #define UDDR2 0x0180 /* UDC Endpoint 2 Data Register */
  73. #define UDDR3 0x0200 /* UDC Endpoint 3 Data Register */
  74. #define UDDR4 0x0400 /* UDC Endpoint 4 Data Register */
  75. #define UDDR5 0x00A0 /* UDC Endpoint 5 Data Register */
  76. #define UDDR6 0x0600 /* UDC Endpoint 6 Data Register */
  77. #define UDDR7 0x0680 /* UDC Endpoint 7 Data Register */
  78. #define UDDR8 0x0700 /* UDC Endpoint 8 Data Register */
  79. #define UDDR9 0x0900 /* UDC Endpoint 9 Data Register */
  80. #define UDDR10 0x00C0 /* UDC Endpoint 10 Data Register */
  81. #define UDDR11 0x0B00 /* UDC Endpoint 11 Data Register */
  82. #define UDDR12 0x0B80 /* UDC Endpoint 12 Data Register */
  83. #define UDDR13 0x0C00 /* UDC Endpoint 13 Data Register */
  84. #define UDDR14 0x0E00 /* UDC Endpoint 14 Data Register */
  85. #define UDDR15 0x00E0 /* UDC Endpoint 15 Data Register */
  86. #define UICR0 0x0050 /* UDC Interrupt Control Register 0 */
  87. #define UICR1 0x0054 /* UDC Interrupt Control Register 1 */
  88. #define USIR0 0x0058 /* UDC Status Interrupt Register 0 */
  89. #define USIR1 0x005C /* UDC Status Interrupt Register 1 */
  90. #define UDCCR_UDE (1 << 0) /* UDC enable */
  91. #define UDCCR_UDA (1 << 1) /* UDC active */
  92. #define UDCCR_RSM (1 << 2) /* Device resume */
  93. #define UDCCR_RESIR (1 << 3) /* Resume interrupt request */
  94. #define UDCCR_SUSIR (1 << 4) /* Suspend interrupt request */
  95. #define UDCCR_SRM (1 << 5) /* Suspend/resume interrupt mask */
  96. #define UDCCR_RSTIR (1 << 6) /* Reset interrupt request */
  97. #define UDCCR_REM (1 << 7) /* Reset interrupt mask */
  98. #define UDCCS0_OPR (1 << 0) /* OUT packet ready */
  99. #define UDCCS0_IPR (1 << 1) /* IN packet ready */
  100. #define UDCCS0_FTF (1 << 2) /* Flush Tx FIFO */
  101. #define UDCCS0_DRWF (1 << 3) /* Device remote wakeup feature */
  102. #define UDCCS0_SST (1 << 4) /* Sent stall */
  103. #define UDCCS0_FST (1 << 5) /* Force stall */
  104. #define UDCCS0_RNE (1 << 6) /* Receive FIFO no empty */
  105. #define UDCCS0_SA (1 << 7) /* Setup active */
  106. #define UDCCS_BI_TFS (1 << 0) /* Transmit FIFO service */
  107. #define UDCCS_BI_TPC (1 << 1) /* Transmit packet complete */
  108. #define UDCCS_BI_FTF (1 << 2) /* Flush Tx FIFO */
  109. #define UDCCS_BI_TUR (1 << 3) /* Transmit FIFO underrun */
  110. #define UDCCS_BI_SST (1 << 4) /* Sent stall */
  111. #define UDCCS_BI_FST (1 << 5) /* Force stall */
  112. #define UDCCS_BI_TSP (1 << 7) /* Transmit short packet */
  113. #define UDCCS_BO_RFS (1 << 0) /* Receive FIFO service */
  114. #define UDCCS_BO_RPC (1 << 1) /* Receive packet complete */
  115. #define UDCCS_BO_DME (1 << 3) /* DMA enable */
  116. #define UDCCS_BO_SST (1 << 4) /* Sent stall */
  117. #define UDCCS_BO_FST (1 << 5) /* Force stall */
  118. #define UDCCS_BO_RNE (1 << 6) /* Receive FIFO not empty */
  119. #define UDCCS_BO_RSP (1 << 7) /* Receive short packet */
  120. #define UDCCS_II_TFS (1 << 0) /* Transmit FIFO service */
  121. #define UDCCS_II_TPC (1 << 1) /* Transmit packet complete */
  122. #define UDCCS_II_FTF (1 << 2) /* Flush Tx FIFO */
  123. #define UDCCS_II_TUR (1 << 3) /* Transmit FIFO underrun */
  124. #define UDCCS_II_TSP (1 << 7) /* Transmit short packet */
  125. #define UDCCS_IO_RFS (1 << 0) /* Receive FIFO service */
  126. #define UDCCS_IO_RPC (1 << 1) /* Receive packet complete */
  127. #ifdef CONFIG_ARCH_IXP4XX /* FIXME: is this right?, datasheed says '2' */
  128. #define UDCCS_IO_ROF (1 << 3) /* Receive overflow */
  129. #endif
  130. #ifdef CONFIG_ARCH_PXA
  131. #define UDCCS_IO_ROF (1 << 2) /* Receive overflow */
  132. #endif
  133. #define UDCCS_IO_DME (1 << 3) /* DMA enable */
  134. #define UDCCS_IO_RNE (1 << 6) /* Receive FIFO not empty */
  135. #define UDCCS_IO_RSP (1 << 7) /* Receive short packet */
  136. #define UDCCS_INT_TFS (1 << 0) /* Transmit FIFO service */
  137. #define UDCCS_INT_TPC (1 << 1) /* Transmit packet complete */
  138. #define UDCCS_INT_FTF (1 << 2) /* Flush Tx FIFO */
  139. #define UDCCS_INT_TUR (1 << 3) /* Transmit FIFO underrun */
  140. #define UDCCS_INT_SST (1 << 4) /* Sent stall */
  141. #define UDCCS_INT_FST (1 << 5) /* Force stall */
  142. #define UDCCS_INT_TSP (1 << 7) /* Transmit short packet */
  143. #define UICR0_IM0 (1 << 0) /* Interrupt mask ep 0 */
  144. #define UICR0_IM1 (1 << 1) /* Interrupt mask ep 1 */
  145. #define UICR0_IM2 (1 << 2) /* Interrupt mask ep 2 */
  146. #define UICR0_IM3 (1 << 3) /* Interrupt mask ep 3 */
  147. #define UICR0_IM4 (1 << 4) /* Interrupt mask ep 4 */
  148. #define UICR0_IM5 (1 << 5) /* Interrupt mask ep 5 */
  149. #define UICR0_IM6 (1 << 6) /* Interrupt mask ep 6 */
  150. #define UICR0_IM7 (1 << 7) /* Interrupt mask ep 7 */
  151. #define UICR1_IM8 (1 << 0) /* Interrupt mask ep 8 */
  152. #define UICR1_IM9 (1 << 1) /* Interrupt mask ep 9 */
  153. #define UICR1_IM10 (1 << 2) /* Interrupt mask ep 10 */
  154. #define UICR1_IM11 (1 << 3) /* Interrupt mask ep 11 */
  155. #define UICR1_IM12 (1 << 4) /* Interrupt mask ep 12 */
  156. #define UICR1_IM13 (1 << 5) /* Interrupt mask ep 13 */
  157. #define UICR1_IM14 (1 << 6) /* Interrupt mask ep 14 */
  158. #define UICR1_IM15 (1 << 7) /* Interrupt mask ep 15 */
  159. #define USIR0_IR0 (1 << 0) /* Interrupt request ep 0 */
  160. #define USIR0_IR1 (1 << 1) /* Interrupt request ep 1 */
  161. #define USIR0_IR2 (1 << 2) /* Interrupt request ep 2 */
  162. #define USIR0_IR3 (1 << 3) /* Interrupt request ep 3 */
  163. #define USIR0_IR4 (1 << 4) /* Interrupt request ep 4 */
  164. #define USIR0_IR5 (1 << 5) /* Interrupt request ep 5 */
  165. #define USIR0_IR6 (1 << 6) /* Interrupt request ep 6 */
  166. #define USIR0_IR7 (1 << 7) /* Interrupt request ep 7 */
  167. #define USIR1_IR8 (1 << 0) /* Interrupt request ep 8 */
  168. #define USIR1_IR9 (1 << 1) /* Interrupt request ep 9 */
  169. #define USIR1_IR10 (1 << 2) /* Interrupt request ep 10 */
  170. #define USIR1_IR11 (1 << 3) /* Interrupt request ep 11 */
  171. #define USIR1_IR12 (1 << 4) /* Interrupt request ep 12 */
  172. #define USIR1_IR13 (1 << 5) /* Interrupt request ep 13 */
  173. #define USIR1_IR14 (1 << 6) /* Interrupt request ep 14 */
  174. #define USIR1_IR15 (1 << 7) /* Interrupt request ep 15 */
  175. /*
  176. * This driver handles the USB Device Controller (UDC) in Intel's PXA 25x
  177. * series processors. The UDC for the IXP 4xx series is very similar.
  178. * There are fifteen endpoints, in addition to ep0.
  179. *
  180. * Such controller drivers work with a gadget driver. The gadget driver
  181. * returns descriptors, implements configuration and data protocols used
  182. * by the host to interact with this device, and allocates endpoints to
  183. * the different protocol interfaces. The controller driver virtualizes
  184. * usb hardware so that the gadget drivers will be more portable.
  185. *
  186. * This UDC hardware wants to implement a bit too much USB protocol, so
  187. * it constrains the sorts of USB configuration change events that work.
  188. * The errata for these chips are misleading; some "fixed" bugs from
  189. * pxa250 a0/a1 b0/b1/b2 sure act like they're still there.
  190. *
  191. * Note that the UDC hardware supports DMA (except on IXP) but that's
  192. * not used here. IN-DMA (to host) is simple enough, when the data is
  193. * suitably aligned (16 bytes) ... the network stack doesn't do that,
  194. * other software can. OUT-DMA is buggy in most chip versions, as well
  195. * as poorly designed (data toggle not automatic). So this driver won't
  196. * bother using DMA. (Mostly-working IN-DMA support was available in
  197. * kernels before 2.6.23, but was never enabled or well tested.)
  198. */
  199. #define DRIVER_VERSION "30-June-2007"
  200. #define DRIVER_DESC "PXA 25x USB Device Controller driver"
  201. static const char driver_name [] = "pxa25x_udc";
  202. static const char ep0name [] = "ep0";
  203. #ifdef CONFIG_ARCH_IXP4XX
  204. /* cpu-specific register addresses are compiled in to this code */
  205. #ifdef CONFIG_ARCH_PXA
  206. #error "Can't configure both IXP and PXA"
  207. #endif
  208. /* IXP doesn't yet support <linux/clk.h> */
  209. #define clk_get(dev,name) NULL
  210. #define clk_enable(clk) do { } while (0)
  211. #define clk_disable(clk) do { } while (0)
  212. #define clk_put(clk) do { } while (0)
  213. #endif
  214. #include "pxa25x_udc.h"
  215. #ifdef CONFIG_USB_PXA25X_SMALL
  216. #define SIZE_STR " (small)"
  217. #else
  218. #define SIZE_STR ""
  219. #endif
  220. /* ---------------------------------------------------------------------------
  221. * endpoint related parts of the api to the usb controller hardware,
  222. * used by gadget driver; and the inner talker-to-hardware core.
  223. * ---------------------------------------------------------------------------
  224. */
  225. static void pxa25x_ep_fifo_flush (struct usb_ep *ep);
  226. static void nuke (struct pxa25x_ep *, int status);
  227. /* one GPIO should control a D+ pullup, so host sees this device (or not) */
  228. static void pullup_off(void)
  229. {
  230. struct pxa2xx_udc_mach_info *mach = the_controller->mach;
  231. int off_level = mach->gpio_pullup_inverted;
  232. if (gpio_is_valid(mach->gpio_pullup))
  233. gpio_set_value(mach->gpio_pullup, off_level);
  234. else if (mach->udc_command)
  235. mach->udc_command(PXA2XX_UDC_CMD_DISCONNECT);
  236. }
  237. static void pullup_on(void)
  238. {
  239. struct pxa2xx_udc_mach_info *mach = the_controller->mach;
  240. int on_level = !mach->gpio_pullup_inverted;
  241. if (gpio_is_valid(mach->gpio_pullup))
  242. gpio_set_value(mach->gpio_pullup, on_level);
  243. else if (mach->udc_command)
  244. mach->udc_command(PXA2XX_UDC_CMD_CONNECT);
  245. }
  246. #if defined(CONFIG_CPU_BIG_ENDIAN)
  247. /*
  248. * IXP4xx has its buses wired up in a way that relies on never doing any
  249. * byte swaps, independent of whether it runs in big-endian or little-endian
  250. * mode, as explained by Krzysztof Hałasa.
  251. *
  252. * We only support pxa25x in little-endian mode, but it is very likely
  253. * that it works the same way.
  254. */
  255. static inline void udc_set_reg(struct pxa25x_udc *dev, u32 reg, u32 val)
  256. {
  257. iowrite32be(val, dev->regs + reg);
  258. }
  259. static inline u32 udc_get_reg(struct pxa25x_udc *dev, u32 reg)
  260. {
  261. return ioread32be(dev->regs + reg);
  262. }
  263. #else
  264. static inline void udc_set_reg(struct pxa25x_udc *dev, u32 reg, u32 val)
  265. {
  266. writel(val, dev->regs + reg);
  267. }
  268. static inline u32 udc_get_reg(struct pxa25x_udc *dev, u32 reg)
  269. {
  270. return readl(dev->regs + reg);
  271. }
  272. #endif
  273. static void pio_irq_enable(struct pxa25x_ep *ep)
  274. {
  275. u32 bEndpointAddress = ep->bEndpointAddress & 0xf;
  276. if (bEndpointAddress < 8)
  277. udc_set_reg(ep->dev, UICR0, udc_get_reg(ep->dev, UICR0) &
  278. ~(1 << bEndpointAddress));
  279. else {
  280. bEndpointAddress -= 8;
  281. udc_set_reg(ep->dev, UICR1, udc_get_reg(ep->dev, UICR1) &
  282. ~(1 << bEndpointAddress));
  283. }
  284. }
  285. static void pio_irq_disable(struct pxa25x_ep *ep)
  286. {
  287. u32 bEndpointAddress = ep->bEndpointAddress & 0xf;
  288. if (bEndpointAddress < 8)
  289. udc_set_reg(ep->dev, UICR0, udc_get_reg(ep->dev, UICR0) |
  290. (1 << bEndpointAddress));
  291. else {
  292. bEndpointAddress -= 8;
  293. udc_set_reg(ep->dev, UICR1, udc_get_reg(ep->dev, UICR1) |
  294. (1 << bEndpointAddress));
  295. }
  296. }
  297. /* The UDCCR reg contains mask and interrupt status bits,
  298. * so using '|=' isn't safe as it may ack an interrupt.
  299. */
  300. #define UDCCR_MASK_BITS (UDCCR_REM | UDCCR_SRM | UDCCR_UDE)
  301. static inline void udc_set_mask_UDCCR(struct pxa25x_udc *dev, int mask)
  302. {
  303. u32 udccr = udc_get_reg(dev, UDCCR);
  304. udc_set_reg(dev, (udccr & UDCCR_MASK_BITS) | (mask & UDCCR_MASK_BITS), UDCCR);
  305. }
  306. static inline void udc_clear_mask_UDCCR(struct pxa25x_udc *dev, int mask)
  307. {
  308. u32 udccr = udc_get_reg(dev, UDCCR);
  309. udc_set_reg(dev, (udccr & UDCCR_MASK_BITS) & ~(mask & UDCCR_MASK_BITS), UDCCR);
  310. }
  311. static inline void udc_ack_int_UDCCR(struct pxa25x_udc *dev, int mask)
  312. {
  313. /* udccr contains the bits we dont want to change */
  314. u32 udccr = udc_get_reg(dev, UDCCR) & UDCCR_MASK_BITS;
  315. udc_set_reg(dev, udccr | (mask & ~UDCCR_MASK_BITS), UDCCR);
  316. }
  317. static inline u32 udc_ep_get_UDCCS(struct pxa25x_ep *ep)
  318. {
  319. return udc_get_reg(ep->dev, ep->regoff_udccs);
  320. }
  321. static inline void udc_ep_set_UDCCS(struct pxa25x_ep *ep, u32 data)
  322. {
  323. udc_set_reg(ep->dev, data, ep->regoff_udccs);
  324. }
  325. static inline u32 udc_ep0_get_UDCCS(struct pxa25x_udc *dev)
  326. {
  327. return udc_get_reg(dev, UDCCS0);
  328. }
  329. static inline void udc_ep0_set_UDCCS(struct pxa25x_udc *dev, u32 data)
  330. {
  331. udc_set_reg(dev, data, UDCCS0);
  332. }
  333. static inline u32 udc_ep_get_UDDR(struct pxa25x_ep *ep)
  334. {
  335. return udc_get_reg(ep->dev, ep->regoff_uddr);
  336. }
  337. static inline void udc_ep_set_UDDR(struct pxa25x_ep *ep, u32 data)
  338. {
  339. udc_set_reg(ep->dev, data, ep->regoff_uddr);
  340. }
  341. static inline u32 udc_ep_get_UBCR(struct pxa25x_ep *ep)
  342. {
  343. return udc_get_reg(ep->dev, ep->regoff_ubcr);
  344. }
  345. /*
  346. * endpoint enable/disable
  347. *
  348. * we need to verify the descriptors used to enable endpoints. since pxa25x
  349. * endpoint configurations are fixed, and are pretty much always enabled,
  350. * there's not a lot to manage here.
  351. *
  352. * because pxa25x can't selectively initialize bulk (or interrupt) endpoints,
  353. * (resetting endpoint halt and toggle), SET_INTERFACE is unusable except
  354. * for a single interface (with only the default altsetting) and for gadget
  355. * drivers that don't halt endpoints (not reset by set_interface). that also
  356. * means that if you use ISO, you must violate the USB spec rule that all
  357. * iso endpoints must be in non-default altsettings.
  358. */
  359. static int pxa25x_ep_enable (struct usb_ep *_ep,
  360. const struct usb_endpoint_descriptor *desc)
  361. {
  362. struct pxa25x_ep *ep;
  363. struct pxa25x_udc *dev;
  364. ep = container_of (_ep, struct pxa25x_ep, ep);
  365. if (!_ep || !desc || _ep->name == ep0name
  366. || desc->bDescriptorType != USB_DT_ENDPOINT
  367. || ep->bEndpointAddress != desc->bEndpointAddress
  368. || ep->fifo_size < usb_endpoint_maxp (desc)) {
  369. DMSG("%s, bad ep or descriptor\n", __func__);
  370. return -EINVAL;
  371. }
  372. /* xfer types must match, except that interrupt ~= bulk */
  373. if (ep->bmAttributes != desc->bmAttributes
  374. && ep->bmAttributes != USB_ENDPOINT_XFER_BULK
  375. && desc->bmAttributes != USB_ENDPOINT_XFER_INT) {
  376. DMSG("%s, %s type mismatch\n", __func__, _ep->name);
  377. return -EINVAL;
  378. }
  379. /* hardware _could_ do smaller, but driver doesn't */
  380. if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK
  381. && usb_endpoint_maxp (desc)
  382. != BULK_FIFO_SIZE)
  383. || !desc->wMaxPacketSize) {
  384. DMSG("%s, bad %s maxpacket\n", __func__, _ep->name);
  385. return -ERANGE;
  386. }
  387. dev = ep->dev;
  388. if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
  389. DMSG("%s, bogus device state\n", __func__);
  390. return -ESHUTDOWN;
  391. }
  392. ep->ep.desc = desc;
  393. ep->stopped = 0;
  394. ep->pio_irqs = 0;
  395. ep->ep.maxpacket = usb_endpoint_maxp (desc);
  396. /* flush fifo (mostly for OUT buffers) */
  397. pxa25x_ep_fifo_flush (_ep);
  398. /* ... reset halt state too, if we could ... */
  399. DBG(DBG_VERBOSE, "enabled %s\n", _ep->name);
  400. return 0;
  401. }
  402. static int pxa25x_ep_disable (struct usb_ep *_ep)
  403. {
  404. struct pxa25x_ep *ep;
  405. unsigned long flags;
  406. ep = container_of (_ep, struct pxa25x_ep, ep);
  407. if (!_ep || !ep->ep.desc) {
  408. DMSG("%s, %s not enabled\n", __func__,
  409. _ep ? ep->ep.name : NULL);
  410. return -EINVAL;
  411. }
  412. local_irq_save(flags);
  413. nuke (ep, -ESHUTDOWN);
  414. /* flush fifo (mostly for IN buffers) */
  415. pxa25x_ep_fifo_flush (_ep);
  416. ep->ep.desc = NULL;
  417. ep->stopped = 1;
  418. local_irq_restore(flags);
  419. DBG(DBG_VERBOSE, "%s disabled\n", _ep->name);
  420. return 0;
  421. }
  422. /*-------------------------------------------------------------------------*/
  423. /* for the pxa25x, these can just wrap kmalloc/kfree. gadget drivers
  424. * must still pass correctly initialized endpoints, since other controller
  425. * drivers may care about how it's currently set up (dma issues etc).
  426. */
  427. /*
  428. * pxa25x_ep_alloc_request - allocate a request data structure
  429. */
  430. static struct usb_request *
  431. pxa25x_ep_alloc_request (struct usb_ep *_ep, gfp_t gfp_flags)
  432. {
  433. struct pxa25x_request *req;
  434. req = kzalloc(sizeof(*req), gfp_flags);
  435. if (!req)
  436. return NULL;
  437. INIT_LIST_HEAD (&req->queue);
  438. return &req->req;
  439. }
  440. /*
  441. * pxa25x_ep_free_request - deallocate a request data structure
  442. */
  443. static void
  444. pxa25x_ep_free_request (struct usb_ep *_ep, struct usb_request *_req)
  445. {
  446. struct pxa25x_request *req;
  447. req = container_of (_req, struct pxa25x_request, req);
  448. WARN_ON(!list_empty (&req->queue));
  449. kfree(req);
  450. }
  451. /*-------------------------------------------------------------------------*/
  452. /*
  453. * done - retire a request; caller blocked irqs
  454. */
  455. static void done(struct pxa25x_ep *ep, struct pxa25x_request *req, int status)
  456. {
  457. unsigned stopped = ep->stopped;
  458. list_del_init(&req->queue);
  459. if (likely (req->req.status == -EINPROGRESS))
  460. req->req.status = status;
  461. else
  462. status = req->req.status;
  463. if (status && status != -ESHUTDOWN)
  464. DBG(DBG_VERBOSE, "complete %s req %p stat %d len %u/%u\n",
  465. ep->ep.name, &req->req, status,
  466. req->req.actual, req->req.length);
  467. /* don't modify queue heads during completion callback */
  468. ep->stopped = 1;
  469. usb_gadget_giveback_request(&ep->ep, &req->req);
  470. ep->stopped = stopped;
  471. }
  472. static inline void ep0_idle (struct pxa25x_udc *dev)
  473. {
  474. dev->ep0state = EP0_IDLE;
  475. }
  476. static int
  477. write_packet(struct pxa25x_ep *ep, struct pxa25x_request *req, unsigned max)
  478. {
  479. u8 *buf;
  480. unsigned length, count;
  481. buf = req->req.buf + req->req.actual;
  482. prefetch(buf);
  483. /* how big will this packet be? */
  484. length = min(req->req.length - req->req.actual, max);
  485. req->req.actual += length;
  486. count = length;
  487. while (likely(count--))
  488. udc_ep_set_UDDR(ep, *buf++);
  489. return length;
  490. }
  491. /*
  492. * write to an IN endpoint fifo, as many packets as possible.
  493. * irqs will use this to write the rest later.
  494. * caller guarantees at least one packet buffer is ready (or a zlp).
  495. */
  496. static int
  497. write_fifo (struct pxa25x_ep *ep, struct pxa25x_request *req)
  498. {
  499. unsigned max;
  500. max = usb_endpoint_maxp(ep->ep.desc);
  501. do {
  502. unsigned count;
  503. int is_last, is_short;
  504. count = write_packet(ep, req, max);
  505. /* last packet is usually short (or a zlp) */
  506. if (unlikely (count != max))
  507. is_last = is_short = 1;
  508. else {
  509. if (likely(req->req.length != req->req.actual)
  510. || req->req.zero)
  511. is_last = 0;
  512. else
  513. is_last = 1;
  514. /* interrupt/iso maxpacket may not fill the fifo */
  515. is_short = unlikely (max < ep->fifo_size);
  516. }
  517. DBG(DBG_VERY_NOISY, "wrote %s %d bytes%s%s %d left %p\n",
  518. ep->ep.name, count,
  519. is_last ? "/L" : "", is_short ? "/S" : "",
  520. req->req.length - req->req.actual, req);
  521. /* let loose that packet. maybe try writing another one,
  522. * double buffering might work. TSP, TPC, and TFS
  523. * bit values are the same for all normal IN endpoints.
  524. */
  525. udc_ep_set_UDCCS(ep, UDCCS_BI_TPC);
  526. if (is_short)
  527. udc_ep_set_UDCCS(ep, UDCCS_BI_TSP);
  528. /* requests complete when all IN data is in the FIFO */
  529. if (is_last) {
  530. done (ep, req, 0);
  531. if (list_empty(&ep->queue))
  532. pio_irq_disable(ep);
  533. return 1;
  534. }
  535. // TODO experiment: how robust can fifo mode tweaking be?
  536. // double buffering is off in the default fifo mode, which
  537. // prevents TFS from being set here.
  538. } while (udc_ep_get_UDCCS(ep) & UDCCS_BI_TFS);
  539. return 0;
  540. }
  541. /* caller asserts req->pending (ep0 irq status nyet cleared); starts
  542. * ep0 data stage. these chips want very simple state transitions.
  543. */
  544. static inline
  545. void ep0start(struct pxa25x_udc *dev, u32 flags, const char *tag)
  546. {
  547. udc_ep0_set_UDCCS(dev, flags|UDCCS0_SA|UDCCS0_OPR);
  548. udc_set_reg(dev, USIR0, USIR0_IR0);
  549. dev->req_pending = 0;
  550. DBG(DBG_VERY_NOISY, "%s %s, %02x/%02x\n",
  551. __func__, tag, udc_ep0_get_UDCCS(dev), flags);
  552. }
  553. static int
  554. write_ep0_fifo (struct pxa25x_ep *ep, struct pxa25x_request *req)
  555. {
  556. struct pxa25x_udc *dev = ep->dev;
  557. unsigned count;
  558. int is_short;
  559. count = write_packet(&dev->ep[0], req, EP0_FIFO_SIZE);
  560. ep->dev->stats.write.bytes += count;
  561. /* last packet "must be" short (or a zlp) */
  562. is_short = (count != EP0_FIFO_SIZE);
  563. DBG(DBG_VERY_NOISY, "ep0in %d bytes %d left %p\n", count,
  564. req->req.length - req->req.actual, req);
  565. if (unlikely (is_short)) {
  566. if (ep->dev->req_pending)
  567. ep0start(ep->dev, UDCCS0_IPR, "short IN");
  568. else
  569. udc_ep0_set_UDCCS(dev, UDCCS0_IPR);
  570. count = req->req.length;
  571. done (ep, req, 0);
  572. ep0_idle(ep->dev);
  573. #ifndef CONFIG_ARCH_IXP4XX
  574. #if 1
  575. /* This seems to get rid of lost status irqs in some cases:
  576. * host responds quickly, or next request involves config
  577. * change automagic, or should have been hidden, or ...
  578. *
  579. * FIXME get rid of all udelays possible...
  580. */
  581. if (count >= EP0_FIFO_SIZE) {
  582. count = 100;
  583. do {
  584. if ((udc_ep0_get_UDCCS(dev) & UDCCS0_OPR) != 0) {
  585. /* clear OPR, generate ack */
  586. udc_ep0_set_UDCCS(dev, UDCCS0_OPR);
  587. break;
  588. }
  589. count--;
  590. udelay(1);
  591. } while (count);
  592. }
  593. #endif
  594. #endif
  595. } else if (ep->dev->req_pending)
  596. ep0start(ep->dev, 0, "IN");
  597. return is_short;
  598. }
  599. /*
  600. * read_fifo - unload packet(s) from the fifo we use for usb OUT
  601. * transfers and put them into the request. caller should have made
  602. * sure there's at least one packet ready.
  603. *
  604. * returns true if the request completed because of short packet or the
  605. * request buffer having filled (and maybe overran till end-of-packet).
  606. */
  607. static int
  608. read_fifo (struct pxa25x_ep *ep, struct pxa25x_request *req)
  609. {
  610. for (;;) {
  611. u32 udccs;
  612. u8 *buf;
  613. unsigned bufferspace, count, is_short;
  614. /* make sure there's a packet in the FIFO.
  615. * UDCCS_{BO,IO}_RPC are all the same bit value.
  616. * UDCCS_{BO,IO}_RNE are all the same bit value.
  617. */
  618. udccs = udc_ep_get_UDCCS(ep);
  619. if (unlikely ((udccs & UDCCS_BO_RPC) == 0))
  620. break;
  621. buf = req->req.buf + req->req.actual;
  622. prefetchw(buf);
  623. bufferspace = req->req.length - req->req.actual;
  624. /* read all bytes from this packet */
  625. if (likely (udccs & UDCCS_BO_RNE)) {
  626. count = 1 + (0x0ff & udc_ep_get_UBCR(ep));
  627. req->req.actual += min (count, bufferspace);
  628. } else /* zlp */
  629. count = 0;
  630. is_short = (count < ep->ep.maxpacket);
  631. DBG(DBG_VERY_NOISY, "read %s %02x, %d bytes%s req %p %d/%d\n",
  632. ep->ep.name, udccs, count,
  633. is_short ? "/S" : "",
  634. req, req->req.actual, req->req.length);
  635. while (likely (count-- != 0)) {
  636. u8 byte = (u8) udc_ep_get_UDDR(ep);
  637. if (unlikely (bufferspace == 0)) {
  638. /* this happens when the driver's buffer
  639. * is smaller than what the host sent.
  640. * discard the extra data.
  641. */
  642. if (req->req.status != -EOVERFLOW)
  643. DMSG("%s overflow %d\n",
  644. ep->ep.name, count);
  645. req->req.status = -EOVERFLOW;
  646. } else {
  647. *buf++ = byte;
  648. bufferspace--;
  649. }
  650. }
  651. udc_ep_set_UDCCS(ep, UDCCS_BO_RPC);
  652. /* RPC/RSP/RNE could now reflect the other packet buffer */
  653. /* iso is one request per packet */
  654. if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
  655. if (udccs & UDCCS_IO_ROF)
  656. req->req.status = -EHOSTUNREACH;
  657. /* more like "is_done" */
  658. is_short = 1;
  659. }
  660. /* completion */
  661. if (is_short || req->req.actual == req->req.length) {
  662. done (ep, req, 0);
  663. if (list_empty(&ep->queue))
  664. pio_irq_disable(ep);
  665. return 1;
  666. }
  667. /* finished that packet. the next one may be waiting... */
  668. }
  669. return 0;
  670. }
  671. /*
  672. * special ep0 version of the above. no UBCR0 or double buffering; status
  673. * handshaking is magic. most device protocols don't need control-OUT.
  674. * CDC vendor commands (and RNDIS), mass storage CB/CBI, and some other
  675. * protocols do use them.
  676. */
  677. static int
  678. read_ep0_fifo (struct pxa25x_ep *ep, struct pxa25x_request *req)
  679. {
  680. u8 *buf, byte;
  681. unsigned bufferspace;
  682. buf = req->req.buf + req->req.actual;
  683. bufferspace = req->req.length - req->req.actual;
  684. while (udc_ep_get_UDCCS(ep) & UDCCS0_RNE) {
  685. byte = (u8) UDDR0;
  686. if (unlikely (bufferspace == 0)) {
  687. /* this happens when the driver's buffer
  688. * is smaller than what the host sent.
  689. * discard the extra data.
  690. */
  691. if (req->req.status != -EOVERFLOW)
  692. DMSG("%s overflow\n", ep->ep.name);
  693. req->req.status = -EOVERFLOW;
  694. } else {
  695. *buf++ = byte;
  696. req->req.actual++;
  697. bufferspace--;
  698. }
  699. }
  700. udc_ep_set_UDCCS(ep, UDCCS0_OPR | UDCCS0_IPR);
  701. /* completion */
  702. if (req->req.actual >= req->req.length)
  703. return 1;
  704. /* finished that packet. the next one may be waiting... */
  705. return 0;
  706. }
  707. /*-------------------------------------------------------------------------*/
  708. static int
  709. pxa25x_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
  710. {
  711. struct pxa25x_request *req;
  712. struct pxa25x_ep *ep;
  713. struct pxa25x_udc *dev;
  714. unsigned long flags;
  715. req = container_of(_req, struct pxa25x_request, req);
  716. if (unlikely (!_req || !_req->complete || !_req->buf
  717. || !list_empty(&req->queue))) {
  718. DMSG("%s, bad params\n", __func__);
  719. return -EINVAL;
  720. }
  721. ep = container_of(_ep, struct pxa25x_ep, ep);
  722. if (unlikely(!_ep || (!ep->ep.desc && ep->ep.name != ep0name))) {
  723. DMSG("%s, bad ep\n", __func__);
  724. return -EINVAL;
  725. }
  726. dev = ep->dev;
  727. if (unlikely (!dev->driver
  728. || dev->gadget.speed == USB_SPEED_UNKNOWN)) {
  729. DMSG("%s, bogus device state\n", __func__);
  730. return -ESHUTDOWN;
  731. }
  732. /* iso is always one packet per request, that's the only way
  733. * we can report per-packet status. that also helps with dma.
  734. */
  735. if (unlikely (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
  736. && req->req.length > usb_endpoint_maxp(ep->ep.desc)))
  737. return -EMSGSIZE;
  738. DBG(DBG_NOISY, "%s queue req %p, len %d buf %p\n",
  739. _ep->name, _req, _req->length, _req->buf);
  740. local_irq_save(flags);
  741. _req->status = -EINPROGRESS;
  742. _req->actual = 0;
  743. /* kickstart this i/o queue? */
  744. if (list_empty(&ep->queue) && !ep->stopped) {
  745. if (ep->ep.desc == NULL/* ep0 */) {
  746. unsigned length = _req->length;
  747. switch (dev->ep0state) {
  748. case EP0_IN_DATA_PHASE:
  749. dev->stats.write.ops++;
  750. if (write_ep0_fifo(ep, req))
  751. req = NULL;
  752. break;
  753. case EP0_OUT_DATA_PHASE:
  754. dev->stats.read.ops++;
  755. /* messy ... */
  756. if (dev->req_config) {
  757. DBG(DBG_VERBOSE, "ep0 config ack%s\n",
  758. dev->has_cfr ? "" : " raced");
  759. if (dev->has_cfr)
  760. udc_set_reg(dev, UDCCFR, UDCCFR_AREN |
  761. UDCCFR_ACM | UDCCFR_MB1);
  762. done(ep, req, 0);
  763. dev->ep0state = EP0_END_XFER;
  764. local_irq_restore (flags);
  765. return 0;
  766. }
  767. if (dev->req_pending)
  768. ep0start(dev, UDCCS0_IPR, "OUT");
  769. if (length == 0 || ((udc_ep0_get_UDCCS(dev) & UDCCS0_RNE) != 0
  770. && read_ep0_fifo(ep, req))) {
  771. ep0_idle(dev);
  772. done(ep, req, 0);
  773. req = NULL;
  774. }
  775. break;
  776. default:
  777. DMSG("ep0 i/o, odd state %d\n", dev->ep0state);
  778. local_irq_restore (flags);
  779. return -EL2HLT;
  780. }
  781. /* can the FIFO can satisfy the request immediately? */
  782. } else if ((ep->bEndpointAddress & USB_DIR_IN) != 0) {
  783. if ((udc_ep_get_UDCCS(ep) & UDCCS_BI_TFS) != 0
  784. && write_fifo(ep, req))
  785. req = NULL;
  786. } else if ((udc_ep_get_UDCCS(ep) & UDCCS_BO_RFS) != 0
  787. && read_fifo(ep, req)) {
  788. req = NULL;
  789. }
  790. if (likely(req && ep->ep.desc))
  791. pio_irq_enable(ep);
  792. }
  793. /* pio or dma irq handler advances the queue. */
  794. if (likely(req != NULL))
  795. list_add_tail(&req->queue, &ep->queue);
  796. local_irq_restore(flags);
  797. return 0;
  798. }
  799. /*
  800. * nuke - dequeue ALL requests
  801. */
  802. static void nuke(struct pxa25x_ep *ep, int status)
  803. {
  804. struct pxa25x_request *req;
  805. /* called with irqs blocked */
  806. while (!list_empty(&ep->queue)) {
  807. req = list_entry(ep->queue.next,
  808. struct pxa25x_request,
  809. queue);
  810. done(ep, req, status);
  811. }
  812. if (ep->ep.desc)
  813. pio_irq_disable(ep);
  814. }
  815. /* dequeue JUST ONE request */
  816. static int pxa25x_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
  817. {
  818. struct pxa25x_ep *ep;
  819. struct pxa25x_request *req = NULL;
  820. struct pxa25x_request *iter;
  821. unsigned long flags;
  822. ep = container_of(_ep, struct pxa25x_ep, ep);
  823. if (!_ep || ep->ep.name == ep0name)
  824. return -EINVAL;
  825. local_irq_save(flags);
  826. /* make sure it's actually queued on this endpoint */
  827. list_for_each_entry(iter, &ep->queue, queue) {
  828. if (&iter->req != _req)
  829. continue;
  830. req = iter;
  831. break;
  832. }
  833. if (!req) {
  834. local_irq_restore(flags);
  835. return -EINVAL;
  836. }
  837. done(ep, req, -ECONNRESET);
  838. local_irq_restore(flags);
  839. return 0;
  840. }
  841. /*-------------------------------------------------------------------------*/
  842. static int pxa25x_ep_set_halt(struct usb_ep *_ep, int value)
  843. {
  844. struct pxa25x_ep *ep;
  845. unsigned long flags;
  846. ep = container_of(_ep, struct pxa25x_ep, ep);
  847. if (unlikely (!_ep
  848. || (!ep->ep.desc && ep->ep.name != ep0name))
  849. || ep->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
  850. DMSG("%s, bad ep\n", __func__);
  851. return -EINVAL;
  852. }
  853. if (value == 0) {
  854. /* this path (reset toggle+halt) is needed to implement
  855. * SET_INTERFACE on normal hardware. but it can't be
  856. * done from software on the PXA UDC, and the hardware
  857. * forgets to do it as part of SET_INTERFACE automagic.
  858. */
  859. DMSG("only host can clear %s halt\n", _ep->name);
  860. return -EROFS;
  861. }
  862. local_irq_save(flags);
  863. if ((ep->bEndpointAddress & USB_DIR_IN) != 0
  864. && ((udc_ep_get_UDCCS(ep) & UDCCS_BI_TFS) == 0
  865. || !list_empty(&ep->queue))) {
  866. local_irq_restore(flags);
  867. return -EAGAIN;
  868. }
  869. /* FST bit is the same for control, bulk in, bulk out, interrupt in */
  870. udc_ep_set_UDCCS(ep, UDCCS_BI_FST|UDCCS_BI_FTF);
  871. /* ep0 needs special care */
  872. if (!ep->ep.desc) {
  873. start_watchdog(ep->dev);
  874. ep->dev->req_pending = 0;
  875. ep->dev->ep0state = EP0_STALL;
  876. /* and bulk/intr endpoints like dropping stalls too */
  877. } else {
  878. unsigned i;
  879. for (i = 0; i < 1000; i += 20) {
  880. if (udc_ep_get_UDCCS(ep) & UDCCS_BI_SST)
  881. break;
  882. udelay(20);
  883. }
  884. }
  885. local_irq_restore(flags);
  886. DBG(DBG_VERBOSE, "%s halt\n", _ep->name);
  887. return 0;
  888. }
  889. static int pxa25x_ep_fifo_status(struct usb_ep *_ep)
  890. {
  891. struct pxa25x_ep *ep;
  892. ep = container_of(_ep, struct pxa25x_ep, ep);
  893. if (!_ep) {
  894. DMSG("%s, bad ep\n", __func__);
  895. return -ENODEV;
  896. }
  897. /* pxa can't report unclaimed bytes from IN fifos */
  898. if ((ep->bEndpointAddress & USB_DIR_IN) != 0)
  899. return -EOPNOTSUPP;
  900. if (ep->dev->gadget.speed == USB_SPEED_UNKNOWN
  901. || (udc_ep_get_UDCCS(ep) & UDCCS_BO_RFS) == 0)
  902. return 0;
  903. else
  904. return (udc_ep_get_UBCR(ep) & 0xfff) + 1;
  905. }
  906. static void pxa25x_ep_fifo_flush(struct usb_ep *_ep)
  907. {
  908. struct pxa25x_ep *ep;
  909. ep = container_of(_ep, struct pxa25x_ep, ep);
  910. if (!_ep || ep->ep.name == ep0name || !list_empty(&ep->queue)) {
  911. DMSG("%s, bad ep\n", __func__);
  912. return;
  913. }
  914. /* toggle and halt bits stay unchanged */
  915. /* for OUT, just read and discard the FIFO contents. */
  916. if ((ep->bEndpointAddress & USB_DIR_IN) == 0) {
  917. while (((udc_ep_get_UDCCS(ep)) & UDCCS_BO_RNE) != 0)
  918. (void)udc_ep_get_UDDR(ep);
  919. return;
  920. }
  921. /* most IN status is the same, but ISO can't stall */
  922. udc_ep_set_UDCCS(ep, UDCCS_BI_TPC|UDCCS_BI_FTF|UDCCS_BI_TUR
  923. | (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
  924. ? 0 : UDCCS_BI_SST));
  925. }
  926. static const struct usb_ep_ops pxa25x_ep_ops = {
  927. .enable = pxa25x_ep_enable,
  928. .disable = pxa25x_ep_disable,
  929. .alloc_request = pxa25x_ep_alloc_request,
  930. .free_request = pxa25x_ep_free_request,
  931. .queue = pxa25x_ep_queue,
  932. .dequeue = pxa25x_ep_dequeue,
  933. .set_halt = pxa25x_ep_set_halt,
  934. .fifo_status = pxa25x_ep_fifo_status,
  935. .fifo_flush = pxa25x_ep_fifo_flush,
  936. };
  937. /* ---------------------------------------------------------------------------
  938. * device-scoped parts of the api to the usb controller hardware
  939. * ---------------------------------------------------------------------------
  940. */
  941. static int pxa25x_udc_get_frame(struct usb_gadget *_gadget)
  942. {
  943. struct pxa25x_udc *dev;
  944. dev = container_of(_gadget, struct pxa25x_udc, gadget);
  945. return ((udc_get_reg(dev, UFNRH) & 0x07) << 8) |
  946. (udc_get_reg(dev, UFNRL) & 0xff);
  947. }
  948. static int pxa25x_udc_wakeup(struct usb_gadget *_gadget)
  949. {
  950. struct pxa25x_udc *udc;
  951. udc = container_of(_gadget, struct pxa25x_udc, gadget);
  952. /* host may not have enabled remote wakeup */
  953. if ((udc_ep0_get_UDCCS(udc) & UDCCS0_DRWF) == 0)
  954. return -EHOSTUNREACH;
  955. udc_set_mask_UDCCR(udc, UDCCR_RSM);
  956. return 0;
  957. }
  958. static void stop_activity(struct pxa25x_udc *, struct usb_gadget_driver *);
  959. static void udc_enable (struct pxa25x_udc *);
  960. static void udc_disable(struct pxa25x_udc *);
  961. /* We disable the UDC -- and its 48 MHz clock -- whenever it's not
  962. * in active use.
  963. */
  964. static int pullup(struct pxa25x_udc *udc)
  965. {
  966. int is_active = udc->vbus && udc->pullup && !udc->suspended;
  967. DMSG("%s\n", is_active ? "active" : "inactive");
  968. if (is_active) {
  969. if (!udc->active) {
  970. udc->active = 1;
  971. /* Enable clock for USB device */
  972. clk_enable(udc->clk);
  973. udc_enable(udc);
  974. }
  975. } else {
  976. if (udc->active) {
  977. if (udc->gadget.speed != USB_SPEED_UNKNOWN) {
  978. DMSG("disconnect %s\n", udc->driver
  979. ? udc->driver->driver.name
  980. : "(no driver)");
  981. stop_activity(udc, udc->driver);
  982. }
  983. udc_disable(udc);
  984. /* Disable clock for USB device */
  985. clk_disable(udc->clk);
  986. udc->active = 0;
  987. }
  988. }
  989. return 0;
  990. }
  991. /* VBUS reporting logically comes from a transceiver */
  992. static int pxa25x_udc_vbus_session(struct usb_gadget *_gadget, int is_active)
  993. {
  994. struct pxa25x_udc *udc;
  995. udc = container_of(_gadget, struct pxa25x_udc, gadget);
  996. udc->vbus = is_active;
  997. DMSG("vbus %s\n", is_active ? "supplied" : "inactive");
  998. pullup(udc);
  999. return 0;
  1000. }
  1001. /* drivers may have software control over D+ pullup */
  1002. static int pxa25x_udc_pullup(struct usb_gadget *_gadget, int is_active)
  1003. {
  1004. struct pxa25x_udc *udc;
  1005. udc = container_of(_gadget, struct pxa25x_udc, gadget);
  1006. /* not all boards support pullup control */
  1007. if (!gpio_is_valid(udc->mach->gpio_pullup) && !udc->mach->udc_command)
  1008. return -EOPNOTSUPP;
  1009. udc->pullup = (is_active != 0);
  1010. pullup(udc);
  1011. return 0;
  1012. }
  1013. /* boards may consume current from VBUS, up to 100-500mA based on config.
  1014. * the 500uA suspend ceiling means that exclusively vbus-powered PXA designs
  1015. * violate USB specs.
  1016. */
  1017. static int pxa25x_udc_vbus_draw(struct usb_gadget *_gadget, unsigned mA)
  1018. {
  1019. struct pxa25x_udc *udc;
  1020. udc = container_of(_gadget, struct pxa25x_udc, gadget);
  1021. if (!IS_ERR_OR_NULL(udc->transceiver))
  1022. return usb_phy_set_power(udc->transceiver, mA);
  1023. return -EOPNOTSUPP;
  1024. }
  1025. static int pxa25x_udc_start(struct usb_gadget *g,
  1026. struct usb_gadget_driver *driver);
  1027. static int pxa25x_udc_stop(struct usb_gadget *g);
  1028. static const struct usb_gadget_ops pxa25x_udc_ops = {
  1029. .get_frame = pxa25x_udc_get_frame,
  1030. .wakeup = pxa25x_udc_wakeup,
  1031. .vbus_session = pxa25x_udc_vbus_session,
  1032. .pullup = pxa25x_udc_pullup,
  1033. .vbus_draw = pxa25x_udc_vbus_draw,
  1034. .udc_start = pxa25x_udc_start,
  1035. .udc_stop = pxa25x_udc_stop,
  1036. };
  1037. /*-------------------------------------------------------------------------*/
  1038. #ifdef CONFIG_USB_GADGET_DEBUG_FS
  1039. static int udc_debug_show(struct seq_file *m, void *_d)
  1040. {
  1041. struct pxa25x_udc *dev = m->private;
  1042. unsigned long flags;
  1043. int i;
  1044. u32 tmp;
  1045. local_irq_save(flags);
  1046. /* basic device status */
  1047. seq_printf(m, DRIVER_DESC "\n"
  1048. "%s version: %s\nGadget driver: %s\nHost %s\n\n",
  1049. driver_name, DRIVER_VERSION SIZE_STR "(pio)",
  1050. dev->driver ? dev->driver->driver.name : "(none)",
  1051. dev->gadget.speed == USB_SPEED_FULL ? "full speed" : "disconnected");
  1052. /* registers for device and ep0 */
  1053. seq_printf(m,
  1054. "uicr %02X.%02X, usir %02X.%02x, ufnr %02X.%02X\n",
  1055. udc_get_reg(dev, UICR1), udc_get_reg(dev, UICR0),
  1056. udc_get_reg(dev, USIR1), udc_get_reg(dev, USIR0),
  1057. udc_get_reg(dev, UFNRH), udc_get_reg(dev, UFNRL));
  1058. tmp = udc_get_reg(dev, UDCCR);
  1059. seq_printf(m,
  1060. "udccr %02X =%s%s%s%s%s%s%s%s\n", tmp,
  1061. (tmp & UDCCR_REM) ? " rem" : "",
  1062. (tmp & UDCCR_RSTIR) ? " rstir" : "",
  1063. (tmp & UDCCR_SRM) ? " srm" : "",
  1064. (tmp & UDCCR_SUSIR) ? " susir" : "",
  1065. (tmp & UDCCR_RESIR) ? " resir" : "",
  1066. (tmp & UDCCR_RSM) ? " rsm" : "",
  1067. (tmp & UDCCR_UDA) ? " uda" : "",
  1068. (tmp & UDCCR_UDE) ? " ude" : "");
  1069. tmp = udc_ep0_get_UDCCS(dev);
  1070. seq_printf(m,
  1071. "udccs0 %02X =%s%s%s%s%s%s%s%s\n", tmp,
  1072. (tmp & UDCCS0_SA) ? " sa" : "",
  1073. (tmp & UDCCS0_RNE) ? " rne" : "",
  1074. (tmp & UDCCS0_FST) ? " fst" : "",
  1075. (tmp & UDCCS0_SST) ? " sst" : "",
  1076. (tmp & UDCCS0_DRWF) ? " dwrf" : "",
  1077. (tmp & UDCCS0_FTF) ? " ftf" : "",
  1078. (tmp & UDCCS0_IPR) ? " ipr" : "",
  1079. (tmp & UDCCS0_OPR) ? " opr" : "");
  1080. if (dev->has_cfr) {
  1081. tmp = udc_get_reg(dev, UDCCFR);
  1082. seq_printf(m,
  1083. "udccfr %02X =%s%s\n", tmp,
  1084. (tmp & UDCCFR_AREN) ? " aren" : "",
  1085. (tmp & UDCCFR_ACM) ? " acm" : "");
  1086. }
  1087. if (dev->gadget.speed != USB_SPEED_FULL || !dev->driver)
  1088. goto done;
  1089. seq_printf(m, "ep0 IN %lu/%lu, OUT %lu/%lu\nirqs %lu\n\n",
  1090. dev->stats.write.bytes, dev->stats.write.ops,
  1091. dev->stats.read.bytes, dev->stats.read.ops,
  1092. dev->stats.irqs);
  1093. /* dump endpoint queues */
  1094. for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
  1095. struct pxa25x_ep *ep = &dev->ep [i];
  1096. struct pxa25x_request *req;
  1097. if (i != 0) {
  1098. const struct usb_endpoint_descriptor *desc;
  1099. desc = ep->ep.desc;
  1100. if (!desc)
  1101. continue;
  1102. tmp = udc_ep_get_UDCCS(&dev->ep[i]);
  1103. seq_printf(m,
  1104. "%s max %d %s udccs %02x irqs %lu\n",
  1105. ep->ep.name, usb_endpoint_maxp(desc),
  1106. "pio", tmp, ep->pio_irqs);
  1107. /* TODO translate all five groups of udccs bits! */
  1108. } else /* ep0 should only have one transfer queued */
  1109. seq_printf(m, "ep0 max 16 pio irqs %lu\n",
  1110. ep->pio_irqs);
  1111. if (list_empty(&ep->queue)) {
  1112. seq_printf(m, "\t(nothing queued)\n");
  1113. continue;
  1114. }
  1115. list_for_each_entry(req, &ep->queue, queue) {
  1116. seq_printf(m,
  1117. "\treq %p len %d/%d buf %p\n",
  1118. &req->req, req->req.actual,
  1119. req->req.length, req->req.buf);
  1120. }
  1121. }
  1122. done:
  1123. local_irq_restore(flags);
  1124. return 0;
  1125. }
  1126. DEFINE_SHOW_ATTRIBUTE(udc_debug);
  1127. #define create_debug_files(dev) \
  1128. do { \
  1129. debugfs_create_file(dev->gadget.name, \
  1130. S_IRUGO, NULL, dev, &udc_debug_fops); \
  1131. } while (0)
  1132. #define remove_debug_files(dev) debugfs_lookup_and_remove(dev->gadget.name, NULL)
  1133. #else /* !CONFIG_USB_GADGET_DEBUG_FILES */
  1134. #define create_debug_files(dev) do {} while (0)
  1135. #define remove_debug_files(dev) do {} while (0)
  1136. #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
  1137. /*-------------------------------------------------------------------------*/
  1138. /*
  1139. * udc_disable - disable USB device controller
  1140. */
  1141. static void udc_disable(struct pxa25x_udc *dev)
  1142. {
  1143. /* block all irqs */
  1144. udc_set_mask_UDCCR(dev, UDCCR_SRM|UDCCR_REM);
  1145. udc_set_reg(dev, UICR0, 0xff);
  1146. udc_set_reg(dev, UICR1, 0xff);
  1147. udc_set_reg(dev, UFNRH, UFNRH_SIM);
  1148. /* if hardware supports it, disconnect from usb */
  1149. pullup_off();
  1150. udc_clear_mask_UDCCR(dev, UDCCR_UDE);
  1151. ep0_idle (dev);
  1152. dev->gadget.speed = USB_SPEED_UNKNOWN;
  1153. }
  1154. /*
  1155. * udc_reinit - initialize software state
  1156. */
  1157. static void udc_reinit(struct pxa25x_udc *dev)
  1158. {
  1159. u32 i;
  1160. /* device/ep0 records init */
  1161. INIT_LIST_HEAD (&dev->gadget.ep_list);
  1162. INIT_LIST_HEAD (&dev->gadget.ep0->ep_list);
  1163. dev->ep0state = EP0_IDLE;
  1164. dev->gadget.quirk_altset_not_supp = 1;
  1165. /* basic endpoint records init */
  1166. for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
  1167. struct pxa25x_ep *ep = &dev->ep[i];
  1168. if (i != 0)
  1169. list_add_tail (&ep->ep.ep_list, &dev->gadget.ep_list);
  1170. ep->ep.desc = NULL;
  1171. ep->stopped = 0;
  1172. INIT_LIST_HEAD (&ep->queue);
  1173. ep->pio_irqs = 0;
  1174. usb_ep_set_maxpacket_limit(&ep->ep, ep->ep.maxpacket);
  1175. }
  1176. /* the rest was statically initialized, and is read-only */
  1177. }
  1178. /* until it's enabled, this UDC should be completely invisible
  1179. * to any USB host.
  1180. */
  1181. static void udc_enable (struct pxa25x_udc *dev)
  1182. {
  1183. udc_clear_mask_UDCCR(dev, UDCCR_UDE);
  1184. /* try to clear these bits before we enable the udc */
  1185. udc_ack_int_UDCCR(dev, UDCCR_SUSIR|/*UDCCR_RSTIR|*/UDCCR_RESIR);
  1186. ep0_idle(dev);
  1187. dev->gadget.speed = USB_SPEED_UNKNOWN;
  1188. dev->stats.irqs = 0;
  1189. /*
  1190. * sequence taken from chapter 12.5.10, PXA250 AppProcDevManual:
  1191. * - enable UDC
  1192. * - if RESET is already in progress, ack interrupt
  1193. * - unmask reset interrupt
  1194. */
  1195. udc_set_mask_UDCCR(dev, UDCCR_UDE);
  1196. if (!(udc_get_reg(dev, UDCCR) & UDCCR_UDA))
  1197. udc_ack_int_UDCCR(dev, UDCCR_RSTIR);
  1198. if (dev->has_cfr /* UDC_RES2 is defined */) {
  1199. /* pxa255 (a0+) can avoid a set_config race that could
  1200. * prevent gadget drivers from configuring correctly
  1201. */
  1202. udc_set_reg(dev, UDCCFR, UDCCFR_ACM | UDCCFR_MB1);
  1203. } else {
  1204. /* "USB test mode" for pxa250 errata 40-42 (stepping a0, a1)
  1205. * which could result in missing packets and interrupts.
  1206. * supposedly one bit per endpoint, controlling whether it
  1207. * double buffers or not; ACM/AREN bits fit into the holes.
  1208. * zero bits (like USIR0_IRx) disable double buffering.
  1209. */
  1210. udc_set_reg(dev, UDC_RES1, 0x00);
  1211. udc_set_reg(dev, UDC_RES2, 0x00);
  1212. }
  1213. /* enable suspend/resume and reset irqs */
  1214. udc_clear_mask_UDCCR(dev, UDCCR_SRM | UDCCR_REM);
  1215. /* enable ep0 irqs */
  1216. udc_set_reg(dev, UICR0, udc_get_reg(dev, UICR0) & ~UICR0_IM0);
  1217. /* if hardware supports it, pullup D+ and wait for reset */
  1218. pullup_on();
  1219. }
  1220. /* when a driver is successfully registered, it will receive
  1221. * control requests including set_configuration(), which enables
  1222. * non-control requests. then usb traffic follows until a
  1223. * disconnect is reported. then a host may connect again, or
  1224. * the driver might get unbound.
  1225. */
  1226. static int pxa25x_udc_start(struct usb_gadget *g,
  1227. struct usb_gadget_driver *driver)
  1228. {
  1229. struct pxa25x_udc *dev = to_pxa25x(g);
  1230. int retval;
  1231. /* first hook up the driver ... */
  1232. dev->driver = driver;
  1233. dev->pullup = 1;
  1234. /* ... then enable host detection and ep0; and we're ready
  1235. * for set_configuration as well as eventual disconnect.
  1236. */
  1237. /* connect to bus through transceiver */
  1238. if (!IS_ERR_OR_NULL(dev->transceiver)) {
  1239. retval = otg_set_peripheral(dev->transceiver->otg,
  1240. &dev->gadget);
  1241. if (retval)
  1242. goto bind_fail;
  1243. }
  1244. dump_state(dev);
  1245. return 0;
  1246. bind_fail:
  1247. return retval;
  1248. }
  1249. static void
  1250. reset_gadget(struct pxa25x_udc *dev, struct usb_gadget_driver *driver)
  1251. {
  1252. int i;
  1253. /* don't disconnect drivers more than once */
  1254. if (dev->gadget.speed == USB_SPEED_UNKNOWN)
  1255. driver = NULL;
  1256. dev->gadget.speed = USB_SPEED_UNKNOWN;
  1257. /* prevent new request submissions, kill any outstanding requests */
  1258. for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
  1259. struct pxa25x_ep *ep = &dev->ep[i];
  1260. ep->stopped = 1;
  1261. nuke(ep, -ESHUTDOWN);
  1262. }
  1263. del_timer_sync(&dev->timer);
  1264. /* report reset; the driver is already quiesced */
  1265. if (driver)
  1266. usb_gadget_udc_reset(&dev->gadget, driver);
  1267. /* re-init driver-visible data structures */
  1268. udc_reinit(dev);
  1269. }
  1270. static void
  1271. stop_activity(struct pxa25x_udc *dev, struct usb_gadget_driver *driver)
  1272. {
  1273. int i;
  1274. /* don't disconnect drivers more than once */
  1275. if (dev->gadget.speed == USB_SPEED_UNKNOWN)
  1276. driver = NULL;
  1277. dev->gadget.speed = USB_SPEED_UNKNOWN;
  1278. /* prevent new request submissions, kill any outstanding requests */
  1279. for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
  1280. struct pxa25x_ep *ep = &dev->ep[i];
  1281. ep->stopped = 1;
  1282. nuke(ep, -ESHUTDOWN);
  1283. }
  1284. del_timer_sync(&dev->timer);
  1285. /* report disconnect; the driver is already quiesced */
  1286. if (driver)
  1287. driver->disconnect(&dev->gadget);
  1288. /* re-init driver-visible data structures */
  1289. udc_reinit(dev);
  1290. }
  1291. static int pxa25x_udc_stop(struct usb_gadget*g)
  1292. {
  1293. struct pxa25x_udc *dev = to_pxa25x(g);
  1294. local_irq_disable();
  1295. dev->pullup = 0;
  1296. stop_activity(dev, NULL);
  1297. local_irq_enable();
  1298. if (!IS_ERR_OR_NULL(dev->transceiver))
  1299. (void) otg_set_peripheral(dev->transceiver->otg, NULL);
  1300. dev->driver = NULL;
  1301. dump_state(dev);
  1302. return 0;
  1303. }
  1304. /*-------------------------------------------------------------------------*/
  1305. #ifdef CONFIG_ARCH_LUBBOCK
  1306. /* Lubbock has separate connect and disconnect irqs. More typical designs
  1307. * use one GPIO as the VBUS IRQ, and another to control the D+ pullup.
  1308. */
  1309. static irqreturn_t
  1310. lubbock_vbus_irq(int irq, void *_dev)
  1311. {
  1312. struct pxa25x_udc *dev = _dev;
  1313. int vbus;
  1314. dev->stats.irqs++;
  1315. if (irq == dev->usb_irq) {
  1316. vbus = 1;
  1317. disable_irq(dev->usb_irq);
  1318. enable_irq(dev->usb_disc_irq);
  1319. } else if (irq == dev->usb_disc_irq) {
  1320. vbus = 0;
  1321. disable_irq(dev->usb_disc_irq);
  1322. enable_irq(dev->usb_irq);
  1323. } else {
  1324. return IRQ_NONE;
  1325. }
  1326. pxa25x_udc_vbus_session(&dev->gadget, vbus);
  1327. return IRQ_HANDLED;
  1328. }
  1329. #endif
  1330. /*-------------------------------------------------------------------------*/
  1331. static inline void clear_ep_state (struct pxa25x_udc *dev)
  1332. {
  1333. unsigned i;
  1334. /* hardware SET_{CONFIGURATION,INTERFACE} automagic resets endpoint
  1335. * fifos, and pending transactions mustn't be continued in any case.
  1336. */
  1337. for (i = 1; i < PXA_UDC_NUM_ENDPOINTS; i++)
  1338. nuke(&dev->ep[i], -ECONNABORTED);
  1339. }
  1340. static void udc_watchdog(struct timer_list *t)
  1341. {
  1342. struct pxa25x_udc *dev = from_timer(dev, t, timer);
  1343. local_irq_disable();
  1344. if (dev->ep0state == EP0_STALL
  1345. && (udc_ep0_get_UDCCS(dev) & UDCCS0_FST) == 0
  1346. && (udc_ep0_get_UDCCS(dev) & UDCCS0_SST) == 0) {
  1347. udc_ep0_set_UDCCS(dev, UDCCS0_FST|UDCCS0_FTF);
  1348. DBG(DBG_VERBOSE, "ep0 re-stall\n");
  1349. start_watchdog(dev);
  1350. }
  1351. local_irq_enable();
  1352. }
  1353. static void handle_ep0 (struct pxa25x_udc *dev)
  1354. {
  1355. u32 udccs0 = udc_ep0_get_UDCCS(dev);
  1356. struct pxa25x_ep *ep = &dev->ep [0];
  1357. struct pxa25x_request *req;
  1358. union {
  1359. struct usb_ctrlrequest r;
  1360. u8 raw [8];
  1361. u32 word [2];
  1362. } u;
  1363. if (list_empty(&ep->queue))
  1364. req = NULL;
  1365. else
  1366. req = list_entry(ep->queue.next, struct pxa25x_request, queue);
  1367. /* clear stall status */
  1368. if (udccs0 & UDCCS0_SST) {
  1369. nuke(ep, -EPIPE);
  1370. udc_ep0_set_UDCCS(dev, UDCCS0_SST);
  1371. del_timer(&dev->timer);
  1372. ep0_idle(dev);
  1373. }
  1374. /* previous request unfinished? non-error iff back-to-back ... */
  1375. if ((udccs0 & UDCCS0_SA) != 0 && dev->ep0state != EP0_IDLE) {
  1376. nuke(ep, 0);
  1377. del_timer(&dev->timer);
  1378. ep0_idle(dev);
  1379. }
  1380. switch (dev->ep0state) {
  1381. case EP0_IDLE:
  1382. /* late-breaking status? */
  1383. udccs0 = udc_ep0_get_UDCCS(dev);
  1384. /* start control request? */
  1385. if (likely((udccs0 & (UDCCS0_OPR|UDCCS0_SA|UDCCS0_RNE))
  1386. == (UDCCS0_OPR|UDCCS0_SA|UDCCS0_RNE))) {
  1387. int i;
  1388. nuke (ep, -EPROTO);
  1389. /* read SETUP packet */
  1390. for (i = 0; i < 8; i++) {
  1391. if (unlikely(!(udc_ep0_get_UDCCS(dev) & UDCCS0_RNE))) {
  1392. bad_setup:
  1393. DMSG("SETUP %d!\n", i);
  1394. goto stall;
  1395. }
  1396. u.raw [i] = (u8) UDDR0;
  1397. }
  1398. if (unlikely((udc_ep0_get_UDCCS(dev) & UDCCS0_RNE) != 0))
  1399. goto bad_setup;
  1400. got_setup:
  1401. DBG(DBG_VERBOSE, "SETUP %02x.%02x v%04x i%04x l%04x\n",
  1402. u.r.bRequestType, u.r.bRequest,
  1403. le16_to_cpu(u.r.wValue),
  1404. le16_to_cpu(u.r.wIndex),
  1405. le16_to_cpu(u.r.wLength));
  1406. /* cope with automagic for some standard requests. */
  1407. dev->req_std = (u.r.bRequestType & USB_TYPE_MASK)
  1408. == USB_TYPE_STANDARD;
  1409. dev->req_config = 0;
  1410. dev->req_pending = 1;
  1411. switch (u.r.bRequest) {
  1412. /* hardware restricts gadget drivers here! */
  1413. case USB_REQ_SET_CONFIGURATION:
  1414. if (u.r.bRequestType == USB_RECIP_DEVICE) {
  1415. /* reflect hardware's automagic
  1416. * up to the gadget driver.
  1417. */
  1418. config_change:
  1419. dev->req_config = 1;
  1420. clear_ep_state(dev);
  1421. /* if !has_cfr, there's no synch
  1422. * else use AREN (later) not SA|OPR
  1423. * USIR0_IR0 acts edge sensitive
  1424. */
  1425. }
  1426. break;
  1427. /* ... and here, even more ... */
  1428. case USB_REQ_SET_INTERFACE:
  1429. if (u.r.bRequestType == USB_RECIP_INTERFACE) {
  1430. /* udc hardware is broken by design:
  1431. * - altsetting may only be zero;
  1432. * - hw resets all interfaces' eps;
  1433. * - ep reset doesn't include halt(?).
  1434. */
  1435. DMSG("broken set_interface (%d/%d)\n",
  1436. le16_to_cpu(u.r.wIndex),
  1437. le16_to_cpu(u.r.wValue));
  1438. goto config_change;
  1439. }
  1440. break;
  1441. /* hardware was supposed to hide this */
  1442. case USB_REQ_SET_ADDRESS:
  1443. if (u.r.bRequestType == USB_RECIP_DEVICE) {
  1444. ep0start(dev, 0, "address");
  1445. return;
  1446. }
  1447. break;
  1448. }
  1449. if (u.r.bRequestType & USB_DIR_IN)
  1450. dev->ep0state = EP0_IN_DATA_PHASE;
  1451. else
  1452. dev->ep0state = EP0_OUT_DATA_PHASE;
  1453. i = dev->driver->setup(&dev->gadget, &u.r);
  1454. if (i < 0) {
  1455. /* hardware automagic preventing STALL... */
  1456. if (dev->req_config) {
  1457. /* hardware sometimes neglects to tell
  1458. * tell us about config change events,
  1459. * so later ones may fail...
  1460. */
  1461. WARNING("config change %02x fail %d?\n",
  1462. u.r.bRequest, i);
  1463. return;
  1464. /* TODO experiment: if has_cfr,
  1465. * hardware didn't ACK; maybe we
  1466. * could actually STALL!
  1467. */
  1468. }
  1469. DBG(DBG_VERBOSE, "protocol STALL, "
  1470. "%02x err %d\n", udc_ep0_get_UDCCS(dev), i);
  1471. stall:
  1472. /* the watchdog timer helps deal with cases
  1473. * where udc seems to clear FST wrongly, and
  1474. * then NAKs instead of STALLing.
  1475. */
  1476. ep0start(dev, UDCCS0_FST|UDCCS0_FTF, "stall");
  1477. start_watchdog(dev);
  1478. dev->ep0state = EP0_STALL;
  1479. /* deferred i/o == no response yet */
  1480. } else if (dev->req_pending) {
  1481. if (likely(dev->ep0state == EP0_IN_DATA_PHASE
  1482. || dev->req_std || u.r.wLength))
  1483. ep0start(dev, 0, "defer");
  1484. else
  1485. ep0start(dev, UDCCS0_IPR, "defer/IPR");
  1486. }
  1487. /* expect at least one data or status stage irq */
  1488. return;
  1489. } else if (likely((udccs0 & (UDCCS0_OPR|UDCCS0_SA))
  1490. == (UDCCS0_OPR|UDCCS0_SA))) {
  1491. unsigned i;
  1492. /* pxa210/250 erratum 131 for B0/B1 says RNE lies.
  1493. * still observed on a pxa255 a0.
  1494. */
  1495. DBG(DBG_VERBOSE, "e131\n");
  1496. nuke(ep, -EPROTO);
  1497. /* read SETUP data, but don't trust it too much */
  1498. for (i = 0; i < 8; i++)
  1499. u.raw [i] = (u8) UDDR0;
  1500. if ((u.r.bRequestType & USB_RECIP_MASK)
  1501. > USB_RECIP_OTHER)
  1502. goto stall;
  1503. if (u.word [0] == 0 && u.word [1] == 0)
  1504. goto stall;
  1505. goto got_setup;
  1506. } else {
  1507. /* some random early IRQ:
  1508. * - we acked FST
  1509. * - IPR cleared
  1510. * - OPR got set, without SA (likely status stage)
  1511. */
  1512. udc_ep0_set_UDCCS(dev, udccs0 & (UDCCS0_SA|UDCCS0_OPR));
  1513. }
  1514. break;
  1515. case EP0_IN_DATA_PHASE: /* GET_DESCRIPTOR etc */
  1516. if (udccs0 & UDCCS0_OPR) {
  1517. udc_ep0_set_UDCCS(dev, UDCCS0_OPR|UDCCS0_FTF);
  1518. DBG(DBG_VERBOSE, "ep0in premature status\n");
  1519. if (req)
  1520. done(ep, req, 0);
  1521. ep0_idle(dev);
  1522. } else /* irq was IPR clearing */ {
  1523. if (req) {
  1524. /* this IN packet might finish the request */
  1525. (void) write_ep0_fifo(ep, req);
  1526. } /* else IN token before response was written */
  1527. }
  1528. break;
  1529. case EP0_OUT_DATA_PHASE: /* SET_DESCRIPTOR etc */
  1530. if (udccs0 & UDCCS0_OPR) {
  1531. if (req) {
  1532. /* this OUT packet might finish the request */
  1533. if (read_ep0_fifo(ep, req))
  1534. done(ep, req, 0);
  1535. /* else more OUT packets expected */
  1536. } /* else OUT token before read was issued */
  1537. } else /* irq was IPR clearing */ {
  1538. DBG(DBG_VERBOSE, "ep0out premature status\n");
  1539. if (req)
  1540. done(ep, req, 0);
  1541. ep0_idle(dev);
  1542. }
  1543. break;
  1544. case EP0_END_XFER:
  1545. if (req)
  1546. done(ep, req, 0);
  1547. /* ack control-IN status (maybe in-zlp was skipped)
  1548. * also appears after some config change events.
  1549. */
  1550. if (udccs0 & UDCCS0_OPR)
  1551. udc_ep0_set_UDCCS(dev, UDCCS0_OPR);
  1552. ep0_idle(dev);
  1553. break;
  1554. case EP0_STALL:
  1555. udc_ep0_set_UDCCS(dev, UDCCS0_FST);
  1556. break;
  1557. }
  1558. udc_set_reg(dev, USIR0, USIR0_IR0);
  1559. }
  1560. static void handle_ep(struct pxa25x_ep *ep)
  1561. {
  1562. struct pxa25x_request *req;
  1563. int is_in = ep->bEndpointAddress & USB_DIR_IN;
  1564. int completed;
  1565. u32 udccs, tmp;
  1566. do {
  1567. completed = 0;
  1568. if (likely (!list_empty(&ep->queue)))
  1569. req = list_entry(ep->queue.next,
  1570. struct pxa25x_request, queue);
  1571. else
  1572. req = NULL;
  1573. // TODO check FST handling
  1574. udccs = udc_ep_get_UDCCS(ep);
  1575. if (unlikely(is_in)) { /* irq from TPC, SST, or (ISO) TUR */
  1576. tmp = UDCCS_BI_TUR;
  1577. if (likely(ep->bmAttributes == USB_ENDPOINT_XFER_BULK))
  1578. tmp |= UDCCS_BI_SST;
  1579. tmp &= udccs;
  1580. if (likely (tmp))
  1581. udc_ep_set_UDCCS(ep, tmp);
  1582. if (req && likely ((udccs & UDCCS_BI_TFS) != 0))
  1583. completed = write_fifo(ep, req);
  1584. } else { /* irq from RPC (or for ISO, ROF) */
  1585. if (likely(ep->bmAttributes == USB_ENDPOINT_XFER_BULK))
  1586. tmp = UDCCS_BO_SST | UDCCS_BO_DME;
  1587. else
  1588. tmp = UDCCS_IO_ROF | UDCCS_IO_DME;
  1589. tmp &= udccs;
  1590. if (likely(tmp))
  1591. udc_ep_set_UDCCS(ep, tmp);
  1592. /* fifos can hold packets, ready for reading... */
  1593. if (likely(req)) {
  1594. completed = read_fifo(ep, req);
  1595. } else
  1596. pio_irq_disable(ep);
  1597. }
  1598. ep->pio_irqs++;
  1599. } while (completed);
  1600. }
  1601. /*
  1602. * pxa25x_udc_irq - interrupt handler
  1603. *
  1604. * avoid delays in ep0 processing. the control handshaking isn't always
  1605. * under software control (pxa250c0 and the pxa255 are better), and delays
  1606. * could cause usb protocol errors.
  1607. */
  1608. static irqreturn_t
  1609. pxa25x_udc_irq(int irq, void *_dev)
  1610. {
  1611. struct pxa25x_udc *dev = _dev;
  1612. int handled;
  1613. dev->stats.irqs++;
  1614. do {
  1615. u32 udccr = udc_get_reg(dev, UDCCR);
  1616. handled = 0;
  1617. /* SUSpend Interrupt Request */
  1618. if (unlikely(udccr & UDCCR_SUSIR)) {
  1619. udc_ack_int_UDCCR(dev, UDCCR_SUSIR);
  1620. handled = 1;
  1621. DBG(DBG_VERBOSE, "USB suspend\n");
  1622. if (dev->gadget.speed != USB_SPEED_UNKNOWN
  1623. && dev->driver
  1624. && dev->driver->suspend)
  1625. dev->driver->suspend(&dev->gadget);
  1626. ep0_idle (dev);
  1627. }
  1628. /* RESume Interrupt Request */
  1629. if (unlikely(udccr & UDCCR_RESIR)) {
  1630. udc_ack_int_UDCCR(dev, UDCCR_RESIR);
  1631. handled = 1;
  1632. DBG(DBG_VERBOSE, "USB resume\n");
  1633. if (dev->gadget.speed != USB_SPEED_UNKNOWN
  1634. && dev->driver
  1635. && dev->driver->resume)
  1636. dev->driver->resume(&dev->gadget);
  1637. }
  1638. /* ReSeT Interrupt Request - USB reset */
  1639. if (unlikely(udccr & UDCCR_RSTIR)) {
  1640. udc_ack_int_UDCCR(dev, UDCCR_RSTIR);
  1641. handled = 1;
  1642. if ((udc_get_reg(dev, UDCCR) & UDCCR_UDA) == 0) {
  1643. DBG(DBG_VERBOSE, "USB reset start\n");
  1644. /* reset driver and endpoints,
  1645. * in case that's not yet done
  1646. */
  1647. reset_gadget(dev, dev->driver);
  1648. } else {
  1649. DBG(DBG_VERBOSE, "USB reset end\n");
  1650. dev->gadget.speed = USB_SPEED_FULL;
  1651. memset(&dev->stats, 0, sizeof dev->stats);
  1652. /* driver and endpoints are still reset */
  1653. }
  1654. } else {
  1655. u32 usir0 = udc_get_reg(dev, USIR0) &
  1656. ~udc_get_reg(dev, UICR0);
  1657. u32 usir1 = udc_get_reg(dev, USIR1) &
  1658. ~udc_get_reg(dev, UICR1);
  1659. int i;
  1660. if (unlikely (!usir0 && !usir1))
  1661. continue;
  1662. DBG(DBG_VERY_NOISY, "irq %02x.%02x\n", usir1, usir0);
  1663. /* control traffic */
  1664. if (usir0 & USIR0_IR0) {
  1665. dev->ep[0].pio_irqs++;
  1666. handle_ep0(dev);
  1667. handled = 1;
  1668. }
  1669. /* endpoint data transfers */
  1670. for (i = 0; i < 8; i++) {
  1671. u32 tmp = 1 << i;
  1672. if (i && (usir0 & tmp)) {
  1673. handle_ep(&dev->ep[i]);
  1674. udc_set_reg(dev, USIR0,
  1675. udc_get_reg(dev, USIR0) | tmp);
  1676. handled = 1;
  1677. }
  1678. #ifndef CONFIG_USB_PXA25X_SMALL
  1679. if (usir1 & tmp) {
  1680. handle_ep(&dev->ep[i+8]);
  1681. udc_set_reg(dev, USIR1,
  1682. udc_get_reg(dev, USIR1) | tmp);
  1683. handled = 1;
  1684. }
  1685. #endif
  1686. }
  1687. }
  1688. /* we could also ask for 1 msec SOF (SIR) interrupts */
  1689. } while (handled);
  1690. return IRQ_HANDLED;
  1691. }
  1692. /*-------------------------------------------------------------------------*/
  1693. static void nop_release (struct device *dev)
  1694. {
  1695. DMSG("%s %s\n", __func__, dev_name(dev));
  1696. }
  1697. /* this uses load-time allocation and initialization (instead of
  1698. * doing it at run-time) to save code, eliminate fault paths, and
  1699. * be more obviously correct.
  1700. */
  1701. static struct pxa25x_udc memory = {
  1702. .gadget = {
  1703. .ops = &pxa25x_udc_ops,
  1704. .ep0 = &memory.ep[0].ep,
  1705. .name = driver_name,
  1706. .dev = {
  1707. .init_name = "gadget",
  1708. .release = nop_release,
  1709. },
  1710. },
  1711. /* control endpoint */
  1712. .ep[0] = {
  1713. .ep = {
  1714. .name = ep0name,
  1715. .ops = &pxa25x_ep_ops,
  1716. .maxpacket = EP0_FIFO_SIZE,
  1717. .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_CONTROL,
  1718. USB_EP_CAPS_DIR_ALL),
  1719. },
  1720. .dev = &memory,
  1721. .regoff_udccs = UDCCS0,
  1722. .regoff_uddr = UDDR0,
  1723. },
  1724. /* first group of endpoints */
  1725. .ep[1] = {
  1726. .ep = {
  1727. .name = "ep1in-bulk",
  1728. .ops = &pxa25x_ep_ops,
  1729. .maxpacket = BULK_FIFO_SIZE,
  1730. .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK,
  1731. USB_EP_CAPS_DIR_IN),
  1732. },
  1733. .dev = &memory,
  1734. .fifo_size = BULK_FIFO_SIZE,
  1735. .bEndpointAddress = USB_DIR_IN | 1,
  1736. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1737. .regoff_udccs = UDCCS1,
  1738. .regoff_uddr = UDDR1,
  1739. },
  1740. .ep[2] = {
  1741. .ep = {
  1742. .name = "ep2out-bulk",
  1743. .ops = &pxa25x_ep_ops,
  1744. .maxpacket = BULK_FIFO_SIZE,
  1745. .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK,
  1746. USB_EP_CAPS_DIR_OUT),
  1747. },
  1748. .dev = &memory,
  1749. .fifo_size = BULK_FIFO_SIZE,
  1750. .bEndpointAddress = 2,
  1751. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1752. .regoff_udccs = UDCCS2,
  1753. .regoff_ubcr = UBCR2,
  1754. .regoff_uddr = UDDR2,
  1755. },
  1756. #ifndef CONFIG_USB_PXA25X_SMALL
  1757. .ep[3] = {
  1758. .ep = {
  1759. .name = "ep3in-iso",
  1760. .ops = &pxa25x_ep_ops,
  1761. .maxpacket = ISO_FIFO_SIZE,
  1762. .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO,
  1763. USB_EP_CAPS_DIR_IN),
  1764. },
  1765. .dev = &memory,
  1766. .fifo_size = ISO_FIFO_SIZE,
  1767. .bEndpointAddress = USB_DIR_IN | 3,
  1768. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  1769. .regoff_udccs = UDCCS3,
  1770. .regoff_uddr = UDDR3,
  1771. },
  1772. .ep[4] = {
  1773. .ep = {
  1774. .name = "ep4out-iso",
  1775. .ops = &pxa25x_ep_ops,
  1776. .maxpacket = ISO_FIFO_SIZE,
  1777. .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO,
  1778. USB_EP_CAPS_DIR_OUT),
  1779. },
  1780. .dev = &memory,
  1781. .fifo_size = ISO_FIFO_SIZE,
  1782. .bEndpointAddress = 4,
  1783. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  1784. .regoff_udccs = UDCCS4,
  1785. .regoff_ubcr = UBCR4,
  1786. .regoff_uddr = UDDR4,
  1787. },
  1788. .ep[5] = {
  1789. .ep = {
  1790. .name = "ep5in-int",
  1791. .ops = &pxa25x_ep_ops,
  1792. .maxpacket = INT_FIFO_SIZE,
  1793. .caps = USB_EP_CAPS(0, 0),
  1794. },
  1795. .dev = &memory,
  1796. .fifo_size = INT_FIFO_SIZE,
  1797. .bEndpointAddress = USB_DIR_IN | 5,
  1798. .bmAttributes = USB_ENDPOINT_XFER_INT,
  1799. .regoff_udccs = UDCCS5,
  1800. .regoff_uddr = UDDR5,
  1801. },
  1802. /* second group of endpoints */
  1803. .ep[6] = {
  1804. .ep = {
  1805. .name = "ep6in-bulk",
  1806. .ops = &pxa25x_ep_ops,
  1807. .maxpacket = BULK_FIFO_SIZE,
  1808. .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK,
  1809. USB_EP_CAPS_DIR_IN),
  1810. },
  1811. .dev = &memory,
  1812. .fifo_size = BULK_FIFO_SIZE,
  1813. .bEndpointAddress = USB_DIR_IN | 6,
  1814. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1815. .regoff_udccs = UDCCS6,
  1816. .regoff_uddr = UDDR6,
  1817. },
  1818. .ep[7] = {
  1819. .ep = {
  1820. .name = "ep7out-bulk",
  1821. .ops = &pxa25x_ep_ops,
  1822. .maxpacket = BULK_FIFO_SIZE,
  1823. .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK,
  1824. USB_EP_CAPS_DIR_OUT),
  1825. },
  1826. .dev = &memory,
  1827. .fifo_size = BULK_FIFO_SIZE,
  1828. .bEndpointAddress = 7,
  1829. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1830. .regoff_udccs = UDCCS7,
  1831. .regoff_ubcr = UBCR7,
  1832. .regoff_uddr = UDDR7,
  1833. },
  1834. .ep[8] = {
  1835. .ep = {
  1836. .name = "ep8in-iso",
  1837. .ops = &pxa25x_ep_ops,
  1838. .maxpacket = ISO_FIFO_SIZE,
  1839. .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO,
  1840. USB_EP_CAPS_DIR_IN),
  1841. },
  1842. .dev = &memory,
  1843. .fifo_size = ISO_FIFO_SIZE,
  1844. .bEndpointAddress = USB_DIR_IN | 8,
  1845. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  1846. .regoff_udccs = UDCCS8,
  1847. .regoff_uddr = UDDR8,
  1848. },
  1849. .ep[9] = {
  1850. .ep = {
  1851. .name = "ep9out-iso",
  1852. .ops = &pxa25x_ep_ops,
  1853. .maxpacket = ISO_FIFO_SIZE,
  1854. .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO,
  1855. USB_EP_CAPS_DIR_OUT),
  1856. },
  1857. .dev = &memory,
  1858. .fifo_size = ISO_FIFO_SIZE,
  1859. .bEndpointAddress = 9,
  1860. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  1861. .regoff_udccs = UDCCS9,
  1862. .regoff_ubcr = UBCR9,
  1863. .regoff_uddr = UDDR9,
  1864. },
  1865. .ep[10] = {
  1866. .ep = {
  1867. .name = "ep10in-int",
  1868. .ops = &pxa25x_ep_ops,
  1869. .maxpacket = INT_FIFO_SIZE,
  1870. .caps = USB_EP_CAPS(0, 0),
  1871. },
  1872. .dev = &memory,
  1873. .fifo_size = INT_FIFO_SIZE,
  1874. .bEndpointAddress = USB_DIR_IN | 10,
  1875. .bmAttributes = USB_ENDPOINT_XFER_INT,
  1876. .regoff_udccs = UDCCS10,
  1877. .regoff_uddr = UDDR10,
  1878. },
  1879. /* third group of endpoints */
  1880. .ep[11] = {
  1881. .ep = {
  1882. .name = "ep11in-bulk",
  1883. .ops = &pxa25x_ep_ops,
  1884. .maxpacket = BULK_FIFO_SIZE,
  1885. .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK,
  1886. USB_EP_CAPS_DIR_IN),
  1887. },
  1888. .dev = &memory,
  1889. .fifo_size = BULK_FIFO_SIZE,
  1890. .bEndpointAddress = USB_DIR_IN | 11,
  1891. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1892. .regoff_udccs = UDCCS11,
  1893. .regoff_uddr = UDDR11,
  1894. },
  1895. .ep[12] = {
  1896. .ep = {
  1897. .name = "ep12out-bulk",
  1898. .ops = &pxa25x_ep_ops,
  1899. .maxpacket = BULK_FIFO_SIZE,
  1900. .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK,
  1901. USB_EP_CAPS_DIR_OUT),
  1902. },
  1903. .dev = &memory,
  1904. .fifo_size = BULK_FIFO_SIZE,
  1905. .bEndpointAddress = 12,
  1906. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1907. .regoff_udccs = UDCCS12,
  1908. .regoff_ubcr = UBCR12,
  1909. .regoff_uddr = UDDR12,
  1910. },
  1911. .ep[13] = {
  1912. .ep = {
  1913. .name = "ep13in-iso",
  1914. .ops = &pxa25x_ep_ops,
  1915. .maxpacket = ISO_FIFO_SIZE,
  1916. .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO,
  1917. USB_EP_CAPS_DIR_IN),
  1918. },
  1919. .dev = &memory,
  1920. .fifo_size = ISO_FIFO_SIZE,
  1921. .bEndpointAddress = USB_DIR_IN | 13,
  1922. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  1923. .regoff_udccs = UDCCS13,
  1924. .regoff_uddr = UDDR13,
  1925. },
  1926. .ep[14] = {
  1927. .ep = {
  1928. .name = "ep14out-iso",
  1929. .ops = &pxa25x_ep_ops,
  1930. .maxpacket = ISO_FIFO_SIZE,
  1931. .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO,
  1932. USB_EP_CAPS_DIR_OUT),
  1933. },
  1934. .dev = &memory,
  1935. .fifo_size = ISO_FIFO_SIZE,
  1936. .bEndpointAddress = 14,
  1937. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  1938. .regoff_udccs = UDCCS14,
  1939. .regoff_ubcr = UBCR14,
  1940. .regoff_uddr = UDDR14,
  1941. },
  1942. .ep[15] = {
  1943. .ep = {
  1944. .name = "ep15in-int",
  1945. .ops = &pxa25x_ep_ops,
  1946. .maxpacket = INT_FIFO_SIZE,
  1947. .caps = USB_EP_CAPS(0, 0),
  1948. },
  1949. .dev = &memory,
  1950. .fifo_size = INT_FIFO_SIZE,
  1951. .bEndpointAddress = USB_DIR_IN | 15,
  1952. .bmAttributes = USB_ENDPOINT_XFER_INT,
  1953. .regoff_udccs = UDCCS15,
  1954. .regoff_uddr = UDDR15,
  1955. },
  1956. #endif /* !CONFIG_USB_PXA25X_SMALL */
  1957. };
  1958. #define CP15R0_VENDOR_MASK 0xffffe000
  1959. #if defined(CONFIG_ARCH_PXA)
  1960. #define CP15R0_XSCALE_VALUE 0x69052000 /* intel/arm/xscale */
  1961. #elif defined(CONFIG_ARCH_IXP4XX)
  1962. #define CP15R0_XSCALE_VALUE 0x69054000 /* intel/arm/ixp4xx */
  1963. #endif
  1964. #define CP15R0_PROD_MASK 0x000003f0
  1965. #define PXA25x 0x00000100 /* and PXA26x */
  1966. #define PXA210 0x00000120
  1967. #define CP15R0_REV_MASK 0x0000000f
  1968. #define CP15R0_PRODREV_MASK (CP15R0_PROD_MASK | CP15R0_REV_MASK)
  1969. #define PXA255_A0 0x00000106 /* or PXA260_B1 */
  1970. #define PXA250_C0 0x00000105 /* or PXA26x_B0 */
  1971. #define PXA250_B2 0x00000104
  1972. #define PXA250_B1 0x00000103 /* or PXA260_A0 */
  1973. #define PXA250_B0 0x00000102
  1974. #define PXA250_A1 0x00000101
  1975. #define PXA250_A0 0x00000100
  1976. #define PXA210_C0 0x00000125
  1977. #define PXA210_B2 0x00000124
  1978. #define PXA210_B1 0x00000123
  1979. #define PXA210_B0 0x00000122
  1980. #define IXP425_A0 0x000001c1
  1981. #define IXP425_B0 0x000001f1
  1982. #define IXP465_AD 0x00000200
  1983. /*
  1984. * probe - binds to the platform device
  1985. */
  1986. static int pxa25x_udc_probe(struct platform_device *pdev)
  1987. {
  1988. struct pxa25x_udc *dev = &memory;
  1989. int retval, irq;
  1990. u32 chiprev;
  1991. pr_info("%s: version %s\n", driver_name, DRIVER_VERSION);
  1992. /* insist on Intel/ARM/XScale */
  1993. asm("mrc p15, 0, %0, c0, c0" : "=r" (chiprev));
  1994. if ((chiprev & CP15R0_VENDOR_MASK) != CP15R0_XSCALE_VALUE) {
  1995. pr_err("%s: not XScale!\n", driver_name);
  1996. return -ENODEV;
  1997. }
  1998. /* trigger chiprev-specific logic */
  1999. switch (chiprev & CP15R0_PRODREV_MASK) {
  2000. #if defined(CONFIG_ARCH_PXA)
  2001. case PXA255_A0:
  2002. dev->has_cfr = 1;
  2003. break;
  2004. case PXA250_A0:
  2005. case PXA250_A1:
  2006. /* A0/A1 "not released"; ep 13, 15 unusable */
  2007. fallthrough;
  2008. case PXA250_B2: case PXA210_B2:
  2009. case PXA250_B1: case PXA210_B1:
  2010. case PXA250_B0: case PXA210_B0:
  2011. /* OUT-DMA is broken ... */
  2012. fallthrough;
  2013. case PXA250_C0: case PXA210_C0:
  2014. break;
  2015. #elif defined(CONFIG_ARCH_IXP4XX)
  2016. case IXP425_A0:
  2017. case IXP425_B0:
  2018. case IXP465_AD:
  2019. dev->has_cfr = 1;
  2020. break;
  2021. #endif
  2022. default:
  2023. pr_err("%s: unrecognized processor: %08x\n",
  2024. driver_name, chiprev);
  2025. /* iop3xx, ixp4xx, ... */
  2026. return -ENODEV;
  2027. }
  2028. irq = platform_get_irq(pdev, 0);
  2029. if (irq < 0)
  2030. return irq;
  2031. dev->regs = devm_platform_ioremap_resource(pdev, 0);
  2032. if (IS_ERR(dev->regs))
  2033. return PTR_ERR(dev->regs);
  2034. dev->clk = devm_clk_get(&pdev->dev, NULL);
  2035. if (IS_ERR(dev->clk))
  2036. return PTR_ERR(dev->clk);
  2037. pr_debug("%s: IRQ %d%s%s\n", driver_name, irq,
  2038. dev->has_cfr ? "" : " (!cfr)",
  2039. SIZE_STR "(pio)"
  2040. );
  2041. /* other non-static parts of init */
  2042. dev->dev = &pdev->dev;
  2043. dev->mach = dev_get_platdata(&pdev->dev);
  2044. dev->transceiver = devm_usb_get_phy(&pdev->dev, USB_PHY_TYPE_USB2);
  2045. if (gpio_is_valid(dev->mach->gpio_pullup)) {
  2046. retval = devm_gpio_request(&pdev->dev, dev->mach->gpio_pullup,
  2047. "pca25x_udc GPIO PULLUP");
  2048. if (retval) {
  2049. dev_dbg(&pdev->dev,
  2050. "can't get pullup gpio %d, err: %d\n",
  2051. dev->mach->gpio_pullup, retval);
  2052. goto err;
  2053. }
  2054. gpio_direction_output(dev->mach->gpio_pullup, 0);
  2055. }
  2056. timer_setup(&dev->timer, udc_watchdog, 0);
  2057. the_controller = dev;
  2058. platform_set_drvdata(pdev, dev);
  2059. udc_disable(dev);
  2060. udc_reinit(dev);
  2061. dev->vbus = 0;
  2062. /* irq setup after old hardware state is cleaned up */
  2063. retval = devm_request_irq(&pdev->dev, irq, pxa25x_udc_irq, 0,
  2064. driver_name, dev);
  2065. if (retval != 0) {
  2066. pr_err("%s: can't get irq %d, err %d\n",
  2067. driver_name, irq, retval);
  2068. goto err;
  2069. }
  2070. dev->got_irq = 1;
  2071. #ifdef CONFIG_ARCH_LUBBOCK
  2072. if (machine_is_lubbock()) {
  2073. dev->usb_irq = platform_get_irq(pdev, 1);
  2074. if (dev->usb_irq < 0)
  2075. return dev->usb_irq;
  2076. dev->usb_disc_irq = platform_get_irq(pdev, 2);
  2077. if (dev->usb_disc_irq < 0)
  2078. return dev->usb_disc_irq;
  2079. retval = devm_request_irq(&pdev->dev, dev->usb_disc_irq,
  2080. lubbock_vbus_irq, 0, driver_name,
  2081. dev);
  2082. if (retval != 0) {
  2083. pr_err("%s: can't get irq %i, err %d\n",
  2084. driver_name, dev->usb_disc_irq, retval);
  2085. goto err;
  2086. }
  2087. retval = devm_request_irq(&pdev->dev, dev->usb_irq,
  2088. lubbock_vbus_irq, 0, driver_name,
  2089. dev);
  2090. if (retval != 0) {
  2091. pr_err("%s: can't get irq %i, err %d\n",
  2092. driver_name, dev->usb_irq, retval);
  2093. goto err;
  2094. }
  2095. } else
  2096. #endif
  2097. create_debug_files(dev);
  2098. retval = usb_add_gadget_udc(&pdev->dev, &dev->gadget);
  2099. if (!retval)
  2100. return retval;
  2101. remove_debug_files(dev);
  2102. err:
  2103. if (!IS_ERR_OR_NULL(dev->transceiver))
  2104. dev->transceiver = NULL;
  2105. return retval;
  2106. }
  2107. static void pxa25x_udc_shutdown(struct platform_device *_dev)
  2108. {
  2109. pullup_off();
  2110. }
  2111. static int pxa25x_udc_remove(struct platform_device *pdev)
  2112. {
  2113. struct pxa25x_udc *dev = platform_get_drvdata(pdev);
  2114. if (dev->driver)
  2115. return -EBUSY;
  2116. usb_del_gadget_udc(&dev->gadget);
  2117. dev->pullup = 0;
  2118. pullup(dev);
  2119. remove_debug_files(dev);
  2120. if (!IS_ERR_OR_NULL(dev->transceiver))
  2121. dev->transceiver = NULL;
  2122. the_controller = NULL;
  2123. return 0;
  2124. }
  2125. /*-------------------------------------------------------------------------*/
  2126. #ifdef CONFIG_PM
  2127. /* USB suspend (controlled by the host) and system suspend (controlled
  2128. * by the PXA) don't necessarily work well together. If USB is active,
  2129. * the 48 MHz clock is required; so the system can't enter 33 MHz idle
  2130. * mode, or any deeper PM saving state.
  2131. *
  2132. * For now, we punt and forcibly disconnect from the USB host when PXA
  2133. * enters any suspend state. While we're disconnected, we always disable
  2134. * the 48MHz USB clock ... allowing PXA sleep and/or 33 MHz idle states.
  2135. * Boards without software pullup control shouldn't use those states.
  2136. * VBUS IRQs should probably be ignored so that the PXA device just acts
  2137. * "dead" to USB hosts until system resume.
  2138. */
  2139. static int pxa25x_udc_suspend(struct platform_device *dev, pm_message_t state)
  2140. {
  2141. struct pxa25x_udc *udc = platform_get_drvdata(dev);
  2142. unsigned long flags;
  2143. if (!gpio_is_valid(udc->mach->gpio_pullup) && !udc->mach->udc_command)
  2144. WARNING("USB host won't detect disconnect!\n");
  2145. udc->suspended = 1;
  2146. local_irq_save(flags);
  2147. pullup(udc);
  2148. local_irq_restore(flags);
  2149. return 0;
  2150. }
  2151. static int pxa25x_udc_resume(struct platform_device *dev)
  2152. {
  2153. struct pxa25x_udc *udc = platform_get_drvdata(dev);
  2154. unsigned long flags;
  2155. udc->suspended = 0;
  2156. local_irq_save(flags);
  2157. pullup(udc);
  2158. local_irq_restore(flags);
  2159. return 0;
  2160. }
  2161. #else
  2162. #define pxa25x_udc_suspend NULL
  2163. #define pxa25x_udc_resume NULL
  2164. #endif
  2165. /*-------------------------------------------------------------------------*/
  2166. static struct platform_driver udc_driver = {
  2167. .shutdown = pxa25x_udc_shutdown,
  2168. .probe = pxa25x_udc_probe,
  2169. .remove = pxa25x_udc_remove,
  2170. .suspend = pxa25x_udc_suspend,
  2171. .resume = pxa25x_udc_resume,
  2172. .driver = {
  2173. .name = "pxa25x-udc",
  2174. },
  2175. };
  2176. module_platform_driver(udc_driver);
  2177. MODULE_DESCRIPTION(DRIVER_DESC);
  2178. MODULE_AUTHOR("Frank Becker, Robert Schwebel, David Brownell");
  2179. MODULE_LICENSE("GPL");
  2180. MODULE_ALIAS("platform:pxa25x-udc");