pxa25x_udc.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Intel PXA25x and IXP4xx on-chip full speed USB device controllers
  4. *
  5. * Copyright (C) 2002 Intrinsyc, Inc. (Frank Becker)
  6. * Copyright (C) 2003 Robert Schwebel, Pengutronix
  7. * Copyright (C) 2003 Benedikt Spranger, Pengutronix
  8. * Copyright (C) 2003 David Brownell
  9. * Copyright (C) 2003 Joshua Wise
  10. * Copyright (C) 2012 Lukasz Dalek <luk0104@gmail.com>
  11. *
  12. * MODULE_AUTHOR("Frank Becker, Robert Schwebel, David Brownell");
  13. */
  14. #define CONFIG_USB_PXA25X_SMALL
  15. #define DRIVER_NAME "pxa25x_udc_linux"
  16. #define ARCH_HAS_PREFETCH
  17. #include <common.h>
  18. #include <errno.h>
  19. #include <asm/byteorder.h>
  20. #include <asm/system.h>
  21. #include <asm/mach-types.h>
  22. #include <asm/unaligned.h>
  23. #include <linux/compat.h>
  24. #include <malloc.h>
  25. #include <asm/io.h>
  26. #include <asm/arch/pxa.h>
  27. #include <linux/usb/ch9.h>
  28. #include <linux/usb/gadget.h>
  29. #include <usb/lin_gadget_compat.h>
  30. #include <asm/arch/pxa-regs.h>
  31. #include "pxa25x_udc.h"
  32. /*
  33. * This driver handles the USB Device Controller (UDC) in Intel's PXA 25x
  34. * series processors. The UDC for the IXP 4xx series is very similar.
  35. * There are fifteen endpoints, in addition to ep0.
  36. *
  37. * Such controller drivers work with a gadget driver. The gadget driver
  38. * returns descriptors, implements configuration and data protocols used
  39. * by the host to interact with this device, and allocates endpoints to
  40. * the different protocol interfaces. The controller driver virtualizes
  41. * usb hardware so that the gadget drivers will be more portable.
  42. *
  43. * This UDC hardware wants to implement a bit too much USB protocol, so
  44. * it constrains the sorts of USB configuration change events that work.
  45. * The errata for these chips are misleading; some "fixed" bugs from
  46. * pxa250 a0/a1 b0/b1/b2 sure act like they're still there.
  47. *
  48. * Note that the UDC hardware supports DMA (except on IXP) but that's
  49. * not used here. IN-DMA (to host) is simple enough, when the data is
  50. * suitably aligned (16 bytes) ... the network stack doesn't do that,
  51. * other software can. OUT-DMA is buggy in most chip versions, as well
  52. * as poorly designed (data toggle not automatic). So this driver won't
  53. * bother using DMA. (Mostly-working IN-DMA support was available in
  54. * kernels before 2.6.23, but was never enabled or well tested.)
  55. */
  56. #define DRIVER_VERSION "18-August-2012"
  57. #define DRIVER_DESC "PXA 25x USB Device Controller driver"
  58. static const char driver_name[] = "pxa25x_udc";
  59. static const char ep0name[] = "ep0";
  60. /* Watchdog */
  61. static inline void start_watchdog(struct pxa25x_udc *udc)
  62. {
  63. debug("Started watchdog\n");
  64. udc->watchdog.base = get_timer(0);
  65. udc->watchdog.running = 1;
  66. }
  67. static inline void stop_watchdog(struct pxa25x_udc *udc)
  68. {
  69. udc->watchdog.running = 0;
  70. debug("Stopped watchdog\n");
  71. }
  72. static inline void test_watchdog(struct pxa25x_udc *udc)
  73. {
  74. if (!udc->watchdog.running)
  75. return;
  76. debug("watchdog %ld %ld\n", get_timer(udc->watchdog.base),
  77. udc->watchdog.period);
  78. if (get_timer(udc->watchdog.base) >= udc->watchdog.period) {
  79. stop_watchdog(udc);
  80. udc->watchdog.function(udc);
  81. }
  82. }
  83. static void udc_watchdog(struct pxa25x_udc *dev)
  84. {
  85. uint32_t udccs0 = readl(&dev->regs->udccs[0]);
  86. debug("Fired up udc_watchdog\n");
  87. local_irq_disable();
  88. if (dev->ep0state == EP0_STALL
  89. && (udccs0 & UDCCS0_FST) == 0
  90. && (udccs0 & UDCCS0_SST) == 0) {
  91. writel(UDCCS0_FST|UDCCS0_FTF, &dev->regs->udccs[0]);
  92. debug("ep0 re-stall\n");
  93. start_watchdog(dev);
  94. }
  95. local_irq_enable();
  96. }
  97. #ifdef DEBUG
  98. static const char * const state_name[] = {
  99. "EP0_IDLE",
  100. "EP0_IN_DATA_PHASE", "EP0_OUT_DATA_PHASE",
  101. "EP0_END_XFER", "EP0_STALL"
  102. };
  103. static void
  104. dump_udccr(const char *label)
  105. {
  106. u32 udccr = readl(&UDC_REGS->udccr);
  107. debug("%s %02X =%s%s%s%s%s%s%s%s\n",
  108. label, udccr,
  109. (udccr & UDCCR_REM) ? " rem" : "",
  110. (udccr & UDCCR_RSTIR) ? " rstir" : "",
  111. (udccr & UDCCR_SRM) ? " srm" : "",
  112. (udccr & UDCCR_SUSIR) ? " susir" : "",
  113. (udccr & UDCCR_RESIR) ? " resir" : "",
  114. (udccr & UDCCR_RSM) ? " rsm" : "",
  115. (udccr & UDCCR_UDA) ? " uda" : "",
  116. (udccr & UDCCR_UDE) ? " ude" : "");
  117. }
  118. static void
  119. dump_udccs0(const char *label)
  120. {
  121. u32 udccs0 = readl(&UDC_REGS->udccs[0]);
  122. debug("%s %s %02X =%s%s%s%s%s%s%s%s\n",
  123. label, state_name[the_controller->ep0state], udccs0,
  124. (udccs0 & UDCCS0_SA) ? " sa" : "",
  125. (udccs0 & UDCCS0_RNE) ? " rne" : "",
  126. (udccs0 & UDCCS0_FST) ? " fst" : "",
  127. (udccs0 & UDCCS0_SST) ? " sst" : "",
  128. (udccs0 & UDCCS0_DRWF) ? " dwrf" : "",
  129. (udccs0 & UDCCS0_FTF) ? " ftf" : "",
  130. (udccs0 & UDCCS0_IPR) ? " ipr" : "",
  131. (udccs0 & UDCCS0_OPR) ? " opr" : "");
  132. }
  133. static void
  134. dump_state(struct pxa25x_udc *dev)
  135. {
  136. u32 tmp;
  137. unsigned i;
  138. debug("%s, uicr %02X.%02X, usir %02X.%02x, ufnr %02X.%02X\n",
  139. state_name[dev->ep0state],
  140. readl(&UDC_REGS->uicr1), readl(&UDC_REGS->uicr0),
  141. readl(&UDC_REGS->usir1), readl(&UDC_REGS->usir0),
  142. readl(&UDC_REGS->ufnrh), readl(&UDC_REGS->ufnrl));
  143. dump_udccr("udccr");
  144. if (dev->has_cfr) {
  145. tmp = readl(&UDC_REGS->udccfr);
  146. debug("udccfr %02X =%s%s\n", tmp,
  147. (tmp & UDCCFR_AREN) ? " aren" : "",
  148. (tmp & UDCCFR_ACM) ? " acm" : "");
  149. }
  150. if (!dev->driver) {
  151. debug("no gadget driver bound\n");
  152. return;
  153. } else
  154. debug("ep0 driver '%s'\n", "ether");
  155. dump_udccs0("udccs0");
  156. debug("ep0 IN %lu/%lu, OUT %lu/%lu\n",
  157. dev->stats.write.bytes, dev->stats.write.ops,
  158. dev->stats.read.bytes, dev->stats.read.ops);
  159. for (i = 1; i < PXA_UDC_NUM_ENDPOINTS; i++) {
  160. if (dev->ep[i].desc == NULL)
  161. continue;
  162. debug("udccs%d = %02x\n", i, *dev->ep->reg_udccs);
  163. }
  164. }
  165. #else /* DEBUG */
  166. static inline void dump_udccr(const char *label) { }
  167. static inline void dump_udccs0(const char *label) { }
  168. static inline void dump_state(struct pxa25x_udc *dev) { }
  169. #endif /* DEBUG */
  170. /*
  171. * ---------------------------------------------------------------------------
  172. * endpoint related parts of the api to the usb controller hardware,
  173. * used by gadget driver; and the inner talker-to-hardware core.
  174. * ---------------------------------------------------------------------------
  175. */
  176. static void pxa25x_ep_fifo_flush(struct usb_ep *ep);
  177. static void nuke(struct pxa25x_ep *, int status);
  178. /* one GPIO should control a D+ pullup, so host sees this device (or not) */
  179. static void pullup_off(void)
  180. {
  181. struct pxa2xx_udc_mach_info *mach = the_controller->mach;
  182. if (mach->udc_command)
  183. mach->udc_command(PXA2XX_UDC_CMD_DISCONNECT);
  184. }
  185. static void pullup_on(void)
  186. {
  187. struct pxa2xx_udc_mach_info *mach = the_controller->mach;
  188. if (mach->udc_command)
  189. mach->udc_command(PXA2XX_UDC_CMD_CONNECT);
  190. }
  191. static void pio_irq_enable(int bEndpointAddress)
  192. {
  193. bEndpointAddress &= 0xf;
  194. if (bEndpointAddress < 8) {
  195. clrbits_le32(&the_controller->regs->uicr0,
  196. 1 << bEndpointAddress);
  197. } else {
  198. bEndpointAddress -= 8;
  199. clrbits_le32(&the_controller->regs->uicr1,
  200. 1 << bEndpointAddress);
  201. }
  202. }
  203. static void pio_irq_disable(int bEndpointAddress)
  204. {
  205. bEndpointAddress &= 0xf;
  206. if (bEndpointAddress < 8) {
  207. setbits_le32(&the_controller->regs->uicr0,
  208. 1 << bEndpointAddress);
  209. } else {
  210. bEndpointAddress -= 8;
  211. setbits_le32(&the_controller->regs->uicr1,
  212. 1 << bEndpointAddress);
  213. }
  214. }
  215. static inline void udc_set_mask_UDCCR(int mask)
  216. {
  217. /*
  218. * The UDCCR reg contains mask and interrupt status bits,
  219. * so using '|=' isn't safe as it may ack an interrupt.
  220. */
  221. const uint32_t mask_bits = UDCCR_REM | UDCCR_SRM | UDCCR_UDE;
  222. mask &= mask_bits;
  223. clrsetbits_le32(&the_controller->regs->udccr, ~mask_bits, mask);
  224. }
  225. static inline void udc_clear_mask_UDCCR(int mask)
  226. {
  227. const uint32_t mask_bits = UDCCR_REM | UDCCR_SRM | UDCCR_UDE;
  228. mask = ~mask & mask_bits;
  229. clrbits_le32(&the_controller->regs->udccr, ~mask);
  230. }
  231. static inline void udc_ack_int_UDCCR(int mask)
  232. {
  233. const uint32_t mask_bits = UDCCR_REM | UDCCR_SRM | UDCCR_UDE;
  234. mask &= ~mask_bits;
  235. clrsetbits_le32(&the_controller->regs->udccr, ~mask_bits, mask);
  236. }
  237. /*
  238. * endpoint enable/disable
  239. *
  240. * we need to verify the descriptors used to enable endpoints. since pxa25x
  241. * endpoint configurations are fixed, and are pretty much always enabled,
  242. * there's not a lot to manage here.
  243. *
  244. * because pxa25x can't selectively initialize bulk (or interrupt) endpoints,
  245. * (resetting endpoint halt and toggle), SET_INTERFACE is unusable except
  246. * for a single interface (with only the default altsetting) and for gadget
  247. * drivers that don't halt endpoints (not reset by set_interface). that also
  248. * means that if you use ISO, you must violate the USB spec rule that all
  249. * iso endpoints must be in non-default altsettings.
  250. */
  251. static int pxa25x_ep_enable(struct usb_ep *_ep,
  252. const struct usb_endpoint_descriptor *desc)
  253. {
  254. struct pxa25x_ep *ep;
  255. struct pxa25x_udc *dev;
  256. ep = container_of(_ep, struct pxa25x_ep, ep);
  257. if (!_ep || !desc || ep->desc || _ep->name == ep0name
  258. || desc->bDescriptorType != USB_DT_ENDPOINT
  259. || ep->bEndpointAddress != desc->bEndpointAddress
  260. || ep->fifo_size <
  261. le16_to_cpu(get_unaligned(&desc->wMaxPacketSize))) {
  262. printf("%s, bad ep or descriptor\n", __func__);
  263. return -EINVAL;
  264. }
  265. /* xfer types must match, except that interrupt ~= bulk */
  266. if (ep->bmAttributes != desc->bmAttributes
  267. && ep->bmAttributes != USB_ENDPOINT_XFER_BULK
  268. && desc->bmAttributes != USB_ENDPOINT_XFER_INT) {
  269. printf("%s, %s type mismatch\n", __func__, _ep->name);
  270. return -EINVAL;
  271. }
  272. /* hardware _could_ do smaller, but driver doesn't */
  273. if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK
  274. && le16_to_cpu(get_unaligned(&desc->wMaxPacketSize))
  275. != BULK_FIFO_SIZE)
  276. || !get_unaligned(&desc->wMaxPacketSize)) {
  277. printf("%s, bad %s maxpacket\n", __func__, _ep->name);
  278. return -ERANGE;
  279. }
  280. dev = ep->dev;
  281. if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
  282. printf("%s, bogus device state\n", __func__);
  283. return -ESHUTDOWN;
  284. }
  285. ep->desc = desc;
  286. ep->stopped = 0;
  287. ep->pio_irqs = 0;
  288. ep->ep.maxpacket = le16_to_cpu(get_unaligned(&desc->wMaxPacketSize));
  289. /* flush fifo (mostly for OUT buffers) */
  290. pxa25x_ep_fifo_flush(_ep);
  291. /* ... reset halt state too, if we could ... */
  292. debug("enabled %s\n", _ep->name);
  293. return 0;
  294. }
  295. static int pxa25x_ep_disable(struct usb_ep *_ep)
  296. {
  297. struct pxa25x_ep *ep;
  298. unsigned long flags;
  299. ep = container_of(_ep, struct pxa25x_ep, ep);
  300. if (!_ep || !ep->desc) {
  301. printf("%s, %s not enabled\n", __func__,
  302. _ep ? ep->ep.name : NULL);
  303. return -EINVAL;
  304. }
  305. local_irq_save(flags);
  306. nuke(ep, -ESHUTDOWN);
  307. /* flush fifo (mostly for IN buffers) */
  308. pxa25x_ep_fifo_flush(_ep);
  309. ep->desc = NULL;
  310. ep->stopped = 1;
  311. local_irq_restore(flags);
  312. debug("%s disabled\n", _ep->name);
  313. return 0;
  314. }
  315. /*-------------------------------------------------------------------------*/
  316. /*
  317. * for the pxa25x, these can just wrap kmalloc/kfree. gadget drivers
  318. * must still pass correctly initialized endpoints, since other controller
  319. * drivers may care about how it's currently set up (dma issues etc).
  320. */
  321. /*
  322. * pxa25x_ep_alloc_request - allocate a request data structure
  323. */
  324. static struct usb_request *
  325. pxa25x_ep_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
  326. {
  327. struct pxa25x_request *req;
  328. req = kzalloc(sizeof(*req), gfp_flags);
  329. if (!req)
  330. return NULL;
  331. INIT_LIST_HEAD(&req->queue);
  332. return &req->req;
  333. }
  334. /*
  335. * pxa25x_ep_free_request - deallocate a request data structure
  336. */
  337. static void
  338. pxa25x_ep_free_request(struct usb_ep *_ep, struct usb_request *_req)
  339. {
  340. struct pxa25x_request *req;
  341. req = container_of(_req, struct pxa25x_request, req);
  342. WARN_ON(!list_empty(&req->queue));
  343. kfree(req);
  344. }
  345. /*-------------------------------------------------------------------------*/
  346. /*
  347. * done - retire a request; caller blocked irqs
  348. */
  349. static void done(struct pxa25x_ep *ep, struct pxa25x_request *req, int status)
  350. {
  351. unsigned stopped = ep->stopped;
  352. list_del_init(&req->queue);
  353. if (likely(req->req.status == -EINPROGRESS))
  354. req->req.status = status;
  355. else
  356. status = req->req.status;
  357. if (status && status != -ESHUTDOWN)
  358. debug("complete %s req %p stat %d len %u/%u\n",
  359. ep->ep.name, &req->req, status,
  360. req->req.actual, req->req.length);
  361. /* don't modify queue heads during completion callback */
  362. ep->stopped = 1;
  363. req->req.complete(&ep->ep, &req->req);
  364. ep->stopped = stopped;
  365. }
  366. static inline void ep0_idle(struct pxa25x_udc *dev)
  367. {
  368. dev->ep0state = EP0_IDLE;
  369. }
  370. static int
  371. write_packet(u32 *uddr, struct pxa25x_request *req, unsigned max)
  372. {
  373. u8 *buf;
  374. unsigned length, count;
  375. debug("%s(): uddr %p\n", __func__, uddr);
  376. buf = req->req.buf + req->req.actual;
  377. prefetch(buf);
  378. /* how big will this packet be? */
  379. length = min(req->req.length - req->req.actual, max);
  380. req->req.actual += length;
  381. count = length;
  382. while (likely(count--))
  383. writeb(*buf++, uddr);
  384. return length;
  385. }
  386. /*
  387. * write to an IN endpoint fifo, as many packets as possible.
  388. * irqs will use this to write the rest later.
  389. * caller guarantees at least one packet buffer is ready (or a zlp).
  390. */
  391. static int
  392. write_fifo(struct pxa25x_ep *ep, struct pxa25x_request *req)
  393. {
  394. unsigned max;
  395. max = le16_to_cpu(get_unaligned(&ep->desc->wMaxPacketSize));
  396. do {
  397. unsigned count;
  398. int is_last, is_short;
  399. count = write_packet(ep->reg_uddr, req, max);
  400. /* last packet is usually short (or a zlp) */
  401. if (unlikely(count != max))
  402. is_last = is_short = 1;
  403. else {
  404. if (likely(req->req.length != req->req.actual)
  405. || req->req.zero)
  406. is_last = 0;
  407. else
  408. is_last = 1;
  409. /* interrupt/iso maxpacket may not fill the fifo */
  410. is_short = unlikely(max < ep->fifo_size);
  411. }
  412. debug_cond(NOISY, "wrote %s %d bytes%s%s %d left %p\n",
  413. ep->ep.name, count,
  414. is_last ? "/L" : "", is_short ? "/S" : "",
  415. req->req.length - req->req.actual, req);
  416. /*
  417. * let loose that packet. maybe try writing another one,
  418. * double buffering might work. TSP, TPC, and TFS
  419. * bit values are the same for all normal IN endpoints.
  420. */
  421. writel(UDCCS_BI_TPC, ep->reg_udccs);
  422. if (is_short)
  423. writel(UDCCS_BI_TSP, ep->reg_udccs);
  424. /* requests complete when all IN data is in the FIFO */
  425. if (is_last) {
  426. done(ep, req, 0);
  427. if (list_empty(&ep->queue))
  428. pio_irq_disable(ep->bEndpointAddress);
  429. return 1;
  430. }
  431. /*
  432. * TODO experiment: how robust can fifo mode tweaking be?
  433. * double buffering is off in the default fifo mode, which
  434. * prevents TFS from being set here.
  435. */
  436. } while (readl(ep->reg_udccs) & UDCCS_BI_TFS);
  437. return 0;
  438. }
  439. /*
  440. * caller asserts req->pending (ep0 irq status nyet cleared); starts
  441. * ep0 data stage. these chips want very simple state transitions.
  442. */
  443. static inline
  444. void ep0start(struct pxa25x_udc *dev, u32 flags, const char *tag)
  445. {
  446. writel(flags|UDCCS0_SA|UDCCS0_OPR, &dev->regs->udccs[0]);
  447. writel(USIR0_IR0, &dev->regs->usir0);
  448. dev->req_pending = 0;
  449. debug_cond(NOISY, "%s() %s, udccs0: %02x/%02x usir: %X.%X\n",
  450. __func__, tag, readl(&dev->regs->udccs[0]), flags,
  451. readl(&dev->regs->usir1), readl(&dev->regs->usir0));
  452. }
  453. static int
  454. write_ep0_fifo(struct pxa25x_ep *ep, struct pxa25x_request *req)
  455. {
  456. unsigned count;
  457. int is_short;
  458. count = write_packet(&ep->dev->regs->uddr0, req, EP0_FIFO_SIZE);
  459. ep->dev->stats.write.bytes += count;
  460. /* last packet "must be" short (or a zlp) */
  461. is_short = (count != EP0_FIFO_SIZE);
  462. debug_cond(NOISY, "ep0in %d bytes %d left %p\n", count,
  463. req->req.length - req->req.actual, req);
  464. if (unlikely(is_short)) {
  465. if (ep->dev->req_pending)
  466. ep0start(ep->dev, UDCCS0_IPR, "short IN");
  467. else
  468. writel(UDCCS0_IPR, &ep->dev->regs->udccs[0]);
  469. count = req->req.length;
  470. done(ep, req, 0);
  471. ep0_idle(ep->dev);
  472. /*
  473. * This seems to get rid of lost status irqs in some cases:
  474. * host responds quickly, or next request involves config
  475. * change automagic, or should have been hidden, or ...
  476. *
  477. * FIXME get rid of all udelays possible...
  478. */
  479. if (count >= EP0_FIFO_SIZE) {
  480. count = 100;
  481. do {
  482. if ((readl(&ep->dev->regs->udccs[0]) &
  483. UDCCS0_OPR) != 0) {
  484. /* clear OPR, generate ack */
  485. writel(UDCCS0_OPR,
  486. &ep->dev->regs->udccs[0]);
  487. break;
  488. }
  489. count--;
  490. udelay(1);
  491. } while (count);
  492. }
  493. } else if (ep->dev->req_pending)
  494. ep0start(ep->dev, 0, "IN");
  495. return is_short;
  496. }
  497. /*
  498. * read_fifo - unload packet(s) from the fifo we use for usb OUT
  499. * transfers and put them into the request. caller should have made
  500. * sure there's at least one packet ready.
  501. *
  502. * returns true if the request completed because of short packet or the
  503. * request buffer having filled (and maybe overran till end-of-packet).
  504. */
  505. static int
  506. read_fifo(struct pxa25x_ep *ep, struct pxa25x_request *req)
  507. {
  508. u32 udccs;
  509. u8 *buf;
  510. unsigned bufferspace, count, is_short;
  511. for (;;) {
  512. /*
  513. * make sure there's a packet in the FIFO.
  514. * UDCCS_{BO,IO}_RPC are all the same bit value.
  515. * UDCCS_{BO,IO}_RNE are all the same bit value.
  516. */
  517. udccs = readl(ep->reg_udccs);
  518. if (unlikely((udccs & UDCCS_BO_RPC) == 0))
  519. break;
  520. buf = req->req.buf + req->req.actual;
  521. prefetchw(buf);
  522. bufferspace = req->req.length - req->req.actual;
  523. /* read all bytes from this packet */
  524. if (likely(udccs & UDCCS_BO_RNE)) {
  525. count = 1 + (0x0ff & readl(ep->reg_ubcr));
  526. req->req.actual += min(count, bufferspace);
  527. } else /* zlp */
  528. count = 0;
  529. is_short = (count < ep->ep.maxpacket);
  530. debug_cond(NOISY, "read %s %02x, %d bytes%s req %p %d/%d\n",
  531. ep->ep.name, udccs, count,
  532. is_short ? "/S" : "",
  533. req, req->req.actual, req->req.length);
  534. while (likely(count-- != 0)) {
  535. u8 byte = readb(ep->reg_uddr);
  536. if (unlikely(bufferspace == 0)) {
  537. /*
  538. * this happens when the driver's buffer
  539. * is smaller than what the host sent.
  540. * discard the extra data.
  541. */
  542. if (req->req.status != -EOVERFLOW)
  543. printf("%s overflow %d\n",
  544. ep->ep.name, count);
  545. req->req.status = -EOVERFLOW;
  546. } else {
  547. *buf++ = byte;
  548. bufferspace--;
  549. }
  550. }
  551. writel(UDCCS_BO_RPC, ep->reg_udccs);
  552. /* RPC/RSP/RNE could now reflect the other packet buffer */
  553. /* iso is one request per packet */
  554. if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
  555. if (udccs & UDCCS_IO_ROF)
  556. req->req.status = -EHOSTUNREACH;
  557. /* more like "is_done" */
  558. is_short = 1;
  559. }
  560. /* completion */
  561. if (is_short || req->req.actual == req->req.length) {
  562. done(ep, req, 0);
  563. if (list_empty(&ep->queue))
  564. pio_irq_disable(ep->bEndpointAddress);
  565. return 1;
  566. }
  567. /* finished that packet. the next one may be waiting... */
  568. }
  569. return 0;
  570. }
  571. /*
  572. * special ep0 version of the above. no UBCR0 or double buffering; status
  573. * handshaking is magic. most device protocols don't need control-OUT.
  574. * CDC vendor commands (and RNDIS), mass storage CB/CBI, and some other
  575. * protocols do use them.
  576. */
  577. static int
  578. read_ep0_fifo(struct pxa25x_ep *ep, struct pxa25x_request *req)
  579. {
  580. u8 *buf, byte;
  581. unsigned bufferspace;
  582. buf = req->req.buf + req->req.actual;
  583. bufferspace = req->req.length - req->req.actual;
  584. while (readl(&ep->dev->regs->udccs[0]) & UDCCS0_RNE) {
  585. byte = (u8)readb(&ep->dev->regs->uddr0);
  586. if (unlikely(bufferspace == 0)) {
  587. /*
  588. * this happens when the driver's buffer
  589. * is smaller than what the host sent.
  590. * discard the extra data.
  591. */
  592. if (req->req.status != -EOVERFLOW)
  593. printf("%s overflow\n", ep->ep.name);
  594. req->req.status = -EOVERFLOW;
  595. } else {
  596. *buf++ = byte;
  597. req->req.actual++;
  598. bufferspace--;
  599. }
  600. }
  601. writel(UDCCS0_OPR | UDCCS0_IPR, &ep->dev->regs->udccs[0]);
  602. /* completion */
  603. if (req->req.actual >= req->req.length)
  604. return 1;
  605. /* finished that packet. the next one may be waiting... */
  606. return 0;
  607. }
  608. /*-------------------------------------------------------------------------*/
  609. static int
  610. pxa25x_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
  611. {
  612. struct pxa25x_request *req;
  613. struct pxa25x_ep *ep;
  614. struct pxa25x_udc *dev;
  615. unsigned long flags;
  616. req = container_of(_req, struct pxa25x_request, req);
  617. if (unlikely(!_req || !_req->complete || !_req->buf
  618. || !list_empty(&req->queue))) {
  619. printf("%s, bad params\n", __func__);
  620. return -EINVAL;
  621. }
  622. ep = container_of(_ep, struct pxa25x_ep, ep);
  623. if (unlikely(!_ep || (!ep->desc && ep->ep.name != ep0name))) {
  624. printf("%s, bad ep\n", __func__);
  625. return -EINVAL;
  626. }
  627. dev = ep->dev;
  628. if (unlikely(!dev->driver
  629. || dev->gadget.speed == USB_SPEED_UNKNOWN)) {
  630. printf("%s, bogus device state\n", __func__);
  631. return -ESHUTDOWN;
  632. }
  633. /*
  634. * iso is always one packet per request, that's the only way
  635. * we can report per-packet status. that also helps with dma.
  636. */
  637. if (unlikely(ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
  638. && req->req.length >
  639. le16_to_cpu(get_unaligned(&ep->desc->wMaxPacketSize))))
  640. return -EMSGSIZE;
  641. debug_cond(NOISY, "%s queue req %p, len %d buf %p\n",
  642. _ep->name, _req, _req->length, _req->buf);
  643. local_irq_save(flags);
  644. _req->status = -EINPROGRESS;
  645. _req->actual = 0;
  646. /* kickstart this i/o queue? */
  647. if (list_empty(&ep->queue) && !ep->stopped) {
  648. if (ep->desc == NULL/* ep0 */) {
  649. unsigned length = _req->length;
  650. switch (dev->ep0state) {
  651. case EP0_IN_DATA_PHASE:
  652. dev->stats.write.ops++;
  653. if (write_ep0_fifo(ep, req))
  654. req = NULL;
  655. break;
  656. case EP0_OUT_DATA_PHASE:
  657. dev->stats.read.ops++;
  658. /* messy ... */
  659. if (dev->req_config) {
  660. debug("ep0 config ack%s\n",
  661. dev->has_cfr ? "" : " raced");
  662. if (dev->has_cfr)
  663. writel(UDCCFR_AREN|UDCCFR_ACM
  664. |UDCCFR_MB1,
  665. &ep->dev->regs->udccfr);
  666. done(ep, req, 0);
  667. dev->ep0state = EP0_END_XFER;
  668. local_irq_restore(flags);
  669. return 0;
  670. }
  671. if (dev->req_pending)
  672. ep0start(dev, UDCCS0_IPR, "OUT");
  673. if (length == 0 ||
  674. ((readl(
  675. &ep->dev->regs->udccs[0])
  676. & UDCCS0_RNE) != 0
  677. && read_ep0_fifo(ep, req))) {
  678. ep0_idle(dev);
  679. done(ep, req, 0);
  680. req = NULL;
  681. }
  682. break;
  683. default:
  684. printf("ep0 i/o, odd state %d\n",
  685. dev->ep0state);
  686. local_irq_restore(flags);
  687. return -EL2HLT;
  688. }
  689. /* can the FIFO can satisfy the request immediately? */
  690. } else if ((ep->bEndpointAddress & USB_DIR_IN) != 0) {
  691. if ((readl(ep->reg_udccs) & UDCCS_BI_TFS) != 0
  692. && write_fifo(ep, req))
  693. req = NULL;
  694. } else if ((readl(ep->reg_udccs) & UDCCS_BO_RFS) != 0
  695. && read_fifo(ep, req)) {
  696. req = NULL;
  697. }
  698. if (likely(req && ep->desc))
  699. pio_irq_enable(ep->bEndpointAddress);
  700. }
  701. /* pio or dma irq handler advances the queue. */
  702. if (likely(req != NULL))
  703. list_add_tail(&req->queue, &ep->queue);
  704. local_irq_restore(flags);
  705. return 0;
  706. }
  707. /*
  708. * nuke - dequeue ALL requests
  709. */
  710. static void nuke(struct pxa25x_ep *ep, int status)
  711. {
  712. struct pxa25x_request *req;
  713. /* called with irqs blocked */
  714. while (!list_empty(&ep->queue)) {
  715. req = list_entry(ep->queue.next,
  716. struct pxa25x_request,
  717. queue);
  718. done(ep, req, status);
  719. }
  720. if (ep->desc)
  721. pio_irq_disable(ep->bEndpointAddress);
  722. }
  723. /* dequeue JUST ONE request */
  724. static int pxa25x_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
  725. {
  726. struct pxa25x_ep *ep;
  727. struct pxa25x_request *req;
  728. unsigned long flags;
  729. ep = container_of(_ep, struct pxa25x_ep, ep);
  730. if (!_ep || ep->ep.name == ep0name)
  731. return -EINVAL;
  732. local_irq_save(flags);
  733. /* make sure it's actually queued on this endpoint */
  734. list_for_each_entry(req, &ep->queue, queue) {
  735. if (&req->req == _req)
  736. break;
  737. }
  738. if (&req->req != _req) {
  739. local_irq_restore(flags);
  740. return -EINVAL;
  741. }
  742. done(ep, req, -ECONNRESET);
  743. local_irq_restore(flags);
  744. return 0;
  745. }
  746. /*-------------------------------------------------------------------------*/
  747. static int pxa25x_ep_set_halt(struct usb_ep *_ep, int value)
  748. {
  749. struct pxa25x_ep *ep;
  750. unsigned long flags;
  751. ep = container_of(_ep, struct pxa25x_ep, ep);
  752. if (unlikely(!_ep
  753. || (!ep->desc && ep->ep.name != ep0name))
  754. || ep->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
  755. printf("%s, bad ep\n", __func__);
  756. return -EINVAL;
  757. }
  758. if (value == 0) {
  759. /*
  760. * this path (reset toggle+halt) is needed to implement
  761. * SET_INTERFACE on normal hardware. but it can't be
  762. * done from software on the PXA UDC, and the hardware
  763. * forgets to do it as part of SET_INTERFACE automagic.
  764. */
  765. printf("only host can clear %s halt\n", _ep->name);
  766. return -EROFS;
  767. }
  768. local_irq_save(flags);
  769. if ((ep->bEndpointAddress & USB_DIR_IN) != 0
  770. && ((readl(ep->reg_udccs) & UDCCS_BI_TFS) == 0
  771. || !list_empty(&ep->queue))) {
  772. local_irq_restore(flags);
  773. return -EAGAIN;
  774. }
  775. /* FST bit is the same for control, bulk in, bulk out, interrupt in */
  776. writel(UDCCS_BI_FST|UDCCS_BI_FTF, ep->reg_udccs);
  777. /* ep0 needs special care */
  778. if (!ep->desc) {
  779. start_watchdog(ep->dev);
  780. ep->dev->req_pending = 0;
  781. ep->dev->ep0state = EP0_STALL;
  782. /* and bulk/intr endpoints like dropping stalls too */
  783. } else {
  784. unsigned i;
  785. for (i = 0; i < 1000; i += 20) {
  786. if (readl(ep->reg_udccs) & UDCCS_BI_SST)
  787. break;
  788. udelay(20);
  789. }
  790. }
  791. local_irq_restore(flags);
  792. debug("%s halt\n", _ep->name);
  793. return 0;
  794. }
  795. static int pxa25x_ep_fifo_status(struct usb_ep *_ep)
  796. {
  797. struct pxa25x_ep *ep;
  798. ep = container_of(_ep, struct pxa25x_ep, ep);
  799. if (!_ep) {
  800. printf("%s, bad ep\n", __func__);
  801. return -ENODEV;
  802. }
  803. /* pxa can't report unclaimed bytes from IN fifos */
  804. if ((ep->bEndpointAddress & USB_DIR_IN) != 0)
  805. return -EOPNOTSUPP;
  806. if (ep->dev->gadget.speed == USB_SPEED_UNKNOWN
  807. || (readl(ep->reg_udccs) & UDCCS_BO_RFS) == 0)
  808. return 0;
  809. else
  810. return (readl(ep->reg_ubcr) & 0xfff) + 1;
  811. }
  812. static void pxa25x_ep_fifo_flush(struct usb_ep *_ep)
  813. {
  814. struct pxa25x_ep *ep;
  815. ep = container_of(_ep, struct pxa25x_ep, ep);
  816. if (!_ep || ep->ep.name == ep0name || !list_empty(&ep->queue)) {
  817. printf("%s, bad ep\n", __func__);
  818. return;
  819. }
  820. /* toggle and halt bits stay unchanged */
  821. /* for OUT, just read and discard the FIFO contents. */
  822. if ((ep->bEndpointAddress & USB_DIR_IN) == 0) {
  823. while (((readl(ep->reg_udccs)) & UDCCS_BO_RNE) != 0)
  824. (void)readb(ep->reg_uddr);
  825. return;
  826. }
  827. /* most IN status is the same, but ISO can't stall */
  828. writel(UDCCS_BI_TPC|UDCCS_BI_FTF|UDCCS_BI_TUR
  829. | (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
  830. ? 0 : UDCCS_BI_SST), ep->reg_udccs);
  831. }
  832. static struct usb_ep_ops pxa25x_ep_ops = {
  833. .enable = pxa25x_ep_enable,
  834. .disable = pxa25x_ep_disable,
  835. .alloc_request = pxa25x_ep_alloc_request,
  836. .free_request = pxa25x_ep_free_request,
  837. .queue = pxa25x_ep_queue,
  838. .dequeue = pxa25x_ep_dequeue,
  839. .set_halt = pxa25x_ep_set_halt,
  840. .fifo_status = pxa25x_ep_fifo_status,
  841. .fifo_flush = pxa25x_ep_fifo_flush,
  842. };
  843. /* ---------------------------------------------------------------------------
  844. * device-scoped parts of the api to the usb controller hardware
  845. * ---------------------------------------------------------------------------
  846. */
  847. static int pxa25x_udc_get_frame(struct usb_gadget *_gadget)
  848. {
  849. return ((readl(&the_controller->regs->ufnrh) & 0x07) << 8) |
  850. (readl(&the_controller->regs->ufnrl) & 0xff);
  851. }
  852. static int pxa25x_udc_wakeup(struct usb_gadget *_gadget)
  853. {
  854. /* host may not have enabled remote wakeup */
  855. if ((readl(&the_controller->regs->udccs[0]) & UDCCS0_DRWF) == 0)
  856. return -EHOSTUNREACH;
  857. udc_set_mask_UDCCR(UDCCR_RSM);
  858. return 0;
  859. }
  860. static void stop_activity(struct pxa25x_udc *, struct usb_gadget_driver *);
  861. static void udc_enable(struct pxa25x_udc *);
  862. static void udc_disable(struct pxa25x_udc *);
  863. /*
  864. * We disable the UDC -- and its 48 MHz clock -- whenever it's not
  865. * in active use.
  866. */
  867. static int pullup(struct pxa25x_udc *udc)
  868. {
  869. if (udc->pullup)
  870. pullup_on();
  871. else
  872. pullup_off();
  873. int is_active = udc->pullup;
  874. if (is_active) {
  875. if (!udc->active) {
  876. udc->active = 1;
  877. udc_enable(udc);
  878. }
  879. } else {
  880. if (udc->active) {
  881. if (udc->gadget.speed != USB_SPEED_UNKNOWN)
  882. stop_activity(udc, udc->driver);
  883. udc_disable(udc);
  884. udc->active = 0;
  885. }
  886. }
  887. return 0;
  888. }
  889. /* VBUS reporting logically comes from a transceiver */
  890. static int pxa25x_udc_vbus_session(struct usb_gadget *_gadget, int is_active)
  891. {
  892. struct pxa25x_udc *udc;
  893. udc = container_of(_gadget, struct pxa25x_udc, gadget);
  894. printf("vbus %s\n", is_active ? "supplied" : "inactive");
  895. pullup(udc);
  896. return 0;
  897. }
  898. /* drivers may have software control over D+ pullup */
  899. static int pxa25x_udc_pullup(struct usb_gadget *_gadget, int is_active)
  900. {
  901. struct pxa25x_udc *udc;
  902. udc = container_of(_gadget, struct pxa25x_udc, gadget);
  903. /* not all boards support pullup control */
  904. if (!udc->mach->udc_command)
  905. return -EOPNOTSUPP;
  906. udc->pullup = (is_active != 0);
  907. pullup(udc);
  908. return 0;
  909. }
  910. /*
  911. * boards may consume current from VBUS, up to 100-500mA based on config.
  912. * the 500uA suspend ceiling means that exclusively vbus-powered PXA designs
  913. * violate USB specs.
  914. */
  915. static int pxa25x_udc_vbus_draw(struct usb_gadget *_gadget, unsigned mA)
  916. {
  917. return -EOPNOTSUPP;
  918. }
  919. static const struct usb_gadget_ops pxa25x_udc_ops = {
  920. .get_frame = pxa25x_udc_get_frame,
  921. .wakeup = pxa25x_udc_wakeup,
  922. .vbus_session = pxa25x_udc_vbus_session,
  923. .pullup = pxa25x_udc_pullup,
  924. .vbus_draw = pxa25x_udc_vbus_draw,
  925. };
  926. /*-------------------------------------------------------------------------*/
  927. /*
  928. * udc_disable - disable USB device controller
  929. */
  930. static void udc_disable(struct pxa25x_udc *dev)
  931. {
  932. /* block all irqs */
  933. udc_set_mask_UDCCR(UDCCR_SRM|UDCCR_REM);
  934. writel(0xff, &dev->regs->uicr0);
  935. writel(0xff, &dev->regs->uicr1);
  936. writel(UFNRH_SIM, &dev->regs->ufnrh);
  937. /* if hardware supports it, disconnect from usb */
  938. pullup_off();
  939. udc_clear_mask_UDCCR(UDCCR_UDE);
  940. ep0_idle(dev);
  941. dev->gadget.speed = USB_SPEED_UNKNOWN;
  942. }
  943. /*
  944. * udc_reinit - initialize software state
  945. */
  946. static void udc_reinit(struct pxa25x_udc *dev)
  947. {
  948. u32 i;
  949. /* device/ep0 records init */
  950. INIT_LIST_HEAD(&dev->gadget.ep_list);
  951. INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
  952. dev->ep0state = EP0_IDLE;
  953. /* basic endpoint records init */
  954. for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
  955. struct pxa25x_ep *ep = &dev->ep[i];
  956. if (i != 0)
  957. list_add_tail(&ep->ep.ep_list, &dev->gadget.ep_list);
  958. ep->desc = NULL;
  959. ep->stopped = 0;
  960. INIT_LIST_HEAD(&ep->queue);
  961. ep->pio_irqs = 0;
  962. }
  963. /* the rest was statically initialized, and is read-only */
  964. }
  965. /*
  966. * until it's enabled, this UDC should be completely invisible
  967. * to any USB host.
  968. */
  969. static void udc_enable(struct pxa25x_udc *dev)
  970. {
  971. debug("udc: enabling udc\n");
  972. udc_clear_mask_UDCCR(UDCCR_UDE);
  973. /*
  974. * Try to clear these bits before we enable the udc.
  975. * Do not touch reset ack bit, we would take care of it in
  976. * interrupt handle routine
  977. */
  978. udc_ack_int_UDCCR(UDCCR_SUSIR|UDCCR_RESIR);
  979. ep0_idle(dev);
  980. dev->gadget.speed = USB_SPEED_UNKNOWN;
  981. dev->stats.irqs = 0;
  982. /*
  983. * sequence taken from chapter 12.5.10, PXA250 AppProcDevManual:
  984. * - enable UDC
  985. * - if RESET is already in progress, ack interrupt
  986. * - unmask reset interrupt
  987. */
  988. udc_set_mask_UDCCR(UDCCR_UDE);
  989. if (!(readl(&dev->regs->udccr) & UDCCR_UDA))
  990. udc_ack_int_UDCCR(UDCCR_RSTIR);
  991. if (dev->has_cfr /* UDC_RES2 is defined */) {
  992. /*
  993. * pxa255 (a0+) can avoid a set_config race that could
  994. * prevent gadget drivers from configuring correctly
  995. */
  996. writel(UDCCFR_ACM | UDCCFR_MB1, &dev->regs->udccfr);
  997. }
  998. /* enable suspend/resume and reset irqs */
  999. udc_clear_mask_UDCCR(UDCCR_SRM | UDCCR_REM);
  1000. /* enable ep0 irqs */
  1001. clrbits_le32(&dev->regs->uicr0, UICR0_IM0);
  1002. /* if hardware supports it, pullup D+ and wait for reset */
  1003. pullup_on();
  1004. }
  1005. static inline void clear_ep_state(struct pxa25x_udc *dev)
  1006. {
  1007. unsigned i;
  1008. /*
  1009. * hardware SET_{CONFIGURATION,INTERFACE} automagic resets endpoint
  1010. * fifos, and pending transactions mustn't be continued in any case.
  1011. */
  1012. for (i = 1; i < PXA_UDC_NUM_ENDPOINTS; i++)
  1013. nuke(&dev->ep[i], -ECONNABORTED);
  1014. }
  1015. static void handle_ep0(struct pxa25x_udc *dev)
  1016. {
  1017. u32 udccs0 = readl(&dev->regs->udccs[0]);
  1018. struct pxa25x_ep *ep = &dev->ep[0];
  1019. struct pxa25x_request *req;
  1020. union {
  1021. struct usb_ctrlrequest r;
  1022. u8 raw[8];
  1023. u32 word[2];
  1024. } u;
  1025. if (list_empty(&ep->queue))
  1026. req = NULL;
  1027. else
  1028. req = list_entry(ep->queue.next, struct pxa25x_request, queue);
  1029. /* clear stall status */
  1030. if (udccs0 & UDCCS0_SST) {
  1031. nuke(ep, -EPIPE);
  1032. writel(UDCCS0_SST, &dev->regs->udccs[0]);
  1033. stop_watchdog(dev);
  1034. ep0_idle(dev);
  1035. }
  1036. /* previous request unfinished? non-error iff back-to-back ... */
  1037. if ((udccs0 & UDCCS0_SA) != 0 && dev->ep0state != EP0_IDLE) {
  1038. nuke(ep, 0);
  1039. stop_watchdog(dev);
  1040. ep0_idle(dev);
  1041. }
  1042. switch (dev->ep0state) {
  1043. case EP0_IDLE:
  1044. /* late-breaking status? */
  1045. udccs0 = readl(&dev->regs->udccs[0]);
  1046. /* start control request? */
  1047. if (likely((udccs0 & (UDCCS0_OPR|UDCCS0_SA|UDCCS0_RNE))
  1048. == (UDCCS0_OPR|UDCCS0_SA|UDCCS0_RNE))) {
  1049. int i;
  1050. nuke(ep, -EPROTO);
  1051. /* read SETUP packet */
  1052. for (i = 0; i < 8; i++) {
  1053. if (unlikely(!(readl(&dev->regs->udccs[0]) &
  1054. UDCCS0_RNE))) {
  1055. bad_setup:
  1056. debug("SETUP %d!\n", i);
  1057. goto stall;
  1058. }
  1059. u.raw[i] = (u8)readb(&dev->regs->uddr0);
  1060. }
  1061. if (unlikely((readl(&dev->regs->udccs[0]) &
  1062. UDCCS0_RNE) != 0))
  1063. goto bad_setup;
  1064. got_setup:
  1065. debug("SETUP %02x.%02x v%04x i%04x l%04x\n",
  1066. u.r.bRequestType, u.r.bRequest,
  1067. le16_to_cpu(u.r.wValue),
  1068. le16_to_cpu(u.r.wIndex),
  1069. le16_to_cpu(u.r.wLength));
  1070. /* cope with automagic for some standard requests. */
  1071. dev->req_std = (u.r.bRequestType & USB_TYPE_MASK)
  1072. == USB_TYPE_STANDARD;
  1073. dev->req_config = 0;
  1074. dev->req_pending = 1;
  1075. switch (u.r.bRequest) {
  1076. /* hardware restricts gadget drivers here! */
  1077. case USB_REQ_SET_CONFIGURATION:
  1078. debug("GOT SET_CONFIGURATION\n");
  1079. if (u.r.bRequestType == USB_RECIP_DEVICE) {
  1080. /*
  1081. * reflect hardware's automagic
  1082. * up to the gadget driver.
  1083. */
  1084. config_change:
  1085. dev->req_config = 1;
  1086. clear_ep_state(dev);
  1087. /*
  1088. * if !has_cfr, there's no synch
  1089. * else use AREN (later) not SA|OPR
  1090. * USIR0_IR0 acts edge sensitive
  1091. */
  1092. }
  1093. break;
  1094. /* ... and here, even more ... */
  1095. case USB_REQ_SET_INTERFACE:
  1096. if (u.r.bRequestType == USB_RECIP_INTERFACE) {
  1097. /*
  1098. * udc hardware is broken by design:
  1099. * - altsetting may only be zero;
  1100. * - hw resets all interfaces' eps;
  1101. * - ep reset doesn't include halt(?).
  1102. */
  1103. printf("broken set_interface (%d/%d)\n",
  1104. le16_to_cpu(u.r.wIndex),
  1105. le16_to_cpu(u.r.wValue));
  1106. goto config_change;
  1107. }
  1108. break;
  1109. /* hardware was supposed to hide this */
  1110. case USB_REQ_SET_ADDRESS:
  1111. debug("GOT SET ADDRESS\n");
  1112. if (u.r.bRequestType == USB_RECIP_DEVICE) {
  1113. ep0start(dev, 0, "address");
  1114. return;
  1115. }
  1116. break;
  1117. }
  1118. if (u.r.bRequestType & USB_DIR_IN)
  1119. dev->ep0state = EP0_IN_DATA_PHASE;
  1120. else
  1121. dev->ep0state = EP0_OUT_DATA_PHASE;
  1122. i = dev->driver->setup(&dev->gadget, &u.r);
  1123. if (i < 0) {
  1124. /* hardware automagic preventing STALL... */
  1125. if (dev->req_config) {
  1126. /*
  1127. * hardware sometimes neglects to tell
  1128. * tell us about config change events,
  1129. * so later ones may fail...
  1130. */
  1131. printf("config change %02x fail %d?\n",
  1132. u.r.bRequest, i);
  1133. return;
  1134. /*
  1135. * TODO experiment: if has_cfr,
  1136. * hardware didn't ACK; maybe we
  1137. * could actually STALL!
  1138. */
  1139. }
  1140. if (0) {
  1141. stall:
  1142. /* uninitialized when goto stall */
  1143. i = 0;
  1144. }
  1145. debug("protocol STALL, "
  1146. "%02x err %d\n",
  1147. readl(&dev->regs->udccs[0]), i);
  1148. /*
  1149. * the watchdog timer helps deal with cases
  1150. * where udc seems to clear FST wrongly, and
  1151. * then NAKs instead of STALLing.
  1152. */
  1153. ep0start(dev, UDCCS0_FST|UDCCS0_FTF, "stall");
  1154. start_watchdog(dev);
  1155. dev->ep0state = EP0_STALL;
  1156. /* deferred i/o == no response yet */
  1157. } else if (dev->req_pending) {
  1158. if (likely(dev->ep0state == EP0_IN_DATA_PHASE
  1159. || dev->req_std || u.r.wLength))
  1160. ep0start(dev, 0, "defer");
  1161. else
  1162. ep0start(dev, UDCCS0_IPR, "defer/IPR");
  1163. }
  1164. /* expect at least one data or status stage irq */
  1165. return;
  1166. } else if (likely((udccs0 & (UDCCS0_OPR|UDCCS0_SA))
  1167. == (UDCCS0_OPR|UDCCS0_SA))) {
  1168. unsigned i;
  1169. /*
  1170. * pxa210/250 erratum 131 for B0/B1 says RNE lies.
  1171. * still observed on a pxa255 a0.
  1172. */
  1173. debug("e131\n");
  1174. nuke(ep, -EPROTO);
  1175. /* read SETUP data, but don't trust it too much */
  1176. for (i = 0; i < 8; i++)
  1177. u.raw[i] = (u8)readb(&dev->regs->uddr0);
  1178. if ((u.r.bRequestType & USB_RECIP_MASK)
  1179. > USB_RECIP_OTHER)
  1180. goto stall;
  1181. if (u.word[0] == 0 && u.word[1] == 0)
  1182. goto stall;
  1183. goto got_setup;
  1184. } else {
  1185. /*
  1186. * some random early IRQ:
  1187. * - we acked FST
  1188. * - IPR cleared
  1189. * - OPR got set, without SA (likely status stage)
  1190. */
  1191. debug("random IRQ %X %X\n", udccs0,
  1192. readl(&dev->regs->udccs[0]));
  1193. writel(udccs0 & (UDCCS0_SA|UDCCS0_OPR),
  1194. &dev->regs->udccs[0]);
  1195. }
  1196. break;
  1197. case EP0_IN_DATA_PHASE: /* GET_DESCRIPTOR etc */
  1198. if (udccs0 & UDCCS0_OPR) {
  1199. debug("ep0in premature status\n");
  1200. if (req)
  1201. done(ep, req, 0);
  1202. ep0_idle(dev);
  1203. } else /* irq was IPR clearing */ {
  1204. if (req) {
  1205. debug("next ep0 in packet\n");
  1206. /* this IN packet might finish the request */
  1207. (void) write_ep0_fifo(ep, req);
  1208. } /* else IN token before response was written */
  1209. }
  1210. break;
  1211. case EP0_OUT_DATA_PHASE: /* SET_DESCRIPTOR etc */
  1212. if (udccs0 & UDCCS0_OPR) {
  1213. if (req) {
  1214. /* this OUT packet might finish the request */
  1215. if (read_ep0_fifo(ep, req))
  1216. done(ep, req, 0);
  1217. /* else more OUT packets expected */
  1218. } /* else OUT token before read was issued */
  1219. } else /* irq was IPR clearing */ {
  1220. debug("ep0out premature status\n");
  1221. if (req)
  1222. done(ep, req, 0);
  1223. ep0_idle(dev);
  1224. }
  1225. break;
  1226. case EP0_END_XFER:
  1227. if (req)
  1228. done(ep, req, 0);
  1229. /*
  1230. * ack control-IN status (maybe in-zlp was skipped)
  1231. * also appears after some config change events.
  1232. */
  1233. if (udccs0 & UDCCS0_OPR)
  1234. writel(UDCCS0_OPR, &dev->regs->udccs[0]);
  1235. ep0_idle(dev);
  1236. break;
  1237. case EP0_STALL:
  1238. writel(UDCCS0_FST, &dev->regs->udccs[0]);
  1239. break;
  1240. }
  1241. writel(USIR0_IR0, &dev->regs->usir0);
  1242. }
  1243. static void handle_ep(struct pxa25x_ep *ep)
  1244. {
  1245. struct pxa25x_request *req;
  1246. int is_in = ep->bEndpointAddress & USB_DIR_IN;
  1247. int completed;
  1248. u32 udccs, tmp;
  1249. do {
  1250. completed = 0;
  1251. if (likely(!list_empty(&ep->queue)))
  1252. req = list_entry(ep->queue.next,
  1253. struct pxa25x_request, queue);
  1254. else
  1255. req = NULL;
  1256. /* TODO check FST handling */
  1257. udccs = readl(ep->reg_udccs);
  1258. if (unlikely(is_in)) { /* irq from TPC, SST, or (ISO) TUR */
  1259. tmp = UDCCS_BI_TUR;
  1260. if (likely(ep->bmAttributes == USB_ENDPOINT_XFER_BULK))
  1261. tmp |= UDCCS_BI_SST;
  1262. tmp &= udccs;
  1263. if (likely(tmp))
  1264. writel(tmp, ep->reg_udccs);
  1265. if (req && likely((udccs & UDCCS_BI_TFS) != 0))
  1266. completed = write_fifo(ep, req);
  1267. } else { /* irq from RPC (or for ISO, ROF) */
  1268. if (likely(ep->bmAttributes == USB_ENDPOINT_XFER_BULK))
  1269. tmp = UDCCS_BO_SST | UDCCS_BO_DME;
  1270. else
  1271. tmp = UDCCS_IO_ROF | UDCCS_IO_DME;
  1272. tmp &= udccs;
  1273. if (likely(tmp))
  1274. writel(tmp, ep->reg_udccs);
  1275. /* fifos can hold packets, ready for reading... */
  1276. if (likely(req))
  1277. completed = read_fifo(ep, req);
  1278. else
  1279. pio_irq_disable(ep->bEndpointAddress);
  1280. }
  1281. ep->pio_irqs++;
  1282. } while (completed);
  1283. }
  1284. /*
  1285. * pxa25x_udc_irq - interrupt handler
  1286. *
  1287. * avoid delays in ep0 processing. the control handshaking isn't always
  1288. * under software control (pxa250c0 and the pxa255 are better), and delays
  1289. * could cause usb protocol errors.
  1290. */
  1291. static struct pxa25x_udc memory;
  1292. static int
  1293. pxa25x_udc_irq(void)
  1294. {
  1295. struct pxa25x_udc *dev = &memory;
  1296. int handled;
  1297. test_watchdog(dev);
  1298. dev->stats.irqs++;
  1299. do {
  1300. u32 udccr = readl(&dev->regs->udccr);
  1301. handled = 0;
  1302. /* SUSpend Interrupt Request */
  1303. if (unlikely(udccr & UDCCR_SUSIR)) {
  1304. udc_ack_int_UDCCR(UDCCR_SUSIR);
  1305. handled = 1;
  1306. debug("USB suspend\n");
  1307. if (dev->gadget.speed != USB_SPEED_UNKNOWN
  1308. && dev->driver
  1309. && dev->driver->suspend)
  1310. dev->driver->suspend(&dev->gadget);
  1311. ep0_idle(dev);
  1312. }
  1313. /* RESume Interrupt Request */
  1314. if (unlikely(udccr & UDCCR_RESIR)) {
  1315. udc_ack_int_UDCCR(UDCCR_RESIR);
  1316. handled = 1;
  1317. debug("USB resume\n");
  1318. if (dev->gadget.speed != USB_SPEED_UNKNOWN
  1319. && dev->driver
  1320. && dev->driver->resume)
  1321. dev->driver->resume(&dev->gadget);
  1322. }
  1323. /* ReSeT Interrupt Request - USB reset */
  1324. if (unlikely(udccr & UDCCR_RSTIR)) {
  1325. udc_ack_int_UDCCR(UDCCR_RSTIR);
  1326. handled = 1;
  1327. if ((readl(&dev->regs->udccr) & UDCCR_UDA) == 0) {
  1328. debug("USB reset start\n");
  1329. /*
  1330. * reset driver and endpoints,
  1331. * in case that's not yet done
  1332. */
  1333. stop_activity(dev, dev->driver);
  1334. } else {
  1335. debug("USB reset end\n");
  1336. dev->gadget.speed = USB_SPEED_FULL;
  1337. memset(&dev->stats, 0, sizeof dev->stats);
  1338. /* driver and endpoints are still reset */
  1339. }
  1340. } else {
  1341. u32 uicr0 = readl(&dev->regs->uicr0);
  1342. u32 uicr1 = readl(&dev->regs->uicr1);
  1343. u32 usir0 = readl(&dev->regs->usir0);
  1344. u32 usir1 = readl(&dev->regs->usir1);
  1345. usir0 = usir0 & ~uicr0;
  1346. usir1 = usir1 & ~uicr1;
  1347. int i;
  1348. if (unlikely(!usir0 && !usir1))
  1349. continue;
  1350. debug_cond(NOISY, "irq %02x.%02x\n", usir1, usir0);
  1351. /* control traffic */
  1352. if (usir0 & USIR0_IR0) {
  1353. dev->ep[0].pio_irqs++;
  1354. handle_ep0(dev);
  1355. handled = 1;
  1356. }
  1357. /* endpoint data transfers */
  1358. for (i = 0; i < 8; i++) {
  1359. u32 tmp = 1 << i;
  1360. if (i && (usir0 & tmp)) {
  1361. handle_ep(&dev->ep[i]);
  1362. setbits_le32(&dev->regs->usir0, tmp);
  1363. handled = 1;
  1364. }
  1365. #ifndef CONFIG_USB_PXA25X_SMALL
  1366. if (usir1 & tmp) {
  1367. handle_ep(&dev->ep[i+8]);
  1368. setbits_le32(&dev->regs->usir1, tmp);
  1369. handled = 1;
  1370. }
  1371. #endif
  1372. }
  1373. }
  1374. /* we could also ask for 1 msec SOF (SIR) interrupts */
  1375. } while (handled);
  1376. return IRQ_HANDLED;
  1377. }
  1378. /*-------------------------------------------------------------------------*/
  1379. /*
  1380. * this uses load-time allocation and initialization (instead of
  1381. * doing it at run-time) to save code, eliminate fault paths, and
  1382. * be more obviously correct.
  1383. */
  1384. static struct pxa25x_udc memory = {
  1385. .regs = UDC_REGS,
  1386. .gadget = {
  1387. .ops = &pxa25x_udc_ops,
  1388. .ep0 = &memory.ep[0].ep,
  1389. .name = driver_name,
  1390. },
  1391. /* control endpoint */
  1392. .ep[0] = {
  1393. .ep = {
  1394. .name = ep0name,
  1395. .ops = &pxa25x_ep_ops,
  1396. .maxpacket = EP0_FIFO_SIZE,
  1397. },
  1398. .dev = &memory,
  1399. .reg_udccs = &UDC_REGS->udccs[0],
  1400. .reg_uddr = &UDC_REGS->uddr0,
  1401. },
  1402. /* first group of endpoints */
  1403. .ep[1] = {
  1404. .ep = {
  1405. .name = "ep1in-bulk",
  1406. .ops = &pxa25x_ep_ops,
  1407. .maxpacket = BULK_FIFO_SIZE,
  1408. },
  1409. .dev = &memory,
  1410. .fifo_size = BULK_FIFO_SIZE,
  1411. .bEndpointAddress = USB_DIR_IN | 1,
  1412. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1413. .reg_udccs = &UDC_REGS->udccs[1],
  1414. .reg_uddr = &UDC_REGS->uddr1,
  1415. },
  1416. .ep[2] = {
  1417. .ep = {
  1418. .name = "ep2out-bulk",
  1419. .ops = &pxa25x_ep_ops,
  1420. .maxpacket = BULK_FIFO_SIZE,
  1421. },
  1422. .dev = &memory,
  1423. .fifo_size = BULK_FIFO_SIZE,
  1424. .bEndpointAddress = 2,
  1425. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1426. .reg_udccs = &UDC_REGS->udccs[2],
  1427. .reg_ubcr = &UDC_REGS->ubcr2,
  1428. .reg_uddr = &UDC_REGS->uddr2,
  1429. },
  1430. #ifndef CONFIG_USB_PXA25X_SMALL
  1431. .ep[3] = {
  1432. .ep = {
  1433. .name = "ep3in-iso",
  1434. .ops = &pxa25x_ep_ops,
  1435. .maxpacket = ISO_FIFO_SIZE,
  1436. },
  1437. .dev = &memory,
  1438. .fifo_size = ISO_FIFO_SIZE,
  1439. .bEndpointAddress = USB_DIR_IN | 3,
  1440. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  1441. .reg_udccs = &UDC_REGS->udccs[3],
  1442. .reg_uddr = &UDC_REGS->uddr3,
  1443. },
  1444. .ep[4] = {
  1445. .ep = {
  1446. .name = "ep4out-iso",
  1447. .ops = &pxa25x_ep_ops,
  1448. .maxpacket = ISO_FIFO_SIZE,
  1449. },
  1450. .dev = &memory,
  1451. .fifo_size = ISO_FIFO_SIZE,
  1452. .bEndpointAddress = 4,
  1453. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  1454. .reg_udccs = &UDC_REGS->udccs[4],
  1455. .reg_ubcr = &UDC_REGS->ubcr4,
  1456. .reg_uddr = &UDC_REGS->uddr4,
  1457. },
  1458. .ep[5] = {
  1459. .ep = {
  1460. .name = "ep5in-int",
  1461. .ops = &pxa25x_ep_ops,
  1462. .maxpacket = INT_FIFO_SIZE,
  1463. },
  1464. .dev = &memory,
  1465. .fifo_size = INT_FIFO_SIZE,
  1466. .bEndpointAddress = USB_DIR_IN | 5,
  1467. .bmAttributes = USB_ENDPOINT_XFER_INT,
  1468. .reg_udccs = &UDC_REGS->udccs[5],
  1469. .reg_uddr = &UDC_REGS->uddr5,
  1470. },
  1471. /* second group of endpoints */
  1472. .ep[6] = {
  1473. .ep = {
  1474. .name = "ep6in-bulk",
  1475. .ops = &pxa25x_ep_ops,
  1476. .maxpacket = BULK_FIFO_SIZE,
  1477. },
  1478. .dev = &memory,
  1479. .fifo_size = BULK_FIFO_SIZE,
  1480. .bEndpointAddress = USB_DIR_IN | 6,
  1481. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1482. .reg_udccs = &UDC_REGS->udccs[6],
  1483. .reg_uddr = &UDC_REGS->uddr6,
  1484. },
  1485. .ep[7] = {
  1486. .ep = {
  1487. .name = "ep7out-bulk",
  1488. .ops = &pxa25x_ep_ops,
  1489. .maxpacket = BULK_FIFO_SIZE,
  1490. },
  1491. .dev = &memory,
  1492. .fifo_size = BULK_FIFO_SIZE,
  1493. .bEndpointAddress = 7,
  1494. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1495. .reg_udccs = &UDC_REGS->udccs[7],
  1496. .reg_ubcr = &UDC_REGS->ubcr7,
  1497. .reg_uddr = &UDC_REGS->uddr7,
  1498. },
  1499. .ep[8] = {
  1500. .ep = {
  1501. .name = "ep8in-iso",
  1502. .ops = &pxa25x_ep_ops,
  1503. .maxpacket = ISO_FIFO_SIZE,
  1504. },
  1505. .dev = &memory,
  1506. .fifo_size = ISO_FIFO_SIZE,
  1507. .bEndpointAddress = USB_DIR_IN | 8,
  1508. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  1509. .reg_udccs = &UDC_REGS->udccs[8],
  1510. .reg_uddr = &UDC_REGS->uddr8,
  1511. },
  1512. .ep[9] = {
  1513. .ep = {
  1514. .name = "ep9out-iso",
  1515. .ops = &pxa25x_ep_ops,
  1516. .maxpacket = ISO_FIFO_SIZE,
  1517. },
  1518. .dev = &memory,
  1519. .fifo_size = ISO_FIFO_SIZE,
  1520. .bEndpointAddress = 9,
  1521. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  1522. .reg_udccs = &UDC_REGS->udccs[9],
  1523. .reg_ubcr = &UDC_REGS->ubcr9,
  1524. .reg_uddr = &UDC_REGS->uddr9,
  1525. },
  1526. .ep[10] = {
  1527. .ep = {
  1528. .name = "ep10in-int",
  1529. .ops = &pxa25x_ep_ops,
  1530. .maxpacket = INT_FIFO_SIZE,
  1531. },
  1532. .dev = &memory,
  1533. .fifo_size = INT_FIFO_SIZE,
  1534. .bEndpointAddress = USB_DIR_IN | 10,
  1535. .bmAttributes = USB_ENDPOINT_XFER_INT,
  1536. .reg_udccs = &UDC_REGS->udccs[10],
  1537. .reg_uddr = &UDC_REGS->uddr10,
  1538. },
  1539. /* third group of endpoints */
  1540. .ep[11] = {
  1541. .ep = {
  1542. .name = "ep11in-bulk",
  1543. .ops = &pxa25x_ep_ops,
  1544. .maxpacket = BULK_FIFO_SIZE,
  1545. },
  1546. .dev = &memory,
  1547. .fifo_size = BULK_FIFO_SIZE,
  1548. .bEndpointAddress = USB_DIR_IN | 11,
  1549. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1550. .reg_udccs = &UDC_REGS->udccs[11],
  1551. .reg_uddr = &UDC_REGS->uddr11,
  1552. },
  1553. .ep[12] = {
  1554. .ep = {
  1555. .name = "ep12out-bulk",
  1556. .ops = &pxa25x_ep_ops,
  1557. .maxpacket = BULK_FIFO_SIZE,
  1558. },
  1559. .dev = &memory,
  1560. .fifo_size = BULK_FIFO_SIZE,
  1561. .bEndpointAddress = 12,
  1562. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1563. .reg_udccs = &UDC_REGS->udccs[12],
  1564. .reg_ubcr = &UDC_REGS->ubcr12,
  1565. .reg_uddr = &UDC_REGS->uddr12,
  1566. },
  1567. .ep[13] = {
  1568. .ep = {
  1569. .name = "ep13in-iso",
  1570. .ops = &pxa25x_ep_ops,
  1571. .maxpacket = ISO_FIFO_SIZE,
  1572. },
  1573. .dev = &memory,
  1574. .fifo_size = ISO_FIFO_SIZE,
  1575. .bEndpointAddress = USB_DIR_IN | 13,
  1576. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  1577. .reg_udccs = &UDC_REGS->udccs[13],
  1578. .reg_uddr = &UDC_REGS->uddr13,
  1579. },
  1580. .ep[14] = {
  1581. .ep = {
  1582. .name = "ep14out-iso",
  1583. .ops = &pxa25x_ep_ops,
  1584. .maxpacket = ISO_FIFO_SIZE,
  1585. },
  1586. .dev = &memory,
  1587. .fifo_size = ISO_FIFO_SIZE,
  1588. .bEndpointAddress = 14,
  1589. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  1590. .reg_udccs = &UDC_REGS->udccs[14],
  1591. .reg_ubcr = &UDC_REGS->ubcr14,
  1592. .reg_uddr = &UDC_REGS->uddr14,
  1593. },
  1594. .ep[15] = {
  1595. .ep = {
  1596. .name = "ep15in-int",
  1597. .ops = &pxa25x_ep_ops,
  1598. .maxpacket = INT_FIFO_SIZE,
  1599. },
  1600. .dev = &memory,
  1601. .fifo_size = INT_FIFO_SIZE,
  1602. .bEndpointAddress = USB_DIR_IN | 15,
  1603. .bmAttributes = USB_ENDPOINT_XFER_INT,
  1604. .reg_udccs = &UDC_REGS->udccs[15],
  1605. .reg_uddr = &UDC_REGS->uddr15,
  1606. },
  1607. #endif /* !CONFIG_USB_PXA25X_SMALL */
  1608. };
  1609. static void udc_command(int cmd)
  1610. {
  1611. switch (cmd) {
  1612. case PXA2XX_UDC_CMD_CONNECT:
  1613. setbits_le32(GPDR(CONFIG_USB_DEV_PULLUP_GPIO),
  1614. GPIO_bit(CONFIG_USB_DEV_PULLUP_GPIO));
  1615. /* enable pullup */
  1616. writel(GPIO_bit(CONFIG_USB_DEV_PULLUP_GPIO),
  1617. GPCR(CONFIG_USB_DEV_PULLUP_GPIO));
  1618. debug("Connected to USB\n");
  1619. break;
  1620. case PXA2XX_UDC_CMD_DISCONNECT:
  1621. /* disable pullup resistor */
  1622. writel(GPIO_bit(CONFIG_USB_DEV_PULLUP_GPIO),
  1623. GPSR(CONFIG_USB_DEV_PULLUP_GPIO));
  1624. /* setup pin as input, line will float */
  1625. clrbits_le32(GPDR(CONFIG_USB_DEV_PULLUP_GPIO),
  1626. GPIO_bit(CONFIG_USB_DEV_PULLUP_GPIO));
  1627. debug("Disconnected from USB\n");
  1628. break;
  1629. }
  1630. }
  1631. static struct pxa2xx_udc_mach_info mach_info = {
  1632. .udc_command = udc_command,
  1633. };
  1634. /*
  1635. * when a driver is successfully registered, it will receive
  1636. * control requests including set_configuration(), which enables
  1637. * non-control requests. then usb traffic follows until a
  1638. * disconnect is reported. then a host may connect again, or
  1639. * the driver might get unbound.
  1640. */
  1641. int usb_gadget_register_driver(struct usb_gadget_driver *driver)
  1642. {
  1643. struct pxa25x_udc *dev = &memory;
  1644. int retval;
  1645. uint32_t chiprev;
  1646. if (!driver
  1647. || driver->speed < USB_SPEED_FULL
  1648. || !driver->disconnect
  1649. || !driver->setup)
  1650. return -EINVAL;
  1651. if (!dev)
  1652. return -ENODEV;
  1653. if (dev->driver)
  1654. return -EBUSY;
  1655. /* Enable clock for usb controller */
  1656. setbits_le32(CKEN, CKEN11_USB);
  1657. /* first hook up the driver ... */
  1658. dev->driver = driver;
  1659. dev->pullup = 1;
  1660. /* trigger chiprev-specific logic */
  1661. switch ((chiprev = pxa_get_cpu_revision())) {
  1662. case PXA255_A0:
  1663. dev->has_cfr = 1;
  1664. break;
  1665. case PXA250_A0:
  1666. case PXA250_A1:
  1667. /* A0/A1 "not released"; ep 13, 15 unusable */
  1668. /* fall through */
  1669. case PXA250_B2: case PXA210_B2:
  1670. case PXA250_B1: case PXA210_B1:
  1671. case PXA250_B0: case PXA210_B0:
  1672. /* OUT-DMA is broken ... */
  1673. /* fall through */
  1674. case PXA250_C0: case PXA210_C0:
  1675. break;
  1676. default:
  1677. printf("%s: unrecognized processor: %08x\n",
  1678. DRIVER_NAME, chiprev);
  1679. return -ENODEV;
  1680. }
  1681. the_controller = dev;
  1682. /* prepare watchdog timer */
  1683. dev->watchdog.running = 0;
  1684. dev->watchdog.period = 5000 * CONFIG_SYS_HZ / 1000000; /* 5 ms */
  1685. dev->watchdog.function = udc_watchdog;
  1686. dev->mach = &mach_info;
  1687. udc_disable(dev);
  1688. udc_reinit(dev);
  1689. dev->gadget.name = "pxa2xx_udc";
  1690. retval = driver->bind(&dev->gadget);
  1691. if (retval) {
  1692. printf("bind to driver %s --> error %d\n",
  1693. DRIVER_NAME, retval);
  1694. dev->driver = NULL;
  1695. return retval;
  1696. }
  1697. /*
  1698. * ... then enable host detection and ep0; and we're ready
  1699. * for set_configuration as well as eventual disconnect.
  1700. */
  1701. printf("registered gadget driver '%s'\n", DRIVER_NAME);
  1702. pullup(dev);
  1703. dump_state(dev);
  1704. return 0;
  1705. }
  1706. static void
  1707. stop_activity(struct pxa25x_udc *dev, struct usb_gadget_driver *driver)
  1708. {
  1709. int i;
  1710. /* don't disconnect drivers more than once */
  1711. if (dev->gadget.speed == USB_SPEED_UNKNOWN)
  1712. driver = NULL;
  1713. dev->gadget.speed = USB_SPEED_UNKNOWN;
  1714. /* prevent new request submissions, kill any outstanding requests */
  1715. for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
  1716. struct pxa25x_ep *ep = &dev->ep[i];
  1717. ep->stopped = 1;
  1718. nuke(ep, -ESHUTDOWN);
  1719. }
  1720. stop_watchdog(dev);
  1721. /* report disconnect; the driver is already quiesced */
  1722. if (driver)
  1723. driver->disconnect(&dev->gadget);
  1724. /* re-init driver-visible data structures */
  1725. udc_reinit(dev);
  1726. }
  1727. int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
  1728. {
  1729. struct pxa25x_udc *dev = the_controller;
  1730. if (!dev)
  1731. return -ENODEV;
  1732. if (!driver || driver != dev->driver || !driver->unbind)
  1733. return -EINVAL;
  1734. local_irq_disable();
  1735. dev->pullup = 0;
  1736. pullup(dev);
  1737. stop_activity(dev, driver);
  1738. local_irq_enable();
  1739. driver->unbind(&dev->gadget);
  1740. dev->driver = NULL;
  1741. printf("unregistered gadget driver '%s'\n", DRIVER_NAME);
  1742. dump_state(dev);
  1743. the_controller = NULL;
  1744. clrbits_le32(CKEN, CKEN11_USB);
  1745. return 0;
  1746. }
  1747. extern void udc_disconnect(void)
  1748. {
  1749. setbits_le32(CKEN, CKEN11_USB);
  1750. udc_clear_mask_UDCCR(UDCCR_UDE);
  1751. udc_command(PXA2XX_UDC_CMD_DISCONNECT);
  1752. clrbits_le32(CKEN, CKEN11_USB);
  1753. }
  1754. /*-------------------------------------------------------------------------*/
  1755. extern int
  1756. usb_gadget_handle_interrupts(int index)
  1757. {
  1758. return pxa25x_udc_irq();
  1759. }