pxa25x_udc.c 59 KB

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