pxa2xx_udc.c 66 KB

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