pxa25x_udc.c 58 KB

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