pxa25x_udc.c 51 KB

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