pxa25x_udc.c 57 KB

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