pxa2xx_udc.c 59 KB

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