pxa25x_udc.c 59 KB

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