pxa25x_udc.c 59 KB

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