pxa25x_udc.c 58 KB

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