pxa25x_udc.c 58 KB

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