pxa25x_udc.c 51 KB

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