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