pxa25x_udc.c 50 KB

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