pxa2xx_udc.c 67 KB

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