pxa25x_udc.c 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377
  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 const struct usb_gadget_ops pxa25x_udc_ops = {
  850. .get_frame = pxa25x_udc_get_frame,
  851. .wakeup = pxa25x_udc_wakeup,
  852. .vbus_session = pxa25x_udc_vbus_session,
  853. .pullup = pxa25x_udc_pullup,
  854. .vbus_draw = pxa25x_udc_vbus_draw,
  855. };
  856. /*-------------------------------------------------------------------------*/
  857. #ifdef CONFIG_USB_GADGET_DEBUG_FS
  858. static int
  859. udc_seq_show(struct seq_file *m, void *_d)
  860. {
  861. struct pxa25x_udc *dev = m->private;
  862. unsigned long flags;
  863. int i;
  864. u32 tmp;
  865. local_irq_save(flags);
  866. /* basic device status */
  867. seq_printf(m, DRIVER_DESC "\n"
  868. "%s version: %s\nGadget driver: %s\nHost %s\n\n",
  869. driver_name, DRIVER_VERSION SIZE_STR "(pio)",
  870. dev->driver ? dev->driver->driver.name : "(none)",
  871. dev->gadget.speed == USB_SPEED_FULL ? "full speed" : "disconnected");
  872. /* registers for device and ep0 */
  873. seq_printf(m,
  874. "uicr %02X.%02X, usir %02X.%02x, ufnr %02X.%02X\n",
  875. UICR1, UICR0, USIR1, USIR0, UFNRH, UFNRL);
  876. tmp = UDCCR;
  877. seq_printf(m,
  878. "udccr %02X =%s%s%s%s%s%s%s%s\n", tmp,
  879. (tmp & UDCCR_REM) ? " rem" : "",
  880. (tmp & UDCCR_RSTIR) ? " rstir" : "",
  881. (tmp & UDCCR_SRM) ? " srm" : "",
  882. (tmp & UDCCR_SUSIR) ? " susir" : "",
  883. (tmp & UDCCR_RESIR) ? " resir" : "",
  884. (tmp & UDCCR_RSM) ? " rsm" : "",
  885. (tmp & UDCCR_UDA) ? " uda" : "",
  886. (tmp & UDCCR_UDE) ? " ude" : "");
  887. tmp = UDCCS0;
  888. seq_printf(m,
  889. "udccs0 %02X =%s%s%s%s%s%s%s%s\n", tmp,
  890. (tmp & UDCCS0_SA) ? " sa" : "",
  891. (tmp & UDCCS0_RNE) ? " rne" : "",
  892. (tmp & UDCCS0_FST) ? " fst" : "",
  893. (tmp & UDCCS0_SST) ? " sst" : "",
  894. (tmp & UDCCS0_DRWF) ? " dwrf" : "",
  895. (tmp & UDCCS0_FTF) ? " ftf" : "",
  896. (tmp & UDCCS0_IPR) ? " ipr" : "",
  897. (tmp & UDCCS0_OPR) ? " opr" : "");
  898. if (dev->has_cfr) {
  899. tmp = UDCCFR;
  900. seq_printf(m,
  901. "udccfr %02X =%s%s\n", tmp,
  902. (tmp & UDCCFR_AREN) ? " aren" : "",
  903. (tmp & UDCCFR_ACM) ? " acm" : "");
  904. }
  905. if (dev->gadget.speed != USB_SPEED_FULL || !dev->driver)
  906. goto done;
  907. seq_printf(m, "ep0 IN %lu/%lu, OUT %lu/%lu\nirqs %lu\n\n",
  908. dev->stats.write.bytes, dev->stats.write.ops,
  909. dev->stats.read.bytes, dev->stats.read.ops,
  910. dev->stats.irqs);
  911. /* dump endpoint queues */
  912. for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
  913. struct pxa25x_ep *ep = &dev->ep [i];
  914. struct pxa25x_request *req;
  915. if (i != 0) {
  916. const struct usb_endpoint_descriptor *desc;
  917. desc = ep->desc;
  918. if (!desc)
  919. continue;
  920. tmp = *dev->ep [i].reg_udccs;
  921. seq_printf(m,
  922. "%s max %d %s udccs %02x irqs %lu\n",
  923. ep->ep.name, le16_to_cpu(desc->wMaxPacketSize),
  924. "pio", tmp, ep->pio_irqs);
  925. /* TODO translate all five groups of udccs bits! */
  926. } else /* ep0 should only have one transfer queued */
  927. seq_printf(m, "ep0 max 16 pio irqs %lu\n",
  928. ep->pio_irqs);
  929. if (list_empty(&ep->queue)) {
  930. seq_printf(m, "\t(nothing queued)\n");
  931. continue;
  932. }
  933. list_for_each_entry(req, &ep->queue, queue) {
  934. seq_printf(m,
  935. "\treq %p len %d/%d buf %p\n",
  936. &req->req, req->req.actual,
  937. req->req.length, req->req.buf);
  938. }
  939. }
  940. done:
  941. local_irq_restore(flags);
  942. return 0;
  943. }
  944. static int
  945. udc_debugfs_open(struct inode *inode, struct file *file)
  946. {
  947. return single_open(file, udc_seq_show, inode->i_private);
  948. }
  949. static const struct file_operations debug_fops = {
  950. .open = udc_debugfs_open,
  951. .read = seq_read,
  952. .llseek = seq_lseek,
  953. .release = single_release,
  954. .owner = THIS_MODULE,
  955. };
  956. #define create_debug_files(dev) \
  957. do { \
  958. dev->debugfs_udc = debugfs_create_file(dev->gadget.name, \
  959. S_IRUGO, NULL, dev, &debug_fops); \
  960. } while (0)
  961. #define remove_debug_files(dev) \
  962. do { \
  963. if (dev->debugfs_udc) \
  964. debugfs_remove(dev->debugfs_udc); \
  965. } while (0)
  966. #else /* !CONFIG_USB_GADGET_DEBUG_FILES */
  967. #define create_debug_files(dev) do {} while (0)
  968. #define remove_debug_files(dev) do {} while (0)
  969. #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
  970. /*-------------------------------------------------------------------------*/
  971. /*
  972. * udc_disable - disable USB device controller
  973. */
  974. static void udc_disable(struct pxa25x_udc *dev)
  975. {
  976. /* block all irqs */
  977. udc_set_mask_UDCCR(UDCCR_SRM|UDCCR_REM);
  978. UICR0 = UICR1 = 0xff;
  979. UFNRH = UFNRH_SIM;
  980. /* if hardware supports it, disconnect from usb */
  981. pullup_off();
  982. udc_clear_mask_UDCCR(UDCCR_UDE);
  983. ep0_idle (dev);
  984. dev->gadget.speed = USB_SPEED_UNKNOWN;
  985. }
  986. /*
  987. * udc_reinit - initialize software state
  988. */
  989. static void udc_reinit(struct pxa25x_udc *dev)
  990. {
  991. u32 i;
  992. /* device/ep0 records init */
  993. INIT_LIST_HEAD (&dev->gadget.ep_list);
  994. INIT_LIST_HEAD (&dev->gadget.ep0->ep_list);
  995. dev->ep0state = EP0_IDLE;
  996. /* basic endpoint records init */
  997. for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
  998. struct pxa25x_ep *ep = &dev->ep[i];
  999. if (i != 0)
  1000. list_add_tail (&ep->ep.ep_list, &dev->gadget.ep_list);
  1001. ep->desc = NULL;
  1002. ep->stopped = 0;
  1003. INIT_LIST_HEAD (&ep->queue);
  1004. ep->pio_irqs = 0;
  1005. }
  1006. /* the rest was statically initialized, and is read-only */
  1007. }
  1008. /* until it's enabled, this UDC should be completely invisible
  1009. * to any USB host.
  1010. */
  1011. static void udc_enable (struct pxa25x_udc *dev)
  1012. {
  1013. udc_clear_mask_UDCCR(UDCCR_UDE);
  1014. /* try to clear these bits before we enable the udc */
  1015. udc_ack_int_UDCCR(UDCCR_SUSIR|/*UDCCR_RSTIR|*/UDCCR_RESIR);
  1016. ep0_idle(dev);
  1017. dev->gadget.speed = USB_SPEED_UNKNOWN;
  1018. dev->stats.irqs = 0;
  1019. /*
  1020. * sequence taken from chapter 12.5.10, PXA250 AppProcDevManual:
  1021. * - enable UDC
  1022. * - if RESET is already in progress, ack interrupt
  1023. * - unmask reset interrupt
  1024. */
  1025. udc_set_mask_UDCCR(UDCCR_UDE);
  1026. if (!(UDCCR & UDCCR_UDA))
  1027. udc_ack_int_UDCCR(UDCCR_RSTIR);
  1028. if (dev->has_cfr /* UDC_RES2 is defined */) {
  1029. /* pxa255 (a0+) can avoid a set_config race that could
  1030. * prevent gadget drivers from configuring correctly
  1031. */
  1032. UDCCFR = UDCCFR_ACM | UDCCFR_MB1;
  1033. } else {
  1034. /* "USB test mode" for pxa250 errata 40-42 (stepping a0, a1)
  1035. * which could result in missing packets and interrupts.
  1036. * supposedly one bit per endpoint, controlling whether it
  1037. * double buffers or not; ACM/AREN bits fit into the holes.
  1038. * zero bits (like USIR0_IRx) disable double buffering.
  1039. */
  1040. UDC_RES1 = 0x00;
  1041. UDC_RES2 = 0x00;
  1042. }
  1043. /* enable suspend/resume and reset irqs */
  1044. udc_clear_mask_UDCCR(UDCCR_SRM | UDCCR_REM);
  1045. /* enable ep0 irqs */
  1046. UICR0 &= ~UICR0_IM0;
  1047. /* if hardware supports it, pullup D+ and wait for reset */
  1048. pullup_on();
  1049. }
  1050. /* when a driver is successfully registered, it will receive
  1051. * control requests including set_configuration(), which enables
  1052. * non-control requests. then usb traffic follows until a
  1053. * disconnect is reported. then a host may connect again, or
  1054. * the driver might get unbound.
  1055. */
  1056. int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
  1057. int (*bind)(struct usb_gadget *))
  1058. {
  1059. struct pxa25x_udc *dev = the_controller;
  1060. int retval;
  1061. if (!driver
  1062. || driver->speed < USB_SPEED_FULL
  1063. || !bind
  1064. || !driver->disconnect
  1065. || !driver->setup)
  1066. return -EINVAL;
  1067. if (!dev)
  1068. return -ENODEV;
  1069. if (dev->driver)
  1070. return -EBUSY;
  1071. /* first hook up the driver ... */
  1072. dev->driver = driver;
  1073. dev->gadget.dev.driver = &driver->driver;
  1074. dev->pullup = 1;
  1075. retval = device_add (&dev->gadget.dev);
  1076. if (retval) {
  1077. fail:
  1078. dev->driver = NULL;
  1079. dev->gadget.dev.driver = NULL;
  1080. return retval;
  1081. }
  1082. retval = bind(&dev->gadget);
  1083. if (retval) {
  1084. DMSG("bind to driver %s --> error %d\n",
  1085. driver->driver.name, retval);
  1086. device_del (&dev->gadget.dev);
  1087. goto fail;
  1088. }
  1089. /* ... then enable host detection and ep0; and we're ready
  1090. * for set_configuration as well as eventual disconnect.
  1091. */
  1092. DMSG("registered gadget driver '%s'\n", driver->driver.name);
  1093. /* connect to bus through transceiver */
  1094. if (dev->transceiver) {
  1095. retval = otg_set_peripheral(dev->transceiver, &dev->gadget);
  1096. if (retval) {
  1097. DMSG("can't bind to transceiver\n");
  1098. if (driver->unbind)
  1099. driver->unbind(&dev->gadget);
  1100. goto bind_fail;
  1101. }
  1102. }
  1103. pullup(dev);
  1104. dump_state(dev);
  1105. return 0;
  1106. bind_fail:
  1107. return retval;
  1108. }
  1109. EXPORT_SYMBOL(usb_gadget_probe_driver);
  1110. static void
  1111. stop_activity(struct pxa25x_udc *dev, struct usb_gadget_driver *driver)
  1112. {
  1113. int i;
  1114. /* don't disconnect drivers more than once */
  1115. if (dev->gadget.speed == USB_SPEED_UNKNOWN)
  1116. driver = NULL;
  1117. dev->gadget.speed = USB_SPEED_UNKNOWN;
  1118. /* prevent new request submissions, kill any outstanding requests */
  1119. for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
  1120. struct pxa25x_ep *ep = &dev->ep[i];
  1121. ep->stopped = 1;
  1122. nuke(ep, -ESHUTDOWN);
  1123. }
  1124. del_timer_sync(&dev->timer);
  1125. /* report disconnect; the driver is already quiesced */
  1126. if (driver)
  1127. driver->disconnect(&dev->gadget);
  1128. /* re-init driver-visible data structures */
  1129. udc_reinit(dev);
  1130. }
  1131. int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
  1132. {
  1133. struct pxa25x_udc *dev = the_controller;
  1134. if (!dev)
  1135. return -ENODEV;
  1136. if (!driver || driver != dev->driver || !driver->unbind)
  1137. return -EINVAL;
  1138. local_irq_disable();
  1139. dev->pullup = 0;
  1140. pullup(dev);
  1141. stop_activity(dev, driver);
  1142. local_irq_enable();
  1143. if (dev->transceiver)
  1144. (void) otg_set_peripheral(dev->transceiver, NULL);
  1145. driver->unbind(&dev->gadget);
  1146. dev->gadget.dev.driver = NULL;
  1147. dev->driver = NULL;
  1148. device_del (&dev->gadget.dev);
  1149. DMSG("unregistered gadget driver '%s'\n", driver->driver.name);
  1150. dump_state(dev);
  1151. return 0;
  1152. }
  1153. EXPORT_SYMBOL(usb_gadget_unregister_driver);
  1154. /*-------------------------------------------------------------------------*/
  1155. #ifdef CONFIG_ARCH_LUBBOCK
  1156. /* Lubbock has separate connect and disconnect irqs. More typical designs
  1157. * use one GPIO as the VBUS IRQ, and another to control the D+ pullup.
  1158. */
  1159. static irqreturn_t
  1160. lubbock_vbus_irq(int irq, void *_dev)
  1161. {
  1162. struct pxa25x_udc *dev = _dev;
  1163. int vbus;
  1164. dev->stats.irqs++;
  1165. switch (irq) {
  1166. case LUBBOCK_USB_IRQ:
  1167. vbus = 1;
  1168. disable_irq(LUBBOCK_USB_IRQ);
  1169. enable_irq(LUBBOCK_USB_DISC_IRQ);
  1170. break;
  1171. case LUBBOCK_USB_DISC_IRQ:
  1172. vbus = 0;
  1173. disable_irq(LUBBOCK_USB_DISC_IRQ);
  1174. enable_irq(LUBBOCK_USB_IRQ);
  1175. break;
  1176. default:
  1177. return IRQ_NONE;
  1178. }
  1179. pxa25x_udc_vbus_session(&dev->gadget, vbus);
  1180. return IRQ_HANDLED;
  1181. }
  1182. #endif
  1183. /*-------------------------------------------------------------------------*/
  1184. static inline void clear_ep_state (struct pxa25x_udc *dev)
  1185. {
  1186. unsigned i;
  1187. /* hardware SET_{CONFIGURATION,INTERFACE} automagic resets endpoint
  1188. * fifos, and pending transactions mustn't be continued in any case.
  1189. */
  1190. for (i = 1; i < PXA_UDC_NUM_ENDPOINTS; i++)
  1191. nuke(&dev->ep[i], -ECONNABORTED);
  1192. }
  1193. static void udc_watchdog(unsigned long _dev)
  1194. {
  1195. struct pxa25x_udc *dev = (void *)_dev;
  1196. local_irq_disable();
  1197. if (dev->ep0state == EP0_STALL
  1198. && (UDCCS0 & UDCCS0_FST) == 0
  1199. && (UDCCS0 & UDCCS0_SST) == 0) {
  1200. UDCCS0 = UDCCS0_FST|UDCCS0_FTF;
  1201. DBG(DBG_VERBOSE, "ep0 re-stall\n");
  1202. start_watchdog(dev);
  1203. }
  1204. local_irq_enable();
  1205. }
  1206. static void handle_ep0 (struct pxa25x_udc *dev)
  1207. {
  1208. u32 udccs0 = UDCCS0;
  1209. struct pxa25x_ep *ep = &dev->ep [0];
  1210. struct pxa25x_request *req;
  1211. union {
  1212. struct usb_ctrlrequest r;
  1213. u8 raw [8];
  1214. u32 word [2];
  1215. } u;
  1216. if (list_empty(&ep->queue))
  1217. req = NULL;
  1218. else
  1219. req = list_entry(ep->queue.next, struct pxa25x_request, queue);
  1220. /* clear stall status */
  1221. if (udccs0 & UDCCS0_SST) {
  1222. nuke(ep, -EPIPE);
  1223. UDCCS0 = UDCCS0_SST;
  1224. del_timer(&dev->timer);
  1225. ep0_idle(dev);
  1226. }
  1227. /* previous request unfinished? non-error iff back-to-back ... */
  1228. if ((udccs0 & UDCCS0_SA) != 0 && dev->ep0state != EP0_IDLE) {
  1229. nuke(ep, 0);
  1230. del_timer(&dev->timer);
  1231. ep0_idle(dev);
  1232. }
  1233. switch (dev->ep0state) {
  1234. case EP0_IDLE:
  1235. /* late-breaking status? */
  1236. udccs0 = UDCCS0;
  1237. /* start control request? */
  1238. if (likely((udccs0 & (UDCCS0_OPR|UDCCS0_SA|UDCCS0_RNE))
  1239. == (UDCCS0_OPR|UDCCS0_SA|UDCCS0_RNE))) {
  1240. int i;
  1241. nuke (ep, -EPROTO);
  1242. /* read SETUP packet */
  1243. for (i = 0; i < 8; i++) {
  1244. if (unlikely(!(UDCCS0 & UDCCS0_RNE))) {
  1245. bad_setup:
  1246. DMSG("SETUP %d!\n", i);
  1247. goto stall;
  1248. }
  1249. u.raw [i] = (u8) UDDR0;
  1250. }
  1251. if (unlikely((UDCCS0 & UDCCS0_RNE) != 0))
  1252. goto bad_setup;
  1253. got_setup:
  1254. DBG(DBG_VERBOSE, "SETUP %02x.%02x v%04x i%04x l%04x\n",
  1255. u.r.bRequestType, u.r.bRequest,
  1256. le16_to_cpu(u.r.wValue),
  1257. le16_to_cpu(u.r.wIndex),
  1258. le16_to_cpu(u.r.wLength));
  1259. /* cope with automagic for some standard requests. */
  1260. dev->req_std = (u.r.bRequestType & USB_TYPE_MASK)
  1261. == USB_TYPE_STANDARD;
  1262. dev->req_config = 0;
  1263. dev->req_pending = 1;
  1264. switch (u.r.bRequest) {
  1265. /* hardware restricts gadget drivers here! */
  1266. case USB_REQ_SET_CONFIGURATION:
  1267. if (u.r.bRequestType == USB_RECIP_DEVICE) {
  1268. /* reflect hardware's automagic
  1269. * up to the gadget driver.
  1270. */
  1271. config_change:
  1272. dev->req_config = 1;
  1273. clear_ep_state(dev);
  1274. /* if !has_cfr, there's no synch
  1275. * else use AREN (later) not SA|OPR
  1276. * USIR0_IR0 acts edge sensitive
  1277. */
  1278. }
  1279. break;
  1280. /* ... and here, even more ... */
  1281. case USB_REQ_SET_INTERFACE:
  1282. if (u.r.bRequestType == USB_RECIP_INTERFACE) {
  1283. /* udc hardware is broken by design:
  1284. * - altsetting may only be zero;
  1285. * - hw resets all interfaces' eps;
  1286. * - ep reset doesn't include halt(?).
  1287. */
  1288. DMSG("broken set_interface (%d/%d)\n",
  1289. le16_to_cpu(u.r.wIndex),
  1290. le16_to_cpu(u.r.wValue));
  1291. goto config_change;
  1292. }
  1293. break;
  1294. /* hardware was supposed to hide this */
  1295. case USB_REQ_SET_ADDRESS:
  1296. if (u.r.bRequestType == USB_RECIP_DEVICE) {
  1297. ep0start(dev, 0, "address");
  1298. return;
  1299. }
  1300. break;
  1301. }
  1302. if (u.r.bRequestType & USB_DIR_IN)
  1303. dev->ep0state = EP0_IN_DATA_PHASE;
  1304. else
  1305. dev->ep0state = EP0_OUT_DATA_PHASE;
  1306. i = dev->driver->setup(&dev->gadget, &u.r);
  1307. if (i < 0) {
  1308. /* hardware automagic preventing STALL... */
  1309. if (dev->req_config) {
  1310. /* hardware sometimes neglects to tell
  1311. * tell us about config change events,
  1312. * so later ones may fail...
  1313. */
  1314. WARNING("config change %02x fail %d?\n",
  1315. u.r.bRequest, i);
  1316. return;
  1317. /* TODO experiment: if has_cfr,
  1318. * hardware didn't ACK; maybe we
  1319. * could actually STALL!
  1320. */
  1321. }
  1322. DBG(DBG_VERBOSE, "protocol STALL, "
  1323. "%02x err %d\n", UDCCS0, i);
  1324. stall:
  1325. /* the watchdog timer helps deal with cases
  1326. * where udc seems to clear FST wrongly, and
  1327. * then NAKs instead of STALLing.
  1328. */
  1329. ep0start(dev, UDCCS0_FST|UDCCS0_FTF, "stall");
  1330. start_watchdog(dev);
  1331. dev->ep0state = EP0_STALL;
  1332. /* deferred i/o == no response yet */
  1333. } else if (dev->req_pending) {
  1334. if (likely(dev->ep0state == EP0_IN_DATA_PHASE
  1335. || dev->req_std || u.r.wLength))
  1336. ep0start(dev, 0, "defer");
  1337. else
  1338. ep0start(dev, UDCCS0_IPR, "defer/IPR");
  1339. }
  1340. /* expect at least one data or status stage irq */
  1341. return;
  1342. } else if (likely((udccs0 & (UDCCS0_OPR|UDCCS0_SA))
  1343. == (UDCCS0_OPR|UDCCS0_SA))) {
  1344. unsigned i;
  1345. /* pxa210/250 erratum 131 for B0/B1 says RNE lies.
  1346. * still observed on a pxa255 a0.
  1347. */
  1348. DBG(DBG_VERBOSE, "e131\n");
  1349. nuke(ep, -EPROTO);
  1350. /* read SETUP data, but don't trust it too much */
  1351. for (i = 0; i < 8; i++)
  1352. u.raw [i] = (u8) UDDR0;
  1353. if ((u.r.bRequestType & USB_RECIP_MASK)
  1354. > USB_RECIP_OTHER)
  1355. goto stall;
  1356. if (u.word [0] == 0 && u.word [1] == 0)
  1357. goto stall;
  1358. goto got_setup;
  1359. } else {
  1360. /* some random early IRQ:
  1361. * - we acked FST
  1362. * - IPR cleared
  1363. * - OPR got set, without SA (likely status stage)
  1364. */
  1365. UDCCS0 = udccs0 & (UDCCS0_SA|UDCCS0_OPR);
  1366. }
  1367. break;
  1368. case EP0_IN_DATA_PHASE: /* GET_DESCRIPTOR etc */
  1369. if (udccs0 & UDCCS0_OPR) {
  1370. UDCCS0 = UDCCS0_OPR|UDCCS0_FTF;
  1371. DBG(DBG_VERBOSE, "ep0in premature status\n");
  1372. if (req)
  1373. done(ep, req, 0);
  1374. ep0_idle(dev);
  1375. } else /* irq was IPR clearing */ {
  1376. if (req) {
  1377. /* this IN packet might finish the request */
  1378. (void) write_ep0_fifo(ep, req);
  1379. } /* else IN token before response was written */
  1380. }
  1381. break;
  1382. case EP0_OUT_DATA_PHASE: /* SET_DESCRIPTOR etc */
  1383. if (udccs0 & UDCCS0_OPR) {
  1384. if (req) {
  1385. /* this OUT packet might finish the request */
  1386. if (read_ep0_fifo(ep, req))
  1387. done(ep, req, 0);
  1388. /* else more OUT packets expected */
  1389. } /* else OUT token before read was issued */
  1390. } else /* irq was IPR clearing */ {
  1391. DBG(DBG_VERBOSE, "ep0out premature status\n");
  1392. if (req)
  1393. done(ep, req, 0);
  1394. ep0_idle(dev);
  1395. }
  1396. break;
  1397. case EP0_END_XFER:
  1398. if (req)
  1399. done(ep, req, 0);
  1400. /* ack control-IN status (maybe in-zlp was skipped)
  1401. * also appears after some config change events.
  1402. */
  1403. if (udccs0 & UDCCS0_OPR)
  1404. UDCCS0 = UDCCS0_OPR;
  1405. ep0_idle(dev);
  1406. break;
  1407. case EP0_STALL:
  1408. UDCCS0 = UDCCS0_FST;
  1409. break;
  1410. }
  1411. USIR0 = USIR0_IR0;
  1412. }
  1413. static void handle_ep(struct pxa25x_ep *ep)
  1414. {
  1415. struct pxa25x_request *req;
  1416. int is_in = ep->bEndpointAddress & USB_DIR_IN;
  1417. int completed;
  1418. u32 udccs, tmp;
  1419. do {
  1420. completed = 0;
  1421. if (likely (!list_empty(&ep->queue)))
  1422. req = list_entry(ep->queue.next,
  1423. struct pxa25x_request, queue);
  1424. else
  1425. req = NULL;
  1426. // TODO check FST handling
  1427. udccs = *ep->reg_udccs;
  1428. if (unlikely(is_in)) { /* irq from TPC, SST, or (ISO) TUR */
  1429. tmp = UDCCS_BI_TUR;
  1430. if (likely(ep->bmAttributes == USB_ENDPOINT_XFER_BULK))
  1431. tmp |= UDCCS_BI_SST;
  1432. tmp &= udccs;
  1433. if (likely (tmp))
  1434. *ep->reg_udccs = tmp;
  1435. if (req && likely ((udccs & UDCCS_BI_TFS) != 0))
  1436. completed = write_fifo(ep, req);
  1437. } else { /* irq from RPC (or for ISO, ROF) */
  1438. if (likely(ep->bmAttributes == USB_ENDPOINT_XFER_BULK))
  1439. tmp = UDCCS_BO_SST | UDCCS_BO_DME;
  1440. else
  1441. tmp = UDCCS_IO_ROF | UDCCS_IO_DME;
  1442. tmp &= udccs;
  1443. if (likely(tmp))
  1444. *ep->reg_udccs = tmp;
  1445. /* fifos can hold packets, ready for reading... */
  1446. if (likely(req)) {
  1447. completed = read_fifo(ep, req);
  1448. } else
  1449. pio_irq_disable (ep->bEndpointAddress);
  1450. }
  1451. ep->pio_irqs++;
  1452. } while (completed);
  1453. }
  1454. /*
  1455. * pxa25x_udc_irq - interrupt handler
  1456. *
  1457. * avoid delays in ep0 processing. the control handshaking isn't always
  1458. * under software control (pxa250c0 and the pxa255 are better), and delays
  1459. * could cause usb protocol errors.
  1460. */
  1461. static irqreturn_t
  1462. pxa25x_udc_irq(int irq, void *_dev)
  1463. {
  1464. struct pxa25x_udc *dev = _dev;
  1465. int handled;
  1466. dev->stats.irqs++;
  1467. do {
  1468. u32 udccr = UDCCR;
  1469. handled = 0;
  1470. /* SUSpend Interrupt Request */
  1471. if (unlikely(udccr & UDCCR_SUSIR)) {
  1472. udc_ack_int_UDCCR(UDCCR_SUSIR);
  1473. handled = 1;
  1474. DBG(DBG_VERBOSE, "USB suspend\n");
  1475. if (dev->gadget.speed != USB_SPEED_UNKNOWN
  1476. && dev->driver
  1477. && dev->driver->suspend)
  1478. dev->driver->suspend(&dev->gadget);
  1479. ep0_idle (dev);
  1480. }
  1481. /* RESume Interrupt Request */
  1482. if (unlikely(udccr & UDCCR_RESIR)) {
  1483. udc_ack_int_UDCCR(UDCCR_RESIR);
  1484. handled = 1;
  1485. DBG(DBG_VERBOSE, "USB resume\n");
  1486. if (dev->gadget.speed != USB_SPEED_UNKNOWN
  1487. && dev->driver
  1488. && dev->driver->resume)
  1489. dev->driver->resume(&dev->gadget);
  1490. }
  1491. /* ReSeT Interrupt Request - USB reset */
  1492. if (unlikely(udccr & UDCCR_RSTIR)) {
  1493. udc_ack_int_UDCCR(UDCCR_RSTIR);
  1494. handled = 1;
  1495. if ((UDCCR & UDCCR_UDA) == 0) {
  1496. DBG(DBG_VERBOSE, "USB reset start\n");
  1497. /* reset driver and endpoints,
  1498. * in case that's not yet done
  1499. */
  1500. stop_activity (dev, dev->driver);
  1501. } else {
  1502. DBG(DBG_VERBOSE, "USB reset end\n");
  1503. dev->gadget.speed = USB_SPEED_FULL;
  1504. memset(&dev->stats, 0, sizeof dev->stats);
  1505. /* driver and endpoints are still reset */
  1506. }
  1507. } else {
  1508. u32 usir0 = USIR0 & ~UICR0;
  1509. u32 usir1 = USIR1 & ~UICR1;
  1510. int i;
  1511. if (unlikely (!usir0 && !usir1))
  1512. continue;
  1513. DBG(DBG_VERY_NOISY, "irq %02x.%02x\n", usir1, usir0);
  1514. /* control traffic */
  1515. if (usir0 & USIR0_IR0) {
  1516. dev->ep[0].pio_irqs++;
  1517. handle_ep0(dev);
  1518. handled = 1;
  1519. }
  1520. /* endpoint data transfers */
  1521. for (i = 0; i < 8; i++) {
  1522. u32 tmp = 1 << i;
  1523. if (i && (usir0 & tmp)) {
  1524. handle_ep(&dev->ep[i]);
  1525. USIR0 |= tmp;
  1526. handled = 1;
  1527. }
  1528. #ifndef CONFIG_USB_PXA25X_SMALL
  1529. if (usir1 & tmp) {
  1530. handle_ep(&dev->ep[i+8]);
  1531. USIR1 |= tmp;
  1532. handled = 1;
  1533. }
  1534. #endif
  1535. }
  1536. }
  1537. /* we could also ask for 1 msec SOF (SIR) interrupts */
  1538. } while (handled);
  1539. return IRQ_HANDLED;
  1540. }
  1541. /*-------------------------------------------------------------------------*/
  1542. static void nop_release (struct device *dev)
  1543. {
  1544. DMSG("%s %s\n", __func__, dev_name(dev));
  1545. }
  1546. /* this uses load-time allocation and initialization (instead of
  1547. * doing it at run-time) to save code, eliminate fault paths, and
  1548. * be more obviously correct.
  1549. */
  1550. static struct pxa25x_udc memory = {
  1551. .gadget = {
  1552. .ops = &pxa25x_udc_ops,
  1553. .ep0 = &memory.ep[0].ep,
  1554. .name = driver_name,
  1555. .dev = {
  1556. .init_name = "gadget",
  1557. .release = nop_release,
  1558. },
  1559. },
  1560. /* control endpoint */
  1561. .ep[0] = {
  1562. .ep = {
  1563. .name = ep0name,
  1564. .ops = &pxa25x_ep_ops,
  1565. .maxpacket = EP0_FIFO_SIZE,
  1566. },
  1567. .dev = &memory,
  1568. .reg_udccs = &UDCCS0,
  1569. .reg_uddr = &UDDR0,
  1570. },
  1571. /* first group of endpoints */
  1572. .ep[1] = {
  1573. .ep = {
  1574. .name = "ep1in-bulk",
  1575. .ops = &pxa25x_ep_ops,
  1576. .maxpacket = BULK_FIFO_SIZE,
  1577. },
  1578. .dev = &memory,
  1579. .fifo_size = BULK_FIFO_SIZE,
  1580. .bEndpointAddress = USB_DIR_IN | 1,
  1581. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1582. .reg_udccs = &UDCCS1,
  1583. .reg_uddr = &UDDR1,
  1584. },
  1585. .ep[2] = {
  1586. .ep = {
  1587. .name = "ep2out-bulk",
  1588. .ops = &pxa25x_ep_ops,
  1589. .maxpacket = BULK_FIFO_SIZE,
  1590. },
  1591. .dev = &memory,
  1592. .fifo_size = BULK_FIFO_SIZE,
  1593. .bEndpointAddress = 2,
  1594. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1595. .reg_udccs = &UDCCS2,
  1596. .reg_ubcr = &UBCR2,
  1597. .reg_uddr = &UDDR2,
  1598. },
  1599. #ifndef CONFIG_USB_PXA25X_SMALL
  1600. .ep[3] = {
  1601. .ep = {
  1602. .name = "ep3in-iso",
  1603. .ops = &pxa25x_ep_ops,
  1604. .maxpacket = ISO_FIFO_SIZE,
  1605. },
  1606. .dev = &memory,
  1607. .fifo_size = ISO_FIFO_SIZE,
  1608. .bEndpointAddress = USB_DIR_IN | 3,
  1609. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  1610. .reg_udccs = &UDCCS3,
  1611. .reg_uddr = &UDDR3,
  1612. },
  1613. .ep[4] = {
  1614. .ep = {
  1615. .name = "ep4out-iso",
  1616. .ops = &pxa25x_ep_ops,
  1617. .maxpacket = ISO_FIFO_SIZE,
  1618. },
  1619. .dev = &memory,
  1620. .fifo_size = ISO_FIFO_SIZE,
  1621. .bEndpointAddress = 4,
  1622. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  1623. .reg_udccs = &UDCCS4,
  1624. .reg_ubcr = &UBCR4,
  1625. .reg_uddr = &UDDR4,
  1626. },
  1627. .ep[5] = {
  1628. .ep = {
  1629. .name = "ep5in-int",
  1630. .ops = &pxa25x_ep_ops,
  1631. .maxpacket = INT_FIFO_SIZE,
  1632. },
  1633. .dev = &memory,
  1634. .fifo_size = INT_FIFO_SIZE,
  1635. .bEndpointAddress = USB_DIR_IN | 5,
  1636. .bmAttributes = USB_ENDPOINT_XFER_INT,
  1637. .reg_udccs = &UDCCS5,
  1638. .reg_uddr = &UDDR5,
  1639. },
  1640. /* second group of endpoints */
  1641. .ep[6] = {
  1642. .ep = {
  1643. .name = "ep6in-bulk",
  1644. .ops = &pxa25x_ep_ops,
  1645. .maxpacket = BULK_FIFO_SIZE,
  1646. },
  1647. .dev = &memory,
  1648. .fifo_size = BULK_FIFO_SIZE,
  1649. .bEndpointAddress = USB_DIR_IN | 6,
  1650. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1651. .reg_udccs = &UDCCS6,
  1652. .reg_uddr = &UDDR6,
  1653. },
  1654. .ep[7] = {
  1655. .ep = {
  1656. .name = "ep7out-bulk",
  1657. .ops = &pxa25x_ep_ops,
  1658. .maxpacket = BULK_FIFO_SIZE,
  1659. },
  1660. .dev = &memory,
  1661. .fifo_size = BULK_FIFO_SIZE,
  1662. .bEndpointAddress = 7,
  1663. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1664. .reg_udccs = &UDCCS7,
  1665. .reg_ubcr = &UBCR7,
  1666. .reg_uddr = &UDDR7,
  1667. },
  1668. .ep[8] = {
  1669. .ep = {
  1670. .name = "ep8in-iso",
  1671. .ops = &pxa25x_ep_ops,
  1672. .maxpacket = ISO_FIFO_SIZE,
  1673. },
  1674. .dev = &memory,
  1675. .fifo_size = ISO_FIFO_SIZE,
  1676. .bEndpointAddress = USB_DIR_IN | 8,
  1677. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  1678. .reg_udccs = &UDCCS8,
  1679. .reg_uddr = &UDDR8,
  1680. },
  1681. .ep[9] = {
  1682. .ep = {
  1683. .name = "ep9out-iso",
  1684. .ops = &pxa25x_ep_ops,
  1685. .maxpacket = ISO_FIFO_SIZE,
  1686. },
  1687. .dev = &memory,
  1688. .fifo_size = ISO_FIFO_SIZE,
  1689. .bEndpointAddress = 9,
  1690. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  1691. .reg_udccs = &UDCCS9,
  1692. .reg_ubcr = &UBCR9,
  1693. .reg_uddr = &UDDR9,
  1694. },
  1695. .ep[10] = {
  1696. .ep = {
  1697. .name = "ep10in-int",
  1698. .ops = &pxa25x_ep_ops,
  1699. .maxpacket = INT_FIFO_SIZE,
  1700. },
  1701. .dev = &memory,
  1702. .fifo_size = INT_FIFO_SIZE,
  1703. .bEndpointAddress = USB_DIR_IN | 10,
  1704. .bmAttributes = USB_ENDPOINT_XFER_INT,
  1705. .reg_udccs = &UDCCS10,
  1706. .reg_uddr = &UDDR10,
  1707. },
  1708. /* third group of endpoints */
  1709. .ep[11] = {
  1710. .ep = {
  1711. .name = "ep11in-bulk",
  1712. .ops = &pxa25x_ep_ops,
  1713. .maxpacket = BULK_FIFO_SIZE,
  1714. },
  1715. .dev = &memory,
  1716. .fifo_size = BULK_FIFO_SIZE,
  1717. .bEndpointAddress = USB_DIR_IN | 11,
  1718. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1719. .reg_udccs = &UDCCS11,
  1720. .reg_uddr = &UDDR11,
  1721. },
  1722. .ep[12] = {
  1723. .ep = {
  1724. .name = "ep12out-bulk",
  1725. .ops = &pxa25x_ep_ops,
  1726. .maxpacket = BULK_FIFO_SIZE,
  1727. },
  1728. .dev = &memory,
  1729. .fifo_size = BULK_FIFO_SIZE,
  1730. .bEndpointAddress = 12,
  1731. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1732. .reg_udccs = &UDCCS12,
  1733. .reg_ubcr = &UBCR12,
  1734. .reg_uddr = &UDDR12,
  1735. },
  1736. .ep[13] = {
  1737. .ep = {
  1738. .name = "ep13in-iso",
  1739. .ops = &pxa25x_ep_ops,
  1740. .maxpacket = ISO_FIFO_SIZE,
  1741. },
  1742. .dev = &memory,
  1743. .fifo_size = ISO_FIFO_SIZE,
  1744. .bEndpointAddress = USB_DIR_IN | 13,
  1745. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  1746. .reg_udccs = &UDCCS13,
  1747. .reg_uddr = &UDDR13,
  1748. },
  1749. .ep[14] = {
  1750. .ep = {
  1751. .name = "ep14out-iso",
  1752. .ops = &pxa25x_ep_ops,
  1753. .maxpacket = ISO_FIFO_SIZE,
  1754. },
  1755. .dev = &memory,
  1756. .fifo_size = ISO_FIFO_SIZE,
  1757. .bEndpointAddress = 14,
  1758. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  1759. .reg_udccs = &UDCCS14,
  1760. .reg_ubcr = &UBCR14,
  1761. .reg_uddr = &UDDR14,
  1762. },
  1763. .ep[15] = {
  1764. .ep = {
  1765. .name = "ep15in-int",
  1766. .ops = &pxa25x_ep_ops,
  1767. .maxpacket = INT_FIFO_SIZE,
  1768. },
  1769. .dev = &memory,
  1770. .fifo_size = INT_FIFO_SIZE,
  1771. .bEndpointAddress = USB_DIR_IN | 15,
  1772. .bmAttributes = USB_ENDPOINT_XFER_INT,
  1773. .reg_udccs = &UDCCS15,
  1774. .reg_uddr = &UDDR15,
  1775. },
  1776. #endif /* !CONFIG_USB_PXA25X_SMALL */
  1777. };
  1778. #define CP15R0_VENDOR_MASK 0xffffe000
  1779. #if defined(CONFIG_ARCH_PXA)
  1780. #define CP15R0_XSCALE_VALUE 0x69052000 /* intel/arm/xscale */
  1781. #elif defined(CONFIG_ARCH_IXP4XX)
  1782. #define CP15R0_XSCALE_VALUE 0x69054000 /* intel/arm/ixp4xx */
  1783. #endif
  1784. #define CP15R0_PROD_MASK 0x000003f0
  1785. #define PXA25x 0x00000100 /* and PXA26x */
  1786. #define PXA210 0x00000120
  1787. #define CP15R0_REV_MASK 0x0000000f
  1788. #define CP15R0_PRODREV_MASK (CP15R0_PROD_MASK | CP15R0_REV_MASK)
  1789. #define PXA255_A0 0x00000106 /* or PXA260_B1 */
  1790. #define PXA250_C0 0x00000105 /* or PXA26x_B0 */
  1791. #define PXA250_B2 0x00000104
  1792. #define PXA250_B1 0x00000103 /* or PXA260_A0 */
  1793. #define PXA250_B0 0x00000102
  1794. #define PXA250_A1 0x00000101
  1795. #define PXA250_A0 0x00000100
  1796. #define PXA210_C0 0x00000125
  1797. #define PXA210_B2 0x00000124
  1798. #define PXA210_B1 0x00000123
  1799. #define PXA210_B0 0x00000122
  1800. #define IXP425_A0 0x000001c1
  1801. #define IXP425_B0 0x000001f1
  1802. #define IXP465_AD 0x00000200
  1803. /*
  1804. * probe - binds to the platform device
  1805. */
  1806. static int __init pxa25x_udc_probe(struct platform_device *pdev)
  1807. {
  1808. struct pxa25x_udc *dev = &memory;
  1809. int retval, irq;
  1810. u32 chiprev;
  1811. /* insist on Intel/ARM/XScale */
  1812. asm("mrc%? p15, 0, %0, c0, c0" : "=r" (chiprev));
  1813. if ((chiprev & CP15R0_VENDOR_MASK) != CP15R0_XSCALE_VALUE) {
  1814. pr_err("%s: not XScale!\n", driver_name);
  1815. return -ENODEV;
  1816. }
  1817. /* trigger chiprev-specific logic */
  1818. switch (chiprev & CP15R0_PRODREV_MASK) {
  1819. #if defined(CONFIG_ARCH_PXA)
  1820. case PXA255_A0:
  1821. dev->has_cfr = 1;
  1822. break;
  1823. case PXA250_A0:
  1824. case PXA250_A1:
  1825. /* A0/A1 "not released"; ep 13, 15 unusable */
  1826. /* fall through */
  1827. case PXA250_B2: case PXA210_B2:
  1828. case PXA250_B1: case PXA210_B1:
  1829. case PXA250_B0: case PXA210_B0:
  1830. /* OUT-DMA is broken ... */
  1831. /* fall through */
  1832. case PXA250_C0: case PXA210_C0:
  1833. break;
  1834. #elif defined(CONFIG_ARCH_IXP4XX)
  1835. case IXP425_A0:
  1836. case IXP425_B0:
  1837. case IXP465_AD:
  1838. dev->has_cfr = 1;
  1839. break;
  1840. #endif
  1841. default:
  1842. pr_err("%s: unrecognized processor: %08x\n",
  1843. driver_name, chiprev);
  1844. /* iop3xx, ixp4xx, ... */
  1845. return -ENODEV;
  1846. }
  1847. irq = platform_get_irq(pdev, 0);
  1848. if (irq < 0)
  1849. return -ENODEV;
  1850. dev->clk = clk_get(&pdev->dev, NULL);
  1851. if (IS_ERR(dev->clk)) {
  1852. retval = PTR_ERR(dev->clk);
  1853. goto err_clk;
  1854. }
  1855. pr_debug("%s: IRQ %d%s%s\n", driver_name, irq,
  1856. dev->has_cfr ? "" : " (!cfr)",
  1857. SIZE_STR "(pio)"
  1858. );
  1859. /* other non-static parts of init */
  1860. dev->dev = &pdev->dev;
  1861. dev->mach = pdev->dev.platform_data;
  1862. dev->transceiver = otg_get_transceiver();
  1863. if (gpio_is_valid(dev->mach->gpio_pullup)) {
  1864. if ((retval = gpio_request(dev->mach->gpio_pullup,
  1865. "pca25x_udc GPIO PULLUP"))) {
  1866. dev_dbg(&pdev->dev,
  1867. "can't get pullup gpio %d, err: %d\n",
  1868. dev->mach->gpio_pullup, retval);
  1869. goto err_gpio_pullup;
  1870. }
  1871. gpio_direction_output(dev->mach->gpio_pullup, 0);
  1872. }
  1873. init_timer(&dev->timer);
  1874. dev->timer.function = udc_watchdog;
  1875. dev->timer.data = (unsigned long) dev;
  1876. device_initialize(&dev->gadget.dev);
  1877. dev->gadget.dev.parent = &pdev->dev;
  1878. dev->gadget.dev.dma_mask = pdev->dev.dma_mask;
  1879. the_controller = dev;
  1880. platform_set_drvdata(pdev, dev);
  1881. udc_disable(dev);
  1882. udc_reinit(dev);
  1883. dev->vbus = 0;
  1884. /* irq setup after old hardware state is cleaned up */
  1885. retval = request_irq(irq, pxa25x_udc_irq,
  1886. IRQF_DISABLED, driver_name, dev);
  1887. if (retval != 0) {
  1888. pr_err("%s: can't get irq %d, err %d\n",
  1889. driver_name, irq, retval);
  1890. goto err_irq1;
  1891. }
  1892. dev->got_irq = 1;
  1893. #ifdef CONFIG_ARCH_LUBBOCK
  1894. if (machine_is_lubbock()) {
  1895. retval = request_irq(LUBBOCK_USB_DISC_IRQ,
  1896. lubbock_vbus_irq,
  1897. IRQF_DISABLED | IRQF_SAMPLE_RANDOM,
  1898. driver_name, dev);
  1899. if (retval != 0) {
  1900. pr_err("%s: can't get irq %i, err %d\n",
  1901. driver_name, LUBBOCK_USB_DISC_IRQ, retval);
  1902. goto err_irq_lub;
  1903. }
  1904. retval = request_irq(LUBBOCK_USB_IRQ,
  1905. lubbock_vbus_irq,
  1906. IRQF_DISABLED | IRQF_SAMPLE_RANDOM,
  1907. driver_name, dev);
  1908. if (retval != 0) {
  1909. pr_err("%s: can't get irq %i, err %d\n",
  1910. driver_name, LUBBOCK_USB_IRQ, retval);
  1911. goto lubbock_fail0;
  1912. }
  1913. } else
  1914. #endif
  1915. create_debug_files(dev);
  1916. return 0;
  1917. #ifdef CONFIG_ARCH_LUBBOCK
  1918. lubbock_fail0:
  1919. free_irq(LUBBOCK_USB_DISC_IRQ, dev);
  1920. err_irq_lub:
  1921. free_irq(irq, dev);
  1922. #endif
  1923. err_irq1:
  1924. if (gpio_is_valid(dev->mach->gpio_pullup))
  1925. gpio_free(dev->mach->gpio_pullup);
  1926. err_gpio_pullup:
  1927. if (dev->transceiver) {
  1928. otg_put_transceiver(dev->transceiver);
  1929. dev->transceiver = NULL;
  1930. }
  1931. clk_put(dev->clk);
  1932. err_clk:
  1933. return retval;
  1934. }
  1935. static void pxa25x_udc_shutdown(struct platform_device *_dev)
  1936. {
  1937. pullup_off();
  1938. }
  1939. static int __exit pxa25x_udc_remove(struct platform_device *pdev)
  1940. {
  1941. struct pxa25x_udc *dev = platform_get_drvdata(pdev);
  1942. if (dev->driver)
  1943. return -EBUSY;
  1944. dev->pullup = 0;
  1945. pullup(dev);
  1946. remove_debug_files(dev);
  1947. if (dev->got_irq) {
  1948. free_irq(platform_get_irq(pdev, 0), dev);
  1949. dev->got_irq = 0;
  1950. }
  1951. #ifdef CONFIG_ARCH_LUBBOCK
  1952. if (machine_is_lubbock()) {
  1953. free_irq(LUBBOCK_USB_DISC_IRQ, dev);
  1954. free_irq(LUBBOCK_USB_IRQ, dev);
  1955. }
  1956. #endif
  1957. if (gpio_is_valid(dev->mach->gpio_pullup))
  1958. gpio_free(dev->mach->gpio_pullup);
  1959. clk_put(dev->clk);
  1960. if (dev->transceiver) {
  1961. otg_put_transceiver(dev->transceiver);
  1962. dev->transceiver = NULL;
  1963. }
  1964. platform_set_drvdata(pdev, NULL);
  1965. the_controller = NULL;
  1966. return 0;
  1967. }
  1968. /*-------------------------------------------------------------------------*/
  1969. #ifdef CONFIG_PM
  1970. /* USB suspend (controlled by the host) and system suspend (controlled
  1971. * by the PXA) don't necessarily work well together. If USB is active,
  1972. * the 48 MHz clock is required; so the system can't enter 33 MHz idle
  1973. * mode, or any deeper PM saving state.
  1974. *
  1975. * For now, we punt and forcibly disconnect from the USB host when PXA
  1976. * enters any suspend state. While we're disconnected, we always disable
  1977. * the 48MHz USB clock ... allowing PXA sleep and/or 33 MHz idle states.
  1978. * Boards without software pullup control shouldn't use those states.
  1979. * VBUS IRQs should probably be ignored so that the PXA device just acts
  1980. * "dead" to USB hosts until system resume.
  1981. */
  1982. static int pxa25x_udc_suspend(struct platform_device *dev, pm_message_t state)
  1983. {
  1984. struct pxa25x_udc *udc = platform_get_drvdata(dev);
  1985. unsigned long flags;
  1986. if (!gpio_is_valid(udc->mach->gpio_pullup) && !udc->mach->udc_command)
  1987. WARNING("USB host won't detect disconnect!\n");
  1988. udc->suspended = 1;
  1989. local_irq_save(flags);
  1990. pullup(udc);
  1991. local_irq_restore(flags);
  1992. return 0;
  1993. }
  1994. static int pxa25x_udc_resume(struct platform_device *dev)
  1995. {
  1996. struct pxa25x_udc *udc = platform_get_drvdata(dev);
  1997. unsigned long flags;
  1998. udc->suspended = 0;
  1999. local_irq_save(flags);
  2000. pullup(udc);
  2001. local_irq_restore(flags);
  2002. return 0;
  2003. }
  2004. #else
  2005. #define pxa25x_udc_suspend NULL
  2006. #define pxa25x_udc_resume NULL
  2007. #endif
  2008. /*-------------------------------------------------------------------------*/
  2009. static struct platform_driver udc_driver = {
  2010. .shutdown = pxa25x_udc_shutdown,
  2011. .remove = __exit_p(pxa25x_udc_remove),
  2012. .suspend = pxa25x_udc_suspend,
  2013. .resume = pxa25x_udc_resume,
  2014. .driver = {
  2015. .owner = THIS_MODULE,
  2016. .name = "pxa25x-udc",
  2017. },
  2018. };
  2019. static int __init udc_init(void)
  2020. {
  2021. pr_info("%s: version %s\n", driver_name, DRIVER_VERSION);
  2022. return platform_driver_probe(&udc_driver, pxa25x_udc_probe);
  2023. }
  2024. module_init(udc_init);
  2025. static void __exit udc_exit(void)
  2026. {
  2027. platform_driver_unregister(&udc_driver);
  2028. }
  2029. module_exit(udc_exit);
  2030. MODULE_DESCRIPTION(DRIVER_DESC);
  2031. MODULE_AUTHOR("Frank Becker, Robert Schwebel, David Brownell");
  2032. MODULE_LICENSE("GPL");
  2033. MODULE_ALIAS("platform:pxa25x-udc");